fix(triage): every critical/high finding gets a verdict, stored on rescans too (#462) #472

Merged
sharang merged 1 commits from fix/462-triage-coverage into main 2026-10-05 22:29:45 +00:00
Owner

Closes #462.

Root cause

1. Batches answered only in part. Triage sent 30 findings per call, and chat() caps a call at 4096 tokens. A reasoning model spends that budget on its thinking and the answer. Each verdict was also asked to echo the 64-character fingerprint, plus a rationale and a remediation. So the JSON array was cut off after a few verdicts (finish_reason: length). The parser kept the complete leading verdicts ("answered 2 of 28"). #457 retries only when the answer is empty, so a truncated non-empty answer went through as it was. Verdicts were matched to findings by position, and the echoed id was requested but ignored. So if the model left a finding out, every later verdict landed on the wrong finding.

2. Triage results not persisted. RunFindings::write_back stored triage_action/triage_rationale/severity/status only on rows this run inserted. A row an earlier run had stored (a rescan, or a scan #459 restarted after stages 1–4 had already saved its findings) got only its control_refs. The triage verdicts for those rows were computed and then thrown away. That matches the pilot data: 74 rows null, and 1 confirm, which was the one row first seen in that run.

3. "passed confidence threshold". Nothing was dropped. Dismissed findings and confirmed ones below confidence 3 became needs_review, still stored (#367). But findings the model never answered were set to triaged and counted as passed, so the log line was misleading.

Findings entering triage vs stored (e.g. 208 → 120): this is not triage and not the threshold. Storage keeps one row per fingerprint and does not re-insert rows an earlier run stored. all_findings contains copies: gitleaks detect walks the git history and reports the same secret at the same file:line once per commit, and a rule can match the same line twice. The dedup is intended. Triage simply ran on the copies too.

Fix

  • 10 findings per call with a 12k-token budget (below #457's 16k retry, so that still applies). Ids are the finding's number within the chunk.
  • Verdicts are matched by id (number or fingerprint). Position is used only when the answer has no ids at all. An id-less verdict in an answer that has ids is ignored, not guessed.
  • Missing ids are asked again in chunks half the size. Then each critical/high finding still missing is asked on its own.
  • One call per fingerprint; copies get the same verdict.
  • Low/info findings are not sent. They are recorded as triage_action: "skipped", triage_rationale: "skipped: low …". The same applies to skipped: over cap and skipped: no answer. Skipped findings keep their status (open), where previously they were wrongly set to triaged.
  • write_back now also stores triage on earlier rows whose status is still undecided (open/triaged/needs_review). A person's false_positive/accepted_risk/ignored/wrong_mapping is left untouched. A skipped result never overwrites an earlier real verdict. Status is still written only onto open rows.
  • One summary line per scan replaces "Triaged: N findings passed confidence threshold":
    LLM triage: N findings (U distinct); triaged T (confirmed …, downgraded …, upgraded …, dismissed … and low confidence … kept as needs review); skipped S (low …, over cap …, no answer … of which … critical/high); C LLM calls
  • Dismissed findings stay needs_review, not false_positive. false_positive is documented as a person's decision (#367).

Re-triage of existing untriaged findings

With the persistence fix, the next scan of a repo re-triages its stored findings and saves the verdicts (any row still open/triaged/needs_review). No flag is needed. An admin endpoint for re-triage without a rescan is not part of this PR. It would need tenant-db resolution, the LLM client and a checkout for the surrounding-code context. It could select triage_action null or skipped: no answer and reuse triage_findings + row_writes.

Tests

The triage now runs against a TriageModel trait, so tests use a fake model:

  • a partial (cut-off) answer, then a retry that asks only about the missing ids
  • malformed chunks, then the high finding asked on its own; the medium one is recorded as skipped: no answer
  • the model is down: every finding is kept and marked skipped, with the expected number of calls
  • the threshold: confirm/low confidence/dismiss/test-code adjustment, so triaged/needs_review with the action and rationale kept and nothing removed
  • lows are skipped and copies share one verdict; the summary line is checked
  • id matching: out of order, a finding left out, no ids
  • persistence (row_writes): new rows, earlier undecided rows, a skip never replacing a verdict, control refs only

cargo fmt, cargo clippy -p compliance-agent --all-targets -- -D warnings and cargo test -p compliance-agent --lib (630 passed) all pass.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB

Closes #462. ## Root cause **1. Batches answered only in part.** Triage sent 30 findings per call, and `chat()` caps a call at 4096 tokens. A reasoning model spends that budget on its thinking *and* the answer. Each verdict was also asked to echo the 64-character fingerprint, plus a rationale and a remediation. So the JSON array was cut off after a few verdicts (`finish_reason: length`). The parser kept the complete leading verdicts ("answered 2 of 28"). #457 retries only when the answer is *empty*, so a truncated non-empty answer went through as it was. Verdicts were matched to findings **by position**, and the echoed id was requested but ignored. So if the model left a finding out, every later verdict landed on the wrong finding. **2. Triage results not persisted.** `RunFindings::write_back` stored `triage_action`/`triage_rationale`/severity/status only on rows **this run inserted**. A row an earlier run had stored (a rescan, or a scan #459 restarted after stages 1–4 had already saved its findings) got only its `control_refs`. The triage verdicts for those rows were computed and then thrown away. That matches the pilot data: 74 rows null, and 1 `confirm`, which was the one row first seen in that run. **3. "passed confidence threshold".** Nothing was dropped. Dismissed findings and confirmed ones below confidence 3 became `needs_review`, still stored (#367). But findings the model never answered were set to `triaged` and **counted as passed**, so the log line was misleading. **Findings entering triage vs stored (e.g. 208 → 120):** this is not triage and not the threshold. Storage keeps one row per fingerprint and does not re-insert rows an earlier run stored. `all_findings` contains copies: gitleaks `detect` walks the git history and reports the same secret at the same file:line once per commit, and a rule can match the same line twice. The dedup is intended. Triage simply ran on the copies too. ## Fix - 10 findings per call with a 12k-token budget (below #457's 16k retry, so that still applies). Ids are the finding's number within the chunk. - Verdicts are **matched by id** (number or fingerprint). Position is used only when the answer has no ids at all. An id-less verdict in an answer that has ids is ignored, not guessed. - Missing ids are asked again in chunks half the size. Then each **critical/high** finding still missing is asked on its own. - One call per fingerprint; copies get the same verdict. - Low/info findings are not sent. They are recorded as `triage_action: "skipped"`, `triage_rationale: "skipped: low …"`. The same applies to `skipped: over cap` and `skipped: no answer`. Skipped findings keep their status (`open`), where previously they were wrongly set to `triaged`. - `write_back` now also stores triage on **earlier rows** whose status is still undecided (`open`/`triaged`/`needs_review`). A person's false_positive/accepted_risk/ignored/wrong_mapping is left untouched. A `skipped` result never overwrites an earlier real verdict. Status is still written only onto `open` rows. - One summary line per scan replaces "Triaged: N findings passed confidence threshold": `LLM triage: N findings (U distinct); triaged T (confirmed …, downgraded …, upgraded …, dismissed … and low confidence … kept as needs review); skipped S (low …, over cap …, no answer … of which … critical/high); C LLM calls` - Dismissed findings stay `needs_review`, not `false_positive`. `false_positive` is documented as a person's decision (#367). ## Re-triage of existing untriaged findings With the persistence fix, **the next scan of a repo re-triages its stored findings and saves the verdicts** (any row still open/triaged/needs_review). No flag is needed. An admin endpoint for re-triage without a rescan is not part of this PR. It would need tenant-db resolution, the LLM client and a checkout for the surrounding-code context. It could select `triage_action` null or `skipped: no answer` and reuse `triage_findings` + `row_writes`. ## Tests The triage now runs against a `TriageModel` trait, so tests use a fake model: - a partial (cut-off) answer, then a retry that asks only about the missing ids - malformed chunks, then the high finding asked on its own; the medium one is recorded as `skipped: no answer` - the model is down: every finding is kept and marked skipped, with the expected number of calls - the threshold: confirm/low confidence/dismiss/test-code adjustment, so `triaged`/`needs_review` with the action and rationale kept and nothing removed - lows are skipped and copies share one verdict; the summary line is checked - id matching: out of order, a finding left out, no ids - persistence (`row_writes`): new rows, earlier undecided rows, a skip never replacing a verdict, control refs only `cargo fmt`, `cargo clippy -p compliance-agent --all-targets -- -D warnings` and `cargo test -p compliance-agent --lib` (630 passed) all pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
sharang added 1 commit 2026-10-05 22:18:59 +00:00
fix(triage): every critical/high finding gets a verdict, stored on rescans too (#462)
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 9m37s
9f298a60e6
The LLM triage of the first pilot scans left 74 of 75 findings without a
triage_action. Three causes:

- Partial answers: 30 findings per call with a 4096-token budget that a
  reasoning model spends on thinking and the answer together. The answer
  was cut off after a few verdicts; #457 only retries an empty answer.
  Verdicts were also matched by position and the echoed id was ignored,
  so a finding the model left out shifted every later verdict.
- Not persisted: write_back stored triage fields only on rows this run
  inserted. A rescan (or a scan restarted after stages 1-4 had saved its
  findings) triaged them, then dropped the result.
- Unanswered findings were marked "triaged" and counted as "passed".

Now:
- 10 findings per call with a 12k budget (below #457's retry budget);
  verdicts matched by id (the finding's number in the chunk, or its
  fingerprint), by position only when the answer carries no ids
- findings without a verdict are asked again by id in chunks half the
  size, then each critical/high one on its own
- one call per fingerprint: copies (gitleaks reports a secret once per
  commit) share the verdict
- low/info findings are not sent; recorded as triage_action "skipped",
  rationale "skipped: low"; likewise "skipped: over cap" and "skipped:
  no answer". Skipped findings keep their status (open).
- write_back stores triage on rows an earlier run stored while nobody
  has decided about them (open/triaged/needs_review); a skip never
  replaces an earlier verdict; status still only onto open rows
- one summary line per scan replaces "N findings passed confidence
  threshold": triaged by outcome, skipped by reason, LLM calls
- dismissed and low-confidence findings stay as needs_review (#367):
  nothing is dropped

Tests drive the triage with a fake model: partial answer then retry by
id, malformed chunks, model down, threshold, lows and copies, and the
stored updates for new and earlier rows.

Closes #462

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
sharang merged commit 6f2e278969 into main 2026-10-05 22:29:45 +00:00
Sign in to join this conversation.