Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Read a controller's agents' heads once per delivery, not three times (CHOO-3667)

Abel Dantasd9a84fee6414
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 27f7ced1d..36f64bc9b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -155,6 +155,11 @@ version of their own to them without also giving them a release of their own.
each process's 30-second re-check of controllers held by another process
read every agent's rooms with a query of its own, which grew with
processes times agents; it is now one query per controller.
+- **A delivery to an agent behind a controller takes two database
+ transactions instead of four.** The controller's stream read the heads of
+ all its agents to find the work, again after delivering, and again before
+ waiting. It now reads them once a pass, and an event that arrives after
+ that read wakes the stream for the next pass.
- **An agent's stream reads in one database session per pass, and pooled
connections are no longer pinged before use.** A pass that delivered a
message opened a session for each of its reads, six pool checkouts and six
diff --git a/core/switch_core/bridges/agent/protocol/controller_stream.py b/core/switch_core/bridges/agent/protocol/controller_stream.py
index db90e0e35..57d7e4d73 100644
--- a/core/switch_core/bridges/agent/protocol/controller_stream.py
+++ b/core/switch_core/bridges/agent/protocol/controller_stream.py
@@ -218,12 +218,21 @@ class _ControllerStream:
for raw in await self._owed_outcomes():
yield raw
- delivered = False
+ # 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()
+ behind = False
for attached, head in await self._due():
for raw in await self._deliver(attached, head):
- delivered = True
yield raw
- if delivered:
+ # A delivery stops at `CATCH_UP_BATCH` events or at a gap,
+ # and no doorbell rings for events already there.
+ behind = behind or attached.cursor < head
+ if behind:
continue
if not await self._wait():
@@ -483,9 +492,9 @@ class _ControllerStream:
)
return frames
- async def _has_work(self) -> bool:
+ def _connection_changed(self) -> bool:
conn = self._conn
- if (
+ return (
conn.closure is not None
or conn.stream_token != self._token
or bool(
@@ -496,23 +505,21 @@ class _ControllerStream:
)
or self._presence.agents_of(conn.controller_id) != set(self._attached)
or any(a.outcomes or a.resync for a in self._attached.values())
- ):
- return True
- if not self._attached:
- return False
- heads = await self._buffer.heads(self._tenant, set(self._attached))
- return any(heads.get(a.agent_id, 0) > a.cursor for a in self._attached.values())
+ )
async def _wait(self) -> bool:
"""Wait for an event, a nudge or a change to the connection. False when
the idle interval passed with none of them."""
bells = [self._buffer.doorbell(agent_id) for agent_id in self._attached]
- for bell in bells:
- bell.clear()
self._conn.wake.clear()
- # Re-checked after clearing: anything that arrived between the last
- # read and the clear would otherwise wait for the idle timeout.
- if await self._has_work() or self._nudges.wake.is_set():
+ # 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.
+ if (
+ any(bell.is_set() for bell in bells)
+ or self._connection_changed()
+ or self._nudges.wake.is_set()
+ ):
return True
waiters = [
asyncio.ensure_future(event.wait())
diff --git a/core/tests/switch_core/management/test_stream.py b/core/tests/switch_core/management/test_stream.py
index af6c54f14..b7b8337f2 100644
--- a/core/tests/switch_core/management/test_stream.py
+++ b/core/tests/switch_core/management/test_stream.py
@@ -8,12 +8,14 @@ since the in-process HTTP transport would wait for a stream that never ends.
from __future__ import annotations
import asyncio
+import contextlib
import time
import pytest
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from switch_core.bridges.agent.commands import room_control_frame
+from switch_core.bridges.agent.protocol import controller_stream
from switch_core.bridges.agent.protocol.agent_detail import assemble_agent_detail
from switch_core.bridges.agent.protocol.liveness import (
HEARTBEAT_INTERVAL_SECONDS,
@@ -802,6 +804,97 @@ async def test_an_idle_pass_reads_its_agents_heads_together(
assert await anext(stream) is controller_routes.IDLE
await stream.aclose()
- # One read of the heads for the pass, one more when the wait checks
- # again for anything that arrived as it began.
+ # One read of the heads for the pass, and none when it waits.
+ assert counting.opened == 1
+
+
+async def test_a_delivery_reads_the_heads_once(
+ harness: Harness, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """From the doorbell to waiting again, a delivery costs the read of the
+ heads and the agent's pass: the heads are not read again after
+ delivering, nor before waiting."""
+ monkeypatch.setattr(controller_routes, "IDLE_INTERVAL_SECONDS", 60.0)
+ owner = await add_member(harness.session_factory, "ada")
+ async with harness.client() as client:
+ controller = await enroll_console(harness, client, owner)
+ agent_id = await place_agent(client, controller, name="reviewer")
+ room_id = await add_room(harness.session_factory, agent_id)
+ opened = await open_connection(client, controller)
+ stream = await open_stream(harness, controller, opened)
+ await take(stream, 2)
+ await _ask(harness, agent_id, room_id, "@you")
+ buffer = harness.protocol.event_buffer
+ counting = CountingSessionFactory(buffer._session_factory)
+ monkeypatch.setattr(buffer, "_session_factory", counting)
+
+ [(name, _)] = await take(stream, 1)
+ waiting = asyncio.ensure_future(anext(stream))
+ await asyncio.sleep(0.2)
+ waiting.cancel()
+ with contextlib.suppress(asyncio.CancelledError):
+ await waiting
+ await stream.aclose()
+
+ assert name == "agent.event"
assert counting.opened == 2
+
+
+async def test_an_event_added_during_a_pass_is_sent_without_waiting(
+ harness: Harness, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """An event added after the pass read the heads is past what the pass
+ reads, and its doorbell brings the next pass at once rather than after
+ the idle interval."""
+ monkeypatch.setattr(controller_routes, "IDLE_INTERVAL_SECONDS", 60.0)
+ owner = await add_member(harness.session_factory, "ada")
+ async with harness.client() as client:
+ controller = await enroll_console(harness, client, owner)
+ agent_id = await place_agent(client, controller, name="reviewer")
+ room_id = await add_room(harness.session_factory, agent_id)
+ opened = await open_connection(client, controller)
+ stream = await open_stream(harness, controller, opened)
+ await take(stream, 2)
+
+ buffer = harness.protocol.event_buffer
+ read_from = buffer.read_from
+ added: list[asyncio.Task[int]] = []
+
+ async def read_while_another_arrives(*args: object, **kwargs: object) -> object:
+ if not added:
+ added.append(asyncio.create_task(_ask(harness, agent_id, room_id, "2nd")))
+ await added[0]
+ return await read_from(*args, **kwargs) # type: ignore[arg-type]
+
+ monkeypatch.setattr(buffer, "read_from", read_while_another_arrives)
+ await _ask(harness, agent_id, room_id, "1st")
+ frames = await asyncio.wait_for(take(stream, 2), timeout=5)
+ await stream.aclose()
+
+ assert [data["event"]["payload"]["body"] for _, data in frames] == ["1st", "2nd"]
+
+
+async def test_a_backlog_past_one_batch_is_sent_without_waiting(
+ harness: Harness, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """No doorbell rings for events already there, so a pass that stops at
+ the batch size reads again at once."""
+ monkeypatch.setattr(controller_routes, "IDLE_INTERVAL_SECONDS", 60.0)
+ monkeypatch.setattr(controller_stream, "CATCH_UP_BATCH", 2)
+ owner = await add_member(harness.session_factory, "ada")
+ async with harness.client() as client:
+ controller = await enroll_console(harness, client, owner)
+ agent_id = await place_agent(client, controller, name="reviewer")
+ room_id = await add_room(harness.session_factory, agent_id)
+ opened = await open_connection(client, controller)
+ stream = await open_stream(harness, controller, opened)
+ await take(stream, 2)
+ for n in range(5):
+ await _ask(harness, agent_id, room_id, f"#{n}")
+
+ frames = await asyncio.wait_for(take(stream, 5), timeout=5)
+ await stream.aclose()
+
+ assert [data["event"]["payload"]["body"] for _, data in frames] == [
+ f"#{n}" for n in range(5)
+ ]