Skip to content

Read a room's context in one transaction, not three (CHOO-3667) - #1258

Closed
abeldantas wants to merge 1 commit into
mainfrom
context-one-transaction
Closed

abeldantas wants to merge 1 commit into
mainfrom
context-one-transaction

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

read_context read the room's newest message number, checked membership and read the history in three sessions of their own. In this PR they share one, in the same order, so the number is still read before the history.

In a local load test (400 agents, 20 messages a second) context reads were 0.9 of the 6.5 transactions a message cost.

read_context read the room's newest message number, checked the agent
is a member and read the history, each in a session of its own: three
pool checkouts and three transactions for one read. They now share one
session (shared_tenant_session), in the same order, so the number is
still read before the history. Without a bound tenant AgentCore opens
its own session as before.

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.

🟢 Approval recommended

No active review issues are present, and the changes include a regression test covering shared-session usage while preserving history behavior.

No open issues 🎉

What changed in this PR

This pull request reduces read_context database overhead by sharing one tenant-bound SQLAlchemy session across room-head lookup, membership authorization, and history retrieval while preserving read ordering.

Changes:

  • Added shared-session support for room membership and context reads.
  • Grouped related reads within shared_tenant_session.
  • Added a regression test for session checkout count.
  • Documented the optimization in the changelog.
File Description
core/​tests/​switch_core/​bridges/​agent/​operations/​test_read_context_room_argument.py Verifies context reads use one session while preserving expected history behavior.
core/​switch_core/​bridges/​agent/​protocol/​agent_core.py Reuses the active tenant-bound session for membership and history reads.
core/​switch_core/​bridges/​agent/​operations/​definitions.py Shares one session across room-head and context-read operations.
CHANGELOG.md Documents the reduced transaction cost for room-context reads.

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