Skip to content

Fix reasoning chunks in approval continuations#1481

Open
whoiskatrin wants to merge 3 commits intomainfrom
fix-1480-reasoning-approval-continuation
Open

Fix reasoning chunks in approval continuations#1481
whoiskatrin wants to merge 3 commits intomainfrom
fix-1480-reasoning-approval-continuation

Conversation

@whoiskatrin
Copy link
Copy Markdown
Contributor

@whoiskatrin whoiskatrin commented May 8, 2026

Summary

Fixes #1480.

When a needsApproval tool is approved with autoContinueAfterToolResult enabled, the auto-continuation stream can resume from an assistant message that already contains a completed reasoning part.

Two related issues were happening in that path:

  1. The continuation's reasoning-start could be suppressed before reaching the client, so AI SDK v6 saw reasoning-delta/reasoning-end without a preceding reasoning-start and threw:

    AI_UIMessageStreamError: Received reasoning-delta for missing reasoning part
  2. After forwarding reasoning-start to fix the stream protocol, completed prior reasoning could still cause the server-side continuation builder to merge new reasoning into the existing reasoning block. That made the live stream look correct, but the final persisted/rehydrated assistant message did not keep the continuation reasoning as its own persisted reasoning part, so the reasoning block disappeared after the response finished.

This PR keeps the client stream and persisted message in sync:

  • the client stream receives a valid reasoning-startreasoning-deltareasoning-end sequence
  • interrupted/streaming reasoning continuations still resume the existing reasoning part
  • continuations after an already-completed reasoning block create and persist a new reasoning part, so rehydrated messages preserve the reasoning shown during streaming

Changes

  • Forward reasoning-start during approval continuations so AI SDK v6 receives a valid reasoning lifecycle.
  • Only skip server-side reasoning-start application when resuming an existing state: "streaming" reasoning part.
  • Preserve continuation reasoning in the final persisted assistant message when the previous reasoning part was already done.
  • Extend the addToolApprovalResponse: Ensure a "reasoning-start" chunk is sent before any "reasoning-delta" chunks. #1480 regression test to assert both the streamed chunk order and the final SQLite-persisted reasoning parts.
  • Add/update a patch changeset for @cloudflare/ai-chat.

Testing

cd packages/ai-chat && npm run test:workers -- src/tests/client-tools-continuation.test.ts -t "#1480"
cd packages/ai-chat && npm run test:workers -- src/tests/client-tools-continuation.test.ts
npm run check

npm run check completes successfully. It still prints existing sherif warnings for package-less workspace directories, unrelated to this change.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 8, 2026

🦋 Changeset detected

Latest commit: 6c20048

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/ai-chat Patch

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 8, 2026

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@1481

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@1481

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@1481

hono-agents

npm i https://pkg.pr.new/hono-agents@1481

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@1481

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@1481

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@1481

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@1481

commit: 6c20048

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

@whoiskatrin whoiskatrin marked this pull request as ready for review May 8, 2026 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

addToolApprovalResponse: Ensure a "reasoning-start" chunk is sent before any "reasoning-delta" chunks.

1 participant