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(sdk): persist canceled scans with their measured cost (#2307)

Michael D'Angelob71feff1fd51
diff --git a/plugins/codex-security/scripts/workbench_cli.py b/plugins/codex-security/scripts/workbench_cli.py
index 47294f39d..09196abc6 100644
--- a/plugins/codex-security/scripts/workbench_cli.py
+++ b/plugins/codex-security/scripts/workbench_cli.py
@@ -308,6 +308,7 @@ def parse_args(description: str, *, inspection_only: bool = False) -> argparse.N
cancel_scan = add_command("cancel-scan", "--scan-id")
cancel_scan.add_argument("--thread-id")
+ cancel_scan.add_argument("--cost-json")
fail_scan = add_command("fail-scan", "--scan-id", "--message")
fail_scan.add_argument("--claim-token")
diff --git a/plugins/codex-security/scripts/workbench_saved_results.py b/plugins/codex-security/scripts/workbench_saved_results.py
index f61f3c3a0..9394cc0f1 100644
--- a/plugins/codex-security/scripts/workbench_saved_results.py
+++ b/plugins/codex-security/scripts/workbench_saved_results.py
@@ -1293,6 +1293,7 @@ def cancel_scan(db: Any, connection: Any, args: Any) -> dict[str, Any]:
def cancel_scan_locked(db: Any, connection: Any, args: Any) -> dict[str, Any]:
scan_id = db.require_uuid(args.scan_id, "scan-id")
thread_id = db.optional_text(args.thread_id, maximum=512)
+ cost_json = db.parse_scan_cost(getattr(args, "cost_json", None))
connection.execute("BEGIN IMMEDIATE")
with connection:
timestamp = db.now()
@@ -1312,10 +1313,11 @@ def cancel_scan_locked(db: Any, connection: Any, args: Any) -> dict[str, Any]:
updated = connection.execute(
"""
UPDATE scans
- SET status = 'failed', canceled_at = ?, completed_at = ?, updated_at = ?
+ SET status = 'failed', canceled_at = ?, completed_at = ?, updated_at = ?,
+ cost_json = COALESCE(?, cost_json)
WHERE id = ? AND status = 'running'
""",
- (timestamp, timestamp, timestamp, scan["id"]),
+ (timestamp, timestamp, timestamp, cost_json, scan["id"]),
)
if updated.rowcount != 1:
raise SystemExit("Only a running scan can be canceled.")
diff --git a/plugins/codex-security/tests/test_workbench_cancellation.py b/plugins/codex-security/tests/test_workbench_cancellation.py
index e0d74a4e5..cae122b3e 100644
--- a/plugins/codex-security/tests/test_workbench_cancellation.py
+++ b/plugins/codex-security/tests/test_workbench_cancellation.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import argparse
+import json
import uuid
from contextlib import closing
from pathlib import Path
@@ -148,12 +149,24 @@ def test_workbench_cancels_running_scan_and_rejects_late_updates(tmp_path: Path)
assert wrong_thread["returncode"] != 0
assert "owning Codex thread" in str(wrong_thread["stderr"])
- canceled = cancel_scan(state_dir, scan_id, thread_id)
+ cost = {
+ "model": "synthetic-model",
+ "inputTokens": 25,
+ "cachedInputTokens": 0,
+ "cacheWriteInputTokens": 0,
+ "outputTokens": 1,
+ "estimatedUsd": 0.001,
+ }
+ canceled = scan_command(
+ state_dir, "cancel-scan", scan_id, "--thread-id", thread_id, "--cost-json", json.dumps(cost)
+ )
+ assert canceled["results"]["cost"] == cost
assert canceled["results"]["progress"]["status"] == "canceled"
assert canceled["results"]["canceledAt"]
assert canceled["results"]["handoffClaimToken"] == claim_token
replayed = cancel_scan(state_dir, scan_id, thread_id)
+ assert replayed["results"]["cost"] == cost
assert replayed["results"]["canceledAt"] == canceled["results"]["canceledAt"]
for command in (
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.md
index ace4160fb..325f226e0 100644
--- a/sdk/typescript/docs/cli.md
+++ b/sdk/typescript/docs/cli.md
@@ -1003,6 +1003,9 @@ findings. Use `codex-security scans list --json` for the current repository.
To find an offline import, use `codex-security scans list --scan-root <DIR> --json`
with its output directory. Imported scans have lifecycle mode `import`. The SDK
exposes `onLifecycle`, and the CLI includes observations in verbose diagnostics.
+Canceling a newly registered SDK scan through its abort signal, closing the
+client during the scan, or receiving a canceled hosted execution records
+`canceled`. A scan that has already completed keeps its committed outcome.
Applications can choose how to collect and export these observations.
For filesystem and approval behavior, see the
diff --git a/sdk/typescript/docs/hosted-scans.md b/sdk/typescript/docs/hosted-scans.md
index 83169ff08..61f279f05 100644
--- a/sdk/typescript/docs/hosted-scans.md
+++ b/sdk/typescript/docs/hosted-scans.md
@@ -288,8 +288,15 @@ a canceled receipt with the requested checkout, scope, and rubric before startin
the executor. Invalid request fields still return a JSON-RPC error.
After dispatch the CLI aborts its executor signal, emits
`execution.cancel` with `{requestId}`, and waits for the host's outcome/usage
-before returning a canceled result. The host must continue reading notifications
-while executing. SIGINT/SIGTERM, output errors/closure, and cancellation with
+before returning a canceled result. For a registered scan, the durable lifecycle
+and final scan receipt record cancellation even if the executor reports failure.
+The `execution` field retains that failure, its diagnostics, and any usage.
+A caller deadline expiring after registration records a failed scan, including
+when the executor acknowledges the deadline with a canceled execution.
+If the executor reports `acceptance_unknown`, the local scan records `failed`
+and the host receives `acceptance_unknown` for reconciliation.
+
+The host must continue reading notifications while executing. SIGINT/SIGTERM, output errors/closure, and cancellation with
blocked output terminate the transport even while a write callback is stuck.
Input EOF before the terminal response fails the run; EOF while that response is
flushing waits for the write callback. A selected successful result is not a
diff --git a/sdk/typescript/scripts/fixtures/package-behavior.mjs b/sdk/typescript/scripts/fixtures/package-behavior.mjs
index 2d3941ce3..be351602e 100644
--- a/sdk/typescript/scripts/fixtures/package-behavior.mjs
+++ b/sdk/typescript/scripts/fixtures/package-behavior.mjs
@@ -183,7 +183,7 @@ try {
}),
sdk.ScanInterruptedError,
);
- assert.equal((await savedScan(1)).progress.status, "failed");
+ assert.equal((await savedScan(1)).progress.status, "canceled");
assert.equal(finished.has(1), true);
let closing;
@@ -198,7 +198,7 @@ try {
);
assert.ok(closing);
await closing;
- assert.equal((await savedScan(2)).progress.status, "failed");
+ assert.equal((await savedScan(2)).progress.status, "canceled");
assert.equal(finished.has(2), true);
assert.equal(turns.length, 3);
await assert.rejects(client.run(repository), /CodexSecurity is closed/u);
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index d3c503a59..3cada68ed 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -3,6 +3,8 @@ import { workbenchArgs } from "./workbench-arguments.js";
import { isSafeNonNegativeInteger as safeInteger } from "./value.js";
import {
+ explicitlyCanceled,
+ hostedScanFailureStatus,
ScanExecutionError,
ScanExecutionResultSchema,
type ScanExecutor,
@@ -2011,11 +2013,11 @@ export class CodexSecurity {
this.#requireOpen();
if (options.mock) return await this.#runMock(repository, options);
const costAbortController = new AbortController();
- const signal = AbortSignal.any([
+ const callerSignal = AbortSignal.any([
this.#abortController.signal,
- costAbortController.signal,
...(options.signal === undefined ? [] : [options.signal]),
]);
+ const signal = AbortSignal.any([callerSignal, costAbortController.signal]);
const budgetAbortController = new AbortController();
const budgetSignal = AbortSignal.any([
signal,
@@ -3655,13 +3657,18 @@ export class CodexSecurity {
}
return result;
} catch (error) {
- const snapshot = await costTracker?.stop().catch(() => null);
- if (artifactRestorationFailure !== null) throw artifactRestorationFailure;
- const terminalTurnStatus = terminalScanTurnStatus();
+ // Capture who stopped the scan before cleanup can fire another signal.
+ const callerCancellation =
+ callerSignal.aborted && Object.is(signal.reason, callerSignal.reason)
+ ? callerSignal
+ : undefined;
let failure =
- signal.reason instanceof ScanCostLimitExceededError
+ signal.aborted && callerCancellation === undefined
? signal.reason
: error;
+ const snapshot = await costTracker?.stop().catch(() => null);
+ if (artifactRestorationFailure !== null) throw artifactRestorationFailure;
+ const terminalTurnStatus = terminalScanTurnStatus();
if (
failure instanceof ScanCostLimitExceededError &&
snapshot?.cost &&
@@ -3678,8 +3685,7 @@ export class CodexSecurity {
budgetRecovery !== null &&
budgetRecovery.threadId !== null &&
activeScan !== null &&
- !this.#abortController.signal.aborted &&
- options.signal?.aborted !== true
+ !callerSignal.aborted
) {
try {
const completion = await workbench(
@@ -3713,10 +3719,7 @@ export class CodexSecurity {
scanDir,
budgetRecovery.pluginRoot,
budgetRecovery.expectation,
- AbortSignal.any([
- this.#abortController.signal,
- ...(options.signal === undefined ? [] : [options.signal]),
- ]),
+ callerSignal,
true,
budgetRecovery.pythonPath,
budgetRecovery.protectedRoot,
@@ -3794,20 +3797,18 @@ export class CodexSecurity {
}).catch(() => undefined);
}
try {
- const failed = await workbench(
- { ...activeScan.options, signal: undefined },
- [
- "fail-scan",
- "--scan-id",
- activeScan.id,
- `--message=${errorMessage(failure)}`,
- ...(snapshot?.cost
- ? ["--cost-json", JSON.stringify(snapshot.cost)]
- : []),
- ],
+ await stopRegisteredScan(
+ workbench,
+ activeScan,
+ options,
+ this.#dependencies.hostedScan
+ ? hostedScanFailureStatus(failure, callerCancellation) ===
+ "canceled"
+ : explicitlyCanceled(callerCancellation),
+ failure,
+ snapshot?.cost,
);
activeScan = null;
- notifyScanLifecycle(options, failed["scan"], true);
} catch {}
}
if (runPostScan !== null && !signal.aborted) {
@@ -4898,17 +4899,14 @@ export class CodexSecurity {
} catch (error) {
if (activeScan !== undefined) {
try {
- const failed = await workbench(
- { ...activeScan.options, signal: undefined },
- [
- "fail-scan",
- "--scan-id",
- activeScan.id,
- `--message=${errorMessage(error)}`,
- ],
+ await stopRegisteredScan(
+ workbench,
+ activeScan,
+ options,
+ explicitlyCanceled(signal),
+ error,
);
activeScan = undefined;
- notifyScanLifecycle(options, failed["scan"], true);
} catch {}
}
if (this.#closed) this.#requireOpen();
@@ -6122,6 +6120,49 @@ function notifyScanLifecycle(
notifyObserver(options, "onLifecycle")(snapshot.data);
}
+async function stopRegisteredScan(
+ workbench: typeof runWorkbench,
+ activeScan: { id: string; options: WorkbenchCommandOptions },
+ options: ScanOptions,
+ canceled: boolean,
+ error: unknown,
+ cost?: Readonly<ScanCost> | null,
+): Promise<void> {
+ const cleanupOptions = { ...activeScan.options, signal: undefined };
+ const costArgs = cost ? ["--cost-json", JSON.stringify(cost)] : [];
+ if (canceled) {
+ try {
+ const stopped = await workbench(cleanupOptions, [
+ "cancel-scan",
+ "--scan-id",
+ activeScan.id,
+ ...costArgs,
+ ]);
+ // A workspace can already display a replacement when cancellation returns.
+ const results = stopped["results"];
+ if (isRecord(results) && results["scanId"] === activeScan.id)
+ notifyScanLifecycle(options, results, true);
+ else observeSettledScan(options, activeScan);
+ return;
+ } catch (cause) {
+ // Older external workbenches can only persist cost on a failed scan.
+ if (
+ !cost ||
+ !errorMessage(cause).includes("unrecognized arguments: --cost-json")
+ )
+ throw cause;
+ }
+ }
+ const failed = await workbench(cleanupOptions, [
+ "fail-scan",
+ "--scan-id",
+ activeScan.id,
+ `--message=${errorMessage(error)}`,
+ ...costArgs,
+ ]);
+ notifyScanLifecycle(options, failed["scan"], true);
+}
+
function observeSettledScan(
options: ScanOptions,
activeScan:
diff --git a/sdk/typescript/src/hosted-scan.ts b/sdk/typescript/src/hosted-scan.ts
index 4580dfe55..73f25f01a 100644
--- a/sdk/typescript/src/hosted-scan.ts
+++ b/sdk/typescript/src/hosted-scan.ts
@@ -8,6 +8,7 @@ import {
import { isRecord } from "./record.js";
import { VERSION } from "./version.js";
import {
+ hostedScanFailureStatus,
ScanExecutionError,
type ScanExecutor,
type ScanExecutionResult,
@@ -20,7 +21,7 @@ import { isAbsolute, relative, resolve, sep } from "node:path";
import { z } from "zod";
import { CodexSecurity, type CodexSecuritySurface } from "./api.js";
import { severityRubricSnapshotSchema } from "./classify-severity-records.js";
-import { ScanInterruptedError, errorMessage } from "./errors.js";
+import { errorMessage } from "./errors.js";
import {
abortable,
normalizeRepository,
@@ -225,6 +226,7 @@ export async function runHostedScanInternal(
let execution: ScanExecutionResult | undefined;
let request: ScanExecutionRequest | undefined;
let scanId: string | undefined;
+ let stoppedStatus: "canceled" | "failed" | undefined;
let outputDirectory = resolve(input.outputDirectory);
const base = () => ({
version: 2 as const,
@@ -309,6 +311,10 @@ export async function runHostedScanInternal(
scanId = scan.scanId;
outputDirectory = scan.scanDir;
},
+ onLifecycle(scan) {
+ if (scan.status === "canceled" || scan.status === "failed")
+ stoppedStatus = scan.status;
+ },
onProgress(progress) {
onEvent({ type: "progress", progress });
},
@@ -372,16 +378,12 @@ export async function runHostedScanInternal(
};
} catch (error) {
if (error instanceof ScanExecutionError) execution = error.result;
+ const status = hostedScanFailureStatus(error, options.signal, execution);
return {
...base(),
+ // A late abort during cleanup cannot change an already committed failure.
status:
- execution?.status === "acceptance_unknown"
- ? ("acceptance_unknown" as const)
- : options.signal?.aborted ||
- execution?.status === "canceled" ||
- error instanceof ScanInterruptedError
- ? ("canceled" as const)
- : ("failed" as const),
+ status === "acceptance_unknown" ? status : (stoppedStatus ?? status),
error: errorMessage(error),
artifacts: [],
};
diff --git a/sdk/typescript/src/scan-executor.ts b/sdk/typescript/src/scan-executor.ts
index 45715aa57..6fef92bb3 100644
--- a/sdk/typescript/src/scan-executor.ts
+++ b/sdk/typescript/src/scan-executor.ts
@@ -1,6 +1,10 @@
import { z } from "zod";
import type { severityRubricSnapshotSchema } from "./classify-severity-records.js";
-import { CodexSecurityError } from "./errors.js";
+import {
+ CodexSecurityError,
+ ScanCostLimitExceededError,
+ ScanInterruptedError,
+} from "./errors.js";
import type { ScanActivity } from "./scan-activity.js";
import { scanPhaseSchema, type ScanProgress } from "./worker-progress.js";
@@ -109,3 +113,31 @@ export class ScanExecutionError extends CodexSecurityError {
);
}
}
+
+export function explicitlyCanceled(signal?: AbortSignal): boolean {
+ return (
+ signal?.aborted === true &&
+ !(signal.reason instanceof ScanCostLimitExceededError) &&
+ !(
+ signal.reason instanceof DOMException &&
+ signal.reason.name === "TimeoutError"
+ )
+ );
+}
+
+/** Classify the scan separately from the executor's retained outcome. */
+export function hostedScanFailureStatus(
+ error: unknown,
+ signal?: AbortSignal,
+ execution?: ScanExecutionResult,
+): "failed" | "canceled" | "acceptance_unknown" {
+ const status = (
+ error instanceof ScanExecutionError ? error.result : execution
+ )?.status;
+ if (status === "acceptance_unknown") return status;
+ if (signal?.aborted)
+ return explicitlyCanceled(signal) ? "canceled" : "failed";
+ return status === "canceled" || error instanceof ScanInterruptedError
+ ? "canceled"
+ : "failed";
+}
diff --git a/sdk/typescript/tests-ts/api-time-budget.test.ts b/sdk/typescript/tests-ts/api-time-budget.test.ts
index 1dfe3d4fe..3731282d9 100644
--- a/sdk/typescript/tests-ts/api-time-budget.test.ts
+++ b/sdk/typescript/tests-ts/api-time-budget.test.ts
@@ -179,7 +179,8 @@ test("explicit cancellation remains authoritative with an advisory allowance", a
signal: controller.signal,
}),
).rejects.toBeInstanceOf(ScanInterruptedError);
- expect(state.commands).toContain("fail-scan");
+ expect(state.commands).toContain("cancel-scan");
+ expect(state.commands).not.toContain("fail-scan");
expect(state.commands).not.toContain("complete-scan");
});
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index 7b4db7b59..b6e8008b9 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -3832,6 +3832,7 @@ if __name__ == "__main__":
await writeFile(join(codexHome, "sessions"), "not a directory");
const commands: string[] = [];
const warnings: string[] = [];
+ let savedFailure: string | undefined;
const client = TestClient.withDependencies({
...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (
@@ -3840,6 +3841,7 @@ if __name__ == "__main__":
input?: string,
): Promise<JsonObject> => {
commands.push(args[0]!);
+ if (args[0] === "fail-scan") savedFailure = args[3];
return mockWorkbench(args, input);
},
createCodex: completedCodex(root),
@@ -3853,6 +3855,8 @@ if __name__ == "__main__":
if (enforceCostLimit) {
await expect(scan).rejects.toThrow("interrupted");
expect(commands).toContain("fail-scan");
+ expect(savedFailure).toContain("sessions");
+ expect(commands).not.toContain("cancel-scan");
} else {
await expect(scan).resolves.toMatchObject({ threadId: "thread-1" });
expect(warnings).toContainEqual(
diff --git a/sdk/typescript/tests-ts/hosted-scan.test.ts b/sdk/typescript/tests-ts/hosted-scan.test.ts
index b88ee2eba..504b511c6 100644
--- a/sdk/typescript/tests-ts/hosted-scan.test.ts
+++ b/sdk/typescript/tests-ts/hosted-scan.test.ts
@@ -38,6 +38,7 @@ import { main } from "../src/cli.js";
import { capture, dependencies } from "./cli-fixtures.js";
import { normalizeTarget } from "../src/targets.js";
import { runWorkbench } from "../src/runtime.js";
+import { readSavedScanLifecycle } from "../src/saved-scan-bootstrap.js";
import { pythonExecutable } from "./support/python.js";
import { git } from "./git-fixture.js";
import { PLUGIN_ROOT } from "./plugin-root.js";
@@ -1098,6 +1099,15 @@ test.each([
expect(terminal.status).toBe(status);
expect(terminal.execution).toEqual(execution);
expect(terminal.artifacts).toEqual([]);
+ expect(
+ readSavedScanLifecycle(
+ { CODEX_SECURITY_STATE_DIR: input.stateDirectory },
+ terminal.scanId!,
+ ),
+ ).toMatchObject({
+ scanId: terminal.scanId,
+ status: status === "canceled" ? "canceled" : "failed",
+ });
},
);
@@ -1266,76 +1276,138 @@ test("wire cleanup failure retains an uncertain terminal result and reports clea
}
});
-test("cancellation after registration retains scan identity before executor dispatch", async () => {
- const input = await fixture();
- const controller = new AbortController();
- const executor = noExecution();
- const result = await runHostedScan(input, {
- signal: controller.signal,
- onEvent(event) {
- if (
- event.type === "progress" &&
- event.progress.phase === "preflight" &&
- event.progress.filesTotal > 0
- )
- controller.abort();
- },
- executor,
- });
- expect(result.status).toBe("canceled");
- expect(executor.run).not.toHaveBeenCalled();
- expect(result.scanId).toBeString();
- expect(result.execution).toBeUndefined();
- const history = await runWorkbench(
- {
- python: pythonExecutable()!,
- pluginRoot: PLUGIN_ROOT,
- environment: {
- PATH: process.env["PATH"],
- SYSTEMROOT: process.env["SYSTEMROOT"],
- CODEX_SECURITY_STATE_DIR: input.stateDirectory,
+test.each([false, true])(
+ "stopping after registration retains scan identity before executor dispatch (timeout=%p)",
+ async (timedOut) => {
+ const input = await fixture();
+ const controller = new AbortController();
+ const executor = noExecution();
+ const result = await runHostedScan(input, {
+ signal: controller.signal,
+ onEvent(event) {
+ if (
+ event.type === "progress" &&
+ event.progress.phase === "preflight" &&
+ event.progress.filesTotal > 0
+ )
+ controller.abort(
+ timedOut
+ ? new DOMException("Host deadline exceeded", "TimeoutError")
+ : undefined,
+ );
},
- },
- ["list-scans", "--repository", input.repository],
- );
- expect(history["scans"]).toMatchObject([
- {
+ executor,
+ });
+ const status = timedOut ? "failed" : "canceled";
+ expect(result.status).toBe(status);
+ expect(executor.run).not.toHaveBeenCalled();
+ expect(result.scanId).toBeString();
+ expect(result.execution).toBeUndefined();
+ const history = await runWorkbench(
+ {
+ python: pythonExecutable()!,
+ pluginRoot: PLUGIN_ROOT,
+ environment: {
+ PATH: process.env["PATH"],
+ SYSTEMROOT: process.env["SYSTEMROOT"],
+ CODEX_SECURITY_STATE_DIR: input.stateDirectory,
+ },
+ },
+ ["list-scans", "--repository", input.repository],
+ );
+ expect(history["scans"]).toMatchObject([
+ {
+ scanId: result.scanId,
+ scanDir: result.outputDirectory,
+ progress: { status },
+ lifecycle: { status },
+ },
+ ]);
+ },
+);
+
+test.each([
+ ["canceled", false, "canceled"],
+ ["failed", false, "canceled"],
+ ["acceptance_unknown", false, "acceptance_unknown"],
+ ["canceled", true, "failed"],
+] as const)(
+ "SDK cancellation retains the executor's %s outcome and usage (timeout=%p, scan=%s)",
+ async (executionStatus, timedOut, scanStatus) => {
+ const input = await fixture();
+ const controller = new AbortController();
+ input.severityRubric = { ...severityRubric };
+ const pending = runHostedScan(input, {
+ signal: controller.signal,
+ executor: {
+ async run(request, options) {
+ expect(request.severityRubric).toEqual(severityRubric);
+ Reflect.set(
+ request.severityRubric!,
+ "markdown",
+ "Edited by executor.",
+ );
+ controller.abort(
+ timedOut
+ ? new DOMException("Host deadline exceeded", "TimeoutError")
+ : undefined,
+ );
+ expect(options.signal.aborted).toBe(true);
+ await Promise.resolve();
+ return receipt(request, {
+ status: executionStatus,
+ error: { code: -32010, message: "Synthetic remote diagnostic" },
+ sessionId: "canceled-session",
+ usage: { input_tokens: 42 },
+ });
+ },
+ },
+ });
+ input.severityRubric.markdown = "Edited by caller.";
+ const result = await pending;
+ expect(result.status).toBe(scanStatus);
+ expect(
+ readSavedScanLifecycle(
+ { CODEX_SECURITY_STATE_DIR: input.stateDirectory },
+ result.scanId!,
+ ),
+ ).toMatchObject({
scanId: result.scanId,
- scanDir: result.outputDirectory,
- progress: { status: "failed" },
- },
- ]);
-});
+ status: scanStatus === "acceptance_unknown" ? "failed" : scanStatus,
+ });
+ expect(result.severityRubric).toEqual(severityRubric);
+ expect(result.execution).toMatchObject({
+ status: executionStatus,
+ error: { code: -32010, message: "Synthetic remote diagnostic" },
+ sessionId: "canceled-session",
+ usage: { input_tokens: 42 },
+ });
+ },
+);
-test("SDK cancellation waits for the executor receipt without losing known session/usage", async () => {
+test("a late cancel preserves a previously committed hosted failure", async () => {
const input = await fixture();
const controller = new AbortController();
- input.severityRubric = { ...severityRubric };
- const pending = runHostedScan(input, {
+ const run = CodexSecurity.prototype.run;
+ using _cancel = spyOn(CodexSecurity.prototype, "run").mockImplementation(
+ async function (this: CodexSecurity, ...args) {
+ try {
+ return await run.apply(this, args);
+ } finally {
+ controller.abort();
+ }
+ },
+ );
+ const result = await runHostedScan(input, {
signal: controller.signal,
executor: {
- async run(request, options) {
- expect(request.severityRubric).toEqual(severityRubric);
- Reflect.set(request.severityRubric!, "markdown", "Edited by executor.");
- controller.abort();
- expect(options.signal.aborted).toBe(true);
- await Promise.resolve();
- return receipt(request, {
- status: "canceled",
- sessionId: "canceled-session",
- usage: { input_tokens: 42 },
- });
- },
+ run: async (request) =>
+ receipt(request, { status: "failed", message: "Recorded failure" }),
},
});
- input.severityRubric.markdown = "Edited by caller.";
- const result = await pending;
- expect(result.status).toBe("canceled");
- expect(result.severityRubric).toEqual(severityRubric);
- expect(result.execution).toMatchObject({
- status: "canceled",
- sessionId: "canceled-session",
- usage: { input_tokens: 42 },
+ expect(result).toMatchObject({
+ status: "failed",
+ execution: { status: "failed", message: "Recorded failure" },
});
});
diff --git a/sdk/typescript/tests-ts/mock-scan.test.ts b/sdk/typescript/tests-ts/mock-scan.test.ts
index 8fd03f0e1..b1c432429 100644
--- a/sdk/typescript/tests-ts/mock-scan.test.ts
+++ b/sdk/typescript/tests-ts/mock-scan.test.ts
@@ -344,35 +344,46 @@ test("mock scans bind clean Git, committed diff, and working-tree snapshots", as
}
});
-test("aborting mock generation leaves a terminal scan record", async () => {
- const { client, repository, environment, python } = await fixture();
- const controller = new AbortController();
- const onLifecycle = mock<NonNullable<ScanOptions["onLifecycle"]>>();
- try {
- await expect(
- client.run(repository, {
- mock: true,
- signal: controller.signal,
- onLifecycle,
- onScanStarted: () => controller.abort(),
- }),
- ).rejects.toThrow(ScanInterruptedError);
- const history = await runWorkbench(
- { python, pluginRoot: PLUGIN_ROOT, environment },
- ["list-scans", "--repository", repository],
- );
- expect(history["scans"]).toMatchObject([
- { progress: { status: "failed" } },
- ]);
- const [saved] = history["scans"] as JsonObject[];
- expect(onLifecycle.mock.calls.map(([snapshot]) => snapshot.status)).toEqual(
- ["running", "failed"],
- );
- expect(saved?.["lifecycle"]).toEqual(onLifecycle.mock.lastCall?.[0]);
- } finally {
- await client.close();
- }
-});
+test.each(["abort", "close", "timeout"] as const)(
+ "stopping mock generation via %s records its terminal outcome",
+ async (stop) => {
+ const { client, repository, environment, python } = await fixture();
+ const controller = new AbortController();
+ const onLifecycle = mock<NonNullable<ScanOptions["onLifecycle"]>>();
+ try {
+ await expect(
+ client.run(repository, {
+ mock: true,
+ signal: controller.signal,
+ onLifecycle,
+ onScanStarted: () => {
+ if (stop === "close") void client.close();
+ else if (stop === "timeout")
+ controller.abort(new DOMException("deadline", "TimeoutError"));
+ else controller.abort();
+ },
+ }),
+ ).rejects.toThrow(
+ stop === "close" ? "CodexSecurity is closed" : ScanInterruptedError,
+ );
+ const terminal = stop === "timeout" ? "failed" : "canceled";
+ const history = await runWorkbench(
+ { python, pluginRoot: PLUGIN_ROOT, environment },
+ ["list-scans", "--repository", repository],
+ );
+ expect(history["scans"]).toMatchObject([
+ { progress: { status: terminal } },
+ ]);
+ const [saved] = history["scans"] as JsonObject[];
+ expect(
+ onLifecycle.mock.calls.map(([snapshot]) => snapshot.status),
+ ).toEqual(["running", terminal]);
+ expect(saved?.["lifecycle"]).toEqual(onLifecycle.mock.lastCall?.[0]);
+ } finally {
+ await client.close();
+ }
+ },
+);
test("reports committed completion after the workbench response is lost", async () => {
const { client, repository } = await fixture(async (options, args, input) => {