Fix duplicate tool call handling: move tool_executed flag and reset parser

- Move tool_executed = true after duplicate check to prevent auto-continue
  from triggering when only duplicate tools were detected
- Reset parser state when duplicate detected to clear any partial/polluted
  state from LLM stuttering or example tool calls in markdown blocks
This commit is contained in:
Dhanji R. Prasanna
2025-12-26 11:55:57 +11:00
parent 46611d9e13
commit 666be4ff40

View File

@@ -3831,12 +3831,9 @@ impl<W: UiWriter> Agent<W> {
for (tool_call, duplicate_type) in deduplicated_tools { for (tool_call, duplicate_type) in deduplicated_tools {
debug!("Processing completed tool call: {:?}", tool_call); debug!("Processing completed tool call: {:?}", tool_call);
// Mark that we detected a tool call - this prevents content from being printed // If it's a duplicate, log it and skip - don't set tool_executed!
// even if the tool is skipped as a duplicate // Setting tool_executed for duplicates would trigger auto-continue
tool_executed = true; // even when no actual tool execution occurred.
// If it's a duplicate, log it and return a warning
if let Some(dup_type) = &duplicate_type { if let Some(dup_type) = &duplicate_type {
// Log the duplicate with red prefix // Log the duplicate with red prefix
let prefixed_tool_name = let prefixed_tool_name =
@@ -3852,9 +3849,18 @@ impl<W: UiWriter> Agent<W> {
let mut modified_tool_call = tool_call.clone(); let mut modified_tool_call = tool_call.clone();
modified_tool_call.tool = prefixed_tool_name; modified_tool_call.tool = prefixed_tool_name;
debug!("{}", warning_msg); debug!("{}", warning_msg);
// Reset the parser to clear any partial/polluted state.
// This prevents "example" tool calls in markdown or LLM stuttering
// from polluting subsequent parsing.
parser.reset();
continue; // Skip execution of duplicate continue; // Skip execution of duplicate
} }
// Mark that we're executing a tool (only for non-duplicates)
tool_executed = true;
// Check if we should auto-compact at 90% BEFORE executing the tool // Check if we should auto-compact at 90% BEFORE executing the tool
// We need to do this before any borrows of self // We need to do this before any borrows of self
if self.auto_compact && self.context_window.percentage_used() >= 90.0 { if self.auto_compact && self.context_window.percentage_used() >= 90.0 {