Skip to content
Source
Sign inSign up
pingdotgg/t3code

[Bug]: First boot after upgrading stalls the server for seconds at a time while importing V1 transcripts #20966

Closedderektrimm(GitHub profile, opens in a new tab) opened this issue 1 comment

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/server

Steps to reproduce

  1. Take a long-lived V1 install. Mine: state.sqlite 14.1 GiB, 353 threads, 1.62M V1 events.
  2. Start current main (13aa882c22) against a VACUUM INTO copy of it with T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD=false, and wait for Hydrated legacy v1 thread transcripts.
  3. Read the sqlite spans for the importer's existing-message lookup in server.trace.ndjson, and logs/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:

mainlookup pinned to the V2 partial index
353 lookups, total148.2 s0.31 s
per lookupmedian 94 ms, p95 2.1 s, max 11.6 s0.9 ms on average
event-loop stalls logged5: 2.6, 3.5, 11.1, 5.5, 3.6 snone
import finished after start526 s326 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.

  1. 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.ensureTranscriptBase has no index hint. SQLite plans it on idx_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. latestAgentSequence in OrchestrationEventStore.ts already forces idx_orchestration_events_agent_stream_sequence for the same application_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_at is set, and that's written after the lookup. #20947 drops V1 projection tables, not V1 rows in orchestration_events.
    • The import is forked in the background, but this query runs on the main node:sqlite connection (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.

  2. juliusmarminge added labelsaccepted

  3. juliusmarminge added labelsbug

  4. juliusmarminge added labelsvia-triage

  5. juliusmarminge closed this

Sign in to join this conversation.