Skip to content

Take an agent's head from the notification of its new event (CHOO-3667) - #1246

Closed
abeldantas wants to merge 1 commit into
controller-pass-retryfrom
notified-heads
Closed

abeldantas wants to merge 1 commit into
controller-pass-retryfrom
notified-heads

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

Each delivery to an agent behind a controller started with a read of the heads of all the controller's agents, to see which had new events. The notification of a new agent event already carries the event's number, and the stream used it only to wake up.

In this PR we:

  • keep the number a notification carries as the agent's head while the listener listens, and the number of an event written in the same process too (EventBuffer.ring, EventBuffer.heads)
  • read the heads only on a stream's first pass, after an idle interval with no wake, and after the listener reconnects, which is when a lost notification is found

A notification lost without its connection dropping still costs at most one idle interval, as a lost wake did before.

In a local run of the load harness (200 agents behind 10 controllers, 2 processes, 5 msg/s), that read was 2.8 transactions per message, and it is gone.

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. This PR ← you are here

Each delivery to an agent behind a controller read the heads of all the
controller's agents first. The notification of a new agent event already
carries its number, so the event buffer now keeps it as the agent's head
while the listener listens, and the controller's stream reads heads only
after an idle interval, when it attaches an agent, and after the listener
reconnects. In our load test that read was one transaction per delivery,
about three per message.

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.

🔵 Needs a closer look

The change affects cross-process PostgreSQL notification ordering, reconnect races, and controller cursor bounds, while focused tests do not replace production-like concurrency and notification-loss validation.

No open issues 🎉

What changed in this PR

This PR reduces database reads during controller-based agent delivery by treating committed event notifications as authoritative head updates while the listener is connected, while retaining fresh reads on startup, idle recovery, and reconnects.

Changes:

  • Added sequence-aware notification handling and per-agent cached heads in EventBuffer.
  • Updated controller streams to use cached heads and refresh only at recovery boundaries.
  • Added router/listener integration tests and documented the performance improvement.
File Description
core/​tests/​switch_core/​management/​test_stream.py Updated as part of this pull request.
core/​tests/​switch_core/​delivery/​test_router_sessions.py Updated as part of this pull request.
core/​tests/​switch_core/​bridges/​agent/​protocol/​test_agent_event_listener.py Updated as part of this pull request.
core/​switch_core/​delivery/​router.py Updated as part of this pull request.
core/​switch_core/​bridges/​agent/​protocol/​event_buffer.py Updated as part of this pull request.
core/​switch_core/​bridges/​agent/​protocol/​controller_stream.py Updated as part of this pull request.
core/​switch_core/​bridges/​agent/​protocol/​agent_event_listener.py Updated as part of this pull request.
CHANGELOG.md Updated as part of this pull request.

Review effort: Lite

@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