Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): turn starts in long threads no longer read the whole thread #35723

Openjuliusmarmingewants to merge 7 commits intomain fromrefs/pull/35723/headopened View on GitHub

juliusmarmingecommented

The conflict item was correct. origin/main registers [87, "ProjectionProjectsCustomTitle"], and this branch had [87, "TurnStartReadIndexes"]. I merged origin/main into the branch with a merge commit, not a rebase. Main's migration keeps id 87. This fix's migration file is renamed to 088_TurnStartReadIndexes.ts and registered as [88, "TurnStartReadIndexes", Migration0088] after it. Both migration-history lists in reconcileV2PreviewMigration.test.ts now list 87 ProjectionProjectsCustomTitle then 88 TurnStartReadIndexes. The ProjectionStore comment that pairs the query predicate with the partial index now names migration 088. A database that already applied main's 87 therefore gets these indexes as 88, and main's migration still runs. The fix is otherwise unchanged: INDEXED BY for getTurnStartHistory, the partial /compact and /goal message index with keyed UNION arms, the subagent routing covering index with subagentRoutes, and the narrowed context-transfer read.

Fixes pingdotgg/t3code#35597

Reproduction on main

On origin/main f70e349eca: started the checkout's own server (node src/bin.ts serve --base-dir <fresh dir>, T3CODE_DEV_SEED=0) so it created and migrated a fresh statev2.sqlite, then stopped it. In a copy of that database, inserted two synthetic threads of 60,000 turn items each (about 8.5 items per run, synthetic payloads) and ran the exact getTurnStartHistory query with two run ids through node:sqlite (SQLite 3.54.1, no planner statistics, as the server runs). EXPLAIN QUERY PLAN: SEARCH ... USING INDEX orchestration_v2_projection_turn_items_thread_ordinal_idx (thread_id=?), so run_id is tested on every item of the thread: 12 rows in 60-70 ms. With INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx, the plan is (thread_id=? AND run_id=?) plus a temp B-tree sort: the same 12 rows in 0.05-0.15 ms. The cost grows with thread length and row size, which fits the reporter's 9.7-11.5 s on a 49 GB database. Added a plan assertion to the existing ProjectionStore.test.ts case 'reads a run's turn-start items by thread and run, not by walking thread history' (10,000-item history). It fails on main; with the one-line INDEXED BY fix it passes. I did not time part 2 (getTurnStartContext over-reading messages, subagents and context transfers); it rests on the code and the reporter's measurements.

Evidence

Being captured: before and after in the real app. This section is filled in before the pull request can merge.

Tests

vp test run on persistence/reconcileV2PreviewMigration.test.ts, persistence/Migrations/087_ProjectionProjectsCustomTitle.test.ts, orchestration-v2/ProjectionStore.test.ts, TurnStartReads.test.ts and ProviderTurnStartService.test.ts: 5 files, 117 tests passed. vp run check:changed after the merge commit: format, lint, knip 1/2 and 2/2, graphql, typecheck (3 packages) and tests (6 files) all pass.

Not in this change

  • getTurnStartContext's messages query still runs json_extract/trim over every user message of the thread to find /compact and /goal turns (and the hasConversation EXISTS does the same). Making that indexed needs a generated column or index migration.
  • getTurnStartContext still decodes full subagent payloads although only childThreadId/providerThreadId are used. Returning just routing ids would change the store's contract and its test mocks.
  • getTurnStartContext still reads every run, attempt, provider thread, provider turn and context handoff of the thread.
  • The hasConversation EXISTS still evaluates json_extract per user message until it finds the first non-/compact one; in practice it stops at the first ordinary message, so it was left as is.
  • RunExecutionService.canRouteRelatedSubagent is now used only by its own test; the store's SQL encodes the same 'completed' rule. I didn't remove the export.
  • getTurnStartContext still reads every run, attempt and provider turn of the thread; the issue itself leaves these because the consumers aggregate over the whole provider thread.
  • The hasConversation EXISTS still checks user messages one by one until the first ordinary one.
  • RunExecutionService.canRouteRelatedSubagent is now used only by its test.
  • getTurnStartContext still reads every run, attempt and provider turn of the thread, as the issue itself leaves.

Who did the work

  • Written by Claude Opus 5.5, in Claude Code
  • Reviewed by GPT-6.1 Sol, in Codex, and by Claude Opus 5.5, each on its own
  • Proof captured by GPT-6.1 Sol and checked by Claude Opus 5.5

Run by the t3code factory in T3 Code.

    1. Pushed9b4562b

    2. juliusmarminge added labelstakeover:blocked

    3. juliusmarminge removed labelstakeover:in-progress

    4. Note

      Written by Claude Opus 5.5 (Claude Code) on behalf of Navid

      The stop on 2026-11-13 said the capture "could not show the problem on the base commit". Its recipe never reaches the read this PR changes. On a turn start, prepareRetryHandoff only runs getTurnStartHistory(threadId, runIds) when the provider thread has an earlier missed run: failed or interrupted without a delivered provider turn, or cancelled with an undelivered one (ProviderTurnStartService.ts:1214-1222, :1249-1259 at 344de5fe8e). The recipe has no such start. One start that fails before its provider turn is enough: every later start on that provider thread runs the read.

      Since the 2026-11-22 merge of main, this PR is the two-line INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx hint plus its plan assertion. It merges cleanly onto 344de5fe8e, and the assertion fails on main and passes with the hint.

      What I ran, in the real app:

      • t3 serve from main and from main with this PR merged, each under env -i on its own scratch base dir, driven through the server's own CLI. The provider was a local ACP test agent with no model.
      • A seed, built once with main's server: one run streamed 6,666 assistant messages of 32 KiB and 53,328 completed tool calls (59,997 turn items; a second thread had twice the messages and tool calls). Then three warm-up sends and a restart.
      • One send on which the provider process failed to start on all 5 attempts (simulated: the test agent answers initialize with an error), so the run ended failed with no provider turn. Each round then started one side's server on a copy of that state and made 6 sends; two rounds per side, alternated. Every later run completed on the same provider thread and agent session.

      Each of those starts ran getTurnStartHistory(thread, [failed run]) from prepareRetryHandoff. The table covers sends 2-6 (send 1 also loads the session), n = 10 per side, from the server's trace spans and the agent's receipt stamp:

      ThreadSideHistory read (SQLite span), median (min-max)Plan, VM steps for 2 rowsSend accepted → agent receives prompt, median (min-max; IQR)
      60,008 itemsmain88.4 ms (80.4-116.5)thread_ordinal_idx (thread_id=?), 493,454241 ms (202-293; 231-255)
      this PR0.48 ms (0.34-0.65)thread_run_idx (thread_id=? AND run_id=?) + temp B-tree, 71143 ms (126-194; 135-158)
      120,002 itemsmain142.8 ms (120.5-158.3)thread_ordinal_idx (thread_id=?), 986,738269 ms (213-325; 232-301)
      this PR0.39 ms (0.31-0.74)thread_run_idx (thread_id=? AND run_id=?) + temp B-tree, 71132 ms (113-211; 124-137)

      Control, the same threads without the failed start: neither side calls getTurnStartHistory, and send → receipt is about the same (137 vs 148 ms at 60k, 132 vs 122 ms at 120k).

      Debian 13 x86-64 (32 cores / 64 threads, load average 31-74 from other work), Node 26.13.1, SQLite 3.54.1, database on tmpfs. Plans are EXPLAIN QUERY PLAN of each checkout's own statement through the server's SQLite setup (no planner statistics).

      On an Apple silicon Mac (macOS 27.0.1, 18 cores), each checkout's own store gives the same plans and VM steps: 493,454 / 986,738 on main and 71 with this PR. That run used synthetic threads of the same shape, written directly to a fresh database; on Linux, those synthetic threads give exactly the real-app counts.

      The timings are from Linux only; macOS timings were not run.

      Not covered:

      • Claude and Codex, and their own ways of failing a start.
      • interrupted and cancelled runs, the other ways a run is missed.
      • The reporter's queued starts.
      • The whole-thread form of getTurnStartHistory, which the hint does not change. It did not run here, because the agent reports context usage.
      • getTurnStartContext and getRuntimeRecoveryProjection.

      Scripts, test agent, per-send data and plans: https://gist.github.com/navidnadali/c145f1a92152138bd02d549fd01514c9

      Made with Claude Opus 5.5 via Claude Code. Reviewed by GPT-6.1 Sol via Codex. Independently verified by Claude Fable 5.2.

Sign in to join this conversation.