The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 19:45 UTC
perf(server): sending to a thread with thousands of finished subagents reads about half as much (#35178)
Refs #29468. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Theo Browne committed
Nov 12, 2026 at 04:04 UTC
5e2438ce94644653dfea926d31c35a87a09632e2
4 files changed
+186
-12
apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+21
@@ -844,6 +844,21 @@ it.effect(
844
(yield* Effect.exit(projections.getThreadRecords(threadId, ["contextTransfers"])))._tag,
845
"Failure",
846
);
847
+ // A delegated task that ended and handed its result over.
848
+ yield* sql`INSERT INTO orchestration_v2_projection_subagents
849
+ (subagent_id, thread_id, run_id, parent_node_id, provider, provider_thread_id,
850
+ child_thread_id, origin, status, updated_at, payload_json, completion_delivery_state)
851
+ VALUES ('obsolete-task', ${threadId}, 'old-run', 'old-node', 'codex', 'obsolete-task-thread',
852
+ 'old-child', 'app_owned', 'completed', ${DateTime.formatIso(now)}, '{"obsolete":true}',
853
+ 'delivered')`;
854
+ yield* sql`INSERT INTO orchestration_v2_projection_nodes
855
+ (node_id, thread_id, run_id, root_node_id, kind, status, payload_json)
856
+ VALUES ('obsolete-task', ${threadId}, 'old-run', 'old-node', 'subagent', 'completed',
857
+ '{"obsolete":true}')`;
858
+ yield* sql`INSERT INTO orchestration_v2_projection_provider_threads
859
+ (provider_thread_id, thread_id, provider, status, updated_at, payload_json)
860
+ VALUES ('obsolete-task-thread', 'old-child', 'codex', 'idle', ${DateTime.formatIso(now)},
861
+ '{"obsolete":true}')`;
862
yield* orchestrator.dispatch({
863
type: "message.dispatch",
864
commandId: CommandId.make("dispatch-with-old-history"),
@@ -862,6 +877,12 @@ it.effect(
877
// Deletion reads transfer history separately to find side chats.
878
yield* sql`DELETE FROM orchestration_v2_projection_context_transfers
879
WHERE target_thread_id = ${threadId}`;
880
+ // Archiving reads every task of the thread to stop the ones still running.
881
+ yield* sql`DELETE FROM orchestration_v2_projection_subagents
882
+ WHERE subagent_id = 'obsolete-task'`;
883
+ yield* sql`DELETE FROM orchestration_v2_projection_nodes WHERE node_id = 'obsolete-task'`;
884
+ yield* sql`DELETE FROM orchestration_v2_projection_provider_threads
885
+ WHERE provider_thread_id = 'obsolete-task-thread'`;
886
yield* orchestrator.dispatch({
887
type: "thread.archive",
888
commandId: CommandId.make("archive-with-old-history"),
apps/server/src/orchestration-v2/Orchestrator.ts
+2
@@ -1553,6 +1553,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1553
// Commands here read only undelivered or active tasks; a dispatcher thread keeps
1554
// thousands of delivered ones, each carrying its full prompt.
1555
omitSettledSubagents: true,
1556
+ // No command reads a settled task's node or its child's provider thread.
1557
+ omitSettledSubagentRecords: true,
1558
// Commands act on pending forks and merge-backs only; consumed ones stay unread.
1559
contextTransferStatuses: ["pending"],
1560
},
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+107
-1
@@ -1575,6 +1575,8 @@ const threadWorktreePathsIncludeArchivedOnRequest = Effect.gen(function* () {
1575
1576
const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
1577
const store = yield* ProjectionStore.ProjectionStoreV2;
1578
+ const sql = yield* SqlClient.SqlClient;
1579
+ const sharedSessionId = ProviderSessionId.make("session:settled-subagents:shared");
1580
const now = yield* DateTime.now;
1581
const threadId = yield* addRolledBackRecoveryCandidate("settled-leaf-nodes");
1582
const settledRun = (yield* store.getThreadProjection(threadId)).runs[0]!;
@@ -1785,7 +1787,7 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
1787
createdBy: "agent",
1788
driver,
1789
providerInstanceId,
1788
- providerThreadId: null,
1790
+ providerThreadId: ProviderThreadId.make(`provider-thread:settled-subagents:${name}`),
1791
childThreadId,
1792
nativeTaskRef: null,
1793
prompt: "Do the work",
@@ -1801,6 +1803,110 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
1803
updatedAt: now,
1804
},
1805
});
1806
+ // Each task has a node in this thread and a provider thread in its child thread.
1807
+ yield* store.apply({
1808
+ id: EventId.make(`event:settled-subagents:node:${name}`),
1809
+ type: "node.updated",
1810
+ threadId,
1811
+ runId: run.id,
1812
+ nodeId: id,
1813
+ driver,
1814
+ occurredAt: now,
1815
+ payload: {
1816
+ id,
1817
+ threadId,
1818
+ runId: run.id,
1819
+ rootNodeId: run.rootNodeId!,
1820
+ parentNodeId: run.rootNodeId,
1821
+ kind: "subagent",
1822
+ status,
1823
+ countsForRun: false,
1824
+ providerThreadId: null,
1825
+ providerTurnId: null,
1826
+ nativeItemRef: null,
1827
+ runtimeRequestId: null,
1828
+ checkpointScopeId: null,
1829
+ startedAt: now,
1830
+ completedAt: status === "running" ? null : now,
1831
+ },
1832
+ });
1833
+ yield* store.apply({
1834
+ id: EventId.make(`event:settled-subagents:provider-thread:${name}`),
1835
+ type: "provider-thread.updated",
1836
+ threadId,
1837
+ occurredAt: now,
1838
+ payload: {
1839
+ id: ProviderThreadId.make(`provider-thread:settled-subagents:${name}`),
1840
+ appThreadId: ThreadId.make(`thread:settled-subagents:child:${name}`),
1841
+ driver,
1842
+ providerInstanceId,
1843
+ // One settled task ran in a provider session this thread also uses.
1844
+ providerSessionId: name === "acknowledged" ? sharedSessionId : null,
1845
+ ownerNodeId: null,
1846
+ nativeThreadRef: null,
1847
+ nativeConversationHeadRef: null,
1848
+ lastRunOrdinal: null,
1849
+ handoffIds: [],
1850
+ forkedFrom: null,
1851
+ status: "idle",
1852
+ firstRunOrdinal: null,
1853
+ createdAt: now,
1854
+ updatedAt: now,
1855
+ },
1856
+ });
1857
+ }
1858
+ yield* sql`INSERT INTO orchestration_v2_projection_provider_session_bindings
1859
+ (provider_session_id, thread_id) VALUES (${sharedSessionId}, ${threadId})`;
1860
+ // A command drops the node and the provider thread of each task it drops. The provider thread
1861
+ // in the shared session stays: detaching that session probes it for background work.
1862
+ const kept = ["active-run-delivered", "claimed", "no-delivery", "pending-delivery", "running"];
1863
+ const commandTrace = traceSqlStatements();
1864
+ const commandRecords = yield* store
1865
+ .getThreadRecords(threadId, ["nodes", "providerThreads"], {
1866
+ omitSettledLeafNodes: true,
1867
+ omitSettledSubagentRecords: true,
1868
+ })
1869
+ .pipe(Effect.withTracer(commandTrace.tracer));
1870
+ assert.deepEqual(
1871
+ commandRecords.nodes
1872
+ .flatMap((node) => (node.kind === "subagent" ? [node.id.split(":").at(-1)] : []))
1873
+ .toSorted(),
1874
+ // A subagent node that no task record names stays.
1875
+ [...kept, "settled-subagent"],
1876
+ );
1877
+ assert.deepEqual(
1878
+ commandRecords.providerThreads.map((thread) => thread.id.split(":").at(-1)).toSorted(),
1879
+ ["acknowledged", ...kept],
1880
+ );
1881
+ assert.lengthOf(
1882
+ (yield* store.getThreadRecords(threadId, ["providerThreads"])).providerThreads,
1883
+ subagents.length,
1884
+ );
1885
+ // Both reads stay inside their indexes: no scan of the thread's nodes or provider threads.
1886
+ for (const [table, seeks] of [
1887
+ [
1888
+ "nodes",
1889
+ [
1890
+ "SEARCH node USING INTEGER PRIMARY KEY",
1891
+ "SEARCH settled USING COVERING INDEX",
1892
+ "SEARCH settled_node USING COVERING INDEX",
1893
+ "subagents_settled_idx (thread_id=?)",
1894
+ ],
1895
+ ],
1896
+ [
1897
+ "provider_threads",
1898
+ ["MULTI-INDEX OR", "provider_threads_session_idx (provider_session_id=?)"],
1899
+ ],
1900
+ ] as const) {
1901
+ const query = commandTrace.statements.find((statement) =>
1902
+ statement.includes(`FROM orchestration_v2_projection_${table}`),
1903
+ );
1904
+ assert.isDefined(query);
1905
+ const plan = (yield* sql.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${query}`))
1906
+ .map((row) => row.detail)
1907
+ .join("\n");
1908
+ for (const seek of seeks) assert.include(plan, seek);
1909
+ assert.notInclude(plan, `SCAN orchestration_v2_projection_${table}`);
1910
}
1911
const subagentNames = (records: {
1912
readonly subagents: ReadonlyArray<{ readonly title: string | null }>;
apps/server/src/orchestration-v2/ProjectionStore.ts
+56
-11
@@ -372,6 +372,12 @@ export interface ProjectionRecordFilter {
372
* run is active or `except` names them.
373
*/
374
readonly omitSettledSubagents?: true | { readonly except: ReadonlyArray<NodeId> };
375
+ /**
376
+ * Also skips the provider thread of each subagent that `omitSettledSubagents: true` skips, and
377
+ * with `omitSettledLeafNodes` its node. A provider thread that shares a provider session with
378
+ * the thread stays.
379
+ */
380
+ readonly omitSettledSubagentRecords?: boolean;
381
}
382
383
// A fork inherits its source's history before it has messages of its own,
@@ -2100,6 +2106,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2106
)
2107
)`;
2108
2109
+ // The settled index holds every column the check reads, so the settled tasks are dropped
2110
+ // there and their rows, each with a full prompt, stay unread.
2111
+ const unsettledSubagentRowids = (threadId: ThreadId) => sql`
2112
+ SELECT candidate.rowid
2113
+ FROM orchestration_v2_projection_subagents AS candidate
2114
+ INDEXED BY orchestration_v2_projection_subagents_settled_idx
2115
+ WHERE candidate.thread_id = ${threadId}
2116
+ AND NOT ${settledSubagent(sql`candidate`)}
2117
+ `;
2118
+
2119
// A todo list is never closed when its run ends, so a long thread holds an active one per
2120
// old run. Counting those as live work drags each run's nodes and subagents into every window.
2121
const todoListOfSettledRun = (row: Statement.Fragment) => sql`(
@@ -3740,6 +3756,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3756
AND (kind IN ('tool_call', 'assistant_message', 'reasoning')
3757
AND status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
3758
AND run_id IS NOT NULL) = 0
3759
+ ${
3760
+ filter.omitSettledSubagentRecords === true
3761
+ ? // A subagent's node shares its id. Both indexes hold every column
3762
+ // read here, so no settled subagent or node row is fetched.
3763
+ sql`EXCEPT
3764
+ SELECT settled_node.rowid
3765
+ FROM orchestration_v2_projection_subagents AS settled
3766
+ INDEXED BY orchestration_v2_projection_subagents_thread_idx
3767
+ CROSS JOIN orchestration_v2_projection_nodes AS settled_node
3768
+ ON settled_node.node_id = settled.subagent_id
3769
+ WHERE settled.thread_id = ${threadId}
3770
+ AND settled.rowid NOT IN (${unsettledSubagentRowids(threadId)})`
3771
+ : sql``
3772
+ }
3773
UNION ALL
3774
SELECT rowid FROM orchestration_v2_projection_nodes
3775
WHERE thread_id = ${threadId}
@@ -3782,15 +3812,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3812
${filter?.subagentRunIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentRunIds)}))`}
3813
${
3814
filter?.omitSettledSubagents === true
3785
- ? // The settled index holds every column this check reads, so the settled
3786
- // tasks are dropped there and their rows, each with a full prompt, stay unread.
3787
- sql`AND subagent.rowid IN (
3788
- SELECT candidate.rowid
3789
- FROM orchestration_v2_projection_subagents AS candidate
3790
- INDEXED BY orchestration_v2_projection_subagents_settled_idx
3791
- WHERE candidate.thread_id = ${threadId}
3792
- AND NOT ${settledSubagent(sql`candidate`)}
3793
- )`
3815
+ ? sql`AND subagent.rowid IN (${unsettledSubagentRowids(threadId)})`
3816
: filter?.omitSettledSubagents !== undefined
3817
? sql`AND (
3818
subagent_id IN (SELECT value FROM json_each(${encodeIdList(filter.omitSettledSubagents.except)}))
@@ -3840,12 +3862,35 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3862
FROM orchestration_v2_projection_provider_threads
3863
WHERE (thread_id = ${threadId}
3864
OR owner_node_id IN (${nodesOwningProviderThreads(threadId)})
3843
- OR provider_thread_id IN (
3865
+ OR ${
3866
+ filter?.omitSettledSubagentRecords === true
3867
+ ? // A dispatcher thread holds thousands of settled tasks. Their provider
3868
+ // threads belong to the child threads; only a session this thread also
3869
+ // uses is probed for background work when the thread detaches it.
3870
+ sql`provider_thread_id IN (
3871
+ SELECT provider_thread_id
3872
+ FROM orchestration_v2_projection_subagents
3873
+ WHERE rowid IN (${unsettledSubagentRowids(threadId)})
3874
+ AND provider_thread_id IS NOT NULL
3875
+ )
3876
+ OR (provider_session_id IN (
3877
+ SELECT provider_session_id
3878
+ FROM orchestration_v2_projection_provider_session_bindings
3879
+ WHERE thread_id = ${threadId}
3880
+ )
3881
+ AND EXISTS (
3882
+ SELECT 1 FROM orchestration_v2_projection_subagents AS owner
3883
+ WHERE owner.provider_thread_id =
3884
+ orchestration_v2_projection_provider_threads.provider_thread_id
3885
+ AND owner.thread_id = ${threadId}
3886
+ ))`
3887
+ : sql`provider_thread_id IN (
3888
SELECT provider_thread_id
3889
FROM orchestration_v2_projection_subagents
3890
WHERE thread_id = ${threadId}
3891
AND provider_thread_id IS NOT NULL
3848
- ))
3892
+ )`
3893
+ })
3894
${filter?.providerThreadIds === undefined ? sql`` : sql`AND provider_thread_id IN (SELECT value FROM json_each(${encodeIdList(filter.providerThreadIds)}))`}
3895
ORDER BY COALESCE(first_run_ordinal, 0), provider_thread_id ASC
3896
`