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(dedupe): observe cancellation after completed writes (#1821)

Michael D'Angelof2c40547d917
diff --git a/sdk/typescript/docs/findings-service.md b/sdk/typescript/docs/findings-service.md
index 790ca3f8e..cb7f5083e 100644
--- a/sdk/typescript/docs/findings-service.md
+++ b/sdk/typescript/docs/findings-service.md
@@ -317,7 +317,9 @@ required-source-access blockers are not retried. Refusals are not retried or
sent to another model. If candidate retrieval or review still fails, the
operation fails without posting groups.
A failed group-write request may already have committed; resubmitting the same
-memberships does not create duplicate groups. Non-cancellation review failures
+memberships does not create duplicate groups. Cancellation can also arrive after
+a write completes; it does not mean that remote findings are unchanged. Completed
+workflow stages remain saved even when cancellation is reported. Non-cancellation review failures
throw `DeduplicationReviewError`, with diagnostic text and `metadata` for the
stage, model, category, attempt count, and reason.
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index edb95269b..965544acb 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -4402,7 +4402,7 @@ export async function main(
signal === "SIGINT" || signal === "SIGTERM"
? options.findingsUrl === undefined
? "Deduplication canceled. Completed local preparation is saved for retry."
- : "Deduplication canceled. Findings are unchanged."
+ : "Deduplication canceled. Remote writes may have completed."
: diagnosticValue(error)
}\n`,
);
diff --git a/sdk/typescript/src/deduplication/scan.ts b/sdk/typescript/src/deduplication/scan.ts
index 331ce27fc..5a731921a 100644
--- a/sdk/typescript/src/deduplication/scan.ts
+++ b/sdk/typescript/src/deduplication/scan.ts
@@ -417,7 +417,11 @@ async function deduplicateResolvedScan(
await client.storeDedupeGroups(result.duplicateGroups);
return result;
};
- return workflow ? await workflow.run("dedupe", dedupe) : await dedupe();
+ const result = workflow
+ ? await workflow.run("dedupe", dedupe)
+ : await dedupe();
+ options.signal?.throwIfAborted();
+ return result;
}
async function modelConfigurationForReview(
diff --git a/sdk/typescript/tests-ts/cli-dedupe.test.ts b/sdk/typescript/tests-ts/cli-dedupe.test.ts
index 544bc495d..923374292 100644
--- a/sdk/typescript/tests-ts/cli-dedupe.test.ts
+++ b/sdk/typescript/tests-ts/cli-dedupe.test.ts
@@ -377,7 +377,7 @@ test.each([
],
[
["--findings-url", "http://127.0.0.1:3000"],
- "Deduplication canceled. Findings are unchanged.",
+ "Deduplication canceled. Remote writes may have completed.",
],
] as const) {
const signals = new FakeSignals();
diff --git a/sdk/typescript/tests-ts/finding-workflow-integration.test.ts b/sdk/typescript/tests-ts/finding-workflow-integration.test.ts
index 22f31de64..e0790e4c8 100644
--- a/sdk/typescript/tests-ts/finding-workflow-integration.test.ts
+++ b/sdk/typescript/tests-ts/finding-workflow-integration.test.ts
@@ -434,6 +434,134 @@ test("an empty scan completes publication and dedupe and remains retrievable", a
});
});
+test.each(["acknowledged", "interrupted", "without workflow"])(
+ "observes cancellation after the final group write (%s) without losing durable state",
+ async (outcome) => {
+ const { environment, document, history } = await fixtureWithFindings(2);
+ const controller = new AbortController();
+ const reason = new Error("Synthetic cancellation during group write");
+ const options = {
+ ...(outcome === "without workflow" ? {} : { workflowId: "final-write" }),
+ findingsUrl: "http://synthetic.test",
+ };
+ const groups = [
+ document.findings.map((finding) => finding.findingId).sort(),
+ ];
+ const writes: string[][][] = [];
+ const reviewer = {
+ screen: mock(async (findings: readonly Finding[]) => ({
+ decisions: Object.fromEntries(
+ findings.slice(1).map(screeningRecommendation),
+ ),
+ })),
+ reviewPair: mock(async () => sameRecommendation),
+ };
+ const dependencies = {
+ environment,
+ runWorkbench: history,
+ reviewer,
+ fetch: async (url: URL, init: RequestInit) => {
+ if (!url.pathname.endsWith("/dedupe-groups"))
+ return await findingFetch(document)(url);
+ writes.push(JSON.parse(init.body as string).groups);
+ if (writes.length === 1) {
+ controller.abort(reason);
+ if (outcome === "interrupted") throw reason;
+ }
+ return Response.json([]);
+ },
+ };
+ await expect(
+ deduplicateScanInternal(
+ document.scanId,
+ { ...options, signal: controller.signal },
+ dependencies,
+ ),
+ ).rejects.toThrow(reason.message);
+ expect(writes).toEqual([groups]);
+ if (options.workflowId === undefined) return;
+ const workflow = new FindingWorkflow(options.workflowId, environment);
+ const saved = (await workflow.get())!.stages.dedupe;
+ expect(saved.status).toBe(
+ outcome === "interrupted" ? "failed" : "completed",
+ );
+ expect(saved.result).toMatchObject({ duplicateGroups: groups });
+ expect(saved.pendingWrite).toEqual(
+ outcome === "interrupted" ? { groups } : undefined,
+ );
+ const reviewCalls = reviewer.reviewPair.mock.calls.length;
+ expect<unknown>(
+ await deduplicateScanInternal(document.scanId, options, dependencies),
+ ).toEqual(saved.result);
+ expect(reviewer.reviewPair).toHaveBeenCalledTimes(reviewCalls);
+ expect(writes).toHaveLength(outcome === "interrupted" ? 2 : 1);
+ expect((await workflow.get())!.stages.dedupe).toEqual({
+ status: "completed",
+ result: saved.result,
+ });
+ },
+);
+
+test.each(["complete", "cached"])(
+ "observes final cancellation for a %s workflow without group writes",
+ async (phase) => {
+ const { environment, document, history } = await fixtureWithFindings(0);
+ const controller = new AbortController();
+ const reason = new Error(
+ "Synthetic cancellation after workflow completion",
+ );
+ const options = {
+ workflowId: "empty-canceled",
+ findingsUrl: "http://synthetic.test",
+ };
+ const fetch = mock(findingFetch(document));
+ if (phase === "cached")
+ await deduplicateScanInternal(document.scanId, options, {
+ environment,
+ runWorkbench: history,
+ fetch,
+ });
+ await expect(
+ deduplicateScanInternal(
+ document.scanId,
+ { ...options, signal: controller.signal },
+ {
+ environment,
+ fetch,
+ runWorkbench: async (args, input) => {
+ const result = await history(args, input);
+ const payload = input ? JSON.parse(input) : {};
+ if (
+ payload.stage === "dedupe" &&
+ payload.action === (phase === "cached" ? "begin" : "complete")
+ )
+ controller.abort(reason);
+ return result;
+ },
+ },
+ ),
+ ).rejects.toThrow(reason.message);
+ const saved = (await new FindingWorkflow(
+ options.workflowId,
+ environment,
+ ).get())!.stages.dedupe;
+ expect(saved).toMatchObject({
+ status: "completed",
+ result: { duplicateGroups: [] },
+ });
+ expect(fetch).toHaveBeenCalledTimes(1);
+ expect(fetch.mock.calls[0]![0].pathname).toBe("/v1/bulk/findings");
+ expect<unknown>(
+ await deduplicateScanInternal(document.scanId, options, {
+ environment,
+ runWorkbench: history,
+ fetch,
+ }),
+ ).toEqual(saved.result);
+ expect(fetch).toHaveBeenCalledTimes(1);
+ },
+);
+
test("does not write workflow metadata into sealed artifacts", async () => {
const { scanDir, environment } = await fixture();
await expect(