Skip to content

fix(think): regenerate answers the parent user message instead of continuing (#2028) - #2413

Merged
threepointone merged 4 commits into
mainfrom
fix-2028-regenerate-from-parent
Sep 29, 2026
Merged

threepointone merged 4 commits into
mainfrom
fix-2028-regenerate-from-parent

Conversation

@threepointone

@threepointone threepointone commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2028.

The problem

Regenerating a response in Think behaved like "continue". The regenerated response was already saved as a new branch beside the one it replaces (as the parent user message's child), but the model prompt was assembled from this.messages, which follows the latest leaf. That path still contained the response being replaced, so the model saw it and continued it.

What changed (packages/think/src/think.ts)

  • Prompt cut at the parent. A regeneration passes its parent user message as the turn's history leaf. _assembleModelMessages builds the prompt from the history up to and including that message (_turnHistory()), so neither the replaced response nor anything after it reaches the model. If that message is off the cached path, it's read from storage and repaired for this request only.
  • Only the prompt's messages are touched. History is cut before transcript repair, so answers after the regenerated message are never rewritten. Repairs merge into the message cache by id. Context-overflow compaction compacts the regenerated path (session.compact(leafId)), not the active branch.
  • Retries stay regenerations. The parent is recorded in the chat fiber snapshot, the stream-interruption route, and the recovery retry data (regeneratedLeafId). A regeneration interrupted before its first chunk, live or after eviction, is retried as a new branch from the same user message, and only while the replaced response is still the leaf.

Tests

Five new Think tests, all failing without the fix:

  • The prompt for a regeneration ends at the parent user message, without the old response.
  • Regenerating an earlier response drops everything after its user message from the prompt.
  • Regenerating an earlier response leaves a later stored answer's pending tool call unchanged.
  • A regeneration that stalls before its first chunk retries as a new branch.
  • A regeneration interrupted before streaming retries as a new branch.

Think's full Workers suite passes (1,218 tests), and pnpm run check is clean.

Known gap

A regeneration interrupted after partial output takes the existing "continue" recovery path, which extends the partial response on the new branch. That's the right branch, but it continues rather than restarting. Left for a follow-up.


Devin Review

threepointone and others added 3 commits September 29, 2026 21:40
…tinuing the replaced response (#2028)

Co-authored-by: Cursor <cursoragent@cursor.com>
…-from-parent

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	packages/think/src/tests/agents/client-tools.ts
Co-authored-by: Cursor <cursoragent@cursor.com>
@changeset-bot

changeset-bot Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1297a72

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

This PR includes changesets to release 2 packages
Name Type
@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

@agent-think

agent-think Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

✅ agents import sizes: no significant changes (9bddf2ca → 1297a72c, workflow run)

devin-ai-integration[bot]

This comment was marked as resolved.

@pkg-pr-new

pkg-pr-new Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

agents

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

@cloudflare/ai-chat

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

@cloudflare/codemode

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: 1297a72

…generated path (#2028)

Co-authored-by: Cursor <cursoragent@cursor.com>
@threepointone
threepointone merged commit bcb1f12 into main Sep 29, 2026
13 of 14 checks passed
@threepointone
threepointone deleted the fix-2028-regenerate-from-parent branch September 29, 2026 21:17
@github-actions github-actions Bot mentioned this pull request Oct 2, 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.

Think regeneration includes the previous response in the model prompt

1 participant