Skip to content

Starting a turn in a long thread spends 20–30 s in store reads that load the whole thread #35597

Open

Description

@Dropje97

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. Use a long coordinator thread. Ours have 6,700–7,100 runs, 58,000–61,000 turn items, 14,000–17,000 messages, 2,400–4,300 subagents and 3,100–4,900 context transfers (statev2.sqlite is 49 GB).
  2. Queue a message behind the active run, so it starts when that run ends.
  3. Time provider-turn.start in the effect outbox, or the three store reads below.

Expected behavior

A turn start's store reads stay well under a second regardless of thread length, so a queued message starts soon after the previous run ends.

Actual behavior

On main (fc2b5de), on a block clone of our database with no other load, one turn start's store reads take 21–29 s:

Read Thread A: 7,101 runs, 60,641 items Thread B: 6,717 runs, 58,031 items
getTurnStartHistory(threadId, runIds) 11.5 s 9.7 s
getTurnStartContext 16.0 s 9.4 s
getRuntimeRecoveryProjection 2.0 s 1.5 s

These are the second of two passes; the first was up to 2.8× slower. In production, on an older nightly with local patches, 17 consecutive queued starts on thread A took a median of 39 s (23–191 s) from the end of one run to the start of the next run's provider turn. Of that, 30.5 s was in provider-turn.start.

On a coordinator thread with many delegated children, completions and messages keep arriving while each turn start takes half a minute or more (up to 191 s here). The queue then grows faster than the coordinator can work it off, and it falls further behind, even when a queued message needs no action.

1. getTurnStartHistory with run ids walks every turn item of the thread. prepareRetryHandoff passes the missed run ids. To satisfy ORDER BY ordinal ASC, turn_item_id ASC, the planner picks orchestration_v2_projection_turn_items_thread_ordinal_idx (thread_id=?) and tests run_id on every item of the thread. With INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx whenever runIds is given, the read takes 2 ms on both threads and returns the same rows. getTurnStartContext's run-items query already avoids this planner choice with INDEXED BY ..._run_ordinal_idx.

2. getTurnStartContext reads more than its consumers use. On thread A:

  • messages, 4.9 s: the /compact and /goal branches evaluate json_extract(payload_json, '$.text') on all 7,483 user messages;
  • subagents, 4.0 s: the full payloads of 4,230 subagents. The only consumer, routableSubagents in ProviderTurnStartService, keeps those with status = 'completed' and uses their childThreadId and providerThreadId;
  • context transfers, 2.1 s: all 4,885 transfers with the thread as source or target. The consumers need the transfers that the thread's handoffs reference, a pending fork transfer for this run, and the earliest incoming fork for hasConversation;
  • runs, attempts and provider turns, 1.2–1.5 s each: every row of the thread.

In a local build we narrowed the first three to those rows:

Together with fix 1, a turn start's reads drop to about 5 s on both threads: getTurnStartContext 4.2 s, recovery 0.6–1.0 s, history 2 ms. What remains in getTurnStartContext is the full read of runs, attempts and provider turns. We left it, because its consumers aggregate over the whole provider thread.

Impact

Major degradation or frequent failure

Version or commit

main fc2b5de

Environment

Linux x64, headless server, Node 26.12.1 (node:sqlite, SQLite 3.54.0)


Measured and written by Claude (Opus 5.5) in T3 Code; posted with the owner's approval.

Activity

  1. Dropje97 commented on Nov 18, 2026

    @Dropje97
    Author

    Still on main (f1d64aa): getTurnStartContext reads every run, attempt and provider turn of the thread at each turn start, and getTurnStartHistory with run ids still filters run_id on the thread's ordinal index (point 1 above).

    One day in production (22 h on 0.1.11-nightly.20261117.4838 with local read patches that leave these three queries as they are), for our two coordinator threads of 9,100 and 10,200 runs. Every statement over 250 ms is logged:

    Read in getTurnStartContext Turn starts logged Median p90 Rows (median)
    runs 790 623 ms 986 ms 9,670
    attempts 929 556 ms 909 ms 9,629
    provider turns 930 611 ms 993 ms 9,321

    So each turn start on these threads pays about 1.8 s for three reads that grow with the thread, about 30 minutes of reader time a day. That is after we rewrote the database into 32K ZFS records, which made its random reads about 30 times cheaper. On a clone, the whole getTurnStartContext on the larger thread took 5.4 s before that rewrite and 2.2–2.7 s after it. A busy coordinator receives about one message a minute, so this fixed cost lands on almost every message it handles.

    Measured and written by Claude (Opus 5.5) in T3 Code; posted with the owner's approval.

  2. navidnadali commented on Nov 21, 2026

    @navidnadali
    Contributor

    Note

    Written by Claude Opus 5.5 on behalf of Navid

    Point 1 is still on main after #39103 (checked at e203593), and it can be reproduced on a fresh database. One earlier start that failed before its provider turn is enough to reach it, which is likely why the capture on #35723 did not show it. No queued message is needed.

    prepareRetryHandoff, which runs on each turn start (ProviderTurnStartService.ts:1858), calls getTurnStartHistory(threadId, missedRunIds) (:1194-1199) when the provider thread has an earlier run that failed or was interrupted before its provider turn started (missedRuns, :1158-1164). Delivering that run's request does not take it out of missedRuns: later starts read its items again and drop them as covered (:1200-1202). So after one start fails before the provider takes the turn, for example on a provider error, every later start on that provider thread runs this read.

    The read has no index hint (ProjectionStore.ts:5603-5610). Without planner statistics (the server deletes them on open, persistence/Sqlite.ts:91-97) its plan is SEARCH orchestration_v2_projection_turn_items USING INDEX orchestration_v2_projection_turn_items_thread_ordinal_idx (thread_id=?). That index holds neither run_id nor type, so the read visits every item of the thread and reads its row to check them.

    Reproduced through the orchestrator with one case added to ProviderSwitch.integration.test.ts: the test adapter fails the second turn start once, then four more messages are sent, each started immediately. Run 2 ends failed with no provider turn, runs 3 to 6 complete on the same provider thread, and starts 3, 4, 5 and 6 each call getTurnStartHistory(thread, [run 2]) from prepareRetryHandoff. Only run 3 gets a context handoff.

    The read's cost on main, on synthetic threads with six turn items per run (real store on a file database; SQLite VM steps of the store's own statement; both forms return the same rows):

    Turn items in the thread As on main With INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx
    6,000 60,046 140
    30,000 300,046 140
    60,000 600,047 140
    120,000 1,200,047 140

    These numbers are from macOS arm64; on Linux x86_64 and arm64 (Debian 13, Node 26.13.1) the plan, rows and VM steps are the same, and the scenario passed 20 of 20 runs on each.

    On the issue's database this read took 9.7–11.5 s, and 2 ms with the hint. The hint from #35723 still applies to main unchanged and needs no migration: the index comes from migration 055 (persistence/Migrations/OrchestrationV2/ShellIndexes.ts:7-10).

    The test case, the measurement and the commands: https://gist.github.com/navidnadali/8acde65574001ac91a9954d5ba32cf93

    Made with Claude Opus 5.5 via Claude Code. Reviewed by GPT-6.1 Sol via Codex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions