Skip to content

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

Merge remote-tracking branch 'origin/shared-room-ids' into scale-core

Abel Dantasb6d8055a5756
diff --cc CHANGELOG.md
index 0d6eccf38,78c3f22e6..ca21865ba
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@@ -315,60 -224,13 +315,65 @@@ version of their own to them without al
group whose subgroup would move up next to a namesake is refused too, and
the dashboard now shows why. Names can no longer contain `/` or be blank.
Existing duplicates are left as they are and can be reached by id.
+- **Delivering an event to an agent behind a controller no longer reads the
+ agent's head first.** The notification of a new agent event already carries
+ its number, and the controller's stream now keeps it as the agent's head, so
+ a delivery costs the agent's own read alone, where it read the heads of all
+ the controller's agents first. The heads are still read after each idle
+ interval, when an agent is attached, and after the listener reconnects.
+- **Delivering an event to an agent costs three statements, where it cost
+ seven.** The rooms dropped after the agent's cursor are read with its
+ events, and the unread count reads the agent's baseline, the room's window
+ and the agent's user id in the statement that counts.
+- **Writing a message no longer costs four statements for each agent it is
+ for.** The agents' own events are stored together, in two statements and
+ one savepoint, where each agent's event took a savepoint, a numbering, an
+ insert 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.
+- **Reading an agent's own events no longer builds its query each time.**
+ Each shape of the read is built once and run with bind parameters.
+- **A message addressed to a connected agent no longer reads the agent's
+ membership after it commits.** That read stops an agent removed from the
+ room in the meantime from saying anything there, and a connected agent has
+ nothing to say about a message addressed to it, so the read is now made only
+ before a refusal or an offline notice is posted, or a machine woken.
+- **Message telemetry reads a workspace's rooms, clients and agents at once.**
+ The first lookup that misses reads all of them, where each room, sender and
+ agent was read on its own, so in a large workspace most messages no longer
+ cost a lookup.
+- **Registering, renaming or deleting an agent no longer makes every process
+ read every client and agent.** The change now names the clients it touched,
+ and each process reads only those, where it read every client of the
+ workspace and, with a messaging app connected, every agent as well. The
+ 30-second re-check reads every client only when a fingerprint of the
+ workspace's clients and agents has changed since its last full read, where
+ it read them all every time.
+- **The 30-second re-check of which controller runs each agent reads one
+ query, and nothing when no placement changed.** Every process read every
+ placed agent's definition, document included, then each controller with a
+ query of its own. It now reads the columns a binding is made of in one
+ query per workspace, and only when a fingerprint of the placements has
+ changed since its last full read.
+- **The 30-second re-check of who holds each controller's socket no longer
+ reads every controller each time.** It compares a fingerprint of their
+ connections and of the live processes first, and when it reads, reads only
+ the connection columns, where it read every row with its status and
+ platform, and the rooms of every agent on a controller another process held.
+ - **Writing to a room for the first time through a process no longer reads the
+ room.** A process's clients now share the room ids they look up, where each
+ kept its own, and the first one a process misses reads every open room of
+ the workspace at once, so with many agents behind a load balancer most sends
+ no longer start with a read of the room.
#### Fixed
+- **A client in more than 32,767 rooms starts reading them.** The reads a
+ client makes as it starts matched its rooms with one parameter each, and
+ Postgres takes at most 32,767 per statement. Switch's own client is in
+ every room of its workspace, archived ones included, so past that many it
+ failed to start, retried, and failed again while the process ran, taking
+ up rooms one at a time in between. The rooms now go as one array.
- **On Slack, the 👀 and the activity card no longer arrive after the agent's
reply.** Every running turn's card was redrawn every 5 seconds to tick its
clock. Slack closes a streamed card after 10 minutes, so on a longer turn
diff --cc core/tests/switch_core/delivery/test_router_sessions.py
index ab11d7c07,634844db0..47467306b
--- a/core/tests/switch_core/delivery/test_router_sessions.py
+++ b/core/tests/switch_core/delivery/test_router_sessions.py
@@@ -214,63 -209,60 +214,122 @@@ class TestWritingAMessage
assert len(await own_events(session_factory, agents["a3"][0].id)) == 1
+class TestTheWritersOwnProcess:
+ async def test_an_addressed_agent_is_told_its_new_head_here(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ """The process that stores an agent's own event knows its number, so
+ a stream of the agent's in this process takes it as the agent's head
+ rather than reading it (`EventBuffer.ring`)."""
+ process, room, louisa, agents = await _seed(session_factory, names=["member"])
+ agent = agents["member"][0]
+ process.buffer.listening(True)
+ process.buffer.doorbell(agent.id)
+
+ await process.say(room, "@member can you look at this", sender=louisa)
+ await process.idle()
+
+ [event] = await own_events(session_factory, agent.id)
+ assert process.buffer._told_heads == {agent.id: event.seq}
+
+
+class TestAfterTheWrite:
+ async def test_a_message_for_a_connected_agent_asks_nothing_more(
+ self,
+ monkeypatch: pytest.MonkeyPatch,
+ session_factory: async_sessionmaker[AsyncSession],
+ ) -> None:
+ """A connected agent has nothing to say about a message addressed to
+ it, so nothing asks whether it is still in the room once the message
+ has committed: that is asked only before an agent says or wakes
+ something (`Delivery.checks_membership`)."""
+ process = process_on(session_factory)
+ room = await make_room(session_factory)
+ louisa = await make_person(session_factory, "louisa")
+ await add_member(session_factory, room, louisa)
+ agent, client = await make_agent(session_factory, "member")
+ await add_member(session_factory, room, client, agent=agent)
+ process.connections.open(
+ agent_id=agent.id,
+ connection_id="c1",
+ scope="single",
+ delivery_filter="all",
+ spawn_capable=False,
+ position={OWN_EVENTS: 0},
+ declaration=ClientDeclaration(speaks=PROTOCOL_VERSION),
+ expected_generation=None,
+ )
+ asked: list[str] = []
+ still_member = AgentRecipient.still_member
+
+ async def recording(self: AgentRecipient) -> bool:
+ asked.append(self.agent.id)
+ return await still_member(self)
+
+ monkeypatch.setattr(AgentRecipient, "still_member", recording)
+
+ await process.say(room, "@member can you look at this", sender=louisa)
+ await process.idle()
+
+ assert len(await own_events(session_factory, agent.id)) == 1
+ assert asked == []
+ assert [r["agent_live"] for r in process.telemetry.addressed] == [True]
++
++
+ class TestFindingTheRoom:
+ async def test_a_room_one_client_found_costs_the_next_no_read(
+ self,
+ session_factory: async_sessionmaker[AsyncSession],
+ pool_checkouts: PoolCheckouts,
+ ) -> None:
+ """Every client looks a room up before its first write there. The
+ answers are shared by the process's clients, so a room another client
+ already wrote to costs the write alone (`RoomIds`)."""
+ process, room, _, _ = await _seed(session_factory, names=["member"])
+ rita = await make_person(session_factory, "rita")
+ await add_member(session_factory, room, rita)
+
+ with pool_checkouts.scope("say") as scope:
+ await process.say(room, "first time here", sender=rita)
+
+ assert (scope.taken, scope.peak) == (1, 1)
+
+ async def test_the_first_room_found_finds_every_open_room_of_the_tenant(
+ self,
+ session_factory: async_sessionmaker[AsyncSession],
+ pool_checkouts: PoolCheckouts,
+ ) -> None:
+ """The first miss in a process reads every open room of the tenant,
+ so a room nobody in the process has written to yet costs no read of
+ its own either; a room archived by then is left to be read on its own."""
+ process = process_on(session_factory)
+ louisa = await make_person(session_factory, "louisa")
+ rooms = [await make_room(session_factory, f"room-{n}") for n in range(3)]
+ for room in rooms:
+ await add_member(session_factory, room, louisa)
+ await process.say(rooms[0], "hello", sender=louisa)
+ rita = await make_person(session_factory, "rita")
+ await add_member(session_factory, rooms[2], rita)
+
+ with pool_checkouts.scope("say") as scope:
+ await process.say(rooms[2], "first time anyone writes here", sender=rita)
+
+ assert (scope.taken, scope.peak) == (1, 1)
+
+ async def test_a_room_created_after_the_first_read_is_read_on_its_own(
+ self,
+ session_factory: async_sessionmaker[AsyncSession],
+ pool_checkouts: PoolCheckouts,
+ ) -> None:
+ process = process_on(session_factory)
+ louisa = await make_person(session_factory, "louisa")
+ first = await make_room(session_factory, "first")
+ await add_member(session_factory, first, louisa)
+ await process.say(first, "hello", sender=louisa)
+ later = await make_room(session_factory, "later")
+ await add_member(session_factory, later, louisa)
+
+ with pool_checkouts.scope("say") as scope:
+ await process.say(later, "a room made since", sender=louisa)
+
+ assert (scope.taken, scope.peak) == (2, 1)