Skip to content

Build the budget standings statement once, not on every read (CHOO-3667) - #1250

Closed
abeldantas wants to merge 1 commit into
mainfrom
budget-standings-once
Closed

abeldantas wants to merge 1 commit into
mainfrom
budget-standings-once

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

Every message an agent posts reads its budget standings twice: once to check the agent may post, and once as the router decides the row. Each read built its statement again, a correlated subquery and a dozen comparisons, and building it took longer than running it.

In this PR we build the two statements (every budget of a workspace, the budgets covering one agent) once, at import, and run them with bind parameters (BudgetStore.standings).

In a local run of the load harness (400 agents behind 20 controllers, 2 processes, 20 msg/s), CPU per message went from 19.8 to 19.0 ms.

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

The implementation has no actionable findings, includes regression coverage for statement reuse, and the only remaining comments are non-blocking wording nits.

No open issues 🎉

What changed in this PR

This pull request reduces per-message overhead by constructing budget standings SQL statements once at import time and reusing them with bind parameters, while preserving existing filtering and spend-calculation behavior.

Changes:

  • Reuses tenant-wide and agent-specific standings statements.
  • Updates BudgetStore.standings to execute parameterized statements.
  • Adds regression coverage and documents the optimization in the changelog.

This pull request is ready to be approved.

File Summary Findings
core/​tests/​switch_core/​db/​stores/​test_budget_store.py Adds coverage confirming standings statements are reused. Nit: change “cost” to “costs” in the new docstring. (1 vote)
core/​switch_core/​db/​stores/​budget_store.py Builds and reuses the parameterized budget standings statements. No actionable findings.
CHANGELOG.md Documents the query-construction performance improvement. Nit: change “cost” to “costs” in the changelog entry. (1 vote)

Review effort: Lite

Every message an agent posts reads its budget standings twice: once to
check the agent may post, and once as the router decides the row. Each
read built its statement again, a correlated subquery and a dozen
comparisons, and in a CPU profile of a load test building it was about a
tenth of the server's time, more than running it. The two statements
(every budget of a tenant, the budgets covering one agent) are now built
at import and run with bind parameters.
@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