Skip to content

Commit 5040275

Browse files
t3dotggclaude
andcommitted
perf(server): sending to a dispatcher thread skips its finished subagents' records
Every command reads its thread before it plans. On a thread that ran 4,000 delegated tasks, that read still loaded the node of every finished task and the provider thread of every finished child: 7,300 of 14,800 rows. - Commands skip the provider thread of each settled subagent. A provider thread that shares a provider session with the thread stays, because detaching that session probes it. - Commands skip the ended node of each settled subagent, through a new partial index (migration 084). - The provider-thread read no longer fetches every subagent row to find its provider thread id. Refs #29468. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent a94846d commit 5040275

7 files changed

Lines changed: 215 additions & 12 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,21 @@ it.effect(
844844
(yield* Effect.exit(projections.getThreadRecords(threadId, ["contextTransfers"])))._tag,
845845
"Failure",
846846
);
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}')`;
847862
yield* orchestrator.dispatch({
848863
type: "message.dispatch",
849864
commandId: CommandId.make("dispatch-with-old-history"),
@@ -862,6 +877,12 @@ it.effect(
862877
// Deletion reads transfer history separately to find side chats.
863878
yield* sql`DELETE FROM orchestration_v2_projection_context_transfers
864879
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'`;
865886
yield* orchestrator.dispatch({
866887
type: "thread.archive",
867888
commandId: CommandId.make("archive-with-old-history"),

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
15531553
// Commands here read only undelivered or active tasks; a dispatcher thread keeps
15541554
// thousands of delivered ones, each carrying its full prompt.
15551555
omitSettledSubagents: true,
1556+
// No command reads a settled task's node or its child's provider thread.
1557+
omitSettledSubagentRecords: true,
15561558
// Commands act on pending forks and merge-backs only; consumed ones stay unread.
15571559
contextTransferStatuses: ["pending"],
15581560
},

‎apps/server/src/orchestration-v2/ProjectionStore.test.ts‎

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,8 @@ const threadWorktreePathsIncludeArchivedOnRequest = Effect.gen(function* () {
15751575

15761576
const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
15771577
const store = yield* ProjectionStore.ProjectionStoreV2;
1578+
const sql = yield* SqlClient.SqlClient;
1579+
const sharedSessionId = ProviderSessionId.make("session:settled-subagents:shared");
15781580
const now = yield* DateTime.now;
15791581
const threadId = yield* addRolledBackRecoveryCandidate("settled-leaf-nodes");
15801582
const settledRun = (yield* store.getThreadProjection(threadId)).runs[0]!;
@@ -1785,7 +1787,7 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
17851787
createdBy: "agent",
17861788
driver,
17871789
providerInstanceId,
1788-
providerThreadId: null,
1790+
providerThreadId: ProviderThreadId.make(`provider-thread:settled-subagents:${name}`),
17891791
childThreadId,
17901792
nativeTaskRef: null,
17911793
prompt: "Do the work",
@@ -1801,6 +1803,108 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
18011803
updatedAt: now,
18021804
},
18031805
});
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+
kept,
1875+
);
1876+
assert.deepEqual(
1877+
commandRecords.providerThreads.map((thread) => thread.id.split(":").at(-1)).toSorted(),
1878+
["acknowledged", ...kept],
1879+
);
1880+
assert.lengthOf(
1881+
(yield* store.getThreadRecords(threadId, ["providerThreads"])).providerThreads,
1882+
subagents.length,
1883+
);
1884+
// Both reads stay inside their indexes: no scan of the thread's nodes or provider threads.
1885+
for (const [table, seeks] of [
1886+
[
1887+
"nodes",
1888+
[
1889+
"SEARCH node USING INTEGER PRIMARY KEY",
1890+
"nodes_ended_subagent_idx (thread_id=?)",
1891+
"subagents_settled_idx (thread_id=?)",
1892+
],
1893+
],
1894+
[
1895+
"provider_threads",
1896+
["MULTI-INDEX OR", "provider_threads_session_idx (provider_session_id=?)"],
1897+
],
1898+
] as const) {
1899+
const query = commandTrace.statements.find((statement) =>
1900+
statement.includes(`FROM orchestration_v2_projection_${table}`),
1901+
);
1902+
assert.isDefined(query);
1903+
const plan = (yield* sql.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${query}`))
1904+
.map((row) => row.detail)
1905+
.join("\n");
1906+
for (const seek of seeks) assert.include(plan, seek);
1907+
assert.notInclude(plan, `SCAN orchestration_v2_projection_${table}`);
18041908
}
18051909
const subagentNames = (records: {
18061910
readonly subagents: ReadonlyArray<{ readonly title: string | null }>;

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,12 @@ export interface ProjectionRecordFilter {
372372
* run is active or `except` names them.
373373
*/
374374
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 ended node. A provider thread that shares a provider session
378+
* with the thread stays.
379+
*/
380+
readonly omitSettledSubagentRecords?: boolean;
375381
}
376382

377383
// A fork inherits its source's history before it has messages of its own,
@@ -2098,6 +2104,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
20982104
)
20992105
)`;
21002106

2107+
// The settled index holds every column the check reads, so the settled tasks are dropped
2108+
// there and their rows, each with a full prompt, stay unread.
2109+
const unsettledSubagentRowids = (threadId: ThreadId) => sql`
2110+
SELECT candidate.rowid
2111+
FROM orchestration_v2_projection_subagents AS candidate
2112+
INDEXED BY orchestration_v2_projection_subagents_settled_idx
2113+
WHERE candidate.thread_id = ${threadId}
2114+
AND NOT ${settledSubagent(sql`candidate`)}
2115+
`;
2116+
21012117
// A todo list is never closed when its run ends, so a long thread holds an active one per
21022118
// old run. Counting those as live work drags each run's nodes and subagents into every window.
21032119
const todoListOfSettledRun = (row: Statement.Fragment) => sql`(
@@ -3738,6 +3754,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
37383754
AND (kind IN ('tool_call', 'assistant_message', 'reasoning')
37393755
AND status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
37403756
AND run_id IS NOT NULL) = 0
3757+
${
3758+
filter.omitSettledSubagentRecords === true
3759+
? // Drops the ended subagent nodes inside migration 084's index, whose
3760+
// predicate this must match. The last branch brings back the node of
3761+
// each subagent the read keeps.
3762+
sql`EXCEPT
3763+
SELECT rowid FROM orchestration_v2_projection_nodes
3764+
INDEXED BY orchestration_v2_projection_nodes_ended_subagent_idx
3765+
WHERE thread_id = ${threadId}
3766+
AND kind = 'subagent'
3767+
AND status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
3768+
AND run_id IS NOT NULL`
3769+
: sql``
3770+
}
37413771
UNION ALL
37423772
SELECT rowid FROM orchestration_v2_projection_nodes
37433773
WHERE thread_id = ${threadId}
@@ -3748,6 +3778,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
37483778
)
37493779
AND kind IN ('tool_call', 'assistant_message', 'reasoning')
37503780
AND status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
3781+
${
3782+
filter.omitSettledSubagentRecords === true
3783+
? sql`UNION ALL
3784+
SELECT kept.rowid
3785+
FROM orchestration_v2_projection_subagents AS owner
3786+
CROSS JOIN orchestration_v2_projection_nodes AS kept
3787+
ON kept.node_id = owner.subagent_id
3788+
WHERE owner.rowid IN (${unsettledSubagentRowids(threadId)})`
3789+
: sql``
3790+
}
37513791
)`
37523792
: sql`${threadColumn(filter?.nodeIds, "node.thread_id")} = ${threadId}`
37533793
}
@@ -3780,15 +3820,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
37803820
${filter?.subagentRunIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentRunIds)}))`}
37813821
${
37823822
filter?.omitSettledSubagents === true
3783-
? // The settled index holds every column this check reads, so the settled
3784-
// tasks are dropped there and their rows, each with a full prompt, stay unread.
3785-
sql`AND subagent.rowid IN (
3786-
SELECT candidate.rowid
3787-
FROM orchestration_v2_projection_subagents AS candidate
3788-
INDEXED BY orchestration_v2_projection_subagents_settled_idx
3789-
WHERE candidate.thread_id = ${threadId}
3790-
AND NOT ${settledSubagent(sql`candidate`)}
3791-
)`
3823+
? sql`AND subagent.rowid IN (${unsettledSubagentRowids(threadId)})`
37923824
: filter?.omitSettledSubagents !== undefined
37933825
? sql`AND (
37943826
subagent_id IN (SELECT value FROM json_each(${encodeIdList(filter.omitSettledSubagents.except)}))
@@ -3838,12 +3870,35 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
38383870
FROM orchestration_v2_projection_provider_threads
38393871
WHERE (thread_id = ${threadId}
38403872
OR owner_node_id IN (${nodesOwningProviderThreads(threadId)})
3841-
OR provider_thread_id IN (
3873+
OR ${
3874+
filter?.omitSettledSubagentRecords === true
3875+
? // A dispatcher thread holds thousands of settled tasks. Their provider
3876+
// threads belong to the child threads; only a session this thread also
3877+
// uses is probed for background work when the thread detaches it.
3878+
sql`provider_thread_id IN (
3879+
SELECT provider_thread_id
3880+
FROM orchestration_v2_projection_subagents
3881+
WHERE rowid IN (${unsettledSubagentRowids(threadId)})
3882+
AND provider_thread_id IS NOT NULL
3883+
)
3884+
OR (provider_session_id IN (
3885+
SELECT provider_session_id
3886+
FROM orchestration_v2_projection_provider_session_bindings
3887+
WHERE thread_id = ${threadId}
3888+
)
3889+
AND EXISTS (
3890+
SELECT 1 FROM orchestration_v2_projection_subagents AS owner
3891+
WHERE owner.provider_thread_id =
3892+
orchestration_v2_projection_provider_threads.provider_thread_id
3893+
AND owner.thread_id = ${threadId}
3894+
))`
3895+
: sql`provider_thread_id IN (
38423896
SELECT provider_thread_id
38433897
FROM orchestration_v2_projection_subagents
38443898
WHERE thread_id = ${threadId}
38453899
AND provider_thread_id IS NOT NULL
3846-
))
3900+
)`
3901+
})
38473902
${filter?.providerThreadIds === undefined ? sql`` : sql`AND provider_thread_id IN (SELECT value FROM json_each(${encodeIdList(filter.providerThreadIds)}))`}
38483903
ORDER BY COALESCE(first_run_ordinal, 0), provider_thread_id ASC
38493904
`

‎apps/server/src/persistence/Migrations.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import Migration0080 from "./Migrations/080_ScheduledTaskThreadMode.ts";
8686
import Migration0081 from "./Migrations/081_ProjectionMessageSearchText.ts";
8787
import Migration0082 from "./Migrations/082_TurnItemConversationIndex.ts";
8888
import Migration0083 from "./Migrations/083_CommandReadIndexes.ts";
89+
import Migration0084 from "./Migrations/084_EndedSubagentNodeIndex.ts";
8990

9091
const migrationEntries = [
9192
[1, "OrchestrationEvents", Migration0001],
@@ -172,6 +173,7 @@ const migrationEntries = [
172173
[81, "ProjectionMessageSearchText", Migration0081],
173174
[82, "TurnItemConversationIndex", Migration0082],
174175
[83, "CommandReadIndexes", Migration0083],
176+
[84, "EndedSubagentNodeIndex", Migration0084],
175177
] as const;
176178

177179
export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as Effect from "effect/Effect";
2+
import * as SqlClient from "effect/sql/SqlClient";
3+
4+
export default Effect.gen(function* () {
5+
const sql = yield* SqlClient.SqlClient;
6+
7+
// A dispatcher thread keeps one node per delegated task it ever ran. This index lists the
8+
// ended ones, so a command read can drop them without fetching each node to check its kind.
9+
// The omitSettledSubagentRecords read in ProjectionStore must spell this predicate the same way.
10+
yield* sql`
11+
CREATE INDEX IF NOT EXISTS orchestration_v2_projection_nodes_ended_subagent_idx
12+
ON orchestration_v2_projection_nodes(thread_id)
13+
WHERE kind = 'subagent'
14+
AND status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
15+
AND run_id IS NOT NULL
16+
`;
17+
});

‎apps/server/src/persistence/reconcileV2PreviewMigration.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ describe("V2 preview upgrade", () => {
6363
[81, "ProjectionMessageSearchText"],
6464
[82, "TurnItemConversationIndex"],
6565
[83, "CommandReadIndexes"],
66+
[84, "EndedSubagentNodeIndex"],
6667
]);
6768
assert.deepStrictEqual(yield* runMigrations(), []);
6869
assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -169,6 +170,7 @@ describe("V2 preview upgrade", () => {
169170
[81, "ProjectionMessageSearchText"],
170171
[82, "TurnItemConversationIndex"],
171172
[83, "CommandReadIndexes"],
173+
[84, "EndedSubagentNodeIndex"],
172174
]);
173175
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
174176
);

0 commit comments

Comments
 (0)