Skip to content

Commit 3fe7c4c

Browse files
t3dotggclaude
andcommitted
perf(server): oversized transcript exports fail fast and thread opens skip unused handoff bodies
A transcript export read and decoded the whole thread before it could fail as too large. It now sums the stored bytes of the rows it is sure to export, without reading a payload, and fails when that sum is over twice the file attachment limit. The exact check still runs after the read for every other thread. The windowed thread read selected whole context handoff payloads. Every reader of that window sends handoffs through projectContextHandoffForWire, which drops history and delivery, so the query now removes both in SQLite. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 34b50df commit 3fe7c4c

7 files changed

Lines changed: 231 additions & 17 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@ export interface OrchestratorV2Shape {
423423
options: ProjectionTimelinePageOptions,
424424
) => Effect.Effect<ProjectionTimelinePage, OrchestratorProjectionError>;
425425
readonly getMessageCount: (threadId: ThreadId) => Effect.Effect<number, OrchestratorV2Error>;
426+
readonly getTranscriptByteFloor: (
427+
threadId: ThreadId,
428+
) => Effect.Effect<number, OrchestratorV2Error>;
426429
readonly getTurnItem: (input: {
427430
readonly threadId: ThreadId;
428431
readonly itemId: TurnItemId;
@@ -14914,6 +14917,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1491414917
.getMessageCount(threadId)
1491514918
.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
1491614919
}),
14920+
getTranscriptByteFloor: Effect.fn("OrchestratorV2.getTranscriptByteFloor")(function (threadId) {
14921+
return projectionStore
14922+
.getTranscriptByteFloor(threadId)
14923+
.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
14924+
}),
1491714925
getTurnItem: Effect.fn("OrchestratorV2.getTurnItem")(function (input) {
1491814926
return projectionStore
1491914927
.getTurnItem(input)

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

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ContextTransferId,
1010
CheckpointRef,
1111
CheckpointScopeId,
12+
ContextHandoffId,
1213
MessageId,
1314
type ModelSelection,
1415
type OrchestrationV2ProviderThread,
@@ -55,6 +56,7 @@ import {
5556
THREAD_HISTORY_PAGE_POLICY,
5657
THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
5758
} from "./threadHistoryPaging.ts";
59+
import { buildBoundedThreadStreamSnapshot } from "./ThreadStream.ts";
5860
import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts";
5961

6062
import { layerReaderDatabase, traceSqlRouting } from "../persistence/testkit/SqliteRouting.ts";
@@ -2553,6 +2555,142 @@ it.effect("SQL snapshot windows keep cards for requests still pending", () =>
25532555
}).pipe(Effect.provide(layerTest)),
25542556
);
25552557

2558+
it.effect("SQL snapshot windows leave handoff bodies in SQLite without changing the wire", () =>
2559+
Effect.gen(function* () {
2560+
const store = yield* ProjectionStore.ProjectionStoreV2;
2561+
const now = yield* DateTime.now;
2562+
const threadId = ThreadId.make("thread:handoff-window");
2563+
yield* store.apply({
2564+
id: EventId.make("event:handoff-window:thread"),
2565+
type: "thread.created",
2566+
threadId,
2567+
occurredAt: now,
2568+
payload: {
2569+
createdBy: "user",
2570+
creationSource: "web",
2571+
id: threadId,
2572+
projectId: ProjectId.make("project:handoff-window"),
2573+
title: "Handoff window",
2574+
providerInstanceId,
2575+
modelSelection,
2576+
runtimeMode: "full-access",
2577+
interactionMode: "default",
2578+
branch: null,
2579+
worktreePath: null,
2580+
activeProviderThreadId: null,
2581+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
2582+
forkedFrom: null,
2583+
createdAt: now,
2584+
updatedAt: now,
2585+
archivedAt: null,
2586+
settledOverride: null,
2587+
settledAt: null,
2588+
lastVisitedAt: null,
2589+
deletedAt: null,
2590+
},
2591+
});
2592+
const providerThreadId = ProviderThreadId.make("provider-thread:handoff-window");
2593+
const earlierItemId = TurnItemId.make("item:handoff-window:earlier");
2594+
const handoff = (id: string, status: "ready" | "superseded") => ({
2595+
id: ContextHandoffId.make(`handoff:handoff-window:${id}`),
2596+
transferId: null,
2597+
threadId,
2598+
targetRunId: RunId.make("run:handoff-window"),
2599+
fromProviderThreadIds: [providerThreadId],
2600+
toProviderThreadId: providerThreadId,
2601+
coveredRunOrdinals: { from: 1, to: 1 },
2602+
strategy: "full_thread_summary" as const,
2603+
status,
2604+
summaryMessageId: null,
2605+
summaryText: `Summary ${id}`,
2606+
history: {
2607+
messages: [
2608+
{
2609+
role: "user" as const,
2610+
text: "Earlier request ".repeat(1_000),
2611+
threadId,
2612+
runId: null,
2613+
itemId: earlierItemId,
2614+
providerThreadId,
2615+
status: "completed",
2616+
kind: "user_message",
2617+
},
2618+
],
2619+
coverage: "full",
2620+
omittedItems: 0,
2621+
},
2622+
delivery: { nativeThreadId: "native", status: "injected" as const, itemIds: [earlierItemId] },
2623+
createdByProviderInstanceId: providerInstanceId,
2624+
createdAt: now,
2625+
updatedAt: now,
2626+
});
2627+
// The window keeps a ready handoff and one its rows show; the third is outside it.
2628+
const handoffs = [
2629+
handoff("ready", "ready"),
2630+
handoff("shown", "superseded"),
2631+
handoff("old", "superseded"),
2632+
];
2633+
for (const payload of handoffs) {
2634+
yield* store.apply({
2635+
id: EventId.make(`event:${payload.id}`),
2636+
type: "context-handoff.updated",
2637+
threadId,
2638+
occurredAt: now,
2639+
payload,
2640+
});
2641+
}
2642+
yield* store.apply({
2643+
id: EventId.make("event:handoff-window:item"),
2644+
type: "turn-item.updated",
2645+
threadId,
2646+
occurredAt: now,
2647+
payload: {
2648+
id: TurnItemId.make("item:handoff-window:handoff"),
2649+
threadId,
2650+
runId: null,
2651+
nodeId: null,
2652+
providerThreadId: null,
2653+
providerTurnId: null,
2654+
nativeItemRef: null,
2655+
parentItemId: null,
2656+
ordinal: 1,
2657+
status: "completed",
2658+
title: null,
2659+
startedAt: now,
2660+
completedAt: now,
2661+
updatedAt: now,
2662+
type: "handoff",
2663+
contextHandoffId: handoffs[1]!.id,
2664+
fromProviderThreadIds: [providerThreadId],
2665+
toProviderThreadId: providerThreadId,
2666+
fromProviderInstanceIds: [providerInstanceId],
2667+
toProviderInstanceId: providerInstanceId,
2668+
strategy: "full_thread_summary",
2669+
},
2670+
});
2671+
2672+
const window = yield* store.getThreadSnapshotWindow(threadId, { rowLimit: 10 });
2673+
const windowHandoffs = window.projection.contextHandoffs;
2674+
assert.deepEqual(
2675+
windowHandoffs.map((row) => row.id),
2676+
[handoffs[0]!.id, handoffs[1]!.id],
2677+
);
2678+
for (const row of windowHandoffs) {
2679+
assert.isUndefined(row.history);
2680+
assert.isUndefined(row.delivery);
2681+
}
2682+
const full = yield* store.getThreadSnapshot(threadId);
2683+
assert.deepEqual(full.projection.contextHandoffs, handoffs);
2684+
assert.deepEqual(
2685+
buildBoundedThreadStreamSnapshot(window),
2686+
buildBoundedThreadStreamSnapshot({
2687+
...window,
2688+
projection: { ...window.projection, contextHandoffs: handoffs.slice(0, 2) },
2689+
}),
2690+
);
2691+
}).pipe(Effect.provide(layerTest)),
2692+
);
2693+
25562694
it.effect("lists only active subagent IDs without loading shell data", () =>
25572695
Effect.gen(function* () {
25582696
const store = yield* ProjectionStore.ProjectionStoreV2;

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,10 @@ export interface ProjectionStoreV2Shape {
472472
options: ProjectionTimelinePageOptions,
473473
) => Effect.Effect<ProjectionTimelinePage, ProjectionStoreV2Error>;
474474
readonly getMessageCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;
475+
/** Stored bytes of the turn items every transcript export includes. Reads no payload. */
476+
readonly getTranscriptByteFloor: (
477+
threadId: ThreadId,
478+
) => Effect.Effect<number, ProjectionStoreV2Error>;
475479
readonly getSubagentCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;
476480
/** Child threads of the thread's app-owned tasks, in task order, without decoding the tasks. */
477481
readonly listAppOwnedChildThreadIds: (
@@ -713,6 +717,7 @@ export interface ProjectionStoreV2Shape {
713717
},
714718
ProjectionStoreV2Error
715719
>;
720+
/** Context handoffs come without `history` and `delivery`; no window reader uses them. */
716721
readonly getThreadSnapshotWindow: (
717722
threadId: ThreadId,
718723
options: {
@@ -3872,7 +3877,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
38723877
WHERE thread_id = ${threadId} ORDER BY rowid ASC
38733878
`
38743879
: sql<PayloadRow>`
3875-
SELECT payload_json FROM orchestration_v2_projection_context_handoffs AS handoff
3880+
-- Megabytes on a long thread, and projectContextHandoffForWire drops both.
3881+
SELECT json_remove(payload_json, '$.history', '$.delivery') AS payload_json
3882+
FROM orchestration_v2_projection_context_handoffs AS handoff
38763883
WHERE handoff.thread_id = ${threadId}
38773884
AND (
38783885
handoff.status IN ('pending', 'ready')
@@ -5752,6 +5759,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
57525759
Effect.map((rows) => rows[0]?.count ?? 0),
57535760
Effect.mapError(controlReadError(threadId)),
57545761
);
5762+
// Skips every row createOrchestrationV2TurnItemVisibility can hide, so the sum never counts a
5763+
// row the export leaves out. Rows a fork inherits are left out; they only add to the export.
5764+
// octet_length reads the row header, not the payload.
5765+
const getTranscriptByteFloor: ProjectionStoreV2Shape["getTranscriptByteFloor"] = (threadId) =>
5766+
sql<{ bytes: number }>`
5767+
SELECT COALESCE(SUM(octet_length(payload_json)), 0) AS bytes
5768+
FROM orchestration_v2_projection_turn_items
5769+
WHERE thread_id = ${threadId}
5770+
AND type NOT IN ('user_message', 'run_interrupt_result')
5771+
AND (run_id IS NULL OR run_id NOT IN (
5772+
SELECT run_id FROM orchestration_v2_projection_runs
5773+
WHERE thread_id = ${threadId} AND status = 'rolled_back'
5774+
))
5775+
`.pipe(
5776+
Effect.map((rows) => rows[0]?.bytes ?? 0),
5777+
Effect.mapError(controlReadError(threadId)),
5778+
);
57555779
const getSubagentCount: ProjectionStoreV2Shape["getSubagentCount"] = (threadId) =>
57565780
sql<{
57575781
count: number;
@@ -7783,6 +7807,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
77837807
hasUnpairedRunInterruptRequest: readOnReader(hasUnpairedRunInterruptRequest),
77847808
isNodeStored: readOnReader(isNodeStored),
77857809
getMessageCount: readOnReader(getMessageCount),
7810+
getTranscriptByteFloor: readOnReader(getTranscriptByteFloor),
77867811
getSubagentCount: readOnReader(getSubagentCount),
77877812
listAppOwnedChildThreadIds: readOnReader(listAppOwnedChildThreadIds),
77887813
getNextTurnItemOrdinal: readOnReader(getNextTurnItemOrdinal),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ it.effect(
239239
getThreadHistoryPage: () => Effect.die("unused"),
240240
getTimelinePage: () => Effect.die("Unused timeline read"),
241241
getMessageCount: () => Effect.die("unused message count"),
242+
getTranscriptByteFloor: () => Effect.die("unused transcript byte floor"),
242243
getSubagentCount: () => Effect.die("unused subagent count"),
243244
listAppOwnedChildThreadIds: () => Effect.die("unused listAppOwnedChildThreadIds"),
244245
getNextTurnItemOrdinal: () => Effect.die("unused ordinal read"),

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ export class ThreadManagementThreadNotFoundError extends Schema.TaggedError<Thre
217217
}
218218
}
219219

220+
// An exported row is its stored payload plus a wrapper, so stored bytes are a floor for the export
221+
// unless decoding drops fields. Twice the limit covers that, and the exact check still decides
222+
// every export near the limit.
223+
const TRANSCRIPT_EARLY_REJECT_BYTES = 2 * PROVIDER_SEND_TURN_MAX_FILE_BYTES;
224+
220225
export class ThreadTranscriptTooLargeError extends Schema.TaggedError<ThreadTranscriptTooLargeError>()(
221226
"ThreadTranscriptTooLargeError",
222227
{ threadId: ThreadId },
@@ -681,7 +686,11 @@ const make = Effect.gen(function* () {
681686
);
682687
const getThreadTranscript = Effect.fn("orchestrationV2.threadManagement.getThreadTranscript")(
683688
function* (threadId: ThreadId) {
684-
const { projection } = yield* getThreadSnapshot(threadId);
689+
yield* ensureProjectionTranscript(threadId);
690+
if ((yield* orchestrator.getTranscriptByteFloor(threadId)) > TRANSCRIPT_EARLY_REJECT_BYTES) {
691+
return yield* new ThreadTranscriptTooLargeError({ threadId });
692+
}
693+
const { projection } = yield* orchestrator.getThreadSnapshot(threadId);
685694
const environment = yield* ServerEnvironment.ServerEnvironmentIdentity;
686695
const transcript = {
687696
threadId: projection.thread.id,

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

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import * as Context from "effect/Context";
2121
import * as DateTime from "effect/DateTime";
2222
import * as Layer from "effect/Layer";
2323
import * as Schema from "effect/Schema";
24+
import * as SqlClient from "effect/sql/SqlClient";
25+
import * as Statement from "effect/sql/Statement";
2426
import * as HttpRouter from "effect/http/HttpRouter";
2527
import * as HttpPlatform from "effect/http/HttpPlatform";
2628
import * as Etag from "effect/http/Etag";
@@ -173,6 +175,18 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
173175
),
174176
);
175177

178+
const readTranscriptError = Context.get(
179+
services,
180+
ThreadManagementService.ThreadManagementService,
181+
)
182+
.getThreadTranscript(thread.id)
183+
.pipe(
184+
Effect.provideService(ServerEnvironment.ServerEnvironmentIdentity, {
185+
getEnvironmentId: Effect.succeed(environmentId),
186+
}),
187+
Effect.flip,
188+
);
189+
176190
const emptyResponse = yield* read(thread.id);
177191
expect(emptyResponse.status).toBe(200);
178192
expect(
@@ -227,23 +241,41 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
227241
});
228242
const response = yield* read(thread.id);
229243
expect(response.status).toBe(expectedStatus);
230-
if (expectedStatus === 400) {
231-
const error = yield* Context.get(services, ThreadManagementService.ThreadManagementService)
232-
.getThreadTranscript(thread.id)
233-
.pipe(
234-
Effect.provideService(ServerEnvironment.ServerEnvironmentIdentity, {
235-
getEnvironmentId: Effect.succeed(environmentId),
236-
}),
237-
Effect.flip,
238-
);
239-
expect(error).toBeInstanceOf(ThreadManagementService.ThreadTranscriptTooLargeError);
240-
expect(yield* Effect.promise(() => response.json())).toMatchObject({
241-
_tag: "EnvironmentRequestInvalidError",
242-
reason: "thread_transcript_too_large",
243-
});
244+
if (expectedStatus === 200) {
245+
const exported = yield* decodeTranscript(yield* Effect.promise(() => response.json()));
246+
expect(exported.items[0]?.item).toMatchObject({ output: boundaryOutput });
247+
continue;
244248
}
249+
expect(yield* readTranscriptError).toBeInstanceOf(
250+
ThreadManagementService.ThreadTranscriptTooLargeError,
251+
);
252+
expect(yield* Effect.promise(() => response.json())).toMatchObject({
253+
_tag: "EnvironmentRequestInvalidError",
254+
reason: "thread_transcript_too_large",
255+
});
245256
}
246257

258+
// Far above the limit the stored size alone rejects the export: its only read is that size.
259+
const sql = Context.get(services, SqlClient.SqlClient);
260+
yield* sql`
261+
UPDATE orchestration_v2_projection_turn_items
262+
SET payload_json = json_set(
263+
payload_json, '$.output', printf('%.*c', ${2 * PROVIDER_SEND_TURN_MAX_FILE_BYTES}, 'x')
264+
)
265+
WHERE turn_item_id = ${items[1]!.id}
266+
`;
267+
const reads: Array<string> = [];
268+
expect(
269+
yield* readTranscriptError.pipe(
270+
Effect.provideService(Statement.CurrentTransformer, (statement) => {
271+
reads.push(statement.compile()[0]);
272+
return Effect.succeed(statement);
273+
}),
274+
),
275+
).toBeInstanceOf(ThreadManagementService.ThreadTranscriptTooLargeError);
276+
expect(reads).toEqual([expect.stringContaining("octet_length(payload_json)")]);
277+
expect((yield* read(thread.id)).status).toBe(400);
278+
247279
expect((yield* read(ThreadId.make("missing"))).status).toBe(404);
248280
allowed = false;
249281
expect((yield* read(thread.id)).status).toBe(403);

‎docs/internals/performance-regressions.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ The v2 checks pin these invariants:
4141
requested.
4242
Explicit cross-environment transcript exports preserve full tool bodies so the destination
4343
agent can read the context. The export rejects timeline JSON above the file attachment limit
44-
before sending it; the client still applies its attachment limit to the complete file.
44+
before sending it, and before reading any payload when the stored rows alone are over twice
45+
that limit; the client still applies its attachment limit to the complete file.
4546
- Live command output reaches a client only through `orchestration.subscribeCommandOutput`, which
4647
an expanded command row opens and closes with itself. Raw output deltas are never orchestration
4748
events: adapters send them to the

0 commit comments

Comments
 (0)