Skip to content

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

perf(chats): deliver an activity update by reading only its room and step

raullopez-sandbox6689c57a87fe
diff --git a/core/switch_core/chats/service.py b/core/switch_core/chats/service.py
index c436d8965..42fd788a7 100644
--- a/core/switch_core/chats/service.py
+++ b/core/switch_core/chats/service.py
@@ -71,6 +71,7 @@ from switch_core.db.models import (
Message,
MessageAttachment,
Room,
+ TenantMember,
UsageMetric,
User,
require_tenant_id,
@@ -748,6 +749,43 @@ class ChatService:
) -> list[AgentSessionActivityItem]:
return await self._activity_store.turn(session, agent_id, session_id, turn_id)
+ async def turn_item(
+ self,
+ session: AsyncSession,
+ agent_id: str,
+ session_id: str,
+ turn_id: str,
+ item_id: str,
+ ) -> AgentSessionActivityItem | None:
+ return await session.get(
+ AgentSessionActivityItem,
+ (require_tenant_id(), agent_id, session_id, turn_id, item_id),
+ )
+
+ async def reads_room(
+ self, session: AsyncSession, tenant_id: str, user_id: str, room_id: str | None
+ ) -> tuple[bool, bool]:
+ """Whether the user still holds a role in the tenant, and whether they
+ may read `room_id` now (`readable_rooms`, for that one room), in one
+ query. A step outside any room is read by nobody here."""
+ member = exists().where(
+ TenantMember.tenant_id == tenant_id, TenantMember.user_id == user_id
+ )
+ reads = (
+ exists().where(
+ ClientRoom.room_id == room_id,
+ ClientRoom.client_id == Client.id,
+ Client.user_id == user_id,
+ Client.type == MEMBER_CLIENT_TYPE,
+ Room.id == room_id,
+ Room.archived_at.is_(None),
+ )
+ if room_id is not None
+ else false()
+ )
+ row = (await session.execute(select(member, reads))).one()
+ return bool(row[0]), bool(row[0] and row[1])
+
async def approval(
self, session: AsyncSession, agent_id: str, session_id: str, request_id: str
) -> ApprovalRequest | None:
diff --git a/core/switch_core/db/session_activity_notify_ddl.py b/core/switch_core/db/session_activity_notify_ddl.py
index 6c4596239..f67f9956d 100644
--- a/core/switch_core/db/session_activity_notify_ddl.py
+++ b/core/switch_core/db/session_activity_notify_ddl.py
@@ -9,11 +9,12 @@ pushes an outcome on without reading anything back. `pg_notify` payloads are
capped just under 8000 bytes, so a request too large to fit is announced by key
alone and the subscriber reads that one row.
-A turn step's announcement carries only the turn it belongs to
-(`agent_id`, `session_id`, `turn_id`) and its room (`room_id`, null for a step
-outside any room). Its subscriber, a bridge, redraws the
-whole turn from its rows on every change anyway, and a step's text is too
-large to ride along often enough to be worth the branch.
+A turn step's announcement carries the turn it belongs to (`agent_id`,
+`session_id`, `turn_id`), its room (`room_id`, null for a step outside any
+room), and the step itself (`item_id`, `revision`), so a subscriber that shows
+steps one by one reads only the one that moved. A bridge redraws the whole
+turn from its rows on every change anyway, and a step's text is too large to
+ride along often enough to be worth the branch.
Fires on every insert or update of `session_activity_items`, every insert into
`approval_requests`, and an approval request's change of state (answered,
@@ -44,7 +45,9 @@ BEGIN
'agent_id', NEW.agent_id,
'session_id', NEW.session_id,
'room_id', NEW.room_id,
- 'key', body->>'turn_id'
+ 'key', body->>'turn_id',
+ 'item_id', NEW.item_id,
+ 'revision', NEW.revision
);
payload := jsonb_build_object('table', TG_TABLE_NAME, 'key', key)::text;
ELSE
diff --git a/core/switch_core/gateway/chats.py b/core/switch_core/gateway/chats.py
index f0a4495cc..3d8b37057 100644
--- a/core/switch_core/gateway/chats.py
+++ b/core/switch_core/gateway/chats.py
@@ -1011,22 +1011,43 @@ async def _activity_frames(
change: Change,
sent: dict[tuple[str, str, str, str], int],
) -> list[bytes] | None:
- """Frames for one change, limited to the rooms the caller may read now;
- None once the caller has no tenant role, which ends the stream."""
+ """Frames for one change, if the caller may read its room now; None once
+ the caller has no tenant role, which ends the stream.
+
+ Reads only what moved: an announcement naming its step reads that one
+ row, and none at all when a revision at least as new was already sent, so
+ a burst of updates to a step is coalesced into the first read of it. An
+ announcement from before steps were named rereads the turn."""
+ if change.kind == "activity" and change.item_id is not None:
+ key = (change.agent_id, change.session_id, change.key, change.item_id)
+ if change.revision is not None and sent.get(key, -1) >= change.revision:
+ return []
async with tenant_session(service.session_factory, tenant_id) as session:
- if not await service.has_tenant_role(session, tenant_id, user.id):
+ member, reads = await service.reads_room(
+ session, tenant_id, user.id, change.room_id
+ )
+ if not member:
return None
- readable = {room.id for room in await service.readable_rooms(session, user.id)}
- if not readable:
+ if not reads:
return []
if change.kind == "activity":
- rows = await service.turn_items(
- session, change.agent_id, change.session_id, change.key
- )
+ if change.item_id is not None:
+ item = await service.turn_item(
+ session,
+ change.agent_id,
+ change.session_id,
+ change.key,
+ change.item_id,
+ )
+ rows = [item] if item is not None else []
+ else:
+ rows = await service.turn_items(
+ session, change.agent_id, change.session_id, change.key
+ )
frames = []
for row in rows:
key = (row.agent_id, row.session_id, row.turn_id, row.item_id)
- if row.room_id in readable and sent.get(key, -1) < row.revision:
+ if row.room_id == change.room_id and sent.get(key, -1) < row.revision:
sent[key] = row.revision
view = ActivityItemView.of(row)
frames.append(
@@ -1036,7 +1057,7 @@ async def _activity_frames(
request = await service.approval(
session, change.agent_id, change.session_id, change.key
)
- if request is None or request.room_id not in readable:
+ if request is None or request.room_id != change.room_id:
return []
approval = ApprovalRequestView.of(request)
return [_frame("approval", approval.model_dump(mode="json", by_alias=True))]
diff --git a/core/switch_core/migrations/versions/b7e1d4c9a2f6_session_activity_step_in_announcements.py b/core/switch_core/migrations/versions/b7e1d4c9a2f6_session_activity_step_in_announcements.py
new file mode 100644
index 000000000..2a2b09582
--- /dev/null
+++ b/core/switch_core/migrations/versions/b7e1d4c9a2f6_session_activity_step_in_announcements.py
@@ -0,0 +1,101 @@
+"""Session activity announcements name the step
+
+A turn step's announcement now also names the step (`item_id`, `revision`),
+so a subscriber showing steps one by one reads only the one that moved, and
+none it already sent. Replaces the notify function only.
+
+Revision ID: b7e1d4c9a2f6
+Revises: ddd07a1fcdcb
+Create Date: 2026-10-28 00:00:00.000000
+"""
+
+from alembic import op
+
+revision: str = "b7e1d4c9a2f6"
+down_revision: str | None = "ddd07a1fcdcb"
+branch_labels = None
+depends_on = None
+
+
+def upgrade() -> None:
+ op.execute(_NOTIFY_FUNCTION)
+
+
+def downgrade() -> None:
+ op.execute(_PREVIOUS_NOTIFY_FUNCTION)
+
+
+# Frozen copy of `switch_core.db.session_activity_notify_ddl` as of this
+# revision; the live module may change later, this migration must not.
+_NOTIFY_FUNCTION = """
+CREATE OR REPLACE FUNCTION switch_notify_session_activity() RETURNS trigger AS $$
+DECLARE
+ body jsonb := to_jsonb(NEW);
+ key jsonb;
+ payload text;
+BEGIN
+ IF TG_TABLE_NAME = 'session_activity_items' THEN
+ key := jsonb_build_object(
+ 'tenant_id', NEW.tenant_id,
+ 'agent_id', NEW.agent_id,
+ 'session_id', NEW.session_id,
+ 'room_id', NEW.room_id,
+ 'key', body->>'turn_id',
+ 'item_id', NEW.item_id,
+ 'revision', NEW.revision
+ );
+ payload := jsonb_build_object('table', TG_TABLE_NAME, 'key', key)::text;
+ ELSE
+ key := jsonb_build_object(
+ 'tenant_id', NEW.tenant_id,
+ 'agent_id', NEW.agent_id,
+ 'session_id', NEW.session_id,
+ 'room_id', NEW.room_id,
+ 'key', body->>'request_id'
+ );
+ payload := jsonb_build_object('table', TG_TABLE_NAME, 'row', body)::text;
+ IF octet_length(payload) > 7500 THEN
+ payload := jsonb_build_object('table', TG_TABLE_NAME, 'key', key)::text;
+ END IF;
+ END IF;
+ PERFORM pg_notify('switch_session_activity', payload);
+ RETURN NULL;
+END;
+$$ LANGUAGE plpgsql
+"""
+
+# As `ddd07a1fcdcb` installed it.
+_PREVIOUS_NOTIFY_FUNCTION = """
+CREATE OR REPLACE FUNCTION switch_notify_session_activity() RETURNS trigger AS $$
+DECLARE
+ body jsonb := to_jsonb(NEW);
+ key jsonb;
+ payload text;
+BEGIN
+ IF TG_TABLE_NAME = 'session_activity_items' THEN
+ key := jsonb_build_object(
+ 'tenant_id', NEW.tenant_id,
+ 'agent_id', NEW.agent_id,
+ 'session_id', NEW.session_id,
+ 'room_id', NEW.room_id,
+ 'key', body->>'turn_id'
+ );
+ payload := jsonb_build_object('table', TG_TABLE_NAME, 'key', key)::text;
+ ELSE
+ key := jsonb_build_object(
+ 'tenant_id', NEW.tenant_id,
+ 'agent_id', NEW.agent_id,
+ 'session_id', NEW.session_id,
+ 'room_id', NEW.room_id,
+ 'key', body->>'request_id'
+ );
+ payload := jsonb_build_object('table', TG_TABLE_NAME, 'row', body)::text;
+ IF octet_length(payload) > 7500 THEN
+ payload := jsonb_build_object('table', TG_TABLE_NAME, 'key', key)::text;
+ END IF;
+ END IF;
+ PERFORM pg_notify('switch_session_activity', payload);
+ RETURN NULL;
+END;
+$$ LANGUAGE plpgsql
+"""
diff --git a/core/switch_core/session_activity/listener.py b/core/switch_core/session_activity/listener.py
index b93c88b17..8988214f4 100644
--- a/core/switch_core/session_activity/listener.py
+++ b/core/switch_core/session_activity/listener.py
@@ -58,6 +58,10 @@ class Change:
# None for a step or request outside any room.
room_id: str | None
row: dict[str, Any] | None
+ # The step and its revision, for `activity`; None for an approval, and on
+ # an announcement from before steps were named.
+ item_id: str | None = None
+ revision: int | None = None
@staticmethod
def parse(payload: str) -> Change | None:
@@ -80,6 +84,8 @@ class Change:
else:
raise ValueError(table)
room_id = fields["room_id"]
+ item_id = fields.get("item_id") if kind == "activity" else None
+ revision = fields.get("revision") if kind == "activity" else None
return Change(
kind=kind,
tenant_id=str(fields["tenant_id"]),
@@ -88,6 +94,8 @@ class Change:
key=key,
room_id=None if room_id is None else str(room_id),
row=row,
+ item_id=None if item_id is None else str(item_id),
+ revision=None if revision is None else int(revision),
)
except (ValueError, TypeError, KeyError):
logger.warning(
diff --git a/core/tests/switch_core/gateway/test_chats_activity_delivery.py b/core/tests/switch_core/gateway/test_chats_activity_delivery.py
new file mode 100644
index 000000000..f0a7fe043
--- /dev/null
+++ b/core/tests/switch_core/gateway/test_chats_activity_delivery.py
@@ -0,0 +1,125 @@
+"""Delivering one activity update to a viewer checks access to that one room
+and reads only the step that moved, whatever the viewer's number of rooms or
+the turn's length; a viewer taken out of the room gets nothing more, and an
+announcement from before steps were named still delivers."""
+
+from __future__ import annotations
+
+from dataclasses import replace
+
+from sqlalchemy import delete
+
+from switch_core.db.models import Agent, ClientRoom, TenantMember
+from switch_core.gateway.chats import _activity_frames
+from switch_core.session_activity.listener import Change
+from tests.switch_core.gateway.conftest import _Harness
+from tests.switch_core.gateway.test_chats_routes import _step, _turn_change
+from tests.switch_core.statement_counts import StatementCounts
+
+TENANT = "00000000-0000-0000-0000-000000000000"
+
+
+def _step_change(agent: Agent, room_id: str, item_id: str, revision: int) -> Change:
+ return replace(
+ _turn_change(agent, room_id, "t1"), item_id=item_id, revision=revision
+ )
+
+
+async def _long_turn_in_one_of_many_rooms(chats: _Harness) -> tuple[Agent, str]:
+ agent = await chats.agent("helper", owner=chats.alice)
+ room_id = await chats.new_chat(chats.alice, agent, "c0")
+ for index in range(1, 20):
+ await chats.new_chat(chats.alice, agent, f"c{index}")
+ await _step(chats, agent, room_id, "t1", "turn", 1)
+ for index in range(50):
+ await _step(chats, agent, room_id, "t1", f"tool-{index}", 1)
+ return agent, room_id
+
+
+async def test_one_update_reads_one_room_and_one_step(
+ chats: _Harness, statement_counts: StatementCounts
+) -> None:
+ agent, room_id = await _long_turn_in_one_of_many_rooms(chats)
+ sent: dict[tuple[str, str, str, str], int] = {}
+
+ with statement_counts.scope("step") as step:
+ frames = await _activity_frames(
+ chats.service,
+ TENANT,
+ chats.alice,
+ _step_change(agent, room_id, "tool-7", 1),
+ sent,
+ )
+ assert frames is not None and len(frames) == 1
+ # The tenant binding, the access check for this room, and the one step.
+ assert (step.count, step.rows) == (3, 3)
+
+ # The same revision again, as a burst of announcements delivers it: no read.
+ with statement_counts.scope("again") as again:
+ frames = await _activity_frames(
+ chats.service,
+ TENANT,
+ chats.alice,
+ _step_change(agent, room_id, "tool-7", 1),
+ sent,
+ )
+ assert frames == [] and again.count == 0
+
+ # Without a step named (an older announcement): the turn is reread.
+ with statement_counts.scope("turn") as turn:
+ frames = await _activity_frames(
+ chats.service, TENANT, chats.alice, _turn_change(agent, room_id, "t1"), sent
+ )
+ assert frames is not None and len(frames) == 50
+ assert turn.count == 3
+
+
+async def test_a_viewer_taken_out_of_the_room_gets_nothing_more(
+ 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
+ await _step(chats, agent, room_id, "t1", "turn", 1)
+ sent: dict[tuple[str, str, str, str], int] = {}
+ first = await _activity_frames(
+ chats.service, TENANT, chats.bob, _step_change(agent, room_id, "turn", 1), sent
+ )
+ assert first is not None and len(first) == 1
+
+ async with chats.factory() as session:
+ client = await chats.service.member_client(session, chats.bob.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()
+ await _step(chats, agent, room_id, "t1", "turn", 2)
+ assert (
+ await _activity_frames(
+ chats.service,
+ TENANT,
+ chats.bob,
+ _step_change(agent, room_id, "turn", 2),
+ sent,
+ )
+ == []
+ )
+
+ async with chats.factory() as session:
+ await session.execute(
+ delete(TenantMember).where(TenantMember.user_id == chats.bob.id)
+ )
+ await session.commit()
+ assert (
+ await _activity_frames(
+ chats.service,
+ TENANT,
+ chats.bob,
+ _step_change(agent, room_id, "turn", 3),
+ sent,
+ )
+ is None
+ )