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.
This commit is contained in:
Dhanji R. Prasanna
2025-12-24 15:50:30 +11:00
parent cd64ebbf87
commit fe96969adb

View File

@@ -4550,20 +4550,20 @@ impl<W: UiWriter> Agent<W> {
}
// 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 {