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(workbench): simplify usage bookkeeping (#1349)

mldangelo-oai951be9737784
diff --git a/plugins/codex-security/scripts/workbench_scan_usage.py b/plugins/codex-security/scripts/workbench_scan_usage.py
index 26d971236..63fa3b45a 100644
--- a/plugins/codex-security/scripts/workbench_scan_usage.py
+++ b/plugins/codex-security/scripts/workbench_scan_usage.py
@@ -267,8 +267,7 @@ def _discover_rollout_sessions(
"thread_spawn_edges",
{"parent_thread_id", "child_thread_id"},
)
- sessions: list[RolloutSession] = []
- seen_thread_ids: set[str] = set()
+ sessions: dict[str, RolloutSession] = {}
missing_thread_ids: set[str] = set()
for root in roots:
row = database.execute(
@@ -279,14 +278,13 @@ def _discover_rollout_sessions(
missing_thread_ids.add(root)
warnings.add("scan_root_unavailable")
continue
- if root not in seen_thread_ids:
+ if root not in sessions:
path = _rollout_path(row["rollout_path"])
if path is None:
missing_thread_ids.add(root)
warnings.add("rollout_unavailable")
continue
- sessions.append(RolloutSession(root, None, path))
- seen_thread_ids.add(root)
+ sessions[root] = RolloutSession(root, None, path)
descendants = database.execute(
"""
WITH RECURSIVE descendants(
@@ -335,16 +333,15 @@ def _discover_rollout_sessions(
missing_thread_ids.add(child_id)
warnings.add("thread_lineage_cycle")
continue
- if child_id in seen_thread_ids:
+ if child_id in sessions:
continue
path = _rollout_path(descendant["rollout_path"])
if path is None:
missing_thread_ids.add(child_id)
warnings.add("rollout_unavailable")
continue
- sessions.append(RolloutSession(child_id, parent_id, path))
- seen_thread_ids.add(child_id)
- return sessions, missing_thread_ids
+ sessions[child_id] = RolloutSession(child_id, parent_id, path)
+ return list(sessions.values()), missing_thread_ids
finally:
database.close()
diff --git a/plugins/codex-security/scripts/workbench_validation.py b/plugins/codex-security/scripts/workbench_validation.py
index 398f51faa..9fd6988ed 100644
--- a/plugins/codex-security/scripts/workbench_validation.py
+++ b/plugins/codex-security/scripts/workbench_validation.py
@@ -84,15 +84,6 @@ def _valid_legacy_scan_cost(cost: object) -> bool:
)
-def _valid_scan_token_counts(usage: object) -> bool:
- return (
- isinstance(usage, dict)
- and set(usage) == set(SCAN_USAGE_TOKEN_KEYS)
- and all(type(usage.get(key)) is int and usage[key] >= 0 for key in SCAN_USAGE_TOKEN_KEYS)
- and usage["cachedInputTokens"] + usage["cacheWriteInputTokens"] <= usage["inputTokens"]
- )
-
-
def _valid_measured_scan_usage(usage: object) -> bool:
if not isinstance(usage, dict):
return False
@@ -131,8 +122,10 @@ def _valid_measured_scan_usage(usage: object) -> bool:
}
if thread_count == 0 or not set(usage).issubset(allowed_keys):
return False
- counts = {key: usage.get(key) for key in SCAN_USAGE_TOKEN_KEYS}
- if not _valid_scan_token_counts(counts):
+ if (
+ not all(type(usage.get(key)) is int and usage[key] >= 0 for key in SCAN_USAGE_TOKEN_KEYS)
+ or usage["cachedInputTokens"] + usage["cacheWriteInputTokens"] > usage["inputTokens"]
+ ):
return False
missing = usage.get("missingThreadCount", 0)
if type(missing) is not int or missing < 0: