Skip to content

Chat: AIChatAgent onto the chat module (4.iii 6f switch, draft) - #3361

Closed
mattzcarey wants to merge 1 commit into
slim/chat-fn-6dfrom
slim/chat-fn-6s1
Closed

mattzcarey wants to merge 1 commit into
slim/chat-fn-6dfrom
slim/chat-fn-6s1

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

AIChatAgent runs its chat turns on the shared chat module, and its own turn engine is deleted. Each turn is a durable Task run of chatTurnDefinition on the chat's lane, and every chat frame goes through chatDispatch, the core that Think's harness runs on. Stacked on #3298 (the chat functions); agent-runs' #3475 (R3-identity) stacks on this PR and deletes its one agent-tool seam. MERGE-WITH #3298 #3361 #3475.

Before this, AIChatAgent carried a second implementation of what chatTurnDefinition does (CLOSEOUT section 2): ChatTurns with its in-memory queue, abort registry and continuations, ReplyStreams writes, the ChatRecoveryEngine with incidents and its successor requests, and the old server-turn paths. A reset lost waiting requests, and a recovered reply came back under a new request id. Now:

Behaviour fixed during pre-review (each with a chat-host test that fails without it):

  • F23: a stop while a turn waited out a retry backoff stored its reply after the next question (U1 U2 A1 A2). The reply is now stored before the run ends.
  • F22: no tab was told such a request ended. Every tab now gets the aborted end.
  • rv-2: the tab that answered a client tool got its continuation twice after a late ack.
  • rv-5: a stopped continuation kept its text twice.
  • arch: cf_agent_chat_recovering on/off is restored (live, and on connect mid-recovery), and a recovered reply's stored list goes to every tab, the one that asked included.
  • verify: the stall error keeps the released text.
  • turn-task: a stop of a running turn no longer frees the lane before its partial reply is stored (the rest of F23, under merge, latest and queue).
  • rv-5/lead: a server tool cut off by a reset is settled by AIChatAgent's repairInterruptedToolPart, with the released text, as on main.
  • rv-5: saveMessages with an aborted signal reports reason: "stopped" again.
  • arch (item 2, turn-task): a tab that loads after a turn failed, naming no request, is told of it from the recorded end, and the release's cf:chat:last-terminal is carried once at upgrade.

After verify's FIX at 25088ca, also:

Not in this PR, by ruling: the release's credit of a child's forwarded stream as a parent turn's recovery progress (N9) is carried in #3475 (F25, which blocks the release), so there is no changeset line for it.

Changeset lines (.changeset/chat-turns.md, .changeset/chat-recovery.md):

  • a recovered turn keeps its request id and ends once (no outcome: "recovering" and new request);
  • saveMessages/continueLastTurn resolve once recovery finished the turn;
  • onError runs once per turn;
  • recovery no longer calls an overridden continueLastTurn/waitUntilStable;
  • a turn a clear reaches ends skipped and keeps nothing;
  • a turn that recovery gives up on reports its end to onChatResponse with the error (arch);
  • a turn cut off before the upgrade is carried under its request id with a new turn's budget;
  • a request with no response ends skipped;
  • persistMessages no longer sees a turn's writes;
  • an over-budget request context isn't kept in memory;
  • a reconnecting tab learns a request's end by naming it, and a tab that names none is told of the newest end only when it was an error;
  • stableTimeoutMs is deprecated with no effect.

docs/agents/chat-agents.md is updated for the one request id and for stableTimeoutMs.

Deleted tests, each family with its successor (the full list, with every title, is in the limits report's successors file):

  • chat-turn-races' persist-timing cases (5), the _enqueueInteractionApply cases (2), and _scheduleAutoContinuation 50ms timer fires before all parallel client-tool results arrive → MissingToolResultsError #1649's apply race → chat-host "a tool result that arrives while the reply streams joins it", "a tab's answer to a live call is written before it is acted on", "every tab is sent the message a tool answer updated, and a repeated answer sends nothing", "a chat clear delivered through Tasks keeps nothing of the turn";
  • programmatic-turns' runFiber listener (Think.saveMessages should accept an external AbortSignal so callers can cancel an in-flight turn from outside #1406) and the shouldApplyMessages turn → chat-host "a caller's signal loses the turn's listener when the turn ends";
  • message-concurrency's merge under a persistMessages override → chat-host "under merge, the queued questions are joined into one message that one turn answers";
  • terminal-frame-order's persist holds (3) → chat-host "a replay after the cutover does not answer again", "a reply the model fails partway keeps what it streamed, stored before the error frame";
  • resumable-streaming's ResumableStream cases (23), ai-chat-eviction's stream cases (2) and the progress-mirror probes → chat-host "a resume request is offered the streaming turn, and its ack replays what it streamed", "a tab offered a stream gets none of its live frames until it acks", "live frames carry each chunk's seq…", "a reply continued after a reset stores the cut text part finished";
  • review-fixes-recovery and recovery-deferral-and-scope (14), server-turn-edges' incident takeover (3) and recovered-context case, durable-admission's fiber race, and durable-chat-recovery's incident, budget, give-up, stable-state and recovery-run cases (34) → chat-host "continues a turn cut off for the fourth time: the budget is ten attempts without progress", "gives up a turn whose attempts keep dying, at the interrupted-attempts limit…", "bounds recovery attempts that keep failing the same way by maxAttempts…", "counts recovery work as released…", "takes one more chunk as progress…", "gives up on a cut-off turn past its attempt budget, with the app's message and reason", "still ends the turn with the terminal message when onExhausted throws", "a turn cut off while its reply waits on a client tool is not given up by the budget", "continues the reply an earlier attempt was cut off in, settling its cut-off tool", the backoff stop/clear tests, turn-task's budget tests (f15b91f), and the released-rows upgrade tests;
  • continue-last-turn's continue-attempt cases (4) → chat-host "a continuation whose reply later messages moved past is skipped", "… already went on to the model's answer is skipped";
  • origin-message-ids' recovery cases (2) → chat-host "a request's end frame names the user messages it came from";
  • agent-tools' and agent-tool-reattach-recovery's old recovery-lifecycle cases (12: the rebind under a new id, _turns.aborts, incident seals) → agent-tools "waits for a run whose child isolate reset…", "cancels a running AIChatAgent child run", "propagates parent abort signals…", and Agent tools (R3-identity): an AIChatAgent agent-tool run is its chat turn's run #3475's run-turn tests. The facet breaker tests keep their wake assertions, without the incident.

Rewritten, not deleted: errored-stream-replay, origin-message-ids, server-turn-edges' interrupted case, chat-recovery's onError case, and client-tools-continuation's parked-batch case (it reads the park from what the chat does).

Cost against the release (perf probe, local, @9852db181; statements / rows written / rows read, release → this PR):

scenario release → PR
turn-short 102→67 / 26→20 / 79→59
turn-long-2000 702→267 / 226→220 / 679→259
turn-server-tool 105→70 / 27→23 / 82→62
startup-empty 87→71 / 88→69 / 831→195
startup-500 56→50 / 1→1 / 5730→3028
queued-3 423→308 / 109→105 / 349→348
resume-mid-stream 196→117 / 56→62 / 172→109
recovery-abort 411→226 / 98→120 / 598→248
turn-slow-300 192→110 / 56→63 / 169→102
connect-500 4→5 / 0→0 / 3→4

Over the release, with the reason: rows written in recovery-abort (+22), turn-slow-300 (+7) and resume-mid-stream (+6) are the pause flush. A model that pauses has its buffered chunks written at once (at most every 50 ms), so a quiet model's last chunks survive a reset; the release lost them. connect-500's one statement is the lane read that tells a connecting tab about open turns.

Tests: chat-host 171/171, harness-think 142/142, ai-chat 945/945, check:public-api, oxlint, oxfmt. agents/chat no longer exports answerTool, continueRequestsOf, ChatModelCall or ChatHooks (no caller).

@changeset-bot

changeset-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 1197fa3

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 changesets to release 4 packages
Name Type
agents Patch
@cloudflare/ai-chat Patch
@cloudflare/think Patch
@cloudflare/agent-think Patch

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

@cloudflare/ai-chat

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

@cloudflare/codemode

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: 1197fa3

@agent-think

agent-think Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

🔴 agents import sizes: 1 entry point over threshold

Entry point Exports Largest gzip change Size now
🔴 agents/chat 35 resized, 12 new up to +2.3 KiB (+398.33%) 2.9 KiB
🟡 agents/harness/think 1 resized +4.8 KiB (+2.1%) 231.9 KiB
Changed exports (48)
Import Gzip change Size now
🔴 agents/chat#storableRequestContext +2.3 KiB (+398.33%) 2.9 KiB
🔴 agents/chat#CHAT_REQUEST_CONTEXT_KEY +2.2 KiB (+557.88%) 2.6 KiB
🔴 agents/chat#INTERRUPTED_TOOL_ERROR_TEXT +2.2 KiB (+410.53%) 2.7 KiB
🔴 agents/chat#replyFinalized +2.2 KiB (+535.39%) 2.6 KiB
🔴 agents/chat#continuationStreamEnded +2.2 KiB (+487.23%) 2.6 KiB
🔴 agents/chat#addContinuationTarget +2.2 KiB (+500.22%) 2.6 KiB
🔴 agents/chat#settleContinuationTurn +2.2 KiB (+415.99%) 2.7 KiB
🔴 agents/chat#continuationTargetIsLatest +2.2 KiB (+485.25%) 2.6 KiB
🔴 agents/chat#clientResolvableToolNames +2.2 KiB (+493.16%) 2.6 KiB
🔴 agents/chat#continuationStreamStarted +2.2 KiB (+423.11%) 2.7 KiB
🔴 agents/chat#dropWaitingContinuations +2.2 KiB (+381.82%) 2.7 KiB
🔴 agents/chat#persistRepairedTranscript +2.2 KiB (+345.88%) 2.8 KiB
🔴 agents/chat#streamChunksFinished +2.2 KiB (+460.37%) 2.6 KiB
🔴 agents/chat#unexecutedToolNames +2.2 KiB (+441.15%) 2.7 KiB
🔴 agents/chat#reportTurnFailure +2.2 KiB (+347.1%) 2.8 KiB
🔴 agents/chat#pausedExecutionUpdate +2.2 KiB (+408.89%) 2.7 KiB
🔴 agents/chat#toolPartHasSettledResult +2.2 KiB (+452.36%) 2.6 KiB
🔴 agents/chat#applyToolUpdate +2.2 KiB (+373.39%) 2.7 KiB
🔴 agents/chat#toolResultUpdate +2.1 KiB (+372.74%) 2.7 KiB
🔴 agents/chat#crossMessageToolResultUpdate +2.1 KiB (+371.14%) 2.7 KiB
🔴 agents/chat#toolApprovalUpdate +2.1 KiB (+365.55%) 2.7 KiB
🔴 agents/chat#stripTrailingReasoning +2.1 KiB (+306.35%) 2.8 KiB
🔴 agents/chat#hasIncompleteToolBatch +2.1 KiB (+345.61%) 2.7 KiB
🔴 agents/chat#partAwaitsClientInteraction +2.1 KiB (+362.04%) 2.7 KiB
🔴 agents/chat#interruptedToolPartAsError +2.1 KiB (+280.91%) 2.9 KiB
🔴 agents/chat#replyToContinueIsEmpty +2.1 KiB (+273.45%) 2.9 KiB
🔴 agents/chat#transcriptEndsInAnswer +2.1 KiB (+349.27%) 2.7 KiB
🔴 agents/chat#continuationWaitsOnClient +2.1 KiB (+335.56%) 2.7 KiB
🔴 agents/chat#removeEmptyReplyToContinue +2.1 KiB (+198.53%) 3.2 KiB
🔴 agents/chat#hasPendingToolInteraction +2.1 KiB (+275.74%) 2.9 KiB
🔴 agents/chat#applyToolPartUpdate +2.1 KiB (+223.98%) 3 KiB
🔴 agents/chat#unsettledToolCallIds +2.1 KiB (+332.87%) 2.7 KiB
🔴 agents/chat#repairInterruptedToolParts +2 KiB (+142.49%) 3.3 KiB
🔴 agents/chat#repairTranscriptBeforeTurn +1.9 KiB (+122.13%) 3.5 KiB
🔴 agents/chat#settleInterruptedReply +1.9 KiB (+120.5%) 3.6 KiB
🟡 agents/harness/think#ThinkHarness +4.8 KiB (+2.1%) 231.9 KiB
🆕 agents/chat#chatTurnDefinition — 134.2 KiB
🆕 agents/chat#chatDispatch — 129.1 KiB
🆕 agents/chat#submitTurn — 125.6 KiB
🆕 agents/chat#stopAllTurns — 124.8 KiB
🆕 agents/chat#clearChat — 124.8 KiB
🆕 agents/chat#cancelTurn — 124.7 KiB
🆕 agents/chat#resumeChat — 3.4 KiB
🆕 agents/chat#planInterruptedTurn — 2.8 KiB
🆕 agents/chat#chatClientView — 2.8 KiB
🆕 agents/chat#forgetTab — 2.7 KiB
🆕 agents/chat#chatTurnStreamId — 2.6 KiB
🆕 agents/chat#CHAT_CLEARED — 2.6 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 54d3ca4e → 1197fa31 · workflow run · reported by agent-think[bot]

@mattzcarey
mattzcarey changed the base branch from slim/chat-fn-6f11 to slim/chat-fn-6d November 27, 2026 10:53
@mattzcarey
mattzcarey marked this pull request as ready for review November 27, 2026 14:56
devin-ai-integration[bot]

This comment was marked as resolved.

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

This comment was marked as resolved.

@mattzcarey
mattzcarey marked this pull request as draft November 27, 2026 15:38
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
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.
…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
mattzcarey marked this pull request as ready for review November 27, 2026 17:04

@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 3 new potential issues.

Devin Review

Comment thread packages/agents/src/chat/chat-host.ts Resolved
mattzcarey added a commit that referenced this pull request Nov 27, 2026
@mattzcarey

Copy link
Copy Markdown
Member Author

Replaced by #3504, which carries this change with the rest of the switch (integrated and tested together).

@mattzcarey mattzcarey closed this Nov 28, 2026
mattzcarey added a commit that referenced this pull request Nov 29, 2026
* Chat: AIChatAgent runs its turns on the chat module, and an agent-tool run is its chat turn's run

One unit, three branches that land together: `slim/chat-fn-6d` (the chat functions, #3298), `slim/chat-fn-6s1` (AIChatAgent onto them, #3361), `slim/agent-runs-r3` (R3-identity, #3475). Review `git diff slim/local-main...slim/agent-runs-r3`; each branch is the one below it plus its own commits.

`AIChatAgent`'s turns now run on the shared chat module: `submitTurn`, `clearChat`, `answerTool` and `chatDispatch` over `ChatDeps`, and one turn definition, `chatTurnDefinition`. Its old turn engine, recovery engine instance and host-interface implementations are deleted, and an agent-tool run on an `AIChatAgent` child is that child's chat turn (`agent-tool:<runId>`), so the child's separate run store and the seam between the two are deleted too.

Why:
- Think's harness and `AIChatAgent` each had their own implementation of a chat turn, its recovery and its socket protocol; this leaves one.
- A turn is one durable Task run across attempts, so a recovered reply keeps its request id and ends once.
- Rows a release left (a cut-off turn, a scheduled recovery, the last terminal, agent-tool child rows, a run in flight) are carried forward once at start, each with an upgrade test.

Kept as before the switch, each with a test: a tool answer's continuation asks the model 50 ms after the answer and a stop inside that window drops it; `tool:result`, `tool:approval`, `message:cancel`, `chat:continuation:dropped` and `chat:request:failed` are published, and a spent recovery budget publishes `chat:recovery:exhausted`; a stop during a recovered attempt turns the recovering status off; an agent-tool run under a stopped turn reports a plain abort.

What a user can see change is exactly the changeset lines (`.changeset/chat-turns.md`, `chat-recovery.md`, `agent-tools.md`); everything else matches main. The deleted tests and their successors are listed in `archive/report-limits-switch-successors.md`. No public API is added: `agents/chat` stays `@internal`, and `ChatHooks.isReplay` and `ChatTurnResult.gaveUp` are internal to it.

Cost (`probes/perf`, local workerd, 60 rows, at 826bcd46a, whose product code is the head's; release values from PERF-BUDGET Table 1, base `slim/local-main` f9564d8). Cells are statements / rows written / rows read:
- **Every `AIChatAgent` row is at or below the release in all three.** turn-short 67 / 20 / 60 (release 102 / 26 / 79); turn-paced-20, a reply of 20 chunks 50 ms apart, 72 / 25 / 65 (release 108 / 28 / 85); queued-3 294 / 91 / 338 (release 423 / 109 / 349); recovery-abort 176 / 70 / 232 (release 411 / 98 / 598); turn-slow-300 101 / 54 / 94 (release 192 / 56 / 169); resume-mid-stream 110 / 55 / 103 (release 196 / 56 / 172); startup-empty 55 / 48 / 91 (release 87 / 88 / 831).
- **Against the base (main), a turn makes more statements and reads more rows, and writes fewer:** turn-short 67 / 20 / 60 against 56 / 25 / 49; queued-3 294 / 91 / 338 against 238 / 102 / 216. A turn is a Task run now: it reads its run and its checkpoint, the stored request context and the transcript at its start, where the old engine held them in memory. I have not attributed each statement.
- A paused model's buffer is written at most every 250 ms, so a reset loses at most 250 ms of what a paused model had sent; with that a paced reply writes fewer rows than the release (25 against 28). A connect makes one more statement than the release (the lane read). `submitTurn` reads each sent message once to compare it with the stored one.
- ThinkHarness, which shares the turn definition: `harness/prompt-*` rows are +8 statements, +1 row written and +7 rows read against the base (prompt-hello 67 / 21 / 86, base 59 / 20 / 79, release 74 statements), except prompt-paced-20, which falls to 72 / 26 / 91 (base 78 / 39 / 98, release 36 rows written). I have not attributed the +8 statements.

Validated on `slim/agent-runs-r3`:
- chat-host, the chat node project, the harness suite, the whole ai-chat suite and Think's class suite pass.
- Think's harness config, test by test against the base run the same way: 16 gained; one timing test (`detached-reconcile-lane`, an alarm that must return within 15 s) failed in the full run beside two other suites and passes alone on the head.
- Each fix folded in after the last review has a test that failed first: the sub-agent count starting again for a chat turn; a given-up turn's end reaches `onChatResponse` after a reset; a `continueLastTurn` with nothing to continue sends no frame; ThinkHarness's `continue` of a finished reply calls the model; a host that runs its own tools may ask again for a call it holds.
- Deployed gate by verify, root and facet, against main and the release: owed on this head.

In a sub-agent the Lifecycle counts a job's attempts that never settled and seals the job at three. A turn is one run now, so that count would have ended a turn cut off by resets before chat recovery's own budget could. A chat attempt that finds the one before it died, mid-reply or before it streamed, counts it in the recovery budget and offers it to `onChatRecovery`, as before; it then tells Tasks, and the Lifecycle's count for that run starts again (two `@internal` members: `Tasks.interruptionsCounted`, `LifecycleJobs.attemptsCounted`).

Unit: slim/agent-runs-r3 @c1c88c257 (group: slim/chat-fn-6d @bdd3c88c8, slim/chat-fn-6s1 @9341dad6a)

* Chat turns: a completed reply that ends in reasoning is kept; the reasoning is left out of the model request (F21)

A stored assistant reply that ends in reasoning is no longer rewritten or removed by the before-turn repair. The turn definition hands the host two lists: `ChatModelCall.messages`, the request view with trailing reasoning left out and a reply left empty dropped, and `ChatModelCall.transcript`, the stored list.

Why:
- Anthropic rejects a request whose assistant message ends in a thinking block, so the repair used to delete that reasoning from storage.
- A model may complete a reply with reasoning only; the user then saw it disappear when they asked again, and the turn had no assistant message.

Applies to `AIChatAgent`, Think's class and the harness-backed Think. The changeset line is in `.changeset/chat-turns.md`; the chat guide and `design/chat-turn-core.md` are updated.

Cost: no extra read or write; the request view is one more pass in memory over the messages the turn already read.

Validated: ai-chat `continue-last-turn` and `chat-turn-races`, Think's `empty-reply` and `client-tools`, and the harness suite pass. Stacked on `slim/agent-runs-r3`; it lands right after that unit.

Unit: slim/inbox-f21-b @640f50be4 (applied by tree: local-main's tree == c1c88c257, the unit's base)

* Chat: step (d), what the switch left alive in ai-chat and the chat module's index

One unit, stacked branches (`slim/switch-d`, `-d2`, `-d3`, `-d4x`, `-d5x`, `slim/switch-mcp-wait-on-d`, top `slim/switch-e`), lands by its top. `packages/ai-chat/src/index.ts` goes from 3,074 to 1,623 lines (904 of code); what a user can see: the two fixes of things the switch dropped (the MCP wait, the memory-limit seal) and the two Tasks fixes below.

**Behaviour:**
- `waitForMcpConnections` is honoured again: a tab's request waits for the agent's MCP connections before `onChatMessage`, with the configured timeout (default 10 s). The switch of AIChatAgent onto the chat module had left the setting with no reader, so a turn that started while connections were restoring saw an incomplete `getAITools()`. As before the switch, `saveMessages`, continuations and tool answers do not wait; a recovered attempt of a tab's request now does.
- A chat turn the alarm's memory-limit breaker seals is given up, not dropped: what it streamed is stored, `onExhausted` is called with `reason: "out_of_memory"`, `chat:recovery:exhausted` is published, every tab gets the terminal message and `onChatResponse` hears `status: "error"`. Before the switch AIChatAgent did this from `onAlarmMemoryLimit`; after it the turn is a Tasks run that Tasks fails and deletes from outside its handler, so the chat said nothing. Tasks gets an `@internal` register option `onSealed(runId)`, called before a sealed run is failed; the chat module's `giveUpSealedTurn` does the give-up. Nothing runs for a turn that is not sealed.
- Tasks starts a cancelled lane head's successor only once the cancelled attempt has returned, so a stopped turn has stored its reply before the next turn stores its question. The chat turn definition's own wait for this is deleted.
- A Task run that a platform failure struck in a live isolate runs at once on the next start.

**Deleted:**
- One read of an agent-tool run per chunk it streams (now one per turn).
- 196 names off `agents/chat`'s index that nothing imports (381 to 185).
- ai-chat's test worker loses 987 lines only the deleted old-engine tests used.
- In `index.ts`: single-caller pass-throughs, a set of resuming tabs nothing added to, an options type nothing passed, comments describing the old engine.

**Moved out of `index.ts`, same code behind small host objects:**
- `ai-chat/src/released-rows.ts`: the carry of rows an ai-chat release left.
- `ai-chat/src/agent-tools.ts`: the agent-tool child's side and detached delivery.
- `ai-chat/src/turn-host.ts`: the class as the chat module's host (`respond`, the turn's end, `onChatResponse` delivery, the wait behind `saveMessages`).
- `agents/src/chat/agent-tool-turns.ts`: the agent-tool progress wiring and the tail asked for before a turn's stream opens.
- `requestContextFromLegacy` and `abortReasonText` out of the old engine's files, so ai-chat imports nothing defined in `chat-turns.ts`, `recovery-engine.ts` or `agent-tool-runs.ts`.

`index.ts` now holds the released API with its docs (627 comment lines), the constructor's wrappers of the app's handlers, and the wiring of the four helper objects. Two `@internal` protected members left the class: `_persistOrphanedStream` and `_orphanStore`.

Not tested with a real memory-limit kill on AIChatAgent (a chat-host test drives the seal); ThinkHarness's chat turns still have that gap.

Not here: the old turn engine's files and the 14 old host interfaces in `agents/src/chat` are still used by Think's class and go when it moves onto the harness.

Validated: ai-chat 949 of 949 at each step, the same tests; agents' chat project 1,482; chat-host, tasks, lifecycle and agent-tool worker tests; Think's class suite on `slim/switch-d`; `tsc`, oxlint, oxfmt, exports, public-API and instance-name checks. The two Tasks changes, the MCP wait, the seal's give-up and the per-chunk read each have a test that fails without them (the old MCP tests only checked that a turn completes). Cost: one promise fewer per turn. No changeset: none of these bugs was released.

Unit: slim/switch-e @a014ec45c (step (d): switch-d..d5x, switch-mcp-wait-on-d, switch-e)

* Chat: PR 2's rulings: a clear that reaches a streaming reply ends its request's frame aborted (as before the switch); AIChatAgent warns once when a subclass overrides continueLastTurn; changeset lines for the kept differences

* fix(ai-chat): the continueLastTurn warning is only for a class that overrides it: the override is looked for before Agent puts its method wrappers on the class's prototype, once per class
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