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
refactor(mcp): simplify internal Deep Scan state and finding caches (#1352)
mldangelo-oaia1b84ecff64a
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.ts b/plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.tsindex fccf08af0..f228f3f35 100644--- a/plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.ts+++ b/plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.ts@@ -315,11 +315,6 @@ class AppServerPreflightClient {async close(): Promise<void> {this.closed = true;this.removeAbortListener();- this.pending?.reject(- this.terminalError ??- codexExecutableStdioError(this.options.codexPath, this.options.context),- );- this.pending = undefined;this.stopChild();if (this.childClosed) return;await this.childClose;diff --git a/plugins/codex-security/mcp-app/src/deep-scan/store.ts b/plugins/codex-security/mcp-app/src/deep-scan/store.tsindex 7e4487cbb..3395ab4e0 100644--- a/plugins/codex-security/mcp-app/src/deep-scan/store.ts+++ b/plugins/codex-security/mcp-app/src/deep-scan/store.ts@@ -690,7 +690,6 @@ export function parseDeepScan(result: JsonObject): DeepScanRunState {return {scanId: requiredString(value.scanId, "deepScan.scanId"),status,- phase: deepScanPhase(value.phase),coordinatorGeneration: optionalPositiveInteger(value.coordinatorGeneration),createdAt: optionalString(value.createdAt),updatedAt: optionalString(value.updatedAt),@@ -708,7 +707,6 @@ export function parseDeepScan(result: JsonObject): DeepScanRunState {value.consecutiveErrors ?? 0,"deepScan.consecutiveErrors",),- canonicalArtifacts: parseCanonicalArtifacts(value.canonicalArtifacts),manifestPath: optionalString(value.manifestPath),terminalReason:value.terminalReason === "saturated" || value.terminalReason === "capped"@@ -748,19 +746,6 @@ function parsePersistedDedupInputs(});}-function deepScanPhase(value: unknown): DeepScanRunState["phase"] {- if (value === undefined || value === null) return undefined;- if (- value === "setup" ||- value === "discovery" ||- value === "reducing" ||- value === "terminal"- ) {- return value;- }- throw new Error("Codex Security workbench returned invalid deepScan.phase.");-}-function parsePersistedWorkers(value: unknown): PersistedDeepScanWorker[] {if (value === undefined || value === null) return [];if (!Array.isArray(value)) {@@ -773,23 +758,6 @@ function parsePersistedWorkers(value: unknown): PersistedDeepScanWorker[] {);}-function parseCanonicalArtifacts(- value: unknown,-): DeepScanRunState["canonicalArtifacts"] {- if (value === null || value === undefined) return undefined;- const artifacts = objectValue(value, "deepScan.canonicalArtifacts");- return {- inScopeFilesPath: requiredString(- artifacts.inScopeFilesPath,- "deepScan.canonicalArtifacts.inScopeFilesPath",- ),- candidateLedgerPath: requiredString(- artifacts.candidateLedgerPath,- "deepScan.canonicalArtifacts.candidateLedgerPath",- ),- };-}-function parseWorker(result: JsonObject,workerId: string,diff --git a/plugins/codex-security/mcp-app/src/deep-scan/types.ts b/plugins/codex-security/mcp-app/src/deep-scan/types.tsindex 33f63cbd2..666cd1984 100644--- a/plugins/codex-security/mcp-app/src/deep-scan/types.ts+++ b/plugins/codex-security/mcp-app/src/deep-scan/types.ts@@ -23,15 +23,9 @@ export interface DeepScanConfig {maxTimeHours?: number;}-export interface DeepScanCanonicalArtifacts {- inScopeFilesPath: string;- candidateLedgerPath: string;-}-export interface DeepScanRunState {scanId: string;status: DeepScanRunStatus;- phase?: "setup" | "discovery" | "reducing" | "terminal";coordinatorGeneration?: number;createdAt?: string;updatedAt?: string;@@ -43,7 +37,6 @@ export interface DeepScanRunState {dispatchedCount: number;noNewStreak: number;consecutiveErrors: number;- canonicalArtifacts?: DeepScanCanonicalArtifacts;manifestPath?: string;terminalReason?: DeepScanTerminalReason;error?: string;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.tsindex e55fadcd8..96f3eb8a4 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@@ -214,7 +214,6 @@ export class FakeStore {this.workers.set(workerId, { ...discovery, mergeState: "buffered" });}}- this.run.phase = "discovery";}persisted.consecutiveErrors = this.run.consecutiveErrors;this.workers.set(update.id, persisted);@@ -386,7 +385,6 @@ export class FakeStore {throw new Error("fixture progress persistence failure");}this.progress.push(structuredClone(input));- if (input.phase) this.run.phase = input.phase as DeepScanRunState["phase"];}}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.tsindex 3482d6c95..08d8553aa 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@@ -19,7 +19,6 @@ import {} from "node:fs/promises";import path from "node:path";import {- DeepScanCoordinator,DeepScanCoordinatorRegistry,DeepScanNonRetryableError,DeepScanRemoteCoordinator,@@ -2564,7 +2563,6 @@ async function testPausedDiscoverySurvivesCoordinatorRestart() {const handoffClaimToken = randomUUID();const store = new FakeStore({...fixture.run,- phase: "setup" as const,coordinatorGeneration: 2,updatedAt: "2026-08-03T13:33:08Z",});@@ -2608,7 +2606,7 @@ async function testPausedDiscoverySurvivesCoordinatorRestart() {consolidating: false,});assert.equal(store.run.status, "running");- assert.equal(store.run.phase, "discovery");+ assert.equal(store.progress.at(-1)?.phase, "discovery");assert.equal(store.finishCalls.length, 0);assert.equal(store.failureMessages.length, 0);assert.equal(store.run.manifestPath, undefined);@@ -2700,7 +2698,6 @@ async function testResumedDiscoveryDeadlineUsesPersistedCreationTime(const store = new FakeStore({...fixture.run,createdAt,- phase: "setup" as const,coordinatorGeneration: 2,});const originalExecutor = new FakeExecutor({@@ -2779,7 +2776,7 @@ async function testResumedManifestPreservesCompletedReducer(stopAfterNoNew: 2,maxDiscoveryRuns: 3,});- const store = new FakeStore({ ...fixture.run, phase: "setup" });+ const store = new FakeStore(fixture.run);store.dedupCommitResponseGate = Promise.withResolvers<void>();const original = createCoordinator(fixture,@@ -2831,7 +2828,6 @@ async function testResumedManifestPreservesCompletedReducer(store.run = {...store.run,status: "running" as const,- phase: "discovery" as const,persistedWorkers: structuredClone([...store.workers.values()]),persistedDedupInputs: store.dedupClaims.flatMap(claimDedupInputs),};@@ -2872,7 +2868,6 @@ async function testResumeUsesHistoricalCandidateSnapshotForEachReducer() {store.run = {...store.run,status: "running" as const,- phase: "discovery" as const,terminalReason: undefined,manifestPath: undefined,persistedWorkers: structuredClone([...store.workers.values()]),@@ -2932,23 +2927,15 @@ async function testPersistedErrorLimitStopsBeforeRescheduling() {}const run = {...fixture.run,- phase: "discovery" as const,consecutiveErrors: 2,dispatchedCount: 1,persistedWorkers: [failedWorker],};const store = new FakeStore(run);const executor = new FakeExecutor();- const coordinator = new DeepScanCoordinator({+ const terminal = await runCoordinator(fixture, store, executor, {run,- store,- executor,- pluginRoot: fixture.pluginRoot,- clock: immediateClock,});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "failed");assert.equal(executor.discoveryCalls, 0);assert.match(@@ -2991,7 +2978,6 @@ async function testPersistedReducerErrorLimitStopsBeforeRescheduling() {);const run = {...fixture.run,- phase: "discovery" as const,consecutiveErrors: 0,persistedWorkers: reducers,};@@ -3155,15 +3141,13 @@ async function testDiscoveryAcceptedAtDeadlineIsReduced() {discoveryCandidateId: "candidate-1",canonicalCandidateId: "candidate-1",});- const coordinator = createCoordinator(fixture, store, executor, {+ const terminalWait = runCoordinator(fixture, store, executor, {discoveryTimeoutMs: 500,log: (event) => {if (event.event === "discovery_deadline_reached")discoveryDeadlineReached.resolve();},});- coordinator.start();- const terminalWait = coordinator.wait(undefined, 5_000);await acceptancePersisted.promise;await discoveryDeadlineReached.promise;@@ -3501,15 +3485,9 @@ async function testResumeRequiresHistoricalWorkerPrompt(],});const executor = new FakeExecutor();- const coordinator = new DeepScanCoordinator({+ const terminal = await runCoordinator(fixture, store, executor, {run: store.run,- store,- executor,- pluginRoot: fixture.pluginRoot,- clock: immediateClock,});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "failed");assert.match(terminal.error, /ENOENT/);assert.ok(terminal.error.includes(promptPath));diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_store.tsindex 129f1fbc2..a88321441 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts@@ -12,17 +12,9 @@ const { WorkbenchDeepScanStore, parseDeepScan } = await importSource(new URL("../src/deep-scan/store.ts", import.meta.url).pathname,);-const canonical = {- inScopeFilesPath:- "/fixture/scans/run/artifacts/02_discovery/in_scope_files.txt",- candidateLedgerPath:- "/fixture/scans/run/artifacts/02_discovery/candidate_ledger.jsonl",-};-await testBeginProtocolAndParsing();await testCanonicalCommitProtocol();await testTerminalProtocol();-testCanonicalNullAndPartialParsing();testRunErrorParsing();testConfiguredMaximumDurationParsing();await testWriteSerializationAndRecovery();@@ -374,17 +366,14 @@ async function testCanonicalCommitProtocol() {const reducerId = randomUUID();const resultManifestPath ="/fixture/scans/run/artifacts/deep_discovery/dedup/result.json";- const runWorkbench = mock.fn(async (args: string[]) => {- return stateResult(scanId, { deepScan: { canonicalArtifacts: canonical } });- });+ const runWorkbench = mock.fn(async (args: string[]) => stateResult(scanId));const store = new WorkbenchDeepScanStore(runWorkbench);- const state = await store.commitDedup({+ await store.commitDedup({id: reducerId,scanId,newFindings: 1,resultManifestPath,});- assert.deepEqual(state.canonicalArtifacts, canonical);assert.deepEqual(runWorkbench.mock.calls.at(-1)?.arguments[0], ["commit-deep-scan-dedup","--scan-id",@@ -798,9 +787,7 @@ function idempotentPersistenceScenarios() {},{operation: "commit-deep-scan-dedup",- result: stateResult(scanId, {- deepScan: { canonicalArtifacts: canonical },- }),+ result: stateResult(scanId),invoke: (store: Store) =>store.commitDedup({id: reducerId,@@ -923,45 +910,6 @@ function testRunErrorParsing() {assert.equal(state.error, "discovery retries exhausted");}-function testCanonicalNullAndPartialParsing() {- assert.equal(- parseDeepScan(- stateResult(randomUUID(), {- deepScan: { canonicalArtifacts: null },- }),- ).canonicalArtifacts,- undefined,- );- assert.throws(- () =>- parseDeepScan(- stateResult(randomUUID(), {- deepScan: {- canonicalArtifacts: {- inScopeFilesPath:- "/fixture/scans/run/artifacts/02_discovery/in_scope_files.txt",- },- },- }),- ),- /invalid deepScan\.canonicalArtifacts\.candidateLedgerPath/,- );- assert.throws(- () =>- parseDeepScan(- stateResult(randomUUID(), {- deepScan: {- canonicalArtifacts: {- candidateLedgerPath:- "/fixture/scans/run/artifacts/02_discovery/candidate_ledger.jsonl",- },- },- }),- ),- /invalid deepScan\.canonicalArtifacts\.inScopeFilesPath/,- );-}-function stateResult(scanId: string,overrides: {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.tsindex 017fda701..0f1edf473 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@@ -1,6 +1,7 @@import { readJson, writeJsonLine } from "./support/json.ts";import { temporaryDirectory } from "./support/temporary-directories.ts";import assert from "node:assert/strict";+import { mock } from "node:test";import { execFile, spawn } from "node:child_process";import { randomUUID } from "node:crypto";import { once } from "node:events";@@ -333,7 +334,7 @@ async function testReducerCommitAndFinishAgainstRealWorkbench(const codexHome = path.join(fixtureRoot, "codex-home");const threadId = "deep-scan-store-integration-thread";const python = process.env.PYTHON?.trim() || "python3";- const runWorkbench = createWorkbenchRunner(environment);+ const runWorkbench = mock.fn(createWorkbenchRunner(environment));const store = new WorkbenchDeepScanStore(runWorkbench);try {@@ -510,7 +511,11 @@ connection.rollback()`,assert.equal(afterCommit.terminalReason, undefined);assert.equal(afterCommit.manifestPath, undefined);assert.equal(afterCommit.noNewStreak, 2);- assert.equal(afterCommit.canonicalArtifacts, undefined);+ assert.equal(+ (await runWorkbench.mock.calls.at(-1)!.result).deepScan+ .canonicalArtifacts,+ null,+ );assert.equal(await readFile(canonical.candidateLedgerPath, "utf8"),'{"candidate_id":"replacement"}\n',@@ -603,7 +608,7 @@ async function testExpiredDeadlineWithoutCompletedDiscoveryAgainstRealWorkbench();const codexHome = path.join(fixtureRoot, "codex-home");const threadId = "deep-scan-store-zero-discovery-thread";- const runWorkbench = createWorkbenchRunner(environment, false);+ const runWorkbench = mock.fn(createWorkbenchRunner(environment, false));const store = new WorkbenchDeepScanStore(runWorkbench);try {@@ -625,7 +630,11 @@ async function testExpiredDeadlineWithoutCompletedDiscoveryAgainstRealWorkbench(threadId,});assert.equal(owned.acquired, true);- assert.equal(owned.run.canonicalArtifacts, undefined);+ assert.equal(+ (await runWorkbench.mock.calls.at(-1)!.result).deepScan+ .canonicalArtifacts,+ null,+ );const canonical = await createCanonicalFixture(run.scanDir);const manifestPath = path.join(run.scanDir,@@ -647,7 +656,11 @@ async function testExpiredDeadlineWithoutCompletedDiscoveryAgainstRealWorkbench(assert.equal(finished.status, "succeeded");assert.equal(finished.terminalReason, "capped");assert.equal(finished.dispatchedCount, 0);- assert.deepEqual(finished.canonicalArtifacts, canonical);+ assert.deepEqual(+ (await runWorkbench.mock.calls.at(-1)!.result).deepScan+ .canonicalArtifacts,+ canonical,+ );assert.equal(await readFile(canonical.candidateLedgerPath, "utf8"), "");assert.equal(await readFile(canonical.inScopeFilesPath, "utf8"),@@ -660,7 +673,11 @@ async function testExpiredDeadlineWithoutCompletedDiscoveryAgainstRealWorkbench();assert.equal(observed.status, "succeeded");assert.equal(observed.terminalReason, "capped");- assert.deepEqual(observed.canonicalArtifacts, canonical);+ assert.deepEqual(+ (await runWorkbench.mock.calls.at(-1)!.result).deepScan+ .canonicalArtifacts,+ canonical,+ );assert.deepEqual(observed.persistedWorkers, []);} finally {await rm(fixtureRoot, { force: true, recursive: true });