Skip to content

Commit d378e2f

Browse files
t3dotggclaude
andcommitted
fix(server): the export size floor reads tool rows only and its limits are pinned by tests
- The two-permit export test from main mocked the orchestrator without the size query and hung after the merge. The mock now answers it. - The size floor selects tool rows only. A rolled back run is the only way the export hides a tool row, so the user message and interrupt filters go. - The export test now also pins the rolled back run filter, and its oversized dynamic tool row is a row the full read can decode. - The transfer budget fixture gives the working run a ready handoff with a 256 KB history. The bounded snapshot cap fails (339 KB against 85 KB) when that history reaches the wire. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 9151c2e commit d378e2f

7 files changed

Lines changed: 105 additions & 14 deletions

File tree

‎apps/server/integration/TransferBudgetReport.integration.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ const FLEET_SHELL_BUDGET = {
106106
// About 14% above the observed 75 KB. The window holds the working run only: 2 nodes, 2 subagents,
107107
// 1 plan, 1 provider turn and the last finished turn's checkpoint. When finished runs ride along
108108
// 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.
109110
const STALE_TODO_BUDGET = {
110111
decodedBytes: 85_000,
111112
rows: 75,

‎apps/server/integration/TransferBudgetV2Fixture.integration.ts‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
CheckpointId,
33
CheckpointRef,
44
CheckpointScopeId,
5+
ContextHandoffId,
56
EventId,
67
MessageId,
78
NodeId,
@@ -446,6 +447,7 @@ export const STALE_TODO_THREAD_ID = ThreadId.make("transfer-budget-stale-todo-li
446447
export const STALE_TODO_SETTLED_RUNS = 40;
447448
export const STALE_TODO_SUBAGENTS_PER_RUN = 2;
448449
export const STALE_TODO_LIVE_ROWS = 600;
450+
const STALE_TODO_HANDOFF_HISTORY_BYTES = 256 * 1024;
449451
const staleTodoCheckpointId = (index: number) =>
450452
CheckpointId.make(`checkpoint:thread:${STALE_TODO_THREAD_ID}:ordinal:${index + 1}`);
451453
/** The checkpoint clients read the last finished turn's diff from. */
@@ -742,7 +744,50 @@ export function staleTodoListThreadBatches(
742744
exitCode: 0,
743745
},
744746
});
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+
}
746791

747792
message("assistant", text(index, 5, 1_200));
748793
const checkpointId = staleTodoCheckpointId(index);

‎apps/server/integration/transferBudgetV2.integration.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ it.live("reports V2 thread HTTP and WebSocket transfer budgets", () =>
448448
staleTodo.subagents.filter((subagent) => subagent.runId === STALE_TODO_LIVE_RUN_ID),
449449
STALE_TODO_SUBAGENTS_PER_RUN,
450450
);
451+
// The byte cap below also passes when the window loses the handoff.
452+
assert.lengthOf(staleTodo.contextHandoffs, 1);
451453
const staleTodoColdOpen = {
452454
http: staleTodoHttp,
453455
rows: staleTodo.visibleTurnItems.length,

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5860,35 +5860,34 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
58605860
Effect.mapError(controlReadError(threadId)),
58615861
);
58625862
// Tool output reaches the export unchanged, so the bytes of its text, also inside a structured
5863-
// result, are a floor for the export. Other stored bytes are not: decoding drops fields. Rows that createOrchestrationV2TurnItemVisibility
5864-
// can hide and rows a fork inherits are left out. The output sum parses payloads, so it runs
5865-
// only when the stored rows, sized from their row headers, are over `limit`.
5863+
// result, are a floor for the export. Other stored bytes are not: decoding drops fields. A
5864+
// rolled back run is the only way the export hides a tool row, and rows a fork inherits only
5865+
// add to it. The output sum parses payloads, so it runs only when the tool rows exceed `limit`.
58665866
const getTranscriptByteFloor: ProjectionStoreV2Shape["getTranscriptByteFloor"] = (
58675867
threadId,
58685868
limit,
58695869
) =>
58705870
sql<{ bytes: number }>`
5871-
WITH exported AS (
5872-
SELECT type, payload_json FROM orchestration_v2_projection_turn_items
5871+
WITH tool_rows AS (
5872+
SELECT payload_json FROM orchestration_v2_projection_turn_items
58735873
WHERE thread_id = ${threadId}
5874-
AND type NOT IN ('user_message', 'run_interrupt_result')
5874+
AND type IN ('command_execution', 'dynamic_tool')
58755875
AND (run_id IS NULL OR run_id NOT IN (
58765876
SELECT run_id FROM orchestration_v2_projection_runs
58775877
WHERE thread_id = ${threadId} AND status = 'rolled_back'
58785878
))
58795879
)
58805880
SELECT CASE
5881-
WHEN (SELECT COALESCE(SUM(octet_length(payload_json)), 0) FROM exported) <= ${limit}
5881+
WHEN (SELECT COALESCE(SUM(octet_length(payload_json)), 0) FROM tool_rows) <= ${limit}
58825882
THEN 0
58835883
ELSE (
58845884
-- json_valid is false past SQLite's nesting limit, where json_tree would fail.
58855885
SELECT COALESCE(SUM(CASE WHEN json_valid(payload_json) THEN (
58865886
SELECT COALESCE(SUM(octet_length(leaf.value)), 0)
5887-
FROM json_tree(exported.payload_json, '$.output') AS leaf
5887+
FROM json_tree(tool_rows.payload_json, '$.output') AS leaf
58885888
WHERE leaf.type = 'text'
58895889
) ELSE 0 END), 0)
5890-
FROM exported
5891-
WHERE type IN ('command_execution', 'dynamic_tool')
5890+
FROM tool_rows
58925891
)
58935892
END AS bytes
58945893
`.pipe(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,7 @@ it.effect(
890890
Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
891891
Layer.provide(
892892
Layer.mock(Orchestrator.OrchestratorV2)({
893+
getTranscriptByteFloor: () => Effect.succeed(0),
893894
getThreadSnapshot: (threadId) =>
894895
Queue.offer(started, threadId).pipe(
895896
Effect.andThen(Deferred.await(gates.get(threadId)!)),

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ORCHESTRATION_PROTOCOL_HEADER,
1010
ORCHESTRATION_PROTOCOL_VERSION_TEXT,
1111
OrchestrationV2AppThread,
12+
OrchestrationV2Run,
1213
OrchestrationV2TurnItem,
1314
OrchestrationV2ThreadTranscript,
1415
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
@@ -98,6 +99,24 @@ const items = Schema.decodeUnknownSync(Schema.Array(OrchestrationV2TurnItem))(
9899
})),
99100
);
100101

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+
101120
// Serves the orchestration HTTP routes over real stores; `allowed` gates the read scope.
102121
const serveOrchestrationHttp = (allowed: () => boolean) =>
103122
Effect.gen(function* () {
@@ -277,7 +296,8 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
277296
WHERE turn_item_id = ${commandItemId}
278297
`;
279298
yield* setCommandPayload(sql`json_set(
280-
replace(payload_json, ${deepField}, ''), '$.output',
299+
replace(payload_json, ${deepField}, ''), '$.type', 'dynamic_tool', '$.toolName', NULL,
300+
'$.output',
281301
json_array(json_object('type', 'text', 'text', ${fill(PROVIDER_SEND_TURN_MAX_FILE_BYTES + 1)}))
282302
)`);
283303
const reads: Array<string> = [];
@@ -291,6 +311,28 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
291311
).toBeInstanceOf(ThreadManagementService.ThreadTranscriptTooLargeError);
292312
expect(reads).toEqual([expect.stringContaining("octet_length(payload_json)")]);
293313
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);
294336

295337
expect((yield* read(ThreadId.make("missing"))).status).toBe(404);
296338
allowed = false;

‎docs/internals/performance-regressions.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ send and caps the decoded bytes and rows of the bounded snapshot and one older p
7272
agent turn, and of the cold-connect shell for a sidebar of settled threads and idle subagents. It
7373
also caps the bytes and the node, subagent, plan, provider turn and checkpoint counts of the
7474
bounded snapshot for a thread of 40 finished runs behind one working run, so finished runs cannot
75-
ride along with a window. When clients gain an opt-in, add it to the test's client queries and give
76-
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.
7778

7879
## Event store and startup
7980

0 commit comments

Comments
 (0)