fix(core): dedupe duplicate tool_call_id before lowering to provider - #40240
Open
C0d3N1nja97342 wants to merge 1 commit into
Open
fix(core): dedupe duplicate tool_call_id before lowering to provider#40240C0d3N1nja97342 wants to merge 1 commit into
C0d3N1nja97342 wants to merge 1 commit into
Conversation
An interrupted tool run can leave a second tool part that reuses an existing callID (a "completed" part plus an "error"/"Tool execution aborted" part with metadata.interrupted). Serializing both to the provider yields a duplicate tool_call_id, which every provider rejects with a 400. Because compaction re-sends the full history head, the duplicate is always present, so the session can never compact again - every subsequent auto-compaction fails the same way and the token counter keeps growing. Deduplicate tool parts by callID in the assistant() lowering in to-llm-message.ts before building provider messages, keeping the most informative state (completed > error > running > pending) and preserving first-occurrence position so tool/text/reasoning order stays stable. Fixes anomalyco#40235
Contributor
|
The following comment was made by an LLM, it may be inaccurate: No duplicate PRs found. |
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.
Issue for this PR
Closes #40235
Type of change
What does this PR do?
An interrupted tool run can leave a second tool part that reuses an existing callID in the assistant message — a
completedpart plus anerror/"Tool execution aborted" part withmetadata.interrupted. WhentoLLMMessagesserializes both to the provider, it emits twoToolCallParts with the sameid, and every provider rejects that with400 Duplicate value for 'tool_call_id'.Because compaction re-sends the full history head to summarize, the duplicate is always present, so compaction can never complete — the summary is never written, the token counter never resets, and every subsequent auto-compaction fails the same way. The session is effectively poisoned until
/new.Fix: in
packages/core/src/session/runner/to-llm-message.ts, theassistant()lowering now deduplicates tool parts bycallIDbefore building provider messages, keeping the most informative state (completed > error > running > pending) and preserving first-occurrence position so tool/text/reasoning order stays stable. Both thecontent(tool calls) and the trailingresults(tool results) paths consume the deduped list, so a duplicate can no longer reach the provider.I verified the root cause against the reporter's sqlite evidence: two parts sharing
callIDfunctions.todowrite:1, onecompleted(tool=todowrite) and oneerror(tool=unknown,error="Tool execution aborted",metadata.interrupted=true) — exactly the shape the dedup now collapses.How did you verify your code works?
Added a regression test in
packages/core/test/session-runner-message.test.tsthat builds an assistant message with two tool parts sharingfunctions.todowrite:1(acompletedtodowrite + anerror/"Tool execution aborted" interrupted part, matching the issue's sqlite evidence) and assertstoLLMMessagesemits exactly onetool-calland onetool-result, both carrying the single callID with no duplicate.The pre-existing 6 tests in that file (empty turns, message-type mapping, durable media replay, OpenAI encrypted reasoning, provider continuation metadata on failed turns and after model switch) all still pass.
Checklist