From 20acca87b8253031d5cc7fee0989afc244b0dd60 Mon Sep 17 00:00:00 2001 From: CyberSecurityUP Date: Wed, 18 Nov 2026 22:41:23 -0300 Subject: [PATCH] fix(harness): raise Claude max_tokens to 16k and add truncation detection Claude models were capped at 4096 output tokens, causing agent ReAct chains to consume the entire budget before writing JSON findings. All findings were silently lost to truncation. Changes: - max_tokens_for() returns 16384 for claude-* models - max_tokens_for() promoted to pub(crate) for cross-module use - models.rs warns on stderr when finish_reason=length with partial content - pipeline.rs exploit loop detects output length near token ceiling and warns operator about likely truncation when 0 findings extracted - New test: claude_models_get_extended_output asserts 16k --- neurosploit-rs/crates/harness/src/models.rs | 16 +++++++++++----- neurosploit-rs/crates/harness/src/pipeline.rs | 12 ++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/neurosploit-rs/crates/harness/src/models.rs b/neurosploit-rs/crates/harness/src/models.rs index 4d9ae72..af1572b 100644 --- a/neurosploit-rs/crates/harness/src/models.rs +++ b/neurosploit-rs/crates/harness/src/models.rs @@ -277,11 +277,8 @@ impl ChatClient { return Err(anyhow!("{} returned no choices: {}", p.key, truncate(err, 200))); } let content = extract_message_text(&choice["message"]); + let finish = choice["finish_reason"].as_str().unwrap_or("unknown"); if content.trim().is_empty() { - // 2xx but no usable text. The usual cause is a reasoning model that - // spent the whole token budget on its reasoning stream and was cut - // off (finish_reason="length") before emitting the answer. - let finish = choice["finish_reason"].as_str().unwrap_or("unknown"); let hint = if finish == "length" { " — raised max_tokens may help, or the model is reasoning-heavy for this prompt size" } else { @@ -292,6 +289,15 @@ impl ChatClient { p.key, m.model )); } + // Output hit token limit with partial content — the JSON findings + // block is likely truncated. Warn so the operator knows findings may + // be lost, and the truncation-repair parser can attempt recovery. + if finish == "length" { + eprintln!( + "[models] ⚠ agent {}: output truncated (finish_reason=length, {} chars, max_tokens={}). JSON repair will attempt recovery.", + label, content.len(), max_tokens_for(&m.model) + ); + } Ok(content) } } @@ -1023,7 +1029,7 @@ fn temperature_for() -> f32 { /// `finish_reason: "length"` with an empty `content`, surfacing as the /// "empty content" error. Give those families more headroom; leave the rest at /// the cheaper default. Override for all models with `NEUROSPLOIT_MAX_TOKENS`. -fn max_tokens_for(model: &str) -> u32 { +pub(crate) fn max_tokens_for(model: &str) -> u32 { if let Some(v) = std::env::var("NEUROSPLOIT_MAX_TOKENS") .ok() .and_then(|v| v.trim().parse::().ok()) diff --git a/neurosploit-rs/crates/harness/src/pipeline.rs b/neurosploit-rs/crates/harness/src/pipeline.rs index 98a3671..4205ffc 100644 --- a/neurosploit-rs/crates/harness/src/pipeline.rs +++ b/neurosploit-rs/crates/harness/src/pipeline.rs @@ -1697,6 +1697,18 @@ pub async fn run(mut cfg: RunConfig, lib: &Library, pool: &ModelPool, tx: Sender Ok((m, text)) => { let f = extract_findings(&text, &ag.name); let _ = txc.send(format!("exploit {} via {} → {} candidate(s)", ag.name, m.label(), f.len())).await; + // Detect output truncation: if text length in chars is + // near the max_tokens limit (tokens ≈ 3-4 chars), the + // JSON findings block was likely cut off mid-stream. + let max_tok = crate::models::max_tokens_for(&m.model) as usize; + let char_floor = max_tok * 3; // conservative: 3 chars/token + if text.len() >= char_floor && f.is_empty() { + let _ = txc.send(format!( + "⚠ agent {} output length ({} chars) hit token ceiling (~{} tokens). \ + JSON findings likely truncated. Consider raising NEUROSPLOIT_MAX_TOKENS.", + ag.name, text.len(), max_tok + )).await; + } // "no findings" is a RESULT, not a parse failure. Models // wrap the empty array in a ```json fence, so comparing the // raw text to "[]" reported every honest negative as