[Bug]: First boot after upgrading stalls the server for seconds at a time while importing V1 transcripts
Before submitting
- [x] I searched existing issues and did not find a duplicate.
- [x] I included enough detail to reproduce or investigate the problem.
Area
apps/server
Steps to reproduce
- Take a long-lived V1 install. Mine:
state.sqlite14.1 GiB, 353 threads, 1.62M V1 events. - Start current
main(13aa882c22) against aVACUUM INTOcopy of it withT3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD=false, and wait forHydrated legacy v1 thread transcripts. - Read the
sqlitespans for the importer's existing-message lookup inserver.trace.ndjson, andlogs/event-loop-stalls.ndjson.
Expected behavior
The transcript import's per-thread lookup reads only that thread's V2 events, so it does not hold the event loop.
Actual behavior
For each thread, LegacyV1ThreadImporter looks up the messages it already imported:
SELECT event_id
FROM orchestration_events
WHERE application_event_version = 2
AND aggregate_kind = 'thread'
AND stream_id = ?
AND event_id LIKE 'migration:v1:message:%'
SQLite plans it on idx_orch_events_stream_sequence (aggregate_kind=? AND stream_id=?), so it visits every V1 event of the thread too. application_event_version comes after payload_json in the table, so checking it on each V1 row reads past that row's payload. The thread with the most events here has 73,650 V1 events and 2,854 V2 events.
Same copy, same disk, two boots of the same commit, one with the query changed to FROM orchestration_events INDEXED BY idx_orchestration_events_agent_stream_sequence:
main |
lookup pinned to the V2 partial index | |
|---|---|---|
| 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 lookups are synchronous, so during the import the server stops answering for seconds at a time. It happens once per upgrade, but on the first boot of 0.1.0, which is when people are watching to see whether their history survived.
OrchestrationEventStore.ts already forces this partial index for latestAgentSequence. #20534 proposes dropping it because idx_orch_events_stream_sequence serves the same reads. That holds once V1 events are compacted away, but while the import runs they are still in the table, and here the partial index is the difference between 148 s and 0.3 s.
Impact
Major degradation or frequent failure
Version or commit
main @ 13aa882c22, migrating a 0.0.45 database
Environment
Linux, Node 26.10.0. The copy was on a SATA SSD (NTFS); the main arm ran second, so it had the warmer page cache.
[!NOTE] Grok responding on behalf of Julius.
Confirmed on main (13aa882c, which already includes #20947). This is a bug.
- The existing-message lookup in
LegacyV1ThreadImporter.ensureTranscriptBasehas no index hint. SQLite plans it onidx_orch_events_stream_sequence (aggregate_kind=? AND stream_id=?), so every V1 row in the thread is a candidate and gets checked against the table.latestAgentSequenceinOrchestrationEventStore.tsalready forcesidx_orchestration_events_agent_stream_sequencefor the sameapplication_event_version = 2 AND aggregate_kind = 'thread' AND stream_id = ?shape. - The lookup always runs while that thread's V1 events are still in the table. The #19435 compaction only deletes V1 events for threads whose
transcript_imported_atis set, and that's written after the lookup. #20947 drops V1 projection tables, not V1 rows inorchestration_events. - The import is forked in the background, but this query runs on the main
node:sqliteconnection (it isn't routed to the read-only worker), so a slow lookup blocks the event loop.
Your measurements (148 s of lookups, stalls of up to 11 s, versus 0.31 s with the partial index forced) line up with this. This also bears on #20534, which proposes dropping that partial index.