Skip to content

switch: the turn definition applies the released chatRecovery budget; AIChatAgent's classifier and stall timeout (recovery side of #3361) - #3415

Merged
mattzcarey merged 9 commits into
slim/chat-fn-6s1from
slim/turn-task-switch-recovery
Nov 27, 2026
Merged

mattzcarey merged 9 commits into
slim/chat-fn-6s1from
slim/turn-task-switch-recovery

Conversation

@mattzcarey

Copy link
Copy Markdown
Member

For the 6f switch (#3361), onto slim/chat-fn-6s1. The recovery side: the turn definition applies the host's released chatRecovery budget, and AIChatAgent gives it what it needs to recover a provider's transient failure. Work in progress; more commits follow, one behaviour at a time.

What is here

chat-host.ts (the definition's recovery block only). A turn found cut off is judged by the released budget, through the released pure evaluator (evaluateChatRecoveryIncident), with what it counted kept in one checkpoint of the run (chat:incident) and progress measured as the chunks the turn has streamed:

  • ten attempts by default, and an attempt that streamed more starts the count again;
  • the no-progress window, the work budget, the interrupted attempts and shouldKeepRecovering;
  • onRecovery gets the incident's attempt number (1 on the first recovery, as released);
  • a spent budget ends the turn with terminalMessage as its error, and onExhausted is told the reason.

ChatOptions.recovery takes the released config object (plus stallTimeoutMs). The default of three attempts by run attempt is gone. Think's released default is the same ten, and the harness's own suite passes (142).

ai-chat/src/index.ts (_chatHost() only). It passes the whole chatRecovery object, its error classifier (the expression the old engine used) and its stall timeout. Without the classifier every transient provider failure mid-stream ended the turn as an error with no retry.

Tests

  • chat-turn-definition.test.ts (61 pass): "gives up on a cut-off turn past its attempt budget, with the app's message and reason" (rewritten), "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".
  • durable-chat-recovery.test.ts: the provider-error tests and the dropped-connection test now expect what the core does: the failure is retried inside the turn's run and the turn completes with the kept text followed by the retry's. Their assertions on incident records are removed (the record is the old engine's).

Behaviour change to state at the switch

A transient provider failure or a stalled stream is retried inside the same run. saveMessages and the like return completed with the reply, where they returned aborted and a recovery finished the reply later. This is the decided design for the shared definition; it needs a changeset line and a migration note on the switch.

Still to do on this branch

The rest of durable-chat-recovery.test.ts (26 in "platform-transient reader errors"): the provider's retry-after wait, the transient retry budget, persist: false, stops during a retry's wait, a question sent during it. Then the context fields (recoveryData, lastBody, lastClientTools), a turn cut off before its stream opened, and the continue: false ending.

@changeset-bot

changeset-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 84f0347

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

@cloudflare/ai-chat

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

@cloudflare/codemode

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: 84f0347

@agent-think

agent-think Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

🟡 agents import sizes: 2 entry points grew

Entry point Exports Largest gzip change Size now
🟡 agents/chat 1 resized +1.9 KiB (+1.43%) 133.4 KiB
🟡 agents/harness/think 1 resized +1.9 KiB (+0.83%) 227.8 KiB
Changed exports (2)
Import Gzip change Size now
🟡 agents/chat#chatTurnDefinition +1.9 KiB (+1.43%) 133.4 KiB
🟡 agents/harness/think#ThinkHarness +1.9 KiB (+0.83%) 227.8 KiB
How this works

Each runtime export is bundled on its own, minified, and gzipped. Changes smaller than 100 B, or smaller than 1% and 1 KiB, are ignored. Growth over 10% or 5 KiB is marked 🔴. This report is informational and does not fail CI. The workflow artifact contains every measurement.

Compared 19389a63 → 84f0347d · workflow run · reported by agent-think[bot]

…Recovery budget (the released evaluator over an incident kept with the run); terminalMessage and onExhausted on give-up
…sed evaluator over an incident kept with the run; terminalMessage and onExhausted when it is spent; AIChatAgent passes its whole config
…all timeout: a provider's transient failure and a stalled stream are retried inside the turn's run
…de the turn, which completes (8 tests rewritten to that outcome)
…un again inside the turn; a declined recovery ends in error with one model call (incident-record assertions removed)
…: a declined recovery ends with that failure, recovery attempts that keep failing are bounded by maxAttempts, and the provider's retry-after sets the wait; AIChatAgent's config is read when a turn runs
…leted, skipped, failed); persist: false and two failures in a row complete inside the turn
@mattzcarey
mattzcarey force-pushed the slim/turn-task-switch-recovery branch from c67c56d to 21a0a82 Compare November 27, 2026 08:27
…etried inside the turn, and a turn that stalls on every attempt ends in error at its budget
@mattzcarey
mattzcarey merged commit f61c0f9 into slim/chat-fn-6s1 Nov 27, 2026
21 of 25 checks passed
@mattzcarey
mattzcarey deleted the slim/turn-task-switch-recovery branch November 27, 2026 09:27
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