mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-03 09:37:19 +01:00
<!-- Write all pull request text in Simplified Technical English (ASD-STE100). --> ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Task descriptions, comments, continuation data, skills, and execution rules enter several agent adapters. > - The same source can be rendered by more than one automatic input carrier. > - Failed resumes can also rebuild input from stale or compact context. > - This pull request gives each Paperclip-owned source one delivery owner and preserves the required transport boundaries. > - It adds deterministic adapter, interaction, runner, and browser tests for these boundaries. > - The benefit is more predictable context delivery with explicit evidence for later live qualification. ## Linked Issues or Issue Description Related: #13144 removes a duplicate environment payload and bounds wake lists. Related: #11360 addresses Hermes resume behavior. This pull request preserves compatible active-session formats while repairing context ownership and stale question creation. **What happened?** Task descriptions and comments could enter more than one automatic context block. Native transports could wrap a complete model input in a second task envelope. Some legacy and gateway adapters could omit the owned assignment on ordinary tasks or rebuild a failed resume with stale compact context. A continuation could also request a question after newer human comments had arrived. **Expected behavior** Each task or comment source has one automatic model-facing owner. Distinct comment IDs and repeated wording remain distinct. Fresh fallback attempts rebuild the required full context. A question request is rejected when newer queued human direction makes it stale. Harness access policy remains owned by execution configuration. **Steps to reproduce** 1. Build a task with a description and current comments. 2. Capture the actual adapter or runner input. 3. Compare source ownership and task-envelope nesting. 4. Queue a human comment before a continuation requests a question. 5. Trigger a failed resume and inspect the fresh retry input. 6. Run the focused adapter, interaction, runner, and browser checks. ## What Changed - Add shared prompt-section selection at the provider-attempt boundary. - Deliver owned assignment context through native, legacy CLI, ACP, gateway, cloud, Pi, Kimi, Grok, Gemini, OpenCode, Cursor, OpenClaw, and Hermes paths. - Rebuild full or compact context after resume recovery changes the attempt. Add native and Claude ACP tests of actual recovery requests. - Preserve custom templates, loaded instruction files, execution policies, and older active-session formats. - Record continuation source metadata and reject stale question creation under the issue-row lock. - Add explicit Product E2E context-integrity profiles, prerequisite gates, credential-isolation checks, and report fixtures. - Bypass service-worker forwarding for same-origin Vite development modules. A real Chromium test fails with resource exhaustion before the repair and passes after it. Production asset caching keeps its existing policy. - Add browser diagnostics and service-worker module-loading regressions. - Add an explicit zero-retry eval option. The default retry behavior remains unchanged. Each campaign records its effective policy. - Remove the model-facing working-directory sentence from four prompt builders. Existing workspace, sandbox, permission, and custom-template configuration remains unchanged. - Align the everyday workflow assertion with the current 47-entry catalog. Compared with current upstream master, the branch carries the context-ownership implementation and its tests, the explicit context-integrity catalog and evidence harness, and the focused browser regression checks. ## Verification **Merge assessment:** focused regression evidence supports merge. This is not full completion of the original broad qualification matrix. The maintainer has authorized merge after fresh verification of the master integration. - Current head: `bbd52f82114eabf09bc7b1a7e97d54a5b43bbc00`. This integrates current master `2f585ef26a1814fa209715242d1ca791b63e4c4e`. All 14 conflicts are resolved. Cancellation checks, workspace finalization, native Grok support, and both sets of tests are retained. - Current-head Greptile: **5/5**, with no blocking findings. The review names this exact commit. All **59 reported checks are terminal: 55 successful, 4 skipped, zero pending or failing**. This includes the full root general and serialized suites, separate runner checks, typecheck, build, canary, browser E2E, Docker, and security checks. The successful legacy security status is included in that total. - After integration: workspace typecheck and full build passed. Separate runner checks passed: **2,160 TypeScript tests (10 skipped), 582 Rust tests, and 39 preparation checks**. Other passing checks include 621 Product E2E harness units, 376 focused shared/adapter tests, 160 real-database/API tests, 86 Hermes tests, 18 browser-support checks, and Product E2E typechecking. The complete root suite passed in CI. The duplicate local monolithic root run was stopped after that CI result; it is not counted as a completed local pass. - New native recovery coverage retains full assignment, completion contract, and explicit skill selection after safe replacement, for old and prepared input formats. Full native session test file: **136/136 passed**. - New Claude ACP coverage captures actual fresh, resumed, and missing-session fallback requests. It verifies one assignment copy, comment order, identical text under distinct comment IDs, and full fallback context. Full file: **33/33 passed**. Both affected TypeScript checks passed. - Existing deterministic tests cover source revisions, approval and trust boundaries, completion validation, custom templates, compatible sessions, standalone driver wrapping, and maintained adapter transport requests. - Provider-free browser support: **17/17 passed** after the master merge. Service-worker unit tests: **33/33 passed**. The module-overload regression failed before the repair and passed after it in real Chromium. ### Fresh live comparisons The new batch ran exactly four Product E2E attempts. **All four passed on the first attempt; no retries.** Each has six terminal matchers plus the existing browser lifecycle and invariant checks. | Exact case ID | Control | Candidate | |---|---|---| | `core-compatibility.runner-codex.local.plan-revise-accept` | Passed | Passed | | `local-session-integrity.runner-acpx-claude.local.structured-question-restart-resume` | Passed | Passed | The plan case checks a revised canonical plan and revision-bound approval before completion. The question case restarts the server before submitting the answer, then verifies the continuation completes. Control source is `dfa4e1bda8d50a1a01746603251a9128dbe9d0d6`. Candidate source is `79fcdb5dece501d28064ea9da306603881b46f0c`. They use identical frozen definitions and provider versions: Codex `0.156.0` with `gpt-5.6-sol`; ACPX `0.13.1` / Claude ACP `0.73.0` with `claude-sonnet-5`. The September 24 head added master browser recovery and test-only changes. The September 28 head also integrates newer master changes, including cancellation, workspace finalization, and native Grok. These are frozen-source live results, not exact-head live runs. The candidate received one description copy where the control initially received three. The submitted initial plan envelopes were 7,969 versus 19,097 characters. Question envelopes were 7,592 versus 18,919. These are structural measurements, not whole-provider token or dollar savings. ### Earlier evidence and failed attempts - The preceding fresh batch has four effective passing pairs: OpenCode comment continuation and assigned skill, native Codex comment continuation, and native Claude comment continuation. It retains **11 attempts: eight passed and three failed**. - Original failures remain recorded: missing local PostgreSQL library links before task creation; host-sleep cleanup after task/page checks passed; and a Claude **control** session-open rejection before a model turn. Setup was repaired identically on both worktrees. The permitted unchanged infrastructure retries passed. The underlying Claude provider startup error was not retained and remains unknown. - Older R2 retains **17 passes and one failure** across 18 attempts, including eight both-pass native/legacy Codex/Claude pairs. Its OpenCode blank-page failure led to the service-worker repair. R2 is historical evidence: master changed the native fixed prompt and removed duplicate wake environment data afterward. - The September 24 CI run initially failed one unrelated preview readiness test (`ECONNREFUSED` on its local fixture). Its test and production code match master. Isolated local verification passed **28 tests, 3 skipped**. One unchanged CI retry passed the full shard: **831 passed, 1 skipped**, including all **31 preview-exposure tests**. The aggregate CI gate passed afterward. The precise startup cause remains unknown; a port race is a hypothesis, not a proved cause. ### Limits The original wider profile/workflow matrix, repeated trials, and remote Daytona qualification are incomplete. These results support a focused merge recommendation, not statistical equivalence or universal harness qualification. Some usage receipts are missing in both variants, so no token or dollar savings are claimed. The $500 ceiling was preserved using conservative allowances; failed attempts and unknown charges remain in the ledger. Reproduce the focused additions with `pnpm exec vitest run packages/adapters/claude-local/src/server/acp.test.ts` and `pnpm --filter @paperclipai/paperclip-runner exec vitest run src/native-session-runtime.test.ts`. Full checks use `pnpm -r typecheck`, `pnpm test:run`, `pnpm build`, and the separate runner checks. Paid evals require the frozen definitions, profiles, and credentials; do not use `--all` as a substitute for the selected cases. ## Risks - Context placement changes can affect model behavior. Deterministic checks cover the selected paths, but live qualification remains incomplete. - The stale-question guard can reject a request when queued human comments arrived during the run. This is intended. - New stored inputs and model envelopes retain compatibility readers for older active sessions. - Custom templates may intentionally repeat content. - Removing a model-facing working-directory sentence does not change filesystem, command, sandbox, or permission configuration. - The worker bypass applies only to same-origin development module paths. Cache-policy tests preserve private-response handling and production asset caching. Mounted HTTP fixture changes remain test-only. - This PR does not claim measured token savings or statistical equivalence across every harness. ## Model Used OpenAI Codex, exact model gpt-6-astra, with repository tools and code execution. Bounded supporting work used gpt-5.6-luna and gpt-6-luna. The serving context-window size is not exposed in this task. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have described the issue in-PR using the required issue fields - [x] I have not referenced internal/instance-local Paperclip issues or links - [x] My branch name describes the change and contains no internal ticket id - [x] I have run the focused local checks and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect these changes - [x] I have considered and documented risks above - [x] All current-head Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups for the current head - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
1426 lines
51 KiB
TypeScript
1426 lines
51 KiB
TypeScript
import fs from "node:fs/promises";
|
|
import { randomUUID } from "node:crypto";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import type { AdapterExecutionContext, AdapterInvocationMeta } from "@paperclipai/adapter-utils";
|
|
import { runChildProcess } from "@paperclipai/adapter-utils/server-utils";
|
|
|
|
// Every test in this file needs a real teardown, so the mock below delegates
|
|
// to the actual factory by default. Only the wiring test further down reads
|
|
// the call arguments; it does not change this behavior.
|
|
const mockCreateWorkspaceRestoreTeardown = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock("@paperclipai/adapter-utils/workspace-restore-teardown", async (importOriginal) => {
|
|
const actual = await importOriginal<Record<string, unknown>>();
|
|
mockCreateWorkspaceRestoreTeardown.mockImplementation(
|
|
actual.createWorkspaceRestoreTeardown as (...args: unknown[]) => unknown,
|
|
);
|
|
return {
|
|
...actual,
|
|
createWorkspaceRestoreTeardown: mockCreateWorkspaceRestoreTeardown,
|
|
};
|
|
});
|
|
|
|
import {
|
|
buildCodexAcpConfig,
|
|
createCodexAcpExecutor,
|
|
nodeVersionMeetsCodexAcpMinimum,
|
|
resolveCodexAcpBillingIdentity,
|
|
resolveCodexExecutionEngine,
|
|
resolveCodexExecutionEngineForRun,
|
|
testCodexAcpEnvironment,
|
|
} from "./acp.js";
|
|
|
|
// A local stand-in for a sandbox runner: runs the managed-runtime staging
|
|
// scripts (mkdir/tar/find) as real child processes so the remote ACP lane can
|
|
// be exercised end-to-end against the host filesystem.
|
|
function createLocalSandboxRunner() {
|
|
let counter = 0;
|
|
return {
|
|
execute: async (input: {
|
|
command: string;
|
|
args?: string[];
|
|
cwd?: string;
|
|
env?: Record<string, string>;
|
|
stdin?: string;
|
|
timeoutMs?: number;
|
|
onLog?: (stream: "stdout" | "stderr", chunk: string) => Promise<void>;
|
|
}) => {
|
|
counter += 1;
|
|
const command = input.command === "bash" ? "/bin/bash" : input.command;
|
|
return await runChildProcess(`codex-acp-sandbox-run-${counter}`, command, input.args ?? [], {
|
|
cwd: input.cwd ?? process.cwd(),
|
|
env: input.env ?? {},
|
|
stdin: input.stdin,
|
|
timeoutSec: Math.max(1, Math.ceil((input.timeoutMs ?? 30_000) / 1000)),
|
|
graceSec: 5,
|
|
onLog: input.onLog ?? (async () => {}),
|
|
});
|
|
},
|
|
};
|
|
}
|
|
|
|
type FakeRuntimeOptions = Record<string, unknown>;
|
|
type FakeRuntimeEvent = { type: string; text?: string; stream?: string; tag?: string };
|
|
type FakeRuntimeHandle = {
|
|
sessionKey: string;
|
|
backend: string;
|
|
runtimeSessionName: string;
|
|
cwd?: string;
|
|
acpxRecordId: string;
|
|
backendSessionId: string;
|
|
agentSessionId: string;
|
|
};
|
|
type FakeRuntimeTurnResult = { status: "completed" | "failed" | "cancelled"; stopReason?: string };
|
|
type FakeRuntimeTurn = {
|
|
requestId: string;
|
|
events: AsyncIterable<FakeRuntimeEvent>;
|
|
result: Promise<FakeRuntimeTurnResult>;
|
|
cancel: () => Promise<void>;
|
|
closeStream: () => Promise<void>;
|
|
};
|
|
|
|
const tempRoots: string[] = [];
|
|
const originalNodeVersion = process.version;
|
|
const originalPaperclipHome = process.env.PAPERCLIP_HOME;
|
|
const originalPaperclipInstanceId = process.env.PAPERCLIP_INSTANCE_ID;
|
|
const originalCodexHome = process.env.CODEX_HOME;
|
|
const originalOpenAiApiKey = process.env.OPENAI_API_KEY;
|
|
|
|
// Older/newer ISO timestamps for the copy-back monotonic (strictly-newer)
|
|
// decision predicate, plus a subscription-shaped auth.json fixture matching the
|
|
// predicate's parseAuth contract (tokens.account_id + token material +
|
|
// last_refresh).
|
|
const OLDER_REFRESH = "2026-01-01T00:00:00.000Z";
|
|
const NEWER_REFRESH = "2026-06-01T00:00:00.000Z";
|
|
|
|
function subscriptionAuthJson(accountId: string, lastRefresh: string, marker: string): string {
|
|
return JSON.stringify(
|
|
{
|
|
tokens: {
|
|
id_token: `id-${marker}`,
|
|
access_token: `acc-${marker}`,
|
|
refresh_token: `ref-${marker}`,
|
|
account_id: accountId,
|
|
},
|
|
last_refresh: lastRefresh,
|
|
},
|
|
null,
|
|
2,
|
|
);
|
|
}
|
|
|
|
// Enumerate the host staged-home temp dirs `stageCodexHomeForSync` created for a
|
|
// given runId (`paperclip-codex-home-sync-<runId>-<random>` under os.tmpdir()).
|
|
// A unique per-test runId scopes the match to this run's staging dirs only, so
|
|
// the assertion is not disturbed by other tests/processes sharing the tmp dir.
|
|
async function listCodexHomeSyncDirs(runId: string): Promise<string[]> {
|
|
const prefix = `paperclip-codex-home-sync-${runId}-`;
|
|
const entries = await fs.readdir(os.tmpdir());
|
|
return entries.filter((name) => name.startsWith(prefix)).map((name) => path.join(os.tmpdir(), name));
|
|
}
|
|
|
|
function setNodeVersion(version: string): void {
|
|
Object.defineProperty(process, "version", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
value: version,
|
|
});
|
|
}
|
|
|
|
afterEach(async () => {
|
|
setNodeVersion(originalNodeVersion);
|
|
if (originalPaperclipHome === undefined) delete process.env.PAPERCLIP_HOME;
|
|
else process.env.PAPERCLIP_HOME = originalPaperclipHome;
|
|
if (originalPaperclipInstanceId === undefined) delete process.env.PAPERCLIP_INSTANCE_ID;
|
|
else process.env.PAPERCLIP_INSTANCE_ID = originalPaperclipInstanceId;
|
|
if (originalCodexHome === undefined) delete process.env.CODEX_HOME;
|
|
else process.env.CODEX_HOME = originalCodexHome;
|
|
if (originalOpenAiApiKey === undefined) delete process.env.OPENAI_API_KEY;
|
|
else process.env.OPENAI_API_KEY = originalOpenAiApiKey;
|
|
await Promise.all(tempRoots.splice(0).map((root) => fs.rm(root, { recursive: true, force: true })));
|
|
});
|
|
|
|
class FakeRuntime {
|
|
ensureInputs: Array<{
|
|
sessionKey: string;
|
|
agent: string;
|
|
mode: "persistent" | "oneshot";
|
|
cwd?: string;
|
|
resumeSessionId?: string;
|
|
}> = [];
|
|
startInputs: Array<{ handle: FakeRuntimeHandle; text: string; requestId: string; timeoutMs?: number }> = [];
|
|
closeInputs: Array<{ handle: FakeRuntimeHandle; reason: string; discardPersistentState?: boolean }> = [];
|
|
setConfigInputs: Array<{ handle: FakeRuntimeHandle; key: string; value: string }> = [];
|
|
ensureCount = 0;
|
|
|
|
constructor(
|
|
readonly options: FakeRuntimeOptions,
|
|
readonly events: FakeRuntimeEvent[] = [
|
|
{ type: "text_delta", text: "hello", stream: "output", tag: "agent_message_chunk" },
|
|
],
|
|
readonly terminal: FakeRuntimeTurnResult = { status: "completed", stopReason: "end_turn" },
|
|
) {}
|
|
|
|
async ensureSession(input: {
|
|
sessionKey: string;
|
|
agent: string;
|
|
mode: "persistent" | "oneshot";
|
|
cwd?: string;
|
|
resumeSessionId?: string;
|
|
}): Promise<FakeRuntimeHandle> {
|
|
this.ensureInputs.push(input);
|
|
this.ensureCount += 1;
|
|
return {
|
|
sessionKey: input.sessionKey,
|
|
backend: "acpx",
|
|
runtimeSessionName: `runtime-${this.ensureCount}`,
|
|
cwd: input.cwd,
|
|
acpxRecordId: `record-${this.ensureCount}`,
|
|
backendSessionId: `acp-${this.ensureCount}`,
|
|
agentSessionId: `agent-${this.ensureCount}`,
|
|
};
|
|
}
|
|
|
|
startTurn(input: {
|
|
handle: FakeRuntimeHandle;
|
|
text: string;
|
|
requestId: string;
|
|
timeoutMs?: number;
|
|
}): FakeRuntimeTurn {
|
|
this.startInputs.push(input);
|
|
const events = this.events;
|
|
const terminal = this.terminal;
|
|
return {
|
|
requestId: input.requestId,
|
|
events: {
|
|
[Symbol.asyncIterator]: async function* () {
|
|
for (const event of events) yield event;
|
|
},
|
|
},
|
|
result: Promise.resolve(terminal),
|
|
cancel: async () => {},
|
|
closeStream: async () => {},
|
|
};
|
|
}
|
|
|
|
runTurn(): AsyncIterable<FakeRuntimeEvent> {
|
|
throw new Error("not used");
|
|
}
|
|
|
|
getCapabilities() {
|
|
return { controls: [] };
|
|
}
|
|
|
|
getStatus() {
|
|
return Promise.resolve({});
|
|
}
|
|
|
|
async setConfigOption(input: { handle: FakeRuntimeHandle; key: string; value: string }) {
|
|
this.setConfigInputs.push(input);
|
|
}
|
|
|
|
async setMode() {}
|
|
|
|
async cancel() {}
|
|
|
|
async close(input: { handle: FakeRuntimeHandle; reason: string; discardPersistentState?: boolean }) {
|
|
this.closeInputs.push(input);
|
|
}
|
|
}
|
|
|
|
async function makeTempRoot(prefix: string) {
|
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
|
|
tempRoots.push(root);
|
|
process.env.PAPERCLIP_HOME = path.join(root, "paperclip-home");
|
|
process.env.PAPERCLIP_INSTANCE_ID = "test";
|
|
return root;
|
|
}
|
|
|
|
async function createRuntimeSkill(root: string) {
|
|
const source = path.join(root, "skills", "review");
|
|
await fs.mkdir(source, { recursive: true });
|
|
await fs.writeFile(path.join(source, "SKILL.md"), "---\n---\nUse the review skill.\n", "utf8");
|
|
return {
|
|
key: "company/review",
|
|
runtimeName: "review",
|
|
source,
|
|
};
|
|
}
|
|
|
|
function buildContext(root: string, overrides: Partial<AdapterExecutionContext> = {}): AdapterExecutionContext {
|
|
return {
|
|
runId: "run-1",
|
|
agent: {
|
|
id: "agent-1",
|
|
companyId: "company-1",
|
|
name: "Codex ACP",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
},
|
|
runtime: {
|
|
sessionId: null,
|
|
sessionParams: null,
|
|
sessionDisplayId: null,
|
|
taskKey: "PAP-1",
|
|
},
|
|
config: {
|
|
engine: "acp",
|
|
cwd: root,
|
|
stateDir: path.join(root, "state"),
|
|
env: {
|
|
CODEX_HOME: path.join(root, "codex-home"),
|
|
},
|
|
promptTemplate: "Do the assigned work.",
|
|
},
|
|
context: {
|
|
issueId: "issue-1",
|
|
paperclipTaskMarkdown: "Task context",
|
|
paperclipWorkspace: {
|
|
cwd: root,
|
|
source: "project_workspace",
|
|
workspaceId: "workspace-1",
|
|
},
|
|
},
|
|
onLog: async () => {},
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("codex_local ACP lane", () => {
|
|
it("keeps ACP selected and reports unavailable prerequisites for default and explicit engines", async () => {
|
|
const root = await makeTempRoot("paperclip-codex-acp-default-");
|
|
const commandPath = path.join(root, "bin", "codex-acp");
|
|
await fs.mkdir(path.dirname(commandPath), { recursive: true });
|
|
await fs.writeFile(commandPath, "#!/usr/bin/env sh\n", "utf8");
|
|
setNodeVersion("v24.11.0");
|
|
|
|
expect(resolveCodexExecutionEngine({})).toEqual({ engine: "acp", explicit: false });
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({
|
|
config: { agentCommand: commandPath },
|
|
executionTarget: null,
|
|
}),
|
|
).resolves.toEqual({ engine: "acp", explicit: false });
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({
|
|
config: { engine: "cli", agentCommand: commandPath },
|
|
executionTarget: null,
|
|
}),
|
|
).resolves.toEqual({ engine: "cli", explicit: true });
|
|
expect(resolveCodexExecutionEngine({ engine: "acp" })).toEqual({
|
|
engine: "acp",
|
|
explicit: true,
|
|
});
|
|
|
|
setNodeVersion("v24.10.0");
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({
|
|
config: { agentCommand: commandPath },
|
|
executionTarget: null,
|
|
}),
|
|
).resolves.toMatchObject({
|
|
engine: "acp",
|
|
explicit: false,
|
|
unavailableReason: expect.stringContaining("Node"),
|
|
});
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({
|
|
config: { engine: "acp", agentCommand: "/missing/codex-acp" },
|
|
executionTarget: null,
|
|
}),
|
|
).resolves.toMatchObject({ engine: "acp", explicit: true, unavailableReason: expect.stringContaining("Node") });
|
|
});
|
|
|
|
it("requires explicit CLI selection for local filesystem or network scope", async () => {
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({
|
|
config: { filesystemScope: "workspace" },
|
|
executionTarget: null,
|
|
}),
|
|
).resolves.toMatchObject({
|
|
engine: "acp",
|
|
explicit: false,
|
|
unavailableReason: expect.stringContaining("confinement"),
|
|
});
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({
|
|
config: { engine: "acp", filesystemScope: "workspace" },
|
|
executionTarget: null,
|
|
}),
|
|
).resolves.toMatchObject({ engine: "acp", unavailableReason: expect.stringContaining("ACP confinement is not supported") });
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({
|
|
config: { networkScope: "allowlist" },
|
|
executionTarget: null,
|
|
}),
|
|
).resolves.toMatchObject({
|
|
engine: "acp",
|
|
explicit: false,
|
|
unavailableReason: expect.stringContaining("confinement"),
|
|
});
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({
|
|
config: { filesystemScope: "workpace" },
|
|
executionTarget: null,
|
|
}),
|
|
).rejects.toThrow('filesystemScope must be "workspace"');
|
|
});
|
|
|
|
it("requires explicit CLI selection for in-place realization", async () => {
|
|
const executionTarget = {
|
|
kind: "remote" as const,
|
|
transport: "sandbox" as const,
|
|
remoteCwd: "/app",
|
|
workspaceRealization: {
|
|
mode: "in_place" as const,
|
|
authoritativeRoot: "/app",
|
|
pathAliases: [],
|
|
outboundRestorePaths: [],
|
|
},
|
|
};
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({ config: {}, executionTarget }),
|
|
).resolves.toMatchObject({
|
|
engine: "acp",
|
|
explicit: false,
|
|
unavailableReason: expect.stringContaining("ACP archive staging"),
|
|
});
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({ config: { engine: "acp" }, executionTarget }),
|
|
).resolves.toMatchObject({ engine: "acp", unavailableReason: expect.stringContaining("In-place workspace realization requires the Codex CLI engine") });
|
|
});
|
|
|
|
it("uses ACP for bridged sandbox auto runs when the ACP command is configured as a shell command", async () => {
|
|
setNodeVersion("v24.11.0");
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({
|
|
config: { agentCommand: "codex-acp" },
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
providerKey: "fake-plugin",
|
|
remoteCwd: "/work",
|
|
runner: {
|
|
execute: async () => ({
|
|
exitCode: 0,
|
|
signal: null,
|
|
timedOut: false,
|
|
stdout: "",
|
|
stderr: "",
|
|
pid: null,
|
|
startedAt: new Date().toISOString(),
|
|
}),
|
|
},
|
|
},
|
|
}),
|
|
).resolves.toEqual({ engine: "acp", explicit: false });
|
|
});
|
|
|
|
it("reports unavailable ACP for one-shot sandbox auto runs", async () => {
|
|
setNodeVersion("v24.11.0");
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({
|
|
config: {},
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
providerKey: "fake-plugin",
|
|
remoteCwd: "/work",
|
|
},
|
|
}),
|
|
).resolves.toMatchObject({
|
|
engine: "acp",
|
|
explicit: false,
|
|
unavailableReason: expect.stringContaining("bidirectional remote process"),
|
|
});
|
|
});
|
|
|
|
it("reports unavailable ACP for non-sandbox remote auto runs", async () => {
|
|
setNodeVersion("v24.11.0");
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({
|
|
config: {},
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "ssh",
|
|
remoteCwd: "/work",
|
|
spec: {
|
|
host: "127.0.0.1",
|
|
port: 22,
|
|
username: "fixture",
|
|
remoteCwd: "/work",
|
|
remoteWorkspacePath: "/work",
|
|
privateKey: null,
|
|
knownHosts: null,
|
|
strictHostKeyChecking: true,
|
|
},
|
|
},
|
|
}),
|
|
).resolves.toMatchObject({
|
|
engine: "acp",
|
|
explicit: false,
|
|
unavailableReason: expect.stringContaining("sandbox remote targets only"),
|
|
});
|
|
});
|
|
|
|
it("enables workspace networking for ACP without changing other env settings", () => {
|
|
expect(buildCodexAcpConfig({ env: { CUSTOM: "kept" } })).toMatchObject({
|
|
env: { CUSTOM: "kept", PAPERCLIP_CODEX_ACP_NETWORK_ACCESS: "true" },
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
{ env: { PAPERCLIP_CODEX_ACP_NETWORK_ACCESS: "false" } },
|
|
{ extraArgs: ["-c", "sandbox_workspace_write.network_access=false"] },
|
|
{ extraArgs: ["--config=sandbox_workspace_write.network_access=false"] },
|
|
{ args: ["-csandbox_workspace_write.network_access=false"] },
|
|
{ extraArgs: ["-c", "sandbox_workspace_write.network_access=true", "-c", "sandbox_workspace_write.network_access=false"] },
|
|
])("preserves explicit ACP network denial %j", (config) => {
|
|
expect(buildCodexAcpConfig(config)).toMatchObject({
|
|
env: { PAPERCLIP_CODEX_ACP_NETWORK_ACCESS: "false" },
|
|
});
|
|
});
|
|
|
|
it("maps Codex config to the ACPX Codex target", () => {
|
|
expect(buildCodexAcpConfig({
|
|
engine: "acp",
|
|
cwd: "/repo",
|
|
command: "codex",
|
|
model: "gpt-5.5",
|
|
modelReasoningEffort: "high",
|
|
fastMode: true,
|
|
agentCommand: "custom-codex-acp",
|
|
warmHandleIdleMs: 25,
|
|
})).toMatchObject({
|
|
agent: "codex",
|
|
cwd: "/repo",
|
|
command: "codex",
|
|
model: "gpt-5.5",
|
|
modelReasoningEffort: "high",
|
|
fastMode: true,
|
|
agentCommand: "custom-codex-acp",
|
|
mode: "persistent",
|
|
permissionMode: "approve-all",
|
|
nonInteractivePermissions: "deny",
|
|
warmHandleIdleMs: 25,
|
|
});
|
|
});
|
|
|
|
it.each([["gpt-6-astra", "ultra"], ["gpt-6-sol", "ultra"], ["gpt-6-luna", "max"], ["gpt-5.6-sol", "ultra"], ["gpt-5.6-terra", "ultra"], ["gpt-5.6-luna", "max"]])("forwards %s controls to the ACPX Codex target", (model, effort) => {
|
|
expect(buildCodexAcpConfig({
|
|
engine: "acp",
|
|
model,
|
|
modelReasoningEffort: effort,
|
|
fastMode: true,
|
|
})).toMatchObject({
|
|
model,
|
|
modelReasoningEffort: effort,
|
|
fastMode: true,
|
|
});
|
|
});
|
|
|
|
it("normalizes the legacy bare gpt-5.6 alias to gpt-5.6-sol", () => {
|
|
expect(buildCodexAcpConfig({ engine: "acp", model: "gpt-5.6" })).toMatchObject({
|
|
model: "gpt-5.6-sol",
|
|
});
|
|
});
|
|
|
|
it("checks the Node version required by the ACPX runtime", () => {
|
|
setNodeVersion("v24.10.0");
|
|
expect(nodeVersionMeetsCodexAcpMinimum()).toBe(false);
|
|
setNodeVersion("v24.11.0");
|
|
expect(nodeVersionMeetsCodexAcpMinimum()).toBe(true);
|
|
});
|
|
|
|
it("reports ACP prerequisites for the ACP lane", async () => {
|
|
const root = await makeTempRoot("paperclip-codex-acp-env-");
|
|
const commandPath = path.join(root, "bin", "codex-acp");
|
|
await fs.mkdir(path.dirname(commandPath), { recursive: true });
|
|
await fs.writeFile(commandPath, "#!/usr/bin/env sh\n", "utf8");
|
|
setNodeVersion("v24.11.0");
|
|
|
|
const result = await testCodexAcpEnvironment({
|
|
adapterType: "codex_local",
|
|
companyId: "company-1",
|
|
config: {
|
|
engine: "acp",
|
|
cwd: root,
|
|
agentCommand: commandPath,
|
|
env: { OPENAI_API_KEY: "test-key" },
|
|
},
|
|
});
|
|
|
|
expect(result.status).toBe("pass");
|
|
expect(result.checks).toContainEqual(
|
|
expect.objectContaining({
|
|
code: "codex_engine_selected",
|
|
level: "info",
|
|
}),
|
|
);
|
|
expect(result.checks).toContainEqual(
|
|
expect.objectContaining({
|
|
code: "codex_acp_command_resolvable",
|
|
level: "info",
|
|
}),
|
|
);
|
|
expect(result.checks).toContainEqual(
|
|
expect.objectContaining({
|
|
code: "codex_acp_runtime_scaffold",
|
|
level: "info",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("detects shared managed Codex auth in ACP environment tests", async () => {
|
|
const root = await makeTempRoot("paperclip-codex-acp-managed-auth-");
|
|
const commandPath = path.join(root, "bin", "codex-acp");
|
|
const sharedCodexHome = path.join(root, "shared-codex-home");
|
|
const managedAgentHome = path.join(
|
|
root,
|
|
"paperclip-home",
|
|
"instances",
|
|
"test",
|
|
"companies",
|
|
"company-1",
|
|
"agents",
|
|
"agent-1",
|
|
"codex-home",
|
|
);
|
|
await fs.mkdir(path.dirname(commandPath), { recursive: true });
|
|
await fs.writeFile(commandPath, "#!/usr/bin/env sh\n", "utf8");
|
|
await fs.mkdir(sharedCodexHome, { recursive: true });
|
|
await fs.writeFile(path.join(sharedCodexHome, "auth.json"), '{"OPENAI_API_KEY":"sk-shared"}', "utf8");
|
|
setNodeVersion("v24.11.0");
|
|
process.env.CODEX_HOME = sharedCodexHome;
|
|
delete process.env.OPENAI_API_KEY;
|
|
|
|
const result = await testCodexAcpEnvironment({
|
|
adapterType: "codex_local",
|
|
companyId: "company-1",
|
|
config: {
|
|
engine: "acp",
|
|
cwd: root,
|
|
agentCommand: commandPath,
|
|
env: { CODEX_HOME: managedAgentHome, OPENAI_API_KEY: "" },
|
|
},
|
|
});
|
|
|
|
expect(result.status).toBe("pass");
|
|
expect(result.checks).toContainEqual(
|
|
expect.objectContaining({
|
|
code: "codex_acp_native_auth_detected",
|
|
level: "info",
|
|
detail: expect.stringContaining(sharedCodexHome),
|
|
}),
|
|
);
|
|
expect(result.checks).not.toContainEqual(
|
|
expect.objectContaining({
|
|
code: "codex_acp_credentials_missing",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("explains the Paperclip server credential boundary when ACP auth is missing", async () => {
|
|
const root = await makeTempRoot("paperclip-codex-acp-missing-auth-");
|
|
const commandPath = path.join(root, "bin", "codex-acp");
|
|
const sharedCodexHome = path.join(root, "shared-codex-home");
|
|
const managedAgentHome = path.join(
|
|
root,
|
|
"paperclip-home",
|
|
"instances",
|
|
"test",
|
|
"companies",
|
|
"company-1",
|
|
"agents",
|
|
"agent-1",
|
|
"codex-home",
|
|
);
|
|
await fs.mkdir(path.dirname(commandPath), { recursive: true });
|
|
await fs.writeFile(commandPath, "#!/usr/bin/env sh\n", "utf8");
|
|
await fs.mkdir(sharedCodexHome, { recursive: true });
|
|
setNodeVersion("v24.11.0");
|
|
process.env.CODEX_HOME = sharedCodexHome;
|
|
delete process.env.OPENAI_API_KEY;
|
|
|
|
const result = await testCodexAcpEnvironment({
|
|
adapterType: "codex_local",
|
|
companyId: "company-1",
|
|
config: {
|
|
engine: "acp",
|
|
cwd: root,
|
|
agentCommand: commandPath,
|
|
env: { CODEX_HOME: managedAgentHome, OPENAI_API_KEY: "" },
|
|
},
|
|
});
|
|
|
|
expect(result.status).toBe("warn");
|
|
expect(result.checks).toContainEqual(
|
|
expect.objectContaining({
|
|
code: "codex_acp_credentials_missing",
|
|
level: "warn",
|
|
message: expect.stringContaining("Paperclip server"),
|
|
hint: expect.stringContaining("separate Codex/chat session"),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("emits the canonical adapter_auth_missing check for a missing-auth sandbox target", async () => {
|
|
const root = await makeTempRoot("paperclip-codex-acp-sandbox-missing-auth-");
|
|
const sharedCodexHome = path.join(root, "shared-codex-home");
|
|
const managedAgentHome = path.join(
|
|
root,
|
|
"paperclip-home",
|
|
"instances",
|
|
"test",
|
|
"companies",
|
|
"company-1",
|
|
"agents",
|
|
"agent-1",
|
|
"codex-home",
|
|
);
|
|
await fs.mkdir(sharedCodexHome, { recursive: true });
|
|
setNodeVersion("v24.11.0");
|
|
process.env.CODEX_HOME = sharedCodexHome;
|
|
delete process.env.OPENAI_API_KEY;
|
|
|
|
const result = await testCodexAcpEnvironment({
|
|
adapterType: "codex_local",
|
|
companyId: "company-1",
|
|
config: {
|
|
engine: "acp",
|
|
cwd: root,
|
|
// A shell-style command resolves without a real binary in the sandbox.
|
|
agentCommand: "node ./fake-acp.js",
|
|
env: { CODEX_HOME: managedAgentHome, OPENAI_API_KEY: "" },
|
|
},
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
providerKey: "fake-plugin",
|
|
remoteCwd: "/work",
|
|
runner: {
|
|
execute: async () => ({
|
|
exitCode: 0,
|
|
signal: null,
|
|
timedOut: false,
|
|
stdout: "",
|
|
stderr: "",
|
|
pid: null,
|
|
startedAt: new Date().toISOString(),
|
|
}),
|
|
},
|
|
} as never,
|
|
});
|
|
|
|
// A missing-auth sandbox is a warning, not a failure, and it carries the
|
|
// neutral canonical check code for the user interface.
|
|
expect(result.status).toBe("warn");
|
|
expect(result.checks).toContainEqual(
|
|
expect.objectContaining({
|
|
code: "adapter_auth_missing",
|
|
level: "warn",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("executes through ACPX with Codex session config and ephemeral skills", async () => {
|
|
const root = await makeTempRoot("paperclip-codex-acp-exec-");
|
|
const skill = await createRuntimeSkill(root);
|
|
const runtimes: FakeRuntime[] = [];
|
|
const meta: AdapterInvocationMeta[] = [];
|
|
const execute = createCodexAcpExecutor({
|
|
createRuntime: (options: FakeRuntimeOptions) => {
|
|
const runtime = new FakeRuntime(options);
|
|
runtimes.push(runtime);
|
|
return runtime as never;
|
|
},
|
|
});
|
|
|
|
const result = await execute(buildContext(root, {
|
|
config: {
|
|
engine: "acp",
|
|
cwd: root,
|
|
stateDir: path.join(root, "state"),
|
|
env: {
|
|
CODEX_HOME: path.join(root, "codex-home"),
|
|
},
|
|
model: "gpt-5.5",
|
|
modelReasoningEffort: "high",
|
|
fastMode: true,
|
|
promptTemplate: "Do the assigned work.",
|
|
paperclipRuntimeSkills: [skill],
|
|
paperclipSkillSync: { desiredSkills: [skill.key] },
|
|
},
|
|
onMeta: async (payload: AdapterInvocationMeta) => {
|
|
meta.push(payload);
|
|
},
|
|
}));
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.sessionParams).toMatchObject({
|
|
agent: "codex",
|
|
mode: "persistent",
|
|
acpSessionId: "acp-1",
|
|
workspaceId: "workspace-1",
|
|
});
|
|
expect(result.sessionParams?.skills).toMatchObject({
|
|
mode: "codex",
|
|
selectedSkills: ["review"],
|
|
});
|
|
const skillsHome = (result.sessionParams?.skills as { skillsHome?: string }).skillsHome;
|
|
expect(skillsHome).toBeTruthy();
|
|
await expect(fs.readFile(path.join(skillsHome!, "review", "SKILL.md"), "utf8")).resolves.toContain("review skill");
|
|
expect(runtimes[0]?.ensureInputs[0]).toMatchObject({
|
|
agent: "codex",
|
|
mode: "persistent",
|
|
cwd: root,
|
|
});
|
|
expect(runtimes[0]?.setConfigInputs).toEqual([]);
|
|
expect(meta[0]?.commandNotes?.join("\n")).toContain("Prepared ACPX Codex skill home");
|
|
expect(meta[0]?.env?.CODEX_HOME).toBe(path.join(root, "codex-home"));
|
|
expect(JSON.parse(String(meta[0]?.env?.CODEX_CONFIG))).toEqual({
|
|
model: "gpt-5.5",
|
|
model_reasoning_effort: "high",
|
|
service_tier: "fast",
|
|
features: { fast_mode: true },
|
|
});
|
|
});
|
|
|
|
it("sends assignment-owned markdown and ordered distinct wake comments at the ACP boundary", async () => {
|
|
const root = await makeTempRoot("paperclip-codex-acp-context-owner-");
|
|
const runtimes: FakeRuntime[] = [];
|
|
const execute = createCodexAcpExecutor({
|
|
createRuntime: (options: FakeRuntimeOptions) => {
|
|
const runtime = new FakeRuntime(options);
|
|
runtimes.push(runtime);
|
|
return runtime as never;
|
|
},
|
|
});
|
|
const issue = {
|
|
id: "issue-1",
|
|
identifier: "PAP-901",
|
|
title: "Repeat phrase Repeat phrase",
|
|
description: "Repeat phrase Repeat phrase",
|
|
};
|
|
const comments = [
|
|
{ id: "comment-a", body: "Same event body." },
|
|
{ id: "comment-b", body: "Same event body." },
|
|
];
|
|
// The adapter receives server-rendered fields. Keep the server builder's
|
|
// own tests in the server package; adapter packages compile independently.
|
|
const assignmentMarkdown = [
|
|
"Paperclip task context:",
|
|
`- Issue: ${JSON.stringify(issue.identifier)}`,
|
|
`- Title: ${JSON.stringify(issue.title)}`,
|
|
"", "Issue description:", "```text", issue.description, "```",
|
|
].join("\n");
|
|
const historicalMarkdown = [
|
|
assignmentMarkdown,
|
|
...comments.map((comment) => `${comment.id}: ${comment.body}`),
|
|
].join("\n");
|
|
const result = await execute(buildContext(root, {
|
|
context: {
|
|
issueId: issue.id,
|
|
paperclipTaskMarkdown: historicalMarkdown,
|
|
paperclipTaskMarkdownAssignment: assignmentMarkdown,
|
|
paperclipWake: {
|
|
reason: "issue_commented",
|
|
issue: { ...issue, status: "in_progress" },
|
|
comments: comments.map((comment, index) => ({
|
|
...comment,
|
|
issueId: issue.id,
|
|
createdAt: `2026-09-21T00:0${index}:00.000Z`,
|
|
})),
|
|
commentWindow: { requestedCount: 2, includedCount: 2, missingCount: 0 },
|
|
fallbackFetchNeeded: false,
|
|
},
|
|
paperclipTurnContext: {
|
|
version: 1,
|
|
assignment: { owner: "task_markdown" },
|
|
events: {
|
|
owner: "wake_prompt",
|
|
comments: [
|
|
{ id: "comment-a", revision: "a" },
|
|
{ id: "comment-b", revision: "b" },
|
|
],
|
|
},
|
|
},
|
|
paperclipWorkspace: { cwd: root, source: "project_workspace", workspaceId: "workspace-1" },
|
|
},
|
|
}));
|
|
expect(result.exitCode).toBe(0);
|
|
const prompt = String(runtimes[0]?.startInputs[0]?.text ?? "");
|
|
expect(prompt.split("Same event body.")).toHaveLength(3);
|
|
expect(prompt.indexOf("comment comment-a")).toBeLessThan(prompt.indexOf("comment comment-b"));
|
|
expect(prompt).toContain("Repeat phrase Repeat phrase");
|
|
});
|
|
|
|
it("creates the ACP session on the in-sandbox workspace cwd for runner-backed remote runs", async () => {
|
|
const root = await makeTempRoot("paperclip-codex-acp-remote-cwd-");
|
|
const localCwd = path.join(root, "worktree");
|
|
const remoteCwd = path.join(root, "remote-workspace");
|
|
await fs.mkdir(localCwd, { recursive: true });
|
|
await fs.mkdir(remoteCwd, { recursive: true });
|
|
await fs.writeFile(path.join(localCwd, "hello.txt"), "hi", "utf8");
|
|
|
|
const runtimes: FakeRuntime[] = [];
|
|
const execute = createCodexAcpExecutor({
|
|
createRuntime: (options: FakeRuntimeOptions) => {
|
|
const runtime = new FakeRuntime(options);
|
|
runtimes.push(runtime);
|
|
return runtime as never;
|
|
},
|
|
});
|
|
|
|
const result = await execute(
|
|
buildContext(localCwd, {
|
|
config: {
|
|
engine: "acp",
|
|
cwd: localCwd,
|
|
// Use a throwaway ACP command so the process-session bridge does not
|
|
// require a real codex-acp binary in the local sandbox stand-in.
|
|
agentCommand: "node ./fake-acp.js",
|
|
stateDir: path.join(root, "state"),
|
|
env: { CODEX_HOME: path.join(root, "codex-home") },
|
|
promptTemplate: "Do the assigned work.",
|
|
},
|
|
context: {
|
|
issueId: "issue-1",
|
|
paperclipTaskMarkdown: "Task context",
|
|
paperclipWorkspace: { cwd: localCwd, source: "project_workspace", workspaceId: "workspace-1" },
|
|
},
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
providerKey: "fake-plugin",
|
|
remoteCwd,
|
|
runner: createLocalSandboxRunner(),
|
|
} as never,
|
|
authToken: "real-run-jwt",
|
|
}),
|
|
);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
// The workspace was shipped into the sandbox and session/new was created on
|
|
// the in-sandbox workspace dir — not the HOST worktree path.
|
|
await expect(fs.readFile(path.join(remoteCwd, "hello.txt"), "utf8")).resolves.toBe("hi");
|
|
expect(runtimes[0]?.ensureInputs[0]?.cwd).toBe(remoteCwd);
|
|
expect(runtimes[0]?.ensureInputs[0]?.cwd).not.toBe(localCwd);
|
|
});
|
|
|
|
it("seeds the managed Codex home into the sandbox and repoints CODEX_HOME to the in-sandbox path", async () => {
|
|
const root = await makeTempRoot("paperclip-codex-acp-home-seed-");
|
|
const localCwd = path.join(root, "worktree");
|
|
const remoteCwd = path.join(root, "remote-workspace");
|
|
const sourceHome = path.join(root, "codex-home");
|
|
// A separate shared host home with no auth.json so the teardown copy-back is
|
|
// a benign no-op here (this test asserts the inbound seed + remap only).
|
|
const sharedHostHome = path.join(root, "shared-codex-home");
|
|
await fs.mkdir(localCwd, { recursive: true });
|
|
await fs.mkdir(remoteCwd, { recursive: true });
|
|
await fs.mkdir(sourceHome, { recursive: true });
|
|
await fs.mkdir(sharedHostHome, { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(sourceHome, "auth.json"),
|
|
subscriptionAuthJson("acct-seed", NEWER_REFRESH, "seed"),
|
|
{ mode: 0o600 },
|
|
);
|
|
process.env.CODEX_HOME = sharedHostHome;
|
|
|
|
const meta: AdapterInvocationMeta[] = [];
|
|
const execute = createCodexAcpExecutor({
|
|
createRuntime: (options: FakeRuntimeOptions) => new FakeRuntime(options) as never,
|
|
});
|
|
|
|
const result = await execute(
|
|
buildContext(localCwd, {
|
|
config: {
|
|
engine: "acp",
|
|
cwd: localCwd,
|
|
agentCommand: "node ./fake-acp.js",
|
|
stateDir: path.join(root, "state"),
|
|
env: { CODEX_HOME: sourceHome },
|
|
promptTemplate: "Do the assigned work.",
|
|
},
|
|
context: {
|
|
issueId: "issue-1",
|
|
paperclipWorkspace: { cwd: localCwd, source: "project_workspace", workspaceId: "workspace-1" },
|
|
},
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
providerKey: "fake-plugin",
|
|
remoteCwd,
|
|
runner: createLocalSandboxRunner(),
|
|
} as never,
|
|
authToken: "real-run-jwt",
|
|
onMeta: async (payload: AdapterInvocationMeta) => {
|
|
meta.push(payload);
|
|
},
|
|
}),
|
|
);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
const remappedCodexHome = String(meta[0]?.env?.CODEX_HOME ?? "");
|
|
// C2 — the managed home was repointed onto an in-sandbox path, distinct from
|
|
// the host managed home; it is NOT the host CODEX_HOME.
|
|
expect(remappedCodexHome).not.toBe(sourceHome);
|
|
expect(remappedCodexHome).not.toBe(sharedHostHome);
|
|
expect(remappedCodexHome).toContain(".paperclip-runtime");
|
|
// Seeded: the credential materialized into the in-sandbox home (the local
|
|
// runner uses the host FS, so the in-sandbox path is a real host path).
|
|
await expect(fs.readFile(path.join(remappedCodexHome, "auth.json"), "utf8")).resolves.toContain(
|
|
"account_id",
|
|
);
|
|
// C4 — no XDG_* variable is introduced for in-sandbox credential discovery.
|
|
expect(Object.keys(meta[0]?.env ?? {}).filter((key) => key.startsWith("XDG_"))).toEqual([]);
|
|
});
|
|
|
|
it("copies a strictly-newer sandbox Codex auth back to the shared host on teardown", async () => {
|
|
const root = await makeTempRoot("paperclip-codex-acp-copyback-newer-");
|
|
const localCwd = path.join(root, "worktree");
|
|
const remoteCwd = path.join(root, "remote-workspace");
|
|
const sourceHome = path.join(root, "codex-home");
|
|
const sharedHostHome = path.join(root, "shared-codex-home");
|
|
await fs.mkdir(localCwd, { recursive: true });
|
|
await fs.mkdir(remoteCwd, { recursive: true });
|
|
await fs.mkdir(sourceHome, { recursive: true });
|
|
await fs.mkdir(sharedHostHome, { recursive: true });
|
|
// The home staged into the sandbox carries a strictly-newer, same-identity
|
|
// credential (simulating an in-sandbox token rotation); the shared host copy
|
|
// is older.
|
|
await fs.writeFile(
|
|
path.join(sourceHome, "auth.json"),
|
|
subscriptionAuthJson("acct-same", NEWER_REFRESH, "sandbox-newer"),
|
|
{ mode: 0o600 },
|
|
);
|
|
await fs.writeFile(
|
|
path.join(sharedHostHome, "auth.json"),
|
|
subscriptionAuthJson("acct-same", OLDER_REFRESH, "host-older"),
|
|
{ mode: 0o600 },
|
|
);
|
|
process.env.CODEX_HOME = sharedHostHome;
|
|
|
|
const execute = createCodexAcpExecutor({
|
|
createRuntime: (options: FakeRuntimeOptions) => new FakeRuntime(options) as never,
|
|
});
|
|
const result = await execute(
|
|
buildContext(localCwd, {
|
|
config: {
|
|
engine: "acp",
|
|
cwd: localCwd,
|
|
agentCommand: "node ./fake-acp.js",
|
|
stateDir: path.join(root, "state"),
|
|
env: { CODEX_HOME: sourceHome },
|
|
promptTemplate: "Do the assigned work.",
|
|
},
|
|
context: {
|
|
issueId: "issue-1",
|
|
paperclipWorkspace: { cwd: localCwd, source: "project_workspace", workspaceId: "workspace-1" },
|
|
},
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
providerKey: "fake-plugin",
|
|
remoteCwd,
|
|
runner: createLocalSandboxRunner(),
|
|
} as never,
|
|
authToken: "real-run-jwt",
|
|
}),
|
|
);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
// C5 — copy-back fired on teardown and installed the strictly-newer sandbox
|
|
// credential onto the shared host under the merge-lock / monotonic guard.
|
|
const hostAuth = JSON.parse(await fs.readFile(path.join(sharedHostHome, "auth.json"), "utf8"));
|
|
expect(hostAuth.last_refresh).toBe(NEWER_REFRESH);
|
|
expect(hostAuth.tokens.refresh_token).toBe("ref-sandbox-newer");
|
|
// Mode preserved at 0600 by the atomic same-directory rename.
|
|
const mode = (await fs.stat(path.join(sharedHostHome, "auth.json"))).mode & 0o777;
|
|
expect(mode).toBe(0o600);
|
|
});
|
|
|
|
it("keeps the shared host Codex auth when the sandbox copy is not strictly newer", async () => {
|
|
const root = await makeTempRoot("paperclip-codex-acp-copyback-older-");
|
|
const localCwd = path.join(root, "worktree");
|
|
const remoteCwd = path.join(root, "remote-workspace");
|
|
const sourceHome = path.join(root, "codex-home");
|
|
const sharedHostHome = path.join(root, "shared-codex-home");
|
|
await fs.mkdir(localCwd, { recursive: true });
|
|
await fs.mkdir(remoteCwd, { recursive: true });
|
|
await fs.mkdir(sourceHome, { recursive: true });
|
|
await fs.mkdir(sharedHostHome, { recursive: true });
|
|
// The staged/sandbox credential is OLDER than the shared host copy: the
|
|
// strictly-newer guard must keep the host credential (never overwrite a good
|
|
// token with a spent one).
|
|
await fs.writeFile(
|
|
path.join(sourceHome, "auth.json"),
|
|
subscriptionAuthJson("acct-same", OLDER_REFRESH, "sandbox-older"),
|
|
{ mode: 0o600 },
|
|
);
|
|
await fs.writeFile(
|
|
path.join(sharedHostHome, "auth.json"),
|
|
subscriptionAuthJson("acct-same", NEWER_REFRESH, "host-newer"),
|
|
{ mode: 0o600 },
|
|
);
|
|
process.env.CODEX_HOME = sharedHostHome;
|
|
|
|
const execute = createCodexAcpExecutor({
|
|
createRuntime: (options: FakeRuntimeOptions) => new FakeRuntime(options) as never,
|
|
});
|
|
const result = await execute(
|
|
buildContext(localCwd, {
|
|
config: {
|
|
engine: "acp",
|
|
cwd: localCwd,
|
|
agentCommand: "node ./fake-acp.js",
|
|
stateDir: path.join(root, "state"),
|
|
env: { CODEX_HOME: sourceHome },
|
|
promptTemplate: "Do the assigned work.",
|
|
},
|
|
context: {
|
|
issueId: "issue-1",
|
|
paperclipWorkspace: { cwd: localCwd, source: "project_workspace", workspaceId: "workspace-1" },
|
|
},
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
providerKey: "fake-plugin",
|
|
remoteCwd,
|
|
runner: createLocalSandboxRunner(),
|
|
} as never,
|
|
authToken: "real-run-jwt",
|
|
}),
|
|
);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
const hostAuth = JSON.parse(await fs.readFile(path.join(sharedHostHome, "auth.json"), "utf8"));
|
|
expect(hostAuth.last_refresh).toBe(NEWER_REFRESH);
|
|
expect(hostAuth.tokens.refresh_token).toBe("ref-host-newer");
|
|
});
|
|
|
|
it("keeps the host staged Codex home after a clean teardown so a compatible resume can reuse it", async () => {
|
|
// Session-re-staging guardrail: the per-run copy-back (`teardown`) must NOT
|
|
// remove the host staged-home temp dir — that removal moved to the one-time
|
|
// `disposeStaged`, fired only when the runtime is dropped. So after a CLEAN
|
|
// turn the engine caches the staged runtime warm and its host staged home is
|
|
// still on disk for the next compatible resume to reuse.
|
|
const runId = `run-keep-staged-home-${randomUUID()}`;
|
|
const root = await makeTempRoot("paperclip-codex-acp-keep-staged-");
|
|
const localCwd = path.join(root, "worktree");
|
|
const remoteCwd = path.join(root, "remote-workspace");
|
|
const sourceHome = path.join(root, "codex-home");
|
|
const sharedHostHome = path.join(root, "shared-codex-home");
|
|
await fs.mkdir(localCwd, { recursive: true });
|
|
await fs.mkdir(remoteCwd, { recursive: true });
|
|
await fs.mkdir(sourceHome, { recursive: true });
|
|
await fs.mkdir(sharedHostHome, { recursive: true });
|
|
// Strictly-newer sandbox credential so the per-run copy-back has real work.
|
|
await fs.writeFile(
|
|
path.join(sourceHome, "auth.json"),
|
|
subscriptionAuthJson("acct-same", NEWER_REFRESH, "sandbox-newer"),
|
|
{ mode: 0o600 },
|
|
);
|
|
await fs.writeFile(
|
|
path.join(sharedHostHome, "auth.json"),
|
|
subscriptionAuthJson("acct-same", OLDER_REFRESH, "host-older"),
|
|
{ mode: 0o600 },
|
|
);
|
|
process.env.CODEX_HOME = sharedHostHome;
|
|
|
|
// Isolated staged-runtime cache so this test observes only its own entry.
|
|
const stagedRuntimes = new Map();
|
|
const execute = createCodexAcpExecutor({
|
|
createRuntime: (options: FakeRuntimeOptions) => new FakeRuntime(options) as never,
|
|
stagedRuntimes,
|
|
stagingLocks: new Map(),
|
|
});
|
|
const result = await execute(
|
|
buildContext(localCwd, {
|
|
runId,
|
|
config: {
|
|
engine: "acp",
|
|
cwd: localCwd,
|
|
agentCommand: "node ./fake-acp.js",
|
|
stateDir: path.join(root, "state"),
|
|
env: { CODEX_HOME: sourceHome },
|
|
promptTemplate: "Do the assigned work.",
|
|
},
|
|
context: {
|
|
issueId: "issue-1",
|
|
paperclipWorkspace: { cwd: localCwd, source: "project_workspace", workspaceId: "workspace-1" },
|
|
},
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
providerKey: "fake-plugin",
|
|
remoteCwd,
|
|
runner: createLocalSandboxRunner(),
|
|
} as never,
|
|
authToken: "real-run-jwt",
|
|
}),
|
|
);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
// Guardrail: `teardown` ran the copy-back but left the host staged home in
|
|
// place, and the clean turn cached the staged runtime warm for reuse.
|
|
const stagedDirs = await listCodexHomeSyncDirs(runId);
|
|
expect(stagedDirs).toHaveLength(1);
|
|
await expect(fs.stat(stagedDirs[0]!)).resolves.toBeDefined();
|
|
expect(stagedRuntimes.size).toBe(1);
|
|
// The per-run copy-back still fired: the strictly-newer sandbox credential
|
|
// landed on the shared host under the monotonic guard.
|
|
const hostAuth = JSON.parse(await fs.readFile(path.join(sharedHostHome, "auth.json"), "utf8"));
|
|
expect(hostAuth.last_refresh).toBe(NEWER_REFRESH);
|
|
expect(hostAuth.tokens.refresh_token).toBe("ref-sandbox-newer");
|
|
// No `disposeStaged` fires while the entry stays warm, so remove the
|
|
// intentionally-persisted staged temp ourselves to avoid leaking it.
|
|
await Promise.all(stagedDirs.map((dir) => fs.rm(dir, { recursive: true, force: true })));
|
|
});
|
|
|
|
it("removes the host staged Codex home when a failed turn drops the staged runtime", async () => {
|
|
// The complementary guardrail: when the staged runtime IS dropped (here, a
|
|
// failed turn), the one-time `disposeStaged` fires and removes the host
|
|
// staged-home temp dir — while the per-run copy-back (`teardown`) STILL fires
|
|
// on the unclean exit path, so a rotated sandbox credential is never lost.
|
|
const runId = `run-drop-staged-home-${randomUUID()}`;
|
|
const root = await makeTempRoot("paperclip-codex-acp-drop-staged-");
|
|
const localCwd = path.join(root, "worktree");
|
|
const remoteCwd = path.join(root, "remote-workspace");
|
|
const sourceHome = path.join(root, "codex-home");
|
|
const sharedHostHome = path.join(root, "shared-codex-home");
|
|
await fs.mkdir(localCwd, { recursive: true });
|
|
await fs.mkdir(remoteCwd, { recursive: true });
|
|
await fs.mkdir(sourceHome, { recursive: true });
|
|
await fs.mkdir(sharedHostHome, { recursive: true });
|
|
await fs.writeFile(
|
|
path.join(sourceHome, "auth.json"),
|
|
subscriptionAuthJson("acct-same", NEWER_REFRESH, "sandbox-newer"),
|
|
{ mode: 0o600 },
|
|
);
|
|
await fs.writeFile(
|
|
path.join(sharedHostHome, "auth.json"),
|
|
subscriptionAuthJson("acct-same", OLDER_REFRESH, "host-older"),
|
|
{ mode: 0o600 },
|
|
);
|
|
process.env.CODEX_HOME = sharedHostHome;
|
|
|
|
const stagedRuntimes = new Map();
|
|
const execute = createCodexAcpExecutor({
|
|
// A failed turn drives the drop path (discard staged runtime + dispose).
|
|
createRuntime: (options: FakeRuntimeOptions) =>
|
|
new FakeRuntime(options, [], { status: "failed", stopReason: "error" }) as never,
|
|
stagedRuntimes,
|
|
stagingLocks: new Map(),
|
|
});
|
|
const result = await execute(
|
|
buildContext(localCwd, {
|
|
runId,
|
|
config: {
|
|
engine: "acp",
|
|
cwd: localCwd,
|
|
agentCommand: "node ./fake-acp.js",
|
|
stateDir: path.join(root, "state"),
|
|
env: { CODEX_HOME: sourceHome },
|
|
promptTemplate: "Do the assigned work.",
|
|
},
|
|
context: {
|
|
issueId: "issue-1",
|
|
paperclipWorkspace: { cwd: localCwd, source: "project_workspace", workspaceId: "workspace-1" },
|
|
},
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
providerKey: "fake-plugin",
|
|
remoteCwd,
|
|
runner: createLocalSandboxRunner(),
|
|
} as never,
|
|
authToken: "real-run-jwt",
|
|
}),
|
|
);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
// Guardrail: the dropped staged runtime disposed its host staged home and
|
|
// left nothing cached for reuse.
|
|
await expect(listCodexHomeSyncDirs(runId)).resolves.toEqual([]);
|
|
expect(stagedRuntimes.size).toBe(0);
|
|
// ...yet the per-run copy-back still ran on the failure teardown path, so the
|
|
// strictly-newer sandbox credential was not lost.
|
|
const hostAuth = JSON.parse(await fs.readFile(path.join(sharedHostHome, "auth.json"), "utf8"));
|
|
expect(hostAuth.last_refresh).toBe(NEWER_REFRESH);
|
|
expect(hostAuth.tokens.refresh_token).toBe("ref-sandbox-newer");
|
|
});
|
|
|
|
it("passes the Codex-specific teardown messages to the shared workspace-restore-teardown factory", async () => {
|
|
// Wiring test only: `createWorkspaceRestoreTeardown` owns the
|
|
// classify-and-redact contract, proven once for all three adapters by its
|
|
// own table-driven test in `packages/adapter-utils`. This test proves only
|
|
// that the Codex adapter passes its own two message strings to it.
|
|
// Clear the shared, hoisted mock first: earlier tests in this file also
|
|
// call through it, and a leftover call could hide a real wiring bug.
|
|
mockCreateWorkspaceRestoreTeardown.mockClear();
|
|
const root = await makeTempRoot("paperclip-codex-acp-teardown-wiring-");
|
|
const localCwd = path.join(root, "worktree");
|
|
const remoteCwd = path.join(root, "remote-workspace");
|
|
await fs.mkdir(localCwd, { recursive: true });
|
|
await fs.mkdir(remoteCwd, { recursive: true });
|
|
|
|
const execute = createCodexAcpExecutor({
|
|
createRuntime: (options: FakeRuntimeOptions) => new FakeRuntime(options) as never,
|
|
stagedRuntimes: new Map(),
|
|
stagingLocks: new Map(),
|
|
});
|
|
const result = await execute(
|
|
buildContext(localCwd, {
|
|
config: {
|
|
engine: "acp",
|
|
cwd: localCwd,
|
|
agentCommand: "node ./fake-acp.js",
|
|
stateDir: path.join(root, "state"),
|
|
env: { CODEX_HOME: path.join(root, "codex-home") },
|
|
promptTemplate: "Do the assigned work.",
|
|
},
|
|
context: {
|
|
issueId: "issue-1",
|
|
paperclipWorkspace: { cwd: localCwd, source: "project_workspace", workspaceId: "workspace-1" },
|
|
},
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
providerKey: "fake-plugin",
|
|
remoteCwd,
|
|
runner: createLocalSandboxRunner(),
|
|
} as never,
|
|
authToken: "real-run-jwt",
|
|
}),
|
|
);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(mockCreateWorkspaceRestoreTeardown).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
startMessage: "[paperclip] Restoring workspace changes and Codex auth from the sandbox.\n",
|
|
failurePrefix: "[paperclip] Codex ACP teardown restore/copy-back failed",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("reports unavailable ACP for a runner-less sandbox even when the ACP command is set", async () => {
|
|
setNodeVersion("v24.11.0");
|
|
// Isolate the missing bidirectional runner as the sole fallback cause:
|
|
// provide a valid ACP command and Node version so the only difference from
|
|
// the runner-backed ACP case is the absent `runner`.
|
|
await expect(
|
|
resolveCodexExecutionEngineForRun({
|
|
config: { agentCommand: "codex-acp" },
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
providerKey: "fake-plugin",
|
|
remoteCwd: "/work",
|
|
},
|
|
}),
|
|
).resolves.toMatchObject({
|
|
engine: "acp",
|
|
explicit: false,
|
|
unavailableReason: expect.stringContaining("bidirectional remote process"),
|
|
});
|
|
});
|
|
|
|
it("classifies ACP refresh-token auth failures", async () => {
|
|
const root = await makeTempRoot("paperclip-codex-acp-refresh-token-");
|
|
const execute = createCodexAcpExecutor({
|
|
createRuntime: (options: FakeRuntimeOptions) => new FakeRuntime(
|
|
options,
|
|
[],
|
|
{
|
|
status: "failed",
|
|
error: { message: "OAuth failed: refresh_token_invalidated" },
|
|
} as unknown as FakeRuntimeTurnResult,
|
|
) as never,
|
|
});
|
|
|
|
const result = await execute(buildContext(root));
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.errorCode).toBe("refresh_token_invalidated");
|
|
expect(result.errorFamily).toBe("refresh_token_invalidated");
|
|
expect(result.resultJson?.errorFamily).toBe("refresh_token_invalidated");
|
|
expect(result.resultJson).not.toHaveProperty("codexCredentialTelemetry");
|
|
});
|
|
|
|
it("resumes compatible ACP sessions on later Codex ACP runs", async () => {
|
|
const root = await makeTempRoot("paperclip-codex-acp-resume-");
|
|
const runtimes: FakeRuntime[] = [];
|
|
const execute = createCodexAcpExecutor({
|
|
createRuntime: (options: FakeRuntimeOptions) => {
|
|
const runtime = new FakeRuntime(options);
|
|
runtimes.push(runtime);
|
|
return runtime as never;
|
|
},
|
|
});
|
|
|
|
const first = await execute(buildContext(root));
|
|
const second = await execute(buildContext(root, {
|
|
runtime: {
|
|
sessionId: first.sessionId ?? null,
|
|
sessionParams: first.sessionParams ?? null,
|
|
sessionDisplayId: first.sessionDisplayId ?? null,
|
|
taskKey: "PAP-1",
|
|
},
|
|
}));
|
|
|
|
expect(second.exitCode).toBe(0);
|
|
expect(runtimes).toHaveLength(2);
|
|
expect(runtimes[1]?.ensureInputs[0]?.resumeSessionId).toBe("acp-1");
|
|
});
|
|
});
|
|
|
|
describe("resolveCodexAcpBillingIdentity", () => {
|
|
const originalOpenAiKey = process.env.OPENAI_API_KEY;
|
|
const originalOpenRouterKey = process.env.OPENROUTER_API_KEY;
|
|
|
|
afterEach(() => {
|
|
if (originalOpenAiKey === undefined) delete process.env.OPENAI_API_KEY;
|
|
else process.env.OPENAI_API_KEY = originalOpenAiKey;
|
|
if (originalOpenRouterKey === undefined) delete process.env.OPENROUTER_API_KEY;
|
|
else process.env.OPENROUTER_API_KEY = originalOpenRouterKey;
|
|
});
|
|
|
|
it("classifies an adapter-config API key as api billing to openai", () => {
|
|
delete process.env.OPENROUTER_API_KEY;
|
|
expect(
|
|
resolveCodexAcpBillingIdentity({ config: { env: { OPENAI_API_KEY: "sk-test" } } }),
|
|
).toEqual({ provider: "openai", biller: "openai", billingType: "api" });
|
|
});
|
|
|
|
it("falls back to chatgpt subscription without an API key", () => {
|
|
delete process.env.OPENAI_API_KEY;
|
|
delete process.env.OPENROUTER_API_KEY;
|
|
expect(resolveCodexAcpBillingIdentity({ config: {} })).toEqual({
|
|
provider: "openai",
|
|
biller: "chatgpt",
|
|
billingType: "subscription",
|
|
});
|
|
});
|
|
|
|
it("bills OpenRouter-backed runs to openrouter", () => {
|
|
expect(
|
|
resolveCodexAcpBillingIdentity({
|
|
config: { env: { OPENAI_API_KEY: "sk-test", OPENROUTER_API_KEY: "or-test" } },
|
|
}),
|
|
).toEqual({ provider: "openai", biller: "openrouter", billingType: "api" });
|
|
});
|
|
|
|
it("ignores host env for remote execution targets", () => {
|
|
process.env.OPENAI_API_KEY = "sk-host-only";
|
|
expect(
|
|
resolveCodexAcpBillingIdentity({
|
|
config: {},
|
|
executionTarget: { kind: "remote", transport: "sandbox", remoteCwd: "/work" },
|
|
} as never).billingType,
|
|
).toBe("subscription");
|
|
});
|
|
});
|