The best way to control Claude Code, Codex and more from any computer or phone
perf(server): long threads open 2 to 6x faster from a cold cache (#35286)
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
Theo Browne committed
Nov 12, 2026 at 06:04 UTC
156dcd8c7a9b6d664826926346efa8703d41f997
3 files changed
+44
-13
apps/server/src/orchestration-v2/ProjectionRecovery.test.ts
+22
-8
@@ -484,11 +484,12 @@ it.effect("selects unfinished recovery work without reading settled thread histo
484
WHERE thread_id = ${queued} AND ordinal = 1
485
`;
486
assert.deepEqual(yield* projections.getRecoveryThreadIds("queued-runs"), [queued]);
487
- assert.deepEqual(
488
- new Set(yield* projections.getRecoveryThreadIds("runtime")),
489
- new Set([queued, archived, blocked, background, outboxOnly, requestOnly]),
490
- );
487
for (const [kind, expected, index] of [
488
+ [
489
+ "runtime",
490
+ [queued, archived, blocked, background, outboxOnly, requestOnly],
491
+ "nodes_live_idx",
492
+ ],
493
["delegated-completions", [delivery], "runs_delegated_delivery_idx"],
494
["subagent-results", [], "threads_subagent_result_idx"],
495
] as const) {
@@ -498,10 +499,12 @@ it.effect("selects unfinished recovery work without reading settled thread histo
499
return Effect.succeed(statement);
500
};
501
assert.deepEqual(
501
- yield* projections
502
- .getRecoveryThreadIds(kind)
503
- .pipe(Effect.provideService(Statement.CurrentTransformer, observe)),
504
- expected,
502
+ new Set(
503
+ yield* projections
504
+ .getRecoveryThreadIds(kind)
505
+ .pipe(Effect.provideService(Statement.CurrentTransformer, observe)),
506
+ ),
507
+ new Set<ThreadId>(expected),
508
);
509
const [query, params] = statements[0]!;
510
const plan = yield* sql.unsafe<{ detail: string }>(`EXPLAIN QUERY PLAN ${query}`, params);
@@ -1128,14 +1131,25 @@ it.effect("bounds runtime provider-thread reads by ownership and preserves recov
1131
${ProviderThreadId.make("provider-thread:ownership:item")}, 1, 'assistant_message', 'waiting', '', '{}')
1132
`;
1133
const statements: Array<ReturnType<Statement.Statement<unknown>["compile"]>> = [];
1134
+ const nodeStatements: typeof statements = [];
1135
const observe: Statement.Transformer = (statement) => {
1136
const compiled = statement.compile();
1137
if (compiled[0].includes("SELECT provider_thread.payload_json")) statements.push(compiled);
1138
+ if (compiled[0].includes("SELECT node.payload_json")) nodeStatements.push(compiled);
1139
return Effect.succeed(statement);
1140
};
1141
const projection = yield* projections
1142
.getRuntimeRecoveryProjection(threadId)
1143
.pipe(Effect.provideService(Statement.CurrentTransformer, observe));
1144
+ assert.equal(nodeStatements.length, 1);
1145
+ const nodePlan = yield* sql.unsafe<{ detail: string }>(
1146
+ `EXPLAIN QUERY PLAN ${nodeStatements[0]![0]}`,
1147
+ nodeStatements[0]![1],
1148
+ );
1149
+ assert.isTrue(
1150
+ nodePlan.some((row) => /^SEARCH node USING INDEX .*nodes_live_idx/.test(row.detail)),
1151
+ JSON.stringify(nodePlan),
1152
+ );
1153
assert.deepEqual(
1154
projection.providerThreads.map((row) => row.id),
1155
cases
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+13
-3
@@ -9225,9 +9225,19 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
9225
const boundedFork = yield* projectionStore
9226
.getThreadSnapshotWindow(targetThreadId, { rowLimit: 2 })
9227
.pipe(Effect.withTracer(tracer));
9228
- assert.lengthOf(
9229
- statements.filter((statement) => statement.includes("WITH RECURSIVE retained")),
9230
- 1,
9228
+ const retained = statements.filter((statement) =>
9229
+ statement.includes("WITH RECURSIVE retained"),
9230
+ );
9231
+ assert.lengthOf(retained, 1);
9232
+ // The settled-leaf index also starts with thread_id, and through it this lookup fetches
9233
+ // every node of the thread to check its status.
9234
+ const retainedPlan = (yield* sql.unsafe<{ readonly detail: string }>(
9235
+ `EXPLAIN QUERY PLAN ${retained[0]}`,
9236
+ )).map((row) => row.detail);
9237
+ assert.match(
9238
+ retainedPlan.find((detail) => detail.startsWith("SEARCH live ")) ?? "",
9239
+ /nodes_live_idx/,
9240
+ JSON.stringify(retainedPlan),
9241
);
9242
assert.lengthOf(
9243
statements.filter((statement) => statement.includes("SELECT node.payload_json")),
apps/server/src/orchestration-v2/ProjectionStore.ts
+9
-2
@@ -2105,6 +2105,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2105
`(${DEFERRED_WORK_ITEM_TYPES.map((type) => `'${type}'`).join(", ")})`,
2106
);
2107
2108
+ // Other node indexes also start with thread_id. Without this hint SQLite reads a thread's live
2109
+ // nodes through one of them and fetches every node of the thread to check its status. The
2110
+ // query must filter on the same status list as the index of migration 060.
2111
+ const LIVE_NODES_INDEX = sql.literal("INDEXED BY orchestration_v2_projection_nodes_live_idx");
2112
+
2113
// An ended task whose completion was acknowledged, delivered or disposed, outside an active run.
2114
const settledSubagent = (row: Statement.Fragment) => sql`(
2115
${row}.status IN ('completed', 'failed', 'cancelled', 'interrupted')
@@ -3622,6 +3627,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3627
SELECT value FROM json_each(${cohortJson("nodeId")})
3628
UNION
3629
SELECT node_id FROM orchestration_v2_projection_nodes AS live
3630
+ ${LIVE_NODES_INDEX}
3631
WHERE thread_id = ${threadId} AND status IN ('pending','starting','running','waiting')
3632
AND NOT ${todoListOfSettledRun(sql`live`)}
3633
UNION
@@ -4770,10 +4776,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4776
WHERE subagents.child_thread_id IS NOT NULL
4777
AND EXISTS (
4778
SELECT 1 FROM orchestration_v2_projection_nodes AS node
4779
+ ${LIVE_NODES_INDEX}
4780
WHERE node.thread_id = subagents.child_thread_id
4774
- -- Matches nodes_live_idx; +run_id keeps thread_run_idx from scanning settled nodes.
4781
AND node.status IN ('pending', 'starting', 'running', 'waiting')
4776
- AND +node.run_id IS NULL
4782
+ AND node.run_id IS NULL
4783
AND node.kind = 'root_turn'
4784
)
4785
UNION
@@ -5174,6 +5180,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5180
`,
5181
nodes: sql<PayloadRow>`
5182
SELECT node.payload_json FROM orchestration_v2_projection_nodes AS node
5183
+ ${LIVE_NODES_INDEX}
5184
WHERE node.thread_id = ${threadId}
5185
AND node.status IN ('pending', 'starting', 'running', 'waiting')
5186
AND (