Skip to content

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

Read a room's context in one transaction, not three (CHOO-3667)

Abel Dantas9988162038ad
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8b9791612..b92a05b21 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -224,6 +224,9 @@ version of their own to them without also giving them a release of their own.
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.
+- **Reading a room's context costs one transaction.** The room's newest
+ message number, the membership check and the history read share one
+ session, where each opened its own.
#### Fixed
- **On Slack, the 👀 and the activity card no longer arrive after the agent's
diff --git a/core/switch_core/bridges/agent/operations/definitions.py b/core/switch_core/bridges/agent/operations/definitions.py
index a967be102..2002eff5b 100644
--- a/core/switch_core/bridges/agent/operations/definitions.py
+++ b/core/switch_core/bridges/agent/operations/definitions.py
@@ -57,6 +57,7 @@ from switch_core.bridges.agent.protocol.instructions import build_room_instructi
from switch_core.bridges.agent.protocol.types import IntegrationProfile
from switch_core.clients.agent_admin import DELETE_CONFIRM_SECONDS
from switch_core.db.models import CollaborationBridge, User, require_tenant_id
+from switch_core.db.session_scope import shared_tenant_session
from switch_core.db.stores.template_store import TemplateStore
from switch_core.feature_flags import AGENT_MANAGEMENT, SWITCH_BUDDY
from switch_core.rooms_yaml import GroupSpec, RoomReader, template_json_schema
@@ -716,16 +717,19 @@ async def read_context(
buffer = protocol.event_buffer
tenant_id = require_tenant_id()
- # The room's newest `messages.seq`, taken before the history read because
- # a message written while the read is in flight is not in its response.
- # Recording the catch-up through a later number would mark such a message
- # as read when the agent never saw it. Recording it through this number
- # can at worst count a message twice.
- through = (await buffer.room_heads(tenant_id, {room_id}))[room_id]
-
- context = await protocol.read_context(
- agent_id, room_id, limit=limit, since_ms=since_ms, before_ms=before_ms
- )
+ # The room's head, the membership check and the history read share one
+ # session, so a context read costs one pool checkout and one transaction.
+ async with shared_tenant_session(protocol.session_factory, tenant_id):
+ # The room's newest `messages.seq`, taken before the history read
+ # because a message written while the read is in flight is not in its
+ # response. Recording the catch-up through a later number would mark
+ # such a message as read when the agent never saw it. Recording it
+ # through this number can at worst count a message twice.
+ through = (await buffer.room_heads(tenant_id, {room_id}))[room_id]
+
+ context = await protocol.read_context(
+ agent_id, room_id, limit=limit, since_ms=since_ms, before_ms=before_ms
+ )
reader = counting_reader()
if reader is not None and room_id in connected:
diff --git a/core/switch_core/bridges/agent/protocol/agent_core.py b/core/switch_core/bridges/agent/protocol/agent_core.py
index 4ca938067..5ce277306 100644
--- a/core/switch_core/bridges/agent/protocol/agent_core.py
+++ b/core/switch_core/bridges/agent/protocol/agent_core.py
@@ -146,6 +146,8 @@ from switch_core.trust.client import (
)
if TYPE_CHECKING:
+ from contextlib import AbstractAsyncContextManager
+
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from switch_core.agent_runs import RunService
@@ -1450,9 +1452,18 @@ class AgentCore:
async def require_room_member(self, agent_id: str, room_id: str) -> RoomDescriptor:
"""Get room and verify agent is a member. Raises PermissionError if not."""
- async with self.session_factory() as session:
+ async with self._bound_session() as session:
return await self._room_member_in(session, agent_id, room_id)
+ def _bound_session(self) -> AbstractAsyncContextManager[AsyncSession]:
+ """A session for the bound tenant: the one a `shared_tenant_session`
+ block lends this task, if it is inside one, so a caller can put
+ several reads in one transaction."""
+ tenant_id = current_tenant_id()
+ if tenant_id is None:
+ return self.session_factory()
+ return tenant_session(self.session_factory, tenant_id)
+
async def require_room_poster(self, agent_id: str, room_id: str) -> RoomDescriptor:
"""`require_room_member`, and raise `BudgetExceeded` if the agent has
reached a budget covering it. One session for both, so a post still
@@ -2593,7 +2604,7 @@ class AgentCore:
await self.require_room_member(agent_id, room_id)
limit = max(1, min(limit, HISTORY_MAX_LIMIT))
- async with self.session_factory() as session:
+ async with self._bound_session() as session:
# One more than asked for. Whether that row exists is precisely the
# question `truncated` answers, so there is nothing to estimate and
# no reason to be conservative about it.
diff --git a/core/tests/switch_core/bridges/agent/operations/test_read_context_room_argument.py b/core/tests/switch_core/bridges/agent/operations/test_read_context_room_argument.py
index 3b7659b64..3e789c7e6 100644
--- a/core/tests/switch_core/bridges/agent/operations/test_read_context_room_argument.py
+++ b/core/tests/switch_core/bridges/agent/operations/test_read_context_room_argument.py
@@ -17,6 +17,7 @@ from collections.abc import Awaitable, Callable
from typing import Any
import pytest
+from sqlalchemy import insert
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from switch_core.bridges.agent.operations import context as op_context
@@ -32,10 +33,15 @@ from switch_core.bridges.agent.protocol.agent_connections import (
AgentConnectionRegistry,
ClientDeclaration,
)
+from switch_core.bridges.agent.protocol.agent_core import AgentCore
from switch_core.bridges.agent.protocol.event_buffer import EventBuffer, Reader
from switch_core.bridges.agent.protocol.positions import OWN_EVENTS
-from switch_core.db.models import TENANT_ZERO_ID
+from switch_core.db.models import TENANT_ZERO_ID, room_agents
+from switch_core.db.session_scope import tenant_session
+from switch_core.db.stores.message_store import MessageStore
+from switch_core.db.stores.room_store import RoomStore
from tests.db_buffer import db_buffer_on, say, seed_member
+from tests.session_counting import CountingSessionFactory
AGENT = "agent-1"
CONN = "conn-1"
@@ -46,9 +52,15 @@ OTHER_ROOM = "room-elsewhere"
class _Protocol:
"""Just enough protocol to answer a read: a registry and a recorder."""
- def __init__(self, registry: AgentConnectionRegistry, buffer: EventBuffer) -> None:
+ def __init__(
+ self,
+ registry: AgentConnectionRegistry,
+ buffer: EventBuffer,
+ session_factory: async_sessionmaker[AsyncSession],
+ ) -> None:
self.connections = registry
self.event_buffer = buffer
+ self.session_factory = session_factory
self.calls: list[tuple[str, str]] = []
self.members: set[str] = {CONNECTED_ROOM, OTHER_ROOM}
self.while_in_flight: list[Callable[[], Awaitable[None]]] = []
@@ -73,7 +85,7 @@ async def protocol(
for room_id in (CONNECTED_ROOM, OTHER_ROOM):
await seed_member(session_factory, AGENT, room_id)
registry = AgentConnectionRegistry()
- service = _Protocol(registry, db_buffer)
+ service = _Protocol(registry, db_buffer, session_factory)
monkeypatch.setattr(op_context, "_protocol", service)
return service
@@ -186,6 +198,36 @@ async def test_an_unconnected_caller_naming_no_room_is_told_to_connect(
await read_context()
+async def test_a_read_costs_one_session(
+ monkeypatch: pytest.MonkeyPatch,
+ session_factory: async_sessionmaker[AsyncSession],
+ unbound_caller: None,
+) -> None:
+ """The room's head, the membership check and the history are read in one
+ session: one pool checkout and one transaction for the whole read."""
+ counting = CountingSessionFactory(session_factory)
+ await seed_member(session_factory, AGENT, OTHER_ROOM)
+ async with tenant_session(session_factory, TENANT_ZERO_ID) as session:
+ await session.execute(
+ insert(room_agents).values(room_id=OTHER_ROOM, agent_id=AGENT)
+ )
+ await session.commit()
+ await say(session_factory, OTHER_ROOM, "catch me up")
+ core = object.__new__(AgentCore)
+ core.connections = AgentConnectionRegistry()
+ core.session_factory = counting # type: ignore[assignment]
+ core.message_store = MessageStore()
+ core.room_store = RoomStore()
+ core.event_buffer = db_buffer_on(counting) # type: ignore[arg-type]
+ monkeypatch.setattr(op_context, "_protocol", core)
+
+ context = await read_context(room_id=OTHER_ROOM)
+
+ assert [thread["root"]["body"] for thread in context["threads"]] == ["catch me up"]
+ assert counting.opened == 1
+
+
@pytest.mark.asyncio
async def test_the_room_is_advertised_as_an_argument() -> None:
"""Agents learn the tool from its generated schema, so it must appear there."""