Open History to see the last commit
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";
8
9const NOW = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
10
11function row(index: number): OrchestrationV2ProjectedTurnItem {
12 const id = `item-${index}` as never;
13 return {
14 position: index,
15 visibility: "local",
16 sourceThreadId: v2ThreadId,
17 sourceItemId: id,
18 item: {
19 id,
20 type: "command_execution",
21 threadId: v2ThreadId,
22 runId: null,
23 nodeId: null,
24 providerThreadId: null,
25 providerTurnId: null,
26 nativeItemRef: null,
27 parentItemId: null,
28 ordinal: index + 1,
29 status: "completed",
30 title: `Command ${index}`,
31 input: `cmd-${index}`,
32 output: `out-${index}`,
33 exitCode: 0,
34 startedAt: NOW,
35 completedAt: NOW,
36 updatedAt: NOW,
37 },
38 };
39}
40
41describe("threadHistoryMerge", () => {
42 it("fills activity around partial messages in canonical order and preserves live edits", () => {
43 const initial = { ...v2Projection, turnItems: [row(4).item], visibleTurnItems: [row(4)] };
44 const preview = [row(1), row(3)];
45 const partial = mergeOlderHistoryIntoProjection(initial, preview);
46 const updated = { ...row(1), item: { ...row(1).item, title: "newer live title" } };
47 const live = {
48 ...partial,
49 visibleTurnItems: partial.visibleTurnItems.map((entry) =>
50 entry.sourceItemId === updated.sourceItemId ? updated : entry,
51 ),
52 turnItems: partial.turnItems.map((item) =>
53 item.id === updated.item.id ? updated.item : item,
54 ),
55 };
56 const complete = mergeOlderHistoryIntoProjection(live, [row(0), row(1), row(2), row(3)]);
57 expect(complete.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
58 "item-0",
59 "item-1",
60 "item-2",
61 "item-3",
62 "item-4",
63 ]);
64 expect(complete.visibleTurnItems[1]?.item).toBe(updated.item);
65 expect(complete.visibleTurnItems.map((entry) => entry.position)).toEqual([0, 1, 2, 3, 4]);
66 expect(new Set(complete.turnItems.map((item) => item.id)).size).toBe(5);
67 const hidden = {
68 ...partial,
69 visibleTurnItems: partial.visibleTurnItems.filter(
70 (entry) => entry.sourceItemId !== preview[0]!.sourceItemId,
71 ),
72 };
73 const hiddenComplete = mergeOlderHistoryIntoProjection(hidden, [
74 row(0),
75 row(1),
76 row(2),
77 row(3),
78 ]);
79 expect(hiddenComplete.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
80 "item-0",
81 "item-2",
82 "item-3",
83 "item-4",
84 ]);
85 });
86
87 it("prepends older rows and dedupes by source identity", () => {
88 const recent = [row(2), row(3)];
89 const projection = {
90 ...v2Projection,
91 turnItems: recent.map((entry) => entry.item),
92 visibleTurnItems: recent.map((entry, position) => ({ ...entry, position })),
93 };
94 const older = [row(0), row(1), row(2)];
95 const merged = mergeOlderHistoryIntoProjection(projection, older);
96 expect(merged.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
97 "item-0",
98 "item-1",
99 "item-2",
100 "item-3",
101 ]);
102 expect(merged.visibleTurnItems.map((entry) => entry.position)).toEqual([0, 1, 2, 3]);
103 expect(merged.turnItems.map((item) => item.id)).toEqual([
104 "item-2",
105 "item-3",
106 "item-0",
107 "item-1",
108 ]);
109 });
110
111 it("fills inherited activity without confusing identical item IDs from different source threads", () => {
112 const inherited = (index: number) => ({
113 ...row(index),
114 visibility: "inherited" as const,
115 sourceThreadId: "fork-source" as never,
116 });
117 const local = row(1);
118 const preview = { ...inherited(1), item: { ...row(1).item, title: "newer inherited title" } };
119 const projection = {
120 ...v2Projection,
121 turnItems: [local.item],
122 visibleTurnItems: [preview, local],
123 };
124 const merged = mergeOlderHistoryIntoProjection(projection, [inherited(0), inherited(1)]);
125 expect(
126 merged.visibleTurnItems.map((entry) => [entry.sourceThreadId, entry.sourceItemId]),
127 ).toEqual([
128 ["fork-source", "item-0"],
129 ["fork-source", "item-1"],
130 [v2ThreadId, "item-1"],
131 ]);
132 expect(merged.visibleTurnItems[1]?.item).toBe(preview.item);
133 expect(merged.turnItems).toEqual([local.item]);
134 expect(mergeOlderHistoryIntoProjection(merged, [inherited(0), inherited(1)])).toBe(merged);
135 });
136
137 it("retains live rows that arrived after the older page was fetched", () => {
138 const live = [row(5), row(6)];
139 const projection = {
140 ...v2Projection,
141 turnItems: live.map((entry) => entry.item),
142 visibleTurnItems: live.map((entry, position) => ({ ...entry, position })),
143 };
144 const older = [row(3), row(4)];
145 const merged = mergeOlderHistoryIntoProjection(projection, older);
146 expect(merged.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
147 "item-3",
148 "item-4",
149 "item-5",
150 "item-6",
151 ]);
152 });
153
154 it("does not resurrect a local item hidden while an older page was in flight", () => {
155 const stalePageRow = row(1);
156 const currentItem = {
157 ...stalePageRow.item,
158 output: "newer live output",
159 updatedAt: DateTime.makeUnsafe("2026-06-20T00:00:01.000Z"),
160 };
161 const projection = {
162 ...v2Projection,
163 turnItems: [currentItem],
164 visibleTurnItems: [],
165 };
166
167 const merged = mergeOlderHistoryIntoProjection(projection, [stalePageRow]);
168
169 expect(merged).toBe(projection);
170 expect(merged.turnItems).toEqual([currentItem]);
171 expect(merged.visibleTurnItems).toEqual([]);
172 });
173
174 it("retains merged older rows when a live turn-item update arrives", () => {
175 const recent = [row(2), row(3)];
176 const projection = {
177 ...v2Projection,
178 turnItems: recent.map((entry) => entry.item),
179 visibleTurnItems: recent.map((entry, position) => ({ ...entry, position })),
180 };
181 const merged = mergeOlderHistoryIntoProjection(projection, [row(0), row(1)]);
182 const liveItem = {
183 ...row(3).item,
184 output: "streamed",
185 };
186 const next = applyOrchestrationV2ProjectionEvent(merged, {
187 id: "event-live-item" as never,
188 type: "turn-item.updated",
189 threadId: v2ThreadId,
190 occurredAt: NOW,
191 payload: liveItem,
192 } as never);
193 expect(next?.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
194 "item-0",
195 "item-1",
196 "item-2",
197 "item-3",
198 ]);
199 expect(next?.visibleTurnItems[3]?.item).toEqual(liveItem);
200 });
201
202 it("keeps a history-page interrupt result visible when its request was retained at cold open", () => {
203 const runId = "run-interrupt-cold" as never;
204 const nodeId = "node-interrupt-cold" as never;
205 const recent = row(5);
206 const requestItem = {
207 id: "item-interrupt-request" as never,
208 type: "run_interrupt_request" as const,
209 threadId: v2ThreadId,
210 runId,
211 nodeId,
212 providerThreadId: null,
213 providerTurnId: null,
214 nativeItemRef: null,
215 parentItemId: null,
216 ordinal: 1,
217 status: "completed" as const,
218 title: null,
219 message: "Stopping",
220 startedAt: NOW,
221 completedAt: NOW,
222 updatedAt: NOW,
223 };
224 const resultItem = {
225 id: "item-interrupt-result" as never,
226 type: "run_interrupt_result" as const,
227 threadId: v2ThreadId,
228 runId,
229 nodeId,
230 providerThreadId: null,
231 providerTurnId: null,
232 nativeItemRef: null,
233 parentItemId: null,
234 ordinal: 2,
235 status: "completed" as const,
236 title: null,
237 message: "Stopped",
238 startedAt: NOW,
239 completedAt: NOW,
240 updatedAt: NOW,
241 };
242 const resultRow = {
243 position: 0,
244 visibility: "local" as const,
245 sourceThreadId: v2ThreadId,
246 sourceItemId: resultItem.id,
247 item: resultItem,
248 };
249 const coldOpen = {
250 ...v2Projection,
251 runs: [
252 {
253 id: runId,
254 threadId: v2ThreadId,
255 ordinal: 1,
256 providerInstanceId: "codex" as never,
257 modelSelection: { instanceId: "codex" as never, model: "gpt-5" },
258 providerThreadId: null,
259 userMessageId: "message-1" as never,
260 rootNodeId: nodeId,
261 activeAttemptId: null,
262 status: "running" as const,
263 requestedAt: NOW,
264 startedAt: NOW,
265 completedAt: null,
266 checkpointId: null,
267 contextHandoffId: null,
268 },
269 ],
270 attempts: [
271 {
272 id: "attempt-1" as never,
273 runId,
274 attemptOrdinal: 1,
275 rootNodeId: nodeId,
276 providerInstanceId: "codex" as never,
277 providerThreadId: "provider-thread-1" as never,
278 providerTurnId: null,
279 reason: "initial" as const,
280 status: "running" as const,
281 startedAt: NOW,
282 completedAt: null,
283 },
284 ],
285 turnItems: [requestItem, recent.item],
286 visibleTurnItems: [{ ...recent, position: 0 }],
287 };
288
289 const requestMerged = mergeOlderHistoryIntoProjection(coldOpen, [
290 {
291 position: 0,
292 visibility: "local",
293 sourceThreadId: v2ThreadId,
294 sourceItemId: requestItem.id,
295 item: requestItem,
296 },
297 ]);
298 expect(requestMerged.visibleTurnItems.map((entry) => String(entry.sourceItemId))).toEqual([
299 "item-interrupt-request",
300 "item-5",
301 ]);
302
303 const rolledBackRequestMerged = mergeOlderHistoryIntoProjection(
304 {
305 ...coldOpen,
306 runs: coldOpen.runs.map((run) => ({
307 ...run,
308 status: "rolled_back" as const,
309 completedAt: NOW,
310 })),
311 },
312 [
313 {
314 position: 0,
315 visibility: "local",
316 sourceThreadId: v2ThreadId,
317 sourceItemId: requestItem.id,
318 item: requestItem,
319 },
320 ],
321 );
322 expect(
323 rolledBackRequestMerged.visibleTurnItems.map((entry) => String(entry.sourceItemId)),
324 ).toEqual(["item-5"]);
325
326 const merged = mergeOlderHistoryIntoProjection(coldOpen, [resultRow]);
327 expect(merged.visibleTurnItems.map((entry) => String(entry.sourceItemId))).toEqual([
328 "item-interrupt-result",
329 "item-5",
330 ]);
331 expect(merged.turnItems.some((item) => item.type === "run_interrupt_request")).toBe(true);
332 expect(merged.turnItems.some((item) => item.type === "run_interrupt_result")).toBe(true);
333
334 const attemptSuperseded = applyOrchestrationV2ProjectionEvent(
335 merged,
336 {
337 id: "event-attempt-superseded" as never,
338 type: "run-attempt.updated",
339 threadId: v2ThreadId,
340 runId,
341 occurredAt: NOW,
342 payload: {
343 id: "attempt-1" as never,
344 runId,
345 attemptOrdinal: 1,
346 rootNodeId: nodeId,
347 providerInstanceId: "codex" as never,
348 providerThreadId: "provider-thread-1" as never,
349 providerTurnId: null,
350 reason: "initial",
351 status: "superseded",
352 startedAt: NOW,
353 completedAt: NOW,
354 },
355 } as never,
356 { partialTimeline: true },
357 );
358
359 expect(attemptSuperseded?.visibleTurnItems.map((entry) => String(entry.sourceItemId))).toEqual([
360 "item-interrupt-result",
361 "item-5",
362 ]);
363
364 const withoutRequest = {
365 ...merged,
366 turnItems: merged.turnItems.filter((item) => item.type !== "run_interrupt_request"),
367 };
368 const hidden = applyOrchestrationV2ProjectionEvent(
369 withoutRequest,
370 {
371 id: "event-attempt-superseded-missing" as never,
372 type: "run-attempt.updated",
373 threadId: v2ThreadId,
374 runId,
375 occurredAt: NOW,
376 payload: {
377 id: "attempt-1" as never,
378 runId,
379 attemptOrdinal: 1,
380 rootNodeId: nodeId,
381 providerInstanceId: "codex" as never,
382 providerThreadId: "provider-thread-1" as never,
383 providerTurnId: null,
384 reason: "initial",
385 status: "superseded",
386 startedAt: NOW,
387 completedAt: NOW,
388 },
389 } as never,
390 { partialTimeline: true },
391 );
392 expect(hidden?.visibleTurnItems.map((entry) => String(entry.sourceItemId))).toEqual(["item-5"]);
393 });
394});