TypeScript
perf(server): fork threads load faster when local turns fill the first page (#20104)
Julius Marmingec7f8404d2ead
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.tsindex 64635a75d9d..35581cde540 100644--- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts+++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts@@ -42,6 +42,7 @@ import {decodeThreadHistoryCursor,selectHistoryPageFromCursor,THREAD_HISTORY_PAGE_POLICY,+ THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,} from "./threadHistoryPaging.ts";const layerTest = Layer.mergeAll(@@ -6074,6 +6075,161 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {}),);+ it.effect("skips parent bodies once a fork's own turns fill its first page", () =>+ Effect.gen(function* () {+ const store = yield* ProjectionStore.ProjectionStoreV2;+ const now = DateTime.makeUnsafe("2026-10-01T00:00:00.000Z");+ const parentId = ThreadId.make("thread:filled-fork:parent");+ const forkId = ThreadId.make("thread:filled-fork:fork");+ const parentText = "parent prompt";+ const createThread = (+ threadId: ThreadId,+ forkedFrom: { readonly threadId: ThreadId; readonly runId: RunId } | null,+ ) =>+ store.apply({+ id: EventId.make(`event:${threadId}`),+ type: "thread.created",+ threadId,+ occurredAt: now,+ payload: {+ createdBy: "user",+ creationSource: "web",+ id: threadId,+ projectId: ProjectId.make("project:filled-fork"),+ title: "Filled fork",+ providerInstanceId,+ modelSelection,+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ activeProviderThreadId: null,+ lineage: {+ parentThreadId: forkedFrom?.threadId ?? null,+ relationshipToParent: forkedFrom === null ? null : "fork",+ rootThreadId: parentId,+ },+ forkedFrom: forkedFrom === null ? null : { type: "run", ...forkedFrom },+ createdAt: now,+ updatedAt: now,+ archivedAt: null,+ settledOverride: null,+ settledAt: null,+ lastVisitedAt: null,+ deletedAt: null,+ },+ });+ 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",+ threadId,+ runId,+ nodeId,+ driver,+ occurredAt: now,+ payload: {+ id: runId,+ threadId,+ ordinal: turn,+ 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,+ },+ });+ 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,+ 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 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")),+ (spy) => Effect.sync(() => spy.mockRestore()),+ );+ 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} ${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");+ const partial = yield* readFirstPage(forkId, true);+ assert.include(+ partial.map((row) => row.visibility),+ "inherited",+ );++ yield* addTurn(forkId, 10, "fork prompt");+ const filled = yield* readFirstPage(forkId, false);+ assert.notInclude(+ filled.map((row) => row.visibility),+ "inherited",+ );++ 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");+ yield* readFirstPage(nestedId, true);+ }),+ );+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.tsindex 71251df7176..8448ff8268e 100644--- a/apps/server/src/orchestration-v2/ProjectionStore.ts+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts@@ -1339,8 +1339,9 @@ function withLocalVisibleTurnItems(function renumberVisibleTurnItems(rows: ReadonlyArray<Omit<OrchestrationV2ProjectedTurnItem, "position">>,+ offset = 0,): Array<OrchestrationV2ProjectedTurnItem> {- return rows.map((row, position) => ({ ...row, position }));+ return rows.map((row, position) => ({ ...row, position: position + offset }));}function makeForkMarkerTurnItem(input: {@@ -1428,16 +1429,23 @@ function visibleTurnItemsThroughRun(input: {function buildVisibleTurnItems(input: {readonly projection: OrchestrationV2ThreadProjection;readonly sourceProjection: OrchestrationV2ThreadProjection | null;+ readonly inheritedRowCount?: number;}): Array<OrchestrationV2ProjectedTurnItem> {const forkedFrom = input.projection.thread.forkedFrom;- if (forkedFrom?.type !== "run" || input.sourceProjection === null) {+ if (+ forkedFrom?.type !== "run" ||+ (input.sourceProjection === null && input.inheritedRowCount === 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,@@ -1450,16 +1458,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.inheritedRowCount ?? 0,+ );}function pendingRuntimeRequestSummary(@@ -2779,47 +2790,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =),);- const readCanonicalProjection = Effect.fn("ProjectionStore.readCanonicalProjection")(- function* (- threadId: ThreadId,- window?: {- readonly rowLimit: number;- readonly userTurnLimit?: number | undefined;- readonly anchorItemId?: TurnItemId | undefined;- readonly requiredRunId?: RunId | undefined;- readonly suppressLocal?: boolean | undefined;- },- fields?: ReadonlyArray<ProjectionRecordField>,- filter?: ProjectionRecordFilter,- ) {- const threadRows = yield* sql<PayloadRow>`- 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 &&- matchesNothing(filter?.turnItemTypes, filter?.turnItemStatuses, filter?.turnItemRunIds))- ? []- : window === undefined- ? yield* sql<PayloadRow>`- 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<PayloadRow>`+ // Share selection so counting discarded ancestry preserves payload-read positions.+ const retainedWindowRows = (+ threadId: ThreadId,+ window: {+ readonly rowLimit: number;+ readonly userTurnLimit?: number | undefined;+ readonly anchorItemId?: TurnItemId | undefined;+ readonly requiredRunId?: RunId | undefined;+ },+ ) => 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@@ -2984,6 +2964,50 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =) AS latestORDER BY ordinal ASC, turn_item_id ASC)+ `;++ const readCanonicalProjection = Effect.fn("ProjectionStore.readCanonicalProjection")(+ function* (+ threadId: ThreadId,+ window?: {+ readonly rowLimit: number;+ readonly userTurnLimit?: number | undefined;+ readonly anchorItemId?: TurnItemId | undefined;+ readonly requiredRunId?: RunId | undefined;+ readonly suppressLocal?: boolean | undefined;+ },+ fields?: ReadonlyArray<ProjectionRecordField>,+ filter?: ProjectionRecordFilter,+ ) {+ const threadRows = yield* sql<PayloadRow>`+ 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 &&+ matchesNothing(filter?.turnItemTypes, filter?.turnItemStatuses, filter?.turnItemRunIds))+ ? []+ : window === undefined+ ? yield* sql<PayloadRow>`+ 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<PayloadRow>`+ ${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.@@ -3592,6 +3616,45 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =};});+ // 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 &&+ seenThreadIds.size === 0 &&+ sourceWindow !== undefined &&+ activeLocalTurnItems(projection).filter((row) => isThreadHistoryUserTurn(row.item))+ .length >= Math.max(window.userTurnLimit, THREAD_HISTORY_PAGE_POLICY.maxUserTurns)+ ) {+ 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}+ `;+ const inheritedRowCount = counted[0]?.inherited_rows ?? null;+ if (inheritedRowCount !== null) {+ return {+ ...projection,+ visibleTurnItems: buildVisibleTurnItems({+ projection,+ sourceProjection: null,+ inheritedRowCount,+ }),+ };+ }+ }+const sourceProjection = yield* readProjection(forkedFrom.threadId,new Set([...seenThreadIds, threadId]),