Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
Have a controller's stream wait on one bell, not one per agent (CHOO-3667)
Abel Dantas7e6144584880
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 51277ff92..06dd2a8cd 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -237,6 +237,9 @@ version of their own to them without also giving them a release of their own.for.** The agents' own events are stored together, in two statements andone savepoint, where each agent's event took a savepoint, a numbering, aninsert and a release of its own.+- **A controller's stream waits on three events, where it waited on one per+ agent and two more.** Every attached agent's doorbell now rings the stream's+ one bell.#### Fixed- **The agent HTTP API answers a refused or malformed request with a 4xx, notdiff --git a/core/switch_core/bridges/agent/protocol/controller_stream.py b/core/switch_core/bridges/agent/protocol/controller_stream.pyindex cdff0bd71..33461d600 100644--- a/core/switch_core/bridges/agent/protocol/controller_stream.py+++ b/core/switch_core/bridges/agent/protocol/controller_stream.py@@ -186,6 +186,9 @@ class _ControllerStream:)self._tenant: str = self._tenant_idself._attached: dict[str, _Attached] = {}+ # Rung with the doorbell of every attached agent (`EventBuffer.listen`),+ # so a wait is on this one event rather than on one per agent.+ self._bell = asyncio.Event()# Whether the next pass reads every head from the database rather# than taking the heads this process was told (`EventBuffer.heads`):# the first pass, and the pass after an idle interval with no wake,@@ -256,12 +259,11 @@ class _ControllerStream:yield raw# Cleared before the pass reads the heads, so an event- # added after that read rings its agent's doorbell again- # and the wait below returns at once. The pass is then the- # only read of the heads, rather than the heads being read- # again after delivering and again before waiting.- for agent_id in self._attached:- self._buffer.doorbell(agent_id).clear()+ # added after that read rings the bell again and the wait+ # below returns at once. The pass is then the only read of+ # the heads, rather than the heads being read again after+ # delivering and again before waiting.+ self._bell.clear()behind = Falsefor attached, head in await self._due():for raw in await self._deliver(attached, head):@@ -290,6 +292,7 @@ class _ControllerStream:yield IDLEfinally:for attached in self._attached.values():+ self._buffer.stop_listening(attached.agent_id, self._bell)if attached.unsubscribe is not None:attached.unsubscribe()self._nudges.close()@@ -336,6 +339,7 @@ class _ControllerStream:if agent_id in bound and agent_id not in owed:continueattached = self._attached.pop(agent_id)+ self._buffer.stop_listening(agent_id, self._bell)# Bound here again later, it starts at its head: what it missed in# between was never this controller's.conn.resume_cursors.pop(agent_id, None)@@ -425,6 +429,7 @@ class _ControllerStream:)self._subscribe_outcomes(attached)self._attached[agent_id] = attached+ self._buffer.listen(agent_id, self._bell)yield frame("agent.attached",{"agent_id": agent_id, "from_seq": cursor, "rooms": sorted(rooms)},@@ -583,20 +588,19 @@ class _ControllerStream:async def _wait(self) -> bool:"""Wait for an event, a nudge or a change to the connection. False whenthe idle interval passed with none of them."""- bells = [self._buffer.doorbell(agent_id) for agent_id in self._attached]self._conn.wake.clear()- # A doorbell set now was rung after the pass cleared it, for an event- # the pass may not have read. The connection is re-checked after its- # wake is cleared, for any change that arrived since the pass began.+ # A bell set now was rung after the pass cleared it, for an event the+ # pass may not have read. The connection is re-checked after its wake+ # is cleared, for any change that arrived since the pass began.if (- any(bell.is_set() for bell in bells)+ self._bell.is_set()or self._connection_changed()or self._nudges.wake.is_set()):return Truewaiters = [asyncio.ensure_future(event.wait())- for event in (*bells, self._conn.wake, self._nudges.wake)+ for event in (self._bell, self._conn.wake, self._nudges.wake)]try:done, _ = await asyncio.wait(diff --git a/core/switch_core/bridges/agent/protocol/event_buffer.py b/core/switch_core/bridges/agent/protocol/event_buffer.pyindex 37022d980..387314a41 100644--- a/core/switch_core/bridges/agent/protocol/event_buffer.py+++ b/core/switch_core/bridges/agent/protocol/event_buffer.py@@ -516,6 +516,10 @@ class EventBuffer:# listener sets it for own events written by other processes. The events# themselves are always read from `agent_events`.self._notify: dict[str, asyncio.Event] = {}+ # Events of readers that serve several agents at once (a controller's+ # stream), set with each of those agents' doorbells, so such a reader+ # waits on one event rather than one per agent (`listen`).+ self._bells: dict[str, set[asyncio.Event]] = {}# The head of each agent read here, as this process was last told it:# by the notification of an own event written anywhere, or by a write# of its own. Kept only while the notification listener is listening,@@ -1025,7 +1029,7 @@ class EventBuffer:A wake-up is advisory: callers re-read from their position rather thantrusting this to deliver anything."""- notify = self._notify.setdefault(agent_id, asyncio.Event())+ notify = self.doorbell(agent_id)notify.clear()try:await asyncio.wait_for(notify.wait(), timeout=timeout)@@ -1040,7 +1044,28 @@ class EventBuffer:signal carries no payload, so a spurious or missed wake-up costslatency, never correctness."""- return self._notify.setdefault(agent_id, asyncio.Event())+ notify = self._notify.get(agent_id)+ if notify is None:+ notify = self._notify[agent_id] = asyncio.Event()+ return notify++ def listen(self, agent_id: str, bell: asyncio.Event) -> None:+ """Set `bell` whenever the agent's doorbell rings, until+ `stop_listening`: for a reader serving several agents, which then+ waits on its one `bell` rather than on a doorbell per agent."""+ self._bells.setdefault(agent_id, set()).add(bell)++ def stop_listening(self, agent_id: str, bell: asyncio.Event) -> None:+ bells = self._bells.get(agent_id)+ if bells is None:+ return+ bells.discard(bell)+ if not bells:+ del self._bells[agent_id]++ def _served(self, agent_id: str) -> bool:+ """Whether a reader in this process waits on the agent's doorbell."""+ return agent_id in self._notify or agent_id in self._bellsdef ring(self, agent_id: str, seq: int | None = None) -> None:"""Set the agent's doorbell in this process, and keep `seq`, the@@ -1054,13 +1079,17 @@ class EventBuffer:head kept, and the next pass reads it."""notify = self._notify.get(agent_id)- if notify is None:+ bells = self._bells.get(agent_id)+ if notify is None and bells is None:returnif seq is None or not self._listening:self._told_heads.pop(agent_id, None)else:self._told_heads[agent_id] = max(seq, self._told_heads.get(agent_id, seq))- notify.set()+ if notify is not None:+ notify.set()+ for bell in bells or ():+ bell.set()def ring_everyone(self) -> None:"""Set every doorbell in this process, and forget every head kept.@@ -1072,6 +1101,9 @@ class EventBuffer:self._told_epoch += 1for notify in self._notify.values():notify.set()+ for bells in self._bells.values():+ for bell in bells:+ bell.set()def listening(self, now_listening: bool) -> None:"""Told by the notification listener each time it starts or stops@@ -1134,7 +1166,7 @@ class EventBuffer:self._told_heads.get(agent_id, 0),)heads[agent_id] = head- if keep and agent_id in self._notify:+ if keep and self._served(agent_id):self._told_heads[agent_id] = headreturn heads@@ -1989,6 +2021,7 @@ class EventBuffer:row in `agents` (`ON DELETE CASCADE`)."""self._notify.pop(agent_id, None)+ self._bells.pop(agent_id, None)self._told_heads.pop(agent_id, None)# ------------------------------------------------------------------diff --git a/core/tests/switch_core/management/test_stream.py b/core/tests/switch_core/management/test_stream.pyindex c0e60ff6d..010e11ac0 100644--- a/core/tests/switch_core/management/test_stream.py+++ b/core/tests/switch_core/management/test_stream.py@@ -1130,6 +1130,37 @@ async def test_a_delivery_told_its_head_costs_three_statements(assert len(statements) == 3, statements+async def test_a_wait_watches_one_bell_whatever_the_number_of_agents(+ harness: Harness, monkeypatch: pytest.MonkeyPatch+) -> None:+ """Every attached agent's doorbell rings the stream's one bell, so a wait+ is on three events (the bell, the connection's wake and the nudges'), not+ one per agent and two more."""+ monkeypatch.setattr(controller_routes, "IDLE_INTERVAL_SECONDS", 0.05)+ owner = await add_member(harness.session_factory, "ada")+ async with harness.client() as client:+ controller = await enroll_console(harness, client, owner)+ for n in range(5):+ agent_id = await place_agent(client, controller, name=f"agent-{n}")+ await add_room(harness.session_factory, agent_id, name=f"room-{n}")+ opened = await open_connection(client, controller)+ stream = await open_stream(harness, controller, opened)+ await take(stream, 6)+ assert await anext(stream) is controller_routes.IDLE+ waited: list[object] = []+ ensure_future = asyncio.ensure_future++ def counting(awaitable: Any) -> Any:+ waited.append(awaitable)+ return ensure_future(awaitable)++ monkeypatch.setattr(controller_stream.asyncio, "ensure_future", counting)+ assert await anext(stream) is controller_routes.IDLE+ await stream.aclose()++ assert len(waited) == 3++class TestToldHeads:"""What `EventBuffer.heads` takes from the doorbells instead of reading."""