Skip to content

harness-backed Think: Think's chatRecovery honoured through the turn definition's recovery budget (A-4) - #3447

Merged
mattzcarey merged 6 commits into
mainfrom
slim/harness-score-recovery-4
Nov 27, 2026
Merged

mattzcarey merged 6 commits into
mainfrom
slim/harness-score-recovery-4

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

The harness-backed Think now hands Think's released chatRecovery config to the turn definition's recovery budget (#3444), so its fields are honoured as on Think: maxAttempts, terminalMessage, onExhausted, shouldKeepRecovering, and the transient and retry-after bounds. The harness carries the config through an @internal recovery.chatRecovery option. The shell passes chatRecovery as it is, and only translates the ids in onExhausted/shouldKeepRecovering contexts from the run id to the operation id an app knows.

This is arch's A-4 ruling: one implementation in the turn definition, and the shell maps onto it and does nothing else. It merges together with #3444, behind verify's gate.

Validated against std's give-up successors (#3437, merged locally): "gives up through onExhausted with max_attempts_exceeded once the attempts are spent" and "ends recovery with recovery_aborted when shouldKeepRecovering says no" fail on the harness config before this change and pass with it. The class config passes 14/14. Two other successors in that file still fail on the shell, for other slices: stash's recoveryData, and createdAt from A-3 (#3429), which isn't in this base.


Included: #3444 chat: the turn definition applies the released chatRecovery budget

The chat turn definition (chat/chat-host.ts) gets the one implementation of the recovery budget: a turn found cut off is judged by the host's released chatRecovery options, where it counted three attempts by the run's attempt number. This is the chat-module part of the AIChatAgent switch's recovery work, taken out of that stack so the harness-backed Think can map Think's ChatRecoveryConfig onto it now.

What changed

ChatOptions.recovery takes the released config object (plus stallTimeoutMs). In the definition:

  • The budget. The released pure evaluator (evaluateChatRecoveryIncident) decides, with what it counted kept in one checkpoint of the run (chat:incident) and progress measured as the chunks the turn has streamed. So: ten attempts by default, and an attempt that streamed more starts the count again; the no-progress window; the work budget; the interrupted attempts; shouldKeepRecovering.
  • Giving up. A spent budget ends the turn with terminalMessage as its error, and onExhausted is told the reason (max_attempts_exceeded, no_progress_timeout, work_budget_exceeded, interrupted_attempts_exceeded, recovery_aborted). A recovery the app declines (onRecovery → continue: false) ends with the failure that led to it when there was one.
  • onRecovery's attempt is the incident's: 1 on the first recovery, as released.
  • A failure worth another attempt (a transient provider error, a stalled stream) keeps its text and count in one checkpoint (chat:failed). The wait before the next attempt honours the provider's retry-after, up to the cap. Recovery attempts that keep failing that way are bounded by maxAttempts however fast they come.

harness.ts (small): the harness passed maxAttempts + 1 and reported attempt - 1 to make the old count look like Think's; both adjustments are gone, since the definition now counts as Think does. An operation whose recovery gave up still ends with the reason interrupted: the harness asks the definition (chatRecoveryGaveUp(step), @internal) and no longer matches an error text. A model call's error carries the provider's error as its cause, which is where the retry-after is read.

Also, from review:

  • recovery work is counted as released, one unit for ten chunks, while progress is still every chunk streamed (one more chunk starts the attempt count again and refreshes the no-progress window); shouldKeepRecovering is told the work in units;
  • an attempt that ended itself to be tried again (a stall, a transient failure) is not counted as an interrupted attempt; only an attempt whose isolate died is.

Not here (they need ChatDeps.emit, which is on the switch's branch): the chat:recovery:* and chat:stream:stalled events.

Tests

chat-turn-definition.test.ts (59 pass):

  • "gives up on a cut-off turn past its attempt budget, with the app's message and reason" (rewritten from the three-attempt version);
  • "continues a turn cut off for the fourth time: the budget is ten attempts without progress";
  • "starts the attempt count again when the cut-off attempt streamed more";
  • "ends a recovery the app declines with the failure that led to it";
  • "bounds recovery attempts that keep failing the same way by maxAttempts, however fast they come";
  • "waits as long as the provider asked before the next attempt" (retry-after, retry-after-ms, a wait past the cap);
  • "counts recovery work as released: one unit for up to ten chunks";
  • "takes one more chunk as progress, and tells shouldKeepRecovering the work in units";
  • "does not count an attempt that ended itself to be tried again as an interrupted one".

Also run: the harness's own suite (149 pass) and Think's chat-recovery-public.test.ts on the harness config (5 of 5; three of them fail without the two harness.ts lines).

Behaviour change for ThinkHarness

ThinkHarness is a released experimental export (agents/harness/think, agents 0.34), and it runs on this definition. With recovery unset it counted ten interruptions and had no other bound. It now has the chatRecovery defaults:

0.34 Now
recovery.maxAttempts (default 10) interruptions, never reset recoveries without progress; an attempt that streamed more starts the count again
No progress no bound the operation ends after 5 minutes without progress
Interrupted attempts (the isolate died) no separate bound the operation ends at 5
Work since the first interruption no bound the operation ends past 10000 flushed segments

So an operation can now be given up where 0.34 kept going (no progress, five resets in a row) and kept going where 0.34 stopped (more than ten interruptions that each made progress). There is a line for it in .changeset/chat-recovery.md. The harness-backed Think built on it is not released.

Cost

Measured by the reviewer on a deployed minimal Durable Object running the definition, main against this head, in SQL statements counted in the object (three runs each, identical):

main head
A turn with no interruption 85 85
One interruption, counted in the recovering isolate 73 74

The one extra statement is the chat:incident checkpoint write. A transient failure writes one more checkpoint (chat:failed).

How to probe

On a harness-backed Think agent with chatRecovery = { maxAttempts: 2, terminalMessage: "…", onExhausted } and a model that stalls on every call: the turn ends with the terminal message after the first attempt and two recoveries, and onExhausted is called once with max_attempts_exceeded. With a model that streams a little more on each attempt before the object is reset, the turn is still recovered on its fourth reset.

@changeset-bot

changeset-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 8603860

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

agents

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

@cloudflare/ai-chat

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

@cloudflare/codemode

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: 8603860

@mattzcarey
mattzcarey force-pushed the slim/harness-score-recovery-4 branch from 54d8cf6 to c69bf58 Compare November 27, 2026 09:52
@agent-think

agent-think Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

✅ agents import sizes: no significant changes (e25ce837 → 8603860c, workflow run)

@mattzcarey
mattzcarey marked this pull request as ready for review November 27, 2026 10:15
devin-ai-integration[bot]

This comment was marked as resolved.

@mattzcarey
mattzcarey force-pushed the slim/harness-score-recovery-4 branch from c69bf58 to 3e9670e Compare November 27, 2026 10:21
devin-ai-integration[bot]

This comment was marked as resolved.

@mattzcarey
mattzcarey force-pushed the slim/harness-score-recovery-4 branch from cebcb93 to 97a04ff Compare November 27, 2026 10:37
@mattzcarey
mattzcarey changed the base branch from slim/turn-task-recovery-budget to main November 27, 2026 11:04
@mattzcarey
mattzcarey force-pushed the slim/harness-score-recovery-4 branch from 97a04ff to b5e991a Compare November 27, 2026 11:08
devin-ai-integration[bot]

This comment was marked as resolved.

@mattzcarey
mattzcarey force-pushed the slim/harness-score-recovery-4 branch from efa2b1c to 8603860 Compare November 27, 2026 11:17

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

Devin Review

@mattzcarey
mattzcarey merged commit 4eef284 into main Nov 27, 2026
28 checks passed
@mattzcarey
mattzcarey deleted the slim/harness-score-recovery-4 branch November 27, 2026 11:27
mattzcarey added a commit that referenced this pull request Nov 27, 2026
mattzcarey added a commit that referenced this pull request Nov 27, 2026
…o main as one commit

AIChatAgent runs its turns on chatTurnDefinition and chatDispatch, and its old engine is deleted. Rebased onto the rebased #3298 from f87cd85 plus verify's FIX: main merged in (#3389, #3444, #3447, #3435, #3423 resolved in chat-host.ts with turn-task's notes), F27, the stop in a retry backoff ending the turn once, a throwing onChatRecovery given up with recovery_error, and turn-task's 25baf34.
mattzcarey added a commit that referenced this pull request Nov 27, 2026
…o main as one commit

AIChatAgent runs its turns on chatTurnDefinition and chatDispatch, and its old engine is deleted. Rebased onto the rebased #3298 from f87cd85 plus verify's FIX: main merged in (#3389, #3444, #3447, #3435, #3423 resolved in chat-host.ts with turn-task's notes), F27, the stop in a retry backoff ending the turn once, a throwing onChatRecovery given up with recovery_error, and turn-task's 25baf34.
mattzcarey added a commit that referenced this pull request Nov 27, 2026
…o main as one commit

AIChatAgent runs its turns on chatTurnDefinition and chatDispatch, and its old engine is deleted. Rebased onto the rebased #3298 from f87cd85 plus verify's FIX: main merged in (#3389, #3444, #3447, #3435, #3423 resolved in chat-host.ts with turn-task's notes), F27, the stop in a retry backoff ending the turn once, a throwing onChatRecovery given up with recovery_error, and turn-task's 25baf34.
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.

1 participant