Skip to content

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

Start a client in more than 32,767 rooms, by matching its rooms as one array (CHOO-3667)

Abel Dantas88d85875a927
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8b9791612..c772b6399 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -226,6 +226,12 @@ version of their own to them without also giving them a release of their own.
Existing duplicates are left as they are and can be reached by id.
#### Fixed
+- **A client in more than 32,767 rooms starts reading them.** The reads a
+ client makes as it starts matched its rooms with one parameter each, and
+ Postgres takes at most 32,767 per statement. Switch's own client is in
+ every room of its workspace, archived ones included, so past that many it
+ failed to start, retried, and failed again while the process ran, taking
+ up rooms one at a time in between. The rooms now go as one array.
- **On Slack, the 👀 and the activity card no longer arrive after the agent's
reply.** Every running turn's card was redrawn every 5 seconds to tick its
clock. Slack closes a streamed card after 10 minutes, so on a longer turn
diff --git a/core/switch_core/db/id_lists.py b/core/switch_core/db/id_lists.py
new file mode 100644
index 000000000..891a68caf
--- /dev/null
+++ b/core/switch_core/db/id_lists.py
@@ -0,0 +1,23 @@
+"""Matching a column against a list of ids of any length.
+
+`column.in_(ids)` sends one bind parameter per id, and Postgres refuses a
+statement with more than 32,767 of them. A list that grows with a deployment,
+such as the rooms a client is in (Switch's own client is in every room of its
+workspace, archived ones included), reaches that sooner or later, and the read
+then fails every time. `any_of` sends the whole list as one array parameter.
+"""
+
+from __future__ import annotations
+
+from collections.abc import Collection
+from typing import Any
+
+from sqlalchemy import ColumnElement, SQLColumnExpression, Text, any_, bindparam
+from sqlalchemy.dialects.postgresql import ARRAY
+
+
+def any_of(
+ column: SQLColumnExpression[Any], ids: Collection[str]
+) -> ColumnElement[bool]:
+ """`column = ANY(:ids)`, with `ids` bound as one text array."""
+ return column == any_(bindparam(None, list(ids), type_=ARRAY(Text)))
diff --git a/core/switch_core/db/stores/delivery_cursor_store.py b/core/switch_core/db/stores/delivery_cursor_store.py
index 4e374ecd2..037ff376c 100644
--- a/core/switch_core/db/stores/delivery_cursor_store.py
+++ b/core/switch_core/db/stores/delivery_cursor_store.py
@@ -14,6 +14,7 @@ from sqlalchemy import BigInteger, bindparam, func, literal, select, text
from sqlalchemy.dialects.postgresql import ARRAY, insert
from sqlalchemy.types import Text
+from switch_core.db.id_lists import any_of
from switch_core.db.models import DeliveryCursor, Message
if TYPE_CHECKING:
@@ -131,7 +132,7 @@ class DeliveryCursorStore:
result = await session.execute(
select(DeliveryCursor.room_id, DeliveryCursor.last_seq).where(
DeliveryCursor.client_id == client_id,
- DeliveryCursor.room_id.in_(list(room_ids)),
+ any_of(DeliveryCursor.room_id, room_ids),
)
)
return dict(result.tuples().all())
diff --git a/core/switch_core/db/stores/message_store.py b/core/switch_core/db/stores/message_store.py
index 31313d92f..5727c863b 100644
--- a/core/switch_core/db/stores/message_store.py
+++ b/core/switch_core/db/stores/message_store.py
@@ -4,6 +4,7 @@ from datetime import datetime
from sqlalchemy import BigInteger, Text, and_, column, func, or_, select, text, values
from sqlalchemy.ext.asyncio import AsyncSession
+from switch_core.db.id_lists import any_of
from switch_core.db.models import Message, MessageAttachment
@@ -184,7 +185,7 @@ class MessageStore:
return {}
result = await session.execute(
select(Message.room_id, func.max(Message.seq))
- .where(Message.room_id.in_(room_ids))
+ .where(any_of(Message.room_id, room_ids))
.group_by(Message.room_id)
)
heads = {room_id: max(int(seq or 0), 0) for room_id, seq in result.all()}
diff --git a/core/switch_core/db/stores/room_store.py b/core/switch_core/db/stores/room_store.py
index e91ce9b15..4c7c7b213 100644
--- a/core/switch_core/db/stores/room_store.py
+++ b/core/switch_core/db/stores/room_store.py
@@ -17,6 +17,7 @@ from sqlalchemy.dialects.postgresql import insert as pg_insert
from sqlalchemy.ext.asyncio import AsyncSession
from switch_core.chats import MEMBER_CLIENT_TYPE
+from switch_core.db.id_lists import any_of
from switch_core.db.models import (
Agent,
Client,
@@ -645,7 +646,7 @@ class RoomStore:
result = await session.execute(
select(ClientRoom.room_id, ClientRoom.joined_at).where(
ClientRoom.client_id == client_id,
- ClientRoom.room_id.in_(room_ids),
+ any_of(ClientRoom.room_id, room_ids),
)
)
return {room_id: joined_at for room_id, joined_at in result.all()}
diff --git a/core/tests/switch_core/db/stores/test_long_room_lists.py b/core/tests/switch_core/db/stores/test_long_room_lists.py
new file mode 100644
index 000000000..d7893d1cb
--- /dev/null
+++ b/core/tests/switch_core/db/stores/test_long_room_lists.py
@@ -0,0 +1,51 @@
+"""A client in more rooms than one statement takes parameters still starts.
+
+Postgres refuses a statement with more than 32,767 bind parameters, and the
+reads a client makes as it starts receiving matched its rooms with one
+parameter each. Switch's own client is in every room of its workspace,
+archived ones included, so past that many rooms its reader failed to start,
+retried, and failed again, for as long as the process ran.
+"""
+
+from __future__ import annotations
+
+import uuid
+
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
+
+from switch_core.db.models import Client, Room
+from switch_core.db.stores.delivery_cursor_store import DeliveryCursorStore
+from switch_core.db.stores.message_store import MessageStore
+from switch_core.db.stores.room_store import RoomStore
+
+_MORE_THAN_A_STATEMENT_TAKES = 40_000
+
+
+async def test_the_start_reads_take_any_number_of_rooms(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ async with session_factory() as session:
+ client = Client(
+ transport_user_id=f"@many-{uuid.uuid4().hex[:8]}:test",
+ display_name="in every room",
+ type="admin",
+ )
+ room = Room(
+ transport_room_id=f"!{uuid.uuid4().hex[:8]}:test",
+ name="the one that exists",
+ description="",
+ )
+ session.add_all([client, room])
+ await session.flush()
+ await RoomStore().add_client(session, client.id, room.id)
+ await session.commit()
+ rooms = [f"room-{n}" for n in range(_MORE_THAN_A_STATEMENT_TAKES)] + [room.id]
+
+ async with session_factory() as session:
+ members = await RoomStore().member_room_ids(session, client.id, rooms)
+ heads = await MessageStore().head_seqs(session, rooms)
+ places = await DeliveryCursorStore().positions(session, client.id, rooms)
+
+ assert list(members) == [room.id]
+ assert len(heads) == len(rooms) and heads[room.id] == 0
+ assert set(places) <= {room.id}