From 2401ae6485e86c1ad2a976961640264d018dc0dd Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Tue, 6 Oct 2026 00:35:53 +0200 Subject: [PATCH] fix(triage): stop asking a model that is down (#462) The per-id retries of #472 ask about a finding up to three times, the critical/high ones on their own. A model that accepts requests and never answers would hold each call for the client's 300 s timeout: up to 720 calls instead of 60. When every call in flight fails at once, the model is taken as down; what it answered so far is kept, the rest is recorded as "skipped: no answer". An unreadable answer still counts as an answer and is asked about again. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB --- compliance-agent/src/llm/triage.rs | 68 ++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/compliance-agent/src/llm/triage.rs b/compliance-agent/src/llm/triage.rs index 5d71025..14d8d75 100644 --- a/compliance-agent/src/llm/triage.rs +++ b/compliance-agent/src/llm/triage.rs @@ -320,14 +320,19 @@ fn chunk_prompt( /// /// # Returns /// -/// The verdicts the answer holds, by position in `findings`; none when the -/// call failed or its answer could not be read. +/// The verdicts the answer holds, by position in `findings`; none when its +/// answer could not be read. +/// +/// # Errors +/// +/// [`ModelDown`] when the call itself failed: the model could not be reached, +/// returned an error or gave no answer at all. async fn triage_chunk( model: &M, findings: &[Finding], chunk: &[usize], graph_context: Option<&GraphContext>, -) -> Vec<(usize, TriageResult)> { +) -> Result, ModelDown> { let user_prompt = chunk_prompt(findings, chunk, graph_context); let answer = match model.answer(TRIAGE_SYSTEM_PROMPT, &user_prompt).await { Ok(answer) => answer, @@ -337,7 +342,7 @@ async fn triage_chunk( this was the last try", chunk.len() ); - return Vec::new(); + return Err(ModelDown); } }; let verdicts = match parse_triage_response(&answer.content) { @@ -358,7 +363,7 @@ async fn triage_chunk( tracing::debug!( "Unreadable batch triage response (first {RESPONSE_SAMPLE_CHARS} chars): {sample}" ); - return Vec::new(); + return Ok(Vec::new()); } }; let fingerprints: Vec<&str> = chunk @@ -383,9 +388,13 @@ async fn triage_chunk( }, ); } - matched + Ok(matched) } +/// A triage call failed as a call (not an unreadable answer). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct ModelDown; + /// The verdict for each finding of a chunk (`fingerprints`, in chunk order), /// matched by the `id` the model echoed: the finding's number in the chunk /// (from 1), or its fingerprint. Only when no verdict carries an id that names @@ -439,7 +448,7 @@ async fn ask_in_chunks( size: usize, graph_context: Option<&GraphContext>, calls: &mut usize, -) -> Vec<(usize, TriageResult)> { +) -> (Vec<(usize, TriageResult)>, Option) { let chunks: Vec<&[usize]> = ask.chunks(size.max(1)).collect(); let mut answered = Vec::new(); for group in chunks.chunks(TRIAGE_CONCURRENCY) { @@ -450,9 +459,15 @@ async fn ask_in_chunks( .map(|chunk| triage_chunk(model, findings, chunk, graph_context)), ) .await; - answered.extend(answers.into_iter().flatten()); + // Every call at once failed: the model is down (or hangs until the + // client's timeout), not cutting answers short. Asking about the + // rest, chunk by chunk and then one by one, would only take hours. + if answers.iter().all(Result::is_err) { + return (answered, Some(ModelDown)); + } + answered.extend(answers.into_iter().flatten().flatten()); } - answered + (answered, None) } /// The tries of the triage: the chunk size of each, and which findings still @@ -466,7 +481,8 @@ const TRIES: [(usize, Severity); 3] = [ ]; /// Ask the model about every finding at `sent`, and again by id about those it -/// gave no verdict (see [`TRIES`]). +/// gave no verdict (see [`TRIES`]). Once every call in flight fails at once, +/// the model is taken as down and nothing more is asked. /// /// # Returns /// @@ -495,10 +511,18 @@ async fn ask_all( size ); } - for (i, verdict) in ask_in_chunks(model, findings, &ask, *size, graph_context, calls).await - { + let (answered, down) = + ask_in_chunks(model, findings, &ask, *size, graph_context, calls).await; + for (i, verdict) in answered { verdicts.entry(i).or_insert(verdict); } + if down.is_some() { + tracing::warn!( + "LLM triage: every call at once failed; the model is not answering, so the \ + findings without a verdict are not asked again" + ); + break; + } } verdicts } @@ -1525,8 +1549,26 @@ mod tests { .all(|f| f.triage_action.as_deref() == Some(SKIPPED_ACTION) && f.status == FindingStatus::Open)); assert_eq!(summary.skipped_no_answer_severe, 3); - // One chunk, its retry, then each critical finding on its own. + // The first call fails: the model is down, nothing is asked again. + assert_eq!(summary.calls, 1); + } + + #[tokio::test] + async fn a_model_that_goes_down_midway_keeps_what_it_answered() { + let mut findings: Vec = (1..=45).map(|n| numbered(n, Severity::Medium)).collect(); + let full: String = format!( + "[{}]", + (1..=10) + .map(|n| format!(r#"{{"id":{n},"action":"confirm","confidence":8}}"#)) + .collect::>() + .join(",") + ); + // The first four chunks are answered, then the model is gone. + let model = FakeModel::scripted(&[&full, &full, &full, &full]); + let summary = triage_findings(&model, &mut findings, None).await; assert_eq!(summary.calls, 5); + assert_eq!(summary.confirmed, 40); + assert_eq!(summary.skipped_no_answer, 5); } #[tokio::test] -- 2.54.1