Files
PaperClipAI/packages/paperclip-runner/src/drivers/codex/codex-protocol-integrity.test.ts
T
Nicky LeachandClaude Opus 5.5 83c6832ccb fix(build): typecheck package, Playwright, and e2e test files in CI (#18839)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Runner, plugin, and Playwright tests verify how those agents run,
connect, and appear in the board
> - Most package tsconfigs exclude tests, and Vitest strips types, so a
type error in those tests fails nothing
> - The tests drifted to 803 real type errors, and some of those errors
hid assertions that checked nothing
> - paperclipai/paperclip#18739 and paperclipai/paperclip#18738 check
the server test tree and four test projects, but not these files
> - This pull request adds a required check for these test files, fixes
every error, and fails CI if a test file is in no program
> - The benefit is that a type error in any test file outside
`server/src` now fails CI

## Linked Issues or Issue Description

Refs #18739
Refs #18738

**What happened?**

517 test files were in no program that required CI checks. A type error
in those files did not fail CI.

**Expected behavior**

CI fails on a type error in any test file.

**Steps to reproduce**

Add `export const value: number = "text";` to a test file under
`packages/paperclip-runner/src` on master. CI stays green, because
`tsconfig.json` excludes test files and Vitest strips types.

**Measured gap**

Each row is a program with its tests included, on one thread. File and
error counts are for head `7d4b5e907a`. Memory peaks were measured
before the two latest merges of master.

| Group | Test files | Real errors | Peak |
| --- | ---: | ---: | ---: |
| `paperclip-runner` Node source | 290 | 644 | 1.09 GiB |
| `paperclip-runner` surfaces | 9 | 12 | 0.42 GiB |
| `paperclip-runner` devtools and examples | 14 | 0 | 0.68 GiB |
| `sandbox-providers/kubernetes` | 21 | 65 | 0.44 GiB |
| 8 other sandbox providers and `bridge-template` | 32 | 4 | 0.34 GiB |
| `plugins/sdk` | 22 | 15 | 0.41 GiB |
| `plugin-workspace-diff` | 5 | 0 | 0.37 GiB |
| `plugin-orchestration-smoke-example` | 1 | 1 | 0.15 GiB |
| `kv-demo-mcp-server` | 3 | 0 | 0.13 GiB |
| `packages/db` scripts | 1 | 0 | 1.01 GiB |
| 8 Playwright suites under `tests/` | 52 | 25 | 1.72 GiB |
| `tests/runner-acceptance` | 3 | 0 | 0.58 GiB |
| `tests/e2e`, through layer declarations | 63 | 37 | at most 1.59 GiB |
| `ui` `AttentionInteractionResolver.test.tsx` | 1 | 0 | n/a |
| **Total** | **517** | **803** | |

27 of the 803 errors came from master while this pull request was open.
Each merge of master brought new test code that no check had read.

Errors that were config errors, not real errors:

- One TS2688 for each provider, because the root install does not
install provider dependencies.
- 9 in the Runner Node program (TS7016 and TS6059).
- 2 in `tsconfig.browser.json`. TypeScript 7 removed `baseUrl`, so that
config was invalid.
- 213 in the Playwright suites (`@/*` aliases and Vite types).
- About 60 in `tests/e2e` (import extensions and aliases).

`packages/plugins/create-paperclip-plugin` has no gap. Its tsconfig
excludes its test, but `cli/tsconfig.json` reaches that test through an
import. `server/test-baselines` also has no gap, because
`tests/lifecycle-baseline` reads it.

## What Changed

- Added `scripts/typecheck-test-programs.mjs` and its tests. The
`Typecheck + Release Registry` job runs it. It checks each program in
`PROGRAMS` on one thread.
- The script fails if a tracked `*.test.ts(x)` or `*.spec.ts(x)` file
outside `server/src` is in no program. The compiler file list decides
this, because `include` drops `foo.test.tsx` when `foo.test.ts` exists.
- Added a `tsconfig.test.json` to each uncovered package, and
`tests/tsconfig.playwright.json`.
- Added `tests/e2e` to `PROJECTS` in
`scripts/typecheck-test-projects.mjs`, with a `tsconfig.json` and a
`tsconfig.layers.json`.
- Added `scripts/typecheck-sandbox-providers`. It is a small workspace
that installs provider SDKs from one lockfile. I did not add a lockfile
inside each provider, because
`scripts/build-standalone-public-packages.mjs` uses a frozen install
when one exists, and that changes what a release resolves.
- Corrected `packages/paperclip-runner/tsconfig.browser.json` for
TypeScript 7.
- Set `rootDir` to `..` in
`packages/paperclip-runner/tsconfig.test.json`, because one Runner test
imports from `packages/adapter-utils/src`. The program does not emit
files, so this changes no build output.
- Moved the Kubernetes provider from `vitest ^3.2.4` to `^4.1.8`, the
version the other providers use. Vitest 3 requires `tinypool ^1.1.1`,
which has two critical advisories (GHSA-85c8-ppgw-ccpr and
GHSA-5gmw-xhrv-c9v3). The first patched version is 2.1.2. Its tests have
the same result on both versions: 189 passed, and the same 12
`file-sync` tests failed.
- Moved the Kubernetes provider from `zod ^3.24.2` to `^4.6.6`, and gave
its two `z.record` calls a key schema. This is the only production
change, and it stays in this pull request. See "Production change for
review".
- Renamed `AttentionInteractionResolver.test.ts` to
`AttentionInteractionResolver.replace.test.ts`, so TypeScript reads the
`.test.tsx` beside it.
- Fixed all 803 errors, with changes to test files and test support
only. The fix for `contracts/native-execution.test.ts` is the same as
the fix in paperclipai/paperclip#18739, so those two changes merge
cleanly.
- Updated `doc/DEVELOPING.md`.

This pull request does not edit `server/scripts/typecheck-layers.mjs`.
paperclipai/paperclip#18739 and paperclipai/paperclip#18738 have both
merged, and this branch includes them.

### Production change for review

The new lockfile made dependency review flag `zod@3.25.76` as a
high-severity advisory (GHSA-mrhx-c7xc-m3w5). The first patched version
is 4.6.6, so 3.x has no fix. The published Kubernetes plugin uses zod to
validate operator config. The zod 3 source does not compile against zod
4, so a typecheck-only override cannot fix this.

The existing tests do not prove this change, because they also passed
with the invalid one-argument `z.record`. So I parsed 45 inputs with the
same schema on zod 3.25.76 and zod 4.6.7:

| Result | Inputs | Detail |
| --- | ---: | --- |
| Identical | 23 | Includes defaults, valid config, and a `__proto__`
key |
| Same outcome, new error wording | 21 | For example, `Invalid url` is
now `Invalid URL`. `validateConfig` shows this text to operators. |
| Different outcome | 1 | `jobTtlSecondsAfterFinished: 2 ** 60` was
valid and is now rejected. The new limit is `Number.MAX_SAFE_INTEGER`. |

I did not test this against a real cluster.

The decision is to keep this change in this pull request. Since then,
paperclipai/paperclip#17364 has moved the rest of the repository to zod
4.6.x, so the Kubernetes plugin now matches it.

### Behavior change for review

`reducedMotion` under `use` is not a Playwright option. It had no effect
in 11 configs and 2 `test.use` calls, so those suites ran with
`no-preference`. The options are now under `contextOptions`, so reduced
motion now takes effect. `app-shell-design.spec.ts` asserts a real
transition, so it now asks for `no-preference` itself.

The baseline manifest has `snapshotCount: 0`, so this invalidates no
published baseline. A frozen trial reference PNG can differ.

### Findings

These are product questions. I did not change them, and I did not hide
them with casts:

1. **`PrpEvent` and its schema disagree.** The TypeScript type in
`src/protocol/replay-contract.ts` requires `normalizedSessionId`. The
`required` list in `protocol/schemas/event.schema.json` does not include
it. `validatePrpEvent` accepts events that the type forbids.
2. **`requestCheckboxConfirmation` is stricter than its runtime.** It
uses the output type, so plugin authors must pass `continuationPolicy`
and `allowDeclineReason`, although the schema gives them defaults.
`z.input` would fix this.
3. **`RetainedRunnerdMaintenanceInput.backend.kind` is a free string.**
The quarantine domain comes from `descriptor.kind`, but the release uses
`proof.backend.kind`. If they differ,
`completeRetainedNativeSessionCleanup` returns 0 and the quarantine
stays. The production caller passes `"runner"` today, so this is not a
live bug.
4. **`CodexHarnessSession.close(input?)` is optional, but the contract
requires `{ reason }`.**

These are tests that verified nothing. I corrected them, and they pass:

- `backends/native-instruction-measurement.test.ts` filtered on
`placement === "core"`, which is not a valid placement. It measured an
empty tool list. It now measures 14 tools.
- `drivers/acpx/codex-acpx-driver.test.ts` checked for `usage.updated`,
which is not an event type.
- The `hostFixture()` helper in `drivers/acpx/runtime-host.test.ts`
ignored two overrides, so
`expect(prepareSandbox).not.toHaveBeenCalled()` could not fail.
- `live/live-session.test.ts` set `agent: "pi"` before a spread that
overwrote it.
- `native-distribution-integrity.test.ts` passed an AbortSignal where
`openCommand` expects `{ signal }`.

These are gaps that this pull request does not close:

- **The root lockfile still has some old zod versions.**
paperclipai/paperclip#17364 moved the direct zod dependencies to 4.6.x.
`pnpm-lock.yaml` still resolves `zod@3.25.76`, `4.1.11`, and `4.4.3`,
which are below the patched 4.6.6.
`packages/paperclip-runner/scripts/pi-distribution/package.json` pins
`zod` to `3.25.76`, and some agent SDKs resolve `4.4.3`. Dependency
review does not flag them, because this pull request does not add them.
- **Dead GitHub coverage.** `chat-adapters-ui-providers.spec.ts`
returned early for GitHub, so four later `provider === "github"`
branches never ran. I removed them. They included the private key file
and paste match, and the reconnect flow. Nothing asserts those behaviors
in that spec now.
- **Provider tests do not run in CI.** The root `vitest.config.ts` lists
only Daytona. The other 8 providers, `bridge-template` included, are
typechecked now, but CI does not run their tests.
- **One `packages/db` test never runs.**
`scripts/clean-poisoned-claude-sessions.test.ts` is outside the
`include` of `packages/db/vitest.config.ts`.
- **`plugin-orchestration-smoke-example` does not collect.**
`@paperclipai/shared` is not linked in its `node_modules`. It passes 3
of 3 with an alias.

## Verification

Run these commands:

```sh
node scripts/typecheck-test-programs.mjs --low-memory
pnpm --filter @paperclipai/server typecheck
node scripts/typecheck-test-projects.mjs --low-memory --reuse-declarations
node --test scripts/typecheck-test-programs.test.mjs
```

Results on macOS (arm64), Node 26.12. CI uses Node 24, and its
`Typecheck + Release Registry` job also passes. At head `7d4b5e907a`,
the test programs, `tests/e2e`, and the script tests pass again with 0
errors. The times and peaks below come from the run before the two
latest merges of master:

| Check | Result |
| --- | --- |
| Test programs, `--low-memory` | Exit 0, 0 errors, 42 seconds, peak
1.78 GiB |
| Server layers, low memory | Exit 0, 0 errors, 38 seconds, peak 3.41
GiB |
| Test projects, including `tests/e2e` | Exit 0, 0 errors, 13 seconds,
peak 1.59 GiB |
| Script tests | 12 passed |
| Injected type error in each of 23 places | Each one failed its program
with TS2322 |
| Coverage check | 0 unchecked files, 10 seconds |

I did not run this in a 4 GiB container. The peaks above are `maximum
resident set size` for the largest single process.

**Runtime check of the fixes.** Types do not prove that a test still
works, so I ran the suites before the two latest merges of master:

| Suite | Result |
| --- | --- |
| Runner, full suite | 4,885 passed, 3 failed, 13 skipped |
| Sandbox providers, 10 packages | 939 passed, 12 failed, 36 skipped |
| Plugin SDK | 200 passed |
| `kv-demo-mcp-server` | 12 passed |
| Renamed UI test and its sibling | 4 passed |

All 15 failures exist without these fixes or come from the local
machine:

- **3 Cursor Cloud timeouts.** `artifact-delivery.test.ts` has 1 and
`cursor-cloud-driver.test.ts` has 2. They time out in a full local run,
and all 133 tests in those files with `codex-security-config.test.ts`
pass when run alone. They fail in the same way with those two files at
their unedited versions.
- **12 Kubernetes `file-sync.test.ts` failures.** They fail in the same
way before and after the fixes. The first failure is that
`/proc/self/fd` does not exist on macOS.

After each of the two latest merges of master, I ran only the Runner
test files that I changed:

| Merge | Result |
| --- | --- |
| `885c249cb9`, 8 files | 534 passed |
| `7d4b5e907a`, 7 files | 672 passed, 16 failed |

The 16 failures are in the two `source-reconciliation` test files. They
fail in the same way with master's unedited versions of those files. 15
of them fail with `ACPX sidecar exited at initialize`, so I think my
local build of the Rust and sidecar binaries is out of date. I did not
rebuild them to check. CI builds them from scratch, and all Runner jobs
pass in CI at `7d4b5e907a`.

**Not run:**

- `tests/e2e` and the 8 Playwright suites need a server, so most of them
have only compile checks. `playwright test --list` loads 308 tests from
the main e2e config.
- `packages/plugins/sandbox-providers/kubernetes/test/integration` needs
a cluster, so it has only compile checks.
- I did not run the full `pnpm test:run` or root `pnpm build`. Required
CI runs them.

**Reduced motion.** I ran 6 motion-sensitive Storybook specs in system
Chrome with a temporary config, once with `reduce` and once with
`no-preference`. Both runs had 45 passed and the same 5 failed. So
reduced motion did not change those results. The 5 failures do not
depend on motion. At least one comes from the browser, because system
Chrome prints `oklch(55.6% 0 0)` where the test expects `oklch(0.556 0
0)`. I did not run the other Storybook specs. The `storybook-visual`
label runs that workflow.

## Risks

- **A new lockfile to maintain.** A change to a provider `package.json`
now fails the typecheck job until someone refreshes
`scripts/typecheck-sandbox-providers/pnpm-lock.yaml`. The failure
message gives the command.
- **The job has a new install.** The step downloads provider SDKs, so it
depends on the npm registry. It uses `--frozen-lockfile` and
`--ignore-scripts`.
- **The local install replaces provider `node_modules`.** A contributor
with a standalone provider install must reinstall that provider after
running the script.
- **Reduced motion can change Storybook results.** See "Behavior change
for review". That workflow is not part of required CI.
- **Kubernetes plugin validation changed.** A release of this plugin
ships zod 4. Error wording changes, and very large integers are now
rejected. See "Production change for review".
- **Master moves fast.** Each merge of master brought new test code with
type errors, 27 so far. After this merges, the check reports those
errors in the pull request that adds them.

## Model Used

- Provider: Anthropic Claude, through Claude Code
- Model: Claude Opus 5.5 (`claude-opus-5-5`)
- Capabilities: tool use, code execution, and subagents for the type
fixes in separate files

## 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 either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [ ] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived
details. The app generated the branch name.
- [ ] I have run tests locally and they pass. See the failures explained
in Verification.
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 07:33:59 -08:00

1185 lines
44 KiB
TypeScript

import {
createCipheriv,
createDecipheriv,
createHash,
createHmac,
} from "node:crypto";
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { HarnessDriverBackend } from "../../backends/harness-driver-backend.js";
import { createCodexTaskEnvelope } from "../../contracts/codex.js";
import type { ControlPlanePort } from "../../contracts/control-plane-port.js";
import type { NativeExecutionInputV1 } from "../../contracts/native-execution.js";
import { NativeSessionProtocolIntegrityError } from "../../contracts/native-session-backend.js";
import {
DurablePrpControlPlane,
durableRecoveryInternals,
} from "../../control-plane/durable-prp-control-plane.js";
import type { DurableRecoveryIdentity } from "../../control-plane/prp-transport-types.js";
import { createCapabilityRunnerdCodexTransport } from "../../live/runnerd-codex-transport.js";
import { executeNativeSession } from "../../native-session-runtime.js";
import { CodexAppServerDriver } from "./codex-app-server-driver.js";
import { CodexSessionState } from "./codex-session-state.js";
import {
FakeCodexTransport,
TestQueue,
WORKSPACE,
describe,
expect,
it,
makeDriver,
result,
vi,
type PrpEvent,
} from "./codex-app-server-driver.test-support.js";
// Synthetic runner process boundary; authentication and the encrypted wire are
// real. Keep this client local to this test rather than exporting test protocol
// machinery from the production controller.
async function authenticatedRunner(
core: DurablePrpControlPlane,
identity: DurableRecoveryIdentity,
runnerBinary: string,
) {
const framed = (domain: string, parts: Buffer[]) => {
const values: Buffer[] = [Buffer.from(domain), Buffer.from([0])];
for (const part of parts) {
const length = Buffer.alloc(8);
length.writeBigUInt64BE(BigInt(part.length));
values.push(length, part);
}
return Buffer.concat(values);
};
const digest = (domain: string, parts: Buffer[]) =>
createHash("sha256").update(framed(domain, parts)).digest();
const credential = Buffer.from(core.issueBootstrapTicket());
const authKey = digest("paperclip-runner-auth-key-v1", [credential]);
const mac = (domain: string, parts: Buffer[]) =>
createHmac("sha256", authKey).update(framed(domain, parts)).digest();
const credentialId = `sha256:${digest("paperclip-runner-credential-id-v1", [credential]).toString("hex")}`;
const socket = new WebSocket(core.connectUrl);
const frames = new TestQueue<Record<string, unknown>>();
const reader = frames[Symbol.asyncIterator]();
socket.addEventListener("message", (event) =>
frames.push(JSON.parse(String(event.data))),
);
socket.addEventListener("close", () => frames.close());
socket.addEventListener("error", () =>
frames.fail(new Error("Synthetic runner socket failed")),
);
await new Promise<void>((resolve, reject) => {
socket.addEventListener("open", () => resolve(), { once: true });
socket.addEventListener("error", reject, { once: true });
});
socket.send(
JSON.stringify({
protocol: "paperclip.runner",
version: 1,
kind: "auth_hello",
payload: {
credentialId,
clientNonce: "composed-integrity-client",
protocolMin: 1,
protocolMax: 1,
...identity,
runnerVersion: "0.3.0",
runnerDigest: `sha256:${createHash("sha256").update(readFileSync(runnerBinary)).digest("hex")}`,
},
}),
);
const challenge = (await reader.next()).value!.payload as Record<
string,
unknown
>;
const { serverProof, ...challengeFields } = challenge;
const canonical = Buffer.from(
durableRecoveryInternals.canonicalJson(challengeFields),
);
expect(serverProof).toBe(
mac("paperclip-runner-server-proof-v1", [canonical]).toString("hex"),
);
const clientProof = mac("paperclip-runner-client-proof-v1", [
canonical,
Buffer.from(String(serverProof)),
]).toString("hex");
socket.send(
JSON.stringify({
protocol: "paperclip.runner",
version: 1,
kind: "auth_response",
payload: {
credentialId,
clientNonce: challenge.clientNonce,
serverNonce: challenge.serverNonce,
clientProof,
},
}),
);
const binding = digest("paperclip-runner-session-binding-v1", [
canonical,
Buffer.from(String(serverProof)),
Buffer.from(clientProof),
]);
const sessionId = `sha256:${binding.toString("hex")}`;
const nonce = (prefix: string, counter: number) => {
const value = Buffer.alloc(12);
value.write(prefix, 0, "ascii");
value.writeBigUInt64BE(BigInt(counter), 4);
return value;
};
const aad = (direction: string, counter: number) =>
Buffer.from(
`paperclip.runner.secure-frame.v1\0${sessionId}\0${direction}\0${counter}`,
);
const welcome = (await reader.next()).value!;
expect(welcome.counter).toBe(0);
const sealed = Buffer.from(String(welcome.ciphertext), "hex");
const decipher = createDecipheriv(
"aes-256-gcm",
mac("paperclip-runner-core-to-client-key-v1", [binding]),
nonce("P3S1", 0),
);
decipher.setAAD(aad("core_to_client", 0));
decipher.setAuthTag(sealed.subarray(-16));
const opened = JSON.parse(
Buffer.concat([
decipher.update(sealed.subarray(0, -16)),
decipher.final(),
]).toString("utf8"),
);
expect(opened.kind).toBe("welcome");
let counter = 0;
return {
socket,
send(value: Record<string, unknown>) {
const cipher = createCipheriv(
"aes-256-gcm",
mac("paperclip-runner-client-to-core-key-v1", [binding]),
nonce("P3C1", counter),
);
cipher.setAAD(aad("client_to_core", counter));
const ciphertext = Buffer.concat([
cipher.update(JSON.stringify(value)),
cipher.final(),
cipher.getAuthTag(),
]);
socket.send(
JSON.stringify({
schema: "paperclip.runner.secure-frame.v1",
counter: counter++,
ciphertext: ciphertext.toString("hex"),
}),
);
},
};
}
describe("Codex protocol integrity propagation", () => {
it("blocks goal operations after an integrity fault without blocking cleanup", async () => {
const transport = new FakeCodexTransport();
const session = await makeDriver([transport]).openSession({
runId: "run-goal-integrity", normalizedSessionId: "session-goal-integrity", workingDirectory: WORKSPACE,
});
if (!(session instanceof CodexSessionState)) throw new Error("Expected Codex state");
const fault = new NativeSessionProtocolIntegrityError("semantic_input_digest_mismatch");
session.failProtocolIntegrity(fault);
const before = transport.calls.length;
for (const action of ["get", "clear", "resume"] as const) {
await expect(session.goal!({ action })).rejects.toBe(fault);
}
expect(transport.calls).toHaveLength(before);
await session.close({ reason: "integrity cleanup" });
});
it("preserves a fault that arrives while a goal read is in flight", async () => {
const transport = new FakeCodexTransport();
const session = await makeDriver([transport]).openSession({
runId: "run-goal-race", normalizedSessionId: "session-goal-race", workingDirectory: WORKSPACE,
});
if (!(session instanceof CodexSessionState)) throw new Error("Expected Codex state");
let release!: (value: Record<string, unknown>) => void;
const held = new Promise<Record<string, unknown>>((resolve) => { release = resolve; });
const request = transport.request.bind(transport);
const spy = vi.spyOn(transport, "request").mockImplementation((method, params) =>
method === "thread/goal/get" ? held : request(method, params),
);
const pending = session.goal!({ action: "get" });
const fault = new NativeSessionProtocolIntegrityError("semantic_input_digest_mismatch");
session.failProtocolIntegrity(fault);
release({ goal: null });
await expect(pending).rejects.toBe(fault);
spy.mockRestore();
await session.close({ reason: "integrity cleanup" });
});
it.each(["matching", "foreign"] as const)(
"defers an early %s semantic call until turn admission, then enforces its binding",
async (binding) => {
const transport = new FakeCodexTransport();
let releaseStart!: (response: Record<string, unknown>) => void;
transport.turnStartResponse = new Promise((resolve) => {
releaseStart = resolve;
});
const session = await makeDriver([transport]).openSession({
runId: `run-early-semantic-${binding}`,
normalizedSessionId: `session-early-semantic-${binding}`,
workingDirectory: WORKSPACE,
});
if (!(session instanceof CodexSessionState))
throw new Error("Expected Codex state");
const events: PrpEvent[] = [];
const consumed = (async () => {
for await (const event of session.events()) events.push(event);
})();
const started = session.startTurn({
message: { role: "user", text: "Complete the task." },
});
// Keep cleanup safe even when the negative assertion below fails.
void started.catch(() => {});
let semantic: Promise<Record<string, unknown>> | undefined;
try {
await vi.waitFor(() => expect(session.turnStartPending).toBe(true));
expect(session.activeTurnId).toBeNull();
let semanticSettled = false;
semantic = transport.invoke({
id: "early-finish",
method: "item/tool/call",
params: {
threadId: "thread-1",
turnId: binding === "matching" ? "turn-1" : "foreign-turn",
callId: "early-finish",
tool: "paperclip_finish",
arguments: result,
},
});
void semantic.then(
() => {
semanticSettled = true;
},
() => {
semanticSettled = true;
},
);
// The provider callback can beat both the turn/start response and
// normalized turn/started notification. Its valid identity must not
// be judged against the not-yet-admitted null active turn.
await Promise.resolve();
await Promise.resolve();
if (binding === "matching") {
expect(session.protocolFailed).toBe(false);
expect(semanticSettled).toBe(false);
expect(
events.some((event) => event.eventType === "run.result.proposed"),
).toBe(false);
}
releaseStart({
turn: { id: "turn-1", status: "inProgress", items: [] },
});
await started;
transport.push("turn/started", {
threadId: "thread-1",
turn: { id: "turn-1", status: "inProgress", items: [] },
});
const response = await semantic;
if (binding === "matching") {
expect(response.success).toBe(true);
expect(session.protocolFailed).toBe(false);
await vi.waitFor(() =>
expect(
events.some((event) => event.eventType === "run.result.proposed"),
).toBe(true),
);
const accepted = events.findIndex(
(event) => event.eventType === "turn.accepted",
);
const proposed = events.findIndex(
(event) => event.eventType === "run.result.proposed",
);
expect(accepted).toBeGreaterThanOrEqual(0);
expect(proposed).toBeGreaterThan(accepted);
} else {
expect(response.success).toBe(false);
expect(session.protocolFailed).toBe(true);
await vi.waitFor(() =>
expect(
events.find((event) => event.eventType === "session.failed")
?.payload.code,
).toBe("tool_binding_mismatch"),
);
expect(
events.some((event) => event.eventType === "run.result.proposed"),
).toBe(false);
}
} finally {
releaseStart({
turn: { id: "turn-1", status: "inProgress", items: [] },
});
await started.catch(() => {});
await session.close({ reason: "test cleanup" });
await semantic;
await consumed;
}
},
);
it.each([
"start-rejected",
"invalid-start-response",
"integrity-fault",
] as const)(
"does not admit a queued semantic result after %s while turn admission is pending",
async (failure) => {
const transport = new FakeCodexTransport();
let rejectStart!: (error: Error) => void;
let resolveStart!: (response: Record<string, unknown>) => void;
transport.turnStartResponse = new Promise((resolve, reject) => {
resolveStart = resolve;
rejectStart = reject;
});
const session = await makeDriver([transport]).openSession({
runId: `run-early-${failure}`,
normalizedSessionId: `session-early-${failure}`,
workingDirectory: WORKSPACE,
});
if (!(session instanceof CodexSessionState))
throw new Error("Expected Codex state");
const events: PrpEvent[] = [];
const consumed = (async () => {
for await (const event of session.events()) events.push(event);
})().catch((error: unknown) => error);
const started = session
.startTurn({ message: { role: "user", text: "Complete the task." } })
.catch((error: unknown) => error);
const fault =
failure === "integrity-fault"
? new NativeSessionProtocolIntegrityError(
"semantic_input_digest_mismatch",
)
: new Error("Provider rejected turn start");
let semantic: Promise<unknown> | undefined;
try {
await vi.waitFor(() => expect(session.turnStartPending).toBe(true));
transport.push("turn/started", {
threadId: "thread-1",
turn: { id: "turn-1", status: "inProgress", items: [] },
});
await vi.waitFor(() => expect(session.activeTurnId).toBe("turn-1"));
semantic = transport
.invoke({
id: "early-rejected-finish",
method: "item/tool/call",
params: {
threadId: "thread-1",
turnId: "turn-1",
callId: "early-rejected-finish",
tool: "paperclip_finish",
arguments: result,
},
})
.catch((error: unknown) => error);
await Promise.resolve();
expect(session.result).toBeNull();
if (failure === "integrity-fault") {
transport.queue.fail(fault);
await vi.waitFor(() => expect(session.protocolFailed).toBe(true));
}
if (failure === "invalid-start-response") {
resolveStart({ turn: { status: "inProgress", items: [] } });
expect(await started).toMatchObject({
message: "Codex turn response omitted turn.id",
});
} else {
rejectStart(fault);
expect(await started).toBe(fault);
}
if (failure === "integrity-fault") expect(await semantic).toBe(fault);
else expect(await semantic).toMatchObject({ success: false });
expect(
events.some((event) =>
["turn.accepted", "run.result.proposed", "turn.completed"].includes(
event.eventType,
),
),
).toBe(false);
} finally {
rejectStart(fault);
await started;
await session.close({ reason: "test cleanup" });
await semantic;
await consumed;
}
},
);
it.each([
"integrity-fault",
"early-semantic",
"early-start-rejected",
"early-foreign-turn",
"early-new-epoch",
"early-integrity-fault",
"early-close",
"early-detach",
] as const)(
"composes authenticated controller, transport, driver, backend, and runtime for %s",
async (scenario) => {
const directory = mkdtempSync(
join(tmpdir(), "paperclip-composed-integrity-"),
);
// Only the process launcher is synthetic; hash a small fixture instead
// of cold-reading the host Node executable during the protocol deadline.
const runnerBinary = join(directory, "synthetic-runner");
writeFileSync(runnerBinary, "synthetic runner artifact\n", { mode: 0o600 });
const identity: DurableRecoveryIdentity = {
runnerInstanceId: `composed-runner-${scenario}`,
environmentLeaseId: `composed-lease-${scenario}`,
runId: `composed-run-${scenario}`,
normalizedSessionId: `composed-session-${scenario}`,
turnId: `composed-turn-${scenario}`,
itemId: `composed-item-${scenario}`,
};
const contract = {
revision: "1",
objective: "Validate the authenticated failure boundary",
criteria: [
{
id: "objective",
requirement: "Do not accept corrupt provider input",
},
],
};
const input: NativeExecutionInputV1 = {
schema: "paperclip.native-execution-input.v1",
binding: {
companyId: `composed-company-${scenario}`,
issueId: `composed-issue-${scenario}`,
agentId: `composed-agent-${scenario}`,
runId: identity.runId,
executionWorkspaceId: "composed-workspace",
},
task: {
identifier: "TEST-1",
title: contract.objective,
description: null,
prompt: contract.objective,
workMode: "standard",
},
workspace: {
cwd: directory,
repoUrl: null,
repoRef: null,
branchName: null,
},
session: {
normalizedSessionId: identity.normalizedSessionId,
driverKind: "codex_app_server",
protocolVersion: 1,
lifecyclePolicy: { mode: "per_turn", idleTimeoutMs: null },
},
provider: { kind: "codex", model: null },
completionContract: {
id: "composed-contract",
sha256: "composed-contract-sha",
schemaVersion: "paperclip.completion-contract.v1",
contract,
},
interactionResponses: [],
credentialBindings: [],
};
const events: PrpEvent[] = [];
const controlPlane: ControlPlanePort = {
openRun: vi.fn(async () => undefined),
checkpointSession: vi.fn(async () => undefined),
appendEvent: vi.fn(async (event) => {
events.push(event as PrpEvent);
return {
cursor: events.length,
highestContiguousSourceSeq: events.length,
disposition: "committed" as const,
};
}),
replayEvents: vi.fn(async () => ({
events: [],
highestContiguousSourceSeq: 0,
})),
completeRun: vi.fn(async () => undefined),
};
let authority: DurablePrpControlPlane | undefined;
let finishProcess!: (result: {
code: number;
signal: null;
stdout: string;
stderr: string;
}) => void;
const completion = new Promise<{
code: number;
signal: null;
stdout: string;
stderr: string;
}>((resolve) => {
finishProcess = resolve;
});
const kill = vi.fn(() => {
finishProcess({ code: 0, signal: null, stdout: "", stderr: "" });
return true;
});
const launch = vi.fn(() => ({
child: { exitCode: null, kill },
completion,
}));
const bundle = createCapabilityRunnerdCodexTransport({
stateDirectory: directory,
prpIdentity: identity,
runnerBinary,
codexCommand: process.execPath,
codexArgs: [],
sourceCodexHome: null,
environment: {},
runnerReconnectGraceMs: 900_000,
closeGraceMs: scenario === "early-semantic" ? 1_000 : 50,
readRunnerState: async () => ({
schema: "paperclip.runner.durable.state.v1",
...identity,
lifecycle: authority?.store.state.commands.some(
(command) =>
command.type === "runner.suspend" &&
command.status === "completed",
)
? "suspended"
: "running",
}),
runnerProcessLauncher: launch,
controlPlaneRegistration: async (core) => {
authority = core;
await core.start();
return { connectUrl: core.connectUrl, release: () => core.stop() };
},
});
const driver = new CodexAppServerDriver({
taskEnvelope: createCodexTaskEnvelope({
objective: contract.objective,
}),
environment: { PAPERCLIP_WORKSPACE_CWD: directory },
approvalPolicy: "never",
transportFactory: () => bundle.transport,
});
const backend = new HarnessDriverBackend(driver);
const admitted = vi.fn();
const execution = executeNativeSession({
input,
backend,
controlPlane,
runnerInstanceId: identity.runnerInstanceId,
controlPlaneInstanceId: "composed-core",
timeoutMs: 900_000,
requireSessionCloseBeforeReturn: true,
onSession: admitted,
}).catch((error: unknown) => error);
let client: Awaited<ReturnType<typeof authenticatedRunner>> | undefined;
try {
await vi.waitFor(() => expect(launch).toHaveBeenCalledTimes(1));
const core = authority!;
client = await authenticatedRunner(core, identity, runnerBinary);
const commandResult = async (
type: string,
result: Record<string, unknown> = {},
) => {
await vi.waitFor(() =>
expect(
core.store.state.commands.some(
(command) => command.type === type,
),
).toBe(true),
);
const command = core.store.state.commands.find(
(candidate) => candidate.type === type,
)!;
client!.send({
protocol: "paperclip.runner",
version: 1,
kind: "command_result",
payload: {
commandId: command.commandId,
commandType: command.type,
controllerSeq: command.controllerSeq,
status: "completed",
result,
},
});
await vi.waitFor(() =>
expect(core.getCommand(command.commandId)?.status).toBe("completed"),
);
};
const event = (
sourceSeq: number,
eventType: PrpEvent["eventType"],
payload: Record<string, unknown>,
) => ({
protocol: "paperclip.runner",
version: 1,
kind: "event",
...identity,
payload: {
schema: "paperclip.prp.event.v1",
schemaVersion: 1,
sourceEventId: `composed-event-${sourceSeq}`,
sourceSeq,
sourceInstanceId: identity.runnerInstanceId,
sourceKind: "runner",
runId: identity.runId,
normalizedSessionId: identity.normalizedSessionId,
turnId: identity.turnId,
itemId: identity.itemId,
eventType,
priority: 0,
emittedAt: "2026-09-08T00:00:00.000Z",
payload,
},
});
await commandResult("run.prepare");
await commandResult("session.open");
client.send(
event(1, "session.started", {
threadId: "composed-provider-thread",
sessionId: "composed-provider-session",
runtimeIdentity: { processId: process.pid },
}),
);
await commandResult("session.goal.get", { goal: null });
if (scenario !== "integrity-fault") {
await vi.waitFor(() =>
expect(
core.store.state.commands.some(
(command) => command.type === "turn.start",
),
).toBe(true),
);
const start = core.store.state.commands.find(
(command) => command.type === "turn.start",
)!;
// Commit the exact provider start and semantic call while the durable
// command response is still withheld. No polling/scheduling luck can
// make this cross-channel order disappear.
client.send(
event(2, "turn.started", {
providerTurnId:
scenario === "early-foreign-turn"
? "foreign-provider-turn"
: "composed-provider-turn",
status: "inProgress",
}),
);
const earlyResult = {
...result,
summary: "Composed completion.",
completionClaim: {
...result.completionClaim,
contractRevision: contract.revision,
criteria: [
{
criterionId: "objective",
status: "satisfied",
evidenceRefs: ["hello.txt"],
},
],
},
};
client.send(
event(3, "semantic_tool.input", {
semantic_tool: {
schema: "paperclip.prp.semantic_tool.v1",
schemaVersion: 1,
phase: "input",
callId: "early-composed-finish",
operationId: "paperclip_finish",
correlation: {
runId: identity.runId,
normalizedSessionId: identity.normalizedSessionId,
turnId: identity.turnId,
itemId: identity.itemId,
},
idempotencyKey: null,
content: {
digest: `sha256:${createHash("sha256").update(durableRecoveryInternals.canonicalJson(earlyResult)).digest("hex")}`,
redactionDisposition: "digest_only",
references: [],
},
input: earlyResult,
},
}),
);
await vi.waitFor(() =>
expect(core.store.state.ackedSourceSeq).toBe(3),
);
expect(start.status).toBe("pending");
expect(
core.store.state.commands.some(
(command) => command.type === "semantic_tool.result",
),
).toBe(false);
expect(
events.some((entry) => entry.eventType === "run.result.proposed"),
).toBe(false);
if (scenario === "early-detach") {
await bundle.detachControllerForRestart();
kill();
} else if (scenario === "early-integrity-fault") {
const transportFailure = bundle.transport
.request("thread/read", { threadId: "composed-provider-thread" })
.catch((error: unknown) => error);
client.send(
event(4, "semantic_tool.input", {
semantic_tool: {
schema: "paperclip.prp.semantic_tool.v1",
schemaVersion: 1,
phase: "input",
callId: "corrupt-while-admission-pending",
operationId: "paperclip_finish",
correlation: {
runId: identity.runId,
normalizedSessionId: identity.normalizedSessionId,
turnId: identity.turnId,
itemId: identity.itemId,
},
idempotencyKey: null,
content: {
digest: `sha256:${"0".repeat(64)}`,
redactionDisposition: "digest_only",
references: [],
},
input: earlyResult,
},
}),
);
const primary = await transportFailure;
expect(primary).toBeInstanceOf(NativeSessionProtocolIntegrityError);
expect(await execution).toBe(primary);
expect(core.store.state.ackedSourceSeq).toBe(3);
} else if (scenario === "early-new-epoch") {
// A replaced start fence must reject this parked call, never
// reinterpret it under the second request's mutable turn id.
const nextStart = bundle.transport
.request("turn/start", { input: [{ text: "Next turn" }] })
.catch((error: unknown) => error);
await vi.waitFor(() =>
expect(
core.store.state.commands.filter(
(command) => command.type === "turn.start",
),
).toHaveLength(2),
);
await vi.waitFor(() =>
expect(
core.store.state.commands.find(
(command) => command.type === "semantic_tool.result",
)?.payload.isError,
).toBe(true),
);
for (const command of core.store.state.commands.filter(
(command) => command.type === "turn.start",
)) {
client.send({
protocol: "paperclip.runner",
version: 1,
kind: "command_result",
payload: {
commandId: command.commandId,
commandType: command.type,
controllerSeq: command.controllerSeq,
status: "failed",
result: { message: "Provider rejected turn start" },
},
});
}
expect(await nextStart).toBeInstanceOf(Error);
} else if (
scenario === "early-start-rejected" ||
scenario === "early-close"
) {
if (scenario === "early-close")
void bundle.transport
.close("Close during pending admission")
.catch(() => undefined);
client.send({
protocol: "paperclip.runner",
version: 1,
kind: "command_result",
payload: {
commandId: start.commandId,
commandType: start.type,
controllerSeq: start.controllerSeq,
status: "failed",
result: { message: "Provider rejected turn start" },
},
});
} else {
await commandResult("turn.start", {
providerTurnId: "composed-provider-turn",
});
}
if (scenario === "early-semantic") {
await vi.waitFor(() =>
expect(
core.store.state.commands.find(
(command) => command.type === "semantic_tool.result",
)?.payload.isError,
).toBe(false),
);
await vi.waitFor(() =>
expect(
events.some(
(entry) => entry.eventType === "run.result.proposed",
),
).toBe(true),
);
const accepted = events.findIndex(
(entry) => entry.eventType === "turn.accepted",
);
expect(accepted).toBeGreaterThanOrEqual(0);
expect(
events.findIndex(
(entry) => entry.eventType === "run.result.proposed",
),
).toBeGreaterThan(accepted);
// Proposal admission is not the runner's durable result receipt.
// Complete that exact command before asking close to certify reuse.
await commandResult("semantic_tool.result");
expect(core.semanticToolResultsSettled()).toBe(true);
// This fixture replaces only the runner process. Model the new
// durable close contract explicitly instead of accepting an
// unreadable provider suffix as a reusable checkpoint.
mkdirSync(join(directory, "runner"), { recursive: true });
writeFileSync(
join(directory, "runner", "codex-provider-state.json"),
JSON.stringify({ pendingEvents: [], activeProviderTurnId: null }),
);
client.send(
event(4, "turn.completed", {
providerTurnId: "composed-provider-turn",
status: "completed",
}),
);
await commandResult("runner.drain", { retainedEventsDrained: true });
await commandResult("runner.suspend");
kill();
expect(await execution).toMatchObject({
result: { summary: "Composed completion." },
});
expect(controlPlane.completeRun).toHaveBeenCalledTimes(1);
} else {
expect(await execution).toBeInstanceOf(Error);
expect(
events.some((entry) => entry.eventType === "run.result.proposed"),
).toBe(false);
expect(
core.store.state.commands
.filter((command) => command.type === "semantic_tool.result")
.every((command) => command.payload.isError === true),
).toBe(true);
expect(controlPlane.completeRun).not.toHaveBeenCalled();
}
expect(launch).toHaveBeenCalledTimes(1);
return;
}
await commandResult("turn.start", {
providerTurnId: "composed-provider-turn",
});
client.send(
event(2, "turn.started", {
providerTurnId: "composed-provider-turn",
status: "inProgress",
}),
);
await vi.waitFor(() =>
expect(
events.some((entry) => entry.eventType === "turn.started"),
).toBe(true),
);
expect(controlPlane.openRun).toHaveBeenCalledTimes(1);
expect(admitted).toHaveBeenCalledWith(expect.anything());
// Capture the actual transport fault, not a newly constructed lookalike.
// A pending read also proves that request and notification consumers see
// the very same object before the runtime closes its transport.
const transportFailure = bundle.transport
.request("thread/read", { threadId: "composed-provider-thread" })
.catch((error: unknown) => error);
await vi.waitFor(() =>
expect(
core.store.state.commands.some(
(command) => command.type === "session.snapshot",
),
).toBe(true),
);
const faultAt = Date.now();
client.send(
event(3, "semantic_tool.input", {
semantic_tool: {
schema: "paperclip.prp.semantic_tool.v1",
schemaVersion: 1,
phase: "input",
callId: "composed-call",
operationId: "get_task_context",
correlation: {
runId: identity.runId,
normalizedSessionId: identity.normalizedSessionId,
turnId: identity.turnId,
itemId: identity.itemId,
},
idempotencyKey: null,
content: {
digest: `sha256:${"0".repeat(64)}`,
redactionDisposition: "digest_only",
references: [],
},
input: { summary: "DO-NOT-LEAK-composed-test" },
},
}),
);
const primary = await transportFailure;
expect(primary).toBeInstanceOf(NativeSessionProtocolIntegrityError);
expect(primary).toMatchObject({
code: "native_event_replay_conflict",
reason: "semantic_input_digest_mismatch",
recovery: "operator_required",
});
expect(await execution).toBe(primary);
expect(Date.now() - faultAt).toBeLessThan(5_000);
expect(core.store.state.ackedSourceSeq).toBe(2);
expect(
core.store.state.committedEvents.map((entry) => entry.eventType),
).toEqual(["session.started", "turn.started"]);
expect(controlPlane.completeRun).not.toHaveBeenCalled();
expect(
events.some((entry) => entry.eventType === "run.result.proposed"),
).toBe(false);
expect(launch).toHaveBeenCalledTimes(1);
expect(kill).toHaveBeenCalled();
expect(bundle.evidence().diagnostics.join("\n")).not.toContain(
"DO-NOT-LEAK",
);
} finally {
client?.socket.close();
kill();
await bundle.detachControllerForRestart();
await authority?.stop();
await execution;
rmSync(directory, { recursive: true, force: true });
}
},
15_000,
);
it.each(["pre-start", "pending-input", "buffered-terminal"] as const)(
"preserves the exact integrity fault through the composed backend at %s",
async (stage) => {
const transport = new FakeCodexTransport();
const driver = makeDriver([transport]);
const harness = await driver.openSession({
runId: "run-integrity",
normalizedSessionId: "session-integrity",
workingDirectory: WORKSPACE,
});
if (!(harness instanceof CodexSessionState))
throw new Error("Expected Codex state");
const backend = new HarnessDriverBackend({
descriptor: () => driver.descriptor(),
openSession: async () => harness,
});
const session = await backend.openSession({
identity: {
runId: "run-integrity",
sessionId: "session-integrity",
companyId: "company",
issueId: "issue",
agentId: "agent",
},
workingDirectory: WORKSPACE,
});
const fault = new NativeSessionProtocolIntegrityError(
"semantic_input_digest_mismatch",
);
const events: PrpEvent[] = [];
let pending: Promise<Record<string, unknown>> | undefined;
let consumed: Promise<unknown> | undefined;
const consume = async () => {
try {
for await (const event of session.events()) events.push(event);
return null;
} catch (error) {
return error;
}
};
try {
if (stage !== "pre-start") {
const { turnId } = await session.startTurn({
message: { role: "user", text: "Work safely." },
});
if (stage === "pending-input") {
consumed = consume();
pending = transport.invoke({
id: "input-integrity",
method: "item/tool/requestUserInput",
params: {
threadId: "thread-1",
turnId,
itemId: "input-integrity-item",
questions: [
{
id: "color",
header: "Color",
question: "Which color?",
options: [{ label: "Amber" }, { label: "Cobalt" }],
},
],
},
});
await vi.waitFor(() =>
expect(
events.some(
(event) => event.eventType === "runtime_request.created",
),
).toBe(true),
);
} else {
transport.queue.push({
method: "turn/completed",
params: {
threadId: "thread-1",
turn: {
id: turnId,
status: "completed",
items: [
{
id: "final",
type: "agentMessage",
text: JSON.stringify(result),
},
],
},
},
});
await vi.waitFor(() => expect(harness.terminal).toBe(true));
expect(harness.result).not.toBeNull();
}
}
transport.queue.fail(fault);
await vi.waitFor(() => expect(harness.protocolFailed).toBe(true));
consumed ??= consume();
expect(await consumed).toBe(fault);
expect(
events.filter((event) =>
[
"turn.completed",
"turn.failed",
"turn.interrupted",
"run.result.proposed",
"runtime_request.expired",
].includes(event.eventType),
),
).toEqual([]);
if (pending) expect(await pending).toEqual({ answers: {} });
await expect(session.result()).rejects.toBe(fault);
await expect(session.snapshot()).rejects.toBe(fault);
await expect(
session.startTurn({
message: { role: "user", text: "Do not retry." },
}),
).rejects.toBe(fault);
await expect(
session.attachRun!({
identity: { ...session.identity(), runId: "replacement-run" },
}),
).rejects.toBe(fault);
expect(
transport.calls.filter((call) => call.method === "turn/start"),
).toHaveLength(stage === "pre-start" ? 0 : 1);
} finally {
await session.close({ reason: "test cleanup" });
await consumed;
await pending;
}
},
);
it("preserves a protocol failure received before turn start as a typed terminal", async () => {
const transport = new FakeCodexTransport();
const session = await makeDriver([transport]).openSession({ runId: "prestart", normalizedSessionId: "prestart-session", workingDirectory: WORKSPACE });
transport.queue.push({ method: "turn/completed", params: { threadId: "unrelated", turn: { id: "wrong", status: "completed" } } });
const events: PrpEvent[] = [];
for await (const event of session.events()) events.push(event);
await expect(session.startTurn({ message: { role: "user", text: "Work" } })).rejects.toMatchObject({ code: "native_provider_terminal_failed", providerCode: "thread_binding_mismatch", recoverable: false });
await session.close({ reason: "test complete" });
});
it("keeps the redacted, bounded transport cause on the terminal failure", async () => {
// A host-side supervision loss (here, a stopped sandbox plugin worker)
// closed the notification stream. The run's terminal error must explain
// it instead of a bare "failed closed" that reads like a provider fault.
const transport = new FakeCodexTransport();
const session = await makeDriver([transport]).openSession({
runId: "run-cause",
normalizedSessionId: "session-cause",
workingDirectory: WORKSPACE,
});
const events: PrpEvent[] = [];
const consumed = (async () => {
for await (const event of session.events()) events.push(event);
})();
try {
await session.startTurn({ message: { role: "user", text: "Work." } });
transport.queue.fail(new Error(
`runner_process_termination_unconfirmed: runnerd process supervision failed: Cannot call "environmentExecute" — worker for "plugin-1" is stopped token=sandbox-secret ${"x".repeat(4_000)}`,
));
await consumed;
const failed = events.filter((event) => event.eventType === "session.failed");
expect(failed).toHaveLength(1);
const message = String(failed[0]!.payload.message);
expect(failed[0]!.payload).toMatchObject({ code: "notification_transport_failed", recoverable: false });
expect(message).toMatch(/^Notification transport failed closed: /);
expect(message).toContain("runner_process_termination_unconfirmed");
expect(message).toContain('worker for "plugin-1" is stopped');
expect(message).not.toContain("Error: ");
expect(message).not.toContain("sandbox-secret");
expect(message.length).toBeLessThanOrEqual(1_200);
const turnFailed = events.find((event) => event.eventType === "turn.failed");
expect((turnFailed?.payload.error as { message?: string } | undefined)?.message).toBe(message);
} finally {
await session.close({ reason: "test cleanup" });
await consumed;
}
});
it("does not promote a message-and-field lookalike transport error", async () => {
const transport = new FakeCodexTransport();
const session = await makeDriver([transport]).openSession({
runId: "run-generic",
normalizedSessionId: "session-generic",
workingDirectory: WORKSPACE,
});
const fault = Object.assign(new Error("native_event_replay_conflict"), {
code: "native_event_replay_conflict",
reason: "semantic_input_digest_mismatch",
});
const events: PrpEvent[] = [];
const consumed = (async () => {
for await (const event of session.events()) events.push(event);
})();
try {
await session.startTurn({ message: { role: "user", text: "Work." } });
transport.queue.fail(fault);
await consumed;
expect(
events.find((event) => event.eventType === "session.failed")?.payload
.code,
).toBe("notification_transport_failed");
expect(events.some((event) => event.eventType === "turn.failed")).toBe(
true,
);
await expect(session.snapshot()).resolves.toBeDefined();
} finally {
await session.close({ reason: "test cleanup" });
await consumed;
}
});
});