perf(server): sending to a thread with thousands of finished subagents reads about half as much #35178
main fromrefs/pull/35178/head·merged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubProblem#
Every command reads its thread before it plans. On a thread that ran thousands of delegated tasks, a send still loaded the node of every finished task and the provider thread of every finished child thread.
Fix#
The command read gets one new store filter, omitSettledSubagentRecords. No other reader changes. There is no migration.
- Provider threads. The read skips the provider thread of each settled subagent. A provider thread that shares a provider session with the thread stays, because a session detach probes it for background work.
- Nodes. The read skips the node of each settled subagent. It finds those nodes through two indexes that exist already (
subagents_thread_idxand the node primary key), so it fetches no settled subagent row and no node row.
A subagent is settled when it ended, its result was acknowledged, delivered or disposed, and its run is not active. A subagent with an undelivered result, a running subagent, and every provider-native subagent stay, with their nodes and provider threads.
Messages and user_message turn items are not bounded here. Several command paths scan all user messages by payload fields, so a safe bound needs its own change.
Why no command needs a dropped record#
Only readCommandProjection passes the filter. Interrupt, stop, rewind, fork, task cancel, task completion, background settle, archive and delete use their own reads, which did not change.
I hid nodes and providerThreads in the type of the command read and ran the type checker. It listed every place that takes them (40 sites, in dispatchMessage, steer, startNextQueuedRun, and the cohort disposal that interrupt, stop, archive and delete call):
- Nodes are read only by the root node of a run. A root node has kind
root_turnand is never the node of a subagent. - Provider threads are read by the provider thread of a run, by the active provider thread, by root provider threads of the thread (
appThreadIdis the thread), and by a session bound to the thread. The first three always havethread_idequal to the thread, which the read keeps. The fourth is the session rule above.
Checked against a copy of a real 9 GB database, all 3,952 threads: the filter drops 3,105 nodes and 2,976 provider threads. None is a run root, the provider thread of a run, or an active provider thread. No dropped node has a child node, a pending request, an unsettled child task, a checkpoint scope, or an owned provider thread. No dropped provider thread has background tasks.
Measured#
The command read through the real store, on a copy of a real database. Warm time is the median of 8 reads.
| Thread | Finished tasks | Rows before | Rows after | Warm ms before | Warm ms after |
|---|---|---|---|---|---|
| Real | 377 | 1,535 | 781 | 28.7 | 11.9 |
| Real | 171 | 417 | 75 | 10.4 | 6.5 |
| Real | 150 | 1,088 | 788 | 22.6 | 14.4 |
| Real thread, tasks cloned 12 times | 4,524 | 9,829 | 781 | 92 | 19.5 |
On the 4,524 task thread, provider threads went from 4.5 MB to 0 and nodes from 4.3 MB to 0.1 MB. Every other collection is identical before and after on all four threads.
The first version of this PR measured two threads of a 26 GB database (14,847 rows to 7,560, and 7,628 to 3,973). That database is not on the machine I used, so I did not measure those threads again.
Why the migration is gone#
The first version added a partial index on the nodes table. I timed that index build on a fresh copy of the 9 GB database: 26.4 s with a cold page cache, 0.2 s warm. On the 26 GB database the same table scan took 69 s for migration 083. The desktop shows only its splash for that time, and after 3 minutes it reports a failed start (#35056 is not merged).
The existing indexes do the same work. On the 4,524 task thread the read takes 19.5 ms warm without the new index and 17.1 ms with it. So this PR adds no migration, no first-start cost, and nothing that a downgrade to Stable can meet.
Tests#
ProjectionStore.test.ts, "command reads skip finished leaf nodes of finished runs": asserts the kept and dropped nodes and provider threads, the shared session rule, and both query plans.Orchestrator.control-reads.test.ts, "dispatches metadata, queue resume and request controls without hydrating unrelated history": holds a finished task with unreadable rows.
Both fail with the source files of main and pass here.
Real server pass#
vp run dev on this branch with real data and the Replay (dev) provider. One thread delegated six tasks: four finished and were delivered, one was cancelled while it waited on an approval, one finished while the parent turn ran. On that thread: send, send during an approval, queue, queue edit, steer, promote a queued message to a steer, restart, Stop, read a finished task's result, model switch, a send with a fresh provider session, settle, unsettle, archive and unarchive. Each command succeeded. The server log has no error and no "not found".
Overlap with #35180#
#35180 changes the same filter type and the same node query. A test merge has two text conflicts in ProjectionStore.ts, and both resolve by keeping both sides. With both filters on, six real compacted threads return exactly the intersection of the two node sets and keep every run root, and the 144 tests of both PRs pass. Either merge order works; the second one resolves the two conflicts.
Not verified#
- The two threads of the 26 GB database.
- A real provider. The server pass used the Replay provider only.
- A provider switch between two providers, and a session shared by a parent thread and its child (covered by the unit test only).
Stacked on #35130, which is merged. Refs #29468.
First commit reviewed with sol-loop: 1 pass with GPT-6.1 Sol. Sol verdict: approved. The later commits (merge of main, removal of the migration) were not in that review.
Created with Claude Fable 5.2 in Claude 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:M
Approvability#
Verdict: Approved at
498f449Macroscope's review found this PR approvable — This reduces command-time reads of finished subagent records while retaining active work and shared-session provider threads. It changes neither stored history nor product defaults, with no obvious material risk requiring human judgment.
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 ✅ Baseline:
d412871· PR result:498f449· 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
-
Projection reads can now omit records associated with settled subagents. Command projection reads enable this filtering, and tests cover retained nodes and provider threads, indexed queries, and control reads with completed subagent history.
Changes: Settled Subagent Record Filtering
Layer / File(s) Summary Projection record filtering
apps/server/src/orchestration-v2/ProjectionStore.ts,apps/server/src/orchestration-v2/ProjectionStore.test.tsThe projection filter can exclude nodes and provider threads associated with settled subagents. It retains provider threads that share a provider session with the thread. Tests check the retained records and indexed queries. Command projection integration
apps/server/src/orchestration-v2/Orchestrator.ts,apps/server/src/orchestration-v2/Orchestrator.control-reads.test.tsCommand projection reads enable settled-subagent record filtering. Control-read test fixtures add completed subagent records and remove them before archiving. Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Refactor
Possibly related issues
- Issue 29468: The change reduces command-path reads of settled subagent records, but the issue also identifies other history queries that this change does not address.
Suggested reviewers:
juliusmarminge,yash-singh1
Merge Risk:
Minimal · up to498f4-
The change reduces how much data command reads load for threads with many finished subagents, and the settled-subagent filtering is limited to command reads. No concrete merge-blocking risk was found in the supplied changes.
Pre-merge checks:
4-
Check name Status Explanation Title check The title names a performance improvement for sending to a thread with many finished subagents, and the change does exactly that. It adds an omitSettledSubagentRecords filter so the command read skips… 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. Description check The description follows the repository template closely. It covers Problem, Fix (Change), Scope (via the Problem and linked references), Verification, and an agent attribution line. The problem statem… Full details: Title check
-
Explanation
The title names a performance improvement for sending to a thread with many finished subagents, and the change does exactly that. It adds an omitSettledSubagentRecords filter so the command read skips settled subagent nodes and provider threads. The title is specific, follows the conventional commit form, and states the main effect. It says 'about half as much', which matches the reported row and byte reductions (14,847 to 7,560 rows on thread A; 77.8 MB to 36.7 MB).
Full details: Description check
-
Explanation
The description follows the repository template closely. It covers Problem, Fix (Change), Scope (via the Problem and linked references), Verification, and an agent attribution line. The problem statement is clear and tied to measured behavior. It links
#29468as the reference and explains why no issue-scope concerns apply, though it does not explicitly frame the change as a bug fix with a linked bug issue. Verification is detailed: focused tests, a real-server pass, benchmark tables, and stated unverified items. Section headings differ slightly from the template (the template's 'Change' is written as 'Fix'), and the 'Scope' section lacks an explicit statement that it is a bug fix. These are minor gaps, so the description is mostly complete.
-
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: perf/command-read-skips-finished-subagents
- File: .coderabbit.config.ts
- Commit: 498f449
- Layers: Repository TS config
- Review profile: CHILL
- Plan: Team
- Run ID:
5a47c581-ab48-41de-aa2b-7b117d89c578
Commits-
Reviewing files that changed from the base of the PR and between 50402754efba2a6ae84f40d5fd5150074a22b070 and 498f449d2061fa8860d324a7fe78a94c8b3e88b3.
Files selected for processing: 2-
apps/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.
-
-
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