Skip to content

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

refactor: simplify workbench history and usage readers (#1495)

mldangelo-oai97d27e4a0583
diff --git a/plugins/codex-security/scripts/workbench_scan_history.py b/plugins/codex-security/scripts/workbench_scan_history.py
index c3d035399..9e3202c02 100644
--- a/plugins/codex-security/scripts/workbench_scan_history.py
+++ b/plugins/codex-security/scripts/workbench_scan_history.py
@@ -460,7 +460,7 @@ def _saved_finding_links(connection: sqlite3.Connection, scan_ids: set[str]) ->
FROM scan_comparison_matches AS matches
JOIN finding_occurrences AS before ON before.id = matches.before_occurrence_id
JOIN finding_occurrences AS after ON after.id = matches.after_occurrence_id
- WHERE matches.before_scan_id IN ({placeholders})
+ WHERE matches.before_scan_id IN (SELECT value FROM json_each(?))
ORDER BY matches.before_scan_id, after.scan_id, before.finding_id, after.finding_id
""",
sorted(scan_ids),
@@ -828,14 +828,8 @@ def _rows_for_ids(
connection: sqlite3.Connection, query: str, ids: Iterable[str]
) -> Iterator[sqlite3.Row]:
values = tuple(dict.fromkeys(ids))
- getlimit = getattr(connection, "getlimit", None)
- # Python 3.10 lacks getlimit; 999 is SQLite's older host-parameter limit.
- limit = getlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER) if getlimit else 999
- for start in range(0, len(values), limit):
- batch = values[start : start + limit]
- yield from connection.execute(
- query.format(placeholders=", ".join("?" for _ in batch)), batch
- )
+ if values:
+ yield from connection.execute(query, (json.dumps(values),))
# Stable finding IDs already include the target identity. Follow their indexed
@@ -855,7 +849,7 @@ _LINKED_FINDINGS_SQL = f"""
WITH RECURSIVE linked(finding_id) AS (
SELECT occurrences.finding_id
FROM finding_occurrences AS occurrences
- WHERE occurrences.id IN ({{placeholders}})
+ WHERE occurrences.id IN (SELECT value FROM json_each(?))
UNION
SELECT neighbor.finding_id
{_FINDING_NEIGHBORS_SQL}
@@ -924,7 +918,7 @@ def finding_relations(
for row in _rows_for_ids(
connection,
"SELECT id, finding_id, scan_id, title FROM finding_occurrences "
- "WHERE id IN ({placeholders})",
+ "WHERE id IN (SELECT value FROM json_each(?))",
(pair[key] for pair in pairs for key in ("beforeOccurrenceId", "afterOccurrenceId")),
)
}
diff --git a/plugins/codex-security/scripts/workbench_scan_usage.py b/plugins/codex-security/scripts/workbench_scan_usage.py
index 463a23585..6f3e01e79 100644
--- a/plugins/codex-security/scripts/workbench_scan_usage.py
+++ b/plugins/codex-security/scripts/workbench_scan_usage.py
@@ -263,12 +263,13 @@ def _discover_rollout_sessions(
try:
database.row_factory = sqlite3.Row
database.execute("PRAGMA query_only = ON")
- _require_state_columns(database, "threads", {"id", "rollout_path"})
- _require_state_columns(
- database,
- "thread_spawn_edges",
- {"parent_thread_id", "child_thread_id"},
- )
+ for table, required in (
+ ("threads", {"id", "rollout_path"}),
+ ("thread_spawn_edges", {"parent_thread_id", "child_thread_id"}),
+ ):
+ 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.")
sessions: dict[str, RolloutSession] = {}
missing_thread_ids: set[str] = set()
for root in roots:
@@ -348,20 +349,6 @@ def _discover_rollout_sessions(
database.close()
-def _require_state_columns(
- connection: sqlite3.Connection,
- table: str,
- required: set[str],
-) -> None:
- statements = {
- "threads": "PRAGMA table_info(threads)",
- "thread_spawn_edges": "PRAGMA table_info(thread_spawn_edges)",
- }
- columns = {str(row["name"]) for row in connection.execute(statements[table])}
- if not required.issubset(columns):
- raise ValueError("Codex state graph does not expose the required thread columns.")
-
-
def _rollout_path(value: object) -> Path | None:
if not isinstance(value, str) or not value:
return None
diff --git a/sdk/typescript/tests-ts/workbench-scan-history.test.ts b/sdk/typescript/tests-ts/workbench-scan-history.test.ts
index a764180a6..b21825ced 100644
--- a/sdk/typescript/tests-ts/workbench-scan-history.test.ts
+++ b/sdk/typescript/tests-ts/workbench-scan-history.test.ts
@@ -368,7 +368,7 @@ test("loads each scan once and scopes saved links to uncached history", async ()
" 'unscopedQueries': sum('WHERE matches.before_scan_id' not in query for query in link_queries),",
" 'unavailable': unavailable, 'forcedKnownGroups': [batch.get('knownFindingGroups') for batch in forced['batches']],",
" 'batchedLinks': [[row['before_scan_id'], row['after_scan_id']] for row in batched],",
- " 'batchedQueryCount': batched_queries, 'expectedBatchedQueryCount': 2 if limited else 1,",
+ " 'batchedQueryCount': batched_queries,",
" 'emptyLinks': empty, 'emptyQueryCount': len(queries),",
"}))",
].join("\n");
@@ -390,6 +390,7 @@ test("loads each scan once and scopes saved links to uncached history", async ()
["scan-1", "scan-2"],
["scan-2", "scan-0"],
],
+ batchedQueryCount: 1,
emptyLinks: [],
emptyQueryCount: 0,
unavailable: {
@@ -415,9 +416,6 @@ test("loads each scan once and scopes saved links to uncached history", async ()
],
},
});
- expect(observed["batchedQueryCount"]).toBe(
- observed["expectedBatchedQueryCount"],
- );
});
test("reconciles cached statuses without losing grouped coverage or uncertainty", async () => {
@@ -606,13 +604,9 @@ batched_queries = len(queries)
if limited:
connection.setlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, old_limit)
-class LegacyConnection:
- def execute(self, *args):
- return connection.execute(*args)
-
queries.clear()
legacy_rows = list(history._rows_for_ids(
- LegacyConnection(), 'SELECT id FROM finding_occurrences WHERE id IN ({placeholders})',
+ connection, 'SELECT id FROM finding_occurrences WHERE id IN (SELECT value FROM json_each(?))',
(f'left-{index}' for index in range(1001))
))
print(json.dumps({
@@ -621,7 +615,6 @@ print(json.dumps({
'reverse': reverse, 'remaining': sorted(remaining), 'unchanged': unchanged,
'restoredAfterUnlink': restored,
'batchedCount': len(batched), 'batchedQueries': batched_queries,
- 'expectedBatchedQueries': 6 if limited else 3,
'legacyCount': len(legacy_rows), 'legacyQueries': len(queries)
}))
`;
@@ -640,10 +633,10 @@ print(json.dumps({
unchanged: true,
restoredAfterUnlink: true,
batchedCount: 10,
+ batchedQueries: 3,
legacyCount: 1001,
- legacyQueries: 2,
+ legacyQueries: 1,
});
- expect(observed["batchedQueries"]).toBe(observed["expectedBatchedQueries"]);
});
test("includes recurring stable identities in confirmed finding history", async () => {