fix(agent-tools): clone run parts so StrictMode doesn't double streamed text (#1835)#1836
Merged
Merged
Conversation
…ouble text (#1835) `applyToRun` shallow-copied a run's parts with `[...seeded.parts]`, sharing element references with the previous state. `applyChunkToParts` then mutated those part objects in place (e.g. `text += delta`), leaking the mutation into `prev`. React double-invokes setState updaters in StrictMode / dev hydration, so each text-delta was applied twice against the same already-mutated prev, doubling streamed text in Next.js, TanStack Start, Remix, and any <StrictMode> app. Clone each part before mutating to keep the reducer pure. Co-authored-by: Cursor <cursoragent@cursor.com>
🦋 Changeset detectedLatest commit: b9bbd64 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 |
agents
@cloudflare/ai-chat
@cloudflare/codemode
create-think
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
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 #1835 —
useAgentToolEventsdoubles every word of streamed text in React StrictMode / SSR frameworks (Next.js, TanStack Start, Remix).Root cause
The agent-tool-event reducer is impure. In
applyToRun(packages/agents/src/chat/agent-tools.ts), thechunkcase did:This is a shallow array copy — the new array's elements are the same object references as
prev.parts. It then callsapplyChunkToParts, which mutates part objects in place (e.g.lastTextPart.text += delta). So the mutation leaks back into the previous state object.React double-invokes
setStateupdaters in StrictMode and during dev hydration (hydrateRoot). BecauseuseAgentToolEventsupdates viasetState((prev) => applyAgentToolEvent(prev, message)), eachtext-deltais applied twice against the same, already-mutatedprev→ doubled text.Fix
Clone each part before mutating, so the reducer stays pure:
Every mutation in
applyChunkToPartstargets a top-level field of a part (text,state,input,output,data, …), so a per-part shallow copy is sufficient — no deep clone needed. This matches the one-line fix proposed in the issue, with a typed cast for the genericPart.Validity / accuracy of the report
Confirmed valid and accurate: the mechanism (shared references + in-place mutation + StrictMode double-invoke) is exactly as described, and the proposed fix resolves it.
Test plan
packages/agents/src/chat/__tests__/agent-tools.test.tsthat replays atext-deltaagainst the sameprevtwice (simulating StrictMode) and asserts the result is not doubled andprevis left untouched.vitest --project chat— all 24 tests pass.oxlint+oxfmt --checkclean on edited files.tsc --noEmitclean for theagentspackage.agentspatch).Made with Cursor