Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import type { ProviderEvent, ProviderSession } from "@t3tools/contracts";
4import {
5 type WorkLogEntry,
6 applyEventToMessages,
7 derivePendingApprovals,
8 deriveTimelineEntries,
9 deriveWorkLogEntries,
10 evolveSession,
11} from "./session-logic";
12import type { ChatMessage } from "./types";
13
14function makeEvent(overrides: Partial<ProviderEvent>): ProviderEvent {
15 return {
16 id: "evt-1",
17 kind: "notification",
18 provider: "codex",
19 sessionId: "sess-1",
20 createdAt: "2026-02-08T10:00:00.000Z",
21 method: "noop",
22 ...overrides,
23 };
24}
25
26function makeSession(overrides: Partial<ProviderSession> = {}): ProviderSession {
27 return {
28 sessionId: "sess-1",
29 provider: "codex",
30 status: "ready",
31 createdAt: "2026-02-08T09:59:00.000Z",
32 updatedAt: "2026-02-08T09:59:00.000Z",
33 ...overrides,
34 };
35}
36
37describe("deriveTimelineEntries", () => {
38 it("interleaves messages and work entries by timestamp", () => {
39 const messages: ChatMessage[] = [
40 {
41 id: "m-user",
42 role: "user",
43 text: "Hi",
44 createdAt: "2026-02-08T10:00:00.000Z",
45 streaming: false,
46 },
47 {
48 id: "m-assistant",
49 role: "assistant",
50 text: "Hello",
51 createdAt: "2026-02-08T10:05:00.000Z",
52 streaming: false,
53 },
54 ];
55 const workEntries: WorkLogEntry[] = [
56 {
57 id: "w-1",
58 label: "Tool call",
59 createdAt: "2026-02-08T10:02:00.000Z",
60 tone: "tool",
61 },
62 {
63 id: "w-2",
64 label: "Preamble",
65 createdAt: "2026-02-08T10:03:00.000Z",
66 tone: "thinking",
67 },
68 ];
69
70 const timeline = deriveTimelineEntries(messages, workEntries);
71
72 expect(timeline.map((entry) => entry.id)).toEqual([
73 "message:m-user",
74 "work:w-1",
75 "work:w-2",
76 "message:m-assistant",
77 ]);
78 });
79
80 it("prefers work entries when timestamps are equal", () => {
81 const messages: ChatMessage[] = [
82 {
83 id: "m-1",
84 role: "assistant",
85 text: "Done",
86 createdAt: "2026-02-08T10:00:00.000Z",
87 streaming: false,
88 },
89 ];
90 const workEntries: WorkLogEntry[] = [
91 {
92 id: "w-1",
93 label: "Tool call",
94 createdAt: "2026-02-08T10:00:00.000Z",
95 tone: "tool",
96 },
97 ];
98
99 const timeline = deriveTimelineEntries(messages, workEntries);
100
101 expect(timeline.map((entry) => entry.id)).toEqual(["work:w-1", "message:m-1"]);
102 });
103});
104
105describe("deriveWorkLogEntries", () => {
106 it("drops preamble/work events from the visible work log", () => {
107 const entries = deriveWorkLogEntries(
108 [
109 makeEvent({
110 id: "evt-1",
111 method: "item/started",
112 turnId: "turn-1",
113 createdAt: "2026-02-08T10:00:00.000Z",
114 payload: { item: { type: "preamble", text: "thinking" } },
115 }),
116 makeEvent({
117 id: "evt-2",
118 method: "item/started",
119 turnId: "turn-1",
120 createdAt: "2026-02-08T10:00:01.000Z",
121 payload: { item: { type: "work", text: "planning" } },
122 }),
123 makeEvent({
124 id: "evt-3",
125 method: "item/started",
126 turnId: "turn-1",
127 createdAt: "2026-02-08T10:00:02.000Z",
128 payload: { item: { type: "tool_call", command: "ls -la" } },
129 }),
130 ],
131 "turn-1",
132 );
133
134 expect(entries).toHaveLength(1);
135 expect(entries[0]?.label).toBe("Tool call");
136 });
137
138 it("does not surface successful turn completion as a work-log row", () => {
139 const entries = deriveWorkLogEntries(
140 [
141 makeEvent({
142 id: "evt-start",
143 method: "turn/started",
144 turnId: "turn-1",
145 createdAt: "2026-02-08T10:00:00.000Z",
146 }),
147 makeEvent({
148 id: "evt-complete",
149 method: "turn/completed",
150 turnId: "turn-1",
151 createdAt: "2026-02-08T10:00:10.000Z",
152 payload: { turn: { id: "turn-1", status: "completed" } },
153 }),
154 ],
155 "turn-1",
156 );
157
158 expect(entries).toHaveLength(0);
159 });
160
161 it("shows failed turn completion in the work log", () => {
162 const entries = deriveWorkLogEntries(
163 [
164 makeEvent({
165 id: "evt-start",
166 method: "turn/started",
167 turnId: "turn-1",
168 createdAt: "2026-02-08T10:00:00.000Z",
169 }),
170 makeEvent({
171 id: "evt-complete",
172 method: "turn/completed",
173 turnId: "turn-1",
174 createdAt: "2026-02-08T10:00:10.000Z",
175 payload: {
176 turn: {
177 id: "turn-1",
178 status: "failed",
179 error: { message: "sandbox denied" },
180 },
181 },
182 }),
183 ],
184 "turn-1",
185 );
186
187 expect(entries).toHaveLength(1);
188 expect(entries[0]?.label).toBe("Turn failed");
189 expect(entries[0]?.detail).toBe("sandbox denied");
190 });
191
192 it("hides reasoning and agent-message noise from the visible work log", () => {
193 const entries = deriveWorkLogEntries(
194 [
195 makeEvent({
196 id: "evt-turn-start",
197 method: "turn/started",
198 turnId: "turn-1",
199 createdAt: "2026-02-08T10:00:00.000Z",
200 }),
201 makeEvent({
202 id: "evt-summary-part",
203 method: "item/reasoning/summaryPartAdded",
204 turnId: "turn-1",
205 createdAt: "2026-02-08T10:00:01.000Z",
206 }),
207 makeEvent({
208 id: "evt-summary-delta",
209 method: "item/reasoning/summaryTextDelta",
210 turnId: "turn-1",
211 createdAt: "2026-02-08T10:00:02.000Z",
212 }),
213 makeEvent({
214 id: "evt-agent-start",
215 method: "item/started",
216 turnId: "turn-1",
217 createdAt: "2026-02-08T10:00:03.000Z",
218 payload: {
219 item: { id: "item-msg", type: "agentMessage", text: "Working..." },
220 },
221 }),
222 makeEvent({
223 id: "evt-agent-complete",
224 method: "item/completed",
225 turnId: "turn-1",
226 createdAt: "2026-02-08T10:00:04.000Z",
227 payload: {
228 item: {
229 id: "item-msg",
230 type: "agentMessage",
231 text: "Done response",
232 },
233 },
234 }),
235 makeEvent({
236 id: "evt-tool-start",
237 method: "item/started",
238 turnId: "turn-1",
239 createdAt: "2026-02-08T10:00:05.000Z",
240 payload: {
241 item: { id: "item-tool", type: "tool_call", command: "ls -la" },
242 },
243 }),
244 makeEvent({
245 id: "evt-tool-complete",
246 method: "item/completed",
247 turnId: "turn-1",
248 createdAt: "2026-02-08T10:00:06.000Z",
249 payload: {
250 item: { id: "item-tool", type: "tool_call", command: "ls -la" },
251 },
252 }),
253 makeEvent({
254 id: "evt-turn-complete",
255 method: "turn/completed",
256 turnId: "turn-1",
257 createdAt: "2026-02-08T10:00:08.000Z",
258 payload: { turn: { id: "turn-1", status: "completed" } },
259 }),
260 ],
261 "turn-1",
262 );
263
264 expect(entries.map((entry) => entry.label)).toEqual(["Tool call"]);
265 });
266
267 it("coalesces command start/completed lifecycle into one entry", () => {
268 const entries = deriveWorkLogEntries(
269 [
270 makeEvent({
271 id: "evt-command-start",
272 method: "item/started",
273 turnId: "turn-1",
274 createdAt: "2026-02-08T10:00:01.000Z",
275 payload: {
276 item: {
277 id: "item-command",
278 type: "command_execution",
279 command: "git status --short",
280 },
281 },
282 }),
283 makeEvent({
284 id: "evt-command-complete",
285 method: "item/completed",
286 turnId: "turn-1",
287 createdAt: "2026-02-08T10:00:02.000Z",
288 payload: {
289 item: {
290 id: "item-command",
291 type: "command_execution",
292 command: "git status --short",
293 },
294 },
295 }),
296 ],
297 "turn-1",
298 );
299
300 expect(entries).toHaveLength(1);
301 expect(entries[0]?.label).toBe("Command run");
302 expect(entries[0]?.detail).toBe("git status --short");
303 });
304
305 it("preserves full tool-call detail text without data truncation", () => {
306 const longCommand =
307 'node ./scripts/sync.js --project ct-round-5 --mode dry-run --include "very long argument with lots of detail and metadata to verify there is no hard truncation in session logic"';
308 const entries = deriveWorkLogEntries(
309 [
310 makeEvent({
311 id: "evt-tool-start",
312 method: "item/started",
313 turnId: "turn-1",
314 createdAt: "2026-02-08T10:00:05.000Z",
315 payload: {
316 item: { id: "item-tool", type: "tool_call", command: longCommand },
317 },
318 }),
319 ],
320 "turn-1",
321 );
322
323 expect(entries[0]?.detail).toBe(longCommand);
324 });
325
326 it("hides generic tool-call rows that have no identifying detail", () => {
327 const entries = deriveWorkLogEntries(
328 [
329 makeEvent({
330 id: "evt-tool-start",
331 method: "item/started",
332 turnId: "turn-1",
333 createdAt: "2026-02-08T10:00:05.000Z",
334 payload: {
335 item: { id: "item-tool", type: "tool_call" },
336 },
337 }),
338 ],
339 "turn-1",
340 );
341
342 expect(entries).toHaveLength(0);
343 });
344
345 it("includes collab tool-call rows with tool name detail", () => {
346 const entries = deriveWorkLogEntries(
347 [
348 makeEvent({
349 id: "evt-collab-start",
350 method: "item/started",
351 turnId: "turn-1",
352 createdAt: "2026-02-08T10:00:05.000Z",
353 payload: {
354 item: {
355 id: "item-collab",
356 type: "collabAgentToolCall",
357 tool: "spawnAgent",
358 },
359 },
360 }),
361 ],
362 "turn-1",
363 );
364
365 expect(entries).toHaveLength(1);
366 expect(entries[0]?.label).toBe("Tool call");
367 expect(entries[0]?.detail).toBe("spawnAgent");
368 });
369
370 it("can derive work-log entries across all turns when not scoped", () => {
371 const entries = deriveWorkLogEntries(
372 [
373 makeEvent({
374 id: "evt-turn-1-tool",
375 method: "item/started",
376 turnId: "turn-1",
377 createdAt: "2026-02-08T10:00:01.000Z",
378 payload: { item: { type: "tool_call", command: "ls -la" } },
379 }),
380 makeEvent({
381 id: "evt-turn-2-tool",
382 method: "item/started",
383 turnId: "turn-2",
384 createdAt: "2026-02-08T10:00:02.000Z",
385 payload: { item: { type: "tool_call", command: "pwd" } },
386 }),
387 ],
388 undefined,
389 );
390
391 expect(entries).toHaveLength(2);
392 expect(entries.map((entry) => entry.detail)).toEqual(["pwd", "ls -la"]);
393 });
394});
395
396describe("derivePendingApprovals", () => {
397 it("returns pending command/file-change approvals", () => {
398 const approvals = derivePendingApprovals([
399 makeEvent({
400 id: "evt-request",
401 kind: "request",
402 method: "item/commandExecution/requestApproval",
403 requestId: "req-1",
404 requestKind: "command",
405 createdAt: "2026-02-08T10:00:00.000Z",
406 payload: { command: "git commit -m 'msg'" },
407 }),
408 ]);
409
410 expect(approvals).toEqual([
411 {
412 requestId: "req-1",
413 requestKind: "command",
414 createdAt: "2026-02-08T10:00:00.000Z",
415 detail: "git commit -m 'msg'",
416 },
417 ]);
418 });
419
420 it("removes approvals after a decision event", () => {
421 const approvals = derivePendingApprovals([
422 makeEvent({
423 id: "evt-decision",
424 method: "item/requestApproval/decision",
425 requestId: "req-1",
426 createdAt: "2026-02-08T10:00:01.000Z",
427 }),
428 makeEvent({
429 id: "evt-request",
430 kind: "request",
431 method: "item/fileChange/requestApproval",
432 requestId: "req-1",
433 requestKind: "file-change",
434 createdAt: "2026-02-08T10:00:00.000Z",
435 }),
436 ]);
437
438 expect(approvals).toHaveLength(0);
439 });
440
441 it("clears pending approvals after turn completion", () => {
442 const approvals = derivePendingApprovals([
443 makeEvent({
444 id: "evt-turn-complete",
445 method: "turn/completed",
446 createdAt: "2026-02-08T10:00:01.000Z",
447 payload: { turn: { id: "turn-1", status: "completed" } },
448 }),
449 makeEvent({
450 id: "evt-request",
451 kind: "request",
452 method: "item/commandExecution/requestApproval",
453 requestId: "req-1",
454 requestKind: "command",
455 createdAt: "2026-02-08T10:00:00.000Z",
456 }),
457 ]);
458
459 expect(approvals).toHaveLength(0);
460 });
461});
462
463describe("evolveSession", () => {
464 it("updates thread id when thread starts", () => {
465 const previous = makeSession();
466 const next = evolveSession(
467 previous,
468 makeEvent({
469 method: "thread/started",
470 createdAt: "2026-02-08T10:01:00.000Z",
471 payload: { thread: { id: "thread-1" } },
472 }),
473 );
474
475 expect(next.threadId).toBe("thread-1");
476 expect(next.updatedAt).toBe("2026-02-08T10:01:00.000Z");
477 });
478
479 it("moves to running and records active turn on turn start", () => {
480 const previous = makeSession();
481 const next = evolveSession(
482 previous,
483 makeEvent({
484 method: "turn/started",
485 turnId: "turn-1",
486 createdAt: "2026-02-08T10:02:00.000Z",
487 }),
488 );
489
490 expect(next.status).toBe("running");
491 expect(next.activeTurnId).toBe("turn-1");
492 expect(next.updatedAt).toBe("2026-02-08T10:02:00.000Z");
493 });
494
495 it("returns to ready and clears active turn on successful completion", () => {
496 const previous = makeSession({
497 status: "running",
498 activeTurnId: "turn-1",
499 lastError: "older error",
500 });
501 const next = evolveSession(
502 previous,
503 makeEvent({
504 method: "turn/completed",
505 createdAt: "2026-02-08T10:03:00.000Z",
506 payload: { turn: { id: "turn-1", status: "completed" } },
507 }),
508 );
509
510 expect(next.status).toBe("ready");
511 expect(next.activeTurnId).toBeUndefined();
512 expect(next.lastError).toBe("older error");
513 });
514
515 it("marks session as error when turn fails", () => {
516 const previous = makeSession({
517 status: "running",
518 activeTurnId: "turn-1",
519 });
520 const next = evolveSession(
521 previous,
522 makeEvent({
523 method: "turn/completed",
524 createdAt: "2026-02-08T10:03:00.000Z",
525 payload: {
526 turn: { id: "turn-1", status: "failed", error: { message: "boom" } },
527 },
528 }),
529 );
530
531 expect(next.status).toBe("error");
532 expect(next.activeTurnId).toBeUndefined();
533 expect(next.lastError).toBe("boom");
534 });
535
536 it("moves to error on runtime error events", () => {
537 const previous = makeSession();
538 const next = evolveSession(
539 previous,
540 makeEvent({
541 kind: "error",
542 method: "runtime/error",
543 createdAt: "2026-02-08T10:04:00.000Z",
544 message: "runtime failure",
545 }),
546 );
547
548 expect(next.status).toBe("error");
549 expect(next.lastError).toBe("runtime failure");
550 });
551
552 it("closes session on close lifecycle events", () => {
553 const previous = makeSession({
554 status: "running",
555 activeTurnId: "turn-1",
556 });
557 const next = evolveSession(
558 previous,
559 makeEvent({
560 method: "session/closed",
561 createdAt: "2026-02-08T10:05:00.000Z",
562 message: "closed",
563 }),
564 );
565
566 expect(next.status).toBe("closed");
567 expect(next.activeTurnId).toBeUndefined();
568 expect(next.lastError).toBe("closed");
569 });
570});
571
572describe("applyEventToMessages", () => {
573 it("handles start/delta/completed flow for assistant messages", () => {
574 const activeAssistantItemRef = { current: null as string | null };
575 const started = applyEventToMessages(
576 [],
577 makeEvent({
578 method: "item/started",
579 createdAt: "2026-02-08T10:00:00.000Z",
580 payload: {
581 item: {
582 id: "item-1",
583 type: "agentMessage",
584 text: "Hello",
585 },
586 },
587 }),
588 activeAssistantItemRef,
589 );
590
591 expect(started).toEqual([
592 {
593 id: "item-1",
594 role: "assistant",
595 text: "Hello",
596 createdAt: "2026-02-08T10:00:00.000Z",
597 streaming: true,
598 },
599 ]);
600 expect(activeAssistantItemRef.current).toBe("item-1");
601
602 const withDelta = applyEventToMessages(
603 started,
604 makeEvent({
605 method: "item/agentMessage/delta",
606 createdAt: "2026-02-08T10:00:01.000Z",
607 itemId: "item-1",
608 textDelta: " world",
609 }),
610 activeAssistantItemRef,
611 );
612 expect(withDelta[0]?.text).toBe("Hello world");
613 expect(withDelta[0]?.streaming).toBe(true);
614
615 const completed = applyEventToMessages(
616 withDelta,
617 makeEvent({
618 method: "item/completed",
619 createdAt: "2026-02-08T10:00:02.000Z",
620 payload: {
621 item: {
622 id: "item-1",
623 type: "agentMessage",
624 text: "Hello world!",
625 },
626 },
627 }),
628 activeAssistantItemRef,
629 );
630 expect(completed[0]?.text).toBe("Hello world!");
631 expect(completed[0]?.streaming).toBe(false);
632 expect(activeAssistantItemRef.current).toBeNull();
633 });
634
635 it("supports out-of-order delta before started/completed", () => {
636 const activeAssistantItemRef = { current: null as string | null };
637 const withDelta = applyEventToMessages(
638 [],
639 makeEvent({
640 method: "item/agentMessage/delta",
641 createdAt: "2026-02-08T10:00:01.000Z",
642 itemId: "item-2",
643 textDelta: "Partial",
644 }),
645 activeAssistantItemRef,
646 );
647 expect(withDelta[0]?.id).toBe("item-2");
648 expect(withDelta[0]?.text).toBe("Partial");
649 expect(withDelta[0]?.streaming).toBe(true);
650 expect(activeAssistantItemRef.current).toBe("item-2");
651
652 const completed = applyEventToMessages(
653 withDelta,
654 makeEvent({
655 method: "item/completed",
656 createdAt: "2026-02-08T10:00:02.000Z",
657 payload: {
658 item: {
659 id: "item-2",
660 type: "agentMessage",
661 text: "Partial + final",
662 },
663 },
664 }),
665 activeAssistantItemRef,
666 );
667
668 expect(completed[0]?.text).toBe("Partial + final");
669 expect(completed[0]?.streaming).toBe(false);
670 });
671
672 it("clears all streaming flags when turn completes", () => {
673 const previous: ChatMessage[] = [
674 {
675 id: "m-user",
676 role: "user",
677 text: "Hi",
678 createdAt: "2026-02-08T10:00:00.000Z",
679 streaming: false,
680 },
681 {
682 id: "m-assistant",
683 role: "assistant",
684 text: "Typing",
685 createdAt: "2026-02-08T10:00:01.000Z",
686 streaming: true,
687 },
688 ];
689
690 const next = applyEventToMessages(
691 previous,
692 makeEvent({
693 method: "turn/completed",
694 }),
695 { current: null },
696 );
697
698 expect(next.every((entry) => entry.streaming === false)).toBe(true);
699 });
700});