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

threadHistoryMerge.test.ts

547 lines17.2 KB

1import type { OrchestrationV2ProjectedTurnItem } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as DateTime from "effect/DateTime";
4​
5import { v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
6import { mergeOlderHistoryIntoProjection } from "./threadHistoryMerge.ts";
7import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts";
8import { derivePendingThreadRequests } from "./threadRequests.ts";
9​
10const NOW = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
11​
12function row(index: number): OrchestrationV2ProjectedTurnItem {
13 const id = `item-${index}` as never;
14 return {
15 position: index,
16 visibility: "local",
17 sourceThreadId: v2ThreadId,
18 sourceItemId: id,
19 item: {
20 id,
21 type: "command_execution",
22 threadId: v2ThreadId,
23 runId: null,
24 nodeId: null,
25 providerThreadId: null,
26 providerTurnId: null,
27 nativeItemRef: null,
28 parentItemId: null,
29 ordinal: index + 1,
30 status: "completed",
31 title: `Command ${index}`,
32 input: `cmd-${index}`,
33 output: `out-${index}`,
34 exitCode: 0,
35 startedAt: NOW,
36 completedAt: NOW,
37 updatedAt: NOW,
38 },
39 };
41​
42describe("threadHistoryMerge", () => {
43 it("fills activity around partial messages in canonical order and preserves live edits", () => {
44 const initial = { ...v2Projection, turnItems: [row(4).item], visibleTurnItems: [row(4)] };
45 const preview = [row(1), row(3)];
46 const partial = mergeOlderHistoryIntoProjection(initial, preview);
47 const updated = { ...row(1), item: { ...row(1).item, title: "newer live title" } };
48 const live = {
49 ...partial,
50 visibleTurnItems: partial.visibleTurnItems.map((entry) =>
51 entry.sourceItemId === updated.sourceItemId ? updated : entry,
52 ),
53 turnItems: partial.turnItems.map((item) =>
54 item.id === updated.item.id ? updated.item : item,
55 ),
56 };
57 const complete = mergeOlderHistoryIntoProjection(live, [row(0), row(1), row(2), row(3)]);
58 expect(complete.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
59 "item-0",
60 "item-1",
61 "item-2",
62 "item-3",
63 "item-4",
64 ]);
65 expect(complete.visibleTurnItems[1]?.item).toBe(updated.item);
66 expect(complete.visibleTurnItems.map((entry) => entry.position)).toEqual([0, 1, 2, 3, 4]);
67 expect(new Set(complete.turnItems.map((item) => item.id)).size).toBe(5);
68 const anchorItem = {
69 ...updated.item,
70 deferredWork: { cursor: "work", itemCount: 4, lastOrdinal: 5 },
71 };
72 const deferred = {
73 ...live,
74 turnItems: live.turnItems.map((item) => (item.id === anchorItem.id ? anchorItem : item)),
75 visibleTurnItems: live.visibleTurnItems.map((entry) =>
76 entry.item.id === anchorItem.id ? { ...entry, item: anchorItem } : entry,
77 ),
78 };
79 const loadedWork = mergeOlderHistoryIntoProjection(deferred, [row(1), row(2), row(3)], {
80 anchorId: anchorItem.id,
81 nextCursor: null,
82 });
83 expect(loadedWork.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
84 "item-1",
85 "item-2",
86 "item-3",
87 "item-4",
88 ]);
89 expect(loadedWork.visibleTurnItems[0]?.item.title).toBe("newer live title");
90 expect(loadedWork.visibleTurnItems[0]?.item.deferredWork).toBeUndefined();
91 const hidden = {
92 ...partial,
93 visibleTurnItems: partial.visibleTurnItems.filter(
94 (entry) => entry.sourceItemId !== preview[0]!.sourceItemId,
95 ),
96 };
97 const hiddenComplete = mergeOlderHistoryIntoProjection(hidden, [
98 row(0),
99 row(1),
100 row(2),
101 row(3),
102 ]);
103 expect(hiddenComplete.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
104 "item-0",
105 "item-2",
106 "item-3",
107 "item-4",
108 ]);
109 });
110​
111 it("prepends older rows and dedupes by source identity", () => {
112 const recent = [row(2), row(3)];
113 const projection = {
114 ...v2Projection,
115 turnItems: recent.map((entry) => entry.item),
116 visibleTurnItems: recent.map((entry, position) => ({ ...entry, position })),
117 };
118 const older = [row(0), row(1), row(2)];
119 const merged = mergeOlderHistoryIntoProjection(projection, older);
120 expect(merged.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
121 "item-0",
122 "item-1",
123 "item-2",
124 "item-3",
125 ]);
126 expect(merged.visibleTurnItems.map((entry) => entry.position)).toEqual([0, 1, 2, 3]);
127 expect(merged.turnItems.map((item) => item.id)).toEqual([
128 "item-2",
129 "item-3",
130 "item-0",
131 "item-1",
132 ]);
133 });
134​
135 it("fills inherited activity without confusing identical item IDs from different source threads", () => {
136 const inherited = (index: number) => ({
137 ...row(index),
138 visibility: "inherited" as const,
139 sourceThreadId: "fork-source" as never,
140 });
141 const local = row(1);
142 const preview = { ...inherited(1), item: { ...row(1).item, title: "newer inherited title" } };
143 const projection = {
144 ...v2Projection,
145 turnItems: [local.item],
146 visibleTurnItems: [preview, local],
147 };
148 const merged = mergeOlderHistoryIntoProjection(projection, [inherited(0), inherited(1)]);
149 expect(
150 merged.visibleTurnItems.map((entry) => [entry.sourceThreadId, entry.sourceItemId]),
151 ).toEqual([
152 ["fork-source", "item-0"],
153 ["fork-source", "item-1"],
154 [v2ThreadId, "item-1"],
155 ]);
156 expect(merged.visibleTurnItems[1]?.item).toBe(preview.item);
157 expect(merged.turnItems).toEqual([local.item]);
158 expect(mergeOlderHistoryIntoProjection(merged, [inherited(0), inherited(1)])).toBe(merged);
159 });
160​
161 it("retains live rows that arrived after the older page was fetched", () => {
162 const live = [row(5), row(6)];
163 const projection = {
164 ...v2Projection,
165 turnItems: live.map((entry) => entry.item),
166 visibleTurnItems: live.map((entry, position) => ({ ...entry, position })),
167 };
168 const older = [row(3), row(4)];
169 const merged = mergeOlderHistoryIntoProjection(projection, older);
170 expect(merged.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
171 "item-3",
172 "item-4",
173 "item-5",
174 "item-6",
175 ]);
176 });
177​
178 it("keeps an older answer before a full page that only covers the later part of a conversation page", () => {
179 const [question, olderAnswer, reasoningA, reasoningB, reasoningC, newerAnswer] = [
180 row(0),
181 row(1),
182 row(2),
183 row(3),
184 row(4),
185 row(5),
186 ];
187 const window = [row(6), row(7)];
188 const projection = {
189 ...v2Projection,
190 turnItems: window.map((entry) => entry.item),
191 visibleTurnItems: window.map((entry, position) => ({ ...entry, position })),
192 };
193 const conversation = mergeOlderHistoryIntoProjection(projection, [
194 question,
195 olderAnswer,
196 newerAnswer,
197 ]);
198 const full = mergeOlderHistoryIntoProjection(conversation, [
199 reasoningA,
200 reasoningB,
201 reasoningC,
202 newerAnswer,
203 ]);
204 expect(full.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
205 "item-0",
206 "item-1",
207 "item-2",
208 "item-3",
209 "item-4",
210 "item-5",
211 "item-6",
212 "item-7",
213 ]);
214 });
215​
216 it("does not resurrect a local item hidden while an older page was in flight", () => {
217 const stalePageRow = row(1);
218 const currentItem = {
219 ...stalePageRow.item,
220 output: "newer live output",
221 updatedAt: DateTime.makeUnsafe("2026-06-20T00:00:01.000Z"),
222 };
223 const projection = {
224 ...v2Projection,
225 turnItems: [currentItem],
226 visibleTurnItems: [],
227 };
228​
229 const merged = mergeOlderHistoryIntoProjection(projection, [stalePageRow]);
230​
231 expect(merged).toBe(projection);
232 expect(merged.turnItems).toEqual([currentItem]);
233 expect(merged.visibleTurnItems).toEqual([]);
234 });
235​
236 it("retains merged older rows when a live turn-item update arrives", () => {
237 const recent = [row(2), row(3)];
238 const projection = {
239 ...v2Projection,
240 turnItems: recent.map((entry) => entry.item),
241 visibleTurnItems: recent.map((entry, position) => ({ ...entry, position })),
242 };
243 const merged = mergeOlderHistoryIntoProjection(projection, [row(0), row(1)]);
244 const liveItem = {
245 ...row(3).item,
246 output: "streamed",
247 };
248 const next = applyOrchestrationV2ProjectionEvent(merged, {
249 id: "event-live-item" as never,
250 type: "turn-item.updated",
251 threadId: v2ThreadId,
252 occurredAt: NOW,
253 payload: liveItem,
254 } as never);
255 expect(next?.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
256 "item-0",
257 "item-1",
258 "item-2",
259 "item-3",
260 ]);
261 expect(next?.visibleTurnItems[3]?.item).toEqual(liveItem);
262 });
263​
264 // regression: #25806 a pending question card retained outside the window shows when paged in
265 it.each(["completed", "rolled_back"] as const)(
266 "keeps a pending question actionable when paging a %s run",
267 (status) => {
268 const recent = row(5);
269 const runId = "question-run" as never;
270 const nodeId = "question-node" as never;
271 const card = {
272 id: "item-question" as never,
273 type: "user_input_request" as const,
274 threadId: v2ThreadId,
275 runId,
276 nodeId,
277 providerThreadId: null,
278 providerTurnId: null,
279 nativeItemRef: null,
280 parentItemId: null,
281 ordinal: 1,
282 status: "running" as const,
283 title: null,
284 requestId: "request-question" as never,
285 responseMode: "message" as const,
286 questions: [{ id: "scope", header: "Scope", question: "Which scope?", options: [] }],
287 startedAt: NOW,
288 completedAt: null,
289 updatedAt: NOW,
290 };
291 const coldOpen = {
292 ...v2Projection,
293 runs: [
294 {
295 id: runId,
296 threadId: v2ThreadId,
297 ordinal: 1,
298 providerInstanceId: v2Projection.thread.providerInstanceId,
299 modelSelection: v2Projection.thread.modelSelection,
300 providerThreadId: null,
301 userMessageId: "question-message" as never,
302 rootNodeId: nodeId,
303 activeAttemptId: null,
304 status,
305 requestedAt: NOW,
306 startedAt: NOW,
307 completedAt: NOW,
308 checkpointId: null,
309 contextHandoffId: null,
310 },
311 ],
312 runtimeRequests: [
313 {
314 id: card.requestId,
315 nodeId,
316 providerTurnId: null,
317 nativeRequestRef: null,
318 kind: "user_input" as const,
319 status: "pending" as const,
320 responseCapability: { type: "message" as const },
321 createdAt: NOW,
322 resolvedAt: null,
323 },
324 ],
325 turnItems: [card, recent.item],
326 visibleTurnItems: [{ ...recent, position: 0 }],
327 };
328​
329 const merged = mergeOlderHistoryIntoProjection(coldOpen, [
330 { ...row(0), position: 0 },
331 {
332 position: 1,
333 visibility: "local",
334 sourceThreadId: v2ThreadId,
335 sourceItemId: card.id,
336 item: card,
337 },
338 ]);
339​
340 expect(merged.visibleTurnItems.map((entry) => String(entry.sourceItemId))).toEqual(
341 status === "rolled_back" ? ["item-0", "item-5"] : ["item-0", "item-question", "item-5"],
342 );
343 expect(merged.turnItems).toContain(card);
344 const pending = derivePendingThreadRequests(merged).userInputs;
345 expect(pending).toEqual(derivePendingThreadRequests(coldOpen).userInputs);
346 expect(pending).toHaveLength(1);
347 expect(pending[0]).toMatchObject({
348 requestId: card.requestId,
349 dismissible: true,
350 questions: card.questions.map((question) => ({ ...question, multiSelect: false })),
351 });
352 },
353 );
354​
355 it("keeps a history-page interrupt result visible when its request was retained at cold open", () => {
356 const runId = "run-interrupt-cold" as never;
357 const nodeId = "node-interrupt-cold" as never;
358 const recent = row(5);
359 const requestItem = {
360 id: "item-interrupt-request" as never,
361 type: "run_interrupt_request" as const,
362 threadId: v2ThreadId,
363 runId,
364 nodeId,
365 providerThreadId: null,
366 providerTurnId: null,
367 nativeItemRef: null,
368 parentItemId: null,
369 ordinal: 1,
370 status: "completed" as const,
371 title: null,
372 message: "Stopping",
373 startedAt: NOW,
374 completedAt: NOW,
375 updatedAt: NOW,
376 };
377 const resultItem = {
378 id: "item-interrupt-result" as never,
379 type: "run_interrupt_result" as const,
380 threadId: v2ThreadId,
381 runId,
382 nodeId,
383 providerThreadId: null,
384 providerTurnId: null,
385 nativeItemRef: null,
386 parentItemId: null,
387 ordinal: 2,
388 status: "completed" as const,
389 title: null,
390 message: "Stopped",
391 startedAt: NOW,
392 completedAt: NOW,
393 updatedAt: NOW,
394 };
395 const resultRow = {
396 position: 0,
397 visibility: "local" as const,
398 sourceThreadId: v2ThreadId,
399 sourceItemId: resultItem.id,
400 item: resultItem,
401 };
402 const coldOpen = {
403 ...v2Projection,
404 runs: [
405 {
406 id: runId,
407 threadId: v2ThreadId,
408 ordinal: 1,
409 providerInstanceId: "codex" as never,
410 modelSelection: { instanceId: "codex" as never, model: "gpt-5" },
411 providerThreadId: null,
412 userMessageId: "message-1" as never,
413 rootNodeId: nodeId,
414 activeAttemptId: null,
415 status: "running" as const,
416 requestedAt: NOW,
417 startedAt: NOW,
418 completedAt: null,
419 checkpointId: null,
420 contextHandoffId: null,
421 },
422 ],
423 attempts: [
424 {
425 id: "attempt-1" as never,
426 runId,
427 attemptOrdinal: 1,
428 rootNodeId: nodeId,
429 providerInstanceId: "codex" as never,
430 providerThreadId: "provider-thread-1" as never,
431 providerTurnId: null,
432 reason: "initial" as const,
433 status: "running" as const,
434 startedAt: NOW,
435 completedAt: null,
436 },
437 ],
438 turnItems: [requestItem, recent.item],
439 visibleTurnItems: [{ ...recent, position: 0 }],
440 };
441​
442 const requestMerged = mergeOlderHistoryIntoProjection(coldOpen, [
443 {
444 position: 0,
445 visibility: "local",
446 sourceThreadId: v2ThreadId,
447 sourceItemId: requestItem.id,
448 item: requestItem,
449 },
450 ]);
451 expect(requestMerged.visibleTurnItems.map((entry) => String(entry.sourceItemId))).toEqual([
452 "item-interrupt-request",
453 "item-5",
454 ]);
455​
456 const rolledBackRequestMerged = mergeOlderHistoryIntoProjection(
457 {
458 ...coldOpen,
459 runs: coldOpen.runs.map((run) => ({
460 ...run,
461 status: "rolled_back" as const,
462 completedAt: NOW,
463 })),
464 },
465 [
466 {
467 position: 0,
468 visibility: "local",
469 sourceThreadId: v2ThreadId,
470 sourceItemId: requestItem.id,
471 item: requestItem,
472 },
473 ],
474 );
475 expect(
476 rolledBackRequestMerged.visibleTurnItems.map((entry) => String(entry.sourceItemId)),
477 ).toEqual(["item-5"]);
478​
479 const merged = mergeOlderHistoryIntoProjection(coldOpen, [resultRow]);
480 expect(merged.visibleTurnItems.map((entry) => String(entry.sourceItemId))).toEqual([
481 "item-interrupt-result",
482 "item-5",
483 ]);
484 expect(merged.turnItems.some((item) => item.type === "run_interrupt_request")).toBe(true);
485 expect(merged.turnItems.some((item) => item.type === "run_interrupt_result")).toBe(true);
486​
487 const attemptSuperseded = applyOrchestrationV2ProjectionEvent(
488 merged,
489 {
490 id: "event-attempt-superseded" as never,
491 type: "run-attempt.updated",
492 threadId: v2ThreadId,
493 runId,
494 occurredAt: NOW,
495 payload: {
496 id: "attempt-1" as never,
497 runId,
498 attemptOrdinal: 1,
499 rootNodeId: nodeId,
500 providerInstanceId: "codex" as never,
501 providerThreadId: "provider-thread-1" as never,
502 providerTurnId: null,
503 reason: "initial",
504 status: "superseded",
505 startedAt: NOW,
506 completedAt: NOW,
507 },
508 } as never,
509 { partialTimeline: true },
510 );
511​
512 expect(attemptSuperseded?.visibleTurnItems.map((entry) => String(entry.sourceItemId))).toEqual([
513 "item-interrupt-result",
514 "item-5",
515 ]);
516​
517 const withoutRequest = {
518 ...merged,
519 turnItems: merged.turnItems.filter((item) => item.type !== "run_interrupt_request"),
520 };
521 const hidden = applyOrchestrationV2ProjectionEvent(
522 withoutRequest,
523 {
524 id: "event-attempt-superseded-missing" as never,
525 type: "run-attempt.updated",
526 threadId: v2ThreadId,
527 runId,
528 occurredAt: NOW,
529 payload: {
530 id: "attempt-1" as never,
531 runId,
532 attemptOrdinal: 1,
533 rootNodeId: nodeId,
534 providerInstanceId: "codex" as never,
535 providerThreadId: "provider-thread-1" as never,
536 providerTurnId: null,
537 reason: "initial",
538 status: "superseded",
539 startedAt: NOW,
540 completedAt: NOW,
541 },
542 } as never,
543 { partialTimeline: true },
544 );
545 expect(hidden?.visibleTurnItems.map((entry) => String(entry.sourceItemId))).toEqual(["item-5"]);
546 });
547});