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

fix(deep-scan): finish saving cancellation before returning to callers (#1260)

Michael D'Angelo7905d70a6b51
diff --git a/plugins/codex-security/mcp-app/server.ts b/plugins/codex-security/mcp-app/server.ts
index 62c86f750..ac3f080cb 100644
--- a/plugins/codex-security/mcp-app/server.ts
+++ b/plugins/codex-security/mcp-app/server.ts
@@ -1064,6 +1064,23 @@ export function createCodexSecurityServer(): McpServer {
async () => {
workspace = await runWorkbench(args);
},
+ async () => {
+ const current = (await runWorkbench(["get-scan", "--scan-id", scanId]))[
+ "workspace"
+ ] as JsonObject;
+ const scan = current["results"];
+ const progress = isJsonObject(scan) ? scan["progress"] : undefined;
+ return {
+ status:
+ isJsonObject(progress) && typeof progress["status"] === "string"
+ ? progress["status"]
+ : undefined,
+ failureMessage:
+ isJsonObject(scan) && typeof scan["failureMessage"] === "string"
+ ? scan["failureMessage"]
+ : undefined,
+ };
+ },
);
if (workspace === undefined && canceledLocally) {
workspace = (await runWorkbench(["get-scan", "--scan-id", scanId]))[
@@ -1547,7 +1564,10 @@ export function createCodexSecurityServer(): McpServer {
`--message=${message}`,
...optionalArg("--claim-token", handoffClaimToken),
]);
- deepScanCoordinators.get(scanId)?.failExternallyPersisted(message);
+ const scan = failed["scan"];
+ const progress = isJsonObject(scan) ? scan["progress"] : undefined;
+ if (isJsonObject(progress) && progress["status"] === "failed")
+ deepScanCoordinators.get(scanId)?.failExternallyPersisted(message);
return scanActionResult(
failed,
"Recorded the Codex Security scan failure.",
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts b/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
index 09586aeda..5f66e30aa 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
@@ -61,6 +61,11 @@ interface SchedulerResult {
resultPath?: string;
}
+export interface ParentScanState {
+ status: string | undefined;
+ failureMessage?: string;
+}
+
export interface CoordinatorOptions {
run: DeepScanRunState;
store: DeepScanStore;
@@ -114,6 +119,7 @@ export class DeepScanCoordinator {
private terminal = false;
private canceled = false;
private externallyFailed = false;
+ private failurePersisted = false;
private setupComplete = false;
private discoveryDeadlineReached = false;
private state: DeepScanRunState;
@@ -241,27 +247,95 @@ export class DeepScanCoordinator {
async cancelAfterPersistence(
reason: string,
persistCancellation: () => Promise<void>,
+ readParentState: () => Promise<ParentScanState>,
): Promise<DeepScanRunState> {
- if (this.terminal || this.state.status !== "running")
+ const existing = this.cancellationPersistence;
+ if (existing) {
+ try {
+ await existing.promise;
+ } catch (error) {
+ if (existing.failure) await this.settled();
+ throw error;
+ }
+ return await this.settled();
+ }
+ if (
+ this.terminal ||
+ this.canceled ||
+ this.failurePersisted ||
+ this.state.status === "canceled" ||
+ this.state.status === "interrupted"
+ )
return await this.settled();
- this.cancellationPersistence ??= Promise.withResolvers<void>();
- this.cancel(reason);
- await this.cancellationReady.promise;
+ let resolve!: () => void;
+ let reject!: (error: unknown) => void;
+ const promise = new Promise<void>((resolvePromise, rejectPromise) => {
+ resolve = resolvePromise;
+ reject = rejectPromise;
+ });
+ // Cancellation callers share operation errors; terminal waiters inspect only
+ // persistence failures below. Handle a rejection even with no joined caller.
+ void promise.catch(() => {});
+ const persistence: NonNullable<typeof this.cancellationPersistence> = {
+ promise,
+ resolve,
+ };
+ this.cancellationPersistence = persistence;
try {
- await persistCancellation();
+ const parent = await readParentState();
+ const parentStatus = parent.status;
+ if (parentStatus === "failed")
+ this.failExternallyPersisted(parent.failureMessage);
+ if (parentStatus === "canceled") this.cancel(reason);
+ if (
+ parentStatus === "running" &&
+ !this.failurePersisted &&
+ !["canceled", "interrupted"].includes(this.state.status)
+ ) {
+ // Terminal discovery has no active writers to stop. Keep its outcome
+ // until the parent actually accepts cancellation.
+ if (this.state.status === "running") this.cancel(reason);
+ await this.cancellationReady.promise;
+ if (!this.failurePersisted) {
+ try {
+ await persistCancellation();
+ this.cancel(reason);
+ } catch (error) {
+ let parentStopped = false;
+ if (this.state.status === "succeeded") {
+ try {
+ parentStopped = ["complete", "failed"].includes(
+ (await readParentState()).status ?? "",
+ );
+ } catch {
+ // Preserve the cancellation diagnostic if reconciliation fails.
+ }
+ }
+ if (!parentStopped) persistence.failure = { error };
+ throw error;
+ }
+ }
+ }
} catch (error) {
- this.cancellationPersistence.failure = { error };
+ reject(error);
+ if (!persistence.failure) {
+ this.cancellationPersistence = undefined;
+ throw error;
+ }
} finally {
// Cleanup still inspects durable state and preserves results when the
// process lost a committed response, then reports the persistence failure.
- this.cancellationPersistence.resolve();
+ persistence.resolve();
}
- return await this.settled();
+ const settled = await this.settled();
+ if (persistence.failure) throw persistence.failure.error;
+ return settled;
}
- failExternallyPersisted(reason: string): void {
+ failExternallyPersisted(reason: string | undefined): void {
if (this.externallyFailed || this.terminal) return;
this.externallyFailed = true;
+ this.failurePersisted = true;
this.state = { ...this.state, status: "failed", error: reason };
this.log({
event: "coordinator_external_failure",
@@ -369,6 +443,7 @@ export class DeepScanCoordinator {
);
if (this.canceled || this.externallyFailed) return;
this.state = failed;
+ this.failurePersisted ||= this.state.status === "failed";
} catch (persistError) {
if (this.canceled || this.externallyFailed) return;
if (
@@ -401,20 +476,29 @@ export class DeepScanCoordinator {
await this.settleSchedulerWork();
await this.ownershipCheck;
this.cancellationReady.resolve();
- await this.cancellationPersistence?.promise;
if (this.options.onStopped && this.options.threadId) {
let current: DeepScanRunState | undefined;
- try {
- current = await this.options.store.get(
- this.state.scanId,
- this.options.threadId,
- );
- } catch (error) {
- this.log({
- event: "coordinator_terminal_state_read_failed",
- scanId: this.state.scanId,
- reason: errorKind(error),
- });
+ let persistence: Promise<void> | undefined;
+ do {
+ persistence = this.cancellationPersistence?.promise;
+ await persistence?.catch(() => {});
+ current = undefined;
+ try {
+ current = await this.options.store.get(
+ this.state.scanId,
+ this.options.threadId,
+ );
+ } catch (error) {
+ this.log({
+ event: "coordinator_terminal_state_read_failed",
+ scanId: this.state.scanId,
+ reason: errorKind(error),
+ });
+ }
+ } while (persistence !== this.cancellationPersistence?.promise);
+ if (current?.status === "failed" || current?.status === "canceled") {
+ this.state = current;
+ this.failurePersisted = current.status === "failed";
}
if (
current &&
@@ -470,10 +554,13 @@ export class DeepScanCoordinator {
scanId: this.state.scanId,
});
}
- if (this.cancellationPersistence?.failure) {
+ if (this.cancellationPersistence)
+ await this.cancellationPersistence.promise.catch(() => {});
+ if (this.cancellationPersistence?.failure && !this.failurePersisted) {
throw this.cancellationPersistence.failure.error;
}
- if (this.canceled) this.state = { ...this.state, status: "canceled" };
+ if (this.canceled && !this.failurePersisted)
+ this.state = { ...this.state, status: "canceled" };
if (this.stopLocally())
this.terminalResult.resolve(cloneState(this.state));
}
@@ -580,6 +667,7 @@ export class DeepScanCoordinator {
let current = confirmedOwnershipChange(error, this.state.scanId)?.run;
try {
current ??= await this.options.store.get(this.state.scanId, threadId);
+ this.failurePersisted ||= current.status === "failed";
} catch (readError) {
this.log({
event: "coordinator_ownership_read_failed",
@@ -608,6 +696,7 @@ export class DeepScanCoordinator {
current,
this.observationAbortController.signal,
);
+ this.failurePersisted ||= this.state.status === "failed";
} catch (observeError) {
this.log({
event: "coordinator_replacement_observation_failed",
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/registry.ts b/plugins/codex-security/mcp-app/src/deep-scan/registry.ts
index 3b0d269d8..ae96daf05 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/registry.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/registry.ts
@@ -1,6 +1,6 @@
import { setTimeout as delay } from "node:timers/promises";
import { DeepScanCoordinator } from "./coordinator.js";
-import type { CoordinatorOptions } from "./coordinator.js";
+import type { CoordinatorOptions, ParentScanState } from "./coordinator.js";
import { isTransientPersistenceError } from "./store.js";
import type { DeepScanCoordinatorClaim, DeepScanRunState } from "./types.js";
@@ -58,10 +58,15 @@ export class DeepScanCoordinatorRegistry {
scanId: string,
reason: string,
persistCancellation: () => Promise<void>,
+ readParentState: () => Promise<ParentScanState>,
): Promise<boolean> {
const coordinator = this.coordinators.get(scanId);
if (!coordinator) return false;
- await coordinator.cancelAfterPersistence(reason, persistCancellation);
+ await coordinator.cancelAfterPersistence(
+ reason,
+ persistCancellation,
+ readParentState,
+ );
return true;
}
diff --git a/plugins/codex-security/mcp-app/src/workbench-client.ts b/plugins/codex-security/mcp-app/src/workbench-client.ts
index 3d1332bbc..7da2a8073 100644
--- a/plugins/codex-security/mcp-app/src/workbench-client.ts
+++ b/plugins/codex-security/mcp-app/src/workbench-client.ts
@@ -98,8 +98,7 @@ async function executeWorkbench(
windowsHide: true,
env: process.env,
encoding: "utf8" as const,
- // Artifact bytes are base64-encoded here; retain the existing file-size behavior.
- maxBuffer: args[0] === "read-artifact" ? Infinity : 4 * 1024 * 1024,
+ maxBuffer: Infinity,
timeout,
},
);
diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_lifecycle_cases.ts b/plugins/codex-security/mcp-app/tests/deep_scan_lifecycle_cases.ts
index 7f3371119..4ca6224b0 100644
--- a/plugins/codex-security/mcp-app/tests/deep_scan_lifecycle_cases.ts
+++ b/plugins/codex-security/mcp-app/tests/deep_scan_lifecycle_cases.ts
@@ -1,7 +1,10 @@
import assert from "node:assert/strict";
+import { execFileSync } from "node:child_process";
import { randomUUID } from "node:crypto";
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
import path from "node:path";
+import { fileURLToPath } from "node:url";
+import { importModule } from "./import-module.ts";
import type { DeepScanRunState } from "../src/deep-scan/types.js";
type LifecycleFixtures = Pick<
@@ -30,6 +33,8 @@ export async function testDeepScanLifecycle({
};
const errors = [];
for (const test of [
+ lateCancellationWaitsForPersistence,
+ failedParentCancellationStopsActiveWorker,
canceledPublicationWaitsForHeartbeat,
delayedReducerDoesNotReplaceStoppedState,
replacementWaitsForTerminalResult,
@@ -51,6 +56,734 @@ export async function testDeepScanLifecycle({
if (errors.length)
throw new AggregateError(errors, "Deep Scan lifecycle regressions");
+ async function failedParentCancellationStopsActiveWorker() {
+ for (const terminalReadFails of [false, true]) {
+ const fixture = await fixtureRun(config);
+ fixture.run.coordinatorGeneration = 1;
+ const store = new FakeStore(fixture.run);
+ const executor = new FakeExecutor({ blockDiscoveryAfterCalls: 0 });
+ const registry = new DeepScanCoordinatorRegistry();
+ const savedError = "synthetic externally persisted failure";
+ let publications = 0;
+ let cancellationWrites = 0;
+ const coordinator = registry.start({
+ ...fixture,
+ store,
+ executor,
+ threadId: "fixture-owner",
+ heartbeatIntervalMs: 60_000,
+ onStopped: async (stopped: DeepScanRunState) => {
+ publications += 1;
+ assert.equal(stopped.status, "failed");
+ assert.equal(stopped.error, savedError);
+ },
+ });
+ const terminal = coordinator.settled();
+ void terminal.catch(() => {});
+ let cancellation: Promise<boolean> | undefined;
+ try {
+ await executor.discoveryStarted.promise;
+ assert.equal(executor.runningDiscovery, 1);
+ store.run.status = "failed";
+ store.run.error = savedError;
+ store.failNextTerminalGet = terminalReadFails;
+ cancellation = registry.cancelAndWait(
+ fixture.run.scanId,
+ "user_canceled_scan",
+ async () => {
+ cancellationWrites += 1;
+ throw new Error(
+ "saved failure must not be replaced by cancellation",
+ );
+ },
+ async () => ({ status: "failed", failureMessage: savedError }),
+ );
+ void cancellation!.catch(() => {});
+ await new Promise(setImmediate);
+ assert.equal(
+ executor.runningDiscovery,
+ 0,
+ "saved parent failure stops the worker before the next heartbeat",
+ );
+ assert.equal(await cancellation, true);
+ const stopped = await terminal;
+ assert.equal(stopped.status, "failed");
+ assert.equal(stopped.error, savedError);
+ assert.equal(store.run.status, "failed");
+ assert.equal(store.run.error, savedError);
+ assert.equal(cancellationWrites, 0);
+ assert.equal(publications, terminalReadFails ? 0 : 1);
+ } finally {
+ registry.shutdown("fixture cleanup");
+ await Promise.allSettled([terminal, cancellation]);
+ }
+ }
+ }
+
+ async function lateCancellationWaitsForPersistence() {
+ const completedParentStatus: string = JSON.parse(
+ execFileSync(
+ process.env.PYTHON || "python3",
+ [
+ "-I",
+ "-B",
+ "-c",
+ `import json, sys, tempfile
+from pathlib import Path
+sys.path.insert(0, sys.argv[1])
+from workbench_test_support import empty_target_scan, run_workbench, write_completed_contract
+with tempfile.TemporaryDirectory(prefix="deep-scan-completion-contract-") as root:
+ state, target, scan_id, output = empty_target_scan(Path(root))
+ write_completed_contract(output, scan_id, target)
+ run_workbench(state, "complete-scan", "--scan-id", scan_id)
+ saved = run_workbench(state, "get-scan", "--scan-id", scan_id)
+ print(json.dumps(saved["workspace"]["results"]["progress"]["status"]))
+`,
+ fileURLToPath(new URL("../../tests/", import.meta.url)),
+ ],
+ { encoding: "utf8" },
+ ),
+ );
+ const applicationRoot = fileURLToPath(new URL("../", import.meta.url));
+ const source = await readFile(
+ new URL("../server.ts", import.meta.url),
+ "utf8",
+ );
+ let current: {
+ registry: InstanceType<LifecycleFixtures["DeepScanCoordinatorRegistry"]>;
+ store: InstanceType<LifecycleFixtures["FakeStore"]>;
+ persistEntered: PromiseWithResolvers<void>;
+ persistRelease: PromiseWithResolvers<void>;
+ rejectPersistence: boolean;
+ calls: string[];
+ readParent?: () => Promise<{ workspace: typeof current.workspace }>;
+ workspace: {
+ setup: { submitted: boolean };
+ results: { progress: { status: string }; failureMessage?: string };
+ };
+ };
+ Object.assign(globalThis, {
+ deepScanCancellationFixture: {
+ registry: {
+ get: (...args: unknown[]) => current.registry.get(...args),
+ cancelAndWait: (...args: unknown[]) =>
+ current.registry.cancelAndWait(...args),
+ shutdown: (...args: unknown[]) => current.registry.shutdown(...args),
+ },
+ async workbench([command]: string[]) {
+ current.calls.push(command);
+ if (command === "get-scan")
+ return current.readParent
+ ? await current.readParent()
+ : { workspace: structuredClone(current.workspace) };
+ if (command === "fail-scan") {
+ current.store.run.status = "failed";
+ current.store.run.error = "synthetic external failure";
+ current.workspace.results.progress.status = "failed";
+ return {
+ scan: current.workspace.results,
+ workspace: current.workspace,
+ };
+ }
+ assert.equal(command, "cancel-scan");
+ if (current.workspace.results.progress.status !== "running")
+ throw new Error("Only a running scan can be canceled.");
+ if (
+ current.calls.filter((command) => command === "cancel-scan")
+ .length > 1
+ )
+ throw new Error("synthetic duplicate cancellation persistence");
+ current.persistEntered.resolve();
+ await current.persistRelease.promise;
+ if (current.rejectPersistence)
+ throw new Error("synthetic cancellation persistence failure");
+ current.store.run.status = "canceled";
+ current.workspace.results.progress.status = "canceled";
+ return current.workspace;
+ },
+ },
+ });
+ const { createCodexSecurityServer } = await importModule({
+ stdin: {
+ contents: source
+ .replace(
+ "const deepScanCoordinators = new DeepScanCoordinatorRegistry();",
+ "const deepScanCoordinators = globalThis.deepScanCancellationFixture.registry;",
+ )
+ .replace(
+ 'import { PLUGIN_ROOT, runWorkbench, scanRoot } from "./src/workbench-client.js";',
+ 'import { PLUGIN_ROOT, scanRoot } from "./src/workbench-client.js";\nconst runWorkbench = (...args) => globalThis.deepScanCancellationFixture.workbench(...args);',
+ ),
+ loader: "ts",
+ resolveDir: applicationRoot,
+ },
+ define: {
+ __dirname: JSON.stringify(applicationRoot),
+ "import.meta.url": JSON.stringify(
+ new URL("../server.ts", import.meta.url).href,
+ ),
+ },
+ loader: { ".md": "text" },
+ });
+ const server = createCodexSecurityServer();
+ const cancel =
+ server._registeredTools.cancel_codex_security_scan_from_app.handler;
+ const fail = server._registeredTools.fail_codex_security_scan.handler;
+ const failures = [];
+ try {
+ for (const stage of [
+ "final read",
+ "final read heartbeat",
+ "final read heartbeat microtask",
+ "final read repeated cancellation",
+ "onComplete",
+ "unpersisted failure",
+ "unpersisted unowned failure",
+ "persisted failure",
+ "observed failure",
+ "lost-response failure",
+ "lost-response pending-read failure",
+ "unpersisted pending-read failure",
+ "unavailable pending-read failure",
+ "stale heartbeat failure",
+ "observed interrupted",
+ "unpersisted pending-read external failure",
+ "final read completed parent",
+ "final read parent completes during admission",
+ "onComplete external failure during cleanup",
+ ]) {
+ for (const rejectPersistence of [false, true]) {
+ const fixture = await fixtureRun(config);
+ fixture.run.coordinatorGeneration = 1;
+ const store = new FakeStore(fixture.run);
+ const registry = new DeepScanCoordinatorRegistry();
+ const entered = Promise.withResolvers<void>();
+ const release = Promise.withResolvers<void>();
+ const persistEntered = Promise.withResolvers<void>();
+ const persistRelease = Promise.withResolvers<void>();
+ const failureEntered = Promise.withResolvers<void>();
+ const failureRelease = Promise.withResolvers<void>();
+ const failureSaved = Promise.withResolvers<void>();
+ const admissionEntered = Promise.withResolvers<void>();
+ const admissionRelease = Promise.withResolvers<void>();
+ const heartbeatRead = Promise.withResolvers<void>();
+ const heartbeatRelease = Promise.withResolvers<void>();
+ let publications = 0;
+ let cancellation: ReturnType<typeof cancel> | undefined;
+ let repeatedCancellation: ReturnType<typeof cancel> | undefined;
+ let coordinator:
+ | ReturnType<
+ InstanceType<
+ LifecycleFixtures["DeepScanCoordinatorRegistry"]
+ >["start"]
+ >
+ | undefined;
+ let gateHeartbeat = false;
+ const concurrentFailure = stage.endsWith("external failure");
+ const failureDuringCleanup =
+ stage === "onComplete external failure during cleanup";
+ const completedParent = stage.includes("completed parent");
+ const completesDuringAdmission = stage.includes(
+ "completes during admission",
+ );
+ const pendingFailureRead = stage.includes("pending-read");
+ const unavailableRead = stage.startsWith("unavailable");
+ const lostResponse =
+ stage.startsWith("lost-response") || unavailableRead;
+ const interrupted = stage === "observed interrupted";
+ const stoppedStatus = interrupted ? "interrupted" : "failed";
+ const observeFailure = stage === "observed failure" || interrupted;
+ const publicationGate =
+ stage === "lost-response failure" ||
+ stage === "stale heartbeat failure";
+ const durableFailure =
+ stage === "persisted failure" ||
+ observeFailure ||
+ publicationGate ||
+ lostResponse ||
+ unavailableRead ||
+ concurrentFailure ||
+ failureDuringCleanup;
+ current = {
+ store,
+ registry,
+ persistEntered,
+ persistRelease,
+ rejectPersistence,
+ calls: [],
+ workspace: {
+ setup: { submitted: true },
+ results: {
+ progress: {
+ status: completedParent ? completedParentStatus : "running",
+ },
+ },
+ },
+ };
+ let admissionPaused = false;
+ current.readParent = async () => {
+ const workspace = structuredClone(current.workspace);
+ if (workspace.results.progress.status === "failed")
+ workspace.results.failureMessage = current.store.run.error;
+ if (pendingFailureRead && !admissionPaused) {
+ admissionPaused = true;
+ admissionEntered.resolve();
+ await admissionRelease.promise;
+ if (unavailableRead)
+ throw new Error("synthetic terminal state unavailable");
+ }
+ if (completesDuringAdmission)
+ queueMicrotask(() => {
+ current.workspace.results.progress.status =
+ completedParentStatus;
+ });
+ return { workspace };
+ };
+ if (
+ stage.startsWith("final read") ||
+ observeFailure ||
+ publicationGate ||
+ pendingFailureRead
+ ) {
+ const get = store.get.bind(store);
+ let paused = false;
+ store.get = async () => {
+ const snapshot = await get();
+ if (concurrentFailure && paused && !admissionPaused) {
+ admissionPaused = true;
+ admissionEntered.resolve();
+ await admissionRelease.promise;
+ }
+ if (gateHeartbeat) {
+ gateHeartbeat = false;
+ heartbeatRead.resolve();
+ await heartbeatRelease.promise;
+ }
+ if (
+ !paused &&
+ (observeFailure || pendingFailureRead
+ ? coordinator?.snapshot().status === stoppedStatus
+ : snapshot.status === "succeeded")
+ ) {
+ paused = true;
+ entered.resolve();
+ await release.promise;
+ }
+ return snapshot;
+ };
+ }
+ if (stage.startsWith("unpersisted"))
+ store.fail = async () => {
+ throw new Error("synthetic failure persistence unavailable");
+ };
+ if (publicationGate || lostResponse) {
+ const fail = store.fail.bind(store);
+ store.fail = async (...args) => {
+ if (stage === "stale heartbeat failure") {
+ failureEntered.resolve();
+ await failureRelease.promise;
+ }
+ const result = await fail(...args);
+ current.workspace.results.progress.status = "failed";
+ if (lostResponse)
+ throw new Error("synthetic committed failure response lost");
+ return result;
+ };
+ }
+ const executor = new FakeExecutor(
+ observeFailure
+ ? { blockDiscoveryAfterCalls: 0 }
+ : stage.endsWith("failure")
+ ? {
+ nonRetryableDiscoveryMessage: "synthetic worker failure",
+ }
+ : {},
+ );
+ coordinator = registry.start({
+ run: fixture.run,
+ store,
+ executor,
+ pluginRoot: fixture.pluginRoot,
+ clock: immediateClock,
+ threadId:
+ stage === "unpersisted unowned failure"
+ ? undefined
+ : "fixture-owner",
+ heartbeatIntervalMs: 60_000,
+ onComplete: async () => {
+ if (stage.startsWith("onComplete")) {
+ entered.resolve();
+ await release.promise;
+ }
+ },
+ onStopped: async () => {
+ publications++;
+ if (publicationGate) {
+ entered.resolve();
+ await release.promise;
+ }
+ },
+ log: (event: { event: string }) => {
+ if (
+ failureDuringCleanup &&
+ event.event === "coordinator_cancel_requested"
+ )
+ void fail(
+ {
+ scanId: fixture.run.scanId,
+ message: "synthetic external failure",
+ },
+ {},
+ ).then(() => failureSaved.resolve());
+ if (
+ stage.endsWith("microtask") &&
+ event.event === "coordinator_cleanup_settled"
+ )
+ queueMicrotask(() => {
+ cancellation = cancel({ scanId: fixture.run.scanId });
+ void cancellation.catch(() => {});
+ });
+ if (
+ !stage.endsWith("failure") ||
+ event.event !== "coordinator_failed"
+ )
+ return;
+ if (publicationGate || pendingFailureRead) {
+ failureSaved.resolve();
+ return;
+ }
+ if (stage === "persisted failure")
+ current.workspace.results.progress.status = "failed";
+ entered.resolve();
+ cancellation = cancel({ scanId: fixture.run.scanId });
+ void cancellation.catch(() => {});
+ },
+ });
+ const terminal = coordinator.settled();
+ void terminal.catch(() => {});
+ try {
+ if (stage === "stale heartbeat failure") {
+ await failureEntered.promise;
+ gateHeartbeat = true;
+ const heartbeat = coordinator.renewHeartbeat();
+ await heartbeatRead.promise;
+ failureRelease.resolve();
+ await failureSaved.promise;
+ heartbeatRelease.resolve();
+ await heartbeat;
+ }
+ if (observeFailure) {
+ await executor.discoveryStarted.promise;
+ store.run.status = stoppedStatus;
+ current.workspace.results.progress.status = "failed";
+ await coordinator.renewHeartbeat();
+ }
+ await entered.promise;
+ if (stage.startsWith("final read heartbeat"))
+ await coordinator.renewHeartbeat();
+ if (!stage.endsWith("microtask")) {
+ cancellation ??= cancel({ scanId: fixture.run.scanId });
+ void cancellation.catch(() => {});
+ }
+ if (stage === "final read repeated cancellation") {
+ await coordinator.renewHeartbeat();
+ repeatedCancellation = cancel({ scanId: fixture.run.scanId });
+ void repeatedCancellation.catch(() => {});
+ }
+ if (pendingFailureRead) {
+ const admitted = await Promise.race([
+ admissionEntered.promise.then(() => true),
+ persistEntered.promise.then(() => false),
+ cancellation.then(
+ () => false,
+ () => false,
+ ),
+ coordinator.wait(undefined, 25).then(() => false),
+ ]);
+ if (admitted) {
+ if (concurrentFailure)
+ await fail(
+ {
+ scanId: fixture.run.scanId,
+ message: "synthetic external failure",
+ },
+ {},
+ );
+ repeatedCancellation = cancel({ scanId: fixture.run.scanId });
+ void repeatedCancellation.catch(() => {});
+ assert.equal(await coordinator.wait(undefined, 25), undefined);
+ admissionRelease.resolve();
+ }
+ }
+ if (failureDuringCleanup) await failureSaved.promise;
+ release.resolve();
+ if (completedParent || completesDuringAdmission) {
+ persistRelease.resolve();
+ assert.equal((await terminal).status, "succeeded");
+ if (completesDuringAdmission)
+ await assert.rejects(
+ cancellation,
+ /Only a running scan can be canceled/,
+ );
+ else
+ assert.equal(
+ (await cancellation).structuredContent.workspace.results
+ .progress.status,
+ completedParentStatus,
+ );
+ assert.equal(store.run.status, "succeeded");
+ assert.equal(
+ current.workspace.results.progress.status,
+ completedParentStatus,
+ );
+ assert.equal(
+ current.calls.filter((command) => command === "cancel-scan")
+ .length,
+ completesDuringAdmission ? 1 : 0,
+ );
+ continue;
+ }
+ if (durableFailure) {
+ assert.equal((await terminal).status, stoppedStatus);
+ if (unavailableRead)
+ await assert.rejects(
+ cancellation,
+ /synthetic terminal state unavailable/,
+ );
+ else
+ assert.equal(
+ (await cancellation).structuredContent.workspace.results
+ .progress.status,
+ "failed",
+ );
+ assert.equal(current.calls.includes("cancel-scan"), false);
+ if (concurrentFailure || failureDuringCleanup)
+ assert.match(
+ (await terminal).error,
+ /synthetic external failure/,
+ );
+ assert.equal(publications, interrupted ? 0 : 1);
+ if (unavailableRead)
+ assert.match(
+ (await terminal).error,
+ /synthetic worker failure/,
+ );
+ if (repeatedCancellation) {
+ if (unavailableRead)
+ await assert.rejects(
+ repeatedCancellation,
+ /synthetic terminal state unavailable/,
+ );
+ else
+ assert.equal(
+ (await repeatedCancellation).structuredContent.workspace
+ .results.progress.status,
+ "failed",
+ );
+ }
+ continue;
+ }
+ await persistEntered.promise;
+ const waiting = await coordinator.wait(undefined, 25);
+ persistRelease.resolve();
+ if (stage.endsWith("microtask")) {
+ assert.equal(
+ waiting?.status,
+ "succeeded",
+ "settlement must finish before admitting another cancellation",
+ );
+ if (rejectPersistence)
+ await assert.rejects(
+ cancellation,
+ /synthetic cancellation persistence failure/,
+ );
+ else
+ assert.equal(
+ (await cancellation).structuredContent.workspace.results
+ .progress.status,
+ "canceled",
+ );
+ assert.equal((await terminal).status, "succeeded");
+ assert.equal(publications, 0);
+ assert.deepEqual(current.calls, ["get-scan", "cancel-scan"]);
+ continue;
+ }
+ assert.equal(
+ waiting,
+ undefined,
+ "start waiter must await durable cancellation",
+ );
+ if (rejectPersistence) {
+ await assert.rejects(
+ cancellation,
+ /synthetic cancellation persistence failure/,
+ );
+ await assert.rejects(
+ terminal,
+ /synthetic cancellation persistence failure/,
+ );
+ if (repeatedCancellation)
+ await assert.rejects(
+ repeatedCancellation,
+ /synthetic cancellation persistence failure/,
+ );
+ assert.equal(publications, 0);
+ } else {
+ assert.equal(
+ (await cancellation).structuredContent.workspace.results
+ .progress.status,
+ "canceled",
+ );
+ assert.equal((await terminal).status, "canceled");
+ assert.equal(store.run.status, "canceled");
+ assert.equal(
+ publications,
+ stage === "unpersisted unowned failure" ? 0 : 1,
+ );
+ if (repeatedCancellation)
+ assert.equal(
+ (await repeatedCancellation).structuredContent.workspace
+ .results.progress.status,
+ "canceled",
+ );
+ }
+ assert.equal(current.calls[0], "get-scan");
+ assert.equal(
+ current.calls.filter((command) => command === "cancel-scan")
+ .length,
+ 1,
+ );
+ } catch (error) {
+ failures.push(
+ new Error(
+ `${stage}; persistence rejects=${rejectPersistence}: ${error instanceof Error ? error.message : String(error)}`,
+ { cause: error },
+ ),
+ );
+ } finally {
+ release.resolve();
+ persistRelease.resolve();
+ failureRelease.resolve();
+ heartbeatRelease.resolve();
+ admissionRelease.resolve();
+ registry.shutdown("fixture cleanup");
+ await Promise.allSettled([
+ terminal,
+ cancellation,
+ repeatedCancellation,
+ ]);
+ }
+ }
+ }
+ for (const joined of [false, true]) {
+ const fixture = await fixtureRun(config);
+ const store = new FakeStore(fixture.run);
+ const registry = new DeepScanCoordinatorRegistry();
+ const executor = new FakeExecutor({ blockDiscoveryAfterCalls: 0 });
+ const readEntered = Promise.withResolvers<void>();
+ const readRelease = Promise.withResolvers<void>();
+ const persistEntered = Promise.withResolvers<void>();
+ const persistRelease = Promise.withResolvers<void>();
+ let reads = 0;
+ current = {
+ store,
+ registry,
+ persistEntered,
+ persistRelease,
+ rejectPersistence: false,
+ calls: [],
+ workspace: {
+ setup: { submitted: true },
+ results: { progress: { status: "running" } },
+ },
+ readParent: async () => {
+ if (reads++ === 0) {
+ readEntered.resolve();
+ await readRelease.promise;
+ throw new Error("synthetic temporary parent read failure");
+ }
+ return { workspace: structuredClone(current.workspace) };
+ },
+ };
+ const coordinator = registry.start({
+ run: fixture.run,
+ store,
+ executor,
+ pluginRoot: fixture.pluginRoot,
+ clock: immediateClock,
+ threadId: "fixture-owner",
+ heartbeatIntervalMs: 60_000,
+ });
+ const outcome = (operation: ReturnType<typeof cancel>) =>
+ operation.then(
+ () => undefined,
+ (error: Error) => error,
+ );
+ let first: Promise<Error | undefined> | undefined;
+ let second: Promise<Error | undefined> | undefined;
+ let retry: ReturnType<typeof cancel> | undefined;
+ try {
+ await executor.discoveryStarted.promise;
+ first = outcome(cancel({ scanId: fixture.run.scanId }));
+ await readEntered.promise;
+ if (joined) second = outcome(cancel({ scanId: fixture.run.scanId }));
+ readRelease.resolve();
+ assert.match(
+ (await first)?.message ?? "",
+ /temporary parent read failure/,
+ );
+ if (second) {
+ const result = await Promise.race([
+ second,
+ coordinator.wait(undefined, 25).then(() => undefined),
+ ]);
+ assert.match(
+ result?.message ?? "",
+ /temporary parent read failure/,
+ );
+ }
+ assert.equal(coordinator.snapshot().status, "running");
+ assert.equal(await coordinator.wait(undefined, 25), undefined);
+ retry = cancel({ scanId: fixture.run.scanId });
+ void retry.catch(() => {});
+ await persistEntered.promise;
+ assert.equal(await coordinator.wait(undefined, 25), undefined);
+ persistRelease.resolve();
+ assert.equal(
+ (await retry).structuredContent.workspace.results.progress.status,
+ "canceled",
+ );
+ assert.equal((await coordinator.settled()).status, "canceled");
+ assert.equal(
+ current.calls.filter((command) => command === "cancel-scan").length,
+ 1,
+ );
+ } catch (error) {
+ failures.push(
+ new Error(`Admission read failure; joined=${joined}`, {
+ cause: error,
+ }),
+ );
+ } finally {
+ readRelease.resolve();
+ persistRelease.resolve();
+ registry.shutdown("fixture cleanup");
+ await Promise.allSettled([
+ first,
+ second,
+ retry,
+ coordinator.settled(),
+ ]);
+ }
+ }
+ if (failures.length)
+ throw new AggregateError(failures, "Late cancellation responses");
+ } finally {
+ await server.close();
+ Reflect.deleteProperty(globalThis, "deepScanCancellationFixture");
+ }
+ }
+
async function canceledPublicationWaitsForHeartbeat() {
const fixture = await fixtureRun(config);
fixture.run.coordinatorGeneration = 1;
@@ -357,10 +1090,14 @@ export async function testDeepScanLifecycle({
const terminal = coordinator.settled().catch((error: Error) => error);
await executor.discoveryStarted.promise;
const cancellation = coordinator
- .cancelAfterPersistence("fixture cancellation", async () => {
- store.run.status = "canceled";
- throw new Error("fixture cancellation response lost");
- })
+ .cancelAfterPersistence(
+ "fixture cancellation",
+ async () => {
+ store.run.status = "canceled";
+ throw new Error("fixture cancellation response lost");
+ },
+ async () => ({ status: "running" }),
+ )
.catch((error: Error) => {
cancellationSettled = true;
return error;
@@ -411,6 +1148,7 @@ export async function testDeepScanLifecycle({
async () => {
cancellations += 1;
},
+ async () => ({ status: "running" }),
);
release.resolve();
const result = await cancellation;
@@ -453,9 +1191,15 @@ export async function testDeepScanLifecycle({
let persisted = false;
let resolved = false;
const cancellation = registry
- .cancelAndWait(fixture.run.scanId, "cancel parent", async () => {
- persisted = true;
- })
+ .cancelAndWait(
+ fixture.run.scanId,
+ "cancel parent",
+ async () => {
+ persisted = true;
+ store.run.status = "canceled";
+ },
+ async () => ({ status: "running" }),
+ )
.then((handled: boolean) => {
resolved = true;
return handled;
@@ -470,17 +1214,14 @@ export async function testDeepScanLifecycle({
assert.equal(
await cancellation,
true,
- "local cleanup completed; the server checks durable parent state",
+ "local cleanup and durable cancellation completed",
);
assert.equal(
persisted,
- false,
- "the server performs durable cancellation after coordinator cleanup",
- );
- assert.equal(
- store.run.status,
- status === "failed" ? "running" : "succeeded",
+ true,
+ "persist cancellation before releasing coordinator waiters",
);
+ assert.equal(store.run.status, "canceled");
}
}
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.ts
index adb2bc629..d323a8212 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.ts
@@ -45,6 +45,7 @@ await testLateParentDraftPreservesCheckpointWithoutOverwritingTerminalSeal();
await testRecoveredPublicationRejectsLateFailure();
await testNoopStoppedRefreshRetainsPublicationFailure();
await testConcurrentParentDraftsPreserveBothCheckpoints();
+await testReplacementFailureDuringCancellation();
await testCoordinatorCommitResponseRecovery();
await testCoordinatorCommitResponseRecovery("commit-deep-scan-dedup");
await testCoordinatorCommitResponseRecovery("finish-deep-scan");
@@ -1038,3 +1039,118 @@ async function writePrivateFile(filePath: string, content: string) {
}
console.log("deep scan store integration tests passed");
+
+async function testReplacementFailureDuringCancellation() {
+ const { fixtureRoot, targetPath, environment } = await createWorkbenchFixture(
+ "deep-scan-cancel-replacement-",
+ );
+ const runWorkbench = createWorkbenchRunner(environment);
+ const cancellationEntered = Promise.withResolvers<void>();
+ const releaseCancellation = Promise.withResolvers<void>();
+ let coordinator: InstanceType<typeof DeepScanCoordinator> | undefined;
+ let cancellation: Promise<void> | undefined;
+ try {
+ const threadId = "cancel-replacement-owner";
+ const store = new WorkbenchDeepScanStore(runWorkbench);
+ const run = await store.begin({
+ targetPath,
+ scope: ".",
+ threadId,
+ scanRoot: path.join(fixtureRoot, "scans"),
+ });
+ const claim = await store.claimCoordinator({
+ scanId: run.scanId,
+ threadId,
+ });
+ assert.equal(claim.acquired, true);
+ const executor = new FakeExecutor({ blockDiscoveryAfterCalls: 0 });
+ let publications = 0;
+ coordinator = new DeepScanCoordinator({
+ run: claim.run,
+ store,
+ executor,
+ pluginRoot,
+ threadId,
+ heartbeatIntervalMs: 60_000,
+ onStopped: async () => {
+ publications += 1;
+ },
+ });
+ coordinator.start();
+ const settled = coordinator.settled();
+ await Promise.race([
+ executor.discoveryStarted.promise,
+ settled.then(() => assert.fail("coordinator stopped before discovery")),
+ ]);
+ cancellation = assert.rejects(
+ coordinator.cancelAfterPersistence(
+ "user_cancel",
+ async () => {
+ cancellationEntered.resolve();
+ await releaseCancellation.promise;
+ await runWorkbench(["cancel-scan", "--scan-id", run.scanId]);
+ },
+ async () => {
+ const parent = (
+ await runWorkbench(["get-scan", "--scan-id", run.scanId])
+ ).workspace.results;
+ return {
+ status: parent.progress.status,
+ failureMessage: parent.failureMessage,
+ };
+ },
+ ),
+ /Only a running scan can be canceled/,
+ );
+ await cancellationEntered.promise;
+ assert.equal(executor.runningDiscovery, 0);
+ assert.equal((await store.get(run.scanId, threadId)).status, "running");
+
+ // Expire only the lease clock; claim and failure use the real workbench.
+ await execFileAsync(process.env.PYTHON?.trim() || "python3", [
+ "-c",
+ `import sqlite3, sys
+connection = sqlite3.connect(sys.argv[1])
+connection.execute("UPDATE deep_scan_runs SET updated_at = ? WHERE scan_id = ?", ("2000-01-01T00:00:00Z", sys.argv[2]))
+connection.commit()`,
+ path.join(environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
+ run.scanId,
+ ]);
+ await rm(
+ path.join(
+ run.scanDir,
+ "artifacts",
+ "deep_discovery",
+ `coordinator-heartbeat-${claim.run.coordinatorGeneration}.json`,
+ ),
+ { force: true },
+ );
+ const replacement = new WorkbenchDeepScanStore(runWorkbench);
+ const adopted = await replacement.claimCoordinator({
+ scanId: run.scanId,
+ threadId,
+ });
+ assert.equal(adopted.acquired, true);
+ assert.equal(
+ adopted.run.coordinatorGeneration,
+ claim.run.coordinatorGeneration + 1,
+ );
+ const failure = await replacement.fail(run.scanId, "replacement failure");
+ assert.equal(failure.status, "failed");
+ releaseCancellation.resolve();
+ await cancellation;
+ const terminal = await settled;
+ assert.equal(terminal.status, "failed");
+ assert.equal(terminal.error, "replacement failure");
+ assert.equal(
+ terminal.coordinatorGeneration,
+ adopted.run.coordinatorGeneration,
+ );
+ assert.equal(publications, 0, "the old generation must not publish");
+ } finally {
+ releaseCancellation.resolve();
+ coordinator?.cancel("fixture_cleanup");
+ await Promise.allSettled([cancellation, coordinator?.settled()]);
+ await rm(fixtureRoot, { recursive: true, force: true });
+ }
+}
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_terminal_race.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_terminal_race.ts
new file mode 100644
index 000000000..3ab9376bf
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_terminal_race.ts
@@ -0,0 +1,403 @@
+import assert from "node:assert/strict";
+import { execFile } from "node:child_process";
+import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
+import { join } from "node:path";
+import { test } from "node:test";
+import { fileURLToPath } from "node:url";
+import { promisify } from "node:util";
+import type { ScanDraftInput } from "../src/artifact-scan-draft.js";
+import type { DeepScanRunState } from "../src/deep-scan/types.js";
+import { importModule } from "./import-module.ts";
+import { temporaryDirectory } from "./support/temporary-directories.ts";
+import { FakeExecutor } from "./deep_scan_coordinator_fixture.ts";
+
+const execFileAsync = promisify(execFile);
+const applicationRoot = fileURLToPath(new URL("../", import.meta.url));
+const pluginRoot = fileURLToPath(new URL("../../", import.meta.url));
+const {
+ WorkbenchDeepScanStore,
+ DeepScanCoordinatorRegistry,
+ createScanArtifactContext,
+ recordCodexSecurityScanDraftViaWorkbench,
+} = await importModule({
+ stdin: {
+ contents: `export { WorkbenchDeepScanStore } from "./src/deep-scan/store.ts";
+export { DeepScanCoordinatorRegistry } from "./src/deep-scan/registry.ts";
+export { createScanArtifactContext } from "./src/artifact-context.ts";
+export { recordCodexSecurityScanDraftViaWorkbench } from "./src/artifact-scan-draft.ts";`,
+ resolveDir: applicationRoot,
+ },
+ loader: { ".md": "text" },
+});
+const source = await readFile(new URL("../server.ts", import.meta.url), "utf8");
+
+for (const ordering of [
+ "failure response first",
+ "failure response last",
+ "lost cancellation response",
+ "cancellation before admission",
+ "cancellation before admission with active worker",
+ "failure before admission with active worker",
+ "cancellation before failure",
+ "lost cancellation before failure",
+ "cancellation before failure during publication",
+ "lost cancellation before failure during publication",
+]) {
+ await test(ordering, { timeout: 30_000 }, async () => {
+ const canceledBeforeAdmission = ordering.startsWith(
+ "cancellation before admission",
+ );
+ const failedBeforeAdmission =
+ ordering === "failure before admission with active worker";
+ const activeBeforeAdmission = ordering.endsWith("active worker");
+ const canceledBeforeFailure = ordering.includes(
+ "cancellation before failure",
+ );
+ const lostCancellation = ordering.startsWith("lost cancellation");
+ const failureDuringPublication = ordering.endsWith("during publication");
+ const root = await temporaryDirectory("deep-scan-terminal-race-");
+ const registry = new DeepScanCoordinatorRegistry();
+ const cleanupEntered = Promise.withResolvers<void>();
+ const cleanupRelease = Promise.withResolvers<void>();
+ const cancelEntered = Promise.withResolvers<void>();
+ const cancelRelease = Promise.withResolvers<void>();
+ const failureCommitted = Promise.withResolvers<void>();
+ const failureResponse = Promise.withResolvers<void>();
+ const finishCommitted = Promise.withResolvers<void>();
+ const finishResponse = Promise.withResolvers<void>();
+ const admissionRead = Promise.withResolvers<void>();
+ const publicationEntered = Promise.withResolvers<void>();
+ const publicationRelease = Promise.withResolvers<void>();
+ let server:
+ | ReturnType<(typeof import("../server.ts"))["createCodexSecurityServer"]>
+ | undefined;
+ let terminal: Promise<DeepScanRunState> | undefined;
+ let cancellation: Promise<unknown> | undefined;
+ let failure: Promise<unknown> | undefined;
+ try {
+ const target = join(root, "target");
+ const environment = {
+ ...process.env,
+ CODEX_SECURITY_STATE_DIR: join(root, "state"),
+ CODEX_HOME: join(root, "home"),
+ };
+ await mkdir(target);
+ await writeFile(join(target, "fixture.py"), "value = 1\n");
+ await mkdir(join(environment.CODEX_HOME, "codex-security"), {
+ recursive: true,
+ });
+ await writeFile(
+ join(environment.CODEX_HOME, "codex-security", "config.toml"),
+ "[deep_scan]\nworkers = 1\nmax_discovery_runs = 1\n" +
+ (canceledBeforeFailure || activeBeforeAdmission
+ ? ""
+ : "max_time_hours = 1e-12\n"),
+ { mode: 0o600 },
+ );
+ const runWorkbench = async (args: string[]) => {
+ const { stdout } = await execFileAsync(
+ process.env.PYTHON || "python3",
+ [join(pluginRoot, "scripts", "workbench_db.py"), ...args],
+ { cwd: pluginRoot, env: environment },
+ );
+ return JSON.parse(stdout);
+ };
+ const workbench = async (args: string[]) => {
+ if (args[0] === "cancel-scan" && !canceledBeforeFailure) {
+ cancelEntered.resolve();
+ await cancelRelease.promise;
+ }
+ const result = await runWorkbench(args);
+ if (
+ args[0] === "finish-deep-scan" &&
+ canceledBeforeAdmission &&
+ !activeBeforeAdmission
+ ) {
+ finishCommitted.resolve();
+ await finishResponse.promise;
+ }
+ if (args[0] === "get-scan") admissionRead.resolve();
+ if (args[0] === "cancel-scan" && canceledBeforeFailure) {
+ cancelEntered.resolve();
+ await cancelRelease.promise;
+ }
+ if (args[0] === "fail-scan") {
+ failureCommitted.resolve();
+ await failureResponse.promise;
+ }
+ if (args[0] === "cancel-scan" && lostCancellation)
+ throw new Error("synthetic committed cancellation response lost");
+ return result;
+ };
+ Object.assign(globalThis, {
+ terminalRaceFixture: { registry, workbench },
+ });
+ const { createCodexSecurityServer } = await importModule({
+ stdin: {
+ contents: source
+ .replace(
+ "const deepScanCoordinators = new DeepScanCoordinatorRegistry();",
+ "const deepScanCoordinators = globalThis.terminalRaceFixture.registry;",
+ )
+ .replace(
+ 'import { PLUGIN_ROOT, runWorkbench, scanRoot } from "./src/workbench-client.js";',
+ 'import { PLUGIN_ROOT, scanRoot } from "./src/workbench-client.js";\nconst runWorkbench = (...args) => globalThis.terminalRaceFixture.workbench(...args);',
+ ),
+ loader: "ts",
+ resolveDir: applicationRoot,
+ },
+ define: {
+ __dirname: JSON.stringify(applicationRoot),
+ "import.meta.url": JSON.stringify(
+ new URL("../server.ts", import.meta.url).href,
+ ),
+ },
+ loader: { ".md": "text" },
+ });
+ const application = createCodexSecurityServer();
+ server = application;
+ const cancel =
+ application._registeredTools.cancel_codex_security_scan_from_app
+ .handler;
+ const fail =
+ application._registeredTools.fail_codex_security_scan.handler;
+ const store = new WorkbenchDeepScanStore(workbench);
+ const run = await store.begin({
+ targetPath: target,
+ scope: ".",
+ threadId: "fixture-owner",
+ scanRoot: join(root, "scans"),
+ });
+ const claim = await store.claimCoordinator({
+ scanId: run.scanId,
+ threadId: "fixture-owner",
+ });
+ const get = store.get.bind(store);
+ let held = false;
+ store.get = async (...args: [string, string]) => {
+ const result = await get(...args);
+ if (
+ !held &&
+ result.status === "succeeded" &&
+ !canceledBeforeAdmission
+ ) {
+ held = true;
+ cleanupEntered.resolve();
+ await cleanupRelease.promise;
+ }
+ return result;
+ };
+ const executor = new FakeExecutor({ blockDiscoveryAfterCalls: 0 });
+ const coordinator = registry.start({
+ run: claim.run,
+ store,
+ executor:
+ canceledBeforeFailure || activeBeforeAdmission
+ ? executor
+ : {
+ async run() {
+ throw new Error("expired deadline must not launch a worker");
+ },
+ },
+ pluginRoot,
+ threadId: "fixture-owner",
+ heartbeatIntervalMs: 60_000,
+ onComplete: async (draft: ScanDraftInput) => {
+ const context = await createScanArtifactContext(
+ run.scanId,
+ runWorkbench,
+ { requireRunning: true },
+ );
+ await recordCodexSecurityScanDraftViaWorkbench(
+ context,
+ draft,
+ runWorkbench,
+ );
+ },
+ onStopped: async (stopped: DeepScanRunState) => {
+ if (failureDuringPublication) {
+ publicationEntered.resolve();
+ await publicationRelease.promise;
+ }
+ await runWorkbench([
+ "preserve-scan-results",
+ "--scan-id",
+ stopped.scanId,
+ "--thread-id",
+ "fixture-owner",
+ "--coordinator-generation",
+ String(stopped.coordinatorGeneration),
+ ]);
+ },
+ });
+ terminal = coordinator.settled();
+ void terminal!.catch(() => {});
+ if (canceledBeforeAdmission || failedBeforeAdmission) {
+ if (activeBeforeAdmission) await executor.discoveryStarted.promise;
+ else await finishCommitted.promise;
+ await runWorkbench([
+ failedBeforeAdmission ? "fail-scan" : "cancel-scan",
+ "--scan-id",
+ run.scanId,
+ ...(failedBeforeAdmission
+ ? ["--message", "synthetic externally persisted failure"]
+ : []),
+ ]);
+ cancelRelease.resolve();
+ const cancellationResult = cancel({ scanId: run.scanId });
+ cancellation = cancellationResult;
+ void cancellation!.catch(() => {});
+ await Promise.race([admissionRead.promise, cancellation]);
+ if (activeBeforeAdmission) {
+ await new Promise(setImmediate);
+ assert.equal(
+ executor.runningDiscovery,
+ 0,
+ "accepted cancellation stops the worker before the next heartbeat",
+ );
+ }
+ finishResponse.resolve();
+ const result = await terminal!;
+ assert.equal(
+ result.status,
+ failedBeforeAdmission ? "failed" : "canceled",
+ );
+ if (!activeBeforeAdmission) assert.equal(result.error, undefined);
+ const canceled = await cancellationResult;
+ if (failedBeforeAdmission) {
+ assert.equal(result.error, "synthetic externally persisted failure");
+ assert.equal(
+ canceled.structuredContent.workspace.results.failureMessage,
+ "synthetic externally persisted failure",
+ );
+ assert.equal(
+ canceled.structuredContent.workspace.results.progress.status,
+ "failed",
+ );
+ }
+ return;
+ }
+ if (canceledBeforeFailure) {
+ await executor.discoveryStarted.promise;
+ cancellation = cancel({ scanId: run.scanId });
+ void cancellation!.catch(() => {});
+ await cancelEntered.promise;
+ if (failureDuringPublication) {
+ cancelRelease.resolve();
+ await publicationEntered.promise;
+ }
+ failureResponse.resolve();
+ await fail(
+ { scanId: run.scanId, message: "synthetic no-op failure" },
+ {},
+ );
+ assert.equal(
+ (await runWorkbench(["get-scan", "--scan-id", run.scanId])).workspace
+ .results.progress.status,
+ "canceled",
+ );
+ assert.equal(
+ (await store.get(run.scanId, "fixture-owner")).status,
+ "canceled",
+ );
+ cancelRelease.resolve();
+ publicationRelease.resolve();
+ if (lostCancellation) {
+ await assert.rejects(
+ cancellation!,
+ /synthetic committed cancellation response lost/,
+ );
+ await assert.rejects(
+ terminal!,
+ /synthetic committed cancellation response lost/,
+ );
+ } else {
+ await cancellation;
+ const result = await terminal!;
+ assert.equal(result.status, "canceled");
+ assert.equal(
+ result.error,
+ (await store.get(run.scanId, "fixture-owner")).error,
+ );
+ assert.doesNotMatch(result.error ?? "", /synthetic no-op failure/);
+ }
+ return;
+ }
+ await cleanupEntered.promise;
+ cancellation = cancel({ scanId: run.scanId });
+ void cancellation!.catch(() => {});
+ await cancelEntered.promise;
+ if (ordering !== "lost cancellation response") {
+ failure = fail(
+ { scanId: run.scanId, message: "synthetic committed parent failure" },
+ {},
+ );
+ void failure!.catch(() => {});
+ await failureCommitted.promise;
+ assert.equal(
+ (
+ await runWorkbench([
+ "get-deep-scan",
+ "--scan-id",
+ run.scanId,
+ "--thread-id",
+ "fixture-owner",
+ ])
+ ).deepScan.status,
+ "succeeded",
+ );
+ assert.equal(
+ (await runWorkbench(["get-scan", "--scan-id", run.scanId])).workspace
+ .results.progress.status,
+ "failed",
+ );
+ if (ordering === "failure response first") {
+ failureResponse.resolve();
+ await failure;
+ }
+ }
+ cancelRelease.resolve();
+ cleanupRelease.resolve();
+ if (ordering === "lost cancellation response") {
+ await assert.rejects(
+ cancellation!,
+ /synthetic committed cancellation response lost/,
+ );
+ await assert.rejects(
+ terminal!,
+ /synthetic committed cancellation response lost/,
+ );
+ assert.equal(
+ (await runWorkbench(["get-scan", "--scan-id", run.scanId])).workspace
+ .results.progress.status,
+ "canceled",
+ );
+ } else {
+ await assert.rejects(
+ cancellation!,
+ /Only a running scan can be canceled/,
+ );
+ const stopped = await terminal!;
+ assert.equal(
+ stopped.status,
+ ordering === "failure response first" ? "failed" : "succeeded",
+ );
+ if (ordering === "failure response first")
+ assert.match(stopped.error!, /synthetic committed parent failure/);
+ failureResponse.resolve();
+ await failure;
+ }
+ } finally {
+ cleanupRelease.resolve();
+ cancelRelease.resolve();
+ failureResponse.resolve();
+ finishResponse.resolve();
+ publicationRelease.resolve();
+ registry.shutdown("fixture cleanup");
+ await Promise.allSettled([terminal, cancellation, failure]);
+ await server?.close();
+ Reflect.deleteProperty(globalThis, "terminalRaceFixture");
+ await rm(root, { recursive: true, force: true });
+ }
+ });
+}
diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_process.ts b/plugins/codex-security/mcp-app/tests/test_workbench_process.ts
index 7521156c6..913668023 100644
--- a/plugins/codex-security/mcp-app/tests/test_workbench_process.ts
+++ b/plugins/codex-security/mcp-app/tests/test_workbench_process.ts
@@ -90,6 +90,24 @@ try {
return true;
},
);
+ await assert.rejects(
+ promisify(execFile)(
+ process.execPath,
+ ["-e", 'process.stdout.write("x".repeat(65536))'],
+ { maxBuffer: 1024 },
+ ),
+ (error: unknown) => {
+ assert.ok(error instanceof Error);
+ assert.ok("code" in error);
+ assert.equal(error.code, "ERR_CHILD_PROCESS_STDIO_MAXBUFFER");
+ failure = error;
+ return true;
+ },
+ );
+ await assert.rejects(
+ executeWorkbench("fixture-python", ["cancel-scan"]),
+ (error) => error === failure,
+ );
} finally {
delete globalThis.workbenchProcessFixture;
}
@@ -293,3 +311,65 @@ try {
else process.env.CODEX_SECURITY_STATE_DIR = previousStateDir;
await rm(root, { recursive: true, force: true });
}
+
+await test("large saved context retains cancellation results and diagnostics", async () => {
+ const root = await temporaryDirectory("workbench-large-context-");
+ const previousStateDir = process.env.CODEX_SECURITY_STATE_DIR;
+ try {
+ const target = path.join(root, "target");
+ await mkdir(target);
+ await writeFile(path.join(target, "example.py"), "value = 1\n");
+ const state = path.join(root, "state");
+ process.env.CODEX_SECURITY_STATE_DIR = state;
+ const python = process.env.PYTHON || "python3";
+ const userContext = "x".repeat(1_500_000);
+ const begun = (await executeWorkbench(
+ python,
+ [
+ "begin-deep-scan",
+ "--target-path",
+ target,
+ "--scope",
+ ".",
+ "--thread-id",
+ "synthetic-owner",
+ "--scan-root",
+ path.join(root, "scans"),
+ "--user-context-stdin",
+ ],
+ userContext,
+ )) as { deepScan: { scanId: string } };
+ const scanId = begun.deepScan.scanId;
+ const readScan = async () =>
+ (await executeWorkbench(python, ["get-scan", "--scan-id", scanId])) as {
+ workspace: {
+ results: { userContext: string; progress: { status: string } };
+ };
+ };
+ const current = await readScan();
+ assert.equal(current.workspace.results.userContext, userContext);
+ assert.equal(current.workspace.results.progress.status, "running");
+ await executeWorkbench(python, ["cancel-scan", "--scan-id", scanId]);
+ assert.equal(
+ (await readScan()).workspace.results.progress.status,
+ "canceled",
+ );
+ await assert.rejects(
+ executeWorkbench(python, [
+ "cancel-scan",
+ "--scan-id",
+ "00000000-0000-4000-8000-000000000000",
+ ]),
+ (error: unknown) => {
+ assert.ok(error instanceof Error && "stderr" in error);
+ assert.match(String(error.stderr), /Codex Security scan not found/);
+ return true;
+ },
+ );
+ } finally {
+ if (previousStateDir === undefined)
+ delete process.env.CODEX_SECURITY_STATE_DIR;
+ else process.env.CODEX_SECURITY_STATE_DIR = previousStateDir;
+ await rm(root, { recursive: true, force: true });
+ }
+});