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/lazy-member-check' into scale-core

Abel Dantas9d6257f6051a
diff --cc CHANGELOG.md
index 4250e777d,6cc25cf8e..4a6760d0e
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@@ -288,25 -233,11 +288,30 @@@ 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.
#### Fixed
- **On Slack, the 👀 and the activity card no longer arrive after the agent's
diff --cc core/tests/switch_core/delivery/test_router_sessions.py
index 073fa4735,8edcbeec8..ab11d7c07
--- a/core/tests/switch_core/delivery/test_router_sessions.py
+++ b/core/tests/switch_core/delivery/test_router_sessions.py
@@@ -209,20 -214,44 +214,63 @@@ 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]