Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

refactor(runtime): share Codex response handling across scan modes (#917)

mldangelo-oai944fb86c338b
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/executor.ts b/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
index 579d5f192..4cfe5da5e 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
@@ -13,6 +13,7 @@ import {
dirname,
isAbsolute,
join,
+ parse,
resolve,
sep,
win32,
@@ -22,6 +23,7 @@ import {
type CyberAccessProgram,
type ThreadEvent,
} from "@openai/codex-sdk";
+import { readCodexSessionTurn } from "../../../scripts/codex_session.mjs";
import { parse as parseToml } from "smol-toml";
import {
createCodexProfileClient,
@@ -193,46 +195,46 @@ export class CodexSdkWorkerExecutor implements CodexWorkerExecutor {
signal: controller.signal,
cyberAccessProgram: runtimeSettings.cyberAccessProgram,
});
- let threadId: string | undefined;
- let turnCompleted = false;
- let lastStreamError: string | undefined;
const diagnostics: CodexWorkerDiagnostic[] = [];
- for await (const event of events) {
- const item = event.type === "item.completed" ? event.item : event;
- if (item.type === "error") {
- const fallbackError = deepScanPermissionProfileFallbackError(
- item.message,
- );
- if (fallbackError) {
- controller.abort(fallbackError);
- throw fallbackError;
+ const turn = await readCodexSessionTurn({
+ thread,
+ events,
+ stopOnCompletion: true,
+ onEvent: async (event) => {
+ const item = event.type === "item.completed" ? event.item : event;
+ if (item.type === "error") {
+ const fallbackError = deepScanPermissionProfileFallbackError(
+ item.message,
+ );
+ if (fallbackError) {
+ controller.abort(fallbackError);
+ throw fallbackError;
+ }
}
- }
- if (event.type === "thread.started") {
- threadId = event.thread_id;
- await request.onThreadStarted?.(threadId);
- } else if (event.type === "item.completed") {
- appendItemDiagnostic(diagnostics, event.item);
- } else if (event.type === "turn.completed") {
- turnCompleted = true;
- request.signal.removeEventListener("abort", forwardAbort);
- break;
- } else if (event.type === "turn.failed") {
- throw new Error(event.error.message);
- } else if (event.type === "error") {
- // Codex exec currently emits retry-in-progress notifications as error events.
- lastStreamError = event.message;
- appendStreamDiagnostic(diagnostics, event.message);
- }
- }
- if (!turnCompleted) {
- const detail = lastStreamError ? `: ${lastStreamError}` : "";
+ if (event.type === "thread.started") {
+ await request.onThreadStarted?.(event.thread_id);
+ } else if (event.type === "item.completed") {
+ appendItemDiagnostic(diagnostics, event.item);
+ } else if (event.type === "turn.completed") {
+ request.signal.removeEventListener("abort", forwardAbort);
+ } else if (event.type === "turn.failed") {
+ throw new Error(event.error.message);
+ } else if (event.type === "error") {
+ // Codex exec emits retry-in-progress notifications as error events.
+ appendStreamDiagnostic(diagnostics, event.message);
+ }
+ },
+ });
+ if (turn.status !== "completed") {
+ const detail = turn.lastStreamError
+ ? `: ${turn.lastStreamError}`
+ : "";
throw new Error(
`Codex worker stream ended before turn.completed${detail}`,
);
}
return {
- threadId: threadId ?? thread.id ?? undefined,
+ threadId: turn.threadId ?? thread.id ?? undefined,
...(diagnostics.length > 0 ? { diagnostics } : {}),
};
} finally {
@@ -339,21 +341,35 @@ function workerSubagentConfig(subagents: number, inheritedFeatures: unknown) {
}
function workerPermissionProfile(sandbox: DeepWorkerParentSandbox) {
+ const filesystem = new Map<string, string | Record<string, string>>([
+ [":root", "read"],
+ ...sandbox.filesystemDenies.map((key): [string, string] => [key, "deny"]),
+ ]);
+ for (const literal of sandbox.literalFilesystemDenies ?? []) {
+ if (sandbox.filesystemDenies.includes(literal)) {
+ const root = parse(literal).root;
+ const scope = filesystem.get(root);
+ filesystem.set(root, {
+ ...(typeof scope === "object"
+ ? scope
+ : scope === undefined
+ ? {}
+ : { ".": scope }),
+ [literal.slice(root.length)]: "deny",
+ });
+ }
+ filesystem.set(literal, { ".": "deny" });
+ }
return {
extends: ":read-only",
// Object.fromEntries preserves literal keys such as "__proto__" without
// letting a denied path mutate the serializer object prototype.
- filesystem: Object.fromEntries([
- [":root", "read"],
- ...Array.from(sandbox.filesystemDenies, (key) => [key, "deny"]),
- ...Array.from(sandbox.literalFilesystemDenies ?? [], (key) => [
- key,
- { ".": "deny" },
- ]),
+ filesystem: {
+ ...Object.fromEntries(filesystem),
...(sandbox.globScanMaxDepth === undefined
- ? []
- : [["glob_scan_max_depth", sandbox.globScanMaxDepth]]),
- ]),
+ ? {}
+ : { glob_scan_max_depth: sandbox.globScanMaxDepth }),
+ },
network: { enabled: false },
};
}
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/parent-sandbox.ts b/plugins/codex-security/mcp-app/src/deep-scan/parent-sandbox.ts
index 19f5cb94a..e2356dd17 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/parent-sandbox.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/parent-sandbox.ts
@@ -131,12 +131,6 @@ export function resolveDeepWorkerParentSandbox(
}
}
- if (literalFilesystemDenies.some((path) => filesystemDenies.includes(path))) {
- throw unsupportedParentSandbox(
- "literal path and glob denials with the same key cannot be preserved",
- );
- }
-
if (!hasRootRead) {
throw unsupportedParentSandbox(
"the parent restricts readable paths beyond the supported read-only worker sandbox",
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
index 7cc9d0fb8..8de208a01 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
@@ -80,8 +80,11 @@ const trustedParentSandboxWithDenials = Object.freeze({
"/repo/.env",
"/repo/**/.secret",
"/repo/**/*.pem",
+ "/repo/temp[1]",
+ "/repo/secret[1]",
"/repo/.env",
],
+ literalFilesystemDenies: ["/repo/temp[1]", "/repo/secret[1]"],
globScanMaxDepth: 3,
});
const emptyWorkerPermissionProfile = {
@@ -98,6 +101,9 @@ const deniedWorkerPermissionProfile = {
"/repo/.env": "deny",
"/repo/**/.secret": "deny",
"/repo/**/*.pem": "deny",
+ "/repo/temp[1]": { ".": "deny" },
+ "/repo/secret[1]": { ".": "deny" },
+ "/": { "repo/temp[1]": "deny", "repo/secret[1]": "deny" },
glob_scan_max_depth: 3,
},
network: { enabled: false },
@@ -106,6 +112,8 @@ const deniedWorkerPermissionProfile = {
try {
testCodeModeFrameDiagnosticBoundaries();
await testOpenAiCredentialsReachWorker();
+ await testWorkerCancellation();
+ await testNullUsageCompletion();
await testWorkerRuntimeSettings();
await testUnsupportedProviderSnapshotFailsBeforeLaunch();
await testWorkerCyberAccessSettings();
@@ -1530,7 +1538,11 @@ env_key = "SYNTHETIC_GATEWAY_KEY"`,
filesystemDenies: [
...trustedParentSandboxWithDenials.filesystemDenies,
],
- literalFilesystemDenies: [deepPath, codexHome],
+ literalFilesystemDenies: [
+ ...trustedParentSandboxWithDenials.literalFilesystemDenies,
+ deepPath,
+ codexHome,
+ ],
globScanMaxDepth: trustedParentSandboxWithDenials.globScanMaxDepth,
};
const provider = index === 0 ? undefined : "synthetic.gateway";
@@ -1588,6 +1600,15 @@ env_key = "SYNTHETIC_GATEWAY_KEY"`,
`native-state-${index}`,
),
},
+ authentication:
+ index === 0
+ ? {}
+ : {
+ cli_auth_credentials_store:
+ index % 2 ? "file" : "ephemeral",
+ forced_login_method: index % 2 ? "api" : "chatgpt",
+ forced_chatgpt_workspace_id: `synthetic-workspace-${index}`,
+ },
endpoint,
serviceTier,
instructionsFile,
@@ -1661,6 +1682,7 @@ env_key = "SYNTHETIC_GATEWAY_KEY"`,
entry.deepPath,
stringifyToml({
worker_runtime: {
+ ...entry.authentication,
...entry.workerTelemetry,
...(entry.endpoint === undefined
? {}
@@ -1904,6 +1926,7 @@ env_key = "SYNTHETIC_GATEWAY_KEY"`,
assert.equal(process.env.SYNTHETIC_GATEWAY_KEY, undefined);
assert.equal(process.env.SYNTHETIC_HEADER_VALUE, undefined);
assertConfigOverrides(invocation.argv, {
+ ...workerConfigurations[index].authentication,
openai_base_url: workerConfigurations[index].endpoint,
model_reasoning_summary: expected,
service_tier: workerConfigurations[index].serviceTier,
@@ -3373,6 +3396,7 @@ const permissionProfileFallbackWarning = 'Configured value for \`permission_prof
if (stdin.includes('PERMISSION_PROFILE_FALLBACK_ITEM')) console.log(JSON.stringify({ type: 'item.completed', item: { id: 'warning-1', type: 'error', message: permissionProfileFallbackWarning } }));
if (stdin.includes('PERMISSION_PROFILE_FALLBACK_EVENT')) console.log(JSON.stringify({ type: 'error', message: permissionProfileFallbackWarning }));
if (stdin.includes('BLOCK_AFTER_START')) await new Promise(() => {});
+if (stdin.includes('FAIL_THEN_HANG')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'fixture worker failed' } })); setInterval(() => {}, 1_000); await new Promise(() => {}); }
if (stdin.includes('RATE_LIMIT_CYBER_POLICY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: '429 Too Many Requests: Request blocked by cyberPolicy.' } })); process.exit(0); }
if (stdin.includes('CYBER_POLICY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'Request blocked by cyberPolicy.' } })); process.exit(0); }
if (stdin.includes('SAFETY_POLICY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'Request blocked by a safety policy violation.' } })); process.exit(0); }
@@ -3390,7 +3414,7 @@ if (stdin.includes('ARTIFACT_TOOL_')) {
console.log(JSON.stringify({ type: 'item.completed', item }));
}
console.log(JSON.stringify({ type: 'item.completed', item: { id: 'message-1', type: 'agent_message', text: 'fixture final response' } }));
-console.log(JSON.stringify({ type: 'turn.completed', usage: { input_tokens: 1, cached_input_tokens: 0, output_tokens: 1 } }));
+console.log(JSON.stringify({ type: 'turn.completed', usage: stdin.includes('NULL_USAGE') ? null : { input_tokens: 1, cached_input_tokens: 0, output_tokens: 1 } }));
if (stdin.includes('COMPLETE_THEN_HANG')) { setInterval(() => {}, 1_000); await new Promise(() => {}); }
}
`,
@@ -3511,3 +3535,191 @@ async function withWorkerFixture<Result>(
restoreEnv("CODEX_CLI_PATH", previousPath);
}
}
+
+async function testWorkerCancellation() {
+ const fixture = await fakeCodexFixture();
+ const previousPath = process.env.CODEX_CLI_PATH;
+ const originalSpawn = childProcess.spawn;
+ let worker: ChildProcess | undefined;
+ let workerSignal: AbortSignal | undefined;
+ let cancelDuringCleanup: (() => void) | undefined;
+ let timeout: NodeJS.Timeout | undefined;
+ childProcess.spawn = ((
+ command: string,
+ args: readonly string[] = [],
+ options: SpawnOptions = {},
+ ) => {
+ const child = originalSpawn(
+ command,
+ command === process.execPath ||
+ command === path.toNamespacedPath(process.execPath)
+ ? [fixture.executablePath, ...args]
+ : args,
+ options,
+ );
+ if (args[0] === "exec") {
+ assertFlagPair(args, "--thread-source", "security_scan");
+ worker = child;
+ workerSignal = options.signal;
+ const kill = child.kill;
+ child.kill = function (...args: Parameters<ChildProcess["kill"]>) {
+ cancelDuringCleanup?.();
+ return kill.apply(this, args);
+ };
+ }
+ return child;
+ }) as typeof childProcess.spawn;
+ syncBuiltinESMExports();
+ process.env.CODEX_CLI_PATH = process.execPath;
+ try {
+ const promptPath = path.join(fixture.root, "prompt.md");
+ const workingDirectory = path.join(fixture.root, "artifacts");
+ await mkdir(workingDirectory);
+ const executor = new CodexSdkWorkerExecutor({
+ parentSandbox: trustedParentSandbox,
+ });
+ const run = (controller: AbortController, onThreadStarted?: () => void) =>
+ executor.run({
+ kind: "discovery",
+ promptPath,
+ workingDirectory,
+ subagents: 0,
+ signal: controller.signal,
+ onThreadStarted,
+ });
+ const preAborted = new AbortController();
+ const beforeStartup = new Error(
+ "coordinator canceled before worker startup",
+ );
+ preAborted.abort(beforeStartup);
+ await assert.rejects(
+ run(preAborted),
+ (error: Error) => error === beforeStartup,
+ );
+ assert.equal(worker, undefined);
+ await assert.rejects(readFile(fixture.preflightMarkerPath), {
+ code: "ENOENT",
+ });
+ await assert.rejects(readFile(fixture.markerPath), { code: "ENOENT" });
+
+ for (const prompt of [
+ "BLOCK_AFTER_START",
+ "COMPLETE_THEN_HANG",
+ "FAIL_THEN_HANG",
+ ]) {
+ await writeFile(promptPath, `${prompt}\n`);
+ const controller = new AbortController();
+ const cancellation = new Error(
+ "coordinator canceled its remaining workers",
+ );
+ // Exercise cancellation inside the real SDK's iterator cleanup, before kill returns.
+ cancelDuringCleanup =
+ prompt === "COMPLETE_THEN_HANG"
+ ? () => controller.abort(cancellation)
+ : undefined;
+ const deadline = new Promise<never>((_, reject) => {
+ timeout = setTimeout(
+ () => reject(new Error(`${prompt} worker did not settle`)),
+ 10_000,
+ );
+ });
+ const execution = Promise.race([
+ run(controller, () => {
+ if (prompt === "BLOCK_AFTER_START") controller.abort(cancellation);
+ }),
+ deadline,
+ ]);
+ if (prompt === "BLOCK_AFTER_START") {
+ await assert.rejects(
+ execution,
+ (error: Error) =>
+ error?.name === "AbortError" ||
+ /abort|SIGTERM/i.test(error?.message ?? ""),
+ );
+ assert(workerSignal);
+ assert.equal(workerSignal.aborted, true);
+ assert.equal(workerSignal.reason, cancellation);
+ } else {
+ if (prompt === "FAIL_THEN_HANG") {
+ await assert.rejects(execution, /fixture worker failed/);
+ controller.abort(cancellation);
+ } else {
+ const result = await execution;
+ assert.equal(result.threadId, "fixture-thread-id");
+ }
+ assert.equal(controller.signal.aborted, true);
+ assert(workerSignal);
+ assert.equal(workerSignal.aborted, false);
+ }
+ assert.notEqual(workerSignal, controller.signal);
+ const currentWorker = worker as ChildProcess | undefined;
+ assert(currentWorker);
+ assert.equal(currentWorker.killed, true);
+ if (
+ currentWorker.exitCode === null &&
+ currentWorker.signalCode === null
+ ) {
+ await Promise.race([
+ new Promise<void>((resolve) =>
+ worker!.once("close", () => resolve()),
+ ),
+ deadline,
+ ]);
+ }
+ clearTimeout(timeout);
+ cancelDuringCleanup = undefined;
+ }
+ } finally {
+ clearTimeout(timeout);
+ cancelDuringCleanup = undefined;
+ if (worker && worker.exitCode === null && worker.signalCode === null)
+ worker.kill("SIGKILL");
+ childProcess.spawn = originalSpawn;
+ syncBuiltinESMExports();
+ restoreEnv("CODEX_CLI_PATH", previousPath);
+ }
+}
+
+async function testNullUsageCompletion() {
+ const fixture = await fakeCodexFixture();
+ const previousPath = process.env.CODEX_CLI_PATH;
+ const originalSpawn = childProcess.spawn;
+ childProcess.spawn = ((
+ command: string,
+ args: readonly string[] = [],
+ options: SpawnOptions = {},
+ ) =>
+ originalSpawn(
+ command,
+ command === process.execPath ||
+ command === path.toNamespacedPath(process.execPath)
+ ? [fixture.executablePath, ...args]
+ : args,
+ options,
+ )) as typeof childProcess.spawn;
+ syncBuiltinESMExports();
+ process.env.CODEX_CLI_PATH = process.execPath;
+ try {
+ const promptPath = path.join(fixture.root, "prompt.md");
+ await writeFile(promptPath, "NULL_USAGE\n");
+ for (const kind of ["discovery", "dedup"] as const) {
+ for (const resumeThreadId of [undefined, "fixture-resumed-thread"]) {
+ const result = await new CodexSdkWorkerExecutor({
+ parentSandbox: trustedParentSandbox,
+ }).run({
+ kind,
+ promptPath,
+ workingDirectory: fixture.root,
+ subagents: 0,
+ resumeThreadId,
+ signal: new AbortController().signal,
+ });
+ assert.equal(result.threadId, resumeThreadId ?? "fixture-thread-id");
+ }
+ }
+ } finally {
+ childProcess.spawn = originalSpawn;
+ syncBuiltinESMExports();
+ restoreEnv("CODEX_CLI_PATH", previousPath);
+ }
+}
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.ts
index 388c753cb..1fd7b135a 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.ts
@@ -114,21 +114,6 @@ assert.deepEqual(
{ filesystemDenies: [], literalFilesystemDenies: literalPaths },
);
-const collidingDenies = [
- { path: { type: "glob_pattern", pattern: "/repo/[ab]" }, access: "deny" },
- { path: { type: "path", path: "/repo/[ab]" }, access: "none" },
-];
-for (const entries of [collidingDenies, [...collidingDenies].reverse()]) {
- assert.throws(
- () => resolveDeepWorkerParentSandbox(restricted([rootRead, ...entries])),
- (error: Error) =>
- error.name === "DeepScanNonRetryableError" &&
- /literal path and glob denials with the same key cannot be preserved/i.test(
- error.message,
- ),
- );
-}
-
assert.throws(
() =>
resolveDeepWorkerParentSandbox(
@@ -148,6 +133,20 @@ assert.throws(
/symbolic project-roots denial metadata/i.test(error.message),
);
+const collidingDenies = [
+ { path: { type: "glob_pattern", pattern: "/repo/[ab]" }, access: "deny" },
+ { path: { type: "path", path: "/repo/[ab]" }, access: "none" },
+];
+for (const entries of [collidingDenies, [...collidingDenies].reverse()]) {
+ assert.deepEqual(
+ resolveDeepWorkerParentSandbox(restricted([rootRead, ...entries])),
+ {
+ filesystemDenies: ["/repo/[ab]"],
+ literalFilesystemDenies: ["/repo/[ab]"],
+ },
+ );
+}
+
const pinnedFileUri = extra(
pinnedReadOnly,
"file:///tmp/codex-security-parent",
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts
index c65e64550..63d217561 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts
@@ -47,12 +47,14 @@ const expectedProfile = {
filesystem: {
":root": "read",
"/repo/.env": "deny",
+ "/repo/temp[1]": { ".": "deny" },
+ "/": { "repo/temp[1]": "deny" },
},
network: { enabled: false },
};
const rawOverrides = [
`default_permissions="${profileId}"`,
- `permissions.${profileId}={filesystem={":root"="read","/repo/.env"="deny"},network={enabled=false}}`,
+ `permissions.${profileId}={filesystem={":root"="read","/repo/.env"="deny","/repo/temp[1]"={"."="deny"},"/"={"repo/temp[1]"="deny"}},network={enabled=false}}`,
];
const unsupportedConfiguration = (error: Error) =>
@@ -69,6 +71,7 @@ await testRepeatedCatalogCursorFailsClosed();
await testDisallowedProfileGivesAdminGuidance();
await testOtherManagedPolicyRejectionIsGeneric();
await testMergedProfileCollisionFailsClosed();
+await testDroppedLiteralOrGlobFailsClosed();
await testLiteralProtoKeyCollisionFailsClosed();
await testSelectedProfileClassificationRequiresVerifiedString();
await testMalformedAndUnsupportedResponsesFailClosed();
@@ -428,6 +431,27 @@ async function testMergedProfileCollisionFailsClosed() {
);
}
+async function testDroppedLiteralOrGlobFailsClosed() {
+ for (const key of ["/repo/temp[1]", "/"] as const) {
+ const weakened = structuredClone(expectedProfile);
+ delete weakened.filesystem[key];
+ await withFakeCodex(
+ {
+ configResult: configReadResult(weakened),
+ catalogResults: [catalogResult(true)],
+ },
+ async ({ codexPath, cwd }) => {
+ await assert.rejects(
+ preflight(codexPath, cwd),
+ (error: Error) =>
+ error?.name === "DeepScanNonRetryableError" &&
+ error.message.includes("existing Codex configuration changes"),
+ );
+ },
+ );
+ }
+}
+
async function testLiteralProtoKeyCollisionFailsClosed() {
const profileWithLiteralProtoKey = Object.fromEntries([
...Object.entries(expectedProfile),
diff --git a/plugins/codex-security/plugin-files.json b/plugins/codex-security/plugin-files.json
index b051eb870..b7b6ac3b5 100644
--- a/plugins/codex-security/plugin-files.json
+++ b/plugins/codex-security/plugin-files.json
@@ -59,6 +59,7 @@
"schemas/tools/scan-draft.schema.json",
"scripts/config_preflight.py",
"scripts/codex_profile.mjs",
+ "scripts/codex_session.mjs",
"scripts/deep_scan_config.py",
"scripts/deep_scan_defaults.json",
"scripts/deep_scan_workbench.py",
diff --git a/plugins/codex-security/scripts/codex_session.d.mts b/plugins/codex-security/scripts/codex_session.d.mts
new file mode 100644
index 000000000..49392cb7a
--- /dev/null
+++ b/plugins/codex-security/scripts/codex_session.d.mts
@@ -0,0 +1,12 @@
+export function readCodexSessionTurn<Event extends { type: string }>(options: {
+ thread: { readonly id: string | null };
+ events: AsyncGenerator<Event>;
+ onEvent: (event: Event) => Promise<void> | void;
+ stopOnCompletion?: boolean;
+}): Promise<{
+ threadId: string | null;
+ status: "in_progress" | "completed";
+ finalResponse: string;
+ usage: unknown;
+ lastStreamError: string | null;
+}>;
diff --git a/plugins/codex-security/scripts/codex_session.mjs b/plugins/codex-security/scripts/codex_session.mjs
new file mode 100644
index 000000000..d15b899dd
--- /dev/null
+++ b/plugins/codex-security/scripts/codex_session.mjs
@@ -0,0 +1,53 @@
+/** Reduce a single stream; callers retain error, retry and acceptance policy. */
+export async function readCodexSessionTurn(options) {
+ let threadId = options.thread.id;
+ let status = "in_progress";
+ let finalResponse = "";
+ let usage = null;
+ let lastStreamError = null;
+ for await (const event of eventsWithOptionalUsage(options.events)) {
+ await options.onEvent(event);
+ if (
+ event.type === "thread.started" &&
+ typeof event["thread_id"] === "string"
+ ) {
+ threadId = event["thread_id"];
+ } else if (
+ event.type === "item.completed" &&
+ isRecord(event["item"]) &&
+ event["item"]["type"] === "agent_message" &&
+ typeof event["item"]["text"] === "string"
+ ) {
+ finalResponse = event["item"]["text"];
+ } else if (event.type === "turn.completed") {
+ status = "completed";
+ usage = event["usage"] ?? null;
+ if (options.stopOnCompletion) break;
+ } else if (event.type === "error" && typeof event["message"] === "string") {
+ lastStreamError = event["message"];
+ }
+ }
+ return { threadId, status, finalResponse, usage, lastStreamError };
+}
+
+async function* eventsWithOptionalUsage(events) {
+ try {
+ yield* events;
+ } catch (error) {
+ // The pinned SDK accesses this field before yielding a completion with
+ // absent usage. Preserve completion without inventing a zero-token receipt.
+ if (
+ error instanceof TypeError &&
+ /\b(?:null|undefined)\b/u.test(error.message) &&
+ /\bcache_write_input_tokens\b/u.test(error.message)
+ ) {
+ yield { type: "turn.completed", usage: null };
+ return;
+ }
+ throw error;
+ }
+}
+
+function isRecord(value) {
+ return typeof value === "object" && value !== null;
+}
diff --git a/sdk/typescript/scripts/fixtures/package-deep-codex.mjs b/sdk/typescript/scripts/fixtures/package-deep-codex.mjs
new file mode 100644
index 000000000..c57ba26ae
--- /dev/null
+++ b/sdk/typescript/scripts/fixtures/package-deep-codex.mjs
@@ -0,0 +1,178 @@
+#!/usr/bin/env node
+import assert from "node:assert/strict";
+import { existsSync } from "node:fs";
+import { appendFile, writeFile } from "node:fs/promises";
+import { basename, join } from "node:path";
+import { createInterface } from "node:readline";
+import { startRpc } from "./package-rpc.mjs";
+import { parse } from "./package-toml.cjs";
+
+try {
+ await run();
+ process.exit(0);
+} catch (error) {
+ await trace({ phase: "fixture-error", error: error.stack });
+ console.error(error);
+ process.exit(1);
+}
+
+async function trace(event) {
+ await appendFile(
+ process.env.PACKAGE_DEEP_TRACE,
+ `${JSON.stringify({ ...event, python: process.env.PYTHON })}\n`,
+ );
+}
+
+async function run() {
+ const args = process.argv.slice(2);
+ if (args.includes("app-server")) {
+ await trace({ phase: "preflight", args });
+ for await (const line of createInterface({ input: process.stdin })) {
+ const message = JSON.parse(line);
+ if (message.id === undefined) continue;
+ let result;
+ switch (message.method) {
+ case "initialize":
+ result = { userAgent: "package-fixture" };
+ break;
+ case "config/read":
+ result = {
+ config: {
+ default_permissions: "codex_security_deep_scan_worker",
+ permissions: {
+ codex_security_deep_scan_worker: {
+ extends: ":read-only",
+ filesystem: { ":root": "read" },
+ network: { enabled: false },
+ },
+ },
+ },
+ origins: {},
+ layers: null,
+ };
+ break;
+ case "permissionProfile/list":
+ result = {
+ data: [
+ {
+ id: "codex_security_deep_scan_worker",
+ description: null,
+ allowed: true,
+ },
+ ],
+ nextCursor: null,
+ };
+ break;
+ case "account/read":
+ result = { account: null, requiresOpenaiAuth: true };
+ break;
+ default:
+ throw new Error(`Unexpected preflight method: ${message.method}`);
+ }
+ console.log(JSON.stringify({ jsonrpc: "2.0", id: message.id, result }));
+ }
+ return;
+ }
+ let prompt = "";
+ for await (const chunk of process.stdin) prompt += chunk;
+ const overrides = [];
+ for (let index = 0; index < args.length; index++) {
+ if (args[index] === "-c" || args[index] === "--config")
+ overrides.push(args[++index]);
+ }
+ const config = parse(overrides.join("\n"));
+ const artifacts = config.mcp_servers.cs_artifacts;
+ const env = artifacts.env;
+ const root = env.CODEX_SECURITY_ARTIFACT_ROOT;
+ assert.ok(root, "The real worker must supply its bound artifact root.");
+ assert.equal(config.mcp_servers["codex-security"].enabled, false);
+ assert.notEqual(artifacts.enabled, false);
+ const layout = env.CODEX_SECURITY_ARTIFACT_LAYOUT;
+ const threadId = `package-${layout}-${basename(root)}-${basename(join(root, ".."))}`;
+ console.log(JSON.stringify({ type: "thread.started", thread_id: threadId }));
+ if (
+ layout === "worker" &&
+ basename(join(root, "..")) === "discovery-0002" &&
+ process.env.PACKAGE_DEEP_HOLD &&
+ existsSync(process.env.PACKAGE_DEEP_HOLD)
+ ) {
+ await trace({ phase: "held", scanId: env.CODEX_SECURITY_SCAN_ID });
+ await new Promise(() => setInterval(() => {}, 1_000));
+ }
+ const server = await startRpc(artifacts.command, artifacts.args, {
+ cwd: root,
+ env: { ...process.env, ...env },
+ });
+ let complete = true;
+ try {
+ if (layout === "worker") {
+ const draft = {
+ scanId: env.CODEX_SECURITY_SCAN_ID,
+ findings: [],
+ coverage: {
+ completeness: "complete",
+ surfaces: [],
+ explicitExclusions: [],
+ deferred: [],
+ },
+ };
+ const marker = process.env.PACKAGE_DEEP_EMPTY_ONCE;
+ if (marker && !existsSync(marker)) {
+ await writeFile(marker, "process completed without a final artifact");
+ complete = false;
+ } else {
+ await server.call("record_codex_security_scan_draft", {
+ ...draft,
+ complete: false,
+ });
+ await server.call("record_codex_security_scan_draft", {
+ ...draft,
+ complete: true,
+ });
+ }
+ } else {
+ assert.equal(layout, "reducer");
+ let cursor;
+ let json = "";
+ do {
+ const page = await server.call(
+ "get_codex_security_deep_reducer_inputs",
+ {
+ maxBytes: 64_000,
+ ...(cursor === undefined ? {} : { cursor }),
+ },
+ );
+ json += page.json;
+ cursor = page.nextCursor;
+ } while (cursor !== undefined);
+ const inputs = JSON.parse(json);
+ assert.ok(inputs.discoveries.length > 0);
+ await server.call("record_codex_security_deep_reduction", {
+ scanId: env.CODEX_SECURITY_SCAN_ID,
+ findings: [],
+ });
+ }
+ await trace({
+ phase: layout,
+ complete,
+ resumed: args.includes("resume"),
+ scanId: env.CODEX_SECURITY_SCAN_ID,
+ home: process.env.CODEX_HOME,
+ hasApiKey: process.env.CODEX_API_KEY === "synthetic-package-deep-key",
+ root,
+ args,
+ });
+ console.log(
+ JSON.stringify({
+ type: "turn.completed",
+ usage: {
+ input_tokens: 1,
+ cached_input_tokens: 0,
+ output_tokens: 1,
+ },
+ }),
+ );
+ } finally {
+ await server.close();
+ }
+}
diff --git a/sdk/typescript/scripts/fixtures/package-deep-scan.mjs b/sdk/typescript/scripts/fixtures/package-deep-scan.mjs
new file mode 100644
index 000000000..4abe15ee9
--- /dev/null
+++ b/sdk/typescript/scripts/fixtures/package-deep-scan.mjs
@@ -0,0 +1,509 @@
+import assert from "node:assert/strict";
+import { execFile } from "node:child_process";
+import { randomUUID } from "node:crypto";
+import {
+ chmod,
+ copyFile,
+ cp,
+ mkdir,
+ mkdtemp,
+ readFile,
+ realpath,
+ rm,
+ stat,
+ writeFile,
+} from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { createRequire } from "node:module";
+import { dirname, join } from "node:path";
+import { pathToFileURL } from "node:url";
+import { promisify } from "node:util";
+import { startRpc } from "./package-rpc.mjs";
+import { packageSmokeTimeouts } from "../package-smoke-timeouts.mjs";
+
+const installedRoot = await realpath(process.argv[2]);
+const { resolvePluginPython } = await import(
+ pathToFileURL(join(installedRoot, "dist", "runtime.js")).href
+);
+const root = await realpath(
+ await mkdtemp(join(tmpdir(), "package deep % fixture-")),
+);
+const installedPlugin = join(installedRoot, "_bundled_plugin");
+try {
+ const detachedPlugin = join(root, "standalone plugin %", "codex-security");
+ await cp(installedPlugin, detachedPlugin, { recursive: true });
+ // Assert physical independence instead of merely changing the working directory.
+ for (let ancestor = detachedPlugin; ; ancestor = dirname(ancestor)) {
+ for (const dependency of ["node_modules", join("sdk", "typescript")]) {
+ await assert.rejects(stat(join(ancestor, dependency)), {
+ code: "ENOENT",
+ });
+ }
+ if (ancestor === dirname(ancestor)) break;
+ }
+ for (const name of [
+ "package-deep-codex.mjs",
+ "package-rpc.mjs",
+ "package-deep-spawn.mjs",
+ ]) {
+ await copyFile(new URL(name, import.meta.url), join(root, name));
+ }
+ const installedRequire = createRequire(join(installedRoot, "package.json"));
+ await copyFile(
+ installedRequire.resolve("smol-toml"),
+ join(root, "package-toml.cjs"),
+ );
+ const executable = join(
+ root,
+ process.platform === "win32"
+ ? "package-codex.exe"
+ : "package-deep-codex.mjs",
+ );
+ if (process.platform === "win32")
+ await copyFile(process.execPath, executable);
+ await chmod(executable, 0o700);
+
+ await runInstalledSdk(installedPlugin, executable);
+ await runDetachedPlugin(detachedPlugin, executable);
+ console.log(
+ "Validated installed SDK and detached plugin: real Deep processes, bound artifact tools, checkpoints, reducer acceptance, restart before finalization, and sealed results.",
+ );
+} catch (error) {
+ for (const name of ["installed", "detached"]) {
+ try {
+ error.message += `\n${await readFile(join(root, name, "executions.jsonl"), "utf8")}`;
+ } catch (readError) {
+ if (readError.code !== "ENOENT") throw readError;
+ }
+ }
+ throw error;
+} finally {
+ await rm(root, {
+ recursive: true,
+ force: true,
+ maxRetries: 10,
+ retryDelay: 100,
+ });
+}
+
+async function fixture(name, pluginRoot, executable) {
+ const directory = join(root, name);
+ const target = join(directory, "target with spaces %");
+ const home = join(directory, "home");
+ await mkdir(target, { recursive: true });
+ await mkdir(join(home, "codex-security"), { recursive: true });
+ await writeFile(
+ join(target, "fixture.py"),
+ "print('synthetic package fixture')\n",
+ );
+ await writeFile(
+ join(home, "codex-security", "config.toml"),
+ "[deep_scan]\nworkers = 1\nsubagents = 0\nstop_after_no_new = 1\nmax_discovery_runs = 2\n",
+ );
+ const env = Object.fromEntries(
+ [
+ "PATH",
+ "Path",
+ "SystemRoot",
+ "WINDIR",
+ "ComSpec",
+ "PATHEXT",
+ "TMP",
+ "TEMP",
+ "TMPDIR",
+ "PYTHON",
+ ]
+ .filter((key) => process.env[key] !== undefined)
+ .map((key) => [key, process.env[key]]),
+ );
+ Object.assign(env, {
+ HOME: home,
+ USERPROFILE: home,
+ CODEX_HOME: home,
+ CODEX_CLI_PATH: executable,
+ CODEX_SECURITY_PLUGIN_ROOT: pluginRoot,
+ CODEX_SECURITY_STATE_DIR: join(directory, "state"),
+ CODEX_SECURITY_SCAN_ROOT: join(directory, "scans"),
+ OPENAI_API_KEY: "synthetic-package-deep-key",
+ ...(process.platform === "win32"
+ ? {
+ PACKAGE_DEEP_EXECUTABLE: executable,
+ NODE_OPTIONS: `--import=${pathToFileURL(join(root, "package-deep-spawn.mjs")).href}`,
+ }
+ : {}),
+ PACKAGE_DEEP_TRACE: join(directory, "executions.jsonl"),
+ });
+ env.PYTHON = await resolvePluginPython({
+ environment: env,
+ protectedRoot: target,
+ homeDirectory: home,
+ });
+ return { directory, target, home, env, pluginRoot };
+}
+
+function metadata(f, owner) {
+ return {
+ "openai/threadId": owner,
+ "codex/sandbox-state-meta": {
+ permissionProfile: {
+ type: "managed",
+ file_system: {
+ type: "restricted",
+ entries: [
+ {
+ path: { type: "special", value: { kind: "root" } },
+ access: "read",
+ },
+ ],
+ },
+ network: "restricted",
+ },
+ sandboxCwd: pathToFileURL(f.target).href,
+ },
+ "x-codex-turn-metadata": { model: "gpt-5.5", reasoning_effort: "high" },
+ };
+}
+
+function server(f, env = f.env) {
+ return startRpc(
+ process.execPath,
+ [join(f.pluginRoot, "mcp", "server.mjs"), "--stdio"],
+ {
+ cwd: f.target,
+ env,
+ requestTimeoutMs: packageSmokeTimeouts().commandTimeoutMs,
+ },
+ );
+}
+
+async function runDetachedPlugin(pluginRoot, executable) {
+ const f = await fixture("detached", pluginRoot, executable);
+ const owner = "package-detached-owner";
+ f.env.PACKAGE_DEEP_HOLD = join(f.directory, "hold-second-worker");
+ await writeFile(f.env.PACKAGE_DEEP_HOLD, "hold");
+ let rpc = await server(f);
+ let scanId;
+ let scanDir;
+ let partial;
+ const handoffClaimToken = randomUUID();
+ try {
+ const opened = await rpc.call(
+ "open_codex_security_workspace",
+ {
+ targetPath: f.target,
+ scope: ".",
+ mode: "deep",
+ },
+ metadata(f, owner),
+ );
+ const sessionId = opened.workspace.id;
+ await rpc.call(
+ "submit_codex_security_setup",
+ {
+ sessionId,
+ targetPath: f.target,
+ scope: ".",
+ mode: "deep",
+ },
+ metadata(f, owner),
+ );
+ const started = await rpc.call(
+ "start_codex_security_scan",
+ { sessionId },
+ metadata(f, owner),
+ );
+ ({ scanId, scanDir } = started.workspace.results);
+ await rpc.call(
+ "claim_codex_security_scan_handoff_delivery",
+ {
+ scanId,
+ claimToken: handoffClaimToken,
+ },
+ metadata(f, owner),
+ );
+ await rpc.call(
+ "attach_codex_security_scan_continuation_thread",
+ {
+ scanId,
+ claimToken: handoffClaimToken,
+ threadId: owner,
+ },
+ metadata(f, owner),
+ );
+ const pending = rpc
+ .call(
+ "start_codex_security_deep_scan",
+ { scanId, handoffClaimToken },
+ metadata(f, owner),
+ )
+ .catch((error) => error);
+ const deadline = Date.now() + 30_000;
+ while (!(await readExecutions(f)).some((entry) => entry.phase === "held")) {
+ assert.ok(
+ Date.now() < deadline,
+ "Second worker did not reach the interruption boundary.",
+ );
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ }
+ partial = (
+ await workbench(f, [
+ "get-deep-scan",
+ "--scan-id",
+ scanId,
+ "--thread-id",
+ owner,
+ ])
+ ).deepScan;
+ assert.equal(
+ partial.workers.filter(
+ (worker) =>
+ worker.kind === "discovery" && worker.status === "succeeded",
+ ).length,
+ 1,
+ );
+ await rpc.close();
+ await pending;
+ } finally {
+ await rpc.close();
+ }
+ // Simulate an expired owner lease without waiting for wall-clock expiry. Keep
+ // the real stored workers/results and use the production recovery path.
+ await rm(
+ join(
+ scanDir,
+ "artifacts",
+ "deep_discovery",
+ `coordinator-heartbeat-${partial.coordinatorGeneration}.json`,
+ ),
+ { force: true },
+ );
+ await promisify(execFile)(
+ f.env.PYTHON,
+ [
+ "-c",
+ "import sqlite3,sys; c=sqlite3.connect(sys.argv[1]); c.execute('UPDATE deep_scan_runs SET updated_at = ? WHERE scan_id = ?', ('2000-01-01T00:00:00Z',sys.argv[2])); c.commit()",
+ join(f.env.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
+ scanId,
+ ],
+ { env: f.env },
+ );
+ await rm(f.env.PACKAGE_DEEP_HOLD);
+ rpc = await server(f);
+ try {
+ const result = await rpc.call(
+ "start_codex_security_deep_scan",
+ { scanId, handoffClaimToken },
+ metadata(f, owner),
+ );
+ await assertDraft(result.manifestPath);
+ const recovered = (
+ await workbench(f, [
+ "get-deep-scan",
+ "--scan-id",
+ scanId,
+ "--thread-id",
+ owner,
+ ])
+ ).deepScan;
+ assert.equal(
+ recovered.coordinatorGeneration,
+ partial.coordinatorGeneration + 1,
+ );
+ assert.equal(recovered.dispatchedCount, 2);
+ const retained = partial.workers.find(
+ (worker) => worker.status === "succeeded",
+ );
+ assert.equal(
+ recovered.workers.find((worker) => worker.id === retained.id).status,
+ "succeeded",
+ );
+ } finally {
+ await rpc.close();
+ }
+ // Publication uses the saved aggregate after the recovered executor exits too.
+ rpc = await server(f);
+ try {
+ await rpc.call(
+ "complete_codex_security_scan",
+ { scanId, handoffClaimToken },
+ metadata(f, owner),
+ );
+ const completed = await rpc.call(
+ "get_codex_security_completed_scan",
+ { scanId, handoffClaimToken },
+ metadata(f, owner),
+ );
+ assert.equal(completed.manifest.scan.id, scanId);
+ assert.equal(completed.manifest.scan.status, "completed");
+ assert.ok(completed.manifest.scan.sealedAt);
+ } finally {
+ await rpc.close();
+ }
+ await assertExecutions(f, scanId, 4);
+}
+
+async function workbench(f, args) {
+ const { stdout } = await promisify(execFile)(
+ f.env.PYTHON,
+ [join(f.pluginRoot, "scripts", "workbench_db.py"), ...args],
+ {
+ env: f.env,
+ cwd: f.target,
+ maxBuffer: 4 * 1024 * 1024,
+ },
+ );
+ return JSON.parse(stdout);
+}
+
+async function runInstalledSdk(pluginRoot, executable) {
+ const f = await fixture("installed", pluginRoot, executable);
+ f.env.PACKAGE_DEEP_EMPTY_ONCE = join(
+ f.directory,
+ "missing-result-completion",
+ );
+ const sdk = await import(
+ pathToFileURL(join(installedRoot, "dist", "index.js")).href
+ );
+ const manifest = JSON.parse(
+ await readFile(join(pluginRoot, ".codex-plugin", "plugin.json"), "utf8"),
+ );
+ const owner = "package-sdk-owner";
+ let scanId;
+ const client = new sdk.CodexSecurity(
+ { pythonPath: f.env.PYTHON },
+ {
+ environment: f.env,
+ prepareRuntime: async () => ({
+ codexHome: f.home,
+ environment: f.env,
+ credentialsAvailable: true,
+ plugin: {
+ pluginRoot,
+ marketplaceRoot: pluginRoot,
+ installedRoot: pluginRoot,
+ marketplaceName: "codex-security-sdk",
+ name: manifest.name,
+ version: manifest.version,
+ },
+ }),
+ // Replace only the parent model's tool choice. The installed SDK registers
+ // and finalizes the scan; the packaged MCP runs the real Deep lifecycle.
+ createCodex({ env, apiKey }) {
+ return {
+ startThread() {
+ return {
+ id: owner,
+ async runStreamed() {
+ return {
+ events: (async function* () {
+ yield { type: "thread.started", thread_id: owner };
+ scanId = env.CODEX_SECURITY_SCAN_ID;
+ // The pinned SDK maps its apiKey option to this child variable.
+ const rpc = await server(f, {
+ ...env,
+ ...(apiKey ? { CODEX_API_KEY: apiKey } : {}),
+ });
+ try {
+ const result = await rpc.call(
+ "start_codex_security_deep_scan",
+ { scanId },
+ metadata(f, owner),
+ );
+ await assertDraft(result.manifestPath);
+ } finally {
+ await rpc.close();
+ }
+ yield {
+ type: "turn.completed",
+ usage: {
+ input_tokens: 1,
+ cached_input_tokens: 0,
+ output_tokens: 1,
+ },
+ };
+ })(),
+ };
+ },
+ };
+ },
+ };
+ },
+ },
+ );
+ try {
+ const result = await client.run(f.target, {
+ mode: "deep",
+ auth: "api-key",
+ workers: 1,
+ subagents: 0,
+ maxDiscoveryRuns: 2,
+ stopAfterNoNew: 1,
+ outputDir: join(f.directory, "output"),
+ });
+ assert.equal(result.threadId, owner);
+ assert.equal(result.manifest.scan.status, "completed");
+ assert.ok(result.manifest.scan.sealedAt);
+ assert.equal(result.manifest.scan.id, scanId);
+ assert.deepEqual(result.findings.findings, []);
+ assert.ok(
+ (await readFile(join(f.directory, "output", "report.md"), "utf8"))
+ .length > 0,
+ );
+ } finally {
+ await client.close();
+ }
+ await assertExecutions(f, scanId, 4);
+}
+
+async function assertDraft(path) {
+ const document = JSON.parse(await readFile(path, "utf8"));
+ const findings = JSON.parse(
+ await readFile(join(dirname(path), "findings.json"), "utf8"),
+ );
+ assert.deepEqual(findings.findings, []);
+ assert.ok(document.scan.target);
+}
+
+async function readExecutions(f) {
+ try {
+ return (await readFile(f.env.PACKAGE_DEEP_TRACE, "utf8"))
+ .trim()
+ .split("\n")
+ .filter(Boolean)
+ .map(JSON.parse);
+ } catch (error) {
+ if (error.code === "ENOENT") return [];
+ throw error;
+ }
+}
+
+async function assertExecutions(f, scanId, preflights = 3) {
+ const executions = await readExecutions(f);
+ for (const execution of executions) {
+ assert.equal(
+ await realpath(execution.python),
+ await realpath(f.env.PYTHON),
+ );
+ }
+ const workers = executions.filter((entry) => entry.phase === "worker");
+ const reducers = executions.filter((entry) => entry.phase === "reducer");
+ const incomplete = f.env.PACKAGE_DEEP_EMPTY_ONCE ? 1 : 0;
+ assert.equal(workers.length, 2 + incomplete);
+ assert.equal(workers.filter((entry) => !entry.complete).length, incomplete);
+ assert.equal(workers.filter((entry) => entry.resumed).length, incomplete);
+ assert.equal(reducers.length, 1);
+ assert.equal(
+ executions.filter((entry) => entry.phase === "preflight").length,
+ preflights,
+ );
+ for (const execution of [...workers, ...reducers]) {
+ assert.equal(execution.scanId, scanId);
+ assert.equal(execution.home, f.home);
+ assert.equal(execution.hasApiKey, true);
+ assert.equal(
+ execution.args[execution.args.indexOf("--model") + 1],
+ "gpt-5.5",
+ );
+ assert.ok(execution.args.includes('approval_policy="never"'));
+ }
+}
diff --git a/sdk/typescript/scripts/fixtures/package-deep-spawn.mjs b/sdk/typescript/scripts/fixtures/package-deep-spawn.mjs
new file mode 100644
index 000000000..e59440558
--- /dev/null
+++ b/sdk/typescript/scripts/fixtures/package-deep-spawn.mjs
@@ -0,0 +1,20 @@
+import childProcess from "node:child_process";
+import { syncBuiltinESMExports } from "node:module";
+import { win32 } from "node:path";
+import { fileURLToPath } from "node:url";
+
+// Windows cannot execute the POSIX fixture's shebang. Preserve the selected
+// native executable and its options, inserting only the deterministic protocol
+// script, as in the worker launch tests. Every other child runs unchanged.
+const executable = win32.toNamespacedPath(process.env.PACKAGE_DEEP_EXECUTABLE);
+const script = fileURLToPath(
+ new URL("package-deep-codex.mjs", import.meta.url),
+);
+const spawn = childProcess.spawn;
+childProcess.spawn = (command, args, options) =>
+ spawn(
+ command,
+ win32.toNamespacedPath(command) === executable ? [script, ...args] : args,
+ options,
+ );
+syncBuiltinESMExports();
diff --git a/sdk/typescript/scripts/fixtures/package-rpc.mjs b/sdk/typescript/scripts/fixtures/package-rpc.mjs
new file mode 100644
index 000000000..ceb6156f2
--- /dev/null
+++ b/sdk/typescript/scripts/fixtures/package-rpc.mjs
@@ -0,0 +1,90 @@
+import assert from "node:assert/strict";
+import { spawn } from "node:child_process";
+import { once } from "node:events";
+import { createInterface } from "node:readline";
+
+// This client deliberately uses only Node builtins. Detached plugin tests must
+// not resolve an MCP client or SDK from the checkout's node_modules.
+export async function startRpc(command, args, options) {
+ const { requestTimeoutMs = 30_000, ...spawnOptions } = options;
+ const child = spawn(command, args, {
+ ...spawnOptions,
+ stdio: ["pipe", "pipe", "pipe"],
+ windowsHide: true,
+ });
+ let sequence = 0;
+ let stderr = "";
+ const pending = new Map();
+ child.stderr.setEncoding("utf8").on("data", (chunk) => {
+ stderr += chunk;
+ });
+ createInterface({ input: child.stdout }).on("line", (line) => {
+ const response = JSON.parse(line);
+ const waiter = pending.get(response.id);
+ if (!waiter) return;
+ pending.delete(response.id);
+ clearTimeout(waiter.timer);
+ if (response.error)
+ waiter.reject(new Error(JSON.stringify(response.error)));
+ else waiter.resolve(response.result);
+ });
+ const exited = once(child, "exit");
+ child.on("exit", (code, signal) => {
+ for (const waiter of pending.values()) {
+ clearTimeout(waiter.timer);
+ waiter.reject(
+ new Error(`Fixture RPC exited (${code}, ${signal}): ${stderr}`),
+ );
+ }
+ pending.clear();
+ });
+ const client = {
+ child,
+ request(method, params = {}) {
+ return new Promise((resolve, reject) => {
+ const id = ++sequence;
+ const timer = setTimeout(() => {
+ pending.delete(id);
+ reject(new Error(`Fixture RPC timed out: ${method}\n${stderr}`));
+ }, requestTimeoutMs);
+ pending.set(id, { resolve, reject, timer });
+ child.stdin.write(
+ `${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`,
+ );
+ });
+ },
+ async call(name, args, meta) {
+ const result = await this.request("tools/call", {
+ name,
+ arguments: args,
+ ...(meta ? { _meta: meta } : {}),
+ });
+ assert.notEqual(result.isError, true, JSON.stringify(result));
+ return result.structuredContent ?? JSON.parse(result.content[0].text);
+ },
+ async close() {
+ if (child.exitCode !== null || child.signalCode !== null) return;
+ child.stdin.end();
+ const timeout = setTimeout(() => child.kill("SIGKILL"), 5_000);
+ try {
+ await exited;
+ } finally {
+ clearTimeout(timeout);
+ }
+ },
+ };
+ try {
+ await client.request("initialize", {
+ protocolVersion: "2025-11-25",
+ capabilities: {},
+ clientInfo: { name: "installed-deep-fixture", version: "1.0.0" },
+ });
+ child.stdin.write(
+ `${JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" })}\n`,
+ );
+ return client;
+ } catch (error) {
+ await client.close();
+ throw error;
+ }
+}
diff --git a/sdk/typescript/scripts/smoke-package.mjs b/sdk/typescript/scripts/smoke-package.mjs
index a3aea5cdd..d57b921e3 100644
--- a/sdk/typescript/scripts/smoke-package.mjs
+++ b/sdk/typescript/scripts/smoke-package.mjs
@@ -799,6 +799,15 @@ try {
);
await smokeNestedDeepScanWorker(installedRoot, consumer);
+ run(
+ process.execPath,
+ [
+ join(packageRoot, "scripts", "fixtures", "package-deep-scan.mjs"),
+ installedRoot,
+ ],
+ { cwd: consumer },
+ );
+
console.log(
`Validated installed ${packageManifest.name}@${packageManifest.version}: public import, NodeNext types, CLI, SDK lifecycle, credential locking, ${expectedPluginFiles.length} bundled plugin files, MCP initialization, bundled Codex version, and a nested worker without global codex.`,
);
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index 17a21a644..f85efa5c7 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -14,6 +14,7 @@ import {
import { randomUUID } from "node:crypto";
import { tmpdir } from "node:os";
import { isDeepStrictEqual } from "node:util";
+import { pathToFileURL } from "node:url";
import {
basename,
dirname,
@@ -3886,61 +3887,33 @@ async function readCodexTurn(options: {
usage: unknown;
lastStreamError: string | null;
}> {
- let threadId = options.thread.id;
- let status: "in_progress" | "completed" = "in_progress";
- let finalResponse = "";
- let usage: unknown = null;
- let lastStreamError: string | null = null;
- for await (const event of eventsWithOptionalUsage(options.events)) {
- await options.onEvent?.(event);
- if (
- event.type === "thread.started" &&
- typeof event["thread_id"] === "string"
- ) {
- threadId = event["thread_id"];
- } else if (
- event.type === "item.completed" &&
- isRecord(event["item"]) &&
- event["item"]["type"] === "agent_message" &&
- typeof event["item"]["text"] === "string"
- ) {
- finalResponse = event["item"]["text"];
- } else if (event.type === "turn.completed") {
- status = "completed";
- usage = event["usage"];
- } else if (event.type === "turn.failed") {
- throw new CodexSecurityError(turnFailureMessage(event["error"]));
- } else if (event.type === "error" && typeof event["message"] === "string") {
- const message = event["message"];
- const classification = classifyConnectionFailure(message);
- if (classification === "unauthorized" || classification === "forbidden") {
- throw new CodexSecurityError(message);
+ const { readCodexSessionTurn } = await import(
+ pathToFileURL(
+ join(await bundledPluginRoot(), "scripts", "codex_session.mjs"),
+ ).href
+ );
+ return readCodexSessionTurn({
+ ...options,
+ onEvent: async (event: ScanEvent) => {
+ await options.onEvent?.(event);
+ if (event.type === "turn.failed") {
+ throw new CodexSecurityError(turnFailureMessage(event["error"]));
}
- const reconnect = reconnectAttempt(message);
- if (reconnect === null) throw new CodexSecurityError(message);
- lastStreamError = message;
- options.onReconnect?.(message, reconnect);
- }
- }
- return { threadId, status, finalResponse, usage, lastStreamError };
-}
-
-async function* eventsWithOptionalUsage(
- events: AsyncGenerator<ScanEvent>,
-): AsyncGenerator<ScanEvent> {
- try {
- yield* events;
- } catch (error) {
- if (
- error instanceof TypeError &&
- /\b(?:null|undefined)\b/u.test(error.message) &&
- /\bcache_write_input_tokens\b/u.test(error.message)
- ) {
- yield { type: "turn.completed", usage: null };
- return;
- }
- throw error;
- }
+ if (event.type === "error" && typeof event["message"] === "string") {
+ const message = event["message"];
+ const classification = classifyConnectionFailure(message);
+ if (
+ classification === "unauthorized" ||
+ classification === "forbidden"
+ ) {
+ throw new CodexSecurityError(message);
+ }
+ const reconnect = reconnectAttempt(message);
+ if (reconnect === null) throw new CodexSecurityError(message);
+ options.onReconnect?.(message, reconnect);
+ }
+ },
+ });
}
function trustedAccessStatusFromEvent(
@@ -4947,6 +4920,7 @@ function selectedWorkerRuntimeConfig(
return {
...Object.fromEntries(
[
+ ...CODEX_AUTH_CONFIG_KEYS,
"analytics",
"responses_api_metadata",
"openai_base_url",
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index f21ff46b1..75f1a1820 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -1876,6 +1876,14 @@ describe("CodexSecurity orchestration", () => {
if (typeof instructionsFile === "string") {
await writeFile(join(scanDir, instructionsFile), instructions);
}
+ const authentication: Record<string, string> =
+ index === 0
+ ? {}
+ : {
+ cli_auth_credentials_store: index % 2 ? "file" : "ephemeral",
+ forced_login_method: index % 2 ? "api" : "chatgpt",
+ forced_chatgpt_workspace_id: `synthetic-workspace-${index}`,
+ };
return new TestClient(
{
pluginPath: PLUGIN_ROOT,
@@ -1883,6 +1891,7 @@ describe("CodexSecurity orchestration", () => {
model: "openai.gpt-5.6-luna",
model_provider: "amazon-bedrock",
...overrides,
+ ...authentication,
},
},
{
@@ -1945,6 +1954,16 @@ describe("CodexSecurity orchestration", () => {
windows,
);
expect(options.config?.["windows"]).toEqual(windows);
+ for (const key of [
+ "cli_auth_credentials_store",
+ "forced_login_method",
+ "forced_chatgpt_workspace_id",
+ ] as const) {
+ const expected = (authentication[key] ??
+ DEFAULT_CODEX_CONFIG[key]) as string | undefined;
+ expect(workerConfig[key]).toEqual(expected);
+ expect(options.config?.[key]).toEqual(expected);
+ }
for (const key of [
"model_context_window",
"model_auto_compact_token_limit",
@@ -7476,6 +7495,171 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
expect(scanSignal?.aborted).toBe(false);
});
+ test.each(["standard", "deep"] as const)(
+ "isolates concurrent managed %s sessions at the Codex child boundary",
+ async (mode) => {
+ const clients: TestClient[] = [];
+ try {
+ const outcomes = await Promise.allSettled(
+ ["first", "second"].map(async (name) => {
+ const root = await temporaryDirectory();
+ const repository = join(root, "repository");
+ const codexHome = join(root, "codex-home");
+ const scanDir = join(root, "scan");
+ const preload = join(root, "fake-codex.mjs");
+ const marker = join(root, "invocation.jsonl");
+ await Promise.all([
+ mkdir(repository),
+ mkdir(codexHome),
+ mkdir(scanDir, { mode: 0o700 }),
+ ]);
+ await writeFile(
+ preload,
+ [
+ 'import { appendFileSync } from "node:fs";',
+ 'let prompt = ""; for await (const chunk of process.stdin) prompt += chunk;',
+ `appendFileSync(${JSON.stringify(marker)}, JSON.stringify({args:process.argv, executable:process.execPath, home:process.env.CODEX_HOME, key:process.env.CODEX_API_KEY, value:process.env.FIXTURE_SCAN_VALUE, prompt}) + "\\n");`,
+ `console.log(JSON.stringify({type:"thread.started",thread_id:${JSON.stringify(`fixture-${name}-thread`)}}));`,
+ 'console.log(JSON.stringify({type:"item.completed",item:{type:"agent_message",text:"scan complete"}}));',
+ 'console.log(JSON.stringify({type:"turn.completed",usage:null}));',
+ "process.exit(0);",
+ ].join("\n"),
+ );
+ const fake = nodeCodex(preload);
+ const model = `fixture-${name}-model`;
+ const provider = `fixture-${name}-provider`;
+ const client = new TestClient(
+ {
+ codexOverrides: {
+ model,
+ model_provider: provider,
+ model_reasoning_effort: "ultra",
+ model_reasoning_summary:
+ name === "first" ? "none" : "concise",
+ features: {
+ multi_agent_v2: { max_concurrent_threads_per_session: 4 },
+ },
+ },
+ },
+ {
+ environment: {
+ OPENAI_API_KEY: `synthetic-${name}-key`,
+ CODEX_CLI_PATH: fake.command.command,
+ },
+ prepareRuntime: async () => ({
+ ...preparedRuntime(codexHome),
+ configPath: join(codexHome, "preflight.toml"),
+ deepScanConfigPath: join(codexHome, "deep.toml"),
+ environment: {
+ ...fake.environment,
+ FIXTURE_SCAN_VALUE: name,
+ },
+ }),
+ resolvePluginPython: async () => "/managed/python",
+ prepareOutputDir: async () => scanDir,
+ repositoryRevision: async () => "deadbeef",
+ createCodex: (options: CodexOptions) => {
+ const codex = new Codex(options);
+ return {
+ startThread: (threadOptions: ThreadOptions) => {
+ const thread = codex.startThread(threadOptions);
+ return {
+ get id() {
+ return thread.id;
+ },
+ runStreamed: async (
+ ...args: Parameters<typeof thread.runStreamed>
+ ) => {
+ if (thread.id === null) {
+ await copyCompletedScan(root);
+ if (mode === "deep") {
+ const coveragePath = join(
+ scanDir,
+ "coverage.json",
+ );
+ const coverage = JSON.parse(
+ await readFile(coveragePath, "utf8"),
+ );
+ coverage.mode = "deep_repository";
+ const coverageBytes = JSON.stringify(coverage);
+ await writeFile(coveragePath, coverageBytes);
+ const manifestPath = join(
+ scanDir,
+ "scan-manifest.json",
+ );
+ const manifest = JSON.parse(
+ await readFile(manifestPath, "utf8"),
+ );
+ manifest.scan.artifacts.find(
+ (artifact: { path: string }) =>
+ artifact.path === "coverage.json",
+ ).sha256 = hash("sha256", coverageBytes);
+ await writeFile(
+ manifestPath,
+ JSON.stringify(manifest),
+ );
+ }
+ }
+ return thread.runStreamed(...args);
+ },
+ };
+ },
+ };
+ },
+ },
+ );
+ clients.push(client);
+ const postScanPrompt = "Summarize the completed synthetic scan.";
+ const result = await client.run(repository, {
+ mode,
+ postScanPrompt,
+ });
+ expect(result.threadId).toBe(`fixture-${name}-thread`);
+ expect(result.turnResult.usage).toBeNull();
+ const children = (await readFile(marker, "utf8"))
+ .trim()
+ .split("\n")
+ .map((line) => JSON.parse(line));
+ expect(children).toHaveLength(2);
+ expect(children[1].prompt).toBe(postScanPrompt);
+ expect(children[1].args).toContain("resume");
+ expect(children[1].args).toContain(`fixture-${name}-thread`);
+ for (const child of children) {
+ expect(child.executable).toBe(
+ process.platform === "win32"
+ ? win32.toNamespacedPath(fake.command.command)
+ : fake.command.command,
+ );
+ expect(child.home).toBe(codexHome);
+ expect(child.key).toBe(`synthetic-${name}-key`);
+ expect(child.value).toBe(name);
+ expect(child.args).toContain(`model=${JSON.stringify(model)}`);
+ expect(child.args).toContain(
+ `model_provider=${JSON.stringify(provider)}`,
+ );
+ expect(child.args).toContain('model_reasoning_effort="ultra"');
+ expect(child.args).toContain(
+ `model_reasoning_summary=${JSON.stringify(name === "first" ? "none" : "concise")}`,
+ );
+ expect(child.args).toContain(
+ "features.multi_agent_v2.max_concurrent_threads_per_session=4",
+ );
+ expect(child.args).toContain(
+ 'default_permissions="codex_security_scan"',
+ );
+ expect(child.args).toContain('approval_policy="on-request"');
+ }
+ }),
+ );
+ for (const outcome of outcomes) {
+ if (outcome.status === "rejected") throw outcome.reason;
+ }
+ } finally {
+ await Promise.all(clients.map((client) => client.close()));
+ }
+ },
+ );
+
test("closes a real Codex subprocess cleanly after a streamed terminal failure", async () => {
const { root, repository, codexHome, scanDir } = await scanDirectories();
const preload = join(root, "fake-codex.mjs");
diff --git a/sdk/typescript/tests-ts/build-plugin.test.ts b/sdk/typescript/tests-ts/build-plugin.test.ts
index 79044d16c..ba23197b1 100644
--- a/sdk/typescript/tests-ts/build-plugin.test.ts
+++ b/sdk/typescript/tests-ts/build-plugin.test.ts
@@ -104,6 +104,8 @@ describe("bundled plugin build", () => {
"reserved_artifact_paths.json",
"codex_profile.mjs",
"codex_profile.d.mts",
+ "codex_session.mjs",
+ "codex_session.d.mts",
]) {
await writeFixture(
source,
diff --git a/sdk/typescript/tests-ts/deep-scan-parent-denials.test.ts b/sdk/typescript/tests-ts/deep-scan-parent-denials.test.ts
index 26840ce85..65987eb77 100644
--- a/sdk/typescript/tests-ts/deep-scan-parent-denials.test.ts
+++ b/sdk/typescript/tests-ts/deep-scan-parent-denials.test.ts
@@ -6,7 +6,11 @@ import { parse } from "smol-toml";
import { loadBundledRuntime } from "./plugin-root.js";
import { profileConfigOverrides } from "../../../plugins/codex-security/scripts/codex_profile.mjs";
-type Sandbox = { filesystemDenies: string[]; globScanMaxDepth?: number };
+type Sandbox = {
+ filesystemDenies: string[];
+ literalFilesystemDenies?: string[];
+ globScanMaxDepth?: number;
+};
async function bundledPolicy() {
const runtime = await loadBundledRuntime();
@@ -128,3 +132,23 @@ test("rejects parent denials that cannot be preserved", async () => {
}
expect(() => policy.resolve({})).toThrow("trusted parent sandbox metadata");
});
+
+test("preserves bracket paths as literal denials at the bundled worker boundary", async () => {
+ const policy = await bundledPolicy();
+ const denied = path.resolve("synthetic", "temp[1]", "credential-home");
+ const sandbox = policy.resolve(
+ metadata([{ access: "deny", path: { type: "path", path: denied } }]),
+ );
+ const config = parse(policy.overrides(sandbox).join("\n"));
+ expect(config["permissions"]).toEqual({
+ codex_security_deep_scan_worker: {
+ extends: ":read-only",
+ filesystem: {
+ ":root": "read",
+ [denied]: { ".": "deny" },
+ glob_scan_max_depth: 8,
+ },
+ network: { enabled: false },
+ },
+ });
+});
diff --git a/sdk/typescript/tests-ts/deep-scan-worker-shutdown.test.ts b/sdk/typescript/tests-ts/deep-scan-worker-shutdown.test.ts
index 2ff4520ae..f01acc52b 100644
--- a/sdk/typescript/tests-ts/deep-scan-worker-shutdown.test.ts
+++ b/sdk/typescript/tests-ts/deep-scan-worker-shutdown.test.ts
@@ -1,238 +1,21 @@
-import { expect, test } from "bun:test";
-import { loadBundledRuntime } from "./plugin-root.js";
-import { profileConfigOverrides } from "../../../plugins/codex-security/scripts/codex_profile.mjs";
-
-type WorkerEvent =
- | { type: "thread.started"; thread_id: string }
- | { type: "item.completed"; item: { type: "agent_message"; text: string } }
- | { type: "turn.completed" }
- | { type: "turn.failed"; error: { message: string } };
-
-type WorkerExecutorConstructor = new (settings: {
- parentSandbox: { filesystemDenies: string[] };
-}) => {
- run(request: {
- kind: "discovery";
- promptPath: string;
- workingDirectory: string;
- subagents: number;
- signal: AbortSignal;
- onThreadStarted?: () => void;
- }): Promise<{ threadId?: string }>;
-};
-
-async function bundledWorkerExecutor(
- events: (signal: AbortSignal) => AsyncGenerator<WorkerEvent>,
- preflight = async () => ({ useOpenAiApiKey: false }),
-): Promise<WorkerExecutorConstructor> {
- const runtime = await loadBundledRuntime();
- const source = /var CodexSdkWorkerExecutor = class \{[\s\S]*?\n\};/u.exec(
- runtime,
- )?.[0];
- if (source === undefined) {
- throw new Error("Bundled Deep Scan worker executor was not found.");
- }
- const fileSystemImport = /\b(import_node_fs\d*)\.promises\.readFile\(/u.exec(
- source,
- )?.[1];
- expect(fileSystemImport).toBeDefined();
-
- class FakeCodex {
- startThread(options: { threadSource: string }) {
- expect(options.threadSource).toBe("security_scan");
- return {
- id: "fixture-worker-thread",
- async runStreamed(_input: string, options: { signal: AbortSignal }) {
- return { events: events(options.signal) };
- },
- };
- }
- }
-
- return new Function(
- "Codex",
- fileSystemImport!,
- "workerPermissionProfile",
- "profileConfigOverrides",
- "DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID",
- "snapshotWorkerEnvironment",
- "workerRuntimeSettings",
- "environmentVariable",
- "preflightDeepScanWorkerPermissionProfile",
- "deepScanPermissionProfileFallbackError",
- "resolveCodexPath",
- "executablePathForSpawn",
- "workerSubagentConfig",
- "appendItemDiagnostic",
- "classifyCodexWorkerError",
- `${source}\nreturn CodexSdkWorkerExecutor;`,
- )(
- FakeCodex,
- { promises: { readFile: async () => "fixture worker prompt" } },
- () => ({}),
- profileConfigOverrides,
- "codex_security_deep_scan_worker",
- async () => ({}),
- async () => ({ config: {} }),
- () => undefined,
- preflight,
- () => undefined,
- () => "/fixture/codex",
- (path: string) => path,
- () => ({}),
- () => {},
- (error: unknown) => error,
- ) as WorkerExecutorConstructor;
-}
-
-function runWorker(
- WorkerExecutor: WorkerExecutorConstructor,
- signal: AbortSignal,
- onThreadStarted?: () => void,
-) {
- return new WorkerExecutor({
- parentSandbox: { filesystemDenies: [] },
- }).run({
- kind: "discovery",
- promptPath: "/fixture/prompt.md",
- workingDirectory: "/fixture/artifacts",
- subagents: 0,
- signal,
- ...(onThreadStarted ? { onThreadStarted } : {}),
- });
-}
-
-test("does not start a bundled worker when its permission profile check fails", async () => {
- let started = false;
- const WorkerExecutor = await bundledWorkerExecutor(
- async function* () {
- started = true;
- yield { type: "turn.completed" };
- },
- async () => {
- throw new Error("worker permission profile rejected");
- },
+import { execFile } from "node:child_process";
+import { fileURLToPath } from "node:url";
+import { promisify } from "node:util";
+import { test } from "bun:test";
+
+test("Deep worker cancellation at the child-process boundary", async () => {
+ // Keep the portable child fixture in the SDK's macOS and Windows test shards.
+ await promisify(execFile)(
+ "node",
+ [
+ "--experimental-strip-types",
+ fileURLToPath(
+ new URL(
+ "../../../plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts",
+ import.meta.url,
+ ),
+ ),
+ ],
+ { timeout: 110_000 },
);
- await expect(
- runWorker(WorkerExecutor, new AbortController().signal),
- ).rejects.toThrow("worker permission profile rejected");
- expect(started).toBe(false);
-});
-
-test("settles completed bundled Deep Scan workers during coordinator cancellation", async () => {
- const parentController = new AbortController();
- let workerSignal: AbortSignal | undefined;
- let iteratorClosed = false;
- const WorkerExecutor = await bundledWorkerExecutor(async function* (
- signal: AbortSignal,
- ) {
- workerSignal = signal;
- try {
- yield { type: "thread.started", thread_id: "fixture-worker-thread" };
- yield {
- type: "item.completed",
- item: { type: "agent_message", text: "worker completed" },
- };
- yield { type: "turn.completed" };
- await new Promise<void>(() => {});
- } finally {
- iteratorClosed = true;
- parentController.abort(
- "coordinator canceled its remaining workers during cleanup",
- );
- }
- });
- const timeout = setTimeout(() => {
- parentController.abort("completed bundled worker remained pending");
- }, 1_000);
-
- try {
- const result = await runWorker(WorkerExecutor, parentController.signal);
-
- expect(result).toEqual({
- threadId: "fixture-worker-thread",
- });
- expect(iteratorClosed).toBe(true);
- expect(parentController.signal.aborted).toBe(true);
- expect(workerSignal).not.toBe(parentController.signal);
- expect(workerSignal?.aborted).toBe(false);
- } finally {
- clearTimeout(timeout);
- }
-});
-
-test("forwards coordinator cancellation to active bundled Deep Scan workers", async () => {
- const parentController = new AbortController();
- const cancellation = new Error("coordinator canceled an active worker");
- let workerSignal: AbortSignal | undefined;
- let iteratorClosed = false;
- const WorkerExecutor = await bundledWorkerExecutor(async function* (
- signal: AbortSignal,
- ) {
- workerSignal = signal;
- try {
- yield { type: "thread.started", thread_id: "fixture-worker-thread" };
- signal.throwIfAborted();
- yield { type: "turn.completed" };
- } finally {
- iteratorClosed = true;
- }
- });
-
- await expect(
- runWorker(WorkerExecutor, parentController.signal, () => {
- parentController.abort(cancellation);
- }),
- ).rejects.toThrow(cancellation.message);
- expect(iteratorClosed).toBe(true);
- expect(workerSignal).not.toBe(parentController.signal);
- expect(workerSignal?.aborted).toBe(true);
- expect(workerSignal?.reason).toBe(cancellation);
-});
-
-test("preserves cancellation when a bundled Deep Scan worker starts aborted", async () => {
- const cancellation = new Error("coordinator canceled before worker startup");
- const parentController = new AbortController();
- parentController.abort(cancellation);
- let workerSignal: AbortSignal | undefined;
- const WorkerExecutor = await bundledWorkerExecutor(async function* (
- signal: AbortSignal,
- ) {
- workerSignal = signal;
- signal.throwIfAborted();
- yield { type: "turn.completed" };
- });
-
- await expect(
- runWorker(WorkerExecutor, parentController.signal),
- ).rejects.toThrow(cancellation.message);
- expect(workerSignal).not.toBe(parentController.signal);
- expect(workerSignal?.aborted).toBe(true);
- expect(workerSignal?.reason).toBe(cancellation);
-});
-
-test("detaches bundled Deep Scan worker cancellation after terminal failure", async () => {
- const parentController = new AbortController();
- let workerSignal: AbortSignal | undefined;
- let iteratorClosed = false;
- const WorkerExecutor = await bundledWorkerExecutor(async function* (
- signal: AbortSignal,
- ) {
- workerSignal = signal;
- try {
- yield {
- type: "turn.failed",
- error: { message: "fixture worker failed" },
- };
- } finally {
- iteratorClosed = true;
- }
- });
-
- await expect(
- runWorker(WorkerExecutor, parentController.signal),
- ).rejects.toThrow("fixture worker failed");
- expect(iteratorClosed).toBe(true);
- parentController.abort("coordinator canceled after terminal failure");
- expect(workerSignal?.aborted).toBe(false);
-});
+}, 120_000);