From ad853d164dd9304a433c41fd7cce8b7db71af973 Mon Sep 17 00:00:00 2001 From: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Date: Thu, 29 Oct 2026 10:52:46 -0700 Subject: [PATCH 1/3] perf(server): preparing a run reads only that run and its message Retrying or resuming workspace preparation loaded the thread's full projection to find one run. It now reads the run by id and its user message by id. Co-Authored-By: Claude Opus 5.5 --- .../orchestration-v2/ThreadLaunchService.ts | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/apps/server/src/orchestration-v2/ThreadLaunchService.ts b/apps/server/src/orchestration-v2/ThreadLaunchService.ts index 7a45a54a312f4..38d8c91c10958 100644 --- a/apps/server/src/orchestration-v2/ThreadLaunchService.ts +++ b/apps/server/src/orchestration-v2/ThreadLaunchService.ts @@ -1066,12 +1066,14 @@ const make = Effect.gen(function* () { "ThreadLaunchService.prepareRun", )(function* (input) { const scheduled = yield* Effect.gen(function* () { - const projection = yield* threads.getThreadProjection(input.threadId); - const run = projection.runs.find((candidate) => candidate.id === input.runId); + const { thread, runs } = yield* threads.getThreadRecords(input.threadId, ["runs"], { + runIds: [input.runId], + }); + const run = runs.find((candidate) => candidate.id === input.runId); const workspacePreparation = run?.workspacePreparation; if (run?.status !== "preparing" || workspacePreparation === undefined) return; if (!(yield* reservePreparation(input.commandId))) return; - yield* scheduleRunPreparation(input, projection, run, workspacePreparation).pipe( + yield* scheduleRunPreparation(input, thread, run, workspacePreparation).pipe( Effect.onError(() => releasePreparation(input.commandId)), ); }).pipe(Effect.exit); @@ -1093,30 +1095,33 @@ const make = Effect.gen(function* () { return dispatched; }); - const scheduleRunPreparation = ( + const scheduleRunPreparation = Effect.fn("ThreadLaunchService.scheduleRunPreparation")(function* ( input: ThreadLaunchPreparationInput, - projection: OrchestrationV2ThreadProjection, + thread: OrchestrationV2ThreadProjection["thread"], run: OrchestrationV2ThreadProjection["runs"][number], workspacePreparation: ThreadLaunchWorkspaceStrategy, - ) => { - const message = projection.messages.find((candidate) => candidate.id === run.userMessageId); + ) { + const { messages } = yield* threads.getThreadRecords(input.threadId, ["messages"], { + messageIds: [run.userMessageId], + }); + const message = messages.find((candidate) => candidate.id === run.userMessageId); const reuse = workspacePreparation.type === "worktree" && - projection.thread.worktreePath !== null && - projection.thread.branch !== null + thread.worktreePath !== null && + thread.branch !== null ? { strategy: { type: "existing_worktree" as const, - worktreePath: projection.thread.worktreePath, - branch: projection.thread.branch, + worktreePath: thread.worktreePath, + branch: thread.branch, }, reusedWorktree: { baseRef: workspacePreparation.baseRef }, } : null; - return schedulePreparation( + return yield* schedulePreparation( { commandId: input.commandId, - projectId: projection.thread.projectId, + projectId: thread.projectId, workspaceStrategy: reuse?.strategy ?? workspacePreparation, ...(reuse === null ? {} : { reusedWorktree: reuse.reusedWorktree }), ...(message === undefined @@ -1132,7 +1137,7 @@ const make = Effect.gen(function* () { input.threadId, run.id, ); - }; + }); return ThreadLaunchService.of({ launch, checkWorktreeBase, retryPreparation, prepareRun }); }); From 9d532d929cd2c1c95b53593f37a3442ad33671b7 Mon Sep 17 00:00:00 2001 From: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Date: Thu, 29 Oct 2026 11:03:20 -0700 Subject: [PATCH 2/3] perf(server): pre-paging thread snapshots read a window, not the transcript The socket subscribe without acceptBoundedSnapshot and the HTTP threadSnapshot route still loaded the whole thread (386 ms, +124 MB heap for a 5.5k-item thread). No current client uses either path, so serve them the bounded window as a plain projection, the way the getThreadProjection compatibility RPC already does. Co-Authored-By: Claude Opus 5.5 --- .../boundedSnapshotTransport.test.ts | 6 +++ apps/server/src/orchestration-v2/http.ts | 51 +++++++++---------- apps/server/src/ws.ts | 10 +++- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/apps/server/src/orchestration-v2/boundedSnapshotTransport.test.ts b/apps/server/src/orchestration-v2/boundedSnapshotTransport.test.ts index 220e707b6c735..299b776f559f2 100644 --- a/apps/server/src/orchestration-v2/boundedSnapshotTransport.test.ts +++ b/apps/server/src/orchestration-v2/boundedSnapshotTransport.test.ts @@ -1,3 +1,4 @@ +import { THREAD_HISTORY_SNAPSHOT_ROW_LIMIT } from "./threadHistoryPaging.ts"; import { assert, describe, it } from "@effect/vitest"; import { AuthOrchestrationReadScope, @@ -553,6 +554,11 @@ it.layer(TestLayer)("compact bounded snapshot transport", (it) => { assert.isUndefined(legacy.decoded.turnItemsOmitLocalVisible); assert.isUndefined(full.decoded.turnItemsOmitLocalVisible); assert.isUndefined(full.decoded.historyCursor); + // Clients that predate paging still get a window, never the whole transcript. + assert.isAtMost( + full.decoded.projection.turnItems.length, + THREAD_HISTORY_SNAPSHOT_ROW_LIMIT, + ); assert.isTrue(compact.decoded.turnItemsOmitLocalVisible); assert.isBelow( compact.decoded.projection.turnItems.length, diff --git a/apps/server/src/orchestration-v2/http.ts b/apps/server/src/orchestration-v2/http.ts index 61ff54d0c794f..549b50d09f9ba 100644 --- a/apps/server/src/orchestration-v2/http.ts +++ b/apps/server/src/orchestration-v2/http.ts @@ -101,29 +101,6 @@ export const layer = HttpApiBuilder.group( }, ); - const loadThreadSnapshot = Effect.fn("http.orchestration.loadThreadSnapshot")(function* ( - threadId: Parameters[0], - failureReason: - | "orchestration_thread_snapshot_failed" - | "orchestration_thread_bounded_snapshot_failed" - | "orchestration_thread_history_failed", - ) { - return yield* threadManagement.getThreadSnapshot(threadId).pipe( - Effect.map((snapshot) => ({ - ...snapshot, - projection: projectThreadProjectionForWire(snapshot.projection), - })), - Effect.catch( - Effect.fnUntraced(function* (error) { - if (isThreadNotFound(error)) { - return yield* failEnvironmentNotFound("thread_not_found"); - } - return yield* failEnvironmentInternal(failureReason, error); - }), - ), - ); - }); - const loadThreadSnapshotWindow = Effect.fn("http.orchestration.loadThreadSnapshotWindow")( function* ( threadId: Parameters[0], @@ -185,13 +162,31 @@ export const layer = HttpApiBuilder.group( Effect.fn("environment.orchestration.threadSnapshot")(function* (args) { yield* annotateEnvironmentRequest(args.endpoint.name); yield* requireEnvironmentScope(AuthOrchestrationReadScope); - const snapshot = yield* loadThreadSnapshot( - args.params.threadId, - "orchestration_thread_snapshot_failed", - ).pipe(traceLocalHandlerWork); + // Unbounded snapshots materialized whole transcripts; pre-pagination clients get the window. + const snapshot = yield* threadManagement + .getThreadSnapshotWindow(args.params.threadId, { + rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT, + }) + .pipe( + Effect.catch( + Effect.fnUntraced(function* (error) { + if (isThreadNotFound(error)) { + return yield* failEnvironmentNotFound("thread_not_found"); + } + return yield* failEnvironmentInternal( + "orchestration_thread_snapshot_failed", + error, + ); + }), + ), + traceLocalHandlerWork, + ); return { snapshotSequence: snapshot.snapshotSequence, - projection: snapshot.projection, + projection: buildBoundedThreadProjection({ + projection: projectThreadProjectionForWire(snapshot.projection), + snapshotSequence: snapshot.snapshotSequence, + }).projection, }; }), ) diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 86c7a4db56af4..a68dfb33ad2ee 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -859,6 +859,7 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs const snapshotThenLive = Effect.fn("ws.orchestrationV2.threadSnapshotThenLive")(function* () { const useBoundedSnapshot = shouldUseBoundedThreadSnapshot(input); + // Pre-pagination clients get the window as a plain projection, never the whole transcript. const snapshot = yield* ( useBoundedSnapshot ? threadManagement.getThreadSnapshotWindow(input.threadId, { @@ -866,7 +867,9 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs deferWork: input.acceptDeferredWork === true, userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns, }) - : threadManagement.getThreadSnapshot(input.threadId) + : threadManagement.getThreadSnapshotWindow(input.threadId, { + rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT, + }) ).pipe( Effect.mapError( (cause) => @@ -886,7 +889,10 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs compactIds: input.acceptCompactIds === true, }) : (() => { - const projection = projectThreadProjectionForWire(snapshot.projection); + const projection = buildBoundedThreadProjection({ + projection: projectThreadProjectionForWire(snapshot.projection), + snapshotSequence, + }).projection; return { kind: "snapshot" as const, snapshotSequence, From 4b145cd8ed656bf9d2d3c8222aba357fec374b36 Mon Sep 17 00:00:00 2001 From: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Date: Thu, 29 Oct 2026 13:19:15 -0700 Subject: [PATCH 3/3] Revert "perf(server): pre-paging thread snapshots read a window, not the transcript" This reverts commit 9d532d929cd2c1c95b53593f37a3442ad33671b7. --- .../boundedSnapshotTransport.test.ts | 6 --- apps/server/src/orchestration-v2/http.ts | 51 ++++++++++--------- apps/server/src/ws.ts | 10 +--- 3 files changed, 30 insertions(+), 37 deletions(-) diff --git a/apps/server/src/orchestration-v2/boundedSnapshotTransport.test.ts b/apps/server/src/orchestration-v2/boundedSnapshotTransport.test.ts index 299b776f559f2..220e707b6c735 100644 --- a/apps/server/src/orchestration-v2/boundedSnapshotTransport.test.ts +++ b/apps/server/src/orchestration-v2/boundedSnapshotTransport.test.ts @@ -1,4 +1,3 @@ -import { THREAD_HISTORY_SNAPSHOT_ROW_LIMIT } from "./threadHistoryPaging.ts"; import { assert, describe, it } from "@effect/vitest"; import { AuthOrchestrationReadScope, @@ -554,11 +553,6 @@ it.layer(TestLayer)("compact bounded snapshot transport", (it) => { assert.isUndefined(legacy.decoded.turnItemsOmitLocalVisible); assert.isUndefined(full.decoded.turnItemsOmitLocalVisible); assert.isUndefined(full.decoded.historyCursor); - // Clients that predate paging still get a window, never the whole transcript. - assert.isAtMost( - full.decoded.projection.turnItems.length, - THREAD_HISTORY_SNAPSHOT_ROW_LIMIT, - ); assert.isTrue(compact.decoded.turnItemsOmitLocalVisible); assert.isBelow( compact.decoded.projection.turnItems.length, diff --git a/apps/server/src/orchestration-v2/http.ts b/apps/server/src/orchestration-v2/http.ts index 549b50d09f9ba..61ff54d0c794f 100644 --- a/apps/server/src/orchestration-v2/http.ts +++ b/apps/server/src/orchestration-v2/http.ts @@ -101,6 +101,29 @@ export const layer = HttpApiBuilder.group( }, ); + const loadThreadSnapshot = Effect.fn("http.orchestration.loadThreadSnapshot")(function* ( + threadId: Parameters[0], + failureReason: + | "orchestration_thread_snapshot_failed" + | "orchestration_thread_bounded_snapshot_failed" + | "orchestration_thread_history_failed", + ) { + return yield* threadManagement.getThreadSnapshot(threadId).pipe( + Effect.map((snapshot) => ({ + ...snapshot, + projection: projectThreadProjectionForWire(snapshot.projection), + })), + Effect.catch( + Effect.fnUntraced(function* (error) { + if (isThreadNotFound(error)) { + return yield* failEnvironmentNotFound("thread_not_found"); + } + return yield* failEnvironmentInternal(failureReason, error); + }), + ), + ); + }); + const loadThreadSnapshotWindow = Effect.fn("http.orchestration.loadThreadSnapshotWindow")( function* ( threadId: Parameters[0], @@ -162,31 +185,13 @@ export const layer = HttpApiBuilder.group( Effect.fn("environment.orchestration.threadSnapshot")(function* (args) { yield* annotateEnvironmentRequest(args.endpoint.name); yield* requireEnvironmentScope(AuthOrchestrationReadScope); - // Unbounded snapshots materialized whole transcripts; pre-pagination clients get the window. - const snapshot = yield* threadManagement - .getThreadSnapshotWindow(args.params.threadId, { - rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT, - }) - .pipe( - Effect.catch( - Effect.fnUntraced(function* (error) { - if (isThreadNotFound(error)) { - return yield* failEnvironmentNotFound("thread_not_found"); - } - return yield* failEnvironmentInternal( - "orchestration_thread_snapshot_failed", - error, - ); - }), - ), - traceLocalHandlerWork, - ); + const snapshot = yield* loadThreadSnapshot( + args.params.threadId, + "orchestration_thread_snapshot_failed", + ).pipe(traceLocalHandlerWork); return { snapshotSequence: snapshot.snapshotSequence, - projection: buildBoundedThreadProjection({ - projection: projectThreadProjectionForWire(snapshot.projection), - snapshotSequence: snapshot.snapshotSequence, - }).projection, + projection: snapshot.projection, }; }), ) diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index a68dfb33ad2ee..86c7a4db56af4 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -859,7 +859,6 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs const snapshotThenLive = Effect.fn("ws.orchestrationV2.threadSnapshotThenLive")(function* () { const useBoundedSnapshot = shouldUseBoundedThreadSnapshot(input); - // Pre-pagination clients get the window as a plain projection, never the whole transcript. const snapshot = yield* ( useBoundedSnapshot ? threadManagement.getThreadSnapshotWindow(input.threadId, { @@ -867,9 +866,7 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs deferWork: input.acceptDeferredWork === true, userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns, }) - : threadManagement.getThreadSnapshotWindow(input.threadId, { - rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT, - }) + : threadManagement.getThreadSnapshot(input.threadId) ).pipe( Effect.mapError( (cause) => @@ -889,10 +886,7 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs compactIds: input.acceptCompactIds === true, }) : (() => { - const projection = buildBoundedThreadProjection({ - projection: projectThreadProjectionForWire(snapshot.projection), - snapshotSequence, - }).projection; + const projection = projectThreadProjectionForWire(snapshot.projection); return { kind: "snapshot" as const, snapshotSequence,