Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): sending to a long compacted thread waits about 3x less on its history read #35180

Mergedt3dotgg(GitHub profile, opens in a new tab)merged 0 commits intomain fromrefs/pull/35180/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHub

Sending a prompt in a long thread waits on one read: message.dispatch loads the attempts, nodes, user messages and provider turns of every run before it plans. #35130 made that read cheaper for every thread, and #35178 stopped it from reading the records of finished subagents. In a thread that was compacted, most of what is left belongs to runs the send never looks at again.

This PR reads those four record types only for the runs that can still matter. On an 8,008-run compacted thread the read goes from about 340 ms to about 109 ms warm, and from about 1.4 s to about 0.65 s with a cold page cache.

This replaces #34641 by Braulio Oliveira (brauliobo). The idea and the sinceLastCompaction filter are his, and his commits are kept in this branch. Refs #29468.

What the read keeps

For a thread with a completed compaction, attempts, nodes, messages and provider turns are read only for:

  • runs from the latest completed compaction's run on
  • runs that had not ended when that run was requested (queued, live, and a held queue run that ran late)
  • runs the command names (continuation source, steer or restart target, steer follow-up)
  • the run that already holds the message, when a message is sent again

Runs and turn items are not narrowed. A thread with no completed compaction reads in full, as before.

Why runs are not narrowed

The original PR also dropped old runs. I reviewed every read in dispatchMessage and found old runs it still needs:

ReadNeeds
latestExecutedRun, usageLimitBlockedRun, restart stalenessthe run that ended last. A held queue run has a low ordinal and can end after the compaction.
worktreeSetupPendingany old failed worktree setup
idle-compaction busy checkany old run with an open delegated result
legacy import handoffthe latest completed run
modelSwitchSincethe last prompt the timeline shows, which can be far older than the compaction

The last one failed on real data. On 1 of 31 real compacted threads, the run before the compaction was a delegated-result wake. The original window started there and lost the model-switch divider. Each case would need its own SQL branch that mirrors Orchestrator logic. Reading all runs and turn items makes these reads the same as on main by construction.

Two commands read in full: an idle compaction (its busy check looks for a message under any old run) and a provider switch (the new provider is handed what ran since it last saw the thread).

With the settled subagent filter of #35178

#35178 added omitSettledSubagentRecords to the same read. It skips the node and the provider thread of each settled subagent. This branch now has main merged in, and the command read applies both filters.

  • Only the node read is narrowed by both. It takes the nodes of the recent runs first, then drops the settled subagent nodes. The result is the intersection of the two filters.
  • Provider threads are narrowed only by #35178. Attempts, messages and provider turns are narrowed only by this PR.
  • The full read of an idle compaction and of a provider switch is the command read of main, with the #35178 filter and no window.
  • Cost: the node read still walks the index of every settled subagent of the thread. With 8,162 settled subagents that is about 9 ms of the 109 ms. A lookup per kept node would remove it, at the price of a second form of the node query. It is not in this PR.

Smaller than the original

2 source files, +138/-9. The original changed 4 source files, +254/-21, one of them a migration with four indexes.

  • No migration and no new index. The lookups use the indexes from migrations 082 and 083.
  • No hasOnlyMaintenanceUserMessages store query. The title check uses the loaded prompt items and falls back to one read of the user messages.
  • No second read for a wake's parent run. Every run is loaded.
  • No change to turn item, subagent or context transfer plans. #35130 owns those now.

Measured

Real ProjectionStore code, current main (with #35130 and #35178) against this branch, on a copy of my database. The 8,008-run thread is a real compacted thread of 104 runs and 106 subagents, cloned 77 times with shifted timestamps (77 compactions, 8,162 subagents). Real threads here have at most 156 runs. Warm: median of 7 reads, 5 rounds. Cold: 5 runs, each in a new process after the file was dropped from the page cache.

ThreadmainThis PR
8,008 runs, compacted, warm331 to 351 ms106 to 112 ms
8,008 runs, compacted, cold cache1.33 to 1.49 s0.60 to 0.69 s
7,992 runs, never compacted, warm313 to 332 ms314 to 334 ms
Longest real compacted threads (151 to 156 runs), warm8.2 to 9.8 ms5.0 to 7.6 ms

That is about 3.1x warm and about 2.1x cold. Before #35178 landed, the same test gave 365 to 385 ms on main and 101 to 104 ms here (about 3.5x).

What is left is the run read: about 70 ms for 8,008 runs, most of it payload decoding.

Proof

  • Real data. A read-only snapshot of my database with migrations 084 and 085 applied: 3,965 threads, 32 of them compacted, 19 with settled subagents on both sides of the compaction. For each compacted thread, the combined read and a full read give the same thread, runs, turn items, subagents, provider sessions, runtime requests, pending transfers and checkpoint scopes, the same records of every recent run (attempts, messages in order, provider turns, root nodes, open nodes of active runs, queued and held runs), the same active provider thread and provider threads of the thread, every provider thread that a run, attempt, provider turn, kept node or kept subagent names, and the same latest executed run and its failure, usage-limit run, shell error class, modelSwitchSince result and worktree setup state. The combined read is the intersection of the two filters for each record type. 1,312 checks, 0 differences. The 3,933 threads that were never compacted read the same with and without the window. The copy that vp run migrate-dev-db makes keeps 13 of the compacted threads: 533 checks, 0 differences. No thread or run in the snapshot (8,185 runs) names a provider thread of another thread, and no run root is a subagent node.
  • Store test. One thread in ProjectionStore.test.ts covers an old queued run, a held queue run that ended late, several compactions, a compaction that was rolled back, failed or is still running, a named run and a re-sent message, for attempts, nodes, provider turns and messages. It fails on main, and it fails when any one of 10 rules of the window is removed. The same thread has subagents that ended on both sides of the compaction, settled and not, and one that ran in the thread's own provider thread. The combined read must equal the intersection of the two filters. It fails when either filter is dropped from the combined read (6 mutations).
  • Integration test (from the original PR). A steer and a restart of a wake whose parent run is older than the compaction still settle the wake. It passes on main too: behavior does not change.
  • vp run check:changed passes. All 2,690 tests in the 122 orchestration-v2 test files pass on the merged branch.

Not covered

  • Threads that were never compacted. No change there, as the table shows. The same window could start at the latest executed run instead of a compaction, because nothing in it depends on the compaction. That needs its own review of the shell error read, so it is not in this PR.
  • The other commands that use the same read (queued run start, subagent finalize, delegate-task, wake and restart continuation) still read in full.
  • Not measured: a real send on a running server, and a database larger than 9 GB. The timings above ran on a 4.3 GB copy.

Created with Claude Fable 5.2 in Claude Code (T3 Code).

🤖 Generated with Claude Code

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    2. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Approved at 14e4561

      Macroscope's review found this PR approvable — This reduces history reads when sending messages to compacted threads while retaining relevant runs and full-history fallbacks for idle compaction and provider switches. It changes internal read behavior without deleting history or changing product defaults.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    3. github-actions[bot](GitHub profile, opens in a new tab)commented

      Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      CodexThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      CodexLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      CodexLive turn WebSocket decoded21.5 KiB21.5 KiB0 B (0.0%)24.4 KiB✅
      CodexLive turn messages110 (0.0%)8✅
      ClaudeTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      ClaudeThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      ClaudeLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      ClaudeLive turn WebSocket decoded21.9 KiB21.9 KiB0 B (0.0%)24.4 KiB✅
      ClaudeLive turn messages110 (0.0%)8✅

      Other scenarios

      All 56 scenario measurements match main.

      All 56 scenario measurements
      ProviderScenarioMetricMain baselineThis PRImpactPR ceiling
      Codexshort turn resumeWebSocket decoded10.5 KiB10.5 KiB0 B (0.0%)12.7 KiB✅
      Codexshort turn resumeWebSocket messages220 (0.0%)2✅
      Codexlong turn running, cold openHTTP decoded281.7 KiB281.7 KiB0 B (0.0%)302.7 KiB✅
      Codexlong turn running, cold openrows75750 (0.0%)75✅
      Codexlong turn running, history pageHTTP decoded92.4 KiB92.4 KiB0 B (0.0%)107.4 KiB✅
      Codexlong turn running, history pagerows75750 (0.0%)75✅
      Codexlong turn finished, cold openHTTP decoded381.5 KiB381.5 KiB0 B (0.0%)410.2 KiB✅
      Codexlong turn finished, cold openrows1511510 (0.0%)151✅
      Codexlong turn finished, socket cold openWebSocket decoded381.6 KiB381.6 KiB0 B (0.0%)410.2 KiB✅
      Codexlong turn finished, socket cold openWebSocket messages220 (0.0%)2✅
      Codexlong turn finished, socket cold openrows1511510 (0.0%)151✅
      Codexlong turn finished, work pageHTTP decoded186.2 KiB186.2 KiB0 B (0.0%)200.2 KiB✅
      Codexlong turn finished, work pagerows1501500 (0.0%)150✅
      Codexidle thread resumeWebSocket decoded65 B65 B0 B (0.0%)200 B✅
      Codexidle thread resumeWebSocket messages110 (0.0%)1✅
      Codexfleet shell snapshotHTTP decoded41.0 KiB41.0 KiB0 B (0.0%)53.7 KiB✅
      Codexfleet shell snapshotrows27270 (0.0%)27✅
      Codexfleet shell subscribeWebSocket decoded65 B65 B0 B (0.0%)200 B✅
      Codexfleet shell subscribeWebSocket messages110 (0.0%)1✅
      Codexstale shell resumeWebSocket decoded140 B140 B0 B (0.0%)400 B✅
      Codexstale shell resumeWebSocket messages220 (0.0%)2✅
      Codexstale todo list cold openHTTP decoded72.4 KiB72.4 KiB0 B (0.0%)83.0 KiB✅
      Codexstale todo list cold openrows75750 (0.0%)75✅
      Codexstale todo list cold opennodes220 (0.0%)4✅
      Codexstale todo list cold opensubagents220 (0.0%)4✅
      Codexstale todo list cold openplans110 (0.0%)2✅
      Codexstale todo list cold openproviderTurns110 (0.0%)2✅
      Codexstale todo list cold opencheckpoints110 (0.0%)2✅
      Claudeshort turn resumeWebSocket decoded10.6 KiB10.6 KiB0 B (0.0%)12.7 KiB✅
      Claudeshort turn resumeWebSocket messages220 (0.0%)2✅
      Claudelong turn running, cold openHTTP decoded283.3 KiB283.3 KiB0 B (0.0%)302.7 KiB✅
      Claudelong turn running, cold openrows75750 (0.0%)75✅
      Claudelong turn running, history pageHTTP decoded93.4 KiB93.4 KiB0 B (0.0%)107.4 KiB✅
      Claudelong turn running, history pagerows75750 (0.0%)75✅
      Claudelong turn finished, cold openHTTP decoded384.2 KiB384.2 KiB0 B (0.0%)410.2 KiB✅
      Claudelong turn finished, cold openrows1511510 (0.0%)151✅
      Claudelong turn finished, socket cold openWebSocket decoded384.3 KiB384.3 KiB0 B (0.0%)410.2 KiB✅
      Claudelong turn finished, socket cold openWebSocket messages220 (0.0%)2✅
      Claudelong turn finished, socket cold openrows1511510 (0.0%)151✅
      Claudelong turn finished, work pageHTTP decoded188.1 KiB188.1 KiB0 B (0.0%)200.2 KiB✅
      Claudelong turn finished, work pagerows1501500 (0.0%)150✅
      Claudeidle thread resumeWebSocket decoded66 B66 B0 B (0.0%)200 B✅
      Claudeidle thread resumeWebSocket messages110 (0.0%)1✅
      Claudefleet shell snapshotHTTP decoded41.6 KiB41.6 KiB0 B (0.0%)53.7 KiB✅
      Claudefleet shell snapshotrows27270 (0.0%)27✅
      Claudefleet shell subscribeWebSocket decoded66 B66 B0 B (0.0%)200 B✅
      Claudefleet shell subscribeWebSocket messages110 (0.0%)1✅
      Claudestale shell resumeWebSocket decoded140 B140 B0 B (0.0%)400 B✅
      Claudestale shell resumeWebSocket messages220 (0.0%)2✅
      Claudestale todo list cold openHTTP decoded73.4 KiB73.4 KiB0 B (0.0%)83.0 KiB✅
      Claudestale todo list cold openrows75750 (0.0%)75✅
      Claudestale todo list cold opennodes220 (0.0%)4✅
      Claudestale todo list cold opensubagents220 (0.0%)4✅
      Claudestale todo list cold openplans110 (0.0%)2✅
      Claudestale todo list cold openproviderTurns110 (0.0%)2✅
      Claudestale todo list cold opencheckpoints110 (0.0%)2✅

      Baseline: 476fc9b · PR result: 14e4561 · Source CI: success

      Scenario and decoded snapshot size

      10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

      • Codex decoded thread snapshot: 108.5 KiB
      • Claude decoded thread snapshot: 108.8 KiB

      Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

    4. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Explain Change →

      No actionable comments were generated in the recent review.

      Walkthrough

      Dispatch now loads projections filtered by the latest completed compaction for ordinary messages, while retaining specified runs and messages. Idle compaction and provider changes continue to use full projections. Store and integration tests cover filtering and steering completion across compaction.

      Changes: Compaction-aware dispatch

      Layer / File(s)Summary
      Select records from the compaction window
      apps/server/src/orchestration-v2/ProjectionStore.ts, apps/server/src/orchestration-v2/ProjectionStore.test.ts
      ProjectionRecordFilter can retain specified runs and the run associated with a message. Unwindowed reads filter attempts, nodes, provider turns, and messages using eligible runs from the latest completed, non-rolled-back compaction. Tests cover run statuses, retained records, and settled subagent records.
      Load filtered projections during dispatch
      apps/server/src/orchestration-v2/Orchestrator.ts
      Ordinary message dispatch passes retention IDs and the current message to filtered projection reads. Idle compaction uses a full projection. Dispatch reloads the full projection when the active and selected providers differ. Maintenance-history detection checks both loaded user messages and user-message turn items.
      Verify steering completion across compaction
      apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
      Integration tests cover a user run before the delegated-completion wake and both steer dispatch modes when the origin run precedes the latest compaction. They assert that task completion is delivered after the steered wake finishes.

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Refactor

      Sequence Diagram(s)

      sequenceDiagram
        participant dispatchMessage
        participant ProjectionStore
        participant providerSelection
        dispatchMessage->>ProjectionStore: Read filtered projection for ordinary message
        dispatchMessage->>providerSelection: Compare active and selected providers
        alt Providers differ
          dispatchMessage->>ProjectionStore: Reload full projection
        end

      Possibly related issues

      • pingdotgg/t3code#29468 — This change narrows the message.dispatch history read by the latest completed compaction. It does not change queued-run edits or promote-to-steer reads.

      Possibly related PRs

      Suggested reviewers: juliusmarminge, yash-singh1, maria-rcks

      Merge Risk: Minimal · up to 14e45

      This change narrows the history read when sending to long compacted threads, which should make sends faster. Threads without a compaction, idle compaction and provider switches keep reading full history. I found no concrete merge-blocking risk in the supplied context.

      Pre-merge checks: Passed 4

      Check nameStatusExplanation
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Title checkPassedThe title names the main change: sending to a long compacted thread reads less history, with a perf(server) conventional prefix. The description and objectives confirm the change narrows the message.d…
      Description checkPassedThe description follows the template sections (Problem, Change, Scope, Verification) in substance. It explains the problem with message.dispatch reads, the change (windowed reads for compacted threads…
      Full details: Title check

      Explanation

      The title names the main change: sending to a long compacted thread reads less history, with a perf(server) conventional prefix. The description and objectives confirm the change narrows the message.dispatch history read for compacted threads. The title's 3x figure is a measured claim from the PR body, and the objectives cite a 3.5x figure, a minor inconsistency that does not affect relevance.

      Full details: Description check

      Explanation

      The description follows the template sections (Problem, Change, Scope, Verification) in substance. It explains the problem with message.dispatch reads, the change (windowed reads for compacted threads), the scope (links #29468 and replaces #34641, notes what is not covered), and detailed verification with timings, real-data comparison, and store and integration tests. It does not use the explicit Problem/Change/Scope/Verification headings, and the Scope section does not state a bug-fix justification in template form, but the content is complete enough for a pass.

      Finishing Touches

      Generate unit tests (beta)

      • Commit to this branch
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
      Recent review info

      Run configuration

      Commits

      Reviewing files that changed from the base of the PR and between c466357025be6e5fb3affc97c278c93e2c33d001 and 14e45616e46341cbdde0392cf6e6edff7ac25ea4.

      Files selected for processing: 3

      • apps/server/src/orchestration-v2/Orchestrator.ts
      • apps/server/src/orchestration-v2/ProjectionStore.test.ts
      • apps/server/src/orchestration-v2/ProjectionStore.ts

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

    5. t3dotgg(GitHub profile, opens in a new tab) changed the title

Sign in to join this conversation.