Repository navigation
Description
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
- 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.sqliteis 49 GB). - Queue a message behind the active run, so it starts when that run ends.
- Time
provider-turn.startin 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
/compactand/goalbranches evaluatejson_extract(payload_json, '$.text')on all 7,483 user messages; - subagents, 4.0 s: the full payloads of 4,230 subagents. The only consumer,
routableSubagentsinProviderTurnStartService, keeps those withstatus = 'completed'and uses theirchildThreadIdandproviderThreadId; - 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:
- the
/compactand/goalmessages through the expression index described in [Bug]: Starting a turn in a long thread freezes the server for 0.3–0.4 s (0.03–0.05 s with one index) #21578; - the subagent routing columns through a covering index on
(thread_id, status, subagent_id, child_thread_id, provider_thread_id); - the transfers by id.
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
fix(server): turn start reads only the messages, subagents and transf…
test(server): a retry handoff's history read stays on the thread's ru…
Dropje97 commentedon Nov 18, 2026
Still on main (f1d64aa):
getTurnStartContextreads every run, attempt and provider turn of the thread at each turn start, andgetTurnStartHistorywith run ids still filtersrun_idon 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:
getTurnStartContextSo 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
getTurnStartContexton 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.
navidnadali commentedon Nov 21, 2026
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), callsgetTurnStartHistory(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 ofmissedRuns: 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 isSEARCH orchestration_v2_projection_turn_items USING INDEX orchestration_v2_projection_turn_items_thread_ordinal_idx (thread_id=?). That index holds neitherrun_idnortype, 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 endsfailedwith no provider turn, runs 3 to 6 complete on the same provider thread, and starts 3, 4, 5 and 6 each callgetTurnStartHistory(thread, [run 2])fromprepareRetryHandoff. 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):
INDEXED BY orchestration_v2_projection_turn_items_thread_run_idxThese 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.