fix(agent): balance history when a tool-call wave aborts mid-flight - #146
Merged
Conversation
The anti-loop fatal path saved the assistant message with its tool_calls and no tool results, discarding results that had already completed.
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.
What & why
When the anti-loop detector aborts a turn mid-wave, the loop persisted the assistant message with its
tool_callsand no tool results at all, and discarded every result that had already completed in that wave.Two consequences. The saved transcript is unbalanced, which providers reject — the only reason it does not surface today is that
pkg/llm/anthropicrepairs history on every call. And completed tool work (a query that ran, a subprocess that finished) is silently thrown away, so the next turn's model never sees results the user already paid for.Changes
synthesizeMissingToolErrorsfills any call with no recorded result, leaving completed results untouched. Generalises the existingsynthesizeDroppedToolErrorspattern, which covered only the per-turn budget-drop path.runIterationOncenow drainsws.toolMsgsthroughappendToolResultsInOrderbeforesaveSession, so history leaves the loop balanced by construction.Deliberately not in this PR: removing the defensive
PatchDanglingToolCallscall inpkg/llm/anthropic. It is now redundant for this path, but that is a critical-package edit and the two other callers (regenerate.go,loop_stream.go) have not been proven redundant.Testing
TestFatalWave_SavesBalancedHistory— drives a 6-call wave of identical calls so the detector trips partway through, then asserts everytool_callin the saved history has a matching tool result, that at least one real result survived, and that the total is exactly 6. Verified to fail on the pre-fix code:tool_call "dup0" has no matching tool result in saved history (3 messages).TestSynthesizeMissingToolErrors_PreservesCompleted/_FillsEmptyMap— gap-filling never overwrites a recorded result; an empty map still yields a balanced transcript.gofmt -l .empty,make lint0 issues,make build,make test,make test-raceall clean.Checklist
gofmt -l .prints nothingmake lintis cleanmake buildpassesmake test(andmake test-race) passesCHANGELOG.mdupdated if this PR is cut as a release tagpkg/llm/,pkg/history/, orpkg/telemetry/