Skip to content

Deliver an agent's event in three statements, not seven (CHOO-3667) - #1251

Closed
abeldantas wants to merge 1 commit into
notified-headsfrom
fused-delivery-reads
Closed

abeldantas wants to merge 1 commit into
notified-headsfrom
fused-delivery-reads

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

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:

  • read the rooms dropped after the agent's cursor in the statement that reads its events (EventBuffer.read_from), which also makes the two one snapshot
  • read the agent's baseline, the room's window start and the agent's user id in the statement that counts (_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)

  1. Make idle controller agents and startup cheap on the database as deployments grow (CHOO-3667) #1200
  2. Read a controller's agents' heads once per delivery, not three times (CHOO-3667) #1220
  3. Try a controller's delivery pass again when the pool has no connection, instead of closing its socket (CHOO-3667) #1221
  4. Take an agent's head from the notification of its new event (CHOO-3667) #1246
  5. This PR ← you are here

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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)
@abeldantas

Copy link
Copy Markdown
Collaborator Author

Combined into #1307 with the other scaling PRs.

@abeldantas abeldantas closed this Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants