fix(ai-chat): preserve server tool outputs for approval-responded state#993
Open
ferdousbhai wants to merge 4 commits intocloudflare:mainfrom
Open
fix(ai-chat): preserve server tool outputs for approval-responded state#993ferdousbhai wants to merge 4 commits intocloudflare:mainfrom
ferdousbhai wants to merge 4 commits intocloudflare:mainfrom
Conversation
_mergeIncomingWithServerState only handled the input-available → output-available transition when preserving server-side tool outputs. This missed the approval-responded state: when a tool with needsApproval is executed server-side (e.g. in onChatMessage after the client approves it), the server sets the part to output-available. But the client, which never received that state update, still holds approval-responded. On the next persistMessages call the client's stale state overwrites the server's output, causing the tool to appear unexecuted and potentially be re-run. Expand the merge condition to also restore output-available when the incoming part is in approval-responded state. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 9cfb69a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
commit: |
Treat the client-side "approval-requested" state as a stale/in-progress state when merging incoming messages so the server's output-available state and output are preserved. Update the conditional and comment in packages/ai-chat/src/index.ts to include "approval-requested", and add a test in packages/ai-chat/src/tests/merge-server-state.test.ts that verifies a server-side output-available tool result is not overwritten when the client sends the same tool in approval-requested state.
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
_mergeIncomingWithServerStateonly preserved server-side tool outputs when the incoming client part was ininput-availablestate. This missedapproval-responded— when aneedsApprovaltool is executed server-side (inonChatMessageafter client approval), the server sets the part tooutput-available, but the client still holdsapproval-respondedsince it never received that state update.On the next
persistMessagescall, the client's staleapproval-respondedoverwrites the server'soutput-available+ output, making the tool appear unexecuted. This causes duplicate tool execution if the server re-runs approved tools from message history.Repro scenario
needsApproval: true(e.g.create_doc)output-availableneedsApprovaltool (e.g.edit_doc)sendAutomaticallyWhenapproval-responded(stale)persistMessages→_mergeIncomingWithServerStatecheckspart.state === "input-available"→ doesn't matchapproval-respondedoutput-availableis overwritten → first tool appears unexecuted → gets re-runFix
Expand the merge condition to also restore
output-availablewhen the incoming part is inapproval-respondedstate.Test plan
🤖 Generated with Claude Code