Repository navigation
Deliver an agent's event in three statements, not seven (CHOO-3667) - #1251
Closed
abeldantas wants to merge 1 commit into
Closed
abeldantas wants to merge 1 commit into
abeldantas wants to merge 1 commit into
Conversation
A delivery read the agent's events, then the rooms dropped after its cursor, then the unread count: the agent's baselines, its own user id, the room's window start and the count each in a statement of their own. The dropped rooms are now read in the statement that reads the events, which also makes the two one snapshot, and the unread count reads its baseline, window start and user id in the statement that counts. In a CPU profile of a load test, building and running statements was most of the server's time.
abeldantas
requested review from
agent-runner353,
amaudruz,
christian-mcdermott,
lbangalosbt,
massimo-sandboxaq,
niall-sandboxaq,
petr-sandbox,
raullopez-sandbox and
simonflk-sb
as code owners
November 1, 2026 01:50
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
A critical correctness issue remains in filtered reads because removing the post-read cursor check can advance the agent cursor past a deleted event without reporting a gap.
1 open issue
What changed in this PR
This pull request reduces agent event-delivery database work from seven statements to three by combining event, expiration, and unread-count queries.
Changes:
- Combines expired-room detection with
EventBuffer.read_from. - Combines unread-count metadata lookups with
_UNREAD_IN_ROOM_SQL. - Adds coverage for the three-statement delivery path.
- Documents the optimization in the changelog.
This pull request requires changes.
| File | Summary |
|---|---|
core/tests/switch_core/management/test_stream.py |
Adds coverage verifying that a delivery uses three database statements. |
core/switch_core/bridges/agent/protocol/event_buffer.py |
Combines event-gap and unread-count queries. Critical: filtered reads can advance the cursor past a deleted event without reporting a gap because the post-read cursor check was removed. |
CHANGELOG.md |
Documents the reduced delivery statement count. |
Review effort: Lite
Comment on lines
+847
to
+852
| if found: | ||
| dropped = found[0][1] | ||
| if dropped: | ||
| await self._expired(tenant_id, agent_id, after_seq, dropped) | ||
| else: | ||
| await self.check_cursor(tenant_id, agent_id, after_seq) |
This was referenced Nov 1, 2026
Collaborator
Author
|
Combined into #1307 with the other scaling PRs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

A delivery to an agent read its events, then the rooms dropped after its cursor, then the unread count: the agent's baselines, its own user id, the room's window start and the count each in a statement of their own. With the tenant binding that was seven statements per delivery, and in a CPU profile of a load test building and running statements was most of the server's time.
In this PR we:
EventBuffer.read_from), which also makes the two one snapshot_UNREAD_IN_ROOM_SQL,EventBuffer.unread_batch)A delivery now costs three statements. In a local run of the load harness (400 agents behind 20 controllers, 2 processes, 20 msg/s), CPU per message went from 18.5 to 17.6 ms.
Stack (merge order)