Skip to content

Commit 2a49cf0

Browse files
author
PR Batch Tester
committed
fix(server): reserve snapshot space for pending question cards
1 parent a43ea75 commit 2a49cf0

3 files changed

Lines changed: 154 additions & 45 deletions

File tree

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
MessageId,
33
NodeId,
44
RunId,
5+
RuntimeRequestId,
56
ThreadId,
67
TurnItemId,
78
type OrchestrationV2ProjectedTurnItem,
@@ -738,6 +739,69 @@ describe("threadHistoryPaging", () => {
738739
).toBe(2_097_152);
739740
});
740741

742+
it.each([40_000, 80_000])(
743+
"reserves pending question cards before visible history: %s bytes",
744+
(questionBytes) => {
745+
const rows = Array.from({ length: 20 }, (_, index) =>
746+
makeRow(index + 1, { outputBytes: 1_500 }),
747+
);
748+
const requestId = RuntimeRequestId.make("pending-question");
749+
const card: OrchestrationV2TurnItem = {
750+
id: TurnItemId.make("pending-question-card"),
751+
type: "user_input_request",
752+
threadId: THREAD,
753+
runId: null,
754+
nodeId: NODE,
755+
providerThreadId: null,
756+
providerTurnId: null,
757+
nativeItemRef: null,
758+
parentItemId: null,
759+
ordinal: 1,
760+
status: "completed",
761+
title: null,
762+
requestId,
763+
responseMode: "message",
764+
questions: [
765+
{ id: "scope", header: "Scope", question: "q".repeat(questionBytes), options: [] },
766+
],
767+
startedAt: NOW,
768+
completedAt: NOW,
769+
updatedAt: NOW,
770+
};
771+
const projection = {
772+
...makeProjection(rows),
773+
turnItems: [card, ...rows.map((row) => row.item)],
774+
runtimeRequests: [
775+
{
776+
id: requestId,
777+
nodeId: NODE,
778+
providerTurnId: null,
779+
nativeRequestRef: null,
780+
kind: "user_input" as const,
781+
status: "pending" as const,
782+
responseCapability: { type: "message" as const },
783+
createdAt: NOW,
784+
resolvedAt: null,
785+
},
786+
],
787+
};
788+
const policy = { ...THREAD_HISTORY_PAGE_POLICY, maxEncodedBytes: 64_000 };
789+
790+
const bounded = buildBoundedThreadProjection({ projection, snapshotSequence: 9, policy });
791+
792+
expect(bounded.projection.turnItems).toContain(card);
793+
expect(bounded.projection.visibleTurnItems.length).toBeGreaterThan(0);
794+
expect(bounded.hasMoreHistory).toBe(true);
795+
if (questionBytes < policy.maxEncodedBytes) {
796+
expect(Buffer.byteLength(JSON.stringify(bounded.projection), "utf8")).toBeLessThanOrEqual(
797+
policy.maxEncodedBytes,
798+
);
799+
} else {
800+
expect(bounded.projection.visibleTurnItems).toHaveLength(1);
801+
}
802+
},
803+
);
804+
741805
it("preserves oversized actionable state", () => {
742806
const projection = makeProjection([makeRow(0)]);
743807
const actionable = {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,10 +668,9 @@ export function buildBoundedThreadProjection(input: {
668668
turnItems: [],
669669
visibleTurnItems: [],
670670
});
671-
// Control state (checkpoint file lists, the latest task list) cannot be paged, so it may claim at most
672-
// half the budget; past that the window would shrink to the one row every page admits.
671+
// Keep the control-only window minimum, but required dependency cards claim their reserve first.
673672
const windowBudget = Math.max(
674-
policy.maxEncodedBytes / 2,
673+
dependencyReserve === 0 ? policy.maxEncodedBytes / 2 : 0,
675674
policy.maxEncodedBytes - dependencyReserve - controlBytes - 1_024,
676675
);
677676
const windowPolicy: ThreadHistoryPagePolicy = { ...policy, maxEncodedBytes: windowBudget };

‎packages/client-runtime/src/state/threadHistoryMerge.test.ts‎

Lines changed: 88 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as DateTime from "effect/DateTime";
55
import { v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
66
import { mergeOlderHistoryIntoProjection } from "./threadHistoryMerge.ts";
77
import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts";
8+
import { derivePendingThreadRequests } from "./threadRequests.ts";
89

910
const NOW = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
1011

@@ -223,50 +224,95 @@ describe("threadHistoryMerge", () => {
223224
});
224225

225226
// regression: #25806 a pending question card retained outside the window shows when paged in
226-
it("shows a pending request card retained at cold open when its history page arrives", () => {
227-
const recent = row(5);
228-
const card = {
229-
id: "item-question" as never,
230-
type: "user_input_request" as const,
231-
threadId: v2ThreadId,
232-
runId: null,
233-
nodeId: null,
234-
providerThreadId: null,
235-
providerTurnId: null,
236-
nativeItemRef: null,
237-
parentItemId: null,
238-
ordinal: 1,
239-
status: "running" as const,
240-
title: null,
241-
requestId: "request-question" as never,
242-
questions: [],
243-
startedAt: NOW,
244-
completedAt: null,
245-
updatedAt: NOW,
246-
};
247-
const coldOpen = {
248-
...v2Projection,
249-
turnItems: [card, recent.item],
250-
visibleTurnItems: [{ ...recent, position: 0 }],
251-
};
227+
it.each(["completed", "rolled_back"] as const)(
228+
"keeps a pending question actionable when paging a %s run",
229+
(status) => {
230+
const recent = row(5);
231+
const runId = "question-run" as never;
232+
const nodeId = "question-node" as never;
233+
const card = {
234+
id: "item-question" as never,
235+
type: "user_input_request" as const,
236+
threadId: v2ThreadId,
237+
runId,
238+
nodeId,
239+
providerThreadId: null,
240+
providerTurnId: null,
241+
nativeItemRef: null,
242+
parentItemId: null,
243+
ordinal: 1,
244+
status: "running" as const,
245+
title: null,
246+
requestId: "request-question" as never,
247+
responseMode: "message" as const,
248+
questions: [{ id: "scope", header: "Scope", question: "Which scope?", options: [] }],
249+
startedAt: NOW,
250+
completedAt: null,
251+
updatedAt: NOW,
252+
};
253+
const coldOpen = {
254+
...v2Projection,
255+
runs: [
256+
{
257+
id: runId,
258+
threadId: v2ThreadId,
259+
ordinal: 1,
260+
providerInstanceId: v2Projection.thread.providerInstanceId,
261+
modelSelection: v2Projection.thread.modelSelection,
262+
providerThreadId: null,
263+
userMessageId: "question-message" as never,
264+
rootNodeId: nodeId,
265+
activeAttemptId: null,
266+
status,
267+
requestedAt: NOW,
268+
startedAt: NOW,
269+
completedAt: NOW,
270+
checkpointId: null,
271+
contextHandoffId: null,
272+
},
273+
],
274+
runtimeRequests: [
275+
{
276+
id: card.requestId,
277+
nodeId,
278+
providerTurnId: null,
279+
nativeRequestRef: null,
280+
kind: "user_input" as const,
281+
status: "pending" as const,
282+
responseCapability: { type: "message" as const },
283+
createdAt: NOW,
284+
resolvedAt: null,
285+
},
286+
],
287+
turnItems: [card, recent.item],
288+
visibleTurnItems: [{ ...recent, position: 0 }],
289+
};
252290

253-
const merged = mergeOlderHistoryIntoProjection(coldOpen, [
254-
{ ...row(0), position: 0 },
255-
{
256-
position: 1,
257-
visibility: "local",
258-
sourceThreadId: v2ThreadId,
259-
sourceItemId: card.id,
260-
item: card,
261-
},
262-
]);
291+
const merged = mergeOlderHistoryIntoProjection(coldOpen, [
292+
{ ...row(0), position: 0 },
293+
{
294+
position: 1,
295+
visibility: "local",
296+
sourceThreadId: v2ThreadId,
297+
sourceItemId: card.id,
298+
item: card,
299+
},
300+
]);
263301

264-
expect(merged.visibleTurnItems.map((entry) => String(entry.sourceItemId))).toEqual([
265-
"item-0",
266-
"item-question",
267-
"item-5",
268-
]);
269-
});
302+
expect(merged.visibleTurnItems.map((entry) => String(entry.sourceItemId))).toEqual(
303+
status === "rolled_back" ? ["item-0", "item-5"] : ["item-0", "item-question", "item-5"],
304+
);
305+
expect(merged.turnItems).toContain(card);
306+
const pending = derivePendingThreadRequests(merged).userInputs;
307+
expect(pending).toEqual(derivePendingThreadRequests(coldOpen).userInputs);
308+
expect(pending).toHaveLength(1);
309+
expect(pending[0]).toMatchObject({
310+
requestId: card.requestId,
311+
dismissible: true,
312+
questions: card.questions.map((question) => ({ ...question, multiSelect: false })),
313+
});
314+
},
315+
);
270316

271317
it("keeps a history-page interrupt result visible when its request was retained at cold open", () => {
272318
const runId = "run-interrupt-cold" as never;

0 commit comments

Comments
 (0)