Skip to content

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

fix(workbench): retain shared history across worktree changes (#456)

Michael D'Angeloea617a4c0941
diff --git a/README.md b/README.md
index 9b65d504e..c30adfcdb 100644
--- a/README.md
+++ b/README.md
@@ -127,6 +127,10 @@ try {
The [SDK guide](sdk/typescript/docs/sdk.md) includes deep-scan configuration,
validation, severity classification, owner suggestions, and result handling.
+Linked Git worktrees share scan history and saved findings when they use the same
+Codex Security state directory. Keep `CODEX_SECURITY_STATE_DIR` stable to retain
+that shared history and sign-in scope. See the [scan history guide](sdk/typescript/docs/cli.md#scan-history-and-reruns).
+
## Read existing Cloud findings
When Codex Security Cloud is available to your account, the Security plugin's
diff --git a/plugins/codex-security/mcp-app/src/workbench/migrations.ts b/plugins/codex-security/mcp-app/src/workbench/migrations.ts
index 721021b53..6235e7c0a 100644
--- a/plugins/codex-security/mcp-app/src/workbench/migrations.ts
+++ b/plugins/codex-security/mcp-app/src/workbench/migrations.ts
@@ -14,7 +14,10 @@ export interface Migration {
statements: readonly string[];
}
-export const migrations: readonly Migration[] = history;
+// Repository-dependent migrations run in Python before semantic workbench operations.
+export const migrations: readonly Migration[] = history.filter(
+ (item) => item.version <= 66,
+);
const migration = (version: number) =>
migrations.find((item) => item.version === version)!;
@@ -130,9 +133,16 @@ export function stableTargetId(path: string): string {
}
function backfillTargets(database: DatabaseSync): void {
+ const validatedWorkspace = columns(database, "workspaces").has(
+ "target_path_unvalidated",
+ )
+ ? " AND target_path_unvalidated = 0"
+ : "";
const targets = database
.prepare(
- "SELECT target_path FROM workspaces WHERE target_path IS NOT NULL UNION SELECT target_path FROM scans",
+ "SELECT target_path FROM workspaces WHERE target_path IS NOT NULL" +
+ validatedWorkspace +
+ " UNION SELECT target_path FROM scans",
)
.all();
const lookup = database.prepare(
@@ -152,7 +162,8 @@ function backfillTargets(database: DatabaseSync): void {
for (const table of ["workspaces", "scans"]) {
database
.prepare(
- `UPDATE ${table} SET target_id = ? WHERE target_path = ? AND target_id IS NULL`,
+ `UPDATE ${table} SET target_id = ? WHERE target_path = ? AND target_id IS NULL` +
+ (table === "workspaces" ? validatedWorkspace : ""),
)
.run(id, path);
}
diff --git a/plugins/codex-security/mcp-app/tests/fixtures/pre-severity-repository-migrations.json b/plugins/codex-security/mcp-app/tests/fixtures/pre-severity-repository-migrations.json
new file mode 100644
index 000000000..dcbdfa9a2
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/fixtures/pre-severity-repository-migrations.json
@@ -0,0 +1,22 @@
+[
+ {
+ "version": 43,
+ "name": "persist repository identities",
+ "statements": [
+ "ALTER TABLE security_targets\n ADD COLUMN repository_identity TEXT;",
+ "CREATE INDEX security_targets_by_repository_identity\n ON security_targets(repository_identity);",
+ "ALTER TABLE scans\n ADD COLUMN repository_generation TEXT;",
+ "CREATE INDEX scans_by_repository_generation\n ON scans(repository_generation);",
+ "CREATE INDEX scans_by_target_path\n ON scans(target_path);",
+ "ALTER TABLE scans\n ADD COLUMN completion_sequence INTEGER CHECK (completion_sequence >= 1);",
+ "CREATE UNIQUE INDEX scans_completion_sequence\n ON scans(completion_sequence);",
+ "CREATE TRIGGER scans_assign_inserted_completion_sequence\n AFTER INSERT ON scans\n WHEN NEW.status = 'complete' AND NEW.completion_sequence IS NULL\n BEGIN\n UPDATE scans\n SET completion_sequence = (\n SELECT COALESCE(MAX(completion_sequence), 0) + 1 FROM scans\n )\n WHERE id = NEW.id AND completion_sequence IS NULL;\n END;",
+ "CREATE TRIGGER scans_assign_updated_completion_sequence\n AFTER UPDATE OF status ON scans\n WHEN NEW.status = 'complete' AND NEW.completion_sequence IS NULL\n BEGIN\n UPDATE scans\n SET completion_sequence = (\n SELECT COALESCE(MAX(completion_sequence), 0) + 1 FROM scans\n )\n WHERE id = NEW.id AND completion_sequence IS NULL;\n END;"
+ ]
+ },
+ {
+ "version": 44,
+ "name": "stabilize Linux repository generations",
+ "statements": []
+ }
+]
diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_database.ts b/plugins/codex-security/mcp-app/tests/test_workbench_database.ts
index 5f9c73d69..41e080eda 100644
--- a/plugins/codex-security/mcp-app/tests/test_workbench_database.ts
+++ b/plugins/codex-security/mcp-app/tests/test_workbench_database.ts
@@ -1,13 +1,16 @@
import assert from "node:assert/strict";
+import { spawnSync } from "node:child_process";
+import { fileURLToPath } from "node:url";
import { createHash } from "node:crypto";
import { once } from "node:events";
-import { mkdir, stat, symlink } from "node:fs/promises";
+import { copyFile, mkdir, readFile, 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 workbenchMigrations from "../../shared/workbench-migrations.json" with { type: "json" };
import type * as Database from "../src/workbench/database.ts";
import type * as Migrations from "../src/workbench/migrations.ts";
@@ -28,6 +31,17 @@ const { openWorkbenchDatabase, databaseInfo } = (await importSource(
const { applyMigrations, migrations } = (await importSource(
"src/workbench/migrations.ts",
)) as typeof Migrations;
+// Frozen from 7fa0980411d89d7d1f2aafd5b2cbae941f539724, before severity migration 43.
+const historicalRepositoryMigrations: readonly Migrations.Migration[] =
+ JSON.parse(
+ await readFile(
+ new URL(
+ "./fixtures/pre-severity-repository-migrations.json",
+ import.meta.url,
+ ),
+ "utf8",
+ ),
+ );
const temporary = createTemporaryDirectories(true);
after(() => temporary.cleanup());
@@ -683,7 +697,7 @@ test("database-info retains scan names from installed migration 42", async () =>
await databaseInfo(directory);
const upgraded = new DatabaseSync(databasePath);
try {
- assertMigrationNames(upgraded, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51);
+ assertMigrationNames(upgraded, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52);
assert.equal(storedValue(upgraded, "scans", "name"), "Original scan");
assert.equal(storedValue(upgraded, "scans", "source_scopes_json"), null);
assert.equal(
@@ -977,6 +991,57 @@ test("repairs recorded Deep Scan and target migrations and backfills stable iden
assert.equal(storedValue(database, "scans", "target_id"), null);
});
+test("stable target repairs leave unvalidated setup paths unbound", (t) => {
+ const database = memory(t, 53);
+ insertScan(database);
+ database.exec(`
+ ALTER TABLE workspaces ADD COLUMN target_path_unvalidated INTEGER NOT NULL
+ DEFAULT 0 CHECK (target_path_unvalidated IN (0, 1));
+ INSERT INTO workspaces (
+ id, target_path, target_path_unvalidated, created_at, updated_at
+ ) VALUES
+ ('unvalidated', '/synthetic/missing', 1, 'created', 'updated'),
+ ('unvalidated-shared', '/synthetic/repository', 1, 'created', 'updated');
+ `);
+ for (let attempt = 0; attempt < 2; attempt++) {
+ database.exec("DROP INDEX scans_by_target");
+ applyMigrations(database);
+ const targetId = database
+ .prepare(
+ "SELECT id FROM security_targets WHERE current_path = '/synthetic/repository'",
+ )
+ .get()?.id;
+ assert.equal(typeof targetId, "string");
+ assert.equal(
+ database.prepare("SELECT target_id FROM scans").get()?.target_id,
+ targetId,
+ );
+ assert.deepEqual(
+ database
+ .prepare(
+ "SELECT id, target_id, target_path_unvalidated FROM workspaces ORDER BY id",
+ )
+ .all()
+ .map((row) => ({ ...row })),
+ [
+ { id: "unvalidated", target_id: null, target_path_unvalidated: 1 },
+ {
+ id: "unvalidated-shared",
+ target_id: null,
+ target_path_unvalidated: 1,
+ },
+ { id: "workspace", target_id: targetId, target_path_unvalidated: 0 },
+ ],
+ );
+ assert.equal(
+ database.prepare("SELECT COUNT(*) AS count FROM security_targets").get()
+ ?.count,
+ 1,
+ );
+ assert.deepEqual(database.prepare("PRAGMA foreign_key_check").all(), []);
+ }
+});
+
test("recorded additive migrations restore missing columns and configured error thresholds", (t) => {
const database = memory(t, 26);
insertScan(database);
@@ -1264,3 +1329,478 @@ test("retries an upgrade when another process holds the write lock beyond the bu
await writer.terminate();
}
});
+
+function pythonMigrations(databasePath: string, through?: number): void {
+ const result = spawnSync(
+ process.env.PYTHON?.trim() || "python3",
+ [
+ "-I",
+ "-B",
+ "-c",
+ `import sqlite3, sys
+sys.path.insert(0, sys.argv[1])
+import workbench_db, workbench_schema
+with sqlite3.connect(sys.argv[2]) as database:
+ database.row_factory = sqlite3.Row
+ database.execute("PRAGMA foreign_keys = ON")
+ workbench_schema.apply_migrations(database,
+ tuple(item for item in workbench_db.MIGRATIONS if item[0] <= int(sys.argv[3])),
+ workbench_db.now, workbench_db.backfill_security_targets)
+`,
+ fileURLToPath(new URL("../../scripts", import.meta.url)),
+ databasePath,
+ String(through ?? workbenchMigrations.at(-1)!.version),
+ ],
+ { encoding: "utf8" },
+ );
+ assert.equal(result.status, 0, result.stderr || String(result.error));
+}
+
+function storedRows(database: DatabaseSync) {
+ return database
+ .prepare(
+ "SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name",
+ )
+ .all()
+ .map(({ name }) => {
+ const rows = database.prepare(`SELECT * FROM "${name}"`);
+ rows.setReadBigInts(true);
+ return [
+ name,
+ rows.all().map((row) => {
+ // Independently run initializers use different migration application times.
+ if (name === "schema_migrations") delete row.applied_at;
+ return row;
+ }),
+ ];
+ });
+}
+
+test("Node initialization leaves repository migration semantics to the Python consumer", async (t) => {
+ // Published schema version, followed by the historical identity migration range if installed.
+ const fixtures: [string, number, number?, number?][] = [
+ ["main42", 42],
+ ["main43", 43],
+ ["main53", 53],
+ ["main60", 60],
+ ["main62", 62],
+ ["preview62", 60, 61, 62],
+ ["preview64MainReader", 60, 61, 64],
+ ["legacy30", 29],
+ ["legacy31", 30],
+ ["partial43", 42],
+ ["current43", 42, 43, 43],
+ ["current44", 42, 43, 44],
+ ["current48", 47, 48, 48],
+ ["current49", 47, 48, 49],
+ ["current50", 49, 50, 50],
+ ["current51", 49, 50, 51],
+ ["preview52and53", 51, 52, 53],
+ ["current53", 52, 53, 53],
+ ["current54", 52, 53, 54],
+ ["current56", 53, 54, 56],
+ ];
+ for (const [
+ fixture,
+ publishedVersion,
+ identityVersion,
+ currentVersion,
+ ] of fixtures) {
+ await t.test(fixture, async () => {
+ const directory = await temporary.create("workbench-repository-upgrade-");
+ const databasePath = join(directory, "workbench.sqlite3");
+ const database = new DatabaseSync(databasePath);
+ applyMigrations(
+ database,
+ migrations.filter((item) => item.version <= publishedVersion),
+ );
+ insertScan(database);
+ database.exec(`INSERT INTO security_targets (id,current_path,display_name,created_at,updated_at)
+ VALUES ('target','/synthetic/repository','Synthetic repository','created','updated');
+ UPDATE workspaces SET target_id='target'; UPDATE scans SET target_id='target';
+ UPDATE scans SET status='complete', started_at='2026-01-01T00:00:00Z',
+ completed_at='2026-01-01T00:00:01.123456789123z';`);
+ if (fixture === "main53")
+ database.exec(`INSERT INTO external_source_reports
+ (id,repository,provider,source_key,source_finding_id,evidence_json,version,created_at,updated_at)
+ VALUES ('report','/synthetic/repository','synthetic','source','finding','{"title":"Saved report"}',1,'created','updated')`);
+ if (["legacy30", "legacy31", "partial43"].includes(fixture)) {
+ database.exec(
+ "ALTER TABLE security_targets ADD COLUMN repository_identity TEXT",
+ );
+ database.exec(
+ "UPDATE security_targets SET repository_identity='unestablished-legacy-identity'",
+ );
+ }
+ if (fixture === "legacy30" || fixture === "legacy31")
+ database
+ .prepare(
+ "INSERT INTO schema_migrations VALUES(?,'persist repository identities','original')",
+ )
+ .run(fixture === "legacy30" ? 30 : 31);
+ database.close();
+ if (identityVersion !== undefined && currentVersion !== undefined) {
+ const current = new DatabaseSync(databasePath);
+ applyMigrations(
+ current,
+ historicalRepositoryMigrations
+ .map((item) => ({
+ ...item,
+ version: item.version - 43 + identityVersion,
+ }))
+ .filter((item) => item.version <= currentVersion),
+ );
+ if (currentVersion === 56)
+ current.exec(`ALTER TABLE workspaces ADD COLUMN target_path_unvalidated INTEGER
+ NOT NULL DEFAULT 0 CHECK (target_path_unvalidated IN (0, 1));
+ INSERT INTO schema_migrations VALUES (56, 'distinguish unvalidated workspace targets', 'original');`);
+ if (fixture === "preview64MainReader")
+ current.exec(`ALTER TABLE workspaces ADD COLUMN target_path_unvalidated INTEGER
+ NOT NULL DEFAULT 0 CHECK (target_path_unvalidated IN (0, 1));
+ ALTER TABLE security_targets ADD COLUMN git_checkout_root TEXT;
+ INSERT INTO schema_migrations VALUES (63, 'distinguish unvalidated workspace targets', 'original');
+ INSERT INTO schema_migrations VALUES (64, 'retain checkout roots for Git discovery', 'original');
+ ALTER TABLE scans ADD COLUMN launch_surface TEXT;`);
+ assert.equal(
+ current
+ .prepare("SELECT MAX(version) AS version FROM schema_migrations")
+ .get()?.version,
+ currentVersion,
+ );
+ current.exec(
+ "UPDATE scans SET completion_sequence=77,repository_generation='synthetic-generation'; UPDATE security_targets SET repository_identity='synthetic-generation'",
+ );
+ current.close();
+ }
+ const saved = new DatabaseSync(databasePath);
+ const originalReports =
+ fixture === "main53"
+ ? saved.prepare("SELECT * FROM external_source_reports").all()
+ : [];
+ const originalHistory = saved
+ .prepare(
+ "SELECT name, applied_at FROM schema_migrations WHERE name IN ('persist repository identities', 'stabilize Linux repository generations', 'distinguish unvalidated workspace targets') ORDER BY name",
+ )
+ .all();
+ saved.close();
+ const controlPath = join(directory, "python.sqlite3");
+ await copyFile(databasePath, controlPath);
+ pythonMigrations(controlPath);
+ await databaseInfo(directory);
+ const initialized = new DatabaseSync(databasePath);
+ assertMigrationNames(initialized, 48, 49, 53, 54, 55, 56, 60);
+ assert.deepEqual(
+ initialized.prepare("SELECT * FROM external_source_reports").all(),
+ originalReports,
+ );
+ const identity = initialized
+ .prepare("SELECT * FROM security_targets")
+ .get()?.repository_identity;
+ assert.equal(
+ identity,
+ currentVersion !== undefined
+ ? "synthetic-generation"
+ : fixture.startsWith("main")
+ ? undefined
+ : "unestablished-legacy-identity",
+ );
+ assert.equal(
+ initialized
+ .prepare("PRAGMA table_info(scans)")
+ .all()
+ .some((row) => row.name === "repository_generation"),
+ currentVersion !== undefined,
+ );
+ const beforeRepeat = storedRows(initialized);
+ initialized.close();
+ await databaseInfo(directory);
+ const repeated = new DatabaseSync(databasePath);
+ assert.deepEqual(storedRows(repeated), beforeRepeat);
+ repeated.close();
+ pythonMigrations(databasePath);
+ const actual = new DatabaseSync(databasePath);
+ const expected = new DatabaseSync(controlPath);
+ try {
+ assert.deepEqual(storedRows(actual), storedRows(expected));
+ assert.deepEqual(
+ actual.prepare("SELECT * FROM external_source_reports").all(),
+ originalReports,
+ );
+ for (const row of originalHistory)
+ assert.equal(
+ actual
+ .prepare("SELECT applied_at FROM schema_migrations WHERE name=?")
+ .get(row.name!)?.applied_at,
+ row.applied_at,
+ );
+ assert.equal(
+ actual
+ .prepare("SELECT MAX(version) AS version FROM schema_migrations")
+ .get()?.version,
+ workbenchMigrations.at(-1)!.version,
+ );
+ assert.equal(
+ actual.prepare("SELECT completion_sequence FROM scans").get()
+ ?.completion_sequence,
+ currentVersion !== undefined ? 77 : 1,
+ );
+ assert.deepEqual(actual.prepare("PRAGMA foreign_key_check").all(), []);
+ } finally {
+ actual.close();
+ expected.close();
+ }
+ });
+ }
+});
+
+test("Node repository migration-label collisions roll back without changing stored rows", async (t) => {
+ for (const [name, setup] of [
+ [
+ "identity destination",
+ "UPDATE schema_migrations SET name='persist repository identities' WHERE version=31; INSERT INTO schema_migrations VALUES(67,'persist repository identities','original')",
+ ],
+ [
+ "generation destination",
+ "UPDATE schema_migrations SET name='stabilize Linux repository generations' WHERE version=44; INSERT INTO schema_migrations VALUES(68,'occupied destination','retained')",
+ ],
+ [
+ "identity collision after generation relocation",
+ "UPDATE schema_migrations SET name='persist repository identities' WHERE version IN (31,43); UPDATE schema_migrations SET name='stabilize Linux repository generations' WHERE version=44",
+ ],
+ ] as const)
+ await t.test(name, async () => {
+ const directory = await temporary.create(
+ "workbench-repository-collision-",
+ );
+ const databasePath = join(directory, "workbench.sqlite3");
+ const database = new DatabaseSync(databasePath);
+ applyMigrations(database);
+ database.exec(setup);
+ const beforeSchema = schema(database);
+ const beforeRows = storedRows(database);
+ const beforeHistory = database
+ .prepare("SELECT * FROM schema_migrations ORDER BY version")
+ .all();
+ database.close();
+ await assert.rejects(
+ databaseInfo(directory),
+ /UNIQUE constraint failed/u,
+ );
+ const after = new DatabaseSync(databasePath);
+ try {
+ assert.deepEqual(schema(after), beforeSchema);
+ assert.deepEqual(storedRows(after), beforeRows);
+ assert.deepEqual(
+ after
+ .prepare("SELECT * FROM schema_migrations ORDER BY version")
+ .all(),
+ beforeHistory,
+ );
+ } finally {
+ after.close();
+ }
+ });
+});
+
+test("Node database-info preserves a linked repository's stored43 history for Python", async () => {
+ const directory = await temporary.create("workbench-linked-upgrade-");
+ const databasePath = join(directory, "workbench.sqlite3");
+ const repository = join(directory, "repository");
+ const linked = join(directory, "linked");
+ const database = new DatabaseSync(databasePath);
+ applyMigrations(
+ database,
+ migrations.filter((item) => item.version <= 42),
+ );
+ insertScan(database, repository);
+ database.exec(
+ "INSERT INTO scan_progress (scan_id,updated_at) VALUES ('scan','updated')",
+ );
+ database.close();
+ const scripts = fileURLToPath(new URL("../../scripts", import.meta.url));
+ const seeded = spawnSync(
+ process.env.PYTHON?.trim() || "python3",
+ [
+ "-I",
+ "-B",
+ "-c",
+ `
+import json, pathlib, sqlite3, subprocess, sys
+from datetime import datetime, timezone
+sys.path.insert(0, sys.argv[1])
+import workbench_db as workbench, workbench_schema as schema, workbench_target_state as state
+repository, linked = map(pathlib.Path, sys.argv[3:5])
+subprocess.run(['git', 'init', '-q', str(repository)], check=True)
+subprocess.run(['git', '-C', str(repository), '-c', 'user.name=Fixture', '-c',
+ '[email protected]', 'commit', '-q', '--allow-empty', '-m', 'fixture'], check=True)
+subprocess.run(['git', '-C', str(repository), 'worktree', 'add', '-q', '--detach', str(linked)], check=True)
+with sqlite3.connect(sys.argv[2]) as connection:
+ connection.row_factory = sqlite3.Row
+ timestamp = datetime.now(timezone.utc).isoformat()
+ connection.execute('UPDATE scans SET started_at=?, created_at=?, updated_at=?', (timestamp, timestamp, timestamp))
+ connection.execute('UPDATE scan_progress SET updated_at=?', (timestamp,))
+ metadata = repository.stat()
+ connection.execute('UPDATE scans SET target_device=?, target_inode=?', (str(metadata.st_dev), str(metadata.st_ino)))
+ migration = json.loads(pathlib.Path(sys.argv[5]).read_text())[0]
+ assert migration['version'] == 43 and migration['name'] == 'persist repository identities'
+ for statement in migration['statements']:
+ connection.execute(statement)
+ connection.execute('INSERT INTO schema_migrations VALUES (?, ?, ?)',
+ (migration['version'], migration['name'], 'original'))
+ workbench.backfill_security_targets(connection)
+ identity = state._repository_identity_details(repository)
+ generation = identity.previous_value or identity.value
+ connection.execute('UPDATE security_targets SET repository_identity=?', (generation,))
+ connection.execute('UPDATE scans SET repository_generation=?', (generation,))
+`,
+ scripts,
+ databasePath,
+ repository,
+ linked,
+ fileURLToPath(
+ new URL(
+ "./fixtures/pre-severity-repository-migrations.json",
+ import.meta.url,
+ ),
+ ),
+ ],
+ { encoding: "utf8" },
+ );
+ assert.equal(seeded.status, 0, seeded.stderr || String(seeded.error));
+ const before = new DatabaseSync(databasePath);
+ const savedRows = storedRows(before);
+ assert.equal(
+ before
+ .prepare("SELECT MAX(version) AS version FROM schema_migrations")
+ .get()?.version,
+ 43,
+ );
+ before.close();
+ assert.equal((await databaseInfo(directory)).databasePath, databasePath);
+ const initialized = new DatabaseSync(databasePath);
+ assert.deepEqual(
+ storedRows(initialized)
+ .filter(
+ ([name]) =>
+ name !== "schema_migrations" &&
+ name !== "scan_severity_assessments" &&
+ name !== "local_finding_embeddings" &&
+ name !== "finding_issue_receipts" &&
+ name !== "deep_scan_worker_threads" &&
+ name !== "external_source_reports" &&
+ name !== "sqlite_sequence",
+ )
+ .map(([name, rows]) => {
+ if (name === "scans")
+ for (const row of rows as Record<string, unknown>[]) {
+ for (const column of [
+ "source_scopes_json",
+ "retained_checkpoint_heads_json",
+ "launch_surface",
+ "launch_package_version",
+ "launch_plugin_version",
+ "launch_kind",
+ "log_completed_at",
+ "windows_resume_output_identity",
+ "windows_resume_target_identity",
+ ]) {
+ assert.equal(row[column], null);
+ delete row[column];
+ }
+ }
+ if (name === "workspaces")
+ for (const row of rows as Record<string, unknown>[]) {
+ assert.equal(row.deep_scan_config_json, null);
+ delete row.deep_scan_config_json;
+ }
+ return [name, rows];
+ }),
+ savedRows.filter(
+ ([name]) =>
+ name !== "schema_migrations" &&
+ name !== "finding_publications" &&
+ name !== "sqlite_sequence",
+ ),
+ );
+ assert.deepEqual(
+ initialized.prepare("SELECT * FROM deep_scan_worker_threads").all(),
+ [],
+ );
+ assert.deepEqual(
+ savedRows.find(([name]) => name === "finding_publications")?.[1],
+ [],
+ );
+ assert.deepEqual(
+ savedRows.find(([name]) => name === "sqlite_sequence")?.[1],
+ [],
+ );
+ assert.deepEqual(
+ initialized.prepare("SELECT * FROM finding_issue_receipts").all(),
+ [],
+ );
+ assert.deepEqual(
+ initialized
+ .prepare("SELECT name, seq FROM sqlite_sequence")
+ .all()
+ .map((row) => ({ ...row })),
+ [{ name: "finding_issue_receipts", seq: 0 }],
+ );
+ assert.equal(
+ initialized
+ .prepare("SELECT COUNT(*) AS count FROM local_finding_embeddings")
+ .get()?.count,
+ 0,
+ );
+ assert.equal(
+ initialized
+ .prepare("SELECT COUNT(*) AS count FROM scan_severity_assessments")
+ .get()?.count,
+ 0,
+ );
+ assert.deepEqual(
+ initialized.prepare("SELECT * FROM external_source_reports").all(),
+ [],
+ );
+ assertMigrationNames(initialized, 53);
+ assert.equal(
+ initialized
+ .prepare("SELECT name FROM schema_migrations WHERE version=67")
+ .get()?.name,
+ "persist repository identities",
+ );
+ assert.equal(
+ initialized
+ .prepare("SELECT name FROM schema_migrations WHERE version=68")
+ .get(),
+ undefined,
+ );
+ initialized.close();
+ const queried = spawnSync(
+ process.env.PYTHON?.trim() || "python3",
+ [
+ "-I",
+ "-B",
+ "-c",
+ `
+import argparse, sqlite3, sys
+sys.path.insert(0, sys.argv[1])
+import workbench_db as workbench, workbench_scan_history as history
+with sqlite3.connect(sys.argv[2]) as connection:
+ connection.row_factory = sqlite3.Row
+ workbench.apply_migrations(connection)
+ for repository in sys.argv[3:5]:
+ args = argparse.Namespace(repository=repository, scan_root=None, target_id=None,
+ mode=None, status=None, query=None, limit=None, offset=0)
+ scans = history.list_scans(connection, args)['scans']
+ assert [scan['scanId'] for scan in scans] == ['scan'], (repository, scans)
+ assert connection.execute('SELECT MAX(version) FROM schema_migrations').fetchone()[0] == workbench.MIGRATIONS[-1][0]
+`,
+ scripts,
+ databasePath,
+ repository,
+ linked,
+ ],
+ { encoding: "utf8" },
+ );
+ assert.equal(queried.status, 0, queried.stderr || String(queried.error));
+});
diff --git a/plugins/codex-security/scripts/deep_scan_workbench.py b/plugins/codex-security/scripts/deep_scan_workbench.py
index 0ea4c6a05..c83145644 100644
--- a/plugins/codex-security/scripts/deep_scan_workbench.py
+++ b/plugins/codex-security/scripts/deep_scan_workbench.py
@@ -25,6 +25,7 @@ from workbench.storage import (
from workbench_constants import PHASES
from workbench_source_scopes import capture_source_scopes
from workbench_target import directory_snapshot_regular_file_count
+from workbench_target_state import require_scan_checkout_owner
from workbench_validation import optional_text, require_uuid, user_context_argument
DEEP_SCAN_COORDINATOR_LEASE_SECONDS = 30
@@ -611,6 +612,7 @@ def begin_deep_scan_for_scan(
) -> dict[str, Any]:
scan_id = require_uuid(scan_id, "scan-id")
candidate = dependencies().require_scan(connection, scan_id)
+ require_scan_checkout_owner(connection, candidate)
model_thread_id = getattr(args, "model_thread_id", None)
require_tokenless_scan_owner(connection, candidate, model_thread_id)
workspace = dependencies().require_workspace(connection, candidate["workspace_id"])
@@ -695,6 +697,7 @@ def begin_deep_scan_for_scan(
connection.execute("BEGIN IMMEDIATE")
with connection:
scan, _ = require_owned_scan(connection, scan_id, thread_id)
+ require_scan_checkout_owner(connection, scan)
require_current_continuation(
scan,
args.claim_token,
@@ -761,6 +764,7 @@ def begin_deep_scan_for_target(
with connection:
existing = existing_deep_scan_for_target(connection, thread_id, target_path, scope)
if existing is not None:
+ require_scan_checkout_owner(connection, existing)
connection.commit()
return begin_deep_scan_for_scan(connection, existing["id"], thread_id, args)
current_target = dependencies().require_remediation_target(target_path)
@@ -785,6 +789,7 @@ def begin_deep_scan_for_target(
target_inode,
)
if terminal is not None:
+ require_scan_checkout_owner(connection, terminal)
connection.commit()
return deep_scan_result(
connection,
@@ -810,7 +815,8 @@ def begin_deep_scan_for_target(
workspace_id = str(uuid.uuid4())
scan_id = str(uuid.uuid4())
timestamp = dependencies().now()
- target_id = dependencies().ensure_security_target(connection, target_path)
+ registration = dependencies().register_security_target(connection, target_path)
+ target_id = registration.target_id
connection.execute(
"""
INSERT INTO workspaces (
@@ -842,6 +848,7 @@ def begin_deep_scan_for_target(
scope=scope,
diff_target=None,
target_identity=target_identity,
+ repository_generation=registration.repository_generation,
source_scopes=source_scopes,
target_root=target_root,
target_summary=None,
diff --git a/plugins/codex-security/scripts/workbench_cli.py b/plugins/codex-security/scripts/workbench_cli.py
index b4dca9287..636e904df 100644
--- a/plugins/codex-security/scripts/workbench_cli.py
+++ b/plugins/codex-security/scripts/workbench_cli.py
@@ -227,6 +227,7 @@ def parse_args(description: str, *, inspection_only: bool = False) -> argparse.N
list_scans.add_argument("--limit", type=positive_int)
list_unmatched_scan_pairs = add_command("list-unmatched-scan-pairs", "--repository")
+ list_unmatched_scan_pairs.add_argument("--after-scan-id")
list_unmatched_scan_pairs.add_argument("--force", action="store_true")
register_cli_scan = add_command("register-cli-scan", "--scan-dir", "--repository")
@@ -242,7 +243,16 @@ def parse_args(description: str, *, inspection_only: bool = False) -> argparse.N
)
register_cli_scan.add_argument("--archived-scan-dir")
- add_command("set-scan-thread", "--scan-id", "--thread-id")
+ restore_cli_scan_archive = subparsers.add_parser("restore-cli-scan-archive")
+ restore_cli_scan_archive.add_argument("--scan-dir", required=True)
+ restore_cli_scan_archive.add_argument("--archived-scan-dir", required=True)
+ previous_scan = restore_cli_scan_archive.add_mutually_exclusive_group(required=True)
+ previous_scan.add_argument("--previous-scan-id")
+ previous_scan.add_argument("--previous-scan-absent", action="store_true")
+
+ set_scan_thread = subparsers.add_parser("set-scan-thread")
+ set_scan_thread.add_argument("--scan-id", required=True)
+ set_scan_thread.add_argument("--thread-id", required=True)
set_scan_cost_limit = add_command("set-scan-cost-limit", "--scan-id")
set_scan_cost_limit.add_argument("--max-cost-usd", required=True, type=float)
@@ -270,7 +280,9 @@ def parse_args(description: str, *, inspection_only: bool = False) -> argparse.N
list_global_findings.add_argument("--query")
list_global_findings.add_argument("--severity", choices=FINDING_SEVERITIES)
list_global_findings.add_argument("--status", choices=FINDING_STATUSES)
- list_global_findings.add_argument("--target-id")
+ finding_repository = list_global_findings.add_mutually_exclusive_group()
+ finding_repository.add_argument("--target-id")
+ finding_repository.add_argument("--repository")
list_global_findings.add_argument("--offset", type=non_negative_int, default=0)
list_global_findings.add_argument("--limit", type=positive_int, default=FINDINGS_PAGE_MAX)
list_repositories = subparsers.add_parser("list-repositories")
diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.py
index 4af2afdfe..024ec6228 100644
--- a/plugins/codex-security/scripts/workbench_db.py
+++ b/plugins/codex-security/scripts/workbench_db.py
@@ -77,7 +77,6 @@ from workbench_constants import (
FINDING_SUMMARY_BYTES,
FINDING_TITLE_BYTES,
FINDINGS_RESULT_LIMIT,
- PATCH_PREVIEW_BYTES,
SQLITE_RETRY_ATTEMPTS,
)
from workbench_feedback import get_scan_feedback
@@ -89,6 +88,7 @@ from workbench_scan_start import (
loaded_plugin_version,
prepare_cli_scan_target,
register_windows_resume_output,
+ restore_cli_scan_archive,
safe_segment,
scan_diff_identity,
scan_target_identity,
@@ -96,6 +96,8 @@ from workbench_scan_start import (
)
from workbench_schema import (
MIGRATIONS,
+ migration_target_paths,
+ recorded_git_checkout_roots,
)
from workbench_schema import (
apply_migrations as apply_schema_migrations,
@@ -134,17 +136,25 @@ from workbench_target import (
git_submodule_entries,
git_target_metadata,
git_worktree_context,
+ rejected_checkout_roots,
remediation_checkout_snapshot,
require_remediation_target,
require_scan_target_identity,
require_scan_target_unchanged,
restore_directory_junctions,
scan_target_warning,
+ scoped_git_discovery,
scoped_regular_file_count,
worktree_content_digest,
worktree_content_digest_for_context,
)
-from workbench_target_state import backfill_security_targets, ensure_security_target
+from workbench_target_state import (
+ RepositoryScanScope,
+ backfill_security_targets,
+ ensure_security_target,
+ register_security_target,
+ require_scan_checkout_owner,
+)
from workbench_validation import (
bounded_output_text,
optional_text,
@@ -243,13 +253,17 @@ def release_completion_file_lock(descriptor: int) -> None:
windows_file_lock.locking(descriptor, windows_file_lock.LK_UNLCK, 1)
-def connect(*, deferred: bool = False) -> sqlite3.Connection:
+def connect(
+ *, deferred: bool = False, discovery_roots: tuple[Path, ...] = ()
+) -> sqlite3.Connection:
"""Open the workbench SQLite database and bring its schema up to date.
Args:
deferred (bool): Let the first migration attempt defer its write lock.
Retries after lock contention use an immediate transaction.
+ discovery_roots: Additional local paths for bounded target discovery.
+
Returns:
sqlite3.Connection: A caller-owned connection with named rows, foreign
keys enabled, and write-ahead logging.
@@ -280,7 +294,9 @@ def connect(*, deferred: bool = False) -> sqlite3.Connection:
connection.execute("PRAGMA foreign_keys = ON")
connection.execute("PRAGMA busy_timeout = 5000")
# Keep writer admission retries; readers only wait when a repair needs to write.
- apply_migrations(connection, immediate=not deferred or attempt > 0)
+ apply_migrations(
+ connection, immediate=not deferred or attempt > 0, discovery_roots=discovery_roots
+ )
connection.execute("PRAGMA journal_mode = WAL")
path.chmod(0o600)
return connection
@@ -304,9 +320,19 @@ def begin_immediate_with_retry(connection: sqlite3.Connection) -> None:
time.sleep(0.05 * (2**attempt))
-def apply_migrations(connection: sqlite3.Connection, *, immediate: bool = False) -> None:
+def apply_migrations(
+ connection: sqlite3.Connection,
+ *,
+ immediate: bool = False,
+ discovery_roots: tuple[Path, ...] = (),
+) -> None:
apply_schema_migrations(
- connection, MIGRATIONS, now, backfill_security_targets, immediate=immediate
+ connection,
+ MIGRATIONS,
+ now,
+ backfill_security_targets,
+ immediate=immediate,
+ discovery_roots=discovery_roots,
)
@@ -358,24 +384,7 @@ def scan_contract(scan: sqlite3.Row) -> dict[str, Any]:
}
-def expected_coverage_mode(scan: sqlite3.Row) -> str:
- """Return the coverage mode implied by the recorded scope, scan mode, and diff target kind."""
- if scan["mode"] == "diff":
- mode = {
- "commit": "commit",
- "range": "branch_diff",
- "working_tree": "working_tree",
- }.get(scan["diff_target_kind"])
- if mode is None:
- raise SystemExit("This migrated diff scan does not have a validated change set.")
- return mode
- if scan["scope"] != "." or (
- "recipe_json" in scan.keys()
- and scan["recipe_json"] is not None
- and json.loads(scan["recipe_json"])["target"]["kind"] == "paths"
- ):
- return "scoped_path"
- return "deep_repository" if scan["mode"] == "deep" else "repository"
+expected_coverage_mode = scan_history.expected_coverage_mode
def workbench_completion_binding(
@@ -546,6 +555,8 @@ def create_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -
diff_content_digest = (
optional_text(args.diff_content_digest, maximum=128) if args.mode == "diff" else None
)
+ target_path_unvalidated = False
+ rejected_roots = None
if target_path:
try:
inspected = inspect_setup_values(
@@ -565,11 +576,17 @@ def create_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -
diff_head_revision = inspected["diffTarget"]["headRevision"]
diff_content_digest = inspected["diffTarget"].get("contentDigest")
except SystemExit:
- # Preserve an incomplete setup so workspace_state can report its validation error.
- pass
+ # Setup may fail on scope even when the target itself is valid.
+ try:
+ require_target(target_path)
+ except SystemExit:
+ target_path_unvalidated = True
+ rejected_roots = rejected_checkout_roots(target_path)
with connection:
target_id = (
- ensure_security_target(connection, target_path) if target_path is not None else None
+ ensure_security_target(connection, target_path)
+ if target_path is not None and not target_path_unvalidated
+ else None
)
connection.execute(
"""
@@ -577,8 +594,8 @@ def create_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -
id, thread_id, target_id, target_path, target_title, target_summary,
default_scope, default_mode,
user_context, diff_target_kind, diff_base_revision, diff_head_revision,
- diff_content_digest, created_at, updated_at
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ diff_content_digest, created_at, updated_at, target_path_unvalidated, rejected_checkout_roots_json
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
workspace_id,
@@ -596,6 +613,8 @@ def create_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -
diff_content_digest,
timestamp,
timestamp,
+ target_path_unvalidated,
+ json.dumps(rejected_roots) if rejected_roots is not None else None,
),
)
return workspace_state(connection, workspace_id)
@@ -630,7 +649,10 @@ def save_workspace(connection: sqlite3.Connection, args: argparse.Namespace) ->
SET target_id = ?, target_path = ?, target_title = ?, target_summary = ?, default_scope = ?,
default_mode = ?, user_context = ?, diff_target_kind = ?,
diff_base_revision = ?, diff_head_revision = ?, diff_content_digest = ?,
- deep_scan_config_json = ?, submitted = 1, updated_at = ?
+ deep_scan_config_json = ?, submitted = 1, updated_at = ?, target_path_unvalidated = 0,
+ -- JSON null keeps PATH discovery disabled after a legacy rejection.
+ rejected_checkout_roots_json = COALESCE(rejected_checkout_roots_json,
+ CASE WHEN target_path_unvalidated = 1 AND target_id IS NULL THEN 'null' END)
WHERE id = ? AND active_scan_id IS NULL
""",
(
@@ -704,6 +726,7 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
(workspace["id"],),
).fetchone()
if active is not None:
+ require_scan_checkout_owner(connection, active)
return workspace_state(connection, workspace["id"], result_scan_id=active["id"])
workspace_version = workspace["updated_at"]
scan_id = str(uuid.uuid4())
@@ -753,6 +776,7 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
(workspace["id"],),
).fetchone()
if active is not None:
+ require_scan_checkout_owner(connection, active)
if manages_transaction:
connection.commit()
return workspace_state(connection, workspace["id"], result_scan_id=active["id"])
@@ -779,6 +803,11 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
"This Codex thread already has an active Deep Scan for the selected "
"target and scope. Rejoin that scan instead of starting another one."
)
+ registration = register_security_target(connection, str(current_target))
+ if registration.target_id != workspace["target_id"]:
+ raise SystemExit(
+ "The saved workspace no longer matches the selected repository target."
+ )
insert_running_scan(
connection,
scan_id=scan_id,
@@ -787,6 +816,7 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
scope=scope,
diff_target=diff_target,
target_identity=target_identity,
+ repository_generation=registration.repository_generation,
source_scopes=source_scopes,
target_root=target_root,
target_summary=target_summary,
@@ -906,6 +936,7 @@ def _start_prompt_driven_scan(
),
).fetchone()
if existing is not None:
+ require_scan_checkout_owner(connection, existing)
connection.commit()
return {
**scan_context(connection, existing["id"], model_thread_id=thread_id),
@@ -915,7 +946,8 @@ def _start_prompt_driven_scan(
workspace_id = str(uuid.uuid4())
scan_id = str(uuid.uuid4())
timestamp = now()
- target_id = ensure_security_target(connection, target_path)
+ registration = register_security_target(connection, target_path)
+ target_id = registration.target_id
connection.execute(
"""
INSERT INTO workspaces (
@@ -948,6 +980,7 @@ def _start_prompt_driven_scan(
scope=scope,
diff_target=diff_target,
target_identity=target_identity,
+ repository_generation=registration.repository_generation,
source_scopes=source_scopes,
target_root=target_root,
target_summary=target_summary,
@@ -1560,10 +1593,9 @@ def register_cli_scan(
repository = require_target(args.repository)
require_scannable_target(repository)
- scan_dir = None
if not args.archive_existing or args.archived_scan_dir is not None:
# Preserve early output rejection when this call will not move the directory.
- scan_dir = require_cli_scan_directory(repository, args)
+ require_cli_scan_directory(repository, args)
user_context = None
workflow_id = None
hosted = False
@@ -1639,9 +1671,19 @@ def register_cli_scan(
with ExitStack() as archive:
deadline = None
while True:
- if scan_dir is None:
- # Wait for any previous archive to finish before inspecting its replacement.
- scan_dir = require_cli_scan_directory(repository, args)
+ registration = register_security_target(connection, str(repository))
+ target_id = registration.target_id
+ if parent_scan_id is not None:
+ parent = require_scan(connection, parent_scan_id)
+ parent_scope = RepositoryScanScope(
+ registration.repository_generation, target_id
+ )
+ if not parent_scope.contains(parent):
+ raise SystemExit(
+ "A rerun must belong to the same repository as its parent scan."
+ )
+ # Revalidate the output and its owner under the current write lock.
+ scan_dir = require_cli_scan_directory(repository, args)
try:
archived_scan_dir = archive.enter_context(
archive_scan(
@@ -1672,14 +1714,6 @@ def register_cli_scan(
scan_dir = None # Revalidate the directory and its owner under the next lock.
time.sleep(min(0.05, remaining))
connection.execute("BEGIN IMMEDIATE")
- target_id = ensure_security_target(connection, str(repository))
- if parent_scan_id is not None:
- parent = require_scan(connection, parent_scan_id)
- if parent["target_id"] != target_id:
- raise SystemExit(
- "A rerun must belong to the same repository as its parent scan."
- )
-
connection.execute(
"""
INSERT INTO workspaces (
@@ -1709,6 +1743,7 @@ def register_cli_scan(
scope=scope,
diff_target=diff_target,
target_identity=target_identity,
+ repository_generation=registration.repository_generation,
source_scopes=source_scopes,
target_root=scan_dir.parent,
target_summary=None,
@@ -3165,39 +3200,10 @@ def read_finding_details(value: str) -> dict[str, Any]:
return details if isinstance(details, dict) else {}
-def finding_management_updated_at(connection: sqlite3.Connection, scan_id: str) -> str | None:
- return connection.execute(
- """
- SELECT MAX(updated_at)
- FROM (
- SELECT triage.updated_at
- FROM finding_triage AS triage
- JOIN finding_occurrences AS occurrences ON occurrences.id = triage.occurrence_id
- WHERE occurrences.scan_id = ?
- UNION ALL
- SELECT remediation.updated_at
- FROM finding_remediation_attempts AS remediation
- JOIN finding_occurrences AS occurrences ON occurrences.id = remediation.occurrence_id
- WHERE occurrences.scan_id = ?
- )
- """,
- (scan_id, scan_id),
- ).fetchone()[0]
+finding_management_updated_at = scan_history.finding_management_updated_at
-def finding_triage_result(connection: sqlite3.Connection, occurrence_id: str) -> dict[str, Any]:
- row = connection.execute(
- "SELECT status, close_reason, note, updated_at FROM finding_triage WHERE occurrence_id = ?",
- (occurrence_id,),
- ).fetchone()
- if row is None:
- return {"status": "open"}
- return {
- "closeReason": row["close_reason"],
- "note": row["note"],
- "status": row["status"],
- "updatedAt": row["updated_at"],
- }
+finding_triage_result = scan_history.finding_triage_result
def finding_remediation_result(
@@ -3247,48 +3253,9 @@ def finding_remediation_result(
def patch_artifact_preview(
scan_dir: Path, relative_path: str | None, expected_digest: str | None
) -> tuple[str | None, dict[str, int | bool] | None]:
- if relative_path is None or expected_digest is None:
- return None, None
- digest = hashlib.sha256()
- preview = bytearray()
- additions = 0
- deletions = 0
- file_count = 0
- old_headers = 0
- new_headers = 0
- at_line_start = True
- try:
- with open_scan_local_file(scan_dir, relative_path) as patch:
- while chunk := patch.readline(1024 * 1024):
- digest.update(chunk)
- if len(preview) <= PATCH_PREVIEW_BYTES:
- preview.extend(chunk[: PATCH_PREVIEW_BYTES + 1 - len(preview)])
- if at_line_start:
- if chunk.startswith(b"diff --git "):
- file_count += 1
- elif chunk.startswith(b"+++ "):
- new_headers += 1
- elif chunk.startswith(b"--- "):
- old_headers += 1
- elif chunk.startswith(b"+"):
- additions += 1
- elif chunk.startswith(b"-"):
- deletions += 1
- at_line_start = chunk.endswith(b"\n")
- except SystemExit:
- return None, None
- if f"sha256:{digest.hexdigest()}" != expected_digest:
- return None, None
- preview_truncated = len(preview) > PATCH_PREVIEW_BYTES
- preview_text = preview[:PATCH_PREVIEW_BYTES].decode("utf-8", errors="replace")
- if preview_truncated:
- preview_text = f"{preview_text}\n... patch preview truncated ..."
- return preview_text, {
- "additions": additions,
- "deletions": deletions,
- "fileCount": file_count or min(old_headers, new_headers),
- "previewTruncated": preview_truncated,
- }
+ return remediation_state.patch_artifact_preview(
+ scan_dir, relative_path, expected_digest, open_scan_local_file
+ )
def available_artifact_path(scan_dir: Path, candidate: Path) -> Path | None:
@@ -3401,12 +3368,30 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:
raise SystemExit(73) from None
print(json.dumps(result, allow_nan=False, sort_keys=True))
return
- with closing(
- connect(
- deferred=args.command
- in {"get-scan", "read-scan-usage", "list-scans", "database-info", "update-progress"}
- )
- ) as connection:
+ command_targets = tuple(
+ Path(value).expanduser()
+ for field in ("target_path", "repository")
+ if (value := getattr(args, field, None))
+ )
+ with (
+ closing(
+ connect(
+ deferred=args.command
+ in {
+ "get-scan",
+ "read-scan-usage",
+ "list-scans",
+ "database-info",
+ "update-progress",
+ },
+ discovery_roots=command_targets,
+ )
+ ) as connection,
+ scoped_git_discovery(
+ lambda: [*migration_target_paths(connection), *command_targets],
+ recorded_git_roots=lambda: recorded_git_checkout_roots(connection),
+ ),
+ ):
remediation_state.require_available(connection, args, require_scan)
handlers = {
"create-workspace": create_workspace,
@@ -3423,6 +3408,9 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:
"record-deep-scan-publication-failure": deep_scan.record_deep_scan_publication_failure,
"list-scans": scan_history.list_scans,
"register-cli-scan": partial(register_cli_scan, before_archive=before_archive),
+ "restore-cli-scan-archive": partial(
+ restore_cli_scan_archive, canonical_directory=require_canonical_scan_directory
+ ),
"set-scan-thread": set_scan_thread,
"set-scan-cost-limit": set_scan_cost_limit,
"read-scan-usage": read_scan_usage,
diff --git a/plugins/codex-security/scripts/workbench_feedback.py b/plugins/codex-security/scripts/workbench_feedback.py
index ea5452480..2e4c0e347 100644
--- a/plugins/codex-security/scripts/workbench_feedback.py
+++ b/plugins/codex-security/scripts/workbench_feedback.py
@@ -17,6 +17,8 @@ from workbench_constants import (
FINDING_TITLE_BYTES,
PRIMARY_FINDING_LOCATION_SQL,
)
+from workbench_native_indexes import _indexed_findings, _legacy_generations
+from workbench_target_state import RepositoryIdentityCache
from workbench_validation import bounded_output_text
@@ -26,19 +28,51 @@ def get_scan_feedback(connection: sqlite3.Connection, scan: sqlite3.Row) -> dict
For each finding, include the latest decision only if it closes the finding as a
false positive with a nonblank reason. Prefer the root-control location in its preview.
"""
+ identities = RepositoryIdentityCache(connection)
+ scope = identities.scope_for_scan(scan)
+ if not scope.available:
+ return {"scanId": scan["id"], "targetId": scan["target_id"], "falsePositives": []}
+
+ legacy_generations = _legacy_generations(connection, identities)
+ indexed_findings = {
+ occurrence_id: finding
+ for finding in _indexed_findings(
+ connection,
+ identities=identities,
+ scan_scope=scope,
+ legacy_generations=legacy_generations,
+ )
+ for occurrence_id in finding["matched_occurrence_ids"]
+ }
+ source_filter, source_values = scope.sql(
+ "source_scans", supports_generation=identities.supports_generation
+ )
+ source_generation = (
+ "source_scans.repository_generation" if identities.supports_generation else "NULL"
+ )
+ legacy_generation = legacy_generations.get(scope.target_id)
+ decision_generation = (
+ f"COALESCE({source_generation}, ?)" if legacy_generation is not None else source_generation
+ )
+ decision_values = (
+ (legacy_generation, legacy_generation) if legacy_generation is not None else ()
+ )
rows = connection.execute(
f"""
WITH ranked_decisions AS (
- SELECT findings.id AS finding_id, findings.fingerprint, findings.rule_id,
+ SELECT occurrences.id AS occurrence_id, findings.id AS finding_id, findings.fingerprint, findings.rule_id,
findings.identity_anchor, findings.identity_instance, occurrences.title,
occurrences.summary, COALESCE(triage.status, 'open') AS triage_status,
triage.close_reason, triage.note,
COALESCE(triage.updated_at, source_scans.completed_at) AS updated_at,
source_scans.id AS source_scan_id,
+ source_scans.target_id,
+ {source_generation} AS repository_generation,
source_scans.completed_at AS source_completed_at,
locations.relative_path, locations.start_line, locations.end_line, locations.role,
ROW_NUMBER() OVER (
- PARTITION BY findings.id
+ PARTITION BY findings.id, {decision_generation},
+ CASE WHEN {decision_generation} IS NULL THEN source_scans.target_id END
ORDER BY
julianday(upper(COALESCE(triage.updated_at, source_scans.completed_at))) DESC,
julianday(upper(source_scans.completed_at)) DESC,
@@ -51,7 +85,7 @@ def get_scan_feedback(connection: sqlite3.Connection, scan: sqlite3.Row) -> dict
JOIN finding_locations AS locations ON locations.id = (
SELECT id {PRIMARY_FINDING_LOCATION_SQL}
)
- WHERE source_scans.target_id = ?
+ WHERE {source_filter}
AND source_scans.id != ?
AND source_scans.status = 'complete'
)
@@ -66,10 +100,21 @@ def get_scan_feedback(connection: sqlite3.Connection, scan: sqlite3.Row) -> dict
julianday(upper(source_completed_at)) DESC,
source_scan_id DESC, finding_id DESC
""",
- (scan["target_id"], scan["id"]),
+ (*decision_values, *source_values, scan["id"]),
)
false_positives = []
+ reviewed_components: set[str] = set()
for row in rows:
+ finding = indexed_findings.get(row["occurrence_id"])
+ if (
+ finding is None
+ or finding["status"] != "closed"
+ or finding["close_reason"] != "false_positive"
+ or finding["occurrence_id"] in reviewed_components
+ ):
+ continue
+ reviewed_components.add(finding["occurrence_id"])
+
identity = {"anchor": row["identity_anchor"]}
if row["identity_instance"] is not None:
identity["instance"] = row["identity_instance"]
diff --git a/plugins/codex-security/scripts/workbench_native_indexes.py b/plugins/codex-security/scripts/workbench_native_indexes.py
index 2bd0c96fa..32a628d95 100644
--- a/plugins/codex-security/scripts/workbench_native_indexes.py
+++ b/plugins/codex-security/scripts/workbench_native_indexes.py
@@ -5,6 +5,7 @@ import os
import sqlite3
import sys
from collections.abc import Iterator
+from functools import cache
from itertools import islice
from pathlib import Path
from typing import Any
@@ -18,6 +19,11 @@ from workbench_constants import (
FINDINGS_PAGE_MAX,
PRIMARY_FINDING_LOCATION_SQL,
)
+from workbench_target_state import (
+ RepositoryIdentityCache,
+ RepositoryScanScope,
+ scan_repository_group,
+)
from workbench_validation import bounded_output_text, timestamp_key
@@ -28,11 +34,22 @@ def list_global_findings(
"""Return a page of indexed findings after applying the requested filters, with nextOffset for continuation."""
limit = min(args.limit, FINDINGS_PAGE_MAX)
query = args.query.strip().casefold() if args.query else ""
+ identities = RepositoryIdentityCache(connection)
+ repository = getattr(args, "repository", None)
+ requested = None
+ if repository is not None:
+ repository = str(Path(repository).expanduser().absolute())
+ requested = identities.for_path(repository)
+ scan_scope = identities.scope_for_path(repository)
+ else:
+ scan_scope = None if args.target_id is None else identities.scope(args.target_id)
+ target = identities.targets.get(args.target_id)
+ if identities.supports_identity and target is not None:
+ requested = identities.for_row(target)
findings = (
row
- for row in _indexed_findings(connection)
- if (args.target_id is None or row["target_id"] == args.target_id)
- and (args.severity is None or row["severity"] == args.severity)
+ for row in _indexed_findings(connection, identities=identities, scan_scope=scan_scope)
+ if (args.severity is None or row["severity"] == args.severity)
and (args.status is None or row["status"] == args.status)
and (
not query
@@ -77,41 +94,149 @@ def list_global_findings(
"limit": limit,
"nextOffset": args.offset + limit if has_more else None,
"offset": args.offset,
+ "projectionAvailable": requested is None or requested.ownership_matches,
}
-def _indexed_findings(connection: sqlite3.Connection) -> Iterator[dict[str, Any]]:
- parents: dict[tuple[str, str], tuple[str, str]] = {}
+def _legacy_generations(
+ connection: sqlite3.Connection, identities: RepositoryIdentityCache
+) -> dict[str, str]:
+ return (
+ {
+ row["target_id"]: row["generation"]
+ for row in connection.execute(
+ "SELECT target_id, MIN(repository_generation) AS generation FROM scans "
+ "WHERE repository_generation IS NOT NULL GROUP BY target_id "
+ "HAVING COUNT(DISTINCT repository_generation) = 1"
+ )
+ }
+ if identities.supports_generation
+ else {}
+ )
+
+
+def _indexed_findings(
+ connection: sqlite3.Connection,
+ *,
+ identities: RepositoryIdentityCache | None = None,
+ scan_scope: RepositoryScanScope | None = None,
+ legacy_generations: dict[str, str] | None = None,
+) -> Iterator[dict[str, Any]]:
+ identities = identities or RepositoryIdentityCache(connection)
+
+ def scope_sql(alias: str) -> tuple[str, tuple[str, ...]]:
+ return (
+ scan_scope.sql(alias, supports_generation=identities.supports_generation)
+ if scan_scope is not None
+ else ("1", ())
+ )
+
+ def generation_sql(alias: str) -> str:
+ return f"{alias}.repository_generation" if identities.supports_generation else "NULL"
+
+ if legacy_generations is None:
+ legacy_generations = _legacy_generations(connection, identities)
+
+ def repository_group(scan: sqlite3.Row | dict) -> tuple[str, str]:
+ identity = scan_repository_group(scan)
+ generation = legacy_generations.get(scan["target_id"])
+ return (
+ ("repository", generation)
+ if identity[0] == "target" and generation is not None
+ else identity
+ )
+
+ target_filter, target_values = scope_sql("scans")
+ before_filter, before_values = scope_sql("before_scans")
+ after_filter, after_values = scope_sql("after_scans")
+ parents: dict[tuple[tuple[str, str], str], tuple[tuple[str, str], str]] = {}
- def group(identity: tuple[str, str]) -> tuple[str, str]:
+ def group(identity: tuple[tuple[str, str], str]) -> tuple[tuple[str, str], str]:
while identity in parents:
identity = parents[identity]
return identity
for match in connection.execute(
- """
- SELECT before_scans.target_id, before.finding_id AS before_finding_id,
+ f"""
+ SELECT before_scans.target_id AS before_target_id,
+ after_scans.target_id AS after_target_id,
+ {generation_sql("before_scans")} AS before_generation,
+ {generation_sql("after_scans")} AS after_generation,
+ before.finding_id AS before_finding_id,
after.finding_id AS after_finding_id
FROM scan_comparison_matches AS matches
JOIN finding_occurrences AS before ON before.id = matches.before_occurrence_id
JOIN scans AS before_scans ON before_scans.id = before.scan_id
+ JOIN security_targets AS before_targets ON before_targets.id = before_scans.target_id
JOIN finding_occurrences AS after ON after.id = matches.after_occurrence_id
JOIN scans AS after_scans ON after_scans.id = after.scan_id
- WHERE before_scans.target_id = after_scans.target_id
- """
+ JOIN security_targets AS after_targets ON after_targets.id = after_scans.target_id
+ WHERE {before_filter} AND {after_filter}
+ """,
+ (*before_values, *after_values),
):
- before = group((match["target_id"], match["before_finding_id"]))
- after = group((match["target_id"], match["after_finding_id"]))
+ if match["before_target_id"] != match["after_target_id"] and (
+ match["before_generation"] is None or match["after_generation"] is None
+ ):
+ continue
+ before_repository = repository_group(
+ {
+ "target_id": match["before_target_id"],
+ "repository_generation": match["before_generation"],
+ }
+ )
+ after_repository = repository_group(
+ {
+ "target_id": match["after_target_id"],
+ "repository_generation": match["after_generation"],
+ }
+ )
+ if before_repository != after_repository and (
+ match["before_target_id"] != match["after_target_id"]
+ or match["before_generation"] is not None
+ and match["after_generation"] is not None
+ ):
+ continue
+ before = group(
+ (
+ before_repository,
+ match["before_finding_id"],
+ )
+ )
+ after = group(
+ (
+ after_repository,
+ match["after_finding_id"],
+ )
+ )
if before != after:
parents[after] = before
- latest_scan_by_target = dict(
+ completion_column = (
+ "scans.completion_sequence" if "completion_sequence" in identities.scan_columns else "NULL"
+ )
+ completed_at_column = (
+ "scans.completed_at" if "completed_at" in identities.scan_columns else "NULL"
+ )
+ completed_scans = sorted(
connection.execute(
- "SELECT target_id, id FROM scans WHERE status = 'complete' ORDER BY julianday(upper(started_at)), id"
- )
+ f"""
+ SELECT scans.target_id, scans.id, scans.started_at,
+ {generation_sql("scans")} AS repository_generation,
+ {completed_at_column} AS completed_at,
+ {completion_column} AS completion_sequence
+ FROM scans
+ JOIN security_targets AS targets ON targets.id = scans.target_id
+ WHERE scans.status = 'complete' AND {target_filter}
+ """,
+ target_values,
+ ),
+ key=scan_history._scan_completion_order,
)
+ latest_scan_by_repository = {repository_group(row): row for row in completed_scans}
+ selected_latest_scan_id = completed_scans[-1]["id"] if completed_scans else None
- grouped: dict[tuple[str, str], list[sqlite3.Row]] = {}
+ grouped: dict[tuple[tuple[str, str], str], list[sqlite3.Row]] = {}
for row in connection.execute(
f"""
SELECT
@@ -122,6 +247,7 @@ def _indexed_findings(connection: sqlite3.Connection) -> Iterator[dict[str, Any]
scans.id AS scan_id,
scans.started_at AS scan_started_at,
scans.target_id,
+ {generation_sql("scans")} AS repository_generation,
targets.current_path AS target_path,
scans.scope,
scans.updated_at,
@@ -135,9 +261,36 @@ def _indexed_findings(connection: sqlite3.Connection) -> Iterator[dict[str, Any]
JOIN scans ON scans.id = occurrences.scan_id
JOIN security_targets AS targets ON targets.id = scans.target_id
LEFT JOIN finding_triage AS triage ON triage.occurrence_id = occurrences.id
+ WHERE {target_filter}
""",
+ target_values,
):
- grouped.setdefault(group((row["target_id"], row["finding_id"])), []).append(row)
+ grouped.setdefault(
+ group(
+ (
+ repository_group(row),
+ row["finding_id"],
+ )
+ ),
+ [],
+ ).append(row)
+
+ # Global comparisons do not bind legacy history across current targets. A scoped
+ # projection already admits only its own target's legacy rows and decisions.
+ for identity, occurrences in list(grouped.items()):
+ current_targets = {
+ row["target_id"] for row in occurrences if row["repository_generation"] is not None
+ }
+ if len(current_targets) <= 1 or scan_scope is not None:
+ continue
+ current = []
+ for row in occurrences:
+ if row["repository_generation"] is None:
+ legacy_identity = (scan_repository_group(row), identity[1])
+ grouped.setdefault(legacy_identity, []).append(row)
+ else:
+ current.append(row)
+ grouped[identity] = current
findings = []
for occurrences in grouped.values():
@@ -161,16 +314,33 @@ def _indexed_findings(connection: sqlite3.Connection) -> Iterator[dict[str, Any]
{(row["scan_started_at"], row["scan_id"]) for row in occurrences},
key=lambda scan: (timestamp_key(scan[0]), scan[1]),
)
+ component_latest_scan = max(
+ (
+ latest_scan_by_repository[repository]
+ for repository in {repository_group(row) for row in occurrences}
+ if repository in latest_scan_by_repository
+ ),
+ key=scan_history._scan_completion_order,
+ default=None,
+ )
+ latest_scan_id = (
+ selected_latest_scan_id
+ if scan_scope is not None
+ else component_latest_scan["id"]
+ if component_latest_scan is not None
+ else None
+ )
findings.append(
{
**dict(latest),
+ "close_reason": decision["close_reason"] if decision is not None else None,
"confirmed_in_latest_scan": any(
- row["scan_id"] == latest_scan_by_target.get(row["target_id"])
- for row in occurrences
+ row["scan_id"] == latest_scan_id for row in occurrences
),
"known_since": scans[0][0],
"known_scan_ids": [scan_id for _, scan_id in scans],
"matched_finding_ids": sorted({row["finding_id"] for row in occurrences}),
+ "matched_occurrence_ids": sorted({row["occurrence_id"] for row in occurrences}),
"occurrence_count": len(occurrences),
"status": status,
"updated_at": (
@@ -203,6 +373,7 @@ def list_repositories(
) -> dict[str, Any]:
"""Return tracked targets with scan summaries, finding counts, and current checkout availability."""
by_target: dict[str, dict[str, Any]] = {}
+ latest_rows: dict[str, sqlite3.Row] = {}
for row in connection.execute(
f"""
SELECT targets.id AS repository_id, targets.display_name, targets.current_path,
@@ -215,25 +386,50 @@ def list_repositories(
ORDER BY julianday(upper(scans.started_at)) DESC, scans.id DESC, targets.id
"""
).fetchall():
- scan = scan_history.scan_summary(row) if row["id"] is not None else None
target_id = row["repository_id"]
if target_id not in by_target:
by_target[target_id] = {
"displayName": row["display_name"],
- "latestScan": scan,
+ "latestScan": None,
"openFindingsCount": 0,
"scanCount": 0,
"targetId": target_id,
"targetPath": row["current_path"],
}
- if scan is not None:
- by_target[target_id]["scanCount"] += 1
+ if row["id"] is None:
+ continue
+ repository = by_target[target_id]
+ scan = scan_history.scan_summary(row)
+ repository["scanCount"] += 1
+ previous = latest_rows.get(target_id)
+ if previous is None or (
+ previous["status"] == row["status"] == "complete"
+ and scan_history._scan_completion_order(row)
+ > scan_history._scan_completion_order(previous)
+ ):
+ latest_rows[target_id] = row
+ repository["latestScan"] = scan
+
+ identities = RepositoryIdentityCache(connection)
+ legacy_generations = _legacy_generations(connection, identities)
+
+ @cache
+ def open_findings_count(target_id: str) -> int:
+ if not by_target[target_id]["scanCount"]:
+ return 0
+ scope = identities.scope(target_id)
+ if not scope.available:
+ return 0
+ return sum(
+ row["status"] == "open"
+ for row in _indexed_findings(
+ connection,
+ identities=identities,
+ scan_scope=scope,
+ legacy_generations=legacy_generations,
+ )
+ )
- # A writer can publish a target or its first scan after the repository query.
- for finding in _indexed_findings(connection):
- repository = by_target.get(finding["target_id"])
- if finding["status"] == "open" and repository is not None and repository["scanCount"]:
- repository["openFindingsCount"] += 1
repositories = list(by_target.values())
pagination = {}
if args is not None:
@@ -244,12 +440,12 @@ def list_repositories(
if (args.target_id is None or repository["targetId"] == args.target_id)
and (args.status != "scanned" or repository["scanCount"] > 0)
and (args.status != "not_scanned" or repository["scanCount"] == 0)
- and (args.status != "open_findings" or repository["openFindingsCount"] > 0)
and (
not query
or query in repository["displayName"].casefold()
or query in repository["targetPath"].casefold()
)
+ and (args.status != "open_findings" or open_findings_count(repository["targetId"]) > 0)
]
if args.limit is not None or args.offset:
limit = args.limit or FINDINGS_PAGE_MAX
@@ -261,6 +457,7 @@ def list_repositories(
}
repositories = repositories[args.offset : end]
for repository in repositories:
+ repository["openFindingsCount"] = open_findings_count(repository["targetId"])
path = os.path.expanduser(repository["targetPath"])
try:
repository["checkoutAvailable"] = Path(path).is_absolute() and os.path.isdir(
diff --git a/plugins/codex-security/scripts/workbench_remediation.py b/plugins/codex-security/scripts/workbench_remediation.py
index 1e197517a..a651eb90c 100644
--- a/plugins/codex-security/scripts/workbench_remediation.py
+++ b/plugins/codex-security/scripts/workbench_remediation.py
@@ -11,7 +11,7 @@ import re
import sqlite3
import sys
from collections.abc import Callable
-from contextlib import ExitStack, suppress
+from contextlib import AbstractContextManager, ExitStack, suppress
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Any, BinaryIO
@@ -30,7 +30,11 @@ from finalize_scan_contract import (
stage_scan_local_bytes,
)
from posix_scan_local_files import managed_fd
-from workbench_constants import CLAIM_LEASE_SECONDS, DELIVERED_ACTION_LEASE_SECONDS
+from workbench_constants import (
+ CLAIM_LEASE_SECONDS,
+ DELIVERED_ACTION_LEASE_SECONDS,
+ PATCH_PREVIEW_BYTES,
+)
from workbench_target import git_command, git_output
from workbench_validation import optional_text, require_occurrence, require_uuid
@@ -46,6 +50,56 @@ COMMANDS = {
SCAN_STATUS_ERROR = "Remediation is available only for successfully completed scans."
+def patch_artifact_preview(
+ scan_dir: Path,
+ relative_path: str | None,
+ expected_digest: str | None,
+ open_scan_file: Callable[[Path, str], AbstractContextManager[BinaryIO]],
+) -> tuple[str | None, dict[str, int | bool] | None]:
+ if relative_path is None or expected_digest is None:
+ return None, None
+ digest = hashlib.sha256()
+ preview = bytearray()
+ additions = 0
+ deletions = 0
+ file_count = 0
+ old_headers = 0
+ new_headers = 0
+ at_line_start = True
+ try:
+ with open_scan_file(scan_dir, relative_path) as patch:
+ while chunk := patch.readline(1024 * 1024):
+ digest.update(chunk)
+ if len(preview) <= PATCH_PREVIEW_BYTES:
+ preview.extend(chunk[: PATCH_PREVIEW_BYTES + 1 - len(preview)])
+ if at_line_start:
+ if chunk.startswith(b"diff --git "):
+ file_count += 1
+ elif chunk.startswith(b"+++ "):
+ new_headers += 1
+ elif chunk.startswith(b"--- "):
+ old_headers += 1
+ elif chunk.startswith(b"+"):
+ additions += 1
+ elif chunk.startswith(b"-"):
+ deletions += 1
+ at_line_start = chunk.endswith(b"\n")
+ except SystemExit:
+ return None, None
+ if f"sha256:{digest.hexdigest()}" != expected_digest:
+ return None, None
+ preview_truncated = len(preview) > PATCH_PREVIEW_BYTES
+ preview_text = preview[:PATCH_PREVIEW_BYTES].decode("utf-8", errors="replace")
+ if preview_truncated:
+ preview_text = f"{preview_text}\n... patch preview truncated ..."
+ return preview_text, {
+ "additions": additions,
+ "deletions": deletions,
+ "fileCount": file_count or min(old_headers, new_headers),
+ "previewTruncated": preview_truncated,
+ }
+
+
def reverse_reviewed_patch(
target: Path,
checkout: Path,
diff --git a/plugins/codex-security/scripts/workbench_scan_history.py b/plugins/codex-security/scripts/workbench_scan_history.py
index a26a7bb75..1fdeef87b 100644
--- a/plugins/codex-security/scripts/workbench_scan_history.py
+++ b/plugins/codex-security/scripts/workbench_scan_history.py
@@ -8,11 +8,10 @@ import os
import sqlite3
import sys
from collections.abc import Iterable, Iterator
-from functools import cache
+from fractions import Fraction
from itertools import chain
from pathlib import Path, PurePosixPath
from typing import Any, Callable
-from urllib.parse import urlsplit
# Some plugin hosts launch Python with safe-path isolation enabled.
sys.path.insert(0, str(Path(__file__).resolve().parent))
@@ -26,15 +25,20 @@ from finalize_scan_contract import (
from report_projection import SEVERITY_ORDER
from review_state import review_state
from workbench_constants import ARTIFACTS, FINDINGS_PAGE_MAX, PRIMARY_FINDING_LOCATION_SQL
-from workbench_scan_start import scan_target_identity
from workbench_scan_usage import stored_scan_cost_fields
from workbench_source_scopes import public_scan_recipe
from workbench_target import (
- git_output,
require_remediation_target,
require_scan_target_identity,
verified_windows_checkout,
)
+from workbench_target_state import (
+ RepositoryIdentityCache,
+ _timestamp_ns,
+ repository_relative_path,
+ same_repository_worktree,
+ scan_repository_generation,
+)
from workbench_validation import timestamp_key
@@ -58,6 +62,26 @@ def scan_recipe(scan: sqlite3.Row) -> dict[str, Any]:
}
+def expected_coverage_mode(scan: sqlite3.Row) -> str:
+ """Return the coverage mode implied by the recorded scope, scan mode, and diff target kind."""
+ if scan["mode"] == "diff":
+ mode = {
+ "commit": "commit",
+ "range": "branch_diff",
+ "working_tree": "working_tree",
+ }.get(scan["diff_target_kind"])
+ if mode is None:
+ raise SystemExit("This migrated diff scan does not have a validated change set.")
+ return mode
+ if scan["scope"] != "." or (
+ "recipe_json" in scan.keys()
+ and scan["recipe_json"] is not None
+ and json.loads(scan["recipe_json"])["target"]["kind"] == "paths"
+ ):
+ return "scoped_path"
+ return "deep_repository" if scan["mode"] == "deep" else "repository"
+
+
def preserve_sealed_completion(
binding: dict[str, Any], manifest: dict[str, Any] | None
) -> dict[str, Any]:
@@ -74,6 +98,8 @@ def preserve_sealed_completion(
def _windows_resume_checkout(scan: sqlite3.Row) -> Path:
+ from workbench_scan_start import scan_target_identity
+
replaced = "Cannot resume: the original checkout is missing or was replaced."
try:
repository = require_remediation_target(scan["target_path"])
@@ -95,6 +121,7 @@ def cli_scan_resume(
scan: sqlite3.Row,
workspace: sqlite3.Row,
) -> dict[str, Any]:
+
scan = scan_with_progress(connection, scan["id"])
if scan["mode"] != "deep" or scan["recipe_json"] is None:
raise SystemExit("Resume requires a Deep Scan with a saved CLI launch recipe.")
@@ -124,6 +151,8 @@ def cli_scan_resume(
except OSError as exc:
raise SystemExit(f"Cannot verify the original Windows checkout: {exc}") from exc
else:
+ from workbench_scan_start import scan_target_identity
+
try:
repository = require_scan_target_identity(scan)
except SystemExit as exc:
@@ -264,59 +293,61 @@ def _windows_path_key(value: str) -> str:
def _same_repository(
+ connection: sqlite3.Connection,
before: sqlite3.Row,
after: sqlite3.Row,
*,
- after_git: Callable[..., str | None] | None = None,
+ identities: RepositoryIdentityCache | None = None,
) -> bool:
- if before["target_id"] is not None and before["target_id"] == after["target_id"]:
- return True
- before_target = Path(before["target_path"])
- after_target = Path(after["target_path"])
- if before_target.resolve() == after_target.resolve():
+ before_stored = scan_repository_generation(before)
+ after_stored = scan_repository_generation(after)
+ if before_stored is not None and before_stored == after_stored:
return True
- common_dir = ("rev-parse", "--path-format=absolute", "--git-common-dir")
- after_git = after_git or git_output
- before_git_dir = git_output(before_target, *common_dir)
+ if before["target_id"] and before["target_id"] == after["target_id"]:
+ return before_stored is None or after_stored is None
+ identities = identities or RepositoryIdentityCache(connection)
+ before_state = identities.for_row(before)
+ after_state = identities.for_row(after)
+ if (
+ not before_state.ownership_matches
+ or not after_state.ownership_matches
+ or before_stored is not None
+ and before_stored != before_state.live_identity
+ or after_stored is not None
+ and after_stored != after_state.live_identity
+ ):
+ return False
if (
- before_git_dir is not None
- and (after_git_dir := after_git(after_target, *common_dir)) is not None
- and Path(before_git_dir).resolve() == Path(after_git_dir).resolve()
+ before_state.resolved_path is not None
+ and before_state.resolved_path == after_state.resolved_path
):
+ return before_stored is None or after_stored is None
+ if before_state.repository is None or after_state.repository is None:
+ if same_repository_worktree(Path(before_state.target_path), Path(after_state.target_path)):
+ return True
+ before_relative = repository_relative_path(Path(before_state.target_path))
+ after_relative = repository_relative_path(Path(after_state.target_path))
+ after_origin = identities.origin(after_state)
+ return (
+ before_relative is not None
+ and before_relative == after_relative
+ and after_origin is not None
+ and identities.origin(before_state) == after_origin
+ )
+ before_identity = before_state.live_identity
+ after_identity = after_state.live_identity
+ if before_identity is not None and before_identity == after_identity:
return True
- origin = ("remote", "get-url", "origin")
- before_origin = _repository_origin(git_output(before_target, *origin))
- return before_origin is not None and before_origin == _repository_origin(
- after_git(after_target, *origin)
+ if before_identity is None or after_identity is None:
+ return False
+ after_origin = identities.origin(after_state)
+ return (
+ after_origin is not None
+ and identities.origin(before_state) == after_origin
+ and before_state.repository.relative_path == after_state.repository.relative_path
)
-def _repository_origin(remote: str | None) -> tuple[str, str] | None:
- if remote is None:
- return None
- if "://" in remote:
- try:
- parsed = urlsplit(remote)
- port = parsed.port
- except ValueError:
- return None
- if parsed.scheme not in {"https", "ssh"} or parsed.hostname is None:
- return None
- if parsed.query or parsed.fragment:
- return None
- host = parsed.hostname
- if port is not None and port != {"https": 443, "ssh": 22}[parsed.scheme]:
- host = f"{host}:{port}"
- path = parsed.path
- else:
- authority, separator, path = remote.partition(":")
- if not separator or "?" in path or "#" in path:
- return None
- host = authority.rsplit("@", 1)[-1]
- path = path.strip("/").removesuffix(".git")
- return (host.lower(), path) if host and path else None
-
-
SCAN_SUMMARY_COLUMNS = """
scans.*,
lifecycle.snapshot AS lifecycle_json,
@@ -413,29 +444,13 @@ def list_scans(
clauses: list[str] = []
values: list[Any] = []
if args is not None and args.repository:
- repository = Path(args.repository).expanduser().resolve()
- requested_repository = connection.execute(
- """
- SELECT COALESCE((SELECT id FROM security_targets WHERE current_path = ?), '') AS target_id,
- ? AS target_path
- """,
- (str(repository), str(repository)),
- ).fetchone()
- requested_git = cache(git_output)
- related_target_ids = [
- target["target_id"]
- for target in connection.execute(
- "SELECT id AS target_id, current_path AS target_path FROM security_targets"
- )
- if _same_repository(target, requested_repository, after_git=requested_git)
- ]
- repository_clauses = ["scans.target_path = ?"]
- values.append(str(repository))
- if related_target_ids:
- placeholders = ", ".join("?" for _ in related_target_ids)
- repository_clauses.append(f"scans.target_id IN ({placeholders})")
- values.extend(related_target_ids)
- clauses.append(f"({' OR '.join(repository_clauses)})")
+ repository = Path(args.repository).expanduser().absolute()
+ identities = RepositoryIdentityCache(connection)
+ clause, scope_values = identities.scope_for_path(str(repository)).sql(
+ supports_generation=identities.supports_generation
+ )
+ clauses.append(clause)
+ values.extend(scope_values)
if args is not None and args.scan_root:
scan_root = str(Path(args.scan_root).expanduser().resolve())
prefix = scan_root.rstrip(os.sep) + os.sep
@@ -503,6 +518,17 @@ def list_scans(
return result
+def _scan_completion_order(scan: sqlite3.Row) -> tuple[int, int | Fraction, str]:
+ sequence = scan["completion_sequence"] if "completion_sequence" in scan.keys() else None
+ if sequence is not None:
+ return (1, sequence, scan["id"])
+ completed_at = scan["completed_at"] if "completed_at" in scan.keys() else None
+ timestamp = _timestamp_ns(completed_at)
+ if timestamp is None:
+ timestamp = _timestamp_ns(scan["started_at"])
+ return (0, timestamp if timestamp is not None else 0, scan["id"])
+
+
_FindingSites = dict[tuple[str, str | None, PurePosixPath, int, int], dict[str, Any] | None]
# Match the first location shown by compactFinding in finding-catalogue.ts.
_COMPARISON_LOCATION_RANK = {"root_control": 0, "expected_control": 1, "concrete_implementation": 1}
@@ -565,50 +591,58 @@ def list_unmatched_scan_pairs(
backfill_finding_details: Callable[[sqlite3.Connection, sqlite3.Row], None],
read_coverage: Callable[[sqlite3.Row], dict[str, Any]],
) -> dict[str, Any]:
- repository = Path(args.repository).expanduser().resolve()
- requested = connection.execute(
- """
- SELECT COALESCE((SELECT id FROM security_targets WHERE current_path = ?), '') AS target_id,
- ? AS target_path
- """,
- (str(repository), str(repository)),
- ).fetchone()
- selected = []
- requested_git = cache(git_output)
- membership: dict[tuple[str | None, str], bool] = {}
- for scan in connection.execute("SELECT * FROM scans WHERE status = 'complete'"):
- target = (scan["target_id"], scan["target_path"])
- if target not in membership:
- membership[target] = _same_repository(scan, requested, after_git=requested_git)
- if membership[target]:
- selected.append(scan)
+ requested_repository = Path(args.repository).expanduser().absolute()
+ repository = requested_repository.resolve()
+ identities = RepositoryIdentityCache(connection)
+ requested = identities.for_path(str(requested_repository))
+ if identities.supports_identity:
+ requested.require_owner()
+ clause, values = identities.scope_for_path(str(requested_repository)).sql(
+ supports_generation=identities.supports_generation
+ )
+ selected = connection.execute(
+ f"SELECT * FROM scans WHERE status = 'complete' AND {clause}",
+ values,
+ ).fetchall()
selected.sort(key=lambda scan: (timestamp_key(scan["started_at"]), scan["id"]))
available = []
- for selected_index, scan in enumerate(selected):
+ for scan in selected:
try:
read_coverage(scan)
except SystemExit:
continue
- available.append((selected_index, scan))
+ available.append(scan)
saved_pairs = {
- (row["before_scan_id"], row["after_scan_id"])
+ frozenset((row["before_scan_id"], row["after_scan_id"]))
for row in connection.execute("SELECT before_scan_id, after_scan_id FROM scan_comparisons")
}
+ focus_scan_id = getattr(args, "after_scan_id", None)
+ if focus_scan_id is not None and not any(scan["id"] == focus_scan_id for scan in selected):
+ raise SystemExit("The scan to match is not a completed scan in this repository.")
+ if focus_scan_id is not None:
+ available.sort(key=_scan_completion_order)
batches = []
skipped = 0
matching_findings: dict[str, list[dict[str, Any]]] = {}
sites: dict[str, _FindingSites] = {}
known_links: list[sqlite3.Row] | None = None
- for index, (selected_index, after) in enumerate(available):
+ for index, after in enumerate(available):
+ if focus_scan_id is not None and after["id"] != focus_scan_id:
+ continue
+ candidates = [
+ before
+ for before in available[:index]
+ if _same_repository(connection, before, after, identities=identities)
+ ]
previous = [
before
- for _, before in available[:index]
- if args.force or (before["id"], after["id"]) not in saved_pairs
+ for before in candidates
+ if args.force or frozenset((before["id"], after["id"])) not in saved_pairs
]
- skipped += index - len(previous)
+ skipped += len(candidates) - len(previous)
if not previous:
continue
if known_links is None:
@@ -625,7 +659,16 @@ def list_unmatched_scan_pairs(
]
known_groups = _known_finding_groups(
known_links,
- {scan["id"] for scan in selected[: selected_index + 1]},
+ {
+ scan["id"]
+ for scan in selected
+ if (
+ _scan_completion_order(scan) <= _scan_completion_order(after)
+ if focus_scan_id is not None
+ else (timestamp_key(scan["started_at"]), scan["id"])
+ <= (timestamp_key(after["started_at"]), after["id"])
+ )
+ },
)
batches.append(
{
@@ -715,7 +758,7 @@ def compare_scans(
raise SystemExit("Select two different scans to compare.")
if before["status"] != "complete" or after["status"] != "complete":
raise SystemExit("Only completed scans can be compared.")
- if not _same_repository(before, after):
+ if not _same_repository(connection, before, after):
raise SystemExit("Semantic scan comparisons require the same repository target.")
cached = connection.execute(
"SELECT result_json FROM scan_comparisons WHERE before_scan_id = ? AND after_scan_id = ?",
@@ -850,14 +893,13 @@ def compare_scans(
for pair in related
]
if include_matching_inputs:
+ identities = RepositoryIdentityCache(connection)
after_key = (timestamp_key(after["started_at"]), after["id"])
known_scan_ids = {
scan["id"]
- for scan in connection.execute(
- "SELECT id, started_at, target_id, target_path FROM scans WHERE status = 'complete'"
- )
+ for scan in connection.execute("SELECT * FROM scans WHERE status = 'complete'")
if (timestamp_key(scan["started_at"]), scan["id"]) <= after_key
- and _same_repository(scan, after)
+ and _same_repository(connection, scan, after, identities=identities)
}
excluded_pairs = {(before["id"], after["id"]), (after["id"], before["id"])}
known_groups = _known_finding_groups(
@@ -896,7 +938,7 @@ def save_scan_comparison(
raise SystemExit("Select two different scans to compare.")
if before["status"] != "complete" or after["status"] != "complete":
raise SystemExit("Only completed scans can be compared.")
- if not _same_repository(before, after):
+ if not _same_repository(connection, before, after):
raise SystemExit("Semantic scan comparisons require the same repository target.")
read_coverage(after)
allowed = {
@@ -977,8 +1019,10 @@ def save_scan_comparison(
with connection:
connection.execute("BEGIN IMMEDIATE")
connection.execute(
- "DELETE FROM scan_comparisons WHERE before_scan_id = ? AND after_scan_id = ?",
- (before["id"], after["id"]),
+ "DELETE FROM scan_comparisons "
+ "WHERE (before_scan_id = ? AND after_scan_id = ?) "
+ "OR (before_scan_id = ? AND after_scan_id = ?)",
+ (before["id"], after["id"], after["id"], before["id"]),
)
connection.execute(
"""
@@ -1140,6 +1184,41 @@ def finding_relations(
return result
+def finding_management_updated_at(connection: sqlite3.Connection, scan_id: str) -> str | None:
+ return connection.execute(
+ """
+ SELECT MAX(updated_at)
+ FROM (
+ SELECT triage.updated_at
+ FROM finding_triage AS triage
+ JOIN finding_occurrences AS occurrences ON occurrences.id = triage.occurrence_id
+ WHERE occurrences.scan_id = ?
+ UNION ALL
+ SELECT remediation.updated_at
+ FROM finding_remediation_attempts AS remediation
+ JOIN finding_occurrences AS occurrences ON occurrences.id = remediation.occurrence_id
+ WHERE occurrences.scan_id = ?
+ )
+ """,
+ (scan_id, scan_id),
+ ).fetchone()[0]
+
+
+def finding_triage_result(connection: sqlite3.Connection, occurrence_id: str) -> dict[str, Any]:
+ row = connection.execute(
+ "SELECT status, close_reason, note, updated_at FROM finding_triage WHERE occurrence_id = ?",
+ (occurrence_id,),
+ ).fetchone()
+ if row is None:
+ return {"status": "open"}
+ return {
+ "closeReason": row["close_reason"],
+ "note": row["note"],
+ "status": row["status"],
+ "updatedAt": row["updated_at"],
+ }
+
+
def finding_matches(
connection: sqlite3.Connection, occurrence_id: str, scan_id: str, started_at: str
) -> tuple[list[dict[str, Any]], str, list[str]]:
diff --git a/plugins/codex-security/scripts/workbench_scan_start.py b/plugins/codex-security/scripts/workbench_scan_start.py
index f6152551e..641c52f70 100644
--- a/plugins/codex-security/scripts/workbench_scan_start.py
+++ b/plugins/codex-security/scripts/workbench_scan_start.py
@@ -304,6 +304,57 @@ def archive_scan(
raise
+def restore_cli_scan_archive(
+ connection: sqlite3.Connection,
+ args: argparse.Namespace,
+ canonical_directory: Callable[[Path], Path],
+) -> dict[str, str]:
+ supplied_scan_dir = Path(args.scan_dir).expanduser().absolute()
+ supplied_archive = Path(args.archived_scan_dir).expanduser().absolute()
+ connection.execute("BEGIN IMMEDIATE")
+ try:
+ scan_dir = supplied_scan_dir.resolve()
+ archived_scan_dir = supplied_archive.resolve()
+ archived_owner = connection.execute(
+ "SELECT id FROM scans WHERE scan_dir = ?", (str(archived_scan_dir),)
+ ).fetchone()
+ previous_scan = connection.execute(
+ "SELECT id FROM scans WHERE scan_dir = ?", (str(scan_dir),)
+ ).fetchone()
+ previous_scan_id = previous_scan["id"] if previous_scan is not None else None
+ if archived_owner is not None:
+ disposition = "already-recorded"
+ elif previous_scan_id != args.previous_scan_id:
+ disposition = "ownership-changed"
+ else:
+ archived_scan_dir = canonical_directory(supplied_archive)
+ if (
+ os.path.normcase(supplied_scan_dir) != os.path.normcase(scan_dir)
+ or archived_scan_dir.parent != scan_dir.parent
+ or not archived_scan_dir.name.startswith(f"{scan_dir.name}.previous-")
+ ):
+ raise SystemExit(
+ "The archived scan must be a previous sibling of the scan directory."
+ )
+ try:
+ supplied_scan_dir.lstat()
+ except FileNotFoundError:
+ pass
+ else:
+ canonical_directory(supplied_scan_dir).rmdir()
+ archived_scan_dir.rename(scan_dir)
+ disposition = "restored"
+ connection.commit()
+ except BaseException:
+ connection.rollback()
+ raise
+ return {
+ "disposition": disposition,
+ "scanDir": str(scan_dir),
+ "archivedScanDir": str(archived_scan_dir),
+ }
+
+
@contextmanager
def register_windows_resume_output(
connection: sqlite3.Connection,
@@ -351,6 +402,7 @@ def insert_running_scan(
scope: str,
diff_target: dict[str, str] | None,
target_identity: tuple[str, str | None, int | str, int | str],
+ repository_generation: str | None,
source_scopes: dict[str, Any],
target_root: Path,
target_summary: str | None,
@@ -381,19 +433,21 @@ def insert_running_scan(
connection.execute(
"""
INSERT INTO scans (
- id, workspace_id, target_id, target_path, target_revision, target_snapshot_digest,
+ id, workspace_id, target_id, repository_generation, target_path,
+ target_revision, target_snapshot_digest,
target_device, target_inode, source_scopes_json, scope, mode, user_context,
deep_scan_owner_thread_id, diff_target_kind, diff_base_revision,
diff_head_revision, diff_content_digest, target_summary, scan_dir, model,
reasoning_effort, status, phase, handoff_status, started_at, created_at, updated_at,
launch_surface, launch_package_version, launch_plugin_version, launch_kind
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
'running', ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
scan_id,
workspace["id"],
workspace["target_id"],
+ repository_generation,
str(target),
*target_identity,
json.dumps(source_scopes, allow_nan=False, separators=(",", ":"), sort_keys=True),
diff --git a/plugins/codex-security/scripts/workbench_schema.py b/plugins/codex-security/scripts/workbench_schema.py
index 2d3aa8c1a..41f63478e 100644
--- a/plugins/codex-security/scripts/workbench_schema.py
+++ b/plugins/codex-security/scripts/workbench_schema.py
@@ -52,6 +52,7 @@ def apply_migrations(
backfill_security_targets: Callable[[sqlite3.Connection], None],
*,
immediate: bool = False,
+ discovery_roots: tuple[Path, ...] = (),
) -> None:
"""Apply missing schema versions and repair supported historical migrations.
@@ -59,9 +60,17 @@ def apply_migrations(
Set immediate to acquire the SQLite write lock up front. The backfill callback
links older scans and workspaces to stable targets when required.
"""
+ from workbench_target import scoped_git_discovery
+
connection.commit()
connection.execute("BEGIN IMMEDIATE" if immediate else "BEGIN")
- with connection:
+ with (
+ connection,
+ scoped_git_discovery(
+ lambda: [*migration_target_paths(connection), *discovery_roots],
+ recorded_git_roots=lambda: recorded_git_checkout_roots(connection),
+ ),
+ ):
connection.execute(
"""
CREATE TABLE IF NOT EXISTS schema_migrations (
@@ -83,8 +92,33 @@ def apply_migrations(
apply_repair_statement(connection, statement.replace("\n", " "))
elif version == 16:
should_backfill_targets = repair_stable_targets_migration(connection)
+ elif version == 67:
+ should_backfill_targets = (
+ repair_repository_identity_migration(connection)
+ or version not in applied
+ or should_backfill_targets
+ )
elif version in applied:
- if version in (2, 12, 13, 26, 28, 31, 32, 42, 47, 50, 54, 55, 58, 59, 60, 61):
+ if version in (
+ 2,
+ 12,
+ 13,
+ 26,
+ 28,
+ 31,
+ 32,
+ 42,
+ 47,
+ 50,
+ 54,
+ 55,
+ 58,
+ 59,
+ 60,
+ 61,
+ 69,
+ 70,
+ ):
repair_schema(connection, version)
elif version == 11:
repair_deep_scan_migration(connection)
@@ -96,6 +130,10 @@ def apply_migrations(
connection.execute(statement)
if version == 38:
migrate_finding_workflow_results(connection)
+ elif version == 68:
+ from workbench_target_state import upgrade_linux_repository_generations
+
+ upgrade_linux_repository_generations(connection)
if version not in applied:
connection.execute(
"INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",
@@ -107,6 +145,65 @@ def apply_migrations(
backfill_security_targets(connection)
+def migration_target_paths(connection: sqlite3.Connection) -> list[Path]:
+ paths: set[Path] = set()
+ for table, column in (
+ ("security_targets", "current_path"),
+ ("workspaces", "target_path"),
+ ("scans", "target_path"),
+ ):
+ columns = {row["name"] for row in connection.execute(f"PRAGMA table_info({table})")}
+ if column in columns:
+ condition = (
+ " AND (target_path_unvalidated = 0 OR target_id IS NOT NULL)"
+ if table == "workspaces" and "target_path_unvalidated" in columns
+ else ""
+ )
+ paths.update(
+ Path(row[0])
+ for row in connection.execute(
+ f"SELECT DISTINCT {column} FROM {table} WHERE {column} IS NOT NULL{condition}"
+ )
+ )
+ return list(paths)
+
+
+def recorded_git_checkout_roots(connection: sqlite3.Connection) -> set[Path] | None:
+ """Return saved boundaries, or None if any historical target lacks one."""
+ target_columns = {
+ row["name"] for row in connection.execute("PRAGMA table_info(security_targets)")
+ }
+ boundaries = (
+ {
+ Path(target): Path(root)
+ for target, root in connection.execute(
+ "SELECT current_path, git_checkout_root FROM security_targets "
+ "WHERE git_checkout_root IS NOT NULL"
+ )
+ }
+ if "git_checkout_root" in target_columns
+ else {}
+ )
+ # Even an unversioned scan may have come from an unborn Git checkout.
+ if any(path not in boundaries for path in migration_target_paths(connection)):
+ return None
+ roots = set(boundaries.values())
+ columns = {row["name"] for row in connection.execute("PRAGMA table_info(workspaces)")}
+ if "target_path_unvalidated" in columns:
+ saved = (
+ "rejected_checkout_roots_json" if "rejected_checkout_roots_json" in columns else "NULL"
+ )
+ for (root,) in connection.execute(
+ f"SELECT DISTINCT value FROM workspaces LEFT JOIN json_each({saved}) "
+ f"WHERE {saved} IS NOT NULL OR "
+ "(target_path IS NOT NULL AND target_path_unvalidated = 1 AND target_id IS NULL)"
+ ):
+ if root is None:
+ return None
+ roots.add(Path(root))
+ return roots
+
+
def normalize_pre_release_migrations(connection: sqlite3.Connection, timestamp: str) -> None:
for query in _REPAIR_PLAN:
actions = connection.execute("\n".join(query), {"history": _REPAIR_HISTORY}).fetchall()
@@ -181,22 +278,131 @@ def repair_stable_targets_migration(connection: sqlite3.Connection) -> bool:
and "target_id" in scan_columns
and existing_objects == {"security_targets", "scans_by_target"}
):
- return False
+ validated_workspace = (
+ "workspace.target_path_unvalidated = 0 AND "
+ if "target_path_unvalidated" in workspace_columns
+ else ""
+ )
+ needs_backfill = bool(
+ connection.execute(
+ f"""
+ SELECT EXISTS (
+ SELECT 1 FROM workspaces AS workspace
+ WHERE ({validated_workspace}workspace.target_id IS NULL
+ AND workspace.target_path IS NOT NULL)
+ OR (
+ workspace.target_id IS NOT NULL
+ AND NOT EXISTS (
+ SELECT 1 FROM security_targets
+ WHERE security_targets.id = workspace.target_id
+ )
+ )
+ ) OR EXISTS (
+ SELECT 1 FROM scans AS scan
+ WHERE scan.target_id IS NULL
+ OR NOT EXISTS (
+ SELECT 1 FROM security_targets
+ WHERE security_targets.id = scan.target_id
+ )
+ )
+ """
+ ).fetchone()[0]
+ )
+ if not needs_backfill:
+ return False
+ else:
+ repair_schema(connection, 16)
- repair_schema(connection, 16)
- connection.execute(
- """
- 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
- )
- """
- )
+ for table in ("workspaces", "scans"):
+ connection.execute(
+ f"""
+ UPDATE {table}
+ SET target_id = NULL
+ WHERE target_id IS NOT NULL
+ AND NOT EXISTS (
+ SELECT 1 FROM security_targets
+ WHERE security_targets.id = {table}.target_id
+ )
+ """
+ )
return True
+def repair_repository_identity_migration(connection: sqlite3.Connection) -> bool:
+ columns = {row["name"] for row in connection.execute("PRAGMA table_info(security_targets)")}
+ index_exists = connection.execute(
+ "SELECT 1 FROM sqlite_master WHERE type = 'index' "
+ "AND name = 'security_targets_by_repository_identity'"
+ ).fetchone()
+ identity_changed = "repository_identity" not in columns or index_exists is None
+ if (
+ "repository_identity" in columns
+ and connection.execute(
+ "SELECT 1 FROM schema_migrations WHERE version = 67 AND name = ?",
+ ("persist repository identities",),
+ ).fetchone()
+ is not None
+ and not any(
+ row["name"] == "repository_generation"
+ for row in connection.execute("PRAGMA table_info(scans)")
+ )
+ ):
+ from workbench_target_state import normalize_pre_release_repository_identities
+
+ normalize_pre_release_repository_identities(connection)
+ migration_sql = next(sql for version, _, sql in MIGRATIONS if version == 67)
+ for statement in sql_statements(migration_sql):
+ if statement.startswith(("ALTER TABLE security_targets", "ALTER TABLE scans")):
+ add_migration_column(connection, statement)
+ continue
+ for prefix in ("CREATE UNIQUE INDEX ", "CREATE INDEX ", "CREATE TRIGGER "):
+ if statement.startswith(prefix):
+ statement = statement.replace(prefix, f"{prefix}IF NOT EXISTS ", 1)
+ break
+ connection.execute(statement)
+
+ from workbench_scan_history import _scan_completion_order
+
+ if connection.execute(
+ "SELECT 1 FROM scans WHERE status = 'complete' AND completion_sequence IS NULL LIMIT 1"
+ ).fetchone():
+ # SQL preserves the recorded sequence. Both projections omit it so the merge
+ # compares each pending row with the timestamp of the next recorded completion.
+ recorded = connection.execute(
+ "SELECT id, started_at, completed_at FROM scans "
+ "WHERE status = 'complete' AND completion_sequence IS NOT NULL "
+ "ORDER BY completion_sequence"
+ ).fetchall()
+ unsequenced = connection.execute(
+ "SELECT id, started_at, completed_at FROM scans "
+ "WHERE status = 'complete' AND completion_sequence IS NULL"
+ ).fetchall()
+ pending = iter(sorted(unsequenced, key=_scan_completion_order))
+ candidate = next(pending, None)
+ completed = []
+ for scan in recorded:
+ while candidate is not None and _scan_completion_order(
+ candidate
+ ) <= _scan_completion_order(scan):
+ completed.append(candidate)
+ candidate = next(pending, None)
+ completed.append(scan)
+ if candidate is not None:
+ completed.append(candidate)
+ completed.extend(pending)
+ connection.execute("UPDATE scans SET completion_sequence = NULL WHERE status = 'complete'")
+ sequence = connection.execute(
+ "SELECT COALESCE(MAX(completion_sequence), 0) FROM scans"
+ ).fetchone()[0]
+ for scan in completed:
+ sequence += 1
+ connection.execute(
+ "UPDATE scans SET completion_sequence = ? WHERE id = ?",
+ (sequence, scan["id"]),
+ )
+ return identity_changed
+
+
def repair_schema(connection: sqlite3.Connection, version: int) -> None:
for statement in _STATEMENTS[version]:
apply_repair_statement(connection, statement)
diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.py
index 6cf171245..9be1a698d 100644
--- a/plugins/codex-security/scripts/workbench_target.py
+++ b/plugins/codex-security/scripts/workbench_target.py
@@ -15,9 +15,10 @@ import subprocess
import sys
import tempfile
import unicodedata
-from collections.abc import Iterator
+from collections.abc import Iterable, Iterator
from concurrent.futures import ThreadPoolExecutor
from contextlib import ExitStack, contextmanager
+from contextvars import ContextVar
from pathlib import Path, PurePosixPath
from typing import Any, BinaryIO, Callable, cast
@@ -29,6 +30,7 @@ from workbench_constants import GIT_REPOSITORY_ENVIRONMENT
SOURCE_METADATA_DIRECTORIES = {".git", ".svn"}
_WINDOWS = os.name == "nt"
+_SCOPED_GIT: ContextVar[Callable[[Path], str] | None] = ContextVar("workbench_git", default=None)
class UnreadableLocalFile(SystemExit):
@@ -207,8 +209,10 @@ def _stream_is_binary(output: BinaryIO, size: int, *, drain: bool = False) -> bo
def _protected_repository_root(target: Path) -> Path:
root = target.resolve()
- if root.is_file():
- root = root.parent
+ return _outer_checkout_root(root.parent if root.is_file() else root)
+
+
+def _outer_checkout_root(root: Path) -> Path:
protected = root
for ancestor in (root, *root.parents):
try:
@@ -219,9 +223,67 @@ def _protected_repository_root(target: Path) -> Path:
return protected
+def rejected_checkout_roots(value: str) -> list[str] | None:
+ # Retain both spellings before a checkout marker or symlink can move.
+ try:
+ lexical = Path(value).expanduser().absolute()
+ canonical = lexical.resolve()
+ lexical_root = _outer_checkout_root(lexical)
+ roots = {
+ lexical,
+ canonical,
+ lexical_root,
+ lexical_root.resolve(),
+ _protected_repository_root(canonical),
+ }
+ return sorted(map(str, roots))
+ except (OSError, RuntimeError):
+ return None
+
+
+def pin_scoped_git(target: Path) -> str | None:
+ scoped = _SCOPED_GIT.get()
+ return scoped(target) if scoped is not None else None
+
+
def trusted_git_executable(protected_root: Path) -> str | None:
"""Validate host-selected Git, or discover Git for a direct plugin invocation."""
configured = os.environ.get("CODEX_SECURITY_GIT")
+ if configured is None:
+ configured = pin_scoped_git(protected_root)
+ return _resolve_git_executable(protected_root, configured)
+
+
+def _historical_repository_roots(target: Path) -> Iterator[Path]:
+ try:
+ yield _protected_repository_root(target)
+ except (OSError, RuntimeError):
+ # Keep checking lexical ancestry when the saved path cannot be resolved.
+ pass
+ # A moved or unavailable descendant still excludes its lexical checkout ancestry.
+ root = Path(os.path.abspath(target))
+ protected = root
+ for ancestor in (root, *root.parents):
+ try:
+ (ancestor / ".git").lstat()
+ except (OSError, RuntimeError):
+ continue
+ protected = ancestor
+ yield protected
+ try:
+ yield protected.resolve()
+ except (OSError, RuntimeError):
+ # The lexical root above still excludes tools at an unavailable checkout.
+ pass
+
+
+def _resolve_git_executable(
+ protected_root: Path,
+ configured: str | None,
+ *,
+ discovery_roots: Iterable[Path] = (),
+ recorded_git_roots: Iterable[Path] | None = (),
+) -> str | None:
windows = sys.platform == "win32"
if configured is None:
names = ("git.exe", "git.com") if windows else ("git",)
@@ -239,7 +301,22 @@ def trusted_git_executable(protected_root: Path) -> str | None:
candidates = iter((candidate,))
try:
- repository = _protected_repository_root(protected_root)
+ repositories = [_protected_repository_root(protected_root)]
+ if configured is None:
+ # A legacy Git target has no trustworthy boundary to exclude from PATH.
+ if recorded_git_roots is None:
+ return None
+ saved_boundaries = tuple(recorded_git_roots)
+ for root in saved_boundaries:
+ repositories.extend(_historical_repository_roots(root))
+ for root in discovery_roots:
+ for repository in _historical_repository_roots(root):
+ if not repository.is_dir() and not any(
+ repository.is_relative_to(boundary) for boundary in saved_boundaries
+ ):
+ # Its former checkout is neither discoverable nor already excluded.
+ return None
+ repositories.append(repository)
except (OSError, RuntimeError):
return None
@@ -264,6 +341,7 @@ def trusted_git_executable(protected_root: Path) -> str | None:
continue
if any(
path == repository or repository in path.parents
+ for repository in repositories
for path in (lexical, invocation, canonical)
):
if configured is not None:
@@ -273,6 +351,39 @@ def trusted_git_executable(protected_root: Path) -> str | None:
return None
+@contextmanager
+def scoped_git_discovery(
+ protected_roots: Callable[[], Iterable[Path]],
+ *,
+ recorded_git_roots: Callable[[], Iterable[Path] | None] = lambda: (),
+) -> Iterator[None]:
+ """Pin ambient Git when an operation first needs to inspect repository targets."""
+ if os.environ.get("CODEX_SECURITY_GIT") is not None:
+ yield
+ return
+ executable: str | None = None
+
+ def resolve_once(target: Path) -> str:
+ nonlocal executable
+ if executable is None:
+ executable = (
+ _resolve_git_executable(
+ Path.cwd(),
+ None,
+ discovery_roots=(*protected_roots(), target),
+ recorded_git_roots=recorded_git_roots(),
+ )
+ or ""
+ )
+ return executable
+
+ token = _SCOPED_GIT.set(resolve_once)
+ try:
+ yield
+ finally:
+ _SCOPED_GIT.reset(token)
+
+
def git_command(
target: Path,
*args: str,
diff --git a/plugins/codex-security/scripts/workbench_target_state.py b/plugins/codex-security/scripts/workbench_target_state.py
index 23daebb95..ebe087466 100644
--- a/plugins/codex-security/scripts/workbench_target_state.py
+++ b/plugins/codex-security/scripts/workbench_target_state.py
@@ -3,10 +3,34 @@
from __future__ import annotations
import argparse
+import ctypes
import hashlib
+import os
+import platform
+import re
import sqlite3
+import stat
+import sys
+from dataclasses import dataclass, replace
from datetime import datetime, timezone
+from decimal import Decimal
+from fractions import Fraction
from pathlib import Path
+from urllib.parse import urlsplit
+
+# Some plugin hosts launch Python with safe-path isolation enabled.
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+from filesystem_identity import (
+ serialize_filesystem_identity,
+ stored_filesystem_identity_matches,
+)
+from workbench_target import (
+ _protected_repository_root,
+ git_bytes,
+ git_output,
+ git_worktree_context,
+ pin_scoped_git,
+)
def stable_target_id(target: Path) -> str:
@@ -15,47 +39,1088 @@ def stable_target_id(target: Path) -> str:
return f"target_sha256_{digest}"
+def repository_relative_path(target: Path) -> str | None:
+ """Return the target's normalized location within its own Git worktree."""
+ worktree = _repository_worktree(target)
+ return worktree[1] if worktree is not None else None
+
+
+def _repository_worktree(target: Path) -> tuple[Path, str] | None:
+ try:
+ return git_worktree_context(target)
+ except SystemExit:
+ return None
+
+
+def _path_from_git_bytes(
+ value: bytes, relative_to: Path, *, strip_line_feed: bool = True
+) -> Path | None:
+ if strip_line_feed and value.endswith(b"\n"):
+ value = value[:-2] if sys.platform == "win32" and value.endswith(b"\r\n") else value[:-1]
+ if not value or b"\0" in value:
+ return None
+ path = Path(os.fsdecode(value))
+ return Path(os.path.realpath(path if path.is_absolute() else relative_to / path))
+
+
+def _git_path(target: Path, *args: str) -> Path | None:
+ value = git_bytes(target, *args)
+ return _path_from_git_bytes(value, target) if value is not None else None
+
+
+def _same_existing_path(first: Path | None, second: Path | None) -> bool:
+ if first is None or second is None:
+ return False
+ try:
+ return first.samefile(second)
+ except OSError:
+ return False
+
+
+def _registered_worktree(target: Path, root: Path, common: Path) -> bool:
+ registered = git_bytes(target, "worktree", "list", "--porcelain", "-z")
+ if registered is not None:
+ return any(
+ os.path.realpath(os.fsdecode(record[len(b"worktree ") :])) == str(root)
+ for record in registered.split(b"\0")
+ if record.startswith(b"worktree ")
+ )
+ # Git before 2.36 emits unquoted newline-delimited paths. Repository-side
+ # ownership records are unambiguous even when a checkout path has newlines.
+ gitdir = _git_path(target, "rev-parse", "--absolute-git-dir")
+ dotgit = root / ".git"
+ try:
+ mode = dotgit.lstat().st_mode
+ if stat.S_ISDIR(mode):
+ return _same_existing_path(gitdir, common) and _same_existing_path(gitdir, dotgit)
+ if not stat.S_ISREG(mode) or gitdir is None or not gitdir.is_dir():
+ return False
+ forward = dotgit.read_bytes()
+ if not forward.startswith(b"gitdir: ") or not _same_existing_path(
+ _path_from_git_bytes(forward[len(b"gitdir: ") :], root), gitdir
+ ):
+ return False
+ if _same_existing_path(gitdir, common):
+ configured = git_bytes(
+ target,
+ "config",
+ "--null",
+ "--show-scope",
+ "--no-includes",
+ "--get",
+ "core.worktree",
+ )
+ fields = configured.split(b"\0") if configured is not None else []
+ if (
+ len(fields) != 3
+ or fields[0] not in (b"local", b"worktree")
+ or not fields[1]
+ or fields[2]
+ ):
+ return False
+ configured_root = _path_from_git_bytes(fields[1], gitdir, strip_line_feed=False)
+ return _same_existing_path(configured_root, root)
+ return _same_existing_path(gitdir.parent, common / "worktrees") and _same_existing_path(
+ _path_from_git_bytes((gitdir / "gitdir").read_bytes(), gitdir), dotgit
+ )
+ except (OSError, ValueError):
+ return False
+
+
+class _LinuxStatxTimestamp(ctypes.Structure):
+ _fields_ = (
+ ("seconds", ctypes.c_int64),
+ ("nanoseconds", ctypes.c_uint32),
+ ("_reserved", ctypes.c_int32),
+ )
+
+
+class _LinuxStatx(ctypes.Structure):
+ # Linux's statx UAPI keeps birth time at offset 0x50 in a 0x100-byte buffer.
+ _fields_ = (
+ ("mask", ctypes.c_uint32),
+ ("_before_birth_time", ctypes.c_ubyte * 76),
+ ("birth_time", _LinuxStatxTimestamp),
+ ("_after_birth_time", ctypes.c_ubyte * 160),
+ )
+
+
+def _linux_repository_birth_time_ns(path: str) -> int | None:
+ if ctypes.sizeof(_LinuxStatx) != 256 or _LinuxStatx.birth_time.offset != 80:
+ return None
+ metadata = _LinuxStatx()
+ birth_time_mask = 0x800
+ arguments = (-100, os.fsencode(path), 0, birth_time_mask, ctypes.byref(metadata))
+ try:
+ libc = ctypes.CDLL(None, use_errno=True)
+ try:
+ statx = libc.statx
+ except AttributeError:
+ # Older musl has the syscall but no statx wrapper. Only use known LP64 ABIs.
+ if ctypes.sizeof(ctypes.c_void_p) != 8 or ctypes.sizeof(ctypes.c_long) != 8:
+ return None
+ number = {"x86_64": 332, "aarch64": 291}.get(platform.machine())
+ if number is None:
+ return None
+ statx = libc.syscall
+ statx.argtypes = (
+ ctypes.c_long,
+ ctypes.c_long,
+ ctypes.c_char_p,
+ ctypes.c_long,
+ ctypes.c_ulong,
+ ctypes.POINTER(_LinuxStatx),
+ )
+ statx.restype = ctypes.c_long
+ status = statx(number, *arguments)
+ else:
+ statx.argtypes = (
+ ctypes.c_int,
+ ctypes.c_char_p,
+ ctypes.c_int,
+ ctypes.c_uint,
+ ctypes.POINTER(_LinuxStatx),
+ )
+ statx.restype = ctypes.c_int
+ status = statx(*arguments)
+ except (AttributeError, OSError):
+ return None
+ birth_time = metadata.birth_time
+ if (
+ status != 0
+ or not metadata.mask & birth_time_mask
+ or not 0 <= birth_time.nanoseconds < 1_000_000_000
+ ):
+ return None
+ birth_time_ns = birth_time.seconds * 1_000_000_000 + birth_time.nanoseconds
+ return birth_time_ns if birth_time_ns > 0 else None
+
+
+def _repository_birth_time_ns(path: str, metadata: os.stat_result) -> int | None:
+ birth_time_ns = getattr(metadata, "st_birthtime_ns", None)
+ if birth_time_ns is not None:
+ return birth_time_ns if birth_time_ns > 0 else None
+ if os.name == "nt":
+ return metadata.st_ctime_ns if metadata.st_ctime_ns > 0 else None
+ birth_time = getattr(metadata, "st_birthtime", None)
+ if birth_time is not None:
+ return int(birth_time * 1_000_000_000) if birth_time > 0 else None
+ if not sys.platform.startswith("linux"):
+ return None
+ return _linux_repository_birth_time_ns(path)
+
+
+@dataclass(frozen=True)
+class GitRepositoryIdentity:
+ value: str
+ relative_path: str
+ common_directory: str
+ device: int | str
+ inode: int | str
+ birth_time_ns: int
+ object_birth_time_ns: int
+ previous_value: str | None = None
+
+ @property
+ def legacy_value(self) -> str:
+ return _identity_digest(
+ f"git-common-dir\0{self.common_directory}\0{self.device}\0{self.inode}\0"
+ f"{self.birth_time_ns}\0{self.relative_path}"
+ )
+
+
+def _identity_digest(material: str, prefix: str = "repository_sha256_") -> str:
+ return f"{prefix}{hashlib.sha256(material.encode(errors='surrogateescape')).hexdigest()}"
+
+
+def _repository_worktree_location(target: Path) -> tuple[Path, str] | None:
+ common_directory = _git_path(target, "rev-parse", "--path-format=absolute", "--git-common-dir")
+ worktree = _repository_worktree(target)
+ if common_directory is None or worktree is None:
+ return None
+ worktree_root, relative = worktree
+ if not _registered_worktree(target, worktree_root, common_directory):
+ return None
+ return common_directory, relative
+
+
+def same_repository_worktree(before: Path, after: Path) -> bool:
+ before_location = _repository_worktree_location(before)
+ after_location = _repository_worktree_location(after)
+ return (
+ before_location is not None
+ and after_location is not None
+ and before_location[1] == after_location[1]
+ and _same_existing_path(before_location[0], after_location[0])
+ )
+
+
+def _repository_identity_details(target: Path | str) -> GitRepositoryIdentity | None:
+ target = Path(target)
+ location = _repository_worktree_location(target)
+ if location is None:
+ return None
+ common_directory, relative = location
+ object_directory = _git_path(
+ target, "rev-parse", "--path-format=absolute", "--git-path", "objects"
+ )
+ if object_directory is None:
+ return None
+ canonical_directory = str(common_directory)
+ try:
+ metadata = common_directory.stat()
+ object_metadata = object_directory.stat()
+ except OSError:
+ return None
+ if not stat.S_ISDIR(metadata.st_mode) or not stat.S_ISDIR(object_metadata.st_mode):
+ return None
+ birth_time_ns = _repository_birth_time_ns(canonical_directory, metadata)
+ object_birth_time_ns = _repository_birth_time_ns(str(object_directory), object_metadata)
+ if birth_time_ns is None or object_birth_time_ns is None:
+ return None
+ device = serialize_filesystem_identity(metadata.st_dev)
+ inode = serialize_filesystem_identity(metadata.st_ino)
+ material = (
+ f"git-generation-v2\0{canonical_directory}\0{device}\0{inode}\0"
+ f"{birth_time_ns}\0{object_directory}\0"
+ f"{serialize_filesystem_identity(object_metadata.st_dev)}\0"
+ f"{serialize_filesystem_identity(object_metadata.st_ino)}\0"
+ f"{object_birth_time_ns}\0{relative}"
+ )
+ previous_value = None
+ value = _identity_digest(material)
+ if sys.platform == "linux":
+ try:
+ common_filesystem = os.statvfs(common_directory).f_fsid
+ object_filesystem = os.statvfs(object_directory).f_fsid
+ except OSError:
+ return None
+ previous_value = value
+ value = _identity_digest(
+ f"git-generation-v3\0{canonical_directory}\0{common_filesystem}\0{inode}\0"
+ f"{birth_time_ns}\0{object_directory}\0{object_filesystem}\0"
+ f"{serialize_filesystem_identity(object_metadata.st_ino)}\0"
+ f"{object_birth_time_ns}\0{relative}",
+ "repository_v3_sha256_",
+ )
+ return GitRepositoryIdentity(
+ value,
+ relative,
+ canonical_directory,
+ device,
+ inode,
+ birth_time_ns,
+ object_birth_time_ns,
+ previous_value,
+ )
+
+
+def repository_identity(target: Path | str) -> str | None:
+ """Identify matching Git worktree targets without storing remote credentials."""
+ identity = _repository_identity_details(target)
+ return identity.value if identity is not None else None
+
+
+def repository_origin(target: Path) -> tuple[str, str] | None:
+ remote = git_output(target, "remote", "get-url", "origin")
+ if remote is None:
+ return None
+ if "://" in remote:
+ try:
+ parsed = urlsplit(remote)
+ port = parsed.port
+ except ValueError:
+ return None
+ if parsed.scheme not in {"https", "ssh"} or parsed.hostname is None:
+ return None
+ if parsed.query or parsed.fragment:
+ return None
+ host = parsed.hostname
+ if port is not None and port != {"https": 443, "ssh": 22}[parsed.scheme]:
+ host = f"{host}:{port}"
+ path = parsed.path
+ else:
+ authority, separator, path = remote.partition(":")
+ if not separator or "?" in path or "#" in path:
+ return None
+ host = authority.rsplit("@", 1)[-1]
+ path = path.strip("/").removesuffix(".git")
+ return (host.lower(), path) if host and path else None
+
+
+def supports_repository_identity(connection: sqlite3.Connection) -> bool:
+ return any(
+ row["name"] == "repository_identity"
+ for row in connection.execute("PRAGMA table_info(security_targets)")
+ )
+
+
+@dataclass(frozen=True)
+class RepositoryTargetState:
+ target_id: str
+ target_path: str
+ stored_identity: str | None
+ resolved_path: str | None = None
+ metadata: os.stat_result | None = None
+ repository: GitRepositoryIdentity | None = None
+ ownership_matches: bool = False
+ strict_owner_matches: bool = False
+ generation_predates_history: bool = False
+ has_historical_scans: bool = False
+ missing: bool = False
+ previous_generation: str | None = None
+
+ @property
+ def live_identity(self) -> str | None:
+ return (
+ self.previous_generation or self.repository.value
+ if self.repository is not None
+ else None
+ )
+
+ @property
+ def verified_identity(self) -> str | None:
+ if not self.ownership_matches or self.repository is None:
+ return None
+ if self.stored_identity is None:
+ # Checkout ownership and repository age cannot identify an older Git generation.
+ return (
+ self.live_identity
+ if self.strict_owner_matches
+ and self.generation_predates_history
+ and not self.has_historical_scans
+ else None
+ )
+ return self.live_identity if self.live_identity == self.stored_identity else None
+
+ def require_owner(self) -> None:
+ if not self.ownership_matches:
+ raise SystemExit(
+ f"The repository checkout at {self.target_path} no longer matches its recorded "
+ "security scan history; refusing to reuse its target."
+ )
+
+
+def _inspect_repository_target(
+ connection: sqlite3.Connection,
+ target_id: str,
+ target_path: str,
+ stored_identity: str | None,
+ *,
+ scan_columns: set[str] | None = None,
+) -> RepositoryTargetState:
+ target = Path(target_path)
+ try:
+ resolved_path = str(target.resolve())
+ except (OSError, RuntimeError):
+ return RepositoryTargetState(target_id, target_path, stored_identity)
+ try:
+ metadata = target.stat()
+ except (FileNotFoundError, NotADirectoryError):
+ return RepositoryTargetState(
+ target_id,
+ target_path,
+ stored_identity,
+ resolved_path=resolved_path,
+ ownership_matches=True,
+ missing=True,
+ )
+ except OSError:
+ return RepositoryTargetState(
+ target_id, target_path, stored_identity, resolved_path=resolved_path
+ )
+ repository = _repository_identity_details(target)
+ if scan_columns is None:
+ scan_columns = {row["name"] for row in connection.execute("PRAGMA table_info(scans)")}
+ historical_scan = False
+ recorded_owner = False
+ malformed_owner = False
+ mismatch = False
+ strict_owner_matches = False
+ generation_predates_history = False
+ ownership_matches = True
+ if {"target_id", "target_path"} <= scan_columns:
+ recorded_generations = (
+ {
+ row[0]
+ for row in connection.execute(
+ "SELECT DISTINCT repository_generation FROM scans "
+ "WHERE (target_id = ? OR target_path = ?) AND repository_generation IS NOT NULL",
+ (target_id, target_path),
+ )
+ }
+ if "repository_generation" in scan_columns
+ else set()
+ )
+ generation_conflict = bool(recorded_generations) and (
+ repository is None
+ or recorded_generations not in ({repository.value}, {repository.previous_value})
+ )
+ if not {"target_device", "target_inode"} <= scan_columns:
+ historical_scan = (
+ connection.execute(
+ "SELECT 1 FROM scans WHERE target_id = ? OR target_path = ? LIMIT 1",
+ (target_id, target_path),
+ ).fetchone()
+ is not None
+ )
+ strict_owner_matches = not historical_scan
+ generation_predates_history = repository is not None and not historical_scan
+ else:
+ strict_owner_matches = True
+ timestamps = (
+ ", started_at, created_at" if {"started_at", "created_at"} <= scan_columns else ""
+ )
+ revisions = ", target_revision" if "target_revision" in scan_columns else ""
+ scans = connection.execute(
+ f"""
+ SELECT target_device, target_inode{timestamps}{revisions} FROM scans
+ WHERE target_id = ? OR target_path = ?
+ """,
+ (target_id, target_path),
+ ).fetchall()
+ git_scans = [
+ scan
+ for scan in scans
+ if "target_revision" not in scan.keys() or scan["target_revision"] != "unversioned"
+ ]
+ generation_predates_history = repository is not None and _repository_predates_history(
+ repository, git_scans
+ )
+ for scan in scans:
+ historical_scan = True
+ device, inode = scan["target_device"], scan["target_inode"]
+ if device is None and inode is None:
+ strict_owner_matches = False
+ continue
+ if device is None or inode is None:
+ malformed_owner = True
+ strict_owner_matches = False
+ continue
+ recorded_owner = True
+ if not stored_filesystem_identity_matches(device, metadata.st_dev):
+ strict_owner_matches = False
+ mismatch = mismatch or sys.platform != "linux"
+ if not stored_filesystem_identity_matches(inode, metadata.st_ino):
+ mismatch = True
+ strict_owner_matches = False
+ stored_matches = repository is not None and (
+ stored_identity
+ in {
+ repository.value,
+ repository.legacy_value,
+ repository.previous_value or repository.value,
+ }
+ or (
+ repository.value.startswith("repository_v3_sha256_")
+ and recorded_generations == {repository.value}
+ and isinstance(stored_identity, str)
+ and re.fullmatch(r"repository_sha256_[0-9a-f]{64}", stored_identity) is not None
+ )
+ )
+ verified_repository = repository is not None and (
+ stored_matches
+ or recorded_generations in ({repository.value}, {repository.previous_value})
+ )
+ ownership_matches = not (
+ malformed_owner
+ or mismatch
+ and (
+ not verified_repository or repository is None or repository.relative_path == "."
+ )
+ or stored_identity is not None
+ and (not stored_matches or historical_scan and not recorded_owner)
+ or stored_identity is None
+ and repository is not None
+ and historical_scan
+ and not verified_repository
+ and not generation_predates_history
+ )
+ ownership_matches = ownership_matches and not generation_conflict
+ previous_generation = None
+ if (
+ ownership_matches
+ and repository is not None
+ and repository.previous_value is not None
+ and supports_repository_identity(connection)
+ and "repository_generation" in scan_columns
+ and connection.execute(
+ "SELECT 1 FROM security_targets WHERE repository_identity = ? "
+ "UNION ALL SELECT 1 FROM scans WHERE repository_generation = ? LIMIT 1",
+ (repository.previous_value, repository.previous_value),
+ ).fetchone()
+ is not None
+ ):
+ previous_generation = repository.previous_value
+ return RepositoryTargetState(
+ target_id,
+ target_path,
+ stored_identity,
+ resolved_path,
+ metadata,
+ repository,
+ ownership_matches,
+ strict_owner_matches,
+ generation_predates_history,
+ historical_scan,
+ previous_generation=previous_generation,
+ )
+
+
+def verified_repository_identity(
+ connection: sqlite3.Connection,
+ target_id: str,
+ target_path: str,
+ *,
+ stored_identity: str | None = None,
+) -> str | None:
+ try:
+ return _inspect_repository_target(
+ connection, target_id, target_path, stored_identity
+ ).verified_identity
+ except SystemExit:
+ # Historical identity enrichment is optional; active registration stays strict.
+ return None
+
+
+def scan_repository_generation(scan: sqlite3.Row | dict) -> str | None:
+ return scan["repository_generation"] if "repository_generation" in scan.keys() else None
+
+
+def scan_repository_group(scan: sqlite3.Row | dict) -> tuple[str, str]:
+ generation = scan_repository_generation(scan)
+ return ("repository", generation) if generation is not None else ("target", scan["target_id"])
+
+
+@dataclass(frozen=True)
+class RepositoryScanScope:
+ """A verified generation and, separately, its exact target's legacy scans."""
+
+ generation: str | None = None
+ target_id: str | None = None
+ exact_target: bool = False
+ available: bool = True
+
+ def sql(
+ self, alias: str = "scans", *, supports_generation: bool = True
+ ) -> tuple[str, tuple[str, ...]]:
+ if not self.available:
+ return "0", ()
+ if self.exact_target:
+ return (f"{alias}.target_id = ?", (self.target_id,)) if self.target_id else ("0", ())
+ clauses, values = [], []
+ if self.generation is not None and supports_generation:
+ clauses.append(f"{alias}.repository_generation = ?")
+ values.append(self.generation)
+ if self.target_id:
+ legacy = f"{alias}.repository_generation IS NULL AND " if supports_generation else ""
+ clauses.append(f"({legacy}{alias}.target_id = ?)")
+ values.append(self.target_id)
+ return (f"({' OR '.join(clauses)})", tuple(values)) if clauses else ("0", ())
+
+ def contains(self, scan: sqlite3.Row | dict) -> bool:
+ if not self.available:
+ return False
+ if self.exact_target:
+ return bool(self.target_id) and scan["target_id"] == self.target_id
+ generation = scan_repository_generation(scan)
+ if generation is not None:
+ return generation == self.generation
+ return bool(self.target_id) and scan["target_id"] == self.target_id
+
+
+class RepositoryIdentityCache:
+ """One request's saved identities and verified live aliases."""
+
+ def __init__(self, connection: sqlite3.Connection) -> None:
+ self.connection = connection
+ self.supports_identity = supports_repository_identity(connection)
+ self.scan_columns = {row["name"] for row in connection.execute("PRAGMA table_info(scans)")}
+ self.supports_generation = "repository_generation" in self.scan_columns
+ identity_column = "repository_identity" if self.supports_identity else "NULL"
+ self.targets = {
+ row["target_id"]: row
+ for row in connection.execute(
+ "SELECT id AS target_id, current_path AS target_path, "
+ f"{identity_column} AS repository_identity FROM security_targets"
+ )
+ }
+ self.targets_by_path = {row["target_path"]: row for row in self.targets.values()}
+ self._states: dict[tuple[str, str, str | None], RepositoryTargetState] = {}
+ self._origins: dict[str, tuple[str, str] | None] = {}
+
+ def for_row(self, row: sqlite3.Row | dict) -> RepositoryTargetState:
+ target = self.targets.get(row["target_id"])
+ stored = (
+ row["repository_identity"]
+ if "repository_identity" in row.keys()
+ else target["repository_identity"]
+ if target is not None
+ else None
+ )
+ key = (
+ row["target_id"] or "",
+ row["target_path"],
+ stored,
+ )
+ if key not in self._states:
+ self._states[key] = _inspect_repository_target(
+ self.connection, *key, scan_columns=self.scan_columns
+ )
+ return self._states[key]
+
+ def for_path(self, target_path: str) -> RepositoryTargetState:
+ path = Path(target_path)
+ if os.name != "nt" and ".." in path.parts:
+ last_parent = len(path.parts) - 1 - path.parts[::-1].index("..")
+ try:
+ # A missing final checkout can retain history. Every component
+ # traversed before a parent segment must still exist.
+ Path(*path.parts[: last_parent + 1]).stat()
+ except (OSError, RuntimeError):
+ return RepositoryTargetState("", target_path, None)
+ row = self.targets_by_path.get(target_path)
+ if row is None:
+ try:
+ canonical = str(Path(target_path).resolve())
+ except (OSError, RuntimeError):
+ canonical = None
+ row = self.targets_by_path.get(canonical)
+ if row is None:
+ row = next(
+ (
+ target
+ for target in self.targets.values()
+ if _same_existing_path(Path(target["target_path"]), Path(target_path))
+ ),
+ None,
+ )
+ return self.for_row(
+ row
+ if row is not None
+ else {
+ "target_id": "",
+ "target_path": target_path,
+ "repository_identity": None,
+ }
+ )
+
+ def scope(self, target_id: str) -> RepositoryScanScope:
+ target = self.targets.get(target_id)
+ return (
+ self._scope_for_state(self.for_row(target))
+ if target is not None
+ else RepositoryScanScope(target_id=target_id)
+ )
+
+ def scope_for_path(self, target_path: str) -> RepositoryScanScope:
+ return self._scope_for_state(self.for_path(target_path))
+
+ def scope_for_scan(self, scan: sqlite3.Row | dict) -> RepositoryScanScope:
+ requested = self.for_row(scan)
+ generation = scan_repository_generation(scan)
+ if not requested.ownership_matches or (
+ generation is not None and generation != requested.live_identity
+ ):
+ return RepositoryScanScope(available=False)
+ return RepositoryScanScope(generation, requested.target_id)
+
+ def _scope_for_state(self, requested: RepositoryTargetState) -> RepositoryScanScope:
+ if not requested.ownership_matches:
+ return RepositoryScanScope(available=False)
+ return RepositoryScanScope(
+ requested.live_identity if not requested.missing else None,
+ requested.target_id or None,
+ exact_target=requested.missing,
+ )
+
+ def origin(self, state: RepositoryTargetState) -> tuple[str, str] | None:
+ if state.target_path not in self._origins:
+ self._origins[state.target_path] = repository_origin(Path(state.target_path))
+ return self._origins[state.target_path]
+
+
+def require_scan_checkout_owner(connection: sqlite3.Connection, scan: sqlite3.Row | dict) -> None:
+ identities = RepositoryIdentityCache(connection)
+ owner = identities.for_row(scan)
+ owner.require_owner()
+ if owner.missing or not identities.scope_for_scan(scan).contains(scan):
+ raise SystemExit("The saved scan no longer matches the selected repository checkout.")
+
+
+def upgrade_linux_repository_generations(connection: sqlite3.Connection) -> None:
+ """Convert only old digests authenticated before their device evidence changes."""
+ if sys.platform != "linux":
+ return
+ targets = connection.execute(
+ "SELECT id, current_path, repository_identity FROM security_targets "
+ "WHERE repository_identity GLOB 'repository_sha256_*' OR EXISTS ("
+ "SELECT 1 FROM scans WHERE (scans.target_id = security_targets.id "
+ "OR scans.target_path = security_targets.current_path) "
+ "AND repository_generation GLOB 'repository_sha256_*')"
+ ).fetchall()
+ for target in targets:
+ try:
+ state = _inspect_repository_target(
+ connection, target["id"], target["current_path"], target["repository_identity"]
+ )
+ except SystemExit:
+ continue
+ _upgrade_repository_generation(connection, state)
+
+
+def _upgrade_repository_generation(
+ connection: sqlite3.Connection, state: RepositoryTargetState
+) -> RepositoryTargetState:
+ identity = state.repository
+ if (
+ identity is None
+ or identity.previous_value is None
+ or state.stored_identity not in (None, identity.value, identity.previous_value)
+ or not state.ownership_matches
+ or not state.strict_owner_matches
+ ):
+ return state
+ connection.execute(
+ "UPDATE security_targets SET repository_identity = ? WHERE repository_identity = ?",
+ (identity.value, identity.previous_value),
+ )
+ connection.execute(
+ "UPDATE scans SET repository_generation = ? WHERE repository_generation = ?",
+ (identity.value, identity.previous_value),
+ )
+ return replace(
+ state,
+ stored_identity=(
+ identity.value
+ if state.stored_identity == identity.previous_value
+ else state.stored_identity
+ ),
+ previous_generation=None,
+ )
+
+
+def _pre_release_repository_identities(identity: GitRepositoryIdentity) -> set[str]:
+ directory = os.path.normcase(identity.common_directory)
+ relative = os.path.normcase(os.fspath(Path(identity.relative_path))).replace(os.sep, "/")
+ prefix = f"git-common-dir\0{directory}\0{identity.device}\0{identity.inode}\0"
+ identities = {_identity_digest(f"{prefix}{relative}")}
+ try:
+ generation = (Path(identity.common_directory) / "description").lstat()
+ except OSError:
+ return identities
+ if stat.S_ISREG(generation.st_mode):
+ identities.add(
+ _identity_digest(
+ f"{prefix}git-description\0"
+ f"{serialize_filesystem_identity(generation.st_dev)}\0"
+ f"{serialize_filesystem_identity(generation.st_ino)}\0"
+ f"{generation.st_ctime_ns}\0{relative}"
+ )
+ )
+ return identities
+
+
+def _timestamp_ns(value: str) -> int | Fraction | None:
+ try:
+ # Python 3.10 requires three or six fractional digits; stored timestamps
+ # can come from newer runtimes that accept any RFC 3339 precision.
+ fraction = re.search(r"\.(\d+)(?:[Zz]|[+-]\d{2}:\d{2})$", value)
+ normalized = value
+ if (
+ fraction is not None
+ and fraction[1].isascii()
+ and re.search(r"[0-9]{2}:[0-9]{2}:[0-9]{2}$", value[: fraction.start()]) is not None
+ ):
+ normalized = (
+ value[: fraction.start(1)]
+ + fraction[1][:6].ljust(6, "0")
+ + value[fraction.end(1) :]
+ )
+ if normalized.endswith(("Z", "z")):
+ normalized = normalized[:-1] + "+00:00"
+ timestamp = datetime.fromisoformat(normalized)
+ except (TypeError, ValueError):
+ return None
+ if timestamp.tzinfo is None:
+ return None
+ delta = timestamp - datetime(1970, 1, 1, tzinfo=timezone.utc)
+ nanoseconds = (delta.days * 86400 + delta.seconds) * 1_000_000_000 + delta.microseconds * 1000
+ # datetime truncates RFC 3339 fractions after six digits; sealed timestamps retain them.
+ if fraction is not None and len(fraction[1]) > 6:
+ return nanoseconds + Fraction(Decimal("0." + fraction[1][6:])) * 1000
+ return nanoseconds
+
+
+def _repository_predates_history(
+ identity: GitRepositoryIdentity,
+ scans: list[sqlite3.Row],
+ *,
+ empty_timestamp: str | None = None,
+) -> bool:
+ if not scans and empty_timestamp is None:
+ return True
+ if any(not {"started_at", "created_at"} <= set(scan.keys()) for scan in scans):
+ return False
+ timestamps = (
+ [_timestamp_ns(scan[column]) for scan in scans for column in ("started_at", "created_at")]
+ if scans
+ else [_timestamp_ns(empty_timestamp)]
+ )
+ repository_birth = max(identity.birth_time_ns, identity.object_birth_time_ns)
+ return all(value is not None for value in timestamps) and repository_birth <= min(timestamps)
+
+
+def normalize_pre_release_repository_identities(connection: sqlite3.Connection) -> None:
+ """Retain only individually established pre-release identity bindings."""
+ if not supports_repository_identity(connection):
+ return
+ identities = RepositoryIdentityCache(connection)
+ targets = connection.execute(
+ "SELECT id AS target_id, current_path AS target_path, created_at, repository_identity "
+ "FROM security_targets WHERE repository_identity IS NOT NULL"
+ ).fetchall()
+ states = {target["target_id"]: identities.for_row(target) for target in targets}
+ anchors = {
+ state.stored_identity: state.repository
+ for state in states.values()
+ if state.repository is not None
+ and state.ownership_matches
+ and state.stored_identity
+ in {
+ state.repository.value,
+ state.repository.legacy_value,
+ state.repository.previous_value or state.repository.value,
+ }
+ }
+ for target in targets:
+ stored = target["repository_identity"]
+ state = states[target["target_id"]]
+ scans = connection.execute(
+ "SELECT started_at, created_at FROM scans WHERE target_id = ? OR target_path = ?",
+ (target["target_id"], target["target_path"]),
+ ).fetchall()
+ anchor = anchors.get(stored)
+ identity = state.repository
+ replacement = None
+ if anchor is not None:
+ if _repository_predates_history(anchor, scans, empty_timestamp=target["created_at"]):
+ replacement = stored
+ elif (
+ identity is not None
+ and state.strict_owner_matches
+ and stored in _pre_release_repository_identities(identity)
+ and _repository_predates_history(identity, scans, empty_timestamp=target["created_at"])
+ ):
+ replacement = identity.legacy_value
+ if replacement != stored:
+ connection.execute(
+ "UPDATE security_targets SET repository_identity = ? "
+ "WHERE id = ? AND repository_identity = ?",
+ (replacement, target["target_id"], stored),
+ )
+
+
+def _bind_unscanned_repository_identity(
+ connection: sqlite3.Connection,
+ target_id: str,
+ target_path: str,
+ identity: str | None,
+ *,
+ previous_identity: str | None = None,
+) -> bool:
+ return (
+ connection.execute(
+ """
+ UPDATE security_targets
+ SET repository_identity = ?
+ WHERE id = ? AND current_path = ? AND repository_identity IS ?
+ AND NOT EXISTS (
+ SELECT 1 FROM scans
+ WHERE scans.target_id = security_targets.id
+ OR scans.target_path = security_targets.current_path
+ )
+ """,
+ (identity, target_id, target_path, previous_identity),
+ ).rowcount
+ == 1
+ )
+
+
+def backfill_repository_identities(connection: sqlite3.Connection) -> None:
+ if not supports_repository_identity(connection):
+ return
+ targets = connection.execute(
+ """
+ SELECT targets.id, targets.current_path
+ FROM security_targets AS targets
+ WHERE targets.repository_identity IS NULL
+ AND NOT EXISTS (
+ SELECT 1 FROM scans
+ WHERE scans.target_id = targets.id OR scans.target_path = targets.current_path
+ )
+ """
+ ).fetchall()
+ for target in targets:
+ identity = verified_repository_identity(
+ connection, str(target["id"]), target["current_path"]
+ )
+ if identity is not None:
+ _bind_unscanned_repository_identity(
+ connection, str(target["id"]), target["current_path"], identity
+ )
+
+
def backfill_security_targets(connection: sqlite3.Connection) -> None:
"""Fill missing target IDs on scans and workspaces in the caller’s transaction."""
+ workspace_columns = {row["name"] for row in connection.execute("PRAGMA table_info(workspaces)")}
+ validated_workspace = (
+ " AND target_path_unvalidated = 0" if "target_path_unvalidated" in workspace_columns else ""
+ )
rows = connection.execute(
- """
- SELECT target_path FROM workspaces WHERE target_path IS NOT NULL
+ f"""
+ SELECT target_path FROM workspaces WHERE target_path IS NOT NULL{validated_workspace}
UNION
SELECT target_path FROM scans
"""
).fetchall()
for row in rows:
target_path = row["target_path"]
- target_id = ensure_security_target(connection, target_path)
+ target_id = ensure_security_target(connection, target_path, verify_ownership=False)
connection.execute(
- "UPDATE workspaces SET target_id = ? WHERE target_path = ? AND target_id IS NULL",
+ "UPDATE workspaces SET target_id = ? WHERE target_path = ? AND target_id IS NULL"
+ + validated_workspace,
(target_id, target_path),
)
connection.execute(
"UPDATE scans SET target_id = ? WHERE target_path = ? AND target_id IS NULL",
(target_id, target_path),
)
+ backfill_repository_identities(connection)
-def ensure_security_target(connection: sqlite3.Connection, target_path: str) -> str:
- """Return the ID already associated with the path or insert a deterministic ID for it."""
- existing = connection.execute(
- "SELECT id FROM security_targets WHERE current_path = ?",
- (target_path,),
- ).fetchone()
- if existing is not None:
- return str(existing["id"])
- target_id = stable_target_id(Path(target_path))
- timestamp = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
- connection.execute(
- """
- INSERT OR IGNORE INTO security_targets (
- id, current_path, display_name, created_at, updated_at
- ) VALUES (?, ?, ?, ?, ?)
- """,
- (target_id, target_path, Path(target_path).name, timestamp, timestamp),
+@dataclass(frozen=True)
+class RegisteredRepositoryTarget:
+ target_id: str
+ repository_generation: str | None
+
+
+def _record_checkout_root(
+ connection: sqlite3.Connection, row: sqlite3.Row, state: RepositoryTargetState, inserted: bool
+) -> None:
+ if "git_checkout_root" not in row.keys():
+ return
+ previous = row["git_checkout_root"]
+ # Migrations and old rows cannot attest to the former boundary of a checkout.
+ if previous is None and (not inserted or state.has_historical_scans):
+ return
+ target = Path(state.target_path)
+ if (
+ state.metadata is None
+ or not stat.S_ISDIR(state.metadata.st_mode)
+ or state.resolved_path != state.target_path
+ ):
+ return
+ try:
+ root = _protected_repository_root(target)
+ except (OSError, RuntimeError):
+ return
+ if previous is None or root in Path(previous).parents:
+ connection.execute(
+ "UPDATE security_targets SET git_checkout_root = ? "
+ "WHERE id = ? AND current_path = ? AND git_checkout_root IS ?",
+ (str(root), state.target_id, state.target_path, previous),
+ )
+
+
+def register_security_target(
+ connection: sqlite3.Connection, target_path: str, *, verify_ownership: bool = True
+) -> RegisteredRepositoryTarget:
+ target_query = "SELECT * FROM security_targets WHERE current_path = ?"
+ existing = connection.execute(target_query, (target_path,)).fetchone()
+ inserted = False
+ if existing is None:
+ if verify_ownership:
+ # Pin ambient Git before the new row temporarily lacks a checkout boundary.
+ pin_scoped_git(Path(target_path))
+ target_id = stable_target_id(Path(target_path))
+ timestamp = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
+ # Leave repository_identity NULL until the guarded binding below succeeds.
+ insert = connection.execute(
+ """
+ INSERT OR IGNORE INTO security_targets (
+ id, current_path, display_name, created_at, updated_at
+ ) VALUES (?, ?, ?, ?, ?)
+ """,
+ (target_id, target_path, Path(target_path).name, timestamp, timestamp),
+ )
+ inserted = insert.rowcount == 1
+ existing = connection.execute(target_query, (target_path,)).fetchone()
+ if existing is None:
+ existing = connection.execute(
+ "SELECT * FROM security_targets WHERE id = ?", (target_id,)
+ ).fetchone()
+ if existing is None or Path(existing["current_path"]) != Path(target_path):
+ raise SystemExit("The repository target changed while it was being registered.")
+ target_path = existing["current_path"]
+ target_id = str(existing["id"])
+ if "repository_identity" not in existing.keys():
+ return RegisteredRepositoryTarget(target_id, None)
+ try:
+ state = _inspect_repository_target(
+ connection, target_id, target_path, existing["repository_identity"]
+ )
+ except SystemExit:
+ if verify_ownership:
+ raise
+ return RegisteredRepositoryTarget(target_id, None)
+ if not state.has_historical_scans and state.live_identity != state.stored_identity:
+ _bind_unscanned_repository_identity(
+ connection,
+ target_id,
+ target_path,
+ state.live_identity,
+ previous_identity=existing["repository_identity"],
+ )
+ existing = connection.execute(target_query, (target_path,)).fetchone()
+ if existing is None:
+ raise SystemExit("The repository target changed while it was being registered.")
+ target_id = str(existing["id"])
+ state = _inspect_repository_target(
+ connection, target_id, target_path, existing["repository_identity"]
+ )
+ state = _upgrade_repository_generation(connection, state)
+ if verify_ownership:
+ state.require_owner()
+ if existing["repository_identity"] is None:
+ identity = state.verified_identity
+ if identity is not None and not _bind_unscanned_repository_identity(
+ connection, target_id, target_path, identity
+ ):
+ # A writer may have registered history or bound this target after inspection.
+ existing = connection.execute(target_query, (target_path,)).fetchone()
+ if existing is None:
+ raise SystemExit("The repository target changed while it was being registered.")
+ target_id = str(existing["id"])
+ state = _inspect_repository_target(
+ connection, target_id, target_path, existing["repository_identity"]
+ )
+ if verify_ownership:
+ state.require_owner()
+ if verify_ownership:
+ _record_checkout_root(connection, existing, state, inserted)
+ return RegisteredRepositoryTarget(
+ target_id, state.live_identity if state.ownership_matches and not state.missing else None
)
- return target_id
+
+
+def ensure_security_target(
+ connection: sqlite3.Connection, target_path: str, *, verify_ownership: bool = True
+) -> str:
+ """Return the stable ID for the registered target and its verified ownership."""
+ return register_security_target(
+ connection, target_path, verify_ownership=verify_ownership
+ ).target_id
if __name__ == "__main__":
diff --git a/plugins/codex-security/shared/workbench-history-repairs.json b/plugins/codex-security/shared/workbench-history-repairs.json
index 3d292e9bc..4de303740 100644
--- a/plugins/codex-security/shared/workbench-history-repairs.json
+++ b/plugins/codex-security/shared/workbench-history-repairs.json
@@ -3,6 +3,23 @@
"WITH input AS (SELECT :history AS history),",
"previews AS (",
" SELECT version, CASE",
+ " -- Prefer the original timestamp when a reader reopened the 52/53 repository preview.",
+ " WHEN version IN (64, 67) AND name = 'persist repository identities'",
+ " AND EXISTS (SELECT 1 FROM schema_migrations WHERE version = 52 AND name = receipt.name)",
+ " THEN 'DELETE FROM schema_migrations'",
+ " WHEN version IN (65, 68) AND name = 'stabilize Linux repository generations'",
+ " AND EXISTS (SELECT 1 FROM schema_migrations WHERE version = 53 AND name = receipt.name)",
+ " THEN 'DELETE FROM schema_migrations'",
+ " WHEN version IN (50, 52, 53, 54, 61, 63, 64) AND name = 'persist repository identities'",
+ " THEN 'UPDATE schema_migrations SET version = 67'",
+ " WHEN version IN (51, 53, 54, 55, 62, 64, 65) AND name = 'stabilize Linux repository generations'",
+ " THEN 'UPDATE schema_migrations SET version = 68'",
+ " WHEN version IN (56, 63, 65, 66) AND name = 'distinguish unvalidated workspace targets'",
+ " THEN 'UPDATE schema_migrations SET version = 69'",
+ " WHEN version IN (64, 66, 67) AND name = 'retain checkout roots for Git discovery'",
+ " THEN 'UPDATE schema_migrations SET version = 70'",
+ " WHEN version IN (67, 68) AND name = 'retain rejected workspace checkout boundaries'",
+ " THEN 'UPDATE schema_migrations SET version = 71'",
" WHEN version IN (48, 50, 63) AND name = 'persist scan log completion boundaries'",
" THEN 'UPDATE schema_migrations SET version = 64'",
" WHEN version IN (58, 59) AND name = 'record classification rubric provenance'",
@@ -13,23 +30,13 @@
" THEN 'UPDATE schema_migrations SET version = 50'",
" WHEN version = 63 AND name = 'retain native investigation time allowances'",
" THEN 'DELETE FROM schema_migrations'",
- " END AS repair FROM schema_migrations",
+ " END AS repair FROM schema_migrations AS receipt",
")",
"SELECT 'sql' AS operation, repair || ' WHERE version = ' || version AS value",
"FROM previews WHERE repair IS NOT NULL",
"-- Vacate checkpoint preview 50 before moving a lower source-scope receipt into it.",
"ORDER BY version DESC;"
],
- [
- "WITH history AS (",
- " SELECT json_extract(value, '$.version') AS version,",
- " json_extract(value, '$.name') AS name FROM json_each(:history)",
- ")",
- "SELECT 'error' AS operation,",
- " 'The Codex Security database has an unsupported source-scope migration history.' AS value",
- "FROM schema_migrations JOIN history USING (version)",
- "WHERE version IN (43, 50) AND schema_migrations.name != history.name;"
- ],
[
"-- mirror and early-moves: relocate numbers before checking profiles.",
"WITH",
@@ -50,9 +57,20 @@
"),",
"moves(position, version, destination, name) AS (",
" VALUES",
- " (0, 33, 40, 'index finding identity and comparison history'),",
- " (1, 25, 26, 'persist scan completion warnings'),",
- " (2, 12, 20, 'phase-specific scan progress')",
+ " (-2, 48, 67, 'persist repository identities'),",
+ " (-1, 49, 68, 'stabilize Linux repository generations'),",
+ " (0, 44, 68, 'stabilize Linux repository generations'),",
+ " (1, 45, 68, 'stabilize Linux repository generations'),",
+ " (2, 30, 67, 'persist repository identities'),",
+ " (3, 31, 67, 'persist repository identities'),",
+ " (4, 33, 67, 'persist repository identities'),",
+ " (5, 40, 67, 'persist repository identities'),",
+ " (6, 42, 67, 'persist repository identities'),",
+ " (7, 43, 67, 'persist repository identities'),",
+ " (8, 44, 67, 'persist repository identities'),",
+ " (9, 33, 40, 'index finding identity and comparison history'),",
+ " (10, 25, 26, 'persist scan completion warnings'),",
+ " (11, 12, 20, 'phase-specific scan progress')",
")",
"SELECT 0 AS position, 'error' AS operation,",
" 'The Codex Security database has an unsupported mirror migration history.' AS value",
@@ -69,6 +87,34 @@
" FROM moves JOIN schema_migrations USING (version, name)",
"ORDER BY position;"
],
+ [
+ "WITH history AS (",
+ " SELECT json_extract(value, '$.version') AS version,",
+ " json_extract(value, '$.name') AS name FROM json_each(:history)",
+ ")",
+ "SELECT 'error' AS operation,",
+ " 'The Codex Security database has an unsupported source-scope migration history.' AS value",
+ "FROM schema_migrations JOIN history USING (version)",
+ "WHERE version IN (43, 50) AND schema_migrations.name != history.name;"
+ ],
+ [
+ "-- A released reader can add its columns while a repository preview still owns the version.",
+ "WITH released(version, table_name, column_name) AS (",
+ " VALUES (54, 'workspaces', 'deep_scan_config_json'),",
+ " (55, 'deep_scan_runs', 'pending_target_project_path'),",
+ " (58, 'deep_scan_runs', 'terminal_reviewed_surface_count'),",
+ " (59, 'deep_scan_runs', 'service_tier'),",
+ " (61, 'scans', 'launch_surface')",
+ ")",
+ "SELECT 'timestamp' AS operation,",
+ " 'INSERT INTO schema_migrations (version, name, applied_at) VALUES (' ||",
+ " released.version || ', ' || quote(json_extract(history.value, '$.name')) || ', ?)' AS value",
+ "FROM released JOIN json_each(:history) AS history",
+ " ON json_extract(history.value, '$.version') = released.version",
+ "WHERE EXISTS (SELECT 1 FROM schema_migrations WHERE name = 'persist repository identities')",
+ " AND NOT EXISTS (SELECT 1 FROM schema_migrations WHERE version = released.version)",
+ " AND EXISTS (SELECT 1 FROM pragma_table_info(released.table_name) WHERE name = released.column_name);"
+ ],
[
"-- A main-branch reader may have added discovery context under the old checkpoint number.",
"SELECT 'timestamp' AS operation,",
diff --git a/plugins/codex-security/shared/workbench-migrations.json b/plugins/codex-security/shared/workbench-migrations.json
index a3bf99381..1db8b4ce9 100644
--- a/plugins/codex-security/shared/workbench-migrations.json
+++ b/plugins/codex-security/shared/workbench-migrations.json
@@ -536,5 +536,46 @@
"statements": [
"ALTER TABLE scans ADD COLUMN windows_resume_target_identity TEXT;"
]
+ },
+ {
+ "version": 67,
+ "name": "persist repository identities",
+ "statements": [
+ "ALTER TABLE security_targets\n ADD COLUMN repository_identity TEXT;",
+ "CREATE INDEX security_targets_by_repository_identity\n ON security_targets(repository_identity);",
+ "ALTER TABLE scans\n ADD COLUMN repository_generation TEXT;",
+ "CREATE INDEX scans_by_repository_generation\n ON scans(repository_generation);",
+ "CREATE INDEX scans_by_target_path\n ON scans(target_path);",
+ "ALTER TABLE scans\n ADD COLUMN completion_sequence INTEGER CHECK (completion_sequence >= 1);",
+ "CREATE UNIQUE INDEX scans_completion_sequence\n ON scans(completion_sequence);",
+ "CREATE TRIGGER scans_assign_inserted_completion_sequence\n AFTER INSERT ON scans\n WHEN NEW.status = 'complete' AND NEW.completion_sequence IS NULL\n BEGIN\n UPDATE scans\n SET completion_sequence = (\n SELECT COALESCE(MAX(completion_sequence), 0) + 1 FROM scans\n )\n WHERE id = NEW.id AND completion_sequence IS NULL;\n END;",
+ "CREATE TRIGGER scans_assign_updated_completion_sequence\n AFTER UPDATE OF status ON scans\n WHEN NEW.status = 'complete' AND NEW.completion_sequence IS NULL\n BEGIN\n UPDATE scans\n SET completion_sequence = (\n SELECT COALESCE(MAX(completion_sequence), 0) + 1 FROM scans\n )\n WHERE id = NEW.id AND completion_sequence IS NULL;\n END;"
+ ]
+ },
+ {
+ "version": 68,
+ "name": "stabilize Linux repository generations",
+ "statements": []
+ },
+ {
+ "version": 69,
+ "name": "distinguish unvalidated workspace targets",
+ "statements": [
+ "ALTER TABLE workspaces\n ADD COLUMN target_path_unvalidated INTEGER NOT NULL DEFAULT 0\n CHECK (target_path_unvalidated IN (0, 1));"
+ ]
+ },
+ {
+ "version": 70,
+ "name": "retain checkout roots for Git discovery",
+ "statements": [
+ "ALTER TABLE security_targets ADD COLUMN git_checkout_root TEXT;"
+ ]
+ },
+ {
+ "version": 71,
+ "name": "retain rejected workspace checkout boundaries",
+ "statements": [
+ "ALTER TABLE workspaces ADD COLUMN rejected_checkout_roots_json TEXT;"
+ ]
}
]
diff --git a/plugins/codex-security/tests/test_checkpoint_schema.py b/plugins/codex-security/tests/test_checkpoint_schema.py
index c66456c2d..95a6969ac 100644
--- a/plugins/codex-security/tests/test_checkpoint_schema.py
+++ b/plugins/codex-security/tests/test_checkpoint_schema.py
@@ -70,16 +70,21 @@ def test_frozen_checkpoint_head_migration_preserves_scan_state(
)
if main_reader:
connection.execute(next(sql for version, _, sql in migrations if version == 47))
+ connection.execute(
+ "INSERT INTO security_targets (id, current_path, display_name, created_at, updated_at) "
+ "VALUES ('saved-target', 'target', 'Target', ?, ?)",
+ (timestamp, timestamp),
+ )
connection.execute(
"INSERT INTO workspaces (id, created_at, updated_at) VALUES (?, ?, ?)",
("workspace", timestamp, timestamp),
)
for status in ("running", "failed"):
connection.execute(
- "INSERT INTO scans (id, workspace_id, target_path, target_revision, scope, "
+ "INSERT INTO scans (id, workspace_id, target_id, target_path, target_revision, scope, "
"mode, scan_dir, status, phase, started_at, created_at, updated_at, "
"failure_message, retained_source_digests_json) "
- "VALUES (?, 'workspace', 'target', 'revision', '.', 'deep', ?, ?, "
+ "VALUES (?, 'workspace', 'saved-target', 'target', 'revision', '.', 'deep', ?, ?, "
"'discovery', ?, ?, ?, ?, ?)",
(
status,
diff --git a/plugins/codex-security/tests/test_deep_security_scan_concurrency_guard.py b/plugins/codex-security/tests/test_deep_security_scan_concurrency_guard.py
index 89beb9f4a..33ea81a7e 100644
--- a/plugins/codex-security/tests/test_deep_security_scan_concurrency_guard.py
+++ b/plugins/codex-security/tests/test_deep_security_scan_concurrency_guard.py
@@ -23,10 +23,11 @@ def test_scan_context_reports_only_other_running_deep_scans(
workspace_id = str(uuid.uuid4())
now = workbench_api["now"]()
with workbench_db:
+ target_id = workbench_api["ensure_security_target"](workbench_db, str(target))
workbench_db.execute(
- "INSERT INTO workspaces (id, target_path, default_mode, submitted, created_at, "
- "updated_at) VALUES (?, ?, ?, 1, ?, ?)",
- (workspace_id, str(target), mode, now, now),
+ "INSERT INTO workspaces (id, target_id, target_path, default_mode, submitted, "
+ "created_at, updated_at) VALUES (?, ?, ?, ?, 1, ?, ?)",
+ (workspace_id, target_id, str(target), mode, now, now),
)
scans[name] = workbench_api["start_scan"](
workbench_db,
diff --git a/plugins/codex-security/tests/test_report_projection.py b/plugins/codex-security/tests/test_report_projection.py
index baf53eedf..74c585f95 100644
--- a/plugins/codex-security/tests/test_report_projection.py
+++ b/plugins/codex-security/tests/test_report_projection.py
@@ -159,24 +159,21 @@ def test_projection_retains_distinct_source_fixes(linked_writeup: bool) -> None:
def test_projection_renders_inline_code_and_section_code_evidence() -> None:
manifest, findings, coverage = canonical_documents()
finding = findings["findings"][0]
- finding["summary"] = (
- "The `environment/add` RPC forwards `environmentId` to "
- "`EnvironmentManager::upsert_environment()`."
- )
+ finding["summary"] = "The `route/set` RPC forwards `routeName` to `RouteTable::set_route()`."
finding["codeEvidence"] = [
{
"id": "runtime-upsert",
"label": "Runtime upsert omits the reserved-ID check",
- "path": "codex-rs/exec-server/src/environment.rs",
- "startLine": 253,
- "endLine": 281,
+ "path": "src/routes/table.rs",
+ "startLine": 25,
+ "endLine": 29,
"language": "rust",
- "code": "self.environments.write().insert(environment_id, environment);",
- "explanation": "The runtime path inserts `local` without reusing the startup check.",
+ "code": "self.routes.insert(route_name, destination);",
+ "explanation": "The runtime path inserts `owner` without reusing the startup check.",
}
]
finding["rootCause"] = {
- "summary": "`local` is reserved, but `upsert_environment()` accepts it.",
+ "summary": "`owner` is reserved, but `set_route()` accepts it.",
"evidenceRefs": ["runtime-upsert"],
}
finding["validation"] = {
@@ -184,19 +181,19 @@ def test_projection_renders_inline_code_and_section_code_evidence() -> None:
"evidenceRefs": ["runtime-upsert"],
}
finding["attackPath"] = {
- "dataflow": {"summary": "`environment/add` -> shared environment map"},
+ "dataflow": {"summary": "`route/set` -> shared route map"},
"evidenceRefs": ["runtime-upsert"],
}
markdown = PROJECTION.generate_report_markdown(manifest, findings, coverage).decode()
- assert "The `environment/add` RPC forwards `environmentId`" in markdown
+ assert "The `route/set` RPC forwards `routeName`" in markdown
assert "#### Root Cause" in markdown
assert "**Runtime upsert omits the reserved-ID check**" in markdown
- assert "`codex-rs/exec-server/src/environment.rs:253-281`" in markdown
+ assert "`src/routes/table.rs:25-29`" in markdown
assert "```rust" in markdown
- assert "self.environments.write().insert(environment_id, environment);" in markdown
- assert "The runtime path inserts `local` without reusing the startup check." in markdown
+ assert "self.routes.insert(route_name, destination);" in markdown
+ assert "The runtime path inserts `owner` without reusing the startup check." in markdown
@pytest.mark.parametrize("reference_key", ["evidenceRefs", "evidence_refs"])
diff --git a/plugins/codex-security/tests/test_windows_scan_local_files.py b/plugins/codex-security/tests/test_windows_scan_local_files.py
index 020601e22..9587161b1 100644
--- a/plugins/codex-security/tests/test_windows_scan_local_files.py
+++ b/plugins/codex-security/tests/test_windows_scan_local_files.py
@@ -9,10 +9,9 @@ import shutil
import sqlite3
import subprocess
import sys
-from concurrent.futures import ThreadPoolExecutor
+from concurrent.futures import FIRST_COMPLETED, Future, ThreadPoolExecutor, wait
from contextlib import ExitStack, contextmanager
from pathlib import Path
-from threading import Event
from unittest.mock import Mock
import pytest
@@ -258,13 +257,18 @@ def test_archive_contention_during_windows_patch_write(
backend = sys.modules["windows_scan_local_files"]
write_all = backend._write_all
rename = Path.rename
- writing, rename_denied = Event(), Event()
+ rename_denied = Future()
+ archive_attempt = None
before_archive = Mock()
def write_while_archiving(handle, data):
+ nonlocal archive_attempt
if data == patch:
- writing.set()
- assert rename_denied.wait(10)
+ archive_attempt = pool.submit(archive)
+ wait((rename_denied, archive_attempt), return_when=FIRST_COMPLETED)
+ if not rename_denied.done():
+ archive_attempt.result()
+ pytest.fail("archive succeeded while the patch was open")
# A different writer can commit before the patch releases its open handles.
with sqlite3.connect(database) as other:
other.execute("BEGIN IMMEDIATE")
@@ -275,12 +279,11 @@ def test_archive_contention_during_windows_patch_write(
try:
return rename(source, destination)
except PermissionError as exc:
- if source == scan_dir and exc.winerror in {5, 32}:
- rename_denied.set()
+ if source == scan_dir and exc.winerror in {5, 32} and not rename_denied.done():
+ rename_denied.set_result(None)
raise
def archive():
- assert writing.wait(10)
target = tmp_path / "target"
registration = argparse.Namespace(
repository=str(target),
@@ -303,14 +306,14 @@ def test_archive_contention_during_windows_patch_write(
monkeypatch.setattr(backend, "_write_all", write_while_archiving)
monkeypatch.setattr(Path, "rename", observe_rename)
with ThreadPoolExecutor(max_workers=1) as pool, sqlite3.connect(database) as connection:
- future = pool.submit(archive)
connection.row_factory = sqlite3.Row
result = workbench_api["set_finding_remediation"](connection, args)
+ assert archive_attempt is not None
if timeout:
- archived = future.result(timeout=10)
+ archived = archive_attempt.result()
else:
with pytest.raises(PermissionError) as denied:
- future.result(timeout=10)
+ archive_attempt.result()
assert denied.value.winerror in {5, 32}
assert not list(scan_dir.parent.glob(f"{scan_dir.name}.previous-*"))
archived = scan_dir
diff --git a/plugins/codex-security/tests/test_windows_workbench_output_permissions.py b/plugins/codex-security/tests/test_windows_workbench_output_permissions.py
index ef22a717c..e34a5de21 100644
--- a/plugins/codex-security/tests/test_windows_workbench_output_permissions.py
+++ b/plugins/codex-security/tests/test_windows_workbench_output_permissions.py
@@ -363,7 +363,14 @@ def test_registration_restores_output_when_checkout_is_replaced_before_commit(
patch.setattr(workbench_db, "scan_scope_file_count", replace_target)
if not full_ids:
patch.setattr(windows_scan_local_files, "_full_directory_id", lambda *args: None)
- with pytest.raises(SystemExit, match="target changed while the scan was starting"):
+ # An archived scan already owns the registered target. Its ownership check
+ # detects the replacement before the final Windows resume pin is acquired.
+ rejection = (
+ "no longer matches its recorded security scan history"
+ if archive
+ else "target changed while the scan was starting"
+ )
+ with pytest.raises(SystemExit, match=rejection):
register(archive=archive, connection=connection)
ids = [row[0] for row in connection.execute("SELECT id FROM scans")]
assert not list(output.parent.glob(f"{output.name}.previous-*"))
diff --git a/plugins/codex-security/tests/test_workbench_archive.py b/plugins/codex-security/tests/test_workbench_archive.py
index e094dd883..118d5563d 100644
--- a/plugins/codex-security/tests/test_workbench_archive.py
+++ b/plugins/codex-security/tests/test_workbench_archive.py
@@ -299,6 +299,8 @@ def test_generate_waits_for_the_archive_transaction(
if sql.startswith("UPDATE scans SET scan_dir"):
moved.set()
assert finish_archive.wait(10)
+ if rollback:
+ raise sqlite3.OperationalError("fixture archive rollback")
return result
def archive():
@@ -313,10 +315,10 @@ def test_generate_waits_for_the_archive_transaction(
recipe_json=recipe(target),
recipe_json_stdin=False,
registration_json_stdin=False,
- parent_scan_id=str(uuid.uuid4()) if rollback else None,
+ parent_scan_id=None,
)
if rollback:
- with pytest.raises(SystemExit, match="not found"):
+ with pytest.raises(sqlite3.OperationalError, match="fixture archive rollback"):
workbench_api["register_cli_scan"](connection, args)
return output
return Path(workbench_api["register_cli_scan"](connection, args)["archivedScanDir"])
diff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.py
index e291b84f6..43322dfa3 100644
--- a/plugins/codex-security/tests/test_workbench_db.py
+++ b/plugins/codex-security/tests/test_workbench_db.py
@@ -6,6 +6,7 @@ import hashlib
import json
import os
import runpy
+import shutil
import sqlite3
import subprocess
import sys
@@ -998,6 +999,9 @@ def test_completed_finding_triage_and_remediation_persist(tmp_path: Path, git_au
state_dir, target = empty_target_paths(tmp_path)
source = target / "source.txt"
source.write_bytes(b"vulnerable\r\n")
+ create_workspace(
+ state_dir, str(uuid.uuid4()), "--target-path", str(tmp_path / "missing-target")
+ )
scan_id, scan_dir = start_saved_scan(state_dir, target, tmp_path / "scans")
write_completed_contract(scan_dir, scan_id, target, relative_path=source.name)
completed = scan_command(state_dir, "complete-scan", scan_id)["scan"]
@@ -1870,9 +1874,10 @@ def test_workspace_keeps_literal_scope_at_length_boundary(tmp_path: Path):
def test_workbench_opens_invalid_target_for_correction(tmp_path: Path) -> None:
state_dir = tmp_path / "state"
missing = tmp_path / "missing"
+ workspace_id = str(uuid.uuid4())
workspace = create_workspace(
state_dir,
- str(uuid.uuid4()),
+ workspace_id,
"--target-path",
str(missing),
"--user-context",
@@ -1883,6 +1888,77 @@ def test_workbench_opens_invalid_target_for_correction(tmp_path: Path) -> None:
assert workspace["setupValidation"]["valid"] is False
assert "readable local directory" in workspace["setupValidation"]["error"]
assert workspace["userContext"] == "Focus on authentication and uploaded archives."
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ connection.execute("DROP INDEX scans_by_target")
+ run_workbench(state_dir, "get-workspace", "--workspace-id", workspace_id)
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ assert connection.execute(
+ "SELECT target_id, target_path_unvalidated FROM workspaces WHERE id = ?",
+ (workspace_id,),
+ ).fetchone() == (None, 1)
+ assert connection.execute("SELECT COUNT(*) FROM security_targets").fetchone() == (0,)
+
+ missing.mkdir()
+ invalid_scope = save_workspace(
+ state_dir, workspace_id, str(missing), "missing-scope", "standard", check=False
+ )
+ assert invalid_scope["returncode"] != 0
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ assert connection.execute(
+ "SELECT target_id, target_path_unvalidated FROM workspaces WHERE id = ?",
+ (workspace_id,),
+ ).fetchone() == (None, 1)
+
+ saved = save_workspace(state_dir, workspace_id, str(missing), ".", "standard")
+ assert saved["setupValidation"]["valid"] is True
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ assert connection.execute(
+ "SELECT target_id, target_path_unvalidated FROM workspaces WHERE id = ?",
+ (workspace_id,),
+ ).fetchone() == (stable_target_id(missing), 0)
+
+
[email protected](os.name == "nt", reason="uses a POSIX executable shim")
[email protected]("relative", [False, True])
[email protected]("legacy", [False, True])
[email protected]("corrected", [False, True])
+def test_rejected_workspace_cannot_supply_git_to_later_history(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, relative: bool, legacy: bool, corrected: bool
+) -> None:
+ state = tmp_path / "state"
+ requested, rejected = tmp_path / "requested", tmp_path / "rejected"
+ initialize_git_repository(requested)
+ initialize_git_repository(rejected)
+ create_workspace(state, str(uuid.uuid4()), "--target-path", str(requested))
+ monkeypatch.chdir(rejected)
+ value = "." if relative else str(rejected / "missing")
+ workspace_id = str(uuid.uuid4())
+ workspace = create_workspace(state, workspace_id, "--target-path", value)
+ assert workspace["targetPath"] == value
+ assert workspace["setupValidation"]["valid"] is False
+
+ if legacy:
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.execute("UPDATE workspaces SET rejected_checkout_roots_json = NULL")
+ (rejected / ".git").rename(tmp_path / "removed-git")
+ monkeypatch.chdir(requested)
+ if corrected:
+ saved = save_workspace(state, workspace_id, str(requested), ".", "standard")
+ assert saved["setupValidation"]["valid"] is True
+ monkeypatch.delenv("CODEX_SECURITY_GIT", raising=False)
+ trusted = tmp_path / "trusted"
+ trusted.mkdir()
+ host_git = shutil.which("git")
+ assert host_git
+ monkeypatch.setenv("WORKBENCH_HOST_GIT", host_git)
+ monkeypatch.setenv("PATH", os.pathsep.join(map(str, (rejected, trusted))))
+ for directory in (rejected, trusted):
+ shim = directory / "git"
+ shim.write_text('#!/bin/sh\nprintf invoked > "$0.ran"\nexec "$WORKBENCH_HOST_GIT" "$@"\n')
+ shim.chmod(0o700)
+ run_workbench(state, "list-scans", "--repository", str(requested))
+ assert not (rejected / "git.ran").exists()
+ assert (trusted / "git.ran").exists() is not legacy
def test_workbench_preserves_long_user_context(tmp_path: Path) -> None:
@@ -1978,15 +2054,26 @@ def test_workbench_replaces_context_only_for_running_owned_scan(tmp_path: Path)
assert "Only a running scan" in str(rejected_terminal["stderr"])
-def test_workbench_marks_invalid_initial_scope_for_correction(tmp_path: Path) -> None:
- state_dir, target = empty_target_paths(tmp_path)
[email protected]("is_git", [False, True])
+def test_workbench_marks_invalid_initial_scope_for_correction(tmp_path: Path, is_git: bool) -> None:
+ state_dir = tmp_path / "state"
+ target = tmp_path / "target"
+ if is_git:
+ initialize_git_repository(target)
+ else:
+ target.mkdir()
workspace = create_workspace(
state_dir, str(uuid.uuid4()), "--target-path", str(target), "--scope", "missing"
)
- assert workspace["targetMetadata"]["isGit"] is False
+ assert workspace["targetMetadata"]["isGit"] is is_git
assert workspace["scope"] == "missing"
assert workspace["setupValidation"]["valid"] is False
assert "existing directory" in workspace["setupValidation"]["error"]
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ assert connection.execute(
+ "SELECT target_id, target_path_unvalidated FROM workspaces WHERE id = ?",
+ (workspace["id"],),
+ ).fetchone() == (stable_target_id(target), 0)
def test_workbench_resolves_structured_diff_target(tmp_path: Path) -> None:
diff --git a/plugins/codex-security/tests/test_workbench_deep_scan.py b/plugins/codex-security/tests/test_workbench_deep_scan.py
index d5d713235..32748e3ff 100644
--- a/plugins/codex-security/tests/test_workbench_deep_scan.py
+++ b/plugins/codex-security/tests/test_workbench_deep_scan.py
@@ -26,6 +26,7 @@ from workbench_test_support import (
finish_deep_scan,
get_deep_scan,
get_scan,
+ load_script,
mark_deep_coordinator_succeeded,
read_fixture_deep_scan,
read_review_state,
@@ -140,6 +141,20 @@ def restore_v53_schema(state_dir: Path) -> None:
connection.execute("ALTER TABLE scans DROP COLUMN launch_plugin_version")
connection.execute("ALTER TABLE scans DROP COLUMN launch_package_version")
connection.execute("ALTER TABLE scans DROP COLUMN launch_surface")
+ connection.executescript("""
+ DROP TRIGGER scans_assign_inserted_completion_sequence;
+ DROP TRIGGER scans_assign_updated_completion_sequence;
+ DROP INDEX scans_completion_sequence;
+ DROP INDEX scans_by_target_path;
+ DROP INDEX scans_by_repository_generation;
+ DROP INDEX security_targets_by_repository_identity;
+ ALTER TABLE scans DROP COLUMN completion_sequence;
+ ALTER TABLE scans DROP COLUMN repository_generation;
+ ALTER TABLE security_targets DROP COLUMN repository_identity;
+ ALTER TABLE security_targets DROP COLUMN git_checkout_root;
+ ALTER TABLE workspaces DROP COLUMN rejected_checkout_roots_json;
+ ALTER TABLE workspaces DROP COLUMN target_path_unvalidated;
+ """)
connection.execute(
"ALTER TABLE scan_severity_classifications DROP COLUMN rubric_binding_json"
)
@@ -476,6 +491,7 @@ def test_existing_generation_safely_claims_and_reclaims_without_schema_migration
with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
schema_version = connection.execute("SELECT MAX(version) FROM schema_migrations").fetchone()
+ assert schema_version == (load_script("workbench_schema").MIGRATIONS[-1][0],)
assert claim()["deepScan"]["coordinatorGeneration"] == 2
assert claim()["coordinatorDisposition"] == "observing"
expire_deep_scan_coordinator(state_dir, scan_id)
diff --git a/plugins/codex-security/tests/test_workbench_feedback.py b/plugins/codex-security/tests/test_workbench_feedback.py
index f5c27821a..2cda4621e 100644
--- a/plugins/codex-security/tests/test_workbench_feedback.py
+++ b/plugins/codex-security/tests/test_workbench_feedback.py
@@ -14,6 +14,7 @@ from test_workbench_completion_binding import _seal_draft, register_cli_scan
from workbench_test_support import (
create_saved_workspace,
get_scan,
+ initialize_git_repository,
run_workbench,
scan_command,
set_triage,
@@ -349,6 +350,63 @@ def test_scan_materializes_all_false_positive_decisions(tmp_path: Path) -> None:
assert json.loads(feedback_path.read_text()) == feedback["falsePositives"]
[email protected]("legacy_first", [False, True])
[email protected]("rediscovered", [False, True])
+def test_sealed_same_target_feedback_keeps_latest_decision_across_upgrade(
+ tmp_path: Path, monkeypatch, legacy_first: bool, rediscovered: bool
+) -> None:
+ import test_workbench_scan_history as producer
+
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ original_write = producer.write_completed_contract
+
+ def write_current_completion(scan_dir, scan_id, target, **values):
+ original_write(scan_dir, scan_id, target, **values)
+ manifest_path = scan_dir / "scan-manifest.json"
+ manifest = json.loads(manifest_path.read_text())
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ started_at = connection.execute(
+ "SELECT started_at FROM scans WHERE id=?", (scan_id,)
+ ).fetchone()[0]
+ manifest["scan"]["startedAt"] = started_at
+ manifest["scan"]["completedAt"] = (
+ datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
+ )
+ manifest_path.write_text(json.dumps(manifest))
+
+ monkeypatch.setattr(producer, "write_completed_contract", write_current_completion)
+ first = producer.create_cli_scan(
+ state, tmp_path / "results", repository, target_revision=revision
+ )
+ finding = run_workbench(state, "get-scan", "--scan-id", first["scanId"])["scan"]["findings"][0]
+ if legacy_first:
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.execute(
+ "UPDATE scans SET repository_generation=NULL WHERE id=?", (first["scanId"],)
+ )
+ _close_finding(state, finding["occurrenceId"], "false_positive", "Synthetic checked guard.")
+ later = producer.create_cli_scan(
+ state, tmp_path / "results", repository, target_revision=revision, finding=rediscovered
+ )
+ if rediscovered:
+ later_finding = run_workbench(state, "get-scan", "--scan-id", later["scanId"])["scan"][
+ "findings"
+ ][0]
+ assert later_finding["findingId"] == finding["findingId"]
+ assert later_finding["triage"]["status"] == "open"
+ current = producer.create_cli_scan(
+ state, tmp_path / "results", repository, target_revision=revision, complete=False
+ )
+ feedback = _feedback(state, current["scanId"])["falsePositives"]
+ if rediscovered:
+ assert feedback == []
+ else:
+ assert len(feedback) == 1
+ assert feedback[0]["sourceScanId"] == first["scanId"]
+
+
def test_later_offset_completion_supersedes_false_positive_feedback(tmp_path: Path) -> None:
state, target = tmp_path / "state", tmp_path / "target"
target.mkdir()
diff --git a/plugins/codex-security/tests/test_workbench_native_index_queries.py b/plugins/codex-security/tests/test_workbench_native_index_queries.py
index 3b0a4a148..812365b6a 100644
--- a/plugins/codex-security/tests/test_workbench_native_index_queries.py
+++ b/plugins/codex-security/tests/test_workbench_native_index_queries.py
@@ -4,8 +4,10 @@ from __future__ import annotations
import ntpath
import sqlite3
+import sys
from argparse import Namespace
from contextlib import closing
+from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock
@@ -122,7 +124,8 @@ def test_global_finding_filters_apply_before_pagination(workbench_api, indexed_c
assert [finding["scanId"] for finding in targeted["findings"]] == [SCAN_IDS[0]]
assert query(connection, query_args(query="needle", severity="low"))["findings"] == []
unfiltered = query(connection, query_args())
- assert set(unfiltered) == {"findings", "limit", "nextOffset", "offset"}
+ assert set(unfiltered) == {"findings", "limit", "nextOffset", "offset", "projectionAvailable"}
+ assert unfiltered["projectionAvailable"] is True
assert len(unfiltered["findings"]) == 3
@@ -363,24 +366,111 @@ def test_scan_root_filter_matches_windows_path_aliases(
def test_scan_list_probes_requested_repository_once(
workbench_api, indexed_collections, monkeypatch
):
+ import workbench_target as target_module
+
connection, targets = indexed_collections
history = workbench_api["scan_history"]
probes = []
+ git_command = target_module.git_command
- def record_git_output(target, *arguments):
+ def record_git_command(target, *arguments, **kwargs):
probes.append((target, arguments))
- if arguments[0] == "rev-parse":
- return str(target / ".git")
- return f"https://example.test/{target.name}.git"
+ return git_command(target, *arguments, **kwargs)
- monkeypatch.setattr(history, "git_output", record_git_output)
+ monkeypatch.setattr(target_module, "git_command", record_git_command)
result = history.list_scans(connection, query_args(repository=str(targets[0]), limit=1))
assert [scan["scanId"] for scan in result["scans"]] == [SCAN_IDS[0]]
assert [arguments for target, arguments in probes if target == targets[0]] == [
("rev-parse", "--path-format=absolute", "--git-common-dir"),
- ("remote", "get-url", "origin"),
+ ("rev-parse", "--show-toplevel"),
]
+ assert all(target == targets[0] for target, _ in probes)
+
+
[email protected]("exact_saved", [False, True])
[email protected]("stale_exact", [False, True])
[email protected]("device_changed", [False, True])
+def test_repository_findings_match_directory_aliases_after_exact_paths(
+ workbench_api,
+ indexed_collections,
+ tmp_path,
+ monkeypatch,
+ exact_saved,
+ stale_exact,
+ device_changed,
+):
+ connection, targets = indexed_collections
+ alias = tmp_path / "alias"
+ alias.symlink_to(targets[0], target_is_directory=True)
+ resolve = Path.resolve
+
+ def preserve_alias(self, *args, **kwargs):
+ return self if self == alias else resolve(self, *args, **kwargs)
+
+ # Exercise preserved case-alias spelling with an actual directory link.
+ monkeypatch.setattr(Path, "resolve", preserve_alias)
+ if exact_saved:
+ connection.execute(
+ "UPDATE security_targets SET current_path = ? WHERE id = ?",
+ (str(alias), stable_target_id(targets[1])),
+ )
+ selected = 1 if exact_saved else 0
+ if stale_exact or device_changed:
+ metadata = targets[0].stat()
+ connection.execute(
+ "UPDATE scans SET target_device = ?, target_inode = ? WHERE id = ?",
+ (
+ str(metadata.st_dev + 1 if device_changed else metadata.st_dev),
+ str(metadata.st_ino + 1 if stale_exact else metadata.st_ino),
+ SCAN_IDS[selected],
+ ),
+ )
+ result = workbench_api["native_indexes"].list_global_findings(
+ connection, query_args(repository=str(alias))
+ )
+ available = not stale_exact and (not device_changed or sys.platform == "linux")
+ assert result["projectionAvailable"] is available
+ assert [item["scanId"] for item in result["findings"]] == (
+ [SCAN_IDS[selected]] if available else []
+ )
+
+
[email protected]("operation", ["list", "matching"])
[email protected]("requested_kind", ["saved_alias", "canonical", "unsaved_alias"])
+def test_requested_alias_keeps_its_own_legacy_scan_history(
+ workbench_api, indexed_collections, tmp_path, operation, requested_kind
+):
+ connection, targets = indexed_collections
+ alias = tmp_path / "saved-alias"
+ alias.symlink_to(targets[0], target_is_directory=True)
+ unsaved_alias = tmp_path / "unsaved-alias"
+ unsaved_alias.symlink_to(targets[0], target_is_directory=True)
+ connection.execute(
+ "UPDATE security_targets SET current_path = ? WHERE id = ?",
+ (str(alias), stable_target_id(targets[1])),
+ )
+ connection.execute("UPDATE scans SET target_path = ? WHERE id = ?", (str(alias), SCAN_IDS[1]))
+ selected, requested = {
+ "saved_alias": (SCAN_IDS[1], alias),
+ "canonical": (SCAN_IDS[0], targets[0]),
+ "unsaved_alias": (SCAN_IDS[0], unsaved_alias),
+ }[requested_kind]
+ history = workbench_api["scan_history"]
+ args = query_args(repository=str(requested), limit=None, force=False, after_scan_id=selected)
+ if operation == "list":
+ assert [scan["scanId"] for scan in history.list_scans(connection, args)["scans"]] == [
+ selected
+ ]
+ else:
+ result = history.list_unmatched_scan_pairs(
+ connection,
+ args,
+ backfill_finding_details=lambda *args: None,
+ read_coverage=lambda *args: {},
+ )
+ assert result["scanCount"] == 1
+ assert result["repository"] == str(targets[0])
@pytest.mark.parametrize("collection", ["scans", "repositories", "findings"])
@@ -531,6 +621,30 @@ def test_scan_start_chronology_keeps_latest_and_first_seen_queries_consistent(
assert list(connection.iterdump()) == before
[email protected]("newer_status", ["complete", "running", "failed"])
+def test_repository_latest_scan_uses_completion_order_without_hiding_active_scans(
+ workbench_api, indexed_collections, newer_status
+):
+ connection, targets = indexed_collections
+ target_id = stable_target_id(targets[0])
+ connection.execute("UPDATE scans SET completion_sequence = completion_sequence + 100")
+ for index, sequence in [(0, 2), (1, 1)]:
+ connection.execute(
+ "UPDATE scans SET target_id = ?, started_at = ?, completion_sequence = ?, status = ? WHERE id = ?",
+ (
+ target_id,
+ f"2026-08-01T00:{index * 10:02d}:00Z",
+ sequence if index == 0 or newer_status == "complete" else None,
+ "complete" if index == 0 else newer_status,
+ SCAN_IDS[index],
+ ),
+ )
+ repository = workbench_api["native_indexes"].list_repositories(
+ connection, query_args(target_id=target_id)
+ )["repositories"][0]
+ assert repository["latestScan"]["scanId"] == SCAN_IDS[0 if newer_status == "complete" else 1]
+
+
@pytest.mark.parametrize("in_transaction", [False, True])
def test_repository_index_keeps_one_scan_cohort(
workbench_api, indexed_collections, tmp_path, in_transaction
diff --git a/plugins/codex-security/tests/test_workbench_native_indexes.py b/plugins/codex-security/tests/test_workbench_native_indexes.py
index 80b3649fe..d3b23e3c6 100644
--- a/plugins/codex-security/tests/test_workbench_native_indexes.py
+++ b/plugins/codex-security/tests/test_workbench_native_indexes.py
@@ -4,12 +4,16 @@ from __future__ import annotations
import json
import sqlite3
+import subprocess
+from datetime import datetime, timezone
from pathlib import Path
import pytest
+import test_workbench_scan_history as producer
from workbench_test_support import (
create_saved_workspace,
create_workspace,
+ initialize_git_repository,
run_workbench,
save_workspace,
scan_command,
@@ -211,6 +215,16 @@ def test_repository_index_probes_draft_checkout(
"missing-directory",
)
+ if target_path == ".":
+ assert run_workbench(state_dir, "list-repositories")["repositories"] == []
+ # Older clients registered relative draft paths before validation.
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ connection.execute(
+ "INSERT INTO security_targets (id, current_path, display_name, created_at, updated_at) "
+ "SELECT ?, target_path, target_path, created_at, updated_at FROM workspaces",
+ (stable_target_id(Path(target_path)),),
+ )
+
(repository,) = run_workbench(state_dir, "list-repositories")["repositories"]
assert repository["targetPath"] == target_path
assert repository["checkoutAvailable"] is available
@@ -289,6 +303,384 @@ def test_repository_index_reports_latest_scan_open_findings_and_missing_checkout
assert second["scanCount"] == 1
[email protected]("legacy", (False, True))
[email protected]("same_target", (False, True))
+def test_native_completion_keeps_same_target_legacy_triage_without_matching(
+ tmp_path: Path, legacy: bool, same_target: bool
+) -> None:
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ first = producer.create_cli_scan(
+ state,
+ tmp_path / "results",
+ repository,
+ identity_anchor="same-stable-finding",
+ target_revision=revision,
+ )
+ first = run_workbench(state, "get-scan", "--scan-id", first["scanId"])["scan"]
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ assert (
+ connection.execute(
+ "SELECT repository_generation FROM scans WHERE id = ?", (first["scanId"],)
+ ).fetchone()[0]
+ is not None
+ )
+ if legacy:
+ connection.execute(
+ "UPDATE scans SET repository_generation = NULL WHERE id = ?",
+ (first["scanId"],),
+ )
+ run_workbench(
+ state,
+ "set-finding-triage",
+ "--occurrence-id",
+ str(first["findings"][0]["occurrenceId"]),
+ "--status",
+ "closed",
+ "--close-reason",
+ "false_positive",
+ "--note",
+ "Synthetic checked guard.",
+ )
+ next_target = repository
+ if not same_target:
+ next_target = tmp_path / "other"
+ revision = initialize_git_repository(next_target)
+ producer.create_cli_scan(
+ state,
+ tmp_path / "results",
+ next_target,
+ identity_anchor="same-stable-finding",
+ target_revision=revision,
+ )
+ findings = run_workbench(state, "list-global-findings")["findings"]
+ assert len(findings) == (1 if same_target else 2)
+ current = next(row for row in findings if row["targetId"] == stable_target_id(next_target))
+ assert current["status"] == ("closed" if same_target else "open")
+ assert current["occurrenceCount"] == (2 if same_target else 1)
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ assert connection.execute("SELECT COUNT(*) FROM scan_comparison_matches").fetchone() == (0,)
+
+
[email protected]("saved_comparison", [False, True])
+def test_legacy_linked_comparison_keeps_unbound_triage_target_local(
+ tmp_path: Path, saved_comparison: bool
+) -> None:
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ linked = tmp_path / "linked"
+ subprocess.run(
+ ["git", "-C", str(repository), "worktree", "add", "-q", "--detach", str(linked)],
+ check=True,
+ )
+ first = producer.create_cli_scan(
+ state, tmp_path / "results", repository, target_revision=revision
+ )
+ later = producer.create_cli_scan(state, tmp_path / "results", linked, target_revision=revision)
+ first_finding = run_workbench(state, "get-scan", "--scan-id", first["scanId"])["scan"][
+ "findings"
+ ][0]
+ later_finding = run_workbench(state, "get-scan", "--scan-id", later["scanId"])["scan"][
+ "findings"
+ ][0]
+ assert first_finding["findingId"] != later_finding["findingId"]
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.execute(
+ "UPDATE scans SET repository_generation=NULL WHERE id IN (?,?)",
+ (first["scanId"], later["scanId"]),
+ )
+ run_workbench(
+ state,
+ "set-finding-triage",
+ "--occurrence-id",
+ first_finding["occurrenceId"],
+ "--status",
+ "closed",
+ "--close-reason",
+ "false_positive",
+ "--note",
+ "Synthetic checked guard.",
+ )
+ if saved_comparison:
+ match = producer.save_scan_matches(
+ state,
+ first,
+ later,
+ producer.confirmed_match(first_finding["occurrenceId"], later_finding["occurrenceId"]),
+ )
+ assert match["summary"]["persisting"] == 1
+ producer.create_cli_scan(
+ state, tmp_path / "results", repository, target_revision=revision, finding=False
+ )
+ producer.create_cli_scan(
+ state, tmp_path / "results", linked, target_revision=revision, finding=False
+ )
+ global_findings = run_workbench(state, "list-global-findings")["findings"]
+ scoped_findings = run_workbench(state, "list-global-findings", "--repository", str(linked))[
+ "findings"
+ ]
+ for findings in (global_findings, scoped_findings):
+ finding = next(row for row in findings if row["findingId"] == later_finding["findingId"])
+ assert finding["status"] == "open"
+
+
[email protected]("legacy_first", [False, True])
[email protected]("saved_current_match", [False, True])
+def test_sealed_legacy_recurrence_match_keeps_linked_scope_status(
+ tmp_path, monkeypatch, legacy_first, saved_current_match
+):
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ linked = tmp_path / "linked"
+ revision = initialize_git_repository(repository)
+ subprocess.run(
+ ["git", "-C", str(repository), "worktree", "add", "-q", "--detach", str(linked)], check=True
+ )
+ original_write = producer.write_completed_contract
+
+ def write_current_completion(scan_dir, scan_id, target, **values):
+ original_write(scan_dir, scan_id, target, **values)
+ p = scan_dir / "scan-manifest.json"
+ manifest = json.loads(p.read_text())
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ started_at = connection.execute(
+ "SELECT started_at FROM scans WHERE id=?", (scan_id,)
+ ).fetchone()[0]
+ manifest["scan"]["startedAt"] = started_at
+ manifest["scan"]["completedAt"] = (
+ datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
+ )
+ p.write_text(json.dumps(manifest))
+
+ monkeypatch.setattr(producer, "write_completed_contract", write_current_completion)
+ first = producer.create_cli_scan(
+ state, tmp_path / "results", repository, target_revision=revision
+ )
+ first_finding = run_workbench(state, "get-scan", "--scan-id", first["scanId"])["scan"][
+ "findings"
+ ][0]
+ if legacy_first:
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.execute(
+ "UPDATE scans SET repository_generation=NULL WHERE id=?", (first["scanId"],)
+ )
+ run_workbench(
+ state,
+ "set-finding-triage",
+ "--occurrence-id",
+ first_finding["occurrenceId"],
+ "--status",
+ "closed",
+ "--close-reason",
+ "false_positive",
+ "--note",
+ "Synthetic checked guard.",
+ )
+ current = producer.create_cli_scan(
+ state, tmp_path / "results", repository, target_revision=revision
+ )
+ current_finding = run_workbench(state, "get-scan", "--scan-id", current["scanId"])["scan"][
+ "findings"
+ ][0]
+ later = producer.create_cli_scan(state, tmp_path / "results", linked, target_revision=revision)
+ later_finding = run_workbench(state, "get-scan", "--scan-id", later["scanId"])["scan"][
+ "findings"
+ ][0]
+ assert first_finding["findingId"] == current_finding["findingId"]
+ assert first_finding["findingId"] != later_finding["findingId"]
+ assert current_finding["triage"]["status"] == later_finding["triage"]["status"] == "open"
+ if saved_current_match:
+ result = producer.save_scan_matches(
+ state,
+ current,
+ later,
+ producer.confirmed_match(
+ current_finding["occurrenceId"], later_finding["occurrenceId"]
+ ),
+ )
+ assert result["summary"]["persisting"] == 1
+ global_rows = run_workbench(state, "list-global-findings")["findings"]
+ scoped_rows = run_workbench(state, "list-global-findings", "--repository", str(linked))[
+ "findings"
+ ]
+ open_rows = run_workbench(state, "list-global-findings", "--status", "open")["findings"]
+ global_b = next(
+ row for row in global_rows if later_finding["findingId"] in row["matchedFindingIds"]
+ )
+ scoped_b = next(
+ row for row in scoped_rows if later_finding["findingId"] in row["matchedFindingIds"]
+ )
+ local_rows = run_workbench(state, "list-global-findings", "--repository", str(repository))[
+ "findings"
+ ]
+ local_current = next(row for row in local_rows if current["scanId"] in row["knownScanIds"])
+ assert local_current["status"] == "closed"
+ expected = "closed" if saved_current_match and not legacy_first else "open"
+ assert scoped_b["status"] == expected
+ assert global_b["status"] == expected
+ assert any(later_finding["findingId"] in row["matchedFindingIds"] for row in open_rows) == (
+ expected == "open"
+ )
+
+
[email protected]("saved_legacy_alias", [False, True])
[email protected]("saved_current_match", [False, True])
+def test_saved_legacy_alias_survives_current_linked_match(
+ tmp_path, monkeypatch, saved_legacy_alias, saved_current_match
+):
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ linked = tmp_path / "linked"
+ revision = initialize_git_repository(repository)
+ subprocess.run(
+ ["git", "-C", str(repository), "worktree", "add", "-q", "--detach", str(linked)], check=True
+ )
+ original_write = producer.write_completed_contract
+
+ def write_current_completion(scan_dir, scan_id, target, **values):
+ original_write(scan_dir, scan_id, target, **values)
+ p = scan_dir / "scan-manifest.json"
+ manifest = json.loads(p.read_text())
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ started_at = connection.execute(
+ "SELECT started_at FROM scans WHERE id=?", (scan_id,)
+ ).fetchone()[0]
+ manifest["scan"]["startedAt"] = started_at
+ manifest["scan"]["completedAt"] = (
+ datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
+ )
+ p.write_text(json.dumps(manifest))
+
+ monkeypatch.setattr(producer, "write_completed_contract", write_current_completion)
+ first = producer.create_cli_scan(
+ state,
+ tmp_path / "results",
+ repository,
+ target_revision=revision,
+ identity_anchor="synthetic-first-control",
+ )
+ first_finding = run_workbench(state, "get-scan", "--scan-id", first["scanId"])["scan"][
+ "findings"
+ ][0]
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.execute(
+ "UPDATE scans SET repository_generation=NULL WHERE id=?", (first["scanId"],)
+ )
+ run_workbench(
+ state,
+ "set-finding-triage",
+ "--occurrence-id",
+ first_finding["occurrenceId"],
+ "--status",
+ "closed",
+ "--close-reason",
+ "false_positive",
+ "--note",
+ "Synthetic checked guard.",
+ )
+ second = producer.create_cli_scan(
+ state,
+ tmp_path / "results",
+ repository,
+ target_revision=revision,
+ identity_anchor="synthetic-second-control",
+ )
+ second_finding = run_workbench(state, "get-scan", "--scan-id", second["scanId"])["scan"][
+ "findings"
+ ][0]
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.execute(
+ "UPDATE scans SET repository_generation=NULL WHERE id=?", (second["scanId"],)
+ )
+ assert first_finding["findingId"] != second_finding["findingId"]
+ if saved_legacy_alias:
+ result = producer.save_scan_matches(
+ state,
+ first,
+ second,
+ producer.confirmed_match(first_finding["occurrenceId"], second_finding["occurrenceId"]),
+ )
+ assert result["summary"]["persisting"] == 1
+ current = producer.create_cli_scan(
+ state,
+ tmp_path / "results",
+ repository,
+ target_revision=revision,
+ identity_anchor="synthetic-second-control",
+ )
+ current_finding = run_workbench(state, "get-scan", "--scan-id", current["scanId"])["scan"][
+ "findings"
+ ][0]
+ later = producer.create_cli_scan(
+ state,
+ tmp_path / "results",
+ linked,
+ target_revision=revision,
+ identity_anchor="synthetic-second-control",
+ )
+ later_finding = run_workbench(state, "get-scan", "--scan-id", later["scanId"])["scan"][
+ "findings"
+ ][0]
+ assert second_finding["findingId"] == current_finding["findingId"]
+ assert current_finding["findingId"] != later_finding["findingId"]
+ if saved_current_match:
+ result = producer.save_scan_matches(
+ state,
+ current,
+ later,
+ producer.confirmed_match(
+ current_finding["occurrenceId"], later_finding["occurrenceId"]
+ ),
+ )
+ assert result["summary"]["persisting"] == 1
+ global_rows = run_workbench(state, "list-global-findings")["findings"]
+ scoped_rows = run_workbench(state, "list-global-findings", "--repository", str(repository))[
+ "findings"
+ ]
+ linked_rows = run_workbench(state, "list-global-findings", "--repository", str(linked))[
+ "findings"
+ ]
+ for rows in [global_rows, scoped_rows]:
+ first_row = next(row for row in rows if first["scanId"] in row["knownScanIds"])
+ second_row = next(row for row in rows if second["scanId"] in row["knownScanIds"])
+ assert first_row["status"] == "closed"
+ assert second_row["status"] == ("closed" if saved_legacy_alias else "open")
+ assert (first["scanId"] in second_row["knownScanIds"]) == saved_legacy_alias
+ for rows in [global_rows, linked_rows]:
+ linked_row = next(row for row in rows if later["scanId"] in row["knownScanIds"])
+ assert linked_row["status"] == "open"
+
+
+def test_overlapping_scans_mark_findings_present_in_latest_started_scan(tmp_path: Path) -> None:
+ state_dir = tmp_path / "state"
+ target = tmp_path / "repo"
+ target.mkdir()
+ first = complete_scan(state_dir, target, identity_anchor="recurring-finding")
+ second = complete_scan(state_dir, target, identity_anchor="recurring-finding")
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ connection.execute(
+ "UPDATE scans SET started_at = ? WHERE id = ?",
+ ("2026-01-01T00:00:00Z", first["scanId"]),
+ )
+ connection.execute(
+ "UPDATE scans SET started_at = ? WHERE id = ?",
+ ("2026-01-02T00:00:00Z", second["scanId"]),
+ )
+ connection.execute(
+ "UPDATE finding_occurrences SET created_at = ? WHERE scan_id = ?",
+ ("2026-01-04T00:00:00Z", first["scanId"]),
+ )
+ connection.execute(
+ "UPDATE finding_occurrences SET created_at = ? WHERE scan_id = ?",
+ ("2026-01-03T00:00:00Z", second["scanId"]),
+ )
+ finding = run_workbench(state_dir, "list-global-findings")["findings"][0]
+ assert finding["confirmedInLatestScan"] is True
+ assert set(finding["knownScanIds"]) == {first["scanId"], second["scanId"]}
+
+
def test_scan_and_finding_search_casefold_unicode(tmp_path: Path) -> None:
state_dir = tmp_path / "state"
target = tmp_path / "Straße ÉCLAIR"
diff --git a/plugins/codex-security/tests/test_workbench_scan_archive.py b/plugins/codex-security/tests/test_workbench_scan_archive.py
index 93d94f552..559772bcd 100644
--- a/plugins/codex-security/tests/test_workbench_scan_archive.py
+++ b/plugins/codex-security/tests/test_workbench_scan_archive.py
@@ -8,7 +8,6 @@ import os
import sqlite3
import stat
import threading
-import uuid
from pathlib import Path
import pytest
@@ -322,7 +321,7 @@ def test_registration_rechecks_empty_output_after_competing_archive_rolls_back(
archived_scan_dir=None,
registration_json_stdin=False,
recipe_json_stdin=False,
- parent_scan_id=str(uuid.uuid4()),
+ parent_scan_id=None,
recipe_json=json.dumps(
{
"config": {},
@@ -368,6 +367,7 @@ def test_registration_rechecks_empty_output_after_competing_archive_rolls_back(
if path == scan_dir:
competitor.start()
assert waiting.wait(5)
+ raise SystemExit("Synthetic failure after archive replacement")
return result
with sqlite3.connect(database) as first:
@@ -375,7 +375,7 @@ def test_registration_rechecks_empty_output_after_competing_archive_rolls_back(
first.row_factory = sqlite3.Row
with monkeypatch.context() as patch:
patch.setattr(creation_owner, creation_method, replacement)
- with pytest.raises(SystemExit, match="not found"):
+ with pytest.raises(SystemExit, match="Synthetic failure after archive replacement"):
register(first, arguments)
competitor.join(10)
assert not competitor.is_alive()
diff --git a/plugins/codex-security/tests/test_workbench_scan_history.py b/plugins/codex-security/tests/test_workbench_scan_history.py
index cb9433ced..31f5476f7 100644
--- a/plugins/codex-security/tests/test_workbench_scan_history.py
+++ b/plugins/codex-security/tests/test_workbench_scan_history.py
@@ -5,6 +5,7 @@ from __future__ import annotations
import copy
import hashlib
import json
+import os
import shutil
import sqlite3
import subprocess
@@ -20,6 +21,7 @@ from test_workbench_db import HEAD_CHANGED_WARNING
from test_workbench_deep_scan import begin_target_scan
from test_workbench_prompt_only_scan import start_headless_standard_scan, start_prompt_only_scan
from workbench_test_support import (
+ create_workspace,
fail_scan,
get_scan,
initialize_git_repository,
@@ -891,7 +893,7 @@ def test_scan_history_resolves_unique_prefixes_and_rejects_ambiguity(tmp_path: P
assert get_scan(state_dir, scan["scanId"])["scan"]["scanId"] == scan["scanId"]
-def test_scan_list_includes_related_git_worktrees_and_clones(tmp_path: Path) -> None:
+def test_scan_list_shares_worktree_history_without_including_clones(tmp_path: Path) -> None:
state_dir = tmp_path / "state"
repository = tmp_path / "repository"
worktree = tmp_path / "worktree"
@@ -917,15 +919,14 @@ def test_scan_list_includes_related_git_worktrees_and_clones(tmp_path: Path) ->
clone_scan = create_cli_scan(state_dir, root / "clone", clone, complete=False)
unrelated_scan = create_cli_scan(state_dir, root / "unrelated", unrelated, complete=False)
- for target in (repository, worktree, clone):
+ for target in (repository, worktree):
scans = run_workbench(state_dir, "list-scans", "--repository", str(target))["scans"]
assert [scan["scanId"] for scan in scans] == [
- clone_scan["scanId"],
worktree_scan["scanId"],
original_scan["scanId"],
]
- for offset, expected in enumerate((clone_scan, worktree_scan, original_scan)):
+ for offset, expected in enumerate((worktree_scan, original_scan)):
page = run_workbench(
state_dir,
"list-scans",
@@ -937,12 +938,14 @@ def test_scan_list_includes_related_git_worktrees_and_clones(tmp_path: Path) ->
str(offset),
)
assert [scan["scanId"] for scan in page["scans"]] == [expected["scanId"]]
- assert page["nextOffset"] == (offset + 1 if offset < 2 else None)
+ assert page["nextOffset"] == (offset + 1 if offset < 1 else None)
queried = run_workbench(
state_dir, "list-scans", "--repository", str(repository), "--query", "clone"
)["scans"]
- assert [scan["scanId"] for scan in queried] == [clone_scan["scanId"]]
+ assert queried == []
+ clone_history = run_workbench(state_dir, "list-scans", "--repository", str(clone))["scans"]
+ assert [scan["scanId"] for scan in clone_history] == [clone_scan["scanId"]]
scoped = run_workbench(
state_dir,
@@ -1275,7 +1278,7 @@ def test_list_unmatched_scan_pairs_groups_pending_pairs_and_skips_saved_results(
("2026-01-01t02:00:00.000000+02:00", "2026-01-01T00:00:00Z"),
],
)
-def test_automatic_matching_orders_instants_and_preserves_directed_cache(
+def test_automatic_matching_orders_instants_and_reuses_reverse_cache(
tmp_path: Path, recurring_history, before_started: str, after_started: str
) -> None:
database, repository, (before, after) = recurring_history
@@ -1285,13 +1288,18 @@ def test_automatic_matching_orders_instants_and_preserves_directed_cache(
"UPDATE scans SET started_at = ? WHERE id = ?",
[(before_started, before["scanId"]), (after_started, after["scanId"])],
)
- save_scan_matches(state_dir, after, before)
-
pending = run_workbench(state_dir, "list-unmatched-scan-pairs", "--repository", str(repository))
assert pending["skippedPairs"] == 0
assert [batch["afterScanId"] for batch in pending["batches"]] == [after["scanId"]]
assert [scan["scanId"] for scan in pending["batches"][0]["beforeScans"]] == [before["scanId"]]
+ save_scan_matches(state_dir, after, before)
+ reverse_cached = run_workbench(
+ state_dir, "list-unmatched-scan-pairs", "--repository", str(repository)
+ )
+ assert reverse_cached["batches"] == []
+ assert reverse_cached["skippedPairs"] == 1
+
save_scan_matches(state_dir, before, after)
cached = run_workbench(state_dir, "list-unmatched-scan-pairs", "--repository", str(repository))
assert cached["batches"] == []
@@ -1534,10 +1542,10 @@ def test_explicit_comparison_uses_chronological_matching_cutoff(
history = load_script("workbench_scan_history")
- def unexpected_git_lookup(*_args: Any) -> str:
+ def unexpected_repository_lookup(*_args: Any) -> str:
raise AssertionError("Future scans must not trigger repository discovery")
- monkeypatch.setattr(history, "git_output", unexpected_git_lookup)
+ monkeypatch.setattr(history.RepositoryIdentityCache, "for_row", unexpected_repository_lookup)
with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
assert list(connection.iterdump()) == original
connection.row_factory = sqlite3.Row
@@ -1679,7 +1687,10 @@ def test_uncertain_semantic_scan_matches_stay_separate(tmp_path: Path) -> None:
assert "knownScanIds" not in shown
-def test_semantic_scan_comparison_replaces_cached_matches_atomically(tmp_path: Path) -> None:
[email protected]("reverse", (False, True))
+def test_semantic_scan_comparison_replaces_cached_matches_atomically(
+ tmp_path: Path, reverse: bool
+) -> None:
state_dir, repository = empty_history_paths(tmp_path)
root = tmp_path / "results"
before = create_cli_scan(state_dir, root, repository)
@@ -1694,7 +1705,10 @@ def test_semantic_scan_comparison_replaces_cached_matches_atomically(tmp_path: P
]
previous = inputs["before"][0]["occurrenceId"]
current = inputs["after"][0]["occurrenceId"]
- save_scan_matches(state_dir, before, after, confirmed_match(previous, current))
+ if reverse:
+ save_scan_matches(state_dir, after, before, confirmed_match(current, previous))
+ else:
+ save_scan_matches(state_dir, before, after, confirmed_match(previous, current))
compared = save_scan_matches(state_dir, before, after)
assert compared["summary"]["not_observed"] == 1
@@ -1704,6 +1718,95 @@ def test_semantic_scan_comparison_replaces_cached_matches_atomically(tmp_path: P
assert connection.execute("SELECT COUNT(*) FROM scan_comparison_matches").fetchone() == (0,)
shown = get_scan(state_dir, before["scanId"])["scan"]["findings"][0]
assert "matches" not in shown
+ assert len(run_workbench(state_dir, "list-global-findings")["findings"]) == 2
+
+
[email protected]("indirect", (False, True))
+def test_replacing_reverse_comparison_preserves_other_pairs_and_triage(
+ tmp_path: Path, indirect: bool
+) -> None:
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ repository.mkdir()
+ scans = [
+ create_cli_scan(state, tmp_path / "results", repository, identity_anchor=f"root-{index}")
+ for index in range(4)
+ ]
+ occurrences = [get_scan(state, scan["scanId"])["scan"]["findings"][0] for scan in scans]
+
+ def match(before: int, after: int) -> None:
+ save_scan_matches(
+ state,
+ scans[before],
+ scans[after],
+ confirmed_match(
+ occurrences[before]["occurrenceId"], occurrences[after]["occurrenceId"]
+ ),
+ )
+
+ match(1, 0)
+ match(2, 3)
+ if indirect:
+ match(0, 2)
+ match(2, 1)
+ set_triage(
+ state,
+ occurrences[0]["occurrenceId"],
+ "closed",
+ "--close-reason",
+ "wont_fix",
+ "--note",
+ "Accepted synthetic risk.",
+ )
+ artifacts = {
+ path: path.read_bytes()
+ for scan in scans
+ for name in ("scan-manifest.json", "findings.json", "coverage.json")
+ for path in (Path(scan["scanDir"]) / name,)
+ }
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ comparisons = connection.execute("SELECT * FROM scan_comparisons ORDER BY 1, 2").fetchall()
+ links = connection.execute("SELECT * FROM scan_comparison_matches ORDER BY 1, 2").fetchall()
+ triage = connection.execute(
+ "SELECT * FROM finding_triage ORDER BY occurrence_id"
+ ).fetchall()
+ stored_occurrences = connection.execute(
+ "SELECT * FROM finding_occurrences ORDER BY id"
+ ).fetchall()
+ old_pair = (scans[1]["scanId"], scans[0]["scanId"])
+
+ save_scan_matches(state, scans[0], scans[1])
+
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ remaining = connection.execute("SELECT * FROM scan_comparisons ORDER BY 1, 2").fetchall()
+ new_pair = (scans[0]["scanId"], scans[1]["scanId"])
+ assert [row for row in remaining if row[:2] != new_pair] == [
+ row for row in comparisons if row[:2] != old_pair
+ ]
+ assert len([row for row in remaining if row[:2] == new_pair]) == 1
+ assert connection.execute(
+ "SELECT * FROM scan_comparison_matches ORDER BY 1, 2"
+ ).fetchall() == [row for row in links if row[:2] != old_pair]
+ assert (
+ connection.execute("SELECT * FROM finding_triage ORDER BY occurrence_id").fetchall()
+ == triage
+ )
+ assert (
+ connection.execute("SELECT * FROM finding_occurrences ORDER BY id").fetchall()
+ == stored_occurrences
+ )
+ findings = run_workbench(state, "list-global-findings")["findings"]
+ groups = {frozenset(finding["knownScanIds"]) for finding in findings}
+ assert groups == (
+ {frozenset(scan["scanId"] for scan in scans)}
+ if indirect
+ else {
+ frozenset([scans[0]["scanId"]]),
+ frozenset([scans[1]["scanId"]]),
+ frozenset([scans[2]["scanId"], scans[3]["scanId"]]),
+ }
+ )
+ assert all(path.read_bytes() == content for path, content in artifacts.items())
def test_semantic_scan_comparison_rejects_cross_target_scans(tmp_path: Path) -> None:
@@ -1745,6 +1848,53 @@ def test_semantic_scan_comparison_rejects_cross_target_scans(tmp_path: Path) ->
assert "same repository target" in rejected["stderr"]
[email protected](os.name == "nt", reason="uses a POSIX executable fixture")
[email protected]("legacy", [False, True], ids=["retained", "legacy"])
+def test_history_ignores_git_from_saved_checkout_after_nested_reinitialization(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, legacy: bool
+) -> None:
+ host_git = shutil.which("git")
+ assert host_git is not None
+ state, original, requested = (tmp_path / name for name in ("state", "original", "requested"))
+ initialize_git_repository(original)
+ initialize_git_repository(requested)
+ saved = original / "surviving" / "component"
+ saved.mkdir(parents=True)
+ monkeypatch.chdir(requested)
+ monkeypatch.setenv("CODEX_SECURITY_GIT", host_git)
+ create_workspace(state, str(uuid.uuid4()), "--target-path", str(saved))
+ current = create_cli_scan(state, tmp_path / "scans", requested, complete=False)
+
+ if legacy:
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ updated = connection.execute(
+ "UPDATE security_targets SET git_checkout_root = NULL "
+ "WHERE current_path = ? AND repository_identity IS NOT NULL",
+ (str(saved),),
+ )
+ assert updated.rowcount == 1
+
+ (original / ".git").rename(tmp_path / "previous-metadata")
+ subprocess.run([host_git, "init", "-q", str(saved)], check=True)
+ # An unscanned target can be registered again after its Git generation changes.
+ create_workspace(state, str(uuid.uuid4()), "--target-path", str(saved))
+ executable = original / "git"
+ executable.write_text('#!/bin/sh\nprintf ran > "$0.ran"\nexec "$WORKBENCH_HOST_GIT" "$@"\n')
+ executable.chmod(0o700)
+ monkeypatch.setenv("WORKBENCH_HOST_GIT", host_git)
+ monkeypatch.setenv("PATH", str(original), prepend=os.pathsep)
+ query = ("list-scans", "--repository", str(requested))
+ pinned = run_workbench(state, *query)
+ assert [scan["scanId"] for scan in pinned["scans"]] == [current["scanId"]]
+ assert not (original / "git.ran").exists()
+
+ monkeypatch.delenv("CODEX_SECURITY_GIT")
+ listed = run_workbench(state, *query)
+ assert not (original / "git.ran").exists()
+ if not legacy:
+ assert [scan["scanId"] for scan in listed["scans"]] == [current["scanId"]]
+
+
def test_semantic_scan_comparison_accepts_linked_git_worktrees(tmp_path: Path) -> None:
state_dir = tmp_path / "state"
repository = tmp_path / "repository"
@@ -1803,9 +1953,8 @@ def test_semantic_scan_comparison_accepts_matching_git_origins(tmp_path: Path) -
)
for target in (before_repository, after_repository):
pending = run_workbench(state_dir, "list-unmatched-scan-pairs", "--repository", str(target))
- assert pending["scanCount"] == 2
- assert pending["batches"][0]["afterScanId"] == after["scanId"]
- assert pending["batches"][0]["beforeScans"][0]["scanId"] == before["scanId"]
+ assert pending["scanCount"] == 1
+ assert pending["batches"] == []
compared = compare_scan_pair(state_dir, before, after, "--include-matching-inputs")
assert compared["summary"]["new"] == 1
@@ -1949,6 +2098,1302 @@ def test_cli_diff_launch_accepts_equal_refs_and_distinct_working_tree_base(tmp_p
)
+def test_history_repair_matches_legacy_generation_in_owned_checkout(tmp_path: Path) -> None:
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ first = create_cli_scan(state, tmp_path / "results", repository, target_revision=revision)
+ second = create_cli_scan(state, tmp_path / "results", repository, target_revision=revision)
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.execute(
+ "UPDATE scans SET repository_generation = NULL WHERE id = ?", (first["scanId"],)
+ )
+ pending = run_workbench(state, "list-unmatched-scan-pairs", "--repository", str(repository))
+ assert len(pending["batches"]) == 1
+ assert pending["batches"][0]["afterScanId"] == second["scanId"]
+ assert pending["batches"][0]["beforeScans"][0]["scanId"] == first["scanId"]
+
+
[email protected]("remove_checkout", (False, True))
+def test_history_repair_keeps_triage_across_legacy_match(
+ tmp_path: Path, remove_checkout: bool
+) -> None:
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ first = create_cli_scan(
+ state,
+ tmp_path / "results",
+ repository,
+ target_revision=revision,
+ identity_anchor="synthetic-prior-anchor",
+ )
+ second = create_cli_scan(
+ state,
+ tmp_path / "results",
+ repository,
+ target_revision=revision,
+ identity_anchor="synthetic-current-anchor",
+ )
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.execute(
+ "UPDATE scans SET repository_generation = NULL WHERE id = ?", (first["scanId"],)
+ )
+ inputs = compare_scan_pair(state, first, second, "--include-matching-inputs")["matchingInputs"]
+ prior = inputs["before"][0]["occurrenceId"]
+ current = inputs["after"][0]["occurrenceId"]
+ save_scan_matches(state, first, second, confirmed_match(prior, current))
+ run_workbench(
+ state,
+ "set-finding-triage",
+ "--occurrence-id",
+ prior,
+ "--status",
+ "closed",
+ "--close-reason",
+ "false_positive",
+ "--note",
+ "The synthetic finding is already prevented by its checked guard.",
+ )
+ current = create_cli_scan(
+ state, tmp_path / "results", repository, target_revision=revision, finding=False
+ )
+ feedback = run_workbench(state, "get-scan-feedback", "--scan-id", current["scanId"])
+ assert len(feedback["falsePositives"]) == 1
+ assert (
+ feedback["falsePositives"][0]["reason"]
+ == "The synthetic finding is already prevented by its checked guard."
+ )
+ if remove_checkout:
+ repository.rename(tmp_path / "archived-checkout")
+ rows = run_workbench(state, "list-global-findings", "--repository", str(repository))["findings"]
+ assert len(rows) == 1
+ assert rows[0]["status"] == "closed"
+ assert rows[0]["occurrenceCount"] == 2
+ assert len(rows[0]["matchedFindingIds"]) == 2
+ assert run_workbench(state, "list-repositories")["repositories"][0]["openFindingsCount"] == 0
+
+
+def test_history_repair_uses_completion_horizon_for_focused_matching(tmp_path: Path) -> None:
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ first = create_cli_scan(
+ state,
+ tmp_path / "results",
+ repository,
+ target_revision=revision,
+ identity_anchor="synthetic-first-anchor",
+ )
+ second = create_cli_scan(
+ state,
+ tmp_path / "results",
+ repository,
+ target_revision=revision,
+ identity_anchor="synthetic-second-anchor",
+ )
+ inputs = compare_scan_pair(state, first, second, "--include-matching-inputs")["matchingInputs"]
+ save_scan_matches(
+ state,
+ first,
+ second,
+ confirmed_match(inputs["before"][0]["occurrenceId"], inputs["after"][0]["occurrenceId"]),
+ )
+ focused = create_cli_scan(
+ state,
+ tmp_path / "results",
+ repository,
+ target_revision=revision,
+ identity_anchor="synthetic-focused-anchor",
+ )
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.execute(
+ "UPDATE scans SET started_at = '2026-01-01T00:00:00Z' WHERE id = ?",
+ (focused["scanId"],),
+ )
+ pending = run_workbench(
+ state,
+ "list-unmatched-scan-pairs",
+ "--repository",
+ str(repository),
+ "--after-scan-id",
+ focused["scanId"],
+ )
+ assert len(pending["batches"]) == 1
+ groups = pending["batches"][0].get("knownFindingGroups", [])
+ assert len(groups) == 1
+ assert len(groups[0]) == 2
+
+
+def test_history_repair_rebinds_only_unscanned_workspace(tmp_path: Path) -> None:
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ initialize_git_repository(repository)
+ workspace = str(uuid.uuid4())
+ created = run_workbench(
+ state, "create-workspace", "--workspace-id", workspace, "--target-path", str(repository)
+ )
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ old = connection.execute(
+ "SELECT id, repository_identity FROM security_targets WHERE current_path = ?",
+ (str(repository),),
+ ).fetchone()
+ (repository / ".git").rename(tmp_path / "previous-git")
+ subprocess.run(["git", "init", "-q"], cwd=repository, check=True)
+ run_workbench(
+ state,
+ "save-workspace",
+ "--workspace-id",
+ workspace,
+ "--target-path",
+ str(repository),
+ "--scope",
+ ".",
+ "--mode",
+ "deep",
+ )
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ new = connection.execute(
+ "SELECT id, repository_identity FROM security_targets WHERE current_path = ?",
+ (str(repository),),
+ ).fetchone()
+ assert old is not None and new is not None
+ assert new[0] == old[0]
+ assert new[1] != old[1]
+ assert created["targetMetadata"]["isGit"] is True
+
+
+def test_history_repair_counts_only_each_worktrees_legacy_findings(tmp_path: Path) -> None:
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ linked = tmp_path / "linked"
+ revision = initialize_git_repository(repository)
+ subprocess.run(
+ ["git", "-C", str(repository), "worktree", "add", "-q", "--detach", str(linked)], check=True
+ )
+ legacy = create_cli_scan(state, tmp_path / "results", repository, target_revision=revision)
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.execute(
+ "UPDATE scans SET repository_generation = NULL WHERE id = ?", (legacy["scanId"],)
+ )
+ create_cli_scan(state, tmp_path / "results", linked, target_revision=revision, finding=False)
+ counts = {
+ row["targetPath"]: row["openFindingsCount"]
+ for row in run_workbench(state, "list-repositories")["repositories"]
+ }
+ assert (
+ counts[str(repository)]
+ == len(
+ run_workbench(state, "list-global-findings", "--repository", str(repository))[
+ "findings"
+ ]
+ )
+ == 1
+ )
+ assert (
+ counts[str(linked)]
+ == len(
+ run_workbench(state, "list-global-findings", "--repository", str(linked))["findings"]
+ )
+ == 0
+ )
+
+
+def test_history_repair_rebinds_unscanned_directory(tmp_path: Path) -> None:
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ initialize_git_repository(repository)
+ workspace = str(uuid.uuid4())
+ run_workbench(
+ state, "create-workspace", "--workspace-id", workspace, "--target-path", str(repository)
+ )
+ (repository / ".git").rename(tmp_path / "previous-git")
+ run_workbench(
+ state,
+ "save-workspace",
+ "--workspace-id",
+ workspace,
+ "--target-path",
+ str(repository),
+ "--scope",
+ ".",
+ "--mode",
+ "standard",
+ )
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ assert (
+ connection.execute(
+ "SELECT repository_identity FROM security_targets WHERE current_path = ?",
+ (str(repository),),
+ ).fetchone()[0]
+ is None
+ )
+ create_cli_scan(state, tmp_path / "results", repository)
+
+
+ "timestamp",
+ [
+ "2026-07-01T00:00:00Z",
+ "2026-07-01T00:00:00z",
+ "2026-07-01t00:00:00z",
+ "2026-07-01T00:00:00+00:00",
+ "2026-06-30T19:00:00-05:00",
+ ],
+)
+def test_history_timestamp_preserves_utc_completion_order(workbench_api, timestamp):
+ target_state = sys.modules["workbench_target_state"]
+ assert target_state._timestamp_ns(timestamp) == 1782864000000000000
+
+
+ ("digits", "prefix"),
+ [(digits, "") for digits in [1, 2, 3, 4, 5, 6, 7, 10, 40, 5000]] + [(7, "123456")],
+)
+def test_history_timestamp_preserves_fractional_order_and_ownership(workbench_api, digits, prefix):
+ target_state = sys.modules["workbench_target_state"]
+ history = sys.modules["workbench_scan_history"]
+ fraction = prefix.ljust(digits - 1, "0")
+ earlier = f"1970-01-01T00:00:00.{fraction}1z"
+ later = f"1970-01-01T01:00:00.{fraction}2+01:00"
+ # Persisted rows can have been finalized by a newer Python runtime.
+ for field in ("completed_at", "started_at"):
+ scans = [{"id": "b", field: earlier}, {"id": "a", field: later}]
+ assert sorted(scans, key=history._scan_completion_order) == scans
+ timestamp = target_state._timestamp_ns(earlier)
+ assert timestamp is not None
+ birth = int((fraction + "1")[:9].ljust(9, "0"))
+ assert birth <= timestamp < birth + 1
+ scan = {"started_at": earlier, "created_at": earlier}
+ for common_birth, object_birth, expected in (
+ (birth, birth, True),
+ (birth + 1, birth, False),
+ (birth, birth + 1, False),
+ ):
+ identity = target_state.GitRepositoryIdentity(
+ "repository", ".", "common", 1, 2, common_birth, object_birth
+ )
+ assert target_state._repository_predates_history(identity, [scan]) is expected
+
+
+ "timestamp",
+ [
+ None,
+ "invalid",
+ "2026-07-01T00:00:00",
+ "2026-07-01.1+00:00",
+ "1970-01-01T00:00:00.1Z\n",
+ "1970-01-01T00:00:00.1234567+00:00\n",
+ "1970-01-01T00:00:00.١23456Z",
+ ],
+)
+def test_history_timestamp_keeps_unavailable_completion_order(workbench_api, timestamp):
+ target_state = sys.modules["workbench_target_state"]
+ assert target_state._timestamp_ns(timestamp) is None
+
+
[email protected]("matched", (False, True))
[email protected]("legacy_representative", (False, True))
+def test_matched_repository_confirmation_uses_all_saved_repository_buckets(
+ tmp_path: Path, matched: bool, legacy_representative: bool
+) -> None:
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ before = create_cli_scan(
+ state,
+ tmp_path / "results",
+ repository,
+ target_revision=revision,
+ identity_anchor="synthetic-legacy-anchor",
+ )
+ after = create_cli_scan(
+ state,
+ tmp_path / "results",
+ repository,
+ target_revision=revision,
+ identity_anchor="synthetic-current-anchor",
+ )
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.execute(
+ "UPDATE scans SET repository_generation = NULL WHERE id = ?", (before["scanId"],)
+ )
+ # Preserve the two possible imported report orders independently of completion order.
+ for scan, timestamp in (
+ (before, "2026-09-02T00:00:00Z" if legacy_representative else "2026-09-01T00:00:00Z"),
+ (after, "2026-09-01T00:00:00Z" if legacy_representative else "2026-09-02T00:00:00Z"),
+ ):
+ connection.execute(
+ "UPDATE finding_occurrences SET created_at = ? WHERE scan_id = ?",
+ (timestamp, scan["scanId"]),
+ )
+ if matched:
+ inputs = compare_scan_pair(state, before, after, "--include-matching-inputs")[
+ "matchingInputs"
+ ]
+ save_scan_matches(
+ state,
+ before,
+ after,
+ confirmed_match(
+ inputs["before"][0]["occurrenceId"], inputs["after"][0]["occurrenceId"]
+ ),
+ )
+ create_cli_scan(
+ state, tmp_path / "results", repository, target_revision=revision, finding=False
+ )
+ global_findings = run_workbench(state, "list-global-findings")["findings"]
+ legacy = next(
+ finding for finding in global_findings if before["scanId"] in finding["knownScanIds"]
+ )
+ assert legacy["confirmedInLatestScan"] is False
+ if matched:
+ assert len(global_findings) == 1
+ assert set(legacy["knownScanIds"]) == {before["scanId"], after["scanId"]}
+ scoped = run_workbench(state, "list-global-findings", "--repository", str(repository))[
+ "findings"
+ ]
+ assert len(scoped) == 1
+ assert scoped[0]["confirmedInLatestScan"] is False
+
+
[email protected]("mode", ("standard", "deep"))
+def test_portable_identity_allows_git_initialization_after_plain_directory_history(
+ tmp_path: Path, mode: str
+) -> None:
+ repository = tmp_path / "repository"
+ repository.mkdir()
+ (repository / "README.md").write_text("fixture\n")
+ state = tmp_path / "state"
+ before = create_cli_scan(state, tmp_path / "results", repository, mode=mode)
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ assert (
+ connection.execute(
+ "SELECT target_revision FROM scans WHERE id = ?", (before["scanId"],)
+ ).fetchone()[0]
+ == "unversioned"
+ )
+ subprocess.run(["git", "init", "-q"], cwd=repository, check=True)
+ subprocess.run(
+ ["git", "-c", "user.name=Fixture", "-c", "[email protected]", "add", "."],
+ cwd=repository,
+ check=True,
+ )
+ subprocess.run(
+ [
+ "git",
+ "-c",
+ "user.name=Fixture",
+ "-c",
+ "commit",
+ "-qm",
+ "Initialize repository",
+ ],
+ cwd=repository,
+ check=True,
+ )
+ revision = subprocess.check_output(
+ ["git", "rev-parse", "HEAD"], cwd=repository, text=True
+ ).strip()
+ after = create_cli_scan(
+ state, tmp_path / "results", repository, mode=mode, target_revision=revision
+ )
+ assert compare_scan_pair(state, before, after)["afterScanId"] == after["scanId"]
+
+
[email protected]("same_origin", (True, False))
+def test_portable_identity_explicit_clone_comparison_without_birth_timestamps(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, same_origin: bool
+) -> None:
+ import argparse
+
+ monkeypatch.syspath_prepend(str(SCRIPT.parent))
+ import workbench_scan_history as history
+ import workbench_target_state as target_state
+
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ subprocess.run(
+ ["git", "remote", "add", "origin", "https://example.test/synthetic/project.git"],
+ cwd=repository,
+ check=True,
+ )
+ clone = tmp_path / "clone"
+ subprocess.run(["git", "clone", "-q", str(repository), str(clone)], check=True)
+ subprocess.run(
+ [
+ "git",
+ "remote",
+ "set-url",
+ "origin",
+ "https://example.test/synthetic/project.git"
+ if same_origin
+ else "https://example.test/synthetic/other.git",
+ ],
+ cwd=clone,
+ check=True,
+ )
+ state = tmp_path / "state"
+ before = create_cli_scan(state, tmp_path / "results", repository, target_revision=revision)
+ after = create_cli_scan(state, tmp_path / "results", clone, target_revision=revision)
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ connection.execute("UPDATE scans SET repository_generation = NULL")
+ connection.execute("UPDATE security_targets SET repository_identity = NULL")
+ monkeypatch.setattr(target_state, "_repository_birth_time_ns", lambda *_: None)
+ args = argparse.Namespace(before_scan_id=before["scanId"], after_scan_id=after["scanId"])
+
+ def compare():
+ return history.compare_scans(
+ connection,
+ args,
+ require_scan=lambda db, id: db.execute(
+ "SELECT * FROM scans WHERE id = ?", (id,)
+ ).fetchone(),
+ read_coverage=lambda row: json.loads(
+ (Path(row["scan_dir"]) / "coverage.json").read_text()
+ ),
+ )
+
+ if same_origin:
+ assert compare()["afterScanId"] == after["scanId"]
+ else:
+ with pytest.raises(SystemExit, match="same repository"):
+ compare()
+
+
+ "platform,ending,literal_cr",
+ (
+ ("win32", b"\r\n", False),
+ ("win32", b"\n", False),
+ ("linux", b"\n", False),
+ ("linux", b"\n", True),
+ ),
+)
+def test_portable_identity_decodes_git_line_protocol_without_changing_path_bytes(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, platform: str, ending: bytes, literal_cr: bool
+) -> None:
+ import os
+
+ monkeypatch.syspath_prepend(str(SCRIPT.parent))
+ import workbench_target_state as target_state
+
+ monkeypatch.setattr(target_state.sys, "platform", platform)
+ expected = tmp_path / ("path\r" if literal_cr else "path")
+ assert target_state._path_from_git_bytes(os.fsencode(expected) + ending, tmp_path) == expected
+ assert (
+ target_state._path_from_git_bytes(os.fsencode(expected), tmp_path, strip_line_feed=False)
+ == expected
+ )
+
+
[email protected]("reopen", (False, True))
+def test_portable_identity_history_lookup_uses_indexes(tmp_path: Path, reopen: bool) -> None:
+ state = tmp_path / "state"
+ run_workbench(state, "list-repositories")
+ if reopen:
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.execute("DROP INDEX IF EXISTS scans_by_target_path")
+ run_workbench(state, "list-repositories")
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ plan = [
+ row[3]
+ for row in connection.execute(
+ "EXPLAIN QUERY PLAN SELECT target_device, target_inode, started_at, created_at FROM scans WHERE target_id = ? OR target_path = ?",
+ ("synthetic-target", "synthetic-path"),
+ )
+ ]
+ assert not any(line.startswith("SCAN scans") for line in plan), plan
+ assert any("target_id=?" in line for line in plan), plan
+ assert any("target_path=?" in line for line in plan), plan
+
+
[email protected]("mode", ("standard", "deep"))
+def test_legacy_aliases_restore_mixed_plain_and_git_history(tmp_path: Path, mode: str) -> None:
+ repository = tmp_path / "repository"
+ repository.mkdir()
+ (repository / "README.md").write_text("fixture\n")
+ state = tmp_path / "state"
+ before = create_cli_scan(state, tmp_path / "results", repository, mode=mode)
+ subprocess.run(["git", "init", "-q"], cwd=repository, check=True)
+ subprocess.run(
+ ["git", "-c", "user.name=Fixture", "-c", "[email protected]", "add", "."],
+ cwd=repository,
+ check=True,
+ )
+ subprocess.run(
+ [
+ "git",
+ "-c",
+ "user.name=Fixture",
+ "-c",
+ "commit",
+ "-qm",
+ "Initialize repository",
+ ],
+ cwd=repository,
+ check=True,
+ )
+ revision = subprocess.check_output(
+ ["git", "rev-parse", "HEAD"], cwd=repository, text=True
+ ).strip()
+ after = create_cli_scan(
+ state, tmp_path / "results", repository, mode=mode, target_revision=revision
+ )
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.execute("UPDATE scans SET repository_generation = NULL")
+ connection.execute("UPDATE security_targets SET repository_identity = NULL")
+ current = create_cli_scan(
+ state, tmp_path / "results", repository, mode=mode, target_revision=revision
+ )
+ assert compare_scan_pair(state, after, current)["afterScanId"] == current["scanId"]
+ assert before["scanId"] != current["scanId"]
+
+
[email protected]("missing_birth", ("before", "after"))
[email protected]("same_origin", (True, False))
+def test_legacy_aliases_explicit_comparison_with_mixed_birth_support(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, missing_birth: str, same_origin: bool
+) -> None:
+ import argparse
+
+ monkeypatch.syspath_prepend(str(SCRIPT.parent))
+ import workbench_scan_history as history
+ import workbench_target_state as target_state
+
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ subprocess.run(
+ ["git", "remote", "add", "origin", "https://example.test/synthetic/project.git"],
+ cwd=repository,
+ check=True,
+ )
+ clone = tmp_path / "clone"
+ subprocess.run(["git", "clone", "-q", str(repository), str(clone)], check=True)
+ subprocess.run(
+ [
+ "git",
+ "remote",
+ "set-url",
+ "origin",
+ "https://example.test/synthetic/project.git"
+ if same_origin
+ else "https://example.test/synthetic/other.git",
+ ],
+ cwd=clone,
+ check=True,
+ )
+ state = tmp_path / "state"
+ before = create_cli_scan(state, tmp_path / "results", repository, target_revision=revision)
+ after = create_cli_scan(state, tmp_path / "results", clone, target_revision=revision)
+ missing = before if missing_birth == "before" else after
+ missing_path = repository if missing_birth == "before" else clone
+ original_birth = target_state._repository_birth_time_ns
+ monkeypatch.setattr(
+ target_state,
+ "_repository_birth_time_ns",
+ lambda path, metadata: (
+ None if Path(path) == missing_path / ".git" else original_birth(path, metadata)
+ ),
+ )
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ connection.execute(
+ "UPDATE scans SET repository_generation = NULL WHERE id = ?", (missing["scanId"],)
+ )
+ connection.execute(
+ "UPDATE security_targets SET repository_identity = NULL WHERE current_path = ?",
+ (str(missing_path),),
+ )
+ args = argparse.Namespace(before_scan_id=before["scanId"], after_scan_id=after["scanId"])
+
+ def compare():
+ return history.compare_scans(
+ connection,
+ args,
+ require_scan=lambda db, id: db.execute(
+ "SELECT * FROM scans WHERE id = ?", (id,)
+ ).fetchone(),
+ read_coverage=lambda row: json.loads(
+ (Path(row["scan_dir"]) / "coverage.json").read_text()
+ ),
+ )
+
+ if same_origin:
+ assert compare()["afterScanId"] == after["scanId"]
+ else:
+ with pytest.raises(SystemExit, match="same repository"):
+ compare()
+
+
[email protected]("other_checkout", ("linked", "clone", "different-scope"))
+def test_originless_linked_comparison_without_birth_timestamps(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, other_checkout: str
+) -> None:
+ import argparse
+
+ monkeypatch.syspath_prepend(str(SCRIPT.parent))
+ import workbench_scan_history as history
+ import workbench_target_state as target_state
+
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ (repository / "src").mkdir()
+ linked = tmp_path / "linked"
+ if other_checkout == "clone":
+ subprocess.run(["git", "clone", "-q", str(repository), str(linked)], check=True)
+ subprocess.run(["git", "remote", "remove", "origin"], cwd=linked, check=True)
+ else:
+ subprocess.run(
+ ["git", "worktree", "add", "-q", "-b", "fixture-linked", str(linked)],
+ cwd=repository,
+ check=True,
+ )
+ (linked / "src").mkdir(exist_ok=True)
+ state = tmp_path / "state"
+ before_target = repository / "src" if other_checkout == "different-scope" else repository
+ before = create_cli_scan(state, tmp_path / "results", before_target, target_revision=revision)
+ after = create_cli_scan(state, tmp_path / "results", linked, target_revision=revision)
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ connection.execute("UPDATE scans SET repository_generation = NULL")
+ connection.execute("UPDATE security_targets SET repository_identity = NULL")
+ connection.commit()
+ monkeypatch.setattr(target_state, "_repository_birth_time_ns", lambda *_: None)
+ args = argparse.Namespace(before_scan_id=before["scanId"], after_scan_id=after["scanId"])
+ require_scan = lambda db, id: db.execute(
+ "SELECT * FROM scans WHERE id = ?", (id,)
+ ).fetchone()
+ read_coverage = lambda row: json.loads(
+ (Path(row["scan_dir"]) / "coverage.json").read_text()
+ )
+ if other_checkout != "linked":
+ with pytest.raises(SystemExit, match="same repository"):
+ history.compare_scans(
+ connection, args, require_scan=require_scan, read_coverage=read_coverage
+ )
+ return
+ result = history.compare_scans(
+ connection, args, require_scan=require_scan, read_coverage=read_coverage
+ )
+ assert result["afterScanId"] == after["scanId"]
+ before_id = connection.execute(
+ "SELECT id FROM finding_occurrences WHERE scan_id = ?", (before["scanId"],)
+ ).fetchone()[0]
+ after_id = connection.execute(
+ "SELECT id FROM finding_occurrences WHERE scan_id = ?", (after["scanId"],)
+ ).fetchone()[0]
+ args.matches_json = json.dumps(
+ {"matches": [confirmed_match(before_id, after_id)], "uncertain": []}
+ )
+ saved = history.save_scan_comparison(
+ connection,
+ args,
+ now=lambda: "2026-10-07T00:00:00Z",
+ require_scan=require_scan,
+ read_coverage=read_coverage,
+ )
+ assert saved["afterScanId"] == after["scanId"]
+
+
[email protected]("scope", [".", "component"])
+def test_repository_identity_preserves_directory_aliases(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, scope: str
+) -> None:
+ import os
+
+ monkeypatch.syspath_prepend(str(SCRIPT.parent))
+ import workbench_target_state as target_state
+
+ repository = tmp_path / "repository"
+ initialize_git_repository(repository)
+ (repository / "component").mkdir()
+ alias = tmp_path / "alias"
+ alias.symlink_to(repository, target_is_directory=True)
+ selected = alias / scope
+ actual_realpath = os.path.realpath
+
+ def preserve_alias(path, *args, **kwargs):
+ result = actual_realpath(path, *args, **kwargs)
+ return str(selected) if str(path) == str(selected) else result
+
+ # Case-insensitive filesystems can retain an alias's spelling. Keep real
+ # directory identity and Git operations while controlling that spelling.
+ monkeypatch.setattr(os.path, "realpath", preserve_alias)
+ expected = target_state.repository_identity(repository / scope)
+ assert expected is not None
+ assert target_state.repository_identity(selected) == expected
+ assert target_state.repository_relative_path(selected) == scope
+
+
+def change_repository_metadata(
+ monkeypatch: pytest.MonkeyPatch, repository: Path, change: str
+) -> None:
+ from types import SimpleNamespace
+
+ import workbench_target_state as target_state
+
+ original_stat = Path.stat
+ original_statvfs = getattr(target_state.os, "statvfs", None)
+ original_birth = target_state._repository_birth_time_ns
+ objects = repository / ".git" / "objects"
+
+ class ChangedMetadata:
+ def __init__(self, metadata: Any, name: str) -> None:
+ self.metadata, self.name = metadata, name
+
+ def __getattr__(self, name: str) -> Any:
+ value = getattr(self.metadata, name)
+ return value + 1 if name == self.name else value
+
+ def metadata(path: Path, *args: Any, **kwargs: Any) -> Any:
+ result = original_stat(path, *args, **kwargs)
+ if change in ("device", "legacy-device") and (
+ path == repository or repository in path.parents
+ ):
+ return ChangedMetadata(result, "st_dev")
+ if change == "inode" and path == objects:
+ return ChangedMetadata(result, "st_ino")
+ return result
+
+ def filesystem(path: Path) -> Any:
+ result = original_statvfs(path) if original_statvfs else SimpleNamespace(f_fsid=1)
+ return (
+ ChangedMetadata(result, "f_fsid")
+ if change in ("filesystem", "common-filesystem")
+ and Path(path) == (objects if change == "filesystem" else repository / ".git")
+ else result
+ )
+
+ def birth(path: str, value: Any) -> int | None:
+ result = original_birth(path, value)
+ return (
+ result + 1
+ if result is not None and change == "birth" and Path(path) == objects
+ else result
+ )
+
+ monkeypatch.setattr(Path, "stat", metadata)
+ monkeypatch.setattr(target_state.os, "statvfs", filesystem, raising=False)
+ monkeypatch.setattr(target_state, "_repository_birth_time_ns", birth)
+
+
+ "change",
+ (
+ "device",
+ "legacy-device",
+ "stored-legacy-device",
+ "foreign-strong-device",
+ "filesystem",
+ "common-filesystem",
+ "inode",
+ "birth",
+ ),
+)
+def test_repository_history_distinguishes_linux_remount_from_replacement(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, change: str
+) -> None:
+ import argparse
+
+ monkeypatch.syspath_prepend(str(SCRIPT.parent))
+ import workbench_scan_history as history
+ import workbench_target_state as target_state
+
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ state = tmp_path / "state"
+ scan = create_cli_scan(state, tmp_path / "results", repository, target_revision=revision)
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ if change == "legacy-device":
+ connection.execute("UPDATE security_targets SET repository_identity=NULL")
+ connection.execute("UPDATE scans SET repository_generation=NULL")
+ if change == "stored-legacy-device":
+ identity = target_state._repository_identity_details(repository)
+ assert identity is not None
+ connection.execute(
+ "UPDATE security_targets SET repository_identity=?", (identity.legacy_value,)
+ )
+ before = dict(connection.execute("SELECT * FROM scans").fetchone())
+ args = argparse.Namespace(
+ repository=str(repository),
+ scan_root=None,
+ target_id=None,
+ mode=None,
+ status=None,
+ query=None,
+ limit=None,
+ offset=0,
+ )
+ assert [row["scanId"] for row in history.list_scans(connection, args)["scans"]] == [
+ scan["scanId"]
+ ]
+ change_repository_metadata(
+ monkeypatch,
+ repository,
+ "device" if change in ("stored-legacy-device", "foreign-strong-device") else change,
+ )
+ if change == "foreign-strong-device":
+ connection.execute(
+ "UPDATE security_targets SET repository_identity=?",
+ (
+ target_state._identity_digest(
+ "synthetic foreign binding", "repository_v3_sha256_"
+ ),
+ ),
+ )
+ binding = connection.execute("SELECT repository_identity FROM security_targets").fetchone()[
+ 0
+ ]
+ changes = connection.total_changes
+ preserved = (
+ change in ("device", "legacy-device", "stored-legacy-device")
+ and sys.platform == "linux"
+ or change in ("filesystem", "common-filesystem")
+ and sys.platform != "linux"
+ )
+ assert [row["scanId"] for row in history.list_scans(connection, args)["scans"]] == (
+ [scan["scanId"]] if preserved else []
+ )
+ if preserved:
+ assert (
+ target_state.ensure_security_target(connection, str(repository))
+ == before["target_id"]
+ )
+ else:
+ with pytest.raises(SystemExit, match="no longer matches"):
+ target_state.ensure_security_target(connection, str(repository))
+ assert dict(connection.execute("SELECT * FROM scans").fetchone()) == before
+ assert connection.total_changes == changes
+ assert (
+ connection.execute("SELECT repository_identity FROM security_targets").fetchone()[0]
+ == binding
+ )
+
+
[email protected]("remounted_before_upgrade", (False, True))
[email protected]("stored_binding", (False, True))
+def test_repository_generation_upgrade_requires_the_original_v2_digest(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ remounted_before_upgrade: bool,
+ stored_binding: bool,
+) -> None:
+ monkeypatch.syspath_prepend(str(SCRIPT.parent))
+ import workbench_db as workbench
+ import workbench_target_state as target_state
+
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ state = tmp_path / "state"
+ create_cli_scan(state, tmp_path / "results", repository, target_revision=revision)
+ identity = target_state._repository_identity_details(repository)
+ assert identity is not None
+ previous = identity.previous_value or identity.value
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ connection.execute(
+ "UPDATE security_targets SET repository_identity=?",
+ (previous if stored_binding else None,),
+ )
+ connection.execute("UPDATE scans SET repository_generation=?", (previous,))
+ connection.execute(
+ "DELETE FROM schema_migrations WHERE name = ?",
+ ("stabilize Linux repository generations",),
+ )
+ before = dict(connection.execute("SELECT * FROM scans").fetchone())
+ if remounted_before_upgrade:
+ change_repository_metadata(monkeypatch, repository, "device")
+ workbench.apply_migrations(connection)
+ expected = previous if remounted_before_upgrade else identity.value
+ assert connection.execute("SELECT repository_identity FROM security_targets").fetchone()[
+ 0
+ ] == (expected if stored_binding else None)
+ assert dict(connection.execute("SELECT * FROM scans").fetchone()) == {
+ **before,
+ "repository_generation": expected,
+ }
+ if remounted_before_upgrade:
+ with pytest.raises(SystemExit, match="no longer matches"):
+ target_state.ensure_security_target(connection, str(repository))
+ else:
+ assert (
+ target_state.ensure_security_target(connection, str(repository))
+ == before["target_id"]
+ )
+
+
[email protected]("historical_scan", (False, True))
[email protected]("binding", ("authenticated", "unmatched", "remounted"))
+def test_repository_generation_migration_preserves_null_history(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, historical_scan: bool, binding: str
+) -> None:
+ monkeypatch.syspath_prepend(str(SCRIPT.parent))
+ import workbench_db as workbench
+ import workbench_target_state as target_state
+
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ state = tmp_path / "state"
+ if historical_scan:
+ create_cli_scan(state, tmp_path / "results", repository, target_revision=revision)
+ else:
+ state.mkdir()
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ workbench.apply_migrations(connection)
+ target_id = target_state.ensure_security_target(connection, str(repository))
+ identity = target_state._repository_identity_details(repository)
+ assert identity is not None
+ previous = identity.previous_value or identity.value
+ if binding == "unmatched":
+ previous = "repository_sha256_" + "0" * 64
+ connection.execute("UPDATE security_targets SET repository_identity=?", (previous,))
+ connection.execute("UPDATE scans SET repository_generation=NULL")
+ connection.execute(
+ "DELETE FROM schema_migrations WHERE name = ?",
+ ("stabilize Linux repository generations",),
+ )
+ before = [dict(row) for row in connection.execute("SELECT * FROM scans")]
+ if binding == "authenticated":
+ scope = target_state.RepositoryIdentityCache(connection).scope(target_id)
+ assert all(scope.contains(row) for row in before)
+ if binding == "remounted":
+ change_repository_metadata(monkeypatch, repository, "device")
+ workbench.apply_migrations(connection)
+ expected = identity.value if binding == "authenticated" else previous
+ assert (
+ connection.execute("SELECT repository_identity FROM security_targets").fetchone()[0]
+ == expected
+ )
+ assert [dict(row) for row in connection.execute("SELECT * FROM scans")] == before
+ assert (
+ connection.execute("SELECT MAX(version) FROM schema_migrations").fetchone()[0]
+ == workbench.MIGRATIONS[-1][0]
+ )
+ # A completed migration, including an unverifiable binding, does not probe
+ # local repositories again on a database reopen.
+ with monkeypatch.context() as no_probes:
+
+ def unexpected_probe(*args, **kwargs):
+ raise AssertionError("A completed migration reprobed repository identity")
+
+ no_probes.setattr(target_state, "_repository_identity_details", unexpected_probe)
+ workbench.apply_migrations(connection)
+ if binding == "authenticated":
+ registration = target_state.register_security_target(connection, str(repository))
+ assert registration.target_id == target_id
+ assert registration.repository_generation == identity.value
+ if sys.platform == "linux":
+ change_repository_metadata(monkeypatch, repository, "device")
+ scope = target_state.RepositoryIdentityCache(connection).scope(target_id)
+ assert scope.available
+ assert all(scope.contains(row) for row in before)
+ elif historical_scan:
+ with pytest.raises(SystemExit, match="no longer matches"):
+ target_state.ensure_security_target(connection, str(repository))
+ # Unscanned targets retain the existing registration rebind behavior.
+ else:
+ assert target_state.ensure_security_target(connection, str(repository)) == target_id
+
+
+def test_repository_generation_migration_is_atomic_and_preserves_shared_history(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.syspath_prepend(str(SCRIPT.parent))
+ import workbench_db as workbench
+ import workbench_target_state as target_state
+
+ git = shutil.which("git")
+ assert git is not None
+ # Keep Git available when the linked checkout below is temporarily absent.
+ monkeypatch.setenv("CODEX_SECURITY_GIT", str(Path(git).absolute()))
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ linked = tmp_path / "linked"
+ subprocess.run(
+ ["git", "-C", str(repository), "worktree", "add", "-q", "--detach", str(linked)], check=True
+ )
+ state = tmp_path / "state"
+ first = create_cli_scan(state, tmp_path / "results", repository, target_revision=revision)
+ second = create_cli_scan(state, tmp_path / "results", linked, target_revision=revision)
+ legacy = create_cli_scan(state, tmp_path / "results", repository, target_revision=revision)
+ identity = target_state._repository_identity_details(repository)
+ assert identity is not None
+ previous = identity.previous_value or identity.value
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ connection.execute("UPDATE security_targets SET repository_identity=?", (previous,))
+ connection.execute("UPDATE scans SET repository_generation=?", (previous,))
+ connection.execute(
+ "UPDATE scans SET repository_generation=NULL WHERE id=?", (legacy["scanId"],)
+ )
+ connection.execute(
+ "DELETE FROM schema_migrations WHERE name = ?",
+ ("stabilize Linux repository generations",),
+ )
+ connection.commit()
+ before = [dict(row) for row in connection.execute("SELECT * FROM scans ORDER BY id")]
+ dump = list(connection.iterdump())
+ target_updates = []
+
+ def deny_generation_update(operation, table, column, *_):
+ if (
+ operation == sqlite3.SQLITE_UPDATE
+ and table == "security_targets"
+ and column == "repository_identity"
+ ):
+ target_updates.append(True)
+ return (
+ sqlite3.SQLITE_DENY
+ if operation == sqlite3.SQLITE_UPDATE
+ and table == "scans"
+ and column == "repository_generation"
+ else sqlite3.SQLITE_OK
+ )
+
+ # Only Linux converts the v2 format; other platforms still record the
+ # semantic migration without changing their existing generations.
+ if sys.platform == "linux":
+ connection.set_authorizer(deny_generation_update)
+ with pytest.raises(sqlite3.DatabaseError, match="not authorized"):
+ workbench.apply_migrations(connection)
+ connection.set_authorizer(lambda *_: sqlite3.SQLITE_OK)
+ assert target_updates
+ assert list(connection.iterdump()) == dump
+ # One authenticated checkout can update its shared generation even if a
+ # linked checkout is temporarily absent; no NULL-generation scan is joined.
+ linked.rename(tmp_path / "offline")
+ workbench.apply_migrations(connection)
+ assert {
+ row[0] for row in connection.execute("SELECT repository_identity FROM security_targets")
+ } == {identity.value}
+ assert [dict(row) for row in connection.execute("SELECT * FROM scans ORDER BY id")] == [
+ {
+ **row,
+ "repository_generation": None if row["id"] == legacy["scanId"] else identity.value,
+ }
+ for row in before
+ ]
+ scope = target_state.RepositoryIdentityCache(connection).scope_for_path(str(repository))
+ assert all(
+ scope.contains(
+ connection.execute("SELECT * FROM scans WHERE id=?", (scan["scanId"],)).fetchone()
+ )
+ for scan in (first, second, legacy)
+ )
+
+
[email protected]("old_version", (41, 42))
+def test_repository_generation_migration_keeps_released_null_history_local(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, old_version: int
+) -> None:
+ monkeypatch.syspath_prepend(str(SCRIPT.parent))
+ import workbench_db as workbench
+ import workbench_schema as schema
+ import workbench_target_state as target_state
+
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ state = tmp_path / "state"
+ scan = create_cli_scan(state, tmp_path / "results", repository, target_revision=revision)
+ with (
+ sqlite3.connect(state / "workbench.sqlite3") as source,
+ sqlite3.connect(":memory:") as connection,
+ ):
+ source.row_factory = connection.row_factory = sqlite3.Row
+ schema.apply_migrations(
+ connection,
+ tuple(item for item in schema.MIGRATIONS if item[0] <= old_version),
+ workbench.now,
+ lambda _: None,
+ )
+ for table in ("security_targets", "workspaces", "scans"):
+ columns = [row["name"] for row in connection.execute(f"PRAGMA table_info({table})")]
+ for row in source.execute(f"SELECT * FROM {table}"):
+ connection.execute(
+ f"INSERT INTO {table} ({', '.join(columns)}) VALUES ({', '.join('?' for _ in columns)})",
+ tuple(row[name] for name in columns),
+ )
+ before = dict(connection.execute("SELECT * FROM scans").fetchone())
+ workbench.apply_migrations(connection)
+ after = dict(connection.execute("SELECT * FROM scans").fetchone())
+ assert {name: after[name] for name in before} == before
+ assert after["repository_generation"] is None
+ assert after["completion_sequence"] == 1
+ assert (
+ connection.execute("SELECT MAX(version) FROM schema_migrations").fetchone()[0]
+ == workbench.MIGRATIONS[-1][0]
+ )
+ scope = target_state.RepositoryIdentityCache(connection).scope_for_path(str(repository))
+ assert scope.available and scope.contains(after)
+ assert after["id"] == scan["scanId"]
+
+
+ "alias_binding", ("unsaved", "stale", "current", "legacy", "unbound-owner")
+)
+def test_repository_generation_returns_after_offline_migration(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, alias_binding: str
+) -> None:
+ monkeypatch.syspath_prepend(str(SCRIPT.parent))
+ import workbench_db as workbench
+ import workbench_target_state as target_state
+
+ repository = tmp_path / "repository"
+ revision = initialize_git_repository(repository)
+ linked = tmp_path / "linked"
+ subprocess.run(
+ ["git", "-C", str(repository), "worktree", "add", "-q", "--detach", str(linked)],
+ check=True,
+ )
+ state = tmp_path / "state"
+ scan = create_cli_scan(state, tmp_path / "results", repository, target_revision=revision)
+ if alias_binding == "legacy":
+ create_cli_scan(state, tmp_path / "results", linked, target_revision=revision)
+ identity = target_state._repository_identity_details(repository)
+ assert identity is not None
+ previous = identity.previous_value or identity.value
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ if alias_binding in ("stale", "current"):
+ target_state.ensure_security_target(connection, str(linked))
+ connection.execute(
+ "UPDATE security_targets SET repository_identity=? WHERE current_path=?",
+ (previous, str(repository)),
+ )
+ connection.execute("UPDATE scans SET repository_generation=?", (previous,))
+ if alias_binding == "legacy":
+ connection.execute(
+ "UPDATE security_targets SET repository_identity=NULL WHERE current_path=?",
+ (str(linked),),
+ )
+ connection.execute(
+ "UPDATE scans SET repository_generation=NULL, target_device=NULL, target_inode=NULL WHERE target_path=?",
+ (str(linked),),
+ )
+ elif alias_binding == "stale":
+ connection.execute(
+ "UPDATE security_targets SET repository_identity=? WHERE current_path=?",
+ ("repository_sha256_" + "0" * 64, str(linked)),
+ )
+ if alias_binding == "unbound-owner":
+ connection.execute(
+ "UPDATE security_targets SET repository_identity=NULL WHERE current_path=?",
+ (str(repository),),
+ )
+ connection.execute(
+ "DELETE FROM schema_migrations WHERE name = ?",
+ ("stabilize Linux repository generations",),
+ )
+ offline = tmp_path / "offline"
+ repository.rename(offline)
+ workbench.apply_migrations(connection)
+ assert (
+ connection.execute("SELECT MAX(version) FROM schema_migrations").fetchone()[0]
+ == workbench.MIGRATIONS[-1][0]
+ )
+ assert (
+ connection.execute(
+ "SELECT repository_generation FROM scans WHERE id=?", (scan["scanId"],)
+ ).fetchone()[0]
+ == previous
+ )
+ offline.rename(repository)
+ workbench.apply_migrations(connection)
+ saved = connection.execute("SELECT * FROM scans WHERE id=?", (scan["scanId"],)).fetchone()
+ cache = target_state.RepositoryIdentityCache(connection)
+ assert cache.scope_for_scan(saved).contains(saved)
+ assert cache.scope_for_path(str(repository)).contains(saved)
+ if alias_binding != "stale":
+ assert cache.scope_for_path(str(linked)).contains(saved)
+ with connection:
+ registration = target_state.register_security_target(connection, str(linked))
+ expected = previous if alias_binding == "legacy" else identity.value
+ assert registration.repository_generation == expected
+ new_scan = create_cli_scan(state, tmp_path / "results", linked, target_revision=revision)
+ assert (
+ connection.execute(
+ "SELECT repository_generation FROM scans WHERE id=?", (new_scan["scanId"],)
+ ).fetchone()[0]
+ == expected
+ )
+ assert {
+ row[0]
+ for row in connection.execute(
+ "SELECT repository_generation FROM scans WHERE repository_generation IS NOT NULL"
+ )
+ } == {expected}
+ # An owner that can authenticate the complete binding converts all pending
+ # scans together; a NULL-generation historical scan remains local.
+ with connection:
+ target_state.register_security_target(connection, str(repository))
+ assert {
+ row[0] for row in connection.execute("SELECT repository_identity FROM security_targets")
+ } == (
+ {identity.value, None}
+ if alias_binding in ("legacy", "unbound-owner")
+ else {identity.value}
+ )
+ assert {
+ row[0] for row in connection.execute("SELECT repository_generation FROM scans")
+ } == ({identity.value, None} if alias_binding == "legacy" else {identity.value})
+ if sys.platform == "linux":
+ change_repository_metadata(monkeypatch, repository, "device")
+ updated = connection.execute("SELECT * FROM scans WHERE id=?", (scan["scanId"],)).fetchone()
+ target_state.require_scan_checkout_owner(connection, updated)
+
+
[email protected]("nested", (False, True))
+def test_repository_history_respects_native_parent_traversal(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, nested: bool
+) -> None:
+ monkeypatch.syspath_prepend(str(SCRIPT.parent))
+ import workbench_target_state as target_state
+
+ repository = tmp_path / "repository"
+ initialize_git_repository(repository)
+ child = tmp_path / "child"
+ child.mkdir()
+ (tmp_path / "other").mkdir()
+ state = tmp_path / "state"
+ scan = create_cli_scan(state, tmp_path / "results", repository, complete=False)
+ requested = str(child) + ("/../other" if nested else "") + "/../repository"
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ saved = connection.execute("SELECT * FROM scans WHERE id=?", (scan["scanId"],)).fetchone()
+ assert (
+ target_state.RepositoryIdentityCache(connection)
+ .scope_for_path(requested)
+ .contains(saved)
+ )
+ repository.rename(tmp_path / "offline")
+ assert (
+ target_state.RepositoryIdentityCache(connection)
+ .scope_for_path(requested)
+ .contains(saved)
+ )
+ child.rmdir()
+ assert target_state.RepositoryIdentityCache(connection).scope_for_path(
+ requested
+ ).available == (os.name == "nt")
+ assert (
+ target_state.RepositoryIdentityCache(connection)
+ .scope_for_path(str(repository))
+ .contains(saved)
+ )
+ child.write_text("ordinary file", encoding="utf-8")
+ assert target_state.RepositoryIdentityCache(connection).scope_for_path(
+ requested
+ ).available == (os.name == "nt")
+
+
def test_scan_list_honors_advertised_page_size(tmp_path: Path) -> None:
state_dir = tmp_path / "state"
repository = tmp_path / "repository"
diff --git a/plugins/codex-security/tests/test_workbench_scan_history_queries.py b/plugins/codex-security/tests/test_workbench_scan_history_queries.py
index b6f421b75..208958eb6 100644
--- a/plugins/codex-security/tests/test_workbench_scan_history_queries.py
+++ b/plugins/codex-security/tests/test_workbench_scan_history_queries.py
@@ -98,6 +98,8 @@ def link(db, previous, current, reason="Confirmed link."):
def test_legacy_repository_matching_excludes_confirmed_links_from_another_checkout(
workbench_api, workbench_db, tmp_path
):
+ for directory in ("selected", "other"):
+ (tmp_path / directory).mkdir()
for index, (scan, repository) in enumerate(
[
("foreign-before", "other"),
@@ -374,16 +376,16 @@ def test_matching_reuses_repository_membership_for_repeated_scans(
add_scan(workbench_db, "legacy-previous", previous_path)
add_scan(workbench_db, "another-legacy-previous", previous_path)
add_scan(workbench_db, "clone", tmp_path / "clone")
+ import workbench_target_state as target_state
+
+ inspect = target_state._inspect_repository_target
probes = []
- def probe(path, *arguments):
- probes.append((path, *arguments))
- if arguments[0] == "remote":
- remote = "other" if path == previous_path else "current"
- return f"https://example.test/{remote}.git"
- return None
+ def tracked_inspect(connection, target_id, path, *args, **kwargs):
+ probes.append((target_id, path))
+ return inspect(connection, target_id, path, *args, **kwargs)
- monkeypatch.setattr(history, "git_output", probe)
+ monkeypatch.setattr(target_state, "_inspect_repository_target", tracked_inspect)
selected = []
history.list_unmatched_scan_pairs(
workbench_db,
@@ -391,8 +393,61 @@ def test_matching_reuses_repository_membership_for_repeated_scans(
backfill_finding_details=lambda _, scan: selected.append(scan["id"]),
read_coverage=lambda _: {},
)
- assert sorted(selected) == ["clone", "current", "legacy-current", "moved"]
- assert len(probes) == len(set(probes))
+ assert sorted(selected) == ["current", "moved"]
+ assert probes == [("selected", str(repository))]
+
+
+def test_automatic_matching_uses_completion_order(workbench_api, workbench_db, tmp_path):
+ history = workbench_api["scan_history"]
+ db = workbench_db
+ for index in range(3):
+ scan = f"scan-{index}"
+ add_scan(db, scan, tmp_path, target_id="target", started_at=f"2026-08-01T0{index}:00:00Z")
+ add_occurrence(db, scan, scan)
+ db.execute(
+ "UPDATE scans SET completed_at = ? WHERE id = ?",
+ (f"2026-08-01T0{index + 3}:00:00Z", scan),
+ )
+
+ def planned(focus=None):
+ result = history.list_unmatched_scan_pairs(
+ db,
+ Namespace(repository=str(tmp_path), force=False, after_scan_id=focus),
+ backfill_finding_details=lambda *_: None,
+ read_coverage=lambda _: {},
+ )
+ return [
+ (before["scanId"], batch["afterScanId"])
+ for batch in result["batches"]
+ for before in batch["beforeScans"]
+ ], result["skippedPairs"]
+
+ db.execute(
+ "UPDATE scans SET status = 'running', completed_at = NULL, completion_sequence = NULL WHERE id = 'scan-0'"
+ )
+ assert planned("scan-2") == ([("scan-1", "scan-2")], 0)
+ db.execute(
+ "UPDATE scans SET status = 'complete', completed_at = '2026-08-01T06:00:00Z' WHERE id = 'scan-0'"
+ )
+ assert planned("scan-0") == ([("scan-1", "scan-0"), ("scan-2", "scan-0")], 0)
+ assert planned("scan-2") == ([("scan-1", "scan-2")], 0)
+ assert planned("scan-1") == ([], 0)
+
+ cache_comparison(db, "scan-2", "scan-0")
+ assert planned("scan-0") == ([("scan-1", "scan-0")], 1)
+ assert planned() == ([("scan-0", "scan-1"), ("scan-1", "scan-2")], 1)
+ db.execute("DELETE FROM scan_comparisons")
+ db.execute(
+ "UPDATE scans SET completion_sequence = NULL, completed_at = CASE id WHEN 'scan-0' THEN NULL "
+ "WHEN 'scan-1' THEN '2026-08-01T05:00:00+01:00' ELSE '2026-08-01T04:00:00Z' END"
+ )
+ assert planned("scan-0") == ([], 0)
+ assert planned("scan-1") == ([("scan-0", "scan-1")], 0)
+ assert planned("scan-2") == ([("scan-0", "scan-2"), ("scan-1", "scan-2")], 0)
+ legacy = dict(db.execute("SELECT * FROM scans WHERE id = 'scan-0'").fetchone())
+ order = history._scan_completion_order(legacy)
+ del legacy["completed_at"]
+ assert history._scan_completion_order(legacy) == order
def test_matching_reads_each_scan_once_and_skips_cached_links(
@@ -403,7 +458,7 @@ def test_matching_reads_each_scan_once_and_skips_cached_links(
repository = tmp_path / "repository"
for index in range(3):
scan = f"scan-{index}"
- add_scan(db, scan, repository, started_at=f"2026-01-01T00:00:0{index}Z")
+ add_scan(db, scan, repository, target_id="target", started_at=f"2026-01-01T00:00:0{index}Z")
add_occurrence(db, scan, scan)
queries = []
backfilled = []
@@ -458,7 +513,7 @@ def test_matching_reads_each_scan_once_and_skips_cached_links(
for index in (3, 4):
scan = f"scan-{index}"
- add_scan(db, scan, repository, started_at=f"2026-01-01T00:00:0{index}Z")
+ add_scan(db, scan, repository, target_id="target", started_at=f"2026-01-01T00:00:0{index}Z")
add_occurrence(db, scan, scan, finding_id=f"scan-{index - 3}")
def coverage(scan):
diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
index d1e787dc1..64ea6f4fb 100644
--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
@@ -7,6 +7,7 @@ import errno
import json
import os
import runpy
+import shutil
import sqlite3
import subprocess
import sys
@@ -113,6 +114,11 @@ EXPECTED_MIGRATIONS = [
(64, "persist scan log completion boundaries"),
(65, "bind Windows Deep Scan resume output directories"),
(66, "bind Windows Deep Scan resume checkouts"),
+ (67, "persist repository identities"),
+ (68, "stabilize Linux repository generations"),
+ (69, "distinguish unvalidated workspace targets"),
+ (70, "retain checkout roots for Git discovery"),
+ (71, "retain rejected workspace checkout boundaries"),
]
@@ -195,6 +201,74 @@ def create_historical_database(
return connection, apply_migrations
[email protected]("reopened", [False, True])
+def test_repository_previews_52_and_53_preserve_classification_and_import_schema(reopened) -> None:
+ connection, migrate = create_historical_database(52)
+ for preview, (canonical, name, sql) in zip((52, 53), SCHEMA.MIGRATIONS[66:68], strict=True):
+ apply_historical_migrations(connection, ((preview, name, sql),), "preview-time")
+ if reopened:
+ record_migration(connection, canonical, name, "later-open")
+
+ for _ in range(2):
+ migrate(connection)
+ assert [
+ tuple(row)
+ for row in connection.execute(
+ "SELECT version, name FROM schema_migrations ORDER BY version"
+ )
+ ] == [(version, name) for version, name, _ in SCHEMA.MIGRATIONS]
+ for table in (
+ "finding_severity_assessments",
+ "scan_severity_assessments",
+ "scan_severity_classifications",
+ ):
+ assert "classification_runs" in {
+ row["name"] for row in connection.execute(f"PRAGMA table_info({table})")
+ }
+ assert connection.execute("SELECT COUNT(*) FROM external_source_reports").fetchone()[0] == 0
+ assert {
+ row[0]
+ for row in connection.execute(
+ "SELECT applied_at FROM schema_migrations WHERE version IN (67, 68)"
+ )
+ } == {"preview-time"}
+
+
+def test_repository_preview_history_keeps_original_application_times_after_budget_upgrade():
+ connection, migrate = create_historical_database(72)
+ connection.execute("DROP TRIGGER retain_unverified_deep_scan_worker_attempt")
+ connection.execute("ALTER TABLE deep_scan_runs DROP COLUMN worker_usage_gap")
+ connection.execute("DELETE FROM schema_migrations WHERE version = 63")
+ for column in (
+ "log_completed_at",
+ "windows_resume_output_identity",
+ "windows_resume_target_identity",
+ ):
+ connection.execute(f"ALTER TABLE scans DROP COLUMN {column}")
+ connection.execute("DELETE FROM schema_migrations WHERE version BETWEEN 64 AND 66")
+ for version in range(67, 72):
+ connection.execute(
+ "UPDATE schema_migrations SET version = ?, applied_at = ? WHERE version = ?",
+ (version - 4, f"preview-{version - 4}", version),
+ )
+
+ migrate(connection)
+ migrate(connection)
+ assert [
+ tuple(row)
+ for row in connection.execute(
+ "SELECT version, applied_at FROM schema_migrations WHERE version >= 67 ORDER BY version"
+ )
+ ] == [(version, f"preview-{version - 4}") for version in range(67, 72)]
+ assert (
+ connection.execute("SELECT name FROM schema_migrations WHERE version = 63").fetchone()[0]
+ == "retain unverified Deep Scan worker attempts"
+ )
+ assert "worker_usage_gap" in {
+ row["name"] for row in connection.execute("PRAGMA table_info(deep_scan_runs)")
+ }
+
+
def insert_historical_row(
connection: sqlite3.Connection, table: str, timestamp: str, **columns: object
) -> None:
@@ -1146,6 +1220,103 @@ def test_workbench_backfills_repository_targets_only_during_migration() -> None:
backfill.assert_called_once_with(connection)
+def test_registration_retains_the_widest_observed_checkout(tmp_path: Path) -> None:
+ connection, migrate = create_historical_database(70)
+ with closing(connection):
+ migrate(connection)
+ target = tmp_path / "checkout" / "nested" / "target"
+ target.mkdir(parents=True)
+ register = migrate.__globals__["ensure_security_target"]
+
+ def registered_root() -> str | None:
+ target_id = register(connection, str(target))
+ return connection.execute(
+ "SELECT git_checkout_root FROM security_targets WHERE id = ?", (target_id,)
+ ).fetchone()[0]
+
+ assert registered_root() == str(target)
+ subprocess.run(["git", "init", "-q", str(target.parent)], check=True)
+ assert registered_root() == str(target.parent)
+ outer = target.parents[1]
+ subprocess.run(["git", "init", "-q", str(outer)], check=True)
+ assert registered_root() == str(outer)
+ (outer / ".git").rename(tmp_path / "retired-git-dir")
+ assert registered_root() == str(outer)
+
+
[email protected]("legacy_registration", ["target", "workspace"])
+def test_checkout_root_is_not_inferred_from_legacy_paths(
+ tmp_path: Path, legacy_registration: str
+) -> None:
+ target = tmp_path / "nested" / "target"
+ initialize_git_repository(target.parent)
+ target.mkdir()
+ connection, migrate = create_historical_database(70)
+ with closing(connection):
+ if legacy_registration == "target":
+ connection.execute(
+ "INSERT INTO security_targets (id, current_path, display_name, created_at, updated_at) "
+ "VALUES ('old', ?, 'Old target', 'created', 'updated')",
+ (str(target),),
+ )
+ else:
+ connection.execute(
+ "INSERT INTO workspaces (id, target_path, created_at, updated_at) "
+ "VALUES ('old', ?, 'created', 'updated')",
+ (str(target),),
+ )
+ migrate(connection)
+ for _ in range(2):
+ assert (
+ connection.execute("SELECT git_checkout_root FROM security_targets").fetchone()[0]
+ is None
+ )
+ migrate.__globals__["ensure_security_target"](connection, str(target))
+
+
[email protected]("binding", ["bound", "unbound", "dangling"])
+def test_unvalidated_target_migration_preserves_legacy_workspace_paths(
+ tmp_path: Path, binding: str
+) -> None:
+ connection, apply_migrations = create_historical_database(69)
+ with closing(connection):
+ target_path = str(tmp_path / "unavailable")
+ target_id = None if binding == "unbound" else "legacy-target"
+ if binding == "bound":
+ connection.execute(
+ "INSERT INTO security_targets(id, current_path, display_name, created_at, updated_at) "
+ "VALUES (?, ?, 'Legacy target', 'created', 'updated')",
+ (target_id, target_path),
+ )
+ connection.execute(
+ "INSERT INTO workspaces(id, target_id, target_path, created_at, updated_at) "
+ "VALUES ('legacy-workspace', ?, ?, 'created', 'updated')",
+ (target_id, target_path),
+ )
+ before = dict(connection.execute("SELECT * FROM workspaces").fetchone())
+ for _ in range(2):
+ apply_migrations(connection)
+ after = dict(connection.execute("SELECT * FROM workspaces").fetchone())
+ assert after == {
+ **before,
+ "target_id": after["target_id"],
+ "target_path_unvalidated": 0,
+ "rejected_checkout_roots_json": None,
+ }
+ assert after["target_id"] is not None
+ if binding == "bound":
+ assert after["target_id"] == target_id
+ assert (
+ connection.execute(
+ "SELECT current_path FROM security_targets WHERE id = ?", (after["target_id"],)
+ ).fetchone()[0]
+ == target_path
+ )
+
+
@pytest.mark.parametrize("complete", [False, True])
def test_scan_model_migration_preserves_existing_scans(complete: bool) -> None:
connection, apply_migrations = create_historical_database(25)
@@ -1233,12 +1404,18 @@ def test_log_completion_migration_preserves_unknown_legacy_boundaries() -> None:
apply_migrations(connection)
after = [dict(row) for row in connection.execute("SELECT * FROM scans ORDER BY id")]
+ (target_id,) = connection.execute(
+ "SELECT id FROM security_targets WHERE current_path = ?", ("/legacy/target",)
+ ).fetchone()
assert after == [
{
**row,
+ "target_id": target_id,
"log_completed_at": None,
"windows_resume_output_identity": None,
"windows_resume_target_identity": None,
+ "repository_generation": None,
+ "completion_sequence": 1 if row["status"] == "complete" else None,
}
for row in before
]
@@ -1269,7 +1446,7 @@ def test_scan_lifecycle_migration_recognizes_saved_recipes(
registered_version: str | None,
write_after_upgrade: bool,
) -> None:
- connection, apply_migrations = create_historical_database(62 if registered_version else 61)
+ connection, apply_migrations = create_historical_database(65 if registered_version else 64)
if write_after_upgrade:
apply_migrations(connection)
@@ -1558,6 +1735,7 @@ def test_workbench_creates_single_final_schema(tmp_path: Path) -> None:
"diff_resolution_id",
"capability_preflight_json",
"thread_id",
+ "target_path_unvalidated",
}
assert {row[1] for row in connection.execute("PRAGMA table_info(scans)")} >= {
@@ -2979,3 +3157,462 @@ def test_workbench_reconciles_profile_and_public_warning_histories(
).fetchone()
is not None
) is supported
+
+
[email protected]("installed", ["main", "repository-identities", "fresh"])
+def test_repository_identity_upgrade_keeps_named_scan_history(installed: str) -> None:
+ namespace = runpy.run_path(str(SCRIPT), run_name="codex_security_workbench_db")
+ with sqlite3.connect(":memory:") as connection:
+ connection.row_factory = sqlite3.Row
+ if installed != "fresh":
+ create_migration_history(connection)
+ historical = []
+ for version, name, sql in namespace["MIGRATIONS"]:
+ if installed == "main" and name == "persist repository identities":
+ continue
+ if installed == "repository-identities" and name == "editable scan names":
+ continue
+ historical.append(
+ (42 if name == "persist repository identities" else version, name, sql)
+ )
+ apply_historical_migrations(connection, historical, "synthetic-original")
+ else:
+ namespace["apply_schema_migrations"](
+ connection, namespace["MIGRATIONS"], namespace["now"], lambda _: None
+ )
+ connection.execute(
+ "INSERT INTO workspaces(id,created_at,updated_at) "
+ "VALUES ('synthetic-workspace','synthetic-created','synthetic-updated')"
+ )
+ connection.execute(
+ "INSERT INTO scans(id,workspace_id,target_path,target_revision,scope,mode,scan_dir,"
+ "status,phase,started_at,completed_at,created_at,updated_at) VALUES "
+ "('synthetic-scan','synthetic-workspace','/synthetic/repository','synthetic-revision',"
+ "'.','standard','/synthetic/output','complete','reporting',"
+ "'2026-01-01T00:00:00Z','2026-01-01T00:01:00Z','synthetic-created','synthetic-updated')"
+ )
+ saved_scan = dict(connection.execute("SELECT * FROM scans").fetchone())
+ timestamps = {
+ row["name"]: row["applied_at"]
+ for row in connection.execute("SELECT * FROM schema_migrations")
+ }
+ for _ in range(2):
+ namespace["apply_schema_migrations"](
+ connection, namespace["MIGRATIONS"], namespace["now"], lambda _: None
+ )
+ scan = dict(connection.execute("SELECT * FROM scans").fetchone())
+ assert saved_scan.items() <= scan.items()
+ assert scan["name"] is None
+ assert scan["repository_generation"] is None
+ assert scan["completion_sequence"] == 1
+ migrations = {
+ row["name"]: (row["version"], row["applied_at"])
+ for row in connection.execute("SELECT * FROM schema_migrations")
+ }
+ assert migrations["editable scan names"][0] == 42
+ assert migrations["persist repository identities"][0] == 67
+ for name, timestamp in timestamps.items():
+ assert migrations[name][1] == timestamp
+
+
[email protected]("spelling", ["canonical", "dot", "trailing"])
+def test_correcting_aliased_workspace_setup_retains_target(tmp_path: Path, spelling: str) -> None:
+ state = tmp_path / "state"
+ repository = tmp_path / "repository"
+ repository.mkdir()
+ canonical = str(repository)
+ original = (
+ canonical + ({"canonical": "", "dot": os.sep + "." + os.sep, "trailing": os.sep}[spelling])
+ )
+ workspace = str(uuid.uuid4())
+ create_workspace(state, workspace, "--target-path", original, "--scope", "missing")
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ before = connection.execute("SELECT id, current_path FROM security_targets").fetchall()
+ assert len(before) == 1
+ assert before[0][1] == original
+ save_workspace(state, workspace, canonical, ".", "standard")
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ assert (
+ connection.execute("SELECT id, current_path FROM security_targets").fetchall() == before
+ )
+ assert connection.execute(
+ "SELECT target_id, target_path, submitted FROM workspaces WHERE id = ?", (workspace,)
+ ).fetchone() == (before[0][0], canonical, 1)
+ scan_id, _ = start_workspace_scan(state, workspace, tmp_path / "scans")
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ assert (
+ connection.execute("SELECT target_id FROM scans WHERE id = ?", (scan_id,)).fetchone()[0]
+ == before[0][0]
+ )
+
+
[email protected]("before_version", [67, 68])
[email protected]("with_scan", [False, True])
+def test_identity_migrations_skip_unavailable_historical_git(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, before_version: int, with_scan: bool
+) -> None:
+ git = Path(shutil.which("git") or "git").resolve()
+ historical = str(git.parent)
+ monkeypatch.setenv("CODEX_SECURITY_GIT", str(git))
+ connection, apply_migrations = create_historical_database(before_version)
+ timestamp = "2026-08-01T00:00:00Z"
+ target_id, workspace_id = "historical-target", str(uuid.uuid4())
+ connection.execute(
+ "INSERT INTO security_targets(id, current_path, display_name, created_at, updated_at) "
+ "VALUES (?, ?, 'Historical tools', ?, ?)",
+ (target_id, historical, timestamp, timestamp),
+ )
+ connection.execute(
+ "INSERT INTO workspaces(id, target_id, target_path, created_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?)",
+ (workspace_id, target_id, historical, timestamp, timestamp),
+ )
+ old_identity = "repository_sha256_" + "1" * 64 if before_version == 68 else None
+ if old_identity is not None:
+ connection.execute("UPDATE security_targets SET repository_identity = ?", (old_identity,))
+ if with_scan:
+ connection.execute(
+ "INSERT INTO scans(id, workspace_id, target_id, target_path, target_revision, "
+ "scope, mode, scan_dir, status, phase, started_at, created_at, updated_at) "
+ "VALUES ('historical-scan', ?, ?, ?, 'unversioned', '.', 'standard', "
+ "'historical-output', 'complete', 'reporting', ?, ?, ?)",
+ (workspace_id, target_id, historical, timestamp, timestamp, timestamp),
+ )
+ apply_migrations(connection)
+ assert connection.execute("SELECT MAX(version) FROM schema_migrations").fetchone()[0] == 71
+ assert tuple(
+ connection.execute(
+ "SELECT id, current_path, repository_identity FROM security_targets"
+ ).fetchone()
+ ) == (target_id, historical, old_identity)
+ assert tuple(
+ connection.execute(
+ "SELECT target_id, target_path FROM workspaces WHERE id = ?", (workspace_id,)
+ ).fetchone()
+ ) == (target_id, historical)
+ if with_scan:
+ assert tuple(
+ connection.execute("SELECT id, target_id, target_path FROM scans").fetchone()
+ ) == ("historical-scan", target_id, historical)
+ with pytest.raises(SystemExit, match="outside the protected repository"):
+ apply_migrations.__globals__["ensure_security_target"](connection, historical)
+ state = tmp_path / "state"
+ state.mkdir(mode=0o700)
+ with sqlite3.connect(state / "workbench.sqlite3") as saved:
+ connection.backup(saved)
+ connection.close()
+ unrelated = tmp_path / "unrelated"
+ unrelated.mkdir()
+ workspace = create_saved_workspace(state, unrelated)
+ scan_id, _ = start_workspace_scan(state, str(workspace["id"]), tmp_path / "scans")
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ assert connection.execute(
+ "SELECT target_path FROM scans WHERE id = ?", (scan_id,)
+ ).fetchone()[0] == str(unrelated)
+
+
+def test_registration_keeps_a_competing_targets_existing_id(tmp_path: Path) -> None:
+ repository = tmp_path / "repository"
+ repository.mkdir()
+ connection, apply_migrations = create_historical_database(68)
+ connection.executescript("""
+ CREATE TRIGGER competing_target BEFORE INSERT ON security_targets
+ WHEN NEW.id <> 'existing-target'
+ BEGIN
+ INSERT INTO security_targets(id, current_path, display_name, created_at, updated_at)
+ VALUES ('existing-target', NEW.current_path, NEW.display_name, NEW.created_at, NEW.updated_at);
+ SELECT RAISE(IGNORE);
+ END;
+ """)
+ target = apply_migrations.__globals__["ensure_security_target"](connection, str(repository))
+ assert target == "existing-target"
+ assert [
+ tuple(row) for row in connection.execute("SELECT id, current_path FROM security_targets")
+ ] == [(target, str(repository))]
+
+
[email protected](os.name == "nt", reason="The executable marker fixture is a POSIX script")
[email protected]("configured", [False, True])
+ "executable_source",
+ [
+ "caller",
+ "historical",
+ "unavailable-historical",
+ "redirected-historical",
+ "redirected-destination",
+ ],
+)
+def test_identity_migration_excludes_all_history_and_caller_git(
+ tmp_path: Path, executable_source: str, configured: bool
+) -> None:
+ git = shutil.which("git")
+ assert git is not None
+ caller, saved, other = (tmp_path / name for name in ("caller", "saved", "other"))
+ for repository in (caller, saved, other):
+ initialize_git_repository(repository)
+ state = tmp_path / "state"
+ state.mkdir(mode=0o700)
+ connection, _ = create_historical_database(67)
+ timestamp = "2026-08-01T00:00:00Z"
+ destination = tmp_path / "destination"
+ if executable_source.startswith("redirected-"):
+ initialize_git_repository(destination)
+ (other / "subdir").symlink_to(destination, target_is_directory=True)
+ for index, repository in enumerate((saved, other)):
+ if index == 1 and executable_source.startswith("redirected-"):
+ repository = repository / "subdir"
+ if index == 1 and executable_source == "unavailable-historical":
+ (repository / "replaced-component").write_text("no longer a directory\n")
+ repository = repository / "replaced-component" / "old-target"
+ connection.execute(
+ "INSERT INTO security_targets(id, current_path, display_name, created_at, updated_at) "
+ "VALUES (?, ?, 'Saved repository', ?, ?)",
+ (f"saved-{index}", str(repository), timestamp, timestamp),
+ )
+ connection.commit()
+ with sqlite3.connect(state / "workbench.sqlite3") as database:
+ connection.backup(database)
+ connection.close()
+ executable_root = caller if executable_source == "caller" else other
+ if executable_source == "redirected-destination":
+ executable_root = destination
+ marker = tmp_path / "repository-git-ran"
+ executable = executable_root / "git"
+ executable.write_text(
+ '#!/bin/sh\nprintf invoked > "$MIGRATION_GIT_MARKER"\nexec "$MIGRATION_HOST_GIT" "$@"\n',
+ encoding="utf-8",
+ )
+ executable.chmod(0o700)
+ environment = {key: value for key, value in os.environ.items() if key != "CODEX_SECURITY_GIT"}
+ environment.update(
+ CODEX_SECURITY_STATE_DIR=str(state),
+ PATH=str(executable_root) + os.pathsep + os.environ.get("PATH", ""),
+ MIGRATION_GIT_MARKER=str(marker),
+ MIGRATION_HOST_GIT=git,
+ )
+ if configured:
+ environment["CODEX_SECURITY_GIT"] = git
+ completed = subprocess.run(
+ [sys.executable, str(SCRIPT), "list-scans"],
+ cwd=caller,
+ env=environment,
+ capture_output=True,
+ text=True,
+ check=True,
+ )
+ assert json.loads(completed.stdout)["scans"] == []
+ assert not marker.exists()
+ with sqlite3.connect(state / "workbench.sqlite3") as database:
+ identities = dict(database.execute("SELECT id, repository_identity FROM security_targets"))
+ assert (identities["saved-0"] is not None) is configured
+ assert (identities["saved-1"] is not None) is (
+ configured and executable_source != "unavailable-historical"
+ )
+
+
+ "completions,pending,expected",
+ [
+ pytest.param(
+ [("older", 2), ("newer", 3), ("last", 4)],
+ ["older"],
+ ["older", "newer", "last"],
+ id="earlier",
+ ),
+ pytest.param(
+ [("older", 2), ("newer", 3), ("last", 1)],
+ ["older"],
+ ["older", "newer", "last"],
+ id="earlier-with-reversed-recorded-clock",
+ ),
+ pytest.param(
+ [
+ ("first", 10),
+ ("middle", 20),
+ ("last", 30),
+ ("early", 5),
+ ("between", 25),
+ ("late", 40),
+ ],
+ ["early", "between", "late"],
+ ["early", "first", "middle", "between", "last", "late"],
+ id="before-between-after",
+ ),
+ pytest.param(
+ [("first", 20), ("middle", 10), ("last", 30), ("between", 25), ("late", 50)],
+ ["between", "late"],
+ ["first", "middle", "between", "last", "late"],
+ id="interleave-without-reordering-recorded-history",
+ ),
+ pytest.param(
+ [("first", 1), ("second", 2), ("latest", 3)],
+ ["latest"],
+ ["first", "second", "latest"],
+ id="later",
+ ),
+ ],
+)
+def test_identity_migration_merges_partially_sequenced_completion_history(
+ workbench_api, completions: list[tuple[str, int]], pending: list[str], expected: list[str]
+) -> None:
+ connection, apply_migrations = create_historical_database(68)
+ connection.execute(
+ "INSERT INTO workspaces(id, created_at, updated_at) VALUES ('workspace', 'created', 'updated')"
+ )
+ for scan_id, minute in completions:
+ insert_legacy_scan(
+ connection,
+ "2026-08-01T00:01:00Z" if scan_id == "older" else "2026-08-01T00:00:00Z",
+ id=scan_id,
+ workspace_id="workspace",
+ scan_dir=f"/synthetic/output/{scan_id}",
+ status="complete",
+ completed_at=f"2026-08-01T00:{minute:02}:00Z",
+ )
+ connection.executemany(
+ "UPDATE scans SET completion_sequence = NULL WHERE id = ?",
+ [(scan_id,) for scan_id in pending],
+ )
+ for _ in range(2):
+ apply_migrations(connection)
+ rows = connection.execute("SELECT * FROM scans ORDER BY completion_sequence").fetchall()
+ assert [row["id"] for row in rows] == expected
+ assert (
+ max(rows, key=workbench_api["scan_history"]._scan_completion_order)["id"]
+ == expected[-1]
+ )
+ connection.close()
+
+
+ "migration",
+ [
+ pytest.param("pre-release", marks=pytest.mark.cross_platform),
+ pytest.param(
+ "linux-v3",
+ marks=pytest.mark.skipif(sys.platform != "linux", reason="Linux generation conversion"),
+ ),
+ ],
+)
[email protected]("unavailable", ["file-parent", "permission-error", "bounded-missing"])
[email protected]("configured", [False, True])
+def test_identity_migrations_respect_git_availability_for_unavailable_history(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ migration: str,
+ unavailable: str,
+ configured: bool,
+) -> None:
+ repository = tmp_path / "healthy"
+ initialize_git_repository(repository)
+ connection, apply_migrations = create_historical_database(
+ 67 if migration == "pre-release" else 68
+ )
+ import workbench_target as target
+ import workbench_target_state as state
+ from filesystem_identity import serialize_filesystem_identity
+
+ identity = state._repository_identity_details(repository)
+ assert identity is not None
+ stored_identity = identity.value if migration == "pre-release" else identity.previous_value
+ assert stored_identity is not None
+ timestamp = apply_migrations.__globals__["now"]()
+ if migration == "pre-release":
+ connection.execute("ALTER TABLE security_targets ADD COLUMN repository_identity TEXT")
+ connection.execute(
+ "INSERT INTO schema_migrations VALUES (67, 'persist repository identities', ?)",
+ (timestamp,),
+ )
+ missing = tmp_path / "unavailable" / "old-checkout"
+ if unavailable == "file-parent":
+ missing.parent.write_text("no longer a directory\n")
+ elif unavailable == "bounded-missing":
+ initialize_git_repository(missing.parent)
+ else:
+ original = target._protected_repository_root
+
+ def inaccessible(path: Path) -> Path:
+ if path == missing:
+ raise PermissionError("historical checkout is inaccessible")
+ return original(path)
+
+ monkeypatch.setattr(target, "_protected_repository_root", inaccessible)
+ connection.executemany(
+ "INSERT INTO security_targets(id, current_path, display_name, created_at, updated_at, "
+ "repository_identity) VALUES (?, ?, 'Saved checkout', ?, ?, ?)",
+ [
+ ("healthy", str(repository), timestamp, timestamp, stored_identity),
+ ("unavailable", str(missing), timestamp, timestamp, None),
+ ],
+ )
+ connection.execute(
+ "INSERT INTO workspaces(id, target_id, target_path, created_at, updated_at) "
+ "VALUES ('workspace', 'healthy', ?, ?, ?)",
+ (str(repository), timestamp, timestamp),
+ )
+ metadata = repository.stat()
+ connection.execute(
+ "INSERT INTO scans(id, workspace_id, target_id, target_path, target_revision, scope, mode, "
+ "scan_dir, status, phase, started_at, completed_at, created_at, updated_at, "
+ "target_device, target_inode) "
+ "VALUES ('scan', 'workspace', 'healthy', ?, 'revision', '.', 'standard', "
+ "'/synthetic/output', 'complete', 'reporting', ?, ?, ?, ?, ?, ?)",
+ (
+ str(repository),
+ timestamp,
+ timestamp,
+ timestamp,
+ timestamp,
+ serialize_filesystem_identity(metadata.st_dev),
+ serialize_filesystem_identity(metadata.st_ino),
+ ),
+ )
+ if migration == "linux-v3":
+ connection.execute("UPDATE scans SET repository_generation = ?", (stored_identity,))
+ monkeypatch.delenv("CODEX_SECURITY_GIT", raising=False)
+ if configured:
+ host_git = shutil.which("git")
+ assert host_git is not None
+ monkeypatch.setenv("CODEX_SECURITY_GIT", host_git)
+ # The healthy legacy target has a saved Git identity but no recorded checkout root.
+ expected_identity = (
+ identity.value if configured else None if migration == "pre-release" else stored_identity
+ )
+ for _ in range(2):
+ apply_migrations(connection)
+ assert (
+ connection.execute(
+ "SELECT repository_identity FROM security_targets WHERE id = 'healthy'"
+ ).fetchone()[0]
+ == expected_identity
+ )
+ if migration == "linux-v3":
+ assert (
+ connection.execute("SELECT repository_generation FROM scans").fetchone()[0]
+ == expected_identity
+ )
+ connection.close()
+
+
+def test_current_database_does_not_inspect_historical_git_paths(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ connection, apply_migrations = create_historical_database(68)
+ connection.executemany(
+ "INSERT INTO security_targets(id, current_path, display_name, created_at, updated_at) "
+ "VALUES (?, ?, 'Saved checkout', 'created', 'updated')",
+ [(str(index), f"/synthetic/history/{index}") for index in range(100)],
+ )
+ import workbench_target as target
+
+ monkeypatch.delenv("CODEX_SECURITY_GIT", raising=False)
+ with mock.patch.object(
+ target, "_protected_repository_root", wraps=target._protected_repository_root
+ ) as inspect_root:
+ apply_migrations(connection)
+ inspect_root.assert_not_called()
+ assert connection.execute("SELECT COUNT(*) FROM security_targets").fetchone()[0] == 100
+ connection.close()
diff --git a/plugins/codex-security/tests/test_workbench_target.py b/plugins/codex-security/tests/test_workbench_target.py
index 806d6445c..bf1aa82e7 100644
--- a/plugins/codex-security/tests/test_workbench_target.py
+++ b/plugins/codex-security/tests/test_workbench_target.py
@@ -6,11 +6,14 @@ import difflib
import errno
import hashlib
import io
+import json
import os
import runpy
import shutil
+import sqlite3
import stat
import subprocess
+import sys
from pathlib import Path
from types import SimpleNamespace
from typing import Any, Callable, cast
@@ -1870,6 +1873,424 @@ def test_git_discovery_continues_past_repository_tools_and_batch_shims(
trusted_git_executable(repository)
[email protected]("windows", [False, True])
+def test_scoped_git_discovery_pins_all_target_roots_and_restores_discovery(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, windows: bool
+) -> None:
+ first, second, host = (tmp_path / name for name in ("first", "second", "host"))
+ for root in (first, second, host):
+ root.mkdir()
+ for repository in (first, second):
+ (repository / ".git").mkdir()
+ name = "git.exe" if windows else "git"
+ for root in (second, host):
+ (root / name).write_text("synthetic executable fixture\n")
+ (root / name).chmod(0o700)
+ monkeypatch.setitem(
+ trusted_git_executable.__globals__,
+ "sys",
+ SimpleNamespace(platform="win32" if windows else "linux"),
+ )
+ monkeypatch.delenv("CODEX_SECURITY_GIT", raising=False)
+ monkeypatch.setenv("PATH", os.pathsep.join(map(str, (second, host))))
+ with WORKBENCH_TARGET["scoped_git_discovery"](lambda: (first, second)):
+ assert trusted_git_executable(first) == str(host / name)
+ monkeypatch.setenv("PATH", str(second))
+ assert trusted_git_executable(second) == str(host / name)
+ assert trusted_git_executable(first) == str(second / name)
+
+
[email protected]("windows", [False, True])
+def test_unvalidated_workspace_paths_exclude_tools_without_blocking_host_git(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, windows: bool
+) -> None:
+ repository, candidate, host = (tmp_path / name for name in ("repository", "candidate", "host"))
+ (repository / ".git").mkdir(parents=True)
+ host.mkdir()
+ name = "git.exe" if windows else "git"
+ (host / name).write_text("synthetic executable fixture\n")
+ (host / name).chmod(0o700)
+ monkeypatch.setitem(
+ trusted_git_executable.__globals__,
+ "sys",
+ SimpleNamespace(platform="win32" if windows else "linux"),
+ )
+ monkeypatch.delenv("CODEX_SECURITY_GIT", raising=False)
+ monkeypatch.setenv("PATH", os.pathsep.join(map(str, (candidate, host))))
+ for exists in (False, True):
+ if exists:
+ candidate.mkdir()
+ (candidate / name).write_text("synthetic repository executable fixture\n")
+ (candidate / name).chmod(0o700)
+ with WORKBENCH_TARGET["scoped_git_discovery"](
+ lambda: (repository,), recorded_git_roots=lambda: (candidate,)
+ ):
+ assert trusted_git_executable(repository) == str(host / name)
+
+
[email protected]("windows", [False, True])
[email protected]("recorded", [False, True])
+def test_scoped_git_discovery_excludes_unavailable_target_ancestor_tools(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, windows: bool, recorded: bool
+) -> None:
+ repository, historical, host = (
+ tmp_path / name for name in ("repository", "historical", "host")
+ )
+ (repository / ".git").mkdir(parents=True)
+ surviving = historical / "surviving"
+ surviving.mkdir(parents=True)
+ host.mkdir()
+ name = "git.exe" if windows else "git"
+ for root in (historical, host):
+ (root / name).write_text("synthetic executable fixture\n")
+ (root / name).chmod(0o700)
+ monkeypatch.setitem(
+ trusted_git_executable.__globals__,
+ "sys",
+ SimpleNamespace(platform="win32" if windows else "linux"),
+ )
+ monkeypatch.delenv("CODEX_SECURITY_GIT", raising=False)
+ monkeypatch.setenv("PATH", os.pathsep.join(map(str, (historical, host))))
+ saved = surviving if recorded else surviving / "missing"
+ with WORKBENCH_TARGET["scoped_git_discovery"](
+ lambda: (repository, saved),
+ recorded_git_roots=lambda: None if recorded else (),
+ ):
+ assert trusted_git_executable(repository) is None
+
+ # A recorded outer boundary keeps the historical shim excluded after target deletion.
+ with WORKBENCH_TARGET["scoped_git_discovery"](
+ lambda: (repository, saved), recorded_git_roots=lambda: (historical,)
+ ):
+ assert trusted_git_executable(repository) == str(host / name)
+
+ # The caller can still explicitly select a tool outside the current repository.
+ monkeypatch.setenv("CODEX_SECURITY_GIT", str(historical / name))
+ with WORKBENCH_TARGET["scoped_git_discovery"](
+ lambda: (repository, saved),
+ recorded_git_roots=lambda: None if recorded else (),
+ ):
+ assert trusted_git_executable(repository) == str(historical / name)
+
+
[email protected](os.name == "nt", reason="uses a POSIX executable shim")
[email protected]("operation", ["current-query", "missing-query", "missing-migration"])
[email protected]("shim_directory", [".", "bin"])
[email protected]("configured", [False, True])
+def test_workbench_commands_exclude_saved_checkout_git(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ workbench_api: dict[str, Any],
+ operation: str,
+ shim_directory: str,
+ configured: bool,
+) -> None:
+ host_git = shutil.which("git")
+ assert host_git is not None
+ requested, historical = (tmp_path / name for name in ("requested", "historical"))
+ for repository in (requested, historical):
+ initialize_git_repository(repository)
+ historical_target = historical
+ if operation.startswith("missing-"):
+ shutil.rmtree(historical / ".git")
+ (historical / "surviving").mkdir()
+ historical_target = historical / "surviving" / "missing"
+ state = tmp_path / "state"
+ state.mkdir(mode=0o700)
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ apply_migrations = workbench_api["apply_migrations"]
+ with monkeypatch.context() as migration_patch:
+ if operation == "missing-migration":
+ # Reproduce a database written before repository identities were recorded.
+ migration_patch.setitem(
+ apply_migrations.__globals__,
+ "MIGRATIONS",
+ tuple(
+ migration for migration in workbench_api["MIGRATIONS"] if migration[0] < 53
+ ),
+ )
+ apply_migrations(connection)
+ connection.executemany(
+ "INSERT INTO security_targets(id, current_path, display_name, created_at, updated_at) "
+ "VALUES (?, ?, 'Saved checkout', 'created', 'updated')",
+ [("requested", str(requested)), ("historical", str(historical_target))],
+ )
+ marker = tmp_path / "repository-git-ran"
+ tools = historical / shim_directory
+ tools.mkdir(exist_ok=True)
+ executable = tools / "git"
+ executable.write_text(
+ '#!/bin/sh\nprintf invoked > "$WORKBENCH_GIT_MARKER"\nexec "$WORKBENCH_HOST_GIT" "$@"\n'
+ )
+ executable.chmod(0o700)
+ environment = {key: value for key, value in os.environ.items() if key != "CODEX_SECURITY_GIT"}
+ environment.update(
+ CODEX_SECURITY_STATE_DIR=str(state),
+ PATH=str(tools) + os.pathsep + os.environ.get("PATH", ""),
+ WORKBENCH_GIT_MARKER=str(marker),
+ WORKBENCH_HOST_GIT=host_git,
+ )
+ if configured:
+ environment["CODEX_SECURITY_GIT"] = host_git
+ arguments = (
+ ["list-scans"]
+ if operation == "missing-migration"
+ else ["list-global-findings", "--repository", str(requested)]
+ )
+ script = Path(__file__).resolve().parents[1] / "scripts" / "workbench_db.py"
+ completed = subprocess.run(
+ [sys.executable, str(script), *arguments],
+ cwd=requested,
+ env=environment,
+ capture_output=True,
+ text=True,
+ check=True,
+ )
+ key = "scans" if operation == "missing-migration" else "findings"
+ assert json.loads(completed.stdout)[key] == []
+ assert not marker.exists()
+ if operation == "missing-migration":
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ identities = dict(
+ connection.execute("SELECT id, repository_identity FROM security_targets")
+ )
+ assert (identities["requested"] is not None) is configured
+ assert identities["historical"] is None
+
+
[email protected](os.name == "nt", reason="uses a POSIX executable shim")
+ "evidence",
+ [
+ "identity",
+ "generation",
+ "revision",
+ "legacy-revision",
+ "legacy-unborn",
+ "legacy-workspace",
+ "legacy-directory",
+ "directory",
+ ],
+)
[email protected]("configured", [False, True])
+def test_saved_git_boundary_removal_does_not_select_its_parent_tool(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ workbench_api: dict[str, Any],
+ evidence: str,
+ configured: bool,
+) -> None:
+ from workbench_target_state import repository_identity
+
+ host_git = shutil.which("git")
+ assert host_git is not None
+ requested, historical = (tmp_path / name for name in ("requested", "historical"))
+ initialize_git_repository(requested)
+ if evidence in {"legacy-unborn", "legacy-workspace"}:
+ subprocess.run([host_git, "init", "-q", str(historical)], check=True)
+ revision = WORKBENCH_TARGET["git_revision"](historical)
+ assert revision == "unversioned"
+ elif evidence in {"directory", "legacy-directory"}:
+ historical.mkdir()
+ revision = "unversioned"
+ else:
+ revision = initialize_git_repository(historical)
+ saved = historical / "surviving" / "component"
+ saved.mkdir(parents=True)
+ identity = repository_identity(saved)
+ if identity is None and evidence in {"identity", "generation"}:
+ pytest.skip("filesystem does not expose repository generation evidence")
+ state = tmp_path / "state"
+ state.mkdir(mode=0o700)
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ apply_migrations = workbench_api["apply_migrations"]
+ with monkeypatch.context() as migration_patch:
+ if evidence.startswith("legacy-"):
+ last_version = 52 if evidence == "legacy-revision" else 60
+ migration_patch.setitem(
+ apply_migrations.__globals__,
+ "MIGRATIONS",
+ tuple(item for item in workbench_api["MIGRATIONS"] if item[0] <= last_version),
+ )
+ apply_migrations(connection)
+ register = workbench_api["register_security_target"]
+ historical_id = register(connection, str(saved)).target_id
+ if not evidence.startswith("legacy-") or evidence == "legacy-revision":
+ register(connection, str(requested))
+ if evidence in {"identity", "generation", "revision"}:
+ # These rows predate recorded roots, but retain distinct kinds of Git evidence.
+ connection.execute(
+ "UPDATE security_targets SET git_checkout_root = NULL, repository_identity = ? "
+ "WHERE id = ?",
+ (identity if evidence == "identity" else None, historical_id),
+ )
+ if evidence != "identity":
+ connection.execute(
+ "INSERT INTO workspaces(id, target_id, target_path, created_at, updated_at) "
+ "VALUES ('workspace', ?, ?, 'created', 'updated')",
+ (historical_id, str(saved)),
+ )
+ if evidence not in {"identity", "legacy-workspace"}:
+ connection.execute(
+ "INSERT INTO scans(id, workspace_id, target_id, target_path, target_revision, "
+ "scope, mode, scan_dir, status, phase, started_at, created_at, updated_at) "
+ "VALUES ('scan', 'workspace', ?, ?, ?, '.', 'standard', "
+ "?, 'complete', 'reporting', 'created', 'created', 'updated')",
+ (
+ historical_id,
+ str(saved),
+ "unversioned" if evidence == "generation" else revision,
+ str(tmp_path / "saved-scan"),
+ ),
+ )
+ if evidence == "generation":
+ connection.execute("UPDATE scans SET repository_generation = ?", (identity,))
+ if evidence not in {"directory", "legacy-directory"}:
+ shutil.rmtree(historical / ".git")
+ tools = (
+ tmp_path / "host-tools"
+ if evidence in {"directory", "legacy-directory"}
+ else historical / "bin"
+ )
+ tools.mkdir()
+ executable = tools / "git"
+ executable.write_text('#!/bin/sh\nprintf invoked > "$0.ran"\nexec "$WORKBENCH_HOST_GIT" "$@"\n')
+ executable.chmod(0o700)
+ environment = {key: value for key, value in os.environ.items() if key != "CODEX_SECURITY_GIT"}
+ environment.update(
+ CODEX_SECURITY_STATE_DIR=str(state),
+ PATH=str(tools) + os.pathsep + os.environ.get("PATH", ""),
+ WORKBENCH_HOST_GIT=host_git,
+ )
+ pinned = tmp_path / "trusted-git"
+ if configured:
+ shutil.copy(executable, pinned)
+ environment["CODEX_SECURITY_GIT"] = str(pinned)
+ script = Path(__file__).resolve().parents[1] / "scripts" / "workbench_db.py"
+ completed = subprocess.run(
+ [sys.executable, str(script), "list-scans", "--repository", str(requested)],
+ cwd=requested,
+ env=environment,
+ capture_output=True,
+ text=True,
+ check=True,
+ )
+ assert json.loads(completed.stdout)["scans"] == []
+ assert executable.with_suffix(".ran").exists() is (evidence == "directory" and not configured)
+ assert pinned.with_suffix(".ran").exists() is configured
+ if evidence == "legacy-directory":
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ register(connection, str(saved))
+ assert (
+ connection.execute(
+ "SELECT git_checkout_root FROM security_targets WHERE id = ?", (historical_id,)
+ ).fetchone()[0]
+ is None
+ )
+ if evidence == "legacy-revision":
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ identities = dict(
+ connection.execute("SELECT current_path, repository_identity FROM security_targets")
+ )
+ assert (identities[str(requested)] is not None) is configured
+ assert identities[str(saved)] is None
+
+
[email protected](os.name == "nt", reason="uses a POSIX executable shim")
[email protected](("migrate", "configured"), [(False, False), (True, False), (True, True)])
[email protected]("spelling", ["absolute", "tilde"])
+def test_new_workspace_skips_git_in_its_unregistered_checkout(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ workbench_api: dict[str, Any],
+ migrate: bool,
+ configured: bool,
+ spelling: str,
+) -> None:
+ host_git = shutil.which("git")
+ assert host_git is not None
+ repository = tmp_path / "repository"
+ initialize_git_repository(repository)
+ tools = repository / "bin"
+ tools.mkdir()
+ marker = tmp_path / "repository-git-ran"
+ executable = tools / "git"
+ executable.write_text(
+ '#!/bin/sh\nprintf invoked > "$WORKBENCH_GIT_MARKER"\nexec "$WORKBENCH_HOST_GIT" "$@"\n'
+ )
+ executable.chmod(0o700)
+ state = tmp_path / "state"
+ if migrate:
+ saved = tmp_path / "saved"
+ initialize_git_repository(saved)
+ state.mkdir(mode=0o700)
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ apply_migrations = workbench_api["apply_migrations"]
+ with monkeypatch.context() as migration_patch:
+ migration_patch.setitem(
+ apply_migrations.__globals__,
+ "MIGRATIONS",
+ tuple(item for item in workbench_api["MIGRATIONS"] if item[0] < 54),
+ )
+ apply_migrations(connection)
+ connection.execute(
+ "INSERT INTO security_targets(id, current_path, display_name, created_at, updated_at) "
+ "VALUES ('saved', ?, 'Saved checkout', '2099-01-01T00:00:00Z', '2099-01-01T00:00:00Z')",
+ (str(saved),),
+ )
+ caller = tmp_path / "caller"
+ caller.mkdir()
+ environment = {key: value for key, value in os.environ.items() if key != "CODEX_SECURITY_GIT"}
+ environment.update(
+ CODEX_SECURITY_STATE_DIR=str(state),
+ PATH=str(tools) + os.pathsep + os.environ.get("PATH", ""),
+ WORKBENCH_GIT_MARKER=str(marker),
+ WORKBENCH_HOST_GIT=host_git,
+ )
+ if configured:
+ environment["CODEX_SECURITY_GIT"] = host_git
+ script = Path(__file__).resolve().parents[1] / "scripts" / "workbench_db.py"
+ requested_path = (
+ str(repository)
+ if spelling == "absolute"
+ else "~/" + os.path.relpath(repository, Path.home())
+ )
+ completed = subprocess.run(
+ [
+ sys.executable,
+ str(script),
+ "create-workspace",
+ "--workspace-id",
+ "00000000-0000-4000-8000-000000000001",
+ "--target-path",
+ requested_path,
+ ],
+ cwd=caller,
+ env=environment,
+ capture_output=True,
+ text=True,
+ check=True,
+ )
+ assert json.loads(completed.stdout)["targetPath"] == str(repository)
+ assert not marker.exists()
+ with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ identities = dict(
+ connection.execute("SELECT current_path, repository_identity FROM security_targets")
+ )
+ assert len(identities) == (2 if migrate else 1)
+ if migrate:
+ assert (identities[str(saved)] is not None) is configured
+ # Only an explicit Git can inspect a state with an unknown legacy boundary.
+ assert (identities[str(repository)] is not None) is (configured or not migrate)
+
+
def test_git_discovery_does_not_invoke_through_repository_directory_alias(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.md
index 57409bb69..4d8779927 100644
--- a/sdk/typescript/docs/cli.md
+++ b/sdk/typescript/docs/cli.md
@@ -1012,7 +1012,9 @@ credentials, and workflow resume behavior.
For `dedupe --scan latest`, matching across worktrees or clones requires a Git
executable outside all saved scan targets. If a historical target includes the
available Git installation, use `codex-security dedupe --scan SCAN_ID` with an
-explicit saved scan ID. Exact-path `latest` lookup still works without Git.
+explicit saved scan ID. Exact-path `latest` lookup works without Git for non-Git
+scan targets; recorded Git generations require a trusted Git executable to verify
+that the checkout still owns its saved history.
Repeat `--codex KEY=VALUE` for supported native settings. For example,
`--codex "model_reasoning_effort='high'"` passes the TOML string `high` in Bash,
@@ -2083,19 +2085,29 @@ attach only their owner's session. Unrecorded earlier retry sessions may be abse
Commands default to the current repository. IDs accept unique prefixes of at
least eight characters.
-| Command | Purpose |
-| ----------------------------------------------------- | ---------------------------------------------------------- |
-| `scans list [REPOSITORY]` | List scans; `--scan-root DIR` filters artifact roots. |
-| `scans show [SCAN_ID]` | Show a scan; defaults to latest completed. |
-| `scans logs [SCAN_ID]` | Show session events; defaults to latest, including active. |
-| `scans resume SCAN_ID` | Continue an interrupted Deep Scan. |
-| `scans rerun [SCAN_ID]` | Repeat on the current checkout. |
-| `scans match BEFORE AFTER` | Link findings with the same root cause. |
-| `scans match --all` | Match completed scans across worktrees/clones. |
-| `scans compare [BEFORE] [AFTER]` | Compare scans; defaults to latest two completed. |
-| `findings list [REPOSITORY]` | List open findings. |
-| `findings imported [REPOSITORY]` | Read locally imported, unassessed vendor evidence. |
-| `findings false-positive OCCURRENCE_ID --reason TEXT` | Dismiss a finding while the reason applies. |
+Linked Git worktrees share scans, findings, and reviewer decisions when their
+scans record the same repository generation in the selected state database.
+Older scans without this evidence remain target-local. Removed worktrees retain
+their recorded history; a reused checkout path does not inherit its previous
+owner's history. Separately verified clones can still be compared explicitly,
+but their finding histories remain separate.
+
+Finding confirmation and automatic post-scan matching follow completion order
+in the workbench, without changing sealed report timestamps.
+
+| Command | Purpose |
+| ----------------------------------------------------- | -------------------------------------------------------------------------------- |
+| `scans list [REPOSITORY]` | List scans; `--scan-root DIR` filters artifact roots. |
+| `scans show [SCAN_ID]` | Show a scan; defaults to latest completed. |
+| `scans logs [SCAN_ID]` | Show session events; defaults to latest, including active. |
+| `scans resume SCAN_ID` | Continue an interrupted Deep Scan. |
+| `scans rerun [SCAN_ID]` | Repeat on the current checkout. |
+| `scans match BEFORE AFTER` | Link findings with the same root cause. |
+| `scans match --all` | Match completed scans in this repository generation, including linked worktrees. |
+| `scans compare [BEFORE] [AFTER]` | Compare scans; defaults to latest two completed. |
+| `findings list [REPOSITORY]` | List open findings. |
+| `findings imported [REPOSITORY]` | Read locally imported, unassessed vendor evidence. |
+| `findings false-positive OCCURRENCE_ID --reason TEXT` | Dismiss a finding while the reason applies. |
In the terminal, `findings list` shows the occurrence ID for each open finding.
Copy that ID into `findings false-positive` with your reason. Subsequent scans can
@@ -2158,6 +2170,17 @@ History lives in `$CODEX_SECURITY_STATE_DIR/workbench.sqlite3`, or
writable, and outside the target repository. Session logs may contain sensitive
data even though scan recipes do not store credentials.
+Keep the same state directory across linked worktrees. Changing it selects
+separate history, artifacts, and a dedicated Codex sign-in. `--scan-root`
+filters recorded artifact paths; it does not import another directory's scans.
+
+The workbench records the outer checkout directory when it registers a target.
+A later nested checkout cannot make tools from that directory eligible for
+automatic discovery. When an older saved target lacks a recorded checkout
+directory, set `CODEX_SECURITY_GIT` to the absolute path of a trusted Git
+executable outside the checkouts. If you use a system Python instead of the
+Codex managed runtime, set `PYTHON` to the absolute path of your interpreter.
+
Saved scan logs retain the finishing turn, including its final response, and
exclude later turns once the scan records a completion boundary. Older scans
retain the full history of their attributed sessions and keep their existing
diff --git a/sdk/typescript/docs/findings-service.md b/sdk/typescript/docs/findings-service.md
index 4c2460d96..df84201df 100644
--- a/sdk/typescript/docs/findings-service.md
+++ b/sdk/typescript/docs/findings-service.md
@@ -44,13 +44,21 @@ select logical findings using their current stored bodies; dedupe does not
replace newer bodies with old scan artifacts. A local database does not include
findings stored only in a separate Docker volume or remote service.
-A scan or workflow selector is required. `--scan` also accepts a unique ID
-prefix. The scan must have sealed artifacts and a local checkout available.
-Local `latest` lookup matches the exact checkout path without Git. Matching
-across worktrees or clones additionally requires a Git executable outside all
-saved scan targets. If an unrelated historical target includes the available
-Git installation, use `codex-security dedupe --scan SCAN_ID` with an explicit
-saved scan ID instead.
+A scan or workflow selector is required. Use a full scan ID, a unique prefix, or
+`latest` for the current repository. The scan must be complete and its artifacts
+sealed. Reviews that read source also require the local checkout.
+
+Local `latest` verifies the checkout generation and component scope. Non-Git
+targets match by exact path. Git history, including matches across worktrees or
+clones, requires a Git executable outside all saved checkout directories. For a
+legacy Git target without a recorded checkout directory, set `CODEX_SECURITY_GIT`
+to an absolute, trusted Git executable. If a saved checkout contains the
+available Git installation, select the scan by its explicit ID.
+
+For older history, use an absolute `PYTHON` path when selecting system Python.
+If Bun cannot locate a trusted Node.js helper, run local deduplication with
+Node.js.
+
By default, candidates come from its manifest's `scan.target.targetId`. Use
`--all-repositories` to search the whole selected database or service.
diff --git a/sdk/typescript/scripts/fixtures/package-deep-spawn.mjs b/sdk/typescript/scripts/fixtures/package-deep-spawn.mjs
index f75f1f2db..8030decef 100644
--- a/sdk/typescript/scripts/fixtures/package-deep-spawn.mjs
+++ b/sdk/typescript/scripts/fixtures/package-deep-spawn.mjs
@@ -11,12 +11,16 @@ const script = fileURLToPath(
);
export function installFixtureSpawn(executable) {
- const selected = win32.toNamespacedPath(executable);
const spawn = childProcess.spawn;
childProcess.spawn = (command, args, options) =>
spawn(
command,
- win32.toNamespacedPath(command) === selected ? [script, ...args] : args,
+ win32.toNamespacedPath(command) ===
+ win32.toNamespacedPath(
+ options?.env?.PACKAGE_DEEP_EXECUTABLE ?? executable,
+ )
+ ? [script, ...args]
+ : args,
options,
);
syncBuiltinESMExports();
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index bc6bbab97..03023c252 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -4051,9 +4051,7 @@ export class CodexSecurity {
scanId,
repository: repo,
previousFindings: previousFindings.filter(
- (finding) =>
- finding["scanId"] !== scanId &&
- finding["targetId"] === targetId,
+ (finding) => finding["scanId"] !== scanId,
),
falsePositives: falsePositiveExamples as Record<string, unknown>[],
findings: result.findings.findings,
@@ -5869,20 +5867,28 @@ export class CodexSecurity {
export async function listRepositoryFindings(
workbench: (args: readonly string[]) => Promise<JsonObject>,
- targetId: string,
+ target: string | { repository: string },
status: "open" | "all" = "open",
): Promise<JsonObject[] | undefined> {
const findings: JsonObject[] = [];
+ const selector =
+ typeof target === "string"
+ ? ["--target-id", target]
+ : ["--repository", target.repository];
let offset: number | undefined;
do {
const page = await workbench([
"list-global-findings",
- "--target-id",
- targetId,
+ ...selector,
...(status === "open" ? ["--status", "open"] : []),
...(offset === undefined ? [] : ["--offset", String(offset)]),
]);
- if (!Array.isArray(page["findings"])) return undefined;
+ if (
+ page["projectionAvailable"] === false ||
+ !Array.isArray(page["findings"])
+ ) {
+ return undefined;
+ }
findings.push(...(page["findings"] as JsonObject[]));
offset =
typeof page["nextOffset"] === "number" ? page["nextOffset"] : undefined;
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index eda7efad8..7dec91a46 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -158,7 +158,11 @@ import {
resolvePatchReviewPublisher,
} from "./patch-publication.js";
import { DEFAULT_DEDUPE_CONCURRENCY } from "./deduplication/deduplication.js";
-import { savedScanWorkbench } from "./saved-scan-bootstrap.js";
+import {
+ savedCheckoutBoundaries,
+ savedDiscoveryRoots,
+ savedScanWorkbench,
+} from "./saved-scan-bootstrap.js";
import { deduplicateScanInternal } from "./deduplication/scan.js";
import { runHostRecordsProtocol } from "./host-records-protocol.js";
import { runHostedScanProtocol } from "./hosted-scan-protocol.js";
@@ -315,12 +319,12 @@ import {
canonicalizeModelSafePath,
codexSecurityCredentialAllowsAmbientImport,
codexSecurityCredentialHome,
- canonicalConfigPath,
codexSecurityHasStoredFileCredentials,
codexSecurityStateDirectory,
logoutCredentialHome,
executablePathForSpawn,
environmentWithGit,
+ isPythonPathCandidate,
expandHome,
resolveInputPath,
resolveParentTraversal,
@@ -330,7 +334,6 @@ import {
pluginMetadata,
runWorkbench,
workbenchEnvironment,
- sameFile,
setCodexSecurityCredentialLogout,
type CodexCommand,
type PluginPythonOptions,
@@ -1647,10 +1650,12 @@ const DEFAULT_DEPENDENCIES: CliDependencies = {
) => {
const checkingTarget =
args[0] === "get-scan" && args.includes("--check-target");
+ const configuredGit = environmentEntry(process.env, "CODEX_SECURITY_GIT");
let environment: NodeJS.ProcessEnv =
!checkingTarget || protectedRoot === undefined
? {
...exportEnvironment(),
+ CODEX_SECURITY_GIT: configuredGit,
CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(),
}
: workbenchEnvironment(process.env);
@@ -1665,18 +1670,42 @@ const DEFAULT_DEPENDENCIES: CliDependencies = {
(await gitMarkerRoot(root, signal, "outermost")) ?? root,
),
);
+ const configuredPython =
+ pythonPath ?? environmentEntry(environment, "PYTHON");
+ const boundaries =
+ configuredPython && isPythonPathCandidate(configuredPython)
+ ? undefined
+ : await savedCheckoutBoundaries(environment);
const python = await resolvePluginPython({
configuredPath: pythonPath,
environment,
signal,
protectedRoot: protectedRoot === undefined ? undefined : protectedRoots,
+ discoveryProtectedRoots: boundaries
+ ? await savedDiscoveryRoots(
+ boundaries,
+ [...boundaries.byTarget.keys()],
+ signal,
+ )
+ : undefined,
+ allowPathLookup: boundaries?.complete,
});
- if (checkingTarget && protectedRoot !== undefined) {
- const git = await inspectTrustedExecutable("git", environment, [
- ...protectedRoots,
- ...gitProtectedRoots,
- ]);
- environment = environmentWithGit(git.environment, git);
+ if (
+ checkingTarget &&
+ gitProtectedRoots.length > 0 &&
+ (configuredGit === undefined || isAbsolute(configuredGit))
+ ) {
+ const git = await inspectTrustedExecutable(
+ configuredGit ?? "git",
+ environment,
+ [...protectedRoots, ...gitProtectedRoots],
+ );
+ // Filter knowledge-base tools, then let the workbench choose ambient Git
+ // after it also knows the boundaries recorded in the history database.
+ environment =
+ configuredGit !== undefined || git.executable === null
+ ? environmentWithGit(git.environment, git)
+ : git.environment;
}
const workspace =
pluginPath === undefined
@@ -2605,7 +2634,7 @@ export async function main(
runScanImport(options, errorOutput, dependencies),
);
const history = async (
- args: readonly string[],
+ args: readonly string[] | (() => Promise<JsonObject>),
select: (value: JsonObject) => JsonObject | Promise<JsonObject> = (value) =>
value,
pythonPath?: string,
@@ -2613,13 +2642,15 @@ export async function main(
): Promise<JsonObject> => {
try {
return await select(
- await dependencies.runWorkbench(
- args,
- undefined,
- undefined,
- pythonPath,
- protectedRoot,
- ),
+ await (typeof args === "function"
+ ? args()
+ : dependencies.runWorkbench(
+ args,
+ undefined,
+ undefined,
+ pythonPath,
+ protectedRoot,
+ )),
);
} catch (error) {
errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);
@@ -2854,56 +2885,38 @@ export async function main(
canonicalPath === undefined
? (storedPath ?? requestedPath)
: requestedPath;
- return await history(
- ["list-repositories", "--status", "scanned"],
- async (value): Promise<JsonObject> => {
- const repositories = value["repositories"] as JsonObject[];
- let target =
- repositories.find(
- (entry) => entry["targetPath"] === requestedRepository,
- ) ??
- repositories.find((entry) => entry["targetPath"] === storedPath);
- if (target === undefined && storedPath !== undefined) {
- const canonical =
- canonicalPath ??
- (await canonicalConfigPath(storedPath, {
- verifyIdentity: true,
- }).catch(() => undefined));
- const saved = repositories.find(
- (entry) => entry["targetPath"] === canonical,
- );
- if (
- saved !== undefined &&
- (canonicalPath === undefined ||
- (await sameFile(canonicalPath, requestedRepository)))
- ) {
- target = saved;
- }
- }
- if (target === undefined) {
- for (const entry of repositories) {
- const storedPath = entry["targetPath"] as string;
- if (await sameFile(storedPath, requestedRepository)) {
- target = entry;
- break;
- }
- }
- }
- const findings =
- target === undefined
- ? []
- : await listRepositoryFindings(
- dependencies.runWorkbench,
- target["targetId"] as string,
- );
- return presentHistory(
- { repository: requestedRepository, findings: findings ?? [] },
- "findings",
- format,
- { missingRepositoryHistory: target === undefined },
+ return await history(async () => {
+ const findings = await listRepositoryFindings(
+ dependencies.runWorkbench,
+ {
+ repository: requestedRepository,
+ },
+ );
+ if (findings === undefined) {
+ throw new CodexSecurityError(
+ "Repository findings are unavailable for the requested checkout.",
);
- },
- );
+ }
+ let missingRepositoryHistory = false;
+ if (findings.length === 0 && useInteractiveHistory(format)) {
+ const saved = await dependencies.runWorkbench(
+ workbenchArgs("list-scans", {
+ repository: requestedRepository,
+ limit: "1",
+ }),
+ );
+ missingRepositoryHistory =
+ (saved["scans"] as SavedScan[]).length === 0;
+ }
+ return presentHistory(
+ { repository: requestedRepository, findings },
+ "findings",
+ format,
+ {
+ missingRepositoryHistory,
+ },
+ );
+ });
},
});
findingFeedback.command("imported", {
@@ -3321,8 +3334,18 @@ export async function main(
"SCAN_IMPORT_FAILED",
);
}
+ const replay =
+ args.scanId === undefined && isJsonObject(recipe)
+ ? {
+ ...saved,
+ recipe: {
+ ...recipe,
+ repository: dependencies.currentDirectory(),
+ },
+ }
+ : saved;
scanArguments = await prepareScanArgumentsFromRecipe(
- saved,
+ replay,
"rerun",
dependencies.currentDirectory(),
dependencies.environment,
@@ -12716,7 +12739,7 @@ function printScanSummary(
repositoryFindings?.filter((finding) => finding.confirmedInLatestScan)
.length ?? 0;
const findingSummary = repositoryFindings?.length
- ? `${confirmedCount} confirmed this scan; ${findingCount - confirmedCount} previously found; ${severitySummary}`
+ ? `${confirmedCount} confirmed in latest repository scan; ${findingCount - confirmedCount} previously found; ${severitySummary}`
: severitySummary;
const findingColor =
findingCount === 0
diff --git a/sdk/typescript/src/deduplication/local.ts b/sdk/typescript/src/deduplication/local.ts
index 8075e07b3..7def95598 100644
--- a/sdk/typescript/src/deduplication/local.ts
+++ b/sdk/typescript/src/deduplication/local.ts
@@ -6,6 +6,7 @@ import type {
FindingSearchScope,
} from "../finding-retrieval.js";
import { CodexSecurityError } from "../errors.js";
+import type { SavedToolDiscovery } from "../saved-scan-bootstrap.js";
import { workflowDigest } from "../finding-workflow.js";
import {
bundledPluginRoot,
@@ -50,6 +51,7 @@ export class LocalDeduplication {
private readonly workbench: typeof runWorkbench = runWorkbench,
embedding?: FindingEmbeddingBinding,
onEmbeddingUsage?: EmbeddingUsageObserver,
+ private readonly nodeDiscovery?: SavedToolDiscovery,
) {
const endpoint =
environmentEntry(environment, "CODEX_SECURITY_EMBEDDINGS_URL") ||
@@ -139,6 +141,14 @@ export class LocalDeduplication {
private async command(payload: { action: string; [key: string]: unknown }) {
this.signal?.throwIfAborted();
this.options ??= (async () => {
+ // Bun finds Node on PATH. Native Node helpers use the running process executable.
+ if (
+ process.versions["bun"] &&
+ this.nodeDiscovery?.allowPathLookup === false
+ )
+ throw new CodexSecurityError(
+ "Could not establish the saved checkout boundary for Node.js discovery. Run local deduplication with Node.js.",
+ );
const environment = workbenchEnvironment(this.environment);
return {
pluginRoot: await bundledPluginRoot(),
@@ -146,6 +156,7 @@ export class LocalDeduplication {
environment,
signal: this.signal,
protectedRoot: [
+ ...(this.nodeDiscovery?.protectedRoots ?? []),
this.repositoryPath,
...(await gitProtectionRoots(this.repositoryPath, this.signal)),
...(await gitProtectionRoots(process.cwd(), this.signal)),
diff --git a/sdk/typescript/src/deduplication/scan.ts b/sdk/typescript/src/deduplication/scan.ts
index ccb782534..6c5c3bb5a 100644
--- a/sdk/typescript/src/deduplication/scan.ts
+++ b/sdk/typescript/src/deduplication/scan.ts
@@ -12,7 +12,10 @@ import {
resolveCompletedScan,
type SavedScanDependencies,
} from "../saved-scan.js";
-import { savedScanWorkbench } from "../saved-scan-bootstrap.js";
+import {
+ savedScanWorkbench,
+ type SavedToolDiscovery,
+} from "../saved-scan-bootstrap.js";
import { CodexReviewRunner, reviewSqliteHome } from "./codex-review.js";
import { mkdir } from "node:fs/promises";
import { isWithin } from "../trusted-executable.js";
@@ -168,6 +171,7 @@ export async function deduplicateScanInternal(
: dependencies,
pluginRoot,
false,
+ bootstrap?.nodeDiscovery,
);
}
@@ -179,6 +183,7 @@ async function deduplicateResolvedScan(
dependencies: DeduplicateScanDependencies,
pluginRoot: string,
bindRepository: boolean,
+ nodeDiscovery?: SavedToolDiscovery,
): Promise<DeduplicateScanResult> {
const environment = dependencies.environment ?? process.env;
if (options.embedding !== undefined && options.findingsUrl !== undefined)
@@ -213,6 +218,7 @@ async function deduplicateResolvedScan(
workbench,
options.embedding,
options.onEmbeddingUsage,
+ nodeDiscovery,
)
: undefined;
const client =
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index 36f324d0a..93d17c0e0 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -218,6 +218,10 @@ export interface PluginPythonOptions {
homeDirectory?: string;
managedRuntimeRoots?: readonly string[];
protectedRoot?: string | readonly string[];
+ /** @internal Additional roots excluded only from ambient interpreter discovery. */
+ discoveryProtectedRoots?: readonly string[];
+ /** @internal Ambient PATH lookup requires a known boundary for saved Git targets. */
+ allowPathLookup?: boolean;
currentDirectory?: string;
signal?: AbortSignal;
}
@@ -2126,7 +2130,23 @@ export async function runWorkbench(
(process.platform === "win32" ||
(args.includes("--archive-existing") &&
!args.includes("--archived-scan-dir")));
- stdout = await run(args, input, registrationHandshake);
+ try {
+ stdout = await run(args, input, registrationHandshake);
+ } catch (error) {
+ const focusedIndex = args.indexOf("--after-scan-id");
+ if (
+ args[0] !== "list-unmatched-scan-pairs" ||
+ focusedIndex === -1 ||
+ !/unrecognized arguments: --after-scan-id(?:\s+\S+)?$/u.test(
+ processErrorDetail(error),
+ )
+ )
+ throw error;
+ options.signal?.throwIfAborted();
+ const legacyArgs = [...args];
+ legacyArgs.splice(focusedIndex, 2);
+ stdout = await run(legacyArgs, input, registrationHandshake);
+ }
} catch (error) {
if (options.signal?.aborted) throw error;
const detail = processErrorDetail(error);
@@ -4063,7 +4083,10 @@ export async function resolvePluginPython(
}
// Named interpreters are ambient PATH discovery, even when PYTHON names one.
// Explicit trusted paths and managed runtimes retain their existing precedence.
- const discoveryRoots = [...protectedRoot];
+ const discoveryRoots = [
+ ...protectedRoot,
+ ...(options.discoveryProtectedRoots ?? []),
+ ];
for (const directory of callerDirectories) {
const physical =
directory === cwd
@@ -4078,7 +4101,14 @@ export async function resolvePluginPython(
)
discoveryRoots.push(directory);
}
+ const missingBoundaryMessage =
+ "Could not establish the saved checkout boundary for Python discovery. Set PYTHON to an absolute trusted interpreter.";
if (options.configuredPath !== undefined) {
+ if (
+ !isPythonPathCandidate(options.configuredPath) &&
+ options.allowPathLookup === false
+ )
+ throw new PluginPythonUnavailableError(missingBoundaryMessage);
return await requirePython(
options.configuredPath,
"configured plugin Python",
@@ -4091,6 +4121,8 @@ export async function resolvePluginPython(
}
const inherited = environmentValue(environment, "PYTHON", true);
if (inherited) {
+ if (!isPythonPathCandidate(inherited) && options.allowPathLookup === false)
+ throw new PluginPythonUnavailableError(missingBoundaryMessage);
const roots = isPythonPathCandidate(inherited)
? protectedRoot
: discoveryRoots;
@@ -4154,6 +4186,8 @@ export async function resolvePluginPython(
}
}
+ if (options.allowPathLookup === false)
+ throw new PluginPythonUnavailableError(missingBoundaryMessage);
for (const candidate of process.platform === "win32"
? ["python", "python3", "py"]
: ["python3", "python"]) {
diff --git a/sdk/typescript/src/saved-scan-bootstrap.ts b/sdk/typescript/src/saved-scan-bootstrap.ts
index fcfe89db1..ec88af842 100644
--- a/sdk/typescript/src/saved-scan-bootstrap.ts
+++ b/sdk/typescript/src/saved-scan-bootstrap.ts
@@ -1,8 +1,8 @@
import { isRecord } from "./record.js";
-import { compareTimestamps } from "./timestamps.js";
+import type { JsonValue } from "./config.js";
import { environmentEntry } from "./auth.js";
import { inspectTrustedExecutable } from "./trusted-executable.js";
-import { realpath } from "node:fs/promises";
+import { access } from "node:fs/promises";
import { isAbsolute, join, resolve } from "node:path";
import {
gitHistoryIdentity,
@@ -15,6 +15,7 @@ import {
codexSecurityStateDirectory,
resolvePluginPython,
runWorkbench,
+ sameFile,
workbenchEnvironment,
type ProcessEnvironment,
sqliteDatabaseOpener,
@@ -24,10 +25,28 @@ import {
interface ScanTarget {
id: string;
target_path: string;
- target_id?: string | null;
+ repository_generation?: string | null;
+ originFallback?: boolean;
+ protectPython?: boolean;
}
-/** Read only enough history to protect Python discovery; the workbench still validates the scan. */
+interface SavedCheckout {
+ target_path: string;
+ checkout_root: string | null;
+}
+
+interface SavedBoundaries {
+ byTarget: Map<string, string | null>;
+ roots: string[];
+ complete: boolean;
+}
+
+export interface SavedToolDiscovery {
+ protectedRoots: readonly string[];
+ allowPathLookup: boolean;
+}
+
+/** Read saved checkout boundaries before starting tools. The workbench validates the scan. */
export async function savedScanWorkbench(
requestedId: string | { workflowId: string },
options: {
@@ -40,13 +59,14 @@ export async function savedScanWorkbench(
SavedScanDependencies["runWorkbench"] & {
environment: ProcessEnvironment;
readonly protectedRoots: readonly string[];
+ readonly nodeDiscovery: SavedToolDiscovery;
}
> {
options.signal?.throwIfAborted();
const environment: ProcessEnvironment = workbenchEnvironment(
options.environment,
);
- const targets = await readTargets(
+ const { targets, checkouts } = await readTargets(
requestedId,
environment,
options.currentDirectory,
@@ -80,17 +100,29 @@ export async function savedScanWorkbench(
options.currentDirectory,
options.signal,
);
+ const liveTargets = targets.filter(
+ (target) => target.protectPython !== false,
+ );
const protectedRoots = [
...callerRoots,
- ...targets.map((row) => row.target_path),
+ ...liveTargets.flatMap((row) => {
+ const root = checkouts.byTarget.get(row.target_path);
+ return root ? [root] : [];
+ }),
+ ...liveTargets.map((row) => row.target_path),
...(
await Promise.all(
- targets.map((row) =>
+ liveTargets.map((row) =>
gitProtectionRoots(row.target_path, options.signal),
),
)
).flat(),
];
+ const discoveryProtectedRoots = await savedDiscoveryRoots(
+ checkouts,
+ [options.currentDirectory, ...targets.map((row) => row.target_path)],
+ options.signal,
+ );
let python: string | undefined;
const workbench: SavedScanDependencies["runWorkbench"] = async (
args,
@@ -98,9 +130,8 @@ export async function savedScanWorkbench(
signal = options.signal,
) => {
signal?.throwIfAborted();
- // Return the selected ID directly: probing Python must not revisit unrelated history.
- if (requestedId === "latest" && args[0] === "list-scans")
- return { scans: targets.map((target) => ({ scanId: target.id })) };
+ const selectingLatest =
+ requestedId === "latest" && args[0] === "list-scans";
const target =
args[0] === "get-scan"
? latest
@@ -111,26 +142,72 @@ export async function savedScanWorkbench(
throw new CodexSecurityError(
"Saved scan history changed during lookup. Retry the command.",
);
- // Pin the interpreter selected with every candidate and the caller checkout protected.
+ // Pin one interpreter; ambient discovery also excludes historical candidate paths.
python ??= await resolvePluginPython({
environment,
protectedRoot: protectedRoots,
+ discoveryProtectedRoots,
+ allowPathLookup: checkouts.complete,
currentDirectory: options.currentDirectory,
signal,
});
environment["PYTHON"] = python;
- const result = await runWorkbench(
- {
- environment,
- pluginRoot: options.pluginRoot,
- python,
- signal,
- protectedRoot: protectedRoots,
- currentDirectory: options.currentDirectory,
- failureMessage: "Could not read Codex Security scan history",
- },
+ const execute = (command: readonly string[]) =>
+ runWorkbench(
+ {
+ environment,
+ pluginRoot: options.pluginRoot,
+ python,
+ signal,
+ protectedRoot: protectedRoots,
+ currentDirectory: options.currentDirectory,
+ failureMessage: "Could not read Codex Security scan history",
+ },
+ command,
+ input,
+ );
+ if (selectingLatest) {
+ // Let the workbench enforce the caller's persisted generation and component scope,
+ // including history whose original linked worktree has since been removed.
+ const scoped = new Map<string, JsonValue[]>();
+ const scansAt = async (repository: string): Promise<JsonValue[]> => {
+ let scans = scoped.get(repository);
+ if (scans === undefined) {
+ const result = await execute([
+ "list-scans",
+ "--repository",
+ repository,
+ "--status",
+ "complete",
+ ]);
+ scans = Array.isArray(result["scans"]) ? result["scans"] : [];
+ scoped.set(repository, scans);
+ }
+ return scans;
+ };
+ const callerScans = await scansAt(options.currentDirectory);
+ for (const candidate of targets) {
+ let selected = callerScans.find(
+ (scan) => isRecord(scan) && scan["scanId"] === candidate.id,
+ );
+ // Dedupe also accepts the same component of an independent same-origin clone.
+ // Validate that clone's current ownership before using its saved history.
+ if (selected === undefined && candidate.originFallback) {
+ selected = (await scansAt(candidate.target_path)).find(
+ (scan) => isRecord(scan) && scan["scanId"] === candidate.id,
+ );
+ }
+ if (selected !== undefined) return { scans: [selected] };
+ }
+ if (environment["CODEX_SECURITY_GIT"] === "") {
+ throw new CodexSecurityError(
+ "Could not verify the saved checkout automatically without a trusted Git executable. Use an explicit saved scan ID: codex-security dedupe --scan SCAN_ID",
+ );
+ }
+ return { scans: [] };
+ }
+ const result = await execute(
target ? ["get-scan", "--scan-id", target.id] : args,
- input,
);
if (typeof requestedId !== "string" && args[0] === "finding-workflow") {
const workflow = result["workflow"];
@@ -156,106 +233,212 @@ export async function savedScanWorkbench(
return Object.assign(workbench, {
environment,
protectedRoots: [options.currentDirectory, ...protectedRoots],
+ nodeDiscovery: {
+ protectedRoots: discoveryProtectedRoots,
+ allowPathLookup: checkouts.complete,
+ },
});
}
-function openSavedScanDatabase(
+async function readTargets(
+ requestedId: string | { workflowId: string },
environment: ProcessEnvironment,
-): RuntimeDatabase {
- const openDatabase = sqliteDatabaseOpener("read-existing");
- return openDatabase(
- join(codexSecurityStateDirectory(environment), "workbench.sqlite3"),
+ currentDirectory: string,
+ signal?: AbortSignal,
+): Promise<{ targets: ScanTarget[]; checkouts: SavedBoundaries }> {
+ try {
+ const latest = requestedId === "latest";
+ const snapshot = readSnapshot(
+ workbenchDatabasePath(environment),
+ (database) => {
+ const targets = latest
+ ? completedScans(database)
+ : exactTargets(database, requestedId);
+ const checkouts = savedCheckouts(database);
+ return { targets, checkouts };
+ },
+ );
+ if (latest)
+ snapshot.targets = await latestTargets(
+ snapshot.targets,
+ currentDirectory,
+ environment,
+ snapshot.checkouts,
+ signal,
+ );
+ return snapshot;
+ } catch (error) {
+ signal?.throwIfAborted();
+ if (error instanceof CodexSecurityError) throw error;
+ throw new CodexSecurityError(
+ `Could not read saved scan targets before Python discovery: ${errorMessage(error)}`,
+ { cause: error },
+ );
+ }
+}
+
+function workbenchDatabasePath(environment: ProcessEnvironment): string {
+ return join(codexSecurityStateDirectory(environment), "workbench.sqlite3");
+}
+
+/** @internal Read executable exclusions before launching an ordinary history command. */
+export async function savedCheckoutBoundaries(
+ environment: ProcessEnvironment,
+): Promise<SavedBoundaries | undefined> {
+ const path = workbenchDatabasePath(environment);
+ try {
+ await access(path);
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
+ throw error;
+ }
+ return readSnapshot(path, (database) => savedCheckouts(database));
+}
+
+/** @internal Exclude both recorded checkout roots and the currently visible checkouts. */
+export async function savedDiscoveryRoots(
+ checkouts: SavedBoundaries | undefined,
+ paths: readonly string[],
+ signal?: AbortSignal,
+): Promise<string[]> {
+ const targets = [...new Set([...paths, ...(checkouts?.roots ?? [])])];
+ const currentRoots = await Promise.all(
+ targets.map((path) => gitProtectionRoots(path, signal)),
);
+ return [...new Set([...targets, ...currentRoots.flat()])];
}
export function readSavedScanLifecycle(
environment: ProcessEnvironment,
scanId: string,
): unknown {
- const database = openSavedScanDatabase(environment);
+ return readSnapshot(
+ workbenchDatabasePath(environment),
+ (database) => {
+ const [row] = readRows<{ snapshot: string }>(
+ database,
+ "SELECT snapshot FROM scan_lifecycles WHERE scan_id = ?",
+ scanId,
+ );
+ return row && JSON.parse(row.snapshot);
+ },
+ 0,
+ );
+}
+
+function readSnapshot<T>(
+ path: string,
+ select: (database: RuntimeDatabase) => T,
+ timeout: 0 | 5000 = 5000,
+): T {
+ const database = sqliteDatabaseOpener("read-existing")(path);
try {
- database.exec("PRAGMA busy_timeout = 0");
+ database.exec(`PRAGMA busy_timeout = ${timeout}`);
database.exec("PRAGMA query_only = ON");
- const [row] = readRows<{ snapshot: string }>(
- database,
- "SELECT snapshot FROM scan_lifecycles WHERE scan_id = ?",
- scanId,
- );
- return row && JSON.parse(row.snapshot);
+ // Select scans and their saved boundaries from the same snapshot.
+ database.exec("BEGIN");
+ return select(database);
} finally {
database.close();
}
}
-async function readTargets(
+function exactTargets(
+ database: RuntimeDatabase,
requestedId: string | { workflowId: string },
- environment: ProcessEnvironment,
- currentDirectory: string,
- signal?: AbortSignal,
-): Promise<ScanTarget[]> {
- let database: RuntimeDatabase | undefined;
- try {
- database = openSavedScanDatabase(environment);
- database.exec("PRAGMA busy_timeout = 5000");
- database.exec("PRAGMA query_only = ON");
- if (typeof requestedId !== "string") {
- const columns = readRows<{ name: string }>(
- database,
- "PRAGMA table_info(finding_workflows)",
- );
- const scanId = columns.some((column) => column.name === "scan_id")
- ? "workflow.scan_id"
- : "json_extract(workflow.state_json, '$.scanId')";
- return readRows<ScanTarget>(
- database,
- `SELECT scans.id, scans.target_path FROM scans
+): ScanTarget[] {
+ if (typeof requestedId !== "string") {
+ const scanId = tableColumns(database, "finding_workflows").has("scan_id")
+ ? "workflow.scan_id"
+ : "json_extract(workflow.state_json, '$.scanId')";
+ return readRows<ScanTarget>(
+ database,
+ `SELECT scans.id, scans.target_path FROM scans
JOIN finding_workflows AS workflow ON scans.id = ${scanId} WHERE workflow.id = ?`,
- requestedId.workflowId,
- );
- }
- if (requestedId === "latest")
- return await latestTargets(
- database,
- currentDirectory,
- environment,
- signal,
- );
- // Match uuid.UUID's accepted full-ID spellings before considering a prefix.
- const compact = trimBoundary(
- requestedId.replaceAll("urn:", "").replaceAll("uuid:", ""),
- "{",
- "}",
- ).replaceAll("-", "");
- if (/^[0-9a-f]{32}$/i.test(compact)) {
- const id = compact
- .toLowerCase()
- .replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, "$1-$2-$3-$4-$5");
- return readRows<ScanTarget>(
- database,
- "SELECT id, target_path FROM scans WHERE id = ?",
- id,
- );
- }
- if (requestedId.length < 8)
- throw new CodexSecurityError(
- "Scan ID prefixes must be at least eight characters.",
- );
+ requestedId.workflowId,
+ );
+ }
+ // Match uuid.UUID's accepted full-ID spellings before considering a prefix.
+ const compact = trimBoundary(
+ requestedId.replaceAll("urn:", "").replaceAll("uuid:", ""),
+ "{",
+ "}",
+ ).replaceAll("-", "");
+ if (/^[0-9a-f]{32}$/i.test(compact)) {
+ const id = compact
+ .toLowerCase()
+ .replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, "$1-$2-$3-$4-$5");
return readRows<ScanTarget>(
database,
- "SELECT id, target_path FROM scans WHERE substr(id, 1, ?) = ? LIMIT 2",
- requestedId.length,
- requestedId.toLowerCase(),
+ "SELECT id, target_path FROM scans WHERE id = ?",
+ id,
);
- } catch (error) {
- signal?.throwIfAborted();
- if (error instanceof CodexSecurityError) throw error;
+ }
+ if (requestedId.length < 8)
throw new CodexSecurityError(
- `Could not read saved scan targets before Python discovery: ${errorMessage(error)}`,
- { cause: error },
+ "Scan ID prefixes must be at least eight characters.",
);
- } finally {
- database?.close();
+ return readRows<ScanTarget>(
+ database,
+ "SELECT id, target_path FROM scans WHERE substr(id, 1, ?) = ? LIMIT 2",
+ requestedId.length,
+ requestedId.toLowerCase(),
+ );
+}
+
+function tableColumns(database: RuntimeDatabase, table: string): Set<string> {
+ return new Set(
+ readRows<{ name: string }>(database, `PRAGMA table_info(${table})`).map(
+ (row) => row.name,
+ ),
+ );
+}
+
+/** Read registered boundaries without assuming an unversioned scan was outside Git. */
+function savedCheckouts(database: RuntimeDatabase): SavedBoundaries {
+ const scan = tableColumns(database, "scans");
+ const registry = tableColumns(database, "security_targets");
+ const workspace = tableColumns(database, "workspaces");
+ const sources = [];
+ if (scan.has("target_path"))
+ sources.push("SELECT target_path, NULL AS checkout_root FROM scans");
+ if (workspace.has("target_path")) {
+ const validated = workspace.has("target_path_unvalidated")
+ ? " AND (target_path_unvalidated = 0 OR target_id IS NOT NULL)"
+ : "";
+ sources.push(`SELECT target_path, NULL AS checkout_root FROM workspaces
+ WHERE target_path IS NOT NULL${validated}`);
+ }
+ if (registry.has("current_path")) {
+ sources.push(`SELECT current_path AS target_path,
+ ${registry.has("git_checkout_root") ? "git_checkout_root" : "NULL"} AS checkout_root
+ FROM security_targets`);
}
+ const rows =
+ sources.length === 0
+ ? []
+ : readRows<SavedCheckout>(
+ database,
+ `SELECT target_path, MAX(checkout_root) AS checkout_root
+ FROM (${sources.join(" UNION ALL ")}) GROUP BY target_path`,
+ );
+ const saved = workspace.has("rejected_checkout_roots_json")
+ ? "rejected_checkout_roots_json"
+ : "NULL";
+ const rejected = workspace.has("target_path_unvalidated")
+ ? readRows<{ root: string | null }>(
+ database,
+ `SELECT DISTINCT value AS root FROM workspaces LEFT JOIN json_each(${saved})
+ WHERE ${saved} IS NOT NULL OR
+ (target_path IS NOT NULL AND target_path_unvalidated = 1 AND target_id IS NULL)`,
+ ).map((row) => row.root)
+ : [];
+ const roots = [...rows.map((row) => row.checkout_root), ...rejected];
+ return {
+ byTarget: new Map(rows.map((row) => [row.target_path, row.checkout_root])),
+ roots: roots.filter((root): root is string => root !== null),
+ complete: !roots.includes(null),
+ };
}
function readRows<T>(
@@ -273,137 +456,169 @@ function readRows<T>(
}
}
+function completedScans(database: RuntimeDatabase): ScanTarget[] {
+ const columns = tableColumns(database, "scans");
+ return readRows<
+ ScanTarget & {
+ updated_at: string;
+ progress_updated_at: string;
+ started_at: string;
+ }
+ >(
+ database,
+ `SELECT scans.id, scans.target_path, scans.updated_at, scans.started_at,
+ progress.updated_at AS progress_updated_at,
+ ${columns.has("repository_generation") ? "scans.repository_generation" : "NULL"} AS repository_generation
+ FROM scans JOIN scan_progress AS progress ON progress.scan_id = scans.id
+ WHERE scans.status = 'complete' ${columns.has("canceled_at") ? "AND scans.canceled_at IS NULL" : ""}`,
+ )
+ .map((scan) => {
+ const updated = timestampOrderKey(scan.updated_at);
+ const progress = timestampOrderKey(scan.progress_updated_at);
+ return {
+ ...scan,
+ updatedKey: updated > progress ? updated : progress,
+ startedKey: timestampOrderKey(scan.started_at),
+ };
+ })
+ .sort(
+ (left, right) =>
+ compareOrderKeys(right.updatedKey, left.updatedKey) ||
+ compareOrderKeys(right.startedKey, left.startedKey) ||
+ compareOrderKeys(left.id, right.id),
+ );
+}
+
async function latestTargets(
- database: RuntimeDatabase,
+ scans: ScanTarget[],
directory: string,
environment: ProcessEnvironment,
+ checkouts: SavedBoundaries,
signal?: AbortSignal,
): Promise<ScanTarget[]> {
- const current = await pathKey(directory);
- const columns = new Set(
- readRows<{ name: string }>(database, "PRAGMA table_info(scans)").map(
- (column) => column.name,
- ),
- );
- // The workbench migrates after discovery; schemas before v16 have no target registry.
- const hasTargets =
- readRows(database, "PRAGMA table_info(security_targets)").length > 0;
- const registered = readRows<ScanTarget>(
- database,
- hasTargets
- ? "SELECT id, current_path AS target_path FROM security_targets"
- : "SELECT DISTINCT target_path AS id, target_path FROM scans",
- );
- const related = new Set<string>();
- for (const target of registered)
- if ((await pathKey(target.target_path)) === current) related.add(target.id);
+ const paths = [...new Set(scans.map((scan) => scan.target_path))];
+ const related = new Map<string, { originFallback: boolean }>();
+ for (const path of paths)
+ if (await sameFile(path, directory))
+ related.set(path, { originFallback: false });
+ const protectedAncestorPaths = new Set<string>();
let gitMatchingUnavailable = false;
const caller = await gitMarkerRoot(directory, signal, "outermost");
if (caller !== null) {
// Resolve metadata with a host Git, never a Git executable from any candidate checkout.
- const roots = await Promise.all(
- registered.map((target) =>
- gitProtectionRoots(target.target_path, signal),
- ),
+ const protectedRoots = await savedDiscoveryRoots(
+ checkouts,
+ [directory, ...checkouts.byTarget.keys()],
+ signal,
);
- const protectedRoots = [
- ...(await gitProtectionRoots(directory, signal)),
- ...registered.map((target) => target.target_path),
- ...roots.flat(),
- ];
const configured = environmentEntry(environment, "CODEX_SECURITY_GIT");
const inspected =
- configured === ""
+ configured === "" ||
+ (!checkouts.complete && !isAbsolute(configured ?? ""))
? { executable: null, environment }
: await inspectTrustedExecutable(
configured ?? "git",
environment,
protectedRoots,
);
- // Keep subsequent workbench operations on this host Git without changing Python's PATH.
for (const key of Object.keys(environment))
if (key.toUpperCase() === "CODEX_SECURITY_GIT") delete environment[key];
environment["CODEX_SECURITY_GIT"] = inspected.executable ?? "";
gitMatchingUnavailable = inspected.executable === null;
- const git =
- inspected.executable === null
- ? null
- : {
- executable: inspected.executable,
- environment: inspected.environment,
- };
- const identity =
- git === null
- ? { commonDirectory: null, origin: null }
- : await gitHistoryIdentity(directory, git, signal);
- const common =
- identity.commonDirectory === null
- ? null
- : await pathKey(identity.commonDirectory);
- const origin = repositoryOrigin(identity.origin);
- for (const [index, target] of registered.entries()) {
- signal?.throwIfAborted();
- if (related.has(target.id) || roots[index] === null || git === null)
- continue;
- const candidate = await gitHistoryIdentity(
- target.target_path,
- git,
- signal,
- );
- if (
- (common !== null &&
+ if (inspected.executable !== null) {
+ const git = {
+ executable: inspected.executable,
+ environment: inspected.environment,
+ };
+ const identity = await gitHistoryIdentity(directory, git, signal);
+ const origin = repositoryOrigin(identity.origin);
+ for (const path of paths) {
+ signal?.throwIfAborted();
+ if (related.has(path)) continue;
+ const candidate = await gitHistoryIdentity(path, git, signal);
+ if (
+ candidate.commonDirectory === null &&
+ identity.commonDirectory !== null
+ ) {
+ const enclosing = await gitMarkerRoot(path, signal, "outermost");
+ if (enclosing !== null) {
+ const ancestor = await gitHistoryIdentity(enclosing, git, signal);
+ if (
+ ancestor.commonDirectory !== null &&
+ (await sameFile(
+ identity.commonDirectory,
+ ancestor.commonDirectory,
+ ))
+ )
+ protectedAncestorPaths.add(path);
+ }
+ }
+ if (
+ identity.relativePath === null ||
+ candidate.relativePath !== identity.relativePath
+ )
+ continue;
+ const common =
+ identity.commonDirectory !== null &&
candidate.commonDirectory !== null &&
- (await pathKey(candidate.commonDirectory)) === common) ||
- (origin !== null && repositoryOrigin(candidate.origin) === origin)
- )
- related.add(target.id);
+ (await sameFile(identity.commonDirectory, candidate.commonDirectory));
+ const clone =
+ !common &&
+ identity.commonDirectory !== null &&
+ candidate.commonDirectory !== null &&
+ origin !== null &&
+ repositoryOrigin(candidate.origin) === origin;
+ if (common || clone) related.set(path, { originFallback: clone });
+ }
}
}
- const scans = readRows<
- ScanTarget & {
- updated_at: string;
- progress_updated_at: string;
- started_at: string;
- }
- >(
- database,
- `SELECT scans.id, scans.target_path, scans.updated_at, scans.started_at,
- progress.updated_at AS progress_updated_at,
- ${columns.has("target_id") ? "scans.target_id" : "scans.target_path"} AS target_id
- FROM scans JOIN scan_progress AS progress ON progress.scan_id = scans.id
- WHERE scans.status = 'complete' ${columns.has("canceled_at") ? "AND scans.canceled_at IS NULL" : ""}
- ORDER BY scans.id`,
- )
- .map((scan) => ({
- ...scan,
- updatedAt:
- compareTimestamps(scan.updated_at, scan.progress_updated_at) > 0
- ? scan.updated_at
- : scan.progress_updated_at,
- }))
- .sort(
- (a, b) =>
- compareTimestamps(b.updatedAt, a.updatedAt) ||
- compareTimestamps(b.started_at, a.started_at),
- );
+ const selected: ScanTarget[] = [];
for (const scan of scans) {
signal?.throwIfAborted();
- if (
- related.has(scan.target_id ?? "") ||
- (await pathKey(scan.target_path)) === current
- )
- return [scan];
+ const match = related.get(scan.target_path);
+ // Scan generations survive legacy unbound registry rows and removed or reused paths.
+ // The caller-scoped workbench query remains authoritative for membership.
+ if (match || (caller !== null && scan.repository_generation != null))
+ selected.push({
+ ...scan,
+ originFallback: match?.originFallback ?? false,
+ // An existing path unrelated to the caller no longer supplies its source.
+ // Keep the historical binding for lookup without trusting it as a live target.
+ protectPython:
+ match !== undefined || protectedAncestorPaths.has(scan.target_path),
+ });
}
- if (gitMatchingUnavailable)
+ if (related.size === 0 && gitMatchingUnavailable)
throw new CodexSecurityError(
"No completed saved scan matched this exact path, and Git-based matching across worktrees or clones is unavailable. Use an explicit saved scan ID: codex-security dedupe --scan SCAN_ID",
);
- return [];
+ return selected;
+}
+
+function timestampOrderKey(value: string): string {
+ const match =
+ /^(\d{4}-\d{2}-\d{2}[Tt]\d{2}:\d{2}:\d{2})(?:\.([0-9]+))?([Zz]|[+-]\d{2}:\d{2})$/.exec(
+ value,
+ );
+ const seconds = match
+ ? Date.parse(`${match[1]}${match[3]}`.toUpperCase())
+ : NaN;
+ // Legacy naive or malformed rows retain opaque text ordering. The scoped
+ // workbench query still owns their diagnostics, without using the host timezone.
+ if (!Number.isFinite(seconds)) return `0${value}`;
+ const instant = new Date(seconds);
+ const whole = instant.toISOString().slice(0, -5);
+ // An offset can carry a supported four-digit local year into UTC year 10000.
+ const year = String(instant.getUTCFullYear()).padStart(5, "0");
+ const digits = match![2] ?? "";
+ let fractionEnd = digits.length;
+ while (fractionEnd > 0 && digits[fractionEnd - 1] === "0") fractionEnd--;
+ const fraction = digits.slice(0, fractionEnd);
+ return `${year}${whole.slice(whole.indexOf("-"))}${fraction ? `.${fraction}` : ""}`;
}
-async function pathKey(path: string): Promise<string> {
- const canonical = await realpath(path).catch(() => resolve(path));
- return process.platform === "win32" ? canonical.toLowerCase() : canonical;
+function compareOrderKeys(left: string, right: string): number {
+ return left < right ? -1 : left > right ? 1 : 0;
}
// Match the workbench history's host/path identity across HTTPS and SSH origins.
diff --git a/sdk/typescript/src/scan-comparison.ts b/sdk/typescript/src/scan-comparison.ts
index 046736c39..0de8b18f3 100644
--- a/sdk/typescript/src/scan-comparison.ts
+++ b/sdk/typescript/src/scan-comparison.ts
@@ -842,6 +842,11 @@ export async function matchCompletedScan(
const previousOccurrences = new Set(
options.previousFindings.map(({ occurrenceId }) => occurrenceId),
);
+ const previousScans = new Set(
+ options.previousFindings.flatMap((finding) =>
+ Array.isArray(finding["knownScanIds"]) ? finding["knownScanIds"] : [],
+ ),
+ );
const falsePositiveScans = new Map(
options.falsePositives.map(
({ findingId, sourceScanId }) => [findingId, sourceScanId] as const,
@@ -852,6 +857,8 @@ export async function matchCompletedScan(
"list-unmatched-scan-pairs",
"--repository",
options.repository,
+ "--after-scan-id",
+ options.scanId,
])) as {
batches?: ScanMatchingBatch[];
};
@@ -862,12 +869,14 @@ export async function matchCompletedScan(
// A saved comparison covers the whole pair. Let the catalogue group repeated
// occurrences instead of dropping findings from the selected scans.
- const beforeScans = batch.beforeScans.filter(({ scanId, findings }) =>
- findings.some(
- (finding) =>
- previousOccurrences.has(finding.occurrenceId) ||
- falsePositiveScans.get(finding["findingId"]) === scanId,
- ),
+ const beforeScans = batch.beforeScans.filter(
+ ({ scanId, findings }) =>
+ previousScans.has(scanId) ||
+ findings.some(
+ (finding) =>
+ previousOccurrences.has(finding.occurrenceId) ||
+ falsePositiveScans.get(finding["findingId"]) === scanId,
+ ),
);
if (beforeScans.length === 0) return;
diff --git a/sdk/typescript/src/targets.ts b/sdk/typescript/src/targets.ts
index 4ce34967a..4dcd59e2f 100644
--- a/sdk/typescript/src/targets.ts
+++ b/sdk/typescript/src/targets.ts
@@ -839,8 +839,15 @@ export async function gitHistoryIdentity(
repository: string,
git: TrustedExecutable,
signal?: AbortSignal,
-): Promise<{ commonDirectory: string | null; origin: string | null }> {
- const read = async (args: readonly string[]): Promise<string | null> => {
+): Promise<{
+ commonDirectory: string | null;
+ origin: string | null;
+ relativePath: string | null;
+}> {
+ const read = async (
+ args: readonly string[],
+ allowEmpty = false,
+ ): Promise<string | null> => {
try {
const { stdout } = await execFile(
git.executable,
@@ -852,20 +859,22 @@ export async function gitHistoryIdentity(
maxBuffer: Infinity,
},
);
- return (
- stdout.replace(process.platform === "win32" ? /\r?\n$/u : /\n$/u, "") ||
- null
+ const value = stdout.replace(
+ process.platform === "win32" ? /\r?\n$/u : /\n$/u,
+ "",
);
+ return value || (allowEmpty ? "" : null);
} catch {
throwIfAborted(signal);
return null;
}
};
- const [commonDirectory, origin] = await Promise.all([
+ const [commonDirectory, origin, relativePath] = await Promise.all([
read(["rev-parse", "--path-format=absolute", "--git-common-dir"]),
read(["remote", "get-url", "origin"]),
+ read(["rev-parse", "--show-prefix"], true),
]);
- return { commonDirectory, origin };
+ return { commonDirectory, origin, relativePath };
}
/** Resolve Git and its sanitized executable search path for a single selected checkout. */
@@ -986,7 +995,7 @@ export async function gitMarkerRoot(
const code = (error as NodeJS.ErrnoException).code;
if (
search !== "outermost" ||
- !["ENOENT", "ENOTDIR", "EACCES", "EPERM"].includes(code ?? "")
+ !["ENOENT", "ENOTDIR", "EACCES", "EPERM", "ELOOP"].includes(code ?? "")
)
throw error;
const parent = dirname(candidate);
@@ -1027,7 +1036,8 @@ async function walkGitMarkers(
if (
code !== "ENOENT" &&
code !== "ENOTDIR" &&
- (search !== "outermost" || (code !== "EACCES" && code !== "EPERM"))
+ (search !== "outermost" ||
+ !["EACCES", "EPERM", "ELOOP"].includes(code ?? ""))
)
throw error;
}
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index ca6da9fb3..bbd042ce3 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -60,6 +60,7 @@ import {
import {
classifyConnectionFailure,
initialCredentialsAvailable,
+ listRepositoryFindings,
} from "../src/api.js";
import {
OperationTelemetry,
@@ -14298,9 +14299,38 @@ if __name__ == "__main__":
},
);
+ test("discards partial findings when a repository projection becomes unavailable", async () => {
+ const pages: JsonObject[] = [
+ { findings: [{ findingId: "first-page" }], nextOffset: 1 },
+ { findings: [], projectionAvailable: false, nextOffset: null },
+ ];
+ const commands: Array<readonly string[]> = [];
+ expect(
+ await listRepositoryFindings(async (args) => {
+ commands.push(args);
+ return pages[commands.length - 1]!;
+ }, "target_sha256_example"),
+ ).toBeUndefined();
+ expect(commands).toHaveLength(2);
+ expect(commands[1]).toEqual([...commands[0]!, "--offset", "1"]);
+ expect(
+ await listRepositoryFindings(
+ async () => ({ findings: [], projectionAvailable: true }),
+ "target_sha256_example",
+ ),
+ ).toEqual([]);
+ expect(
+ await listRepositoryFindings(
+ async () => ({ findings: [] }),
+ "target_sha256_example",
+ ),
+ ).toEqual([]);
+ });
+
test.each([
["semantic matching fails", "matcher", "matcher unavailable"],
["the repository index fails", "index", "index unavailable"],
+ ["the repository projection is unavailable", "projection", undefined],
["a cost limit still allows false-positive matching", "budget", undefined],
[
"cost-limited matching needs additional context",
@@ -14556,6 +14586,9 @@ if __name__ == "__main__":
if (args[0] === "list-global-findings") {
expect(progressSignal?.aborted).toBe(true);
if (failure === "index") throw new Error("index unavailable");
+ if (failure === "projection") {
+ return { findings: [], projectionAvailable: false };
+ }
if (
failure === "dismissed" ||
failure === "managed" ||
@@ -14778,16 +14811,26 @@ if __name__ == "__main__":
? []
: undefined,
);
- expect(modelCalled).toBe(failure !== "index");
+ expect(modelCalled).toBe(
+ failure !== "index" && failure !== "projection",
+ );
expect(observedSingleTurn).toBe(
- failure === "index" ? undefined : limited,
+ failure === "index" || failure === "projection" ? undefined : limited,
);
expect(matchingTurns).toBe(
- failure === "index" || failure === "matcher" || failure === "managed"
+ failure === "index" ||
+ failure === "projection" ||
+ failure === "matcher" ||
+ failure === "managed"
? 0
: 1,
);
- if (failure === "budget-context") {
+ if (failure === "projection") {
+ expect(
+ result.findings.findings.map(({ findingId }) => findingId),
+ ).toContain(current.findingId);
+ } else if (failure === "budget-context") {
+ expect(matchingTurns).toBe(1);
expect(matched).toBe(false);
}
expect(commands.some(([command]) => command === "complete-scan")).toBe(
diff --git a/sdk/typescript/tests-ts/cli-findings-aliases.test.ts b/sdk/typescript/tests-ts/cli-findings-aliases.test.ts
new file mode 100644
index 000000000..e6a913d48
--- /dev/null
+++ b/sdk/typescript/tests-ts/cli-findings-aliases.test.ts
@@ -0,0 +1,292 @@
+import { existsSync } from "node:fs";
+import {
+ mkdir,
+ realpath,
+ rename,
+ rm,
+ symlink,
+ writeFile,
+} from "node:fs/promises";
+import { delimiter, join } from "node:path";
+import { expect, test } from "bun:test";
+import { main } from "../src/cli.js";
+import { dependencies, capture } from "./cli-fixtures.js";
+import { PLUGIN_ROOT } from "./plugin-root.js";
+import { useTestPython } from "./support/python.js";
+import { runPython } from "./support/python-probe.js";
+import {
+ scopedTemporaryDirectory,
+ temporaryDirectory,
+} from "./support/temporary-directories.js";
+import {
+ scanRegistrationArguments,
+ workbenchCommand,
+} from "./support/workbench-command.js";
+import { git } from "./git-fixture.js";
+import { runCommand } from "./support/shell.js";
+
+const pythonExecutable = useTestPython();
+const ambientPython = useTestPython(false);
+
+const savedAliasFindings = String.raw`
+import sqlite3, sys
+from pathlib import Path
+sys.path.insert(0, sys.argv[1])
+import workbench_db as workbench
+from filesystem_identity import serialize_filesystem_identity
+state, repository, alias = map(Path, sys.argv[2:5])
+state.mkdir(exist_ok=True)
+with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ workbench.apply_migrations(connection)
+ def insert(table, **values):
+ connection.execute(f"INSERT INTO {table} ({','.join(values)}) VALUES ({','.join('?' for _ in values)})", tuple(values.values()))
+ timestamp = "2026-08-01T00:00:00Z"
+ for name, target in (("canonical", repository), ("alias", alias)):
+ metadata = target.stat()
+ insert("security_targets", id=name, current_path=str(target), display_name=name, created_at=timestamp, updated_at=timestamp)
+ insert("workspaces", id=name, target_id=name, created_at=timestamp, updated_at=timestamp)
+ insert("scans", id=name, workspace_id=name, target_id=name, target_path=str(target), target_revision="unversioned", target_device=serialize_filesystem_identity(metadata.st_dev), target_inode=serialize_filesystem_identity(metadata.st_ino), scope=".", mode="standard", scan_dir=str(state / name), status="complete", phase="reporting", started_at=timestamp, completed_at=timestamp, created_at=timestamp, updated_at=timestamp)
+ insert("scan_progress", scan_id=name, updated_at=timestamp)
+ insert("findings", id=name, fingerprint=name, rule_id="synthetic-rule", identity_anchor=name, created_at=timestamp, updated_at=timestamp)
+ insert("finding_occurrences", id=name, finding_id=name, scan_id=name, title=name, summary="Synthetic finding", severity="high", confidence="high", remediation="Constrain the path", details_json="{}", created_at=timestamp)
+ if sys.argv[5] == "stale":
+ connection.execute("UPDATE scans SET target_inode = ? WHERE id = 'alias'", (serialize_filesystem_identity(alias.stat().st_ino + 1),))
+`;
+
+test.each(["current", "stale"] as const)(
+ "findings list preserves exact saved aliases with %s ownership",
+ async (ownership) => {
+ const root = await temporaryDirectory("findings-alias-boundary-");
+ try {
+ const repository = join(root, "repository");
+ const alias = join(root, "saved-alias");
+ const unsavedAlias = join(root, "unsaved-alias");
+ await mkdir(repository);
+ for (const path of [alias, unsavedAlias]) {
+ await symlink(
+ repository,
+ path,
+ process.platform === "win32" ? "junction" : "dir",
+ );
+ }
+ const python = pythonExecutable();
+ expect(python).not.toBeNull();
+ const stale = ownership === "stale";
+ const state = join(root, "state");
+ const initialized = runPython(python!, [
+ "-c",
+ savedAliasFindings,
+ join(PLUGIN_ROOT, "scripts"),
+ state,
+ repository,
+ alias,
+ ownership,
+ ]);
+ expect(new TextDecoder().decode(initialized.stderr)).toBe("");
+ expect(initialized.exitCode).toBe(0);
+ for (const requested of [alias, repository, unsavedAlias]) {
+ const stdout = capture();
+ const stderr = capture();
+ const result = await main(
+ ["findings", "list", requested, "--json"],
+ stdout.stream,
+ stderr.stream,
+ dependencies({ onWorkbench: workbenchCommand(python!, state) }),
+ );
+ if (stale && requested === alias) {
+ expect(result).toBe(2);
+ expect(stderr.text()).toContain(
+ "Repository findings are unavailable",
+ );
+ } else {
+ expect(result, stderr.text()).toBe(0);
+ const output = JSON.parse(stdout.text());
+ expect(output.repository).toBe(requested);
+ expect(
+ output.findings.map(
+ (finding: { scanId: string }) => finding.scanId,
+ ),
+ ).toEqual([requested === alias ? "alias" : "canonical"]);
+ }
+ const scanOutput = capture();
+ expect(
+ await main(
+ ["scans", "list", requested, "--json"],
+ scanOutput.stream,
+ capture().stream,
+ dependencies({ onWorkbench: workbenchCommand(python!, state) }),
+ ),
+ ).toBe(0);
+ expect(
+ JSON.parse(scanOutput.text()).scans.map(
+ (scan: { scanId: string }) => scan.scanId,
+ ),
+ ).toEqual(
+ stale && requested === alias
+ ? []
+ : [requested === alias ? "alias" : "canonical"],
+ );
+
+ const matchingOutput = capture();
+ const matchingErrors = capture();
+ const matchingStatus = await main(
+ ["scans", "match", "--all", "--json"],
+ matchingOutput.stream,
+ matchingErrors.stream,
+ dependencies({
+ currentDirectory: requested,
+ onWorkbench: workbenchCommand(python!, state),
+ }),
+ );
+ if (stale && requested === alias) {
+ expect(matchingStatus).toBe(2);
+ expect(matchingErrors.text()).toContain("no longer matches");
+ } else {
+ expect(matchingStatus).toBe(0);
+ expect(JSON.parse(matchingOutput.text()).scanCount).toBe(1);
+ expect(JSON.parse(matchingOutput.text()).repository).toBe(
+ await realpath(repository),
+ );
+ }
+ }
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+ },
+);
+
+test.skipIf(process.platform !== "linux")(
+ "findings list distinguishes an undecodable directory name from its replacement spelling",
+ async () => {
+ await using temp = await scopedTemporaryDirectory(
+ "finding-repository-byte-name-",
+ );
+ const rawPath = Buffer.concat([
+ Buffer.from(join(temp.path, "repo-")),
+ Buffer.from([0xff]),
+ ]);
+ const replacement = join(temp.path, "repo-\uFFFD");
+ const requested = join(temp.path, "requested-link");
+ await Promise.all([mkdir(rawPath), mkdir(replacement)]);
+ await symlink(rawPath, requested);
+ expect(await realpath(requested)).toBe(replacement);
+ const python = pythonExecutable()!;
+ for (const [savedMatch, removed] of [
+ [false, false],
+ [true, false],
+ [false, true],
+ ] as const) {
+ const savedAlias = join(temp.path, `saved-${savedMatch}-${removed}`);
+ const state = join(temp.path, `state-${savedMatch}-${removed}`);
+ const stored = removed ? join(replacement, "removed") : replacement;
+ const selected = removed ? join(requested, "removed") : requested;
+ if (removed) await mkdir(stored);
+ await symlink(savedMatch ? rawPath : stored, savedAlias);
+ const setup = runPython(python, [
+ "-c",
+ savedAliasFindings,
+ join(PLUGIN_ROOT, "scripts"),
+ state,
+ stored,
+ savedAlias,
+ "current",
+ ]);
+ expect(setup.exitCode, new TextDecoder().decode(setup.stderr)).toBe(0);
+ if (removed) await rm(stored, { recursive: true });
+ const stdout = capture();
+ const stderr = capture();
+ const status = await main(
+ ["findings", "list", selected, "--json"],
+ stdout.stream,
+ stderr.stream,
+ dependencies({ onWorkbench: workbenchCommand(python, state) }),
+ );
+ expect(status, stderr.text()).toBe(0);
+ expect(
+ JSON.parse(stdout.text()).findings.map(
+ (finding: { scanId: string }) => finding.scanId,
+ ),
+ ).toEqual(savedMatch ? ["alias"] : []);
+ }
+ },
+);
+
+test.skipIf(process.platform === "win32")(
+ "scans list excludes tools in a former checkout and forwards configured Git",
+ async () => {
+ await using temp = await scopedTemporaryDirectory(
+ "history-checkout-tools-",
+ );
+ const repository = join(temp.path, "repository");
+ const linked = join(temp.path, "linked");
+ const former = join(temp.path, "former");
+ const target = join(former, "component");
+ await mkdir(repository);
+ await mkdir(target, { recursive: true });
+ for (const checkout of [repository, former]) {
+ git(checkout, "init", "-q");
+ git(checkout, "commit", "--allow-empty", "-qm", "Synthetic history");
+ }
+ git(repository, "worktree", "add", "--quiet", "--detach", linked);
+ const state = join(temp.path, "state");
+ const workbench = workbenchCommand(ambientPython()!, state);
+ const scanDir = join(temp.path, "scan");
+ await mkdir(scanDir, { mode: 0o700 });
+ const scan = await workbench(
+ scanRegistrationArguments(repository, scanDir),
+ );
+ await workbench([
+ "create-workspace",
+ "--workspace-id",
+ "00000000-0000-4000-8000-000000000001",
+ "--target-path",
+ target,
+ ]);
+ await rename(join(former, ".git"), join(temp.path, "old-git"));
+ git(target, "init", "-q");
+ const tools = join(former, "bin");
+ await mkdir(tools);
+ for (const tool of ["git", "python3"]) {
+ await writeFile(join(tools, tool), '#!/bin/sh\n: > "$0-ran"\nexit 1\n', {
+ mode: 0o700,
+ });
+ }
+ const configured = join(temp.path, "selected-git");
+ await symlink(Bun.which("git")!, join(temp.path, "git"));
+ await writeFile(
+ configured,
+ '#!/bin/sh\n: > "$0-ran"\nexec "${0%/*}/git" "$@"\n',
+ { mode: 0o700 },
+ );
+ const cli = new URL("../src/cli.ts", import.meta.url).href;
+ for (const binding of [undefined, configured, ""]) {
+ const env: NodeJS.ProcessEnv = {
+ ...process.env,
+ PATH: [tools, process.env["PATH"]].join(delimiter),
+ CODEX_SECURITY_STATE_DIR: state,
+ CODEX_SECURITY_GIT: binding,
+ XDG_CACHE_HOME: join(temp.path, "cache"),
+ };
+ delete env["PYTHON"];
+ const result = await runCommand(
+ process.execPath,
+ [
+ "-e",
+ `const { main } = await import(${JSON.stringify(cli)}); process.exitCode = await main(["scans", "list", "--json"]);`,
+ ],
+ { env, cwd: linked },
+ );
+ expect(result.status, result.stderr).toBe(0);
+ expect(
+ JSON.parse(result.stdout).scans.map(
+ (row: { scanId: string }) => row.scanId,
+ ),
+ ).toEqual(binding === "" ? [] : [scan["scanId"]]);
+ expect(existsSync(join(tools, "git-ran"))).toBe(false);
+ expect(existsSync(join(tools, "python3-ran"))).toBe(false);
+ if (binding === configured)
+ expect(existsSync(configured + "-ran")).toBe(true);
+ }
+ },
+);
diff --git a/sdk/typescript/tests-ts/cli-scan-prompts.test.ts b/sdk/typescript/tests-ts/cli-scan-prompts.test.ts
index 3dc5114a0..9c1e426d7 100644
--- a/sdk/typescript/tests-ts/cli-scan-prompts.test.ts
+++ b/sdk/typescript/tests-ts/cli-scan-prompts.test.ts
@@ -2,6 +2,7 @@ import { mkdir, realpath, symlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, test, mock } from "bun:test";
import { main } from "../src/cli.js";
+import type { JsonObject } from "../src/config.js";
import { dependencies } from "./cli-fixtures.js";
import { git } from "./git-fixture.js";
import { scopedTemporaryDirectory } from "./support/temporary-directories.js";
@@ -261,3 +262,60 @@ describe("CLI scan prompts", () => {
).toBe(2);
});
});
+
+test("history repair loads implicit rerun prompts from the current checkout", async () => {
+ await using temp = await scopedTemporaryDirectory(
+ "codex-security-rerun-current-",
+ );
+ const root = temp.path;
+ const removed = join(root, "removed-checkout");
+ await writeFile(
+ join(root, "validation.md"),
+ "Validate the current synthetic checkout.\n",
+ );
+ const onTurn = mock<(repository: string, options: unknown) => void>();
+ const deps = dependencies({
+ currentDirectory: root,
+ onTurn,
+ onWorkbench: (args): JsonObject =>
+ args[0] === "list-scans"
+ ? { scans: [{ scanId: "saved" }] }
+ : {
+ scanId: "saved",
+ recipe: {
+ repository: removed,
+ target: { kind: "repository", paths: [] },
+ mode: "standard",
+ config: {},
+ validationMode: "custom",
+ },
+ },
+ });
+ expect(
+ await runCapturedCli(
+ main,
+ ["scans", "rerun", "--validation-prompt-file", "validation.md", "--json"],
+ deps,
+ ),
+ ).toBe(0);
+ expect(onTurn.mock.lastCall?.[0]).toBe(root);
+ expect(onTurn.mock.lastCall?.[1]).toMatchObject({
+ validationPrompt: "Validate the current synthetic checkout.\n",
+ });
+ const runs = onTurn.mock.calls.length;
+ expect(
+ await runCapturedCli(
+ main,
+ [
+ "scans",
+ "rerun",
+ "saved",
+ "--validation-prompt-file",
+ "validation.md",
+ "--json",
+ ],
+ deps,
+ ),
+ ).toBe(2);
+ expect(onTurn.mock.calls.length).toBe(runs);
+});
diff --git a/sdk/typescript/tests-ts/cli-scan-validation.test.ts b/sdk/typescript/tests-ts/cli-scan-validation.test.ts
index 0c7b2f23c..af52882fb 100644
--- a/sdk/typescript/tests-ts/cli-scan-validation.test.ts
+++ b/sdk/typescript/tests-ts/cli-scan-validation.test.ts
@@ -533,6 +533,98 @@ test("empty scan validation preserves configured Python and Git ignore rules", a
}
});
+test.skipIf(process.platform === "win32")(
+ "empty scan validation excludes Git from a former enclosing checkout",
+ async () => {
+ if (
+ runTestInSubprocess(
+ import.meta.path,
+ "empty scan validation excludes Git from a former enclosing checkout",
+ )
+ )
+ return;
+ const root = await temporaryDirectory("validation-saved-checkout-");
+ const repository = join(root, "current");
+ const outer = join(root, "previous");
+ const saved = join(outer, "component");
+ const python = await resolvePluginPython();
+ const hostGit = Bun.which("git")!;
+ const previous = {
+ PATH: process.env["PATH"],
+ CODEX_SECURITY_GIT: process.env["CODEX_SECURITY_GIT"],
+ CODEX_SECURITY_STATE_DIR: process.env["CODEX_SECURITY_STATE_DIR"],
+ };
+ try {
+ process.env["CODEX_SECURITY_STATE_DIR"] = join(root, "state");
+ process.env["CODEX_SECURITY_GIT"] = hostGit;
+ await mkdir(saved, { recursive: true });
+ await mkdir(repository);
+ for (const checkout of [outer, repository]) {
+ git(checkout, "init", "-q");
+ git(checkout, "commit", "-qm", "scan target", "--allow-empty");
+ }
+ const workbench = {
+ python,
+ pluginRoot: PLUGIN_ROOT,
+ environment: process.env,
+ };
+ await runWorkbench(workbench, [
+ "create-workspace",
+ "--workspace-id",
+ "00000000-0000-4000-8000-000000000001",
+ "--target-path",
+ saved,
+ ]);
+ const scanDir = join(root, "scan");
+ await mkdir(scanDir, { mode: 0o700 });
+ const registered = await runWorkbench(
+ workbench,
+ scanRegistrationArguments(repository, scanDir),
+ );
+ await rm(join(outer, ".git"), { recursive: true });
+ git(saved, "init", "-q");
+ const shim = join(outer, "git");
+ await writeFile(shim, '#!/bin/sh\nprintf invoked > "$0.ran"\nexit 1\n', {
+ mode: 0o700,
+ });
+ const result = fakeResult();
+ result.manifest.scan.id = String(registered["scanId"]);
+ const scan = spyOn(CodexSecurity.prototype, "run").mockImplementation(
+ async () => {
+ // Expose the shim specifically to the post-scan target check.
+ process.env["PATH"] = [outer, previous.PATH].join(delimiter);
+ return result;
+ },
+ );
+ try {
+ for (const binding of [undefined, hostGit, ""]) {
+ process.env["PATH"] = previous.PATH;
+ if (binding === undefined) delete process.env["CODEX_SECURITY_GIT"];
+ else process.env["CODEX_SECURITY_GIT"] = binding;
+ const cli = createCliTest(main);
+ const status = await cli.runCli(
+ ["scan", repository, "--validate", "--python", python, "--json"],
+ undefined,
+ );
+ expect(await Bun.file(shim + ".ran").exists()).toBe(false);
+ expect(status, cli.stderr.text()).toBe(binding === "" ? 2 : 0);
+ expect(JSON.parse(cli.stdout.text()).validation.status).toBe(
+ binding === "" ? "failed" : "complete",
+ );
+ }
+ } finally {
+ scan.mockRestore();
+ }
+ } finally {
+ for (const [name, value] of Object.entries(previous)) {
+ if (value === undefined) delete process.env[name];
+ else process.env[name] = value;
+ }
+ await rm(root, { recursive: true, force: true });
+ }
+ },
+);
+
test("empty scan validation preserves knowledge-base Git exclusions", async () => {
if (
runTestInSubprocess(
diff --git a/sdk/typescript/tests-ts/cli-workbench.test.ts b/sdk/typescript/tests-ts/cli-workbench.test.ts
index 8b168d732..b4f176e51 100644
--- a/sdk/typescript/tests-ts/cli-workbench.test.ts
+++ b/sdk/typescript/tests-ts/cli-workbench.test.ts
@@ -1,8 +1,7 @@
import { codexWithRun, jsonCodex } from "./support/codex.js";
-import { mkdir, realpath, rm, symlink, writeFile } from "node:fs/promises";
-import * as fs from "node:fs/promises";
+import { mkdir, rm, symlink, writeFile } from "node:fs/promises";
import { basename, join, resolve } from "node:path";
-import { describe, expect, test, mock, spyOn } from "bun:test";
+import { describe, expect, test, mock } from "bun:test";
import type { CodexSecurityConfig, JsonObject } from "../src/index.js";
import { DiffTarget, type ScanOptions } from "../src/index.js";
import { main } from "../src/cli.js";
@@ -12,6 +11,7 @@ import {
type ScanComparisonInput,
} from "../src/scan-comparison.js";
import {
+ capture,
savedRecipe,
dependencies,
FakeSignals,
@@ -30,85 +30,6 @@ import {
} from "./support/cli-run.js";
describe("CLI workbench", () => {
- test.skipIf(process.platform !== "linux")(
- "findings list distinguishes an undecodable directory name from its replacement spelling",
- async () => {
- await using temp = await scopedTemporaryDirectory(
- "finding-repository-byte-name-",
- );
- const root = temp.path;
- const bytePath = Buffer.concat([
- Buffer.from(`${root}/repository-`),
- Buffer.from([0xff]),
- ]);
- const replacementPath = join(root, "repository-\uFFFD");
- const alias = join(root, "repository-link");
- const savedAlias = join(root, "saved-link");
- await Promise.all([mkdir(bytePath), mkdir(replacementPath)]);
- await Promise.all([
- symlink(bytePath, alias),
- symlink(bytePath, savedAlias),
- ]);
- expect(await realpath(alias)).toBe(replacementPath);
- const [actual, replacement] = await Promise.all([
- fs.stat(alias, { bigint: true }),
- fs.stat(replacementPath, { bigint: true }),
- ]);
- expect(actual.ino).not.toBe(replacement.ino);
- for (const [suffix, includeMatchingAlias] of [
- ["", false],
- ["", true],
- ["removed", false],
- ] as const) {
- const calls: Array<readonly string[]> = [];
- const stdout = captureCli(main, "stdout");
- expect(
- await stdout.run(
- ["findings", "list", join(alias, suffix), "--json"],
- dependencies({
- onWorkbench: (args): JsonObject => {
- calls.push(args);
- return args[0] === "list-repositories"
- ? {
- repositories: [
- {
- targetId: "unrelated",
- targetPath: join(replacementPath, suffix),
- },
- ...(includeMatchingAlias
- ? [{ targetId: "selected", targetPath: savedAlias }]
- : []),
- ],
- }
- : {
- findings: [{ title: `${args[2]} finding` }],
- nextOffset: null,
- };
- },
- }),
- ),
- ).toBe(0);
- expect(calls).toEqual([
- ["list-repositories", "--status", "scanned"],
- ...(includeMatchingAlias
- ? [
- [
- "list-global-findings",
- "--target-id",
- "selected",
- "--status",
- "open",
- ],
- ]
- : []),
- ]);
- expect(JSON.parse(stdout.text())).toEqual({
- repository: join(alias, suffix),
- findings: includeMatchingAlias ? [{ title: "selected finding" }] : [],
- });
- }
- },
- );
test("findings list resolves parent paths after the checkout is removed", async () => {
await using temp = await scopedTemporaryDirectory(
"finding-removed-checkout-",
@@ -143,26 +64,16 @@ describe("CLI workbench", () => {
currentDirectory: directory,
onWorkbench: (args): JsonObject => {
calls.push(args);
- return args[0] === "list-repositories"
- ? {
- repositories: [
- { targetId: "saved", targetPath: repository },
- ],
- }
- : {
- findings: [{ title: "Saved finding" }],
- nextOffset: null,
- };
+ return {
+ findings: [{ title: "Saved finding" }],
+ nextOffset: null,
+ };
},
}),
),
).toBe(0);
- expect(calls[1]).toEqual([
- "list-global-findings",
- "--target-id",
- "saved",
- "--status",
- "open",
+ expect(calls).toEqual([
+ ["list-global-findings", "--repository", displayed, "--status", "open"],
]);
expect(JSON.parse(stdout.text())).toEqual({
repository: displayed,
@@ -171,263 +82,112 @@ describe("CLI workbench", () => {
}
});
- test("findings list matches directory identity when realpath preserves alias spelling", async () => {
- const root = await temporaryDirectory("finding-repository-identity-");
- const originalRealpath = fs.realpath;
- let spelling: ReturnType<typeof spyOn> | undefined;
- try {
- const repository = join(root, "repository");
- const upper = join(root, "first-alias");
- const lower = join(root, "second-alias");
- const other = join(root, "other");
- await Promise.all([mkdir(repository), mkdir(other)]);
- for (const alias of [upper, lower]) {
- await symlink(
- repository,
- alias,
- process.platform === "win32" ? "junction" : "dir",
- );
- }
- const [upperMetadata, lowerMetadata, otherMetadata] = await Promise.all(
- [upper, lower, other].map((path) => fs.stat(path, { bigint: true })),
- );
- expect(upperMetadata!.ino).toBe(lowerMetadata!.ino);
- expect(upperMetadata!.dev).toBe(lowerMetadata!.dev);
- expect(upperMetadata!.ino).not.toBe(otherMetadata!.ino);
- // Case-insensitive POSIX realpath may retain each alias's spelling.
- // Keep actual directory stat calls while controlling only that spelling.
- const preserveSpelling = (async (
- ...args: Parameters<typeof realpath>
- ) => {
- const result = await originalRealpath(...args);
- const path = args[0];
- return path === upper || path === lower
- ? typeof result === "string"
- ? path
- : Buffer.from(path)
- : result;
- }) as typeof realpath;
- spelling = spyOn(fs, "realpath").mockImplementation(preserveSpelling);
- for (const [requested, stored] of [
- [upper, lower],
- [lower, upper],
- ]) {
- const calls: Array<readonly string[]> = [];
- const stdout = captureCli(main, "stdout");
- expect(
- await stdout.run(
- ["findings", "list", requested!, "--json"],
- dependencies({
- onWorkbench: (args): JsonObject => {
- calls.push(args);
- return args[0] === "list-repositories"
- ? {
- repositories: [
- { targetId: "other", targetPath: other },
- {
- targetId: "missing",
- targetPath: join(root, "missing"),
- },
- { targetId: "selected", targetPath: stored! },
- ],
- }
- : {
- findings: [{ title: "Saved finding" }],
- nextOffset: null,
- };
- },
- }),
- ),
- ).toBe(0);
- expect(calls[1]).toEqual([
- "list-global-findings",
- "--target-id",
- "selected",
- "--status",
- "open",
- ]);
- expect(JSON.parse(stdout.text()).findings).toEqual([
- { title: "Saved finding" },
- ]);
- }
- } finally {
- spelling?.mockRestore();
- await rm(root, { recursive: true, force: true });
- }
- });
-
- test("findings list prefers an exact scanned repository before directory aliases", async () => {
- await using temp = await scopedTemporaryDirectory(
- "finding-exact-repository-",
- );
- const root = temp.path;
- const repository = join(root, "repository");
- const alias = join(root, "previous-checkout");
- await mkdir(repository);
- await symlink(
- repository,
- alias,
- process.platform === "win32" ? "junction" : "dir",
- );
- const unsavedAlias = join(root, "selected-checkout");
- await symlink(
- repository,
- unsavedAlias,
- process.platform === "win32" ? "junction" : "dir",
- );
- for (const [requested, first, exact] of [
- [repository, alias, repository],
- [alias, repository, alias],
- [unsavedAlias, alias, repository],
- ] as const) {
- for (const args of [[], [requested!]]) {
- const calls: Array<readonly string[]> = [];
- const stdout = captureCli(main, "stdout");
- expect(
- await stdout.run(
- ["findings", "list", ...args, "--json"],
- dependencies({
- currentDirectory: requested!,
- onWorkbench: (args): JsonObject => {
- calls.push(args);
- return args[0] === "list-repositories"
- ? {
- repositories: [
- { targetId: "alias-target", targetPath: first },
- { targetId: "exact-target", targetPath: exact },
- ...(args.includes("scanned")
- ? []
- : [
- {
- targetId: "unscanned",
- targetPath: unsavedAlias,
- },
- ]),
- ],
- }
- : {
- findings: [
- {
- title:
- args[2] === "exact-target"
- ? "Exact saved finding"
- : "Other target finding",
- },
- ],
- nextOffset: null,
- };
- },
- }),
- ),
- ).toBe(0);
- expect(calls[1]).toEqual([
- "list-global-findings",
- "--target-id",
- "exact-target",
- "--status",
- "open",
- ]);
- expect(JSON.parse(stdout.text())).toEqual({
- repository: requested!,
- findings: [{ title: "Exact saved finding" }],
- });
- }
- }
- });
-
test.each(["json", "text"])(
- "findings list preserves the requested alias in %s output",
+ "findings list retains requested aliases after history delegation in %s output",
async (format) => {
await using temp = await scopedTemporaryDirectory(
- "finding-repository-alias-",
+ "finding-delegated-alias-",
);
const root = temp.path;
const repository = join(root, "repository");
- const alias = join(root, "alias");
+ const alias = join(root, "requested-alias");
await mkdir(repository);
await symlink(
repository,
alias,
process.platform === "win32" ? "junction" : "dir",
);
- const canonical = await realpath(repository);
- for (const [requested, stored] of [
- [alias, canonical],
- [canonical, alias],
- [alias, alias],
- ]) {
- for (const args of [[], [requested!]]) {
- const stdout = captureCli(main, "stdout", format === "text");
- expect(
- await stdout.run(
- [
- "findings",
- "list",
- ...args,
- ...(format === "json" ? ["--json"] : []),
- ],
- dependencies({
- currentDirectory: requested!,
- onWorkbench: (args): JsonObject =>
- args[0] === "list-repositories"
- ? {
- repositories: [
- {
- targetId: "other",
- targetPath: join(root, "missing"),
- },
- { targetId: "selected", targetPath: stored! },
- ],
- }
- : {
- findings: [
- {
- title: "Saved finding",
- occurrenceId: "occ_sample_copyable",
- severity: "high",
- path: "source.ts:1",
- },
- ],
- nextOffset: null,
- },
+ const finding = {
+ title: "Saved finding",
+ occurrenceId: "occ_sample_copyable",
+ severity: "high",
+ path: "source.ts:1",
+ };
+ for (const requested of [repository, alias]) {
+ for (const args of [[], [requested]]) {
+ const cli = createCliTest(main, { stdout: format === "text" });
+ const status = await cli.runCli(
+ [
+ "findings",
+ "list",
+ ...args,
+ ...(format === "json" ? ["--json"] : []),
+ ],
+ dependencies({
+ currentDirectory: requested,
+ onWorkbench: (): JsonObject => ({
+ findings: [finding],
+ nextOffset: null,
}),
- ),
- ).toBe(0);
+ }),
+ );
+ expect(status, cli.stderr.text()).toBe(0);
if (format === "json") {
- expect(JSON.parse(stdout.text())).toEqual({
- repository: requested!,
- findings: [
- {
- title: "Saved finding",
- occurrenceId: "occ_sample_copyable",
- severity: "high",
- path: "source.ts:1",
- },
- ],
+ expect(JSON.parse(cli.stdout.text())).toEqual({
+ repository: requested,
+ findings: [finding],
});
} else {
- expect(stdout.text()).toContain(basename(requested!));
- expect(stdout.text()).toContain("Saved finding");
- expect(stdout.text()).toContain("occ_sample_copyable");
+ expect(cli.stdout.text()).toContain(basename(requested));
+ expect(cli.stdout.text()).toContain(finding.title);
+ expect(cli.stdout.text()).toContain(finding.occurrenceId);
}
}
}
},
);
+ test("findings list forwards directory aliases to the repository history query", async () => {
+ await using temp = await scopedTemporaryDirectory(
+ "finding-repository-alias-",
+ );
+ const root = temp.path;
+ const repository = join(root, "repository");
+ const alias = join(root, "alias");
+ await mkdir(repository);
+ await symlink(
+ repository,
+ alias,
+ process.platform === "win32" ? "junction" : "dir",
+ );
+ const calls: Array<readonly string[]> = [];
+ const stdout = captureCli(main, "stdout");
+ expect(
+ await stdout.run(
+ ["findings", "list", alias, "--json"],
+ dependencies({
+ onWorkbench: (args): JsonObject => {
+ calls.push(args);
+ return {
+ findings: [{ title: "Saved finding" }],
+ nextOffset: null,
+ };
+ },
+ }),
+ ),
+ ).toBe(0);
+ expect(calls).toEqual([
+ ["list-global-findings", "--repository", alias, "--status", "open"],
+ ]);
+ expect(JSON.parse(stdout.text())).toEqual({
+ repository: alias,
+ findings: [{ title: "Saved finding" }],
+ });
+ });
+
test.each([false, true])(
"distinguishes missing repository history from an empty finding list in the terminal (scanned=%j)",
async (scanned) => {
const repository = resolve("/sample/repository");
+ const historyQueries: Array<readonly string[]> = [];
const deps = dependencies({
currentDirectory: repository,
- onWorkbench: (args): JsonObject =>
- args[0] === "list-repositories"
- ? {
- repositories: scanned
- ? [{ targetId: "repo", targetPath: repository }]
- : [],
- }
- : { findings: [], nextOffset: null },
+ onWorkbench: (args): JsonObject => {
+ if (args[0] === "list-scans") {
+ historyQueries.push(args);
+ return { scans: scanned ? [{ scanId: "saved" }] : [] };
+ }
+ return { findings: [], nextOffset: null };
+ },
});
const tty = captureCli(main, "stdout", true);
expect(await tty.run(["findings", "list"], deps)).toBe(0);
@@ -437,6 +197,9 @@ describe("CLI workbench", () => {
const json = captureCli(main, "stdout");
expect(await json.run(["findings", "list", "--json"], deps)).toBe(0);
expect(JSON.parse(json.text())).toEqual({ repository, findings: [] });
+ expect(historyQueries).toEqual([
+ ["list-scans", "--repository", repository, "--limit", "1"],
+ ]);
},
);
test("lists and summarizes open findings for the current repository", async () => {
@@ -444,12 +207,6 @@ describe("CLI workbench", () => {
const stdout = captureCli(main, "stdout");
const calls: Array<readonly string[]> = [];
const responses: JsonObject[] = [
- {
- repositories: [
- { targetId: "other", targetPath: `${repository}-clone` },
- { targetId: "selected", targetPath: repository },
- ],
- },
{ findings: [{ title: "Finding 1" }], nextOffset: 1 },
{ findings: [{ title: "Finding 2" }], nextOffset: null },
];
@@ -461,15 +218,14 @@ describe("CLI workbench", () => {
}),
),
).toBe(0);
- expect(calls[0]).toEqual(["list-repositories", "--status", "scanned"]);
- expect(calls[1]).toEqual([
+ expect(calls[0]).toEqual([
"list-global-findings",
- "--target-id",
- "selected",
+ "--repository",
+ repository,
"--status",
"open",
]);
- expect(calls[2]).toEqual([...calls[1]!, "--offset", "1"]);
+ expect(calls[1]).toEqual([...calls[0]!, "--offset", "1"]);
expect(JSON.parse(stdout.text())).toEqual({
repository,
findings: [{ title: "Finding 1" }, { title: "Finding 2" }],
@@ -478,7 +234,7 @@ describe("CLI workbench", () => {
await runCapturedCli(
main,
["findings", "--json"],
- dependencies({ onWorkbench: () => ({ repositories: [] }) }),
+ dependencies({ onWorkbench: () => ({ findings: [] }) }),
),
).toBe(0);
for (const confirmed of [[true, false], []]) {
@@ -493,12 +249,121 @@ describe("CLI workbench", () => {
expect(await stderr.run(["scan"], dependencies({ result }))).toBe(0);
expect(stderr.text()).toContain(
confirmed.length
- ? "FINDINGS 2 (1 confirmed this scan; 1 previously found; 2 high)"
+ ? "FINDINGS 2 (1 confirmed in latest repository scan; 1 previously found; 2 high)"
: "FINDINGS 0\n",
);
}
});
+ test("uses sealed scan findings when the repository projection is unavailable", async () => {
+ const stderr = capture();
+ expect(
+ await main(
+ ["scan"],
+ capture().stream,
+ stderr.stream,
+ dependencies({ result: fakeResult(["high"]) }),
+ ),
+ ).toBe(0);
+ expect(stderr.text()).toMatch(/FINDINGS\s+1\b/u);
+ expect(stderr.text()).toContain("1 high");
+ });
+
+ test("preserves the original findings requester instead of selecting a historical alias", async () => {
+ const repository = resolve("/current/repository");
+ const findings: JsonObject[] = [
+ { occurrenceId: "requested-finding", status: "open" },
+ { occurrenceId: "linked-finding", status: "open" },
+ ];
+ const calls: Array<readonly string[]> = [];
+ const stdout = capture();
+ expect(
+ await main(
+ ["findings", "list", "--json"],
+ stdout.stream,
+ capture().stream,
+ dependencies({
+ onWorkbench: (args): JsonObject => {
+ calls.push(args);
+ return { findings, nextOffset: null };
+ },
+ }),
+ ),
+ ).toBe(0);
+ expect(calls).toEqual([
+ ["list-global-findings", "--repository", repository, "--status", "open"],
+ ]);
+ expect(JSON.parse(stdout.text())).toEqual({ repository, findings });
+ });
+
+ test("returns no findings when no related repository has been scanned", async () => {
+ const calls: Array<readonly string[]> = [];
+ const stdout = capture();
+ const repository = resolve("/current/repository");
+ expect(
+ await main(
+ ["findings", "list", "--json"],
+ stdout.stream,
+ capture().stream,
+ dependencies({
+ onWorkbench: (args) => {
+ calls.push(args);
+ return {
+ findings: [],
+ nextOffset: null,
+ projectionAvailable: true,
+ };
+ },
+ }),
+ ),
+ ).toBe(0);
+ expect(calls).toEqual([
+ ["list-global-findings", "--repository", repository, "--status", "open"],
+ ]);
+ expect(JSON.parse(stdout.text())).toEqual({ repository, findings: [] });
+ });
+
+ test("reports unavailable findings on any page instead of claiming an empty repository", async () => {
+ for (const laterPage of [false, true]) {
+ for (const json of [false, true]) {
+ const stdout = capture(!json);
+ const stderr = capture();
+ const responses: JsonObject[] = [
+ ...(laterPage
+ ? [
+ {
+ findings: [{ title: "Synthetic finding" }],
+ nextOffset: 1,
+ projectionAvailable: true,
+ },
+ ]
+ : []),
+ {
+ findings: [],
+ nextOffset: null,
+ projectionAvailable: false,
+ },
+ ];
+ const calls: Array<readonly string[]> = [];
+ expect(
+ await main(
+ ["findings", "list", ...(json ? ["--json"] : [])],
+ stdout.stream,
+ stderr.stream,
+ dependencies({
+ onWorkbench: (args) => responses[calls.push(args) - 1]!,
+ }),
+ ),
+ ).toBe(2);
+ expect(calls).toHaveLength(laterPage ? 2 : 1);
+ expect(stderr.text()).toContain("findings are unavailable");
+ expect(stdout.text()).not.toContain("0 open findings");
+ expect(stdout.text()).not.toContain("Synthetic finding");
+ expect(stdout.text()).not.toContain('"findings": []');
+ }
+ }
+ });
+
test("lists repository and scan-root history without starting Codex", async () => {
const repository = resolve("/current/repository");
const cases: Array<[string[], string[]]> = [
@@ -1640,6 +1505,7 @@ describe("CLI workbench", () => {
test("reruns the latest completed scan by default", async () => {
let parentScanId: unknown;
+ let rerunRepository: string | undefined;
let maxTimeMinutes: unknown;
expect(
@@ -1647,7 +1513,8 @@ describe("CLI workbench", () => {
main,
["scans", "rerun"],
dependencies({
- onTurn: (_repository, options) => {
+ onTurn: (repository, options) => {
+ rerunRepository = repository;
parentScanId = options.parentScanId;
maxTimeMinutes = options.maxTimeMinutes;
},
@@ -1657,7 +1524,7 @@ describe("CLI workbench", () => {
: {
scanId: "latest-scan",
recipe: {
- repository: "/current/repository",
+ repository: "/removed/linked-worktree",
target: { kind: "repository", paths: [] },
mode: "standard",
maxTimeMinutes: 15,
@@ -1668,6 +1535,7 @@ describe("CLI workbench", () => {
),
).toBe(0);
expect(parentScanId).toBe("latest-scan");
+ expect(rerunRepository).toBe("/current/repository");
expect(maxTimeMinutes).toBe(15);
});
diff --git a/sdk/typescript/tests-ts/deep-scan-workbench.test.ts b/sdk/typescript/tests-ts/deep-scan-workbench.test.ts
index 475ab82a2..8c57976f3 100644
--- a/sdk/typescript/tests-ts/deep-scan-workbench.test.ts
+++ b/sdk/typescript/tests-ts/deep-scan-workbench.test.ts
@@ -39,6 +39,7 @@ const deepScanOwnershipProbe = [
"elif case.get('mutation') == 'withdraw':",
" connection.executescript(\"CREATE TRIGGER withdraw_handoff BEFORE UPDATE OF thread_id ON workspaces BEGIN UPDATE scans SET handoff_status = 'pending' WHERE workspace_id = NEW.id; END\")",
"deep_scan.require_scan = lambda database, value: database.execute('SELECT * FROM scans WHERE id = ?', (value,)).fetchone()",
+ "deep_scan.require_scan_checkout_owner = lambda database, scan: None",
"deep_scan.require_workspace = lambda database, value: database.execute('SELECT * FROM workspaces WHERE id = ?', (value,)).fetchone()",
"deep_scan.now = lambda: 'after'",
"deep_scan.configure(deep_scan)",
diff --git a/sdk/typescript/tests-ts/fixtures/target-paths-node.ts b/sdk/typescript/tests-ts/fixtures/target-paths-node.ts
index 20226efb8..fe73647ca 100644
--- a/sdk/typescript/tests-ts/fixtures/target-paths-node.ts
+++ b/sdk/typescript/tests-ts/fixtures/target-paths-node.ts
@@ -14,15 +14,14 @@ import {
import { homedir } from "node:os";
import { join, relative } from "node:path";
import { TestClient } from "../support/api-client.js";
-import type { JsonObject } from "../../src/config.js";
import { main } from "../../src/cli.js";
import { runComponentScans } from "../../src/component-scan.js";
-import { runWorkbench } from "../../src/runtime.js";
+import { resolvePluginPython, runWorkbench } from "../../src/runtime.js";
import { runArtifactExport } from "../../src/artifact-export.js";
import { prepareNormalizedLocalImport } from "../../src/external-findings-local.js";
import { dependencies, fakeResult } from "../cli-fixtures.js";
import { createCliTest } from "../support/cli-run.js";
-import { copyCompletedScan } from "../plugin-root.js";
+import { copyCompletedScan, PLUGIN_ROOT } from "../plugin-root.js";
const root = process.argv[2]!;
const entry = join(root, "entry");
@@ -270,44 +269,83 @@ try {
}
await mkdir(sibling, { mode: 0o700 });
const savedAlias = join(root, "owner", "alias");
+ const historyState = join(root, "history-state");
+ await mkdir(historyState);
+ const historyPython = await resolvePluginPython();
+ execFileSync(
+ historyPython,
+ [
+ "-c",
+ String.raw`
+import sqlite3, sys
+from pathlib import Path
+sys.path.insert(0, sys.argv[1])
+import workbench_db as workbench
+from filesystem_identity import serialize_filesystem_identity
+state, sibling, physical = map(Path, sys.argv[2:5])
+with sqlite3.connect(state / "workbench.sqlite3") as connection:
+ connection.row_factory = sqlite3.Row
+ workbench.apply_migrations(connection)
+ def insert(table, **values):
+ connection.execute(f"INSERT INTO {table} ({','.join(values)}) VALUES ({','.join('?' for _ in values)})", tuple(values.values()))
+ timestamp = "2026-08-01T00:00:00Z"
+ for name, target in (("sibling", sibling), ("physical", physical)):
+ metadata = target.stat()
+ insert("security_targets", id=name, current_path=str(target), display_name=name, created_at=timestamp, updated_at=timestamp)
+ insert("workspaces", id=name, target_id=name, created_at=timestamp, updated_at=timestamp)
+ insert("scans", id=name, workspace_id=name, target_id=name, target_path=str(target), target_revision="unversioned", target_device=serialize_filesystem_identity(metadata.st_dev), target_inode=serialize_filesystem_identity(metadata.st_ino), scope=".", mode="standard", scan_dir=str(state / name), status="complete", phase="reporting", started_at=timestamp, completed_at=timestamp, created_at=timestamp, updated_at=timestamp)
+ insert("scan_progress", scan_id=name, updated_at=timestamp)
+ insert("findings", id=name, fingerprint=name, rule_id="synthetic-rule", identity_anchor=name, created_at=timestamp, updated_at=timestamp)
+ insert("finding_occurrences", id=name, finding_id=name, scan_id=name, title=name + " finding", summary="Synthetic finding", severity="high", confidence="high", remediation="Constrain the path", details_json="{}", created_at=timestamp)
+`,
+ join(PLUGIN_ROOT, "scripts"),
+ historyState,
+ sibling,
+ repository,
+ ],
+ { encoding: "utf8" },
+ );
async function checkHistory(
expectedTarget: string | undefined,
requested = requestedRepository,
) {
const history = createCliTest(main);
- let selectedTarget: string | undefined;
assert.equal(
await history.runCli(
["findings", "list", requested, "--json"],
dependencies({
currentDirectory: root,
- onWorkbench: (args): JsonObject => {
- if (args[0] === "list-repositories") {
- return {
- repositories: [
- { targetId: "sibling", targetPath: sibling },
- { targetId: "physical", targetPath: repository },
- { targetId: "saved-alias", targetPath: savedAlias },
- ],
- };
- }
- selectedTarget = args[2];
- return {
- findings: [{ title: `${selectedTarget} finding` }],
- nextOffset: null,
- };
- },
+ onWorkbench: (args, input) =>
+ runWorkbench(
+ {
+ python: historyPython,
+ pluginRoot: PLUGIN_ROOT,
+ environment: {
+ PATH: process.env["PATH"],
+ CODEX_SECURITY_STATE_DIR: historyState,
+ },
+ },
+ args,
+ input,
+ ),
}),
),
- 0,
+ expectedTarget === undefined ? 2 : 0,
history.stderr.text(),
);
- assert.equal(selectedTarget, expectedTarget);
+ if (expectedTarget === undefined) {
+ assert.match(
+ history.stderr.text(),
+ /Repository findings are unavailable/,
+ );
+ assert.equal(history.stdout.text(), "");
+ return;
+ }
assert.deepEqual(
- JSON.parse(history.stdout.text()).findings,
- expectedTarget === undefined
- ? []
- : [{ title: `${expectedTarget} finding` }],
+ JSON.parse(history.stdout.text()).findings.map(
+ (finding: { title: string }) => finding.title,
+ ),
+ [`${expectedTarget} finding`],
);
}
await checkHistory("physical");
@@ -495,8 +533,8 @@ try {
"Source",
);
}
- await checkHistory("saved-alias", savedAlias);
- await checkHistory("saved-alias", "owner/tools/../alias");
+ await checkHistory("physical", savedAlias);
+ await checkHistory("physical", "owner/tools/../alias");
const requestedAlias = `${owner}/tools/../alias`;
const alias = createCliTest(main);
assert.equal(
diff --git a/sdk/typescript/tests-ts/mock-scan.test.ts b/sdk/typescript/tests-ts/mock-scan.test.ts
index e82c170bd..19daedf85 100644
--- a/sdk/typescript/tests-ts/mock-scan.test.ts
+++ b/sdk/typescript/tests-ts/mock-scan.test.ts
@@ -5,6 +5,7 @@ import {
mkdtemp,
readFile,
readdir,
+ rm,
realpath,
symlink,
writeFile,
@@ -486,6 +487,71 @@ test.each([
}
});
+test("history archive compatibility restores completed mock output after a checkout is replaced", async () => {
+ const { root, repository, client, environment, python } = await fixture();
+ const git = async (...args: string[]) => {
+ const result = await runCommand(
+ "git",
+ [
+ "-C",
+ repository,
+ "-c",
+ "core.hooksPath=/dev/null",
+ "-c",
+ "commit.gpgsign=false",
+ "-c",
+ "user.name=Example",
+ "-c",
+ ...args,
+ ],
+ { timeout: 10000 },
+ );
+ expect(result.status).toBe(0);
+ };
+ const output = join(root, "completed-output");
+ try {
+ await git("init");
+ await git("add", ".");
+ await git("commit", "-m", "Initial synthetic fixture");
+ const first = await client.run(repository, {
+ mock: true,
+ outputDir: output,
+ });
+ const manifest = await readFile(join(output, "scan-manifest.json"));
+ await rm(join(repository, ".git"), { recursive: true, force: true });
+ await git("init");
+ await git("add", ".");
+ await git("commit", "-m", "Replacement synthetic fixture");
+ await expect(
+ client.run(repository, {
+ mock: true,
+ outputDir: output,
+ archiveExisting: true,
+ }),
+ ).rejects.toThrow();
+ expect(await readFile(join(output, "scan-manifest.json"))).toEqual(
+ manifest,
+ );
+ const history = await runWorkbench(
+ { python, pluginRoot: PLUGIN_ROOT, environment },
+ ["list-scans", "--scan-root", output],
+ );
+ expect(
+ (history["scans"] as { scanId: string; scanDir: string }[]).find(
+ ({ scanId }) => scanId === first.manifest.scan.id,
+ )?.scanDir,
+ ).toBe(output);
+ expect(
+ (await readdir(root)).filter((name) =>
+ name.startsWith("completed-output.previous-"),
+ ),
+ ).toEqual([]);
+ } finally {
+ await client.close();
+ }
+});
+
describe("interrupted workbench registration", () => {
let registeredId: string | undefined;
let saved: { id: string; status: string } | undefined;
diff --git a/sdk/typescript/tests-ts/repository-feedback-aliases.test.ts b/sdk/typescript/tests-ts/repository-feedback-aliases.test.ts
new file mode 100644
index 000000000..c70dbdbe6
--- /dev/null
+++ b/sdk/typescript/tests-ts/repository-feedback-aliases.test.ts
@@ -0,0 +1,199 @@
+import { join } from "node:path";
+import { expect, test } from "bun:test";
+import { PLUGIN_ROOT } from "./plugin-root.js";
+import { runCommand } from "./support/shell.js";
+
+test("reuses reviewed feedback only across matching persisted repository identities", async () => {
+ const python = (Bun.which("python3") ?? Bun.which("python"))!;
+ const probe = `
+import json, sqlite3, sys
+sys.path.insert(0, sys.argv[1])
+import workbench_target_state as state
+from workbench_feedback import get_scan_feedback
+
+scenario = sys.argv[2]
+connection = sqlite3.connect(":memory:")
+connection.row_factory = sqlite3.Row
+connection.executescript("""
+CREATE TABLE security_targets(id TEXT, current_path TEXT, display_name TEXT, origin TEXT);
+CREATE TABLE scans(id TEXT, target_id TEXT, target_path TEXT, repository_generation TEXT, status TEXT, completed_at TEXT, started_at TEXT, updated_at TEXT, scope TEXT);
+CREATE TABLE findings(id TEXT PRIMARY KEY, fingerprint TEXT, rule_id TEXT, identity_anchor TEXT, identity_instance TEXT);
+CREATE TABLE finding_occurrences(id TEXT, finding_id TEXT, scan_id TEXT, title TEXT, summary TEXT, severity TEXT, created_at TEXT);
+CREATE TABLE finding_triage(occurrence_id TEXT, status TEXT, close_reason TEXT, note TEXT, updated_at TEXT);
+CREATE TABLE finding_locations(id INTEGER PRIMARY KEY, occurrence_id TEXT, relative_path TEXT, start_line INTEGER, end_line INTEGER, role TEXT, sort_order INTEGER);
+CREATE TABLE scan_comparison_matches(before_occurrence_id TEXT, after_occurrence_id TEXT);
+""")
+if scenario == "identities":
+ connection.execute("ALTER TABLE security_targets ADD COLUMN repository_identity TEXT")
+refused = set()
+def inspect(database, target_id, path, stored, **kwargs):
+ repository = state.GitRepositoryIdentity(stored, ".", "synthetic", 1, 2, 3, 3) if stored else None
+ return state.RepositoryTargetState(
+ target_id, path, stored, resolved_path=path, repository=repository,
+ ownership_matches=target_id not in refused, strict_owner_matches=True,
+ has_historical_scans=True,
+ )
+state._inspect_repository_target = inspect
+
+origin = "https://example.invalid/synthetic/repository"
+for target, identity in [
+ ("primary", "common-git-directory::."),
+ ("linked", "common-git-directory::."),
+ ("same-origin-clone", "independent-git-directory::."),
+ ("different-scope", "common-git-directory::packages/api"),
+ ("unknown-first", None),
+ ("unknown-second", None),
+]:
+ connection.execute(
+ "INSERT INTO security_targets(id, current_path, display_name, origin) VALUES (?, ?, ?, ?)",
+ (target, f"/{target}", target, origin),
+ )
+ if scenario == "identities":
+ connection.execute(
+ "UPDATE security_targets SET repository_identity = ? WHERE id = ?",
+ (identity, target),
+ )
+
+def add_scan(scan_id, target, day, status="complete"):
+ timestamp = f"2026-03-{day:02d}T00:00:00Z"
+ completed_at = timestamp if status == "complete" else None
+ generation = connection.execute("SELECT repository_identity FROM security_targets WHERE id = ?", (target,)).fetchone()[0] if scenario == "identities" else None
+ connection.execute("INSERT INTO scans VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", (scan_id, target, f"/{target}", generation, status, completed_at, timestamp, timestamp, "repository"))
+
+def add_finding(scan_id, finding_id, day, *, status="closed", reason="false_positive", note="Reviewed and safe"):
+ occurrence_id = f"{scan_id}:{finding_id}"
+ connection.execute(
+ "INSERT OR IGNORE INTO findings VALUES (?, ?, ?, ?, ?)",
+ (finding_id, f"fingerprint-{finding_id}", "synthetic-rule", f"anchor-{finding_id}", None),
+ )
+ connection.execute(
+ "INSERT INTO finding_occurrences VALUES (?, ?, ?, ?, ?, ?, ?)",
+ (occurrence_id, finding_id, scan_id, finding_id, "Synthetic summary", "high", f"2026-03-{day:02d}T00:00:00Z"),
+ )
+ connection.execute(
+ "INSERT INTO finding_locations(occurrence_id, relative_path, start_line, end_line, role, sort_order) VALUES (?, ?, ?, ?, ?, ?)",
+ (occurrence_id, "src/auth.py", 3, 4, "root_control", 0),
+ )
+ connection.execute(
+ "INSERT INTO finding_triage VALUES (?, ?, ?, ?, ?)",
+ (occurrence_id, status, reason, note, f"2026-03-{day:02d}T12:00:00Z"),
+ )
+
+for scan_id, target, day in [
+ ("primary-reviewed", "primary", 1),
+ ("linked-reviewed", "linked", 2),
+ ("linked-reopened", "linked", 3),
+ ("clone-reviewed", "same-origin-clone", 4),
+ ("scope-reviewed", "different-scope", 5),
+ ("unknown-first-reviewed", "unknown-first", 6),
+ ("unknown-second-reviewed", "unknown-second", 7),
+]:
+ add_scan(scan_id, target, day)
+
+add_finding("primary-reviewed", "primary-false-positive", 1)
+add_finding("primary-reviewed", "reopened-across-alias", 1)
+add_finding("primary-reviewed", "renamed-before-reopening", 1)
+add_finding("linked-reviewed", "linked-false-positive", 2)
+add_finding("linked-reviewed", "linked-wont-fix", 2, reason="wont_fix")
+add_finding("linked-reviewed", "linked-no-note", 2, note=" ")
+add_finding("linked-reopened", "reopened-across-alias", 3, status="open", reason=None, note=None)
+add_finding("linked-reopened", "renamed-after-reopening", 3, status="open", reason=None, note=None)
+connection.execute(
+ "INSERT INTO scan_comparison_matches VALUES (?, ?)",
+ ("primary-reviewed:renamed-before-reopening", "linked-reopened:renamed-after-reopening"),
+)
+add_finding("clone-reviewed", "clone-false-positive", 4)
+add_finding("scope-reviewed", "scope-false-positive", 5)
+add_finding("unknown-first-reviewed", "unknown-first-false-positive", 6)
+add_finding("unknown-second-reviewed", "unknown-second-false-positive", 7)
+add_scan("linked-incomplete", "linked", 8, status="running")
+add_finding("linked-incomplete", "linked-incomplete-false-positive", 8)
+
+for target in ["primary", "linked", "same-origin-clone", "different-scope", "unknown-first", "unknown-second"]:
+ add_scan(f"current-{target}", target, 9, status="running")
+
+def feedback(target):
+ scan = connection.execute("SELECT * FROM scans WHERE id = ?", (f"current-{target}",)).fetchone()
+ return get_scan_feedback(connection, scan)
+
+result = {
+ "primary": feedback("primary"),
+ "linked": feedback("linked"),
+ "clone": feedback("same-origin-clone"),
+ "differentScope": feedback("different-scope"),
+ "unknownFirst": feedback("unknown-first"),
+ "unknownSecond": feedback("unknown-second"),
+}
+if scenario == "identities":
+ refused.add("primary")
+ result["reusedPath"] = feedback("primary")
+ result["deletedAlias"] = feedback("linked")
+print(json.dumps(result))
+`;
+
+ const run = async (scenario: "identities" | "legacy") => {
+ const execution = await runCommand(
+ python,
+ ["-I", "-B", "-c", probe, join(PLUGIN_ROOT, "scripts"), scenario],
+ { timeout: 10_000 },
+ );
+ expect(execution.status, execution.stderr).toBe(0);
+ return JSON.parse(execution.stdout) as Record<
+ string,
+ {
+ scanId: string;
+ targetId: string;
+ falsePositives: Array<Record<string, unknown>>;
+ }
+ >;
+ };
+
+ const identities = await run("identities");
+ expect(identities["primary"]).toMatchObject({
+ scanId: "current-primary",
+ targetId: "primary",
+ falsePositives: [
+ {
+ findingId: "linked-false-positive",
+ sourceScanId: "linked-reviewed",
+ reason: "Reviewed and safe",
+ },
+ {
+ findingId: "primary-false-positive",
+ sourceScanId: "primary-reviewed",
+ },
+ ],
+ });
+ expect(identities["linked"]?.falsePositives).toEqual(
+ identities["primary"]?.falsePositives,
+ );
+ expect(identities["clone"]?.falsePositives).toMatchObject([
+ { findingId: "clone-false-positive" },
+ ]);
+ expect(identities["differentScope"]?.falsePositives).toMatchObject([
+ { findingId: "scope-false-positive" },
+ ]);
+ expect(identities["unknownFirst"]?.falsePositives).toMatchObject([
+ { findingId: "unknown-first-false-positive" },
+ ]);
+ expect(identities["unknownSecond"]?.falsePositives).toMatchObject([
+ { findingId: "unknown-second-false-positive" },
+ ]);
+ expect(identities["reusedPath"]?.falsePositives).toEqual([]);
+ expect(identities["deletedAlias"]?.falsePositives).toEqual(
+ identities["linked"]?.falsePositives,
+ );
+
+ const legacy = await run("legacy");
+ expect(legacy["primary"]?.falsePositives).toMatchObject([
+ { findingId: "reopened-across-alias", sourceScanId: "primary-reviewed" },
+ {
+ findingId: "renamed-before-reopening",
+ sourceScanId: "primary-reviewed",
+ },
+ { findingId: "primary-false-positive", sourceScanId: "primary-reviewed" },
+ ]);
+ expect(legacy["linked"]?.falsePositives).toMatchObject([
+ { findingId: "linked-false-positive", sourceScanId: "linked-reviewed" },
+ ]);
+});
diff --git a/sdk/typescript/tests-ts/repository-findings-worktrees.test.ts b/sdk/typescript/tests-ts/repository-findings-worktrees.test.ts
new file mode 100644
index 000000000..cfed1dd3c
--- /dev/null
+++ b/sdk/typescript/tests-ts/repository-findings-worktrees.test.ts
@@ -0,0 +1,173 @@
+import { mkdir } from "node:fs/promises";
+import { join } from "node:path";
+import { afterEach, describe, expect, test } from "bun:test";
+import { CodexSecurity } from "../src/index.js";
+import { completedEvents, preparedRuntime } from "./support/api-events.js";
+
+import { createApiTestFixtures } from "./support/temporary-directories.js";
+import { copyCompletedScan } from "./plugin-root.js";
+
+const fixtures = createApiTestFixtures();
+const TestClient = CodexSecurity as unknown as new (
+ config: Record<string, unknown>,
+ dependencies: Record<string, unknown>,
+) => CodexSecurity;
+
+afterEach(fixtures.cleanup);
+
+describe("repository findings across linked worktrees", () => {
+ test("matches only repository-identity-scoped history from another worktree", async () => {
+ const root = await fixtures.temporaryDirectory();
+ const repository = join(root, "repository");
+ const codexHome = join(root, "codex-home");
+ const scanDir = join(root, "scan");
+ await mkdir(repository);
+ await mkdir(codexHome);
+ await mkdir(scanDir, { mode: 0o700 });
+
+ const targetId = "target_sha256_example";
+ const linkedFinding = {
+ findingId: "linked_worktree_finding",
+ occurrenceId: "linked_worktree_occurrence",
+ scanId: "linked_worktree_scan",
+ targetId: "target_linked_worktree",
+ };
+ const unrelatedFinding = {
+ findingId: "independent_clone_finding",
+ occurrenceId: "independent_clone_occurrence",
+ scanId: "independent_clone_scan",
+ targetId: "target_independent_clone",
+ };
+ const currentFinding = {
+ findingId: "csf_852f90d6e1177502ff113d4a",
+ occurrenceId: "occ_e79cb19591e696572a1c22be",
+ scanId: "scan_example_001",
+ targetId,
+ };
+ const mergedFinding = {
+ ...linkedFinding,
+ title: "Unsafe archive extraction",
+ summary: "Archive entries can escape their destination.",
+ severity: { level: "high" as const },
+ status: "open" as const,
+ confirmedInLatestScan: true,
+ knownScanIds: [linkedFinding.scanId, currentFinding.scanId],
+ };
+ const commands: Array<{ args: readonly string[]; input?: string }> = [];
+ const comparison = {
+ matches: [
+ {
+ beforeOccurrenceIds: [linkedFinding.occurrenceId],
+ afterOccurrenceIds: [currentFinding.occurrenceId],
+ confidence: "high",
+ reason: "The linked worktree has the same root cause.",
+ },
+ ],
+ uncertain: [],
+ };
+ const matchedInputs: Array<{
+ before: readonly Record<string, unknown>[];
+ after: readonly Record<string, unknown>[];
+ }> = [];
+
+ const client = new TestClient(
+ {},
+ {
+ environment: {},
+ prepareRuntime: async () => preparedRuntime(codexHome),
+ resolvePluginPython: async () => "/managed/python",
+ prepareOutputDir: async () => scanDir,
+ repositoryRevision: async () => "deadbeef",
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ input?: string,
+ ) => {
+ commands.push({ args, input });
+ if (args[0] === "register-cli-scan") {
+ return {
+ scanId: currentFinding.scanId,
+ targetId,
+ targetRevision: "deadbeef",
+ scanDir: args[args.indexOf("--scan-dir") + 1],
+ contract: { target: { allowedKinds: ["git_revision"] } },
+ };
+ }
+ if (args[0] === "get-scan-feedback") {
+ return {
+ scanId: currentFinding.scanId,
+ targetId,
+ falsePositives: [],
+ };
+ }
+ if (args[0] === "list-global-findings") {
+ expect(args).toContain("--target-id");
+ expect(args).toContain(targetId);
+ return {
+ findings: args.includes("--status")
+ ? [mergedFinding]
+ : [linkedFinding, currentFinding],
+ };
+ }
+ if (args[0] === "list-unmatched-scan-pairs") {
+ return {
+ batches: [
+ {
+ afterScanId: currentFinding.scanId,
+ afterFindings: [currentFinding],
+ beforeScans: [
+ {
+ scanId: linkedFinding.scanId,
+ findings: [linkedFinding],
+ },
+ {
+ scanId: unrelatedFinding.scanId,
+ findings: [unrelatedFinding],
+ },
+ ],
+ },
+ ],
+ };
+ }
+ return {};
+ },
+ async matchFindings(input: (typeof matchedInputs)[number]) {
+ matchedInputs.push(input);
+ return comparison;
+ },
+ createCodex: () => ({
+ startThread: () => ({
+ id: null,
+ async runStreamed() {
+ await copyCompletedScan(root);
+ return { events: completedEvents() };
+ },
+ }),
+ }),
+ },
+ );
+
+ const result = await client.run(repository);
+
+ expect(matchedInputs).toEqual([
+ { before: [linkedFinding], after: [currentFinding] },
+ ]);
+ expect(
+ commands.filter(({ args }) => args[0] === "save-scan-comparison"),
+ ).toEqual([
+ {
+ args: [
+ "save-scan-comparison",
+ "--before-scan-id",
+ linkedFinding.scanId,
+ "--after-scan-id",
+ currentFinding.scanId,
+ "--matches-json-stdin",
+ ],
+ input: JSON.stringify(comparison),
+ },
+ ]);
+ expect(result.repositoryFindings).toEqual([mergedFinding]);
+ await client.close();
+ });
+});
diff --git a/sdk/typescript/tests-ts/repository-findings.test.ts b/sdk/typescript/tests-ts/repository-findings.test.ts
index 4ea2981a7..a212d3aeb 100644
--- a/sdk/typescript/tests-ts/repository-findings.test.ts
+++ b/sdk/typescript/tests-ts/repository-findings.test.ts
@@ -118,3 +118,298 @@ print(json.dumps(result))
"renamed-again",
]);
});
+
+test("shares findings only between explicitly identified repository and scope aliases", async () => {
+ const python = await resolvePluginPython();
+ const probe = `
+import argparse, json, sqlite3, sys
+from pathlib import Path
+sys.path.insert(0, sys.argv[1])
+import workbench_native_indexes as indexes
+import workbench_target_state as state
+
+connection = sqlite3.connect(":memory:")
+connection.row_factory = sqlite3.Row
+connection.executescript("""
+CREATE TABLE security_targets(id TEXT, current_path TEXT, display_name TEXT, repository_identity TEXT, origin TEXT);
+CREATE TABLE scans(
+ id TEXT, target_id TEXT, target_path TEXT, repository_generation TEXT, scope TEXT,
+ updated_at TEXT, status TEXT, started_at TEXT, completed_at TEXT,
+ continuation_thread_id TEXT, cost_json TEXT, handoff_status TEXT,
+ mode TEXT, model TEXT, name TEXT, parent_scan_id TEXT, phase TEXT,
+ canceled_at TEXT, recipe_json TEXT, reasoning_effort TEXT, scan_dir TEXT,
+ target_revision TEXT, target_summary TEXT, completion_warnings_json TEXT DEFAULT '[]'
+);
+CREATE TABLE scan_progress(
+ scan_id TEXT, reportable_findings_count INTEGER DEFAULT 0,
+ scope_file_count INTEGER DEFAULT 0, review_items_completed INTEGER DEFAULT 0,
+ review_items_total INTEGER DEFAULT 0, updated_at TEXT
+);
+CREATE TABLE finding_occurrences(id TEXT, finding_id TEXT, severity TEXT, created_at TEXT, scan_id TEXT, title TEXT, summary TEXT);
+CREATE TABLE finding_triage(occurrence_id TEXT, status TEXT, updated_at TEXT, close_reason TEXT);
+CREATE TABLE finding_locations(occurrence_id TEXT, relative_path TEXT, role TEXT, sort_order INTEGER);
+CREATE TABLE scan_comparison_matches(before_occurrence_id TEXT, after_occurrence_id TEXT);
+""")
+history = Path(sys.argv[1]).parent / "shared" / "workbench-migrations.json"
+for migration in json.loads(history.read_text(encoding="utf-8")):
+ if migration["version"] in (61, 62, 64):
+ for statement in migration["statements"]:
+ connection.execute(statement)
+origin = "https://example.invalid/synthetic/repository"
+connection.executemany("INSERT INTO security_targets VALUES (?, ?, ?, ?, ?)", [
+ ("primary", "/primary", "Primary", "common-git-directory::.", origin),
+ ("linked", "/linked", "Linked", "common-git-directory::.", origin),
+ ("empty-alias", "/empty-alias", "Empty alias", "common-git-directory::.", origin),
+ ("same-origin-clone", "/clone", "Clone", "independent-git-directory::.", origin),
+ ("different-scope", "/primary/packages/api", "Scoped", "common-git-directory::packages/api", origin),
+ ("unknown-first", "/unknown-first", "Unknown first", None, origin),
+ ("unknown-second", "/unknown-second", "Unknown second", None, origin),
+])
+refused = set()
+def inspect(database, target_id, path, stored, **kwargs):
+ repository = state.GitRepositoryIdentity(stored, ".", "synthetic", 1, 2, 3, 3) if stored else None
+ return state.RepositoryTargetState(
+ target_id, path, stored, resolved_path=path, repository=repository,
+ ownership_matches=target_id not in refused, strict_owner_matches=True,
+ has_historical_scans=True,
+ )
+state._inspect_repository_target = inspect
+indexes.Path.is_dir = lambda path: True
+
+def add_scan(scan_id, target, day):
+ timestamp = f"2026-02-{day:02d}T00:00:00Z"
+ saved = connection.execute("SELECT current_path, repository_identity FROM security_targets WHERE id = ?", (target,)).fetchone()
+ connection.execute("INSERT INTO scans (id, target_id, target_path, repository_generation, scope, updated_at, status, started_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (scan_id, target, saved["current_path"], saved["repository_identity"], "repository", timestamp, "complete", timestamp))
+ connection.execute("INSERT INTO scan_progress (scan_id, updated_at) VALUES (?, ?)", (scan_id, timestamp))
+
+def add_finding(occurrence, finding, scan, severity="high"):
+ started = connection.execute("SELECT started_at FROM scans WHERE id = ?", (scan,)).fetchone()[0]
+ connection.execute("INSERT INTO finding_occurrences VALUES (?, ?, ?, ?, ?, ?, ?)", (occurrence, finding, severity, started, scan, finding, "Summary"))
+ connection.execute("INSERT INTO finding_locations VALUES (?, ?, ?, ?)", (occurrence, "src/auth.py", "root_control", 0))
+
+for scan_id, target, day in [
+ ("primary-old", "primary", 1),
+ ("empty-alias-scan", "empty-alias", 1),
+ ("linked-reviewed", "linked", 2),
+ ("primary-open", "primary", 3),
+ ("linked-latest", "linked", 4),
+ ("clone-scan", "same-origin-clone", 5),
+ ("scope-scan", "different-scope", 6),
+ ("unknown-first-scan", "unknown-first", 7),
+ ("unknown-second-scan", "unknown-second", 8),
+]:
+ add_scan(scan_id, target, day)
+
+for occurrence, finding, scan in [
+ ("dismissed-primary", "dismissed-original", "primary-old"),
+ ("dismissed-linked", "dismissed-renamed", "linked-reviewed"),
+ ("fixed-primary", "fixed-original", "primary-old"),
+ ("fixed-linked", "fixed-rediscovered", "linked-reviewed"),
+ ("wont-fix-primary", "wont-fix-original", "primary-old"),
+ ("wont-fix-linked", "wont-fix-renamed", "linked-reviewed"),
+ ("open-primary", "primary-only", "primary-open"),
+ ("same-id-primary", "same-id", "primary-open"),
+ ("open-linked", "linked-only", "linked-latest"),
+ ("same-id-linked", "same-id", "linked-latest"),
+ ("clone-occurrence", "clone-only", "clone-scan"),
+ ("scope-occurrence", "scope-only", "scope-scan"),
+ ("unknown-first-occurrence", "unknown-first-only", "unknown-first-scan"),
+ ("unknown-second-occurrence", "unknown-second-only", "unknown-second-scan"),
+]:
+ add_finding(occurrence, finding, scan)
+
+connection.executemany("INSERT INTO scan_comparison_matches VALUES (?, ?)", [
+ ("dismissed-primary", "dismissed-linked"),
+ ("fixed-primary", "fixed-linked"),
+ ("wont-fix-primary", "wont-fix-linked"),
+ ("open-linked", "clone-occurrence"),
+ ("open-linked", "scope-occurrence"),
+ ("unknown-first-occurrence", "unknown-second-occurrence"),
+])
+connection.executemany("INSERT INTO finding_triage VALUES (?, ?, ?, ?)", [
+ ("dismissed-primary", "closed", "2026-02-01T12:00:00Z", "false_positive"),
+ ("fixed-primary", "closed", "2026-02-01T12:00:00Z", "already_fixed"),
+ ("wont-fix-primary", "closed", "2026-02-01T12:00:00Z", "wont_fix"),
+])
+
+def findings(target, status="open", limit=20, offset=0):
+ arguments = argparse.Namespace(limit=limit, offset=offset, query=None, severity=None, status=status, target_id=target)
+ return indexes.list_global_findings(connection, arguments)
+
+repository_arguments = argparse.Namespace(
+ query=None, target_id=None, status="open_findings", limit=None, offset=0
+)
+result = {
+ "primary": findings("primary"),
+ "linked": findings("linked"),
+ "closed": findings("primary", "closed"),
+ "all": findings("primary", None),
+ "clone": findings("same-origin-clone"),
+ "differentScope": findings("different-scope"),
+ "unknownFirst": findings("unknown-first"),
+ "unknownSecond": findings("unknown-second"),
+ "firstPage": findings("primary", limit=2),
+ "secondPage": findings("primary", limit=2, offset=2),
+ "thirdPage": findings("primary", limit=2, offset=4),
+ "repositories": indexes.list_repositories(connection),
+ "openRepositories": indexes.list_repositories(connection, repository_arguments),
+}
+connection.execute(
+ "UPDATE finding_occurrences SET created_at = ? WHERE id = ?",
+ ("2026-02-05T00:00:00Z", "same-id-primary"),
+)
+result["overlappingCompletions"] = findings("primary", None)
+connection.execute(
+ "UPDATE finding_occurrences SET created_at = ? WHERE id = ?",
+ ("2026-02-03T00:00:00Z", "same-id-primary"),
+)
+connection.execute(
+ "INSERT INTO finding_triage VALUES (?, ?, ?, ?)",
+ ("same-id-primary", "closed", "2026-02-05T00:00:00Z", "false_positive"),
+)
+result["sameIdDismissed"] = findings("primary")
+result["sameIdClosed"] = findings("linked", "closed")
+connection.execute(
+ "INSERT INTO finding_triage VALUES (?, ?, ?, ?)",
+ ("same-id-linked", "open", "2026-02-06T00:00:00Z", None),
+)
+result["sameIdReopened"] = findings("primary")
+refused.add("primary")
+result["reusedPath"] = findings("primary")
+result["deletedAlias"] = findings("linked")
+print(json.dumps(result))
+`;
+
+ const execution = await runCodexCommand(
+ { command: python },
+ ["-I", "-B", "-", join(PLUGIN_ROOT, "scripts")],
+ process.env,
+ probe,
+ AbortSignal.timeout(10_000),
+ );
+ expect(execution.exitCode, execution.stderr).toBe(0);
+
+ const result = JSON.parse(execution.stdout) as Record<
+ string,
+ {
+ findings: Array<Record<string, unknown>>;
+ nextOffset: number | null;
+ repositories?: Array<Record<string, unknown>>;
+ }
+ >;
+ const primary = result["primary"]!.findings;
+ expect(result["linked"]!.findings).toEqual(primary);
+ expect(
+ result["overlappingCompletions"]!.findings.find(
+ (finding) => finding["findingId"] === "same-id",
+ ),
+ ).toMatchObject({
+ scanId: "primary-open",
+ confirmedInLatestScan: true,
+ knownScanIds: ["primary-open", "linked-latest"],
+ });
+ expect(primary.map((finding) => finding["findingId"])).toEqual([
+ "linked-only",
+ "same-id",
+ "primary-only",
+ "fixed-rediscovered",
+ ]);
+ expect(
+ primary.filter((finding) => finding["findingId"] === "same-id"),
+ ).toMatchObject([
+ {
+ targetId: "linked",
+ occurrenceCount: 2,
+ knownScanIds: ["primary-open", "linked-latest"],
+ matchedFindingIds: ["same-id"],
+ },
+ ]);
+ expect(
+ primary.find((finding) => finding["findingId"] === "linked-only"),
+ ).toMatchObject({
+ confirmedInLatestScan: true,
+ matchedFindingIds: ["linked-only"],
+ });
+ expect(
+ primary.find((finding) => finding["findingId"] === "primary-only"),
+ ).toMatchObject({ confirmedInLatestScan: false });
+ expect(
+ primary.find((finding) => finding["findingId"] === "fixed-rediscovered"),
+ ).toMatchObject({
+ status: "open",
+ knownScanIds: ["primary-old", "linked-reviewed"],
+ matchedFindingIds: ["fixed-original", "fixed-rediscovered"],
+ occurrenceCount: 2,
+ });
+ expect(result["closed"]!.findings).toMatchObject([
+ {
+ findingId: "dismissed-renamed",
+ status: "closed",
+ matchedFindingIds: ["dismissed-original", "dismissed-renamed"],
+ },
+ {
+ findingId: "wont-fix-renamed",
+ status: "closed",
+ matchedFindingIds: ["wont-fix-original", "wont-fix-renamed"],
+ },
+ ]);
+ expect(result["all"]!.findings).toHaveLength(6);
+ expect(result["clone"]!.findings).toMatchObject([
+ { findingId: "clone-only", targetId: "same-origin-clone" },
+ ]);
+ expect(result["differentScope"]!.findings).toMatchObject([
+ { findingId: "scope-only", targetId: "different-scope" },
+ ]);
+ expect(result["unknownFirst"]!.findings).toMatchObject([
+ { findingId: "unknown-first-only", targetId: "unknown-first" },
+ ]);
+ expect(result["unknownSecond"]!.findings).toMatchObject([
+ { findingId: "unknown-second-only", targetId: "unknown-second" },
+ ]);
+ expect(
+ Object.fromEntries(
+ result["repositories"]!.repositories!.map((repository) => [
+ repository["targetId"],
+ repository["openFindingsCount"],
+ ]),
+ ),
+ ).toEqual({
+ primary: 4,
+ linked: 4,
+ "empty-alias": 4,
+ "same-origin-clone": 1,
+ "different-scope": 1,
+ "unknown-first": 1,
+ "unknown-second": 1,
+ });
+ expect(
+ result["openRepositories"]!.repositories!.map(
+ (repository) => repository["targetId"],
+ ),
+ ).toContain("empty-alias");
+ expect(result["firstPage"]!.nextOffset).toBe(2);
+ expect(result["secondPage"]!.nextOffset).toBeNull();
+ expect(result["thirdPage"]!.nextOffset).toBeNull();
+ expect([
+ ...result["firstPage"]!.findings,
+ ...result["secondPage"]!.findings,
+ ...result["thirdPage"]!.findings,
+ ]).toEqual(primary);
+ expect(
+ result["sameIdDismissed"]!.findings.some(
+ (finding) => finding["findingId"] === "same-id",
+ ),
+ ).toBe(false);
+ expect(
+ result["sameIdClosed"]!.findings.find(
+ (finding) => finding["findingId"] === "same-id",
+ ),
+ ).toMatchObject({ status: "closed", occurrenceCount: 2 });
+ expect(
+ result["sameIdReopened"]!.findings.find(
+ (finding) => finding["findingId"] === "same-id",
+ ),
+ ).toMatchObject({ status: "open", occurrenceCount: 2 });
+ expect(result["reusedPath"]!.findings).toEqual([]);
+ expect(result["deletedAlias"]!.findings).toHaveLength(4);
+});
diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.ts
index d120d6abd..08ba802ab 100644
--- a/sdk/typescript/tests-ts/runtime.test.ts
+++ b/sdk/typescript/tests-ts/runtime.test.ts
@@ -7757,3 +7757,58 @@ describe("runtime directories and plugin Python boundary", () => {
},
);
});
+
+test.each([
+ ["legacy", false],
+ ["current", true],
+] as const)(
+ "lists automatic matching pairs with a %s custom plugin",
+ async (_kind, supportsFocus) => {
+ const root = await temporaryDirectory();
+ const pluginRoot = join(root, "custom-matching-plugin");
+ const scripts = join(pluginRoot, "scripts");
+ await mkdir(scripts, { recursive: true });
+ await writeFile(
+ join(scripts, "workbench_db.py"),
+ [
+ "import argparse, json, os, sys",
+ "from pathlib import Path",
+ "with Path(__file__).with_name('calls').open('a') as calls: calls.write(json.dumps(sys.argv[1:]) + '\\n')",
+ "parser = argparse.ArgumentParser()",
+ "command = parser.add_subparsers(dest='command', required=True).add_parser('list-unmatched-scan-pairs')",
+ "command.add_argument('--repository', required=True)",
+ ...(supportsFocus ? ["command.add_argument('--after-scan-id')"] : []),
+ "args = parser.parse_args()",
+ "if os.environ.get('FAIL_LIST'): sys.exit('Synthetic matching database failure')",
+ "print(json.dumps({'batches': [{'afterScanId': 'current'}]}))",
+ ].join("\n"),
+ );
+ const options = {
+ python: await resolvePluginPython(),
+ pluginRoot,
+ environment: { PATH: process.env["PATH"] },
+ };
+ const args = [
+ "list-unmatched-scan-pairs",
+ "--repository",
+ root,
+ "--after-scan-id",
+ "current",
+ ];
+ expect(await runWorkbench(options, args)).toEqual({
+ batches: [{ afterScanId: "current" }],
+ });
+ const calls = (await readFile(join(scripts, "calls"), "utf8"))
+ .trim()
+ .split("\n")
+ .map((line) => JSON.parse(line));
+ expect(calls).toEqual(supportsFocus ? [args] : [args, args.slice(0, -2)]);
+ expect(args.slice(-2)).toEqual(["--after-scan-id", "current"]);
+ await expect(
+ runWorkbench(
+ { ...options, environment: { ...options.environment, FAIL_LIST: "1" } },
+ args,
+ ),
+ ).rejects.toThrow("Synthetic matching database failure");
+ },
+);
diff --git a/sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts b/sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts
index de487a7b4..feb4625ef 100644
--- a/sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts
+++ b/sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts
@@ -7,13 +7,18 @@ import {
mkdir,
readFile,
realpath,
+ rename,
+ rm,
symlink,
writeFile,
} from "node:fs/promises";
import { delimiter, dirname, join, toNamespacedPath } from "node:path";
+import { fileURLToPath } from "node:url";
import { afterEach, expect, mock, test } from "bun:test";
import { deduplicateScanInternal } from "../src/deduplication/scan.js";
import {
+ savedCheckoutBoundaries,
+ savedDiscoveryRoots,
readSavedScanLifecycle,
savedScanWorkbench,
} from "../src/saved-scan-bootstrap.js";
@@ -127,6 +132,88 @@ function writePythonProbe(path: string) {
);
}
+test.each([
+ { recorded: true, corrected: false },
+ { recorded: false, corrected: false },
+ { recorded: true, corrected: true },
+ { recorded: false, corrected: true },
+])(
+ "a rejected workspace excludes its former checkout from Python discovery: %j",
+ async ({ recorded, corrected }) => {
+ const root = await temporaryDirectory();
+ const checkout = join(root, "untrusted");
+ const candidate = join(checkout, "requested");
+ await mkdir(checkout);
+ const database = new Database(join(root, "workbench.sqlite3"), {
+ create: true,
+ });
+ try {
+ database.exec(
+ "CREATE TABLE workspaces (target_path, target_path_unvalidated, target_id)",
+ );
+ database
+ .query("INSERT INTO workspaces VALUES (?, 1, NULL)")
+ .run(candidate);
+ if (recorded || corrected) {
+ database.exec(
+ "ALTER TABLE workspaces ADD COLUMN rejected_checkout_roots_json TEXT",
+ );
+ database
+ .query("UPDATE workspaces SET rejected_checkout_roots_json = ?")
+ .run(JSON.stringify(recorded ? [checkout, candidate] : null));
+ }
+ if (corrected) {
+ const target = join(root, "corrected");
+ database.exec(
+ "CREATE TABLE security_targets (current_path, git_checkout_root)",
+ );
+ database
+ .query("INSERT INTO security_targets VALUES (?, ?)")
+ .run(target, target);
+ database
+ .query(
+ "UPDATE workspaces SET target_path = ?, target_path_unvalidated = 0, target_id = 1",
+ )
+ .run(target);
+ }
+ } finally {
+ database.close();
+ }
+ const boundaries = await savedCheckoutBoundaries({
+ CODEX_SECURITY_STATE_DIR: root,
+ });
+ expect(boundaries?.complete).toBe(recorded);
+ const roots = await savedDiscoveryRoots(boundaries, []);
+ if (recorded) expect(roots).toContain(checkout);
+
+ if (process.platform !== "win32") {
+ const marker = join(root, "probed");
+ await writePythonProbe(join(checkout, "python3"));
+ await expect(
+ resolvePluginPython({
+ configuredPath: "python3",
+ environment: { PATH: checkout, TEST_PYTHON_PROBE: marker },
+ discoveryProtectedRoots: roots,
+ allowPathLookup: boundaries?.complete,
+ managedRuntimeRoots: [],
+ }),
+ ).rejects.toThrow();
+ expect(existsSync(marker)).toBe(false);
+ }
+ },
+);
+
+function insertSavedTarget(
+ db: Database,
+ id: string,
+ target: string,
+ checkoutRoot = target,
+) {
+ db.query(
+ "INSERT INTO security_targets (id, current_path, git_checkout_root, display_name, created_at, updated_at) VALUES (?, ?, ?, 'Synthetic target', '2026-01-01T00:00:00Z', '2026-01-01T00:00:00Z')",
+ ).run(id, target, checkoutRoot);
+}
+
async function workspace(versioned = false) {
const root = await temporaryDirectory();
const repository = join(root, "repository");
@@ -163,17 +250,31 @@ async function registerScan(
return registered["scanId"] as string;
}
-async function fixture(versioned = false, separateSetupCommands = false) {
+async function fixture(
+ versioned = false,
+ {
+ separateSetupCommands = false,
+ completedAt,
+ }: {
+ separateSetupCommands?: boolean;
+ completedAt?: readonly [string, string];
+ } = {},
+) {
const base = await workspace(versioned);
const { root, repository, environment, python } = base;
const options = { environment, python, pluginRoot: PLUGIN_ROOT };
- const registrationArguments = (scanDir: string) =>
- scanRegistrationArguments(repository, scanDir);
- async function scan(name: string, registration?: JsonObject) {
+ async function scan(
+ name: string,
+ target = repository,
+ timestamp?: string,
+ registration?: JsonObject,
+ ) {
const scanDir = join(root, name);
- const scanId = registration
- ? (registration["scanId"] as string)
- : await registerScan(base, scanDir);
+ if (!registration) await mkdir(scanDir, { mode: 0o700 });
+ const registered =
+ registration ??
+ (await runWorkbench(options, scanRegistrationArguments(target, scanDir)));
+ const scanId = registered["scanId"] as string;
await copyCompletedScanFixture(scanDir);
const manifest = JSON.parse(
await readFile(join(scanDir, "scan-manifest.json"), "utf8"),
@@ -183,9 +284,27 @@ async function fixture(versioned = false, separateSetupCommands = false) {
? "git_revision"
: "directory_snapshot";
if (versioned)
- manifest.scan.target.revision = git(repository, "rev-parse", "HEAD");
+ manifest.scan.target.revision = git(target, "rev-parse", "HEAD");
delete manifest.scan.sealedAt;
delete manifest.scan.artifacts;
+ if (timestamp !== undefined) {
+ const targetContract = (
+ registered["contract"] as {
+ target: {
+ targetId: string;
+ displayName: string;
+ requiredSnapshotDigest: string;
+ };
+ }
+ ).target;
+ manifest.scan.completedAt = timestamp;
+ manifest.scan.target = {
+ kind: "directory_snapshot",
+ targetId: targetContract.targetId,
+ displayName: targetContract.displayName,
+ snapshotDigest: targetContract.requiredSnapshotDigest,
+ };
+ }
await writeFile(
join(scanDir, "scan-manifest.json"),
JSON.stringify(manifest),
@@ -220,15 +339,29 @@ async function fixture(versioned = false, separateSetupCommands = false) {
);
coverage.scanId = scanId;
await writeFile(join(scanDir, "coverage.json"), JSON.stringify(coverage));
- if (separateSetupCommands)
+ if (timestamp !== undefined)
+ execFileSync(
+ python,
+ [
+ "-I",
+ "-B",
+ join(PLUGIN_ROOT, "scripts", "finalize_scan_contract.py"),
+ "--scan-dir",
+ scanDir,
+ "--source-root",
+ target,
+ ],
+ { env: environment },
+ );
+ if (!registration)
await runWorkbench(options, ["complete-scan", "--scan-id", scanId]);
return { scanId, scanDir };
}
let first: Awaited<ReturnType<typeof scan>>;
let second: Awaited<ReturnType<typeof scan>>;
if (separateSetupCommands) {
- first = await scan("first");
- second = await scan("second");
+ first = await scan("first", repository, completedAt?.[0]);
+ second = await scan("second", repository, completedAt?.[1]);
} else {
const directories = [join(root, "first"), join(root, "second")];
for (const directory of directories)
@@ -236,10 +369,22 @@ async function fixture(versioned = false, separateSetupCommands = false) {
const registrations = await runWorkbenchFixtureCommands(
python,
environment,
- directories.map(registrationArguments),
+ directories.map((directory) =>
+ scanRegistrationArguments(repository, directory),
+ ),
+ );
+ first = await scan(
+ "first",
+ repository,
+ completedAt?.[0],
+ registrations[0]!,
+ );
+ second = await scan(
+ "second",
+ repository,
+ completedAt?.[1],
+ registrations[1]!,
);
- first = await scan("first", registrations[0]!);
- second = await scan("second", registrations[1]!);
await runWorkbenchFixtureCommands(
python,
environment,
@@ -281,6 +426,7 @@ async function fixture(versioned = false, separateSetupCommands = false) {
...base,
first,
second,
+ scan,
embedding,
embed,
reviewer,
@@ -289,7 +435,7 @@ async function fixture(versioned = false, separateSetupCommands = false) {
test("historical reviews carry the saved occurrence revision instead of the current checkout", async () => {
// Keep an end-to-end registration/completion fixture on separate processes.
- const f = await fixture(true, true);
+ const f = await fixture(true, { separateSetupCommands: true });
const manifest = JSON.parse(
await readFile(join(f.first.scanDir, "scan-manifest.json"), "utf8"),
);
@@ -389,9 +535,20 @@ for (const source of ["PYTHON", "PATH", "linked PYTHON"] as const) {
}
test.skipIf(process.platform === "win32")(
- "saved-scan dedupe excludes repository Node when invoking native findings helpers",
+ "saved-scan dedupe excludes Node from the saved checkout when invoking native findings helpers",
async () => {
- const f = await fixture();
+ const f = await fixture(true);
+ const component = join(f.repository, "component");
+ await mkdir(join(component, "src"), { recursive: true });
+ await writeFile(
+ join(component, "src", "extract.py"),
+ "# Synthetic fixture\n",
+ );
+ git(f.repository, "add", ".");
+ git(f.repository, "commit", "-qm", "Add component");
+ const scan = await f.scan("component-scan", component);
+ await rename(join(f.repository, ".git"), join(f.root, "old-git"));
+ git(component, "init", "--quiet");
const executable = join(f.repository, "node");
const marker = join(f.root, "node-probed");
await writeFile(
@@ -399,21 +556,35 @@ test.skipIf(process.platform === "win32")(
'#!/bin/sh\nprintf probed > "$TEST_NODE_PROBE"\nexit 1\n',
);
await chmod(executable, 0o700);
- const result = await deduplicateScanInternal(
- f.first.scanId,
- { embedding: f.embedding },
- {
- environment: {
- ...f.environment,
- PYTHON: f.python,
- PATH: [f.repository, f.environment.PATH].join(delimiter),
- TEST_NODE_PROBE: marker,
+ const deduplicate = () =>
+ deduplicateScanInternal(
+ scan.scanId,
+ { embedding: f.embedding },
+ {
+ environment: {
+ ...f.environment,
+ PYTHON: f.python,
+ PATH: [f.repository, f.environment.PATH].join(delimiter),
+ TEST_NODE_PROBE: marker,
+ },
+ reviewer: f.reviewer,
+ fetch: rejecting("Unexpected HTTP request"),
},
- reviewer: f.reviewer,
- fetch: rejecting("Unexpected HTTP request"),
- },
+ );
+ expect((await deduplicate()).deduplicationStatus).toBe("completed");
+ const db = new Database(
+ join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
+ );
+ try {
+ db.query(
+ "UPDATE security_targets SET git_checkout_root = NULL WHERE current_path = ?",
+ ).run(component);
+ } finally {
+ db.close();
+ }
+ await expect(deduplicate()).rejects.toThrow(
+ "Run local deduplication with Node.js",
);
- expect(result.deduplicationStatus).toBe("completed");
expect(existsSync(marker)).toBe(false);
},
);
@@ -515,7 +686,7 @@ test.skipIf(process.platform === "win32").each([true, false])(
},
);
-test("a no-Git latest workflow resumes its pending group write by explicit scan ID", async () => {
+test("a no-Git explicit scan workflow resumes its pending group write", async () => {
const f = await fixture(true);
const hostGit = await inspectTrustedExecutable("git", f.environment, []);
expect(hostGit.executable).not.toBeNull();
@@ -523,12 +694,10 @@ test("a no-Git latest workflow resumes its pending group write by explicit scan
join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
);
try {
- db.query("INSERT INTO security_targets VALUES (?, ?, ?, ?, ?)").run(
+ insertSavedTarget(
+ db,
"unrelated-target",
dirname(await realpath(hostGit.executable!)),
- "Synthetic target",
- "created",
- "updated",
);
} finally {
db.close();
@@ -544,14 +713,25 @@ test("a no-Git latest workflow resumes its pending group write by explicit scan
currentDirectory: f.repository,
});
expect(bootstrap.environment["CODEX_SECURITY_GIT"]).toBe("");
+ await expect(
+ resolveCompletedScan("latest", {
+ currentDirectory: () => f.repository,
+ runWorkbench: bootstrap,
+ }),
+ ).rejects.toThrow("codex-security dedupe --scan SCAN_ID");
+ const explicit = await savedScanWorkbench(f.second.scanId, {
+ environment: bootstrap.environment,
+ pluginRoot: PLUGIN_ROOT,
+ currentDirectory: f.repository,
+ });
const options = { embedding: f.embedding, workflowId: "no-git-replay" };
await expect(
- deduplicateScanInternal("latest", options, {
+ deduplicateScanInternal(f.second.scanId, options, {
environment: bootstrap.environment,
currentDirectory: () => f.repository,
reviewer: f.reviewer,
runWorkbench: async (args, input) => {
- const result = await bootstrap(args, input);
+ const result = await explicit(args, input);
if (args[0] === "store-dedupe-groups")
throw new Error("Lost group acknowledgement");
return result;
@@ -694,6 +874,164 @@ test.skipIf(process.platform === "win32")(
},
);
+test.skipIf(process.platform === "win32")(
+ "a replacement nested checkout cannot supply Git or Python from its former parent",
+ async () => {
+ const f = await fixture(true);
+ const outer = join(f.root, "former-checkout");
+ const component = join(outer, "src");
+ git(f.root, "clone", "--quiet", f.repository, outer);
+ const scanId = await registerScan(
+ { ...f, repository: component },
+ join(f.root, "component-scan"),
+ );
+ const caller = join(f.root, "caller");
+ git(f.repository, "worktree", "add", "--quiet", "--detach", caller);
+ await rename(join(outer, ".git"), join(f.root, "former-git-metadata"));
+ git(component, "init", "--quiet");
+
+ const gitProbe = join(f.root, "git-probed");
+ const pythonProbe = join(f.root, "python-probed");
+ const hostGit = (await inspectTrustedExecutable("git", f.environment, []))
+ .executable!;
+ await writeFile(
+ join(outer, "git"),
+ '#!/bin/sh\nprintf probed > "$TEST_GIT_PROBE"\nexec "$TEST_HOST_GIT" "$@"\n',
+ { mode: 0o700 },
+ );
+ await writePythonProbe(join(outer, "python3"));
+ const options = {
+ environment: {
+ ...f.environment,
+ PATH: outer + delimiter + f.environment.PATH,
+ TEST_HOST_GIT: hostGit,
+ TEST_GIT_PROBE: gitProbe,
+ TEST_PYTHON_PROBE: pythonProbe,
+ },
+ pluginRoot: PLUGIN_ROOT,
+ currentDirectory: caller,
+ };
+ async function latest(configured?: string) {
+ const workbench = await savedScanWorkbench("latest", {
+ ...options,
+ environment: {
+ ...options.environment,
+ PYTHON: f.python,
+ CODEX_SECURITY_GIT: configured,
+ },
+ });
+ return resolveCompletedScan("latest", {
+ currentDirectory: () => caller,
+ runWorkbench: workbench,
+ });
+ }
+ expect((await latest()).scanId).toBe(f.second.scanId);
+ expect(existsSync(gitProbe)).toBe(false);
+
+ async function getById() {
+ const workbench = await savedScanWorkbench(scanId, {
+ ...options,
+ environment: { ...options.environment, PYTHON: "python3" },
+ });
+ return workbench(["get-scan", "--scan-id", scanId]);
+ }
+ expect((await getById())["scan"]).toMatchObject({ scanId });
+ expect(existsSync(pythonProbe)).toBe(false);
+
+ const db = new Database(
+ join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
+ );
+ try {
+ db.query(
+ "UPDATE security_targets SET git_checkout_root = NULL WHERE current_path = ?",
+ ).run(component);
+ } finally {
+ db.close();
+ }
+ for (const configured of [undefined, "git"])
+ await expect(latest(configured)).rejects.toThrow(
+ "codex-security dedupe --scan SCAN_ID",
+ );
+ await expect(getById()).rejects.toThrow("absolute trusted interpreter");
+ expect((await latest(hostGit)).scanId).toBe(f.second.scanId);
+ expect(existsSync(gitProbe)).toBe(false);
+ expect(existsSync(pythonProbe)).toBe(false);
+ },
+);
+
+for (const history of ["scan", "workspace"] as const) {
+ test.skipIf(process.platform === "win32")(
+ `scans list avoids ambient Python from a former unborn checkout with a legacy ${history}`,
+ async () => {
+ const f = await workspace();
+ const former = join(f.root, "former");
+ const target = join(former, "component");
+ await mkdir(target, { recursive: true });
+ git(former, "init", "-q");
+ const scanDir = join(f.root, "scan");
+ let expected: string[] = [];
+ if (history === "scan") {
+ expected = [await registerScan({ ...f, repository: target }, scanDir)];
+ } else {
+ await runWorkbench({ ...f, pluginRoot: PLUGIN_ROOT }, [
+ "create-workspace",
+ "--workspace-id",
+ "00000000-0000-4000-8000-000000000001",
+ "--target-path",
+ target,
+ ]);
+ }
+ const state = await copyLegacyWorkbench(f, 60);
+ await rename(join(former, ".git"), join(f.root, "previous-metadata"));
+ const executable = join(former, "python3");
+ await writeFile(
+ executable,
+ '#!/bin/sh\nprintf probed > "$0.ran"\nexit 1\n',
+ { mode: 0o700 },
+ );
+ const cache = join(f.root, "cache");
+ const cli = fileURLToPath(new URL("../src/cli.ts", import.meta.url));
+ const command = ["scans", "list", "--scan-root", scanDir, "--json"];
+ function list(python?: string) {
+ return spawnSync(process.execPath, [cli, ...command], {
+ cwd: f.repository,
+ env: {
+ ...f.environment,
+ CODEX_SECURITY_STATE_DIR: state,
+ XDG_CACHE_HOME: cache,
+ PYTHON: python,
+ PATH: former + delimiter + f.environment.PATH,
+ },
+ encoding: "utf8",
+ });
+ }
+ for (const python of [undefined, "python3"]) {
+ const result = list(python);
+ expect(existsSync(executable + ".ran")).toBe(false);
+ expect(result.status).toBe(2);
+ expect(result.stderr).toContain("absolute trusted interpreter");
+ }
+ const managed = join(
+ cache,
+ "codex-runtimes",
+ "codex-primary-runtime/dependencies/python/bin/python3",
+ );
+ await mkdir(dirname(managed), { recursive: true });
+ await symlink(f.python, managed);
+ for (const python of [f.python, undefined]) {
+ const result = list(python);
+ expect(result.status, result.stderr).toBe(0);
+ expect(
+ JSON.parse(result.stdout).scans.map(
+ (scan: { scanId: string }) => scan.scanId,
+ ),
+ ).toEqual(expected);
+ expect(existsSync(executable + ".ran")).toBe(false);
+ }
+ },
+ );
+}
+
test("latest explains the explicit scan-ID fallback when history protects the host Git", async () => {
const f = await fixture(true);
const worktree = join(f.root, "other-worktree");
@@ -708,12 +1046,10 @@ test("latest explains the explicit scan-ID fallback when history protects the ho
join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
);
try {
- db.query("INSERT INTO security_targets VALUES (?, ?, ?, ?, ?)").run(
+ insertSavedTarget(
+ db,
"unrelated-target",
dirname(await realpath(hostGit.executable!)),
- "Unrelated synthetic target",
- "2026-01-01T00:00:00Z",
- "2026-01-01T00:00:00Z",
);
} finally {
db.close();
@@ -743,14 +1079,12 @@ test("latest explains the explicit scan-ID fallback when history protects the ho
...options,
currentDirectory: f.repository,
});
- expect(
- (
- await resolveCompletedScan("latest", {
- currentDirectory: () => f.repository,
- runWorkbench: exactPath,
- })
- ).scanId,
- ).toBe(f.second.scanId);
+ await expect(
+ resolveCompletedScan("latest", {
+ currentDirectory: () => f.repository,
+ runWorkbench: exactPath,
+ }),
+ ).rejects.toThrow("codex-security dedupe --scan SCAN_ID");
});
test.skipIf(process.platform === "win32")(
@@ -946,6 +1280,29 @@ test.skipIf(process.platform === "win32")(
test("latest and workflow selectors ignore unrelated history enclosing trusted Python", async () => {
const f = await fixture(true);
+ const trustedRuntime = join(f.root, "trusted-python");
+ // Keep the interpreter separate from host Git, including when Python comes
+ // from a virtualenv whose packages (such as Python 3.10's tomli) are needed.
+ execFileSync(f.python, [
+ "-I",
+ "-c",
+ [
+ "import pathlib, site, subprocess, sys",
+ "base = pathlib.Path(sys.base_prefix)",
+ "names = ['python.exe'] if sys.platform == 'win32' else [f'bin/python{sys.version_info.major}.{sys.version_info.minor}', 'bin/python3', 'bin/python']",
+ "python = next(base / name for name in names if (base / name).is_file())",
+ "subprocess.run([str(python), '-m', 'venv', '--copies', '--without-pip', '--system-site-packages', sys.argv[1]], check=True)",
+ "isolated = pathlib.Path(sys.argv[1]) / ('Scripts/python.exe' if sys.platform == 'win32' else 'bin/python')",
+ "library = subprocess.check_output([str(isolated), '-I', '-c', 'import sysconfig; print(sysconfig.get_path(\"purelib\"))'], text=True).strip()",
+ "pathlib.Path(library, 'fixture-parent.pth').write_text('\\n'.join(site.getsitepackages()) + '\\n', encoding='utf-8')",
+ ].join("\n"),
+ trustedRuntime,
+ ]);
+ const trustedPython = join(
+ trustedRuntime,
+ process.platform === "win32" ? "Scripts" : "bin",
+ process.platform === "win32" ? "python.exe" : "python",
+ );
const workflowId = "scoped-bootstrap";
await deduplicateScanInternal(
f.first.scanId,
@@ -956,21 +1313,15 @@ test("latest and workflow selectors ignore unrelated history enclosing trusted P
join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
);
try {
- db.query("INSERT INTO security_targets VALUES (?, ?, ?, ?, ?)").run(
- "unrelated-target",
- dirname(f.python),
- "Unrelated synthetic target",
- "2026-01-01T00:00:00Z",
- "2026-01-01T00:00:00Z",
- );
+ insertSavedTarget(db, "unrelated-target", dirname(trustedPython));
db.query(
- "UPDATE scans SET target_path = ?, target_id = ? WHERE id = ?",
- ).run(dirname(f.python), "unrelated-target", f.second.scanId);
+ "UPDATE scans SET target_path = ?, target_id = ?, repository_generation = NULL, target_revision = 'unversioned' WHERE id = ?",
+ ).run(dirname(trustedPython), "unrelated-target", f.second.scanId);
} finally {
db.close();
}
const options = {
- environment: { ...f.environment, PYTHON: f.python },
+ environment: { ...f.environment, PYTHON: trustedPython },
pluginRoot: PLUGIN_ROOT,
currentDirectory: f.repository,
};
@@ -1133,13 +1484,13 @@ test.skipIf(process.platform === "win32")(
},
);
-for (const version of [1, 15]) {
- test(`latest upgrades schema ${version} after selecting a trusted target`, async () => {
- const f = await fixture();
- const state = join(f.root, `legacy-${version}`);
- await mkdir(state);
- const database = join(state, "workbench.sqlite3");
- const script = `
+async function copyLegacyWorkbench(
+ f: Awaited<ReturnType<typeof workspace>>,
+ version: number,
+) {
+ const state = join(f.root, `legacy-${version}`);
+ await mkdir(state);
+ const script = `
import sqlite3, sys
sys.path.insert(0, sys.argv[1])
from workbench_schema import MIGRATIONS
@@ -1150,22 +1501,31 @@ for version, name, sql in MIGRATIONS:
connection.executescript(sql)
connection.execute("INSERT INTO schema_migrations VALUES (?, ?, ?)", (version, name, "2026-01-01T00:00:00Z"))
connection.execute("ATTACH DATABASE ? AS source", (sys.argv[3],))
-for table in ["workspaces", "scans", "scan_progress"]:
+for table in ["security_targets", "workspaces", "scans", "scan_progress"]:
columns = ",".join(row[1] for row in connection.execute(f"PRAGMA main.table_info({table})"))
+ if not columns: continue
connection.execute(f"INSERT INTO main.{table} ({columns}) SELECT {columns} FROM source.{table}")
connection.commit()
connection.close()
`;
- execFileSync(f.python, [
- "-I",
- "-B",
- "-c",
- script,
- join(PLUGIN_ROOT, "scripts"),
- database,
- join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
- String(version),
- ]);
+ execFileSync(f.python, [
+ "-I",
+ "-B",
+ "-c",
+ script,
+ join(PLUGIN_ROOT, "scripts"),
+ join(state, "workbench.sqlite3"),
+ join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
+ String(version),
+ ]);
+ return state;
+}
+
+for (const version of [1, 15]) {
+ test(`latest upgrades schema ${version} after selecting a trusted target`, async () => {
+ const f = await fixture();
+ const state = await copyLegacyWorkbench(f, version);
+ const database = join(state, "workbench.sqlite3");
const workbench = await savedScanWorkbench("latest", {
environment: {
...f.environment,
@@ -1306,15 +1666,9 @@ for (const kind of ["non-Git", "symlinked"] as const) {
join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
);
try {
- db.query("INSERT INTO security_targets VALUES (?, ?, ?, ?, ?)").run(
- "non-git-target",
- target,
- "Synthetic non-Git target",
- "2026-01-01T00:00:00Z",
- "2026-01-01T00:00:00Z",
- );
+ insertSavedTarget(db, "non-git-target", target, other);
db.query(
- "UPDATE scans SET target_path = ?, target_id = ? WHERE id = ?",
+ "UPDATE scans SET target_path = ?, target_id = ?, repository_generation = NULL, target_revision = 'unversioned' WHERE id = ?",
).run(target, "non-git-target", f.second.scanId);
} finally {
db.close();
@@ -1427,29 +1781,22 @@ for (const kind of ["missing", "symlinked"] as const) {
test.skipIf(process.platform === "win32")(
`saved ${kind} targets still protect their enclosing checkout from ${source}`,
async () => {
- const f = await fixture(true);
- const target = join(f.repository, "deleted-component", "nested");
+ const f = await workspace(true);
+ const target = join(f.repository, "src");
+ const scanId = await registerScan(
+ { ...f, repository: target },
+ join(f.root, "scan"),
+ );
+ await rename(target, join(f.root, "removed-component"));
if (kind === "symlinked") {
const destination = join(f.root, "external-target");
await mkdir(destination);
- await mkdir(dirname(target), { recursive: true });
await symlink(destination, target);
}
const python = join(f.repository, "python3");
const marker = join(f.root, "missing-target-probed");
await writePythonProbe(python);
- const db = new Database(
- join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
- );
- try {
- db.query("UPDATE scans SET target_path = ? WHERE id = ?").run(
- target,
- f.first.scanId,
- );
- } finally {
- db.close();
- }
- const workbench = await savedScanWorkbench(f.first.scanId, {
+ const workbench = await savedScanWorkbench(scanId, {
environment: {
...f.environment,
TEST_PYTHON_PROBE: marker,
@@ -1460,15 +1807,13 @@ for (const kind of ["missing", "symlinked"] as const) {
pluginRoot: PLUGIN_ROOT,
currentDirectory: f.root,
});
- const result = workbench(["get-scan", "--scan-id", f.first.scanId]);
+ const result = workbench(["get-scan", "--scan-id", scanId]);
if (source === "PYTHON")
await expect(result).rejects.toThrow(
"PYTHON interpreter is unavailable or unusable",
);
else {
- expect((await result)["scan"]).toMatchObject({
- scanId: f.first.scanId,
- });
+ expect((await result)["scan"]).toMatchObject({ scanId });
expect(workbench.environment["PYTHON"]).not.toBe(python);
}
expect(existsSync(marker)).toBe(false);
@@ -1496,15 +1841,9 @@ test.skipIf(process.platform === "win32")(
join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
);
try {
- db.query("INSERT INTO security_targets VALUES (?, ?, ?, ?, ?)").run(
- "inaccessible-target",
- target,
- "Synthetic target",
- "2026-01-01T00:00:00Z",
- "2026-01-01T00:00:00Z",
- );
+ insertSavedTarget(db, "inaccessible-target", target, other);
db.query(
- "UPDATE scans SET target_path = ?, target_id = ? WHERE id = ?",
+ "UPDATE scans SET target_path = ?, target_id = ?, repository_generation = NULL, target_revision = 'unversioned' WHERE id = ?",
).run(target, "inaccessible-target", f.second.scanId);
} finally {
db.close();
@@ -1535,3 +1874,529 @@ test.skipIf(process.platform === "win32")(
}
},
);
+
+test.each(["checkout", "git metadata"])(
+ "latest excludes the prior owner after replacing %s at the same path",
+ async (replacement) => {
+ const f = await fixture(true);
+ const options = {
+ environment: { ...f.environment, PYTHON: f.python },
+ pluginRoot: PLUGIN_ROOT,
+ currentDirectory: f.repository,
+ };
+ const before = await savedScanWorkbench("latest", options);
+ expect(
+ (
+ await resolveCompletedScan("latest", {
+ currentDirectory: () => f.repository,
+ runWorkbench: before,
+ })
+ ).scanId,
+ ).toBe(f.second.scanId);
+ if (replacement === "checkout") {
+ await rename(f.repository, join(f.root, "previous-checkout"));
+ await mkdir(f.repository);
+ } else {
+ await rename(
+ join(f.repository, ".git"),
+ join(f.root, "previous-git-metadata"),
+ );
+ }
+ execFileSync("git", ["init", "--quiet", f.repository]);
+ const workbench = await savedScanWorkbench("latest", options);
+ await expect(
+ resolveCompletedScan("latest", {
+ currentDirectory: () => f.repository,
+ runWorkbench: workbench,
+ }),
+ ).rejects.toThrow("No completed saved scan was found for this repository.");
+ },
+);
+
+async function latestFor(
+ f: Awaited<ReturnType<typeof fixture>>,
+ directory: string,
+) {
+ const workbench = await savedScanWorkbench("latest", {
+ environment: { ...f.environment, PYTHON: f.python },
+ pluginRoot: PLUGIN_ROOT,
+ currentDirectory: directory,
+ });
+ return resolveCompletedScan("latest", {
+ currentDirectory: () => directory,
+ runWorkbench: workbench,
+ });
+}
+
+test.each(["checkout", "linked worktree", "same-origin clone"])(
+ "latest keeps component scope when a sibling in a %s has newer history",
+ async (kind) => {
+ const f = await fixture(true);
+ for (const component of ["a", "b"]) {
+ await mkdir(join(f.repository, component, "src"), { recursive: true });
+ await writeFile(
+ join(f.repository, component, "src", "extract.py"),
+ "# Synthetic component\n",
+ );
+ }
+ execFileSync("git", ["-C", f.repository, "add", "."]);
+ execFileSync("git", [
+ "-C",
+ f.repository,
+ "-c",
+ "user.name=Synthetic Fixture",
+ "-c",
+ "commit",
+ "-qm",
+ "Synthetic components",
+ ]);
+ let other = f.repository;
+ if (kind === "linked worktree") {
+ other = join(f.root, "linked");
+ execFileSync("git", [
+ "-C",
+ f.repository,
+ "worktree",
+ "add",
+ "--quiet",
+ "--detach",
+ other,
+ "HEAD",
+ ]);
+ } else if (kind === "same-origin clone") {
+ other = join(f.root, "clone");
+ execFileSync("git", ["clone", "--quiet", f.repository, other]);
+ execFileSync("git", [
+ "-C",
+ f.repository,
+ "remote",
+ "add",
+ "origin",
+ "https://example.test/team/components.git",
+ ]);
+ execFileSync("git", [
+ "-C",
+ other,
+ "remote",
+ "set-url",
+ "origin",
+ "https://example.test/team/components.git",
+ ]);
+ }
+ const own = await f.scan("component-a", join(f.repository, "a"));
+ await f.scan("component-b", join(other, "b"));
+ expect((await latestFor(f, join(f.repository, "a"))).scanId).toBe(
+ own.scanId,
+ );
+ },
+);
+
+test("latest continues to an older valid scan after rejecting a replaced same-origin clone", async () => {
+ const f = await fixture(true);
+ const clone = join(f.root, "clone");
+ execFileSync("git", ["clone", "--quiet", f.repository, clone]);
+ execFileSync("git", [
+ "-C",
+ f.repository,
+ "remote",
+ "add",
+ "origin",
+ "https://example.test/team/replacement.git",
+ ]);
+ execFileSync("git", [
+ "-C",
+ clone,
+ "remote",
+ "set-url",
+ "origin",
+ "https://example.test/team/replacement.git",
+ ]);
+ const newest = await f.scan("clone-scan", clone);
+ expect((await latestFor(f, f.repository)).scanId).toBe(newest.scanId);
+ await rename(join(clone, ".git"), join(f.root, "previous-clone-metadata"));
+ execFileSync("git", ["init", "--quiet", clone]);
+ execFileSync("git", [
+ "-C",
+ clone,
+ "remote",
+ "add",
+ "origin",
+ "https://example.test/team/replacement.git",
+ ]);
+ expect((await latestFor(f, f.repository)).scanId).toBe(f.second.scanId);
+});
+
+test.each(["removed", "recreated directory", "replacement Git checkout"])(
+ "latest retains generation-bound history when its worktree path is %s",
+ async (state) => {
+ const f = await fixture(true);
+ const linked = join(f.root, "linked");
+ execFileSync("git", [
+ "-C",
+ f.repository,
+ "worktree",
+ "add",
+ "--quiet",
+ "--detach",
+ linked,
+ "HEAD",
+ ]);
+ const newest = await f.scan("removed-worktree-scan", linked);
+ execFileSync("git", [
+ "-C",
+ f.repository,
+ "worktree",
+ "remove",
+ "--force",
+ linked,
+ ]);
+ if (state !== "removed") await mkdir(linked);
+ if (state === "replacement Git checkout")
+ execFileSync("git", ["init", "--quiet", linked]);
+ const db = new Database(
+ join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
+ );
+ try {
+ db.query(
+ "UPDATE security_targets SET repository_identity = NULL WHERE id = (SELECT target_id FROM scans WHERE id = ?)",
+ ).run(newest.scanId);
+ db.query("UPDATE scans SET status = 'failed' WHERE id IN (?, ?)").run(
+ f.first.scanId,
+ f.second.scanId,
+ );
+ } finally {
+ db.close();
+ }
+ expect((await latestFor(f, f.repository)).scanId).toBe(newest.scanId);
+ const hostGit = await inspectTrustedExecutable("git", f.environment, [
+ f.repository,
+ linked,
+ ]);
+ expect(hostGit.executable).not.toBeNull();
+ // The direct Python helper needs the same explicit host Git binding as the SDK.
+ const canonical = await resolveCompletedScan("latest", {
+ currentDirectory: () => f.repository,
+ runWorkbench: (args, input, signal) =>
+ runWorkbench(
+ {
+ environment: {
+ ...f.environment,
+ CODEX_SECURITY_GIT: hostGit.executable!,
+ },
+ pluginRoot: PLUGIN_ROOT,
+ python: f.python,
+ signal,
+ },
+ args,
+ input,
+ ),
+ });
+ expect(canonical.scanId).toBe(newest.scanId);
+ },
+);
+
+test.each([
+ ["2099-01-01T02:00:00+02:00", "2099-01-01T01:00:00Z"],
+ ["2099-01-01T00:00:00Z", "2099-01-01T00:00:00.500Z"],
+] as const)(
+ "latest orders sealed completion instants %s and %s",
+ async (older, newer) => {
+ const f = await fixture(false, { completedAt: [older, newer] });
+ expect((await latestFor(f, f.repository)).scanId).toBe(f.second.scanId);
+ },
+);
+
+test("latest preserves exact update ordering ahead of scan-ID ties", async () => {
+ const f = await fixture();
+ const cases = [
+ ["2099-01-01T00:00:00.000100Z", "2099-01-01T00:00:00.000200Z"],
+ ["2099-01-01t02:00:00.10+02:00", "2099-01-01t00:00:00.1001z"],
+ ["2099-01-01T23:59:59-01:00", "2099-01-02T01:00:00Z"],
+ ["0001-01-01T00:00:00Z", "0001-01-01T00:00:00.1Z"],
+ ["9999-12-31T23:30:00Z", "9999-12-31T23:30:00-01:00"],
+ [
+ `2099-01-01T00:00:00.${"0".repeat(100_000)}1Z`,
+ `2099-01-01T00:00:00.${"0".repeat(100_000)}2Z`,
+ ],
+ ["2099-01-01T00:00:00", "2099-01-01T01:00:00"],
+ ] as const;
+ for (const [index, [olderTime, newerTime]] of cases.entries()) {
+ const [older, newer] = [f.first, f.second].sort((left, right) =>
+ left.scanId.localeCompare(right.scanId),
+ );
+ const db = new Database(
+ join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
+ );
+ try {
+ for (const [scan, timestamp] of [
+ [older!, olderTime],
+ [newer!, newerTime],
+ ] as const) {
+ db.query(
+ "UPDATE scans SET started_at = '2099-01-01T00:00:00Z', updated_at = ? WHERE id = ?",
+ ).run(timestamp, scan.scanId);
+ db.query(
+ "UPDATE scan_progress SET updated_at = ? WHERE scan_id = ?",
+ ).run(timestamp, scan.scanId);
+ }
+ } finally {
+ db.close();
+ }
+ expect(
+ (await latestFor(f, f.repository)).scanId,
+ `timestamp case ${index}`,
+ ).toBe(newer!.scanId);
+ }
+});
+
+test.each([false, true])(
+ "latest takes the exact scan/progress maximum (progress newer: %p)",
+ async (progressNewer) => {
+ const f = await fixture();
+ const db = new Database(
+ join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
+ );
+ try {
+ const older = "2099-01-01T02:00:00+02:00";
+ const newer = "2099-01-01T00:00:00.0002Z";
+ db.query("UPDATE scans SET updated_at = ? WHERE id = ?").run(
+ progressNewer ? older : newer,
+ f.first.scanId,
+ );
+ db.query("UPDATE scan_progress SET updated_at = ? WHERE scan_id = ?").run(
+ progressNewer ? newer : older,
+ f.first.scanId,
+ );
+ for (const table of ["scans", "scan_progress"]) {
+ const id = table === "scans" ? "id" : "scan_id";
+ db.query(`UPDATE ${table} SET updated_at = ? WHERE ${id} = ?`).run(
+ "2099-01-01T00:00:00.0001Z",
+ f.second.scanId,
+ );
+ }
+ } finally {
+ db.close();
+ }
+ expect((await latestFor(f, f.repository)).scanId).toBe(f.first.scanId);
+ },
+);
+
+test.each(["not-a-timestamp", `2099-01-01T00:00:00.${"0".repeat(100_000)}1Z`])(
+ "latest leaves unrelated history to its own workbench scope %#",
+ async (timestamp) => {
+ const f = await fixture();
+ const unrelated = join(f.root, "unrelated");
+ await mkdir(join(unrelated, "src"), { recursive: true });
+ await writeFile(join(unrelated, "src", "extract.py"), "# Other fixture\n");
+ const other = await f.scan("other-scan", unrelated);
+ const db = new Database(
+ join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
+ );
+ try {
+ db.query("UPDATE scans SET updated_at = ? WHERE id = ?").run(
+ timestamp,
+ other.scanId,
+ );
+ db.query("UPDATE scan_progress SET updated_at = ? WHERE scan_id = ?").run(
+ timestamp,
+ other.scanId,
+ );
+ } finally {
+ db.close();
+ }
+ expect((await latestFor(f, f.repository)).scanId).toBe(f.second.scanId);
+ },
+);
+
+test("latest ties equivalent instants by start time, then binary scan ID", async () => {
+ const f = await fixture();
+ const cases = [
+ [".100", ".1"],
+ [".000", ""],
+ [`.${"0".repeat(100_000)}`, ""],
+ ] as const;
+ for (const [index, [firstFraction, secondFraction]] of cases.entries()) {
+ const [first, second] = [f.first, f.second].sort((a, b) =>
+ a.scanId < b.scanId ? -1 : 1,
+ );
+ const db = new Database(
+ join(f.environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
+ );
+ try {
+ for (const [scan, timestamp] of [
+ [first!, `2099-01-01T02:00:00${firstFraction}+02:00`],
+ [second!, `2099-01-01T00:00:00${secondFraction}Z`],
+ ] as const) {
+ db.query(
+ "UPDATE scans SET started_at = '2098-01-01T00:00:00Z', updated_at = ? WHERE id = ?",
+ ).run(timestamp, scan.scanId);
+ db.query(
+ "UPDATE scan_progress SET updated_at = ? WHERE scan_id = ?",
+ ).run(timestamp, scan.scanId);
+ }
+ expect(
+ (await latestFor(f, f.repository)).scanId,
+ `equivalent case ${index}`,
+ ).toBe(first!.scanId);
+ db.query(
+ "UPDATE scans SET started_at = '2098-01-01T00:00:00.0001Z' WHERE id = ?",
+ ).run(second!.scanId);
+ expect(
+ (await latestFor(f, f.repository)).scanId,
+ `start-time case ${index}`,
+ ).toBe(second!.scanId);
+ } finally {
+ db.close();
+ }
+ }
+});
+
+for (const removedComponent of [false, true]) {
+ test.skipIf(process.platform === "win32")(
+ `latest ignores an unrelated registered Git target around configured Python (removed component: ${removedComponent})`,
+ async () => {
+ const f = await fixture(true);
+ const unrelated = join(f.root, "unrelated");
+ execFileSync("git", ["clone", "--quiet", f.repository, unrelated]);
+ const target = removedComponent
+ ? join(unrelated, "component")
+ : unrelated;
+ if (removedComponent) {
+ await mkdir(join(target, "src"), { recursive: true });
+ await writeFile(
+ join(target, "src", "extract.py"),
+ "# Synthetic component\n",
+ );
+ }
+ if (removedComponent) {
+ execFileSync("git", ["-C", unrelated, "add", "."]);
+ execFileSync("git", [
+ "-C",
+ unrelated,
+ "-c",
+ "user.name=Synthetic Fixture",
+ "-c",
+ "commit",
+ "-qm",
+ "Synthetic component",
+ ]);
+ }
+ await f.scan("unrelated-scan", target);
+ if (removedComponent) await rm(target, { recursive: true });
+ const interpreter = join(unrelated, "python");
+ await writeFile(
+ interpreter,
+ `#!/bin/sh\nexec ${JSON.stringify(f.python)} "$@"\n`,
+ { mode: 0o700 },
+ );
+ const workbench = await savedScanWorkbench("latest", {
+ environment: { ...f.environment, PYTHON: interpreter },
+ pluginRoot: PLUGIN_ROOT,
+ currentDirectory: f.repository,
+ });
+ expect(
+ (
+ await resolveCompletedScan("latest", {
+ currentDirectory: () => f.repository,
+ runWorkbench: workbench,
+ })
+ ).scanId,
+ ).toBe(f.second.scanId);
+ },
+ );
+}
+
+test.skipIf(process.platform === "win32")(
+ "latest protects the surviving checkout around a removed related component",
+ async () => {
+ const f = await fixture(true);
+ const component = join(f.repository, "component");
+ await mkdir(join(component, "src"), { recursive: true });
+ await writeFile(
+ join(component, "src", "extract.py"),
+ "# Synthetic component\n",
+ );
+ execFileSync("git", ["-C", f.repository, "add", "."]);
+ execFileSync("git", [
+ "-C",
+ f.repository,
+ "-c",
+ "user.name=Synthetic Fixture",
+ "-c",
+ "commit",
+ "-qm",
+ "Synthetic component",
+ ]);
+ const linked = join(f.root, "linked");
+ execFileSync("git", [
+ "-C",
+ f.repository,
+ "worktree",
+ "add",
+ "--quiet",
+ "--detach",
+ linked,
+ "HEAD",
+ ]);
+ await f.scan("removed-component", component);
+ await rm(component, { recursive: true });
+ const interpreter = join(f.repository, "python");
+ const marker = join(f.root, "python-probed");
+ await writeFile(
+ interpreter,
+ `#!/bin/sh\nprintf probed > ${JSON.stringify(marker)}\nexec ${JSON.stringify(f.python)} "$@"\n`,
+ { mode: 0o700 },
+ );
+ const currentDirectory = join(linked, "component");
+ const workbench = await savedScanWorkbench("latest", {
+ environment: { ...f.environment, PYTHON: interpreter },
+ pluginRoot: PLUGIN_ROOT,
+ currentDirectory,
+ });
+ await expect(
+ resolveCompletedScan("latest", {
+ currentDirectory: () => currentDirectory,
+ runWorkbench: workbench,
+ }),
+ ).rejects.toThrow("PYTHON interpreter is unavailable or unusable");
+ expect(existsSync(marker)).toBe(false);
+ },
+);
+
+test.skipIf(process.platform === "win32").each([undefined, "python3"])(
+ "latest excludes unrelated historical checkouts from Python PATH discovery (PYTHON: %p)",
+ async (namedPython) => {
+ const f = await fixture(true);
+ const unrelated = join(f.root, "unrelated-path-checkout");
+ execFileSync("git", ["clone", "--quiet", f.repository, unrelated]);
+ await f.scan("unrelated-path-scan", unrelated);
+ const marker = join(f.root, "unrelated-python-probed");
+ await writeFile(
+ join(unrelated, "python3"),
+ `#!/bin/sh\nprintf probed > ${JSON.stringify(marker)}\nexec ${JSON.stringify(f.python)} "$@"\n`,
+ { mode: 0o700 },
+ );
+ const workbench = await savedScanWorkbench("latest", {
+ environment: {
+ ...f.environment,
+ PATH: `${unrelated}${delimiter}${f.environment.PATH ?? ""}`,
+ XDG_CACHE_HOME: join(f.root, "empty-runtime-cache"),
+ ...(namedPython === undefined ? {} : { PYTHON: namedPython }),
+ },
+ pluginRoot: PLUGIN_ROOT,
+ currentDirectory: f.repository,
+ });
+ expect(
+ (
+ await resolveCompletedScan("latest", {
+ currentDirectory: () => f.repository,
+ runWorkbench: workbench,
+ })
+ ).scanId,
+ ).toBe(f.second.scanId);
+ expect(existsSync(marker)).toBe(false);
+ },
+);
diff --git a/sdk/typescript/tests-ts/scan-comparison.test.ts b/sdk/typescript/tests-ts/scan-comparison.test.ts
index 3543581a5..72a9736a5 100644
--- a/sdk/typescript/tests-ts/scan-comparison.test.ts
+++ b/sdk/typescript/tests-ts/scan-comparison.test.ts
@@ -2219,6 +2219,13 @@ describe("semantic scan comparison", () => {
after: [after],
knownFindingGroups: [["dismissed", "historical-alias"]],
});
+ expect(commands[0]?.args).toEqual([
+ "list-unmatched-scan-pairs",
+ "--repository",
+ "/repository",
+ "--after-scan-id",
+ "current",
+ ]);
expect(commands.map(({ args: [command] }) => command)).toEqual([
"list-unmatched-scan-pairs",
]);
@@ -2304,6 +2311,61 @@ describe("semantic scan comparison", () => {
}
});
+ test("matches a later-started alias when the earlier scan completes last", async () => {
+ const later = {
+ findingId: "later-finding",
+ occurrenceId: "later-occurrence",
+ };
+ const earlier = {
+ findingId: "earlier-finding",
+ occurrenceId: "earlier-occurrence",
+ };
+ const commands: (readonly string[])[] = [];
+ await matchCompletedScan({
+ scanId: "earlier-scan",
+ repository: "/repository",
+ previousFindings: [later],
+ falsePositives: [],
+ findings: [earlier],
+ async workbench(args) {
+ commands.push(args);
+ return args[0] === "list-unmatched-scan-pairs"
+ ? {
+ batches: [
+ {
+ afterScanId: "earlier-scan",
+ afterFindings: [earlier],
+ beforeScans: [{ scanId: "later-scan", findings: [later] }],
+ },
+ ],
+ }
+ : {};
+ },
+ async matchFindings(input) {
+ expect(input).toEqual({ before: [later], after: [earlier] });
+ return {
+ matches: [
+ {
+ beforeOccurrenceIds: ["later-occurrence"],
+ afterOccurrenceIds: ["earlier-occurrence"],
+ confidence: "high",
+ reason: "Same synthetic finding.",
+ },
+ ],
+ uncertain: [],
+ };
+ },
+ });
+ expect(commands[0]?.slice(-2)).toEqual(["--after-scan-id", "earlier-scan"]);
+ expect(commands[1]?.slice(0, 5)).toEqual([
+ "save-scan-comparison",
+ "--before-scan-id",
+ "later-scan",
+ "--after-scan-id",
+ "earlier-scan",
+ ]);
+ });
+
test.each([
["no history", false, false, false, 0, false],
["a stable identity", true, false, true, 2, false],
@@ -3221,3 +3283,68 @@ describe("semantic scan comparison", () => {
},
);
});
+
+test.each(["selected predecessor", "unrelated predecessor"] as const)(
+ "history archive compatibility matches %s when a later scan represents its group",
+ async (selection) => {
+ const before = {
+ findingId: "saved-finding",
+ occurrenceId: "saved-occurrence",
+ };
+ const representative = {
+ findingId: "later-finding",
+ occurrenceId: "later-occurrence",
+ scanId: "later-scan",
+ knownScanIds: ["saved-scan", "later-scan"],
+ };
+ const after = {
+ findingId: "current-finding",
+ occurrenceId: "current-occurrence",
+ };
+ const beforeScanId =
+ selection === "selected predecessor" ? "saved-scan" : "unrelated-scan";
+ const comparisons: (readonly string[])[] = [];
+ const matchFindings = mock<typeof matchScanFindings>(async () => ({
+ matches: [],
+ uncertain: [],
+ }));
+ await matchCompletedScan({
+ scanId: "current-scan",
+ repository: "/repository",
+ previousFindings: [representative],
+ falsePositives: [],
+ findings: [after],
+ matchFindings,
+ async workbench(args) {
+ if (args[0] === "list-unmatched-scan-pairs")
+ return {
+ batches: [
+ {
+ afterScanId: "current-scan",
+ afterFindings: [after],
+ beforeScans: [{ scanId: beforeScanId, findings: [before] }],
+ },
+ ],
+ };
+ comparisons.push(args);
+ return {};
+ },
+ });
+ expect(matchFindings).toHaveBeenCalledTimes(
+ selection === "selected predecessor" ? 1 : 0,
+ );
+ if (selection === "selected predecessor") {
+ expect(matchFindings.mock.lastCall?.[0]).toEqual({
+ before: [before],
+ after: [after],
+ });
+ expect(comparisons[0]?.slice(0, 5)).toEqual([
+ "save-scan-comparison",
+ "--before-scan-id",
+ "saved-scan",
+ "--after-scan-id",
+ "current-scan",
+ ]);
+ }
+ },
+);
diff --git a/sdk/typescript/tests-ts/support/api-client.ts b/sdk/typescript/tests-ts/support/api-client.ts
index a08fa252d..b4e9ed90e 100644
--- a/sdk/typescript/tests-ts/support/api-client.ts
+++ b/sdk/typescript/tests-ts/support/api-client.ts
@@ -147,6 +147,7 @@ export function mockWorkbench(
args: readonly string[],
input?: string,
): JsonObject {
+ if (args[0] === "list-scans") return { scans: [] };
if (args[0] === "register-cli-scan") {
return mockScanRegistration(args, input);
}
diff --git a/sdk/typescript/tests-ts/targets.test.ts b/sdk/typescript/tests-ts/targets.test.ts
index 47e5a3dee..e2f0d4d49 100644
--- a/sdk/typescript/tests-ts/targets.test.ts
+++ b/sdk/typescript/tests-ts/targets.test.ts
@@ -984,6 +984,21 @@ test("finds Git boundaries through directory aliases and file inputs", async ()
).toBe(repo);
});
+test.skipIf(process.platform === "win32")(
+ "protects the enclosing checkout when a saved component becomes a symlink loop",
+ async () => {
+ const repo = await repository();
+ const component = join(repo, "component");
+ await symlink("component", component);
+ expect(await gitProtectionRoots(component)).toContain(repo);
+ await expect(
+ gitMarkerRoot(component, undefined, "nearest"),
+ ).rejects.toMatchObject({
+ code: "ELOOP",
+ });
+ },
+);
+
test("ordinary storage folders with malformed config are not Git metadata", async () => {
const root = await temporaryDirectory();
await mkdir(join(root, "objects"));
diff --git a/sdk/typescript/tests-ts/workbench-canonical-paths.test.ts b/sdk/typescript/tests-ts/workbench-canonical-paths.test.ts
index e10bfff4a..95c4d6e25 100644
--- a/sdk/typescript/tests-ts/workbench-canonical-paths.test.ts
+++ b/sdk/typescript/tests-ts/workbench-canonical-paths.test.ts
@@ -50,6 +50,64 @@ const simulatedPathProbe = [
"print(json.dumps({'accepted': accepted, 'nativePathEquality': path_type(supplied) == path_type(resolved), 'resolvedPath': result}))",
].join("\n");
+const caseSensitiveWindowsHistoryProbe = [
+ "import json, sqlite3, sys",
+ "from pathlib import PureWindowsPath",
+ "from types import SimpleNamespace",
+ "sys.path.insert(0, sys.argv[1])",
+ "import workbench_scan_history as history",
+ "import workbench_target_state as target_state",
+ "class WindowsPath(PureWindowsPath):",
+ " def expanduser(self):",
+ " return self",
+ " def absolute(self):",
+ " return self",
+ " def resolve(self, strict=False):",
+ " return self",
+ "history.Path = WindowsPath",
+ "connection = sqlite3.connect(':memory:')",
+ "connection.row_factory = sqlite3.Row",
+ "connection.executescript('''",
+ "CREATE TABLE security_targets (id TEXT, current_path TEXT, repository_identity TEXT);",
+ "CREATE TABLE scans (id TEXT, target_id TEXT, target_path TEXT, repository_generation TEXT, status TEXT, started_at TEXT);",
+ "CREATE TABLE scan_comparisons (before_scan_id TEXT, after_scan_id TEXT);",
+ "''')",
+ "def target(identifier, path, identity):",
+ " return connection.execute('SELECT ? AS target_id, ? AS target_path, ? AS repository_identity, ? AS repository_generation', (identifier, str(WindowsPath(path)), identity, identity)).fetchone()",
+ "upper_root = target('upper-root', 'D:/Repository', 'root-upper')",
+ "lower_root = target('lower-root', 'D:/repository', 'root-lower')",
+ "upper_scope = target('upper-scope', 'D:/Repository/Service', 'scope-upper')",
+ "lower_scope = target('lower-scope', 'D:/Repository/service', 'scope-lower')",
+ "linked_scope = target('linked-scope', 'E:/Linked/Service', 'scope-upper')",
+ "clone_scope = target('clone-scope', 'D:/Clone/Service', 'scope-clone')",
+ "same_path = target('same-path', 'D:/Repository/Service', None)",
+ "targets = [upper_root, lower_root, upper_scope, lower_scope, linked_scope, clone_scope]",
+ "identities = {entry['target_path']: entry['repository_identity'] for entry in targets}",
+ "def inspect(database, target_id, path, stored, **kwargs):",
+ " identity = identities.get(path)",
+ " repository = target_state.GitRepositoryIdentity(identity, WindowsPath(path).name, 'synthetic', 1, 2, 3, 3) if identity else None",
+ " return target_state.RepositoryTargetState(target_id, path, stored, resolved_path=str(WindowsPath(path)), repository=repository, ownership_matches=True, strict_owner_matches=True)",
+ "target_state._inspect_repository_target = inspect",
+ "target_state.repository_origin = lambda path: ('example.test', 'synthetic/repository')",
+ "for entry in targets:",
+ " connection.execute('INSERT INTO security_targets VALUES (?, ?, ?)', (entry['target_id'], entry['target_path'], entry['repository_identity']))",
+ "cache = history.RepositoryIdentityCache(connection)",
+ "checks = {",
+ " 'nativeWindowsScopeEquality': WindowsPath(upper_scope['target_path']) == WindowsPath(lower_scope['target_path']),",
+ " 'nativeWindowsRootEquality': WindowsPath(upper_root['target_path']) == WindowsPath(lower_root['target_path']),",
+ " 'caseSensitiveScopesMatch': cache.scope(lower_scope['target_id']).generation == cache.scope(upper_scope['target_id']).generation,",
+ " 'caseSensitiveRootsMatch': cache.scope(lower_root['target_id']).generation == cache.scope(upper_root['target_id']).generation,",
+ " 'linkedWorktreeMatches': cache.scope(linked_scope['target_id']).generation == cache.scope(upper_scope['target_id']).generation,",
+ " 'sameOriginCloneMatches': cache.scope(clone_scope['target_id']).generation == cache.scope(upper_scope['target_id']).generation,",
+ " 'exactResolvedPathMatches': history._same_repository(connection, same_path, upper_scope, identities=cache),",
+ "}",
+ "connection.execute('INSERT INTO scans VALUES (?, ?, ?, ?, ?, ?)', ('lower-scan', lower_scope['target_id'], lower_scope['target_path'], lower_scope['repository_generation'], 'complete', '2026-08-15T00:00:00Z'))",
+ "history._same_repository = lambda *args, **kwargs: False",
+ "selected = history.list_unmatched_scan_pairs(connection, SimpleNamespace(repository=upper_scope['target_path'], force=False), backfill_finding_details=lambda *args: None, read_coverage=lambda scan: {})",
+ "checks['caseSensitiveUnmatchedScanCount'] = selected['scanCount']",
+ "print(json.dumps(checks))",
+].join("\n");
+
const realFilesystemProbe = [
"import json, sys",
"from pathlib import Path",
@@ -270,6 +328,19 @@ describe("bundled workbench canonical paths", () => {
});
});
+ test("keeps case-sensitive Windows repository roots and scopes distinct", () => {
+ expect(runPythonProbe(caseSensitiveWindowsHistoryProbe)).toEqual({
+ nativeWindowsScopeEquality: true,
+ nativeWindowsRootEquality: true,
+ caseSensitiveScopesMatch: false,
+ caseSensitiveRootsMatch: false,
+ linkedWorktreeMatches: true,
+ sameOriginCloneMatches: false,
+ exactResolvedPathMatches: true,
+ caseSensitiveUnmatchedScanCount: 0,
+ });
+ });
+
testCaseSensitive(
"rejects case-differing symlinks at every workbench and finalizer boundary",
async () => {
diff --git a/sdk/typescript/tests-ts/workbench-identity-cache.test.ts b/sdk/typescript/tests-ts/workbench-identity-cache.test.ts
new file mode 100644
index 000000000..f37e2b9aa
--- /dev/null
+++ b/sdk/typescript/tests-ts/workbench-identity-cache.test.ts
@@ -0,0 +1,2190 @@
+import { join } from "node:path";
+import { expect, test } from "bun:test";
+import { PLUGIN_ROOT } from "./plugin-root.js";
+import { runCommand } from "./support/shell.js";
+import { useTestPython } from "./support/python.js";
+
+const pythonExecutable = useTestPython();
+
+const probe = String.raw`
+import argparse
+import ctypes
+import errno
+import hashlib
+import io
+import json
+import os
+import sqlite3
+import stat
+import sys
+from collections import Counter
+from contextlib import ExitStack, redirect_stderr
+from pathlib import Path
+from tempfile import TemporaryDirectory
+from types import SimpleNamespace
+from unittest.mock import Mock, patch
+
+sys.path.insert(0, sys.argv[1])
+import workbench_scan_history as history
+import workbench_native_indexes as indexes
+import workbench_target_state as state
+import workbench_target as target_module
+import workbench_feedback as feedback_module
+import workbench_db as workbench
+import workbench_scan_start as scan_start
+import deep_scan_workbench as deep_workbench
+from workbench_feedback import get_scan_feedback
+from workbench_schema import MIGRATIONS, apply_migrations
+
+deep_workbench.configure(SimpleNamespace(
+ **vars(workbench),
+ preserve_stopped_results=workbench.preserve_stopped_results_after_transition,
+))
+
+
+def deep_fixture_dependency(name):
+ if name in {
+ "require_target", "require_remediation_target", "require_scope", "require_scan",
+ "require_workspace", "require_scannable_target", "safe_segment",
+ "register_security_target", "scan_target_identity", "now",
+ "state_dir",
+ }:
+ return deep_workbench.dependencies()
+ return deep_workbench
+
+scenario = sys.argv[2]
+timestamp = "2026-08-01T00:00:00Z"
+connection = sqlite3.connect(":memory:")
+connection.row_factory = sqlite3.Row
+connection.execute("PRAGMA foreign_keys = ON")
+legacy_identity_migration = (
+ 30, "persist repository identities",
+ "ALTER TABLE security_targets ADD COLUMN repository_identity TEXT;\n"
+ "CREATE INDEX security_targets_by_repository_identity ON security_targets(repository_identity);\n",
+)
+recorded_identity31 = scenario == "migration-recorded31"
+initial = (
+ (*tuple(item for item in MIGRATIONS if item[0] <= 28), legacy_identity_migration)
+ if scenario in ("migration", "v30-current", "completion-order") else
+ tuple(item for item in MIGRATIONS if item[0] <= 30)
+ if scenario == "null-history" or recorded_identity31 else MIGRATIONS
+)
+apply_migrations(connection, initial, lambda: timestamp, lambda database: None)
+if recorded_identity31:
+ connection.executescript(legacy_identity_migration[2])
+ connection.execute(
+ "INSERT INTO schema_migrations VALUES (?, ?, ?)",
+ (31, legacy_identity_migration[1], timestamp),
+ )
+paths = {}
+details = {}
+metadata = {}
+missing = set()
+resolution_errors = {}
+probes = Counter()
+origins = Counter()
+real_identity_details = state._repository_identity_details
+description = SimpleNamespace(
+ st_mode=stat.S_IFREG, st_dev=44, st_ino=55, st_ctime_ns=66
+)
+
+
+def add_target(name, stored, live=None, relative=".", birth=1_000_000_000):
+ path = str(root / name)
+ paths[name] = path
+ metadata[path] = SimpleNamespace(st_mode=stat.S_IFDIR, st_dev=7, st_ino=100 + len(paths))
+ details[path] = state.GitRepositoryIdentity(
+ live or stored or "repository-current", relative, str(root / "common"), 11, 22, birth, birth
+ )
+ if state.supports_repository_identity(connection):
+ connection.execute(
+ "INSERT INTO security_targets "
+ "(id, current_path, display_name, created_at, updated_at, repository_identity) "
+ "VALUES (?, ?, ?, ?, ?, ?)",
+ (name, path, name, timestamp, timestamp, stored),
+ )
+ else:
+ connection.execute(
+ "INSERT INTO security_targets "
+ "(id, current_path, display_name, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
+ (name, path, name, timestamp, timestamp),
+ )
+ return details[path]
+
+
+current_generation = object()
+
+
+def add_scan(scan_id, target, owner="current", started=timestamp, created=timestamp,
+ generation=current_generation, status="complete"):
+ path = paths[target]
+ device, inode = metadata[path].st_dev, metadata[path].st_ino
+ if owner == "missing":
+ device, inode = None, None
+ elif owner == "mismatch":
+ inode += 1
+ connection.execute(
+ "INSERT INTO workspaces (id, target_path, target_id, created_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?)",
+ ("workspace-" + scan_id, path, target, timestamp, timestamp),
+ )
+ supports_generation = any(
+ row["name"] == "repository_generation"
+ for row in connection.execute("PRAGMA table_info(scans)")
+ )
+ if generation is current_generation:
+ generation = connection.execute(
+ "SELECT repository_identity FROM security_targets WHERE id = ?", (target,)
+ ).fetchone()[0] if state.supports_repository_identity(connection) else None
+ include_generation = supports_generation and generation is not None
+ generation_column = ", repository_generation" if include_generation else ""
+ generation_placeholder = ", ?" if include_generation else ""
+ connection.execute(
+ "INSERT INTO scans (id, workspace_id, target_path, target_id, target_device, "
+ "target_inode, target_revision, scope, mode, scan_dir, status, phase, "
+ f"started_at, completed_at, created_at, updated_at{generation_column}) "
+ f"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?{generation_placeholder})",
+ (scan_id, "workspace-" + scan_id, path, target, device, inode, "synthetic",
+ ".", "standard", str(root / "scans" / scan_id), status, "reporting",
+ started, created if status == "complete" else None, created, created,
+ *((generation,) if include_generation else ())),
+ )
+ connection.execute(
+ "INSERT INTO scan_progress (scan_id, updated_at) VALUES (?, ?)",
+ (scan_id, timestamp),
+ )
+
+
+def add_finding(scan_id, finding_id, closed=False):
+ occurrence = scan_id + ":" + finding_id
+ connection.execute(
+ "INSERT OR IGNORE INTO findings "
+ "(id, fingerprint, rule_id, identity_anchor, created_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?, ?)",
+ (finding_id, "fingerprint-" + finding_id, "synthetic-rule", finding_id, timestamp, timestamp),
+ )
+ connection.execute(
+ "INSERT INTO finding_occurrences "
+ "(id, finding_id, scan_id, title, summary, severity, confidence, remediation, created_at) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
+ (occurrence, finding_id, scan_id, finding_id, "Synthetic summary", "high",
+ "high", "Synthetic remediation", timestamp),
+ )
+ connection.execute(
+ "INSERT INTO finding_locations "
+ "(occurrence_id, relative_path, start_line, end_line, role, sort_order) "
+ "VALUES (?, ?, ?, ?, ?, ?)",
+ (occurrence, "src/example.py", 1, 1, "root_control", 0),
+ )
+ if closed:
+ connection.execute(
+ "INSERT INTO finding_triage VALUES (?, ?, ?, ?, ?)",
+ (occurrence, "closed", "false_positive", "Synthetic review", timestamp),
+ )
+ return occurrence
+
+
+original_resolve, original_stat, original_lstat = Path.resolve, Path.stat, Path.lstat
+
+
+def resolve_path(path, *args, **kwargs):
+ value = str(path)
+ if value in resolution_errors:
+ raise resolution_errors[value]("Synthetic path resolution failure")
+ return path if value in metadata else original_resolve(path, *args, **kwargs)
+
+
+def stat_path(path, *args, **kwargs):
+ value = str(path)
+ if value in missing:
+ raise FileNotFoundError(errno.ENOENT, "Synthetic missing path", value)
+ return metadata[value] if value in metadata else original_stat(path, *args, **kwargs)
+
+
+def lstat_path(path, *args, **kwargs):
+ if path.name == "description" and path.parent == root / "common":
+ return description
+ return original_lstat(path, *args, **kwargs)
+
+
+def identity_details(path):
+ value = str(path)
+ probes[value] += 1
+ return details.get(value)
+
+
+def origin(path):
+ value = str(path)
+ origins[value] += 1
+ identity = details.get(value)
+ return ("example.test", identity.value) if identity is not None else None
+
+
+def listed(target):
+ args = argparse.Namespace(
+ repository=paths[target], scan_root=None, target_id=None, mode=None,
+ status=None, query=None, limit=None, offset=0,
+ )
+ return sorted(row["scanId"] for row in history.list_scans(connection, args)["scans"])
+
+
+def findings(target):
+ return indexes.list_global_findings(
+ connection,
+ argparse.Namespace(target_id=target, limit=50, offset=0, query=None, severity=None, status=None),
+ )["findings"]
+
+
+def legacy_hash(identity, with_description=False):
+ directory = os.path.normcase(identity.common_directory)
+ relative = os.path.normcase(os.fspath(Path(identity.relative_path))).replace(os.sep, "/")
+ material = f"git-common-dir\0{directory}\0{identity.device}\0{identity.inode}\0"
+ if with_description:
+ material += f"git-description\0{description.st_dev}\0{description.st_ino}\0{description.st_ctime_ns}\0"
+ return "repository_sha256_" + hashlib.sha256((material + relative).encode()).hexdigest()
+
+
+with ExitStack() as stack:
+ # Windows scan output validates every ancestor. Hosted checkouts can live on an
+ # untrusted drive, so keep the fixture in a private directory under the profile.
+ root = Path(stack.enter_context(TemporaryDirectory(
+ prefix="workbench-identity-", dir=Path.home() if os.name == "nt" else None,
+ )))
+ stack.enter_context(patch.object(Path, "resolve", resolve_path))
+ stack.enter_context(patch.object(Path, "stat", stat_path))
+ stack.enter_context(patch.object(Path, "lstat", lstat_path))
+ stack.enter_context(patch.object(state, "_repository_identity_details", identity_details))
+ stack.enter_context(patch.object(state, "repository_origin", origin))
+ if scenario == "generation-metadata":
+ main = root / "main\ncheckout"
+ linked = root / "linked\ncheckout"
+ common = main / ".git"
+ admin = common / "worktrees" / "linked"
+ objects = common / "objects"
+ active = {"root": main, "gitdir": common, "nul": False, "config": None}
+ config_command = ("config", "--null", "--show-scope", "--no-includes", "--get", "core.worktree")
+ commands = []
+ directory = lambda inode, birth: SimpleNamespace(
+ st_mode=stat.S_IFDIR, st_dev=7, st_ino=inode, st_birthtime_ns=birth
+ )
+ records = {
+ str(main): directory(18, 100), str(linked): directory(19, 100),
+ str(common): directory(20, 100), str(objects): directory(21, 200),
+ str(admin): directory(22, 300), str(admin.parent): directory(24, 300),
+ str(linked / ".git"): SimpleNamespace(st_mode=stat.S_IFREG, st_dev=7, st_ino=25),
+ }
+ files = {
+ str(linked / ".git"): b"gitdir: " + os.fsencode(admin) + b"\n",
+ str(admin / "gitdir"): os.fsencode(linked / ".git") + b"\n",
+ }
+ def git_bytes(target, *arguments):
+ commands.append(arguments)
+ if arguments == ("worktree", "list", "--porcelain", "-z"):
+ return b"worktree " + os.fsencode(active["root"]) + b"\0\0" if active["nul"] else None
+ if arguments == config_command:
+ return active["config"]
+ if arguments == ("rev-parse", "--show-prefix"):
+ return b"\n"
+ paths_by_command = {
+ ("rev-parse", "--show-toplevel"): active["root"],
+ ("rev-parse", "--path-format=absolute", "--git-common-dir"): common,
+ ("rev-parse", "--absolute-git-dir"): active["gitdir"],
+ ("rev-parse", "--path-format=absolute", "--git-path", "objects"): objects,
+ }
+ value = paths_by_command.get(arguments)
+ return os.fsencode(value) + b"\n" if value is not None else None
+ def git_command(target, *arguments, **kwargs):
+ output = git_bytes(target, *arguments)
+ return SimpleNamespace(returncode=0 if output is not None else 1, stdout=output or b"")
+ primary = root / "primary-checkout"
+ def file_primary(configuration, selected_root=primary, forward=common):
+ active.update(root=selected_root, gitdir=common, nul=False, config=configuration)
+ records[str(selected_root)] = directory(200 + len(records), 300)
+ records[str(selected_root / ".git")] = SimpleNamespace(st_mode=stat.S_IFREG, st_dev=7, st_ino=100 + len(records))
+ files[str(selected_root / ".git")] = b"gitdir: " + os.fsencode(forward) + b"\n"
+ return real_identity_details(selected_root) is not None
+ def configured(value, scope=b"local"):
+ return scope + b"\0" + os.fsencode(value) + b"\0"
+ def recorded_stat(path, *args, **kwargs):
+ try:
+ return records[str(path)]
+ except KeyError:
+ raise FileNotFoundError(errno.ENOENT, "Synthetic missing metadata", str(path)) from None
+ # Keep directory checks inside the metadata fixture on every supported Python.
+ with patch.object(target_module, "git_command", git_command), \
+ patch.object(os.path, "realpath", side_effect=lambda path, **kwargs: os.path.normpath(path)), \
+ patch.object(Path, "stat", recorded_stat), \
+ patch.object(Path, "lstat", recorded_stat), \
+ patch.object(Path, "is_dir", lambda path, *a, **k: stat.S_ISDIR(records[str(path)].st_mode)), \
+ patch.object(Path, "read_bytes", lambda path: files[str(path)]), \
+ patch.object(state.os, "statvfs", side_effect=lambda path: SimpleNamespace(f_fsid=records[str(path)].st_dev), create=True), \
+ patch.object(state, "_repository_birth_time_ns", side_effect=lambda path, value: value.st_birthtime_ns):
+ first = real_identity_details(main)
+ same = real_identity_details(main)
+ active.update(root=linked, gitdir=admin)
+ old_linked = real_identity_details(linked)
+ files[str(admin / "gitdir")] = os.fsencode(main / ".git") + b"\n"
+ wrong_backlink = real_identity_details(linked)
+ files[str(admin / "gitdir")] = os.fsencode(linked / ".git") + b"\n"
+ active["nul"] = True
+ nul_linked = real_identity_details(linked)
+ records[str(objects)] = directory(23, 400)
+ changed = real_identity_details(linked)
+ literal_tilde = common / "~literal" / "checkout"
+ trailing_lf = root / "primary-checkout\n"
+ trailing_space = root / "primary-checkout "
+ primary_results = {
+ "localAbsolute": file_primary(configured(primary)),
+ "worktreeRelative": file_primary(configured(os.path.relpath(primary, common), b"worktree")),
+ "literalLeadingTilde": file_primary(configured(Path("~literal") / "checkout"), literal_tilde),
+ "trailingLineFeed": file_primary(configured(trailing_lf), trailing_lf),
+ "trailingWhitespace": file_primary(configured(trailing_space), trailing_space),
+ "missing": file_primary(None),
+ "wrongScopes": all(not file_primary(configured(primary, scope)) for scope in (
+ b"global", b"system", b"command", b"unknown",
+ )),
+ "malformed": all(not file_primary(value) for value in (
+ b"local\0", b"local\0\0", configured(primary)[:-1],
+ configured(primary) + b"extra\0",
+ )),
+ "wrongRoot": file_primary(configured(main)),
+ "foreignForward": file_primary(configured(primary), forward=root / "other-common"),
+ }
+ print(json.dumps({
+ "oldMain": first is not None,
+ "oldLinkedMatches": old_linked.value == first.value,
+ "nulMatches": nul_linked.value == first.value,
+ "wrongBacklink": wrong_backlink,
+ "unchanged": same.value == first.value,
+ "objectInstanceChangesGeneration": changed.value != first.value,
+ "legacyMaterialUnchanged": changed.legacy_value == first.legacy_value,
+ "domainSeparated": first.value != first.legacy_value,
+ "actualObjectLookup": ("rev-parse", "--path-format=absolute", "--git-path", "objects") in commands,
+ "oldPrimary": primary_results,
+ "rawConfigLookup": any(command == config_command for command in commands)
+ and all(command == config_command for command in commands if command[0] == "config"),
+ }))
+ elif scenario == "scan-generation":
+ for name, value in (("requested", "generation-current"), ("alias", "generation-current"),
+ ("clone", "generation-clone")):
+ add_target(name, value)
+ add_scan("requested-new", "requested")
+ add_scan("alias-proved", "alias")
+ add_scan("alias-old-client", "alias", generation=None)
+ add_scan("requested-legacy", "requested", generation=None)
+ add_scan("clone-scan", "clone")
+ proved = add_finding("alias-proved", "proved-review", closed=True)
+ ignored = add_finding("alias-old-client", "unproved-review", closed=True)
+ current = add_finding("requested-new", "current-open")
+ add_finding("requested-legacy", "local-review", closed=True)
+ connection.execute("INSERT INTO scan_comparisons VALUES (?, ?, ?, ?, ?)",
+ ("alias-old-client", "requested-new", "{}", timestamp, timestamp))
+ connection.execute("INSERT INTO scan_comparison_matches VALUES (?, ?, ?, ?, ?)",
+ ("alias-old-client", "requested-new", ignored, current, "Synthetic match"))
+ cache = state.RepositoryIdentityCache(connection)
+ scope = cache.scope("requested")
+ clause, values = scope.sql()
+ selected = sorted(row[0] for row in connection.execute(f"SELECT id FROM scans WHERE {clause}", values))
+ feedback = get_scan_feedback(connection, connection.execute("SELECT * FROM scans WHERE id = 'requested-new'").fetchone())
+ indexed = findings("requested")
+ with patch.object(state, "repository_origin", return_value=("example.test", "same-repository")):
+ rows = {row["id"]: row for row in connection.execute("SELECT * FROM scans")}
+ explicit_clones = history._same_repository(connection, rows["requested-new"], rows["clone-scan"])
+ explicit_legacy = history._same_repository(connection, rows["alias-old-client"], rows["clone-scan"])
+ contradictory = dict(rows["clone-scan"], repository_generation="different-snapshot")
+ own_contradiction = history._same_repository(connection, rows["requested-new"], contradictory)
+ legacy = add_target("weak", None, "generation-strong")
+ connection.execute("UPDATE security_targets SET repository_identity = ? WHERE id = 'weak'", (legacy.legacy_value,))
+ add_scan("weak-history", "weak", generation=None)
+ registration = state.register_security_target(connection, paths["weak"])
+ add_finding("requested-new", "shared-decision", closed=True)
+ add_finding("alias-proved", "shared-decision")
+ missing.add(paths["alias"])
+ absent_findings = sum(row["status"] == "open" for row in findings("alias"))
+ absent_repository = next(row for row in indexes.list_repositories(connection)["repositories"] if row["targetId"] == "alias")
+ print(json.dumps({
+ "selected": selected,
+ "predicate": {"binds": len(values), "ors": clause.count(" OR ")},
+ "feedback": sorted(row["findingId"] for row in feedback["falsePositives"]),
+ "findings": sorted((row["findingId"], row["occurrenceCount"], row["status"]) for row in indexed),
+ "oldClientGeneration": rows["alias-old-client"]["repository_generation"],
+ "explicitClones": explicit_clones, "explicitLegacy": explicit_legacy,
+ "ownContradiction": own_contradiction,
+ "weakRegistration": registration.repository_generation,
+ "weakHistory": connection.execute("SELECT repository_generation FROM scans WHERE id = 'weak-history'").fetchone()[0],
+ "weakBindingPreserved": connection.execute("SELECT repository_identity FROM security_targets WHERE id = 'weak'").fetchone()[0] == legacy.legacy_value,
+ "absentExactCounts": [absent_findings, absent_repository["openFindingsCount"]],
+ }))
+ elif scenario == "repository-counts":
+ for name, stored, live in (
+ ("first", "shared-generation", None),
+ ("second", "shared-generation", None),
+ ("absent", "shared-generation", None),
+ ("independent", "other-generation", None),
+ ("refused", "previous-generation", "current-generation"),
+ ):
+ add_target(name, stored, live)
+ add_scan(name + "-scan", name)
+ add_scan("first-legacy", "first", generation=None)
+ add_scan("second-legacy", "second", generation=None)
+ add_finding("first-scan", "shared-open")
+ add_finding("second-scan", "shared-open")
+ add_finding("first-legacy", "first-local")
+ add_finding("second-legacy", "second-local")
+ reviewed = add_finding("first-scan", "reviewed-original", closed=True)
+ renamed = add_finding("absent-scan", "reviewed-renamed")
+ add_finding("independent-scan", "independent-open")
+ add_finding("refused-scan", "refused-open")
+ connection.execute("INSERT INTO scan_comparisons VALUES (?, ?, ?, ?, ?)",
+ ("first-scan", "absent-scan", "{}", timestamp, timestamp))
+ connection.execute("INSERT INTO scan_comparison_matches VALUES (?, ?, ?, ?, ?)",
+ ("first-scan", "absent-scan", reviewed, renamed, "Synthetic match"))
+ missing.add(paths["absent"])
+ real_index = indexes._indexed_findings
+ real_inspect = state._inspect_repository_target
+ def repository_counts(**options):
+ args = argparse.Namespace(query=None, target_id=None, status=None, limit=None, offset=0)
+ vars(args).update(options)
+ calls, inspections = [], []
+ def indexed(database, **kwargs):
+ scope = kwargs.get("scan_scope")
+ calls.append(None if scope is None else scope.target_id)
+ return real_index(database, **kwargs)
+ def inspected(database, target_id, *args, **kwargs):
+ inspections.append(target_id)
+ return real_inspect(database, target_id, *args, **kwargs)
+ with patch.object(indexes, "_indexed_findings", side_effect=indexed), \
+ patch.object(state, "_inspect_repository_target", side_effect=inspected):
+ result = indexes.list_repositories(connection, args)
+ return {
+ "counts": {row["targetId"]: row["openFindingsCount"] for row in result["repositories"]},
+ "ids": [row["targetId"] for row in result["repositories"]],
+ "calls": calls, "inspections": sorted(inspections),
+ "nextOffset": result.get("nextOffset"),
+ }
+ all_repositories = repository_counts()
+ direct_counts = {
+ name: sum(row["status"] == "open" for row in findings(name)) for name in paths
+ }
+ open_repositories = repository_counts(status="open_findings")
+ page = repository_counts(status="open_findings", limit=2, offset=1)
+ print(json.dumps({
+ "all": all_repositories,
+ "directCounts": direct_counts,
+ "absentOnly": repository_counts(target_id="absent"),
+ "queryOnly": repository_counts(query="INDEPENDENT"),
+ "refusedOnly": repository_counts(target_id="refused"),
+ "notScanned": repository_counts(status="not_scanned"),
+ "openIds": open_repositories["ids"],
+ "ordinaryPage": repository_counts(limit=2),
+ "offsetPage": repository_counts(limit=2, offset=1),
+ "pastEnd": repository_counts(limit=2, offset=99),
+ "emptyQuery": repository_counts(query="no-matching-repository", limit=2),
+ "openPage": page,
+ "openPastEnd": repository_counts(status="open_findings", limit=2, offset=99),
+ "pagePreserved": page["ids"] == open_repositories["ids"][1:3] and page["nextOffset"] == 3,
+ }))
+ elif scenario == "scan-writers":
+ add_target("writer", "generation-writer")
+ target = Path(paths["writer"])
+ connection.execute("INSERT INTO workspaces (id, target_id, target_path, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
+ ("current-workspace", "writer", str(target), timestamp, timestamp))
+ connection.commit()
+ checks = []
+ def register(database, path):
+ checks.append(database.in_transaction)
+ return state.register_security_target(database, path)
+ connection.execute("BEGIN IMMEDIATE")
+ registration = register(connection, str(target))
+ scan_start.insert_running_scan(
+ connection, scan_id="current-writer", workspace=connection.execute("SELECT * FROM workspaces WHERE id = 'current-workspace'").fetchone(),
+ target=target, scope=".", diff_target=None,
+ target_identity=("synthetic", None, 7, metadata[str(target)].st_ino),
+ repository_generation=registration.repository_generation,
+ source_scopes={"version": 1, "revision": "synthetic", "scopes": []},
+ target_root=root / "artifacts", target_summary=None, scope_file_count=0,
+ timestamp=timestamp, scan_dir=root / "artifacts" / "current",
+ )
+ connection.commit()
+ add_scan("old-writer", "writer", generation=None)
+ connection.commit()
+ with ExitStack() as mocks:
+ for name, value in {
+ "require_target": target, "require_remediation_target": target,
+ "require_scope": ".", "existing_deep_scan_for_target": None,
+ "terminal_deep_scan_for_target_snapshot": None,
+ "scan_target_identity": ("synthetic", "snapshot", 7, metadata[str(target)].st_ino),
+ "directory_snapshot_regular_file_count": 0,
+ "effective_deep_scan_config": {"settings": {}}, "now": timestamp,
+ "state_dir": root,
+ }.items():
+ mocks.enter_context(patch.object(deep_fixture_dependency(name), name, return_value=value))
+ mocks.enter_context(patch.object(deep_workbench.dependencies(), "require_scannable_target"))
+ mocks.enter_context(patch.object(deep_workbench.dependencies(), "safe_segment", side_effect=lambda value: value))
+ mocks.enter_context(patch.object(deep_workbench.dependencies(), "register_security_target", side_effect=register))
+ mocks.enter_context(patch.object(deep_workbench.dependencies(), "require_scan", side_effect=lambda database, scan_id: database.execute("SELECT * FROM scans WHERE id = ?", (scan_id,)).fetchone()))
+ mocks.enter_context(patch.object(deep_workbench, "ensure_deep_scan_run"))
+ mocks.enter_context(patch.object(deep_workbench, "deep_scan_result", side_effect=lambda database, scan_id, **kwargs: {"scanId": scan_id}))
+ deep = deep_workbench.begin_deep_scan_for_target(connection, argparse.Namespace(
+ target_path=str(target), scope=".", workflow_version="synthetic", scan_root=str(root / "artifacts"),
+ user_context=None, model=None, reasoning_effort=None,
+ join_only=False, project_config_path=None, service_tier=None,
+ ), "synthetic-thread")
+ print(json.dumps({
+ "generations": {row["id"]: row["repository_generation"] for row in connection.execute("SELECT id, repository_generation FROM scans")},
+ "deepId": deep["scanId"], "registeredInsideTransaction": checks,
+ "generationIndex": connection.execute("SELECT 1 FROM sqlite_master WHERE name = 'scans_by_repository_generation'").fetchone() is not None,
+ }))
+ elif scenario == "scan-reuse":
+ for name, stored, live, generation in (
+ ("current", "generation-current", None, "generation-current"),
+ ("legacy", None, "generation-legacy", None),
+ ("refused", "generation-before", "generation-after", "generation-before"),
+ ("contradictory", "generation-current", None, "generation-other"),
+ ("missing", "generation-missing", None, "generation-missing"),
+ ):
+ add_target(name, stored, live)
+ add_scan(name + "-scan", name, generation=generation, status="running")
+ missing.add(paths["missing"])
+ connection.commit()
+ scan = lambda name: connection.execute(
+ "SELECT * FROM scans WHERE id = ?", (name + "-scan",)
+ ).fetchone()
+ accepted = {}
+ for name in paths:
+ try:
+ state.require_scan_checkout_owner(connection, scan(name))
+ except SystemExit:
+ accepted[name] = False
+ else:
+ accepted[name] = True
+
+ def reuse(name, kind):
+ target = Path(paths[name])
+ scan_id = name + "-scan"
+ workspace_id = "workspace-" + scan_id
+ mode = "deep" if kind.startswith("deep-") else "standard"
+ headless = kind == "headless"
+ connection.execute(
+ "UPDATE workspaces SET thread_id = 'synthetic-thread', active_scan_id = ?, "
+ "default_scope = '.', default_mode = ?, submitted = 1 WHERE id = ?",
+ (scan_id, mode, workspace_id),
+ )
+ connection.execute(
+ "UPDATE scans SET mode = ?, handoff_status = 'delivered', "
+ "handoff_claim_token = ?, continuation_thread_id = ? WHERE id = ?",
+ (mode, "synthetic-token" if headless else None,
+ "synthetic-thread" if headless else None, scan_id),
+ )
+ connection.commit()
+ row = scan(name)
+ workspace = connection.execute(
+ "SELECT * FROM workspaces WHERE id = ?", (workspace_id,)
+ ).fetchone()
+ ensure_run = Mock()
+ with ExitStack() as mocks:
+ for module in (workbench, deep_workbench):
+ mocks.enter_context(patch.object(module, "require_uuid", side_effect=lambda value, label: value))
+ owner = deep_workbench.dependencies() if module is deep_workbench else module
+ mocks.enter_context(patch.object(owner, "require_remediation_target", return_value=target))
+ mocks.enter_context(patch.object(module, "directory_snapshot_regular_file_count", return_value=0))
+ mocks.enter_context(patch.object(workbench, "workspace_state", return_value={}))
+ mocks.enter_context(patch.object(workbench, "scan_context", return_value={}))
+ mocks.enter_context(patch.object(workbench, "inspect_setup_values", return_value={
+ "target": {"targetPath": str(target)}, "scope": ".", "diffTarget": None,
+ }))
+ mocks.enter_context(patch.object(workbench, "scan_target_identity", return_value=(
+ "synthetic", None, 7, metadata[str(target)].st_ino,
+ )))
+ mocks.enter_context(patch.object(workbench, "scan_target_root", return_value=root / "artifacts"))
+ for function, value in {
+ "require_target": target, "require_scope": ".",
+ "require_scan": row, "require_workspace": workspace,
+ "require_owned_scan": (row, workspace),
+ "scan_target_identity": ("synthetic", None, 7, metadata[str(target)].st_ino),
+ "effective_deep_scan_config": {"settings": {}},
+ "now": timestamp, "deep_scan_result": {},
+ }.items():
+ mocks.enter_context(patch.object(deep_fixture_dependency(function), function, return_value=value))
+ mocks.enter_context(patch.object(deep_workbench.dependencies(), "require_scannable_target"))
+ mocks.enter_context(patch.object(deep_workbench, "require_current_continuation"))
+ mocks.enter_context(patch.object(deep_workbench, "ensure_deep_scan_run", ensure_run))
+ existing = [row] if kind == "deep-direct" else [None, row] if kind == "deep-transaction" else [None, None]
+ mocks.enter_context(patch.object(deep_workbench, "existing_deep_scan_for_target", side_effect=existing))
+ mocks.enter_context(patch.object(deep_workbench, "terminal_deep_scan_for_target_snapshot", return_value=row))
+ args = argparse.Namespace(
+ workspace_id=workspace_id, target_path=str(target), scope=".", mode="standard",
+ thread_id="synthetic-thread", diff_target_kind=None, diff_base_revision=None,
+ diff_head_revision=None, diff_content_digest=None, user_context=None,
+ target_summary=None, scan_root=None, model=None, reasoning_effort=None,
+ claim_token=None, workflow_version="synthetic",
+ join_only=False, project_config_path=None, service_tier=None,
+ )
+ try:
+ if kind == "workspace":
+ workbench.start_scan(connection, args)
+ elif kind in ("prompt", "headless"):
+ workbench._start_prompt_driven_scan(connection, args, headless_standard=headless)
+ else:
+ deep_workbench.begin_deep_scan_for_target(connection, args, "synthetic-thread")
+ except SystemExit:
+ return {"accepted": False, "createdRun": ensure_run.call_count != 0}
+ return {"accepted": True, "createdRun": ensure_run.call_count != 0}
+
+ kinds = ("workspace", "prompt", "headless", "deep-direct", "deep-transaction", "deep-terminal")
+ print(json.dumps({
+ "owners": accepted,
+ "reuse": {name: {kind: reuse(name, kind) for kind in kinds}
+ for name in ("current", "refused")},
+ "legacyGeneration": scan("legacy")["repository_generation"],
+ }))
+ elif scenario == "birth-time":
+ path = "/synthetic repository/.git"
+ real_sizeof = ctypes.sizeof
+
+ def native_result(*, wrapper=True, machine="x86_64", mask=0x800,
+ seconds=42, nanoseconds=123, status=0,
+ pointer_size=8, long_size=8):
+ calls = []
+
+ def invoke(*arguments):
+ number = None
+ if not wrapper:
+ number, *arguments = arguments
+ directory, encoded, flags, requested_mask, output = arguments
+ assert (directory, encoded, flags, requested_mask) == (
+ -100, os.fsencode(path), 0, 0x800
+ )
+ value = ctypes.cast(output, ctypes.POINTER(state._LinuxStatx)).contents
+ assert not value.mask and not value.birth_time.seconds
+ value.mask = mask
+ value.birth_time.seconds = seconds
+ value.birth_time.nanoseconds = nanoseconds
+ calls.append(number)
+ return status
+
+ function = Mock(side_effect=invoke)
+ libc = SimpleNamespace(**{"statx" if wrapper else "syscall": function})
+
+ def sizeof(kind):
+ if kind is ctypes.c_void_p:
+ return pointer_size
+ if kind is ctypes.c_long:
+ return long_size
+ return real_sizeof(kind)
+
+ with patch.object(state.ctypes, "CDLL", return_value=libc), \
+ patch.object(state.ctypes, "sizeof", side_effect=sizeof), \
+ patch.object(state.platform, "machine", return_value=machine):
+ result = state._linux_repository_birth_time_ns(path)
+ expected_arguments = (
+ (ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_uint,
+ ctypes.POINTER(state._LinuxStatx)) if wrapper else
+ (ctypes.c_long, ctypes.c_long, ctypes.c_char_p, ctypes.c_long,
+ ctypes.c_ulong, ctypes.POINTER(state._LinuxStatx))
+ )
+ return {
+ "value": result, "calls": calls,
+ "typed": not calls or function.argtypes == expected_arguments
+ and function.restype is (ctypes.c_int if wrapper else ctypes.c_long),
+ }
+
+ cases = {
+ "wrapper": native_result(),
+ "x86_64": native_result(wrapper=False),
+ "aarch64": native_result(wrapper=False, machine="aarch64"),
+ "unknown-abi": native_result(wrapper=False, machine="unknown"),
+ "pointer32": native_result(wrapper=False, pointer_size=4),
+ "long32": native_result(wrapper=False, long_size=4),
+ "missing-mask": native_result(mask=0),
+ "invalid-nanoseconds": native_result(nanoseconds=1_000_000_000),
+ "zero": native_result(seconds=0, nanoseconds=0),
+ "negative": native_result(seconds=-1, nanoseconds=999_999_999),
+ "failed": native_result(status=-1),
+ }
+ with patch.object(state.ctypes, "CDLL", return_value=SimpleNamespace()), \
+ patch.object(state.platform, "machine", return_value="x86_64"):
+ cases["missing-symbols"] = state._linux_repository_birth_time_ns(path)
+ with patch.object(state.ctypes, "CDLL", side_effect=OSError("unavailable")):
+ cases["missing-libc"] = state._linux_repository_birth_time_ns(path)
+ with patch.object(state.ctypes, "sizeof", return_value=255), \
+ patch.object(state.ctypes, "CDLL") as library:
+ cases["invalid-layout"] = state._linux_repository_birth_time_ns(path)
+ assert not library.called
+ with patch.object(sys, "platform", "linux"), patch.object(os, "name", "posix"), \
+ patch.object(state, "_linux_repository_birth_time_ns", return_value=42_000_000_123), \
+ patch("subprocess.run") as command:
+ native = state._repository_birth_time_ns(path, SimpleNamespace(st_ctime_ns=41))
+ assert not command.called
+ with patch.object(sys, "platform", "linux"), patch.object(os, "name", "posix"), \
+ patch.object(state, "_linux_repository_birth_time_ns", return_value=None), \
+ patch("subprocess.run") as command:
+ unavailable = state._repository_birth_time_ns(path, SimpleNamespace(st_ctime_ns=41))
+ assert not command.called
+ print(json.dumps({
+ "cases": cases, "native": native, "unavailable": unavailable,
+ "layout": [real_sizeof(state._LinuxStatxTimestamp),
+ state._LinuxStatx.birth_time.offset, real_sizeof(state._LinuxStatx)],
+ }))
+ elif scenario == "cache":
+ for name, stored, live in [
+ ("requested", "repository-current", None),
+ ("persisted", "repository-current", None),
+ ("legacy", None, "repository-current"),
+ ("unverified", None, "repository-current"),
+ ("unrelated", "repository-other", None),
+ ("unresolvable-runtime", None, "repository-current"),
+ ("unresolvable-os", None, "repository-current"),
+ ("removed", "repository-current", None),
+ ("changed", "repository-previous", "repository-current"),
+ ]:
+ add_target(name, stored, live)
+ state.ensure_security_target(connection, paths["legacy"])
+ for name in paths:
+ add_scan(name + "-scan", name, "missing" if name == "unverified" else "current")
+ for number in range(5):
+ add_scan("unrelated-extra-" + str(number), "unrelated")
+ add_scan("legacy-second", "legacy")
+ missing.add(paths["removed"])
+ resolution_errors[paths["unresolvable-runtime"]] = RuntimeError
+ resolution_errors[paths["unresolvable-os"]] = OSError
+ before = add_finding("persisted-scan", "before-review")
+ after = add_finding("legacy-scan", "after-review", closed=True)
+ add_finding("legacy-second", "legacy-open")
+ add_finding("unrelated-scan", "unrelated-finding", closed=True)
+ connection.execute(
+ "INSERT INTO scan_comparisons VALUES (?, ?, ?, ?, ?)",
+ ("persisted-scan", "legacy-scan", "{}", timestamp, timestamp),
+ )
+ connection.execute(
+ "INSERT INTO scan_comparison_matches VALUES (?, ?, ?, ?, ?)",
+ ("persisted-scan", "legacy-scan", before, after, "Synthetic match"),
+ )
+ scans = listed("requested")
+ listing_probes = dict(probes)
+ probes.clear()
+ origins.clear()
+ matching = history.list_unmatched_scan_pairs(
+ connection, argparse.Namespace(repository=paths["requested"], force=False),
+ backfill_finding_details=lambda database, scan: None,
+ read_coverage=lambda scan: {},
+ )
+ matching_probes = dict(probes)
+ matching_origins = dict(origins)
+ probes.clear()
+ indexed = findings("requested")
+ indexing_probes = dict(probes)
+ probes.clear()
+ feedback_scope = []
+ feedback_queries = []
+ def scoped_index(database, **kwargs):
+ clause, values = kwargs["scan_scope"].sql()
+ feedback_scope.extend(sorted(row[0] for row in database.execute(
+ f"SELECT DISTINCT target_id FROM scans WHERE {clause}", values
+ )))
+ return indexes._indexed_findings(database, **kwargs)
+ connection.set_trace_callback(feedback_queries.append)
+ with patch.object(feedback_module, "_indexed_findings", side_effect=scoped_index):
+ feedback = get_scan_feedback(
+ connection, connection.execute("SELECT * FROM scans WHERE id = 'requested-scan'").fetchone()
+ )
+ connection.set_trace_callback(None)
+ index_queries = [query for query in feedback_queries if any(marker in query for marker in (
+ "SELECT before_scans.target_id AS before_target_id",
+ "SELECT scans.target_id, scans.id",
+ "occurrences.id AS occurrence_id",
+ ))]
+ feedback_probes = dict(probes)
+ print(json.dumps({
+ "scans": scans,
+ "removedExact": listed("removed"),
+ "matchingCount": matching["scanCount"],
+ "findings": indexed,
+ "feedback": [row["findingId"] for row in feedback["falsePositives"]],
+ "feedbackScope": feedback_scope,
+ "feedbackIndexQueriesScoped": bool(index_queries) and all(
+ "repository_generation = " in query for query in index_queries
+ ),
+ "listingRequestedProbes": listing_probes.get(paths["requested"], 0),
+ "matchingRequestedProbes": matching_probes.get(paths["requested"], 0),
+ "matchingUnrelatedProbes": matching_probes.get(paths["unrelated"], 0),
+ "matchingUnrelatedOrigins": matching_origins.get(paths["unrelated"], 0),
+ "indexingMaxProbes": max(indexing_probes.values(), default=0),
+ "feedbackMaxProbes": max(feedback_probes.values(), default=0),
+ "legacyStored": connection.execute(
+ "SELECT repository_identity FROM security_targets WHERE id = 'legacy'"
+ ).fetchone()[0],
+ "changedStored": connection.execute(
+ "SELECT repository_identity FROM security_targets WHERE id = 'changed'"
+ ).fetchone()[0],
+ }))
+ elif scenario == "selected-latest":
+ add_target("requested", "repository-current")
+ add_target("linked", "repository-current")
+ add_target("unrelated", "repository-other")
+ add_scan("legacy", "requested", generation=None)
+ add_finding("legacy", "legacy-only")
+ add_finding("legacy", "same-key", closed=True)
+ add_scan("bound", "linked")
+ add_finding("bound", "bound-only")
+ add_finding("bound", "same-key")
+ add_scan("other", "unrelated")
+ add_finding("other", "other-only")
+ selected = findings("requested")
+ unscoped = list(indexes._indexed_findings(connection))
+ print(json.dumps({
+ "selected": {row["occurrenceId"]: [row["confirmedInLatestScan"], row["status"], row["occurrenceCount"]] for row in selected},
+ "unscoped": {row["occurrence_id"]: row["confirmed_in_latest_scan"] for row in unscoped},
+ }))
+ elif scenario == "archive-recovery":
+ database = sqlite3.connect(":memory:")
+ database.row_factory = sqlite3.Row
+ database.execute("CREATE TABLE scans(id TEXT PRIMARY KEY, scan_dir TEXT UNIQUE)")
+ scan_dir = root / "outputs" / "scan"
+ archive = scan_dir.with_name(scan_dir.name + ".previous-synthetic")
+
+ def recover(*, expected="previous", owner="previous", archived_owner=None,
+ original_exists=True, nonempty=False, invalid_sibling=False,
+ invalid_directory=False, rename_fails=False):
+ selected_archive = archive if not invalid_sibling else scan_dir.with_name("unrelated")
+ database.execute("DELETE FROM scans")
+ if owner is not None:
+ database.execute("INSERT INTO scans VALUES (?, ?)", (owner, str(scan_dir)))
+ if archived_owner is not None:
+ database.execute("INSERT INTO scans VALUES (?, ?)", (archived_owner, str(selected_archive)))
+ database.commit()
+ present = {selected_archive, *((scan_dir,) if original_exists else ())}
+ operations = []
+ lock_checks = []
+
+ def canonical(path):
+ lock_checks.append(database.in_transaction)
+ if path not in present or invalid_directory:
+ raise SystemExit("Synthetic invalid scan directory")
+ return path
+
+ def lstat(path):
+ if path not in present:
+ raise FileNotFoundError(errno.ENOENT, "Synthetic missing path", str(path))
+ return SimpleNamespace(st_mode=stat.S_IFDIR)
+
+ def rmdir(path):
+ lock_checks.append(database.in_transaction)
+ if nonempty:
+ raise OSError(errno.ENOTEMPTY, "Synthetic nonempty output")
+ present.remove(path)
+ operations.append("rmdir")
+
+ def rename(path, destination):
+ lock_checks.append(database.in_transaction)
+ if rename_fails:
+ raise OSError("Synthetic rename failure")
+ assert destination not in present
+ present.remove(path)
+ present.add(destination)
+ operations.append("rename")
+
+ with patch.object(Path, "resolve", lambda path, *a, **k: path), \
+ patch.object(Path, "lstat", lstat), \
+ patch.object(Path, "rmdir", rmdir), \
+ patch.object(Path, "rename", rename):
+ try:
+ result = scan_start.restore_cli_scan_archive(database, argparse.Namespace(
+ scan_dir=str(scan_dir), archived_scan_dir=str(selected_archive),
+ previous_scan_id=expected, previous_scan_absent=expected is None,
+ ), canonical)["disposition"]
+ except (SystemExit, OSError):
+ result = "error"
+ return {
+ "disposition": result, "operations": operations,
+ "originalPresent": scan_dir in present, "archivePresent": selected_archive in present,
+ "locked": all(lock_checks), "transactionClosed": not database.in_transaction,
+ "owners": dict(database.execute("SELECT scan_dir, id FROM scans")),
+ }
+
+ base = ["workbench", "restore-cli-scan-archive", "--scan-dir", str(scan_dir), "--archived-scan-dir", str(archive)]
+ parser_accepts = []
+ for flags in (["--previous-scan-id", "previous"], ["--previous-scan-absent"], [], ["--previous-scan-id", "previous", "--previous-scan-absent"]):
+ with patch.object(sys, "argv", [*base, *flags]), redirect_stderr(io.StringIO()):
+ try:
+ workbench.parse_args("Synthetic archive recovery")
+ except SystemExit:
+ parser_accepts.append(False)
+ else:
+ parser_accepts.append(True)
+ cases = {
+ "recorded": recover(),
+ "unrecorded": recover(expected=None, owner=None),
+ "missingOriginal": recover(original_exists=False),
+ "committed": recover(owner="new", archived_owner="previous"),
+ "changedOwner": recover(owner="other"),
+ "unexpectedOwner": recover(expected=None),
+ "nonempty": recover(nonempty=True),
+ "invalidSibling": recover(invalid_sibling=True),
+ "invalidDirectory": recover(invalid_directory=True),
+ "renameFailure": recover(rename_fails=True),
+ }
+ print(json.dumps({"cases": cases, "parserAccepts": parser_accepts, "scanDir": str(scan_dir), "archiveDir": str(archive)}))
+ elif scenario == "archive-registration":
+ target = root / "requested"
+ scan_dir = root / "outputs" / "scan"
+ cases = {}
+ class ReadyToInsert(Exception):
+ pass
+ for case in ("valid", "owner-refused", "parent-refused", "nonempty", "noncanonical"):
+ events = []
+ def canonical(path, *, writable_output):
+ assert writable_output is True
+ events.append(["canonical", connection.in_transaction])
+ if case == "noncanonical" and connection.in_transaction:
+ raise SystemExit("Synthetic changed output")
+ return path
+ def entries(path):
+ assert path == scan_dir
+ events.append(["empty", connection.in_transaction])
+ return iter(["retained-artifact"] if case == "nonempty" and connection.in_transaction else [])
+ def register(database, path):
+ events.append(["owner", database.in_transaction])
+ if case == "owner-refused":
+ raise SystemExit("Synthetic refused owner")
+ return state.RegisteredRepositoryTarget("requested", "current")
+ def parent(database, scan_id):
+ events.append(["parent", database.in_transaction])
+ return {"target_id": "requested", "repository_generation": "other" if case == "parent-refused" else "current"}
+ def archive_scan(database, *args, **kwargs):
+ events.append(["archive", database.in_transaction])
+ raise ReadyToInsert()
+ with ExitStack() as mocks:
+ for name, value in {
+ "require_target": target,
+ "parse_scan_recipe": {"target": {"kind": "repository", "paths": []}, "mode": "standard"},
+ "prepare_cli_scan_target": (("synthetic", None, 7, 8), None),
+ "directory_snapshot_regular_file_count": 0,
+ }.items():
+ mocks.enter_context(patch.object(workbench, name, return_value=value))
+ mocks.enter_context(patch.object(workbench, "require_scannable_target"))
+ mocks.enter_context(patch.object(workbench, "require_uuid", side_effect=lambda value, label: value))
+ mocks.enter_context(patch.object(workbench, "require_canonical_scan_directory", side_effect=canonical))
+ mocks.enter_context(patch.object(workbench, "register_security_target", side_effect=register))
+ mocks.enter_context(patch.object(workbench, "require_scan", side_effect=parent))
+ mocks.enter_context(patch.object(workbench, "archive_scan", side_effect=archive_scan))
+ mocks.enter_context(patch.object(Path, "iterdir", entries))
+ try:
+ workbench.register_cli_scan(connection, argparse.Namespace(
+ repository=str(target), scan_dir=str(scan_dir), recipe_json="{}", parent_scan_id="parent", registration_json_stdin=False, recipe_json_stdin=False, archive_existing=False, archived_scan_dir=None,
+ ))
+ except ReadyToInsert:
+ accepted = True
+ except SystemExit:
+ accepted = False
+ cases[case] = {"accepted": accepted, "events": events, "transactionClosed": not connection.in_transaction}
+ print(json.dumps(cases))
+ elif scenario == "completion-order":
+ add_target("first", "repository-current")
+ add_target("second", "repository-current")
+ from finalize_scan_contract import _validate_date_time
+ precise = sys.argv[3] == "precise"
+ completions = {
+ "legacy-a": "2026-07-31t04:00:00.0000000002z" if precise else "2026-07-31t04:00:00z",
+ "legacy-b": "2026-07-31T05:00:00.0000000001+01:00" if precise else "2026-07-31T05:00:00+01:00",
+ }
+ add_scan("legacy-b", "second", started="2026-07-31T01:00:00Z", created="2026-07-31T05:00:00+01:00")
+ add_scan("legacy-a", "first", started="2026-07-31T02:00:00Z", created="2026-07-31T04:00:00Z")
+ for scan_id, completed_at in completions.items():
+ _validate_date_time(completed_at, "scan.completedAt")
+ connection.execute("UPDATE scans SET completed_at = ? WHERE id = ?", (completed_at, scan_id))
+ add_scan("legacy-missing", "first", started="2026-07-31T03:00:00Z")
+ connection.execute("UPDATE scans SET completed_at = NULL WHERE id = 'legacy-missing'")
+ apply_migrations(connection, MIGRATIONS, lambda: timestamp, state.backfill_security_targets)
+ add_scan("visible-last", "first", started="2026-08-01T01:00:00Z", status="running")
+ add_scan("visible-first", "second", started="2026-08-01T02:00:00Z", status="running")
+ def sequences():
+ return {row["id"]: row["completion_sequence"] for row in connection.execute(
+ "SELECT id, completion_sequence FROM scans ORDER BY completion_sequence, id"
+ )}
+ def predecessors(scan_id):
+ plan = history.list_unmatched_scan_pairs(
+ connection,
+ argparse.Namespace(repository=paths["first"], force=False, after_scan_id=scan_id),
+ backfill_finding_details=lambda *_: None, read_coverage=lambda _: {},
+ )
+ return [scan["scanId"] for batch in plan["batches"] for scan in batch["beforeScans"]]
+ legacy = sequences()
+ connection.execute(
+ "UPDATE scans SET status = 'complete', completed_at = '2026-08-01T04:00:00Z' "
+ "WHERE id = 'visible-first'"
+ )
+ connection.commit()
+ first_predecessors = predecessors("visible-first")
+ connection.execute(
+ "UPDATE scans SET status = 'complete', completed_at = '2026-08-01T03:00:00Z' "
+ "WHERE id = 'visible-last'"
+ )
+ connection.commit()
+ add_finding("visible-first", "first-finding")
+ add_finding("visible-last", "last-finding")
+ confirmed = {row["findingId"]: row["confirmedInLatestScan"] for row in findings("first")}
+ last_predecessors = predecessors("visible-last")
+ reciprocal_predecessors = predecessors("visible-first")
+ connection.execute("UPDATE scans SET status = 'complete' WHERE id = 'visible-last'")
+ connection.execute("UPDATE scans SET status = 'failed' WHERE id = 'visible-last'")
+ add_scan("inserted-complete", "second", created="2026-07-01T00:00:00Z")
+ before_repair = sequences()
+ apply_migrations(connection, MIGRATIONS, lambda: timestamp, state.backfill_security_targets)
+ print(json.dumps({
+ "legacy": legacy,
+ "legacyGenerationsNull": all(row[0] is None for row in connection.execute(
+ "SELECT repository_generation FROM scans WHERE id LIKE 'legacy-%'"
+ )),
+ "firstPredecessors": first_predecessors,
+ "lastPredecessors": last_predecessors,
+ "reciprocalPredecessors": reciprocal_predecessors,
+ "confirmed": confirmed,
+ "sequences": sequences(),
+ "idempotent": sequences() == before_repair,
+ "sequenceOutranksFallback": history._scan_completion_order({
+ "id": "fallback", "started_at": "2999-01-01T00:00:00Z"
+ }) < history._scan_completion_order({
+ "id": "sequenced", "completion_sequence": 1
+ }),
+ "sealedTimes": {row["id"]: row["completed_at"] for row in connection.execute(
+ "SELECT id, completed_at FROM scans WHERE id IN ('legacy-a', 'legacy-b', 'visible-first', 'visible-last')"
+ )},
+ }))
+ elif scenario == "persisted-alias":
+ for name, stored, live in [
+ ("requested", "repository-current", None),
+ ("reused", "repository-current", "repository-other"),
+ ("legacy", None, "repository-current"),
+ ("unverified", None, "repository-current"),
+ ("unrelated", "repository-other", None),
+ ]:
+ add_target(name, stored, live)
+ if name == "legacy":
+ state.ensure_security_target(connection, paths[name])
+ add_scan(name + "-scan", name, "missing" if name == "unverified" else "current")
+ for name, identity in [("fresh", "repository-current"), ("empty", "repository-empty")]:
+ add_target(name, identity)
+ connection.execute("DELETE FROM security_targets WHERE id = ?", (name,))
+ metadata[paths["reused"]].st_ino += 1000
+ add_finding("requested-scan", "current-finding")
+ add_finding("reused-scan", "historical-finding", closed=True)
+ scans = listed("requested")
+ reused_listing_probes = probes[paths["reused"]]
+ matching = history.list_unmatched_scan_pairs(
+ connection, argparse.Namespace(repository=paths["requested"], force=False),
+ backfill_finding_details=lambda *_: None, read_coverage=lambda _: {},
+ )
+ feedback = get_scan_feedback(
+ connection, connection.execute("SELECT * FROM scans WHERE id = 'requested-scan'").fetchone()
+ )
+ def findings_page(name=None, target_id=None):
+ return indexes.list_global_findings(
+ connection,
+ argparse.Namespace(repository=paths[name] if name else None,
+ target_id=target_id, limit=50, offset=0,
+ query=None, severity=None, status=None),
+ )
+ target_count = connection.execute("SELECT COUNT(*) FROM security_targets").fetchone()[0]
+ fresh_findings = findings_page("fresh")
+ empty_findings = findings_page("empty")
+ replacement_findings = findings_page("reused")
+ with patch.object(sys, "argv", [
+ "workbench", "list-global-findings", "--repository", paths["fresh"],
+ "--target-id", "reused",
+ ]), redirect_stderr(io.StringIO()):
+ try:
+ workbench.parse_args("Synthetic selector test")
+ except SystemExit as error:
+ rejects_both_selectors = error.code == 2
+ else:
+ rejects_both_selectors = False
+ print(json.dumps({
+ "scans": scans,
+ "replacementRequest": listed("reused"),
+ "findings": sorted(row["findingId"] for row in findings("requested")),
+ "freshFindings": sorted(row["findingId"] for row in fresh_findings["findings"]),
+ "emptyFindings": empty_findings["findings"],
+ "replacementFindings": replacement_findings["findings"],
+ "projectionAvailable": {
+ "fresh": fresh_findings["projectionAvailable"],
+ "empty": empty_findings["projectionAvailable"],
+ "replacementPath": replacement_findings["projectionAvailable"],
+ "replacementId": findings_page(target_id="reused")["projectionAvailable"],
+ "unknownId": findings_page(target_id="not-registered")["projectionAvailable"],
+ },
+ "rejectsBothSelectors": rejects_both_selectors,
+ "readOnly": target_count == connection.execute("SELECT COUNT(*) FROM security_targets").fetchone()[0]
+ and connection.execute("SELECT 1 FROM security_targets WHERE id = 'fresh'").fetchone() is None,
+ "feedback": [row["findingId"] for row in feedback["falsePositives"]],
+ "matchingCount": matching["scanCount"],
+ "reusedListingProbes": reused_listing_probes,
+ "stored": connection.execute(
+ "SELECT repository_identity FROM security_targets WHERE id = 'reused'"
+ ).fetchone()[0],
+ }))
+ elif scenario == "saved-start":
+ original_identity = add_target("requested", "repository-current")
+ other_identity = add_target("other", "repository-other")
+ add_scan("historical", "requested")
+ target = Path(paths["requested"])
+ target_root = root / "scan-output"
+ stack.enter_context(patch.object(workbench, "require_uuid", side_effect=lambda value, label: value))
+ stack.enter_context(patch.object(workbench, "require_target", return_value=target))
+ stack.enter_context(patch.object(workbench, "require_remediation_target", return_value=target))
+ stack.enter_context(patch.object(workbench, "require_scannable_target"))
+ stack.enter_context(patch.object(workbench, "require_scope", return_value="."))
+ stack.enter_context(patch.object(workbench, "directory_snapshot_regular_file_count", return_value=0))
+ stack.enter_context(patch.object(workbench, "scan_target_identity", return_value=("synthetic", None, 7, metadata[str(target)].st_ino)))
+ stack.enter_context(patch.object(workbench, "scan_target_root", return_value=target_root))
+ checks = []
+ original_register = workbench.register_security_target
+ def register(database, target_path):
+ checks.append({"transaction": database.in_transaction, "path": target_path})
+ return original_register(database, target_path)
+ stack.enter_context(patch.object(workbench, "register_security_target", side_effect=register))
+ class StartAccepted(Exception):
+ pass
+ stack.enter_context(patch.object(workbench, "insert_running_scan", side_effect=StartAccepted))
+ accepted = {}
+ for name, saved_target, live in [
+ ("valid", "requested", original_identity),
+ ("changed-generation", "requested", other_identity),
+ ("changed-saved-id", "other", original_identity),
+ ]:
+ details[str(target)] = live
+ connection.execute(
+ "INSERT INTO workspaces (id, target_id, target_path, submitted, created_at, updated_at) "
+ "VALUES (?, ?, ?, 1, ?, ?)",
+ (name, saved_target, str(target), timestamp, timestamp),
+ )
+ connection.commit()
+ try:
+ workbench.start_scan(
+ connection, argparse.Namespace(workspace_id=name, scan_root=None, model=None, reasoning_effort=None)
+ )
+ except StartAccepted:
+ accepted[name] = True
+ except SystemExit:
+ accepted[name] = False
+ print(json.dumps({
+ "accepted": accepted,
+ "verifiedInsideTransaction": len(checks) == 3 and all(
+ item == {"transaction": True, "path": str(target)} for item in checks
+ ),
+ "scanCount": connection.execute("SELECT COUNT(*) FROM scans").fetchone()[0],
+ "stored": connection.execute("SELECT repository_identity FROM security_targets WHERE id = 'requested'").fetchone()[0],
+ }))
+ elif scenario == "v30-current":
+ generation_birth = state._timestamp_ns("2026-08-02T00:00:00Z")
+ later = "2026-08-03T00:00:00Z"
+ for name in ("current-owner", "old-owner", "removed-old", "removed-valid", "invalid-time"):
+ add_target(name, "current-generation", birth=generation_birth)
+ recorded = later if name in ("current-owner", "removed-valid") else timestamp
+ add_scan(name + "-scan", name, started="invalid" if name == "invalid-time" else recorded, created=recorded)
+ add_target("unverified-anchor", "unverified-current")
+ add_scan("unverified-anchor-scan", "unverified-anchor", owner="missing")
+ add_target("opaque-missing", "opaque-current")
+ add_scan("opaque-missing-scan", "opaque-missing")
+ add_target("valid-scope", "scope-current", relative="service")
+ add_scan("valid-scope-scan", "valid-scope")
+ missing.update(paths[name] for name in ("removed-old", "removed-valid", "opaque-missing"))
+ for name in ("current-owner", "old-owner", "removed-old", "removed-valid"):
+ add_finding(name + "-scan", name + "-finding", closed=name != "current-owner")
+ connection.execute(
+ "INSERT INTO scan_artifacts VALUES (?, ?, ?, ?)",
+ (name + "-scan", "findings", str(root / name / "findings.json"), timestamp),
+ )
+ tables = ("scans", "findings", "finding_occurrences", "finding_triage", "scan_artifacts")
+ columns = {
+ table: ", ".join(row["name"] for row in connection.execute("PRAGMA table_info(" + table + ")"))
+ for table in tables
+ }
+ def retained_records():
+ return {table: [tuple(row) for row in connection.execute(
+ "SELECT " + columns[table] + " FROM " + table
+ )] for table in tables}
+ retained = retained_records()
+ target_ids = sorted(paths)
+ apply_migrations(connection, MIGRATIONS, lambda: timestamp, state.backfill_security_targets)
+ state.backfill_repository_identities(connection)
+ try:
+ state.ensure_security_target(connection, paths["old-owner"])
+ except SystemExit:
+ old_registration_rejected = True
+ else:
+ old_registration_rejected = False
+ print(json.dumps({
+ "stored": {row["id"]: row["repository_identity"] for row in connection.execute(
+ "SELECT id, repository_identity FROM security_targets"
+ )},
+ "recordsPreserved": retained == retained_records(),
+ "targetIdsPreserved": target_ids == sorted(row["id"] for row in connection.execute("SELECT id FROM security_targets")),
+ "removedExact": listed("removed-old"),
+ "visibleFindings": sorted(row["findingId"] for row in findings("current-owner")),
+ "oldRegistrationRejected": old_registration_rejected,
+ }))
+ elif scenario == "lineage":
+ for name, stored, live in [
+ ("requested", "repository-current", None),
+ ("removed", "repository-current", None),
+ ("legacy", None, "repository-current"),
+ ("unverified", None, "repository-current"),
+ ("clone", "repository-other", None),
+ ("scope", "repository-current-scope", None),
+ ("changed", "repository-previous", "repository-current"),
+ ]:
+ add_target(name, stored, live, relative="service" if name == "scope" else ".")
+ add_scan(name + "-scan", name, "missing" if name == "unverified" else "current")
+ missing.add(paths["removed"])
+ scan_dir = root / "scan-output"
+ original_iterdir = Path.iterdir
+ stack.enter_context(patch.object(Path, "iterdir", lambda path: iter(()) if path == scan_dir else original_iterdir(path)))
+ stack.enter_context(patch.object(workbench, "require_target", return_value=Path(paths["requested"])))
+ stack.enter_context(patch.object(workbench, "require_scannable_target"))
+ stack.enter_context(patch.object(workbench, "require_canonical_scan_directory", return_value=scan_dir))
+ stack.enter_context(patch.object(workbench, "directory_snapshot_regular_file_count", return_value=0))
+ target_identity = ("synthetic", "snapshot", 7, metadata[paths["requested"]].st_ino)
+ stack.enter_context(patch.object(workbench, "prepare_cli_scan_target", return_value=(target_identity, None)))
+ stack.enter_context(patch.object(workbench, "require_uuid", side_effect=lambda value, label: value))
+ stack.enter_context(patch.object(workbench, "archive_scan"))
+ class LineageAccepted(Exception):
+ pass
+ stack.enter_context(patch.object(workbench, "insert_running_scan", side_effect=LineageAccepted))
+ connection.commit()
+ accepted = {}
+ for name in paths:
+ args = argparse.Namespace(
+ repository=paths["requested"], scan_dir=str(scan_dir), parent_scan_id=name + "-scan",
+ recipe_json=json.dumps({"repository": paths["requested"], "mode": "standard", "config": {}, "target": {"kind": "repository", "paths": []}}),
+ registration_json_stdin=False, recipe_json_stdin=False,
+ archive_existing=False, archived_scan_dir=None,
+ )
+ try:
+ workbench.register_cli_scan(connection, args)
+ except LineageAccepted:
+ accepted[name] = True
+ except SystemExit:
+ accepted[name] = False
+ print(json.dumps({"accepted": accepted, "scanCount": connection.execute("SELECT COUNT(*) FROM scans").fetchone()[0]}))
+ elif scenario == "null-history":
+ newer_birth = state._timestamp_ns("2026-08-02T00:00:00Z")
+ for name, birth in [
+ ("unchanged", 1_000_000_000), ("newer", newer_birth),
+ ("invalid-time", 1_000_000_000), ("no-history", newer_birth),
+ ]:
+ add_target(name, None, "current-" + name, birth=birth)
+ if name != "no-history":
+ add_scan(name + "-scan", name, started="invalid" if name == "invalid-time" else timestamp)
+ apply_migrations(connection, MIGRATIONS, lambda: timestamp, state.backfill_security_targets)
+ add_target("replacement-alias", "current-newer", birth=newer_birth)
+ add_scan("replacement-alias-scan", "replacement-alias", started="2026-08-03T00:00:00Z")
+ errors = {}
+ for name in ("unchanged", "newer", "invalid-time", "no-history"):
+ try:
+ state.ensure_security_target(connection, paths[name])
+ except SystemExit as error:
+ errors[name] = str(error)
+ print(json.dumps({
+ "stored": {row["id"]: row["repository_identity"] for row in connection.execute(
+ "SELECT id, repository_identity FROM security_targets"
+ )},
+ "registrationErrors": sorted(errors),
+ "scans": listed("replacement-alias"),
+ "sameCheckoutMetadata": connection.execute(
+ "SELECT target_inode FROM scans WHERE id = 'newer-scan'"
+ ).fetchone()[0] == metadata[paths["newer"]].st_ino,
+ }))
+ elif scenario == "late-null":
+ add_target("requested", "repository-current")
+ add_scan("requested-scan", "requested")
+ add_target("unscanned", None, "repository-current")
+ add_target("unselected", None, "repository-current")
+ details.pop(paths["unselected"])
+ add_target("historical", None, "repository-current")
+ add_scan("historical-scan", "historical")
+ add_finding("historical-scan", "historical-finding", closed=True)
+ probes.clear()
+ backfill = Mock(wraps=state.backfill_security_targets)
+ apply_migrations(connection, MIGRATIONS, lambda: timestamp, backfill)
+ apply_migrations(connection, MIGRATIONS, lambda: timestamp, backfill)
+ maintenance_probes = dict(probes)
+ probes.clear()
+ with patch.object(state, "_bind_unscanned_repository_identity", wraps=state._bind_unscanned_repository_identity) as guarded_bind:
+ selected_id = state.ensure_security_target(connection, paths["unscanned"])
+ selected_guard = guarded_bind.call_count == 1 and guarded_bind.call_args.args[1:] == (
+ "unscanned", paths["unscanned"], "repository-current",
+ )
+ selected_probes = dict(probes)
+ historical_id = state.ensure_security_target(connection, paths["historical"])
+ add_scan("established-scan", "unscanned")
+ missing.update((paths["unscanned"], paths["historical"]))
+ print(json.dumps({
+ "stored": {row["id"]: row["repository_identity"] for row in connection.execute(
+ "SELECT id, repository_identity FROM security_targets"
+ )},
+ "fullBackfillCalls": backfill.call_count,
+ "maintenanceProbes": {name: maintenance_probes.get(path, 0) for name, path in paths.items()},
+ "selectedProbes": {name: selected_probes.get(path, 0) for name, path in paths.items()},
+ "selectedRegistrationId": selected_id,
+ "selectedGuarded": selected_guard,
+ "historicalRegistrationId": historical_id,
+ "scans": listed("requested"),
+ "historicalExact": listed("historical"),
+ "feedback": get_scan_feedback(
+ connection, connection.execute("SELECT * FROM scans WHERE id = 'requested-scan'").fetchone()
+ )["falsePositives"],
+ }))
+ elif scenario == "binding":
+ for name, stored in (
+ ("eligible", None), ("id-history", None), ("path-history", None),
+ ("donor", None), ("bound", "repository-bound"),
+ ):
+ add_target(name, stored, "repository-current")
+ add_scan("id-history-scan", "id-history")
+ connection.execute("UPDATE scans SET target_path = ? WHERE id = 'id-history-scan'", (paths["donor"],))
+ add_scan("path-history-scan", "donor")
+ connection.execute("UPDATE scans SET target_path = ? WHERE id = 'path-history-scan'", (paths["path-history"],))
+ def bind(name, path=None):
+ return state._bind_unscanned_repository_identity(
+ connection, name, path or paths[name], "repository-current"
+ )
+ guarded = {
+ "idHistory": bind("id-history"),
+ "pathHistory": bind("path-history"),
+ "wrongPath": bind("eligible", paths["donor"]),
+ "alreadyBound": bind("bound"),
+ "eligible": bind("eligible"),
+ "repeat": bind("eligible"),
+ }
+ path = paths["eligible"]
+ def inspected(target_id, stored=None, *, historical=False, owner=True):
+ return state.RepositoryTargetState(
+ target_id, path, stored, resolved_path=path, repository=details[path],
+ ownership_matches=owner, strict_owner_matches=True,
+ generation_predates_history=True, has_historical_scans=historical,
+ )
+ def cursor(row):
+ return SimpleNamespace(fetchone=lambda: row)
+ ignored = Mock()
+ ignored.execute.side_effect = [cursor(None), SimpleNamespace(rowcount=0), cursor({
+ "id": "actual-target", "repository_identity": None,
+ })]
+ with patch.object(state, "stable_target_id", return_value="proposed-target"), \
+ patch.object(state, "_inspect_repository_target", return_value=inspected("actual-target", historical=True)) as inspect, \
+ patch.object(state, "_bind_unscanned_repository_identity") as guarded_bind:
+ ignored_id = state.ensure_security_target(ignored, path)
+ ignored_reselected = inspect.call_args.args[1:] == ("actual-target", path, None)
+ ignored_unbound = guarded_bind.call_count == 0
+ insert = ignored.execute.call_args_list[1].args
+ def lost_binding(stored, owner):
+ database = Mock()
+ database.execute.side_effect = [
+ cursor({"id": "actual-target", "repository_identity": None}),
+ cursor({"id": "actual-target", "repository_identity": stored}),
+ ]
+ with patch.object(state, "_inspect_repository_target", side_effect=[
+ inspected("actual-target"),
+ inspected("actual-target", stored, historical=True, owner=owner),
+ ]) as inspect, \
+ patch.object(state, "_bind_unscanned_repository_identity", return_value=False) as guarded_bind:
+ try:
+ accepted = state.ensure_security_target(database, path) == "actual-target"
+ except SystemExit:
+ accepted = False
+ return {
+ "accepted": accepted,
+ "rechecked": inspect.call_count == 2 and inspect.call_args.args[1:] == ("actual-target", path, stored),
+ "guarded": guarded_bind.call_args.args[1:] == ("actual-target", path, "repository-current"),
+ }
+ print(json.dumps({
+ "guarded": guarded,
+ "stored": {row["id"]: row["repository_identity"] for row in connection.execute(
+ "SELECT id, repository_identity FROM security_targets"
+ )},
+ "ignoredInsert": {
+ "id": ignored_id, "reselected": ignored_reselected, "unbound": ignored_unbound,
+ "insertsNull": "INSERT OR IGNORE" in insert[0] and "repository_identity" not in insert[0] and len(insert[1]) == 5,
+ },
+ "lostToHistory": lost_binding(None, True),
+ "lostToConflictingIdentity": lost_binding("repository-other", False),
+ }))
+ elif scenario == "sealed-comparison":
+ for name, stored in (("legacy", None), ("first", "repository-saved"), ("second", "repository-saved")):
+ add_target(name, stored)
+ for scan_id, target in (("legacy-before", "legacy"), ("legacy-after", "legacy"), ("first-scan", "first"), ("second-scan", "second")):
+ add_scan(scan_id, target)
+ resolution_errors.update({paths["legacy"]: RuntimeError, paths["first"]: OSError, paths["second"]: RuntimeError})
+ connection.commit()
+ require_scan = lambda database, scan_id: database.execute("SELECT * FROM scans WHERE id = ?", (scan_id,)).fetchone()
+ coverage = lambda scan: {"completeness": "complete"}
+ accepted = []
+ with patch.object(state, "_inspect_repository_target", side_effect=AssertionError("Sealed comparison inspected the live target")):
+ for before, after in (("legacy-before", "legacy-after"), ("first-scan", "second-scan")):
+ args = argparse.Namespace(before_scan_id=before, after_scan_id=after, matches_json='{"matches":[],"uncertain":[]}', matches_json_stdin=False)
+ compared = history.compare_scans(connection, args, require_scan=require_scan, read_coverage=coverage)
+ saved = history.save_scan_comparison(connection, args, now=lambda: timestamp, require_scan=require_scan, read_coverage=coverage)
+ accepted.append(compared["afterScanId"] == after and saved["beforeScanId"] == before)
+ conflicting = history._same_repository(
+ connection,
+ {"target_id": "same-target", "repository_generation": "repository-first"},
+ {"target_id": "same-target", "repository_generation": "repository-second"},
+ identities=Mock(),
+ )
+ targetless = {"target_id": None, "target_path": paths["legacy"]}
+ healthy = state.RepositoryTargetState(
+ "", paths["legacy"], None, resolved_path=paths["legacy"], ownership_matches=True
+ )
+ refused_targetless = state.RepositoryTargetState(
+ "", paths["legacy"], None, resolved_path=paths["legacy"]
+ )
+ healthy_path = history._same_repository(
+ connection, targetless, targetless,
+ identities=Mock(for_row=Mock(return_value=healthy)),
+ )
+ refused_path = history._same_repository(
+ connection, targetless, targetless,
+ identities=Mock(for_row=Mock(return_value=refused_targetless)),
+ )
+ add_target("unregistered", None)
+ connection.execute("DELETE FROM security_targets WHERE id = 'unregistered'")
+ refused = state.RepositoryTargetState("", paths["unregistered"], None)
+ cache = Mock(supports_identity=True)
+ cache.for_path.return_value = refused
+ with patch.object(history, "RepositoryIdentityCache", return_value=cache):
+ try:
+ history.list_unmatched_scan_pairs(
+ connection, argparse.Namespace(repository=paths["unregistered"], force=False),
+ backfill_finding_details=lambda *_: None, read_coverage=coverage,
+ )
+ except SystemExit:
+ automatic_rejected = True
+ else:
+ automatic_rejected = False
+ print(json.dumps({
+ "accepted": accepted, "conflictingIdentitiesAccepted": conflicting,
+ "healthyTargetless": healthy_path, "refusedTargetless": refused_path,
+ "savedCount": connection.execute("SELECT COUNT(*) FROM scan_comparisons").fetchone()[0],
+ "unregisteredAutomaticRequesterRejected": automatic_rejected,
+ }))
+ else:
+ stack.enter_context(patch.object(os.path, "normcase", lambda value: os.fspath(value).lower()))
+ originals = {}
+ expected = {}
+ for name in [
+ "old-basic", "old-description", "current", "unknown", "newer-generation",
+ "unverified", "changed-owner", "unavailable", "invalid-time", "no-scans",
+ "scope-upper", "scope-lower",
+ ]:
+ relative = "Service" if name == "scope-upper" else "service" if name == "scope-lower" else "."
+ birth = state._timestamp_ns("2026-08-02T00:00:00Z") if name == "newer-generation" else 1_000_000_000
+ identity = add_target(name, None, "current-" + name, relative, birth)
+ stored = (
+ identity.value if name == "current" else
+ "unknown-identity" if name == "unknown" else
+ legacy_hash(identity, name == "old-description")
+ )
+ originals[name] = stored
+ connection.execute(
+ "UPDATE security_targets SET repository_identity = ? WHERE id = ?", (stored, name)
+ )
+ if name != "no-scans":
+ owner = "missing" if name == "unverified" else "mismatch" if name == "changed-owner" else "current"
+ add_scan(name + "-scan", name, owner, "invalid" if name == "invalid-time" else timestamp)
+ if name == "unavailable":
+ missing.add(paths[name])
+ expected[name] = (
+ identity.value if name == "current" else identity.legacy_value if name in {
+ "old-basic", "old-description", "no-scans", "scope-upper", "scope-lower"
+ } else None
+ )
+ add_finding("old-basic-scan", "historical-finding", closed=True)
+ connection.execute(
+ "INSERT INTO scan_artifacts VALUES (?, ?, ?, ?)",
+ ("old-basic-scan", "findings", str(root / "historical-findings.json"), timestamp),
+ )
+ tables = ("scans", "findings", "finding_occurrences", "finding_triage", "scan_artifacts")
+ columns = {
+ table: ", ".join(row["name"] for row in connection.execute("PRAGMA table_info(" + table + ")"))
+ for table in tables
+ }
+ def retained_records():
+ return {table: [tuple(row) for row in connection.execute(
+ "SELECT " + columns[table] + " FROM " + table
+ )] for table in tables}
+ retained = retained_records()
+ starting_version = connection.execute(
+ "SELECT version FROM schema_migrations WHERE name = ?",
+ ("persist repository identities",),
+ ).fetchone()[0]
+ real_normalize = state.normalize_pre_release_repository_identities
+ normalization_transactions = []
+ def normalize(database):
+ normalization_transactions.append(database.in_transaction)
+ return real_normalize(database)
+ with patch.object(state, "normalize_pre_release_repository_identities", side_effect=normalize) as normalizer:
+ apply_migrations(connection, MIGRATIONS, lambda: timestamp, state.backfill_security_targets)
+ first = {
+ row["id"]: row["repository_identity"]
+ for row in connection.execute("SELECT id, repository_identity FROM security_targets")
+ }
+ probes.clear()
+ apply_migrations(connection, MIGRATIONS, lambda: timestamp, state.backfill_security_targets)
+ current_open_probes = sum(probes.values())
+ normalization_count = normalizer.call_count
+ second = {
+ row["id"]: row["repository_identity"]
+ for row in connection.execute("SELECT id, repository_identity FROM security_targets")
+ }
+ print(json.dumps({
+ "identities": first,
+ "expected": expected,
+ "startingVersion": starting_version,
+ "normalizationCount": normalization_count,
+ "normalizationTransactions": normalization_transactions,
+ "currentOpenProbes": current_open_probes,
+ "recordsPreserved": retained == retained_records(),
+ "scanGenerationsUnbound": all(row[0] is None for row in connection.execute("SELECT repository_generation FROM scans")),
+ "idempotent": first == second,
+ "foldedLegacyScopesEqual": originals["scope-upper"] == originals["scope-lower"],
+ "currentScopesDistinct": first["scope-upper"] != first["scope-lower"],
+ "targetCount": len(first),
+ "migrations": [row["version"] for row in connection.execute("SELECT version FROM schema_migrations ORDER BY version")],
+ }))
+`;
+
+async function run(
+ scenario: string,
+ ...scenarioArgs: string[]
+): Promise<Record<string, unknown>> {
+ const python = pythonExecutable()!;
+ // Keep the Python fixture off the Windows command line.
+ const execution = await runCommand(
+ python,
+ ["-I", "-B", "-", join(PLUGIN_ROOT, "scripts"), scenario, ...scenarioArgs],
+ { input: probe, timeout: 10_000 },
+ );
+ expect(execution.status, execution.error?.message ?? execution.stderr).toBe(
+ 0,
+ );
+ return JSON.parse(execution.stdout) as Record<string, unknown>;
+}
+
+test("uses registered old-Git paths and stable object-directory generation evidence", async () => {
+ expect(await run("generation-metadata")).toEqual({
+ oldMain: true,
+ oldLinkedMatches: true,
+ nulMatches: true,
+ wrongBacklink: null,
+ unchanged: true,
+ objectInstanceChangesGeneration: true,
+ legacyMaterialUnchanged: true,
+ domainSeparated: true,
+ actualObjectLookup: true,
+ oldPrimary: {
+ localAbsolute: true,
+ worktreeRelative: true,
+ literalLeadingTilde: true,
+ trailingLineFeed: true,
+ trailingWhitespace: true,
+ missing: false,
+ wrongScopes: true,
+ malformed: true,
+ wrongRoot: false,
+ foreignForward: false,
+ },
+ rawConfigLookup: true,
+ });
+});
+
+test("keeps automatic history bounded by saved scan generation", async () => {
+ const result = await run("scan-generation");
+ expect(result["selected"]).toEqual([
+ "alias-proved",
+ "requested-legacy",
+ "requested-new",
+ ]);
+ expect(result["predicate"]).toEqual({ binds: 2, ors: 1 });
+ expect(result["feedback"]).toEqual(["local-review", "proved-review"]);
+ expect(result["findings"]).toEqual([
+ ["current-open", 1, "open"],
+ ["local-review", 1, "closed"],
+ ["proved-review", 1, "closed"],
+ ]);
+ expect(result["oldClientGeneration"]).toBeNull();
+ expect(result["explicitClones"]).toBe(true);
+ expect(result["explicitLegacy"]).toBe(true);
+ expect(result["ownContradiction"]).toBe(false);
+ expect(result["weakRegistration"]).toBe("generation-strong");
+ expect(result["weakHistory"]).toBeNull();
+ expect(result["weakBindingPreserved"]).toBe(true);
+ expect(result["absentExactCounts"]).toEqual([1, 1]);
+});
+
+test("counts findings in each selected repository scope and preserves absent decisions", async () => {
+ const result = await run("repository-counts");
+ const all = result["all"] as {
+ counts: Record<string, number>;
+ ids: string[];
+ calls: Array<string | null>;
+ inspections: string[];
+ };
+
+ expect(all.counts).toEqual({
+ first: 2,
+ second: 2,
+ absent: 1,
+ independent: 1,
+ refused: 0,
+ });
+ expect(result["directCounts"]).toEqual(all.counts);
+ expect(all.calls.toSorted()).toEqual([
+ "absent",
+ "first",
+ "independent",
+ "second",
+ ]);
+ expect(all.inspections).toEqual([
+ "absent",
+ "first",
+ "independent",
+ "refused",
+ "second",
+ ]);
+ expect(result["absentOnly"]).toEqual({
+ counts: { absent: 1 },
+ ids: ["absent"],
+ calls: ["absent"],
+ inspections: ["absent"],
+ nextOffset: null,
+ });
+ expect(result["queryOnly"]).toEqual({
+ counts: { independent: 1 },
+ ids: ["independent"],
+ calls: ["independent"],
+ inspections: ["independent"],
+ nextOffset: null,
+ });
+ expect(result["refusedOnly"]).toEqual({
+ counts: { refused: 0 },
+ ids: ["refused"],
+ calls: [],
+ inspections: ["refused"],
+ nextOffset: null,
+ });
+ expect(result["notScanned"]).toEqual({
+ counts: {},
+ ids: [],
+ calls: [],
+ inspections: [],
+ nextOffset: null,
+ });
+ expect((result["openIds"] as string[]).sort()).toEqual([
+ "absent",
+ "first",
+ "independent",
+ "second",
+ ]);
+ expect(result["pagePreserved"]).toBe(true);
+ for (const [name, offset] of [
+ ["ordinaryPage", 0],
+ ["offsetPage", 1],
+ ["pastEnd", 99],
+ ] as const) {
+ const page = result[name] as typeof all & { nextOffset: number | null };
+ const ids = all.ids.slice(offset, offset + 2);
+ expect(page.ids).toEqual(ids);
+ expect(page.counts).toEqual(
+ Object.fromEntries(
+ Object.entries(all.counts).filter(([id]) => ids.includes(id)),
+ ),
+ );
+ expect(page.calls.toSorted()).toEqual(
+ ids.filter((id) => id !== "refused").toSorted(),
+ );
+ expect(page.inspections).toEqual(ids.toSorted());
+ expect(page.nextOffset).toBe(
+ offset + ids.length < all.ids.length ? offset + ids.length : null,
+ );
+ }
+ expect(result["emptyQuery"]).toEqual({
+ counts: {},
+ ids: [],
+ calls: [],
+ inspections: [],
+ nextOffset: null,
+ });
+ for (const name of ["openPage", "openPastEnd"]) {
+ const page = result[name] as typeof all;
+ expect(page.calls.toSorted()).toEqual(all.calls.toSorted());
+ expect(page.inspections).toEqual(all.inspections);
+ }
+ const openPastEnd = result["openPastEnd"] as typeof all & {
+ nextOffset: number | null;
+ };
+ expect(openPastEnd.ids).toEqual([]);
+ expect(openPastEnd.counts).toEqual({});
+ expect(openPastEnd.nextOffset).toBeNull();
+});
+
+test("records generation explicitly in both transactional parent scan writers", async () => {
+ const result = await run("scan-writers");
+ expect(result["generations"]).toEqual({
+ "current-writer": "generation-writer",
+ "old-writer": null,
+ [result["deepId"] as string]: "generation-writer",
+ });
+ expect(result["registeredInsideTransaction"]).toEqual([true, true]);
+ expect(result["generationIndex"]).toBe(true);
+});
+
+test("reads precise Linux birth time through supported native interfaces", async () => {
+ const result = await run("birth-time");
+ const cases = result["cases"] as Record<string, unknown>;
+
+ expect(result["layout"]).toEqual([16, 80, 256]);
+ expect(result["native"]).toBe(42_000_000_123);
+ expect(result["unavailable"]).toBeNull();
+ expect(cases["wrapper"]).toEqual({
+ value: 42_000_000_123,
+ calls: [null],
+ typed: true,
+ });
+ for (const [architecture, number] of [
+ ["x86_64", 332],
+ ["aarch64", 291],
+ ] as const) {
+ expect(cases[architecture]).toEqual({
+ value: 42_000_000_123,
+ calls: [number],
+ typed: true,
+ });
+ }
+ for (const name of ["unknown-abi", "pointer32", "long32"]) {
+ expect(cases[name]).toEqual({ value: null, calls: [], typed: true });
+ }
+ for (const name of [
+ "missing-mask",
+ "invalid-nanoseconds",
+ "zero",
+ "negative",
+ "failed",
+ ]) {
+ expect(cases[name]).toEqual({ value: null, calls: [null], typed: true });
+ }
+ for (const name of ["missing-symbols", "missing-libc", "invalid-layout"]) {
+ expect(cases[name]).toBeNull();
+ }
+});
+
+test("reuses established aliases and probes each saved target once per request", async () => {
+ const result = await run("cache");
+
+ expect(result["scans"]).toEqual([
+ "legacy-scan",
+ "legacy-second",
+ "persisted-scan",
+ "removed-scan",
+ "requested-scan",
+ ]);
+ expect(result["removedExact"]).toEqual(["removed-scan"]);
+ expect(result["matchingCount"]).toBe(5);
+ const findings = result["findings"] as Array<Record<string, unknown>>;
+ expect(
+ findings.find((finding) => finding["status"] === "closed"),
+ ).toMatchObject({
+ occurrenceCount: 2,
+ matchedFindingIds: ["after-review", "before-review"],
+ });
+ expect(
+ findings.find((finding) => finding["findingId"] === "legacy-open"),
+ ).toBeDefined();
+ expect(result["feedback"]).toEqual(["after-review"]);
+ expect(result["feedbackScope"]).toEqual([
+ "legacy",
+ "persisted",
+ "removed",
+ "requested",
+ ]);
+ expect(result["feedbackIndexQueriesScoped"]).toBe(true);
+ expect(result["listingRequestedProbes"]).toBe(1);
+ expect(result["matchingRequestedProbes"]).toBe(1);
+ expect(result["matchingUnrelatedProbes"]).toBe(0);
+ expect(result["matchingUnrelatedOrigins"]).toBe(0);
+ expect(result["indexingMaxProbes"]).toBe(1);
+ expect(result["feedbackMaxProbes"]).toBe(1);
+ expect(result["legacyStored"]).toBe("repository-current");
+ expect(result["changedStored"]).toBe("repository-previous");
+});
+
+test.each(["tied", "precise"])(
+ "orders completed history by database visibility across legacy and current writers (%s)",
+ async (precision) => {
+ const result = await run("completion-order", precision);
+ const precise = precision === "precise";
+
+ expect(result["legacy"]).toEqual({
+ "visible-first": null,
+ "visible-last": null,
+ "legacy-missing": 1,
+ "legacy-a": precise ? 3 : 2,
+ "legacy-b": precise ? 2 : 3,
+ });
+ expect(result["legacyGenerationsNull"]).toBe(true);
+ expect(result["firstPredecessors"]).not.toContain("visible-last");
+ expect(result["lastPredecessors"]).toContain("visible-first");
+ expect(result["reciprocalPredecessors"]).not.toContain("visible-last");
+ expect(result["confirmed"]).toEqual({
+ "first-finding": false,
+ "last-finding": true,
+ });
+ expect(result["sequences"]).toEqual({
+ "legacy-missing": 1,
+ "legacy-a": precise ? 3 : 2,
+ "legacy-b": precise ? 2 : 3,
+ "visible-first": 4,
+ "visible-last": 5,
+ "inserted-complete": 6,
+ });
+ expect(result["idempotent"]).toBe(true);
+ expect(result["sequenceOutranksFallback"]).toBe(true);
+ expect(result["sealedTimes"]).toEqual({
+ "legacy-a": precise
+ ? "2026-07-31t04:00:00.0000000002z"
+ : "2026-07-31t04:00:00z",
+ "legacy-b": precise
+ ? "2026-07-31T05:00:00.0000000001+01:00"
+ : "2026-07-31T05:00:00+01:00",
+ "visible-first": "2026-08-01T04:00:00Z",
+ "visible-last": "2026-08-01T03:00:00Z",
+ });
+ },
+);
+
+test("confirms findings against the latest selected scan without merging legacy groups", async () => {
+ expect(await run("selected-latest")).toEqual({
+ selected: {
+ "legacy:legacy-only": [false, "open", 1],
+ "legacy:same-key": [false, "closed", 1],
+ "bound:bound-only": [true, "open", 1],
+ "bound:same-key": [true, "open", 1],
+ },
+ unscoped: {
+ "legacy:legacy-only": true,
+ "legacy:same-key": true,
+ "bound:bound-only": true,
+ "bound:same-key": true,
+ "other:other-only": true,
+ },
+ });
+});
+
+test("restores only an unclaimed empty scan output under the workbench writer lock", async () => {
+ const result = await run("archive-recovery");
+ const cases = result["cases"] as Record<string, Record<string, unknown>>;
+ expect(result["parserAccepts"]).toEqual([true, true, false, false]);
+ for (const name of ["recorded", "unrecorded", "missingOriginal"]) {
+ expect(cases[name]).toMatchObject({
+ disposition: "restored",
+ operations: name === "missingOriginal" ? ["rename"] : ["rmdir", "rename"],
+ originalPresent: true,
+ archivePresent: false,
+ });
+ }
+ expect(cases["committed"]).toMatchObject({
+ disposition: "already-recorded",
+ operations: [],
+ owners: {
+ [String(result["scanDir"])]: "new",
+ [String(result["archiveDir"])]: "previous",
+ },
+ });
+ for (const name of ["changedOwner", "unexpectedOwner"]) {
+ expect(cases[name]).toMatchObject({
+ disposition: "ownership-changed",
+ operations: [],
+ originalPresent: true,
+ archivePresent: true,
+ });
+ }
+ for (const name of ["nonempty", "invalidSibling", "invalidDirectory"]) {
+ expect(cases[name]).toMatchObject({
+ disposition: "error",
+ operations: [],
+ originalPresent: true,
+ archivePresent: true,
+ });
+ }
+ expect(cases["renameFailure"]).toMatchObject({
+ disposition: "error",
+ operations: ["rmdir"],
+ archivePresent: true,
+ });
+ for (const value of Object.values(cases)) {
+ expect(value).toMatchObject({ locked: true, transactionClosed: true });
+ }
+});
+
+test("rechecks scan output inside registration after owner and parent validation", async () => {
+ const result = (await run("archive-registration")) as Record<
+ string,
+ {
+ accepted: boolean;
+ events: Array<[string, boolean]>;
+ transactionClosed: boolean;
+ }
+ >;
+ expect(result["valid"]).toEqual({
+ accepted: true,
+ events: [
+ ["canonical", false],
+ ["empty", false],
+ ["owner", true],
+ ["parent", true],
+ ["canonical", true],
+ ["empty", true],
+ ["archive", true],
+ ],
+ transactionClosed: true,
+ });
+ for (const name of [
+ "owner-refused",
+ "parent-refused",
+ "nonempty",
+ "noncanonical",
+ ]) {
+ expect(result[name]?.accepted).toBe(false);
+ expect(result[name]?.events.some(([event]) => event === "archive")).toBe(
+ false,
+ );
+ expect(result[name]?.transactionClosed).toBe(true);
+ }
+});
+
+test("keeps authenticated historical aliases visible without trusting a replacement checkout", async () => {
+ const result = await run("persisted-alias");
+
+ expect(result["scans"]).toEqual([
+ "legacy-scan",
+ "requested-scan",
+ "reused-scan",
+ ]);
+ expect(result["replacementRequest"]).toEqual([]);
+ expect(result["findings"]).toEqual(["current-finding", "historical-finding"]);
+ expect(result["freshFindings"]).toEqual([
+ "current-finding",
+ "historical-finding",
+ ]);
+ expect(result["replacementFindings"]).toEqual([]);
+ expect(result["emptyFindings"]).toEqual([]);
+ expect(result["projectionAvailable"]).toEqual({
+ fresh: true,
+ empty: true,
+ replacementPath: false,
+ replacementId: false,
+ unknownId: true,
+ });
+ expect(result["rejectsBothSelectors"]).toBe(true);
+ expect(result["readOnly"]).toBe(true);
+ expect(result["feedback"]).toEqual(["historical-finding"]);
+ expect(result["matchingCount"]).toBe(3);
+ expect(result["reusedListingProbes"]).toBe(0);
+ expect(result["stored"]).toBe("repository-current");
+});
+
+test.each(["migration", "migration-recorded31"])(
+ "upgrades only independently verified pre-release repository hashes (%s)",
+ async (scenario) => {
+ const result = await run(scenario);
+
+ expect(result["identities"]).toEqual(result["expected"]);
+ expect(result["startingVersion"]).toBe(
+ scenario === "migration-recorded31" ? 31 : 30,
+ );
+ expect(result["normalizationCount"]).toBe(1);
+ expect(result["normalizationTransactions"]).toEqual([true]);
+ expect(result["currentOpenProbes"]).toBe(0);
+ expect(result["recordsPreserved"]).toBe(true);
+ expect(result["scanGenerationsUnbound"]).toBe(true);
+ expect(result["idempotent"]).toBe(true);
+ expect(result["foldedLegacyScopesEqual"]).toBe(true);
+ expect(result["currentScopesDistinct"]).toBe(true);
+ expect(result["targetCount"]).toBe(12);
+ expect(result["migrations"]).toEqual(
+ Array.from({ length: 71 }, (_, index) => index + 1),
+ );
+ },
+);
+
+test("keeps unproved legacy history unbound and rejects newer or indeterminate owners", async () => {
+ const result = await run("null-history");
+
+ expect(result["sameCheckoutMetadata"]).toBe(true);
+ expect(result["stored"]).toEqual({
+ unchanged: null,
+ newer: null,
+ "invalid-time": null,
+ "no-history": "current-no-history",
+ "replacement-alias": "current-newer",
+ });
+ expect(result["registrationErrors"]).toEqual(["invalid-time", "newer"]);
+ expect(result["scans"]).toEqual(["replacement-alias-scan"]);
+});
+
+test("binds late NULL identities only when the selected target is registered", async () => {
+ const result = await run("late-null");
+
+ expect(result["stored"]).toEqual({
+ requested: "repository-current",
+ unscanned: "repository-current",
+ unselected: null,
+ historical: null,
+ });
+ expect(result["fullBackfillCalls"]).toBe(0);
+ expect(result["maintenanceProbes"]).toEqual({
+ requested: 0,
+ unscanned: 0,
+ unselected: 0,
+ historical: 0,
+ });
+ expect(result["selectedProbes"]).toEqual({
+ requested: 0,
+ unscanned: 2,
+ unselected: 0,
+ historical: 0,
+ });
+ expect(result["selectedRegistrationId"]).toBe("unscanned");
+ expect(result["selectedGuarded"]).toBe(true);
+ expect(result["historicalRegistrationId"]).toBe("historical");
+ expect(result["scans"]).toEqual(["established-scan", "requested-scan"]);
+ expect(result["historicalExact"]).toEqual(["historical-scan"]);
+ expect(result["feedback"]).toEqual([]);
+});
+
+test("guards identity binding at the database write and rechecks lost bindings", async () => {
+ const result = await run("binding");
+
+ expect(result["guarded"]).toEqual({
+ idHistory: false,
+ pathHistory: false,
+ wrongPath: false,
+ alreadyBound: false,
+ eligible: true,
+ repeat: false,
+ });
+ expect(result["stored"]).toEqual({
+ eligible: "repository-current",
+ "id-history": null,
+ "path-history": null,
+ donor: null,
+ bound: "repository-bound",
+ });
+ expect(result["ignoredInsert"]).toEqual({
+ id: "actual-target",
+ reselected: true,
+ unbound: true,
+ insertsNull: true,
+ });
+ expect(result["lostToHistory"]).toEqual({
+ accepted: true,
+ rechecked: true,
+ guarded: true,
+ });
+ expect(result["lostToConflictingIdentity"]).toEqual({
+ accepted: false,
+ rechecked: true,
+ guarded: true,
+ });
+});
+
+test("compares sealed history from persisted evidence without weakening automatic requesters", async () => {
+ const result = await run("sealed-comparison");
+
+ expect(result["accepted"]).toEqual([true, true]);
+ expect(result["conflictingIdentitiesAccepted"]).toBe(false);
+ expect(result["healthyTargetless"]).toBe(true);
+ expect(result["refusedTargetless"]).toBe(false);
+ expect(result["savedCount"]).toBe(2);
+ expect(result["unregisteredAutomaticRequesterRejected"]).toBe(true);
+});
+
+test("admits rerun lineage only through the verified repository and exact scope", async () => {
+ const result = await run("lineage");
+
+ expect(result["accepted"]).toEqual({
+ requested: true,
+ removed: true,
+ legacy: false,
+ unverified: false,
+ clone: false,
+ scope: false,
+ changed: false,
+ });
+ expect(result["scanCount"]).toBe(7);
+});
+
+test("revalidates saved workspace identity inside the scan-start transaction", async () => {
+ const result = await run("saved-start");
+
+ expect(result["accepted"]).toEqual({
+ valid: true,
+ "changed-generation": false,
+ "changed-saved-id": false,
+ });
+ expect(result["verifiedInsideTransaction"]).toBe(true);
+ expect(result["scanCount"]).toBe(1);
+ expect(result["stored"]).toBe("repository-current");
+});
+
+test("checks current ownership before rejoining saved scan tasks", async () => {
+ const result = await run("scan-reuse");
+
+ expect(result["owners"]).toEqual({
+ current: true,
+ legacy: true,
+ refused: false,
+ contradictory: false,
+ missing: false,
+ });
+ expect(result["reuse"]).toEqual({
+ current: {
+ workspace: { accepted: true, createdRun: false },
+ prompt: { accepted: true, createdRun: false },
+ headless: { accepted: true, createdRun: false },
+ "deep-direct": { accepted: true, createdRun: true },
+ "deep-transaction": { accepted: true, createdRun: true },
+ "deep-terminal": { accepted: true, createdRun: false },
+ },
+ refused: Object.fromEntries(
+ [
+ "workspace",
+ "prompt",
+ "headless",
+ "deep-direct",
+ "deep-transaction",
+ "deep-terminal",
+ ].map((kind) => [kind, { accepted: false, createdRun: false }]),
+ ),
+ });
+ expect(result["legacyGeneration"]).toBeNull();
+});
+
+test("quarantines unproved public-v30 bindings without discarding historical records", async () => {
+ const result = await run("v30-current");
+
+ expect(result["stored"]).toEqual({
+ "current-owner": "current-generation",
+ "old-owner": null,
+ "removed-old": null,
+ "removed-valid": "current-generation",
+ "invalid-time": null,
+ "unverified-anchor": null,
+ "opaque-missing": null,
+ "valid-scope": "scope-current",
+ });
+ expect(result["recordsPreserved"]).toBe(true);
+ expect(result["targetIdsPreserved"]).toBe(true);
+ expect(result["removedExact"]).toEqual(["removed-old-scan"]);
+ expect(result["visibleFindings"]).toEqual(["current-owner-finding"]);
+ expect(result["oldRegistrationRejected"]).toBe(true);
+});
diff --git a/sdk/typescript/tests-ts/workbench-repository-identity.test.ts b/sdk/typescript/tests-ts/workbench-repository-identity.test.ts
new file mode 100644
index 000000000..8d58cf760
--- /dev/null
+++ b/sdk/typescript/tests-ts/workbench-repository-identity.test.ts
@@ -0,0 +1,938 @@
+import { execFileSync, spawnSync } from "node:child_process";
+import {
+ mkdirSync,
+ mkdtempSync,
+ realpathSync,
+ rmSync,
+ writeFileSync,
+} from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, describe, expect, test } from "bun:test";
+import { PLUGIN_ROOT } from "./plugin-root.js";
+
+const temporaryRoots: string[] = [];
+const remote =
+ "https://fixture-user:[email protected]/acme/project.git";
+
+afterEach(() => {
+ for (const root of temporaryRoots.splice(0)) {
+ rmSync(root, { recursive: true, force: true });
+ }
+});
+
+function git(repository: string, ...args: string[]): string {
+ return execFileSync(
+ "git",
+ [
+ "-c",
+ "user.name=Fixture",
+ "-c",
+ ...args,
+ ],
+ { cwd: repository, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] },
+ ).trim();
+}
+
+function fixture(): {
+ root: string;
+ repository: string;
+ worktree: string;
+ clone: string;
+} {
+ const root = realpathSync(
+ mkdtempSync(join(tmpdir(), "codex-security-repository-identity-")),
+ );
+ temporaryRoots.push(root);
+ const repository = join(root, "repository");
+ const worktree = join(root, "linked-worktree");
+ const clone = join(root, "same-origin-clone");
+ mkdirSync(repository);
+ git(repository, "init", "-q");
+ for (const service of ["service-a", "service-b", "MixedCase"]) {
+ mkdirSync(join(repository, service));
+ writeFileSync(join(repository, service, "service.py"), "value = 1\n");
+ }
+ git(repository, "add", ".");
+ git(repository, "commit", "-qm", "fixture");
+ git(repository, "remote", "add", "origin", remote);
+ git(repository, "worktree", "add", "--detach", "-q", worktree, "HEAD");
+ execFileSync("git", ["clone", "-q", repository, clone], {
+ stdio: ["ignore", "pipe", "pipe"],
+ });
+ git(clone, "remote", "set-url", "origin", remote);
+ return { root, repository, worktree, clone };
+}
+
+const probe = String.raw`
+import argparse
+import json
+import os
+import shutil
+import sqlite3
+import subprocess
+import sys
+from dataclasses import replace
+from datetime import datetime, timezone
+from pathlib import Path
+from types import SimpleNamespace
+from unittest.mock import patch
+
+sys.path.insert(0, sys.argv[1])
+
+import workbench_scan_history as history
+from filesystem_identity import serialize_filesystem_identity
+from workbench_schema import MIGRATIONS, apply_migrations
+from workbench_target_state import (
+ _repository_birth_time_ns,
+ _repository_identity_details,
+ backfill_repository_identities,
+ backfill_security_targets,
+ ensure_security_target,
+ register_security_target,
+ repository_identity,
+ repository_relative_path,
+ stable_target_id,
+)
+
+scenario = sys.argv[2]
+root, repository, worktree, clone = map(Path, sys.argv[3:7])
+timestamp = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
+connection = sqlite3.connect(":memory:")
+connection.row_factory = sqlite3.Row
+connection.execute("PRAGMA foreign_keys = ON")
+apply_migrations(connection, MIGRATIONS, lambda: timestamp, backfill_security_targets)
+
+
+def git(target, *args):
+ return subprocess.run(
+ [
+ "git",
+ "-c", "user.name=Fixture",
+ "-C", str(target),
+ *args,
+ ],
+ check=True,
+ capture_output=True,
+ text=True,
+ ).stdout.strip()
+
+
+def add_scan(scan_id, target, ownership="current", verify_ownership=True):
+ target = Path(target)
+ registration = register_security_target(
+ connection, str(target), verify_ownership=verify_ownership
+ )
+ target_id = registration.target_id
+ metadata = target.stat()
+ device = serialize_filesystem_identity(metadata.st_dev)
+ inode = serialize_filesystem_identity(metadata.st_ino)
+ if ownership == "missing":
+ device, inode = None, None
+ elif ownership == "malformed":
+ inode = None
+ elif ownership == "mismatch":
+ inode = serialize_filesystem_identity(metadata.st_ino + 1)
+
+ workspace_id = f"workspace-{scan_id}"
+ connection.execute(
+ "INSERT INTO workspaces (id, target_path, target_id, created_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?)",
+ (workspace_id, str(target), target_id, timestamp, timestamp),
+ )
+ connection.execute(
+ "INSERT INTO scans (id, workspace_id, target_path, target_id, repository_generation, target_device, "
+ "target_inode, target_revision, scope, mode, scan_dir, status, phase, "
+ "started_at, created_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+ (
+ scan_id, workspace_id, str(target), target_id, registration.repository_generation, device, inode,
+ "synthetic-revision", ".", "standard", str(root / "scans" / scan_id),
+ "complete", "reporting", timestamp, timestamp, timestamp,
+ ),
+ )
+ connection.execute(
+ "INSERT INTO scan_progress (scan_id, updated_at) VALUES (?, ?)",
+ (scan_id, timestamp),
+ )
+ return target_id
+
+
+def listed(target):
+ arguments = argparse.Namespace(
+ repository=str(target),
+ scan_root=None,
+ target_id=None,
+ mode=None,
+ status=None,
+ query=None,
+ limit=None,
+ offset=0,
+ )
+ return sorted(scan["scanId"] for scan in history.list_scans(connection, arguments)["scans"])
+
+
+def forged_worktree():
+ forged = root / "forged-git-pointer"
+ forged.mkdir()
+ common = git(repository, "rev-parse", "--path-format=absolute", "--git-common-dir")
+ (forged / ".git").write_text(f"gitdir: {common}\n")
+ return forged
+
+
+if scenario == "identity":
+ targets = {
+ "repository": repository,
+ "worktree": worktree,
+ "serviceA": repository / "service-a",
+ "worktreeServiceA": worktree / "service-a",
+ "serviceB": repository / "service-b",
+ "mixedCase": repository / "MixedCase",
+ "worktreeMixedCase": worktree / "MixedCase",
+ "clone": clone,
+ }
+ target_ids = {
+ name: ensure_security_target(connection, str(target))
+ for name, target in targets.items()
+ }
+ identities = {
+ name: repository_identity(target)
+ for name, target in targets.items()
+ }
+ common = os.path.realpath(git(
+ repository, "rev-parse", "--path-format=absolute", "--git-common-dir"
+ ))
+ forged_identity = repository_identity(forged_worktree())
+ (repository / "service-a" / "service.py").write_text("value = 2\n")
+ git(repository, "add", ".")
+ git(repository, "commit", "-qm", "ordinary update")
+ commit_independent = repository_identity(repository) == identities["repository"]
+ additional_worktree = root / "additional-worktree"
+ git(repository, "worktree", "add", "--detach", "-q", str(additional_worktree), "HEAD")
+ worktree_independent = repository_identity(repository) == identities["repository"]
+ git(repository, "worktree", "remove", "--force", str(additional_worktree))
+ git(
+ repository,
+ "remote", "set-url", "origin",
+ "https://different-user:[email protected]/other/repo.git",
+ )
+ remote_independent = repository_identity(repository) == identities["repository"]
+ description = Path(common) / "description"
+ description.write_text("An ordinary user-edited Git description.\n")
+ description_edit_independent = repository_identity(repository) == identities["repository"]
+ os.chmod(description, 0o400)
+ description_mode_independent = repository_identity(repository) == identities["repository"]
+ os.chmod(description, 0o600)
+ description.unlink()
+ description_absence_independent = repository_identity(repository) == identities["repository"]
+ custom_template = root / "custom-template"
+ custom_template.mkdir()
+ git(custom_template, "init", "-q", "--template=")
+ markerless_repository_identity = repository_identity(custom_template)
+ with patch("workbench_target_state._repository_birth_time_ns", side_effect=(41, 100, 42, 100)):
+ recycled_inode_distinguished = (
+ repository_identity(repository) != repository_identity(repository)
+ )
+ original_normcase = os.path.normcase
+ os.path.normcase = lambda path: os.fspath(path).lower()
+ try:
+ case_preserved = (
+ repository_relative_path(repository / "MixedCase") == "MixedCase"
+ and repository_identity(repository / "MixedCase") == identities["mixedCase"]
+ )
+ finally:
+ os.path.normcase = original_normcase
+
+ legacy_metadata = SimpleNamespace(st_ctime_ns=41)
+ modern_metadata = SimpleNamespace(st_birthtime_ns=43, st_ctime_ns=41)
+ with patch.object(os, "name", "nt"):
+ windows_legacy_birth_time = _repository_birth_time_ns(common, legacy_metadata)
+ windows_modern_birth_time = _repository_birth_time_ns(common, modern_metadata)
+ linux_birth_times = {}
+ with patch.object(sys, "platform", "linux"), patch.object(os, "name", "posix"):
+ for label, value in (("native", 42_000_000_123), ("unavailable", None)):
+ with patch("workbench_target_state._linux_repository_birth_time_ns", return_value=value), \
+ patch("subprocess.run") as command:
+ linux_birth_times[label] = _repository_birth_time_ns(common, legacy_metadata)
+ assert not command.called
+ print(json.dumps({
+ "identities": identities,
+ "forgedIdentity": forged_identity,
+ "commitIndependent": commit_independent,
+ "worktreeIndependent": worktree_independent,
+ "remoteIndependent": remote_independent,
+ "descriptionEditIndependent": description_edit_independent,
+ "descriptionModeIndependent": description_mode_independent,
+ "descriptionAbsenceIndependent": description_absence_independent,
+ "markerlessRepositoryIdentity": markerless_repository_identity,
+ "recycledInodeDistinguished": recycled_inode_distinguished,
+ "casePreserved": case_preserved,
+ "windowsLegacyBirthTime": windows_legacy_birth_time,
+ "windowsModernBirthTime": windows_modern_birth_time,
+ "linuxBirthTimes": linux_birth_times,
+ "targetIdsPreserved": all(
+ target_ids[name] == stable_target_id(target)
+ for name, target in targets.items()
+ ),
+ "stored": {
+ row["current_path"]: row["repository_identity"]
+ for row in connection.execute(
+ "SELECT current_path, repository_identity FROM security_targets"
+ )
+ },
+ }))
+
+elif scenario == "native-unavailable":
+ import ctypes
+ import workbench_target_state as target_state
+
+ targets = (repository, worktree)
+ for name, target in zip(("primary", "linked"), targets):
+ add_scan(name, target)
+ before = [listed(target) for target in targets]
+ stored = list(connection.execute("SELECT * FROM security_targets"))
+ marker = root / "repository-stat-ran"
+ observations = []
+ for target in targets:
+ tools = target / "tools"
+ tools.mkdir()
+ executable = tools / "stat"
+ executable.write_text(
+ '#!/bin/sh\nprintf invoked > "$SYNTHETIC_STAT_MARKER"\n'
+ 'printf "42.000000123\\n"\n'
+ )
+ executable.chmod(0o755)
+ with patch.dict(os.environ, {
+ "PATH": str(tools) + os.pathsep + os.environ["PATH"],
+ "SYNTHETIC_STAT_MARKER": str(marker),
+ }), patch.object(ctypes, "CDLL", side_effect=OSError("unavailable native library")):
+ cached = target_state.RepositoryIdentityCache(connection).for_path(str(target))
+ observations.append({
+ "executed": marker.exists(),
+ "identity": cached.live_identity,
+ "ownerMatches": cached.ownership_matches,
+ "visible": listed(target),
+ })
+ marker.unlink(missing_ok=True)
+ print(json.dumps({
+ "before": before,
+ "unavailable": observations,
+ "restored": [listed(target) for target in targets],
+ "storedUnchanged": list(connection.execute("SELECT * FROM security_targets")) == stored,
+ }))
+
+elif scenario == "history":
+ add_scan("canonical-root", repository)
+ add_scan("linked-root", worktree)
+ add_scan("spoof-root", clone)
+ add_scan("canonical-a", repository / "service-a")
+ add_scan("linked-a", worktree / "service-a")
+ add_scan("canonical-b", repository / "service-b")
+ add_scan("spoof-a", clone / "service-a")
+ add_scan("forged-root", forged_worktree())
+ before = {
+ "root": listed(repository),
+ "serviceA": listed(repository / "service-a"),
+ "serviceB": listed(repository / "service-b"),
+ }
+ git(repository, "worktree", "remove", "--force", str(worktree))
+ after = {
+ "root": listed(repository),
+ "serviceA": listed(repository / "service-a"),
+ }
+ arguments = argparse.Namespace(repository=str(repository), force=False)
+ matched = history.list_unmatched_scan_pairs(
+ connection,
+ arguments,
+ backfill_finding_details=lambda _connection, _scan: None,
+ read_coverage=lambda _scan: {},
+ )
+
+ def unavailable(scan):
+ if scan["id"] == "linked-root":
+ raise SystemExit("Saved scan artifacts are unavailable.")
+ return {}
+
+ unavailable_matches = history.list_unmatched_scan_pairs(
+ connection,
+ arguments,
+ backfill_finding_details=lambda _connection, _scan: None,
+ read_coverage=unavailable,
+ )
+
+ def require_scan(database, scan_id):
+ return database.execute("SELECT * FROM scans WHERE id = ?", (scan_id,)).fetchone()
+
+ comparison_args = argparse.Namespace(
+ before_scan_id="canonical-root",
+ after_scan_id="linked-root",
+ matches_json_stdin=False,
+ matches_json=json.dumps({"matches": [], "uncertain": []}),
+ )
+ compared = history.compare_scans(
+ connection,
+ comparison_args,
+ require_scan=require_scan,
+ read_coverage=lambda _scan: {"completeness": "complete"},
+ )
+ connection.commit()
+ saved = history.save_scan_comparison(
+ connection,
+ comparison_args,
+ now=lambda: timestamp,
+ require_scan=require_scan,
+ read_coverage=lambda _scan: {"completeness": "complete"},
+ )
+ explicit_clone = history.compare_scans(
+ connection,
+ argparse.Namespace(before_scan_id="canonical-root", after_scan_id="spoof-root"),
+ require_scan=require_scan,
+ read_coverage=lambda _scan: {"completeness": "complete"},
+ )
+
+ print(json.dumps({
+ "before": before,
+ "after": after,
+ "matchedScanCount": matched["scanCount"],
+ "matchingBatches": len(matched["batches"]),
+ "unavailableScans": unavailable_matches["unavailableScans"],
+ "compared": compared["beforeScanId"] == "canonical-root"
+ and compared["afterScanId"] == "linked-root",
+ "saved": saved["beforeScanId"] == "canonical-root"
+ and saved["afterScanId"] == "linked-root",
+ "explicitCloneCompared": explicit_clone["afterScanId"] == "spoof-root",
+ }))
+
+elif scenario == "backfill":
+ add_scan("valid", repository)
+ add_scan("reused", clone, "mismatch")
+ add_scan("mixed-current", repository / "service-a")
+ add_scan("mixed-previous", repository / "service-a", "mismatch")
+ add_scan("malformed", repository / "service-b", "malformed")
+ add_scan("missing", worktree)
+ missing_path = str(worktree)
+ git(repository, "worktree", "remove", "--force", missing_path)
+ connection.execute("UPDATE security_targets SET repository_identity = NULL")
+ before_ids = {
+ row["current_path"]: row["id"]
+ for row in connection.execute("SELECT id, current_path FROM security_targets")
+ }
+ backfill_repository_identities(connection)
+ rows = {
+ row["current_path"]: {"id": row["id"], "identity": row["repository_identity"]}
+ for row in connection.execute(
+ "SELECT id, current_path, repository_identity FROM security_targets"
+ )
+ }
+ print(json.dumps({
+ "valid": rows[str(repository)]["identity"],
+ "reused": rows[str(clone)]["identity"],
+ "mixed": rows[str(repository / "service-a")]["identity"],
+ "malformed": rows[str(repository / "service-b")]["identity"],
+ "missing": rows[missing_path]["identity"],
+ "idsPreserved": all(rows[path]["id"] == target_id for path, target_id in before_ids.items()),
+ }))
+
+elif scenario == "replacement":
+ add_scan("previous-owner", worktree)
+ add_scan("trusted-alias", repository)
+ git(repository, "worktree", "remove", "--force", str(worktree))
+ worktree.mkdir()
+ git(worktree, "init", "-q")
+ (worktree / "replacement.py").write_text("value = 2\n")
+ git(worktree, "add", ".")
+ git(worktree, "commit", "-qm", "replacement")
+ git(worktree, "remote", "add", "origin", sys.argv[7])
+ before = listed(worktree)
+ try:
+ add_scan("replacement-owner", worktree)
+ except SystemExit as error:
+ registration_error = str(error)
+ else:
+ registration_error = None
+ try:
+ history.list_unmatched_scan_pairs(
+ connection,
+ argparse.Namespace(repository=str(worktree), force=False),
+ backfill_finding_details=lambda _connection, _scan: None,
+ read_coverage=lambda _scan: {},
+ )
+ except SystemExit as error:
+ matching_error = str(error)
+ else:
+ matching_error = None
+ after = listed(worktree)
+
+ empty_target = clone / "service-a"
+ ensure_security_target(connection, str(empty_target))
+ connection.execute(
+ "UPDATE security_targets SET repository_identity = ? WHERE current_path = ?",
+ (repository_identity(repository), str(empty_target)),
+ )
+ try:
+ ensure_security_target(connection, str(empty_target))
+ except SystemExit as error:
+ empty_target_error = str(error)
+ else:
+ empty_target_error = None
+
+ explicit_clone_id = add_scan("explicit-clone", clone)
+ connection.execute(
+ "UPDATE security_targets SET repository_identity = ? WHERE id = ?",
+ (repository_identity(repository), explicit_clone_id),
+ )
+ try:
+ ensure_security_target(connection, str(clone))
+ except SystemExit as error:
+ explicit_clone_error = str(error)
+ else:
+ explicit_clone_error = None
+
+ unverified_target = clone / "service-b"
+ add_scan("unverified-owner", unverified_target, "missing")
+ try:
+ ensure_security_target(connection, str(unverified_target))
+ except SystemExit as error:
+ unverified_owner_error = str(error)
+ else:
+ unverified_owner_error = None
+ print(json.dumps({
+ "before": before,
+ "after": after,
+ "registrationError": registration_error,
+ "matchingError": matching_error,
+ "emptyTargetError": empty_target_error,
+ "explicitCloneError": explicit_clone_error,
+ "unverifiedOwnerError": unverified_owner_error,
+ "replacementScanCreated": connection.execute(
+ "SELECT 1 FROM scans WHERE id = 'replacement-owner'"
+ ).fetchone() is not None,
+ }))
+
+elif scenario == "recreated-directory":
+ target = repository / "service-a"
+ linked = worktree / "service-a"
+ target_id = add_scan("before-recreation", target)
+ add_scan("linked-scope", linked)
+ original_metadata = target.stat()
+ original_identity = repository_identity(target)
+ original_scan = connection.execute(
+ "SELECT target_device, target_inode FROM scans WHERE id = 'before-recreation'"
+ ).fetchone()
+
+ target.rename(root / "retired-directory-inode")
+ target.mkdir()
+ (target / "service.py").write_text("value = 2\n")
+ recreated_metadata = target.stat()
+ before_rescan = listed(target)
+ recreated_id = add_scan("after-recreation", target)
+ repeated_id = add_scan("repeated-rescan", target)
+ after_rescan = listed(target)
+
+ malformed_target = repository / "service-b"
+ add_scan("malformed-owner", malformed_target, "malformed")
+ try:
+ ensure_security_target(connection, str(malformed_target))
+ except SystemExit as error:
+ malformed_owner_error = str(error)
+ else:
+ malformed_owner_error = None
+
+ plain = root / "recreated-nongit"
+ plain.mkdir()
+ add_scan("plain-before-recreation", plain)
+ plain.rename(root / "retired-nongit-inode")
+ plain.mkdir()
+ try:
+ ensure_security_target(connection, str(plain))
+ except SystemExit as error:
+ nongit_owner_error = str(error)
+ else:
+ nongit_owner_error = None
+
+ add_scan("linked-root-before", worktree)
+ add_scan("canonical-root-alias", repository)
+ root_identity = repository_identity(worktree)
+ root_metadata = worktree.stat()
+ git_pointer = (worktree / ".git").read_text()
+ worktree.rename(root / "retired-worktree-root-inode")
+ worktree.mkdir()
+ (worktree / ".git").write_text(git_pointer)
+ try:
+ ensure_security_target(connection, str(worktree))
+ except SystemExit as error:
+ recreated_root_error = str(error)
+ else:
+ recreated_root_error = None
+
+ preserved_scan = connection.execute(
+ "SELECT target_device, target_inode FROM scans WHERE id = 'before-recreation'"
+ ).fetchone()
+ print(json.dumps({
+ "ownerChanged": (
+ original_metadata.st_dev != recreated_metadata.st_dev
+ or original_metadata.st_ino != recreated_metadata.st_ino
+ ),
+ "identityPreserved": repository_identity(target) == original_identity,
+ "targetIdPreserved": recreated_id == target_id and repeated_id == target_id,
+ "historicalOwnerPreserved": (
+ preserved_scan["target_device"] == original_scan["target_device"]
+ and preserved_scan["target_inode"] == original_scan["target_inode"]
+ ),
+ "beforeRescan": before_rescan,
+ "afterRescan": after_rescan,
+ "malformedOwnerError": malformed_owner_error,
+ "nongitOwnerError": nongit_owner_error,
+ "rootOwnerChanged": root_metadata.st_ino != worktree.stat().st_ino,
+ "rootIdentityPreserved": repository_identity(worktree) == root_identity,
+ "recreatedRootError": recreated_root_error,
+ "recreatedRootScans": listed(worktree),
+ }))
+
+elif scenario == "candidate-generation":
+ add_scan("requested-scan", repository)
+ legacy_id = add_scan("trusted-alias", worktree)
+ candidate_id = add_scan("previous-generation", clone)
+ requested_identity = repository_identity(repository)
+ candidate_identity = repository_identity(clone)
+
+ def live_identity(target):
+ details = _repository_identity_details(target)
+ return replace(details, value=requested_identity) if Path(target) == clone else details
+
+ def automatic_history():
+ matching = history.list_unmatched_scan_pairs(
+ connection,
+ argparse.Namespace(repository=str(repository), force=False),
+ backfill_finding_details=lambda _connection, _scan: None,
+ read_coverage=lambda _scan: {},
+ )
+ matching_ids = {
+ batch["afterScanId"] for batch in matching["batches"]
+ } | {
+ scan["scanId"]
+ for batch in matching["batches"]
+ for scan in batch["beforeScans"]
+ }
+ return {
+ "scans": listed(repository),
+ "matchingScanCount": matching["scanCount"],
+ "matchingScanIds": sorted(matching_ids),
+ }
+
+ with patch("workbench_target_state._repository_identity_details", side_effect=live_identity):
+ persisted = automatic_history()
+ connection.execute(
+ "UPDATE security_targets SET repository_identity = NULL WHERE id = ?",
+ (legacy_id,),
+ )
+ connection.execute(
+ "UPDATE scans SET repository_generation = NULL WHERE id = ?", ("trusted-alias",)
+ )
+ verified_legacy = automatic_history()
+ connection.execute(
+ "UPDATE scans SET target_device = NULL, target_inode = NULL WHERE id = ?",
+ ("trusted-alias",),
+ )
+ unverified_legacy = automatic_history()
+
+ print(json.dumps({
+ "persisted": persisted,
+ "verifiedLegacy": verified_legacy,
+ "unverifiedLegacy": unverified_legacy,
+ "candidateIdentityPreserved": connection.execute(
+ "SELECT repository_identity FROM security_targets WHERE id = ?",
+ (candidate_id,),
+ ).fetchone()[0] == candidate_identity,
+ }))
+
+elif scenario == "git-replacement":
+ add_scan("original-repository", repository)
+ add_scan("original-alias", worktree)
+ original_identity = repository_identity(repository)
+ original_metadata = repository.stat()
+
+ def remove_read_only(operation, path, _error):
+ os.chmod(path, 0o700)
+ operation(path)
+
+ shutil.rmtree(repository / ".git", onerror=remove_read_only)
+ git(repository, "init", "-q")
+ git(repository, "add", ".")
+ git(repository, "commit", "-qm", "replacement")
+ replacement_identity = repository_identity(repository)
+ try:
+ ensure_security_target(connection, str(repository))
+ except SystemExit as error:
+ registration_error = str(error)
+ else:
+ registration_error = None
+ replacement_metadata = repository.stat()
+ print(json.dumps({
+ "originalIdentity": original_identity,
+ "replacementIdentity": replacement_identity,
+ "checkoutOwnerUnchanged": (
+ original_metadata.st_dev == replacement_metadata.st_dev
+ and original_metadata.st_ino == replacement_metadata.st_ino
+ ),
+ "visibleScans": listed(repository),
+ "registrationError": registration_error,
+ }))
+
+elif scenario == "unverified-owner":
+ ensure_security_target(connection, str(repository))
+ add_scan("trusted-alias", worktree)
+ git(repository, "worktree", "remove", "--force", str(worktree))
+ before = listed(repository)
+ add_scan("unverified-owner", repository, "missing")
+ after = listed(repository)
+ print(json.dumps({
+ "before": before,
+ "after": after,
+ }))
+
+elif scenario == "nongit":
+ first = root / "plain-a"
+ second = root / "plain-b"
+ first.mkdir()
+ second.mkdir()
+ add_scan("plain-a", first)
+ add_scan("legacy-a", first, "missing")
+ add_scan("malformed-a", first, "malformed", verify_ownership=False)
+ add_scan("mismatched-a", first, "mismatch", verify_ownership=False)
+ add_scan("plain-b", second)
+ before = connection.execute(
+ "SELECT NULL AS target_id, ? AS target_path", (str(first),)
+ ).fetchone()
+ after = connection.execute(
+ "SELECT NULL AS target_id, ? AS target_path", (str(second),)
+ ).fetchone()
+ same = connection.execute(
+ "SELECT NULL AS target_id, ? AS target_path", (str(first),)
+ ).fetchone()
+ print(json.dumps({
+ "firstIdentity": repository_identity(first),
+ "secondIdentity": repository_identity(second),
+ "differentNullIdsMatch": history._same_repository(connection, before, after),
+ "sameNullPathMatches": history._same_repository(connection, before, same),
+ "firstScans": listed(first),
+ "secondScans": listed(second),
+ }))
+`;
+
+function runProbe(
+ scenario: string,
+ repositories: ReturnType<typeof fixture>,
+): Record<string, unknown> {
+ const python = Bun.which("python3") ?? Bun.which("python") ?? Bun.which("py");
+ expect(python).not.toBeNull();
+ if (python === null) throw new Error("A Python interpreter is required.");
+ const execution = spawnSync(
+ python,
+ [
+ "-I",
+ "-B",
+ "-c",
+ probe,
+ join(PLUGIN_ROOT, "scripts"),
+ scenario,
+ repositories.root,
+ repositories.repository,
+ repositories.worktree,
+ repositories.clone,
+ remote,
+ ],
+ { encoding: "utf8", timeout: 20_000 },
+ );
+ expect(execution.status, execution.stderr).toBe(0);
+ expect(execution.stderr).toBe("");
+ return JSON.parse(execution.stdout) as Record<string, unknown>;
+}
+
+describe("durable workbench repository identities", () => {
+ test("hashes Git common directories and repository-relative target scopes", () => {
+ const repositories = fixture();
+ const result = runProbe("identity", repositories);
+ const identities = result["identities"] as Record<string, string>;
+
+ const identityPattern =
+ process.platform === "linux"
+ ? /^repository_v3_sha256_[a-f0-9]{64}$/
+ : /^repository_sha256_[a-f0-9]{64}$/;
+ expect(identities["repository"]).toMatch(identityPattern);
+ expect(identities["worktree"]).toBe(identities["repository"]);
+ expect(identities["worktreeServiceA"]).toBe(identities["serviceA"]);
+ expect(identities["worktreeMixedCase"]).toBe(identities["mixedCase"]);
+ expect(identities["serviceA"]).not.toBe(identities["repository"]);
+ expect(identities["serviceA"]).not.toBe(identities["serviceB"]);
+ expect(identities["clone"]).not.toBe(identities["repository"]);
+ expect(result["forgedIdentity"]).toBeNull();
+ expect(result["commitIndependent"]).toBe(true);
+ expect(result["worktreeIndependent"]).toBe(true);
+ expect(result["remoteIndependent"]).toBe(true);
+ expect(result["descriptionEditIndependent"]).toBe(true);
+ expect(result["descriptionModeIndependent"]).toBe(true);
+ expect(result["descriptionAbsenceIndependent"]).toBe(true);
+ expect(result["markerlessRepositoryIdentity"]).toMatch(identityPattern);
+ expect(result["recycledInodeDistinguished"]).toBe(true);
+ expect(result["casePreserved"]).toBe(true);
+ expect(result["windowsLegacyBirthTime"]).toBe(41);
+ expect(result["windowsModernBirthTime"]).toBe(43);
+ expect(result["linuxBirthTimes"]).toEqual({
+ native: 42_000_000_123,
+ unavailable: null,
+ });
+ expect(result["targetIdsPreserved"]).toBe(true);
+ expect(JSON.stringify(result["stored"])).not.toContain(
+ "SYNTHETIC_PASSWORD",
+ );
+ }, 30_000);
+
+ test.skipIf(process.platform !== "linux")(
+ "unavailable native identity does not execute checkout tools or reuse saved aliases",
+ () => {
+ const result = runProbe("native-unavailable", fixture());
+ const history = [
+ ["linked", "primary"],
+ ["linked", "primary"],
+ ];
+ expect(result["before"]).toEqual(history);
+ expect(result["unavailable"]).toEqual([
+ { executed: false, identity: null, ownerMatches: false, visible: [] },
+ { executed: false, identity: null, ownerMatches: false, visible: [] },
+ ]);
+ expect(result["restored"]).toEqual(history);
+ expect(result["storedUnchanged"]).toBe(true);
+ },
+ 30_000,
+ );
+
+ test("retains removed worktrees while excluding spoofed origins and unrelated scopes", () => {
+ const result = runProbe("history", fixture());
+ const before = result["before"] as Record<string, string[]>;
+ const after = result["after"] as Record<string, string[]>;
+
+ expect(before["root"]).toEqual(["canonical-root", "linked-root"]);
+ expect(before["serviceA"]).toEqual(["canonical-a", "linked-a"]);
+ expect(before["serviceB"]).toEqual(["canonical-b"]);
+ expect(after["root"]).toEqual(["canonical-root", "linked-root"]);
+ expect(after["serviceA"]).toEqual(["canonical-a", "linked-a"]);
+ expect(result["matchedScanCount"]).toBe(2);
+ expect(result["matchingBatches"]).toBe(1);
+ expect(result["unavailableScans"]).toBe(1);
+ expect(result["compared"]).toBe(true);
+ expect(result["saved"]).toBe(true);
+ expect(result["explicitCloneCompared"]).toBe(true);
+ }, 30_000);
+
+ test("leaves unproved historical identities unbound and preserves every target ID", () => {
+ const result = runProbe("backfill", fixture());
+
+ expect(result["valid"]).toBeNull();
+ expect(result["reused"]).toBeNull();
+ expect(result["mixed"]).toBeNull();
+ expect(result["malformed"]).toBeNull();
+ expect(result["missing"]).toBeNull();
+ expect(result["idsPreserved"]).toBe(true);
+ }, 30_000);
+
+ test("does not expose a previous checkout owner or its trusted aliases", () => {
+ const result = runProbe("replacement", fixture());
+
+ expect(result["before"]).toEqual([]);
+ expect(result["after"]).toEqual([]);
+ expect(result["registrationError"]).toContain(
+ "refusing to reuse its target",
+ );
+ expect(result["matchingError"]).toContain("refusing to reuse its target");
+ expect(result["emptyTargetError"]).toBeNull();
+ expect(result["replacementScanCreated"]).toBe(false);
+ expect(result["explicitCloneError"]).toContain(
+ "refusing to reuse its target",
+ );
+ expect(result["unverifiedOwnerError"]).toContain(
+ "refusing to reuse its target",
+ );
+ }, 30_000);
+
+ test("rejects an in-place Git directory replacement under the same checkout", () => {
+ const result = runProbe("git-replacement", fixture());
+
+ expect(result["checkoutOwnerUnchanged"]).toBe(true);
+ expect(result["replacementIdentity"]).not.toBe(result["originalIdentity"]);
+ expect(result["visibleScans"]).toEqual([]);
+ expect(result["registrationError"]).toContain(
+ "refusing to reuse its target",
+ );
+ }, 30_000);
+
+ test("does not discover historical targets through a conflicting live generation", () => {
+ const result = runProbe("candidate-generation", fixture());
+ const verified = {
+ scans: ["requested-scan", "trusted-alias"],
+ matchingScanCount: 2,
+ matchingScanIds: ["requested-scan", "trusted-alias"],
+ };
+
+ expect(result["persisted"]).toEqual(verified);
+ const isolated = {
+ scans: ["requested-scan"],
+ matchingScanCount: 1,
+ matchingScanIds: [],
+ };
+ expect(result["verifiedLegacy"]).toEqual(isolated);
+ expect(result["unverifiedLegacy"]).toEqual(isolated);
+ expect(result["candidateIdentityPreserved"]).toBe(true);
+ }, 30_000);
+
+ test("rescans recreated directories when their Git repository and scope are unchanged", () => {
+ const result = runProbe("recreated-directory", fixture());
+
+ expect(result["ownerChanged"]).toBe(true);
+ expect(result["identityPreserved"]).toBe(true);
+ expect(result["targetIdPreserved"]).toBe(true);
+ expect(result["historicalOwnerPreserved"]).toBe(true);
+ expect(result["beforeRescan"]).toEqual([
+ "before-recreation",
+ "linked-scope",
+ ]);
+ expect(result["afterRescan"]).toEqual([
+ "after-recreation",
+ "before-recreation",
+ "linked-scope",
+ "repeated-rescan",
+ ]);
+ expect(result["malformedOwnerError"]).toContain(
+ "refusing to reuse its target",
+ );
+ expect(result["nongitOwnerError"]).toContain(
+ "refusing to reuse its target",
+ );
+ expect(result["rootOwnerChanged"]).toBe(true);
+ expect(result["rootIdentityPreserved"]).toBe(true);
+ expect(result["recreatedRootError"]).toContain(
+ "refusing to reuse its target",
+ );
+ expect(result["recreatedRootScans"]).toEqual([]);
+ }, 30_000);
+
+ test("does not expand trusted aliases when historical checkout ownership is unverified", () => {
+ const result = runProbe("unverified-owner", fixture());
+
+ expect(result["before"]).toEqual(["trusted-alias"]);
+ expect(result["after"]).toEqual([]);
+ }, 30_000);
+
+ test("keeps non-Git paths isolated and never equates unrelated null target IDs", () => {
+ const result = runProbe("nongit", fixture());
+
+ expect(result["firstIdentity"]).toBeNull();
+ expect(result["secondIdentity"]).toBeNull();
+ expect(result["differentNullIdsMatch"]).toBe(false);
+ expect(result["sameNullPathMatches"]).toBe(false);
+ expect(result["firstScans"]).toEqual([]);
+ expect(result["secondScans"]).toEqual(["plain-b"]);
+ }, 30_000);
+});
diff --git a/sdk/typescript/tests-ts/workbench-target-migration.test.ts b/sdk/typescript/tests-ts/workbench-target-migration.test.ts
new file mode 100644
index 000000000..8142e7a95
--- /dev/null
+++ b/sdk/typescript/tests-ts/workbench-target-migration.test.ts
@@ -0,0 +1,484 @@
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { describe, expect, test } from "bun:test";
+import { resolvePluginPython, runCodexCommand } from "../src/runtime.js";
+import { PLUGIN_ROOT } from "./plugin-root.js";
+
+async function runPythonProbe(
+ program: string,
+ ...args: string[]
+): Promise<Record<string, unknown>> {
+ const python = await resolvePluginPython();
+ const result = await runCodexCommand(
+ { command: python },
+ ["-I", "-X", "utf8", "-B", "-", join(PLUGIN_ROOT, "scripts"), ...args],
+ process.env,
+ program,
+ AbortSignal.timeout(10_000),
+ );
+ expect(result.exitCode, result.stderr).toBe(0);
+ expect(result.stderr).toBe("");
+ return JSON.parse(result.stdout) as Record<string, unknown>;
+}
+
+const migrationProbe = String.raw`
+import json
+import sqlite3
+import sys
+from pathlib import Path
+
+sys.path.insert(0, sys.argv[1])
+
+from workbench_schema import MIGRATIONS, apply_migrations
+from workbench_target_state import backfill_security_targets, stable_target_id
+
+scenario = sys.argv[2]
+root = Path(sys.argv[3])
+timestamp = "2026-08-01T00:00:00Z"
+backfill_calls = []
+
+def backfill(connection):
+ backfill_calls.append(True)
+ backfill_security_targets(connection)
+
+connection = sqlite3.connect(":memory:")
+connection.row_factory = sqlite3.Row
+connection.execute("PRAGMA foreign_keys = ON")
+apply_migrations(connection, MIGRATIONS, lambda: timestamp, backfill)
+backfill_calls.clear()
+
+existing_path = str(root / "existing-repository")
+missing_path = str(root / "deleted-repository")
+connection.execute(
+ "INSERT INTO security_targets (id, current_path, display_name, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
+ ("target-existing", existing_path, "existing-repository", timestamp, timestamp),
+)
+connection.executemany(
+ "INSERT INTO workspaces (id, target_path, target_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
+ [
+ ("workspace-existing", existing_path, "target-existing", timestamp, timestamp),
+ ("workspace-empty", None, None, timestamp, timestamp),
+ ],
+)
+
+def insert_scan(scan_id, workspace_id, target_path, target_id):
+ connection.execute(
+ "INSERT INTO scans (id, workspace_id, target_path, target_id, target_revision, scope, mode, scan_dir, status, phase, started_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+ (
+ scan_id,
+ workspace_id,
+ target_path,
+ target_id,
+ "synthetic-revision",
+ ".",
+ "standard",
+ str(root / scan_id),
+ "complete",
+ "reporting",
+ timestamp,
+ timestamp,
+ timestamp,
+ ),
+ )
+
+insert_scan("scan-existing", "workspace-existing", existing_path, "target-existing")
+
+if scenario == "orphan-scan":
+ insert_scan("scan-orphan", "workspace-existing", existing_path, None)
+ expected_target_id = "target-existing"
+ orphan_path = existing_path
+elif scenario in ("dangling-scan", "dangling-workspace", "dangling-targets"):
+ connection.commit()
+ connection.execute("PRAGMA foreign_keys = OFF")
+ if scenario != "dangling-scan":
+ connection.execute(
+ "INSERT INTO workspaces (id, target_path, target_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
+ ("workspace-orphan", missing_path, "target-dangling", timestamp, timestamp),
+ )
+ if scenario != "dangling-workspace":
+ workspace = (
+ "workspace-existing" if scenario == "dangling-scan" else "workspace-orphan"
+ )
+ insert_scan("scan-orphan", workspace, missing_path, "target-dangling")
+ connection.commit()
+ connection.execute("PRAGMA foreign_keys = ON")
+ expected_target_id = stable_target_id(Path(missing_path))
+ orphan_path = missing_path
+else:
+ connection.execute(
+ "INSERT INTO workspaces (id, target_path, target_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
+ ("workspace-orphan", missing_path, None, timestamp, timestamp),
+ )
+ expected_target_id = stable_target_id(Path(missing_path))
+ orphan_path = missing_path
+ if scenario == "orphan-workspace-and-scan":
+ insert_scan("scan-orphan", "workspace-orphan", missing_path, None)
+
+connection.commit()
+violations_before_repair = len(connection.execute("PRAGMA foreign_key_check").fetchall())
+apply_migrations(connection, MIGRATIONS, lambda: timestamp, backfill)
+apply_migrations(connection, MIGRATIONS, lambda: timestamp, backfill)
+
+def target_id(table, row_id):
+ row = connection.execute(
+ f"SELECT target_id FROM {table} WHERE id = ?", (row_id,)
+ ).fetchone()
+ return row["target_id"] if row is not None else None
+
+print(json.dumps({
+ "backfillCalls": len(backfill_calls),
+ "emptyWorkspaceTargetId": target_id("workspaces", "workspace-empty"),
+ "existingScanTargetId": target_id("scans", "scan-existing"),
+ "existingWorkspaceTargetId": target_id("workspaces", "workspace-existing"),
+ "expectedTargetId": expected_target_id,
+ "foreignKeysEnforced": bool(connection.execute("PRAGMA foreign_keys").fetchone()[0]),
+ "foreignKeyViolationsBeforeRepair": violations_before_repair,
+ "foreignKeyViolationsAfterRepair": len(
+ connection.execute("PRAGMA foreign_key_check").fetchall()
+ ),
+ "migrationRecorded": connection.execute(
+ "SELECT name FROM schema_migrations WHERE version = 16"
+ ).fetchone()[0] == "stable repository targets",
+ "orphanPathExists": Path(orphan_path).exists(),
+ "orphanScanTargetId": target_id("scans", "scan-orphan"),
+ "orphanWorkspaceTargetId": target_id("workspaces", "workspace-orphan"),
+ "targetCount": connection.execute(
+ "SELECT count(*) FROM security_targets"
+ ).fetchone()[0],
+}))
+`;
+
+const repositoryIdentityMigrationProbe = String.raw`
+import json
+import sqlite3
+import sys
+
+sys.path.insert(0, sys.argv[1])
+
+from workbench_schema import MIGRATIONS, apply_migrations, sql_statements
+from workbench_target_state import backfill_security_targets
+
+scenario = sys.argv[2]
+timestamp = "2026-08-01T00:00:00Z"
+backfill_calls = []
+
+def backfill(connection):
+ backfill_calls.append(True)
+ backfill_security_targets(connection)
+
+connection = sqlite3.connect(":memory:")
+connection.row_factory = sqlite3.Row
+identity_migration = next(
+ migration for migration in MIGRATIONS if migration[1] == "persist repository identities"
+)
+identity_version = identity_migration[0]
+legacy_identity_migration = (
+ 30, "persist repository identities",
+ "ALTER TABLE security_targets ADD COLUMN repository_identity TEXT;\n"
+ "CREATE INDEX security_targets_by_repository_identity ON security_targets(repository_identity);\n",
+)
+historical = tuple(migration for migration in MIGRATIONS if migration[0] <= 28)
+published = tuple(
+ migration for migration in MIGRATIONS
+ if migration[0] < (
+ 31 if scenario == "pre-release-identity-version31" else
+ 33 if scenario == "pre-release-identity-version33" else
+ 40 if scenario == "pre-release-identity-version40" else identity_version
+ )
+)
+apply_migrations(
+ connection,
+ historical if scenario in (
+ "out-of-order-publication-migrations", "pre-release-identity-version"
+ ) else published,
+ lambda: timestamp,
+ backfill,
+)
+backfill_calls.clear()
+connection.execute(
+ "INSERT INTO security_targets (id, current_path, display_name, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
+ ("target-existing", "/synthetic/deleted-repository", "repository", timestamp, timestamp),
+)
+
+if scenario == "recorded-without-column":
+ connection.execute(
+ "INSERT INTO schema_migrations VALUES (?, ?, ?)",
+ (identity_version, "persist repository identities", timestamp),
+ )
+elif scenario == "recorded-without-index":
+ connection.execute(
+ "ALTER TABLE security_targets ADD COLUMN repository_identity TEXT"
+ )
+ connection.execute(
+ "INSERT INTO schema_migrations VALUES (?, ?, ?)",
+ (identity_version, "persist repository identities", timestamp),
+ )
+
+connection.commit()
+if scenario == "out-of-order-publication-migrations":
+ apply_migrations(
+ connection, (*historical, identity_migration), lambda: timestamp, backfill
+ )
+elif scenario in ("pre-release-identity-version", "pre-release-identity-version31"):
+ if scenario == "pre-release-identity-version":
+ apply_migrations(
+ connection,
+ (*historical, legacy_identity_migration),
+ lambda: timestamp,
+ backfill,
+ )
+ else:
+ connection.executescript(legacy_identity_migration[2])
+ connection.execute(
+ "INSERT INTO schema_migrations VALUES (?, ?, ?)",
+ (31, legacy_identity_migration[1], timestamp),
+ )
+ connection.execute(
+ "UPDATE security_targets SET repository_identity = 'synthetic-identity'"
+ )
+elif scenario in ("pre-release-identity-version33", "pre-release-identity-version40"):
+ for statement in sql_statements(identity_migration[2]):
+ connection.execute(statement)
+ connection.execute(
+ "INSERT INTO schema_migrations VALUES (?, ?, ?)",
+ (int(scenario.rsplit("version", 1)[1]), identity_migration[1], timestamp),
+ )
+ connection.execute(
+ "UPDATE security_targets SET repository_identity = 'synthetic-identity'"
+ )
+apply_migrations(connection, MIGRATIONS, lambda: timestamp, backfill)
+apply_migrations(connection, MIGRATIONS, lambda: timestamp, backfill)
+
+columns = {
+ row["name"]: row
+ for row in connection.execute("PRAGMA table_info(security_targets)")
+}
+indexes = {
+ row["name"]: row
+ for row in connection.execute("PRAGMA index_list(security_targets)")
+}
+scan_columns = {
+ row["name"]: row for row in connection.execute("PRAGMA table_info(scans)")
+}
+scan_indexes = {
+ row["name"]: row for row in connection.execute("PRAGMA index_list(scans)")
+}
+print(json.dumps({
+ "backfillCalls": len(backfill_calls),
+ "hasRepositoryIdentityColumn": "repository_identity" in columns,
+ "hasRepositoryIdentityIndex": "security_targets_by_repository_identity" in indexes,
+ "identityVersion": identity_version,
+ "hasCurrentFindingsSchema": (
+ any(row["name"] == "details_json" for row in connection.execute("PRAGMA table_info(findings)"))
+ and connection.execute("SELECT 1 FROM sqlite_master WHERE name = 'finding_embeddings'").fetchone() is not None
+ ),
+ "publicationMigrations": {
+ str(row["version"]): row["name"]
+ for row in connection.execute(
+ "SELECT version, name FROM schema_migrations WHERE version IN (29, 30)"
+ )
+ },
+ "hasLegacyPublicationTable": connection.execute(
+ "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'finding_publications'"
+ ).fetchone() is not None,
+ "issueReceiptIndexes": sorted(
+ row["name"]
+ for row in connection.execute("PRAGMA index_list(finding_issue_receipts)")
+ if row["name"].startswith("finding_issue_receipts_")
+ ),
+ "repositoryIdentityColumnIsNullable": not bool(
+ columns["repository_identity"]["notnull"]
+ ),
+ "repositoryIdentityIndexIsUnique": bool(
+ indexes["security_targets_by_repository_identity"]["unique"]
+ ),
+ "hasRepositoryGenerationColumn": "repository_generation" in scan_columns,
+ "hasRepositoryGenerationIndex": "scans_by_repository_generation" in scan_indexes,
+ "repositoryGenerationColumnIsNullable": not bool(
+ scan_columns["repository_generation"]["notnull"]
+ ),
+ "completionSequenceIsNullable": not bool(
+ scan_columns["completion_sequence"]["notnull"]
+ ),
+ "completionSequenceIndexIsUnique": bool(
+ scan_indexes["scans_completion_sequence"]["unique"]
+ ),
+ "completionSequenceTriggers": sorted(row["name"] for row in connection.execute(
+ "SELECT name FROM sqlite_master WHERE type = 'trigger' "
+ "AND name LIKE 'scans_assign_%_completion_sequence'"
+ )),
+ "migrationName": connection.execute(
+ "SELECT name FROM schema_migrations WHERE version = ?", (identity_version,)
+ ).fetchone()[0],
+ "targetIdentity": connection.execute(
+ "SELECT repository_identity FROM security_targets"
+ ).fetchone()[0],
+ "targetId": connection.execute(
+ "SELECT id FROM security_targets"
+ ).fetchone()[0],
+}))
+`;
+
+describe("stable workbench target migration", () => {
+ test.each([
+ ["orphan-scan", "reuses an existing target for an orphaned scan"],
+ ["orphan-workspace", "repairs a workspace without an orphaned scan"],
+ ["dangling-scan", "repairs a dangling scan foreign key independently"],
+ [
+ "dangling-workspace",
+ "repairs a dangling workspace foreign key independently",
+ ],
+ [
+ "dangling-targets",
+ "repairs dangling workspace and scan foreign keys atomically",
+ ],
+ [
+ "orphan-workspace-and-scan",
+ "repairs a workspace and scan after their repository is deleted",
+ ],
+ ] as const)("%s: %s", async (scenario) => {
+ const result = (await runPythonProbe(
+ migrationProbe,
+ scenario,
+ join(tmpdir(), "codex-security-stable-target-migration", scenario),
+ )) as {
+ backfillCalls: number;
+ emptyWorkspaceTargetId: string | null;
+ existingScanTargetId: string;
+ existingWorkspaceTargetId: string;
+ expectedTargetId: string;
+ foreignKeysEnforced: boolean;
+ foreignKeyViolationsBeforeRepair: number;
+ foreignKeyViolationsAfterRepair: number;
+ migrationRecorded: boolean;
+ orphanPathExists: boolean;
+ orphanScanTargetId: string | null;
+ orphanWorkspaceTargetId: string | null;
+ targetCount: number;
+ };
+
+ expect(result).toMatchObject({
+ backfillCalls: 1,
+ emptyWorkspaceTargetId: null,
+ existingScanTargetId: "target-existing",
+ existingWorkspaceTargetId: "target-existing",
+ foreignKeysEnforced: true,
+ foreignKeyViolationsBeforeRepair:
+ scenario === "dangling-targets"
+ ? 2
+ : scenario.startsWith("dangling-")
+ ? 1
+ : 0,
+ foreignKeyViolationsAfterRepair: 0,
+ migrationRecorded: true,
+ orphanPathExists: false,
+ targetCount: scenario === "orphan-scan" ? 1 : 2,
+ });
+ expect(result.orphanScanTargetId).toBe(
+ scenario === "orphan-workspace" || scenario === "dangling-workspace"
+ ? null
+ : result.expectedTargetId,
+ );
+ expect(result.orphanWorkspaceTargetId).toBe(
+ scenario === "orphan-scan" || scenario === "dangling-scan"
+ ? null
+ : result.expectedTargetId,
+ );
+ });
+
+ test.each([
+ [
+ "unapplied",
+ "applies and backfills the new repository-identity migration",
+ ],
+ [
+ "recorded-without-column",
+ "repairs a recorded migration missing its column and index",
+ ],
+ [
+ "recorded-without-index",
+ "repairs a recorded migration missing only its index",
+ ],
+ [
+ "out-of-order-publication-migrations",
+ "applies published migrations after repository identity was recorded",
+ ],
+ [
+ "pre-release-identity-version",
+ "quarantines an unverifiable pre-release identity without changing its target",
+ ],
+ [
+ "pre-release-identity-version31",
+ "repairs an already-renumbered pre-release identity migration",
+ ],
+ [
+ "pre-release-identity-version33",
+ "preserves the previous identity migration and installs the current findings schema",
+ ],
+ [
+ "pre-release-identity-version40",
+ "preserves the previous identity migration alongside published history and severity migrations",
+ ],
+ ] as const)("%s: %s", async (scenario) => {
+ const result = (await runPythonProbe(
+ repositoryIdentityMigrationProbe,
+ scenario,
+ )) as {
+ backfillCalls: number;
+ hasRepositoryIdentityColumn: boolean;
+ hasRepositoryIdentityIndex: boolean;
+ identityVersion: number;
+ hasCurrentFindingsSchema: boolean;
+ migrationName: string;
+ publicationMigrations: Record<string, string>;
+ repositoryIdentityColumnIsNullable: boolean;
+ repositoryIdentityIndexIsUnique: boolean;
+ hasRepositoryGenerationColumn: boolean;
+ hasRepositoryGenerationIndex: boolean;
+ repositoryGenerationColumnIsNullable: boolean;
+ completionSequenceIsNullable: boolean;
+ completionSequenceIndexIsUnique: boolean;
+ completionSequenceTriggers: string[];
+ targetIdentity: string | null;
+ targetId: string;
+ hasLegacyPublicationTable: boolean;
+ issueReceiptIndexes: string[];
+ };
+ expect(result).toEqual({
+ backfillCalls: scenario.startsWith("pre-release-identity-version")
+ ? 0
+ : 1,
+ hasRepositoryIdentityColumn: true,
+ hasRepositoryIdentityIndex: true,
+ identityVersion: 67,
+ hasCurrentFindingsSchema: true,
+ migrationName: "persist repository identities",
+ publicationMigrations: {
+ "29": "persist finding publication associations",
+ "30": "preserve team-only finding publication associations",
+ },
+ repositoryIdentityColumnIsNullable: true,
+ repositoryIdentityIndexIsUnique: false,
+ hasRepositoryGenerationColumn: true,
+ hasRepositoryGenerationIndex: true,
+ repositoryGenerationColumnIsNullable: true,
+ completionSequenceIsNullable: true,
+ completionSequenceIndexIsUnique: true,
+ completionSequenceTriggers: [
+ "scans_assign_inserted_completion_sequence",
+ "scans_assign_updated_completion_sequence",
+ ],
+ targetIdentity:
+ scenario === "pre-release-identity-version33" ||
+ scenario === "pre-release-identity-version40"
+ ? "synthetic-identity"
+ : null,
+ targetId: "target-existing",
+ hasLegacyPublicationTable: false,
+ issueReceiptIndexes: [
+ "finding_issue_receipts_by_destination_issue",
+ "finding_issue_receipts_by_finding",
+ ],
+ });
+ });
+});