Skip to content

Commit 0f21292

Browse files
committed
fix(server): keep retained wake starts in bounded snapshots small
Retained notification starts carried their full detail, which is unbounded and not shortened by the wire projection. Keep them as stubs and drop them past a quarter of the snapshot budget. User-started runs need no evidence, since a run without a known notification start already counts as real work.
1 parent 189b46d commit 0f21292

2 files changed

Lines changed: 49 additions & 16 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ describe("threadHistoryPaging", () => {
722722
source: { kind: "delegated_task", taskIds: [] },
723723
outcome: "failed",
724724
summary: "Subagent failed",
725+
detail: "d".repeat(50_000),
725726
} as unknown as OrchestrationV2TurnItem;
726727
return { run, row: { ...row, sourceItemId: item.id, item } };
727728
});
@@ -742,6 +743,21 @@ describe("threadHistoryPaging", () => {
742743
expect(
743744
usageLimitBlockedRun(bounded.projection.runs, bounded.projection.turnItems, null)?.id,
744745
).toBe(limited.id);
746+
// Retained wake starts are stubs: a 50 KB notification detail must not reach the snapshot.
747+
const visibleIds = new Set(bounded.projection.visibleTurnItems.map((row) => row.sourceItemId));
748+
const retained = bounded.projection.turnItems.filter((item) => !visibleIds.has(item.id));
749+
expect(retained.length).toBeGreaterThan(0);
750+
expect(JSON.stringify(retained).length).toBeLessThan(10_000);
751+
752+
// Evidence past a quarter of the budget is dropped, so the snapshot stays within it.
753+
const tight = buildBoundedThreadProjection({
754+
projection: full,
755+
snapshotSequence: 1,
756+
policy: { maxItems: 2, maxEncodedBytes: 8_000 },
757+
});
758+
expect(
759+
usageLimitBlockedRun(tight.projection.runs, tight.projection.turnItems, null),
760+
).toBeNull();
745761
});
746762

747763
it("charges local row.item duplication when measuring bounded timeline bytes", () => {

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

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -525,26 +525,28 @@ function isRetainedDependency(
525525
);
526526
}
527527

528-
// Clients find a limited run that later notification wakes hide (usageLimitBlockedRun) from its
529-
// root error and from how each later run started. A long chain of wakes pushes those out of the
530-
// newest turns, which would leave the thread unresumable.
531-
function limitRecoveryItemIds(
528+
// usageLimitBlockedRun needs the limited run's root error and the notification that started each
529+
// later run, and a long chain of wakes pushes both out of the window. Starts are kept as stubs
530+
// without their unbounded detail, and dropped past a quarter of the budget (clients then show
531+
// the plain latest run).
532+
function limitRecoveryItems(
532533
projection: Pick<OrchestrationV2ThreadProjection, "runs" | "turnItems">,
533-
): ReadonlySet<string> {
534-
const ids = new Set<string>();
534+
maxEncodedBytes: number,
535+
): ReadonlyMap<string, OrchestrationV2TurnItem> {
536+
const items = new Map<string, OrchestrationV2TurnItem>();
535537
const blocked = usageLimitBlockedRun(projection.runs, projection.turnItems, null);
536-
if (blocked === null) return ids;
538+
if (blocked === null) return items;
537539
const laterRoots = new Set<string>();
538540
for (const run of projection.runs) {
539541
if (run.ordinal > blocked.ordinal && run.rootNodeId !== null) {
540542
laterRoots.add(String(run.rootNodeId));
541543
}
542544
}
543-
if (laterRoots.size === 0) return ids;
545+
if (laterRoots.size === 0) return items;
544546
const firstInputs = new Map<string, OrchestrationV2TurnItem>();
545547
for (const item of projection.turnItems) {
546548
if (item.type === "error" && item.runId === blocked.id && item.nodeId === blocked.rootNodeId) {
547-
ids.add(String(item.id));
549+
items.set(String(item.id), item);
548550
} else if (
549551
(item.type === "user_message" || item.type === "notification") &&
550552
item.nodeId !== null &&
@@ -556,15 +558,26 @@ function limitRecoveryItemIds(
556558
}
557559
}
558560
}
559-
for (const item of firstInputs.values()) ids.add(String(item.id));
560-
return ids;
561+
let startBytes = 0;
562+
const starts: Array<OrchestrationV2TurnItem> = [];
563+
for (const item of firstInputs.values()) {
564+
if (item.type !== "notification") continue;
565+
const { detail: _detail, ...rest } = item;
566+
const stub = { ...rest, title: null, summary: item.summary.slice(0, 120).trimEnd() };
567+
startBytes += bytesOfJson(stub);
568+
starts.push(stub);
569+
}
570+
if (startBytes <= maxEncodedBytes / 4) {
571+
for (const stub of starts) items.set(String(stub.id), stub);
572+
}
573+
return items;
561574
}
562575

563576
/** Retain small requests outside the visible window; later result pages still need them for attempt/run reducers. */
564577
function retainedInterruptRequestTurnItems(
565578
projection: OrchestrationV2ThreadProjection,
566579
visible: ReadonlyArray<OrchestrationV2ProjectedTurnItem>,
567-
limitItemIds: ReadonlySet<string>,
580+
limitItems: ReadonlyMap<string, OrchestrationV2TurnItem>,
568581
): OrchestrationV2TurnItem[] {
569582
const visibleLocalIds = new Set<string>();
570583
for (const row of visible) {
@@ -576,14 +589,17 @@ function retainedInterruptRequestTurnItems(
576589
const pendingRequestIds = pendingRequestIdsOf(projection);
577590
const retained: OrchestrationV2TurnItem[] = [];
578591
for (const item of projection.turnItems) {
579-
if (!isRetainedDependency(item, pendingRequestIds) && !limitItemIds.has(String(item.id))) {
592+
if (!isRetainedDependency(item, pendingRequestIds)) {
580593
continue;
581594
}
582595
if (visibleLocalIds.has(String(item.id))) {
583596
continue;
584597
}
585598
retained.push(item);
586599
}
600+
for (const [id, item] of limitItems) {
601+
if (!visibleLocalIds.has(id)) retained.push(item);
602+
}
587603
return retained;
588604
}
589605

@@ -694,15 +710,16 @@ export function buildBoundedThreadProjection(input: {
694710
};
695711
const latestLocalTurnOrdinal = computeLatestLocalTurnOrdinal(input.projection.turnItems);
696712

697-
const limitItemIds = limitRecoveryItemIds(controlProjection);
713+
const limitItems = limitRecoveryItems(controlProjection, policy.maxEncodedBytes);
698714
const dependencyReserve = (() => {
699715
let reserve = 0;
700716
const pendingRequestIds = pendingRequestIdsOf(controlProjection);
701717
for (const item of controlProjection.turnItems) {
702-
if (isRetainedDependency(item, pendingRequestIds) || limitItemIds.has(String(item.id))) {
718+
if (isRetainedDependency(item, pendingRequestIds)) {
703719
reserve += bytesOfJson(item);
704720
}
705721
}
722+
for (const item of limitItems.values()) reserve += bytesOfJson(item);
706723
return reserve;
707724
})();
708725
const controlBytes = bytesOfJson({
@@ -730,7 +747,7 @@ export function buildBoundedThreadProjection(input: {
730747
const dependencyTurnItems = retainedInterruptRequestTurnItems(
731748
controlProjection,
732749
visibleTurnItems,
733-
limitItemIds,
750+
limitItems,
734751
);
735752
const turnItemById = new Map<string, OrchestrationV2TurnItem>();
736753
for (const item of windowTurnItems) {

0 commit comments

Comments
 (0)