From 8ba4bdb1f6a96979ab0c00335334d210750b3b76 Mon Sep 17 00:00:00 2001 From: Aaron Queen Date: Thu, 8 Oct 2026 23:12:24 -0600 Subject: [PATCH 1/6] perf(server): skip hidden ancestor payloads in full fork pages --- .../orchestration-v2/ProjectionStore.test.ts | 136 +++++++++++++ .../src/orchestration-v2/ProjectionStore.ts | 178 +++++++++++++----- 2 files changed, 264 insertions(+), 50 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index e8a3c1bcfc08..bc2b8b18570b 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -4695,6 +4695,142 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { }), ); + it.effect("preserves the cursor when local turns fill a fork snapshot", () => + Effect.gen(function* () { + const store = yield* ProjectionStore.ProjectionStoreV2; + const sql = yield* SqlClient.SqlClient; + const now = DateTime.makeUnsafe("2026-10-01T00:00:00.000Z"); + const parentId = ThreadId.make("thread:counted-fork:parent"); + const childId = ThreadId.make("thread:counted-fork:child"); + const parentRunId = RunId.make("run:counted-fork:parent"); + for (const threadId of [parentId, childId]) { + const runId = threadId === parentId ? parentRunId : RunId.make("run:counted-fork:child"); + const nodeId = NodeId.make(`node:${threadId}`); + yield* store.apply({ + id: EventId.make(`event:${threadId}`), + type: "thread.created", + threadId, + occurredAt: now, + payload: { + createdBy: "user", + creationSource: "web", + id: threadId, + projectId: ProjectId.make("project:counted-fork"), + title: "Counted fork", + providerInstanceId, + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + activeProviderThreadId: null, + lineage: { + parentThreadId: threadId === parentId ? null : parentId, + relationshipToParent: threadId === parentId ? null : "fork", + rootThreadId: parentId, + }, + forkedFrom: + threadId === parentId + ? null + : { type: "run", threadId: parentId, runId: parentRunId }, + createdAt: now, + updatedAt: now, + archivedAt: null, + settledOverride: null, + settledAt: null, + lastVisitedAt: null, + deletedAt: null, + }, + }); + yield* store.apply({ + id: EventId.make(`event:${runId}`), + type: "run.created", + threadId, + runId, + nodeId, + driver, + occurredAt: now, + payload: { + id: runId, + threadId, + ordinal: 1, + providerInstanceId, + modelSelection, + providerThreadId: null, + userMessageId: MessageId.make(`message:${runId}`), + rootNodeId: nodeId, + activeAttemptId: null, + status: "completed", + requestedAt: now, + startedAt: now, + completedAt: now, + checkpointId: null, + contextHandoffId: null, + }, + }); + const turns = threadId === parentId ? 4 : 12; + for (let ordinal = 0; ordinal < turns; ordinal++) { + yield* store.apply({ + id: EventId.make(`event:${threadId}:${ordinal}`), + type: "turn-item.updated", + threadId, + runId, + driver, + occurredAt: now, + payload: { + id: TurnItemId.make(`item:${threadId}:${ordinal}`), + threadId, + runId, + nodeId: null, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal, + status: "completed", + title: null, + startedAt: now, + completedAt: now, + updatedAt: now, + type: "user_message", + messageId: MessageId.make(`message:${threadId}:${ordinal}`), + inputIntent: "turn_start", + createdBy: "user", + creationSource: "web", + text: `Turn ${ordinal}`, + attachments: [], + }, + }); + } + } + // A fork keeps its inherited prefix even after its source run rolls back. + yield* sql`UPDATE orchestration_v2_projection_runs + SET status = 'rolled_back', payload_json = json_set(payload_json, '$.status', 'rolled_back') + WHERE run_id = ${parentRunId}`; + const full = yield* store.getThreadSnapshot(childId); + const expected = buildBoundedThreadProjection({ + projection: full.projection, + snapshotSequence: full.snapshotSequence, + }); + const snapshot = yield* store.getThreadSnapshotWindow(childId, { + rowLimit: 77, + userTurnLimit: 10, + }); + const actual = buildBoundedThreadProjection({ + projection: snapshot.projection, + snapshotSequence: snapshot.snapshotSequence, + }); + assert.deepEqual(actual, expected); + assert.isNotNull(actual.historyCursor); + assert.equal(decodeThreadHistoryCursor(actual.historyCursor!).p, 7); + const older = yield* store.getThreadHistoryPage(childId, actual.historyCursor!); + assert.deepEqual( + [...older.items, ...actual.projection.visibleTurnItems].map((row) => row.sourceItemId), + full.projection.visibleTurnItems.map((row) => row.sourceItemId), + ); + }), + ); + it.effect("a pull request watch keeps a finished thread working until it ends", () => Effect.gen(function* () { const store = yield* ProjectionStore.ProjectionStoreV2; diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index a6a092ff84b7..c92859cde2f9 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -1223,8 +1223,9 @@ function withLocalVisibleTurnItems( function renumberVisibleTurnItems( rows: ReadonlyArray>, + offset = 0, ): Array { - return rows.map((row, position) => ({ ...row, position })); + return rows.map((row, position) => ({ ...row, position: position + offset })); } function makeForkMarkerTurnItem(input: { @@ -1312,16 +1313,23 @@ function visibleTurnItemsThroughRun(input: { function buildVisibleTurnItems(input: { readonly projection: OrchestrationV2ThreadProjection; readonly sourceProjection: OrchestrationV2ThreadProjection | null; + readonly inheritedItemCount?: number; }): Array { const forkedFrom = input.projection.thread.forkedFrom; - if (forkedFrom?.type !== "run" || input.sourceProjection === null) { + if ( + forkedFrom?.type !== "run" || + (input.sourceProjection === null && input.inheritedItemCount === undefined) + ) { return localVisibleTurnItems(input.projection); } - const inherited = visibleTurnItemsThroughRun({ - sourceProjection: input.sourceProjection, - sourceRunId: forkedFrom.runId, - }); + const inherited = + input.sourceProjection === null + ? [] + : visibleTurnItemsThroughRun({ + sourceProjection: input.sourceProjection, + sourceRunId: forkedFrom.runId, + }); const markerItem = makeForkMarkerTurnItem({ targetProjection: input.projection, sourceThreadId: forkedFrom.threadId, @@ -1334,16 +1342,19 @@ function buildVisibleTurnItems(input: { item: row.item, })); - return renumberVisibleTurnItems([ - ...inherited, - { - visibility: "synthetic", - sourceThreadId: forkedFrom.threadId, - sourceItemId: markerItem.id, - item: markerItem, - }, - ...local, - ]); + return renumberVisibleTurnItems( + [ + ...inherited, + { + visibility: "synthetic", + sourceThreadId: forkedFrom.threadId, + sourceItemId: markerItem.id, + item: markerItem, + }, + ...local, + ], + input.inheritedItemCount ?? 0, + ); } /** @@ -2677,45 +2688,16 @@ export const layer: Layer.Layer = ), ); - const readCanonicalProjection = ( + const readWindowTurnItemRows = ( threadId: ThreadId, - window?: { + window: { readonly rowLimit: number; readonly userTurnLimit?: number | undefined; readonly anchorItemId?: TurnItemId | undefined; readonly requiredRunId?: RunId | undefined; - readonly suppressLocal?: boolean | undefined; }, - fields?: ReadonlyArray, - filter?: ProjectionRecordFilter, - ) => - Effect.gen(function* () { - const threadRows = yield* sql` - SELECT payload_json - FROM orchestration_v2_projection_threads - WHERE thread_id = ${threadId} - LIMIT 1 - `; - const threadRow = threadRows[0]; - if (!threadRow) { - return yield* new ProjectionStoreThreadNotFoundError({ threadId }); - } - - const boundedTurnItemRows = - fields !== undefined && !fields.includes("turnItems") - ? [] - : window === undefined - ? yield* sql` - SELECT payload_json - FROM orchestration_v2_projection_turn_items - WHERE thread_id = ${threadId} - ${filter?.turnItemTypes === undefined ? sql`` : sql`AND type IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemTypes)}))`} - ${filter?.turnItemStatuses === undefined ? sql`` : sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemStatuses)}))`} - ${filter?.turnItemRunId === undefined ? sql`` : sql`AND run_id = ${filter.turnItemRunId}`} - ${filter?.turnItemRunIds === undefined ? sql`` : sql`AND (run_id IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemRunIds.filter((id): id is RunId => id !== null))})) OR (${filter.turnItemRunIds.includes(null) ? 1 : 0} = 1 AND run_id IS NULL))`} - ORDER BY ordinal ASC, turn_item_id ASC - ` - : yield* sql` + countOnly = false, + ) => sql` WITH eligible AS NOT MATERIALIZED ( SELECT item.rowid AS item_rowid, item.payload_json, item.ordinal, item.turn_item_id, item.run_id, item.node_id, item.type @@ -2841,12 +2823,70 @@ export const layer: Layer.Layer = -- CROSS JOIN keeps the sorted IDs as the outer loop, so SQLite -- skips sorting again once the payloads are attached. Carry the -- rowid through selection to avoid a second string-key index lookup. - SELECT item.payload_json + SELECT ${countOnly ? sql`'' AS payload_json, COUNT(*) AS retained_count` : sql`item.payload_json, 0 AS retained_count`} FROM retained CROSS JOIN orchestration_v2_projection_turn_items AS item ON item.rowid = retained.item_rowid + ${ + countOnly + ? sql`WHERE EXISTS ( + SELECT 1 FROM orchestration_v2_projection_runs + WHERE thread_id = ${threadId} AND run_id = ${window.requiredRunId ?? null} + ) AND ((item.run_id IS NULL AND EXISTS ( + SELECT 1 FROM orchestration_v2_projection_threads source + WHERE source.thread_id = ${threadId} + AND json_extract(source.payload_json, '$.historyOrigin') = 'v1_import' + )) OR item.run_id IN ( + SELECT run_id FROM orchestration_v2_projection_runs + WHERE thread_id = ${threadId} AND ordinal <= ( + SELECT ordinal FROM orchestration_v2_projection_runs + WHERE thread_id = ${threadId} AND run_id = ${window.requiredRunId ?? null} + ) + ))` + : sql`` + } ORDER BY retained.ordinal ASC, retained.turn_item_id ASC `; + + const readCanonicalProjection = ( + threadId: ThreadId, + window?: { + readonly rowLimit: number; + readonly userTurnLimit?: number | undefined; + readonly anchorItemId?: TurnItemId | undefined; + readonly requiredRunId?: RunId | undefined; + readonly suppressLocal?: boolean | undefined; + }, + fields?: ReadonlyArray, + filter?: ProjectionRecordFilter, + ) => + Effect.gen(function* () { + const threadRows = yield* sql` + SELECT payload_json + FROM orchestration_v2_projection_threads + WHERE thread_id = ${threadId} + LIMIT 1 + `; + const threadRow = threadRows[0]; + if (!threadRow) { + return yield* new ProjectionStoreThreadNotFoundError({ threadId }); + } + + const boundedTurnItemRows = + fields !== undefined && !fields.includes("turnItems") + ? [] + : window === undefined + ? yield* sql` + SELECT payload_json + FROM orchestration_v2_projection_turn_items + WHERE thread_id = ${threadId} + ${filter?.turnItemTypes === undefined ? sql`` : sql`AND type IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemTypes)}))`} + ${filter?.turnItemStatuses === undefined ? sql`` : sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemStatuses)}))`} + ${filter?.turnItemRunId === undefined ? sql`` : sql`AND run_id = ${filter.turnItemRunId}`} + ${filter?.turnItemRunIds === undefined ? sql`` : sql`AND (run_id IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemRunIds.filter((id): id is RunId => id !== null))})) OR (${filter.turnItemRunIds.includes(null) ? 1 : 0} = 1 AND run_id IS NULL))`} + ORDER BY ordinal ASC, turn_item_id ASC + ` + : yield* readWindowTurnItemRows(threadId, window); // Reuse the decoded items for cohort IDs and the resulting projection. // Parsing these rows separately duplicates every retained tool output. const turnItems = yield* decodeRows(decodeTurnItemPayload, threadId)(boundedTurnItemRows); @@ -3416,6 +3456,44 @@ export const layer: Layer.Layer = }; }); + // When local turns fill the page, only the parent's row count affects + // the cursor. Nested forks keep the existing ancestry read. + if ( + window?.userTurnLimit !== undefined && + window.userTurnLimit > 0 && + seenThreadIds.size === 0 && + window.historyAnchor === undefined && + sourceWindow !== undefined && + activeLocalTurnItems(projection).filter((row) => isThreadHistoryUserTurn(row.item)) + .length >= Math.max(window.userTurnLimit, THREAD_HISTORY_PAGE_POLICY.maxUserTurns) + ) { + const sourceThreadRows = yield* sql` + SELECT payload_json FROM orchestration_v2_projection_threads + WHERE thread_id = ${forkedFrom.threadId} LIMIT 1 + `; + if (sourceThreadRows[0] !== undefined) { + const sourceThread = yield* decodeThreadPayload(sourceThreadRows[0].payload_json); + if (sourceThread.forkedFrom?.type !== "run") { + const counts = yield* readWindowTurnItemRows( + forkedFrom.threadId, + { + ...sourceWindow, + rowLimit: sourceWindow.suppressLocal ? 0 : sourceWindow.rowLimit, + }, + true, + ); + return { + ...projection, + visibleTurnItems: buildVisibleTurnItems({ + projection, + sourceProjection: null, + inheritedItemCount: counts[0]?.retained_count ?? 0, + }), + }; + } + } + } + const sourceProjection = yield* readProjection( forkedFrom.threadId, new Set([...seenThreadIds, threadId]), From 0c6af1ebb6ea8e97226b6bf501a68a803599d929 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 15 Oct 2026 15:05:12 -0700 Subject: [PATCH 2/6] test(server): prove filled fork pages match the full ancestor read Replace the PR's fork cursor test with one that compares the bounded snapshot of a fork against the full projection. With nine local turns the page still includes inherited parent rows and reads their bodies; with ten it skips them, and positions, the fork marker, cursor and hasMoreHistory still match the full read. Co-Authored-By: Claude Opus 5.5 --- .../orchestration-v2/ProjectionStore.test.ts | 173 +++++++++++------- 1 file changed, 102 insertions(+), 71 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index e4a29d1c420e..9aa556107721 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -9,6 +9,7 @@ import { MessageId, type ModelSelection, type OrchestrationV2ProviderThread, + type OrchestrationV2ThreadProjection, type OrchestrationV2ThreadShellSnapshot, NodeId, ProjectId, @@ -40,6 +41,7 @@ import { decodeThreadHistoryCursor, selectHistoryPageFromCursor, THREAD_HISTORY_PAGE_POLICY, + THREAD_HISTORY_SNAPSHOT_ROW_LIMIT, } from "./threadHistoryPaging.ts"; const layerTest = Layer.mergeAll( @@ -5660,18 +5662,15 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { }), ); - it.effect("preserves the cursor when local turns fill a fork snapshot", () => + it.effect("skips parent bodies once a fork's own turns fill its first page", () => Effect.gen(function* () { const store = yield* ProjectionStore.ProjectionStoreV2; - const sql = yield* SqlClient.SqlClient; const now = DateTime.makeUnsafe("2026-10-01T00:00:00.000Z"); - const parentId = ThreadId.make("thread:counted-fork:parent"); - const childId = ThreadId.make("thread:counted-fork:child"); - const parentRunId = RunId.make("run:counted-fork:parent"); - for (const threadId of [parentId, childId]) { - const runId = threadId === parentId ? parentRunId : RunId.make("run:counted-fork:child"); - const nodeId = NodeId.make(`node:${threadId}`); - yield* store.apply({ + const parentId = ThreadId.make("thread:filled-fork:parent"); + const forkId = ThreadId.make("thread:filled-fork:fork"); + const parentText = "parent prompt"; + const createThread = (threadId: ThreadId, forkedFromRunId: RunId | null) => + store.apply({ id: EventId.make(`event:${threadId}`), type: "thread.created", threadId, @@ -5680,8 +5679,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { createdBy: "user", creationSource: "web", id: threadId, - projectId: ProjectId.make("project:counted-fork"), - title: "Counted fork", + projectId: ProjectId.make("project:filled-fork"), + title: "Filled fork", providerInstanceId, modelSelection, runtimeMode: "full-access", @@ -5690,14 +5689,14 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { worktreePath: null, activeProviderThreadId: null, lineage: { - parentThreadId: threadId === parentId ? null : parentId, - relationshipToParent: threadId === parentId ? null : "fork", + parentThreadId: forkedFromRunId === null ? null : parentId, + relationshipToParent: forkedFromRunId === null ? null : "fork", rootThreadId: parentId, }, forkedFrom: - threadId === parentId + forkedFromRunId === null ? null - : { type: "run", threadId: parentId, runId: parentRunId }, + : { type: "run", threadId: parentId, runId: forkedFromRunId }, createdAt: now, updatedAt: now, archivedAt: null, @@ -5707,6 +5706,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { deletedAt: null, }, }); + // Each turn is one run with one user prompt. + const addTurn = Effect.fnUntraced(function* (threadId: ThreadId, turn: number, text: string) { + const runId = RunId.make(`run:${threadId}:${turn}`); + const nodeId = NodeId.make(`node:${threadId}:${turn}`); yield* store.apply({ id: EventId.make(`event:${runId}`), type: "run.created", @@ -5718,7 +5721,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { payload: { id: runId, threadId, - ordinal: 1, + ordinal: turn, providerInstanceId, modelSelection, providerThreadId: null, @@ -5733,66 +5736,94 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { contextHandoffId: null, }, }); - const turns = threadId === parentId ? 4 : 12; - for (let ordinal = 0; ordinal < turns; ordinal++) { - yield* store.apply({ - id: EventId.make(`event:${threadId}:${ordinal}`), - type: "turn-item.updated", + yield* store.apply({ + id: EventId.make(`event:${runId}:prompt`), + type: "turn-item.updated", + threadId, + runId, + driver, + occurredAt: now, + payload: { + id: TurnItemId.make(`item:${runId}`), threadId, runId, - driver, - occurredAt: now, - payload: { - id: TurnItemId.make(`item:${threadId}:${ordinal}`), - threadId, - runId, - nodeId: null, - providerThreadId: null, - providerTurnId: null, - nativeItemRef: null, - parentItemId: null, - ordinal, - status: "completed", - title: null, - startedAt: now, - completedAt: now, - updatedAt: now, - type: "user_message", - messageId: MessageId.make(`message:${threadId}:${ordinal}`), - inputIntent: "turn_start", - createdBy: "user", - creationSource: "web", - text: `Turn ${ordinal}`, - attachments: [], - }, - }); - } - } - // A fork keeps its inherited prefix even after its source run rolls back. - yield* sql`UPDATE orchestration_v2_projection_runs - SET status = 'rolled_back', payload_json = json_set(payload_json, '$.status', 'rolled_back') - WHERE run_id = ${parentRunId}`; - const full = yield* store.getThreadSnapshot(childId); - const expected = buildBoundedThreadProjection({ - projection: full.projection, - snapshotSequence: full.snapshotSequence, - }); - const snapshot = yield* store.getThreadSnapshotWindow(childId, { - rowLimit: 77, - userTurnLimit: 10, + nodeId: null, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: turn, + status: "completed", + title: null, + startedAt: now, + completedAt: now, + updatedAt: now, + type: "user_message", + messageId: MessageId.make(`message:${runId}`), + inputIntent: "turn_start", + createdBy: "user", + creationSource: "web", + text, + attachments: [], + }, + }); }); - const actual = buildBoundedThreadProjection({ - projection: snapshot.projection, - snapshotSequence: snapshot.snapshotSequence, + const firstPage = (projection: OrchestrationV2ThreadProjection) => { + const bounded = buildBoundedThreadProjection({ projection, snapshotSequence: 0 }); + return { + items: bounded.projection.visibleTurnItems, + historyCursor: bounded.historyCursor, + hasMoreHistory: bounded.hasMoreHistory, + }; + }; + // The initial window must be the tail of the full timeline, with the same + // positions, and bound to the same first page and cursor. + const readFirstPage = Effect.fnUntraced(function* () { + const full = (yield* store.getThreadSnapshot(forkId)).projection; + const { window, parsedParent } = yield* Effect.acquireUseRelease( + Effect.sync(() => vi.spyOn(JSON, "parse")), + (parse) => + store + .getThreadSnapshotWindow(forkId, { + rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT, + userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns, + }) + .pipe( + Effect.map(({ projection }) => ({ + window: projection, + parsedParent: parse.mock.calls.some(([text]) => text.includes(parentText)), + })), + ), + (parse) => Effect.sync(() => parse.mockRestore()), + ); + const visible = window.visibleTurnItems; + assert.deepEqual(visible, full.visibleTurnItems.slice(-visible.length)); + const page = firstPage(window); + assert.deepEqual(page, firstPage(full)); + return { page, parsedParent }; }); - assert.deepEqual(actual, expected); - assert.isNotNull(actual.historyCursor); - assert.equal(decodeThreadHistoryCursor(actual.historyCursor!).p, 7); - const older = yield* store.getThreadHistoryPage(childId, actual.historyCursor!); - assert.deepEqual( - [...older.items, ...actual.projection.visibleTurnItems].map((row) => row.sourceItemId), - full.projection.visibleTurnItems.map((row) => row.sourceItemId), + + yield* createThread(parentId, null); + for (const turn of [1, 2, 3]) yield* addTurn(parentId, turn, parentText); + // The parent kept going after the fork, so its third turn is not inherited. + yield* createThread(forkId, RunId.make(`run:${parentId}:2`)); + for (let turn = 1; turn <= 9; turn += 1) yield* addTurn(forkId, turn, "fork prompt"); + + // Nine local turns leave room for the parent's last inherited turn, so + // the window still reads parent bodies. + const partial = yield* readFirstPage(); + assert.include( + partial.page.items.map((row) => row.visibility), + "inherited", ); + assert.isTrue(partial.parsedParent); + + // Ten local turns fill the page: parent rows only shift positions and + // the cursor, so their bodies are not read. + yield* addTurn(forkId, 10, "fork prompt"); + const filled = yield* readFirstPage(); + assert.isNotNull(filled.page.historyCursor); + assert.isFalse(filled.parsedParent); }), ); From 88f4be166864ae2cc957124b07fb54a7d93b167e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 15 Oct 2026 16:43:13 -0700 Subject: [PATCH 3/6] perf(server): fork threads load faster when local turns fill the first page After #17387 an initial bounded snapshot of a forked thread still read and decoded its parent's retained turn items, including large tool outputs, even when the fork's own user turns filled the returned page. Those inherited rows are dropped during bounding; only their count sets the positions and the history cursor. Share the window selection between the payload read and a count, and when local turns fill the page count the parent's retained rows in SQL instead of reading their bodies. The count query returns NULL for a parent that is itself a run fork, which keeps the full ancestry read for nested forks. Closes #18915 Co-Authored-By: Claude Fable 5.1 --- .../src/orchestration-v2/ProjectionStore.ts | 113 ++++++++---------- 1 file changed, 53 insertions(+), 60 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 947d068cbd0e..5da46f4c978a 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -1397,12 +1397,13 @@ function visibleTurnItemsThroughRun(input: { function buildVisibleTurnItems(input: { readonly projection: OrchestrationV2ThreadProjection; readonly sourceProjection: OrchestrationV2ThreadProjection | null; - readonly inheritedItemCount?: number; + /** Inherited rows a window counted instead of reading; positions start after them. */ + readonly inheritedRowCount?: number; }): Array { const forkedFrom = input.projection.thread.forkedFrom; if ( forkedFrom?.type !== "run" || - (input.sourceProjection === null && input.inheritedItemCount === undefined) + (input.sourceProjection === null && input.inheritedRowCount === undefined) ) { return localVisibleTurnItems(input.projection); } @@ -1437,7 +1438,7 @@ function buildVisibleTurnItems(input: { }, ...local, ], - input.inheritedItemCount ?? 0, + input.inheritedRowCount ?? 0, ); } @@ -2773,7 +2774,11 @@ export const layer: Layer.Layer = ), ); - const readWindowTurnItemRows = ( + // The rows a bounded window keeps for one thread: the newest user turns up + // to the limit (rows up to the limit when nothing starts a turn), the + // interrupt requests they depend on, and the latest row. Ends with the + // `retained` CTE so a read can fetch the payloads or only count the rows. + const retainedWindowRows = ( threadId: ThreadId, window: { readonly rowLimit: number; @@ -2781,8 +2786,7 @@ export const layer: Layer.Layer = readonly anchorItemId?: TurnItemId | undefined; readonly requiredRunId?: RunId | undefined; }, - countOnly = false, - ) => sql` + ) => sql` WITH eligible AS NOT MATERIALIZED ( SELECT item.rowid AS item_rowid, item.payload_json, item.ordinal, item.turn_item_id, item.run_id, item.node_id, item.type @@ -2947,32 +2951,6 @@ export const layer: Layer.Layer = ) AS latest ORDER BY ordinal ASC, turn_item_id ASC ) - -- CROSS JOIN keeps the sorted IDs as the outer loop, so SQLite - -- skips sorting again once the payloads are attached. Carry the - -- rowid through selection to avoid a second string-key index lookup. - SELECT ${countOnly ? sql`'' AS payload_json, COUNT(*) AS retained_count` : sql`item.payload_json, 0 AS retained_count`} - FROM retained - CROSS JOIN orchestration_v2_projection_turn_items AS item - ON item.rowid = retained.item_rowid - ${ - countOnly - ? sql`WHERE EXISTS ( - SELECT 1 FROM orchestration_v2_projection_runs - WHERE thread_id = ${threadId} AND run_id = ${window.requiredRunId ?? null} - ) AND ((item.run_id IS NULL AND EXISTS ( - SELECT 1 FROM orchestration_v2_projection_threads source - WHERE source.thread_id = ${threadId} - AND json_extract(source.payload_json, '$.historyOrigin') = 'v1_import' - )) OR item.run_id IN ( - SELECT run_id FROM orchestration_v2_projection_runs - WHERE thread_id = ${threadId} AND ordinal <= ( - SELECT ordinal FROM orchestration_v2_projection_runs - WHERE thread_id = ${threadId} AND run_id = ${window.requiredRunId ?? null} - ) - ))` - : sql`` - } - ORDER BY retained.ordinal ASC, retained.turn_item_id ASC `; const readCanonicalProjection = ( @@ -3013,7 +2991,17 @@ export const layer: Layer.Layer = ${filter?.turnItemRunIds === undefined ? sql`` : sql`AND (run_id IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemRunIds.filter((id): id is RunId => id !== null))})) OR (${filter.turnItemRunIds.includes(null) ? 1 : 0} = 1 AND run_id IS NULL))`} ORDER BY ordinal ASC, turn_item_id ASC ` - : yield* readWindowTurnItemRows(threadId, window); + : yield* sql` + ${retainedWindowRows(threadId, window)} + -- CROSS JOIN keeps the sorted IDs as the outer loop, so SQLite + -- skips sorting again once the payloads are attached. Carry the + -- rowid through selection to avoid a second string-key index lookup. + SELECT item.payload_json + FROM retained + CROSS JOIN orchestration_v2_projection_turn_items AS item + ON item.rowid = retained.item_rowid + ORDER BY retained.ordinal ASC, retained.turn_item_id ASC + `; // Reuse the decoded items for cohort IDs and the resulting projection. // Parsing these rows separately duplicates every retained tool output. const turnItems = yield* decodeRows(decodeTurnItemPayload, threadId)(boundedTurnItemRows); @@ -3597,41 +3585,46 @@ export const layer: Layer.Layer = }; }); - // When local turns fill the page, only the parent's row count affects - // the cursor. Nested forks keep the existing ancestry read. + // A fork whose own turns fill its first page shows no inherited row + // there, but the inherited count still sets the page's positions and + // history cursor. Count the parent's retained rows instead of reading + // and decoding their bodies. The count mirrors visibleTurnItemsThroughRun + // for a parent with no inherited rows of its own: a parent that is itself + // a run fork returns NULL and keeps the full ancestry read. if ( window?.userTurnLimit !== undefined && - window.userTurnLimit > 0 && - seenThreadIds.size === 0 && window.historyAnchor === undefined && + seenThreadIds.size === 0 && sourceWindow !== undefined && activeLocalTurnItems(projection).filter((row) => isThreadHistoryUserTurn(row.item)) .length >= Math.max(window.userTurnLimit, THREAD_HISTORY_PAGE_POLICY.maxUserTurns) ) { - const sourceThreadRows = yield* sql` - SELECT payload_json FROM orchestration_v2_projection_threads - WHERE thread_id = ${forkedFrom.threadId} LIMIT 1 + const counted = yield* sql<{ readonly inherited_rows: number | null }>` + ${retainedWindowRows(forkedFrom.threadId, { + ...sourceWindow, + rowLimit: sourceWindow.suppressLocal ? 0 : sourceWindow.rowLimit, + })} + SELECT CASE + WHEN json_extract(source.payload_json, '$.forkedFrom.type') = 'run' THEN NULL + WHEN NOT EXISTS ( + SELECT 1 FROM orchestration_v2_projection_runs + WHERE thread_id = source.thread_id AND run_id = ${forkedFrom.runId} + ) THEN 0 + ELSE (SELECT COUNT(*) FROM retained) + END AS inherited_rows + FROM orchestration_v2_projection_threads AS source + WHERE source.thread_id = ${forkedFrom.threadId} `; - if (sourceThreadRows[0] !== undefined) { - const sourceThread = yield* decodeThreadPayload(sourceThreadRows[0].payload_json); - if (sourceThread.forkedFrom?.type !== "run") { - const counts = yield* readWindowTurnItemRows( - forkedFrom.threadId, - { - ...sourceWindow, - rowLimit: sourceWindow.suppressLocal ? 0 : sourceWindow.rowLimit, - }, - true, - ); - return { - ...projection, - visibleTurnItems: buildVisibleTurnItems({ - projection, - sourceProjection: null, - inheritedItemCount: counts[0]?.retained_count ?? 0, - }), - }; - } + const inheritedRowCount = counted[0]?.inherited_rows ?? null; + if (inheritedRowCount !== null) { + return { + ...projection, + visibleTurnItems: buildVisibleTurnItems({ + projection, + sourceProjection: null, + inheritedRowCount, + }), + }; } } From 0c27137888fdc204e2c1359f3f1bc1e7578c2824 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 15 Oct 2026 16:45:05 -0700 Subject: [PATCH 4/6] test(server): nested forks keep the full ancestry read A fork of a fork inherits the grandparent's rows through its parent, so counting only the parent's own rows would misplace the page. Add the nested case to the filled fork page test; it fails when the count query stops excluding run-forked parents. Co-Authored-By: Claude Fable 5.1 --- .../orchestration-v2/ProjectionStore.test.ts | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index 9aa556107721..e00ee60c0b13 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -5669,7 +5669,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { const parentId = ThreadId.make("thread:filled-fork:parent"); const forkId = ThreadId.make("thread:filled-fork:fork"); const parentText = "parent prompt"; - const createThread = (threadId: ThreadId, forkedFromRunId: RunId | null) => + const createThread = ( + threadId: ThreadId, + forkedFrom: { readonly threadId: ThreadId; readonly runId: RunId } | null, + ) => store.apply({ id: EventId.make(`event:${threadId}`), type: "thread.created", @@ -5689,14 +5692,11 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { worktreePath: null, activeProviderThreadId: null, lineage: { - parentThreadId: forkedFromRunId === null ? null : parentId, - relationshipToParent: forkedFromRunId === null ? null : "fork", + parentThreadId: forkedFrom?.threadId ?? null, + relationshipToParent: forkedFrom === null ? null : "fork", rootThreadId: parentId, }, - forkedFrom: - forkedFromRunId === null - ? null - : { type: "run", threadId: parentId, runId: forkedFromRunId }, + forkedFrom: forkedFrom === null ? null : { type: "run", ...forkedFrom }, createdAt: now, updatedAt: now, archivedAt: null, @@ -5778,13 +5778,13 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { }; // The initial window must be the tail of the full timeline, with the same // positions, and bound to the same first page and cursor. - const readFirstPage = Effect.fnUntraced(function* () { - const full = (yield* store.getThreadSnapshot(forkId)).projection; + const readFirstPage = Effect.fnUntraced(function* (threadId: ThreadId) { + const full = (yield* store.getThreadSnapshot(threadId)).projection; const { window, parsedParent } = yield* Effect.acquireUseRelease( Effect.sync(() => vi.spyOn(JSON, "parse")), (parse) => store - .getThreadSnapshotWindow(forkId, { + .getThreadSnapshotWindow(threadId, { rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT, userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns, }) @@ -5806,12 +5806,12 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { yield* createThread(parentId, null); for (const turn of [1, 2, 3]) yield* addTurn(parentId, turn, parentText); // The parent kept going after the fork, so its third turn is not inherited. - yield* createThread(forkId, RunId.make(`run:${parentId}:2`)); + yield* createThread(forkId, { threadId: parentId, runId: RunId.make(`run:${parentId}:2`) }); for (let turn = 1; turn <= 9; turn += 1) yield* addTurn(forkId, turn, "fork prompt"); // Nine local turns leave room for the parent's last inherited turn, so // the window still reads parent bodies. - const partial = yield* readFirstPage(); + const partial = yield* readFirstPage(forkId); assert.include( partial.page.items.map((row) => row.visibility), "inherited", @@ -5821,9 +5821,18 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { // Ten local turns fill the page: parent rows only shift positions and // the cursor, so their bodies are not read. yield* addTurn(forkId, 10, "fork prompt"); - const filled = yield* readFirstPage(); + const filled = yield* readFirstPage(forkId); assert.isNotNull(filled.page.historyCursor); assert.isFalse(filled.parsedParent); + + // A fork of the fork inherits the grandparent's rows through its parent, + // and its parent's own ten turns fill a page too. Counting rows at either + // level would misplace the page, so a nested fork keeps the full read. + const nestedId = ThreadId.make("thread:filled-fork:nested"); + yield* createThread(nestedId, { threadId: forkId, runId: RunId.make(`run:${forkId}:10`) }); + for (let turn = 1; turn <= 10; turn += 1) yield* addTurn(nestedId, turn, "nested prompt"); + const nested = yield* readFirstPage(nestedId); + assert.isTrue(nested.parsedParent); }), ); From e7bdede49428e15a97a2e4110fd9c65b75efcc1c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 19 Oct 2026 02:31:53 +0000 Subject: [PATCH 5/6] test(server): simplify filled fork page regression --- .../orchestration-v2/ProjectionStore.test.ts | 84 +++++++------------ 1 file changed, 32 insertions(+), 52 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index 0f741127e49d..5829ddc7a8a0 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -9,7 +9,6 @@ import { MessageId, type ModelSelection, type OrchestrationV2ProviderThread, - type OrchestrationV2ThreadProjection, type OrchestrationV2ThreadShellSnapshot, NodeId, ProjectId, @@ -5874,7 +5873,6 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { deletedAt: null, }, }); - // Each turn is one run with one user prompt. const addTurn = Effect.fnUntraced(function* (threadId: ThreadId, turn: number, text: string) { const runId = RunId.make(`run:${threadId}:${turn}`); const nodeId = NodeId.make(`node:${threadId}:${turn}`); @@ -5936,71 +5934,53 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { }, }); }); - const firstPage = (projection: OrchestrationV2ThreadProjection) => { - const bounded = buildBoundedThreadProjection({ projection, snapshotSequence: 0 }); - return { - items: bounded.projection.visibleTurnItems, - historyCursor: bounded.historyCursor, - hasMoreHistory: bounded.hasMoreHistory, - }; - }; - // The initial window must be the tail of the full timeline, with the same - // positions, and bound to the same first page and cursor. - const readFirstPage = Effect.fnUntraced(function* (threadId: ThreadId) { - const full = (yield* store.getThreadSnapshot(threadId)).projection; - const { window, parsedParent } = yield* Effect.acquireUseRelease( + const readFirstPage = Effect.fnUntraced(function* ( + threadId: ThreadId, + parsesParent: boolean, + ) { + const full = yield* store.getThreadSnapshot(threadId); + // Measure only the window read; the full snapshot is the pagination oracle. + const parse = yield* Effect.acquireRelease( Effect.sync(() => vi.spyOn(JSON, "parse")), - (parse) => - store - .getThreadSnapshotWindow(threadId, { - rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT, - userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns, - }) - .pipe( - Effect.map(({ projection }) => ({ - window: projection, - parsedParent: parse.mock.calls.some(([text]) => text.includes(parentText)), - })), - ), - (parse) => Effect.sync(() => parse.mockRestore()), + (spy) => Effect.sync(() => spy.mockRestore()), ); - const visible = window.visibleTurnItems; - assert.deepEqual(visible, full.visibleTurnItems.slice(-visible.length)); - const page = firstPage(window); - assert.deepEqual(page, firstPage(full)); - return { page, parsedParent }; - }); + const window = yield* store.getThreadSnapshotWindow(threadId, { + rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT, + userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns, + }); + assert.equal( + parse.mock.calls.some(([text]) => text.includes(parentText)), + parsesParent, + ); + const page = buildBoundedThreadProjection(window); + assert.deepEqual(page, buildBoundedThreadProjection(full)); + assert.isTrue(page.hasMoreHistory); + assert.isNotNull(page.historyCursor); + return page.projection.visibleTurnItems; + }, Effect.scoped); yield* createThread(parentId, null); - for (const turn of [1, 2, 3]) yield* addTurn(parentId, turn, parentText); - // The parent kept going after the fork, so its third turn is not inherited. + for (const turn of [1, 2, 3]) yield* addTurn(parentId, turn, `${parentText} ${turn}`); + // The third parent turn is after the fork boundary and must stay excluded. yield* createThread(forkId, { threadId: parentId, runId: RunId.make(`run:${parentId}:2`) }); for (let turn = 1; turn <= 9; turn += 1) yield* addTurn(forkId, turn, "fork prompt"); - - // Nine local turns leave room for the parent's last inherited turn, so - // the window still reads parent bodies. - const partial = yield* readFirstPage(forkId); + const partial = yield* readFirstPage(forkId, true); assert.include( - partial.page.items.map((row) => row.visibility), + partial.map((row) => row.visibility), "inherited", ); - assert.isTrue(partial.parsedParent); - // Ten local turns fill the page: parent rows only shift positions and - // the cursor, so their bodies are not read. yield* addTurn(forkId, 10, "fork prompt"); - const filled = yield* readFirstPage(forkId); - assert.isNotNull(filled.page.historyCursor); - assert.isFalse(filled.parsedParent); + const filled = yield* readFirstPage(forkId, false); + assert.notInclude( + filled.map((row) => row.visibility), + "inherited", + ); - // A fork of the fork inherits the grandparent's rows through its parent, - // and its parent's own ten turns fill a page too. Counting rows at either - // level would misplace the page, so a nested fork keeps the full read. const nestedId = ThreadId.make("thread:filled-fork:nested"); yield* createThread(nestedId, { threadId: forkId, runId: RunId.make(`run:${forkId}:10`) }); for (let turn = 1; turn <= 10; turn += 1) yield* addTurn(nestedId, turn, "nested prompt"); - const nested = yield* readFirstPage(nestedId); - assert.isTrue(nested.parsedParent); + yield* readFirstPage(nestedId, true); }), ); From b2e45497248b1f51a2b5d56d7ea5d6d0d966d4ad Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 19 Oct 2026 02:35:42 +0000 Subject: [PATCH 6/6] chore(server): simplify fork paging comments --- .../server/src/orchestration-v2/ProjectionStore.ts | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 72a771925a4a..46486bf20ca5 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -1385,7 +1385,6 @@ function visibleTurnItemsThroughRun(input: { function buildVisibleTurnItems(input: { readonly projection: OrchestrationV2ThreadProjection; readonly sourceProjection: OrchestrationV2ThreadProjection | null; - /** Inherited rows a window counted instead of reading; positions start after them. */ readonly inheritedRowCount?: number; }): Array { const forkedFrom = input.projection.thread.forkedFrom; @@ -2742,10 +2741,7 @@ export const layer: Layer.Layer = ), ); - // The rows a bounded window keeps for one thread: the newest user turns up - // to the limit (rows up to the limit when nothing starts a turn), the - // interrupt requests they depend on, and the latest row. Ends with the - // `retained` CTE so a read can fetch the payloads or only count the rows. + // Share selection so counting discarded ancestry preserves payload-read positions. const retainedWindowRows = ( threadId: ThreadId, window: { @@ -3553,12 +3549,8 @@ export const layer: Layer.Layer = }; }); - // A fork whose own turns fill its first page shows no inherited row - // there, but the inherited count still sets the page's positions and - // history cursor. Count the parent's retained rows instead of reading - // and decoding their bodies. The count mirrors visibleTurnItemsThroughRun - // for a parent with no inherited rows of its own: a parent that is itself - // a run fork returns NULL and keeps the full ancestry read. + // Filled local pages need only the inherited count for cursor positions. + // Run-forked parents still need the recursive read to include their ancestry. if ( window?.userTurnLimit !== undefined && window.historyAnchor === undefined &&