fix(sessions): mid-loop user message injection and duplicate tool call detection#351
Merged
Conversation
… call detection (#350) Two layered fixes for runaway tool loops where the model repeats completed work and ignores user corrections: 1. Mid-loop buffer drain: between tool iterations, buffered user messages are now injected into the conversation history so the LLM sees corrections like "stop" before the next iteration instead of after the entire turn completes. 2. Duplicate tool call detection: tracks hash(toolName:argsJson) per turn. When the same tool+args combination appears 3+ times, a system nudge warns the model it is likely repeating completed work. Fires once per turn to avoid nudge spam. Both mechanisms are additive — existing hard cap (MaxToolCallsPerTurn=30) and budget nudge (75% threshold) behavior is unchanged.
This was referenced Mar 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ToolExecutionCompleted, buffered user messages are now injected into the conversation history before the nextFireLlmCall(). User corrections like "stop" or "that's already done" are seen by the LLM on the next iteration instead of being silently queued until the turn completes.hash(toolName:argsJson)per turn inHandleToolCallResponse. When the same tool+args appears 3+ times, a system nudge warns the model it is repeating completed work. Fires once per turn to avoid nudge spam.MaxToolCallsPerTurn=30) and budget nudge (75% threshold) behavior is unchanged.Fixes #350
Evidence from production session
Session
D0AC6CKBK5K/1773983518.003979— bot scaffolded a GitHub repo (done by iteration 5-6), then continued for 15+ more iterations redoing its own work (git ls-files4x, re-creating.gitignore, re-committing). 5 user correction messages were buffered and never seen by the LLM.Test plan
MaxToolIterationTests— all 4 existing tests passturn_duplicate_tool_detected tool=web_search count=3)turn_mid_loop_buffer_drainappears in daemon log when sending messages during tool loopsturn_duplicate_tool_detectedfires for repeated identical tool calls in production