fix(server): the V1 transcript import no longer stalls on each thread's V1 events #21050
main fromrefs/pull/21050/head·merged by juliusmarminge View on GitHubFixes #20966
During the first boot after upgrading, LegacyV1ThreadImporter looks up which messages of a thread it already imported. The query filters to V2 thread events, but SQLite planned it on idx_orch_events_stream_sequence, which also walks every V1 event of the thread. application_event_version comes after payload_json, so each of those rows is read past its payload. The lookup runs on the main node:sqlite connection, so it holds the event loop while it does.
The lookup now names idx_orchestration_events_agent_stream_sequence, the V2 thread partial index latestAgentSequence already forces for the same filter.
Checked by booting a VACUUM INTO copy of a real 0.0.45 database (14.1 GiB, 353 threads, 1.62M V1 events) until Hydrated legacy v1 thread transcripts, once on main (13aa882c22) and once with this change. Same copy and disk, and the main run went second, with the warmer page cache:
main | this change | |
|---|---|---|
| 353 lookups, total | 148.2 s | 0.31 s |
| per lookup | median 94 ms, p95 2.1 s, max 11.6 s | 0.9 ms on average |
| event-loop stalls logged | 5: 2.6, 3.5, 11.1, 5.5, 3.6 s | none |
| import finished after start | 526 s | 326 s |
The new test in LegacyV1ThreadImporter.test.ts captures the lookup from ensureTranscript and checks its plan uses the partial index. It fails on main with the stream index and passes here. The legacy importer, cutover, V1ImportBoundary, OrchestrationEventStore and initializeV2Database tests pass, as do typecheck, lint and format for apps/server.
#20534 proposes dropping this index. The forced lookup would then fail on every upgrade, so this test and the importer tests would catch that.
Not checked: Windows and macOS, and an NVMe disk. The copy was on a SATA SSD.
Made with Claude Opus 5.5 in Claude Code, running in T3 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
cdfa980Macroscope's review found this PR approvable — This is a small, self-contained SQLite query-plan fix that preserves transcript-import semantics while preventing costly scans of legacy V1 events. A focused regression test verifies use of the existing V2 partial index.
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:
95e0a6e4-6522-41ba-b645-61f044987271
📥 Commits
-
Reviewing files that changed from the base of the PR and between 16918b57d6754d6f145e9b6367c825aa31a740da and cdfa980f4eaa028a1094d33ba9d005c06de06210.
📒 Files selected for processing: 2
-
apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.tsapps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.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
-
The V1 transcript importer now pins its existing-event lookup to the V2 agent stream sequence index. A test checks that the lookup uses this index.
Changes: V1 transcript lookup
Layer / File(s) Summary Pin and verify the existing-event lookup
apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.ts,apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.tsThe importer forces idx_orchestration_events_agent_stream_sequencefor the existing-event lookup. A test verifies the query plan uses that index.Priority: ➖ Normal
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
juliusmarminge
Merge Risk: ⚪ Minimal · up to
cdfa9-
This change speeds up the first-boot V1 transcript import lookup by pinning it to an existing index. Results are unchanged, and a test guards the query plan. No merge-blocking risk is evident.
Pre-merge checks:
4-
Check name Status Explanation Title check The title clearly describes the primary fix: preventing V1 transcript import lookups from stalling on V1 events. Description check The description clearly explains the problem, the index change, linked issue, performance evidence, test coverage, limitations, and agent attribution. It is sufficiently complete for review. Linked Issues check Issue #20966requires the first-boot V1 transcript lookup to read only V2 events for the thread and avoid event-loop stalls.LegacyV1ThreadImporter.tsnow forces `idx_orchestration_events_agent_stre…Out of Scope Changes check The pull request changes only the legacy transcript lookup and adds a focused plan regression test. The test directly protects the fix for issue #20966. No unrelated production behavior or unrelated f…Full details: Linked Issues check
-
Explanation
Issue
#20966requires the first-boot V1 transcript lookup to read only V2 events for the thread and avoid event-loop stalls.LegacyV1ThreadImporter.tsnow forcesidx_orchestration_events_agent_stream_sequencefor the lookup while retaining the V2, thread, stream, and migration-event filters. The newLegacyV1ThreadImporter.test.tscaptures the lookup and asserts that SQLite uses this index. These changes implement the required query and add automated coverage.
Full details: Out of Scope Changes check
-
Explanation
The pull request changes only the legacy transcript lookup and adds a focused plan regression test. The test directly protects the fix for issue
#20966. No unrelated production behavior or unrelated files appear in the reviewed diff.
-
✨ Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
-
juliusmarminge merged this7506aed
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes