Skip to content

fix(think): preserve regeneration branches after restart - #2057

Open
ben-reitz wants to merge 2 commits into
fix/think-regeneration-contextfrom
fix/think-regeneration-restart-recovery
Open

fix(think): preserve regeneration branches after restart#2057
ben-reitz wants to merge 2 commits into
fix/think-regeneration-contextfrom
fix/think-regeneration-restart-recovery

Conversation

@ben-reitz

@ben-reitz ben-reitz commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Stacked on #2038.

What users see

The parent PR fixes regenerated answers using the wrong conversation branch. One restart case remained: if the server's Durable Object restarted while a regenerated answer was streaming, the recovered text could be saved beneath the original answer instead of beside it.

flowchart TB
  subgraph after["After"]
    AU["User message"] --> AO["Original answer"]
    AU --> AR["Recovered regeneration"]
  end

  subgraph before["Before"]
    BU["User message"] --> BO["Original answer"]
    BO --> BR["Recovered regeneration"]
  end
Loading

Why it happens

The streamed text survives a restart because it is stored in SQLite. The message it belongs beside did not: that link only lived in memory. After a restart, Think still had the words, but had forgotten where to put them.

The fix

Resumable streams now save that optional parent link with their existing metadata. When Think rebuilds an interrupted answer, it reads the link back and restores the answer to the right branch.

Existing rows keep the old fallback behavior. Conversations that do not branch also keep using the existing schema until they actually need this field.

Tests

A new WebSocket test disables automatic chat recovery, forces a Durable Object restart, acknowledges the resumed stream, and checks that the recovered answer is a sibling of the original. Migration tests cover both old databases and ordinary linear streams.

Suites: think 925 · agents workers 1746 · agents chat 514 · ai-chat workers 655 · pnpm run check.


Open in Devin Review

@changeset-bot

changeset-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 5a43800

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

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

Copy link
Copy Markdown
Contributor

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 1 additional finding.

Open in Devin Review

@pkg-pr-new

pkg-pr-new Bot commented Aug 6, 2026

Copy link
Copy Markdown

Open in StackBlitz

agents

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

@cloudflare/ai-chat

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

@cloudflare/codemode

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: 5a43800

@ben-reitz
ben-reitz force-pushed the fix/think-regeneration-restart-recovery branch from 88b1368 to 7f0e51a Compare August 6, 2026 14:08
@ben-reitz
ben-reitz force-pushed the fix/think-regeneration-restart-recovery branch from 7f0e51a to 7e46e91 Compare August 7, 2026 07:24
@ben-reitz
ben-reitz force-pushed the fix/think-regeneration-restart-recovery branch from 7e46e91 to 4139ef0 Compare August 12, 2026 08:41
@ben-reitz
ben-reitz force-pushed the fix/think-regeneration-restart-recovery branch from 4139ef0 to 474f208 Compare August 13, 2026 10:21
@ben-reitz ben-reitz added the ready-for-review-and-merge PR is ready for review and/or merge label Aug 13, 2026
devin-ai-integration[bot]

This comment was marked as resolved.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Devin Review found 1 new potential issue.

View 2 additional findings in Devin Review.

Open in Devin Review

Comment on lines +12607 to +12610
const streamId = this._startResumableStream(requestId, {
parentMessageId: parentId
});
const continuation = options?.continuation ?? false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Reconnecting users can see a follow-up answer duplicated as a new message instead of continuing the previous one

The follow-up-answer marker is left off when the resumable stream is opened (_startResumableStream(requestId, { parentMessageId: parentId }) at packages/think/src/think.ts:12607-12609) even though live frames carry it, so a user who reconnects mid-answer sees the replayed text split into a separate duplicate message.
Impact: After a brief disconnect during a tool-driven follow-up answer, the chat shows a second, partial copy of the answer instead of the continued one.

Replay frames lose the `continuation` flag because `ResumableStream._activeIsContinuation` is never set for Think

ResumableStream.start derives _activeIsContinuation and the durable is_continuation column solely from options.continuation (packages/agents/src/chat/resumable-stream.ts:290). Think's _streamResult computes const continuation = options?.continuation ?? false and adds continuation: true to every live broadcast frame (packages/think/src/think.ts:12757-12762), but never passes it to start, so the row is written with is_continuation = 0 and _activeIsContinuation stays false.

Replay is the only other producer of those frames: replayChunks (packages/agents/src/chat/resumable-stream.ts:525) and replayCompletedChunksByRequestId/replayErroredChunksByRequestId (:621, :668) gate continuation: true on that state. The client branches on the flag when applying frames (packages/agents/src/chat/react.tsx:2101, :2127, :2210, :2267-2268): without it a replayed continuation start (whose messageId is deliberately omitted for continuations, packages/think/src/think.ts:11933) is treated as a fresh message and the pre-continuation parts are dropped — the same failure #1733 fixed for AIChatAgent. This PR rewrites exactly this start option object, so the omission is easy to fix here.

Suggested change
const streamId = this._startResumableStream(requestId, {
parentMessageId: parentId
});
const continuation = options?.continuation ?? false;
const streamId = this._startResumableStream(requestId, {
parentMessageId: parentId,
continuation: options?.continuation ?? false
});
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-review-and-merge PR is ready for review and/or merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant