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!: remove legacy message and plugin compatibility (#1548)

mldangelo-oai1d54bd702da3
diff --git a/.github/workflows/native-musl.yml b/.github/workflows/native-musl.yml
index 5abe43bdd..0ef5e1e1c 100644
--- a/.github/workflows/native-musl.yml
+++ b/.github/workflows/native-musl.yml
@@ -17,14 +17,14 @@ jobs:
include:
- runner: ubuntu-24.04
arch: x64
- rust-image: rust@sha256:83b16fd204613a557527e81ef4f87fd513229187c49635e3c7f96b08dfa43c33
- node22-image: node@sha256:133cdce957f50f47236d6d926592fb1db7a120ac3c33191e611b60dfab63e324
- node20-image: node@sha256:2ffec31a58e85fbcd575c544a3584f6f4d128779e6b856153a04366b8dd01bb0
+ rust-image: public.ecr.aws/docker/library/rust@sha256:83b16fd204613a557527e81ef4f87fd513229187c49635e3c7f96b08dfa43c33
+ node22-image: public.ecr.aws/docker/library/node@sha256:133cdce957f50f47236d6d926592fb1db7a120ac3c33191e611b60dfab63e324
+ node20-image: public.ecr.aws/docker/library/node@sha256:2ffec31a58e85fbcd575c544a3584f6f4d128779e6b856153a04366b8dd01bb0
- runner: ubuntu-24.04-arm
arch: arm64
- rust-image: rust@sha256:ec97f172f73f5ec381a238dad6ddd99e0e9c14fd3709a05beb41ccdf50eb666a
- node22-image: node@sha256:9090fa64b73b14ab658d907e7916afcb1633e4bdd5134f9b1be4a21a6c56f0b8
- node20-image: node@sha256:9a3cb11e78b5082ce7d22bb5113ed5bb7ac4c20195c05f3981e765e21e31c29c
+ rust-image: public.ecr.aws/docker/library/rust@sha256:ec97f172f73f5ec381a238dad6ddd99e0e9c14fd3709a05beb41ccdf50eb666a
+ node22-image: public.ecr.aws/docker/library/node@sha256:9090fa64b73b14ab658d907e7916afcb1633e4bdd5134f9b1be4a21a6c56f0b8
+ node20-image: public.ecr.aws/docker/library/node@sha256:9a3cb11e78b5082ce7d22bb5113ed5bb7ac4c20195c05f3981e765e21e31c29c
defaults:
run:
shell: bash
diff --git a/evals/triage-finding/README.md b/evals/triage-finding/README.md
index 22c4e4fff..f057a72f4 100644
--- a/evals/triage-finding/README.md
+++ b/evals/triage-finding/README.md
@@ -57,7 +57,7 @@ The eval target is `fixtures/repo`, a small synthetic Express app with both true
- `contains-json` validates the fenced `triage-finding/v0` JSON block against `schemas/triage-result-v0.schema.json`.
- `assertions/triage-io.mts` checks input order, `input_id`, `source_type`,
- verdicts, array fields, and `$fix-finding` handoff behavior.
+ verdicts, array fields, and `$fix-finding` handoff behavior. Its shared triage parser accepts complete JSON or multiline JSON code fences, including indented and CRLF fences. Inline fences and JSON embedded in prose are not accepted.
- `tests/invocation-behavior.yaml` opts out of those default JSON assertions for the no-finding case with `options.disableDefaultAsserts: true`.
- `assertions/missing-input.mts` checks that bare invocation asks for a finding,
names supported input formats, and does not emit triage result JSON.
diff --git a/evals/triage-finding/assertions/output.mts b/evals/triage-finding/assertions/output.mts
index 29624fe9b..59d81b0c6 100644
--- a/evals/triage-finding/assertions/output.mts
+++ b/evals/triage-finding/assertions/output.mts
@@ -74,43 +74,18 @@ export function extractTriageResult(
const parsed = JSON.parse(text) as TriageResult;
if (parsed && parsed.schema_version === "triage-finding/v0") return parsed;
} catch {
- // The result may be surrounded by prose or Markdown fences.
+ // The result may be inside a multiline Markdown fence.
}
- const fencedBlocks: [number, string][] = [];
- const openingFence = /```(?:json)?\s*/gi;
- // Complete JSON strings may contain backticks, but never literal line breaks.
- const tokens = /"|```/g;
- const quoted = /"(?:\\.|[^"\\\r\n])*(")?/y;
- // Escaped quotes within an unterminated string cannot start complete strings.
- let unterminatedUntil = 0;
- let opening;
- while ((opening = openingFence.exec(text))) {
- const start = openingFence.lastIndex;
- tokens.lastIndex = start;
- let token;
- while ((token = tokens.exec(text)) && token[0] !== "```") {
- if (token.index < unterminatedUntil) continue;
- quoted.lastIndex = token.index;
- const string = quoted.exec(text)!;
- if (string[1]) tokens.lastIndex = quoted.lastIndex;
- else unterminatedUntil = quoted.lastIndex;
+ const candidates: string[] = [];
+ let fenceStart = -1;
+ for (const match of text.matchAll(/^[\t ]*```([^\r\n]*)\r?$/gm)) {
+ if (fenceStart < 0) {
+ fenceStart = match.index + match[0].length;
+ } else if (/^[\t ]*$/.test(match[1])) {
+ candidates.push(text.slice(fenceStart, match.index));
+ fenceStart = -1;
}
- if (!token) break;
- fencedBlocks.push([opening.index, text.slice(start, token.index).trim()]);
- openingFence.lastIndex = tokens.lastIndex;
}
- // Preserve legacy inline framing when malformed quoted text hides a later fence.
- for (const match of text.matchAll(/```(?:json)?\s*([\s\S]*?)```/gi)) {
- fencedBlocks.push([match.index, match[1].trim()]);
- }
- const start = text.indexOf("{");
- const end = text.lastIndexOf("}");
- const candidates =
- fencedBlocks.length > 0
- ? fencedBlocks
- .sort((left, right) => left[0] - right[0])
- .map(([, body]) => body)
- : [text.slice(start, end + 1)];
for (const candidate of candidates) {
try {
const parsed = JSON.parse(candidate) as TriageResult;
diff --git a/evals/triage-finding/sastbench/scripts/test-sastbench-result.mts b/evals/triage-finding/sastbench/scripts/test-sastbench-result.mts
index 96d9eab75..cb2bdfca3 100644
--- a/evals/triage-finding/sastbench/scripts/test-sastbench-result.mts
+++ b/evals/triage-finding/sastbench/scripts/test-sastbench-result.mts
@@ -31,13 +31,16 @@ assert.equal(
extractTriageResult("```json\ninvalid\n```\n" + fenced).findings[0].verdict,
"confirmed",
);
-assert.throws(
- () =>
- extractTriageResult(
- "```json\ninvalid\n```\n" + JSON.stringify(triageResult("confirmed")),
- ),
- { message: "Could not find a parseable triage-finding/v0 JSON result" },
-);
+for (const output of [
+ "```json\ninvalid\n```\n" + JSON.stringify(triageResult("confirmed")),
+ "Summary: " + JSON.stringify(triageResult("confirmed")),
+ "```json " + JSON.stringify(triageResult("confirmed")) + "```",
+ "```json\ntruncated output\n".repeat(16_000),
+]) {
+ assert.throws(() => extractTriageResult(output), {
+ message: "Could not find a parseable triage-finding/v0 JSON result",
+ });
+}
const truncated =
'```json\n{"schema_version":"triage-finding/v0","findings":[{"evidence":["' +
'\\"x'.repeat(32_000);
@@ -53,23 +56,15 @@ const truncatedMetrics = addSastBenchMetrics(
).result;
assert.equal(truncatedMetrics.metadata.sastbench.status, "invalid_output");
assert.equal(truncatedMetrics.namedScores.invalid_output, 1);
-const inlineSecond = triageResult("confirmed");
-inlineSecond.findings[0].evidence = [];
for (const output of [
JSON.stringify(triageResult("confirmed")),
JSON.stringify(triageResult("confirmed"), null, 2),
- '```json "invalid ``` ```json ' +
- JSON.stringify(inlineSecond) +
- "```\n```json\n" +
- JSON.stringify(triageResult("not_actionable")) +
- "\n```",
+ fenced.replace("```json", "```"),
fenced.replaceAll("\n", "\r\n"),
+ "```typescript\nconst value = 1;\n```\n\n" + fenced,
truncated + "\n```\n" + fenced,
'Prose {"other": true}\n' + fenced.replaceAll("```", " ```"),
'```json\n"invalid\n```\n' + fenced,
- 'Prose {"other": true} ```json ' +
- JSON.stringify(triageResult("confirmed")) +
- "``` trailing prose",
]) {
assert.equal(
parseCaseOutcome(output, "sastbench-000000").verdict,
diff --git a/plugins/codex-security/mcp-app/server.ts b/plugins/codex-security/mcp-app/server.ts
index f6cc923ca..1fd560d6a 100644
--- a/plugins/codex-security/mcp-app/server.ts
+++ b/plugins/codex-security/mcp-app/server.ts
@@ -7,7 +7,6 @@ import { isRecord as isJsonObject } from "./src/record.js";
import { execFile } from "node:child_process";
import { randomUUID } from "node:crypto";
import { promises as fs } from "node:fs";
-import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { promisify } from "node:util";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
@@ -39,30 +38,13 @@ import {
} from "./src/deep-scan/parent-sandbox.js";
import { WorkbenchDeepScanStore } from "./src/deep-scan/store.js";
import type { DeepScanRunState } from "./src/deep-scan/types.js";
-import {
- WORKBENCH_PYTHON,
- WORKBENCH_STATE_UNAVAILABLE_EXIT_CODE,
-} from "./src/server/workbench-process.js";
+import { WORKBENCH_PYTHON } from "./src/server/workbench-process.js";
const execFileAsync = promisify(execFile);
const CONFIGURED_SCAN_ROOT = process.env.CODEX_SECURITY_SCAN_ROOT?.trim();
-const CONFIGURED_WORKBENCH_STATE_DIR =
- process.env.CODEX_SECURITY_STATE_DIR?.trim();
const PLUGIN_ROOT =
process.env.CODEX_SECURITY_PLUGIN_ROOT || resolve(__dirname, "..");
const USER_INPUT_WAIT_TIMEOUT_MS = 14 * 60 * 1000;
-const WORKBENCH_COMMANDS_WITHOUT_DATABASE = new Set([
- "resolve-scan-root",
- "inspect-target",
- "inspect-setup",
- "save-artifact",
- "read-artifact",
-]);
-
-let fallbackWorkbenchStateDir: Promise<string> | undefined;
-let persistentWorkbenchStateSucceeded = false;
-const workbenchStateSelectionLock = new AsyncLock();
-
const userContextSchema = z.string().trim().min(1);
const editableUserContextSchema = z.string().trim();
const pathSchema = z
@@ -116,18 +98,6 @@ const daybreakEntitlementContextSchema = z.object({
});
async function scanRoot(): Promise<string> {
- if (
- !CONFIGURED_SCAN_ROOT &&
- !CONFIGURED_WORKBENCH_STATE_DIR &&
- !persistentWorkbenchStateSucceeded &&
- !fallbackWorkbenchStateDir
- ) {
- // Select the workbench state before choosing its default artifact directory.
- await runWorkbench(["list-scans", "--limit", "1"]);
- }
- if (!CONFIGURED_SCAN_ROOT && fallbackWorkbenchStateDir) {
- return join(await fallbackWorkbenchStateDir, "scans");
- }
const result = await runWorkbench([
"resolve-scan-root",
...optionalArg("--scan-root", CONFIGURED_SCAN_ROOT),
@@ -2244,7 +2214,7 @@ async function runWorkbench(
let pythonCommand: string | undefined;
try {
pythonCommand = await resolvePythonCommand();
- return await executeWorkbenchWithStateSelection(pythonCommand, args, input);
+ return await executeWorkbench(pythonCommand, args, input);
} catch (error) {
const launchError = pythonCommand
? missingPythonHelperMessage(error, pythonCommand)
@@ -2259,78 +2229,9 @@ async function runWorkbench(
}
}
-async function executeWorkbenchWithStateSelection(
- pythonCommand: string,
- args: string[],
- input?: string | Buffer,
-): Promise<JsonObject> {
- if (WORKBENCH_COMMANDS_WITHOUT_DATABASE.has(args[0] ?? "")) {
- return await executeWorkbench(pythonCommand, args, undefined, input);
- }
- if (CONFIGURED_WORKBENCH_STATE_DIR) {
- return await executeWorkbench(pythonCommand, args, undefined, input);
- }
- if (fallbackWorkbenchStateDir) {
- return await executeWorkbench(
- pythonCommand,
- args,
- await fallbackWorkbenchStateDir,
- input,
- );
- }
- if (persistentWorkbenchStateSucceeded) {
- return await executeWorkbench(pythonCommand, args, undefined, input);
- }
- return await workbenchStateSelectionLock.run(async () => {
- if (fallbackWorkbenchStateDir) {
- return await executeWorkbench(
- pythonCommand,
- args,
- await fallbackWorkbenchStateDir,
- input,
- );
- }
- if (persistentWorkbenchStateSucceeded) {
- return await executeWorkbench(pythonCommand, args, undefined, input);
- }
- try {
- const result = await executeWorkbench(
- pythonCommand,
- args,
- undefined,
- input,
- );
- persistentWorkbenchStateSucceeded = true;
- return result;
- } catch (error) {
- if (
- !isExecError(error) ||
- !("code" in error) ||
- error.code !== WORKBENCH_STATE_UNAVAILABLE_EXIT_CODE
- )
- throw error;
- const fallbackStateDir = await pinFallbackWorkbenchStateDir();
- console.error(
- JSON.stringify({
- component: "codex_security_workbench",
- event: "state_fallback_pinned",
- reason: "persistent_sqlite_unwritable",
- }),
- );
- return await executeWorkbench(
- pythonCommand,
- args,
- fallbackStateDir,
- input,
- );
- }
- });
-}
-
async function executeWorkbench(
pythonCommand: string,
args: string[],
- stateDir?: string,
input?: string | Buffer,
): Promise<JsonObject> {
const timeout = [
@@ -2367,9 +2268,7 @@ async function executeWorkbench(
{
cwd: PLUGIN_ROOT,
windowsHide: true,
- env: stateDir
- ? { ...process.env, CODEX_SECURITY_STATE_DIR: stateDir }
- : process.env,
+ 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,
@@ -2406,17 +2305,6 @@ async function executeWorkbench(
return result;
}
-async function pinFallbackWorkbenchStateDir(): Promise<string> {
- fallbackWorkbenchStateDir ??= (async () => {
- const stateDir = CONFIGURED_SCAN_ROOT
- ? join(await scanRoot(), "workbench-state")
- : await fs.mkdtemp(join(tmpdir(), "codex-security-state-"));
- await fs.mkdir(stateDir, { recursive: true, mode: 0o700 });
- return stateDir;
- })();
- return await fallbackWorkbenchStateDir;
-}
-
function workbenchScriptPath(): string {
return join(PLUGIN_ROOT, "scripts", "workbench_db.py");
}
diff --git a/plugins/codex-security/mcp-app/src/artifact-discovery.ts b/plugins/codex-security/mcp-app/src/artifact-discovery.ts
index f5a0e7202..d04a99668 100644
--- a/plugins/codex-security/mcp-app/src/artifact-discovery.ts
+++ b/plugins/codex-security/mcp-app/src/artifact-discovery.ts
@@ -162,14 +162,7 @@ async function diffCandidateSources(
const nativePath = (value: string) =>
process.platform === "win32" ? value.replaceAll("\\", "/") : value;
const paths = decodeUtf8(await readFile(inventory))
- .split("\n")
- .flatMap((row, index, lines) =>
- row.endsWith("\r") && index < lines.length - 1
- ? process.platform === "win32"
- ? [row.slice(0, -1)]
- : [row, row.slice(0, -1)]
- : [row],
- )
+ .split(/\r?\n/u)
.filter(Boolean)
.map(nativePath);
const locations = candidates.flatMap((candidate, index) => {
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 2287efe56..9ff4b924b 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
@@ -312,7 +312,12 @@ export class DeepScanCoordinator {
this.publicationAbortController.signal,
);
if (this.canceled || this.externallyFailed) return;
- const completed = await this.finishWithReplay(schedulerResult);
+ const completed = await this.options.store.finish({
+ scanId: this.state.scanId,
+ reason: schedulerResult.reason,
+ manifestPath: join(this.state.scanDir, "scan-manifest.json"),
+ omittedWorkerIds: schedulerResult.omittedWorkerIds,
+ });
if (this.canceled || this.externallyFailed) return;
this.state = completed;
this.log({
@@ -1077,43 +1082,6 @@ export class DeepScanCoordinator {
completed: count,
});
}
-
- /**
- * A workbench process can commit SQLite and still lose its stdout response.
- * Replay the exact idempotent finish once before treating the run as failed;
- * otherwise we could overwrite a successful terminal state after durable success.
- */
- private async finishWithReplay(
- result: SchedulerResult,
- ): Promise<DeepScanRunState> {
- const input = {
- scanId: this.state.scanId,
- reason: result.reason,
- manifestPath: join(this.state.scanDir, "scan-manifest.json"),
- omittedWorkerIds: result.omittedWorkerIds,
- };
- try {
- return await this.options.store.finish(input);
- } catch (firstError) {
- if (confirmedOwnershipChange(firstError, this.state.scanId))
- throw firstError;
- this.log({
- event: "coordinator_finish_replay",
- scanId: this.state.scanId,
- reason: errorKind(firstError),
- });
- try {
- return await this.options.store.finish(input);
- } catch (replayError) {
- if (confirmedOwnershipChange(replayError, this.state.scanId))
- throw replayError;
- throw new Error(
- `Deep Scan terminal persistence replay failed: ${errorMessage(replayError)}`,
- { cause: firstError },
- );
- }
- }
- }
}
const systemClock: DeepScanClock = {
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/store.ts b/plugins/codex-security/mcp-app/src/deep-scan/store.ts
index 05c1166d6..f39eb28bc 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/store.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/store.ts
@@ -598,6 +598,8 @@ export function isTransientPersistenceError(error: unknown): boolean {
}
for (const record of persistenceErrorRecords(error)) {
+ // A committed workbench mutation can lose or truncate its JSON stdout.
+ if (record.name === "SyntaxError") return true;
if (
typeof record.code === "string" &&
/^(?:SQLITE_BUSY(?:_[A-Z]+)?|SQLITE_LOCKED(?:_[A-Z]+)?|SQLITE_IOERR(?:_[A-Z]+)?|EAGAIN|EBUSY|EINTR|ETIMEDOUT|ETIME)$/i.test(
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/worker-runner.ts b/plugins/codex-security/mcp-app/src/deep-scan/worker-runner.ts
index 84d952647..a3125b771 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/worker-runner.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/worker-runner.ts
@@ -207,11 +207,7 @@ export class DeepScanWorkerRunner {
};
let persisted: PersistedDeepScanWorker;
try {
- persisted = await this.replayStoreMutation(
- "discovery_acceptance_replay",
- workerId,
- async () => await this.options.store.updateWorker(acceptance),
- );
+ persisted = await this.options.store.updateWorker(acceptance);
} catch (error) {
if (confirmedOwnershipChange(error, run.scanId)) throw error;
if (!this.options.signal.aborted) throw error;
@@ -344,11 +340,7 @@ export class DeepScanWorkerRunner {
newFindings: reducerValidation.newFindings,
resultManifestPath: resultPath,
};
- const committed = await this.replayStoreMutation(
- "dedup_commit_replay",
- reducerId,
- async () => await this.options.store.commitDedup(commit),
- );
+ const committed = await this.options.store.commitDedup(commit);
this.options.log({
event: "dedup_committed",
scanId: run.scanId,
@@ -606,36 +598,6 @@ export class DeepScanWorkerRunner {
});
}
- /** Replay idempotent SQLite commits when their process response is ambiguous. */
- private async replayStoreMutation<T>(
- event: string,
- workerId: string,
- operation: () => Promise<T>,
- ): Promise<T> {
- try {
- return await operation();
- } catch (firstError) {
- if (confirmedOwnershipChange(firstError, this.options.run.scanId))
- throw firstError;
- this.options.log({
- event,
- scanId: this.options.run.scanId,
- workerId,
- reason: errorNameWithCode(asError(firstError)),
- });
- try {
- return await operation();
- } catch (replayError) {
- if (confirmedOwnershipChange(replayError, this.options.run.scanId))
- throw replayError;
- throw new Error(
- `Deep Scan persistence replay failed: ${asError(replayError).message}`,
- { cause: firstError },
- );
- }
- }
- }
-
private async cancelAttempt(
input: {
workerId: string;
diff --git a/plugins/codex-security/mcp-app/src/helpers/normalize-candidates.ts b/plugins/codex-security/mcp-app/src/helpers/normalize-candidates.ts
index b4d8d8249..731458293 100644
--- a/plugins/codex-security/mcp-app/src/helpers/normalize-candidates.ts
+++ b/plugins/codex-security/mcp-app/src/helpers/normalize-candidates.ts
@@ -149,65 +149,8 @@ function readScope(
sources?: ReadonlyMap<string, CandidateSource>,
): Set<string> {
const contents = decodeUtf8(readFile(path));
- const lines = contents.split("\n");
- const listedRows = new Set(lines);
- const isScopeFile = (value: string): boolean => {
- try {
- relativeFile(value, root, sources);
- return true;
- } catch {
- return false;
- }
- };
- const carriageRows = new Map<string, [boolean, boolean]>();
- if (!windows) {
- for (const line of lines) {
- if (line.endsWith("\r") && line !== "\r") {
- carriageRows.set(line, [
- isScopeFile(line),
- isScopeFile(line.slice(0, -1)),
- ]);
- }
- }
- }
- const crlfEvidence = lines.slice(0, -1).some((line) => {
- if (line === "\r") return true;
- const paths = carriageRows.get(line);
- return paths !== undefined && paths[1] && !paths[0];
- });
- const literalEvidence = [...carriageRows.values()].some(
- ([literal, stripped]) => literal && !stripped,
- );
const scope = new Set<string>();
- for (const [index, row] of lines.entries()) {
- let line = row;
- const finalLiteral = index === lines.length - 1;
- if (!finalLiteral && (windows || line === "\r")) {
- if (line.endsWith("\r")) line = line.slice(0, -1);
- } else if (!finalLiteral && line.endsWith("\r")) {
- const [literal, stripped] = carriageRows.get(line)!;
- const trimmed = line.slice(0, -1);
- if (allowMissing && literal && !stripped && !listedRows.has(trimmed)) {
- throw new Error(
- `in-scope file row ${index + 1}: ambiguous carriage-return paths`,
- );
- }
- if (stripped && !literal) {
- line = trimmed;
- } else if (stripped && literal) {
- if (!listedRows.has(trimmed)) {
- if (crlfEvidence && !literalEvidence) {
- line = trimmed;
- } else if (!literalEvidence || crlfEvidence) {
- throw new Error(
- `in-scope file row ${index + 1}: ambiguous carriage-return paths`,
- );
- }
- }
- } else if (!literal && crlfEvidence) {
- line = trimmed;
- }
- }
+ for (const [index, line] of contents.split(/\r?\n/u).entries()) {
if (line === "") continue;
try {
scope.add(relativeFile(line, root, sources)[0]);
diff --git a/plugins/codex-security/mcp-app/src/server/workbench-process.ts b/plugins/codex-security/mcp-app/src/server/workbench-process.ts
index 813cf6984..455a3ffd8 100644
--- a/plugins/codex-security/mcp-app/src/server/workbench-process.ts
+++ b/plugins/codex-security/mcp-app/src/server/workbench-process.ts
@@ -1,16 +1,8 @@
-// This exit status belongs only to the private MCP launcher, not the Python CLI.
-export const WORKBENCH_STATE_UNAVAILABLE_EXIT_CODE = 73;
export const WORKBENCH_PYTHON = `
-import json, sys, traceback
+import json, sys
from pathlib import Path
sys.path.insert(0, str(Path(sys.argv.pop(1)).resolve().parent))
import workbench_db
sys.argv = [workbench_db.__file__, *json.loads(sys.stdin.buffer.readline())]
-try:
- workbench_db.main()
-except Exception as error:
- if not getattr(error, "_codex_security_state_unavailable", False):
- raise
- traceback.print_exc()
- sys.exit(${WORKBENCH_STATE_UNAVAILABLE_EXIT_CODE})
+workbench_db.main()
`;
diff --git a/plugins/codex-security/mcp-app/src/workbench/database.ts b/plugins/codex-security/mcp-app/src/workbench/database.ts
index 3fd5ccdce..49bcf82e4 100644
--- a/plugins/codex-security/mcp-app/src/workbench/database.ts
+++ b/plugins/codex-security/mcp-app/src/workbench/database.ts
@@ -21,28 +21,6 @@ export function requireSqliteText(
throw new TypeError("SQLite text keys must contain valid Unicode.");
}
-export function timestampOrder(
- database: DatabaseSync,
- column: string,
- direction: "ASC" | "DESC" = "ASC",
-): string {
- database.function("timestamp_seconds", { deterministic: true }, (value) => {
- const timestamp = value as string;
- const zone = timestamp.slice(-6);
- const minutes = /[Zz]$/u.test(timestamp)
- ? 0
- : (Number(zone.slice(1, 3)) * 60 + Number(zone.slice(4))) *
- (zone[0] === "-" ? -1 : 1);
- // Keep Python's accepted offset arithmetic separate from date parsing.
- return Date.parse(timestamp.slice(0, 19) + "Z") - minutes * 60_000;
- });
- // Fractional digits retain their precision; trailing zeros denote the same instant.
- database.function("timestamp_fraction", { deterministic: true }, (value) =>
- ((value as string).match(/\.(\d+)/u)?.[1] ?? "").replace(/0+$/u, ""),
- );
- return `timestamp_seconds(${column}) ${direction}, timestamp_fraction(${column}) ${direction}`;
-}
-
function createStateDirectory(path: string): void {
if (process.platform !== "win32") path = path.replace(/\/+$/u, "") || "/";
const nativePath =
diff --git a/plugins/codex-security/mcp-app/src/workbench/duplicates.ts b/plugins/codex-security/mcp-app/src/workbench/duplicates.ts
index 6ada81bb1..3dd719d2a 100644
--- a/plugins/codex-security/mcp-app/src/workbench/duplicates.ts
+++ b/plugins/codex-security/mcp-app/src/workbench/duplicates.ts
@@ -3,7 +3,7 @@ import { isDeepStrictEqual } from "node:util";
import type { DatabaseSync } from "node:sqlite";
import { parseJson } from "../helpers/json";
import { transaction } from "./transaction";
-import { requireSqliteText, timestampOrder } from "./database";
+import { requireSqliteText } from "./database";
export interface DedupeGroup {
groupId: string;
@@ -63,7 +63,7 @@ export function findPotentialDuplicates(
JOIN findings ON findings.id = embeddings.finding_id
WHERE ${predicate}embeddings.model = (SELECT model FROM ${table} WHERE finding_id = ?)
AND embeddings.finding_id != ?
- ORDER BY ${timestampOrder(database, "findings.created_at")}, findings.id`,
+ ORDER BY julianday(upper(findings.created_at)), findings.id`,
);
const ranked: { id: string; similarity: number }[] = [];
try {
@@ -242,7 +242,7 @@ export function listDedupeGroups(database: DatabaseSync, findingId: string) {
JOIN finding_dedupe_groups AS groups ON groups.id = matched.group_id
JOIN finding_dedupe_group_members AS members ON members.group_id = groups.id
WHERE matched.finding_id = ?
- GROUP BY groups.id ORDER BY ${timestampOrder(database, "groups.created_at")}, groups.id
+ GROUP BY groups.id ORDER BY julianday(upper(groups.created_at)), groups.id
`);
return {
groups: rows
diff --git a/plugins/codex-security/mcp-app/src/workbench/findings.ts b/plugins/codex-security/mcp-app/src/workbench/findings.ts
index ed785d94a..249e8ea64 100644
--- a/plugins/codex-security/mcp-app/src/workbench/findings.ts
+++ b/plugins/codex-security/mcp-app/src/workbench/findings.ts
@@ -1,7 +1,7 @@
import type { DatabaseSync } from "node:sqlite";
import { isDeepStrictEqual } from "node:util";
import { parseJson, stringifyJson } from "../helpers/json";
-import { requireSqliteText, timestampOrder } from "./database";
+import { requireSqliteText } from "./database";
import { transaction } from "./transaction";
export interface Finding {
@@ -127,7 +127,7 @@ export function listStoredFindings(
const rows = database
.prepare(
`SELECT details_json FROM findings WHERE details_json IS NOT NULL
- ORDER BY ${timestampOrder(database, "created_at")}, id LIMIT ? OFFSET ?`,
+ ORDER BY julianday(upper(created_at)), id LIMIT ? OFFSET ?`,
)
.all(limit, offset);
const nextOffset = offset + rows.length;
diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_coordinator_fixture.ts b/plugins/codex-security/mcp-app/tests/deep_scan_coordinator_fixture.ts
index 0daa7aeb3..0998f4885 100644
--- a/plugins/codex-security/mcp-app/tests/deep_scan_coordinator_fixture.ts
+++ b/plugins/codex-security/mcp-app/tests/deep_scan_coordinator_fixture.ts
@@ -115,9 +115,6 @@ export class FakeStore {
rejectFailurePersistence = false;
replacementManifestBeforeFinishRejection: string | undefined = undefined;
replacementCandidatesBeforeDedupRejection: string | undefined = undefined;
- loseFirstFinishResponseAfterCommit = false;
- loseFirstDiscoveryAcceptanceResponseAfterCommit = false;
- loseFirstDedupCommitResponseAfterCommit = false;
dedupCommitResponseGate?: PromiseWithResolvers<void>;
blockDiscoveryUpdate?: "failed" | "succeeded";
discoveryBlocked = Promise.withResolvers<void>();
@@ -219,16 +216,6 @@ export class FakeStore {
}
persisted.consecutiveErrors = this.run.consecutiveErrors;
this.workers.set(update.id, persisted);
- if (
- this.loseFirstDiscoveryAcceptanceResponseAfterCommit &&
- update.kind === "discovery" &&
- update.status === "succeeded"
- ) {
- this.loseFirstDiscoveryAcceptanceResponseAfterCommit = false;
- throw new Error(
- "fixture lost discovery acceptance response after commit",
- );
- }
return structuredClone(persisted);
}
@@ -297,10 +284,6 @@ export class FakeStore {
}
if (this.dedupCommitResponseGate)
await this.dedupCommitResponseGate.promise;
- if (this.loseFirstDedupCommitResponseAfterCommit) {
- this.loseFirstDedupCommitResponseAfterCommit = false;
- throw new Error("fixture lost dedup commit response after commit");
- }
return structuredClone(this.run);
}
@@ -343,12 +326,6 @@ export class FakeStore {
this.run.status = "succeeded";
this.run.terminalReason = input.reason;
this.run.manifestPath = input.manifestPath;
- if (
- this.loseFirstFinishResponseAfterCommit &&
- this.finishCalls.length === 1
- ) {
- throw new Error("fixture lost finish response after commit");
- }
return structuredClone(this.run);
}
diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_storage.ts b/plugins/codex-security/mcp-app/tests/test_artifact_storage.ts
index 157b68714..3c2c3bc0d 100644
--- a/plugins/codex-security/mcp-app/tests/test_artifact_storage.ts
+++ b/plugins/codex-security/mcp-app/tests/test_artifact_storage.ts
@@ -455,6 +455,7 @@ try {
await mkdir(defaultState, { recursive: true });
await chmod(defaultState, 0o500);
try {
+ const repositoryEntries = await readdir(repository);
await client.close();
client = await connect({
CODEX_HOME: codexHome,
@@ -469,16 +470,15 @@ try {
path: "artifacts/standalone.md",
content,
}),
- /Artifact storage must be outside the target repository/,
- );
- const fallbackStates = (await readdir(repository)).filter((name) =>
- name.startsWith("codex-security-state-"),
- );
- assert.equal(fallbackStates.length, 1);
- await assert.rejects(
- stat(path.join(repository, fallbackStates[0], "scans")),
- { code: "ENOENT" },
+ (error: unknown) => {
+ assert.ok(error instanceof Error);
+ assert.match(error.message, /EACCES: permission denied, mkdir/);
+ assert.ok(error.message.includes(path.join(defaultState, "scans")));
+ return true;
+ },
);
+ assert.deepEqual(await readdir(defaultState), []);
+ assert.deepEqual(await readdir(repository), repositoryEntries);
} finally {
await chmod(defaultState, 0o700);
}
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
index f345a3dbb..ebe641a9b 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
@@ -1065,31 +1065,6 @@ async function testFinishPersistenceFailureRewritesManifestAsFailure() {
assert.equal(terminal.manifestPath, undefined);
}
-async function testLostFinishResponseReplaysWithoutOverwritingSuccessManifest() {
- const { fixture, store } = await coordinatorFixture();
- store.loseFirstFinishResponseAfterCommit = true;
- const terminal = await runCoordinator(fixture, store, new FakeExecutor());
- assert.equal(terminal?.status, "succeeded");
- assert.equal(store.finishCalls.length, 2);
- assert.deepEqual(store.finishCalls[1], store.finishCalls[0]);
- assert.equal(store.failureMessages.length, 0);
- const manifest = await readJson(terminal.manifestPath);
- assert.equal(manifest.scan.scanId, fixture.run.scanId);
-}
-
-async function testLostWorkerCommitResponsesReplayIdempotently() {
- const { fixture, store } = await coordinatorFixture();
- store.loseFirstDiscoveryAcceptanceResponseAfterCommit = true;
- store.loseFirstDedupCommitResponseAfterCommit = true;
- const terminal = await runCoordinator(fixture, store, new FakeExecutor());
- assert.equal(terminal?.status, "succeeded");
- assert.equal(store.loseFirstDiscoveryAcceptanceResponseAfterCommit, false);
- assert.equal(store.loseFirstDedupCommitResponseAfterCommit, false);
- assert.equal(store.dedupCommitCalls.length, 2);
- assert.equal(store.dedupCommits.length, 1);
- assert.equal(store.failureMessages.length, 0);
-}
-
async function testCommittedReducerIsReconciledBeforeDiscoveryFailureManifest() {
const { fixture, store } = await coordinatorFixture({
workers: 3,
@@ -2589,7 +2564,7 @@ async function testStoreConfirmationSurvivesReplayAndThreadMetadata() {
case "upsert-deep-scan-worker":
mutations += 1;
if (operation !== "thread-started" && mutations === 1)
- throw new Error("Synthetic initial persistence failure.");
+ throw new Error("sqlite3.OperationalError: database is locked");
if (replaced) current = { ...current, coordinatorGeneration: 3 };
throw new Error(diagnostic);
case "get-deep-scan": {
@@ -4190,8 +4165,6 @@ try {
await testConfigurationFailureDoesNotRetry("Request blocked by cyberPolicy.");
await testFailureManifestWriteDoesNotMaskOriginalError();
await testFinishPersistenceFailureRewritesManifestAsFailure();
- await testLostFinishResponseReplaysWithoutOverwritingSuccessManifest();
- await testLostWorkerCommitResponsesReplayIdempotently();
await testCommittedReducerIsReconciledBeforeDiscoveryFailureManifest();
await testLongWorkerErrorIsBoundedOnlyAtPersistenceBoundary();
await testDiscoveryPhasePersistenceFailureStopsDispatch();
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 157b2b289..7cc9d0fb8 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
@@ -18,6 +18,7 @@ import {
mkdir,
readFile,
realpath,
+ stat,
symlink,
utimes,
writeFile,
@@ -2534,10 +2535,13 @@ export { recordCodexSecurityDeepReduction } from "./src/artifact-deep-reducer.ts
loader: { ".md": "text" },
});
await withWorkerFixture(async (fixture) => {
- const saved = ["CODEX_HOME", "FAKE_CODEX_ARTIFACT_EVENT"].map(
- (name) => [name, process.env[name]] as const,
- );
+ const saved = [
+ "CODEX_HOME",
+ "CODEX_SECURITY_STATE_DIR",
+ "FAKE_CODEX_ARTIFACT_EVENT",
+ ].map((name) => [name, process.env[name]] as const);
process.env.CODEX_HOME = fixture.root;
+ process.env.CODEX_SECURITY_STATE_DIR = path.join(fixture.root, "state");
try {
const targetPath = path.join(fixture.root, "target");
await mkdir(targetPath);
@@ -2550,7 +2554,6 @@ export { recordCodexSecurityDeepReduction } from "./src/artifact-deep-reducer.ts
executeWorkbench(
process.env.PYTHON?.trim() || "python3",
args,
- path.join(fixture.root, "state"),
input,
),
);
@@ -2559,6 +2562,11 @@ export { recordCodexSecurityDeepReduction } from "./src/artifact-deep-reducer.ts
threadId: "diagnostic-owner",
scanRoot: path.join(fixture.root, "scans"),
});
+ assert.ok(
+ (
+ await stat(path.join(fixture.root, "state", "workbench.sqlite3"))
+ ).isFile(),
+ );
const { run } = await store.claimCoordinator({
scanId: started.scanId,
threadId: "diagnostic-owner",
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 135025ee3..50e907b93 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
@@ -9,6 +9,11 @@ import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
import path from "node:path";
import { promisify } from "node:util";
import { importModule } from "./import-module.ts";
+import type { ScanDraftInput } from "../src/artifact-scan-draft.js";
+import {
+ DeepScanCoordinator,
+ FakeExecutor,
+} from "./deep_scan_coordinator_fixture.ts";
const execFileAsync = promisify(execFile);
const mcpAppRoot = path.resolve(import.meta.dirname, "..");
@@ -35,6 +40,149 @@ await testLateParentDraftPreservesCheckpointWithoutOverwritingTerminalSeal();
await testRecoveredPublicationRejectsLateFailure();
await testNoopStoppedRefreshRetainsPublicationFailure();
await testConcurrentParentDraftsPreserveBothCheckpoints();
+await testCoordinatorCommitResponseRecovery();
+await testCoordinatorCommitResponseRecovery("commit-deep-scan-dedup");
+await testCoordinatorCommitResponseRecovery("finish-deep-scan");
+
+async function testCoordinatorCommitResponseRecovery(
+ exhaustOperation?: "commit-deep-scan-dedup" | "finish-deep-scan",
+) {
+ const { fixtureRoot, targetPath, environment } = await createWorkbenchFixture(
+ "deep-scan-commit-response-",
+ );
+ const exhaustCommitRetries = exhaustOperation === "commit-deep-scan-dedup";
+ const runWorkbench = createWorkbenchRunner(environment);
+ const attempts = new Map<string, string[][]>();
+ let failureWrites = 0;
+ let committedManifest: string | undefined;
+ const store = new WorkbenchDeepScanStore(async (args: string[]) => {
+ const result = await runWorkbench(args);
+ if (args[0] === "fail-deep-scan") failureWrites += 1;
+ if (args[0] === "finish-deep-scan") {
+ committedManifest ??= await readFile(
+ args[args.indexOf("--manifest-path") + 1],
+ "utf8",
+ );
+ }
+ if (
+ args[0] !== "commit-deep-scan-dedup" &&
+ args[0] !== "finish-deep-scan" &&
+ !(
+ args[0] === "upsert-deep-scan-worker" &&
+ args[args.indexOf("--kind") + 1] === "discovery" &&
+ args[args.indexOf("--status") + 1] === "succeeded"
+ )
+ )
+ return result;
+ const calls = attempts.get(args[0]) ?? [];
+ calls.push([...args]);
+ attempts.set(args[0], calls);
+ if (
+ args[0] === "commit-deep-scan-dedup" &&
+ (calls.length === 1 || exhaustCommitRetries)
+ ) {
+ throw Object.assign(
+ new Error("workbench response timed out after commit"),
+ {
+ code: "ETIMEDOUT",
+ },
+ );
+ }
+ // Reproduce truncated stdout after the actual SQLite mutation committed.
+ if (calls.length === 1 || args[0] === exhaustOperation)
+ return JSON.parse('{"deepScan":');
+ return result;
+ });
+ try {
+ await mkdir(path.join(environment.CODEX_HOME, "codex-security"), {
+ recursive: true,
+ });
+ await writeFile(
+ path.join(environment.CODEX_HOME, "codex-security", "config.toml"),
+ "[deep_scan]\nworkers = 1\nstop_after_no_new = 1\nmax_discovery_runs = 1\n",
+ );
+ const threadId = "commit-response-owner";
+ const run = await store.begin({
+ targetPath,
+ threadId,
+ scanRoot: path.join(fixtureRoot, "scans"),
+ });
+ const claim = await store.claimCoordinator({
+ scanId: run.scanId,
+ threadId,
+ });
+ const coordinator = new DeepScanCoordinator({
+ run: claim.run,
+ store,
+ pluginRoot,
+ threadId,
+ executor: new FakeExecutor(),
+ heartbeatIntervalMs: 60_000,
+ onComplete: async (draft: ScanDraftInput, signal: AbortSignal) => {
+ const context = await createScanArtifactContext(
+ run.scanId,
+ runWorkbench,
+ {
+ requireRunning: true,
+ pluginRoot,
+ },
+ );
+ await recordCodexSecurityScanDraftViaWorkbench(
+ context,
+ draft,
+ runWorkbench,
+ signal,
+ );
+ },
+ });
+ coordinator.start();
+ const terminal = await coordinator.wait(undefined, 30_000);
+ assert.equal(
+ terminal?.status,
+ exhaustCommitRetries ? "failed" : "succeeded",
+ terminal?.error,
+ );
+ assert.equal(failureWrites, exhaustCommitRetries ? 1 : 0);
+ assert.equal(
+ attempts.get("commit-deep-scan-dedup")?.length,
+ exhaustCommitRetries ? 3 : 2,
+ );
+ assert.equal(attempts.get("upsert-deep-scan-worker")?.length, 2);
+ assert.equal(
+ attempts.get("finish-deep-scan")?.length,
+ exhaustCommitRetries
+ ? undefined
+ : exhaustOperation === "finish-deep-scan"
+ ? 3
+ : 2,
+ );
+ for (const calls of attempts.values()) {
+ for (const call of calls) assert.deepEqual(call, calls[0]);
+ }
+ const persisted = await store.get(run.scanId, threadId);
+ assert.equal(persisted.status, terminal.status);
+ assert.equal(
+ persisted.noNewStreak,
+ 1,
+ "replayed commits must not double-count discovery",
+ );
+ assert.equal(
+ persisted.persistedWorkers.filter(
+ (worker: { status: string }) => worker.status === "succeeded",
+ ).length,
+ 2,
+ );
+ if (!exhaustCommitRetries) {
+ assert.equal(
+ await readFile(terminal.manifestPath, "utf8"),
+ committedManifest,
+ "lost finish replies must not overwrite the published manifest",
+ );
+ }
+ } finally {
+ await rm(fixtureRoot, { recursive: true, force: true });
+ }
+}
async function createWorkbenchFixture(prefix: string) {
const fixtureRoot = await temporaryDirectory(prefix);
diff --git a/plugins/codex-security/mcp-app/tests/test_normalize_candidates.ts b/plugins/codex-security/mcp-app/tests/test_normalize_candidates.ts
index cec244078..4a127a770 100644
--- a/plugins/codex-security/mcp-app/tests/test_normalize_candidates.ts
+++ b/plugins/codex-security/mcp-app/tests/test_normalize_candidates.ts
@@ -11,10 +11,10 @@ const { normalizeCandidatesCommand } = await importSource(
const cases = [
{
- name: "preserves a literal trailing carriage return",
- files: ["file\r"],
- inventory: "file\r\n",
- candidate: "file\r",
+ name: "accepts LF inventories and preserves filename whitespace",
+ files: [" file.ts", "other.ts"],
+ inventory: "\n file.ts\nother.ts\n",
+ candidate: " file.ts",
},
{
name: "accepts CRLF inventories",
@@ -23,84 +23,29 @@ const cases = [
candidate: "file",
},
{
- name: "uses independent CRLF evidence when filenames collide",
- files: ["file", "file\r", "other"],
- inventory: "file\r\nother\r\n",
- candidate: "file",
- },
- {
- name: "preserves separately listed carriage-return filenames",
- files: ["file", "file\r"],
- inventory: "file\nfile\r\n",
- candidate: "file\r",
- },
- {
- name: "preserves an unterminated final literal filename",
- files: ["file", "file\r", "other"],
- inventory: "other\r\nfile\r",
- candidate: "file\r",
- },
- {
- name: "uses independent literal filename evidence",
- files: ["file", "file\r", "literal\r"],
- inventory: "file\r\nliteral\r\n",
- candidate: "file\r",
- },
- {
- name: "does not strip an unterminated missing literal filename",
- files: ["file"],
- inventory: "file\r",
+ name: "accepts an unterminated final row",
+ files: ["file", "other"],
+ inventory: "other\nfile",
candidate: "file",
- error: /ENOENT/,
},
{
- name: "keeps an unterminated missing literal outside a diff scope",
+ name: "keeps missing diff-scope files with CRLF inventories",
files: ["file"],
- inventory: "file\r",
+ inventory: "file\r\ndeleted\r\n",
candidate: "file",
allowMissing: true,
- error: /expected at least one in-scope file/,
- },
- {
- name: "does not infer CRLF from an unterminated row",
- files: ["file", "file\r", "other"],
- inventory: "file\r\nother\r",
- candidate: "file",
- error: /ambiguous carriage-return paths/,
},
{
- name: "rejects an ambiguous inventory without selecting another file",
- files: ["file", "file\r"],
- inventory: "file\r\n",
- candidate: "file",
- error: /ambiguous carriage-return paths/,
- },
- {
- name: "keeps CRLF normalization for missing diff-scope files",
+ name: "rejects a missing file outside diff mode",
files: ["file"],
- inventory: "file\r\ndeleted\r\n",
+ inventory: "deleted\nfile\n",
candidate: "file",
- allowMissing: true,
- },
- {
- name: "does not replace a missing CRLF path with its literal sibling",
- files: ["file\r", "other"],
- inventory: "file\r\nother\r\n",
- candidate: "file\r",
- allowMissing: true,
- error: /ambiguous carriage-return paths/,
- },
- {
- name: "preserves explicitly listed literal siblings in a diff scope",
- files: ["file\r"],
- inventory: "file\nfile\r\n",
- candidate: "file\r",
- allowMissing: true,
+ error: /ENOENT/,
},
];
for (const fixture of cases) {
- test(fixture.name, { skip: process.platform === "win32" }, async (t) => {
+ test(fixture.name, async (t) => {
const root = await temporaryDirectory("normalize-candidates-");
t.after(() => rm(root, { recursive: true, force: true }));
const repo = join(root, "repo");
diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_findings.ts b/plugins/codex-security/mcp-app/tests/test_workbench_findings.ts
index 277a2ba96..66cc381b9 100644
--- a/plugins/codex-security/mcp-app/tests/test_workbench_findings.ts
+++ b/plugins/codex-security/mcp-app/tests/test_workbench_findings.ts
@@ -204,34 +204,15 @@ test("import batches preserve identity, repository memberships and stable pages"
assert.equal(listStoredFindings(database, { limit: 10, offset: 0 }).total, 2);
});
-test("historical timestamp spellings retain exact chronology, page boundaries, and ties", (t) => {
+test("timestamp ordering keeps time zones, page boundaries, and stable ties", (t) => {
const database = open(t);
const timestamps = [
- ["microsecond", "2026-10-08T01:00:00.123456Z"],
- ["zero-b", "2026-10-08t01:00:00.000000000z"],
- ["nanosecond", "2026-10-08T01:00:00.123456001Z"],
- ["bare", "2026-10-08T01:00:00Z"],
- ["offset", "2026-10-08T03:00:00.123456000+02:00"],
- ["offset-overflow", "2026-10-08T03:00:00.123456+01:60"],
- ["native", "2026-10-08T01:00:00.123Z"],
- ["zero-a", "2026-10-08T01:00:00.000Z"],
- ["later", "2026-10-08T01:00:01Z"],
- ["upper-year", "9999-12-31T23:59:59.999999999-23:59"],
- ["lower-year", "0001-01-01T00:00:00.000000001+23:59"],
- ];
- const expected = [
- "lower-year",
- "bare",
- "zero-a",
- "zero-b",
- "native",
- "microsecond",
- "offset",
- "offset-overflow",
- "nanosecond",
- "later",
- "upper-year",
+ ["middle-b", "2026-10-08t01:00:01.123z"],
+ ["first", "2026-10-08T03:00:00+02:00"],
+ ["last", "2026-10-08T01:00:02.000001Z"],
+ ["middle-a", "2026-10-08T03:00:01.123000+02:00"],
];
+ const expected = ["first", "middle-a", "middle-b", "last"];
for (const [id, timestamp] of timestamps) {
storeFindings(database, [entry(id)], timestamp, "repository");
}
@@ -240,14 +221,9 @@ test("historical timestamp spellings retain exact chronology, page boundaries, a
.prepare("INSERT INTO finding_dedupe_groups VALUES (?, ?)")
.run(id, timestamp);
database
- .prepare("INSERT INTO finding_dedupe_group_members VALUES (?, 'native')")
+ .prepare("INSERT INTO finding_dedupe_group_members VALUES (?, 'first')")
.run(id);
}
- const before = database
- .prepare(
- "SELECT id, created_at, updated_at, details_json FROM findings ORDER BY id",
- )
- .all();
assert.deepEqual(
expected.flatMap((_, offset) =>
listStoredFindings(database, { limit: 1, offset }).findings.map(
@@ -257,27 +233,19 @@ test("historical timestamp spellings retain exact chronology, page boundaries, a
expected,
);
assert.deepEqual(
- listDedupeGroups(database, "native").groups.map((group) => group.groupId),
+ listDedupeGroups(database, "first").groups.map((group) => group.groupId),
expected,
);
for (const repository of [undefined, "repository"]) {
- const result = findPotentialDuplicates(database, "native", repository);
+ const result = findPotentialDuplicates(database, "first", repository);
assert.ok(result.potentialDuplicates);
assert.deepEqual(
result.potentialDuplicates.map(
(finding) => (finding as Findings.Finding).findingId,
),
- expected.filter((id) => id !== "native"),
+ expected.slice(1),
);
}
- assert.deepEqual(
- database
- .prepare(
- "SELECT id, created_at, updated_at, details_json FROM findings ORDER BY id",
- )
- .all(),
- before,
- );
});
test("mixed writers retain unchanged Python embeddings and replace supplied Node embeddings", async (t) => {
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 52b8c017a..750a0db30 100644
--- a/plugins/codex-security/mcp-app/tests/test_workbench_process.ts
+++ b/plugins/codex-security/mcp-app/tests/test_workbench_process.ts
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
import { test } from "node:test";
import { randomUUID } from "node:crypto";
import path from "node:path";
-import { mkdir, rm } from "node:fs/promises";
+import { mkdir, rm, stat } from "node:fs/promises";
import { temporaryDirectory } from "./support/temporary-directories.ts";
import { loadWorkbenchProcess } from "./support/workbench-process.ts";
import { PassThrough } from "node:stream";
@@ -24,7 +24,6 @@ interface WorkbenchModule {
executeWorkbench(
python: string,
args: string[],
- stateDir?: string,
input?: string | Buffer,
): Promise<Record<string, unknown>>;
}
@@ -94,7 +93,10 @@ try {
const { executeWorkbench } = await loadWorkbenchProcess();
const root = await temporaryDirectory("workbench-framing-", true);
+const stateDir = path.join(root, "state");
+const previousStateDir = process.env.CODEX_SECURITY_STATE_DIR;
try {
+ process.env.CODEX_SECURITY_STATE_DIR = stateDir;
const target = path.join(root, "target");
await mkdir(target);
const title = "--message\0café\n日本語😀high\ud800low\udfff";
@@ -116,12 +118,15 @@ try {
? "--user-context-stdin"
: `--user-context=${input.toString("utf8")}`,
],
- path.join(root, "state"),
stdin ? input : undefined,
);
assert.equal(result.targetTitle, Buffer.from(title).toString("utf8"));
assert.equal(result.userContext, input.toString("utf8"));
}
+ assert.equal(
+ (await stat(path.join(stateDir, "workbench.sqlite3"))).isFile(),
+ true,
+ );
const artifactRoot = path.join(root, "artifacts");
await mkdir(artifactRoot, { mode: 0o700 });
const binary = Buffer.from([0xff, 0x00, 0x0a, 0x0d, 0xfe]);
@@ -134,7 +139,6 @@ try {
await executeWorkbench(
process.env.PYTHON?.trim() || "python3",
["save-artifact", ...artifactArgs],
- undefined,
binary,
);
const saved = await executeWorkbench(
@@ -166,5 +170,8 @@ try {
);
}
} 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 });
}
diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_state.ts b/plugins/codex-security/mcp-app/tests/test_workbench_state.ts
new file mode 100644
index 000000000..32dfd5560
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/test_workbench_state.ts
@@ -0,0 +1,142 @@
+import { assertNoError } from "./assertions.ts";
+import { WORKBENCH_PYTHON } from "../src/server/workbench-process.ts";
+import assert from "node:assert/strict";
+import { execFileSync, spawnSync } from "node:child_process";
+import { randomUUID } from "node:crypto";
+import {
+ mkdir,
+ mkdtemp,
+ readFile,
+ realpath,
+ rm,
+ stat,
+ writeFile,
+} from "node:fs/promises";
+import { tmpdir } from "node:os";
+import path from "node:path";
+
+import { applicationRoot as mcpAppRoot, buildServer } from "./build-server.ts";
+import { startRpcServer } from "./support/rpc-server.ts";
+
+const pluginRoot = path.resolve(mcpAppRoot, "..");
+const fixtureRoot = await realpath(
+ await mkdtemp(path.join(tmpdir(), "codex-security-workbench-state-")),
+);
+const targetPath = path.join(fixtureRoot, "target");
+const serverBundlePath = path.join(
+ pluginRoot,
+ "mcp",
+ `.state-test-${randomUUID()}.cjs`,
+);
+const script = path.join(pluginRoot, "scripts/workbench_db.py");
+const python = execFileSync(
+ process.env.PYTHON?.trim() || "python3",
+ ["-c", "import sys; print(sys.executable)"],
+ { encoding: "utf8" },
+).trim();
+const codexHome = path.join(fixtureRoot, "home");
+const defaultState = path.join(codexHome, "state/plugins/codex-security");
+const environment = {
+ ...process.env,
+ CODEX_HOME: codexHome,
+ CODEX_SECURITY_SCAN_ROOT: undefined,
+ CODEX_SECURITY_STATE_DIR: undefined,
+ PYTHON: python,
+};
+
+function runWorkbench(args: string[], env: NodeJS.ProcessEnv, framed = false) {
+ return spawnSync(
+ python,
+ framed ? ["-c", WORKBENCH_PYTHON, script] : [script, ...args],
+ {
+ env,
+ encoding: "utf8",
+ input: framed ? `${JSON.stringify(args)}\n` : undefined,
+ },
+ );
+}
+
+async function openWorkspace(env: NodeJS.ProcessEnv) {
+ const server = startRpcServer({
+ command: process.execPath,
+ args: [serverBundlePath, "--stdio"],
+ cwd: pluginRoot,
+ env,
+ });
+ try {
+ assertNoError(await server.initialize("workbench-state-test"));
+ return await server.callTool(2, {
+ name: "open_codex_security_workspace",
+ arguments: { targetPath, scope: ".", mode: "standard" },
+ _meta: { "openai/threadId": "workbench-state-thread" },
+ });
+ } finally {
+ await server.stop();
+ }
+}
+
+try {
+ await mkdir(targetPath);
+ await writeFile(path.join(targetPath, "fixture.py"), "print('fixture')\n");
+ await buildServer(serverBundlePath, { target: "node20" });
+
+ assertNoError(await openWorkspace(environment));
+ assert.equal(
+ (await stat(path.join(defaultState, "workbench.sqlite3"))).isFile(),
+ true,
+ );
+
+ // A file in place of the state directory fails regardless of process privileges.
+ await rm(defaultState, { recursive: true });
+ await writeFile(defaultState, "occupied state path\n");
+ const direct = runWorkbench(["database-info"], environment);
+ const wrapped = runWorkbench(["database-info"], environment, true);
+ assert.equal(direct.status, 1);
+ assert.equal(wrapped.status, direct.status);
+ const diagnostic = direct.stderr.trim().split("\n").at(-1)!;
+ assert.match(diagnostic, /FileExistsError/);
+ assert.equal(wrapped.stderr.trim().split("\n").at(-1), diagnostic);
+ const failure = await openWorkspace(environment);
+ assert.equal(failure.error, undefined);
+ assert.equal(failure.result.isError, true);
+ assert.ok(
+ failure.result.content.some((item: { text?: string }) =>
+ item.text?.includes(diagnostic),
+ ),
+ );
+
+ const explicitState = path.join(fixtureRoot, "explicit-state");
+ const explicitEnvironment = {
+ ...environment,
+ CODEX_SECURITY_STATE_DIR: explicitState,
+ };
+ assertNoError(await openWorkspace(explicitEnvironment));
+ assert.equal(
+ (await stat(path.join(explicitState, "workbench.sqlite3"))).isFile(),
+ true,
+ );
+ assert.equal(await readFile(defaultState, "utf8"), "occupied state path\n");
+
+ for (const args of [
+ ["--help"],
+ ["create-workspace", "--help"],
+ ["resolve-scan-root"],
+ ["database-info"],
+ ["inspect-target", "--target-path", path.join(fixtureRoot, "missing")],
+ ]) {
+ const env = {
+ ...explicitEnvironment,
+ PYTHONSAFEPATH: "1",
+ PYTHONIOENCODING: "ascii",
+ };
+ const direct = runWorkbench(args, env);
+ const wrapped = runWorkbench(args, env, true);
+ assert.deepEqual(
+ [wrapped.status, wrapped.stdout, wrapped.stderr],
+ [direct.status, direct.stdout, direct.stderr],
+ );
+ }
+} finally {
+ await rm(serverBundlePath, { force: true });
+ await rm(fixtureRoot, { recursive: true, force: true });
+}
diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.ts b/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.ts
deleted file mode 100644
index 93ecdd066..000000000
--- a/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.ts
+++ /dev/null
@@ -1,504 +0,0 @@
-import { assertNoError } from "./assertions.ts";
-import {
- WORKBENCH_PYTHON,
- WORKBENCH_STATE_UNAVAILABLE_EXIT_CODE,
-} from "../src/server/workbench-process.ts";
-import assert from "node:assert/strict";
-import { execFileSync, spawnSync } from "node:child_process";
-import { randomUUID } from "node:crypto";
-import {
- chmod,
- mkdir,
- mkdtemp,
- readFile,
- realpath,
- rm,
- stat,
- writeFile,
-} from "node:fs/promises";
-import { tmpdir } from "node:os";
-import path from "node:path";
-
-import { applicationRoot as mcpAppRoot, buildServer } from "./build-server.ts";
-import { startRpcServer } from "./support/rpc-server.ts";
-
-if (process.platform !== "win32") {
- await testWorkbenchStateFallback();
-}
-
-async function testWorkbenchStateFallback() {
- const pluginRoot = path.resolve(mcpAppRoot, "..");
- const fixtureRoot = await realpath(
- await mkdtemp(path.join(tmpdir(), "codex-security-state-fallback-")),
- );
- const targetPath = path.join(fixtureRoot, "target");
- const serverBundlePath = path.join(
- pluginRoot,
- "mcp",
- `.state-fallback-test-${randomUUID()}.cjs`,
- );
- const pythonCommand = process.env.PYTHON?.trim() || "python3";
- const realPython = execFileSync(
- pythonCommand,
- ["-c", "import sys; print(sys.executable)"],
- {
- encoding: "utf8",
- },
- ).trim();
-
- await mkdir(targetPath, { recursive: true });
- await writeFile(path.join(targetPath, "fixture.py"), "print('fixture')\n");
- await buildServer(serverBundlePath, { target: "node20" });
-
- try {
- if (process.getuid?.() !== 0) {
- for (const firstOperation of [
- "scan",
- "standalone",
- "fresh-home",
- "inspection",
- ]) {
- const codexHome = path.join(
- fixtureRoot,
- `default-${firstOperation}-home`,
- );
- const defaultState = path.join(
- codexHome,
- "state",
- "plugins",
- "codex-security",
- );
- const lockedDirectory =
- firstOperation === "fresh-home" ? codexHome : defaultState;
- await mkdir(lockedDirectory, { recursive: true });
- await chmod(lockedDirectory, 0o500);
- const environment = {
- ...process.env,
- CODEX_HOME: codexHome,
- CODEX_SECURITY_STATE_DIR: undefined,
- };
- const direct = spawnSync(
- realPython,
- [path.join(pluginRoot, "scripts/workbench_db.py"), "database-info"],
- { env: environment, encoding: "utf8" },
- );
- const wrapped = spawnSync(
- realPython,
- [
- "-c",
- WORKBENCH_PYTHON,
- path.join(pluginRoot, "scripts/workbench_db.py"),
- ],
- { env: environment, encoding: "utf8", input: '["database-info"]\n' },
- );
- assert.equal(direct.status, 1);
- assert.equal(wrapped.status, WORKBENCH_STATE_UNAVAILABLE_EXIT_CODE);
- assert.equal(
- wrapped.stderr.trim().split("\n").at(-1),
- direct.stderr.trim().split("\n").at(-1),
- );
- const server = startServer(serverBundlePath, {
- CODEX_HOME: codexHome,
- CODEX_SECURITY_SCAN_ROOT: undefined,
- CODEX_SECURITY_STATE_DIR: undefined,
- PYTHON: realPython,
- });
- let fallbackState;
- try {
- await initialize(server, 1);
- if (firstOperation === "inspection")
- assertNoError(await inspectTarget(server, 2, targetPath));
- if (firstOperation === "fresh-home") {
- const opened = await Promise.all([
- openWorkspace(server, 20, targetPath, "first-thread"),
- openWorkspace(server, 21, targetPath, "second-thread"),
- ]);
- opened.forEach(assertNoError);
- }
- const standaloneInput = {
- targetPath,
- storage: "persistent",
- path: "threatmodel.md",
- content: "retained context\n",
- };
- if (firstOperation === "standalone") {
- assertNoError(
- await server.request(2, "tools/call", {
- name: "save_codex_security_artifact",
- arguments: standaloneInput,
- }),
- );
- }
- const started = await server.request(3, "tools/call", {
- name: "start_codex_security_standard_scan",
- arguments: { targetPath },
- _meta: { "openai/threadId": "default-state-fallback" },
- });
- assertNoError(started);
- const { scanId, scanDir, handoffClaimToken } =
- started.result.structuredContent;
- fallbackState = path.dirname(path.dirname(path.dirname(scanDir)));
- assert.ok(
- fallbackState.startsWith(
- path.join(await realpath(tmpdir()), "codex-security-state-"),
- ),
- );
- assert.equal(
- (
- await stat(path.join(fallbackState, "workbench.sqlite3"))
- ).isFile(),
- true,
- );
- const standalone = await server.request(4, "tools/call", {
- name: "save_codex_security_artifact",
- arguments: standaloneInput,
- });
- assertNoError(standalone);
- assert.ok(
- standalone.result.structuredContent.path.startsWith(
- path.join(fallbackState, "scans") + path.sep,
- ),
- );
- assert.equal(
- await readFile(standalone.result.structuredContent.path, "utf8"),
- standaloneInput.content,
- );
- const location = {
- scanId,
- handoffClaimToken,
- storage: "persistent",
- path: "artifacts/proof.txt",
- };
- const saved = await server.request(5, "tools/call", {
- name: "save_codex_security_artifact",
- arguments: { ...location, content: "exact café bytes\n" },
- });
- assertNoError(saved);
- assert.ok(
- saved.result.structuredContent.path.startsWith(scanDir + path.sep),
- );
- const read = await server.request(6, "tools/call", {
- name: "read_codex_security_artifact",
- arguments: location,
- });
- assertNoError(read);
- assert.equal(
- read.result.structuredContent.content,
- "exact café bytes\n",
- );
- assert.equal(
- server
- .stderrEvents()
- .filter((event) => event.event === "state_fallback_pinned")
- .length,
- 1,
- );
- assert.equal(
- await pathExists(path.join(defaultState, "scans")),
- false,
- );
- } finally {
- await server.stop();
- await chmod(lockedDirectory, 0o700);
- if (fallbackState)
- await rm(fallbackState, { recursive: true, force: true });
- }
- }
-
- const readFirstHome = path.join(fixtureRoot, "read-first-home");
- const readFirstDefaultState = path.join(
- readFirstHome,
- "state",
- "plugins",
- "codex-security",
- );
- const readFirstScanRoot = path.join(fixtureRoot, "read-first-scans");
- await mkdir(readFirstDefaultState, { recursive: true });
- await chmod(readFirstDefaultState, 0o500);
- const readFirstEnvironment = {
- CODEX_HOME: readFirstHome,
- CODEX_SECURITY_SCAN_ROOT: readFirstScanRoot,
- CODEX_SECURITY_STATE_DIR: undefined,
- PYTHON: realPython,
- };
- let readFirstServer = startServer(serverBundlePath, readFirstEnvironment);
- try {
- await initialize(readFirstServer, 1);
- const artifact = {
- targetPath,
- storage: "persistent",
- path: "threatmodel.md",
- };
- const saved = await readFirstServer.request(2, "tools/call", {
- name: "save_codex_security_artifact",
- arguments: { ...artifact, content: "retained context\n" },
- });
- assertNoError(saved);
- await readFirstServer.stop();
- readFirstServer = startServer(serverBundlePath, readFirstEnvironment);
- await initialize(readFirstServer, 1);
- const read = await readFirstServer.request(2, "tools/call", {
- name: "read_codex_security_artifact",
- arguments: artifact,
- });
- assertNoError(read);
- assert.equal(
- read.result.structuredContent.content,
- "retained context\n",
- );
- const fallbackStateDir = path.join(
- readFirstScanRoot,
- "workbench-state",
- );
- assert.equal(
- await pathExists(path.join(fallbackStateDir, "workbench.sqlite3")),
- false,
- );
- const started = await startPromptOnlyScan(
- readFirstServer,
- 3,
- targetPath,
- );
- assertNoError(started);
- assert.ok(
- started.result.structuredContent.scan.scanDir.startsWith(
- (await realpath(readFirstScanRoot)) + path.sep,
- ),
- );
- assert.equal(
- (
- await stat(path.join(fallbackStateDir, "workbench.sqlite3"))
- ).isFile(),
- true,
- );
- assert.equal(
- await pathExists(
- path.join(readFirstDefaultState, "workbench.sqlite3"),
- ),
- false,
- );
- assert.equal(
- readFirstServer
- .stderrEvents()
- .filter((event) => event.event === "state_fallback_pinned").length,
- 1,
- );
- assert.equal(
- await readFile(saved.result.structuredContent.path, "utf8"),
- "retained context\n",
- );
- } finally {
- await readFirstServer.stop();
- await chmod(readFirstDefaultState, 0o700);
- }
- }
-
- for (const [index, name] of [
- "ordinary-missing",
- "WorkbenchStateDirectoryError:missing",
- "newline\nWorkbenchStateDirectoryError: [Errno 13] Permission denied: state",
- "sqlite3.OperationalError: unable to open database file",
- ].entries()) {
- const codexHome = path.join(fixtureRoot, `collision-${index}`);
- const scanRoot = path.join(fixtureRoot, `collision-scans-${index}`);
- const server = startServer(serverBundlePath, {
- CODEX_HOME: codexHome,
- CODEX_SECURITY_STATE_DIR: undefined,
- CODEX_SECURITY_SCAN_ROOT: scanRoot,
- PYTHON: realPython,
- });
- try {
- await initialize(server, 1);
- const missing = path.join(fixtureRoot, name);
- const failure = await startPromptOnlyScan(server, 2, missing);
- assertToolError(
- failure,
- /Scan target is not a readable local directory/,
- );
- assert.ok(failure.result.content[0].text.includes(missing));
- assertNoError(await openWorkspace(server, 3, targetPath));
- assert.equal(server.stderrEvents().length, 0);
- assert.equal(
- await pathExists(path.join(scanRoot, "workbench-state")),
- false,
- );
- assert.equal(
- await pathExists(
- path.join(
- codexHome,
- "state/plugins/codex-security/workbench.sqlite3",
- ),
- ),
- true,
- );
- } finally {
- await server.stop();
- }
- }
-
- for (const scenario of [
- "explicit",
- "explicit-open",
- "proven",
- "proven-open",
- "malformed",
- ]) {
- if (scenario !== "malformed" && process.getuid?.() === 0) continue;
- const codexHome = path.join(fixtureRoot, `${scenario}-home`);
- const stateDir = path.join(codexHome, "state/plugins/codex-security");
- const scanRoot = path.join(fixtureRoot, `${scenario}-scans`);
- await mkdir(stateDir, { recursive: true });
- const server = startServer(serverBundlePath, {
- CODEX_HOME: codexHome,
- CODEX_SECURITY_STATE_DIR:
- scenario === "explicit"
- ? path.join(stateDir, "explicit-state")
- : scenario === "explicit-open"
- ? stateDir
- : undefined,
- CODEX_SECURITY_SCAN_ROOT: scanRoot,
- PYTHON: realPython,
- });
- try {
- await initialize(server, 1);
- if (scenario.startsWith("proven"))
- assertNoError(await openWorkspace(server, 2, targetPath));
- if (scenario === "malformed")
- await writeFile(
- path.join(stateDir, "workbench.sqlite3"),
- "not a database",
- );
- else await chmod(stateDir, 0o500);
- if (scenario === "proven-open")
- await chmod(path.join(stateDir, "workbench.sqlite3"), 0o000);
- assertToolError(
- await openWorkspace(server, 3, targetPath, "next-thread"),
- /Permission denied|readonly|read-only|not a database|unable to open database file/,
- );
- assert.equal(server.stderrEvents().length, 0);
- assert.equal(
- await pathExists(path.join(scanRoot, "workbench-state")),
- false,
- );
- } finally {
- await server.stop();
- await chmod(stateDir, 0o700);
- }
- }
-
- const script = path.join(pluginRoot, "scripts/workbench_db.py");
- for (const args of [
- ["--help"],
- ["create-workspace", "--help"],
- ["resolve-scan-root"],
- ["database-info"],
- ["inspect-target", "--target-path", path.join(fixtureRoot, "missing")],
- ]) {
- const env = {
- ...process.env,
- CODEX_SECURITY_STATE_DIR: path.join(fixtureRoot, "parity-state"),
- PYTHONSAFEPATH: "1",
- PYTHONIOENCODING: "ascii",
- };
- const direct = spawnSync(realPython, [script, ...args], {
- env,
- encoding: "utf8",
- });
- const wrapped = spawnSync(realPython, ["-c", WORKBENCH_PYTHON, script], {
- env,
- encoding: "utf8",
- input: `${JSON.stringify(args)}\n`,
- });
- assert.deepEqual(
- [wrapped.status, wrapped.stdout, wrapped.stderr],
- [direct.status, direct.stdout, direct.stderr],
- );
- }
- } finally {
- await rm(serverBundlePath, { force: true });
- await rm(fixtureRoot, { recursive: true, force: true });
- }
-}
-
-function startServer(serverPath: string, env: NodeJS.ProcessEnv) {
- return startRpcServer(
- {
- command: process.execPath,
- args: [serverPath, "--stdio"],
- cwd: path.dirname(path.dirname(serverPath)),
- env: { ...process.env, ...env },
- stderr: "pipe",
- },
- { component: "codex_security_workbench", timeoutMs: 15_000 },
- );
-}
-
-async function initialize(server: ReturnType<typeof startServer>, id: number) {
- const response = await server.request(id, "initialize", {
- protocolVersion: "2025-11-25",
- capabilities: {},
- clientInfo: { name: "workbench-state-fallback", version: "0.1.0" },
- });
- assertNoError(response);
-}
-
-function inspectTarget(
- server: ReturnType<typeof startServer>,
- id: number,
- targetPath: string,
-) {
- return server.request(id, "tools/call", {
- name: "inspect_codex_security_target",
- arguments: { targetPath },
- });
-}
-
-function startPromptOnlyScan(
- server: ReturnType<typeof startServer>,
- id: number,
- targetPath: string,
-) {
- return server.request(id, "tools/call", {
- name: "start_codex_security_prompt_only_scan",
- arguments: { mode: "standard", scope: ".", targetPath },
- _meta: { "openai/threadId": "state-fallback-prompt-only-thread" },
- });
-}
-
-function openWorkspace(
- server: ReturnType<typeof startServer>,
- id: number,
- targetPath: string,
- threadId = "state-fallback-thread",
-) {
- return server.request(id, "tools/call", {
- name: "open_codex_security_workspace",
- arguments: { targetPath, scope: ".", mode: "standard" },
- _meta: { "openai/threadId": threadId },
- });
-}
-
-function assertToolError(
- response: {
- error?: unknown;
- result: { isError?: boolean; content: { text: string }[] };
- },
- pattern: RegExp,
-) {
- assert.equal(response.error, undefined);
- assert.equal(response.result?.isError, true);
- assert.match(
- response.result.content.map((item) => item.text).join(" "),
- pattern,
- );
-}
-
-async function pathExists(filePath: string) {
- try {
- await stat(filePath);
- return true;
- } catch (error) {
- if ((error as NodeJS.ErrnoException)?.code === "ENOENT") return false;
- throw error;
- }
-}
diff --git a/plugins/codex-security/references/artifact-storage.md b/plugins/codex-security/references/artifact-storage.md
index fb3476791..dac64c653 100644
--- a/plugins/codex-security/references/artifact-storage.md
+++ b/plugins/codex-security/references/artifact-storage.md
@@ -19,7 +19,7 @@ Identify the owner with exactly one of:
Select `storage` explicitly:
-- `persistent`: retained documents/evidence under the authoritative scan directory or standalone collection. The default root is `$CODEX_SECURITY_STATE_DIR/scans`, or `$CODEX_HOME/state/plugins/codex-security/scans` when no state override is set; `CODEX_HOME` defaults to `~/.codex`. The existing `CODEX_SECURITY_SCAN_ROOT` override still takes precedence for plugin-created output. If the default workbench state is unwritable, retained artifacts follow its temporary fallback state's `scans` directory and can be removed by OS temporary-directory cleanup. Preserve an existing scan's saved path, including older temporary paths.
+- `persistent`: retained documents/evidence under the authoritative scan directory or standalone collection. The default root is `$CODEX_SECURITY_STATE_DIR/scans`, or `$CODEX_HOME/state/plugins/codex-security/scans` when no state override is set; `CODEX_HOME` defaults to `~/.codex`. The existing `CODEX_SECURITY_SCAN_ROOT` override still takes precedence for plugin-created output. The workbench state directory must be writable; set `CODEX_SECURITY_STATE_DIR` explicitly on hosts with an unwritable default. Preserve an existing scan's saved path, including older temporary paths.
- `temporary`: disposable staging and execution output in a context-specific OS temporary directory. Standalone temporary saves and reads do not create or require the persistent collection. It is not part of the completed result and must not be referenced from canonical findings or coverage. It can disappear independently of retained files.
Relative storage overrides resolve from the plugin directory, matching the workbench, regardless of the MCP launch directory.
diff --git a/plugins/codex-security/references/scan-contract.md b/plugins/codex-security/references/scan-contract.md
index 06eef1789..486b7a4e1 100644
--- a/plugins/codex-security/references/scan-contract.md
+++ b/plugins/codex-security/references/scan-contract.md
@@ -26,6 +26,8 @@ Retention is an explicit consumer decision. Producing a sealed bundle must not s
A sealed manifest records the terminal timestamp and hashes for the canonical documents and immutable evidence receipts included in that bundle. Readable reports and generated exports are projections and are not included in the canonical seal. Later adapters may read the sealed bundle to create projections, but must not mutate the sealed manifest or canonical documents. Store projections separately. Every sealed manifest includes exactly one artifact record for each canonical JSON document, and artifact paths must not repeat.
+Producers should use UTC ISO 8601 timestamps in the native SDK or plugin format. All schema-valid fractional widths remain readable on supported Python versions; parsing uses native microsecond precision. History and finding queries use native date precision and stable ID tie-breakers; ordering beyond millisecond precision is not guaranteed. Stored timestamp text remains unchanged.
+
`scan.status` records why the bundle was sealed:
| Status | Meaning |
@@ -134,6 +136,8 @@ For Standard and diff scans, record:
`inventoryStrategy` records how the producer enumerated the reviewed content, independently of the requested scan workflow:
+The discovery helper's `in_scope_files.txt` input contains one UTF-8 path per line. LF and CRLF separators are accepted; filenames containing CR or LF are unsupported. Other path whitespace and spelling are preserved.
+
For a whole-repository Deep scan, keep `inventoryStrategy` as `repository`; repeated discovery is workflow metadata, not a different inventory strategy.
| Inventory strategy | Meaning |
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.py
index 1a94345fc..32cd40d6b 100644
--- a/plugins/codex-security/scripts/finalize_scan_contract.py
+++ b/plugins/codex-security/scripts/finalize_scan_contract.py
@@ -63,7 +63,8 @@ SARIF_SECURITY_SCORES = {
}
SLUG_RE = re.compile(r"^[a-z0-9][a-z0-9._/-]*$")
RFC3339_RE = re.compile(
- r"^\d{4}-\d{2}-\d{2}[Tt]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:[Zz]|[+-]\d{2}:\d{2})$"
+ r"^\d{4}-\d{2}-\d{2}[Tt]\d{2}:\d{2}:\d{2}(?:\.(?P<fraction>\d+))?(?:[Zz]|[+-]\d{2}:\d{2})$",
+ re.ASCII,
)
REMOTE_CONTROL_RE = re.compile(r"[\x00-\x1f\x7f-\x9f\u2028\u2029]")
GITHUB_HASH_BLOCK_SIZE = 100
@@ -878,16 +879,19 @@ def _validate_remote(remote: str, context: str) -> None:
)
+def parse_timestamp(value: str) -> datetime:
+ match = RFC3339_RE.fullmatch(value)
+ if match and (fraction := match.group("fraction")):
+ start, end = match.span("fraction")
+ value = value[:start] + fraction[:6].ljust(6, "0") + value[end:]
+ return datetime.fromisoformat(value.upper().replace("Z", "+00:00"))
+
+
def _validate_date_time(value: str, context: str) -> None:
if not RFC3339_RE.fullmatch(value):
raise ContractError(f"{context}: expected an RFC 3339 timestamp")
- # Python 3.10 only parses 3 or 6 fractional digits. Validate the calendar and
- # offset without a complete ASCII fraction, leaving the original text intact.
- parser_value = re.sub(r"\.[0-9]+(?=[Zz+-])", "", value)
try:
- parsed = datetime.fromisoformat(
- parser_value[:-1] + "+00:00" if parser_value[-1] in "Zz" else parser_value
- )
+ parsed = parse_timestamp(value)
except ValueError as exc:
raise ContractError(f"{context}: expected an RFC 3339 timestamp") from exc
if parsed.tzinfo is None:
diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.py
index 505df4d84..e2082285f 100644
--- a/plugins/codex-security/scripts/workbench_db.py
+++ b/plugins/codex-security/scripts/workbench_db.py
@@ -225,16 +225,13 @@ def connect(*, deferred: bool = False) -> sqlite3.Connection:
)
try:
create_private_directory(path.parent)
- except OSError as exc:
- if exc.errno in {errno.EACCES, errno.EPERM, errno.EROFS}:
- exc._codex_security_state_unavailable = True
+ except OSError:
+ print(guidance, file=sys.stderr)
raise
for attempt in range(SQLITE_RETRY_ATTEMPTS):
try:
connection = sqlite3.connect(path, timeout=5)
- except sqlite3.OperationalError as exc:
- if str(exc) == "unable to open database file":
- exc._codex_security_state_unavailable = True
+ except sqlite3.OperationalError:
print(guidance, file=sys.stderr)
raise
try:
diff --git a/plugins/codex-security/scripts/workbench_feedback.py b/plugins/codex-security/scripts/workbench_feedback.py
index 906ee0c95..d756b3fb4 100644
--- a/plugins/codex-security/scripts/workbench_feedback.py
+++ b/plugins/codex-security/scripts/workbench_feedback.py
@@ -16,11 +16,10 @@ from workbench_constants import (
FINDING_SUMMARY_BYTES,
FINDING_TITLE_BYTES,
)
-from workbench_validation import bounded_output_text, register_timestamp_collation
+from workbench_validation import bounded_output_text
def get_scan_feedback(connection: sqlite3.Connection, scan: sqlite3.Row) -> dict[str, Any]:
- register_timestamp_collation(connection)
rows = connection.execute(
"""
WITH ranked_decisions AS (
@@ -35,9 +34,8 @@ def get_scan_feedback(connection: sqlite3.Connection, scan: sqlite3.Row) -> dict
ROW_NUMBER() OVER (
PARTITION BY findings.id
ORDER BY
- COALESCE(triage.updated_at, source_scans.completed_at)
- COLLATE codex_security_timestamp DESC,
- source_scans.completed_at COLLATE codex_security_timestamp DESC,
+ julianday(upper(COALESCE(triage.updated_at, source_scans.completed_at))) DESC,
+ julianday(upper(source_scans.completed_at)) DESC,
source_scans.id DESC, occurrences.id DESC
) AS decision_rank
FROM finding_occurrences AS occurrences
@@ -63,8 +61,8 @@ def get_scan_feedback(connection: sqlite3.Connection, scan: sqlite3.Row) -> dict
AND close_reason = 'false_positive'
AND note IS NOT NULL
AND trim(note) != ''
- ORDER BY updated_at COLLATE codex_security_timestamp DESC,
- source_completed_at COLLATE codex_security_timestamp DESC,
+ ORDER BY julianday(upper(updated_at)) DESC,
+ julianday(upper(source_completed_at)) DESC,
source_scan_id DESC, finding_id DESC
LIMIT 50
""",
diff --git a/plugins/codex-security/scripts/workbench_native_indexes.py b/plugins/codex-security/scripts/workbench_native_indexes.py
index f11eb8cda..6fd1a52cf 100644
--- a/plugins/codex-security/scripts/workbench_native_indexes.py
+++ b/plugins/codex-security/scripts/workbench_native_indexes.py
@@ -13,7 +13,7 @@ from typing import Any
sys.path.insert(0, str(Path(__file__).resolve().parent))
import workbench_scan_history as scan_history
from workbench_constants import FINDING_SUMMARY_BYTES, FINDING_TITLE_BYTES, FINDINGS_PAGE_MAX
-from workbench_validation import bounded_output_text, register_timestamp_collation, timestamp_key
+from workbench_validation import bounded_output_text, timestamp_key
def list_global_findings(
@@ -75,7 +75,6 @@ def list_global_findings(
def _indexed_findings(connection: sqlite3.Connection) -> Iterator[dict[str, Any]]:
- register_timestamp_collation(connection)
parents: dict[tuple[str, str], tuple[str, str]] = {}
def group(identity: tuple[str, str]) -> tuple[str, str]:
@@ -102,7 +101,7 @@ def _indexed_findings(connection: sqlite3.Connection) -> Iterator[dict[str, Any]
latest_scan_by_target = dict(
connection.execute(
- "SELECT target_id, id FROM scans WHERE status = 'complete' ORDER BY started_at COLLATE codex_security_timestamp, id"
+ "SELECT target_id, id FROM scans WHERE status = 'complete' ORDER BY julianday(upper(started_at)), id"
)
)
@@ -204,14 +203,13 @@ def list_repositories(
connection: sqlite3.Connection,
args: argparse.Namespace | None = None,
) -> dict[str, Any]:
- register_timestamp_collation(connection)
scans = scan_history.list_scans(connection)["scans"]
scans_by_id = {scan["scanId"]: scan for scan in scans}
scan_count_by_target = dict(Counter(scan["targetId"] for scan in scans))
latest_scan_by_target: dict[str, dict[str, Any]] = {}
for row in connection.execute(
- "SELECT id, target_id FROM scans ORDER BY started_at COLLATE codex_security_timestamp DESC, id DESC"
+ "SELECT id, target_id FROM scans ORDER BY julianday(upper(started_at)) DESC, id DESC"
):
latest_scan_by_target.setdefault(row["target_id"], scans_by_id[row["id"]])
diff --git a/plugins/codex-security/scripts/workbench_scan_history.py b/plugins/codex-security/scripts/workbench_scan_history.py
index 7b9a84ec4..418817224 100644
--- a/plugins/codex-security/scripts/workbench_scan_history.py
+++ b/plugins/codex-security/scripts/workbench_scan_history.py
@@ -20,7 +20,7 @@ from workbench_constants import ARTIFACTS, FINDINGS_PAGE_MAX
from workbench_scan_start import scan_target_identity
from workbench_scan_usage import stored_scan_cost_fields
from workbench_target import git_output, require_scan_target_identity
-from workbench_validation import register_timestamp_collation, reject_non_finite_json, timestamp_key
+from workbench_validation import reject_non_finite_json, timestamp_key
def rename_scan(connection: sqlite3.Connection, scan: sqlite3.Row, name: str) -> dict[str, Any]:
@@ -204,7 +204,6 @@ def _repository_origin(target: Path) -> tuple[str, str] | None:
def list_scans(
connection: sqlite3.Connection, args: argparse.Namespace | None = None
) -> dict[str, Any]:
- register_timestamp_collation(connection)
connection.create_function("casefold", 1, str.casefold, deterministic=True)
if os.name == "nt":
connection.create_function("codex_security_path_key", 1, _windows_path_key)
@@ -298,9 +297,8 @@ def list_scans(
{where}
ORDER BY
CASE WHEN scans.status = 'running' AND scans.canceled_at IS NULL THEN 0 ELSE 1 END,
- MAX(scans.updated_at COLLATE codex_security_timestamp, progress.updated_at)
- COLLATE codex_security_timestamp DESC,
- scans.started_at COLLATE codex_security_timestamp DESC,
+ MAX(julianday(upper(scans.updated_at)), julianday(upper(progress.updated_at))) DESC,
+ julianday(upper(scans.started_at)) DESC,
scans.id
{pagination}
""",
diff --git a/plugins/codex-security/scripts/workbench_validation.py b/plugins/codex-security/scripts/workbench_validation.py
index 36b51dc44..bd69acd57 100644
--- a/plugins/codex-security/scripts/workbench_validation.py
+++ b/plugins/codex-security/scripts/workbench_validation.py
@@ -21,23 +21,8 @@ from workbench_scan_usage import _reject_nonstandard_json_number as reject_nonst
reject_non_finite_json = finalizer._reject_non_finite_json
-def timestamp_key(value: str) -> tuple[datetime, str]:
- """Compare accepted timestamps without rounding fractions or rewriting text."""
- utc = value[-1] in "Zz"
- whole_second = datetime.fromisoformat(value[:19] + ("+00:00" if utc else value[-6:]))
- fraction = value[20 : -1 if utc else -6].rstrip("0") if value[19:20] == "." else ""
- return whole_second, fraction
-
-
-def compare_timestamps(left: str, right: str) -> int:
- left_key, right_key = timestamp_key(left), timestamp_key(right)
- return (left_key > right_key) - (left_key < right_key)
-
-
-def register_timestamp_collation(connection: sqlite3.Connection) -> None:
- name = "codex_security_timestamp"
- if not any(row[1].lower() == name for row in connection.execute("PRAGMA collation_list")):
- connection.create_collation(name, compare_timestamps)
+def timestamp_key(value: str) -> datetime:
+ return finalizer.parse_timestamp(value)
def require_uuid(value: str, label: str) -> str:
diff --git a/plugins/codex-security/tests/test_finalize_scan_timestamps.py b/plugins/codex-security/tests/test_finalize_scan_timestamps.py
index 71384a295..595eb329d 100644
--- a/plugins/codex-security/tests/test_finalize_scan_timestamps.py
+++ b/plugins/codex-security/tests/test_finalize_scan_timestamps.py
@@ -46,38 +46,75 @@ class FinalizeScanTimestampsTest(unittest.TestCase):
self.assertNotIn("sealedAt", self.read_json("scan-manifest.json")["scan"])
- def test_preserves_rfc3339_fractional_timestamps_when_sealing(self) -> None:
- for fraction in ("", ".1", ".12", ".123", ".1234", ".12345", ".123456", ".1234567"):
- for offset in ("z", "+02:00", "-07:30"):
- timestamp = f"2024-02-29t18:09:00{fraction}{offset}"
- with self.subTest(timestamp=timestamp):
- self.manifest["scan"]["startedAt"] = timestamp
- self.manifest["scan"]["completedAt"] = timestamp
- self.write_scan()
-
- manifest, _, _ = FINALIZER.finalize_scan(self.scan_dir)
-
- for field in ("startedAt", "completedAt", "sealedAt"):
- self.assertEqual(manifest["scan"][field], timestamp)
- before = self.scan_file_bytes(*CANONICAL_FILES)
- FINALIZER.finalize_scan(self.scan_dir)
- self.assertEqual(self.scan_file_bytes(*CANONICAL_FILES), before)
+ def test_preserves_timestamp_text_when_sealing(self) -> None:
+ for timestamp in (
+ "2024-02-29T18:09:00Z",
+ "2024-02-29T18:09:00.1Z",
+ "2024-02-29T18:09:00.12+02:00",
+ "2024-02-29T18:09:00.123Z",
+ "2024-02-29T18:09:00.1234Z",
+ "2024-02-29T18:09:00.12345-02:00",
+ "2024-02-29T18:09:00.123456+02:00",
+ "2024-02-29T18:09:00.1234567+02:00",
+ "2024-02-29T18:09:00.123456789Z",
+ "2024-02-29t18:09:00.123z",
+ ):
+ with self.subTest(timestamp=timestamp):
+ self.manifest["scan"]["startedAt"] = timestamp
+ self.manifest["scan"]["completedAt"] = timestamp
+ self.write_scan()
+
+ manifest, _, _ = FINALIZER.finalize_scan(self.scan_dir)
+
+ for field in ("startedAt", "completedAt", "sealedAt"):
+ self.assertEqual(manifest["scan"][field], timestamp)
+ before = self.scan_file_bytes(*CANONICAL_FILES)
+ FINALIZER.finalize_scan(self.scan_dir)
+ self.assertEqual(self.scan_file_bytes(*CANONICAL_FILES), before)
+
+ def test_exports_sealed_scan_with_fractional_timestamps_without_rewriting(self) -> None:
+ self.write_scan()
+ manifest, _, _ = FINALIZER.finalize_scan(self.scan_dir)
+ fractions = (
+ "1",
+ "12",
+ "123",
+ "1234",
+ "12345",
+ "123456",
+ "1234567",
+ "123456789",
+ "123456789" * 8,
+ )
+ for fraction in fractions:
+ with self.subTest(fraction=fraction):
+ timestamp = f"2026-10-08t01:00:00.{fraction}z"
+ for field in ("startedAt", "completedAt", "sealedAt"):
+ manifest["scan"][field] = timestamp
+ self.write_json("scan-manifest.json", manifest)
+ before = self.scan_file_bytes(*CANONICAL_FILES)
+
+ sarif = FINALIZER.build_sarif_projection(self.scan_dir)
+
+ self.assertEqual(len(sarif["runs"]), 1)
+ self.assertEqual(self.scan_file_bytes(*CANONICAL_FILES), before)
def test_rejects_non_rfc3339_timestamps(self) -> None:
for timestamp in (
"2026-W22-7T18:09:00+00:00",
"2026-05-31T18:09:00+0000",
- "2026-02-29T18:09:00.123456789Z",
- "2026-05-31T24:09:00.1Z",
- "2026-05-31T18:60:00.1Z",
+ "2026-02-29T18:09:00.123456Z",
+ "2026-05-31T24:09:00.123Z",
+ "2026-05-31T18:60:00.123Z",
"2026-05-31T18:09:61.1Z",
- "2026-05-31T18:09:00.123456789+24:00",
- "2026-05-31T18:09:00.123456789-24:00",
+ "2026-05-31T18:09:00.123456+24:00",
+ "2026-05-31T18:09:00.123456-24:00",
"2026-05-31T18:09:00.Z",
"2026-05-31T18:09:00,123Z",
- "2026-05-31T18:09:00.1",
+ "2026-05-31T18:09:00.123",
"2026-05-31T18:09:00.\u0661Z",
"2026-05-31T18:09:00.123\u0664Z",
+ "2026-05-31T18:09:00.1Z",
):
with self.subTest(timestamp=timestamp):
self.manifest["scan"]["startedAt"] = timestamp
diff --git a/plugins/codex-security/tests/test_workbench_completion_binding.py b/plugins/codex-security/tests/test_workbench_completion_binding.py
index ed8f5201b..6b23a2c0c 100644
--- a/plugins/codex-security/tests/test_workbench_completion_binding.py
+++ b/plugins/codex-security/tests/test_workbench_completion_binding.py
@@ -415,9 +415,7 @@ def test_prepared_completion_does_not_publish_scan_before_acceptance(tmp_path: P
"timestamp",
[
"2026-10-08T03:00:00.123456+02:00",
- "2026-10-08t01:00:00.123456789z",
- "2026-10-08T01:00:00.1-00:00",
- "2026-10-07T17:30:00.123456789012345678901234567890-07:30",
+ "2026-10-08T01:00:00.123Z",
],
)
def test_sealed_completion_preserves_original_timestamp_text(
diff --git a/plugins/codex-security/tests/test_workbench_feedback.py b/plugins/codex-security/tests/test_workbench_feedback.py
index 9e1e34ad3..eb0282445 100644
--- a/plugins/codex-security/tests/test_workbench_feedback.py
+++ b/plugins/codex-security/tests/test_workbench_feedback.py
@@ -363,7 +363,7 @@ def test_later_offset_completion_supersedes_false_positive_feedback(tmp_path: Pa
timestamp = timestamp.astimezone(timezone(timedelta(hours=-5)))
manifest["scan"].update(
startedAt="2026-10-08T00:00:00Z",
- completedAt=timestamp.isoformat().replace("+00:00", "Z"),
+ completedAt=timestamp.isoformat().replace("+00:00", "Z").lower(),
)
manifest_path.write_text(json.dumps(manifest))
_seal_draft(scan_dir, target)
diff --git a/plugins/codex-security/tests/test_workbench_native_index_queries.py b/plugins/codex-security/tests/test_workbench_native_index_queries.py
index 309b0294f..8aa6a8ab9 100644
--- a/plugins/codex-security/tests/test_workbench_native_index_queries.py
+++ b/plugins/codex-security/tests/test_workbench_native_index_queries.py
@@ -1,19 +1,11 @@
from __future__ import annotations
-import json
import ntpath
from argparse import Namespace
-from pathlib import Path
from types import SimpleNamespace
import pytest
-from test_workbench_completion_binding import _seal_draft, register_cli_scan
-from workbench_test_support import (
- run_workbench,
- scan_command,
- stable_target_id,
- write_completed_contract,
-)
+from workbench_test_support import stable_target_id
SCAN_IDS = [f"00000000-0000-4000-8000-{index:012d}" for index in range(3)]
@@ -166,66 +158,12 @@ def test_latest_scan_confirmation_uses_group_membership(
assert finding["confirmedInLatestScan"] is False
- ("older", "later", "equal"),
- [
- ("2026-10-08T03:00:00.123456+02:00", "2026-10-08T01:00:02.000Z", False),
- ("2026-10-08T01:00:00.123Z", "2026-10-08T01:00:00.123456Z", False),
- (
- "2026-10-08T01:00:00.123456000000000001Z",
- "2026-10-08T01:00:00.123456000000000002Z",
- False,
- ),
- ("2026-10-08T03:00:00.123000+02:00", "2026-10-08t01:00:00.123z", True),
- ],
-)
-def test_global_findings_select_exact_sealed_completion_times(
- tmp_path: Path, older: str, later: str, equal: bool
-) -> None:
- state, target = tmp_path / "state", tmp_path / "target"
- target.mkdir()
- expected = []
- sealed = {}
- for index, timestamp in enumerate((older, later)):
- scan = tmp_path / f"scan-{index}"
- scan_id = register_cli_scan(state, target, scan)["scanId"]
- write_completed_contract(scan, scan_id, target)
- manifest = json.loads((scan / "scan-manifest.json").read_text())
- manifest["scan"].update(startedAt="2026-10-08T00:00:00Z", completedAt=timestamp)
- (scan / "scan-manifest.json").write_text(json.dumps(manifest))
- findings = json.loads((scan / "findings.json").read_text())
- findings["findings"][0]["title"] = f"Synthetic observation {index}"
- findings["findings"][0]["severity"]["level"] = "low" if index == 0 else "high"
- (scan / "findings.json").write_text(json.dumps(findings))
- _seal_draft(scan, target)
- sealed.update(
- {
- scan / name: (scan / name).read_bytes()
- for name in ("scan-manifest.json", "findings.json", "coverage.json")
- }
- )
- scan_command(state, "complete-scan", scan_id)
- expected.append(json.loads((scan / "findings.json").read_text())["findings"][0])
- selected = max(expected, key=lambda item: item["occurrenceId"]) if equal else expected[1]
- (finding,) = run_workbench(state, "list-global-findings")["findings"]
- assert finding["occurrenceId"] == selected["occurrenceId"]
- assert finding["title"] == selected["title"]
- assert finding["severity"] == {"level": selected["severity"]["level"]}
- assert finding["createdAt"] == (older if selected is expected[0] else later)
- assert {path: path.read_bytes() for path in sealed} == sealed
-
-
@pytest.mark.parametrize(
("completion", "status", "updated"),
[
("2026-10-08T03:00:00+02:00", "closed", "2026-10-08T01:00:01.000001Z"),
("2026-10-07T23:00:02-02:00", "open", "2026-10-07T23:00:02-02:00"),
- ("2026-10-08T03:00:01.000001000+02:00", "closed", "2026-10-08T03:00:01.000001000+02:00"),
- (
- "2026-10-08T01:00:01.000001000000000001Z",
- "open",
- "2026-10-08T01:00:01.000001000000000001Z",
- ),
+ ("2026-10-08T03:00:01.000001+02:00", "closed", "2026-10-08T03:00:01.000001+02:00"),
],
)
def test_global_findings_compare_decisions_and_return_original_update_text(
@@ -266,15 +204,7 @@ def test_global_findings_compare_decisions_and_return_original_update_text(
[
"2026-10-08T03:00:00+02:00",
"2026-10-08T01:00:00.123Z",
- "2026-10-08T01:00:00.123456Z",
- ],
- [2, 1, 0],
- ),
- (
- [
- "2026-10-08T01:00:00.123456000000000001Z",
- "2026-10-08T01:00:00.123456000000000002Z",
- "2026-10-08T01:00:00.123456000000000003Z",
+ "2026-10-08T01:00:00.234567Z",
],
[2, 1, 0],
),
@@ -286,17 +216,9 @@ def test_global_findings_compare_decisions_and_return_original_update_text(
],
[0, 1, 2],
),
- (
- [
- "0001-01-01T00:00:00.000000001+23:59",
- "0001-01-01T00:00:00Z",
- "9999-12-31T23:59:59.999999999-23:59",
- ],
- [2, 1, 0],
- ),
],
)
-def test_global_finding_pages_follow_exact_completion_order(
+def test_global_finding_pages_follow_completion_order(
workbench_api, indexed_collections, timestamps, order
):
connection, _ = indexed_collections
@@ -461,15 +383,7 @@ def test_scan_list_probes_requested_repository_once(
[
"2026-10-08T03:00:00+02:00",
"2026-10-08T01:00:00.123Z",
- "2026-10-08T01:00:00.123456Z",
- ],
- [2, 1, 0],
- ),
- (
- [
- "2026-10-08T01:00:00.123456000000000001Z",
- "2026-10-08T01:00:00.123456000000000002Z",
- "2026-10-08T01:00:00.123456000000000003Z",
+ "2026-10-08T01:00:00.234567Z",
],
[2, 1, 0],
),
@@ -483,7 +397,7 @@ def test_scan_list_probes_requested_repository_once(
),
],
)
-def test_scan_pages_compare_exact_updates_and_preserve_ties(
+def test_scan_pages_compare_updates_and_preserve_ties(
workbench_api, indexed_collections, timestamps, order
):
connection, _ = indexed_collections
@@ -515,8 +429,17 @@ def test_scan_pages_compare_exact_updates_and_preserve_ties(
[
("2026-10-08T01:00:00Z", "2026-10-08T01:00:00.100000Z", False),
("2026-10-08T03:00:00+02:00", "2026-10-08T01:00:00.100000Z", False),
- ("2026-10-08T01:00:00.000000001Z", "2026-10-08T01:00:00.000000002Z", False),
- ("2026-10-08T03:00:00.100000+02:00", "2026-10-08t01:00:00.1z", True),
+ ("2026-10-08T03:00:00.100000+02:00", "2026-10-08t01:00:00.100z", True),
+ ("2026-10-08T01:00:00.1Z", "2026-10-08T01:00:00.12Z", False),
+ ("2026-10-08T01:00:00.1234Z", "2026-10-08T01:00:00.23456Z", False),
+ ("2026-10-08T03:00:00.1+02:00", "2026-10-08t01:00:00.100000z", True),
+ ("2026-10-08T01:00:00.1234567Z", "2026-10-08T01:00:00.2345678Z", False),
+ ("2026-10-08T01:00:00.123456789Z", "2026-10-08T01:00:00.234567891Z", False),
+ (
+ "2026-10-08T01:00:00." + "123456789" * 8 + "Z",
+ "2026-10-08T01:00:00." + "234567891" * 8 + "Z",
+ False,
+ ),
],
)
def test_scan_start_chronology_keeps_latest_and_first_seen_queries_consistent(
diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
index 5e12899bf..37e926584 100644
--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
@@ -92,8 +92,12 @@ EXPECTED_MIGRATIONS = [
]
[email protected]("code", [errno.EACCES, errno.EPERM, errno.EROFS, errno.ENOSPC])
-def test_state_directory_failure_preserves_original_exception(workbench_api, tmp_path, code):
+ "code", [errno.EACCES, errno.EPERM, errno.EROFS, errno.ENOSPC, errno.EEXIST]
+)
+def test_state_directory_failure_preserves_original_exception(
+ workbench_api, tmp_path, code, capsys
+):
error = OSError(code, os.strerror(code), str(tmp_path / "state"))
connect = workbench_api["connect"]
with (
@@ -109,11 +113,14 @@ def test_state_directory_failure_preserves_original_exception(workbench_api, tmp
connect()
assert str(failure.value) == str(error)
assert failure.value is error
- assert getattr(error, "_codex_security_state_unavailable", False) == (code != errno.ENOSPC)
+ detail = capsys.readouterr().err
+ assert str(tmp_path / "state" / "workbench.sqlite3") in detail
+ assert "SQLite journal files" in detail
+ assert "CODEX_SECURITY_STATE_DIR" in detail
@pytest.mark.parametrize("during_open", [True, False])
-def test_state_open_failure_metadata_is_limited_to_sqlite_connect(
+def test_state_open_or_migration_failure_preserves_original_exception(
workbench_api, tmp_path, during_open, capsys
):
error = sqlite3.OperationalError("unable to open database file")
@@ -139,7 +146,6 @@ def test_state_open_failure_metadata_is_limited_to_sqlite_connect(
connect()
assert failure.value is error
assert str(error) == "unable to open database file"
- assert getattr(error, "_codex_security_state_unavailable", False) is during_open
detail = capsys.readouterr().err
assert str(tmp_path / "state" / "workbench.sqlite3") in detail
assert "SQLite journal files" in detail
diff --git a/sdk/typescript/README.md b/sdk/typescript/README.md
index b78577840..ff23f23b3 100644
--- a/sdk/typescript/README.md
+++ b/sdk/typescript/README.md
@@ -240,6 +240,11 @@ stops before model work with the plugin upgrade message.
When no provider is selected, discovery, reducer, and resumed workers inherit the
same native configuration as the parent.
+Custom plugins must support the current workbench protocol for scan comparison
+and archival. Update an older custom plugin or omit `pluginPath` to use the
+bundled version. The SDK no longer adapts payloads or archives scan directories
+on behalf of older workbench implementations.
+
Scans use an isolated Codex configuration. See
[runtime configuration](docs/cli.md#runtime-configuration-and-worker-limits)
for supported overrides and defaults.
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index 2ca651fcf..761f9233c 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -1611,18 +1611,13 @@ else:
print(json.dumps(result, allow_nan=False, sort_keys=True))
`;
-const workbenchComparisonSupport = new Map<
- string,
- { stdin: boolean; related: boolean }
->();
-
export async function runWorkbench(
options: Omit<WorkbenchCommandOptions, "python"> & { python?: string },
args: readonly string[],
input?: string,
): Promise<JsonObject> {
const script = join(options.pluginRoot, "scripts", "workbench_db.py");
- let signal = options.signal;
+ const signal = options.signal;
const run = async (
arguments_: readonly string[],
input?: string,
@@ -1723,136 +1718,14 @@ export async function runWorkbench(
return result.stdout;
};
let stdout: string;
- const savedScanIdentities = async (scanDir: string): Promise<string> => {
- const result: unknown = JSON.parse(
- await run(["list-scans", "--scan-root", scanDir]),
- );
- if (!isRecord(result) || !Array.isArray(result["scans"])) {
- throw new Error(
- "The workbench returned an invalid scan history response.",
- );
- }
- return JSON.stringify(
- result["scans"]
- .map((scan: unknown) => {
- if (
- !isRecord(scan) ||
- typeof scan["scanId"] !== "string" ||
- typeof scan["scanDir"] !== "string"
- ) {
- throw new Error(
- "The workbench returned an invalid scan history entry.",
- );
- }
- return JSON.stringify([scan["scanId"], scan["scanDir"]]);
- })
- .sort(),
- );
- };
- let legacyArchive:
- { scanDir: string; archiveDir: string; savedScans: string } | undefined;
try {
- const arguments_ = [...args];
- let archiveHandshake = false;
- if (
- arguments_[0] === "register-cli-scan" &&
- arguments_.includes("--archive-existing") &&
- !arguments_.includes("--archived-scan-dir")
- ) {
- const help = (await run(["register-cli-scan", "--help"])).replace(
- /\s+/gu,
- " ",
- );
- if (help.includes("Supports cancellable archival preparation.")) {
- archiveHandshake = signal !== undefined;
- } else if (
- help.includes("Archive output in the registration transaction.")
- ) {
- // Earlier transactional helpers cannot acknowledge the safe cancellation
- // boundary. Keep their move and database commit together.
- signal?.throwIfAborted();
- signal = undefined;
- } else {
- const scanDir = arguments_[arguments_.indexOf("--scan-dir") + 1]!;
- const archiveDir = await planOutputArchive(scanDir);
- if (archiveDir !== null) {
- const stateDirectory = codexSecurityStateDirectory(
- options.environment,
- );
- for (let path = stateDirectory; ; path = dirname(path)) {
- if (isWithin(scanDir, await canonicalConfigPath(path))) {
- throw new Error(
- "The scan artifact directory cannot contain the active workbench database.",
- );
- }
- if (dirname(path) === path) break;
- }
- const savedScans = await savedScanIdentities(scanDir);
- signal?.throwIfAborted();
- await rename(scanDir, archiveDir);
- legacyArchive = { scanDir, archiveDir, savedScans };
- await mkdir(scanDir, { mode: 0o700 });
- if ((process.umask() & 0o700) !== 0) await chmod(scanDir, 0o700);
- arguments_.push("--archived-scan-dir", archiveDir);
- }
- }
- }
- const matchesStdinIndex = arguments_.indexOf("--matches-json-stdin");
- if (
- arguments_[0] === "save-scan-comparison" &&
- matchesStdinIndex !== -1 &&
- input !== undefined
- ) {
- const key = JSON.stringify([options.python, script]);
- let support = workbenchComparisonSupport.get(key);
- if (support === undefined) {
- const help = await run(["save-scan-comparison", "--help"]);
- options.signal?.throwIfAborted();
- support = {
- stdin: help.includes("--matches-json-stdin"),
- related: help
- .replace(/\s+/gu, " ")
- .includes("Comparison payload supports related findings."),
- };
- workbenchComparisonSupport.set(key, support);
- }
- const comparison: unknown = JSON.parse(input);
- if (isRecord(comparison) && "related" in comparison && !support.related) {
- delete comparison["related"];
- input = JSON.stringify(comparison);
- }
- if (!support.stdin) {
- arguments_.splice(matchesStdinIndex, 1, "--matches-json", input);
- input = undefined;
- }
- }
- stdout = await run(arguments_, input, archiveHandshake);
+ const archiveHandshake =
+ signal !== undefined &&
+ args[0] === "register-cli-scan" &&
+ args.includes("--archive-existing") &&
+ !args.includes("--archived-scan-dir");
+ stdout = await run(args, input, archiveHandshake);
} catch (error) {
- if (legacyArchive !== undefined) {
- // Cancellation can stop legacy registration; recovery must still settle.
- signal = undefined;
- try {
- // A helper can commit registration and lose its response. Restore only
- // when saved identities prove that registration did not change them.
- if (
- (await savedScanIdentities(legacyArchive.scanDir)) ===
- legacyArchive.savedScans
- ) {
- await rmdir(legacyArchive.scanDir).catch(
- (error: NodeJS.ErrnoException) => {
- if (error.code !== "ENOENT") throw error;
- },
- );
- await rename(legacyArchive.archiveDir, legacyArchive.scanDir);
- }
- } catch (restoreError) {
- throw new AggregateError(
- [error, restoreError],
- `Scan registration failed: ${processErrorDetail(error)}. Previous output remains at ${legacyArchive.archiveDir}; restoration could not be verified or completed: ${processErrorDetail(restoreError)}`,
- { cause: error },
- );
- }
- }
if (options.signal?.aborted) throw error;
const detail = processErrorDetail(error);
const failure =
@@ -1873,8 +1746,6 @@ export async function runWorkbench(
"The Codex Security workbench returned an invalid response.",
);
}
- if (legacyArchive !== undefined)
- result["archivedScanDir"] = legacyArchive.archiveDir;
return result as JsonObject;
}
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index 636fb9a59..f21ff46b1 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -2317,7 +2317,7 @@ describe("CodexSecurity orchestration", () => {
);
test.each(
- (["preparation", "empty", "legacy", "commit", "rollback"] as const).flatMap(
+ (["preparation", "empty", "commit", "rollback"] as const).flatMap(
(boundary) =>
(["real", "mock", "import"] as const).flatMap((mode) =>
(mode === "import"
@@ -2404,13 +2404,6 @@ describe("CodexSecurity orchestration", () => {
" connection.recv(1)",
"def main(**kwargs):",
" sys.stdout.reconfigure(newline='\\r\\n')",
- ...(boundary === "legacy"
- ? [
- " if sys.argv[1:3] == ['register-cli-scan', '--help']:",
- " print('--archive-existing --archived-scan-dir')",
- " return",
- ]
- : []),
" if sys.argv[1:2] == ['register-cli-scan'] and '--help' not in sys.argv:",
...(mode === "real"
? [
diff --git a/sdk/typescript/tests-ts/import-scan.test.ts b/sdk/typescript/tests-ts/import-scan.test.ts
index 23d3375f6..1d0f252a3 100644
--- a/sdk/typescript/tests-ts/import-scan.test.ts
+++ b/sdk/typescript/tests-ts/import-scan.test.ts
@@ -1,6 +1,5 @@
import { execFileSync } from "node:child_process";
import {
- cp,
mkdir,
mkdtemp,
readFile,
@@ -134,30 +133,6 @@ async function fixture(format: "csv" | "json" = "csv") {
};
}
-async function legacyArchivePlugin(
- context: Awaited<ReturnType<typeof fixture>>,
-) {
- const plugin = join(context.root, "legacy-plugin");
- await cp(PLUGIN_ROOT, plugin, { recursive: true });
- await writeFile(
- join(plugin, "scripts", "workbench_db.py"),
- [
- "import runpy, sys",
- "from pathlib import Path",
- "args = sys.argv[1:]",
- 'if args[:2] == ["register-cli-scan", "--help"]:',
- ' print("--archive-existing --archived-scan-dir")',
- " raise SystemExit(0)",
- 'if args and args[0] == "register-cli-scan":',
- ' scan_dir = Path(args[args.index("--scan-dir") + 1])',
- " if next(scan_dir.iterdir(), None) is not None:",
- ' raise SystemExit("The scan artifact directory must be empty before the scan starts.")',
- `runpy.run_path(${JSON.stringify(join(PLUGIN_ROOT, "scripts", "workbench_db.py"))}, run_name="__main__")`,
- ].join("\n"),
- );
- return plugin;
-}
-
function completed(result: Awaited<ReturnType<typeof importScan>>): ScanResult {
expect(result).toBeInstanceOf(ScanResult);
if (!(result instanceof ScanResult)) throw new Error("Expected a saved scan");
@@ -603,49 +578,38 @@ test("dry run validates source rows without creating database state or invoking
});
});
-test.each(["bundled", "legacy"])(
- "imports with %s helpers archive prior output without replacing its saved findings",
- async (helper) => {
- const context = await fixture();
- const options = {
- ...context.options,
- ...(helper === "legacy"
- ? {
- config: {
- ...context.options.config,
- pluginPath: await legacyArchivePlugin(context),
- },
- }
- : {}),
- outputDir: join(context.root, "results"),
- };
- const first = completed(await importScan(options, context.dependencies));
- const originalManifest = await readFile(first.manifestPath, "utf8");
- await expect(importScan(options, context.dependencies)).rejects.toThrow();
- expect(await readFile(first.manifestPath, "utf8")).toBe(originalManifest);
- const second = completed(
- await importScan(
- { ...options, archiveExisting: true },
- context.dependencies,
- ),
- );
- const scans = await storedScans(context);
- expect(scans).toHaveLength(2);
- const archived = scans.find((scan) => scan.id === first.manifest.scan.id)!;
- expect(archived.scan_dir).not.toBe(second.scanDir);
- expect(archived.occurrence_count).toBe(2);
- expect(
- await readFile(join(archived.scan_dir, "scan-manifest.json"), "utf8"),
- ).toBe(originalManifest);
- expect(
- (await loadContract(archived.scan_dir, { pluginRoot: PLUGIN_ROOT }))
- .findings.findings,
- ).toHaveLength(2);
- },
-);
+test("imports archive prior output without replacing its saved findings", async () => {
+ const context = await fixture();
+ const options = {
+ ...context.options,
+ outputDir: join(context.root, "results"),
+ };
+ const first = completed(await importScan(options, context.dependencies));
+ const originalManifest = await readFile(first.manifestPath, "utf8");
+ await expect(importScan(options, context.dependencies)).rejects.toThrow();
+ expect(await readFile(first.manifestPath, "utf8")).toBe(originalManifest);
+ const second = completed(
+ await importScan(
+ { ...options, archiveExisting: true },
+ context.dependencies,
+ ),
+ );
+ const scans = await storedScans(context);
+ expect(scans).toHaveLength(2);
+ const archived = scans.find((scan) => scan.id === first.manifest.scan.id)!;
+ expect(archived.scan_dir).not.toBe(second.scanDir);
+ expect(archived.occurrence_count).toBe(2);
+ expect(
+ await readFile(join(archived.scan_dir, "scan-manifest.json"), "utf8"),
+ ).toBe(originalManifest);
+ expect(
+ (await loadContract(archived.scan_dir, { pluginRoot: PLUGIN_ROOT }))
+ .findings.findings,
+ ).toHaveLength(2);
+});
test.each(["root", "child", "linked", "inverse", "aliased", "aliased-child"])(
- "legacy archive preserves the active workbench at a %s state path",
+ "archival preserves the active workbench at a %s state path",
async (layout) => {
const context = await fixture();
const outputDir = join(context.root, "results");
@@ -700,13 +664,10 @@ test.each(["root", "child", "linked", "inverse", "aliased", "aliased-child"])(
join(repository, "example.ts"),
"export const value = 1;\n",
);
- const client = new TestClient(
- {
- ...context.options.config,
- pluginPath: await legacyArchivePlugin(context),
- },
- { ...context.dependencies, runWorkbench },
- );
+ const client = new TestClient(context.options.config!, {
+ ...context.dependencies,
+ runWorkbench,
+ });
try {
await expect(
client.run(repository, {
@@ -714,7 +675,7 @@ test.each(["root", "child", "linked", "inverse", "aliased", "aliased-child"])(
outputDir,
archiveExisting: true,
}),
- ).rejects.toThrow("active workbench database");
+ ).rejects.toThrow("workbench state or active database");
const saved = await runWorkbench(context.workbenchOptions, [
"get-scan",
"--scan-id",
@@ -733,15 +694,11 @@ test.each(["root", "child", "linked", "inverse", "aliased", "aliased-child"])(
);
test.each(["active scan", "missing parent"])(
- "legacy registration restores output after rejecting %s",
+ "registration preserves output when rejecting %s",
async (reason) => {
const context = await fixture();
const options = {
...context.options,
- config: {
- ...context.options.config,
- pluginPath: await legacyArchivePlugin(context),
- },
outputDir: join(context.root, "results"),
};
const first = completed(await importScan(options, context.dependencies));
@@ -778,58 +735,41 @@ test.each(["active scan", "missing parent"])(
},
);
-test.each(["response lost", "state unreadable"])(
- "legacy registration preserves its archive when %s after commit",
- async (failure) => {
- const context = await fixture();
- const pluginPath = await legacyArchivePlugin(context);
- const options = {
- ...context.options,
- config: { ...context.options.config, pluginPath },
- outputDir: join(context.root, "results"),
- };
- const first = completed(await importScan(options, context.dependencies));
- const manifest = await readFile(first.manifestPath, "utf8");
- const helper = join(pluginPath, "scripts", "workbench_db.py");
- const original = await readFile(helper, "utf8");
- await writeFile(
- helper,
- original
- .replace(
- "import runpy, sys",
- [
- "import contextlib, io, runpy, sys",
- "from pathlib import Path",
- "args = sys.argv[1:]",
- ...(failure === "state unreadable"
- ? [
- 'if args and args[0] == "list-scans" and Path(__file__).with_suffix(".committed").exists():',
- ' raise SystemExit("Synthetic state read failure")',
- ]
- : []),
- ].join("\n"),
- )
- .replace(
- "runpy.run_path(",
- 'lost_response = args and args[0] == "register-cli-scan"\nwith contextlib.redirect_stdout(io.StringIO()) if lost_response else contextlib.nullcontext():\n runpy.run_path(',
- ) +
- '\nif lost_response:\n Path(__file__).with_suffix(".committed").touch()\n raise SystemExit("Synthetic response lost after commit")\n',
- );
- await expect(
- importScan({ ...options, archiveExisting: true }, context.dependencies),
- ).rejects.toThrow("Synthetic response lost after commit");
- const scans = await storedScans(context);
- expect(scans).toHaveLength(2);
- const archived = scans.find((scan) => scan.id === first.manifest.scan.id)!;
- expect(archived.scan_dir).not.toBe(first.scanDir);
- expect(
- await readFile(join(archived.scan_dir, "scan-manifest.json"), "utf8"),
- ).toBe(manifest);
- expect(
- await stat(join(first.scanDir, "scan-manifest.json")).catch(() => null),
- ).toBeNull();
- },
-);
+test("registration preserves archived output when its response is lost after commit", async () => {
+ const context = await fixture();
+ const options = {
+ ...context.options,
+ outputDir: join(context.root, "results"),
+ };
+ const first = completed(await importScan(options, context.dependencies));
+ const manifest = await readFile(first.manifestPath, "utf8");
+ const loseRegistrationReply: typeof runWorkbench = async (
+ options,
+ args,
+ input,
+ ) => {
+ const result = await runWorkbench(options, args, input);
+ if (args[0] === "register-cli-scan")
+ throw new Error("Synthetic response lost after commit");
+ return result;
+ };
+ await expect(
+ importScan(
+ { ...options, archiveExisting: true },
+ { ...context.dependencies, runWorkbench: loseRegistrationReply },
+ ),
+ ).rejects.toThrow("Synthetic response lost after commit");
+ const scans = await storedScans(context);
+ expect(scans).toHaveLength(2);
+ const archived = scans.find((scan) => scan.id === first.manifest.scan.id)!;
+ expect(archived.scan_dir).not.toBe(first.scanDir);
+ expect(
+ await readFile(join(archived.scan_dir, "scan-manifest.json"), "utf8"),
+ ).toBe(manifest);
+ expect(
+ await stat(join(first.scanDir, "scan-manifest.json")).catch(() => null),
+ ).toBeNull();
+});
test.each(["failure", "abort"] as const)(
"an import %s after registration leaves a terminal saved scan",
diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.ts
index 4261a3012..14bd734ea 100644
--- a/sdk/typescript/tests-ts/runtime.test.ts
+++ b/sdk/typescript/tests-ts/runtime.test.ts
@@ -5514,126 +5514,54 @@ describe("runtime directories and plugin Python boundary", () => {
},
);
- test.each([
- ["legacy", "0.1.22", false, false, undefined],
- ["previous", "0.1.37", true, false, undefined],
- ["independent version", "1.0.0", true, false, undefined],
- ["development", "dev", true, true, undefined],
- ["current", BUNDLED_PLUGIN_VERSION, true, true, undefined],
- ["narrow-terminal", BUNDLED_PLUGIN_VERSION, true, true, "40"],
- ] as const)(
- "saves comparisons with a %s custom plugin",
- async (_kind, version, supportsStdin, supportsRelated, columns) => {
- const root = await temporaryDirectory();
- const pluginRoot = join(root, "custom plugin");
- const scripts = join(pluginRoot, "scripts");
- await mkdir(scripts, { recursive: true });
- await mkdir(join(pluginRoot, ".codex-plugin"));
- await writeFile(
- join(pluginRoot, ".codex-plugin", "plugin.json"),
- JSON.stringify({ name: "codex-security", version }),
- );
- await writeFile(
- join(scripts, "workbench_db.py"),
- [
- "import argparse, json, os, sys",
- "from pathlib import Path",
- "if '--help' in sys.argv:",
- " with Path(__file__).with_name('help-calls').open('ab') as calls: calls.write(b'help\\n')",
- " if os.environ.get('FAIL_COMPARISON_HELP'): sys.exit('Synthetic help failure')",
- "parser = argparse.ArgumentParser()",
- `command = parser.add_subparsers(dest='command', required=True).add_parser('save-scan-comparison', description=${supportsRelated ? "'Comparison payload supports related findings.'" : "None"})`,
- "command.add_argument('--before-scan-id', required=True)",
- "command.add_argument('--after-scan-id', required=True)",
- ...(supportsStdin
- ? [
- "transport = command.add_mutually_exclusive_group(required=True)",
- "transport.add_argument('--matches-json')",
- "transport.add_argument('--matches-json-stdin', action='store_true')",
- ]
- : ["command.add_argument('--matches-json', required=True)"]),
- "args = parser.parse_args()",
- "uses_stdin = getattr(args, 'matches_json_stdin', False)",
- "payload = json.loads(sys.stdin.buffer.read().decode('utf-8') if uses_stdin else args.matches_json)",
- ...(supportsRelated
- ? []
- : [
- "if 'related' in payload: sys.exit('Unsupported comparison fields')",
- ]),
- "print(json.dumps({'payload': payload, 'usesStdin': uses_stdin}))",
- ].join("\n"),
- );
- const python = await resolvePluginPython();
- const options = {
- python,
- pluginRoot,
- environment: {
- PATH: process.env["PATH"],
- ...(columns === undefined ? {} : { COLUMNS: columns }),
- OPENAI_API_KEY: "synthetic-openai-key",
- CODEX_API_KEY: "synthetic-codex-key",
- OPENROUTER_API_KEY: "synthetic-openrouter-key",
- FIREWORKS_API_KEY: "synthetic-fireworks-key",
+ test("saves comparison payloads unchanged through the current stdin protocol", async () => {
+ const root = await temporaryDirectory();
+ const pluginRoot = join(root, "custom plugin");
+ const scripts = join(pluginRoot, "scripts");
+ await mkdir(scripts, { recursive: true });
+ await writeFile(
+ join(scripts, "workbench_db.py"),
+ [
+ "import argparse, json, sys",
+ "parser = argparse.ArgumentParser()",
+ "command = parser.add_subparsers(dest='command', required=True).add_parser('save-scan-comparison')",
+ "command.add_argument('--before-scan-id', required=True)",
+ "command.add_argument('--after-scan-id', required=True)",
+ "command.add_argument('--matches-json-stdin', action='store_true', required=True)",
+ "args = parser.parse_args()",
+ "print(json.dumps({'payload': json.load(sys.stdin), 'usesStdin': args.matches_json_stdin}))",
+ ].join("\n"),
+ );
+ const payload = {
+ matches: [],
+ uncertain: [],
+ related: [
+ {
+ beforeOccurrenceId: "before",
+ afterOccurrenceId: "after",
+ reason: "Separate synthetic controls. 🙂",
},
- };
- const original = {
- matches: [
- {
- beforeOccurrenceIds: ["before"],
- afterOccurrenceIds: ["after"],
- confidence: "high",
- reason: "Same synthetic control.",
- },
- ],
- uncertain: [
- {
- beforeOccurrenceId: "uncertain-before",
- afterOccurrenceId: "uncertain-after",
- reason: "Needs more evidence.",
- },
- ],
- related: [
- {
- beforeOccurrenceId: "related-before",
- afterOccurrenceId: "related-after",
- reason: "Separate synthetic controls. 🙂",
- },
+ ],
+ };
+ expect(
+ await runWorkbench(
+ {
+ python: await resolvePluginPython(),
+ pluginRoot,
+ environment: { PATH: process.env["PATH"] },
+ },
+ [
+ "save-scan-comparison",
+ "--before-scan-id",
+ "before-scan",
+ "--after-scan-id",
+ "after-scan",
+ "--matches-json-stdin",
],
- };
- const args = [
- "save-scan-comparison",
- "--before-scan-id",
- "before-scan",
- "--after-scan-id",
- "after-scan",
- "--matches-json-stdin",
- ];
- const input = JSON.stringify(original);
- await expect(
- runWorkbench(
- {
- ...options,
- environment: { ...options.environment, FAIL_COMPARISON_HELP: "1" },
- },
- args,
- input,
- ),
- ).rejects.toThrow("Synthetic help failure");
- const expected = {
- usesStdin: supportsStdin,
- payload: supportsRelated
- ? original
- : { matches: original.matches, uncertain: original.uncertain },
- };
- expect(await runWorkbench(options, args, input)).toEqual(expected);
- expect(await runWorkbench(options, args, input)).toEqual(expected);
- expect(await readFile(join(scripts, "help-calls"), "utf8")).toBe(
- "help\nhelp\n",
- );
- expect(args.at(-1)).toBe("--matches-json-stdin");
- expect(JSON.parse(input)).toEqual(original);
- },
- );
+ JSON.stringify(payload),
+ ),
+ ).toEqual({ payload, usesStdin: true });
+ });
test("upgrades colliding legacy execution-profile and public CLI migrations", async () => {
const root = await temporaryDirectory("codex-security-legacy-migrations-");