fix(server): usage-limit sweep stops scanning every run each tick #21191
main fromrefs/pull/21191/head·merged by juliusmarminge View on GitHubThe usage-limit recovery sweep runs on every scheduler tick (every 5 s). Its latest-run lookup sorts every run of every thread before filtering, so with one long thread it costs about 40 ms of synchronous SQL per tick, even when no thread has a usage-limit failure. That was about three-quarters of the server's JavaScript time while the app was idle.
Summary#
getLimitRecoveryCandidates
FROM threads t ... latest run per thread ...
WHERE t.deleted_at IS NULL
+ AND t.thread_id IN (threads with an error turn item whose failure.class = 'usage_limit')
Only threads with a usage-limit failure can qualify, so the extra filter returns the same rows. SQLite now evaluates the list first and only sorts runs for those threads.
Evidence#
- Before: 20.9 ms per sweep query on a read-only copy of a real database. In the running app it was about 40 ms every 5 s, found with a server CPU profile of an idle minute. After: 0.004 ms. Identical result rows.
vp test runProjectionStore, ProviderTurnControlService, and Scheduler.integration tests: 45 passed.
Measured on release desktop builds (build-desktop-artifact --target dir) on an Apple Silicon MacBook Pro running macOS 27.2. The seeded scratch HOME holds a 100,000-message chat and three chats streaming from a synthetic agent. The harness is in pingdotgg/t3code#21190.
Merge Danger#
Door: two-way
A query-only change.
Blast Radius: small
If the filter disagreed with the outer predicate, a thread could miss automatic recovery after a usage limit. Both read the same failure.class = 'usage_limit' error items.
Done with Claude Opus 5.5 in Claude Code.
🤖 Generated with Claude Code
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XS
Approvability#
Verdict: Approved at
f00b5c8Macroscope's review found this PR approvable — This is a tightly scoped SQL performance fix that prefilters the existing usage-limit recovery query without changing its candidate results or workflow behavior. It introduces no schema, default, deployment, or static-analysis changes.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
-
⚙️ Run configuration
-
- Configuration used: Path: .coderabbit.config.ts
- Review profile: CHILL
- Plan: Advanced
- Run ID:
4caa20cb-2162-40fd-91fe-574579877c23
📥 Commits
-
Reviewing files that changed from the base of the PR and between 9c161462c94f7d9d6d9169da5c841457cbadf213 and f00b5c8ede1248a2318ecfe6a4698aa166152252.
📒 Files selected for processing: 1
-
apps/server/src/orchestration-v2/ProjectionStore.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
-
📝 Walkthrough
-
The limit-recovery query now restricts its thread scan to threads with a failed error item classified as
usage_limit. Existing candidate checks remain unchanged.Changes: Limit recovery candidate filtering
Layer / File(s) Summary Prefilter limit-recovery candidates
apps/server/src/orchestration-v2/ProjectionStore.tsThe query filters for a failed usage_limiterror item before applying its existing candidate checks.Priority: ➖ Normal
Estimated code review effort: 2 (Simple) | ~8 minutes
Change: Refactor
Suggested reviewers:
t3dotgg
### Architecture SummaryMerge Risk: ⚪ Minimal · up to
f00b5-
The change preserves eligible recovery candidates and filters before latest-run selection in the inspected plan. No concrete behavior requiring a merge delay is established.
Architecture risk: 🔵 Low · up to
f00b5The change affects 1 system.
Changed systems:
apps/serverArchitecture concerns No architecture-level concerns identified.
Review details
-
Systems and components
- observed — apps/server (service) was modified; 1 changed file maps to changed impact.
Before / after behavior
- observed — Modified behavior in apps/server/src/orchestration-v2/ProjectionStore.ts: The limit-recovery query now prefilters threads to those with a failed error item classified as
usage_limitbefore applying its existing candidate conditions.
Pre-merge checks:
4-
Check name Status Explanation Title check The title clearly and concisely describes the main change: preventing the usage-limit sweep from scanning every run on each scheduler tick. Description check The description explains the problem, the query change, performance impact, verification results, and merge risk. It does not use the required "## Scope and approval" heading or explicitly document ma… Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Full details: Description check
-
Explanation
The description explains the problem, the query change, performance impact, verification results, and merge risk. It does not use the required "## Scope and approval" heading or explicitly document maintainer approval or the small-obvious-bug exemption, but the remaining required information is substantially complete.
-
✨ Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
-
juliusmarminge merged thisd8125a5
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes