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): preserve disputed comparisons across context retries (#2321)

Michael D'Angelo57a7d6cef46c
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/errors.ts b/plugins/codex-security/mcp-app/src/deep-scan/errors.ts
index 3dd3bac13..03e3b268c 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/errors.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/errors.ts
@@ -1,3 +1,4 @@
+import { asRecord } from "../record.js";
import type { DeepScanRunState } from "./types.js";
const STALE_COORDINATOR_GENERATION_MESSAGE =
@@ -104,6 +105,15 @@ export function errorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
+export function exceedsContext(error: unknown) {
+ return (
+ asRecord(error)?.code === "context_length_exceeded" ||
+ /\b(?:context_length_exceeded|context (?:length|window) exceeded|maximum context length is|exceeds? (?:the |this model's |the model's )?context (?:length|window)|(?:ran out of room in|exceeded) (?:the |this model's |the model's )?context window)\b/i.test(
+ errorMessage(error),
+ )
+ );
+}
+
export function errorNameWithCode(error: Error): string {
const code =
"code" in error && typeof error.code === "string" ? error.code : undefined;
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/iteration.ts b/plugins/codex-security/mcp-app/src/deep-scan/iteration.ts
index 745669aae..ca89ef5ab 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/iteration.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/iteration.ts
@@ -4,11 +4,11 @@ import {
type ScanDraftInput,
} from "../artifact-scan-draft.js";
import { z } from "zod";
-import { asRecord } from "../record.js";
+import { asRecord, canonicalJson } from "../record.js";
import { addScanCoverage, type ScanUnion } from "./artifact-validation.js";
import type { AcceptedDiscovery } from "./worker-runner.js";
-import { errorMessage } from "./errors.js";
-import { checkSameFix } from "./same-fix.js";
+import { errorMessage, exceedsContext } from "./errors.js";
+import { checkSameFix, sharesSource } from "./same-fix.js";
type Finding = ScanDraftInput["findings"][number];
export type ComparisonReport = ReturnType<typeof report>;
@@ -136,12 +136,6 @@ function equalRepairs(a?: number[], b?: number[]) {
);
}
-function contextWindowExceeded(error: unknown) {
- return errorMessage(error).startsWith(
- "Codex ran out of room in the model's context window.",
- );
-}
-
async function compareRecords(
records: ComparisonReport[],
compare: CompareReports,
@@ -164,6 +158,7 @@ async function compareWithinContext(
previous: number,
compare: CompareReports,
matches: Map<number, Set<number>>,
+ overlaps: Map<number, Set<number>>,
) {
const byFile = new Map<string, number[]>();
const seen = new Set<string>();
@@ -186,14 +181,21 @@ async function compareWithinContext(
compare,
);
for (const [i, other] of ids.entries()) {
- if (equalRepairs(keys.get(0), keys.get(i + 1))) {
- const earlier = matches.get(id) ?? new Set<number>();
+ const current = keys.get(0);
+ const prior = keys.get(i + 1);
+ const table = equalRepairs(current, prior) ? matches : overlaps;
+ if (
+ current && prior
+ ? current.some((key) => prior.includes(key))
+ : sharesSource(record, records[other]!)
+ ) {
+ const earlier = table.get(id) ?? new Set<number>();
earlier.add(other);
- matches.set(id, earlier);
+ table.set(id, earlier);
}
}
} catch (error) {
- if (!contextWindowExceeded(error)) throw error;
+ if (!exceedsContext(error)) throw error;
if (ids.length === 1) return; // Even this pair cannot fit. Retain both reports.
const mid = Math.ceil(ids.length / 2);
await check(ids.slice(0, mid));
@@ -334,6 +336,11 @@ function distinctIdentity(
);
}
+// JSON object field order has no meaning, including inside extension fields.
+function findingContent({ provenance, ...fields }: Record<string, unknown>) {
+ return canonicalJson(fields);
+}
+
function addLocations(finding: Finding, source: Pick<Finding, "locations">) {
for (const location of source.locations) {
const sameRange = finding.locations.filter(
@@ -521,9 +528,7 @@ export async function mergeBatch(
...retained.map(({ finding }) => finding),
...workers.flatMap((worker) => worker.result.findings),
];
- const content = (finding: object) =>
- JSON.stringify({ ...finding, provenance: undefined });
- const exact = originals.map(content);
+ const exact = originals.map(findingContent);
const copies = new Map<string, (typeof retained)[number]>();
const incomplete = new Set<number>();
for (const group of retained) {
@@ -535,10 +540,12 @@ export async function mergeBatch(
// A legacy checkpoint can include all originals without the separate ID array.
group.finding.provenance.sourceFindingIds ??= sourceIds(group.finding);
for (const source of group.finding.provenance.sourceFindings ?? [])
- copies.set(content(source.finding), group);
+ copies.set(findingContent(source.finding), group);
}
let repairs: Map<number, number[] | undefined> | undefined;
const fallbackMatches = new Map<number, Set<number>>();
+ const fallbackOverlaps = new Map<number, Set<number>>();
+ const joined = new Map<number, number[]>();
let records: ComparisonReport[] = [];
let limitation: string | undefined;
const unseen = new Set(
@@ -569,7 +576,7 @@ export async function mergeBatch(
} catch (error) {
onError(error);
limitation = `Deep Scan could not compare this batch for duplicates: ${errorMessage(error)}`;
- if (contextWindowExceeded(error) && records.length > 2) {
+ if (exceedsContext(error) && records.length > 2) {
limitation +=
" Retried reports with shared files in smaller comparisons. Pairs that could not fit were kept separate.";
try {
@@ -579,6 +586,7 @@ export async function mergeBatch(
previous,
compare,
fallbackMatches,
+ fallbackOverlaps,
);
} catch (retryError) {
onError(retryError);
@@ -597,12 +605,15 @@ export async function mergeBatch(
copy ??
retained.find((prior) => {
const other = repairs?.get(prior.id);
- if (
- incomplete.has(prior.target) ||
- (!equalRepairs(key, other) &&
- !fallbackMatches.get(id)?.has(prior.id))
- )
- return false;
+ if (incomplete.has(prior.target)) return false;
+ if (!equalRepairs(key, other)) {
+ const earlier = fallbackMatches.get(id);
+ if (
+ !earlier?.has(prior.id) ||
+ joined.get(prior.id)?.some((member) => !earlier.has(member))
+ )
+ return false;
+ }
if (
separateSinks(
source,
@@ -632,6 +643,12 @@ export async function mergeBatch(
group = { id, finding, worker: workerIndex, target: id };
retained.push(group);
} else {
+ // Pair-local repair IDs can yield non-transitive matches across retries.
+ if (!repairs && !copy) {
+ const members = joined.get(group.id) ?? [];
+ members.push(id);
+ joined.set(group.id, members);
+ }
addLocations(group.finding, source);
if (!copy && group.id < previous) changed.set(group.id, workerIndex);
}
@@ -673,13 +690,10 @@ export async function mergeBatch(
group.target = group.id;
group.worker = worker;
}
- const reviewable = retained.filter(
- (group) =>
- group.target === group.id &&
- !incomplete.has(group.target) &&
- (repairs || (fallbackMatches.size > 0 && group.id < previous)),
- );
- if (reviewable.some((group) => group.worker >= 0)) {
+ if (
+ (repairs || fallbackOverlaps.size || fallbackMatches.size) &&
+ retained.some((group) => group.worker >= 0)
+ ) {
const sources = new Map<number, Record<string, unknown>[]>();
for (const group of retained) {
if (incomplete.has(group.target)) continue;
@@ -691,7 +705,9 @@ export async function mergeBatch(
}
const reviewFailures = new Set<string>();
const matches = await checkSameFix(
- reviewable,
+ retained.filter(
+ (group) => group.target === group.id && !incomplete.has(group.target),
+ ),
priorTargets,
records,
repairs ?? new Map(),
@@ -701,6 +717,13 @@ export async function mergeBatch(
onError(error);
reviewFailures.add(errorMessage(error));
},
+ repairs
+ ? undefined
+ : (left, right) =>
+ Boolean(
+ fallbackOverlaps.get(right)?.has(left) ||
+ fallbackMatches.get(right)?.has(left),
+ ),
);
if (reviewFailures.size) {
limitation = [
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/same-fix.ts b/plugins/codex-security/mcp-app/src/deep-scan/same-fix.ts
index f9f33ffb3..8804a2057 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/same-fix.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/same-fix.ts
@@ -1,3 +1,5 @@
+import { canonicalJson } from "../record.js";
+import { exceedsContext } from "./errors.js";
import type { ComparisonReport, CompareReports } from "./iteration.js";
const schema = {
@@ -65,26 +67,26 @@ type Pair = { id: number; left: number; right: number };
function claim({
ruleId,
identity,
+ validation,
+ attackPath,
title,
summary,
rootCause,
remediation,
locations,
codeEvidence,
- attackPath,
- validation,
}: Record<string, unknown>) {
return {
ruleId,
identity,
+ validation,
+ attackPath,
title,
summary,
rootCause,
remediation,
locations,
codeEvidence,
- attackPath,
- validation,
};
}
@@ -95,32 +97,67 @@ function unambiguous<T>(rows: T[], id: (row: T) => number): T[] {
);
}
+// Keep the normal streaming batch size. A smaller retry still carries every
+// field for each pair and source, even when one pair cannot be graded.
+async function* gradeInContext<T>(
+ rows: T[],
+ request: (part: T[]) => Promise<string>,
+ oversized: (row: T, error: unknown) => void,
+): AsyncGenerator<[T[], string]> {
+ let response: string;
+ try {
+ response = await request(rows);
+ } catch (error) {
+ if (!exceedsContext(error)) throw error;
+ if (rows.length === 1) {
+ oversized(rows[0]!, error);
+ return;
+ }
+ const mid = Math.ceil(rows.length / 2);
+ yield* gradeInContext(rows.slice(0, mid), request, oversized);
+ yield* gradeInContext(rows.slice(mid), request, oversized);
+ return;
+ }
+ yield [rows, response];
+}
+
async function confirmSources(
pairs: Pair[],
claimsFor: (id: number) => ReturnType<typeof claim>[],
compare: CompareReports,
+ onReviewError: (error: unknown) => void,
) {
const rejected = new Set<number>();
type Claim = ReturnType<typeof claim>;
const batch: { pair: Pair; A: Claim; B: Claim }[] = [];
async function gradeBatch() {
- const rows = batch.map(({ A, B }, id) => ({ id, A, B }));
- const { sources } = JSON.parse(
- await compare(
- sourceInstructions + "\n" + JSON.stringify(rows),
- sourceSchema,
- ),
- );
- const same = new Set(
- unambiguous(
- sources as { id: number; same: boolean; survivingPaths: string[] }[],
- (s) => s.id,
- )
- .filter((s) => s.same === true && !s.survivingPaths.length)
- .map((s) => s.id),
+ const results = gradeInContext(
+ batch,
+ (part) =>
+ compare(
+ sourceInstructions +
+ "\n" +
+ JSON.stringify(part.map(({ A, B }, id) => ({ id, A, B }))),
+ sourceSchema,
+ ),
+ ({ pair }, error) => {
+ rejected.add(pair.id);
+ onReviewError(error);
+ },
);
- for (const [id, { pair }] of batch.entries())
- if (!same.has(id)) rejected.add(pair.id);
+ for await (const [part, response] of results) {
+ const { sources } = JSON.parse(response);
+ const same = new Set(
+ unambiguous(
+ sources as { id: number; same: boolean; survivingPaths: string[] }[],
+ (s) => s.id,
+ )
+ .filter((s) => s.same === true && !s.survivingPaths.length)
+ .map((s) => s.id),
+ );
+ for (const [id, { pair }] of part.entries())
+ if (!same.has(id)) rejected.add(pair.id);
+ }
batch.length = 0;
}
// A single surviving path rejects the pair. Generate only the cases we still need.
@@ -140,7 +177,7 @@ async function confirmSources(
return pairs.filter(({ id }) => !rejected.has(id));
}
-function sharesSource(a: ComparisonReport, b: ComparisonReport) {
+export function sharesSource(a: ComparisonReport, b: ComparisonReport) {
return (
a.locations.some(([path, start, end]) =>
b.locations.some(
@@ -166,6 +203,7 @@ export async function checkSameFix(
originals: Map<number, Record<string, unknown>[]>,
compare: CompareReports,
onReviewError: (error: unknown) => void,
+ fallbackOverlap?: (left: number, right: number) => boolean,
) {
const evidence = records.map((record) =>
record.evidence.flatMap(([path, code]) => (path ? [{ path, code }] : [])),
@@ -185,9 +223,12 @@ export async function checkSameFix(
if (prior !== undefined && priorTargets[left] !== prior) continue;
if (
prior === undefined &&
- !(a && b
- ? a.some((key) => b.includes(key))
- : sharesSource(records[left]!, records[right]!))
+ !(
+ fallbackOverlap?.(left, right) ??
+ (a && b
+ ? a.some((key) => b.includes(key))
+ : sharesSource(records[left]!, records[right]!))
+ )
)
continue;
if (
@@ -208,7 +249,7 @@ export async function checkSameFix(
const distinct = new Map<string, ReturnType<typeof claim>>();
for (const source of originals.get(id)!) {
const row = claim(source);
- distinct.set(JSON.stringify(row), row);
+ distinct.set(canonicalJson(row), row);
}
rows = [...distinct.values()];
claims.set(id, rows);
@@ -216,7 +257,7 @@ export async function checkSameFix(
return rows;
}
const matches = new Map<number, Set<number>>();
- async function reviewPairs(pairs: Pair[]) {
+ function requestReview(pairs: Pair[]) {
const reports = [
...new Set(pairs.flatMap(({ left, right }) => [left, right])),
].map((id) => {
@@ -232,12 +273,13 @@ export async function checkSameFix(
attached: rest,
};
});
- const { reviews } = JSON.parse(
- await compare(
- instructions + "\n" + JSON.stringify({ pairs, reports }),
- schema,
- ),
+ return compare(
+ instructions + "\n" + JSON.stringify({ pairs, reports }),
+ schema,
);
+ }
+ async function acceptReviews(pairs: Pair[], response: string) {
+ const { reviews } = JSON.parse(response);
const proposed: Pair[] = [];
const decisions = unambiguous(
reviews as {
@@ -260,6 +302,7 @@ export async function checkSameFix(
proposed,
claimsFor,
compare,
+ onReviewError,
)) {
const earlier = matches.get(right) ?? new Set<number>();
earlier.add(left);
@@ -268,7 +311,11 @@ export async function checkSameFix(
}
async function reviewBatch(pairs: Pair[]) {
try {
- await reviewPairs(pairs);
+ const results = gradeInContext(pairs, requestReview, (_pair, error) =>
+ onReviewError(error),
+ );
+ for await (const [part, response] of results)
+ await acceptReviews(part, response);
} catch (error) {
onReviewError(error);
}
diff --git a/plugins/codex-security/mcp-app/src/record.ts b/plugins/codex-security/mcp-app/src/record.ts
index 57deeeb70..9b8f1d635 100644
--- a/plugins/codex-security/mcp-app/src/record.ts
+++ b/plugins/codex-security/mcp-app/src/record.ts
@@ -9,3 +9,17 @@ export function asRecord(value: unknown): Record<string, unknown> | undefined {
export function isNonEmptyString(value: unknown): value is string {
return typeof value === "string" && value.trim().length > 0;
}
+
+/** Preserve array order while comparing JSON objects independently of field order. */
+export function canonicalJson(input: unknown): string {
+ return JSON.stringify(input, (_key, value: unknown) => {
+ const object = asRecord(value);
+ return object
+ ? Object.fromEntries(
+ Object.keys(object)
+ .sort()
+ .map((key) => [key, object[key]]),
+ )
+ : value;
+ });
+}
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_comparison_context.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_comparison_context.ts
new file mode 100644
index 000000000..c36e0073c
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_comparison_context.ts
@@ -0,0 +1,268 @@
+import assert from "node:assert/strict";
+import { join } from "node:path";
+import { test } from "node:test";
+import type { ScanUnion } from "../src/deep-scan/artifact-validation.js";
+import { standardScanDraft } from "./deep_scan_coordinator_fixture.ts";
+import { importSource } from "./import-module.ts";
+
+const { mergeBatch } = await importSource(
+ join(import.meta.dirname, "../src/deep-scan/iteration.ts"),
+);
+
+function worker(id: string, titles: string[]) {
+ const draft = standardScanDraft("fixture", "finding", id);
+ const source = draft.findings[0]!;
+ return {
+ id,
+ resultPath: join("/tmp", id, "result.json"),
+ result: {
+ ...draft,
+ findings: titles.map((title) => ({ ...source, title })),
+ },
+ };
+}
+
+type Row = {
+ id: number;
+ title: string;
+};
+function grade(rows: Row[], defect: (row: Row) => string) {
+ const groups = Map.groupBy(rows, defect);
+ return JSON.stringify({
+ uncertain: [],
+ defects: [...groups.values()].map((rows) => ({
+ operation: "query",
+ failure: "check",
+ path: "fixture.py",
+ reports: rows.map(({ id }) => ({ id, evidence: 0 })),
+ })),
+ });
+}
+
+test("pairwise grades must agree on every member before forming a repair group", async () => {
+ const result: ScanUnion = { scanId: "fixture", findings: [] };
+ await mergeBatch(
+ result,
+ [worker("first", ["A", "B", "C"])],
+ "/tmp",
+ async (prompt: string) => {
+ const rows: Row[] = JSON.parse(prompt.split("\n").at(-1)!);
+ if (rows.length > 2)
+ throw Object.assign(new Error("Input too large"), {
+ code: "context_length_exceeded",
+ });
+ return grade(rows, (row) =>
+ rows.some(({ title }) => title === "A") ? "common" : row.title,
+ );
+ },
+ () => {},
+ );
+ assert.deepEqual(
+ result.findings.map(({ title }) => title),
+ ["A", "C"],
+ );
+});
+
+for (const assignment of [
+ "overlapping",
+ "uncertain",
+ "equal at distinct sites",
+])
+ test(`${assignment} repair assignments still reach disputed review after splitting`, async () => {
+ const result: ScanUnion = { scanId: "fixture", findings: [] };
+ const observation = worker("first", ["A", "B", "Unrelated"]);
+ const findings = observation.result.findings.map((finding) => ({
+ ...finding,
+ ...(assignment === "equal at distinct sites" && {
+ ruleId: finding.title,
+ identity: { anchor: finding.title },
+ }),
+ codeEvidence: [
+ {
+ id: "site",
+ path: "fixture.py",
+ label: "query",
+ explanation: "call site",
+ startLine: 1,
+ code: "query()",
+ },
+ ],
+ }));
+ let reviewed = 0;
+ await mergeBatch(
+ result,
+ [{ ...observation, result: { ...observation.result, findings } }],
+ "/tmp",
+ async (prompt: string, schema: { properties: object }) => {
+ const input = JSON.parse(prompt.split("\n").at(-1)!);
+ if ("reviews" in schema.properties) {
+ assert.deepEqual(input.pairs, [{ id: 0, left: 0, right: 1 }]);
+ reviewed++;
+ return JSON.stringify({
+ reviews: [
+ { pair: 0, decision: "same", leftEvidence: 0, rightEvidence: 0 },
+ ],
+ });
+ }
+ if ("sources" in schema.properties)
+ return JSON.stringify({
+ sources: [{ id: 0, same: true, survivingPaths: [] }],
+ });
+ const rows = input as Row[];
+ if (rows.length > 2) throw new Error("context_length_exceeded");
+ const overlaps = rows.every(({ title }) => title !== "Unrelated");
+ const answer = JSON.parse(
+ grade(rows, (row) => (overlaps ? "common" : row.title)),
+ );
+ if (overlaps && assignment === "uncertain")
+ return JSON.stringify({
+ uncertain: rows.map(({ id }) => id),
+ defects: [],
+ });
+ if (overlaps && assignment === "overlapping")
+ answer.defects.push({
+ ...answer.defects[0],
+ reports: [{ id: 0, evidence: 0 }],
+ });
+ return JSON.stringify(answer);
+ },
+ () => {},
+ );
+ assert.equal(reviewed, 1);
+ assert.deepEqual(
+ result.findings.map(({ title }) => title),
+ assignment === "equal at distinct sites"
+ ? ["A", "B", "Unrelated"]
+ : ["A", "Unrelated"],
+ );
+ });
+
+for (const failingPass of ["reviews", "sources"]) {
+ test(`oversized disputed ${failingPass} leaves its pair separate and retains completed comparisons`, async () => {
+ const { checkSameFix } = await importSource(
+ join(import.meta.dirname, "../src/deep-scan/same-fix.ts"),
+ );
+ const titles = ["Large", "Small", "Alias"];
+ const records = titles.map(() => ({
+ locations: [["file.ts", 1, 1]],
+ evidence: [["file.ts", "read()"]],
+ }));
+ const errors: Error[] = [];
+ const size = new Error("context_length_exceeded");
+ const matches = await checkSameFix(
+ [
+ { id: 0, worker: -1 },
+ { id: 1, worker: -1 },
+ { id: 2, worker: 0 },
+ ],
+ [],
+ records,
+ new Map([
+ [0, [1]],
+ [1, [1]],
+ [2, [1]],
+ ]),
+ new Map(titles.map((title, id) => [id, [{ title }]])),
+ async (prompt: string, schema: { properties: object }) => {
+ const rows = JSON.parse(prompt.split("\n").at(-1)!);
+ if ("reviews" in schema.properties) {
+ const pairs = rows.pairs as { id: number; left: number }[];
+ if (failingPass === "reviews" && pairs.some(({ left }) => left === 0))
+ throw size;
+ return JSON.stringify({
+ reviews: pairs.map(({ id }) => ({
+ pair: id,
+ decision: "same",
+ leftEvidence: 0,
+ rightEvidence: 0,
+ })),
+ });
+ }
+ const claims = rows as { id: number; B: { title: string } }[];
+ if (
+ failingPass === "sources" &&
+ claims.some(({ B }) => B.title === "Large")
+ )
+ throw size;
+ return JSON.stringify({
+ sources: claims.map(({ id }) => ({
+ id,
+ same: true,
+ survivingPaths: [],
+ })),
+ });
+ },
+ (error: Error) => errors.push(error),
+ );
+ assert.deepEqual(matches.get(2), new Set([1]));
+ assert.equal(errors.length, 1);
+ assert.equal(errors[0], size);
+ });
+}
+
+test("repeated observations are compared once without dropping distinct attack or validation evidence", async () => {
+ const left = {
+ title: "Left",
+ attackPath: { impact: "read", resource: "tenant" },
+ validation: { evidence: "test A" },
+ };
+ const right = { title: "Right" };
+ const copies = [
+ left,
+ { ...left, attackPath: { resource: "tenant", impact: "read" } },
+ ];
+ const additional = {
+ ...left,
+ attackPath: { impact: "write" },
+ validation: { evidence: "test B" },
+ };
+ const observed: unknown[] = [];
+ const { checkSameFix } = await importSource(
+ join(import.meta.dirname, "../src/deep-scan/same-fix.ts"),
+ );
+ const groups = [
+ { id: 0, worker: -1 },
+ { id: 1, worker: 0 },
+ ];
+ const records = groups.map(() => ({
+ locations: [["fixture.ts", 1, 1]],
+ evidence: [["fixture.ts", "read()"]],
+ }));
+ const matches = await checkSameFix(
+ groups,
+ [],
+ records,
+ new Map(),
+ new Map([
+ [0, [...copies, additional]],
+ [1, [right, right]],
+ ]),
+ async (prompt: string, schema: { properties: object }) => {
+ const input = JSON.parse(prompt.split("\n").at(-1)!);
+ if ("reviews" in schema.properties) {
+ assert.equal(input.reports[0].attached.length, 1);
+ return JSON.stringify({
+ reviews: [
+ { pair: 0, decision: "same", leftEvidence: 0, rightEvidence: 0 },
+ ],
+ });
+ }
+ for (const { B } of input) observed.push([B.attackPath, B.validation]);
+ return JSON.stringify({
+ sources: input.map(({ id }: { id: number }) => ({
+ same: true,
+ survivingPaths: [],
+ id,
+ })),
+ });
+ },
+ (error: unknown) => {
+ throw error;
+ },
+ );
+ assert.deepEqual(observed, [
+ [left.attackPath, left.validation],
+ [additional.attackPath, additional.validation],
+ ]);
+ assert.deepEqual(matches.get(1), new Set([0]));
+});
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_iterations.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_iterations.ts
index d4358d92d..bd9e47929 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_iterations.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_iterations.ts
@@ -197,6 +197,24 @@ test("an exact repeat of a merged source saturates without another comparison",
);
});
+test("reordered extension fields do not spend a comparison or restart the no-new streak", async () => {
+ const first = {
+ ...base,
+ extensions: { observed: { input: "request", operation: "lookup" } },
+ };
+ const repeat = {
+ ...base,
+ extensions: { observed: { operation: "lookup", input: "request" } },
+ };
+ const { terminal, draft } = await scanLoop([[first], [repeat]], {
+ maxDiscoveryRuns: 2,
+ stopAfterNoNew: 1,
+ });
+ assert.equal(terminal.terminalReason, "saturated");
+ assert.equal(draft.findings.length, 1);
+ assert.equal(draft.findings[0].provenance.sourceFindings?.length, 2);
+});
+
test("one scan's distinct rule and anchor survive a cross-scan alias", async () => {
const extract = {
...base,