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 {
debug!("Processing completed tool call: {:?}", tool_call);
// Mark that we detected a tool call - this prevents content from being printed
// even if the tool is skipped as a duplicate
tool_executed = true;
// If it's a duplicate, log it and return a warning
// If it's a duplicate, log it and skip - don't set tool_executed!
// Setting tool_executed for duplicates would trigger auto-continue
// even when no actual tool execution occurred.
if let Some(dup_type) = &duplicate_type {
// Log the duplicate with red prefix
let prefixed_tool_name =
@@ -3852,9 +3849,18 @@ impl<W: UiWriter> Agent<W> {
let mut modified_tool_call = tool_call.clone();
modified_tool_call.tool = prefixed_tool_name;
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
}
// 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
// We need to do this before any borrows of self
if self.auto_compact && self.context_window.percentage_used() >= 90.0 {