Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(deep-scan): resume scans when old worker prompts are missing (#900)

mldangelo-oai9cfdc3362706
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 a5fe4b449..3d4931088 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
@@ -636,7 +636,6 @@ export class DeepScanCoordinator {
.map((worker) => worker.id),
);
const omittedWorkerIds: string[] = [];
- await this.readPersistedExecutionPrompts();
const recoveredReducers = await this.recoverCompletedReducers(recovered);
let latestResult = recoveredReducers.result;
let buffer: AcceptedDiscovery[] = recovered.filter(
@@ -978,7 +977,6 @@ export class DeepScanCoordinator {
worker.resultManifestPath,
this.state.scanId,
);
- await fs.readFile(worker.promptPath, "utf8");
recovered.push({
id: worker.id,
resultPath: worker.resultManifestPath,
@@ -1039,26 +1037,11 @@ export class DeepScanCoordinator {
this.state.scanId,
);
latestResult = result;
- await fs.readFile(worker.promptPath, "utf8");
resultPath = worker.resultManifestPath;
}
return { resultPath, result: latestResult };
}
- private async readPersistedExecutionPrompts(): Promise<void> {
- for (const worker of this.state.persistedWorkers ?? []) {
- if (
- worker.kind === "setup" ||
- worker.status === "queued" ||
- worker.status === "running" ||
- (worker.status === "canceled" && worker.attempt === 0)
- ) {
- continue;
- }
- await fs.readFile(worker.promptPath, "utf8");
- }
- }
-
private reducerReady(
buffer: AcceptedDiscovery[],
previousReducerResultPath: string | undefined,
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 55ec4b9cb..056f97216 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
@@ -2556,7 +2556,9 @@ async function testJoinAndOrphanRules() {
assert.equal(failures.mock.callCount(), 0);
}
-async function testPausedDiscoverySurvivesCoordinatorRestart() {
+async function testPausedDiscoverySurvivesCoordinatorRestart(
+ removeHistoricalPrompts = true,
+) {
const fixture = await fixtureRun({
stopAfterNoNew: 2,
maxDiscoveryRuns: 2,
@@ -2642,6 +2644,11 @@ async function testPausedDiscoverySurvivesCoordinatorRestart() {
store.heartbeatCoordinator = async () => structuredClone(store.run);
const replacementExecutor = new FakeExecutor();
const acceptedResult = await readFile(accepted.resultManifestPath!, "utf8");
+ const acceptedWorkerId = await workerIdFromPrompt(accepted.promptPath);
+ if (removeHistoricalPrompts)
+ await Promise.all(
+ persistedWorkers.map((worker) => rm(worker.promptPath, { force: true })),
+ );
const resumed = await startOrJoinDeepScanCoordinator({
run: structuredClone(store.run),
registry: new DeepScanCoordinatorRegistry(),
@@ -2662,9 +2669,7 @@ async function testPausedDiscoverySurvivesCoordinatorRestart() {
assert.equal(store.failureMessages.length, 0);
assert.equal(replacementExecutor.discoveryAttempts.size, 1);
assert.equal(
- replacementExecutor.discoveryAttempts.has(
- await workerIdFromPrompt(accepted.promptPath),
- ),
+ replacementExecutor.discoveryAttempts.has(acceptedWorkerId),
false,
);
assert.equal(store.dedupClaims.length, 1);
@@ -2733,6 +2738,11 @@ async function testResumedDiscoveryDeadlineUsesPersistedCreationTime(
persistedWorkers: structuredClone([...store.workers.values()]),
};
+ await Promise.all(
+ store.run.persistedWorkers!.map((worker) =>
+ rm(worker.promptPath, { force: true }),
+ ),
+ );
const resumedExecutor = new FakeExecutor({
blockDiscoveryAfterCalls: 0,
canonicalCandidateId: "candidate-1",
@@ -2771,6 +2781,7 @@ async function testResumedDiscoveryDeadlineUsesPersistedCreationTime(
async function testResumedManifestPreservesCompletedReducer(
includeUnstartedReducer = false,
+ removeHistoricalPrompts = true,
) {
const fixture = await fixtureRun({
workers: 2,
@@ -2832,7 +2843,21 @@ async function testResumedManifestPreservesCompletedReducer(
persistedWorkers: structuredClone([...store.workers.values()]),
persistedDedupInputs: store.dedupClaims.flatMap(claimDedupInputs),
};
- const terminal = await runCoordinator(fixture, store, new FakeExecutor(), {
+ const acceptedWorkers = store.run.persistedWorkers!.filter(
+ (worker) => worker.status === "succeeded",
+ );
+ const acceptedResults = await Promise.all(
+ acceptedWorkers.map((worker) =>
+ readFile(worker.resultManifestPath!, "utf8"),
+ ),
+ );
+ const persistedInputs = structuredClone(store.run.persistedDedupInputs);
+ const persistedWorkers = structuredClone(store.run.persistedWorkers);
+ const committedReducer = store.workers.get(store.dedupClaims[0].id)!;
+ const committedResult = await readJson(committedReducer.resultManifestPath!);
+ if (removeHistoricalPrompts) await rm(committedReducer.promptPath);
+ const replacementExecutor = new FakeExecutor();
+ const terminal = await runCoordinator(fixture, store, replacementExecutor, {
run: store.run,
});
assert.equal(terminal?.status, "succeeded");
@@ -2840,6 +2865,22 @@ async function testResumedManifestPreservesCompletedReducer(
assert.equal(manifest.scan.scanId, fixture.run.scanId);
assert.equal(store.dedupCommits.length, 1);
assert.equal(store.dedupClaims.length, 1);
+ assert.equal(
+ replacementExecutor.discoveryCalls + replacementExecutor.dedupCalls,
+ 0,
+ "accepted work needs no new worker launch",
+ );
+ assert.deepEqual(manifest.findings, committedResult.findings);
+ assert.deepEqual(store.run.persistedDedupInputs, persistedInputs);
+ assert.deepEqual(store.run.persistedWorkers, persistedWorkers);
+ assert.deepEqual(
+ await Promise.all(
+ acceptedWorkers.map((worker) =>
+ readFile(worker.resultManifestPath!, "utf8"),
+ ),
+ ),
+ acceptedResults,
+ );
assert.equal(
store.run.persistedWorkers!.some(
(worker) => worker.id === unstartedReducer?.id,
@@ -2874,8 +2915,15 @@ async function testResumeUsesHistoricalCandidateSnapshotForEachReducer() {
persistedWorkers: structuredClone([...store.workers.values()]),
persistedDedupInputs: store.dedupClaims.flatMap(claimDedupInputs),
};
+ const persistedInputs = structuredClone(store.run.persistedDedupInputs);
+ await Promise.all(
+ store.run.persistedWorkers!.map((worker) =>
+ rm(worker.promptPath, { force: true }),
+ ),
+ );
+ const replacementExecutor = new FakeExecutor();
const completedDrafts: ScanDraftInput[] = [];
- const resumed = await runCoordinator(fixture, store, new FakeExecutor(), {
+ const resumed = await runCoordinator(fixture, store, replacementExecutor, {
run: store.run,
onComplete: async (draft) =>
void completedDrafts.push(structuredClone(draft)),
@@ -2903,6 +2951,11 @@ async function testResumeUsesHistoricalCandidateSnapshotForEachReducer() {
completedDrafts.map((draft) => draft.findings),
[latestResult.findings],
);
+ assert.deepEqual(store.run.persistedDedupInputs, persistedInputs);
+ assert.equal(
+ replacementExecutor.discoveryCalls + replacementExecutor.dedupCalls,
+ 0,
+ );
}
async function testPersistedErrorLimitStopsBeforeRescheduling() {
@@ -2941,10 +2994,9 @@ async function testPersistedErrorLimitStopsBeforeRescheduling() {
assert.equal(executor.discoveryCalls, 0);
assert.match(
terminal?.error ?? "",
- promptExists ? /2 consecutive unsuccessful discovery workers/ : /ENOENT/,
+ /2 consecutive unsuccessful discovery workers/,
);
- if (promptExists)
- assert.match(terminal?.error ?? "", /persisted worker failure/);
+ assert.match(terminal?.error ?? "", /persisted worker failure/);
assert.equal(terminal.manifestPath, undefined);
assert.equal(store.run.persistedWorkers![0].id, failedWorker.id);
}
@@ -3464,7 +3516,7 @@ async function testPublicationUsesAcceptedReducerSnapshot() {
assert.equal(completed[0].coverage.completeness, "complete");
}
-async function testResumeRequiresHistoricalWorkerPrompt(
+async function testResumeDoesNotRequireHistoricalWorkerPrompt(
status: "failed" | "canceled",
) {
const fixture = await fixtureRun({
@@ -3489,10 +3541,10 @@ async function testResumeRequiresHistoricalWorkerPrompt(
const terminal = await runCoordinator(fixture, store, executor, {
run: store.run,
});
- assert.equal(terminal?.status, "failed");
- assert.match(terminal.error, /ENOENT/);
- assert.ok(terminal.error.includes(promptPath));
- assert.equal(executor.discoveryCalls, 0);
+ assert.equal(terminal?.status, "succeeded", terminal?.error);
+ assert.equal(store.failureMessages.length, 0);
+ assert.equal(executor.discoveryCalls, 2);
+ assert.equal(store.run.persistedWorkers?.[0].promptPath, promptPath);
}
async function testRecoverableWorkerErrorsCannotFailScan() {
@@ -3801,13 +3853,15 @@ try {
await testCoordinatorHeartbeatsContinueDuringBlockedOwnershipRead();
await testRemoteObserverRetriesTransientPersistenceFailures();
await testJoinAndOrphanRules();
- await testResumeRequiresHistoricalWorkerPrompt("failed");
- await testResumeRequiresHistoricalWorkerPrompt("canceled");
+ await testResumeDoesNotRequireHistoricalWorkerPrompt("failed");
+ await testResumeDoesNotRequireHistoricalWorkerPrompt("canceled");
+ await testPausedDiscoverySurvivesCoordinatorRestart(false);
await testPausedDiscoverySurvivesCoordinatorRestart();
await testResumedDiscoveryDeadlineUsesPersistedCreationTime();
await testResumedDiscoveryDeadlineUsesPersistedCreationTime(true);
await testResumedDiscoveryDeadlineUsesPersistedCreationTime(false, 2.5);
await testResumedDiscoveryDeadlineUsesPersistedCreationTime(true, 96);
+ await testResumedManifestPreservesCompletedReducer(false, false);
await testResumedManifestPreservesCompletedReducer();
await testResumedManifestPreservesCompletedReducer(true);
await testResumeUsesHistoricalCandidateSnapshotForEachReducer();