The best way to control Claude Code, Codex and more from any computer or phone
perf(server): Load earlier turns takes about half the time on a running long thread (#35224)
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Theo Browne committed
Nov 12, 2026 at 07:13 UTC
4fd40e1a16e3800418b8f6a32932b68179098c8d
3 files changed
+177
-34
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+92
-3
@@ -4205,6 +4205,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4205
window.projection.visibleTurnItems.find((row) => row.item.deferredWork)?.item.ordinal,
4206
3,
4207
);
4208
+ const pageRead = traceSqlStatements();
4209
const page = yield* historyPage(
4210
threadId,
4211
encodeThreadHistoryCursor({
@@ -4216,12 +4217,100 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4217
undefined,
4218
false,
4219
"deferred",
4219
- );
4220
+ ).pipe(Effect.withTracer(pageRead.tracer));
4221
assert.isUndefined(page.items.find((row) => row.item.ordinal === 2)?.item.deferredWork);
4222
+ // Tool payloads hold megabytes, so a page looks for inline output only in its own runs.
4223
+ const payloadScans = pageRead.statements.filter((statement) =>
4224
+ statement.includes("LIKE '%htmlRender%'"),
4225
+ );
4226
+ assert.lengthOf(payloadScans, 1);
4227
+ assert.include(payloadScans[0], "item.run_id IS json_extract(wanted.value, '$.id')");
4228
}
4222
- yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution',
4223
- payload_json = json_set(payload_json, '$.type', 'command_execution') WHERE thread_id = ${threadId} AND ordinal = 2`;
4229
+ // Until its run is scanned, a cursor on inline output is hidden inside a work group.
4230
+ yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'dynamic_tool',
4231
+ payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', 't3-code.delegate_task')
4232
+ WHERE thread_id = ${threadId} AND ordinal = 4`;
4233
const sharedRunId = "run:bounded-sql-history:1";
4234
+ // A work page of rows that have no run leaves their inline output out too.
4235
+ yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = NULL
4236
+ WHERE thread_id = ${threadId} AND ordinal BETWEEN 3 AND 6`;
4237
+ const runlessWork = yield* historyPage(
4238
+ threadId,
4239
+ encodeThreadHistoryCursor({
4240
+ snapshotSequence: 0,
4241
+ sourceThreadId: threadId,
4242
+ sourceItemId: "turn-item:bounded-sql-history:6",
4243
+ position: 6,
4244
+ inclusive: true,
4245
+ }),
4246
+ "turn-item:bounded-sql-history:3",
4247
+ false,
4248
+ "work",
4249
+ );
4250
+ assert.deepEqual(
4251
+ runlessWork.items.map((row) => row.item.ordinal),
4252
+ [3, 5, 6],
4253
+ );
4254
+ yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = ${sharedRunId}
4255
+ WHERE thread_id = ${threadId} AND ordinal BETWEEN 3 AND 6`;
4256
+ // A stale position selects from the cursor row itself, or from more than a page after it
4257
+ // once the later rows are another run's and cannot fold.
4258
+ for (const [position, laterStatus, laterRun] of [
4259
+ [0, "completed", sharedRunId],
4260
+ [999, "running", "run:bounded-sql-history:300"],
4261
+ ] as const) {
4262
+ yield* sql`UPDATE orchestration_v2_projection_turn_items SET status = ${laterStatus},
4263
+ run_id = ${laterRun},
4264
+ payload_json = json_set(payload_json, '$.status', ${laterStatus}, '$.runId', ${laterRun})
4265
+ WHERE thread_id = ${threadId} AND ordinal >= 300`;
4266
+ const beforeInlineOutput = yield* historyPage(
4267
+ threadId,
4268
+ encodeThreadHistoryCursor({
4269
+ snapshotSequence: 0,
4270
+ sourceThreadId: threadId,
4271
+ sourceItemId: "turn-item:bounded-sql-history:4",
4272
+ position,
4273
+ }),
4274
+ undefined,
4275
+ false,
4276
+ "deferred",
4277
+ );
4278
+ assert.deepEqual(
4279
+ beforeInlineOutput.items.map((row) => row.item.ordinal),
4280
+ [1, 2, 3],
4281
+ );
4282
+ }
4283
+ // A cursor whose row is gone falls back to its position. Inline output between the page
4284
+ // and that position can belong to a run with no other row there.
4285
+ yield* sql`UPDATE orchestration_v2_projection_turn_items
4286
+ SET status = CASE WHEN ordinal <= 700 THEN 'running' ELSE 'completed' END,
4287
+ run_id = CASE WHEN ordinal <= 700 THEN 'run:bounded-sql-history:300' ELSE ${sharedRunId} END,
4288
+ payload_json = json_set(payload_json,
4289
+ '$.status', CASE WHEN ordinal <= 700 THEN 'running' ELSE 'completed' END,
4290
+ '$.runId', CASE WHEN ordinal <= 700 THEN 'run:bounded-sql-history:300' ELSE ${sharedRunId} END)
4291
+ WHERE thread_id = ${threadId} AND ordinal >= 100`;
4292
+ yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'dynamic_tool',
4293
+ payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', 't3-code.delegate_task')
4294
+ WHERE thread_id = ${threadId} AND ordinal = 720`;
4295
+ const beforeMissingCursor = yield* historyPage(
4296
+ threadId,
4297
+ encodeThreadHistoryCursor({
4298
+ snapshotSequence: 0,
4299
+ sourceThreadId: threadId,
4300
+ sourceItemId: "turn-item:bounded-sql-history:gone",
4301
+ position: 800,
4302
+ }),
4303
+ undefined,
4304
+ false,
4305
+ "deferred",
4306
+ );
4307
+ assert.strictEqual(beforeMissingCursor.items.at(-1)?.item.ordinal, 720);
4308
+ yield* sql`UPDATE orchestration_v2_projection_turn_items SET status = 'completed',
4309
+ run_id = ${sharedRunId},
4310
+ payload_json = json_set(payload_json, '$.status', 'completed', '$.runId', ${sharedRunId})
4311
+ WHERE thread_id = ${threadId} AND ordinal >= 100`;
4312
+ yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution',
4313
+ payload_json = json_set(payload_json, '$.type', 'command_execution') WHERE thread_id = ${threadId} AND ordinal IN (2, 4, 720)`;
4314
for (const [ordinal, status] of [
4315
[0, "completed"],
4316
[1, "superseded"],
apps/server/src/orchestration-v2/ProjectionStore.ts
+84
-30
@@ -103,7 +103,9 @@ import {
103
THREAD_HISTORY_MAX_RAW_TURNS,
104
missingInterruptRequests,
105
DEFERRED_WORK_ITEM_TYPES,
106
+ decodeThreadHistoryCursor,
107
deferWorkHistoryRows,
108
+ findCursorEndIndex,
109
selectDeferredWorkPage,
110
encodeThreadHistoryCursor,
111
olderHistoryPagePolicy,
@@ -1131,7 +1133,7 @@ type ShellItemCountRow = {
1133
1134
const encodeTimelineSources = Schema.encodeEffect(
1135
Schema.fromJsonString(
1134
- Schema.Array(Schema.Struct({ threadId: Schema.String, id: Schema.String })),
1136
+ Schema.Array(Schema.Struct({ threadId: Schema.String, id: Schema.NullOr(Schema.String) })),
1137
),
1138
);
1139
const encodeIdList = Schema.encodeSync(Schema.fromJsonString(Schema.Array(Schema.String)));
@@ -6280,11 +6282,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6282
const readTimelineIndex: (
6283
threadId: ThreadId,
6284
seen: ReadonlySet<ThreadId>,
6283
- includePersistentOutput?: boolean,
6285
+ includeAttempts?: boolean,
6286
) => Effect.Effect<TimelineIndex, ProjectionStoreV2Error> = Effect.fn(
6287
"ProjectionStore.readTimelineIndex",
6288
)(
6287
- function* (threadId: ThreadId, seen: ReadonlySet<ThreadId>, includePersistentOutput = false) {
6289
+ function* (threadId: ThreadId, seen: ReadonlySet<ThreadId>, includeAttempts = false) {
6290
const records = yield* getThreadRecords(threadId, ["runs", "attempts"]);
6291
const rows = yield* sql<{
6292
turn_item_id: string;
@@ -6297,21 +6299,17 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6299
message_id: string | null;
6300
status: OrchestrationV2TurnItem["status"];
6301
ordinal: number;
6300
- persistent_output: number;
6302
attempt_id: string | null;
6303
attempt_status: string | null;
6304
}>`SELECT item.turn_item_id, item.run_id, item.node_id, item.parent_item_id, item.type, item.status, item.ordinal,
6304
- ${includePersistentOutput ? sql`attempt.attempt_id` : sql`NULL`} AS attempt_id,
6305
- ${includePersistentOutput ? sql`attempt.status` : sql`NULL`} AS attempt_status,
6306
- ${
6307
- includePersistentOutput ? sql`CASE WHEN ${inlineWorkOutput} THEN 1 ELSE 0 END` : sql`0`
6308
- } AS persistent_output,
6305
+ ${includeAttempts ? sql`attempt.attempt_id` : sql`NULL`} AS attempt_id,
6306
+ ${includeAttempts ? sql`attempt.status` : sql`NULL`} AS attempt_status,
6307
CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.inputIntent') END AS input_intent,
6308
CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.createdBy') END AS created_by,
6309
CASE WHEN item.type IN ('user_message', 'assistant_message') THEN json_extract(item.payload_json, '$.messageId') END AS message_id
6310
FROM orchestration_v2_projection_turn_items AS item
6311
${
6314
- includePersistentOutput
6312
+ includeAttempts
6313
? sql`LEFT JOIN orchestration_v2_projection_nodes AS node ON node.node_id = item.node_id
6314
LEFT JOIN orchestration_v2_projection_run_attempts AS attempt ON attempt.attempt_id = (
6315
SELECT attempt_id FROM orchestration_v2_projection_run_attempts
@@ -6335,7 +6333,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6333
runStatus: row.run_id === null ? undefined : runStatuses.get(row.run_id),
6334
attemptId: row.attempt_id,
6335
attemptStatus: row.attempt_status,
6338
- persistentOutput: row.persistent_output === 1,
6336
...(row.input_intent === null ? {} : { inputIntent: row.input_intent }),
6337
...(row.created_by === null ? {} : { createdBy: row.created_by }),
6338
...(row.message_id === null ? {} : { messageId: MessageId.make(row.message_id) }),
@@ -6358,7 +6355,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6355
const source = yield* readTimelineIndex(
6356
fork.threadId,
6357
new Set([...seen, threadId]),
6361
- includePersistentOutput,
6358
+ includeAttempts,
6359
);
6360
const sourceRun = source.records.runs.find((run) => run.id === fork.runId);
6361
let inherited: Array<TimelineIndexRow> = [];
@@ -6429,14 +6426,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6426
Effect.mapError((cause) => new ProjectionStoreReadError({ threadId, cause })),
6427
);
6428
const readSnapshotTimelineIndex = Effect.fn("ProjectionStore.readSnapshotTimelineIndex")(
6432
- function* (threadId: ThreadId, includePersistentOutput: boolean) {
6433
- const key = `${includePersistentOutput ? "work" : "plain"}:${threadId}`;
6429
+ function* (threadId: ThreadId, includeAttempts: boolean) {
6430
+ const key = `${includeAttempts ? "work" : "plain"}:${threadId}`;
6431
const cached = timelineIndexes.get(key);
6432
const index =
6433
cached !== undefined &&
6434
(yield* latestThreadEventSequence(threadId, cached.index.threadIds)) === cached.sequence
6435
? cached.index
6439
- : yield* readTimelineIndex(threadId, new Set(), includePersistentOutput);
6436
+ : yield* readTimelineIndex(threadId, new Set(), includeAttempts);
6437
const sequence =
6438
index === cached?.index
6439
? cached.sequence
@@ -6478,32 +6475,89 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6475
position,
6476
canonicalPosition: position,
6477
}));
6481
- const historyItems = workMode === "deferred" ? deferWorkHistoryRows(indexed) : indexed;
6482
- const selection = yield* Effect.try({
6483
- try: () => {
6484
- const policy = olderHistoryPagePolicy({ items: indexed, cursor, throughEntryId });
6485
- const page =
6478
+ const { olderPolicy, cursorPosition } = yield* Effect.try({
6479
+ try: () => ({
6480
+ olderPolicy: olderHistoryPagePolicy({ items: indexed, cursor, throughEntryId }),
6481
+ // The cursor's own row, or the row its stale position falls back to.
6482
+ cursorPosition: Math.min(
6483
+ findCursorEndIndex(indexed, decodeThreadHistoryCursor(cursor)),
6484
+ indexed.length - 1,
6485
+ ),
6486
+ }),
6487
+ catch: (cause) => new ProjectionStoreReadError({ threadId, cause }),
6488
+ });
6489
+ const selectPage = (rows: typeof indexed) =>
6490
+ Effect.try({
6491
+ try: () =>
6492
workMode === "work"
6493
? selectDeferredWorkPage({
6488
- items: indexed,
6494
+ items: rows,
6495
cursor,
6496
throughEntryId,
6497
snapshotSequence,
6498
rowEncodedBytes: () => 0,
6499
})
6500
: selectHistoryPageFromCursor({
6495
- items: historyItems,
6501
+ items: workMode === "deferred" ? deferWorkHistoryRows(rows) : rows,
6502
cursor,
6503
snapshotSequence,
6504
rowEncodedBytes: () => 0,
6499
- policy,
6500
- });
6501
- return { policy, page };
6502
- },
6503
- catch: (cause) => new ProjectionStoreReadError({ threadId, cause }),
6504
- });
6505
- const { page } = selection;
6506
- const policy = workMode === "work" ? THREAD_HISTORY_PAGE_POLICY : selection.policy;
6505
+ policy: olderPolicy,
6506
+ }),
6507
+ catch: (cause) => new ProjectionStoreReadError({ threadId, cause }),
6508
+ });
6509
+ let page = yield* selectPage(indexed);
6510
+ // Inline output is found by scanning payloads, which costs a whole long thread several
6511
+ // seconds. Scan only the runs the page spans, then select again with what they hold.
6512
+ const scannedRuns = new Set<string>();
6513
+ const persistentOutputs = new Set<string>();
6514
+ while (workMode !== undefined) {
6515
+ // Rows hidden in a work group move the page, so the scan always reaches the cursor.
6516
+ const span = [...page.items.slice(0, 1), ...page.items.slice(-1)].map(
6517
+ (row) => row.canonicalPosition,
6518
+ );
6519
+ if (cursorPosition >= 0) span.push(cursorPosition);
6520
+ if (span.length === 0) break;
6521
+ const runs = new Map(
6522
+ // A work page holds rows of one run, which can be the rows that have no run.
6523
+ (workMode === "work"
6524
+ ? page.items.slice(0, 1)
6525
+ : indexed.slice(Math.min(...span), Math.max(...span) + 1)
6526
+ ).flatMap(({ sourceThreadId, item }) =>
6527
+ (workMode === "work" ||
6528
+ (item.runId !== null &&
6529
+ (item.runStatus === "completed" || item.attemptStatus === "superseded"))) &&
6530
+ !scannedRuns.has(`${sourceThreadId}:${item.runId}`)
6531
+ ? [
6532
+ [
6533
+ `${sourceThreadId}:${item.runId}`,
6534
+ { threadId: sourceThreadId, id: item.runId },
6535
+ ] as const,
6536
+ ]
6537
+ : [],
6538
+ ),
6539
+ );
6540
+ if (runs.size === 0) break;
6541
+ for (const run of runs.keys()) scannedRuns.add(run);
6542
+ const found = yield* sql<{ thread_id: string; turn_item_id: string }>`
6543
+ SELECT item.thread_id, item.turn_item_id
6544
+ FROM json_each(${yield* encodeTimelineSources([...runs.values()])}) AS wanted
6545
+ CROSS JOIN orchestration_v2_projection_turn_items AS item
6546
+ ON item.thread_id = json_extract(wanted.value, '$.threadId')
6547
+ AND item.run_id IS json_extract(wanted.value, '$.id')
6548
+ WHERE item.status = 'completed' AND ${inlineWorkOutput}`;
6549
+ if (found.length === 0) break;
6550
+ for (const row of found)
6551
+ persistentOutputs.add(`${row.thread_id}:${row.turn_item_id}`);
6552
+ page = yield* selectPage(
6553
+ indexed.map((row) =>
6554
+ persistentOutputs.has(`${row.sourceThreadId}:${row.sourceItemId}`)
6555
+ ? { ...row, item: { ...row.item, persistentOutput: true } }
6556
+ : row,
6557
+ ),
6558
+ );
6559
+ }
6560
+ const policy = workMode === "work" ? THREAD_HISTORY_PAGE_POLICY : olderPolicy;
6561
const end = (page.items.at(-1)?.canonicalPosition ?? -1) + 1;
6562
const hasUserTurns = index.visible
6563
.slice(0, end)
apps/server/src/orchestration-v2/threadHistoryPaging.ts
+1
-1
@@ -394,7 +394,7 @@ export function selectRecentTimelineWindow(input: {
394
});
395
}
396
397
-function findCursorEndIndex(
397
+export function findCursorEndIndex(
398
items: ReadonlyArray<HistoryRow>,
399
cursor: ThreadHistoryCursorPayload,
400
): number {