fix: prevent duplicate tool results on provider disconnect/fallback - #46
Open
alfep wants to merge 1 commit into
Open
fix: prevent duplicate tool results on provider disconnect/fallback#46alfep wants to merge 1 commit into
alfep wants to merge 1 commit into
Conversation
… duplicate tool results Addresses issues freecodexyz#37 and freecodexyz#38: after provider disconnect/reconnect or model fallback, the same tool_use ID could be submitted twice to the provider, resulting in duplicate tool_result blocks. Changes: - StreamingToolExecutor.addTool: skip duplicate tool_use IDs that are already tracked (provider reconnect path) - yieldMissingToolResultBlocks: deduplicate tool_use blocks by ID within each assistant message before yielding interruption fallback results (model fallback recovery path) Reproducer (untested by AI, reviewer must run): - Issue freecodexyz#37 gist: https://gist.github.com/N0zoM1z0/adb097dd6be467aa0cc1608d0e1dca6b - Issue freecodexyz#38 gist: https://gist.github.com/N0zoM1z0/1c2db900f5aef155cc647458cb7ba6d2 Picks up work from closed-unmerged PR freecodexyz#42 Fixes freecodexyz#37 Fixes freecodexyz#38
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
Fixes duplicate
tool_resultsubmission when the provider reconnects mid-stream or when the model fallback pipeline re-processes an assistant message that already contains tool_use blocks.Picks up work from closed-unmerged PR #42 (same fix, rebased onto current upstream
main@6b25ab6).Root cause
Two paths can emit a duplicate tool result for the same provider-issued
tool_use.id:tool_useblock.addTool()blindly queued it again → tool ran twice → two results.yieldMissingToolResultBlocks()walks every assistant message and emits one result pertool_useblock. If the same ID appears twice in the same message (reconnect artifact), it yields two results.Fix
Two minimal dedup guards, both keyed by
tool_use.id:src/services/tools/StreamingToolExecutor.ts—addToolreturns early ifblock.idis already inthis.tools. This guarantees at-most-once execution per ID.src/query.ts—yieldMissingToolResultBlocksfilterscontentso eachtool_useID is walked at most once. This guarantees at-most-once result per ID even in the fallback path.Together they implement the "treat a completed provider-issued tool-call ID as committed" invariant from #38.
Test plan
REPRODUCEDREPRODUCEDI have not run the reproducers in this PR — they're self-contained Python+Docker and need a Linux host. Reviewer to verify.
Fixes #37
Fixes #38