Skip to content

fix(think): preserve regeneration branch context - #2038

Open
ben-reitz wants to merge 2 commits into
mainfrom
fix/think-regeneration-context
Open

fix(think): preserve regeneration branch context#2038
ben-reitz wants to merge 2 commits into
mainfrom
fix/think-regeneration-context

Conversation

@ben-reitz

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

Copy link
Copy Markdown
Contributor

Bug

Regeneration means “answer this user message again” while keeping the previous answer available. Think already stored the new answer as a sibling, but it generated that answer from the wrong history.

Stored conversation:

Earlier history → User message
                  └─ Old answer  ← active leaf

Expected model input: Earlier history → User message
Actual model input:   Earlier history → User message → Old answer → “Continue…”

The result looked like a new branch in storage, but the model had been asked to continue the old answer instead of producing an independent alternative.

Fix

Think now uses one server-validated branch point for both model input and output storage. For regeneration, model history ends at the selected user message, and the generated answer is stored beneath that same message.

Earlier history → User message
                  ├─ Old answer  ← preserved
                  └─ New answer  ← generated without seeing the old answer

The selected branch is also preserved through hooks, retries, context compaction, Durable Object eviction recovery, and partial-response reconstruction. Re-delivered recovery work is idempotent, so it cannot create duplicate alternatives.

Summary

  • generate regenerated responses from the server-selected branch point instead of the previous assistant leaf
  • preserve the selected branch through lifecycle hooks, retries, context-overflow compaction, and chat recovery
  • persist recovered responses as sibling branches and make branch-scoped recovery retries idempotent
  • add model-boundary, Session, and forced-eviction regression coverage

Fixes #2028

Testing

  • pnpm run check
  • pnpm --dir packages/agents run test:chat — 514 passed
  • pnpm --dir packages/think run test:workers — 923 passed

@changeset-bot

changeset-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 24f92d0

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

This PR includes changesets to release 3 packages
Name Type
agents Patch
@cloudflare/think Patch
@cloudflare/agent-think 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

pkg-pr-new Bot commented Aug 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

agents

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

@cloudflare/ai-chat

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

@cloudflare/codemode

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

create-think

npm i https://pkg.pr.new/create-think@2038

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: 24f92d0

@ben-reitz
ben-reitz marked this pull request as ready for review August 5, 2026 08:44

@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 potential issue.

View 1 additional finding in Devin Review.

Open in Devin Review

Comment thread packages/agents/src/chat/recovery.ts
@ben-reitz
ben-reitz marked this pull request as draft August 5, 2026 13:03
@ben-reitz
ben-reitz marked this pull request as ready for review August 6, 2026 11:04
@ben-reitz
ben-reitz force-pushed the fix/think-regeneration-context branch from 6133537 to f77f98a Compare August 6, 2026 14:08
@ben-reitz
ben-reitz force-pushed the fix/think-regeneration-context branch from f77f98a to 24f92d0 Compare August 7, 2026 07:24
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.

Think regeneration includes the previous response in the model prompt

1 participant