Skip to content

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

Merge remote-tracking branch 'origin/many-rooms-start' into scale-core

Abel Dantas574e487f4328
diff --cc CHANGELOG.md
index 904939300,861a7967e..0d6eccf38
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@@ -315,54 -236,14 +315,60 @@@ version of their own to them without al
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.
+- **Delivering an event to an agent behind a controller no longer reads the
+ agent's head first.** The notification of a new agent event already carries
+ its number, and the controller's stream now keeps it as the agent's head, so
+ a delivery costs the agent's own read alone, where it read the heads of all
+ the controller's agents first. The heads are still read after each idle
+ interval, when an agent is attached, and after the listener reconnects.
+- **Delivering an event to an agent costs three statements, where it cost
+ seven.** The rooms dropped after the agent's cursor are read with its
+ events, and the unread count reads the agent's baseline, the room's window
+ and the agent's user id in the statement that counts.
+- **Writing a message no longer costs four statements for each agent it is
+ for.** The agents' own events are stored together, in two statements and
+ one savepoint, where each agent's event took a savepoint, a numbering, an
+ insert and a release of its own.
+- **A controller's stream waits on three events, where it waited on one per
+ agent and two more.** Every attached agent's doorbell now rings the stream's
+ one bell.
+- **Reading an agent's own events no longer builds its query each time.**
+ Each shape of the read is built once and run with bind parameters.
+- **A message addressed to a connected agent no longer reads the agent's
+ membership after it commits.** That read stops an agent removed from the
+ room in the meantime from saying anything there, and a connected agent has
+ nothing to say about a message addressed to it, so the read is now made only
+ before a refusal or an offline notice is posted, or a machine woken.
+- **Message telemetry reads a workspace's rooms, clients and agents at once.**
+ The first lookup that misses reads all of them, where each room, sender and
+ agent was read on its own, so in a large workspace most messages no longer
+ cost a lookup.
+- **Registering, renaming or deleting an agent no longer makes every process
+ read every client and agent.** The change now names the clients it touched,
+ and each process reads only those, where it read every client of the
+ workspace and, with a messaging app connected, every agent as well. The
+ 30-second re-check reads every client only when a fingerprint of the
+ workspace's clients and agents has changed since its last full read, where
+ it read them all every time.
+- **The 30-second re-check of which controller runs each agent reads one
+ query, and nothing when no placement changed.** Every process read every
+ placed agent's definition, document included, then each controller with a
+ query of its own. It now reads the columns a binding is made of in one
+ query per workspace, and only when a fingerprint of the placements has
+ changed since its last full read.
+- **The 30-second re-check of who holds each controller's socket no longer
+ reads every controller each time.** It compares a fingerprint of their
+ connections and of the live processes first, and when it reads, reads only
+ the connection columns, where it read every row with its status and
+ platform, and the rooms of every agent on a controller another process held.
#### 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 --cc core/switch_core/db/stores/message_store.py
index fc6504a11,5727c863b..f70e3e62b
--- a/core/switch_core/db/stores/message_store.py
+++ b/core/switch_core/db/stores/message_store.py
@@@ -1,22 -1,11 +1,23 @@@
from collections.abc import Collection
from 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 AsyncSession
+ from switch_core.db.id_lists import any_of
from switch_core.db.models import Message, MessageAttachment
+from switch_core.db.raw import RawStatement
class MessageStore:
@@@ -199,12 -183,11 +200,12 @@@
"""
if not room_ids:
return {}
- result = await session.execute(
- select(Message.room_id, func.max(Message.seq))
- .where(any_of(Message.room_id, room_ids))
- .group_by(Message.room_id)
+ query = select(Message.room_id, func.max(Message.seq)).where(
- Message.room_id.in_(room_ids)
++ any_of(Message.room_id, room_ids)
)
+ if types is not None:
+ query = query.where(Message.event_type.in_(list(types)))
+ result = await session.execute(query.group_by(Message.room_id))
heads = {room_id: max(int(seq or 0), 0) for room_id, seq in result.all()}
return {room_id: heads.get(room_id, 0) for room_id in room_ids}