Skip to content

Commit 9d532d9

Browse files
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 <[email protected]>
1 parent ad853d1 commit 9d532d9

3 files changed

Lines changed: 37 additions & 30 deletions

File tree

‎apps/server/src/orchestration-v2/boundedSnapshotTransport.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { THREAD_HISTORY_SNAPSHOT_ROW_LIMIT } from "./threadHistoryPaging.ts";
12
import { assert, describe, it } from "@effect/vitest";
23
import {
34
AuthOrchestrationReadScope,
@@ -553,6 +554,11 @@ it.layer(TestLayer)("compact bounded snapshot transport", (it) => {
553554
assert.isUndefined(legacy.decoded.turnItemsOmitLocalVisible);
554555
assert.isUndefined(full.decoded.turnItemsOmitLocalVisible);
555556
assert.isUndefined(full.decoded.historyCursor);
557+
// Clients that predate paging still get a window, never the whole transcript.
558+
assert.isAtMost(
559+
full.decoded.projection.turnItems.length,
560+
THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
561+
);
556562
assert.isTrue(compact.decoded.turnItemsOmitLocalVisible);
557563
assert.isBelow(
558564
compact.decoded.projection.turnItems.length,

‎apps/server/src/orchestration-v2/http.ts‎

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,6 @@ export const layer = HttpApiBuilder.group(
101101
},
102102
);
103103

104-
const loadThreadSnapshot = Effect.fn("http.orchestration.loadThreadSnapshot")(function* (
105-
threadId: Parameters<typeof threadManagement.getThreadSnapshot>[0],
106-
failureReason:
107-
| "orchestration_thread_snapshot_failed"
108-
| "orchestration_thread_bounded_snapshot_failed"
109-
| "orchestration_thread_history_failed",
110-
) {
111-
return yield* threadManagement.getThreadSnapshot(threadId).pipe(
112-
Effect.map((snapshot) => ({
113-
...snapshot,
114-
projection: projectThreadProjectionForWire(snapshot.projection),
115-
})),
116-
Effect.catch(
117-
Effect.fnUntraced(function* (error) {
118-
if (isThreadNotFound(error)) {
119-
return yield* failEnvironmentNotFound("thread_not_found");
120-
}
121-
return yield* failEnvironmentInternal(failureReason, error);
122-
}),
123-
),
124-
);
125-
});
126-
127104
const loadThreadSnapshotWindow = Effect.fn("http.orchestration.loadThreadSnapshotWindow")(
128105
function* (
129106
threadId: Parameters<typeof threadManagement.getThreadSnapshot>[0],
@@ -185,13 +162,31 @@ export const layer = HttpApiBuilder.group(
185162
Effect.fn("environment.orchestration.threadSnapshot")(function* (args) {
186163
yield* annotateEnvironmentRequest(args.endpoint.name);
187164
yield* requireEnvironmentScope(AuthOrchestrationReadScope);
188-
const snapshot = yield* loadThreadSnapshot(
189-
args.params.threadId,
190-
"orchestration_thread_snapshot_failed",
191-
).pipe(traceLocalHandlerWork);
165+
// Unbounded snapshots materialized whole transcripts; pre-pagination clients get the window.
166+
const snapshot = yield* threadManagement
167+
.getThreadSnapshotWindow(args.params.threadId, {
168+
rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
169+
})
170+
.pipe(
171+
Effect.catch(
172+
Effect.fnUntraced(function* (error) {
173+
if (isThreadNotFound(error)) {
174+
return yield* failEnvironmentNotFound("thread_not_found");
175+
}
176+
return yield* failEnvironmentInternal(
177+
"orchestration_thread_snapshot_failed",
178+
error,
179+
);
180+
}),
181+
),
182+
traceLocalHandlerWork,
183+
);
192184
return {
193185
snapshotSequence: snapshot.snapshotSequence,
194-
projection: snapshot.projection,
186+
projection: buildBoundedThreadProjection({
187+
projection: projectThreadProjectionForWire(snapshot.projection),
188+
snapshotSequence: snapshot.snapshotSequence,
189+
}).projection,
195190
};
196191
}),
197192
)

‎apps/server/src/ws.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,14 +859,17 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
859859

860860
const snapshotThenLive = Effect.fn("ws.orchestrationV2.threadSnapshotThenLive")(function* () {
861861
const useBoundedSnapshot = shouldUseBoundedThreadSnapshot(input);
862+
// Pre-pagination clients get the window as a plain projection, never the whole transcript.
862863
const snapshot = yield* (
863864
useBoundedSnapshot
864865
? threadManagement.getThreadSnapshotWindow(input.threadId, {
865866
rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
866867
deferWork: input.acceptDeferredWork === true,
867868
userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns,
868869
})
869-
: threadManagement.getThreadSnapshot(input.threadId)
870+
: threadManagement.getThreadSnapshotWindow(input.threadId, {
871+
rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
872+
})
870873
).pipe(
871874
Effect.mapError(
872875
(cause) =>
@@ -886,7 +889,10 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
886889
compactIds: input.acceptCompactIds === true,
887890
})
888891
: (() => {
889-
const projection = projectThreadProjectionForWire(snapshot.projection);
892+
const projection = buildBoundedThreadProjection({
893+
projection: projectThreadProjectionForWire(snapshot.projection),
894+
snapshotSequence,
895+
}).projection;
890896
return {
891897
kind: "snapshot" as const,
892898
snapshotSequence,

0 commit comments

Comments
 (0)