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
Run the statements every message write costs on asyncpg directly (CHOO-3667)
Abel Dantasfa3fe02f52ed
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c9c304786..430b32ccd 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -142,6 +142,11 @@ version of their own to them without also giving them a release of their own.compares it only when someone signs in.#### Changed+- **The statements every message write costs run on asyncpg directly.**+ Taking the room's lock, reading its head, storing the agents' own events+ and reading the room's agent members again under the lock go straight to+ asyncpg on the transaction's connection (`db/raw.py`), as the delivery+ reads do, rather than through SQLAlchemy's execution.- **The two reads behind every delivery run on asyncpg directly.** Astream reads an agent's events, and counts its unread messages, for everyevent it delivers, and under load SQLAlchemy's work around each statementdiff --git a/core/switch_core/bridges/agent/protocol/event_buffer.py b/core/switch_core/bridges/agent/protocol/event_buffer.pyindex e722e076f..ef95cee7b 100644--- a/core/switch_core/bridges/agent/protocol/event_buffer.py+++ b/core/switch_core/bridges/agent/protocol/event_buffer.py@@ -482,6 +482,11 @@ _STORE_OWN_EVENTS_SQL = text(""")+# The two statements above, run by asyncpg directly (`db/raw.py`): every+# message stores its agents' own events with them.+_LOCK_HEADS = RawStatement(_LOCK_HEADS_SQL)+_STORE_OWN_EVENTS = RawStatement(_STORE_OWN_EVENTS_SQL)+def session_slots_for(pool_size: int, max_overflow: int) -> int:"""How many `EventBuffer.one_session` blocks may hold a connection at once:@@ -671,44 +676,38 @@ class EventBuffer:return {}savepoint = await session.begin_nested()try:- heads: dict[str, int] = dict(- (- await session.execute(- _LOCK_HEADS_SQL,- {- "agent_ids": [own.agent_id for own in events],- "floor": self.sequence_floor,- },- )+ heads: dict[str, int] = {+ agent_id: head+ for agent_id, head in await _LOCK_HEADS.all(+ session,+ {+ "agent_ids": [own.agent_id for own in events],+ "floor": self.sequence_floor,+ },)- .tuples()- .all()- )+ }wanted = [own for own in events if own.agent_id in heads]- stored: dict[str, int] = dict(- (- await session.execute(- _STORE_OWN_EVENTS_SQL,- {- "tenant_id": tenant_id,- "room_id": room_id,- "agent_ids": [own.agent_id for own in wanted],- "client_ids": [own.client_id for own in wanted],- "seqs": [heads[own.agent_id] + 1 for own in wanted],- "room_seqs": [own.room_seq for own in wanted],- "message_keys": [own.message_key for own in wanted],- "types": [own.event.type for own in wanted],- "payloads": [- json.dumps(own.event.model_dump(mode="json"))- for own in wanted- ],- "notifiables": [is_notifiable(own.event) for own in wanted],- },- )+ stored: dict[str, int] = {+ agent_id: seq+ for agent_id, seq in await _STORE_OWN_EVENTS.all(+ session,+ {+ "tenant_id": tenant_id,+ "room_id": room_id,+ "agent_ids": [own.agent_id for own in wanted],+ "client_ids": [own.client_id for own in wanted],+ "seqs": [heads[own.agent_id] + 1 for own in wanted],+ "room_seqs": [own.room_seq for own in wanted],+ "message_keys": [own.message_key for own in wanted],+ "types": [own.event.type for own in wanted],+ "payloads": [+ json.dumps(own.event.model_dump(mode="json"))+ for own in wanted+ ],+ "notifiables": [is_notifiable(own.event) for own in wanted],+ },)- .tuples()- .all()- )+ }except Exception:await savepoint.rollback()logger.warning(diff --git a/core/switch_core/db/raw.py b/core/switch_core/db/raw.pyindex 38e01a866..90c31bd7f 100644--- a/core/switch_core/db/raw.py+++ b/core/switch_core/db/raw.py@@ -63,6 +63,11 @@ class RawStatement:self._statement = statementself._compiled: _Compiled | None = None+ @property+ def sql(self) -> str | None:+ """The SQL it runs, once it has run."""+ return self._compiled.sql if self._compiled is not None else None+async def all(self, session: AsyncSession, parameters: Mapping[str, Any]) -> list[Any]:diff --git a/core/switch_core/db/stores/message_store.py b/core/switch_core/db/stores/message_store.pyindex 31313d92f..743f68c0c 100644--- a/core/switch_core/db/stores/message_store.py+++ b/core/switch_core/db/stores/message_store.py@@ -1,10 +1,22 @@from collections.abc import Collectionfrom datetime import datetime-from sqlalchemy import BigInteger, Text, and_, column, func, or_, select, text, values+from sqlalchemy import (+ BigInteger,+ Text,+ and_,+ bindparam,+ column,+ func,+ or_,+ select,+ text,+ values,+)from sqlalchemy.ext.asyncio import AsyncSessionfrom switch_core.db.models import Message, MessageAttachment+from switch_core.db.raw import RawStatementclass MessageStore:@@ -146,16 +158,9 @@ class MessageStore:The cost is per-room serialisation of a single INSERT. Rooms do notcontend with each other."""- await session.execute(- text("SELECT pg_advisory_xact_lock(hashtext(:room_id))"),- {"room_id": room_id},- )- result = await session.execute(- select(func.coalesce(func.max(Message.seq), 0)).where(- Message.room_id == room_id- )- )- return int(result.scalar_one()) + 1+ await _LOCK_ROOM.all(session, {"room_id": room_id})+ head = await _ROOM_HEAD.all(session, {"room_id": room_id})+ return int(head[0].head) + 1async def head_seq(self, session: AsyncSession, room_id: str) -> int:"""The room's current position, or 0 when nothing has been sent.@@ -355,3 +360,14 @@ class MessageStore:.order_by(MessageAttachment.position))return list(result.scalars().all())+++# The two statements a message's number costs, run by asyncpg directly+# (`db/raw.py`): every message an agent or a person sends takes the room's+# lock and reads its head.+_LOCK_ROOM = RawStatement(text("SELECT pg_advisory_xact_lock(hashtext(:room_id))"))+_ROOM_HEAD = RawStatement(+ select(func.coalesce(func.max(Message.seq), 0).label("head")).where(+ Message.room_id == bindparam("room_id")+ )+)diff --git a/core/switch_core/delivery/router.py b/core/switch_core/delivery/router.pyindex cc33ad81d..e32e6dca2 100644--- a/core/switch_core/delivery/router.py+++ b/core/switch_core/delivery/router.py@@ -66,7 +66,7 @@ from dataclasses import dataclass, fieldfrom datetime import UTC, datetime, timedeltafrom typing import TYPE_CHECKING, Any, Literal, cast, overload-from sqlalchemy import and_, delete, func, literal, select, update+from sqlalchemy import and_, bindparam, delete, func, literal, select, updatefrom sqlalchemy.dialects.postgresql import insertfrom sqlalchemy.exc import DBAPIError@@ -89,6 +89,7 @@ from switch_core.db.models import (SwitchCoreProcess,room_agents,)+from switch_core.db.raw import RawStatementfrom switch_core.db.session_scope import tenant_sessionfrom switch_core.db.stores.room_store import follow_ups_enabled_in, main_agent_infrom switch_core.db.stores.template_install_store import TemplateInstallStore@@ -905,15 +906,10 @@ class MessageRouter:its members. Read with the room's lock held, when every arrivalnumbered before the row being written has committed."""known = {member.client_id for member in reads.members}- now = set(- (- await session.scalars(- select(ClientRoom.client_id)- .join(Agent, Agent.client_id == ClientRoom.client_id)- .where(ClientRoom.room_id == reads.room_id)- )- ).all()- )+ now = {+ row.client_id+ for row in await _AGENT_MEMBERS.all(session, {"room_id": reads.room_id})+ }return bool(now - known)async def membership_changed(self, change: tuple[str, str, str]) -> None:@@ -1745,3 +1741,12 @@ def _unwritten(message: Message) -> _Unwritten:# The time on a row not written yet, which nothing deciding it reads._NOT_STAMPED = datetime.fromtimestamp(0, UTC)+++# The room's agent members, read again under its lock for every message+# (`MessageRouter._joined_since`), run by asyncpg directly (`db/raw.py`).+_AGENT_MEMBERS = RawStatement(+ select(ClientRoom.client_id)+ .join(Agent, Agent.client_id == ClientRoom.client_id)+ .where(ClientRoom.room_id == bindparam("room_id"))+)diff --git a/core/tests/switch_core/delivery/test_router.py b/core/tests/switch_core/delivery/test_router.pyindex 3eb572899..d69662254 100644--- a/core/tests/switch_core/delivery/test_router.py+++ b/core/tests/switch_core/delivery/test_router.py@@ -49,6 +49,7 @@ from switch_core.db.models import (PendingAttachmentGroup,Room,)+from switch_core.db.raw import RawStatementfrom switch_core.db.session_scope import tenant_sessionfrom switch_core.db.stores.room_store import RoomStorefrom switch_core.delivery import router as router_module@@ -431,7 +432,9 @@ class TestFailuresStayWithTheAgent:# The agents' events are stored together first, and when that fails,# one at a time, each in a savepoint of its own.- monkeypatch.setattr(event_buffer, "_STORE_OWN_EVENTS_SQL", text("SELECT 1 / 0"))+ monkeypatch.setattr(+ event_buffer, "_STORE_OWN_EVENTS", RawStatement(text("SELECT 1 / 0"))+ )monkeypatch.setattr(EventBuffer, "_claim", failing_claim)registry = MetricsRegistry()install(registry)diff --git a/core/tests/switch_core/statement_counts.py b/core/tests/switch_core/statement_counts.pyindex b9d3cabbe..b532cc3ac 100644--- a/core/tests/switch_core/statement_counts.py+++ b/core/tests/switch_core/statement_counts.py@@ -7,7 +7,8 @@ guard against that is a count that does not grow: the same route over one rowand over many runs the same number of statements.Counted at the engine's `before_cursor_execute` event, which every statement-reaches whichever store or helper issued it. A request is a `StatementScope`,+reaches whichever store or helper issued it, and, for the statements run on+asyncpg directly (`db/raw.py`), which never reach it, at `RawStatement.all`. A request is a `StatementScope`,entered with `StatementCounts.scope()` or, for an app under test,`StatementCounts.wrap(app)`; the scope lives in a context variable, whichSQLAlchemy carries into the greenlet a statement runs in, the same way@@ -16,14 +17,16 @@ SQLAlchemy carries into the greenlet a statement runs in, the same wayfrom __future__ import annotations-from collections.abc import Iterator+from collections.abc import Iterator, Mappingfrom contextlib import contextmanagerfrom contextvars import ContextVarfrom dataclasses import dataclass, fieldfrom typing import Anyfrom sqlalchemy import event-from sqlalchemy.ext.asyncio import AsyncEngine+from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession++from switch_core.db import raw@dataclass@@ -51,10 +54,27 @@ class StatementCounts:self.scopes: list[StatementScope] = []event.listen(self._engine, "before_cursor_execute", self._on_execute)event.listen(self._engine, "after_cursor_execute", self._on_executed)+ self._raw_all = raw.RawStatement.all+ run = self._raw_all++ async def counted(+ statement: raw.RawStatement,+ session: AsyncSession,+ parameters: Mapping[str, Any],+ ) -> list[Any]:+ rows = await run(statement, session, parameters)+ scope = _current.get()+ if scope is not None:+ scope.statements.append(statement.sql or "")+ scope.rows += len(rows)+ return rows++ raw.RawStatement.all = counted # type: ignore[method-assign]def close(self) -> None:event.remove(self._engine, "before_cursor_execute", self._on_execute)event.remove(self._engine, "after_cursor_execute", self._on_executed)+ raw.RawStatement.all = self._raw_all # type: ignore[method-assign]def _on_executed(self,