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(plugin): initialize workbench databases with Node SQLite (#1335)

mldangelo-oaia44eda3ea2bd
diff --git a/plugins/codex-security/mcp-app/helpers-main.ts b/plugins/codex-security/mcp-app/helpers-main.ts
index 15d5e6248..258a7ffc6 100644
--- a/plugins/codex-security/mcp-app/helpers-main.ts
+++ b/plugins/codex-security/mcp-app/helpers-main.ts
@@ -1,4 +1,5 @@
import { closeSync, readFileSync } from "node:fs";
+import { parseArgs } from "node:util";
import { resolveSecurityMdCommand } from "./src/helpers/resolve-security-md";
import { decodePosixBytes } from "./src/helpers/posix-path";
import { windowsBinding } from "./src/native";
@@ -8,6 +9,8 @@ import { deepReviewInputCommand } from "./src/helpers/deep-review-input";
import { rankShardsCommand } from "./src/helpers/rank-shards";
import { rankPoolCommand } from "./src/helpers/rank-pool";
import { bindRepoScopesCommand } from "./src/helpers/bind-repo-scopes";
+import { escapeControls } from "./src/helpers/json";
+import { decodeUtf8 } from "./src/helpers/utf8";
let commandLine = process.argv.slice(2);
if (process.platform === "win32") {
@@ -58,9 +61,40 @@ if (command === "resolve-security-md") {
process.exitCode = rankPoolCommand(command, args, posixHome);
} else if (command === "bind-repo-scopes") {
process.exitCode = bindRepoScopesCommand(args, posixHome);
+} else if (command === "database-info") {
+ void (async () => {
+ const { values } = parseArgs({
+ args,
+ options: { help: { type: "boolean", short: "h" } },
+ });
+ if (values.help) {
+ console.log(
+ "Usage: database-info (reads a JSON absolute state-directory string from stdin)",
+ );
+ return;
+ }
+ const { databaseInfo } = await import("./src/workbench/database");
+ console.log(
+ JSON.stringify(
+ await databaseInfo(JSON.parse(decodeUtf8(readFileSync(0)))),
+ ).replace(/[\p{Cc}\p{Cf}]/gu, (character) =>
+ character
+ .split("")
+ .map(
+ (unit) => `\\u${unit.charCodeAt(0).toString(16).padStart(4, "0")}`,
+ )
+ .join(""),
+ ),
+ );
+ })().catch((error: unknown) => {
+ console.error(
+ escapeControls(error instanceof Error ? error.message : String(error)),
+ );
+ process.exitCode = 1;
+ });
} else {
console.error(
- "Usage: launch_codex_security_mcp[.cmd] --helper <resolve-security-md | normalize-candidates | validate-patch-risk-assessment | copy-deep-review-input | select-deep-review-input | make-rank-shards | validate-rank-shard | merge-rank-outputs | make-rank-pool-plan | validate-rank-worker | validate-rank-pool | bind-repo-scopes> [options]",
+ "Usage: launch_codex_security_mcp[.cmd] --helper <resolve-security-md | normalize-candidates | validate-patch-risk-assessment | copy-deep-review-input | select-deep-review-input | make-rank-shards | validate-rank-shard | merge-rank-outputs | make-rank-pool-plan | validate-rank-worker | validate-rank-pool | bind-repo-scopes | database-info> [options]",
);
process.exitCode = 2;
}
diff --git a/plugins/codex-security/mcp-app/src/workbench/database.ts b/plugins/codex-security/mcp-app/src/workbench/database.ts
new file mode 100644
index 000000000..90cc7c584
--- /dev/null
+++ b/plugins/codex-security/mcp-app/src/workbench/database.ts
@@ -0,0 +1,98 @@
+import {
+ chmodSync,
+ lstatSync,
+ mkdirSync,
+ readlinkSync,
+ realpathSync,
+ statSync,
+} from "node:fs";
+import { dirname, isAbsolute, join, sep } from "node:path";
+import { DatabaseSync } from "node:sqlite";
+import { setTimeout } from "node:timers/promises";
+import { applyMigrations } from "./migrations";
+import { decodePosixBytes, encodePosixPath } from "../helpers/posix-path";
+import { windowsBinding } from "../native";
+import { widePath, windowsFileSystem } from "../../../native/windows-files.mjs";
+
+function createStateDirectory(path: string): void {
+ if (process.platform !== "win32") path = path.replace(/\/+$/u, "") || "/";
+ const nativePath =
+ process.platform === "win32" ? path : encodePosixPath(path);
+ if (statSync(nativePath, { throwIfNoEntry: false })?.isDirectory()) return;
+ const entry = lstatSync(nativePath, { throwIfNoEntry: false });
+ if (entry?.isSymbolicLink()) {
+ const target =
+ process.platform === "win32"
+ ? readlinkSync(nativePath)
+ : decodePosixBytes(readlinkSync(nativePath, { encoding: "buffer" }));
+ createStateDirectory(
+ isAbsolute(target) ? target : `${dirname(path)}${sep}${target}`,
+ );
+ return;
+ }
+ if (!entry) {
+ const parent = dirname(path);
+ if (parent !== path) createStateDirectory(parent);
+ }
+ try {
+ if (process.platform === "win32")
+ windowsFileSystem(windowsBinding()).mkdirPrivate(widePath(path));
+ else mkdirSync(nativePath, { mode: 0o700 });
+ } catch (error) {
+ if (!statSync(nativePath, { throwIfNoEntry: false })?.isDirectory())
+ throw error;
+ }
+}
+
+export async function openWorkbenchDatabase(
+ databasePath: string,
+ { deferred = false }: { deferred?: boolean } = {},
+): Promise<DatabaseSync> {
+ createStateDirectory(dirname(databasePath));
+ for (let attempt = 0; ; attempt++) {
+ const database = new DatabaseSync(databasePath);
+ try {
+ database.exec("PRAGMA foreign_keys = ON; PRAGMA busy_timeout = 5000;");
+ applyMigrations(database, undefined, !deferred || attempt > 0);
+ database.exec("PRAGMA journal_mode = WAL");
+ chmodSync(databasePath, 0o600);
+ return database;
+ } catch (error) {
+ database.close();
+ const busy =
+ error instanceof Error &&
+ "errcode" in error &&
+ [5, 6].includes(Number(error.errcode) & 0xff);
+ if (attempt === 4 || !busy) throw error;
+ await setTimeout(50 * 2 ** attempt);
+ }
+ }
+}
+
+export async function databaseInfo(
+ state: string,
+): Promise<{ databasePath: string }> {
+ if (
+ typeof state !== "string" ||
+ !isAbsolute(state) ||
+ !state.isWellFormed()
+ ) {
+ throw new Error(
+ "database-info requires an absolute Unicode state-directory string.",
+ );
+ }
+ // Keep an ASCII alias usable even when its destination has raw POSIX bytes.
+ const database = await openWorkbenchDatabase(
+ `${state}${sep}workbench.sqlite3`,
+ { deferred: true },
+ );
+ database.close();
+ const canonicalState =
+ process.platform === "win32"
+ ? realpathSync.native(state)
+ : decodePosixBytes(
+ realpathSync.native(encodePosixPath(state), { encoding: "buffer" }),
+ );
+ const databasePath = join(canonicalState, "workbench.sqlite3");
+ return { databasePath };
+}
diff --git a/plugins/codex-security/mcp-app/src/workbench/migrations.ts b/plugins/codex-security/mcp-app/src/workbench/migrations.ts
new file mode 100644
index 000000000..1d6271550
--- /dev/null
+++ b/plugins/codex-security/mcp-app/src/workbench/migrations.ts
@@ -0,0 +1,461 @@
+import { createHash } from "node:crypto";
+import { basename } from "node:path";
+import type { DatabaseSync } from "node:sqlite";
+import history from "../../../shared/workbench-migrations.json";
+import { parseJson, stringifyJson } from "../helpers/json";
+
+export interface Migration {
+ version: number;
+ name: string;
+ statements: readonly string[];
+}
+
+export const migrations: readonly Migration[] = history;
+const migration = (version: number) =>
+ migrations.find((item) => item.version === version)!;
+
+function columns(database: DatabaseSync, table: string): Set<string> {
+ return new Set(
+ database
+ .prepare(`PRAGMA table_info(${table})`)
+ .all()
+ .map((row) => String(row.name)),
+ );
+}
+
+function addColumn(database: DatabaseSync, statement: string): void {
+ const [, table, column] = /^ALTER TABLE\s+(\w+)\s+ADD COLUMN\s+(\w+)/u.exec(
+ statement,
+ )!;
+ if (!columns(database, table).has(column)) database.exec(statement);
+}
+
+function repairAdditive(database: DatabaseSync, version: number): void {
+ for (const statement of migration(version).statements)
+ addColumn(database, statement);
+}
+
+function moveMigration(
+ database: DatabaseSync,
+ from: number,
+ to: number,
+ name: string,
+): void {
+ if (
+ !database
+ .prepare("SELECT 1 FROM schema_migrations WHERE version = ? AND name = ?")
+ .get(from, name)
+ )
+ return;
+ database
+ .prepare(
+ "UPDATE schema_migrations SET version = ? WHERE version = ? AND name = ?",
+ )
+ .run(to, from, name);
+}
+
+function normalizeExecutionProfiles(database: DatabaseSync): void {
+ const scanColumns = columns(database, "scans");
+ const workspaceColumns = columns(database, "workspaces");
+ const recorded = new Map(
+ database
+ .prepare(
+ "SELECT version, name FROM schema_migrations WHERE version IN (11, 12, 25)",
+ )
+ .all()
+ .map((row) => [Number(row.version), String(row.name)]),
+ );
+ const modelName = migration(25).name;
+ if (recorded.get(25) === "dynamic scan execution profiles") {
+ database
+ .prepare("UPDATE schema_migrations SET name = ? WHERE version = 25")
+ .run(modelName);
+ recorded.set(25, modelName);
+ }
+ const legacyNames = new Map([
+ [11, ["scan execution profiles"]],
+ [12, ["scan execution profiles", "dynamic scan execution profiles"]],
+ ]);
+ const legacyHistory = [...legacyNames].some(([version, names]) =>
+ names.includes(recorded.get(version) ?? ""),
+ );
+ const legacyColumns =
+ scanColumns.has("execution_model") ||
+ workspaceColumns.has("execution_model") ||
+ workspaceColumns.has("reasoning_effort");
+ if (!legacyHistory && !legacyColumns) return;
+ const supported = new Map([
+ [11, ["deep scan orchestration state", "scan execution profiles"]],
+ [
+ 12,
+ [
+ "scan continuation threads",
+ "scan execution profiles",
+ "dynamic scan execution profiles",
+ ],
+ ],
+ [25, [modelName]],
+ ]);
+ const invalidHistory = [...supported].some(
+ ([version, names]) =>
+ recorded.has(version) && !names.includes(recorded.get(version)!),
+ );
+ const validColumns = [scanColumns, workspaceColumns].every(
+ (names) =>
+ names.has("execution_model") &&
+ names.has("reasoning_effort") &&
+ !names.has("legacy_execution_model") &&
+ !names.has("legacy_reasoning_effort"),
+ );
+ if (
+ invalidHistory ||
+ (legacyColumns && !validColumns) ||
+ (legacyHistory && !legacyColumns)
+ ) {
+ throw new Error(
+ "The Codex Security database has an unsupported execution-profile migration history.",
+ );
+ }
+ for (const table of ["workspaces", "scans"]) {
+ database.exec(`ALTER TABLE ${table} RENAME COLUMN execution_model TO legacy_execution_model;
+ ALTER TABLE ${table} RENAME COLUMN reasoning_effort TO legacy_reasoning_effort;`);
+ }
+ repairAdditive(database, 25);
+ database.exec(`UPDATE scans SET model = COALESCE(model, legacy_execution_model),
+ reasoning_effort = COALESCE(reasoning_effort, legacy_reasoning_effort)`);
+ const remove = database.prepare(
+ "DELETE FROM schema_migrations WHERE version = ? AND name = ?",
+ );
+ for (const [version, names] of legacyNames)
+ for (const name of names) remove.run(version, name);
+ if (!recorded.has(25)) {
+ database
+ .prepare(
+ "INSERT INTO schema_migrations (version, name, applied_at) VALUES (25, ?, ?)",
+ )
+ .run(modelName, new Date().toISOString());
+ }
+}
+
+function normalizeHistory(database: DatabaseSync): void {
+ const mirror = database
+ .prepare(
+ "SELECT version, name FROM schema_migrations WHERE version BETWEEN 29 AND 32 ORDER BY version",
+ )
+ .all();
+ const mirrorNames = new Map([
+ [29, "freeze stopped scan source digests"],
+ [30, "separate deep scan publication failures"],
+ ]);
+ if (mirror.some((row) => mirrorNames.get(Number(row.version)) === row.name)) {
+ if (
+ mirror.length !== 2 ||
+ mirror.some((row) => mirrorNames.get(Number(row.version)) !== row.name)
+ ) {
+ throw new Error(
+ "The Codex Security database has an unsupported mirror migration history.",
+ );
+ }
+ for (const [from, to] of [
+ [30, 32],
+ [29, 31],
+ ])
+ moveMigration(database, from, to, mirrorNames.get(from)!);
+ }
+ moveMigration(
+ database,
+ 33,
+ 40,
+ "index finding identity and comparison history",
+ );
+ moveMigration(database, 25, 26, "persist scan completion warnings");
+ moveMigration(database, 12, 20, "phase-specific scan progress");
+ normalizeExecutionProfiles(database);
+ moveMigration(database, 13, 21, "current scan preflight state");
+ const shadowed = new Map([
+ [18, ["scan target summaries"]],
+ [
+ 19,
+ [
+ "structured scan guidance context",
+ "idempotent scan lifecycle requests",
+ ],
+ ],
+ [
+ 20,
+ [
+ "retain superseded scan lifecycle requests",
+ "threat model publication receipts",
+ ],
+ ],
+ [
+ 21,
+ [
+ "scan progress projection and activity",
+ "deep coordinator manifest receipts",
+ ],
+ ],
+ [22, ["dynamic scan execution profiles"]],
+ ]);
+ const lookup = database.prepare(
+ "SELECT name FROM schema_migrations WHERE version = ?",
+ );
+ for (const [version, names] of shadowed) {
+ const name = lookup.get(version)?.name;
+ if (typeof name !== "string" || !names.includes(name)) continue;
+ if (version === 18) database.exec(migration(version).statements.join("\n"));
+ else if (version === 19) {
+ for (const sql of migration(version).statements)
+ database.exec(
+ sql.replace("CREATE TABLE ", "CREATE TABLE IF NOT EXISTS "),
+ );
+ } else repairAdditive(database, version);
+ database
+ .prepare("UPDATE schema_migrations SET name = ? WHERE version = ?")
+ .run(migration(version).name, version);
+ }
+ if (lookup.get(2)?.name !== "finding management schema") return;
+ const expected = new Map([
+ [2, "finding management schema"],
+ [3, "scan handoff delivery claims"],
+ [4, "finding remediation action claims"],
+ [5, "scan target snapshot digests"],
+ ]);
+ const legacy = database
+ .prepare(
+ "SELECT version, name FROM schema_migrations WHERE version BETWEEN 2 AND 5",
+ )
+ .all();
+ if (legacy.some((row) => expected.get(Number(row.version)) !== row.name)) {
+ throw new Error(
+ "The Codex Security database has an unsupported pre-release migration history.",
+ );
+ }
+ database.exec("DELETE FROM schema_migrations WHERE version = 5");
+ for (const [from, to] of [
+ [4, 5],
+ [3, 4],
+ [2, 3],
+ ])
+ moveMigration(database, from, to, expected.get(from)!);
+ repairAdditive(database, 2);
+ addColumn(
+ database,
+ "ALTER TABLE scans ADD COLUMN target_snapshot_digest TEXT;",
+ );
+ database
+ .prepare(
+ "INSERT INTO schema_migrations (version, name, applied_at) VALUES (2, ?, ?)",
+ )
+ .run(migration(2).name, new Date().toISOString());
+}
+
+function repairDeepScan(database: DatabaseSync): void {
+ const scanColumns = columns(database, "scans");
+ const missingOwner = !scanColumns.has("deep_scan_owner_thread_id");
+ const existing = new Set(
+ database
+ .prepare(
+ "SELECT name FROM sqlite_master WHERE name LIKE 'deep_scan_%' OR name = 'scans_one_running_deep_per_owner_target'",
+ )
+ .all()
+ .map((row) => row.name),
+ );
+ const expected = [
+ "scans_one_running_deep_per_owner_target",
+ "deep_scan_runs",
+ "deep_scan_workers",
+ "deep_scan_workers_completion_sequence",
+ "deep_scan_workers_by_scan_status",
+ "deep_scan_dedup_inputs",
+ ];
+ if (!missingOwner && expected.every((name) => existing.has(name))) return;
+ addColumn(
+ database,
+ "ALTER TABLE scans ADD COLUMN deep_scan_owner_thread_id TEXT;",
+ );
+ for (const statement of migration(11).statements) {
+ if (
+ statement.startsWith("ALTER TABLE scans") ||
+ (statement.startsWith("UPDATE scans") && !missingOwner)
+ )
+ continue;
+ database.exec(
+ statement.replace(
+ /^CREATE (UNIQUE INDEX|INDEX|TABLE) /u,
+ "CREATE $1 IF NOT EXISTS ",
+ ),
+ );
+ if (
+ statement.startsWith("UPDATE scans") &&
+ scanColumns.has("continuation_thread_id")
+ ) {
+ database.exec(
+ "UPDATE scans SET deep_scan_owner_thread_id = continuation_thread_id WHERE mode = 'deep' AND status = 'running' AND continuation_thread_id IS NOT NULL",
+ );
+ }
+ }
+}
+
+function repairStableTargets(database: DatabaseSync): boolean {
+ const existing = database
+ .prepare(
+ "SELECT name FROM sqlite_master WHERE name IN ('security_targets', 'scans_by_target')",
+ )
+ .all();
+ if (
+ columns(database, "workspaces").has("target_id") &&
+ columns(database, "scans").has("target_id") &&
+ existing.length === 2
+ )
+ return false;
+ for (const statement of migration(16).statements) {
+ if (statement.startsWith("ALTER TABLE")) addColumn(database, statement);
+ else
+ database.exec(
+ statement.replace(
+ /^CREATE (TABLE|INDEX) /u,
+ "CREATE $1 IF NOT EXISTS ",
+ ),
+ );
+ }
+ database.exec(
+ "UPDATE scans SET target_id = NULL WHERE target_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM security_targets WHERE security_targets.id = scans.target_id)",
+ );
+ return true;
+}
+
+function backfillTargets(database: DatabaseSync): void {
+ const targets = database
+ .prepare(
+ "SELECT target_path FROM workspaces WHERE target_path IS NOT NULL UNION SELECT target_path FROM scans",
+ )
+ .all();
+ const lookup = database.prepare(
+ "SELECT id FROM security_targets WHERE current_path = ?",
+ );
+ const insert = database.prepare(
+ "INSERT OR IGNORE INTO security_targets (id, current_path, display_name, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
+ );
+ for (const row of targets) {
+ const path = String(row.target_path);
+ let id = lookup.get(path)?.id;
+ if (id === undefined) {
+ id =
+ "target_sha256_" +
+ createHash("sha256")
+ .update("local-workspace\0" + path)
+ .digest("hex");
+ const timestamp = new Date().toISOString();
+ insert.run(id, path, basename(path), timestamp, timestamp);
+ }
+ for (const table of ["workspaces", "scans"]) {
+ database
+ .prepare(
+ `UPDATE ${table} SET target_id = ? WHERE target_path = ? AND target_id IS NULL`,
+ )
+ .run(id, path);
+ }
+ }
+}
+
+function migrateWorkflowResults(database: DatabaseSync): void {
+ const update = database.prepare(
+ `UPDATE finding_workflows SET scan_error = ?, publish_error = ?, dedupe_error = ?,
+ results_json = ? WHERE rowid = ?`,
+ );
+ const rows = database.prepare(
+ "SELECT rowid, results_json FROM finding_workflows",
+ );
+ rows.setReadBigInts(true);
+ for (const row of rows.all()) {
+ const { stages } = parseJson(String(row.results_json)) as {
+ stages: Record<
+ string,
+ { error?: string; result?: unknown; pendingWrite?: unknown }
+ >;
+ };
+ const results: Record<string, unknown> = {};
+ for (const [stage, value] of Object.entries(stages)) {
+ if ("result" in value) results[stage] = value.result;
+ }
+ if ("pendingWrite" in stages.dedupe)
+ results.dedupePendingWrite = stages.dedupe.pendingWrite;
+ // Keep diagnostic strings intact on older SQLite runtimes.
+ update.run(
+ stages.scan.error ?? null,
+ stages.publish.error ?? null,
+ stages.dedupe.error ?? null,
+ stringifyJson(results),
+ row.rowid,
+ );
+ }
+}
+
+export function applyMigrations(
+ database: DatabaseSync,
+ selected: readonly Migration[] = migrations,
+ immediate = false,
+): void {
+ database.exec(immediate ? "BEGIN IMMEDIATE" : "BEGIN");
+ try {
+ database.exec(
+ "CREATE TABLE IF NOT EXISTS schema_migrations (version INTEGER PRIMARY KEY, name TEXT NOT NULL, applied_at TEXT NOT NULL)",
+ );
+ normalizeHistory(database);
+ const applied = new Set(
+ database
+ .prepare("SELECT version FROM schema_migrations")
+ .all()
+ .map((row) => Number(row.version)),
+ );
+ let backfill = false;
+ for (const item of selected) {
+ if (item.version === 6) {
+ addColumn(
+ database,
+ "ALTER TABLE workspaces ADD COLUMN thread_id TEXT;",
+ );
+ database.exec(
+ "CREATE INDEX IF NOT EXISTS workspaces_by_thread_and_updated_at ON workspaces(thread_id, updated_at DESC)",
+ );
+ } else if (item.version === 16) backfill = repairStableTargets(database);
+ else if (applied.has(item.version)) {
+ if ([2, 12, 13, 26, 28, 31, 32].includes(item.version))
+ repairAdditive(database, item.version);
+ else if (item.version === 11) repairDeepScan(database);
+ } else {
+ database.exec(item.statements.join("\n"));
+ if (item.version === 38) migrateWorkflowResults(database);
+ }
+ if (!applied.has(item.version)) {
+ database
+ .prepare(
+ "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",
+ )
+ .run(item.version, item.name, new Date().toISOString());
+ }
+ }
+ if (applied.has(27)) {
+ const [threshold, count] = migration(27).statements;
+ const missing = !columns(database, "deep_scan_runs").has(
+ "stop_after_consecutive_errors",
+ );
+ addColumn(database, threshold);
+ if (missing)
+ database.exec(
+ "UPDATE deep_scan_runs SET stop_after_consecutive_errors = stop_after_no_new",
+ );
+ addColumn(database, count);
+ }
+ if (backfill) backfillTargets(database);
+ database.exec("COMMIT");
+ } catch (error) {
+ try {
+ database.exec("ROLLBACK");
+ } catch {
+ // SQLite may have rolled back automatically after a storage failure.
+ }
+ throw error;
+ }
+}
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
index 64fd66877..1de08a563 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
@@ -1297,7 +1297,11 @@ async function testWorkerRuntimeSettings() {
);
process.env.PYTHON = python;
const gitEnvironment = {
- PATH: path.join(fixture.root, "selected tools"),
+ PATH: [
+ path.join(fixture.root, "selected tools"),
+ "../selected tools",
+ "",
+ ].join(path.delimiter),
CODEX_SECURITY_GIT: path.join(fixture.root, "selected tools", "git"),
GIT_SSH_COMMAND: "synthetic-ssh --fixture",
GIT_CONFIG_GLOBAL: path.join(fixture.root, "operator.gitconfig"),
diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_database.ts b/plugins/codex-security/mcp-app/tests/test_workbench_database.ts
new file mode 100644
index 000000000..6f7b77718
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/test_workbench_database.ts
@@ -0,0 +1,737 @@
+import assert from "node:assert/strict";
+import { createHash } from "node:crypto";
+import { once } from "node:events";
+import { mkdir, stat, symlink } from "node:fs/promises";
+import { join } from "node:path";
+import { DatabaseSync } from "node:sqlite";
+import { after, test, type TestContext } from "node:test";
+import { Worker } from "node:worker_threads";
+import { importSource } from "./import-module.ts";
+import { createTemporaryDirectories } from "./support/temporary-directories.ts";
+import type * as Database from "../src/workbench/database.ts";
+import type * as Migrations from "../src/workbench/migrations.ts";
+
+const { openWorkbenchDatabase, databaseInfo } = (await importSource(
+ "src/workbench/database.ts",
+ {
+ define: {
+ // Native bindings are installed beside the generated server bundle.
+ "import.meta.url": JSON.stringify(
+ new URL(
+ "../../../../sdk/typescript/_bundled_plugin/mcp/server.mjs",
+ import.meta.url,
+ ).href,
+ ),
+ },
+ },
+)) as typeof Database;
+const { applyMigrations, migrations } = (await importSource(
+ "src/workbench/migrations.ts",
+)) as typeof Migrations;
+const temporary = createTemporaryDirectories(true);
+after(() => temporary.cleanup());
+
+function memory(t: TestContext, version?: number) {
+ const database = new DatabaseSync(":memory:");
+ t.after(() => database.close());
+ database.exec("PRAGMA foreign_keys = ON");
+ if (version !== undefined)
+ applyMigrations(
+ database,
+ migrations.filter((item) => item.version <= version),
+ );
+ return database;
+}
+
+function schema(database: DatabaseSync) {
+ return database
+ .prepare("SELECT name, sql FROM sqlite_master ORDER BY name")
+ .all();
+}
+
+function assertMigrationNames(database: DatabaseSync, ...versions: number[]) {
+ for (const version of versions)
+ assert.equal(
+ database
+ .prepare("SELECT name FROM schema_migrations WHERE version = ?")
+ .get(version)?.name,
+ migrations[version - 1].name,
+ );
+}
+
+function insertScan(database: DatabaseSync, target = "/synthetic/repository") {
+ database
+ .prepare(
+ "INSERT INTO workspaces (id, target_path, created_at, updated_at) VALUES ('workspace', ?, 'created', 'updated')",
+ )
+ .run(target);
+ database
+ .prepare(
+ `INSERT INTO scans (id, workspace_id, target_path, target_revision,
+ scope, mode, scan_dir, status, phase, started_at, created_at, updated_at)
+ VALUES ('scan', 'workspace', ?, 'revision', '.', 'deep', '/synthetic/scan',
+ 'running', 'discovery', 'started', 'created', 'updated')`,
+ )
+ .run(target);
+}
+
+test("opens a private WAL database at the configured state path", async () => {
+ const directory = await temporary.create("workbench-database-");
+ const home = join(directory, "codex-home");
+ const { databasePath } = await databaseInfo(
+ join(home, "state/plugins/codex-security"),
+ );
+ assert.equal(
+ databasePath,
+ join(home, "state/plugins/codex-security/workbench.sqlite3"),
+ );
+ const database = await openWorkbenchDatabase(databasePath);
+ try {
+ assert.equal(
+ database.prepare("PRAGMA foreign_keys").get()?.foreign_keys,
+ 1,
+ );
+ assert.equal(database.prepare("PRAGMA busy_timeout").get()?.timeout, 5000);
+ assert.equal(
+ database.prepare("PRAGMA journal_mode").get()?.journal_mode,
+ "wal",
+ );
+ assert.equal(
+ database.prepare("SELECT COUNT(*) AS count FROM schema_migrations").get()
+ ?.count,
+ 42,
+ );
+ assert.deepEqual(database.prepare("PRAGMA foreign_key_check").all(), []);
+ if (process.platform !== "win32") {
+ assert.equal((await stat(databasePath)).mode & 0o777, 0o600);
+ for (const part of [
+ "",
+ "state",
+ "state/plugins",
+ "state/plugins/codex-security",
+ ])
+ assert.equal((await stat(join(home, part))).mode & 0o777, 0o700);
+ }
+ } finally {
+ database.close();
+ }
+ const override = join(directory, "existing-state");
+ await mkdir(override, { mode: 0o755 });
+ assert.equal(
+ (await databaseInfo(override)).databasePath,
+ join(override, "workbench.sqlite3"),
+ );
+ if (process.platform !== "win32")
+ assert.equal((await stat(override)).mode & 0o777, 0o755);
+});
+
+test("every released schema upgrades to the same current schema and remains idempotent", (t) => {
+ const current = memory(t);
+ applyMigrations(current);
+ const expected = schema(current);
+ for (let version = 0; version <= 42; version++) {
+ const database = memory(t, version);
+ applyMigrations(database);
+ assert.deepEqual(
+ schema(database),
+ expected,
+ `upgrade from version ${version}`,
+ );
+ const history = database
+ .prepare("SELECT * FROM schema_migrations ORDER BY version")
+ .all();
+ applyMigrations(database);
+ assert.deepEqual(
+ database
+ .prepare("SELECT * FROM schema_migrations ORDER BY version")
+ .all(),
+ history,
+ );
+ assert.deepEqual(database.prepare("PRAGMA foreign_key_check").all(), []);
+ }
+});
+
+test("configured state paths use native parent traversal semantics", async () => {
+ const directory = await temporary.create("workbench-symlink-");
+ const actual = join(directory, "actual");
+ const child = join(actual, "child");
+ await mkdir(child, { recursive: true });
+ const alias = join(directory, "alias");
+ await symlink(
+ child,
+ alias,
+ process.platform === "win32" ? "junction" : "dir",
+ );
+ const parent = process.platform === "win32" ? directory : actual;
+ assert.equal(
+ (await databaseInfo(alias + "/../state")).databasePath,
+ join(parent, "state", "workbench.sqlite3"),
+ );
+ assert.equal(
+ (await databaseInfo(alias + "/../state/plugins/codex-security"))
+ .databasePath,
+ join(parent, "state/plugins/codex-security/workbench.sqlite3"),
+ );
+});
+
+test("configured directory links create missing destinations privately", async () => {
+ const directory = await temporary.create("workbench-dangling-");
+ for (const nested of [false, true]) {
+ const destination = join(directory, `destination-${nested}`, "state");
+ const alias = join(directory, `alias-${nested}`);
+ await symlink(
+ destination,
+ alias,
+ process.platform === "win32" ? "junction" : "dir",
+ );
+ const result = await databaseInfo(nested ? join(alias, "nested") : alias);
+ assert.equal(
+ result.databasePath,
+ join(destination, ...(nested ? ["nested"] : []), "workbench.sqlite3"),
+ );
+ assert.equal((await stat(result.databasePath)).isFile(), true);
+ if (process.platform !== "win32")
+ assert.equal((await stat(destination)).mode & 0o777, 0o700);
+ }
+ if (process.platform !== "win32") {
+ const state = join(directory, "trailing-state");
+ const alias = join(directory, "trailing-alias");
+ const destination = join(directory, "trailing-destination");
+ await symlink("trailing-alias/", state);
+ await symlink("trailing-destination", alias);
+ const result = await databaseInfo(state);
+ assert.equal(result.databasePath, join(destination, "workbench.sqlite3"));
+ assert.equal((await stat(result.databasePath)).isFile(), true);
+ assert.equal((await stat(destination)).mode & 0o777, 0o700);
+ const cycle = join(directory, "cycle");
+ await symlink(cycle, cycle);
+ await assert.rejects(databaseInfo(cycle), { code: "ELOOP" });
+ }
+});
+
+test(
+ "an ASCII state alias opens its raw-byte POSIX target without changing a sibling database",
+ { skip: process.platform === "win32" },
+ async () => {
+ const directory = await temporary.create("workbench-raw-target-");
+ const target = Buffer.concat([
+ Buffer.from(directory + "/state-"),
+ Buffer.from([0xff]),
+ ]);
+ await mkdir(target);
+ const alias = join(directory, "alias");
+ await symlink(target, alias);
+ const sibling = join(directory, "state-\ufffd");
+ await mkdir(sibling);
+ const existing = new DatabaseSync(join(alias, "workbench.sqlite3"));
+ existing.exec(
+ "CREATE TABLE retained (value TEXT); INSERT INTO retained VALUES ('original')",
+ );
+ existing.close();
+ const result = await databaseInfo(alias);
+ assert.equal(
+ result.databasePath,
+ directory + "/state-\udcff/workbench.sqlite3",
+ );
+ const database = new DatabaseSync(join(alias, "workbench.sqlite3"));
+ try {
+ assert.equal(
+ database.prepare("SELECT value FROM retained").get()?.value,
+ "original",
+ );
+ assert.equal(
+ database
+ .prepare("SELECT MAX(version) AS version FROM schema_migrations")
+ .get()?.version,
+ 42,
+ );
+ } finally {
+ database.close();
+ }
+ await assert.rejects(stat(join(sibling, "workbench.sqlite3")), {
+ code: "ENOENT",
+ });
+ },
+);
+
+test("preview index history does not skip findings, embedding or checkpoint migrations", (t) => {
+ const database = memory(t);
+ applyMigrations(database, [
+ ...migrations.filter((item) => item.version <= 32),
+ { ...migrations.find((item) => item.version === 40)!, version: 33 },
+ ]);
+ database.exec(
+ "INSERT INTO findings (id, fingerprint, rule_id, identity_anchor, created_at, updated_at) VALUES ('finding', 'fingerprint', 'rule', 'anchor', 'created', 'updated')",
+ );
+ const indexes = database
+ .prepare(
+ "SELECT name, rootpage FROM sqlite_master WHERE name IN ('finding_occurrences_by_finding', 'scan_comparisons_by_after_scan') ORDER BY name",
+ )
+ .all();
+ applyMigrations(database);
+ assert.equal(
+ database
+ .prepare("SELECT details_json FROM findings WHERE id = 'finding'")
+ .get()?.details_json,
+ null,
+ );
+ assert.equal(
+ database.prepare("SELECT COUNT(*) AS count FROM finding_embeddings").get()
+ ?.count,
+ 0,
+ );
+ assert.equal(
+ database
+ .prepare("SELECT COUNT(*) AS count FROM finding_workflow_reviews")
+ .get()?.count,
+ 0,
+ );
+ assert.deepEqual(
+ database
+ .prepare(
+ "SELECT name, rootpage FROM sqlite_master WHERE name IN ('finding_occurrences_by_finding', 'scan_comparisons_by_after_scan') ORDER BY name",
+ )
+ .all(),
+ indexes,
+ );
+ assertMigrationNames(database, 33);
+});
+
+test("repairs mirror migration numbers without losing publication fields", (t) => {
+ const database = memory(t, 28);
+ for (const [version, from] of [
+ [29, 31],
+ [30, 32],
+ ]) {
+ const item = migrations.find((item) => item.version === from)!;
+ database.exec(item.statements.join("\n"));
+ database
+ .prepare("INSERT INTO schema_migrations VALUES (?, ?, 'original')")
+ .run(version, item.name);
+ }
+ applyMigrations(database);
+ assertMigrationNames(database, 29, 30, 31, 32);
+ assert.equal(
+ database
+ .prepare("SELECT applied_at FROM schema_migrations WHERE version = 31")
+ .get()?.applied_at,
+ "original",
+ );
+});
+
+test("legacy execution profiles retain values while allowing independent model settings", (t) => {
+ for (const version of [11, 12]) {
+ const database = memory(t);
+ applyMigrations(
+ database,
+ migrations.filter(
+ (item) => item.version < 25 && item.version !== version,
+ ),
+ );
+ for (const table of ["workspaces", "scans"]) {
+ database.exec(`ALTER TABLE ${table} ADD COLUMN execution_model TEXT;
+ ALTER TABLE ${table} ADD COLUMN reasoning_effort TEXT;`);
+ }
+ database
+ .prepare(
+ "INSERT INTO schema_migrations VALUES (?, 'scan execution profiles', 'original')",
+ )
+ .run(version);
+ insertScan(database);
+ database.exec(
+ "UPDATE scans SET execution_model = 'synthetic-model', reasoning_effort = 'future-effort'",
+ );
+ applyMigrations(database);
+ const row = database
+ .prepare(
+ "SELECT model, reasoning_effort, legacy_execution_model, legacy_reasoning_effort FROM scans",
+ )
+ .get()!;
+ assert.deepEqual(
+ { ...row },
+ {
+ model: "synthetic-model",
+ reasoning_effort: "future-effort",
+ legacy_execution_model: "synthetic-model",
+ legacy_reasoning_effort: "future-effort",
+ },
+ );
+ database.exec("UPDATE scans SET model = NULL, reasoning_effort = 'high'");
+ assertMigrationNames(database, version);
+ }
+});
+
+test("rejects unsupported history without partially changing its schema or rows", (t) => {
+ const database = memory(t, 24);
+ database.exec(
+ "UPDATE schema_migrations SET name = 'scan execution profiles' WHERE version = 11",
+ );
+ const before = schema(database);
+ const history = database
+ .prepare("SELECT * FROM schema_migrations ORDER BY version")
+ .all();
+ assert.throws(
+ () => applyMigrations(database),
+ /unsupported execution-profile migration history/u,
+ );
+ assert.deepEqual(schema(database), before);
+ assert.deepEqual(
+ database.prepare("SELECT * FROM schema_migrations ORDER BY version").all(),
+ history,
+ );
+});
+
+test("repairs pre-release numbering and shadowed migrations without repeating data cleanup", (t) => {
+ const database = memory(t);
+ applyMigrations(
+ database,
+ migrations.filter((item) => item.version <= 5 && item.version !== 2),
+ );
+ database.exec(
+ "UPDATE schema_migrations SET version = version - 1 WHERE version BETWEEN 3 AND 5",
+ );
+ database.exec(
+ "INSERT INTO schema_migrations VALUES (5, 'scan target snapshot digests', 'original')",
+ );
+ applyMigrations(database);
+ insertScan(database);
+ database.exec(`UPDATE scans SET handoff_status = 'delivered', handoff_claimed_at = 'old', handoff_claim_token = 'old';
+ UPDATE schema_migrations SET name = 'scan target summaries' WHERE version = 18;
+ UPDATE schema_migrations SET name = 'idempotent scan lifecycle requests' WHERE version = 19;
+ UPDATE schema_migrations SET name = 'threat model publication receipts' WHERE version = 20;
+ UPDATE schema_migrations SET name = 'deep coordinator manifest receipts' WHERE version = 21;
+ UPDATE schema_migrations SET name = 'dynamic scan execution profiles' WHERE version = 22;
+ DROP TABLE setup_preferences;
+ ALTER TABLE scan_progress DROP COLUMN phase_items_completed;
+ ALTER TABLE scan_progress DROP COLUMN phase_items_total;
+ ALTER TABLE scan_progress DROP COLUMN phase_progress_unit;
+ ALTER TABLE scan_progress DROP COLUMN preflight_issues_json;
+ ALTER TABLE scans DROP COLUMN recipe_json;`);
+ applyMigrations(database);
+ assert.equal(
+ database.prepare("SELECT handoff_claim_token FROM scans").get()
+ ?.handoff_claim_token,
+ null,
+ );
+ assert.equal(
+ database.prepare("SELECT COUNT(*) AS count FROM setup_preferences").get()
+ ?.count,
+ 0,
+ );
+ assertMigrationNames(database, 2, 3, 4, 5, 18, 19, 20, 21, 22);
+ database.exec("UPDATE scans SET handoff_claim_token = 'current'");
+ applyMigrations(database);
+ assert.equal(
+ database.prepare("SELECT handoff_claim_token FROM scans").get()
+ ?.handoff_claim_token,
+ "current",
+ );
+});
+
+test("relocates released warning and progress histories while retaining their data", (t) => {
+ const database = memory(t);
+ applyMigrations(database);
+ insertScan(database);
+ database.exec(`UPDATE scans SET completion_warnings_json = '["retained warning"]';
+ DELETE FROM schema_migrations WHERE version IN (12, 13, 25);
+ ALTER TABLE scans DROP COLUMN continuation_thread_id;
+ ALTER TABLE scan_progress DROP COLUMN scope_file_count;
+ ALTER TABLE scans DROP COLUMN model;
+ ALTER TABLE scans DROP COLUMN reasoning_effort;
+ UPDATE schema_migrations SET version = 12 WHERE version = 20;
+ UPDATE schema_migrations SET version = 13 WHERE version = 21;
+ UPDATE schema_migrations SET version = 25 WHERE version = 26;`);
+ applyMigrations(database);
+ assert.equal(
+ database.prepare("SELECT completion_warnings_json FROM scans").get()
+ ?.completion_warnings_json,
+ '["retained warning"]',
+ );
+ assertMigrationNames(database, 12, 13, 20, 21, 25, 26);
+});
+
+test("repairs recorded Deep Scan and target migrations and backfills stable identities once", (t) => {
+ const database = memory(t);
+ applyMigrations(
+ database,
+ migrations.filter((item) => item.version < 16 && item.version !== 11),
+ );
+ database.exec(
+ "ALTER TABLE scans ADD COLUMN target_id TEXT; INSERT INTO schema_migrations VALUES (11, 'stable scan target identities', 'original')",
+ );
+ insertScan(database);
+ database.exec(
+ "UPDATE workspaces SET thread_id = 'owner'; UPDATE scans SET continuation_thread_id = 'continuation', target_id = 'obsolete-id'",
+ );
+ applyMigrations(database);
+ const targetId =
+ "target_sha256_" +
+ createHash("sha256")
+ .update("local-workspace\0/synthetic/repository")
+ .digest("hex");
+ assert.equal(
+ database.prepare("SELECT target_id FROM workspaces").get()?.target_id,
+ targetId,
+ );
+ const scan = database
+ .prepare("SELECT target_id, deep_scan_owner_thread_id FROM scans")
+ .get()!;
+ assert.equal(scan.target_id, targetId);
+ assert.equal(scan.deep_scan_owner_thread_id, "continuation");
+ assert.deepEqual(database.prepare("PRAGMA foreign_key_check").all(), []);
+ database.exec(
+ "UPDATE workspaces SET target_id = NULL; UPDATE scans SET target_id = NULL",
+ );
+ applyMigrations(database);
+ assert.equal(
+ database.prepare("SELECT target_id FROM scans").get()?.target_id,
+ null,
+ );
+});
+
+test("recorded additive migrations restore missing columns and configured error thresholds", (t) => {
+ const database = memory(t, 26);
+ insertScan(database);
+ database.exec(`INSERT INTO deep_scan_runs (scan_id, schema_version, workflow_version, status, phase,
+ workers, subagents, stop_after_no_new, max_discovery_runs, created_at, updated_at)
+ VALUES ('scan', 1, 'v1', 'running', 'discovery', 1, 0, 7, 10, 'created', 'updated')`);
+ for (const version of [27, 28, 31, 32])
+ database
+ .prepare("INSERT INTO schema_migrations VALUES (?, ?, 'original')")
+ .run(version, migrations[version - 1].name);
+ applyMigrations(database);
+ assert.equal(
+ database
+ .prepare("SELECT stop_after_consecutive_errors FROM deep_scan_runs")
+ .get()?.stop_after_consecutive_errors,
+ 7,
+ );
+ database.exec("UPDATE deep_scan_runs SET stop_after_consecutive_errors = 2");
+ applyMigrations(database);
+ assert.equal(
+ database
+ .prepare("SELECT stop_after_consecutive_errors FROM deep_scan_runs")
+ .get()?.stop_after_consecutive_errors,
+ 2,
+ );
+});
+
+test("workflow and review checkpoints upgrade atomically with their preserved result data", (t) => {
+ const database = memory(t, 37);
+ const state = {
+ repositoryPath: "/synthetic/repository",
+ scanRequestDigest: "request",
+ scanId: "scan",
+ scanDir: "/synthetic/scan",
+ artifactDigest: "artifact",
+ destination: "local",
+ scope: { allRepositories: true },
+ stages: {
+ scan: { status: "complete", result: { findings: 2 } },
+ publish: { status: "failed", error: "diagnostic\0retained tail" },
+ dedupe: { status: "running", pendingWrite: { digest: "pending" } },
+ },
+ };
+ const binding = {
+ version: 1,
+ codexVersion: "1.0.0",
+ source: {
+ repository: "/synthetic/repository",
+ revision: "revision",
+ refsDigest: "refs",
+ content: "content",
+ },
+ scope: { repositoryId: "repository", allRepositories: false },
+ model: "synthetic-model",
+ effort: "high",
+ settingsDigest: "settings",
+ promptDigest: "prompt",
+ contractDigest: "contract",
+ };
+ const workflowIds = ["workflow", "workflow\0one", "workflow\0two"];
+ const reviewKeys = ["review", "review\0one", "review\0two"];
+ const states = workflowIds.map((_, index) =>
+ JSON.stringify({
+ ...state,
+ repositoryPath: `${state.repositoryPath}/${index}`,
+ stages: {
+ ...state.stages,
+ scan: { ...state.stages.scan, result: { findings: index } },
+ },
+ }).replace(
+ `"findings":${index}`,
+ `"findings":${index},"opaqueId":9007199254740993`,
+ ),
+ );
+ for (const [index, id] of workflowIds.entries()) {
+ database
+ .prepare(
+ "INSERT INTO finding_workflows (rowid, id, state_json, created_at, updated_at) VALUES (?, ?, ?, 'created', 'updated')",
+ )
+ .run(9007199254740993n + BigInt(index), id, states[index]);
+ for (const [keyIndex, key] of reviewKeys.entries()) {
+ database
+ .prepare(
+ "INSERT INTO finding_workflow_reviews (rowid, workflow_id, review_key, binding_json, result_json, created_at) VALUES (?, ?, ?, ?, ?, 'created')",
+ )
+ .run(
+ 9007199254740993n + BigInt(index * reviewKeys.length + keyIndex),
+ id,
+ key,
+ JSON.stringify({
+ ...binding,
+ promptDigest: `prompt-${index}-${keyIndex}`,
+ }),
+ JSON.stringify({ retained: `${index}-${keyIndex}` }),
+ );
+ }
+ }
+ const before = schema(database);
+ assert.throws(
+ () =>
+ applyMigrations(database, [
+ ...migrations,
+ {
+ version: 999,
+ name: "failing migration",
+ statements: ["SELECT * FROM missing_table;"],
+ },
+ ]),
+ /missing_table/u,
+ );
+ assert.deepEqual(schema(database), before);
+ for (const [index, id] of workflowIds.entries()) {
+ assert.equal(
+ database
+ .prepare("SELECT state_json FROM finding_workflows WHERE id = ?")
+ .get(id)?.state_json,
+ states[index],
+ );
+ }
+ applyMigrations(database);
+ for (const [index, id] of workflowIds.entries()) {
+ const workflow = database
+ .prepare(
+ "SELECT *, json_quote(publish_error) AS publish_error_json FROM finding_workflows WHERE id = ?",
+ )
+ .get(id)!;
+ assert.equal(workflow.repository_path, `${state.repositoryPath}/${index}`);
+ assert.equal(workflow.scope_all_repositories, 1);
+ assert.equal(
+ JSON.parse(String(workflow.publish_error_json)),
+ state.stages.publish.error,
+ );
+ const resultsJson = String(workflow.results_json);
+ assert.match(resultsJson, /"opaqueId":\s*9007199254740993/u);
+ assert.deepEqual(
+ JSON.parse(
+ resultsJson.replace(/,\s*"opaqueId":\s*9007199254740993/u, ""),
+ ),
+ {
+ scan: { findings: index },
+ dedupePendingWrite: { digest: "pending" },
+ },
+ );
+ for (const [keyIndex, key] of reviewKeys.entries()) {
+ const review = database
+ .prepare(
+ "SELECT * FROM finding_workflow_reviews WHERE workflow_id = ? AND review_key = ?",
+ )
+ .get(id, key)!;
+ assert.equal(review.settings_digest, "settings");
+ assert.equal(review.prompt_digest, `prompt-${index}-${keyIndex}`);
+ assert.equal(review.source_revision, "revision");
+ assert.equal(review.scope_all_repositories, 0);
+ assert.equal(
+ review.result_json,
+ JSON.stringify({ retained: `${index}-${keyIndex}` }),
+ );
+ }
+ }
+ assert.deepEqual(database.prepare("PRAGMA foreign_key_check").all(), []);
+});
+
+test("a storage failure preserves its error and the original migration state", (t) => {
+ const database = memory(t, 37);
+ const state = JSON.stringify({
+ stages: {
+ scan: { status: "completed", result: { data: Array(3000).fill("x") } },
+ publish: { status: "pending" },
+ dedupe: { status: "pending" },
+ },
+ });
+ database
+ .prepare(
+ "INSERT INTO finding_workflows (id, state_json, created_at, updated_at) VALUES ('workflow', ?, 'created', 'updated')",
+ )
+ .run(state);
+ const pages = Number(database.prepare("PRAGMA page_count").get()!.page_count);
+ database.exec(`PRAGMA max_page_count = ${pages + 5}`);
+ assert.throws(() => applyMigrations(database), { errcode: 13 });
+ assert.equal(
+ database
+ .prepare("SELECT MAX(version) AS version FROM schema_migrations")
+ .get()!.version,
+ 37,
+ );
+ assert.equal(
+ database
+ .prepare("SELECT state_json FROM finding_workflows WHERE id = 'workflow'")
+ .get()!.state_json,
+ state,
+ );
+});
+
+test("an up-to-date database-info reader does not wait for a writer", async () => {
+ const directory = await temporary.create("workbench-reader-");
+ const first = await databaseInfo(directory);
+ const writer = new DatabaseSync(first.databasePath);
+ try {
+ writer.exec("BEGIN IMMEDIATE; UPDATE workspaces SET updated_at = 'writer'");
+ assert.deepEqual(await databaseInfo(directory), first);
+ } finally {
+ writer.exec("ROLLBACK");
+ writer.close();
+ }
+});
+
+test("retries an upgrade when another process holds the write lock beyond the busy timeout", async () => {
+ const directory = await temporary.create("workbench-retry-");
+ const databasePath = join(directory, "workbench.sqlite3");
+ const old = new DatabaseSync(databasePath);
+ applyMigrations(
+ old,
+ migrations.filter((item) => item.version < 16),
+ );
+ insertScan(old);
+ old.close();
+ const writer = new Worker(
+ `const { DatabaseSync } = require('node:sqlite');
+ const { parentPort, workerData } = require('node:worker_threads');
+ const db = new DatabaseSync(workerData);
+ db.exec('BEGIN IMMEDIATE'); parentPort.postMessage('locked');
+ setTimeout(() => { db.exec('COMMIT'); db.close(); }, 6000);`,
+ { eval: true, workerData: databasePath },
+ );
+ try {
+ await once(writer, "message");
+ const database = await openWorkbenchDatabase(databasePath);
+ try {
+ assert.equal(
+ database
+ .prepare("SELECT COUNT(*) AS count FROM schema_migrations")
+ .get()?.count,
+ 42,
+ );
+ assert.equal(
+ database.prepare("SELECT COUNT(*) AS count FROM security_targets").get()
+ ?.count,
+ 1,
+ );
+ } finally {
+ database.close();
+ }
+ } finally {
+ await writer.terminate();
+ }
+});
diff --git a/plugins/codex-security/native/Cargo.toml b/plugins/codex-security/native/Cargo.toml
index 693a45214..c413d03ef 100644
--- a/plugins/codex-security/native/Cargo.toml
+++ b/plugins/codex-security/native/Cargo.toml
@@ -17,7 +17,7 @@ napi-derive = "=3.6.9"
libc = "=0.2.189"
[target.'cfg(windows)'.dependencies]
-windows-sys = { version = "=0.61.2", features = ["Win32_Foundation", "Win32_Security", "Win32_Storage_FileSystem"] }
+windows-sys = { version = "=0.61.2", features = ["Win32_Foundation", "Win32_Security", "Win32_Security_Authorization", "Win32_Storage_FileSystem", "Win32_System_Threading"] }
[build-dependencies]
napi-build = "=2.5.0"
diff --git a/plugins/codex-security/native/proof-windows-acl.mts b/plugins/codex-security/native/proof-windows-acl.mts
new file mode 100644
index 000000000..224996f32
--- /dev/null
+++ b/plugins/codex-security/native/proof-windows-acl.mts
@@ -0,0 +1,110 @@
+import assert from "node:assert/strict";
+import { execFileSync } from "node:child_process";
+import { readFileSync, writeFileSync } from "node:fs";
+import { join, win32 } from "node:path";
+import type { WindowsBinding } from "./windows-binding.mjs";
+
+// Inspect the actual Windows ACL, including inherited grants.
+const inspectAccess = String.raw`
+$ErrorActionPreference = 'Stop'
+$path = $env:CODEX_SECURITY_TEST_ACL_PATH
+$acl = if ([System.IO.Directory]::Exists($path)) {
+ [System.IO.Directory]::GetAccessControl($path)
+} else {
+ [System.IO.File]::GetAccessControl($path)
+}
+$descriptor = $acl.GetSecurityDescriptorBinaryForm()
+$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
+$rules = @($acl.GetAccessRules($true, $true, [System.Security.Principal.SecurityIdentifier]))
+$principals = @($rules | ForEach-Object { $_.IdentityReference.Value } | Select-Object -Unique)
+$privateRules = @($rules | Where-Object {
+ $_.AccessControlType -eq 'Allow' -and
+ $_.IdentityReference.Value -in @($identity, 'S-1-5-18', 'S-1-5-32-544') -and
+ ($_.FileSystemRights -band [System.Security.AccessControl.FileSystemRights]::FullControl) -eq [System.Security.AccessControl.FileSystemRights]::FullControl
+})
+$hash = [System.Security.Cryptography.SHA256]::Create()
+try { $digest = [Convert]::ToBase64String($hash.ComputeHash($descriptor)) } finally { $hash.Dispose() }
+[pscustomobject]@{
+ digest = $digest
+ protected = $acl.AreAccessRulesProtected
+ privateRules = ($rules.Count -eq 3 -and $privateRules.Count -eq 3 -and $principals.Count -eq 3)
+ inheritable = @($rules | Where-Object { $_.InheritanceFlags -eq ([System.Security.AccessControl.InheritanceFlags]::ObjectInherit -bor [System.Security.AccessControl.InheritanceFlags]::ContainerInherit) }).Count -eq 3
+ inherited = @($rules | Where-Object { $_.IsInherited }).Count -eq 3
+ everyone = @($rules | Where-Object { $_.IdentityReference.Value -eq 'S-1-1-0' -and $_.AccessControlType -eq 'Allow' -and ($_.FileSystemRights -band 1) }).Count -gt 0
+} | ConvertTo-Json -Compress
+`;
+
+export function privateDirectoryProof(root: string, native: WindowsBinding) {
+ const system = join(process.env["SystemRoot"] ?? "C:\\Windows", "System32");
+ function access(path: string) {
+ return JSON.parse(
+ execFileSync(
+ join(system, "WindowsPowerShell", "v1.0", "powershell.exe"),
+ [
+ "-NoLogo",
+ "-NoProfile",
+ "-NonInteractive",
+ "-EncodedCommand",
+ Buffer.from(inspectAccess, "utf16le").toString("base64"),
+ ],
+ {
+ encoding: "utf8",
+ windowsHide: true,
+ env: { ...process.env, CODEX_SECURITY_TEST_ACL_PATH: path },
+ },
+ ),
+ ) as {
+ digest: string;
+ protected: boolean;
+ privateRules: boolean;
+ inheritable: boolean;
+ inherited: boolean;
+ everyone: boolean;
+ };
+ }
+ const bytes = (path: string) =>
+ Buffer.from(win32.toNamespacedPath(path), "utf16le");
+ const parent = join(root, "broad-parent");
+ assert.equal(native.createWindowsDirectories(bytes(parent)), 0);
+ execFileSync(
+ join(system, "icacls.exe"),
+ [parent, "/grant", "*S-1-1-0:(OI)(CI)F"],
+ { windowsHide: true },
+ );
+ const before = access(parent);
+ assert(before.everyone, "Permissive parent must grant outsider read access");
+
+ const inherited = join(parent, "default-directory");
+ assert.equal(native.createWindowsDirectories(bytes(inherited)), 0);
+ assert(
+ access(inherited).everyone,
+ "Default directory creation must still inherit permissions",
+ );
+
+ const first = join(parent, "private-parent");
+ const state = join(first, "private-state");
+ for (const path of [first, state]) {
+ assert.equal(native.createPrivateWindowsDirectory(bytes(path)), 0);
+ const permissions = access(path);
+ assert(
+ permissions.protected &&
+ permissions.privateRules &&
+ permissions.inheritable,
+ "Every new directory must have only protected inheritable private grants",
+ );
+ }
+ const file = join(state, "synthetic-state");
+ writeFileSync(file, "synthetic private state\n");
+ assert.equal(readFileSync(file, "utf8"), "synthetic private state\n");
+ const permissions = access(file);
+ assert(
+ permissions.privateRules && permissions.inherited,
+ "Files must inherit only the private directory grants",
+ );
+ assert.equal(native.createPrivateWindowsDirectory(bytes(parent)), 183);
+ assert(
+ access(parent).digest === before.digest,
+ "Existing directory permissions must remain unchanged",
+ );
+ return true;
+}
diff --git a/plugins/codex-security/native/proof-windows.mts b/plugins/codex-security/native/proof-windows.mts
index 16d1d8705..81f312974 100644
--- a/plugins/codex-security/native/proof-windows.mts
+++ b/plugins/codex-security/native/proof-windows.mts
@@ -14,6 +14,7 @@ import {
import { tmpdir } from "node:os";
import { basename, join, win32 } from "node:path";
import { setImmediate } from "node:timers/promises";
+import { privateDirectoryProof } from "./proof-windows-acl.mjs";
import { wideProcessProof } from "./proof-windows-wide.mjs";
import { windowsFileSystem } from "./windows-files.mjs";
import {
@@ -388,6 +389,7 @@ try {
architecture: process.arch,
nodeApi: 8,
handles: handleProof(root),
+ privateDirectories: privateDirectoryProof(root, native),
wideProcessAndPaths: wideProcessProof(root),
garbageCollectionClosesHandle: await ownershipProof(root),
fixture: basename(root),
diff --git a/plugins/codex-security/native/src/windows.rs b/plugins/codex-security/native/src/windows.rs
index 521fa721f..9310d3658 100644
--- a/plugins/codex-security/native/src/windows.rs
+++ b/plugins/codex-security/native/src/windows.rs
@@ -1,20 +1,31 @@
use napi::bindgen_prelude::Buffer;
use napi_derive::napi;
use std::{
- ffi::OsString,
+ ffi::{CStr, OsString},
fs::{self, File},
io::{self, Read, Write},
mem::{offset_of, size_of, MaybeUninit},
os::windows::{
ffi::{OsStrExt, OsStringExt},
fs::FileTypeExt,
- io::{AsRawHandle, FromRawHandle},
+ io::{AsRawHandle, FromRawHandle, OwnedHandle},
},
ptr::{copy_nonoverlapping, null, null_mut},
};
use windows_sys::Win32::{
- Foundation::{GetLastError, SetLastError, ERROR_INVALID_HANDLE, HANDLE, INVALID_HANDLE_VALUE},
+ Foundation::{
+ GetLastError, LocalFree, SetLastError, ERROR_INVALID_HANDLE, HANDLE, INVALID_HANDLE_VALUE,
+ },
+ Security::{
+ Authorization::{
+ ConvertSidToStringSidA, ConvertStringSecurityDescriptorToSecurityDescriptorW,
+ SDDL_REVISION_1,
+ },
+ GetTokenInformation, TokenUser, SECURITY_ATTRIBUTES, SECURITY_MAX_SID_SIZE, TOKEN_QUERY,
+ TOKEN_USER,
+ },
Storage::FileSystem::*,
+ System::Threading::{GetCurrentProcess, OpenProcessToken},
};
fn invalid(message: &str) -> napi::Error {
@@ -275,6 +286,70 @@ pub fn create_windows_directories(path: Buffer) -> napi::Result<u32> {
Ok(io_status(fs::create_dir_all(os_string(path)?)))
}
+#[napi]
+pub fn create_private_windows_directory(path: Buffer) -> napi::Result<u32> {
+ let path = wide_path(path)?;
+ let mut token = null_mut();
+ let error = status(unsafe { OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) });
+ if error != 0 {
+ return Ok(error);
+ }
+ let token = unsafe { OwnedHandle::from_raw_handle(token) };
+ // TOKEN_USER is followed by the SID; use Windows' defined maximum SID size.
+ #[repr(C)]
+ struct TokenUserBuffer {
+ user: TOKEN_USER,
+ sid: [u8; SECURITY_MAX_SID_SIZE as usize],
+ }
+ let mut user = TokenUserBuffer {
+ user: TOKEN_USER::default(),
+ sid: [0; SECURITY_MAX_SID_SIZE as usize],
+ };
+ let mut length = 0;
+ let error = status(unsafe {
+ GetTokenInformation(
+ token.as_raw_handle(),
+ TokenUser,
+ (&mut user as *mut TokenUserBuffer).cast(),
+ size_of::<TokenUserBuffer>() as u32,
+ &mut length,
+ )
+ });
+ if error != 0 {
+ return Ok(error);
+ }
+ let mut sid = null_mut();
+ let error = status(unsafe { ConvertSidToStringSidA(user.user.User.Sid, &mut sid) });
+ if error != 0 {
+ return Ok(error);
+ }
+ let descriptor = format!(
+ "D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;FA;;;{})",
+ unsafe { CStr::from_ptr(sid.cast()) }.to_str().unwrap()
+ );
+ unsafe { LocalFree(sid.cast()) };
+ let mut attributes = SECURITY_ATTRIBUTES {
+ nLength: size_of::<SECURITY_ATTRIBUTES>() as u32,
+ ..Default::default()
+ };
+ // Match the credential-home policy: current user, SYSTEM and administrators.
+ let descriptor = descriptor.encode_utf16().chain(Some(0)).collect::<Vec<_>>();
+ let error = status(unsafe {
+ ConvertStringSecurityDescriptorToSecurityDescriptorW(
+ descriptor.as_ptr(),
+ SDDL_REVISION_1,
+ &mut attributes.lpSecurityDescriptor,
+ null_mut(),
+ )
+ });
+ if error != 0 {
+ return Ok(error);
+ }
+ let error = status(unsafe { CreateDirectoryW(path.as_ptr(), &attributes) });
+ unsafe { LocalFree(attributes.lpSecurityDescriptor) };
+ Ok(error)
+}
+
#[napi]
impl WindowsHandle {
#[napi]
diff --git a/plugins/codex-security/native/windows-binding.mts b/plugins/codex-security/native/windows-binding.mts
index 9a915f905..48df485ea 100644
--- a/plugins/codex-security/native/windows-binding.mts
+++ b/plugins/codex-security/native/windows-binding.mts
@@ -37,6 +37,7 @@ export interface WindowsBinding {
flags: number,
): { error: number; handle?: WindowsHandle | null };
createWindowsDirectories(path: Buffer): number;
+ createPrivateWindowsDirectory(path: Buffer): number;
}
export { windowsFlags } from "./windows-flags.mjs";
diff --git a/plugins/codex-security/native/windows-files.mts b/plugins/codex-security/native/windows-files.mts
index de57b7f89..0c62eae45 100644
--- a/plugins/codex-security/native/windows-files.mts
+++ b/plugins/codex-security/native/windows-files.mts
@@ -144,6 +144,10 @@ export function windowsFileSystem(native: WindowsBinding) {
check(native.createWindowsDirectories(operationPath(path)), path);
}
+ function mkdirPrivate(path: Buffer): void {
+ check(native.createPrivateWindowsDirectory(operationPath(path)), path);
+ }
+
function readInto(path: Buffer, buffer: Buffer): number {
return withFile(path, flags.GENERIC_READ, (handle) => {
let length = 0;
@@ -235,6 +239,7 @@ export function windowsFileSystem(native: WindowsBinding) {
identity,
entriesWithTypes,
mkdir,
+ mkdirPrivate,
readInto,
readFile,
writeFile,
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index dc379f911..18fc5b687 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -182,6 +182,8 @@ export interface WorkbenchCommandOptions {
python: string;
pluginRoot: string;
environment: ProcessEnvironment;
+ stateDirectory?: string;
+ protectedRoot?: string;
signal?: AbortSignal;
failureMessage?: string;
}
@@ -1553,7 +1555,7 @@ const workbenchComparisonSupport = new Map<
>();
export async function runWorkbench(
- options: WorkbenchCommandOptions,
+ options: Omit<WorkbenchCommandOptions, "python"> & { python?: string },
args: readonly string[],
input?: string,
): Promise<JsonObject> {
@@ -1562,11 +1564,38 @@ export async function runWorkbench(
arguments_: readonly string[],
input?: string,
): Promise<string> => {
+ const native = arguments_[0] === "database-info";
+ const node =
+ native && process.versions["bun"]
+ ? await resolveTrustedExecutable(
+ "node",
+ options.environment,
+ options.protectedRoot ?? process.cwd(),
+ )
+ : undefined;
+ if (node === null) {
+ throw new Error("Node.js is not available on a trusted PATH.");
+ }
+ const command = native
+ ? (node?.executable ?? process.execPath)
+ : (options.python ??= await resolvePluginPython({
+ environment: options.environment,
+ protectedRoot: options.protectedRoot,
+ signal: options.signal,
+ }));
const result = await runCodexCommand(
- { command: options.python },
- ["-I", "-X", "utf8", "-B", script, ...arguments_],
- pluginHelperEnvironment(options.environment),
- input,
+ { command },
+ native
+ ? [join(options.pluginRoot, "mcp", "helpers.mjs"), ...arguments_]
+ : ["-I", "-X", "utf8", "-B", script, ...arguments_],
+ pluginHelperEnvironment(node?.environment ?? options.environment),
+ // The SDK owns configuration normalization; the helper receives its resolved location.
+ native
+ ? JSON.stringify(
+ options.stateDirectory ??
+ codexSecurityStateDirectory(options.environment),
+ )
+ : input,
options.signal,
);
if (!result.success) {
@@ -1623,7 +1652,8 @@ export async function runWorkbench(
throw new CodexSecurityError(
databaseFailure
? `${failure}: cannot open the workbench database at ${join(
- codexSecurityStateDirectory(options.environment),
+ options.stateDirectory ??
+ codexSecurityStateDirectory(options.environment),
"workbench.sqlite3",
)}. Ensure the state directory and SQLite journal files are writable, or set CODEX_SECURITY_STATE_DIR to a writable directory outside the scanned repository.`
: `${failure}: ${detail}`,
@@ -3350,8 +3380,12 @@ function nullIfMissingFileError(error: unknown): null {
/** @internal */
export function workbenchEnvironment(environment: ProcessEnvironment) {
+ const python = environmentValue(environment, "PYTHON");
return {
...environment,
+ ...(python && isPythonPathCandidate(python)
+ ? { PYTHON: resolve(expandHome(python, environment)) }
+ : {}),
CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(environment),
};
}
diff --git a/sdk/typescript/src/server/sqlite-store.ts b/sdk/typescript/src/server/sqlite-store.ts
index c05052dab..84818a170 100644
--- a/sdk/typescript/src/server/sqlite-store.ts
+++ b/sdk/typescript/src/server/sqlite-store.ts
@@ -1,6 +1,7 @@
+import { delimiter, isAbsolute, resolve } from "node:path";
import {
+ bundledPluginRoot,
workbenchEnvironment,
- resolveWorkbenchRuntime,
runWorkbench,
type WorkbenchCommandOptions,
} from "../runtime.js";
@@ -18,7 +19,7 @@ import type {
} from "./storage.js";
export class SqliteFindingsStore implements FindingsStore {
- private options?: Promise<WorkbenchCommandOptions>;
+ private options?: Promise<Omit<WorkbenchCommandOptions, "python">>;
constructor(private readonly environment: NodeJS.ProcessEnv = process.env) {}
@@ -113,13 +114,39 @@ export class SqliteFindingsStore implements FindingsStore {
return await runWorkbench(options, args, input);
}
- private async resolveOptions(): Promise<WorkbenchCommandOptions> {
- const environment = workbenchEnvironment(this.environment);
- const [python, pluginRoot] = await resolveWorkbenchRuntime({ environment });
+ private async resolveOptions(): Promise<
+ Omit<WorkbenchCommandOptions, "python">
+ > {
+ const protectedRoot = process.cwd();
+ const environment: NodeJS.ProcessEnv = workbenchEnvironment(
+ this.environment,
+ );
+ for (const [name, value] of Object.entries(environment)) {
+ if (value === undefined) continue;
+ if (name.toUpperCase() === "XDG_CACHE_HOME" && value.trim())
+ environment[name] = resolve(value);
+ if (name.toUpperCase() !== "PATH") continue;
+ environment[name] = value
+ .split(delimiter)
+ .map((entry) => {
+ const directory =
+ process.platform === "win32"
+ ? entry.replace(/^"(.*)"$/u, "$1")
+ : entry;
+ if (!directory) return directory;
+ if (process.platform === "win32") return resolve(directory);
+ // Keep POSIX symlink/.. traversal intact.
+ return isAbsolute(directory)
+ ? directory
+ : `${protectedRoot}/${directory}`;
+ })
+ .join(delimiter);
+ }
return {
- python,
- pluginRoot,
+ protectedRoot,
+ pluginRoot: await bundledPluginRoot(),
environment,
+ stateDirectory: environment["CODEX_SECURITY_STATE_DIR"],
failureMessage: "Could not access the findings database",
};
}
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index 4b8fc0028..f117a9a28 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -56,11 +56,7 @@ import {
type JsonObject,
} from "../src/config.js";
import { estimateScanCost, type ScanCost } from "../src/cost.js";
-import {
- resolveCodexCommand,
- runWorkbench,
- type WorkbenchCommandOptions,
-} from "../src/runtime.js";
+import { resolveCodexCommand, runWorkbench } from "../src/runtime.js";
import * as runtime from "../src/runtime.js";
import { matchScanFindingsInternal } from "../src/scan-comparison.js";
import { normalizeTarget } from "../src/targets.js";
@@ -2386,7 +2382,7 @@ describe("CodexSecurity orchestration", () => {
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
runWorkbench: async (
- workbenchOptions: WorkbenchCommandOptions,
+ workbenchOptions: Parameters<typeof runWorkbench>[0],
args: readonly string[],
input?: string,
): Promise<JsonObject> => {
@@ -4875,7 +4871,7 @@ describe("CodexSecurity orchestration", () => {
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
runWorkbench: async (
- workbenchOptions: WorkbenchCommandOptions,
+ workbenchOptions: Parameters<typeof runWorkbench>[0],
args: readonly string[],
input?: string,
): Promise<JsonObject> => {
diff --git a/sdk/typescript/tests-ts/build-plugin.test.ts b/sdk/typescript/tests-ts/build-plugin.test.ts
index 149269de3..859de266c 100644
--- a/sdk/typescript/tests-ts/build-plugin.test.ts
+++ b/sdk/typescript/tests-ts/build-plugin.test.ts
@@ -95,9 +95,11 @@ describe("bundled plugin build", () => {
filter: (path) =>
!["node_modules", ".preview"].includes(basename(path)),
});
- await cp(join(plugin, "schemas"), join(source, "schemas"), {
- recursive: true,
- });
+ for (const directory of ["schemas", "shared"]) {
+ await cp(join(plugin, directory), join(source, directory), {
+ recursive: true,
+ });
+ }
for (const file of [
"reserved_artifact_paths.json",
"codex_profile.mjs",
diff --git a/sdk/typescript/tests-ts/findings-server.test.ts b/sdk/typescript/tests-ts/findings-server.test.ts
index be0731349..92a06e622 100644
--- a/sdk/typescript/tests-ts/findings-server.test.ts
+++ b/sdk/typescript/tests-ts/findings-server.test.ts
@@ -1,11 +1,26 @@
-import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
+import {
+ mkdir,
+ mkdtemp,
+ readFile,
+ readdir,
+ realpath,
+ rm,
+ symlink,
+ writeFile,
+} from "node:fs/promises";
import type { Server } from "node:http";
import { tmpdir } from "node:os";
-import { join } from "node:path";
+import { basename, delimiter, dirname, join, relative } from "node:path";
+import { fileURLToPath, pathToFileURL } from "node:url";
import { afterEach, expect, spyOn, test, mock } from "bun:test";
+import { build } from "esbuild";
import type { Finding, FindingsDocument } from "../src/models.js";
import type { FindingDedupeGroup } from "../src/finding-dedupe-groups.js";
-import { resolvePluginPython, runCodexCommand } from "../src/runtime.js";
+import {
+ resolvePluginPython,
+ runCodexCommand,
+ runWorkbench,
+} from "../src/runtime.js";
import type { FindingEmbedder } from "../src/server/embeddings.js";
import { FindingsError } from "../src/server/errors.js";
import { startFindingsServer } from "../src/server/server.js";
@@ -79,6 +94,388 @@ async function fixture() {
return { environment, store: new SqliteFindingsStore(environment) };
}
+test("initializes the shared database concurrently without Python", async () => {
+ const { environment } = await fixture();
+ const nativeEnvironment = {
+ ...environment,
+ PYTHON: join(environment.CODEX_SECURITY_STATE_DIR, "missing-python"),
+ };
+ await Promise.all([
+ new SqliteFindingsStore(nativeEnvironment).initialize(),
+ new SqliteFindingsStore(nativeEnvironment).initialize(),
+ ]);
+ const result = await runWorkbench(
+ { pluginRoot: PLUGIN_ROOT, environment: nativeEnvironment },
+ ["database-info"],
+ );
+ expect(result).toEqual({
+ databasePath: join(
+ await realpath(environment.CODEX_SECURITY_STATE_DIR),
+ "workbench.sqlite3",
+ ),
+ });
+});
+
+test("missing explicit Python fails only when a Python operation runs", async () => {
+ const { environment } = await fixture();
+ const store = new SqliteFindingsStore({
+ ...environment,
+ PYTHON: join(environment.CODEX_SECURITY_STATE_DIR, "missing-python"),
+ });
+ await store.initialize();
+ await expect(store.list({ limit: 50, offset: 0 })).rejects.toThrow(
+ "The PYTHON interpreter is unavailable or unusable",
+ );
+});
+
+test.skipIf(process.platform === "win32")(
+ "database initialization under Bun ignores repository-local Node shims",
+ async () => {
+ const directory = await mkdtemp(join(tmpdir(), "database-info-node-"));
+ directories.push(directory);
+ const repository = join(directory, "repository");
+ const other = join(directory, "other", "nested");
+ await mkdir(other, { recursive: true });
+ const bin = join(repository, "node_modules", ".bin");
+ await mkdir(bin, { recursive: true });
+ await writeFile(
+ join(bin, "node"),
+ `#!/bin/sh
+: > "$SYNTHETIC_NODE_SHIM_MARKER"
+printf '%s\n' '{"databasePath":"shim"}'
+`,
+ { mode: 0o755 },
+ );
+ const node = Bun.which("node");
+ expect(node).not.toBeNull();
+ const tools = join(directory, "tools");
+ const replacementTools = join(directory, "other", "tools");
+ await Promise.all([mkdir(tools), mkdir(replacementTools)]);
+ await symlink(node!, join(tools, "node"));
+ await writeFile(
+ join(replacementTools, "node"),
+ '#!/bin/sh\nprintf invoked > "$SYNTHETIC_NODE_SHIM_MARKER"\nexit 1\n',
+ { mode: 0o755 },
+ );
+ const state = join(directory, "state");
+ const result = Bun.spawnSync(
+ [
+ process.execPath,
+ "--eval",
+ `const { SqliteFindingsStore } = await import(${JSON.stringify(new URL("../src/server/sqlite-store.ts", import.meta.url).href)});
+const store = new SqliteFindingsStore();
+await store.initialize();
+process.chdir(${JSON.stringify(other)});
+await store.initialize();`,
+ ],
+ {
+ cwd: repository,
+ env: {
+ ...process.env,
+ PATH: [bin, "../tools"].join(delimiter),
+ CODEX_SECURITY_STATE_DIR: state,
+ SYNTHETIC_NODE_SHIM_MARKER: join(bin, "invoked"),
+ },
+ },
+ );
+ expect(result.exitCode).toBe(0);
+ expect(await readdir(bin)).toEqual(["node"]);
+ expect(
+ (await readFile(join(state, "workbench.sqlite3")))
+ .subarray(0, 16)
+ .toString(),
+ ).toBe("SQLite format 3\0");
+ },
+);
+
+test.skipIf(process.platform === "win32")(
+ "database initialization and Python operations share the configured state directory",
+ async () => {
+ const directory = await mkdtemp(join(tmpdir(), "database-info-location-"));
+ directories.push(directory);
+ const store = new SqliteFindingsStore({
+ ...process.env,
+ CODEX_SECURITY_STATE_DIR: join(directory, "state ") + "/",
+ });
+ await store.insert([embedded(1)], "repository-a");
+ await store.initialize();
+ expect(await readdir(directory)).toEqual(["state "]);
+ expect((await store.list({ limit: 50, offset: 0 })).findings).toEqual([
+ finding(1),
+ ]);
+ },
+);
+
+test("initialized stores keep relative state in the original directory", async () => {
+ const directory = await mkdtemp(join(tmpdir(), "findings-relative-state-"));
+ directories.push(directory);
+ const first = join(directory, "first");
+ const second = join(directory, "second");
+ await Promise.all([mkdir(first), mkdir(second)]);
+ const result = Bun.spawnSync(
+ [
+ process.execPath,
+ "--eval",
+ `const { SqliteFindingsStore } = await import(${JSON.stringify(new URL("../src/server/sqlite-store.ts", import.meta.url).href)});
+const store = new SqliteFindingsStore();
+await store.initialize();
+process.chdir(${JSON.stringify(second)});
+await store.insert(${JSON.stringify([embedded(1)])});
+console.log(JSON.stringify(await store.list({ limit: 50, offset: 0 })));`,
+ ],
+ {
+ cwd: first,
+ env: {
+ ...process.env,
+ CODEX_SECURITY_STATE_DIR: "state",
+ },
+ },
+ );
+ expect(result.exitCode, result.stderr.toString()).toBe(0);
+ expect(JSON.parse(result.stdout.toString()).findings).toEqual([finding(1)]);
+ expect(await readdir(first)).toEqual(["state"]);
+ expect(await readdir(second)).toEqual([]);
+});
+
+test("initialized stores retain their environment across findings operations", async () => {
+ const { environment } = await fixture();
+ const store = new SqliteFindingsStore(environment);
+ await store.initialize();
+ const directory = dirname(environment.CODEX_SECURITY_STATE_DIR);
+ environment.CODEX_SECURITY_STATE_DIR = join(directory, "other state");
+ const entries = [embedded(1), embedded(2)];
+ await store.insert(entries);
+ const groups = await store.storeDedupeGroups([
+ entries.map(({ finding }) => finding.findingId),
+ ]);
+ expect(await store.listDedupeGroups(entries[0]!.finding.findingId)).toEqual(
+ groups,
+ );
+ expect((await store.list({ limit: 50, offset: 0 })).findings).toEqual(
+ entries.map(({ finding }) => finding),
+ );
+ expect(await readdir(directory)).toEqual(["state with spaces"]);
+});
+
+test.each(["explicit", "PATH", "Path", "XDG_CACHE_HOME", "xdg_cache_home"])(
+ "findings stores retain relative Python paths before and after first use (%s)",
+ async (configuration) => {
+ const directory = await mkdtemp(join(tmpdir(), "findings-python-"));
+ directories.push(directory);
+ const first = join(directory, "first");
+ const second = join(directory, "second", "nested");
+ await Promise.all([mkdir(first), mkdir(second, { recursive: true })]);
+ const python = await resolvePluginPython();
+ const managedCache = configuration.toUpperCase() === "XDG_CACHE_HOME";
+ const cacheDirectory = join(directory, "managed cache");
+ if (managedCache) {
+ const managedPython = join(
+ cacheDirectory,
+ "codex-runtimes/codex-primary-runtime/dependencies/python",
+ );
+ await mkdir(dirname(managedPython), { recursive: true });
+ if (process.platform === "win32") {
+ await symlink(dirname(python), managedPython, "junction");
+ } else {
+ await mkdir(join(managedPython, "bin"), { recursive: true });
+ await symlink(python, join(managedPython, "bin", "python3"));
+ }
+ }
+ let pythonDirectory = relative(first, dirname(python));
+ if (
+ process.platform !== "win32" &&
+ ["PATH", "Path"].includes(configuration)
+ ) {
+ const link = join(directory, "python-link");
+ await symlink(dirname(python), link, "dir");
+ pythonDirectory = `${configuration === "PATH" ? link : relative(first, link)}/../${basename(dirname(python))}`;
+ }
+ const searchPath = [
+ process.platform === "win32" ? `"${pythonDirectory}"` : pythonDirectory,
+ "",
+ dirname(Bun.which("node")!),
+ ].join(delimiter);
+ const storeModule = join(directory, "sqlite-store.cjs");
+ await build({
+ entryPoints: [
+ fileURLToPath(
+ new URL("../src/server/sqlite-store.ts", import.meta.url),
+ ),
+ ],
+ outfile: storeModule,
+ bundle: true,
+ platform: "node",
+ format: "cjs",
+ define: {
+ "import.meta.url": JSON.stringify(
+ new URL("../src/runtime.ts", import.meta.url).href,
+ ),
+ },
+ });
+ const result = Bun.spawnSync(
+ [
+ Bun.which("node")!,
+ "--input-type=module",
+ "--eval",
+ `const assert = await import("node:assert/strict");
+const { SqliteFindingsStore } = await import(${JSON.stringify(pathToFileURL(storeModule).href)});
+const environment = { ...process.env };
+if (${managedCache}) {
+ for (const key of Object.keys(environment)) if (["PATH", "PYTHON", "XDG_CACHE_HOME"].includes(key.toUpperCase())) delete environment[key];
+ environment.PATH = "";
+ environment[${JSON.stringify(configuration)}] = ${JSON.stringify(relative(first, cacheDirectory))};
+} else if (${JSON.stringify(configuration)} !== "explicit") {
+ for (const key of Object.keys(environment)) if (key.toUpperCase() === "PATH") delete environment[key];
+ environment[${JSON.stringify(configuration)}] = ${JSON.stringify(searchPath)};
+}
+const original = ["PATH", "Path", "PYTHON", "XDG_CACHE_HOME", "xdg_cache_home", "CODEX_SECURITY_STATE_DIR"].map((key) => [key, environment[key], process.env[key]]);
+const store = new SqliteFindingsStore(environment);
+await store.initialize();
+process.chdir(${JSON.stringify(second)});
+await store.insert(${JSON.stringify([embedded(1)])});
+process.chdir(${JSON.stringify(first)});
+console.log(JSON.stringify(await store.list({ limit: 50, offset: 0 })));
+for (const [key, supplied, inherited] of original) {
+ assert.equal(environment[key], supplied);
+ assert.equal(process.env[key], inherited);
+}`,
+ ],
+ {
+ cwd: first,
+ env: {
+ ...process.env,
+ PYTHON:
+ configuration === "explicit"
+ ? relative(first, python)
+ : basename(python),
+ CODEX_SECURITY_STATE_DIR: join(directory, "state"),
+ },
+ },
+ );
+ expect(result.exitCode, result.stderr.toString()).toBe(0);
+ expect(JSON.parse(result.stdout.toString()).findings).toEqual([finding(1)]);
+ },
+);
+
+test.skipIf(process.platform === "win32")(
+ "changing directories does not trust the original repository's Python shim",
+ async () => {
+ const directory = await mkdtemp(join(tmpdir(), "findings-python-shim-"));
+ directories.push(directory);
+ const repository = join(directory, "repository");
+ const other = join(directory, "other");
+ await Promise.all([mkdir(repository), mkdir(other)]);
+ await writeFile(
+ join(repository, "python"),
+ '#!/bin/sh\nprintf invoked > "$SYNTHETIC_PYTHON_MARKER"\nprintf "codex-security-python-ok\\n"\n',
+ { mode: 0o755 },
+ );
+ const result = Bun.spawnSync(
+ [
+ process.execPath,
+ "--eval",
+ `const assert = await import("node:assert/strict");
+const { SqliteFindingsStore } = await import(${JSON.stringify(new URL("../src/server/sqlite-store.ts", import.meta.url).href)});
+const store = new SqliteFindingsStore();
+await store.initialize();
+process.chdir(${JSON.stringify(other)});
+await assert.rejects(store.list({ limit: 50, offset: 0 }), /The PYTHON interpreter is unavailable or unusable/);`,
+ ],
+ {
+ cwd: repository,
+ env: {
+ ...process.env,
+ PYTHON: "./python",
+ CODEX_SECURITY_STATE_DIR: join(directory, "state"),
+ SYNTHETIC_PYTHON_MARKER: join(repository, "invoked"),
+ },
+ },
+ );
+ expect(result.exitCode, result.stderr.toString()).toBe(0);
+ expect(await readdir(repository)).toEqual(["python"]);
+ },
+);
+
+test("escapes terminal controls in database helper diagnostics", async () => {
+ const directory = await mkdtemp(join(tmpdir(), "database-info-"));
+ directories.push(directory);
+ const blocked = join(directory, "blocked\u202e");
+ await writeFile(blocked, "existing file");
+ const result = await runCodexCommand(
+ { command: "node" },
+ [join(PLUGIN_ROOT, "mcp", "helpers.mjs"), "database-info"],
+ process.env,
+ JSON.stringify(join(blocked, "state")),
+ );
+ expect(result.success).toBe(false);
+ expect(result.stderr).toContain("\\u202e");
+ expect(result.stderr).not.toContain("\u202e");
+});
+
+test("successful database helper JSON escapes terminal controls without changing the path", async () => {
+ const directory = await mkdtemp(join(tmpdir(), "database-info-controls-"));
+ directories.push(directory);
+ const state = join(directory, "state\u009b\u202e\u{e0001}");
+ const result = await runCodexCommand(
+ { command: "node" },
+ [join(PLUGIN_ROOT, "mcp", "helpers.mjs"), "database-info"],
+ process.env,
+ JSON.stringify(state),
+ );
+ expect(result.success).toBe(true);
+ expect(JSON.parse(result.stdout)).toEqual({
+ databasePath: join(await realpath(state), "workbench.sqlite3"),
+ });
+ expect(result.stdout.trimEnd()).not.toMatch(/[\p{Cc}\p{Cf}]/u);
+ expect(result.stdout).toContain("\\udb40\\udc01");
+});
+
+test("database initialization uses the SDK's existing CODEX_HOME configuration", async () => {
+ const directory = await mkdtemp(join(tmpdir(), "database-info-home-"));
+ directories.push(directory);
+ const result = await runWorkbench(
+ {
+ pluginRoot: PLUGIN_ROOT,
+ environment: {
+ ...process.env,
+ CODEX_SECURITY_STATE_DIR: undefined,
+ CODEX_HOME: directory,
+ PYTHON: join(directory, "missing-python"),
+ },
+ },
+ ["database-info"],
+ );
+ expect(result).toEqual({
+ databasePath: join(
+ await realpath(directory),
+ "state/plugins/codex-security/workbench.sqlite3",
+ ),
+ });
+});
+
+test("database helper rejects invalid state-directory input before writing", async () => {
+ const directory = await mkdtemp(join(tmpdir(), "database-info-input-"));
+ directories.push(directory);
+ const ignoredEnvironmentPath = join(directory, "must-not-be-created");
+ for (const input of [
+ undefined,
+ JSON.stringify(null),
+ JSON.stringify("~synthetic-user"),
+ JSON.stringify("C:"),
+ JSON.stringify(directory + "/raw\udcff"),
+ ]) {
+ const result = await runCodexCommand(
+ { command: "node" },
+ [join(PLUGIN_ROOT, "mcp", "helpers.mjs"), "database-info"],
+ { ...process.env, CODEX_SECURITY_STATE_DIR: ignoredEnvironmentPath },
+ input,
+ );
+ expect(result.success).toBe(false);
+ expect(result.stderr).not.toBe("");
+ }
+ expect(await readdir(directory)).toEqual([]);
+});
+
async function start(
store: SqliteFindingsStore,
embeddings = embedder,
diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.ts
index 282dd9e12..2c26c617a 100644
--- a/sdk/typescript/tests-ts/runtime.test.ts
+++ b/sdk/typescript/tests-ts/runtime.test.ts
@@ -4561,9 +4561,52 @@ describe("runtime directories and plugin Python boundary", () => {
);
test.skipIf(process.platform !== "win32")(
- "creates credential homes with a verified managed-compatible Windows ACL",
+ "initializes the workbench in an explicitly configured Windows drive root",
async () => {
- const { home } = await credentialHome(true);
+ const directory = await temporaryDirectory();
+ const drive = [..."ZYXWVUTSRQPONMLKJIHGFED"].find(
+ (letter) => !existsSync(`${letter}:\\`),
+ );
+ expect(drive).toBeDefined();
+ const volume = `${drive}:`;
+ const subst = join(
+ process.env["SystemRoot"] ?? "C:\\Windows",
+ "System32",
+ "subst.exe",
+ );
+ expect(spawnSync(subst, [volume, directory]).status).toBe(0);
+ try {
+ await runWorkbench(
+ {
+ pluginRoot: PLUGIN_ROOT,
+ environment: {
+ ...process.env,
+ CODEX_SECURITY_STATE_DIR: `${volume}\\`,
+ },
+ },
+ ["database-info"],
+ );
+ expect(
+ (await stat(join(directory, "workbench.sqlite3"))).isFile(),
+ ).toBe(true);
+ } finally {
+ expect(spawnSync(subst, [volume, "/d"]).status).toBe(0);
+ }
+ },
+ );
+
+ test.skipIf(process.platform !== "win32")(
+ "creates credential homes after database initialization with a verified Windows ACL",
+ async () => {
+ const root = await temporaryDirectory();
+ const environment = {
+ ...process.env,
+ CODEX_SECURITY_STATE_DIR: join(root, "state"),
+ };
+ await runWorkbench({ pluginRoot: PLUGIN_ROOT, environment }, [
+ "database-info",
+ ]);
+ const home = await prepareCodexSecurityCredentialHome(environment);
const powershell = join(
process.env["SystemRoot"] ?? "C:\\Windows",
"System32",