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
feat(core): wake the console activity stream only for readable rooms
raullopez-sandbox62d2ded9dcc8
diff --git a/core/switch_core/gateway/chats.py b/core/switch_core/gateway/chats.pyindex 7b9d6b7c5..483bea6f6 100644--- a/core/switch_core/gateway/chats.py+++ b/core/switch_core/gateway/chats.py@@ -12,6 +12,7 @@ Refusals are `ChatError`s, answered as `{"detail": {"code", "message"}}`.from __future__ import annotationsimport asyncio+import contextlibimport jsonimport loggingimport time@@ -57,7 +58,7 @@ from switch_core.db.models import (from switch_core.db.session_scope import tenant_sessionfrom switch_core.gateway.agent_sessions import ApprovalRequestViewfrom switch_core.gateway.auth import get_current_user-from switch_core.session_activity.listener import Change+from switch_core.session_activity.listener import Change, RoomSubscriptionlogger = logging.getLogger(__name__)@@ -69,6 +70,10 @@ RECHECK_SECONDS = 30.0_CATCH_UP_PAGE = 200# Revisions one activity stream remembers before forgetting them all._ACTIVITY_SENT_MAX = 10_000+# Activity stream wake-ups that are not a change: reload everything, or+# re-read the caller's rooms.+_RESYNC = object()+_MEMBERSHIPS = object()MAX_PAGE = 200@@ -337,6 +342,7 @@ async def chat_activity_stream(tenant_id,user,ping_seconds=PING_SECONDS,+ recheck_seconds=RECHECK_SECONDS,queue_max=ACTIVITY_QUEUE_MAX,),media_type="text/event-stream",@@ -821,62 +827,103 @@ async def chat_activity_events(user: User,*,ping_seconds: float,+ recheck_seconds: float,queue_max: int,) -> AsyncIterator[bytes]:"""Turn steps and approval requests of the rooms the caller may read, as SSE.Says `ready` once subscribed, so a snapshot fetched after it misses- nothing. Readability is checked on every change, so leaving a room stops- its frames at once. `resync` means changes may have been missed (the- listener reconnected, or this connection fell behind): reload snapshots.+ nothing. Only changes in the caller's rooms wake the stream; the rooms are+ re-read when the caller's chats change, on a resync and every+ `recheck_seconds`. Readability is still checked on every change, so+ leaving a room stops its frames at once. `resync` means changes may have+ been missed (the listener reconnected, this connection fell behind, or the+ caller joined a room): reload snapshots."""- queue: asyncio.Queue[Change | None] = asyncio.Queue(maxsize=queue_max)+ queue: asyncio.Queue[object] = asyncio.Queue(maxsize=queue_max)def resync() -> None:while not queue.empty():queue.get_nowait()- queue.put_nowait(None)+ queue.put_nowait(_RESYNC)- async def on_change(change: Change) -> None:+ def put(item: object) -> None:try:- queue.put_nowait(change)+ queue.put_nowait(item)except asyncio.QueueFull:resync()+ async def on_change(change: Change) -> None:+ put(change)+async def on_resync() -> None:resync()- unsubscribe = service.activity_listener.subscribe(tenant_id, on_change, on_resync)+ def on_memberships() -> None:+ put(_MEMBERSHIPS)++ service.watch_memberships(tenant_id, user.id, on_memberships)+ subscription: RoomSubscription | None = None# Per-connection, so a turn reread on every change resends only moved steps.sent: dict[tuple[str, str, str, str], int] = {}try:await service.sync_owned_rooms(tenant_id, user.id)+ rooms = await _readable_room_ids(service, tenant_id, user)+ if rooms is None:+ return+ subscription = service.activity_listener.subscribe_rooms(+ tenant_id, rooms, on_change, on_resync+ )yield _frame("ready", {})+ now = time.monotonic()+ next_ping = now + ping_seconds+ next_recheck = now + recheck_secondswhile True:- try:- change = await asyncio.wait_for(queue.get(), timeout=ping_seconds)- except TimeoutError:- async with tenant_session(- service.session_factory, tenant_id- ) as session:- if not await service.has_tenant_role(session, tenant_id, user.id):- return+ timeout = max(0.0, min(next_ping, next_recheck) - time.monotonic())+ item: object = None+ with contextlib.suppress(TimeoutError):+ item = await asyncio.wait_for(queue.get(), timeout=timeout)+ now = time.monotonic()+ if now >= next_ping:+ next_ping = now + ping_secondsyield b": ping\n\n"- continue- if change is None:- sent.clear()- yield _frame("resync", {})- continue- if len(sent) > _ACTIVITY_SENT_MAX:- # Forgetting only resends steps the client already holds.- sent.clear()- frames = await _activity_frames(service, tenant_id, user, change, sent)- if frames is None:- return- for frame in frames:- yield frame+ recheck = now >= next_recheck+ if recheck:+ next_recheck = now + recheck_seconds+ if recheck or item is _RESYNC or item is _MEMBERSHIPS:+ current = await _readable_room_ids(service, tenant_id, user)+ if current is None:+ return+ subscription.set_rooms(current)+ added = current - rooms+ rooms = current+ if item is _RESYNC or added:+ sent.clear()+ yield _frame("resync", {})+ if isinstance(item, Change):+ if len(sent) > _ACTIVITY_SENT_MAX:+ # Forgetting only resends steps the client already holds.+ sent.clear()+ frames = await _activity_frames(service, tenant_id, user, item, sent)+ if frames is None:+ return+ for frame in frames:+ yield framefinally:- unsubscribe()+ if subscription is not None:+ subscription.close()+ service.unwatch_memberships(tenant_id, user.id, on_memberships)+++async def _readable_room_ids(+ service: ChatService, tenant_id: str, user: User+) -> set[str] | None:+ """The rooms the caller may read now; None once the caller has no tenant+ role, which ends the stream."""+ async with tenant_session(service.session_factory, tenant_id) as session:+ if not await service.has_tenant_role(session, tenant_id, user.id):+ return None+ return {room.id for room in await service.readable_rooms(session, user.id)}async def _activity_frames(diff --git a/core/tests/switch_core/gateway/conftest.py b/core/tests/switch_core/gateway/conftest.pyindex 6e82f0389..dd09857cc 100644--- a/core/tests/switch_core/gateway/conftest.py+++ b/core/tests/switch_core/gateway/conftest.py@@ -95,24 +95,66 @@ class _FakeListener:await waker(room_id)+class _FakeRoomSubscription:+ def __init__(+ self,+ listener: _FakeActivityListener,+ tenant_id: str,+ rooms: Any,+ on_change: Any,+ on_resync: Any,+ ) -> None:+ self.listener = listener+ self.tenant_id = tenant_id+ self.rooms = set(rooms)+ self.on_change = on_change+ self.on_resync = on_resync+ self.delivered = 0++ def set_rooms(self, rooms: Any) -> None:+ self.rooms = set(rooms)++ def close(self) -> None:+ self.listener.room_subscriptions.remove(self)++class _FakeActivityListener:"""The session-activity listener; a test delivers changes by hand."""def __init__(self) -> None:self.subscribers: list[tuple[Any, Any]] = []+ self.room_subscriptions: list[_FakeRoomSubscription] = []def subscribe(self, tenant_id: str, on_change: Any, on_resync: Any) -> Any:entry = (on_change, on_resync)self.subscribers.append(entry)return lambda: self.subscribers.remove(entry)+ def subscribe_rooms(+ self, tenant_id: str, rooms: Any, on_change: Any, on_resync: Any+ ) -> _FakeRoomSubscription:+ subscription = _FakeRoomSubscription(+ self, tenant_id, rooms, on_change, on_resync+ )+ self.room_subscriptions.append(subscription)+ return subscription+async def change(self, change: Change) -> None:for on_change, _ in list(self.subscribers):await on_change(change)+ for subscription in list(self.room_subscriptions):+ if (+ subscription.tenant_id == change.tenant_id+ and change.room_id in subscription.rooms+ ):+ subscription.delivered += 1+ await subscription.on_change(change)async def resync(self) -> None:for _, on_resync in list(self.subscribers):await on_resync()+ for subscription in list(self.room_subscriptions):+ await subscription.on_resync()class _Harness(SimpleNamespace):diff --git a/core/tests/switch_core/gateway/test_chats_routes.py b/core/tests/switch_core/gateway/test_chats_routes.pyindex cb5403f83..27fd84dd3 100644--- a/core/tests/switch_core/gateway/test_chats_routes.py+++ b/core/tests/switch_core/gateway/test_chats_routes.py@@ -1622,7 +1622,11 @@ def _turn_change(agent: Agent, room_id: str, turn_id: str) -> Change:def _activity_stream(- chats: _Harness, user: User, queue_max: int = 64, ping_seconds: float = 60.0+ chats: _Harness,+ user: User,+ queue_max: int = 64,+ ping_seconds: float = 60.0,+ recheck_seconds: float = 60.0,) -> _Events:return _Events(chat_activity_events(@@ -1630,11 +1634,25 @@ def _activity_stream("00000000-0000-0000-0000-000000000000",user,ping_seconds=ping_seconds,+ recheck_seconds=recheck_seconds,queue_max=queue_max,))+async def _silently_leave(chats: _Harness, user: User, room_id: str) -> None:+ """Take the user out of the room without telling any stream."""+ async with chats.factory() as session:+ client = await chats.service.member_client(session, user.id)+ assert client is not None+ await session.execute(+ delete(ClientRoom).where(+ ClientRoom.client_id == client.id, ClientRoom.room_id == room_id+ )+ )+ await session.commit()++async def test_room_activity_is_the_rooms_steps_and_requests_in_every_state(chats: _Harness,) -> None:@@ -1736,6 +1754,7 @@ async def test_the_activity_stream_sends_only_readable_rooms_and_moved_steps("answered",)+ [subscription] = chats.activity.room_subscriptionsremoved = await chats.client.delete(f"/chats/{room_id}/members/{chats.bob.id}", headers=chats.as_user(chats.alice))@@ -1744,8 +1763,9 @@ async def test_the_activity_stream_sends_only_readable_rooms_and_moved_steps(await chats.activity.change(_turn_change(agent, room_id, "t1"))with pytest.raises(TimeoutError):await events.next(timeout=0.3)+ assert room_id not in subscription.roomsawait events.stream.aclose() # type: ignore[attr-defined]- assert chats.activity.subscribers == []+ assert chats.activity.room_subscriptions == []async def test_the_activity_stream_says_resync_when_changes_may_be_lost(@@ -1770,6 +1790,157 @@ async def test_the_activity_stream_says_resync_when_changes_may_be_lost(await events.stream.aclose() # type: ignore[attr-defined]+async def test_the_activity_stream_rereads_its_rooms_on_a_resync(+ chats: _Harness,+) -> None:+ agent = await chats.agent("helper", owner=chats.alice)+ room_id = await chats.new_chat(chats.alice, agent, "c1")+ assert (await chats.invite(chats.alice, room_id, chats.bob)).status_code == 200+ events = _activity_stream(chats, chats.bob)+ assert (await events.next())[0] == "ready"+ [subscription] = chats.activity.room_subscriptions+ assert subscription.rooms == {room_id}++ await _silently_leave(chats, chats.bob, room_id)+ await chats.activity.resync()+ assert await events.next() == ("resync", {})+ assert subscription.rooms == set()+ await events.stream.aclose() # type: ignore[attr-defined]+++async def test_the_activity_stream_wakes_only_for_the_readers_rooms(+ chats: _Harness, monkeypatch: pytest.MonkeyPatch+) -> None:+ 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)+ unrelated = await chats.new_chat(chats.bob, other_agent, "c2")+ events = _activity_stream(chats, chats.alice)+ assert (await events.next())[0] == "ready"+ [subscription] = chats.activity.room_subscriptions++ wakes: list[str] = []+ turn_reads: list[str] = []+ frames_of = chats_module._activity_frames+ turn_items = chats.service.turn_items++ async def counted_frames(*args: Any) -> Any:+ wakes.append(args[3].key)+ return await frames_of(*args)++ async def counted_turn_items(*args: Any) -> Any:+ turn_reads.append(args[3])+ return await turn_items(*args)++ monkeypatch.setattr(chats_module, "_activity_frames", counted_frames)+ monkeypatch.setattr(chats.service, "turn_items", counted_turn_items)++ await _step(chats, other_agent, unrelated, "t9", "turn", 1)+ await chats.activity.change(_turn_change(other_agent, unrelated, "t9"))+ assert subscription.delivered == 0++ await _step(chats, agent, room_id, "t1", "turn", 1)+ await chats.activity.change(_turn_change(agent, room_id, "t1"))+ name, item = await events.next()+ assert (name, item["turnId"]) == ("activity", "t1")+ assert subscription.delivered == 1+ assert wakes == ["t1"]+ assert turn_reads == ["t1"]+ await events.stream.aclose() # type: ignore[attr-defined]+++async def test_the_activity_stream_follows_a_room_the_reader_joins(+ chats: _Harness,+) -> None:+ agent = await chats.agent("helper", owner=chats.alice)+ room_id = await chats.new_chat(chats.alice, agent, "c1")+ events = _activity_stream(chats, chats.bob)+ assert (await events.next())[0] == "ready"++ assert (await chats.invite(chats.alice, room_id, chats.bob)).status_code == 200+ assert await events.next() == ("resync", {})+ await _step(chats, agent, room_id, "t1", "turn", 1)+ await chats.activity.change(_turn_change(agent, room_id, "t1"))+ name, item = await events.next()+ assert (name, item["roomId"]) == ("activity", room_id)+ await events.stream.aclose() # type: ignore[attr-defined]+++async def test_a_room_joined_while_the_stream_reads_its_rooms_is_not_missed(+ chats: _Harness, monkeypatch: pytest.MonkeyPatch+) -> None:+ agent = await chats.agent("helper", owner=chats.alice)+ room_id = await chats.new_chat(chats.alice, agent, "c1")+ readable_rooms = chats.service.readable_rooms+ joined: list[bool] = []++ async def join_after_reading(*args: Any) -> Any:+ rooms = await readable_rooms(*args)+ if not joined:+ joined.append(True)+ invited = await chats.invite(chats.alice, room_id, chats.bob)+ assert invited.status_code == 200+ return rooms++ monkeypatch.setattr(chats.service, "readable_rooms", join_after_reading)+ events = _activity_stream(chats, chats.bob)+ assert (await events.next())[0] == "ready"+ assert room_id not in chats.activity.room_subscriptions[0].rooms++ assert await events.next() == ("resync", {})+ await _step(chats, agent, room_id, "t1", "turn", 1)+ await chats.activity.change(_turn_change(agent, room_id, "t1"))+ name, item = await events.next()+ assert (name, item["roomId"]) == ("activity", room_id)+ await events.stream.aclose() # type: ignore[attr-defined]+++async def test_a_stale_room_index_sends_nothing_from_a_room_left(+ chats: _Harness,+) -> None:+ agent = await chats.agent("helper", owner=chats.alice)+ left = await chats.new_chat(chats.alice, agent, "c1")+ kept = await chats.new_chat(chats.alice, agent, "c2")+ for room_id in (left, kept):+ assert (await chats.invite(chats.alice, room_id, chats.bob)).status_code == 200+ events = _activity_stream(chats, chats.bob)+ assert (await events.next())[0] == "ready"+ [subscription] = chats.activity.room_subscriptions++ await _silently_leave(chats, chats.bob, left)+ await _step(chats, agent, left, "t1", "turn", 1)+ await chats.activity.change(_turn_change(agent, left, "t1"))+ await _step(chats, agent, kept, "t2", "turn", 1)+ await chats.activity.change(_turn_change(agent, kept, "t2"))+ name, item = await events.next()+ assert (name, item["roomId"]) == ("activity", kept)+ assert subscription.delivered == 2+ await events.stream.aclose() # type: ignore[attr-defined]+++async def test_the_activity_stream_rechecks_its_rooms_under_steady_traffic(+ chats: _Harness,+) -> None:+ agent = await chats.agent("helper", owner=chats.alice)+ room_id = await chats.new_chat(chats.alice, agent, "c1")+ assert (await chats.invite(chats.alice, room_id, chats.bob)).status_code == 200+ events = _activity_stream(chats, chats.bob, recheck_seconds=0.2)+ assert (await events.next())[0] == "ready"+ [subscription] = chats.activity.room_subscriptions++ await _silently_leave(chats, chats.bob, room_id)+ reading = asyncio.create_task(events.next(timeout=10))+ deadline = asyncio.get_running_loop().time() + 5+ while room_id in subscription.rooms:+ assert asyncio.get_running_loop().time() < deadline+ await chats.activity.change(_turn_change(agent, room_id, "t1"))+ await asyncio.sleep(0.01)+ assert subscription.delivered > 5+ reading.cancel()+ await asyncio.gather(reading, return_exceptions=True)+ assert chats.activity.room_subscriptions == []++async def _drop_tenant_role(chats: _Harness, user: User) -> None:async with chats.factory() as session:await session.execute(@@ -1787,8 +1958,8 @@ async def test_the_activity_stream_ends_when_the_tenant_role_is_gone(on_change = _activity_stream(chats, chats.bob)assert (await on_change.next())[0] == "ready"- on_ping = _activity_stream(chats, chats.bob, ping_seconds=0.05)- assert (await on_ping.next())[0] == "ready"+ on_recheck = _activity_stream(chats, chats.bob, recheck_seconds=0.05)+ assert (await on_recheck.next())[0] == "ready"await _drop_tenant_role(chats, chats.bob)await _step(chats, agent, room_id, "t1", "turn", 1)@@ -1796,8 +1967,8 @@ async def test_the_activity_stream_ends_when_the_tenant_role_is_gone(with pytest.raises(StopAsyncIteration):await on_change.next()with pytest.raises(StopAsyncIteration):- await on_ping.next()- assert chats.activity.subscribers == []+ await on_recheck.next()+ assert chats.activity.room_subscriptions == []async def test_the_activity_stream_forgets_revisions_past_its_cap(