fix(deep-scan): report cost-limit exhaustion as stopped

This commit is contained in:
Aditya-linux
2026-11-10 00:03:50 +05:30
parent 7cc6bae500
commit 0b5ab78b22
6 changed files with 33 additions and 20 deletions
@@ -39,6 +39,7 @@ Producers should use UTC ISO 8601 timestamps in the native SDK or plugin format.
| `interrupted` | The scan stopped before normal completion because execution was interrupted; retained artifacts may be partial. |
Only `completed` supports a completed-scan conclusion. For every stopped outcome, consumers must preserve retained findings and review state while treating absence of findings as inconclusive.
Reaching a configured scan cost limit is a stopped outcome, including when Deep Scan discovery finished before the limit was observed. The sealed bundle uses `failed`, preserves validated findings and deferred candidates, and does not support a completed-scan conclusion.
Optional `scan.warnings` records run-level diagnostics available when the bundle is first sealed. SARIF exports include these messages alongside deferred-task reasons in `toolExecutionNotifications`; warnings do not change `executionSuccessful`, which follows `scan.status`. Missing or empty warnings add no notifications. Existing sealed bundles remain unchanged, and export or publication warnings discovered afterward remain separate operation diagnostics.
@@ -1091,7 +1091,7 @@ def finalize_scan_usage(connection: sqlite3.Connection, args: argparse.Namespace
def complete_budget_exhausted_scan(
connection: sqlite3.Connection, args: argparse.Namespace
) -> dict[str, Any]:
"""Complete a running CLI Deep Scan using the measured cost and saved results after its budget is exhausted."""
"""Stop a running CLI Deep Scan and preserve its results after its budget is exhausted."""
scan_id = require_uuid(args.scan_id, "scan-id")
cost_json = parse_scan_cost(args.cost_json)
if cost_json is None:
@@ -1147,7 +1147,14 @@ def complete_budget_exhausted_scan(
(json.dumps([*warnings, warning]), scan_id),
)
connection.commit()
return complete_scan_locked(connection, scan_id, None, cost_json)
failure = argparse.Namespace(
claim_token=None,
cost_json=cost_json,
message=warning,
model_thread_id=None,
scan_id=scan_id,
)
return saved_results.fail_scan_locked(_WORKBENCH_DB_CONTEXT, connection, failure)
def budget_exhausted_candidates(scan: sqlite3.Row, scan_dir: Path) -> list[dict[str, Any]]:
@@ -205,7 +205,7 @@ def test_cost_limit_increases_are_saved_without_replacing_the_scan_recipe(
scan_command(state_dir, "set-scan-cost-limit", scan_id, "--max-cost-usd", str(limit))
saved = scan_command(state_dir, "get-scan-recipe", scan_id)["recipe"]
assert saved == {**original, "maxCostUsd": 0.006}
assert complete_budget_scan(state_dir, scan_id)["scan"]["progress"]["status"] == "complete"
assert complete_budget_scan(state_dir, scan_id)["scan"]["progress"]["status"] == "failed"
stopped = scan_command(
state_dir, "set-scan-cost-limit", scan_id, "--max-cost-usd", "1", check=False
)
@@ -242,13 +242,14 @@ def test_budget_exhaustion_preserves_unvalidated_discovery_as_deferred_work(
completed = complete_budget_scan(state_dir, scan_id, warning=warning)["scan"]
assert completed["progress"]["status"] == "complete"
assert completed["progress"]["status"] == "failed"
assert completed["findings"] == []
assert completed["cost"] == BUDGET_COST
assert completed["warnings"] == [warning]
assert get_scan(state_dir, scan_id)["scan"]["warnings"] == [warning]
assert ledger.read_bytes() == original_ledger
manifest = json.loads((scan_dir / "scan-manifest.json").read_text())
assert manifest["scan"]["status"] == "failed"
assert manifest["scan"]["target"]["displayName"] == target.name
assert manifest["scan"]["target"]["targetId"] == stable_target_id(target)
assert manifest["scan"]["sealedAt"]
@@ -283,7 +284,7 @@ def test_budget_exhaustion_preserves_authored_validated_findings(tmp_path: Path)
completed = complete_budget_scan(state_dir, scan_id)["scan"]
assert completed["progress"]["status"] == "complete"
assert completed["progress"]["status"] == "failed"
assert completed["findingCount"] == 1
assert "Unsafe archive extraction" in completed["findings"][0]["title"]
coverage = read_review_state(state_dir, scan_dir)
@@ -329,7 +330,7 @@ def test_budget_exhaustion_preserves_existing_candidate_decisions(
completed = complete_budget_scan(state_dir, scan_id)["scan"]
assert completed["progress"]["status"] == "complete"
assert completed["progress"]["status"] == "failed"
assert completed["findings"] == []
coverage = read_review_state(state_dir, scan_dir)
@@ -401,7 +402,7 @@ def test_budget_exhaustion_preserves_existing_deferred_work_with_one_trusted_mar
completed = complete_budget_scan(state_dir, scan_id)["scan"]
assert completed["progress"]["status"] == "complete"
assert completed["progress"]["status"] == "failed"
preserved = read_review_state(state_dir, scan_dir)
assert preserved["deferred"][0] == existing
assert len(preserved["deferred"]) == 1 + marker_added
@@ -418,7 +419,7 @@ def test_budget_exhaustion_preserves_capped_discovery(tmp_path: Path) -> None:
completed = complete_budget_scan(state_dir, scan_id)["scan"]
assert completed["progress"]["status"] == "complete"
assert completed["progress"]["status"] == "failed"
assert read_review_state(state_dir, scan_dir)["deferred"][0]["id"] == "candidate-1"
@@ -427,7 +428,7 @@ def test_budget_exhaustion_with_no_candidates_is_honestly_partial(tmp_path: Path
completed = complete_budget_scan(state_dir, scan_id)["scan"]
assert completed["progress"]["status"] == "complete"
assert completed["progress"]["status"] == "failed"
assert completed["findings"] == []
coverage = read_review_state(state_dir, scan_dir)
@@ -444,7 +445,7 @@ def test_budget_exhaustion_preserves_scoped_path_inventory(tmp_path: Path) -> No
completed = complete_budget_scan(state_dir, scan_id)["scan"]
assert completed["progress"]["status"] == "complete"
assert completed["progress"]["status"] == "failed"
coverage = read_review_state(state_dir, scan_dir)
assert coverage["mode"] == "scoped_path"
assert coverage["inventoryStrategy"] == "scoped_path"
@@ -524,7 +525,7 @@ def test_budget_exhaustion_preserves_out_of_scope_supporting_evidence(tmp_path:
completed = complete_budget_scan(state_dir, scan_id)["scan"]
assert completed["progress"]["status"] == "complete"
assert completed["progress"]["status"] == "failed"
coverage = read_review_state(state_dir, scan_dir)
assert coverage["deferred"][0]["paths"] == ["app.py", "shared/control.py"]
@@ -543,7 +544,7 @@ def test_budget_exhaustion_preserves_unicode_line_separator_inventory(tmp_path:
completed = complete_budget_scan(state_dir, scan_id)["scan"]
assert completed["progress"]["status"] == "complete"
assert completed["progress"]["status"] == "failed"
coverage = read_review_state(state_dir, scan_dir)
assert coverage["deferred"][0]["paths"] == ["dir\u2028name.py"]
@@ -2202,7 +2202,7 @@ def test_budget_exhaustion_preserves_validated_standard_results_without_candidat
warning,
)["scan"]
assert completed["progress"]["status"] == "complete"
assert completed["progress"]["status"] == "failed"
assert completed["findingCount"] == 1
assert "Unsafe archive extraction" in completed["findings"][0]["title"]
assert completed["warnings"] == [warning]
@@ -2217,7 +2217,7 @@ def test_budget_exhaustion_preserves_validated_standard_results_without_candidat
sarif = json.loads((scan_dir / "exports/results.sarif").read_text())
assert sarif["runs"][0]["invocations"] == [
{
"executionSuccessful": True,
"executionSuccessful": False,
"toolExecutionNotifications": [{"level": "warning", "message": {"text": warning}}],
}
]
+9 -5
View File
@@ -1218,11 +1218,15 @@ Verifying compressed `.jsonl.zst` sessions requires Python 3.14 or later, or
Missing or unreadable usage fails budget verification. Without a limit, usage
reporting remains best effort.
When Deep discovery has finished at a cost stop and usage is verified, the scan returns a sealed
report and retains unvalidated candidates as follow-up work. The saved report and
the manifest's `scan.scope.limitations` record that validation was deferred.
Reaching a configured limit with a valid finalized report returns `0` by default,
or `1` if findings meet a configured failure threshold.
When Deep discovery has finished at a cost stop and usage is verified, the scan
retains a sealed report and unvalidated candidates as follow-up work. The saved
report and the manifest's `scan.scope.limitations` record that validation was
deferred. The manifest uses `scan.status: "failed"`, SARIF reports
`executionSuccessful: false`, and the CLI emits `scan.failed` with
`classification="cost_limit_exceeded"` and exits `2`. This is the same
machine-readable stopped outcome used when the limit is reached before discovery
finishes; retained findings remain available, but absent findings are
inconclusive.
When a worker turn fails with a known cybersecurity policy refusal or an
exhausted usage limit, Deep Scan stops retries and replacements, cancels active
@@ -571,7 +571,7 @@ describe("deep scan workbench ownership", () => {
progress: { status: string };
warnings: string[];
};
expect(scan.progress.status).toBe("complete");
expect(scan.progress.status).toBe("failed");
expect(scan.warnings).toContain(warning);
const findings = JSON.parse(
await readFile(join(scanDir, "findings.json"), "utf8"),