Skip to content

Have a controller's stream wait on one bell, not one per agent (CHOO-3667) - #1253

Closed
abeldantas wants to merge 1 commit into
batch-own-eventsfrom
one-stream-bell
Closed

abeldantas wants to merge 1 commit into
batch-own-eventsfrom
one-stream-bell

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

Each wait of a controller's stream started a waiter for the doorbell of every agent on it, two more for the connection and the nudges, and cancelled them all when one fired. With 20 agents on a controller that was 22 tasks for every wait, and a CPU profile of a load test put the waiting among the larger costs of a delivery.

In this PR the stream has one bell, which every attached agent's doorbell rings (EventBuffer.listen), so a wait watches three events. Reading a doorbell also no longer builds an asyncio.Event it then throws away.

Stack (merge order)

  1. Make idle controller agents and startup cheap on the database as deployments grow (CHOO-3667) #1200
  2. Read a controller's agents' heads once per delivery, not three times (CHOO-3667) #1220
  3. Try a controller's delivery pass again when the pool has no connection, instead of closing its socket (CHOO-3667) #1221
  4. Take an agent's head from the notification of its new event (CHOO-3667) #1246
  5. Deliver an agent's event in three statements, not seven (CHOO-3667) #1251
  6. Store a message's own events together, in two statements (CHOO-3667) #1252
  7. This PR ← you are here

…3667)

Each wait of a controller's stream started a waiter for the doorbell of
every agent on it, two more for the connection and the nudges, and
cancelled them all when one fired: with 20 agents that was 22 tasks per
wait, and a CPU profile of a load test put the waiting among the larger
costs of a delivery. The stream now has one bell that every attached
agent's doorbell rings (EventBuffer.listen), so a wait watches three
events. Reading a doorbell no longer builds an Event it then discards.

Copilot AI 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.

🟢 Approval recommended

The review records no active issues, with the shared-bell optimization accompanied by regression coverage and documentation despite the noted stale-head caveat.

No open issues 🎉

What changed in this PR

This pull request reduces controller-stream waiting overhead by replacing per-agent waiters with one shared event bell per stream.

Changes:

  • Added shared bell listener registration and fan-out to EventBuffer.
  • Updated controller streams to wait on one shared bell plus connection and nudge events.
  • Added regression coverage for constant wait-task counts.
  • Documented the optimization in the changelog.
  • A moderate issue remains around stale cached heads when an agent is detached and rebound.
File Summary
core/​tests/​switch_core/​management/​test_stream.py Verifies that wait-task counts remain constant with multiple attached agents.
core/​switch_core/​bridges/​agent/​protocol/​event_buffer.py Adds shared bell fan-out and listener lifecycle handling; cached heads may remain stale across agent detach/rebind cycles.
core/​switch_core/​bridges/​agent/​protocol/​controller_stream.py Uses one shared bell for attached agents and waits on three events.
CHANGELOG.md Documents the controller-stream waiting optimization.

Review effort: Lite

@abeldantas

Copy link
Copy Markdown
Collaborator Author

Combined into #1307 with the other scaling PRs.

@abeldantas abeldantas closed this Nov 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.

2 participants