Skip to content

fix(agents): one root call per sub-agent broadcast, none without recipients (#2252) - #2399

Merged
threepointone merged 2 commits into
fix/2299-facet-host-jobsfrom
fix/2252-facet-broadcast-rpcs
Sep 28, 2026
Merged

threepointone merged 2 commits into
fix/2299-facet-host-jobsfrom
fix/2252-facet-broadcast-rpcs

Conversation

@threepointone

@threepointone threepointone commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2252.

Problem

Outside the client frame that started the work, every facet broadcast resolved the root through getAgentByName() before calling _cf_broadcastToSubAgent. That resolution is an __unsafe_ensureInitialized round trip, so a streamed answer cost two billed root requests per chunk, and they ran one after the other.

Fix

  • The root endpoints that facets call now start the root's lifecycle themselves, so facets use a plain stub. A root that was evicted still runs onStart() before serving the call. That is the case the extra ensureInitialized call was guarding.
  • A facet skips the root entirely when its hydrated connection mirror shows no connection outside without. Until the mirror is hydrated, broadcasts go to the root as before, so an unknown mirror never drops a message.

The issue also suggested batching broadcasts. I left that out: with at most one call per chunk, and none when nobody is listening, batching would change delivery timing and need a new RPC shape for a smaller gain.

Tests

sub-agent-rpc-bridge.test.ts counts root calls:

  • No root call when the sub-agent has no connections, or when every connection is excluded.
  • Exactly one root call per broadcast to a connected client.
  • A root-held client is still reached after the sub-agent restarts, which is the case where the mirror is not hydrated yet.
  • An evicted root runs onStart() before it serves a sub-agent call.

@changeset-bot

changeset-bot Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 52853b1

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

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

@threepointone
threepointone added this pull request to stack #2407 September 28, 2026 14:47
@threepointone
threepointone marked this pull request as ready for review September 28, 2026 14:49

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

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

Devin Review found 2 potential issues.

Devin Review

Comment thread packages/agents/src/index.ts
Comment thread packages/agents/src/dynamic-agents/dynamic-agents.ts
@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

agents

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

@cloudflare/ai-chat

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

@cloudflare/codemode

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: 52853b1

@threepointone
threepointone force-pushed the fix/2252-facet-broadcast-rpcs branch from ceb270b to a32aa3d Compare September 28, 2026 15:46
@agent-think

agent-think Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

✅ agents import sizes: no significant changes (59cad8a5 → 52853b12, workflow run)

@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

Comment thread packages/agents/src/dynamic-agents/dynamic-agents.ts
threepointone and others added 2 commits September 28, 2026 17:50
…pients (#2252)

Co-authored-by: Cursor <cursoragent@cursor.com>
@threepointone
threepointone force-pushed the fix/2252-facet-broadcast-rpcs branch from a32aa3d to 52853b1 Compare September 28, 2026 16:54
@threepointone
threepointone merged commit cbb859b into main Sep 28, 2026
19 of 33 checks passed
@threepointone
threepointone deleted the fix/2252-facet-broadcast-rpcs branch September 28, 2026 18:04
@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.

Facet broadcasts after the frame completes cost two root RPCs per message (getAgentByName on every broadcast)

1 participant