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(deep-scan): attribute comparison sessions to usage and logs (#2299)

Michael D'Angelo5b1f8fee8f7b
diff --git a/plugins/codex-security/scripts/workbench_scan_usage.py b/plugins/codex-security/scripts/workbench_scan_usage.py
index 23f4cf1ef..a0cb5b902 100644
--- a/plugins/codex-security/scripts/workbench_scan_usage.py
+++ b/plugins/codex-security/scripts/workbench_scan_usage.py
@@ -114,6 +114,7 @@ def collect_scan_usage(
roots,
warnings,
worker_directories=worker_directories,
+ scan_directory=Path(scan["scan_dir"]) if worker_directories else None,
started_at=started_at,
completed_at=stopped_at,
)
@@ -269,10 +270,10 @@ def _scan_execution_thread_ids(connection: sqlite3.Connection, scan: sqlite3.Row
def _scan_worker_directories(connection: sqlite3.Connection, scan: sqlite3.Row) -> set[str]:
if scan["mode"] != "deep":
return set()
- return {
- row["artifact_dir"]
- if row["kind"] == "discovery"
- else str(Path(scan["scan_dir"]) / "artifacts")
+ artifacts = Path(scan["scan_dir"]) / "artifacts"
+ # Finding comparisons start independent sessions before the batch checkpoint is saved.
+ return {str(artifacts / "deep_discovery")} | {
+ row["artifact_dir"] if row["kind"] == "discovery" else str(artifacts)
for row in connection.execute(
"SELECT kind, artifact_dir FROM deep_scan_workers "
"WHERE scan_id = ? AND kind IN ('discovery', 'dedup')",
@@ -324,6 +325,7 @@ def _discover_rollout_sessions(
warnings: set[str],
*,
worker_directories: set[str],
+ scan_directory: Path | None = None,
started_at: datetime,
completed_at: datetime | None,
) -> tuple[list[RolloutSession], set[str]]:
@@ -342,6 +344,9 @@ def _discover_rollout_sessions(
columns = {str(row["name"]) for row in database.execute(f"PRAGMA table_info({table})")}
if not required.issubset(columns):
raise ValueError("Codex state graph does not expose the required thread columns.")
+ if table == "threads" and worker_directories and not {"cwd", "created_at"} <= columns:
+ worker_directories = set()
+ warnings.add("worker_directories_unavailable")
sessions: dict[str, RolloutSession] = {}
missing_thread_ids: set[str] = set()
root_ids = dict.fromkeys(roots)
@@ -356,6 +361,36 @@ def _discover_rollout_sessions(
)
directory_expression = "codex_security_path_key(cwd)"
worker_directories = {_normalized_windows_path(value) for value in worker_directories}
+ if scan_directory is not None:
+ scan_directory = Path(_normalized_windows_path(scan_directory))
+ if worker_directories and scan_directory and ".previous-" in scan_directory.name:
+ # Archived files move, but session working directories describe where execution ran.
+ # Confirm the original output with an already-bound session before including peers.
+ bound_directories = {
+ Path(row[0])
+ for row in database.execute(
+ f"SELECT DISTINCT {directory_expression} FROM threads WHERE cwd IS NOT NULL "
+ "AND id IN (SELECT value FROM json_each(?))",
+ (json.dumps(roots),),
+ )
+ }
+ relatives = {
+ Path(path).relative_to(scan_directory)
+ for path in worker_directories
+ if Path(path).is_relative_to(scan_directory)
+ }
+ for marker in re.finditer(r"\.previous-", scan_directory.name):
+ if marker.start() == 0:
+ continue
+ original = scan_directory.with_name(scan_directory.name[: marker.start()])
+ candidates = {original / relative for relative in relatives}
+ # Older archives kept absolute discovery paths in their original directory.
+ if (
+ original in bound_directories
+ or candidates & bound_directories
+ or any(Path(path).is_relative_to(original) for path in worker_directories)
+ ):
+ worker_directories = worker_directories | {str(path) for path in candidates}
for directory in sorted(worker_directories):
for row in database.execute(
"SELECT id FROM threads WHERE cwd IS NOT NULL "
@@ -392,7 +427,11 @@ def _discover_rollout_sessions(
and isinstance(payload := metadata.get("payload"), dict)
and (payload.get("id") or payload.get("session_id")) == root
and (created_at := _timestamp(payload.get("timestamp"))) is not None
- and created_at < started_millisecond
+ and (
+ created_at < started_millisecond
+ or completed_at is not None
+ and created_at > completed_at
+ )
):
continue
sessions[root] = RolloutSession(root, None, path)
diff --git a/plugins/codex-security/tests/test_workbench_scan_usage.py b/plugins/codex-security/tests/test_workbench_scan_usage.py
index 77fd112d4..87cc68523 100644
--- a/plugins/codex-security/tests/test_workbench_scan_usage.py
+++ b/plugins/codex-security/tests/test_workbench_scan_usage.py
@@ -1730,8 +1730,13 @@ def test_failed_scan_preserves_legacy_failure_behavior(tmp_path: Path) -> None:
assert "usage" not in failed
[email protected]("worker_kind", ["discovery", "dedup"])
-def test_completion_recovers_replaced_deep_worker_threads(tmp_path: Path, worker_kind: str) -> None:
+ ("worker_kind", "legacy_archive"),
+ [("discovery", False), ("discovery", True), ("dedup", False)],
+)
+def test_completion_recovers_replaced_deep_worker_threads(
+ tmp_path: Path, worker_kind: str, legacy_archive: bool
+) -> None:
fixture = _start_scan(tmp_path, mode="deep")
fixture = replace(
fixture,
@@ -1805,6 +1810,9 @@ def test_completion_recovers_replaced_deep_worker_threads(tmp_path: Path, worker
[_token_event(counted, 20, 5), _token_event(counted, 30, 8)],
session_started_at=before_scan,
),
+ "finding-comparison": _rollout(
+ tmp_path, "finding-comparison", [_token_event(counted, 12, 4)]
+ ),
"other-scan-worker": _rollout(
tmp_path, "other-scan-worker", [_token_event(counted, 80_000, 20_000)]
),
@@ -1834,6 +1842,11 @@ def test_completion_recovers_replaced_deep_worker_threads(tmp_path: Path, worker
connection.executemany(
"UPDATE threads SET cwd = ?, created_at = ? WHERE id = ?",
[
+ (
+ str(fixture.scan_dir / "artifacts" / "deep_discovery"),
+ int(counted.timestamp()),
+ "finding-comparison",
+ ),
(str(working_directory), int(counted.timestamp()), "sdk-first-attempt"),
(str(working_directory), int(counted.timestamp()), "sdk-first-child"),
(str(working_directory), int(counted.timestamp()), "sdk-current-attempt"),
@@ -1853,16 +1866,79 @@ def test_completion_recovers_replaced_deep_worker_threads(tmp_path: Path, worker
(str(working_directory), int(after_scan.timestamp()), "later-missing-worker"),
],
)
- usage = _complete_scan(fixture)["scan"]["usage"]
+ arguments = ("--thread-id", "scan-parent", "--turn-id", "turn-scan-parent")
+ completed = _complete_scan(fixture, *arguments)["scan"]
+ usage = completed["usage"]
assert (
usage.items()
>= {
- "coverage": "complete",
+ "coverage": "partial",
+ "warnings": ["turn_in_progress"],
"source": "codex_rollout",
- **_counts(67, 0, 18),
- "threadCount": 4,
+ **_counts(79, 0, 22),
+ "threadCount": 5,
}.items()
)
+ run_workbench(
+ fixture.state_dir,
+ "register-cli-scan",
+ "--repository",
+ str(fixture.target),
+ "--scan-dir",
+ str(fixture.scan_dir),
+ "--archive-existing",
+ "--recipe-json",
+ json.dumps(
+ {
+ "config": {},
+ "mode": "deep",
+ "repository": str(fixture.target),
+ "target": {"kind": "repository", "paths": []},
+ }
+ ),
+ environment=fixture.environment,
+ )
+ if legacy_archive:
+ with sqlite3.connect(fixture.state_dir / "workbench.sqlite3") as db:
+ db.execute(
+ "UPDATE deep_scan_workers SET artifact_dir = ? WHERE id = ?",
+ (str(artifact), worker_id),
+ )
+ ended = datetime.fromisoformat(completed["usage"]["window"]["endedAt"])
+ replacement_started = ended + timedelta(microseconds=1)
+ replacement = _rollout(
+ tmp_path,
+ "new-comparison",
+ [_token_event(replacement_started, 100, 10)],
+ session_started_at=replacement_started,
+ )
+ with sqlite3.connect(Path(fixture.environment["CODEX_SQLITE_HOME"]) / "state_5.sqlite") as db:
+ db.execute(
+ "INSERT INTO threads (id, rollout_path, cwd, created_at) VALUES (?, ?, ?, ?)",
+ (
+ "new-comparison",
+ str(replacement),
+ str(fixture.scan_dir / "artifacts" / "deep_discovery"),
+ int(ended.timestamp()),
+ ),
+ )
+ finalized = fixture.command("finalize-scan-usage", *arguments)["scan"]["usage"]
+ assert finalized["completion"]["finalized"] is True
+ assert (finalized["totalTokens"], finalized["threadCount"]) == (101, 5)
+
+
[email protected]("missing", ["cwd", "created_at"])
+def test_deep_usage_retains_bound_sessions_without_directory_metadata(
+ tmp_path: Path, missing: str
+) -> None:
+ fixture = _start_scan(tmp_path, mode="deep")
+ fixture.write_parent_rollout([_token_event(fixture.started_at, 10, 3)])
+ with sqlite3.connect(Path(fixture.environment["CODEX_SQLITE_HOME"]) / "state_5.sqlite") as db:
+ db.execute(f"ALTER TABLE threads DROP COLUMN {missing}")
+ usage = _complete_scan(fixture)["scan"]["usage"]
+ assert usage["coverage"] == "partial"
+ assert usage["warnings"] == ["worker_directories_unavailable"]
+ assert (usage["totalTokens"], usage["threadCount"]) == (13, 1)
@pytest.mark.cross_platform
diff --git a/sdk/typescript/src/scan-logs.ts b/sdk/typescript/src/scan-logs.ts
index 03557ed5b..0b94e1620 100644
--- a/sdk/typescript/src/scan-logs.ts
+++ b/sdk/typescript/src/scan-logs.ts
@@ -22,6 +22,7 @@ interface ScanLogOptions {
executionThreadIds?: readonly string[];
codexHome: string | readonly string[];
scanDirectory?: string;
+ startedAt?: string;
completedAt?: string | null;
allowMissingRoot?: boolean;
}
@@ -33,6 +34,7 @@ export type ScanLogSource = JsonObject & {
executionThreadIds?: string[];
mode?: string;
scanDir?: string;
+ lifecycle?: { startedAt: string; finishedAt?: string | null };
progress?: { status?: string; updatedAt?: string };
};
@@ -55,13 +57,14 @@ export function readSavedScanLogs(
codexHome,
allowMissingRoot: options.allowMissingRoot,
scanDirectory: scan.mode === "deep" ? scan.scanDir : undefined,
+ startedAt: scan.lifecycle?.startedAt,
completedAt:
scan.progress?.status === "running"
? null
: scan.progress?.status === "complete" ||
scan.progress?.status === "failed" ||
scan.progress?.status === "canceled"
- ? (scan.progress.updatedAt ?? "")
+ ? (scan.lifecycle?.finishedAt ?? scan.progress.updatedAt ?? "")
: "",
});
}
@@ -127,7 +130,6 @@ export async function readScanLogs(options: ScanLogOptions) {
`No saved session logs are available for scan ${options.scanId}.`,
);
}
- let children: Map<string | undefined, SessionLog[]> | undefined;
const included = new Set([
...(options.threadId ? [options.threadId] : []),
@@ -137,24 +139,55 @@ export async function readScanLogs(options: ScanLogOptions) {
// A Desktop owner can contain other work. Include its log without treating
// the whole conversation tree as part of this scan.
const traversed = new Set(options.executionThreadIds ?? included);
- for (const parentId of traversed) {
- const parent = logs.get(parentId)?.[0];
- // Parentless workers can only belong to the root, in discovery order.
- children ??= Map.groupBy(
- logs.values().map(([session]) => session),
- (session) => session.parentThreadId ?? root?.threadId,
- );
- for (const session of children.get(parentId) ?? []) {
+ const children = Map.groupBy(
+ logs.values().map(([session]) => session),
+ (session) => session.parentThreadId,
+ );
+ // Comparisons have no spawn edge. The saved scan window also works for a
+ // shared Desktop owner, whose unrelated child threads must stay excluded.
+ const startedAt =
+ options.startedAt === undefined
+ ? root && traversed.has(root.threadId)
+ ? root.startedAt
+ : null
+ : sessionStartedAt(options.startedAt);
+ if (options.scanDirectory && startedAt !== null) {
+ const directories = [options.scanDirectory];
+ const name = basename(options.scanDirectory);
+ for (const marker of name.matchAll(/\.previous-/g)) {
+ if (!marker.index) continue;
+ const original = join(
+ dirname(options.scanDirectory),
+ name.slice(0, marker.index),
+ );
+ // A Desktop owner can work outside the scan directory. Its registered
+ // workers provide the same binding after archiving the output.
if (
- !traversed.has(session.threadId) &&
- (session.parentThreadId === parentId ||
- (parent !== undefined && belongsToScan(session, parent, options)))
+ (root?.workingDirectory != null &&
+ relative(original, root.workingDirectory) === "") ||
+ Array.from(traversed).some((threadId) => {
+ const cwd = logs.get(threadId)?.[0].workingDirectory;
+ return cwd != null && isScanArtifactDirectory(original, cwd);
+ })
+ )
+ directories.push(original);
+ }
+ for (const session of children.get(null) ?? []) {
+ if (
+ session !== root &&
+ belongsToScan(session, directories, startedAt, options.completedAt)
) {
included.add(session.threadId);
traversed.add(session.threadId);
}
}
}
+ for (const parentId of traversed) {
+ for (const session of children.get(parentId) ?? []) {
+ included.add(session.threadId);
+ traversed.add(session.threadId);
+ }
+ }
const sessions: SessionLog[] = [];
for (const threadId of included) {
const copies = logs.get(threadId);
@@ -202,16 +235,14 @@ export async function readScanLogs(options: ScanLogOptions) {
function belongsToScan(
session: SessionLog,
- root: SessionLog,
- options: ScanLogOptions,
+ directories: string[],
+ startedAt: number,
+ completedAt: string | null | undefined,
): boolean {
- const { scanDirectory, completedAt } = options;
if (
- scanDirectory === undefined ||
session.workingDirectory === null ||
- root.startedAt === null ||
session.startedAt === null ||
- session.startedAt < root.startedAt
+ session.startedAt < startedAt
) {
return false;
}
@@ -222,21 +253,7 @@ function belongsToScan(
}
}
- const roots = [scanDirectory];
- const name = basename(scanDirectory);
- const marker = name.lastIndexOf(".previous-");
- if (
- marker > 0 &&
- root.workingDirectory !== null &&
- relative(
- join(dirname(scanDirectory), name.slice(0, marker)),
- root.workingDirectory,
- ) === ""
- ) {
- roots.push(root.workingDirectory);
- }
-
- for (const directoryRoot of roots) {
+ for (const directoryRoot of directories) {
if (isScanArtifactDirectory(directoryRoot, session.workingDirectory)) {
return true;
}
diff --git a/sdk/typescript/src/scan-sessions.ts b/sdk/typescript/src/scan-sessions.ts
index b74dab0ea..50dacc6af 100644
--- a/sdk/typescript/src/scan-sessions.ts
+++ b/sdk/typescript/src/scan-sessions.ts
@@ -56,7 +56,10 @@ export function isScanArtifactDirectory(
const artifacts = join(scanDirectory, "artifacts");
if (relative(artifacts, workingDirectory) === "") return true;
- const workers = join(artifacts, "deep_discovery", "workers");
+ const deepDirectory = join(artifacts, "deep_discovery");
+ if (relative(deepDirectory, workingDirectory) === "") return true;
+
+ const workers = join(deepDirectory, "workers");
const directory = relative(workers, workingDirectory);
const components = directory.split(sep);
return (
diff --git a/sdk/typescript/tests-ts/cost.test.ts b/sdk/typescript/tests-ts/cost.test.ts
index a2ac8b73b..414231e7b 100644
--- a/sdk/typescript/tests-ts/cost.test.ts
+++ b/sdk/typescript/tests-ts/cost.test.ts
@@ -3389,6 +3389,15 @@ describe("live scan cost tracking", () => {
timestamp: "2026-07-26T12:02:00Z",
},
);
+ await writeSession(
+ home,
+ "finding-comparison",
+ { input_tokens: 40, output_tokens: 4 },
+ {
+ cwd: join(artifacts, "deep_discovery"),
+ timestamp: "2026-07-26T12:03:00Z",
+ },
+ );
await writeSession(
home,
"deep-worker-child",
@@ -3449,11 +3458,11 @@ describe("live scan cost tracking", () => {
tracker.start("scan-thread");
expect((await tracker.stop()).usage).toMatchObject({
- input_tokens: 1_425,
- output_tokens: 14,
+ input_tokens: 1_465,
+ output_tokens: 18,
});
expect(workers).toEqual(
- [1, 2, 3].map((worker) => ({ kind: "observed", worker })),
+ [1, 2, 3, 4].map((worker) => ({ kind: "observed", worker })),
);
const labels = new Map(
events.map(({ threadId, worker }) => [threadId, worker]),
@@ -3463,13 +3472,14 @@ describe("live scan cost tracking", () => {
"scan-thread",
"deep-worker",
"deep-reducer",
+ "finding-comparison",
"deep-worker-child",
]),
);
expect(labels.get("scan-thread")).toBeUndefined();
expect(
[...labels.values()].filter((worker) => worker !== undefined).sort(),
- ).toEqual([1, 2, 3]);
+ ).toEqual([1, 2, 3, 4]);
const logs = await readScanLogs({
scanId: "scan-example",
threadId: "scan-thread",
diff --git a/sdk/typescript/tests-ts/scan-logs.test.ts b/sdk/typescript/tests-ts/scan-logs.test.ts
index ac076bbc7..c55e034e8 100644
--- a/sdk/typescript/tests-ts/scan-logs.test.ts
+++ b/sdk/typescript/tests-ts/scan-logs.test.ts
@@ -165,6 +165,69 @@ describe("saved scan logs", () => {
expect(result.sessions.map(({ threadId }) => threadId)).toEqual(["parent"]);
});
+ test.each([false, true])(
+ "collects parentless comparisons inside a shared Desktop scan window (archived: %p)",
+ async (archived) => {
+ const home = await temporaryHome();
+ const scanDir = join(home, "scan");
+ const cwd = join(scanDir, "artifacts", "deep_discovery");
+ await writeSession(home, "owner", [], undefined, "2026-08-01T00:00:00Z");
+ await writeSession(home, "other-task", [], "owner");
+ await writeSession(
+ home,
+ "worker",
+ [],
+ undefined,
+ "2026-08-02T00:30:00Z",
+ join(cwd, "workers", "discovery-0001", "output"),
+ );
+ await writeSession(
+ home,
+ "replacement-comparison",
+ [],
+ undefined,
+ "2026-08-02T02:00:00Z",
+ cwd,
+ );
+ await writeSession(
+ home,
+ "older-comparison",
+ [],
+ undefined,
+ "2026-08-01T01:00:00Z",
+ cwd,
+ );
+ await writeSession(
+ home,
+ "comparison",
+ [],
+ undefined,
+ "2026-08-02T01:00:00Z",
+ cwd,
+ );
+ const result = await readSavedScanLogs(
+ {
+ scanId: "scan-1",
+ scanDir: archived ? scanDir + ".previous-test" : scanDir,
+ mode: "deep",
+ continuationThreadId: "owner",
+ lifecycle: {
+ startedAt: "2026-08-02T00:00:00Z",
+ finishedAt: "2026-08-02T02:00:00Z",
+ },
+ progress: { status: "complete", updatedAt: "2026-08-03T00:00:00Z" },
+ executionThreadIds: ["worker"],
+ },
+ home,
+ );
+ expect(result.sessions.map(({ threadId }) => threadId).sort()).toEqual([
+ "comparison",
+ "owner",
+ "worker",
+ ]);
+ },
+ );
+
test("collects known desktop and CLI threads across active and archived homes without duplicates", async () => {
const desktop = await temporaryHome();
const cli = await temporaryHome();