Skip to content

Keep what a post checks first until a signal says it changed (CHOO-3667) - #1269

Closed
abeldantas wants to merge 2 commits into
hold-auth-rowsfrom
hold-room-access
Closed

abeldantas wants to merge 2 commits into
hold-auth-rowsfrom
hold-room-access

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

Every post checks that its agent is in the room and that no budget covering it has run out, and a controller relaying the post checks the membership once more, for the room it names. That's two transactions per post, reading rows that rarely change.

In this PR we keep those answers in each process, the way #1259 keeps a controller token's rows: triggers on room_agents, rooms and usage_budgets signal every change, and an answer is kept while the signal connection listens, and for 5 s otherwise. A refusal is never kept, so an agent outside the room, a missing room or a budget that covers the agent always reaches the database. The bookkeeping #1259 added moves to held_rows.py, which both caches now share.

Local A/B on two pods, 400 agents behind 20 controllers at 20 msg/s, rooms of 4, a fresh database, 150 s windows, two rounds interleaved:

CPU per message delivery p50 / p99
before 14.3, 14.0 ms 36 / 59 ms
this PR 12.8, 13.0 ms 31 / 55 ms

Stack (merge order)

  1. Keep a controller token's rows until a signal says they changed (CHOO-3667) #1259
  2. This PR ← you are here

Every post checks that its agent is in the room and that no budget
covering it has run out, and a controller relaying the post checks the
membership again for the room it names: two transactions per post, on
rows that rarely change. Each process now keeps those answers the way it
keeps a controller token's rows. Triggers on `room_agents`, `rooms` and
`usage_budgets` signal every change, an answer is kept while the signal
connection listens and for the TTL otherwise, and a refusal is never
kept. The bookkeeping both caches share moves to `held_rows.py`.

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 active issue leaves the positive cache fast path able to authorize posts after local membership, archive, or budget changes commit, allowing stale authorization decisions.

1 open issue
What changed in this PR

This pull request adds process-local caching for room-membership and “no budget applies” checks during agent posts, using PostgreSQL notifications for invalidation and shared cache bookkeeping with controller authorization.

Changes:

  • Added HeldRows and RoomAccessCache with TTL, bounded storage, and signal-based invalidation.
  • Added room, membership, and budget notification DDL, migrations, configuration, and application wiring.
  • Updated budget checks, agent authorization flow, tests, and changelog documentation.

Status: This pull request requires changes.

File Summary
core/​tests/​switch_core/​management/​test_room_access_cache.py Adds PostgreSQL-backed tests for cache reuse, refusals, and trigger invalidation.
core/​tests/​switch_core/​management/​test_controller_auth_cache.py Updates management cache tests to use shared bookkeeping.
core/​tests/​switch_core/​bridges/​agent/​test_room_access_cache.py Tests cache TTL, bounds, tenant isolation, invalidation, and refusal behavior.
core/​tests/​switch_core/​bridges/​agent/​test_controller_auth_cache.py Updates controller-cache tests for the shared implementation.
core/​switch_core/​migrations/​versions/​4103210581d6_room_access_notify.py Adds production notification functions and triggers. Moderate issue: the migration DDL is not covered by the frozen-DDL parity check.
core/​switch_core/​main.py Creates and wires the process-wide room-access cache and signal listener.
core/​switch_core/​db/​room_access_notify_ddl.py Defines reusable room-access notification DDL.
core/​switch_core/​db/​models.py Registers room-access triggers for test schema creation.
core/​switch_core/​config.py Adds the room-access cache capacity setting.
core/​switch_core/​budgets.py Reports whether any budget covers an agent.
core/​switch_core/​bridges/​agent/​room_access_cache.py Implements cached membership and budget access checks.
core/​switch_core/​bridges/​agent/​protocol/​agent_core.py Uses cached room and budget checks during agent operations. Critical issue: local membership, room, and budget writes can leave positive entries usable until asynchronous signal dispatch invalidates them.
core/​switch_core/​bridges/​agent/​held_rows.py Provides shared TTL, signal-generation, listening-connection, and bounded-entry mechanics.
core/​switch_core/​bridges/​agent/​dependencies.py Passes the room-access cache through dependency initialization.
core/​switch_core/​bridges/​agent/​controller_auth_cache.py Refactors controller authorization caching onto HeldRows.
core/​switch_core/​bridges/​agent/​app.py Accepts and injects the room-access cache into AgentCore.
CHANGELOG.md Documents the new post-check caching behavior and configuration.

Review effort: Lite

Comment on lines +1474 to +1476
room = access.member(tenant_id, agent_id, room_id)
if room is not None and access.unbudgeted(tenant_id, agent_id):
return room
@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