diff --git a/rust/crates/runtime/src/conversation.rs b/rust/crates/runtime/src/conversation.rs index 35c0f73f..ea50e3bd 100644 --- a/rust/crates/runtime/src/conversation.rs +++ b/rust/crates/runtime/src/conversation.rs @@ -342,6 +342,7 @@ where let mut tool_results = Vec::new(); let mut prompt_cache_events = Vec::new(); let mut iterations = 0; + let mut auto_compaction = None; loop { iterations += 1; @@ -397,6 +398,12 @@ where .map_err(|error| RuntimeError::new(error.to_string()))?; assistant_messages.push(assistant_message); + // Run auto-compaction check before next API call, including on the terminal + // (no-tool) iteration, to prevent unbounded session growth (#3106). + if let Some(compaction) = self.maybe_auto_compact() { + auto_compaction = Some(compaction); + } + if pending_tool_uses.is_empty() { break; } @@ -503,8 +510,6 @@ where } } - let auto_compaction = self.maybe_auto_compact(); - let summary = TurnSummary { assistant_messages, tool_results,