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(tooling): migrate tests to TypeScript and simplify tooling (#1219)
mldangelo-oai2d8053142e00
diff --git a/.github/workflows/node-ci.yml b/.github/workflows/node-ci.ymlindex 3ffe73f16..0b500d6d3 100644--- a/.github/workflows/node-ci.yml+++ b/.github/workflows/node-ci.yml@@ -301,7 +301,7 @@ jobs:TMP: ${{ runner.temp }}TMPDIR: ${{ runner.temp }}CODEX_SECURITY_ALLOW_MACHINE_POLICY_TEST: "false"- run: node scripts/run-ci-tests.mjs ${{ matrix.shard }}/3 ${{ matrix.os == 'ubuntu-latest' && '--coverage --coverage-reporter=text --coverage-reporter=lcov' || '' }}+ run: node --experimental-strip-types scripts/run-ci-tests.mts ${{ matrix.shard }}/3 ${{ matrix.os == 'ubuntu-latest' && '--coverage --coverage-reporter=text --coverage-reporter=lcov' || '' }}- name: Upload test reportsif: always()continue-on-error: true@@ -416,7 +416,7 @@ jobs:- name: Install plugin dependenciesrun: pnpm --dir plugins/codex-security/mcp-app install --frozen-lockfile- name: Build and test the standalone host runtime- run: node --test plugins/codex-security/mcp-app/scripts/test_host_build.mjs+ run: node --experimental-strip-types --test plugins/codex-security/mcp-app/scripts/test_host_build.mtsplugin-source:name: plugin source contracts@@ -438,6 +438,11 @@ jobs:with:setup-node: falsecompile: true+ cache-dependency-path: |+ sdk/typescript/pnpm-lock.yaml+ plugins/codex-security/mcp-app/pnpm-lock.yaml+ - name: Install plugin dependencies+ run: pnpm --dir plugins/codex-security/mcp-app install --frozen-lockfile- name: Set up Pythonuses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0with:@@ -469,11 +474,13 @@ jobs:path: reports/python.xmlif-no-files-found: warnretention-days: 14+ - name: Build SDK and type-check eval tooling+ run: pnpm --dir sdk/typescript run build:evals- name: Test deterministic triage eval contractsworking-directory: evals/triage-findingrun: node --run test:deterministic- name: Test secret discovery eval contracts- run: node --test evals/secret-discovery/test_*.mjs+ run: node --experimental-strip-types --test evals/secret-discovery/test_*.mtsrequired-test:name: ${{ matrix.os }} / node-22@@ -562,7 +569,7 @@ jobs:TMP: ${{ steps.windows-temp.outputs.path }}TMPDIR: ${{ steps.windows-temp.outputs.path }}CODEX_SECURITY_ALLOW_MACHINE_POLICY_TEST: "false"- run: node sdk/typescript/scripts/run-ci-tests.mjs ${{ matrix.shard }}/7+ run: node --experimental-strip-types sdk/typescript/scripts/run-ci-tests.mts ${{ matrix.shard }}/7- name: Upload Windows test reportsif: always()continue-on-error: truediff --git a/.github/workflows/test-quality.yml b/.github/workflows/test-quality.ymlindex c0c383fce..ea3fe834b 100644--- a/.github/workflows/test-quality.yml+++ b/.github/workflows/test-quality.yml@@ -159,7 +159,8 @@ jobs:- uses: ./.github/actions/setup-toolswith:setup-node: false- compile: true+ - name: Install dependencies+ run: pnpm --dir sdk/typescript install --frozen-lockfile- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1with:pattern: runner-*@@ -171,10 +172,10 @@ jobs:comparison_status=0for os in ubuntu-latest windows-latest; dofor mode in isolated parallel; do- node sdk/typescript/scripts/compare-test-reports.mjs "reports/runner-$os-baseline.xml" "reports/runner-$os-$mode*.xml" >> "$GITHUB_STEP_SUMMARY" || comparison_status=1+ node --experimental-strip-types sdk/typescript/scripts/compare-test-reports.mts "reports/runner-$os-baseline.xml" "reports/runner-$os-$mode*.xml" >> "$GITHUB_STEP_SUMMARY" || comparison_status=1donedone- node sdk/typescript/scripts/compare-test-reports.mjs reports/runner-windows-latest-baseline.xml 'reports/runner-windows-latest-shard-*.xml' >> "$GITHUB_STEP_SUMMARY" || comparison_status=1+ node --experimental-strip-types sdk/typescript/scripts/compare-test-reports.mts reports/runner-windows-latest-baseline.xml 'reports/runner-windows-latest-shard-*.xml' >> "$GITHUB_STEP_SUMMARY" || comparison_status=1exit "$comparison_status"mutation:diff --git a/evals/deep-reducer/README.md b/evals/deep-reducer/README.mdindex 15a6a68ee..f1e7f6f3c 100644--- a/evals/deep-reducer/README.md+++ b/evals/deep-reducer/README.md@@ -6,10 +6,11 @@ The fixture has a 32 MiB individual semantic field containing quotes, backslasheThe grader requires a smaller byte budget on the retry with the same cursor and reference, byte-bounded subsequent pages, one successful result submission, every source accounted for exactly once, complete original payloads matching their SHA-256 hashes, and preservation of the previous finding identity and synthesized history.-Install the MCP app's locked dependencies using the repository's normal setup, then run from the repository root:+Install the SDK and MCP app’s locked dependencies using the repository's normal setup, then run from the repository root:```sh-node evals/deep-reducer/run.mjs+pnpm --dir sdk/typescript run build:evals+node --experimental-strip-types evals/deep-reducer/run.mts```The run uses the caller's normal Codex credentials and configuration and consumes model usage. An optional final argument selects a model; otherwise Codex uses its configured model. This eval is opt-in and does not run in CI. The model receives only the production reducer prompt and must discover and carry out recovery itself. The SDK does not expose every code-mode error event, so the grade relies on the observed oversized response, changed request, and saved result.@@ -23,5 +24,5 @@ The same fixture, real transport, and grading helpers live under `plugins/codex-Run that test directly from the repository root:```sh-node --test plugins/codex-security/mcp-app/tests/test_deep_reducer_paging_eval.mjs+node --experimental-strip-types --test plugins/codex-security/mcp-app/tests/test_deep_reducer_paging_eval.ts```diff --git a/evals/deep-reducer/run.mjs b/evals/deep-reducer/run.mtssimilarity index 64%rename from evals/deep-reducer/run.mjsrename to evals/deep-reducer/run.mtsindex 81b8c5373..c7c612946 100644--- a/evals/deep-reducer/run.mjs+++ b/evals/deep-reducer/run.mts@@ -1,13 +1,12 @@import { mkdir, mkdtemp } from "node:fs/promises";import path from "node:path";-import { fileURLToPath } from "node:url";-import { runReducerPagingEval } from "../../plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging.mjs";+import { runReducerPagingEval } from "../../plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging.ts";-const reports = fileURLToPath(new URL("./reports/", import.meta.url));+const reports = path.join(import.meta.dirname, "reports/");await mkdir(reports, { recursive: true });const root = await mkdtemp(path.join(reports, "deep-reducer-paging-"));console.log(`Eval artifacts: ${root}`);-const report = await runReducerPagingEval({+const { report } = await runReducerPagingEval({root,mode: "model",model: process.argv[2],diff --git a/evals/secret-discovery/README.md b/evals/secret-discovery/README.mdindex b581e79d9..05dbeed6b 100644--- a/evals/secret-discovery/README.md+++ b/evals/secret-discovery/README.md@@ -39,12 +39,15 @@ Credential values remain part of the source evidence.## RunInstall dependencies and build the TypeScript SDK using the repository's normal-setup. The eval reuses the pinned Codex SDK, CLI, esbuild, and SDK helper for+setup, including the MCP app dependencies. `build:evals` type-checks the eval+sources; Node runs them directly with `--experimental-strip-types`. The eval+reuses the pinned Codex SDK, CLI, esbuild, and SDK helper forcreating private homes. It bundles the MCP app's permission-profile preflightlocally with esbuild and adds no dependencies.```sh-node evals/secret-discovery/run.mjs+pnpm --dir sdk/typescript run build:evals+node --experimental-strip-types evals/secret-discovery/run.mts```An optional positional argument selects a model. When omitted, Codex selects@@ -100,7 +103,8 @@ the model.## Deterministic checks```sh-node --test evals/secret-discovery/test_*.mjs+pnpm --dir sdk/typescript run build:evals+node --experimental-strip-types --test evals/secret-discovery/test_*.mts```CI runs these Node-only checks for fixture staging, production-prompt loading,diff --git a/evals/secret-discovery/fixtures.mjs b/evals/secret-discovery/fixtures.mtssimilarity index 51%rename from evals/secret-discovery/fixtures.mjsrename to evals/secret-discovery/fixtures.mtsindex 0630a023c..5d90cf2ce 100644--- a/evals/secret-discovery/fixtures.mjs+++ b/evals/secret-discovery/fixtures.mts@@ -16,46 +16,36 @@ export function createFixture() {privateKeyEncoding: { type: "pkcs8", format: "pem" },publicKeyEncoding: { type: "spki", format: "pem" },});- const files = {- "README.md": [- "# Delivery worker",- "",- "A small delivery worker with service clients, message signing, deployment",- "configuration, and integration checks. Source access is also granted to",- "reviewers who do not operate the delivery service or its database.",- "",- ].join("\n"),- "src/client.py": [- "from urllib.request import Request",- "",- `SERVICE_TOKEN = "${values.active}"`,- "",- "def delivery_request(payload):",- ' return Request("https://delivery.example.test/messages", data=payload,',- ' headers={"Authorization": "Bearer " + SERVICE_TOKEN})',- "",- ].join("\n"),- "src/retired.py": [- '"""Retired client settings, no longer imported by the worker."""',- `LEGACY_SERVICE_TOKEN = "${values.unused}"`,- 'LEGACY_ENDPOINT = "https://archive.example.test/messages"',- "",- ].join("\n"),- "tests/test_delivery.py": [- "from urllib.request import Request",- "",- `INTEGRATION_TOKEN = "${values.integration}"`,- "",- "def integration_request():",- ' return Request("https://delivery.example.test/health",',- ' headers={"Authorization": "Bearer " + INTEGRATION_TOKEN})',- "",- ].join("\n"),- "config/.env": [- `DATABASE_URL=postgresql://delivery:${values.database}@db.example.test:5432/delivery`,- "LOG_LEVEL=info",- "",- ].join("\n"),+ const files: Record<string, string> = {+ "README.md": `# Delivery worker++A small delivery worker with service clients, message signing, deployment+configuration, and integration checks. Source access is also granted to+reviewers who do not operate the delivery service or its database.+`,+ "src/client.py": `from urllib.request import Request++SERVICE_TOKEN = "${values.active}"++def delivery_request(payload):+ return Request("https://delivery.example.test/messages", data=payload,+ headers={"Authorization": "Bearer " + SERVICE_TOKEN})+`,+ "src/retired.py": `"""Retired client settings, no longer imported by the worker."""+LEGACY_SERVICE_TOKEN = "${values.unused}"+LEGACY_ENDPOINT = "https://archive.example.test/messages"+`,+ "tests/test_delivery.py": `from urllib.request import Request++INTEGRATION_TOKEN = "${values.integration}"++def integration_request():+ return Request("https://delivery.example.test/health",+ headers={"Authorization": "Bearer " + INTEGRATION_TOKEN})+`,+ "config/.env": `DATABASE_URL=postgresql://delivery:${values.database}@db.example.test:5432/delivery+LOG_LEVEL=info+`,"config/service.json":JSON.stringify({@@ -67,55 +57,46 @@ export function createFixture() {null,2,) + "\n",- "src/signing.mjs": [- 'import { sign } from "node:crypto";',- `const signingKey = \`${privateKey.trim()}\`;`,- "export function signMessage(message) {",- " return sign(null, Buffer.from(message), signingKey);",- "}",- "",- ].join("\n"),- "src/runtime_config.py": [- "import os",- "",- 'SERVICE_TOKEN = os.environ["SERVICE_TOKEN"]',- 'DATABASE_URL = os.environ["DATABASE_URL"]',- "",- ].join("\n"),- "examples/settings.env.example": [- "SERVICE_TOKEN=YOUR_SERVICE_TOKEN_HERE",- "DATABASE_PASSWORD=<replace-with-secret>",- "",- ].join("\n"),+ "src/signing.mjs": `import { sign } from "node:crypto";+const signingKey = \`${privateKey.trim()}\`;+export function signMessage(message) {+ return sign(null, Buffer.from(message), signingKey);+}+`,+ "src/runtime_config.py": `import os++SERVICE_TOKEN = os.environ["SERVICE_TOKEN"]+DATABASE_URL = os.environ["DATABASE_URL"]+`,+ "examples/settings.env.example": `SERVICE_TOKEN=YOUR_SERVICE_TOKEN_HERE+DATABASE_PASSWORD=<replace-with-secret>+`,"config/public.pem": publicKey,- "src/constants.py": [- 'PUBLIC_CLIENT_ID = "delivery-worker"',- 'CONTENT_SHA256 = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"',- "",- ].join("\n"),+ "src/constants.py": `PUBLIC_CLIENT_ID = "delivery-worker"+CONTENT_SHA256 = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"+`,};const positive = (- id,- path,- marker,- cwes,- consumerMarker,+ id: string,+ path: string,+ marker: string,+ cwes: string[],+ consumerMarker?: string,endMarker = marker,- ) => ({- id,- path,- line:- files[path].split("\n").findIndex((line) => line.includes(marker)) + 1,- endLine:- files[path].split("\n").findIndex((line) => line.includes(endMarker)) + 1,- lineCount: files[path].trimEnd().split("\n").length,- consumerLine: consumerMarker- ? files[path]- .split("\n")- .findIndex((line) => line.includes(consumerMarker)) + 1- : null,- cwes,- });+ ) => {+ const lines = files[path].split("\n");+ return {+ id,+ path,+ line: lines.findIndex((line) => line.includes(marker)) + 1,+ endLine: lines.findIndex((line) => line.includes(endMarker)) + 1,+ lineCount: files[path].trimEnd().split("\n").length,+ consumerLine: consumerMarker+ ? lines.findIndex((line) => line.includes(consumerMarker)) + 1+ : null,+ cwes,+ };+ };return {files,positives: [@@ -182,7 +163,10 @@ export function createFixture() {}/** Stage source only. Gold labels and secret-value lookup stay in the harness. */-export async function writeFixture(repo, fixture) {+export async function writeFixture(+ repo: string,+ fixture: ReturnType<typeof createFixture>,+) {for (const [relative, contents] of Object.entries(fixture.files)) {const destination = join(repo, relative);await mkdir(dirname(destination), { recursive: true });diff --git a/evals/secret-discovery/grade.mjs b/evals/secret-discovery/grade.mtssimilarity index 80%rename from evals/secret-discovery/grade.mjsrename to evals/secret-discovery/grade.mtsindex 483b01e64..94ff16c08 100644--- a/evals/secret-discovery/grade.mjs+++ b/evals/secret-discovery/grade.mts@@ -1,4 +1,37 @@import { posix, win32 } from "node:path";+import type { createFixture } from "./fixtures.mts";+type Fixture = ReturnType<typeof createFixture>;+type Expected = Fixture["positives"][number];+export interface SourceLocation {+ path: string;+ startLine: number;+ endLine?: number | null;+ role?: string;+}+export interface CodeEvidence {+ path: string;+ startLine: number;+ code?: string;+ [key: string]: unknown;+}+export interface Finding {+ taxonomy?: { category?: string; cwe?: string[] };+ locations?: SourceLocation[];+ codeEvidence?: CodeEvidence[];+ [key: string]: unknown;+}+export interface Coverage {+ completeness?: string;+ deferred?: unknown[];+ surfaces?: { disposition?: string; [key: string]: unknown }[];+ explicitExclusions?: { pattern: string; reason?: string }[];+ openQuestions?: unknown[];+}+export interface Result {+ findings?: Finding[];+ coverage?: Coverage;+ [key: string]: unknown;+}const explicitSecretTerms =/secrets?|credentials?|passwords?|tokens?|(?:api|private)[-_ ]?keys?|hard[-_ ]?cod(?:e[ds]?|ing)?/i;@@ -12,12 +45,12 @@ const negatedSecretCategory = new RegExp(String.raw`^(?:(?:not(?:[-_\s]*a)?|non|no|without)[-_\s]*(?:${secretTerms})\b|(?:${secretTerms})[-_\s]*free\b)`,"i",);-const supportingLocation = (location) =>+const supportingLocation = (location: SourceLocation) =>/^(?:supporting|support|context|consumer|expected_control)$/.test(location.role ?? "",);-function matchesExclusion(path, pattern, repo) {+function matchesExclusion(path: string, pattern: string, repo?: string) {const paths = [repo, pattern].some((value) => value && win32.parse(value).root.length > 1,)@@ -61,19 +94,19 @@ function matchesExclusion(path, pattern, repo) {}}-function normalizeSourcePath(path) {+function normalizeSourcePath(path: string | undefined) {const normalized = path?.replaceAll("\\", "/").replace(/^(?:\.\/)+/u, "");return process.platform === "win32" ? normalized?.toLowerCase() : normalized;}-function sourceText(path, fixture) {+function sourceText(path: string, fixture: Fixture) {return Object.entries(fixture.files).find(([sourcePath]) =>normalizeSourcePath(sourcePath) === normalizeSourcePath(path),)?.[1];}-function validSourceRange(location, fixture) {+function validSourceRange(location: SourceLocation, fixture: Fixture) {const source = sourceText(location.path, fixture);const end = location.endLine ?? location.startLine;return (@@ -86,7 +119,7 @@ function validSourceRange(location, fixture) {);}-function matchesLocation(location, expected) {+function matchesLocation(location: SourceLocation, expected: Expected) {const end = location.endLine ?? location.startLine;return (normalizeSourcePath(location.path) === normalizeSourcePath(expected.path) &&@@ -101,7 +134,7 @@ function matchesLocation(location, expected) {);}-function matches(finding, expected) {+function matches(finding: Finding, expected: Expected) {const category = (finding.taxonomy?.category ?? "").replaceAll("_", " ").replace(/([a-z])([A-Z])/g, "$1 $2")@@ -121,7 +154,7 @@ function matches(finding, expected) {);}-function matchesConsumer(location, expected) {+function matchesConsumer(location: SourceLocation, expected: Expected) {return (expected.consumerLine !== null &&matchesLocation(location, {@@ -132,7 +165,7 @@ function matchesConsumer(location, expected) {);}-function validEvidence(finding, fixture) {+function validEvidence(finding: Finding, fixture: Fixture) {const evidence = (finding.codeEvidence ?? []).map((entry) => {const code = entry.code?.replaceAll("\r\n", "\n").replace(/\n$/u, "");return {@@ -148,7 +181,7 @@ function validEvidence(finding, fixture) {Boolean(entry.code?.trim()) &&validSourceRange(entry, fixture) &&entry.code ===- sourceText(entry.path, fixture)+ sourceText(entry.path, fixture)!.replaceAll("\r\n", "\n").split("\n").slice(entry.startLine - 1, entry.endLine)@@ -167,7 +200,11 @@ function validEvidence(finding, fixture) {}/** Grade retained final findings, never keyword mentions or deferred candidates. */-export function gradeResult(result, fixture, repo) {+export function gradeResult(+ result: Result | undefined,+ fixture: Fixture,+ repo?: string,+) {const errors = [];const findings = Array.isArray(result?.findings) ? result.findings : [];if (!Array.isArray(result?.findings)) errors.push("missing findings array");@@ -175,7 +212,11 @@ export function gradeResult(result, fixture, repo) {id: expected.id,found: findings.some((finding) => matches(finding, expected)),}));- const falsePositives = findings.flatMap((finding, index) => {+ const falsePositives = findings.flatMap<{+ index: number;+ unexpectedLocations?: string[];+ reason?: string;+ }>((finding, index) => {const matchedCases = fixture.positives.filter((expected) =>matches(finding, expected),);diff --git a/evals/secret-discovery/harness.mjs b/evals/secret-discovery/harness.mtssimilarity index 82%rename from evals/secret-discovery/harness.mjsrename to evals/secret-discovery/harness.mtsindex c2c244862..372148fa0 100644--- a/evals/secret-discovery/harness.mjs+++ b/evals/secret-discovery/harness.mts@@ -1,9 +1,9 @@+import type { ThreadOptions } from "../../sdk/typescript/node_modules/@openai/codex-sdk/dist/index.js";import { createHash } from "node:crypto";import { cp, mkdir, readFile, writeFile } from "node:fs/promises";import { dirname, join, resolve } from "node:path";-import { fileURLToPath } from "node:url";-import { createFixture, writeFixture } from "./fixtures.mjs";-import { gradeResult } from "./grade.mjs";+import { createFixture, writeFixture } from "./fixtures.mts";+import { gradeResult } from "./grade.mts";import {DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID,bundledCodexSdkEnvironment,@@ -11,14 +11,35 @@ import {executablePathForSpawn,inlineToml,preflightDeepScanWorkerPermissionProfile,-} from "./runtime.mjs";+} from "./runtime.mts";-const pluginRoot = fileURLToPath(- new URL("../../plugins/codex-security/", import.meta.url),-);+export type PreparedEval = Awaited<ReturnType<typeof prepareEval>>;+export interface EvalSettings {+ codexPathOverride: string;+ env: Record<string, string>;+ configOverrides: string[];+ apiKey?: string;+}+interface EvalEvent {+ type: string;+ message?: string;+ item?: { type: string; text?: string; message?: string };+ usage?: unknown;+ error?: { message: string };+}+export interface EvalCodex {+ startThread(settings: ThreadOptions): {+ runStreamed(+ prompt: string,+ options: { outputSchema: unknown; signal: AbortSignal },+ ): Promise<{ events: AsyncIterable<EvalEvent> }>;+ };+}++const pluginRoot = join(import.meta.dirname, "../../plugins/codex-security/");const text = { type: "string" };-const array = (items) => ({ type: "array", items });-const object = (properties) => ({+const array = (items: unknown) => ({ type: "array", items });+const object = (properties: Record<string, unknown>) => ({type: "object",properties,required: Object.keys(properties),@@ -97,7 +118,7 @@ export const outputSchema = object({});export async function prepareEval(- root,+ root: string,{ corePromptPath = join(pluginRoot, "references/core-scan.md") } = {},) {const repo = join(root, "repository");@@ -136,7 +157,10 @@ export async function prepareEval(};}-export function threadSettings(prepared, model) {+export function threadSettings(+ prepared: Pick<PreparedEval, "repo" | "runtime">,+ model?: string,+): ThreadOptions {return {...(model ? { model } : {}),workingDirectory: prepared.repo,@@ -149,7 +173,11 @@ export function threadSettings(prepared, model) {};}-export function codexSettings(home, codexPath, environment = process.env) {+export function codexSettings(+ home: string,+ codexPath: string,+ environment = process.env,+): EvalSettings {// Keep unrelated service credentials out of the eval process entirely.const inherited = new Set(["PATH",@@ -207,7 +235,7 @@ export function codexSettings(home, codexPath, environment = process.env) {};}-function permissionProfile(home, codexPath) {+function permissionProfile(home: string, codexPath: string) {return {filesystem: {":minimal": "read",@@ -219,7 +247,11 @@ function permissionProfile(home, codexPath) {};}-export async function preflightEval(prepared, settings, signal) {+export async function preflightEval(+ prepared: Pick<PreparedEval, "repo">,+ settings: EvalSettings,+ signal?: AbortSignal,+) {const openAiApiKey = environmentEntry(settings.env, "OPENAI_API_KEY")?.trim();const codexApiKey = environmentEntry(settings.env, "CODEX_API_KEY")?.trim();const { useOpenAiApiKey } = await preflightDeepScanWorkerPermissionProfile({@@ -241,7 +273,7 @@ export async function preflightEval(prepared, settings, signal) {};}-function environmentEntry(environment, requested) {+function environmentEntry(environment: NodeJS.ProcessEnv, requested: string) {const exact = environment[requested];if (exact !== undefined || process.platform !== "win32") return exact;// Plain snapshots need the same case-insensitive lookup as Windows process.env.@@ -250,7 +282,11 @@ function environmentEntry(environment, requested) {)?.[1];}-export async function runPreparedEval(prepared, codex, { model, signal } = {}) {+export async function runPreparedEval(+ prepared: PreparedEval,+ codex: EvalCodex,+ { model, signal }: { model?: string; signal?: AbortSignal } = {},+) {const thread = codex.startThread(threadSettings(prepared, model));const controller = new AbortController();const combinedSignal = signal@@ -269,8 +305,8 @@ export async function runPreparedEval(prepared, codex, { model, signal } = {}) {const warning =event.type === "error"? event.message- : event.type === "item.completed" && event.item.type === "error"- ? event.item.message+ : event.type === "item.completed" && event.item!.type === "error"+ ? event.item!.message: undefined;const fallback = deepScanPermissionProfileFallbackError(warning);if (fallback && !failure) {@@ -281,14 +317,14 @@ export async function runPreparedEval(prepared, codex, { model, signal } = {}) {if (failure) continue;if (event.type === "item.completed" &&- event.item.type === "agent_message"+ event.item!.type === "agent_message") {- finalResponse = event.item.text;+ finalResponse = event.item!.text!;} else if (event.type === "turn.completed") {completed = true;usage = event.usage;} else if (event.type === "turn.failed") {- failure = new Error(event.error.message);+ failure = new Error(event.error!.message);}}} catch (error) {diff --git a/evals/secret-discovery/run.mjs b/evals/secret-discovery/run.mtssimilarity index 79%rename from evals/secret-discovery/run.mjsrename to evals/secret-discovery/run.mtsindex 580c219c7..760da86bd 100644--- a/evals/secret-discovery/run.mjs+++ b/evals/secret-discovery/run.mts@@ -1,8 +1,7 @@import { mkdir, mkdtemp, realpath, writeFile } from "node:fs/promises";import { join } from "node:path";-import { fileURLToPath } from "node:url";import { Codex } from "../../sdk/typescript/node_modules/@openai/codex-sdk/dist/index.js";-import { configuredCodexHome } from "../../sdk/typescript/dist/auth.js";+import * as auth from "../../sdk/typescript/dist/auth.js";import {createIsolatedHome,resolveCodexCommand,@@ -12,15 +11,18 @@ import {preflightEval,prepareEval,runPreparedEval,-} from "./harness.mjs";-import { createEvalHome, withEvalState } from "./runtime.mjs";+} from "./harness.mts";+import { withEvalState } from "./runtime.mts";-const reports = fileURLToPath(new URL("./reports/", import.meta.url));+const reports = join(import.meta.dirname, "reports/");await mkdir(reports, { recursive: true });const reportDirectory = await mkdtemp(join(reports, "run-"));console.log(`Eval artifacts: ${reportDirectory}`);await withEvalState(- () => createEvalHome(createIsolatedHome, configuredCodexHome(process.env)),+ createIsolatedHome,+ (+ auth as unknown as typeof import("../../sdk/typescript/src/auth.js")+ ).configuredCodexHome(process.env),async ({ root, home, signal }) => {const prepared = await prepareEval(root);const codexPath = await realpath(resolveCodexCommand({}).command);diff --git a/evals/secret-discovery/runtime.mjs b/evals/secret-discovery/runtime.mjsdeleted file mode 100644index 71294f012..000000000--- a/evals/secret-discovery/runtime.mjs+++ /dev/null@@ -1,92 +0,0 @@-import { link, mkdtemp, realpath, rm, stat } from "node:fs/promises";-import { tmpdir } from "node:os";-import { dirname, join } from "node:path";-import { fileURLToPath } from "node:url";-import { build } from "../../sdk/typescript/node_modules/esbuild/lib/main.js";--// Bundle production runtime helpers directly so deterministic checks need no SDK build.-const bundle = await build({- stdin: {- contents: [- 'export { DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID, preflightDeepScanWorkerPermissionProfile, deepScanPermissionProfileFallbackError } from "../../plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.ts";',- 'export { executablePathForSpawn } from "../../plugins/codex-security/mcp-app/src/deep-scan/executable-path.ts";',- 'export { inlineToml } from "../../sdk/typescript/src/config.ts";',- 'export { bundledCodexSdkEnvironment } from "../../sdk/typescript/src/codex-sdk-environment.ts";',- ].join("\n"),- resolveDir: fileURLToPath(new URL(".", import.meta.url)),- },- bundle: true,- format: "esm",- platform: "node",- write: false,-});-export const {- DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID,- preflightDeepScanWorkerPermissionProfile,- deepScanPermissionProfileFallbackError,- executablePathForSpawn,- inlineToml,- bundledCodexSdkEnvironment,-} = await import(- `data:text/javascript;base64,${Buffer.from(bundle.outputFiles[0].contents).toString("base64")}`-);--/** Share saved login updates without importing the caller's configuration. */-export async function createEvalHome(createHome, ambientHome) {- let auth;- try {- const path = await realpath(join(ambientHome, "auth.json"));- if ((await stat(path)).isFile()) auth = path;- } catch (error) {- if (error.code !== "ENOENT") throw error;- }- // A hard link must stay on the saved login's filesystem, including symlinks.- const home = await createHome(auth ? dirname(auth) : undefined);- try {- if (auth) await link(auth, join(home, "auth.json"));- return { home };- } catch (error) {- await rm(home, { recursive: true, force: true });- throw error;- }-}--/** Keep temporary source and the login link alive until cancelled work stops. */-export async function withEvalState(createHome, run) {- const controller = new AbortController();- let interrupted;- const handlers = ["SIGINT", "SIGTERM"].map((signal) => {- const handler = () => {- interrupted = signal;- process.exitCode = signal === "SIGINT" ? 130 : 143;- controller.abort(- new DOMException(`Eval interrupted by ${signal}`, "AbortError"),- );- };- process.on(signal, handler);- return [signal, handler];- });- let root;- let state;- try {- root = await mkdtemp(join(tmpdir(), "source-audit-"));- state = await createHome();- controller.signal.throwIfAborted();- const result = await run({ root, ...state, signal: controller.signal });- controller.signal.throwIfAborted();- return result;- } catch (error) {- if (!interrupted) throw error;- process.exitCode = interrupted === "SIGINT" ? 130 : 143;- } finally {- try {- await Promise.all(- [root, state?.home]- .filter(Boolean)- .map((path) => rm(path, { recursive: true, force: true })),- );- } finally {- for (const [signal, handler] of handlers) process.off(signal, handler);- }- }-}diff --git a/evals/secret-discovery/runtime.mts b/evals/secret-discovery/runtime.mtsnew file mode 100644index 000000000..7a3df7440--- /dev/null+++ b/evals/secret-discovery/runtime.mts@@ -0,0 +1,77 @@+import { link, mkdtemp, realpath, rm, stat } from "node:fs/promises";+import { tmpdir } from "node:os";+import { dirname, join } from "node:path";+import { importModule } from "../../plugins/codex-security/mcp-app/tests/import-module.ts";++// Bundle production runtime helpers directly for the deterministic checks.+export const {+ DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID,+ preflightDeepScanWorkerPermissionProfile,+ deepScanPermissionProfileFallbackError,+ executablePathForSpawn,+ inlineToml,+ bundledCodexSdkEnvironment,+} = await importModule({+ stdin: {+ contents: `export { DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID, preflightDeepScanWorkerPermissionProfile, deepScanPermissionProfileFallbackError } from "../../plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.ts";+export { executablePathForSpawn } from "../../plugins/codex-security/mcp-app/src/deep-scan/executable-path.ts";+export { inlineToml } from "../../sdk/typescript/src/config.ts";+export { bundledCodexSdkEnvironment } from "../../sdk/typescript/src/codex-sdk-environment.ts";`,+ resolveDir: import.meta.dirname,+ },+});++/** Keep temporary source and the login link alive until cancelled work stops. */+export async function withEvalState(+ createHome: (parent?: string) => Promise<string>,+ ambientHome: string,+ run: (state: {+ home: string;+ root: string;+ signal: AbortSignal;+ }) => Promise<void>,+) {+ const controller = new AbortController();+ let interrupted: NodeJS.Signals | undefined;+ const signals = ["SIGINT", "SIGTERM"];+ const handler = (signal: NodeJS.Signals) => {+ interrupted = signal;+ controller.abort(+ new DOMException(`Eval interrupted by ${signal}`, "AbortError"),+ );+ };+ for (const signal of signals) process.on(signal, handler);+ let root;+ let home;+ try {+ root = await mkdtemp(join(tmpdir(), "source-audit-"));+ // Share saved login updates without importing the caller's configuration.+ let auth;+ try {+ const path = await realpath(join(ambientHome, "auth.json"));+ if ((await stat(path)).isFile()) auth = path;+ } catch (error) {+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;+ }+ // A hard link must stay on the saved login's filesystem, including symlinks.+ const allocatedHome = await createHome(auth ? dirname(auth) : undefined);+ try {+ if (auth) await link(auth, join(allocatedHome, "auth.json"));+ } catch (error) {+ await rm(allocatedHome, { recursive: true, force: true });+ throw error;+ }+ home = allocatedHome;+ if (!interrupted) await run({ root, home, signal: controller.signal });+ } catch (error) {+ if (!interrupted) throw error;+ } finally {+ await Promise.all([+ root && rm(root, { recursive: true, force: true }),+ home && rm(home, { recursive: true, force: true }),+ ]).finally(() => {+ for (const signal of signals) process.off(signal, handler);+ if (interrupted) process.exitCode = interrupted === "SIGINT" ? 130 : 143;+ });+ }+}diff --git a/evals/secret-discovery/test_discovery.mjs b/evals/secret-discovery/test_discovery.mtssimilarity index 89%rename from evals/secret-discovery/test_discovery.mjsrename to evals/secret-discovery/test_discovery.mtsindex 0aae51d9e..3a4e8c39f 100644--- a/evals/secret-discovery/test_discovery.mjs+++ b/evals/secret-discovery/test_discovery.mts@@ -4,17 +4,31 @@ import { tmpdir } from "node:os";import { dirname, join, resolve } from "node:path";import test from "node:test";import { parse } from "../../sdk/typescript/node_modules/smol-toml/dist/index.js";-import { createFixture } from "./fixtures.mjs";-import { gradeResult } from "./grade.mjs";+import { createFixture } from "./fixtures.mts";+import type {+ CodeEvidence,+ Coverage,+ SourceLocation,+ Finding,+ Result,+} from "./grade.mts";+import type { EvalCodex } from "./harness.mts";+import type { ThreadOptions } from "../../sdk/typescript/node_modules/@openai/codex-sdk/dist/index.js";+import { gradeResult } from "./grade.mts";import {codexSettings,prepareEval,runPreparedEval,threadSettings,-} from "./harness.mjs";-import { DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID } from "./runtime.mjs";+} from "./harness.mts";+import { DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID } from "./runtime.mts";-function sourceEvidence(fixture, path, startLine, endLine = startLine) {+function sourceEvidence(+ fixture: ReturnType<typeof createFixture>,+ path: string,+ startLine: number,+ endLine = startLine,+) {const source = fixture.files[path].split("\n").slice(startLine - 1, endLine)@@ -29,7 +43,18 @@ function sourceEvidence(fixture, path, startLine, endLine = startLine) {};}-function retainedResult(fixture) {+interface TestFinding extends Finding {+ title?: string;+ taxonomy: { category: string; cwe: string[] };+ locations: SourceLocation[];+ codeEvidence: CodeEvidence[];+}+interface TestResult extends Result {+ findings: TestFinding[];+ coverage: Required<Coverage>;+ resolved_questions?: TestFinding[];+}+function retainedResult(fixture: ReturnType<typeof createFixture>): TestResult {return {findings: fixture.positives.map((expected) => ({title: "Embedded credential",@@ -51,6 +76,11 @@ function retainedResult(fixture) {};}+function retainedFixture() {+ const fixture = createFixture();+ return { fixture, result: retainedResult(fixture) };+}+test("counts every retained secret location, including unused and integration source", () => {const fixture = createFixture();const report = gradeResult(retainedResult(fixture), fixture);@@ -70,8 +100,7 @@ test("counts every retained secret location, including unused and integration so});test("resolved, deferred, and intermediate findings do not satisfy final recall", () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();result.coverage.deferred = result.findings;result.resolved_questions = result.findings;result.findings = [];@@ -83,38 +112,37 @@ test("resolved, deferred, and intermediate findings do not satisfy final recall"for (const [name, mutate] of [["wrong source line",- (finding) => {+ (finding: TestFinding) => {finding.locations[0].startLine = 1;},],["wrong source path",- (finding) => {+ (finding: TestFinding) => {finding.locations[0].path = "src/absent.py";},],["unrelated CWE",- (finding) => {+ (finding: TestFinding) => {finding.taxonomy.cwe = ["CWE-89"];},],["unrelated category",- (finding) => {+ (finding: TestFinding) => {finding.taxonomy.category = "sql-injection";},],["line range outside source",- (finding) => {+ (finding: TestFinding) => {finding.locations[0].endLine = 100000;},],-]) {+] as const) {test(`rejects ${name}`, () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();mutate(result.findings[0]);const report = gradeResult(result, fixture);assert.equal(report.passed, false);@@ -142,8 +170,7 @@ for (const category of ["without-credentials",]) {test(`rejects negated category ${category}`, () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();result.findings[0].taxonomy.category = category;const report = gradeResult(result, fixture);assert.equal(report.passed, false);@@ -180,8 +207,7 @@ test("accepts plural and identifier-style credential categories", () => {});test("accepts credential exposure with non-secret context in the category", () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();for (const category of ["credential exposure with non-secret context","credential exposure with no-secret context",@@ -194,8 +220,7 @@ test("accepts credential exposure with non-secret context in the category", () =});test("accepts a secret's valid source range and sensitive-data category", () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();result.findings[0].locations[0].endLine = fixture.positives[0].line + 1;result.findings[0].taxonomy.category = "sensitive-data-exposure";assert.equal(gradeResult(result, fixture).passed, true);@@ -207,8 +232,7 @@ for (const category of ["api-key-exposure",]) {test(`accepts credential category ${category}`, () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();result.findings[0].taxonomy.category = category;assert.equal(gradeResult(result, fixture).passed, true);});@@ -222,7 +246,7 @@ test("accepts equivalent CWE spelling without weakening generic disclosure categ[" CWE-798 ", false],[" cwe-00200 ", true],["CWE-0540", true],- ]) {+ ] as const) {const result = retainedResult(fixture);result.findings[0].taxonomy.cwe = ["CWE-798", cwe];assert.equal(gradeResult(result, fixture).passed, true, cwe);@@ -282,14 +306,13 @@ test("matches path casing only on Windows while preserving citation checks", ()}}} finally {- Object.defineProperty(process, "platform", descriptor);+ Object.defineProperty(process, "platform", descriptor!);}});for (const cwe of ["CWE-256", "CWE-259", "CWE-260"]) {test(`accepts ${cwe} for the password in the database configuration`, () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();const databaseIndex = fixture.positives.findIndex((entry) => entry.id === "dotenv-url",);@@ -302,8 +325,7 @@ for (const cwe of ["CWE-256", "CWE-259", "CWE-260"]) {for (const cwe of ["CWE-312", "CWE-313"]) {test(`accepts ${cwe} for credentials stored in source and configuration files`, () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();for (const finding of result.findings) finding.taxonomy.cwe = [cwe];assert.equal(gradeResult(result, fixture).passed, true);@@ -322,7 +344,7 @@ test("keeps password and cryptographic-key CWEs specific to those exposures", ()["CWE-259", "dotenv-url"],["CWE-260", "dotenv-url"],["CWE-321", "private-key"],- ]) {+ ] as const) {if (expected.id === applicableId) continue;const result = retainedResult(fixture);result.findings[index].taxonomy.cwe = [cwe];@@ -335,8 +357,7 @@ test("keeps password and cryptographic-key CWEs specific to those exposures", ()for (const cwe of ["CWE-200", "CWE-540"]) {test(`accepts ${cwe} for specifically categorized credential exposure`, () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();for (const finding of result.findings) finding.taxonomy.cwe = [cwe];assert.equal(gradeResult(result, fixture).passed, true);@@ -349,8 +370,7 @@ for (const cwe of ["CWE-200", "CWE-540"]) {for (const negativeIndex of [0, 1, 2, 3]) {test(`rejects a finding against negative control ${negativeIndex}`, () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();result.findings.push({taxonomy: { category: "hardcoded-credentials", cwe: ["CWE-798"] },locations: [@@ -360,7 +380,7 @@ for (const negativeIndex of [0, 1, 2, 3]) {role: "root_control",},],- });+ } as TestFinding);const report = gradeResult(result, fixture);assert.equal(report.recall, 1);assert.equal(report.falsePositiveCount, 1);@@ -390,7 +410,7 @@ for (const role of [role,});assert.equal(gradeResult(result, fixture).passed, true);- result.findings[0].locations.at(-1).endLine = 4;+ result.findings[0].locations.at(-1)!.endLine = 4;assert.equal(gradeResult(result, fixture).passed, true);}});@@ -420,10 +440,9 @@ for (const [name, location] of [["invented source file", { path: "src/absent.py", startLine: 1 }],["wrong line in a positive file", { path: "src/retired.py", startLine: 1 }],["negative control", { path: "src/runtime_config.py", startLine: 1 }],-]) {+] as const) {test(`rejects an additional non-supporting location at ${name}`, () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();result.findings[0].locations.push({ ...location, role: "root_control" });const report = gradeResult(result, fixture);assert.equal(report.recall, 1);@@ -434,16 +453,15 @@ for (const [name, location] of [for (const role of ["root_control", "sink"]) {test(`rejects a ${role} citation classified only by a different finding`, () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();const finding = result.findings[0];finding.taxonomy.cwe = ["CWE-522"];const key = fixture.positives.find((expected) => expected.id === "private-key",- );+ )!;const location = {path: key.path,- startLine: role === "sink" ? key.consumerLine : key.line,+ startLine: role === "sink" ? key.consumerLine! : key.line,role: "supporting",};finding.locations.push(location);@@ -469,7 +487,7 @@ test("correct finding locations cannot hide missing or unrelated code evidence",[{ ...sourceEvidence(fixture, "src/client.py", 3), code: " \n" }],]) {const result = retainedResult(fixture);- result.findings[0].codeEvidence = evidence;+ result.findings[0].codeEvidence = evidence as CodeEvidence[];const report = gradeResult(result, fixture);assert.equal(report.recall, 1);assert.equal(report.passed, false);@@ -529,14 +547,15 @@ test("accepts multiline exposure and consumer excerpts with supporting context",for (const [startLine, endLine] of [[1, 3],[4, 7],- ]) {+ ] as const) {const result = retainedResult(fixture);result.findings[0].codeEvidence = [sourceEvidence(fixture, "src/client.py", startLine, endLine),sourceEvidence(fixture, "src/runtime_config.py", 3),];result.findings[0].codeEvidence[0].code =- result.findings[0].codeEvidence[0].code.replaceAll("\n", "\r\n") + "\r\n";+ result.findings[0].codeEvidence[0].code!.replaceAll("\n", "\r\n") ++ "\r\n";assert.equal(gradeResult(result, fixture).passed, true);}});@@ -550,7 +569,7 @@ test("consumer evidence and sinks must cover credential use, not function framin["integration-source", 6],["private-key", 5],["private-key", 7],- ]) {+ ] as const) {const index = fixture.positives.findIndex((expected) => expected.id === id);const expected = fixture.positives[index];const result = retainedResult(fixture);@@ -584,7 +603,7 @@ test("accepts actual credential-use lines and excerpts containing them", () => {for (const [startLine, endLine] of [[expected.consumerLine, expected.consumerLine],[5, expected.lineCount],- ]) {+ ] as const) {const result = retainedResult(fixture);result.findings[index].codeEvidence = [sourceEvidence(fixture, expected.path, startLine, endLine),@@ -601,8 +620,7 @@ test("accepts actual credential-use lines and excerpts containing them", () => {});test("multiple expected exposures can share a finding", () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();const unused = result.findings.splice(1, 1)[0];result.findings[0].locations.push(...unused.locations);assert.deepEqual(gradeResult(result, fixture).errors, [@@ -613,8 +631,7 @@ test("multiple expected exposures can share a finding", () => {});test("grouped exposures retain their applicable CWE classifications", () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();const indices = ["dotenv-url", "private-key"].map((id) =>fixture.positives.findIndex((expected) => expected.id === id),);@@ -643,8 +660,7 @@ test("grouped exposures retain their applicable CWE classifications", () => {for (const role of ["supporting", "expected_control"]) {test(`${role} citations cannot satisfy an independently missed exposure`, () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();const unused = result.findings.splice(1, 1)[0];result.findings[0].locations.push({...unused.locations[0],@@ -657,8 +673,7 @@ for (const role of ["supporting", "expected_control"]) {}test("accepts real credential-use sinks without treating them as separate exposures", () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();for (const [index, expected] of fixture.positives.entries()) {if (expected.consumerLine === null) continue;const sink = {@@ -688,7 +703,7 @@ test("accepts real credential-use sinks without treating them as separate exposufor (const [name, separator] of [["POSIX", "/"],["Windows", "\\"],-]) {+] as const) {test(`accepts ${name} source and credential-use paths with relative prefixes`, () => {const fixture = createFixture();for (const prefix of ["", "./", "././", ".\\", ".\\./", "./.\\"]) {@@ -732,8 +747,7 @@ test("path normalization does not make unrelated or unsafe source locations vali});test("accepts the private-key body and rejects reversed source ranges", () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();const keyIndex = fixture.positives.findIndex((entry) => entry.id === "private-key",);@@ -746,8 +760,7 @@ test("accepts the private-key body and rejects reversed source ranges", () => {for (const footerLocation of [true, false]) {test(`rejects footer-only private-key evidence with ${footerLocation ? "footer" : "declaration"} location`, () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();const keyIndex = fixture.positives.findIndex((entry) => entry.id === "private-key",);@@ -768,8 +781,7 @@ for (const footerLocation of [true, false]) {}test("accepts public-key context alongside credential evidence", () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();result.findings[0].codeEvidence.push(sourceEvidence(fixture, "src/signing.mjs", 2, 4),sourceEvidence(fixture, "config/public.pem", 1, 3),@@ -778,8 +790,7 @@ test("accepts public-key context alongside credential evidence", () => {});test("incomplete coverage cannot pass even with all positive findings", () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();result.coverage.completeness = "partial";assert.equal(gradeResult(result, fixture).passed, false);});@@ -787,7 +798,7 @@ test("incomplete coverage cannot pass even with all positive findings", () => {for (const [name, mutate] of [["deferred work",- (coverage) => {+ (coverage: Required<Coverage>) => {coverage.deferred.push({reason: "source review remains unfinished",paths: ["README.md"],@@ -796,7 +807,7 @@ for (const [name, mutate] of [],["surface needing follow-up",- (coverage) => {+ (coverage: Required<Coverage>) => {coverage.surfaces.push({label: "configuration",disposition: "needs_follow_up",@@ -804,10 +815,9 @@ for (const [name, mutate] of [});},],-]) {+] as const) {test(`complete coverage cannot pass with ${name}`, () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();mutate(result.coverage);const report = gradeResult(result, fixture);assert.equal(report.recall, 1);@@ -817,8 +827,7 @@ for (const [name, mutate] of [}test("complete coverage permits reviewed surfaces and nonblocking questions", () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();result.coverage.surfaces.push({label: "configuration",disposition: "reported",@@ -843,8 +852,7 @@ for (const pattern of ["/",]) {test(`complete coverage cannot exclude fixture path ${pattern}`, () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();result.coverage.explicitExclusions.push({pattern,reason: "Not reviewed",@@ -857,8 +865,7 @@ for (const pattern of [}test("complete coverage permits exclusions outside the generated fixture", () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();result.coverage.explicitExclusions.push({ pattern: "node_modules/**", reason: "No dependencies in fixture" },{ pattern: "https://delivery.example.test/**", reason: "Offline review" },@@ -918,8 +925,7 @@ for (const repo of [}test("POSIX exclusion globs remain case-sensitive", () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();result.coverage.explicitExclusions.push({pattern: "SRC/**/*.PY",reason: "Outside fixture",@@ -928,8 +934,7 @@ test("POSIX exclusion globs remain case-sensitive", () => {});test("Windows drive casing does not hide an in-scope exclusion", () => {- const fixture = createFixture();- const result = retainedResult(fixture);+ const { fixture, result } = retainedFixture();result.coverage.explicitExclusions.push({pattern: "c:/Temp/repository/src/client.py",reason: "Not reviewed",@@ -966,7 +971,7 @@ for (const [repo, prefix, ancestor] of ["//server/share/user[fixture]/repository","//server/share/user[fixture]",],-]) {+] as const) {test(`absolute exclusions treat the repository prefix literally for ${repo}`, () => {const fixture = createFixture();for (const pattern of [@@ -1030,14 +1035,17 @@ test("stages production prompt unchanged and no labels; grades only the final SDfor (const value of prepared.fixture.secretValues)assert.equal(prepared.prompt.includes(value), false);- let settings;- const fakeCodex = {+ let settings: ThreadOptions | undefined;+ const fakeCodex: EvalCodex = {startThread(options) {settings = options;return {async runStreamed(prompt, options) {assert.equal(prompt, prepared.prompt);- assert.ok(options.outputSchema.properties.findings);+ assert.ok(+ (options.outputSchema as { properties: { findings: unknown } })+ .properties.findings,+ );return {events: (async function* () {yield {@@ -1069,9 +1077,9 @@ test("stages production prompt unchanged and no labels; grades only the final SD};const { report } = await runPreparedEval(prepared, fakeCodex);assert.equal(report.recall, 0);- assert.equal(settings.sandboxMode, undefined);- assert.deepEqual(settings.additionalDirectories, [prepared.runtime]);- assert.equal(settings.workingDirectory, prepared.repo);+ assert.equal(settings!.sandboxMode, undefined);+ assert.deepEqual(settings!.additionalDirectories, [prepared.runtime]);+ assert.equal(settings!.workingDirectory, prepared.repo);});test("named read-only profile excludes gold and credentials without a legacy sandbox override", () => {@@ -1085,7 +1093,19 @@ test("named read-only profile excludes gold and credentials without a legacy sanCODEX_SQLITE_HOME: "/tmp/ambient-state",CODEX_CLI_PATH: "/tmp/ambient-codex",});- const config = parse(settings.configOverrides.join("\n"));+ const config = structuredClone(+ parse(settings.configOverrides.join("\n")),+ ) as {+ default_permissions: string;+ features: Record<string, boolean>;+ allow_login_shell: boolean;+ windows: { sandbox: string };+ shell_environment_policy: Record<string, unknown>;+ permissions: Record<+ string,+ { filesystem: unknown; network: Record<string, unknown> }+ >;+ };assert.deepEqual(settings.env, {PATH: "/usr/bin",CODEX_API_KEY: "synthetic-model-auth",@@ -1104,19 +1124,12 @@ test("named read-only profile excludes gold and credentials without a legacy sanassert.equal(config.features.shell_snapshot, false);assert.equal(config.allow_login_shell, false);assert.equal(config.windows.sandbox, "elevated");+ assert.deepEqual(config.shell_environment_policy, {+ inherit: "core",+ ignore_default_excludes: false,+ });assert.deepEqual(- { ...config.shell_environment_policy },- {- inherit: "core",- ignore_default_excludes: false,- },- );- assert.deepEqual(- JSON.parse(- JSON.stringify(- config.permissions[DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID].filesystem,- ),- ),+ config.permissions[DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID].filesystem,{":minimal": "read",":workspace_roots": "read",@@ -1125,7 +1138,7 @@ test("named read-only profile excludes gold and credentials without a legacy san},);assert.deepEqual(- { ...config.permissions[DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID].network },+ config.permissions[DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID].network,{ enabled: false },);assert.equal(diff --git a/evals/secret-discovery/test_runtime.mjs b/evals/secret-discovery/test_runtime.mtssimilarity index 77%rename from evals/secret-discovery/test_runtime.mjsrename to evals/secret-discovery/test_runtime.mtsindex fd203ca15..79b07b7d9 100644--- a/evals/secret-discovery/test_runtime.mjs+++ b/evals/secret-discovery/test_runtime.mts@@ -1,4 +1,7 @@+import { temporaryDirectory } from "../../plugins/codex-security/mcp-app/tests/support/temporary-directories.ts";+import { readJson } from "../../plugins/codex-security/mcp-app/tests/support/json.ts";import assert from "node:assert/strict";+import { once } from "node:events";import childProcess, { spawn } from "node:child_process";import {access,@@ -25,12 +28,11 @@ import {preflightEval,prepareEval,runPreparedEval,-} from "./harness.mjs";+} from "./harness.mts";import {DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID,- createEvalHome,withEvalState,-} from "./runtime.mjs";+} from "./runtime.mts";const unixOnly = { skip: process.platform === "win32", timeout: 15000 };const sdkUrl = new URL(@@ -41,11 +43,15 @@ const tomlUrl = new URL("../../sdk/typescript/node_modules/smol-toml/dist/index.js",import.meta.url,).href;-const harnessUrl = new URL("./harness.mjs", import.meta.url).href;-const runtimeUrl = new URL("./runtime.mjs", import.meta.url).href;+const harnessUrl = new URL("./harness.mts", import.meta.url).href;+const runtimeUrl = new URL("./runtime.mts", import.meta.url).href;-async function readJson(path) {- return JSON.parse(await readFile(path, "utf8"));+type RpcCall = { method: string; params: Record<string, unknown> };+async function readCalls(directory: string): Promise<RpcCall[]> {+ return (await readFile(join(directory, "rpc.jsonl"), "utf8"))+ .trim()+ .split("\n")+ .map((line) => JSON.parse(line));}const createHome = (root = tmpdir()) => mkdtemp(join(root, "eval-home-test-"));@@ -61,41 +67,35 @@ test("saved login refresh survives cleanup without importing config or changingjoin(ambient, "config.toml"),'[mcp_servers.unrelated]\ncommand="unused"\n',);- let home;- await withEvalState(- () => createEvalHome(createHome, ambient),- async (state) => {- home = state.home;- assert.equal(dirname(home), await realpath(ambient));- assert.deepEqual(await readdir(home), ["auth.json"]);- assert.equal(- (await stat(join(home, "auth.json"))).ino,- (await stat(auth)).ino,- );- assert.equal((await stat(auth)).mode, originalMode);- const env = { OPENAI_API_KEY: "synthetic-env-key" };- assert.equal(- codexSettings(home, "/tmp/bin/codex", env).apiKey,- undefined,- );- const explicit = codexSettings(home, "/tmp/bin/codex", {- ...env,- CODEX_API_KEY: "synthetic-explicit-key",- });- assert.equal(explicit.env.CODEX_API_KEY, "synthetic-explicit-key");- // The pinned native file store truncates and writes the existing auth file.- await writeFile(- join(home, "auth.json"),- '{"tokens":{"refresh_token":"synthetic-refreshed"}}',- );- },- );+ let home: string | undefined;+ await withEvalState(createHome, ambient, async (state) => {+ home = state.home;+ assert.equal(dirname(home), await realpath(ambient));+ assert.deepEqual(await readdir(home), ["auth.json"]);+ assert.equal(+ (await stat(join(home, "auth.json"))).ino,+ (await stat(auth)).ino,+ );+ assert.equal((await stat(auth)).mode, originalMode);+ const env = { OPENAI_API_KEY: "synthetic-env-key" };+ assert.equal(codexSettings(home, "/tmp/bin/codex", env).apiKey, undefined);+ const explicit = codexSettings(home, "/tmp/bin/codex", {+ ...env,+ CODEX_API_KEY: "synthetic-explicit-key",+ });+ assert.equal(explicit.env.CODEX_API_KEY, "synthetic-explicit-key");+ // The pinned native file store truncates and writes the existing auth file.+ await writeFile(+ join(home, "auth.json"),+ '{"tokens":{"refresh_token":"synthetic-refreshed"}}',+ );+ });assert.equal((await readJson(auth)).tokens.refresh_token,"synthetic-refreshed",);assert.equal((await stat(auth)).mode, originalMode);- await assert.rejects(access(home), { code: "ENOENT" });+ await assert.rejects(access(home!), { code: "ENOENT" });assert.equal(await readFile(join(ambient, "config.toml"), "utf8"),'[mcp_servers.unrelated]\ncommand="unused"\n',@@ -105,12 +105,9 @@ test("saved login refresh survives cleanup without importing config or changingtest("missing file login creates an empty temporary home", async (t) => {const ambient = await mkdtemp(join(tmpdir(), "eval-login-test-"));t.after(() => rm(ambient, { recursive: true, force: true }));- await withEvalState(- () => createEvalHome(createHome, ambient),- async ({ home }) => {- assert.deepEqual(await readdir(home), []);- },- );+ await withEvalState(createHome, ambient, async ({ home }) => {+ assert.deepEqual(await readdir(home), []);+ });});test(@@ -126,16 +123,13 @@ test(const auth = join(store, "saved.json");await writeFile(auth, '{"OPENAI_API_KEY":"synthetic-file-key"}');await symlink(auth, join(ambient, "auth.json"));- await withEvalState(- () => createEvalHome(createHome, ambient),- async ({ home }) => {- assert.equal(dirname(home), await realpath(store));- assert.equal(- (await stat(join(home, "auth.json"))).ino,- (await stat(auth)).ino,- );- },- );+ await withEvalState(createHome, ambient, async ({ home }) => {+ assert.equal(dirname(home), await realpath(store));+ assert.equal(+ (await stat(join(home, "auth.json"))).ino,+ (await stat(auth)).ino,+ );+ });await access(auth);},);@@ -145,17 +139,14 @@ test("an eval does not overwrite a later replacement login", async (t) => {t.after(() => rm(ambient, { recursive: true, force: true }));const auth = join(ambient, "auth.json");await writeFile(auth, '{"OPENAI_API_KEY":"synthetic-original-key"}');- await withEvalState(- () => createEvalHome(createHome, ambient),- async ({ home }) => {- await rename(auth, join(ambient, "old-auth.json"));- await writeFile(auth, '{"OPENAI_API_KEY":"synthetic-replacement-key"}');- await writeFile(- join(home, "auth.json"),- '{"OPENAI_API_KEY":"synthetic-refreshed-key"}',- );- },- );+ await withEvalState(createHome, ambient, async ({ home }) => {+ await rename(auth, join(ambient, "old-auth.json"));+ await writeFile(auth, '{"OPENAI_API_KEY":"synthetic-replacement-key"}');+ await writeFile(+ join(home, "auth.json"),+ '{"OPENAI_API_KEY":"synthetic-refreshed-key"}',+ );+ });assert.equal((await readJson(auth)).OPENAI_API_KEY,"synthetic-replacement-key",@@ -176,13 +167,13 @@ test("the SDK launches long Windows paths with the native namespace prefix", asyObject.defineProperty(process, "platform", { value: "win32" });settings = codexSettings("C:\\synthetic-home", executable, {});} finally {- Object.defineProperty(process, "platform", platform);+ Object.defineProperty(process, "platform", platform!);}assert.equal(settings.env.CODEX_CLI_PATH, executable);const intercepted = new Error("Synthetic launch intercepted");let launched;- const spawn = t.mock.method(childProcess, "spawn", (file) => {+ const spawn = t.mock.method(childProcess, "spawn", (file: string) => {launched = file;throw intercepted;});@@ -217,12 +208,16 @@ test("the SDK executable override preserves bundled tools at the child boundary"};const original = { ...inherited };const intercepted = new Error("Synthetic launch intercepted");- const environments = [];+ const environments: NodeJS.ProcessEnv[] = [];const spawn = t.mock.method(childProcess,"spawn",- (_file, _args, options) => {- environments.push(options.env);+ (+ _file: string,+ _args: string[],+ options: import("node:child_process").SpawnOptions,+ ) => {+ environments.push(options.env!);throw intercepted;},);@@ -265,13 +260,13 @@ test("the SDK executable override preserves bundled tools at the child boundary"}});-async function waitForFile(path) {+async function waitForFile(path: string) {for (let attempt = 0; attempt < 500; attempt++) {try {await access(path);return;} catch (error) {- if (error.code !== "ENOENT") throw error;+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;}await delay(20);}@@ -279,13 +274,11 @@ async function waitForFile(path) {}async function nativeFixture(- t,+ t: import("node:test").TestContext,mode = "complete",- accountResult = { account: null, requiresOpenaiAuth: true },+ account: { type: string } | null = null,) {- const directory = await realpath(- await mkdtemp(join(tmpdir(), "eval-runtime-test-")),- );+ const directory = await temporaryDirectory("eval-runtime-test-", true);t.after(() => rm(directory, { recursive: true, force: true }));const prepared = await prepareEval(join(directory, "eval"));const home = join(directory, "home[private]");@@ -326,30 +319,16 @@ async function nativeFixture(deferred: [],},};+ // Fixed, offline app-server and exec protocol; only generated source and auth exist here.await writeFile(executable,- fakeNativeSource({ directory, mode, result, accountResult }),- { mode: 0o755 },- );- const settings = codexSettings(home, executable, {- PATH: dirname(process.execPath),- Path: join(directory, "unrelated alias"),- HOME: home,- OPENAI_API_KEY: "synthetic-env-key",- DATABASE_URL: "synthetic-unrelated-secret",- });- return { directory, prepared, settings, executable, result };-}--// Fixed, offline app-server and exec protocol; only generated source and auth exist here.-function fakeNativeSource(scenario) {- return `#!${process.execPath}+ `#!${process.execPath}import { existsSync, writeFileSync, appendFileSync } from "node:fs";import { execFileSync } from "node:child_process";import { join } from "node:path";import { createInterface } from "node:readline";import { parse } from ${JSON.stringify(tomlUrl)};-const scenario = ${JSON.stringify(scenario)};+const scenario = ${JSON.stringify({ directory, mode, result, account })};const args = process.argv.slice(2);const kind = args.includes("app-server") ? "preflight" : "exec";const cwd = args.includes("--cd") ? args[args.indexOf("--cd") + 1] : process.cwd();@@ -364,7 +343,6 @@ const record = (name, value) => writeFileSync(join(scenario.directory, name + ".const bundledTool = execFileSync("rg", ["--version"], { encoding: "utf8" }).trim();record(kind, { args, cwd: process.cwd(), effectiveCwd: cwd, config, env: process.env, pid: process.pid, bundledTool });process.on("SIGTERM", () => {- record(kind + "-stopping", { sourceExists: existsSync(cwd), homeExists: existsSync(process.env.CODEX_HOME) });setTimeout(() => {record(kind + "-stopped", { sourceExists: existsSync(cwd), homeExists: existsSync(process.env.CODEX_HOME) });process.exit(0);@@ -388,15 +366,13 @@ if (kind === "preflight") {data: [{ id: config.default_permissions, allowed: scenario.mode !== "managed-rejection" }], nextCursor: null,};if (request.method === "configRequirements/read") result = { requirements: null };- if (request.method === "account/read") result = scenario.accountResult;+ if (request.method === "account/read") result = { account: scenario.account, requiresOpenaiAuth: true };send({ jsonrpc: "2.0", id: request.id, result });});} else {- let prompt = "";- process.stdin.setEncoding("utf8");- process.stdin.on("data", (chunk) => { prompt += chunk; });+ process.stdin.resume();process.stdin.on("end", () => {- record("ready", { prompt });+ record("ready", {});if (scenario.mode === "block") {writeFileSync(join(process.env.CODEX_HOME, "auth.json"), '{"tokens":{"refresh_token":"synthetic-refreshed"}}');setInterval(() => {}, 1000); return;@@ -413,7 +389,17 @@ if (kind === "preflight") {}});}-`;+`,+ { mode: 0o755 },+ );+ const settings = codexSettings(home, executable, {+ PATH: dirname(process.execPath),+ Path: join(directory, "unrelated alias"),+ HOME: home,+ OPENAI_API_KEY: "synthetic-env-key",+ DATABASE_URL: "synthetic-unrelated-secret",+ });+ return { directory, prepared, settings, executable, result };}test(@@ -482,13 +468,11 @@ test(exec.args[exec.args.indexOf("--add-dir") + 1],prepared.runtime,);- for (const entry of [preflight, exec]) {+ for (const entry of [preflight, exec])assert.equal(entry.bundledTool, "synthetic rg");- assert.equal(entry.env.CODEX_HOME, settings.env.CODEX_HOME);- assert.equal(entry.env.CODEX_SQLITE_HOME, settings.env.CODEX_HOME);- assert.equal(entry.env.DATABASE_URL, undefined);- }- assert.equal(preflight.env.CODEX_API_KEY, undefined);+ assert.equal(exec.env.CODEX_HOME, settings.env.CODEX_HOME);+ assert.equal(exec.env.CODEX_SQLITE_HOME, settings.env.CODEX_HOME);+ assert.equal(exec.env.DATABASE_URL, undefined);assert.equal(exec.env.CODEX_API_KEY, "synthetic-env-key");assert.equal(original.apiKey, undefined);const {@@ -497,10 +481,7 @@ test(...execEnvironment} = exec.env;assert.deepEqual(execEnvironment, preflight.env);- const calls = (await readFile(join(directory, "rpc.jsonl"), "utf8"))- .trim()- .split("\n")- .map(JSON.parse);+ const calls = await readCalls(directory);assert.deepEqual(calls.map((call) => call.method),[@@ -578,64 +559,48 @@ for (const {directory,prepared,settings: original,- } = await nativeFixture(t, "complete", {- account,- requiresOpenaiAuth: true,- });+ } = await nativeFixture(t, "complete", account);const ambient = original.env.CODEX_HOME;if (authFile !== undefined)await writeFile(join(ambient, "auth.json"), authFile);- await withEvalState(- () => createEvalHome(createHome, ambient),- async ({ home }) => {- const initialSettings = codexSettings(- home,- original.codexPathOverride,- {- ...original.env,- CODEX_API_KEY: codexApiKey,- OPENAI_API_KEY: openAiApiKey,- },- );- const settings = await preflightEval(- prepared,- initialSettings,- new AbortController().signal,- );- const { report } = await runPreparedEval(prepared, new Codex(settings));- assert.equal(report.passed, true);- assert.equal(initialSettings.apiKey, undefined);- const preflight = await readJson(join(directory, "preflight.json"));- const exec = await readJson(join(directory, "exec.json"));- assert.equal(preflight.env.CODEX_API_KEY, codexApiKey);- assert.equal(exec.env.CODEX_API_KEY, expectedKey);- assert.equal(preflight.env.CODEX_HOME, home);- assert.equal(exec.env.CODEX_HOME, home);- const calls = (await readFile(join(directory, "rpc.jsonl"), "utf8"))- .trim()- .split("\n")- .map(JSON.parse);- const accountCalls = calls.filter(- (call) => call.method === "account/read",- );+ await withEvalState(createHome, ambient, async ({ home }) => {+ const initialSettings = codexSettings(home, original.codexPathOverride, {+ ...original.env,+ CODEX_API_KEY: codexApiKey,+ OPENAI_API_KEY: openAiApiKey,+ });+ const settings = await preflightEval(+ prepared,+ initialSettings,+ new AbortController().signal,+ );+ const { report } = await runPreparedEval(prepared, new Codex(settings));+ assert.equal(report.passed, true);+ assert.equal(initialSettings.apiKey, undefined);+ const preflight = await readJson(join(directory, "preflight.json"));+ const exec = await readJson(join(directory, "exec.json"));+ assert.equal(preflight.env.CODEX_API_KEY, codexApiKey);+ assert.equal(exec.env.CODEX_API_KEY, expectedKey);+ assert.equal(preflight.env.CODEX_HOME, home);+ assert.equal(exec.env.CODEX_HOME, home);+ const calls = await readCalls(directory);+ const accountCalls = calls.filter(+ (call) => call.method === "account/read",+ );+ assert.equal(+ accountCalls.length,+ openAiApiKey.trim() && !codexApiKey?.trim() ? 1 : 0,+ );+ if (accountCalls.length)+ assert.deepEqual(accountCalls[0].params, { refreshToken: false });+ if (authFile !== undefined) {+ assert.equal(await readFile(join(home, "auth.json"), "utf8"), authFile);assert.equal(- accountCalls.length,- openAiApiKey.trim() && !codexApiKey?.trim() ? 1 : 0,+ await readFile(join(ambient, "auth.json"), "utf8"),+ authFile,);- if (accountCalls.length)- assert.deepEqual(accountCalls[0].params, { refreshToken: false });- if (authFile !== undefined) {- assert.equal(- await readFile(join(home, "auth.json"), "utf8"),- authFile,- );- assert.equal(- await readFile(join(ambient, "auth.json"), "utf8"),- authFile,- );- }- },- );+ }+ });});}@@ -673,7 +638,7 @@ for (const codexApiKey of [undefined, "synthetic-explicit-key"]) {new AbortController().signal,);} finally {- Object.defineProperty(process, "platform", platform);+ Object.defineProperty(process, "platform", platform!);}await runPreparedEval(prepared, new Codex(settings));const exec = await readJson(join(directory, "exec.json"));@@ -682,10 +647,7 @@ for (const codexApiKey of [undefined, "synthetic-explicit-key"]) {codexApiKey ? undefined : "synthetic-fallback-key",);assert.equal(exec.env.CodeX_Api_Key, codexApiKey);- const calls = (await readFile(join(directory, "rpc.jsonl"), "utf8"))- .trim()- .split("\n")- .map(JSON.parse);+ const calls = await readCalls(directory);assert.equal(calls.filter((call) => call.method === "account/read").length,codexApiKey ? 0 : 1,@@ -697,7 +659,7 @@ for (const codexApiKey of [undefined, "synthetic-explicit-key"]) {for (const [mode, expectedError] of [["managed-rejection", /managed Codex policy rejected/],["account-error", /account\/read/],-]) {+] as const) {test(`${mode} prevents starting the SDK exec turn`, unixOnly, async (t) => {const { directory, prepared, settings } = await nativeFixture(t, mode);await assert.rejects(async () => {@@ -738,7 +700,7 @@ for (const mode of ["fallback-item", "fallback-error"]) {for (const [signal, exitCode] of [["SIGINT", 130],["SIGTERM", 143],-]) {+] as const) {test(`${signal} awaits the SDK child before cleanup and preserves refreshed login`,unixOnly,@@ -758,11 +720,11 @@ import { mkdtemp, writeFile } from "node:fs/promises";import { join } from "node:path";import { Codex } from ${JSON.stringify(sdkUrl)};import { codexSettings, preflightEval, prepareEval, runPreparedEval } from ${JSON.stringify(harnessUrl)};-import { createEvalHome, withEvalState } from ${JSON.stringify(runtimeUrl)};-await withEvalState(() => createEvalHome(+import { withEvalState } from ${JSON.stringify(runtimeUrl)};+await withEvalState((base) => mkdtemp(join(base, "signal-home-")),${JSON.stringify(ambient)},-), async ({ root, home, signal }) => {+async ({ root, home, signal }) => {await writeFile(join(${JSON.stringify(directory)}, "state.json"), JSON.stringify({ root, home }));const prepared = await prepareEval(root);const settings = await preflightEval(prepared,@@ -771,14 +733,18 @@ await withEvalState(() => createEvalHome(});`,);- const child = spawn(process.execPath, [driver], {- env: {- PATH: dirname(process.execPath),- HOME: directory,- TMPDIR: directory,+ const child = spawn(+ process.execPath,+ ["--experimental-strip-types", driver],+ {+ env: {+ PATH: dirname(process.execPath),+ HOME: directory,+ TMPDIR: directory,+ },+ stdio: ["ignore", "ignore", "pipe"],},- stdio: ["ignore", "ignore", "pipe"],- });+ );let stderr = "";child.stderr.on("data", (chunk) => {stderr += chunk;@@ -786,10 +752,7 @@ await withEvalState(() => createEvalHome(t.after(() => {if (child.exitCode === null) child.kill("SIGKILL");});- const exited = new Promise((resolve, reject) => {- child.once("error", reject);- child.once("exit", (code, signal) => resolve({ code, signal }));- });+ const exited: Promise<unknown> = once(child, "exit");await Promise.race([waitForFile(join(directory, "ready.json")),exited.then(() => {@@ -797,7 +760,7 @@ await withEvalState(() => createEvalHome(}),]);child.kill(signal);- assert.deepEqual(await exited, { code: exitCode, signal: null }, stderr);+ assert.deepEqual(await exited, [exitCode, null], stderr);const stopped = await readJson(join(directory, "exec-stopped.json"));assert.deepEqual(stopped, { sourceExists: true, homeExists: true });const state = await readJson(join(directory, "state.json"));diff --git a/evals/triage-finding/README.md b/evals/triage-finding/README.mdindex e21c3ab23..bb7b6cc9c 100644--- a/evals/triage-finding/README.md+++ b/evals/triage-finding/README.md@@ -8,14 +8,16 @@ The suite uses the Promptfoo Codex SDK provider because it only needs final assiUse Node.js 22.22.0 or newer for the eval runner. Run these commands from the repository root to install dependencies under this eval directory.-The eval directory has its own `pnpm-workspace.yaml` so pnpm treats it as a small standalone workspace, independent of the SDK and MCP app packages.+The eval directory has its own `pnpm-workspace.yaml` for its Promptfoo dependencies. Tooling type checks use the SDK’s TypeScript dependencies.-Install the local eval runner:+Install the SDK and MCP app dependencies using the repository’s normal setup, then install the local eval runner and check the TypeScript tooling:```bashpnpm --dir evals/triage-finding run setup```+After editing the tooling, rerun `pnpm --dir sdk/typescript run build:evals`. It prepares the SDK and type-checks the eval sources. Node and Promptfoo execute the TypeScript sources directly; package commands enable type stripping for Node 22.13.+Validate the config:```bash@@ -43,13 +45,13 @@ pnpm --dir evals/triage-finding run eval --filter-metadata case_id=sarif-redirecThe eval target is `fixtures/repo`, a small synthetic Express app with both true positive and false positive/review cases. Assertions are deterministic:- `contains-json` validates the fenced `triage-finding/v0` JSON block against `schemas/triage-result-v0.schema.json`.-- `assertions/triage-io.js` checks input order, `input_id`, `source_type`,+- `assertions/triage-io.mts` checks input order, `input_id`, `source_type`,verdicts, array fields, and `$fix-finding` handoff behavior.- `tests/invocation-behavior.yaml` opts out of those default JSON assertions for the no-finding case with `options.disableDefaultAsserts: true`.-- `assertions/missing-input.js` checks that bare invocation asks for a finding,+- `assertions/missing-input.mts` checks that bare invocation asks for a finding,names supported input formats, and does not emit triage result JSON.- `tests/github-rest-intake.yaml` opts out of default JSON assertions for GitHub repository-source routing cases.-- `assertions/github-rest-intake.js` checks GitHub source selection, REST endpoint selection, Codex project repository inference, advisory/private-report handling, connector auth-only behavior, and explicit-only GitHub Issue handling.+- `assertions/github-rest-intake.mts` checks GitHub source selection, REST endpoint selection, Codex project repository inference, advisory/private-report handling, connector auth-only behavior, and explicit-only GitHub Issue handling.## Calibration Dataset@@ -102,7 +104,7 @@ pnpm --dir evals/triage-finding run eval:calibrationThe calibration config is separate from the default synthetic eval:- `promptfooconfig.yaml` covers the small fixture app and GitHub intake routing.-- `promptfooconfig.calibration.yaml` covers the OSS vulnerable/fixed commit pairs and adds `assertions/calibration-evidence.js` to check that required paths or fix commits are cited in the response.+- `promptfooconfig.calibration.yaml` covers the OSS vulnerable/fixed commit pairs and adds `assertions/calibration-evidence.mts` to check that required paths or fix commits are cited in the response.- `promptfooconfig.calibration-smoke.yaml` covers a generated one-row smoke file under `artifacts/calibration-smoke.yaml`.The calibration eval uses the Codex SDK provider, which shells out to `codex exec`. Run it from a normal shell or an unsandboxed command runner so the Codex CLI can write its normal `$CODEX_HOME` state.diff --git a/evals/triage-finding/assertions/calibration-evidence.js b/evals/triage-finding/assertions/calibration-evidence.mtssimilarity index 69%rename from evals/triage-finding/assertions/calibration-evidence.jsrename to evals/triage-finding/assertions/calibration-evidence.mtsindex dc9af63b8..e8ab5f4a0 100644--- a/evals/triage-finding/assertions/calibration-evidence.js+++ b/evals/triage-finding/assertions/calibration-evidence.mts@@ -1,8 +1,7 @@-"use strict";+import type { AssertionContext } from "../types.ts";+import { outputText, parseExpected } from "./output.mts";-const { outputText, parseExpected } = require("./output");--module.exports = (output, context) => {+export default (output: unknown, context: AssertionContext) => {const expectedTerms = parseExpected(context.vars.expected_evidence_terms,true,@@ -16,7 +15,9 @@ module.exports = (output, context) => {}const normalizedOutput = outputText(output).toLowerCase();- const missingTerms = expectedTerms.filter((term) => !normalizedOutput.includes(term.toLowerCase()));+ const missingTerms = expectedTerms.filter(+ (term) => !normalizedOutput.includes(term.toLowerCase()),+ );return {pass: missingTerms.length === 0,diff --git a/evals/triage-finding/assertions/github-rest-intake.js b/evals/triage-finding/assertions/github-rest-intake.jsdeleted file mode 100644index 15cea1b32..000000000--- a/evals/triage-finding/assertions/github-rest-intake.js+++ /dev/null@@ -1,148 +0,0 @@-const { outputText: textFor, hasTriageJson } = require("./output");--function containsAll(text, patterns) {- return patterns.every((pattern) => pattern.test(text));-}--function endpointPattern(path, queryParts = []) {- const escapedPath = path.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");- const queryPatterns = queryParts.map((part) => new RegExp(part, "i"));- return (text) => new RegExp(escapedPath, "i").test(text) && containsAll(text, queryPatterns);-}--function escapedLiteralPattern(value) {- return new RegExp(String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "i");-}--const checks = {- choose_source: (text) => {- const failures = [];- if (!/choose|specify|which|select/i.test(text)) {- failures.push("must ask the user to choose or specify a GitHub source");- }- for (const pattern of [- /code scanning/i,- /Dependabot/i,- /malware/i,- /security advisories|advisories/i,- /private (vulnerability )?reports?|private reports?/i,- /\ball\b/i,- ]) {- if (!pattern.test(text)) {- failures.push(`missing source option matching ${pattern}`);- }- }- if (hasTriageJson(text)) {- failures.push("must not emit triage JSON before a GitHub source is selected");- }- return failures;- },-- project_repo_inference: (text, context) => {- const failures = checks.choose_source(text);- const expectedRepo = String(context.vars.expected_inferred_repo || "");-- if (!/Codex project.*(attached|GitHub)|attached.*Codex project|project.*attached.*GitHub/is.test(text)) {- failures.push("must say the GitHub repository is inferred from the attached Codex project");- }- if (expectedRepo && !escapedLiteralPattern(expectedRepo).test(text)) {- failures.push(`must include inferred GitHub repository ${expectedRepo}`);- }- if (/provide.*(owner\/repo|GitHub repository|repository URL)|ask.*(owner\/repo|GitHub repository|repository URL)/is.test(text)) {- failures.push("must not ask for a GitHub repository when the Codex project attached repo is available");- }- return failures;- },-- dependabot_malware: (text) => {- const hasEndpoint = endpointPattern("/repos/{owner}/{repo}/dependabot/alerts", [- "classification=malware",- "state=open",- "per_page=100",- ])(text);- return [- ...(!hasEndpoint- ? ["must use Dependabot alerts endpoint with classification=malware, state=open, and per_page=100"]- : []),- ...(!/source_type:\s*`?advisory`?|normalize as `?advisory`?/i.test(text)- ? ["must say Dependabot malware normalizes as advisory"]- : []),- ];- },-- code_scanning: (text) => {- const hasAlerts = endpointPattern("/repos/{owner}/{repo}/code-scanning/alerts", [- "state=open",- "per_page=100",- ])(text);- const hasInstances = /code-scanning\/alerts\/\{alert_number\}\/instances/i.test(text);- return [- ...(!hasAlerts ? ["must use code scanning alerts endpoint with state=open and per_page=100"] : []),- ...(!hasInstances ? ["must fetch code scanning alert instances per alert"] : []),- ...(!/source_type:\s*`?sarif`?|normalize as `?sarif`?/i.test(text)- ? ["must say code scanning normalizes as sarif"]- : []),- ];- },-- advisories_private_reports: (text) => {- const hasEndpoint = endpointPattern("/repos/{owner}/{repo}/security-advisories", [- "per_page=100",- ])(text);- const hasEachState = ["triage", "draft", "published", "closed"].every((state) =>- new RegExp(`state=${state}`, "i").test(text),- );- return [- ...(!hasEndpoint ? ["must use repository security advisories endpoint with per_page=100"] : []),- ...(!hasEachState ? ["must include separate triage, draft, published, and closed advisory state requests"] : []),- ...(/state=\{triage\|draft\|published\|closed\}/i.test(text)- ? ["must not combine advisory states in one state={triage|draft|published|closed} request"]- : []),- ...(!/triage.*private vulnerability reports?|private vulnerability reports?.*triage/is.test(text)- ? ["must identify state=triage as private vulnerability reports"]- : []),- ...(!/source_type:\s*`?advisory`?|normalize as `?advisory`?/i.test(text)- ? ["must say advisories/private reports normalize as advisory"]- : []),- ];- },-- connector_rest_only: (text) => {- return [- ...(!/GitHub Connector.*token|connector.*auth token|token.*GitHub Connector/is.test(text)- ? ["must allow GitHub Connector only as an auth token source"]- : []),- ...(!/REST/i.test(text) ? ["must state that finding retrieval uses REST"] : []),- ...(!/do not use.*GitHub Connector.*(fetch|retrieve|data|findings)|GitHub Connector.*not.*(fetch|retrieve|data|findings)/is.test(text)- ? ["must say not to use the GitHub Connector for finding retrieval"]- : []),- ];- },-- explicit_issue: (text) => {- return [- ...(!/GitHub Issues?.*(explicit|specific)|specific.*GitHub Issues?/is.test(text)- ? ["must say GitHub Issues are only used when explicitly/specially provided"]- : []),- ...(!/not.*\ball\b|exclude.*\ball\b|do not include.*\ball\b/is.test(text)- ? ["must say GitHub Issues are not included in all/default source selection"]- : []),- ...(!/source_type:\s*`?freeform`?|normalize as `?freeform`?/i.test(text)- ? ["must say explicit GitHub Issues normalize as freeform"]- : []),- ];- },-};--module.exports = (output, context) => {- const text = textFor(output);- const behavior = String(context.vars.expected_github_rest_behavior || "");- const check = checks[behavior];- const failures = check ? check(text, context) : [`unknown expected_github_rest_behavior: ${behavior}`];-- return {- pass: failures.length === 0,- score: failures.length === 0 ? 1 : 0,- reason: failures.length === 0 ? "GitHub REST intake behavior matched." : failures.join("; "),- };-};diff --git a/evals/triage-finding/assertions/github-rest-intake.mts b/evals/triage-finding/assertions/github-rest-intake.mtsnew file mode 100644index 000000000..1d7c8ccdd--- /dev/null+++ b/evals/triage-finding/assertions/github-rest-intake.mts@@ -0,0 +1,196 @@+import { outputText as textFor, hasTriageJson } from "./output.mts";+import type { AssertionContext } from "../types.ts";+function endpointPattern(path: string, queryParts: string[] = []) {+ const patterns = [+ escapedLiteralPattern(path),+ ...queryParts.map((part) => new RegExp(part, "i")),+ ];+ return (text: string) => patterns.every((pattern) => pattern.test(text));+}++function escapedLiteralPattern(value: unknown) {+ return new RegExp(String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "i");+}++const checks = {+ choose_source: (text: string) => {+ const failures = [];+ if (!/choose|specify|which|select/i.test(text)) {+ failures.push("must ask the user to choose or specify a GitHub source");+ }+ for (const pattern of [+ /code scanning/i,+ /Dependabot/i,+ /malware/i,+ /security advisories|advisories/i,+ /private (vulnerability )?reports?|private reports?/i,+ /\ball\b/i,+ ]) {+ if (!pattern.test(text)) {+ failures.push(`missing source option matching ${pattern}`);+ }+ }+ if (hasTriageJson(text)) {+ failures.push(+ "must not emit triage JSON before a GitHub source is selected",+ );+ }+ return failures;+ },++ project_repo_inference: (text: string, context: AssertionContext) => {+ const failures = checks.choose_source(text);+ const expectedRepo = String(context.vars.expected_inferred_repo || "");++ if (+ !/Codex project.*(attached|GitHub)|attached.*Codex project|project.*attached.*GitHub/is.test(+ text,+ )+ ) {+ failures.push(+ "must say the GitHub repository is inferred from the attached Codex project",+ );+ }+ if (expectedRepo && !escapedLiteralPattern(expectedRepo).test(text)) {+ failures.push(`must include inferred GitHub repository ${expectedRepo}`);+ }+ if (+ /provide.*(owner\/repo|GitHub repository|repository URL)|ask.*(owner\/repo|GitHub repository|repository URL)/is.test(+ text,+ )+ ) {+ failures.push(+ "must not ask for a GitHub repository when the Codex project attached repo is available",+ );+ }+ return failures;+ },++ dependabot_malware: (text: string) => {+ const hasEndpoint = endpointPattern(+ "/repos/{owner}/{repo}/dependabot/alerts",+ ["classification=malware", "state=open", "per_page=100"],+ )(text);+ return [+ ...(!hasEndpoint+ ? [+ "must use Dependabot alerts endpoint with classification=malware, state=open, and per_page=100",+ ]+ : []),+ ...(!/source_type:\s*`?advisory`?|normalize as `?advisory`?/i.test(text)+ ? ["must say Dependabot malware normalizes as advisory"]+ : []),+ ];+ },++ code_scanning: (text: string) => {+ const hasAlerts = endpointPattern(+ "/repos/{owner}/{repo}/code-scanning/alerts",+ ["state=open", "per_page=100"],+ )(text);+ const hasInstances =+ /code-scanning\/alerts\/\{alert_number\}\/instances/i.test(text);+ return [+ ...(!hasAlerts+ ? [+ "must use code scanning alerts endpoint with state=open and per_page=100",+ ]+ : []),+ ...(!hasInstances+ ? ["must fetch code scanning alert instances per alert"]+ : []),+ ...(!/source_type:\s*`?sarif`?|normalize as `?sarif`?/i.test(text)+ ? ["must say code scanning normalizes as sarif"]+ : []),+ ];+ },++ advisories_private_reports: (text: string) => {+ const hasEndpoint = endpointPattern(+ "/repos/{owner}/{repo}/security-advisories",+ ["per_page=100"],+ )(text);+ const hasEachState = ["triage", "draft", "published", "closed"].every(+ (state) => new RegExp(`state=${state}`, "i").test(text),+ );+ return [+ ...(!hasEndpoint+ ? ["must use repository security advisories endpoint with per_page=100"]+ : []),+ ...(!hasEachState+ ? [+ "must include separate triage, draft, published, and closed advisory state requests",+ ]+ : []),+ ...(/state=\{triage\|draft\|published\|closed\}/i.test(text)+ ? [+ "must not combine advisory states in one state={triage|draft|published|closed} request",+ ]+ : []),+ ...(!/triage.*private vulnerability reports?|private vulnerability reports?.*triage/is.test(+ text,+ )+ ? ["must identify state=triage as private vulnerability reports"]+ : []),+ ...(!/source_type:\s*`?advisory`?|normalize as `?advisory`?/i.test(text)+ ? ["must say advisories/private reports normalize as advisory"]+ : []),+ ];+ },++ connector_rest_only: (text: string) => {+ return [+ ...(!/GitHub Connector.*token|connector.*auth token|token.*GitHub Connector/is.test(+ text,+ )+ ? ["must allow GitHub Connector only as an auth token source"]+ : []),+ ...(!/REST/i.test(text)+ ? ["must state that finding retrieval uses REST"]+ : []),+ ...(!/do not use.*GitHub Connector.*(fetch|retrieve|data|findings)|GitHub Connector.*not.*(fetch|retrieve|data|findings)/is.test(+ text,+ )+ ? ["must say not to use the GitHub Connector for finding retrieval"]+ : []),+ ];+ },++ explicit_issue: (text: string) => {+ return [+ ...(!/GitHub Issues?.*(explicit|specific)|specific.*GitHub Issues?/is.test(+ text,+ )+ ? [+ "must say GitHub Issues are only used when explicitly/specially provided",+ ]+ : []),+ ...(!/not.*\ball\b|exclude.*\ball\b|do not include.*\ball\b/is.test(text)+ ? [+ "must say GitHub Issues are not included in all/default source selection",+ ]+ : []),+ ...(!/source_type:\s*`?freeform`?|normalize as `?freeform`?/i.test(text)+ ? ["must say explicit GitHub Issues normalize as freeform"]+ : []),+ ];+ },+};++export default (output: unknown, context: AssertionContext) => {+ const text = textFor(output);+ const behavior = String(context.vars.expected_github_rest_behavior || "");+ const check = checks[behavior as keyof typeof checks];+ const failures = check+ ? check(text, context)+ : [`unknown expected_github_rest_behavior: ${behavior}`];++ return {+ pass: failures.length === 0,+ score: failures.length === 0 ? 1 : 0,+ reason:+ failures.length === 0+ ? "GitHub REST intake behavior matched."+ : failures.join("; "),+ };+};diff --git a/evals/triage-finding/assertions/missing-input.js b/evals/triage-finding/assertions/missing-input.mtssimilarity index 72%rename from evals/triage-finding/assertions/missing-input.jsrename to evals/triage-finding/assertions/missing-input.mtsindex 891e959b8..e45866975 100644--- a/evals/triage-finding/assertions/missing-input.js+++ b/evals/triage-finding/assertions/missing-input.mts@@ -1,14 +1,17 @@-const { outputText } = require("./output");+import { outputText } from "./output.mts";-function matchedFormatNames(text) {- const supportedFormats = [+function matchedFormatNames(text: string) {+ const supportedFormats: [string, RegExp][] = [["SARIF", /\bSARIF\b/i],["CVE", /\bCVE\b/i],["advisory", /\badvisor(?:y|ies)\b/i],["scanner ticket", /\bscanner\b|\bticket\b/i],["bug bounty report", /\bbug bounty\b/i],["Codex Security finding", /\bCodex Security\b|\bfinding artifact\b/i],- ["freeform claim", /\bfreeform\b|\bplain[- ]language\b|\bvulnerability claim\b/i],+ [+ "freeform claim",+ /\bfreeform\b|\bplain[- ]language\b|\bvulnerability claim\b/i,+ ],];return supportedFormats@@ -16,7 +19,7 @@ function matchedFormatNames(text) {.map(([name]) => name);}-module.exports = (output) => {+export default (output: unknown) => {const text = outputText(output);const failures = [];const promptForInputPattern =@@ -25,7 +28,9 @@ module.exports = (output) => {/\b(finding|input|SARIF|CVE|advisory|scanner|ticket|bug bounty|report|artifact|claim)\b[\s\S]{0,120}\b(provide|paste|share|send|supply|include)\b/i;if (!promptForInputPattern.test(text) && !inputBeforeVerbPattern.test(text)) {- failures.push("response must ask the user to provide, paste, share, send, or include a finding/input");+ failures.push(+ "response must ask the user to provide, paste, share, send, or include a finding/input",+ );}const formats = matchedFormatNames(text);@@ -36,7 +41,9 @@ module.exports = (output) => {}if (/```(?:json)?\s*[\s\S]*?```/i.test(text)) {- failures.push("response must not return a fenced JSON block when no finding was supplied");+ failures.push(+ "response must not return a fenced JSON block when no finding was supplied",+ );}for (const pattern of [@@ -45,7 +52,9 @@ module.exports = (output) => {/["']verdict["']\s*:/i,]) {if (pattern.test(text)) {- failures.push("response must not emit a triage result JSON object when no finding was supplied");+ failures.push(+ "response must not emit a triage result JSON object when no finding was supplied",+ );break;}}diff --git a/evals/triage-finding/assertions/output.js b/evals/triage-finding/assertions/output.jsdeleted file mode 100644index 96bdd386d..000000000--- a/evals/triage-finding/assertions/output.js+++ /dev/null@@ -1,22 +0,0 @@-function outputText(output) {- return typeof output === "string" ? output : JSON.stringify(output);-}--function hasTriageJson(text) {- return (- /```(?:json)?\s*[\s\S]*?```/i.test(text) ||- /schema_version\s*["']?\s*:\s*["']?triage-finding\/v0/i.test(text) ||- /["']findings["']\s*:/i.test(text) ||- /["']verdict["']\s*:/i.test(text)- );-}--function parseExpected(value, trim = typeof value === "string") {- if (typeof value === "string") {- value = value.split(",");- }- const terms = Array.isArray(value) ? value.map(String) : [];- return trim ? terms.flatMap((term) => term.trim() || []) : terms;-}--module.exports = { outputText, hasTriageJson, parseExpected };diff --git a/evals/triage-finding/assertions/output.mts b/evals/triage-finding/assertions/output.mtsnew file mode 100644index 000000000..0f2959295--- /dev/null+++ b/evals/triage-finding/assertions/output.mts@@ -0,0 +1,50 @@+import type { TriageResult } from "../types.ts";++export function outputText(output: unknown) {+ return typeof output === "string" ? output : JSON.stringify(output);+}++export function hasTriageJson(text: string) {+ return (+ /```(?:json)?\s*[\s\S]*?```/i.test(text) ||+ /schema_version\s*["']?\s*:\s*["']?triage-finding\/v0/i.test(text) ||+ /["']findings["']\s*:/i.test(text) ||+ /["']verdict["']\s*:/i.test(text)+ );+}++export function parseExpected(+ value: unknown,+ trim = typeof value === "string",+) {+ if (typeof value === "string") {+ value = value.split(",");+ }+ const terms = Array.isArray(value) ? value.map(String) : [];+ return trim ? terms.flatMap((term) => term.trim() || []) : terms;+}++export function extractTriageResult(+ output: unknown,+ failureMessage = "Could not find a parseable triage-finding/v0 JSON result",+) {+ const text = outputText(output);+ const fencedBlocks = [...text.matchAll(/```(?:json)?\s*([\s\S]*?)```/gi)].map(+ (match) => match[1].trim(),+ );+ const start = text.indexOf("{");+ const end = text.lastIndexOf("}");+ const candidates =+ fencedBlocks.length > 0 ? fencedBlocks : [text.slice(start, end + 1)];+ for (const candidate of candidates) {+ try {+ const parsed = JSON.parse(candidate) as TriageResult;+ if (parsed && parsed.schema_version === "triage-finding/v0") {+ return parsed;+ }+ } catch {+ // The response may contain more than one fenced block. Try the next one.+ }+ }+ throw new Error(failureMessage);+}diff --git a/evals/triage-finding/assertions/ticket-intake.js b/evals/triage-finding/assertions/ticket-intake.jsdeleted file mode 100644index 7a85e8471..000000000--- a/evals/triage-finding/assertions/ticket-intake.js+++ /dev/null@@ -1,41 +0,0 @@-const { outputText, hasTriageJson } = require("./output");--const expectedPatterns = {- unavailable: [/connector|Linear/i, /connect|authenticate|reauthorize/i, /paste|provide.*content/i],- permission: [/permission|access/i, /request.*access|ask.*access/i, /paste|provide.*content/i],- not_found: [/not found|inaccessible|identifier/i, /verify|check/i, /paste|provide.*content/i],- transient: [/retr(?:y|ied)|try again/i, /once|one time|second attempt/i, /paste|provide.*content/i],-};--const expectedSubissuePatterns = {- direct_confirmation: [/SEC-294/i, /SEC-295/i, /2\s+(?:direct\s+)?sub-issues|two\s+(?:direct\s+)?sub-issues/i, /include|import/i, /ask|would you|do you want/i],- next_depth: [/SEC-296/i, /next (?:level|depth)|deeper|grandchild/i, /include|import/i, /ask|would you|do you want/i],- ambiguous_parent: [/parent/i, /independent|standalone|separate/i, /include|triage/i, /ask|would you|do you want/i],- over_limit: [/250/i, /narrow|smaller|filter|depth|status|label/i, /not.*truncate|cannot.*truncate|stop/i],-};--module.exports = (output, context) => {- const text = outputText(output);- const behavior = String(context.vars.expected_ticket_failure || "");- const subissueBehavior = String(context.vars.expected_linear_subissues || "");- const patterns = expectedPatterns[behavior] || [];- const subissuePatterns = expectedSubissuePatterns[subissueBehavior] || [];- const failures = [];-- for (const pattern of patterns) {- if (!pattern.test(text)) failures.push(`missing recovery detail matching ${pattern}`);- }- for (const pattern of subissuePatterns) {- if (!pattern.test(text)) failures.push(`missing Linear sub-issue detail matching ${pattern}`);- }- if (hasTriageJson(text)) failures.push("must not emit triage JSON when ticket retrieval failed");- if (/inspected the repository|repository analysis (?:is )?complete|verdict:\s*(?:confirmed|needs_review|not_actionable)/i.test(text)) {- failures.push("must stop before repository analysis or verdicting");- }-- return {- pass: failures.length === 0,- score: failures.length === 0 ? 1 : 0,- reason: failures.length === 0 ? "Ticket connector failure produced recovery guidance without triage output." : failures.join("; "),- };-};diff --git a/evals/triage-finding/assertions/ticket-intake.mts b/evals/triage-finding/assertions/ticket-intake.mtsnew file mode 100644index 000000000..d47bc574f--- /dev/null+++ b/evals/triage-finding/assertions/ticket-intake.mts@@ -0,0 +1,87 @@+import { outputText, hasTriageJson } from "./output.mts";+import type { AssertionContext } from "../types.ts";+const expectedPatterns: Record<string, RegExp[]> = {+ unavailable: [+ /connector|Linear/i,+ /connect|authenticate|reauthorize/i,+ /paste|provide.*content/i,+ ],+ permission: [+ /permission|access/i,+ /request.*access|ask.*access/i,+ /paste|provide.*content/i,+ ],+ not_found: [+ /not found|inaccessible|identifier/i,+ /verify|check/i,+ /paste|provide.*content/i,+ ],+ transient: [+ /retr(?:y|ied)|try again/i,+ /once|one time|second attempt/i,+ /paste|provide.*content/i,+ ],+};++const expectedSubissuePatterns: Record<string, RegExp[]> = {+ direct_confirmation: [+ /SEC-294/i,+ /SEC-295/i,+ /2\s+(?:direct\s+)?sub-issues|two\s+(?:direct\s+)?sub-issues/i,+ /include|import/i,+ /ask|would you|do you want/i,+ ],+ next_depth: [+ /SEC-296/i,+ /next (?:level|depth)|deeper|grandchild/i,+ /include|import/i,+ /ask|would you|do you want/i,+ ],+ ambiguous_parent: [+ /parent/i,+ /independent|standalone|separate/i,+ /include|triage/i,+ /ask|would you|do you want/i,+ ],+ over_limit: [+ /250/i,+ /narrow|smaller|filter|depth|status|label/i,+ /not.*truncate|cannot.*truncate|stop/i,+ ],+};++export default (output: unknown, context: AssertionContext) => {+ const text = outputText(output);+ const behavior = String(context.vars.expected_ticket_failure || "");+ const subissueBehavior = String(context.vars.expected_linear_subissues || "");+ const patterns = expectedPatterns[behavior] || [];+ const subissuePatterns = expectedSubissuePatterns[subissueBehavior] || [];+ const failures = [];++ for (const pattern of patterns) {+ if (!pattern.test(text))+ failures.push(`missing recovery detail matching ${pattern}`);+ }+ for (const pattern of subissuePatterns) {+ if (!pattern.test(text))+ failures.push(`missing Linear sub-issue detail matching ${pattern}`);+ }+ if (hasTriageJson(text))+ failures.push("must not emit triage JSON when ticket retrieval failed");+ if (+ /inspected the repository|repository analysis (?:is )?complete|verdict:\s*(?:confirmed|needs_review|not_actionable)/i.test(+ text,+ )+ ) {+ failures.push("must stop before repository analysis or verdicting");+ }++ return {+ pass: failures.length === 0,+ score: failures.length === 0 ? 1 : 0,+ reason:+ failures.length === 0+ ? "Ticket connector failure produced recovery guidance without triage output."+ : failures.join("; "),+ };+};diff --git a/evals/triage-finding/assertions/triage-io.js b/evals/triage-finding/assertions/triage-io.mtssimilarity index 61%rename from evals/triage-finding/assertions/triage-io.jsrename to evals/triage-finding/assertions/triage-io.mtsindex 797ccfd59..41170d237 100644--- a/evals/triage-finding/assertions/triage-io.js+++ b/evals/triage-finding/assertions/triage-io.mts@@ -1,7 +1,7 @@-const { parseExpected } = require("./output");-const { extractTriageResult } = require("../sastbench/scripts/sastbench-result");+import type { AssertionContext } from "../types.ts";+import { extractTriageResult, parseExpected } from "./output.mts";-function expectedScanbenchMapping(context) {+function expectedScanbenchMapping(context: AssertionContext) {const caseId = String(context.vars.case_id || "");const label = String(context.vars.expected_binary_label || "");const mappings = [];@@ -37,7 +37,7 @@ function expectedScanbenchMapping(context) {return mappings;}-module.exports = (output, context) => {+export default (output: unknown, context: AssertionContext) => {const result = extractTriageResult(output,"Could not find a parseable triage-finding/v0 JSON block.",@@ -48,7 +48,10 @@ module.exports = (output, context) => {const failures = [];for (const mapping of expectedScanbenchMapping(context)) {- if (expectedVerdicts.length === 1 && expectedVerdicts[0] !== mapping.expectedVerdict) {+ if (+ expectedVerdicts.length === 1 &&+ expectedVerdicts[0] !== mapping.expectedVerdict+ ) {failures.push(`${mapping.label}: expected_verdicts must be ${mapping.expectedVerdict}, got ${expectedVerdicts[0]}`,);@@ -66,39 +69,60 @@ module.exports = (output, context) => {if (!Array.isArray(result.findings)) {failures.push("findings is not an array");} else if (result.findings.length !== expectedIds.length) {- failures.push(`expected ${expectedIds.length} findings, got ${result.findings.length}`);+ failures.push(+ `expected ${expectedIds.length} findings, got ${result.findings.length}`,+ );}const findings = Array.isArray(result.findings) ? result.findings : [];- const ranksByQueue = new Map();+ const ranksByQueue = new Map<+ string | null | undefined,+ { label: string; rank: number }[]+ >();findings.forEach((finding, index) => {const label = expectedIds[index] || `index ${index}`;if (finding.input_id !== expectedIds[index]) {- failures.push(`${label}: expected input_id ${expectedIds[index]}, got ${finding.input_id}`);+ failures.push(+ `${label}: expected input_id ${expectedIds[index]}, got ${finding.input_id}`,+ );}if (finding.source_type !== expectedSourceTypes[index]) {- failures.push(`${label}: expected source_type ${expectedSourceTypes[index]}, got ${finding.source_type}`);+ failures.push(+ `${label}: expected source_type ${expectedSourceTypes[index]}, got ${finding.source_type}`,+ );}if (finding.verdict !== expectedVerdicts[index]) {- failures.push(`${label}: expected verdict ${expectedVerdicts[index]}, got ${finding.verdict}`);+ failures.push(+ `${label}: expected verdict ${expectedVerdicts[index]}, got ${finding.verdict}`,+ );}const rank = finding.exploitability_stack_rank?.rank;const rankQueue = finding.exploitability_stack_rank?.rank_queue;if (finding.verdict === "not_actionable") {if (rank !== null || rankQueue !== null) {- failures.push(`${label}: not_actionable finding must use null rank and rank_queue`);+ failures.push(+ `${label}: not_actionable finding must use null rank and rank_queue`,+ );}- } else if (!Number.isInteger(rank) || rank < 1) {- failures.push(`${label}: actionable or unresolved finding must use a positive integer rank`);+ } else if (!Number.isInteger(rank) || rank! < 1) {+ failures.push(+ `${label}: actionable or unresolved finding must use a positive integer rank`,+ );} else {const ranks = ranksByQueue.get(rankQueue) || [];- ranks.push({ label, rank });+ ranks.push({ label, rank: rank! });ranksByQueue.set(rankQueue, ranks);}- for (const field of ["affected_locations", "reachable_path", "evidence", "counterevidence", "proof_gaps"]) {+ for (const field of [+ "affected_locations",+ "reachable_path",+ "evidence",+ "counterevidence",+ "proof_gaps",+ ]) {if (!Array.isArray(finding[field])) {failures.push(`${label}: ${field} is not an array`);}@@ -107,10 +131,17 @@ module.exports = (output, context) => {const handoff = finding.fix_finding_handoff;if (finding.verdict === "confirmed") {if (typeof handoff !== "string" || handoff.trim().length === 0) {- failures.push(`${label}: confirmed finding must include a non-empty fix_finding_handoff`);+ failures.push(+ `${label}: confirmed finding must include a non-empty fix_finding_handoff`,+ );}- } else if (handoff !== null && !(typeof handoff === "string" && handoff.trim().length === 0)) {- failures.push(`${label}: non-confirmed finding must not include a fix_finding_handoff`);+ } else if (+ handoff !== null &&+ !(typeof handoff === "string" && handoff.trim().length === 0)+ ) {+ failures.push(+ `${label}: non-confirmed finding must not include a fix_finding_handoff`,+ );}});@@ -119,7 +150,9 @@ module.exports = (output, context) => {ranks.forEach(({ label, rank }, index) => {const expectedRank = index + 1;if (rank !== expectedRank) {- failures.push(`${label}: ${rankQueue} ranks must be contiguous from 1; expected ${expectedRank}, got ${rank}`);+ failures.push(+ `${label}: ${rankQueue} ranks must be contiguous from 1; expected ${expectedRank}, got ${rank}`,+ );}});}@@ -127,6 +160,9 @@ module.exports = (output, context) => {return {pass: failures.length === 0,score: failures.length === 0 ? 1 : 0,- reason: failures.length === 0 ? "Triage I/O matched expected source types, order, and verdicts." : failures.join("; "),+ reason:+ failures.length === 0+ ? "Triage I/O matched expected source types, order, and verdicts."+ : failures.join("; "),};};diff --git a/evals/triage-finding/datasets/README.md b/evals/triage-finding/datasets/README.mdindex ed524d609..bfb354ea8 100644--- a/evals/triage-finding/datasets/README.md+++ b/evals/triage-finding/datasets/README.md@@ -16,9 +16,9 @@ The important distinction is that GitHub advisory state is source material, not## Files- `triage-calibration-seed.json` is the first OSS-only seed dataset. It contains public vulnerable/fixed commit pairs.-- `../scripts/test-calibration-dataset.js` performs deterministic structural validation so future additions do not silently break the dataset shape.-- `../scripts/generate-calibration-tests.js` converts the dataset into Promptfoo tests at `../tests/calibration-oss.yaml`.-- `../scripts/hydrate-calibration-repos.js` downloads the pinned OSS checkouts under `../artifacts/calibration-repos/` for local eval runs.+- `../scripts/test-calibration-dataset.mts` performs deterministic structural validation so future additions do not silently break the dataset shape.+- `../scripts/generate-calibration-tests.mts` converts the dataset into Promptfoo tests at `../tests/calibration-oss.yaml`.+- `../scripts/hydrate-calibration-repos.mts` downloads the pinned OSS checkouts under `../artifacts/calibration-repos/` for local eval runs.## Case Familiesdiff --git a/evals/triage-finding/package.json b/evals/triage-finding/package.jsonindex 23ce1b71a..43a88d386 100644--- a/evals/triage-finding/package.json+++ b/evals/triage-finding/package.json@@ -3,30 +3,30 @@"private": true,"description": "Promptfoo evals for the Codex Security triage-finding skill","scripts": {- "setup": "pnpm install --frozen-lockfile && pnpm rebuild better-sqlite3",- "calibration:generate": "node scripts/generate-calibration-tests.js",- "calibration:hydrate": "node scripts/hydrate-calibration-repos.js",- "sastbench:install": "node sastbench/scripts/install-sastbench.js",- "sastbench:hydrate": "node sastbench/scripts/hydrate-sastbench-repos.js",+ "setup": "pnpm install --frozen-lockfile && pnpm rebuild better-sqlite3 && pnpm --dir ../../sdk/typescript run build:evals",+ "calibration:generate": "node --experimental-strip-types scripts/generate-calibration-tests.mts",+ "calibration:hydrate": "node --experimental-strip-types scripts/hydrate-calibration-repos.mts",+ "sastbench:install": "node --experimental-strip-types sastbench/scripts/install-sastbench.mts",+ "sastbench:hydrate": "node --experimental-strip-types sastbench/scripts/hydrate-sastbench-repos.mts","sastbench:prepare": "pnpm run sastbench:install && pnpm run sastbench:hydrate","test:calibration": "pnpm run test:dataset && pnpm run test:calibration-generate && pnpm run test:calibration-evidence && pnpm run test:calibration-hydrate",- "test:calibration-evidence": "node scripts/test-calibration-evidence.js",- "test:calibration-generate": "node scripts/test-generate-calibration-tests.js",- "test:calibration-hydrate": "node scripts/test-hydrate-calibration-repos.js",- "test:dataset": "node scripts/test-calibration-dataset.js",- "test:deterministic": "node scripts/test-app-surface-instructions.js && node scripts/test-triage-io.js && node scripts/test-calibration-dataset.js && node scripts/test-generate-calibration-tests.js && node scripts/test-calibration-evidence.js && node scripts/test-hydrate-calibration-repos.js && node sastbench/scripts/test-sastbench-lib.js && node sastbench/scripts/test-install-sastbench.js && node sastbench/scripts/test-hydrate-sastbench-repos.js && node sastbench/scripts/test-sastbench-result.js && node sastbench/scripts/test-promptfoo-native-harness.js",- "test:instructions": "node scripts/test-app-surface-instructions.js",- "test:sastbench": "node sastbench/scripts/test-sastbench-lib.js && node sastbench/scripts/test-install-sastbench.js && node sastbench/scripts/test-hydrate-sastbench-repos.js && node sastbench/scripts/test-sastbench-result.js && node sastbench/scripts/test-promptfoo-native-harness.js",+ "test:calibration-evidence": "node --experimental-strip-types scripts/test-calibration-evidence.mts",+ "test:calibration-generate": "node --experimental-strip-types scripts/test-generate-calibration-tests.mts",+ "test:calibration-hydrate": "node --experimental-strip-types scripts/test-hydrate-calibration-repos.mts",+ "test:dataset": "node --experimental-strip-types scripts/test-calibration-dataset.mts",+ "test:deterministic": "node --experimental-strip-types scripts/test-app-surface-instructions.mts && node --experimental-strip-types scripts/test-triage-io.mts && node --experimental-strip-types scripts/test-calibration-dataset.mts && node --experimental-strip-types scripts/test-generate-calibration-tests.mts && node --experimental-strip-types scripts/test-calibration-evidence.mts && node --experimental-strip-types scripts/test-hydrate-calibration-repos.mts && node --experimental-strip-types sastbench/scripts/test-sastbench-lib.mts && node --experimental-strip-types sastbench/scripts/test-install-sastbench.mts && node --experimental-strip-types sastbench/scripts/test-hydrate-sastbench-repos.mts && node --experimental-strip-types sastbench/scripts/test-sastbench-result.mts && node --experimental-strip-types sastbench/scripts/test-promptfoo-native-harness.mts",+ "test:instructions": "node --experimental-strip-types scripts/test-app-surface-instructions.mts",+ "test:sastbench": "node --experimental-strip-types sastbench/scripts/test-sastbench-lib.mts && node --experimental-strip-types sastbench/scripts/test-install-sastbench.mts && node --experimental-strip-types sastbench/scripts/test-hydrate-sastbench-repos.mts && node --experimental-strip-types sastbench/scripts/test-sastbench-result.mts && node --experimental-strip-types sastbench/scripts/test-promptfoo-native-harness.mts","validate": "PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true promptfoo validate config -c ./promptfooconfig.yaml","validate:calibration": "pnpm run calibration:generate && PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true promptfoo validate config -c ./promptfooconfig.calibration.yaml",- "validate:sastbench": "PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true node sastbench/scripts/run-sastbench-promptfoo.js validate config -c ./sastbench/promptfooconfig.sastbench.yaml",- "validate:sastbench:sample": "PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true node sastbench/scripts/run-sastbench-promptfoo.js validate config -c ./sastbench/promptfooconfig.sastbench-sample.yaml",+ "validate:sastbench": "PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true node --experimental-strip-types sastbench/scripts/run-sastbench-promptfoo.mts validate config -c ./sastbench/promptfooconfig.sastbench.yaml",+ "validate:sastbench:sample": "PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true node --experimental-strip-types sastbench/scripts/run-sastbench-promptfoo.mts validate config -c ./sastbench/promptfooconfig.sastbench-sample.yaml","eval": "mkdir -p artifacts && PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true promptfoo eval -c ./promptfooconfig.yaml -o ./artifacts/triage-finding-eval.json --no-cache --no-share","eval:smoke": "mkdir -p artifacts && PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true promptfoo eval -c ./promptfooconfig.yaml -o ./artifacts/triage-finding-eval-smoke.json --no-cache --no-share --filter-range 0:1","eval:calibration": "pnpm run calibration:generate && pnpm run calibration:hydrate && mkdir -p artifacts && PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true promptfoo eval -c ./promptfooconfig.calibration.yaml -o ./artifacts/triage-finding-calibration-eval.json --no-cache --no-share",- "eval:calibration:smoke": "node scripts/generate-calibration-tests.js --case oss-dompurify-ghsa-v8jm-5vwx-cfxm --variant vulnerable --output artifacts/calibration-smoke.yaml && node scripts/hydrate-calibration-repos.js --case oss-dompurify-ghsa-v8jm-5vwx-cfxm --variant vulnerable && mkdir -p artifacts && PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true promptfoo eval -c ./promptfooconfig.calibration-smoke.yaml -o ./artifacts/triage-finding-calibration-smoke.json --no-cache --no-share",- "eval:sastbench": "PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true node sastbench/scripts/run-sastbench-promptfoo.js eval -c ./sastbench/promptfooconfig.sastbench.yaml --no-cache --no-share",- "eval:sastbench:sample": "PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true node sastbench/scripts/run-sastbench-promptfoo.js eval -c ./sastbench/promptfooconfig.sastbench-sample.yaml --no-cache --no-share --max-concurrency 32",+ "eval:calibration:smoke": "node --experimental-strip-types scripts/generate-calibration-tests.mts --case oss-dompurify-ghsa-v8jm-5vwx-cfxm --variant vulnerable --output artifacts/calibration-smoke.yaml && node --experimental-strip-types scripts/hydrate-calibration-repos.mts --case oss-dompurify-ghsa-v8jm-5vwx-cfxm --variant vulnerable && mkdir -p artifacts && PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true promptfoo eval -c ./promptfooconfig.calibration-smoke.yaml -o ./artifacts/triage-finding-calibration-smoke.json --no-cache --no-share",+ "eval:sastbench": "PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true node --experimental-strip-types sastbench/scripts/run-sastbench-promptfoo.mts eval -c ./sastbench/promptfooconfig.sastbench.yaml --no-cache --no-share",+ "eval:sastbench:sample": "PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true node --experimental-strip-types sastbench/scripts/run-sastbench-promptfoo.mts eval -c ./sastbench/promptfooconfig.sastbench-sample.yaml --no-cache --no-share --max-concurrency 32","pf:view": "PROMPTFOO_CONFIG_DIR=.promptfoo PROMPTFOO_DISABLE_WAL_MODE=true promptfoo view"},"dependencies": {diff --git a/evals/triage-finding/promptfooconfig.calibration-smoke.yaml b/evals/triage-finding/promptfooconfig.calibration-smoke.yamlindex 7259be208..d02c6afa2 100644--- a/evals/triage-finding/promptfooconfig.calibration-smoke.yaml+++ b/evals/triage-finding/promptfooconfig.calibration-smoke.yaml@@ -28,8 +28,8 @@ defaultTest:- type: contains-jsonvalue: file://./schemas/triage-result-v0.schema.json- type: javascript- value: file://./assertions/triage-io.js+ value: file://./assertions/triage-io.mts- type: javascript- value: file://./assertions/calibration-evidence.js+ value: file://./assertions/calibration-evidence.mtstests: file://./artifacts/calibration-smoke.yamldiff --git a/evals/triage-finding/promptfooconfig.calibration.yaml b/evals/triage-finding/promptfooconfig.calibration.yamlindex 3ebd971f3..acf6597d8 100644--- a/evals/triage-finding/promptfooconfig.calibration.yaml+++ b/evals/triage-finding/promptfooconfig.calibration.yaml@@ -28,8 +28,8 @@ defaultTest:- type: contains-jsonvalue: file://./schemas/triage-result-v0.schema.json- type: javascript- value: file://./assertions/triage-io.js+ value: file://./assertions/triage-io.mts- type: javascript- value: file://./assertions/calibration-evidence.js+ value: file://./assertions/calibration-evidence.mtstests: file://./tests/calibration-oss.yamldiff --git a/evals/triage-finding/promptfooconfig.yaml b/evals/triage-finding/promptfooconfig.yamlindex 088478f43..262a310eb 100644--- a/evals/triage-finding/promptfooconfig.yaml+++ b/evals/triage-finding/promptfooconfig.yaml@@ -41,7 +41,7 @@ defaultTest:- type: contains-jsonvalue: file://./schemas/triage-result-v0.schema.json- type: javascript- value: file://./assertions/triage-io.js+ value: file://./assertions/triage-io.mtstests:- file://./tests/input-types.yamldiff --git a/evals/triage-finding/sastbench/assertions/sastbench-metrics.js b/evals/triage-finding/sastbench/assertions/sastbench-metrics.jsdeleted file mode 100644index 31d414f50..000000000--- a/evals/triage-finding/sastbench/assertions/sastbench-metrics.js+++ /dev/null@@ -1,53 +0,0 @@-"use strict";--const {- namedScoresForOutcome,- normalizePromptfooResult,-} = require("../scripts/sastbench-result");--/**- * Add benchmark counters after every Promptfoo row, including provider errors- * for which normal assertions do not run. Sample rows scale the counters by- * their population weight so the shared derived metrics estimate the full- * benchmark without a second scoring implementation.- */-function afterEach(context) {- const row = {- ...context.result,- vars: context.result.vars || context.test.vars,- testCase: context.result.testCase || context.test,- };- const outcome = normalizePromptfooResult(row);- const rawSampleWeight = context.test.metadata?.sample_weight;- const sampleWeight = rawSampleWeight === undefined ? 1 : Number(rawSampleWeight);- if (!Number.isFinite(sampleWeight) || sampleWeight <= 0) {- throw new Error(`SastBench sample_weight must be a positive number, got ${rawSampleWeight}`);- }- const weightedScores = Object.fromEntries(- Object.entries(namedScoresForOutcome(outcome)).map(([name, value]) => [- name,- value * sampleWeight,- ]),- );- return {- test: context.test,- result: {- ...context.result,- namedScores: {- ...context.result.namedScores,- ...weightedScores,- },- metadata: {- ...context.result.metadata,- sastbench: {- status: outcome.status,- verdict: outcome.verdict,- error: outcome.error,- sampleWeight,- },- },- },- };-}--module.exports = { afterEach };diff --git a/evals/triage-finding/sastbench/assertions/sastbench-metrics.mts b/evals/triage-finding/sastbench/assertions/sastbench-metrics.mtsnew file mode 100644index 000000000..dab7ec7ac--- /dev/null+++ b/evals/triage-finding/sastbench/assertions/sastbench-metrics.mts@@ -0,0 +1,103 @@+import type { PromptfooRow, PromptfooTest } from "../../types.ts";++import { parseCaseOutcome } from "../scripts/sastbench-result.mts";++const VALID_LABELS = new Set(["true_positive", "false_positive"]);++/**+ * Add benchmark counters after every Promptfoo row, including provider errors+ * for which normal assertions do not run. Sample rows scale the counters by+ * their population weight so the shared derived metrics estimate the full+ * benchmark without a second scoring implementation. Promptfoo sums these+ * named scores before evaluating the YAML derived metrics.+ */+export function afterEach(context: {+ result: PromptfooRow;+ test: PromptfooTest;+}) {+ const row = context.result;+ const vars = row.vars || context.test.vars;+ const caseId = String(vars?.case_id || "");+ const expectedGroundTruth = String(context.test.metadata?.ground_truth || "");+ const output = row.response?.output;+ let status = "model_error";+ let verdict: string | null = null;+ let error: string | null = null;+ if (typeof output !== "string" || output.trim().length === 0) {+ error = String(+ row.response?.error ||+ row.error ||+ row.failureReason ||+ "Model returned no output",+ );+ } else {+ try {+ verdict = parseCaseOutcome(output, caseId).verdict;+ status = "ok";+ } catch (cause) {+ status = "invalid_output";+ error = cause instanceof Error ? cause.message : String(cause);+ }+ }+ const rawSampleWeight = context.test.metadata?.sample_weight;+ const sampleWeight =+ rawSampleWeight === undefined ? 1 : Number(rawSampleWeight);+ if (!Number.isFinite(sampleWeight) || sampleWeight <= 0) {+ throw new Error(+ `SastBench sample_weight must be a positive number, got ${rawSampleWeight}`,+ );+ }+ if (!VALID_LABELS.has(expectedGroundTruth)) {+ throw new Error(+ `Unsupported expected ground truth: ${expectedGroundTruth}`,+ );+ }+ const positive = expectedGroundTruth === "true_positive";+ const confirmed = verdict === "confirmed";+ const closed = verdict === "not_actionable";+ const review = verdict === "needs_review";+ const scores: Record<string, number> = {+ ...context.result.namedScores,+ strict_tp: positive && confirmed ? sampleWeight : 0,+ strict_tn: !positive && closed ? sampleWeight : 0,+ strict_fp: !positive && !closed ? sampleWeight : 0,+ strict_fn: positive && !confirmed ? sampleWeight : 0,+ decided_tp: positive && confirmed ? sampleWeight : 0,+ decided_tn: !positive && closed ? sampleWeight : 0,+ decided_fp: !positive && confirmed ? sampleWeight : 0,+ decided_fn: positive && closed ? sampleWeight : 0,+ decided_cases: confirmed || closed ? sampleWeight : 0,+ positive_cases: positive ? sampleWeight : 0,+ negative_cases: !positive ? sampleWeight : 0,+ verdict_confirmed: confirmed ? sampleWeight : 0,+ verdict_not_actionable: closed ? sampleWeight : 0,+ verdict_needs_review: review ? sampleWeight : 0,+ retained_positive: positive && (confirmed || review) ? sampleWeight : 0,+ unsafe_closure: positive && closed ? sampleWeight : 0,+ false_alert_auto_closure: !positive && closed ? sampleWeight : 0,+ false_alert_escalation: !positive && review ? sampleWeight : 0,+ confirmed_true: positive && confirmed ? sampleWeight : 0,+ confirmed_total: confirmed ? sampleWeight : 0,+ abstention: review ? sampleWeight : 0,+ remaining_analyst_work: !closed ? sampleWeight : 0,+ execution_or_parse_error: status !== "ok" ? sampleWeight : 0,+ model_error: status === "model_error" ? sampleWeight : 0,+ invalid_output: status === "invalid_output" ? sampleWeight : 0,+ };+ return {+ test: context.test,+ result: {+ ...context.result,+ namedScores: scores,+ metadata: {+ ...context.result.metadata,+ sastbench: {+ status,+ verdict,+ error,+ sampleWeight,+ },+ },+ },+ };+}diff --git a/evals/triage-finding/sastbench/assertions/sastbench-verdict.js b/evals/triage-finding/sastbench/assertions/sastbench-verdict.mtssimilarity index 55%rename from evals/triage-finding/sastbench/assertions/sastbench-verdict.jsrename to evals/triage-finding/sastbench/assertions/sastbench-verdict.mtsindex 11e7d3ae4..b53f7d791 100644--- a/evals/triage-finding/sastbench/assertions/sastbench-verdict.js+++ b/evals/triage-finding/sastbench/assertions/sastbench-verdict.mts@@ -1,16 +1,23 @@-"use strict";+import type { AssertionContext } from "../../types.ts";-const { parseCaseOutcome } = require("../scripts/sastbench-result");+import { parseCaseOutcome } from "../scripts/sastbench-result.mts";-module.exports = (output, context) => {+export default (output: unknown, context: AssertionContext) => {const expectedInputId = String(context.vars.case_id || "");- const expectedGroundTruth = String(context.test?.metadata?.ground_truth || "");+ const expectedGroundTruth = String(+ context.test?.metadata?.ground_truth || "",+ );try {const { verdict } = parseCaseOutcome(output, expectedInputId);if (verdict === "needs_review") {- return { pass: false, score: 0, reason: "SastBench case abstained with needs_review." };+ return {+ pass: false,+ score: 0,+ reason: "SastBench case abstained with needs_review.",+ };}- const prediction = verdict === "confirmed" ? "true_positive" : "false_positive";+ const prediction =+ verdict === "confirmed" ? "true_positive" : "false_positive";const pass = prediction === expectedGroundTruth;return {pass,diff --git a/evals/triage-finding/sastbench/promptfooconfig.sastbench-sample.yaml b/evals/triage-finding/sastbench/promptfooconfig.sastbench-sample.yamlindex de3a0bed3..aa4c5e25d 100644--- a/evals/triage-finding/sastbench/promptfooconfig.sastbench-sample.yaml+++ b/evals/triage-finding/sastbench/promptfooconfig.sastbench-sample.yaml@@ -7,4 +7,4 @@ description: "Codex Security triage-finding representative SastBench v0.1 sampletags:suite: sastbench-v0.1-representative-sample-tests: file://./scripts/generate-sastbench-tests.js:generateSampleTests+tests: file://./scripts/sastbench-lib.mts:generateSampleTestsdiff --git a/evals/triage-finding/sastbench/promptfooconfig.sastbench.yaml b/evals/triage-finding/sastbench/promptfooconfig.sastbench.yamlindex 44bc707dd..b26ea2b25 100644--- a/evals/triage-finding/sastbench/promptfooconfig.sastbench.yaml+++ b/evals/triage-finding/sastbench/promptfooconfig.sastbench.yaml@@ -43,7 +43,7 @@ providers:plugins: falseextensions:- - file://./assertions/sastbench-metrics.js:afterEach+ - file://./assertions/sastbench-metrics.mts:afterEachdefaultTest:assert:@@ -51,7 +51,7 @@ defaultTest:value: file://../schemas/triage-result-v0.schema.jsonmetric: schema_valid- type: javascript- value: file://./assertions/sastbench-verdict.js+ value: file://./assertions/sastbench-verdict.mtsmetric: strict_case_correct# The afterEach extension emits additive counters for every row. Representative@@ -115,4 +115,4 @@ derivedMetrics:- name: execution_or_parse_error_ratevalue: "execution_or_parse_error / max(positive_cases + negative_cases, 1)"-tests: file://./scripts/generate-sastbench-tests.js:generateTests+tests: file://./scripts/sastbench-lib.mts:generateTestsdiff --git a/evals/triage-finding/sastbench/scripts/generate-sastbench-tests.js b/evals/triage-finding/sastbench/scripts/generate-sastbench-tests.jsdeleted file mode 100644index c48087138..000000000--- a/evals/triage-finding/sastbench/scripts/generate-sastbench-tests.js+++ /dev/null@@ -1,32 +0,0 @@-#!/usr/bin/env node-"use strict";--const {- DEFAULT_DATASET_PATH,- DEFAULT_TARGET_ROOT,- generatePromptfooTests,- generateRepresentativeSampleTests,- loadDataset,-} = require("./sastbench-lib");--function loadRecords(options) {- return Array.isArray(options.dataset)- ? options.dataset- : loadDataset(options.datasetPath || DEFAULT_DATASET_PATH);-}--function generateTests(options = {}) {- const records = loadRecords(options);- return generatePromptfooTests(records, options.targetRoot || DEFAULT_TARGET_ROOT);-}--function generateSampleTests(options = {}) {- const records = loadRecords(options);- return generateRepresentativeSampleTests(- records,- options.targetRoot || DEFAULT_TARGET_ROOT,- options.sampleSpec,- );-}--module.exports = { generateSampleTests, generateTests };diff --git a/evals/triage-finding/sastbench/scripts/hydrate-sastbench-repos.js b/evals/triage-finding/sastbench/scripts/hydrate-sastbench-repos.jsdeleted file mode 100644index ab99177e8..000000000--- a/evals/triage-finding/sastbench/scripts/hydrate-sastbench-repos.js+++ /dev/null@@ -1,188 +0,0 @@-#!/usr/bin/env node-"use strict";--const childProcess = require("node:child_process");-const fs = require("node:fs");-const path = require("node:path");--const {- DEFAULT_DATASET_PATH,- DEFAULT_TARGET_ROOT,- EVAL_ROOT,- buildStateJobs,- loadDataset,- sha256Text,-} = require("./sastbench-lib");--const DEFAULT_CACHE_ROOT = path.join(EVAL_ROOT, "artifacts", "sastbench-git-cache");--function parseArgs(argv) {- const args = {- dataset: DEFAULT_DATASET_PATH,- cacheRoot: DEFAULT_CACHE_ROOT,- targetRoot: DEFAULT_TARGET_ROOT,- dryRun: false,- };- for (let index = 0; index < argv.length; index += 1) {- const arg = argv[index];- if (arg === "--") {- continue;- } else if (arg === "--dataset") {- args.dataset = argv[++index];- } else if (arg === "--cache-root") {- args.cacheRoot = argv[++index];- } else if (arg === "--target-root") {- args.targetRoot = argv[++index];- } else if (arg === "--dry-run") {- args.dryRun = true;- } else {- throw new Error(`Unknown argument: ${arg}`);- }- }- return {- ...args,- dataset: path.resolve(args.dataset),- cacheRoot: path.resolve(args.cacheRoot),- targetRoot: path.resolve(args.targetRoot),- };-}--function repositoryCacheId(repoUrl) {- return `repo-${sha256Text(repoUrl).slice(0, 16)}`;-}--function assertRepositoryNamesAreUnambiguous(records) {- const urlsByName = new Map();- for (const record of records) {- const previous = urlsByName.get(record.repo_name);- if (previous && previous !== record.repo_url) {- throw new Error(- `SastBench repository ${record.repo_name} maps to multiple repository URLs: ${previous}, ${record.repo_url}`,- );- }- urlsByName.set(record.repo_name, record.repo_url);- }-}--function buildHydrationPlan(records, { cacheRoot = DEFAULT_CACHE_ROOT, targetRoot = DEFAULT_TARGET_ROOT } = {}) {- assertRepositoryNamesAreUnambiguous(records);- const resolvedCacheRoot = path.resolve(cacheRoot);- return buildStateJobs(records, targetRoot).map((job) => ({- ...job,- cacheId: repositoryCacheId(job.repoUrl),- cacheDir: path.join(resolvedCacheRoot, `${repositoryCacheId(job.repoUrl)}.git`),- }));-}--function runGit(args, cwd, stderr = "pipe") {- return childProcess.execFileSync("git", args, {- cwd,- encoding: "utf8",- stdio: ["ignore", "pipe", stderr],- });-}--function gitOutput(args, cwd) {- return runGit(args, cwd).trim();-}--function bareGit(cacheDir, args) {- return gitOutput([`--git-dir=${cacheDir}`, ...args], path.dirname(cacheDir));-}--function ensureRepositoryCache(job) {- fs.mkdirSync(path.dirname(job.cacheDir), { recursive: true });- if (!fs.existsSync(job.cacheDir)) {- runGit(["init", "--bare", job.cacheDir], path.dirname(job.cacheDir));- bareGit(job.cacheDir, ["remote", "add", "origin", job.repoUrl]);- }- if (bareGit(job.cacheDir, ["rev-parse", "--is-bare-repository"]) !== "true") {- throw new Error(`SastBench Git cache is not bare: ${job.cacheDir}`);- }- const origin = bareGit(job.cacheDir, ["remote", "get-url", "origin"]);- if (origin !== job.repoUrl) {- throw new Error(- `SastBench Git cache origin mismatch for ${job.repoName}: expected ${job.repoUrl}, got ${origin}`,- );- }- bareGit(job.cacheDir, ["fetch", "--depth", "1", "origin", job.commitHash]);- const fetchedCommit = bareGit(job.cacheDir, ["rev-parse", "FETCH_HEAD"]);- if (fetchedCommit !== job.commitHash) {- throw new Error(- `SastBench fetched commit mismatch for ${job.repoName}: expected ${job.commitHash}, got ${fetchedCommit}`,- );- }-}--function inspectTarget(job) {- const head = gitOutput(["rev-parse", "HEAD"], job.targetDir);- const clean = gitOutput(["status", "--porcelain", "--untracked-files=all"], job.targetDir) === "";- const origin = gitOutput(["remote", "get-url", "origin"], job.targetDir);- if (head !== job.commitHash) {- throw new Error(- `SastBench target commit mismatch for ${job.stateId}: expected ${job.commitHash}, got ${head}`,- );- }- if (!clean) {- throw new Error(`SastBench target has local changes: ${job.targetDir}`);- }- if (origin !== job.repoUrl) {- throw new Error(- `SastBench target origin mismatch for ${job.stateId}: expected ${job.repoUrl}, got ${origin}`,- );- }-}--function ensureTarget(job) {- if (fs.existsSync(job.targetDir)) {- inspectTarget(job);- return "already current";- }- fs.mkdirSync(path.dirname(job.targetDir), { recursive: true });- bareGit(job.cacheDir, ["worktree", "add", "--detach", job.targetDir, job.commitHash]);- inspectTarget(job);- return "hydrated";-}--function hydrateJobs(jobs) {- console.log(`hydrating ${jobs.length} unique SastBench repository states`);- jobs.forEach((job, index) => {- ensureRepositoryCache(job);- const status = ensureTarget(job);- console.log(- `[${index + 1}/${jobs.length}] ${status}: ${job.repoName} @ ${job.commitHash} (${job.cases.length} cases)`,- );- });-}--function printPlan(jobs) {- console.log(`would hydrate ${jobs.length} unique SastBench repository states`);- for (const job of jobs) {- console.log(`${job.repoName} @ ${job.commitHash} -> ${job.targetDir} (${job.cases.length} cases)`);- }-}--function main() {- const args = parseArgs(process.argv.slice(2));- const records = loadDataset(args.dataset);- const jobs = buildHydrationPlan(records, args);- if (args.dryRun) {- printPlan(jobs);- } else {- hydrateJobs(jobs);- }-}--if (require.main === module) {- main();-}--module.exports = {- runGit,- gitOutput,- DEFAULT_CACHE_ROOT,- parseArgs,- repositoryCacheId,- buildHydrationPlan,- hydrateJobs,-};diff --git a/evals/triage-finding/sastbench/scripts/hydrate-sastbench-repos.mts b/evals/triage-finding/sastbench/scripts/hydrate-sastbench-repos.mtsnew file mode 100644index 000000000..6b95450ca--- /dev/null+++ b/evals/triage-finding/sastbench/scripts/hydrate-sastbench-repos.mts@@ -0,0 +1,187 @@+#!/usr/bin/env node+import { hash } from "node:crypto";+import type { SastBenchRecord } from "../../types.ts";++import childProcess from "node:child_process";+import fs from "node:fs";+import path from "node:path";++import {+ DEFAULT_DATASET_PATH,+ DEFAULT_TARGET_ROOT,+ EVAL_ROOT,+ repositoryStateId,+ loadDataset,+} from "./sastbench-lib.mts";++export const DEFAULT_CACHE_ROOT = path.join(+ EVAL_ROOT,+ "artifacts",+ "sastbench-git-cache",+);++export function parseArgs(argv: string[]) {+ const args = {+ dataset: DEFAULT_DATASET_PATH,+ cacheRoot: DEFAULT_CACHE_ROOT,+ targetRoot: DEFAULT_TARGET_ROOT,+ dryRun: false,+ };+ for (let index = 0; index < argv.length; index += 1) {+ const arg = argv[index];+ if (arg === "--") {+ continue;+ } else if (arg === "--dataset") {+ args.dataset = path.resolve(argv[++index]);+ } else if (arg === "--cache-root") {+ args.cacheRoot = path.resolve(argv[++index]);+ } else if (arg === "--target-root") {+ args.targetRoot = path.resolve(argv[++index]);+ } else if (arg === "--dry-run") {+ args.dryRun = true;+ } else {+ throw new Error(`Unknown argument: ${arg}`);+ }+ }+ return args;+}++export function buildHydrationPlan(+ records: Pick<SastBenchRecord, "repo_name" | "repo_url" | "commit_hash">[],+ { cacheRoot = DEFAULT_CACHE_ROOT, targetRoot = DEFAULT_TARGET_ROOT } = {},+) {+ const urlsByName = new Map();+ const groups = Map.groupBy(records, (record) => {+ const previous = urlsByName.get(record.repo_name);+ if (previous && previous !== record.repo_url) {+ throw new Error(+ `SastBench repository ${record.repo_name} maps to multiple repository URLs: ${previous}, ${record.repo_url}`,+ );+ }+ urlsByName.set(record.repo_name, record.repo_url);+ return `${record.repo_url}\0${record.commit_hash}`;+ });+ const resolvedCacheRoot = path.resolve(cacheRoot);+ return [...groups.values()].map((group) => {+ const record = group[0];+ const stateId = repositoryStateId(record);+ return {+ stateId,+ repoName: record.repo_name,+ repoUrl: record.repo_url,+ commitHash: record.commit_hash,+ targetDir: path.join(path.resolve(targetRoot), stateId),+ caseCount: group.length,+ cacheDir: path.join(+ resolvedCacheRoot,+ `repo-${hash("sha256", record.repo_url).slice(0, 16)}.git`,+ ),+ };+ });+}++export function runGit(+ args: string[],+ cwd: string,+ stderr: "pipe" | "ignore" = "pipe",+) {+ return childProcess+ .execFileSync("git", args, {+ cwd,+ encoding: "utf8",+ stdio: ["ignore", "pipe", stderr],+ })+ .trim();+}++function bareGit(cacheDir: string, args: string[]) {+ return runGit([`--git-dir=${cacheDir}`, ...args], path.dirname(cacheDir));+}++function ensureRepositoryCache(+ job: ReturnType<typeof buildHydrationPlan>[number],+) {+ fs.mkdirSync(path.dirname(job.cacheDir), { recursive: true });+ if (!fs.existsSync(job.cacheDir)) {+ runGit(["init", "--bare", job.cacheDir], path.dirname(job.cacheDir));+ bareGit(job.cacheDir, ["remote", "add", "origin", job.repoUrl]);+ }+ if (bareGit(job.cacheDir, ["rev-parse", "--is-bare-repository"]) !== "true") {+ throw new Error(`SastBench Git cache is not bare: ${job.cacheDir}`);+ }+ const origin = bareGit(job.cacheDir, ["remote", "get-url", "origin"]);+ if (origin !== job.repoUrl) {+ throw new Error(+ `SastBench Git cache origin mismatch for ${job.repoName}: expected ${job.repoUrl}, got ${origin}`,+ );+ }+ bareGit(job.cacheDir, ["fetch", "--depth", "1", "origin", job.commitHash]);+ const fetchedCommit = bareGit(job.cacheDir, ["rev-parse", "FETCH_HEAD"]);+ if (fetchedCommit !== job.commitHash) {+ throw new Error(+ `SastBench fetched commit mismatch for ${job.repoName}: expected ${job.commitHash}, got ${fetchedCommit}`,+ );+ }+}++function inspectTarget(job: ReturnType<typeof buildHydrationPlan>[number]) {+ const head = runGit(["rev-parse", "HEAD"], job.targetDir);+ const clean =+ runGit(+ ["status", "--porcelain", "--untracked-files=all"],+ job.targetDir,+ ) === "";+ const origin = runGit(["remote", "get-url", "origin"], job.targetDir);+ if (head !== job.commitHash) {+ throw new Error(+ `SastBench target commit mismatch for ${job.stateId}: expected ${job.commitHash}, got ${head}`,+ );+ }+ if (!clean) {+ throw new Error(`SastBench target has local changes: ${job.targetDir}`);+ }+ if (origin !== job.repoUrl) {+ throw new Error(+ `SastBench target origin mismatch for ${job.stateId}: expected ${job.repoUrl}, got ${origin}`,+ );+ }+}++function ensureTarget(job: ReturnType<typeof buildHydrationPlan>[number]) {+ if (fs.existsSync(job.targetDir)) {+ inspectTarget(job);+ return "already current";+ }+ fs.mkdirSync(path.dirname(job.targetDir), { recursive: true });+ bareGit(job.cacheDir, [+ "worktree",+ "add",+ "--detach",+ job.targetDir,+ job.commitHash,+ ]);+ inspectTarget(job);+ return "hydrated";+}++if (import.meta.filename === fs.realpathSync(process.argv[1])) {+ const args = parseArgs(process.argv.slice(2));+ const records = loadDataset(args.dataset);+ const jobs = buildHydrationPlan(records, args);+ console.log(+ `${args.dryRun ? "would hydrate" : "hydrating"} ${jobs.length} unique SastBench repository states`,+ );+ for (const [index, job] of jobs.entries()) {+ if (args.dryRun) {+ console.log(+ `${job.repoName} @ ${job.commitHash} -> ${job.targetDir} (${job.caseCount} cases)`,+ );+ } else {+ ensureRepositoryCache(job);+ const status = ensureTarget(job);+ console.log(+ `[${index + 1}/${jobs.length}] ${status}: ${job.repoName} @ ${job.commitHash} (${job.caseCount} cases)`,+ );+ }+ }+}diff --git a/evals/triage-finding/sastbench/scripts/install-sastbench.js b/evals/triage-finding/sastbench/scripts/install-sastbench.mtssimilarity index 62%rename from evals/triage-finding/sastbench/scripts/install-sastbench.jsrename to evals/triage-finding/sastbench/scripts/install-sastbench.mtsindex 7889539ac..11535502e 100644--- a/evals/triage-finding/sastbench/scripts/install-sastbench.js+++ b/evals/triage-finding/sastbench/scripts/install-sastbench.mts@@ -1,11 +1,10 @@#!/usr/bin/env node-"use strict";+import { hash } from "node:crypto";+import { runGit } from "./hydrate-sastbench-repos.mts";+import fs from "node:fs";+import path from "node:path";-const { runGit, gitOutput } = require("./hydrate-sastbench-repos");-const fs = require("node:fs");-const path = require("node:path");--const {+import {DEFAULT_INSTALL_ROOT,EXPECTED_CASE_COUNT,EXPECTED_LABEL_COUNTS,@@ -13,11 +12,10 @@ const {SASTBENCH_DATASET_RELATIVE_PATH,SASTBENCH_DATASET_SHA256,SASTBENCH_REPOSITORY_URL,- sha256File,validateDataset,-} = require("./sastbench-lib");+} from "./sastbench-lib.mts";-function parseArgs(argv) {+export function parseArgs(argv: string[]) {const args = { target: DEFAULT_INSTALL_ROOT, dryRun: false };for (let index = 0; index < argv.length; index += 1) {const arg = argv[index];@@ -34,28 +32,39 @@ function parseArgs(argv) {return { ...args, target: path.resolve(args.target) };}-function inspectInstallation(target) {+export function inspectInstallation(target: string) {const resolvedTarget = path.resolve(target);if (!fs.existsSync(path.join(resolvedTarget, ".git"))) {- throw new Error(`SastBench target is not a Git checkout: ${resolvedTarget}`);+ throw new Error(+ `SastBench target is not a Git checkout: ${resolvedTarget}`,+ );}- const datasetPath = path.join(resolvedTarget, SASTBENCH_DATASET_RELATIVE_PATH);+ const datasetPath = path.join(+ resolvedTarget,+ SASTBENCH_DATASET_RELATIVE_PATH,+ );if (!fs.existsSync(datasetPath)) {throw new Error(`SastBench dataset is missing: ${datasetPath}`);}const records = JSON.parse(fs.readFileSync(datasetPath, "utf8"));const datasetSummary = validateDataset(records);return {- origin: gitOutput(["remote", "get-url", "origin"], resolvedTarget),- head: gitOutput(["rev-parse", "HEAD"], resolvedTarget),- clean: gitOutput(["status", "--porcelain", "--untracked-files=all"], resolvedTarget) === "",- datasetSha256: sha256File(datasetPath),+ origin: runGit(["remote", "get-url", "origin"], resolvedTarget),+ head: runGit(["rev-parse", "HEAD"], resolvedTarget),+ clean:+ runGit(+ ["status", "--porcelain", "--untracked-files=all"],+ resolvedTarget,+ ) === "",+ datasetSha256: hash("sha256", fs.readFileSync(datasetPath)),caseCount: datasetSummary.caseCount,labelCounts: datasetSummary.labelCounts,};}-function verifyInstallation(inspection) {+export function verifyInstallation(+ inspection: ReturnType<typeof inspectInstallation>,+) {if (inspection.origin !== SASTBENCH_REPOSITORY_URL) {throw new Error(`SastBench origin mismatch: expected ${SASTBENCH_REPOSITORY_URL}, got ${inspection.origin}`,@@ -67,7 +76,9 @@ function verifyInstallation(inspection) {);}if (!inspection.clean) {- throw new Error("SastBench checkout has local changes; refusing to replace or discard them");+ throw new Error(+ "SastBench checkout has local changes; refusing to replace or discard them",+ );}if (inspection.datasetSha256 !== SASTBENCH_DATASET_SHA256) {throw new Error(@@ -82,13 +93,15 @@ function verifyInstallation(inspection) {for (const [label, expected] of Object.entries(EXPECTED_LABEL_COUNTS)) {const actual = inspection.labelCounts?.[label];if (actual !== expected) {- throw new Error(`SastBench ${label} count mismatch: expected ${expected}, got ${actual}`);+ throw new Error(+ `SastBench ${label} count mismatch: expected ${expected}, got ${actual}`,+ );}}return inspection;}-function createInstallation(target) {+function createInstallation(target: string) {fs.mkdirSync(target, { recursive: true });if (fs.readdirSync(target).length !== 0) {throw new Error(`Refusing to install into non-empty directory: ${target}`);@@ -99,7 +112,7 @@ function createInstallation(target) {runGit(["checkout", "--detach", "FETCH_HEAD"], target);}-function installSastBench(target = DEFAULT_INSTALL_ROOT) {+export function installSastBench(target: string = DEFAULT_INSTALL_ROOT) {const resolvedTarget = path.resolve(target);if (!fs.existsSync(resolvedTarget)) {fs.mkdirSync(path.dirname(resolvedTarget), { recursive: true });@@ -108,27 +121,18 @@ function installSastBench(target = DEFAULT_INSTALL_ROOT) {return verifyInstallation(inspectInstallation(resolvedTarget));}-function main() {+if (import.meta.filename === fs.realpathSync(process.argv[1])) {const args = parseArgs(process.argv.slice(2));if (args.dryRun) {- console.log(`would install SastBench ${SASTBENCH_COMMIT} into ${args.target}`);- return;+ console.log(+ `would install SastBench ${SASTBENCH_COMMIT} into ${args.target}`,+ );+ } else {+ const inspection = installSastBench(args.target);+ console.log(+ `verified SastBench ${inspection.head}: ${inspection.caseCount} cases ` ++ `(${inspection.labelCounts.true_positive} true_positive, ` ++ `${inspection.labelCounts.false_positive} false_positive)`,+ );}- const inspection = installSastBench(args.target);- console.log(- `verified SastBench ${inspection.head}: ${inspection.caseCount} cases ` +- `(${inspection.labelCounts.true_positive} true_positive, ` +- `${inspection.labelCounts.false_positive} false_positive)`,- );}--if (require.main === module) {- main();-}--module.exports = {- parseArgs,- inspectInstallation,- verifyInstallation,- installSastBench,-};diff --git a/evals/triage-finding/sastbench/scripts/run-sastbench-promptfoo.js b/evals/triage-finding/sastbench/scripts/run-sastbench-promptfoo.jsdeleted file mode 100644index fc6d4b060..000000000--- a/evals/triage-finding/sastbench/scripts/run-sastbench-promptfoo.js+++ /dev/null@@ -1,91 +0,0 @@-#!/usr/bin/env node-"use strict";--const childProcess = require("node:child_process");-const fs = require("node:fs");-const os = require("node:os");-const path = require("node:path");--const EVAL_ROOT = path.resolve(__dirname, "..", "..");-const PLUGIN_ROOT = path.resolve(EVAL_ROOT, "..", "..", "plugins", "codex-security");-const TRIAGE_SKILL_ROOT = path.join(PLUGIN_ROOT, "skills", "triage-finding");-const DEFAULT_TARGET_ROOT = path.join(EVAL_ROOT, "artifacts", "sastbench-targets");-const DEFAULT_GIT_CACHE_ROOT = path.join(EVAL_ROOT, "artifacts", "sastbench-git-cache");-const PROMPTFOO_BIN = path.join(EVAL_ROOT, "node_modules", ".bin", "promptfoo");--function copyDirectory(sourceRoot, targetRoot, excludedNames = new Set()) {- fs.mkdirSync(targetRoot, { recursive: true });- for (const entry of fs.readdirSync(sourceRoot, { withFileTypes: true })) {- if (excludedNames.has(entry.name)) {- continue;- }- const sourcePath = path.join(sourceRoot, entry.name);- const targetPath = path.join(targetRoot, entry.name);- if (entry.isDirectory()) {- copyDirectory(sourcePath, targetPath, excludedNames);- continue;- }- if (!entry.isFile()) {- throw new Error("Refusing to stage non-file runtime entry: " + sourcePath);- }- fs.copyFileSync(sourcePath, targetPath);- }-}--/**- * Give Codex a throwaway working directory that contains only the skill files- * it needs. The label-bearing dataset and Promptfoo harness stay in EVAL_ROOT.- */-function stageSkillRuntime() {- const runtimeRoot = fs.mkdtempSync(- path.join(os.tmpdir(), "codex-security-triage-finding-sastbench-"),- );- const stagedPluginRoot = path.join(runtimeRoot, "plugins", "codex-security");- copyDirectory(- TRIAGE_SKILL_ROOT,- path.join(stagedPluginRoot, "skills", "triage-finding"),- new Set(["evals"]),- );- for (const sharedDirectory of ["references", "schemas"]) {- const sourcePath = path.join(PLUGIN_ROOT, sharedDirectory);- if (fs.existsSync(sourcePath)) {- copyDirectory(sourcePath, path.join(stagedPluginRoot, sharedDirectory));- }- }- return runtimeRoot;-}--function runPromptfoo(promptfooArgs) {- const runtimeRoot = stageSkillRuntime();- const env = {- ...process.env,- SASTBENCH_RUNTIME_ROOT: runtimeRoot,- SASTBENCH_TARGET_ROOT: DEFAULT_TARGET_ROOT,- SASTBENCH_GIT_CACHE_ROOT: DEFAULT_GIT_CACHE_ROOT,- };- try {- childProcess.execFileSync(PROMPTFOO_BIN, promptfooArgs, {- cwd: EVAL_ROOT,- env,- stdio: "inherit",- });- } finally {- fs.rmSync(runtimeRoot, { recursive: true, force: true });- }-}--function main(argv = process.argv.slice(2)) {- if (argv.length === 0) {- throw new Error("Expected Promptfoo arguments");- }- runPromptfoo(argv);-}--if (require.main === module) {- main();-}--module.exports = {- runPromptfoo,- stageSkillRuntime,-};diff --git a/evals/triage-finding/sastbench/scripts/run-sastbench-promptfoo.mts b/evals/triage-finding/sastbench/scripts/run-sastbench-promptfoo.mtsnew file mode 100644index 000000000..3ab8483e4--- /dev/null+++ b/evals/triage-finding/sastbench/scripts/run-sastbench-promptfoo.mts@@ -0,0 +1,81 @@+#!/usr/bin/env node+import childProcess from "node:child_process";+import fs from "node:fs";+import os from "node:os";+import path from "node:path";++import { EVAL_ROOT, DEFAULT_TARGET_ROOT } from "./sastbench-lib.mts";+import { DEFAULT_CACHE_ROOT } from "./hydrate-sastbench-repos.mts";+const PLUGIN_ROOT = path.resolve(+ EVAL_ROOT,+ "..",+ "..",+ "plugins",+ "codex-security",+);+const TRIAGE_SKILL_ROOT = path.join(PLUGIN_ROOT, "skills", "triage-finding");+const PROMPTFOO_BIN = path.join(EVAL_ROOT, "node_modules", ".bin", "promptfoo");++function copyDirectory(+ sourceRoot: string,+ targetRoot: string,+ excludedNames: Set<string> = new Set(),+) {+ fs.mkdirSync(targetRoot, { recursive: true });+ for (const entry of fs.readdirSync(sourceRoot, { withFileTypes: true })) {+ if (excludedNames.has(entry.name)) {+ continue;+ }+ const sourcePath = path.join(sourceRoot, entry.name);+ const targetPath = path.join(targetRoot, entry.name);+ if (entry.isDirectory()) {+ copyDirectory(sourcePath, targetPath, excludedNames);+ continue;+ }+ if (!entry.isFile()) {+ throw new Error(+ "Refusing to stage non-file runtime entry: " + sourcePath,+ );+ }+ fs.copyFileSync(sourcePath, targetPath);+ }+}++/**+ * Give Codex a throwaway working directory that contains only the skill files+ * it needs. The label-bearing dataset and Promptfoo harness stay in EVAL_ROOT.+ */+const promptfooArgs = process.argv.slice(2);+if (promptfooArgs.length === 0) {+ throw new Error("Expected Promptfoo arguments");+}+const runtimeRoot = fs.mkdtempSync(+ path.join(os.tmpdir(), "codex-security-triage-finding-sastbench-"),+);+const stagedPluginRoot = path.join(runtimeRoot, "plugins", "codex-security");+copyDirectory(+ TRIAGE_SKILL_ROOT,+ path.join(stagedPluginRoot, "skills", "triage-finding"),+ new Set(["evals"]),+);+for (const sharedDirectory of ["references", "schemas"]) {+ const sourcePath = path.join(PLUGIN_ROOT, sharedDirectory);+ if (fs.existsSync(sourcePath)) {+ copyDirectory(sourcePath, path.join(stagedPluginRoot, sharedDirectory));+ }+}+const env = {+ ...process.env,+ SASTBENCH_RUNTIME_ROOT: runtimeRoot,+ SASTBENCH_TARGET_ROOT: DEFAULT_TARGET_ROOT,+ SASTBENCH_GIT_CACHE_ROOT: DEFAULT_CACHE_ROOT,+};+try {+ childProcess.execFileSync(PROMPTFOO_BIN, promptfooArgs, {+ cwd: EVAL_ROOT,+ env,+ stdio: "inherit",+ });+} finally {+ fs.rmSync(runtimeRoot, { recursive: true, force: true });+}diff --git a/evals/triage-finding/sastbench/scripts/sastbench-lib.js b/evals/triage-finding/sastbench/scripts/sastbench-lib.mtssimilarity index 53%rename from evals/triage-finding/sastbench/scripts/sastbench-lib.jsrename to evals/triage-finding/sastbench/scripts/sastbench-lib.mtsindex 621667122..e76b9a9b5 100644--- a/evals/triage-finding/sastbench/scripts/sastbench-lib.js+++ b/evals/triage-finding/sastbench/scripts/sastbench-lib.mts@@ -1,23 +1,32 @@#!/usr/bin/env node-"use strict";+import type {+ SastBenchRecord,+ SastLocation,+ DatasetExpectations,+ SampleSpec,+} from "../../types.ts";-const crypto = require("node:crypto");-const fs = require("node:fs");-const path = require("node:path");+type SampleEntry = { record: SastBenchRecord; index: number };+import { hash } from "node:crypto";+import fs from "node:fs";+import path from "node:path";-const SASTBENCH_ROOT = path.resolve(__dirname, "..");-const EVAL_ROOT = path.resolve(SASTBENCH_ROOT, "..");-const SASTBENCH_REPOSITORY_URL = "https://github.com/RivalSecurity/sastbench.git";-const SASTBENCH_COMMIT = "b8d95b7720491b21d039b7504999577ba7ebb12b";-const SASTBENCH_DATASET_RELATIVE_PATH = path.join("data", "sastbench-v0.1-original.json");-const SASTBENCH_DATASET_SHA256 =+export const EVAL_ROOT = path.resolve(import.meta.dirname, "..", "..");+export const SASTBENCH_REPOSITORY_URL =+ "https://github.com/RivalSecurity/sastbench.git";+export const SASTBENCH_COMMIT = "b8d95b7720491b21d039b7504999577ba7ebb12b";+export const SASTBENCH_DATASET_RELATIVE_PATH = path.join(+ "data",+ "sastbench-v0.1-original.json",+);+export const SASTBENCH_DATASET_SHA256 ="57c765633bdefc2130d3725dd9648bc49b85456e4dd733c55f49d44d883c9487";-const EXPECTED_CASE_COUNT = 2737;-const EXPECTED_LABEL_COUNTS = Object.freeze({+export const EXPECTED_CASE_COUNT = 2737;+export const EXPECTED_LABEL_COUNTS = Object.freeze({true_positive: 299,false_positive: 2438,});-const REPRESENTATIVE_SAMPLE_SPEC = Object.freeze({+export const REPRESENTATIVE_SAMPLE_SPEC = Object.freeze({profile: "representative-100-v1",seed: "sastbench-representative-v1",labelCounts: Object.freeze({@@ -25,9 +34,20 @@ const REPRESENTATIVE_SAMPLE_SPEC = Object.freeze({false_positive: 60,}),});-const DEFAULT_INSTALL_ROOT = path.join(EVAL_ROOT, "artifacts", "sastbench");-const DEFAULT_DATASET_PATH = path.join(DEFAULT_INSTALL_ROOT, SASTBENCH_DATASET_RELATIVE_PATH);-const DEFAULT_TARGET_ROOT = path.join(EVAL_ROOT, "artifacts", "sastbench-targets");+export const DEFAULT_INSTALL_ROOT = path.join(+ EVAL_ROOT,+ "artifacts",+ "sastbench",+);+export const DEFAULT_DATASET_PATH = path.join(+ DEFAULT_INSTALL_ROOT,+ SASTBENCH_DATASET_RELATIVE_PATH,+);+export const DEFAULT_TARGET_ROOT = path.join(+ EVAL_ROOT,+ "artifacts",+ "sastbench-targets",+);const ALLOWED_LABELS = new Set(Object.keys(EXPECTED_LABEL_COUNTS));const REQUIRED_ANALYZER_FIELDS = ["vulnerability_type",@@ -37,48 +57,60 @@ const REQUIRED_ANALYZER_FIELDS = ["commit_context",];-function sha256Text(value) {- return crypto.createHash("sha256").update(value).digest("hex");-}--function sha256File(filename) {- return sha256Text(fs.readFileSync(filename));-}--function loadDataset(filename = DEFAULT_DATASET_PATH, expectations) {- const records = JSON.parse(fs.readFileSync(filename, "utf8"));- validateDataset(records, expectations);+export function loadDataset(filename: string = DEFAULT_DATASET_PATH) {+ const records: SastBenchRecord[] = JSON.parse(+ fs.readFileSync(filename, "utf8"),+ );+ validateDataset(records);return records;}-function requireNonEmptyString(value, field, index) {+function requireNonEmptyString(+ value: unknown,+ field: string,+ index: number | string,+) {if (typeof value !== "string" || value.trim().length === 0) {throw new Error(`SastBench case ${index} requires non-empty ${field}`);}}-function requireString(value, field, index) {+function requireString(value: unknown, field: string, index: number | string) {if (typeof value !== "string") {throw new Error(`SastBench case ${index} requires string ${field}`);}}-function validateLocation(location, caseIndex, locationIndex) {+function validateLocation(+ location: SastLocation,+ caseIndex: number,+ locationIndex: number,+) {if (!location || typeof location !== "object" || Array.isArray(location)) {- throw new Error(`SastBench case ${caseIndex} location ${locationIndex} must be an object`);+ throw new Error(+ `SastBench case ${caseIndex} location ${locationIndex} must be an object`,+ );}- requireNonEmptyString(location.file, `locations[${locationIndex}].file`, caseIndex);+ requireNonEmptyString(+ location.file,+ `locations[${locationIndex}].file`,+ caseIndex,+ );// SastBench preserves scanner-native locations. One finding uses line 0 to// mean file-level scope, and some zero-width findings encode end = start - 1.if (!Number.isInteger(location.line_start) || location.line_start < 0) {- throw new Error(`SastBench case ${caseIndex} location ${locationIndex} requires line_start`);+ throw new Error(+ `SastBench case ${caseIndex} location ${locationIndex} requires line_start`,+ );}if (!Number.isInteger(location.line_end) || location.line_end < 0) {- throw new Error(`SastBench case ${caseIndex} location ${locationIndex} requires line_end`);+ throw new Error(+ `SastBench case ${caseIndex} location ${locationIndex} requires line_end`,+ );}}-function validateRecord(record, index) {+function validateRecord(record: SastBenchRecord, index: number) {if (!record || typeof record !== "object" || Array.isArray(record)) {throw new Error(`SastBench case ${index} must be an object`);}@@ -86,10 +118,14 @@ function validateRecord(record, index) {requireNonEmptyString(record.repo_url, "repo_url", index);requireNonEmptyString(record.commit_hash, "commit_hash", index);if (!/^[a-f0-9]{40}$/i.test(record.commit_hash)) {- throw new Error(`SastBench case ${index} commit_hash must be a 40-character Git SHA`);+ throw new Error(+ `SastBench case ${index} commit_hash must be a 40-character Git SHA`,+ );}if (!ALLOWED_LABELS.has(record.ground_truth)) {- throw new Error(`SastBench case ${index} has unsupported ground_truth: ${record.ground_truth}`);+ throw new Error(+ `SastBench case ${index} has unsupported ground_truth: ${record.ground_truth}`,+ );}if (!record.to_analyzer || typeof record.to_analyzer !== "object") {throw new Error(`SastBench case ${index} requires to_analyzer`);@@ -107,24 +143,34 @@ function validateRecord(record, index) {for (const field of ["vulnerability_name", "description"]) {requireString(record.to_analyzer[field], `to_analyzer.${field}`, index);}- if (!Array.isArray(record.to_analyzer.locations) || record.to_analyzer.locations.length === 0) {- throw new Error(`SastBench case ${index} requires at least one to_analyzer location`);+ if (+ !Array.isArray(record.to_analyzer.locations) ||+ record.to_analyzer.locations.length === 0+ ) {+ throw new Error(+ `SastBench case ${index} requires at least one to_analyzer location`,+ );}- record.to_analyzer.locations.forEach((location, locationIndex) =>- validateLocation(location, index, locationIndex),+ record.to_analyzer.locations.forEach(+ (location: SastLocation, locationIndex: number) =>+ validateLocation(location, index, locationIndex),);const context = record.to_analyzer.commit_context;if (!context || typeof context !== "object") {- throw new Error(`SastBench case ${index} requires to_analyzer.commit_context`);+ throw new Error(+ `SastBench case ${index} requires to_analyzer.commit_context`,+ );}if (context.commit !== record.commit_hash) {- throw new Error(`SastBench case ${index} commit_context.commit does not match commit_hash`);+ throw new Error(+ `SastBench case ${index} commit_context.commit does not match commit_hash`,+ );}}-function validateDataset(- records,- expectations = {+export function validateDataset(+ records: SastBenchRecord[],+ expectations: DatasetExpectations = {caseCount: EXPECTED_CASE_COUNT,labelCounts: EXPECTED_LABEL_COUNTS,},@@ -133,7 +179,10 @@ function validateDataset(throw new Error("SastBench dataset must be a JSON array");}records.forEach(validateRecord);- const counts = { true_positive: 0, false_positive: 0 };+ const counts: Record<string, number> = {+ true_positive: 0,+ false_positive: 0,+ };for (const record of records) {counts[record.ground_truth] += 1;}@@ -152,48 +201,29 @@ function validateDataset(return { caseCount: records.length, labelCounts: counts };}-function opaqueCaseId(index) {+export function opaqueCaseId(index: number) {if (!Number.isInteger(index) || index < 0) {throw new Error("SastBench case index must be a non-negative integer");}return `sastbench-${String(index).padStart(6, "0")}`;}-function repositoryStateId(record) {- return `state-${sha256Text(`${record.repo_url}\0${record.commit_hash}`).slice(0, 16)}`;-}--function buildStateJobs(records, targetRoot = DEFAULT_TARGET_ROOT) {- const jobsByKey = new Map();- records.forEach((record, index) => {- const key = `${record.repo_url}\0${record.commit_hash}`;- const existing = jobsByKey.get(key);- const caseEntry = { index, caseId: opaqueCaseId(index), groundTruth: record.ground_truth };- if (existing) {- existing.cases.push(caseEntry);- return;- }- const stateId = repositoryStateId(record);- jobsByKey.set(key, {- stateId,- repoName: record.repo_name,- repoUrl: record.repo_url,- commitHash: record.commit_hash,- targetDir: path.join(path.resolve(targetRoot), stateId),- cases: [caseEntry],- });- });- return [...jobsByKey.values()];+export function repositoryStateId(+ record: Pick<SastBenchRecord, "repo_url" | "commit_hash">,+) {+ return `state-${hash("sha256", `${record.repo_url}\0${record.commit_hash}`).slice(0, 16)}`;}-function formatLocation(location) {+function formatLocation(location: SastLocation) {const functionName = location.function ? ` (${location.function})` : "";return `${location.file}:${location.line_start}-${location.line_end}${functionName}`;}-function buildFindingInput(record, caseId) {+function buildFindingInput(record: SastBenchRecord, caseId: string) {const analyzer = record.to_analyzer;- const locations = analyzer.locations.map((location) => ` - ${formatLocation(location)}`);+ const locations = analyzer.locations.map(+ (location: SastLocation) => ` - ${formatLocation(location)}`,+ );const title = [analyzer.vulnerability_type, analyzer.vulnerability_name].filter((part) => part.trim().length > 0).join(" ");@@ -215,7 +245,11 @@ function buildFindingInput(record, caseId) {return findingLines.join("\n");}-function buildPromptVars(record, index, targetRoot = DEFAULT_TARGET_ROOT) {+export function buildPromptVars(+ record: SastBenchRecord,+ index: number,+ targetRoot: string | undefined = DEFAULT_TARGET_ROOT,+) {const caseId = opaqueCaseId(index);return {case_id: caseId,@@ -224,7 +258,12 @@ function buildPromptVars(record, index, targetRoot = DEFAULT_TARGET_ROOT) {};}-function buildPromptfooTest(record, index, targetRoot, metadata = {}) {+function buildPromptfooTest<Metadata extends Record<string, unknown>>(+ record: SastBenchRecord,+ index: number,+ targetRoot: string | undefined,+ metadata: Metadata,+) {const vars = buildPromptVars(record, index, targetRoot);return {description: `SastBench case ${vars.case_id}`,@@ -237,11 +276,16 @@ function buildPromptfooTest(record, index, targetRoot, metadata = {}) {};}-function generatePromptfooTests(records, targetRoot = DEFAULT_TARGET_ROOT) {- return records.map((record, index) => buildPromptfooTest(record, index, targetRoot));+export function generatePromptfooTests(+ records: SastBenchRecord[],+ targetRoot: string | undefined = DEFAULT_TARGET_ROOT,+) {+ return records.map((record: SastBenchRecord, index: number) =>+ buildPromptfooTest(record, index, targetRoot, {}),+ );}-function evidenceSizeBucket(record) {+function evidenceSizeBucket(record: SastBenchRecord) {const locationCount = record.to_analyzer.locations.length;if (locationCount === 1) {return "1";@@ -255,40 +299,38 @@ function evidenceSizeBucket(record) {return "11+";}-function allocateSampleCounts(strata, sampleCount) {+function allocateSampleCounts(+ strata: { name: string; entries: SampleEntry[] }[],+ sampleCount: number,+) {if (sampleCount < strata.length) {throw new Error(`Representative sample count ${sampleCount} cannot cover ${strata.length} evidence strata`,);}- const populationCount = strata.reduce((total, stratum) => total + stratum.entries.length, 0);- const allocations = new Map(strata.map((stratum) => [stratum.name, 1]));+ const populationCount = strata.reduce(+ (total, stratum) => total + stratum.entries.length,+ 0,+ );+ const allocations = strata.map((stratum) => ({ ...stratum, count: 1 }));+ const deficit = (stratum: (typeof allocations)[number]) =>+ (stratum.entries.length * sampleCount) / populationCount - stratum.count;let remaining = sampleCount - strata.length;while (remaining > 0) {- const candidates = strata- .filter((stratum) => allocations.get(stratum.name) < stratum.entries.length)- .map((stratum) => ({- stratum,- deficit:- (stratum.entries.length * sampleCount) / populationCount -- allocations.get(stratum.name),- }))+ const candidates = allocations+ .filter((stratum) => stratum.count < stratum.entries.length).sort((left, right) =>- right.deficit - left.deficit || left.stratum.name.localeCompare(right.stratum.name),+ deficit(right) - deficit(left) || left.name.localeCompare(right.name),);- if (candidates.length === 0) {- throw new Error(`Representative sample count ${sampleCount} exceeds the population`);- }- const selectedName = candidates[0].stratum.name;- allocations.set(selectedName, allocations.get(selectedName) + 1);+ candidates[0].count += 1;remaining -= 1;}return allocations;}-function representativeSortKey(entry) {+function representativeSortKey(entry: SampleEntry) {const record = entry.record;return [record.repo_name,@@ -299,7 +341,12 @@ function representativeSortKey(entry) {].join("\0");}-function deterministicSystematicSample(entries, sampleCount, seed, stratumName) {+function deterministicSystematicSample(+ entries: SampleEntry[],+ sampleCount: number,+ seed: string,+ stratumName: string,+) {if (sampleCount === entries.length) {return [...entries];}@@ -307,15 +354,16 @@ function deterministicSystematicSample(entries, sampleCount, seed, stratumName)representativeSortKey(left).localeCompare(representativeSortKey(right)),);const interval = sortedEntries.length / sampleCount;- const seedValue = crypto- .createHash("sha256")- .update(`${seed}\0${stratumName}`)- .digest()- .readUInt32BE(0);+ const seedValue = hash(+ "sha256",+ `${seed}\0${stratumName}`,+ "buffer",+ ).readUInt32BE(0);const start = (seedValue / 2 ** 32) * interval;return Array.from({ length: sampleCount },- (_, sampleIndex) => sortedEntries[Math.floor(start + sampleIndex * interval)],+ (_, sampleIndex) =>+ sortedEntries[Math.floor(start + sampleIndex * interval)],);}@@ -325,7 +373,10 @@ function deterministicSystematicSample(entries, sampleCount, seed, stratumName)* systematic selection over repository/CWE/language ordering spreads each* stratum across the benchmark's main metadata dimensions.*/-function selectRepresentativeSample(records, sampleSpec = REPRESENTATIVE_SAMPLE_SPEC) {+export function selectRepresentativeSample(+ records: SastBenchRecord[],+ sampleSpec: SampleSpec = REPRESENTATIVE_SAMPLE_SPEC,+) {requireNonEmptyString(sampleSpec.profile, "sample profile", "sample");requireNonEmptyString(sampleSpec.seed, "sample seed", "sample");const selectedEntries = [];@@ -333,34 +384,31 @@ function selectRepresentativeSample(records, sampleSpec = REPRESENTATIVE_SAMPLE_for (const label of ALLOWED_LABELS) {const requestedCount = sampleSpec.labelCounts[label];if (!Number.isInteger(requestedCount) || requestedCount <= 0) {- throw new Error(`Representative sample requires a positive integer count for ${label}`);+ throw new Error(+ `Representative sample requires a positive integer count for ${label}`,+ );}const labelEntries = records- .map((record, index) => ({ record, index }))- .filter((entry) => entry.record.ground_truth === label);+ .map((record: SastBenchRecord, index: number) => ({ record, index }))+ .filter((entry: SampleEntry) => entry.record.ground_truth === label);if (requestedCount > labelEntries.length) {throw new Error(`Representative sample requests ${requestedCount} ${label} cases from ${labelEntries.length}`,);}- const entriesByBucket = new Map();- for (const entry of labelEntries) {- const bucket = evidenceSizeBucket(entry.record);- const existing = entriesByBucket.get(bucket) || [];- existing.push(entry);- entriesByBucket.set(bucket, existing);- }+ const entriesByBucket = Map.groupBy(labelEntries, (entry) =>+ evidenceSizeBucket(entry.record),+ );const strata = [...entriesByBucket.entries()].map(([bucket, entries]) => ({ name: `${label}:${bucket}`, entries })).sort((left, right) => left.name.localeCompare(right.name));const allocations = allocateSampleCounts(strata, requestedCount);- for (const stratum of strata) {- const stratumSampleCount = allocations.get(stratum.name);- const sampleWeight = stratum.entries.length / stratumSampleCount;+ for (const stratum of allocations) {+ const sampleWeight = stratum.entries.length / stratum.count;for (const entry of deterministicSystematicSample(stratum.entries,- stratumSampleCount,+ stratum.count,sampleSpec.seed,stratum.name,)) {@@ -376,10 +424,10 @@ function selectRepresentativeSample(records, sampleSpec = REPRESENTATIVE_SAMPLE_return selectedEntries.sort((left, right) => left.index - right.index);}-function generateRepresentativeSampleTests(- records,- targetRoot = DEFAULT_TARGET_ROOT,- sampleSpec = REPRESENTATIVE_SAMPLE_SPEC,+export function generateRepresentativeSampleTests(+ records: SastBenchRecord[],+ targetRoot: string | undefined = DEFAULT_TARGET_ROOT,+ sampleSpec: SampleSpec = REPRESENTATIVE_SAMPLE_SPEC,) {return selectRepresentativeSample(records, sampleSpec).map((entry) =>buildPromptfooTest(entry.record, entry.index, targetRoot, {@@ -390,28 +438,24 @@ function generateRepresentativeSampleTests();}-module.exports = {- SASTBENCH_ROOT,- EVAL_ROOT,- SASTBENCH_REPOSITORY_URL,- SASTBENCH_COMMIT,- SASTBENCH_DATASET_RELATIVE_PATH,- SASTBENCH_DATASET_SHA256,- EXPECTED_CASE_COUNT,- EXPECTED_LABEL_COUNTS,- REPRESENTATIVE_SAMPLE_SPEC,- DEFAULT_INSTALL_ROOT,- DEFAULT_DATASET_PATH,- DEFAULT_TARGET_ROOT,- sha256Text,- sha256File,- loadDataset,- validateDataset,- opaqueCaseId,- repositoryStateId,- buildStateJobs,- buildPromptVars,- generatePromptfooTests,- selectRepresentativeSample,- generateRepresentativeSampleTests,-};+interface GenerateOptions {+ dataset?: SastBenchRecord[];+ datasetPath?: string;+ targetRoot?: string;+ sampleSpec?: SampleSpec;+}++export function generateTests(options: GenerateOptions = {}) {+ return generatePromptfooTests(+ options.dataset || loadDataset(options.datasetPath || DEFAULT_DATASET_PATH),+ options.targetRoot || DEFAULT_TARGET_ROOT,+ );+}++export function generateSampleTests(options: GenerateOptions = {}) {+ return generateRepresentativeSampleTests(+ options.dataset || loadDataset(options.datasetPath || DEFAULT_DATASET_PATH),+ options.targetRoot || DEFAULT_TARGET_ROOT,+ options.sampleSpec,+ );+}diff --git a/evals/triage-finding/sastbench/scripts/sastbench-result.js b/evals/triage-finding/sastbench/scripts/sastbench-result.jsdeleted file mode 100644index 00923e45c..000000000--- a/evals/triage-finding/sastbench/scripts/sastbench-result.js+++ /dev/null@@ -1,192 +0,0 @@-"use strict";--const VALID_VERDICTS = new Set(["confirmed", "not_actionable", "needs_review"]);-const VALID_LABELS = new Set(["true_positive", "false_positive"]);--function extractTriageResult(- output,- failureMessage = "Could not find a parseable triage-finding/v0 JSON result",-) {- const text = typeof output === "string" ? output : JSON.stringify(output);- const fencedBlocks = [...text.matchAll(/```(?:json)?\s*([\s\S]*?)```/gi)].map((match) =>- match[1].trim(),- );- const start = text.indexOf("{");- const end = text.lastIndexOf("}");- const candidates =- fencedBlocks.length > 0- ? fencedBlocks- : start >= 0 && end >= start- ? [text.slice(start, end + 1)]- : [];- for (const candidate of candidates) {- try {- const parsed = JSON.parse(candidate);- if (parsed && parsed.schema_version === "triage-finding/v0") {- return parsed;- }- } catch {- // The response may contain more than one fenced block. Try the next one.- }- }- throw new Error(failureMessage);-}--function parseCaseOutcome(output, expectedInputId) {- const result = extractTriageResult(output);- if (!Array.isArray(result.findings) || result.findings.length !== 1) {- throw new Error("SastBench triage output must contain exactly one finding");- }- const finding = result.findings[0];- if (finding.input_id !== expectedInputId) {- throw new Error(- `SastBench input_id mismatch: expected ${expectedInputId}, got ${finding.input_id}`,- );- }- if (finding.source_type !== "scanner_ticket") {- throw new Error(- `SastBench source_type mismatch: expected scanner_ticket, got ${finding.source_type}`,- );- }- if (!VALID_VERDICTS.has(finding.verdict)) {- throw new Error(`SastBench output has unsupported verdict: ${finding.verdict}`);- }- return { verdict: finding.verdict, result };-}--function normalizePromptfooResult(row, shardFile = null) {- const caseId = String(row.vars?.case_id || "");- const expectedGroundTruth = String(- row.testCase?.metadata?.ground_truth || row.test?.metadata?.ground_truth || "",- );- const base = {- caseId,- expectedGroundTruth,- shardFile,- latencyMs: Number.isFinite(row.latencyMs) ? row.latencyMs : null,- cost: Number.isFinite(row.cost) ? row.cost : null,- tokenUsage: row.response?.tokenUsage || row.tokenUsage || null,- sessionId: row.response?.sessionId || row.vars?.sessionId || null,- };- const output = row.response?.output;- if (typeof output !== "string" || output.trim().length === 0) {- return {- ...base,- status: "model_error",- verdict: null,- error: String(- row.response?.error || row.error || row.failureReason || "Model returned no output",- ),- };- }- try {- const parsed = parseCaseOutcome(output, caseId);- return { ...base, status: "ok", verdict: parsed.verdict, error: null };- } catch (error) {- return {- ...base,- status: "invalid_output",- verdict: null,- error: error instanceof Error ? error.message : String(error),- };- }-}--function emptyNamedScores() {- return {- strict_tp: 0,- strict_tn: 0,- strict_fp: 0,- strict_fn: 0,- decided_tp: 0,- decided_tn: 0,- decided_fp: 0,- decided_fn: 0,- decided_cases: 0,- positive_cases: 0,- negative_cases: 0,- verdict_confirmed: 0,- verdict_not_actionable: 0,- verdict_needs_review: 0,- retained_positive: 0,- unsafe_closure: 0,- false_alert_auto_closure: 0,- false_alert_escalation: 0,- confirmed_true: 0,- confirmed_total: 0,- abstention: 0,- remaining_analyst_work: 0,- execution_or_parse_error: 0,- model_error: 0,- invalid_output: 0,- };-}--/**- * Convert one normalized case into additive counters for Promptfoo.- * Promptfoo sums these named scores before evaluating the YAML derived metrics.- */-function namedScoresForOutcome(outcome) {- if (!VALID_LABELS.has(outcome.expectedGroundTruth)) {- throw new Error(`Unsupported expected ground truth: ${outcome.expectedGroundTruth}`);- }-- const scores = emptyNamedScores();- const isPositive = outcome.expectedGroundTruth === "true_positive";- scores[isPositive ? "positive_cases" : "negative_cases"] = 1;-- if (outcome.status !== "ok" || outcome.verdict === "needs_review") {- scores[isPositive ? "strict_fn" : "strict_fp"] = 1;- } else if (isPositive) {- scores[outcome.verdict === "confirmed" ? "strict_tp" : "strict_fn"] = 1;- } else {- scores[outcome.verdict === "not_actionable" ? "strict_tn" : "strict_fp"] = 1;- }-- if (outcome.status !== "ok") {- scores.execution_or_parse_error = 1;- scores[outcome.status] = 1;- scores.remaining_analyst_work = 1;- return scores;- }-- scores[`verdict_${outcome.verdict}`] = 1;- if (outcome.verdict !== "needs_review") {- scores.decided_cases = 1;- if (isPositive) {- scores[outcome.verdict === "confirmed" ? "decided_tp" : "decided_fn"] = 1;- } else {- scores[outcome.verdict === "not_actionable" ? "decided_tn" : "decided_fp"] = 1;- }- }- if (outcome.verdict === "needs_review") {- scores.abstention = 1;- }- if (outcome.verdict === "confirmed") {- scores.confirmed_total = 1;- scores.confirmed_true = isPositive ? 1 : 0;- }- if (isPositive && (outcome.verdict === "confirmed" || outcome.verdict === "needs_review")) {- scores.retained_positive = 1;- }- if (isPositive && outcome.verdict === "not_actionable") {- scores.unsafe_closure = 1;- }- if (!isPositive && outcome.verdict === "not_actionable") {- scores.false_alert_auto_closure = 1;- }- if (!isPositive && outcome.verdict === "needs_review") {- scores.false_alert_escalation = 1;- }- if (outcome.verdict === "confirmed" || outcome.verdict === "needs_review") {- scores.remaining_analyst_work = 1;- }- return scores;-}--module.exports = {- extractTriageResult,- parseCaseOutcome,- normalizePromptfooResult,- namedScoresForOutcome,-};diff --git a/evals/triage-finding/sastbench/scripts/sastbench-result.mts b/evals/triage-finding/sastbench/scripts/sastbench-result.mtsnew file mode 100644index 000000000..c098281f8--- /dev/null+++ b/evals/triage-finding/sastbench/scripts/sastbench-result.mts@@ -0,0 +1,29 @@+import { extractTriageResult } from "../../assertions/output.mts";++export { extractTriageResult };++const VALID_VERDICTS = new Set(["confirmed", "not_actionable", "needs_review"]);++export function parseCaseOutcome(output: unknown, expectedInputId: string) {+ const result = extractTriageResult(output);+ if (!Array.isArray(result.findings) || result.findings.length !== 1) {+ throw new Error("SastBench triage output must contain exactly one finding");+ }+ const finding = result.findings[0];+ if (finding.input_id !== expectedInputId) {+ throw new Error(+ `SastBench input_id mismatch: expected ${expectedInputId}, got ${finding.input_id}`,+ );+ }+ if (finding.source_type !== "scanner_ticket") {+ throw new Error(+ `SastBench source_type mismatch: expected scanner_ticket, got ${finding.source_type}`,+ );+ }+ if (!VALID_VERDICTS.has(finding.verdict)) {+ throw new Error(+ `SastBench output has unsupported verdict: ${finding.verdict}`,+ );+ }+ return { verdict: finding.verdict, result };+}diff --git a/evals/triage-finding/sastbench/scripts/test-hydrate-sastbench-repos.js b/evals/triage-finding/sastbench/scripts/test-hydrate-sastbench-repos.jsdeleted file mode 100644index 2f3289c4d..000000000--- a/evals/triage-finding/sastbench/scripts/test-hydrate-sastbench-repos.js+++ /dev/null@@ -1,71 +0,0 @@-#!/usr/bin/env node-"use strict";--const assert = require("node:assert/strict");-const path = require("node:path");--const {- buildHydrationPlan,- parseArgs,- repositoryCacheId,-} = require("./hydrate-sastbench-repos");--function record({ repoName = "example/project", repoUrl, commit, label = "true_positive" }) {- return {- finding_id: "hidden",- repo_name: repoName,- repo_url: repoUrl,- commit_hash: commit,- ground_truth: label,- metadata: { source: "cve" },- to_analyzer: {- vulnerability_type: "CWE-79",- vulnerability_name: "Cross-site Scripting",- description: "A claim.",- locations: [{ file: "src/a.js", function: "a", line_start: 1, line_end: 2 }],- commit_context: { repo: repoName, commit },- },- };-}--const repoUrl = "https://github.com/example/project";-const firstCommit = "1".repeat(40);-const secondCommit = "2".repeat(40);-const records = [- record({ repoUrl, commit: firstCommit }),- record({ repoUrl, commit: firstCommit, label: "false_positive" }),- record({ repoUrl, commit: secondCommit }),-];-const cacheRoot = path.resolve("/tmp/sastbench-git-cache");-const targetRoot = path.resolve("/tmp/sastbench-targets");--assert.equal(parseArgs(["--", "--dry-run"]).dryRun, true);--const cacheId = repositoryCacheId(repoUrl);-assert.match(cacheId, /^repo-[a-f0-9]{16}$/);-assert.equal(repositoryCacheId(repoUrl), cacheId);--const jobs = buildHydrationPlan(records, { cacheRoot, targetRoot });-assert.equal(jobs.length, 2);-assert.equal(jobs[0].cases.length, 2);-assert.equal(jobs[1].cases.length, 1);-assert.equal(jobs[0].cacheDir, path.join(cacheRoot, `${cacheId}.git`));-for (const job of jobs) {- assert.equal(path.relative(targetRoot, job.targetDir).startsWith(".."), false);- assert.equal(path.relative(cacheRoot, job.cacheDir).startsWith(".."), false);- assert.equal(job.repoUrl, repoUrl);-}--assert.throws(- () =>- buildHydrationPlan(- [- record({ repoName: "same/name", repoUrl: "https://github.com/example/one", commit: firstCommit }),- record({ repoName: "same/name", repoUrl: "https://github.com/example/two", commit: secondCommit }),- ],- { cacheRoot, targetRoot },- ),- /maps to multiple repository URLs/,-);--console.log("sastbench hydration planning tests passed");diff --git a/evals/triage-finding/sastbench/scripts/test-hydrate-sastbench-repos.mts b/evals/triage-finding/sastbench/scripts/test-hydrate-sastbench-repos.mtsnew file mode 100644index 000000000..b09314472--- /dev/null+++ b/evals/triage-finding/sastbench/scripts/test-hydrate-sastbench-repos.mts@@ -0,0 +1,68 @@+#!/usr/bin/env node+import assert from "node:assert/strict";+import path from "node:path";++import { buildHydrationPlan, parseArgs } from "./hydrate-sastbench-repos.mts";++const repoUrl = "https://github.com/example/project";+const firstCommit = "1".repeat(40);+const secondCommit = "2".repeat(40);+const baseRecord = {+ repo_name: "example/project",+ repo_url: repoUrl,+ commit_hash: firstCommit,+ ground_truth: "true_positive",+};+const records = [+ { ...baseRecord },+ { ...baseRecord, ground_truth: "false_positive" },+ { ...baseRecord, commit_hash: secondCommit },+];+const cacheRoot = path.resolve("/tmp/sastbench-git-cache");+const targetRoot = path.resolve("/tmp/sastbench-targets");++assert.equal(parseArgs(["--", "--dry-run"]).dryRun, true);++const jobs = buildHydrationPlan(records, { cacheRoot, targetRoot });+assert.equal(+ buildHydrationPlan(records, { cacheRoot, targetRoot })[0].cacheDir,+ jobs[0].cacheDir,+);+assert.equal(jobs.length, 2);+assert.equal(jobs[0].caseCount, 2);+assert.equal(jobs[1].caseCount, 1);+assert.equal(+ jobs[0].cacheDir,+ path.join(cacheRoot, "repo-acb9cf7430784a93.git"),+);+for (const job of jobs) {+ assert.equal(+ path.relative(targetRoot, job.targetDir).startsWith(".."),+ false,+ );+ assert.equal(path.relative(cacheRoot, job.cacheDir).startsWith(".."), false);+ assert.equal(job.repoUrl, repoUrl);+}++assert.throws(+ () =>+ buildHydrationPlan(+ [+ {+ ...baseRecord,+ repo_name: "same/name",+ repo_url: "https://github.com/example/one",+ },+ {+ ...baseRecord,+ repo_name: "same/name",+ repo_url: "https://github.com/example/two",+ commit_hash: secondCommit,+ },+ ],+ { cacheRoot, targetRoot },+ ),+ /maps to multiple repository URLs/,+);++console.log("sastbench hydration planning tests passed");diff --git a/evals/triage-finding/sastbench/scripts/test-install-sastbench.js b/evals/triage-finding/sastbench/scripts/test-install-sastbench.mtssimilarity index 71%rename from evals/triage-finding/sastbench/scripts/test-install-sastbench.jsrename to evals/triage-finding/sastbench/scripts/test-install-sastbench.mtsindex 11d88d8ed..f34993632 100644--- a/evals/triage-finding/sastbench/scripts/test-install-sastbench.js+++ b/evals/triage-finding/sastbench/scripts/test-install-sastbench.mts@@ -1,16 +1,14 @@#!/usr/bin/env node-"use strict";+import assert from "node:assert/strict";-const assert = require("node:assert/strict");--const { verifyInstallation } = require("./install-sastbench");-const {+import { verifyInstallation } from "./install-sastbench.mts";+import {EXPECTED_CASE_COUNT,EXPECTED_LABEL_COUNTS,SASTBENCH_COMMIT,SASTBENCH_DATASET_SHA256,SASTBENCH_REPOSITORY_URL,-} = require("./sastbench-lib");+} from "./sastbench-lib.mts";function validInspection(overrides = {}) {return {@@ -26,20 +24,27 @@ function validInspection(overrides = {}) {assert.deepEqual(verifyInstallation(validInspection()), validInspection());assert.throws(- () => verifyInstallation(validInspection({ origin: "https://example.test/wrong.git" })),+ () =>+ verifyInstallation(+ validInspection({ origin: "https://example.test/wrong.git" }),+ ),/origin mismatch/,);assert.throws(() => verifyInstallation(validInspection({ head: "0".repeat(40) })),/commit mismatch/,);-assert.throws(() => verifyInstallation(validInspection({ clean: false })), /local changes/);+assert.throws(+ () => verifyInstallation(validInspection({ clean: false })),+ /local changes/,+);assert.throws(() => verifyInstallation(validInspection({ datasetSha256: "0".repeat(64) })),/dataset SHA-256 mismatch/,);assert.throws(- () => verifyInstallation(validInspection({ caseCount: EXPECTED_CASE_COUNT - 1 })),+ () =>+ verifyInstallation(validInspection({ caseCount: EXPECTED_CASE_COUNT - 1 })),/case count mismatch/,);assert.throws(diff --git a/evals/triage-finding/sastbench/scripts/test-promptfoo-native-harness.js b/evals/triage-finding/sastbench/scripts/test-promptfoo-native-harness.mtssimilarity index 65%rename from evals/triage-finding/sastbench/scripts/test-promptfoo-native-harness.jsrename to evals/triage-finding/sastbench/scripts/test-promptfoo-native-harness.mtsindex 013a2fd16..8c201d53e 100644--- a/evals/triage-finding/sastbench/scripts/test-promptfoo-native-harness.js+++ b/evals/triage-finding/sastbench/scripts/test-promptfoo-native-harness.mts@@ -1,22 +1,28 @@#!/usr/bin/env node-"use strict";+import assert from "node:assert/strict";+import fs from "node:fs";+import path from "node:path";-const assert = require("node:assert/strict");-const fs = require("node:fs");-const path = require("node:path");--const sastbenchRoot = path.resolve(__dirname, "..");+const sastbenchRoot = path.resolve(import.meta.dirname, "..");const evalRoot = path.resolve(sastbenchRoot, "..");-const config = fs.readFileSync(path.join(sastbenchRoot, "promptfooconfig.sastbench.yaml"), "utf8");+const config = fs.readFileSync(+ path.join(sastbenchRoot, "promptfooconfig.sastbench.yaml"),+ "utf8",+);const sampleConfig = fs.readFileSync(path.join(sastbenchRoot, "promptfooconfig.sastbench-sample.yaml"),"utf8",);-const packageJson = JSON.parse(fs.readFileSync(path.join(evalRoot, "package.json"), "utf8"));+const packageJson = JSON.parse(+ fs.readFileSync(path.join(evalRoot, "package.json"), "utf8"),+);assert.match(config, /working_dir:\s+"\{\{env\.SASTBENCH_RUNTIME_ROOT\}\}"/);assert.match(config, /skip_git_repo_check:\s+true/);-assert.doesNotMatch(config, /working_dir:\s+\.\.\/\.\.\/\.\.\/\.\.\/\.\.\/\.\./);+assert.doesNotMatch(+ config,+ /working_dir:\s+\.\.\/\.\.\/\.\.\/\.\.\/\.\.\/\.\./,+);assert.match(config,/additional_directories:\n\s+- "\{\{env\.SASTBENCH_TARGET_ROOT\}\}"\n\s+- "\{\{env\.SASTBENCH_GIT_CACHE_ROOT\}\}"/,@@ -29,17 +35,20 @@ assert.match(config, /":minimal":\s+read/);assert.match(config, /":workspace_roots":\s+read/);assert.match(config, /network:\n\s+enabled:\s+false/);assert.match(config, /id:\s+openai:codex-sdk:gpt-5\.5/);-assert.match(config, /label:\s+"Codex SDK triage-finding SastBench \(gpt-5\.5\)"/);+assert.match(+ config,+ /label:\s+"Codex SDK triage-finding SastBench \(gpt-5\.5\)"/,+);assert.doesNotMatch(config, /gpt-5\.6/);assert.match(config,- /tests:\s+file:\/\/\.\/scripts\/generate-sastbench-tests\.js:generateTests/,+ /tests:\s+file:\/\/\.\/scripts\/sastbench-lib\.mts:generateTests/,);assert.match(config, /metric:\s+schema_valid/);assert.match(config, /metric:\s+strict_case_correct/);assert.match(config,- /extensions:\n\s+- file:\/\/\.\/assertions\/sastbench-metrics\.js:afterEach/,+ /extensions:\n\s+- file:\/\/\.\/assertions\/sastbench-metrics\.mts:afterEach/,);for (const metric of ["strict_precision",@@ -66,37 +75,58 @@ assert.match(config,/strict_accuracy\n\s+value:\s+"\(strict_tp \+ strict_tn\) \/ max\(positive_cases \+ negative_cases, 1\)"/,);-assert.match(sampleConfig, /^\$ref:\s+\.\/sastbench\/promptfooconfig\.sastbench\.yaml/m);assert.match(sampleConfig,- /tests:\s+file:\/\/\.\/scripts\/generate-sastbench-tests\.js:generateSampleTests/,+ /^\$ref:\s+\.\/sastbench\/promptfooconfig\.sastbench\.yaml/m,+);+assert.match(+ sampleConfig,+ /tests:\s+file:\/\/\.\/scripts\/sastbench-lib\.mts:generateSampleTests/,);assert.doesNotMatch(sampleConfig, /providers:/);assert.doesNotMatch(sampleConfig, /derivedMetrics:/);-assert.match(packageJson.scripts["eval:sastbench"], /run-sastbench-promptfoo\.js eval/);+assert.match(+ packageJson.scripts["eval:sastbench"],+ /run-sastbench-promptfoo\.mts eval/,+);assert.match(packageJson.scripts["eval:sastbench"], /--no-cache/);assert.match(packageJson.scripts["eval:sastbench"], /--no-share/);assert.doesNotMatch(packageJson.scripts["eval:sastbench"], /--filter-range/);assert.doesNotMatch(packageJson.scripts["eval:sastbench"], /--max-concurrency/);assert.equal("score:sastbench" in packageJson.scripts, false);assert.equal("report:sastbench" in packageJson.scripts, false);-assert.doesNotMatch(packageJson.scripts["eval:sastbench"], /PROMPTFOO_FAILED_TEST_EXIT_CODE/);+assert.doesNotMatch(+ packageJson.scripts["eval:sastbench"],+ /PROMPTFOO_FAILED_TEST_EXIT_CODE/,+);assert.match(packageJson.scripts["validate:sastbench:sample"],- /run-sastbench-promptfoo\.js validate config -c .\/sastbench\/promptfooconfig\.sastbench-sample\.yaml/,+ /run-sastbench-promptfoo\.mts validate config -c .\/sastbench\/promptfooconfig\.sastbench-sample\.yaml/,);assert.match(packageJson.scripts["eval:sastbench:sample"],- /run-sastbench-promptfoo\.js eval -c .\/sastbench\/promptfooconfig\.sastbench-sample\.yaml/,+ /run-sastbench-promptfoo\.mts eval -c .\/sastbench\/promptfooconfig\.sastbench-sample\.yaml/,);assert.match(packageJson.scripts["eval:sastbench:sample"], /--no-cache/);assert.match(packageJson.scripts["eval:sastbench:sample"], /--no-share/);-assert.match(packageJson.scripts["eval:sastbench:sample"], /--max-concurrency 32/);+assert.match(+ packageJson.scripts["eval:sastbench:sample"],+ /--max-concurrency 32/,+);assert.equal("sastbench:generate" in packageJson.scripts, false);-assert.equal(fs.existsSync(path.join(__dirname, "run-sastbench-promptfoo.js")), true);assert.equal(- fs.existsSync(path.join(sastbenchRoot, "docs", "2026-06-22-sastbench-triage-finding-eval.md")),+ fs.existsSync(path.join(import.meta.dirname, "run-sastbench-promptfoo.mts")),+ true,+);+assert.equal(+ fs.existsSync(+ path.join(+ sastbenchRoot,+ "docs",+ "2026-06-22-sastbench-triage-finding-eval.md",+ ),+ ),false,);diff --git a/evals/triage-finding/sastbench/scripts/test-sastbench-lib.js b/evals/triage-finding/sastbench/scripts/test-sastbench-lib.mtssimilarity index 86%rename from evals/triage-finding/sastbench/scripts/test-sastbench-lib.jsrename to evals/triage-finding/sastbench/scripts/test-sastbench-lib.mtsindex 21c4224d8..a5de447a7 100644--- a/evals/triage-finding/sastbench/scripts/test-sastbench-lib.js+++ b/evals/triage-finding/sastbench/scripts/test-sastbench-lib.mts@@ -1,21 +1,20 @@#!/usr/bin/env node-"use strict";+import assert from "node:assert/strict";+import path from "node:path";+import { buildHydrationPlan } from "./hydrate-sastbench-repos.mts";-const assert = require("node:assert/strict");-const path = require("node:path");--const {+import {REPRESENTATIVE_SAMPLE_SPEC,buildPromptVars,- buildStateJobs,+ generateTests,+ generateSampleTests,generatePromptfooTests,generateRepresentativeSampleTests,opaqueCaseId,repositoryStateId,selectRepresentativeSample,validateDataset,-} = require("./sastbench-lib");-const { generateSampleTests, generateTests } = require("./generate-sastbench-tests");+} from "./sastbench-lib.mts";assert.deepEqual(REPRESENTATIVE_SAMPLE_SPEC, {profile: "representative-100-v1",@@ -40,7 +39,8 @@ function benchmarkRecord(overrides = {}) {to_analyzer: {vulnerability_type: "CWE-79",vulnerability_name: "Cross-site Scripting",- description: "Untrusted text may be rendered without context-aware escaping.",+ description:+ "Untrusted text may be rendered without context-aware escaping.",locations: [{file: "src/render.js",@@ -109,9 +109,9 @@ const stateId = repositoryStateId(positive);assert.match(stateId, /^state-[a-f0-9]{16}$/);assert.equal(repositoryStateId(negative), stateId);-const jobs = buildStateJobs([positive, negative], targetRoot);+const jobs = buildHydrationPlan([positive, negative], { targetRoot });assert.equal(jobs.length, 1);-assert.equal(jobs[0].cases.length, 2);+assert.equal(jobs[0].caseCount, 2);assert.equal(jobs[0].repoUrl, positive.repo_url);assert.equal(jobs[0].commitHash, positive.commit_hash);assert.equal(jobs[0].targetDir, path.join(targetRoot, stateId));@@ -142,9 +142,16 @@ assert.equal(tests[0].metadata.ground_truth, "true_positive");assert.equal(tests[1].metadata.ground_truth, "false_positive");assert.equal("expected_ground_truth" in tests[0].vars, false);assert.equal(tests[0].metadata.case_id, "sastbench-000000");-assert.deepEqual(Object.keys(tests[0].vars).sort(), ["case_id", "finding_input", "target_repo"]);+assert.deepEqual(Object.keys(tests[0].vars).sort(), [+ "case_id",+ "finding_input",+ "target_repo",+]);-const generatedTests = generateTests({ dataset: [positive, negative], targetRoot });+const generatedTests = generateTests({+ dataset: [positive, negative],+ targetRoot,+});assert.deepEqual(generatedTests, tests);const sampleDataset = Array.from({ length: 20 }, (_, index) => {@@ -184,17 +191,25 @@ assert.deepEqual(selectedSample.map(({ index }) => index),selectedAgain.map(({ index }) => index),);-assert.equal(new Set(selectedSample.map(({ index }) => index)).size, selectedSample.length);assert.equal(- selectedSample.filter(({ record }) => record.ground_truth === "true_positive").length,+ new Set(selectedSample.map(({ index }) => index)).size,+ selectedSample.length,+);+assert.equal(+ selectedSample.filter(({ record }) => record.ground_truth === "true_positive")+ .length,4,);assert.equal(- selectedSample.filter(({ record }) => record.ground_truth === "false_positive").length,+ selectedSample.filter(+ ({ record }) => record.ground_truth === "false_positive",+ ).length,6,);for (const label of ["true_positive", "false_positive"]) {- const populationCount = sampleDataset.filter((record) => record.ground_truth === label).length;+ const populationCount = sampleDataset.filter(+ (record) => record.ground_truth === label,+ ).length;const representedPopulation = selectedSample.filter(({ record }) => record.ground_truth === label).reduce((total, entry) => total + entry.sampleWeight, 0);@@ -212,9 +227,15 @@ assert.ok((test) => Number(test.metadata.case_id.replace("sastbench-", "")) >= 10,),);-assert.ok(representativeTests.every((test) => test.metadata.sample_profile === sampleSpec.profile));+assert.ok(+ representativeTests.every(+ (test) => test.metadata.sample_profile === sampleSpec.profile,+ ),+);assert.ok(representativeTests.every((test) => test.metadata.sample_weight > 0));-assert.ok(representativeTests.every((test) => "sample_stratum" in test.metadata));+assert.ok(+ representativeTests.every((test) => "sample_stratum" in test.metadata),+);assert.ok(representativeTests.every((test) => !("sample_weight" in test.vars)));const generatedSampleTests = generateSampleTests({diff --git a/evals/triage-finding/sastbench/scripts/test-sastbench-result.js b/evals/triage-finding/sastbench/scripts/test-sastbench-result.jsdeleted file mode 100644index 7a799e7c1..000000000--- a/evals/triage-finding/sastbench/scripts/test-sastbench-result.js+++ /dev/null@@ -1,210 +0,0 @@-#!/usr/bin/env node-"use strict";--const assert = require("node:assert/strict");--const {- extractTriageResult,- normalizePromptfooResult,- parseCaseOutcome,-} = require("./sastbench-result");-const { afterEach: addSastBenchMetrics } = require("../assertions/sastbench-metrics");-const verdictAssertion = require("../assertions/sastbench-verdict");--function triageResult(verdict, inputId = "sastbench-000000") {- return {- schema_version: "triage-finding/v0",- repository: { path: "/target", revision: "a".repeat(40) },- findings: [- {- input_id: inputId,- source_type: "scanner_ticket",- verdict,- },- ],- };-}--const fenced = `Summary\n\n\`\`\`json\n${JSON.stringify(triageResult("confirmed"), null, 2)}\n\`\`\``;-assert.equal(extractTriageResult(fenced).findings[0].verdict, "confirmed");-assert.equal(- extractTriageResult(JSON.stringify(triageResult("not_actionable"))).findings[0].verdict,- "not_actionable",-);-assert.throws(() => extractTriageResult("no json"), {- message: "Could not find a parseable triage-finding/v0 JSON result",-});-assert.throws(- () => parseCaseOutcome(JSON.stringify(triageResult("confirmed", "wrong")), "sastbench-000000"),- /input_id mismatch/,-);-assert.throws(- () => parseCaseOutcome(JSON.stringify({ ...triageResult("confirmed"), findings: [] }), "sastbench-000000"),- /exactly one finding/,-);-assert.throws(- () => parseCaseOutcome(JSON.stringify(triageResult("unexpected")), "sastbench-000000"),- /unsupported verdict/,-);--assert.equal(- verdictAssertion(fenced, {- vars: { case_id: "sastbench-000000" },- test: { metadata: { ground_truth: "true_positive" } },- }).pass,- true,-);-assert.equal(- verdictAssertion(fenced, {- vars: { case_id: "sastbench-000000" },- test: { metadata: { ground_truth: "false_positive" } },- }).pass,- false,-);--function extensionContext({ caseId, expectedGroundTruth, output, error, sampleWeight }) {- return {- test: {- vars: { case_id: caseId },- metadata: {- ground_truth: expectedGroundTruth,- ...(sampleWeight === undefined ? {} : { sample_weight: sampleWeight }),- },- },- result: {- namedScores: { existing_metric: 1 },- response: output === undefined ? { error } : { output },- failureReason: error ? "error" : undefined,- },- };-}--function sumNamedScores(rows) {- const totals = {};- for (const row of rows) {- for (const [name, value] of Object.entries(row.result.namedScores)) {- totals[name] = (totals[name] || 0) + value;- }- }- return totals;-}--const outcomes = [- { caseId: "tp-confirmed", expectedGroundTruth: "true_positive", verdict: "confirmed" },- { caseId: "tp-closed", expectedGroundTruth: "true_positive", verdict: "not_actionable" },- { caseId: "tp-review", expectedGroundTruth: "true_positive", verdict: "needs_review" },- { caseId: "fp-closed", expectedGroundTruth: "false_positive", verdict: "not_actionable" },- { caseId: "fp-confirmed", expectedGroundTruth: "false_positive", verdict: "confirmed" },- { caseId: "fp-review", expectedGroundTruth: "false_positive", verdict: "needs_review" },-];-const nativeMetricRows = outcomes.map((outcome) =>- addSastBenchMetrics(- extensionContext({- caseId: outcome.caseId,- expectedGroundTruth: outcome.expectedGroundTruth,- output: JSON.stringify(triageResult(outcome.verdict, outcome.caseId)),- }),- ),-);-const totals = sumNamedScores(nativeMetricRows);--assert.deepEqual(- {- tp: totals.strict_tp,- tn: totals.strict_tn,- fp: totals.strict_fp,- fn: totals.strict_fn,- },- { tp: 1, tn: 1, fp: 2, fn: 2 },-);-assert.deepEqual(- {- tp: totals.decided_tp,- tn: totals.decided_tn,- fp: totals.decided_fp,- fn: totals.decided_fn,- },- { tp: 1, tn: 1, fp: 1, fn: 1 },-);-assert.equal(totals.decided_cases, 4);-assert.equal(totals.positive_cases, 3);-assert.equal(totals.negative_cases, 3);-assert.equal(totals.retained_positive, 2);-assert.equal(totals.unsafe_closure, 1);-assert.equal(totals.false_alert_auto_closure, 1);-assert.equal(totals.false_alert_escalation, 1);-assert.equal(totals.confirmed_true, 1);-assert.equal(totals.confirmed_total, 2);-assert.equal(totals.abstention, 2);-assert.equal(totals.remaining_analyst_work, 4);-assert.equal(totals.execution_or_parse_error, 0);-assert.equal(totals.existing_metric, 6);--const positiveProviderError = addSastBenchMetrics(- extensionContext({- caseId: "tp-error",- expectedGroundTruth: "true_positive",- error: "provider failed",- }),-);-assert.equal(positiveProviderError.result.namedScores.strict_fn, 1);-assert.equal(positiveProviderError.result.namedScores.execution_or_parse_error, 1);-assert.equal(positiveProviderError.result.namedScores.remaining_analyst_work, 1);-assert.equal(positiveProviderError.result.metadata.sastbench.status, "model_error");--const negativeInvalidOutput = addSastBenchMetrics(- extensionContext({- caseId: "fp-invalid",- expectedGroundTruth: "false_positive",- output: "not a triage result",- }),-);-assert.equal(negativeInvalidOutput.result.namedScores.strict_fp, 1);-assert.equal(negativeInvalidOutput.result.namedScores.execution_or_parse_error, 1);-assert.equal(negativeInvalidOutput.result.metadata.sastbench.status, "invalid_output");--const weightedPositive = addSastBenchMetrics(- extensionContext({- caseId: "weighted-tp",- expectedGroundTruth: "true_positive",- output: JSON.stringify(triageResult("confirmed", "weighted-tp")),- sampleWeight: 2.5,- }),-);-assert.equal(weightedPositive.result.namedScores.strict_tp, 2.5);-assert.equal(weightedPositive.result.namedScores.positive_cases, 2.5);-assert.equal(weightedPositive.result.namedScores.existing_metric, 1);--const normalized = normalizePromptfooResult({- latencyMs: 1234,- cost: 0.25,- vars: { case_id: "sastbench-000000" },- testCase: { metadata: { ground_truth: "true_positive" } },- response: {- output: fenced,- tokenUsage: { prompt: 10, completion: 5, total: 15, cached: 2 },- },-});-assert.equal(normalized.status, "ok");-assert.equal(normalized.verdict, "confirmed");-assert.equal(normalized.latencyMs, 1234);-assert.equal(normalized.cost, 0.25);-assert.equal(normalized.tokenUsage.total, 15);--const invalid = normalizePromptfooResult({- vars: { case_id: "sastbench-000001" },- testCase: { metadata: { ground_truth: "false_positive" } },- response: { output: "invalid" },-});-assert.equal(invalid.status, "invalid_output");-assert.match(invalid.error, /triage-finding\/v0/);--const modelError = normalizePromptfooResult({- vars: { case_id: "sastbench-000002" },- testCase: { metadata: { ground_truth: "true_positive" } },- response: { error: "provider failed" },-});-assert.equal(modelError.status, "model_error");-assert.equal(modelError.error, "provider failed");--console.log("sastbench result and native metric tests passed");diff --git a/evals/triage-finding/sastbench/scripts/test-sastbench-result.mts b/evals/triage-finding/sastbench/scripts/test-sastbench-result.mtsnew file mode 100644index 000000000..40a53598e--- /dev/null+++ b/evals/triage-finding/sastbench/scripts/test-sastbench-result.mts@@ -0,0 +1,262 @@+#!/usr/bin/env node+import assert from "node:assert/strict";++import { extractTriageResult, parseCaseOutcome } from "./sastbench-result.mts";+import { afterEach as addSastBenchMetrics } from "../assertions/sastbench-metrics.mts";+import verdictAssertion from "../assertions/sastbench-verdict.mts";++function triageResult(verdict: string, inputId = "sastbench-000000") {+ return {+ schema_version: "triage-finding/v0",+ repository: { path: "/target", revision: "a".repeat(40) },+ findings: [+ {+ input_id: inputId,+ source_type: "scanner_ticket",+ verdict,+ },+ ],+ };+}++const fenced = `Summary\n\n\`\`\`json\n${JSON.stringify(triageResult("confirmed"), null, 2)}\n\`\`\``;+assert.equal(extractTriageResult(fenced).findings[0].verdict, "confirmed");+assert.equal(+ extractTriageResult(JSON.stringify(triageResult("not_actionable")))+ .findings[0].verdict,+ "not_actionable",+);+assert.equal(+ extractTriageResult("```json\ninvalid\n```\n" + fenced).findings[0].verdict,+ "confirmed",+);+assert.throws(+ () =>+ extractTriageResult(+ "```json\ninvalid\n```\n" + JSON.stringify(triageResult("confirmed")),+ ),+ { message: "Could not find a parseable triage-finding/v0 JSON result" },+);+assert.throws(+ () =>+ parseCaseOutcome(+ JSON.stringify(triageResult("confirmed", "wrong")),+ "sastbench-000000",+ ),+ /input_id mismatch/,+);+assert.throws(+ () =>+ parseCaseOutcome(+ JSON.stringify({ ...triageResult("confirmed"), findings: [] }),+ "sastbench-000000",+ ),+ /exactly one finding/,+);+assert.throws(+ () =>+ parseCaseOutcome(+ JSON.stringify(triageResult("unexpected")),+ "sastbench-000000",+ ),+ /unsupported verdict/,+);++assert.equal(+ verdictAssertion(fenced, {+ vars: { case_id: "sastbench-000000" },+ test: { metadata: { ground_truth: "true_positive" } },+ }).pass,+ true,+);+assert.equal(+ verdictAssertion(fenced, {+ vars: { case_id: "sastbench-000000" },+ test: { metadata: { ground_truth: "false_positive" } },+ }).pass,+ false,+);++function extensionContext({+ caseId,+ expectedGroundTruth,+ output,+ error,+ sampleWeight,+}: {+ caseId: string;+ expectedGroundTruth: string;+ output?: string;+ error?: string;+ sampleWeight?: number;+}) {+ return {+ test: {+ vars: { case_id: caseId },+ metadata: {+ ground_truth: expectedGroundTruth,+ ...(sampleWeight === undefined ? {} : { sample_weight: sampleWeight }),+ },+ },+ result: {+ latencyMs: 1234,+ cost: 0.25,+ namedScores: { existing_metric: 1 },+ response:+ output === undefined+ ? { error }+ : {+ output,+ tokenUsage: { prompt: 10, completion: 5, total: 15, cached: 2 },+ sessionId: "session",+ },+ failureReason: error ? "error" : undefined,+ },+ };+}++const outcomes = [+ {+ caseId: "tp-confirmed",+ expectedGroundTruth: "true_positive",+ verdict: "confirmed",+ },+ {+ caseId: "tp-closed",+ expectedGroundTruth: "true_positive",+ verdict: "not_actionable",+ },+ {+ caseId: "tp-review",+ expectedGroundTruth: "true_positive",+ verdict: "needs_review",+ },+ {+ caseId: "fp-closed",+ expectedGroundTruth: "false_positive",+ verdict: "not_actionable",+ },+ {+ caseId: "fp-confirmed",+ expectedGroundTruth: "false_positive",+ verdict: "confirmed",+ },+ {+ caseId: "fp-review",+ expectedGroundTruth: "false_positive",+ verdict: "needs_review",+ },+];+const nativeContexts = outcomes.map((outcome) =>+ extensionContext({+ caseId: outcome.caseId,+ expectedGroundTruth: outcome.expectedGroundTruth,+ output: JSON.stringify(triageResult(outcome.verdict, outcome.caseId)),+ }),+);+const nativeMetricRows = nativeContexts.map(addSastBenchMetrics);+const totals: Record<string, number> = {};+for (const row of nativeMetricRows) {+ for (const [name, value] of Object.entries(row.result.namedScores)) {+ totals[name] = (totals[name] || 0) + value;+ }+}++assert.deepEqual(+ {+ tp: totals.strict_tp,+ tn: totals.strict_tn,+ fp: totals.strict_fp,+ fn: totals.strict_fn,+ },+ { tp: 1, tn: 1, fp: 2, fn: 2 },+);+assert.deepEqual(+ {+ tp: totals.decided_tp,+ tn: totals.decided_tn,+ fp: totals.decided_fp,+ fn: totals.decided_fn,+ },+ { tp: 1, tn: 1, fp: 1, fn: 1 },+);+assert.equal(totals.decided_cases, 4);+assert.equal(totals.positive_cases, 3);+assert.equal(totals.negative_cases, 3);+assert.equal(totals.retained_positive, 2);+assert.equal(totals.unsafe_closure, 1);+assert.equal(totals.false_alert_auto_closure, 1);+assert.equal(totals.false_alert_escalation, 1);+assert.equal(totals.confirmed_true, 1);+assert.equal(totals.confirmed_total, 2);+assert.equal(totals.abstention, 2);+assert.equal(totals.remaining_analyst_work, 4);+assert.equal(totals.execution_or_parse_error, 0);+assert.equal(totals.existing_metric, 6);++const positiveProviderError = addSastBenchMetrics(+ extensionContext({+ caseId: "tp-error",+ expectedGroundTruth: "true_positive",+ error: "provider failed",+ }),+);+assert.equal(positiveProviderError.result.namedScores.strict_fn, 1);+assert.equal(+ positiveProviderError.result.namedScores.execution_or_parse_error,+ 1,+);+assert.equal(+ positiveProviderError.result.namedScores.remaining_analyst_work,+ 1,+);+assert.equal(+ positiveProviderError.result.metadata.sastbench.status,+ "model_error",+);++const negativeInvalidOutput = addSastBenchMetrics(+ extensionContext({+ caseId: "fp-invalid",+ expectedGroundTruth: "false_positive",+ output: "not a triage result",+ }),+);+assert.equal(negativeInvalidOutput.result.namedScores.strict_fp, 1);+assert.equal(+ negativeInvalidOutput.result.namedScores.execution_or_parse_error,+ 1,+);+assert.equal(+ negativeInvalidOutput.result.metadata.sastbench.status,+ "invalid_output",+);++const weightedPositive = addSastBenchMetrics(+ extensionContext({+ caseId: "weighted-tp",+ expectedGroundTruth: "true_positive",+ output: JSON.stringify(triageResult("confirmed", "weighted-tp")),+ sampleWeight: 2.5,+ }),+);+assert.equal(weightedPositive.result.namedScores.strict_tp, 2.5);+assert.equal(weightedPositive.result.namedScores.positive_cases, 2.5);+assert.equal(weightedPositive.result.namedScores.existing_metric, 1);++const firstResult = nativeMetricRows[0].result;+assert.equal(firstResult.metadata.sastbench.status, "ok");+assert.equal(firstResult.metadata.sastbench.verdict, "confirmed");+assert.equal(firstResult.latencyMs, 1234);+assert.equal(firstResult.cost, 0.25);+assert.equal(firstResult.response, nativeContexts[0].result.response);+assert.match(+ negativeInvalidOutput.result.metadata.sastbench.error!,+ /triage-finding\/v0/,+);+assert.equal(+ positiveProviderError.result.metadata.sastbench.error,+ "provider failed",+);++console.log("sastbench result and native metric tests passed");diff --git a/evals/triage-finding/scripts/generate-calibration-tests.js b/evals/triage-finding/scripts/generate-calibration-tests.mtssimilarity index 56%rename from evals/triage-finding/scripts/generate-calibration-tests.jsrename to evals/triage-finding/scripts/generate-calibration-tests.mtsindex 1ac5f23e3..c2724e41c 100644--- a/evals/triage-finding/scripts/generate-calibration-tests.js+++ b/evals/triage-finding/scripts/generate-calibration-tests.mts@@ -1,21 +1,30 @@#!/usr/bin/env node-"use strict";--const fs = require("node:fs");-const path = require("node:path");--const DEFAULT_DATASET = path.join(__dirname, "..", "datasets", "triage-calibration-seed.json");-const DEFAULT_OUTPUT = path.join(__dirname, "..", "tests", "calibration-oss.yaml");-const DEFAULT_REPO_ROOT =- "evals/triage-finding/artifacts/calibration-repos";--function parseArgs(argv) {+import type { CalibrationCase, CalibrationVariant } from "../types.ts";++import fs from "node:fs";+import path from "node:path";++export const DEFAULT_DATASET = path.join(+ import.meta.dirname,+ "..",+ "datasets",+ "triage-calibration-seed.json",+);+const DEFAULT_OUTPUT = path.join(+ import.meta.dirname,+ "..",+ "tests",+ "calibration-oss.yaml",+);+const DEFAULT_REPO_ROOT = "evals/triage-finding/artifacts/calibration-repos";++function parseArgs(argv: string[]) {const args = {dataset: DEFAULT_DATASET,output: DEFAULT_OUTPUT,repoRoot: DEFAULT_REPO_ROOT,- caseId: null,- variantId: null,+ caseId: null as string | null,+ variantId: null as string | null,};for (let index = 0; index < argv.length; index += 1) {@@ -38,48 +47,35 @@ function parseArgs(argv) {return args;}-function quote(value) {+function quote(value: unknown) {return JSON.stringify(String(value));}-function scalar(value) {- return String(value).replace(/\r\n/g, "\n").replace(/\r/g, "\n");+function indentedBlock(value: unknown) {+ return ` ${String(value)+ .replace(/\r\n?/g, "\n")+ .replace(/\n/g, "\n ")}`;}-function indentedBlock(value, spaces) {- const indent = " ".repeat(spaces);- return scalar(value)- .split("\n")- .map((line) => `${indent}${line}`)- .join("\n");-}--function variantCaseId(testCase, variant) {- return `${testCase.case_id}-${variant.variant_id}`;-}--function inputId(testCase, variant) {- const base = testCase.finding.input_id || testCase.finding.input_id_base || testCase.case_id;+function inputId(testCase: CalibrationCase, variant: CalibrationVariant) {+ const base =+ testCase.finding.input_id ||+ testCase.finding.input_id_base ||+ testCase.case_id;return `${base}-${variant.variant_id}`;}-function targetRepoPath(repoRoot, testCase, variant) {- return path.posix.join(repoRoot, testCase.case_id, variant.variant_id);-}--function evidenceTerms(testCase, variant) {- const terms = [];- for (const location of testCase.finding.anchor_locations || []) {- terms.push(location.path);- }+function evidenceTerms(testCase: CalibrationCase, variant: CalibrationVariant) {+ const terms = (testCase.finding.anchor_locations || []).map(+ (location) => location.path,+ );if (variant.variant_id === "fixed" && testCase.finding.fix_patch_ref) {- const fixCommit = variant.checkout_ref;- terms.push(fixCommit);+ terms.push(variant.checkout_ref);}return [...new Set(terms)];}-function findingInput(testCase, variant) {+function findingInput(testCase: CalibrationCase, variant: CalibrationVariant) {const finding = testCase.finding;const lines = [`Source type: ${testCase.source_type}`,@@ -115,8 +111,12 @@ function findingInput(testCase, variant) {return lines.join("\n");}-function testYaml(testCase, variant, args) {- const generatedCaseId = variantCaseId(testCase, variant);+function testYaml(+ testCase: CalibrationCase,+ variant: CalibrationVariant,+ repoRoot: string,+) {+ const generatedCaseId = `${testCase.case_id}-${variant.variant_id}`;const terms = evidenceTerms(testCase, variant);const lines = [`- description: ${quote(`calibration ${variant.variant_id}: ${testCase.case_id}`)}`,@@ -129,7 +129,7 @@ function testYaml(testCase, variant, args) {` expected_binary_label: ${variant.expected_binary_label}`," vars:",` case_id: ${generatedCaseId}`,- ` target_repo: ${targetRepoPath(args.repoRoot, testCase, variant)}`,+ ` target_repo: ${path.posix.join(repoRoot, testCase.case_id, variant.variant_id)}`,` source_type_under_test: ${testCase.source_type}`,` expected_ids: ${inputId(testCase, variant)}`,` expected_source_types: ${testCase.source_type}`,@@ -137,24 +137,24 @@ function testYaml(testCase, variant, args) {` expected_binary_label: ${variant.expected_binary_label}`,` expected_evidence_terms: ${quote(terms.join(", "))}`," finding_input: |-",- indentedBlock(findingInput(testCase, variant), 6),+ indentedBlock(findingInput(testCase, variant))," eval_instructions: |-",indentedBlock(- [- "This is an automated OSS calibration eval. Do not ask follow-up questions.",- "Inspect only the supplied repository checkout, the named anchor locations, the fix evidence, and the smallest related static evidence needed for the verdict.",- "Do not spawn subagents, run tests, run builds, start applications, run exploit PoCs, modify files, or search for unrelated vulnerabilities.",- "Return the normal triage-finding result: concise Markdown plus exactly one fenced JSON block.",- 'The JSON block must conform to schema_version "triage-finding/v0" and include source_type, verdict, evidence, counterevidence, proof_gaps, boundary_assessment, and exploitability_stack_rank.',- ].join("\n"),- 6,+ `This is an automated OSS calibration eval. Do not ask follow-up questions.+Inspect only the supplied repository checkout, the named anchor locations, the fix evidence, and the smallest related static evidence needed for the verdict.+Do not spawn subagents, run tests, run builds, start applications, run exploit PoCs, modify files, or search for unrelated vulnerabilities.+Return the normal triage-finding result: concise Markdown plus exactly one fenced JSON block.+The JSON block must conform to schema_version "triage-finding/v0" and include source_type, verdict, evidence, counterevidence, proof_gaps, boundary_assessment, and exploitability_stack_rank.`,),];return lines.join("\n");}-function selectedVariants(dataset, args) {+export function selectedVariants(+ dataset: { cases: CalibrationCase[] },+ args: { caseId?: string | null; variantId?: string | null },+) {const variants = [];for (const testCase of dataset.cases) {if (args.caseId && testCase.case_id !== args.caseId) {@@ -169,40 +169,21 @@ function selectedVariants(dataset, args) {variants.push({ testCase, variant });}}- return variants;-}--function generateFromVariants(variants, args) {if (variants.length === 0) {throw new Error("No calibration variants matched the requested filters.");}-- const tests = variants.map(({ testCase, variant }) => testYaml(testCase, variant, args));- return `${tests.join("\n\n")}\n`;-}--function generate(dataset, args) {- return generateFromVariants(selectedVariants(dataset, args), args);+ return variants;}-function main() {+if (import.meta.filename === fs.realpathSync(process.argv[1])) {const args = parseArgs(process.argv.slice(2));const dataset = JSON.parse(fs.readFileSync(args.dataset, "utf8"));const variants = selectedVariants(dataset, args);- const output = generateFromVariants(variants, args);+ const tests = variants.map(({ testCase, variant }) =>+ testYaml(testCase, variant, args.repoRoot),+ );+ const output = `${tests.join("\n\n")}\n`;fs.mkdirSync(path.dirname(args.output), { recursive: true });fs.writeFileSync(args.output, output);console.log(`wrote ${variants.length} calibration tests to ${args.output}`);}--if (require.main === module) {- main();-}--module.exports = {- generate,- inputId,- selectedVariants,- targetRepoPath,- variantCaseId,-};diff --git a/evals/triage-finding/scripts/hydrate-calibration-repos.js b/evals/triage-finding/scripts/hydrate-calibration-repos.jsdeleted file mode 100644index d43ad48f4..000000000--- a/evals/triage-finding/scripts/hydrate-calibration-repos.js+++ /dev/null@@ -1,155 +0,0 @@-#!/usr/bin/env node-"use strict";--const { runGit } = require("../sastbench/scripts/hydrate-sastbench-repos");-const fs = require("node:fs");-const path = require("node:path");--const DEFAULT_DATASET = path.join(__dirname, "..", "datasets", "triage-calibration-seed.json");-const DEFAULT_REPO_ROOT = path.join(__dirname, "..", "artifacts", "calibration-repos");--function parseArgs(argv) {- const args = {- dataset: DEFAULT_DATASET,- repoRoot: DEFAULT_REPO_ROOT,- caseId: null,- variantId: null,- dryRun: false,- };-- for (let index = 0; index < argv.length; index += 1) {- const arg = argv[index];- if (arg === "--dataset") {- args.dataset = argv[++index];- } else if (arg === "--repo-root") {- args.repoRoot = argv[++index];- } else if (arg === "--case") {- args.caseId = argv[++index];- } else if (arg === "--variant") {- args.variantId = argv[++index];- } else if (arg === "--dry-run") {- args.dryRun = true;- } else {- throw new Error(`Unknown argument: ${arg}`);- }- }-- return {- ...args,- dataset: path.resolve(args.dataset),- repoRoot: path.resolve(args.repoRoot),- };-}--function plannedJobs(dataset, args) {- const jobs = [];-- for (const testCase of dataset.cases) {- if (args.caseId && testCase.case_id !== args.caseId) {- continue;- }-- for (const variant of testCase.variants) {- if (args.variantId && variant.variant_id !== args.variantId) {- continue;- }-- jobs.push({- caseId: testCase.case_id,- variantId: variant.variant_id,- repoUrl: testCase.repo.url,- checkoutRef: variant.checkout_ref,- targetDir: path.join(args.repoRoot, testCase.case_id, variant.variant_id),- });- }- }-- return jobs;-}--function gitOutput(args, cwd) {- try {- return runGit(args, cwd, "ignore").trim();- } catch {- return null;- }-}--function isGitCheckout(directory) {- return fs.existsSync(path.join(directory, ".git"));-}--function ensureGitCheckout(job) {- fs.mkdirSync(path.dirname(job.targetDir), { recursive: true });-- if (!fs.existsSync(job.targetDir)) {- fs.mkdirSync(job.targetDir, { recursive: true });- }-- if (!isGitCheckout(job.targetDir)) {- const entries = fs.readdirSync(job.targetDir);- if (entries.length > 0) {- throw new Error(`Refusing to hydrate into non-empty non-git directory: ${job.targetDir}`);- }- runGit(["init"], job.targetDir);- runGit(["remote", "add", "origin", job.repoUrl], job.targetDir);- } else {- const originUrl = gitOutput(["remote", "get-url", "origin"], job.targetDir);- if (!originUrl) {- runGit(["remote", "add", "origin", job.repoUrl], job.targetDir);- } else if (originUrl !== job.repoUrl) {- runGit(["remote", "set-url", "origin", job.repoUrl], job.targetDir);- }- }-- const currentHead = gitOutput(["rev-parse", "HEAD"], job.targetDir);- if (currentHead === job.checkoutRef) {- return "already current";- }-- runGit(["fetch", "--depth", "1", "origin", job.checkoutRef], job.targetDir);- runGit(["checkout", "--detach", "FETCH_HEAD"], job.targetDir);- return "hydrated";-}--function printPlan(jobs) {- const variantWord = jobs.length === 1 ? "variant" : "variants";- console.log(`would hydrate ${jobs.length} calibration ${variantWord}`);- for (const job of jobs) {- console.log(`${job.caseId}/${job.variantId} <- ${job.repoUrl} @ ${job.checkoutRef}`);- console.log(` ${job.targetDir}`);- }-}--function hydrate(jobs) {- const variantWord = jobs.length === 1 ? "variant" : "variants";- console.log(`hydrating ${jobs.length} calibration ${variantWord}`);- for (const job of jobs) {- const status = ensureGitCheckout(job);- console.log(`${status}: ${job.caseId}/${job.variantId} @ ${job.checkoutRef}`);- }-}--function main() {- const args = parseArgs(process.argv.slice(2));- const dataset = JSON.parse(fs.readFileSync(args.dataset, "utf8"));- const jobs = plannedJobs(dataset, args);-- if (jobs.length === 0) {- throw new Error("No calibration variants matched the requested filters.");- }-- if (args.dryRun) {- printPlan(jobs);- } else {- hydrate(jobs);- }-}--if (require.main === module) {- main();-}--module.exports = {- plannedJobs,-};diff --git a/evals/triage-finding/scripts/hydrate-calibration-repos.mts b/evals/triage-finding/scripts/hydrate-calibration-repos.mtsnew file mode 100644index 000000000..f76856daf--- /dev/null+++ b/evals/triage-finding/scripts/hydrate-calibration-repos.mts@@ -0,0 +1,111 @@+#!/usr/bin/env node+import { runGit } from "../sastbench/scripts/hydrate-sastbench-repos.mts";++import {+ DEFAULT_DATASET,+ selectedVariants,+} from "./generate-calibration-tests.mts";++import fs from "node:fs";+import path from "node:path";++const DEFAULT_REPO_ROOT = path.join(+ import.meta.dirname,+ "..",+ "artifacts",+ "calibration-repos",+);++function parseArgs(argv: string[]) {+ const args = {+ dataset: DEFAULT_DATASET,+ repoRoot: DEFAULT_REPO_ROOT,+ caseId: null as string | null,+ variantId: null as string | null,+ dryRun: false,+ };++ for (let index = 0; index < argv.length; index += 1) {+ const arg = argv[index];+ if (arg === "--dataset") {+ args.dataset = path.resolve(argv[++index]);+ } else if (arg === "--repo-root") {+ args.repoRoot = path.resolve(argv[++index]);+ } else if (arg === "--case") {+ args.caseId = argv[++index];+ } else if (arg === "--variant") {+ args.variantId = argv[++index];+ } else if (arg === "--dry-run") {+ args.dryRun = true;+ } else {+ throw new Error(`Unknown argument: ${arg}`);+ }+ }++ return args;+}++function gitOutput(args: string[], cwd: string) {+ try {+ return runGit(args, cwd, "ignore");+ } catch {+ return null;+ }+}++function ensureGitCheckout(job: (typeof jobs)[number]) {+ fs.mkdirSync(path.dirname(job.targetDir), { recursive: true });++ if (!fs.existsSync(job.targetDir)) {+ fs.mkdirSync(job.targetDir, { recursive: true });+ }++ if (!fs.existsSync(path.join(job.targetDir, ".git"))) {+ const entries = fs.readdirSync(job.targetDir);+ if (entries.length > 0) {+ throw new Error(+ `Refusing to hydrate into non-empty non-git directory: ${job.targetDir}`,+ );+ }+ runGit(["init"], job.targetDir);+ runGit(["remote", "add", "origin", job.repoUrl], job.targetDir);+ } else {+ const originUrl = gitOutput(["remote", "get-url", "origin"], job.targetDir);+ if (!originUrl) {+ runGit(["remote", "add", "origin", job.repoUrl], job.targetDir);+ } else if (originUrl !== job.repoUrl) {+ runGit(["remote", "set-url", "origin", job.repoUrl], job.targetDir);+ }+ }++ const currentHead = gitOutput(["rev-parse", "HEAD"], job.targetDir);+ if (currentHead === job.checkoutRef) {+ return "already current";+ }++ runGit(["fetch", "--depth", "1", "origin", job.checkoutRef], job.targetDir);+ runGit(["checkout", "--detach", "FETCH_HEAD"], job.targetDir);+ return "hydrated";+}++const args = parseArgs(process.argv.slice(2));+const dataset = JSON.parse(fs.readFileSync(args.dataset, "utf8"));+const jobs = selectedVariants(dataset, args).map(({ testCase, variant }) => ({+ repoUrl: testCase.repo.url,+ checkoutRef: variant.checkout_ref,+ targetDir: path.join(args.repoRoot, testCase.case_id, variant.variant_id),+ label: `${testCase.case_id}/${variant.variant_id}`,+}));+const variantWord = jobs.length === 1 ? "variant" : "variants";+console.log(+ `${args.dryRun ? "would hydrate" : "hydrating"} ${jobs.length} calibration ${variantWord}`,+);+for (const job of jobs) {+ if (args.dryRun) {+ console.log(`${job.label} <- ${job.repoUrl} @ ${job.checkoutRef}`);+ console.log(` ${job.targetDir}`);+ } else {+ const status = ensureGitCheckout(job);+ console.log(`${status}: ${job.label} @ ${job.checkoutRef}`);+ }+}diff --git a/evals/triage-finding/scripts/test-app-surface-instructions.js b/evals/triage-finding/scripts/test-app-surface-instructions.mtssimilarity index 70%rename from evals/triage-finding/scripts/test-app-surface-instructions.jsrename to evals/triage-finding/scripts/test-app-surface-instructions.mtsindex 0d51c5fe8..31e09b2ac 100644--- a/evals/triage-finding/scripts/test-app-surface-instructions.js+++ b/evals/triage-finding/scripts/test-app-surface-instructions.mts@@ -1,8 +1,15 @@-const assert = require("node:assert/strict");-const fs = require("node:fs");-const path = require("node:path");+import assert from "node:assert/strict";+import fs from "node:fs";+import path from "node:path";-const pluginRoot = path.resolve(__dirname, "..", "..", "..", "plugins", "codex-security");+const pluginRoot = path.resolve(+ import.meta.dirname,+ "..",+ "..",+ "..",+ "plugins",+ "codex-security",+);const skillRoot = path.join(pluginRoot, "skills", "triage-finding");const skillPath = path.join(skillRoot, "SKILL.md");const skill = fs.readFileSync(skillPath, "utf8");@@ -19,16 +26,30 @@ assert.match(ticketIntake, /Atlassian Rovo[\s\S]*JQL/);assert.match(ticketIntake, /natural-language search[\s\S]*discover[\s\S]*JQL/);assert.match(skill, /security or vulnerability Jira\/Linear tickets/);assert.match(skill, /Atlassian Rovo and Linear mentions\s+as connector hints/);-assert.match(skill, /not as a reason to switch to\s+Atlassian Rovo's `triage-issue` skill/);-assert.match(skill, /Do not run duplicate-bug triage instead of security-impact triage/);-assert.match(ticketIntake, /Normalize Jira and Linear vulnerability tickets as `source_type: "scanner_ticket"`/);-assert.match(ticketIntake, /issue key[\s\S]*URL[\s\S]*project[\s\S]*status[\s\S]*labels[\s\S]*components[\s\S]*priority/);+assert.match(+ skill,+ /not as a reason to switch to\s+Atlassian Rovo's `triage-issue` skill/,+);+assert.match(+ skill,+ /Do not run duplicate-bug triage instead of security-impact triage/,+);+assert.match(+ ticketIntake,+ /Normalize Jira and Linear vulnerability tickets as `source_type: "scanner_ticket"`/,+);+assert.match(+ ticketIntake,+ /issue key[\s\S]*URL[\s\S]*project[\s\S]*status[\s\S]*labels[\s\S]*components[\s\S]*priority/,+);assert.match(ticketIntake,/Default to read-only import and triage[\s\S]*Do not add comments, transition issues,\s+close issues, assign owners, or change labels/,);-assert.match(agent, /Import security or vulnerability tickets from Jira\/Linear, scanners, advisories, or GitHub/);-assert.match(agent, /security or vulnerability tickets/);+assert.match(+ agent,+ /Import security or vulnerability tickets from Jira\/Linear, scanners, advisories, or GitHub/,+);assert.match(agent, /import Jira issues matching <JQL or project\/search>/);assert.match(ticketIntake, /missing connector|connector.*unavailable/i);assert.match(ticketIntake, /authentication|reauthorize/i);@@ -49,6 +70,12 @@ assert.match(ticketIntake, /deterministic[\s\S]*tree order/i);assert.match(ticketIntake, /250[\s\S]*do not truncate/i);assert.equal(plugin.interface.defaultPrompt.length, 3);assert(- plugin.interface.defaultPrompt.every((prompt) => [...prompt].length <= 128),+ plugin.interface.defaultPrompt.every(+ (prompt: string) => [...prompt].length <= 128,+ ),+);+assert(+ plugin.interface.defaultPrompt.includes(+ "Triage existing security findings against this repository.",+ ),);-assert(plugin.interface.defaultPrompt.includes("Triage existing security findings against this repository."));diff --git a/evals/triage-finding/scripts/test-calibration-dataset.js b/evals/triage-finding/scripts/test-calibration-dataset.jsdeleted file mode 100644index fd55bd907..000000000--- a/evals/triage-finding/scripts/test-calibration-dataset.js+++ /dev/null@@ -1,97 +0,0 @@-#!/usr/bin/env node-"use strict";--const assert = require("node:assert/strict");-const fs = require("node:fs");-const path = require("node:path");--const datasetPath = path.join(__dirname, "..", "datasets", "triage-calibration-seed.json");-const dataset = JSON.parse(fs.readFileSync(datasetPath, "utf8"));--assert.equal(dataset.schema_version, "triage-finding-calibration-dataset/v0");-assert.equal(dataset.name, "triage-finding-calibration-seed");-assert.ok(Array.isArray(dataset.static_constraints));-assert.ok(Array.isArray(dataset.scoring_targets));-assert.ok(dataset.shared_evidence_sets && typeof dataset.shared_evidence_sets === "object");-assert.ok(Array.isArray(dataset.cases));--const caseIds = new Set();-const evidenceSetIds = new Set(Object.keys(dataset.shared_evidence_sets));-let variantCount = 0;-let confirmedCount = 0;-let notActionableCount = 0;-let ossPairCount = 0;--for (const testCase of dataset.cases) {- assert.ok(testCase.case_id, "case_id is required");- assert.ok(!caseIds.has(testCase.case_id), `duplicate case_id: ${testCase.case_id}`);- caseIds.add(testCase.case_id);-- assert.ok(testCase.case_family, `${testCase.case_id}: case_family is required`);- assert.equal(- testCase.case_family,- "oss-vuln-fix-pair",- `${testCase.case_id}: calibration seed must stay OSS-only`,- );- assert.ok(testCase.source_type, `${testCase.case_id}: source_type is required`);- assert.ok(testCase.repo?.name, `${testCase.case_id}: repo.name is required`);- assert.ok(testCase.finding?.title, `${testCase.case_id}: finding.title is required`);- assert.ok(testCase.gold?.basis, `${testCase.case_id}: gold.basis is required`);- assert.ok(Array.isArray(testCase.variants), `${testCase.case_id}: variants must be an array`);- assert.ok(testCase.variants.length > 0, `${testCase.case_id}: must have at least one variant`);-- if (testCase.case_family === "oss-vuln-fix-pair") {- ossPairCount += 1;- const labels = testCase.variants.map((variant) => variant.expected_binary_label).sort();- assert.deepEqual(labels, ["negative", "positive"], `${testCase.case_id}: OSS pairs need positive and negative variants`);- }-- const variantIds = new Set();- for (const variant of testCase.variants) {- variantCount += 1;- assert.ok(variant.variant_id, `${testCase.case_id}: variant_id is required`);- assert.ok(!variantIds.has(variant.variant_id), `${testCase.case_id}: duplicate variant_id ${variant.variant_id}`);- variantIds.add(variant.variant_id);- assert.ok(variant.checkout_ref, `${testCase.case_id}/${variant.variant_id}: checkout_ref is required`);- for (const evidenceSetId of variant.required_evidence_set_ids || []) {- assert.ok(- evidenceSetIds.has(evidenceSetId),- `${testCase.case_id}/${variant.variant_id}: unknown required_evidence_set_id ${evidenceSetId}`,- );- }- assert.ok(- ["confirmed", "not_actionable", "needs_review"].includes(variant.expected_verdict),- `${testCase.case_id}/${variant.variant_id}: invalid expected_verdict`,- );- assert.ok(- ["positive", "negative", "review"].includes(variant.expected_binary_label),- `${testCase.case_id}/${variant.variant_id}: invalid expected_binary_label`,- );-- if (variant.expected_verdict === "confirmed") {- confirmedCount += 1;- assert.equal(- variant.expected_binary_label,- "positive",- `${testCase.case_id}/${variant.variant_id}: confirmed variants must be positive`,- );- } else if (variant.expected_verdict === "not_actionable") {- notActionableCount += 1;- assert.equal(- variant.expected_binary_label,- "negative",- `${testCase.case_id}/${variant.variant_id}: not_actionable variants must be negative`,- );- }- }-}--assert.equal(dataset.case_count, dataset.cases.length, "case_count does not match cases.length");-assert.equal(dataset.variant_count, variantCount, "variant_count does not match variants.length sum");-assert.ok(confirmedCount > 0, "dataset needs at least one confirmed variant");-assert.ok(notActionableCount > 0, "dataset needs at least one not_actionable variant");-assert.ok(ossPairCount >= 1, "dataset needs at least one OSS vulnerable/fixed pair");--console.log(- `calibration dataset ok: ${dataset.cases.length} cases, ${variantCount} variants, ${confirmedCount} confirmed, ${notActionableCount} not_actionable`,-);diff --git a/evals/triage-finding/scripts/test-calibration-dataset.mts b/evals/triage-finding/scripts/test-calibration-dataset.mtsnew file mode 100644index 000000000..2cddd1177--- /dev/null+++ b/evals/triage-finding/scripts/test-calibration-dataset.mts@@ -0,0 +1,145 @@+#!/usr/bin/env node++import assert from "node:assert/strict";+import fs from "node:fs";+import path from "node:path";++const datasetPath = path.join(+ import.meta.dirname,+ "..",+ "datasets",+ "triage-calibration-seed.json",+);+const dataset = JSON.parse(fs.readFileSync(datasetPath, "utf8"));++assert.equal(dataset.schema_version, "triage-finding-calibration-dataset/v0");+assert.equal(dataset.name, "triage-finding-calibration-seed");+assert.ok(Array.isArray(dataset.static_constraints));+assert.ok(Array.isArray(dataset.scoring_targets));+assert.ok(+ dataset.shared_evidence_sets &&+ typeof dataset.shared_evidence_sets === "object",+);+assert.ok(Array.isArray(dataset.cases));++const caseIds = new Set();+const evidenceSetIds = new Set(Object.keys(dataset.shared_evidence_sets));+let confirmedCount = 0;+let notActionableCount = 0;++for (const testCase of dataset.cases) {+ assert.ok(testCase.case_id, "case_id is required");+ assert.ok(+ !caseIds.has(testCase.case_id),+ `duplicate case_id: ${testCase.case_id}`,+ );+ caseIds.add(testCase.case_id);++ assert.ok(+ testCase.case_family,+ `${testCase.case_id}: case_family is required`,+ );+ assert.equal(+ testCase.case_family,+ "oss-vuln-fix-pair",+ `${testCase.case_id}: calibration seed must stay OSS-only`,+ );+ assert.ok(+ testCase.source_type,+ `${testCase.case_id}: source_type is required`,+ );+ assert.ok(testCase.repo?.name, `${testCase.case_id}: repo.name is required`);+ assert.ok(+ testCase.finding?.title,+ `${testCase.case_id}: finding.title is required`,+ );+ assert.ok(+ testCase.gold?.basis,+ `${testCase.case_id}: gold.basis is required`,+ );+ assert.ok(+ Array.isArray(testCase.variants),+ `${testCase.case_id}: variants must be an array`,+ );+ assert.ok(+ testCase.variants.length > 0,+ `${testCase.case_id}: must have at least one variant`,+ );++ const labels = testCase.variants+ .map(+ (variant: import("../types.ts").CalibrationVariant) =>+ variant.expected_binary_label,+ )+ .sort();+ assert.deepEqual(+ labels,+ ["negative", "positive"],+ `${testCase.case_id}: OSS pairs need positive and negative variants`,+ );++ const variantIds = new Set();+ for (const variant of testCase.variants) {+ assert.ok(+ variant.variant_id,+ `${testCase.case_id}: variant_id is required`,+ );+ assert.ok(+ !variantIds.has(variant.variant_id),+ `${testCase.case_id}: duplicate variant_id ${variant.variant_id}`,+ );+ variantIds.add(variant.variant_id);+ assert.ok(+ variant.checkout_ref,+ `${testCase.case_id}/${variant.variant_id}: checkout_ref is required`,+ );+ for (const evidenceSetId of variant.required_evidence_set_ids || []) {+ assert.ok(+ evidenceSetIds.has(evidenceSetId),+ `${testCase.case_id}/${variant.variant_id}: unknown required_evidence_set_id ${evidenceSetId}`,+ );+ }+ assert.ok(+ ["confirmed", "not_actionable", "needs_review"].includes(+ variant.expected_verdict,+ ),+ `${testCase.case_id}/${variant.variant_id}: invalid expected_verdict`,+ );++ if (variant.expected_verdict === "confirmed") {+ confirmedCount += 1;+ assert.equal(+ variant.expected_binary_label,+ "positive",+ `${testCase.case_id}/${variant.variant_id}: confirmed variants must be positive`,+ );+ } else if (variant.expected_verdict === "not_actionable") {+ notActionableCount += 1;+ assert.equal(+ variant.expected_binary_label,+ "negative",+ `${testCase.case_id}/${variant.variant_id}: not_actionable variants must be negative`,+ );+ }+ }+}++assert.equal(+ dataset.case_count,+ dataset.cases.length,+ "case_count does not match cases.length",+);+assert.equal(+ dataset.variant_count,+ dataset.cases.length * 2,+ "variant_count does not match variants.length sum",+);+assert.ok(confirmedCount > 0, "dataset needs at least one confirmed variant");+assert.ok(+ notActionableCount > 0,+ "dataset needs at least one not_actionable variant",+);++console.log(+ `calibration dataset ok: ${dataset.cases.length} cases, ${dataset.variant_count} variants, ${confirmedCount} confirmed, ${notActionableCount} not_actionable`,+);diff --git a/evals/triage-finding/scripts/test-calibration-evidence.js b/evals/triage-finding/scripts/test-calibration-evidence.mtssimilarity index 70%rename from evals/triage-finding/scripts/test-calibration-evidence.jsrename to evals/triage-finding/scripts/test-calibration-evidence.mtsindex a5c80b6cf..ad6c0403f 100644--- a/evals/triage-finding/scripts/test-calibration-evidence.js+++ b/evals/triage-finding/scripts/test-calibration-evidence.mts@@ -1,8 +1,6 @@#!/usr/bin/env node-"use strict";--const assert = require("node:assert/strict");-const calibrationEvidence = require("../assertions/calibration-evidence.js");+import assert from "node:assert/strict";+import calibrationEvidence from "../assertions/calibration-evidence.mts";const context = {vars: {@@ -17,7 +15,10 @@ const passing = calibrationEvidence(assert.equal(passing.pass, true, passing.reason);assert.equal(passing.score, 1);-const failing = calibrationEvidence("The finding is affected. Evidence includes src/example.js.", context);+const failing = calibrationEvidence(+ "The finding is affected. Evidence includes src/example.js.",+ context,+);assert.equal(failing.pass, false, "missing evidence terms should fail");assert.match(failing.reason, /abc123/);assert.ok(failing.score > 0);diff --git a/evals/triage-finding/scripts/test-generate-calibration-tests.js b/evals/triage-finding/scripts/test-generate-calibration-tests.jsdeleted file mode 100644index a70d34fa2..000000000--- a/evals/triage-finding/scripts/test-generate-calibration-tests.js+++ /dev/null@@ -1,63 +0,0 @@-#!/usr/bin/env node-"use strict";--const assert = require("node:assert/strict");-const { execFileSync } = require("node:child_process");-const fs = require("node:fs");-const os = require("node:os");-const path = require("node:path");--const evalDir = path.resolve(__dirname, "..");-const generator = path.join(evalDir, "scripts", "generate-calibration-tests.js");-const dataset = path.join(evalDir, "datasets", "triage-calibration-seed.json");-const trackedTests = path.join(evalDir, "tests", "calibration-oss.yaml");--const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "triage-calibration-tests-"));-const generated = path.join(tmpDir, "calibration-oss.yaml");-const generatedSmoke = path.join(tmpDir, "calibration-smoke.yaml");--execFileSync(process.execPath, [generator, "--dataset", dataset, "--output", generated], {- cwd: evalDir,- stdio: "pipe",-});--const generatedYaml = fs.readFileSync(generated, "utf8");-const trackedYaml = fs.readFileSync(trackedTests, "utf8");--assert.equal(generatedYaml, trackedYaml, "tracked calibration tests are stale; run calibration:generate");-assert.match(generatedYaml, /case_id: oss-mantisbt-ghsa-73vx-49mv-v8w5-vulnerable/);-assert.match(generatedYaml, /case_id: oss-mantisbt-ghsa-73vx-49mv-v8w5-fixed/);-assert.match(generatedYaml, /expected_verdicts: confirmed/);-assert.match(generatedYaml, /expected_verdicts: not_actionable/);-assert.match(generatedYaml, /target_repo: evals\/triage-finding\/artifacts\/calibration-repos\//);-assert.doesNotMatch(- generatedYaml,- /expected_evidence_terms:\n\s+- /,- "Promptfoo expands array-valued vars into extra test cases; evidence terms must be a scalar",-);--execFileSync(- process.execPath,- [- generator,- "--dataset",- dataset,- "--output",- generatedSmoke,- "--case",- "oss-dompurify-ghsa-v8jm-5vwx-cfxm",- "--variant",- "vulnerable",- ],- {- cwd: evalDir,- stdio: "pipe",- },-);--const smokeYaml = fs.readFileSync(generatedSmoke, "utf8");-assert.equal((smokeYaml.match(/^- description:/gm) || []).length, 1);-assert.match(smokeYaml, /case_id: oss-dompurify-ghsa-v8jm-5vwx-cfxm-vulnerable/);-assert.doesNotMatch(smokeYaml, /case_id: oss-dompurify-ghsa-v8jm-5vwx-cfxm-fixed/);--console.log("calibration test generation matches tracked YAML and supports filtered smoke output");diff --git a/evals/triage-finding/scripts/test-generate-calibration-tests.mts b/evals/triage-finding/scripts/test-generate-calibration-tests.mtsnew file mode 100644index 000000000..f512906b6--- /dev/null+++ b/evals/triage-finding/scripts/test-generate-calibration-tests.mts@@ -0,0 +1,98 @@+#!/usr/bin/env node++import assert from "node:assert/strict";+import { execFileSync } from "node:child_process";+import fs from "node:fs";+import os from "node:os";+import path from "node:path";++const evalDir = path.resolve(import.meta.dirname, "..");+const generator = path.join(+ evalDir,+ "scripts",+ "generate-calibration-tests.mts",+);+const dataset = path.join(evalDir, "datasets", "triage-calibration-seed.json");+const trackedTests = path.join(evalDir, "tests", "calibration-oss.yaml");++const tmpDir = fs.mkdtempSync(+ path.join(os.tmpdir(), "triage-calibration-tests-"),+);+const generated = path.join(tmpDir, "calibration-oss.yaml");+const generatedSmoke = path.join(tmpDir, "calibration-smoke.yaml");++execFileSync(+ process.execPath,+ [+ "--experimental-strip-types",+ generator,+ "--dataset",+ dataset,+ "--output",+ generated,+ ],+ {+ cwd: evalDir,+ stdio: "pipe",+ },+);++const generatedYaml = fs.readFileSync(generated, "utf8");+const trackedYaml = fs.readFileSync(trackedTests, "utf8");++assert.equal(+ generatedYaml,+ trackedYaml,+ "tracked calibration tests are stale; run calibration:generate",+);+assert.match(+ generatedYaml,+ /case_id: oss-mantisbt-ghsa-73vx-49mv-v8w5-vulnerable/,+);+assert.match(generatedYaml, /case_id: oss-mantisbt-ghsa-73vx-49mv-v8w5-fixed/);+assert.match(generatedYaml, /expected_verdicts: confirmed/);+assert.match(generatedYaml, /expected_verdicts: not_actionable/);+assert.match(+ generatedYaml,+ /target_repo: evals\/triage-finding\/artifacts\/calibration-repos\//,+);+assert.doesNotMatch(+ generatedYaml,+ /expected_evidence_terms:\n\s+- /,+ "Promptfoo expands array-valued vars into extra test cases; evidence terms must be a scalar",+);++execFileSync(+ process.execPath,+ [+ "--experimental-strip-types",+ generator,+ "--dataset",+ dataset,+ "--output",+ generatedSmoke,+ "--case",+ "oss-dompurify-ghsa-v8jm-5vwx-cfxm",+ "--variant",+ "vulnerable",+ ],+ {+ cwd: evalDir,+ stdio: "pipe",+ },+);++const smokeYaml = fs.readFileSync(generatedSmoke, "utf8");+assert.equal((smokeYaml.match(/^- description:/gm) || []).length, 1);+assert.match(+ smokeYaml,+ /case_id: oss-dompurify-ghsa-v8jm-5vwx-cfxm-vulnerable/,+);+assert.doesNotMatch(+ smokeYaml,+ /case_id: oss-dompurify-ghsa-v8jm-5vwx-cfxm-fixed/,+);++console.log(+ "calibration test generation matches tracked YAML and supports filtered smoke output",+);diff --git a/evals/triage-finding/scripts/test-hydrate-calibration-repos.js b/evals/triage-finding/scripts/test-hydrate-calibration-repos.jsdeleted file mode 100644index e31e3140d..000000000--- a/evals/triage-finding/scripts/test-hydrate-calibration-repos.js+++ /dev/null@@ -1,29 +0,0 @@-#!/usr/bin/env node-"use strict";--const assert = require("node:assert/strict");-const childProcess = require("node:child_process");-const path = require("node:path");--const evalDir = path.join(__dirname, "..");-const scriptPath = path.join(evalDir, "scripts", "hydrate-calibration-repos.js");--function runHydrator(args) {- return childProcess.execFileSync(process.execPath, [scriptPath, ...args], {- cwd: evalDir,- encoding: "utf8",- });-}--const allOutput = runHydrator(["--dry-run"]);-assert.match(allOutput, /would hydrate 16 calibration variants/);-assert.match(allOutput, /oss-mantisbt-ghsa-73vx-49mv-v8w5\/vulnerable/);-assert.match(allOutput, /https:\/\/github\.com\/mantisbt\/mantisbt/);-assert.match(allOutput, /80990f43153167c73f11eb4b2bc7108d0c3d6b46/);--const filteredOutput = runHydrator(["--dry-run", "--case", "oss-mantisbt-ghsa-73vx-49mv-v8w5", "--variant", "fixed"]);-assert.match(filteredOutput, /would hydrate 1 calibration variant/);-assert.match(filteredOutput, /oss-mantisbt-ghsa-73vx-49mv-v8w5\/fixed/);-assert.doesNotMatch(filteredOutput, /oss-mantisbt-ghsa-73vx-49mv-v8w5\/vulnerable/);--console.log("calibration hydration dry-run tests passed");diff --git a/evals/triage-finding/scripts/test-hydrate-calibration-repos.mts b/evals/triage-finding/scripts/test-hydrate-calibration-repos.mtsnew file mode 100644index 000000000..5393ca3ef--- /dev/null+++ b/evals/triage-finding/scripts/test-hydrate-calibration-repos.mts@@ -0,0 +1,44 @@+#!/usr/bin/env node+import assert from "node:assert/strict";+import childProcess from "node:child_process";+import path from "node:path";++const evalDir = path.join(import.meta.dirname, "..");+const scriptPath = path.join(+ evalDir,+ "scripts",+ "hydrate-calibration-repos.mts",+);++function runHydrator(args: string[]) {+ return childProcess.execFileSync(+ process.execPath,+ ["--experimental-strip-types", scriptPath, ...args],+ {+ cwd: evalDir,+ encoding: "utf8",+ },+ );+}++const allOutput = runHydrator(["--dry-run"]);+assert.match(allOutput, /would hydrate 16 calibration variants/);+assert.match(allOutput, /oss-mantisbt-ghsa-73vx-49mv-v8w5\/vulnerable/);+assert.match(allOutput, /https:\/\/github\.com\/mantisbt\/mantisbt/);+assert.match(allOutput, /80990f43153167c73f11eb4b2bc7108d0c3d6b46/);++const filteredOutput = runHydrator([+ "--dry-run",+ "--case",+ "oss-mantisbt-ghsa-73vx-49mv-v8w5",+ "--variant",+ "fixed",+]);+assert.match(filteredOutput, /would hydrate 1 calibration variant/);+assert.match(filteredOutput, /oss-mantisbt-ghsa-73vx-49mv-v8w5\/fixed/);+assert.doesNotMatch(+ filteredOutput,+ /oss-mantisbt-ghsa-73vx-49mv-v8w5\/vulnerable/,+);++console.log("calibration hydration dry-run tests passed");diff --git a/evals/triage-finding/scripts/test-triage-io.js b/evals/triage-finding/scripts/test-triage-io.mtssimilarity index 55%rename from evals/triage-finding/scripts/test-triage-io.jsrename to evals/triage-finding/scripts/test-triage-io.mtsindex 18ed75cf1..87c8d8ba0 100644--- a/evals/triage-finding/scripts/test-triage-io.js+++ b/evals/triage-finding/scripts/test-triage-io.mts@@ -1,18 +1,13 @@#!/usr/bin/env node-"use strict";+import assert from "assert";+import triageIo from "../assertions/triage-io.mts";-const assert = require("assert");-const triageIo = require("../assertions/triage-io.js");--assert.throws(() => triageIo("no json", {}), {- message: "Could not find a parseable triage-finding/v0 JSON block.",-});--function outputFor({ inputId, sourceType, verdict }) {+function outputFor({ inputId, verdict }: { inputId: string; verdict: string }) {const stackRank = {rank_queue: verdict === "not_actionable" ? null : verdict,rank: verdict === "not_actionable" ? null : 1,- rationale: verdict === "not_actionable" ? "not actionable" : "Example ranking",+ rationale:+ verdict === "not_actionable" ? "not actionable" : "Example ranking",drivers: [],};return `\`\`\`json@@ -45,66 +40,86 @@ function outputFor({ inputId, sourceType, verdict }) {\`\`\``;}-function baseContext({ caseId, inputId, sourceType, expectedVerdict, expectedBinaryLabel }) {- return {- vars: {- case_id: caseId,- expected_ids: inputId,- expected_source_types: sourceType,- expected_verdicts: expectedVerdict,- expected_binary_label: expectedBinaryLabel,- },- };-}--function assertPasses(name, output, context) {+function assertPasses(+ name: string,+ output: unknown,+ context: import("../types.ts").AssertionContext,+) {const result = triageIo(output, context);assert.equal(result.pass, true, `${name}: ${result.reason}`);}-function assertFails(name, output, context, expectedReason) {+function assertFails(+ name: string,+ output: unknown,+ context: import("../types.ts").AssertionContext,+ expectedReason: RegExp,+) {const result = triageIo(output, context);assert.equal(result.pass, false, `${name}: expected assertion to fail`);- assert.match(result.reason, expectedReason, `${name}: unexpected failure reason`);+ assert.match(+ result.reason,+ expectedReason,+ `${name}: unexpected failure reason`,+ );}const sourceType = "cve";assertPasses("vulnerable scanbench cases map to confirmed/positive",- outputFor({ inputId: "GHSA-example-000-vulnerable", sourceType, verdict: "confirmed" }),- baseContext({- caseId: "ghsa-example-vulnerable",+ outputFor({inputId: "GHSA-example-000-vulnerable",- sourceType,- expectedVerdict: "confirmed",- expectedBinaryLabel: "positive",+ verdict: "confirmed",}),+ {+ vars: {+ case_id: "ghsa-example-vulnerable",+ expected_ids: "GHSA-example-000-vulnerable",+ expected_source_types: sourceType,+ expected_verdicts: "confirmed",+ expected_binary_label: "positive",+ },+ },);assertPasses("fixed scanbench cases map to not_actionable/negative",- outputFor({ inputId: "GHSA-example-000-fixed", sourceType, verdict: "not_actionable" }),- baseContext({- caseId: "ghsa-example-fixed",+ outputFor({inputId: "GHSA-example-000-fixed",- sourceType,- expectedVerdict: "not_actionable",- expectedBinaryLabel: "negative",+ verdict: "not_actionable",}),+ {+ vars: {+ case_id: "ghsa-example-fixed",+ expected_ids: "GHSA-example-000-fixed",+ expected_source_types: sourceType,+ expected_verdicts: "not_actionable",+ expected_binary_label: "negative",+ },+ },);assertFails("fixed scanbench cases cannot be labeled confirmed",- outputFor({ inputId: "GHSA-example-000-fixed", sourceType, verdict: "confirmed" }),- baseContext({- caseId: "ghsa-example-fixed",+ outputFor({inputId: "GHSA-example-000-fixed",- sourceType,- expectedVerdict: "confirmed",- expectedBinaryLabel: "positive",+ verdict: "confirmed",}),+ {+ vars: {+ case_id: "ghsa-example-fixed",+ expected_ids: "GHSA-example-000-fixed",+ expected_source_types: sourceType,+ expected_verdicts: "confirmed",+ expected_binary_label: "positive",+ },+ },/fixed.*not_actionable|negative/,);+assert.throws(() => triageIo("no json", { vars: {} }), {+ message: "Could not find a parseable triage-finding/v0 JSON block.",+});+console.log("triage-io assertion tests passed");diff --git a/evals/triage-finding/tests/github-rest-intake.yaml b/evals/triage-finding/tests/github-rest-intake.yamlindex 6b6f63b82..0b0e4bd5a 100644--- a/evals/triage-finding/tests/github-rest-intake.yaml+++ b/evals/triage-finding/tests/github-rest-intake.yaml@@ -17,7 +17,7 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/github-rest-intake.js+ value: file://./assertions/github-rest-intake.mts- description: "GitHub source infers attached Codex project repository"metadata:@@ -40,7 +40,7 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/github-rest-intake.js+ value: file://./assertions/github-rest-intake.mts- description: "GitHub Dependabot malware uses REST malware classification"metadata:@@ -61,7 +61,7 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/github-rest-intake.js+ value: file://./assertions/github-rest-intake.mts- description: "GitHub code scanning uses REST alerts and instances"metadata:@@ -82,7 +82,7 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/github-rest-intake.js+ value: file://./assertions/github-rest-intake.mts- description: "GitHub advisories include private report triage state"metadata:@@ -103,7 +103,7 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/github-rest-intake.js+ value: file://./assertions/github-rest-intake.mts- description: "GitHub Connector request still uses REST-only finding retrieval"metadata:@@ -124,7 +124,7 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/github-rest-intake.js+ value: file://./assertions/github-rest-intake.mts- description: "Explicit GitHub Issue is supported outside all-source intake"metadata:@@ -145,4 +145,4 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/github-rest-intake.js+ value: file://./assertions/github-rest-intake.mtsdiff --git a/evals/triage-finding/tests/invocation-behavior.yaml b/evals/triage-finding/tests/invocation-behavior.yamlindex 6526d1978..4296f0629 100644--- a/evals/triage-finding/tests/invocation-behavior.yaml+++ b/evals/triage-finding/tests/invocation-behavior.yaml@@ -15,4 +15,4 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/missing-input.js+ value: file://./assertions/missing-input.mtsdiff --git a/evals/triage-finding/tests/ticket-intake.yaml b/evals/triage-finding/tests/ticket-intake.yamlindex 2a8fd4b2f..ef24dc541 100644--- a/evals/triage-finding/tests/ticket-intake.yaml+++ b/evals/triage-finding/tests/ticket-intake.yaml@@ -17,7 +17,7 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/ticket-intake.js+ value: file://./assertions/ticket-intake.mts- description: "Linear permission failure stops with access guidance"metadata:@@ -38,7 +38,7 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/ticket-intake.js+ value: file://./assertions/ticket-intake.mts- description: "Missing Linear issue stops with identifier guidance"metadata:@@ -59,7 +59,7 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/ticket-intake.js+ value: file://./assertions/ticket-intake.mts- description: "Repeated transient Linear failure stops after one retry"metadata:@@ -81,7 +81,7 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/ticket-intake.js+ value: file://./assertions/ticket-intake.mts- description: "Linear parent asks before importing direct sub-issues"metadata:@@ -102,7 +102,7 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/ticket-intake.js+ value: file://./assertions/ticket-intake.mts- description: "Linear nested sub-issues ask again at the next depth"metadata:@@ -123,7 +123,7 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/ticket-intake.js+ value: file://./assertions/ticket-intake.mts- description: "Ambiguous Linear parent asks whether it is a finding"metadata:@@ -144,7 +144,7 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/ticket-intake.js+ value: file://./assertions/ticket-intake.mts- description: "Linear issue tree over 250 stops for narrowing"metadata:@@ -164,4 +164,4 @@disableDefaultAsserts: trueassert:- type: javascript- value: file://./assertions/ticket-intake.js+ value: file://./assertions/ticket-intake.mtsdiff --git a/evals/triage-finding/types.ts b/evals/triage-finding/types.tsnew file mode 100644index 000000000..c5ed7a0a7--- /dev/null+++ b/evals/triage-finding/types.ts@@ -0,0 +1,102 @@+export interface AssertionContext {+ vars: Record<string, unknown>;+ test?: PromptfooTest;+}++export interface PromptfooTest {+ vars?: Record<string, unknown>;+ metadata?: Record<string, unknown>;+}++export interface PromptfooRow {+ vars?: Record<string, unknown>;+ latencyMs?: number;+ cost?: number;+ response?: {+ output?: unknown;+ error?: unknown;+ tokenUsage?: { total?: number; [key: string]: unknown };+ sessionId?: string;+ };+ tokenUsage?: { total?: number; [key: string]: unknown };+ error?: unknown;+ failureReason?: unknown;+ namedScores?: Record<string, number>;+ metadata?: Record<string, unknown>;+}++export interface TriageFinding {+ input_id: string;+ source_type: string;+ verdict: string;+ exploitability_stack_rank?: {+ rank: number | null;+ rank_queue: string | null;+ };+ fix_finding_handoff?: string | null;+ [key: string]: unknown;+}++export interface TriageResult {+ schema_version: string;+ findings: TriageFinding[];+}++export interface CalibrationVariant {+ variant_id: string;+ checkout_ref: string;+ expected_verdict: string;+ expected_binary_label: string;+}++export interface CalibrationCase {+ case_id: string;+ source_type: string;+ repo: { name: string; url: string };+ finding: {+ input_id?: string;+ input_id_base?: string;+ title: string;+ advisory_ids?: string[];+ weakness?: string;+ severity?: string;+ language?: string;+ anchor_locations?: { path: string; line: number }[];+ fix_patch_ref?: string;+ };+ variants: CalibrationVariant[];+}++export interface SastLocation {+ file: string;+ line_start: number;+ line_end: number;+ function?: string;+}++export interface SastBenchRecord {+ repo_name: string;+ repo_url: string;+ commit_hash: string;+ ground_truth: string;+ to_analyzer: {+ vulnerability_type: string;+ vulnerability_name: string;+ description: string;+ locations: SastLocation[];+ commit_context: { repo: string; commit: string };+ [key: string]: unknown;+ };+ metadata: { cwe_id?: string; languages?: string[]; [key: string]: unknown };+}++export interface DatasetExpectations {+ caseCount: number;+ labelCounts: Record<string, number>;+}++export interface SampleSpec {+ profile: string;+ seed: string;+ labelCounts: Record<string, number>;+}diff --git a/evals/tsconfig.json b/evals/tsconfig.jsonnew file mode 100644index 000000000..0aed99eab--- /dev/null+++ b/evals/tsconfig.json@@ -0,0 +1,14 @@+{+ "extends": "../plugins/codex-security/mcp-app/tsconfig.json",+ "compilerOptions": {+ "typeRoots": ["../sdk/typescript/node_modules/@types"]+ },+ "include": [+ "deep-reducer/*.mts",+ "secret-discovery/*.mts",+ "triage-finding/assertions/*.mts",+ "triage-finding/scripts/*.mts",+ "triage-finding/sastbench/assertions/*.mts",+ "triage-finding/sastbench/scripts/*.mts"+ ]+}diff --git a/plugins/codex-security/mcp-app/TESTING.md b/plugins/codex-security/mcp-app/TESTING.mdindex a79b05197..e0283c7d1 100644--- a/plugins/codex-security/mcp-app/TESTING.md+++ b/plugins/codex-security/mcp-app/TESTING.md@@ -2,12 +2,14 @@From `sdk/typescript`, install both packages' dependencies, prepare the [universal native payload](../native/README.md#package-inputs), and run `pnpm run test:mcp`. That command builds the bundled plugin before testing it. To rerun only the tests after a build, run `pnpm run test:mcp` from this directory.-Node's test runner discovers `tests/test_*.mjs` and runs at most two files at once, each in a separate process. Assertions within a file remain sequential. Keep shared helpers outside that filename pattern. A failing script fails the command. The reporter streams Node's TAP output and then tries to write Node's JUnit report to `reports/junit.xml`; an unavailable report path warns without changing the test result. CI uploads that file with the Node 22 reports.+`pnpm run typecheck` checks the TypeScript tests and tooling. `pnpm run test:mcp` runs the sources directly with Node's `--experimental-strip-types` flag, including on Node 22.13.++Node's test runner discovers `tests/test_*.ts` and runs at most two files at once, each in a separate process. Assertions within a file remain sequential. Write new tests in TypeScript and keep shared helpers outside that filename pattern. A failing script fails the command. The reporter streams Node's TAP output and then tries to write Node's JUnit report to `reports/junit.xml`; an unavailable report path warns without changing the test result. CI uploads that file with the Node 22 reports.For a serial comparison, run:```sh-node --test --test-concurrency=1 "tests/test_*.mjs"+node --experimental-strip-types --test --test-concurrency=1 "tests/test_*.ts"```Use the existing injected clocks for retry policy tests. Keep real timers, subprocesses, SQLite files, and isolated temporary directories in the lifecycle, locking, permissions, and stdio tests. Do not share writable fixtures between test files or rebuild the bundled plugin while another test is reading it.diff --git a/plugins/codex-security/mcp-app/package.json b/plugins/codex-security/mcp-app/package.jsonindex c81819b75..7d10e6b86 100644--- a/plugins/codex-security/mcp-app/package.json+++ b/plugins/codex-security/mcp-app/package.json@@ -6,7 +6,7 @@"scripts": {"build": "tsc --noEmit","build:mcp": "node scripts/build_mcp_app.mjs --output .preview/mcp",- "test:mcp": "node --test --test-concurrency=2 --test-reporter=./scripts/test_reporter.mjs \"tests/test_*.mjs\"",+ "test:mcp": "node --experimental-strip-types --test --test-concurrency=2 --test-reporter=./scripts/test_reporter.mts \"tests/test_*.ts\"","typecheck": "tsc --noEmit"},"dependencies": {diff --git a/plugins/codex-security/mcp-app/scripts/test_host_build.mjs b/plugins/codex-security/mcp-app/scripts/test_host_build.mtssimilarity index 87%rename from plugins/codex-security/mcp-app/scripts/test_host_build.mjsrename to plugins/codex-security/mcp-app/scripts/test_host_build.mtsindex 1520e28ed..49793937e 100644--- a/plugins/codex-security/mcp-app/scripts/test_host_build.mjs+++ b/plugins/codex-security/mcp-app/scripts/test_host_build.mts@@ -1,21 +1,24 @@+import { temporaryDirectory } from "../tests/support/temporary-directories.ts";+import { readJson } from "../tests/support/json.ts";import assert from "node:assert/strict";import { execFileSync } from "node:child_process";import {copyFile,mkdir,- mkdtemp,readFile,readdir,rm,writeFile,} from "node:fs/promises";-import { tmpdir } from "node:os";import { join, relative, resolve, sep } from "node:path";import test from "node:test";const plugin = resolve(import.meta.dirname, "../..");-function runNode(args, options = {}) {+function runNode(+ args: string[],+ options: import("node:child_process").ExecFileSyncOptions = {},+) {return execFileSync(process.execPath, args, {cwd: plugin,stdio: "inherit",@@ -24,7 +27,7 @@ function runNode(args, options = {}) {}test("builds and loads the host runtime from standalone plugin source", async (t) => {- const output = await mkdtemp(join(tmpdir(), "codex-security-host-build-"));+ const output = await temporaryDirectory("codex-security-host-build-");const mcp = join(output, "mcp");t.after(() => rm(output, { recursive: true, force: true }));@@ -38,16 +41,14 @@ test("builds and loads the host runtime from standalone plugin source", async (t]);const { nativeTarget } = await import("../../native/platform.mjs");- const contract = JSON.parse(- await readFile(join(plugin, "plugin-files.json"), "utf8"),- );+ const contract = await readJson(plugin, "plugin-files.json");const actual = (await readdir(mcp, { recursive: true, withFileTypes: true })).filter((entry) => entry.isFile()).map((entry) =>relative(output, join(entry.parentPath, entry.name)).split(sep).join("/"),);const expected = contract.shippedExact.filter(- (path) =>+ (path: string) =>path.startsWith("mcp/") &&(!path.endsWith(".node") ||path.startsWith(`mcp/native/${nativeTarget}/`)),@@ -62,7 +63,7 @@ test("builds and loads the host runtime from standalone plugin source", async (t),PATH: "",};- const nativeFile = expected.find((path) => path.endsWith(".node"));+ const nativeFile = expected.find((path: string) => path.endsWith(".node"));runNode(["-e",@@ -115,7 +116,7 @@ test("builds and loads the host runtime from standalone plugin source", async (t});test("rejects an unsupported native host before replacing existing output", async (t) => {- const output = await mkdtemp(join(tmpdir(), "codex-security-unsupported-"));+ const output = await temporaryDirectory("codex-security-unsupported-");t.after(() => rm(output, { recursive: true, force: true }));const marker = join(output, "keep");await writeFile(marker, "previous output");@@ -145,7 +146,7 @@ test("rejects an unsupported native host before replacing existing output", asyn],{ stdio: "pipe", encoding: "utf8" },),- (error) => {+ (error: Error & { status: number; stderr: string }) => {assert.equal(error.status, 1);assert.equal(error.stderr.trim(),diff --git a/plugins/codex-security/mcp-app/scripts/test_reporter.mjs b/plugins/codex-security/mcp-app/scripts/test_reporter.mjsdeleted file mode 100644index 8cd2c5ded..000000000--- a/plugins/codex-security/mcp-app/scripts/test_reporter.mjs+++ /dev/null@@ -1,23 +0,0 @@-import { mkdir, writeFile } from "node:fs/promises";-import { junit, tap } from "node:test/reporters";--export default async function* report(source) {- const events = [];- async function* record() {- for await (const event of source) {- events.push(event);- yield event;- }- }-- yield* tap(record());-- try {- await mkdir("reports", { recursive: true });- await writeFile("reports/junit.xml", junit(events));- } catch (error) {- console.warn(- `Could not write the optional MCP test report: ${error.message}`,- );- }-}diff --git a/plugins/codex-security/mcp-app/scripts/test_reporter.mts b/plugins/codex-security/mcp-app/scripts/test_reporter.mtsnew file mode 100644index 000000000..ec79aed40--- /dev/null+++ b/plugins/codex-security/mcp-app/scripts/test_reporter.mts@@ -0,0 +1,16 @@+import { mkdir, writeFile } from "node:fs/promises";+import { junit, tap, type TestEvent } from "node:test/reporters";++export default async function* report(source: AsyncIterable<TestEvent>) {+ const [tapEvents, reportEvents] = ReadableStream.from(source).tee();+ yield* tap(tapEvents);++ try {+ await mkdir("reports", { recursive: true });+ await writeFile("reports/junit.xml", junit(reportEvents));+ } catch (error) {+ console.warn(+ `Could not write the optional MCP test report: ${(error as Error).message}`,+ );+ }+}diff --git a/plugins/codex-security/mcp-app/tests/assertions.mjs b/plugins/codex-security/mcp-app/tests/assertions.tssimilarity index 56%rename from plugins/codex-security/mcp-app/tests/assertions.mjsrename to plugins/codex-security/mcp-app/tests/assertions.tsindex b550b6d9a..abb186e4f 100644--- a/plugins/codex-security/mcp-app/tests/assertions.mjs+++ b/plugins/codex-security/mcp-app/tests/assertions.ts@@ -1,15 +1,24 @@import assert from "node:assert/strict";-export function assertNoError(response) {- assert.equal(response.error, undefined, response.error?.message);+interface ToolResponse {+ error?: { message?: string };+ result?: { isError?: boolean; content?: { text?: string }[] };+}++export function assertNoError(response: ToolResponse) {+ assert.equal(response.error, undefined, response.error?.message!);assert.equal(response.result?.isError,undefined,- response.result?.content?.map((item) => item.text).join(" "),+ response.result?.content?.map((item) => item.text).join(" ")!,);}-export function assertFlagPair(args, flag, value) {+export function assertFlagPair(+ args: readonly string[],+ flag: string,+ value: string,+) {const index = args.indexOf(flag);assert.notEqual(index, -1, `missing ${flag}`);assert.equal(args[index + 1], value);diff --git a/plugins/codex-security/mcp-app/tests/build-server.mjs b/plugins/codex-security/mcp-app/tests/build-server.tssimilarity index 59%rename from plugins/codex-security/mcp-app/tests/build-server.mjsrename to plugins/codex-security/mcp-app/tests/build-server.tsindex 5037b8b97..c916ec075 100644--- a/plugins/codex-security/mcp-app/tests/build-server.mjs+++ b/plugins/codex-security/mcp-app/tests/build-server.ts@@ -1,13 +1,9 @@import path from "node:path";-import { fileURLToPath } from "node:url";-import { build } from "esbuild";+import { build, type BuildOptions } from "esbuild";-export const applicationRoot = path.resolve(- path.dirname(fileURLToPath(import.meta.url)),- "..",-);+export const applicationRoot = path.resolve(import.meta.dirname, "..");-export function buildServer(outfile, options = {}) {+export function buildServer(outfile: string, options: BuildOptions = {}) {return build({bundle: true,define: { "import.meta.url": "__filename" },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.tsnew file mode 100644index 000000000..e55fadcd8--- /dev/null+++ b/plugins/codex-security/mcp-app/tests/deep_scan_coordinator_fixture.ts@@ -0,0 +1,823 @@+import { readJson, writeJson } from "./support/json.ts";+import type { CoordinatorOptions } from "../src/deep-scan/coordinator.js";+import type {+ DeepScanConfig,+ DeepScanRunState,+ DeepScanStore,+ PersistedDeepScanWorker,+ DeepScanWorkerMutation,+ DedupCommit,+ CodexWorkerRequest,+ CodexWorkerResult,+ CodexWorkerArtifactContext,+ CodexWorkerDiagnostic,+ DeepScanClock,+} from "../src/deep-scan/types.js";+import { sourceReferences } from "./support/source-references.ts";+import { createTemporaryDirectories } from "./support/temporary-directories.ts";+import { once } from "node:events";+import assert from "node:assert/strict";+import { createHash, randomUUID } from "node:crypto";+import { readFile, rename, rm, writeFile } from "node:fs/promises";+import path from "node:path";+import { importModule } from "./import-module.ts";++export const {+ DeepScanCoordinator,+ DeepScanCoordinatorRegistry,+ DeepScanNonRetryableError,+ DeepScanRemoteCoordinator,+ AsyncLock,+ classifyCodexWorkerError,+ startOrJoinDeepScanCoordinator,+} = await importModule({+ stdin: {+ contents: `+ export * from "./registry.ts";+ export { classifyCodexWorkerError } from "./errors.ts";+ `,+ resolveDir: new URL("../src/deep-scan/", import.meta.url).pathname,+ },+ loader: { ".md": "text" },+});+export const claimDedupInputs = (claim: StoreInput<"claimDedup">) =>+ claim.workerIds.map((discoveryWorkerId, inputOrder) => ({+ dedupWorkerId: claim.id,+ discoveryWorkerId,+ inputOrder,+ }));++export const recordSleeps =+ (sleeps: number[]): DeepScanClock["sleep"] =>+ async (delayMs, signal) => {+ assert.equal(signal.aborted, false);+ sleeps.push(delayMs);+ };++export const temporaryDirectories = createTemporaryDirectories(true);+export async function fixtureRun(+ config: Partial<DeepScanConfig> = {},+): Promise<{ pluginRoot: string; run: DeepScanRunState }> {+ const root = await temporaryDirectories.create(+ "codex-security-deep-coordinator-",+ );+ return {+ pluginRoot: path.join(root, "plugin"),+ run: {+ scanId: randomUUID(),+ status: "running" as const,+ targetPath: path.join(root, "target"),+ scope: ".",+ scanDir: path.join(root, "scan"),+ config: {+ workers: 1,+ subagents: 0,+ stopAfterNoNew: 1,+ maxDiscoveryRuns: 1,+ ...config,+ stopAfterConsecutiveErrors:+ config.stopAfterConsecutiveErrors ?? config.stopAfterNoNew ?? 1,+ },+ dispatchedCount: 0,+ noNewStreak: 0,+ consecutiveErrors: 0,+ },+ };+}++export type TestWorker = PersistedDeepScanWorker &+ Pick<DeepScanWorkerMutation, "replaceableFailureKind">;+export type StoreInput<Method extends keyof DeepScanStore> = Parameters<+ DeepScanStore[Method]+>[0];++export class FakeStore {+ run: DeepScanRunState;+ declare failProgressAt: number | undefined;+ constructor(run: DeepScanRunState) {+ this.run = structuredClone(run);+ }++ workerUpdates: DeepScanWorkerMutation[] = [];+ workers = new Map<string, TestWorker>();+ completionSequence = 0;+ progress: StoreInput<"updateProgress">[] = [];+ dedupClaims: StoreInput<"claimDedup">[] = [];+ dedupCommits: DedupCommit[] = [];+ dedupCommitCalls: DedupCommit[] = [];+ failDedupCommitFromCall: number | undefined = undefined;+ loseEveryDedupCommitResponseAfterCommit = false;+ failureMessages: string[] = [];+ finishCalls: StoreInput<"finish">[] = [];+ failFinish = false;+ rejectFailurePersistence = false;+ replacementManifestBeforeFinishRejection: string | undefined = undefined;+ replacementCandidatesBeforeDedupRejection: string | undefined = undefined;+ loseFirstFinishResponseAfterCommit = false;+ loseFirstDiscoveryAcceptanceResponseAfterCommit = false;+ loseFirstDedupCommitResponseAfterCommit = false;+ dedupCommitResponseGate?: PromiseWithResolvers<void>;+ blockDiscoveryUpdate?: "failed" | "succeeded";+ discoveryBlocked = Promise.withResolvers<void>();+ discoveryGate = Promise.withResolvers<void>();+ dedupCommitted = Promise.withResolvers<void>();+ failNextTerminalGet = false;+ publicationFailureMessages: string[] = [];++ async get(_scanId?: string, _threadId?: string): Promise<DeepScanRunState> {+ if (this.failNextTerminalGet && this.run.status !== "running") {+ this.failNextTerminalGet = false;+ throw new Error("database is locked");+ }+ return structuredClone({+ ...this.run,+ ...(this.workers.size > 0+ ? { persistedWorkers: [...this.workers.values()] }+ : {}),+ });+ }++ async claimCoordinator(_input?: StoreInput<"claimCoordinator">) {+ return { run: structuredClone(this.run), acquired: true };+ }++ async heartbeatCoordinator(_input?: StoreInput<"heartbeatCoordinator">) {+ this.run.updatedAt = new Date().toISOString();+ return structuredClone(this.run);+ }++ async updateWorker(update: DeepScanWorkerMutation) {+ if (update.error) {+ assert.equal(+ update.error.length <= 2_400,+ true,+ "persisted worker errors must be bounded",+ );+ }+ if (+ update.kind === "discovery" &&+ update.status === this.blockDiscoveryUpdate+ ) {+ this.discoveryBlocked.resolve();+ await this.discoveryGate.promise;+ if (update.status === "succeeded" && this.run.status !== "running") {+ throw new Error(+ "Only a running Deep Scan can update orchestration state.",+ );+ }+ }+ this.workerUpdates.push(structuredClone(update));+ const previous = this.workers.get(update.id);+ const persisted: TestWorker = {+ mergeState: "none" as const,+ ...previous,+ ...structuredClone(update),+ };+ if (+ update.kind === "discovery" &&+ update.status === "queued" &&+ !previous+ ) {+ this.run.dispatchedCount += 1;+ }+ if (+ update.kind === "discovery" &&+ update.status === "canceled" &&+ update.replaceableFailureKind &&+ previous?.status === "running"+ ) {+ this.run.consecutiveErrors += 1;+ }+ if (+ update.kind === "discovery" &&+ update.status === "succeeded" &&+ !persisted.completionSequence+ ) {+ persisted.completionSequence = ++this.completionSequence;+ persisted.mergeState = "buffered";+ this.run.consecutiveErrors = 0;+ }+ if (+ update.kind === "dedup" &&+ update.status === "failed" &&+ previous?.status === "running"+ ) {+ const claim = this.dedupClaims.find(+ (candidate) => candidate.id === update.id,+ );+ for (const workerId of claim?.workerIds ?? []) {+ const discovery = this.workers.get(workerId);+ if (+ discovery?.status === "succeeded" &&+ discovery.mergeState === "merging"+ ) {+ this.workers.set(workerId, { ...discovery, mergeState: "buffered" });+ }+ }+ this.run.phase = "discovery";+ }+ persisted.consecutiveErrors = this.run.consecutiveErrors;+ this.workers.set(update.id, persisted);+ if (+ this.loseFirstDiscoveryAcceptanceResponseAfterCommit &&+ update.kind === "discovery" &&+ update.status === "succeeded"+ ) {+ this.loseFirstDiscoveryAcceptanceResponseAfterCommit = false;+ throw new Error(+ "fixture lost discovery acceptance response after commit",+ );+ }+ return structuredClone(persisted);+ }++ async claimDedup(input: StoreInput<"claimDedup">) {+ this.dedupClaims.push(structuredClone(input));+ for (const workerId of input.workerIds) {+ const discovery = this.workers.get(workerId);+ assert.equal(discovery?.mergeState, "buffered");+ this.workers.set(workerId, { ...discovery, mergeState: "merging" });+ }+ await this.updateWorker({+ id: input.id,+ scanId: input.scanId,+ kind: "dedup" as const,+ status: "running" as const,+ promptPath: input.promptPath,+ artifactDir: input.artifactDir,+ attempt: 1,+ });+ }++ async commitDedup(commit: DedupCommit) {+ this.dedupCommitCalls.push(structuredClone(commit));+ if (+ this.failDedupCommitFromCall !== undefined &&+ this.dedupCommitCalls.length >= this.failDedupCommitFromCall+ ) {+ if (this.replacementCandidatesBeforeDedupRejection) {+ await writeFile(+ this.dedupCommits.at(-1)!.resultManifestPath,+ this.replacementCandidatesBeforeDedupRejection,+ );+ }+ throw new DeepScanNonRetryableError(+ "fixture rejected the reducer commit",+ );+ }+ if (this.dedupCommits.some((previous) => previous.id === commit.id)) {+ if (this.loseEveryDedupCommitResponseAfterCommit) {+ throw new Error("fixture lost the committed reducer response");+ }+ return structuredClone(this.run);+ }+ this.dedupCommits.push(structuredClone(commit));+ const claim = this.dedupClaims.find((claim) => claim.id === commit.id);+ assert.ok(claim);+ if (commit.newFindings > 0) this.run.noNewStreak = 0;+ else this.run.noNewStreak += claim.workerIds.length;+ for (const workerId of claim.workerIds) {+ const discovery = this.workers.get(workerId);+ assert.equal(discovery?.mergeState, "merging");+ this.workers.set(workerId, { ...discovery, mergeState: "merged" });+ }+ await this.updateWorker({+ ...this.workers.get(commit.id)!,+ id: commit.id,+ scanId: commit.scanId,+ kind: "dedup" as const,+ status: "succeeded",+ attempt: this.workers.get(commit.id)?.attempt ?? 1,+ resultManifestPath: commit.resultManifestPath,+ });+ this.dedupCommitted.resolve();+ if (this.loseEveryDedupCommitResponseAfterCommit) {+ throw new Error("fixture lost the committed reducer response");+ }+ if (this.dedupCommitResponseGate)+ await this.dedupCommitResponseGate.promise;+ if (this.loseFirstDedupCommitResponseAfterCommit) {+ this.loseFirstDedupCommitResponseAfterCommit = false;+ throw new Error("fixture lost dedup commit response after commit");+ }+ return structuredClone(this.run);+ }++ async finish(input: StoreInput<"finish">) {+ this.finishCalls.push(structuredClone(input));+ if (this.run.status === "succeeded") return structuredClone(this.run);+ if (this.failFinish) {+ if (this.replacementManifestBeforeFinishRejection) {+ await writeFile(+ input.manifestPath,+ this.replacementManifestBeforeFinishRejection,+ );+ }+ throw new DeepScanNonRetryableError("fixture finish persistence failure");+ }+ if (input.stagedManifestPath)+ await rename(input.stagedManifestPath, input.manifestPath);+ const latestReducer = this.dedupCommits.at(-1);+ const draft = latestReducer?.resultManifestPath+ ? await readJson(latestReducer.resultManifestPath!)+ : {+ scanId: this.run.scanId,+ findings: [],+ coverage: {+ completeness: "partial",+ surfaces: [],+ explicitExclusions: [],+ deferred: [],+ },+ };+ await writeJson(input.manifestPath, {+ scan: {+ scanId: draft.scanId,+ mode: "deep",+ ...(draft.threatModel ? { threatModel: draft.threatModel } : {}),+ },+ findings: draft.findings,+ coverage: draft.coverage,+ });+ this.run.status = "succeeded";+ this.run.terminalReason = input.reason;+ this.run.manifestPath = input.manifestPath;+ if (+ this.loseFirstFinishResponseAfterCommit &&+ this.finishCalls.length === 1+ ) {+ throw new Error("fixture lost finish response after commit");+ }+ return structuredClone(this.run);+ }++ async fail(+ _scanId: string,+ message: string,+ status: "failed" | "interrupted" = "failed",+ manifestPath?: string,+ stagedManifestPath?: string,+ ) {+ this.failureMessages.push(message);+ if (this.rejectFailurePersistence) {+ throw new DeepScanNonRetryableError(+ "fixture rejected stale failure persistence",+ );+ }+ if (stagedManifestPath && manifestPath)+ await rename(stagedManifestPath, manifestPath);+ this.run.status = status;+ this.run.error = message;+ this.run.manifestPath = manifestPath;+ return structuredClone(this.run);+ }++ async recordStoppedPublicationFailure(_scanId: string, message: string) {+ this.publicationFailureMessages.push(message);+ const original = this.run.error?.trim();+ this.run.error = original+ ? boundedFixtureErrorPair(message, original)+ : message;+ return structuredClone(this.run);+ }++ async updateProgress(input: StoreInput<"updateProgress">) {+ if (this.failProgressAt && --this.failProgressAt === 0) {+ throw new Error("fixture progress persistence failure");+ }+ this.progress.push(structuredClone(input));+ if (input.phase) this.run.phase = input.phase as DeepScanRunState["phase"];+ }+}++interface ExecutorOptions {+ blockDedupAfterWrite?: boolean;+ blockDiscoveryAfterWrite?: boolean;+ corruptAcceptedSource?: boolean;+ dropLastDedupFinding?: boolean;+ failFirstDiscoveryAttempt?: boolean;+ invalidFirstDedupResult?: boolean;+ omitFirstDedupCandidateLedger?: boolean;+ writePartialBeforeFailure?: boolean;+ blockDiscoveryAfterCalls?: number;+ invalidDedupFromCall?: number;+ invalidDiscoveryAttempts?: number;+ malformedDiscoveryAttempts?: number;+ canonicalCandidateId?: string;+ discoveryCandidateId?: string;+ discoveryFailureMessage?: string;+ nonRetryableDiscoveryMessage?: string;+ dedupEvidenceByCall?: string[];+ failDiscoveryWorkersAfterGate?: string[];+ nonRetryableDiscoveryWorkers?: string[];+ dedupDiagnostics?: CodexWorkerDiagnostic[];+ discoveryDiagnostics?: CodexWorkerDiagnostic[];+ discoveryCandidates?: Record<string, string>;+ discoveryFailureMessages?: Record<string, string>;+ discoveryGates?: Record<string, Promise<void>>;+ missingDedupResultsByLabel?: Record<string, number>;+}++export class FakeExecutor {+ options: ExecutorOptions;+ discoveryStarted = Promise.withResolvers<void>();+ dedupStarted = Promise.withResolvers<void>();+ dedupGate?: PromiseWithResolvers<void>;+ dedupArtifactsWritten = Promise.withResolvers<void>();+ discoveryArtifactsWritten = Promise.withResolvers<void>();+ constructor(options: ExecutorOptions = {}) {+ this.options = options;+ }++ discoveryCalls = 0;+ discoveryAttempts = new Map<string, number>();+ discoveryPromptPaths = new Map<string, Set<string>>();+ discoveryWorkingDirectories = new Set<string>();+ discoveryResumeThreadIds: (string | undefined)[] = [];+ discoveryContinuationPrompts: (string | undefined)[] = [];+ discoveryThreadIds: string[] = [];+ runningDiscovery = 0;+ maximumDiscoveryConcurrency = 0;+ runningDedup = 0;+ maximumDedupConcurrency = 0;+ dedupCalls = 0;+ dedupAttemptsByLabel = new Map<string, number>();+ dedupResumeThreadIds: (string | undefined)[] = [];+ dedupThreadIds: string[] = [];+ dedupContinuationPrompts: (string | undefined)[] = [];+ dedupPromptPaths: string[] = [];+ dedupArtifactContexts: CodexWorkerArtifactContext[] = [];+ dedupSignal: AbortSignal | undefined = undefined;++ async run(request: CodexWorkerRequest): Promise<CodexWorkerResult> {+ const threadId = request.resumeThreadId ?? randomUUID();+ await request.onThreadStarted?.(threadId);+ if (request.kind === "discovery") {+ const workerId = await workerIdFromPrompt(request.promptPath);+ this.discoveryWorkingDirectories.add(request.workingDirectory);+ this.discoveryResumeThreadIds.push(request.resumeThreadId);+ this.discoveryContinuationPrompts.push(request.continuationPrompt);+ this.discoveryThreadIds.push(threadId);+ this.discoveryCalls += 1;+ this.discoveryAttempts.set(+ workerId,+ (this.discoveryAttempts.get(workerId) ?? 0) + 1,+ );+ const promptPaths = this.discoveryPromptPaths.get(workerId) ?? new Set();+ promptPaths.add(request.promptPath);+ this.discoveryPromptPaths.set(workerId, promptPaths);+ this.runningDiscovery += 1;+ this.maximumDiscoveryConcurrency = Math.max(+ this.maximumDiscoveryConcurrency,+ this.runningDiscovery,+ );+ this.discoveryStarted.resolve();+ try {+ const message =+ this.options.discoveryFailureMessages?.[workerId] ??+ this.options.discoveryFailureMessage;+ if (message) throw new Error(message);+ if (this.options.failFirstDiscoveryAttempt) {+ this.options.failFirstDiscoveryAttempt = false;+ if (this.options.writePartialBeforeFailure) {+ await writeFile(+ path.join(request.workingDirectory, "partial-progress.txt"),+ "preserve me\n",+ );+ }+ throw new Error("transient worker failure");+ }+ if (this.options.nonRetryableDiscoveryMessage) {+ throw new DeepScanNonRetryableError(+ this.options.nonRetryableDiscoveryMessage,+ );+ }+ if (this.options.nonRetryableDiscoveryWorkers?.includes(workerId)) {+ throw new DeepScanNonRetryableError("fixture configuration failure");+ }+ await this.options.discoveryGates?.[workerId];+ if (this.options.failDiscoveryWorkersAfterGate?.includes(workerId)) {+ throw new DeepScanNonRetryableError("fixture late discovery failure");+ }+ if (+ this.options.blockDiscoveryAfterCalls !== undefined &&+ this.discoveryCalls > this.options.blockDiscoveryAfterCalls+ ) {+ await waitForAbort(request.signal);+ }+ const omitArtifact = (this.options.invalidDiscoveryAttempts ?? 0) > 0;+ const malformedResult =+ (this.options.malformedDiscoveryAttempts ?? 0) > 0;+ if (omitArtifact) this.options.invalidDiscoveryAttempts! -= 1;+ if (malformedResult) this.options.malformedDiscoveryAttempts! -= 1;+ const candidateId =+ this.options.discoveryCandidates?.[workerId] ??+ this.options.discoveryCandidateId;+ const { promptPath, workingDirectory, artifactContext } = request;+ const context = await promptContext(promptPath);+ assert.equal(artifactContext?.layout, "worker");+ assert.equal(artifactContext.root, workingDirectory);+ if (!omitArtifact) {+ const draft = standardScanDraft(+ context.scanId,+ candidateId,+ context.workerLabel,+ );+ await writeJson(+ path.join(artifactContext.root, "result.json"),+ draft,+ );+ }+ if (malformedResult) {+ await writeFile(+ path.join(request.artifactContext!.root, "result.json"),+ "{malformed",+ );+ }+ this.discoveryArtifactsWritten.resolve();+ if (this.options.blockDiscoveryAfterWrite)+ await waitForAbort(request.signal);+ return {+ threadId,+ diagnostics: this.options.discoveryDiagnostics,+ };+ } finally {+ this.runningDiscovery -= 1;+ }+ }++ this.runningDedup += 1;+ this.dedupSignal = request.signal;+ this.dedupResumeThreadIds.push(request.resumeThreadId);+ this.dedupThreadIds.push(threadId);+ this.dedupContinuationPrompts.push(request.continuationPrompt);+ this.dedupPromptPaths.push(request.promptPath);+ this.dedupArtifactContexts.push(request.artifactContext!);+ this.maximumDedupConcurrency = Math.max(+ this.maximumDedupConcurrency,+ this.runningDedup,+ );+ this.dedupStarted.resolve();+ try {+ if (this.dedupGate) await this.dedupGate.promise;+ if (request.signal.aborted)+ throw new DOMException("aborted", "AbortError");+ this.dedupCalls += 1;+ const reducerLabel = (await promptContext(request.promptPath))+ .reducerLabel;+ const reducerAttempt =+ (this.dedupAttemptsByLabel.get(reducerLabel) ?? 0) + 1;+ this.dedupAttemptsByLabel.set(reducerLabel, reducerAttempt);+ if (+ reducerAttempt <=+ (this.options.missingDedupResultsByLabel?.[reducerLabel] ?? 0)+ ) {+ return {+ threadId,+ diagnostics: this.options.dedupDiagnostics,+ };+ }+ const invalidResult =+ (this.options.invalidDedupFromCall !== undefined &&+ this.dedupCalls >= this.options.invalidDedupFromCall) ||+ this.options.invalidFirstDedupResult;+ this.options.invalidFirstDedupResult &&= false;+ await writeDedupArtifacts(request, invalidResult, {+ canonicalCandidateId: this.options.canonicalCandidateId,+ evidence: this.options.dedupEvidenceByCall?.[this.dedupCalls - 1],+ dropLastFinding: this.options.dropLastDedupFinding,+ corruptAcceptedSource: this.options.corruptAcceptedSource,+ });+ if (this.options.omitFirstDedupCandidateLedger && this.dedupCalls === 1) {+ await rm(path.join(request.artifactContext!.root, "result.json"));+ }+ this.dedupArtifactsWritten.resolve();+ if (this.options.blockDedupAfterWrite) await waitForAbort(request.signal);+ return {+ threadId,+ diagnostics: this.options.dedupDiagnostics,+ };+ } finally {+ this.runningDedup -= 1;+ }+ }+}++export async function workerIdFromPrompt(promptPath: string) {+ return (await promptContext(promptPath)).workerLabel ?? promptPath;+}++export async function promptContext(promptPath: string) {+ const prompt = await readFile(promptPath, "utf8");+ const encoded = prompt.match(/```json\n([\s\S]*?)\n```/)?.[1];+ if (!encoded) throw new Error(`Missing JSON context in ${promptPath}`);+ return JSON.parse(encoded);+}++async function writeDedupArtifacts(+ request: CodexWorkerRequest,+ invalidResult: boolean | undefined,+ options: {+ canonicalCandidateId?: string;+ evidence?: string;+ dropLastFinding?: boolean;+ corruptAcceptedSource?: boolean;+ } = {},+) {+ const context = await promptContext(request.promptPath);+ const artifactContext = request.artifactContext!;+ assert.equal(artifactContext?.layout, "reducer");+ const reducer = artifactContext.deepReducer;+ assert.ok(reducer);+ const workerIds = reducer.claimedWorkers.map((worker) => worker.id);+ assert.deepEqual(context.claimedWorkerIds, workerIds);+ const workerDrafts = await Promise.all(+ reducer.claimedWorkers.map(async (worker) => {+ const result = await readJson(worker.resultPath);+ result.findings = result.findings.map(sourceReferences(worker));+ return result;+ }),+ );+ const previousDraft = reducer.previousReducerResultPath+ ? await readJson(reducer.previousReducerResultPath)+ : undefined;+ const mergedFindings = new Map<string, CoordinatorFinding>();+ for (const finding of [+ ...(previousDraft?.findings ?? []),+ ...workerDrafts.flatMap((draft) => draft.findings),+ ]) {+ const findingIdentity =+ finding.identity?.anchor ??+ finding.provenance?.candidateId ??+ finding.ruleId;+ mergedFindings.set(findingIdentity, {+ ...mergedFindings.get(findingIdentity),+ ...finding,+ ...(options.evidence === undefined+ ? {}+ : { rootCause: { summary: options.evidence } }),+ provenance: {+ ...finding.provenance,+ sourceFindingIds: [+ ...new Set([+ ...(mergedFindings.get(findingIdentity)?.provenance+ .sourceFindingIds ?? []),+ ...(finding.provenance.sourceFindingIds ?? []),+ ]),+ ],+ },+ });+ }+ const draft = standardScanDraft(+ previousDraft?.scanId ?? workerDrafts[0]?.scanId,+ undefined,+ context.reducerLabel,+ );+ draft.findings = [...mergedFindings.values()];+ if (options.canonicalCandidateId && draft.findings.length > 0) {+ draft.findings[0].provenance.candidateId = options.canonicalCandidateId;+ }+ delete (draft as { coverage?: unknown }).coverage;+ if (invalidResult) (draft as { findings: unknown }).findings = "invalid";+ if (options.dropLastFinding) draft.findings.pop();+ const resultPath = path.join(artifactContext.root, "result.json");+ await writeJson(resultPath, draft);+ if (options.corruptAcceptedSource) {+ const source = reducer.claimedWorkers.at(-1);+ await writeFile(source!.resultPath, "{invalid standard scan\n");+ }+}++export function standardScanDraft(+ scanId: string,+ candidateId: string | undefined,+ workerLabel: string,+) {+ return {+ scanId,+ threatModel: { summary: `Independent threat model for ${workerLabel}.` },+ findings: candidateId+ ? [+ {+ ruleId: "sql-injection.fixture",+ title: "Unsafe fixture query",+ summary:+ "A request-controlled value reaches a security-sensitive sink.",+ severity: { level: "high" },+ confidence: {+ level: "high",+ rationale: "Source evidence establishes reachability.",+ },+ taxonomy: { category: "sql-injection", cwe: ["CWE-89"] },+ locations: [{ path: "fixture.py", startLine: 1, endLine: 1 }],+ remediation: "Use a parameterized query.",+ provenance: {+ source: "local_plugin",+ candidateId,+ workerId: workerLabel,+ },+ },+ ]+ : [],+ coverage: {+ completeness: "complete",+ surfaces: [+ {+ label: "Fixture query",+ disposition: candidateId ? "reported" : "no_issue_found",+ },+ ],+ explicitExclusions: [],+ deferred: [],+ },+ };+}++async function waitForAbort(signal: AbortSignal) {+ if (!signal.aborted) await once(signal, "abort");+ throw new DOMException("aborted", "AbortError");+}++export async function eventually(predicate: () => boolean) {+ const deadline = Date.now() + 2_000;+ while (Date.now() < deadline) {+ if (predicate()) return;+ await new Promise((resolve) => setTimeout(resolve, 5));+ }+ assert.fail("condition did not become true");+}++export async function assertNoPublishedCandidates(scanDir: string) {+ await assert.rejects(+ readFile(path.join(scanDir, "scan-manifest.json"), "utf8"),+ (error: NodeJS.ErrnoException) => error.code === "ENOENT",+ );+}++export const immediateClock: DeepScanClock = {+ now: () => 1_700_000_000_000,+ sleep: async (_delayMs, signal) => {+ if (signal.aborted) throw new DOMException("aborted", "AbortError");+ },+};++function boundedFixtureErrorPair(primary: string, secondary: string) {+ const separator = "\nOriginal Deep Scan failure:\n";+ const available = 2_400 - separator.length;+ let primaryBudget = Math.min(primary.length, Math.floor(available / 2));+ const secondaryBudget = Math.min(secondary.length, available - primaryBudget);+ primaryBudget = Math.min(primary.length, available - secondaryBudget);+ return (+ boundedFixtureErrorText(primary, primaryBudget) ++ separator ++ boundedFixtureErrorText(secondary, secondaryBudget)+ );+}++function boundedFixtureErrorText(message: string, maximum: number) {+ if (message.length <= maximum) return message;+ const digest = createHash("sha256").update(message).digest("hex");+ const suffix = `\n...[truncated; sha256:${digest}]`;+ return message.slice(0, maximum - suffix.length) + suffix;+}++export function createCoordinator(+ fixture: Awaited<ReturnType<typeof fixtureRun>>,+ store: FakeStore,+ executor: { run(request: CodexWorkerRequest): Promise<CodexWorkerResult> },+ options: Partial<CoordinatorOptions> = {},+) {+ return new DeepScanCoordinator({+ run: fixture.run,+ store,+ executor,+ pluginRoot: fixture.pluginRoot,+ clock: immediateClock,+ ...options,+ });+}++type CoordinatorFinding = ReturnType<+ typeof standardScanDraft+>["findings"][number] & {+ identity?: { anchor: string };+ provenance: { sourceFindingIds?: string[] };+};++export function runCoordinator(...args: Parameters<typeof createCoordinator>) {+ const coordinator = createCoordinator(...args);+ coordinator.start();+ return coordinator.wait(undefined, 5_000);+}++export function recordingClock(sleeps: number[]): DeepScanClock {+ return {+ now: immediateClock.now,+ sleep: async (delayMs) => void sleeps.push(delayMs),+ };+}++export async function coordinatorFixture(config: Partial<DeepScanConfig> = {}) {+ const fixture = await fixtureRun(config);+ return { fixture, store: new FakeStore(fixture.run) };+}diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_deadline_cases.mjs b/plugins/codex-security/mcp-app/tests/deep_scan_deadline_cases.mjsdeleted file mode 100644index 6dae96da3..000000000--- a/plugins/codex-security/mcp-app/tests/deep_scan_deadline_cases.mjs+++ /dev/null@@ -1,280 +0,0 @@-import assert from "node:assert/strict";-import { readFile } from "node:fs/promises";--export async function testDeepScanDeadlines({- fixtureRun,- FakeStore,- FakeExecutor,- createCoordinator,- immediateClock,- eventually,-}) {- async function testDiscoveryDeadlineDrainsActiveReducerAndPreservesFindings() {- const fixture = await fixtureRun({- workers: 2,- subagents: 0,- stopAfterNoNew: 99,- maxDiscoveryRuns: 12,- });- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({- blockDedup: true,- blockDiscoveryAfterCalls: 2,- discoveryCandidateId: "candidate-1",- canonicalCandidateId: "candidate-1",- dedupNewFindings: [1],- });- const coordinator = createCoordinator(fixture, store, executor, {- discoveryTimeoutMs: 500,- });- coordinator.start();-- await executor.dedupStarted.promise;- await eventually(() => executor.runningDiscovery > 0);- await eventually(() => executor.runningDiscovery === 0);-- const discoveryCallsAtDeadline = executor.discoveryCalls;- assert.equal(- executor.runningDedup,- 1,- "the deadline must let an active reducer finish",- );- assert.equal(executor.dedupSignal?.aborted, false);- assert.equal(store.finishCalls.length, 0);- executor.releaseDedup();-- const terminal = await coordinator.wait(undefined, 5_000);- assert.equal(terminal?.status, "succeeded");- assert.equal(terminal?.terminalReason, "capped");- assert.equal(store.failureInputs.length, 0);- assert.equal(executor.discoveryCalls, discoveryCallsAtDeadline);- assert.equal(- terminal.dispatchedCount < fixture.run.config.maxDiscoveryRuns,- true,- );-- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));- assert.equal(manifest.scan.scanId, fixture.run.scanId);- assert.deepEqual(store.finishCalls[0].omittedWorkerIds, []);- assert.equal(- [...store.workers.values()].some(- (worker) => worker.status === "canceled",- ),- true,- );- assert.equal(store.dedupCommits.length, 1);- assert.deepEqual(- manifest.findings.map((finding) => finding.provenance.candidateId),- ["candidate-1"],- );- assert.equal(- [...store.workers.values()].some((worker) =>- ["queued", "running"].includes(worker.status),- ),- false,- "deadline completion must wait for every canceled discovery and reducer to settle",- );- }-- async function testDiscoveryDeadlineReducesSingleBufferedFinding() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 99,- maxDiscoveryRuns: 8,- });- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({- blockDiscoveryAfterCalls: 1,- discoveryCandidateId: "candidate-1",- canonicalCandidateId: "candidate-1",- dedupNewFindings: [1],- });- const coordinator = createCoordinator(fixture, store, executor, {- discoveryTimeoutMs: 500,- });- coordinator.start();-- await eventually(- () =>- executor.discoveryCalls === 2 &&- executor.runningDiscovery === 1 &&- [...store.workers.values()].some(- (worker) =>- worker.kind === "discovery" && worker.status === "succeeded",- ),- );- assert.equal(- executor.dedupCalls,- 0,- "the first singleton remains buffered before the deadline",- );-- const terminal = await coordinator.wait(undefined, 5_000);- assert.equal(terminal?.status, "succeeded");- assert.equal(terminal?.terminalReason, "capped");- assert.equal(terminal.dispatchedCount, 2);- assert.equal(- terminal.dispatchedCount < fixture.run.config.maxDiscoveryRuns,- true,- );- assert.equal(store.failureInputs.length, 0);- assert.equal(executor.dedupCalls, 1);- assert.equal(executor.runningDiscovery, 0);-- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));- assert.equal(store.dedupClaims[0].workerIds.length, 1);- assert.deepEqual(store.finishCalls[0].omittedWorkerIds, []);- assert.equal(- [...store.workers.values()].filter(- (worker) => worker.status === "canceled",- ).length,- 1,- );- assert.equal(store.dedupCommits.length, 1);- assert.deepEqual(- manifest.findings.map((finding) => finding.provenance.candidateId),- ["candidate-1"],- );- }-- async function testDiscoveryAcceptedAtDeadlineIsReduced() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 99,- maxDiscoveryRuns: 8,- });- const store = new FakeStore(fixture.run);- const acceptancePersisted = Promise.withResolvers();- const releaseAcceptance = Promise.withResolvers();- const discoveryDeadlineReached = Promise.withResolvers();- const updateWorker = store.updateWorker.bind(store);- store.updateWorker = async (update) => {- const persisted = await updateWorker(update);- if (update.kind === "discovery" && update.status === "succeeded") {- acceptancePersisted.resolve();- await releaseAcceptance.promise;- }- return persisted;- };- const executor = new FakeExecutor({- discoveryCandidateId: "candidate-1",- canonicalCandidateId: "candidate-1",- dedupNewFindings: [1],- });- const coordinator = createCoordinator(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;- releaseAcceptance.resolve();-- const terminal = await terminalWait;- assert.equal(terminal?.status, "succeeded");- assert.equal(terminal?.terminalReason, "capped");- assert.equal(store.failureInputs.length, 0);- assert.equal(executor.discoveryCalls, 1);- assert.equal(executor.dedupCalls, 1);-- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));- assert.equal(store.dedupClaims[0].workerIds.length, 1);- assert.deepEqual(store.finishCalls[0].omittedWorkerIds, []);- assert.equal(- [...store.workers.values()].some(- (worker) => worker.status === "canceled",- ),- false,- );- assert.deepEqual(- manifest.findings.map((finding) => finding.provenance.candidateId),- ["candidate-1"],- );- }-- async function testDiscoveryDeadlineWithoutAcceptedWorkersReturnsPartialEvidence() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 99,- maxDiscoveryRuns: 8,- });- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({ blockDiscovery: true });- const completedDrafts = [];- const coordinator = createCoordinator(fixture, store, executor, {- discoveryTimeoutMs: 500,- onComplete: async (draft) => completedDrafts.push(structuredClone(draft)),- });- coordinator.start();- await executor.discoveryStarted.promise;-- const terminal = await coordinator.wait(undefined, 5_000);- assert.equal(terminal?.status, "succeeded");- assert.equal(terminal?.terminalReason, "capped");- assert.equal(store.failureInputs.length, 0);- assert.equal(store.finishCalls.length, 1);- assert.equal(executor.runningDiscovery, 0);- assert.equal(executor.dedupCalls, 0);-- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));- assert.deepEqual(manifest.findings, []);- assert.equal(manifest.coverage.completeness, "partial");- assert.deepEqual(completedDrafts[0].coverage.deferred, [- {- reason:- "The configured discovery time limit elapsed before any source review completed.",- },- ]);- assert.deepEqual(store.finishCalls[0].omittedWorkerIds, []);- assert.equal(- [...store.workers.values()].filter(- (worker) => worker.status === "canceled",- ).length,- 1,- );- assert.equal(store.dedupCommits.length, 0);- }-- async function testDiscoveryDeadlineBeforeWorkerDispatchReturnsPartialEvidence() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 99,- maxDiscoveryRuns: 8,- maxTimeHours: 1e-12,- });- fixture.run.createdAt = new Date(immediateClock.now()).toISOString();- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor();- const coordinator = createCoordinator(fixture, store, executor, {});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);- assert.equal(terminal?.status, "succeeded");- assert.equal(terminal?.terminalReason, "capped");- assert.equal(store.failureInputs.length, 0);- assert.equal(store.finishCalls.length, 1);- assert.equal(executor.discoveryCalls, 0);- assert.equal(executor.dedupCalls, 0);-- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));- assert.deepEqual(manifest.findings, []);- assert.equal(manifest.coverage.completeness, "partial");- assert.deepEqual(store.finishCalls[0].omittedWorkerIds, []);- assert.equal(store.workers.size, 0);- }-- await testDiscoveryDeadlineDrainsActiveReducerAndPreservesFindings();- await testDiscoveryDeadlineReducesSingleBufferedFinding();- await testDiscoveryAcceptedAtDeadlineIsReduced();- await testDiscoveryDeadlineWithoutAcceptedWorkersReturnsPartialEvidence();- await testDiscoveryDeadlineBeforeWorkerDispatchReturnsPartialEvidence();-}diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_publication_cases.mjs b/plugins/codex-security/mcp-app/tests/deep_scan_publication_cases.mjsdeleted file mode 100644index b0cc82c79..000000000--- a/plugins/codex-security/mcp-app/tests/deep_scan_publication_cases.mjs+++ /dev/null@@ -1,253 +0,0 @@-import assert from "node:assert/strict";-import { readFile, rm, writeFile } from "node:fs/promises";-import path from "node:path";--export async function testDeepScanPublication({- fixtureRun,- FakeStore,- FakeExecutor,- createCoordinator,- eventually,-}) {- async function testSaturationOmitsWorkerAcceptedDuringCancellation() {- const fixture = await fixtureRun({- workers: 3,- subagents: 0,- stopAfterNoNew: 2,- maxDiscoveryRuns: 3,- });- const store = new FakeStore(fixture.run);- const releaseLateWorker = Promise.withResolvers();- const lateAcceptance = Promise.withResolvers();- const releaseAcceptance = Promise.withResolvers();- const updateWorker = store.updateWorker.bind(store);- let acceptedLateWorker;- store.updateWorker = async (update) => {- const persisted = await updateWorker(update);- if (- update.kind === "discovery" &&- update.status === "succeeded" &&- path.basename(path.dirname(update.promptPath)) === "discovery-0003"- ) {- acceptedLateWorker = persisted;- // This worker finishes too late to be included in the final result.- await rm(update.resultManifestPath);- lateAcceptance.resolve();- await releaseAcceptance.promise;- }- return persisted;- };- const executor = new FakeExecutor({- blockDedup: true,- discoveryGates: { "discovery-0003": releaseLateWorker.promise },- discoveryCandidates: { "discovery-0003": "late-accepted-finding" },- });- const completed = [];- const coordinator = createCoordinator(fixture, store, executor, {- onComplete: async (draft) => completed.push(structuredClone(draft)),- });- coordinator.start();- await executor.dedupStarted.promise;- releaseLateWorker.resolve();- await lateAcceptance.promise;- executor.releaseDedup();- await eventually(() => executor.dedupSignal?.aborted === true);- releaseAcceptance.resolve();- const terminal = await coordinator.wait(undefined, 5_000);- assert.equal(terminal?.status, "succeeded", terminal?.error);- assert.equal(terminal.terminalReason, "saturated");- assert.equal(executor.discoveryCalls, 3);- assert.equal(- executor.dedupCalls,- 1,- "late accepted results do not restart convergence",- );- assert.deepEqual(store.finishCalls[0].omittedWorkerIds, [- acceptedLateWorker.id,- ]);- assert.equal(completed.length, 1);- assert.equal(completed[0].coverage.completeness, "complete");- assert.deepEqual(- completed[0].findings,- [],- "late worker findings are not appended to the saturated aggregate",- );- }-- async function testSuccessfulDeepCoverageIgnoresWorkerAndReducerReviewStatus() {- const fixture = await fixtureRun({- workers: 2,- subagents: 0,- stopAfterNoNew: 2,- maxDiscoveryRuns: 2,- });- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor();- const run = executor.run.bind(executor);- const reviewed = { label: "Reviewed query", disposition: "no_issue_found" };- const workerReviewed = {- ...reviewed,- receiptRefs: ["artifacts/missing-worker-receipt.md"],- };- const followUp = {- label: "Worker follow-up",- disposition: "needs_follow_up",- };- executor.run = async (request) => {- const outcome = await run(request);- const resultPath = path.join(request.artifactContext.root, "result.json");- const draft = JSON.parse(await readFile(resultPath, "utf8"));- draft.coverage = {- completeness:- request.kind === "discovery" &&- request.promptPath.includes("discovery-0002")- ? "unknown"- : "partial",- surfaces: [workerReviewed, followUp],- explicitExclusions: [],- deferred: [- { reason: "An independent review left this question unresolved." },- ],- };- await writeFile(resultPath, JSON.stringify(draft));- return outcome;- };- const completed = [];- const coordinator = createCoordinator(fixture, store, executor, {- onComplete: async (draft) => completed.push(structuredClone(draft)),- });- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);- assert.equal(terminal?.status, "succeeded", terminal?.error);- assert.equal(completed.length, 1);- assert.deepEqual(completed[0].coverage, {- completeness: "complete",- surfaces: [],- explicitExclusions: [],- deferred: [],- });- for (const worker of store.workers.values()) {- if (worker.kind !== "discovery") continue;- const draft = JSON.parse(- await readFile(worker.resultManifestPath, "utf8"),- );- assert.notEqual(draft.coverage.completeness, "complete");- assert.deepEqual(draft.coverage.surfaces, [workerReviewed, followUp]);- assert.equal(draft.coverage.deferred.length, 1);- }- }-- async function testSaturationIgnoresDiscoveryCancellationWriteFailure() {- const fixture = await fixtureRun({- workers: 2,- subagents: 0,- stopAfterNoNew: 2,- maxDiscoveryRuns: 6,- });- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({- blockDedup: true,- blockDiscoveryAfterCalls: 2,- });- const updateWorker = store.updateWorker.bind(store);- const rejectedCancellations = new Set();- store.updateWorker = async (update) => {- if (update.kind === "discovery" && update.status === "canceled") {- assert.equal(executor.dedupSignal?.aborted, true);- assert.equal(store.run.noNewStreak, 2);- rejectedCancellations.add(update.id);- throw new Error("fixture cancellation persistence failure");- }- return updateWorker(update);- };- const completed = [];- const coordinator = createCoordinator(fixture, store, executor, {- onComplete: async (draft) => completed.push(structuredClone(draft)),- });- coordinator.start();- await executor.dedupStarted.promise;- await eventually(- () => executor.discoveryCalls === 4 && executor.runningDiscovery === 2,- );- executor.releaseDedup();-- const terminal = await coordinator.wait(undefined, 5_000);- assert.equal(- rejectedCancellations.size,- 2,- "the redundant discoveries reached the failing cancellation write",- );- assert.equal(terminal?.status, "succeeded", terminal?.error);- assert.equal(terminal.terminalReason, "saturated");- assert.equal(store.failureInputs.length, 0);- assert.equal(store.finishCalls.length, 1);- assert.equal(store.finishCalls[0].reason, "saturated");- assert.equal(- executor.discoveryCalls,- 4,- "cancellation persistence failures must not dispatch replacement reviews after saturation",- );- assert.equal(- executor.dedupCalls,- 1,- "cancellation persistence failures must not restart reduction after saturation",- );- assert.equal(executor.runningDiscovery, 0);- assert.equal(completed.length, 1);- assert.equal(completed[0].coverage.completeness, "complete");- const acceptedReducer = [...store.workers.values()].find(- (worker) => worker.kind === "dedup" && worker.status === "succeeded",- );- const { coverage, ...publishedReduction } = completed[0];- assert.deepEqual(- publishedReduction,- JSON.parse(await readFile(acceptedReducer.resultManifestPath, "utf8")),- "the accepted aggregate still reaches publication when redundant cancellation writes fail",- );- }-- async function testPublicationUsesAcceptedReducerSnapshot() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);- const commitDedup = store.commitDedup.bind(store);- store.commitDedup = async (commit) => {- const accepted = await commitDedup(commit);- await rm(commit.resultManifestPath);- return accepted;- };- store.finish = async (input) => {- store.finishCalls.push(input);- Object.assign(store.run, {- status: "succeeded",- terminalReason: input.reason,- manifestPath: input.manifestPath,- });- return structuredClone(store.run);- };- const completed = [];- const coordinator = createCoordinator(- fixture,- store,- new FakeExecutor({ discoveryCandidateId: "accepted-finding" }),- { onComplete: async (draft) => completed.push(structuredClone(draft)) },- );- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);- assert.equal(terminal?.status, "succeeded", terminal?.error);- assert.equal(- completed[0].findings[0].provenance.candidateId,- "accepted-finding",- );- assert.equal(completed[0].coverage.completeness, "complete");- }-- await testSaturationOmitsWorkerAcceptedDuringCancellation();- await testSuccessfulDeepCoverageIgnoresWorkerAndReducerReviewStatus();- await testSaturationIgnoresDiscoveryCancellationWriteFailure();- await testPublicationUsesAcceptedReducerSnapshot();-}diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_worker_failure_cases.mjs b/plugins/codex-security/mcp-app/tests/deep_scan_worker_failure_cases.mjsdeleted file mode 100644index 4dab534d2..000000000--- a/plugins/codex-security/mcp-app/tests/deep_scan_worker_failure_cases.mjs+++ /dev/null@@ -1,285 +0,0 @@-import assert from "node:assert/strict";-import path from "node:path";-import { readFile } from "node:fs/promises";--export function createDeepScanWorkerFailureCases({- fixtureRun,- FakeStore,- FakeExecutor,- createCoordinator,- DeepScanCoordinator,- DeepScanNonRetryableError,- classifyCodexWorkerError,- immediateClock,- recordingClock,- workerIdFromPrompt,- promptContext,-}) {- async function testResumeRequiresHistoricalWorkerPrompt(status) {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 2,- maxDiscoveryRuns: 2,- });- const promptPath = path.join(fixture.run.scanDir, "missing-prompt.md");- const store = new FakeStore({- ...fixture.run,- persistedWorkers: [- {- id: "historical-worker",- kind: "discovery",- status,- attempt: 1,- promptPath,- artifactDir: fixture.run.scanDir,- },- ],- });- const executor = new FakeExecutor();- const coordinator = new DeepScanCoordinator({- 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));- assert.equal(executor.discoveryCalls, 0);- }-- async function testRecoverableWorkerErrorsCannotFailScan() {- const failures = ["config unknown", "authentication required"].map(- (output) =>- new Error(- `Failed to parse item: ${JSON.stringify({- type: "item.completed",- item: {- id: "fixture-command",- type: "command_execution",- command: "synthetic-command",- aggregated_output: output,- },- })}`,- ),- );- failures.push(- ...["ENOENT", "EACCES", "ENOEXEC", "EPERM"].map((code) =>- Object.assign(new Error("fixture worker I/O failure"), { code }),- ),- );- for (const failure of failures) {- const fixture = await fixtureRun({- workers: 2,- subagents: 0,- stopAfterNoNew: 4,- stopAfterConsecutiveErrors: 2,- maxDiscoveryRuns: 4,- });- const store = new FakeStore(fixture.run);- const normalExecutor = new FakeExecutor();- const attempts = [];- const events = [];- const coordinator = createCoordinator(- fixture,- store,- {- async run(request) {- if (- request.kind === "discovery" &&- (await workerIdFromPrompt(request.promptPath)) ===- "discovery-0001"- ) {- attempts.push(request.resumeThreadId);- await request.onThreadStarted?.(- request.resumeThreadId ?? "fixture-parse-thread",- );- throw classifyCodexWorkerError(failure);- }- return normalExecutor.run(request);- },- },- { retryDelaysMs: [1, 3, 9], log: (event) => events.push(event) },- );- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);-- assert.equal(terminal?.status, "succeeded", terminal?.error);- assert.deepEqual(attempts, [- undefined,- "fixture-parse-thread",- "fixture-parse-thread",- "fixture-parse-thread",- ]);- const discoveries = [...store.workers.values()].filter(- (worker) => worker.kind === "discovery",- );- assert.equal(- discoveries.filter((worker) => worker.status === "succeeded").length,- 3,- "the sibling and subsequent discoveries must finish",- );- assert.equal(- discoveries.filter((worker) => worker.status === "failed").length,- 0,- );- assert.equal(- discoveries.find((worker) => worker.status === "canceled")- ?.replaceableFailureKind,- "transient_error",- );- assert.equal(- events.filter((event) => event.event === "discovery_worker_replaced")- .length,- 1,- );- assert.equal(store.failureInputs.length, 0);- }- }-- async function testPolicyRefusedReducerPreservesInputsAndCommittedAggregate() {- const fixture = await fixtureRun({- workers: 2,- subagents: 0,- stopAfterNoNew: 10,- stopAfterConsecutiveErrors: 2,- maxDiscoveryRuns: 4,- });- const store = new FakeStore(fixture.run);- const nextDiscovery = Promise.withResolvers();- const siblingDiscovery = Promise.withResolvers();- const normalExecutor = new FakeExecutor({- discoveryCandidateId: "candidate-1",- canonicalCandidateId: "candidate-1",- discoveryGates: {- "discovery-0003": nextDiscovery.promise,- "discovery-0004": siblingDiscovery.promise,- },- });- let committedResultPath;- let committedContent;- const failedAttempts = [];- const coordinator = createCoordinator(- fixture,- store,- {- async run(request) {- if (request.kind === "dedup") {- const label = (await promptContext(request.promptPath))- .reducerLabel;- if (label === "dedup-0002") {- failedAttempts.push(request.resumeThreadId);- await request.onThreadStarted?.("fixture-retired-reducer");- throw classifyCodexWorkerError(- new Error("Request blocked by cyberPolicy."),- );- }- if (label === "dedup-0003") {- assert.equal(request.resumeThreadId, undefined);- assert.equal(- request.artifactContext.deepReducer.previousReducerResultPath,- committedResultPath,- );- assert.deepEqual(- store.dedupClaims[2].workerIds,- store.dedupClaims[1].workerIds,- "the replacement must receive every uncommitted input",- );- assert.deepEqual(- await readFile(committedResultPath),- committedContent,- );- siblingDiscovery.resolve();- }- }- return normalExecutor.run(request);- },- },- { retryDelaysMs: [1, 3, 9] },- );- coordinator.start();- await store.dedupCommitted.promise;- committedResultPath = store.dedupCommits[0].resultManifestPath;- committedContent = await readFile(committedResultPath);- nextDiscovery.resolve();-- const terminal = await coordinator.wait(undefined, 5_000);-- assert.equal(terminal?.status, "succeeded", terminal?.error);- assert.deepEqual(failedAttempts, [undefined]);- const discoveries = [...store.workers.values()].filter(- (worker) => worker.kind === "discovery",- );- assert.equal(discoveries.length, 4);- assert.equal(- discoveries.every((worker) => worker.status === "succeeded"),- true,- );- const retiredReducer = [...store.workers.values()].find(- (worker) => worker.kind === "dedup" && worker.status === "failed",- );- assert.equal(retiredReducer?.attempt, 1);- assert.deepEqual(await readFile(committedResultPath), committedContent);- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));- assert.deepEqual(- manifest.findings.map((finding) => finding.provenance.candidateId),- ["candidate-1"],- );- assert.equal(store.failureInputs.length, 0);- }-- async function testNonRetryableReducerAbortsScanWithoutRetry(- failureMessage = "fixture fatal reducer failure",- ) {- const fixture = await fixtureRun({- workers: 2,- subagents: 0,- stopAfterNoNew: 10,- stopAfterConsecutiveErrors: 2,- maxDiscoveryRuns: 4,- });- const store = new FakeStore(fixture.run);- const normalExecutor = new FakeExecutor({ blockDiscoveryAfterCalls: 2 });- const attempts = [];- const sleeps = [];- const coordinator = createCoordinator(- fixture,- store,- {- async run(request) {- if (request.kind === "dedup") {- attempts.push(request.resumeThreadId);- throw new DeepScanNonRetryableError(failureMessage);- }- return normalExecutor.run(request);- },- },- {- clock: recordingClock(sleeps),- },- );- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);-- assert.equal(terminal?.status, "failed");- assert.equal(terminal?.error, failureMessage);- assert.deepEqual(attempts, [undefined]);- assert.deepEqual(sleeps, []);- assert.equal(normalExecutor.runningDiscovery, 0);- assert.equal(store.failureInputs.length, 1);- assert.equal(store.dedupCommits.length, 0);- }-- return {- testResumeRequiresHistoricalWorkerPrompt,- testRecoverableWorkerErrorsCannotFailScan,- testPolicyRefusedReducerPreservesInputsAndCommittedAggregate,- testNonRetryableReducerAbortsScanWithoutRetry,- };-}diff --git a/plugins/codex-security/mcp-app/tests/import-module.mjs b/plugins/codex-security/mcp-app/tests/import-module.tssimilarity index 55%rename from plugins/codex-security/mcp-app/tests/import-module.mjsrename to plugins/codex-security/mcp-app/tests/import-module.tsindex 5c924efad..efdb75f1f 100644--- a/plugins/codex-security/mcp-app/tests/import-module.mjs+++ b/plugins/codex-security/mcp-app/tests/import-module.ts@@ -1,6 +1,6 @@-import { build } from "esbuild";+import { build, type BuildOptions } from "esbuild";-export async function importModule(options) {+export async function importModule(options: BuildOptions) {const result = await build({bundle: true,format: "esm",@@ -9,10 +9,10 @@ export async function importModule(options) {...options,});return import(- `data:text/javascript;base64,${Buffer.from(result.outputFiles[0].contents).toString("base64")}`+ `data:text/javascript;base64,${Buffer.from(result.outputFiles![0].contents).toString("base64")}`);}-export function importSource(entryPoint, options = {}) {+export function importSource(entryPoint: string, options: BuildOptions = {}) {return importModule({ entryPoints: [entryPoint], ...options });}diff --git a/plugins/codex-security/mcp-app/tests/sandbox-state.mjs b/plugins/codex-security/mcp-app/tests/sandbox-state.tssimilarity index 85%rename from plugins/codex-security/mcp-app/tests/sandbox-state.mjsrename to plugins/codex-security/mcp-app/tests/sandbox-state.tsindex db6d477ec..5e6b88bdc 100644--- a/plugins/codex-security/mcp-app/tests/sandbox-state.mjs+++ b/plugins/codex-security/mcp-app/tests/sandbox-state.ts@@ -1,6 +1,6 @@import { pathToFileURL } from "node:url";-export function readOnlyParentSandboxState(pluginRoot) {+export function readOnlyParentSandboxState(pluginRoot: string) {return {permissionProfile: {type: "managed",diff --git a/plugins/codex-security/mcp-app/tests/scan-draft-fixture.mjs b/plugins/codex-security/mcp-app/tests/scan-draft-fixture.tssimilarity index 82%rename from plugins/codex-security/mcp-app/tests/scan-draft-fixture.mjsrename to plugins/codex-security/mcp-app/tests/scan-draft-fixture.tsindex 4c2a21a90..4ac21d338 100644--- a/plugins/codex-security/mcp-app/tests/scan-draft-fixture.mjs+++ b/plugins/codex-security/mcp-app/tests/scan-draft-fixture.ts@@ -1,6 +1,9 @@export const scanId = "7fc17317-9594-49e0-b06a-d72fd7e14bba";-export function workerDraft(findings, extra = {}) {+export function workerDraft<Finding extends Record<string, unknown>>(+ findings: Finding[],+ extra = {},+) {return {scanId,findings,@@ -14,7 +17,7 @@ export function workerDraft(findings, extra = {}) {};}-export function finding(id, repositoryPath) {+export function finding(id: string, repositoryPath: string) {return {ruleId: "cross-site-scripting." + id,identity: { anchor: id },diff --git a/plugins/codex-security/mcp-app/tests/scan-draft-recovery-fixture.mjs b/plugins/codex-security/mcp-app/tests/scan-draft-recovery-fixture.tssimilarity index 61%rename from plugins/codex-security/mcp-app/tests/scan-draft-recovery-fixture.mjsrename to plugins/codex-security/mcp-app/tests/scan-draft-recovery-fixture.tsindex 49d8699ff..67cf7c82a 100644--- a/plugins/codex-security/mcp-app/tests/scan-draft-recovery-fixture.mjs+++ b/plugins/codex-security/mcp-app/tests/scan-draft-recovery-fixture.ts@@ -1,19 +1,23 @@+import { temporaryDirectory } from "./support/temporary-directories.ts";+import { readJson } from "./support/json.ts";import assert from "node:assert/strict";import { promises as fs } from "node:fs";-import { mkdir, mkdtemp, readFile, realpath, rm } from "node:fs/promises";-import { tmpdir } from "node:os";+import { mkdir, rm } from "node:fs/promises";import path from "node:path";-import { fileURLToPath } from "node:url";-import { importModule } from "./import-module.mjs";-export const draftApi = await importModule({- absWorkingDir: path.dirname(fileURLToPath(import.meta.url)),- entryPoints: ["../src/artifact-scan-draft.ts"],+import type { TestContext } from "node:test";+import type { ScanDraftInput } from "../src/artifact-scan-draft.js";+import type { ArtifactContext } from "../src/artifact-io.js";+import { importSource } from "./import-module.ts";+export const draftApi = await importSource("../src/artifact-scan-draft.ts", {+ absWorkingDir: import.meta.dirname,});export const scanId = "7b95abf2-dc04-47a9-9950-53b5c2057f49";export const claimToken = "19bfba38-0913-4bd7-86ef-134e9a4d9a42";-export function draftFixture(root, layout) {- const context = {+type Layout = "standard" | "diff" | "deep" | "worker";++export function draftFixture(root: string, layout: Layout) {+ const context: ArtifactContext = {root,repoRoot: root,scanId,@@ -42,7 +46,10 @@ export function draftFixture(root, layout) {: null,},};- const draft = (coverage = {}, complete = false) => ({+ const draft = (+ coverage: { deferred?: unknown[]; [key: string]: unknown } = {},+ complete = false,+ ): ScanDraftInput => ({scanId,...(layout === "worker" ? {} : { handoffClaimToken: claimToken }),complete,@@ -60,36 +67,29 @@ export function draftFixture(root, layout) {root,context,draft,- write: (input) =>+ write: (input: ScanDraftInput) =>layout === "worker"? draftApi.recordCodexSecurityWorkerScanDraft(context, input): draftApi.recordCodexSecurityScanDraft(context, input),- read: async () => {- const value = JSON.parse(- await readFile(- path.join(- root,- layout === "worker" ? "result.json" : "coverage.json",- ),- "utf8",- ),- );- return layout === "worker" ? value.coverage : value;- },+ read: async () =>+ layout === "worker"+ ? (await readJson(root, "result.json")).coverage+ : await readJson(root, "coverage.json"),};}-export async function fixture(t, layout) {- const directory = await realpath(- await mkdtemp(path.join(tmpdir(), "draft-recovery-")),- );+export async function fixture(t: TestContext, layout: Layout) {+ const directory = await temporaryDirectory("draft-recovery-", true);t.after(() => rm(directory, { recursive: true, force: true }));const root = path.join(directory, "output");await mkdir(root);return draftFixture(root, layout);}-export async function interruptDraftWrite(destination, action) {+export async function interruptDraftWrite(+ destination: string,+ action: () => Promise<unknown>,+) {const rename = fs.rename;fs.rename = async (source, target) => {if (target === destination) throw new Error("interrupted draft write");@@ -101,3 +101,11 @@ export async function interruptDraftWrite(destination, action) {fs.rename = rename;}}++export const surfaceDisposition = ({+ id,+ disposition,+}: {+ id: string;+ disposition: string;+}) => ({ id, disposition });diff --git a/plugins/codex-security/mcp-app/tests/support/json.ts b/plugins/codex-security/mcp-app/tests/support/json.tsnew file mode 100644index 000000000..04d2f4830--- /dev/null+++ b/plugins/codex-security/mcp-app/tests/support/json.ts@@ -0,0 +1,38 @@+import { readFile, writeFile } from "node:fs/promises";+import { join } from "node:path";++export async function readJson(file: string, ...parts: string[]) {+ return JSON.parse(+ await readFile(parts.length ? join(file, ...parts) : file, "utf8"),+ );+}++export async function snapshotScanDraft(scanDirectory: string) {+ return Promise.all(+ ["scan-manifest.json", "findings.json", "coverage.json"].map((artifact) =>+ readFile(join(scanDirectory, artifact), "utf8").catch((error) => {+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;+ throw error;+ }),+ ),+ );+}++export async function readJsonLines(file: string) {+ return (await readFile(file, "utf8"))+ .split(/\r?\n/)+ .filter(Boolean)+ .map((line) => JSON.parse(line));+}++export function jsonLines(rows: unknown[]) {+ return rows.map((row) => `${JSON.stringify(row)}\n`).join("");+}++export function writeJson(file: string, value: unknown) {+ return writeFile(file, JSON.stringify(value));+}++export function writeJsonLine(file: string, value: unknown) {+ return writeFile(file, `${JSON.stringify(value)}\n`);+}diff --git a/plugins/codex-security/mcp-app/tests/support/reducer-paging/controlled-code-mode.mjs b/plugins/codex-security/mcp-app/tests/support/reducer-paging/controlled-code-mode.tssimilarity index 73%rename from plugins/codex-security/mcp-app/tests/support/reducer-paging/controlled-code-mode.mjsrename to plugins/codex-security/mcp-app/tests/support/reducer-paging/controlled-code-mode.tsindex 7af70e079..58ceb79a8 100644--- a/plugins/codex-security/mcp-app/tests/support/reducer-paging/controlled-code-mode.mjs+++ b/plugins/codex-security/mcp-app/tests/support/reducer-paging/controlled-code-mode.ts@@ -1,11 +1,20 @@-import { temporaryDirectory } from "../temporary-directories.mjs";+import { temporaryDirectory } from "../temporary-directories.ts";+import type { ChildProcessByStdio } from "node:child_process";+import type { Readable } from "node:stream";+import type { AddressInfo } from "node:net";+interface ToolOutput {+ call_id: string;+ type: string;+ output: string | { text: string }[];+}import { spawn } from "node:child_process";-import { mkdir, rm } from "node:fs/promises";+import { once } from "node:events";+import { rm } from "node:fs/promises";import { createServer } from "node:http";import { createRequire } from "node:module";import path from "node:path";-function responseEvents(id, item) {+function responseEvents(id: string, item: unknown) {return [{ type: "response.created", response: { id } },{ type: "response.output_item.done", item },@@ -30,9 +39,14 @@ function responseEvents(id, item) {/** Execute one scripted cell through the real CLI, external code host, and MCP. */export async function runControlledCodeMode({code,- mcpServers,+ mcpServer,workingDirectory,- prompt = "Run the deterministic transport eval.",+ prompt,+}: {+ code: string;+ mcpServer: { command: string; args: string[] };+ workingDirectory: string;+ prompt: string;}) {// Resolve through the SDK so the launcher and native host use its pinned version.const sdkRequire = createRequire(import.meta.resolve("@openai/codex-sdk"));@@ -41,14 +55,13 @@ export async function runControlledCodeMode({"bin","codex.js",);- const fixtureRoot = await temporaryDirectory("codex-controlled-ipc-");- const codexHome = path.join(fixtureRoot, "home");- const toolOutputs = new Map();- const serverErrors = [];+ const codexHome = await temporaryDirectory("codex-controlled-ipc-");+ const toolOutputs = new Map<string, ToolOutput>();+ const serverErrors: string[] = [];let responseCount = 0;let pendingCallId = "controlled-exec";- let child;- let closed;+ let child: ChildProcessByStdio<null, null, Readable> | undefined;+ let closed: Promise<number | null> | undefined;let stderr = "";const server = createServer(async (request, response) => {try {@@ -59,8 +72,7 @@ export async function runControlledCodeMode({response.end("{}");return;}- const chunks = [];- for await (const chunk of request) chunks.push(chunk);+ const chunks = await request.toArray();const body = JSON.parse(Buffer.concat(chunks).toString("utf8"));for (const item of body.input ?? []) {if (@@ -106,36 +118,25 @@ export async function runControlledCodeMode({}});try {- await mkdir(codexHome, { mode: 0o700 });- await new Promise((resolve, reject) => {+ await new Promise<void>((resolve, reject) => {server.once("error", reject);server.listen(0, "127.0.0.1", resolve);});- const port = server.address().port;+ const port = (server.address() as AddressInfo).port;const config = ['model_provider="controlled"',`model_providers.controlled={name="controlled",base_url="http://127.0.0.1:${port}/v1",wire_api="responses",requires_openai_auth=false,supports_websockets=false}`,"features.code_mode.enabled=true","features.code_mode_host.enabled=true","features.code_mode_host.disable_in_process_fallback=true",+ `mcp_servers.cs_artifacts.command=${JSON.stringify(mcpServer.command)}`,+ `mcp_servers.cs_artifacts.args=${JSON.stringify(mcpServer.args)}`,];- for (const [name, entry] of Object.entries(mcpServers)) {- const prefix = `mcp_servers.${name}`;- config.push(`${prefix}.command=${JSON.stringify(entry.command)}`);- config.push(`${prefix}.args=${JSON.stringify(entry.args ?? [])}`);- if (entry.env) {- config.push(- `${prefix}.env={${Object.entries(entry.env)- .map(- ([key, value]) =>- `${JSON.stringify(key)}=${JSON.stringify(value)}`,- )- .join(",")}}`,- );- }- }// Pass only process-launch essentials; never inherit the caller's model auth.- const env = { CODEX_HOME: codexHome, CODEX_SQLITE_HOME: codexHome };+ const env: NodeJS.ProcessEnv = {+ CODEX_HOME: codexHome,+ CODEX_SQLITE_HOME: codexHome,+ };for (const key of ["PATH","SystemRoot",@@ -161,32 +162,27 @@ export async function runControlledCodeMode({prompt,];child = spawn(process.execPath, args, {- cwd: workingDirectory ?? fixtureRoot,+ cwd: workingDirectory,env,// JSONL stdout can duplicate the deliberately oversized MCP response.stdio: ["ignore", "ignore", "pipe"],});- closed = new Promise((resolve) => child.once("close", resolve));+ closed = new Promise((resolve) => child!.once("close", resolve));child.stderr.setEncoding("utf8").on("data", (chunk) => {stderr += chunk;});- const outcome = await new Promise((resolve, reject) => {- child.once("error", reject);- child.once("close", (code, signal) => resolve({ code, signal }));- });+ const exitCode: number | null = (await once(child, "close"))[0];return {- outcome,+ exitCode,toolOutputs: [...toolOutputs.values()],stderr,serverErrors,};} finally {- if (child && child.exitCode === null && child.signalCode === null) {- child.kill();- }+ child?.kill();if (closed) await closed;server.closeAllConnections();await new Promise((resolve) => server.close(resolve));- await rm(fixtureRoot, { recursive: true, force: true });+ await rm(codexHome, { recursive: true, force: true });}}diff --git a/plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging-fixture.mjs b/plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging-fixture.tssimilarity index 80%rename from plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging-fixture.mjsrename to plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging-fixture.tsindex 60a76de6c..75c635a18 100644--- a/plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging-fixture.mjs+++ b/plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging-fixture.ts@@ -1,16 +1,16 @@+import { readJson, writeJson } from "../json.ts";import assert from "node:assert/strict";import { createHash } from "node:crypto";-import { mkdir, readFile, realpath, writeFile } from "node:fs/promises";+import { mkdir, realpath } from "node:fs/promises";import path from "node:path";-import { sourceReferences } from "../source-references.mjs";+import { sourceReferences } from "../source-references.ts";// This is the existing code-mode transport ceiling, used only to size the eval.const IPC_FRAME_LIMIT_BYTES = 64 * 1024 * 1024;const SCAN_ID = "7fc17317-9594-49e0-b06a-d72fd7e14bba";/** Generate one reducer assignment; no repository scan or external service runs. */-export async function createReducerPagingFixture(root) {- await mkdir(root, { recursive: true });+export async function createReducerPagingFixture(root: string) {const fixtureRoot = await realpath(root);const scanRoot = path.join(fixtureRoot, "scan");const deepRoot = path.join(scanRoot, "artifacts", "deep_discovery");@@ -41,11 +41,11 @@ export async function createReducerPagingFixture(root) {"The account lookup builds a query by concatenating request-controlled text. Parameter binding is absent on this path.",remediation: "Use parameter binding for account lookup query values.",});- const synthesizedHistory = finding("query-injection", {+ const synthesizedHistory = {...previousOriginal,summary:"Earlier synthesis also established that the transactional account lookup calls the same unsafe query builder. Both paths need the shared parameter-binding fix.",- });+ };const previousSourceId = "previous-worker:0";const previousCanonical = {...previousOriginal,@@ -60,8 +60,10 @@ export async function createReducerPagingFixture(root) {};const workerId = "current-worker";const currentFindings = [freshHtml, freshArchive];- const expectedSources = new Map([- ...currentFindings.map((value, index) => [`${workerId}:${index}`, value]),+ const expectedSources = new Map<string, ReturnType<typeof finding>>([+ ...currentFindings.map(+ (value, index) => [`${workerId}:${index}`, value] as const,+ ),[previousSourceId, previousOriginal],]);const sourceSha256ById = Object.fromEntries(@@ -84,19 +86,16 @@ export async function createReducerPagingFixture(root) {};const workerPath = path.join(workerRoot, "result.json");const previousPath = path.join(previousRoot, "result.json");- await writeFile(- workerPath,- JSON.stringify({- ...result,- coverage: {- completeness: "complete",- surfaces: [],- explicitExclusions: [],- deferred: [],- },- }),- );- await writeFile(previousPath, JSON.stringify(previous));+ await writeJson(workerPath, {+ ...result,+ coverage: {+ completeness: "complete",+ surfaces: [],+ explicitExclusions: [],+ deferred: [],+ },+ });+ await writeJson(previousPath, previous);const logicalInputs = {discoveries: [@@ -127,7 +126,7 @@ export async function createReducerPagingFixture(root) {root: outputRoot,repoRoot: fixtureRoot,scanId: SCAN_ID,- layout: "reducer",+ layout: "reducer" as const,deepReducer: {scanRoot,claimedWorkers: [{ id: workerId, resultPath: workerPath }],@@ -141,7 +140,6 @@ export async function createReducerPagingFixture(root) {[...expectedSources].map(([id, value]) => [id, value.identity]),),previousSourceId,- previousIdentity: previousOriginal.identity,historySha256: canonicalSha256(synthesizedHistory),},measurements: {@@ -154,8 +152,10 @@ export async function createReducerPagingFixture(root) {}/** Fail on lost, duplicated, substituted, or de-identified reducer evidence. */-export async function gradeReducerPagingResult(fixture) {- const result = JSON.parse(await readFile(fixture.resultPath, "utf8"));+export async function gradeReducerPagingResult(+ fixture: Awaited<ReturnType<typeof createReducerPagingFixture>>,+) {+ const result = await readJson(fixture.resultPath);assert.equal(result.scanId, fixture.context.scanId);assert.notEqual(result.complete, false);assert.ok(Array.isArray(result.findings));@@ -166,7 +166,6 @@ export async function gradeReducerPagingResult(fixture) {"the three independent issues must remain separate output findings",);const refs = [];- const retainedIds = [];for (const finding of result.findings) {const provenance = finding.provenance;assert.ok(Array.isArray(provenance?.sourceFindingIds));@@ -184,56 +183,55 @@ export async function gradeReducerPagingResult(fixture) {assert.ok(Array.isArray(provenance?.sourceFindings));refs.push(...provenance.sourceFindingIds);assert.deepEqual(- provenance.sourceFindings.map((source) => source.id).sort(),+ provenance.sourceFindings+ .map((source: { id: string }) => source.id)+ .sort(),[...provenance.sourceFindingIds].sort(),"each output finding must retain exactly its attributed originals",);- for (const source of provenance.sourceFindings) {- assert.equal(- canonicalSha256(source.finding),- fixture.expected.sourceSha256ById[source.id],- `original evidence changed for ${source.id}`,- );- retainedIds.push(source.id);- }+ const [source] = provenance.sourceFindings;+ assert.equal(+ canonicalSha256(source.finding),+ fixture.expected.sourceSha256ById[source.id],+ `original evidence changed for ${source.id}`,+ );}assert.deepEqual(refs.sort(),expectedIds,"each assigned reference must occur exactly once",);- assert.deepEqual(- retainedIds.sort(),- expectedIds,- "each original must be retained exactly once",- );- const previous = result.findings.find((finding) =>- finding.provenance.sourceFindingIds.includes(- fixture.expected.previousSourceId,- ),+ const previous = result.findings.find(+ (finding: { provenance: { sourceFindingIds: string[] } }) =>+ finding.provenance.sourceFindingIds.includes(+ fixture.expected.previousSourceId,+ ),);- assert.deepEqual(previous.identity, fixture.expected.previousIdentity);assert.ok(previous.provenance.previousFindings?.some(- (history) => canonicalSha256(history) === fixture.expected.historySha256,+ (history: unknown) =>+ canonicalSha256(history) === fixture.expected.historySha256,),"the prior synthesized history must survive losslessly",);return {findingCount: result.findings.length,accountedSourceCount: refs.length,- preservedOriginalCount: retainedIds.length,+ preservedOriginalCount: refs.length,previousIdentityPreserved: true,synthesizedHistoryPreserved: true,};}-function finding(id, extra = {}) {+function finding(+ id: "reflected-html" | "archive-path" | "query-injection",+ extra: Record<"title" | "summary" | "remediation", string>,+) {return {ruleId: "synthetic." + id,identity: { anchor: id },- title: "Synthetic finding " + id,- summary: "An untrusted input reaches a sensitive operation.",+ title: extra.title,+ summary: extra.summary,severity: { level: "high" },confidence: { level: "high", rationale: "Synthetic validated fixture." },taxonomy: {@@ -247,13 +245,12 @@ function finding(id, extra = {}) {],},locations: [{ path: `src/${id}.ts`, startLine: 1, endLine: 2 }],- remediation: "Apply the issue-specific boundary check.",+ remediation: extra.remediation,provenance: { source: "local_plugin" },- ...extra,};}-function paddedSummary(bytes) {+function paddedSummary(bytes: number) {const beginning ="BEGIN ISSUE: Request text reaches an HTML response without encoding. The following repeated text is synthetic padding to exercise byte-aware paging, not additional evidence.\n";const ending =@@ -270,13 +267,13 @@ function paddedSummary(bytes) {);}-function canonicalSha256(value) {+function canonicalSha256(value: unknown) {const hash = createHash("sha256");visitJson(value, (chunk) => hash.update(chunk), true);return hash.digest("hex");}-function jsonMetrics(value) {+function jsonMetrics(value: unknown) {let bytes = 0;let stringEscapes = 0;visitJson(value, (chunk) => {@@ -290,7 +287,11 @@ function jsonMetrics(value) {// Hash and measure one scalar at a time instead of retaining another complete// copy of the oversized document. Fixture values contain only JSON types.-function visitJson(value, emit, sorted = false) {+function visitJson(+ value: unknown,+ emit: (chunk: string) => void,+ sorted = false,+) {if (Array.isArray(value)) {emit("[");value.forEach((item, index) => {@@ -306,10 +307,10 @@ function visitJson(value, emit, sorted = false) {if (index > 0) emit(",");emit(JSON.stringify(key));emit(":");- visitJson(value[key], emit, sorted);+ visitJson((value as Record<string, unknown>)[key], emit, sorted);});emit("}");} else {- emit(JSON.stringify(value));+ emit(JSON.stringify(value)!);}}diff --git a/plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging-server.mjs b/plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging-server.tssimilarity index 58%rename from plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging-server.mjsrename to plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging-server.tsindex 97c42aebb..54a584d36 100644--- a/plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging-server.mjs+++ b/plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging-server.ts@@ -1,17 +1,22 @@-import { appendFile, readFile } from "node:fs/promises";+import { readJson } from "../json.ts";+import type { TextContent } from "@modelcontextprotocol/sdk/types.js";+import type { ZodRawShapeCompat } from "@modelcontextprotocol/sdk/server/zod-compat.js";+import { appendFile } from "node:fs/promises";import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";-import { getCodexSecurityDeepReducerInputs } from "../../../src/artifact-deep-reducer.ts";-import { registerCompactWorkerArtifactTools } from "../../../src/server/compact-artifact-tools.ts";+import { getCodexSecurityDeepReducerInputs } from "../../../src/artifact-deep-reducer.js";+import { registerCompactWorkerArtifactTools } from "../../../src/server/compact-artifact-tools.js";async function main() {- const { context, tracePath } = JSON.parse(- await readFile(process.argv[2], "utf8"),- );+ const { context, tracePath } = await readJson(process.argv[2]);const server = new McpServer({name: "deep-reducer-paging-eval",version: "1",});+ type RegisterTool = typeof server.registerTool<+ ZodRawShapeCompat,+ ZodRawShapeCompat+ >;let injected = false;let sequence = 0;@@ -19,7 +24,7 @@ async function main() {// returns the old duplicated result, forcing a real code-mode transport error.registerCompactWorkerArtifactTools({- registerTool(name, definition, handler) {+ registerTool(...[name, definition, handler]: Parameters<RegisterTool>) {return server.registerTool(name, definition, async (input, extra) => {const id = ++sequence;const read = name === "get_codex_security_deep_reducer_inputs";@@ -27,7 +32,12 @@ async function main() {event: "request",id,tool: name,- ...(read ? { input } : { findingCount: input.findings?.length }),+ ...(read+ ? { input }+ : {+ findingCount: (input.findings as unknown[] | undefined)+ ?.length,+ }),});try {let result;@@ -36,8 +46,10 @@ async function main() {injected = true;const full = await getCodexSecurityDeepReducerInputs(context);result = {- content: [{ type: "text", text: JSON.stringify(full) }],- structuredContent: full,+ content: [+ { type: "text" as const, text: JSON.stringify(full) },+ ],+ structuredContent: full as unknown as Record<string, unknown>,};} else {result = await handler(input, extra);@@ -48,23 +60,31 @@ async function main() {injected: inject,bytes: Buffer.byteLength(JSON.stringify(result)),...(read && !inject- ? { nextCursor: JSON.parse(result.content[0].text).nextCursor }+ ? {+ nextCursor: JSON.parse(+ (result.content[0] as TextContent).text,+ ).nextCursor,+ }: {}),});return result;} catch (error) {- await trace({ event: "error", id, message: error.message });+ await trace({+ event: "error",+ id,+ message: (error as Error).message,+ });throw error;}});},- },+ } as unknown as McpServer,context,);await server.connect(new StdioServerTransport());- async function trace(event) {+ async function trace(event: Record<string, unknown>) {await appendFile(tracePath, JSON.stringify(event) + "\n");}}diff --git a/plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging.mjs b/plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging.tssimilarity index 74%rename from plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging.mjsrename to plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging.tsindex 06ee9aaba..1c23832ee 100644--- a/plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging.mjs+++ b/plugins/codex-security/mcp-app/tests/support/reducer-paging/deep-reducer-paging.ts@@ -1,38 +1,46 @@+import { writeJson } from "../json.ts";+import type { DeepReducerPageInput } from "../../../src/artifact-deep-reducer-pages.js";+export interface ReducerTraceEvent {+ event: string;+ id: number;+ tool?: string;+ input?: DeepReducerPageInput;+ bytes?: number;+ injected?: boolean;+ nextCursor?: string;+}import assert from "node:assert/strict";import { readFile, writeFile } from "node:fs/promises";import path from "node:path";-import { fileURLToPath, pathToFileURL } from "node:url";import { Codex } from "@openai/codex-sdk";import { build } from "esbuild";-import { runControlledCodeMode } from "./controlled-code-mode.mjs";+import { importSource } from "../../import-module.ts";+import { runControlledCodeMode } from "./controlled-code-mode.ts";import {createReducerPagingFixture,gradeReducerPagingResult,-} from "./deep-reducer-paging-fixture.mjs";--const supportDirectory = path.dirname(fileURLToPath(import.meta.url));+} from "./deep-reducer-paging-fixture.ts";/** Run one reducer through real code-mode IPC and the production artifact tools. */export async function runReducerPagingEval({root,mode = "deterministic",model,+}: {+ root: string;+ mode?: "deterministic" | "model";+ model?: string;}) {- assert.ok(mode === "deterministic" || mode === "model");const fixture = await createReducerPagingFixture(root);const tracePath = path.join(root, "tool-trace.jsonl");const serverConfigPath = path.join(root, "server-config.json");const serverPath = path.join(root, "artifact-server.cjs");- const promptModulePath = path.join(root, "prompt.mjs");await writeFile(tracePath, "");- await writeFile(- serverConfigPath,- JSON.stringify({ context: fixture.context, tracePath }),- );- await Promise.all([+ await writeJson(serverConfigPath, { context: fixture.context, tracePath });+ const [, { renderDedupPrompt }] = await Promise.all([build({entryPoints: [- path.join(supportDirectory, "deep-reducer-paging-server.mjs"),+ path.join(import.meta.dirname, "deep-reducer-paging-server.ts"),],outfile: serverPath,bundle: true,@@ -40,20 +48,11 @@ export async function runReducerPagingEval({format: "cjs",loader: { ".md": "text" },}),- build({- entryPoints: [- path.join(supportDirectory, "../../../src/deep-scan/templates.ts"),- ],- outfile: promptModulePath,- bundle: true,- platform: "node",- format: "esm",- loader: { ".md": "text" },- }),+ importSource(+ path.join(import.meta.dirname, "../../../src/deep-scan/templates.ts"),+ { loader: { ".md": "text" } },+ ),]);- const { renderDedupPrompt } = await import(- pathToFileURL(promptModulePath).href- );const claimedWorkerIds = fixture.context.deepReducer.claimedWorkers.map((worker) => worker.id,);@@ -62,23 +61,21 @@ export async function runReducerPagingEval({reducerLabel: "paging-eval",claimedWorkerIds,});- const mcpServers = {- cs_artifacts: {- command: process.execPath,- args: [serverPath, serverConfigPath],- required: true,- },+ const mcpServer = {+ command: process.execPath,+ args: [serverPath, serverConfigPath],+ required: true,};let transport;let usage;if (mode === "deterministic") {transport = await runControlledCodeMode({code: controlledReducerCode,- mcpServers,+ mcpServer,workingDirectory: fixture.context.repoRoot,prompt,});- assert.equal(transport.outcome.code, 0, transport.stderr);+ assert.equal(transport.exitCode, 0, transport.stderr);assert.deepEqual(transport.serverErrors, []);const output = JSON.stringify(transport.toolOutputs);assert.match(@@ -90,7 +87,7 @@ export async function runReducerPagingEval({} else {const codex = new Codex({config: {- mcp_servers: mcpServers,+ mcp_servers: { cs_artifacts: mcpServer },features: {code_mode: { enabled: true },code_mode_host: { enabled: true, disable_in_process_fallback: true },@@ -128,11 +125,14 @@ export async function runReducerPagingEval({path.join(root, "report.json"),JSON.stringify(report, null, 2),);- return report;+ return { report, fixture };}/** Grade the captured I/O independently of model wording or another model run. */-export function gradeReducerPagingTrace(trace, ipcFrameLimitBytes) {+export function gradeReducerPagingTrace(+ trace: ReducerTraceEvent[],+ ipcFrameLimitBytes: number,+) {const reads = trace.filter((event) =>event.event === "request" &&@@ -141,23 +141,23 @@ export function gradeReducerPagingTrace(trace, ipcFrameLimitBytes) {const readsById = new Map(reads.map((event) => [event.id, event]));const injected = trace.filter((event) => event.injected);assert.equal(injected.length, 1);- assert.ok(injected[0].bytes > ipcFrameLimitBytes);+ assert.ok(injected[0].bytes! > ipcFrameLimitBytes);const failed = reads.find((event) => event.id === injected[0].id);- const retried = reads.find((event) => event.id > failed.id);+ const retried = reads.find((event) => event.id > failed!.id);assert.ok(retried, "the reducer must recover from the oversized response");assert.ok(- retried.input.maxBytes < failed.input.maxBytes,+ retried.input!.maxBytes < failed!.input!.maxBytes,"recovery must change the failed request's byte budget",);- assert.equal(retried.input.cursor ?? "0", failed.input.cursor ?? "0");- assert.equal(retried.input.findingRef, failed.input.findingRef);+ assert.equal(retried.input!.cursor ?? "0", failed!.input!.cursor ?? "0");+ assert.equal(retried.input!.findingRef, failed!.input!.findingRef);const pages = trace.filter((event) =>event.event === "response" && !event.injected && readsById.has(event.id),);for (const page of pages) {const request = readsById.get(page.id);- assert.ok(page.bytes <= request.input.maxBytes);+ assert.ok(page.bytes! <= request!.input!.maxBytes);}assert.ok(pages.some((page) => page.nextCursor !== undefined));const submissions = trace.filter(@@ -174,47 +174,40 @@ export function gradeReducerPagingTrace(trace, ipcFrameLimitBytes) {1,"the reducer must successfully record its result exactly once",);- const beforeRecording = trace.slice(- 0,- trace.findIndex(- (event) => event.event === "request" && event.id === recorded[0].id,- ),- );- const pageEdges = new Map();- for (const page of beforeRecording) {+ const pageEdges = new Map<string | undefined, Set<string | undefined>>();+ for (const page of trace) {+ if (page.event === "request" && page.id === recorded[0].id) break;const request = readsById.get(page.id);if (page.event !== "response" ||page.injected ||!request ||- request.input.findingRef !== undefined+ request.input!.findingRef !== undefined)continue;- const cursor = request.input.cursor ?? "0";+ const cursor = request.input!.cursor ?? "0";const edges = pageEdges.get(cursor) ?? new Set();edges.add(page.nextCursor);pageEdges.set(cursor, edges);}- const reached = new Set(["0"]);- let inputsComplete = false;+ const reached = new Set<string | undefined>(["0"]);for (const cursor of reached) {for (const next of pageEdges.get(cursor) ?? []) {- if (next === undefined) inputsComplete = true;- else reached.add(next);+ reached.add(next);}}assert.equal(- inputsComplete,+ reached.has(undefined),true,"read every assigned-input page before recording",);return {- actualOversizedResponseBytes: injected[0].bytes,- firstBudget: failed.input.maxBytes,- recoveryBudget: retried.input.maxBytes,+ actualOversizedResponseBytes: injected[0].bytes!,+ firstBudget: failed!.input!.maxBytes,+ recoveryBudget: retried.input!.maxBytes,successfulPages: pages.length,- assignedInputsFullyRead: inputsComplete,- referenceReads: reads.filter((event) => event.input.findingRef).length,+ assignedInputsFullyRead: reached.has(undefined),+ referenceReads: reads.filter((event) => event.input!.findingRef).length,};}diff --git a/plugins/codex-security/mcp-app/tests/support/source-references.mjs b/plugins/codex-security/mcp-app/tests/support/source-references.mjsdeleted file mode 100644index 037fab583..000000000--- a/plugins/codex-security/mcp-app/tests/support/source-references.mjs+++ /dev/null@@ -1,7 +0,0 @@-export const sourceReferences = (worker) => (finding, index) => ({- ...finding,- provenance: {- ...finding.provenance,- sourceFindingIds: [`${worker.id}:${index}`],- },-});diff --git a/plugins/codex-security/mcp-app/tests/support/source-references.ts b/plugins/codex-security/mcp-app/tests/support/source-references.tsnew file mode 100644index 000000000..792164ad8--- /dev/null+++ b/plugins/codex-security/mcp-app/tests/support/source-references.ts@@ -0,0 +1,9 @@+export const sourceReferences =+ (worker: { id: string }) =>+ <T extends { provenance: object }>(finding: T, index: number) => ({+ ...finding,+ provenance: {+ ...finding.provenance,+ sourceFindingIds: [`${worker.id}:${index}`],+ },+ });diff --git a/plugins/codex-security/mcp-app/tests/support/streams.mjs b/plugins/codex-security/mcp-app/tests/support/streams.tssimilarity index 51%rename from plugins/codex-security/mcp-app/tests/support/streams.mjsrename to plugins/codex-security/mcp-app/tests/support/streams.tsindex 03d7ba9af..e56f991f4 100644--- a/plugins/codex-security/mcp-app/tests/support/streams.mjs+++ b/plugins/codex-security/mcp-app/tests/support/streams.ts@@ -1,13 +1,18 @@+import type { ChildProcessWithoutNullStreams } from "node:child_process";+import type { Readable } from "node:stream";import { spawn } from "node:child_process";import { setTimeout as delay } from "node:timers/promises";-function consumeStreamLines(stream, consume) {+export function consumeStreamLines(+ stream: Readable,+ consume: (line: string) => void,+) {let buffer = "";stream.setEncoding("utf8");stream.on("data", (chunk) => {buffer += chunk;const lines = buffer.split("\n");- buffer = lines.pop();+ buffer = lines.pop()!;for (const line of lines) {const trimmed = line.trim();if (trimmed) consume(trimmed);@@ -16,18 +21,30 @@ function consumeStreamLines(stream, consume) {}export function startServer(- serverPath,- env,- { cwd, component, withTimeout, responseLabel, stderrLines, checkSignalCode },+ serverPath: string,+ env: NodeJS.ProcessEnv,+ {+ cwd,+ component,+ timeoutMessage,+ }: {+ cwd: string;+ component: string;+ timeoutMessage: (id: number) => string;+ },) {const child = spawn(process.execPath, [serverPath, "--stdio"], {cwd,env,stdio: ["pipe", "pipe", "pipe"],});- const responses = new Map();- const waiters = new Map();- const stderrEvents = [];+ const responses = new Map<number, ReturnType<typeof JSON.parse>>();+ const waiters = new Map<+ number,+ (response: ReturnType<typeof JSON.parse>) => void+ >();+ const stderrEvents: ReturnType<typeof JSON.parse>[] = [];+ const stderrLines: string[] = [];consumeStreamLines(child.stdout, (line) => {const response = JSON.parse(line);responses.set(response.id, response);@@ -35,34 +52,37 @@ export function startServer(waiters.delete(response.id);});consumeStreamLines(child.stderr, (line) => {- stderrLines?.push(line);+ stderrLines.push(line);try {const event = JSON.parse(line);if (event.component === component) stderrEvents.push(event);} catch {- // Callers choose whether to retain non-structured diagnostics.+ // Non-structured diagnostics remain available through stderrText.}});return {pid: child.pid,- notify(method, params = {}) {+ notify(method: string, params = {}) {writeMessage(child, { jsonrpc: "2.0", method, params });},- sendRequest(id, method, params = {}) {+ sendRequest(id: number, method: string, params = {}) {writeMessage(child, { jsonrpc: "2.0", id, method, params });},- request(id, method, params = {}) {+ request(id: number, method: string, params = {}) {this.sendRequest(id, method, params);return this.waitForResponse(id);},- waitForResponse(id, timeoutMs = 15_000) {+ async waitForResponse(id: number, timeoutMs = 15_000) {const existing = responses.get(id);- if (existing) return Promise.resolve(existing);- return withTimeout(- new Promise((resolve) => waiters.set(id, resolve)),+ if (existing) return existing;+ const { promise, resolve, reject } =+ Promise.withResolvers<ReturnType<typeof JSON.parse>>();+ waiters.set(id, resolve);+ const timer = setTimeout(+ () => reject(new Error(timeoutMessage(id))),timeoutMs,- `${responseLabel} ${id}`,);+ return promise.finally(() => clearTimeout(timer));},stderrEvents() {return [...stderrEvents];@@ -70,25 +90,23 @@ export function startServer(stderrText() {return stderrLines.join("\n");},- response(id) {+ response(id: number) {return responses.get(id);},async stop() {- if (- child.exitCode !== null ||- (checkSignalCode && child.signalCode !== null)- )- return;- child.stdin.end();+ if (child.exitCode !== null || child.signalCode !== null) return;const exited = new Promise((resolve) => child.once("exit", resolve));+ child.stdin.end();await Promise.race([exited, delay(2_000)]);- if (child.exitCode === null && child.signalCode === null)- child.kill("SIGKILL");+ child.kill("SIGKILL");await exited;},};}-export function writeMessage(child, message) {+export function writeMessage(+ child: Pick<ChildProcessWithoutNullStreams, "stdin">,+ message: unknown,+) {child.stdin.write(`${JSON.stringify(message)}\n`);}diff --git a/plugins/codex-security/mcp-app/tests/support/temporary-directories.d.mts b/plugins/codex-security/mcp-app/tests/support/temporary-directories.d.mtsdeleted file mode 100644index 6b4a225db..000000000--- a/plugins/codex-security/mcp-app/tests/support/temporary-directories.d.mts+++ /dev/null@@ -1,9 +0,0 @@-export function createTemporaryDirectories(canonicalize?: boolean): {- create(prefix: string): Promise<string>;- cleanup(): Promise<void[]>;-};--export function temporaryDirectory(- prefix: string,- canonicalize?: boolean,-): Promise<string>;diff --git a/plugins/codex-security/mcp-app/tests/support/temporary-directories.mjs b/plugins/codex-security/mcp-app/tests/support/temporary-directories.tssimilarity index 55%rename from plugins/codex-security/mcp-app/tests/support/temporary-directories.mjsrename to plugins/codex-security/mcp-app/tests/support/temporary-directories.tsindex 89d977306..00d490a5c 100644--- a/plugins/codex-security/mcp-app/tests/support/temporary-directories.mjs+++ b/plugins/codex-security/mcp-app/tests/support/temporary-directories.ts@@ -3,22 +3,27 @@ import { tmpdir } from "node:os";import { join } from "node:path";export function createTemporaryDirectories(canonicalize = false) {- const roots = [];+ const roots: string[] = [];return {- async create(prefix) {+ track(root: string): void {+ roots.push(root);+ },+ async create(prefix: string) {const root = await temporaryDirectory(prefix, canonicalize);roots.push(root);return root;},- cleanup() {- return Promise.all(- roots.map((root) => rm(root, { recursive: true, force: true })),+ async cleanup(): Promise<void> {+ await Promise.all(+ roots+ .splice(0)+ .map((root) => rm(root, { recursive: true, force: true })),);},};}-export async function temporaryDirectory(prefix, canonicalize = false) {+export async function temporaryDirectory(prefix: string, canonicalize = false) {const directory = await mkdtemp(join(tmpdir(), prefix));return canonicalize ? realpath(directory) : directory;}diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_attack_path.mjs b/plugins/codex-security/mcp-app/tests/test_artifact_attack_path.tssimilarity index 90%rename from plugins/codex-security/mcp-app/tests/test_artifact_attack_path.mjsrename to plugins/codex-security/mcp-app/tests/test_artifact_attack_path.tsindex 5546c6911..92f397989 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_attack_path.mjs+++ b/plugins/codex-security/mcp-app/tests/test_artifact_attack_path.ts@@ -1,8 +1,11 @@-import { createTemporaryDirectories } from "./support/temporary-directories.mjs";+import { jsonLines, readJson, readJsonLines } from "./support/json.ts";+import { createTemporaryDirectories } from "./support/temporary-directories.ts";+import type { RawDiscoveryCandidate } from "../src/artifact-discovery.js";+import type { CandidateValidationRecord } from "../src/artifact-validation-phase.js";import assert from "node:assert/strict";import { mkdir, readFile, writeFile } from "node:fs/promises";import path from "node:path";-import { importSource } from "./import-module.mjs";+import { importSource } from "./import-module.ts";const {candidateAttackPathsInputSchema,@@ -29,14 +32,9 @@ try {}async function testSchemaMatchesDocumentedAttackPathDecisions() {- const schema = JSON.parse(- await readFile(- new URL(- "../../schemas/tools/candidate-attack-paths.schema.json",- import.meta.url,- ),- "utf8",- ),+ const schema = await readJson(+ import.meta.dirname,+ "../../schemas/tools/candidate-attack-paths.schema.json",);assert.equal(schema.$schema, "https://json-schema.org/draft/2020-12/schema");@@ -120,7 +118,7 @@ async function testEligibleRowsKeepDiscoveryValidationAndOrder() {operation: "replace",rowsWritten: 2,});- assert.deepEqual(await readRows(fixture), [+ assert.deepEqual(await readJsonLines(fixture.ledgerPath), [{ ...fixture.originalRows[0], attack_path: reportable },fixture.originalRows[1],{ ...fixture.originalRows[2], attack_path: deferred },@@ -278,7 +276,10 @@ async function testEmptyLedgerAcceptsAnEmptyBatch() {assert.equal(await readFile(fixture.ledgerPath, "utf8"), "");}-async function createFixture(label, originalRows) {+async function createFixture(+ label: string,+ originalRows: ReturnType<typeof candidate>[],+) {const root = await temporaryDirectories.create(`codex-security-attack-path-${label.replace(/\s+/gu, "-")}-`,);@@ -289,21 +290,26 @@ async function createFixture(label, originalRows) {"candidate_ledger.jsonl",);await mkdir(path.dirname(ledgerPath), { recursive: true });- await writeFile(ledgerPath, jsonl(originalRows), "utf8");+ await writeFile(ledgerPath, jsonLines(originalRows), "utf8");return {context: {root,repoRoot: root,layout: "scan",- scanId,},ledgerPath,originalRows: structuredClone(originalRows),};}-function candidate(candidateId, disposition) {- const row = {+function candidate(+ candidateId: string,+ disposition?: CandidateValidationRecord["disposition"],+) {+ const row: RawDiscoveryCandidate & {+ candidate_id: string;+ validation?: CandidateValidationRecord;+ } = {candidate_id: candidateId,cwe_ids: ["CWE-79"],locations: [@@ -334,7 +340,7 @@ function candidate(candidateId, disposition) {}function attackPath(decision = "reportable") {- const value = {+ const value: Record<string, string> = {decision,dataflow: "Request parameter flows directly into the template sink.",reachability: "The route is reachable through the HTTP handler.",@@ -352,18 +358,11 @@ function attackPath(decision = "reportable") {return value;}-async function readRows(fixture) {- const content = await readFile(fixture.ledgerPath, "utf8");- return content.split(/\r?\n/u).filter(Boolean).map(JSON.parse);-}--async function assertUnchanged(fixture) {+async function assertUnchanged(+ fixture: Awaited<ReturnType<typeof createFixture>>,+) {assert.equal(await readFile(fixture.ledgerPath, "utf8"),- jsonl(fixture.originalRows),+ jsonLines(fixture.originalRows),);}--function jsonl(rows) {- return rows.map((row) => `${JSON.stringify(row)}\n`).join("");-}diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer.mjs b/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer.tssimilarity index 87%rename from plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer.mjsrename to plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer.tsindex 257dcef2f..9d6cf90ec 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer.mjs+++ b/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer.ts@@ -1,10 +1,13 @@-import { sourceReferences } from "./support/source-references.mjs";-import { temporaryDirectory } from "./support/temporary-directories.mjs";-import { finding, scanId, workerDraft } from "./scan-draft-fixture.mjs";+import { readJson, writeJson } from "./support/json.ts";+import { sourceReferences } from "./support/source-references.ts";+import { temporaryDirectory } from "./support/temporary-directories.ts";+import { finding, scanId, workerDraft } from "./scan-draft-fixture.ts";import assert from "node:assert/strict";import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";import path from "node:path";-import { importSource } from "./import-module.mjs";+import { importSource } from "./import-module.ts";++type Finding = ReturnType<typeof finding>;const {deepReducerInputsInputSchema,@@ -59,10 +62,6 @@ try {"workers",);const dedupRoot = path.join(scanRoot, "artifacts", "deep_discovery", "dedup");- await Promise.all([- mkdir(workersRoot, { recursive: true }),- mkdir(dedupRoot, { recursive: true }),- ]);const shared = finding("shared", "src/shared.ts");const independent = finding("independent", "src/independent.ts");@@ -101,7 +100,6 @@ try {threatModel: { summary: "Requests may reach shared code." },coverage: rejectedCoverage,}),- completionSequence: 1,});const second = await createWorker({workersRoot,@@ -111,7 +109,6 @@ try {scope: { summary: "Shared and independent request handling." },coverage: { ...workerDraft([]).coverage, completeness: "unknown" },}),- completionSequence: 2,});const originalWorkerArtifacts = await Promise.all([first, second].map((worker) => readFile(worker.resultPath, "utf8")),@@ -214,18 +211,13 @@ try {consumedWorkerIds: [first.id, second.id],});assert.deepEqual(- JSON.parse(await readFile(path.join(outputRoot, "result.json"), "utf8")),+ await readJson(outputRoot, "result.json"),mergedWithSources,);const checkpointNames = await readdir(path.join(outputRoot, "checkpoints"));assert.equal(checkpointNames.length, 1);assert.deepEqual(- JSON.parse(- await readFile(- path.join(outputRoot, "checkpoints", checkpointNames[0]),- "utf8",- ),- ),+ await readJson(outputRoot, "checkpoints", checkpointNames[0]),mergedWithSources,"reducer checkpoints retain the accepted findings and scope without coverage",);@@ -248,7 +240,6 @@ try {label: "discovery-collision",id: "worker-collision",result: workerDraft([shared, collision]),- completionSequence: 5,});const collisionRoot = path.join(dedupRoot, "dedup-collision", "output");await mkdir(collisionRoot, { recursive: true });@@ -265,7 +256,8 @@ try {await getCodexSecurityDeepReducerInputs(collisionContext);const sourceFindingIds =collisionInputs.discoveries[0].result.findings.flatMap(- (finding) => finding.provenance.sourceFindingIds,+ (finding: { provenance: { sourceFindingIds: string[] } }) =>+ finding.provenance.sourceFindingIds,);assert.deepEqual(sourceFindingIds, ["worker-collision:0",@@ -280,9 +272,7 @@ try {},]),);- const collisionOutput = JSON.parse(- await readFile(path.join(collisionRoot, "result.json"), "utf8"),- );+ const collisionOutput = await readJson(collisionRoot, "result.json");assert.deepEqual(collisionOutput.findings[0].provenance.sourceFindings, [{ id: "worker-collision:0", finding: shared },{ id: "worker-collision:1", finding: collision },@@ -319,7 +309,6 @@ try {label: "discovery-0003",id: "worker-003",result: workerDraft([shared]),- completionSequence: 3,});const nextOutputRoot = path.join(dedupRoot, "dedup-0002", "output");await mkdir(nextOutputRoot, { recursive: true });@@ -341,28 +330,24 @@ try {});await assert.rejects(recordCodexSecurityDeepReduction(nextContext, reduction([shared])),- (error) => error.code === "merge_traceability_unstable_candidate_id",+ (error: NodeJS.ErrnoException) =>+ error.code === "merge_traceability_unstable_candidate_id",);assert.deepEqual(await recordCodexSecurityDeepReduction(nextContext, merged),{ findingCount: 2, consumedWorkerIds: [third.id] },);- assert.deepEqual(- JSON.parse(- await readFile(path.join(nextOutputRoot, "result.json"), "utf8"),- ),- {- ...mergedWithSources,- findings: [- retainedFinding(shared, [- { id: "worker-003:0", finding: shared },- { id: "worker-001:0", finding: shared },- { id: "worker-002:0", finding: shared },- ]),- mergedWithSources.findings[1],- ],- },- );+ assert.deepEqual(await readJson(nextOutputRoot, "result.json"), {+ ...mergedWithSources,+ findings: [+ retainedFinding(shared, [+ { id: "worker-003:0", finding: shared },+ { id: "worker-001:0", finding: shared },+ { id: "worker-002:0", finding: shared },+ ]),+ mergedWithSources.findings[1],+ ],+ });const enrichedPrevious = structuredClone(mergedWithSources);enrichedPrevious.findings[0].summary =@@ -401,9 +386,7 @@ try {"utf8",);await recordCodexSecurityDeepReduction(nextContext, merged);- const preservedEnrichment = JSON.parse(- await readFile(path.join(nextOutputRoot, "result.json"), "utf8"),- );+ const preservedEnrichment = await readJson(nextOutputRoot, "result.json");assert.equal(Object.hasOwn(preservedEnrichment, "coverage"),false,@@ -451,10 +434,7 @@ try {/repeats assigned Standard scan worker/,);- await writeFile(- first.resultPath,- JSON.stringify({ ...first.result, complete: false }),- );+ await writeJson(first.resultPath, { ...first.result, complete: false });await assert.rejects(getCodexSecurityDeepReducerInputs(context),/only a checkpoint/,@@ -465,10 +445,10 @@ try {undefined,{ ...workerDraft([]).coverage, completeness: "outdated" },]) {- await writeFile(- first.resultPath,- JSON.stringify({ ...first.result, coverage: invalidCoverage }),- );+ await writeJson(first.resultPath, {+ ...first.result,+ coverage: invalidCoverage,+ });await assert.rejects(getCodexSecurityDeepReducerInputs(context),/coverage|completeness/,@@ -479,22 +459,19 @@ try {await writeFile(first.resultPath, "{invalid Standard scan\n");await assert.rejects(getCodexSecurityDeepReducerInputs(context),- (error) =>+ (error: Error) =>error.name !== "DeepScanNonRetryableError" &&/Invalid Deep Scan JSON artifact/.test(error.message) &&!error.message.includes(root),);- await writeFile(- first.resultPath,- JSON.stringify({- ...first.result,- scanId: "12c17317-9594-49e0-b06a-d72fd7e14bba",- }),- );+ await writeJson(first.resultPath, {+ ...first.result,+ scanId: "12c17317-9594-49e0-b06a-d72fd7e14bba",+ });await assert.rejects(getCodexSecurityDeepReducerInputs(context),- (error) =>+ (error: Error) =>error.name !== "DeepScanNonRetryableError" &&/different scan/.test(error.message) &&!error.message.includes(root),@@ -510,20 +487,24 @@ async function createWorker({label,id,result,- completionSequence,+}: {+ workersRoot: string;+ label: string;+ id: string;+ result: ReturnType<typeof workerDraft<Finding>>;}) {const workerRoot = path.join(workersRoot, label, "output");await mkdir(workerRoot, { recursive: true });const resultPath = path.join(workerRoot, "result.json");await writeFile(resultPath, JSON.stringify(result) + "\n");- return { id, resultPath, completionSequence, result };+ return { id, resultPath, result };}-function reduction(findings, extra = {}) {+function reduction(findings: Record<string, unknown>[], extra = {}) {return { scanId, findings, ...extra };}-function withSourceRefs(worker) {+function withSourceRefs(worker: Awaited<ReturnType<typeof createWorker>>) {const { coverage: _coverage, ...result } = worker.result;return {...result,@@ -531,7 +512,10 @@ function withSourceRefs(worker) {};}-function retainedFinding(finding, sourceFindings) {+function retainedFinding(+ finding: Finding & { validation?: unknown },+ sourceFindings: { id: string; finding: unknown }[],+) {return {...finding,provenance: {diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer_pages.mjs b/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer_pages.tssimilarity index 81%rename from plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer_pages.mjsrename to plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer_pages.tsindex e74510204..950fe60ec 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer_pages.mjs+++ b/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer_pages.ts@@ -1,8 +1,17 @@-import { temporaryDirectory } from "./support/temporary-directories.mjs";+import { readJson } from "./support/json.ts";+import { finding as draftFinding } from "./scan-draft-fixture.ts";+import { temporaryDirectory } from "./support/temporary-directories.ts";+import type { ArtifactContext } from "../src/artifact-io.js";+import type {+ DeepReducerPageInput,+ DeepReducerPage,+} from "../src/artifact-deep-reducer-pages.js";import assert from "node:assert/strict";-import { mkdir, readFile, rm, writeFile } from "node:fs/promises";+import { mkdir, rm, writeFile } from "node:fs/promises";import path from "node:path";-import { importModule } from "./import-module.mjs";+import { importModule } from "./import-module.ts";++type SourceFinding = { id: string; finding: unknown };const {deepReducerInputsInputSchema,@@ -65,11 +74,9 @@ try {"current","output",);- await Promise.all(- [workerRoot, previousRoot, outputRoot].map((directory) =>- mkdir(directory, { recursive: true }),- ),- );+ for (const directory of [workerRoot, previousRoot, outputRoot]) {+ await mkdir(directory, { recursive: true });+ }const largeText ='Quoted "text" with \\ newline\n and Unicode 😀é\u0000'.repeat(2048);const fresh = finding("fresh", {@@ -118,15 +125,13 @@ try {};const workerPath = path.join(workerRoot, "result.json");const previousPath = path.join(previousRoot, "result.json");- await Promise.all([- writeFile(workerPath, JSON.stringify(worker)),- writeFile(previousPath, JSON.stringify(previous)),- ]);+ await writeFile(workerPath, JSON.stringify(worker));+ await writeFile(previousPath, JSON.stringify(previous));const context = {root: outputRoot,repoRoot: root,scanId,- layout: "reducer",+ layout: "reducer" as const,deepReducer: {scanRoot,claimedWorkers: [{ id: "worker-one", resultPath: workerPath }],@@ -141,7 +146,8 @@ try {assert.deepEqual(projected.previous.threatModel, previous.threatModel);assert.deepEqual(projected.previous.findings.map(- (entry) => entry.provenance.sourceFindingIds,+ (entry: { provenance: { sourceFindingIds: string[] } }) =>+ entry.provenance.sourceFindingIds,),[["original-worker:0"], ["previous:0"], ["previous:2"]],);@@ -233,11 +239,10 @@ try {scope: projected.discoveries[0].result.scope,threatModel: projected.previous.threatModel,});- const saved = JSON.parse(- await readFile(path.join(outputRoot, "result.json"), "utf8"),- );- const allSources = saved.findings.flatMap(- (entry) => entry.provenance.sourceFindings,+ const saved = await readJson(outputRoot, "result.json");+ const allSources: SourceFinding[] = saved.findings.flatMap(+ (entry: { provenance: { sourceFindings: SourceFinding[] } }) =>+ entry.provenance.sourceFindings,);assert.deepEqual(allSources.map((source) => source.id).sort(), ["original-worker:0",@@ -246,16 +251,18 @@ try {"worker-one:0",]);assert.deepEqual(- allSources.find((source) => source.id === "worker-one:0").finding,+ allSources.find((source) => source.id === "worker-one:0")!.finding,fresh,);assert.deepEqual(- allSources.find((source) => source.id === "original-worker:0").finding,+ allSources.find((source) => source.id === "original-worker:0")!.finding,original,);assert.deepEqual(- saved.findings.find((entry) => entry.identity.anchor === "old").provenance- .previousFindings,+ saved.findings.find(+ (entry: { identity: { anchor: string } }) =>+ entry.identity.anchor === "old",+ ).provenance.previousFindings,canonical.provenance.previousFindings,);console.log("deep reducer paging tests passed");@@ -263,16 +270,22 @@ try {await rm(root, { recursive: true, force: true });}-async function readDocument(context, options = {}) {+async function readDocument(+ context: ArtifactContext,+ options: Partial<DeepReducerPageInput> = {},+) {const maxBytes = options.maxBytes ?? 4096;- let cursor;+ let cursor: string | undefined;const fragments = [];do {- const page = await getCodexSecurityDeepReducerInputsPage(context, {- ...options,- maxBytes,- ...(cursor === undefined ? {} : { cursor }),- });+ const page: DeepReducerPage = await getCodexSecurityDeepReducerInputsPage(+ context,+ {+ ...options,+ maxBytes,+ ...(cursor === undefined ? {} : { cursor }),+ },+ );const response = deepReducerPageResponse(page);assert.deepEqual(Object.keys(response), ["content"]);assert.ok(Buffer.byteLength(JSON.stringify(response), "utf8") <= maxBytes);@@ -286,21 +299,12 @@ async function readDocument(context, options = {}) {return JSON.parse(fragments.join(""));}-function finding(id, extra = {}) {+function finding<Extra extends object = object>(+ id: string,+ extra: Extra = {} as Extra,+) {return {- ruleId: "cross-site-scripting." + id,- identity: { anchor: id },- title: "Unsafe request output " + id,- summary: "A request-controlled value reaches an HTML response.",- severity: { level: "high" },- confidence: {- level: "high",- rationale: "The source establishes reachability.",- },- taxonomy: { category: "cross-site-scripting", cwe: ["CWE-79"] },- locations: [{ path: `src/${id}.ts`, startLine: 1, endLine: 2 }],- remediation: "Encode request-controlled values before emitting HTML.",- provenance: { source: "local_plugin" },+ ...draftFinding(id, `src/${id}.ts`),...extra,};}diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_discovery.mjs b/plugins/codex-security/mcp-app/tests/test_artifact_discovery.tssimilarity index 85%rename from plugins/codex-security/mcp-app/tests/test_artifact_discovery.mjsrename to plugins/codex-security/mcp-app/tests/test_artifact_discovery.tsindex 8a582d4c6..849fd56df 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_discovery.mjs+++ b/plugins/codex-security/mcp-app/tests/test_artifact_discovery.ts@@ -1,4 +1,10 @@-import { createTemporaryDirectories } from "./support/temporary-directories.mjs";+import { jsonLines, readJson } from "./support/json.ts";+import { createTemporaryDirectories } from "./support/temporary-directories.ts";+import type { ArtifactContext } from "../src/artifact-io.js";+import type {+ RawDiscoveryLocation,+ CompactDiscoveryCandidate,+} from "../src/artifact-discovery.js";import assert from "node:assert/strict";import {copyFile,@@ -9,9 +15,8 @@ import {writeFile,} from "node:fs/promises";import path from "node:path";-import { fileURLToPath } from "node:url";import { build } from "esbuild";-import { importSource } from "./import-module.mjs";+import { importSource } from "./import-module.ts";const {compactDiscoveryCandidateSchema,@@ -25,28 +30,18 @@ const {new URL("../src/artifact-discovery.ts", import.meta.url).pathname,);-const pluginRoot = fileURLToPath(new URL("../../", import.meta.url));-const definitions = JSON.parse(- await readFile(- path.join(- pluginRoot,- "schemas",- "definitions",- "discovery-candidate.schema.json",- ),- "utf8",- ),+const pluginRoot = path.join(import.meta.dirname, "../../");+const definitions = await readJson(+ pluginRoot,+ "schemas",+ "definitions",+ "discovery-candidate.schema.json",);-const toolSchemas = JSON.parse(- await readFile(- path.join(- pluginRoot,- "schemas",- "tools",- "discovery-candidates.schema.json",- ),- "utf8",- ),+const toolSchemas = await readJson(+ pluginRoot,+ "schemas",+ "tools",+ "discovery-candidates.schema.json",);assert.equal(@@ -86,6 +81,7 @@ assert.deepEqual(toolSchemas.$defs.workbenchListCandidatesInput.required, [const rootDirectories = createTemporaryDirectories(true);const root = await rootDirectories.create("security-artifact-discovery-");const runtimePluginRoot = path.join(root, "plugin");+const repoRoot = path.join(root, "repository");try {await build({bundle: true,@@ -103,7 +99,6 @@ try {path.join(destination, "windows.node"),);}- const repoRoot = path.join(root, "repository");await mkdir(path.join(repoRoot, "src"), { recursive: true });await mkdir(path.join(repoRoot, "support"), { recursive: true });await writeFile(@@ -119,22 +114,22 @@ try {"first\nsecond\n",);- const scan = await createContext(root, repoRoot, "scan", "scan");+ const scan = await createContext("scan", "scan");await verifyInputSchema();await verifyNormalizationAndPagination(scan);await verifyReaderPreservesSharedPhaseRecords(scan);await verifyNormalizerFailuresPreserveOutput(scan);- await verifyDiffInventoryAllowsDeletedFiles(root, repoRoot);+ await verifyDiffInventoryAllowsDeletedFiles();await verifyEmptyReplacement(scan);- await verifyWorkerContext(root, repoRoot);- await verifyMalformedLedgerIsNotModified(root, repoRoot);- await verifySymlinkRejection(root, repoRoot);+ await verifyWorkerContext();+ await verifyMalformedLedgerIsNotModified();+ await verifySymlinkRejection();} finally {await rootDirectories.cleanup();}async function verifyInputSchema() {- const parseCandidate = (value) =>+ const parseCandidate = (value: unknown) =>discoveryCandidatesInputSchema.safeParse({ candidates: [value] });const candidate = rawCandidate();assert.equal(parseCandidate(candidate).success, true);@@ -249,7 +244,7 @@ async function verifyInputSchema() {);}-async function verifyNormalizationAndPagination(context) {+async function verifyNormalizationAndPagination(context: ArtifactContext) {const first = rawCandidate({cwe_ids: ["cwe-089", "CWE-89"],summary: "Request input reaches a query",@@ -282,12 +277,16 @@ async function verifyNormalizationAndPagination(context) {assert.match(row.candidate_id, /^candidate-[a-f0-9]{16}$/u);assert.deepEqual(row.cwe_ids, ["CWE-89"]);assert.equal(- row.locations.every((location) => "end_line" in location),+ row.locations.every(+ (location: RawDiscoveryLocation) => "end_line" in location,+ ),true,);}- const merged = all.rows.find((row) => row.instance === undefined);+ const merged = all.rows.find(+ (row: CompactDiscoveryCandidate) => row.instance === undefined,+ );assert.ok(merged);assert.deepEqual(merged.summary.split("\n"), ["A second review confirms the query",@@ -297,7 +296,11 @@ async function verifyNormalizationAndPagination(context) {"First independent review","Second independent review",]);- assert.ok(all.rows.some((row) => row.instance === "sort parameter"));+ assert.ok(+ all.rows.some(+ (row: CompactDiscoveryCandidate) => row.instance === "sort parameter",+ ),+ );const firstPage = await listCodexSecurityCandidates({ limit: 1 }, context);assert.equal(firstPage.rows.length, 1);@@ -327,7 +330,9 @@ async function verifyNormalizationAndPagination(context) {]);}-async function verifyNormalizerFailuresPreserveOutput(context) {+async function verifyNormalizerFailuresPreserveOutput(+ context: ArtifactContext,+) {const destination = path.join(context.root,"artifacts",@@ -399,8 +404,8 @@ async function verifyNormalizerFailuresPreserveOutput(context) {assert.equal(await readFile(destination, "utf8"), original);}-async function verifyDiffInventoryAllowsDeletedFiles(root, repoRoot) {- const context = await createContext(root, repoRoot, "diff-output", "scan");+async function verifyDiffInventoryAllowsDeletedFiles() {+ const context = await createContext("diff-output", "scan");const inventory = path.join(context.root,"artifacts",@@ -434,7 +439,9 @@ async function verifyDiffInventoryAllowsDeletedFiles(root, repoRoot) {);}-async function verifyReaderPreservesSharedPhaseRecords(context) {+async function verifyReaderPreservesSharedPhaseRecords(+ context: ArtifactContext,+) {const destination = path.join(context.root,"artifacts",@@ -442,7 +449,10 @@ async function verifyReaderPreservesSharedPhaseRecords(context) {"candidate_ledger.jsonl",);const original = await readFile(destination, "utf8");- const rows = original.trimEnd().split("\n").map(JSON.parse);+ const rows = original+ .trimEnd()+ .split("\n")+ .map((line) => JSON.parse(line));rows[0].validation = {disposition: "reportable",evidence: "The existing validation phase confirmed the affected code path.",@@ -451,17 +461,15 @@ async function verifyReaderPreservesSharedPhaseRecords(context) {disposition: "reportable",evidence: "The existing attack-path phase confirmed request reachability.",};- await writeFile(destination, `${rows.map(JSON.stringify).join("\n")}\n`);+ await writeFile(destination, jsonLines(rows));const page = await listCodexSecurityCandidates({}, context);assert.deepEqual(page.rows, rows);- assert.deepEqual(page.rows[0].validation, rows[0].validation);- assert.deepEqual(page.rows[0].attack_path, rows[0].attack_path);await writeFile(destination, original);}-async function verifyEmptyReplacement(context) {+async function verifyEmptyReplacement(context: ArtifactContext) {const result = await recordCodexSecurityDiscoveryCandidates({ candidates: [] },context,@@ -482,8 +490,8 @@ async function verifyEmptyReplacement(context) {assert.equal(content, "");}-async function verifyWorkerContext(root, repoRoot) {- const worker = await createContext(root, repoRoot, "worker-output", "worker");+async function verifyWorkerContext() {+ const worker = await createContext("worker-output", "worker");const result = await recordCodexSecurityDiscoveryCandidates({candidates: [rawCandidate()],@@ -496,13 +504,8 @@ async function verifyWorkerContext(root, repoRoot) {assert.match(page.rows[0].candidate_id, /^candidate-[a-f0-9]{16}$/u);}-async function verifyMalformedLedgerIsNotModified(root, repoRoot) {- const context = await createContext(- root,- repoRoot,- "malformed-output",- "scan",- );+async function verifyMalformedLedgerIsNotModified() {+ const context = await createContext("malformed-output", "scan");const destination = path.join(context.root,"artifacts",@@ -518,10 +521,10 @@ async function verifyMalformedLedgerIsNotModified(root, repoRoot) {assert.equal(await readFile(destination, "utf8"), malformed);}-async function verifySymlinkRejection(root, repoRoot) {+async function verifySymlinkRejection() {if (process.platform === "win32") return;- const context = await createContext(root, repoRoot, "unsafe-output", "scan");+ const context = await createContext("unsafe-output", "scan");const outside = path.join(root, "outside.jsonl");await writeFile(outside, "outside must not change\n");const destination = path.join(@@ -546,7 +549,10 @@ async function verifySymlinkRejection(root, repoRoot) {);}-async function createContext(root, repoRoot, name, layout) {+async function createContext(+ name: string,+ layout: ArtifactContext["layout"],+): Promise<ArtifactContext> {const artifactRoot = path.join(root, name);const discoveryDirectory = path.join(artifactRoot,diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_foundation.mjs b/plugins/codex-security/mcp-app/tests/test_artifact_foundation.tssimilarity index 87%rename from plugins/codex-security/mcp-app/tests/test_artifact_foundation.mjsrename to plugins/codex-security/mcp-app/tests/test_artifact_foundation.tsindex 0cd79c660..73bcdb882 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_foundation.mjs+++ b/plugins/codex-security/mcp-app/tests/test_artifact_foundation.ts@@ -1,3 +1,4 @@+import { readJson } from "./support/json.ts";import assert from "node:assert/strict";import {mkdir,@@ -11,44 +12,34 @@ import {} from "node:fs/promises";import { tmpdir } from "node:os";import path from "node:path";-import { build } from "esbuild";+import { importSource } from "./import-module.ts";-const compiled = await build({- bundle: true,- entryPoints: [- new URL("../src/artifact-io.ts", import.meta.url).pathname,- new URL("../src/artifact-context.ts", import.meta.url).pathname,- new URL("../artifact-writer-main.ts", import.meta.url).pathname,- new URL("../src/artifact-schema-loader.ts", import.meta.url).pathname,- ],- format: "esm",- outdir: "codex-security-artifact-foundation",- platform: "node",- write: false,- plugins: [- {- name: "observe-worker-context",- setup(builder) {- builder.onLoad({ filter: /compact-artifact-tools\.ts$/ }, () => ({- contents:- "export function registerCompactWorkerArtifactTools(server, context) { server.context = context; }",- loader: "js",- }));+const io = await importSource(+ new URL("../src/artifact-io.ts", import.meta.url).pathname,+);+const contextApi = await importSource(+ new URL("../src/artifact-context.ts", import.meta.url).pathname,+);+const writerApi = await importSource(+ new URL("../artifact-writer-main.ts", import.meta.url).pathname,+ {+ plugins: [+ {+ name: "observe-worker-context",+ setup(builder) {+ builder.onLoad({ filter: /compact-artifact-tools\.ts$/ }, () => ({+ contents:+ "export function registerCompactWorkerArtifactTools(server, context) { server.context = context; }",+ loader: "js",+ }));+ },},- },- ],-});-const modules = new Map(- compiled.outputFiles.map((file) => [- path.basename(file.path),- "data:text/javascript;base64," +- Buffer.from(file.contents).toString("base64"),- ]),+ ],+ },+);+const schemas = await importSource(+ new URL("../src/artifact-schema-loader.ts", import.meta.url).pathname,);-const io = await import(modules.get("artifact-io.js"));-const contextApi = await import(modules.get("artifact-context.js"));-const writerApi = await import(modules.get("artifact-writer-main.js"));-const schemas = await import(modules.get("artifact-schema-loader.js"));const fixture = await realpath(await mkdtemp(path.join(tmpdir(), "codex-security-artifact-foundation-")),);@@ -76,14 +67,9 @@ try {console.log("Codex Security compact artifact foundation tests passed");async function testWorkerThreatModelSchema() {- const schema = JSON.parse(- await readFile(- new URL(- "../../schemas/tools/worker-threat-model.schema.json",- import.meta.url,- ),- "utf8",- ),+ const schema = await readJson(+ import.meta.dirname,+ "../../schemas/tools/worker-threat-model.schema.json",);assert.equal(schema.$schema, "https://json-schema.org/draft/2020-12/schema");assert.equal(@@ -100,14 +86,9 @@ async function testWorkerThreatModelSchema() {}async function testSchemaSourceOfTruth() {- const common = JSON.parse(- await readFile(- new URL(- "../../schemas/definitions/artifact-common.schema.json",- import.meta.url,- ),- "utf8",- ),+ const common = await readJson(+ import.meta.dirname,+ "../../schemas/definitions/artifact-common.schema.json",);const fixtureDocument = {$id: "codex-security://schemas/tools/artifact-foundation-fixture.schema.json",@@ -198,10 +179,8 @@ async function testSchemaSourceOfTruth() {async function testScanContext() {const root = path.join(fixture, "scan");const repoRoot = path.join(fixture, "repository");- await Promise.all([- mkdir(root, { recursive: true }),- mkdir(repoRoot, { recursive: true }),- ]);+ await mkdir(root, { recursive: true });+ await mkdir(repoRoot, { recursive: true });const scanId = "61a20957-1be8-4ccf-8de8-eab4061e8cc3";const contract = {target: {@@ -214,8 +193,8 @@ async function testScanContext() {requiredExcludePaths: [],},};- const calls = [];- const runWorkbench = async (args) => {+ const calls: string[][] = [];+ const runWorkbench = async (args: string[]) => {calls.push(args);return {scan: {@@ -361,7 +340,7 @@ async function testSafeJsonAndJsonl() {{ candidate_id: "two" },]);const rowSchema = {- safeParse(value) {+ safeParse(value: { candidate_id?: unknown } | null) {return value && typeof value.candidate_id === "string"? { success: true, data: value }: {@@ -440,7 +419,7 @@ async function testAtomicReplacement() {await Promise.all(replacements.map((row) => io.replaceArtifactJsonl(destination, [row])),);- const written = JSON.parse(await readFile(destination, "utf8"));+ const written = await readJson(destination);assert.deepEqual(written,replacements.find((row) => row.candidate_id === written.candidate_id),diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.mjs b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.tssimilarity index 85%rename from plugins/codex-security/mcp-app/tests/test_artifact_inventory.mjsrename to plugins/codex-security/mcp-app/tests/test_artifact_inventory.tsindex 6a759d39c..ec5366ed2 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.mjs+++ b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts@@ -1,10 +1,10 @@-import { createTemporaryDirectories } from "./support/temporary-directories.mjs";+import { createTemporaryDirectories } from "./support/temporary-directories.ts";import assert from "node:assert/strict";import { execFile as nodeExecFile } from "node:child_process";import { mkdir, readFile, symlink, unlink, writeFile } from "node:fs/promises";import path from "node:path";import { promisify } from "node:util";-import { importSource } from "./import-module.mjs";+import { importSource } from "./import-module.ts";const execFile = promisify(nodeExecFile);const temporaryDirectories = createTemporaryDirectories(true);@@ -57,29 +57,41 @@ async function testSchemasAreBoundAndExact() {async function testPrepareUsesTheExistingStandardGenerator() {const fixture = await createFixture("standard repository");- await writeRepositoryFile(- fixture.repoRoot,- "src/a.ts",- "export const a = 1;\n",- );- await writeRepositoryFile(- fixture.repoRoot,- "src/résumé.ts",- "export const b = 2;\n",- );- await writeRepositoryFile(- fixture.repoRoot,+ await fixture.writeRepositoryFile("src/a.ts", "export const a = 1;\n");+ await fixture.writeRepositoryFile("src/résumé.ts", "export const b = 2;\n");+ await fixture.writeRepositoryFile(".hidden/handler.ts","export const c = 3;\n",);const result = await inventory.prepareCodexSecurityReviewItems(fixture.scan);const stored = await readFile(fixture.scanInventory, "utf8");- const expected = await standardInventory(fixture.repoRoot, ".");+ const { stdout } = await execFile(+ "rg",+ [+ "--files",+ "--hidden",+ "--glob",+ "!**/.git",+ "--glob",+ "!**/.git/**",+ "--path-separator=/",+ "--",+ ".",+ ],+ { cwd: fixture.repoRoot, encoding: "utf8" },+ );+ const expectedPaths = stdout+ .split("\n")+ .filter(Boolean)+ .sort((left, right) =>+ Buffer.compare(Buffer.from(left), Buffer.from(right)),+ );+ const expected = expectedPaths.map((line) => `${line}\n`).join("");assert.equal(stored, expected);assert.deepEqual(result, {- reviewItemsTotal: expected.split("\n").filter(Boolean).length,+ reviewItemsTotal: expectedPaths.length,});const first = await inventory.listCodexSecurityReviewItems(fixture.scan, {limit: 2,@@ -88,7 +100,7 @@ async function testPrepareUsesTheExistingStandardGenerator() {assert.equal(first.nextCursor, "2");assert.equal(Object.hasOwn(first.items[0], "area"), false);assert.equal(- first.items.every((item) => item.path.startsWith("./")),+ first.items.every((item: { path: string }) => item.path.startsWith("./")),true,);const second = await inventory.listCodexSecurityReviewItems(fixture.scan, {@@ -97,18 +109,19 @@ async function testPrepareUsesTheExistingStandardGenerator() {});assert.equal(Object.hasOwn(second, "nextCursor"), false);assert.deepEqual(- [...first.items, ...second.items].map((item) => item.path),- expected.split("\n").filter(Boolean),+ [...first.items, ...second.items].map(+ (item: { path: string }) => item.path,+ ),+ expectedPaths,);}async function testPrepareListsIgnoredTrackedFilesOnce() {const fixture = await createFixture("ignored tracked files");- await initializeRepository(fixture.repoRoot);- await writeRepositoryFile(fixture.repoRoot, ".gitignore", "generated/\n");+ await runGit(fixture.repoRoot, "init", "-q");+ await fixture.writeRepositoryFile(".gitignore", "generated/\n");for (const name of ["a.ts", "b.ts"]) {- await writeRepositoryFile(- fixture.repoRoot,+ await fixture.writeRepositoryFile(`generated/${name}`,"export const value = 1;\n",);@@ -147,8 +160,8 @@ async function testPrepareExcludesGitMetadata() {[".gitignore", "*.skip\n"],[".github/workflows/check.yml", "name: example\n"],["src/widget.git", "example\n"],- ]) {- await writeRepositoryFile(fixture.repoRoot, name, source);+ ] as const) {+ await fixture.writeRepositoryFile(name, source);}assert.deepEqual(@@ -180,19 +193,16 @@ async function testPrepareExcludesGitMetadata() {async function testPrepareUsesOnlyAuthoritativeDiffChanges() {const fixture = await createFixture("selected committed changes");- await initializeRepository(fixture.repoRoot);- await writeRepositoryFile(- fixture.repoRoot,+ await runGit(fixture.repoRoot, "init", "-q");+ await fixture.writeRepositoryFile("src/changed.ts","export const value = 1;\n",);- await writeRepositoryFile(- fixture.repoRoot,+ await fixture.writeRepositoryFile("src/deleted.ts","export const guard = true;\n",);- await writeRepositoryFile(- fixture.repoRoot,+ await fixture.writeRepositoryFile("src/unrelated.ts","export const unrelated = 1;\n",);@@ -200,18 +210,15 @@ async function testPrepareUsesOnlyAuthoritativeDiffChanges() {await runGit(fixture.repoRoot, "commit", "-qm", "base");const baseRevision = await runGit(fixture.repoRoot, "rev-parse", "HEAD");- await writeRepositoryFile(- fixture.repoRoot,+ await fixture.writeRepositoryFile("src/changed.ts","export const value = 2;\n",);- await writeRepositoryFile(- fixture.repoRoot,+ await fixture.writeRepositoryFile("src/new.ts","export const added = true;\n",);- await writeRepositoryFile(- fixture.repoRoot,+ await fixture.writeRepositoryFile("tests/example.ts","export const ignored = true;\n",);@@ -241,9 +248,8 @@ async function testPrepareUsesOnlyAuthoritativeDiffChanges() {async function testPrepareIncludesStagedAndUnstagedChanges() {const fixture = await createFixture("selected working tree changes");- await initializeRepository(fixture.repoRoot);- await writeRepositoryFile(- fixture.repoRoot,+ await runGit(fixture.repoRoot, "init", "-q");+ await fixture.writeRepositoryFile("src/changed.ts","export const value = 1;\n",);@@ -251,19 +257,16 @@ async function testPrepareIncludesStagedAndUnstagedChanges() {await runGit(fixture.repoRoot, "commit", "-qm", "base");const revision = await runGit(fixture.repoRoot, "rev-parse", "HEAD");- await writeRepositoryFile(- fixture.repoRoot,+ await fixture.writeRepositoryFile("src/changed.ts","export const value = 2;\n",);- await writeRepositoryFile(- fixture.repoRoot,+ await fixture.writeRepositoryFile("src/staged.ts","export const staged = true;\n",);await runGit(fixture.repoRoot, "add", "src/staged.ts");- await writeRepositoryFile(- fixture.repoRoot,+ await fixture.writeRepositoryFile("src/untracked.ts","export const untracked = true;\n",);@@ -422,7 +425,7 @@ async function testInvalidDiffTargetPreservesPreviousInventory() {}}-async function createFixture(label) {+async function createFixture(label: string) {const root = await temporaryDirectories.create("security-artifact-inventory-",);@@ -431,14 +434,14 @@ async function createFixture(label) {const scanRoot = path.join(fixtureRoot, "scan");const workerRoot = path.join(fixtureRoot, "worker");const pluginRoot = new URL("../../", import.meta.url).pathname;- await Promise.all([- mkdir(repoRoot, { recursive: true }),- mkdir(scanRoot, { recursive: true }),- mkdir(workerRoot, { recursive: true }),- ]);+ await mkdir(repoRoot, { recursive: true });+ await mkdir(scanRoot, { recursive: true });return {root,repoRoot,+ async writeRepositoryFile(relativePath: string, source: string) {+ return writeInventory(path.join(repoRoot, relativePath), source);+ },scanInventory: path.join(scanRoot,"artifacts",@@ -455,7 +458,6 @@ async function createFixture(label) {root: scanRoot,repoRoot,layout: "scan",- scanId: "f84c8312-a602-4660-8e01-518a176cd75a",scope: ".",pluginRoot,pythonCommand: process.env.PYTHON ?? "python3",@@ -468,48 +470,12 @@ async function createFixture(label) {};}-async function writeRepositoryFile(repository, relativePath, source) {- const destination = path.join(repository, relativePath);+async function writeInventory(destination: string, source: string) {await mkdir(path.dirname(destination), { recursive: true });await writeFile(destination, source);}-async function writeInventory(destination, source) {- await mkdir(path.dirname(destination), { recursive: true });- await writeFile(destination, source);-}--async function standardInventory(repository, scope) {- const { stdout } = await execFile(- "rg",- [- "--files",- "--hidden",- "--glob",- "!**/.git",- "--glob",- "!**/.git/**",- "--path-separator=/",- "--",- scope,- ],- { cwd: repository, encoding: "utf8" },- );- return stdout- .split("\n")- .filter(Boolean)- .sort((left, right) =>- Buffer.compare(Buffer.from(left), Buffer.from(right)),- )- .map((line) => `${line}\n`)- .join("");-}--async function initializeRepository(repository) {- await runGit(repository, "init", "-q");-}--async function runGit(repository, ...arguments_) {+async function runGit(repository: string, ...arguments_: string[]) {const { stdout } = await execFile("git",[diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.mjs b/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.tssimilarity index 78%rename from plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.mjsrename to plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.tsindex c95bcb9c2..9e690dd42 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.mjs+++ b/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.ts@@ -1,7 +1,15 @@+import { readJson, snapshotScanDraft, writeJsonLine } from "./support/json.ts";import { mock } from "node:test";-import { temporaryDirectory } from "./support/temporary-directories.mjs";+import { temporaryDirectory } from "./support/temporary-directories.ts";+import type { ArtifactContext } from "../src/artifact-context.js";+import type { ScanDraftInput } from "../src/artifact-scan-draft.js";+type FixtureFinding = Record<string, unknown> & {+ provenance: Record<string, unknown>;+ identity?: { anchor: string; instance?: string };+};+import assert from "node:assert/strict";-import { createHash } from "node:crypto";+import { hash } from "node:crypto";import { promises as fsPromises } from "node:fs";import {mkdir,@@ -19,7 +27,8 @@ import {draftApi,draftFixture,scanId,-} from "./scan-draft-recovery-fixture.mjs";+ surfaceDisposition,+} from "./scan-draft-recovery-fixture.ts";const {completedScanInputSchema,@@ -31,8 +40,6 @@ const {scanDraftInputSchema,} = draftApi;-const surfaceDisposition = ({ id, disposition }) => ({ id, disposition });-const root = await temporaryDirectory("codex-security-scan-draft-", true);try {@@ -86,6 +93,15 @@ try {coverage,};+ const findingInput = (changes: Record<string, unknown>) => ({+ ...input,+ findings: [{ ...finding, ...changes }],+ });+ const coverageInput = (changes: Record<string, unknown>) => ({+ ...input,+ coverage: { ...coverage, ...changes },+ });+const workerRoot = path.join(root, "worker-output");await mkdir(workerRoot);const workerContext = {@@ -94,6 +110,12 @@ try {layout: "worker",scanId,};+ const createWorkerContext = async (name: string) => {+ const directory = path.join(root, name);+ await mkdir(directory);+ return { ...workerContext, root: directory };+ };+const workerInput = {scanId,scope: { summary: "Archive handling" },@@ -103,9 +125,8 @@ try {};const workerResultPath = path.join(workerRoot, "result.json");- const checkpointRoot = path.join(root, "checkpoint-worker");- await mkdir(checkpointRoot);- const checkpointContext = { ...workerContext, root: checkpointRoot };+ const checkpointContext = await createWorkerContext("checkpoint-worker");+ const checkpointRoot = checkpointContext.root;const checkpoint = { ...workerInput, complete: false };await recordCodexSecurityWorkerScanDraft(checkpointContext, checkpoint);const checkpointFiles = await readdir(@@ -113,12 +134,7 @@ try {);assert.equal(checkpointFiles.length, 1);assert.deepEqual(- JSON.parse(- await readFile(- path.join(checkpointRoot, "checkpoints", checkpointFiles[0]),- "utf8",- ),- ),+ await readJson(checkpointRoot, "checkpoints", checkpointFiles[0]),checkpoint,);const checkpointBytes = await readFile(@@ -129,8 +145,7 @@ try {findings: [],});assert.deepEqual(- JSON.parse(await readFile(path.join(checkpointRoot, "result.json"), "utf8"))- .findings,+ (await readJson(checkpointRoot, "result.json")).findings,[finding],"a later write cannot silently remove a saved validated finding",);@@ -146,10 +161,12 @@ try {"raw and reconciled drafts remain immutable while the head selects the accepted result",);- const interruptedRoot = path.join(root, "interrupted-checkpoint-worker");- await mkdir(interruptedRoot);- const interruptedContext = { ...workerContext, root: interruptedRoot };- const interruptedFinding = structuredClone(finding);+ const interruptedContext = await createWorkerContext(+ "interrupted-checkpoint-worker",+ );+ const interruptedRoot = interruptedContext.root;+ const interruptedFinding: typeof finding & { identity?: { anchor: string } } =+ structuredClone(finding);interruptedFinding.identity = { anchor: "interrupted-checkpoint" };interruptedFinding.provenance.candidateId = "interrupted-checkpoint";interruptedFinding.extensions.candidateId = "interrupted-checkpoint";@@ -176,23 +193,19 @@ try {...workerInput,findings: [],});- const interruptedResult = JSON.parse(- await readFile(path.join(interruptedRoot, "result.json"), "utf8"),- );+ const interruptedResult = await readJson(interruptedRoot, "result.json");assert.deepEqual(interruptedResult.findings, [interruptedFinding]);assert.equal(interruptedResult.coverage.deferred.some(- (item) => item.candidateId === "interrupted-review",+ (item: FixtureFinding) => item.candidateId === "interrupted-review",),true,);- const rejectedIncompleteRoot = path.join(root, "rejected-incomplete-worker");- await mkdir(rejectedIncompleteRoot);- const rejectedIncompleteContext = {- ...workerContext,- root: rejectedIncompleteRoot,- };+ const rejectedIncompleteContext = await createWorkerContext(+ "rejected-incomplete-worker",+ );+ const rejectedIncompleteRoot = rejectedIncompleteContext.root;await recordCodexSecurityWorkerScanDraft(rejectedIncompleteContext,workerInput,@@ -220,17 +233,14 @@ try {],},});- const acceptedHead = JSON.parse(- await readFile(- path.join(rejectedIncompleteRoot, "checkpoint-head.json"),- "utf8",- ),+ const acceptedHead = await readJson(+ rejectedIncompleteRoot,+ "checkpoint-head.json",);- const acceptedHeadDraft = JSON.parse(- await readFile(- path.join(rejectedIncompleteRoot, "checkpoints", acceptedHead.checkpoint),- "utf8",- ),+ const acceptedHeadDraft = await readJson(+ rejectedIncompleteRoot,+ "checkpoints",+ acceptedHead.checkpoint,);assert.notEqual(acceptedHeadDraft.complete,@@ -238,26 +248,19 @@ try {"a rejected incomplete write must not become the authoritative checkpoint head",);assert.deepEqual(acceptedHeadDraft.findings, [finding]);- const acceptedResult = JSON.parse(- await readFile(path.join(rejectedIncompleteRoot, "result.json"), "utf8"),- );+ const acceptedResult = await readJson(rejectedIncompleteRoot, "result.json");assert.equal(acceptedResult.coverage.deferred.some(- (item) => item.candidateId === "late-incomplete-review",+ (item: FixtureFinding) => item.candidateId === "late-incomplete-review",),false,"a rejected incomplete write must not change the completed result",);- const deferredDoesNotRejectRoot = path.join(- root,+ const deferredDoesNotRejectContext = await createWorkerContext("deferred-does-not-reject-worker",);- await mkdir(deferredDoesNotRejectRoot);- const deferredDoesNotRejectContext = {- ...workerContext,- root: deferredDoesNotRejectRoot,- };+ const deferredDoesNotRejectRoot = deferredDoesNotRejectContext.root;await recordCodexSecurityWorkerScanDraft(deferredDoesNotRejectContext,workerInput,@@ -278,8 +281,9 @@ try {],},});- const deferredDoesNotReject = JSON.parse(- await readFile(path.join(deferredDoesNotRejectRoot, "result.json"), "utf8"),+ const deferredDoesNotReject = await readJson(+ deferredDoesNotRejectRoot,+ "result.json",);assert.deepEqual(deferredDoesNotReject.findings,@@ -298,21 +302,14 @@ try {findings: [],coverage: { ...coverage, surfaces: [rejection] },});- const rejectedCheckpoint = JSON.parse(- await readFile(path.join(checkpointRoot, "result.json"), "utf8"),- );+ const rejectedCheckpoint = await readJson(checkpointRoot, "result.json");assert.equal(rejectedCheckpoint.findings.length, 0);assert.deepEqual(rejectedCheckpoint.coverage.surfaces[0].finding, finding);- const rejectionWithoutNotesRoot = path.join(- root,+ const rejectionWithoutNotesContext = await createWorkerContext("rejection-without-notes-worker",);- await mkdir(rejectionWithoutNotesRoot);- const rejectionWithoutNotesContext = {- ...workerContext,- root: rejectionWithoutNotesRoot,- };+ const rejectionWithoutNotesRoot = rejectionWithoutNotesContext.root;await recordCodexSecurityWorkerScanDraft(rejectionWithoutNotesContext,checkpoint,@@ -323,15 +320,15 @@ try {findings: [],coverage: { ...coverage, surfaces: [rejectionWithoutNotes] },});- const rejectedWithoutNotes = JSON.parse(- await readFile(path.join(rejectionWithoutNotesRoot, "result.json"), "utf8"),+ const rejectedWithoutNotes = await readJson(+ rejectionWithoutNotesRoot,+ "result.json",);assert.equal(rejectedWithoutNotes.findings.length, 0);assert.deepEqual(rejectedWithoutNotes.coverage.surfaces[0].finding, finding);- const carriedContextRoot = path.join(root, "carried-context-worker");- await mkdir(carriedContextRoot);- const carriedContext = { ...workerContext, root: carriedContextRoot };+ const carriedContext = await createWorkerContext("carried-context-worker");+ const carriedContextRoot = carriedContext.root;await recordCodexSecurityWorkerScanDraft(carriedContext, {...workerInput,complete: false,@@ -340,18 +337,14 @@ try {carriedContext,withoutScanContext(workerInput),);- const carriedWorker = JSON.parse(- await readFile(path.join(carriedContextRoot, "result.json"), "utf8"),- );+ const carriedWorker = await readJson(carriedContextRoot, "result.json");assert.deepEqual(carriedWorker.scope, workerInput.scope);assert.deepEqual(carriedWorker.threatModel, workerInput.threatModel);- const coverageProgressRoot = path.join(root, "coverage-progress-worker");- await mkdir(coverageProgressRoot);- const coverageProgressContext = {- ...workerContext,- root: coverageProgressRoot,- };+ const coverageProgressContext = await createWorkerContext(+ "coverage-progress-worker",+ );+ const coverageProgressRoot = coverageProgressContext.root;const pendingQuestion ="Does the alternate archive handler enforce containment?";await recordCodexSecurityWorkerScanDraft(coverageProgressContext, {@@ -385,8 +378,8 @@ try {],},});- const progressedCoverage = JSON.parse(- await readFile(path.join(coverageProgressRoot, "result.json"), "utf8"),+ const progressedCoverage = (+ await readJson(coverageProgressRoot, "result.json")).coverage;assert.deepEqual(progressedCoverage.surfaces.map(surfaceDisposition), [{ id: "surface-archive", disposition: "reported" },@@ -394,15 +387,10 @@ try {assert.deepEqual(progressedCoverage.openQuestions ?? [], []);assert.equal(progressedCoverage.completeness, "complete");- const renamedProgressRoot = path.join(- root,+ const renamedProgressContext = await createWorkerContext("renamed-coverage-progress-worker",);- await mkdir(renamedProgressRoot);- const renamedProgressContext = {- ...workerContext,- root: renamedProgressRoot,- };+ const renamedProgressRoot = renamedProgressContext.root;const staleSurfaces = [{label: "ZIP entry candidate awaiting validation",@@ -449,9 +437,7 @@ try {complete: false,coverage: resolvedCoverage,});- const resolvedProgress = JSON.parse(- await readFile(path.join(renamedProgressRoot, "result.json"), "utf8"),- );+ const resolvedProgress = await readJson(renamedProgressRoot, "result.json");assert.deepEqual(resolvedProgress.coverage.surfaces,resolvedCoverage.surfaces,@@ -476,15 +462,14 @@ try {complete: true,coverage: resolvedCoverage,});- const finalProgress = JSON.parse(- await readFile(path.join(renamedProgressRoot, "result.json"), "utf8"),- );+ const finalProgress = await readJson(renamedProgressRoot, "result.json");assert.deepEqual(finalProgress.coverage.surfaces, resolvedCoverage.surfaces);assert.equal(finalProgress.coverage.completeness, "complete");- const anchorRoot = path.join(root, "anchor-is-not-candidate-worker");- await mkdir(anchorRoot);- const anchorContext = { ...workerContext, root: anchorRoot };+ const anchorContext = await createWorkerContext(+ "anchor-is-not-candidate-worker",+ );+ const anchorRoot = anchorContext.root;const anchor = "shared-finding-and-deferred-label";const { candidateId: _provenanceCandidate, ...anchorProvenance } =finding.provenance;@@ -514,9 +499,7 @@ try {],},});- const anchorResult = JSON.parse(- await readFile(path.join(anchorRoot, "result.json"), "utf8"),- );+ const anchorResult = await readJson(anchorRoot, "result.json");assert.equal(anchorResult.findings.length, 1);assert.deepEqual(anchorResult.findings[0].identity, { anchor });assert.equal("finding" in anchorResult.coverage.deferred[0], false);@@ -527,15 +510,10 @@ try {{ ...context, root: parentCheckpointRoot },{ ...input, complete: false },);- const parentSnapshot = JSON.parse(- await readFile(- path.join(- parentCheckpointRoot,- "checkpoints",- (await readdir(path.join(parentCheckpointRoot, "checkpoints")))[0],- ),- "utf8",- ),+ const parentSnapshot = await readJson(+ parentCheckpointRoot,+ "checkpoints",+ (await readdir(path.join(parentCheckpointRoot, "checkpoints")))[0],);assert.equal(parentSnapshot.handoffClaimToken, undefined);assert.equal(parentSnapshot.complete, false);@@ -602,9 +580,17 @@ try {const retained = await readJson(unresolvedRoot, "coverage.json");assert.equal(retained.completeness, "partial");assert.deepEqual(- retained.deferred.map((row) => row.candidateId ?? row.id).sort(),+ retained.deferred+ .map(+ (row: { candidateId?: string; id?: string }) =>+ row.candidateId ?? row.id,+ )+ .sort(),[- ...pendingCandidates.map((row) => row.candidateId ?? row.id),+ ...pendingCandidates.map(+ (row: { candidateId?: string; id?: string }) =>+ row.candidateId ?? row.id,+ ),unresolved.id,].sort(),);@@ -620,7 +606,7 @@ try {},/no saved generic deferral/,],- ...pendingCandidates.map((row) => [+ ...pendingCandidates.map((row: { candidateId?: string; id?: string }) => [{...closingDraft,coverage: {@@ -642,7 +628,7 @@ try {},/still active/,],- ]) {+ ] as const) {const before = await readFile(path.join(unresolvedRoot, "coverage.json"),"utf8",@@ -704,7 +690,8 @@ try {);assert.equal((await readJson(interruptedParentRoot, "coverage.json")).deferred.some(- (item) => item.candidateId === "interrupted-parent-review",+ (item: FixtureFinding) =>+ item.candidateId === "interrupted-parent-review",),true,);@@ -779,7 +766,7 @@ try {assert.deepEqual(new Set((await readJson(deepParentRoot, "findings.json")).findings.map(- (item) => item.provenance.candidateId,+ (item: FixtureFinding) => item.provenance.candidateId,),),new Set(["obsolete-parent-candidate", "interrupted-checkpoint"]),@@ -851,7 +838,7 @@ try {"obsolete.json",);await writeFile(obsoleteCheckpointPath, "{malformed obsolete checkpoint\n");- const deepWorkbenchWrites = mock.fn(async (arguments_) => {+ const deepWorkbenchWrites = mock.fn(async (arguments_: string[]) => {assert.deepEqual(arguments_.slice(0, 3), ["write-scan-draft","--scan-id",@@ -862,8 +849,8 @@ try {const draftPath = arguments_[arguments_.indexOf("--draft-path") + 1];const checkpointPath =arguments_[arguments_.indexOf("--checkpoint-path") + 1];- const staged = JSON.parse(await readFile(draftPath, "utf8"));- const stagedCheckpoint = JSON.parse(await readFile(checkpointPath, "utf8"));+ const staged = await readJson(draftPath);+ const stagedCheckpoint = await readJson(checkpointPath);assert.deepEqual(staged.findings, acceptedDeepFindings);assert.deepEqual(staged.coverage, acceptedDeepCoverage);assert.deepEqual(stagedCheckpoint.findings, acceptedDeepDraft.findings);@@ -881,9 +868,8 @@ try {);assert.deepEqual(await readdir(path.join(deepParentRoot, "drafts")), []);- const pendingRoot = path.join(root, "pending-worker");- await mkdir(pendingRoot);- const pendingContext = { ...workerContext, root: pendingRoot };+ const pendingContext = await createWorkerContext("pending-worker");+ const pendingRoot = pendingContext.root;const candidate = {summary: "An unvalidated archive extraction candidate.",evidence: "Original nested-worker source trace.",@@ -907,9 +893,7 @@ try {};await recordCodexSecurityWorkerScanDraft(pendingContext, pending);await recordCodexSecurityWorkerScanDraft(pendingContext, workerInput);- const resolved = JSON.parse(- await readFile(path.join(pendingRoot, "result.json"), "utf8"),- );+ const resolved = await readJson(pendingRoot, "result.json");assert.deepEqual(resolved.coverage.deferred, []);assert.equal(resolved.coverage.completeness, "complete");assert.deepEqual(resolved.findings[0].provenance.originalCandidates, [@@ -923,18 +907,14 @@ try {findings: [],coverage: { ...coverage, surfaces: [rejection] },});- const resolvedRejection = JSON.parse(- await readFile(path.join(pendingRoot, "result.json"), "utf8"),- );+ const resolvedRejection = await readJson(pendingRoot, "result.json");assert.deepEqual(resolvedRejection.coverage.deferred, []);assert.deepEqual(resolvedRejection.coverage.surfaces[0].candidate, candidate);- const undefinedCandidateRoot = path.join(root, "undefined-candidate-worker");- await mkdir(undefinedCandidateRoot);- const undefinedCandidateContext = {- ...workerContext,- root: undefinedCandidateRoot,- };+ const undefinedCandidateContext = await createWorkerContext(+ "undefined-candidate-worker",+ );+ const undefinedCandidateRoot = undefinedCandidateContext.root;await recordCodexSecurityWorkerScanDraft(undefinedCandidateContext, {...workerInput,complete: false,@@ -957,8 +937,9 @@ try {findings: [literalUndefinedFinding],coverage: { ...coverage, completeness: "partial", surfaces: [] },});- const undefinedCandidateResult = JSON.parse(- await readFile(path.join(undefinedCandidateRoot, "result.json"), "utf8"),+ const undefinedCandidateResult = await readJson(+ undefinedCandidateRoot,+ "result.json",);assert.equal(undefinedCandidateResult.coverage.deferred.length, 1);assert.equal(@@ -971,9 +952,8 @@ try {{ opaque: true },7,].entries()) {- const historyRoot = path.join(root, `legacy-history-${index}`);- await mkdir(historyRoot);- const historyContext = { ...workerContext, root: historyRoot };+ const historyContext = await createWorkerContext(`legacy-history-${index}`);+ const historyRoot = historyContext.root;const earlierFinding = {...finding,summary: "Earlier verified source proof.",@@ -984,21 +964,18 @@ try {findings: [earlierFinding],});await recordCodexSecurityWorkerScanDraft(historyContext, workerInput);- const savedHistory = JSON.parse(- await readFile(path.join(historyRoot, "result.json"), "utf8"),- );+ const savedHistory = await readJson(historyRoot, "result.json");const original = structuredClone(earlierFinding);- delete original.provenance.previousFindings;+ delete (original.provenance as { previousFindings?: unknown })+ .previousFindings;assert.deepEqual(savedHistory.findings[0].provenance.previousFindings,[original],"opaque legacy metadata is not interpreted as finding history, but the original proof survives",);const snapshots = await Promise.all(- (await readdir(path.join(historyRoot, "checkpoints"))).map(async (name) =>- JSON.parse(- await readFile(path.join(historyRoot, "checkpoints", name), "utf8"),- ),+ (await readdir(path.join(historyRoot, "checkpoints"))).map(+ async (name) => await readJson(historyRoot, "checkpoints", name),),);assert.deepEqual(@@ -1037,15 +1014,12 @@ try {scanId,findingCount: 1,surfaceCount: 1,- coverage: JSON.parse(await readFile(workerResultPath, "utf8")).coverage,+ coverage: (await readJson(workerResultPath)).coverage,operation: "replace",status: "draft_written",},);- assert.deepEqual(- JSON.parse(await readFile(workerResultPath, "utf8")),- workerInput,- );+ assert.deepEqual(await readJson(workerResultPath), workerInput);const outOfScopeFinding = {...finding,title: "Outside the selected scope",@@ -1093,12 +1067,12 @@ try {scanId,findingCount: 1,surfaceCount: 1,- coverage: JSON.parse(await readFile(workerResultPath, "utf8")).coverage,+ coverage: (await readJson(workerResultPath)).coverage,operation: "replace",status: "draft_written",},);- assert.deepEqual(JSON.parse(await readFile(workerResultPath, "utf8")), {+ assert.deepEqual(await readJson(workerResultPath), {...scopedWorkerInput,findings: [supportedScopedFinding],});@@ -1113,12 +1087,12 @@ try {scanId,findingCount: 1,surfaceCount: 1,- coverage: JSON.parse(await readFile(workerResultPath, "utf8")).coverage,+ coverage: (await readJson(workerResultPath)).coverage,operation: "replace",status: "draft_written",},);- assert.deepEqual(JSON.parse(await readFile(workerResultPath, "utf8")), {+ assert.deepEqual(await readJson(workerResultPath), {...scopedWorkerInput,findings: [supportedScopedFinding],});@@ -1132,15 +1106,12 @@ try {scanId,findingCount: 3,surfaceCount: 1,- coverage: JSON.parse(await readFile(workerResultPath, "utf8")).coverage,+ coverage: (await readJson(workerResultPath)).coverage,operation: "replace",status: "draft_written",},);- assert.deepEqual(- JSON.parse(await readFile(workerResultPath, "utf8")),- scopedWorkerInput,- );+ assert.deepEqual(await readJson(workerResultPath), scopedWorkerInput);for (const name of ["scan-manifest.json", "findings.json", "coverage.json"]) {await assert.rejects(readFile(path.join(workerRoot, name)), {code: "ENOENT",@@ -1240,14 +1211,10 @@ try {evidence: "A separate caller needs review.",},];- const reasonOnlyInput = {- ...input,- coverage: {- ...coverage,- completeness: "partial",- deferred: reasonOnlyDeferred,- },- };+ const reasonOnlyInput = coverageInput({+ completeness: "partial",+ deferred: reasonOnlyDeferred,+ });const incompleteCodeEvidence = {id: "evidence-archive-sink",@@ -1258,172 +1225,101 @@ try {explanation: "The reviewed archive entry reaches a filesystem write.",};- const rejectedDraftInputs = [+ const rejectedDraftInputs: [string, unknown][] = [["finding rule IDs must describe a lowercase vulnerability family, not a CWE",- { ...input, findings: [{ ...finding, ruleId: "CWE-1321" }] },+ findingInput({ ruleId: "CWE-1321" }),],["finding taxonomy must use the candidate's canonical cwe array",- {- ...input,- findings: [- {- ...finding,- taxonomy: { category: "prototype-pollution", cweIds: ["CWE-1321"] },- },- ],- },+ findingInput({+ taxonomy: { category: "prototype-pollution", cweIds: ["CWE-1321"] },+ }),],["finding provenance must identify the actual source",- {- ...input,- findings: [- {- ...finding,- provenance: { candidateId: "candidate-b5b7a3d14a148f6a" },- },- ],- },+ findingInput({+ provenance: { candidateId: "candidate-b5b7a3d14a148f6a" },+ }),],["provided code evidence must contain the verified source snippet",- {- ...input,- findings: [{ ...finding, codeEvidence: [incompleteCodeEvidence] }],- },+ findingInput({ codeEvidence: [incompleteCodeEvidence] }),],["legacy code evidence must be an array",- {- ...input,- findings: [{ ...finding, code_evidence: null }],- },+ findingInput({ code_evidence: null }),],["legacy root-cause code must be text",- {- ...input,- findings: [{ ...finding, root_cause: { code: ["not text"] } }],- },+ findingInput({ root_cause: { code: ["not text"] } }),],["legacy root-cause language must be text",- {- ...input,- findings: [{ ...finding, root_cause: { language: 42 } }],- },+ findingInput({ root_cause: { language: 42 } }),],["canonical root-cause code must be text",- {- ...input,- findings: [- {- ...finding,- rootCause: { summary: "Root cause.", code: ["not text"] },- },- ],- },+ findingInput({+ rootCause: { summary: "Root cause.", code: ["not text"] },+ }),],["canonical root-cause language must be text",- {- ...input,- findings: [- {- ...finding,- rootCause: { summary: "Root cause.", language: 42 },- },- ],- },+ findingInput({ rootCause: { summary: "Root cause.", language: 42 } }),],["coverage surfaces must use canonical labels and dispositions",- {- ...input,- coverage: {- ...coverage,- surfaces: [{ surface: "Archive extraction", outcome: "reported" }],- },- },+ coverageInput({+ surfaces: [{ surface: "Archive extraction", outcome: "reported" }],+ }),],["reason-only deferred coverage rejects a missing reason",- {- ...input,- coverage: {- ...coverage,- completeness: "partial",- deferred: [{ paths: ["src/extract.py"] }],- },- },+ coverageInput({+ completeness: "partial",+ deferred: [{ paths: ["src/extract.py"] }],+ }),],["reason-only deferred coverage rejects a whitespace-only reason",- {- ...input,- coverage: {- ...coverage,- completeness: "partial",- deferred: [{ reason: " \t\n " }],- },- },+ coverageInput({+ completeness: "partial",+ deferred: [{ reason: " \t\n " }],+ }),],["explicit deferred identities do not bypass the required reason",- {- ...input,- coverage: {- ...coverage,- completeness: "partial",- deferred: [{ id: "deferred-explicit-archive" }],- },- },+ coverageInput({+ completeness: "partial",+ deferred: [{ id: "deferred-explicit-archive" }],+ }),],["deferred candidate identities must not contain only whitespace",- {- ...input,- coverage: {- ...coverage,- completeness: "partial",- deferred: [- {- candidateId: " ",- reason: "The upload runtime was unavailable.",- },- ],- },- },+ coverageInput({+ completeness: "partial",+ deferred: [+ {+ candidateId: " ",+ reason: "The upload runtime was unavailable.",+ },+ ],+ }),],["plain-string open questions must contain meaningful text",- {- ...input,- coverage: { ...coverage, openQuestions: [" \t "] },- },+ coverageInput({ openQuestions: [" \t "] }),],["structured open questions require a question",- {- ...input,- coverage: {- ...coverage,- openQuestions: [{ followUpPrompt: "Trace the upload boundary." }],- },- },+ coverageInput({+ openQuestions: [{ followUpPrompt: "Trace the upload boundary." }],+ }),],["structured open questions must not contain only whitespace",- {- ...input,- coverage: {- ...coverage,- openQuestions: [{ question: " \n " }],- },- },+ coverageInput({ openQuestions: [{ question: " \n " }] }),],["included scope paths are supplied by the authoritative scan",@@ -1435,69 +1331,43 @@ try {],["coverage mode is derived by the authoritative scan",- { ...input, coverage: { ...coverage, mode: "repository" } },+ coverageInput({ mode: "repository" }),],["inventory strategy is derived by the authoritative scan",- { ...input, coverage: { ...coverage, inventoryStrategy: "repository" } },+ coverageInput({ inventoryStrategy: "repository" }),],["coverage included paths are derived from the authoritative scope",- { ...input, coverage: { ...coverage, includePaths: ["src"] } },+ coverageInput({ includePaths: ["src"] }),],["coverage excluded paths are derived from the authoritative scope",- { ...input, coverage: { ...coverage, excludePaths: ["vendor"] } },+ coverageInput({ excludePaths: ["vendor"] }),],["top-level receipt references are not semantic coverage inputs",- {- ...input,- coverage: {- ...coverage,- receiptRefs: ["artifacts/02_discovery/candidate_ledger.jsonl"],- },- },+ coverageInput({+ receiptRefs: ["artifacts/02_discovery/candidate_ledger.jsonl"],+ }),],["finding IDs are generated during finalization",- {- ...input,- findings: [- {- ...finding,- findingId: "csf_0123456789abcdef01234567",- },- ],- },+ findingInput({ findingId: "csf_0123456789abcdef01234567" }),],["occurrence IDs are generated during finalization",- {- ...input,- findings: [- {- ...finding,- occurrenceId: "cso_0123456789abcdef01234567",- },- ],- },+ findingInput({ occurrenceId: "cso_0123456789abcdef01234567" }),],["finding fingerprints are generated during finalization",- {- ...input,- findings: [- {- ...finding,- fingerprints: {- algorithm: "codex-security/v1",- primary:- "codex-security/v1:sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",- },- },- ],- },+ findingInput({+ fingerprints: {+ algorithm: "codex-security/v1",+ primary:+ "codex-security/v1:sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",+ },+ }),],];@@ -1507,37 +1377,29 @@ try {["backslash", "candidate\\nested"],["control character", "candidate\u0001nested"],["oversized identity", "a".repeat(513)],- ]) {+ ] as const) {rejectedDraftInputs.push([`deferred candidate identities reject ${description}`,- {- ...input,- coverage: {- ...coverage,- completeness: "partial",- deferred: [- { candidateId, reason: "The candidate identity must remain safe." },- ],- },- },+ coverageInput({+ completeness: "partial",+ deferred: [+ { candidateId, reason: "The candidate identity must remain safe." },+ ],+ }),]);rejectedDraftInputs.push([`explicit deferred identities do not bypass invalid candidate ${description}`,- {- ...input,- coverage: {- ...coverage,- completeness: "partial",- deferred: [- {- id: "deferred-explicit-archive",- candidateId,- reason:- "The candidate identity must remain safe even with an explicit identity.",- },- ],- },- },+ coverageInput({+ completeness: "partial",+ deferred: [+ {+ id: "deferred-explicit-archive",+ candidateId,+ reason:+ "The candidate identity must remain safe even with an explicit identity.",+ },+ ],+ }),]);}@@ -1560,15 +1422,9 @@ try {);}assert.equal(- scanDraftInputSchema.safeParse({- ...input,- findings: [- {- ...finding,- taxonomy: { category: "path-traversal", cwe: [] },- },- ],- }).success,+ scanDraftInputSchema.safeParse(+ findingInput({ taxonomy: { category: "path-traversal", cwe: [] } }),+ ).success,true,"do not invent a CWE when the reviewed candidate has none",);@@ -1593,36 +1449,28 @@ try {false,);assert.equal(- scanDraftInputSchema.safeParse({- ...input,- findings: [{ ...finding, severity: { level: "moderate" } }],- }).success,+ scanDraftInputSchema.safeParse(+ findingInput({ severity: { level: "moderate" } }),+ ).success,false,);assert.equal(- scanDraftInputSchema.safeParse({- ...input,- findings: [{ ...finding, confidence: "high" }],- }).success,+ scanDraftInputSchema.safeParse(findingInput({ confidence: "high" }))+ .success,false,);assert.equal(- scanDraftInputSchema.safeParse({- ...input,- coverage: {- ...coverage,+ scanDraftInputSchema.safeParse(+ coverageInput({surfaces: [{ label: "surface", disposition: "unreviewed" }],- },- }).success,+ }),+ ).success,false,);assert.equal(- scanDraftInputSchema.safeParse({- ...input,- findings: [- { ...finding, locations: [{ path: "../outside.py", startLine: 1 }] },- ],- }).success,+ scanDraftInputSchema.safeParse(+ findingInput({ locations: [{ path: "../outside.py", startLine: 1 }] }),+ ).success,false,);assert.equal(@@ -1633,45 +1481,35 @@ try {false,);assert.equal(- scanDraftInputSchema.safeParse({- ...input,- coverage: { ...coverage, inventoryStrategy: "directory" },- }).success,+ scanDraftInputSchema.safeParse(+ coverageInput({ inventoryStrategy: "directory" }),+ ).success,false,);assert.equal(- scanDraftInputSchema.safeParse({- ...input,- coverage: { ...coverage, mode: "repository" },- }).success,+ scanDraftInputSchema.safeParse(coverageInput({ mode: "repository" }))+ .success,false,);assert.equal(- scanDraftInputSchema.safeParse({- ...input,- coverage: { ...coverage, includePaths: ["outside"] },- }).success,+ scanDraftInputSchema.safeParse(coverageInput({ includePaths: ["outside"] }))+ .success,false,);assert.equal(- scanDraftInputSchema.safeParse({- ...input,- coverage: { ...coverage, excludePaths: ["outside"] },- }).success,+ scanDraftInputSchema.safeParse(coverageInput({ excludePaths: ["outside"] }))+ .success,false,);assert.equal(- scanDraftInputSchema.safeParse({- ...input,- findings: [{ ...finding, findingId: "csf_0123456789abcdef01234567" }],- }).success,+ scanDraftInputSchema.safeParse(+ findingInput({ findingId: "csf_0123456789abcdef01234567" }),+ ).success,false,);assert.equal(- scanDraftInputSchema.safeParse({- ...input,- coverage: {- ...coverage,+ scanDraftInputSchema.safeParse(+ coverageInput({surfaces: [{label: "surface",@@ -1679,15 +1517,13 @@ try {receiptRefs: ["artifacts/02_discovery/candidate_ledger.jsonl"],},],- },- }).success,+ }),+ ).success,true,);assert.equal(- scanDraftInputSchema.safeParse({- ...input,- coverage: {- ...coverage,+ scanDraftInputSchema.safeParse(+ coverageInput({surfaces: [{label: "surface",@@ -1695,17 +1531,17 @@ try {receiptRefs: ["artifacts/../../outside.jsonl"],},],- },- }).success,+ }),+ ).success,false,);- let checkpointsVisibleBeforePublication = [];+ let checkpointsVisibleBeforePublication: string[] = [];await assert.rejects(recordCodexSecurityScanDraftViaWorkbench(context,input,- async (arguments_) => {+ async (arguments_: string[]) => {checkpointsVisibleBeforePublication = await readdir(path.join(root, "checkpoints"),).catch((error) => {@@ -1720,9 +1556,7 @@ try {const draftPathIndex = arguments_.indexOf("--draft-path");assert.notEqual(draftPathIndex, -1);assert.deepEqual(arguments_.slice(-2), ["--claim-token", claimToken]);- const staged = JSON.parse(- await readFile(arguments_[draftPathIndex + 1], "utf8"),- );+ const staged = await readJson(arguments_[draftPathIndex + 1]);assert.equal(staged.findings.findings.length, 1);assert.deepEqual(staged.findings.findings[0].taxonomy,@@ -1819,9 +1653,9 @@ try {await recordCodexSecurityScanDraftViaWorkbench(monotonicContext,staleCheckpoint,- async (arguments_) => {+ async (arguments_: string[]) => {const draftPath = arguments_[arguments_.indexOf("--draft-path") + 1];- const staged = JSON.parse(await readFile(draftPath, "utf8"));+ const staged = await readJson(draftPath);monotonicWrites += 1;if (monotonicWrites === 1) {await recordCodexSecurityScanDraft(monotonicContext, finalDraft);@@ -1845,7 +1679,9 @@ try {...workerInput,complete: false,});- const checkpointOnlyFinding = structuredClone(finding);+ const checkpointOnlyFinding: typeof finding & {+ identity?: { anchor: string };+ } = structuredClone(finding);checkpointOnlyFinding.title = "Checkpoint-only finding";checkpointOnlyFinding.identity = { anchor: "checkpoint-only-finding" };checkpointOnlyFinding.provenance.candidateId = "checkpoint-only-candidate";@@ -1865,12 +1701,15 @@ try {...withoutScanContext(workerInput),findings: [],});- const archivedRetryResult = JSON.parse(- await readFile(path.join(archivedRetryOutput, "result.json"), "utf8"),+ const archivedRetryResult = await readJson(+ archivedRetryOutput,+ "result.json",);assert.deepEqual(new Set(- archivedRetryResult.findings.map((item) => item.provenance.candidateId),+ archivedRetryResult.findings.map(+ (item: FixtureFinding) => item.provenance.candidateId,+ ),),new Set([finding.provenance.candidateId, "checkpoint-only-candidate"]),"a replacement attempt must reconcile newer checkpoints with an older archived result",@@ -1922,8 +1761,9 @@ try {...withoutScanContext(workerInput),findings: [],});- const archivedResolutionResult = JSON.parse(- await readFile(path.join(archivedResolutionOutput, "result.json"), "utf8"),+ const archivedResolutionResult = await readJson(+ archivedResolutionOutput,+ "result.json",);assert.deepEqual(archivedResolutionResult.findings,@@ -1950,11 +1790,10 @@ try {const [findingCheckpointName] = await readdir(path.join(repeatedCheckpointOutput, "checkpoints"),);- repeatedFindingDraft = JSON.parse(- await readFile(- path.join(repeatedCheckpointOutput, "checkpoints", findingCheckpointName),- "utf8",- ),+ repeatedFindingDraft = await readJson(+ repeatedCheckpointOutput,+ "checkpoints",+ findingCheckpointName,);await recordCodexSecurityWorkerScanDraft(repeatedCheckpointContext, {...workerInput,@@ -1998,12 +1837,13 @@ try {...withoutScanContext(workerInput),findings: [],});- const repeatedCheckpointResult = JSON.parse(- await readFile(path.join(repeatedCheckpointOutput, "result.json"), "utf8"),+ const repeatedCheckpointResult = await readJson(+ repeatedCheckpointOutput,+ "result.json",);assert.deepEqual(repeatedCheckpointResult.findings.map(- (item) => item.provenance.candidateId,+ (item: FixtureFinding) => item.provenance.candidateId,),[finding.provenance.candidateId],"a byte-identical repeated checkpoint must supersede an intervening demotion",@@ -2044,9 +1884,7 @@ try {...withoutScanContext(workerInput),findings: [],});- const multiAttemptResult = JSON.parse(- await readFile(path.join(multiAttemptOutput, "result.json"), "utf8"),- );+ const multiAttemptResult = await readJson(multiAttemptOutput, "result.json");assert.deepEqual(multiAttemptResult.findings,[finding],@@ -2087,9 +1925,7 @@ try {findings: [],});assert.deepEqual(- JSON.parse(- await readFile(path.join(malformedArchivedOutput, "result.json"), "utf8"),- ).findings,+ (await readJson(malformedArchivedOutput, "result.json")).findings,[finding],"valid checkpoints must recover evidence when an archived result is malformed",);@@ -2110,9 +1946,7 @@ try {await mkdir(crossScanAttempts);await rename(crossScanRetryOutput, crossScanAttempt);const crossScanResultPath = path.join(crossScanAttempt, "result.json");- const crossScanResult = JSON.parse(- await readFile(crossScanResultPath, "utf8"),- );+ const crossScanResult = await readJson(crossScanResultPath);crossScanResult.scanId = "11111111-1111-4111-8111-111111111111";await writeFile(crossScanResultPath, JSON.stringify(crossScanResult));await mkdir(crossScanRetryOutput);@@ -2136,7 +1970,10 @@ try {await mkdir(unreadableCheckpointRoot, { recursive: true });await mkdir(unreadableRetryOutput);const originalLstat = fsPromises.lstat;- fsPromises.lstat = async (candidate, ...arguments_) => {+ fsPromises.lstat = (async (+ candidate: Parameters<typeof originalLstat>[0],+ ...arguments_: [options?: import("node:fs").StatOptions]+ ) => {if (path.resolve(String(candidate)) === path.resolve(unreadableCheckpointRoot)) {@@ -2145,7 +1982,7 @@ try {});}return originalLstat(candidate, ...arguments_);- };+ }) as typeof fsPromises.lstat;try {await assert.rejects(recordCodexSecurityWorkerScanDraft(@@ -2159,12 +1996,10 @@ try {fsPromises.lstat = originalLstat;}- const partialDeferredRoot = path.join(root, "partial-deferred-worker");- await mkdir(partialDeferredRoot);- const partialDeferredContext = {- ...workerContext,- root: partialDeferredRoot,- };+ const partialDeferredContext = await createWorkerContext(+ "partial-deferred-worker",+ );+ const partialDeferredRoot = partialDeferredContext.root;const partialDeferredFinding = {summary: "Partial evidence captured before validation.",};@@ -2186,8 +2021,9 @@ try {},});await recordCodexSecurityWorkerScanDraft(partialDeferredContext, workerInput);- const resolvedPartialDeferred = JSON.parse(- await readFile(path.join(partialDeferredRoot, "result.json"), "utf8"),+ const resolvedPartialDeferred = await readJson(+ partialDeferredRoot,+ "result.json",);assert.deepEqual(resolvedPartialDeferred.findings[0].provenance.previousFindings,@@ -2310,19 +2146,21 @@ try {await recordFreshScanDraft(context, reasonOnlyInput);const normalizedReasonOnlyCoverage = await readJson(root, "coverage.json");const generatedIds = normalizedReasonOnlyCoverage.deferred.map(- ({ id }) => id,+ ({ id }: { id: string }) => id,);- assert.ok(generatedIds.every((id) => typeof id === "string"));+ assert.ok(generatedIds.every((id: string) => typeof id === "string"));assert.equal(new Set(generatedIds).size, reasonOnlyDeferred.length);assert.deepEqual(- normalizedReasonOnlyCoverage.deferred.map(({ id, ...row }) => row),+ normalizedReasonOnlyCoverage.deferred.map(+ ({ id, ...row }: { id: string; [key: string]: unknown }) => row,+ ),reasonOnlyDeferred,"assigning IDs preserves every distinct ID-less observation",);for (const name of await readdir(path.join(root, "checkpoints"))) {const checkpoint = await readJson(path.join(root, "checkpoints"), name);assert.deepEqual(- checkpoint.coverage.deferred.map(({ id }) => id),+ checkpoint.coverage.deferred.map(({ id }: { id: string }) => id),generatedIds,"the first saved checkpoint already contains the published IDs",);@@ -2347,17 +2185,16 @@ try {reason:"A later candidate-backed deferred record already owns this semantic identity.",};- await recordFreshScanDraft(context, {- ...input,- coverage: {- ...coverage,+ await recordFreshScanDraft(+ context,+ coverageInput({completeness: "partial",deferred: [collidingWithExplicit, collidingWithCandidate],- },- });+ }),+ );const [reservedExplicitId, reservedCandidateId] = (await readJson(root, "coverage.json")- ).deferred.map(({ id }) => id);+ ).deferred.map(({ id }: { id: string }) => id);const reservedDeferred = [collidingWithExplicit,collidingWithCandidate,@@ -2370,14 +2207,10 @@ try {reason: "Keep the later candidate-backed deferred identity unchanged.",},];- await recordFreshScanDraft(context, {- ...input,- coverage: {- ...coverage,- completeness: "partial",- deferred: reservedDeferred,- },- });+ await recordFreshScanDraft(+ context,+ coverageInput({ completeness: "partial", deferred: reservedDeferred }),+ );assert.deepEqual((await readJson(root, "coverage.json")).deferred,[@@ -2393,10 +2226,9 @@ try {assert.equal("id" in reservedDeferred[3], false);const existingReceiptRefs = ["artifacts/02_discovery/candidate_ledger.jsonl"];- await recordFreshScanDraft(context, {- ...input,- coverage: {- ...coverage,+ await recordFreshScanDraft(+ context,+ coverageInput({surfaces: [{label: "Archive extraction",@@ -2404,8 +2236,8 @@ try {receiptRefs: existingReceiptRefs,},],- },- });+ }),+ );assert.deepEqual((await readJson(root, "coverage.json")).surfaces[0].receiptRefs,existingReceiptRefs,@@ -2452,26 +2284,26 @@ try {await rm(externalHardeningPortfolio);const explicitIdentity = { anchor: "preserve-the-existing-finding-identity" };- await recordFreshScanDraft(context, {- ...input,- findings: [{ ...finding, identity: explicitIdentity }],- });+ await recordFreshScanDraft(+ context,+ findingInput({ identity: explicitIdentity }),+ );assert.deepEqual((await readJson(root, "findings.json")).findings[0].identity,explicitIdentity,);- const identityRoot = path.join(root, "preserved-finding-identity-worker");- await mkdir(identityRoot);- const identityContext = { ...workerContext, root: identityRoot };+ const identityContext = await createWorkerContext(+ "preserved-finding-identity-worker",+ );+ const identityRoot = identityContext.root;await recordCodexSecurityWorkerScanDraft(identityContext, {...workerInput,findings: [{ ...finding, identity: explicitIdentity }],});await recordCodexSecurityWorkerScanDraft(identityContext, workerInput);assert.deepEqual(- JSON.parse(await readFile(path.join(identityRoot, "result.json"), "utf8"))- .findings[0].identity,+ (await readJson(identityRoot, "result.json")).findings[0].identity,explicitIdentity,"a later refinement inherits the stable identity of the matched saved finding",);@@ -2491,24 +2323,21 @@ try {});assert.deepEqual((await readJson(root, "findings.json")).findings.map(- (item) => item.identity.anchor,+ (item: FixtureFinding) => item.identity!.anchor,),["candidate-a", "candidate-b"],);- await recordFreshScanDraft(context, {- ...input,- findings: [- {- ...finding,- extensions: {- ...finding.extensions,- candidateId: "candidate-singleton",- reportId: "DSS-144-A",- },+ await recordFreshScanDraft(+ context,+ findingInput({+ extensions: {+ ...finding.extensions,+ candidateId: "candidate-singleton",+ reportId: "DSS-144-A",},- ],- });+ }),+ );assert.deepEqual((await readJson(root, "findings.json")).findings[0].identity,{ anchor: "candidate-singleton", instance: "dss-144-a" },@@ -2546,7 +2375,7 @@ try {});assert.deepEqual((await readJson(root, "findings.json")).findings.map(- (item) => item.identity,+ (item: FixtureFinding) => item.identity,),[{ anchor: "candidate-cross-rule", instance: "shared-report" },@@ -2579,7 +2408,7 @@ try {});assert.deepEqual((await readJson(root, "findings.json")).findings.map(- (item) => item.identity,+ (item: FixtureFinding) => item.identity,),[{ anchor: "candidate-shared", instance: "dss-145-a" },@@ -2606,7 +2435,7 @@ try {});assert.deepEqual((await readJson(root, "findings.json")).findings.map(- (item) => item.identity,+ (item: FixtureFinding) => item.identity,),[{ anchor: "candidate-authored-collision" },@@ -2644,7 +2473,7 @@ try {});assert.deepEqual((await readJson(root, "findings.json")).findings.map(- (item) => item.identity,+ (item: FixtureFinding) => item.identity,),[{ anchor: "candidate-authored-instance", instance: "dss-147-a" },@@ -2675,7 +2504,7 @@ try {const collisionCases = [{label: "authored",- findings: collisionFindings.map((item) => ({+ findings: collisionFindings.map((item: FixtureFinding) => ({...item,identity: authoredCollisionIdentity,})),@@ -2692,11 +2521,11 @@ try {{label: "authored with reserved suffixes",findings: [- ...collisionFindings.map((item) => ({+ ...collisionFindings.map((item: FixtureFinding) => ({...item,identity: reservedCollisionIdentity,})),- ...collisionFindings.slice(0, 2).map((item, index) => ({+ ...collisionFindings.slice(0, 2).map((item: FixtureFinding, index) => ({...item,identity: {...reservedCollisionIdentity,@@ -2719,21 +2548,24 @@ try {await recordFreshScanDraft(context, collisionInput);const standardFindings = (await readJson(root, "findings.json")).findings;assert.deepEqual(- standardFindings.map((item) => item.identity),+ standardFindings.map((item: FixtureFinding) => item.identity),collisionCase.findings.map(- (item) => item.identity ?? collisionCase.originalIdentity,+ (item: FixtureFinding) =>+ item.identity ?? collisionCase.originalIdentity,),`${collisionCase.label} identity collisions retain the existing Standard shape`,);assert.deepEqual(- standardFindings.map((item) => item.provenance),- collisionCase.findings.map((item) => item.provenance),+ standardFindings.map((item: FixtureFinding) => item.provenance),+ collisionCase.findings.map((item: FixtureFinding) => item.provenance),);const deepIdentityContext = { ...context, mode: "deep" };await recordFreshScanDraft(deepIdentityContext, collisionInput);const deepFindings = (await readJson(root, "findings.json")).findings;- const deepIdentities = deepFindings.map((item) => item.identity);+ const deepIdentities = deepFindings.map(+ (item: FixtureFinding) => item.identity,+ );assert.equal(deepFindings.length,collisionCase.findings.length,@@ -2747,14 +2579,14 @@ try {? collisionCase.originalIdentity: {...collisionCase.originalIdentity,- instance: `${collisionCase.originalIdentity.instance ?? "saved"}-${index + 1}`,+ instance: `${(collisionCase.originalIdentity as { instance?: string }).instance ?? "saved"}-${index + 1}`,},),`${collisionCase.label} collisions receive successive numeric suffixes without replacing authored identities`,);assert.deepEqual(- deepFindings.map((item) => item.provenance),- collisionCase.findings.map((item, index) =>+ deepFindings.map((item: FixtureFinding) => item.provenance),+ collisionCase.findings.map((item: FixtureFinding, index) =>index === 1 || index === 2? {...item.provenance,@@ -2764,14 +2596,14 @@ try {),);assert.deepEqual(- deepFindings.map((item) => item.locations),- collisionCase.findings.map((item) => item.locations),+ deepFindings.map((item: FixtureFinding) => item.locations),+ collisionCase.findings.map((item: FixtureFinding) => item.locations),);await recordCodexSecurityScanDraft(deepIdentityContext, collisionInput);assert.deepEqual((await readJson(root, "findings.json")).findings.map(- (item) => item.identity,+ (item: FixtureFinding) => item.identity,),deepIdentities,`${collisionCase.label} Deep finding identities remain stable when the accepted aggregate is republished`,@@ -2823,15 +2655,10 @@ try {legacyEvidencedFinding.attackPath,);- await recordFreshScanDraft(context, {- ...input,- findings: [- {- ...finding,- taxonomy: { category: "path-traversal", cwe: [] },- },- ],- });+ await recordFreshScanDraft(+ context,+ findingInput({ taxonomy: { category: "path-traversal", cwe: [] } }),+ );assert.deepEqual((await readJson(root, "findings.json")).findings[0].taxonomy,{ category: "path-traversal", cwe: [] },@@ -2843,7 +2670,7 @@ try {targetContract: {...context.targetContract,target: {- ...context.targetContract.target,+ ...(context.targetContract!.target as Record<string, unknown>),allowedKinds: ["directory_snapshot"],},},@@ -2939,14 +2766,10 @@ try {},};await recordFreshScanDraft(committedDiff, input);- const expectedDigest = createHash("sha256")- .update("codex-security-diff/v1\0")- .update(kind)- .update("\0")- .update("base123")- .update("\0")- .update("head456")- .digest("hex");+ const expectedDigest = hash(+ "sha256",+ `codex-security-diff/v1\0${kind}\0base123\0head456`,+ );assert.equal((await readJson(root, "scan-manifest.json")).scan.target.snapshotDigest,`codex-security-snapshot/v1:sha256:${expectedDigest}`,@@ -2959,18 +2782,8 @@ try {await recordFreshScanDraft(context, input);- const originalManifest = await readFile(- path.join(root, "scan-manifest.json"),- "utf8",- );- const originalFindings = await readFile(- path.join(root, "findings.json"),- "utf8",- );- const originalCoverage = await readFile(- path.join(root, "coverage.json"),- "utf8",- );+ const originalDraft = await snapshotScanDraft(root);+ assert.ok(originalDraft.every((artifact) => artifact !== null));for (const [description, rejectedInput] of rejectedDraftInputs) {await assert.rejects(@@ -2978,184 +2791,138 @@ try {Error,description,);- assert.equal(- await readFile(path.join(root, "scan-manifest.json"), "utf8"),- originalManifest,- `${description}: scan manifest must not be modified`,- );- assert.equal(- await readFile(path.join(root, "findings.json"), "utf8"),- originalFindings,- `${description}: findings must not be modified`,- );- assert.equal(- await readFile(path.join(root, "coverage.json"), "utf8"),- originalCoverage,- `${description}: coverage must not be modified`,- );+ const currentDraft = await snapshotScanDraft(root);+ for (const [index, artifact] of [+ "scan manifest",+ "findings",+ "coverage",+ ].entries()) {+ assert.equal(+ currentDraft[index],+ originalDraft[index],+ `${description}: ${artifact} must not be modified`,+ );+ }}await assert.rejects(- recordCodexSecurityScanDraft(context, {- ...input,- findings: [{ ...finding, severity: { level: "high", score: 8.1 } }],- }),+ recordCodexSecurityScanDraft(+ context,+ findingInput({ severity: { level: "high", score: 8.1 } }),+ ),/severity\.scoringSystem/,);await assert.rejects(- recordCodexSecurityScanDraft(context, {- ...input,- findings: [- {- ...finding,- locations: [{ path: "src/extract.py", startLine: 8, endLine: 2 }],- },- ],- }),+ recordCodexSecurityScanDraft(+ context,+ findingInput({+ locations: [{ path: "src/extract.py", startLine: 8, endLine: 2 }],+ }),+ ),/endLine.*precede startLine/,);await assert.rejects(- recordCodexSecurityScanDraft(context, {- ...input,- findings: [- {- ...finding,- code_evidence: [completeCodeEvidence, completeCodeEvidence],- },- ],- }),+ recordCodexSecurityScanDraft(+ context,+ findingInput({+ code_evidence: [completeCodeEvidence, completeCodeEvidence],+ }),+ ),/code_evidence\[1\]\.id duplicates/,);await assert.rejects(- recordCodexSecurityScanDraft(context, {- ...input,- findings: [- {- ...finding,- codeEvidence: [completeCodeEvidence],- code_evidence: [completeCodeEvidence],- },- ],- }),+ recordCodexSecurityScanDraft(+ context,+ findingInput({+ codeEvidence: [completeCodeEvidence],+ code_evidence: [completeCodeEvidence],+ }),+ ),/code_evidence\[0\]\.id duplicates/,);await assert.rejects(- recordCodexSecurityScanDraft(context, {- ...input,- coverage: {- ...coverage,+ recordCodexSecurityScanDraft(+ context,+ coverageInput({deferred: [{ id: "deferred-upload", reason: "Runtime unavailable." }],- },- }),+ }),+ ),/complete coverage cannot contain deferred/,);await assert.rejects(- recordCodexSecurityScanDraft(context, {- ...input,- coverage: {- ...coverage,+ recordCodexSecurityScanDraft(+ context,+ coverageInput({deferred: [{candidateId: "candidate-deferred-archive",reason: "The upload runtime was unavailable.",},],- },- }),+ }),+ ),/complete coverage cannot contain deferred/,);await assert.rejects(- recordCodexSecurityScanDraft(context, {- ...input,- coverage: {- ...coverage,+ recordCodexSecurityScanDraft(+ context,+ coverageInput({deferred: [{ reason: "The upload runtime was unavailable." }],- },- }),+ }),+ ),/complete coverage cannot contain deferred/,);- assert.equal(- await readFile(path.join(root, "scan-manifest.json"), "utf8"),- originalManifest,- );- assert.equal(- await readFile(path.join(root, "findings.json"), "utf8"),- originalFindings,- );- assert.equal(- await readFile(path.join(root, "coverage.json"), "utf8"),- originalCoverage,- );+ assert.deepEqual(await snapshotScanDraft(root), originalDraft);await assert.rejects(- recordCodexSecurityScanDraft(context, {- ...input,- coverage: {- ...coverage,+ recordCodexSecurityScanDraft(+ context,+ coverageInput({surfaces: [{ label: "Uploads", disposition: "needs_follow_up" }],- },- }),+ }),+ ),/complete coverage cannot contain needs_follow_up/,);await assert.rejects(- recordCodexSecurityScanDraft(context, {- ...input,- findings: [- {- ...finding,- validation: { evidenceRefs: ["missing-evidence"] },- },- ],- }),+ recordCodexSecurityScanDraft(+ context,+ findingInput({ validation: { evidenceRefs: ["missing-evidence"] } }),+ ),/evidenceRefs must refer/,);await assert.rejects(- recordCodexSecurityScanDraft(context, {- ...input,- findings: [- {- ...finding,- root_cause: { evidenceRefs: ["missing-root-cause-evidence"] },- },- ],- }),+ recordCodexSecurityScanDraft(+ context,+ findingInput({+ root_cause: { evidenceRefs: ["missing-root-cause-evidence"] },+ }),+ ),/root_cause\.evidenceRefs must refer/,);await assert.rejects(- recordCodexSecurityScanDraft(context, {- ...input,- findings: [- {- ...finding,- root_cause: { summary: ["not a string"] },- },- ],- }),+ recordCodexSecurityScanDraft(+ context,+ findingInput({ root_cause: { summary: ["not a string"] } }),+ ),/root_cause/,);await assert.rejects(- recordCodexSecurityScanDraft(context, {- ...input,- findings: [- {- ...finding,- attackPath: {- dataflow: { evidenceRefs: ["missing-dataflow-evidence"] },- },+ recordCodexSecurityScanDraft(+ context,+ findingInput({+ attackPath: {+ dataflow: { evidenceRefs: ["missing-dataflow-evidence"] },},- ],- }),+ }),+ ),/attackPath\.dataflow\.evidenceRefs must refer/,);await assert.rejects(- recordCodexSecurityScanDraft(context, {- ...input,- findings: [- {- ...finding,- validation: { evidence_refs: ["missing-validation-evidence"] },- },- ],- }),+ recordCodexSecurityScanDraft(+ context,+ findingInput({+ validation: { evidence_refs: ["missing-validation-evidence"] },+ }),+ ),/validation\.evidence_refs must refer/,);await assert.rejects(@@ -3186,7 +2953,10 @@ try {...context,targetContract: {...context.targetContract,- target: { ...context.targetContract.target, allowedKinds: [] },+ target: {+ ...(context.targetContract!.target as Record<string, unknown>),+ allowedKinds: [],+ },},},input,@@ -3194,18 +2964,7 @@ try {/no allowed target kind/,);- assert.equal(- await readFile(path.join(root, "scan-manifest.json"), "utf8"),- originalManifest,- );- assert.equal(- await readFile(path.join(root, "findings.json"), "utf8"),- originalFindings,- );- assert.equal(- await readFile(path.join(root, "coverage.json"), "utf8"),- originalCoverage,- );+ assert.deepEqual(await snapshotScanDraft(root), originalDraft);const duplicateSurfaceCoverage = {...coverage,@@ -3244,7 +3003,9 @@ try {coverage: duplicateSurfaceCoverage,});const normalizedDuplicateCoverage = await readJson(root, "coverage.json");- const surfaceIds = normalizedDuplicateCoverage.surfaces.map(({ id }) => id);+ const surfaceIds = normalizedDuplicateCoverage.surfaces.map(+ ({ id }: { id: string }) => id,+ );assert.equal(new Set(surfaceIds).size,duplicateSurfaceCoverage.surfaces.length,@@ -3255,14 +3016,20 @@ try {"surface-web-ui-2",]);assert.equal(surfaceIds[4], "surface_uploads");- assert.ok(surfaceIds.every((id) => /^[a-z0-9][a-z0-9._/-]*$/u.test(id)));+ assert.ok(+ surfaceIds.every((id: string) => /^[a-z0-9][a-z0-9._/-]*$/u.test(id)),+ );assert.deepEqual(normalizedDuplicateCoverage.deferred,duplicateSurfaceCoverage.deferred,);assert.deepEqual(- normalizedDuplicateCoverage.surfaces.map((surface) => surface.label),- duplicateSurfaceCoverage.surfaces.map((surface) => surface.label),+ normalizedDuplicateCoverage.surfaces.map(+ (surface: { disposition: string; label: string }) => surface.label,+ ),+ duplicateSurfaceCoverage.surfaces.map(+ (surface: { disposition: string; label: string }) => surface.label,+ ),);assert.deepEqual(normalizedDuplicateCoverage.surfaces[0].receiptRefs, ["artifacts/primary.json",@@ -3342,18 +3109,9 @@ try {};const sealedFindings = { scanId, findings: [] };const sealedCoverage = { scanId, surfaces: [] };- await writeFile(- path.join(root, "scan-manifest.json"),- `${JSON.stringify(sealedManifest)}\n`,- );- await writeFile(- path.join(root, "findings.json"),- `${JSON.stringify(sealedFindings)}\n`,- );- await writeFile(- path.join(root, "coverage.json"),- `${JSON.stringify(sealedCoverage)}\n`,- );+ await writeJsonLine(path.join(root, "scan-manifest.json"), sealedManifest);+ await writeJsonLine(path.join(root, "findings.json"), sealedFindings);+ await writeJsonLine(path.join(root, "coverage.json"), sealedCoverage);const completeContext = { ...context, status: "complete" };assert.deepEqual(@@ -3369,10 +3127,10 @@ try {},);- await writeFile(- path.join(root, "findings.json"),- `${JSON.stringify({ scanId: "d7caa0cf-b785-47ef-95e7-e753dc288608", findings: [] })}\n`,- );+ await writeJsonLine(path.join(root, "findings.json"), {+ scanId: "d7caa0cf-b785-47ef-95e7-e753dc288608",+ findings: [],+ });await assert.rejects(getCodexSecurityCompletedScan(completeContext, {scanId,@@ -3381,10 +3139,7 @@ try {/do not match the sealed workbench scan/,);- await writeFile(- path.join(root, "findings.json"),- `${JSON.stringify(sealedFindings)}\n`,- );+ await writeJsonLine(path.join(root, "findings.json"), sealedFindings);await writeFile(path.join(root, "coverage.json"), "{not valid JSON\n");await assert.rejects(getCodexSecurityCompletedScan(completeContext, {@@ -3394,12 +3149,9 @@ try {/malformed|invalid JSON/,);- await writeFile(- path.join(root, "coverage.json"),- `${JSON.stringify(sealedCoverage)}\n`,- );+ await writeJsonLine(path.join(root, "coverage.json"), sealedCoverage);const external = path.join(root, "external.json");- await writeFile(external, `${JSON.stringify(sealedFindings)}\n`);+ await writeJsonLine(external, sealedFindings);await rm(path.join(root, "findings.json"));await symlink(external, path.join(root, "findings.json"));await assert.rejects(@@ -3415,12 +3167,11 @@ try {console.log("Codex Security scan draft artifact tests passed");-async function readJson(rootDirectory, name) {- return JSON.parse(await readFile(path.join(rootDirectory, name), "utf8"));-}-// Projection cases below use the same fixture root but model independent scans.-async function recordFreshScanDraft(context, input) {+async function recordFreshScanDraft(+ context: ArtifactContext,+ input: ScanDraftInput,+) {await Promise.all([...["scan-manifest.json",@@ -3436,10 +3187,8 @@ async function recordFreshScanDraft(context, input) {return recordCodexSecurityScanDraft(context, input);}-function withoutScanContext({- scope: _scope,- threatModel: _threatModel,- ...input-}) {+function withoutScanContext<+ Input extends { scope?: unknown; threatModel?: unknown },+>({ scope: _scope, threatModel: _threatModel, ...input }: Input) {return input;}diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft_recovery.mjs b/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft_recovery.tssimilarity index 86%rename from plugins/codex-security/mcp-app/tests/test_artifact_scan_draft_recovery.mjsrename to plugins/codex-security/mcp-app/tests/test_artifact_scan_draft_recovery.tsindex 981807125..4826db998 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft_recovery.mjs+++ b/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft_recovery.ts@@ -1,6 +1,8 @@+import { readJson, writeJson } from "./support/json.ts";+import type { ScanDraftInput } from "../src/artifact-scan-draft.js";import assert from "node:assert/strict";import { execFile } from "node:child_process";-import { createHash } from "node:crypto";+import { hash } from "node:crypto";import { promises as fsPromises } from "node:fs";import {mkdir,@@ -14,20 +16,20 @@ import {} from "node:fs/promises";import path from "node:path";import test from "node:test";-import { fileURLToPath } from "node:url";import { isDeepStrictEqual, promisify } from "node:util";import {draftApi,fixture,interruptDraftWrite,-} from "./scan-draft-recovery-fixture.mjs";+ surfaceDisposition,+} from "./scan-draft-recovery-fixture.ts";const { recordCodexSecurityScanDraftViaWorkbench, saveScanDraftCheckpoint } =draftApi;const execFileAsync = promisify(execFile);const generic = { reason: "Review remains.", paths: ["src/example.py"] };-const close = (id, reason = "Review completed.") => ({ id, reason });-const findingFor = (candidateId) => ({+const close = (id: string, reason = "Review completed.") => ({ id, reason });+const findingFor = (candidateId: string) => ({ruleId: "fixture.review",title: "Synthetic review finding",summary: "The candidate outcome must survive publication.",@@ -46,13 +48,13 @@ for (const observation of ["checkpoint head", "worker result"]) {const headPath = path.join(f.root, "checkpoint-head.json");const resultPath = path.join(f.root, "result.json");await f.write(f.draft({ deferred: [task] }));- const reopenedHead = JSON.parse(await readFile(headPath, "utf8"));+ const reopenedHead = await readJson(headPath);await f.write(f.draft({ resolvedDeferred: [close(task.id)] }, true));assert.deepEqual((await f.read()).deferred, []);const checkpoints = path.join(f.root, "checkpoints");for (const name of await readdir(checkpoints)) {const checkpointPath = path.join(checkpoints, name);- const checkpoint = JSON.parse(await readFile(checkpointPath, "utf8"));+ const checkpoint = await readJson(checkpointPath);const time = checkpoint.coverage.deferred.length ? 50 : 100;await utimes(checkpointPath, time, time);}@@ -61,20 +63,17 @@ for (const observation of ["checkpoint head", "worker result"]) {if (observation === "checkpoint head") {await utimes(headPath, 100, 100);await utimes(resultPath, 200, 200);- await writeFile(replacementPath, JSON.stringify(reopenedHead));+ await writeJson(replacementPath, reopenedHead);} else {- await writeFile(headPath, JSON.stringify(reopenedHead));+ await writeJson(headPath, reopenedHead);await utimes(headPath, 200, 200);await utimes(resultPath, 100, 100);- await writeFile(- replacementPath,- JSON.stringify(f.draft({ deferred: [task] }, true)),- );+ await writeJson(replacementPath, f.draft({ deferred: [task] }, true));}await utimes(replacementPath, 300, 300);let replaced = false;- const replaceOnce = async (destination) => {+ const replaceOnce = async (destination: string) => {if (replaced) return;replaced = true;await rename(replacementPath, destination);@@ -83,44 +82,46 @@ for (const observation of ["checkpoint head", "worker result"]) {const originalReadFile = fsPromises.readFile;const originalOpen = fsPromises.open;if (observation === "checkpoint head") {- fsPromises.lstat = async (filename, ...args) => {+ t.mock.method(fsPromises, "lstat", (async (+ filename: Parameters<typeof originalLstat>[0],+ ...args: [options?: import("node:fs").StatOptions]+ ) => {const metadata = await originalLstat(filename, ...args);if (filename === headPath) await replaceOnce(headPath);return metadata;- };+ }) as typeof fsPromises.lstat);} else {// Replace after returning the old file's bytes, for both pathname and// descriptor readers. Its observation time must still belong to those bytes.- fsPromises.readFile = async (filename, ...args) => {- const contents = await originalReadFile(filename, ...args);- if (filename === resultPath) await replaceOnce(resultPath);+ t.mock.method(fsPromises, "readFile", (async (+ ...args: Parameters<typeof originalReadFile>+ ) => {+ const contents = await originalReadFile(...args);+ if (args[0] === resultPath) await replaceOnce(resultPath);return contents;- };- fsPromises.open = async (filename, ...args) => {+ }) as typeof fsPromises.readFile);+ t.mock.method(fsPromises, "open", (async (+ filename: Parameters<typeof originalOpen>[0],+ ...args: [flags: string | number, mode?: string | number]+ ) => {const handle = await originalOpen(filename, ...args);if (filename === resultPath) {const read = handle.readFile.bind(handle);- handle.readFile = async (...readArgs) => {+ handle.readFile = (async (...readArgs: Parameters<typeof read>) => {const contents = await read(...readArgs);await replaceOnce(resultPath);return contents;- };+ }) as typeof handle.readFile;}return handle;- };+ }) as typeof fsPromises.open);}- try {- const result = await f.write(f.draft({}, true));- assert.equal(replaced, true);- for (const coverage of [result.coverage, await f.read()]) {- assert.deepEqual(coverage.deferred, [task]);- assert.deepEqual(coverage.resolvedDeferred ?? [], []);- assert.equal(coverage.completeness, "partial");- }- } finally {- fsPromises.lstat = originalLstat;- fsPromises.readFile = originalReadFile;- fsPromises.open = originalOpen;+ const result = await f.write(f.draft({}, true));+ assert.equal(replaced, true);+ for (const coverage of [result.coverage, await f.read()]) {+ assert.deepEqual(coverage.deferred, [task]);+ assert.deepEqual(coverage.resolvedDeferred ?? [], []);+ assert.equal(coverage.completeness, "partial");}});}@@ -128,7 +129,7 @@ for (const observation of ["checkpoint head", "worker result"]) {for (const headTime of [1, 2, 3]) {test(`worker: stopped recovery retains accepted coverage with head time ${headTime}`, async (t) => {const f = await fixture(t, "worker");- const surface = (id) => ({+ const surface = (id: string) => ({id,label: id,disposition: "no_issue_found",@@ -154,9 +155,9 @@ for (const headTime of [1, 2, 3]) {),);const headPath = path.join(f.root, "checkpoint-head.json");- const head = JSON.parse(await readFile(headPath, "utf8"));+ const head = await readJson(headPath);const selectedPath = path.join(f.root, "checkpoints", head.checkpoint);- const selected = JSON.parse(await readFile(selectedPath, "utf8"));+ const selected = await readJson(selectedPath);assert.deepEqual(selected.coverage.surfaces, [surface("existing"),...added.surfaces,@@ -164,7 +165,7 @@ for (const headTime of [1, 2, 3]) {await utimes(resultPath, 2, 2);await utimes(headPath, headTime, headTime);await utimes(selectedPath, headTime, headTime);- const originals = new Map();+ const originals = new Map<string, Buffer>();for (const filename of [resultPath,headPath,@@ -172,12 +173,7 @@ for (const headTime of [1, 2, 3]) {path.join(f.root, "checkpoints", name),),]) {- originals.set(- filename,- createHash("sha256")- .update(await readFile(filename))- .digest("hex"),- );+ originals.set(filename, await readFile(filename));}const { stdout } = await execFileAsync(process.env.PYTHON?.trim() || "python3",@@ -193,17 +189,17 @@ binding={"status":"interrupted","allowedTargetKinds":["git_revision"],"target":{first=merge_saved_results(root,sys.argv[4],binding,[worker],[],stopped=True,reason="interrupted")replay=merge_saved_results(root,sys.argv[4],binding,[worker],[],stopped=True,reason="interrupted",frozen_source_digests=first[0]["scan"]["preservedSources"])print(json.dumps([first[2],replay[2]]))`,- fileURLToPath(new URL("../../scripts", import.meta.url)),+ path.join(import.meta.dirname, "../../scripts"),path.dirname(f.root),f.root,- f.context.scanId,+ f.context.scanId!,],);const [coverage, replay] = JSON.parse(stdout);assert.deepEqual(replay, coverage);for (const [field, rows] of Object.entries(added)) {assert.equal(- (coverage[field] ?? []).some((row) =>+ (coverage[field] ?? []).some((row: Record<string, unknown>) =>Object.entries(rows[0]).every(([key, value]) =>isDeepStrictEqual(row[key], value),),@@ -211,14 +207,13 @@ print(json.dumps([first[2],replay[2]]))`,headTime >= 2,);}- assert.ok(coverage.surfaces.some((row) => row.id === "existing"));- for (const [filename, digest] of originals) {- assert.equal(- createHash("sha256")- .update(await readFile(filename))- .digest("hex"),- digest,- );+ assert.ok(+ coverage.surfaces.some(+ (row: Record<string, unknown>) => row.id === "existing",+ ),+ );+ for (const [filename, contents] of originals) {+ assert.deepEqual(await readFile(filename), contents);}});}@@ -248,20 +243,11 @@ for (const updateSavedSurface of [false, true]) {);const independent = { id: "independent-review", ...generic };const finding = {- ruleId: "fixture.review",- title: "Synthetic review finding",+ ...findingFor("new-finding"),summary: "A separate review result must survive progress publication.",- severity: { level: "low" },- confidence: {- level: "high",- rationale: "Synthetic persistence fixture.",- },- taxonomy: { category: "other", cwe: [] },- locations: [{ path: "src/example.py", startLine: 1 }],remediation: "Complete the independent review.",- provenance: { source: "local_plugin", candidateId: "new-finding" },};- const surfaces = [+ const surfaces: (Record<string, unknown> & { id: string })[] = [{id: "api",label: "API",@@ -296,20 +282,11 @@ for (const updateSavedSurface of [false, true]) {receiptRefs: surface.receiptRefs ?? [],})),);- const published = JSON.parse(- await readFile(path.join(f.root, "result.json"), "utf8"),- );+ const published = await readJson(f.root, "result.json");assert.deepEqual(published.findings, [finding]);- const head = JSON.parse(- await readFile(path.join(f.root, "checkpoint-head.json"), "utf8"),- );+ const head = await readJson(f.root, "checkpoint-head.json");assert.deepEqual(- JSON.parse(- await readFile(- path.join(f.root, "checkpoints", head.checkpoint),- "utf8",- ),- ),+ await readJson(f.root, "checkpoints", head.checkpoint),published,);}@@ -340,11 +317,13 @@ for (const updateSavedSurface of [false, true]) {assert.equal(result.findingCount, 2);assert.equal(result.coverage.completeness, "partial");assert.deepEqual(- new Set(result.coverage.deferred.map(({ id }) => id)),+ new Set(result.coverage.deferred.map(({ id }: { id: string }) => id)),new Set([reopened.id, independent.id, nextTask.id]),);assert.deepEqual(- result.coverage.deferred.find(({ id }) => id === updated.id),+ result.coverage.deferred.find(+ ({ id }: { id: string }) => id === updated.id,+ ),updated,);assert.deepEqual((await f.read()).deferred, result.coverage.deferred);@@ -352,8 +331,8 @@ for (const updateSavedSurface of [false, true]) {close(stillClosed.id),]);assert.deepEqual(- new Set(result.coverage.surfaces.map(({ id }) => id)),- new Set([...surfaces, nextSurface].map(({ id }) => id)),+ new Set(result.coverage.surfaces.map(({ id }: { id: string }) => id)),+ new Set([...surfaces, nextSurface].map(({ id }: { id: string }) => id)),);}});@@ -372,18 +351,11 @@ for (const outcome of ["rejected", "reported"]) {disposition: outcome,};const finding = {+ ...findingFor(candidateId),ruleId: "fixture.accepted-review",title: "Accepted review finding",summary: "A completed candidate outcome remains authoritative.",- severity: { level: "low" },- confidence: {- level: "high",- rationale: "Synthetic persistence fixture.",- },- taxonomy: { category: "other", cwe: [] },- locations: [{ path: "src/example.py", startLine: 1 }],remediation: "Complete the independent review.",- provenance: { source: "local_plugin", candidateId },};const genericTask = { id: "generic-review", ...generic };await f.write({@@ -414,10 +386,7 @@ for (const outcome of ["rejected", "reported"]) {assert.equal(result.findingCount, outcome === "reported" ? 1 : 0);assert.deepEqual(result.coverage.deferred, [genericTask]);assert.deepEqual(- result.coverage.surfaces.map(({ id, disposition }) => ({- id,- disposition,- })),+ result.coverage.surfaces.map(surfaceDisposition),surfaceLink === "none"? []: [{ id: surface.id, disposition: outcome }],@@ -425,9 +394,7 @@ for (const outcome of ["rejected", "reported"]) {if (outcome === "rejected") {assert.deepEqual(result.coverage.surfaces[0].candidate, candidate);} else {- const saved = JSON.parse(- await readFile(path.join(f.root, "result.json"), "utf8"),- );+ const saved = await readJson(f.root, "result.json");assert.deepEqual(saved.findings[0].provenance.originalCandidates, [candidate,]);@@ -437,7 +404,7 @@ for (const outcome of ["rejected", "reported"]) {}}-for (const layout of ["standard", "diff", "worker"]) {+for (const layout of ["standard", "diff", "worker"] as const) {test(`${layout}: moving a saved task stops blocking its former surface`, async (t) => {const f = await fixture(t, layout);const first = {@@ -468,11 +435,15 @@ for (const layout of ["standard", "diff", "worker"]) {const result = await f.write(input);assert.deepEqual(result.coverage.deferred, [moved]);assert.equal(- result.coverage.surfaces.find(({ id }) => id === first.id).disposition,+ result.coverage.surfaces.find(+ ({ id }: { id: string }) => id === first.id,+ ).disposition,"no_issue_found",);assert.equal(- result.coverage.surfaces.find(({ id }) => id === second.id).disposition,+ result.coverage.surfaces.find(+ ({ id }: { id: string }) => id === second.id,+ ).disposition,"needs_follow_up",);}@@ -554,13 +525,9 @@ for (const layout of ["standard", "diff", "worker"]) {const result = await f.write(f.draft({}, true));assert.equal(result.findingCount, 1);assert.equal(result.coverage.completeness, "partial");- assert.deepEqual(- result.coverage.surfaces.map(({ id, disposition }) => ({- id,- disposition,- })),- [{ id: surface.id, disposition: surface.disposition }],- );+ assert.deepEqual(result.coverage.surfaces.map(surfaceDisposition), [+ { id: surface.id, disposition: surface.disposition },+ ]);});for (const outcome of ["reported", "rejected", "not_applicable"]) {@@ -639,12 +606,14 @@ for (const layout of ["standard", "diff", "worker"]) {f.root,layout === "worker" ? "result.json" : "coverage.json",);- const saved = JSON.parse(await readFile(filename, "utf8"));+ const saved = await readJson(filename);(layout === "worker" ? saved.coverage : saved).deferred = tasks;- await writeFile(filename, JSON.stringify(saved));+ await writeJson(filename, saved);}- const snapshot = async (directory = f.root) => {- const files = [];+ const snapshot = async (+ directory = f.root,+ ): Promise<[string, string][]> => {+ const files: [string, string][] = [];for (const entry of await readdir(directory, { withFileTypes: true })) {const filename = path.join(directory, entry.name);if (entry.isDirectory()) files.push(...(await snapshot(filename)));@@ -719,7 +688,7 @@ for (const layout of ["standard", "diff", "worker"]) {}});- for (const payload of ["candidate", "finding"]) {+ for (const payload of ["candidate", "finding"] as const) {test(`${layout}: generic closure preserves ${payload} evidence until its explicit outcome`, async (t) => {const f = await fixture(t, layout);const pending = {@@ -757,15 +726,16 @@ for (const layout of ["standard", "diff", "worker"]) {assert.deepEqual(saved.deferred, []);assert.deepEqual(saved.resolvedDeferred, [closure]);assert.deepEqual(- saved.surfaces.find((row) => row.candidateId === pending.candidateId)[- payload- ],- pending[payload],+ saved.surfaces.find(+ (row: Record<string, unknown>) =>+ row.candidateId === pending.candidateId,+ )[payload],+ (pending as Record<string, unknown>)[payload],);});}- for (const payload of ["generic", "candidate", "finding"]) {+ for (const payload of ["generic", "candidate", "finding"] as const) {test(`${layout}: ID-less ${payload} checkpoints cannot borrow a saved identity`, async (t) => {const f = await fixture(t, layout);const evidence =@@ -793,10 +763,14 @@ for (const layout of ["standard", "diff", "worker"]) {const saved = await f.read();assert.equal(saved.completeness, "partial");assert.deepEqual(- saved.deferred.find((row) => row.id === named.id),+ saved.deferred.find(+ (row: Record<string, unknown>) => row.id === named.id,+ ),named,);- const independent = saved.deferred.find((row) => row.id !== named.id);+ const independent = saved.deferred.find(+ (row: Record<string, unknown>) => row.id !== named.id,+ );assert.ok(independent);assert.deepEqual(independent, { ...raw, id: independent.id });const outcome =@@ -858,12 +832,16 @@ for (const layout of ["standard", "diff", "worker"]) {const saved = await f.read();assert.deepEqual(saved.deferred, []);assert.deepEqual(- saved.surfaces.find((row) => row.id === other.id),+ saved.surfaces.find(+ (row: Record<string, unknown>) => row.id === other.id,+ ),other,);assert.equal(saved.completeness, "partial");assert.deepEqual(- saved.surfaces.find((row) => row.id === surface.id),+ saved.surfaces.find(+ (row: Record<string, unknown>) => row.id === surface.id,+ ),submittedId === surface.id? { ...surface, disposition: "no_issue_found" }: surface,@@ -871,7 +849,7 @@ for (const layout of ["standard", "diff", "worker"]) {});}- for (const payload of ["generic", "candidate"]) {+ for (const payload of ["generic", "candidate"] as const) {test(`${layout}: closing one task retains ${payload} work on a shared surface`, async (t) => {const f = await fixture(t, layout);const first = { id: "review-a", ...generic, surfaceIds: ["shared"] };@@ -952,7 +930,7 @@ for (const layout of ["standard", "diff", "worker"]) {const checkpoints = path.join(f.root, "checkpoints");for (const name of await readdir(checkpoints)) {const file = path.join(checkpoints, name);- const row = JSON.parse(await readFile(file, "utf8"));+ const row = await readJson(file);const time = row.coverage.resolvedDeferred?.length ? 2 : 1;await utimes(file, time, time);}@@ -1012,7 +990,7 @@ for (const layout of ["standard", "diff", "worker"]) {const f = await fixture(t, layout);const pending = { id: "review", ...generic };const closing = f.draft({ resolvedDeferred: [close(pending.id)] }, true);- const fail = (input) =>+ const fail = (input: ScanDraftInput) =>interruptDraftWrite(path.join(f.root, destination), () =>f.write(input),);@@ -1022,10 +1000,10 @@ for (const layout of ["standard", "diff", "worker"]) {assert.deepEqual((await f.read()).deferred, []);assert.deepEqual((await f.read()).resolvedDeferred, [close(pending.id)]);const checkpointRoot = path.join(f.root, "checkpoints");- const originalClosures = [];+ const originalClosures: [string, number][] = [];for (const name of await readdir(checkpointRoot)) {const file = path.join(checkpointRoot, name);- const saved = JSON.parse(await readFile(file, "utf8"));+ const saved = await readJson(file);if (saved.coverage.resolvedDeferred?.length)originalClosures.push([file, (await stat(file)).mtimeMs]);}@@ -1068,7 +1046,7 @@ test("worker: inherited closures cannot erase ambiguous legacy tasks", async (t)const checkpoints = path.join(f.root, "checkpoints");for (const name of await readdir(checkpoints)) {const filename = path.join(checkpoints, name);- const saved = JSON.parse(await readFile(filename, "utf8"));+ const saved = await readJson(filename);const timestamp = saved.coverage.deferred.length ? 100 : 200;await utimes(filename, timestamp, timestamp);}@@ -1080,15 +1058,15 @@ test("worker: inherited closures cannot erase ambiguous legacy tasks", async (t)}});-for (const layout of ["standard", "diff"]) {+for (const layout of ["standard", "diff"] as const) {for (const taskCount of [1, 2]) {test(`${layout}: an upgraded checkpoint retains ${taskCount} older canonical task IDs`, async (t) => {const f = await fixture(t, layout);await f.write(f.draft({ deferred: [generic] }));- const legacyId = `deferred-${createHash("sha256")- .update(JSON.stringify([generic.reason, generic.paths, []]))- .digest("hex")- .slice(0, 16)}`;+ const legacyId = `deferred-${hash(+ "sha256",+ JSON.stringify([generic.reason, generic.paths, []]),+ ).slice(0, 16)}`;const coverage = await f.read();const named = Array.from({ length: taskCount }, (_, index) => ({id: index === 0 ? legacyId : `${legacyId}-${index + 1}`,@@ -1096,10 +1074,7 @@ for (const layout of ["standard", "diff"]) {reason: generic.reason,}));coverage.deferred = named;- await writeFile(- path.join(f.root, "coverage.json"),- JSON.stringify(coverage),- );+ await writeJson(path.join(f.root, "coverage.json"), coverage);await rm(path.join(f.root, "checkpoint-head.json"), { force: true });await rm(path.join(f.root, "checkpoints"), {recursive: true,@@ -1119,21 +1094,27 @@ for (const layout of ["standard", "diff"]) {if (taskCount > 1)await f.write(f.draft(- { resolvedDeferred: named.slice(1).map(({ id }) => close(id)) },+ {+ resolvedDeferred: named+ .slice(1)+ .map(({ id }: { id: string }) => close(id)),+ },true,),);const retried = await f.write(f.draft({}, true));assert.deepEqual(retried.coverage.deferred, []);assert.deepEqual(- new Set(retried.coverage.resolvedDeferred.map(({ id }) => id)),- new Set(named.map(({ id }) => id)),+ new Set(+ retried.coverage.resolvedDeferred.map(({ id }: { id: string }) => id),+ ),+ new Set(named.map(({ id }: { id: string }) => id)),);});}}-for (const layout of ["standard", "diff"]) {+for (const layout of ["standard", "diff"] as const) {test(`${layout}: a staged explicit surface closeout survives omitted and repeated retries`, async (t) => {const f = await fixture(t, layout);const surface = {@@ -1166,9 +1147,9 @@ for (const layout of ["standard", "diff"]) {recordCodexSecurityScanDraftViaWorkbench(f.context,closed,- async (args) => {- const checkpoint = JSON.parse(- await readFile(args[args.indexOf("--checkpoint-path") + 1], "utf8"),+ async (args: string[]) => {+ const checkpoint = await readJson(+ args[args.indexOf("--checkpoint-path") + 1],);await saveScanDraftCheckpoint(f.context, checkpoint);throw new Error("interrupted draft write");@@ -1189,7 +1170,7 @@ for (const layout of ["standard", "diff"]) {});}-for (const layout of ["standard", "diff", "worker"]) {+for (const layout of ["standard", "diff", "worker"] as const) {for (const interrupted of [false, true]) {test(`${layout}: returned surface IDs support closeout after first-write interruption=${interrupted}`, async (t) => {const f = await fixture(t, layout);@@ -1215,9 +1196,7 @@ for (const layout of ["standard", "diff", "worker"]) {assert.deepEqual(initial.coverage, await f.read());assert.deepEqual(input, original);for (const name of await readdir(path.join(f.root, "checkpoints"))) {- const checkpoint = JSON.parse(- await readFile(path.join(f.root, "checkpoints", name), "utf8"),- );+ const checkpoint = await readJson(f.root, "checkpoints", name);assert.equal(checkpoint.coverage.surfaces[0].id, surface.id);assert.deepEqual(checkpoint.coverage.surfaces[0].receiptRefs, []);assert.equal(checkpoint.coverage.deferred[0].id, task.id);@@ -1265,24 +1244,27 @@ test("worker: duplicate authored surface IDs preserve each observation", async (];const initial = await f.write(f.draft({ surfaces: rows }));assert.deepEqual(- initial.coverage.surfaces.map(({ id }) => id),+ initial.coverage.surfaces.map(({ id }: { id: string }) => id),["api", "api-3", "api-2"],);assert.deepEqual((await f.write(f.draft({ surfaces: rows }))).coverage.surfaces,initial.coverage.surfaces,);- const updated = initial.coverage.surfaces.map((row) =>- row.id === "api-3" ? { ...row, disposition: "no_issue_found" } : row,+ const updated = initial.coverage.surfaces.map(+ (row: Record<string, unknown>) =>+ row.id === "api-3" ? { ...row, disposition: "no_issue_found" } : row,);const saved = await f.write(f.draft({ surfaces: updated }));assert.equal(saved.coverage.surfaces.length, 3);assert.equal(- saved.coverage.surfaces.find(({ id }) => id === "api").disposition,+ saved.coverage.surfaces.find(({ id }: { id: string }) => id === "api")+ .disposition,"needs_follow_up",);assert.equal(- saved.coverage.surfaces.find(({ id }) => id === "api-3").disposition,+ saved.coverage.surfaces.find(({ id }: { id: string }) => id === "api-3")+ .disposition,"no_issue_found",);});@@ -1317,8 +1299,8 @@ test("worker: generated surface IDs preserve distinct observations and reservedconst second = { ...first, notes: "Second caller." };const input = f.draft({ surfaces: [first, second] });const initial = await f.write(input);- const ids = initial.coverage.surfaces.map(({ id }) => id);- assert.ok(ids.every((id) => typeof id === "string"));+ const ids = initial.coverage.surfaces.map(({ id }: { id: string }) => id);+ assert.ok(ids.every((id: string) => typeof id === "string"));assert.equal(new Set(ids).size, 2);assert.deepEqual((await f.write(input)).coverage.surfaces,@@ -1329,7 +1311,8 @@ test("worker: generated surface IDs preserve distinct observations and reservedassert.equal(updated.coverage.surfaces.length, 3);assert.ok(updated.coverage.surfaces.some(- (row) => row.id === ids[0] && row.notes === first.notes,+ (row: Record<string, unknown>) =>+ row.id === ids[0] && row.notes === first.notes,),);const other = await fixture(t, "worker");@@ -1341,7 +1324,7 @@ test("worker: generated surface IDs preserve distinct observations and reserved];const saved = (await other.write(other.draft({ surfaces: reserved }))).coverage.surfaces;- assert.equal(new Set(saved.map(({ id }) => id)).size, 4);+ assert.equal(new Set(saved.map(({ id }: { id: string }) => id)).size, 4);assert.equal(saved[2].id, ids[0]);assert.equal(saved[3].candidateId, ids[1]);assert.notEqual(saved[0].id, ids[0]);@@ -1350,7 +1333,9 @@ test("worker: generated surface IDs preserve distinct observations and reservedconst retained = (await other.write(input)).coverage.surfaces;assert.equal(retained.length, saved.length);assert.deepEqual(- retained.find((row) => row.notes === second.notes),+ retained.find(+ (row: Record<string, unknown>) => row.notes === second.notes,+ ),saved[1],);}@@ -1374,7 +1359,7 @@ test("worker: archived closures cannot discard a later explicitly reopened task"assert.deepEqual((await f.read()).resolvedDeferred ?? [], []);});-for (const layout of ["standard", "diff"]) {+for (const layout of ["standard", "diff"] as const) {for (const selectedTime of [150, 200, 300]) {test(`${layout}: parent checkpoint selection at ${selectedTime} orders a closure`, async (t) => {const f = await fixture(t, layout);@@ -1389,7 +1374,7 @@ for (const layout of ["standard", "diff"]) {let selected;for (const name of await readdir(path.join(f.root, "checkpoints"))) {const file = path.join(f.root, "checkpoints", name);- const value = JSON.parse(await readFile(file, "utf8"));+ const value = await readJson(file);const time = value.coverage.resolvedDeferred?.length ? 100 : 200;await utimes(file, time, time);if (value.complete && value.coverage.resolvedDeferred?.length)@@ -1403,7 +1388,7 @@ for (const layout of ["standard", "diff"]) {])await utimes(path.join(f.root, name), 200, 200);const head = path.join(f.root, "checkpoint-head.json");- await writeFile(head, JSON.stringify({ checkpoint: selected }));+ await writeJson(head, { checkpoint: selected });await utimes(head, selectedTime, selectedTime);await f.write(f.draft({}, true));const saved = await f.read();@@ -1423,7 +1408,7 @@ for (const layout of ["standard", "diff"]) {}}-for (const payload of ["generic", "candidate"]) {+for (const payload of ["generic", "candidate"] as const) {test(`worker: legacy unnamed ${payload} evidence remains pending after a named closeout`, async (t) => {const f = await fixture(t, "worker");const raw = {@@ -1481,9 +1466,7 @@ for (const complete of [false, true]) {const files = await readdir(path.join(f.root, "checkpoints"));assert.equal(files.length, 1);assert.deepEqual(- JSON.parse(- await readFile(path.join(f.root, "checkpoints", files[0]), "utf8"),- ),+ await readJson(f.root, "checkpoints", files[0]),submitted,);await rm(destination);@@ -1515,7 +1498,7 @@ for (const malformed of [false, true]) {});}-for (const layout of ["standard", "diff", "worker"]) {+for (const layout of ["standard", "diff", "worker"] as const) {for (const reopened of [false, true]) {test(`${layout}: accepted progress remains incomplete after a terminal draft, reopened=${reopened}`, async (t) => {const f = await fixture(t, layout);@@ -1525,42 +1508,29 @@ for (const layout of ["standard", "diff", "worker"]) {await f.write(f.draft({ resolvedDeferred: [close(task.id)] }, true));for (const input of [f.draft({ deferred: [task] }), f.draft()]) {await f.write(input);- const published = JSON.parse(- await readFile(- path.join(- f.root,- layout === "worker" ? "result.json" : "scan-manifest.json",- ),- "utf8",- ),+ const published = await readJson(+ f.root,+ layout === "worker" ? "result.json" : "scan-manifest.json",);assert.equal(layout === "worker" ? published.complete : published.scan.complete,false,);if (layout === "worker") {- const head = JSON.parse(- await readFile(path.join(f.root, "checkpoint-head.json"), "utf8"),- );- const checkpoint = JSON.parse(- await readFile(- path.join(f.root, "checkpoints", head.checkpoint),- "utf8",- ),+ const head = await readJson(f.root, "checkpoint-head.json");+ const checkpoint = await readJson(+ f.root,+ "checkpoints",+ head.checkpoint,);assert.equal(checkpoint.complete, false);assert.deepEqual(checkpoint.coverage.deferred, [task]);}}await f.write(f.draft({ resolvedDeferred: [close(task.id)] }, true));- const finished = JSON.parse(- await readFile(- path.join(- f.root,- layout === "worker" ? "result.json" : "scan-manifest.json",- ),- "utf8",- ),+ const finished = await readJson(+ f.root,+ layout === "worker" ? "result.json" : "scan-manifest.json",);assert.notEqual(layout === "worker" ? finished.complete : finished.scan.complete,@@ -1573,14 +1543,9 @@ for (const layout of ["standard", "diff", "worker"]) {const f = await fixture(t, layout);await f.write(f.draft({}, true));await f.write(f.draft({ deferred: [{ id: "late", ...generic }] }));- const published = JSON.parse(- await readFile(- path.join(- f.root,- layout === "worker" ? "result.json" : "scan-manifest.json",- ),- "utf8",- ),+ const published = await readJson(+ f.root,+ layout === "worker" ? "result.json" : "scan-manifest.json",);assert.notEqual(layout === "worker" ? published.complete : published.scan.complete,@@ -1667,11 +1632,12 @@ for (const layout of ["standard", "diff", "worker"]) {assert.equal(restored.coverage.deferred.length, 3);assert.ok(restored.coverage.deferred.some(- (row) => row.candidateId === "candidate-a",+ (row: Record<string, unknown>) =>+ row.candidateId === "candidate-a",),);assert.ok(- restored.coverage.deferred.some((row) =>+ restored.coverage.deferred.some((row: Record<string, unknown>) =>isDeepStrictEqual(row, task),),);@@ -1700,13 +1666,16 @@ for (const layout of ["standard", "diff", "worker"]) {assert.equal(result.coverage.completeness, "partial");assert.equal(result.findingCount, outcome === "reported" ? 1 : 0);assert.deepEqual(- result.coverage.surfaces.filter(({ id }) => id === followUp.id),+ result.coverage.surfaces.filter(+ ({ id }: { id: string }) => id === followUp.id,+ ),[followUp],);if (outcome === "rejected")assert.equal(- result.coverage.surfaces.find(({ id }) => id === "outcome")- .disposition,+ result.coverage.surfaces.find(+ ({ id }: { id: string }) => id === "outcome",+ ).disposition,outcome,);}diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_storage.mjs b/plugins/codex-security/mcp-app/tests/test_artifact_storage.tssimilarity index 89%rename from plugins/codex-security/mcp-app/tests/test_artifact_storage.mjsrename to plugins/codex-security/mcp-app/tests/test_artifact_storage.tsindex 048974c9e..76fe8824f 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_storage.mjs+++ b/plugins/codex-security/mcp-app/tests/test_artifact_storage.ts@@ -1,4 +1,4 @@-import { temporaryDirectory } from "./support/temporary-directories.mjs";+import { temporaryDirectory } from "./support/temporary-directories.ts";import assert from "node:assert/strict";import { execFileSync } from "node:child_process";import {@@ -15,14 +15,14 @@ import path from "node:path";import { Client } from "@modelcontextprotocol/sdk/client/index.js";import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";-import { applicationRoot, buildServer } from "./build-server.mjs";+import { applicationRoot, buildServer } from "./build-server.ts";const fixture = await temporaryDirectory("codex-security-storage-test-", true);const stateRoot = path.join(fixture, "state");const repository = path.join(fixture, "repository");const bundle = path.join(fixture, "server.cjs");-const temporaryDirectories = new Set();-let client;+const temporaryDirectories = new Set<string>();+let client: Client | undefined;try {await mkdir(repository);@@ -34,7 +34,11 @@ try {},});client = await connect();- const started = await call("start_codex_security_standard_scan", {+ const started = await call<{+ scanId: string;+ scanDir: string;+ handoffClaimToken: string;+ }>("start_codex_security_standard_scan", {targetPath: repository,});const { scanId, scanDir, handoffClaimToken } = started;@@ -101,7 +105,7 @@ try {const outside = path.join(fixture, "outside.txt");await writeFile(outside, "outside\n");- await rejected(() =>+ await assert.rejects(() =>save({...identity,storage: "persistent",@@ -110,7 +114,7 @@ try {}),);await symlink(outside, path.join(scratch.directory, "linked.txt"));- await rejected(() =>+ await assert.rejects(() =>save({...identity,storage: "persistent",@@ -128,7 +132,7 @@ try {"artifacts/name.","artifacts/name ",]) {- await rejected(() =>+ await assert.rejects(() =>save({...identity,storage: "persistent",@@ -149,11 +153,11 @@ try {"artifacts/02_discovery/CANDIDATE_LEDGER.JSONL","artifacts/02_discovery/in_scope_files.txt",]) {- await rejected(() =>+ await assert.rejects(() =>save({ ...identity, storage: "persistent", path: owned, content: "bad" }),);}- await rejected(() =>+ await assert.rejects(() =>save({...identity,handoffClaimToken: "00000000-0000-4000-8000-000000000000",@@ -220,7 +224,7 @@ try {).content,legacyModel,);- await rejected(() =>+ await assert.rejects(() =>save({...identity,targetPath: repository,@@ -272,7 +276,9 @@ try {path: "threatmodel.md",});assert.equal(provisionalModel.content.slice(0, content.length), content);- const exportedModel = await call("export_codex_security_findings", {+ const exportedModel = await call<{+ export: { format: string; path: string };+ }>("export_codex_security_findings", {scanId,artifact: "threat-model",});@@ -288,7 +294,7 @@ try {),content,);- await rejected(() =>+ await assert.rejects(() =>call("export_codex_security_findings", {scanId,artifact: "threat-model",@@ -333,7 +339,7 @@ try {{ env: { ...process.env, CODEX_SECURITY_STATE_DIR: stateRoot } },);// Preparation seals receipts before the database transitions to complete.- await rejected(() =>+ await assert.rejects(() =>save({...identity,storage: "persistent",@@ -343,7 +349,7 @@ try {);assert.equal(await readFile(saved.path, "utf8"), content);await call("complete_codex_security_scan", identity);- await rejected(() =>+ await assert.rejects(() =>save({...identity,storage: "persistent",@@ -371,7 +377,7 @@ async function connect(overrides = {}) {name: "artifact-storage-test",version: "1.0.0",});- const env = {+ const env: Record<string, string> = {...process.env,CODEX_SECURITY_STATE_DIR: stateRoot,CODEX_HOME: path.join(fixture, "home"),@@ -389,22 +395,25 @@ async function connect(overrides = {}) {return result;}-async function call(name, arguments_) {- const result = await client.callTool({+async function call<Result = unknown>(+ name: string,+ arguments_: Record<string, unknown>,+): Promise<Result> {+ const result = await client!.callTool({name,arguments: arguments_,_meta: { "openai/threadId": "storage-test-owner" },});if (result.isError) throw new Error(JSON.stringify(result));- return result.structuredContent;+ return result.structuredContent as Result;}-function save(input) {- return call("save_codex_security_artifact", input);-}-function read(input) {- return call("read_codex_security_artifact", input);+function save(input: Record<string, unknown>) {+ return call<{ directory: string; path: string; relativePath: string }>(+ "save_codex_security_artifact",+ input,+ );}-async function rejected(action) {- await assert.rejects(action);+function read(input: Record<string, unknown>) {+ return call<{ content: string }>("read_codex_security_artifact", input);}diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.mjs b/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.tssimilarity index 94%rename from plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.mjsrename to plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.tsindex 15458e3c3..d19b85682 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.mjs+++ b/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.ts@@ -1,6 +1,19 @@+interface ArtifactToolResults {+ save_codex_security_artifact: {+ directory: string;+ path: string;+ relativePath: string;+ };+ read_codex_security_artifact: { content: string; directory: string };+ start_codex_security_standard_scan: {+ scanId: string;+ scanDir: string;+ handoffClaimToken: string;+ };+ record_codex_security_scan_draft: unknown;+}import assert from "node:assert/strict";import { execFile } from "node:child_process";-import { createRequire } from "node:module";import { promises as fs } from "node:fs";import { tmpdir, userInfo } from "node:os";import path from "node:path";@@ -9,8 +22,8 @@ import { test } from "node:test";import { promisify } from "node:util";import { Client } from "@modelcontextprotocol/sdk/client/index.js";import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";-import { build } from "esbuild";-import { applicationRoot, buildServer } from "./build-server.mjs";+import { importModule } from "./import-module.ts";+import { applicationRoot, buildServer } from "./build-server.ts";const pluginRoot = path.dirname(applicationRoot);const python = process.env.PYTHON || "python3";@@ -20,16 +33,15 @@ const fixture = await fs.realpath();const repository = path.join(fixture, "repository");const bundle = path.join(fixture, "server.cjs");-const helpers = path.join(fixture, "helpers.cjs");-const environment = {+const environment: Record<string, string | undefined> = {...process.env,CODEX_SECURITY_STATE_DIR: path.join(fixture, "state"),CODEX_HOME: path.join(fixture, "home"),};delete environment.CODEX_SECURITY_SCAN_ROOT;delete environment.PYTHONSAFEPATH;-const clients = [];-const temporaryDirectories = [];+const clients: Client[] = [];+const temporaryDirectories: string[] = [];await fs.mkdir(repository);await fs.writeFile(path.join(repository, "example.py"), "value = 1\n");@@ -39,12 +51,13 @@ await buildServer(bundle, {"import.meta.url": "__filename",},});-await build({- bundle: true,- format: "cjs",- platform: "node",+const {+ readCodexSecurityArtifact,+ saveCodexSecurityArtifact,+ standaloneArtifactContext,+ createScanArtifactContext,+} = await importModule({logLevel: "silent",- outfile: helpers,stdin: {resolveDir: applicationRoot,contents: `@@ -53,14 +66,8 @@ await build({`,},});-const {- readCodexSecurityArtifact,- saveCodexSecurityArtifact,- standaloneArtifactContext,- createScanArtifactContext,-} = createRequire(import.meta.url)(helpers);-async function workbench(args, input, launcher) {+async function workbench(args: string[], input?: string, launcher?: string) {const command = launcher? [launcher, path.join(pluginRoot, "scripts/workbench_db.py"), ...args]: [path.join(pluginRoot, "scripts/workbench_db.py"), ...args];@@ -69,12 +76,12 @@ async function workbench(args, input, launcher) {env: environment,maxBuffer: 4 * 1024 * 1024,});- execution.child.stdin.on("error", () => {});- execution.child.stdin.end(input);+ execution.child.stdin!.on("error", () => {});+ execution.child.stdin!.end(input);return JSON.parse((await execution).stdout);}-async function connect(overrides = {}) {+async function connect(overrides: Record<string, string | undefined> = {}) {const client = new Client({name: "artifact-regression-test",version: "1.0.0",@@ -88,17 +95,20 @@ async function connect(overrides = {}) {command: process.execPath,args: [bundle, "--stdio"],cwd: repository,- env,+ env: env as Record<string, string>,}),);- return async (name, args) => {+ return async <Name extends keyof ArtifactToolResults>(+ name: Name,+ args: Record<string, unknown>,+ ): Promise<ArtifactToolResults[Name]> => {const result = await client.callTool({name,arguments: args,_meta: { "openai/threadId": "artifact-regression-owner" },});assert.notEqual(result.isError, true, JSON.stringify(result));- return result.structuredContent;+ return result.structuredContent as ArtifactToolResults[Name];};}@@ -195,7 +205,8 @@ sys.argv = sys.argv[1:]runpy.run_path(sys.argv[0], run_name="__main__")`,);- const run = (args, input) => workbench(args, input, launcher);+ const run = (args: string[], input?: string) =>+ workbench(args, input, launcher);const read = async () => {if (reader === "import") {const saved = await saveCodexSecurityArtifact(@@ -380,7 +391,7 @@ runpy.run_path(sys.argv[0], run_name="__main__")await fs.readFile(saved.path, "utf8"),"temporary evidence\n",);- await clients.at(-1).close();+ await clients.at(-1)!.close();call = await connect(overrides);assert.equal((@@ -548,7 +559,7 @@ runpy.run_path(sys.argv[0], run_name="__main__")path: "artifacts/nested/proof.bin",content: "new artifact",},- (args, input) => workbench(args, input, launcher),+ (args: string[], input?: string) => workbench(args, input, launcher),);assert.equal(await fs.readFile(marker, "utf8"), "python");assert.equal(diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_validation_phase.mjs b/plugins/codex-security/mcp-app/tests/test_artifact_validation_phase.tssimilarity index 77%rename from plugins/codex-security/mcp-app/tests/test_artifact_validation_phase.mjsrename to plugins/codex-security/mcp-app/tests/test_artifact_validation_phase.tsindex 4146c7fd1..453d639cd 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_validation_phase.mjs+++ b/plugins/codex-security/mcp-app/tests/test_artifact_validation_phase.ts@@ -1,9 +1,11 @@-import { temporaryDirectory } from "./support/temporary-directories.mjs";+import { jsonLines, readJson, readJsonLines } from "./support/json.ts";+import { temporaryDirectory } from "./support/temporary-directories.ts";+import type { ArtifactContext } from "../src/artifact-io.js";import assert from "node:assert/strict";import { randomUUID } from "node:crypto";import { mkdir, readFile, rm, symlink, writeFile } from "node:fs/promises";import path from "node:path";-import { importSource } from "./import-module.mjs";+import { importSource } from "./import-module.ts";const {candidateValidationsInputSchema,@@ -12,14 +14,9 @@ const {new URL("../src/artifact-validation-phase.ts", import.meta.url).pathname,);-const toolSchema = JSON.parse(- await readFile(- new URL(- "../../schemas/tools/candidate-validations.schema.json",- import.meta.url,- ),- "utf8",- ),+const toolSchema = await readJson(+ import.meta.dirname,+ "../../schemas/tools/candidate-validations.schema.json",);assert.equal(toolSchema.$schema,@@ -87,15 +84,19 @@ assert.equal(const root = await temporaryDirectory("codex-security-validation-phase-", true);try {- const context = await scanContext(root, "scan", scanId);+ const context = {+ root: path.join(root, "scan"),+ repoRoot: root,+ layout: "scan" as const,+ };const ledger = path.join(- root,- "scan",+ context.root,"artifacts","02_discovery","candidate_ledger.jsonl",);- const original = [+ await mkdir(path.dirname(ledger), { recursive: true });+ const original: (ReturnType<typeof candidate> & Record<string, unknown>)[] = [{...candidate("candidate-a", "src/a.ts"),context: "Original discovery context must not change.",@@ -107,7 +108,7 @@ try {instance: "second-route",},];- await writeJsonl(ledger, original);+ await writeFile(ledger, jsonLines(original));const recorded = await recordCodexSecurityCandidateValidations(context, {validations: updates,@@ -121,16 +122,12 @@ try {{ ...original[0], validation: firstValidation },{ ...original[1], validation: secondValidation },];- assert.deepEqual(await readJsonl(ledger), expected);- assert.deepEqual(- (await readJsonl(ledger))[0].attack_path,- original[0].attack_path,- );+ assert.deepEqual(await readJsonLines(ledger), expected);await recordCodexSecurityCandidateValidations(context, {validations: updates,});- assert.deepEqual(await readJsonl(ledger), expected);+ assert.deepEqual(await readJsonLines(ledger), expected);await assertNoMutation(context,@@ -186,7 +183,7 @@ try {/scan-bound artifact context/,);- await writeJsonl(ledger, [original[0], original[0]]);+ await writeFile(ledger, jsonLines([original[0], original[0]]));await assertNoMutation(context,ledger,@@ -198,15 +195,15 @@ try {/repeats candidate candidate-a/,);- const empty = await scanContext(root, "empty", randomUUID());+ const empty = { ...context, root: path.join(root, "empty") };const emptyLedger = path.join(- root,- "empty",+ empty.root,"artifacts","02_discovery","candidate_ledger.jsonl",);- await writeJsonl(emptyLedger, []);+ await mkdir(path.dirname(emptyLedger), { recursive: true });+ await writeFile(emptyLedger, jsonLines([]));assert.deepEqual(await recordCodexSecurityCandidateValidations(empty, {validations: [],@@ -217,11 +214,11 @@ try {rowsWritten: 0,},);- assert.deepEqual(await readJsonl(emptyLedger), []);+ assert.deepEqual(await readJsonLines(emptyLedger), []);if (process.platform !== "win32") {const outside = path.join(root, "outside-candidate-ledger.jsonl");- await writeJsonl(outside, original);+ await writeFile(outside, jsonLines(original));await rm(ledger);await symlink(outside, ledger, "file");await assert.rejects(@@ -230,25 +227,13 @@ try {}),/symbolic|symlink|canonical|escape|contained|regular/i,);- assert.deepEqual(await readJsonl(outside), original);+ assert.deepEqual(await readJsonLines(outside), original);}} finally {await rm(root, { recursive: true, force: true });}-async function scanContext(root, directory, scanId) {- const scanRoot = path.join(root, directory);- const repository = path.join(root, "repository");- await Promise.all([- mkdir(path.join(scanRoot, "artifacts", "02_discovery"), {- recursive: true,- }),- mkdir(repository, { recursive: true }),- ]);- return { root: scanRoot, repoRoot: repository, layout: "scan", scanId };-}--function candidate(candidateId, sourcePath) {+function candidate(candidateId: string, sourcePath: string) {return {candidate_id: candidateId,cwe_ids: ["CWE-79"],@@ -259,7 +244,7 @@ function candidate(candidateId, sourcePath) {};}-function validation(disposition) {+function validation(disposition: string) {return {disposition,method: "Static source-to-sink trace.",@@ -275,7 +260,12 @@ function validation(disposition) {};}-async function assertNoMutation(context, ledger, input, expectedError) {+async function assertNoMutation(+ context: ArtifactContext,+ ledger: string,+ input: unknown,+ expectedError: RegExp,+) {const before = await readFile(ledger, "utf8");await assert.rejects(recordCodexSecurityCandidateValidations(context, input),@@ -283,17 +273,3 @@ async function assertNoMutation(context, ledger, input, expectedError) {);assert.equal(await readFile(ledger, "utf8"), before);}--async function writeJsonl(file, rows) {- await writeFile(- file,- rows.length > 0 ? `${rows.map(JSON.stringify).join("\n")}\n` : "",- );-}--async function readJsonl(file) {- return (await readFile(file, "utf8"))- .split(/\r?\n/)- .filter(Boolean)- .map((line) => JSON.parse(line));-}diff --git a/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.mjs b/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.tssimilarity index 79%rename from plugins/codex-security/mcp-app/tests/test_compact_artifact_server.mjsrename to plugins/codex-security/mcp-app/tests/test_compact_artifact_server.tsindex af975d9e5..564fda830 100644--- a/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.mjs+++ b/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.ts@@ -1,6 +1,39 @@+import { readJson, snapshotScanDraft, writeJson } from "./support/json.ts";+import { finding } from "./scan-draft-fixture.ts";import { gitText } from "../scripts/git.mjs";-import { temporaryDirectory } from "./support/temporary-directories.mjs";-import { readOnlyParentSandboxState } from "./sandbox-state.mjs";+import { temporaryDirectory } from "./support/temporary-directories.ts";+import type { TextContent } from "@modelcontextprotocol/sdk/types.js";+type CompletedResult = {+ scanId: string;+ manifest: {+ scan: {+ status: string;+ sealedAt: string;+ artifacts: unknown[];+ target: Record<string, unknown> & { snapshotDigest: string };+ };+ };+ findings: {+ findings: (Record<string, unknown> & {+ extensions: Record<string, unknown>;+ fingerprints: { primary: string };+ })[];+ };+ coverage: Record<string, unknown> & { surfaces: { disposition: string }[] };+};+type ToolResponse = Awaited<ReturnType<Client["callTool"]>>;+type WorkspaceResult = {+ workspace: { id: string; results: { scanId: string; scanDir: string } };+};+type ScanResult = {+ scan: {+ progress: Record<string, unknown>;+ reportAvailable: boolean;+ continuationThreadId?: string;+ handoffClaimToken?: string;+ };+};+import { readOnlyParentSandboxState } from "./sandbox-state.ts";import assert from "node:assert/strict";import { execFileSync } from "node:child_process";import { randomUUID } from "node:crypto";@@ -16,7 +49,7 @@ import path from "node:path";import { Client } from "@modelcontextprotocol/sdk/client/index.js";import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";-import { applicationRoot, buildServer } from "./build-server.mjs";+import { applicationRoot, buildServer } from "./build-server.ts";const pluginRoot = path.resolve(applicationRoot, "..");const bundledPluginRoot = process.env.CODEX_SECURITY_TEST_PLUGIN_ROOT@@ -35,35 +68,40 @@ try {target: "node20",});- await testParentToolList(runtimeBundle);- await testClaimedParentArtifactOperations(runtimeBundle, "source");- await testSemanticScanDraftCompletion(runtimeBundle, "source");- await testCompactDiffScanCompletion(runtimeBundle, "source");- await testDiscoveryWorkerToolList(runtimeBundle);- await testReducerWorkerToolList(runtimeBundle);-- const shippedRuntime = path.join(bundledPluginRoot, "mcp", "server.mjs");- await testParentToolList(shippedRuntime);- await testClaimedParentArtifactOperations(shippedRuntime, "shipped");- await testSemanticScanDraftCompletion(shippedRuntime, "shipped");- await testCompactDiffScanCompletion(shippedRuntime, "shipped");- await testDiscoveryWorkerToolList(shippedRuntime);- await testReducerWorkerToolList(shippedRuntime);+ for (const [bundle, runtimeLabel] of [+ [runtimeBundle, "source"],+ [path.join(bundledPluginRoot, "mcp", "server.mjs"), "shipped"],+ ]) {+ await testParentToolList(bundle);+ await testClaimedParentArtifactOperations(bundle, runtimeLabel);+ await testSemanticScanDraftCompletion(bundle, runtimeLabel);+ await testCompactDiffScanCompletion(bundle, runtimeLabel);+ await testDiscoveryWorkerToolList(bundle);+ await testReducerWorkerToolList(bundle);+ }} finally {await rm(temporaryRoot, { recursive: true, force: true });}-async function testCompactDiffScanCompletion(bundle, runtimeLabel) {- const fixtureRoot = path.join(temporaryRoot, `compact-diff-${runtimeLabel}`);+async function createScanFixture(label: string) {+ const fixtureRoot = path.join(temporaryRoot, label);const repoRoot = path.join(fixtureRoot, "repository");- const stateRoot = path.join(fixtureRoot, "state");- const scanRoot = path.join(fixtureRoot, "scans");- await Promise.all([- mkdir(path.join(repoRoot, "src"), { recursive: true }),- mkdir(stateRoot, { recursive: true }),- mkdir(scanRoot, { recursive: true }),- ]);- const git = (...arguments_) =>+ const environment = {+ CODEX_SECURITY_SCAN_ROOT: path.join(fixtureRoot, "scans"),+ CODEX_SECURITY_STATE_DIR: path.join(fixtureRoot, "state"),+ };+ await mkdir(path.join(repoRoot, "src"), { recursive: true });+ return { repoRoot, environment };+}++async function testCompactDiffScanCompletion(+ bundle: string,+ runtimeLabel: string,+) {+ const { repoRoot, environment } = await createScanFixture(+ `compact-diff-${runtimeLabel}`,+ );+ const git = (...arguments_: string[]) =>gitText(["-c",@@ -72,7 +110,7 @@ async function testCompactDiffScanCompletion(bundle, runtimeLabel) {...arguments_,],- { cwd: repoRoot, encoding: "utf8" },+ { cwd: repoRoot },).trim();git("init", "-q");await writeFile(path.join(repoRoot, "src", "guard.py"), "allowed = True\n");@@ -86,10 +124,7 @@ async function testCompactDiffScanCompletion(bundle, runtimeLabel) {git("commit", "-qm", "selected changes");const headRevision = git("rev-parse", "HEAD");- const client = await startClient(bundle, {- CODEX_SECURITY_SCAN_ROOT: scanRoot,- CODEX_SECURITY_STATE_DIR: stateRoot,- });+ const client = await startClient(bundle, environment);const ownerThread = `compact-diff-owner-${runtimeLabel}`;const call = toolCaller(client, ownerThread);@@ -100,37 +135,19 @@ async function testCompactDiffScanCompletion(bundle, runtimeLabel) {mode: "diff",diffTarget: { kind: "range", baseRevision, headRevision },};- const opened = requireSuccessfulTool(- await call("open_codex_security_workspace", selection),- `${runtimeLabel}: open compact diff workspace`,- );- const sessionId = opened.workspace.id;- requireSuccessfulTool(- await call("submit_codex_security_setup", { ...selection, sessionId }),- `${runtimeLabel}: submit compact diff setup`,- );- const started = requireSuccessfulTool(- await call("start_codex_security_scan", { sessionId }),- `${runtimeLabel}: start compact diff scan`,- );- const scanId = started.workspace.results.scanId;- const handoffClaimToken = randomUUID();- requireSuccessfulTool(- await call("claim_codex_security_scan_handoff_delivery", {- scanId,- claimToken: handoffClaimToken,- }),- `${runtimeLabel}: claim compact diff scan`,- );- requireSuccessfulTool(- await call("attach_codex_security_scan_continuation_thread", {- scanId,- claimToken: handoffClaimToken,- threadId: ownerThread,- }),- `${runtimeLabel}: attach compact diff owner`,+ const { scanId, handoffClaimToken } = await startScan(+ call,+ ownerThread,+ selection,+ {+ workspace: `${runtimeLabel}: open compact diff workspace`,+ setup: `${runtimeLabel}: submit compact diff setup`,+ scan: `${runtimeLabel}: start compact diff scan`,+ claim: `${runtimeLabel}: claim compact diff scan`,+ owner: `${runtimeLabel}: attach compact diff owner`,+ },);- requireSuccessfulTool(+ requireSuccessfulTool<ScanResult>(await call("get_codex_security_scan_context", {scanId,handoffClaimToken,@@ -193,7 +210,9 @@ async function testCompactDiffScanCompletion(bundle, runtimeLabel) {}),`${runtimeLabel}: record a diff candidate alongside a deleted file`,);- const candidates = requireSuccessfulTool(+ const candidates = requireSuccessfulTool<{+ rows: { candidate_id: string }[];+ }>(await call("list_codex_security_candidates", { scanId }),`${runtimeLabel}: read compact diff candidates`,);@@ -248,11 +267,11 @@ async function testCompactDiffScanCompletion(bundle, runtimeLabel) {}),`${runtimeLabel}: record compact diff canonical semantics`,);- requireSuccessfulTool(+ requireSuccessfulTool<ScanResult>(await call("complete_codex_security_scan", { scanId, handoffClaimToken }),`${runtimeLabel}: complete compact diff scan`,);- const completed = requireSuccessfulTool(+ const completed = requireSuccessfulTool<CompletedResult>(await call("get_codex_security_completed_scan", {scanId,handoffClaimToken,@@ -272,79 +291,41 @@ async function testCompactDiffScanCompletion(bundle, runtimeLabel) {}}-async function testSemanticScanDraftCompletion(bundle, runtimeLabel) {- const fixtureRoot = path.join(- temporaryRoot,+async function testSemanticScanDraftCompletion(+ bundle: string,+ runtimeLabel: string,+) {+ const { repoRoot, environment } = await createScanFixture(`semantic-draft-${runtimeLabel}`,);- const repoRoot = path.join(fixtureRoot, "repository");- const stateRoot = path.join(fixtureRoot, "state");- const scanRoot = path.join(fixtureRoot, "scans");- await Promise.all([- mkdir(path.join(repoRoot, "src"), { recursive: true }),- mkdir(stateRoot, { recursive: true }),- mkdir(scanRoot, { recursive: true }),- ]);const sourceLine = " return connection.execute(query)";await writeFile(path.join(repoRoot, "src", "fixture.py"),`def execute(query):\n${sourceLine}\n`,);- const client = await startClient(bundle, {- CODEX_SECURITY_SCAN_ROOT: scanRoot,- CODEX_SECURITY_STATE_DIR: stateRoot,- });+ const client = await startClient(bundle, environment);const ownerThread = `semantic-draft-owner-${runtimeLabel}`;const call = toolCaller(client, ownerThread);try {- const opened = requireSuccessfulTool(- await call("open_codex_security_workspace", {- targetPath: repoRoot,- scope: ".",- mode: "standard",- }),- `${runtimeLabel}: open semantic-draft workspace`,- );- const sessionId = opened.workspace.id;-- requireSuccessfulTool(- await call("submit_codex_security_setup", {- sessionId,- targetPath: repoRoot,- scope: ".",- mode: "standard",- }),- `${runtimeLabel}: submit semantic-draft setup`,- );-- const started = requireSuccessfulTool(- await call("start_codex_security_scan", {- sessionId,- }),- `${runtimeLabel}: start semantic-draft scan`,- );- const scanId = started.workspace.results.scanId;- const scanDirectory = started.workspace.results.scanDir;- const handoffClaimToken = randomUUID();-- requireSuccessfulTool(- await call("claim_codex_security_scan_handoff_delivery", {- scanId,- claimToken: handoffClaimToken,- }),- `${runtimeLabel}: claim semantic-draft scan`,- );- requireSuccessfulTool(- await call("attach_codex_security_scan_continuation_thread", {- scanId,- claimToken: handoffClaimToken,- threadId: ownerThread,- }),- `${runtimeLabel}: attach semantic-draft owner`,+ const {+ scanId,+ scanDir: scanDirectory,+ handoffClaimToken,+ } = await startScan(+ call,+ ownerThread,+ { targetPath: repoRoot, scope: ".", mode: "standard" },+ {+ workspace: `${runtimeLabel}: open semantic-draft workspace`,+ setup: `${runtimeLabel}: submit semantic-draft setup`,+ scan: `${runtimeLabel}: start semantic-draft scan`,+ claim: `${runtimeLabel}: claim semantic-draft scan`,+ owner: `${runtimeLabel}: attach semantic-draft owner`,+ },);- requireSuccessfulTool(+ requireSuccessfulTool<ScanResult>(await call("get_codex_security_scan_context", {scanId,handoffClaimToken,@@ -369,7 +350,7 @@ async function testSemanticScanDraftCompletion(bundle, runtimeLabel) {attackPaths: [],},],- ]) {+ ] as const) {requireToolError(await call(name, arguments_),/only available for Deep or diff scans/,@@ -547,7 +528,7 @@ async function testSemanticScanDraftCompletion(bundle, runtimeLabel) {rejected = await call("record_codex_security_scan_draft", malformed);} catch (error) {assert.equal(- error.code,+ (error as { code: number }).code,-32602,`${runtimeLabel}: malformed draft must fail before handler execution`,);@@ -618,7 +599,7 @@ async function testSemanticScanDraftCompletion(bundle, runtimeLabel) {},],]),- ]) {+ ] as const) {let invalid;try {invalid = await call("record_codex_security_scan_draft", {@@ -629,7 +610,7 @@ async function testSemanticScanDraftCompletion(bundle, runtimeLabel) {});} catch (error) {assert.equal(- error.code,+ (error as { code: number }).code,-32602,`${runtimeLabel}: ${description} must fail input validation`,);@@ -649,7 +630,7 @@ async function testSemanticScanDraftCompletion(bundle, runtimeLabel) {}const progress = async () =>- requireSuccessfulTool(+ requireSuccessfulTool<ScanResult>(await call("get_codex_security_scan_context", {scanId,handoffClaimToken,@@ -675,11 +656,12 @@ async function testSemanticScanDraftCompletion(bundle, runtimeLabel) {requireSuccessfulTool(await call("record_codex_security_scan_draft", checkpoint),);- const checkpointDeferred = JSON.parse(- await readFile(path.join(scanDirectory, "coverage.json"), "utf8"),- ).deferred;- const generatedIds = checkpointDeferred.slice(5, 9).map(({ id }) => id);- assert.ok(generatedIds.every((id) => typeof id === "string"));+ const checkpointDeferred = (await readJson(scanDirectory, "coverage.json"))+ .deferred;+ const generatedIds = checkpointDeferred+ .slice(5, 9)+ .map(({ id }: { id: string }) => id);+ assert.ok(generatedIds.every((id: string) => typeof id === "string"));assert.equal(new Set(generatedIds).size, generatedIds.length);const discovery = await progress();assert.equal(discovery.status, "running");@@ -725,9 +707,7 @@ async function testSemanticScanDraftCompletion(bundle, runtimeLabel) {schemaVersion,scanId: coverageScanId,...savedCoverage- } = JSON.parse(- await readFile(path.join(scanDirectory, "coverage.json"), "utf8"),- );+ } = await readJson(scanDirectory, "coverage.json");assert.equal(documentType, "codex-security.coverage");assert.equal(schemaVersion, "1.0");assert.equal(coverageScanId, scanId);@@ -748,14 +728,14 @@ async function testSemanticScanDraftCompletion(bundle, runtimeLabel) {unit: null,});- const completed = requireSuccessfulTool(+ const completed = requireSuccessfulTool<ScanResult>(await call("complete_codex_security_scan", { scanId, handoffClaimToken }),`${runtimeLabel}: finalize the accepted draft exactly once`,);assert.equal(completed.scan.progress.status, "complete");assert.equal(completed.scan.reportAvailable, true);- const results = requireSuccessfulTool(+ const results = requireSuccessfulTool<CompletedResult>(await call("get_codex_security_completed_scan", {scanId,handoffClaimToken,@@ -848,81 +828,40 @@ async function testSemanticScanDraftCompletion(bundle, runtimeLabel) {}}-async function snapshotScanDraft(scanDirectory) {- return Promise.all(- ["scan-manifest.json", "findings.json", "coverage.json"].map(- async (artifact) => {- try {- return await readFile(path.join(scanDirectory, artifact), "utf8");- } catch (error) {- if (error.code === "ENOENT") return null;- throw error;- }- },- ),- );-}--async function testClaimedParentArtifactOperations(bundle, runtimeLabel) {- const repoRoot = path.join(- temporaryRoot,- `claimed-parent-${runtimeLabel}-repository`,+async function testClaimedParentArtifactOperations(+ bundle: string,+ runtimeLabel: string,+) {+ const { repoRoot, environment } = await createScanFixture(+ `claimed-parent-${runtimeLabel}`,);- const stateRoot = path.join(- temporaryRoot,- `claimed-parent-${runtimeLabel}-state`,- );- const scanRoot = path.join(- temporaryRoot,- `claimed-parent-${runtimeLabel}-scans`,- );- await Promise.all([- mkdir(path.join(repoRoot, "src"), { recursive: true }),- mkdir(stateRoot, { recursive: true }),- mkdir(scanRoot, { recursive: true }),- ]);await writeFile(path.join(repoRoot, "src", "fixture.py"),"print('fixture')\n",);- const client = await startClient(bundle, {- CODEX_SECURITY_SCAN_ROOT: scanRoot,- CODEX_SECURITY_STATE_DIR: stateRoot,- });+ const client = await startClient(bundle, environment);const ownerThread = `compact-artifact-owner-${runtimeLabel}`;const otherThread = `compact-artifact-other-${runtimeLabel}`;const call = toolCaller(client, ownerThread);try {- const opened = requireSuccessfulTool(- await call("open_codex_security_workspace", {- targetPath: repoRoot,- scope: ".",- mode: "deep",- }),- "open claimed parent workspace",- );- const sessionId = opened.workspace.id;-- requireSuccessfulTool(- await call("submit_codex_security_setup", {- sessionId,- targetPath: repoRoot,- scope: ".",- mode: "deep",- }),- "submit claimed parent setup",- );-- const started = requireSuccessfulTool(- await call("start_codex_security_scan", {- sessionId,- }),- "start claimed parent scan",+ const {+ scanId,+ scanDir: scanDirectory,+ handoffClaimToken: claimToken,+ } = await startScan(+ call,+ ownerThread,+ { targetPath: repoRoot, scope: ".", mode: "deep" },+ {+ workspace: "open claimed parent workspace",+ setup: "submit claimed parent setup",+ scan: "start claimed parent scan",+ claim: "claim parent scan handoff",+ owner: "attach parent scan owner",+ },);- const scanId = started.workspace.results.scanId;- const scanDirectory = started.workspace.results.scanDir;const inventoryPath = path.join(scanDirectory,"artifacts",@@ -935,25 +874,7 @@ async function testClaimedParentArtifactOperations(bundle, runtimeLabel) {"02_discovery","candidate_ledger.jsonl",);- const claimToken = randomUUID();-- requireSuccessfulTool(- await call("claim_codex_security_scan_handoff_delivery", {- scanId,- claimToken,- }),- "claim parent scan handoff",- );- requireSuccessfulTool(- await call("attach_codex_security_scan_continuation_thread", {- scanId,- claimToken,- threadId: ownerThread,- }),- "attach parent scan owner",- );-- const phaseCalls = [+ const phaseCalls: [string, Record<string, unknown>][] = [["list_codex_security_review_items", { scanId }],["list_codex_security_candidates", { scanId }],[@@ -970,7 +891,7 @@ async function testClaimedParentArtifactOperations(bundle, runtimeLabel) {);}- const delivered = requireSuccessfulTool(+ const delivered = requireSuccessfulTool<ScanResult>(await call("get_codex_security_scan_context", {scanId,handoffClaimToken: claimToken,@@ -986,7 +907,7 @@ async function testClaimedParentArtifactOperations(bundle, runtimeLabel) {"record_codex_security_discovery_candidates",{ scanId, candidates: [] },],- ]) {+ ] as const) {requireToolError(await call(name, arguments_),/only available for diff scans/,@@ -1043,7 +964,7 @@ async function testClaimedParentArtifactOperations(bundle, runtimeLabel) {assert.deepEqual(explicitlyListed.items, [{ path: "./src/fixture.py" }]);const originalInventory = await readFile(inventoryPath, "utf8");- const listed = requireSuccessfulTool(+ const listed = requireSuccessfulTool<{ rows: { candidate_id: string }[] }>(await call("list_codex_security_candidates", {scanId,}),@@ -1124,7 +1045,7 @@ async function testClaimedParentArtifactOperations(bundle, runtimeLabel) {"import sqlite3,sys; c=sqlite3.connect(sys.argv[1]); " +"c.execute('UPDATE scans SET handoff_claim_token = ? WHERE id = ?', " +"(sys.argv[2],sys.argv[3])); c.commit(); c.close()",- path.join(stateRoot, "workbench.sqlite3"),+ path.join(environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),randomUUID(),scanId,]);@@ -1142,27 +1063,35 @@ async function testClaimedParentArtifactOperations(bundle, runtimeLabel) {}}-function requireSuccessfulTool(result, label) {+function requireSuccessfulTool<Result = Record<string, unknown>>(+ result: ToolResponse,+ label?: string,+): Result {assert.notEqual(result.isError,true,- `${label}: ${result.content?.[0]?.text ?? "tool failed"}`,+ `${label}: ${(result.content as TextContent[] | undefined)?.[0]?.text ?? "tool failed"}`,);assert.ok(result.structuredContent, `${label}: missing structured result`);- return result.structuredContent;+ return result.structuredContent as Result;}-function requireToolError(result, expected, label) {+function requireToolError(+ result: ToolResponse,+ expected: RegExp,+ label: string,+) {assert.equal(result.isError, true, `${label}: unexpectedly succeeded`);- assert.match(result.content?.[0]?.text ?? "", expected, label);+ assert.match(+ (result.content as TextContent[] | undefined)?.[0]?.text ?? "",+ expected,+ label,+ );}-async function testParentToolList(bundle) {- const stateRoot = await mkdtemp(- path.join(temporaryRoot, "parent-tool-state-"),- );+async function testParentToolList(bundle: string) {const client = await startClient(bundle, {- CODEX_SECURITY_STATE_DIR: stateRoot,+ CODEX_SECURITY_STATE_DIR: path.join(temporaryRoot, randomUUID()),});try {assert.deepEqual(@@ -1174,7 +1103,12 @@ async function testParentToolList(bundle) {);const tools = (await client.listTools()).tools;for (const tool of tools) {- if (tool._meta?.ui?.visibility?.includes("model") === false) continue;+ if (+ (+ tool._meta?.ui as { visibility?: string[] } | undefined+ )?.visibility?.includes("model") === false+ )+ continue;const projectedName = `mcp__codex_security__${tool.name}`;assert.ok(projectedName.length <= 64,@@ -1212,19 +1146,17 @@ async function testParentToolList(bundle) {);}- for (const tool of tools.filter(- (entry) =>- names.has(entry.name) &&- [- "prepare_codex_security_review_items",- "list_codex_security_review_items",- "record_codex_security_discovery_candidates",- "list_codex_security_candidates",- "record_codex_security_candidate_validations",- "record_candidate_attack_paths",- "record_codex_security_scan_draft",- "get_codex_security_completed_scan",- ].includes(entry.name),+ for (const tool of tools.filter((entry) =>+ [+ "prepare_codex_security_review_items",+ "list_codex_security_review_items",+ "record_codex_security_discovery_candidates",+ "list_codex_security_candidates",+ "record_codex_security_candidate_validations",+ "record_candidate_attack_paths",+ "record_codex_security_scan_draft",+ "get_codex_security_completed_scan",+ ].includes(entry.name),)) {assert.equal(tool.inputSchema.required?.includes("scanId"),@@ -1253,14 +1185,12 @@ async function testParentToolList(bundle) {deepScanTool,"The parent MCP must expose Deep Scan initialization.",);- assert.equal(- deepScanTool.inputSchema.properties.userContext.minLength,- undefined,- );- assert.equal(- deepScanTool.inputSchema.properties.userContext.maxLength,- undefined,- );+ const contextSchema = deepScanTool.inputSchema.properties!.userContext as {+ minLength?: number;+ maxLength?: number;+ };+ assert.equal(contextSchema.minLength, undefined);+ assert.equal(contextSchema.maxLength, undefined);const sandboxState = readOnlyParentSandboxState(pluginRoot);for (const userContext of ["", " "]) {@@ -1302,7 +1232,7 @@ async function testParentToolList(bundle) {}}-async function testDiscoveryWorkerToolList(bundle) {+async function testDiscoveryWorkerToolList(bundle: string) {const repoRoot = path.join(temporaryRoot, "discovery-repository");const artifactRoot = await mkdtemp(path.join(temporaryRoot, "discovery-output-"),@@ -1337,11 +1267,6 @@ async function testDiscoveryWorkerToolList(bundle) {);const [tool] = tools;- const projectedName = `mcp__cs_artifacts__${tool.name}`;- assert.ok(- projectedName.length <= 64,- `Nested worker MCP tool ${projectedName} exceeds Codex's 64-character limit.`,- );assert.deepEqual(tool.inputSchema.required, ["scanId","findings",@@ -1404,11 +1329,11 @@ async function testDiscoveryWorkerToolList(bundle) {scanId,findingCount: 0,surfaceCount: 0,- coverage: JSON.parse(await readFile(resultPath, "utf8")).coverage,+ coverage: (await readJson(resultPath)).coverage,operation: "replace",status: "draft_written",});- assert.deepEqual(JSON.parse(await readFile(resultPath, "utf8")), input);+ assert.deepEqual(await readJson(resultPath), input);for (const canonicalName of ["scan-manifest.json","findings.json",@@ -1423,7 +1348,7 @@ async function testDiscoveryWorkerToolList(bundle) {}}-async function testReducerWorkerToolList(bundle) {+async function testReducerWorkerToolList(bundle: string) {const repoRoot = path.join(temporaryRoot, "reducer-repository");const scanRoot = await realpath(await mkdtemp(path.join(temporaryRoot, "reducer-scan-")),@@ -1454,15 +1379,13 @@ async function testReducerWorkerToolList(bundle) {"dedup-0002","output",);- await Promise.all([- mkdir(repoRoot, { recursive: true }),- mkdir(workerRoot, { recursive: true }),- mkdir(previousRoot, { recursive: true }),- mkdir(artifactRoot, { recursive: true }),- ]);- const sourceOriginal = reducerPagingFinding("original-source");+ await mkdir(repoRoot, { recursive: true });+ await mkdir(workerRoot, { recursive: true });+ await mkdir(previousRoot, { recursive: true });+ await mkdir(artifactRoot, { recursive: true });+ const sourceOriginal = finding("original-source", "src/fixture.ts");const workerFinding = {- ...reducerPagingFinding("fresh-source"),+ ...finding("fresh-source", "src/fixture.ts"),summary: 'A large finding with quoted "evidence" and Unicode 🧭. '.repeat(120,),@@ -1473,9 +1396,9 @@ async function testReducerWorkerToolList(bundle) {originalCandidates: [{ summary: "retained source candidate" }],},};- const previousOriginal = reducerPagingFinding("previous-original");+ const previousOriginal = finding("previous-original", "src/fixture.ts");const previousFinding = {- ...reducerPagingFinding("previous-aggregate"),+ ...finding("previous-aggregate", "src/fixture.ts"),provenance: {source: "local_plugin",sourceFindingIds: ["previous-worker:0"],@@ -1486,25 +1409,20 @@ async function testReducerWorkerToolList(bundle) {};const workerResultPath = path.join(workerRoot, "result.json");const previousReducerResultPath = path.join(previousRoot, "result.json");- await Promise.all([- writeFile(- workerResultPath,- JSON.stringify({- scanId,- findings: [workerFinding],- coverage: {- completeness: "complete",- surfaces: [],- explicitExclusions: [],- deferred: [],- },- }),- ),- writeFile(- previousReducerResultPath,- JSON.stringify({ scanId, findings: [previousFinding] }),- ),- ]);+ await writeJson(workerResultPath, {+ scanId,+ findings: [workerFinding],+ coverage: {+ completeness: "complete",+ surfaces: [],+ explicitExclusions: [],+ deferred: [],+ },+ });+ await writeJson(previousReducerResultPath, {+ scanId,+ findings: [previousFinding],+ });const client = await startClient(bundle, {CODEX_SECURITY_ARTIFACT_ROOT: artifactRoot,@@ -1540,11 +1458,6 @@ async function testReducerWorkerToolList(bundle) {]);for (const tool of tools) {- const projectedName = `mcp__cs_artifacts__${tool.name}`;- assert.ok(- projectedName.length <= 64,- `Nested worker MCP tool ${projectedName} exceeds Codex's 64-character limit.`,- );assert.equal(Object.hasOwn(tool.inputSchema.properties ?? {}, "scanId"),tool.name === "record_codex_security_deep_reduction",@@ -1554,14 +1467,14 @@ async function testReducerWorkerToolList(bundle) {assert.deepEqual(tool.inputSchema.required, ["scanId", "findings"]);assert.equal(tool.inputSchema.additionalProperties, false);assert.equal(- Object.hasOwn(tool.inputSchema.properties, "coverage"),+ Object.hasOwn(tool.inputSchema.properties!, "coverage"),false,"The reducer must not be asked to submit coverage.",);} else {assert.deepEqual(tool.inputSchema.required, ["maxBytes"]);assert.equal(tool.inputSchema.additionalProperties, false);- assert.deepEqual(Object.keys(tool.inputSchema.properties).sort(), [+ assert.deepEqual(Object.keys(tool.inputSchema.properties!).sort(), ["cursor","findingRef","maxBytes",@@ -1583,7 +1496,7 @@ async function testReducerWorkerToolList(bundle) {}const maxBytes = 768;- async function readPagedInputs(findingRef) {+ async function readPagedInputs(findingRef?: string) {const fragments = [];const cursors = new Set();let cursor;@@ -1598,13 +1511,13 @@ async function testReducerWorkerToolList(bundle) {});assert.notEqual(response.isError, true, JSON.stringify(response));assert.equal(Object.hasOwn(response, "structuredContent"), false);- assert.equal(response.content.length, 1);- assert.equal(response.content[0].type, "text");+ assert.equal((response.content as TextContent[]).length, 1);+ assert.equal((response.content as TextContent[])[0].type, "text");assert.ok(Buffer.byteLength(JSON.stringify(response), "utf8") <= maxBytes,"The full encoded MCP result must fit the requested byte budget.",);- const page = JSON.parse(response.content[0].text);+ const page = JSON.parse((response.content as TextContent[])[0].text);assert.equal(typeof page.json, "string");assert.ok(page.json.length > 0, "Every page must make progress.");fragments.push(page.json);@@ -1680,25 +1593,55 @@ async function testReducerWorkerToolList(bundle) {}}-function reducerPagingFinding(id) {- return {- ruleId: `cross-site-scripting.${id}`,- identity: { anchor: id },- title: `Unsafe request output ${id}`,- summary: "A request-controlled value reaches an HTML response.",- severity: { level: "high" },- confidence: {- level: "high",- rationale: "The source establishes reachability.",- },- taxonomy: { category: "cross-site-scripting", cwe: ["CWE-79"] },- locations: [{ path: "src/fixture.ts", startLine: 1, endLine: 2 }],- remediation: "Encode request-controlled values before emitting HTML.",- provenance: { source: "local_plugin" },- };+async function startScan(+ call: ReturnType<typeof toolCaller>,+ ownerThread: string,+ selection: Record<string, unknown>,+ labels: {+ workspace: string;+ setup: string;+ scan: string;+ claim: string;+ owner: string;+ },+) {+ const opened = requireSuccessfulTool<WorkspaceResult>(+ await call("open_codex_security_workspace", selection),+ labels.workspace,+ );+ const sessionId = opened.workspace.id;+ requireSuccessfulTool(+ await call("submit_codex_security_setup", { ...selection, sessionId }),+ labels.setup,+ );+ const started = requireSuccessfulTool<WorkspaceResult>(+ await call("start_codex_security_scan", { sessionId }),+ labels.scan,+ );+ const { scanId } = started.workspace.results;+ const handoffClaimToken = randomUUID();+ requireSuccessfulTool(+ await call("claim_codex_security_scan_handoff_delivery", {+ scanId,+ claimToken: handoffClaimToken,+ }),+ labels.claim,+ );+ requireSuccessfulTool(+ await call("attach_codex_security_scan_continuation_thread", {+ scanId,+ claimToken: handoffClaimToken,+ threadId: ownerThread,+ }),+ labels.owner,+ );+ return { ...started.workspace.results, handoffClaimToken };}-async function startClient(bundle, environment) {+async function startClient(+ bundle: string,+ environment: Record<string, string>,+) {const client = new Client({name: "codex-security-compact-artifact-test",version: "1.0.0",@@ -1716,14 +1659,18 @@ async function startClient(bundle, environment) {env: {...process.env,...environment,- },+ } as Record<string, string>,});await client.connect(transport);return client;}-function toolCaller(client, ownerThread) {- return (name, arguments_, threadId = ownerThread) =>+function toolCaller(client: Client, ownerThread: string) {+ return (+ name: string,+ arguments_: Record<string, unknown>,+ threadId: string | null = ownerThread,+ ) =>client.callTool({name,arguments: arguments_,diff --git a/plugins/codex-security/mcp-app/tests/test_deep_reducer_paging_eval.mjs b/plugins/codex-security/mcp-app/tests/test_deep_reducer_paging_eval.tssimilarity index 68%rename from plugins/codex-security/mcp-app/tests/test_deep_reducer_paging_eval.mjsrename to plugins/codex-security/mcp-app/tests/test_deep_reducer_paging_eval.tsindex bf9fb1307..f6dd2e697 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_reducer_paging_eval.mjs+++ b/plugins/codex-security/mcp-app/tests/test_deep_reducer_paging_eval.ts@@ -1,23 +1,21 @@+import { readJson, readJsonLines } from "./support/json.ts";import assert from "node:assert/strict";-import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";+import { mkdtemp, rm, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import path from "node:path";import test from "node:test";import {gradeReducerPagingTrace,runReducerPagingEval,-} from "./support/reducer-paging/deep-reducer-paging.mjs";-import {- createReducerPagingFixture,- gradeReducerPagingResult,-} from "./support/reducer-paging/deep-reducer-paging-fixture.mjs";+} from "./support/reducer-paging/deep-reducer-paging.ts";+import { gradeReducerPagingResult } from "./support/reducer-paging/deep-reducer-paging-fixture.ts";test("a reducer recovers from the real IPC frame limit and records all sources", async (t) => {const root = await mkdtemp(path.join(tmpdir(), "deep-reducer-ipc-eval-"));t.after(() => rm(root, { recursive: true, force: true }));- const report = await runReducerPagingEval({ root });+ const { report, fixture } = await runReducerPagingEval({ root });assert.equal(report.realIpcErrorObserved, true);- assert.ok(report.actualOversizedResponseBytes > report.ipcFrameLimitBytes);+ assert.ok(report.actualOversizedResponseBytes! > report.ipcFrameLimitBytes);assert.ok(report.recoveryBudget < report.firstBudget);assert.ok(report.successfulPages > 1);assert.equal(report.referenceReads, 2);@@ -26,10 +24,7 @@ test("a reducer recovers from the real IPC frame limit and records all sources",assert.equal(report.previousIdentityPreserved, true);assert.equal(report.synthesizedHistoryPreserved, true);- const trace = (await readFile(path.join(root, "tool-trace.jsonl"), "utf8"))- .trim()- .split("\n")- .map((line) => JSON.parse(line));+ const trace = await readJsonLines(path.join(root, "tool-trace.jsonl"));const rootPages = trace.filter((event) =>event.event === "request" &&@@ -49,20 +44,23 @@ test("a reducer recovers from the real IPC frame limit and records all sources",);// Keeping every original is insufficient if distinct issues are collapsed.- const fixture = await createReducerPagingFixture(root);- const result = JSON.parse(await readFile(fixture.resultPath, "utf8"));+ const result: {+ findings: {+ provenance: { sourceFindingIds: string[]; sourceFindings: unknown[] };+ }[];+ } = await readJson(fixture.resultPath);const previous = result.findings.find((finding) =>finding.provenance.sourceFindingIds.includes(fixture.expected.previousSourceId,),);- previous.provenance.sourceFindingIds = result.findings.flatMap(+ previous!.provenance.sourceFindingIds = result.findings.flatMap((finding) => finding.provenance.sourceFindingIds,);- previous.provenance.sourceFindings = result.findings.flatMap(+ previous!.provenance.sourceFindings = result.findings.flatMap((finding) => finding.provenance.sourceFindings,);- result.findings = [previous];+ result.findings = [previous!];await writeFile(fixture.resultPath, JSON.stringify(result));await assert.rejects(gradeReducerPagingResult(fixture),diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.tssimilarity index 78%rename from plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.mjsrename to plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.tsindex 71532a760..9a7ac50c0 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.mjs+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.ts@@ -1,13 +1,10 @@-import {- finding,- scanId,- workerDraft as draft,-} from "./scan-draft-fixture.mjs";-import { temporaryDirectory } from "./support/temporary-directories.mjs";+import { readJson, writeJsonLine as writeResult } from "./support/json.ts";+import { finding, scanId, workerDraft as draft } from "./scan-draft-fixture.ts";+import { temporaryDirectory } from "./support/temporary-directories.ts";import assert from "node:assert/strict";import { mkdir, readFile, rm, symlink, writeFile } from "node:fs/promises";import path from "node:path";-import { importSource } from "./import-module.mjs";+import { importSource } from "./import-module.ts";const { validateDiscoveryArtifacts, validateReducerArtifacts } =await importSource(@@ -27,14 +24,12 @@ try {console.log("deep scan artifact validation tests passed");-async function testDiscoveryValidation(root) {- const artifacts = await createLayout(path.join(root, "discovery"));+async function testDiscoveryValidation(root: string) {const result = draft([finding("shared", "src/a.js")], {threatModel: { summary: "Requests reach shared code." },});- const worker = await createWorker(- artifacts,- "discovery-0001",+ const { artifacts, ...worker } = await createWorker(+ path.join(root, "discovery"),"worker-001",result,);@@ -198,42 +193,29 @@ async function testDiscoveryValidation(root) {}}-async function testReducerValidation(root) {- const artifacts = await createLayout(path.join(root, "reducer"));+async function testReducerValidation(root: string) {const firstFinding = finding("shared", "src/a.js");const secondFinding = finding("independent", "src/b.js");- const first = await createWorker(- artifacts,- "discovery-0001",+ const { artifacts, ...first } = await createWorker(+ path.join(root, "reducer"),"worker-001",draft([firstFinding]),);- await createWorker(- artifacts,- "discovery-0002",- "worker-002",- draft([firstFinding, secondFinding]),- );const artifactDir = path.join(artifacts.dedupRoot, "dedup-0001", "output");const resultPath = path.join(artifactDir, "result.json");await mkdir(artifactDir, { recursive: true });await writeResult(resultPath, draft([firstFinding]));- const sources = {- discoveries: [- { workerId: first.id, result: draft([firstFinding, secondFinding]) },- ],- previous: null,- };- const validateSnapshot = () =>+ const sources: import("../src/deep-scan/artifact-validation.js").DeepReductionSources =+ {+ discoveries: [+ { workerId: first.id, result: draft([firstFinding, secondFinding]) },+ ],+ previous: null,+ };+ const validateSnapshot = (reducerId = "dedup-0001", snapshot = sources) =>validateReducerArtifacts(- {- artifacts,- artifactDir,- resultPath,- reducerId: "dedup-0001",- sources,- },+ { artifacts, artifactDir, resultPath, reducerId, sources: snapshot },scanId,);await assert.rejects(validateSnapshot(), /unaccounted source findings/);@@ -241,7 +223,7 @@ async function testReducerValidation(root) {await writeFile(first.resultPath, "{source changed after dispatch");const validatedSnapshot = await validateSnapshot();assert.equal(validatedSnapshot.newFindings, 2);- const admitted = JSON.parse(await readFile(resultPath, "utf8"));+ const admitted = await readJson(resultPath);assert.deepEqual(validatedSnapshot.result,admitted,@@ -252,14 +234,14 @@ async function testReducerValidation(root) {"worker-001:1",]);sources.previous = structuredClone(admitted);- sources.previous.findings[0].summary =+ sources.previous!.findings[0].summary ="Additional proof established by the previous reducer.";await writeResult(resultPath, draft([firstFinding, secondFinding]));assert.equal((await validateSnapshot()).newFindings, 0);assert.equal(- JSON.parse(await readFile(resultPath, "utf8")).findings[0].provenance- .previousFindings[0].summary,- sources.previous.findings[0].summary,+ (await readJson(resultPath)).findings[0].provenance.previousFindings[0]+ .summary,+ sources.previous!.findings[0].summary,);const inheritedThreatModel = {@@ -275,52 +257,34 @@ async function testReducerValidation(root) {previous: null,};await writeResult(resultPath, draft([firstFinding]));- await validateReducerArtifacts(- {- artifacts,- artifactDir,- resultPath,- reducerId: "dedup-threat-model",- sources: threatModelSources,- },- scanId,- );+ await validateSnapshot("dedup-threat-model", threatModelSources);assert.deepEqual(- JSON.parse(await readFile(resultPath, "utf8")).threatModel,+ (await readJson(resultPath)).threatModel,inheritedThreatModel,"a reducer cannot erase the accepted discovery threat model by omission",);await writeResult(resultPath, draft([]));await assert.rejects(- validateReducerArtifacts(- {- artifacts,- artifactDir,- resultPath,- reducerId: "dedup-ambiguous-threat-model",- sources: {- discoveries: [- {- workerId: "worker-a",- result: draft([], {- threatModel: { summary: "Public API." },- scope: { summary: "Public API" },- }),- },- {- workerId: "worker-b",- result: draft([], {- threatModel: { summary: "Local operator." },- scope: { summary: "Local operator" },- }),- },- ],- previous: null,+ validateSnapshot("dedup-ambiguous-threat-model", {+ discoveries: [+ {+ workerId: "worker-a",+ result: draft([], {+ threatModel: { summary: "Public API." },+ scope: { summary: "Public API" },+ }),},- },- scanId,- ),+ {+ workerId: "worker-b",+ result: draft([], {+ threatModel: { summary: "Local operator." },+ scope: { summary: "Local operator" },+ }),+ },+ ],+ previous: null,+ }),/ambiguous threat models/i,);@@ -329,26 +293,17 @@ async function testReducerValidation(root) {includePaths: ["src"],};await writeResult(resultPath, draft([firstFinding]));- await validateReducerArtifacts(- {- artifacts,- artifactDir,- resultPath,- reducerId: "dedup-scope",- sources: {- discoveries: [- {- workerId: first.id,- result: draft([firstFinding], { scope: inheritedScope }),- },- ],- previous: null,+ await validateSnapshot("dedup-scope", {+ discoveries: [+ {+ workerId: first.id,+ result: draft([firstFinding], { scope: inheritedScope }),},- },- scanId,- );+ ],+ previous: null,+ });assert.deepEqual(- JSON.parse(await readFile(resultPath, "utf8")).scope,+ (await readJson(resultPath)).scope,inheritedScope,"a reducer cannot erase an unambiguous accepted scope by omission",);@@ -362,28 +317,19 @@ async function testReducerValidation(root) {await writeResult(resultPath, draft([]));await assert.rejects(- validateReducerArtifacts(- {- artifacts,- artifactDir,- resultPath,- reducerId: "dedup-ambiguous-scope",- sources: {- discoveries: [- {- workerId: "worker-a",- result: draft([], { scope: { summary: "Public API" } }),- },- {- workerId: "worker-b",- result: draft([], { scope: { summary: "Admin API" } }),- },- ],- previous: null,+ validateSnapshot("dedup-ambiguous-scope", {+ discoveries: [+ {+ workerId: "worker-a",+ result: draft([], { scope: { summary: "Public API" } }),},- },- scanId,- ),+ {+ workerId: "worker-b",+ result: draft([], { scope: { summary: "Admin API" } }),+ },+ ],+ previous: null,+ }),/ambiguous scopes/i,);@@ -429,33 +375,26 @@ async function testReducerValidation(root) {},]),);- await validateReducerArtifacts(- {- artifacts,- artifactDir,- resultPath,- reducerId: "dedup-colliding-previous",- sources: collidingSources,- },- scanId,- );- const reconciledCollisions = JSON.parse(- await readFile(resultPath, "utf8"),- ).findings;+ await validateSnapshot("dedup-colliding-previous", collidingSources);+ const reconciledCollisions = (await readJson(resultPath)).findings;assert.deepEqual(- reconciledCollisions.map((item) => item.provenance.sourceFindingIds),+ reconciledCollisions.map(+ (item: { provenance: { sourceFindingIds: string[] } }) =>+ item.provenance.sourceFindingIds,+ ),[["origin:a"], ["origin:b"]],);assert.deepEqual(reconciledCollisions.map(- (item) => item.provenance.previousFindings[0].summary,+ (item: { provenance: { previousFindings: { summary: string }[] } }) =>+ item.provenance.previousFindings[0].summary,),[previousCollisionA.summary, previousCollisionB.summary],);await writeResult(first.resultPath, draft([firstFinding]));await writeResult(resultPath, draft([firstFinding]));- const validate = (previousReducerResultPath) =>+ const validate = (previousReducerResultPath?: string) =>validateReducerArtifacts({artifacts,@@ -495,7 +434,7 @@ async function testReducerValidation(root) {{ ...legacyPartial.coverage, completeness: "complete" },],["malformed", null],- ]) {+ ] as const) {const legacyReducer = {...legacyPartial,coverage: legacyCoverage,@@ -563,7 +502,8 @@ async function testReducerValidation(root) {await writeResult(resultPath, draft([secondFinding]));await assert.rejects(validate(previousReducerResultPath),- (error) => error.code === "merge_traceability_unstable_candidate_id",+ (error: NodeJS.ErrnoException) =>+ error.code === "merge_traceability_unstable_candidate_id",);const replacement = finding("replacement", "src/c.js");@@ -574,7 +514,7 @@ async function testReducerValidation(root) {assert.equal((await validate(previousReducerResultPath)).newFindings, 1);const implicitFirst = { ...firstFinding };- delete implicitFirst.identity;+ delete (implicitFirst as { identity?: unknown }).identity;const implicitRenamed = {...implicitFirst,summary: "More complete evidence.",@@ -606,7 +546,7 @@ async function testReducerValidation(root) {});await assert.rejects(validate(),- (error) =>+ (error: NodeJS.ErrnoException) =>error.name !== "DeepScanNonRetryableError" &&/different scan/.test(error.message),);@@ -644,11 +584,9 @@ async function testReducerValidation(root) {}}-async function testEmptyDiscoveryAndReduction(root) {- const artifacts = await createLayout(path.join(root, "empty"));- const worker = await createWorker(- artifacts,- "discovery-0001",+async function testEmptyDiscoveryAndReduction(root: string) {+ const { artifacts, ...worker } = await createWorker(+ path.join(root, "empty"),"worker-empty",draft([]),);@@ -671,33 +609,15 @@ async function testEmptyDiscoveryAndReduction(root) {);}-async function createLayout(scanDir) {- const workersRoot = path.join(- scanDir,- "artifacts",- "deep_discovery",- "workers",- );- const dedupRoot = path.join(scanDir, "artifacts", "deep_discovery", "dedup");- await Promise.all([- mkdir(workersRoot, { recursive: true }),- mkdir(dedupRoot, { recursive: true }),- ]);- return {+async function createWorker(scanDir: string, id: string, result: unknown) {+ const artifacts = {scanDir,- workersRoot,- dedupRoot,+ workersRoot: path.join(scanDir, "artifacts", "deep_discovery", "workers"),+ dedupRoot: path.join(scanDir, "artifacts", "deep_discovery", "dedup"),};-}--async function createWorker(artifacts, label, id, result) {- const output = path.join(artifacts.workersRoot, label, "output");+ const output = path.join(artifacts.workersRoot, "discovery-0001", "output");await mkdir(output, { recursive: true });const resultPath = path.join(output, "result.json");await writeResult(resultPath, result);- return { id, resultPath };-}--async function writeResult(file, value) {- await writeFile(file, JSON.stringify(value) + "\n");+ return { artifacts, id, resultPath };}diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.tssimilarity index 60%rename from plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.mjsrename to plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.tsindex 20e276d31..3482d6c95 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.mjs+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts@@ -1,25 +1,24 @@-import { sourceReferences } from "./support/source-references.mjs";-import { createTemporaryDirectories } from "./support/temporary-directories.mjs";-import { once } from "node:events";+import { readJson, writeJson } from "./support/json.ts";+import type { CoordinatorOptions } from "../src/deep-scan/coordinator.js";+import type { ScanDraftInput } from "../src/artifact-scan-draft.js";+import type {+ DeepScanRunState,+ PersistedDeepScanWorker,+ DeepScanLogEvent,+} from "../src/deep-scan/types.js";import { mock } from "node:test";import assert from "node:assert/strict";-import { createHash, randomUUID } from "node:crypto";+import { randomUUID } from "node:crypto";import {mkdir,readFile,readdir,realpath,- rename,rm,writeFile,} from "node:fs/promises";import path from "node:path";-import { importModule } from "./import-module.mjs";-import { testDeepScanDeadlines } from "./deep_scan_deadline_cases.mjs";-import { testDeepScanPublication } from "./deep_scan_publication_cases.mjs";-import { createDeepScanWorkerFailureCases } from "./deep_scan_worker_failure_cases.mjs";--const {+import {DeepScanCoordinator,DeepScanCoordinatorRegistry,DeepScanNonRetryableError,@@ -27,57 +26,45 @@ const {AsyncLock,classifyCodexWorkerError,startOrJoinDeepScanCoordinator,-} = await importModule({- stdin: {- contents: `- export * from "./registry.ts";- export { classifyCodexWorkerError } from "./errors.ts";- `,- resolveDir: new URL("../src/deep-scan/", import.meta.url).pathname,- },- loader: { ".md": "text" },-});-const claimDedupInputs = (claim) =>- claim.workerIds.map((discoveryWorkerId, inputOrder) => ({- dedupWorkerId: claim.id,- discoveryWorkerId,- inputOrder,- }));--const recordSleeps = (sleeps) => async (delayMs, signal) => {- assert.equal(signal.aborted, false);- sleeps.push(delayMs);-};--function recordingClock(sleeps) {- return {- now: immediateClock.now,- sleep: async (delayMs) => sleeps.push(delayMs),- };-}+ claimDedupInputs,+ recordSleeps,+ temporaryDirectories,+ fixtureRun,+ coordinatorFixture,+ FakeStore,+ FakeExecutor,+ workerIdFromPrompt,+ promptContext,+ standardScanDraft,+ eventually,+ assertNoPublishedCandidates,+ immediateClock,+ createCoordinator,+ runCoordinator,+ recordingClock,+ type TestWorker,+ type StoreInput,+} from "./deep_scan_coordinator_fixture.ts";-const temporaryDirectories = createTemporaryDirectories(true);async function testCappedQueueAndSerialDedup() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 3,subagents: 2,stopAfterNoNew: 10,maxDiscoveryRuns: 5,});- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({ dedupNewFindings: [1, 0] });- const completedDrafts = [];- const coordinator = createCoordinator(fixture, store, executor, {+ const executor = new FakeExecutor({ discoveryCandidateId: "candidate-1" });+ const completedDrafts: ScanDraftInput[] = [];+ const terminal = await runCoordinator(fixture, store, executor, {random: () => 0,retryDelaysMs: [1, 3, 9],handoffClaimToken: "claim-fixture",- onComplete: async (draft) => completedDrafts.push(structuredClone(draft)),+ onComplete: async (draft) =>+ void completedDrafts.push(structuredClone(draft)),});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded");assert.equal(terminal?.terminalReason, "capped");- assert.equal(executor.logicalDiscoveryWorkers.size, 5);+ assert.equal(executor.discoveryAttempts.size, 5);assert.equal(executor.maximumDiscoveryConcurrency <= 3, true);assert.equal(executor.maximumDedupConcurrency, 1);assert.equal(executor.discoveryWorkingDirectories.size, 5);@@ -96,16 +83,19 @@ async function testCappedQueueAndSerialDedup() {assert.deepEqual(store.progress, [{scanId: fixture.run.scanId,- phase: "discovery",+ phase: "discovery" as const,handoffClaimToken: "claim-fixture",},]);- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));+ const manifest = await readJson(terminal.manifestPath);assert.equal(manifest.scan.scanId, fixture.run.scanId);assert.equal(manifest.scan.mode, "deep");assert.deepEqual(- manifest.findings.map((finding) => finding.provenance.candidateId),+ manifest.findings.map(+ (finding: Record<string, unknown>) =>+ (finding.provenance as { candidateId: string }).candidateId,+ ),["candidate-1"],);assert.equal(completedDrafts.length, 1);@@ -114,33 +104,33 @@ async function testCappedQueueAndSerialDedup() {const reducerWorkers = [...store.workers.values()].filter((worker) => worker.kind === "dedup" && worker.status === "succeeded",);- const finalReducerResult = JSON.parse(- await readFile(reducerWorkers.at(-1).resultManifestPath, "utf8"),+ const finalReducerResult = await readJson(+ reducerWorkers.at(-1)!.resultManifestPath!,);assert.equal(finalReducerResult.scanId, fixture.run.scanId);assert.deepEqual(finalReducerResult.findings, manifest.findings);const finalReducerContext = await promptContext(- executor.dedupPromptPaths.at(-1),+ executor.dedupPromptPaths.at(-1)!,);const finalReducerArtifacts = executor.dedupArtifactContexts.at(-1);assert.deepEqual(finalReducerContext.claimedWorkerIds,- finalReducerArtifacts.deepReducer.claimedWorkers.map((worker) => worker.id),+ finalReducerArtifacts!.deepReducer!.claimedWorkers.map(+ (worker) => worker.id,+ ),);assert.equal(Object.hasOwn(finalReducerContext, "previousReducerResultPath"),false,);assert.equal(- finalReducerArtifacts.deepReducer.previousReducerResultPath,- reducerWorkers.at(-2).resultManifestPath,+ finalReducerArtifacts!.deepReducer!.previousReducerResultPath,+ reducerWorkers.at(-2)!.resultManifestPath,);for (const worker of [...store.workers.values()].filter((worker) => worker.kind === "discovery" && worker.status === "succeeded",)) {- const result = JSON.parse(- await readFile(worker.resultManifestPath, "utf8"),- );+ const result = await readJson(worker.resultManifestPath!);const context = await promptContext(worker.promptPath);assert.equal(result.scanId, fixture.run.scanId);assert.match(result.threatModel.summary, new RegExp(context.workerLabel));@@ -165,12 +155,7 @@ async function testCappedQueueAndSerialDedup() {}async function testStandardWorkersReceiveExistingFalsePositiveFeedback() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });+ const { fixture, store } = await coordinatorFixture();const feedbackPath = path.join(fixture.run.scanDir,"artifacts",@@ -178,15 +163,8 @@ async function testStandardWorkersReceiveExistingFalsePositiveFeedback() {"false_positive_feedback.json",);await mkdir(path.dirname(feedbackPath), { recursive: true });- await writeFile(- feedbackPath,- JSON.stringify([{ reason: "existing control still applies" }]),- );- const store = new FakeStore(fixture.run);- const coordinator = createCoordinator(fixture, store, new FakeExecutor(), {});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);+ await writeJson(feedbackPath, [{ reason: "existing control still applies" }]);+ const terminal = await runCoordinator(fixture, store, new FakeExecutor());assert.equal(terminal?.status, "succeeded", terminal?.error);const worker = [...store.workers.values()].find((candidate) => candidate.kind === "discovery",@@ -216,19 +194,17 @@ async function testStandardWorkersReceiveExistingFalsePositiveFeedback() {async function testDiscoveryWorkersKeepOneContextAfterPersistedUpdate() {const fixture = await fixtureRun({- workers: 1,- subagents: 0,stopAfterNoNew: 10,maxDiscoveryRuns: 2,});fixture.run.userContext = "Initial context.";- const firstWorkerGate = Promise.withResolvers();+ const firstWorkerGate = Promise.withResolvers<void>();const store = new FakeStore(fixture.run);const executor = new FakeExecutor({- dedupNewFindings: [1],+ discoveryCandidateId: "candidate-1",discoveryGates: { "discovery-0001": firstWorkerGate.promise },});- const coordinator = createCoordinator(fixture, store, executor, {});+ const coordinator = createCoordinator(fixture, store, executor);coordinator.start();await executor.discoveryStarted.promise;store.run.userContext = "Updated context.";@@ -249,16 +225,14 @@ async function testDiscoveryWorkersKeepOneContextAfterPersistedUpdate() {async function testPersistedContextDoesNotChangeAnotherProcessDiscoverySnapshot() {const fixture = await fixtureRun({- workers: 1,- subagents: 0,stopAfterNoNew: 10,maxDiscoveryRuns: 2,});fixture.run.userContext = "Initial cross-process context.";- const firstWorkerGate = Promise.withResolvers();+ const firstWorkerGate = Promise.withResolvers<void>();const store = new FakeStore(fixture.run);const executor = new FakeExecutor({- dedupNewFindings: [1],+ discoveryCandidateId: "candidate-1",discoveryGates: { "discovery-0001": firstWorkerGate.promise },});const owningRegistry = new DeepScanCoordinatorRegistry();@@ -295,41 +269,36 @@ async function testPersistedContextDoesNotChangeAnotherProcessDiscoverySnapshot(}async function testWorkerScopedCandidateSourceAggregation() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,- subagents: 0,stopAfterNoNew: 10,maxDiscoveryRuns: 2,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({discoveryCandidateId: "candidate-1",canonicalCandidateId: "candidate-1",- dedupNewFindings: [1],});- const coordinator = createCoordinator(fixture, store, executor, {});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);+ const terminal = await runCoordinator(fixture, store, executor);assert.equal(terminal?.terminalReason, "capped");- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));+ const manifest = await readJson(terminal.manifestPath);const expectedWorkerIds = [...store.workers.values()].filter((worker) => worker.kind === "discovery" && worker.status === "succeeded",)- .sort((left, right) => left.completionSequence - right.completionSequence)+ .sort((left, right) => left.completionSequence! - right.completionSequence!).map((worker) => worker.id);assert.deepEqual(- manifest.findings.map((finding) => finding.provenance.candidateId),+ manifest.findings.map(+ (finding: Record<string, unknown>) =>+ (finding.provenance as { candidateId: string }).candidateId,+ ),["candidate-1"],);assert.deepEqual(store.dedupClaims[0].workerIds, expectedWorkerIds);const reducer = [...store.workers.values()].find((worker) => worker.kind === "dedup",);- const reducerResult = JSON.parse(- await readFile(reducer.resultManifestPath, "utf8"),- );+ const reducerResult = await readJson(reducer!.resultManifestPath!);assert.equal(reducerResult.scanId, fixture.run.scanId);assert.deepEqual(reducerResult.findings, manifest.findings);assert.equal(@@ -340,91 +309,74 @@ async function testWorkerScopedCandidateSourceAggregation() {}async function testConsumedSourceIsNotRereadAfterReducerWritesResult() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,- subagents: 0,stopAfterNoNew: 2,maxDiscoveryRuns: 2,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({discoveryCandidateId: "candidate-1",canonicalCandidateId: "candidate-1",corruptAcceptedSource: true,});- const coordinator = createCoordinator(fixture, store, executor, {});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);+ const terminal = await runCoordinator(fixture, store, executor);assert.equal(terminal?.status, "succeeded", terminal?.error);assert.equal(terminal?.terminalReason, "capped");assert.equal(executor.dedupCalls, 1);assert.deepEqual(- JSON.parse(await readFile(terminal.manifestPath, "utf8")).findings.map(- (finding) => finding.provenance.candidateId,+ (await readJson(terminal.manifestPath)).findings.map(+ (finding: Record<string, unknown>) =>+ (finding.provenance as { candidateId: string }).candidateId,),["candidate-1"],);}async function testConsumedSourceWithToolDiagnosticIsNotRereadAfterReducerWritesResult() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,- subagents: 0,stopAfterNoNew: 2,maxDiscoveryRuns: 2,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({- dedupNewFindings: [0],corruptAcceptedSource: true,dedupDiagnostics: [{- code: "artifact_tool_failed",+ code: "artifact_tool_failed" as const,message:"Codex worker artifact tool record_codex_security_deep_reduction failed.",},],});- const coordinator = createCoordinator(fixture, store, executor, {});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);+ const terminal = await runCoordinator(fixture, store, executor);assert.equal(terminal?.status, "succeeded", terminal?.error);assert.equal(terminal?.terminalReason, "saturated");assert.equal(executor.dedupCalls, 1);- assert.deepEqual(- JSON.parse(await readFile(terminal.manifestPath, "utf8")).findings,- [],- );+ assert.deepEqual((await readJson(terminal.manifestPath)).findings, []);}async function testRetryKeepsLogicalWorker() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,subagents: 1,stopAfterNoNew: 2,maxDiscoveryRuns: 2,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({failFirstDiscoveryAttempt: true,- dedupNewFindings: [0],});- const sleeps = [];- const coordinator = createCoordinator(fixture, store, executor, {+ const sleeps: number[] = [];+ const terminal = await runCoordinator(fixture, store, executor, {random: () => 0.5,clock: {now: () => 1_700_000_000_000,sleep: recordSleeps(sleeps),},});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.terminalReason, "saturated", terminal?.error);assert.deepEqual(sleeps, [69_000]);- assert.equal(executor.logicalDiscoveryWorkers.size, 2);+ assert.equal(executor.discoveryAttempts.size, 2);assert.equal(executor.discoveryCalls, 3);const attemptsByWorker = new Map();for (const update of store.workerUpdates) {@@ -446,7 +398,7 @@ async function testRetryKeepsLogicalWorker() {const retriedPromptPaths = executor.discoveryPromptPaths.get([...executor.discoveryAttempts.entries()].find(([, attempts]) => attempts === 2,- )?.[0],+ )?.[0]!,);assert.equal(retriedPromptPaths?.size,@@ -461,15 +413,12 @@ async function testRetryKeepsLogicalWorker() {}async function testSandboxDiagnosticSurvivesArtifactRetries() {- const fixture = await fixtureRun({- workers: 1,+ const { fixture, store } = await coordinatorFixture({subagents: 1,stopAfterNoNew: 2,- maxDiscoveryRuns: 1,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({- alwaysOmitDiscoveryArtifact: "result.json",+ invalidDiscoveryAttempts: Infinity,discoveryDiagnostics: [{code: "sandbox_namespace_exhausted",@@ -478,20 +427,17 @@ async function testSandboxDiagnosticSurvivesArtifactRetries() {},],});- const sleeps = [];- const coordinator = createCoordinator(fixture, store, executor, {+ const sleeps: number[] = [];+ const terminal = await runCoordinator(fixture, store, executor, {retryDelaysMs: [1, 3, 9],random: () => 0,clock: {now: () => 1_700_000_000_000,- sleep: async (delayMs) => sleeps.push(delayMs),+ sleep: async (delayMs) => void sleeps.push(delayMs),},});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "failed");assert.deepEqual(sleeps, [1, 3, 9]);- assert.equal(executor.discoveryCalls, 4);assert.deepEqual(executor.discoveryResumeThreadIds, [undefined,undefined,@@ -507,71 +453,66 @@ async function testSandboxDiagnosticSurvivesArtifactRetries() {terminal?.error ?? "",/super-secret-command|private source text/,);- await assertFailureManifest(terminal, "discovery");+ assert.equal(terminal.manifestPath, undefined);}async function testCompletionOrdering() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,subagents: 1,stopAfterNoNew: 10,maxDiscoveryRuns: 3,});- const store = new FakeStore(fixture.run);- const firstWorkerGate = Promise.withResolvers();+ const firstWorkerGate = Promise.withResolvers<void>();const executor = new FakeExecutor({- dedupNewFindings: [1, 0],+ discoveryCandidateId: "candidate-1",discoveryGates: { "discovery-0001": firstWorkerGate.promise },});- const coordinator = createCoordinator(fixture, store, executor, {});+ const coordinator = createCoordinator(fixture, store, executor);coordinator.start();await eventually(() => store.dedupClaims.length === 1);firstWorkerGate.resolve();const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.terminalReason, "capped");- const workerLabel = (workerId) =>- path.basename(path.dirname(store.workers.get(workerId).promptPath));+ const workerLabel = (workerId: string) =>+ path.basename(path.dirname(store.workers.get(workerId)!.promptPath));assert.deepEqual(store.dedupClaims[0].workerIds.map(workerLabel), ["discovery-0002","discovery-0003",]);assert.equal(- store.workers.get(store.dedupClaims[0].workerIds[0]).completionSequence,+ store.workers.get(store.dedupClaims[0].workerIds[0])!.completionSequence,1,);assert.equal(- store.workers.get(store.dedupClaims[0].workerIds[1]).completionSequence,+ store.workers.get(store.dedupClaims[0].workerIds[1])!.completionSequence,2,);}async function testSaturationDrainsBufferedAndCancelsInflight() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 4,subagents: 1,stopAfterNoNew: 2,maxDiscoveryRuns: 6,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({- blockDedup: true,blockDiscoveryAfterCalls: 4,- dedupNewFindings: [0],});- const coordinator = createCoordinator(fixture, store, executor, {});+ executor.dedupGate = Promise.withResolvers<void>();+ const coordinator = createCoordinator(fixture, store, executor);coordinator.start();await executor.dedupStarted.promise;await eventually(() => executor.discoveryCalls === 6 && executor.runningDiscovery === 2,);- executor.releaseDedup();+ executor.dedupGate.resolve();const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.terminalReason, "saturated");- await eventually(- () => executor.runningDiscovery === 0 && executor.runningDedup === 0,- );- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));+ assert.ok(executor.runningDiscovery === 0 && executor.runningDedup === 0);+ const manifest = await readJson(terminal.manifestPath);assert.equal(store.dedupClaims.length,2,@@ -596,16 +537,13 @@ async function testSaturationDrainsBufferedAndCancelsInflight() {}async function testSaturationPreservesFindingAlreadyBuffered() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 4,- subagents: 0,stopAfterNoNew: 2,maxDiscoveryRuns: 4,});- const store = new FakeStore(fixture.run);- const laterDiscoveries = Promise.withResolvers();+ const laterDiscoveries = Promise.withResolvers<void>();const executor = new FakeExecutor({- blockDedup: true,discoveryGates: {"discovery-0003": laterDiscoveries.promise,"discovery-0004": laterDiscoveries.promise,@@ -615,9 +553,10 @@ async function testSaturationPreservesFindingAlreadyBuffered() {"discovery-0004": "buffered-finding",},});- const completed = [];+ executor.dedupGate = Promise.withResolvers<void>();+ const completed: ScanDraftInput[] = [];const coordinator = createCoordinator(fixture, store, executor, {- onComplete: async (draft) => completed.push(draft),+ onComplete: async (draft) => void completed.push(draft),});coordinator.start();await executor.dedupStarted.promise;@@ -629,25 +568,25 @@ async function testSaturationPreservesFindingAlreadyBuffered() {worker.kind === "discovery" && worker.status === "succeeded",).length === 4,);- executor.releaseDedup();+ executor.dedupGate.resolve();const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded", terminal?.error);assert.deepEqual(- completed[0].findings.map((finding) => finding.provenance.candidateId),+ completed[0].findings.map(+ (finding: Record<string, unknown>) =>+ (finding.provenance as { candidateId: string }).candidateId,+ ),["buffered-finding"],);assert.equal(store.finishCalls[0].omittedWorkerIds.length, 0);}async function testDirectReducerCannotDropAcceptedFinding() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,- subagents: 0,- stopAfterNoNew: 1,maxDiscoveryRuns: 2,});- const store = new FakeStore(fixture.run);- const coordinator = createCoordinator(+ const terminal = await runCoordinator(fixture,store,new FakeExecutor({@@ -659,8 +598,6 @@ async function testDirectReducerCannotDropAcceptedFinding() {}),{ retryDelaysMs: [] },);- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status,"failed",@@ -676,34 +613,30 @@ async function testDirectReducerCannotDropAcceptedFinding() {}async function testSaturationIgnoresWorkerFailureSettledAfterStop() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 3,- subagents: 0,stopAfterNoNew: 2,maxDiscoveryRuns: 3,});- const store = new FakeStore(fixture.run);- store.blockDiscoveryFailure = true;+ store.blockDiscoveryUpdate = "failed";const executor = new FakeExecutor({- blockDedup: true,- dedupNewFindings: [0],nonRetryableDiscoveryWorkers: ["discovery-0003"],});- const completedDrafts = [];+ executor.dedupGate = Promise.withResolvers<void>();+ const completedDrafts: ScanDraftInput[] = [];const coordinator = createCoordinator(fixture, store, executor, {retryDelaysMs: [],threadId: "fixture-owning-thread",- onComplete: async (draft) => completedDrafts.push(structuredClone(draft)),+ onComplete: async (draft) =>+ void completedDrafts.push(structuredClone(draft)),});coordinator.start();- await Promise.all([- executor.dedupStarted.promise,- store.discoveryFailureBlocked.promise,- ]);- executor.releaseDedup();+ await executor.dedupStarted.promise;+ await store.discoveryBlocked.promise;+ executor.dedupGate.resolve();await eventually(() => executor.dedupSignal?.aborted === true);- store.releaseDiscoveryFailure();+ store.discoveryGate.resolve();const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded", terminal?.error);@@ -717,27 +650,24 @@ async function testSaturationIgnoresWorkerFailureSettledAfterStop() {path.basename(path.dirname(worker.promptPath)) === "discovery-0003",);assert.ok(failedWorker);- assert.equal(failedWorker.status, "failed");- assert.equal(store.failureInputs.length, 0);+ assert.equal(store.failureMessages.length, 0);assert.equal(store.finishCalls.length, 1);assert.equal(executor.discoveryCalls, 3);assert.equal(executor.dedupCalls, 1);}async function testSettledReducerIsNotStarvedByDiscoveryBacklog() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 4,subagents: 1,stopAfterNoNew: 2,maxDiscoveryRuns: 20,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({- blockDedup: true,blockDiscoveryAfterCalls: 4,- dedupNewFindings: [0],});- const coordinator = createCoordinator(fixture, store, executor, {});+ executor.dedupGate = Promise.withResolvers<void>();+ const coordinator = createCoordinator(fixture, store, executor);coordinator.start();await executor.dedupStarted.promise;@@ -745,7 +675,7 @@ async function testSettledReducerIsNotStarvedByDiscoveryBacklog() {() => executor.discoveryCalls >= 8 && executor.runningDiscovery === 4,);const dispatchedBeforeConvergence = executor.discoveryCalls;- executor.releaseDedup();+ executor.dedupGate.resolve();const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.terminalReason, "saturated");@@ -754,92 +684,47 @@ async function testSettledReducerIsNotStarvedByDiscoveryBacklog() {dispatchedBeforeConvergence,"a settled reducer must stop dispatch before canceled workers can refill the pool",);- assert.equal(- executor.logicalDiscoveryWorkers.size,- dispatchedBeforeConvergence,- );+ assert.equal(executor.discoveryAttempts.size, dispatchedBeforeConvergence);}async function testSingletonHardCapReduction() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 4,- subagents: 0,stopAfterNoNew: 6,- maxDiscoveryRuns: 1,});- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({ dedupNewFindings: [1] });- const coordinator = createCoordinator(fixture, store, executor, {});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);+ const executor = new FakeExecutor({ discoveryCandidateId: "candidate-1" });+ const terminal = await runCoordinator(fixture, store, executor);assert.equal(terminal?.terminalReason, "capped");- assert.equal(executor.logicalDiscoveryWorkers.size, 1);+ assert.equal(executor.discoveryAttempts.size, 1);assert.deepEqual(store.dedupClaims.map((claim) => claim.workerIds.length),[1],);- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));+ const manifest = await readJson(terminal.manifestPath);assert.equal(manifest.scan.scanId, fixture.run.scanId);assert.equal(store.dedupCommits.length, 1);}async function testExhaustedRetryFailsScan() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,subagents: 1,stopAfterNoNew: 2,maxDiscoveryRuns: 2,});- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({ alwaysFailDiscovery: true });- const coordinator = createCoordinator(fixture, store, executor, {+ const executor = new FakeExecutor({+ discoveryFailureMessage: "transient worker failure",+ });+ const terminal = await runCoordinator(fixture, store, executor, {random: () => 0,retryDelaysMs: [1, 3, 9],});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "failed");assert.match(terminal?.error ?? "", /transient worker failure/);assert.equal(executor.discoveryCalls >= 4, true);assert.equal(executor.discoveryCalls <= 8, true);assert.equal(executor.runningDiscovery, 0);- await assertFailureManifest(terminal, "discovery");-}--async function testCybersecurityRefusalReplacesOnlyRefusedDiscovery() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 4,- stopAfterConsecutiveErrors: 3,- maxDiscoveryRuns: 3,- });- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({- policyRefusalWorkers: ["discovery-0001"],- dedupNewFindings: [1],- });- const sleeps = [];- const coordinator = createCoordinator(fixture, store, executor, {- retryDelaysMs: [1, 3, 9],- clock: recordingClock(sleeps),- });- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);-- assert.equal(terminal?.status, "succeeded");- assert.equal(terminal?.terminalReason, "capped");- assert.equal(executor.logicalDiscoveryWorkers.size, 3);- assert.equal(executor.discoveryAttempts.get("discovery-0001"), 1);- assert.deepEqual(sleeps, [], "a refused conversation must never be resumed");- assert.equal(store.run.consecutiveErrors, 0);- const refusal = [...store.workers.values()].find(- (worker) =>- path.basename(path.dirname(worker.promptPath)) === "discovery-0001",- );- assert.equal(refusal?.replaceableFailureKind, "policy_refusal");- assert.equal(refusal?.status, "canceled");+ assert.equal(terminal.manifestPath, undefined);}async function testProviderCybersecurityRiskMessagesReplaceRefusedDiscoveryImmediately() {@@ -849,30 +734,25 @@ async function testProviderCybersecurityRiskMessagesReplaceRefusedDiscoveryImmed"This content was flagged for possible cybersecurity risk.","This content was flagged for potentially high-risk cyber activity.",]) {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,+ const { fixture, store } = await coordinatorFixture({stopAfterNoNew: 4,stopAfterConsecutiveErrors: 3,maxDiscoveryRuns: 3,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({- policyRefusalWorkers: ["discovery-0001"],- policyRefusalMessages: { "discovery-0001": message },- dedupNewFindings: [1],+ discoveryFailureMessages: { "discovery-0001": message },+ discoveryCandidateId: "candidate-1",});- const sleeps = [];- const coordinator = createCoordinator(fixture, store, executor, {+ const sleeps: number[] = [];+ const terminal = await runCoordinator(fixture, store, executor, {random: () => 0,retryDelaysMs: [1, 3, 9],clock: recordingClock(sleeps),});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded", message);+ assert.equal(terminal?.terminalReason, "capped");+ assert.equal(executor.discoveryAttempts.size, 3);assert.equal(executor.discoveryAttempts.get("discovery-0001"), 1, message);assert.deepEqual(sleeps,@@ -880,18 +760,16 @@ async function testProviderCybersecurityRiskMessagesReplaceRefusedDiscoveryImmed`a refused conversation must not be resumed: ${message}`,);assert.equal(store.run.consecutiveErrors, 0, message);- assert.equal(- [...store.workers.values()].find(- (worker) =>- path.basename(path.dirname(worker.promptPath)) === "discovery-0001",- )?.replaceableFailureKind,- "policy_refusal",- message,+ const refusal = [...store.workers.values()].find(+ (worker) =>+ path.basename(path.dirname(worker.promptPath)) === "discovery-0001",);+ assert.equal(refusal?.replaceableFailureKind, "policy_refusal", message);+ assert.equal(refusal?.status, "canceled");}}-async function testRateLimitAndUnrelatedRefusalsRetainTransientRecovery() {+async function testTransientDiscoveryErrorsRetainRecovery() {for (const message of ["RateLimitExhaustedError: request rate limit reached; " +"This content was flagged for possible cybersecurity risk.",@@ -899,32 +777,27 @@ async function testRateLimitAndUnrelatedRefusalsRetainTransientRecovery() {"429 Too Many Requests: Request blocked by cyberPolicy.","ordinary model refusal [HTTP 400]","generic safety/security error [HTTP 403]",+ "transient worker failure",]) {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,+ const { fixture, store } = await coordinatorFixture({stopAfterNoNew: 4,stopAfterConsecutiveErrors: 2,maxDiscoveryRuns: 3,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({- policyRefusalWorkers: ["discovery-0001"],- policyRefusalMessages: { "discovery-0001": message },- dedupNewFindings: [1],+ discoveryFailureMessages: { "discovery-0001": message },+ discoveryCandidateId: "candidate-1",});- const sleeps = [];- const coordinator = createCoordinator(fixture, store, executor, {+ const sleeps: number[] = [];+ const terminal = await runCoordinator(fixture, store, executor, {random: () => 0,retryDelaysMs: [1, 3, 9],clock: recordingClock(sleeps),});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded", message);assert.equal(executor.discoveryAttempts.get("discovery-0001"), 4, message);+ assert.equal(executor.discoveryAttempts.size, 3);assert.deepEqual(sleeps, [1, 3, 9], message);assert.equal(store.run.consecutiveErrors, 0, message);assert.equal(@@ -939,27 +812,20 @@ async function testRateLimitAndUnrelatedRefusalsRetainTransientRecovery() {}async function testConsecutiveCybersecurityRefusalsFailAtConfiguredThreshold() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,+ const { fixture, store } = await coordinatorFixture({stopAfterNoNew: 6,stopAfterConsecutiveErrors: 2,maxDiscoveryRuns: 10,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({- policyRefusalWorkers: ["discovery-0001", "discovery-0002"],- policyRefusalMessages: {+ discoveryFailureMessages: {"discovery-0001": "Request blocked by cyberPolicy.","discovery-0002": "Request blocked by cyberPolicy.",},});- const coordinator = createCoordinator(fixture, store, executor, {+ const terminal = await runCoordinator(fixture, store, executor, {retryDelaysMs: [1, 3, 9],});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "failed");assert.match(@@ -967,72 +833,36 @@ async function testConsecutiveCybersecurityRefusalsFailAtConfiguredThreshold() {/2 consecutive unsuccessful discovery workers/,);assert.match(terminal?.error ?? "", /policy_refusal/);- assert.equal(executor.logicalDiscoveryWorkers.size, 2);+ assert.equal(executor.discoveryAttempts.size, 2);assert.equal(executor.discoveryCalls, 2);assert.equal(store.run.consecutiveErrors, 2);assert.equal(store.finishCalls.length, 0);- await assertFailureManifest(terminal, "discovery");+ assert.equal(terminal.manifestPath, undefined);assert.deepEqual([...store.workers.values()].map((worker) => worker.replaceableFailureKind),["policy_refusal", "policy_refusal"],);}-async function testExhaustedTransientDiscoveryIsReplaced() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 4,- stopAfterConsecutiveErrors: 2,- maxDiscoveryRuns: 3,- });- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({- transientFailureWorkers: ["discovery-0001"],- dedupNewFindings: [1],- });- const coordinator = createCoordinator(fixture, store, executor, {- retryDelaysMs: [1, 3, 9],- });- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);-- assert.equal(terminal?.status, "succeeded");- assert.equal(executor.discoveryAttempts.get("discovery-0001"), 4);- assert.equal(executor.logicalDiscoveryWorkers.size, 3);- assert.equal(store.run.consecutiveErrors, 0);- assert.equal(- [...store.workers.values()].find(- (worker) =>- path.basename(path.dirname(worker.promptPath)) === "discovery-0001",- )?.replaceableFailureKind,- "transient_error",- );-}-async function testSuccessfulDiscoveryResetsConsecutiveFailureThreshold() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,+ const { fixture, store } = await coordinatorFixture({stopAfterNoNew: 8,stopAfterConsecutiveErrors: 2,maxDiscoveryRuns: 5,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({- policyRefusalWorkers: ["discovery-0001", "discovery-0003"],- dedupNewFindings: [1],+ discoveryFailureMessages: {+ "discovery-0001": "Request blocked by cyberPolicy.",+ "discovery-0003": "Request blocked by cyberPolicy.",+ },+ discoveryCandidateId: "candidate-1",});- const coordinator = createCoordinator(fixture, store, executor, {+ const terminal = await runCoordinator(fixture, store, executor, {retryDelaysMs: [1, 3, 9],});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded");- assert.equal(executor.logicalDiscoveryWorkers.size, 5);+ assert.equal(executor.discoveryAttempts.size, 5);assert.equal(store.run.consecutiveErrors, 0);assert.equal([...store.workers.values()].filter(@@ -1043,28 +873,22 @@ async function testSuccessfulDiscoveryResetsConsecutiveFailureThreshold() {}async function testExhaustedInvalidDiscoveryArtifactsAreReplaced() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,+ const { fixture, store } = await coordinatorFixture({stopAfterNoNew: 4,stopAfterConsecutiveErrors: 2,maxDiscoveryRuns: 3,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({invalidDiscoveryAttempts: 4,- dedupNewFindings: [1],+ discoveryCandidateId: "candidate-1",});- const coordinator = createCoordinator(fixture, store, executor, {+ const terminal = await runCoordinator(fixture, store, executor, {retryDelaysMs: [1, 3, 9],});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded");assert.equal(executor.discoveryAttempts.get("discovery-0001"), 4);- assert.equal(executor.logicalDiscoveryWorkers.size, 3);+ assert.equal(executor.discoveryAttempts.size, 3);assert.equal([...store.workers.values()].find((worker) =>@@ -1075,24 +899,18 @@ async function testExhaustedInvalidDiscoveryArtifactsAreReplaced() {}async function testExhaustedMalformedDiscoveryDoesNotRemainPublishable() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,+ const { fixture, store } = await coordinatorFixture({stopAfterNoNew: 4,stopAfterConsecutiveErrors: 2,maxDiscoveryRuns: 3,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({malformedDiscoveryAttempts: 4,- dedupNewFindings: [1],+ discoveryCandidateId: "candidate-1",});- const coordinator = createCoordinator(fixture, store, executor, {+ const terminal = await runCoordinator(fixture, store, executor, {retryDelaysMs: [1, 3, 9],});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded");assert.equal(executor.discoveryAttempts.get("discovery-0001"), 4);@@ -1114,30 +932,21 @@ async function testExhaustedMalformedDiscoveryDoesNotRemainPublishable() {}async function testTransientExecutionFailureResumesWorkerThread() {- const fixture = await fixtureRun({- workers: 1,+ const { fixture, store } = await coordinatorFixture({subagents: 1,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({failFirstDiscoveryAttempt: true,writePartialBeforeFailure: true,- dedupNewFindings: [0],});- const coordinator = createCoordinator(fixture, store, executor, {+ const terminal = await runCoordinator(fixture, store, executor, {retryDelaysMs: [1],});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded");- assert.equal(executor.discoveryCalls, 2);- assert.equal(executor.discoveryResumeThreadIds[0], undefined);- assert.equal(- executor.discoveryResumeThreadIds[1],+ assert.deepEqual(executor.discoveryResumeThreadIds, [+ undefined,executor.discoveryThreadIds[0],- );+ ]);assert.equal(new Set(executor.discoveryThreadIds).size, 1);assert.match(executor.discoveryContinuationPrompts[1] ?? "",@@ -1165,23 +974,14 @@ async function testTransientExecutionFailureResumesWorkerThread() {async function testConfigurationFailureDoesNotRetry(failureMessage = "fixture configuration failure",) {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);+ const { fixture, store } = await coordinatorFixture();const executor = new FakeExecutor({- nonRetryableDiscovery: true,nonRetryableDiscoveryMessage: failureMessage,});- const sleeps = [];- const coordinator = createCoordinator(fixture, store, executor, {+ const sleeps: number[] = [];+ const terminal = await runCoordinator(fixture, store, executor, {clock: recordingClock(sleeps),});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "failed");assert.equal(terminal?.error, failureMessage);assert.equal(executor.discoveryCalls, 1);@@ -1190,13 +990,7 @@ async function testConfigurationFailureDoesNotRetry(}async function testFailureManifestWriteDoesNotMaskOriginalError() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);+ const { fixture, store } = await coordinatorFixture();const manifestPath = path.join(fixture.run.scanDir,"artifacts",@@ -1204,122 +998,78 @@ async function testFailureManifestWriteDoesNotMaskOriginalError() {"coordinator-manifest.json",);await mkdir(manifestPath, { recursive: true });- const coordinator = createCoordinator(+ const terminal = await runCoordinator(fixture,store,- new FakeExecutor({ nonRetryableDiscovery: true }),- {},+ new FakeExecutor({+ nonRetryableDiscoveryMessage: "fixture configuration failure",+ }),);- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "failed");assert.match(terminal?.error ?? "", /fixture configuration failure/);assert.equal(terminal?.manifestPath, undefined);- assert.equal(store.failureInputs.length, 1);+ assert.equal(store.failureMessages.length, 1);}async function testFinishPersistenceFailureRewritesManifestAsFailure() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);+ const { fixture, store } = await coordinatorFixture();store.failFinish = true;- const coordinator = createCoordinator(- fixture,- store,- new FakeExecutor({ dedupNewFindings: [0] }),- {},- );- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);+ const terminal = await runCoordinator(fixture, store, new FakeExecutor());assert.equal(terminal?.status, "failed");assert.match(terminal?.error ?? "", /fixture finish persistence failure/);- await assertFailureManifest(terminal, "terminal");+ assert.equal(terminal.manifestPath, undefined);}async function testLostFinishResponseReplaysWithoutOverwritingSuccessManifest() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);+ const { fixture, store } = await coordinatorFixture();store.loseFirstFinishResponseAfterCommit = true;- const coordinator = createCoordinator(- fixture,- store,- new FakeExecutor({ dedupNewFindings: [0] }),- {},- );- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);+ const terminal = await runCoordinator(fixture, store, new FakeExecutor());assert.equal(terminal?.status, "succeeded");assert.equal(store.finishCalls.length, 2);assert.deepEqual(store.finishCalls[1], store.finishCalls[0]);- assert.equal(store.failureInputs.length, 0);- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));+ assert.equal(store.failureMessages.length, 0);+ const manifest = await readJson(terminal.manifestPath);assert.equal(manifest.scan.scanId, fixture.run.scanId);}async function testLostWorkerCommitResponsesReplayIdempotently() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);+ const { fixture, store } = await coordinatorFixture();store.loseFirstDiscoveryAcceptanceResponseAfterCommit = true;store.loseFirstDedupCommitResponseAfterCommit = true;- const coordinator = createCoordinator(- fixture,- store,- new FakeExecutor({ dedupNewFindings: [0] }),- {},- );- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);+ const terminal = await runCoordinator(fixture, store, new FakeExecutor());assert.equal(terminal?.status, "succeeded");- assert.equal(store.discoveryAcceptanceResponseLosses, 1);- assert.equal(store.dedupCommitResponseLosses, 1);+ assert.equal(store.loseFirstDiscoveryAcceptanceResponseAfterCommit, false);+ assert.equal(store.loseFirstDedupCommitResponseAfterCommit, false);assert.equal(store.dedupCommitCalls.length, 2);assert.equal(store.dedupCommits.length, 1);- assert.equal(store.failureInputs.length, 0);+ assert.equal(store.failureMessages.length, 0);}async function testCommittedReducerIsReconciledBeforeDiscoveryFailureManifest() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 3,- subagents: 0,stopAfterNoNew: 10,maxDiscoveryRuns: 3,});- const thirdWorkerGate = Promise.withResolvers();- const store = new FakeStore(fixture.run);- store.blockDedupCommitResponse = true;+ const thirdWorkerGate = Promise.withResolvers<void>();+ store.dedupCommitResponseGate = Promise.withResolvers<void>();const executor = new FakeExecutor({- dedupNewFindings: [0],discoveryGates: { "discovery-0003": thirdWorkerGate.promise },failDiscoveryWorkersAfterGate: ["discovery-0003"],});- const completedDrafts = [];+ const completedDrafts: ScanDraftInput[] = [];const coordinator = createCoordinator(fixture, store, executor, {retryDelaysMs: [],threadId: "fixture-owning-thread",- onComplete: async (draft) => completedDrafts.push(structuredClone(draft)),+ onComplete: async (draft) =>+ void completedDrafts.push(structuredClone(draft)),});coordinator.start();- await store.dedupCommitPersisted.promise;+ await store.dedupCommitted.promise;thirdWorkerGate.resolve();await eventually(() => executor.dedupSignal?.aborted === true);- store.releaseDedupCommitResponse();+ store.dedupCommitResponseGate.resolve();const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "failed");@@ -1332,65 +1082,50 @@ async function testCommittedReducerIsReconciledBeforeDiscoveryFailureManifest()}async function testLongWorkerErrorIsBoundedOnlyAtPersistenceBoundary() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);+ const { fixture, store } = await coordinatorFixture();const fullError = `fixture long validator error: ${"x".repeat(5_000)}`;- const coordinator = createCoordinator(+ const terminal = await runCoordinator(fixture,store,- new FakeExecutor({ longDiscoveryFailure: fullError }),+ new FakeExecutor({ discoveryFailureMessage: fullError }),{ retryDelaysMs: [1, 3, 9], random: () => 0 },);- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "failed");- assert.equal(store.failureInputs[0].message.length <= 2_400, true);- assert.match(store.failureInputs[0].message, /truncated; sha256:/);+ assert.equal(store.failureMessages[0].length <= 2_400, true);+ assert.match(store.failureMessages[0], /truncated; sha256:/);assert.equal(terminal.manifestPath, undefined);const [worker] = [...store.workers.values()];assert.equal(worker.status, "canceled");assert.equal(worker.attempt, 4);- assert.match(worker.error, /truncated; sha256:/);+ assert.match(worker.error!, /truncated; sha256:/);}async function testDiscoveryPhasePersistenceFailureStopsDispatch() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,subagents: 1,stopAfterNoNew: 2,maxDiscoveryRuns: 2,});- const store = new FakeStore(fixture.run);store.failProgressAt = 1;const executor = new FakeExecutor();- const coordinator = createCoordinator(fixture, store, executor, {});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);+ const terminal = await runCoordinator(fixture, store, executor);assert.equal(terminal?.status, "failed");assert.match(terminal?.error ?? "", /fixture progress persistence failure/);- assert.equal(executor.logicalDiscoveryWorkers.size, 0);- await assertFailureManifest(terminal, "discovery");+ assert.equal(executor.discoveryAttempts.size, 0);+ assert.equal(terminal.manifestPath, undefined);}async function testCancellationClearsRetryWait() {- const fixture = await fixtureRun({- workers: 1,+ const { fixture, store } = await coordinatorFixture({subagents: 1,stopAfterNoNew: 2,- maxDiscoveryRuns: 1,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({failFirstDiscoveryAttempt: true,blockDiscoveryAfterCalls: 1,});- const sleepStarted = Promise.withResolvers();+ const sleepStarted = Promise.withResolvers<void>();const coordinator = createCoordinator(fixture, store, executor, {clock: undefined,log: (event) => {@@ -1403,7 +1138,7 @@ async function testCancellationClearsRetryWait() {coordinator.cancel("cancel retry wait");const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "canceled");- await eventually(() => executor.runningDiscovery === 0);+ assert.equal(executor.runningDiscovery, 0);assert.equal(executor.discoveryCalls,callsAtCancellation,@@ -1414,21 +1149,14 @@ async function testCancellationClearsRetryWait() {async function testMissingDiscoveryResultResumesExistingThread(withToolFailure = false,) {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);+ const { fixture, store } = await coordinatorFixture();const executor = new FakeExecutor({- dedupNewFindings: [0],- omitFirstDiscoveryArtifact: true,+ invalidDiscoveryAttempts: 1,...(withToolFailure? {discoveryDiagnostics: [{- code: "artifact_tool_failed",+ code: "artifact_tool_failed" as const,message:"Codex worker artifact tool record_codex_security_scan_draft failed.",},@@ -1436,18 +1164,14 @@ async function testMissingDiscoveryResultResumesExistingThread(}: {}),});- const sleeps = [];- const coordinator = createCoordinator(fixture, store, executor, {+ const sleeps: number[] = [];+ const terminal = await runCoordinator(fixture, store, executor, {random: () => 0,retryDelaysMs: [1, 3, 9],clock: recordingClock(sleeps),});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded");- assert.equal(executor.discoveryCalls, 2);assert.deepEqual(sleeps, [1]);assert.deepEqual(executor.discoveryResumeThreadIds, [undefined,@@ -1480,19 +1204,17 @@ async function testMissingDiscoveryResultResumesExistingThread(}async function testInvalidArtifactsRetry() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,subagents: 1,stopAfterNoNew: 2,maxDiscoveryRuns: 2,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({- dedupNewFindings: [0],malformedDiscoveryAttempts: 1,});- const sleeps = [];- const coordinator = createCoordinator(fixture, store, executor, {+ const sleeps: number[] = [];+ const terminal = await runCoordinator(fixture, store, executor, {random: () => 0,retryDelaysMs: [1, 3, 9],clock: {@@ -1500,22 +1222,17 @@ async function testInvalidArtifactsRetry() {sleep: recordSleeps(sleeps),},});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.terminalReason, "saturated");assert.deepEqual(sleeps, [1]);- assert.equal(executor.discoveryCalls, 3);- const retriedPromptPaths = executor.discoveryPromptPaths.get(- [...executor.discoveryAttempts.entries()].find(- ([, attempts]) => attempts === 2,- )?.[0],- );const retriedWorkerId = [...executor.discoveryAttempts.entries()].find(([, attempts]) => attempts === 2,)?.[0];+ const retriedPromptPaths = executor.discoveryPromptPaths.get(+ retriedWorkerId!,+ );assert.deepEqual(- executor.discoveryResumeThreadIdsByWorker.get(retriedWorkerId),- [undefined, undefined],+ executor.discoveryResumeThreadIds,+ [undefined, undefined, undefined],"deterministic validation retries must start a clean thread",);assert.equal(retriedPromptPaths?.size, 2);@@ -1529,9 +1246,7 @@ async function testInvalidArtifactsRetry() {"workers",retriedContext.workerLabel,);- const retriedResult = JSON.parse(- await readFile(path.join(workerRoot, "output", "result.json"), "utf8"),- );+ const retriedResult = await readJson(workerRoot, "output", "result.json");assert.equal(retriedResult.scanId, fixture.run.scanId);assert.equal(await readFile(@@ -1568,21 +1283,19 @@ async function testInvalidArtifactsRetry() {async function testInvalidReducerResultRetriesFromSnapshot(missingCandidateLedger = false,) {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,subagents: 1,stopAfterNoNew: 2,maxDiscoveryRuns: 2,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({- dedupNewFindings: [0],...(missingCandidateLedger? {omitFirstDedupCandidateLedger: true,dedupDiagnostics: [{- code: "artifact_tool_failed",+ code: "artifact_tool_failed" as const,message:"Codex worker artifact tool record_codex_security_deep_reduction failed.",},@@ -1590,14 +1303,12 @@ async function testInvalidReducerResultRetriesFromSnapshot(}: { invalidFirstDedupResult: true }),});- const sleeps = [];- const coordinator = createCoordinator(fixture, store, executor, {+ const sleeps: number[] = [];+ const terminal = await runCoordinator(fixture, store, executor, {random: () => 0,retryDelaysMs: [1, 3, 9],clock: recordingClock(sleeps),});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.terminalReason, "saturated", terminal?.error);assert.deepEqual(sleeps, [1]);assert.equal(executor.dedupCalls, 2);@@ -1609,9 +1320,7 @@ async function testInvalidReducerResultRetriesFromSnapshot(new Set(executor.dedupPromptPaths).size,missingCandidateLedger ? 1 : 2,);- const [basePromptPath, retryPromptPath] = [- ...new Set(executor.dedupPromptPaths),- ];+ const [basePromptPath, retryPromptPath] = executor.dedupPromptPaths;assert.doesNotMatch(await readFile(basePromptPath, "utf8"),/Deterministic validation retry/,@@ -1638,32 +1347,24 @@ async function testInvalidReducerResultRetriesFromSnapshot(async function testMissingReducerResultResumesExistingThread(diagnosticMessage = "Codex worker artifact tool record_codex_security_deep_reduction failed.",) {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,+ const { fixture, store } = await coordinatorFixture({stopAfterConsecutiveErrors: 2,- maxDiscoveryRuns: 1,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({missingDedupResultsByLabel: { "dedup-0001": 1 },dedupDiagnostics: [{- code: "artifact_tool_failed",+ code: "artifact_tool_failed" as const,message: diagnosticMessage,},],});- const sleeps = [];- const coordinator = createCoordinator(fixture, store, executor, {+ const sleeps: number[] = [];+ const terminal = await runCoordinator(fixture, store, executor, {random: () => 0,retryDelaysMs: [1, 3, 9],clock: recordingClock(sleeps),});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded");assert.equal(store.dedupClaims.length, 1);@@ -1734,33 +1435,25 @@ async function testMissingReducerResultRetainsSizeDiagnosticAfterOtherFailures()code: "sandbox_namespace_exhausted",message: "Codex worker sandbox namespace creation failed (bwrap ENOSPC).",},- ]) {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);+ ] as const) {+ const { fixture, store } = await coordinatorFixture();const executor = new FakeExecutor({missingDedupResultsByLabel: { "dedup-0001": 1 },dedupDiagnostics: [earlierDiagnostic,- { code: "artifact_tool_failed", message: sizeMessage },+ { code: "artifact_tool_failed" as const, message: sizeMessage },],});- const coordinator = createCoordinator(fixture, store, executor, {+ const terminal = await runCoordinator(fixture, store, executor, {retryDelaysMs: [1],});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded", terminal?.error);const failure = store.workerUpdates.find((update) => update.kind === "dedup" && update.error,);- assert.ok(failure.error.includes(earlierDiagnostic.message));- assert.ok(failure.error.includes(sizeMessage));- assert.ok(failure.error.includes("result.json"));+ assert.ok(failure!.error!.includes(earlierDiagnostic.message));+ assert.ok(failure!.error!.includes(sizeMessage));+ assert.ok(failure!.error!.includes("result.json"));const retryPrompt = await readFile(executor.dedupPromptPaths[1], "utf8");assert.ok(retryPrompt.includes(earlierDiagnostic.message));assert.ok(retryPrompt.includes(sizeMessage));@@ -1768,32 +1461,27 @@ async function testMissingReducerResultRetainsSizeDiagnosticAfterOtherFailures()}async function testExhaustedReducerIsReplacedAtDiscoveryLimit() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,- subagents: 0,stopAfterNoNew: 4,stopAfterConsecutiveErrors: 2,maxDiscoveryRuns: 2,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({discoveryCandidateId: "candidate-1",canonicalCandidateId: "candidate-1",missingDedupResultsByLabel: { "dedup-0001": 4 },dedupDiagnostics: [{- code: "artifact_tool_failed",+ code: "artifact_tool_failed" as const,message:"Codex worker artifact tool record_codex_security_deep_reduction failed.",},],});- const coordinator = createCoordinator(fixture, store, executor, {+ const terminal = await runCoordinator(fixture, store, executor, {retryDelaysMs: [1, 3, 9],});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded", terminal?.error);assert.equal(terminal?.terminalReason, "capped");@@ -1812,9 +1500,12 @@ async function testExhaustedReducerIsReplacedAtDiscoveryLimit() {executor.dedupThreadIds[0],]);assert.equal(executor.dedupResumeThreadIds[4], undefined);- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));+ const manifest = await readJson(terminal.manifestPath);assert.deepEqual(- manifest.findings.map((finding) => finding.provenance.candidateId),+ manifest.findings.map(+ (finding: Record<string, unknown>) =>+ (finding.provenance as { candidateId: string }).candidateId,+ ),["candidate-1"],);assert.equal(store.dedupCommits.length, 1);@@ -1822,7 +1513,7 @@ async function testExhaustedReducerIsReplacedAtDiscoveryLimit() {(worker) => worker.kind === "dedup" && worker.status === "failed",);assert.equal(- path.basename(path.dirname(failedReducer.promptPath)),+ path.basename(path.dirname(failedReducer!.promptPath)),"dedup-0001",);assert.equal(failedReducer?.attempt, 4);@@ -1830,26 +1521,25 @@ async function testExhaustedReducerIsReplacedAtDiscoveryLimit() {}async function testExhaustedReducerPreservesCommittedArtifacts() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,- subagents: 0,stopAfterNoNew: 99,stopAfterConsecutiveErrors: 2,maxDiscoveryRuns: 3,});- const nextDiscovery = Promise.withResolvers();- const store = new FakeStore(fixture.run);+ const nextDiscovery = Promise.withResolvers<void>();const executor = new FakeExecutor({discoveryCandidateId: "candidate-1",canonicalCandidateId: "candidate-1",discoveryGates: { "discovery-0003": nextDiscovery.promise },invalidDedupFromCall: 2,});- const completedDrafts = [];+ const completedDrafts: ScanDraftInput[] = [];const coordinator = createCoordinator(fixture, store, executor, {retryDelaysMs: [1],threadId: "fixture-owning-thread",- onComplete: async (draft) => completedDrafts.push(structuredClone(draft)),+ onComplete: async (draft) =>+ void completedDrafts.push(structuredClone(draft)),});coordinator.start();@@ -1873,40 +1563,38 @@ async function testExhaustedReducerPreservesCommittedArtifacts() {);}-async function testCommittedAggregateIsNotSalvagedWhenUntrusted(failure) {- const fixture = await fixtureRun({+async function testCommittedAggregateIsNotSalvagedWhenUntrusted(+ failure: string,+) {+ const { fixture, store } = await coordinatorFixture({workers: 2,- subagents: 0,stopAfterNoNew: 99,stopAfterConsecutiveErrors: 1,maxDiscoveryRuns: 3,});- const nextDiscovery = Promise.withResolvers();- const store = new FakeStore(fixture.run);+ const nextDiscovery = Promise.withResolvers<void>();const executor = new FakeExecutor({discoveryCandidateId: "candidate-1",canonicalCandidateId: "candidate-1",discoveryGates: { "discovery-0003": nextDiscovery.promise },invalidDedupFromCall: 2,});- const completedDrafts = [];+ const completedDrafts: ScanDraftInput[] = [];const coordinator = createCoordinator(fixture, store, executor, {retryDelaysMs: [],...(failure === "missing-owner"? {}: { threadId: "fixture-owning-thread" }),- onComplete: async (draft) => completedDrafts.push(structuredClone(draft)),+ onComplete: async (draft) =>+ void completedDrafts.push(structuredClone(draft)),});coordinator.start();await store.dedupCommitted.promise;const committedResultPath = store.dedupCommits[0].resultManifestPath;if (failure === "wrong-scan") {- const draft = JSON.parse(await readFile(committedResultPath, "utf8"));- await writeFile(- committedResultPath,- JSON.stringify({ ...draft, scanId: randomUUID() }),- );+ const draft = await readJson(committedResultPath);+ await writeJson(committedResultPath, { ...draft, scanId: randomUUID() });}if (failure === "stale-owner") {const get = store.get.bind(store);@@ -1925,15 +1613,13 @@ async function testCommittedAggregateIsNotSalvagedWhenUntrusted(failure) {}async function testCancellationAfterCommittedAggregateRemainsCanceled() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,- subagents: 0,stopAfterNoNew: 99,maxDiscoveryRuns: 3,});- const nextDiscovery = Promise.withResolvers();- const store = new FakeStore(fixture.run);- const completedDrafts = [];+ const nextDiscovery = Promise.withResolvers<void>();+ const completedDrafts: ScanDraftInput[] = [];const coordinator = createCoordinator(fixture,store,@@ -1943,7 +1629,8 @@ async function testCancellationAfterCommittedAggregateRemainsCanceled() {}),{threadId: "fixture-owning-thread",- onComplete: async (draft) => completedDrafts.push(structuredClone(draft)),+ onComplete: async (draft) =>+ void completedDrafts.push(structuredClone(draft)),},);coordinator.start();@@ -1960,20 +1647,11 @@ async function testCancellationAfterCommittedAggregateRemainsCanceled() {}async function testFailedFirstReducerDoesNotPublishTentativeCandidates() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({ alwaysInvalidDedupResult: true });- const coordinator = createCoordinator(fixture, store, executor, {+ const { fixture, store } = await coordinatorFixture();+ const executor = new FakeExecutor({ invalidDedupFromCall: 1 });+ const terminal = await runCoordinator(fixture, store, executor, {retryDelaysMs: [1],});- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "failed");assert.equal(executor.dedupCalls, 2);assert.equal(store.dedupCommits.length, 0);@@ -1981,15 +1659,9 @@ async function testFailedFirstReducerDoesNotPublishTentativeCandidates() {}async function testCanceledReducerDoesNotPublishTentativeCandidates() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);+ const { fixture, store } = await coordinatorFixture();const executor = new FakeExecutor({ blockDedupAfterWrite: true });- const coordinator = createCoordinator(fixture, store, executor, {});+ const coordinator = createCoordinator(fixture, store, executor);coordinator.start();await executor.dedupArtifactsWritten.promise;@@ -2004,14 +1676,12 @@ async function testCanceledReducerDoesNotPublishTentativeCandidates() {}async function testRejectedStaleReducerCommitPreservesReplacementCandidates() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,- subagents: 0,stopAfterNoNew: 99,maxDiscoveryRuns: 3,});- const nextDiscovery = Promise.withResolvers();- const store = new FakeStore(fixture.run);+ const nextDiscovery = Promise.withResolvers<void>();store.failDedupCommitFromCall = 2;store.replacementCandidatesBeforeDedupRejection = JSON.stringify(standardScanDraft(@@ -2029,7 +1699,7 @@ async function testRejectedStaleReducerCommitPreservesReplacementCandidates() {],discoveryGates: { "discovery-0003": nextDiscovery.promise },});- const coordinator = createCoordinator(fixture, store, executor, {});+ const coordinator = createCoordinator(fixture, store, executor);coordinator.start();await store.dedupCommitted.promise;const canonicalPath = store.dedupCommits[0].resultManifestPath;@@ -2048,25 +1718,11 @@ async function testRejectedStaleReducerCommitPreservesReplacementCandidates() {}async function testRejectedFinishDoesNotOverwriteReplacementManifest() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);+ const { fixture, store } = await coordinatorFixture();store.failFinish = true;store.rejectFailurePersistence = true;store.replacementManifestBeforeFinishRejection = '{"owner":"replacement"}\n';- const coordinator = createCoordinator(- fixture,- store,- new FakeExecutor({ dedupNewFindings: [0] }),- {},- );- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);+ const terminal = await runCoordinator(fixture, store, new FakeExecutor());assert.equal(terminal?.status, "failed");assert.equal(await readFile(@@ -2079,14 +1735,12 @@ async function testRejectedFinishDoesNotOverwriteReplacementManifest() {}async function testAmbiguousReducerCommitPreservesPublishedCandidates() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,- subagents: 0,stopAfterNoNew: 99,maxDiscoveryRuns: 3,});- const nextDiscovery = Promise.withResolvers();- const store = new FakeStore(fixture.run);+ const nextDiscovery = Promise.withResolvers<void>();const executor = new FakeExecutor({discoveryCandidateId: "candidate-1",canonicalCandidateId: "candidate-1",@@ -2096,10 +1750,11 @@ async function testAmbiguousReducerCommitPreservesPublishedCandidates() {],discoveryGates: { "discovery-0003": nextDiscovery.promise },});- const completedDrafts = [];+ const completedDrafts: ScanDraftInput[] = [];const coordinator = createCoordinator(fixture, store, executor, {threadId: "fixture-owning-thread",- onComplete: async (draft) => completedDrafts.push(structuredClone(draft)),+ onComplete: async (draft) =>+ void completedDrafts.push(structuredClone(draft)),});coordinator.start();await store.dedupCommitted.promise;@@ -2115,8 +1770,7 @@ async function testAmbiguousReducerCommitPreservesPublishedCandidates() {const currentResultPath = store.dedupCommits[1].resultManifestPath;assert.notEqual(await readFile(currentResultPath, "utf8"), previous);assert.equal(- JSON.parse(await readFile(currentResultPath, "utf8")).findings[0].rootCause- .summary,+ (await readJson(currentResultPath)).findings[0].rootCause.summary,"possibly committed evidence",);assert.equal(@@ -2127,75 +1781,54 @@ async function testAmbiguousReducerCommitPreservesPublishedCandidates() {}async function testReducerTraceabilityRetryNamesExactMissingSource() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,- subagents: 0,stopAfterNoNew: 2,maxDiscoveryRuns: 2,});- const store = new FakeStore(fixture.run);const executor = new FakeExecutor({discoveryCandidateId: "candidate-1",canonicalCandidateId: "candidate-1",- invalidFirstDedupTraceability: true,+ invalidFirstDedupResult: true,});- const sleeps = [];- const coordinator = createCoordinator(fixture, store, executor, {+ const sleeps: number[] = [];+ const terminal = await runCoordinator(fixture, store, executor, {retryDelaysMs: [1, 3, 9],random: () => 0,clock: recordingClock(sleeps),});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded");assert.equal(terminal?.terminalReason, "capped");assert.deepEqual(sleeps, [1]);assert.equal(executor.dedupCalls, 2);- const [basePromptPath, retryPromptPath] = [- ...new Set(executor.dedupPromptPaths),- ];+ const [basePromptPath, retryPromptPath] = executor.dedupPromptPaths;const context = await promptContext(basePromptPath);- const missingWorkerId = context.claimedWorkerIds.at(-1);- const [basePrompt, retryPrompt] = await Promise.all([- readFile(basePromptPath, "utf8"),- readFile(retryPromptPath, "utf8"),- ]);+ const basePrompt = await readFile(basePromptPath, "utf8");+ const retryPrompt = await readFile(retryPromptPath, "utf8");assert.doesNotMatch(basePrompt, /artifact_validation_failed/);assert.match(retryPrompt, /artifact_validation_failed/);assert.match(retryPrompt, /findings/s);- assert.equal(context.claimedWorkerIds.includes(missingWorkerId), true);+ assert.equal(context.claimedWorkerIds.length > 0, true);}async function testThreeValidationAttemptsKeepPriorPromptsImmutable() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);+ const { fixture, store } = await coordinatorFixture();const executor = new FakeExecutor({- dedupNewFindings: [0],malformedDiscoveryAttempts: 2,});- const sleeps = [];- const coordinator = createCoordinator(fixture, store, executor, {+ const sleeps: number[] = [];+ const terminal = await runCoordinator(fixture, store, executor, {retryDelaysMs: [1, 3, 9],random: () => 0,clock: recordingClock(sleeps),});- coordinator.start();- const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded");assert.deepEqual(sleeps, [1, 3]);const promptPaths = [...executor.discoveryPromptPaths.values()][0];assert.equal(promptPaths.size, 3);- const [basePath, secondPath, thirdPath] = [...promptPaths];const [base, second, third] = await Promise.all(- [basePath, secondPath, thirdPath].map((promptPath) =>- readFile(promptPath, "utf8"),- ),+ [...promptPaths].map((promptPath) => readFile(promptPath, "utf8")),);assert.doesNotMatch(base, /Deterministic validation retry/);assert.match(second, /after attempt 1/);@@ -2205,15 +1838,14 @@ async function testThreeValidationAttemptsKeepPriorPromptsImmutable() {}async function testWaiterDetachAndCancellation() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 2,subagents: 1,stopAfterNoNew: 2,maxDiscoveryRuns: 4,});- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({ blockDiscovery: true });- const coordinator = createCoordinator(fixture, store, executor, {});+ const executor = new FakeExecutor({ blockDiscoveryAfterCalls: 0 });+ const coordinator = createCoordinator(fixture, store, executor);coordinator.start();await executor.discoveryStarted.promise;assert.equal(coordinator.snapshot().dispatchedCount, 2);@@ -2235,20 +1867,14 @@ async function testWaiterDetachAndCancellation() {coordinator.cancel("user canceled in UI");const canceled = await coordinator.wait(undefined, 5_000);assert.equal(canceled?.status, "canceled");- await eventually(() => executor.runningDiscovery === 0);- await eventually(() =>+ assert.equal(executor.runningDiscovery, 0);+ assert.ok([...store.workers.values()].every((worker) => worker.status === "canceled"),);}async function testCancellationDropsUnvalidatedDiscoveryResult() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);+ const { fixture, store } = await coordinatorFixture();const executor = new FakeExecutor({ blockDiscoveryAfterWrite: true });const coordinator = createCoordinator(fixture, store, executor, {threadId: "checkpoint-owner",@@ -2258,40 +1884,35 @@ async function testCancellationDropsUnvalidatedDiscoveryResult() {const worker = [...store.workers.values()].find((candidate) => candidate.kind === "discovery",);- const checkpointDir = path.join(worker.artifactDir, "checkpoints");+ const checkpointDir = path.join(worker!.artifactDir, "checkpoints");await mkdir(checkpointDir, { recursive: true });- await writeFile(+ await writeJson(path.join(checkpointDir, "saved.json"),- JSON.stringify(- standardScanDraft(fixture.run.scanId, "checkpoint-candidate", "saved"),- ),+ standardScanDraft(fixture.run.scanId, "checkpoint-candidate", "saved"),);coordinator.cancel("fixture canceled before host validation");const terminal = await coordinator.wait(undefined, 5_000);assert.equal(terminal?.status, "canceled");- await assert.rejects(readFile(path.join(worker.artifactDir, "result.json")), {- code: "ENOENT",- });+ await assert.rejects(+ readFile(path.join(worker!.artifactDir, "result.json")),+ {+ code: "ENOENT",+ },+ );assert.equal(- (await readdir(path.join(worker.artifactDir, "checkpoints"))).length > 0,+ (await readdir(path.join(worker!.artifactDir, "checkpoints"))).length > 0,true,"host-written checkpoints must survive cancellation",);}async function testCancellationDuringDiscoveryAcceptanceRejectsLateSuccess() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);- store.blockDiscoverySuccess = true;- const executor = new FakeExecutor({ dedupNewFindings: [0] });- const events = [];- const preserved = Promise.withResolvers();- const releasePreservation = Promise.withResolvers();+ const { fixture, store } = await coordinatorFixture();+ store.blockDiscoveryUpdate = "succeeded";+ const executor = new FakeExecutor();+ const events: DeepScanLogEvent[] = [];+ const preserved = Promise.withResolvers<DeepScanRunState>();+ const releasePreservation = Promise.withResolvers<void>();const coordinator = createCoordinator(fixture, store, executor, {log: (event) => events.push(event),threadId: "checkpoint-owner",@@ -2299,28 +1920,28 @@ async function testCancellationDuringDiscoveryAcceptanceRejectsLateSuccess() {const worker = [...store.workers.values()].find((worker) => worker.kind === "discovery",);- const result = JSON.parse(- await readFile(path.join(worker.artifactDir, "result.json"), "utf8"),- );- assert.equal(worker.status, "canceled");+ const result = await readJson(worker!.artifactDir, "result.json");+ assert.equal(worker!.status, "canceled");preserved.resolve(result);await releasePreservation.promise;},});coordinator.start();- await store.discoverySuccessBlocked.promise;+ await store.discoveryBlocked.promise;// The real cancel command persists this state before signaling the coordinator.store.run.status = "canceled";for (const worker of store.workers.values()) worker.status = "canceled";coordinator.cancel("fixture persisted cancellation");- store.releaseDiscoverySuccess();+ store.discoveryGate.resolve();let terminalSettled = false;- const terminalPromise = coordinator.wait(undefined, 5_000).then((state) => {- terminalSettled = true;- return state;- });+ const terminalPromise = coordinator+ .wait(undefined, 5_000)+ .then((state: DeepScanRunState) => {+ terminalSettled = true;+ return state;+ });assert.equal((await preserved.promise).scanId, fixture.run.scanId);await Promise.resolve();assert.equal(@@ -2331,7 +1952,7 @@ async function testCancellationDuringDiscoveryAcceptanceRejectsLateSuccess() {releasePreservation.resolve();const terminal = await terminalPromise;assert.equal(terminal?.status, "canceled");- await eventually(() =>+ assert.ok(events.some((event) => event.event === "coordinator_cleanup_settled"),);assert.equal(@@ -2341,9 +1962,7 @@ async function testCancellationDuringDiscoveryAcceptanceRejectsLateSuccess() {const worker = [...store.workers.values()].find((worker) => worker.kind === "discovery",);- const result = JSON.parse(- await readFile(path.join(worker.artifactDir, "result.json"), "utf8"),- );+ const result = await readJson(worker!.artifactDir, "result.json");assert.equal(result.scanId,fixture.run.scanId,@@ -2364,33 +1983,23 @@ async function testCancellationDuringDiscoveryAcceptanceRejectsLateSuccess() {async function testRegistryEvictionAndExternalFailure() {const completedFixture = await fixtureRun({- workers: 1,- subagents: 0,stopAfterNoNew: 2,- maxDiscoveryRuns: 1,});const completedStore = new FakeStore(completedFixture.run);const registry = new DeepScanCoordinatorRegistry();const completed = registry.start({run: completedFixture.run,store: completedStore,- executor: new FakeExecutor({ dedupNewFindings: [1] }),+ executor: new FakeExecutor({ discoveryCandidateId: "candidate-1" }),pluginRoot: completedFixture.pluginRoot,clock: immediateClock,});assert.equal((await completed.wait(undefined, 5_000))?.status, "succeeded");- await eventually(- () => registry.get(completedFixture.run.scanId) === undefined,- );+ assert.equal(registry.get(completedFixture.run.scanId), undefined);- const failedFixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });+ const failedFixture = await fixtureRun();const failedStore = new FakeStore(failedFixture.run);- const failedExecutor = new FakeExecutor({ blockDiscovery: true });+ const failedExecutor = new FakeExecutor({ blockDiscoveryAfterCalls: 0 });const failed = registry.start({run: failedFixture.run,store: failedStore,@@ -2407,25 +2016,19 @@ async function testRegistryEvictionAndExternalFailure() {const terminal = await failed.wait(undefined, 5_000);assert.equal(terminal?.status, "failed");assert.equal(terminal?.error, "failure already persisted by fail-scan");- await eventually(() => failedExecutor.runningDiscovery === 0);- await eventually(() => registry.get(failedFixture.run.scanId) === undefined);+ assert.equal(failedExecutor.runningDiscovery, 0);+ assert.equal(registry.get(failedFixture.run.scanId), undefined);assert.equal(- failedStore.failureInputs.length,+ failedStore.failureMessages.length,0,"an externally persisted failure must not be persisted again",);}async function testStoppedPublicationFailurePreservesOriginalDiagnostic() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({ blockDiscovery: true });- const events = [];+ const { fixture, store } = await coordinatorFixture();+ const executor = new FakeExecutor({ blockDiscoveryAfterCalls: 0 });+ const events: DeepScanLogEvent[] = [];const coordinator = createCoordinator(fixture, store, executor, {threadId: "checkpoint-owner",log: (event) => events.push(event),@@ -2474,14 +2077,8 @@ async function testStoppedPublicationFailurePreservesOriginalDiagnostic() {}async function testStoppedPublicationFailureBoundsPrefixedDiagnostic() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({ blockDiscovery: true });+ const { fixture, store } = await coordinatorFixture();+ const executor = new FakeExecutor({ blockDiscoveryAfterCalls: 0 });const coordinator = createCoordinator(fixture, store, executor, {threadId: "checkpoint-owner",onStopped: async () => {@@ -2506,14 +2103,8 @@ async function testStoppedPublicationFailureBoundsPrefixedDiagnostic() {}async function testTerminalReadFailureIsNotRecordedAsPublicationFailure() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });- const store = new FakeStore(fixture.run);- const executor = new FakeExecutor({ blockDiscovery: true });+ const { fixture, store } = await coordinatorFixture();+ const executor = new FakeExecutor({ blockDiscoveryAfterCalls: 0 });const publicationAttempts = mock.fn(async () => {});const coordinator = createCoordinator(fixture, store, executor, {threadId: "checkpoint-owner",@@ -2535,8 +2126,6 @@ async function testTerminalReadFailureIsNotRecordedAsPublicationFailure() {async function testCoordinatorHeartbeatsStopAfterOwnershipChanges() {const fixture = await fixtureRun({- workers: 1,- subagents: 0,stopAfterNoNew: 2,maxDiscoveryRuns: 2,});@@ -2546,9 +2135,10 @@ async function testCoordinatorHeartbeatsStopAfterOwnershipChanges() {updatedAt: new Date().toISOString(),};const store = new FakeStore(run);- store.heartbeatCoordinator = mock.fn(async () => {+ const heartbeatCoordinator = mock.fn(async () => {return structuredClone(store.run);});+ store.heartbeatCoordinator = heartbeatCoordinator;let reads = 0;store.get = async () => {reads += 1;@@ -2563,7 +2153,7 @@ async function testCoordinatorHeartbeatsStopAfterOwnershipChanges() {};return structuredClone(store.run);};- const executor = new FakeExecutor({ blockDiscovery: true });+ const executor = new FakeExecutor({ blockDiscoveryAfterCalls: 0 });const registry = new DeepScanCoordinatorRegistry();const coordinator = registry.start({run,@@ -2580,7 +2170,7 @@ async function testCoordinatorHeartbeatsStopAfterOwnershipChanges() {await new Promise((resolve) => setTimeout(resolve, 25));assert.equal(- store.heartbeatCoordinator.mock.callCount(),+ heartbeatCoordinator.mock.callCount(),3,"heartbeat writes continue until a newer generation is confirmed",);@@ -2592,7 +2182,7 @@ async function testCoordinatorHeartbeatsStopAfterOwnershipChanges() {assert.equal(current?.status, "succeeded");assert.equal(current?.coordinatorGeneration, 3);assert.equal(- store.failureInputs.length,+ store.failureMessages.length,0,"a stale coordinator must never fail the new owner",);@@ -2600,14 +2190,12 @@ async function testCoordinatorHeartbeatsStopAfterOwnershipChanges() {async function testCoordinatorHeartbeatsContinueDuringBlockedOwnershipRead() {const fixture = await fixtureRun({- workers: 1,- subagents: 0,stopAfterNoNew: 2,maxDiscoveryRuns: 2,});const run = { ...fixture.run, coordinatorGeneration: 2 };const store = new FakeStore(run);- const ownershipRead = Promise.withResolvers();+ const ownershipRead = Promise.withResolvers<void>();let heartbeats = 0;let reads = 0;store.heartbeatCoordinator = async () => {@@ -2622,7 +2210,7 @@ async function testCoordinatorHeartbeatsContinueDuringBlockedOwnershipRead() {const coordinator = new DeepScanCoordinatorRegistry().start({run,store,- executor: new FakeExecutor({ blockDiscovery: true }),+ executor: new FakeExecutor({ blockDiscoveryAfterCalls: 0 }),pluginRoot: fixture.pluginRoot,clock: immediateClock,threadId: "thread-fixture",@@ -2640,12 +2228,7 @@ async function testCoordinatorHeartbeatsContinueDuringBlockedOwnershipRead() {}async function testRemoteObserverRetriesTransientPersistenceFailures() {- const fixture = await fixtureRun({- workers: 1,- subagents: 0,- stopAfterNoNew: 1,- maxDiscoveryRuns: 1,- });+ const fixture = await fixtureRun();const store = new FakeStore({ ...fixture.run, coordinatorGeneration: 2 });let reads = 0;store.get = async () => {@@ -2684,7 +2267,7 @@ async function testRemoteObserverRetriesTransientPersistenceFailures() {"canceled","terminal-read-locked","unauthorized",- ]) {+ ] as const) {store.run = {...fixture.run,coordinatorGeneration: 2,@@ -2750,8 +2333,6 @@ async function testRemoteObserverRetriesTransientPersistenceFailures() {async function testStaleMutationObservesReplacement() {const fixture = await fixtureRun({- workers: 1,- subagents: 0,stopAfterNoNew: 2,maxDiscoveryRuns: 2,});@@ -2791,7 +2372,7 @@ async function testStaleMutationObservesReplacement() {assert.equal(terminal?.status, "succeeded");assert.equal(terminal?.coordinatorGeneration, 3);assert.equal(- store.failureInputs.length,+ store.failureMessages.length,0,"a fenced mutation must observe rather than fail the replacement",);@@ -2813,7 +2394,7 @@ async function testJoinAndOrphanRules() {const starts = mock.fn(() => existingCoordinator);const failures = mock.fn(async () => {});const existing = await startOrJoinDeepScanCoordinator({- run: { ...fixture.run, persistedWorkerCount: 3 },+ run: fixture.run,registry: {get: () => existingCoordinator,start: starts,@@ -2895,7 +2476,7 @@ async function testJoinAndOrphanRules() {run: fixture.run,registry: {get: () => undefined,- start: (options) => {+ start: (options: CoordinatorOptions) => {starts();assert.equal(options.run.coordinatorGeneration, 2);return existingCoordinator;@@ -2926,8 +2507,8 @@ async function testJoinAndOrphanRules() {);const lock = new AsyncLock();- const firstGate = Promise.withResolvers();- let liveCoordinator;+ const firstGate = Promise.withResolvers<void>();+ let liveCoordinator: { marker: string } | undefined;starts.mock.resetCalls();const registry = {get: () => liveCoordinator,@@ -2977,15 +2558,13 @@ async function testJoinAndOrphanRules() {async function testPausedDiscoverySurvivesCoordinatorRestart() {const fixture = await fixtureRun({- workers: 1,- subagents: 0,stopAfterNoNew: 2,maxDiscoveryRuns: 2,});const handoffClaimToken = randomUUID();const store = new FakeStore({...fixture.run,- phase: "setup",+ phase: "setup" as const,coordinatorGeneration: 2,updatedAt: "2026-08-03T13:33:08Z",});@@ -3011,9 +2590,7 @@ async function testPausedDiscoverySurvivesCoordinatorRestart() {// without canceling or finalizing the persisted scan.original.cancel("mcp server process restarted");await eventually(() => originalExecutor.runningDiscovery === 0);- const persistedWorkers = [...store.workers.values()].map((worker) =>- structuredClone(worker),- );+ const persistedWorkers = structuredClone([...store.workers.values()]);const independentReviews = {completed: persistedWorkers.filter((worker) => worker.kind === "discovery" && worker.status === "succeeded",@@ -3033,7 +2610,7 @@ async function testPausedDiscoverySurvivesCoordinatorRestart() {assert.equal(store.run.status, "running");assert.equal(store.run.phase, "discovery");assert.equal(store.finishCalls.length, 0);- assert.equal(store.failureInputs.length, 0);+ assert.equal(store.failureMessages.length, 0);assert.equal(store.run.manifestPath, undefined);await assert.rejects(readFile(@@ -3052,10 +2629,10 @@ async function testPausedDiscoverySurvivesCoordinatorRestart() {dispatchedCount: 1,persistedWorkers,};- const continuationClaims = [];+ const continuationClaims: StoreInput<"claimCoordinator">[] = [];store.claimCoordinator = async (input) => {- continuationClaims.push(structuredClone(input));- assert.equal(input.handoffClaimToken, handoffClaimToken);+ continuationClaims.push(structuredClone(input!));+ assert.equal(input!.handoffClaimToken, handoffClaimToken);store.run = {...store.run,coordinatorGeneration: 3,@@ -3064,8 +2641,8 @@ async function testPausedDiscoverySurvivesCoordinatorRestart() {return { acquired: true, run: structuredClone(store.run) };};store.heartbeatCoordinator = async () => structuredClone(store.run);- const replacementExecutor = new FakeExecutor({ dedupNewFindings: [0] });- const acceptedResult = await readFile(accepted.resultManifestPath, "utf8");+ const replacementExecutor = new FakeExecutor();+ const acceptedResult = await readFile(accepted.resultManifestPath!, "utf8");const resumed = await startOrJoinDeepScanCoordinator({run: structuredClone(store.run),registry: new DeepScanCoordinatorRegistry(),@@ -3083,32 +2660,30 @@ async function testPausedDiscoverySurvivesCoordinatorRestart() {assert.equal(continuationClaims.length, 1);assert.equal(continuationClaims[0].handoffClaimToken, handoffClaimToken);assert.equal(terminal?.status, "succeeded");- assert.equal(store.failureInputs.length, 0);- assert.equal(replacementExecutor.logicalDiscoveryWorkers.size, 1);+ assert.equal(store.failureMessages.length, 0);+ assert.equal(replacementExecutor.discoveryAttempts.size, 1);assert.equal(- replacementExecutor.logicalDiscoveryWorkers.has(+ replacementExecutor.discoveryAttempts.has(await workerIdFromPrompt(accepted.promptPath),),false,);assert.equal(store.dedupClaims.length, 1);assert.equal(store.dedupClaims[0].workerIds.includes(accepted.id), true);- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));+ const manifest = await readJson(terminal.manifestPath);assert.equal(manifest.scan.scanId, fixture.run.scanId);assert.equal(store.dedupClaims[0].workerIds.length, 2);assert.equal(- await readFile(accepted.resultManifestPath, "utf8"),+ await readFile(accepted.resultManifestPath!, "utf8"),acceptedResult,);}async function testResumedDiscoveryDeadlineUsesPersistedCreationTime(alreadyExpired = false,- maxTimeHours,+ maxTimeHours?: number,) {const fixture = await fixtureRun({- workers: 1,- subagents: 0,stopAfterNoNew: 99,maxDiscoveryRuns: 8,...(maxTimeHours === undefined ? {} : { maxTimeHours }),@@ -3125,7 +2700,7 @@ async function testResumedDiscoveryDeadlineUsesPersistedCreationTime(const store = new FakeStore({...fixture.run,createdAt,- phase: "setup",+ phase: "setup" as const,coordinatorGeneration: 2,});const originalExecutor = new FakeExecutor({@@ -3157,15 +2732,13 @@ async function testResumedDiscoveryDeadlineUsesPersistedCreationTime((alreadyExpired ? 1_000 : -1_000);store.run = {...store.run,- persistedWorkers: [...store.workers.values()].map((worker) =>- structuredClone(worker),- ),+ persistedWorkers: structuredClone([...store.workers.values()]),};const resumedExecutor = new FakeExecutor({- blockDiscovery: true,+ blockDiscoveryAfterCalls: 0,canonicalCandidateId: "candidate-1",- dedupNewFindings: [1],+ discoveryCandidateId: "candidate-1",});const resumed = createCoordinator(fixture, store, resumedExecutor, {run: store.run,@@ -3177,12 +2750,12 @@ async function testResumedDiscoveryDeadlineUsesPersistedCreationTime(const terminal = await resumed.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded");assert.equal(terminal?.terminalReason, "capped");- assert.equal(store.failureInputs.length, 0);+ assert.equal(store.failureMessages.length, 0);assert.equal(resumedExecutor.discoveryCalls, alreadyExpired ? 0 : 1);assert.equal(resumedExecutor.dedupCalls, 1);assert.equal(resumedExecutor.runningDiscovery, 0);- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));+ const manifest = await readJson(terminal.manifestPath);assert.equal(store.run.config.maxTimeHours, maxTimeHours);assert.equal(store.dedupClaims[0].workerIds.length, 1);assert.equal(@@ -3190,7 +2763,10 @@ async function testResumedDiscoveryDeadlineUsesPersistedCreationTime(true,);assert.deepEqual(- manifest.findings.map((finding) => finding.provenance.candidateId),+ manifest.findings.map(+ (finding: Record<string, unknown>) =>+ (finding.provenance as { candidateId: string }).candidateId,+ ),["candidate-1"],);}@@ -3200,33 +2776,31 @@ async function testResumedManifestPreservesCompletedReducer() {const fixture = await fixtureRun({workers: 2,- subagents: 0,stopAfterNoNew: 2,maxDiscoveryRuns: 3,});const store = new FakeStore({ ...fixture.run, phase: "setup" });- store.blockDedupCommitResponse = true;+ store.dedupCommitResponseGate = Promise.withResolvers<void>();const original = createCoordinator(fixture,store,new FakeExecutor({- dedupNewFindings: [0],blockDiscoveryAfterCalls: 2,}),{ run: store.run },);original.start();- await store.dedupCommitPersisted.promise;+ await store.dedupCommitted.promise;original.cancel("mcp server process restarted");- store.releaseDedupCommitResponse();+ store.dedupCommitResponseGate.resolve();await original.settled();- await eventually(() =>+ assert.ok([...store.workers.values()].every((worker) => worker.status !== "queued" && worker.status !== "running",),);- let unstartedReducer;+ let unstartedReducer: TestWorker | undefined;if (includeUnstartedReducer) {const artifactDir = path.join(fixture.run.scanDir,@@ -3244,38 +2818,33 @@ async function testResumedManifestPreservesCompletedReducer();unstartedReducer = {id: randomUUID(),- kind: "dedup",- status: "canceled",+ kind: "dedup" as const,+ status: "canceled" as const,promptPath,artifactDir,attempt: 0,- mergeState: "none",+ mergeState: "none" as const,};store.workers.set(unstartedReducer.id, unstartedReducer);}store.run = {...store.run,- status: "running",- phase: "discovery",- persistedWorkers: [...store.workers.values()].map((worker) =>- structuredClone(worker),- ),+ status: "running" as const,+ phase: "discovery" as const,+ persistedWorkers: structuredClone([...store.workers.values()]),persistedDedupInputs: store.dedupClaims.flatMap(claimDedupInputs),};- const replacement = createCoordinator(fixture, store, new FakeExecutor(), {+ const terminal = await runCoordinator(fixture, store, new FakeExecutor(), {run: store.run,});- replacement.start();-- const terminal = await replacement.wait(undefined, 5_000);assert.equal(terminal?.status, "succeeded");- const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));+ const manifest = await readJson(terminal.manifestPath);assert.equal(manifest.scan.scanId, fixture.run.scanId);assert.equal(store.dedupCommits.length, 1);assert.equal(store.dedupClaims.length, 1);assert.equal(- store.run.persistedWorkers.some(+ store.run.persistedWorkers!.some((worker) => worker.id === unstartedReducer?.id,),includeUnstartedReducer,@@ -3283,21 +2852,18 @@ async function testResumedManifestPreservesCompletedReducer(}async function testResumeUsesHistoricalCandidateSnapshotForEachReducer() {- const fixture = await fixtureRun({+ const { fixture, store } = await coordinatorFixture({workers: 3,- subagents: 0,stopAfterNoNew: 10,maxDiscoveryRuns: 5,});- const store = new FakeStore(fixture.run);const original = createCoordinator(fixture,store,new FakeExecutor({- dedupNewFindings: [1, 0],+ discoveryCandidateId: "candidate-1",dedupEvidenceByCall: ["first reducer evidence", "final reducer evidence"],}),- {},);original.start();assert.equal((await original.wait(undefined, 5_000))?.status, "succeeded");@@ -3305,44 +2871,34 @@ async function testResumeUsesHistoricalCandidateSnapshotForEachReducer() {store.run = {...store.run,- status: "running",- phase: "discovery",+ status: "running" as const,+ phase: "discovery" as const,terminalReason: undefined,manifestPath: undefined,- persistedWorkers: [...store.workers.values()].map((worker) =>- structuredClone(worker),- ),+ persistedWorkers: structuredClone([...store.workers.values()]),persistedDedupInputs: store.dedupClaims.flatMap(claimDedupInputs),};- const completedDrafts = [];- const replacement = createCoordinator(fixture, store, new FakeExecutor(), {+ const completedDrafts: ScanDraftInput[] = [];+ const resumed = await runCoordinator(fixture, store, new FakeExecutor(), {run: store.run,- onComplete: async (draft) => completedDrafts.push(structuredClone(draft)),+ onComplete: async (draft) =>+ void completedDrafts.push(structuredClone(draft)),});- replacement.start();-- const resumed = await replacement.wait(undefined, 5_000);assert.equal(resumed?.status, "succeeded", resumed?.error);- const firstReducer = store.run.persistedWorkers.find(+ const firstReducer = store.run.persistedWorkers!.find((worker) =>worker.kind === "dedup" && worker.promptPath.includes("dedup-0001"),);assert.ok(firstReducer);- const firstResult = JSON.parse(- await readFile(firstReducer.resultManifestPath, "utf8"),- );+ const firstResult = await readJson(firstReducer.resultManifestPath!);assert.equal(firstResult.findings[0]?.rootCause.summary,"first reducer evidence",);- const lastReducer = store.run.persistedWorkers- .filter(- (worker) => worker.kind === "dedup" && worker.status === "succeeded",- )- .at(-1);- const latestResult = JSON.parse(- await readFile(lastReducer.resultManifestPath, "utf8"),+ const lastReducer = store.run.persistedWorkers!.findLast(+ (worker) => worker.kind === "dedup" && worker.status === "succeeded",);+ const latestResult = await readJson(lastReducer!.resultManifestPath!);assert.equal(latestResult.findings[0]?.rootCause.summary,"final reducer evidence",@@ -3355,20 +2911,18 @@ async function testResumeUsesHistoricalCandidateSnapshotForEachReducer() {async function testPersistedErrorLimitStopsBeforeRescheduling() {const fixture = await fixtureRun({- workers: 1,- subagents: 0,stopAfterNoNew: 2,stopAfterConsecutiveErrors: 2,maxDiscoveryRuns: 2,});const failedWorker = {id: randomUUID(),- kind: "discovery",- status: "canceled",+ kind: "discovery" as const,+ status: "canceled" as const,promptPath: path.join(fixture.run.scanDir, "failed", "prompt.md"),artifactDir: path.join(fixture.run.scanDir, "failed", "output"),attempt: 1,- mergeState: "none",+ mergeState: "none" as const,error: "transient_error: persisted worker failure",};for (const promptExists of [false, true]) {@@ -3378,7 +2932,7 @@ async function testPersistedErrorLimitStopsBeforeRescheduling() {}const run = {...fixture.run,- phase: "discovery",+ phase: "discovery" as const,consecutiveErrors: 2,dispatchedCount: 1,persistedWorkers: [failedWorker],@@ -3404,14 +2958,12 @@ async function testPersistedErrorLimitStopsBeforeRescheduling() {if (promptExists)assert.match(terminal?.error ?? "", /persisted worker failure/);assert.equal(terminal.manifestPath, undefined);- assert.equal(store.run.persistedWorkers[0].id, failedWorker.id);+ assert.equal(store.run.persistedWorkers![0].id, failedWorker.id);}}async function testPersistedReducerErrorLimitStopsBeforeRescheduling() {const fixture = await fixtureRun({- workers: 1,- subagents: 0,stopAfterNoNew: 3,stopAfterConsecutiveErrors: 2,maxDiscoveryRuns: 2,@@ -3427,28 +2979,25 @@ async function testPersistedReducerErrorLimitStopsBeforeRescheduling() {await writeFile(promptPath, `persisted reducer ${index}\n`);return {id: randomUUID(),- kind: "dedup",- status: "failed",+ kind: "dedup" as const,+ status: "failed" as const,promptPath,artifactDir: directory,attempt: 1,- mergeState: "none",+ mergeState: "none" as const,error: `persisted reducer failure ${index}`,};}),);const run = {...fixture.run,- phase: "discovery",+ phase: "discovery" as const,consecutiveErrors: 0,persistedWorkers: reducers,};const store = new FakeStore(run);const executor = new FakeExecutor();- const coordinator = createCoordinator(fixture, store, executor, { run });- coordinator.start();-- const terminal = await coordinator.wait(undefined, 5_000);+ const terminal = await runCoordinator(fixture, store, executor, { run });assert.equal(terminal?.status, "failed");assert.equal(executor.discoveryCalls, 0);@@ -3460,831 +3009,726 @@ async function testPersistedReducerErrorLimitStopsBeforeRescheduling() {assert.match(terminal?.error ?? "", /persisted reducer failure 2/);}-async function fixtureRun(config) {- const root = await temporaryDirectories.create(- "codex-security-deep-coordinator-",- );- const targetPath = path.join(root, "target");- const scanDir = path.join(root, "scan");- const pluginRoot = path.join(root, "plugin");- await Promise.all([mkdir(targetPath), mkdir(scanDir), mkdir(pluginRoot)]);- return {- pluginRoot,- run: {- scanId: randomUUID(),- status: "running",- targetPath,- scope: ".",- scanDir,- config: {- ...config,- stopAfterConsecutiveErrors:- config.stopAfterConsecutiveErrors ?? config.stopAfterNoNew,- },- dispatchedCount: 0,- noNewStreak: 0,- consecutiveErrors: 0,- persistedWorkerCount: 0,- },- };-}+async function testDiscoveryDeadlineDrainsActiveReducerAndPreservesFindings() {+ const { fixture, store } = await coordinatorFixture({+ workers: 2,+ stopAfterNoNew: 99,+ maxDiscoveryRuns: 12,+ });+ const executor = new FakeExecutor({+ blockDiscoveryAfterCalls: 2,+ discoveryCandidateId: "candidate-1",+ canonicalCandidateId: "candidate-1",+ });+ executor.dedupGate = Promise.withResolvers<void>();+ const coordinator = createCoordinator(fixture, store, executor, {+ discoveryTimeoutMs: 500,+ });+ coordinator.start();-class FakeStore {- constructor(run) {- this.run = structuredClone(run);- }+ await executor.dedupStarted.promise;+ await eventually(() => executor.runningDiscovery > 0);+ await eventually(() => executor.runningDiscovery === 0);- workerUpdates = [];- workers = new Map();- completionSequence = 0;- progress = [];- dedupClaims = [];- dedupCommits = [];- dedupCommitCalls = [];- committedDedupIds = new Set();- failDedupCommitFromCall = undefined;- loseEveryDedupCommitResponseAfterCommit = false;- progressCalls = 0;- failureInputs = [];- finishCalls = [];- failFinish = false;- rejectFailurePersistence = false;- replacementManifestBeforeFinishRejection = undefined;- replacementCandidatesBeforeDedupRejection = undefined;- loseFirstFinishResponseAfterCommit = false;- loseFirstDiscoveryAcceptanceResponseAfterCommit = false;- discoveryAcceptanceResponseLosses = 0;- loseFirstDedupCommitResponseAfterCommit = false;- dedupCommitResponseLosses = 0;- blockDedupCommitResponse = false;- dedupCommitPersisted = Promise.withResolvers();- dedupCommitResponseGate = Promise.withResolvers();- blockDiscoverySuccess = false;- discoverySuccessBlocked = Promise.withResolvers();- discoverySuccessGate = Promise.withResolvers();- blockDiscoveryFailure = false;- discoveryFailureBlocked = Promise.withResolvers();- discoveryFailureGate = Promise.withResolvers();- dedupCommitted = Promise.withResolvers();- failNextTerminalGet = false;- publicationFailureMessages = [];-- async begin() {- return structuredClone(this.run);- }+ const discoveryCallsAtDeadline = executor.discoveryCalls;+ assert.equal(+ executor.runningDedup,+ 1,+ "the deadline must let an active reducer finish",+ );+ assert.equal(executor.dedupSignal?.aborted, false);+ assert.equal(store.finishCalls.length, 0);+ executor.dedupGate.resolve();- async get() {- if (this.failNextTerminalGet && this.run.status !== "running") {- this.failNextTerminalGet = false;- throw new Error("database is locked");- }- return structuredClone({- ...this.run,- ...(this.workers.size > 0- ? { persistedWorkers: [...this.workers.values()] }- : {}),- });- }+ const terminal = await coordinator.wait(undefined, 5_000);+ assert.equal(terminal?.status, "succeeded");+ assert.equal(terminal?.terminalReason, "capped");+ assert.equal(store.failureMessages.length, 0);+ assert.equal(executor.discoveryCalls, discoveryCallsAtDeadline);+ assert.equal(+ terminal.dispatchedCount < fixture.run.config.maxDiscoveryRuns,+ true,+ );- async claimCoordinator() {- return { run: structuredClone(this.run), acquired: true };- }+ const manifest = await readJson(terminal.manifestPath);+ assert.equal(manifest.scan.scanId, fixture.run.scanId);+ assert.deepEqual(store.finishCalls[0].omittedWorkerIds, []);+ assert.equal(+ [...store.workers.values()].some((worker) => worker.status === "canceled"),+ true,+ );+ assert.equal(store.dedupCommits.length, 1);+ assert.deepEqual(+ manifest.findings.map(+ (finding: { provenance: { candidateId: string } }) =>+ finding.provenance.candidateId,+ ),+ ["candidate-1"],+ );+ assert.equal(+ [...store.workers.values()].some((worker) =>+ ["queued", "running"].includes(worker.status),+ ),+ false,+ "deadline completion must wait for every canceled discovery and reducer to settle",+ );+}- async heartbeatCoordinator() {- this.run.updatedAt = new Date().toISOString();- return structuredClone(this.run);- }+async function testDiscoveryDeadlineReducesSingleBufferedFinding() {+ const { fixture, store } = await coordinatorFixture({+ stopAfterNoNew: 99,+ maxDiscoveryRuns: 8,+ });+ const executor = new FakeExecutor({+ blockDiscoveryAfterCalls: 1,+ discoveryCandidateId: "candidate-1",+ canonicalCandidateId: "candidate-1",+ });+ const coordinator = createCoordinator(fixture, store, executor, {+ discoveryTimeoutMs: 500,+ });+ coordinator.start();- async updateWorker(update) {- if (update.error) {- assert.equal(- update.error.length <= 2_400,- true,- "persisted worker errors must be bounded",- );- }- if (- this.blockDiscoveryFailure &&- update.kind === "discovery" &&- update.status === "failed"- ) {- this.discoveryFailureBlocked.resolve();- await this.discoveryFailureGate.promise;- }- if (- this.blockDiscoverySuccess &&- update.kind === "discovery" &&- update.status === "succeeded"- ) {- this.discoverySuccessBlocked.resolve();- await this.discoverySuccessGate.promise;- if (this.run.status !== "running") {- throw new Error(- "Only a running Deep Scan can update orchestration state.",- );- }- }- this.workerUpdates.push(structuredClone(update));- const previous = this.workers.get(update.id);- const persisted = {- mergeState: "none",- ...previous,- ...structuredClone(update),- };- if (- update.kind === "discovery" &&- update.status === "queued" &&- !previous- ) {- this.run.dispatchedCount += 1;- }- if (- update.kind === "discovery" &&- update.status === "canceled" &&- update.replaceableFailureKind &&- previous?.status === "running"- ) {- this.run.consecutiveErrors += 1;- }- if (- update.kind === "discovery" &&- update.status === "succeeded" &&- !persisted.completionSequence- ) {- persisted.completionSequence = ++this.completionSequence;- persisted.mergeState = "buffered";- this.run.consecutiveErrors = 0;- }- if (- update.kind === "dedup" &&- update.status === "failed" &&- previous?.status === "running"- ) {- const claim = this.dedupClaims.find(- (candidate) => candidate.id === update.id,- );- for (const workerId of claim?.workerIds ?? []) {- const discovery = this.workers.get(workerId);- if (- discovery?.status === "succeeded" &&- discovery.mergeState === "merging"- ) {- this.workers.set(workerId, { ...discovery, mergeState: "buffered" });- }- }- this.run.phase = "discovery";- }- persisted.consecutiveErrors = this.run.consecutiveErrors;- this.workers.set(update.id, persisted);- this.run.persistedWorkerCount = this.workers.size;- if (- this.loseFirstDiscoveryAcceptanceResponseAfterCommit &&- update.kind === "discovery" &&- update.status === "succeeded" &&- this.discoveryAcceptanceResponseLosses === 0- ) {- this.discoveryAcceptanceResponseLosses += 1;- throw new Error(- "fixture lost discovery acceptance response after commit",- );- }- return structuredClone(persisted);- }+ await eventually(+ () =>+ executor.discoveryCalls === 2 &&+ executor.runningDiscovery === 1 &&+ [...store.workers.values()].some(+ (worker) =>+ worker.kind === "discovery" && worker.status === "succeeded",+ ),+ );+ assert.equal(+ executor.dedupCalls,+ 0,+ "the first singleton remains buffered before the deadline",+ );- async claimDedup(input) {- this.dedupClaims.push(structuredClone(input));- for (const workerId of input.workerIds) {- const discovery = this.workers.get(workerId);- assert.equal(discovery?.mergeState, "buffered");- this.workers.set(workerId, { ...discovery, mergeState: "merging" });- }- await this.updateWorker({- id: input.id,- scanId: input.scanId,- kind: "dedup",- status: "running",- promptPath: input.promptPath,- artifactDir: input.artifactDir,- attempt: 1,- });- }+ const terminal = await coordinator.wait(undefined, 5_000);+ assert.equal(terminal?.status, "succeeded");+ assert.equal(terminal?.terminalReason, "capped");+ assert.equal(terminal.dispatchedCount, 2);+ assert.equal(+ terminal.dispatchedCount < fixture.run.config.maxDiscoveryRuns,+ true,+ );+ assert.equal(store.failureMessages.length, 0);+ assert.equal(executor.dedupCalls, 1);+ assert.equal(executor.runningDiscovery, 0);- async commitDedup(commit) {- this.dedupCommitCalls.push(structuredClone(commit));- if (- this.failDedupCommitFromCall !== undefined &&- this.dedupCommitCalls.length >= this.failDedupCommitFromCall- ) {- if (this.replacementCandidatesBeforeDedupRejection) {- await writeFile(- this.dedupCommits.at(-1).resultManifestPath,- this.replacementCandidatesBeforeDedupRejection,- );- }- throw new DeepScanNonRetryableError(- "fixture rejected the reducer commit",- );- }- if (this.committedDedupIds.has(commit.id)) {- if (this.loseEveryDedupCommitResponseAfterCommit) {- throw new Error("fixture lost the committed reducer response");- }- return structuredClone(this.run);- }- this.dedupCommits.push(structuredClone(commit));- const consumedCount = this.dedupClaims.find(- (claim) => claim.id === commit.id,- )?.workerIds.length;- assert.equal(typeof consumedCount, "number");- if (commit.newFindings > 0) this.run.noNewStreak = 0;- else this.run.noNewStreak += consumedCount;- const consumed =- this.dedupClaims.find((claim) => claim.id === commit.id)?.workerIds ?? [];- for (const workerId of consumed) {- const discovery = this.workers.get(workerId);- assert.equal(discovery?.mergeState, "merging");- this.workers.set(workerId, { ...discovery, mergeState: "merged" });- }- await this.updateWorker({- ...this.workers.get(commit.id),- id: commit.id,- scanId: commit.scanId,- kind: "dedup",- status: "succeeded",- attempt: this.workers.get(commit.id)?.attempt ?? 1,- resultManifestPath: commit.resultManifestPath,- });- this.committedDedupIds.add(commit.id);- this.dedupCommitted.resolve();- this.dedupCommitPersisted.resolve();- if (this.loseEveryDedupCommitResponseAfterCommit) {- throw new Error("fixture lost the committed reducer response");- }- if (this.blockDedupCommitResponse)- await this.dedupCommitResponseGate.promise;- if (- this.loseFirstDedupCommitResponseAfterCommit &&- this.dedupCommitResponseLosses === 0- ) {- this.dedupCommitResponseLosses += 1;- throw new Error("fixture lost dedup commit response after commit");- }- return structuredClone(this.run);- }+ const manifest = await readJson(terminal.manifestPath);+ assert.equal(store.dedupClaims[0].workerIds.length, 1);+ assert.deepEqual(store.finishCalls[0].omittedWorkerIds, []);+ assert.equal(+ [...store.workers.values()].filter((worker) => worker.status === "canceled")+ .length,+ 1,+ );+ assert.equal(store.dedupCommits.length, 1);+ assert.deepEqual(+ manifest.findings.map(+ (finding: { provenance: { candidateId: string } }) =>+ finding.provenance.candidateId,+ ),+ ["candidate-1"],+ );+}- async finish(input) {- this.finishCalls.push(structuredClone(input));- if (this.run.status === "succeeded") return structuredClone(this.run);- if (this.failFinish) {- if (this.replacementManifestBeforeFinishRejection) {- await writeFile(- input.manifestPath,- this.replacementManifestBeforeFinishRejection,- );- }- throw new DeepScanNonRetryableError("fixture finish persistence failure");- }- if (input.stagedManifestPath)- await rename(input.stagedManifestPath, input.manifestPath);- const latestReducer = [...this.workers.values()]- .filter(- (worker) => worker.kind === "dedup" && worker.status === "succeeded",- )- .at(-1);- const draft = latestReducer?.resultManifestPath- ? JSON.parse(await readFile(latestReducer.resultManifestPath, "utf8"))- : {- scanId: this.run.scanId,- findings: [],- coverage: {- completeness: "partial",- surfaces: [],- explicitExclusions: [],- deferred: [],- },- };- await writeFile(- input.manifestPath,- JSON.stringify({- scan: {- scanId: draft.scanId,- mode: "deep",- ...(draft.threatModel ? { threatModel: draft.threatModel } : {}),- },- findings: draft.findings,- coverage: draft.coverage,- }),- );- this.run.status = "succeeded";- this.run.terminalReason = input.reason;- this.run.manifestPath = input.manifestPath;- if (- this.loseFirstFinishResponseAfterCommit &&- this.finishCalls.length === 1- ) {- throw new Error("fixture lost finish response after commit");+async function testDiscoveryAcceptedAtDeadlineIsReduced() {+ const { fixture, store } = await coordinatorFixture({+ stopAfterNoNew: 99,+ maxDiscoveryRuns: 8,+ });+ const acceptancePersisted = Promise.withResolvers<void>();+ const releaseAcceptance = Promise.withResolvers<void>();+ const discoveryDeadlineReached = Promise.withResolvers<void>();+ const updateWorker = store.updateWorker.bind(store);+ store.updateWorker = async (update) => {+ const persisted = await updateWorker(update);+ if (update.kind === "discovery" && update.status === "succeeded") {+ acceptancePersisted.resolve();+ await releaseAcceptance.promise;}- return structuredClone(this.run);- }+ return persisted;+ };+ const executor = new FakeExecutor({+ discoveryCandidateId: "candidate-1",+ canonicalCandidateId: "candidate-1",+ });+ const coordinator = createCoordinator(fixture, store, executor, {+ discoveryTimeoutMs: 500,+ log: (event) => {+ if (event.event === "discovery_deadline_reached")+ discoveryDeadlineReached.resolve();+ },+ });+ coordinator.start();+ const terminalWait = coordinator.wait(undefined, 5_000);- async fail(- _scanId,- message,- status = "failed",- manifestPath,- stagedManifestPath,- ) {- this.failureInputs.push({ message, status, manifestPath });- if (this.rejectFailurePersistence) {- throw new DeepScanNonRetryableError(- "fixture rejected stale failure persistence",- );- }- if (stagedManifestPath && manifestPath)- await rename(stagedManifestPath, manifestPath);- this.run.status = status;- this.run.error = message;- this.run.manifestPath = manifestPath;- return structuredClone(this.run);- }+ await acceptancePersisted.promise;+ await discoveryDeadlineReached.promise;+ releaseAcceptance.resolve();- async recordStoppedPublicationFailure(_scanId, message) {- this.publicationFailureMessages.push(message);- const original = this.run.error?.trim();- this.run.error = original- ? boundedFixtureErrorPair(- message,- "\nOriginal Deep Scan failure:\n",- original,- )- : message;- return structuredClone(this.run);- }+ const terminal = await terminalWait;+ assert.equal(terminal?.status, "succeeded");+ assert.equal(terminal?.terminalReason, "capped");+ assert.equal(store.failureMessages.length, 0);+ assert.equal(executor.discoveryCalls, 1);+ assert.equal(executor.dedupCalls, 1);- async updateProgress(input) {- this.progressCalls += 1;- if (this.progressCalls === this.failProgressAt) {- throw new Error("fixture progress persistence failure");- }- this.progress.push(structuredClone(input));- if (input.phase) this.run.phase = input.phase;- }+ const manifest = await readJson(terminal.manifestPath);+ assert.equal(store.dedupClaims[0].workerIds.length, 1);+ assert.deepEqual(store.finishCalls[0].omittedWorkerIds, []);+ assert.equal(+ [...store.workers.values()].some((worker) => worker.status === "canceled"),+ false,+ );+ assert.deepEqual(+ manifest.findings.map(+ (finding: { provenance: { candidateId: string } }) =>+ finding.provenance.candidateId,+ ),+ ["candidate-1"],+ );+}- releaseDiscoverySuccess() {- this.discoverySuccessGate.resolve();- }+async function testDiscoveryDeadlineWithoutAcceptedWorkersReturnsPartialEvidence() {+ const { fixture, store } = await coordinatorFixture({+ stopAfterNoNew: 99,+ maxDiscoveryRuns: 8,+ });+ const executor = new FakeExecutor({ blockDiscoveryAfterCalls: 0 });+ const completedDrafts: ScanDraftInput[] = [];+ const coordinator = createCoordinator(fixture, store, executor, {+ discoveryTimeoutMs: 500,+ onComplete: async (draft) =>+ void completedDrafts.push(structuredClone(draft)),+ });+ coordinator.start();+ await executor.discoveryStarted.promise;- releaseDiscoveryFailure() {- this.discoveryFailureGate.resolve();- }+ const terminal = await coordinator.wait(undefined, 5_000);+ assert.equal(terminal?.status, "succeeded");+ assert.equal(terminal?.terminalReason, "capped");+ assert.equal(store.failureMessages.length, 0);+ assert.equal(store.finishCalls.length, 1);+ assert.equal(executor.runningDiscovery, 0);+ assert.equal(executor.dedupCalls, 0);- releaseDedupCommitResponse() {- this.dedupCommitResponseGate.resolve();- }+ const manifest = await readJson(terminal.manifestPath);+ assert.deepEqual(manifest.findings, []);+ assert.equal(manifest.coverage.completeness, "partial");+ assert.deepEqual(completedDrafts[0].coverage.deferred, [+ {+ reason:+ "The configured discovery time limit elapsed before any source review completed.",+ },+ ]);+ assert.deepEqual(store.finishCalls[0].omittedWorkerIds, []);+ assert.equal(+ [...store.workers.values()].filter((worker) => worker.status === "canceled")+ .length,+ 1,+ );+ assert.equal(store.dedupCommits.length, 0);}-class FakeExecutor {- constructor(options = {}) {- this.options = options;- this.discoveryStarted = Promise.withResolvers();- this.dedupStarted = Promise.withResolvers();- this.dedupGate = Promise.withResolvers();- this.dedupArtifactsWritten = Promise.withResolvers();- this.discoveryArtifactsWritten = Promise.withResolvers();- }-- discoveryCalls = 0;- discoveryAttempts = new Map();- discoveryPromptPaths = new Map();- discoveryWorkingDirectories = new Set();- discoveryResumeThreadIds = [];- discoveryResumeThreadIdsByWorker = new Map();- discoveryContinuationPrompts = [];- discoveryThreadIds = [];- logicalDiscoveryWorkers = new Set();- runningDiscovery = 0;- maximumDiscoveryConcurrency = 0;- runningDedup = 0;- maximumDedupConcurrency = 0;- failedOnce = false;- invalidDiscoveryCount = 0;- invalidDedupOnce = false;- invalidDedupTraceabilityOnce = false;- dedupCalls = 0;- dedupAttemptsByLabel = new Map();- dedupResumeThreadIds = [];- dedupThreadIds = [];- dedupContinuationPrompts = [];- dedupPromptPaths = [];- dedupArtifactContexts = [];- dedupSignal = undefined;-- async run(request) {- const threadId = request.resumeThreadId ?? randomUUID();- await request.onThreadStarted?.(threadId);- if (request.kind === "discovery") {- const workerId = await workerIdFromPrompt(request.promptPath);- this.logicalDiscoveryWorkers.add(workerId);- this.discoveryWorkingDirectories.add(request.workingDirectory);- this.discoveryResumeThreadIds.push(request.resumeThreadId);- const resumeThreadIds =- this.discoveryResumeThreadIdsByWorker.get(workerId) ?? [];- resumeThreadIds.push(request.resumeThreadId);- this.discoveryResumeThreadIdsByWorker.set(workerId, resumeThreadIds);- this.discoveryContinuationPrompts.push(request.continuationPrompt);- this.discoveryThreadIds.push(threadId);- this.discoveryCalls += 1;- this.discoveryAttempts.set(- workerId,- (this.discoveryAttempts.get(workerId) ?? 0) + 1,- );- const promptPaths = this.discoveryPromptPaths.get(workerId) ?? new Set();- promptPaths.add(request.promptPath);- this.discoveryPromptPaths.set(workerId, promptPaths);- this.runningDiscovery += 1;- this.maximumDiscoveryConcurrency = Math.max(- this.maximumDiscoveryConcurrency,- this.runningDiscovery,- );- this.discoveryStarted.resolve();- try {- if (this.options.policyRefusalWorkers?.includes(workerId)) {- const message =- this.options.policyRefusalMessages?.[workerId] ??- "Request blocked by cyberPolicy.";- throw new Error(message);- }- if (this.options.transientFailureWorkers?.includes(workerId)) {- throw new Error("transient worker failure");- }- if (this.options.longDiscoveryFailure) {- throw new Error(this.options.longDiscoveryFailure);- }- if (- this.options.alwaysFailDiscovery ||- (this.options.failFirstDiscoveryAttempt && !this.failedOnce)- ) {- this.failedOnce = true;- if (this.options.writePartialBeforeFailure) {- await writeFile(- path.join(request.workingDirectory, "partial-progress.txt"),- "preserve me\n",- );- }- throw new Error("transient worker failure");- }- if (this.options.nonRetryableDiscovery) {- throw new DeepScanNonRetryableError(- this.options.nonRetryableDiscoveryMessage ??- "fixture configuration failure",- );- }- if (this.options.nonRetryableDiscoveryWorkers?.includes(workerId)) {- throw new DeepScanNonRetryableError("fixture configuration failure");- }- await this.options.discoveryGates?.[workerId];- if (this.options.failDiscoveryWorkersAfterGate?.includes(workerId)) {- throw new DeepScanNonRetryableError("fixture late discovery failure");- }- if (- this.options.blockDiscoveryAfterCalls &&- this.discoveryCalls > this.options.blockDiscoveryAfterCalls- ) {- await waitForAbort(request.signal);- }- if (this.options.blockDiscovery) await waitForAbort(request.signal);- const omitArtifact =- this.options.alwaysOmitDiscoveryArtifact ||- this.invalidDiscoveryCount <- (this.options.invalidDiscoveryAttempts ??- (this.options.omitFirstDiscoveryArtifact ? 1 : 0));- const malformedResult =- this.invalidDiscoveryCount <- (this.options.malformedDiscoveryAttempts ?? 0);- if (omitArtifact || malformedResult) this.invalidDiscoveryCount += 1;- await writeDiscoveryArtifacts(- request.promptPath,- omitArtifact ? "result.json" : undefined,- this.options.discoveryCandidates?.[workerId] ??- this.options.discoveryCandidateId ??- (this.options.dedupNewFindings?.some((count) => count > 0)- ? "candidate-1"- : undefined),- request.workingDirectory,- request.artifactContext,- );- if (malformedResult) {- await writeFile(- path.join(request.artifactContext.root, "result.json"),- "{malformed",- );- }- this.discoveryArtifactsWritten.resolve();- if (this.options.blockDiscoveryAfterWrite)- await waitForAbort(request.signal);- return {- threadId,- ...(this.options.discoveryDiagnostics- ? { diagnostics: this.options.discoveryDiagnostics }- : {}),- };- } finally {- this.runningDiscovery -= 1;- }- }-- this.runningDedup += 1;- this.dedupSignal = request.signal;- this.dedupResumeThreadIds.push(request.resumeThreadId);- this.dedupThreadIds.push(threadId);- this.dedupContinuationPrompts.push(request.continuationPrompt);- this.dedupPromptPaths.push(request.promptPath);- this.dedupArtifactContexts.push(request.artifactContext);- this.maximumDedupConcurrency = Math.max(- this.maximumDedupConcurrency,- this.runningDedup,- );- this.dedupStarted.resolve();- try {- if (this.options.blockDedup) await this.dedupGate.promise;- if (request.signal.aborted) throw abortError();- this.dedupCalls += 1;- const reducerLabel = (await promptContext(request.promptPath))- .reducerLabel;- const reducerAttempt =- (this.dedupAttemptsByLabel.get(reducerLabel) ?? 0) + 1;- this.dedupAttemptsByLabel.set(reducerLabel, reducerAttempt);- if (- reducerAttempt <=- (this.options.missingDedupResultsByLabel?.[reducerLabel] ?? 0)- ) {- return {- threadId,- ...(this.options.dedupDiagnostics- ? { diagnostics: this.options.dedupDiagnostics }- : {}),- };- }- const invalidResult =- this.options.alwaysInvalidDedupResult ||- (this.options.invalidDedupFromCall !== undefined &&- this.dedupCalls >= this.options.invalidDedupFromCall) ||- (this.options.invalidFirstDedupResult && !this.invalidDedupOnce);- this.invalidDedupOnce ||= invalidResult;- const invalidTraceability =- this.options.invalidFirstDedupTraceability &&- !this.invalidDedupTraceabilityOnce;- this.invalidDedupTraceabilityOnce ||= invalidTraceability;- await writeDedupArtifacts(request, invalidResult ? [] : undefined, {- canonicalCandidateId: this.options.canonicalCandidateId,- evidence: this.options.dedupEvidenceByCall?.[this.dedupCalls - 1],- omitLastWorkerSource: invalidTraceability,- dropLastFinding: this.options.dropLastDedupFinding,- corruptAcceptedSource: this.options.corruptAcceptedSource,- });- if (this.options.omitFirstDedupCandidateLedger && this.dedupCalls === 1) {- await rm(path.join(request.artifactContext.root, "result.json"));- }- this.dedupArtifactsWritten.resolve();- if (this.options.blockDedupAfterWrite) await waitForAbort(request.signal);- return {- threadId,- ...(this.options.dedupDiagnostics- ? { diagnostics: this.options.dedupDiagnostics }- : {}),- };- } finally {- this.runningDedup -= 1;- }- }+async function testDiscoveryDeadlineBeforeWorkerDispatchReturnsPartialEvidence() {+ const fixture = await fixtureRun({+ stopAfterNoNew: 99,+ maxDiscoveryRuns: 8,+ maxTimeHours: 1e-12,+ });+ fixture.run.createdAt = new Date(immediateClock.now()).toISOString();+ const store = new FakeStore(fixture.run);+ const executor = new FakeExecutor();+ const terminal = await runCoordinator(fixture, store, executor);+ assert.equal(terminal?.status, "succeeded");+ assert.equal(terminal?.terminalReason, "capped");+ assert.equal(store.failureMessages.length, 0);+ assert.equal(store.finishCalls.length, 1);+ assert.equal(executor.discoveryCalls, 0);+ assert.equal(executor.dedupCalls, 0);- releaseDedup() {- this.dedupGate.resolve();- }+ const manifest = await readJson(terminal.manifestPath);+ assert.deepEqual(manifest.findings, []);+ assert.equal(manifest.coverage.completeness, "partial");+ assert.deepEqual(store.finishCalls[0].omittedWorkerIds, []);+ assert.equal(store.workers.size, 0);}-async function workerIdFromPrompt(promptPath) {- return (await promptContext(promptPath)).workerLabel ?? promptPath;+async function testSaturationOmitsWorkerAcceptedDuringCancellation() {+ const { fixture, store } = await coordinatorFixture({+ workers: 3,+ stopAfterNoNew: 2,+ maxDiscoveryRuns: 3,+ });+ const releaseLateWorker = Promise.withResolvers<void>();+ const lateAcceptance = Promise.withResolvers<void>();+ const releaseAcceptance = Promise.withResolvers<void>();+ const updateWorker = store.updateWorker.bind(store);+ let acceptedLateWorker;+ store.updateWorker = async (update) => {+ const persisted = await updateWorker(update);+ if (+ update.kind === "discovery" &&+ update.status === "succeeded" &&+ path.basename(path.dirname(update.promptPath)) === "discovery-0003"+ ) {+ acceptedLateWorker = persisted;+ // This worker finishes too late to be included in the final result.+ await rm(update.resultManifestPath!);+ lateAcceptance.resolve();+ await releaseAcceptance.promise;+ }+ return persisted;+ };+ const executor = new FakeExecutor({+ discoveryGates: { "discovery-0003": releaseLateWorker.promise },+ discoveryCandidates: { "discovery-0003": "late-accepted-finding" },+ });+ executor.dedupGate = Promise.withResolvers<void>();+ const completed: ScanDraftInput[] = [];+ const coordinator = createCoordinator(fixture, store, executor, {+ onComplete: async (draft) => void completed.push(structuredClone(draft)),+ });+ coordinator.start();+ await executor.dedupStarted.promise;+ releaseLateWorker.resolve();+ await lateAcceptance.promise;+ executor.dedupGate.resolve();+ await eventually(() => executor.dedupSignal?.aborted === true);+ releaseAcceptance.resolve();+ const terminal = await coordinator.wait(undefined, 5_000);+ assert.equal(terminal?.status, "succeeded", terminal?.error);+ assert.equal(terminal.terminalReason, "saturated");+ assert.equal(executor.discoveryCalls, 3);+ assert.equal(+ executor.dedupCalls,+ 1,+ "late accepted results do not restart convergence",+ );+ assert.deepEqual(store.finishCalls[0].omittedWorkerIds, [+ acceptedLateWorker!.id,+ ]);+ assert.equal(completed.length, 1);+ assert.equal(completed[0].coverage.completeness, "complete");+ assert.deepEqual(+ completed[0].findings,+ [],+ "late worker findings are not appended to the saturated aggregate",+ );}-async function promptContext(promptPath) {- const prompt = await readFile(promptPath, "utf8");- const encoded = prompt.match(/```json\n([\s\S]*?)\n```/)?.[1];- if (!encoded) throw new Error(`Missing JSON context in ${promptPath}`);- return JSON.parse(encoded);+async function testSuccessfulDeepCoverageIgnoresWorkerAndReducerReviewStatus() {+ const { fixture, store } = await coordinatorFixture({+ workers: 2,+ stopAfterNoNew: 2,+ maxDiscoveryRuns: 2,+ });+ const executor = new FakeExecutor();+ const run = executor.run.bind(executor);+ const reviewed = { label: "Reviewed query", disposition: "no_issue_found" };+ const workerReviewed = {+ ...reviewed,+ receiptRefs: ["artifacts/missing-worker-receipt.md"],+ };+ const followUp = {+ label: "Worker follow-up",+ disposition: "needs_follow_up",+ };+ executor.run = async (request) => {+ const outcome = await run(request);+ const resultPath = path.join(request.artifactContext!.root, "result.json");+ const draft = await readJson(resultPath);+ draft.coverage = {+ completeness:+ request.kind === "discovery" &&+ request.promptPath.includes("discovery-0002")+ ? "unknown"+ : "partial",+ surfaces: [workerReviewed, followUp],+ explicitExclusions: [],+ deferred: [+ { reason: "An independent review left this question unresolved." },+ ],+ };+ await writeJson(resultPath, draft);+ return outcome;+ };+ const completed: ScanDraftInput[] = [];+ const terminal = await runCoordinator(fixture, store, executor, {+ onComplete: async (draft) => void completed.push(structuredClone(draft)),+ });+ assert.equal(terminal?.status, "succeeded", terminal?.error);+ assert.equal(completed.length, 1);+ assert.deepEqual(completed[0].coverage, {+ completeness: "complete",+ surfaces: [],+ explicitExclusions: [],+ deferred: [],+ });+ for (const worker of store.workers.values()) {+ if (worker.kind !== "discovery") continue;+ const draft = await readJson(worker.resultManifestPath!);+ assert.notEqual(draft.coverage.completeness, "complete");+ assert.deepEqual(draft.coverage.surfaces, [workerReviewed, followUp]);+ assert.equal(draft.coverage.deferred.length, 1);+ }}-async function writeDiscoveryArtifacts(- promptPath,- omittedName,- candidateId,- output,- artifactContext,-) {- const context = await promptContext(promptPath);- assert.equal(artifactContext?.layout, "worker");- assert.equal(artifactContext.root, output);- if (omittedName === "result.json") return;- const draft = standardScanDraft(- context.scanId,- candidateId,- context.workerLabel,- );- await writeFile(- path.join(artifactContext.root, "result.json"),- JSON.stringify(draft),+async function testSaturationIgnoresDiscoveryCancellationWriteFailure() {+ const { fixture, store } = await coordinatorFixture({+ workers: 2,+ stopAfterNoNew: 2,+ maxDiscoveryRuns: 6,+ });+ const executor = new FakeExecutor({+ blockDiscoveryAfterCalls: 2,+ });+ executor.dedupGate = Promise.withResolvers<void>();+ const updateWorker = store.updateWorker.bind(store);+ const rejectedCancellations = new Set();+ store.updateWorker = async (update) => {+ if (update.kind === "discovery" && update.status === "canceled") {+ assert.equal(executor.dedupSignal?.aborted, true);+ assert.equal(store.run.noNewStreak, 2);+ rejectedCancellations.add(update.id);+ throw new Error("fixture cancellation persistence failure");+ }+ return updateWorker(update);+ };+ const completed: ScanDraftInput[] = [];+ const coordinator = createCoordinator(fixture, store, executor, {+ onComplete: async (draft) => void completed.push(structuredClone(draft)),+ });+ coordinator.start();+ await executor.dedupStarted.promise;+ await eventually(+ () => executor.discoveryCalls === 4 && executor.runningDiscovery === 2,);-}+ executor.dedupGate.resolve();-async function writeDedupArtifacts(request, consumedOverride, options = {}) {- const context = await promptContext(request.promptPath);- const artifactContext = request.artifactContext;- assert.equal(artifactContext?.layout, "reducer");- const reducer = artifactContext.deepReducer;- assert.ok(reducer);- const workerIds = reducer.claimedWorkers.map((worker) => worker.id);- assert.deepEqual(context.claimedWorkerIds, workerIds);- const workerDrafts = await Promise.all(- reducer.claimedWorkers.map(async (worker) => {- const result = JSON.parse(await readFile(worker.resultPath, "utf8"));- result.findings = result.findings.map(sourceReferences(worker));- return result;- }),+ const terminal = await coordinator.wait(undefined, 5_000);+ assert.equal(+ rejectedCancellations.size,+ 2,+ "the redundant discoveries reached the failing cancellation write",);- const previousDraft = reducer.previousReducerResultPath- ? JSON.parse(await readFile(reducer.previousReducerResultPath, "utf8"))- : undefined;- const mergedFindings = new Map();- for (const finding of [- ...(previousDraft?.findings ?? []),- ...workerDrafts.flatMap((draft) => draft.findings),- ]) {- const findingIdentity =- finding.identity?.anchor ??- finding.provenance?.candidateId ??- finding.ruleId;- mergedFindings.set(findingIdentity, {- ...mergedFindings.get(findingIdentity),- ...finding,- provenance: {- ...finding.provenance,- sourceFindingIds: [- ...new Set([- ...(mergedFindings.get(findingIdentity)?.provenance- .sourceFindingIds ?? []),- ...(finding.provenance.sourceFindingIds ?? []),- ]),- ],- },- });- }- const draft = standardScanDraft(- previousDraft?.scanId ?? workerDrafts[0]?.scanId,- undefined,- context.reducerLabel,+ assert.equal(terminal?.status, "succeeded", terminal?.error);+ assert.equal(terminal.terminalReason, "saturated");+ assert.equal(store.failureMessages.length, 0);+ assert.equal(store.finishCalls.length, 1);+ assert.equal(store.finishCalls[0].reason, "saturated");+ assert.equal(+ executor.discoveryCalls,+ 4,+ "cancellation persistence failures must not dispatch replacement reviews after saturation",+ );+ assert.equal(+ executor.dedupCalls,+ 1,+ "cancellation persistence failures must not restart reduction after saturation",+ );+ assert.equal(executor.runningDiscovery, 0);+ assert.equal(completed.length, 1);+ assert.equal(completed[0].coverage.completeness, "complete");+ const acceptedReducer = [...store.workers.values()].find(+ (worker) => worker.kind === "dedup" && worker.status === "succeeded",+ );+ const { coverage, ...publishedReduction } = completed[0];+ assert.deepEqual(+ publishedReduction,+ await readJson(acceptedReducer!.resultManifestPath!),+ "the accepted aggregate still reaches publication when redundant cancellation writes fail",);- draft.findings = [...mergedFindings.values()].map((finding) => ({- ...finding,- ...(options.evidence === undefined- ? {}- : { rootCause: { summary: options.evidence } }),- }));- if (options.canonicalCandidateId && draft.findings.length > 0) {- draft.findings[0].provenance.candidateId = options.canonicalCandidateId;- }- delete draft.coverage;- if (consumedOverride || options.omitLastWorkerSource)- draft.findings = "invalid";- if (options.dropLastFinding) draft.findings.pop();- const resultPath = path.join(artifactContext.root, "result.json");- await mkdir(path.dirname(resultPath), { recursive: true });- await writeFile(resultPath, JSON.stringify(draft));- if (options.corruptAcceptedSource) {- const source = reducer.claimedWorkers.at(-1);- await writeFile(source.resultPath, "{invalid standard scan\n");- }}-function standardScanDraft(scanId, candidateId, workerLabel) {- return {- scanId,- threatModel: { summary: `Independent threat model for ${workerLabel}.` },- findings: candidateId- ? [- {- ruleId: "sql-injection.fixture",- title: "Unsafe fixture query",- summary:- "A request-controlled value reaches a security-sensitive sink.",- severity: { level: "high" },- confidence: {- level: "high",- rationale: "Source evidence establishes reachability.",- },- taxonomy: { category: "sql-injection", cwe: ["CWE-89"] },- locations: [{ path: "fixture.py", startLine: 1, endLine: 1 }],- remediation: "Use a parameterized query.",- provenance: {- source: "local_plugin",- candidateId,- workerId: workerLabel,- },- },- ]- : [],- coverage: {- completeness: "complete",- surfaces: [- {- label: "Fixture query",- disposition: candidateId ? "reported" : "no_issue_found",- },- ],- explicitExclusions: [],- deferred: [],- },+async function testPublicationUsesAcceptedReducerSnapshot() {+ const { fixture, store } = await coordinatorFixture();+ const commitDedup = store.commitDedup.bind(store);+ store.commitDedup = async (commit) => {+ const accepted = await commitDedup(commit);+ await rm(commit.resultManifestPath);+ return accepted;};+ store.finish = async (input) => {+ store.finishCalls.push(input);+ Object.assign(store.run, {+ status: "succeeded",+ terminalReason: input.reason,+ manifestPath: input.manifestPath,+ });+ return structuredClone(store.run);+ };+ const completed: ScanDraftInput[] = [];+ const terminal = await runCoordinator(+ fixture,+ store,+ new FakeExecutor({ discoveryCandidateId: "accepted-finding" }),+ {+ onComplete: async (draft) => void completed.push(structuredClone(draft)),+ },+ );+ assert.equal(terminal?.status, "succeeded", terminal?.error);+ assert.equal(+ (completed[0].findings[0].provenance as { candidateId: string })+ .candidateId,+ "accepted-finding",+ );+ assert.equal(completed[0].coverage.completeness, "complete");}-async function waitForAbort(signal) {- if (signal.aborted) throw abortError();- await once(signal, "abort");- throw abortError();+async function testResumeRequiresHistoricalWorkerPrompt(+ status: "failed" | "canceled",+) {+ const fixture = await fixtureRun({+ stopAfterNoNew: 2,+ maxDiscoveryRuns: 2,+ });+ const promptPath = path.join(fixture.run.scanDir, "missing-prompt.md");+ const store = new FakeStore({+ ...fixture.run,+ persistedWorkers: [+ {+ id: "historical-worker",+ kind: "discovery",+ status,+ attempt: 1,+ promptPath,+ artifactDir: fixture.run.scanDir,+ } as PersistedDeepScanWorker,+ ],+ });+ const executor = new FakeExecutor();+ const coordinator = new DeepScanCoordinator({+ 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));+ assert.equal(executor.discoveryCalls, 0);}-function abortError() {- const error = new Error("aborted");- error.name = "AbortError";- return error;-}+async function testRecoverableWorkerErrorsCannotFailScan() {+ const failures = ["config unknown", "authentication required"].map(+ (output) =>+ new Error(+ `Failed to parse item: ${JSON.stringify({+ type: "item.completed",+ item: {+ id: "fixture-command",+ type: "command_execution",+ command: "synthetic-command",+ aggregated_output: output,+ },+ })}`,+ ),+ );+ failures.push(+ ...["ENOENT", "EACCES", "ENOEXEC", "EPERM"].map((code) =>+ Object.assign(new Error("fixture worker I/O failure"), { code }),+ ),+ );+ for (const failure of failures) {+ const { fixture, store } = await coordinatorFixture({+ workers: 2,+ stopAfterNoNew: 4,+ stopAfterConsecutiveErrors: 2,+ maxDiscoveryRuns: 4,+ });+ const normalExecutor = new FakeExecutor();+ const attempts: (string | undefined)[] = [];+ const events: DeepScanLogEvent[] = [];+ const terminal = await runCoordinator(+ fixture,+ store,+ {+ async run(request) {+ if (+ request.kind === "discovery" &&+ (await workerIdFromPrompt(request.promptPath)) === "discovery-0001"+ ) {+ attempts.push(request.resumeThreadId);+ await request.onThreadStarted?.(+ request.resumeThreadId ?? "fixture-parse-thread",+ );+ throw classifyCodexWorkerError(failure);+ }+ return normalExecutor.run(request);+ },+ },+ { retryDelaysMs: [1, 3, 9], log: (event) => events.push(event) },+ );-async function eventually(predicate) {- const deadline = Date.now() + 2_000;- while (Date.now() < deadline) {- if (predicate()) return;- await new Promise((resolve) => setTimeout(resolve, 5));+ assert.equal(terminal?.status, "succeeded", terminal?.error);+ assert.deepEqual(attempts, [+ undefined,+ "fixture-parse-thread",+ "fixture-parse-thread",+ "fixture-parse-thread",+ ]);+ const discoveries = [...store.workers.values()].filter(+ (worker) => worker.kind === "discovery",+ );+ assert.equal(+ discoveries.filter((worker) => worker.status === "succeeded").length,+ 3,+ "the sibling and subsequent discoveries must finish",+ );+ assert.equal(+ discoveries.filter((worker) => worker.status === "failed").length,+ 0,+ );+ assert.equal(+ discoveries.find((worker) => worker.status === "canceled")+ ?.replaceableFailureKind,+ "transient_error",+ );+ assert.equal(+ events.filter((event) => event.event === "discovery_worker_replaced")+ .length,+ 1,+ );+ assert.equal(store.failureMessages.length, 0);}- assert.fail("condition did not become true");}-async function assertFailureManifest(terminal, _phase) {- assert.equal(terminal?.status, "failed");- assert.equal(terminal.manifestPath, undefined);-}+async function testPolicyRefusedReducerPreservesInputsAndCommittedAggregate() {+ const { fixture, store } = await coordinatorFixture({+ workers: 2,+ stopAfterNoNew: 10,+ stopAfterConsecutiveErrors: 2,+ maxDiscoveryRuns: 4,+ });+ const nextDiscovery = Promise.withResolvers<void>();+ const siblingDiscovery = Promise.withResolvers<void>();+ const normalExecutor = new FakeExecutor({+ discoveryCandidateId: "candidate-1",+ canonicalCandidateId: "candidate-1",+ discoveryGates: {+ "discovery-0003": nextDiscovery.promise,+ "discovery-0004": siblingDiscovery.promise,+ },+ });+ let committedResultPath!: string;+ let committedContent!: Buffer;+ const failedAttempts: (string | undefined)[] = [];+ const coordinator = createCoordinator(+ fixture,+ store,+ {+ async run(request) {+ if (request.kind === "dedup") {+ const label = (await promptContext(request.promptPath)).reducerLabel;+ if (label === "dedup-0002") {+ failedAttempts.push(request.resumeThreadId);+ await request.onThreadStarted?.("fixture-retired-reducer");+ throw classifyCodexWorkerError(+ new Error("Request blocked by cyberPolicy."),+ );+ }+ if (label === "dedup-0003") {+ assert.equal(request.resumeThreadId, undefined);+ assert.equal(+ request.artifactContext!.deepReducer!.previousReducerResultPath,+ committedResultPath,+ );+ assert.deepEqual(+ store.dedupClaims[2].workerIds,+ store.dedupClaims[1].workerIds,+ "the replacement must receive every uncommitted input",+ );+ assert.deepEqual(+ await readFile(committedResultPath),+ committedContent,+ );+ siblingDiscovery.resolve();+ }+ }+ return normalExecutor.run(request);+ },+ },+ { retryDelaysMs: [1, 3, 9] },+ );+ coordinator.start();+ await store.dedupCommitted.promise;+ committedResultPath = store.dedupCommits[0].resultManifestPath;+ committedContent = await readFile(committedResultPath);+ nextDiscovery.resolve();-async function assertNoPublishedCandidates(scanDir) {- await assert.rejects(- readFile(path.join(scanDir, "scan-manifest.json"), "utf8"),- (error) => error.code === "ENOENT",+ const terminal = await coordinator.wait(undefined, 5_000);++ assert.equal(terminal?.status, "succeeded", terminal?.error);+ assert.deepEqual(failedAttempts, [undefined]);+ const discoveries = [...store.workers.values()].filter(+ (worker) => worker.kind === "discovery",+ );+ assert.equal(discoveries.length, 4);+ assert.equal(+ discoveries.every((worker) => worker.status === "succeeded"),+ true,+ );+ const retiredReducer = [...store.workers.values()].find(+ (worker) => worker.kind === "dedup" && worker.status === "failed",);+ assert.equal(retiredReducer?.attempt, 1);+ assert.deepEqual(await readFile(committedResultPath), committedContent);+ const manifest = await readJson(terminal.manifestPath);+ assert.deepEqual(+ manifest.findings.map(+ (finding: { provenance: { candidateId: string } }) =>+ finding.provenance.candidateId,+ ),+ ["candidate-1"],+ );+ assert.equal(store.failureMessages.length, 0);}-const immediateClock = {- now: () => 1_700_000_000_000,- sleep: async (_delayMs, signal) => {- if (signal.aborted) throw abortError();- },-};--function boundedFixtureErrorPair(primary, separator, secondary) {- const available = 2_400 - separator.length;- let primaryBudget = Math.min(primary.length, Math.floor(available / 2));- const secondaryBudget = Math.min(secondary.length, available - primaryBudget);- primaryBudget = Math.min(primary.length, available - secondaryBudget);- return (- boundedFixtureErrorText(primary, primaryBudget) +- separator +- boundedFixtureErrorText(secondary, secondaryBudget)+async function testNonRetryableReducerAbortsScanWithoutRetry(+ failureMessage = "fixture fatal reducer failure",+) {+ const { fixture, store } = await coordinatorFixture({+ workers: 2,+ stopAfterNoNew: 10,+ stopAfterConsecutiveErrors: 2,+ maxDiscoveryRuns: 4,+ });+ const normalExecutor = new FakeExecutor({ blockDiscoveryAfterCalls: 2 });+ const attempts: (string | undefined)[] = [];+ const sleeps: number[] = [];+ const terminal = await runCoordinator(+ fixture,+ store,+ {+ async run(request) {+ if (request.kind === "dedup") {+ attempts.push(request.resumeThreadId);+ throw new DeepScanNonRetryableError(failureMessage);+ }+ return normalExecutor.run(request);+ },+ },+ {+ clock: recordingClock(sleeps),+ },);-}-function boundedFixtureErrorText(message, maximum) {- if (message.length <= maximum) return message;- const digest = createHash("sha256").update(message).digest("hex");- const suffix = `\n...[truncated; sha256:${digest}]`;- if (suffix.length >= maximum) return message.slice(0, maximum);- return message.slice(0, maximum - suffix.length) + suffix;+ assert.equal(terminal?.status, "failed");+ assert.equal(terminal?.error, failureMessage);+ assert.deepEqual(attempts, [undefined]);+ assert.deepEqual(sleeps, []);+ assert.equal(normalExecutor.runningDiscovery, 0);+ assert.equal(store.failureMessages.length, 1);+ assert.equal(store.dedupCommits.length, 0);}-const {- testResumeRequiresHistoricalWorkerPrompt,- testRecoverableWorkerErrorsCannotFailScan,- testPolicyRefusedReducerPreservesInputsAndCommittedAggregate,- testNonRetryableReducerAbortsScanWithoutRetry,-} = createDeepScanWorkerFailureCases({- fixtureRun,- FakeStore,- FakeExecutor,- createCoordinator,- DeepScanCoordinator,- DeepScanNonRetryableError,- classifyCodexWorkerError,- immediateClock,- recordingClock,- workerIdFromPrompt,- promptContext,-});-try {await testCappedQueueAndSerialDedup();await testStandardWorkersReceiveExistingFalsePositiveFeedback();@@ -4297,32 +3741,24 @@ try {await testSandboxDiagnosticSurvivesArtifactRetries();await testCompletionOrdering();await testSaturationPreservesFindingAlreadyBuffered();- await testDeepScanPublication({- fixtureRun,- FakeStore,- FakeExecutor,- createCoordinator,- eventually,- });+ await testSaturationOmitsWorkerAcceptedDuringCancellation();+ await testSuccessfulDeepCoverageIgnoresWorkerAndReducerReviewStatus();+ await testSaturationIgnoresDiscoveryCancellationWriteFailure();+ await testPublicationUsesAcceptedReducerSnapshot();await testDirectReducerCannotDropAcceptedFinding();await testSaturationDrainsBufferedAndCancelsInflight();- await testDeepScanDeadlines({- fixtureRun,- FakeStore,- FakeExecutor,- createCoordinator,- immediateClock,- eventually,- });+ await testDiscoveryDeadlineDrainsActiveReducerAndPreservesFindings();+ await testDiscoveryDeadlineReducesSingleBufferedFinding();+ await testDiscoveryAcceptedAtDeadlineIsReduced();+ await testDiscoveryDeadlineWithoutAcceptedWorkersReturnsPartialEvidence();+ await testDiscoveryDeadlineBeforeWorkerDispatchReturnsPartialEvidence();await testSaturationIgnoresWorkerFailureSettledAfterStop();await testSettledReducerIsNotStarvedByDiscoveryBacklog();await testSingletonHardCapReduction();await testExhaustedRetryFailsScan();- await testCybersecurityRefusalReplacesOnlyRefusedDiscovery();await testProviderCybersecurityRiskMessagesReplaceRefusedDiscoveryImmediately();- await testRateLimitAndUnrelatedRefusalsRetainTransientRecovery();+ await testTransientDiscoveryErrorsRetainRecovery();await testConsecutiveCybersecurityRefusalsFailAtConfiguredThreshold();- await testExhaustedTransientDiscoveryIsReplaced();await testRecoverableWorkerErrorsCannotFailScan();await testSuccessfulDiscoveryResetsConsecutiveFailureThreshold();await testExhaustedInvalidDiscoveryArtifactsAreReplaced();@@ -4393,14 +3829,3 @@ try {} finally {await temporaryDirectories.cleanup();}--function createCoordinator(fixture, store, executor, options) {- return new DeepScanCoordinator({- run: fixture.run,- store,- executor,- pluginRoot: fixture.pluginRoot,- clock: immediateClock,- ...options,- });-}diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.tssimilarity index 62%rename from plugins/codex-security/mcp-app/tests/test_deep_scan_executor.mjsrename to plugins/codex-security/mcp-app/tests/test_deep_scan_executor.tsindex 345591775..4c05e78a5 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.mjs+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts@@ -1,8 +1,13 @@-import { assertFlagPair } from "./assertions.mjs";-import { createTemporaryDirectories } from "./support/temporary-directories.mjs";+import { readJson, writeJson } from "./support/json.ts";+import { assertFlagPair } from "./assertions.ts";+import { createTemporaryDirectories } from "./support/temporary-directories.ts";import { mock } from "node:test";import assert from "node:assert/strict";-import childProcess, { spawnSync } from "node:child_process";+import childProcess, {+ spawnSync,+ type SpawnOptions,+ type ChildProcess,+} from "node:child_process";import {chmod,copyFile,@@ -16,7 +21,12 @@ import {import { syncBuiltinESMExports } from "node:module";import path from "node:path";import { fileURLToPath } from "node:url";-import { importModule, importSource } from "./import-module.mjs";+import { importModule } from "./import-module.ts";++import type {+ CodexWorkerDiagnostic,+ DeepScanWorkerKind,+} from "../src/deep-scan/types.js";const executorSource = new URL("../src/deep-scan/executor.ts", import.meta.url);@@ -25,30 +35,24 @@ const {resolveCodexPath,snapshotWorkerEnvironment,appendSafeItemDiagnostic,+ classifyCodexWorkerError,+ DeepScanNonRetryableError,+ isCodexCybersecurityPolicyRefusal,} = await importModule({define: {"import.meta.url": JSON.stringify(executorSource.href),},stdin: {// Test the environment snapshot without adding a production export.- contents: [- await readFile(executorSource, "utf8"),- "export { snapshotWorkerEnvironment, appendSafeItemDiagnostic };",- 'export * from "./errors.js";',- ].join("\n"),+ contents: `${await readFile(executorSource, "utf8")}+export { snapshotWorkerEnvironment, appendSafeItemDiagnostic };+export * from "./errors.js";`,loader: "ts",resolveDir: path.dirname(fileURLToPath(executorSource)),sourcefile: fileURLToPath(executorSource),},});-const {- classifyCodexWorkerError,- DeepScanNonRetryableError,- isCodexCybersecurityPolicyRefusal,-} = await importSource(- fileURLToPath(new URL("../src/deep-scan/errors.ts", import.meta.url)),-);const temporaryDirectories = createTemporaryDirectories();const previousMarker = process.env.FAKE_CODEX_MARKER;const trustedParentSandbox = Object.freeze({@@ -295,11 +299,9 @@ async function testWindowsRootRelativePathsStayBoundToOriginalDrive() {"codex-security-windows-home-",);const target = path.join(root, "target", "nested");- await Promise.all([- mkdir(target, { recursive: true }),- mkdir(path.join(root, "target", "home"), { recursive: true }),- mkdir(path.join(root, "home")),- ]);+ await mkdir(target, { recursive: true });+ await mkdir(path.join(root, "target", "home"), { recursive: true });+ await mkdir(path.join(root, "home"));await symlink(target, path.join(root, "link"), "junction");const previousCwd = process.cwd();@@ -317,10 +319,8 @@ async function testWindowsRootRelativePathsStayBoundToOriginalDrive() {process.execPath,["-e",- [- "const { realpathSync } = require('node:fs');",- "process.stdout.write(JSON.stringify({ cwd: process.cwd(), codexHome: process.env.CODEX_HOME, resolvedHome: realpathSync(process.env.CODEX_HOME) }));",- ].join("\n"),+ `const { realpathSync } = require('node:fs');+process.stdout.write(JSON.stringify({ cwd: process.cwd(), codexHome: process.env.CODEX_HOME, resolvedHome: realpathSync(process.env.CODEX_HOME) }));`,],{ encoding: "utf8", env: environment, cwd: childCwd },);@@ -347,20 +347,22 @@ async function testWindowsLongExecutableLaunches() {const promptPath = path.join(fixture.root, "prompt.md");const workingDirectory = path.join(fixture.root, "artifacts");const codexHome = path.join(fixture.root, "codex-home");- await Promise.all([- mkdir(path.dirname(executable), { recursive: true }),- mkdir(workingDirectory),- mkdir(codexHome),- writeFile(promptPath, "fixture long executable worker\n"),- ]);+ await mkdir(path.dirname(executable), { recursive: true });+ await mkdir(workingDirectory);+ await mkdir(codexHome);+ await writeFile(promptPath, "fixture long executable worker\n");await copyFile(process.execPath, executable);assert.ok(executable.length > 260);const previousCodexPath = process.env.CODEX_CLI_PATH;const previousCodexHome = process.env.CODEX_HOME;const originalSpawn = childProcess.spawn;- const launchedCommands = [];- const children = [];- childProcess.spawn = (command, args, options) => {+ const launchedCommands: string[] = [];+ const children: ChildProcess[] = [];+ childProcess.spawn = ((+ command: string,+ args: readonly string[],+ options: SpawnOptions,+ ) => {if (command !== executable &&command !== path.toNamespacedPath(executable)@@ -376,7 +378,7 @@ async function testWindowsLongExecutableLaunches() {);children.push(child);return child;- };+ }) as typeof childProcess.spawn;syncBuiltinESMExports();try {process.env.CODEX_HOME = codexHome;@@ -388,20 +390,24 @@ async function testWindowsLongExecutableLaunches() {);const result = await new CodexSdkWorkerExecutor({parentSandbox: trustedParentSandbox,- }).run(discoveryRequest(promptPath, workingDirectory, 0));+ }).run({+ kind: "discovery",+ promptPath,+ workingDirectory,+ subagents: 0,+ signal: new AbortController().signal,+ });assert.equal(result.threadId, "fixture-thread-id");- const invocation = JSON.parse(await readFile(fixture.markerPath, "utf8"));+ const invocation = await readJson(fixture.markerPath);assert.deepEqual(invocation.argv.slice(0, 2), ["exec","--experimental-json",]);assertReadOnlyWorkerPolicy(invocation.argv);assert.equal(invocation.codexHome, await realpath(codexHome));- const preflight = JSON.parse(- await readFile(fixture.preflightMarkerPath, "utf8"),- );+ const preflight = await readJson(fixture.preflightMarkerPath);assert.deepEqual(- preflight.requests.map((request) => request.method),+ preflight.requests.map((request: { method: string }) => request.method),["config/read", "permissionProfile/list"],);assert.equal(process.env.CODEX_CLI_PATH, configured);@@ -417,15 +423,14 @@ async function testWindowsLongExecutableLaunches() {await Promise.all(children.map((child) => {if (- child.stdout.closed &&- child.stderr.closed &&+ child.stdout!.closed &&+ child.stderr!.closed &&(child.exitCode !== null || child.signalCode !== null))return;return new Promise((resolve) => {child.once("close", resolve);- if (child.exitCode === null && child.signalCode === null)- child.kill();+ child.kill();});}),);@@ -436,25 +441,17 @@ async function testWindowsLongExecutableLaunches() {async function testWindowsWorkerEnvironmentPreservesMixedCaseKeys() {const root = await temporaryDirectories.create("codex-security-windows-env-");- const names = [- "CODEX_CLI_PATH",- "CODEX_HOME",- "CODEX_MANAGED_PACKAGE_ROOT",- "LOCALAPPDATA",- ];- const previousEnvironment = Object.fromEntries(- Object.entries(process.env).filter(([key]) =>- names.includes(key.toUpperCase()),- ),- );const values = {CODEX_CLI_PATH: path.join(root, "custom-codex.exe"),CODEX_HOME: root,CODEX_MANAGED_PACKAGE_ROOT: path.join(root, "managed-package"),LOCALAPPDATA: path.join(root, "local-app-data"),};+ const previousEnvironment = Object.entries(process.env).filter(([key]) =>+ Object.hasOwn(values, key.toUpperCase()),+ );try {- for (const name of names) delete process.env[name];+ for (const name of Object.keys(values)) delete process.env[name];for (const [name, value] of Object.entries(values))process.env[name.toLowerCase()] = value;@@ -469,8 +466,8 @@ async function testWindowsWorkerEnvironmentPreservesMixedCaseKeys() {}assert.equal(resolveCodexPath(environment, "win32"), values.CODEX_CLI_PATH);} finally {- for (const name of names) delete process.env[name];- Object.assign(process.env, previousEnvironment);+ for (const name of Object.keys(values)) delete process.env[name];+ for (const [name, value] of previousEnvironment) process.env[name] = value;}}@@ -533,35 +530,27 @@ async function testWindowsAppsCodexFallsBackToRelocatedBinary() {"bin","codex.exe",);- await Promise.all([- mkdir(path.dirname(olderBinary), { recursive: true }),- mkdir(path.dirname(currentBinary), { recursive: true }),- mkdir(path.dirname(emptyBinary), { recursive: true }),- mkdir(path.dirname(protectedPackageBinary), { recursive: true }),- ]);- await Promise.all([- copyFile(process.execPath, olderBinary),- copyFile(process.execPath, currentBinary),- writeFile(emptyBinary, ""),- writeFile(- path.join(protectedDirectory, "codex.exe"),- "protected direct binary",- ),- writeFile(- path.join(managedPackage, "package.json"),- JSON.stringify({ name: "@openai/codex" }),- ),- writeFile(- path.join(platformPackage, "package.json"),- JSON.stringify({ name: `@openai/codex-win32-${architecture}` }),- ),- writeFile(protectedPackageBinary, "protected package binary"),- ]);- await Promise.all([- utimes(olderBinary, new Date(1_000), new Date(1_000)),- utimes(currentBinary, new Date(2_000), new Date(2_000)),- utimes(emptyBinary, new Date(3_000), new Date(3_000)),- ]);+ await mkdir(path.dirname(olderBinary), { recursive: true });+ await mkdir(path.dirname(currentBinary), { recursive: true });+ await mkdir(path.dirname(emptyBinary), { recursive: true });+ await mkdir(path.dirname(protectedPackageBinary), { recursive: true });+ await copyFile(process.execPath, olderBinary);+ await copyFile(process.execPath, currentBinary);+ await writeFile(emptyBinary, "");+ await writeFile(+ path.join(protectedDirectory, "codex.exe"),+ "protected direct binary",+ );+ await writeJson(path.join(managedPackage, "package.json"), {+ name: "@openai/codex",+ });+ await writeJson(path.join(platformPackage, "package.json"), {+ name: `@openai/codex-win32-${architecture}`,+ });+ await writeFile(protectedPackageBinary, "protected package binary");+ await utimes(olderBinary, new Date(1_000), new Date(1_000));+ await utimes(currentBinary, new Date(2_000), new Date(2_000));+ await utimes(emptyBinary, new Date(3_000), new Date(3_000));const resolved = resolveCodexPath({@@ -643,7 +632,12 @@ async function testWindowsLauncherSkipsExtensionlessNpmShim() {encoding: "utf8",env: brokenEnvironment,});- assert.equal(["ENOENT", "EPERM"].includes(broken.error?.code), true);+ assert.equal(+ ["ENOENT", "EPERM"].includes(+ (broken.error as NodeJS.ErrnoException | undefined)?.code!,+ ),+ true,+ );const environment = windowsLauncherEnvironment(shimDirectory,@@ -691,22 +685,16 @@ async function testWindowsNpmPackageResolution(installation = "global") {"bin","codex.exe",);- await Promise.all([- mkdir(path.dirname(nativeBinary), { recursive: true }),- mkdir(shimDirectory, { recursive: true }),- ]);- await Promise.all([- writeFile(path.join(shimDirectory, "codex"), "#!/bin/sh\nexit 1\n"),- writeFile(- path.join(codexPackage, "package.json"),- JSON.stringify({ name: "@openai/codex" }),- ),- writeFile(- path.join(platformPackage, "package.json"),- JSON.stringify({ name: `@openai/codex-win32-${architecture}` }),- ),- copyFile(process.execPath, nativeBinary),- ]);+ await mkdir(path.dirname(nativeBinary), { recursive: true });+ await mkdir(shimDirectory, { recursive: true });+ await writeFile(path.join(shimDirectory, "codex"), "#!/bin/sh\nexit 1\n");+ await writeJson(path.join(codexPackage, "package.json"), {+ name: "@openai/codex",+ });+ await writeJson(path.join(platformPackage, "package.json"), {+ name: `@openai/codex-win32-${architecture}`,+ });+ await copyFile(process.execPath, nativeBinary);const environment = windowsLauncherEnvironment(shimDirectory);if (installation === "managed") {@@ -717,7 +705,7 @@ async function testWindowsNpmPackageResolution(installation = "global") {await realpath(nativeBinary),);if (installation === "managed") {- const mixedCaseEnvironment = {+ const mixedCaseEnvironment: NodeJS.ProcessEnv = {...environment,codex_managed_package_root: codexPackage,};@@ -731,10 +719,12 @@ async function testWindowsNpmPackageResolution(installation = "global") {}if (process.platform === "win32") {assert.equal(- spawnSync("codex.exe", ["--version"], {- encoding: "utf8",- env: environment,- }).error?.code,+ (+ spawnSync("codex.exe", ["--version"], {+ encoding: "utf8",+ env: environment,+ }).error as NodeJS.ErrnoException | undefined+ )?.code,"ENOENT",);@@ -752,7 +742,7 @@ async function testWindowsNpmPackageResolution(installation = "global") {}}-function windowsLauncherEnvironment(...directories) {+function windowsLauncherEnvironment(...directories: string[]) {const environment = { ...process.env };for (const key of Object.keys(environment)) {if (key.toLowerCase() === "path") {@@ -777,7 +767,10 @@ async function testWorkerLaunchesWithoutGlobalCodex() {encoding: "utf8",env: process.env,});- assert.equal(globalCodex.error?.code, "ENOENT");+ assert.equal(+ (globalCodex.error as NodeJS.ErrnoException | undefined)?.code,+ "ENOENT",+ );const promptPath = path.join(fixture.root, "prompt.md");const workingDirectory = path.join(fixture.root, "artifacts");@@ -786,10 +779,16 @@ async function testWorkerLaunchesWithoutGlobalCodex() {const result = await new CodexSdkWorkerExecutor({parentSandbox: trustedParentSandbox,- }).run(discoveryRequest(promptPath, workingDirectory, 0));+ }).run({+ kind: "discovery",+ promptPath,+ workingDirectory,+ subagents: 0,+ signal: new AbortController().signal,+ });assert.equal(result.threadId, "fixture-thread-id");- const invocation = JSON.parse(await readFile(fixture.markerPath, "utf8"));+ const invocation = await readJson(fixture.markerPath);assert.deepEqual(invocation.argv.slice(0, 2), ["exec","--experimental-json",@@ -813,14 +812,12 @@ async function testPreflightBindsExecutableAndHomeBeforeChangingCwd() {const homeTargetChild = path.join(homeTargetRoot, "child");const codexHome = path.join(homeTargetRoot, "home");const homeLink = path.join(fixture.root, "home-link");- await Promise.all([- mkdir(workingDirectory),- mkdir(nonExecutableDirectory),- mkdir(path.join(directoryShadow, "codex"), { recursive: true }),- mkdir(binaryDirectory),- mkdir(homeTargetChild, { recursive: true }),- mkdir(codexHome, { recursive: true }),- ]);+ await mkdir(workingDirectory);+ await mkdir(nonExecutableDirectory);+ await mkdir(path.join(directoryShadow, "codex"), { recursive: true });+ await mkdir(binaryDirectory);+ await mkdir(homeTargetChild, { recursive: true });+ await mkdir(codexHome, { recursive: true });assert.notEqual(workingDirectory, originalCwd);await writeFile(promptPath, "fixture bound worker executable and home\n");await writeFile(@@ -854,19 +851,23 @@ async function testPreflightBindsExecutableAndHomeBeforeChangingCwd() {],[undefined, codexPath],["codex", codexPath],- ]) {+ ] as const) {if (configured === undefined) delete process.env.CODEX_CLI_PATH;else process.env.CODEX_CLI_PATH = configured;assert.equal(resolveCodexPath(), expectedExecutable);const result = await new CodexSdkWorkerExecutor({parentSandbox: trustedParentSandbox,- }).run(discoveryRequest(promptPath, workingDirectory, 0));+ }).run({+ kind: "discovery",+ promptPath,+ workingDirectory,+ subagents: 0,+ signal: new AbortController().signal,+ });assert.equal(result.threadId, "fixture-thread-id");- const preflight = JSON.parse(- await readFile(fixture.preflightMarkerPath, "utf8"),- );- const invocation = JSON.parse(await readFile(fixture.markerPath, "utf8"));+ const preflight = await readJson(fixture.preflightMarkerPath);+ const invocation = await readJson(fixture.markerPath);assert.equal(preflight.cwd, await realpath(workingDirectory));assert.equal(invocation.cwd, originalCwd);assert.equal(preflight.codexHome, expectedHome);@@ -891,13 +892,7 @@ async function testPreflightBindsExecutableAndHomeBeforeChangingCwd() {}async function testSdkInvocationAndThreadCapture() {- const fixture = await fakeCodexFixture(deniedWorkerPermissionProfile);- const previousPath = process.env.CODEX_CLI_PATH;- process.env.CODEX_CLI_PATH = fixture.executablePath;- try {- const promptPath = path.join(fixture.root, "prompt.md");- const workingDirectory = path.join(fixture.root, "artifacts");- await mkdir(workingDirectory);+ return withWorkerFixture(async (fixture, promptPath, workingDirectory) => {assert.equal(workingDirectory.startsWith("/repo/"), false);await writeFile(promptPath, "fixture worker prompt\n");const onThreadId = mock.fn();@@ -906,13 +901,17 @@ async function testSdkInvocationAndThreadCapture() {reasoningEffort: "xhigh",parentSandbox: trustedParentSandboxWithDenials,}).run({- ...discoveryRequest(promptPath, workingDirectory, 3),+ kind: "discovery",+ promptPath,+ workingDirectory,+ subagents: 3,+ signal: new AbortController().signal,onThreadStarted: onThreadId,});assert.equal(result.threadId, "fixture-thread-id");const callbackThreadId = onThreadId.mock.calls.at(-1)?.arguments[0];assert.equal(callbackThreadId, "fixture-thread-id");- const invocation = JSON.parse(await readFile(fixture.markerPath, "utf8"));+ const invocation = await readJson(fixture.markerPath);assert.equal(invocation.stdin, "fixture worker prompt\n");assert.equal(invocation.originator,@@ -936,9 +935,7 @@ async function testSdkInvocationAndThreadCapture() {assertWorkerSubagentPolicy(invocation.argv, 3);assertFlagPair(invocation.argv, "--cd", workingDirectory);assert.equal(invocation.argv.includes("--skip-git-repo-check"), true);- } finally {- restoreEnv("CODEX_CLI_PATH", previousPath);- }+ }, deniedWorkerPermissionProfile);}async function testOpenAiCredentialsReachWorker() {@@ -977,18 +974,23 @@ async function testOpenAiCredentialsReachWorker() {true,entry.accountResult ?? noAccount,);- const previousEnvironment = Object.fromEntries(- ["OPENAI_API_KEY", "CODEX_API_KEY", "CODEX_CLI_PATH", "CODEX_HOME"].map(- (name) => [name, process.env[name]],- ),- );+ const previousEnvironment = [+ "OPENAI_API_KEY",+ "CODEX_API_KEY",+ "CODEX_CLI_PATH",+ "CODEX_HOME",+ ].map((name) => [name, process.env[name]] as const);const originalSpawn = childProcess.spawn;try {restoreEnv("OPENAI_API_KEY", entry.openai);restoreEnv("CODEX_API_KEY", entry.codex);process.env.CODEX_CLI_PATH = process.execPath;process.env.CODEX_HOME = fixture.root;- childProcess.spawn = (command, args, options) =>+ childProcess.spawn = ((+ command: string,+ args: readonly string[],+ options: SpawnOptions,+ ) =>originalSpawn(command,command === process.execPath ||@@ -996,14 +998,14 @@ async function testOpenAiCredentialsReachWorker() {? [fixture.executablePath, ...args]: args,options,- );+ )) as typeof childProcess.spawn;syncBuiltinESMExports();const promptPath = path.join(fixture.root, "prompt.md");await writeFile(promptPath, "CAPTURE_SYNTHETIC_OPENAI_AUTH\n");const executor = new CodexSdkWorkerExecutor({parentSandbox: trustedParentSandbox,});- for (const kind of ["discovery", "dedup"]) {+ for (const kind of ["discovery", "dedup"] as const) {for (const resumeThreadId of [undefined, "fixture-resume"]) {await executor.run({kind,@@ -1013,12 +1015,8 @@ async function testOpenAiCredentialsReachWorker() {resumeThreadId,signal: new AbortController().signal,});- const preflight = JSON.parse(- await readFile(fixture.preflightMarkerPath, "utf8"),- );- const invocation = JSON.parse(- await readFile(fixture.markerPath, "utf8"),- );+ const preflight = await readJson(fixture.preflightMarkerPath);+ const invocation = await readJson(fixture.markerPath);assert.equal(preflight.codexHome, fixture.root);assert.equal(invocation.codexHome, fixture.root);assert.equal(@@ -1032,7 +1030,7 @@ async function testOpenAiCredentialsReachWorker() {assert.equal(process.env.CODEX_API_KEY, entry.codex);assert.equal(process.env.OPENAI_API_KEY, entry.openai);assert.equal(- invocation.argv.some((arg) => arg.includes("synthetic-")),+ invocation.argv.some((arg: string) => arg.includes("synthetic-")),false,);}@@ -1040,8 +1038,7 @@ async function testOpenAiCredentialsReachWorker() {} finally {childProcess.spawn = originalSpawn;syncBuiltinESMExports();- for (const [name, value] of Object.entries(previousEnvironment))- restoreEnv(name, value);+ for (const [name, value] of previousEnvironment) restoreEnv(name, value);}}}@@ -1060,22 +1057,20 @@ async function testWorkerRuntimeSettings() {"none",],];- const saved = Object.fromEntries(- [- "PYTHON",- "PATH",- "CODEX_SECURITY_GIT",- "GIT_SSH_COMMAND",- "GIT_CONFIG_GLOBAL",- "CODEX_CLI_PATH",- "CODEX_HOME",- "CODEX_SECURITY_CONFIG_PATH",- "CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH",- "OPENAI_API_KEY",- "CODEX_API_KEY",- "SYNTHETIC_GATEWAY_KEY",- ].map((name) => [name, process.env[name]]),- );+ const saved = [+ "PYTHON",+ "PATH",+ "CODEX_SECURITY_GIT",+ "GIT_SSH_COMMAND",+ "GIT_CONFIG_GLOBAL",+ "CODEX_CLI_PATH",+ "CODEX_HOME",+ "CODEX_SECURITY_CONFIG_PATH",+ "CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH",+ "OPENAI_API_KEY",+ "CODEX_API_KEY",+ "SYNTHETIC_GATEWAY_KEY",+ ].map((name) => [name, process.env[name]] as const);const originalSpawn = childProcess.spawn;try {delete process.env.OPENAI_API_KEY;@@ -1107,16 +1102,14 @@ async function testWorkerRuntimeSettings() {await mkdir(codexHome);await writeFile(path.join(codexHome, "config.toml"),- [- 'model_provider = "synthetic"',- "[model_providers.synthetic]",- 'name = "Synthetic gateway"',- 'base_url = "https://gateway.example.test/v1"',- 'wire_api = "responses"',- 'env_key = "SYNTHETIC_GATEWAY_KEY"',- ].join("\n"),+ `model_provider = "synthetic"+[model_providers.synthetic]+name = "Synthetic gateway"+base_url = "https://gateway.example.test/v1"+wire_api = "responses"+env_key = "SYNTHETIC_GATEWAY_KEY"`,);- await writeFile(configPath, configuration);+ await writeFile(configPath, configuration!);await writeFile(promptPath, "synthetic worker configuration fixture");process.env.CODEX_CLI_PATH = process.execPath;process.env.CODEX_HOME = codexHome;@@ -1125,14 +1118,23 @@ async function testWorkerRuntimeSettings() {fixture.root,"deep settings.toml",);- const launches = [];- childProcess.spawn = (command, args, options) => {+ const launches: {+ command?: string;+ args: readonly string[];+ environment?: NodeJS.ProcessEnv;+ markerPath: string;+ }[] = [];+ childProcess.spawn = ((+ command: string,+ args: readonly string[],+ options: SpawnOptions,+ ) => {const markerPath = path.join(fixture.root,`invocation-${launches.length}.json`,);const environment = {- ...options.env,+ ...options!.env,FAKE_CODEX_MARKER: markerPath,FAKE_CODEX_PREFLIGHT_MARKER: markerPath,};@@ -1145,7 +1147,7 @@ async function testWorkerRuntimeSettings() {: args,{ ...options, env: environment },);- };+ }) as typeof childProcess.spawn;syncBuiltinESMExports();const settings = [{ model: "gpt-5.6-sol", reasoningEffort: "xhigh" },@@ -1172,7 +1174,7 @@ async function testWorkerRuntimeSettings() {}),);// A running coordinator retains its settings if the source file changes.- for (const kind of ["discovery", "dedup"]) {+ for (const kind of ["discovery", "dedup"] as const) {for (const resumeThreadId of [undefined, "fixture-resumed-thread"]) {launches.length = 0;await Promise.all(@@ -1224,55 +1226,36 @@ async function testWorkerRuntimeSettings() {: process.execPath,);assert.equal(- workerLaunch.environment.CODEX_CLI_PATH,+ workerLaunch.environment!.CODEX_CLI_PATH,process.execPath,);assert.equal(- workerLaunch.environment.CODEX_HOME,+ workerLaunch.environment!.CODEX_HOME,await realpath(codexHome),);assert.equal(- workerLaunch.environment.CODEX_SECURITY_CONFIG_PATH,- configPath,+ workerLaunch.environment!.CODEX_SECURITY_CONFIG_PATH,+ configPath!,);assert.equal(- workerLaunch.environment.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH,+ workerLaunch.environment!.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH,process.env.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH,);assert.equal(workerPermissionProfileOverride(workerLaunch.args),'permissions.codex_security_deep_scan_worker={extends=":read-only",filesystem={":root"="read","/repo/.env"="deny","/repo/**/.secret"="deny","/repo/**/*.pem"="deny",glob_scan_max_depth=3},network={enabled=false}}',);- const invocation = JSON.parse(- await readFile(workerLaunch.markerPath, "utf8"),- );+ const invocation = await readJson(workerLaunch.markerPath);assert.equal(invocation.providerKey, providerKeys[index]);- assert.equal(workerLaunch.environment.CODEX_API_KEY, undefined);+ assert.equal(workerLaunch.environment!.CODEX_API_KEY, undefined);assert.equal(process.env.SYNTHETIC_GATEWAY_KEY,providerKeys.at(-1),);- if (expected === undefined) {- assert.equal(- invocation.argv.some((arg) =>- arg.startsWith("model_reasoning_summary="),- ),- false,- );- } else {- assert.equal(- invocation.argv.includes(- `model_reasoning_summary=${JSON.stringify(expected)}`,- ),- true,- );- }- assert.equal(- invocation.argv.includes(- `model_reasoning_effort=${JSON.stringify(reasoningEffort)}`,- ),- true,- );+ assertConfigOverrides(invocation.argv, {+ model_reasoning_summary: expected,+ model_reasoning_effort: reasoningEffort,+ });assertFlagPair(invocation.argv, "--model", model);assert.equal(invocation.argv.includes("resume"),@@ -1284,12 +1267,10 @@ async function testWorkerRuntimeSettings() {assert.equal(process.env[name], value);}assert.equal(invocation.python, python);- assert.equal(- invocation.argv.includes(- `mcp_servers.cs_artifacts.env.CODEX_SECURITY_PYTHON_COMMAND=${JSON.stringify(helperPython)}`,- ),- true,- );+ assertConfigOverrides(invocation.argv, {+ "mcp_servers.cs_artifacts.env.CODEX_SECURITY_PYTHON_COMMAND":+ helperPython,+ });assert.equal(process.env.PYTHON, python);assert.equal(invocation.deepConfigPath,@@ -1301,9 +1282,7 @@ async function testWorkerRuntimeSettings() {for (const launch of launches.filter(({ args }) =>args.includes("app-server"),)) {- const preflight = JSON.parse(- await readFile(launch.markerPath, "utf8"),- );+ const preflight = await readJson(launch.markerPath);assert.deepEqual(preflight.gitEnvironment, gitEnvironment);assert.equal(workerPermissionProfileOverride(launch.args),@@ -1317,12 +1296,19 @@ async function testWorkerRuntimeSettings() {} finally {childProcess.spawn = originalSpawn;syncBuiltinESMExports();- for (const [name, value] of Object.entries(saved)) restoreEnv(name, value);+ for (const [name, value] of saved) restoreEnv(name, value);}}async function testWorkerCyberAccessSettings() {- const cases = [+ const cases: {+ name: string;+ configuration: string;+ program?: string;+ features?: Record<string, boolean>;+ configPath?: string;+ executor?: import("../src/deep-scan/executor.js").CodexSdkWorkerExecutor;+ }[] = [{ name: "unset", configuration: "" },{name: "blue",@@ -1364,32 +1350,34 @@ async function testWorkerCyberAccessSettings() {},},];- const saved = Object.fromEntries(- [- "CODEX_CLI_PATH",- "CODEX_HOME",- "CODEX_SECURITY_CONFIG_PATH",- "OPENAI_API_KEY",- "CODEX_API_KEY",- ].map((name) => [name, process.env[name]]),- );+ const saved = [+ "CODEX_CLI_PATH",+ "CODEX_HOME",+ "CODEX_SECURITY_CONFIG_PATH",+ "OPENAI_API_KEY",+ "CODEX_API_KEY",+ ].map((name) => [name, process.env[name]] as const);const originalSpawn = childProcess.spawn;const fixture = await fakeCodexFixture();const promptPath = path.join(fixture.root, "prompt.md");- const launches = [];+ const launches: { args: readonly string[]; markerPath: string }[] = [];try {delete process.env.OPENAI_API_KEY;process.env.CODEX_API_KEY = "synthetic-worker-api-key";process.env.CODEX_CLI_PATH = process.execPath;process.env.CODEX_HOME = fixture.root;await writeFile(promptPath, "CAPTURE_SYNTHETIC_OPENAI_AUTH");- childProcess.spawn = (command, args, options) => {+ childProcess.spawn = ((+ command: string,+ args: readonly string[],+ options: SpawnOptions,+ ) => {const markerPath = path.join(fixture.root,`cyber-${launches.length}.json`,);const environment = {- ...options.env,+ ...options!.env,FAKE_CODEX_MARKER: markerPath,FAKE_CODEX_PREFLIGHT_MARKER: markerPath,};@@ -1398,7 +1386,7 @@ async function testWorkerCyberAccessSettings() {...options,env: environment,});- };+ }) as typeof childProcess.spawn;syncBuiltinESMExports();for (const testCase of cases) {testCase.configPath = path.join(fixture.root, `${testCase.name}.toml`);@@ -1408,14 +1396,14 @@ async function testWorkerCyberAccessSettings() {parentSandbox: trustedParentSandbox,});}- for (const kind of ["discovery", "dedup"]) {+ for (const kind of ["discovery", "dedup"] as const) {for (const resumeThreadId of [undefined, "fixture-resumed-thread"]) {launches.length = 0;await Promise.all(cases.map((testCase) => {// Each scan captures its own config before its asynchronous launch.process.env.CODEX_SECURITY_CONFIG_PATH = testCase.configPath;- return testCase.executor.run({+ return testCase.executor!.run({kind,resumeThreadId,promptPath,@@ -1433,9 +1421,7 @@ async function testWorkerCyberAccessSettings() {const launch = workerLaunches.find(({ args }) => args[args.indexOf("--model") + 1] === name,);- const invocation = JSON.parse(- await readFile(launch.markerPath, "utf8"),- );+ const invocation = await readJson(launch!.markerPath);if (program === undefined) {assert.equal(invocation.argv.includes("--cyber-access-program"),@@ -1449,7 +1435,7 @@ async function testWorkerCyberAccessSettings() {"api_key_model_discovery",]) {assert.deepEqual(- invocation.argv.filter((arg) =>+ invocation.argv.filter((arg: string) =>arg.startsWith(`features.${feature}=`),),features[feature] === undefined@@ -1469,32 +1455,28 @@ async function testWorkerCyberAccessSettings() {assertWorkerSubagentPolicy(invocation.argv, 0);}// Running coordinators and their resumed workers retain their own settings.- await Promise.all(- cases.map(({ configPath }) =>- writeFile(- configPath,- '[codex_security]\ncyber_access_program = "daybreak_red"\n[features]\napi_key_cyber_access_programs = false\napi_key_model_discovery = false\n',- ),- ),- );+ for (const { configPath } of cases) {+ await writeFile(+ configPath!,+ '[codex_security]\ncyber_access_program = "daybreak_red"\n[features]\napi_key_cyber_access_programs = false\napi_key_model_discovery = false\n',+ );+ }}}} finally {childProcess.spawn = originalSpawn;syncBuiltinESMExports();- for (const [name, value] of Object.entries(saved)) restoreEnv(name, value);+ for (const [name, value] of saved) restoreEnv(name, value);}}async function testBedrockCredentialsReachWorker() {const fixture = await fakeCodexFixture();- const mcpConfig = JSON.parse(- await readFile(new URL("../../.mcp.json", import.meta.url), "utf8"),- );+ const mcpConfig = await readJson(import.meta.dirname, "../../.mcp.json");const awsEnvironment = Object.fromEntries(mcpConfig.mcpServers["codex-security"].env_vars- .filter((name) => name.startsWith("AWS_"))- .map((name) => [name, `synthetic-bedrock-${name.toLowerCase()}`]),+ .filter((name: string) => name.startsWith("AWS_"))+ .map((name: string) => [name, `synthetic-bedrock-${name.toLowerCase()}`]),);assert.ok(awsEnvironment.AWS_BEARER_TOKEN_BEDROCK);assert.ok(awsEnvironment.AWS_ACCESS_KEY_ID);@@ -1504,8 +1486,8 @@ async function testBedrockCredentialsReachWorker() {CODEX_CLI_PATH: fixture.executablePath,FAKE_CODEX_BEDROCK_ENV_KEYS: JSON.stringify(Object.keys(awsEnvironment)),};- const previousEnvironment = Object.fromEntries(- Object.keys(environment).map((name) => [name, process.env[name]]),+ const previousEnvironment = Object.keys(environment).map(+ (name) => [name, process.env[name]] as const,);Object.assign(process.env, environment);@@ -1517,7 +1499,7 @@ async function testBedrockCredentialsReachWorker() {const executor = new CodexSdkWorkerExecutor({parentSandbox: trustedParentSandbox,});- for (const kind of ["discovery", "dedup"]) {+ for (const kind of ["discovery", "dedup"] as const) {for (const resumeThreadId of [undefined, "fixture-bedrock-resume"]) {const result = await executor.run({kind,@@ -1528,23 +1510,21 @@ async function testBedrockCredentialsReachWorker() {signal: new AbortController().signal,});assert.equal(result.threadId, resumeThreadId ?? "fixture-thread-id");- const invocation = JSON.parse(- await readFile(fixture.markerPath, "utf8"),- );+ const invocation = await readJson(fixture.markerPath);assert.deepEqual(invocation.bedrockAuthentication, awsEnvironment);assert.equal(invocation.argv.includes("resume"),resumeThreadId !== undefined,);assert.ok(- invocation.argv.some((arg) =>+ invocation.argv.some((arg: string) =>arg.includes("mcp_servers.codex-security.enabled=false"),),);}}} finally {- for (const [name, value] of Object.entries(previousEnvironment)) {+ for (const [name, value] of previousEnvironment) {restoreEnv(name, value);}}@@ -1552,123 +1532,119 @@ async function testBedrockCredentialsReachWorker() {async function testZeroSubagentsPreservesHostRestrictions() {for (const model of ["gpt-5.6-luna", "gpt-5.6-sol"]) {- await withExecutorFixture(- await fakeCodexFixture(),- "fixture zero-subagent worker prompt\n",- async (fixture, promptPath, workingDirectory) => {- const result = await new CodexSdkWorkerExecutor({- model,- parentSandbox: trustedParentSandbox,- }).run(discoveryRequest(promptPath, workingDirectory, 0));- assert.equal(result.threadId, "fixture-thread-id");- const invocation = JSON.parse(- await readFile(fixture.markerPath, "utf8"),- );- assertFlagPair(invocation.argv, "--model", model);- assertWorkerSubagentPolicy(invocation.argv, 0);- assertReadOnlyWorkerPolicy(invocation.argv);- },- );- }-}--async function testArtifactServerUsesExtendedStartupTimeout() {- await withExecutorFixture(- await fakeCodexFixture(),- "fixture artifact worker prompt\n",- async (fixture, promptPath, workingDirectory) => {+ await withWorkerFixture(async (fixture, promptPath, workingDirectory) => {+ await writeFile(promptPath, "fixture zero-subagent worker prompt\n");const result = await new CodexSdkWorkerExecutor({+ model,parentSandbox: trustedParentSandbox,- artifactContext: {- pluginRoot: fixture.root,- repoRoot: fixture.root,- scanId: "fixture-scan-id",- },}).run({- ...discoveryRequest(promptPath, workingDirectory, 0),- artifactContext: { root: workingDirectory, layout: "worker" },+ kind: "discovery",+ promptPath,+ workingDirectory,+ subagents: 0,+ signal: new AbortController().signal,});assert.equal(result.threadId, "fixture-thread-id");- const invocation = JSON.parse(await readFile(fixture.markerPath, "utf8"));- assertConfigOverrides(invocation.argv, {- "mcp_servers.cs_artifacts.startup_timeout_sec": 180,- "mcp_servers.cs_artifacts.required": true,- "mcp_servers.cs_artifacts.tool_timeout_sec": 86400,- });- },- );+ const invocation = await readJson(fixture.markerPath);+ assertFlagPair(invocation.argv, "--model", model);+ assertWorkerSubagentPolicy(invocation.argv, 0);+ assertReadOnlyWorkerPolicy(invocation.argv);+ });+ }+}++async function testArtifactServerUsesExtendedStartupTimeout() {+ return withWorkerFixture(async (fixture, promptPath, workingDirectory) => {+ await writeFile(promptPath, "fixture artifact worker prompt\n");+ const result = await new CodexSdkWorkerExecutor({+ parentSandbox: trustedParentSandbox,+ artifactContext: {+ pluginRoot: fixture.root,+ repoRoot: fixture.root,+ scanId: "fixture-scan-id",+ },+ }).run({+ kind: "discovery",+ promptPath,+ workingDirectory,+ subagents: 0,+ signal: new AbortController().signal,+ artifactContext: { root: workingDirectory, layout: "worker" },+ });+ assert.equal(result.threadId, "fixture-thread-id");+ const invocation = await readJson(fixture.markerPath);+ assertConfigOverrides(invocation.argv, {+ "mcp_servers.cs_artifacts.startup_timeout_sec": 180,+ "mcp_servers.cs_artifacts.required": true,+ "mcp_servers.cs_artifacts.tool_timeout_sec": 86400,+ });+ });}async function testSdkResumesExistingThread() {- await withExecutorFixture(- await fakeCodexFixture(),- "original worker prompt\n",- async (fixture, promptPath, workingDirectory) => {- const result = await new CodexSdkWorkerExecutor({- model: "gpt-5.6-sol",- reasoningEffort: "ultra",- parentSandbox: trustedParentSandbox,- }).run({- ...discoveryRequest(promptPath, workingDirectory, 3),- resumeThreadId: "fixture-existing-thread",- continuationPrompt: "continue the existing worker\n",- });- assert.equal(result.threadId, "fixture-existing-thread");- const invocation = JSON.parse(await readFile(fixture.markerPath, "utf8"));- const resumeIndex = invocation.argv.indexOf("resume");- assert.notEqual(resumeIndex, -1);- assert.equal(invocation.argv[resumeIndex + 1], "fixture-existing-thread");- assertFlagPair(invocation.argv, "--model", "gpt-5.6-sol");- assertConfigOverrides(invocation.argv, {- model_reasoning_effort: "ultra",- });- assertReadOnlyWorkerPolicy(invocation.argv);- assertWorkerSubagentPolicy(invocation.argv, 3);- assert.equal(invocation.stdin, "continue the existing worker\n");- },- );+ return withWorkerFixture(async (fixture, promptPath, workingDirectory) => {+ await writeFile(promptPath, "original worker prompt\n");+ const result = await new CodexSdkWorkerExecutor({+ model: "gpt-5.6-sol",+ reasoningEffort: "ultra",+ parentSandbox: trustedParentSandbox,+ }).run({+ kind: "discovery",+ promptPath,+ workingDirectory,+ subagents: 3,+ signal: new AbortController().signal,+ resumeThreadId: "fixture-existing-thread",+ continuationPrompt: "continue the existing worker\n",+ });+ assert.equal(result.threadId, "fixture-existing-thread");+ const invocation = await readJson(fixture.markerPath);+ const resumeIndex = invocation.argv.indexOf("resume");+ assert.notEqual(resumeIndex, -1);+ assert.equal(invocation.argv[resumeIndex + 1], "fixture-existing-thread");+ assertFlagPair(invocation.argv, "--model", "gpt-5.6-sol");+ assertConfigOverrides(invocation.argv, { model_reasoning_effort: "ultra" });+ assertReadOnlyWorkerPolicy(invocation.argv);+ assertWorkerSubagentPolicy(invocation.argv, 3);+ assert.equal(invocation.stdin, "continue the existing worker\n");+ });}async function testRetryNotificationDoesNotInterruptTurn() {- await withExecutorFixture(- await fakeCodexFixture(),- "RETRYABLE_STREAM_ERROR\n",- async (fixture, promptPath, workingDirectory) => {- const result = await new CodexSdkWorkerExecutor({- parentSandbox: trustedParentSandbox,- }).run(discoveryRequest(promptPath, workingDirectory, 3));- assert.equal(result.threadId, "fixture-thread-id");- const invocation = JSON.parse(await readFile(fixture.markerPath, "utf8"));- assert.equal(invocation.argv.includes("--model"), false);- assertConfigOverrides(invocation.argv, {- model_reasoning_effort: undefined,- });- },- );+ return withWorkerFixture(async (fixture, promptPath, workingDirectory) => {+ await writeFile(promptPath, "RETRYABLE_STREAM_ERROR\n");+ const result = await new CodexSdkWorkerExecutor({+ parentSandbox: trustedParentSandbox,+ }).run({+ kind: "discovery",+ promptPath,+ workingDirectory,+ subagents: 3,+ signal: new AbortController().signal,+ });+ assert.equal(result.threadId, "fixture-thread-id");+ const invocation = await readJson(fixture.markerPath);+ assert.equal(invocation.argv.includes("--model"), false);+ assertConfigOverrides(invocation.argv, {+ model_reasoning_effort: undefined,+ });+ });}async function testSandboxNamespaceDiagnosticIsSanitized() {- await withExecutorFixture(- await fakeCodexFixture(),- "BWRAP_NAMESPACE_FAILURE\n",- async (fixture, promptPath, workingDirectory) => {- const result = await new CodexSdkWorkerExecutor({- parentSandbox: trustedParentSandbox,- }).run(discoveryRequest(promptPath, workingDirectory, 3));- assert.deepEqual(result.diagnostics, [- {- code: "sandbox_namespace_exhausted",- message:- "Codex worker sandbox namespace creation failed (bwrap ENOSPC).",- },- ]);- const serialized = JSON.stringify(result);- assert.doesNotMatch(- serialized,- /super-secret-command|private source text/,- );- },+ const result = await runFixtureWorker(+ "BWRAP_NAMESPACE_FAILURE",+ "discovery",+ 3,);+ assert.deepEqual(result.diagnostics, [+ {+ code: "sandbox_namespace_exhausted",+ message: "Codex worker sandbox namespace creation failed (bwrap ENOSPC).",+ },+ ]);+ const serialized = JSON.stringify(result);+ assert.doesNotMatch(serialized, /super-secret-command|private source text/);}async function testOwnedArtifactToolFailureDiagnosticIsSanitized() {@@ -1705,28 +1681,20 @@ async function testOwnedArtifactToolFailureDiagnosticIsSanitized() {reason: "transport failed",},]) {- await withExecutorFixture(- await fakeCodexFixture(),- `${prompt}\n`,- async (fixture, promptPath, workingDirectory) => {- const result = await new CodexSdkWorkerExecutor({- parentSandbox: trustedParentSandbox,- }).run(discoveryRequest(promptPath, workingDirectory, 0));- if (tool) {- assert.deepEqual(result.diagnostics, [- {- code: "artifact_tool_failed",- message: `Codex worker artifact tool ${tool} ${reason}.`,- },- ]);- } else {- assert.equal(result.diagnostics, undefined);- }- assert.doesNotMatch(- JSON.stringify(result),- /synthetic-secret|private source|private output|private\/customer\/path/i,- );- },+ const result = await runFixtureWorker(prompt);+ if (tool) {+ assert.deepEqual(result.diagnostics, [+ {+ code: "artifact_tool_failed",+ message: `Codex worker artifact tool ${tool} ${reason}.`,+ },+ ]);+ } else {+ assert.equal(result.diagnostics, undefined);+ }+ assert.doesNotMatch(+ JSON.stringify(result),+ /synthetic-secret|private source|private output|private\/customer\/path/i,);}}@@ -1738,13 +1706,15 @@ function testCodeModeFrameDiagnosticBoundaries() {tool: "get_codex_security_deep_reducer_inputs",status: "failed",};- const resultWithText = (text) => ({ content: [{ type: "text", text }] });+ const resultWithText = (text: string) => ({+ content: [{ type: "text", text }],+ });for (const item of [{ type: "error", message: ipcFrameError },{ ...failedArtifactTool, error: { message: ipcFrameError } },{ ...failedArtifactTool, result: resultWithText(ipcFrameError) },]) {- const diagnostics = [];+ const diagnostics: CodexWorkerDiagnostic[] = [];appendSafeItemDiagnostic(diagnostics, failedArtifactTool);appendSafeItemDiagnostic(diagnostics, item);appendSafeItemDiagnostic(diagnostics, failedArtifactTool);@@ -1760,7 +1730,7 @@ function testCodeModeFrameDiagnosticBoundaries() {ipcFrameError.replace("76008279", "unknown"),"private path /customer/repo: IPC frame limit exceeded",]) {- const diagnostics = [];+ const diagnostics: CodexWorkerDiagnostic[] = [];appendSafeItemDiagnostic(diagnostics, { type: "error", message });appendSafeItemDiagnostic(diagnostics, {...failedArtifactTool,@@ -1788,20 +1758,14 @@ function testCodeModeFrameDiagnosticBoundaries() {{ type: "agent_message", text: ipcFrameError },{ type: "unknown", status: "failed", error: { message: ipcFrameError } },]) {- const diagnostics = [];+ const diagnostics: CodexWorkerDiagnostic[] = [];appendSafeItemDiagnostic(diagnostics, item);assert.deepEqual(diagnostics, []);}}async function testCodeModeFrameDiagnosticSurvivesSuccessfulTurn() {- const fixture = await fakeCodexFixture();- const previousPath = process.env.CODEX_CLI_PATH;- process.env.CODEX_CLI_PATH = fixture.executablePath;- try {- const promptPath = path.join(fixture.root, "prompt.md");- const workingDirectory = path.join(fixture.root, "artifacts");- await mkdir(workingDirectory);+ return withWorkerFixture(async (fixture, promptPath, workingDirectory) => {for (const event of [{ type: "error", message: ipcFrameError },{@@ -1827,50 +1791,37 @@ async function testCodeModeFrameDiagnosticSurvivesSuccessfulTurn() {{ code: "artifact_tool_failed", message: ipcFrameError },]);}- } finally {- restoreEnv("CODEX_CLI_PATH", previousPath);- }+ });}async function testStreamTerminationWithoutTerminalEventFails() {- await withExecutorFixture(- await fakeCodexFixture(),- "INCOMPLETE_STREAM\n",- async (fixture, promptPath, workingDirectory) => {- await assert.rejects(- new CodexSdkWorkerExecutor({- parentSandbox: trustedParentSandbox,- }).run(discoveryRequest(promptPath, workingDirectory, 3)),- /before turn\.completed.*fixture stream interrupted/i,- );- },+ await assert.rejects(+ runFixtureWorker("INCOMPLETE_STREAM", "discovery", 3),+ /before turn\.completed.*fixture stream interrupted/i,);}async function testAbortPropagation() {- await withExecutorFixture(- await fakeCodexFixture(),- "BLOCK_AFTER_START\n",- async (fixture, promptPath, workingDirectory) => {- const abortController = new AbortController();- const execution = new CodexSdkWorkerExecutor({- parentSandbox: trustedParentSandbox,- }).run({- kind: "discovery",- promptPath,- workingDirectory,- subagents: 0,- signal: abortController.signal,- onThreadStarted: () => abortController.abort("fixture cancellation"),- });- await assert.rejects(- execution,- (error) =>- error?.name === "AbortError" ||- /abort|SIGTERM/i.test(error?.message ?? ""),- );- },- );+ return withWorkerFixture(async (fixture, promptPath, workingDirectory) => {+ await writeFile(promptPath, "BLOCK_AFTER_START\n");+ const abortController = new AbortController();+ const execution = new CodexSdkWorkerExecutor({+ parentSandbox: trustedParentSandbox,+ }).run({+ kind: "discovery",+ promptPath,+ workingDirectory,+ subagents: 0,+ signal: abortController.signal,+ onThreadStarted: () => abortController.abort("fixture cancellation"),+ });+ await assert.rejects(+ execution,+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>+ error?.name === "AbortError" ||+ /abort|SIGTERM/i.test(error?.message ?? ""),+ );+ });}async function testCompletedWorkerSettlesWithoutWaitingForProcessExit() {@@ -1878,11 +1829,12 @@ async function testCompletedWorkerSettlesWithoutWaitingForProcessExit() {const previousPath = process.env.CODEX_CLI_PATH;process.env.CODEX_CLI_PATH = fixture.executablePath;const controller = new AbortController();- const unexpectedErrors = [];- const captureUnexpectedError = (error) => unexpectedErrors.push(error);+ const unexpectedErrors: unknown[] = [];+ const captureUnexpectedError = (error: NodeJS.ErrnoException) =>+ unexpectedErrors.push(error);let execution;let timeout;- let childPid;+ let childPid!: number;process.on("uncaughtException", captureUnexpectedError);try {@@ -1913,7 +1865,7 @@ async function testCompletedWorkerSettlesWithoutWaitingForProcessExit() {]);clearTimeout(timeout);assert.equal(result.threadId, "fixture-thread-id");- childPid = JSON.parse(await readFile(fixture.markerPath, "utf8")).pid;+ childPid = (await readJson(fixture.markerPath)).pid;controller.abort("coordinator immediately canceled its remaining workers");await new Promise((resolve) => setTimeout(resolve, 250));@@ -1922,8 +1874,7 @@ async function testCompletedWorkerSettlesWithoutWaitingForProcessExit() {assert.throws(() => process.kill(childPid, 0), { code: "ESRCH" });} finally {clearTimeout(timeout);- if (!controller.signal.aborted)- controller.abort("completed worker fixture cleanup");+ controller.abort("completed worker fixture cleanup");await execution?.catch(() => {});if (childPid) {try {@@ -1936,48 +1887,20 @@ async function testCompletedWorkerSettlesWithoutWaitingForProcessExit() {}async function testUnstructuredConfigurationFailureRemainsRetryable() {- await withExecutorFixture(- await fakeCodexFixture(),- "CONFIG_ERROR\n",- async (fixture, promptPath, workingDirectory) => {- await assert.rejects(- new CodexSdkWorkerExecutor({- parentSandbox: trustedParentSandbox,- }).run({- kind: "setup",- promptPath,- workingDirectory,- subagents: 0,- signal: new AbortController().signal,- }),- (error) =>- error?.name === "Error" &&- error.message.startsWith("Codex Exec exited with code 2:"),- );- },+ await assert.rejects(+ runFixtureWorker("CONFIG_ERROR", "setup"),+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>+ error?.name === "Error" &&+ error.message.startsWith("Codex Exec exited with code 2:"),);}async function testUnstructuredThreadStartFailureRemainsRetryable() {- await withExecutorFixture(- await fakeCodexFixture(),- "THREAD_START_CONFIG_ERROR\n",- async (fixture, promptPath, workingDirectory) => {- await assert.rejects(- new CodexSdkWorkerExecutor({- parentSandbox: trustedParentSandbox,- }).run({- kind: "setup",- promptPath,- workingDirectory,- subagents: 0,- signal: new AbortController().signal,- }),- (error) =>- error?.name === "Error" &&- error.message.startsWith("Codex Exec exited with code 1:"),- );- },+ await assert.rejects(+ runFixtureWorker("THREAD_START_CONFIG_ERROR", "setup"),+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>+ error?.name === "Error" &&+ error.message.startsWith("Codex Exec exited with code 1:"),);}@@ -1990,36 +1913,20 @@ async function testPolicyFailuresRemainWorkerErrors() {"UPSTREAM_CYBERSECURITY_RISK_ERROR","UPSTREAM_HIGH_RISK_CYBER_ACTIVITY_ERROR",]) {- await withExecutorFixture(- await fakeCodexFixture(),- `${prompt}\n`,- async (fixture, promptPath, workingDirectory) => {- await assert.rejects(- new CodexSdkWorkerExecutor({- parentSandbox: trustedParentSandbox,- }).run(discoveryRequest(promptPath, workingDirectory, 0)),- (error) =>- error?.name === "Error" && isCodexCybersecurityPolicyRefusal(error),- );- },+ await assert.rejects(+ runFixtureWorker(prompt),+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>+ error?.name === "Error" && isCodexCybersecurityPolicyRefusal(error),);}}async function testRateLimitPolicyFailureRemainsRetryable() {- await withExecutorFixture(- await fakeCodexFixture(),- "RATE_LIMIT_CYBER_POLICY_ERROR\n",- async (fixture, promptPath, workingDirectory) => {- await assert.rejects(- new CodexSdkWorkerExecutor({- parentSandbox: trustedParentSandbox,- }).run(discoveryRequest(promptPath, workingDirectory, 0)),- (error) =>- error?.name === "Error" &&- /429 Too Many Requests/.test(error?.message ?? ""),- );- },+ await assert.rejects(+ runFixtureWorker("RATE_LIMIT_CYBER_POLICY_ERROR"),+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>+ error?.name === "Error" &&+ /429 Too Many Requests/.test(error?.message ?? ""),);}@@ -2031,105 +1938,62 @@ async function testMalformedCommandEventsRemainRetryable() {"Request blocked by cyberPolicy.","This request has been flagged for possible cybersecurity risk.",]) {- await withExecutorFixture(- await fakeCodexFixture(),- `MALFORMED_COMMAND_EVENT\n${JSON.stringify(output)}\n`,- async (fixture, promptPath, workingDirectory) => {- const onThreadId = mock.fn();- await assert.rejects(- new CodexSdkWorkerExecutor({- parentSandbox: trustedParentSandbox,- }).run({- ...discoveryRequest(promptPath, workingDirectory, 0),- onThreadStarted: onThreadId,- }),- (error) =>- error?.name === "Error" &&- error.cause instanceof SyntaxError &&- error.message.startsWith("Failed to parse item: ") &&- error.message.includes('"id":"fixture-command"') &&- error.message.includes(JSON.stringify(output)),- `Malformed command output must remain retryable: ${output}`,- );- const threadId = onThreadId.mock.calls.at(-1)?.arguments[0];- assert.equal(threadId, "fixture-thread-id");- },- );+ await withWorkerFixture(async (fixture, promptPath, workingDirectory) => {+ await writeFile(+ promptPath,+ `MALFORMED_COMMAND_EVENT\n${JSON.stringify(output)}\n`,+ );+ const onThreadId = mock.fn();+ await assert.rejects(+ new CodexSdkWorkerExecutor({+ parentSandbox: trustedParentSandbox,+ }).run({+ kind: "discovery",+ promptPath,+ workingDirectory,+ subagents: 0,+ signal: new AbortController().signal,+ onThreadStarted: onThreadId,+ }),+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>+ error?.name === "Error" &&+ error.cause instanceof SyntaxError &&+ error.message.startsWith("Failed to parse item: ") &&+ error.message.includes('"id":"fixture-command"') &&+ error.message.includes(JSON.stringify(output)),+ `Malformed command output must remain retryable: ${output}`,+ );+ const threadId = onThreadId.mock.calls.at(-1)?.arguments[0];+ assert.equal(threadId, "fixture-thread-id");+ });}}async function testArtifactStartupTimeoutClassification() {- for (const { prompt, retryable } of [- { prompt: "ARTIFACT_MCP_STARTUP_TIMEOUT", retryable: true },- { prompt: "LEGACY_ARTIFACT_MCP_STARTUP_TIMEOUT", retryable: true },- {- prompt: "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT",- retryable: true,- },- {- prompt: "ARTIFACT_MCP_STARTUP_TIMEOUT_SYNC_AUTH_WARNING",- retryable: true,- },- {- prompt: "LEGACY_ARTIFACT_MCP_STARTUP_TIMEOUT_SYNC_AUTH_WARNING",- retryable: true,- },- {- prompt: "ARTIFACT_MCP_STARTUP_TIMEOUT_BOTH_AUTH_WARNINGS",- retryable: true,- },- {- prompt: "LEGACY_ARTIFACT_MCP_STARTUP_TIMEOUT_BOTH_AUTH_WARNINGS",- retryable: true,- },- {- prompt:- "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_SYNC_AUTH_WARNING",- retryable: true,- },- {- prompt:- "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_BOTH_AUTH_WARNINGS",- retryable: true,- },- {- prompt: "ARTIFACT_MCP_STARTUP_TIMEOUT_WITH_MISSING_API_KEY",- retryable: true,- },- {- prompt: "ARTIFACT_MCP_STARTUP_TIMEOUT_WITH_POLICY_REFUSAL",- retryable: true,- },- {- prompt:- "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_SYNC_AUTH_WARNING_WITH_MISSING_API_KEY",- retryable: true,- },- {- prompt:- "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_BOTH_AUTH_WARNINGS_WITH_POLICY_REFUSAL",- retryable: true,- },- { prompt: "OTHER_MCP_STARTUP_TIMEOUT", retryable: true },- {- prompt: "OTHER_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_SYNC_AUTH_WARNING",- retryable: true,- },- { prompt: "CATALOG_AUTH_ONLY", retryable: true },- { prompt: "SYNC_AUTH_ONLY", retryable: true },+ for (const prompt of [+ "ARTIFACT_MCP_STARTUP_TIMEOUT",+ "LEGACY_ARTIFACT_MCP_STARTUP_TIMEOUT",+ "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT",+ "ARTIFACT_MCP_STARTUP_TIMEOUT_SYNC_AUTH_WARNING",+ "LEGACY_ARTIFACT_MCP_STARTUP_TIMEOUT_SYNC_AUTH_WARNING",+ "ARTIFACT_MCP_STARTUP_TIMEOUT_BOTH_AUTH_WARNINGS",+ "LEGACY_ARTIFACT_MCP_STARTUP_TIMEOUT_BOTH_AUTH_WARNINGS",+ "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_SYNC_AUTH_WARNING",+ "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_BOTH_AUTH_WARNINGS",+ "ARTIFACT_MCP_STARTUP_TIMEOUT_WITH_MISSING_API_KEY",+ "ARTIFACT_MCP_STARTUP_TIMEOUT_WITH_POLICY_REFUSAL",+ "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_SYNC_AUTH_WARNING_WITH_MISSING_API_KEY",+ "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_BOTH_AUTH_WARNINGS_WITH_POLICY_REFUSAL",+ "OTHER_MCP_STARTUP_TIMEOUT",+ "OTHER_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_SYNC_AUTH_WARNING",+ "CATALOG_AUTH_ONLY",+ "SYNC_AUTH_ONLY",]) {- await withExecutorFixture(- await fakeCodexFixture(),- `${prompt}\n`,- async (fixture, promptPath, workingDirectory) => {- await assert.rejects(- new CodexSdkWorkerExecutor({- parentSandbox: trustedParentSandbox,- }).run(discoveryRequest(promptPath, workingDirectory, 0)),- (error) => (error?.name === "Error") === retryable,- `${prompt} should ${retryable ? "remain retryable" : "remain terminal"}`,- );- },+ await assert.rejects(+ runFixtureWorker(prompt),+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>+ error?.name === "Error",+ `${prompt} should remain retryable`,);}}@@ -2147,13 +2011,14 @@ async function testMissingParentSandboxFailsBeforeWorkerLaunch() {subagents: 0,signal: new AbortController().signal,}),- (error) =>+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>error?.name === "DeepScanNonRetryableError" &&/verified parent sandbox metadata/i.test(error.message),);await assert.rejects(readFile(fixture.markerPath, "utf8"),- (error) => error?.code === "ENOENT",+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>+ error?.code === "ENOENT",);} finally {restoreEnv("CODEX_CLI_PATH", previousPath);@@ -2161,15 +2026,21 @@ async function testMissingParentSandboxFailsBeforeWorkerLaunch() {}async function testDisallowedWorkerProfileFailsBeforeWorkerLaunch() {- await withExecutorFixture(- await fakeCodexFixture(emptyWorkerPermissionProfile, false),- "fixture blocked worker prompt\n",+ return withWorkerFixture(async (fixture, promptPath, workingDirectory) => {+ await writeFile(promptPath, "fixture blocked worker prompt\n");+await assert.rejects(new CodexSdkWorkerExecutor({parentSandbox: trustedParentSandbox,- }).run(discoveryRequest(promptPath, workingDirectory, 0)),- (error) =>+ }).run({+ kind: "discovery",+ promptPath,+ workingDirectory,+ subagents: 0,+ signal: new AbortController().signal,+ }),+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>error?.name === "DeepScanNonRetryableError" &&error.message.includes("codex_security_deep_scan_worker") &&error.message.includes("[allowed_permission_profiles]") &&@@ -2178,9 +2049,12 @@ async function testDisallowedWorkerProfileFailsBeforeWorkerLaunch() {);await assert.rejects(readFile(fixture.markerPath, "utf8"),- (error) => error?.code === "ENOENT",+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>+ error?.code === "ENOENT",);},+ emptyWorkerPermissionProfile,+ false,);}@@ -2189,47 +2063,57 @@ async function testRuntimePermissionProfileFallbackStopsAndDiscards() {"PERMISSION_PROFILE_FALLBACK_ITEM","PERMISSION_PROFILE_FALLBACK_EVENT",]) {- await withExecutorFixture(- await fakeCodexFixture(),- `${marker}\n`,- async (fixture, promptPath, workingDirectory) => {- await assert.rejects(- new CodexSdkWorkerExecutor({- parentSandbox: trustedParentSandbox,- }).run(discoveryRequest(promptPath, workingDirectory, 0)),- (error) =>- error?.name === "DeepScanNonRetryableError" &&- error.message.includes("worker was stopped") &&- error.message.includes("results were discarded") &&- !error.message.includes("did not run"),- );- const invocation = JSON.parse(- await readFile(fixture.markerPath, "utf8"),- );- assert.equal(invocation.stdin, `${marker}\n`);- },- );+ await withWorkerFixture(async (fixture, promptPath, workingDirectory) => {+ await writeFile(promptPath, `${marker}\n`);++ await assert.rejects(+ new CodexSdkWorkerExecutor({+ parentSandbox: trustedParentSandbox,+ }).run({+ kind: "discovery",+ promptPath,+ workingDirectory,+ subagents: 0,+ signal: new AbortController().signal,+ }),+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>+ error?.name === "DeepScanNonRetryableError" &&+ error.message.includes("worker was stopped") &&+ error.message.includes("results were discarded") &&+ !error.message.includes("did not run"),+ );+ const invocation = await readJson(fixture.markerPath);+ assert.equal(invocation.stdin, `${marker}\n`);+ });}}-async function withExecutorFixture(fixture, prompt, run) {- const previousPath = process.env.CODEX_CLI_PATH;- process.env.CODEX_CLI_PATH = fixture.executablePath;- try {- const promptPath = path.join(fixture.root, "prompt.md");- const workingDirectory = path.join(fixture.root, "artifacts");- await mkdir(workingDirectory);- await writeFile(promptPath, prompt);- await run(fixture, promptPath, workingDirectory);- } finally {- restoreEnv("CODEX_CLI_PATH", previousPath);- }+async function runFixtureWorker(+ prompt: string,+ kind: DeepScanWorkerKind = "discovery",+ subagents = 0,+) {+ return withWorkerFixture(async (fixture, promptPath, workingDirectory) => {+ await writeFile(promptPath, `${prompt}\n`);+ return await new CodexSdkWorkerExecutor({+ parentSandbox: trustedParentSandbox,+ }).run({+ kind,+ promptPath,+ workingDirectory,+ subagents,+ signal: new AbortController().signal,+ });+ });}async function fakeCodexFixture(preflightProfile = emptyWorkerPermissionProfile,preflightAllowed = true,- accountResult = { account: { type: "apiKey" }, requiresOpenaiAuth: true },+ accountResult: {+ account: { type: string } | null;+ requiresOpenaiAuth: boolean;+ } = { account: { type: "apiKey" }, requiresOpenaiAuth: true },) {const root = await temporaryDirectories.create("codex-security-sdk-executor-",@@ -2239,125 +2123,122 @@ async function fakeCodexFixture(const scriptPath = path.join(root, "fake-codex.mjs");await writeFile(scriptPath,- [- "#!/usr/bin/env node",- 'import { writeFileSync } from "node:fs";',- `const preflightProfile = ${JSON.stringify(preflightProfile)};`,- `const preflightAllowed = ${JSON.stringify(preflightAllowed)};`,- `const accountResult = ${JSON.stringify(accountResult)};`,- `const preflightMarkerPath = process.env.FAKE_CODEX_PREFLIGHT_MARKER ?? ${JSON.stringify(preflightMarkerPath)};`,- "if (process.argv.includes('app-server')) {",- " const preflight = { cwd: process.cwd(), codexHome: process.env.CODEX_HOME, gitEnvironment: Object.fromEntries(['PATH', 'CODEX_SECURITY_GIT', 'GIT_SSH_COMMAND', 'GIT_CONFIG_GLOBAL'].map(name => [name, process.env[name]])), requests: [] };",- " writeFileSync(preflightMarkerPath, JSON.stringify(preflight));",- " let buffer = '';",- " process.stdin.setEncoding('utf8');",- " process.stdin.on('data', (chunk) => {",- " buffer += chunk;",- " while (true) {",- " const newline = buffer.indexOf('\\n');",- " if (newline < 0) return;",- " const line = buffer.slice(0, newline).trim();",- " buffer = buffer.slice(newline + 1);",- " if (!line) continue;",- " const message = JSON.parse(line);",- " if (message.method === 'initialized') continue;",- " if (message.method === 'config/read' || message.method === 'permissionProfile/list') {",- " preflight.requests.push({ method: message.method, cwd: message.params?.cwd });",- " writeFileSync(preflightMarkerPath, JSON.stringify(preflight));",- " }",- " let result;",- " if (message.method === 'initialize') {",- " result = { userAgent: 'fixture', codexHome: '/fixture', platformFamily: 'unix', platformOs: 'macos' };",- " } else if (message.method === 'config/read') {",- " result = { config: { default_permissions: 'codex_security_deep_scan_worker', permissions: { codex_security_deep_scan_worker: preflightProfile } }, origins: {}, layers: null };",- " } else if (message.method === 'permissionProfile/list') {",- " result = { data: [{ id: 'codex_security_deep_scan_worker', description: null, allowed: preflightAllowed }], nextCursor: null };",- " } else if (message.method === 'account/read') {",- " result = accountResult;",- " } else if (message.method === 'configRequirements/read') {",- " result = { requirements: { allowedPermissionProfiles: { existing_profile: true } } };",- " } else {",- " process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, error: { code: -32601, message: 'Method not found' } }) + '\\n');",- " continue;",- " }",- " process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result }) + '\\n');",- " }",- " });",- " process.stdin.on('end', () => process.exit(0));",- "} else {",- "let stdin = '';",- "for await (const chunk of process.stdin) stdin += chunk;",- "const openaiAuthentication = stdin.includes('CAPTURE_SYNTHETIC_OPENAI_AUTH') ? { OPENAI_API_KEY: process.env.OPENAI_API_KEY, CODEX_API_KEY: process.env.CODEX_API_KEY } : undefined;",- "const bedrockAuthentication = stdin.includes('CAPTURE_SYNTHETIC_BEDROCK_AUTH') ? Object.fromEntries(JSON.parse(process.env.FAKE_CODEX_BEDROCK_ENV_KEYS).map((name) => [name, process.env[name]])) : undefined;",- "writeFileSync(process.env.FAKE_CODEX_MARKER, JSON.stringify({ argv: process.argv.slice(2), stdin, cwd: process.cwd(), codexHome: process.env.CODEX_HOME, gitEnvironment: Object.fromEntries(['PATH', 'CODEX_SECURITY_GIT', 'GIT_SSH_COMMAND', 'GIT_CONFIG_GLOBAL'].map(name => [name, process.env[name]])), configPath: process.env.CODEX_SECURITY_CONFIG_PATH, deepConfigPath: process.env.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH, python: process.env.PYTHON, providerKey: process.env.SYNTHETIC_GATEWAY_KEY, originator: process.env.CODEX_INTERNAL_ORIGINATOR_OVERRIDE, ...(stdin.includes('COMPLETE_THEN_HANG') ? { pid: process.pid } : {}), ...(openaiAuthentication ? { openaiAuthentication } : {}), ...(bedrockAuthentication ? { bedrockAuthentication } : {}) }));",- "if (stdin.includes('COMPLETE_THEN_HANG')) process.on('SIGTERM', () => setTimeout(() => process.exit(0), 100));",- "if (stdin.includes('THREAD_START_CONFIG_ERROR')) { console.error('Error: thread/start: thread/start failed: agents.max_threads cannot be set when features.multi_agent_v2 is enabled (code -32600)'); process.exit(1); }",- "if (stdin.includes('CONFIG_ERROR')) { console.error('failed to load configuration: invalid value'); process.exit(2); }",- "if (stdin.includes('MCP_STARTUP_TIMEOUT') || stdin.includes('CATALOG_AUTH_ONLY') || stdin.includes('SYNC_AUTH_ONLY')) {",- " const syncWarning = stdin.includes('SYNC_AUTH_WARNING') || stdin.includes('SYNC_AUTH_ONLY');",- " const bothWarnings = stdin.includes('BOTH_AUTH_WARNINGS');",- " if (!syncWarning || bothWarnings) console.error('chatgpt authentication required for remote plugin catalog; api key auth is not supported');",- " if (syncWarning || bothWarnings) console.error('chatgpt authentication required to sync remote plugins; api key auth is not supported');",- " if (!stdin.includes('CATALOG_AUTH_ONLY') && !stdin.includes('SYNC_AUTH_ONLY')) {",- " const serverName = stdin.includes('OTHER_MCP_STARTUP_TIMEOUT') ? 'other_server' : stdin.includes('LEGACY_ARTIFACT_MCP_STARTUP_TIMEOUT') ? 'codex_security_artifacts' : 'cs_artifacts';",- " const timeout = stdin.includes('REQUEST_TIMED_OUT') ? 'request timed out' : 'timed out handshaking with MCP server after 30s';",- " console.error('required MCP servers failed to initialize: ' + serverName + ': ' + timeout);",- " }",- " if (stdin.includes('WITH_MISSING_API_KEY')) console.error('missing API key');",- " if (stdin.includes('WITH_POLICY_REFUSAL')) console.error('Request blocked by cyberPolicy.');",- " process.exit(1);",- "}",- "const resumeIndex = process.argv.indexOf('resume');",- "const threadId = resumeIndex === -1 ? 'fixture-thread-id' : process.argv[resumeIndex + 1];",- "console.log(JSON.stringify({ type: 'thread.started', thread_id: threadId }));",- "if (stdin.includes('IPC_DIAGNOSTIC_EVENT')) console.log(stdin.split('\\n')[1]);",- "if (stdin.includes('MALFORMED_COMMAND_EVENT')) {",- " const output = JSON.parse(stdin.split('\\n')[1]);",- " const event = { type: 'item.completed', item: { id: 'fixture-command', type: 'command_execution', command: 'cat example.ts', aggregated_output: output, exit_code: 0, status: 'completed' } };",- " console.log(JSON.stringify(event).slice(0, -1));",- " process.exit(0);",- "}",- "const permissionProfileFallbackWarning = 'Configured value for `permission_profile` is disallowed by requirements; falling back from `codex_security_deep_scan_worker` to required value `:read-only`.';",- "if (stdin.includes('PERMISSION_PROFILE_FALLBACK_ITEM')) console.log(JSON.stringify({ type: 'item.completed', item: { id: 'warning-1', type: 'error', message: permissionProfileFallbackWarning } }));",- "if (stdin.includes('PERMISSION_PROFILE_FALLBACK_EVENT')) console.log(JSON.stringify({ type: 'error', message: permissionProfileFallbackWarning }));",- "if (stdin.includes('BLOCK_AFTER_START')) await new Promise(() => {});",- "if (stdin.includes('RATE_LIMIT_CYBER_POLICY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: '429 Too Many Requests: Request blocked by cyberPolicy.' } })); process.exit(0); }",- "if (stdin.includes('CYBER_POLICY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'Request blocked by cyberPolicy.' } })); process.exit(0); }",- "if (stdin.includes('SAFETY_POLICY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'Request blocked by a safety policy violation.' } })); process.exit(0); }",- "if (stdin.includes('UPSTREAM_CYBERSECURITY_RISK_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'This request has been flagged for possible cybersecurity risk.' } })); process.exit(0); }",- "if (stdin.includes('UPSTREAM_HIGH_RISK_CYBER_ACTIVITY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'This request has been flagged for potentially high-risk cyber activity.' } })); process.exit(0); }",- "if (stdin.includes('CYBERSECURITY_RISK_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'This content was flagged for possible cybersecurity risk.' } })); process.exit(0); }",- "if (stdin.includes('HIGH_RISK_CYBER_ACTIVITY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'This content was flagged for potentially high-risk cyber activity.' } })); process.exit(0); }",- "if (stdin.includes('RETRYABLE_STREAM_ERROR')) console.log(JSON.stringify({ type: 'error', message: 'Reconnecting... 2/5 (stream disconnected before completion: websocket closed by server before response.completed)' }));",- "if (stdin.includes('INCOMPLETE_STREAM')) { console.log(JSON.stringify({ type: 'error', message: 'fixture stream interrupted' })); process.exit(0); }",- "if (stdin.includes('BWRAP_NAMESPACE_FAILURE')) console.log(JSON.stringify({ type: 'item.completed', item: { id: 'command-1', type: 'command_execution', command: 'super-secret-command', aggregated_output: 'private source text\\nbwrap: Creating new namespace failed: nesting depth or /proc/sys/user/max_user_namespaces exceeded (ENOSPC)', exit_code: 1, status: 'failed' } }));",- "if (stdin.includes('ARTIFACT_TOOL_')) {",- " const server = stdin.includes('FOREIGN_ARTIFACT_TOOL_') ? 'untrusted_server' : stdin.includes('LEGACY_OWNED_ARTIFACT_TOOL_') ? 'codex_security_artifacts' : 'cs_artifacts';",- " const tool = stdin.includes('ADDITIONAL_OWNED_ARTIFACT_TOOL_') ? 'additional_codex_security_worker_tool' : stdin.includes('DISCOVERY_OWNED_ARTIFACT_TOOL_') ? 'record_codex_security_discovery_candidates' : 'record_codex_security_deep_reduction';",- " const item = { id: 'mcp-1', type: 'mcp_tool_call', server, tool, arguments: { secret: 'Bearer synthetic-secret', source: 'private source text' }, result: stdin.includes('REJECTED') ? { content: [{ type: 'text', text: 'private output sk-proj-synthetic-secret' }] } : null, error: stdin.includes('TRANSPORT_FAILED') ? { message: 'transport closed sk-proj-synthetic-secret /private/customer/path' } : null, status: 'failed' };",- " console.log(JSON.stringify({ type: 'item.completed', item }));",- "}",- "console.log(JSON.stringify({ type: 'item.completed', item: { id: 'message-1', type: 'agent_message', text: 'fixture final response' } }));",- "console.log(JSON.stringify({ type: 'turn.completed', usage: { input_tokens: 1, cached_input_tokens: 0, output_tokens: 1 } }));",- "if (stdin.includes('COMPLETE_THEN_HANG')) { setInterval(() => {}, 1_000); await new Promise(() => {}); }",- "}",- "",- ].join("\n"),+ `#!/usr/bin/env node+import { writeFileSync } from "node:fs";+const preflightProfile = ${JSON.stringify(preflightProfile)};+const preflightAllowed = ${JSON.stringify(preflightAllowed)};+const accountResult = ${JSON.stringify(accountResult)};+const preflightMarkerPath = process.env.FAKE_CODEX_PREFLIGHT_MARKER ?? ${JSON.stringify(preflightMarkerPath)};+if (process.argv.includes('app-server')) {+ const preflight = { cwd: process.cwd(), codexHome: process.env.CODEX_HOME, gitEnvironment: Object.fromEntries(['PATH', 'CODEX_SECURITY_GIT', 'GIT_SSH_COMMAND', 'GIT_CONFIG_GLOBAL'].map(name => [name, process.env[name]])), requests: [] };+ writeFileSync(preflightMarkerPath, JSON.stringify(preflight));+ let buffer = '';+ process.stdin.setEncoding('utf8');+ process.stdin.on('data', (chunk) => {+ buffer += chunk;+ while (true) {+ const newline = buffer.indexOf('\\n');+ if (newline < 0) return;+ const line = buffer.slice(0, newline).trim();+ buffer = buffer.slice(newline + 1);+ if (!line) continue;+ const message = JSON.parse(line);+ if (message.method === 'initialized') continue;+ if (message.method === 'config/read' || message.method === 'permissionProfile/list') {+ preflight.requests.push({ method: message.method, cwd: message.params?.cwd });+ writeFileSync(preflightMarkerPath, JSON.stringify(preflight));+ }+ let result;+ if (message.method === 'initialize') {+ result = { userAgent: 'fixture', codexHome: '/fixture', platformFamily: 'unix', platformOs: 'macos' };+ } else if (message.method === 'config/read') {+ result = { config: { default_permissions: 'codex_security_deep_scan_worker', permissions: { codex_security_deep_scan_worker: preflightProfile } }, origins: {}, layers: null };+ } else if (message.method === 'permissionProfile/list') {+ result = { data: [{ id: 'codex_security_deep_scan_worker', description: null, allowed: preflightAllowed }], nextCursor: null };+ } else if (message.method === 'account/read') {+ result = accountResult;+ } else if (message.method === 'configRequirements/read') {+ result = { requirements: { allowedPermissionProfiles: { existing_profile: true } } };+ } else {+ process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, error: { code: -32601, message: 'Method not found' } }) + '\\n');+ continue;+ }+ process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result }) + '\\n');+ }+ });+ process.stdin.on('end', () => process.exit(0));+} else {+const stdin = (await process.stdin.toArray()).join('');+const openaiAuthentication = stdin.includes('CAPTURE_SYNTHETIC_OPENAI_AUTH') ? { OPENAI_API_KEY: process.env.OPENAI_API_KEY, CODEX_API_KEY: process.env.CODEX_API_KEY } : undefined;+const bedrockAuthentication = stdin.includes('CAPTURE_SYNTHETIC_BEDROCK_AUTH') ? Object.fromEntries(JSON.parse(process.env.FAKE_CODEX_BEDROCK_ENV_KEYS).map((name) => [name, process.env[name]])) : undefined;+writeFileSync(process.env.FAKE_CODEX_MARKER, JSON.stringify({ argv: process.argv.slice(2), stdin, cwd: process.cwd(), codexHome: process.env.CODEX_HOME, gitEnvironment: Object.fromEntries(['PATH', 'CODEX_SECURITY_GIT', 'GIT_SSH_COMMAND', 'GIT_CONFIG_GLOBAL'].map(name => [name, process.env[name]])), configPath: process.env.CODEX_SECURITY_CONFIG_PATH, deepConfigPath: process.env.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH, python: process.env.PYTHON, providerKey: process.env.SYNTHETIC_GATEWAY_KEY, originator: process.env.CODEX_INTERNAL_ORIGINATOR_OVERRIDE, ...(stdin.includes('COMPLETE_THEN_HANG') ? { pid: process.pid } : {}), ...(openaiAuthentication ? { openaiAuthentication } : {}), ...(bedrockAuthentication ? { bedrockAuthentication } : {}) }));+if (stdin.includes('COMPLETE_THEN_HANG')) process.on('SIGTERM', () => setTimeout(() => process.exit(0), 100));+if (stdin.includes('THREAD_START_CONFIG_ERROR')) { console.error('Error: thread/start: thread/start failed: agents.max_threads cannot be set when features.multi_agent_v2 is enabled (code -32600)'); process.exit(1); }+if (stdin.includes('CONFIG_ERROR')) { console.error('failed to load configuration: invalid value'); process.exit(2); }+if (stdin.includes('MCP_STARTUP_TIMEOUT') || stdin.includes('CATALOG_AUTH_ONLY') || stdin.includes('SYNC_AUTH_ONLY')) {+ const syncWarning = stdin.includes('SYNC_AUTH_WARNING') || stdin.includes('SYNC_AUTH_ONLY');+ const bothWarnings = stdin.includes('BOTH_AUTH_WARNINGS');+ if (!syncWarning || bothWarnings) console.error('chatgpt authentication required for remote plugin catalog; api key auth is not supported');+ if (syncWarning || bothWarnings) console.error('chatgpt authentication required to sync remote plugins; api key auth is not supported');+ if (!stdin.includes('CATALOG_AUTH_ONLY') && !stdin.includes('SYNC_AUTH_ONLY')) {+ const serverName = stdin.includes('OTHER_MCP_STARTUP_TIMEOUT') ? 'other_server' : stdin.includes('LEGACY_ARTIFACT_MCP_STARTUP_TIMEOUT') ? 'codex_security_artifacts' : 'cs_artifacts';+ const timeout = stdin.includes('REQUEST_TIMED_OUT') ? 'request timed out' : 'timed out handshaking with MCP server after 30s';+ console.error('required MCP servers failed to initialize: ' + serverName + ': ' + timeout);+ }+ if (stdin.includes('WITH_MISSING_API_KEY')) console.error('missing API key');+ if (stdin.includes('WITH_POLICY_REFUSAL')) console.error('Request blocked by cyberPolicy.');+ process.exit(1);+}+const resumeIndex = process.argv.indexOf('resume');+const threadId = resumeIndex === -1 ? 'fixture-thread-id' : process.argv[resumeIndex + 1];+console.log(JSON.stringify({ type: 'thread.started', thread_id: threadId }));+if (stdin.includes('IPC_DIAGNOSTIC_EVENT')) console.log(stdin.split('\\n')[1]);+if (stdin.includes('MALFORMED_COMMAND_EVENT')) {+ const output = JSON.parse(stdin.split('\\n')[1]);+ const event = { type: 'item.completed', item: { id: 'fixture-command', type: 'command_execution', command: 'cat example.ts', aggregated_output: output, exit_code: 0, status: 'completed' } };+ console.log(JSON.stringify(event).slice(0, -1));+ process.exit(0);+}+const permissionProfileFallbackWarning = 'Configured value for \`permission_profile\` is disallowed by requirements; falling back from \`codex_security_deep_scan_worker\` to required value \`:read-only\`.';+if (stdin.includes('PERMISSION_PROFILE_FALLBACK_ITEM')) console.log(JSON.stringify({ type: 'item.completed', item: { id: 'warning-1', type: 'error', message: permissionProfileFallbackWarning } }));+if (stdin.includes('PERMISSION_PROFILE_FALLBACK_EVENT')) console.log(JSON.stringify({ type: 'error', message: permissionProfileFallbackWarning }));+if (stdin.includes('BLOCK_AFTER_START')) await new Promise(() => {});+if (stdin.includes('RATE_LIMIT_CYBER_POLICY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: '429 Too Many Requests: Request blocked by cyberPolicy.' } })); process.exit(0); }+if (stdin.includes('CYBER_POLICY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'Request blocked by cyberPolicy.' } })); process.exit(0); }+if (stdin.includes('SAFETY_POLICY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'Request blocked by a safety policy violation.' } })); process.exit(0); }+if (stdin.includes('UPSTREAM_CYBERSECURITY_RISK_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'This request has been flagged for possible cybersecurity risk.' } })); process.exit(0); }+if (stdin.includes('UPSTREAM_HIGH_RISK_CYBER_ACTIVITY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'This request has been flagged for potentially high-risk cyber activity.' } })); process.exit(0); }+if (stdin.includes('CYBERSECURITY_RISK_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'This content was flagged for possible cybersecurity risk.' } })); process.exit(0); }+if (stdin.includes('HIGH_RISK_CYBER_ACTIVITY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'This content was flagged for potentially high-risk cyber activity.' } })); process.exit(0); }+if (stdin.includes('RETRYABLE_STREAM_ERROR')) console.log(JSON.stringify({ type: 'error', message: 'Reconnecting... 2/5 (stream disconnected before completion: websocket closed by server before response.completed)' }));+if (stdin.includes('INCOMPLETE_STREAM')) { console.log(JSON.stringify({ type: 'error', message: 'fixture stream interrupted' })); process.exit(0); }+if (stdin.includes('BWRAP_NAMESPACE_FAILURE')) console.log(JSON.stringify({ type: 'item.completed', item: { id: 'command-1', type: 'command_execution', command: 'super-secret-command', aggregated_output: 'private source text\\nbwrap: Creating new namespace failed: nesting depth or /proc/sys/user/max_user_namespaces exceeded (ENOSPC)', exit_code: 1, status: 'failed' } }));+if (stdin.includes('ARTIFACT_TOOL_')) {+ const server = stdin.includes('FOREIGN_ARTIFACT_TOOL_') ? 'untrusted_server' : stdin.includes('LEGACY_OWNED_ARTIFACT_TOOL_') ? 'codex_security_artifacts' : 'cs_artifacts';+ const tool = stdin.includes('ADDITIONAL_OWNED_ARTIFACT_TOOL_') ? 'additional_codex_security_worker_tool' : stdin.includes('DISCOVERY_OWNED_ARTIFACT_TOOL_') ? 'record_codex_security_discovery_candidates' : 'record_codex_security_deep_reduction';+ const item = { id: 'mcp-1', type: 'mcp_tool_call', server, tool, arguments: { secret: 'Bearer synthetic-secret', source: 'private source text' }, result: stdin.includes('REJECTED') ? { content: [{ type: 'text', text: 'private output sk-proj-synthetic-secret' }] } : null, error: stdin.includes('TRANSPORT_FAILED') ? { message: 'transport closed sk-proj-synthetic-secret /private/customer/path' } : null, status: 'failed' };+ console.log(JSON.stringify({ type: 'item.completed', item }));+}+console.log(JSON.stringify({ type: 'item.completed', item: { id: 'message-1', type: 'agent_message', text: 'fixture final response' } }));+console.log(JSON.stringify({ type: 'turn.completed', usage: { input_tokens: 1, cached_input_tokens: 0, output_tokens: 1 } }));+if (stdin.includes('COMPLETE_THEN_HANG')) { setInterval(() => {}, 1_000); await new Promise(() => {}); }+}+`,);await chmod(scriptPath, 0o755);process.env.FAKE_CODEX_MARKER = markerPath;return { root, markerPath, preflightMarkerPath, executablePath: scriptPath };}-function assertReadOnlyWorkerPolicy(args) {+function assertReadOnlyWorkerPolicy(args: readonly string[]) {assert.equal(args.includes("--sandbox"), false);assert.equal(args.includes("--add-dir"), false);assert.deepEqual(- args.filter((arg) => arg.startsWith("approval_policy=")),+ args.filter((arg: string) => arg.startsWith("approval_policy=")),['approval_policy="never"'],);assert.equal(- args.some((arg) => arg.includes("network_access")),+ args.some((arg: string) => arg.includes("network_access")),false,);const override = workerPermissionProfileOverride(args);@@ -2367,19 +2248,22 @@ function assertReadOnlyWorkerPolicy(args) {assert.equal(override.includes('"write"'), false);}-function workerPermissionProfileOverride(args) {+function workerPermissionProfileOverride(args: readonly string[]) {assert.deepEqual(- args.filter((arg) => arg.startsWith("default_permissions=")),+ args.filter((arg: string) => arg.startsWith("default_permissions=")),['default_permissions="codex_security_deep_scan_worker"'],);- const overrides = args.filter((arg) =>+ const overrides = args.filter((arg: string) =>arg.startsWith("permissions.codex_security_deep_scan_worker="),);assert.equal(overrides.length, 1);return overrides[0];}-function assertWorkerSubagentPolicy(args, subagents) {+function assertWorkerSubagentPolicy(+ args: readonly string[],+ subagents: number,+) {assertConfigOverrides(args, {"features.multi_agent_v2.enabled": false,"features.multi_agent_v2.max_concurrent_threads_per_session": subagents + 1,@@ -2399,12 +2283,15 @@ function assertWorkerSubagentPolicy(args, subagents) {assert.equal(args.includes("features.multi_agent_v2.enabled=true"), false);}-function restoreEnv(name, value) {+function restoreEnv(name: string, value: string | undefined) {if (value === undefined) delete process.env[name];else process.env[name] = value;}-function assertConfigOverrides(args, values) {+function assertConfigOverrides(+ args: readonly string[],+ values: Record<string, string | number | boolean | undefined>,+) {for (const [key, value] of Object.entries(values)) {if (value === undefined) {assert.equal(@@ -2417,12 +2304,23 @@ function assertConfigOverrides(args, values) {}}-function discoveryRequest(promptPath, workingDirectory, subagents) {- return {- kind: "discovery",- promptPath,- workingDirectory,- subagents,- signal: new AbortController().signal,- };+async function withWorkerFixture<Result>(+ action: (+ fixture: Awaited<ReturnType<typeof fakeCodexFixture>>,+ promptPath: string,+ workingDirectory: string,+ ) => Promise<Result>,+ ...options: Parameters<typeof fakeCodexFixture>+) {+ const fixture = await fakeCodexFixture(...options);+ const previousPath = process.env.CODEX_CLI_PATH;+ process.env.CODEX_CLI_PATH = fixture.executablePath;+ try {+ const promptPath = path.join(fixture.root, "prompt.md");+ const workingDirectory = path.join(fixture.root, "artifacts");+ await mkdir(workingDirectory);+ return await action(fixture, promptPath, workingDirectory);+ } finally {+ restoreEnv("CODEX_CLI_PATH", previousPath);+ }}diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.mjsdeleted file mode 100644index a424977a5..000000000--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.mjs+++ /dev/null@@ -1,359 +0,0 @@-import assert from "node:assert/strict";-import { fileURLToPath } from "node:url";-import { importSource } from "./import-module.mjs";--const { CODEX_SANDBOX_STATE_META_CAPABILITY, resolveDeepWorkerParentSandbox } =- await importSource(- fileURLToPath(- new URL("../src/deep-scan/parent-sandbox.ts", import.meta.url),- ),- );--const rootRead = {- path: { type: "special", value: { kind: "root" } },- access: "read",-};-const pinnedReadOnly = {- type: "managed",- file_system: { type: "restricted", entries: [rootRead] },- network: "restricted",-};--assert.equal(CODEX_SANDBOX_STATE_META_CAPABILITY, "codex/sandbox-state-meta");-assert.deepEqual(resolveDeepWorkerParentSandbox(extra(pinnedReadOnly)), {- filesystemDenies: [],-});-assert.deepEqual(- resolveDeepWorkerParentSandbox(- extra({- ...pinnedReadOnly,- network: "enabled",- }),- ),- {- filesystemDenies: [],- },-);-assert.deepEqual(- resolveDeepWorkerParentSandbox(- extra({- type: "managed",- file_system: { type: "unrestricted" },- network: "restricted",- }),- ),- {- filesystemDenies: [],- },-);-assert.deepEqual(- resolveDeepWorkerParentSandbox(- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [- rootRead,- {- path: { type: "special", value: { kind: "project_roots" } },- access: "write",- },- {- path: { type: "special", value: { kind: "tmpdir" } },- access: "write",- },- ],- },- }),- ),- {- filesystemDenies: [],- },-);-assert.deepEqual(- resolveDeepWorkerParentSandbox(- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [- rootRead,- {- path: { type: "path", path: "/repo/.env" },- access: "deny",- },- {- path: { type: "generated_default_path", path: "/repo/.secrets" },- access: "none",- },- {- path: { type: "glob_pattern", pattern: "/repo-a/**/.env" },- access: "deny",- },- {- path: { type: "glob_pattern", pattern: "/repo-b/**/*.pem" },- access: "none",- },- ],- glob_scan_max_depth: 3,- },- }),- ),- {- filesystemDenies: [- "/repo/.env",- "/repo/.secrets",- "/repo-a/**/.env",- "/repo-b/**/*.pem",- ],- globScanMaxDepth: 3,- },-);--assert.throws(- () =>- resolveDeepWorkerParentSandbox(- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [- rootRead,- {- path: {- type: "glob_pattern",- pattern: "codex-project-roots://**/*.pem",- },- access: "deny",- },- ],- },- }),- ),- (error) =>- error.name === "DeepScanNonRetryableError" &&- /symbolic project-roots denial metadata/i.test(error.message),-);--const pinnedFileUri = extra(- pinnedReadOnly,- "file:///tmp/codex-security-parent",-);-assert.deepEqual(resolveDeepWorkerParentSandbox(pinnedFileUri), {- filesystemDenies: [],-});-assert.deepEqual(- resolveDeepWorkerParentSandbox(- extra(pinnedReadOnly, "/tmp/codex-security-parent"),- ),- {- filesystemDenies: [],- },-);-assert.deepEqual(- resolveDeepWorkerParentSandbox({- requestInfo: pinnedFileUri,- }),- {- filesystemDenies: [],- },-);-assert.deepEqual(- resolveDeepWorkerParentSandbox({- _meta: pinnedFileUri._meta,- requestInfo: pinnedFileUri,- }),- {- filesystemDenies: [],- },-);-assert.deepEqual(- resolveDeepWorkerParentSandbox(- extra(- {- ...pinnedReadOnly,- },- "file:///tmp/codex-security-parent",- { type: "readOnly" },- ),- ),- {- filesystemDenies: [],- },-);--for (const invalid of [- undefined,- null,- {},- { _meta: {} },- { _meta: { [CODEX_SANDBOX_STATE_META_CAPABILITY]: null } },- extra(null),- extra({ ...pinnedReadOnly, type: "external" }),- extra({ ...pinnedReadOnly, type: "disabled" }),- extra({ ...pinnedReadOnly, network: "unknown" }),- extra({ ...pinnedReadOnly, network: { enabled: true } }),- extra({ ...pinnedReadOnly, file_system: null }),- extra({ ...pinnedReadOnly, file_system: { type: "unknown" } }),- extra({- ...pinnedReadOnly,- file_system: { type: "restricted", entries: "not-an-array" },- }),- extra({- ...pinnedReadOnly,- file_system: { type: "restricted", entries: [] },- }),- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [{ path: { type: "path", path: "/limited" }, access: "read" }],- },- }),- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [- {- path: {- type: "special",- value: { kind: "root", subpath: "only-this-subtree" },- },- access: "read",- },- ],- },- }),- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [- rootRead,- {- path: { type: "special", value: { kind: "tmpdir" } },- access: "deny",- },- ],- },- }),- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [- rootRead,- { path: { type: "glob_pattern", pattern: "**/*.env" }, access: "deny" },- ],- },- }),- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [- rootRead,- {- path: { type: "path", path: "/private" },- access: "deny",- missing_path_behavior: "skip",- },- ],- },- }),- ...[- "",- "relative/private",- "/repo/*.env",- "/repo/?.env",- "/repo/[literal]",- ].map((deniedPath) =>- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [- rootRead,- { path: { type: "path", path: deniedPath }, access: "deny" },- ],- },- }),- ),- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [- rootRead,- {- path: { type: "glob_pattern", pattern: "/repo/**/*.env" },- access: "read",- },- ],- },- }),- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [rootRead],- glob_scan_max_depth: 0,- },- }),- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [rootRead],- glob_scan_max_depth: 2,- globScanMaxDepth: 3,- },- }),- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [- {- path: { type: "special", value: { kind: "unknown" } },- access: "read",- },- ],- },- }),- extra({- ...pinnedReadOnly,- file_system: {- type: "restricted",- entries: [{ path: { type: "path", path: "" }, access: "read" }],- },- }),- extra(pinnedReadOnly, "relative/working-directory"),- extra(pinnedReadOnly, "file://remote-host/tmp/codex-security-parent"),- {- _meta: extra(pinnedReadOnly)._meta,- requestInfo: extra({ ...pinnedReadOnly, network: "enabled" }),- },-]) {- assert.throws(- () => resolveDeepWorkerParentSandbox(invalid),- (error) =>- error.name === "DeepScanNonRetryableError" &&- error.message.startsWith(- "Deep Scan cannot safely start a read-only worker:",- ),- );-}--function extra(permissionProfile, sandboxCwd, sandboxPolicy) {- return {- _meta: {- [CODEX_SANDBOX_STATE_META_CAPABILITY]: {- permissionProfile,- ...(sandboxCwd !== undefined ? { sandboxCwd } : {}),- ...(sandboxPolicy !== undefined ? { sandboxPolicy } : {}),- },- },- };-}diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.tsnew file mode 100644index 000000000..2ce33a5e3--- /dev/null+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.ts@@ -0,0 +1,272 @@+import assert from "node:assert/strict";+import path from "node:path";+import { importSource } from "./import-module.ts";++const { CODEX_SANDBOX_STATE_META_CAPABILITY, resolveDeepWorkerParentSandbox } =+ await importSource(+ path.join(import.meta.dirname, "../src/deep-scan/parent-sandbox.ts"),+ );++const rootRead = {+ path: { type: "special", value: { kind: "root" } },+ access: "read",+};+const pinnedReadOnly = {+ type: "managed",+ file_system: { type: "restricted", entries: [rootRead] },+ network: "restricted",+};++assert.equal(CODEX_SANDBOX_STATE_META_CAPABILITY, "codex/sandbox-state-meta");+assert.deepEqual(resolveDeepWorkerParentSandbox(extra(pinnedReadOnly)), {+ filesystemDenies: [],+});+assert.deepEqual(+ resolveDeepWorkerParentSandbox(+ extra({+ ...pinnedReadOnly,+ network: "enabled",+ }),+ ),+ {+ filesystemDenies: [],+ },+);+assert.deepEqual(+ resolveDeepWorkerParentSandbox(+ extra({+ ...pinnedReadOnly,+ file_system: { type: "unrestricted" },+ }),+ ),+ {+ filesystemDenies: [],+ },+);+assert.deepEqual(+ resolveDeepWorkerParentSandbox(+ restricted([+ rootRead,+ {+ path: { type: "special", value: { kind: "project_roots" } },+ access: "write",+ },+ {+ path: { type: "special", value: { kind: "tmpdir" } },+ access: "write",+ },+ ]),+ ),+ {+ filesystemDenies: [],+ },+);+assert.deepEqual(+ resolveDeepWorkerParentSandbox(+ restricted(+ [+ rootRead,+ {+ path: { type: "path", path: "/repo/.env" },+ access: "deny",+ },+ {+ path: { type: "generated_default_path", path: "/repo/.secrets" },+ access: "none",+ },+ {+ path: { type: "glob_pattern", pattern: "/repo-a/**/.env" },+ access: "deny",+ },+ {+ path: { type: "glob_pattern", pattern: "/repo-b/**/*.pem" },+ access: "none",+ },+ ],+ { glob_scan_max_depth: 3 },+ ),+ ),+ {+ filesystemDenies: [+ "/repo/.env",+ "/repo/.secrets",+ "/repo-a/**/.env",+ "/repo-b/**/*.pem",+ ],+ globScanMaxDepth: 3,+ },+);++assert.throws(+ () =>+ resolveDeepWorkerParentSandbox(+ restricted([+ rootRead,+ {+ path: {+ type: "glob_pattern",+ pattern: "codex-project-roots://**/*.pem",+ },+ access: "deny",+ },+ ]),+ ),+ (error: Error) =>+ error.name === "DeepScanNonRetryableError" &&+ /symbolic project-roots denial metadata/i.test(error.message),+);++const pinnedFileUri = extra(+ pinnedReadOnly,+ "file:///tmp/codex-security-parent",+);+assert.deepEqual(resolveDeepWorkerParentSandbox(pinnedFileUri), {+ filesystemDenies: [],+});+assert.deepEqual(+ resolveDeepWorkerParentSandbox(+ extra(pinnedReadOnly, "/tmp/codex-security-parent"),+ ),+ {+ filesystemDenies: [],+ },+);+assert.deepEqual(+ resolveDeepWorkerParentSandbox({+ requestInfo: pinnedFileUri,+ }),+ {+ filesystemDenies: [],+ },+);+assert.deepEqual(+ resolveDeepWorkerParentSandbox({+ _meta: pinnedFileUri._meta,+ requestInfo: pinnedFileUri,+ }),+ {+ filesystemDenies: [],+ },+);+assert.deepEqual(+ resolveDeepWorkerParentSandbox(+ extra(pinnedReadOnly, "file:///tmp/codex-security-parent", {+ type: "readOnly",+ }),+ ),+ {+ filesystemDenies: [],+ },+);++for (const invalid of [+ undefined,+ null,+ {},+ { _meta: {} },+ { _meta: { [CODEX_SANDBOX_STATE_META_CAPABILITY]: null } },+ extra(null),+ extra({ ...pinnedReadOnly, type: "external" }),+ extra({ ...pinnedReadOnly, type: "disabled" }),+ extra({ ...pinnedReadOnly, network: "unknown" }),+ extra({ ...pinnedReadOnly, network: { enabled: true } }),+ extra({ ...pinnedReadOnly, file_system: null }),+ extra({ ...pinnedReadOnly, file_system: { type: "unknown" } }),+ restricted("not-an-array"),+ restricted([]),+ restricted([{ path: { type: "path", path: "/limited" }, access: "read" }]),+ restricted([+ {+ path: {+ type: "special",+ value: { kind: "root", subpath: "only-this-subtree" },+ },+ access: "read",+ },+ ]),+ restricted([+ rootRead,+ {+ path: { type: "special", value: { kind: "tmpdir" } },+ access: "deny",+ },+ ]),+ restricted([+ rootRead,+ { path: { type: "glob_pattern", pattern: "**/*.env" }, access: "deny" },+ ]),+ restricted([+ rootRead,+ {+ path: { type: "path", path: "/private" },+ access: "deny",+ missing_path_behavior: "skip",+ },+ ]),+ ...[+ "",+ "relative/private",+ "/repo/*.env",+ "/repo/?.env",+ "/repo/[literal]",+ ].map((deniedPath) =>+ restricted([+ rootRead,+ { path: { type: "path", path: deniedPath }, access: "deny" },+ ]),+ ),+ restricted([+ rootRead,+ {+ path: { type: "glob_pattern", pattern: "/repo/**/*.env" },+ access: "read",+ },+ ]),+ restricted([rootRead], { glob_scan_max_depth: 0 }),+ restricted([rootRead], { glob_scan_max_depth: 2, globScanMaxDepth: 3 }),+ restricted([+ {+ path: { type: "special", value: { kind: "unknown" } },+ access: "read",+ },+ ]),+ restricted([{ path: { type: "path", path: "" }, access: "read" }]),+ extra(pinnedReadOnly, "relative/working-directory"),+ extra(pinnedReadOnly, "file://remote-host/tmp/codex-security-parent"),+ {+ _meta: extra(pinnedReadOnly)._meta,+ requestInfo: extra({ ...pinnedReadOnly, network: "enabled" }),+ },+]) {+ assert.throws(+ () => resolveDeepWorkerParentSandbox(invalid),+ (error: Error) =>+ error.name === "DeepScanNonRetryableError" &&+ error.message.startsWith(+ "Deep Scan cannot safely start a read-only worker:",+ ),+ );+}++function extra(+ permissionProfile: unknown,+ sandboxCwd?: string,+ sandboxPolicy?: unknown,+) {+ return {+ _meta: {+ [CODEX_SANDBOX_STATE_META_CAPABILITY]: {+ permissionProfile,+ ...(sandboxCwd !== undefined ? { sandboxCwd } : {}),+ ...(sandboxPolicy !== undefined ? { sandboxPolicy } : {}),+ },+ },+ };+}++function restricted(entries: unknown, options: Record<string, unknown> = {}) {+ return extra({+ ...pinnedReadOnly,+ file_system: { type: "restricted", entries, ...options },+ });+}diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.tssimilarity index 89%rename from plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.mjsrename to plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.tsindex 4a18c2c52..2ca93a845 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.mjs+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts@@ -1,4 +1,17 @@-import { temporaryDirectory } from "./support/temporary-directories.mjs";+import { readJson, readJsonLines } from "./support/json.ts";+import type { ChildProcess, SpawnOptions } from "node:child_process";+interface PreflightFixture {+ codexPath: string;+ cwd: string;+ argvPath: string;+ callsPath: string;+ cwdPath: string;+ envPath: string;+ readyPath: string;+ terminatedPath: string;+ children: ChildProcess[];+}+import { temporaryDirectory } from "./support/temporary-directories.ts";import assert from "node:assert/strict";import childProcess from "node:child_process";import {@@ -14,19 +27,16 @@ import {import { syncBuiltinESMExports } from "node:module";import { tmpdir } from "node:os";import path from "node:path";-import { fileURLToPath } from "node:url";-import { importSource } from "./import-module.mjs";+import { importSource } from "./import-module.ts";const {DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID: profileId,deepScanPermissionProfileFallbackError,preflightDeepScanWorkerPermissionProfile,} = await importSource(- fileURLToPath(- new URL(- "../src/deep-scan/permission-profile-preflight.ts",- import.meta.url,- ),+ path.join(+ import.meta.dirname,+ "../src/deep-scan/permission-profile-preflight.ts",),);@@ -43,7 +53,7 @@ const rawOverrides = [`permissions.${profileId}={filesystem={":root"="read","/repo/.env"="deny"},network={enabled=false}}`,];-const unsupportedConfiguration = (error) =>+const unsupportedConfiguration = (error: Error) =>error?.name === "Error" &&error.message.includes("with this Codex configuration");@@ -101,7 +111,7 @@ async function testAllowedProfileAndRawArgv() {async ({ codexPath, cwd, argvPath, callsPath }) => {await preflight(codexPath, cwd);- assert.deepEqual(JSON.parse(await readFile(argvPath, "utf8")), [+ assert.deepEqual(await readJson(argvPath), ["--config",rawOverrides[0],"--config",@@ -125,11 +135,10 @@ async function testAllowedProfileAndRawArgv() {async function testProvidedEnvForwardedWithoutMutation() {const codexHome = "/fixture/canonical-codex-home";const env = Object.freeze({- ...stringEnvironment(),+ ...process.env,CODEX_HOME: codexHome,DEEP_SCAN_PREFLIGHT_ENV_SENTINEL: "same-snapshot",});- const originalEntries = Object.entries(env);await withFakeCodex({@@ -138,11 +147,10 @@ async function testProvidedEnvForwardedWithoutMutation() {},async ({ codexPath, cwd, envPath }) => {await preflight(codexPath, cwd, env);- assert.deepEqual(JSON.parse(await readFile(envPath, "utf8")), {+ assert.deepEqual(await readJson(envPath), {codexHome,sentinel: "same-snapshot",});- assert.deepEqual(Object.entries(env), originalEntries);},);}@@ -219,7 +227,7 @@ async function testPreflightStartsInWorkerCwd() {assert.notEqual(cwd, process.cwd());await preflight(codexPath, cwd);- const childCwd = JSON.parse(await readFile(cwdPath, "utf8"));+ const childCwd = await readJson(cwdPath);assert.equal(await realpath(childCwd), await realpath(cwd));const calls = await readJsonLines(callsPath);assert.deepEqual(@@ -328,7 +336,7 @@ async function testDisallowedProfileGivesAdminGuidance() {async ({ codexPath, cwd, callsPath }) => {await assert.rejects(preflight(codexPath, cwd),- (error) =>+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>error?.name === "DeepScanNonRetryableError" &&error.message.includes(`\`${profileId}\``) &&error.message.includes(`[permissions.${profileId}]`) &&@@ -368,7 +376,7 @@ async function testOtherManagedPolicyRejectionIsGeneric() {async ({ codexPath, cwd }) => {await assert.rejects(preflight(codexPath, cwd),- (error) =>+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>error?.name === "DeepScanNonRetryableError" &&error.message.includes("managed Codex policy rejected") &&error.message.includes(`\`${profileId}\``) &&@@ -391,7 +399,7 @@ async function testMergedProfileCollisionFailsClosed() {async ({ codexPath, cwd }) => {await assert.rejects(preflight(codexPath, cwd),- (error) =>+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>error?.name === "DeepScanNonRetryableError" &&error.message.includes("existing Codex configuration changes") &&error.message.includes(`\`${profileId}\``) &&@@ -416,7 +424,7 @@ async function testLiteralProtoKeyCollisionFailsClosed() {async ({ codexPath, cwd }) => {await assert.rejects(preflight(codexPath, cwd),- (error) =>+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>error?.name === "DeepScanNonRetryableError" &&error.message.includes("existing Codex configuration changes"),);@@ -434,11 +442,13 @@ async function testSelectedProfileClassificationRequiresVerifiedString() {catalogResults: [catalogResult(true)],},async ({ codexPath, cwd, terminatedPath, children }) => {- await assert.rejects(preflight(codexPath, cwd), (error) =>- selected === ":read-only"- ? error?.name === "DeepScanNonRetryableError" &&- error.message.includes("did not select the required")- : unsupportedConfiguration(error),+ await assert.rejects(+ preflight(codexPath, cwd),+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>+ selected === ":read-only"+ ? error?.name === "DeepScanNonRetryableError" &&+ error.message.includes("did not select the required")+ : unsupportedConfiguration(error),);await assertPreflightStopped(children, terminatedPath);},@@ -472,7 +482,7 @@ async function testMalformedAndUnsupportedResponsesFailClosed() {async ({ codexPath, cwd }) => {await assert.rejects(preflight(codexPath, cwd),- (error) =>+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>unsupportedConfiguration(error) &&!error.message.includes("does not support"),);@@ -489,7 +499,7 @@ async function testMalformedAndUnsupportedResponsesFailClosed() {async ({ codexPath, cwd }) => {await assert.rejects(preflight(codexPath, cwd),- (error) =>+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>error?.name === "DeepScanNonRetryableError" &&error.message.includes(JSON.stringify(codexPath)) &&error.message.includes("permissionProfile/list") &&@@ -514,7 +524,7 @@ async function testEarlyExecutableExitIsNotVersionError() {async ({ codexPath, cwd }) => {await assert.rejects(preflight(codexPath, cwd),- (error) =>+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>error?.name === "Error" &&error.message.includes(JSON.stringify(codexPath)) &&error.message.includes(@@ -542,7 +552,7 @@ async function testUnexpectedTerminationRemainsRetryable() {children[0].kill("SIGKILL");await assert.rejects(running,- (error) =>+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>error?.name === "Error" &&error.message.includes(JSON.stringify(codexPath)) &&error.message.includes("signal SIGKILL") &&@@ -554,19 +564,19 @@ async function testUnexpectedTerminationRemainsRetryable() {}async function testStdioFailuresRemainRetryable() {- for (const stream of ["stdin", "stdout"]) {+ for (const stream of ["stdin", "stdout"] as const) {await withFakeCodex({ hangAt: "initialize" },async ({ codexPath, cwd, readyPath, terminatedPath, children }) => {const running = preflight(codexPath, cwd);await waitForFile(readyPath);assert.equal(children.length, 1);- children[0][stream].destroy(+ children[0][stream]!.destroy(new Error("SECRET_REPOSITORY_PATH=/repo/private"),);await assert.rejects(running,- (error) =>+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>error?.name === "Error" &&error.message.includes(JSON.stringify(codexPath)) &&error.message.includes(@@ -594,7 +604,7 @@ async function testUnknownJsonRpcFailuresRemainRetryableAndSafe() {async ({ codexPath, cwd, terminatedPath, children }) => {await assert.rejects(preflight(codexPath, cwd),- (error) =>+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>error?.name === "Error" &&error.message.includes(JSON.stringify(codexPath)) &&error.message.includes("config/read") &&@@ -614,7 +624,7 @@ async function testSpawnErrorFailsClosed() {const missingCodex = path.join(tmpdir(), `missing-codex-${process.pid}`);await assert.rejects(preflight(missingCodex, tmpdir()),- (error) =>+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>error?.name === "Error" &&error.cause?.code === "ENOENT" &&error.message.includes(JSON.stringify(missingCodex)) &&@@ -629,7 +639,7 @@ async function testMissingWorkerDirectoryRemainsRetryable() {await withFakeCodex({}, async ({ codexPath, cwd, argvPath }) => {await assert.rejects(preflight(codexPath, path.join(cwd, "missing-worker-directory")),- (error) =>+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>error?.name === "Error" &&error.cause?.code === "ENOENT" &&error.message.includes("could not start"),@@ -689,13 +699,20 @@ async function testAbortKillsPreflightChild() {});await waitForFile(readyPath);controller.abort(new DOMException("fixture aborted", "AbortError"));- await assert.rejects(running, (error) => error?.name === "AbortError");+ await assert.rejects(+ running,+ (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>+ error?.name === "AbortError",+ );await assertPreflightStopped(children, terminatedPath);},);}-async function assertPreflightStopped(children, terminatedPath) {+async function assertPreflightStopped(+ children: ChildProcess[],+ terminatedPath: string,+) {if (process.platform === "win32") {// Windows termination need not run the fixture's signal or stdin handlers.assert.equal(children.length, 1);@@ -708,7 +725,11 @@ async function assertPreflightStopped(children, terminatedPath) {}}-function preflight(codexPath, cwd, env) {+function preflight(+ codexPath: string,+ cwd: string,+ env?: Record<string, string>,+) {return preflightDeepScanWorkerPermissionProfile({codexPath,cwd,@@ -719,13 +740,18 @@ function preflight(codexPath, cwd, env) {});}-function stringEnvironment() {- return Object.fromEntries(- Object.entries(process.env).filter((entry) => typeof entry[1] === "string"),- );-}--function configReadResult(profile, defaultPermissions = profileId) {+function configReadResult(+ profile: unknown,+ defaultPermissions: unknown = profileId,+): {+ config: {+ default_permissions: unknown;+ permissions: Record<string, unknown>;+ forced_login_method?: string;+ };+ origins: Record<string, unknown>;+ layers: null;+} {return {config: {default_permissions: defaultPermissions,@@ -736,7 +762,7 @@ function configReadResult(profile, defaultPermissions = profileId) {};}-function catalogResult(allowed) {+function catalogResult(allowed: boolean) {return {data: [{ id: profileId, description: null, allowed }],nextCursor: null,@@ -744,8 +770,8 @@ function catalogResult(allowed) {}async function withFakeCodex(- scenario,- callback,+ scenario: Record<string, unknown>,+ callback: (fixture: PreflightFixture) => Promise<void>,{ longExecutable = false } = {},) {const root = await temporaryDirectory("deep-scan-profile-preflight-");@@ -769,7 +795,7 @@ async function withFakeCodex(await writeFile(scriptPath, fakeCodexSource(fixture), "utf8");await chmod(scriptPath, 0o755);const originalSpawn = childProcess.spawn;- const children = [];+ const children: ChildProcess[] = [];try {if (process.platform === "win32") {codexPath = process.execPath;@@ -784,7 +810,11 @@ async function withFakeCodex(assert.ok(codexPath.length > 260);}}- childProcess.spawn = (command, args, options) => {+ childProcess.spawn = ((+ command: string,+ args: readonly string[],+ options: SpawnOptions,+ ) => {if (command !== codexPath &&command !== path.toNamespacedPath(codexPath)@@ -799,7 +829,7 @@ async function withFakeCodex();children.push(child);return child;- };+ }) as typeof childProcess.spawn;syncBuiltinESMExports();await callback({codexPath,@@ -819,7 +849,7 @@ async function withFakeCodex(}}-function fakeCodexSource(scenario) {+function fakeCodexSource(scenario: unknown) {return `#!/usr/bin/env nodeimport { appendFileSync, writeFileSync } from "node:fs";@@ -920,12 +950,7 @@ function send(id, result, error) {`;}-async function readJsonLines(file) {- const content = await readFile(file, "utf8");- return content.trim().split(/\r?\n/u).filter(Boolean).map(JSON.parse);-}--async function waitForFile(file) {+async function waitForFile(file: string) {for (let attempt = 0; attempt < 200; attempt += 1) {try {await stat(file);diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.tssimilarity index 72%rename from plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.mjsrename to plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.tsindex eb25fd2c0..beb79f0d4 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.mjs+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts@@ -1,6 +1,8 @@-import { assertNoError, assertFlagPair } from "./assertions.mjs";-import { readOnlyParentSandboxState } from "./sandbox-state.mjs";-import { temporaryDirectory } from "./support/temporary-directories.mjs";+import type { PersistedDeepScanWorker } from "../src/deep-scan/types.js";+import { readJson, readJsonLines } from "./support/json.ts";+import { assertNoError, assertFlagPair } from "./assertions.ts";+import { readOnlyParentSandboxState } from "./sandbox-state.ts";+import { temporaryDirectory } from "./support/temporary-directories.ts";import assert from "node:assert/strict";import { execFile } from "node:child_process";import { randomUUID } from "node:crypto";@@ -17,8 +19,8 @@ import { setTimeout as delay } from "node:timers/promises";import { promisify } from "node:util";import { pathToFileURL } from "node:url";-import { applicationRoot as mcpAppRoot, buildServer } from "./build-server.mjs";-import * as streams from "./support/streams.mjs";+import { applicationRoot as mcpAppRoot, buildServer } from "./build-server.ts";+import * as streams from "./support/streams.ts";const execFileAsync = promisify(execFile);@@ -39,7 +41,6 @@ async function testDeepScanStdioLifecycle() {const targetPath = path.join(fixtureRoot, "target");const failedTargetPath = path.join(fixtureRoot, "failed-target");const stateDir = path.join(fixtureRoot, "state");- const scanRoot = path.join(fixtureRoot, "scans");const codexHome = path.join(fixtureRoot, "codex-home");const runtimeConfigPath = path.join(fixtureRoot, "active-config.toml");const startLogPath = path.join(fixtureRoot, "fake-codex-started.jsonl");@@ -68,8 +69,6 @@ async function testDeepScanStdioLifecycle() {await mkdir(targetPath, { recursive: true });await mkdir(failedTargetPath, { recursive: true });- await mkdir(stateDir, { recursive: true });- await mkdir(scanRoot, { recursive: true });await mkdir(path.join(codexHome, "codex-security"), { recursive: true });await writeFile(path.join(targetPath, "fixture.py"), "print('fixture')\n");await writeFile(@@ -78,25 +77,21 @@ async function testDeepScanStdioLifecycle() {);await writeFile(path.join(codexHome, "codex-security", "config.toml"),- [- "[deep_scan]",- "workers = 1",- "subagents = 0",- "stop_after_no_new = 1",- "max_discovery_runs = 2",- "",- ].join("\n"),+ `[deep_scan]+workers = 1+subagents = 0+stop_after_no_new = 1+max_discovery_runs = 2+`,);await writeFakeCodex(fakeCodexPath);await writeFile(runtimeConfigPath,- [- 'model_reasoning_summary = "detailed"',- 'profile = "selected"',- "[profiles.selected]",- 'model_reasoning_summary = "none"',- "",- ].join("\n"),+ `model_reasoning_summary = "detailed"+profile = "selected"+[profiles.selected]+model_reasoning_summary = "none"+`,);await writePythonWrapper(pythonWrapperPath);await buildServer(serverBundlePath, { target: "node20" });@@ -108,7 +103,7 @@ async function testDeepScanStdioLifecycle() {CODEX_CLI_PATH: fakeCodexPath,CODEX_HOME: codexHome,CODEX_SECURITY_CONFIG_PATH: runtimeConfigPath,- CODEX_SECURITY_SCAN_ROOT: scanRoot,+ CODEX_SECURITY_SCAN_ROOT: path.join(fixtureRoot, "scans"),CODEX_SECURITY_STATE_DIR: stateDir,PYTHON: pythonWrapperPath,REAL_PYTHON: process.env.PYTHON?.trim() || "python3",@@ -155,7 +150,9 @@ async function testDeepScanStdioLifecycle() {);assert.equal(missingParentSandbox.result?.isError, true);assert.match(- missingParentSandbox.result.content.map((item) => item.text).join(" "),+ missingParentSandbox.result.content+ .map((item: { text: string }) => item.text)+ .join(" "),/parent.*sandbox|sandbox.*metadata|permission/i,);@@ -180,11 +177,11 @@ async function testDeepScanStdioLifecycle() {assert.equal(unsupportedParentSandbox.result?.isError, true);assert.match(unsupportedParentSandbox.result.content- .map((item) => item.text)+ .map((item: { text: string }) => item.text).join(" "),/parent.*sandbox|sandbox.*metadata|permission/i,);- assert.deepEqual(await readJsonLines(startLogPath), []);+ assert.deepEqual(await readLogLines(startLogPath), []);server.sendRequest(10,@@ -205,7 +202,7 @@ async function testDeepScanStdioLifecycle() {assert.match(startedWorker.stdin, /record_codex_security_scan_draft/);const startedState = await getDeepScan({ environment, scanId, threadId });const startedArtifactRoot = startedState.workers.find(- (worker) => worker.kind === "discovery",+ (worker: PersistedDeepScanWorker) => worker.kind === "discovery",)?.artifactDir;assert.equal(typeof startedArtifactRoot, "string");assert.equal(workerContext.pluginRoot, pluginRoot);@@ -283,14 +280,16 @@ async function testDeepScanStdioLifecycle() {assert.equal(stillRunning.status, "running");assert.equal(stillRunning.cancelRequested, false);assert.equal(- stillRunning.workers.some((worker) => worker.kind === "setup"),+ stillRunning.workers.some(+ (worker: PersistedDeepScanWorker) => worker.kind === "setup",+ ),false,);assert.equal(stillRunning.workers.length, 1);assert.equal(stillRunning.workers[0].kind, "discovery");assert.equal(stillRunning.workers[0].status, "running");assertProcessAlive(startedWorker.pid);- assert.equal((await readJsonLines(startLogPath)).length, 1);+ assert.equal((await readLogLines(startLogPath)).length, 1);// Two live calls with the persisted scanId must join the one coordinator.server.sendRequest(@@ -313,7 +312,7 @@ async function testDeepScanStdioLifecycle() {).length >= 2,"both scanId callers to join the coordinator",);- assert.equal((await readJsonLines(startLogPath)).length, 1);+ assert.equal((await readLogLines(startLogPath)).length, 1);const rejectedWrongThreadCancel = await server.request(1312,@@ -323,7 +322,7 @@ async function testDeepScanStdioLifecycle() {assert.equal(rejectedWrongThreadCancel.result?.isError, true);assert.match(rejectedWrongThreadCancel.result.content- .map((item) => item.text)+ .map((item: { text: string }) => item.text).join(" "),/owned by another continuation|owning Codex thread/,);@@ -338,7 +337,9 @@ async function testDeepScanStdioLifecycle() {);assert.equal(failedCancel.result?.isError, true);assert.match(- failedCancel.result.content.map((item) => item.text).join(" "),+ failedCancel.result.content+ .map((item: { text: string }) => item.text)+ .join(" "),/injected cancel-scan failure/,);await delay(150);@@ -348,7 +349,9 @@ async function testDeepScanStdioLifecycle() {]);for (const failedJoin of [failedFirstJoin, failedSecondJoin]) {const failureText =- failedJoin.result?.content?.map((item) => item.text).join(" ") ?? "";+ failedJoin.result?.content+ ?.map((item: { text: string }) => item.text)+ .join(" ") ?? "";assert.equal(failedJoin.result?.isError, true);assert.equal(failedJoin.result?.structuredContent, undefined);assert.match(failureText, /injected cancel-scan failure/);@@ -371,7 +374,7 @@ async function testDeepScanStdioLifecycle() {);assertNoError(cancelResponse);assert.equal(- (await readJsonLines(cancelLogPath)).length,+ (await readLogLines(cancelLogPath)).length,2,"each cancellation request must invoke the durable transition exactly once",);@@ -390,11 +393,9 @@ async function testDeepScanStdioLifecycle() {),"cancellation persistence failure to reach the coordinator",);- const canceledManifest = JSON.parse(- await readFile(- path.join(startedState.scanDir, "scan-manifest.json"),- "utf8",- ),+ const canceledManifest = await readJson(+ startedState.scanDir,+ "scan-manifest.json",);assert.equal(Object.keys(canceledManifest.scan.preservedSources ?? {}).some((source) =>@@ -411,11 +412,15 @@ async function testDeepScanStdioLifecycle() {assert.equal(canceledState.status, "canceled");assert.equal(canceledState.cancelRequested, true);assert.equal(- canceledState.workers.some((worker) => worker.kind === "setup"),+ canceledState.workers.some(+ (worker: PersistedDeepScanWorker) => worker.kind === "setup",+ ),false,);assert.equal(- canceledState.workers.every((worker) => worker.status === "canceled"),+ canceledState.workers.every(+ (worker: PersistedDeepScanWorker) => worker.status === "canceled",+ ),true,);@@ -425,7 +430,7 @@ async function testDeepScanStdioLifecycle() {toolCall("start_codex_security_deep_scan", { scanId }, threadId),);assertCanceled(lateJoin, scanId, startedState.scanDir);- assert.equal((await readJsonLines(startLogPath)).length, 1);+ assert.equal((await readLogLines(startLogPath)).length, 1);const failureThreadId = "deep-scan-stdio-failure-thread";server.sendRequest(@@ -461,7 +466,7 @@ async function testDeepScanStdioLifecycle() {threadId: failureThreadId,});const failedArtifactRoot = activeFailureState.workers.find(- (worker) => worker.kind === "discovery",+ (worker: PersistedDeepScanWorker) => worker.kind === "discovery",)?.artifactDir;assert.equal(typeof failedArtifactRoot, "string");assert.equal(failedWorkerContext.pluginRoot, pluginRoot);@@ -489,7 +494,9 @@ async function testDeepScanStdioLifecycle() {);assertReadOnlyWorkerInvocation(failedWorker.argv);assert.equal(- activeFailureState.workers.some((worker) => worker.kind === "setup"),+ activeFailureState.workers.some(+ (worker: PersistedDeepScanWorker) => worker.kind === "setup",+ ),false,);assert.equal(activeFailureState.workers.length, 1);@@ -511,7 +518,9 @@ async function testDeepScanStdioLifecycle() {const failedWaiter = await server.waitForResponse(20);const failureText =- failedWaiter.result?.content?.map((item) => item.text).join(" ") ?? "";+ failedWaiter.result?.content+ ?.map((item: { text: string }) => item.text)+ .join(" ") ?? "";assert.equal(failedWaiter.result?.isError, true);assert.equal(failedWaiter.result?.structuredContent, undefined);assert.match(failureText, /fixture unrecoverable failure/);@@ -553,12 +562,15 @@ async function testDeepScanStdioLifecycle() {assert.equal(failedState.status, "failed");assert.equal(failedState.error, failureMessage);assert.equal(- failedState.workers.some((worker) => worker.kind === "setup"),+ failedState.workers.some(+ (worker: PersistedDeepScanWorker) => worker.kind === "setup",+ ),false,);assert.equal(failedState.workers.every(- (worker) => !["queued", "running"].includes(worker.status),+ (worker: PersistedDeepScanWorker) =>+ !["queued", "running"].includes(worker.status),),true,);@@ -573,7 +585,9 @@ async function testDeepScanStdioLifecycle() {),);const failedLateJoinText =- failedLateJoin.result?.content?.map((item) => item.text).join(" ") ?? "";+ failedLateJoin.result?.content+ ?.map((item: { text: string }) => item.text)+ .join(" ") ?? "";assert.equal(failedLateJoin.result?.isError, true);assert.equal(failedLateJoin.result?.structuredContent, undefined);assert.match(failedLateJoinText, /fixture unrecoverable failure/);@@ -586,7 +600,8 @@ async function testDeepScanStdioLifecycle() {assertNoError(toolList);assert.equal(toolList.result.tools.some(- (tool) => tool.name === "start_codex_security_deep_scan",+ (tool: { name: string }) =>+ tool.name === "start_codex_security_deep_scan",),true,"the MCP server must remain responsive after canceling one scan",@@ -642,7 +657,7 @@ async function testDeepScanStdioLifecycle() {threadId: resumedThreadId,},],- ]) {+ ] as const) {assertNoError(await server.request(id,@@ -652,7 +667,7 @@ async function testDeepScanStdioLifecycle() {);}- const restartStartIndex = (await readJsonLines(startLogPath)).length;+ const restartStartIndex = (await readLogLines(startLogPath)).length;await writeFile(restartControlPath, "before-restart");server.sendRequest(29,@@ -681,21 +696,23 @@ async function testDeepScanStdioLifecycle() {threadId: resumedThreadId,});return (- partial.workers.some((worker) => worker.status === "succeeded") &&- partial.workers.some((worker) => worker.status === "running")+ partial.workers.some(+ (worker: PersistedDeepScanWorker) => worker.status === "succeeded",+ ) &&+ partial.workers.some(+ (worker: PersistedDeepScanWorker) => worker.status === "running",+ ));}, "one completed discovery and one interrupted discovery");- const completedWorker = partial.workers.find(- (worker) => worker.status === "succeeded",- );- const completedDraft = JSON.parse(- await readFile(completedWorker.resultManifestPath, "utf8"),+ const completedWorker = partial!.workers.find(+ (worker: PersistedDeepScanWorker) => worker.status === "succeeded",);+ const completedDraft = await readJson(completedWorker.resultManifestPath);assert.equal(completedDraft.scanId, resumedScanId);assert.deepEqual(completedDraft.findings, []);await server.stop();assert.throws(- () => process.kill(server.pid, 0),+ () => process.kill(server.pid!, 0),"the original MCP server must have exited",);const paused = await runWorkbench(environment, [@@ -728,7 +745,7 @@ async function testDeepScanStdioLifecycle() {resumedScan.scanDir,"artifacts","deep_discovery",- `coordinator-heartbeat-${partial.coordinatorGeneration}.json`,+ `coordinator-heartbeat-${partial!.coordinatorGeneration}.json`,),{ force: true },);@@ -785,7 +802,7 @@ async function testDeepScanStdioLifecycle() {{ type: "text", text: instructions },]);assert.equal(- resumed.result.content.some((item) =>+ resumed.result.content.some((item: { text: string }) =>item.text.includes(resumedScanId),),true,@@ -799,18 +816,19 @@ async function testDeepScanStdioLifecycle() {assert.equal(finished.status, "succeeded");assert.equal(finished.coordinatorGeneration,- partial.coordinatorGeneration + 1,+ partial!.coordinatorGeneration + 1,);assert.equal(finished.dispatchedCount, 2);const successfulDiscoveries = finished.workers.filter(- (worker) =>+ (worker: PersistedDeepScanWorker) =>worker.kind === "discovery" && worker.status === "succeeded",);assert.equal(successfulDiscoveries.length, 2);assert.equal(successfulDiscoveries[0].id, completedWorker.id);assert.equal(finished.workers.some(- (worker) => worker.kind === "dedup" && worker.status === "succeeded",+ (worker: PersistedDeepScanWorker) =>+ worker.kind === "dedup" && worker.status === "succeeded",),true,);@@ -826,12 +844,7 @@ async function testDeepScanStdioLifecycle() {{ code: "ENOENT" },);assert.deepEqual(- JSON.parse(- await readFile(- path.join(resumedScan.scanDir, "findings.json"),- "utf8",- ),- ).findings,+ (await readJson(resumedScan.scanDir, "findings.json")).findings,[],);const completion = await restartedServer.request(@@ -848,9 +861,7 @@ async function testDeepScanStdioLifecycle() {);assertNoError(completion);const completedScan = resumed.result.structuredContent;- const sealedManifest = JSON.parse(- await readFile(completedScan.manifestPath, "utf8"),- );+ const sealedManifest = await readJson(completedScan.manifestPath);assert.equal(sealedManifest.scan.status, "completed");assert.ok(sealedManifest.scan.sealedAt);const report = await readFile(@@ -858,7 +869,7 @@ async function testDeepScanStdioLifecycle() {"utf8",);assert.ok(report.length > 0);- const executions = (await readJsonLines(startLogPath)).slice(+ const executions = (await readLogLines(startLogPath)).slice(restartStartIndex,);for (const execution of executions) {@@ -885,7 +896,7 @@ async function testDeepScanStdioLifecycle() {await restartedServer.stop();}} catch (error) {- error.message += `\nMCP stderr:\n${server.stderrText()}`;+ (error as Error).message += `\nMCP stderr:\n${server.stderrText()}`;throw error;} finally {await server.stop();@@ -894,24 +905,20 @@ async function testDeepScanStdioLifecycle() {}}-function startServer(serverPath, env) {+function startServer(serverPath: string, env: NodeJS.ProcessEnv) {return streams.startServer(serverPath, env, {cwd: pluginRoot,component: "codex_security_deep_scan",- withTimeout,- responseLabel: "JSON-RPC response",- // Non-structured diagnostics remain available in the child process on test failure.- stderrLines: [],- checkSignalCode: true,+ timeoutMessage: (id) => `Timed out waiting for JSON-RPC response ${id}.`,});}function toolCall(- name,- args,- threadId,- turnMetadata,- sandboxState = parentSandboxState,+ name: string,+ args: Record<string, unknown>,+ threadId: string,+ turnMetadata?: { model: string; reasoning_effort: string },+ sandboxState: unknown = parentSandboxState,) {return {name,@@ -924,7 +931,7 @@ function toolCall(};}-function assertReadOnlyWorkerInvocation(args) {+function assertReadOnlyWorkerInvocation(args: string[]) {assert.equal(args.includes("--sandbox"), false);assert.equal(args.includes("--add-dir"), false);assert.equal(@@ -953,8 +960,16 @@ function assertReadOnlyWorkerInvocation(args) {]);}-async function waitForScanId({ server, requestId = 10, excludedScanIds = [] }) {- let scanId;+async function waitForScanId({+ server,+ requestId = 10,+ excludedScanIds = [],+}: {+ server: ReturnType<typeof startServer>;+ requestId?: number;+ excludedScanIds?: string[];+}): Promise<string> {+ let scanId!: string;const excluded = new Set(excludedScanIds);await waitFor(async () => {const startResponse = server.response(requestId);@@ -969,13 +984,21 @@ async function waitForScanId({ server, requestId = 10, excludedScanIds = [] }) {(event) =>event.event === "coordinator_started" && !excluded.has(event.scanId),);- scanId = coordinatorStarted?.scanId;+ scanId = coordinatorStarted?.scanId!;return typeof scanId === "string";}, "target-based Deep Scan bootstrap");return scanId;}-async function getDeepScan({ environment, scanId, threadId }) {+async function getDeepScan({+ environment,+ scanId,+ threadId,+}: {+ environment: NodeJS.ProcessEnv;+ scanId: string;+ threadId: string;+}) {const result = await runWorkbench(environment, ["get-deep-scan","--scan-id",@@ -986,20 +1009,18 @@ async function getDeepScan({ environment, scanId, threadId }) {return result.deepScan;}-async function waitForDeepScanWorker({ environment, scanId, threadId }) {- let worker;+async function waitForDeepScanWorker(input: Parameters<typeof getDeepScan>[0]) {await waitFor(async () => {- const deepScan = await getDeepScan({ environment, scanId, threadId });- worker = deepScan.workers.find(- (candidate) =>+ const deepScan = await getDeepScan(input);+ const worker = deepScan.workers.find(+ (candidate: PersistedDeepScanWorker) =>candidate.kind === "discovery" && candidate.status === "running",);return worker !== undefined;}, "active Standard scan worker");- return worker;}-function discoveryPromptContext(prompt) {+function discoveryPromptContext(prompt: string) {const match = prompt.match(/```json\n([\s\S]*?)\n```/u);assert.ok(match,@@ -1008,7 +1029,7 @@ function discoveryPromptContext(prompt) {return JSON.parse(match[1]);}-async function runWorkbench(environment, args) {+async function runWorkbench(environment: NodeJS.ProcessEnv, args: string[]) {const python = process.env.PYTHON?.trim() || "python3";const { stdout } = await execFileAsync(python, [workbenchPath, ...args], {cwd: pluginRoot,@@ -1019,114 +1040,112 @@ async function runWorkbench(environment, args) {return JSON.parse(stdout);}-async function writeFakeCodex(executablePath) {+async function writeFakeCodex(executablePath: string) {await writeFile(executablePath,- [- "#!/usr/bin/env node",- 'import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";',- 'import path from "node:path";',- "if (process.argv.includes('app-server')) {",- " let buffer = '';",- " process.stdin.setEncoding('utf8');",- " process.stdin.on('data', (chunk) => {",- " buffer += chunk;",- " while (true) {",- " const newline = buffer.indexOf('\\n');",- " if (newline < 0) return;",- " const line = buffer.slice(0, newline).trim();",- " buffer = buffer.slice(newline + 1);",- " if (!line) continue;",- " const message = JSON.parse(line);",- " if (message.method === 'initialized') continue;",- " let result;",- " if (message.method === 'initialize') {",- " result = { userAgent: 'fixture', codexHome: '/fixture', platformFamily: 'unix', platformOs: 'macos' };",- " } else if (message.method === 'config/read') {",- " result = { config: { default_permissions: 'codex_security_deep_scan_worker', permissions: { codex_security_deep_scan_worker: { extends: ':read-only', filesystem: { ':root': 'read' }, network: { enabled: false } } } }, origins: {}, layers: null };",- " } else if (message.method === 'permissionProfile/list') {",- " result = { data: [{ id: 'codex_security_deep_scan_worker', description: null, allowed: true }], nextCursor: null };",- " } else if (message.method === 'account/read') {",- " result = { account: null, requiresOpenaiAuth: true };",- " } else {",- " process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, error: { code: -32601, message: 'Method not found' } }) + '\\n');",- " continue;",- " }",- " process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result }) + '\\n');",- " }",- " });",- " process.stdin.on('end', () => process.exit(0));",- "} else {",- "let stdin = '';",- "for await (const chunk of process.stdin) stdin += chunk;",- "const context = JSON.parse(stdin.match(/```json\\n([\\s\\S]*?)\\n```/u)[1]);",- "const root = process.argv[process.argv.indexOf('--cd') + 1];",- "appendFileSync(process.env.FAKE_CODEX_START_LOG, JSON.stringify({ pid: process.pid, argv: process.argv.slice(2), stdin, hasExpectedApiKey: process.env.CODEX_API_KEY === 'synthetic-stdio-key' }) + '\\n');",- "console.log(JSON.stringify({ type: 'thread.started', thread_id: `stdio-fixture-${process.pid}` }));",- "if (existsSync(process.env.FAKE_CODEX_RESTART_CONTROL)) {",- " const phase = readFileSync(process.env.FAKE_CODEX_RESTART_CONTROL, 'utf8');",- " if (phase === 'after-restart' || context.workerLabel === 'discovery-0001') {",- " const coverage = { completeness: 'complete', surfaces: [], explicitExclusions: [], deferred: [] };",- " if (context.claimedWorkerIds) {",- " const output = path.join(root, 'deep_discovery', 'dedup', context.reducerLabel, 'output');",- " const firstResult = JSON.parse(readFileSync(path.join(root, 'deep_discovery', 'workers', 'discovery-0001', 'output', 'result.json'), 'utf8'));",- " writeFileSync(path.join(output, 'result.json'), JSON.stringify({ scanId: firstResult.scanId, findings: [], coverage }));",- " } else {",- " writeFileSync(path.join(root, 'result.json'), JSON.stringify({ scanId: context.scanId, findings: [], coverage }));",- " }",- " console.log(JSON.stringify({ type: 'item.completed', item: { id: 'message-1', type: 'agent_message', text: 'fixture completed' } }));",- " console.log(JSON.stringify({ type: 'turn.completed', usage: { input_tokens: 1, cached_input_tokens: 0, output_tokens: 1 } }));",- " process.exit(0);",- " }",- "}",- "const timer = setInterval(() => {}, 1_000);",- "const stop = (signal) => {",- " clearInterval(timer);",- " if (existsSync(process.env.FAKE_CODEX_SIGNAL_CHECKPOINT_CONTROL)) {",- " const coverage = { completeness: 'complete', surfaces: [], explicitExclusions: [], deferred: [] };",- " const checkpointDir = path.join(root, 'checkpoints');",- " mkdirSync(checkpointDir, { recursive: true });",- " writeFileSync(path.join(checkpointDir, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json'), JSON.stringify({ scanId: context.scanId, findings: [], coverage }));",- " }",- " appendFileSync(process.env.FAKE_CODEX_EXIT_LOG, JSON.stringify({ pid: process.pid, signal }) + '\\n');",- " process.exit(0);",- "};",- "process.once('SIGINT', () => stop('SIGINT'));",- "process.once('SIGTERM', () => stop('SIGTERM'));",- "}",- "",- ].join("\n"),+ `#!/usr/bin/env node+import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";+import path from "node:path";+if (process.argv.includes('app-server')) {+ let buffer = '';+ process.stdin.setEncoding('utf8');+ process.stdin.on('data', (chunk) => {+ buffer += chunk;+ while (true) {+ const newline = buffer.indexOf('\\n');+ if (newline < 0) return;+ const line = buffer.slice(0, newline).trim();+ buffer = buffer.slice(newline + 1);+ if (!line) continue;+ const message = JSON.parse(line);+ if (message.method === 'initialized') continue;+ let result;+ if (message.method === 'initialize') {+ result = { userAgent: 'fixture', codexHome: '/fixture', platformFamily: 'unix', platformOs: 'macos' };+ } else if (message.method === 'config/read') {+ result = { config: { default_permissions: 'codex_security_deep_scan_worker', permissions: { codex_security_deep_scan_worker: { extends: ':read-only', filesystem: { ':root': 'read' }, network: { enabled: false } } } }, origins: {}, layers: null };+ } else if (message.method === 'permissionProfile/list') {+ result = { data: [{ id: 'codex_security_deep_scan_worker', description: null, allowed: true }], nextCursor: null };+ } else if (message.method === 'account/read') {+ result = { account: null, requiresOpenaiAuth: true };+ } else {+ process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, error: { code: -32601, message: 'Method not found' } }) + '\\n');+ continue;+ }+ process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result }) + '\\n');+ }+ });+ process.stdin.on('end', () => process.exit(0));+} else {+const stdin = (await process.stdin.toArray()).join('');+const context = JSON.parse(stdin.match(/\`\`\`json\\n([\\s\\S]*?)\\n\`\`\`/u)[1]);+const root = process.argv[process.argv.indexOf('--cd') + 1];+appendFileSync(process.env.FAKE_CODEX_START_LOG, JSON.stringify({ pid: process.pid, argv: process.argv.slice(2), stdin, hasExpectedApiKey: process.env.CODEX_API_KEY === 'synthetic-stdio-key' }) + '\\n');+console.log(JSON.stringify({ type: 'thread.started', thread_id: \`stdio-fixture-\${process.pid}\` }));+if (existsSync(process.env.FAKE_CODEX_RESTART_CONTROL)) {+ const phase = readFileSync(process.env.FAKE_CODEX_RESTART_CONTROL, 'utf8');+ if (phase === 'after-restart' || context.workerLabel === 'discovery-0001') {+ const coverage = { completeness: 'complete', surfaces: [], explicitExclusions: [], deferred: [] };+ if (context.claimedWorkerIds) {+ const output = path.join(root, 'deep_discovery', 'dedup', context.reducerLabel, 'output');+ const firstResult = JSON.parse(readFileSync(path.join(root, 'deep_discovery', 'workers', 'discovery-0001', 'output', 'result.json'), 'utf8'));+ writeFileSync(path.join(output, 'result.json'), JSON.stringify({ scanId: firstResult.scanId, findings: [], coverage }));+ } else {+ writeFileSync(path.join(root, 'result.json'), JSON.stringify({ scanId: context.scanId, findings: [], coverage }));+ }+ console.log(JSON.stringify({ type: 'item.completed', item: { id: 'message-1', type: 'agent_message', text: 'fixture completed' } }));+ console.log(JSON.stringify({ type: 'turn.completed', usage: { input_tokens: 1, cached_input_tokens: 0, output_tokens: 1 } }));+ process.exit(0);+ }+}+setInterval(() => {}, 1_000);+const stop = (signal) => {+ if (existsSync(process.env.FAKE_CODEX_SIGNAL_CHECKPOINT_CONTROL)) {+ const coverage = { completeness: 'complete', surfaces: [], explicitExclusions: [], deferred: [] };+ const checkpointDir = path.join(root, 'checkpoints');+ mkdirSync(checkpointDir, { recursive: true });+ writeFileSync(path.join(checkpointDir, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json'), JSON.stringify({ scanId: context.scanId, findings: [], coverage }));+ }+ appendFileSync(process.env.FAKE_CODEX_EXIT_LOG, JSON.stringify({ pid: process.pid, signal }) + '\\n');+ process.exit(0);+};+process.once('SIGINT', () => stop('SIGINT'));+process.once('SIGTERM', () => stop('SIGTERM'));+}+`,);await chmod(executablePath, 0o755);}-async function writePythonWrapper(executablePath) {+async function writePythonWrapper(executablePath: string) {await writeFile(executablePath,- [- "#!/usr/bin/env node",- 'import { appendFileSync, existsSync, unlinkSync } from "node:fs";',- 'import { spawnSync } from "node:child_process";',- "const args = process.argv.slice(2);",- "const control = process.env.FAKE_WORKBENCH_CANCEL_FAILURE_CONTROL;",- "if (args[1] === 'cancel-scan') {",- " appendFileSync(process.env.FAKE_WORKBENCH_CANCEL_LOG, JSON.stringify(args) + '\\n');",- "}",- "if (args[1] === 'cancel-scan' && control && existsSync(control)) {",- " unlinkSync(control);",- " console.error('injected cancel-scan failure');",- " process.exit(1);",- "}",- "const result = spawnSync(process.env.REAL_PYTHON || 'python3', args, { stdio: 'inherit' });",- "if (result.error) throw result.error;",- "process.exit(result.status ?? 1);",- "",- ].join("\n"),+ `#!/usr/bin/env node+import { appendFileSync, existsSync, unlinkSync } from "node:fs";+import { spawnSync } from "node:child_process";+const args = process.argv.slice(2);+const control = process.env.FAKE_WORKBENCH_CANCEL_FAILURE_CONTROL;+if (args[1] === 'cancel-scan') {+ appendFileSync(process.env.FAKE_WORKBENCH_CANCEL_LOG, JSON.stringify(args) + '\\n');+}+if (args[1] === 'cancel-scan' && control && existsSync(control)) {+ unlinkSync(control);+ console.error('injected cancel-scan failure');+ process.exit(1);+}+const result = spawnSync(process.env.REAL_PYTHON || 'python3', args, { stdio: 'inherit' });+if (result.error) throw result.error;+process.exit(result.status ?? 1);+`,);await chmod(executablePath, 0o755);}-function assertCanceled(response, scanId, scanDir) {+function assertCanceled(+ response: Awaited<ReturnType<ReturnType<typeof startServer>["request"]>>,+ scanId: string,+ scanDir: string,+) {assertNoError(response);const instructions = response.result.structuredContent.instructions;assert.match(@@ -1148,18 +1167,18 @@ function assertCanceled(response, scanId, scanDir) {]);}-function assertProcessAlive(pid) {+function assertProcessAlive(pid: number) {assert.doesNotThrow(() => process.kill(pid, 0),`expected fake Codex process ${pid} to remain alive`,);}-async function waitForJsonLines(filePath, count) {- let lines = [];+async function waitForJsonLines(filePath: string, count: number) {+ let lines: Awaited<ReturnType<typeof readLogLines>> = [];await waitFor(async () => {- lines = await readJsonLines(filePath);+ lines = await readLogLines(filePath);return lines.length >= count;},`${count} record(s) in ${path.basename(filePath)}`,@@ -1167,40 +1186,21 @@ async function waitForJsonLines(filePath, count) {return lines;}-async function readJsonLines(filePath) {- try {- return (await readFile(filePath, "utf8"))- .split("\n")- .filter(Boolean)- .map((line) => JSON.parse(line));- } catch (error) {- if (error?.code === "ENOENT") return [];+function readLogLines(filePath: string) {+ return readJsonLines(filePath).catch((error) => {+ if ((error as NodeJS.ErrnoException)?.code === "ENOENT") return [];throw error;- }+ });}-async function waitFor(predicate, label, timeoutMs = 15_000) {- const deadline = Date.now() + timeoutMs;+async function waitFor(+ predicate: () => boolean | Promise<boolean>,+ label: string,+) {+ const deadline = Date.now() + 15_000;while (Date.now() < deadline) {if (await predicate()) return;await delay(25);}throw new Error(`Timed out waiting for ${label}.`);}--async function withTimeout(promise, timeoutMs, label) {- let timer;- try {- return await Promise.race([- promise,- new Promise((_, reject) => {- timer = setTimeout(- () => reject(new Error(`Timed out waiting for ${label}.`)),- timeoutMs,- );- }),- ]);- } finally {- clearTimeout(timer);- }-}diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_store.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_store.tssimilarity index 84%rename from plugins/codex-security/mcp-app/tests/test_deep_scan_store.mjsrename to plugins/codex-security/mcp-app/tests/test_deep_scan_store.tsindex 3254f68ea..129f1fbc2 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_store.mjs+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts@@ -1,10 +1,12 @@+import { readJson } from "./support/json.ts";import { mock } from "node:test";import assert from "node:assert/strict";import { randomUUID } from "node:crypto";-import { mkdtemp, readFile, rm } from "node:fs/promises";+import { mkdtemp, rm } from "node:fs/promises";import { availableParallelism, tmpdir } from "node:os";import { join } from "node:path";-import { importSource } from "./import-module.mjs";+import { importSource } from "./import-module.ts";+import type { WorkbenchDeepScanStore as Store } from "../src/deep-scan/store.js";const { WorkbenchDeepScanStore, parseDeepScan } = await importSource(new URL("../src/deep-scan/store.ts", import.meta.url).pathname,@@ -39,8 +41,8 @@ testInvalidPersistedConfig();async function testBeginProtocolAndParsing() {const scanId = randomUUID();- const calls = [];- const runner = async (args, input) => {+ const calls: { args: string[]; input?: string }[] = [];+ const runner = async (args: string[], input?: string) => {calls.push({ args, input });return stateResult(scanId, { startDisposition: "created" });};@@ -87,8 +89,8 @@ async function testBeginProtocolAndParsing() {);const claimToken = randomUUID();- let joinedArgs;- const joinedRunner = async (args) => {+ let joinedArgs!: string[];+ const joinedRunner = async (args: string[]) => {joinedArgs = args;return stateResult(scanId, { startDisposition: "joined" });};@@ -103,13 +105,11 @@ async function testBeginProtocolAndParsing() {}async function testWriteSerializationAndRecovery() {- const calls = [];- const firstGate = Promise.withResolvers();- let first = true;- const runner = async (args) => {+ const calls: string[][] = [];+ const firstGate = Promise.withResolvers<void>();+ const runner = async (args: string[]) => {calls.push(args);- if (first) {- first = false;+ if (calls.length === 1) {await firstGate.promise;throw new Error("first write failed");}@@ -156,9 +156,9 @@ async function testWriteSerializationAndRecovery() {async function testBeginUsesTheWriteQueue() {const scanId = randomUUID();- const calls = [];- const firstGate = Promise.withResolvers();- const runner = async (args) => {+ const calls: string[][] = [];+ const firstGate = Promise.withResolvers<void>();+ const runner = async (args: string[]) => {calls.push(args);if (args[0] === "update-progress") {await firstGate.promise;@@ -179,7 +179,7 @@ async function testBeginUsesTheWriteQueue() {firstGate.resolve();await Promise.all([progress, begin]);assert.deepEqual(- calls.map((args) => args[0]),+ calls.map((args: string[]) => args[0]),["update-progress", "begin-deep-scan"],);}@@ -188,9 +188,9 @@ async function testHeartbeatBypassesBlockedWriteQueue() {const scanId = randomUUID();const handoffClaimToken = randomUUID();const scanDir = await mkdtemp(join(tmpdir(), "deep-scan-heartbeat-"));- const blockedWriteGate = Promise.withResolvers();- const calls = [];- const runner = async (args) => {+ const blockedWriteGate = Promise.withResolvers<void>();+ const calls: string[][] = [];+ const runner = async (args: string[]) => {calls.push(args);if (args[0] === "update-progress") {await blockedWriteGate.promise;@@ -219,16 +219,11 @@ async function testHeartbeatBypassesBlockedWriteQueue() {);assert.equal(renewed.coordinatorGeneration, 2);assert.deepEqual(- JSON.parse(- await readFile(- join(- scanDir,- "artifacts",- "deep_discovery",- "coordinator-heartbeat-2.json",- ),- "utf8",- ),+ await readJson(+ scanDir,+ "artifacts",+ "deep_discovery",+ "coordinator-heartbeat-2.json",),{ coordinatorGeneration: 2, updatedAt: renewed.updatedAt },);@@ -245,12 +240,11 @@ async function testHeartbeatBypassesBlockedWriteQueue() {async function testOwnershipReadClearsStaleLease() {const scanId = randomUUID();- const calls = [];+ const calls: string[][] = [];let generation = 2;- let rejectProgress = false;- const store = new WorkbenchDeepScanStore(async (args) => {+ const store = new WorkbenchDeepScanStore(async (args: string[]) => {calls.push(args);- if (args[0] === "update-progress" && rejectProgress) {+ if (args[0] === "update-progress") {throw new Error("Deep Scan coordinator lease belongs to a newer generation.",);@@ -276,7 +270,6 @@ async function testOwnershipReadClearsStaleLease() {"a confirmed newer generation must clear the cached lease before a later claim",);- rejectProgress = true;await assert.rejects(store.updateProgress({ scanId, phase: "discovery" }),/newer generation/,@@ -334,8 +327,8 @@ async function testWorkerResponseParsing() {async function testReplaceableDiscoveryFailureProtocol() {const scanId = randomUUID();const workerId = randomUUID();- let invocation;- const store = new WorkbenchDeepScanStore(async (args) => {+ let invocation!: string[];+ const store = new WorkbenchDeepScanStore(async (args: string[]) => {invocation = args;return stateResult(scanId, {deepScan: {@@ -381,7 +374,7 @@ async function testCanonicalCommitProtocol() {const reducerId = randomUUID();const resultManifestPath ="/fixture/scans/run/artifacts/deep_discovery/dedup/result.json";- const runWorkbench = mock.fn(async (args) => {+ const runWorkbench = mock.fn(async (args: string[]) => {return stateResult(scanId, { deepScan: { canonicalArtifacts: canonical } });});const store = new WorkbenchDeepScanStore(runWorkbench);@@ -408,8 +401,8 @@ async function testCanonicalCommitProtocol() {async function testTerminalProtocol() {const scanId = randomUUID();const omittedWorkerIds = [randomUUID(), randomUUID()];- const calls = [];- const store = new WorkbenchDeepScanStore(async (args) => {+ const calls: string[][] = [];+ const store = new WorkbenchDeepScanStore(async (args: string[]) => {calls.push(args);return stateResult(scanId, {deepScan: {@@ -445,8 +438,8 @@ async function testTerminalProtocol() {async function testTransientIdempotentPersistenceRetries() {for (const scenario of idempotentPersistenceScenarios()) {- const calls = [];- const store = new WorkbenchDeepScanStore(async (args) => {+ const calls: string[][] = [];+ const store = new WorkbenchDeepScanStore(async (args: string[]) => {calls.push([...args]);if (calls.length < 3) {throw Object.assign(@@ -520,8 +513,8 @@ async function testTransientTimeoutAndNestedCauseRetries() {async function testPersistenceRetriesRemainInsideTheWriteQueue() {const scanId = randomUUID();- const calls = [];- const store = new WorkbenchDeepScanStore(async (args) => {+ const calls: string[] = [];+ const store = new WorkbenchDeepScanStore(async (args: string[]) => {calls.push(args[0]);if (args[0] === "claim-deep-scan-dedup" && calls.length === 1) {throw new Error("sqlite3.OperationalError: database is locked");@@ -559,10 +552,10 @@ async function testPersistenceRetryExhaustionPreservesDiagnostics() {timeout: 30_000,},);- const calls = [];- const events = [];+ const calls: string[][] = [];+ const events: string[] = [];const originalConsoleError = console.error;- const store = new WorkbenchDeepScanStore(async (args) => {+ const store = new WorkbenchDeepScanStore(async (args: string[]) => {calls.push([...args]);if (args[0] === "claim-deep-scan-dedup") throw originalFailure;return {};@@ -578,7 +571,7 @@ async function testPersistenceRetryExhaustionPreservesDiagnostics() {promptPath: "/fixture/reducer/prompt.md",artifactDir: "/fixture/reducer/output",}),- (error) => {+ (error: Error & Record<string, unknown>) => {assert.equal(error.name, "DeepScanPersistenceError");assert.equal(error.operation, "claim-deep-scan-dedup");assert.equal(error.scanId, scanId);@@ -592,7 +585,9 @@ async function testPersistenceRetryExhaustionPreservesDiagnostics() {assert.equal(error.cause, originalFailure);assert.match(error.message, /database is locked/);assert.match(error.message, /failed after 3 attempts/);- assert.ok(Number.isInteger(error.elapsedMs) && error.elapsedMs >= 0);+ assert.ok(+ Number.isInteger(error.elapsedMs) && (error.elapsedMs as number) >= 0,+ );return true;},);@@ -650,7 +645,10 @@ async function testDeterministicPersistenceFailuresAreNotRetried() {throw expected;});- await assert.rejects(scenario.invoke(store), (error) => error === expected);+ await assert.rejects(+ scenario.invoke(store),+ (error: Error & Record<string, unknown>) => error === expected,+ );assert.equal(attempts, 1, `${diagnostic} must not be blindly replayed`);}@@ -662,7 +660,7 @@ async function testDeterministicPersistenceFailuresAreNotRetried() {const store = new WorkbenchDeepScanStore(attempts);await assert.rejects(idempotentPersistenceScenarios()[1].invoke(store),- (error) => error === canceled,+ (error: unknown) => error === canceled,);assert.equal(attempts.mock.callCount(),@@ -702,7 +700,7 @@ async function testDeterministicPersistenceFailuresAreNotRetried() {artifactDir: "/fixture/worker/output",attempt: 1,}),- (error) => error === failure,+ (error: unknown) => error === failure,);assert.equal(workerAttempts.mock.callCount(),@@ -730,7 +728,10 @@ async function testNonIdempotentMutationsAreNotRetried() {scanRoot: "/fixture/scans",});- await assert.rejects(request, (error) => error === expected);+ await assert.rejects(+ request,+ (error: Error & Record<string, unknown>) => error === expected,+ );assert.equal(attempts.mock.callCount(),1,@@ -743,118 +744,50 @@ function idempotentPersistenceScenarios() {const scanId = randomUUID();const workerId = randomUUID();const reducerId = randomUUID();- const worker = {+ const mutation = {id: workerId,- kind: "discovery",- status: "succeeded",- mergeState: "buffered",+ kind: "discovery" as const,promptPath: "/fixture/discovery/prompt.md",artifactDir: "/fixture/discovery/output",attempt: 1,- resultManifestPath: "/fixture/discovery/output/result.json",- completionSequence: 1,};+ const resultManifestPath = "/fixture/discovery/output/result.json";return [- {- operation: "upsert-deep-scan-worker",- result: stateResult(scanId, {- deepScan: {- workers: [- {- ...worker,- status: "running",- mergeState: "none",- resultManifestPath: undefined,- completionSequence: undefined,- },- ],- },- }),- invoke: (store) =>- store.updateWorker({- id: workerId,- scanId,- kind: "discovery",- status: "running",- promptPath: worker.promptPath,- artifactDir: worker.artifactDir,- attempt: worker.attempt,- }),- },- {+ ...(+ [+ { status: "running" },+ { status: "running", threadId: "thread-worker" },+ { status: "queued" },+ { status: "failed" },+ { status: "canceled" },+ { status: "succeeded", resultManifestPath },+ ] satisfies Partial<+ import("../src/deep-scan/types.js").DeepScanWorkerMutation+ >[]+ ).map((update) => ({operation: "upsert-deep-scan-worker",result: stateResult(scanId, {deepScan: {workers: [{- ...worker,- status: "running",- mergeState: "none",- resultManifestPath: undefined,- completionSequence: undefined,- sdkThreadId: "thread-worker",+ ...mutation,+ status: update.status,+ sdkThreadId: update.threadId,+ mergeState: update.status === "succeeded" ? "buffered" : "none",+ resultManifestPath: update.resultManifestPath,+ completionSequence: update.status === "succeeded" ? 1 : undefined,},],},}),- invoke: (store) =>- store.updateWorker({- id: workerId,- scanId,- kind: "discovery",- status: "running",- promptPath: worker.promptPath,- artifactDir: worker.artifactDir,- attempt: worker.attempt,- threadId: "thread-worker",- }),- },- ...["queued", "failed", "canceled"].map((status) => ({- operation: "upsert-deep-scan-worker",- result: stateResult(scanId, {- deepScan: {- workers: [- {- ...worker,- status,- mergeState: "none",- resultManifestPath: undefined,- completionSequence: undefined,- },- ],- },- }),- invoke: (store) =>- store.updateWorker({- id: workerId,- scanId,- kind: "discovery",- status,- promptPath: worker.promptPath,- artifactDir: worker.artifactDir,- attempt: worker.attempt,- }),+ invoke: (store: Store) =>+ store.updateWorker({ ...mutation, scanId, ...update }),})),- {- operation: "upsert-deep-scan-worker",- result: stateResult(scanId, { deepScan: { workers: [worker] } }),- invoke: (store) =>- store.updateWorker({- id: workerId,- scanId,- kind: "discovery",- status: "succeeded",- promptPath: worker.promptPath,- artifactDir: worker.artifactDir,- attempt: worker.attempt,- resultManifestPath: worker.resultManifestPath,- }),- },{operation: "claim-deep-scan-dedup",result: {},- invoke: (store) =>+ invoke: (store: Store) =>store.claimDedup({id: reducerId,scanId,@@ -868,12 +801,11 @@ function idempotentPersistenceScenarios() {result: stateResult(scanId, {deepScan: { canonicalArtifacts: canonical },}),- invoke: (store) =>+ invoke: (store: Store) =>store.commitDedup({id: reducerId,scanId,newFindings: 1,- canonicalArtifacts: canonical,resultManifestPath: "/fixture/reducer/output/result.json",}),},@@ -886,7 +818,7 @@ function idempotentPersistenceScenarios() {manifestPath: "/fixture/scans/run/coordinator-manifest.json",},}),- invoke: (store) =>+ invoke: (store: Store) =>store.finish({scanId,reason: "saturated",@@ -902,7 +834,7 @@ function idempotentPersistenceScenarios() {error: "Saved result publication failed: fixture publication failure",},}),- invoke: (store) =>+ invoke: (store: Store) =>store.recordStoppedPublicationFailure(scanId,"Saved result publication failed: fixture publication failure",@@ -1030,7 +962,13 @@ function testCanonicalNullAndPartialParsing() {);}-function stateResult(scanId, overrides = {}) {+function stateResult(+ scanId: string,+ overrides: {+ startDisposition?: string;+ deepScan?: Record<string, unknown> & { config?: Record<string, unknown> };+ } = {},+) {const deepScanOverride = overrides.deepScan ?? {};return {startDisposition: overrides.startDisposition,@@ -1058,14 +996,14 @@ function stateResult(scanId, overrides = {}) {};}-function flagValue(args, flag) {+function flagValue(args: string[], flag: string) {const index = args.indexOf(flag);assert.notEqual(index, -1, `missing ${flag}`);return args[index + 1];}-function repeatedFlagValues(args, flag) {- return args.flatMap((value, index) =>+function repeatedFlagValues(args: string[], flag: string) {+ return args.flatMap((value, index: number) =>value === flag ? [args[index + 1]] : [],);}diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.tssimilarity index 66%rename from plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.mjsrename to plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.tsindex eae9758e8..017fda701 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.mjs+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.ts@@ -1,4 +1,5 @@-import { temporaryDirectory } from "./support/temporary-directories.mjs";+import { readJson, writeJsonLine } from "./support/json.ts";+import { temporaryDirectory } from "./support/temporary-directories.ts";import assert from "node:assert/strict";import { execFile, spawn } from "node:child_process";import { randomUUID } from "node:crypto";@@ -6,14 +7,10 @@ import { once } from "node:events";import { mkdir, readFile, rm, writeFile } from "node:fs/promises";import path from "node:path";import { promisify } from "node:util";-import { fileURLToPath } from "node:url";-import { importModule } from "./import-module.mjs";+import { importModule } from "./import-module.ts";const execFileAsync = promisify(execFile);-const mcpAppRoot = path.resolve(- path.dirname(fileURLToPath(import.meta.url)),- "..",-);+const mcpAppRoot = path.resolve(import.meta.dirname, "..");const pluginRoot = path.resolve(mcpAppRoot, "..");const workbenchPath = path.join(pluginRoot, "scripts", "workbench_db.py");const {@@ -22,11 +19,9 @@ const {recordCodexSecurityScanDraftViaWorkbench,} = await importModule({stdin: {- contents: [- 'export { WorkbenchDeepScanStore } from "./src/deep-scan/store.ts";',- 'export { createScanArtifactContext } from "./src/artifact-context.ts";',- 'export { recordCodexSecurityScanDraftViaWorkbench } from "./src/artifact-scan-draft.ts";',- ].join("\n"),+ contents: `export { WorkbenchDeepScanStore } from "./src/deep-scan/store.ts";+export { createScanArtifactContext } from "./src/artifact-context.ts";+export { recordCodexSecurityScanDraftViaWorkbench } from "./src/artifact-scan-draft.ts";`,resolveDir: mcpAppRoot,},});@@ -39,27 +34,31 @@ await testRecoveredPublicationRejectsLateFailure();await testNoopStoppedRefreshRetainsPublicationFailure();await testConcurrentParentDraftsPreserveBothCheckpoints();-async function createWorkbenchFixture(prefix, homeName = "home") {+async function createWorkbenchFixture(prefix: string) {const fixtureRoot = await temporaryDirectory(prefix);const targetPath = path.join(fixtureRoot, "target");const environment = {...process.env,- CODEX_HOME: path.join(fixtureRoot, homeName),+ CODEX_HOME: path.join(fixtureRoot, "codex-home"),CODEX_SECURITY_STATE_DIR: path.join(fixtureRoot, "state"),};- return { fixtureRoot, targetPath, environment };+ try {+ await mkdir(targetPath, { recursive: true });+ await writeFile(path.join(targetPath, "fixture.py"), "print('fixture')\n");+ return { fixtureRoot, targetPath, environment };+ } catch (error) {+ await rm(fixtureRoot, { recursive: true, force: true });+ throw error;+ }}-function createWorkbenchRunner(- python,- environment,- options = { timeout: 30_000, maxBuffer: 4 * 1024 * 1024 },-) {- return async (args) => {+function createWorkbenchRunner(environment: NodeJS.ProcessEnv, bounded = true) {+ const python = process.env.PYTHON?.trim() || "python3";+ return async (args: string[]) => {const { stdout } = await execFileAsync(python, [workbenchPath, ...args], {cwd: pluginRoot,env: environment,- ...options,+ ...(bounded ? { timeout: 30_000, maxBuffer: 4 * 1024 * 1024 } : {}),});return JSON.parse(stdout);};@@ -69,11 +68,8 @@ async function testRecoveredPublicationRejectsLateFailure() {const { fixtureRoot, targetPath, environment } = await createWorkbenchFixture("deep-scan-publication-failure-",);- const python = process.env.PYTHON?.trim() || "python3";- const runWorkbench = createWorkbenchRunner(python, environment);+ const runWorkbench = createWorkbenchRunner(environment);try {- await mkdir(targetPath, { recursive: true });- await writeFile(path.join(targetPath, "fixture.py"), "print('fixture')\n");const store = new WorkbenchDeepScanStore(runWorkbench);const run = await store.begin({targetPath,@@ -92,23 +88,11 @@ async function testRecoveredPublicationRejectsLateFailure() {});await recordCodexSecurityScanDraftViaWorkbench(context,- {- scanId: run.scanId,- complete: false,- findings: [],- coverage: {- completeness: "partial",- surfaces: [],- explicitExclusions: [],- deferred: [- {- candidateId: "publication-recovery-candidate",- reason: "Publication recovery remains pending.",- paths: ["fixture.py"],- },- ],- },- },+ checkpoint(+ run.scanId,+ "publication-recovery-candidate",+ "Publication recovery remains pending.",+ ),runWorkbench,);await runWorkbench([@@ -148,11 +132,8 @@ async function testNoopStoppedRefreshRetainsPublicationFailure() {const { fixtureRoot, targetPath, environment } = await createWorkbenchFixture("deep-scan-noop-publication-",);- const python = process.env.PYTHON?.trim() || "python3";- const runWorkbench = createWorkbenchRunner(python, environment);+ const runWorkbench = createWorkbenchRunner(environment);try {- await mkdir(targetPath, { recursive: true });- await writeFile(path.join(targetPath, "fixture.py"), "print('fixture')\n");const store = new WorkbenchDeepScanStore(runWorkbench);const run = await store.begin({targetPath,@@ -194,15 +175,28 @@ async function testNoopStoppedRefreshRetainsPublicationFailure() {}}+function checkpoint(scanId: string, candidateId: string, reason: string) {+ return {+ scanId,+ complete: false,+ findings: [],+ coverage: {+ completeness: "partial",+ surfaces: [],+ explicitExclusions: [],+ deferred: [{ candidateId, reason, paths: ["fixture.py"] }],+ },+ };+}+async function testConcurrentParentDraftsPreserveBothCheckpoints() {const { fixtureRoot, targetPath, environment } = await createWorkbenchFixture("scan-draft-concurrency-integration-",);- const python = process.env.PYTHON?.trim() || "python3";- const rawRunWorkbench = createWorkbenchRunner(python, environment);+ const rawRunWorkbench = createWorkbenchRunner(environment);let stagedWrites = 0;- const initialWritesReady = Promise.withResolvers();- const runWorkbench = async (args) => {+ const initialWritesReady = Promise.withResolvers<void>();+ const runWorkbench = async (args: string[]) => {if (args[0] === "write-scan-draft" && ++stagedWrites <= 2) {if (stagedWrites === 2) initialWritesReady.resolve();await initialWritesReady.promise;@@ -210,8 +204,6 @@ async function testConcurrentParentDraftsPreserveBothCheckpoints() {return rawRunWorkbench(args);};try {- await mkdir(targetPath, { recursive: true });- await writeFile(path.join(targetPath, "fixture.py"), "print('fixture')\n");const run = await new WorkbenchDeepScanStore(rawRunWorkbench).begin({targetPath,scope: ".",@@ -221,33 +213,24 @@ async function testConcurrentParentDraftsPreserveBothCheckpoints() {const context = await createScanArtifactContext(run.scanId, runWorkbench, {requireRunning: true,});- const checkpoint = (candidateId) => ({- scanId: run.scanId,- complete: false,- findings: [],- coverage: {- completeness: "partial",- surfaces: [],- explicitExclusions: [],- deferred: [- {- candidateId,- reason: "Independent review remains pending.",- paths: ["fixture.py"],- },- ],- },- });await Promise.all([recordCodexSecurityScanDraftViaWorkbench(context,- checkpoint("concurrent-a"),+ checkpoint(+ run.scanId,+ "concurrent-a",+ "Independent review remains pending.",+ ),runWorkbench,),recordCodexSecurityScanDraftViaWorkbench(context,- checkpoint("concurrent-b"),+ checkpoint(+ run.scanId,+ "concurrent-b",+ "Independent review remains pending.",+ ),runWorkbench,),]);@@ -257,11 +240,13 @@ async function testConcurrentParentDraftsPreserveBothCheckpoints() {"the stale writer retries after the host rejects its digest",);- const coverage = JSON.parse(- await readFile(path.join(run.scanDir, "coverage.json"), "utf8"),- );+ const coverage = await readJson(run.scanDir, "coverage.json");assert.deepEqual(- new Set(coverage.deferred.map((item) => item.candidateId)),+ new Set(+ coverage.deferred.map(+ (item: { candidateId: string }) => item.candidateId,+ ),+ ),new Set(["concurrent-a", "concurrent-b"]),"overlapping canonical writes must merge every immutable checkpoint",);@@ -274,11 +259,8 @@ async function testLateParentDraftPreservesCheckpointWithoutOverwritingTerminalSconst { fixtureRoot, targetPath, environment } = await createWorkbenchFixture("scan-draft-cancel-integration-",);- const python = process.env.PYTHON?.trim() || "python3";- const runWorkbench = createWorkbenchRunner(python, environment);+ const runWorkbench = createWorkbenchRunner(environment);try {- await mkdir(targetPath, { recursive: true });- await writeFile(path.join(targetPath, "fixture.py"), "print('fixture')\n");const run = await new WorkbenchDeepScanStore(runWorkbench).begin({targetPath,scope: ".",@@ -288,26 +270,13 @@ async function testLateParentDraftPreservesCheckpointWithoutOverwritingTerminalSconst context = await createScanArtifactContext(run.scanId, runWorkbench, {requireRunning: true,});- const checkpoint = (candidateId) => ({- scanId: run.scanId,- complete: false,- findings: [],- coverage: {- completeness: "partial",- surfaces: [],- explicitExclusions: [],- deferred: [- {- candidateId,- reason: "Source validation remains pending.",- paths: ["fixture.py"],- },- ],- },- });await recordCodexSecurityScanDraftViaWorkbench(context,- checkpoint("early-result"),+ checkpoint(+ run.scanId,+ "early-result",+ "Source validation remains pending.",+ ),runWorkbench,);await runWorkbench([@@ -324,7 +293,11 @@ async function testLateParentDraftPreservesCheckpointWithoutOverwritingTerminalSawait assert.rejects(recordCodexSecurityScanDraftViaWorkbench(context,- checkpoint("late-result"),+ checkpoint(+ run.scanId,+ "late-result",+ "Source validation remains pending.",+ ),runWorkbench,),/not running|stopped|terminal/i,@@ -336,10 +309,10 @@ async function testLateParentDraftPreservesCheckpointWithoutOverwritingTerminalS);const stopped = await runWorkbench(["get-scan", "--scan-id", run.scanId]);assert.equal(stopped.scan.progress.status, "canceled");- const coverage = JSON.parse(- await readFile(path.join(run.scanDir, "coverage.json"), "utf8"),+ const coverage = await readJson(run.scanDir, "coverage.json");+ const pending = coverage.deferred.map(+ (item: { candidateId: string }) => item.candidateId,);- const pending = coverage.deferred.map((item) => item.candidateId);assert.ok(pending.includes("early-result"));assert.ok(!pending.includes("late-result"),@@ -355,38 +328,25 @@ async function testReducerCommitAndFinishAgainstRealWorkbench() {const { fixtureRoot, targetPath, environment } = await createWorkbenchFixture("deep-scan-store-integration-",- "codex-home",);const scanRoot = path.join(fixtureRoot, "scans");- const stateDir = path.join(fixtureRoot, "state");const codexHome = path.join(fixtureRoot, "codex-home");const threadId = "deep-scan-store-integration-thread";const python = process.env.PYTHON?.trim() || "python3";- const runWorkbench = createWorkbenchRunner(python, environment, {- maxBuffer: 4 * 1024 * 1024,- timeout: 30_000,- });+ const runWorkbench = createWorkbenchRunner(environment);const store = new WorkbenchDeepScanStore(runWorkbench);try {- await Promise.all([- mkdir(targetPath, { recursive: true }),- mkdir(scanRoot, { recursive: true }),- mkdir(stateDir, { recursive: true }),- mkdir(path.join(codexHome, "codex-security"), { recursive: true }),- ]);- await writeFile(path.join(targetPath, "fixture.py"), "print('fixture')\n");+ await mkdir(path.join(codexHome, "codex-security"), { recursive: true });await writeFile(path.join(codexHome, "codex-security", "config.toml"),- [- "[deep_scan]",- "workers = 2",- "subagents = 0",- "stop_after_no_new = 2",- "max_discovery_runs = 3",- "max_time_hours = 2.5",- "",- ].join("\n"),+ `[deep_scan]+workers = 2+subagents = 0+stop_after_no_new = 2+max_discovery_runs = 3+max_time_hours = 2.5+`,);const run = await store.begin({@@ -424,17 +384,15 @@ async function testReducerCommitAndFinishAgainstRealWorkbench();const writer = spawn(python, ["-c",- [- "import sqlite3, sys",- "connection = sqlite3.connect(sys.argv[1])",- 'connection.execute("UPDATE deep_scan_runs SET updated_at = ? WHERE scan_id = ?", ("2000-01-01T00:00:00Z", sys.argv[2]))',- "connection.commit()",- 'connection.execute("BEGIN IMMEDIATE")',- 'print("locked", flush=True)',- "sys.stdin.read(1)",- "connection.rollback()",- ].join("\n"),- path.join(stateDir, "workbench.sqlite3"),+ `import sqlite3, sys+connection = sqlite3.connect(sys.argv[1])+connection.execute("UPDATE deep_scan_runs SET updated_at = ? WHERE scan_id = ?", ("2000-01-01T00:00:00Z", sys.argv[2]))+connection.commit()+connection.execute("BEGIN IMMEDIATE")+print("locked", flush=True)+sys.stdin.read(1)+connection.rollback()`,+ path.join(fixtureRoot, "state", "workbench.sqlite3"),run.scanId,]);await once(writer.stdout, "data");@@ -457,16 +415,11 @@ async function testReducerCommitAndFinishAgainstRealWorkbench(renewed.coordinatorGeneration,owned.run.coordinatorGeneration,);- const lease = JSON.parse(- await readFile(- path.join(- run.scanDir,- "artifacts",- "deep_discovery",- `coordinator-heartbeat-${owned.run.coordinatorGeneration}.json`,- ),- "utf8",- ),+ const lease = await readJson(+ run.scanDir,+ "artifacts",+ "deep_discovery",+ `coordinator-heartbeat-${owned.run.coordinatorGeneration}.json`,);assert.deepEqual(lease, {coordinatorGeneration: owned.run.coordinatorGeneration,@@ -489,29 +442,22 @@ async function testReducerCommitAndFinishAgainstRealWorkbench(owned.run.coordinatorGeneration,);- const first = await createSucceededDiscovery(store, run, "first");- const second = await createSucceededDiscovery(store, run, "second");- const late = await createRunningDiscovery(store, run, "late");+ const first = await createDiscovery(store, run, "first", "succeeded");+ const second = await createDiscovery(store, run, "second", "succeeded");+ const late = await createDiscovery(store, run, "late", "running");if (replaceFailedReducer) {- const retired = await createWorkerFixture(run.scanDir, "retired-reducer");+ const retired = await createWorkerFixture(+ run,+ "retired-reducer",+ "dedup",+ );await store.claimDedup({- id: retired.id,- scanId: run.scanId,+ ...retired,workerIds: [first.id, second.id],- promptPath: retired.promptPath,- artifactDir: retired.artifactDir,});- const mutation = {- id: retired.id,- scanId: run.scanId,- kind: "dedup",- promptPath: retired.promptPath,- artifactDir: retired.artifactDir,- attempt: 1,- };- await store.updateWorker({ ...mutation, status: "running" });+ await store.updateWorker({ ...retired, status: "running" });await store.updateWorker({- ...mutation,+ ...retired,status: "failed",error: "Request blocked by cyberPolicy.",});@@ -520,32 +466,26 @@ async function testReducerCommitAndFinishAgainstRealWorkbench(for (const workerId of [first.id, second.id]) {assert.equal(afterRetirement.persistedWorkers.find(- (worker) => worker.id === workerId,+ (worker: { id: string }) => worker.id === workerId,)?.mergeState,"buffered","a retired reducer must release every input for its replacement",);}}- const reducer = await createReducerFixture(run.scanDir, [- first.id,- second.id,- ]);+ const reducer = await createWorkerFixture(run, "dedup-0001", "dedup");+ await writeJsonLine(reducer.resultPath, {+ schemaVersion: 1,+ consumedWorkerIds: [first.id, second.id],+ merges: [],+ });await store.claimDedup({- id: reducer.id,- scanId: run.scanId,+ ...reducer,workerIds: [first.id, second.id],- promptPath: reducer.promptPath,- artifactDir: reducer.artifactDir,});await store.updateWorker({- id: reducer.id,- scanId: run.scanId,- kind: "dedup",+ ...reducer,status: "running",- promptPath: reducer.promptPath,- artifactDir: reducer.artifactDir,- attempt: 1,threadId: "fixture-reducer-thread",});@@ -581,14 +521,8 @@ async function testReducerCommitAndFinishAgainstRealWorkbench();const acceptedLate = await store.updateWorker({- id: late.id,- scanId: run.scanId,- kind: "discovery",+ ...late,status: "succeeded",- promptPath: late.promptPath,- artifactDir: late.artifactDir,- attempt: 1,- threadId: "fixture-late-thread",resultManifestPath: late.resultPath,});assert.equal(acceptedLate.status, "succeeded");@@ -666,24 +600,14 @@ async function testReducerCommitAndFinishAgainstRealWorkbench(async function testExpiredDeadlineWithoutCompletedDiscoveryAgainstRealWorkbench() {const { fixtureRoot, targetPath, environment } = await createWorkbenchFixture("deep-scan-store-zero-discovery-",- "codex-home",);- const scanRoot = path.join(fixtureRoot, "scans");- const stateDir = path.join(fixtureRoot, "state");const codexHome = path.join(fixtureRoot, "codex-home");const threadId = "deep-scan-store-zero-discovery-thread";- const python = process.env.PYTHON?.trim() || "python3";- const runWorkbench = createWorkbenchRunner(python, environment, {});+ const runWorkbench = createWorkbenchRunner(environment, false);const store = new WorkbenchDeepScanStore(runWorkbench);try {- await Promise.all([- mkdir(targetPath, { recursive: true }),- mkdir(scanRoot, { recursive: true }),- mkdir(stateDir, { recursive: true }),- mkdir(path.join(codexHome, "codex-security"), { recursive: true }),- ]);- await writeFile(path.join(targetPath, "fixture.py"), "print('fixture')\n");+ await mkdir(path.join(codexHome, "codex-security"), { recursive: true });await writeFile(path.join(codexHome, "codex-security", "config.toml"),"[deep_scan]\nworkers = 1\nmax_discovery_runs = 3\nmax_time_hours = 1e-12\n",@@ -693,7 +617,7 @@ async function testExpiredDeadlineWithoutCompletedDiscoveryAgainstRealWorkbench(targetPath,scope: ".",threadId,- scanRoot,+ scanRoot: path.join(fixtureRoot, "scans"),});assert.equal(run.config.maxTimeHours, 1e-12);const owned = await store.claimCoordinator({@@ -743,79 +667,55 @@ async function testExpiredDeadlineWithoutCompletedDiscoveryAgainstRealWorkbench(}}-async function createSucceededDiscovery(store, run, label) {- const fixture = await createWorkerFixture(run.scanDir, `discovery-${label}`);- await store.updateWorker({- id: fixture.id,- scanId: run.scanId,- kind: "discovery",- status: "queued",- promptPath: fixture.promptPath,- artifactDir: fixture.artifactDir,- attempt: 1,- });- await store.updateWorker({- id: fixture.id,- scanId: run.scanId,- kind: "discovery",- status: "running",- promptPath: fixture.promptPath,- artifactDir: fixture.artifactDir,- attempt: 1,- threadId: `fixture-${label}-thread`,- });- const persisted = await store.updateWorker({- id: fixture.id,- scanId: run.scanId,- kind: "discovery",- status: "succeeded",- promptPath: fixture.promptPath,- artifactDir: fixture.artifactDir,- attempt: 1,- threadId: `fixture-${label}-thread`,- resultManifestPath: fixture.resultPath,- });- return { ...fixture, ...persisted };-}--async function createRunningDiscovery(store, run, label) {- const fixture = await createWorkerFixture(run.scanDir, `discovery-${label}`);- await store.updateWorker({- id: fixture.id,- scanId: run.scanId,- kind: "discovery",- status: "running",- promptPath: fixture.promptPath,- artifactDir: fixture.artifactDir,- attempt: 1,- threadId: `fixture-${label}-thread`,- });- return fixture;-}--async function createReducerFixture(scanDir, workerIds) {- const fixture = await createWorkerFixture(scanDir, "dedup-0001");- await writeFile(- fixture.resultPath,- `${JSON.stringify({ schemaVersion: 1, consumedWorkerIds: workerIds, merges: [] })}\n`,+async function createDiscovery(+ store: import("../src/deep-scan/types.js").DeepScanStore,+ run: import("../src/deep-scan/types.js").DeepScanRunState,+ label: string,+ finalState: "running" | "succeeded",+) {+ const fixture = await createWorkerFixture(+ run,+ `discovery-${label}`,+ "discovery",);- return fixture;+ await writeFile(fixture.resultPath, "{}\n");+ if (finalState === "succeeded") {+ await store.updateWorker({ ...fixture, status: "queued" });+ }+ const started = { ...fixture, threadId: `fixture-${label}-thread` };+ await store.updateWorker({ ...started, status: "running" });+ if (finalState === "succeeded") {+ await store.updateWorker({+ ...started,+ status: "succeeded",+ resultManifestPath: fixture.resultPath,+ });+ }+ return started;}-async function createWorkerFixture(scanDir, label) {- const root = path.join(scanDir, "artifacts", "deep_discovery", label);+async function createWorkerFixture(+ run: import("../src/deep-scan/types.js").DeepScanRunState,+ label: string,+ kind: "discovery" | "dedup",+) {+ const root = path.join(run.scanDir, "artifacts", "deep_discovery", label);const artifactDir = path.join(root, "output");const promptPath = path.join(root, "prompt.md");- const resultPath = path.join(artifactDir, "result.json");await mkdir(artifactDir, { recursive: true });- await Promise.all([- writeFile(promptPath, "fixture prompt\n"),- writeFile(resultPath, "{}\n"),- ]);- return { id: randomUUID(), artifactDir, promptPath, resultPath };+ await writeFile(promptPath, "fixture prompt\n");+ return {+ id: randomUUID(),+ scanId: run.scanId,+ kind,+ artifactDir,+ promptPath,+ resultPath: path.join(artifactDir, "result.json"),+ attempt: 1,+ };}-async function createCanonicalFixture(scanDir) {+async function createCanonicalFixture(scanDir: string) {const paths = {inScopeFilesPath: path.join(scanDir,@@ -830,14 +730,12 @@ async function createCanonicalFixture(scanDir) {"candidate_ledger.jsonl",),};- await Promise.all([- writePrivateFile(paths.inScopeFilesPath, "fixture.py\n"),- writePrivateFile(paths.candidateLedgerPath, ""),- ]);+ await writePrivateFile(paths.inScopeFilesPath, "fixture.py\n");+ await writePrivateFile(paths.candidateLedgerPath, "");return paths;}-async function writePrivateFile(filePath, content) {+async function writePrivateFile(filePath: string, content: string) {await mkdir(path.dirname(filePath), { recursive: true });await writeFile(filePath, content, { mode: 0o600 });}diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_templates.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_templates.tssimilarity index 70%rename from plugins/codex-security/mcp-app/tests/test_deep_scan_templates.mjsrename to plugins/codex-security/mcp-app/tests/test_deep_scan_templates.tsindex f8ac9e838..c3f54252e 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_templates.mjs+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_templates.ts@@ -1,6 +1,6 @@import { readFile } from "node:fs/promises";import assert from "node:assert/strict";-import { importSource } from "./import-module.mjs";+import { importSource } from "./import-module.ts";const { renderDedupPrompt, renderDiscoveryPrompt } = await importSource(new URL("../src/deep-scan/templates.ts", import.meta.url).pathname,@@ -34,27 +34,7 @@ assert.match(rendered, /preserve literal \{\{DISCOVERY_CONTEXT_JSON\}\} text/);assert.match(rendered, /record_codex_security_scan_draft/);assert.match(rendered, /coverage\.deferred/);const discoveryContext = firstJsonBlock(rendered);-assert.deepEqual(discoveryContext, {- scanId: "a0d89285-66b7-4e4f-b51a-e21b93b7081b",- pluginRoot: "/fixture/plugins/codex-security",- targetPath: "/fixture/repository",- scope: ".",- userContext:- "preserve literal {{DISCOVERY_CONTEXT_JSON}} text and https://security.example.test/callback",- workerLabel: "discovery-0001",- subagents: 3,-});-for (const field of [- "artifactDir",- "threatModelPath",- "inScopeFilesPath",- "candidateLedgerPath",- "artifactSchemas",- "rankInputPath",- "deepReviewInputPath",-]) {- assert.equal(Object.hasOwn(discoveryContext, field), false);-}+assert.deepEqual(discoveryContext, discoveryInput);const feedbackPath ="/fixture/scans/run/artifacts/01_context/false_positive_feedback.json";@@ -73,19 +53,6 @@ assert.deepEqual(dedupContext, {reducerLabel: "dedup-0001",claimedWorkerIds: ["worker-001"],});-for (const field of [- "artifactDir",- "previousCandidateLedgerPath",- "previousReducerResultPath",- "discoveries",- "canonicalOutputs",- "resultPath",- "rawCandidatesPath",- "previousInventoryPath",- "artifactSchemas",-]) {- assert.equal(Object.hasOwn(dedupContext, field), false);-}const previousReduction = firstJsonBlock(renderDedupPrompt("dedup-0002", []));assert.deepEqual(previousReduction, {@@ -93,7 +60,7 @@ assert.deepEqual(previousReduction, {claimedWorkerIds: [],});-function firstJsonBlock(prompt) {+function firstJsonBlock(prompt: string) {const match = prompt.match(/```json\n([\s\S]*?)\n```/);assert.ok(match, "prompt should contain a JSON context block");return JSON.parse(match[1]);diff --git a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.mjs b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.tssimilarity index 89%rename from plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.mjsrename to plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.tsindex f0a6649d6..de8d4ac06 100644--- a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.mjs+++ b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts@@ -1,13 +1,15 @@+import { temporaryDirectory } from "./support/temporary-directories.ts";+import { readJson, writeJson } from "./support/json.ts";import { gitText } from "../scripts/git.mjs";-import { assertNoError } from "./assertions.mjs";-import { readOnlyParentSandboxState } from "./sandbox-state.mjs";+import { assertNoError } from "./assertions.ts";+import { consumeStreamLines, writeMessage } from "./support/streams.ts";+import { readOnlyParentSandboxState } from "./sandbox-state.ts";import assert from "node:assert/strict";import { execFileSync, spawn } from "node:child_process";-import { createHash, randomUUID } from "node:crypto";+import { hash, randomUUID } from "node:crypto";import {chmod,mkdir,- mkdtemp,readFile,realpath,rm,@@ -15,27 +17,33 @@ import {} from "node:fs/promises";import { tmpdir } from "node:os";import path from "node:path";-import { fileURLToPath } from "node:url";-const sourcePluginRoot = path.resolve(- path.dirname(fileURLToPath(import.meta.url)),- "../..",-);+const continuationOptions = [+ {+ label: "Continue",+ description: "Continue the current workflow.",+ },+ {+ label: "Cancel",+ description: "Leave the current workflow paused.",+ },+];++const sourcePluginRoot = path.resolve(import.meta.dirname, "../..");const pluginRoot = process.env.CODEX_SECURITY_TEST_PLUGIN_ROOT? path.resolve(process.env.CODEX_SECURITY_TEST_PLUGIN_ROOT): path.resolve(sourcePluginRoot, "../../sdk/typescript/_bundled_plugin");const mcpAppRoot = path.join(sourcePluginRoot, "mcp-app");-const pluginManifest = JSON.parse(- await readFile(path.join(pluginRoot, ".codex-plugin", "plugin.json"), "utf8"),+const pluginManifest = await readJson(+ pluginRoot,+ ".codex-plugin",+ "plugin.json",);const PLUGIN_VERSION = pluginManifest.version;assert.equal(typeof PLUGIN_VERSION, "string");const parentSandboxState = readOnlyParentSandboxState(pluginRoot);-const installedPluginRoot = pluginRoot;-const serverPath = path.join(installedPluginRoot, "mcp", "server.mjs");-const mcpConfig = JSON.parse(- await readFile(path.join(installedPluginRoot, ".mcp.json"), "utf8"),-);+const serverPath = path.join(pluginRoot, "mcp", "server.mjs");+const mcpConfig = await readJson(pluginRoot, ".mcp.json");assert.equal(mcpConfig.mcpServers["codex-security"].command,"./scripts/launch_codex_security_mcp",@@ -131,21 +139,15 @@ assert.match(/throw new Error\(error\.stderr\.trim\(\),\s*\{\s*cause:\s*error\s*\}\)/,"Workbench failures must preserve subprocess exit, signal, and stderr diagnostics.",);-const target = await mkdtemp(path.join(tmpdir(), "codex-security-target-"));-const replacementTarget = await mkdtemp(- path.join(tmpdir(), "codex-security-replacement-"),-);-const gitTarget = await mkdtemp(- path.join(tmpdir(), "codex-security-git-target-"),-);-const stateDir = await mkdtemp(path.join(tmpdir(), "codex-security-state-"));-const scanRoot = await mkdtemp(- path.join(tmpdir(), "codex-security-scan-root-"),+const target = await temporaryDirectory("codex-security-target-");+const replacementTarget = await temporaryDirectory(+ "codex-security-replacement-",);+const gitTarget = await temporaryDirectory("codex-security-git-target-");+const stateDir = path.join(tmpdir(), randomUUID());+const scanRoot = await temporaryDirectory("codex-security-scan-root-");const resolvedScanRoot = await realpath(scanRoot);-const launchCwd = await mkdtemp(- path.join(tmpdir(), "codex-security-launch-cwd-"),-);+const launchCwd = await temporaryDirectory("codex-security-launch-cwd-");await mkdir(path.join(target, "src"));await writeFile(path.join(target, "src/a.py"), "vulnerable\n");execFileSync("git", ["init", "-q", gitTarget]);@@ -183,13 +185,16 @@ function startTestServer({command = process.execPath,cwd,env = {},+}: {+ args?: string[];+ command?: string;+ cwd: string;+ env?: NodeJS.ProcessEnv;}) {- const childEnvironment = { ...process.env };+ const childEnvironment = { ...process.env, ...env };for (const [name, value] of Object.entries(env)) {if (value === undefined) {delete childEnvironment[name];- } else {- childEnvironment[name] = value;}}const childProcess = spawn(command, args, {@@ -197,46 +202,32 @@ function startTestServer({env: childEnvironment,stdio: ["pipe", "pipe", "inherit"],});- const responses = [];- let stdout = "";- childProcess.stdout.setEncoding("utf8");- childProcess.stdout.on("data", (chunk) => {- stdout += chunk;- let newlineIndex = stdout.indexOf("\n");- while (newlineIndex >= 0) {- const line = stdout.slice(0, newlineIndex).trim();- stdout = stdout.slice(newlineIndex + 1);- if (line) responses.push(JSON.parse(line));- newlineIndex = stdout.indexOf("\n");- }- });+ const responses: ReturnType<typeof JSON.parse>[] = [];+ consumeStreamLines(childProcess.stdout, (line) =>+ responses.push(JSON.parse(line)),+ );return {- notify(method, params = {}) {- childProcess.stdin.write(- `${JSON.stringify({ jsonrpc: "2.0", method, params })}\n`,- );+ notify(method: string, params = {}) {+ writeMessage(childProcess, { jsonrpc: "2.0", method, params });},- sendRequest(id, method, params = {}) {- childProcess.stdin.write(- `${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`,- );+ sendRequest(id: number, method: string, params = {}) {+ writeMessage(childProcess, { jsonrpc: "2.0", id, method, params });},- sendResponse(id, result) {- childProcess.stdin.write(- `${JSON.stringify({ jsonrpc: "2.0", id, result })}\n`,- );+ sendResponse(id: string, result: unknown) {+ writeMessage(childProcess, { jsonrpc: "2.0", id, result });},- sendError(id, code, message) {- childProcess.stdin.write(- `${JSON.stringify({- jsonrpc: "2.0",- id,- error: { code, message },- })}\n`,- );+ sendError(id: string, code: number, message: string) {+ writeMessage(childProcess, {+ jsonrpc: "2.0",+ id,+ error: { code, message },+ });},- async waitForMessage(predicate, description = "matching JSON-RPC message") {+ async waitForMessage(+ predicate: (message: ReturnType<typeof JSON.parse>) => boolean,+ description = "matching JSON-RPC message",+ ) {const started = Date.now();while (Date.now() - started < 30000) {const message = responses.find(predicate);@@ -245,10 +236,8 @@ function startTestServer({}throw new Error(`Timed out waiting for ${description}`);},- async requestAndWait(id, method, params = {}) {- childProcess.stdin.write(- `${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`,- );+ async requestAndWait(id: number, method: string, params = {}) {+ writeMessage(childProcess, { jsonrpc: "2.0", id, method, params });const started = Date.now();while (Date.now() - started < 30000) {const response = responses.find((candidate) => candidate.id === id);@@ -289,9 +278,7 @@ const testServer = startTestServer({const requestAndWait = testServer.requestAndWait;async function assertBundledNodeLauncher() {- const emptyPath = await mkdtemp(- path.join(tmpdir(), "codex-security-empty-path-"),- );+ const emptyPath = await temporaryDirectory("codex-security-empty-path-");const launcherPath = path.join(pluginRoot,"scripts",@@ -385,7 +372,7 @@ async function assertMissingPythonError() {});assert.equal(response.result.isError, true);const errorText = response.result.content- .map((item) => item.text)+ .map((item: { text: string }) => item.text).join(" ");assert.match(errorText, /could not start its Python 3 helper/);assert.match(errorText, /bundled Python runtime/);@@ -401,8 +388,8 @@ async function assertMissingPythonError() {async function assertWorkbenchStdinFailureDoesNotCrashServer() {if (process.platform === "win32") return;- const helperRoot = await mkdtemp(- path.join(tmpdir(), "codex-security-early-exit-helper-"),+ const helperRoot = await temporaryDirectory(+ "codex-security-early-exit-helper-",);const helper = path.join(helperRoot, "python");await writeFile(helper, "#!/bin/sh\nexit 2\n");@@ -462,16 +449,7 @@ async function assertUnavailableUserInputFallback() {header: "Continue?",id: "continue_scan",question: "Should Codex Security continue?",- options: [- {- label: "Continue",- description: "Continue the current workflow.",- },- {- label: "Cancel",- description: "Leave the current workflow paused.",- },- ],+ options: continuationOptions,},],},@@ -486,9 +464,7 @@ async function assertUnavailableUserInputFallback() {}async function assertWorkspaceWorksWithoutUiCapability() {- const nonUiStateDir = await mkdtemp(- path.join(tmpdir(), "codex-security-non-ui-state-"),- );+ const nonUiStateDir = path.join(tmpdir(), randomUUID());const nonUiServer = startTestServer({cwd: pluginRoot,env: { CODEX_SECURITY_STATE_DIR: nonUiStateDir },@@ -524,12 +500,8 @@ async function assertWorkspaceWorksWithoutUiCapability() {}async function assertHeadlessStandardScanWorksWithoutUiCapability() {- const headlessStateDir = await mkdtemp(- path.join(tmpdir(), "codex-security-headless-state-"),- );- const headlessScanRoot = await mkdtemp(- path.join(tmpdir(), "codex-security-headless-scans-"),- );+ const headlessStateDir = path.join(tmpdir(), randomUUID());+ const headlessScanRoot = path.join(tmpdir(), randomUUID());const headlessServer = startTestServer({cwd: pluginRoot,env: {@@ -705,8 +677,8 @@ async function assertHeadlessStandardScanWorksWithoutUiCapability() {}async function assertDeepScanPersistsRetryableWorkerStartupError() {- const fixtureRoot = await mkdtemp(- path.join(tmpdir(), "codex-security-deep-inventory-"),+ const fixtureRoot = await temporaryDirectory(+ "codex-security-deep-inventory-",);const fixtureTarget = path.join(fixtureRoot, "repository");const fixtureState = path.join(fixtureRoot, "state");@@ -778,8 +750,9 @@ async function assertDeepScanPersistsRetryableWorkerStartupError() {),);assert.equal(deepScan.status, "running");- startupErrorWorker = deepScan.workers.find((worker) =>- worker.error?.includes("missing-deep-scan-codex"),+ startupErrorWorker = deepScan.workers.find(+ (worker: { status: string; error?: string }) =>+ worker.error?.includes("missing-deep-scan-codex"),);if (startupErrorWorker) break;}@@ -817,12 +790,10 @@ async function assertDeepScanPersistsRetryableWorkerStartupError() {"Saved result publication failed: fixture retained result publication failure";execFileSync(process.env.PYTHON?.trim() || "python3", ["-c",- [- "import sqlite3, sys",- "with sqlite3.connect(sys.argv[1]) as connection:",- " updated = connection.execute(\"UPDATE deep_scan_runs SET status = 'canceled', phase = 'terminal', cancel_requested = 1, error_message = ? WHERE scan_id = ?\", (sys.argv[2], sys.argv[3]))",- " assert updated.rowcount == 1",- ].join("\n"),+ `import sqlite3, sys+with sqlite3.connect(sys.argv[1]) as connection:+ updated = connection.execute("UPDATE deep_scan_runs SET status = 'canceled', phase = 'terminal', cancel_requested = 1, error_message = ? WHERE scan_id = ?", (sys.argv[2], sys.argv[3]))+ assert updated.rowcount == 1`,path.join(fixtureState, "workbench.sqlite3"),publicationFailure,scan.scanId,@@ -840,7 +811,7 @@ async function assertDeepScanPersistsRetryableWorkerStartupError() {},);const publicationFailureText = canceledWithPublicationFailure.result.content- .map((item) => item.text)+ .map((item: { text: string }) => item.text).join(" ");assert.equal(canceledWithPublicationFailure.result.isError, true);assert.equal(@@ -881,16 +852,7 @@ async function assertUserInputFailureLogging() {header: "Continue?",id: "continue_scan",question: "Should Codex Security continue?",- options: [- {- label: "Continue",- description: "Continue the current workflow.",- },- {- label: "Cancel",- description: "Leave the current workflow paused.",- },- ],+ options: continuationOptions,},],},@@ -937,9 +899,7 @@ async function assertUserInputFailureLogging() {async function assertBundledPythonRuntime() {if (process.platform === "win32") return;- const runtimeHome = await mkdtemp(- path.join(tmpdir(), "codex-security-runtime-home-"),- );+ const runtimeHome = await temporaryDirectory("codex-security-runtime-home-");const bundledPythonPath = path.join(runtimeHome,".cache/codex-runtimes/codex-primary-runtime/dependencies/python/bin/python3",@@ -977,12 +937,10 @@ async function assertBundledPythonRuntime() {await mkdir(path.dirname(bundledPythonPath), { recursive: true });await writeFile(bundledPythonPath,- [- "#!/bin/sh",- 'printf bundled > "$CODEX_SECURITY_BUNDLED_PYTHON_MARKER"',- 'exec "$CODEX_SECURITY_TEST_SYSTEM_PYTHON" "$@"',- "",- ].join("\n"),+ `#!/bin/sh+printf bundled > "$CODEX_SECURITY_BUNDLED_PYTHON_MARKER"+exec "$CODEX_SECURITY_TEST_SYSTEM_PYTHON" "$@"+`,{ mode: 0o755 },);assertNoError(@@ -998,91 +956,83 @@ async function assertBundledPythonRuntime() {}}-async function writeCompletedContract(scanDir, scanId, snapshotDigest) {- const targetId = `target_sha256_${createHash("sha256")- .update(`local-workspace\0${await realpath(target)}`)- .digest("hex")}`;- await writeFile(- path.join(scanDir, "findings.json"),- JSON.stringify({- documentType: "codex-security.findings",- schemaVersion: "1.0",- scanId,- findings: [- {- ruleId: "path-traversal.archive-extraction",- identity: { anchor: "archive-entry-write-without-containment" },- title: "Fixture finding",- summary: "An attacker-controlled path reaches a filesystem write.",- severity: { level: "high" },- confidence: { level: "high", rationale: "Direct source trace." },- taxonomy: { category: "path-traversal", cwe: ["CWE-22"] },- locations: [{ path: "src/a.py", startLine: 1 }],- remediation:- "Reject archive entries that escape the extraction root.",- provenance: { source: "local_plugin" },- },- {- ruleId: "fixture.informational",- identity: { anchor: "informational-observation" },- title: "Fixture informational observation",- summary: "A low-risk implementation detail is worth recording.",- severity: { level: "informational" },- confidence: { level: "high", rationale: "Direct source inspection." },- taxonomy: { category: "hardening", cwe: [] },- locations: [{ path: "src/info.py", startLine: 2 }],- remediation: "Consider hardening this implementation detail.",- provenance: { source: "local_plugin" },- },- ],- }),- );- await writeFile(- path.join(scanDir, "coverage.json"),- JSON.stringify({- documentType: "codex-security.coverage",- schemaVersion: "1.0",- scanId,- mode: "repository",- completeness: "complete",- inventoryStrategy: "repository",- includePaths: ["."],- excludePaths: [],- surfaces: [- {- id: "surface_fixture",- label: "Fixture surface",- disposition: "reported",- receiptRefs: [],- },- ],- explicitExclusions: [],- deferred: [],- }),- );- await writeFile(- path.join(scanDir, "scan-manifest.json"),- JSON.stringify({- documentType: "codex-security.scan-manifest",- schemaVersion: "1.0",- scan: {- id: scanId,- producer: { name: "codex-security-plugin", version: PLUGIN_VERSION },- status: "completed",- startedAt: "2026-06-02T18:00:00Z",- completedAt: "2026-06-02T18:09:00Z",- target: {- kind: "directory_snapshot",- targetId,- displayName: path.basename(target),- snapshotDigest,- },- scope: { includePaths: ["."], excludePaths: [] },- coverageRef: "coverage.json",- findingsRef: "findings.json",+async function writeCompletedContract(+ scanDir: string,+ scanId: string,+ snapshotDigest: string,+) {+ const targetId = `target_sha256_${hash("sha256", `local-workspace\0${await realpath(target)}`)}`;+ await writeJson(path.join(scanDir, "findings.json"), {+ documentType: "codex-security.findings",+ schemaVersion: "1.0",+ scanId,+ findings: [+ {+ ruleId: "path-traversal.archive-extraction",+ identity: { anchor: "archive-entry-write-without-containment" },+ title: "Fixture finding",+ summary: "An attacker-controlled path reaches a filesystem write.",+ severity: { level: "high" },+ confidence: { level: "high", rationale: "Direct source trace." },+ taxonomy: { category: "path-traversal", cwe: ["CWE-22"] },+ locations: [{ path: "src/a.py", startLine: 1 }],+ remediation: "Reject archive entries that escape the extraction root.",+ provenance: { source: "local_plugin" },},- }),- );+ {+ ruleId: "fixture.informational",+ identity: { anchor: "informational-observation" },+ title: "Fixture informational observation",+ summary: "A low-risk implementation detail is worth recording.",+ severity: { level: "informational" },+ confidence: { level: "high", rationale: "Direct source inspection." },+ taxonomy: { category: "hardening", cwe: [] },+ locations: [{ path: "src/info.py", startLine: 2 }],+ remediation: "Consider hardening this implementation detail.",+ provenance: { source: "local_plugin" },+ },+ ],+ });+ await writeJson(path.join(scanDir, "coverage.json"), {+ documentType: "codex-security.coverage",+ schemaVersion: "1.0",+ scanId,+ mode: "repository",+ completeness: "complete",+ inventoryStrategy: "repository",+ includePaths: ["."],+ excludePaths: [],+ surfaces: [+ {+ id: "surface_fixture",+ label: "Fixture surface",+ disposition: "reported",+ receiptRefs: [],+ },+ ],+ explicitExclusions: [],+ deferred: [],+ });+ await writeJson(path.join(scanDir, "scan-manifest.json"), {+ documentType: "codex-security.scan-manifest",+ schemaVersion: "1.0",+ scan: {+ id: scanId,+ producer: { name: "codex-security-plugin", version: PLUGIN_VERSION },+ status: "completed",+ startedAt: "2026-06-02T18:00:00Z",+ completedAt: "2026-06-02T18:09:00Z",+ target: {+ kind: "directory_snapshot",+ targetId,+ displayName: path.basename(target),+ snapshotDigest,+ },+ scope: { includePaths: ["."], excludePaths: [] },+ coverageRef: "coverage.json",+ findingsRef: "findings.json",+ },+ });}try {@@ -1108,7 +1058,8 @@ try {const trustedAccessToolList = await requestAndWait(9600, "tools/list");assertNoError(trustedAccessToolList);const trustedAccessTool = trustedAccessToolList.result.tools.find(- (tool) => tool.name === "get_codex_security_daybreak_access",+ (tool: { name: string }) =>+ tool.name === "get_codex_security_daybreak_access",);assert.ok(trustedAccessTool,@@ -1503,10 +1454,11 @@ try {});const launcher = toolList.result.tools.find(- (tool) => tool.name === "open_codex_security_workspace",+ (tool: { name: string }) => tool.name === "open_codex_security_workspace",);const startPromptOnlyScan = toolList.result.tools.find(- (tool) => tool.name === "start_codex_security_prompt_only_scan",+ (tool: { name: string }) =>+ tool.name === "start_codex_security_prompt_only_scan",);assert.match(startPromptOnlyScan.description,@@ -1514,43 +1466,50 @@ try {"Prompt-only scan instructions must align Diff callers with checkpointed handoffs",);const startHeadlessStandardScan = toolList.result.tools.find(- (tool) => tool.name === "start_codex_security_standard_scan",+ (tool: { name: string }) =>+ tool.name === "start_codex_security_standard_scan",);const getScan = toolList.result.tools.find(- (tool) => tool.name === "get_codex_security_scan",+ (tool: { name: string }) => tool.name === "get_codex_security_scan",);const recoverScanResults = toolList.result.tools.find(- (tool) => tool.name === "recover_codex_security_scan_results",+ (tool: { name: string }) =>+ tool.name === "recover_codex_security_scan_results",);const listScans = toolList.result.tools.find(- (tool) => tool.name === "list_codex_security_scans",+ (tool: { name: string }) => tool.name === "list_codex_security_scans",);const listGlobalFindings = toolList.result.tools.find(- (tool) => tool.name === "list_codex_security_global_findings",+ (tool: { name: string }) =>+ tool.name === "list_codex_security_global_findings",);const listRepositories = toolList.result.tools.find(- (tool) => tool.name === "list_codex_security_repositories",+ (tool: { name: string }) =>+ tool.name === "list_codex_security_repositories",);const getScanContext = toolList.result.tools.find(- (tool) => tool.name === "get_codex_security_scan_context",+ (tool: { name: string }) => tool.name === "get_codex_security_scan_context",);const updateScanContext = toolList.result.tools.find(- (tool) => tool.name === "update_codex_security_scan_context",+ (tool: { name: string }) =>+ tool.name === "update_codex_security_scan_context",);const updateScanContextFromApp = toolList.result.tools.find(- (tool) => tool.name === "update_codex_security_scan_context_from_app",+ (tool: { name: string }) =>+ tool.name === "update_codex_security_scan_context_from_app",);const submit = toolList.result.tools.find(- (tool) => tool.name === "submit_codex_security_setup",+ (tool: { name: string }) => tool.name === "submit_codex_security_setup",);const inspectTarget = toolList.result.tools.find(- (tool) => tool.name === "inspect_codex_security_target",+ (tool: { name: string }) => tool.name === "inspect_codex_security_target",);const inspectSetup = toolList.result.tools.find(- (tool) => tool.name === "inspect_codex_security_setup",+ (tool: { name: string }) => tool.name === "inspect_codex_security_setup",);const requestUserInput = toolList.result.tools.find(- (tool) => tool.name === "request_codex_security_user_input",+ (tool: { name: string }) =>+ tool.name === "request_codex_security_user_input",);assert.ok(requestUserInput,@@ -1700,16 +1659,7 @@ try {header: "Decline?",id: "decline_request",question: "Decline this Codex Security input request?",- options: [- {- label: "Continue",- description: "Continue the current workflow.",- },- {- label: "Cancel",- description: "Leave the current workflow paused.",- },- ],+ options: continuationOptions,},],},@@ -1738,16 +1688,7 @@ try {header: "Cancel?",id: "cancel_request",question: "Cancel this Codex Security input request?",- options: [- {- label: "Continue",- description: "Continue the current workflow.",- },- {- label: "Cancel",- description: "Leave the current workflow paused.",- },- ],+ options: continuationOptions,},],},@@ -1768,67 +1709,81 @@ try {status: "cancelled",});const start = toolList.result.tools.find(- (tool) => tool.name === "start_codex_security_scan",+ (tool: { name: string }) => tool.name === "start_codex_security_scan",);const startDeepScan = toolList.result.tools.find(- (tool) => tool.name === "start_codex_security_deep_scan",+ (tool: { name: string }) => tool.name === "start_codex_security_deep_scan",);const cancel = toolList.result.tools.find(- (tool) => tool.name === "cancel_codex_security_scan",+ (tool: { name: string }) => tool.name === "cancel_codex_security_scan",);const cancelFromApp = toolList.result.tools.find(- (tool) => tool.name === "cancel_codex_security_scan_from_app",+ (tool: { name: string }) =>+ tool.name === "cancel_codex_security_scan_from_app",);const markHandoff = toolList.result.tools.find(- (tool) => tool.name === "mark_codex_security_scan_handoff_delivered",+ (tool: { name: string }) =>+ tool.name === "mark_codex_security_scan_handoff_delivered",);const claimHandoff = toolList.result.tools.find(- (tool) => tool.name === "claim_codex_security_scan_handoff_delivery",+ (tool: { name: string }) =>+ tool.name === "claim_codex_security_scan_handoff_delivery",);const releaseHandoff = toolList.result.tools.find(- (tool) => tool.name === "release_codex_security_scan_handoff_delivery",+ (tool: { name: string }) =>+ tool.name === "release_codex_security_scan_handoff_delivery",);const attachHandoff = toolList.result.tools.find(- (tool) => tool.name === "attach_codex_security_scan_continuation_thread",+ (tool: { name: string }) =>+ tool.name === "attach_codex_security_scan_continuation_thread",);const progress = toolList.result.tools.find(- (tool) => tool.name === "update_codex_security_scan_progress",+ (tool: { name: string }) =>+ tool.name === "update_codex_security_scan_progress",);const complete = toolList.result.tools.find(- (tool) => tool.name === "complete_codex_security_scan",+ (tool: { name: string }) => tool.name === "complete_codex_security_scan",);const fail = toolList.result.tools.find(- (tool) => tool.name === "fail_codex_security_scan",+ (tool: { name: string }) => tool.name === "fail_codex_security_scan",);const setFindingTriage = toolList.result.tools.find(- (tool) => tool.name === "set_codex_security_finding_triage",+ (tool: { name: string }) =>+ tool.name === "set_codex_security_finding_triage",);const requestFindingRemediation = toolList.result.tools.find(- (tool) => tool.name === "request_codex_security_finding_remediation",+ (tool: { name: string }) =>+ tool.name === "request_codex_security_finding_remediation",);const requestFindingRemediationAction = toolList.result.tools.find(- (tool) => tool.name === "request_codex_security_finding_remediation_action",+ (tool: { name: string }) =>+ tool.name === "request_codex_security_finding_remediation_action",);const claimFindingRemediationResend = toolList.result.tools.find(- (tool) => tool.name === "claim_codex_security_finding_remediation_resend",+ (tool: { name: string }) =>+ tool.name === "claim_codex_security_finding_remediation_resend",);const releaseFindingRemediationClaim = toolList.result.tools.find(- (tool) => tool.name === "release_codex_security_finding_remediation_claim",+ (tool: { name: string }) =>+ tool.name === "release_codex_security_finding_remediation_claim",);const cancelFindingRemediationRequest = toolList.result.tools.find(- (tool) => tool.name === "cancel_codex_security_finding_remediation_request",+ (tool: { name: string }) =>+ tool.name === "cancel_codex_security_finding_remediation_request",);const markFindingRemediationDelivered = toolList.result.tools.find(- (tool) => tool.name === "mark_codex_security_finding_remediation_delivered",+ (tool: { name: string }) =>+ tool.name === "mark_codex_security_finding_remediation_delivered",);const setFindingRemediation = toolList.result.tools.find(- (tool) => tool.name === "set_codex_security_finding_remediation",+ (tool: { name: string }) =>+ tool.name === "set_codex_security_finding_remediation",);const exportFindings = toolList.result.tools.find(- (tool) => tool.name === "export_codex_security_findings",+ (tool: { name: string }) => tool.name === "export_codex_security_findings",);const listFindings = toolList.result.tools.find(- (tool) => tool.name === "list_codex_security_findings",+ (tool: { name: string }) => tool.name === "list_codex_security_findings",);assert.ok(launcher);assert.ok(startPromptOnlyScan);@@ -2104,7 +2059,7 @@ try {},});const missingPersistedDeepScanText = missingPersistedDeepScan.result.content- .map((item) => item.text)+ .map((item: { text: string }) => item.text).join(" ");assert.equal(missingPersistedDeepScan.result.isError, true);assert.equal(missingPersistedDeepScan.result.structuredContent, undefined);@@ -2697,6 +2652,26 @@ try {/preflightChecks derives/,);+ const successfulPreflightChecks = [+ {+ capability: "delegated_workers",+ reason: "Delegated workers are available.",+ severity: "warn",+ status: "pass",+ },+ {+ capability: "goal_tools",+ reason: "Goal tools help long scans preserve completion criteria.",+ severity: "suggest",+ status: "pass",+ },+ {+ capability: "goals_enabled",+ reason: "Goals are enabled.",+ severity: "suggest",+ status: "pass",+ },+ ];const incompletePreflightProgress = await requestAndWait(90162,"tools/call",@@ -2712,24 +2687,7 @@ try {severity: "block",status: "unknown",},- {- capability: "delegated_workers",- reason: "Delegated workers are available.",- severity: "warn",- status: "pass",- },- {- capability: "goal_tools",- reason: "Goal tools help long scans preserve completion criteria.",- severity: "suggest",- status: "pass",- },- {- capability: "goals_enabled",- reason: "Goals are enabled.",- severity: "suggest",- status: "pass",- },+ ...successfulPreflightChecks,],},},@@ -2758,24 +2716,7 @@ try {severity: "block",status: "fail",},- {- capability: "delegated_workers",- reason: "Delegated workers are available.",- severity: "warn",- status: "pass",- },- {- capability: "goal_tools",- reason: "Goal tools help long scans preserve completion criteria.",- severity: "suggest",- status: "pass",- },- {- capability: "goals_enabled",- reason: "Goals are enabled.",- severity: "suggest",- status: "pass",- },+ ...successfulPreflightChecks,],},});@@ -2810,24 +2751,7 @@ try {severity: "warn",status: "fail",},- {- capability: "delegated_workers",- reason: "Delegated workers are available.",- severity: "warn",- status: "pass",- },- {- capability: "goal_tools",- reason: "Goal tools help long scans preserve completion criteria.",- severity: "suggest",- status: "pass",- },- {- capability: "goals_enabled",- reason: "Goals are enabled.",- severity: "suggest",- status: "pass",- },+ ...successfulPreflightChecks,],},});@@ -3048,15 +2972,13 @@ try {rejectedPendingClose.result.content[0].text,/pending remediation operation/,);- const remediationPatch = [- "diff --git a/src/a.py b/src/a.py",- "--- a/src/a.py",- "+++ b/src/a.py",- "@@ -1 +1 @@",- "-vulnerable",- "+fixed",- "",- ].join("\n");+ const remediationPatch = `diff --git a/src/a.py b/src/a.py+--- a/src/a.py++++ b/src/a.py+@@ -1 +1 @@+-vulnerable++fixed+`;await writeFile(path.join(initializedScanDir, "remediation.patch"),remediationPatch,@@ -3070,7 +2992,7 @@ try {expectedVersion: 1,state: "generated",patchPath: "remediation.patch",- patchDigest: `sha256:${createHash("sha256").update(remediationPatch).digest("hex")}`,+ patchDigest: `sha256:${hash("sha256", remediationPatch)}`,summary: "Contain archive extraction under the output root.",},});@@ -3249,7 +3171,7 @@ try {assertNoError(filteredFindingsPage);assert.deepEqual(filteredFindingsPage.result.structuredContent.findingsPage.findings.map(- (finding) => finding.occurrenceId,+ (finding: { occurrenceId: string }) => finding.occurrenceId,),[occurrenceId],);@@ -3462,12 +3384,10 @@ try {const rotatedFallbackClaimToken = `recovery_${randomUUID()}`;execFileSync(process.env.PYTHON?.trim() || "python3", ["-c",- [- "import sqlite3, sys",- "with sqlite3.connect(sys.argv[1]) as connection:",- " updated = connection.execute(\"UPDATE scans SET handoff_status = 'pending', handoff_claim_token = ?, continuation_thread_id = NULL WHERE id = ?\", (sys.argv[2], sys.argv[3]))",- " assert updated.rowcount == 1",- ].join("\n"),+ `import sqlite3, sys+with sqlite3.connect(sys.argv[1]) as connection:+ updated = connection.execute("UPDATE scans SET handoff_status = 'pending', handoff_claim_token = ?, continuation_thread_id = NULL WHERE id = ?", (sys.argv[2], sys.argv[3]))+ assert updated.rowcount == 1`,path.join(stateDir, "workbench.sqlite3"),rotatedFallbackClaimToken,fallbackScanId,@@ -3491,7 +3411,8 @@ try {});assertNoError(scanList);const listedFallback = scanList.result.structuredContent.scans.find(- (scan) => scan.scanId === fallbackScanId,+ (scan: { targetId: string; scanId: string }) =>+ scan.scanId === fallbackScanId,);assert.equal(listedFallback.progress.status, "running");assert.equal("artifacts" in listedFallback, false);@@ -3502,7 +3423,8 @@ try {});assertNoError(globalFindings);const indexedFinding = globalFindings.result.structuredContent.findings.find(- (finding) => finding.occurrenceId === occurrenceId,+ (finding: { occurrenceId: string }) =>+ finding.occurrenceId === occurrenceId,);assert.equal(globalFindings.result.structuredContent.limit, 1);assert.equal(globalFindings.result.structuredContent.nextOffset, 1);@@ -3532,7 +3454,7 @@ try {assertNoError(filteredGlobalFindings);assert.deepEqual(filteredGlobalFindings.result.structuredContent.findings.map(- (finding) => finding.occurrenceId,+ (finding: { occurrenceId: string }) => finding.occurrenceId,),[occurrenceId],);@@ -3547,7 +3469,8 @@ try {assertNoError(repositories);const indexedRepository =repositories.result.structuredContent.repositories.find(- (repository) => repository.targetId === indexedFinding.targetId,+ (repository: { targetId: string }) =>+ repository.targetId === indexedFinding.targetId,);assert.equal(indexedRepository.checkoutAvailable, true);assert.equal(indexedRepository.latestScan.scanId, fallbackScanId);@@ -3555,7 +3478,8 @@ try {assert.equal(indexedRepository.scanCount,scanList.result.structuredContent.scans.filter(- (scan) => scan.targetId === indexedFinding.targetId,+ (scan: { targetId: string; scanId: string }) =>+ scan.targetId === indexedFinding.targetId,).length,);const filteredScans = await requestAndWait(94003, "tools/call", {@@ -3586,7 +3510,7 @@ try {assertNoError(filteredRepositories);assert.deepEqual(filteredRepositories.result.structuredContent.repositories.map(- (repository) => repository.targetId,+ (repository: { targetId: string }) => repository.targetId,),[indexedFinding.targetId],);diff --git a/plugins/codex-security/mcp-app/tests/test_python_command.mjs b/plugins/codex-security/mcp-app/tests/test_python_command.tssimilarity index 91%rename from plugins/codex-security/mcp-app/tests/test_python_command.mjsrename to plugins/codex-security/mcp-app/tests/test_python_command.tsindex ab814a173..eba3de41a 100644--- a/plugins/codex-security/mcp-app/tests/test_python_command.mjs+++ b/plugins/codex-security/mcp-app/tests/test_python_command.ts@@ -2,15 +2,11 @@ import assert from "node:assert/strict";import { chmod, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import path from "node:path";-import { importSource } from "./import-module.mjs";--const {+import {isUsablePythonExecutable,missingPythonHelperMessage,resolvePythonCommand,-} = await importSource(- new URL("../src/python_command.ts", import.meta.url).pathname,-);+} from "../src/python_command.ts";const windowsHome = "C:\\Users\\fixture";const windowsRoot = path.win32.join(@@ -27,12 +23,12 @@ const windowsCandidates = [path.win32.join(windowsRoot, "bin", "python.exe"),];for (const [candidateIndex, expectedCandidate] of windowsCandidates.entries()) {- const checkedCandidates = [];+ const checkedCandidates: string[] = [];assert.equal(await resolvePythonCommand({configuredPython: "",homeDirectory: windowsHome,- isUsableExecutable: async (candidate) => {+ isUsableExecutable: async (candidate: string) => {checkedCandidates.push(candidate);return candidate === expectedCandidate;},@@ -68,12 +64,12 @@ const unixCandidates = [path.posix.join(unixRoot, "bin", "python3"),path.posix.join(unixRoot, "bin", "python"),];-const checkedUnixCandidates = [];+const checkedUnixCandidates: string[] = [];assert.equal(await resolvePythonCommand({configuredPython: "",homeDirectory: unixHome,- isUsableExecutable: async (candidate) => {+ isUsableExecutable: async (candidate: string) => {checkedUnixCandidates.push(candidate);return false;},@@ -122,7 +118,7 @@ try {const pythonCommand = "/selected/python";for (const code of ["ENOENT", "EACCES", "ENOEXEC", "UNKNOWN"]) {assert.match(- missingPythonHelperMessage({ code, path: pythonCommand }, pythonCommand),+ missingPythonHelperMessage({ code, path: pythonCommand }, pythonCommand)!,/could not start its Python 3 helper/,);}diff --git a/plugins/codex-security/mcp-app/tests/test_reporter.mjs b/plugins/codex-security/mcp-app/tests/test_reporter.tssimilarity index 66%rename from plugins/codex-security/mcp-app/tests/test_reporter.mjsrename to plugins/codex-security/mcp-app/tests/test_reporter.tsindex c66a0ff7d..b4589e5dd 100644--- a/plugins/codex-security/mcp-app/tests/test_reporter.mjs+++ b/plugins/codex-security/mcp-app/tests/test_reporter.ts@@ -1,19 +1,11 @@+import { readJson, writeJson } from "./support/json.ts";import assert from "node:assert/strict";import { spawnSync } from "node:child_process";-import {- copyFile,- mkdir,- mkdtemp,- readFile,- rm,- writeFile,-} from "node:fs/promises";-import { tmpdir } from "node:os";+import { copyFile, mkdir, readFile, rm, writeFile } from "node:fs/promises";+import { temporaryDirectory } from "./support/temporary-directories.ts";import { delimiter, dirname, join } from "node:path";-const packageJson = JSON.parse(- await readFile(new URL("../package.json", import.meta.url), "utf8"),-);+const packageJson = await readJson(import.meta.dirname, "../package.json");for (const { blocked, fail } of [{ blocked: undefined, fail: false },@@ -22,18 +14,17 @@ for (const { blocked, fail } of [{ blocked: undefined, fail: true },{ blocked: "directory", fail: true },]) {- const root = await mkdtemp(join(tmpdir(), "codex-security-mcp-report-"));+ const root = await temporaryDirectory("codex-security-mcp-report-");try {- await writeFile(- join(root, "package.json"),- JSON.stringify({- scripts: { "test:mcp": packageJson.scripts["test:mcp"] },- }),- );+ await writeJson(join(root, "package.json"), {+ scripts: {+ "test:mcp": packageJson.scripts["test:mcp"],+ },+ });await mkdir(join(root, "scripts"));await copyFile(- new URL("../scripts/test_reporter.mjs", import.meta.url),- join(root, "scripts", "test_reporter.mjs"),+ new URL("../scripts/test_reporter.mts", import.meta.url),+ join(root, "scripts", "test_reporter.mts"),);await mkdir(join(root, "tests"));if (blocked === "directory") {@@ -41,11 +32,18 @@ for (const { blocked, fail } of [} else if (blocked === "file") {await mkdir(join(root, "reports", "junit.xml"), { recursive: true });}- const probe = join(root, "tests", "test_probe.mjs");await writeFile(- probe,- fail ? 'throw new Error("synthetic test failure");\n' : "",+ join(root, "tests", "test_probe.ts"),+ `const fail: boolean = ${fail};+if (fail) throw new Error("synthetic test failure");+`,);+ for (const file of ["test_probe.js", "test_removed.js"]) {+ await writeFile(+ join(root, "tests", file),+ 'throw new Error("stale JavaScript test executed");\n',+ );+ }const child = spawnSync(process.execPath, ["--run", "test:mcp"], {cwd: root,env: {diff --git a/plugins/codex-security/mcp-app/tests/test_threat_model_document.mjs b/plugins/codex-security/mcp-app/tests/test_threat_model_document.tssimilarity index 80%rename from plugins/codex-security/mcp-app/tests/test_threat_model_document.mjsrename to plugins/codex-security/mcp-app/tests/test_threat_model_document.tsindex 01d179038..c13cbfbde 100644--- a/plugins/codex-security/mcp-app/tests/test_threat_model_document.mjs+++ b/plugins/codex-security/mcp-app/tests/test_threat_model_document.ts@@ -1,7 +1,8 @@+import { temporaryDirectory } from "./support/temporary-directories.ts";+import { readJson } from "./support/json.ts";import assert from "node:assert/strict";import {mkdir,- mkdtemp,readFile,readdir,rm,@@ -10,35 +11,26 @@ import {} from "node:fs/promises";import { EventEmitter } from "node:events";import { createRequire, syncBuiltinESMExports } from "node:module";-import { tmpdir } from "node:os";import { join } from "node:path";import { PassThrough, Writable } from "node:stream";-import { fileURLToPath } from "node:url";import { test } from "node:test";-import { build } from "esbuild";-import { draftFixture } from "./scan-draft-recovery-fixture.mjs";+import { importModule } from "./import-module.ts";+import { draftFixture } from "./scan-draft-recovery-fixture.ts";-const pluginRoot = fileURLToPath(new URL("../../", import.meta.url));-const bundled = await build({- stdin: {- contents:- 'export * from "./artifact-scan-draft.ts"; export { saveThreatModelDocument } from "./threat-model-document.ts";',- resolveDir: join(pluginRoot, "mcp-app/src"),- },- bundle: true,- format: "esm",- platform: "node",- write: false,-});+const pluginRoot = join(import.meta.dirname, "../../");const {recordCodexSecurityScanDraft,recordCodexSecurityScanDraftViaWorkbench,recordCodexSecurityWorkerScanDraft,saveThreatModelDocument,scanDraftInputSchema,-} = await import(- `data:text/javascript;base64,${Buffer.from(bundled.outputFiles[0].text).toString("base64")}`-);+} = await importModule({+ stdin: {+ contents:+ 'export * from "./artifact-scan-draft.ts"; export { saveThreatModelDocument } from "./threat-model-document.ts";',+ resolveDir: join(pluginRoot, "mcp-app/src"),+ },+});const scanId = "7b95abf2-dc04-47a9-9950-53b5c2057f49";const coverage = {completeness: "partial",@@ -58,7 +50,7 @@ const threatModel = {for (const complete of [true, undefined]) {for (const publication of ["direct", "workbench"]) {test(`terminal Deep ${publication} drafts retain an omitted model (${complete})`, async () => {- const root = await mkdtemp(join(tmpdir(), "threatmodel-deep-final-"));+ const root = await temporaryDirectory("threatmodel-deep-final-");try {const { context, draft } = draftFixture(root, "deep");context.pluginRoot = pluginRoot;@@ -80,15 +72,11 @@ for (const complete of [true, undefined]) {const expectedModel = model ?? threatModel;if (publication === "direct") {await recordCodexSecurityScanDraft(context, terminal);- const manifest = JSON.parse(- await readFile(join(root, "scan-manifest.json"), "utf8"),- );+ const manifest = await readJson(root, "scan-manifest.json");assert.deepEqual(manifest.scan.threatModel, expectedModel);const checkpoints = await Promise.all(- (await readdir(join(root, "checkpoints"))).map(async (name) =>- JSON.parse(- await readFile(join(root, "checkpoints", name), "utf8"),- ),+ (await readdir(join(root, "checkpoints"))).map(+ async (name) => await readJson(root, "checkpoints", name),),);assert.ok(@@ -99,9 +87,7 @@ for (const complete of [true, undefined]) {checkpoint.coverage.deferred.length === 0,),);- const savedCoverage = JSON.parse(- await readFile(join(root, "coverage.json"), "utf8"),- );+ const savedCoverage = await readJson(root, "coverage.json");assert.deepEqual(savedCoverage.deferred, []);assert.ok((await readFile(join(root, "threatmodel.md"), "utf8")).startsWith(@@ -112,18 +98,12 @@ for (const complete of [true, undefined]) {await recordCodexSecurityScanDraftViaWorkbench(context,terminal,- async (args) => {- const staged = JSON.parse(- await readFile(- args[args.indexOf("--draft-path") + 1],- "utf8",- ),+ async (args: string[]) => {+ const staged = await readJson(+ args[args.indexOf("--draft-path") + 1],);- const checkpoint = JSON.parse(- await readFile(- args[args.indexOf("--checkpoint-path") + 1],- "utf8",- ),+ const checkpoint = await readJson(+ args[args.indexOf("--checkpoint-path") + 1],);assert.deepEqual(staged.manifest.scan.threatModel,@@ -145,26 +125,24 @@ for (const complete of [true, undefined]) {}test("terminal Deep model inheritance retries a changed canonical draft", async () => {- const root = await mkdtemp(join(tmpdir(), "threatmodel-deep-retry-"));+ const root = await temporaryDirectory("threatmodel-deep-retry-");try {const { context, draft } = draftFixture(root, "deep");context.pluginRoot = pluginRoot;await recordCodexSecurityScanDraft(context, { ...draft(), threatModel });const replacement = { format: "markdown", content: "# Concurrent model\n" };- const digests = [];+ const digests: (string | undefined)[] = [];await recordCodexSecurityScanDraftViaWorkbench(context,draft({}, true),- async (args) => {- const staged = JSON.parse(- await readFile(args[args.indexOf("--draft-path") + 1], "utf8"),- );+ async (args: string[]) => {+ const staged = await readJson(args[args.indexOf("--draft-path") + 1]);assert.ok(args.includes("--expected-draft-digest"));digests.push(args[args.indexOf("--expected-draft-digest") + 1]);if (digests.length === 1) {assert.deepEqual(staged.manifest.scan.threatModel, threatModel);const manifestPath = join(root, "scan-manifest.json");- const manifest = JSON.parse(await readFile(manifestPath, "utf8"));+ const manifest = await readJson(manifestPath);manifest.scan.threatModel = replacement;await writeFile(manifestPath, JSON.stringify(manifest));throw new Error(@@ -182,7 +160,7 @@ test("terminal Deep model inheritance retries a changed canonical draft", async});test("terminal Deep input remains checkpointed when reading the previous model fails", async () => {- const root = await mkdtemp(join(tmpdir(), "threatmodel-deep-read-failure-"));+ const root = await temporaryDirectory("threatmodel-deep-read-failure-");try {const { context, draft } = draftFixture(root, "deep");await writeFile(join(root, "scan-manifest.json"), "unfinished manifest");@@ -192,9 +170,7 @@ test("terminal Deep input remains checkpointed when reading the previous model f);const checkpoints = await readdir(join(root, "checkpoints"));assert.equal(checkpoints.length, 1);- const saved = JSON.parse(- await readFile(join(root, "checkpoints", checkpoints[0]), "utf8"),- );+ const saved = await readJson(root, "checkpoints", checkpoints[0]);assert.equal(saved.complete, true);assert.deepEqual(saved.findings, []);assert.deepEqual(saved.coverage.deferred, []);@@ -204,7 +180,7 @@ test("terminal Deep input remains checkpointed when reading the previous model f});test("model-only worker checkpoints save Markdown before findings and retain it on retry", async () => {- const root = await mkdtemp(join(tmpdir(), "threatmodel-worker-"));+ const root = await temporaryDirectory("threatmodel-worker-");try {const context = {root,@@ -260,7 +236,7 @@ test("model-only worker checkpoints save Markdown before findings and retain itfindings: [],coverage,});- const saved = JSON.parse(await readFile(join(root, "result.json"), "utf8"));+ const saved = await readJson(root, "result.json");assert.deepEqual(saved.threatModel, threatModel);assert.equal(await readFile(join(root, "threatmodel.md"), "utf8"),@@ -272,7 +248,7 @@ test("model-only worker checkpoints save Markdown before findings and retain it});test("a convenience-document failure preserves the worker result and reports a warning", async () => {- const root = await mkdtemp(join(tmpdir(), "threatmodel-write-failure-"));+ const root = await temporaryDirectory("threatmodel-write-failure-");try {await mkdir(join(root, "threatmodel.md"));const result = await recordCodexSecurityWorkerScanDraft(@@ -281,7 +257,7 @@ test("a convenience-document failure preserves the worker result and reports a w);assert.match(result.warnings[0], /threatmodel\.md could not be written/);assert.deepEqual(- JSON.parse(await readFile(join(root, "result.json"), "utf8")).threatModel,+ (await readJson(root, "result.json")).threatModel,threatModel,);} finally {@@ -290,7 +266,7 @@ test("a convenience-document failure preserves the worker result and reports a w});test("the configured worker interpreter is honored without losing the canonical model on failure", async () => {- const root = await mkdtemp(join(tmpdir(), "threatmodel-python-"));+ const root = await temporaryDirectory("threatmodel-python-");try {const result = await recordCodexSecurityWorkerScanDraft({@@ -305,7 +281,7 @@ test("the configured worker interpreter is honored without losing the canonical);assert.match(result.warnings[0], /missing-interpreter/);assert.deepEqual(- JSON.parse(await readFile(join(root, "result.json"), "utf8")).threatModel,+ (await readJson(root, "result.json")).threatModel,threatModel,);} finally {@@ -314,14 +290,15 @@ test("the configured worker interpreter is honored without losing the canonical});test("overlapping draft projections retain the latest committed model", async (t) => {- const root = await mkdtemp(join(tmpdir(), "threatmodel-concurrent-"));+ const root = await temporaryDirectory("threatmodel-concurrent-");const childProcess = createRequire(import.meta.url)("node:child_process");- const firstStarted = Promise.withResolvers();- let releaseFirst;+ const firstStarted = Promise.withResolvers<{+ provenance: { revision: string; snapshotDigest: string };+ }>();+ let releaseFirst: (() => void) | undefined;let activeRenderers = 0;let maximumRenderers = 0;- let renderCount = 0;- t.mock.method(childProcess, "spawn", (command, args) => {+ t.mock.method(childProcess, "spawn", (command: string, args: string[]) => {assert.equal(command, "fixture-python");assert.deepEqual(args, ["-I",@@ -330,7 +307,11 @@ test("overlapping draft projections retain the latest committed model", async (tjoin(pluginRoot, "scripts", "threat_model_projection.py"),"--input-json-stdin",]);- const child = new EventEmitter();+ const child = new EventEmitter() as EventEmitter & {+ stdout: PassThrough;+ stderr: PassThrough;+ stdin: Writable;+ };child.stdout = new PassThrough();child.stderr = new PassThrough();let contents = "";@@ -348,7 +329,7 @@ test("overlapping draft projections retain the latest committed model", async (tactiveRenderers -= 1;child.emit("close", 0, null);};- if (renderCount++ === 0) {+ if (!releaseFirst) {releaseFirst = finish;firstStarted.resolve(input);} else {@@ -376,9 +357,7 @@ test("overlapping draft projections retain the latest committed model", async (tconst latestCommitted = (async () => {for await (const event of events) {if (event.filename !== "result.json") continue;- const value = JSON.parse(- await readFile(join(root, "result.json"), "utf8"),- );+ const value = await readJson(root, "result.json");if (value.threatModel.content === latestModel.content) return;}})();@@ -404,7 +383,7 @@ test("overlapping draft projections retain the latest committed model", async (tcoverage,});await latestCommitted;- releaseFirst();+ releaseFirst!();const results = await Promise.all([first, latest]);assert.deepEqual(results.map((result) => result.warnings),@@ -422,11 +401,11 @@ test("overlapping draft projections retain the latest committed model", async (tlatestModel.content,);assert.deepEqual(- JSON.parse(await readFile(join(root, "result.json"), "utf8")).threatModel,+ (await readJson(root, "result.json")).threatModel,latestModel,);} finally {- await events.return();+ await events.return!();t.mock.restoreAll();syncBuiltinESMExports();await rm(root, { recursive: true, force: true });diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.mjs b/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.tssimilarity index 75%rename from plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.mjsrename to plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.tsindex ff202b81c..01bdf1792 100644--- a/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.mjs+++ b/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.ts@@ -1,3 +1,4 @@+import { readJsonLines } from "./support/json.ts";import assert from "node:assert/strict";import { execFileSync } from "node:child_process";import { randomUUID } from "node:crypto";@@ -13,10 +14,9 @@ import {} from "node:fs/promises";import { tmpdir } from "node:os";import path from "node:path";-import { setTimeout as delay } from "node:timers/promises";-import { applicationRoot as mcpAppRoot, buildServer } from "./build-server.mjs";-import * as streams from "./support/streams.mjs";+import { applicationRoot as mcpAppRoot, buildServer } from "./build-server.ts";+import * as streams from "./support/streams.ts";if (process.platform !== "win32") {await testWorkbenchStateFallback();@@ -64,15 +64,12 @@ async function testWorkbenchStateFallback() {);await mkdir(defaultState, { recursive: true });await chmod(defaultState, 0o500);- const server = startServer(- serverBundlePath,- childEnvironment({- CODEX_HOME: codexHome,- CODEX_SECURITY_SCAN_ROOT: undefined,- CODEX_SECURITY_STATE_DIR: undefined,- PYTHON: realPython,- }),- );+ const server = startServer(serverBundlePath, {+ CODEX_HOME: codexHome,+ CODEX_SECURITY_SCAN_ROOT: undefined,+ CODEX_SECURITY_STATE_DIR: undefined,+ PYTHON: realPython,+ });let fallbackState;try {await initialize(server, 1);@@ -176,12 +173,12 @@ async function testWorkbenchStateFallback() {const readFirstScanRoot = path.join(fixtureRoot, "read-first-scans");await mkdir(readFirstDefaultState, { recursive: true });await chmod(readFirstDefaultState, 0o500);- const readFirstEnvironment = childEnvironment({+ const readFirstEnvironment = {CODEX_HOME: readFirstHome,CODEX_SECURITY_SCAN_ROOT: readFirstScanRoot,CODEX_SECURITY_STATE_DIR: undefined,PYTHON: realPython,- });+ };let readFirstServer = startServer(serverBundlePath, readFirstEnvironment);try {await initialize(readFirstServer, 1);@@ -255,19 +252,15 @@ async function testWorkbenchStateFallback() {}const scanRoot = path.join(fixtureRoot, "fallback-scans");- await mkdir(scanRoot, { recursive: true });- const fallbackServer = startServer(- serverBundlePath,- childEnvironment({- CODEX_SECURITY_SCAN_ROOT: scanRoot,- CODEX_SECURITY_STATE_DIR: undefined,- FAKE_PYTHON_ALWAYS_FAIL: undefined,- FAKE_PYTHON_FAILURE: undefined,- FAKE_PYTHON_LOG: invocationLog,- FAKE_REAL_PYTHON: realPython,- PYTHON: fakePythonPath,- }),- );+ const fallbackServer = startServer(serverBundlePath, {+ CODEX_SECURITY_SCAN_ROOT: scanRoot,+ CODEX_SECURITY_STATE_DIR: undefined,+ FAKE_PYTHON_ALWAYS_FAIL: undefined,+ FAKE_PYTHON_FAILURE: undefined,+ FAKE_PYTHON_LOG: invocationLog,+ FAKE_REAL_PYTHON: realPython,+ PYTHON: fakePythonPath,+ });try {await initialize(fallbackServer, 1);const promptOnly = await startPromptOnlyScan(@@ -311,30 +304,22 @@ async function testWorkbenchStateFallback() {event: "state_fallback_pinned",reason: "persistent_sqlite_unwritable",});- assert.doesNotMatch(- JSON.stringify(events[0]),- new RegExp(escapeRegex(fixtureRoot)),- );} finally {await fallbackServer.stop();}await writeFile(invocationLog, "");const explicitStateDir = path.join(fixtureRoot, "explicit-state");- await mkdir(explicitStateDir, { recursive: true });- const explicitServer = startServer(- serverBundlePath,- childEnvironment({- CODEX_SECURITY_SCAN_ROOT: path.join(fixtureRoot, "explicit-scans"),- CODEX_SECURITY_STATE_DIR: explicitStateDir,- FAKE_PYTHON_ALWAYS_FAIL: "1",- FAKE_PYTHON_FAILURE:- "sqlite3.OperationalError: unable to open database file",- FAKE_PYTHON_LOG: invocationLog,- FAKE_REAL_PYTHON: realPython,- PYTHON: fakePythonPath,- }),- );+ const explicitServer = startServer(serverBundlePath, {+ CODEX_SECURITY_SCAN_ROOT: path.join(fixtureRoot, "explicit-scans"),+ CODEX_SECURITY_STATE_DIR: explicitStateDir,+ FAKE_PYTHON_ALWAYS_FAIL: "1",+ FAKE_PYTHON_FAILURE:+ "sqlite3.OperationalError: unable to open database file",+ FAKE_PYTHON_LOG: invocationLog,+ FAKE_REAL_PYTHON: realPython,+ PYTHON: fakePythonPath,+ });try {await initialize(explicitServer, 10);assertToolError(@@ -360,26 +345,18 @@ async function testWorkbenchStateFallback() {fixtureRoot,"inspection-first-scans",);- const inspectionFirstCodexHome = path.join(- fixtureRoot,- "inspection-first-codex-home",- );- await mkdir(inspectionFirstCodexHome, { recursive: true });- const inspectionFirstServer = startServer(- serverBundlePath,- childEnvironment({- CODEX_HOME: inspectionFirstCodexHome,- CODEX_SECURITY_SCAN_ROOT: inspectionFirstScanRoot,- CODEX_SECURITY_STATE_DIR: undefined,- FAKE_PYTHON_ALWAYS_FAIL: undefined,- FAKE_PYTHON_FAILURE:- "sqlite3.OperationalError: unable to open database file",- FAKE_PYTHON_LOG: invocationLog,- FAKE_PYTHON_PERSISTENT_SUCCESSES: "1",- FAKE_REAL_PYTHON: realPython,- PYTHON: fakePythonPath,- }),- );+ const inspectionFirstServer = startServer(serverBundlePath, {+ CODEX_HOME: path.join(fixtureRoot, "inspection-first-codex-home"),+ CODEX_SECURITY_SCAN_ROOT: inspectionFirstScanRoot,+ CODEX_SECURITY_STATE_DIR: undefined,+ FAKE_PYTHON_ALWAYS_FAIL: undefined,+ FAKE_PYTHON_FAILURE:+ "sqlite3.OperationalError: unable to open database file",+ FAKE_PYTHON_LOG: invocationLog,+ FAKE_PYTHON_PERSISTENT_SUCCESSES: "1",+ FAKE_REAL_PYTHON: realPython,+ PYTHON: fakePythonPath,+ });try {await initialize(inspectionFirstServer, 15);assertNoError(await inspectTarget(inspectionFirstServer, 16, targetPath));@@ -409,24 +386,19 @@ async function testWorkbenchStateFallback() {}await writeFile(invocationLog, "");- const provenCodexHome = path.join(fixtureRoot, "proven-codex-home");const provenScanRoot = path.join(fixtureRoot, "proven-scans");- await mkdir(provenCodexHome, { recursive: true });- const provenServer = startServer(- serverBundlePath,- childEnvironment({- CODEX_HOME: provenCodexHome,- CODEX_SECURITY_SCAN_ROOT: provenScanRoot,- CODEX_SECURITY_STATE_DIR: undefined,- FAKE_PYTHON_ALWAYS_FAIL: undefined,- FAKE_PYTHON_FAILURE:- "sqlite3.OperationalError: unable to open database file",- FAKE_PYTHON_LOG: invocationLog,- FAKE_PYTHON_PERSISTENT_SUCCESSES: "1",- FAKE_REAL_PYTHON: realPython,- PYTHON: fakePythonPath,- }),- );+ const provenServer = startServer(serverBundlePath, {+ CODEX_HOME: path.join(fixtureRoot, "proven-codex-home"),+ CODEX_SECURITY_SCAN_ROOT: provenScanRoot,+ CODEX_SECURITY_STATE_DIR: undefined,+ FAKE_PYTHON_ALWAYS_FAIL: undefined,+ FAKE_PYTHON_FAILURE:+ "sqlite3.OperationalError: unable to open database file",+ FAKE_PYTHON_LOG: invocationLog,+ FAKE_PYTHON_PERSISTENT_SUCCESSES: "1",+ FAKE_REAL_PYTHON: realPython,+ PYTHON: fakePythonPath,+ });try {await initialize(provenServer, 18);assertNoError(@@ -456,19 +428,16 @@ async function testWorkbenchStateFallback() {await writeFile(invocationLog, "");const genericScanRoot = path.join(fixtureRoot, "generic-scans");- const genericServer = startServer(- serverBundlePath,- childEnvironment({- CODEX_SECURITY_SCAN_ROOT: genericScanRoot,- CODEX_SECURITY_STATE_DIR: undefined,- FAKE_PYTHON_ALWAYS_FAIL: "1",- FAKE_PYTHON_FAILURE:- "sqlite3.OperationalError: database disk image is malformed",- FAKE_PYTHON_LOG: invocationLog,- FAKE_REAL_PYTHON: realPython,- PYTHON: fakePythonPath,- }),- );+ const genericServer = startServer(serverBundlePath, {+ CODEX_SECURITY_SCAN_ROOT: genericScanRoot,+ CODEX_SECURITY_STATE_DIR: undefined,+ FAKE_PYTHON_ALWAYS_FAIL: "1",+ FAKE_PYTHON_FAILURE:+ "sqlite3.OperationalError: database disk image is malformed",+ FAKE_PYTHON_LOG: invocationLog,+ FAKE_REAL_PYTHON: realPython,+ PYTHON: fakePythonPath,+ });try {await initialize(genericServer, 20);assertToolError(@@ -494,16 +463,7 @@ async function testWorkbenchStateFallback() {}}-function childEnvironment(overrides) {- const environment = { ...process.env };- for (const [key, value] of Object.entries(overrides)) {- if (value === undefined) delete environment[key];- else environment[key] = value;- }- return environment;-}--async function writeFakePython(executablePath) {+async function writeFakePython(executablePath: string) {await writeFile(executablePath,[@@ -529,23 +489,19 @@ async function writeFakePython(executablePath) {await chmod(executablePath, 0o755);}-function startServer(serverPath, env) {- const server = streams.startServer(serverPath, env, {- cwd: path.dirname(path.dirname(serverPath)),- component: "codex_security_workbench",- withTimeout,- responseLabel: "response",- // Tool errors are asserted from MCP responses; only structured diagnostics matter here.- checkSignalCode: false,- });- return {- request: server.request.bind(server),- stderrEvents: server.stderrEvents,- stop: server.stop,- };+function startServer(serverPath: string, env: NodeJS.ProcessEnv) {+ return streams.startServer(+ serverPath,+ { ...process.env, ...env },+ {+ cwd: path.dirname(path.dirname(serverPath)),+ component: "codex_security_workbench",+ timeoutMessage: (id) => `Timed out waiting for response ${id}`,+ },+ );}-async function initialize(server, id) {+async function initialize(server: ReturnType<typeof startServer>, id: number) {const response = await server.request(id, "initialize", {protocolVersion: "2025-11-25",capabilities: {},@@ -554,14 +510,22 @@ async function initialize(server, id) {assertNoError(response);}-function inspectTarget(server, id, targetPath) {+function inspectTarget(+ server: ReturnType<typeof startServer>,+ id: number,+ targetPath: string,+) {return server.request(id, "tools/call", {name: "inspect_codex_security_target",arguments: { targetPath },});}-function startPromptOnlyScan(server, id, targetPath) {+function startPromptOnlyScan(+ server: ReturnType<typeof startServer>,+ id: number,+ targetPath: string,+) {return server.request(id, "tools/call", {name: "start_codex_security_prompt_only_scan",arguments: { mode: "standard", scope: ".", targetPath },@@ -569,14 +533,23 @@ function startPromptOnlyScan(server, id, targetPath) {});}-function submitSetup(server, id, sessionId, targetPath) {+function submitSetup(+ server: ReturnType<typeof startServer>,+ id: number,+ sessionId: string,+ targetPath: string,+) {return server.request(id, "tools/call", {name: "submit_codex_security_setup",arguments: { sessionId, targetPath, scope: ".", mode: "standard" },});}-function startScan(server, id, sessionId) {+function startScan(+ server: ReturnType<typeof startServer>,+ id: number,+ sessionId: string,+) {return server.request(id, "tools/call", {name: "start_codex_security_scan",arguments: { sessionId },@@ -584,9 +557,9 @@ function startScan(server, id, sessionId) {}function openWorkspace(- server,- id,- targetPath,+ server: ReturnType<typeof startServer>,+ id: number,+ targetPath: string,threadId = "state-fallback-thread",) {return server.request(id, "tools/call", {@@ -597,9 +570,9 @@ function openWorkspace(}function reopenWorkspace(- server,- id,- sessionId,+ server: ReturnType<typeof startServer>,+ id: number,+ sessionId: string,threadId = "state-fallback-thread",) {return server.request(id, "tools/call", {@@ -609,16 +582,25 @@ function reopenWorkspace(});}-function assertNoError(response) {- assert.equal(response.error, undefined, response.error?.message);+function assertNoError(response: {+ error?: { message?: string };+ result?: { isError?: boolean; content?: { text?: string }[] };+}) {+ assert.equal(response.error, undefined, response.error?.message!);assert.equal(response.result?.isError,undefined,- response.result?.content?.[0]?.text,+ response.result?.content?.[0]?.text!,);}-function assertToolError(response, pattern) {+function assertToolError(+ response: {+ error?: unknown;+ result: { isError?: boolean; content: { text: string }[] };+ },+ pattern: RegExp,+) {assert.equal(response.error, undefined);assert.equal(response.result?.isError, true);assert.match(@@ -627,30 +609,12 @@ function assertToolError(response, pattern) {);}-async function readJsonLines(filePath) {- const content = await readFile(filePath, "utf8");- return content.split(/\r?\n/).filter(Boolean).map(JSON.parse);-}--async function pathExists(filePath) {+async function pathExists(filePath: string) {try {await stat(filePath);return true;} catch (error) {- if (error?.code === "ENOENT") return false;+ if ((error as NodeJS.ErrnoException)?.code === "ENOENT") return false;throw error;}}--function withTimeout(promise, timeoutMs, label) {- return Promise.race([- promise,- delay(timeoutMs).then(() => {- throw new Error(`Timed out waiting for ${label}`);- }),- ]);-}--function escapeRegex(value) {- return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");-}diff --git a/plugins/codex-security/mcp-app/tsconfig.json b/plugins/codex-security/mcp-app/tsconfig.jsonindex 47eaa1ac3..1a5114729 100644--- a/plugins/codex-security/mcp-app/tsconfig.json+++ b/plugins/codex-security/mcp-app/tsconfig.json@@ -7,13 +7,9 @@"skipLibCheck": true,"strict": true,"target": "ES2022",- "types": ["node"]+ "types": ["node"],+ "lib": ["ES2024"],+ "allowImportingTsExtensions": true},- "include": [- "main.ts",- "artifact-writer-main.ts",- "helpers-main.ts",- "server.ts",- "src"- ]+ "include": ["*.ts", "src", "tests/**/*.ts", "scripts/*.mts"]}diff --git a/sdk/typescript/.gitignore b/sdk/typescript/.gitignoreindex 3dd160171..973a09c34 100644--- a/sdk/typescript/.gitignore+++ b/sdk/typescript/.gitignore@@ -6,4 +6,3 @@/.stryker-tmp//private_release/dist//*.tsbuildinfo-/scripts/compare-test-reports.mjsdiff --git a/sdk/typescript/TESTING.md b/sdk/typescript/TESTING.mdindex 92d3d5a37..72a1abc91 100644--- a/sdk/typescript/TESTING.md+++ b/sdk/typescript/TESTING.md@@ -103,13 +103,13 @@ package checks instead of repeating the same Bun suite. MCP and Python testsrun in separate required jobs. Python uses four isolated pytest-xdist workerswith work stealing; worker crashes fail the run without automatic restarts.-`scripts/run-ci-tests.mjs` assigns the longest measured files first. Its+`scripts/run-ci-tests.mts` assigns the longest measured files first. Its`ci-test-durations.json` records per-file seconds from CI reports.Every new test file is included automatically with a one-second estimate.Refresh those estimates from the uploaded reports when adding or splittingexpensive files; estimates affect scheduling, never whether a test runs.To reproduce one Windows shard locally after building the plugin, run-`node scripts/run-ci-tests.mjs 3/7 --seed=12345`.+`node --experimental-strip-types scripts/run-ci-tests.mts 3/7 --seed=12345`.Every Bun lane uploads JUnit; Linux lanes also upload LCOV per shard. Pythonreports include case durations, and the MCP runner can upload its JUnit report.@@ -126,7 +126,7 @@ The workflow compares test identities and outcomes against the unshardeddefault run and records timings, including failed shards, in the job summary.It is not a required check or part of the release trigger.-Runner trials use Bun 1.3.13 to avoid the+Runner trials use Bun 1.4.2 to avoid the[async-module initialization bug in 1.3.14](https://github.com/oven-sh/bun/issues/31410)that breaks the Ink UI tests under isolation. Keep the trial pin until a newerrelease passes the full SDK suite in every mode. Required CI and the mutationdiff --git a/sdk/typescript/package.json b/sdk/typescript/package.jsonindex 7c7562adf..de719dab5 100644--- a/sdk/typescript/package.json+++ b/sdk/typescript/package.json@@ -53,11 +53,12 @@"clean": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\"","build": "node --run clean && node scripts/generate-deep-defaults.mjs && tsc -p tsconfig.build.json && node scripts/generate-project-config-schema.mjs && node scripts/build-dashboard.mjs","build:ci": "tsc -p tsconfig.ci.json",+ "build:evals": "node --run build && tsc -p ../../evals/tsconfig.json","build:examples": "tsc -p tsconfig.examples.json","build:plugin": "node scripts/build-plugin.mjs","check:plugin-source": "node scripts/check-plugin-source.mjs","check:package": "node scripts/check-package.mjs",- "format": "prettier --check --ignore-path .gitignore --ignore-path .prettierignore \"**/*.{cjs,mjs,js,ts,mts,tsx,json,md}\" \"../../.github/scripts/*.mts\" \"../../examples/custom-validation/*.{mts,md}\" \"../../plugins/codex-security/native/*.{mts,md}\"",+ "format": "prettier --check --ignore-path .gitignore --ignore-path .prettierignore \"**/*.{cjs,mjs,js,ts,mts,tsx,json,md}\" \"../../.github/scripts/*.mts\" \"../../examples/custom-validation/*.{mts,md}\" \"../../plugins/codex-security/native/*.{mts,md}\" \"../../evals/{deep-reducer,secret-discovery}/*.mts\" \"../../evals/triage-finding/{assertions,scripts,sastbench/assertions,sastbench/scripts}/*.mts\" \"../../evals/triage-finding/types.ts\" \"../../evals/tsconfig.json\"","generate:models": "node scripts/generate-models.cjs","generate:models:check": "node scripts/generate-models.cjs --check","lint": "tsc --noEmit",diff --git a/sdk/typescript/scripts/ci-test-durations.json b/sdk/typescript/scripts/ci-test-durations.jsonindex 400db3503..a1437d24a 100644--- a/sdk/typescript/scripts/ci-test-durations.json+++ b/sdk/typescript/scripts/ci-test-durations.json@@ -104,7 +104,6 @@"test-reports.test.ts": 0.439,"trusted-executable.test.ts": 0.266,"update-notice.test.ts": 0.037,- "windows-machine-policy.test.ts": 0.0,"workbench-canonical-paths.test.ts": 0.692,"workbench-nested-git-utf8.test.ts": 0.063,"workbench-patch.test.ts": 0.198,@@ -214,7 +213,6 @@"test-reports.test.ts": 1.222,"trusted-executable.test.ts": 0.064,"update-notice.test.ts": 0.052,- "windows-machine-policy.test.ts": 3.448,"workbench-canonical-paths.test.ts": 1.853,"workbench-nested-git-utf8.test.ts": 0.364,"workbench-patch.test.ts": 0.323,diff --git a/sdk/typescript/scripts/compare-test-reports.mts b/sdk/typescript/scripts/compare-test-reports.mtsindex eda1be406..e42476069 100644--- a/sdk/typescript/scripts/compare-test-reports.mts+++ b/sdk/typescript/scripts/compare-test-reports.mts@@ -94,18 +94,17 @@ function seconds(value: string): number {function readReport(path: string): TestReport {const parser = new SaxesParser({ xmlns: true, fileName: path });let root: SaxesTagNS | undefined;- const stack: Array<{ record: TestRecord | undefined }> = [];+ const stack: Array<TestRecord | undefined> = [];const records: TestRecord[] = [];const suites: SaxesTagNS[] = [];parser.on("opentag", (node) => {root ??= node;const name = node.uri ? `{${node.uri}}${node.local}` : node.local;const parent = stack.at(-1);- if (parent?.record) {- if (name === "failure" || name === "error")- parent.record.status = "failed";- else if (name === "skipped" && parent.record.status === "passed")- parent.record.status = "skipped";+ if (parent) {+ if (name === "failure" || name === "error") parent.status = "failed";+ else if (name === "skipped" && parent.status === "passed")+ parent.status = "skipped";}let record: TestRecord | undefined;if (name === "testcase") {@@ -113,7 +112,7 @@ function readReport(path: string): TestReport {records.push(record);}if (name === "testsuite" || name === "testsuites") suites.push(node);- stack.push({ record });+ stack.push(record);});parser.on("closetag", () => stack.pop());const content = new TextDecoder("utf-8", { fatal: true }).decode(@@ -121,7 +120,8 @@ function readReport(path: string): TestReport {);parser.write(content).close();- const cases = new Map<string, TestStatus>();+ const identities = new Set<string>();+ const cases = new Map<string, number>();for (const { attributes, status } of records) {const identity: TestIdentity = [(attributes["file"]?.value ?? "")@@ -131,11 +131,12 @@ function readReport(path: string): TestReport {attributes["name"]?.value ?? "",];const key = JSON.stringify(identity);- if (cases.has(key))+ if (identities.has(key))throw new Error(`${path}: duplicate test identity: ${identity.join(" > ")}`,);- cases.set(key, status);+ identities.add(key);+ cases.set(JSON.stringify([...identity, status]), 1);}if (!cases.size) throw new Error(`${path}: no test cases`);if (@@ -145,7 +146,7 @@ function readReport(path: string): TestReport {throw new Error(`${path}: reported test count does not match test cases`);}const failed =- [...cases.values()].includes("failed") ||+ records.some(({ status }) => status === "failed") ||suites.some((node) =>["failures", "errors"].some((field) => integer(node.attributes[field]?.value ?? "0") !== 0n,@@ -153,19 +154,12 @@ function readReport(path: string): TestReport {);if (failed) console.error(`${path}: test run failed`);const duration = seconds(root!.attributes["time"]?.value ?? "0");- const skipped = [...cases.values()].filter(- (status) => status === "skipped",- ).length;+ const skipped = records.filter(({ status }) => status === "skipped").length;console.log(`| ${basename(path)} | ${cases.size} | ${skipped} | ${duration.toFixed(2)} |`,);return {- cases: new Map(- [...cases].map(([identity, status]) => [- JSON.stringify([...(JSON.parse(identity) as TestIdentity), status]),- 1,- ]),- ),+ cases,duration,failed,};@@ -188,7 +182,7 @@ function main(): number {}if (args.values.help) {console.log(- "Compare Bun JUnit inventories before changing the required CI runner.\n\nUsage: node compare-test-reports.mjs baseline candidates [candidates ...]\nCandidates are JUnit files or glob patterns.",+ "Compare Bun JUnit inventories before changing the required CI runner.\n\nUsage: node --experimental-strip-types compare-test-reports.mts baseline candidates [candidates ...]\nCandidates are JUnit files or glob patterns.",);return 0;}diff --git a/sdk/typescript/scripts/run-ci-tests.mjs b/sdk/typescript/scripts/run-ci-tests.mtssimilarity index 88%rename from sdk/typescript/scripts/run-ci-tests.mjsrename to sdk/typescript/scripts/run-ci-tests.mtsindex f530fa555..9060bc2b8 100644--- a/sdk/typescript/scripts/run-ci-tests.mjs+++ b/sdk/typescript/scripts/run-ci-tests.mts@@ -1,22 +1,16 @@import { spawn } from "node:child_process";import { mkdir, readFile, readdir } from "node:fs/promises";-import { fileURLToPath } from "node:url";import { parseArgs } from "node:util";-import { shardTestFiles } from "./test-shards.mjs";+import { shardTestFiles } from "./test-shards.mts";const selection = /^([1-9]\d*)\/([1-9]\d*)$/.exec(process.argv[2] ?? "");const shard = Number(selection?.[1]);const count = Number(selection?.[2]);-if (- !Number.isSafeInteger(shard) ||- !Number.isSafeInteger(count) ||- shard > count-) {+if (!Number.isSafeInteger(count) || shard > count) {throw new Error(- "Usage: node scripts/run-ci-tests.mjs <shard>/<count> [bun test options]",+ "Usage: node --experimental-strip-types scripts/run-ci-tests.mts <shard>/<count> [bun test options]",);}-const packageDirectory = fileURLToPath(new URL("../", import.meta.url));const tests = (await readdir(new URL("../tests-ts/", import.meta.url))).filter((file) =>file.endsWith(".test.ts") && file !== "windows-machine-policy.test.ts",@@ -31,7 +25,7 @@ const assignments = shardTestFiles(count,timings[windows ? "windows" : "unix"],);-const selected = assignments[shard - 1];+const selected = assignments[shard - 1]!;if (selected.files.length === 0) {throw new Error(`Test shard ${shard}/${count} is empty.`);}@@ -62,7 +56,7 @@ const optionalBunOptions = {"inspect-wait": { type: "string" },"inspect-brk": { type: "string" },config: { type: "string", short: "c" },-};+} as const;const parsingArguments = [...testArguments];let parsed;let bareOptional;@@ -129,29 +123,26 @@ do {});// Bun leaves a following flag outside an optional value; parseArgs does not.bareOptional = parsed.tokens.find(- (token) =>+ (token): token is Extract<typeof token, { kind: "option" }> =>token.kind === "option" &&Object.hasOwn(optionalBunOptions, token.name) &&!token.inlineValue &&- token.value?.startsWith("-"),+ token.value?.startsWith("-")!,);if (bareOptional) {parsingArguments[bareOptional.index] = `--${bareOptional.name}=`;}} while (bareOptional);const child = spawn("bun", testArguments, {- cwd: packageDirectory,+ cwd: new URL("../", import.meta.url),env: {...process.env,- CODEX_SECURITY_TEST_TIMEOUT_MS: parsed.values.timeout,+ CODEX_SECURITY_TEST_TIMEOUT_MS: parsed.values.timeout as string | undefined,},stdio: "inherit",windowsHide: true,});-child.once("error", (error) => {- console.error(error);- process.exitCode = 1;-});+child.once("error", console.error);child.once("close", (code) => {process.exitCode = code ?? 1;});diff --git a/sdk/typescript/scripts/test-shards.d.mts b/sdk/typescript/scripts/test-shards.d.mtsdeleted file mode 100644index 51d7be683..000000000--- a/sdk/typescript/scripts/test-shards.d.mts+++ /dev/null@@ -1,5 +0,0 @@-export function shardTestFiles(- files: readonly string[],- count: number,- durations?: Readonly<Record<string, number>>,-): Array<{ files: string[]; seconds: number }>;diff --git a/sdk/typescript/scripts/test-shards.mjs b/sdk/typescript/scripts/test-shards.mtssimilarity index 75%rename from sdk/typescript/scripts/test-shards.mjsrename to sdk/typescript/scripts/test-shards.mtsindex e6e72e427..015458e26 100644--- a/sdk/typescript/scripts/test-shards.mjs+++ b/sdk/typescript/scripts/test-shards.mts@@ -1,13 +1,17 @@/** Place the longest measured files first, without omitting new test files. */-export function shardTestFiles(files, count, durations = {}) {+export function shardTestFiles(+ files: readonly string[],+ count: number,+ durations: Readonly<Record<string, number>> = {},+) {if (!Number.isSafeInteger(count) || count < 1) {throw new Error("The test shard count must be a positive integer.");}const shards = Array.from({ length: count }, () => ({- files: [],+ files: [] as string[],seconds: 0,}));- const duration = (file) => durations[file] ?? 1;+ const duration = (file: string) => durations[file] ?? 1;const ordered = [...files].sort((left, right) =>duration(right) - duration(left) ||diff --git a/sdk/typescript/tests-ts/auth.test.ts b/sdk/typescript/tests-ts/auth.test.tsindex 67f775f14..b8a4e276c 100644--- a/sdk/typescript/tests-ts/auth.test.ts+++ b/sdk/typescript/tests-ts/auth.test.ts@@ -36,18 +36,13 @@ import { basename } from "node:path";const args = [basename(process.argv[1]), ...process.argv.slice(2)];if (args.join(" ") === "login --with-api-key") {- let input = "";- for await (const chunk of process.stdin) input += chunk;+ const input = await process.stdin.reduce((text, chunk) => text + chunk, "");if (input.trim() !== "secret-key") {console.error("wrong key");process.exitCode = 2;- } else {- console.log("API key stored");}} else if (args.join(" ") === "login status") {console.log("Logged in using ChatGPT");-} else if (args.join(" ") === "logout") {- console.log("Logged out");} else if (args[0] === "login") {console.error("Listening on http://localhost:1455.");console.error("Listening on http://localhost.:1455.");@@ -64,8 +59,7 @@ if (args.join(" ") === "login --with-api-key") {console.error('Open "\\u001b[32mhttps://127.auth.example.test/device\\u001b[0m"');console.error("Enter this one-time code");console.error("\\u001b[36m8356-V2EGR\\u001b[0m");- process.exit(0);-} else {+} else if (args.join(" ") !== "logout") {console.error("unexpected args: " + args.join(" "));process.exitCode = 3;}@@ -163,9 +157,8 @@ describe("Codex authentication process boundary", () => {script,`process.stderr.write(${JSON.stringify(`Open ${url}\n${instruction}\n`)}, () => process.exit(0));\n`,);- const command = nodeCommand().command;const handle = new CodexLoginHandle(- { command },+ nodeCommand(),[script],process.env,() => {},diff --git a/sdk/typescript/tests-ts/build-plugin.test.ts b/sdk/typescript/tests-ts/build-plugin.test.tsindex 6bd3fb371..760417d41 100644--- a/sdk/typescript/tests-ts/build-plugin.test.ts+++ b/sdk/typescript/tests-ts/build-plugin.test.ts@@ -16,7 +16,7 @@ import { assertGeneratedPluginUntracked } from "../scripts/check-plugin-source.mimport { createTemporaryDirectories } from "./support/temporary-directories.js";-const temporaryDirectories = createTemporaryDirectories({ canonical: false });+const temporaryDirectories = createTemporaryDirectories(false);const execFileAsync = promisify(execFile);const temporaryDirectory = () =>diff --git a/sdk/typescript/tests-ts/deep-scan-workbench.test.ts b/sdk/typescript/tests-ts/deep-scan-workbench.test.tsindex aa23340c0..3dd034383 100644--- a/sdk/typescript/tests-ts/deep-scan-workbench.test.ts+++ b/sdk/typescript/tests-ts/deep-scan-workbench.test.ts@@ -8,7 +8,7 @@ import { createTemporaryDirectories } from "./support/temporary-directories.js";const originalClaimToken = "22222222-2222-4222-8222-222222222222";const replacementClaimToken = "33333333-3333-4333-8333-333333333333";-const temporaryDirectories = createTemporaryDirectories();+const temporaryDirectories = createTemporaryDirectories(true);afterEach(temporaryDirectories.cleanup);diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.tsindex 45340da1e..4bf2ba487 100644--- a/sdk/typescript/tests-ts/runtime.test.ts+++ b/sdk/typescript/tests-ts/runtime.test.ts@@ -1,4 +1,4 @@-import { createTemporaryDirectories } from "./support/temporary-directories.js";+import { createApiTestFixtures } from "./support/temporary-directories.js";import { parseJsonLines, jsonLines } from "./support/json.js";import { execFile, spawnSync } from "node:child_process";import * as childProcess from "node:child_process";@@ -101,18 +101,13 @@ import {import { rejecting, throwing } from "./support/errors.js";import { mockFs, restoreFs } from "./support/module-mocks.js";-const temporaryDirectories = createTemporaryDirectories();+const { temporaryDirectory, cleanup, temporaryDirectories } =+ createApiTestFixtures("codex-security-runtime-");const testPosix = process.platform === "win32" ? test.skip : test;const restoreSpawn = (spawn: typeof childProcess.spawn) =>mock.module("node:child_process", () => ({ ...childProcess, spawn }));-afterEach(temporaryDirectories.cleanup);--async function temporaryDirectory(- prefix = "codex-security-runtime-",-): Promise<string> {- return temporaryDirectories.create(prefix);-}+afterEach(cleanup);function windowsCredentialAclOutput(descriptors: readonly string[]): string {return `${descriptors.join("\n")}\nCODEX_SECURITY_ACL_COMPLETE:${descriptors.length}\n`;diff --git a/sdk/typescript/tests-ts/security-policy-helper.test.ts b/sdk/typescript/tests-ts/security-policy-helper.test.tsindex 320de33c0..aa89df944 100644--- a/sdk/typescript/tests-ts/security-policy-helper.test.ts+++ b/sdk/typescript/tests-ts/security-policy-helper.test.ts@@ -3,24 +3,24 @@ import {chmodSync,existsSync,mkdirSync,+ mkdtempSync,readFileSync,+ rmSync,symlinkSync,writeFileSync,} from "node:fs";-import { userInfo } from "node:os";+import { tmpdir, userInfo } from "node:os";import { dirname, join, relative, sep, win32 } from "node:path";import { afterEach, describe, expect, test } from "bun:test";import { PLUGIN_ROOT } from "./plugin-root.js";-import { createSyncTestDirectories } from "./support/temporary-directories.js";import { writeSource as write } from "./support/shell.js";const helper = join(PLUGIN_ROOT, "mcp", "helpers.mjs");-const { temporaryDirectory, cleanup } = createSyncTestDirectories(- "security-policy-helper-",-);+const temporaryDirectories: string[] = [];function fixture(name = "repository") {- const directory = temporaryDirectory();+ const directory = mkdtempSync(join(tmpdir(), "security-policy-helper-"));+ temporaryDirectories.push(directory);const root = join(directory, name);const output = join(directory, "output");mkdirSync(root);@@ -62,7 +62,11 @@ function expectGuidance(text: string, policies: [string, string][]): void {);}-afterEach(cleanup);+afterEach(() => {+ for (const directory of temporaryDirectories.splice(0)) {+ rmSync(directory, { recursive: true, force: true });+ }+});describe("built SECURITY.md helper", () => {test("accepts dash-prefixed paths with equals syntax", () => {diff --git a/sdk/typescript/tests-ts/skeleton.test.ts b/sdk/typescript/tests-ts/skeleton.test.tsindex 28115247f..bd9dc2510 100644--- a/sdk/typescript/tests-ts/skeleton.test.ts+++ b/sdk/typescript/tests-ts/skeleton.test.ts@@ -220,7 +220,7 @@ describe("TypeScript package skeleton", () => {expect(packageJson.scripts["test:ci"]).toContain("pnpm run test ");expect(jobs["windows-test"]?.steps).toContainEqual(expect.objectContaining({- run: "node sdk/typescript/scripts/run-ci-tests.mjs ${{ matrix.shard }}/7",+ run: "node --experimental-strip-types sdk/typescript/scripts/run-ci-tests.mts ${{ matrix.shard }}/7",}),);});diff --git a/sdk/typescript/tests-ts/support/shell.ts b/sdk/typescript/tests-ts/support/shell.tsindex c2d891e3f..292530a36 100644--- a/sdk/typescript/tests-ts/support/shell.ts+++ b/sdk/typescript/tests-ts/support/shell.ts@@ -58,3 +58,14 @@ export function nodeCommand() {}).trim(),};}++export async function readSubprocess(+ child: Bun.Subprocess<"ignore", "pipe" | "ignore", "pipe">,+) {+ const [status, stdout, stderr] = await Promise.all([+ child.exited,+ new Response(child.stdout).text(),+ new Response(child.stderr).text(),+ ]);+ return { status, stdout, stderr };+}diff --git a/sdk/typescript/tests-ts/support/temporary-directories.ts b/sdk/typescript/tests-ts/support/temporary-directories.tsindex cfa1b0cf0..27b1cfad0 100644--- a/sdk/typescript/tests-ts/support/temporary-directories.ts+++ b/sdk/typescript/tests-ts/support/temporary-directories.ts@@ -1,44 +1,19 @@-import { mkdtempSync, realpathSync, rmSync } from "node:fs";-import { chmod, mkdtemp, realpath, rm, writeFile } from "node:fs/promises";-import { tmpdir } from "node:os";+import { chmod, writeFile } from "node:fs/promises";import { join } from "node:path";import { copyCompletedScanFixture } from "../plugin-root.js";+import {+ createTemporaryDirectories,+ temporaryDirectory as createPluginTemporaryDirectory,+} from "../../../../plugins/codex-security/mcp-app/tests/support/temporary-directories.ts";-export function createTemporaryDirectories({- canonical = true,-}: { canonical?: boolean } = {}) {- const directories: string[] = [];-- return {- track(path: string): void {- directories.push(path);- },-- async create(prefix: string): Promise<string> {- const directory = await mkdtemp(join(tmpdir(), prefix));- const path = canonical ? await realpath(directory) : directory;- directories.push(path);- return path;- },-- async cleanup(): Promise<void> {- await Promise.all(- directories- .splice(0)- .map((path) => rm(path, { recursive: true, force: true })),- );- },- };-}+export { createTemporaryDirectories };export function createApiTestFixtures(prefix = "codex-security-api-",canonicalize = true,) {- const temporaryDirectories = createTemporaryDirectories({- canonical: canonicalize,- });+ const temporaryDirectories = createTemporaryDirectories(canonicalize);return {temporaryDirectories,cleanup: temporaryDirectories.cleanup,@@ -55,35 +30,9 @@ export function createApiTestFixtures(};}-export function createSyncTestDirectories(- prefix: string,- canonicalize = false,-) {- const temporaryDirectories: string[] = [];- return {- temporaryDirectories,- cleanup(): void {- for (const directory of temporaryDirectories.splice(0)) {- rmSync(directory, { recursive: true, force: true });- }- },- temporaryDirectory(directoryPrefix = prefix): string {- const created = temporaryDirectorySync(directoryPrefix);- const path = canonicalize ? realpathSync(created) : created;- temporaryDirectories.push(path);- return path;- },- };-}--export function temporaryDirectorySync(prefix: string): string {- return mkdtempSync(join(tmpdir(), prefix));-}--export async function temporaryDirectory(+export function temporaryDirectory(prefix: string,canonicalize = true,): Promise<string> {- const path = await mkdtemp(join(tmpdir(), prefix));- return canonicalize ? realpath(path) : path;+ return createPluginTemporaryDirectory(prefix, canonicalize);}diff --git a/sdk/typescript/tests-ts/support/test-subprocess.ts b/sdk/typescript/tests-ts/support/test-subprocess.tsindex c840aa5ee..d25121e9c 100644--- a/sdk/typescript/tests-ts/support/test-subprocess.ts+++ b/sdk/typescript/tests-ts/support/test-subprocess.ts@@ -1,5 +1,4 @@import { spawnSync } from "node:child_process";-import { fileURLToPath } from "node:url";import { expect } from "bun:test";export function runTestInSubprocess(file: string, name: string): boolean {@@ -12,7 +11,7 @@ export function runTestInSubprocess(file: string, name: string): boolean {process.execPath,["test", "--timeout", timeout, "--test-name-pattern", pattern, file],{- cwd: fileURLToPath(new URL("../../", import.meta.url)),+ cwd: new URL("../../", import.meta.url),encoding: "utf8",env: { ...process.env, CODEX_SECURITY_ISOLATED_TEST: identity },windowsHide: true,diff --git a/sdk/typescript/tests-ts/test-reports.test.ts b/sdk/typescript/tests-ts/test-reports.test.tsindex dccf8d5e0..341858145 100644--- a/sdk/typescript/tests-ts/test-reports.test.ts+++ b/sdk/typescript/tests-ts/test-reports.test.ts@@ -1,52 +1,15 @@-import {- mkdir,- mkdtemp,- readFile,- rm,- symlink,- writeFile,-} from "node:fs/promises";-import { tmpdir } from "node:os";+import { mkdir, readFile, writeFile } from "node:fs/promises";import { join } from "node:path";-import { fileURLToPath } from "node:url";-import {- afterAll,- afterEach,- beforeAll,- describe,- expect,- test,-} from "bun:test";-import { bashCommand, runCommand } from "./support/shell.js";+import { afterEach, describe, expect, test } from "bun:test";+import { bashCommand, readSubprocess } from "./support/shell.js";import { createTemporaryDirectories } from "./support/temporary-directories.js";+// Node 22.13 emits this upstream notice when executing TypeScript directly.+const typeStrippingNotice =+ /^(?:\(node:\d+\) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time\n\(Use `node --trace-warnings \.\.\.` to show where the warning was created\)\n)?$/u;+const bash = bashCommand();-const packageRoot = fileURLToPath(new URL("..", import.meta.url));-let buildRoot: string;-beforeAll(async () => {- buildRoot = await mkdtemp(join(tmpdir(), "codex-security-ci-build-"));- const result = await runCommand(- "node",- [- join(packageRoot, "node_modules", "typescript", "bin", "tsc"),- "--project",- join(packageRoot, "tsconfig.ci.json"),- "--outDir",- buildRoot,- ],- { timeout: 30_000 },- );- expect(result.status, result.stdout + result.stderr).toBe(0);- await symlink(- join(packageRoot, "node_modules"),- join(buildRoot, "sdk", "typescript", "node_modules"),- process.platform === "win32" ? "junction" : "dir",- );-}, 30_000);-afterAll(async () => {- if (buildRoot) await rm(buildRoot, { recursive: true, force: true });-});-const directories = createTemporaryDirectories({ canonical: false });+const directories = createTemporaryDirectories(false);afterEach(directories.cleanup);async function fixtures() {@@ -77,13 +40,8 @@ async function compare(baseline: string, ...candidates: string[]) {const child = Bun.spawn({cmd: ["node",- join(- buildRoot,- "sdk",- "typescript",- "scripts",- "compare-test-reports.mjs",- ),+ "--experimental-strip-types",+ join(import.meta.dirname, "..", "scripts", "compare-test-reports.mts"),baseline,...candidates,],@@ -91,12 +49,7 @@ async function compare(baseline: string, ...candidates: string[]) {stdout: "pipe",stderr: "pipe",});- const [status, stdout, stderr] = await Promise.all([- child.exited,- new Response(child.stdout).text(),- new Response(child.stderr).text(),- ]);- return { status, stdout, stderr };+ return readSubprocess(child);}describe("JUnit inventory comparison", () => {@@ -122,8 +75,8 @@ describe("JUnit inventory comparison", () => {"reports/runner-windows-latest-shard-*.xml",];const mock = `node() {- printf '%s\\n' "$3"- [[ "$3" != "$CODEX_SECURITY_TEST_FAIL_REPORT" ]]+ printf '%s\\n' "$4"+ [[ "$4" != "$CODEX_SECURITY_TEST_FAIL_REPORT" ]]}`;const summary = join(fixture.root, "summary.md");for (const failedReport of ["", expected[0]!]) {@@ -141,10 +94,7 @@ describe("JUnit inventory comparison", () => {},timeout: 10_000,});- const [status, stderr] = await Promise.all([- child.exited,- new Response(child.stderr).text(),- ]);+ const { status, stderr } = await readSubprocess(child);expect(status, stderr).toBe(failedReport === "" ? 0 : 1);expect((await readFile(summary, "utf8")).trim().split(/\r?\n/u)).toEqual(expected,@@ -161,7 +111,7 @@ describe("JUnit inventory comparison", () => {await fixture.report("shard-2.xml", [passed]);const result = await compare(baseline, join(fixture.root, "shard-*.xml"));expect(result.status, result.stderr).toBe(0);- expect(result.stderr).toBe("");+ expect(result.stderr).toMatch(typeStrippingNotice);expect(result.stdout).toBe("| Report | Cases | Skipped | Seconds |\n" +"| --- | ---: | ---: | ---: |\n" +@@ -189,7 +139,7 @@ ${testcase("a & " ' < >")});const result = await compare(baseline, candidate);expect(result.status, result.stderr).toBe(0);- expect(result.stderr).toBe("");+ expect(result.stderr).toMatch(typeStrippingNotice);});test("normalizes report paths and rejects duplicates across candidate files", async () => {diff --git a/sdk/typescript/tests-ts/test-shards.test.ts b/sdk/typescript/tests-ts/test-shards.test.tsindex 6dcf02d42..346d3fba2 100644--- a/sdk/typescript/tests-ts/test-shards.test.ts+++ b/sdk/typescript/tests-ts/test-shards.test.ts@@ -4,14 +4,15 @@ import {mkdtemp,readdir,readFile,- realpath,rm,writeFile,} from "node:fs/promises";import { tmpdir } from "node:os";import { delimiter, dirname, join } from "node:path";import { expect, test } from "bun:test";-import { shardTestFiles } from "../scripts/test-shards.mjs";+import { shardTestFiles } from "../scripts/test-shards.mts";+import { readSubprocess } from "./support/shell.js";+import { temporaryDirectory } from "./support/temporary-directories.js";test("balances measured work and includes new files without mutating the inventory", () => {const files = [@@ -105,8 +106,8 @@ test.each([await mkdir(join(root, "scripts"));await mkdir(join(root, "tests-ts"));for (const file of [- "run-ci-tests.mjs",- "test-shards.mjs",+ "run-ci-tests.mts",+ "test-shards.mts","ci-test-durations.json",]) {await copyFile(@@ -128,7 +129,8 @@ test("synthetic report probe --timeout=5000", () => {const child = Bun.spawn({cmd: [node!,- join(root, "scripts", "run-ci-tests.mjs"),+ "--experimental-strip-types",+ join(root, "scripts", "run-ci-tests.mts"),"1/1",...options,],@@ -142,11 +144,7 @@ test("synthetic report probe --timeout=5000", () => {stderr: "pipe",timeout: 30_000,});- const [status, stdout, stderr] = await Promise.all([- child.exited,- new Response(child.stdout).text(),- new Response(child.stderr).text(),- ]);+ const { status, stdout, stderr } = await readSubprocess(child);expect(status, stderr).toBe(fail ? 1 : 0);expect(stdout).toContain("probe.test.ts");expect(stderr).toContain("synthetic report probe");@@ -170,9 +168,7 @@ test("synthetic report probe --timeout=5000", () => {);test("preserves the configured timeout in isolated test subprocesses", async () => {- const directory = await realpath(- await mkdtemp(join(tmpdir(), "codex-security-test-timeout-")),- );+ const directory = await temporaryDirectory("codex-security-test-timeout-");const fixture = join(directory, "isolated.test.ts");const helper = new URL("./support/test-subprocess.ts", import.meta.url).href;await writeFile(@@ -196,10 +192,7 @@ test("isolated timeout", async () => {timeout: 30_000,windowsHide: true,});- const [status, stderr] = await Promise.all([- child.exited,- new Response(child.stderr).text(),- ]);+ const { status, stderr } = await readSubprocess(child);expect(status, stderr).toBe(1);expect(stderr).toContain("this test timed out after 100ms");} finally {diff --git a/sdk/typescript/tests-ts/workbench-canonical-paths.test.ts b/sdk/typescript/tests-ts/workbench-canonical-paths.test.tsindex bfd9ab755..9ef602522 100644--- a/sdk/typescript/tests-ts/workbench-canonical-paths.test.ts+++ b/sdk/typescript/tests-ts/workbench-canonical-paths.test.ts@@ -3,9 +3,11 @@ import { basename, dirname, join } from "node:path";import { afterEach, describe, expect, test } from "bun:test";import { PLUGIN_ROOT } from "./plugin-root.js";import { runPython } from "./support/python-probe.js";-import { createTemporaryDirectories } from "./support/temporary-directories.js";+import { createApiTestFixtures } from "./support/temporary-directories.js";-const temporaryDirectories = createTemporaryDirectories();+const { temporaryDirectory, cleanup } = createApiTestFixtures(+ "codex-security-canonical-paths-",+);const testCaseSensitive = process.platform === "linux" ? test : test.skip;const testPosix = process.platform === "win32" ? test.skip : test;const testWindows = process.platform === "win32" ? test : test.skip;@@ -83,14 +85,7 @@ const realFilesystemProbe = ["print(json.dumps(checks))",].join("\n");-afterEach(temporaryDirectories.cleanup);--async function temporaryDirectory(): Promise<string> {- const directory = await temporaryDirectories.create(- "codex-security-canonical-paths-",- );- return directory;-}+afterEach(cleanup);function runPythonProbe(program: string,diff --git a/sdk/typescript/tests-ts/workbench-patch.test.ts b/sdk/typescript/tests-ts/workbench-patch.test.tsindex 1efd0a36f..2af089950 100644--- a/sdk/typescript/tests-ts/workbench-patch.test.ts+++ b/sdk/typescript/tests-ts/workbench-patch.test.ts@@ -6,7 +6,7 @@ import { PLUGIN_ROOT } from "./plugin-root.js";import { runPython } from "./support/python-probe.js";import { createTemporaryDirectories } from "./support/temporary-directories.js";-const temporaryDirectories = createTemporaryDirectories();+const temporaryDirectories = createTemporaryDirectories(true);afterEach(temporaryDirectories.cleanup);diff --git a/sdk/typescript/tests-ts/workbench-windows-compatibility.test.ts b/sdk/typescript/tests-ts/workbench-windows-compatibility.test.tsindex e3542133a..82d7e9c95 100644--- a/sdk/typescript/tests-ts/workbench-windows-compatibility.test.ts+++ b/sdk/typescript/tests-ts/workbench-windows-compatibility.test.ts@@ -4,7 +4,7 @@ import { PLUGIN_ROOT } from "./plugin-root.js";import { runPython } from "./support/python-probe.js";import { createTemporaryDirectories } from "./support/temporary-directories.js";-const directories = createTemporaryDirectories();+const directories = createTemporaryDirectories(true);afterEach(directories.cleanup);diff --git a/sdk/typescript/tsconfig.ci.json b/sdk/typescript/tsconfig.ci.jsonindex 3bd8a2c5c..79e3b9816 100644--- a/sdk/typescript/tsconfig.ci.json+++ b/sdk/typescript/tsconfig.ci.json@@ -2,8 +2,7 @@"extends": "./tsconfig.json","include": ["../../.github/scripts/*.mts",- "../../plugins/codex-security/native/*.mts",- "scripts/compare-test-reports.mts"+ "../../plugins/codex-security/native/*.mts"],"compilerOptions": {"rootDir": "../..",diff --git a/sdk/typescript/tsconfig.json b/sdk/typescript/tsconfig.jsonindex b30beb90c..91254837d 100644--- a/sdk/typescript/tsconfig.json+++ b/sdk/typescript/tsconfig.json@@ -11,7 +11,8 @@"scripts/compare-test-reports.mts","scripts/smoke-findings-service.ts","scripts/fixtures/findings-service-sqlite.ts",- "scripts/fixtures/prepare-runner-scan.ts"+ "scripts/fixtures/prepare-runner-scan.ts",+ "scripts/run-ci-tests.mts"],"compilerOptions": {"allowJs": false,@@ -33,6 +34,7 @@"strict": true,"strictNullChecks": true,"target": "ESNext",- "types": ["node", "bun"]+ "types": ["node", "bun"],+ "rewriteRelativeImportExtensions": true}}