Skip to content

fix(sessions): mid-loop user message injection and duplicate tool call detection#351

Merged
Aaronontheweb merged 1 commit into
devfrom
fix/runaway-tool-loop
Mar 21, 2026
Merged

fix(sessions): mid-loop user message injection and duplicate tool call detection#351
Aaronontheweb merged 1 commit into
devfrom
fix/runaway-tool-loop

Conversation

@Aaronontheweb

Copy link
Copy Markdown
Collaborator

Summary

  • Mid-loop buffer drain: Between tool iterations in ToolExecutionCompleted, buffered user messages are now injected into the conversation history before the next FireLlmCall(). 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.
  • Duplicate tool call detection: Tracks hash(toolName:argsJson) per turn in HandleToolCallResponse. 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.
  • Both mechanisms are additive — existing hard cap (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-files 4x, re-creating .gitignore, re-committing). 5 user correction messages were buffered and never seen by the LLM.

Test plan

  • MaxToolIterationTests — all 4 existing tests pass
  • Duplicate detection fires correctly in existing test (visible in test logs: turn_duplicate_tool_detected tool=web_search count=3)
  • Build and deploy to dev daemon, verify turn_mid_loop_buffer_drain appears in daemon log when sending messages during tool loops
  • Verify turn_duplicate_tool_detected fires for repeated identical tool calls in production
  • Verify user "stop" messages are honored mid-loop

… 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.
@Aaronontheweb Aaronontheweb enabled auto-merge (squash) March 21, 2026 14:18
@Aaronontheweb Aaronontheweb merged commit 5fa30fa into dev Mar 21, 2026
3 checks passed
@Aaronontheweb Aaronontheweb deleted the fix/runaway-tool-loop branch March 21, 2026 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: runaway tool loop — model repeats completed work, ignores user corrections

1 participant