The best way to control Claude Code, Codex and more from any computer or phone
perf(server): oversized transcript exports fail fast and thread opens skip unused handoff bodies (#34547)
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
Theo Browne committed
Nov 12, 2026 at 02:53 UTC
7d3635e572d95556e82518058a172fc6b5d208f5
11 files changed
+364
-21
apps/server/integration/TransferBudgetReport.integration.ts
+1
@@ -106,6 +106,7 @@ const FLEET_SHELL_BUDGET = {
106
// About 14% above the observed 75 KB. The window holds the working run only: 2 nodes, 2 subagents,
107
// 1 plan, 1 provider turn and the last finished turn's checkpoint. When finished runs ride along
108
// it holds 82 nodes, 82 subagents, 41 plans, 41 provider turns, 40 checkpoints and 740 KB.
109
+// The working run's ready handoff ships without its 256 KB history; with it the open is 339 KB.
110
const STALE_TODO_BUDGET = {
111
decodedBytes: 85_000,
112
rows: 75,
apps/server/integration/TransferBudgetV2Fixture.integration.ts
+46
-1
@@ -2,6 +2,7 @@ import {
2
CheckpointId,
3
CheckpointRef,
4
CheckpointScopeId,
5
+ ContextHandoffId,
6
EventId,
7
MessageId,
8
NodeId,
@@ -446,6 +447,7 @@ export const STALE_TODO_THREAD_ID = ThreadId.make("transfer-budget-stale-todo-li
447
export const STALE_TODO_SETTLED_RUNS = 40;
448
export const STALE_TODO_SUBAGENTS_PER_RUN = 2;
449
export const STALE_TODO_LIVE_ROWS = 600;
450
+const STALE_TODO_HANDOFF_HISTORY_BYTES = 256 * 1024;
451
const staleTodoCheckpointId = (index: number) =>
452
CheckpointId.make(`checkpoint:thread:${STALE_TODO_THREAD_ID}:ordinal:${index + 1}`);
453
/** The checkpoint clients read the last finished turn's diff from. */
@@ -742,7 +744,50 @@ export function staleTodoListThreadBatches(
744
exitCode: 0,
745
},
746
});
745
- if (live) return events;
747
+ if (live) {
748
+ // A ready handoff rides along with every window. Its history and delivery are for the
749
+ // provider turn start and must not reach a client.
750
+ const earlierItemId = TurnItemId.make(`turn-item:stale-todo:${index}:handoff-source`);
751
+ events.push({
752
+ ...common,
753
+ id: EventId.make(`stale-todo-handoff-${index}`),
754
+ type: "context-handoff.updated",
755
+ payload: {
756
+ id: ContextHandoffId.make(`handoff:run:${runKey}`),
757
+ transferId: null,
758
+ threadId,
759
+ targetRunId: runId,
760
+ fromProviderThreadIds: [providerThreadId],
761
+ toProviderThreadId: providerThreadId,
762
+ coveredRunOrdinals: { from: 1, to: index },
763
+ strategy: "full_thread_summary",
764
+ status: "ready",
765
+ summaryMessageId: null,
766
+ summaryText: text(index, 6, 1_024),
767
+ history: {
768
+ messages: [
769
+ {
770
+ role: "user",
771
+ text: text(index, 7, STALE_TODO_HANDOFF_HISTORY_BYTES),
772
+ threadId,
773
+ runId: null,
774
+ itemId: earlierItemId,
775
+ providerThreadId,
776
+ status: "completed",
777
+ kind: "user_message",
778
+ },
779
+ ],
780
+ coverage: "full",
781
+ omittedItems: 0,
782
+ },
783
+ delivery: { nativeThreadId: "native", status: "injected", itemIds: [earlierItemId] },
784
+ createdByProviderInstanceId: providerInstanceId,
785
+ createdAt: now,
786
+ updatedAt: now,
787
+ },
788
+ });
789
+ return events;
790
+ }
791
792
message("assistant", text(index, 5, 1_200));
793
const checkpointId = staleTodoCheckpointId(index);
apps/server/integration/transferBudgetV2.integration.test.ts
+2
@@ -448,6 +448,8 @@ it.live("reports V2 thread HTTP and WebSocket transfer budgets", () =>
448
staleTodo.subagents.filter((subagent) => subagent.runId === STALE_TODO_LIVE_RUN_ID),
449
STALE_TODO_SUBAGENTS_PER_RUN,
450
);
451
+ // The byte cap below also passes when the window loses the handoff.
452
+ assert.lengthOf(staleTodo.contextHandoffs, 1);
453
const staleTodoColdOpen = {
454
http: staleTodoHttp,
455
rows: staleTodo.visibleTurnItems.length,
apps/server/src/orchestration-v2/Orchestrator.ts
+11
@@ -428,6 +428,10 @@ export interface OrchestratorV2Shape {
428
options: ProjectionTimelinePageOptions,
429
) => Effect.Effect<ProjectionTimelinePage, OrchestratorProjectionError>;
430
readonly getMessageCount: (threadId: ThreadId) => Effect.Effect<number, OrchestratorV2Error>;
431
+ readonly getTranscriptByteFloor: (
432
+ threadId: ThreadId,
433
+ limit: number,
434
+ ) => Effect.Effect<number, OrchestratorV2Error>;
435
readonly getTurnItem: (input: {
436
readonly threadId: ThreadId;
437
readonly itemId: TurnItemId;
@@ -15418,6 +15422,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
15422
.getMessageCount(threadId)
15423
.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
15424
}),
15425
+ getTranscriptByteFloor: Effect.fn("OrchestratorV2.getTranscriptByteFloor")(
15426
+ function (threadId, limit) {
15427
+ return projectionStore
15428
+ .getTranscriptByteFloor(threadId, limit)
15429
+ .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
15430
+ },
15431
+ ),
15432
getTurnItem: Effect.fn("OrchestratorV2.getTurnItem")(function (input) {
15433
return projectionStore
15434
.getTurnItem(input)
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+138
@@ -9,6 +9,7 @@ import {
9
ContextTransferId,
10
CheckpointRef,
11
CheckpointScopeId,
12
+ ContextHandoffId,
13
MessageId,
14
type ModelSelection,
15
type OrchestrationV2ProviderThread,
@@ -55,6 +56,7 @@ import {
56
THREAD_HISTORY_PAGE_POLICY,
57
THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
58
} from "./threadHistoryPaging.ts";
59
+import { buildBoundedThreadStreamSnapshot } from "./ThreadStream.ts";
60
import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts";
61
62
import { layerReaderDatabase, traceSqlRouting } from "../persistence/testkit/SqliteRouting.ts";
@@ -2594,6 +2596,142 @@ it.effect("SQL snapshot windows keep cards for requests still pending", () =>
2596
}).pipe(Effect.provide(layerTest)),
2597
);
2598
2599
+it.effect("SQL snapshot windows leave handoff bodies in SQLite without changing the wire", () =>
2600
+ Effect.gen(function* () {
2601
+ const store = yield* ProjectionStore.ProjectionStoreV2;
2602
+ const now = yield* DateTime.now;
2603
+ const threadId = ThreadId.make("thread:handoff-window");
2604
+ yield* store.apply({
2605
+ id: EventId.make("event:handoff-window:thread"),
2606
+ type: "thread.created",
2607
+ threadId,
2608
+ occurredAt: now,
2609
+ payload: {
2610
+ createdBy: "user",
2611
+ creationSource: "web",
2612
+ id: threadId,
2613
+ projectId: ProjectId.make("project:handoff-window"),
2614
+ title: "Handoff window",
2615
+ providerInstanceId,
2616
+ modelSelection,
2617
+ runtimeMode: "full-access",
2618
+ interactionMode: "default",
2619
+ branch: null,
2620
+ worktreePath: null,
2621
+ activeProviderThreadId: null,
2622
+ lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
2623
+ forkedFrom: null,
2624
+ createdAt: now,
2625
+ updatedAt: now,
2626
+ archivedAt: null,
2627
+ settledOverride: null,
2628
+ settledAt: null,
2629
+ lastVisitedAt: null,
2630
+ deletedAt: null,
2631
+ },
2632
+ });
2633
+ const providerThreadId = ProviderThreadId.make("provider-thread:handoff-window");
2634
+ const earlierItemId = TurnItemId.make("item:handoff-window:earlier");
2635
+ const handoff = (id: string, status: "ready" | "superseded") => ({
2636
+ id: ContextHandoffId.make(`handoff:handoff-window:${id}`),
2637
+ transferId: null,
2638
+ threadId,
2639
+ targetRunId: RunId.make("run:handoff-window"),
2640
+ fromProviderThreadIds: [providerThreadId],
2641
+ toProviderThreadId: providerThreadId,
2642
+ coveredRunOrdinals: { from: 1, to: 1 },
2643
+ strategy: "full_thread_summary" as const,
2644
+ status,
2645
+ summaryMessageId: null,
2646
+ summaryText: `Summary ${id}`,
2647
+ history: {
2648
+ messages: [
2649
+ {
2650
+ role: "user" as const,
2651
+ text: "Earlier request ".repeat(1_000),
2652
+ threadId,
2653
+ runId: null,
2654
+ itemId: earlierItemId,
2655
+ providerThreadId,
2656
+ status: "completed",
2657
+ kind: "user_message",
2658
+ },
2659
+ ],
2660
+ coverage: "full",
2661
+ omittedItems: 0,
2662
+ },
2663
+ delivery: { nativeThreadId: "native", status: "injected" as const, itemIds: [earlierItemId] },
2664
+ createdByProviderInstanceId: providerInstanceId,
2665
+ createdAt: now,
2666
+ updatedAt: now,
2667
+ });
2668
+ // The window keeps a ready handoff and one its rows show; the third is outside it.
2669
+ const handoffs = [
2670
+ handoff("ready", "ready"),
2671
+ handoff("shown", "superseded"),
2672
+ handoff("old", "superseded"),
2673
+ ];
2674
+ for (const payload of handoffs) {
2675
+ yield* store.apply({
2676
+ id: EventId.make(`event:${payload.id}`),
2677
+ type: "context-handoff.updated",
2678
+ threadId,
2679
+ occurredAt: now,
2680
+ payload,
2681
+ });
2682
+ }
2683
+ yield* store.apply({
2684
+ id: EventId.make("event:handoff-window:item"),
2685
+ type: "turn-item.updated",
2686
+ threadId,
2687
+ occurredAt: now,
2688
+ payload: {
2689
+ id: TurnItemId.make("item:handoff-window:handoff"),
2690
+ threadId,
2691
+ runId: null,
2692
+ nodeId: null,
2693
+ providerThreadId: null,
2694
+ providerTurnId: null,
2695
+ nativeItemRef: null,
2696
+ parentItemId: null,
2697
+ ordinal: 1,
2698
+ status: "completed",
2699
+ title: null,
2700
+ startedAt: now,
2701
+ completedAt: now,
2702
+ updatedAt: now,
2703
+ type: "handoff",
2704
+ contextHandoffId: handoffs[1]!.id,
2705
+ fromProviderThreadIds: [providerThreadId],
2706
+ toProviderThreadId: providerThreadId,
2707
+ fromProviderInstanceIds: [providerInstanceId],
2708
+ toProviderInstanceId: providerInstanceId,
2709
+ strategy: "full_thread_summary",
2710
+ },
2711
+ });
2712
+
2713
+ const window = yield* store.getThreadSnapshotWindow(threadId, { rowLimit: 10 });
2714
+ const windowHandoffs = window.projection.contextHandoffs;
2715
+ assert.deepEqual(
2716
+ windowHandoffs.map((row) => row.id),
2717
+ [handoffs[0]!.id, handoffs[1]!.id],
2718
+ );
2719
+ for (const row of windowHandoffs) {
2720
+ assert.isUndefined(row.history);
2721
+ assert.isUndefined(row.delivery);
2722
+ }
2723
+ const full = yield* store.getThreadSnapshot(threadId);
2724
+ assert.deepEqual(full.projection.contextHandoffs, handoffs);
2725
+ assert.deepEqual(
2726
+ buildBoundedThreadStreamSnapshot(window),
2727
+ buildBoundedThreadStreamSnapshot({
2728
+ ...window,
2729
+ projection: { ...window.projection, contextHandoffs: handoffs.slice(0, 2) },
2730
+ }),
2731
+ );
2732
+ }).pipe(Effect.provide(layerTest)),
2733
+);
2734
+
2735
it.effect("lists only active subagent IDs without loading shell data", () =>
2736
Effect.gen(function* () {
2737
const store = yield* ProjectionStore.ProjectionStoreV2;
apps/server/src/orchestration-v2/ProjectionStore.ts
+45
-1
@@ -472,6 +472,11 @@ export interface ProjectionStoreV2Shape {
472
options: ProjectionTimelinePageOptions,
473
) => Effect.Effect<ProjectionTimelinePage, ProjectionStoreV2Error>;
474
readonly getMessageCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;
475
+ /** Bytes of tool output a transcript export is sure to hold, or 0 when it can fit in `limit`. */
476
+ readonly getTranscriptByteFloor: (
477
+ threadId: ThreadId,
478
+ limit: number,
479
+ ) => Effect.Effect<number, ProjectionStoreV2Error>;
480
readonly getSubagentCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;
481
/** Child threads of the thread's app-owned tasks, in task order, without decoding the tasks. */
482
readonly listAppOwnedChildThreadIds: (
@@ -716,6 +721,7 @@ export interface ProjectionStoreV2Shape {
721
},
722
ProjectionStoreV2Error
723
>;
724
+ /** Context handoffs come without `history` and `delivery`; no window reader uses them. */
725
readonly getThreadSnapshotWindow: (
726
threadId: ThreadId,
727
options: {
@@ -4003,7 +4009,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4009
WHERE thread_id = ${threadId} ORDER BY rowid ASC
4010
`
4011
: sql<PayloadRow>`
4006
- SELECT payload_json FROM orchestration_v2_projection_context_handoffs AS handoff
4012
+ -- Megabytes on a long thread, and projectContextHandoffForWire drops both.
4013
+ SELECT json_remove(payload_json, '$.history', '$.delivery') AS payload_json
4014
+ FROM orchestration_v2_projection_context_handoffs AS handoff
4015
WHERE handoff.thread_id = ${threadId}
4016
AND (
4017
handoff.status IN ('pending', 'ready')
@@ -5915,6 +5923,41 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5923
Effect.map((rows) => rows[0]?.count ?? 0),
5924
Effect.mapError(controlReadError(threadId)),
5925
);
5926
+ // Tool output reaches the export unchanged, so the bytes of its text, also inside a structured
5927
+ // result, are a floor for the export. Other stored bytes are not: decoding drops fields. A
5928
+ // rolled back run is the only way the export hides a tool row, and rows a fork inherits only
5929
+ // add to it. The output sum parses payloads, so it runs only when the tool rows exceed `limit`.
5930
+ const getTranscriptByteFloor: ProjectionStoreV2Shape["getTranscriptByteFloor"] = (
5931
+ threadId,
5932
+ limit,
5933
+ ) =>
5934
+ sql<{ bytes: number }>`
5935
+ WITH tool_rows AS (
5936
+ SELECT payload_json FROM orchestration_v2_projection_turn_items
5937
+ WHERE thread_id = ${threadId}
5938
+ AND type IN ('command_execution', 'dynamic_tool')
5939
+ AND (run_id IS NULL OR run_id NOT IN (
5940
+ SELECT run_id FROM orchestration_v2_projection_runs
5941
+ WHERE thread_id = ${threadId} AND status = 'rolled_back'
5942
+ ))
5943
+ )
5944
+ SELECT CASE
5945
+ WHEN (SELECT COALESCE(SUM(octet_length(payload_json)), 0) FROM tool_rows) <= ${limit}
5946
+ THEN 0
5947
+ ELSE (
5948
+ -- json_valid is false past SQLite's nesting limit, where json_tree would fail.
5949
+ SELECT COALESCE(SUM(CASE WHEN json_valid(payload_json) THEN (
5950
+ SELECT COALESCE(SUM(octet_length(leaf.value)), 0)
5951
+ FROM json_tree(tool_rows.payload_json, '$.output') AS leaf
5952
+ WHERE leaf.type = 'text'
5953
+ ) ELSE 0 END), 0)
5954
+ FROM tool_rows
5955
+ )
5956
+ END AS bytes
5957
+ `.pipe(
5958
+ Effect.map((rows) => rows[0]?.bytes ?? 0),
5959
+ Effect.mapError(controlReadError(threadId)),
5960
+ );
5961
const getSubagentCount: ProjectionStoreV2Shape["getSubagentCount"] = (threadId) =>
5962
sql<{
5963
count: number;
@@ -7995,6 +8038,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
8038
hasUnpairedRunInterruptRequest: readOnReader(hasUnpairedRunInterruptRequest),
8039
isNodeStored: readOnReader(isNodeStored),
8040
getMessageCount: readOnReader(getMessageCount),
8041
+ getTranscriptByteFloor: readOnReader(getTranscriptByteFloor),
8042
getSubagentCount: readOnReader(getSubagentCount),
8043
listAppOwnedChildThreadIds: readOnReader(listAppOwnedChildThreadIds),
8044
hasPendingDelegatedCompletion: readOnReader(hasPendingDelegatedCompletion),
apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
+1
@@ -239,6 +239,7 @@ it.effect(
239
getThreadHistoryPage: () => Effect.die("unused"),
240
getTimelinePage: () => Effect.die("Unused timeline read"),
241
getMessageCount: () => Effect.die("unused message count"),
242
+ getTranscriptByteFloor: () => Effect.die("unused transcript byte floor"),
243
hasPendingDelegatedCompletion: () => Effect.die("unused hasPendingDelegatedCompletion"),
244
getSubagentCount: () => Effect.die("unused subagent count"),
245
listAppOwnedChildThreadIds: () => Effect.die("unused listAppOwnedChildThreadIds"),
apps/server/src/orchestration-v2/ThreadManagementService.test.ts
+1
@@ -890,6 +890,7 @@ it.effect(
890
Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
891
Layer.provide(
892
Layer.mock(Orchestrator.OrchestratorV2)({
893
+ getTranscriptByteFloor: () => Effect.succeed(0),
894
getThreadSnapshot: (threadId) =>
895
Queue.offer(started, threadId).pipe(
896
Effect.andThen(Deferred.await(gates.get(threadId)!)),
apps/server/src/orchestration-v2/ThreadManagementService.ts
+9
-1
@@ -687,7 +687,15 @@ const make = Effect.gen(function* () {
687
const transcriptExportPermits = yield* Semaphore.make(2);
688
const getThreadTranscript = Effect.fn("orchestrationV2.threadManagement.getThreadTranscript")(
689
function* (threadId: ThreadId) {
690
- const { projection } = yield* getThreadSnapshot(threadId);
690
+ yield* ensureProjectionTranscript(threadId);
691
+ const byteFloor = yield* orchestrator.getTranscriptByteFloor(
692
+ threadId,
693
+ PROVIDER_SEND_TURN_MAX_FILE_BYTES,
694
+ );
695
+ if (byteFloor > PROVIDER_SEND_TURN_MAX_FILE_BYTES) {
696
+ return yield* new ThreadTranscriptTooLargeError({ threadId });
697
+ }
698
+ const { projection } = yield* orchestrator.getThreadSnapshot(threadId);
699
const environment = yield* ServerEnvironment.ServerEnvironmentIdentity;
700
const transcript = {
701
threadId: projection.thread.id,
apps/server/src/orchestration-v2/http.test.ts
+105
-15
@@ -9,6 +9,7 @@ import {
9
ORCHESTRATION_PROTOCOL_HEADER,
10
ORCHESTRATION_PROTOCOL_VERSION_TEXT,
11
OrchestrationV2AppThread,
12
+ OrchestrationV2Run,
13
OrchestrationV2TurnItem,
14
OrchestrationV2ThreadTranscript,
15
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
@@ -21,6 +22,8 @@ import * as Context from "effect/Context";
22
import * as DateTime from "effect/DateTime";
23
import * as Layer from "effect/Layer";
24
import * as Schema from "effect/Schema";
25
+import * as SqlClient from "effect/sql/SqlClient";
26
+import * as Statement from "effect/sql/Statement";
27
import * as HttpRouter from "effect/http/HttpRouter";
28
import * as HttpPlatform from "effect/http/HttpPlatform";
29
import * as Etag from "effect/http/Etag";
@@ -96,6 +99,24 @@ const items = Schema.decodeUnknownSync(Schema.Array(OrchestrationV2TurnItem))(
99
})),
100
);
101
102
+const rolledBackRun = Schema.decodeSync(OrchestrationV2Run)({
103
+ id: "run:rolled-back",
104
+ threadId: "source-thread",
105
+ ordinal: 1,
106
+ providerInstanceId: "codex",
107
+ modelSelection: { instanceId: "codex", model: "gpt-5.4" },
108
+ providerThreadId: null,
109
+ userMessageId: "message:rolled-back",
110
+ rootNodeId: null,
111
+ activeAttemptId: null,
112
+ status: "rolled_back",
113
+ requestedAt: now,
114
+ startedAt: now,
115
+ completedAt: now,
116
+ checkpointId: null,
117
+ contextHandoffId: null,
118
+});
119
+
120
// Serves the orchestration HTTP routes over real stores; `allowed` gates the read scope.
121
const serveOrchestrationHttp = (allowed: () => boolean) =>
122
Effect.gen(function* () {
@@ -173,6 +194,18 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
194
),
195
);
196
197
+ const readTranscriptError = Context.get(
198
+ services,
199
+ ThreadManagementService.ThreadManagementService,
200
+ )
201
+ .getThreadTranscript(thread.id)
202
+ .pipe(
203
+ Effect.provideService(ServerEnvironment.ServerEnvironmentIdentity, {
204
+ getEnvironmentId: Effect.succeed(environmentId),
205
+ }),
206
+ Effect.flip,
207
+ );
208
+
209
const emptyResponse = yield* read(thread.id);
210
expect(emptyResponse.status).toBe(200);
211
expect(
@@ -227,23 +260,80 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
260
});
261
const response = yield* read(thread.id);
262
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
- }
263
+ if (expectedStatus === 200) continue;
264
+ expect(yield* readTranscriptError).toBeInstanceOf(
265
+ ThreadManagementService.ThreadTranscriptTooLargeError,
266
+ );
267
+ expect(yield* Effect.promise(() => response.json())).toMatchObject({
268
+ _tag: "EnvironmentRequestInvalidError",
269
+ reason: "thread_transcript_too_large",
270
+ });
271
}
272
273
+ const sql = Context.get(services, SqlClient.SqlClient);
274
+ const commandItemId = items.find((item) => item.type === "command_execution")!.id;
275
+ const setCommandPayload = (payload: Statement.Fragment) => sql`
276
+ UPDATE orchestration_v2_projection_turn_items SET payload_json = ${payload}
277
+ WHERE turn_item_id = ${commandItemId}
278
+ `;
279
+ const fill = (bytes: number) => sql`printf('%.*c', ${bytes}, 'x')`;
280
+ // Stored bytes the export drops do not count, and neither does a row SQLite cannot parse:
281
+ // only what the export keeps can reject it.
282
+ yield* setCommandPayload(sql`json_set(
283
+ payload_json, '$.output', 'x',
284
+ '$.droppedLegacyField', ${fill(PROVIDER_SEND_TURN_MAX_FILE_BYTES + 1)}
285
+ )`);
286
+ expect((yield* read(thread.id)).status).toBe(200);
287
+ const deepField = sql`'"droppedDeepField":' || printf('%.*c', 1001, '[') || printf('%.*c', 1001, ']') || ','`;
288
+ yield* setCommandPayload(
289
+ sql`replace(payload_json, '"droppedLegacyField":', ${deepField} || '"droppedLegacyField":')`,
290
+ );
291
+ expect((yield* read(thread.id)).status).toBe(200);
292
+ // Tool output over the limit rejects the export, here as the content array a dynamic tool
293
+ // stores. Its only read is the size.
294
+ yield* sql`
295
+ UPDATE orchestration_v2_projection_turn_items SET type = 'dynamic_tool'
296
+ WHERE turn_item_id = ${commandItemId}
297
+ `;
298
+ yield* setCommandPayload(sql`json_set(
299
+ replace(payload_json, ${deepField}, ''), '$.type', 'dynamic_tool', '$.toolName', NULL,
300
+ '$.output',
301
+ json_array(json_object('type', 'text', 'text', ${fill(PROVIDER_SEND_TURN_MAX_FILE_BYTES + 1)}))
302
+ )`);
303
+ const reads: Array<string> = [];
304
+ expect(
305
+ yield* readTranscriptError.pipe(
306
+ Effect.provideService(Statement.CurrentTransformer, (statement) => {
307
+ reads.push(statement.compile()[0]);
308
+ return Effect.succeed(statement);
309
+ }),
310
+ ),
311
+ ).toBeInstanceOf(ThreadManagementService.ThreadTranscriptTooLargeError);
312
+ expect(reads).toEqual([expect.stringContaining("octet_length(payload_json)")]);
313
+ expect((yield* read(thread.id)).status).toBe(400);
314
+ // The export leaves a rolled back run out, so its output cannot reject it.
315
+ yield* eventSink.write({
316
+ events: [
317
+ {
318
+ id: EventId.make("create-rolled-back-run"),
319
+ type: "run.created",
320
+ threadId: thread.id,
321
+ occurredAt: now,
322
+ payload: rolledBackRun,
323
+ },
324
+ ],
325
+ });
326
+ yield* sql`
327
+ UPDATE orchestration_v2_projection_turn_items SET run_id = ${rolledBackRun.id}
328
+ WHERE turn_item_id = ${commandItemId}
329
+ `;
330
+ yield* setCommandPayload(sql`json_set(payload_json, '$.runId', ${rolledBackRun.id})`);
331
+ const withoutRolledBackRun = yield* read(thread.id);
332
+ expect(withoutRolledBackRun.status).toBe(200);
333
+ expect(
334
+ (yield* decodeTranscript(yield* Effect.promise(() => withoutRolledBackRun.json()))).items,
335
+ ).toHaveLength(199);
336
+
337
expect((yield* read(ThreadId.make("missing"))).status).toBe(404);
338
allowed = false;
339
expect((yield* read(thread.id)).status).toBe(403);
docs/internals/performance-regressions.md
+5
-3
@@ -41,7 +41,8 @@ The v2 checks pin these invariants:
41
requested.
42
Explicit cross-environment transcript exports preserve full tool bodies so the destination
43
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 decoding any row when the tool output text alone is over that
45
+ limit; the client still applies its attachment limit to the complete file.
46
- Live command output reaches a client only through `orchestration.subscribeCommandOutput`, which
47
an expanded command row opens and closes with itself. Raw output deltas are never orchestration
48
events: adapters send them to the
@@ -71,8 +72,9 @@ send and caps the decoded bytes and rows of the bounded snapshot and one older p
72
agent turn, and of the cold-connect shell for a sidebar of settled threads and idle subagents. It
73
also caps the bytes and the node, subagent, plan, provider turn and checkpoint counts of the
74
bounded snapshot for a thread of 40 finished runs behind one working run, so finished runs cannot
74
-ride along with a window. When clients gain an opt-in, add it to the test's client queries and give
75
-a fixture the data it saves.
75
+ride along with a window. The working run has a ready context handoff, so its history cannot ride
76
+along either. When clients gain an opt-in, add it to the test's client queries and give a fixture
77
+the data it saves.
78
79
The server config has its own budget in `apps/server/src/ws.test.ts`. A client that asks for
80
catalog digests and status deltas gets the provider list of its first config in under 1 KiB, for a