OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(deep-scan): match user context before reusing discovery (#1048)
mldangelo-oai826c5562385f
diff --git a/plugins/codex-security/mcp-app/src/workbench/migrations.ts b/plugins/codex-security/mcp-app/src/workbench/migrations.tsindex 0ea0324c3..c85cb85b3 100644--- a/plugins/codex-security/mcp-app/src/workbench/migrations.ts+++ b/plugins/codex-security/mcp-app/src/workbench/migrations.ts@@ -421,7 +421,7 @@ export function applyMigrations();} else if (item.version === 16) backfill = repairStableTargets(database);else if (applied.has(item.version)) {- if ([2, 12, 13, 26, 28, 31, 32].includes(item.version))+ if ([2, 12, 13, 26, 28, 31, 32, 47].includes(item.version))repairAdditive(database, item.version);else if (item.version === 11) repairDeepScan(database);} else {diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_context_snapshot_cases.ts b/plugins/codex-security/mcp-app/tests/deep_scan_context_snapshot_cases.tsnew file mode 100644index 000000000..ce3e0aff5--- /dev/null+++ b/plugins/codex-security/mcp-app/tests/deep_scan_context_snapshot_cases.ts@@ -0,0 +1,216 @@+import assert from "node:assert/strict";+import { spawnSync } from "node:child_process";+import { randomUUID } from "node:crypto";+import { mkdir, readFile, writeFile } from "node:fs/promises";+import path from "node:path";+import { fileURLToPath } from "node:url";+import { importModule } from "./import-module.ts";+import type { WorkbenchDeepScanStore as Store } from "../src/deep-scan/store.js";+import type { DeepScanWorkerRunner as Runner } from "../src/deep-scan/worker-runner.js";+import type { createDeepScanArtifacts as Artifacts } from "../src/deep-scan/artifacts.js";+import type { DeepWorkerParentSandbox } from "../src/deep-scan/parent-sandbox.js";+import type { CodexSdkWorkerExecutor as WorkerExecutor } from "../src/deep-scan/executor.js";+import type {+ CodexWorkerRequest,+ DeepScanWorkerKind,+} from "../src/deep-scan/types.js";++export async function testDeepScanContextSnapshots({+ CodexSdkWorkerExecutor,+ fakeCodexFixture,+ parentSandbox,+}: {+ CodexSdkWorkerExecutor: typeof WorkerExecutor;+ fakeCodexFixture: () => Promise<{ root: string; executablePath: string }>;+ parentSandbox: DeepWorkerParentSandbox;+}) {+ const pluginRoot = fileURLToPath(new URL("../../", import.meta.url));+ const {+ WorkbenchDeepScanStore,+ DeepScanWorkerRunner,+ createDeepScanArtifacts,+ } = (await importModule({+ stdin: {+ contents: [+ 'export { WorkbenchDeepScanStore } from "./store.ts";',+ 'export { DeepScanWorkerRunner } from "./worker-runner.ts";',+ 'export { createDeepScanArtifacts } from "./artifacts.ts";',+ ].join("\n"),+ resolveDir: path.join(pluginRoot, "mcp-app/src/deep-scan"),+ },+ loader: { ".md": "text" },+ })) as {+ WorkbenchDeepScanStore: typeof Store;+ DeepScanWorkerRunner: typeof Runner;+ createDeepScanArtifacts: typeof Artifacts;+ };+ const fixture = await fakeCodexFixture();+ // Concurrent children record their input in their own working directories.+ await writeFile(+ fixture.executablePath,+ (await readFile(fixture.executablePath, "utf8")).replace(+ "process.env.FAKE_CODEX_MARKER",+ "process.argv[process.argv.indexOf('--cd') + 1] + '/invocation.json'",+ ),+ );+ const previousPath = process.env.CODEX_CLI_PATH;+ process.env.CODEX_CLI_PATH = fixture.executablePath;+ const environment = {+ ...process.env,+ CODEX_HOME: path.join(fixture.root, "home"),+ CODEX_SECURITY_STATE_DIR: path.join(fixture.root, "state"),+ };+ const command = async (args: string[], input?: string) => {+ const result = spawnSync(+ process.env.PYTHON?.trim() || "python3",+ [path.join(pluginRoot, "scripts/workbench_db.py"), ...args],+ { env: environment, input, encoding: "utf8" },+ );+ assert.equal(result.status, 0, result.stderr);+ return JSON.parse(result.stdout);+ };+ try {+ const targetPath = path.join(fixture.root, "target");+ await mkdir(targetPath);+ await writeFile(path.join(targetPath, "fixture.py"), "print('fixture')\n");+ await Promise.all(+ ["Review authentication only.", undefined].map(+ async (userContext, index) => {+ const threadId = `context-owner-${index}`;+ let store = new WorkbenchDeepScanStore(command);+ let run = await store.begin({+ targetPath,+ scope: ".",+ threadId,+ userContext,+ scanRoot: path.join(fixture.root, "scans"),+ });+ const invocations: {+ kind: DeepScanWorkerKind;+ invocation: { argv: string[]; stdin: string };+ }[] = [];+ const sdk = new CodexSdkWorkerExecutor({ parentSandbox });+ const executor = {+ async run(request: CodexWorkerRequest) {+ const result = await sdk.run(request);+ invocations.push({+ kind: request.kind,+ invocation: JSON.parse(+ await readFile(+ path.join(request.workingDirectory, "invocation.json"),+ "utf8",+ ),+ ),+ });+ // A missing result exercises the runner's real SDK resume path.+ if (request.resumeThreadId) {+ await writeFile(+ path.join(request.artifactContext!.root, "result.json"),+ JSON.stringify({+ scanId: run.scanId,+ findings: [],+ ...(request.kind === "discovery"+ ? {+ coverage: {+ completeness: "complete",+ surfaces: [],+ explicitExclusions: [],+ deferred: [],+ },+ }+ : {}),+ }),+ );+ }+ return result;+ },+ };+ const runner = () =>+ new DeepScanWorkerRunner({+ run,+ store,+ executor,+ artifacts: createDeepScanArtifacts(run.scanDir),+ pluginRoot,+ clock: { now: Date.now, sleep: async () => {} },+ random: () => 0,+ log: () => {},+ retryDelaysMs: [0],+ signal: new AbortController().signal,+ });+ const first = await runner().runDiscoveryWorker(+ randomUUID(),+ "before-edit",+ );+ assert.ok(first.status === "succeeded");+ const editedContext = `Updated instructions for later phases ${index}.`;+ await command([+ "update-scan-context",+ "--scan-id",+ run.scanId,+ "--thread-id",+ threadId,+ "--user-context",+ editedContext,+ ]);+ // Reload through a new store to model a coordinator restart after editing.+ store = new WorkbenchDeepScanStore(command);+ run = await store.begin({+ scanId: run.scanId,+ threadId,+ scanRoot: path.join(fixture.root, "scans"),+ });+ const second = await runner().runDiscoveryWorker(+ randomUUID(),+ "after-edit",+ );+ assert.ok(second.status === "succeeded");+ const reducerId = randomUUID();+ const reduced = await runner().runReducer({+ id: reducerId,+ label: "reducer",+ consumed: [first.worker, second.worker],+ });+ assert.ok(!("status" in reduced), "Reducer did not succeed");+ assert.equal(+ reduced.run.persistedWorkers?.find(+ (worker) => worker.id === reducerId,+ )?.attempt,+ 2,+ );+ assert.equal(invocations.length, 6);+ for (let offset = 0; offset < invocations.length; offset += 2) {+ const fresh = invocations[offset]!;+ const resumed = invocations[offset + 1]!;+ assert.equal(fresh.invocation.argv.includes("resume"), false);+ assert.equal(resumed.invocation.argv.includes("resume"), true);+ const context = JSON.parse(+ fresh.invocation.stdin.match(/```json\n([\s\S]*?)\n```/)![1],+ );+ if (fresh.kind === "discovery") {+ assert.equal(context.userContext, userContext ?? null);+ assert.equal(context.scanId, run.scanId);+ } else {+ assert.equal(Object.hasOwn(context, "userContext"), false);+ assert.deepEqual(context.claimedWorkerIds, [+ first.worker.id,+ second.worker.id,+ ]);+ }+ assert.equal(fresh.invocation.stdin.includes(editedContext), false);+ assert.equal(+ resumed.invocation.stdin.includes(editedContext),+ false,+ );+ assert.match(resumed.invocation.stdin, /record_codex_security_/);+ }+ const current = await command(["get-scan", "--scan-id", run.scanId]);+ assert.equal(current.scan.userContext, editedContext);+ },+ ),+ );+ } finally {+ if (previousPath === undefined) delete process.env.CODEX_CLI_PATH;+ else process.env.CODEX_CLI_PATH = previousPath;+ }+}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.tsindex d837147c6..af59fb837 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@@ -29,6 +29,7 @@ import type {CodexWorkerRequest,DeepScanWorkerKind,} from "../src/deep-scan/types.js";+import { testDeepScanContextSnapshots } from "./deep_scan_context_snapshot_cases.ts";const executorSource = new URL("../src/deep-scan/executor.ts", import.meta.url);@@ -96,6 +97,11 @@ try {await testWorkerCyberAccessSettings();await testCompletedWorkerSettlesWithoutWaitingForProcessExit();if (process.platform !== "win32") {+ await testDeepScanContextSnapshots({+ CodexSdkWorkerExecutor,+ fakeCodexFixture,+ parentSandbox: trustedParentSandbox,+ });await testMissingParentSandboxFailsBeforeWorkerLaunch();await testDisallowedWorkerProfileFailsBeforeWorkerLaunch();await testRuntimePermissionProfileFallbackStopsAndDiscards();diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_database.ts b/plugins/codex-security/mcp-app/tests/test_workbench_database.tsindex dcc42877e..99d675b97 100644--- a/plugins/codex-security/mcp-app/tests/test_workbench_database.ts+++ b/plugins/codex-security/mcp-app/tests/test_workbench_database.ts@@ -495,7 +495,7 @@ test("recorded additive migrations restore missing columns and configured errordatabase.exec(`INSERT INTO deep_scan_runs (scan_id, schema_version, workflow_version, status, phase,workers, subagents, stop_after_no_new, max_discovery_runs, created_at, updated_at)VALUES ('scan', 1, 'v1', 'running', 'discovery', 1, 0, 7, 10, 'created', 'updated')`);- for (const version of [27, 28, 31, 32])+ for (const version of [27, 28, 31, 32, 47])database.prepare("INSERT INTO schema_migrations VALUES (?, ?, 'original')").run(version, migrations[version - 1].name);@@ -506,7 +506,14 @@ test("recorded additive migrations restore missing columns and configured error.get()?.stop_after_consecutive_errors,7,);- database.exec("UPDATE deep_scan_runs SET stop_after_consecutive_errors = 2");+ assert.equal(+ database+ .prepare("SELECT discovery_user_context_json FROM deep_scan_runs")+ .get()?.discovery_user_context_json,+ null,+ );+ database.exec(`UPDATE deep_scan_runs SET stop_after_consecutive_errors = 2,+ discovery_user_context_json = '"Original discovery context"'`);applyMigrations(database);assert.equal(database@@ -514,6 +521,12 @@ test("recorded additive migrations restore missing columns and configured error.get()?.stop_after_consecutive_errors,2,);+ assert.equal(+ database+ .prepare("SELECT discovery_user_context_json FROM deep_scan_runs")+ .get()?.discovery_user_context_json,+ '"Original discovery context"',+ );});test("workflow and review checkpoints upgrade atomically with their preserved result data", (t) => {diff --git a/plugins/codex-security/scripts/deep_scan_workbench.py b/plugins/codex-security/scripts/deep_scan_workbench.pyindex d1d0b2dca..ea9b2af7b 100644--- a/plugins/codex-security/scripts/deep_scan_workbench.py+++ b/plugins/codex-security/scripts/deep_scan_workbench.py@@ -371,7 +371,11 @@ def deep_scan_state(connection: sqlite3.Connection, scan_id: str) -> dict[str, A"scanId": run["scan_id"],"targetPath": scan["target_path"],"scope": scan["scope"],- "userContext": scan["user_context"],+ "userContext": (+ json.loads(run["discovery_user_context_json"])+ if run["discovery_user_context_json"] is not None+ else scan["user_context"]+ ),"scanDir": scan["scan_dir"],"schemaVersion": run["schema_version"],"workflowVersion": run["workflow_version"],@@ -500,9 +504,9 @@ def ensure_deep_scan_run(INSERT INTO deep_scan_runs (scan_id, schema_version, workflow_version, status, phase,workers, subagents, stop_after_no_new, stop_after_consecutive_errors,- max_discovery_runs, max_time_hours,+ max_discovery_runs, max_time_hours, discovery_user_context_json,created_at, updated_at- ) VALUES (?, 1, ?, 'running', 'setup', ?, ?, ?, ?, ?, ?, ?, ?)+ ) VALUES (?, 1, ?, 'running', 'setup', ?, ?, ?, ?, ?, ?, ?, ?, ?)""",(scan["id"],@@ -513,6 +517,7 @@ def ensure_deep_scan_run(config["stopAfterConsecutiveErrors"],config["maxDiscoveryRuns"],config["maxTimeHours"],+ json.dumps(scan["user_context"]),timestamp,timestamp,),@@ -546,6 +551,7 @@ def terminal_deep_scan_for_target_snapshot(thread_id: str,target_path: str,scope: str,+ user_context: str | None,revision: str,snapshot_digest: str,target_device: int | str,@@ -555,7 +561,8 @@ def terminal_deep_scan_for_target_snapshot(A continuation may safely consume a finished coordinator manifest while theparent scan is still open. It must not adopt live orchestration owned by a- different thread, or reuse results after the repository snapshot changed.+ different thread, or reuse results for a different repository snapshot or+ original discovery context."""return connection.execute("""@@ -565,6 +572,7 @@ def terminal_deep_scan_for_target_snapshot(JOIN workspaces ON workspaces.id = scans.workspace_idWHERE scans.target_path = ?AND scans.scope = ?+ AND deep_scan_runs.discovery_user_context_json = ?AND scans.mode = 'deep'AND scans.status = 'running'AND scans.canceled_at IS NULL@@ -588,6 +596,7 @@ def terminal_deep_scan_for_target_snapshot((target_path,scope,+ json.dumps(user_context),revision,snapshot_digest,target_device,@@ -685,6 +694,7 @@ def begin_deep_scan_for_target(existing = existing_deep_scan_for_target(connection, thread_id, target_path, scope)if existing is not None:return begin_deep_scan_for_scan(connection, existing["id"], thread_id, args)+ user_context = user_context_argument(args)target_metadata = target.stat()target_identity = dependencies().scan_target_identity(target, None, metadata=target_metadata)revision, target_snapshot_digest, target_device, target_inode = target_identity@@ -726,6 +736,7 @@ def begin_deep_scan_for_target(thread_id,target_path,scope,+ user_context,revision,target_snapshot_digest,target_device,@@ -747,7 +758,6 @@ def begin_deep_scan_for_target(if target_root == target or target in target_root.parents:raise SystemExit("The scan artifact directory must be outside the selected target.")create_private_directory(target_root)- user_context = user_context_argument(args)model = optional_text(args.model, maximum=200)reasoning_effort = optional_text(args.reasoning_effort, maximum=32)workspace_id = str(uuid.uuid4())diff --git a/plugins/codex-security/scripts/workbench_schema.py b/plugins/codex-security/scripts/workbench_schema.pyindex c157a3593..c1b40dcc8 100644--- a/plugins/codex-security/scripts/workbench_schema.py+++ b/plugins/codex-security/scripts/workbench_schema.py@@ -75,7 +75,7 @@ def apply_migrations(elif version == 16:should_backfill_targets = repair_stable_targets_migration(connection)elif version in applied:- if version in (2, 12, 13, 26, 28, 31, 32):+ if version in (2, 12, 13, 26, 28, 31, 32, 47):repair_additive_migration(connection, version)elif version == 11:repair_deep_scan_migration(connection)diff --git a/plugins/codex-security/shared/workbench-migrations.json b/plugins/codex-security/shared/workbench-migrations.jsonindex 7f6d5b288..e737315d4 100644--- a/plugins/codex-security/shared/workbench-migrations.json+++ b/plugins/codex-security/shared/workbench-migrations.json@@ -386,5 +386,12 @@"statements": ["CREATE TRIGGER invalidate_local_finding_embedding\nAFTER UPDATE OF details_json ON findings\nWHEN OLD.details_json IS NOT NEW.details_json\nBEGIN\n DELETE FROM local_finding_embeddings WHERE finding_id = NEW.id;\nEND;"]+ },+ {+ "version": 47,+ "name": "snapshot deep scan discovery context",+ "statements": [+ "ALTER TABLE deep_scan_runs ADD COLUMN discovery_user_context_json TEXT;"+ ]}]diff --git a/plugins/codex-security/tests/test_deep_scan_request_identity.py b/plugins/codex-security/tests/test_deep_scan_request_identity.pynew file mode 100644index 000000000..8914a2262--- /dev/null+++ b/plugins/codex-security/tests/test_deep_scan_request_identity.py@@ -0,0 +1,250 @@+from __future__ import annotations++import json+import sqlite3+from pathlib import Path++import pytest+from test_workbench_deep_scan import begin_target_scan+from workbench_test_support import mark_deep_coordinator_succeeded, run_workbench+++ ("first_context", "next_context"),+ (+ ("Review authentication only.", "Review SQL injection only."),+ ("Review authentication only.", None),+ (None, "Review SQL injection only."),+ ),+)+def test_target_request_does_not_reuse_different_user_context(+ tmp_path: Path, first_context: str | None, next_context: str | None+) -> None:+ state_dir = tmp_path / "state"+ codex_home = tmp_path / "codex-home"+ target = tmp_path / "target"+ target.mkdir()+ scan_root = tmp_path / "scans"+ first = begin_target_scan(state_dir, codex_home, target, scan_root, user_context=first_context)+ first_scan_id = str(first["deepScan"]["scanId"])+ mark_deep_coordinator_succeeded(+ state_dir, first_scan_id, Path(str(first["deepScan"]["scanDir"]))+ )++ requested = begin_target_scan(+ state_dir,+ codex_home,+ target,+ scan_root,+ thread_id="thread-new-request",+ user_context=next_context,+ )++ assert requested["startDisposition"] == "created"+ assert requested["deepScan"]["scanId"] != first_scan_id+ assert requested["deepScan"]["userContext"] == next_context+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ assert dict(connection.execute("SELECT id, user_context FROM scans")) == {+ first_scan_id: first_context,+ requested["deepScan"]["scanId"]: next_context,+ }+++ ("original_context", "edited_context"),+ (+ ("Review authentication only.", "Review SQL injection only."),+ ("Review authentication only.", None),+ (None, "Review SQL injection only."),+ ),+)[email protected]("request_original", (False, True))+def test_terminal_reuse_matches_discovery_context_after_edit(+ tmp_path: Path,+ original_context: str | None,+ edited_context: str | None,+ request_original: bool,+) -> None:+ state_dir, codex_home = tmp_path / "state", tmp_path / "codex-home"+ target, scan_root = tmp_path / "target", tmp_path / "scans"+ target.mkdir()+ first = begin_target_scan(+ state_dir, codex_home, target, scan_root, user_context=original_context+ )["deepScan"]+ scan_id = str(first["scanId"])+ mark_deep_coordinator_succeeded(state_dir, scan_id, Path(str(first["scanDir"])))+ edited = run_workbench(+ state_dir,+ "update-scan-context",+ "--scan-id",+ scan_id,+ "--thread-id",+ "thread-deep-scan",+ "--user-context-stdin",+ input_text=edited_context or "",+ environment={"CODEX_HOME": str(codex_home)},+ )+ assert edited["scan"]["userContext"] == edited_context++ requested = begin_target_scan(+ state_dir,+ codex_home,+ target,+ scan_root,+ thread_id="thread-new-request",+ user_context=original_context if request_original else edited_context,+ )+ assert requested["startDisposition"] == ("joined" if request_original else "created")+ assert (requested["deepScan"]["scanId"] == scan_id) is request_original+ assert requested["deepScan"]["userContext"] == (+ original_context if request_original else edited_context+ )+ assert (+ run_workbench(state_dir, "get-scan", "--scan-id", scan_id)["scan"]["userContext"]+ == edited_context+ )++[email protected]("original_context", (None, "Review authentication only."))+def test_context_snapshot_survives_owner_join_and_explicit_resume(+ tmp_path: Path, original_context: str | None+) -> None:+ state_dir, codex_home = tmp_path / "state", tmp_path / "codex-home"+ target, scan_root = tmp_path / "target", tmp_path / "scans"+ target.mkdir()+ first = begin_target_scan(+ state_dir, codex_home, target, scan_root, user_context=original_context+ )["deepScan"]+ scan_id = str(first["scanId"])+ run_workbench(+ state_dir,+ "update-scan-context",+ "--scan-id",+ scan_id,+ "--thread-id",+ "thread-deep-scan",+ "--user-context",+ "Use updated context in later phases.",+ )+ for requested in (+ begin_target_scan(state_dir, codex_home, target, scan_root, user_context="New request"),+ run_workbench(+ state_dir,+ "begin-deep-scan",+ "--scan-id",+ scan_id,+ "--thread-id",+ "thread-deep-scan",+ environment={"CODEX_HOME": str(codex_home)},+ ),+ ):+ assert requested["startDisposition"] == "joined"+ assert requested["deepScan"]["scanId"] == scan_id+ assert requested["deepScan"]["userContext"] == original_context++[email protected]("resume_existing", (False, True))+def test_begin_and_resume_after_recorded_deep_scan_tables_are_repaired(+ tmp_path: Path, resume_existing: bool+) -> None:+ state_dir, codex_home = tmp_path / "state", tmp_path / "codex-home"+ target, scan_root = tmp_path / "target", tmp_path / "scans"+ target.mkdir()+ original_context = "Review authentication only."+ first = begin_target_scan(+ state_dir, codex_home, target, scan_root, user_context=original_context+ )["deepScan"]+ database = state_dir / "workbench.sqlite3"+ with sqlite3.connect(database) as connection:+ connection.execute("DROP TABLE deep_scan_dedup_inputs")+ connection.execute("DROP TABLE deep_scan_workers")+ connection.execute("DROP TABLE deep_scan_runs")+ assert connection.execute(+ "SELECT version FROM schema_migrations WHERE version IN (11, 47) ORDER BY version"+ ).fetchall() == [(11,), (47,)]++ thread_id = "thread-deep-scan" if resume_existing else "thread-new-request"+ expected_context = original_context if resume_existing else "Review SQL injection only."+ if resume_existing:+ started = run_workbench(+ state_dir,+ "begin-deep-scan",+ "--scan-id",+ str(first["scanId"]),+ "--thread-id",+ thread_id,+ environment={"CODEX_HOME": str(codex_home)},+ )+ else:+ started = begin_target_scan(+ state_dir,+ codex_home,+ target,+ scan_root,+ thread_id=thread_id,+ user_context=expected_context,+ )+ scan_id = str(started["deepScan"]["scanId"])+ assert started["startDisposition"] == "created"+ assert (scan_id == first["scanId"]) is resume_existing+ assert started["deepScan"]["userContext"] == expected_context+ resumed = run_workbench(+ state_dir,+ "begin-deep-scan",+ "--scan-id",+ scan_id,+ "--thread-id",+ thread_id,+ environment={"CODEX_HOME": str(codex_home)},+ )+ assert resumed["startDisposition"] == "joined"+ assert resumed["deepScan"]["userContext"] == expected_context+ with sqlite3.connect(database) as connection:+ assert connection.execute(+ "SELECT discovery_user_context_json FROM deep_scan_runs WHERE scan_id = ?",+ (scan_id,),+ ).fetchone() == (json.dumps(expected_context),)++[email protected]("migration_recorded", (False, True))[email protected]("current_context", (None, "Review authentication only."))+def test_upgrade_keeps_unknown_discovery_context_resumable_but_not_reusable(+ tmp_path: Path, current_context: str | None, migration_recorded: bool+) -> None:+ state_dir, codex_home = tmp_path / "state", tmp_path / "codex-home"+ target, scan_root = tmp_path / "target", tmp_path / "scans"+ target.mkdir()+ first = begin_target_scan(+ state_dir, codex_home, target, scan_root, user_context=current_context+ )["deepScan"]+ scan_id = str(first["scanId"])+ mark_deep_coordinator_succeeded(state_dir, scan_id, Path(str(first["scanDir"])))+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ connection.execute("ALTER TABLE deep_scan_runs DROP COLUMN discovery_user_context_json")+ if not migration_recorded:+ connection.execute("DELETE FROM schema_migrations WHERE version = 47")+ resumed = run_workbench(+ state_dir,+ "begin-deep-scan",+ "--scan-id",+ scan_id,+ "--thread-id",+ "thread-deep-scan",+ environment={"CODEX_HOME": str(codex_home)},+ )+ assert resumed["startDisposition"] == "joined"+ assert resumed["deepScan"]["userContext"] == current_context+ requested = begin_target_scan(+ state_dir,+ codex_home,+ target,+ scan_root,+ thread_id="thread-new-request",+ user_context=current_context,+ )+ assert requested["startDisposition"] == "created"+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ assert connection.execute(+ "SELECT discovery_user_context_json FROM deep_scan_runs WHERE scan_id = ?",+ (scan_id,),+ ).fetchone() == (None,)diff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.pyindex d78b973c0..6d3fdf864 100644--- a/plugins/codex-security/tests/test_workbench_db.py+++ b/plugins/codex-security/tests/test_workbench_db.py@@ -913,7 +913,7 @@ def test_workbench_persists_progress_and_indexes_completed_findings(tmp_path: Pa)}assert tables == EXPECTED_TABLES- assert connection.execute("SELECT COUNT(*) FROM schema_migrations").fetchone() == (46,)+ assert connection.execute("SELECT COUNT(*) FROM schema_migrations").fetchone() == (47,)assert connection.execute("SELECT COUNT(*) FROM findings").fetchone() == (1,)assert connection.execute("SELECT COUNT(*) FROM finding_locations").fetchone() == (1,)diff --git a/plugins/codex-security/tests/test_workbench_deep_scan.py b/plugins/codex-security/tests/test_workbench_deep_scan.pyindex 491655d8c..38f47c999 100644--- a/plugins/codex-security/tests/test_workbench_deep_scan.py+++ b/plugins/codex-security/tests/test_workbench_deep_scan.py@@ -60,6 +60,7 @@ def begin_target_scan(scan_root: Path,*,thread_id: str = "thread-deep-scan",+ user_context: str | None = None,) -> dict[str, object]:return begin_deep_scan(state_dir,@@ -72,7 +73,9 @@ def begin_target_scan(str(scan_root),"--available-parallelism","16",+ *(("--user-context-stdin",) if user_context is not None else ()),environment=deep_environment(codex_home),+ input_text=user_context,)@@ -1939,8 +1942,10 @@ def test_invalid_discovery_time_limit_fails_before_scan_creation(assert connection.execute("SELECT COUNT(*) FROM scans").fetchone() == (0,)[email protected]("user_context", (None, "Review authentication only."))def test_target_continuation_reuses_terminal_coordinator_across_threads(tmp_path: Path,+ user_context: str | None,) -> None:state_dir = tmp_path / "state"codex_home = tmp_path / "codex-home"@@ -1955,6 +1960,7 @@ def test_target_continuation_reuses_terminal_coordinator_across_threads(target,scan_root,thread_id="thread-before-continuation",+ user_context=user_context,)scan_id = str(first["deepScan"]["scanId"])scan_dir = Path(str(first["deepScan"]["scanDir"]))@@ -1967,10 +1973,12 @@ def test_target_continuation_reuses_terminal_coordinator_across_threads(target,scan_root,thread_id="thread-after-continuation",+ user_context=user_context,)assert continued["startDisposition"] == "joined"assert continued["deepScan"]["scanId"] == scan_idassert continued["deepScan"]["manifestPath"] == str(manifest)+ assert continued["deepScan"]["userContext"] == user_contextassert get_scan(state_dir, scan_id)["scan"]["progress"]["independentReviews"] == {"active": 0,"completed": 0,diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.pyindex c5de157c5..d3c548617 100644--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py@@ -87,6 +87,7 @@ EXPECTED_MIGRATIONS = [(44, "version local finding embedding inputs"),(45, "separate local and service embedding caches"),(46, "invalidate local embeddings when finding bodies change"),+ (47, "snapshot deep scan discovery context"),]