From fe96969adbaae8e4aff064f1ca14a84482a6274d Mon Sep 17 00:00:00 2001 From: "Dhanji R. Prasanna" Date: Wed, 24 Dec 2025 15:50:30 +1100 Subject: [PATCH] Fix auto-continue bug: ensure assistant message before continue prompt The auto-continue logic was adding User continue prompts without first adding an Assistant message when the LLM returned an empty response. This caused consecutive User messages in the conversation history, which confused the LLM and caused it to return more empty responses. The fix ensures an Assistant message is always added before the continue prompt, using '[empty response]' as a placeholder when the LLM returned nothing substantive. This maintains proper User/Assistant alternation. --- crates/g3-core/src/lib.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/g3-core/src/lib.rs b/crates/g3-core/src/lib.rs index 6ff44f1..7e0a8a5 100644 --- a/crates/g3-core/src/lib.rs +++ b/crates/g3-core/src/lib.rs @@ -4550,20 +4550,20 @@ impl Agent { } // Add any text response to context before prompting for continuation - if has_response { - let response_text = if !current_response.is_empty() { - current_response.clone() - } else { - full_response.clone() - }; - if !response_text.trim().is_empty() { - let assistant_msg = Message::new( - MessageRole::Assistant, - response_text.trim().to_string(), - ); - self.context_window.add_message(assistant_msg); - } - } + // IMPORTANT: We must always add an assistant message to maintain User/Assistant alternation + // Without this, consecutive User messages confuse the LLM and cause empty responses + let response_text = if !current_response.is_empty() { + current_response.clone() + } else if !full_response.is_empty() { + full_response.clone() + } else { + "[empty response]".to_string() + }; + let assistant_msg = Message::new( + MessageRole::Assistant, + if response_text.trim().is_empty() { "[empty response]".to_string() } else { response_text.trim().to_string() }, + ); + self.context_window.add_message(assistant_msg); // Add a follow-up message asking for continuation let continue_prompt = if has_incomplete_tool_call {