Skip to content

Fix reasoning chunks in approval continuations#1481

Merged
threepointone merged 4 commits into
mainfrom
fix-1480-reasoning-approval-continuation
May 12, 2026
Merged

Fix reasoning chunks in approval continuations#1481
threepointone merged 4 commits into
mainfrom
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: 015b516

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: 015b516

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

@threepointone threepointone force-pushed the fix-1480-reasoning-approval-continuation branch from 6c20048 to f252ca3 Compare May 12, 2026 11:49
Add tests to verify reasoning continuation is persisted and handled correctly in two scenarios:

- client-tools-continuation.test.ts: adds a test that ensures reasoning is persisted when a tool-result auto-continuation occurs without delayed chunks, asserting both the initial and continuation reasoning parts are stored as done.

- continue-last-turn.test.ts: adds a test that verifies an orphaned continuation (recovered from an interrupted stream) is appended as a new reasoning part after an already-completed reasoning part, and that the recovered text part is persisted as a completed text part.

These tests strengthen recovery/continuation behavior coverage for tool-result continuations and interrupted-stream recovery.
@threepointone threepointone merged commit 12365dd into main May 12, 2026
4 checks passed
@threepointone threepointone deleted the fix-1480-reasoning-approval-continuation branch May 12, 2026 12:26
@github-actions github-actions Bot mentioned this pull request May 12, 2026
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.

2 participants