Skip to content

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

Multipod chat tests (CHOO-2810) (#1113)

Abel Dantas75d0e2f5d4fd
diff --git a/core/tests/switch_core/gateway/conftest.py b/core/tests/switch_core/gateway/conftest.py
index 3c984d5a3..e83d3fa69 100644
--- a/core/tests/switch_core/gateway/conftest.py
+++ b/core/tests/switch_core/gateway/conftest.py
@@ -47,7 +47,10 @@ from switch_core.process_identity import new_process_id
from switch_core.process_lease import ProcessLease
from switch_core.provisioning.postgres import PostgresProvisioning
from switch_core.room_service import RoomService
-from switch_core.session_activity.listener import Change
+from switch_core.session_activity.listener import (
+ AgentSessionActivityListener,
+ Change,
+)
from switch_core.signals import SignalBus
from switch_core.transport.invites import InviteBus
from tests.conftest import TEST_KEYRING
@@ -406,3 +409,30 @@ async def two_processes(
for run in runs:
run.cancel()
await asyncio.gather(*runs, return_exceptions=True)
+
+
+async def listening_processes(
+ two_processes: tuple[_Process, _Process], postgres_url: str
+) -> AsyncIterator[tuple[_Process, _Process]]:
+ """`two_processes`, each told of session activity by its own listener on
+ the real `NOTIFY` channel, as a switch-core process is, rather than by the
+ test."""
+ listeners = []
+ for process in two_processes:
+ listener = AgentSessionActivityListener(
+ lambda: create_async_engine(postgres_url, poolclass=NullPool)
+ )
+ process.service.activity_listener = listener
+ await listener.start()
+ listeners.append(listener)
+ try:
+ for listener in listeners:
+ await asyncio.wait_for(listener.connected.wait(), _CONNECT_SECONDS)
+ # Let each listener's own connect-time resync pass before any stream
+ # subscribes, or a stream can read it after `ready`.
+ await asyncio.sleep(0.1)
+ yield two_processes
+ finally:
+ for listener in listeners:
+ await listener.stop()
diff --git a/core/tests/switch_core/gateway/test_chats_routes.py b/core/tests/switch_core/gateway/test_chats_routes.py
index a2ecc9527..d401db3c0 100644
--- a/core/tests/switch_core/gateway/test_chats_routes.py
+++ b/core/tests/switch_core/gateway/test_chats_routes.py
@@ -1826,3 +1826,46 @@ async def _events_of(chats: _Harness, agent: Agent) -> list[tuple[str, int]]:
)
)
return [(room_id, room_seq) for room_id, room_seq in rows.all()]
+
+
+# ── Activity across processes ────────────────────────────────────────────────
+
+
+def _activity_stream_on(process: _Process, user: User) -> _Events:
+ return _Events(
+ chat_activity_events(
+ process.service,
+ TENANT,
+ user,
+ ping_seconds=60.0,
+ queue_max=64,
+ )
+ )
+
+
+async def test_a_step_reaches_the_activity_streams_of_every_process(
+ chats: _Harness, listening_processes: tuple[_Process, _Process]
+) -> None:
+ """A step is written by whichever process its agent reports to, and the
+ Console streams showing its room are held by any process: each one hears
+ of the step from the database, not from the process that wrote it."""
+ here, there = listening_processes
+ agent = await chats.agent("helper", owner=chats.alice)
+ room_id = await chats.new_chat(chats.alice, agent, "c1")
+ other_agent = await chats.agent("other", owner=chats.bob)
+ hidden = await chats.new_chat(chats.bob, other_agent, "c2")
+ streams = [
+ _activity_stream_on(here, chats.alice),
+ _activity_stream_on(there, chats.alice),
+ ]
+ for events in streams:
+ assert (await events.next())[0] == "ready"
+
+ # A room Alice is not in reaches neither stream: the next frame is hers.
+ await _step(chats, other_agent, hidden, "t9", "turn", 1)
+ await _step(chats, agent, room_id, "t1", "turn", 1)
+
+ for events in streams:
+ name, item = await events.next()
+ assert (name, item["roomId"], item["turnId"]) == ("activity", room_id, "t1")
+ await events.stream.aclose() # type: ignore[attr-defined]