Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadHistoryPaging.test.ts

690 lines22.8 KB

1import {
2 MessageId,
3 NodeId,
4 RunId,
5 ThreadId,
6 TurnItemId,
7 type OrchestrationV2ProjectedTurnItem,
8 type OrchestrationV2TurnItem,
9} from "@t3tools/contracts";
10import { describe, expect, it } from "@effect/vitest";
11import * as DateTime from "effect/DateTime";
12​
13import {
14 buildBoundedThreadProjection,
15 boundedTimelineEncodedBytes,
16 computeLatestLocalTurnOrdinal,
17 encodeThreadHistoryCursor,
18 projectedRowBoundedSnapshotEncodedBytes,
19 projectedRowEncodedBytes,
20 selectHistoryPageFromCursor,
21 selectRecentTimelineWindow,
22 THREAD_HISTORY_PAGE_POLICY,
23} from "./threadHistoryPaging.ts";
24import { projectThreadProjectionForWire } from "./WireProjection.ts";
25​
26const NOW = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
27const THREAD = ThreadId.make("thread-1");
28const RUN = RunId.make("run-interrupt-1");
29const NODE = NodeId.make("node-interrupt-1");
30​
31function makeRow(
32 index: number,
33 options?: { readonly outputBytes?: number },
34): OrchestrationV2ProjectedTurnItem {
35 const id = TurnItemId.make(`item-${index}`);
36 const output =
37 options?.outputBytes !== undefined ? "x".repeat(options.outputBytes) : `output-${index}`;
38 return {
39 position: index,
40 visibility: "local",
41 sourceThreadId: THREAD,
42 sourceItemId: id,
43 item: {
44 id,
45 type: "command_execution",
46 threadId: THREAD,
47 runId: null,
48 nodeId: null,
49 providerThreadId: null,
50 providerTurnId: null,
51 nativeItemRef: null,
52 parentItemId: null,
53 ordinal: index + 1,
54 status: "completed",
55 title: `Command ${index}`,
56 input: `cmd-${index}`,
57 output,
58 exitCode: 0,
59 startedAt: NOW,
60 completedAt: NOW,
61 updatedAt: NOW,
62 },
63 };
65​
66function interruptItem(
67 id: string,
68 type: "run_interrupt_request" | "run_interrupt_result",
69 ordinal: number,
70): OrchestrationV2TurnItem {
71 return {
72 id: TurnItemId.make(id),
73 type,
74 threadId: THREAD,
75 runId: RUN,
76 nodeId: NODE,
77 providerThreadId: null,
78 providerTurnId: null,
79 nativeItemRef: null,
80 parentItemId: null,
81 ordinal,
82 status: "completed",
83 title: type,
84 message: type === "run_interrupt_request" ? "Stopping" : "Stopped",
85 startedAt: NOW,
86 completedAt: NOW,
87 updatedAt: NOW,
88 } as OrchestrationV2TurnItem;
90​
91function interruptRow(
92 index: number,
93 type: "run_interrupt_request" | "run_interrupt_result",
94): OrchestrationV2ProjectedTurnItem {
95 const item = interruptItem(
96 type === "run_interrupt_request" ? "interrupt-req" : "interrupt-res",
97 type,
98 index + 1,
99 );
100 return {
101 position: index,
102 visibility: "local",
103 sourceThreadId: THREAD,
104 sourceItemId: item.id,
105 item,
106 };
108​
109function assistantRow(
110 text: string,
111 visibility: "local" | "inherited" = "local",
112): OrchestrationV2ProjectedTurnItem {
113 const row = makeRow(0);
114 const sourceThreadId = visibility === "local" ? THREAD : ThreadId.make("parent-thread");
115 return {
116 ...row,
117 visibility,
118 sourceThreadId,
119 item: {
120 ...row.item,
121 threadId: sourceThreadId,
122 type: "assistant_message",
123 messageId: MessageId.make("answer-0"),
124 text,
125 streaming: false,
126 },
127 } as OrchestrationV2ProjectedTurnItem;
129​
130function makeProjection(visibleTurnItems: OrchestrationV2ProjectedTurnItem[]) {
131 return {
132 thread: {
133 id: THREAD,
134 projectId: "project-1",
135 title: "Thread",
136 providerInstanceId: "codex",
137 modelSelection: { instanceId: "codex", model: "gpt-5" },
138 runtimeMode: "full-access",
139 interactionMode: "default",
140 branch: null,
141 worktreePath: null,
142 activeProviderThreadId: null,
143 lineage: {
144 parentThreadId: null,
145 relationshipToParent: null,
146 rootThreadId: THREAD,
147 },
148 forkedFrom: null,
149 createdBy: "user",
150 creationSource: "web",
151 createdAt: NOW,
152 updatedAt: NOW,
153 archivedAt: null,
154 deletedAt: null,
155 settledOverride: null,
156 settledAt: null,
157 },
158 runs: [{ id: "run-1" }],
159 attempts: [{ id: "attempt-1" }],
160 nodes: [],
161 subagents: [],
162 providerSessions: [],
163 providerThreads: [],
164 providerTurns: [],
165 runtimeRequests: [{ id: "req-1" }],
166 messages: [],
167 plans: [],
168 turnItems: visibleTurnItems.map((row) => row.item),
169 checkpointScopes: [],
170 checkpoints: [],
171 contextHandoffs: [],
172 contextTransfers: [],
173 visibleTurnItems,
174 updatedAt: NOW,
175 } as unknown as Parameters<typeof buildBoundedThreadProjection>[0]["projection"];
177​
178describe("threadHistoryPaging", () => {
179 it("keeps background turns with user turns, with main's 150-turn fan-out ceiling", () => {
180 const items = Array.from({ length: 161 }, (_, turn) => {
181 const row = makeRow(turn * 2);
182 if (row.item.type !== "command_execution") throw new Error("Expected command fixture");
183 const prompt: OrchestrationV2ProjectedTurnItem = {
184 ...row,
185 item: {
186 ...row.item,
187 type: "user_message",
188 createdBy: turn === 0 ? "user" : "agent",
189 creationSource: "provider",
190 inputIntent: "turn_start",
191 messageId: MessageId.make(`prompt-${turn}`),
192 text: `Prompt ${turn}`,
193 attachments: [],
194 },
195 };
196 return [prompt, makeRow(turn * 2 + 1)];
197 }).flat();
198 const first = selectRecentTimelineWindow({ items, snapshotSequence: 1 });
199 expect(first.items).toHaveLength(300);
200 expect(first.items[0]?.sourceItemId).toBe("item-22");
201 const older = selectHistoryPageFromCursor({
202 items,
203 cursor: first.nextCursor!,
204 snapshotSequence: 1,
205 });
206 expect(older.items).toHaveLength(22);
207 expect(older.hasMoreHistory).toBe(false);
208 expect([...older.items, ...first.items].map((row) => row.sourceItemId)).toEqual(
209 items.map((row) => row.sourceItemId),
210 );
211 });
212​
213 it("counts runs that a notification started toward the 150-turn ceiling", () => {
214 const inRun = (index: number, run: number, item: object = {}) => {
215 const row = makeRow(index);
216 return {
217 ...row,
218 item: { ...row.item, runId: RunId.make(`run-${run}`), ...item },
219 } as OrchestrationV2ProjectedTurnItem;
220 };
221 const notification = (summary: string) => ({
222 type: "notification",
223 source: { kind: "monitor" },
224 outcome: "completed",
225 summary,
226 });
227 const items = Array.from({ length: 161 }, (_, run) => [
228 inRun(run * 4, run),
229 inRun(
230 run * 4 + 1,
231 run,
232 run === 0
233 ? {
234 type: "user_message",
235 createdBy: "user",
236 creationSource: "web",
237 inputIntent: "turn_start",
238 messageId: MessageId.make("prompt-0"),
239 text: "Prompt 0",
240 attachments: [],
241 }
242 : notification(`Wake ${run}`),
243 ),
244 inRun(run * 4 + 2, run, notification(`Steer ${run}`)),
245 inRun(run * 4 + 3, run),
246 ]).flat();
247 const first = selectRecentTimelineWindow({ items, snapshotSequence: 1 });
248 expect(first.items).toHaveLength(150 * 4 - 1);
249 expect(first.items[0]?.sourceItemId).toBe(`item-${11 * 4 + 1}`);
250 const older = selectHistoryPageFromCursor({
251 items,
252 cursor: first.nextCursor!,
253 snapshotSequence: 1,
254 });
255 expect([...older.items, ...first.items].map((row) => row.sourceItemId)).toEqual(
256 items.map((row) => row.sourceItemId),
257 );
258 });
259​
260 it("pages agent-only child transcripts instead of dropping their earlier activity", () => {
261 const commandRows = Array.from({ length: 90 }, (_, index) => makeRow(index + 1));
262 const first = makeRow(0);
263 const prompt = {
264 ...first,
265 item: {
266 ...first.item,
267 type: "user_message" as const,
268 createdBy: "agent" as const,
269 creationSource: "provider" as const,
270 inputIntent: "turn_start" as const,
271 messageId: MessageId.make("child-prompt"),
272 text: "Inspect this project",
273 attachments: [],
274 },
275 } as OrchestrationV2ProjectedTurnItem;
276 const items = [prompt, ...commandRows];
277 const recent = selectRecentTimelineWindow({ items, snapshotSequence: 1 });
278​
279 expect(recent.items).toHaveLength(THREAD_HISTORY_PAGE_POLICY.maxItems);
280 expect(recent.hasMoreHistory).toBe(true);
281 const older = selectHistoryPageFromCursor({
282 items,
283 cursor: recent.nextCursor!,
284 snapshotSequence: 1,
285 });
286 expect(older.items[0]?.item.type).toBe("user_message");
287 expect([...older.items, ...recent.items]).toHaveLength(items.length);
288 });
289​
290 it("always includes at least one pathological oversized item", () => {
291 const items = [makeRow(0, { outputBytes: 2_000_000 }), makeRow(1, { outputBytes: 10 })];
292 const page = selectRecentTimelineWindow({
293 items,
294 snapshotSequence: 1,
295 policy: { maxItems: 50, maxEncodedBytes: 1_024 },
296 });
297 expect(page.items).toHaveLength(1);
298 expect(page.items[0]?.sourceItemId).toBe("item-1");
299 expect(page.hasMoreHistory).toBe(true);
300​
301 const older = selectHistoryPageFromCursor({
302 items,
303 cursor: page.nextCursor!,
304 snapshotSequence: 1,
305 policy: { maxItems: 50, maxEncodedBytes: 1_024 },
306 });
307 expect(older.items).toHaveLength(1);
308 expect(older.items[0]?.sourceItemId).toBe("item-0");
309 expect(older.hasMoreHistory).toBe(false);
310 expect(older.nextCursor).toBeNull();
311 });
312​
313 it("recovers identity-miss cursors when position is past a shrunken timeline", () => {
314 const items = Array.from({ length: 5 }, (_, index) => makeRow(index));
315 const cursor = encodeThreadHistoryCursor({
316 snapshotSequence: 3,
317 sourceThreadId: THREAD,
318 sourceItemId: TurnItemId.make("item-deleted-long-ago"),
319 position: 40,
320 });
321 const page = selectHistoryPageFromCursor({
322 items,
323 cursor,
324 snapshotSequence: 3,
325 policy: { maxItems: 2, maxEncodedBytes: 10_000_000 },
326 });
327 expect(page.items.map((row) => row.sourceItemId)).toEqual(["item-3", "item-4"]);
328 expect(page.hasMoreHistory).toBe(true);
329 expect(page.nextCursor).not.toBeNull();
330 });
331​
332 it("keeps cursor resolution stable when newer items append", () => {
333 const initial = Array.from({ length: 20 }, (_, index) => makeRow(index));
334 const recent = selectRecentTimelineWindow({
335 items: initial,
336 snapshotSequence: 2,
337 policy: { maxItems: 5, maxEncodedBytes: 10_000_000 },
338 });
339 const cursor = recent.nextCursor!;
340 const grown = [...initial, makeRow(20), makeRow(21)];
341 const older = selectHistoryPageFromCursor({
342 items: grown,
343 cursor,
344 snapshotSequence: 5,
345 policy: { maxItems: 5, maxEncodedBytes: 10_000_000 },
346 });
347 expect(older.items.map((row) => row.sourceItemId)).toEqual([
348 "item-10",
349 "item-11",
350 "item-12",
351 "item-13",
352 "item-14",
353 ]);
354 expect(older.hasMoreHistory).toBe(true);
355 });
356​
357 it("builds a bounded projection that preserves non-timeline control state", () => {
358 const full = makeProjection(Array.from({ length: 40 }, (_, index) => makeRow(index)));
359 const bounded = buildBoundedThreadProjection({
360 projection: full,
361 snapshotSequence: 7,
362 policy: { maxItems: 5, maxEncodedBytes: 10_000_000 },
363 });
364 expect(bounded.projection.runs).toEqual(full.runs);
365 expect(bounded.projection.runtimeRequests).toEqual(full.runtimeRequests);
366 expect(bounded.projection.attempts).toEqual(full.attempts);
367 expect(bounded.projection.visibleTurnItems).toHaveLength(5);
368 expect(bounded.projection.turnItems).toHaveLength(5);
369 expect(bounded.hasMoreHistory).toBe(true);
370 expect(bounded.historyCursor).not.toBeNull();
371 expect(bounded.projection.visibleTurnItems.map((row) => row.position)).toEqual([0, 1, 2, 3, 4]);
372 expect(bounded.latestLocalTurnOrdinal).toBe(40);
373 expect(computeLatestLocalTurnOrdinal(full.turnItems)).toBe(40);
374 });
375​
376 it("carries a full-projection watermark when the bounded window is inherited-only", () => {
377 const localOlder = makeRow(0);
378 const localMid = makeRow(1);
379 const inherited = {
380 ...makeRow(2),
381 visibility: "inherited" as const,
382 sourceThreadId: ThreadId.make("parent-thread"),
383 item: {
384 ...makeRow(2).item,
385 threadId: ThreadId.make("parent-thread"),
386 },
387 };
388 const full = makeProjection([localOlder, localMid, inherited]);
389 const inheritedOnly = {
390 ...full,
391 visibleTurnItems: [inherited],
392 turnItems: [localOlder.item, localMid.item],
393 };
394 const bounded = buildBoundedThreadProjection({
395 projection: inheritedOnly,
396 snapshotSequence: 2,
397 policy: { maxItems: 1, maxEncodedBytes: 10_000_000 },
398 });
399 expect(bounded.projection.visibleTurnItems.map((row) => String(row.sourceItemId))).toEqual([
400 String(inherited.sourceItemId),
401 ]);
402 expect(bounded.latestLocalTurnOrdinal).toBe(2);
403 expect(computeLatestLocalTurnOrdinal([])).toBeNull();
404 });
405​
406 it("retains an out-of-window run_interrupt_request needed by a visible result", () => {
407 const request = interruptRow(0, "run_interrupt_request");
408 const filler = makeRow(1);
409 const result = interruptRow(2, "run_interrupt_result");
410 const full = makeProjection([request, filler, result]);
411 const bounded = buildBoundedThreadProjection({
412 projection: full,
413 snapshotSequence: 3,
414 policy: { maxItems: 1, maxEncodedBytes: 10_000_000 },
415 });
416​
417 expect(bounded.projection.visibleTurnItems.map((row) => String(row.sourceItemId))).toEqual([
418 "interrupt-res",
419 ]);
420 expect(bounded.projection.turnItems.map((item) => String(item.id)).sort()).toEqual([
421 "interrupt-req",
422 "interrupt-res",
423 ]);
424 expect(bounded.hasMoreHistory).toBe(true);
425 });
426​
427 it("charges local row.item duplication when measuring bounded timeline bytes", () => {
428 const rows = Array.from({ length: 8 }, (_, index) => makeRow(index, { outputBytes: 2_000 }));
429 const full = makeProjection(rows);
430 const maxEncodedBytes = 12_000;
431 const bounded = buildBoundedThreadProjection({
432 projection: full,
433 snapshotSequence: 1,
434 policy: { maxItems: 50, maxEncodedBytes },
435 });
436​
437 const contribution = boundedTimelineEncodedBytes({
438 visibleTurnItems: bounded.projection.visibleTurnItems,
439 turnItems: bounded.projection.turnItems,
440 });
441 expect(contribution).toBeLessThanOrEqual(maxEncodedBytes);
442 expect(bounded.projection.visibleTurnItems.length).toBeGreaterThan(0);
443 expect(bounded.projection.visibleTurnItems.length).toBeLessThan(rows.length);
444​
445 const historyPage = selectRecentTimelineWindow({
446 items: rows,
447 snapshotSequence: 1,
448 policy: { maxItems: 50, maxEncodedBytes },
449 });
450 const historyBytes = historyPage.items.reduce(
451 (sum, row) => sum + projectedRowEncodedBytes(row),
452 0,
453 );
454 expect(historyBytes).toBeLessThanOrEqual(maxEncodedBytes);
455 expect(historyPage.items.length).toBeGreaterThan(bounded.projection.visibleTurnItems.length);
456​
457 const sample = rows[0]!;
458 expect(projectedRowBoundedSnapshotEncodedBytes(sample, THREAD)).toBeGreaterThan(
459 projectedRowEncodedBytes(sample),
460 );
461 });
462​
463 it("does not carry the full duplicated message table into a bounded snapshot", () => {
464 const projection = makeProjection(Array.from({ length: 120 }, (_, index) => makeRow(index)));
465 const full = {
466 ...projection,
467 messages: Array.from({ length: 2_000 }, (_, index) => ({
468 id: MessageId.make(`message-${index}`),
469 threadId: THREAD,
470 runId: RunId.make(`old-run-${index}`),
471 nodeId: null,
472 role: "assistant" as const,
473 text: "x".repeat(1_000),
474 attachments: [],
475 streaming: false,
476 createdBy: "agent" as const,
477 creationSource: "provider" as const,
478 createdAt: NOW,
479 updatedAt: NOW,
480 })),
481 } as typeof projection;
482​
483 const bounded = buildBoundedThreadProjection({
484 projection: full,
485 snapshotSequence: 9,
486 });
487​
488 expect(bounded.projection.messages).toEqual([]);
489 expect(Buffer.byteLength(JSON.stringify(bounded.projection), "utf8")).toBeLessThan(
490 THREAD_HISTORY_PAGE_POLICY.maxEncodedBytes + 100_000,
491 );
492 });
493​
494 it("keeps a workflow coordinator's script outside the bounded window", () => {
495 const projection = makeProjection(Array.from({ length: 120 }, (_, index) => makeRow(index)));
496 const message = (index: number, workflowScript?: boolean) => ({
497 id: MessageId.make(`message-${index}`),
498 threadId: THREAD,
499 runId: RunId.make(`old-run-${index}`),
500 nodeId: null,
501 role: "user" as const,
502 text: `message ${index}`,
503 attachments: [],
504 streaming: false,
505 createdBy: "agent" as const,
506 creationSource: "provider" as const,
507 createdAt: NOW,
508 updatedAt: NOW,
509 ...(workflowScript === undefined ? {} : { workflowScript }),
510 });
511 const full = {
512 ...projection,
513 messages: [message(0, true), message(1, false), message(2)],
514 } as typeof projection;
515​
516 const bounded = buildBoundedThreadProjection({ projection: full, snapshotSequence: 9 });
517​
518 expect(bounded.projection.messages.map((retained) => retained.id)).toEqual([
519 MessageId.make("message-0"),
520 ]);
521 });
522​
523 it("omits historical control details that remain available from history items", () => {
524 const projection = makeProjection(Array.from({ length: 200 }, (_, index) => makeRow(index)));
525 const populated = {
526 ...projection,
527 plans: Array.from({ length: 120 }, (_, index) => ({
528 id: `plan-${index}`,
529 threadId: THREAD,
530 runId: null,
531 nodeId: `node-${index}`,
532 status: "completed",
533 kind: "proposed_plan",
534 markdown: "p".repeat(2_097_152),
535 })),
536 contextHandoffs: Array.from({ length: 120 }, (_, index) => ({
537 id: `handoff-${index}`,
538 threadId: THREAD,
539 targetRunId: "run-1",
540 fromProviderThreadIds: [],
541 toProviderThreadId: "provider-thread-1",
542 coveredRunOrdinals: { from: 1, to: 1 },
543 strategy: "manual_context",
544 status: "superseded",
545 summaryMessageId: null,
546 summaryText: "h".repeat(2_097_152),
547 createdByProviderInstanceId: null,
548 createdAt: NOW,
549 updatedAt: NOW,
550 })),
551 } as unknown as typeof projection;
552​
553 const bounded = buildBoundedThreadProjection({
554 projection: projectThreadProjectionForWire(populated),
555 snapshotSequence: 9,
556 });
557 const serializedBytes = Buffer.byteLength(JSON.stringify(bounded.projection), "utf8");
558​
559 expect(serializedBytes).toBeLessThanOrEqual(THREAD_HISTORY_PAGE_POLICY.maxEncodedBytes);
560 expect(bounded.payloadBudgetExceeded).toBe(false);
561 expect(bounded.projection.plans).toEqual([]);
562 expect(bounded.projection.contextHandoffs).toEqual([]);
563 expect(populated.plans).toHaveLength(120);
564 expect(
565 populated.plans[0]?.kind === "proposed_plan" ? populated.plans[0].markdown.length : 0,
566 ).toBe(2_097_152);
567 });
568​
569 it("preserves oversized actionable state and reports the budget exception", () => {
570 const projection = makeProjection([makeRow(0)]);
571 const actionable = {
572 ...projection,
573 plans: [
574 {
575 id: "plan-actionable",
576 threadId: THREAD,
577 runId: null,
578 nodeId: "node-actionable",
579 status: "active",
580 kind: "proposed_plan",
581 markdown: "a".repeat(2_097_152),
582 },
583 ],
584 } as unknown as typeof projection;
585​
586 const bounded = buildBoundedThreadProjection({ projection: actionable, snapshotSequence: 9 });
587​
588 expect(bounded.payloadBudgetExceeded).toBe(true);
589 expect(bounded.projection.plans[0]).toEqual(actionable.plans[0]);
590 expect(bounded.projection.visibleTurnItems).toEqual(actionable.visibleTurnItems);
591 });
592​
593 it("flips overflow at the exact encoded size when timeline text fits the budget", () => {
594 // Inherited rows are encoded once, local rows twice; neither may overflow early.
595 for (const visibility of ["local", "inherited"] as const) {
596 const projection = makeProjection([assistantRow("a".repeat(20_000), visibility)]);
597 const build = (maxEncodedBytes: number) =>
598 buildBoundedThreadProjection({
599 projection,
600 snapshotSequence: 1,
601 policy: { maxItems: 75, maxEncodedBytes },
602 });
603 const bytes = Buffer.byteLength(
604 JSON.stringify(build(Number.MAX_SAFE_INTEGER).projection),
605 "utf8",
606 );
607 expect(build(bytes).payloadBudgetExceeded).toBe(false);
608 expect(build(bytes - 1).payloadBudgetExceeded).toBe(true);
609 }
610 });
611​
612 it("keeps agent-only turns visible when checkpoint file lists fill the byte budget", () => {
613 const items = Array.from({ length: 3 }, (_, turn) => {
614 const first = makeRow(turn * 5);
615 const prompt = {
616 ...first,
617 item: {
618 ...first.item,
619 type: "user_message" as const,
620 createdBy: "agent" as const,
621 creationSource: "provider" as const,
622 inputIntent: "turn_start" as const,
623 messageId: MessageId.make(`agent-prompt-${turn}`),
624 text: `Step ${turn}`,
625 attachments: [],
626 },
627 } as OrchestrationV2ProjectedTurnItem;
628 return [prompt, ...Array.from({ length: 4 }, (_, index) => makeRow(turn * 5 + index + 1))];
629 }).flat();
630 const files = Array.from(
631 { length: 15_000 },
632 (_, index) =>
633 `work/ws-${index % 40}/.jj/repo/store/extra/${index.toString(16).padStart(40, "0")}`,
634 );
635 const projection = {
636 ...makeProjection(items),
637 checkpoints: [{ id: "checkpoint-1", files }],
638 } as unknown as ReturnType<typeof makeProjection>;
639​
640 const bounded = buildBoundedThreadProjection({ projection, snapshotSequence: 1 });
641​
642 expect(bounded.projection.visibleTurnItems).toHaveLength(items.length);
643 expect(bounded.hasMoreHistory).toBe(false);
644 expect(bounded.payloadBudgetExceeded).toBe(true);
645 });
646​
647 it("keeps paged historical plan detail in the turn item and only status in its artifact", () => {
648 const detail = "Implement the historical plan exactly.\n".repeat(1_000);
649 const item = {
650 ...makeRow(0).item,
651 type: "proposed_plan",
652 planId: "plan-historical-visible",
653 markdown: detail,
654 streaming: false,
655 } as OrchestrationV2TurnItem;
656 const row = {
657 ...makeRow(0),
658 sourceItemId: item.id,
659 item,
660 } as OrchestrationV2ProjectedTurnItem;
661 const base = makeProjection([row]);
662 const projection = {
663 ...base,
664 plans: [
665 {
666 id: "plan-historical-visible",
667 threadId: THREAD,
668 runId: null,
669 nodeId: "node-historical-visible",
670 status: "completed",
671 kind: "proposed_plan",
672 markdown: detail,
673 },
674 ],
675 } as unknown as typeof base;
676​
677 const bounded = buildBoundedThreadProjection({ projection, snapshotSequence: 9 });
678​
679 expect(bounded.projection.visibleTurnItems[0]?.item).toMatchObject({
680 type: "proposed_plan",
681 markdown: detail,
682 });
683 expect(bounded.projection.plans[0]).toMatchObject({
684 status: "completed",
685 markdown: "",
686 detailInTurnItem: true,
687 });
688 expect(bounded.payloadBudgetExceeded).toBe(false);
689 });
690});