perf(server): index unfinished work during startup recovery #20302
main fromrefs/pull/20302/head·opened View on GitHubProblem#
Startup recovery looks for unfinished items across all item types, while the older partial index covers only selected types. Large finished histories are walked repeatedly. The run-candidate UNION also prevents the existing recovery index from matching its full predicate.
Change#
Add a partial index on thread_id for pending/running/waiting turn items. Consolidate run candidates into the existing index predicate while preserving held-queue exclusion. Keep the query on its current connection; routing it to the reader worker is deliberately outside this fix.
Scope and approval#
This is a focused query/index correction with unchanged selection semantics. Migration 63 is new and unreleased: renumber it against main before merge if another migration lands first. The Activity PR also independently adds migration 63.
Verification#
Run vp test run apps/server/src/orchestration-v2/ProjectionStore.test.ts -t "without walking finished turn items|without scanning historical runs". Two regressions inspect plans from the actual recovery reads and verify their results. Both fail without the query/index fixes and pass after. An earlier copied-history measurement showed provider-query VM instructions dropping from 237k to 59k across three threads with identical results, approximately 4x less VM work. It omitted bulky payloads and is not an end-to-end launch speed claim. The live first index build previously took 106.616 seconds; migration cost must be considered separately from later launches.
The requester stopped the remaining heavy checks. Completed focused results are recorded in the attached log; no repo-wide check was run.
Reproduction and focused verification log · Earlier query measurement and limitations
Prepared with GPT-6.1-Sol through the Codex harness.
- 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:S
Approvability#
Verdict: Approved at
f8b290dMacroscope's review found this PR approvable — This is a narrowly scoped startup-recovery performance optimization that adds a backward-compatible partial index and verifies query-plan usage. Existing recovery selection semantics remain intact, with no product-default, API, security, or deployment behavior 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:
7d4fce69-1ea5-442b-9acd-b67dfbbd8348
📥 Commits
-
Reviewing files that changed from the base of the PR and between cfafb4bb54d244e4612f5681aad3832ea64cb0fb and f8b290dd50cc528eb5acdbcd268dba72515afc41.
📒 Files selected for processing: 4
-
apps/server/src/orchestration-v2/ProjectionStore.test.tsapps/server/src/orchestration-v2/ProjectionStore.tsapps/server/src/persistence/Migrations.tsapps/server/src/persistence/Migrations/063_RuntimeRecoveryItemIndex.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
-
📝 Walkthrough
-
Runtime recovery now selects queued and active runs in one query, with held queued runs excluded when their payload is valid. Migration 63 adds an index for live turn items by thread ID. Tests check both recovery query plans.
Changes: Runtime recovery
Layer / File(s) Summary Live turn-item index
apps/server/src/persistence/Migrations/063_RuntimeRecoveryItemIndex.ts,apps/server/src/persistence/Migrations.ts,apps/server/src/orchestration-v2/ProjectionStore.test.tsMigration 63 creates and registers an index on thread_idfor pending, running, and waiting turn items. A test checks that provider-thread recovery lookups use the index.Recovery candidate query
apps/server/src/orchestration-v2/ProjectionStore.ts,apps/server/src/orchestration-v2/ProjectionStore.test.tsThe query combines queued and active run statuses. It excludes queued runs when valid JSON has queueHeldset to1; invalid JSON remains eligible. A test checks that run scans useruns_recovery_idx.Priority: ➖ Normal
Estimated code review effort: 2 (Simple) | ~12 minutes
Change: Bug fix
Suggested reviewers:
maria-rcks
Merge Risk: ⚪ Minimal · up to
f8b29-
No actionable issue was established in the recovery query, index, or regression tests. The PR is mergeable after normal checks.
Security architecture risk: 🔵 Low · up to
f8b29-
The query rewrite preserves recovery eligibility, including held-queue handling, without expanding access or execution authority. The remaining uncertainty concerns the first index build and migration-number coordination, not an identified security vulnerability.
Retained concerns No architecture-level concerns identified.
Security review details
-
Security Blast Radius
- inferred — The direct operational scope is the configured server SQLite database and its recovery workload. The index build affects that database during initialization; the selection rewrite does not itself expand recovery to another database, service or environment.
Trust Boundaries and Controls
- inferred — Persisted queueHeld data influences run eligibility through the same guarded JSON expression before and after this PR. The comparison does not establish a new path for overriding held-queue handling or gaining recovery authority, and the test-local detail property provides no external ingress.
Resilience and Maintainability Implications
- inferred — Recovery initiation, projection loading, shutdown preparation and downstream reconciliation remain unchanged. Candidate-set equivalence gives no identified PR-induced change to repetition, interruption or concurrent-update behavior. This does not establish end-to-end atomicity: selection and projection loading remain separate operations as before.
-
Pre-merge checks:
4-
Check name Status Explanation Title check The title is concise, uses the conventional commit format, and clearly describes the primary change: adding an index to improve startup recovery performance. Description check The description includes all required sections. It clearly explains the problem, change, scope, migration concern, focused verification, observed results, limitations, and incomplete checks. 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.
✨ Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
-
Note
Written by
claude-opus-5-5on behalf of MariaThanks for this. I'm closing it as part of PR triage. Right now we're only taking focused bug fixes from outside contributors (see Read this first), and this PR is not one.
From the review: The recovery query rewrite and unfinished-item index lack a measured startup improvement through the application. Please establish that benefit and agree on the performance work before proceeding.
If you think this was a mistake, message me on Twitter at @maria_rcks and I'll take another look.
maria-rcks closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes