Skip to content

Commit 2f304d7

Browse files
fix(server): defer checkpoint files after viewport byte trimming
1 parent 1057262 commit 2f304d7

1 file changed

Lines changed: 43 additions & 21 deletions

File tree

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

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,39 @@ export function boundedTimelineEncodedBytes(input: {
454454
return total;
455455
}
456456

457+
function checkpointsForViewport(
458+
projection: OrchestrationV2ThreadProjection,
459+
items: ReadonlyArray<OrchestrationV2TurnItem>,
460+
) {
461+
const retainedRunIds = new Set(items.map((item) => item.runId));
462+
for (const run of projection.runs) {
463+
if (["queued", "preparing", "starting", "running", "waiting"].includes(run.status)) {
464+
retainedRunIds.add(run.id);
465+
}
466+
}
467+
const retainedNodeIds = new Set(items.map((item) => item.nodeId));
468+
for (const node of projection.nodes) {
469+
if (["pending", "running", "waiting"].includes(node.status)) {
470+
retainedNodeIds.add(node.id);
471+
}
472+
}
473+
for (const request of projection.runtimeRequests) {
474+
if (request.status === "pending") retainedNodeIds.add(request.nodeId);
475+
}
476+
const retainedCheckpointIds = new Set(
477+
items.flatMap((item) => ("checkpointId" in item ? [item.checkpointId] : [])),
478+
);
479+
return projection.checkpoints.map((checkpoint) =>
480+
["pending", "capturing"].includes(checkpoint.status) ||
481+
(checkpoint.runId !== null && retainedRunIds.has(checkpoint.runId)) ||
482+
(checkpoint.nodeId !== null && retainedNodeIds.has(checkpoint.nodeId)) ||
483+
retainedCheckpointIds.has(checkpoint.id) ||
484+
checkpoint.deferredFileCount !== undefined
485+
? checkpoint
486+
: { ...checkpoint, files: [], deferredFileCount: checkpoint.files.length },
487+
);
488+
}
489+
457490
export function buildBoundedThreadProjection(input: {
458491
readonly projection: OrchestrationV2ThreadProjection;
459492
readonly snapshotSequence: number;
@@ -462,29 +495,13 @@ export function buildBoundedThreadProjection(input: {
462495
}): BoundedProjectionResult {
463496
const policy = input.policy ?? THREAD_HISTORY_PAGE_POLICY;
464497
const threadId = input.projection.thread.id;
465-
const recentRows = input.projection.visibleTurnItems.slice(-policy.maxItems);
466-
const retainedRunIds = new Set(recentRows.map(({ item }) => item.runId));
467-
for (const run of input.projection.runs) {
468-
if (["queued", "preparing", "starting", "running", "waiting"].includes(run.status)) {
469-
retainedRunIds.add(run.id);
470-
}
471-
}
472-
const retainedNodeIds = new Set(recentRows.map(({ item }) => item.nodeId));
473-
const retainedCheckpointIds = new Set(
474-
recentRows.flatMap(({ item }) => ("checkpointId" in item ? [item.checkpointId] : [])),
475-
);
476498
const controlProjection = {
477499
...input.projection,
478500
...(input.viewportWindow
479501
? {
480-
checkpoints: input.projection.checkpoints.map((checkpoint) =>
481-
["pending", "capturing"].includes(checkpoint.status) ||
482-
(checkpoint.runId !== null && retainedRunIds.has(checkpoint.runId)) ||
483-
(checkpoint.nodeId !== null && retainedNodeIds.has(checkpoint.nodeId)) ||
484-
retainedCheckpointIds.has(checkpoint.id) ||
485-
checkpoint.deferredFileCount !== undefined
486-
? checkpoint
487-
: { ...checkpoint, files: [], deferredFileCount: checkpoint.files.length },
502+
checkpoints: checkpointsForViewport(
503+
input.projection,
504+
input.projection.visibleTurnItems.slice(-policy.maxItems).map(({ item }) => item),
488505
),
489506
}
490507
: {}),
@@ -593,6 +610,7 @@ export function buildBoundedThreadProjection(input: {
593610
for (const item of dependencyTurnItems) {
594611
turnItemById.set(String(item.id), item);
595612
}
613+
const checkpointItems = visibleTurnItems.map(({ item }) => item);
596614
if (input.viewportWindow) {
597615
const activeRunIds = new Set(
598616
controlProjection.runs
@@ -616,6 +634,7 @@ export function buildBoundedThreadProjection(input: {
616634
("requestId" in item && pendingRequestIds.has(item.requestId))
617635
) {
618636
turnItemById.set(String(item.id), item);
637+
checkpointItems.push(item);
619638
}
620639
}
621640
}
@@ -653,6 +672,10 @@ export function buildBoundedThreadProjection(input: {
653672

654673
const projection = {
655674
...controlProjection,
675+
// Byte trimming can remove runs that were eligible when reserving control state.
676+
...(input.viewportWindow
677+
? { checkpoints: checkpointsForViewport(input.projection, checkpointItems) }
678+
: {}),
656679
plans,
657680
contextHandoffs,
658681
messages: messagesForBoundedProjection(controlProjection, turnItems),
@@ -664,7 +687,6 @@ export function buildBoundedThreadProjection(input: {
664687
historyCursor: window.nextCursor,
665688
hasMoreHistory: window.hasMoreHistory,
666689
latestLocalTurnOrdinal,
667-
payloadBudgetExceeded:
668-
controlBytes > policy.maxEncodedBytes || bytesOfJson(projection) > policy.maxEncodedBytes,
690+
payloadBudgetExceeded: bytesOfJson(projection) > policy.maxEncodedBytes,
669691
};
670692
}

0 commit comments

Comments
 (0)