perf(server): sending to a long compacted thread waits about 3x less on its history read #35180
main fromrefs/pull/35180/head·merged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubSending 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:
| Read | Needs |
|---|---|
latestExecutedRun, usageLimitBlockedRun, restart staleness | the run that ended last. A held queue run has a low ordinal and can end after the compaction. |
worktreeSetupPending | any old failed worktree setup |
| idle-compaction busy check | any old run with an open delegated result |
| legacy import handoff | the latest completed run |
modelSwitchSince | the 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
hasOnlyMaintenanceUserMessagesstore 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.
| Thread | main | This PR |
|---|---|---|
| 8,008 runs, compacted, warm | 331 to 351 ms | 106 to 112 ms |
| 8,008 runs, compacted, cold cache | 1.33 to 1.49 s | 0.60 to 0.69 s |
| 7,992 runs, never compacted, warm | 313 to 332 ms | 314 to 334 ms |
| Longest real compacted threads (151 to 156 runs), warm | 8.2 to 9.8 ms | 5.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,
modelSwitchSinceresult 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 thatvp run migrate-dev-dbmakes 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.tscovers 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 onmain, 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
maintoo: behavior does not change. vp run check:changedpasses. All 2,690 tests in the 122orchestration-v2test 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
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
Approvability#
Verdict: Approved at
14e4561Macroscope'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).Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Provider Metric Main baseline This PR Impact PR ceiling Codex Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅ Codex Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Codex Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅ Codex Live turn WebSocket decoded 21.5 KiB 21.5 KiB 0 B (0.0%) 24.4 KiB ✅ Codex Live turn messages 1 1 0 (0.0%) 8 ✅ Claude Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅ Claude Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Claude Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅ Claude Live turn WebSocket decoded 21.9 KiB 21.9 KiB 0 B (0.0%) 24.4 KiB ✅ Claude Live turn messages 1 1 0 (0.0%) 8 ✅ Other scenarios#
All 56 scenario measurements match
main.All 56 scenario measurements
Provider Scenario Metric Main baseline This PR Impact PR ceiling Codex short turn resumeWebSocket decoded10.5 KiB 10.5 KiB 0 B (0.0%) 12.7 KiB ✅ Codex short turn resumeWebSocket messages2 2 0 (0.0%) 2 ✅ Codex long turn running, cold openHTTP decoded281.7 KiB 281.7 KiB 0 B (0.0%) 302.7 KiB ✅ Codex long turn running, cold openrows75 75 0 (0.0%) 75 ✅ Codex long turn running, history pageHTTP decoded92.4 KiB 92.4 KiB 0 B (0.0%) 107.4 KiB ✅ Codex long turn running, history pagerows75 75 0 (0.0%) 75 ✅ Codex long turn finished, cold openHTTP decoded381.5 KiB 381.5 KiB 0 B (0.0%) 410.2 KiB ✅ Codex long turn finished, cold openrows151 151 0 (0.0%) 151 ✅ Codex long turn finished, socket cold openWebSocket decoded381.6 KiB 381.6 KiB 0 B (0.0%) 410.2 KiB ✅ Codex long turn finished, socket cold openWebSocket messages2 2 0 (0.0%) 2 ✅ Codex long turn finished, socket cold openrows151 151 0 (0.0%) 151 ✅ Codex long turn finished, work pageHTTP decoded186.2 KiB 186.2 KiB 0 B (0.0%) 200.2 KiB ✅ Codex long turn finished, work pagerows150 150 0 (0.0%) 150 ✅ Codex idle thread resumeWebSocket decoded65 B 65 B 0 B (0.0%) 200 B ✅ Codex idle thread resumeWebSocket messages1 1 0 (0.0%) 1 ✅ Codex fleet shell snapshotHTTP decoded41.0 KiB 41.0 KiB 0 B (0.0%) 53.7 KiB ✅ Codex fleet shell snapshotrows27 27 0 (0.0%) 27 ✅ Codex fleet shell subscribeWebSocket decoded65 B 65 B 0 B (0.0%) 200 B ✅ Codex fleet shell subscribeWebSocket messages1 1 0 (0.0%) 1 ✅ Codex stale shell resumeWebSocket decoded140 B 140 B 0 B (0.0%) 400 B ✅ Codex stale shell resumeWebSocket messages2 2 0 (0.0%) 2 ✅ Codex stale todo list cold openHTTP decoded72.4 KiB 72.4 KiB 0 B (0.0%) 83.0 KiB ✅ Codex stale todo list cold openrows75 75 0 (0.0%) 75 ✅ Codex stale todo list cold opennodes2 2 0 (0.0%) 4 ✅ Codex stale todo list cold opensubagents2 2 0 (0.0%) 4 ✅ Codex stale todo list cold openplans1 1 0 (0.0%) 2 ✅ Codex stale todo list cold openproviderTurns1 1 0 (0.0%) 2 ✅ Codex stale todo list cold opencheckpoints1 1 0 (0.0%) 2 ✅ Claude short turn resumeWebSocket decoded10.6 KiB 10.6 KiB 0 B (0.0%) 12.7 KiB ✅ Claude short turn resumeWebSocket messages2 2 0 (0.0%) 2 ✅ Claude long turn running, cold openHTTP decoded283.3 KiB 283.3 KiB 0 B (0.0%) 302.7 KiB ✅ Claude long turn running, cold openrows75 75 0 (0.0%) 75 ✅ Claude long turn running, history pageHTTP decoded93.4 KiB 93.4 KiB 0 B (0.0%) 107.4 KiB ✅ Claude long turn running, history pagerows75 75 0 (0.0%) 75 ✅ Claude long turn finished, cold openHTTP decoded384.2 KiB 384.2 KiB 0 B (0.0%) 410.2 KiB ✅ Claude long turn finished, cold openrows151 151 0 (0.0%) 151 ✅ Claude long turn finished, socket cold openWebSocket decoded384.3 KiB 384.3 KiB 0 B (0.0%) 410.2 KiB ✅ Claude long turn finished, socket cold openWebSocket messages2 2 0 (0.0%) 2 ✅ Claude long turn finished, socket cold openrows151 151 0 (0.0%) 151 ✅ Claude long turn finished, work pageHTTP decoded188.1 KiB 188.1 KiB 0 B (0.0%) 200.2 KiB ✅ Claude long turn finished, work pagerows150 150 0 (0.0%) 150 ✅ Claude idle thread resumeWebSocket decoded66 B 66 B 0 B (0.0%) 200 B ✅ Claude idle thread resumeWebSocket messages1 1 0 (0.0%) 1 ✅ Claude fleet shell snapshotHTTP decoded41.6 KiB 41.6 KiB 0 B (0.0%) 53.7 KiB ✅ Claude fleet shell snapshotrows27 27 0 (0.0%) 27 ✅ Claude fleet shell subscribeWebSocket decoded66 B 66 B 0 B (0.0%) 200 B ✅ Claude fleet shell subscribeWebSocket messages1 1 0 (0.0%) 1 ✅ Claude stale shell resumeWebSocket decoded140 B 140 B 0 B (0.0%) 400 B ✅ Claude stale shell resumeWebSocket messages2 2 0 (0.0%) 2 ✅ Claude stale todo list cold openHTTP decoded73.4 KiB 73.4 KiB 0 B (0.0%) 83.0 KiB ✅ Claude stale todo list cold openrows75 75 0 (0.0%) 75 ✅ Claude stale todo list cold opennodes2 2 0 (0.0%) 4 ✅ Claude stale todo list cold opensubagents2 2 0 (0.0%) 4 ✅ Claude stale todo list cold openplans1 1 0 (0.0%) 2 ✅ Claude stale todo list cold openproviderTurns1 1 0 (0.0%) 2 ✅ Claude stale todo list cold opencheckpoints1 1 0 (0.0%) 2 ✅ Baseline:
476fc9b· PR result:14e4561· Source CI: successScenario 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.
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.tsProjectionRecordFiltercan 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.tsOrdinary 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.tsIntegration 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 endPossibly related issues
- pingdotgg/t3code#29468 — This change narrows the
message.dispatchhistory read by the latest completed compaction. It does not change queued-run edits or promote-to-steer reads.
Possibly related PRs
- pingdotgg/t3code#34641 — Adds overlapping compaction-filtered projection reads for message dispatch.
- pingdotgg/t3code#33000 — Adds the same filter and related dispatch behavior for compacted threads.
Suggested reviewers:
juliusmarminge,yash-singh1,maria-rcks - pingdotgg/t3code#29468 — This change narrows the
Merge Risk:
Minimal · up to14e45-
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:
4-
Check name Status Explanation Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Title check 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.d… Description check 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… 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
#29468and 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-
- Source mode: Auto
- Repository: pingdotgg/t3code
- Branch: takeover/34641-command-read-window
- File: .coderabbit.config.ts
- Commit: 14e4561
- Layers: Repository TS config
- Review profile: CHILL
- Plan: Team
- Run ID:
1b81ab4a-b9b6-49cd-b018-d7f806937643
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.tsapps/server/src/orchestration-v2/ProjectionStore.test.tsapps/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.
-
-
t3dotgg(GitHub profile, opens in a new tab) changed the title
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed