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, 13:30 UTC
perf(server): delegated tasks start and finish without reading the whole parent thread (#34551)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Theo Browne committed
Nov 14, 2026 at 16:44 UTC
3e1761821226c472318168238c7599eea26dc49c
8 files changed
+427
-69
apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+221
@@ -1,3 +1,5 @@
1
+import * as NodeSqlite from "node:sqlite";
2
+
3
import { assert, it } from "@effect/vitest";
4
import {
5
CommandId,
@@ -24,8 +26,10 @@ import * as Effect from "effect/Effect";
26
import * as Fiber from "effect/Fiber";
27
import * as Layer from "effect/Layer";
28
import * as Metric from "effect/Metric";
29
+import * as Option from "effect/Option";
30
import * as Result from "effect/Result";
31
import * as Stream from "effect/Stream";
32
+import type * as Tracer from "effect/Tracer";
33
import * as TestClock from "effect/testing/TestClock";
34
import * as SqlClient from "effect/sql/SqlClient";
35
import * as Statement from "effect/sql/Statement";
@@ -44,6 +48,13 @@ import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
48
import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
49
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
50
51
+declare module "node:sqlite" {
52
+ interface StatementSync {
53
+ // Node 26 has this; @types/node 24 does not declare it yet.
54
+ stat(counter: "vmStep"): number;
55
+ }
56
+}
57
+
58
const instanceId = ProviderInstanceId.make("codex");
59
const modelSelection = { instanceId, model: "gpt-5.1-codex" };
60
const adapter = {
@@ -1901,6 +1912,216 @@ it.effect.each([false, true])("rejects thread.create for an existing id (deleted
1912
}).pipe(Effect.provide(layerTest)),
1913
);
1914
1915
+it.effect("starts and finishes a delegated task without reading the parent's history", () =>
1916
+ Effect.gen(function* () {
1917
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
1918
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
1919
+ const eventSink = yield* EventSink.EventSinkV2;
1920
+ const sql = yield* SqlClient.SqlClient;
1921
+ const parentThreadId = ThreadId.make("thread:long-parent");
1922
+ const now = yield* DateTime.now;
1923
+ yield* orchestrator.dispatch({
1924
+ type: "thread.create",
1925
+ commandId: CommandId.make("create-long-parent"),
1926
+ threadId: parentThreadId,
1927
+ projectId: ProjectId.make("project:long-parent"),
1928
+ title: "Parent",
1929
+ modelSelection,
1930
+ runtimeMode: "full-access",
1931
+ interactionMode: "default",
1932
+ branch: null,
1933
+ worktreePath: null,
1934
+ createdBy: "user",
1935
+ creationSource: "web",
1936
+ });
1937
+ yield* orchestrator.dispatch({
1938
+ type: "message.dispatch",
1939
+ commandId: CommandId.make("start-long-parent"),
1940
+ threadId: parentThreadId,
1941
+ messageId: MessageId.make("message:long-parent"),
1942
+ text: "Delegate the work",
1943
+ attachments: [],
1944
+ dispatchMode: { type: "start_immediately" },
1945
+ createdBy: "user",
1946
+ creationSource: "web",
1947
+ });
1948
+ const parent = yield* projections.getThreadRecords(parentThreadId, [
1949
+ "runs",
1950
+ "attempts",
1951
+ "providerThreads",
1952
+ ]);
1953
+ const parentRun = parent.runs[0]!;
1954
+ const providerTurnId = ProviderTurnId.make("provider-turn:long-parent");
1955
+ const nativeRef = (nativeId: string) => ({
1956
+ driver: adapter.driver,
1957
+ nativeId,
1958
+ strength: "strong" as const,
1959
+ });
1960
+ for (const event of [
1961
+ {
1962
+ type: "provider-thread.updated",
1963
+ payload: { ...parent.providerThreads[0]!, nativeThreadRef: nativeRef("native:parent") },
1964
+ },
1965
+ // A steered run: its attempt points at a provider turn that another attempt started.
1966
+ {
1967
+ type: "provider-turn.updated",
1968
+ payload: {
1969
+ id: providerTurnId,
1970
+ providerThreadId: parentRun.providerThreadId!,
1971
+ nodeId: parentRun.rootNodeId!,
1972
+ runAttemptId: null,
1973
+ nativeTurnRef: nativeRef("native:parent:turn"),
1974
+ ordinal: 1,
1975
+ status: "running",
1976
+ startedAt: now,
1977
+ completedAt: null,
1978
+ },
1979
+ },
1980
+ { type: "run-attempt.updated", payload: { ...parent.attempts[0]!, providerTurnId } },
1981
+ ] as const) {
1982
+ yield* projections.apply({
1983
+ ...event,
1984
+ id: EventId.make(`event:long-parent:${event.type}`),
1985
+ threadId: parentThreadId,
1986
+ runId: parentRun.id,
1987
+ occurredAt: now,
1988
+ });
1989
+ }
1990
+
1991
+ // Keep the statements of the task start and finish operations, not of the recovery around them.
1992
+ const taskSpans = [
1993
+ "orchestrationV2.dispatch.delegatedTaskRequest",
1994
+ "Orchestrator.finalizeAppOwnedSubagent",
1995
+ ];
1996
+ const statements: Array<readonly [string, ReadonlyArray<unknown>]> = [];
1997
+ const recordStatements = Effect.provideService(
1998
+ Statement.CurrentTransformer,
1999
+ (statement, _make, _fiber, span) => {
2000
+ for (
2001
+ let parent = Option.some<Tracer.AnySpan>(span);
2002
+ Option.isSome(parent) && parent.value._tag === "Span";
2003
+ parent = parent.value.parent
2004
+ ) {
2005
+ if (taskSpans.includes(parent.value.name)) {
2006
+ statements.push(statement.compile());
2007
+ break;
2008
+ }
2009
+ }
2010
+ return Effect.succeed(statement);
2011
+ },
2012
+ );
2013
+ yield* orchestrator
2014
+ .dispatch({
2015
+ type: "delegated_task.request",
2016
+ commandId: CommandId.make("delegate-long-parent"),
2017
+ parentThreadId,
2018
+ parentRunId: parentRun.id,
2019
+ parentNodeId: parentRun.rootNodeId!,
2020
+ task: "Do the work",
2021
+ completionWake: "always",
2022
+ modelSelection,
2023
+ runtimeMode: "full-access",
2024
+ interactionMode: "default",
2025
+ createdBy: "agent",
2026
+ creationSource: "mcp",
2027
+ })
2028
+ .pipe(recordStatements);
2029
+ const started = yield* projections.getThreadRecords(parentThreadId, ["subagents", "turnItems"]);
2030
+ const task = started.subagents[0]!;
2031
+ const childThreadId = task.childThreadId!;
2032
+ assert.equal(
2033
+ started.turnItems.find((item) => item.type === "subagent")?.providerTurnId,
2034
+ providerTurnId,
2035
+ );
2036
+ const child = yield* projections.getThreadRecords(childThreadId, ["runs", "contextTransfers"]);
2037
+ const spawnPoint = child.contextTransfers[0]?.sourcePoint;
2038
+ assert.deepEqual(
2039
+ [spawnPoint?.providerThreadRef, spawnPoint?.providerTurnRef],
2040
+ [nativeRef("native:parent"), nativeRef("native:parent:turn")],
2041
+ );
2042
+
2043
+ // The run listener skips reconcile commands, so the recovery below finishes the task here.
2044
+ yield* eventSink.write({
2045
+ commandId: CommandId.make("command:runtime-reconcile:long-parent"),
2046
+ events: [
2047
+ {
2048
+ id: EventId.make("event:long-parent:child-completed"),
2049
+ type: "run.updated",
2050
+ threadId: childThreadId,
2051
+ runId: child.runs[0]!.id,
2052
+ occurredAt: now,
2053
+ payload: { ...child.runs[0]!, status: "completed", completedAt: now },
2054
+ },
2055
+ ],
2056
+ });
2057
+ yield* orchestrator.recoverDelegatedTasks.pipe(recordStatements);
2058
+ const finished = yield* projections.getThreadRecords(parentThreadId, [
2059
+ "subagents",
2060
+ "nodes",
2061
+ "turnItems",
2062
+ "runs",
2063
+ ]);
2064
+ assert.deepEqual(
2065
+ [
2066
+ finished.subagents[0]?.status,
2067
+ finished.subagents[0]?.completionDelivery?.state,
2068
+ finished.nodes.find((node) => node.id === task.id)?.status,
2069
+ finished.turnItems.find((item) => item.type === "subagent")?.status,
2070
+ finished.runs[0]?.delegatedCompletion?.delivery?.taskIds,
2071
+ ],
2072
+ ["completed", "claimed", "completed", "completed", [task.id]],
2073
+ );
2074
+
2075
+ // Start and finish read the task's own records, so their VM steps, which are deterministic,
2076
+ // must stay flat on a copy with a long parent history. Left out: the counts that number the
2077
+ // task and its wake, which walk one index.
2078
+ const copyPath = "file:/delegated-task-reads?vfs=memdb";
2079
+ using copy = new NodeSqlite.DatabaseSync(copyPath);
2080
+ yield* sql`VACUUM INTO ${copyPath}`;
2081
+ const reads = statements.filter(
2082
+ ([query]) => /^\s*(SELECT|WITH)\b/.test(query) && !query.includes("COUNT(*)"),
2083
+ );
2084
+ const readSteps = () =>
2085
+ reads.reduce((steps, [query, params]) => {
2086
+ const statement = copy.prepare(query);
2087
+ statement.all(...(params as ReadonlyArray<NodeSqlite.SQLInputValue>));
2088
+ return steps + statement.stat("vmStep");
2089
+ }, 0);
2090
+ const shortSteps = readSteps();
2091
+ assert.isAbove(shortSteps, 0);
2092
+ const addHistory = (table: string, key: string, renamed: ReadonlyArray<string> = []) => {
2093
+ const name = `orchestration_v2_projection_${table}`;
2094
+ const columns = copy
2095
+ .prepare(`SELECT name FROM pragma_table_info('${name}')`)
2096
+ .all()
2097
+ .map((column) => String(column.name));
2098
+ const values = columns.map((column) =>
2099
+ column === key || renamed.includes(column)
2100
+ ? `CASE WHEN ${column} = '${parentThreadId}' THEN ${column} ELSE ${column} || ':history:' || history.n END`
2101
+ : column === "ordinal"
2102
+ ? "ordinal + history.n"
2103
+ : column,
2104
+ );
2105
+ copy.exec(`WITH RECURSIVE history(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM history WHERE n < 300)
2106
+ INSERT INTO ${name} (${columns.join(", ")})
2107
+ SELECT ${values.join(", ")} FROM ${name}, history
2108
+ WHERE '${parentThreadId}' IN (${columns.filter((column) => column.endsWith("thread_id")).join(", ")})`);
2109
+ };
2110
+ addHistory("nodes", "node_id");
2111
+ addHistory("turn_items", "turn_item_id", ["node_id"]);
2112
+ addHistory("subagents", "subagent_id", ["child_thread_id"]);
2113
+ addHistory("context_transfers", "context_transfer_id", [
2114
+ "source_thread_id",
2115
+ "target_thread_id",
2116
+ ]);
2117
+ addHistory("runs", "run_id");
2118
+ addHistory("run_attempts", "attempt_id", ["run_id"]);
2119
+ addHistory("messages", "message_id");
2120
+ addHistory("provider_turns", "provider_turn_id", ["run_attempt_id"]);
2121
+ assert.isAtMost(readSteps(), shortSteps * 1.1);
2122
+ }).pipe(Effect.provide(layerTest)),
2123
+);
2124
+
2125
it.effect("replays a thread.create receipt without changing the thread", () =>
2126
Effect.gen(function* () {
2127
const orchestrator = yield* Orchestrator.OrchestratorV2;
apps/server/src/orchestration-v2/Orchestrator.ts
+64
-48
@@ -1978,6 +1978,34 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1978
]),
1979
];
1980
1981
+ /** The parent records `planDelegatedCompletionDelivery` reads for one task: its run, and the queued wake's message, run and tasks. */
1982
+ const readDelegatedCompletionPlanRecords =
1983
+ /* The delegated task operation that calls this owns the span. */
1984
+ Effect.fnUntraced(function* (parentThreadId: ThreadId, task: OrchestrationV2Subagent) {
1985
+ const {
1986
+ thread,
1987
+ runs: [parentRun],
1988
+ } = yield* projectionStore.getThreadRecords(parentThreadId, ["runs"], {
1989
+ runIds: task.runId === null ? [] : [task.runId],
1990
+ });
1991
+ const delivery = parentRun?.delegatedCompletion?.delivery;
1992
+ if (delivery == null) return { thread, parentRun, runs: [], messages: [], subagents: [] };
1993
+ const { messages } = yield* projectionStore.getThreadRecords(parentThreadId, ["messages"], {
1994
+ messageIds: [delivery.messageId],
1995
+ messageRoles: ["user"],
1996
+ });
1997
+ const deliveryRunId = messages[0]?.runId;
1998
+ const { runs, subagents } = yield* projectionStore.getThreadRecords(
1999
+ parentThreadId,
2000
+ ["runs", "subagents"],
2001
+ {
2002
+ runIds: deliveryRunId == null ? [] : [deliveryRunId],
2003
+ subagentIds: [...delivery.taskIds, task.id],
2004
+ },
2005
+ );
2006
+ return { thread, parentRun, runs, messages, subagents };
2007
+ });
2008
+
2009
const offerDelegatedCompletionDelivery =
2010
/* This helper runs for individual orchestration events; the enclosing operation owns the span. */
2011
Effect.fnUntraced(function* (threadId: ThreadId, parentRunId: RunId) {
@@ -9324,19 +9352,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
9352
effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,
9353
) {
9354
const parentProjection = yield* projectionStore
9327
- .getThreadRecords(
9328
- command.parentThreadId,
9329
- ["runs", "nodes", "providerThreads", "providerTurns", "attempts"],
9330
- // The parent node must belong to the active run, whose nodes this keeps.
9331
- { omitSettledLeafNodes: true },
9332
- )
9355
+ .getRunProviderRecords(command.parentThreadId, command.parentRunId, command.parentNodeId)
9356
.pipe(
9357
Effect.mapError(
9335
- (cause) =>
9336
- new OrchestratorProjectionError({
9337
- threadId: command.parentThreadId,
9338
- cause,
9339
- }),
9358
+ (cause) => new OrchestratorProjectionError({ threadId: command.parentThreadId, cause }),
9359
),
9360
);
9361
const parentRun = parentProjection.runs.find(
@@ -12516,16 +12535,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12535
command: OrchestrationV2ServerCommand,
12536
threadId: ThreadId,
12537
runId: RunId,
12519
- ) =>
12520
- projectionStore
12521
- .getThreadRecords(threadId, ["turnItems"], {
12522
- turnItemTypes: ["run_interrupt_request"],
12523
- turnItemRunIds: [runId],
12524
- })
12525
- .pipe(
12526
- Effect.map(({ turnItems }) => turnItems.length > 0),
12527
- mapDispatchError(command),
12528
- );
12538
+ ) => projectionStore.hasRunInterruptRequest(threadId, runId).pipe(mapDispatchError(command));
12539
12540
const markStoppedRun = Effect.fnUntraced(function* (input: {
12541
readonly command: Extract<
@@ -14043,7 +14053,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14053
)(function* (input: {
14054
readonly parentProjection: Pick<
14055
OrchestrationV2ThreadProjection,
14046
- "thread" | "messages" | "runs" | "subagents" | "providerTurns"
14056
+ "thread" | "messages" | "runs" | "subagents"
14057
>;
14058
readonly parentRun: OrchestrationV2Run | undefined;
14059
readonly task: OrchestrationV2Subagent;
@@ -14313,7 +14323,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14323
const parent = yield* projectionStore.getThreadRecords(
14324
childControls.thread.lineage.parentThreadId,
14325
["nodes", "turnItems"],
14316
- { turnItemTypes: ["subagent"] },
14326
+ { nodeIds: [task.id], turnItemNodeIds: [task.id], turnItemTypes: ["subagent"] },
14327
);
14328
const now = yield* DateTime.now;
14329
const scope = {
@@ -14391,21 +14401,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14401
turnItems: childResult.turnItems,
14402
};
14403
const parentThreadId = childControls.thread.lineage.parentThreadId;
14394
- const parentProjection = yield* projectionStore.getThreadRecords(
14404
+ // The task's node and turn item share its id.
14405
+ const parentTask = yield* projectionStore.getThreadRecords(
14406
parentThreadId,
14396
- [
14397
- "runs",
14398
- "messages",
14399
- "subagents",
14400
- "providerThreads",
14401
- "providerTurns",
14402
- "nodes",
14403
- "turnItems",
14404
- "contextTransfers",
14405
- ],
14406
- { turnItemTypes: ["subagent"], messageRoles: ["user"] },
14407
+ ["subagents", "nodes", "turnItems"],
14408
+ {
14409
+ subagentIds: [options.task.id],
14410
+ nodeIds: [options.task.id],
14411
+ turnItemNodeIds: [options.task.id],
14412
+ turnItemTypes: ["subagent"],
14413
+ },
14414
);
14408
- const task = parentProjection.subagents.find(
14415
+ const task = parentTask.subagents.find(
14416
(candidate) =>
14417
candidate.id === options.task.id &&
14418
candidate.origin === "app_owned" &&
@@ -14417,7 +14424,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14424
// A cancelled task's completed run ends it interrupted.
14425
const cancelled = task.cancelledRunId === childRun.id;
14426
if (cancelled && terminalStatus === "completed") outcome = terminalStatus = "interrupted";
14420
- const existingResultTransfer = parentProjection.contextTransfers.find(
14427
+ // Every result the child sent is among its own transfers, far fewer than the parent's.
14428
+ const childTransfers = yield* projectionStore.getThreadRecords(childThreadId, [
14429
+ "contextTransfers",
14430
+ ]);
14431
+ const existingResultTransfer = childTransfers.contextTransfers.find(
14432
(transfer) =>
14433
transfer.type === "subagent_result" &&
14434
transfer.sourceThreadId === childThreadId &&
@@ -14431,12 +14442,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14442
14443
const now = yield* DateTime.now;
14444
const result = subagentResultForRun(childProjection, { ...childRun, status: outcome });
14434
- const parentRun =
14435
- task.runId === null
14436
- ? undefined
14437
- : parentProjection.runs.find((candidate) => candidate.id === task.runId);
14438
- const parentNode = parentProjection.nodes.find((candidate) => candidate.id === task.id);
14439
- const parentTurnItem = parentProjection.turnItems.find(
14445
+ const { parentRun, ...parentProjection } = yield* readDelegatedCompletionPlanRecords(
14446
+ parentThreadId,
14447
+ task,
14448
+ );
14449
+ const parentNode = parentTask.nodes.find((candidate) => candidate.id === task.id);
14450
+ const parentTurnItem = parentTask.turnItems.find(
14451
(candidate) => candidate.type === "subagent" && candidate.subagentId === task.id,
14452
);
14453
const updatedTask: OrchestrationV2Subagent = {
@@ -14586,8 +14597,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14597
)
14598
return;
14599
const progress = delegatedTaskProgress(childControls);
14589
- const parent = yield* projectionStore.getThreadRecords(parentThreadId, [
14590
- "subagents",
14600
+ // Each task of this child names it, so the parent's other tasks stay unread.
14601
+ const parent = yield* projectionStore.getThreadRecords(parentThreadId, ["subagents"], {
14602
+ subagentChildThreadIds: [childThreadId],
14603
+ });
14604
+ const { contextTransfers } = yield* projectionStore.getThreadRecords(childThreadId, [
14605
"contextTransfers",
14606
]);
14607
for (const task of parent.subagents) {
@@ -14599,9 +14613,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14613
(task.id === childControls.thread.forkedFrom.nodeId &&
14614
(cancelled ? progress.state === "working" : progress.state !== "result_available")) ||
14615
(task.result !== null &&
14602
- parent.contextTransfers.some(
14616
+ contextTransfers.some(
14617
(transfer) =>
14604
- transfer.type === "subagent_result" && transfer.sourceThreadId === childThreadId,
14618
+ transfer.type === "subagent_result" &&
14619
+ transfer.sourceThreadId === childThreadId &&
14620
+ transfer.targetThreadId === parentThreadId,
14621
))
14622
)
14623
continue;
@@ -16022,7 +16038,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
16038
input.parentThreadId,
16039
Effect.gen(function* () {
16040
const { subagents } = yield* projectionStore
16025
- .getThreadRecords(input.parentThreadId, ["subagents"])
16041
+ .getThreadRecords(input.parentThreadId, ["subagents"], { subagentIds: [input.taskId] })
16042
.pipe(mapDispatchError(command));
16043
const task = subagents.find(
16044
(candidate) =>
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+39
-14
@@ -723,26 +723,51 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
723
// An id filter reads by primary key, not every row of the thread.
724
const byId = traceSqlStatements();
725
const found = yield* store
726
- .getThreadRecords(threadId, ["runs", "nodes", "subagents", "messages"], {
727
- runIds: [RunId.make("run:idle-compaction:4")],
728
- nodeIds: [nodeId],
729
- subagentIds: [nodeId],
730
- messageIds: [MessageId.make("message:idle-compaction:4")],
731
- })
726
+ .getThreadRecords(
727
+ threadId,
728
+ ["runs", "nodes", "subagents", "messages", "attempts", "providerTurns"],
729
+ {
730
+ runIds: [RunId.make("run:idle-compaction:4")],
731
+ nodeIds: [nodeId],
732
+ subagentIds: [nodeId],
733
+ messageIds: [MessageId.make("message:idle-compaction:4")],
734
+ attemptIds: [RunAttemptId.make("attempt:idle-compaction")],
735
+ providerTurnIds: [ProviderTurnId.make("turn:idle-old")],
736
+ },
737
+ )
738
.pipe(Effect.withTracer(byId.tracer));
739
assert.deepEqual(
740
found.runs.map((run) => run.id),
741
["run:idle-compaction:4"],
742
);
737
- for (const table of ["runs", "nodes", "subagents", "messages"]) {
738
- const query = byId.statements.find((statement) =>
739
- statement.includes(`FROM orchestration_v2_projection_${table}`),
743
+ assert.deepEqual(
744
+ found.providerTurns.map((turn) => turn.id),
745
+ ["turn:idle-old"],
746
+ );
747
+ // A task's turn item and a child thread's tasks are found through their own indexes.
748
+ yield* store
749
+ .getThreadRecords(threadId, ["subagents", "turnItems"], {
750
+ subagentChildThreadIds: [threadId],
751
+ turnItemNodeIds: [nodeId],
752
+ })
753
+ .pipe(Effect.withTracer(byId.tracer));
754
+ for (const [table, index] of [
755
+ ...["runs", "nodes", "subagents", "messages", "run_attempts", "provider_turns"].map(
756
+ (name) => [name, `sqlite_autoindex_orchestration_v2_projection_${name}_1`] as const,
757
+ ),
758
+ ["subagents", "orchestration_v2_projection_subagents_child_thread_idx"],
759
+ ["turn_items", "orchestration_v2_projection_turn_items_node_ordinal_idx"],
760
+ ] as const) {
761
+ const plans = yield* Effect.forEach(
762
+ byId.statements.filter((statement) =>
763
+ statement.includes(`FROM orchestration_v2_projection_${table}`),
764
+ ),
765
+ (query) => sql.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${query}`),
766
+ );
767
+ assert.isTrue(
768
+ plans.some((plan) => plan.some((row) => row.detail.includes(`USING INDEX ${index}`))),
769
+ `${table} is not read through ${index}`,
770
);
741
- assert.isDefined(query);
742
- const plan = (yield* sql.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${query}`))
743
- .map((row) => row.detail)
744
- .join("\n");
745
- assert.include(plan, `USING INDEX sqlite_autoindex_orchestration_v2_projection_${table}_1`);
771
}
772
// The filters every command reads with seek the rows they keep, not every row of the thread.
773
const command = traceSqlStatements();
apps/server/src/orchestration-v2/ProjectionStore.ts
+86
-7
@@ -348,7 +348,9 @@ export interface ProjectionRuntimeResponseContext {
348
349
export interface ProjectionRecordFilter {
350
readonly providerThreadIds?: ReadonlyArray<ProviderThreadId>;
351
+ readonly providerTurnIds?: ReadonlyArray<ProviderTurnId>;
352
readonly providerTurnAttemptIds?: ReadonlyArray<RunAttemptId>;
353
+ readonly attemptIds?: ReadonlyArray<RunAttemptId>;
354
readonly messageRoles?: ReadonlyArray<OrchestrationV2ConversationMessage["role"]>;
355
readonly monitorNotificationsOnly?: boolean;
356
readonly turnItemRunId?: RunId;
@@ -368,6 +370,7 @@ export interface ProjectionRecordFilter {
370
readonly contextTransferStatuses?: ReadonlyArray<OrchestrationV2ContextTransfer["status"]>;
371
readonly subagentIds?: ReadonlyArray<NodeId>;
372
readonly subagentRunIds?: ReadonlyArray<RunId>;
373
+ readonly subagentChildThreadIds?: ReadonlyArray<ThreadId>;
374
readonly nodeIds?: ReadonlyArray<NodeId>;
375
readonly turnItemRequestIds?: ReadonlyArray<RuntimeRequestId>;
376
readonly turnItemTypes?: ReadonlyArray<OrchestrationV2TurnItem["type"]>;
@@ -697,6 +700,20 @@ export interface ProjectionStoreV2Shape {
700
requestId: TurnItemId,
701
resultId: TurnItemId,
702
) => Effect.Effect<boolean, ProjectionStoreV2Error>;
703
+ /** Whether Stop reached the run. Reads one index entry, however long the thread is. */
704
+ readonly hasRunInterruptRequest: (
705
+ threadId: ThreadId,
706
+ runId: RunId,
707
+ ) => Effect.Effect<boolean, ProjectionStoreV2Error>;
708
+ /** One run and node with the run's provider thread, active attempt and provider turn, read together. */
709
+ readonly getRunProviderRecords: (
710
+ threadId: ThreadId,
711
+ runId: RunId,
712
+ nodeId: NodeId,
713
+ ) => Effect.Effect<
714
+ ProjectionRecords<"runs" | "nodes" | "providerThreads" | "providerTurns" | "attempts">,
715
+ ProjectionStoreV2Error
716
+ >;
717
readonly isNodeStored: (
718
node: OrchestrationV2ExecutionNode,
719
) => Effect.Effect<boolean, ProjectionStoreV2Error>;
@@ -3533,7 +3550,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3550
});
3551
3552
const turnItemMatch = (threadId: ThreadId, filter: ProjectionRecordFilter | undefined) => sql`
3536
- thread_id = ${threadId}
3553
+ ${threadColumn(filter?.turnItemNodeIds)} = ${threadId}
3554
${filter?.turnItemRequestIds === undefined ? sql`` : sql`AND json_extract(payload_json, '$.requestId') IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemRequestIds)}))`}
3555
${filter?.turnItemTypes === undefined ? sql`` : sql`AND type IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemTypes)}))`}
3556
${filter?.turnItemStatuses === undefined ? sql`` : sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemStatuses)}))`}
@@ -3886,13 +3903,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3903
))
3904
ORDER BY ordinal ASC
3905
`,
3889
- fields !== undefined && !fields.includes("attempts")
3906
+ (fields !== undefined && !fields.includes("attempts")) ||
3907
+ (window === undefined && matchesNothing(filter?.attemptIds))
3908
? Effect.succeed([])
3909
: window === undefined
3910
? sql<PayloadRow>`
3911
SELECT payload_json
3912
FROM orchestration_v2_projection_run_attempts
3895
- WHERE thread_id = ${threadId}
3913
+ WHERE ${threadColumn(filter?.attemptIds)} = ${threadId}
3914
+ ${filter?.attemptIds === undefined ? sql`` : sql`AND attempt_id IN (SELECT value FROM json_each(${encodeIdList(filter.attemptIds)}))`}
3915
${inRecentRuns("run_id")}
3916
ORDER BY run_id ASC, attempt_ordinal ASC
3917
`
@@ -3969,14 +3988,19 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3988
),
3989
(fields !== undefined && !fields.includes("subagents")) ||
3990
(window === undefined &&
3972
- (matchesNothing(filter?.subagentIds) || matchesNothing(filter?.subagentRunIds)))
3991
+ matchesNothing(
3992
+ filter?.subagentIds,
3993
+ filter?.subagentRunIds,
3994
+ filter?.subagentChildThreadIds,
3995
+ ))
3996
? Effect.succeed([])
3997
: window === undefined
3998
? sql<PayloadRow>`
3999
SELECT payload_json
4000
FROM orchestration_v2_projection_subagents AS subagent
3978
- WHERE ${threadColumn(filter?.subagentIds)} = ${threadId}
4001
+ WHERE ${threadColumn(filter?.subagentIds ?? filter?.subagentChildThreadIds)} = ${threadId}
4002
${filter?.subagentIds === undefined ? sql`` : sql`AND subagent_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentIds)}))`}
4003
+ ${filter?.subagentChildThreadIds === undefined ? sql`` : sql`AND child_thread_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentChildThreadIds)}))`}
4004
${filter?.subagentRunIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentRunIds)}))`}
4005
${
4006
filter?.omitSettledSubagents === true
@@ -4073,13 +4097,18 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4097
`,
4098
(fields !== undefined && !fields.includes("providerTurns")) ||
4099
(window === undefined &&
4076
- matchesNothing(filter?.providerTurnAttemptIds, filter?.providerTurnThreadIds))
4100
+ matchesNothing(
4101
+ filter?.providerTurnIds,
4102
+ filter?.providerTurnAttemptIds,
4103
+ filter?.providerTurnThreadIds,
4104
+ ))
4105
? Effect.succeed([])
4106
: window === undefined
4107
? sql<PayloadRow>`
4108
SELECT payload_json
4109
FROM orchestration_v2_projection_provider_turns
4082
- WHERE thread_id = ${threadId}
4110
+ WHERE ${threadColumn(filter?.providerTurnIds ?? filter?.providerTurnAttemptIds)} = ${threadId}
4111
+ ${filter?.providerTurnIds === undefined ? sql`` : sql`AND provider_turn_id IN (SELECT value FROM json_each(${encodeIdList(filter.providerTurnIds)}))`}
4112
${
4113
recentRunIds === null
4114
? sql``
@@ -5659,6 +5688,22 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5688
effect.pipe(Effect.mapError(controlReadError(threadId))),
5689
);
5690
5691
+ const hasRunInterruptRequest: ProjectionStoreV2Shape["hasRunInterruptRequest"] = Effect.fn(
5692
+ "ProjectionStore.hasRunInterruptRequest",
5693
+ )(
5694
+ function* (threadId, runId) {
5695
+ yield* requireThread(threadId);
5696
+ // The literal type lets SQLite use the partial stop-request index.
5697
+ const rows = yield* sql<{ readonly found: number }>`
5698
+ SELECT 1 AS found FROM orchestration_v2_projection_turn_items
5699
+ WHERE thread_id = ${threadId} AND run_id = ${runId} AND type = 'run_interrupt_request'
5700
+ LIMIT 1`;
5701
+ return rows.length > 0;
5702
+ },
5703
+ (effect, threadId, _runId) =>
5704
+ sql.withTransaction(effect).pipe(Effect.mapError(controlReadError(threadId))),
5705
+ );
5706
+
5707
const isNodeStored: ProjectionStoreV2Shape["isNodeStored"] = Effect.fn(
5708
"ProjectionStore.isNodeStored",
5709
)(
@@ -6363,6 +6408,38 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6408
Effect.mapError(controlReadError(threadId)),
6409
);
6410
6411
+ const getRunProviderRecords: ProjectionStoreV2Shape["getRunProviderRecords"] = Effect.fn(
6412
+ "ProjectionStore.getRunProviderRecords",
6413
+ )(
6414
+ function* (threadId, runId, nodeId) {
6415
+ const controls = yield* getThreadRecords(threadId, ["runs", "nodes"], {
6416
+ runIds: [runId],
6417
+ nodeIds: [nodeId],
6418
+ });
6419
+ const run = controls.runs[0];
6420
+ const attemptIds = run?.activeAttemptId == null ? [] : [run.activeAttemptId];
6421
+ const records = yield* getThreadRecords(
6422
+ threadId,
6423
+ ["providerThreads", "providerTurns", "attempts"],
6424
+ {
6425
+ providerThreadIds: run?.providerThreadId == null ? [] : [run.providerThreadId],
6426
+ providerTurnAttemptIds: attemptIds,
6427
+ attemptIds,
6428
+ },
6429
+ );
6430
+ const attemptTurnId = records.attempts[0]?.providerTurnId;
6431
+ if (records.providerTurns.length > 0 || attemptTurnId == null)
6432
+ return { ...controls, ...records };
6433
+ // A steered run's attempt can point at a provider turn that another attempt started.
6434
+ const { providerTurns } = yield* getThreadRecords(threadId, ["providerTurns"], {
6435
+ providerTurnIds: [attemptTurnId],
6436
+ });
6437
+ return { ...controls, ...records, providerTurns };
6438
+ },
6439
+ (effect, threadId, _runId, _nodeId) =>
6440
+ sql.withTransaction(effect).pipe(Effect.mapError(controlReadError(threadId))),
6441
+ );
6442
+
6443
const getThreadSnapshot: ProjectionStoreV2Shape["getThreadSnapshot"] = (threadId) =>
6444
sql
6445
.withTransaction(
@@ -9129,6 +9206,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
9206
getHeldQueuedRuns: readOnReader(getHeldQueuedRuns),
9207
getPendingNativeUserInputs: readOnReader(getPendingNativeUserInputs),
9208
hasUnpairedRunInterruptRequest: readOnReader(hasUnpairedRunInterruptRequest),
9209
+ hasRunInterruptRequest: readOnReader(hasRunInterruptRequest),
9210
+ getRunProviderRecords: readOnReader(getRunProviderRecords),
9211
isNodeStored: readOnReader(isNodeStored),
9212
getMessageCount: readOnReader(getMessageCount),
9213
getTranscriptByteFloor: readOnReader(getTranscriptByteFloor),
apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
+2
@@ -235,6 +235,8 @@ it.effect(
235
getRuntimeRecoveryProjection: () => Effect.die("unused getRuntimeRecoveryProjection"),
236
getPlan: () => Effect.die("unused"),
237
hasUnpairedRunInterruptRequest: () => Effect.die("unused interrupt read"),
238
+ hasRunInterruptRequest: () => Effect.die("unused stop read"),
239
+ getRunProviderRecords: () => Effect.die("unused run provider read"),
240
getThreadAttachmentIds: () => Effect.die("Unused attachment lookup"),
241
searchThread: () => Effect.die("unused"),
242
searchThreadStream: () => Stream.empty,
apps/server/src/persistence/Migrations.ts
+2
@@ -100,6 +100,7 @@ import Migration0089 from "./Migrations/089_ShellThreadReadIndexes.ts";
100
import Migration0090 from "./Migrations/090_AuthConnectDevices.ts";
101
import Migration0091 from "./Migrations/091_AuthConnectDeniedRequests.ts";
102
import Migration0092 from "./Migrations/092_QueuedTurnInputIndexJsonValid.ts";
103
+import Migration0093 from "./Migrations/093_ProviderTurnAttemptIndex.ts";
104
105
const migrationEntries = [
106
[1, "OrchestrationEvents", Migration0001],
@@ -195,6 +196,7 @@ const migrationEntries = [
196
[90, "AuthConnectDevices", Migration0090],
197
[91, "AuthConnectDeniedRequests", Migration0091],
198
[92, "QueuedTurnInputIndexJsonValid", Migration0092],
199
+ [93, "ProviderTurnAttemptIndex", Migration0093],
200
] as const;
201
202
export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
apps/server/src/persistence/Migrations/093_ProviderTurnAttemptIndex.ts
new
+11
@@ -0,0 +1,11 @@
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
+ yield* sql`
8
+ CREATE INDEX orchestration_v2_projection_provider_turns_attempt_idx
9
+ ON orchestration_v2_projection_provider_turns(run_attempt_id)
10
+ `;
11
+});
apps/server/src/persistence/reconcileV2PreviewMigration.test.ts
+2
@@ -72,6 +72,7 @@ describe("V2 preview upgrade", () => {
72
[90, "AuthConnectDevices"],
73
[91, "AuthConnectDeniedRequests"],
74
[92, "QueuedTurnInputIndexJsonValid"],
75
+ [93, "ProviderTurnAttemptIndex"],
76
]);
77
assert.deepStrictEqual(yield* runMigrations(), []);
78
assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -187,6 +188,7 @@ describe("V2 preview upgrade", () => {
188
[90, "AuthConnectDevices"],
189
[91, "AuthConnectDeniedRequests"],
190
[92, "QueuedTurnInputIndexJsonValid"],
191
+ [93, "ProviderTurnAttemptIndex"],
192
]);
193
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
194
);