diff --git a/src/agent/mod.rs b/src/agent/mod.rs index b9b1d9b..d517534 100644 --- a/src/agent/mod.rs +++ b/src/agent/mod.rs @@ -155,6 +155,9 @@ pub struct AgentState { pub generation: u64, pub memory_scoring_in_flight: bool, pub active_tools: tools::ActiveTools, + /// Forked agents should not compact on overflow — it blows the + /// KV cache prefix and evicts the step prompts. + pub no_compact: bool, pub changed: Arc, } @@ -214,6 +217,7 @@ impl Agent { generation: 0, memory_scoring_in_flight: false, active_tools, + no_compact: false, changed: Arc::new(tokio::sync::Notify::new()), }), }); @@ -249,6 +253,7 @@ impl Agent { generation: 0, memory_scoring_in_flight: false, active_tools: tools::ActiveTools::new(), + no_compact: true, changed: Arc::new(tokio::sync::Notify::new()), }), }) @@ -354,12 +359,17 @@ impl Agent { // Check for stream/parse errors match parser_handle.await { Ok(Err(e)) => { - if context::is_context_overflow(&e) && overflow_retries < 2 { - overflow_retries += 1; - agent.state.lock().await.notify( - format!("context overflow — retrying ({}/2)", overflow_retries)); - agent.compact().await; - continue; + if context::is_context_overflow(&e) { + if agent.state.lock().await.no_compact { + return Err(e); + } + if overflow_retries < 2 { + overflow_retries += 1; + agent.state.lock().await.notify( + format!("context overflow — retrying ({}/2)", overflow_retries)); + agent.compact().await; + continue; + } } return Err(e); } diff --git a/src/agent/oneshot.rs b/src/agent/oneshot.rs index b228cb8..bc05d0a 100644 --- a/src/agent/oneshot.rs +++ b/src/agent/oneshot.rs @@ -186,8 +186,14 @@ impl AutoAgent { for _ in 0..max_turns { self.turn += 1; - let result = Agent::turn(backend.0.clone()).await - .map_err(|e| format!("{}: {}", self.name, e))?; + let result = match Agent::turn(backend.0.clone()).await { + Ok(r) => r, + Err(e) if super::context::is_context_overflow(&e) => { + dbglog!("[auto] {} context full, stopping gracefully", self.name); + return Ok(String::new()); + } + Err(e) => return Err(format!("{}: {}", self.name, e)), + }; if result.had_tool_calls { continue;