Skip to content

Think on the harness: messengers through Channels (C4, part 1) - #3347

Merged
mattzcarey merged 3 commits into
mainfrom
slim/deliveries-channels-c4
Nov 27, 2026
Merged

mattzcarey merged 3 commits into
mainfrom
slim/deliveries-channels-c4

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

The harness-backed Think (think/src/harness/think.ts) answers its messengers through Channels. Each messenger from getMessengers() becomes a Chat SDK Channel (#3203) whose replies are delivery runs (deliverToSurfaces, #3197), mounted with Channels.forHarness(this.harness). Its webhooks are checked by the messenger's verifyWebhook and then go through the gateway's own code inside the object (ChannelGateway, every sender routed to this object). The webhook answers once its replies are sent, as Think's does.

This is the first part of CHANNELS.md's C4, which H2 (#3295) unblocked:

  • The released think.ts keeps its messenger runtime until H11 swaps the classes. think/src/messengers' routing is deleted then, so C4 is finished together with H11.
  • Every messenger thread shares the harness's default session, as conversation: "self" does in Think, but a reply goes only to the thread whose message started its turn. The asker's surface is kept in KV under the event id until Channels settles the turn, so a reply after a reset still finds it.
  • The webhook waits until Channels settles each turn it started (seen by a Channel that only watches turns) and then for those deliveries' first attempts. A delivery waiting to retry does not hold the webhook.
  • A run a reset cut is followed again from start: the default Workspace is made when the harness first builds its tools, not only in Think's onStart, which runs after Channels re-attaches the session. Before, the rerun's answer never reached the chat.
  • After a reset, the chat gets one reply: the rerun's. Channels used to close the cut run's response normally when the rerun began, and the Telegram adapter then sent its text as a message. Now Channels marks a response whose run never ended as interrupted. A reader of a cut response waits until its turn opens a later response or settles. If a later response answers, the cut one posts nothing: its stream fails with SupersededResponse, so the attempt is retried, and the retry reads it as empty.
  • The Chat SDK Channel starts a native stream only with the first text. Telegram refuses a stream without text, and an empty response now posts nothing.
  • Only the root object takes webhooks, as in Think. A sub-agent learns its path only once it starts, so the check is made per request. A sub-agent still mounts the messenger Channels, which stay idle: no webhook, no delivery.
  • getMessengers() is read in the constructor, not at start as Think reads it, because Channels mounts its channels (and their deliveries register on Tasks) at construction. A subclass whose getMessengers() reads a field set in its own constructor gets the field's initial value.
  • The Chat SDK Channel's posted and edited text no longer ends in whitespace: "Got " then "Got it" is posted as "Got" and edited once, as Think posts it.
  • Next PRs, one per group, stacked on this one: the Chat SDK Chat (subscriptions, mentions, bursts, the lane's arrival policy), recovery of replies a reset cut off, and per-thread sub-agents. Button approvals come after H7.

Validation:

  • messengers.test.ts on the harness-backed Think goes from 79 of 111 to 85 of 115 (pnpm test:harness, harness-compat.md). The new cases pass on both classes: a webhook its messenger's verifyWebhook refuses gets 401 and never reaches the adapter; a webhook sent to a sub-agent reaches the sub-agent's own onRequest; two threads each get only their own reply, through their own messenger only; and nothing of the asker is kept once a turn that a reset cut has settled. durable-admission.test.ts alternates between 3 and 4 of 9 from run to run, so its intermittent test stays in the baseline.
  • New chat-sdk.test.ts case for the trimmed post and edit. The channels unit tests and the agents channels suite pass.
  • New deliveries tests: a run cut before it wrote, and one cut mid-answer, each post nothing once a later run answers. Two email tests now settle their turn after the wake, since a cut response waits for that.
  • The Telegram replay (messenger-replay.test.ts) gains "answers a direct message once after a reset mid-answer": one sendRichMessage "Got it". It passes on Think. Locally it also passes on the harness class through the new vitest.replay.harness.config.ts, which CI does not run.

Cost: no released class changes, so no hot path of agents 0.34 / think 0.24 moves. A sub-agent of a harness-backed Think with messengers creates the empty Streams tables of the idle Channels at its first start, and runs one list query of streaming responses at each start.

Cost (root): for each answered message, the harness-backed class writes Channels' surface KV row, the asker KV row (deleted when the turn settles) and the response stream, plus one delivery run per surface (deleted once delivered).

Probe (reviewer), on account 543fbdef…, Worker slim-outbox-3347:

  1. Deploy a harness-backed Think subclass with getMessengers() returning telegramMessenger({ token, userName, secretToken, apiBaseUrl }). Point apiBaseUrl at a route on the same Worker that records Bot API calls.
  2. Reset mid-answer: script the model to reset after its first text has been drafted. The chat gets exactly one sendRichMessage, with the rerun's full answer.
  3. Root: POST a recorded Telegram private-chat update to the messenger's webhook path. The response returns after the answer's Bot API calls (in a private chat, the adapter's drafts and then one sent message), and the recorder holds exactly one sent message.
  4. Facet: a webhook fetched on a sub-agent of the same class reaches the sub-agent's own onRequest (404 or the app's answer) and posts nothing. Its own chat turns are unchanged.
  5. Base (main) answers nothing on the harness-backed class (getMessengers() returned {}).

@changeset-bot

changeset-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 7b4ddae

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 2 packages
Name Type
agents Minor
@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@3347

@cloudflare/ai-chat

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

@cloudflare/codemode

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: 7b4ddae

@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/experimental/channels 1 resized +405 B (+3.57%) 11.5 KiB
🟡 agents/internal 2 resized up to +170 B (+6.38%) 2.8 KiB
Changed exports (3)
Import Gzip change Size now
🟡 agents/experimental/channels#Channels +405 B (+3.57%) 11.5 KiB
🟡 agents/internal#chatSdkChannel +170 B (+6.38%) 2.8 KiB
🟡 agents/internal#deliverToSurfaces +120 B (+3.29%) 3.7 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 be547f3b → 7b4ddae0 · workflow run · reported by agent-think[bot]

@mattzcarey
mattzcarey force-pushed the slim/deliveries-channels-c4 branch from 04587e5 to 67d676a Compare November 27, 2026 03:00
@mattzcarey mattzcarey changed the title WIP: the harness-backed Think's messengers on Channels (C4) Think on the harness: messengers through Channels (C4, part 1) Nov 27, 2026
@mattzcarey
mattzcarey marked this pull request as ready for review November 27, 2026 03:24
devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

@mattzcarey
mattzcarey force-pushed the slim/deliveries-channels-c4 branch from 6f2ad82 to 1eaf3f1 Compare November 27, 2026 04:55
devin-ai-integration[bot]

This comment was marked as resolved.

mattzcarey added a commit that referenced this pull request Nov 27, 2026
… on a cut response's turn; chatSdkChannel starts a stream with its first text; replay test tg-resetmid on both configs (harness still posts an extra 'Got')
@mattzcarey
mattzcarey force-pushed the slim/deliveries-channels-c4 branch from 573e0d2 to 993bdef Compare November 27, 2026 05:57
devin-ai-integration[bot]

This comment was marked as resolved.

@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 .changeset/harness.md Resolved
@mattzcarey
mattzcarey merged commit 867b9bc into main Nov 27, 2026
30 checks passed
@mattzcarey
mattzcarey deleted the slim/deliveries-channels-c4 branch November 27, 2026 06: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.

1 participant