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

MessagesTimeline.logic.test.ts

1,410 lines40.7 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 computeStableMessagesTimelineRows,
4 computeMessageDurationStart,
5 deriveMessagesTimelineRows,
6 normalizeCompactToolLabel,
7 resolveAssistantMessageCopyState,
8} from "./MessagesTimeline.logic";
9​
10describe("computeMessageDurationStart", () => {
11 it("returns message createdAt when there is no preceding user message", () => {
12 const result = computeMessageDurationStart([
13 {
14 id: "a1",
15 role: "assistant",
16 createdAt: "2026-01-01T00:00:05Z",
17 updatedAt: "2026-01-01T00:00:10Z",
18 streaming: false,
19 },
20 ]);
21 expect(result).toEqual(new Map([["a1", "2026-01-01T00:00:05Z"]]));
22 });
23​
24 it("uses the user message createdAt for the first assistant response", () => {
25 const result = computeMessageDurationStart([
26 {
27 id: "u1",
28 role: "user",
29 createdAt: "2026-01-01T00:00:00Z",
30 updatedAt: "2026-01-01T00:00:00Z",
31 streaming: false,
32 },
33 {
34 id: "a1",
35 role: "assistant",
36 createdAt: "2026-01-01T00:00:30Z",
37 updatedAt: "2026-01-01T00:00:30Z",
38 streaming: false,
39 },
40 ]);
41​
42 expect(result).toEqual(
43 new Map([
44 ["u1", "2026-01-01T00:00:00Z"],
45 ["a1", "2026-01-01T00:00:00Z"],
46 ]),
47 );
48 });
49​
50 it("uses the previous completed assistant updatedAt for subsequent assistant responses", () => {
51 const result = computeMessageDurationStart([
52 {
53 id: "u1",
54 role: "user",
55 createdAt: "2026-01-01T00:00:00Z",
56 updatedAt: "2026-01-01T00:00:00Z",
57 streaming: false,
58 },
59 {
60 id: "a1",
61 role: "assistant",
62 createdAt: "2026-01-01T00:00:30Z",
63 updatedAt: "2026-01-01T00:00:30Z",
64 streaming: false,
65 },
66 {
67 id: "a2",
68 role: "assistant",
69 createdAt: "2026-01-01T00:00:55Z",
70 updatedAt: "2026-01-01T00:00:55Z",
71 streaming: false,
72 },
73 ]);
74​
75 expect(result).toEqual(
76 new Map([
77 ["u1", "2026-01-01T00:00:00Z"],
78 ["a1", "2026-01-01T00:00:00Z"],
79 ["a2", "2026-01-01T00:00:30Z"],
80 ]),
81 );
82 });
83​
84 it("does not advance the boundary for a streaming message", () => {
85 const result = computeMessageDurationStart([
86 {
87 id: "u1",
88 role: "user",
89 createdAt: "2026-01-01T00:00:00Z",
90 updatedAt: "2026-01-01T00:00:00Z",
91 streaming: false,
92 },
93 {
94 id: "a1",
95 role: "assistant",
96 createdAt: "2026-01-01T00:00:30Z",
97 updatedAt: "2026-01-01T00:00:40Z",
98 streaming: true,
99 },
100 {
101 id: "a2",
102 role: "assistant",
103 createdAt: "2026-01-01T00:00:55Z",
104 updatedAt: "2026-01-01T00:00:55Z",
105 streaming: false,
106 },
107 ]);
108​
109 expect(result).toEqual(
110 new Map([
111 ["u1", "2026-01-01T00:00:00Z"],
112 ["a1", "2026-01-01T00:00:00Z"],
113 ["a2", "2026-01-01T00:00:00Z"],
114 ]),
115 );
116 });
117​
118 it("resets the boundary on a new user message", () => {
119 const result = computeMessageDurationStart([
120 {
121 id: "u1",
122 role: "user",
123 createdAt: "2026-01-01T00:00:00Z",
124 updatedAt: "2026-01-01T00:00:00Z",
125 streaming: false,
126 },
127 {
128 id: "a1",
129 role: "assistant",
130 createdAt: "2026-01-01T00:00:30Z",
131 updatedAt: "2026-01-01T00:00:30Z",
132 streaming: false,
133 },
134 {
135 id: "u2",
136 role: "user",
137 createdAt: "2026-01-01T00:01:00Z",
138 updatedAt: "2026-01-01T00:01:00Z",
139 streaming: false,
140 },
141 {
142 id: "a2",
143 role: "assistant",
144 createdAt: "2026-01-01T00:01:20Z",
145 updatedAt: "2026-01-01T00:01:20Z",
146 streaming: false,
147 },
148 ]);
149​
150 expect(result).toEqual(
151 new Map([
152 ["u1", "2026-01-01T00:00:00Z"],
153 ["a1", "2026-01-01T00:00:00Z"],
154 ["u2", "2026-01-01T00:01:00Z"],
155 ["a2", "2026-01-01T00:01:00Z"],
156 ]),
157 );
158 });
159​
160 it("handles system messages without affecting the boundary", () => {
161 const result = computeMessageDurationStart([
162 {
163 id: "u1",
164 role: "user",
165 createdAt: "2026-01-01T00:00:00Z",
166 updatedAt: "2026-01-01T00:00:00Z",
167 streaming: false,
168 },
169 {
170 id: "s1",
171 role: "system",
172 createdAt: "2026-01-01T00:00:01Z",
173 updatedAt: "2026-01-01T00:00:01Z",
174 streaming: false,
175 },
176 {
177 id: "a1",
178 role: "assistant",
179 createdAt: "2026-01-01T00:00:30Z",
180 updatedAt: "2026-01-01T00:00:30Z",
181 streaming: false,
182 },
183 ]);
184​
185 expect(result).toEqual(
186 new Map([
187 ["u1", "2026-01-01T00:00:00Z"],
188 ["s1", "2026-01-01T00:00:00Z"],
189 ["a1", "2026-01-01T00:00:00Z"],
190 ]),
191 );
192 });
193​
194 it("returns empty map for empty input", () => {
195 expect(computeMessageDurationStart([])).toEqual(new Map());
196 });
197});
198​
199describe("normalizeCompactToolLabel", () => {
200 it("removes trailing completion wording from command labels", () => {
201 expect(normalizeCompactToolLabel("Ran command complete")).toBe("Ran command");
202 });
203​
204 it("removes trailing completion wording from other labels", () => {
205 expect(normalizeCompactToolLabel("Read file completed")).toBe("Read file");
206 });
207});
208​
209describe("resolveAssistantMessageCopyState", () => {
210 it("returns enabled copy state for completed assistant messages", () => {
211 expect(
212 resolveAssistantMessageCopyState({
213 showCopyButton: true,
214 text: "Ship it",
215 streaming: false,
216 }),
217 ).toEqual({
218 text: "Ship it",
219 visible: true,
220 });
221 });
222​
223 it("hides copy while an assistant message is still streaming", () => {
224 expect(
225 resolveAssistantMessageCopyState({
226 showCopyButton: true,
227 text: "Still streaming",
228 streaming: true,
229 }),
230 ).toEqual({
231 text: "Still streaming",
232 visible: false,
233 });
234 });
235​
236 it("hides copy for empty completed assistant messages", () => {
237 expect(
238 resolveAssistantMessageCopyState({
239 showCopyButton: true,
240 text: " ",
241 streaming: false,
242 }),
243 ).toEqual({
244 text: null,
245 visible: false,
246 });
247 });
248​
249 it("hides copy for non-terminal assistant messages", () => {
250 expect(
251 resolveAssistantMessageCopyState({
252 showCopyButton: false,
253 text: "Interim thought",
254 streaming: false,
255 }),
256 ).toEqual({
257 text: "Interim thought",
258 visible: false,
259 });
260 });
261});
262​
263describe("deriveMessagesTimelineRows", () => {
264 it("only enables assistant copy for the terminal assistant message in a turn", () => {
265 const rows = deriveMessagesTimelineRows({
266 timelineEntries: [
267 {
268 id: "user-1-entry",
269 kind: "message",
270 createdAt: "2026-01-01T00:00:00Z",
271 message: {
272 id: "user-1" as never,
273 role: "user",
274 text: "Write a poem",
275 runId: null,
276 createdAt: "2026-01-01T00:00:00Z",
277 updatedAt: "2026-01-01T00:00:00Z",
278 streaming: false,
279 },
280 },
281 {
282 id: "assistant-thought-entry",
283 kind: "message",
284 createdAt: "2026-01-01T00:00:10Z",
285 message: {
286 id: "assistant-thought" as never,
287 role: "assistant",
288 text: "I should ground this first.",
289 runId: "turn-1" as never,
290 createdAt: "2026-01-01T00:00:10Z",
291 updatedAt: "2026-01-01T00:00:11Z",
292 streaming: false,
293 },
294 },
295 {
296 id: "assistant-final-entry",
297 kind: "message",
298 createdAt: "2026-01-01T00:00:20Z",
299 message: {
300 id: "assistant-final" as never,
301 role: "assistant",
302 text: "Here is the poem.",
303 runId: "turn-1" as never,
304 createdAt: "2026-01-01T00:00:20Z",
305 updatedAt: "2026-01-01T00:00:30Z",
306 streaming: false,
307 },
308 },
309 ],
310 expandedRunIds: new Set(["turn-1" as never]),
311 isWorking: false,
312 activeTurnStartedAt: null,
313 turnDiffSummaryByAssistantMessageId: new Map(),
314 revertTurnCountByUserMessageId: new Map(),
315 });
316​
317 const assistantRows = rows.filter(
318 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
319 row.kind === "message" && row.message.role === "assistant",
320 );
321​
322 expect(assistantRows).toHaveLength(2);
323 expect(assistantRows[0]?.showAssistantCopyButton).toBe(false);
324 expect(assistantRows[1]?.showAssistantCopyButton).toBe(true);
325 });
326​
327 it("marks only the active assistant turn as streaming for copy controls", () => {
328 const rows = deriveMessagesTimelineRows({
329 timelineEntries: [
330 {
331 id: "assistant-one-entry",
332 kind: "message",
333 createdAt: "2026-01-01T00:00:10Z",
334 message: {
335 id: "assistant-one" as never,
336 role: "assistant",
337 text: "Earlier response.",
338 runId: "turn-1" as never,
339 createdAt: "2026-01-01T00:00:10Z",
340 updatedAt: "2026-01-01T00:00:11Z",
341 streaming: false,
342 },
343 },
344 {
345 id: "assistant-two-entry",
346 kind: "message",
347 createdAt: "2026-01-01T00:00:20Z",
348 message: {
349 id: "assistant-two" as never,
350 role: "assistant",
351 text: "Active response.",
352 runId: "turn-2" as never,
353 createdAt: "2026-01-01T00:00:20Z",
354 updatedAt: "2026-01-01T00:00:30Z",
355 streaming: false,
356 },
357 },
358 ],
359 latestRun: {
360 runId: "turn-2" as never,
361 status: "running",
362 startedAt: "2026-01-01T00:00:19Z",
363 completedAt: null,
364 },
365 isWorking: false,
366 activeTurnStartedAt: null,
367 turnDiffSummaryByAssistantMessageId: new Map(),
368 revertTurnCountByUserMessageId: new Map(),
369 });
370​
371 const assistantRows = rows.filter(
372 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
373 row.kind === "message" && row.message.role === "assistant",
374 );
375​
376 expect(assistantRows[0]?.assistantCopyStreaming).toBe(false);
377 expect(assistantRows[1]?.assistantCopyStreaming).toBe(true);
378 });
379​
380 it("projects assistant diff summaries and user revert counts onto the affected rows", () => {
381 const assistantTurnDiffSummary = {
382 runId: "turn-1" as never,
383 completedAt: "2026-01-01T00:00:30Z",
384 assistantMessageId: "assistant-1" as never,
385 checkpointTurnCount: 2,
386 checkpointRef: "checkpoint-1" as never,
387 status: "ready" as const,
388 files: [{ path: "src/index.ts", kind: "modified", additions: 3, deletions: 1 }],
389 };
390​
391 const rows = deriveMessagesTimelineRows({
392 timelineEntries: [
393 {
394 id: "user-entry",
395 kind: "message",
396 createdAt: "2026-01-01T00:00:00Z",
397 message: {
398 id: "user-1" as never,
399 role: "user",
400 text: "Do the thing",
401 runId: null,
402 createdAt: "2026-01-01T00:00:00Z",
403 updatedAt: "2026-01-01T00:00:00Z",
404 streaming: false,
405 },
406 },
407 {
408 id: "assistant-entry",
409 kind: "message",
410 createdAt: "2026-01-01T00:00:20Z",
411 message: {
412 id: "assistant-1" as never,
413 role: "assistant",
414 text: "Done",
415 runId: "turn-1" as never,
416 createdAt: "2026-01-01T00:00:20Z",
417 updatedAt: "2026-01-01T00:00:30Z",
418 streaming: false,
419 },
420 },
421 ],
422 isWorking: false,
423 activeTurnStartedAt: null,
424 turnDiffSummaryByAssistantMessageId: new Map([
425 ["assistant-1" as never, assistantTurnDiffSummary],
426 ]),
427 revertTurnCountByUserMessageId: new Map([["user-1" as never, 1]]),
428 });
429​
430 const userRow = rows.find(
431 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
432 row.kind === "message" && row.message.role === "user",
433 );
434 const assistantRow = rows.find(
435 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
436 row.kind === "message" && row.message.role === "assistant",
437 );
438​
439 expect(userRow?.revertTurnCount).toBe(1);
440 expect(assistantRow?.assistantTurnDiffSummary).toBe(assistantTurnDiffSummary);
441 });
442​
443 it("folds settled-turn commentary and work behind a Worked-for row", () => {
444 const timelineEntries = [
445 {
446 id: "user-entry",
447 kind: "message" as const,
448 createdAt: "2026-01-01T00:00:00Z",
449 message: {
450 id: "user-1" as never,
451 role: "user" as const,
452 text: "Build it",
453 runId: null,
454 createdAt: "2026-01-01T00:00:00Z",
455 updatedAt: "2026-01-01T00:00:00Z",
456 streaming: false,
457 },
458 },
459 {
460 id: "assistant-thought-entry",
461 kind: "message" as const,
462 createdAt: "2026-01-01T00:00:05Z",
463 message: {
464 id: "assistant-thought" as never,
465 role: "assistant" as const,
466 text: "Looking around first.",
467 runId: "turn-1" as never,
468 createdAt: "2026-01-01T00:00:05Z",
469 updatedAt: "2026-01-01T00:00:06Z",
470 streaming: false,
471 },
472 },
473 {
474 id: "work-entry-1",
475 kind: "work" as const,
476 createdAt: "2026-01-01T00:00:08Z",
477 entry: {
478 id: "work-1",
479 createdAt: "2026-01-01T00:00:08Z",
480 runId: "turn-1" as never,
481 label: "Ran command",
482 tone: "tool" as const,
483 },
484 },
485 {
486 id: "thread-created-entry",
487 kind: "event" as const,
488 createdAt: "2026-01-01T00:00:10Z",
489 projectedItem: {
490 item: {
491 type: "thread_created",
492 },
493 } as never,
494 },
495 {
496 id: "assistant-final-entry",
497 kind: "message" as const,
498 createdAt: "2026-01-01T00:00:20Z",
499 message: {
500 id: "assistant-final" as never,
501 role: "assistant" as const,
502 text: "Done",
503 runId: "turn-1" as never,
504 createdAt: "2026-01-01T00:00:20Z",
505 updatedAt: "2026-01-01T00:00:22Z",
506 streaming: false,
507 },
508 },
509 ];
510​
511 const collapsedRows = deriveMessagesTimelineRows({
512 timelineEntries,
513 isWorking: false,
514 activeTurnStartedAt: null,
515 turnDiffSummaryByAssistantMessageId: new Map(),
516 revertTurnCountByUserMessageId: new Map(),
517 });
518​
519 const foldRow = collapsedRows.find(
520 (row): row is Extract<(typeof collapsedRows)[number], { kind: "turn-fold" }> =>
521 row.kind === "turn-fold",
522 );
523 expect(foldRow?.runId).toBe("turn-1");
524 expect(foldRow?.expanded).toBe(false);
525 // User message boundary (00:00:00) → terminal message updatedAt (00:00:22).
526 expect(foldRow?.label).toBe("Worked for 22s");
527 expect(collapsedRows.map((row) => row.id)).toEqual([
528 "user-entry",
529 "turn-fold:turn-1",
530 "thread-created-entry",
531 "assistant-final-entry",
532 ]);
533​
534 const expandedRows = deriveMessagesTimelineRows({
535 timelineEntries,
536 expandedRunIds: new Set(["turn-1" as never]),
537 isWorking: false,
538 activeTurnStartedAt: null,
539 turnDiffSummaryByAssistantMessageId: new Map(),
540 revertTurnCountByUserMessageId: new Map(),
541 });
542​
543 expect(expandedRows.map((row) => row.id)).toEqual([
544 "user-entry",
545 "turn-fold:turn-1",
546 "assistant-thought-entry",
547 "work-entry-1",
548 "thread-created-entry",
549 "assistant-final-entry",
550 ]);
551 expect(
552 expandedRows.find((row) => row.kind === "turn-fold" && row.expanded === true),
553 ).toBeDefined();
554 });
555​
556 it("keeps persistent cards after the Worked-for row when they arrive before commentary", () => {
557 const timelineEntries = [
558 {
559 id: "user-entry",
560 kind: "message" as const,
561 createdAt: "2026-01-01T00:00:00Z",
562 message: {
563 id: "user-1" as never,
564 role: "user" as const,
565 text: "Spawn a subagent",
566 runId: null,
567 createdAt: "2026-01-01T00:00:00Z",
568 updatedAt: "2026-01-01T00:00:00Z",
569 streaming: false,
570 },
571 },
572 {
573 id: "subagent-card-entry",
574 kind: "event" as const,
575 createdAt: "2026-01-01T00:00:03Z",
576 projectedItem: {
577 item: {
578 type: "subagent",
579 runId: "turn-1",
580 },
581 } as never,
582 },
583 {
584 id: "assistant-commentary-entry",
585 kind: "message" as const,
586 createdAt: "2026-01-01T00:00:05Z",
587 message: {
588 id: "assistant-commentary" as never,
589 role: "assistant" as const,
590 text: "I spawned the subagent.",
591 runId: "turn-1" as never,
592 createdAt: "2026-01-01T00:00:05Z",
593 updatedAt: "2026-01-01T00:00:06Z",
594 streaming: false,
595 },
596 },
597 {
598 id: "assistant-final-entry",
599 kind: "message" as const,
600 createdAt: "2026-01-01T00:00:20Z",
601 message: {
602 id: "assistant-final" as never,
603 role: "assistant" as const,
604 text: "Subagent says: Hello.",
605 runId: "turn-1" as never,
606 createdAt: "2026-01-01T00:00:20Z",
607 updatedAt: "2026-01-01T00:00:22Z",
608 streaming: false,
609 },
610 },
611 ];
612​
613 const collapsedRows = deriveMessagesTimelineRows({
614 timelineEntries,
615 isWorking: false,
616 activeTurnStartedAt: null,
617 turnDiffSummaryByAssistantMessageId: new Map(),
618 revertTurnCountByUserMessageId: new Map(),
619 });
620​
621 expect(collapsedRows.map((row) => row.id)).toEqual([
622 "user-entry",
623 "turn-fold:turn-1",
624 "subagent-card-entry",
625 "assistant-final-entry",
626 ]);
627 });
628​
629 it("collapses only output from a superseded V2 attempt within the active logical run", () => {
630 const runId = "run-steered" as never;
631 const supersededAttemptId = "attempt-1" as never;
632 const activeAttemptId = "attempt-2" as never;
633 const supersededAttempt = {
634 id: supersededAttemptId,
635 runId,
636 attemptOrdinal: 1,
637 rootNodeId: "node-attempt-1" as never,
638 status: "superseded" as const,
639 };
640 const activeAttempt = {
641 id: activeAttemptId,
642 runId,
643 attemptOrdinal: 2,
644 rootNodeId: "node-attempt-2" as never,
645 status: "running" as const,
646 };
647 const timelineEntries = [
648 {
649 id: "initial-user-entry",
650 kind: "message" as const,
651 createdAt: "2026-01-01T00:00:00Z",
652 attempt: supersededAttempt,
653 message: {
654 id: "initial-user" as never,
655 role: "user" as const,
656 text: "Build it",
657 runId,
658 inputIntent: "turn_start" as const,
659 createdAt: "2026-01-01T00:00:00Z",
660 updatedAt: "2026-01-01T00:00:00Z",
661 streaming: false,
662 },
663 },
664 {
665 id: "superseded-assistant-entry",
666 kind: "message" as const,
667 createdAt: "2026-01-01T00:00:02Z",
668 attempt: supersededAttempt,
669 message: {
670 id: "superseded-assistant" as never,
671 role: "assistant" as const,
672 text: "Partial old response",
673 runId,
674 createdAt: "2026-01-01T00:00:02Z",
675 updatedAt: "2026-01-01T00:00:03Z",
676 streaming: false,
677 },
678 },
679 {
680 id: "superseded-work-entry",
681 kind: "work" as const,
682 createdAt: "2026-01-01T00:00:04Z",
683 attempt: supersededAttempt,
684 entry: {
685 id: "superseded-work",
686 createdAt: "2026-01-01T00:00:04Z",
687 runId,
688 label: "Old command",
689 tone: "tool" as const,
690 },
691 },
692 {
693 id: "superseded-thread-created-entry",
694 kind: "event" as const,
695 createdAt: "2026-01-01T00:00:04.500Z",
696 attempt: supersededAttempt,
697 projectedItem: {
698 item: {
699 type: "thread_created",
700 },
701 } as never,
702 },
703 {
704 id: "steer-user-entry",
705 kind: "message" as const,
706 createdAt: "2026-01-01T00:00:05Z",
707 attempt: activeAttempt,
708 message: {
709 id: "steer-user" as never,
710 role: "user" as const,
711 text: "Change direction",
712 runId,
713 inputIntent: "steer" as const,
714 createdAt: "2026-01-01T00:00:05Z",
715 updatedAt: "2026-01-01T00:00:05Z",
716 streaming: false,
717 },
718 },
719 {
720 id: "active-assistant-entry",
721 kind: "message" as const,
722 createdAt: "2026-01-01T00:00:06Z",
723 attempt: activeAttempt,
724 message: {
725 id: "active-assistant" as never,
726 role: "assistant" as const,
727 text: "Current response",
728 runId,
729 createdAt: "2026-01-01T00:00:06Z",
730 updatedAt: "2026-01-01T00:00:07Z",
731 streaming: true,
732 },
733 },
734 ];
735 const common = {
736 timelineEntries,
737 latestRun: {
738 runId,
739 status: "running" as const,
740 startedAt: "2026-01-01T00:00:00Z",
741 completedAt: null,
742 },
743 isWorking: false,
744 activeTurnStartedAt: null,
745 turnDiffSummaryByAssistantMessageId: new Map(),
746 revertTurnCountByUserMessageId: new Map(),
747 };
748​
749 const collapsedRows = deriveMessagesTimelineRows(common);
750 expect(collapsedRows.map((row) => row.id)).toEqual([
751 "initial-user-entry",
752 `attempt-fold:${supersededAttemptId}`,
753 "superseded-thread-created-entry",
754 "steer-user-entry",
755 "active-assistant-entry",
756 ]);
757 expect(collapsedRows.find((row) => row.kind === "attempt-fold")).toMatchObject({
758 attemptId: supersededAttemptId,
759 runId,
760 label: "Superseded attempt",
761 expanded: false,
762 });
763​
764 const expandedRows = deriveMessagesTimelineRows({
765 ...common,
766 expandedAttemptIds: new Set([supersededAttemptId]),
767 });
768 expect(expandedRows.map((row) => row.id)).toEqual([
769 "initial-user-entry",
770 `attempt-fold:${supersededAttemptId}`,
771 "superseded-assistant-entry",
772 "superseded-work-entry",
773 "superseded-thread-created-entry",
774 "steer-user-entry",
775 "active-assistant-entry",
776 ]);
777 });
778​
779 it("derives a sane duration for a steer-superseded turn with one instant commentary message", () => {
780 // A steer ends the previous turn early: its only message completes the
781 // instant it is created, and trailing work entries land after it. The
782 // fold duration must span from the user message that started the turn to
783 // the last entry, not message createdAt → message updatedAt (~0ms).
784 const rows = deriveMessagesTimelineRows({
785 timelineEntries: [
786 {
787 id: "user-entry",
788 kind: "message",
789 createdAt: "2026-01-01T00:00:00Z",
790 message: {
791 id: "user-1" as never,
792 role: "user" as const,
793 text: "do it once more",
794 runId: null,
795 createdAt: "2026-01-01T00:00:00Z",
796 updatedAt: "2026-01-01T00:00:00Z",
797 streaming: false,
798 },
799 },
800 {
801 id: "assistant-commentary-entry",
802 kind: "message",
803 createdAt: "2026-01-01T00:00:09Z",
804 message: {
805 id: "assistant-commentary" as never,
806 role: "assistant" as const,
807 text: "Kicking off call 1.",
808 runId: "turn-1" as never,
809 createdAt: "2026-01-01T00:00:09Z",
810 updatedAt: "2026-01-01T00:00:09Z",
811 streaming: false,
812 },
813 },
814 {
815 id: "work-entry-1",
816 kind: "work",
817 createdAt: "2026-01-01T00:00:12Z",
818 entry: {
819 id: "work-1",
820 createdAt: "2026-01-01T00:00:12Z",
821 runId: "turn-1" as never,
822 label: "Ran command",
823 tone: "tool" as const,
824 },
825 },
826 {
827 id: "steer-user-entry",
828 kind: "message",
829 createdAt: "2026-01-01T00:00:14Z",
830 message: {
831 id: "user-2" as never,
832 role: "user" as const,
833 text: "actually do 15",
834 runId: null,
835 createdAt: "2026-01-01T00:00:14Z",
836 updatedAt: "2026-01-01T00:00:14Z",
837 streaming: false,
838 },
839 },
840 {
841 id: "assistant-next-turn-entry",
842 kind: "message",
843 createdAt: "2026-01-01T00:00:17Z",
844 message: {
845 id: "assistant-next" as never,
846 role: "assistant" as const,
847 text: "One down — adjusting.",
848 runId: "turn-2" as never,
849 createdAt: "2026-01-01T00:00:17Z",
850 updatedAt: "2026-01-01T00:00:17Z",
851 streaming: true,
852 },
853 },
854 ],
855 latestRun: {
856 runId: "turn-2" as never,
857 status: "running",
858 startedAt: "2026-01-01T00:00:14Z",
859 completedAt: null,
860 },
861 isWorking: true,
862 activeTurnStartedAt: "2026-01-01T00:00:14Z",
863 turnDiffSummaryByAssistantMessageId: new Map(),
864 revertTurnCountByUserMessageId: new Map(),
865 });
866​
867 const foldRow = rows.find(
868 (row): row is Extract<(typeof rows)[number], { kind: "turn-fold" }> =>
869 row.kind === "turn-fold",
870 );
871 // User message (00:00:00) → trailing work entry (00:00:12).
872 expect(foldRow?.runId).toBe("turn-1");
873 expect(foldRow?.label).toBe("Worked for 12s");
874 });
875​
876 it("uses latest-turn timings and the stopped label for an interrupted latest turn", () => {
877 const rows = deriveMessagesTimelineRows({
878 timelineEntries: [
879 {
880 id: "work-entry-1",
881 kind: "work",
882 createdAt: "2026-01-01T00:00:05Z",
883 entry: {
884 id: "work-1",
885 createdAt: "2026-01-01T00:00:05Z",
886 runId: "turn-1" as never,
887 label: "Ran command",
888 tone: "tool" as const,
889 },
890 },
891 ],
892 latestRun: {
893 runId: "turn-1" as never,
894 status: "interrupted",
895 startedAt: "2026-01-01T00:00:00Z",
896 completedAt: "2026-01-01T00:00:47Z",
897 },
898 isWorking: false,
899 activeTurnStartedAt: null,
900 turnDiffSummaryByAssistantMessageId: new Map(),
901 revertTurnCountByUserMessageId: new Map(),
902 });
903​
904 expect(rows).toEqual([
905 expect.objectContaining({
906 kind: "turn-fold",
907 runId: "turn-1",
908 label: "You stopped after 47s",
909 expanded: false,
910 }),
911 ]);
912 });
913​
914 it("keeps interruption request, intervening work, and result visible in order", () => {
915 const runId = "turn-1" as never;
916 const interruptEvent = (type: "run_interrupt_request" | "run_interrupt_result") => ({
917 position: type === "run_interrupt_request" ? 0 : 2,
918 visibility: "local" as const,
919 sourceThreadId: "thread-1" as never,
920 sourceItemId: `item-${type}` as never,
921 item: {
922 id: `item-${type}`,
923 threadId: "thread-1",
924 runId,
925 nodeId: null,
926 providerThreadId: null,
927 providerTurnId: null,
928 nativeItemRef: null,
929 parentItemId: null,
930 ordinal: type === "run_interrupt_request" ? 0 : 2,
931 status: "completed",
932 title: null,
933 startedAt: null,
934 completedAt: null,
935 updatedAt: {},
936 type,
937 message: type === "run_interrupt_request" ? "Stopping" : "Stopped",
938 },
939 });
940 const rows = deriveMessagesTimelineRows({
941 timelineEntries: [
942 {
943 id: "interrupt-request",
944 kind: "event",
945 createdAt: "2026-01-01T00:00:01Z",
946 projectedItem: interruptEvent("run_interrupt_request") as never,
947 },
948 {
949 id: "work-entry",
950 kind: "work",
951 createdAt: "2026-01-01T00:00:02Z",
952 entry: {
953 id: "work-1",
954 createdAt: "2026-01-01T00:00:02Z",
955 runId,
956 label: "Finishing tool output",
957 tone: "tool",
958 },
959 },
960 {
961 id: "interrupt-result",
962 kind: "event",
963 createdAt: "2026-01-01T00:00:03Z",
964 projectedItem: interruptEvent("run_interrupt_result") as never,
965 },
966 ],
967 latestRun: {
968 runId,
969 status: "interrupted",
970 startedAt: "2026-01-01T00:00:00Z",
971 completedAt: "2026-01-01T00:00:03Z",
972 },
973 isWorking: false,
974 activeTurnStartedAt: null,
975 turnDiffSummaryByAssistantMessageId: new Map(),
976 revertTurnCountByUserMessageId: new Map(),
977 });
978​
979 expect(rows.map((row) => row.id)).toEqual([
980 "interrupt-request",
981 "work-entry",
982 "interrupt-result",
983 ]);
984 expect(rows.some((row) => row.kind === "turn-fold")).toBe(false);
985 });
986​
987 it("keeps the previous turn folded while a newly sent message awaits its turn", () => {
988 // Right after send, isWorking is true but latestRun still points at the
989 // previous, settled turn — it must stay folded through that window.
990 const rows = deriveMessagesTimelineRows({
991 timelineEntries: [
992 {
993 id: "work-entry-1",
994 kind: "work",
995 createdAt: "2026-01-01T00:00:05Z",
996 entry: {
997 id: "work-1",
998 createdAt: "2026-01-01T00:00:05Z",
999 runId: "turn-1" as never,
1000 label: "Ran command",
1001 tone: "tool" as const,
1002 },
1003 },
1005 id: "assistant-final-entry",
1006 kind: "message",
1007 createdAt: "2026-01-01T00:00:20Z",
1008 message: {
1009 id: "assistant-final" as never,
1010 role: "assistant",
1011 text: "Done",
1012 runId: "turn-1" as never,
1013 createdAt: "2026-01-01T00:00:20Z",
1014 updatedAt: "2026-01-01T00:00:22Z",
1015 streaming: false,
1016 },
1017 },
1019 id: "user-followup-entry",
1020 kind: "message",
1021 createdAt: "2026-01-01T00:01:00Z",
1022 message: {
1023 id: "user-followup" as never,
1024 role: "user",
1025 text: "yooo",
1026 runId: null,
1027 createdAt: "2026-01-01T00:01:00Z",
1028 updatedAt: "2026-01-01T00:01:00Z",
1029 streaming: false,
1030 },
1031 },
1032 ],
1033 latestRun: {
1034 runId: "turn-1" as never,
1035 status: "completed",
1036 startedAt: "2026-01-01T00:00:00Z",
1037 completedAt: "2026-01-01T00:00:22Z",
1038 },
1039 isWorking: true,
1040 activeTurnStartedAt: "2026-01-01T00:01:00Z",
1041 turnDiffSummaryByAssistantMessageId: new Map(),
1042 revertTurnCountByUserMessageId: new Map(),
1043 });
1044​
1045 expect(rows.map((row) => row.id)).toEqual([
1046 "turn-fold:turn-1",
1047 "assistant-final-entry",
1048 "user-followup-entry",
1049 "working-indicator-row",
1050 ]);
1051 const finalRow = rows.find((row) => row.id === "assistant-final-entry");
1052 expect(finalRow?.kind === "message" && finalRow.showAssistantMeta).toBe(true);
1053 });
1054​
1055 it("does not fold the active in-progress turn", () => {
1056 const rows = deriveMessagesTimelineRows({
1057 timelineEntries: [
1059 id: "assistant-thought-entry",
1060 kind: "message",
1061 createdAt: "2026-01-01T00:00:05Z",
1062 message: {
1063 id: "assistant-thought" as never,
1064 role: "assistant",
1065 text: "Working on it.",
1066 runId: "turn-1" as never,
1067 createdAt: "2026-01-01T00:00:05Z",
1068 updatedAt: "2026-01-01T00:00:06Z",
1069 streaming: false,
1070 },
1071 },
1073 id: "work-entry-1",
1074 kind: "work",
1075 createdAt: "2026-01-01T00:00:08Z",
1076 entry: {
1077 id: "work-1",
1078 createdAt: "2026-01-01T00:00:08Z",
1079 runId: "turn-1" as never,
1080 label: "Ran command",
1081 tone: "tool" as const,
1082 },
1083 },
1084 ],
1085 latestRun: {
1086 runId: "turn-1" as never,
1087 status: "running",
1088 startedAt: "2026-01-01T00:00:00Z",
1089 completedAt: null,
1090 },
1091 isWorking: true,
1092 activeTurnStartedAt: "2026-01-01T00:00:00Z",
1093 turnDiffSummaryByAssistantMessageId: new Map(),
1094 revertTurnCountByUserMessageId: new Map(),
1095 });
1096​
1097 expect(rows.some((row) => row.kind === "turn-fold")).toBe(false);
1098 expect(rows.map((row) => row.id)).toEqual([
1099 "assistant-thought-entry",
1100 "work-entry-1",
1101 "working-indicator-row",
1102 ]);
1103 });
1104​
1105 it("only shows assistant metadata on the terminal assistant message", () => {
1106 const rows = deriveMessagesTimelineRows({
1107 timelineEntries: [
1109 id: "assistant-thought-entry",
1110 kind: "message",
1111 createdAt: "2026-01-01T00:00:10Z",
1112 message: {
1113 id: "assistant-thought" as never,
1114 role: "assistant",
1115 text: "Checking first.",
1116 runId: "turn-1" as never,
1117 createdAt: "2026-01-01T00:00:10Z",
1118 updatedAt: "2026-01-01T00:00:11Z",
1119 streaming: false,
1120 },
1121 },
1123 id: "assistant-final-entry",
1124 kind: "message",
1125 createdAt: "2026-01-01T00:00:20Z",
1126 message: {
1127 id: "assistant-final" as never,
1128 role: "assistant",
1129 text: "Done.",
1130 runId: "turn-1" as never,
1131 createdAt: "2026-01-01T00:00:20Z",
1132 updatedAt: "2026-01-01T00:00:30Z",
1133 streaming: false,
1134 },
1135 },
1136 ],
1137 expandedRunIds: new Set(["turn-1" as never]),
1138 isWorking: false,
1139 activeTurnStartedAt: null,
1140 turnDiffSummaryByAssistantMessageId: new Map(),
1141 revertTurnCountByUserMessageId: new Map(),
1142 });
1143​
1144 const assistantRows = rows.filter(
1145 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
1146 row.kind === "message" && row.message.role === "assistant",
1147 );
1148​
1149 expect(assistantRows.map((row) => row.showAssistantMeta)).toEqual([false, true]);
1150 });
1151​
1152 it("withholds assistant metadata while the active turn is still in progress", () => {
1153 const rows = deriveMessagesTimelineRows({
1154 timelineEntries: [
1156 id: "assistant-thought-entry",
1157 kind: "message",
1158 createdAt: "2026-01-01T00:00:10Z",
1159 message: {
1160 id: "assistant-thought" as never,
1161 role: "assistant",
1162 text: "Working on it.",
1163 runId: "turn-1" as never,
1164 createdAt: "2026-01-01T00:00:10Z",
1165 updatedAt: "2026-01-01T00:00:11Z",
1166 streaming: false,
1167 },
1168 },
1169 ],
1170 latestRun: {
1171 runId: "turn-1" as never,
1172 status: "running",
1173 startedAt: "2026-01-01T00:00:00Z",
1174 completedAt: null,
1175 },
1176 isWorking: true,
1177 activeTurnStartedAt: "2026-01-01T00:00:00Z",
1178 turnDiffSummaryByAssistantMessageId: new Map(),
1179 revertTurnCountByUserMessageId: new Map(),
1180 });
1181​
1182 const assistantRow = rows.find(
1183 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
1184 row.kind === "message" && row.message.role === "assistant",
1185 );
1186​
1187 expect(assistantRow?.showAssistantMeta).toBe(false);
1188 expect(assistantRow?.showAssistantCopyButton).toBe(false);
1189 });
1190​
1191 it("groups contiguous work log entries into one timeline row", () => {
1192 const timelineEntries = [
1194 id: "work-entry-1",
1195 kind: "work" as const,
1196 createdAt: "2026-01-01T00:00:01Z",
1197 entry: {
1198 id: "work-1",
1199 createdAt: "2026-01-01T00:00:01Z",
1200 label: "read",
1201 detail: "Reading package.json",
1202 tone: "tool" as const,
1203 },
1204 },
1206 id: "work-entry-2",
1207 kind: "work" as const,
1208 createdAt: "2026-01-01T00:00:02Z",
1209 entry: {
1210 id: "work-2",
1211 createdAt: "2026-01-01T00:00:02Z",
1212 label: "edit",
1213 detail: "Editing MessagesTimeline.tsx",
1214 tone: "tool" as const,
1215 },
1216 },
1218 id: "work-entry-3",
1219 kind: "work" as const,
1220 createdAt: "2026-01-01T00:00:03Z",
1221 entry: {
1222 id: "work-3",
1223 createdAt: "2026-01-01T00:00:03Z",
1224 label: "test",
1225 detail: "Running tests",
1226 tone: "tool" as const,
1227 },
1228 },
1229 ];
1230​
1231 const baseInput = {
1232 timelineEntries,
1233 isWorking: false,
1234 activeTurnStartedAt: null,
1235 turnDiffSummaryByAssistantMessageId: new Map(),
1236 revertTurnCountByUserMessageId: new Map(),
1237 };
1238 const rows = deriveMessagesTimelineRows(baseInput);
1239​
1240 expect(rows).toHaveLength(1);
1241 expect(rows[0]).toMatchObject({
1242 kind: "work",
1243 id: "work-entry-1",
1244 groupedEntries: [
1245 expect.objectContaining({ id: "work-1" }),
1246 expect.objectContaining({ id: "work-2" }),
1247 expect.objectContaining({ id: "work-3" }),
1248 ],
1249 });
1250 });
1251});
1252​
1253describe("computeStableMessagesTimelineRows", () => {
1254 it("returns the previous result when row order and content are unchanged", () => {
1255 const firstUserMessage = {
1256 id: "user-1" as never,
1257 role: "user" as const,
1258 text: "First",
1259 runId: null,
1260 createdAt: "2026-01-01T00:00:00Z",
1261 updatedAt: "2026-01-01T00:00:00Z",
1262 streaming: false,
1263 };
1264 const secondUserMessage = {
1265 id: "user-2" as never,
1266 role: "user" as const,
1267 text: "Second",
1268 runId: null,
1269 createdAt: "2026-01-01T00:00:10Z",
1270 updatedAt: "2026-01-01T00:00:10Z",
1271 streaming: false,
1272 };
1273​
1274 const rows = deriveMessagesTimelineRows({
1275 timelineEntries: [
1277 id: "entry-user-1",
1278 kind: "message",
1279 createdAt: firstUserMessage.createdAt,
1280 message: firstUserMessage,
1281 },
1283 id: "entry-user-2",
1284 kind: "message",
1285 createdAt: secondUserMessage.createdAt,
1286 message: secondUserMessage,
1287 },
1288 ],
1289 isWorking: false,
1290 activeTurnStartedAt: null,
1291 turnDiffSummaryByAssistantMessageId: new Map(),
1292 revertTurnCountByUserMessageId: new Map(),
1293 });
1294​
1295 const initial = computeStableMessagesTimelineRows(rows, {
1296 byId: new Map(),
1297 result: [],
1298 });
1299​
1300 const repeated = computeStableMessagesTimelineRows(rows, initial);
1301​
1302 expect(repeated).toBe(initial);
1303 expect(repeated.result).toBe(initial.result);
1304 });
1305​
1306 it("reuses work rows when equivalent timeline derivations create new grouped arrays", () => {
1307 const firstWorkEntry = {
1308 id: "work-1",
1309 createdAt: "2026-01-01T00:00:00Z",
1310 label: "thinking",
1311 detail: "Inspecting repository state",
1312 tone: "thinking" as const,
1313 };
1314 const secondWorkEntry = {
1315 id: "work-2",
1316 createdAt: "2026-01-01T00:00:01Z",
1317 label: "read",
1318 detail: "Reading package.json",
1319 tone: "tool" as const,
1320 };
1321​
1322 const createRows = () =>
1323 deriveMessagesTimelineRows({
1324 timelineEntries: [
1326 id: "entry-work-1",
1327 kind: "work",
1328 createdAt: firstWorkEntry.createdAt,
1329 entry: firstWorkEntry,
1330 },
1332 id: "entry-work-2",
1333 kind: "work",
1334 createdAt: secondWorkEntry.createdAt,
1335 entry: secondWorkEntry,
1336 },
1337 ],
1338 isWorking: false,
1339 activeTurnStartedAt: null,
1340 turnDiffSummaryByAssistantMessageId: new Map(),
1341 revertTurnCountByUserMessageId: new Map(),
1342 });
1343​
1344 const firstRows = createRows();
1345 const initial = computeStableMessagesTimelineRows(firstRows, {
1346 byId: new Map(),
1347 result: [],
1348 });
1349 const secondRows = createRows();
1350​
1351 expect(secondRows[0]).not.toBe(firstRows[0]);
1352​
1353 const repeated = computeStableMessagesTimelineRows(secondRows, initial);
1354​
1355 expect(repeated).toBe(initial);
1356 expect(repeated.result[0]).toBe(initial.result[0]);
1357 });
1358​
1359 it("returns a new result when row order changes without content changes", () => {
1360 const firstUserMessage = {
1361 id: "user-1" as never,
1362 role: "user" as const,
1363 text: "First",
1364 runId: null,
1365 createdAt: "2026-01-01T00:00:00Z",
1366 updatedAt: "2026-01-01T00:00:00Z",
1367 streaming: false,
1368 };
1369 const secondUserMessage = {
1370 id: "user-2" as never,
1371 role: "user" as const,
1372 text: "Second",
1373 runId: null,
1374 createdAt: "2026-01-01T00:00:10Z",
1375 updatedAt: "2026-01-01T00:00:10Z",
1376 streaming: false,
1377 };
1378​
1379 const firstRows = deriveMessagesTimelineRows({
1380 timelineEntries: [
1382 id: "entry-user-1",
1383 kind: "message",
1384 createdAt: firstUserMessage.createdAt,
1385 message: firstUserMessage,
1386 },
1388 id: "entry-user-2",
1389 kind: "message",
1390 createdAt: secondUserMessage.createdAt,
1391 message: secondUserMessage,
1392 },
1393 ],
1394 isWorking: false,
1395 activeTurnStartedAt: null,
1396 turnDiffSummaryByAssistantMessageId: new Map(),
1397 revertTurnCountByUserMessageId: new Map(),
1398 });
1399​
1400 const initial = computeStableMessagesTimelineRows(firstRows, {
1401 byId: new Map(),
1402 result: [],
1403 });
1404​
1405 const reordered = computeStableMessagesTimelineRows([firstRows[1]!, firstRows[0]!], initial);
1406​
1407 expect(reordered).not.toBe(initial);
1408 expect(reordered.result).toEqual([initial.result[1], initial.result[0]]);
1409 });
1410});