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
fix(agent-bridge): seek to the `before` window instead of paging to it (CHOO-2034)
amaudruz553fdb5813aa
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 85d20326a..acdac4271 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -42,6 +42,11 @@ version of their own to them without also giving them a release of their own.### [Unreleased]#### Fixed+- `read_context` seeks to a `before` window via the homeserver's+ `timestamp_to_event` rather than paging over everything newer to reach it,+ and falls back to a scan with its own page budget when the server cannot+ answer (CHOO-2034). Previously the walk shared the read budget, so a window+ deep in a busy room returned empty however small a `limit` was asked for.- `read_context` now pages the homeserver instead of reading a single`/messages` page, so agents see the whole room rather than the last slice ofit (CHOO-2034). `before` pages backwards into older history rather thandiff --git a/core/switch_core/bridges/agent/protocol/service.py b/core/switch_core/bridges/agent/protocol/service.pyindex 326f938d0..cb77809bb 100644--- a/core/switch_core/bridges/agent/protocol/service.py+++ b/core/switch_core/bridges/agent/protocol/service.py@@ -8,9 +8,11 @@ import uuidfrom collections.abc import Sequencefrom datetime import UTC, datetimefrom typing import TYPE_CHECKING, Any+from urllib.parse import quotefrom nio import (DownloadError,+ RoomContextError,RoomGetEventError,RoomMemberEvent,RoomMessageMedia,@@ -114,6 +116,10 @@ _VALID_NAME_RE = re.compile(r"^[a-z0-9][a-z0-9._-]*$")# whole story.HISTORY_PAGE_SIZE = 100HISTORY_MAX_PAGES = 20+# Pages spent walking back to a `before` window, budgeted separately from the+# pages spent reading inside it. Only used when the homeserver cannot answer+# timestamp_to_event; a scan is the slow path, so it gets room to succeed.+HISTORY_MAX_SEEK_PAGES = 100class AgentExistsError(Exception):@@ -1351,23 +1357,55 @@ class ProtocolService:# Walk backwards page by page until the window is satisfied. A single# page is not enough: the homeserver caps its size, and state events# that never reach the caller still consume it.+ #+ # Reaching a `before` window and reading inside it are budgeted+ # separately. Matrix has no timestamp cursor, so a `before` deep in a+ # busy room can only be reached by paging over everything newer — and+ # if those pages came out of the read budget, a far-enough-back window+ # would return empty no matter how small a `limit` the caller asked+ # for. Seeking is the cost of getting there, not part of the answer.groups: dict[str, dict[str, Any]] = {}collected = 0oldest_ts: int | None = None- start: str | None = None- pages = 0+ read_pages = 0+ seek_pages = 0exhausted = False- while collected < limit and pages < HISTORY_MAX_PAGES:+ start: str | None = None+ if before_ms is not None:+ start = await self._seek_before_token(+ client, room.matrix_room_id, before_ms+ )++ while collected < limit and read_pages < HISTORY_MAX_PAGES:+ if seek_pages >= HISTORY_MAX_SEEK_PAGES:+ logger.warning(+ "read_context gave up seeking back to %s in %s after "+ "%d pages; the window was never reached",+ before_ms,+ room.matrix_room_id,+ seek_pages,+ )+ breakresp = await client.nio_client.room_messages(room.matrix_room_id, start=start, limit=HISTORY_PAGE_SIZE)if isinstance(resp, RoomMessagesError):raise ValueError(f"Failed to fetch room history: {resp.message}")- pages += 1chunk = list(resp.chunk)end = getattr(resp, "end", None)+ # A page that contributes nothing because everything on it is+ # newer than `before` is a seek, not a read.+ reached_window = before_ms is None or any(+ getattr(e, "server_timestamp", None) is not None+ and getattr(e, "server_timestamp") < before_ms+ for e in chunk+ )+ if reached_window:+ read_pages += 1+ else:+ seek_pages += 1for event in chunk:ts = getattr(event, "server_timestamp", None)@@ -1425,11 +1463,12 @@ class ProtocolService:if not exhausted:logger.warning(- "read_context truncated in %s: %d entries over %d pages, "- "older history not reached",+ "read_context truncated in %s: %d entries over %d read pages "+ "(%d spent seeking), older history not reached",room.matrix_room_id,collected,- pages,+ read_pages,+ seek_pages,)return {@@ -1438,6 +1477,62 @@ class ProtocolService:"oldest_timestamp": oldest_ts,}+ async def _seek_before_token(+ self, client: ClientBase[Any], matrix_room_id: str, before_ms: int+ ) -> str | None:+ """Get a pagination token positioned at `before_ms`, if the server can.++ `/messages` takes a token, not a timestamp, so reaching a `before` deep+ in a busy room otherwise means paging over everything newer just to+ arrive. `timestamp_to_event` (Matrix 1.6) jumps straight there.++ Returns None when the homeserver cannot answer — the caller then walks+ back the slow way. A failure here costs speed, not correctness, so it+ is logged and swallowed rather than raised.+ """+ nio_client = client.nio_client+ if nio_client is None:+ return None+ path = (+ f"/_matrix/client/v1/rooms/{quote(matrix_room_id, safe='')}"+ f"/timestamp_to_event?ts={before_ms}&dir=b"+ )+ try:+ resp = await nio_client.send(+ "GET",+ path,+ headers={"Authorization": f"Bearer {nio_client.access_token}"},+ )+ if resp.status != 200:+ logger.info(+ "timestamp_to_event unavailable in %s (HTTP %d); "+ "falling back to scanning back to the window",+ matrix_room_id,+ resp.status,+ )+ return None+ event_id = (await resp.json()).get("event_id")+ except Exception:+ logger.warning(+ "timestamp_to_event failed in %s; falling back to scanning",+ matrix_room_id,+ exc_info=True,+ )+ return None+ if not event_id:+ return None++ context = await nio_client.room_context(matrix_room_id, event_id, limit=1)+ if isinstance(context, RoomContextError):+ logger.info(+ "Could not anchor pagination at %s in %s; scanning instead",+ event_id,+ matrix_room_id,+ )+ return None+ start_token = context.start+ return str(start_token) if start_token else None+async def _fetch_root(self, client: ClientBase[Any], matrix_room_id: str, root_id: str) -> dict[str, Any]:diff --git a/core/tests/switch_core/bridges/agent/protocol/test_read_context_pagination.py b/core/tests/switch_core/bridges/agent/protocol/test_read_context_pagination.pyindex 37eb2cb89..b2e29979b 100644--- a/core/tests/switch_core/bridges/agent/protocol/test_read_context_pagination.py+++ b/core/tests/switch_core/bridges/agent/protocol/test_read_context_pagination.py@@ -13,6 +13,7 @@ from typing import Anyimport nio+from switch_core.bridges.agent.protocol import servicefrom switch_core.bridges.agent.protocol.connections import ConnectionRegistryfrom switch_core.bridges.agent.protocol.service import ProtocolService@@ -56,10 +57,18 @@ def _rename(event_id: str, ts: int, user: str, displayname: str) -> nio.RoomMembclass _PagedNio:- """Homeserver stand-in that hands out history one page at a time."""+ """Homeserver stand-in that hands out history one page at a time.- def __init__(self, pages: list[list[Any]]) -> None:+ `seek_to` mimics a server that answers timestamp_to_event: the page index+ the token lands on. None means the server cannot answer, so the caller has+ to scan back instead.+ """++ access_token = "tok"++ def __init__(self, pages: list[list[Any]], seek_to: int | None = None) -> None:self._pages = pages+ self._seek_to = seek_toself.requested_starts: list[str | None] = []async def room_messages(@@ -75,6 +84,18 @@ class _PagedNio:end=None if is_last else str(index + 1),)+ async def send(self, method: str, path: str, headers: Any = None) -> Any:+ if self._seek_to is None:+ return SimpleNamespace(status=404)++ async def _json() -> dict[str, str]:+ return {"event_id": "$anchor"}++ return SimpleNamespace(status=200, json=_json)++ async def room_context(self, room_id: str, event_id: str, limit: int = 1) -> Any:+ return SimpleNamespace(start=str(self._seek_to))+async def room_get_event(self, room_id: str, event_id: str) -> Any:return object.__new__(nio.RoomGetEventError)@@ -165,6 +186,48 @@ class TestWindowing:assert sorted(_roots(result)) == ["old1", "old2"]+ async def test_seeking_to_the_window_does_not_spend_the_read_budget(self) -> None:+ # The bug Louis hit: reaching a `before` deep in a busy room meant+ # paging over everything newer, and those pages came out of the same+ # budget as the read — so a far-enough-back window returned nothing at+ # all, and raising `limit` could not help because `limit` never+ # governed the walk.+ newer = [+ [_msg(f"n{p}{i}", 10_000 - p * 100 - i) for i in range(50)]+ for p in range(service.HISTORY_MAX_PAGES + 5)+ ]+ pages = [*newer, [_msg("wanted", 100)]]+ svc = _service(_PagedNio(pages))++ result = await svc.read_context("agent", "room", limit=10, before_ms=500)++ assert _roots(result) == ["wanted"]++ async def test_gives_up_loudly_rather_than_scanning_forever(self) -> None:+ endless = [[_msg(f"n{p}", 10_000 - p)] for p in range(200)]+ svc = _service(_PagedNio(endless))++ result = await svc.read_context("agent", "room", limit=10, before_ms=500)++ # Nothing was reachable, and the caller is told so rather than being+ # handed an empty list that looks like "the room is empty".+ assert result["threads"] == []+ assert result["truncated"] is True++ async def test_uses_the_server_timestamp_anchor_when_offered(self) -> None:+ pages = [+ [_msg("new", 9_000)],+ [_msg("old", 100)],+ ]+ nio_client = _PagedNio(pages, seek_to=1)+ svc = _service(nio_client)++ result = await svc.read_context("agent", "room", limit=10, before_ms=500)++ assert _roots(result) == ["old"]+ # Jumped straight to the window instead of paging over page 0.+ assert nio_client.requested_starts == ["1"]+async def test_since_stops_the_walk_once_the_window_is_covered(self) -> None:nio_client = _PagedNio([[_msg("e2", 900)], [_msg("e1", 100)]])svc = _service(nio_client)