perf(server): starting a turn on a long thread no longer scans its messages, tasks and transfers #39103
main fromrefs/pull/39103/head·merged by juliusmarminge View on GitHubProblem#
Starting a provider turn in a long thread is slow before the provider is called. ProviderTurnStartService.start reads the thread through getTurnStartContext, and on a thread with thousands of runs three of its statements read far more than the turn needs:
- messages: to find earlier
/compactand/goalturns it parses the JSON text of every user message of the thread (about 9,000 on the thread below). - subagents: it loads every task that has not failed, but the start only routes completed provider-native ones. A long thread holds thousands of completed delegated ones.
- context transfers: it loads every transfer the thread is a source or target of, but the start only reads the fork into the thread and the transfers its own handoffs came from. A long thread holds thousands of delegated-result transfers.
None of these has an index that serves the predicate, so each reads the whole thread. In traces from a long-running server, the message scan alone took up to 12 s of a 17 s turn start while the disk was busy.
Change#
Three small partial indexes (migration 100) and the three reads rewritten to use them:
| Read | Before | After |
|---|---|---|
/compact and /goal messages | scan of every user message | partial index on exactly that predicate, unioned with the run's own message |
| subagents | every non-failed task | origin = 'provider_native' AND status = 'completed', the set canRouteRelatedSubagent accepts |
| context transfers | every transfer of the thread | forks targeting the thread, plus the transfers named by the thread's handoffs |
The message predicate lives in one place (deferredCommandMessage.ts) and is used by both the migration and the read, so the two cannot drift apart. Runs, attempts and provider turns are still read in full; narrowing them needs a per-decision review and is not in this PR.
Scope#
One read, getTurnStartContext, of the start path. No new setting or feature, and the returned context is the same for every decision the start makes.
Verification#
Real SQLite code on a read-only reflink copy of a 25 GB database, on a thread of 8,882 runs and about 18,700 messages. Each statement was run twice in one process; times are the second run.
| Statement | Before | After |
|---|---|---|
| messages | 1,311 ms (25 rows) | 3 ms (24 rows) |
| subagents | 587 ms (4,638 rows) | 0.1 ms (4 rows) |
| context transfers | 354 ms (5,480 rows) | 4 ms (8 rows) |
The query plans use the new indexes without a hint. Building the three indexes on that copy took about 15 s, 0.6 s and 0.5 s while the machine was busy.
Together that removes about 2.2 s of the roughly 3.7 s the statements of this read cost on that thread. The remaining 1.3 s is the runs, attempts and provider turns. Not measured: a full turn start on a running server.
Test. One new case in ProjectionStore.test.ts seeds a thread with /compact, /goal, a plain prompt, an assistant /compact, a near-miss /compact later, a delegated and a running subagent, and a delegated-result and another thread's fork. It asserts which messages come back (including their order) and that the subagents and transfers read are empty, then checks that each statement's query plan uses its index. It fails against the previous reads.
vp run check:changed passes format, lint, knip, graphql and typecheck. Its test step fails only on the existing turn-item window test, which needs statement.stat from a newer Node than the one I ran (22); the persistence suite, ProviderTurnStartService and the rest of ProjectionStore pass (232 tests).
Created with Claude Sonnet 5.5 in Claude Code (T3 Code).
🤖 Generated with Claude Code
Evidence#
Same Linux x86_64 VM, Node v26.13.1, SQLite 3.45.1; real
t3 serve, fresh synthetic homes, public HTTP/WebSocket commands. Seeded 2,060 runs, 2,068 messages (~61 MB of synthetic user text), 512 completed app-owned tasks, one completed native task, and 6,497 transfers. Exercised native output routing, fork context, merge-back and compaction deferral, then sent identical active/queued prompt pairs.Seven independent server processes per commit: five warmup pairs, then five measured pairs each (70 measured turn starts per side). Values below are medians of the seven process medians; ranges show their full spread. The replay response delay is outside the measured turn-start span.
Turn start improved 98.30 ms (51.2%), exceeding even the combined process-median spreads of 44.57 ms. All measured runs completed and every second prompt queued.
Both sides returned the same five historical command messages, including mixed-case padded
/compactand U+2003-padded/goal, plus the current message. Each retained its completed native child and provider-thread routing target, with the same two relevant transfer types. Both persisted the native child's later output, delivered fork context inline, left the merge handoff undelivered on/compact, and delivered that same handoff and its marker on the next prompt.Measurement script · Seed/behavior driver · Native ACP replay · All scripts, raw observations and recordings · Observation-only patch · Comparison data
This smaller warm-cache capture does not reproduce the author's production absolute timings. Native goal execution and legacy subagent-result handoffs remain unverified.
Captured by gpt-6.1-sol via Codex.
Not shown: