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
fix(workbench): preserve argument text and typed state failures (#1500)
mldangelo-oaif9ef782fefe0
diff --git a/plugins/codex-security/mcp-app/server.ts b/plugins/codex-security/mcp-app/server.tsindex 367d29602..9762fb389 100644--- a/plugins/codex-security/mcp-app/server.ts+++ b/plugins/codex-security/mcp-app/server.ts@@ -39,6 +39,10 @@ import {} from "./src/deep-scan/parent-sandbox.js";import { WorkbenchDeepScanStore } from "./src/deep-scan/store.js";import type { DeepScanRunState } from "./src/deep-scan/types.js";+import {+ WORKBENCH_PYTHON,+ WORKBENCH_STATE_UNAVAILABLE_EXIT_CODE,+} from "./src/server/workbench-process.js";const execFileAsync = promisify(execFile);const CONFIGURED_SCAN_ROOT = process.env.CODEX_SECURITY_SCAN_ROOT?.trim();@@ -61,6 +65,11 @@ const workbenchStateSelectionLock = new AsyncLock();const userContextSchema = z.string().trim().min(1);const editableUserContextSchema = z.string().trim();+const pathSchema = z+ .string()+ .min(1)+ .max(4096)+ .refine((value) => value.trim().length > 0, "Path must not be blank.");const verifiedAccessGrantSchema = z.strictObject({level: z.enum(["tac1", "tac2", "tac3", "government"]),@@ -160,11 +169,7 @@ const openSchema = {.enum(["diff", "standard", "deep"]).optional().describe("Initial scan mode inferred from the user's request."),- scope: z- .string()- .trim()- .min(1)- .max(4096)+ scope: pathSchema.optional().describe("Optional directory inside targetPath. Use '.' or omit it for the whole target. Target-relative paths are preferred; absolute paths inside targetPath are normalized.",@@ -176,11 +181,7 @@ const openSchema = {.describe("Existing workspace ID to reopen without changing its setup. When provided, omit all other fields.",),- targetPath: z- .string()- .trim()- .min(1)- .max(4096)+ targetPath: pathSchema.optional().describe("Optional resolved local target path."),targetSummary: z@@ -216,18 +217,10 @@ const startPromptOnlyScanSchema = {.describe("Prompt-driven scan mode. Deep Scan uses start_codex_security_deep_scan instead.",),- scope: z- .string()- .trim()- .min(1)- .max(4096)- .describe("Directory inside targetPath. Use '.' for the whole target."),- targetPath: z- .string()- .trim()- .min(1)- .max(4096)- .describe("Resolved local target path."),+ scope: pathSchema.describe(+ "Directory inside targetPath. Use '.' for the whole target.",+ ),+ targetPath: pathSchema.describe("Resolved local target path."),targetSummary: z.string().trim()@@ -240,17 +233,8 @@ const startPromptOnlyScanSchema = {.describe("Optional security focus supplied by the user."),};const startHeadlessStandardScanSchema = {- targetPath: z- .string()- .trim()- .min(1)- .max(4096)- .describe("Resolved local target path."),- scope: z- .string()- .trim()- .min(1)- .max(4096)+ targetPath: pathSchema.describe("Resolved local target path."),+ scope: pathSchema.optional().describe("Optional directory inside targetPath. Omit it or use '.' for the whole target.",@@ -329,22 +313,22 @@ const requestUserInputSchema = {),};const targetInspectionSchema = {- targetPath: z.string().trim().min(1).max(4096),+ targetPath: pathSchema,};const submissionSchema = {diffTarget: diffTargetSchema.optional(),mode: z.enum(["diff", "standard", "deep"]),- scope: z.string().trim().min(1).max(4096),+ scope: pathSchema,sessionId: z.string().uuid(),- targetPath: z.string().trim().min(1).max(4096),+ targetPath: pathSchema,targetSummary: z.string().trim().max(2400).optional(),userContext: editableUserContextSchema.optional(),};const setupInspectionSchema = {diffTarget: diffTargetSchema.optional(),mode: z.enum(["diff", "standard", "deep"]),- scope: z.string().trim().min(1).max(4096),- targetPath: z.string().trim().min(1).max(4096),+ scope: pathSchema,+ targetPath: pathSchema,};const scanSchema = { scanId: z.string().uuid() };const startDeepScanSchema = {@@ -353,20 +337,12 @@ const startDeepScanSchema = {.uuid().optional().describe("Existing app-created or previously returned Deep Scan ID."),- targetPath: z- .string()- .trim()- .min(1)- .max(4096)+ targetPath: pathSchema.optional().describe("Resolved local target path for a first terminal or headless Deep Scan call.",),- scope: z- .string()- .trim()- .min(1)- .max(4096)+ scope: pathSchema.optional().describe("Scope inside targetPath. Deep Scan currently requires the whole target.",@@ -519,7 +495,7 @@ const findingRemediationSchema = {.string().regex(/^sha256:[a-f0-9]{64}$/).optional(),- patchPath: z.string().trim().min(1).max(4096).optional(),+ patchPath: pathSchema.optional(),requestId: z.string().uuid(),state: z.enum(["generated", "applied", "verifying", "verified", "failed"]),summary: z.string().trim().max(2400).optional(),@@ -939,8 +915,7 @@ export function createCodexSecurityServer(): McpServer {"inspect-setup","--target-path",targetPath,- "--scope",- scope,+ `--scope=${scope}`,"--mode",mode,...diffTargetArgs(diffTarget),@@ -979,8 +954,7 @@ export function createCodexSecurityServer(): McpServer {sessionId,"--target-path",targetPath,- "--scope",- scope,+ `--scope=${scope}`,"--mode",mode,...definedArg("--target-summary", targetSummary),@@ -1678,8 +1652,7 @@ export function createCodexSecurityServer(): McpServer {"fail-scan","--scan-id",scanId,- "--message",- message,+ `--message=${message}`,...optionalArg("--claim-token", handoffClaimToken),]);deepScanCoordinators.get(scanId)?.failExternallyPersisted(message);@@ -2036,8 +2009,7 @@ async function startPromptOnlyScan(threadId,"--target-path",targetPath,- "--scope",- scope,+ `--scope=${scope}`,"--mode",mode,...optionalArg("--model", modelSettings.model),@@ -2069,8 +2041,7 @@ async function startHeadlessStandardScan(threadId,"--target-path",input.targetPath,- "--scope",- input.scope ?? ".",+ `--scope=${input.scope ?? "."}`,...optionalArg("--model", modelSettings.model),...optionalArg("--reasoning-effort", modelSettings.reasoningEffort),...optionalArg("--target-summary", input.targetSummary),@@ -2378,7 +2349,12 @@ async function executeWorkbenchWithStateSelection(persistentWorkbenchStateSucceeded = true;return result;} catch (error) {- if (!isUnwritableSqliteOpenError(error)) throw error;+ if (+ !isExecError(error) ||+ !("code" in error) ||+ error.code !== WORKBENCH_STATE_UNAVAILABLE_EXIT_CODE+ )+ throw error;const fallbackStateDir = await pinFallbackWorkbenchStateDir();console.error(JSON.stringify({@@ -2403,14 +2379,6 @@ async function executeWorkbench(stateDir?: string,input?: string | Buffer,): Promise<JsonObject> {- const userContextIndex = args.indexOf("--user-context");- const userContext =- userContextIndex === -1 ? undefined : args[userContextIndex + 1];- const workbenchArgs = [...args];- if (userContextIndex !== -1) {- workbenchArgs.splice(userContextIndex, 2, "--user-context-stdin");- }- const workbenchInput = input ?? userContext;const timeout = ["begin-deep-scan","cancel-scan",@@ -2441,7 +2409,7 @@ async function executeWorkbench(: 30_000;const execution = execFileAsync(pythonCommand,- [workbenchScriptPath(), ...workbenchArgs],+ ["-c", WORKBENCH_PYTHON, workbenchScriptPath()],{cwd: PLUGIN_ROOT,windowsHide: true,@@ -2454,12 +2422,14 @@ async function executeWorkbench(timeout,},);- if (workbenchInput !== undefined) {- execution.child.stdin!.on("error", () => {- // The workbench may exit before consuming stdin; surface its process error.- });- execution.child.stdin!.end(workbenchInput);- }+ execution.child.stdin!.on("error", () => {+ // The workbench may exit before consuming stdin; surface its process error.+ });+ // Match native argv's UTF-8 encoding while framing NUL separately from stdin.+ execution.child.stdin!.write(+ `${JSON.stringify(args.map((argument) => argument.toWellFormed()))}\n`,+ );+ execution.child.stdin!.end(input);const { stdout } = await execution.catch((error: unknown) => {if (error instanceof Error &&@@ -2498,11 +2468,11 @@ function workbenchScriptPath(): string {}function optionalArg(name: string, value: string | undefined): string[] {- return value ? [name, value] : [];+ return value ? [`${name}=${value}`] : [];}function definedArg(name: string, value: string | undefined): string[] {- return value === undefined ? [] : [name, value];+ return value === undefined ? [] : [`${name}=${value}`];}function optionalNumberArg(name: string, value: number | undefined): string[] {@@ -2638,13 +2608,3 @@ function deepScanFailureMessage(run: DeepScanRunState): string {"Do not start a replacement Deep Scan, call cancel for this terminal scan, claim a successful or no-findings scan, satisfy a successful-scan output schema, or emit benchmark JSON.",].join("\n");}--function isUnwritableSqliteOpenError(error: unknown): boolean {- return /sqlite3\.OperationalError:\s*unable to open database file/i.test(- isExecError(error)- ? error.stderr- : error instanceof Error- ? error.message- : "",- );-}diff --git a/plugins/codex-security/mcp-app/src/artifact-inventory.ts b/plugins/codex-security/mcp-app/src/artifact-inventory.tsindex f174c6375..dac816920 100644--- a/plugins/codex-security/mcp-app/src/artifact-inventory.ts+++ b/plugins/codex-security/mcp-app/src/artifact-inventory.ts@@ -84,8 +84,7 @@ export async function prepareCodexSecurityReviewItems(helper,"--repo",context.repoRoot,- "--scope",- context.scope ?? ".",+ `--scope=${context.scope ?? "."}`,"--out",destination,];diff --git a/plugins/codex-security/mcp-app/src/deep-scan/store.ts b/plugins/codex-security/mcp-app/src/deep-scan/store.tsindex 5110d5f2e..4928a3691 100644--- a/plugins/codex-security/mcp-app/src/deep-scan/store.ts+++ b/plugins/codex-security/mcp-app/src/deep-scan/store.ts@@ -284,7 +284,7 @@ export class WorkbenchDeepScanStore {? ["--result-manifest-path", update.resultManifestPath]: []),...(update.threadId ? ["--sdk-thread-id", update.threadId] : []),- ...(update.error ? ["--error-message", update.error] : []),+ ...(update.error ? [`--error-message=${update.error}`] : []),...(update.replaceableFailureKind? ["--replaceable-failure-kind", update.replaceableFailureKind]: []),@@ -399,8 +399,7 @@ export class WorkbenchDeepScanStore {"--scan-id",scanId,...this.coordinatorLeaseArgs(scanId),- "--message",- message,+ `--message=${message}`,...(manifestPath ? ["--manifest-path", manifestPath] : []),...(stagedManifestPath? ["--staged-manifest-path", stagedManifestPath]@@ -424,8 +423,7 @@ export class WorkbenchDeepScanStore {...(coordinatorGeneration === undefined? this.coordinatorLeaseArgs(scanId): ["--coordinator-generation", String(coordinatorGeneration)]),- "--message",- message,+ `--message=${message}`,],true,),diff --git a/plugins/codex-security/mcp-app/src/server/workbench-process.ts b/plugins/codex-security/mcp-app/src/server/workbench-process.tsnew file mode 100644index 000000000..813cf6984--- /dev/null+++ b/plugins/codex-security/mcp-app/src/server/workbench-process.ts@@ -0,0 +1,16 @@+// This exit status belongs only to the private MCP launcher, not the Python CLI.+export const WORKBENCH_STATE_UNAVAILABLE_EXIT_CODE = 73;+export const WORKBENCH_PYTHON = `+import json, sys, traceback+from pathlib import Path+sys.path.insert(0, str(Path(sys.argv.pop(1)).resolve().parent))+import workbench_db+sys.argv = [workbench_db.__file__, *json.loads(sys.stdin.buffer.readline())]+try:+ workbench_db.main()+except Exception as error:+ if not getattr(error, "_codex_security_state_unavailable", False):+ raise+ traceback.print_exc()+ sys.exit(${WORKBENCH_STATE_UNAVAILABLE_EXIT_CODE})+`;diff --git a/plugins/codex-security/mcp-app/tests/support/workbench-process.ts b/plugins/codex-security/mcp-app/tests/support/workbench-process.tsnew file mode 100644index 000000000..0187f1c1c--- /dev/null+++ b/plugins/codex-security/mcp-app/tests/support/workbench-process.ts@@ -0,0 +1,24 @@+import { readFile } from "node:fs/promises";+import path from "node:path";+import { pathToFileURL } from "node:url";+import { importModule } from "../import-module.ts";++/** Exercise the production MCP subprocess boundary without registering a server. */+export async function loadWorkbenchProcess(+ transform = (source: string) => source,+) {+ const applicationRoot = path.resolve(import.meta.dirname, "../..");+ const file = path.join(applicationRoot, "server.ts");+ return importModule({+ stdin: {+ contents: `${transform(await readFile(file, "utf8"))}\nexport { executeWorkbench };`,+ loader: "ts",+ resolveDir: applicationRoot,+ },+ define: {+ __dirname: JSON.stringify(applicationRoot),+ "import.meta.url": JSON.stringify(pathToFileURL(file).href),+ },+ loader: { ".md": "text" },+ });+}diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.tsindex b2b98b350..9d04f8520 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts@@ -119,15 +119,15 @@ async function testPrepareUsesTheExistingStandardGenerator() {async function testPrepareListsIgnoredTrackedFilesOnce() {const fixture = await createFixture("ignored tracked files");await runGit(fixture.repoRoot, "init", "-q");- await fixture.writeRepositoryFile(".gitignore", "generated/\n");+ await fixture.writeRepositoryFile(".gitignore", "-generated/\n");for (const name of ["a.ts", "b.ts"]) {await fixture.writeRepositoryFile(- `generated/${name}`,+ `-generated/${name}`,"export const value = 1;\n",);}- await runGit(fixture.repoRoot, "add", "--force", "--", "generated");- const context = { ...fixture.scan, scope: "generated" };+ await runGit(fixture.repoRoot, "add", "--force", "--", "-generated");+ const context = { ...fixture.scan, scope: "-generated" };assert.deepEqual(await inventory.prepareCodexSecurityReviewItems(context), {reviewItemsTotal: 2,@@ -136,7 +136,7 @@ async function testPrepareListsIgnoredTrackedFilesOnce() {limit: 1,});assert.deepEqual(first, {- items: [{ path: "generated/a.ts" }],+ items: [{ path: "-generated/a.ts" }],nextCursor: "1",});assert.deepEqual(@@ -144,7 +144,7 @@ async function testPrepareListsIgnoredTrackedFilesOnce() {cursor: first.nextCursor,limit: 1,}),- { items: [{ path: "generated/b.ts" }] },+ { items: [{ path: "-generated/b.ts" }] },);}diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.tsindex 4ce6e52e5..313a95dd8 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts@@ -555,7 +555,7 @@ model_reasoning_summary = "none"assert.equal(activeFailureState.workers[0].status, "running");assertProcessAlive(failedWorker.pid);- const failureMessage = "fixture unrecoverable failure";+ const failureMessage = "fixture unrecoverable failure\0source";const failureResponse = await server.request(21,"tools/call",@@ -936,11 +936,13 @@ model_reasoning_summary = "none"const helperLaunches = await readLogLines(workbenchLaunchLogPath);for (const command of ["get-scan", "write-scan-draft", "complete-scan"]) {const launches = helperLaunches.filter(- (launch) => launch.args[1] === command,+ (launch) => launch.workbenchArgs[0] === command,);assert.ok(launches.length > 0);for (const launch of launches) {- assert.equal(launch.args[0], workbenchPath);+ assert.equal(launch.args[0], "-c");+ assert.equal(launch.args[2], workbenchPath);+ assert.equal(launch.args.length, 3);assert.equal(launch.cwd, pluginRoot);}}@@ -1239,17 +1241,19 @@ async function writePythonWrapper(executablePath: string) {import { appendFileSync, existsSync, unlinkSync } from "node:fs";import { spawnSync } from "node:child_process";const args = process.argv.slice(2);-appendFileSync(process.env.FAKE_WORKBENCH_LAUNCH_LOG, JSON.stringify({ args, cwd: process.cwd() }) + '\\n');+const input = Buffer.concat(await process.stdin.toArray());+const workbenchArgs = JSON.parse(input.subarray(0, input.indexOf(10)).toString('utf8'));+appendFileSync(process.env.FAKE_WORKBENCH_LAUNCH_LOG, JSON.stringify({ args, workbenchArgs, cwd: process.cwd() }) + '\\n');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 (workbenchArgs[0] === 'cancel-scan') {+ appendFileSync(process.env.FAKE_WORKBENCH_CANCEL_LOG, JSON.stringify(workbenchArgs) + '\\n');}-if (args[1] === 'cancel-scan' && control && existsSync(control)) {+if (workbenchArgs[0] === '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' });+const result = spawnSync(process.env.REAL_PYTHON || 'python3', args, { input, stdio: ['pipe', 'inherit', 'inherit'] });if (result.error) throw result.error;process.exit(result.status ?? 1);`,diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.tsindex 0f1edf473..135025ee3 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.ts+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.ts@@ -27,6 +27,7 @@ export { recordCodexSecurityScanDraftViaWorkbench } from "./src/artifact-scan-dr},});+await testFreeformFailureMessagesAgainstRealWorkbench();await testReducerCommitAndFinishAgainstRealWorkbench();await testReducerCommitAndFinishAgainstRealWorkbench(true);await testExpiredDeadlineWithoutCompletedDiscoveryAgainstRealWorkbench();@@ -65,6 +66,39 @@ function createWorkbenchRunner(environment: NodeJS.ProcessEnv, bounded = true) {};}+async function testFreeformFailureMessagesAgainstRealWorkbench() {+ const { fixtureRoot, targetPath, environment } = await createWorkbenchFixture(+ "deep-scan-error-transport-",+ );+ const store = new WorkbenchDeepScanStore(createWorkbenchRunner(environment));+ try {+ const run = await store.begin({+ targetPath,+ threadId: "error-owner",+ scanRoot: path.join(fixtureRoot, "scans"),+ });+ await store.claimCoordinator({+ scanId: run.scanId,+ threadId: "error-owner",+ });+ const worker = await createWorkerFixture(run, "discovery", "discovery");+ await store.updateWorker({ ...worker, status: "running" });+ const message = "--provider-error=café\nretry the request";+ const failed = await store.updateWorker({+ ...worker,+ status: "failed",+ error: message,+ });+ assert.equal(failed.error, message);+ assert.equal((await store.fail(run.scanId, message)).error, message);+ const reloaded = await store.get(run.scanId, "error-owner");+ assert.equal(reloaded.error, message);+ assert.equal(reloaded.persistedWorkers[0].error, message);+ } finally {+ await rm(fixtureRoot, { recursive: true, force: true });+ }+}+async function testRecoveredPublicationRejectsLateFailure() {const { fixtureRoot, targetPath, environment } = await createWorkbenchFixture("deep-scan-publication-failure-",@@ -154,7 +188,7 @@ async function testNoopStoppedRefreshRetainsPublicationFailure() {"noop-publication-owner",]);const message =- "Saved result publication failed: fixture no-op publication failure";+ "--publication-error=café\nSaved result publication failed.";await store.recordStoppedPublicationFailure(run.scanId,message,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.tsindex 9c1a9ad0b..bf7f86456 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@@ -13,6 +13,7 @@ import {readFile,realpath,rm,+ symlink,writeFile,} from "node:fs/promises";import { tmpdir } from "node:os";@@ -371,25 +372,159 @@ async function assertUnavailableUserInputFallback() {}}-async function assertWorkspaceWorksWithoutUiCapability() {+async function assertWorkspaceWorksWithoutUiCapability(scope: string) {+ const fixtureRoot = await temporaryDirectory("workbench-text-target-");+ const targetPath = path.join(+ fixtureRoot,+ process.platform === "win32" ? "target" : "target ",+ );+ await mkdir(targetPath);+ const alias = path.join(fixtureRoot, "alias");+ if (process.platform !== "win32") {+ await mkdir(path.join(fixtureRoot, "target"));+ await symlink(targetPath, alias, "dir");+ }+ await mkdir(path.join(targetPath, scope));+ if (scope !== scope.trim() && scope.trim() !== "./")+ await mkdir(path.join(targetPath, scope.trim()));+ await writeFile(path.join(targetPath, scope, "source.py"), "pass\n");+ await writeFile(path.join(targetPath, "outside.py"), "pass\n");+ const userContext = "--data\nUnicode Ä 日本語\n".repeat(200).trim();const nonUiStateDir = path.join(tmpdir(), randomUUID());const nonUiServer = startTestServer({cwd: pluginRoot,- env: { CODEX_SECURITY_STATE_DIR: nonUiStateDir },+ env: {+ CODEX_SECURITY_STATE_DIR: nonUiStateDir,+ CODEX_SECURITY_SCAN_ROOT: path.join(fixtureRoot, "scans"),+ },});try {assertNoError(await nonUiServer.initialize("codex-security-non-ui-smoke"));+ const inspected = await nonUiServer.callTool(10, {+ name: "inspect_codex_security_target",+ arguments: {+ targetPath: process.platform === "win32" ? targetPath : alias,+ },+ });+ assertNoError(inspected);+ const canonicalTarget =+ inspected.result.structuredContent.target.targetPath;+ assert.equal(canonicalTarget, await realpath(targetPath));+ const reinspected = await nonUiServer.callTool(11, {+ name: "inspect_codex_security_target",+ arguments: { targetPath: canonicalTarget },+ });+ assertNoError(reinspected);+ assert.equal(+ reinspected.result.structuredContent.target.targetPath,+ canonicalTarget,+ );const response = await nonUiServer.callTool(2, {name: "open_codex_security_workspace",- arguments: { targetPath: target, mode: "standard", scope: "." },+ arguments: {+ targetPath: canonicalTarget,+ mode: "standard",+ scope: `./${scope}`,+ targetTitle: "--user-context",+ targetSummary: "--fixture-summary",+ userContext,+ },_meta: { "openai/threadId": "fixture-non-ui-thread" },});assertNoError(response);const workspace = response.result.structuredContent.workspace;assert.match(workspace.id, /^[0-9a-f-]{36}$/);- assert.equal(workspace.targetPath, await realpath(target));+ assert.equal(workspace.targetPath, await realpath(targetPath));assert.equal(workspace.mode, "standard");- assert.equal(workspace.scope, ".");+ assert.equal(workspace.scope, scope);+ assert.equal(workspace.targetTitle, "--user-context");+ assert.equal(workspace.targetSummary, "--fixture-summary");+ assert.equal(workspace.userContext, userContext);+ const saved = await nonUiServer.callTool(3, {+ name: "submit_codex_security_setup",+ arguments: {+ sessionId: workspace.id,+ targetPath: workspace.targetPath,+ scope: workspace.scope,+ mode: workspace.mode,+ targetSummary: "",+ },+ });+ assertNoError(saved);+ assert.equal(saved.result.structuredContent.workspace.scope, scope);+ assert.equal(saved.result.structuredContent.workspace.targetSummary, null);+ assertNoError(+ await nonUiServer.callTool(4, {+ name: "list_codex_security_scans",+ arguments: { query: "-sub" },+ }),+ );+ const setup = await nonUiServer.callTool(12, {+ name: "inspect_codex_security_setup",+ arguments: {+ targetPath: workspace.targetPath,+ scope: workspace.scope,+ mode: workspace.mode,+ },+ });+ assertNoError(setup);+ assert.equal(setup.result.structuredContent.setup.scope, scope);+ const started = await nonUiServer.callTool(13, {+ name: "start_codex_security_scan",+ arguments: { sessionId: workspace.id },+ });+ assertNoError(started);+ assert.equal(+ started.result.structuredContent.workspace.targetPath,+ canonicalTarget,+ );+ assert.equal(started.result.structuredContent.workspace.scope, scope);+ for (const [index, name] of [+ "start_codex_security_prompt_only_scan",+ "start_codex_security_standard_scan",+ ].entries()) {+ const launched = await nonUiServer.callTool(14 + index, {+ name,+ arguments: {+ targetPath: canonicalTarget,+ scope,+ ...(index === 0 ? { mode: "standard" } : {}),+ },+ _meta: { "openai/threadId": `fixture-path-launch-${index}` },+ });+ assertNoError(launched);+ assert.equal(+ launched.result.structuredContent.scan.targetPath,+ canonicalTarget,+ );+ assert.equal(launched.result.structuredContent.scan.scope, scope);+ assert.deepEqual(+ launched.result.structuredContent.scan.contract.scope+ .requiredIncludePaths,+ [scope],+ );+ }+ const inventoryPath = path.join(fixtureRoot, "in_scope_files.txt");+ execFileSync(process.env.PYTHON?.trim() || "python3", [+ path.join(pluginRoot, "scripts", "generate_in_scope_files.py"),+ "--repo",+ canonicalTarget,+ `--scope=${scope}`,+ "--out",+ inventoryPath,+ ]);+ assert.equal(await readFile(inventoryPath, "utf8"), `${scope}/source.py\n`);+ for (const [index, blank] of ["", " \t\n"].entries()) {+ const invalid = await nonUiServer.callTool(16 + index, {+ name: "inspect_codex_security_setup",+ arguments: {+ targetPath: canonicalTarget,+ scope: blank,+ mode: "standard",+ },+ });+ assert.equal(invalid.result.isError, true);+ }assert.equal(workspace.setup.submitted, false);assert.ok((await readFile(path.join(nonUiStateDir, "workbench.sqlite3"))).length >@@ -398,6 +533,7 @@ async function assertWorkspaceWorksWithoutUiCapability() {} finally {await nonUiServer.stop();await rm(nonUiStateDir, { recursive: true, force: true });+ await rm(fixtureRoot, { recursive: true, force: true });}}@@ -570,7 +706,12 @@ async function assertDeepScanPersistsRetryableWorkerStartupError() {const fixtureRoot = await temporaryDirectory("codex-security-deep-inventory-",);- const fixtureTarget = path.join(fixtureRoot, "repository");+ const fixtureTarget = path.join(+ fixtureRoot,+ process.platform === "win32" ? "repository" : "repository ",+ );+ if (process.platform !== "win32")+ await mkdir(path.join(fixtureRoot, "repository"));const fixtureState = path.join(fixtureRoot, "state");const fixtureScanRoot = path.join(fixtureRoot, "scans");await mkdir(path.join(fixtureTarget, "app"), { recursive: true });@@ -592,7 +733,7 @@ async function assertDeepScanPersistsRetryableWorkerStartupError() {deepServer.sendRequest(2, "tools/call", {name: "start_codex_security_deep_scan",- arguments: { targetPath: fixtureTarget },+ arguments: { targetPath: fixtureTarget, scope: fixtureTarget },_meta: {"openai/threadId": "fixture-deep-inventory-thread","codex/sandbox-state-meta": parentSandboxState,@@ -633,6 +774,8 @@ async function assertDeepScanPersistsRetryableWorkerStartupError() {),);assert.equal(deepScan.status, "running");+ assert.equal(deepScan.targetPath, await realpath(fixtureTarget));+ assert.equal(deepScan.scope, ".");startupErrorWorker = deepScan.workers.find((worker: { status: string; error?: string }) =>worker.error?.includes("missing-deep-scan-codex"),@@ -1297,7 +1440,12 @@ try {await assertMissingPythonError();await assertWorkbenchStdinFailureDoesNotCrashServer();await assertUnavailableUserInputFallback();- await assertWorkspaceWorksWithoutUiCapability();+ for (const scope of [+ "-sub",+ " component",+ ...(process.platform === "win32" ? [] : ["./ "]),+ ])+ await assertWorkspaceWorksWithoutUiCapability(scope);await assertHeadlessStandardScanWorksWithoutUiCapability();await assertDeepScanPersistsRetryableWorkerStartupError();await assertUserInputFailureLogging();@@ -2787,6 +2935,7 @@ try {rejectedPendingClose.result.content[0].text,/pending remediation operation/,);+ const remediationPatchPath = " remediation.patch";const remediationPatch = `diff --git a/src/a.py b/src/a.py--- a/src/a.py+++ b/src/a.py@@ -2795,9 +2944,13 @@ try {+fixed`;await writeFile(- path.join(initializedScanDir, "remediation.patch"),+ path.join(initializedScanDir, remediationPatchPath),remediationPatch,);+ await writeFile(+ path.join(initializedScanDir, remediationPatchPath.trim()),+ "different patch contents\n",+ );const generatedPatch = await testServer.callTool(62, {name: "set_codex_security_finding_remediation",arguments: {@@ -2806,7 +2959,7 @@ try {requestId: remediationRequestId,expectedVersion: 1,state: "generated",- patchPath: "remediation.patch",+ patchPath: remediationPatchPath,patchDigest: `sha256:${hash("sha256", remediationPatch)}`,summary: "Contain archive extraction under the output root.",},@@ -2822,6 +2975,11 @@ try {.patch,remediationPatch,);+ assert.equal(+ generatedPatch.result.structuredContent.scan.findings[0].remediationState+ .patchPath,+ remediationPatchPath,+ );const applyActionToken = randomUUID();const requestedApply = await testServer.callTool(65, {@@ -2860,7 +3018,11 @@ try {);execFileSync("git",- ["apply", "--no-index", path.join(initializedScanDir, "remediation.patch")],+ [+ "apply",+ "--no-index",+ path.join(initializedScanDir, remediationPatchPath),+ ],{cwd: target,},@@ -2875,6 +3037,9 @@ try {occurrenceId,requestId: remediationRequestId,state: "applied",+ patchPath:+ generatedPatch.result.structuredContent.scan.findings[0]+ .remediationState.patchPath,},});assertNoError(appliedPatch);@@ -3386,15 +3551,20 @@ with sqlite3.connect(sys.argv[1]) as connection:},});assertNoError(rotatedProgress);+ const failureMessage = "--failure\nline";const rotatedFailure = await testServer.callTool(2039, {name: "fail_codex_security_scan",arguments: {handoffClaimToken: rotatedFallbackClaimToken,- message: "rotated continuation stopped",+ message: failureMessage,scanId: fallbackScanId,},});assertNoError(rotatedFailure);+ assert.equal(+ rotatedFailure.result.structuredContent.scan.failureMessage,+ failureMessage,+ );assert.equal(rotatedFailure.result.structuredContent.scan.progress.status,"failed",diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_process.ts b/plugins/codex-security/mcp-app/tests/test_workbench_process.tsindex 7d70734cd..52b8c017a 100644--- a/plugins/codex-security/mcp-app/tests/test_workbench_process.ts+++ b/plugins/codex-security/mcp-app/tests/test_workbench_process.ts@@ -1,11 +1,20 @@import assert from "node:assert/strict";+import { test } from "node:test";+import { randomUUID } from "node:crypto";+import path from "node:path";+import { mkdir, rm } from "node:fs/promises";+import { temporaryDirectory } from "./support/temporary-directories.ts";+import { loadWorkbenchProcess } from "./support/workbench-process.ts";+import { PassThrough } from "node:stream";import type { ExecFileOptionsWithStringEncoding } from "node:child_process";type WorkbenchProcessFixture = (python: string,args: string[],options: ExecFileOptionsWithStringEncoding,-) => Promise<{ stdout: string; stderr: string }>;+) => Promise<{ stdout: string; stderr: string }> & {+ child: { stdin: PassThrough };+};declare global {var workbenchProcessFixture: WorkbenchProcessFixture | undefined;@@ -19,12 +28,6 @@ interface WorkbenchModule {input?: string | Buffer,): Promise<Record<string, unknown>>;}-import { readFile } from "node:fs/promises";-import { fileURLToPath } from "node:url";-import { importModule } from "./import-module.ts";--const applicationRoot = fileURLToPath(new URL("../", import.meta.url));-const source = await readFile(new URL("../server.ts", import.meta.url), "utf8");const invocations: {args: string[];options: ExecFileOptionsWithStringEncoding;@@ -32,29 +35,20 @@ const invocations: {let failure: Error | undefined;globalThis.workbenchProcessFixture = (_python, args, options) => {invocations.push({ args, options });- return failure- ? Promise.reject(failure)- : Promise.resolve({ stdout: "{}", stderr: "" });+ return Object.assign(+ failure+ ? Promise.reject(failure)+ : Promise.resolve({ stdout: "{}", stderr: "" }),+ { child: { stdin: new PassThrough() } },+ );};try {- const { executeWorkbench } = (await importModule({- stdin: {- contents:- source.replace(- "const execFileAsync = promisify(execFile);",- "const execFileAsync = globalThis.workbenchProcessFixture;",- ) + "\nexport { executeWorkbench };",- loader: "ts",- resolveDir: applicationRoot,- },- define: {- __dirname: JSON.stringify(applicationRoot),- "import.meta.url": JSON.stringify(- new URL("../server.ts", import.meta.url).href,- ),- },- loader: { ".md": "text" },- })) as WorkbenchModule;+ const { executeWorkbench } = (await loadWorkbenchProcess((source) =>+ source.replace(+ "const execFileAsync = promisify(execFile);",+ "const execFileAsync = globalThis.workbenchProcessFixture;",+ ),+ )) as WorkbenchModule;for (const command of ["cancel-scan","fail-scan",@@ -97,3 +91,80 @@ try {} finally {delete globalThis.workbenchProcessFixture;}++const { executeWorkbench } = await loadWorkbenchProcess();+const root = await temporaryDirectory("workbench-framing-", true);+try {+ const target = path.join(root, "target");+ await mkdir(target);+ const title = "--message\0café\n日本語😀high\ud800low\udfff";+ const input = Buffer.from(+ "--raw context\0café\n".repeat(20_000).trim(),+ "utf8",+ );+ for (const stdin of [true, false]) {+ const result = await executeWorkbench(+ process.env.PYTHON?.trim() || "python3",+ [+ "create-workspace",+ "--workspace-id",+ randomUUID(),+ "--target-path",+ target,+ `--target-title=${title}`,+ stdin+ ? "--user-context-stdin"+ : `--user-context=${input.toString("utf8")}`,+ ],+ path.join(root, "state"),+ stdin ? input : undefined,+ );+ assert.equal(result.targetTitle, Buffer.from(title).toString("utf8"));+ assert.equal(result.userContext, input.toString("utf8"));+ }+ const artifactRoot = path.join(root, "artifacts");+ await mkdir(artifactRoot, { mode: 0o700 });+ const binary = Buffer.from([0xff, 0x00, 0x0a, 0x0d, 0xfe]);+ const artifactArgs = [+ "--artifact-root",+ artifactRoot,+ "--artifact-path",+ "raw.bin",+ ];+ await executeWorkbench(+ process.env.PYTHON?.trim() || "python3",+ ["save-artifact", ...artifactArgs],+ undefined,+ binary,+ );+ const saved = await executeWorkbench(+ process.env.PYTHON?.trim() || "python3",+ ["read-artifact", ...artifactArgs],+ );+ assert.deepEqual(Buffer.from(saved.content, "base64"), binary);+ const replacementTarget = path.join(root, "target-\ufffd");+ await mkdir(replacementTarget);+ for (const rawBytePath of [false, true]) {+ await test(+ `workbench path framing with ${rawBytePath ? "a raw-byte collision" : "a Unicode target"}`,+ { skip: rawBytePath && ["win32", "darwin"].includes(process.platform) },+ async () => {+ if (rawBytePath) {+ await mkdir(+ Buffer.concat([+ Buffer.from(path.join(root, "target-")),+ Buffer.from([0xff]),+ ]),+ );+ }+ const inspected = await executeWorkbench(+ process.env.PYTHON?.trim() || "python3",+ ["inspect-target", "--target-path", path.join(root, "target-\udcff")],+ );+ assert.equal(inspected.targetPath, replacementTarget);+ },+ );+ }+} finally {+ await rm(root, { recursive: true, force: true });+}diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.ts b/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.tsindex 3258212f0..93ecdd066 100644--- a/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.ts+++ b/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.ts@@ -1,7 +1,10 @@-import { readJsonLines } from "./support/json.ts";import { assertNoError } from "./assertions.ts";+import {+ WORKBENCH_PYTHON,+ WORKBENCH_STATE_UNAVAILABLE_EXIT_CODE,+} from "../src/server/workbench-process.ts";import assert from "node:assert/strict";-import { execFileSync } from "node:child_process";+import { execFileSync, spawnSync } from "node:child_process";import { randomUUID } from "node:crypto";import {chmod,@@ -29,8 +32,6 @@ async function testWorkbenchStateFallback() {await mkdtemp(path.join(tmpdir(), "codex-security-state-fallback-")),);const targetPath = path.join(fixtureRoot, "target");- const fakePythonPath = path.join(fixtureRoot, "fake-python.mjs");- const invocationLog = path.join(fixtureRoot, "python-invocations.jsonl");const serverBundlePath = path.join(pluginRoot,"mcp",@@ -47,12 +48,16 @@ async function testWorkbenchStateFallback() {await mkdir(targetPath, { recursive: true });await writeFile(path.join(targetPath, "fixture.py"), "print('fixture')\n");- await writeFakePython(fakePythonPath);await buildServer(serverBundlePath, { target: "node20" });try {if (process.getuid?.() !== 0) {- for (const firstOperation of ["scan", "standalone"]) {+ for (const firstOperation of [+ "scan",+ "standalone",+ "fresh-home",+ "inspection",+ ]) {const codexHome = path.join(fixtureRoot,`default-${firstOperation}-home`,@@ -63,8 +68,35 @@ async function testWorkbenchStateFallback() {"plugins","codex-security",);- await mkdir(defaultState, { recursive: true });- await chmod(defaultState, 0o500);+ const lockedDirectory =+ firstOperation === "fresh-home" ? codexHome : defaultState;+ await mkdir(lockedDirectory, { recursive: true });+ await chmod(lockedDirectory, 0o500);+ const environment = {+ ...process.env,+ CODEX_HOME: codexHome,+ CODEX_SECURITY_STATE_DIR: undefined,+ };+ const direct = spawnSync(+ realPython,+ [path.join(pluginRoot, "scripts/workbench_db.py"), "database-info"],+ { env: environment, encoding: "utf8" },+ );+ const wrapped = spawnSync(+ realPython,+ [+ "-c",+ WORKBENCH_PYTHON,+ path.join(pluginRoot, "scripts/workbench_db.py"),+ ],+ { env: environment, encoding: "utf8", input: '["database-info"]\n' },+ );+ assert.equal(direct.status, 1);+ assert.equal(wrapped.status, WORKBENCH_STATE_UNAVAILABLE_EXIT_CODE);+ assert.equal(+ wrapped.stderr.trim().split("\n").at(-1),+ direct.stderr.trim().split("\n").at(-1),+ );const server = startServer(serverBundlePath, {CODEX_HOME: codexHome,CODEX_SECURITY_SCAN_ROOT: undefined,@@ -74,6 +106,15 @@ async function testWorkbenchStateFallback() {let fallbackState;try {await initialize(server, 1);+ if (firstOperation === "inspection")+ assertNoError(await inspectTarget(server, 2, targetPath));+ if (firstOperation === "fresh-home") {+ const opened = await Promise.all([+ openWorkspace(server, 20, targetPath, "first-thread"),+ openWorkspace(server, 21, targetPath, "second-thread"),+ ]);+ opened.forEach(assertNoError);+ }const standaloneInput = {targetPath,storage: "persistent",@@ -158,7 +199,7 @@ async function testWorkbenchStateFallback() {);} finally {await server.stop();- await chmod(defaultState, 0o700);+ await chmod(lockedDirectory, 0o700);if (fallbackState)await rm(fallbackState, { recursive: true, force: true });}@@ -252,211 +293,126 @@ async function testWorkbenchStateFallback() {}}- const scanRoot = path.join(fixtureRoot, "fallback-scans");- 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(- fallbackServer,- 3,- targetPath,- );- assertNoError(promptOnly);- assert.equal(- promptOnly.result.structuredContent.startDisposition,- "created",- );- assert.equal(- promptOnly.result.structuredContent.scan.handoffStatus,- "delivered",- );- const opened = await openWorkspace(fallbackServer, 4, targetPath);- assertNoError(opened);- const sessionId = opened.result.structuredContent.workspace.id;- assertNoError(await reopenWorkspace(fallbackServer, 5, sessionId));- assertNoError(- await submitSetup(fallbackServer, 7, sessionId, targetPath),- );- assertNoError(await startScan(fallbackServer, 8, sessionId));- const invocations = await readJsonLines(invocationLog);- const fallbackStateDir = path.join(scanRoot, "workbench-state");- assert.equal(invocations[0].stateDir, null);- assert.equal(- invocations- .slice(1)- .every((entry) => entry.stateDir === fallbackStateDir),- true,- );- assert.equal((await stat(fallbackStateDir)).isDirectory(), true);- const events = fallbackServer- .stderrEvents()- .filter((event) => event.event === "state_fallback_pinned");- assert.equal(events.length, 1);- assert.deepEqual(events[0], {- component: "codex_security_workbench",- event: "state_fallback_pinned",- reason: "persistent_sqlite_unwritable",+ for (const [index, name] of [+ "ordinary-missing",+ "WorkbenchStateDirectoryError:missing",+ "newline\nWorkbenchStateDirectoryError: [Errno 13] Permission denied: state",+ "sqlite3.OperationalError: unable to open database file",+ ].entries()) {+ const codexHome = path.join(fixtureRoot, `collision-${index}`);+ const scanRoot = path.join(fixtureRoot, `collision-scans-${index}`);+ const server = startServer(serverBundlePath, {+ CODEX_HOME: codexHome,+ CODEX_SECURITY_STATE_DIR: undefined,+ CODEX_SECURITY_SCAN_ROOT: scanRoot,+ PYTHON: realPython,});- } finally {- await fallbackServer.stop();- }-- await writeFile(invocationLog, "");- const explicitStateDir = path.join(fixtureRoot, "explicit-state");- 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(- await inspectTarget(explicitServer, 11, targetPath),- /unable to open database file/,- );- assert.deepEqual(- (await readJsonLines(invocationLog)).map((entry) => entry.stateDir),- [explicitStateDir],- );- assert.equal(- explicitServer- .stderrEvents()- .some((event) => event.event === "state_fallback_pinned"),- false,- );- } finally {- await explicitServer.stop();- }-- await writeFile(invocationLog, "");- const inspectionFirstScanRoot = path.join(- fixtureRoot,- "inspection-first-scans",- );- 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));- assertNoError(await openWorkspace(inspectionFirstServer, 17, targetPath));- const fallbackStateDir = path.join(- inspectionFirstScanRoot,- "workbench-state",- );- const invocationStateDirs = (await readJsonLines(invocationLog)).map(- (entry) => entry.stateDir,- );- assert.deepEqual(invocationStateDirs.slice(0, 2), [null, null]);- assert.equal(- invocationStateDirs- .slice(2)- .every((stateDir) => stateDir === fallbackStateDir),- true,- );- assert.ok(invocationStateDirs.length > 2);- const fallbackEvents = inspectionFirstServer- .stderrEvents()- .filter((event) => event.event === "state_fallback_pinned");- assert.equal(fallbackEvents.length, 1);- assert.equal(await pathExists(fallbackStateDir), true);- } finally {- await inspectionFirstServer.stop();+ try {+ await initialize(server, 1);+ const missing = path.join(fixtureRoot, name);+ const failure = await startPromptOnlyScan(server, 2, missing);+ assertToolError(+ failure,+ /Scan target is not a readable local directory/,+ );+ assert.ok(failure.result.content[0].text.includes(missing));+ assertNoError(await openWorkspace(server, 3, targetPath));+ assert.equal(server.stderrEvents().length, 0);+ assert.equal(+ await pathExists(path.join(scanRoot, "workbench-state")),+ false,+ );+ assert.equal(+ await pathExists(+ path.join(+ codexHome,+ "state/plugins/codex-security/workbench.sqlite3",+ ),+ ),+ true,+ );+ } finally {+ await server.stop();+ }}- await writeFile(invocationLog, "");- const provenScanRoot = path.join(fixtureRoot, "proven-scans");- 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(- await openWorkspace(provenServer, 19, targetPath, "proven-thread-1"),- );- assertToolError(- await openWorkspace(provenServer, 20, targetPath, "proven-thread-2"),- /unable to open database file/,- );- assert.deepEqual(- (await readJsonLines(invocationLog)).map((entry) => entry.stateDir),- [null, null],- );- assert.equal(- provenServer- .stderrEvents()- .some((event) => event.event === "state_fallback_pinned"),- false,- );- assert.equal(- await pathExists(path.join(provenScanRoot, "workbench-state")),- false,- );- } finally {- await provenServer.stop();+ for (const scenario of [+ "explicit",+ "explicit-open",+ "proven",+ "proven-open",+ "malformed",+ ]) {+ if (scenario !== "malformed" && process.getuid?.() === 0) continue;+ const codexHome = path.join(fixtureRoot, `${scenario}-home`);+ const stateDir = path.join(codexHome, "state/plugins/codex-security");+ const scanRoot = path.join(fixtureRoot, `${scenario}-scans`);+ await mkdir(stateDir, { recursive: true });+ const server = startServer(serverBundlePath, {+ CODEX_HOME: codexHome,+ CODEX_SECURITY_STATE_DIR:+ scenario === "explicit"+ ? path.join(stateDir, "explicit-state")+ : scenario === "explicit-open"+ ? stateDir+ : undefined,+ CODEX_SECURITY_SCAN_ROOT: scanRoot,+ PYTHON: realPython,+ });+ try {+ await initialize(server, 1);+ if (scenario.startsWith("proven"))+ assertNoError(await openWorkspace(server, 2, targetPath));+ if (scenario === "malformed")+ await writeFile(+ path.join(stateDir, "workbench.sqlite3"),+ "not a database",+ );+ else await chmod(stateDir, 0o500);+ if (scenario === "proven-open")+ await chmod(path.join(stateDir, "workbench.sqlite3"), 0o000);+ assertToolError(+ await openWorkspace(server, 3, targetPath, "next-thread"),+ /Permission denied|readonly|read-only|not a database|unable to open database file/,+ );+ assert.equal(server.stderrEvents().length, 0);+ assert.equal(+ await pathExists(path.join(scanRoot, "workbench-state")),+ false,+ );+ } finally {+ await server.stop();+ await chmod(stateDir, 0o700);+ }}- await writeFile(invocationLog, "");- const genericScanRoot = path.join(fixtureRoot, "generic-scans");- 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(- await inspectTarget(genericServer, 21, targetPath),- /database disk image is malformed/,- );+ const script = path.join(pluginRoot, "scripts/workbench_db.py");+ for (const args of [+ ["--help"],+ ["create-workspace", "--help"],+ ["resolve-scan-root"],+ ["database-info"],+ ["inspect-target", "--target-path", path.join(fixtureRoot, "missing")],+ ]) {+ const env = {+ ...process.env,+ CODEX_SECURITY_STATE_DIR: path.join(fixtureRoot, "parity-state"),+ PYTHONSAFEPATH: "1",+ PYTHONIOENCODING: "ascii",+ };+ const direct = spawnSync(realPython, [script, ...args], {+ env,+ encoding: "utf8",+ });+ const wrapped = spawnSync(realPython, ["-c", WORKBENCH_PYTHON, script], {+ env,+ encoding: "utf8",+ input: `${JSON.stringify(args)}\n`,+ });assert.deepEqual(- (await readJsonLines(invocationLog)).map((entry) => entry.stateDir),- [null],- );- assert.equal(- genericServer- .stderrEvents()- .some((event) => event.event === "state_fallback_pinned"),- false,+ [wrapped.status, wrapped.stdout, wrapped.stderr],+ [direct.status, direct.stdout, direct.stderr],);- } finally {- await genericServer.stop();}} finally {await rm(serverBundlePath, { force: true });@@ -464,32 +420,6 @@ async function testWorkbenchStateFallback() {}}-async function writeFakePython(executablePath: string) {- await writeFile(- executablePath,- [- "#!/usr/bin/env node",- 'import { appendFileSync, readFileSync } from "node:fs";',- 'import { spawnSync } from "node:child_process";',- // Root resolution does not open SQLite and must bypass the injected database failure.- "if (process.argv[3] !== 'resolve-scan-root') {",- "let priorInvocations = 0;",- "try { priorInvocations = readFileSync(process.env.FAKE_PYTHON_LOG, 'utf8').split(/\\r?\\n/).filter(Boolean).length; } catch {}",- "appendFileSync(process.env.FAKE_PYTHON_LOG, JSON.stringify({ stateDir: process.env.CODEX_SECURITY_STATE_DIR || null }) + '\\n');",- "const persistentSuccesses = Number(process.env.FAKE_PYTHON_PERSISTENT_SUCCESSES || 0);",- "if (process.env.FAKE_PYTHON_ALWAYS_FAIL === '1' || (!process.env.CODEX_SECURITY_STATE_DIR && priorInvocations >= persistentSuccesses)) {",- " console.error(process.env.FAKE_PYTHON_FAILURE || 'sqlite3.OperationalError: unable to open database file');",- " process.exit(1);",- "}",- "}",- "const result = spawnSync(process.env.FAKE_REAL_PYTHON, process.argv.slice(2), { env: process.env, stdio: 'inherit' });",- "process.exit(result.status ?? 1);",- "",- ].join("\n"),- );- await chmod(executablePath, 0o755);-}-function startServer(serverPath: string, env: NodeJS.ProcessEnv) {return startRpcServer({@@ -535,29 +465,6 @@ function startPromptOnlyScan(});}-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: ReturnType<typeof startServer>,- id: number,- sessionId: string,-) {- return server.request(id, "tools/call", {- name: "start_codex_security_scan",- arguments: { sessionId },- });-}-function openWorkspace(server: ReturnType<typeof startServer>,id: number,@@ -571,19 +478,6 @@ function openWorkspace(});}-function reopenWorkspace(- server: ReturnType<typeof startServer>,- id: number,- sessionId: string,- threadId = "state-fallback-thread",-) {- return server.request(id, "tools/call", {- name: "open_codex_security_workspace",- arguments: { sessionId },- _meta: { "openai/threadId": threadId },- });-}-function assertToolError(response: {error?: unknown;diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.pyindex 1df07714c..c7a0ffbac 100644--- a/plugins/codex-security/scripts/workbench_db.py+++ b/plugins/codex-security/scripts/workbench_db.py@@ -217,9 +217,19 @@ def release_completion_file_lock(descriptor: int) -> None:def connect(*, deferred: bool = False) -> sqlite3.Connection:path = database_path()- create_private_directory(path.parent)+ try:+ create_private_directory(path.parent)+ except OSError as exc:+ if exc.errno in {errno.EACCES, errno.EPERM, errno.EROFS}:+ exc._codex_security_state_unavailable = True+ raisefor attempt in range(SQLITE_RETRY_ATTEMPTS):- connection = sqlite3.connect(path, timeout=5)+ try:+ connection = sqlite3.connect(path, timeout=5)+ except sqlite3.OperationalError as exc:+ if str(exc) == "unable to open database file":+ exc._codex_security_state_unavailable = True+ raisetry:connection.row_factory = sqlite3.Rowconnection.execute("PRAGMA foreign_keys = ON")@@ -588,7 +598,7 @@ def verify_manifest_binding(scan: sqlite3.Row, manifest: dict[str, Any]) -> Nonedef require_scope(scope: str, mode: str, target: Path) -> str:- value = scope.strip() or "."+ value = scope if scope.strip() else "."requested_scope = Path(value)if "\\" in value and (os.name != "nt" or not requested_scope.is_absolute()):raise SystemExit("Scan scope must use repository-relative POSIX paths.")@@ -602,6 +612,8 @@ def require_scope(scope: str, mode: str, target: Path) -> str:except (RuntimeError, ValueError) as exc:raise SystemExit("Scan scope must stay inside the scanned target.") from excnormalized = relative_scope.as_posix() or "."+ if not normalized.strip():+ normalized = "./" + normalizedif mode == "deep" and normalized != ".":raise SystemExit("Deep Scan is repository-wide and cannot use a scoped path.")if not resolved_scope.is_dir():@@ -647,8 +659,10 @@ def resolve_scan_id(connection: sqlite3.Connection, scan_id: str) -> str:def create_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:workspace_id = require_uuid(args.workspace_id, "workspace-id")timestamp = now()- target_path = optional_text(args.target_path, maximum=4096)- default_scope = optional_text(args.scope, maximum=4096) or "."+ target_path = args.target_path if optional_text(args.target_path, maximum=4096) else None+ default_scope = args.scope if optional_text(args.scope, maximum=4096) else "."+ if any(len(value) > 4096 for value in (target_path, default_scope) if value is not None):+ raise SystemExit("Text value must be no longer than 4096 characters.")diff_target_kind = args.diff_target_kind if args.mode == "diff" else Nonediff_base_revision = (optional_text(args.diff_base_revision, maximum=512) if args.mode == "diff" else None@@ -2487,15 +2501,14 @@ def require_sha256_digest(value: str, label: str) -> str:def require_scan_relative_file(scan: sqlite3.Row, value: str) -> str:- normalized = optional_text(value, maximum=4096)- if normalized is None or "\\" in normalized:+ if len(value) > 4096:+ raise SystemExit("Text value must be no longer than 4096 characters.")+ if not value.strip() or "\\" in value:raise SystemExit("Patch path must identify a scan-local regular file.")- parsed = PurePosixPath(normalized)+ parsed = PurePosixPath(value)if parsed.is_absolute() or ".." in parsed.parts:raise SystemExit("Patch path must identify a scan-local regular file.")- path = artifact_path(Path(scan["scan_dir"]), parsed.as_posix(), required=True)- if path is None:- raise SystemExit("Patch path must identify a scan-local regular file.")+ artifact_path(Path(scan["scan_dir"]), parsed.as_posix(), required=True)return parsed.as_posix()diff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.pyindex fd3bde5fd..1ac00d1cc 100644--- a/plugins/codex-security/tests/test_workbench_db.py+++ b/plugins/codex-security/tests/test_workbench_db.py@@ -966,8 +966,9 @@ def test_completed_findings_are_summarized_and_sorted_by_severity(tmp_path: Path@pytest.mark.parametrize("line_ending", ["\n", "\r\n"], ids=["lf", "crlf"])[email protected]("patch_name", ["remediation.patch", " remediation.patch"])def test_completed_finding_triage_and_remediation_persist(- tmp_path: Path, monkeypatch: pytest.MonkeyPatch, line_ending: str+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, line_ending: str, patch_name: str) -> None:monkeypatch.setenv("GIT_CONFIG_COUNT", "1")monkeypatch.setenv("GIT_CONFIG_KEY_0", "core.autocrlf")@@ -1038,8 +1039,10 @@ def test_completed_finding_triage_and_remediation_persist(state_dir, occurrence_id, "closed", "--close-reason", "already_fixed", check=False)assert "pending remediation operation" in str(pending_close["stderr"])- patch_path = scan_dir / "remediation.patch"+ patch_path = scan_dir / patch_namewrite_remediation_patch(patch_path, newline="\n")+ if patch_name.startswith(" "):+ (scan_dir / patch_name.strip()).write_text("different patch contents\n")generated = set_remediation(state_dir,occurrence_id,@@ -1048,7 +1051,7 @@ def test_completed_finding_triage_and_remediation_persist("1","generated","--patch-path",- patch_path.name,+ patch_name,"--patch-digest",f"sha256:{hashlib.sha256(patch_path.read_bytes()).hexdigest()}","--summary",@@ -1056,7 +1059,7 @@ def test_completed_finding_triage_and_remediation_persist()["scan"]assert generated["findings"][0]["remediationState"]["state"] == "generated"assert generated["findings"][0]["remediationState"]["pendingAction"] is None- assert generated["findings"][0]["remediationState"]["patchPath"] == patch_path.name+ assert generated["findings"][0]["remediationState"]["patchPath"] == patch_nameassert generated["findings"][0]["remediationState"]["patch"] == patch_path.read_bytes().decode()assert generated["findings"][0]["remediationState"]["patchStats"] == {"additions": 1,@@ -1219,6 +1222,8 @@ def test_completed_finding_triage_and_remediation_persist("applied","--base-revision","unversioned",+ "--patch-path",+ str(generated["findings"][0]["remediationState"]["patchPath"]),)["scan"]assert applied["findings"][0]["remediationState"]["state"] == "applied"verify_token = str(uuid.uuid4())@@ -1984,6 +1989,76 @@ def test_workbench_marks_nested_git_paths_as_review_changes_unsupported(tmp_pathassert inspected["targetMetadata"]["reviewChangesSupported"] is False[email protected]("scope", ["src", " component", "component ", "./ "])+def test_workbench_roundtrips_literal_target_and_scope(tmp_path: Path, scope: str) -> None:+ if os.name == "nt" and scope.endswith(" "):+ pytest.skip("Windows removes trailing spaces from directory names.")+ state_dir = tmp_path / "state"+ target = tmp_path / ("target" if os.name == "nt" else "target ")+ target.mkdir()+ if target.name.endswith(" "):+ (tmp_path / "target").mkdir()+ (target / scope).mkdir()+ if scope != scope.strip() and scope.strip() != "./":+ (target / scope.strip()).mkdir()+ inspected = run_workbench(state_dir, "inspect-target", "--target-path", str(target))+ workspace_id = str(uuid.uuid4())+ created = create_workspace(+ state_dir, workspace_id, "--target-path", inspected["targetPath"], "--scope", scope+ )+ assert created["targetPath"] == str(target)+ assert created["scope"] == scope+ saved = save_workspace(+ state_dir, workspace_id, created["targetPath"], created["scope"], "standard"+ )+ assert saved["targetPath"] == str(target)+ assert saved["scope"] == scope+ started = start_scan_command(state_dir, workspace_id, "--scan-root", str(tmp_path / "scans"))+ scan = get_scan(state_dir, started["results"]["scanId"])["scan"]+ assert scan["targetPath"] == str(target)+ assert scan["scope"] == scope+ assert scan["contract"]["scope"]["requiredIncludePaths"] == [scope]++[email protected]("blank", ["", " \t\n", " " * 5000])+def test_workbench_keeps_blank_workspace_path_defaults(tmp_path: Path, blank: str) -> None:+ state_dir = tmp_path / "state"+ workspace_id = str(uuid.uuid4())+ created = create_workspace(state_dir, workspace_id, "--target-path", blank, "--scope", blank)+ assert created["targetPath"] is None+ assert created["scope"] == "."+ target = tmp_path / "target"+ target.mkdir()+ saved = save_workspace(state_dir, workspace_id, str(target), blank, "standard")+ assert saved["targetPath"] == str(target)+ assert saved["scope"] == "."++[email protected]("field", ["scope", "target-path"])[email protected]("leading", [False, True])+def test_workspace_checks_retained_literal_path_length(tmp_path: Path, field: str, leading: bool):+ state_dir = tmp_path / "state"+ value = " " * 5000 + "src" if leading else "src" + " " * 5000+ result = run_workbench(+ state_dir,+ "create-workspace",+ "--workspace-id",+ str(uuid.uuid4()),+ f"--{field}={value}",+ check=False,+ )+ assert result["returncode"] != 0+ assert result["stderr"] == "Text value must be no longer than 4096 characters.\n"+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ assert connection.execute("SELECT COUNT(*) FROM workspaces").fetchone()[0] == 0+++def test_workspace_keeps_literal_scope_at_length_boundary(tmp_path: Path):+ scope = " " * 4093 + "src"+ created = create_workspace(tmp_path / "state", str(uuid.uuid4()), "--scope", scope)+ assert created["scope"] == scope++def test_workbench_opens_invalid_target_for_correction(tmp_path: Path) -> None:state_dir = tmp_path / "state"missing = tmp_path / "missing"diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.pyindex 50ac33fe5..f5b9d64ae 100644--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py@@ -92,6 +92,57 @@ EXPECTED_MIGRATIONS = [][email protected]("code", [errno.EACCES, errno.EPERM, errno.EROFS, errno.ENOSPC])+def test_state_directory_failure_preserves_original_exception(workbench_api, tmp_path, code):+ error = OSError(code, os.strerror(code), str(tmp_path / "state"))+ connect = workbench_api["connect"]+ with (+ mock.patch.dict(+ connect.__globals__,+ {+ "database_path": lambda: tmp_path / "state" / "workbench.sqlite3",+ "create_private_directory": mock.Mock(side_effect=error),+ },+ ),+ pytest.raises(OSError) as failure,+ ):+ connect()+ assert str(failure.value) == str(error)+ assert failure.value is error+ assert getattr(error, "_codex_security_state_unavailable", False) == (code != errno.ENOSPC)++[email protected]("during_open", [True, False])+def test_state_open_failure_metadata_is_limited_to_sqlite_connect(+ workbench_api, tmp_path, during_open+):+ error = sqlite3.OperationalError("unable to open database file")+ connect = workbench_api["connect"]+ connection = sqlite3.connect(":memory:")+ try:+ with (+ mock.patch.dict(+ connect.__globals__,+ {+ "database_path": lambda: tmp_path / "state" / "workbench.sqlite3",+ "apply_migrations": mock.Mock(side_effect=error),+ },+ ),+ mock.patch.object(+ sqlite3,+ "connect",+ side_effect=error if during_open else None,+ return_value=connection,+ ),+ pytest.raises(sqlite3.OperationalError) as failure,+ ):+ connect()+ assert failure.value is error+ assert getattr(error, "_codex_security_state_unavailable", False) is during_open+ finally:+ connection.close()++def create_historical_database(before_version: int,extra_migrations: tuple[tuple[int, str, str], ...] = (),diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.tsindex 70636d32f..a7ec0414b 100644--- a/sdk/typescript/src/api.ts+++ b/sdk/typescript/src/api.ts@@ -2380,8 +2380,7 @@ export class CodexSecurity {activeScan.id,"--cost-json",JSON.stringify(snapshot?.cost ?? failure.cost),- "--message",- failure.message.slice(0, 2400),+ `--message=${failure.message.slice(0, 2400)}`,],);activeScan = null;@@ -2455,8 +2454,7 @@ export class CodexSecurity {"fail-scan","--scan-id",activeScan.id,- "--message",- errorMessage(failure).slice(0, 2400),+ `--message=${errorMessage(failure).slice(0, 2400)}`,...(snapshot?.cost? ["--cost-json", JSON.stringify(snapshot.cost)]: []),@@ -3398,8 +3396,7 @@ export class CodexSecurity {"fail-scan","--scan-id",activeScan.id,- "--message",- errorMessage(error).slice(0, 2400),+ `--message=${errorMessage(error).slice(0, 2400)}`,]).catch(() => undefined);}if (this.#closed) this.#requireOpen();diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex ca44899a0..d40a5331c 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -1975,8 +1975,7 @@ export async function main("closed","--close-reason","false_positive",- "--note",- options.reason,+ `--note=${options.reason}`,]);},});diff --git a/sdk/typescript/src/import-scan.ts b/sdk/typescript/src/import-scan.tsindex c1b8e2cf2..c246375f4 100644--- a/sdk/typescript/src/import-scan.ts+++ b/sdk/typescript/src/import-scan.ts@@ -332,8 +332,7 @@ export async function importScan("fail-scan","--scan-id",activeScan.id,- "--message",- errorMessage(error).slice(0, 2400),+ `--message=${errorMessage(error).slice(0, 2400)}`,]).catch(() => undefined);}if (signal?.aborted) {diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.tsindex ef5124927..776bfaac1 100644--- a/sdk/typescript/src/runtime.ts+++ b/sdk/typescript/src/runtime.ts@@ -1569,6 +1569,16 @@ export async function preparePersistentOutputRoot(return root;}+const WORKBENCH_ARGUMENTS_PROGRAM = String.raw`+import json, sys+sys.argv[2:] = json.loads(sys.stdin.buffer.readline())+`;+const WORKBENCH_SCRIPT_PROGRAM = String.raw`+import runpy, sys+sys.argv = sys.argv[1:]+runpy.run_path(sys.argv[0], run_name="__main__")+`;+const ARCHIVE_READY = "codex-security-archive-ready\n";const ARCHIVE_REGISTRATION_PROGRAM = String.raw`import io, json, runpy, sys@@ -1636,6 +1646,13 @@ export async function runWorkbench(? (options.stateDirectory ??codexSecurityStateDirectory(options.environment)): undefined;+ // OS argv cannot carry NUL, but workbench text fields can.+ const framedArguments =+ !native && arguments_.some((argument) => argument.includes("\0"));+ let program = archiveHandshake ? ARCHIVE_REGISTRATION_PROGRAM : undefined;+ if (framedArguments)+ program =+ WORKBENCH_ARGUMENTS_PROGRAM + (program ?? WORKBENCH_SCRIPT_PROGRAM);const result = await runCodexCommand({ command },native@@ -1645,9 +1662,9 @@ export async function runWorkbench("-X","utf8","-B",- ...(archiveHandshake ? ["-c", ARCHIVE_REGISTRATION_PROGRAM] : []),+ ...(program === undefined ? [] : ["-c", program]),script,- ...arguments_,+ ...(framedArguments ? [] : arguments_),],pluginHelperEnvironment(node?.environment ?? options.environment),// The SDK owns configuration normalization; the helper receives its resolved location.@@ -1663,6 +1680,10 @@ export async function runWorkbench(: input,signal,archiveHandshake,+ // Match native argv's UTF-8 encoding for the private argument frame.+ framedArguments+ ? `${JSON.stringify(arguments_.map((argument) => argument.toWellFormed()))}\n`+ : undefined,);if (!result.success) {throw new Error(@@ -3283,6 +3304,7 @@ export async function runCodexCommand(input?: string | Uint8Array,signal?: AbortSignal,archiveHandshake = false,+ stdinPrefix?: string,): Promise<CodexCommandResult> {const cancellation = archiveHandshake ? new AbortController() : undefined;const abort = () => cancellation?.abort(signal?.reason);@@ -3340,6 +3362,7 @@ export async function runCodexCommand(if (signal?.aborted) abort();}try {+ if (stdinPrefix !== undefined) child.stdin.write(stdinPrefix);if (archiveHandshake) child.stdin.write(`${JSON.stringify(input ?? "")}\n`);else child.stdin.end(input);return await completion;diff --git a/sdk/typescript/src/server/routes.ts b/sdk/typescript/src/server/routes.tsindex fd33f0ca3..05ee3fd89 100644--- a/sdk/typescript/src/server/routes.ts+++ b/sdk/typescript/src/server/routes.ts@@ -141,7 +141,11 @@ async function readJson(request: IncomingMessage): Promise<unknown> {const chunks: Buffer[] = [];for await (const chunk of request) chunks.push(Buffer.from(chunk));try {- return JSON.parse(Buffer.concat(chunks).toString("utf8"));+ return JSON.parse(+ new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(+ Buffer.concat(chunks),+ ),+ );} catch {throw new FindingsError("invalid_request",diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.tsindex 30aa60a85..3d6e6191c 100644--- a/sdk/typescript/tests-ts/api.test.ts+++ b/sdk/typescript/tests-ts/api.test.ts@@ -2404,6 +2404,11 @@ describe("CodexSecurity orchestration", () => {]: [])," if sys.argv[1:2] == ['register-cli-scan'] and '--help' not in sys.argv:",+ ...(mode === "real"+ ? [+ ` sys.argv.remove(${JSON.stringify("--fixture-message=出力\0literal\nsecond line")})`,+ ]+ : [])," payload = sys.stdin.buffer.read()",` Path(${JSON.stringify(join(root, "registration-input.json"))}).write_bytes(payload)`,' sys.stdin = io.TextIOWrapper(io.BytesIO(payload), encoding="utf-8")',@@ -2429,6 +2434,10 @@ describe("CodexSecurity orchestration", () => {if (args[0] === "register-cli-scan") {submitted = `\n${JSON.stringify(JSON.parse(input!), null, 2)}\r\n`;input = submitted;+ // The custom wrapper accepts this text argument before calling the+ // real workbench, exercising framed argv at each archive boundary.+ if (mode === "real")+ args = [...args, "--fixture-message=出力\0literal\nsecond line"];}return runWorkbench(options, args, input);};@@ -5244,8 +5253,7 @@ describe("CodexSecurity orchestration", () => {"fail-scan","--scan-id","scan_example_001",- "--message",- `Scan stopped: short-context budget baseline $0.00488 exceeded the $0.004 limit; estimated cost $0.00488–$0.01156 (standard, context unknown, cache writes unknown); partial output remains at ${scanDir}.`,+ `--message=Scan stopped: short-context budget baseline $0.00488 exceeded the $0.004 limit; estimated cost $0.00488–$0.01156 (standard, context unknown, cache writes unknown); partial output remains at ${scanDir}.`,"--cost-json",JSON.stringify(cost),]);@@ -5313,7 +5321,13 @@ describe("CodexSecurity orchestration", () => {artifact.sha256 = hash("sha256", coverageBytes);await writeFile(manifestPath, `${JSON.stringify(manifest)}\n`);return {- scan: { warnings: [args[args.indexOf("--message") + 1]!] },+ scan: {+ warnings: [+ args+ .find((value) => value.startsWith("--message="))!+ .slice("--message=".length),+ ],+ },};},createCodex: codexFactory(runStreamed),@@ -5354,7 +5368,9 @@ describe("CodexSecurity orchestration", () => {(args) => args[0] === "complete-budget-exhausted-scan",);expect(recovery?.includes("--cost-json")).toBe(true);- expect(recovery?.includes("--message")).toBe(true);+ expect(recovery?.some((value) => value.startsWith("--message="))).toBe(+ true,+ );} finally {clearTimeout(keepAlive);await client.close();@@ -5604,8 +5620,7 @@ describe("CodexSecurity orchestration", () => {"fail-scan","--scan-id",expect.stringMatching(/^[0-9a-f-]{36}$/),- "--message",- "original scan failure",+ "--message=original scan failure",]);const history = await runWorkbench({ python: python!, pluginRoot: PLUGIN_ROOT, environment },@@ -5617,66 +5632,68 @@ describe("CodexSecurity orchestration", () => {await client.close();});- test("preserves original failures in saved scan history", async () => {- const { root, repository, codexHome, scanDir } = await scanDirectories();- const stateDirectory = join(root, "state");- const python = Bun.which("python3") ?? Bun.which("python");- expect(python).not.toBeNull();- const environment = {- PATH: process.env["PATH"] ?? "",- CODEX_SECURITY_STATE_DIR: stateDirectory,- };- const commands: Array<readonly string[]> = [];- const quotedCredential = JSON.stringify({- client_secret_value: "SYNTHETIC correct horse battery staple",- });- const originalFailure = `request failed: token=SYNTHETIC_TOKEN ${quotedCredential}`;- const client = TestClient.withDependencies({- environment,- prepareRuntime: runtimePreparer(codexHome, () => ({ environment })),- resolvePluginPython: async () => python!,- prepareOutputDir: async () => scanDir,- repositoryRevision: async () => "deadbeef",- runWorkbench: async (- options: Parameters<typeof runWorkbench>[0],- args: readonly string[],- input?: string,- ): Promise<JsonObject> => {- commands.push(args);- return await runWorkbench(options, args, input);- },- createCodex: codexFactory(async () => {- async function* failingEvents(): AsyncGenerator<ThreadEvent> {- yield { type: "thread.started", thread_id: "failed-thread" };- yield {- type: "error",- message: originalFailure,- };- }- return { events: failingEvents() };- }),- });+ test.each(["", "\0literal", "\0high\ud800.low\udfff.pair😀"])(+ "preserves original failures in saved scan history (%j)",+ async (suffix) => {+ const { root, repository, codexHome, scanDir } = await scanDirectories();+ const stateDirectory = join(root, "state");+ const python = Bun.which("python3") ?? Bun.which("python");+ expect(python).not.toBeNull();+ const environment = {+ PATH: process.env["PATH"] ?? "",+ CODEX_SECURITY_STATE_DIR: stateDirectory,+ };+ const commands: Array<readonly string[]> = [];+ const quotedCredential = JSON.stringify({+ client_secret_value: "SYNTHETIC correct horse battery staple",+ });+ const originalFailure = `--provider-error: token=SYNTHETIC_TOKEN ${quotedCredential}${suffix}`;+ const client = TestClient.withDependencies({+ environment,+ prepareRuntime: runtimePreparer(codexHome, () => ({ environment })),+ resolvePluginPython: async () => python!,+ prepareOutputDir: async () => scanDir,+ repositoryRevision: async () => "deadbeef",+ runWorkbench: async (+ options: Parameters<typeof runWorkbench>[0],+ args: readonly string[],+ input?: string,+ ): Promise<JsonObject> => {+ commands.push(args);+ return await runWorkbench(options, args, input);+ },+ createCodex: codexFactory(async () => {+ async function* failingEvents(): AsyncGenerator<ThreadEvent> {+ yield { type: "thread.started", thread_id: "failed-thread" };+ yield {+ type: "error",+ message: originalFailure,+ };+ }+ return { events: failingEvents() };+ }),+ });- await expect(client.run(repository)).rejects.toThrow(originalFailure);- const failure = commands.find((args) => args[0] === "fail-scan");- const scanId = failure?.[2] ?? "";- expect(scanId).toMatch(/^[0-9a-f-]{36}$/);- expect(failure?.[3]).toBe("--message");- expect(failure?.[4]).toBe(originalFailure);+ await expect(client.run(repository)).rejects.toThrow(originalFailure);+ const failure = commands.find((args) => args[0] === "fail-scan");+ const scanId = failure?.[2] ?? "";+ expect(scanId).toMatch(/^[0-9a-f-]{36}$/);+ expect(failure?.[3]).toBe(`--message=${originalFailure}`);- // `scans show` reads the stored message back through get-scan.- const context = await runWorkbench(- { python: python!, pluginRoot: PLUGIN_ROOT, environment },- ["get-scan", "--scan-id", scanId],- );- expect(context["scan"]).toMatchObject({- continuationThreadId: "failed-thread",- progress: { status: "failed" },- failureMessage: originalFailure,- });+ // `scans show` reads the stored message back through get-scan.+ const context = await runWorkbench(+ { python: python!, pluginRoot: PLUGIN_ROOT, environment },+ ["get-scan", "--scan-id", scanId],+ );+ expect(context["scan"]).toMatchObject({+ continuationThreadId: "failed-thread",+ progress: { status: "failed" },+ failureMessage: Buffer.from(originalFailure).toString("utf8"),+ });- await client.close();- });+ await client.close();+ },+ );test("retains default scan output under persistent plugin state", async () => {const { root, repository, codexHome } = await runtimeDirectories();diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.tsindex f3fb62dd0..2d4cd157c 100644--- a/sdk/typescript/tests-ts/cli.test.ts+++ b/sdk/typescript/tests-ts/cli.test.ts@@ -448,7 +448,7 @@ describe("CLI", () => {});test("marks findings as false positives without starting Codex", async () => {- const reason = " Not reachable from untrusted input. ";+ const reason = " --dry-run is an option in the sample. ";const expectedReason = reason.trim();const response: JsonObject = {scan: {@@ -498,8 +498,7 @@ describe("CLI", () => {"closed","--close-reason","false_positive",- "--note",- expectedReason,+ `--note=${expectedReason}`,],]);expect(JSON.parse(stdout.text())).toEqual(response);diff --git a/sdk/typescript/tests-ts/findings-server.test.ts b/sdk/typescript/tests-ts/findings-server.test.tsindex 2a2bbbe50..ef1248e4f 100644--- a/sdk/typescript/tests-ts/findings-server.test.ts+++ b/sdk/typescript/tests-ts/findings-server.test.ts@@ -1325,6 +1325,48 @@ print(db.execute("SELECT COUNT(*) FROM finding_embeddings").fetchone()[0])`;]);});+test("rejects invalid UTF-8 before embedding or storing findings", async () => {+ const { store } = await fixture();+ const embed = mock(embedder.embed);+ const base = await start(store, { embed });+ const write = spyOn(store, "insert");+ try {+ const valid = { ...finding(), title: "Synthetic � 🧪" };+ const body = Buffer.from(JSON.stringify({ findings: [valid] }));+ const offset = body.indexOf(Buffer.from("�"));+ for (const bytes of [Buffer.from([0xff]), Buffer.from([0xe2, 0x82])]) {+ const response = await fetch(base + "/v1/bulk/findings", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: Buffer.concat([+ body.subarray(0, offset),+ bytes,+ body.subarray(offset + Buffer.byteLength("�")),+ ]),+ });+ expect(response.status).toBe(400);+ expect(await response.json()).toMatchObject({ error: "invalid_request" });+ }+ expect(+ (+ await fetch(base + "/v1/bulk/findings", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), body]),+ })+ ).status,+ ).toBe(400);+ expect(embed).not.toHaveBeenCalled();+ expect(write).not.toHaveBeenCalled();+ expect((await insert(base, [valid])).status).toBe(201);+ expect((await store.list({ limit: 10, offset: 0 })).findings).toEqual([+ valid,+ ]);+ } finally {+ write.mockRestore();+ }+});+test("dashboard can sort and search a stored title with an unpaired surrogate", async () => {const { store } = await fixture();const base = await start(store);diff --git a/sdk/typescript/tests-ts/import-scan.test.ts b/sdk/typescript/tests-ts/import-scan.test.tsindex 0c5d0079a..93f5ea901 100644--- a/sdk/typescript/tests-ts/import-scan.test.ts+++ b/sdk/typescript/tests-ts/import-scan.test.ts@@ -748,7 +748,7 @@ test.each(["failure", "abort"] as const)(const controller = new AbortController();const run: typeof runWorkbench = async (options, args, input) => {if (args[0] === "prepare-scan-completion" && mode === "failure") {- throw new Error("Synthetic import completion failure");+ throw new Error("--synthetic-import-completion-failure");}const result = await runWorkbench(options, args, input);if (args[0] === "register-cli-scan" && mode === "abort")@@ -766,7 +766,7 @@ test.each(["failure", "abort"] as const)(await expect(operation).rejects.toBeInstanceOf(ScanInterruptedError);elseawait expect(operation).rejects.toThrow(- "Synthetic import completion failure",+ "--synthetic-import-completion-failure",);const scans = await storedScans(context);expect(scans).toHaveLength(1);diff --git a/sdk/typescript/tests-ts/prompt-only-start-timeout.test.ts b/sdk/typescript/tests-ts/prompt-only-start-timeout.test.tsindex 8985615e0..9f0d60788 100644--- a/sdk/typescript/tests-ts/prompt-only-start-timeout.test.ts+++ b/sdk/typescript/tests-ts/prompt-only-start-timeout.test.ts@@ -1,4 +1,5 @@import { expect, test } from "bun:test";+import { PassThrough } from "node:stream";import { loadBundledRuntime, PLUGIN_ROOT } from "./plugin-root.js";test("gives prompt-only scan startup the five-minute scan timeout", async () => {@@ -14,16 +15,20 @@ test("gives prompt-only scan startup the five-minute scan timeout", async () =>const executeWorkbench = new Function(execFileHelper!,"workbenchScriptPath",+ "WORKBENCH_PYTHON","PLUGIN_ROOT","isRecord",`${source}\nreturn executeWorkbench;`,)(- async (- _command: string,- _args: string[],- options: { timeout: number },- ) => ({ stdout: JSON.stringify({ timeout: options.timeout }) }),+ (_command: string, _args: string[], options: { timeout: number }) =>+ Object.assign(+ Promise.resolve({+ stdout: JSON.stringify({ timeout: options.timeout }),+ }),+ { child: { stdin: new PassThrough() } },+ ),() => "workbench.py",+ "fixture private workbench launcher",PLUGIN_ROOT,() => true,) as (command: string, args: string[]) => Promise<{ timeout: number }>;diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.tsindex 2c847c974..12e824ae1 100644--- a/sdk/typescript/tests-ts/runtime.test.ts+++ b/sdk/typescript/tests-ts/runtime.test.ts@@ -5317,46 +5317,60 @@ describe("runtime directories and plugin Python boundary", () => {},);- test("runs workbench commands without output limits, credentials, or generated bytecode", async () => {- const root = await temporaryDirectory();- const pluginRoot = join(root, "plugin");- await mkdir(join(pluginRoot, "scripts"), { recursive: true });- await writeFile(- join(pluginRoot, "scripts", "workbench_db.py"),+ test.each(+ [+ ["test-command"],+ ["test-command", "--message=出力\0literal\nsecond line"],[- "import json, os, sys",- "assert sys.flags.isolated",- "assert sys.dont_write_bytecode",- "assert sys.argv[1] == 'test-command'",- "assert os.environ.get('OPENAI_API_KEY') is None",- "assert os.environ.get('CODEX_API_KEY') is None",- "assert os.environ.get('OPENROUTER_API_KEY') is None",- "assert os.environ.get('FIREWORKS_API_KEY') is None",- "payload = sys.stdin.read()",- "print(json.dumps({'ok': True, 'label': '出力', 'inputLength': len(payload), 'details': 'x' * (5 * 1024 * 1024)}, ensure_ascii=False))",- ].join("\n"),- );- const python = await resolvePluginPython();- const result = await runWorkbench(- {- python,- pluginRoot,- environment: {- PATH: process.env["PATH"],- OPENAI_API_KEY: "must-not-reach-python",- CODEX_API_KEY: "also-must-not-reach-python",- OPENROUTER_API_KEY: "openrouter-must-not-reach-python",- FIREWORKS_API_KEY: "fireworks-must-not-reach-python",+ "test-command",+ "--message=\0high\ud800.low\udfff.pair😀",+ "path-\udcff",+ ],+ ].map((args) => ({ args })),+ )(+ "runs custom workbench $args without output limits, credentials, or generated bytecode",+ async ({ args }) => {+ const root = await temporaryDirectory();+ const pluginRoot = join(root, "plugin");+ await mkdir(join(pluginRoot, "scripts"), { recursive: true });+ await writeFile(+ join(pluginRoot, "scripts", "workbench_db.py"),+ [+ "import json, os, sys",+ "assert sys.flags.isolated",+ "assert sys.dont_write_bytecode",+ `assert sys.argv[1:] == json.loads(${JSON.stringify(JSON.stringify(args.map((arg) => Buffer.from(arg).toString("utf8"))))})`,+ "assert os.environ.get('OPENAI_API_KEY') is None",+ "assert os.environ.get('CODEX_API_KEY') is None",+ "assert os.environ.get('OPENROUTER_API_KEY') is None",+ "assert os.environ.get('FIREWORKS_API_KEY') is None",+ 'payload = sys.stdin.buffer.read().decode("utf-8")',+ String.raw`assert payload == "\n\0" + "出力" * (32 * 1024)`,+ "print(json.dumps({'ok': True, 'label': '出力', 'inputLength': len(payload), 'details': 'x' * (5 * 1024 * 1024)}, ensure_ascii=False))",+ ].join("\n"),+ );+ const python = await resolvePluginPython();+ const result = await runWorkbench(+ {+ python,+ pluginRoot,+ environment: {+ PATH: process.env["PATH"],+ OPENAI_API_KEY: "must-not-reach-python",+ CODEX_API_KEY: "also-must-not-reach-python",+ OPENROUTER_API_KEY: "openrouter-must-not-reach-python",+ FIREWORKS_API_KEY: "fireworks-must-not-reach-python",+ },},- },- ["test-command"],- "x".repeat(64 * 1024),- );- expect(result["ok"]).toBe(true);- expect(result["label"]).toBe("出力");- expect(result["inputLength"]).toBe(64 * 1024);- expect(result["details"]).toHaveLength(5 * 1024 * 1024);- });+ args,+ "\n\0" + "出力".repeat(32 * 1024),+ );+ expect(result["ok"]).toBe(true);+ expect(result["label"]).toBe("出力");+ expect(result["inputLength"]).toBe(64 * 1024 + 2);+ expect(result["details"]).toHaveLength(5 * 1024 * 1024);+ },+ );test.each([["unbound", false],