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(plugin): require thread attachment for legacy scan continuations (#2428)

Michael D'Angelofc51c1a42acd
diff --git a/plugins/codex-security/references/scan-runtime.md b/plugins/codex-security/references/scan-runtime.md
index 05a149f7d..2b7862643 100644
--- a/plugins/codex-security/references/scan-runtime.md
+++ b/plugins/codex-security/references/scan-runtime.md
@@ -6,6 +6,8 @@ When delegation is unavailable or a worker cannot start, continue the assigned r
For a host-backed Standard or diff scan, use its authoritative scan context and advance to `threat_model` before starting architecture mapping. The host still validates ownership, target, scope, authentication, and sandbox permissions. Deep Scan's coordinator owns its worker setup and progress; it does not depend on this thread's delegation tools.
+A scan from an older workbench may have no recorded owner. The workbench can still display it. A conversation can continue it once the host attaches its thread.
+
## Daybreak Access Advisory
Read `access-advisory.md` for the optional account advisory before the first substantive scan action. It does not gate the scan or change runtime setup.
diff --git a/plugins/codex-security/scripts/deep_scan_workbench.py b/plugins/codex-security/scripts/deep_scan_workbench.py
index 98c9999ff..0ea4c6a05 100644
--- a/plugins/codex-security/scripts/deep_scan_workbench.py
+++ b/plugins/codex-security/scripts/deep_scan_workbench.py
@@ -524,7 +524,7 @@ def existing_deep_scan_for_target(
SELECT scans.*
FROM scans
JOIN workspaces ON workspaces.id = scans.workspace_id
- WHERE workspaces.thread_id = ?
+ WHERE (workspaces.thread_id = ? OR workspaces.thread_id IS NULL)
AND COALESCE(scans.deep_scan_owner_thread_id, workspaces.thread_id) = ?
AND scans.target_path = ?
AND scans.scope = ?
@@ -576,6 +576,7 @@ def terminal_deep_scan_for_target_snapshot(
AND scans.target_inode = ?
AND scans.handoff_status = 'delivered'
AND scans.handoff_claim_token IS NULL
+ AND scans.continuation_thread_id IS NULL
AND workspaces.active_scan_id = scans.id
AND deep_scan_runs.status = 'succeeded'
AND deep_scan_runs.phase = 'terminal'
@@ -616,7 +617,9 @@ def begin_deep_scan_for_scan(
if (
candidate["mode"] == "deep"
and candidate["status"] == "running"
- and candidate["recipe_json"] is not None
+ and (
+ candidate["recipe_json"] is not None or candidate["continuation_thread_id"] == thread_id
+ )
and candidate["handoff_status"] == "delivered"
and candidate["deep_scan_owner_thread_id"] is None
and workspace["thread_id"] is None
@@ -628,17 +631,23 @@ def begin_deep_scan_for_scan(
)
timestamp = dependencies().now()
with connection:
- claimed_workspace = connection.execute(
- "UPDATE workspaces SET thread_id = ?, updated_at = ? "
- "WHERE id = ? AND thread_id IS NULL",
- (thread_id, timestamp, workspace["id"]),
- )
+ # Legacy scans can share a workspace. Only registered scans claim the workspace.
+ if candidate["recipe_json"] is not None:
+ claimed_workspace = connection.execute(
+ "UPDATE workspaces SET thread_id = ?, updated_at = ? "
+ "WHERE id = ? AND thread_id IS NULL",
+ (thread_id, timestamp, workspace["id"]),
+ )
+ if claimed_workspace.rowcount != 1:
+ raise SystemExit(
+ "A scan can only be orchestrated from its owning Codex thread."
+ )
claimed_scan = connection.execute(
"UPDATE scans SET deep_scan_owner_thread_id = ?, updated_at = ? "
"WHERE id = ? AND deep_scan_owner_thread_id IS NULL "
"AND handoff_status = 'delivered' AND handoff_claim_token IS ? "
- "AND (handoff_claim_token IS NOT NULL OR continuation_thread_id = ? "
- "OR (? IS NULL AND continuation_thread_id IS NULL))",
+ "AND (continuation_thread_id = ? OR (recipe_json IS NOT NULL AND "
+ "(handoff_claim_token IS NOT NULL OR (? IS NULL AND continuation_thread_id IS NULL))))",
(
thread_id,
timestamp,
@@ -648,7 +657,7 @@ def begin_deep_scan_for_scan(
model_thread_id,
),
)
- if claimed_workspace.rowcount != 1 or claimed_scan.rowcount != 1:
+ if claimed_scan.rowcount != 1:
raise SystemExit("A scan can only be orchestrated from its owning Codex thread.")
scan, _ = require_owned_scan(connection, scan_id, thread_id)
require_current_continuation(
@@ -733,7 +742,8 @@ def begin_deep_scan_for_target(
AND (deep_scan_runs.pending_target_project_path IS NULL
OR deep_scan_runs.pending_target_project_path = ?)
AND scans.recipe_json IS NULL
- AND scans.handoff_claim_token IS NULL AND deep_scan_runs.status = 'succeeded'
+ AND scans.handoff_claim_token IS NULL AND scans.continuation_thread_id IS NULL
+ AND deep_scan_runs.status = 'succeeded'
AND deep_scan_runs.discovery_user_context_json = ?
LIMIT 1
""",
diff --git a/plugins/codex-security/scripts/workbench/handoff.py b/plugins/codex-security/scripts/workbench/handoff.py
index 71b8c4980..2ad45be36 100644
--- a/plugins/codex-security/scripts/workbench/handoff.py
+++ b/plugins/codex-security/scripts/workbench/handoff.py
@@ -38,9 +38,8 @@ def require_tokenless_scan_owner(
"SELECT thread_id FROM workspaces WHERE id = ?", (scan["workspace_id"],)
).fetchone()[0]
if owner is None:
- # Only a completed import is intentionally ownerless among registered scans.
completed_import = scan["launch_kind"] == "import" and scan["status"] == "complete"
- if scan["recipe_json"] is not None and not completed_import:
+ if not completed_import:
raise SystemExit("This scan is awaiting its owning Codex thread.")
elif owner != model_thread_id:
raise SystemExit("This scan requires its owning Codex thread.")
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 2cdc36c9b..d1e787dc1 100644
--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
@@ -28,9 +28,11 @@ from workbench_test_support import (
create_workspace,
empty_target_paths,
empty_target_scan,
+ fail_scan,
get_scan,
initialize_git_repository,
load_script,
+ mark_deep_coordinator_succeeded,
run_workbench,
save_workspace,
scan_command,
@@ -2123,6 +2125,84 @@ def test_workbench_applies_shadowed_delivered_claim_cleanup_once(tmp_path: Path)
).fetchone() == ("2026-07-01T00:00:00Z", "current-claim")
+def test_legacy_delivered_scan_waits_for_its_thread(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setenv("CODEX_HOME", str(tmp_path / "codex-home"))
+ state_dir, target = empty_target_paths(tmp_path)
+ workspace = create_saved_workspace(state_dir, target, mode="deep")
+ previous_id = start_delivered_scan(state_dir, workspace["id"])["scanId"]
+ fail_scan(state_dir, previous_id, "Earlier scan failed")
+ scan_id = start_delivered_scan(state_dir, workspace["id"])["scanId"]
+ begin = ("--thread-id", "fixture-thread", "--workflow-version", "fixture")
+
+ assert get_scan(state_dir, scan_id)["scan"]["scanId"] == scan_id
+ for command, args in (
+ ("get-scan", ("--context-claim-token", "")),
+ ("update-progress", ("--phase", "threat_model")),
+ ("begin-deep-scan", begin),
+ ):
+ result = scan_command(
+ state_dir, command, scan_id, "--model-thread-id", "fixture-thread", *args, check=False
+ )
+ assert result["returncode"] != 0
+ assert "awaiting its owning Codex thread" in result["stderr"]
+
+ scan_command(state_dir, "set-scan-thread", scan_id, "--thread-id", "fixture-thread")
+ other = scan_command(
+ state_dir,
+ "begin-deep-scan",
+ scan_id,
+ "--thread-id",
+ "other",
+ "--model-thread-id",
+ "other",
+ check=False,
+ )
+ assert "requires its owning Codex thread" in other["stderr"]
+ updated = update_progress(
+ state_dir, scan_id, "--model-thread-id", "fixture-thread", "--phase", "threat_model"
+ )
+ assert updated["scan"]["progress"]["phase"] == "threat_model"
+ begun = scan_command(
+ state_dir, "begin-deep-scan", scan_id, "--model-thread-id", "fixture-thread", *begin
+ )
+ assert begun["deepScan"]["scanId"] == scan_id
+ rejoined = run_workbench(
+ state_dir,
+ "begin-deep-scan",
+ "--target-path",
+ str(target),
+ "--model-thread-id",
+ "fixture-thread",
+ *begin,
+ )
+ assert rejoined["startDisposition"] == "joined"
+ assert rejoined["deepScan"]["scanId"] == scan_id
+ read_context = ("--model-thread-id", "fixture-thread", "--context-claim-token", "")
+ assert get_scan(state_dir, scan_id, *read_context)["scan"]["scanId"] == scan_id
+ previous = get_scan(state_dir, previous_id, *read_context, check=False)
+ assert "awaiting its owning Codex thread" in previous["stderr"]
+
+ mark_deep_coordinator_succeeded(state_dir, scan_id, Path(begun["deepScan"]["scanDir"]))
+ replacement = run_workbench(
+ state_dir,
+ "begin-deep-scan",
+ "--target-path",
+ str(target),
+ "--thread-id",
+ "other",
+ "--model-thread-id",
+ "other",
+ "--user-context",
+ begun["deepScan"]["userContext"],
+ "--workflow-version",
+ "fixture",
+ )
+ assert replacement["startDisposition"] == "created"
+ assert replacement["deepScan"]["scanId"] != scan_id
+
+
@pytest.mark.parametrize(
"legacy_name",
("structured scan guidance context", "idempotent scan lifecycle requests"),