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, 07:15 UTC
perf(server): task follow-ups no longer read the whole parent thread (#38867)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Navid Nadali committed
Nov 19, 2026 at 05:49 UTC
c80d450d20d6f88f98105b9e67ae47f5b5113fde
4 files changed
+80
-11
apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+38
-5
@@ -1943,7 +1943,7 @@ it.effect.each([false, true])("rejects thread.create for an existing id (deleted
1943
}).pipe(Effect.provide(layerTest)),
1944
);
1945
1946
-it.effect("starts and finishes a delegated task without reading the parent's history", () =>
1946
+it.effect("starts, finishes and follows up a task without reading the parent's history", () =>
1947
Effect.gen(function* () {
1948
const orchestrator = yield* Orchestrator.OrchestratorV2;
1949
const projections = yield* ProjectionStore.ProjectionStoreV2;
@@ -2019,10 +2019,11 @@ it.effect("starts and finishes a delegated task without reading the parent's his
2019
});
2020
}
2021
2022
- // Keep the statements of the task start and finish operations, not of the recovery around them.
2022
+ // Keep the statements of the task start, finish and follow-up, not of the recovery around them.
2023
const taskSpans = [
2024
"orchestrationV2.dispatch.delegatedTaskRequest",
2025
"Orchestrator.finalizeAppOwnedSubagent",
2026
+ "test.followUp",
2027
];
2028
const statements: Array<readonly [string, ReadonlyArray<unknown>]> = [];
2029
const recordStatements = Effect.provideService(
@@ -2103,9 +2104,38 @@ it.effect("starts and finishes a delegated task without reading the parent's his
2104
["completed", "claimed", "completed", "completed", [task.id]],
2105
);
2106
2106
- // Start and finish read the task's own records, so their VM steps, which are deterministic,
2107
- // must stay flat on a copy with a long parent history. Left out: the counts that number the
2108
- // task and its wake, which walk one index.
2107
+ // A person's follow-up to the finished child binds a new task to the parent's running run.
2108
+ yield* orchestrator
2109
+ .dispatch({
2110
+ type: "message.dispatch",
2111
+ commandId: CommandId.make("follow-up-long-parent"),
2112
+ threadId: childThreadId,
2113
+ messageId: MessageId.make("message:long-parent:follow-up"),
2114
+ text: "One more thing",
2115
+ attachments: [],
2116
+ dispatchMode: { type: "start_immediately" },
2117
+ createdBy: "user",
2118
+ creationSource: "web",
2119
+ })
2120
+ .pipe(Effect.withSpan("test.followUp"), recordStatements);
2121
+ const followedUp = yield* projections.getThreadRecords(parentThreadId, [
2122
+ "subagents",
2123
+ "turnItems",
2124
+ ]);
2125
+ const followUp = followedUp.subagents.find((candidate) => candidate.id !== task.id);
2126
+ assert.deepEqual(
2127
+ [
2128
+ followUp?.runId,
2129
+ followedUp.turnItems.find(
2130
+ (item) => item.type === "subagent" && item.subagentId === followUp?.id,
2131
+ )?.providerTurnId,
2132
+ ],
2133
+ [parentRun.id, providerTurnId],
2134
+ );
2135
+
2136
+ // Start, finish and follow-up read the task's own records and the run it binds to, so their VM
2137
+ // steps, which are deterministic, must stay flat on a copy with a long parent history. Left out:
2138
+ // the counts that number the task and its wake, which walk one index.
2139
const copyPath = "file:/delegated-task-reads?vfs=memdb";
2140
using copy = new NodeSqlite.DatabaseSync(copyPath);
2141
yield* sql`VACUUM INTO ${copyPath}`;
@@ -2146,6 +2176,9 @@ it.effect("starts and finishes a delegated task without reading the parent's his
2176
"target_thread_id",
2177
]);
2178
addHistory("runs", "run_id");
2179
+ // The history's runs have ended; only the parent's own run is still going.
2180
+ copy.exec(`UPDATE orchestration_v2_projection_runs
2181
+ SET status = 'completed', completed_at = requested_at WHERE run_id LIKE '%:history:%'`);
2182
addHistory("run_attempts", "attempt_id", ["run_id"]);
2183
addHistory("messages", "message_id");
2184
addHistory("provider_turns", "provider_turn_id", ["run_attempt_id"]);
apps/server/src/orchestration-v2/Orchestrator.ts
+8
-2
@@ -9844,7 +9844,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
9844
.filter((row) => row.id !== run.id && row.delegatedTaskId !== undefined)
9845
.toSorted((a, b) => b.ordinal - a.ordinal)[0]?.delegatedTaskId;
9846
const parent = yield* projectionStore
9847
- .getThreadRecords(parentThreadId, ["runs", "subagents", "providerTurns", "attempts"], {
9847
+ .getThreadRecords(parentThreadId, ["runs", "subagents"], {
9848
+ // Every run followUpOwnerRun can pick, so a long parent's history stays unread.
9849
+ runStatuses: ["preparing", "starting", "running", "waiting"],
9850
+ includeLatestExecutedRun: true,
9851
subagentIds: [
9852
...(child.forkedFrom?.type === "node" ? [child.forkedFrom.nodeId] : []),
9853
...(latestFollowupId === undefined ? [] : [latestFollowupId]),
@@ -9881,6 +9884,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
9884
}
9885
const owner = followUpOwnerRun(parent.runs);
9886
if (owner === null || owner.rootNodeId === null) return;
9887
+ const ownerRecords = yield* projectionStore
9888
+ .getRunProviderRecords(parentThreadId, owner.id, owner.rootNodeId)
9889
+ .pipe(mapDispatchError(command));
9890
const now = message.createdAt;
9891
const adapter = yield* providerAdapters.get(run.providerInstanceId);
9892
const taskId = idAllocator.derive.delegatedTaskNode({ commandId: command.commandId });
@@ -9930,7 +9936,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
9936
runId: owner.id,
9937
nodeId: taskId,
9938
providerThreadId: owner.providerThreadId,
9933
- providerTurnId: providerTurnForRun(parent, owner)?.id ?? null,
9939
+ providerTurnId: providerTurnForRun(ownerRecords, owner)?.id ?? null,
9940
nativeItemRef: null,
9941
parentItemId: null,
9942
ordinal: yield* nextTurnItemOrdinal(parent),
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+3
-1
@@ -729,10 +729,12 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
729
.join("\n");
730
assert.notInclude(runPlan, "orchestration_v2_projection_runs_thread_ordinal_idx");
731
assert.match(runPlan, /SEARCH orchestration_v2_projection_runs USING INDEX \w*status\w*/);
732
- // The newest runs by ordinal, whatever their status, plus older runs the statuses match.
732
+ // The newest runs by ordinal, whatever their status, or the run that ended last, plus older runs
733
+ // the statuses match.
734
for (const [filter, expected] of [
735
[{ latestRunLimit: 2 }, [4, 5]],
736
[{ latestRunLimit: 2, runStatuses: ["rolled_back"] as const }, [1, 4, 5]],
737
+ [{ includeLatestExecutedRun: true, runStatuses: ["rolled_back"] as const }, [1, 4]],
738
] as const) {
739
const traced = traceSqlStatements();
740
const recent = yield* store
apps/server/src/orchestration-v2/ProjectionStore.ts
+31
-3
@@ -365,6 +365,8 @@ export interface ProjectionRecordFilter {
365
readonly includeLatestRun?: true;
366
/** The thread's N highest-ordinal runs, plus any run `runStatuses` matches. Ignores `includeLatestRun`. */
367
readonly latestRunLimit?: number;
368
+ /** Keeps the run `latestExecutedRun` picks even when `runStatuses` excludes it. Ignores `includeLatestRun`. */
369
+ readonly includeLatestExecutedRun?: true;
370
readonly providerTurnThreadIds?: ReadonlyArray<ProviderThreadId>;
371
/** Only the provider turn whose token usage was updated last. */
372
readonly latestProviderTurnUsageOnly?: true;
@@ -3988,13 +3990,18 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3990
(matchesNothing(filter?.runIds) ||
3991
(matchesNothing(filter?.runStatuses) &&
3992
filter?.includeLatestRun !== true &&
3993
+ filter?.includeLatestExecutedRun !== true &&
3994
filter?.latestRunLimit === undefined)))
3995
? Effect.succeed([])
3996
: window === undefined
3997
? sql<PayloadRow>`
3998
SELECT payload_json
3999
FROM orchestration_v2_projection_runs
3997
- WHERE ${threadColumn(filter?.latestRunLimit === undefined ? filter?.runIds : [])} = ${threadId}
4000
+ WHERE ${threadColumn(
4001
+ filter?.latestRunLimit === undefined && filter?.includeLatestExecutedRun !== true
4002
+ ? filter?.runIds
4003
+ : [],
4004
+ )} = ${threadId}
4005
${filter?.runIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.runIds)}))`}
4006
${
4007
filter?.latestRunLimit !== undefined
@@ -4017,12 +4024,33 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4024
ORDER BY ordinal ASC`
4025
: filter?.runStatuses === undefined
4026
? sql`ORDER BY ordinal ASC`
4020
- : filter.includeLatestRun !== true
4027
+ : filter.includeLatestRun !== true && filter.includeLatestExecutedRun !== true
4028
? // `+ordinal` stops SQLite from walking every run in the (thread_id, ordinal)
4029
// index to skip a sort; a status index reads only matching runs.
4030
sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
4031
ORDER BY +ordinal ASC`
4025
- : sql`AND (status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
4032
+ : filter.includeLatestExecutedRun === true
4033
+ ? sql`AND run_id IN (
4034
+ SELECT run_id FROM orchestration_v2_projection_runs
4035
+ WHERE thread_id = ${threadId}
4036
+ AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
4037
+ UNION
4038
+ SELECT run_id FROM (
4039
+ SELECT run_id FROM orchestration_v2_projection_runs
4040
+ WHERE thread_id = ${threadId}
4041
+ AND status <> 'queued'
4042
+ AND NOT (
4043
+ status = 'cancelled'
4044
+ AND json_extract(payload_json, '$.startedAt') IS NULL
4045
+ )
4046
+ -- latestExecutedRun: the run that ended last (runRanAfter).
4047
+ ORDER BY completed_at IS NULL DESC, completed_at DESC, ordinal DESC,
4048
+ run_id DESC
4049
+ LIMIT 1
4050
+ )
4051
+ )
4052
+ ORDER BY ordinal ASC`
4053
+ : sql`AND (status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
4054
OR run_id = (
4055
SELECT run_id FROM orchestration_v2_projection_runs
4056
WHERE thread_id = ${threadId}