Skip to content

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

refactor: share workbench arguments and simplify SDK and eval workflows (#2325)

Michael D'Angelo4d83ce919533
diff --git a/.github/workflows/container-validate.yml b/.github/workflows/container-validate.yml
index 4a65a3aab..0f69dbb96 100644
--- a/.github/workflows/container-validate.yml
+++ b/.github/workflows/container-validate.yml
@@ -183,23 +183,7 @@ jobs:
[[ "$(docker run --rm --entrypoint id codex-security:release-candidate -u)" == 10001 ]]
- name: Verify entrypoint preserves Codex sandbox settings
- shell: bash
- run: |
- set -euo pipefail
- docker run --rm --entrypoint /bin/sh codex-security:release-candidate -ec '
- command_directory="$(mktemp -d)"
- trap '\''rm -rf "$command_directory"'\'' EXIT
- printf "%s\\n" "#!/bin/sh" '\''printf "%s\\n" "$@"'\'' > "$command_directory/codex-security"
- chmod 755 "$command_directory/codex-security"
-
- actual="$(
- PATH="$command_directory:$PATH" \
- /usr/local/bin/codex-security-entrypoint \
- bulk-scan /input/repositories.csv --output-dir /output
- )"
- expected="$(printf "%s\\n" bulk-scan /input/repositories.csv --output-dir /output)"
- test "$actual" = "$expected"
- '
+ run: docker run --rm -i --entrypoint /bin/sh codex-security:release-candidate -s < docker/verify-container-entrypoint.sh
- name: Verify hardened Codex command sandbox
run: bash docker/verify-container-sandbox.sh codex-security:release-candidate
@@ -269,24 +253,10 @@ jobs:
# The single-quoted program is evaluated inside the customer container.
# shellcheck disable=SC2016
- "${compose[@]}" run --rm --entrypoint /bin/sh codex-security -ec '
+ "${compose[@]}" run --rm -T --entrypoint /bin/sh codex-security -ec '
test "$(cat /proc/self/attr/current)" = "codex-security-container (enforce)"
- command_directory="$(mktemp -d)"
- trap '\''rm -rf "$command_directory"'\'' EXIT
- printf "%s\\n" "#!/bin/sh" '\''printf "%s\\n" "$@"'\'' \
- > "$command_directory/codex-security"
- chmod 755 "$command_directory/codex-security"
- actual="$(
- PATH="$command_directory:$PATH" \
- /usr/local/bin/codex-security-entrypoint \
- bulk-scan /input/repositories.csv --output-dir /output
- )"
- if printf "%s\\n" "$actual" |
- grep -Fxq features.use_legacy_landlock=true; then
- printf "%s\\n" "The AppArmor profile must retain the preferred Codex sandbox." >&2
- exit 1
- fi
- '
+ exec /bin/sh
+ ' < docker/verify-container-entrypoint.sh
"${compose[@]}" run --rm --entrypoint /bin/sh codex-security -ec '
cd "$(dirname "$(readlink -f /usr/local/bin/codex-security)")"
diff --git a/.github/workflows/workflow-quality.yml b/.github/workflows/workflow-quality.yml
index 36f2cb9bb..e312ef68a 100644
--- a/.github/workflows/workflow-quality.yml
+++ b/.github/workflows/workflow-quality.yml
@@ -39,7 +39,7 @@ jobs:
run: |
actionlint -color
actionlint -color github-action/examples/*.yml
- shellcheck .github/scripts/*.sh docker/verify-container-compose.sh github-action/scripts/setup-ubuntu-sandbox.sh
+ shellcheck .github/scripts/*.sh docker/verify-container-{compose,entrypoint}.sh github-action/scripts/setup-ubuntu-sandbox.sh
- name: Test release progress reporting
run: node --test .github/scripts/test_release_progress.mjs
- name: Check workflow security
diff --git a/docker/verify-container-entrypoint.sh b/docker/verify-container-entrypoint.sh
new file mode 100644
index 000000000..ade993f8e
--- /dev/null
+++ b/docker/verify-container-entrypoint.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+set -eu
+
+command_directory=$(mktemp -d)
+trap 'rm -rf "$command_directory"' EXIT
+printf '%s\n' '#!/bin/sh' 'printf "%s\0" "$@"' > "$command_directory/codex-security"
+chmod 755 "$command_directory/codex-security"
+
+set -- bulk-scan /input/repositories.csv --output-dir /output
+PATH="$command_directory:$PATH" /usr/local/bin/codex-security-entrypoint "$@" > "$command_directory/actual"
+printf '%s\0' "$@" | cmp - "$command_directory/actual"
diff --git a/evals/standard-time-budget/production.mts b/evals/standard-time-budget/production.mts
index 705933eff..99845e81c 100644
--- a/evals/standard-time-budget/production.mts
+++ b/evals/standard-time-budget/production.mts
@@ -1,10 +1,8 @@
import { randomUUID } from "node:crypto";
import { cp, mkdir, readFile, rm } from "node:fs/promises";
import { join, relative } from "node:path";
-import { Codex } from "../../sdk/typescript/node_modules/@openai/codex-sdk/dist/index.js";
import * as sdk from "../../sdk/typescript/dist/api.js";
import { deepScanOptions } from "../../sdk/typescript/dist/deep-config.js";
-import { createProfileCodex } from "../../sdk/typescript/dist/provider-profile.js";
import {
codexExecutableReadPaths,
resolveCodexCommand,
@@ -19,7 +17,7 @@ import {
type Variant,
} from "./experiment.mts";
-const { CodexSecurity, scanAuthentication } =
+const { CodexSecurity, DEFAULT_DEPENDENCIES, scanAuthentication } =
sdk as unknown as typeof import("../../sdk/typescript/src/api.js");
type Dependencies = NonNullable<ConstructorParameters<typeof CodexSecurity>[1]>;
@@ -69,10 +67,7 @@ export function createStandardRunner({
environment = process.env,
createClient = (config, dependencies) =>
new CodexSecurity(config, dependencies, { surface: "sdk" }),
- createCodex = ({ nativeProfile, ...settings }) =>
- nativeProfile === undefined
- ? new Codex(settings)
- : createProfileCodex(settings, nativeProfile),
+ createCodex = DEFAULT_DEPENDENCIES.createCodex,
}: {
stateRoot: string;
outputRoot: string;
diff --git a/evals/triage-finding/sastbench/scripts/hydrate-sastbench-repos.mts b/evals/triage-finding/sastbench/scripts/hydrate-sastbench-repos.mts
index 6b95450ca..f79b99b4a 100644
--- a/evals/triage-finding/sastbench/scripts/hydrate-sastbench-repos.mts
+++ b/evals/triage-finding/sastbench/scripts/hydrate-sastbench-repos.mts
@@ -1,4 +1,5 @@
#!/usr/bin/env node
+import { isMain } from "../../../../sdk/typescript/scripts/is-main.mjs";
import { hash } from "node:crypto";
import type { SastBenchRecord } from "../../types.ts";
@@ -164,7 +165,7 @@ function ensureTarget(job: ReturnType<typeof buildHydrationPlan>[number]) {
return "hydrated";
}
-if (import.meta.filename === fs.realpathSync(process.argv[1])) {
+if (isMain(import.meta.url)) {
const args = parseArgs(process.argv.slice(2));
const records = loadDataset(args.dataset);
const jobs = buildHydrationPlan(records, args);
diff --git a/evals/triage-finding/sastbench/scripts/install-sastbench.mts b/evals/triage-finding/sastbench/scripts/install-sastbench.mts
index 79a36fe5d..aa30bbf54 100644
--- a/evals/triage-finding/sastbench/scripts/install-sastbench.mts
+++ b/evals/triage-finding/sastbench/scripts/install-sastbench.mts
@@ -1,4 +1,5 @@
#!/usr/bin/env node
+import { isMain } from "../../../../sdk/typescript/scripts/is-main.mjs";
import { hash } from "node:crypto";
import { runGit } from "./hydrate-sastbench-repos.mts";
import fs from "node:fs";
@@ -111,7 +112,7 @@ export function installSastBench(target: string = DEFAULT_INSTALL_ROOT) {
return verifyInstallation(inspectInstallation(resolvedTarget));
}
-if (import.meta.filename === fs.realpathSync(process.argv[1])) {
+if (isMain(import.meta.url)) {
const args = parseArgs(process.argv.slice(2));
if (args.dryRun) {
console.log(
diff --git a/evals/triage-finding/sastbench/scripts/test-promptfoo-native-harness.mts b/evals/triage-finding/sastbench/scripts/test-promptfoo-native-harness.mts
index d8fa10a01..3de6cb55c 100644
--- a/evals/triage-finding/sastbench/scripts/test-promptfoo-native-harness.mts
+++ b/evals/triage-finding/sastbench/scripts/test-promptfoo-native-harness.mts
@@ -115,20 +115,31 @@ const dispatchRoot = fs.mkdtempSync(
);
try {
const runner = path.join(evalRoot, "scripts", "run-promptfoo.mts");
- for (const module of [runner]) {
- for (const entry of ["-", path.join(dispatchRoot, "virtual-entry.mts")]) {
- const imported = execFileSync(
- process.execPath,
- [
- "--experimental-strip-types",
- "--input-type=module",
- "--eval",
- `process.argv[1] = ${JSON.stringify(entry)}; await import(${JSON.stringify(pathToFileURL(module).href)}); console.log("imported");`,
- ],
- { encoding: "utf8" },
- );
- assert.equal(imported.trim(), "imported");
- }
+ const modules = [
+ runner,
+ path.join(evalRoot, "scripts", "generate-calibration-tests.mts"),
+ path.join(evalRoot, "scripts", "hydrate-calibration-repos.mts"),
+ path.join(sastbenchRoot, "scripts", "install-sastbench.mts"),
+ path.join(sastbenchRoot, "scripts", "hydrate-sastbench-repos.mts"),
+ ].map((module) => pathToFileURL(module).href);
+ for (const entry of [
+ undefined,
+ "-",
+ path.join(dispatchRoot, "virtual-entry.mts"),
+ ]) {
+ const imported = execFileSync(
+ process.execPath,
+ [
+ "--experimental-strip-types",
+ "--input-type=module",
+ "--eval",
+ `process.argv[1] = ${JSON.stringify(entry)};
+ for (const module of ${JSON.stringify(modules)}) await import(module);
+ console.log("imported");`,
+ ],
+ { encoding: "utf8" },
+ );
+ assert.equal(imported.trim(), "imported");
}
if (process.platform !== "win32") {
const link = path.join(dispatchRoot, "sastbench-runner.mts");
diff --git a/evals/triage-finding/scripts/generate-calibration-tests.mts b/evals/triage-finding/scripts/generate-calibration-tests.mts
index c396d72cf..59a76580c 100644
--- a/evals/triage-finding/scripts/generate-calibration-tests.mts
+++ b/evals/triage-finding/scripts/generate-calibration-tests.mts
@@ -1,4 +1,5 @@
#!/usr/bin/env node
+import { isMain } from "../../../sdk/typescript/scripts/is-main.mjs";
import type { CalibrationCase, CalibrationVariant } from "../types.ts";
import fs from "node:fs";
@@ -184,11 +185,7 @@ export function generateTests(
);
}
-if (
- process.argv[1] &&
- fs.existsSync(process.argv[1]) &&
- import.meta.filename === fs.realpathSync(process.argv[1])
-) {
+if (isMain(import.meta.url)) {
const args = parseArgs(process.argv.slice(2));
const tests = generateTests({ ...args, datasetPath: args.dataset });
fs.mkdirSync(path.dirname(args.output), { recursive: true });
diff --git a/evals/triage-finding/scripts/hydrate-calibration-repos.mts b/evals/triage-finding/scripts/hydrate-calibration-repos.mts
index de8bbf0c7..4f2f0b026 100644
--- a/evals/triage-finding/scripts/hydrate-calibration-repos.mts
+++ b/evals/triage-finding/scripts/hydrate-calibration-repos.mts
@@ -1,4 +1,5 @@
#!/usr/bin/env node
+import { isMain } from "../../../sdk/typescript/scripts/is-main.mjs";
import { runGit } from "../sastbench/scripts/hydrate-sastbench-repos.mts";
import {
@@ -112,11 +113,7 @@ export function plannedJobs(
}));
}
-if (
- process.argv[1] &&
- fs.existsSync(process.argv[1]) &&
- import.meta.filename === fs.realpathSync(process.argv[1])
-) {
+if (isMain(import.meta.url)) {
const args = parseArgs(process.argv.slice(2));
const dataset = readCalibrationDataset(args.dataset);
const jobs = plannedJobs(dataset, args);
diff --git a/evals/triage-finding/scripts/run-promptfoo.mts b/evals/triage-finding/scripts/run-promptfoo.mts
index e0f7acc1d..6e72ecf4b 100644
--- a/evals/triage-finding/scripts/run-promptfoo.mts
+++ b/evals/triage-finding/scripts/run-promptfoo.mts
@@ -1,4 +1,5 @@
#!/usr/bin/env node
+import { isMain } from "../../../sdk/typescript/scripts/is-main.mjs";
import childProcess, { type ChildProcess } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
@@ -163,24 +164,13 @@ async function runPromptfoo(
}
}
-function runMain(filename: string, environment: NodeJS.ProcessEnv = {}) {
+if (isMain(import.meta.url)) {
try {
- if (!process.argv[1] || fs.realpathSync(process.argv[1]) !== filename)
- return;
- } catch {
- // A virtual entry point imports this module without invoking the runner.
- return;
+ process.exitCode = await runPromptfoo(process.argv.slice(2));
+ } catch (error) {
+ console.error(error);
+ process.exitCode = 1;
}
- runPromptfoo(process.argv.slice(2), environment)
- .then((code) => {
- process.exitCode = code;
- })
- .catch((error) => {
- console.error(error);
- process.exitCode = 1;
- });
}
-runMain(import.meta.filename);
-
-export { runMain, runPromptfoo, stageSkillRuntime };
+export { runPromptfoo, stageSkillRuntime };
diff --git a/plugins/codex-security/mcp-app/server.ts b/plugins/codex-security/mcp-app/server.ts
index 81f03b1bb..504042698 100644
--- a/plugins/codex-security/mcp-app/server.ts
+++ b/plugins/codex-security/mcp-app/server.ts
@@ -1,4 +1,7 @@
-import { workbenchArgs } from "./src/workbench-arguments.js";
+import {
+ workbenchArgs,
+ type WorkbenchFields,
+} from "./src/workbench-arguments.js";
import { CodexSecurityStdioTransport } from "./src/stdio-transport.js";
import {
readingAnnotations,
@@ -825,9 +828,7 @@ export function createCodexSecurityServer(): McpServer {
_meta: appMeta,
},
async ({ targetPath }) => {
- const target = await runWorkbench(
- workbenchArgs("inspect-target", { targetPath }),
- );
+ const target = await workbench("inspect-target", { targetPath });
return scanActionResult(
{ target },
"Validated the local Codex Security target.",
@@ -851,15 +852,13 @@ export function createCodexSecurityServer(): McpServer {
projectConfigPath,
"preview",
);
- const setup = await runWorkbench([
- "inspect-setup",
- `--target-path=${selected.targetPath}`,
- `--scope=${scope}`,
- "--mode",
+ const setup = await workbench("inspect-setup", {
+ targetPath: selected.targetPath,
+ scope,
mode,
- ...diffTargetArgs(diffTarget),
- ...optionalArg("--deep-scan-project-json", selected.projectConfigJson),
- ]);
+ ...diffTargetFields(diffTarget),
+ deepScanProjectJson: selected.projectConfigJson,
+ });
return scanActionResult(
{ setup },
"Validated the local Codex Security setup.",
@@ -893,23 +892,18 @@ export function createCodexSecurityServer(): McpServer {
"save",
);
return workspaceResult(
- (await runWorkbench(
- [
- "save-workspace",
- "--workspace-id",
- sessionId,
- `--target-path=${selected.targetPath}`,
- `--scope=${scope}`,
- "--mode",
+ (await workbench(
+ "save-workspace",
+ {
+ workspaceId: sessionId,
+ targetPath: selected.targetPath,
+ scope,
mode,
- ...definedArg("--target-summary", targetSummary),
- ...(userContext ? ["--user-context-stdin"] : []),
- ...diffTargetArgs(diffTarget),
- ...optionalArg(
- "--deep-scan-project-json",
- selected.projectConfigJson,
- ),
- ],
+ targetSummary,
+ userContextStdin: Boolean(userContext),
+ ...diffTargetFields(diffTarget),
+ deepScanProjectJson: selected.projectConfigJson,
+ },
userContext,
)) as WorkspaceState,
);
@@ -927,15 +921,12 @@ export function createCodexSecurityServer(): McpServer {
_meta: appMeta,
},
async ({ sessionId, model, reasoningEffort }) => {
- const workspace = (await runWorkbench([
- "start-scan",
- "--workspace-id",
- sessionId,
- ...optionalArg("--model", model),
- ...optionalArg("--reasoning-effort", reasoningEffort),
- "--scan-root",
- await scanRoot(),
- ])) as WorkspaceState;
+ const workspace = (await workbench("start-scan", {
+ workspaceId: sessionId,
+ model,
+ reasoningEffort,
+ scanRoot: await scanRoot(),
+ })) as WorkspaceState;
return workspaceResult(workspace, workspace.results?.scanId);
},
);
@@ -1008,22 +999,12 @@ export function createCodexSecurityServer(): McpServer {
if (run.status !== "succeeded" || run.sdkOwnsCompletion)
return deepScanTerminalResult(run);
try {
- const turnMetadata = codexTurnMetadataFromExtra(extra);
- const turnId =
- turnMetadata?.thread_source === "security_scan"
- ? turnMetadata.turn_id
- : undefined;
- const completion = await runWorkbench([
- "complete-scan",
- "--scan-id",
- run.scanId,
- ...optionalArg("--claim-token", handoffClaimToken),
- ...optionalArg("--thread-id", threadId),
- ...optionalArg(
- "--turn-id",
- typeof turnId === "string" ? turnId : undefined,
- ),
- ]);
+ const completion = await workbench("complete-scan", {
+ scanId: run.scanId,
+ claimToken: handoffClaimToken,
+ threadId,
+ turnId: securityScanTurnId(extra),
+ });
// Full findings and workspace history can exceed the host's tool-result limit.
const { progress, reportAvailable, usage, cost } =
completion.scan as {
@@ -1173,11 +1154,9 @@ export function createCodexSecurityServer(): McpServer {
transport.afterResultSent(extra.requestId, extra.signal, () =>
deepScanStartLock.run(async () => {
try {
- await runWorkbench(
- workbenchArgs("acknowledge-deep-scan-result", {
- scanId: begun.scanId,
- }),
- );
+ await workbench("acknowledge-deep-scan-result", {
+ scanId: begun.scanId,
+ });
} catch (error) {
logDeepScanEvent({
event: "target_result_acknowledgment_failed",
@@ -1206,12 +1185,7 @@ export function createCodexSecurityServer(): McpServer {
threadId?: string,
) => {
const scanId = requestedScanId.toLowerCase();
- const args = [
- "cancel-scan",
- "--scan-id",
- scanId,
- ...optionalArg("--thread-id", threadId),
- ];
+ const cancelParent = () => workbench("cancel-scan", { scanId, threadId });
let workspace: Awaited<ReturnType<typeof runWorkbench>> | undefined;
if (threadId && deepScanCoordinators.get(scanId)) {
await deepScanStore.get(scanId, threadId);
@@ -1220,10 +1194,10 @@ export function createCodexSecurityServer(): McpServer {
scanId,
"user_canceled_scan",
async () => {
- workspace = await runWorkbench(args);
+ workspace = await cancelParent();
},
async () => {
- const current = (await runWorkbench(["get-scan", "--scan-id", scanId]))[
+ const current = (await workbench("get-scan", { scanId }))[
"workspace"
] as JsonObject;
const scan = current["results"];
@@ -1242,7 +1216,7 @@ export function createCodexSecurityServer(): McpServer {
);
if (workspace === undefined && canceledLocally) {
// A joined caller did not perform the write. Read the requested parent.
- const saved = (await runWorkbench(workbenchArgs("get-scan", { scanId })))
+ const saved = (await workbench("get-scan", { scanId }))
.workspace as WorkspaceState;
const scan = saved.results;
const progress = isJsonObject(scan?.progress) ? scan.progress : undefined;
@@ -1257,7 +1231,7 @@ export function createCodexSecurityServer(): McpServer {
}
// The displayed workspace may already have a replacement scan.
const { canceledScanId, ...displayedWorkspace } =
- workspace ?? (await runWorkbench(args));
+ workspace ?? (await cancelParent());
return workspaceResult(
displayedWorkspace as unknown as WorkspaceState,
canceledScanId === scanId ? canceledScanId : undefined,
@@ -1310,7 +1284,7 @@ export function createCodexSecurityServer(): McpServer {
},
async ({ scanId }) =>
scanActionResult(
- await runWorkbench(workbenchArgs("recover-scan-results", { scanId })),
+ await workbench("recover-scan-results", { scanId }),
"Recovered retained Codex Security scan results.",
),
);
@@ -1328,12 +1302,7 @@ export function createCodexSecurityServer(): McpServer {
_meta: appMeta,
},
async ({ scanId, occurrenceId }) => {
- const context = await runWorkbench([
- "get-scan",
- "--scan-id",
- scanId,
- ...optionalArg("--occurrence-id", occurrenceId),
- ]);
+ const context = await workbench("get-scan", { scanId, occurrenceId });
const loadedId = isJsonObject(context.scan)
? context.scan.scanId
: undefined;
@@ -1357,15 +1326,14 @@ export function createCodexSecurityServer(): McpServer {
},
async ({ limit, mode, offset, query, status, targetId }) =>
scanActionResult(
- await runWorkbench([
- "list-scans",
- ...optionalArg("--query", query),
- ...optionalArg("--target-id", targetId),
- ...optionalArg("--status", status),
- ...optionalArg("--mode", mode),
- ...optionalNumberArg("--offset", offset),
- ...optionalNumberArg("--limit", limit),
- ]),
+ await workbench("list-scans", {
+ query,
+ targetId,
+ status,
+ mode,
+ offset,
+ limit,
+ }),
"Loaded Codex Security scan summaries.",
),
);
@@ -1382,15 +1350,14 @@ export function createCodexSecurityServer(): McpServer {
},
async ({ limit, offset, query, severity, status, targetId }) =>
scanActionResult(
- await runWorkbench([
- "list-global-findings",
- ...optionalArg("--query", query),
- ...optionalArg("--severity", severity),
- ...optionalArg("--status", status),
- ...optionalArg("--target-id", targetId),
- ...optionalNumberArg("--offset", offset),
- ...optionalNumberArg("--limit", limit),
- ]),
+ await workbench("list-global-findings", {
+ query,
+ severity,
+ status,
+ targetId,
+ offset,
+ limit,
+ }),
"Loaded Codex Security global findings.",
),
);
@@ -1407,14 +1374,13 @@ export function createCodexSecurityServer(): McpServer {
},
async ({ limit, offset, query, status, targetId }) =>
scanActionResult(
- await runWorkbench([
- "list-repositories",
- ...optionalArg("--query", query),
- ...optionalArg("--target-id", targetId),
- ...optionalArg("--status", status),
- ...optionalNumberArg("--offset", offset),
- ...optionalNumberArg("--limit", limit),
- ]),
+ await workbench("list-repositories", {
+ query,
+ targetId,
+ status,
+ offset,
+ limit,
+ }),
"Loaded Codex Security repositories.",
),
);
@@ -1432,21 +1398,13 @@ export function createCodexSecurityServer(): McpServer {
async ({ scanId, occurrenceId, handoffClaimToken }, extra) => {
const threadId = threadIdFromExtra(extra);
if (handoffClaimToken) {
- await runWorkbench([
- "mark-handoff-delivered",
- "--scan-id",
+ await workbench("mark-handoff-delivered", {
scanId,
- "--claim-token",
- handoffClaimToken,
- ...optionalArg("--thread-id", threadId),
- ]);
+ claimToken: handoffClaimToken,
+ threadId,
+ });
}
- const scanContext = await runWorkbench([
- "get-scan",
- "--scan-id",
- scanId,
- ...optionalArg("--occurrence-id", occurrenceId),
- ]);
+ const scanContext = await workbench("get-scan", { scanId, occurrenceId });
const scan = isJsonObject(scanContext.scan)
? scanContext.scan
: undefined;
@@ -1490,12 +1448,7 @@ export function createCodexSecurityServer(): McpServer {
},
async ({ scanId, name }) =>
scanActionResult(
- await runWorkbench([
- "rename-scan",
- "--scan-id",
- scanId,
- `--name=${name}`,
- ]),
+ await workbench("rename-scan", { scanId, name }),
"Renamed Codex Security scan.",
),
);
@@ -1507,16 +1460,15 @@ export function createCodexSecurityServer(): McpServer {
userContext: string;
workspaceId?: string;
}) => {
- const updated = await runWorkbench(
- [
- "update-scan-context",
- "--scan-id",
- input.scanId,
- "--user-context-stdin",
- ...definedArg("--workspace-id", input.workspaceId),
- ...definedArg("--thread-id", input.threadId),
- ...optionalArg("--claim-token", input.claimToken),
- ],
+ const updated = await workbench(
+ "update-scan-context",
+ {
+ scanId: input.scanId,
+ userContextStdin: true,
+ workspaceId: input.workspaceId,
+ threadId: input.threadId,
+ claimToken: input.claimToken,
+ },
input.userContext,
);
return scanActionResult(updated, "Updated Codex Security scan context.");
@@ -1559,7 +1511,7 @@ export function createCodexSecurityServer(): McpServer {
_meta: appMeta,
},
async ({ scanId, userContext }) => {
- const current = await runWorkbench(workbenchArgs("get-scan", { scanId }));
+ const current = await workbench("get-scan", { scanId });
const workspace = isJsonObject(current.workspace)
? current.workspace
: undefined;
@@ -1602,7 +1554,7 @@ export function createCodexSecurityServer(): McpServer {
extra,
) => {
const modelSettings = codexModelSettingsFromExtra(extra);
- const current = await runWorkbench(workbenchArgs("get-scan", { scanId }));
+ const current = await workbench("get-scan", { scanId });
const scan = isJsonObject(current.scan) ? current.scan : undefined;
const progress =
scan && isJsonObject(scan.progress) ? scan.progress : undefined;
@@ -1626,31 +1578,23 @@ export function createCodexSecurityServer(): McpServer {
);
}
return scanActionResult(
- await runWorkbench([
- "update-progress",
- "--scan-id",
+ await workbench("update-progress", {
scanId,
- ...(scan?.mode === "deep"
- ? deepScanStore.coordinatorLeaseArgs(scanId)
- : []),
- ...optionalArg("--model", modelSettings.model),
- ...optionalArg("--reasoning-effort", modelSettings.reasoningEffort),
- ...optionalArg("--claim-token", handoffClaimToken),
- ...optionalNumberArg("--deep-review-pass", deepReviewPass),
- ...optionalArg("--phase", phase),
- ...optionalNumberArg("--phase-items-total", phaseItemsTotal),
- ...optionalNumberArg("--phase-items-completed", phaseItemsCompleted),
- ...optionalArg("--phase-progress-unit", phaseProgressUnit),
- ...optionalNumberArg("--review-items-total", reviewItemsTotal),
- ...optionalNumberArg(
- "--review-items-completed",
- reviewItemsCompleted,
- ),
- ...optionalNumberArg(
- "--reportable-findings-count",
- reportableFindingsCount,
- ),
- ]),
+ coordinatorGeneration:
+ scan?.mode === "deep"
+ ? deepScanStore.coordinatorGeneration(scanId)
+ : undefined,
+ ...modelSettings,
+ claimToken: handoffClaimToken,
+ deepReviewPass,
+ phase,
+ phaseItemsTotal,
+ phaseItemsCompleted,
+ phaseProgressUnit,
+ reviewItemsTotal,
+ reviewItemsCompleted,
+ reportableFindingsCount,
+ }),
"Updated Codex Security scan progress.",
);
},
@@ -1667,23 +1611,13 @@ export function createCodexSecurityServer(): McpServer {
_meta: modelActionMeta,
},
async ({ scanId, handoffClaimToken }, extra) => {
- const turnMetadata = codexTurnMetadataFromExtra(extra);
- const turnId =
- turnMetadata?.thread_source === "security_scan"
- ? turnMetadata.turn_id
- : undefined;
try {
- const completed = await runWorkbench([
- "complete-scan",
- "--scan-id",
+ const completed = await workbench("complete-scan", {
scanId,
- ...optionalArg("--claim-token", handoffClaimToken),
- ...optionalArg("--thread-id", threadIdFromExtra(extra)),
- ...optionalArg(
- "--turn-id",
- typeof turnId === "string" ? turnId : undefined,
- ),
- ]);
+ claimToken: handoffClaimToken,
+ threadId: threadIdFromExtra(extra),
+ turnId: securityScanTurnId(extra),
+ });
return scanActionResult(
completed,
"Validated and indexed the completed Codex Security scan.",
@@ -1712,14 +1646,12 @@ export function createCodexSecurityServer(): McpServer {
},
async ({ scanId, threadId, turnId, turnStatus }) =>
scanActionResult(
- await runWorkbench(
- workbenchArgs("finalize-scan-usage", {
- scanId,
- threadId,
- turnId,
- turnStatus,
- }),
- ),
+ await workbench("finalize-scan-usage", {
+ scanId,
+ threadId,
+ turnId,
+ turnStatus,
+ }),
"Recorded final Codex Security scan usage.",
),
);
@@ -1735,13 +1667,11 @@ export function createCodexSecurityServer(): McpServer {
_meta: modelActionMeta,
},
async ({ scanId, message, handoffClaimToken }) => {
- const failed = await runWorkbench([
- "fail-scan",
- "--scan-id",
+ const failed = await workbench("fail-scan", {
scanId,
- `--message=${message}`,
- ...optionalArg("--claim-token", handoffClaimToken),
- ]);
+ message,
+ claimToken: handoffClaimToken,
+ });
const scan = failed["scan"];
const progress = isJsonObject(scan) ? scan["progress"] : undefined;
if (isJsonObject(progress) && progress["status"] === "failed")
@@ -1766,15 +1696,12 @@ export function createCodexSecurityServer(): McpServer {
},
async ({ occurrenceId, status, closeReason, note }) =>
scanActionResult(
- await runWorkbench([
- "set-finding-triage",
- "--occurrence-id",
+ await workbench("set-finding-triage", {
occurrenceId,
- "--status",
status,
- ...optionalArg("--close-reason", closeReason),
- ...definedArg("--note", note),
- ]),
+ closeReason,
+ note,
+ }),
"Updated the local Codex Security finding status.",
),
);
@@ -1791,13 +1718,11 @@ export function createCodexSecurityServer(): McpServer {
},
async ({ occurrenceId, requestId, actionToken }) =>
scanActionResult(
- await runWorkbench(
- workbenchArgs("request-finding-remediation", {
- occurrenceId,
- requestId,
- actionToken,
- }),
- ),
+ await workbench("request-finding-remediation", {
+ occurrenceId,
+ requestId,
+ actionToken,
+ }),
"Queued the local Codex Security finding remediation request.",
),
);
@@ -1814,15 +1739,13 @@ export function createCodexSecurityServer(): McpServer {
},
async ({ occurrenceId, requestId, expectedVersion, action, actionToken }) =>
scanActionResult(
- await runWorkbench(
- workbenchArgs("request-finding-remediation-action", {
- occurrenceId,
- requestId,
- expectedVersion: String(expectedVersion),
- action,
- actionToken,
- }),
- ),
+ await workbench("request-finding-remediation-action", {
+ occurrenceId,
+ requestId,
+ expectedVersion: String(expectedVersion),
+ action,
+ actionToken,
+ }),
`Queued the local Codex Security finding remediation ${action} request.`,
),
);
@@ -1877,15 +1800,7 @@ export function createCodexSecurityServer(): McpServer {
},
async ({ occurrenceId, requestId, actionToken }) =>
scanActionResult(
- await runWorkbench([
- command,
- "--occurrence-id",
- occurrenceId,
- "--request-id",
- requestId,
- "--action-token",
- actionToken,
- ]),
+ await workbench(command, { occurrenceId, requestId, actionToken }),
message,
),
);
@@ -1903,11 +1818,12 @@ export function createCodexSecurityServer(): McpServer {
},
async ({ patch, ...fields }) =>
scanActionResult(
- await runWorkbench(
- workbenchArgs("set-finding-remediation", {
+ await workbench(
+ "set-finding-remediation",
+ {
...fields,
patchStdin: patch !== undefined,
- }),
+ },
typeof patch === "object"
? Buffer.from(patch.content, patch.encoding)
: patch,
@@ -1928,14 +1844,7 @@ export function createCodexSecurityServer(): McpServer {
},
async ({ scanId, artifact, format }) =>
scanActionResult(
- await runWorkbench([
- "export-findings",
- "--scan-id",
- scanId,
- "--artifact",
- artifact,
- ...optionalArg("--format", format),
- ]),
+ await workbench("export-findings", { scanId, artifact, format }),
artifact === "threat-model"
? "Exported the saved Codex Security threat model as Markdown."
: `Exported Codex Security findings as ${(format ?? "csv").toUpperCase()}.`,
@@ -1954,16 +1863,14 @@ export function createCodexSecurityServer(): McpServer {
},
async ({ limit, offset, query, scanId, severity, status }) =>
scanActionResult(
- await runWorkbench([
- "list-findings",
- "--scan-id",
+ await workbench("list-findings", {
scanId,
- ...optionalArg("--query", query),
- ...optionalArg("--severity", severity),
- ...optionalArg("--status", status),
- ...optionalNumberArg("--offset", offset),
- ...optionalNumberArg("--limit", limit),
- ]),
+ query,
+ severity,
+ status,
+ offset,
+ limit,
+ }),
"Loaded a local Codex Security findings page.",
),
);
@@ -1996,20 +1903,19 @@ async function createWorkspace(
},
threadId?: string,
): Promise<WorkspaceState> {
- return (await runWorkbench(
- [
- "create-workspace",
- "--workspace-id",
- randomUUID(),
- ...optionalArg("--thread-id", threadId),
- ...optionalArg("--mode", input.mode),
- ...optionalArg("--target-path", input.targetPath),
- ...optionalArg("--target-title", input.targetTitle),
- ...optionalArg("--target-summary", input.targetSummary),
- ...optionalArg("--scope", input.scope),
- ...(input.userContext ? ["--user-context-stdin"] : []),
- ...diffTargetArgs(input.diffTarget),
- ],
+ return (await workbench(
+ "create-workspace",
+ {
+ workspaceId: randomUUID(),
+ threadId,
+ mode: input.mode,
+ targetPath: input.targetPath,
+ targetTitle: input.targetTitle,
+ targetSummary: input.targetSummary,
+ scope: input.scope,
+ userContextStdin: Boolean(input.userContext),
+ ...diffTargetFields(input.diffTarget),
+ },
input.userContext,
)) as WorkspaceState;
}
@@ -2018,12 +1924,10 @@ async function getWorkspace(
workspaceId: string,
threadId?: string,
): Promise<WorkspaceState> {
- return (await runWorkbench([
- "get-workspace",
- "--workspace-id",
+ return (await workbench("get-workspace", {
workspaceId,
- ...optionalArg("--thread-id", threadId),
- ])) as WorkspaceState;
+ threadId,
+ })) as WorkspaceState;
}
async function startPromptOnlyScan(
@@ -2033,23 +1937,19 @@ async function startPromptOnlyScan(
): Promise<JsonObject> {
const { mode, targetPath, scope, targetSummary, userContext, diffTarget } =
input;
- return await runWorkbench(
- [
- "start-prompt-only-scan",
- "--thread-id",
+ return await workbench(
+ "start-prompt-only-scan",
+ {
threadId,
- `--target-path=${targetPath}`,
- `--scope=${scope}`,
- "--mode",
+ targetPath,
+ scope,
mode,
- ...optionalArg("--model", modelSettings.model),
- ...optionalArg("--reasoning-effort", modelSettings.reasoningEffort),
- ...optionalArg("--target-summary", targetSummary),
- ...(userContext ? ["--user-context-stdin"] : []),
- ...diffTargetArgs(diffTarget),
- "--scan-root",
- await scanRoot(),
- ],
+ ...modelSettings,
+ targetSummary,
+ userContextStdin: Boolean(userContext),
+ ...diffTargetFields(diffTarget),
+ scanRoot: await scanRoot(),
+ },
userContext,
);
}
@@ -2064,20 +1964,17 @@ async function startHeadlessStandardScan(
threadId: string,
modelSettings: { model?: string; reasoningEffort?: string } = {},
): Promise<JsonObject> {
- return await runWorkbench(
- [
- "start-headless-standard-scan",
- "--thread-id",
+ return await workbench(
+ "start-headless-standard-scan",
+ {
threadId,
- `--target-path=${input.targetPath}`,
- `--scope=${input.scope ?? "."}`,
- ...optionalArg("--model", modelSettings.model),
- ...optionalArg("--reasoning-effort", modelSettings.reasoningEffort),
- ...optionalArg("--target-summary", input.targetSummary),
- ...(input.userContext ? ["--user-context-stdin"] : []),
- "--scan-root",
- await scanRoot(),
- ],
+ targetPath: input.targetPath,
+ scope: input.scope ?? ".",
+ ...modelSettings,
+ targetSummary: input.targetSummary || undefined,
+ userContextStdin: Boolean(input.userContext),
+ scanRoot: await scanRoot(),
+ },
input.userContext,
);
}
@@ -2123,9 +2020,7 @@ async function isCanonicalDirectory(targetPath: string): Promise<boolean> {
}
async function inspectProjectTarget(targetPath: string): Promise<string> {
- const inspected = await runWorkbench(
- workbenchArgs("inspect-target", { targetPath }),
- );
+ const inspected = await workbench("inspect-target", { targetPath });
if (typeof inspected.targetPath !== "string")
throw new Error("Missing scan target path.");
return inspected.targetPath;
@@ -2387,38 +2282,34 @@ function logDeepScanEvent(event: DeepScanLogEvent): void {
);
}
-function optionalArg(name: string, value: string | undefined): string[] {
- return value ? [`${name}=${value}`] : [];
-}
-
-function definedArg(name: string, value: string | undefined): string[] {
- return value === undefined ? [] : [`${name}=${value}`];
+function workbench(
+ command: string,
+ fields: WorkbenchFields,
+ input?: string | Buffer,
+) {
+ return runWorkbench(workbenchArgs(command, fields), input);
}
-function optionalNumberArg(name: string, value: number | undefined): string[] {
- return value === undefined ? [] : [name, String(value)];
+function diffTargetFields(
+ target: z.infer<typeof diffTargetSchema> | undefined,
+) {
+ if (!target) return {};
+ return {
+ diffTargetKind: target.kind,
+ diffBaseRevision: target.baseRevision,
+ diffHeadRevision: target.headRevision,
+ diffContentDigest:
+ target.kind === "working_tree" ? target.contentDigest : undefined,
+ };
}
-function diffTargetArgs(
- target: z.infer<typeof diffTargetSchema> | undefined,
-): string[] {
- if (!target) return [];
- return [
- "--diff-target-kind",
- target.kind,
- ...optionalArg(
- "--diff-base-revision",
- "baseRevision" in target ? target.baseRevision : undefined,
- ),
- ...optionalArg(
- "--diff-head-revision",
- "headRevision" in target ? target.headRevision : undefined,
- ),
- ...optionalArg(
- "--diff-content-digest",
- "contentDigest" in target ? target.contentDigest : undefined,
- ),
- ];
+function securityScanTurnId(extra: unknown): string | undefined {
+ const metadata = codexTurnMetadataFromExtra(extra);
+ return metadata?.thread_source === "security_scan" &&
+ typeof metadata.turn_id === "string" &&
+ metadata.turn_id
+ ? metadata.turn_id
+ : undefined;
}
function requestMetadataFromExtra(extra: unknown): JsonObject | undefined {
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/store.ts b/plugins/codex-security/mcp-app/src/deep-scan/store.ts
index 4b0037477..fe7e34ddb 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/store.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/store.ts
@@ -405,7 +405,7 @@ export class WorkbenchDeepScanStore {
);
}
- private coordinatorGeneration(scanId: string): number | undefined {
+ coordinatorGeneration(scanId: string): number | undefined {
return this.coordinatorLeases.get(scanId)?.run.coordinatorGeneration;
}
@@ -418,13 +418,6 @@ export class WorkbenchDeepScanStore {
* other workbench processes. Reads remain concurrent and observe a committed
* state before or after each mutation.
*/
- coordinatorLeaseArgs(scanId: string): string[] {
- const generation = this.coordinatorGeneration(scanId);
- return generation === undefined
- ? []
- : ["--coordinator-generation", String(generation)];
- }
-
private enqueueWrite(args: string[], input?: string): Promise<JsonObject> {
const operation = this.writeTail.then(async () => {
try {
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/types.ts b/plugins/codex-security/mcp-app/src/deep-scan/types.ts
index c0a306051..82e461268 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/types.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/types.ts
@@ -55,7 +55,7 @@ export type DeepScanReplaceableFailureKind =
/** Durable operations implemented by the Python workbench. */
export type DeepScanStore = Omit<
WorkbenchDeepScanStore,
- "begin" | "coordinatorLeaseArgs"
+ "begin" | "coordinatorGeneration"
>;
/** Host-bound worker artifact state; never populate this from model input. */
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
index 3df88cdbd..607ceac8c 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
@@ -1659,7 +1659,7 @@ async function testStoreConfirmedReplacementSurvivesReadFailure() {
);
if (replaced) {
assert.equal(reads, confirmation === "mutation" ? 3 : 4);
- assert.deepEqual(store.coordinatorLeaseArgs(run.scanId), []);
+ assert.equal(store.coordinatorGeneration(run.scanId), undefined);
} else {
assert.equal(
reads,
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts
index 0da10144d..9e764c690 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts
@@ -344,10 +344,7 @@ async function testOwnershipReadClearsStaleLease() {
owned.updateProgress({ scanId, phase: "discovery" }),
(error: unknown) => error === original,
);
- assert.deepEqual(owned.coordinatorLeaseArgs(scanId), [
- "--coordinator-generation",
- "2",
- ]);
+ assert.equal(owned.coordinatorGeneration(scanId), 2);
}
}
diff --git a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts
index c87daa76c..5dcfe4b1d 100644
--- a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts
+++ b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts
@@ -490,7 +490,8 @@ async function assertHeadlessStandardScanWorksWithoutUiCapability() {
CODEX_SECURITY_STATE_DIR: headlessStateDir,
},
});
- const ownerThread = "fixture-headless-standard-thread";
+ // Option-like metadata must reach the workbench as the literal owner ID.
+ const ownerThread = "--help";
const headlessContext =
`Review https://example.test/internal. ${"Assess the HTTP boundary. ".repeat(44_000)}`.trim();
assert.ok(headlessContext.length > 1_000_000);
diff --git a/plugins/codex-security/scripts/deep_scan_workbench.py b/plugins/codex-security/scripts/deep_scan_workbench.py
index 63a70fb0e..a206d6621 100644
--- a/plugins/codex-security/scripts/deep_scan_workbench.py
+++ b/plugins/codex-security/scripts/deep_scan_workbench.py
@@ -1481,14 +1481,12 @@ def finish_deep_scan_locked(
(scan_id,),
)
}
- # Historical succeeded runs retain their exact omitted-worker replay contract.
- omissions_match = omitted_worker_ids == buffered_worker_ids
if run["status"] == "succeeded":
if (
run["terminal_reason"] != args.terminal_reason
or archived_deep_path(Path(scan["scan_dir"]), run["manifest_path"])
not in {None, manifest_path}
- or not omissions_match
+ or omitted_worker_ids != buffered_worker_ids
):
raise SystemExit(
"Deep Scan terminal state is immutable; finish must exactly replay its "
@@ -1564,7 +1562,7 @@ def finish_deep_scan_locked(
args.terminal_reason,
has_omitted_workers=bool(omitted_worker_ids),
)
- if args.terminal_reason == "saturated" and not omissions_match:
+ if args.terminal_reason == "saturated" and omitted_worker_ids != buffered_worker_ids:
raise SystemExit(
f"Deep Scan {args.terminal_reason} completion must exactly identify all buffered discovery "
"workers with --omitted-worker-id."
@@ -1595,7 +1593,6 @@ def finish_deep_scan_locked(
""",
(args.terminal_reason, manifest_path, timestamp, timestamp, scan_id),
)
- cancel_active_workers(connection, scan_id, timestamp)
connection.commit()
except BaseException:
connection.rollback()
diff --git a/plugins/codex-security/tests/test_workbench_deep_scan.py b/plugins/codex-security/tests/test_workbench_deep_scan.py
index 7210c92b7..c0cf7123e 100644
--- a/plugins/codex-security/tests/test_workbench_deep_scan.py
+++ b/plugins/codex-security/tests/test_workbench_deep_scan.py
@@ -294,15 +294,14 @@ class DeepScan:
prompt_path: Path,
artifact_dir: Path,
input_worker_ids: list[str],
- ) -> dict[str, object]:
- return seed_legacy_reducer(
+ ) -> None:
+ seed_legacy_reducer(
self.state_dir,
self.id,
worker_id,
str(prompt_path),
str(artifact_dir),
- *(item for worker in input_worker_ids for item in ("--input-worker-id", worker)),
- environment=deep_environment(self.codex_home),
+ *input_worker_ids,
)
def finish(
@@ -801,7 +800,7 @@ def test_expired_coordinator_recovers_reducer_publication_after_process_death(
deep.id,
reducer_id,
str(result),
- "1",
+ 1,
environment=deep_environment(deep.codex_home),
)
diff --git a/plugins/codex-security/tests/test_workbench_standard_deep_results.py b/plugins/codex-security/tests/test_workbench_standard_deep_results.py
index 132895d1d..de082d40f 100644
--- a/plugins/codex-security/tests/test_workbench_standard_deep_results.py
+++ b/plugins/codex-security/tests/test_workbench_standard_deep_results.py
@@ -1246,19 +1246,14 @@ def running_standard_reducer(
reducer_id = str(uuid.uuid4())
prompt_path, artifact_dir, result_path = worker_paths(scan_dir, name)
environment = {"CODEX_HOME": str(codex_home)}
- input_worker_args = [
- argument
- for worker_id in (discovery_worker_id, *additional_worker_ids)
- for argument in ("--input-worker-id", worker_id)
- ]
seed_legacy_reducer(
state_dir,
scan_id,
reducer_id,
str(prompt_path),
str(artifact_dir),
- *input_worker_args,
- environment=environment,
+ discovery_worker_id,
+ *additional_worker_ids,
)
upsert_deep_worker(
state_dir,
@@ -1296,7 +1291,7 @@ def committed_standard_reducer(
environment = {"CODEX_HOME": str(codex_home)}
result_path.write_text(discovery_result.read_text())
committed = seal_legacy_reducer(
- state_dir, scan_id, reducer_id, str(result_path), "0", environment=environment
+ state_dir, scan_id, reducer_id, str(result_path), 0, environment=environment
)["deepScan"]
return reducer_id, result_path, committed
@@ -1826,9 +1821,7 @@ def test_failed_reducer_preserves_later_successful_worker_findings(tmp_path: Pat
reducer_id,
str(prompt_path),
str(artifact_dir),
- "--input-worker-id",
second_worker_id,
- environment=environment,
)
base_args = (
"upsert-deep-scan-worker",
diff --git a/plugins/codex-security/tests/workbench_test_support.py b/plugins/codex-security/tests/workbench_test_support.py
index 067ddd560..af68453d7 100644
--- a/plugins/codex-security/tests/workbench_test_support.py
+++ b/plugins/codex-security/tests/workbench_test_support.py
@@ -376,11 +376,9 @@ def seed_legacy_reducer(
worker_id: str,
prompt_path: str,
artifact_dir: str,
- *args: str,
- environment: dict[str, str] | None = None,
-) -> dict[str, object]:
+ *input_worker_ids: str,
+) -> None:
"""Recreate a pre-loop database row for tests of archived scan recovery."""
- ids = [args[i + 1] for i, value in enumerate(args) if value == "--input-worker-id"]
with sqlite3.connect(state_dir / "workbench.sqlite3") as db:
db.execute(
"""INSERT INTO deep_scan_workers
@@ -388,7 +386,7 @@ def seed_legacy_reducer(
VALUES (?, ?, 'dedup', 'queued', ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)""",
(worker_id, scan_id, prompt_path, artifact_dir),
)
- for i, source in enumerate(ids):
+ for i, source in enumerate(input_worker_ids):
db.execute(
"INSERT INTO deep_scan_dedup_inputs VALUES (?, ?, ?, ?)",
(scan_id, worker_id, source, i),
@@ -396,7 +394,6 @@ def seed_legacy_reducer(
db.execute(
"UPDATE deep_scan_workers SET merge_state = 'merging' WHERE id = ?", (source,)
)
- return read_fixture_deep_scan(state_dir, scan_id, environment)
def seal_legacy_reducer(
@@ -404,8 +401,8 @@ def seal_legacy_reducer(
scan_id: str,
worker_id: str,
result: str,
- new_count: str,
- *args: str,
+ new_count: int,
+ *,
environment: dict[str, str] | None = None,
) -> dict[str, object]:
with sqlite3.connect(state_dir / "workbench.sqlite3") as db:
@@ -422,7 +419,7 @@ def seal_legacy_reducer(
db.execute(
"""UPDATE deep_scan_runs SET consecutive_no_new =
CASE WHEN ? > 0 THEN 0 ELSE consecutive_no_new + ? END, phase = 'discovery' WHERE scan_id = ?""",
- (int(new_count), inputs, scan_id),
+ (new_count, inputs, scan_id),
)
return read_fixture_deep_scan(state_dir, scan_id, environment)
diff --git a/sdk/typescript/scripts/generate-models.cjs b/sdk/typescript/scripts/generate-models.cjs
index 4daaea5a2..b81c84539 100644
--- a/sdk/typescript/scripts/generate-models.cjs
+++ b/sdk/typescript/scripts/generate-models.cjs
@@ -92,7 +92,6 @@ generate().then((models) => {
"workbench-arguments",
"executable-path",
"contract-values",
- "finding-normalization",
"windows-path",
].map((name) => [
`src/${name}.ts`,
diff --git a/sdk/typescript/scripts/is-main.d.mts b/sdk/typescript/scripts/is-main.d.mts
new file mode 100644
index 000000000..d01ba4ee7
--- /dev/null
+++ b/sdk/typescript/scripts/is-main.d.mts
@@ -0,0 +1 @@
+export function isMain(url: string): boolean;
diff --git a/sdk/typescript/scripts/package-dist-files.mjs b/sdk/typescript/scripts/package-dist-files.mjs
index 96433ffcc..42f739287 100644
--- a/sdk/typescript/scripts/package-dist-files.mjs
+++ b/sdk/typescript/scripts/package-dist-files.mjs
@@ -151,7 +151,6 @@ const distModules = [
"deduplication/retry",
"deduplication/deduplication",
"finding-retrieval",
- "finding-normalization",
"contract-values",
"workbench-arguments",
"executable-path",
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 82226cc56..7e1bf18de 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -343,7 +343,7 @@ import {
type HistoryCommand,
} from "./scan-history-renderer.js";
import { ScanDashboard } from "./scan-dashboard.js";
-import { graphemes } from "./terminal-columns.js";
+import { fitCell, graphemes } from "./terminal-columns.js";
import {
ACCOUNT_LIMITS_UNAVAILABLE,
formatAccountLimits,
@@ -670,27 +670,6 @@ function publicationScanAge(timestamp: string, now: number): string {
return "just now";
}
-function publicationDisplayWidth(value: string): number {
- const segments = graphemes.segment(stripVTControlCharacters(value));
- let width = 0;
-
- for (const { segment } of segments) {
- if (/^[\p{Mark}\p{Cf}]+$/u.test(segment)) continue;
- width +=
- /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Emoji_Presentation}\p{Regional_Indicator}\u3000-\u303F\uFF01-\uFF60\uFFE0-\uFFE6\u20E3\uFE0F]/u.test(
- segment,
- )
- ? 2
- : 1;
- }
-
- return width;
-}
-
-function padPublicationColumn(value: string, width: number): string {
- return `${value}${" ".repeat(width - publicationDisplayWidth(value))}`;
-}
-
function fitProgressLine(line: string, width: number): string {
const plain = stripVTControlCharacters(line);
let ellipsisIndex = 0;
@@ -3607,16 +3586,16 @@ export async function main(
field: "repository" | "findings" | "age",
) =>
Math.max(
- publicationDisplayWidth(heading),
- ...rows.map((row) => publicationDisplayWidth(row[field])),
+ stringWidth(heading),
+ ...rows.map((row) => stringWidth(row[field])),
);
const repositoryWidth = columnWidth("REPOSITORY", "repository");
const findingsWidth = columnWidth("FINDINGS", "findings");
const ageWidth = columnWidth("AGE", "age");
const header = [
- padPublicationColumn("REPOSITORY", repositoryWidth),
- padPublicationColumn("FINDINGS", findingsWidth),
- padPublicationColumn("AGE", ageWidth),
+ fitCell("REPOSITORY", repositoryWidth),
+ fitCell("FINDINGS", findingsWidth),
+ fitCell("AGE", ageWidth),
"SCAN ID",
].join(" ");
const choices = rows.map((row) => {
@@ -3626,9 +3605,9 @@ export async function main(
return {
label: [
- padPublicationColumn(repository, repositoryWidth),
- padPublicationColumn(row.findings, findingsWidth),
- padPublicationColumn(row.age, ageWidth),
+ fitCell(repository, repositoryWidth),
+ fitCell(row.findings, findingsWidth),
+ fitCell(row.age, ageWidth),
row.scanId,
].join(" "),
short: `${repository} · ${row.scanId}`,
diff --git a/sdk/typescript/src/external-findings-local.ts b/sdk/typescript/src/external-findings-local.ts
index 9cfb7d188..feb505c09 100644
--- a/sdk/typescript/src/external-findings-local.ts
+++ b/sdk/typescript/src/external-findings-local.ts
@@ -16,7 +16,10 @@ import {
checkTrivyReportProvider,
matchTrivyCheckout,
} from "./trivy-import.js";
-import { readHostedGitRemotes } from "./hosted-git-remotes.js";
+import {
+ readGitRemoteNames,
+ readHostedGitRemotes,
+} from "./hosted-git-remotes.js";
import { CodexSecurityError } from "./errors.js";
import {
forwardExternalImportSetting,
@@ -520,27 +523,8 @@ export async function configuredFetchRemotes(
),
signal,
);
- let remoteNames: string;
- try {
- remoteNames = await readGit(["remote"]);
- } catch (error) {
- signal?.throwIfAborted();
- // A local source directory need not be a Git checkout.
- if (
- error instanceof Error &&
- "code" in error &&
- error.code === 128 &&
- "stderr" in error &&
- typeof error.stderr === "string" &&
- (error.stderr.trimEnd() ===
- "fatal: not a git repository (or any of the parent directories): .git" ||
- /^fatal: not a git repository \(or any parent up to mount point [^\r\n]+\)\r?\nStopping at filesystem boundary \(GIT_DISCOVERY_ACROSS_FILESYSTEM not set\)\.$/u.test(
- error.stderr.trimEnd(),
- ))
- )
- return [];
- throw error;
- }
+ const remoteNames = await readGitRemoteNames(readGit, signal);
+ if (remoteNames === undefined) return [];
const remotes: ConfiguredFetchRemote[] = [];
const firstUrlOnly = provider === "sonar" || provider === "checkmarx";
for (const name of remoteNames.split(/\r?\n/u).filter(Boolean)) {
diff --git a/sdk/typescript/src/finding-normalization.ts b/sdk/typescript/src/finding-normalization.ts
deleted file mode 100644
index 6d0fe652b..000000000
--- a/sdk/typescript/src/finding-normalization.ts
+++ /dev/null
@@ -1,179 +0,0 @@
-/* Generated from plugin finding-normalization.ts. Run `pnpm generate:models`. */
-import { isRecord as isObject } from "./record.js";
-import { hasRequiredFindingText } from "./contract-values.js";
-
-type JsonObject = Record<string, unknown>;
-
-/** Compatibility for historical report reads; never use this to admit new writes. */
-export function normalizePersistedFindingDetails(
- finding: JsonObject,
- profile: "sdk" | "tracking" = "sdk",
-): void {
- const evidenceIds = new Set<string>();
- if (profile === "tracking" && Array.isArray(finding["codeEvidence"])) {
- for (const evidence of finding["codeEvidence"]) {
- if (isObject(evidence) && isNonEmptyString(evidence["id"]))
- evidenceIds.add(evidence["id"]);
- }
- }
- if (Array.isArray(finding["code_evidence"])) {
- finding["code_evidence"] = finding["code_evidence"].filter((evidence) => {
- if (
- !isObject(evidence) ||
- !isNonEmptyString(evidence["id"]) ||
- !isNonEmptyString(evidence["code"])
- )
- return false;
- if (profile === "sdk") return true;
- if (
- !hasRequiredFindingText(evidence["id"]) ||
- !hasRequiredFindingText(evidence["code"]) ||
- evidenceIds.has(evidence["id"])
- )
- return false;
- evidenceIds.add(evidence["id"]);
- return true;
- });
- } else if (profile === "tracking" || finding["code_evidence"] !== null) {
- delete finding["code_evidence"];
- }
-
- for (const [sectionName, listFields] of [
- ["rootCause", ["evidenceRefs", "evidence_refs"]],
- ["root_cause", ["evidenceRefs", "evidence_refs"]],
- [
- "validation",
- [
- "assertions",
- "counterEvidence",
- "evidence",
- "evidenceRefs",
- "evidence_refs",
- "limitations",
- ],
- ],
- [
- "attackPath",
- [
- "assumptions",
- "blindspots",
- "controls",
- "evidenceRefs",
- "evidence_refs",
- "limitations",
- "preconditions",
- "steps",
- ],
- ],
- ] satisfies Array<[string, string[]]>) {
- const section = finding[sectionName];
- if (!isObject(section)) continue;
- normalizePersistedStringLists(section, listFields);
- }
-
- const legacyRootCause = finding["root_cause"];
- if (isObject(legacyRootCause)) {
- removeUnsupportedPersistedStrings(legacyRootCause, [
- "summary",
- "code",
- "language",
- ]);
- } else if (
- legacyRootCause !== null &&
- (typeof legacyRootCause !== "string" ||
- (profile === "tracking" && legacyRootCause === ""))
- ) {
- delete finding["root_cause"];
- }
-
- const validation = finding["validation"];
- if (isObject(validation)) {
- removeUnsupportedPersistedStrings(
- validation,
- ["method", "status", "summary", "disposition", "result"].filter(
- (field) =>
- profile === "tracking" ||
- !["status", "disposition", "result"].includes(field) ||
- validation[field] !== null,
- ),
- );
- }
-
- const attackPath = finding["attackPath"];
- if (!isObject(attackPath)) return;
- removeUnsupportedPersistedStrings(attackPath, ["summary"]);
- for (const field of ["dataFlow", "data_flow", "dataflow", "reachability"]) {
- const detail = attackPath[field];
- if (!isObject(detail)) {
- if (!isNonEmptyString(detail)) delete attackPath[field];
- continue;
- }
- removeUnsupportedPersistedStrings(detail, [
- "summary",
- "source",
- "sink",
- "outcome",
- ...(field === "reachability" ? ["attacker", "entrypoint"] : []),
- ]);
- normalizePersistedStringLists(detail, [
- "evidenceRefs",
- "evidence_refs",
- "transformations",
- ...(field === "reachability" ? ["preconditions"] : []),
- ]);
- }
- for (const field of ["impact", "likelihood"]) {
- const detail = attackPath[field];
- if (isObject(detail)) {
- removeUnsupportedPersistedStrings(detail, ["level", "rationale", "why"]);
- } else if (
- detail !== undefined &&
- detail !== null &&
- !isNonEmptyString(detail)
- ) {
- delete attackPath[field];
- }
- }
-
- function normalizePersistedStringLists(
- section: JsonObject,
- fields: string[],
- ): void {
- for (const field of fields) {
- const value = section[field];
- const accepted = (value: unknown): value is string =>
- isNonEmptyString(value) &&
- (profile === "sdk" || hasRequiredFindingText(value));
- if (accepted(value)) {
- if (profile === "tracking" || field !== "evidence")
- section[field] = [value];
- } else if (Array.isArray(value)) {
- const items = value.filter(accepted);
- if (profile === "tracking" && items.length === 0) delete section[field];
- else section[field] = items;
- } else delete section[field];
- if (
- profile === "tracking" &&
- (field === "evidenceRefs" || field === "evidence_refs") &&
- Array.isArray(section[field])
- ) {
- section[field] = section[field].filter(
- (ref) => typeof ref === "string" && evidenceIds.has(ref),
- );
- }
- }
- }
-
- function removeUnsupportedPersistedStrings(
- section: JsonObject,
- fields: string[],
- ): void {
- for (const field of fields) {
- if (!isNonEmptyString(section[field])) delete section[field];
- }
- }
-
- function isNonEmptyString(value: unknown): value is string {
- return typeof value === "string" && value.length > 0;
- }
-}
diff --git a/sdk/typescript/src/hosted-git-remotes.ts b/sdk/typescript/src/hosted-git-remotes.ts
index c5a288efd..faefa6828 100644
--- a/sdk/typescript/src/hosted-git-remotes.ts
+++ b/sdk/typescript/src/hosted-git-remotes.ts
@@ -88,6 +88,32 @@ async function emptyLegacyRemotes(
return true;
}
+/** A source directory can be outside Git. Preserve all other command failures. */
+export async function readGitRemoteNames(
+ readGit: (args: readonly string[]) => Promise<string>,
+ signal?: AbortSignal,
+): Promise<string | undefined> {
+ try {
+ return await readGit(["remote"]);
+ } catch (error) {
+ signal?.throwIfAborted();
+ if (
+ error instanceof Error &&
+ "code" in error &&
+ error.code === 128 &&
+ "stderr" in error &&
+ typeof error.stderr === "string" &&
+ (error.stderr.trimEnd() ===
+ "fatal: not a git repository (or any of the parent directories): .git" ||
+ /^fatal: not a git repository \(or any parent up to mount point [^\r\n]+\)\r?\nStopping at filesystem boundary \(GIT_DISCOVERY_ACROSS_FILESYSTEM not set\)\.$/u.test(
+ error.stderr.trimEnd(),
+ ))
+ )
+ return;
+ throw error;
+ }
+}
+
/** Read effective external Git URLs without invoking a transport. An empty result means none could be verified. */
export async function readHostedGitRemotes(
repository: string,
@@ -136,27 +162,8 @@ export async function readHostedGitRemotes(
const bytes = Buffer.from(raw, "latin1");
return isUtf8(bytes) ? bytes.toString("utf8") : undefined;
};
- let remoteNames: string;
- try {
- remoteNames = await readGit(["remote"]);
- } catch (error) {
- signal?.throwIfAborted();
- // A local source directory need not be a Git checkout.
- if (
- error instanceof Error &&
- "code" in error &&
- error.code === 128 &&
- "stderr" in error &&
- typeof error.stderr === "string" &&
- (error.stderr.trimEnd() ===
- "fatal: not a git repository (or any of the parent directories): .git" ||
- /^fatal: not a git repository \(or any parent up to mount point [^\r\n]+\)\r?\nStopping at filesystem boundary \(GIT_DISCOVERY_ACROSS_FILESYSTEM not set\)\.$/u.test(
- error.stderr.trimEnd(),
- ))
- )
- return;
- throw error;
- }
+ const remoteNames = await readGitRemoteNames(readGit, signal);
+ if (remoteNames === undefined) return;
const config = await readGit([
"config",
"--null",
diff --git a/sdk/typescript/src/snyk-rest-findings.ts b/sdk/typescript/src/snyk-rest-findings.ts
index 1073b87f8..4759a6a12 100644
--- a/sdk/typescript/src/snyk-rest-findings.ts
+++ b/sdk/typescript/src/snyk-rest-findings.ts
@@ -1116,16 +1116,6 @@ function bindSnykFinding(
return { ...repository, issueId };
}
-/** Recognize a retained hosted report before verifying its selected issues. */
-export function isSnykRestReport(
- input: ExternalFindings & Partial<SnykIssuesFindings>,
-): boolean {
- return (
- input.snykRestContext !== undefined ||
- input.findings.some(isSnykRestFinding)
- );
-}
-
function readIssues(document: Resource, selected: Scope) {
if (document["complete"] !== true)
throw new Error("Expected a complete Snyk REST Issues export.");
diff --git a/sdk/typescript/tests-ts/cli-publish.test.ts b/sdk/typescript/tests-ts/cli-publish.test.ts
index 4dca890bd..31db8fa01 100644
--- a/sdk/typescript/tests-ts/cli-publish.test.ts
+++ b/sdk/typescript/tests-ts/cli-publish.test.ts
@@ -1828,6 +1828,13 @@ describe("publish scan", () => {
elapsed: 4 * 24 * 60 * 60_000,
age: "4 days ago",
},
+ {
+ repository: "カナ",
+ width: 2,
+ findingCount: 1,
+ elapsed: 0,
+ age: "just now",
+ },
{
repository: "服务",
width: 4,