Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import type { ProviderEvent, ProviderSession } from "@acme/contracts";
4import {
5 type WorkLogEntry,
6 applyEventToMessages,
7 deriveTimelineEntries,
8 deriveWorkLogEntries,
9 evolveSession,
10} from "./session-logic";
11import type { ChatMessage } from "./types";
12
13function makeEvent(overrides: Partial<ProviderEvent>): ProviderEvent {
14 return {
15 id: "evt-1",
16 kind: "notification",
17 provider: "codex",
18 sessionId: "sess-1",
19 createdAt: "2026-02-08T10:00:00.000Z",
20 method: "noop",
21 ...overrides,
22 };
23}
24
25function makeSession(overrides: Partial<ProviderSession> = {}): ProviderSession {
26 return {
27 sessionId: "sess-1",
28 provider: "codex",
29 status: "ready",
30 createdAt: "2026-02-08T09:59:00.000Z",
31 updatedAt: "2026-02-08T09:59:00.000Z",
32 ...overrides,
33 };
34}
35
36describe("deriveTimelineEntries", () => {
37 it("interleaves messages and work entries by timestamp", () => {
38 const messages: ChatMessage[] = [
39 {
40 id: "m-user",
41 role: "user",
42 text: "Hi",
43 createdAt: "2026-02-08T10:00:00.000Z",
44 streaming: false,
45 },
46 {
47 id: "m-assistant",
48 role: "assistant",
49 text: "Hello",
50 createdAt: "2026-02-08T10:05:00.000Z",
51 streaming: false,
52 },
53 ];
54 const workEntries: WorkLogEntry[] = [
55 {
56 id: "w-1",
57 label: "Tool call",
58 createdAt: "2026-02-08T10:02:00.000Z",
59 tone: "tool",
60 },
61 {
62 id: "w-2",
63 label: "Preamble",
64 createdAt: "2026-02-08T10:03:00.000Z",
65 tone: "thinking",
66 },
67 ];
68
69 const timeline = deriveTimelineEntries(messages, workEntries);
70
71 expect(timeline.map((entry) => entry.id)).toEqual([
72 "message:m-user",
73 "work:w-1",
74 "work:w-2",
75 "message:m-assistant",
76 ]);
77 });
78
79 it("prefers work entries when timestamps are equal", () => {
80 const messages: ChatMessage[] = [
81 {
82 id: "m-1",
83 role: "assistant",
84 text: "Done",
85 createdAt: "2026-02-08T10:00:00.000Z",
86 streaming: false,
87 },
88 ];
89 const workEntries: WorkLogEntry[] = [
90 {
91 id: "w-1",
92 label: "Tool call",
93 createdAt: "2026-02-08T10:00:00.000Z",
94 tone: "tool",
95 },
96 ];
97
98 const timeline = deriveTimelineEntries(messages, workEntries);
99
100 expect(timeline.map((entry) => entry.id)).toEqual(["work:w-1", "message:m-1"]);
101 });
102});
103
104describe("deriveWorkLogEntries", () => {
105 it("drops preamble/work events from the visible work log", () => {
106 const entries = deriveWorkLogEntries(
107 [
108 makeEvent({
109 id: "evt-1",
110 method: "item/started",
111 turnId: "turn-1",
112 createdAt: "2026-02-08T10:00:00.000Z",
113 payload: { item: { type: "preamble", text: "thinking" } },
114 }),
115 makeEvent({
116 id: "evt-2",
117 method: "item/started",
118 turnId: "turn-1",
119 createdAt: "2026-02-08T10:00:01.000Z",
120 payload: { item: { type: "work", text: "planning" } },
121 }),
122 makeEvent({
123 id: "evt-3",
124 method: "item/started",
125 turnId: "turn-1",
126 createdAt: "2026-02-08T10:00:02.000Z",
127 payload: { item: { type: "tool_call", command: "ls -la" } },
128 }),
129 ],
130 "turn-1",
131 );
132
133 expect(entries).toHaveLength(1);
134 expect(entries[0]?.label).toBe("Tool call");
135 });
136
137 it("logs turn completion with elapsed time", () => {
138 const entries = deriveWorkLogEntries(
139 [
140 makeEvent({
141 id: "evt-start",
142 method: "turn/started",
143 turnId: "turn-1",
144 createdAt: "2026-02-08T10:00:00.000Z",
145 }),
146 makeEvent({
147 id: "evt-complete",
148 method: "turn/completed",
149 turnId: "turn-1",
150 createdAt: "2026-02-08T10:00:10.000Z",
151 payload: { turn: { id: "turn-1", status: "completed" } },
152 }),
153 ],
154 "turn-1",
155 );
156
157 expect(entries[0]?.label).toBe("Turn complete in 10s");
158 });
159
160 it("hides reasoning and agent-message noise from the visible work log", () => {
161 const entries = deriveWorkLogEntries(
162 [
163 makeEvent({
164 id: "evt-turn-start",
165 method: "turn/started",
166 turnId: "turn-1",
167 createdAt: "2026-02-08T10:00:00.000Z",
168 }),
169 makeEvent({
170 id: "evt-summary-part",
171 method: "item/reasoning/summaryPartAdded",
172 turnId: "turn-1",
173 createdAt: "2026-02-08T10:00:01.000Z",
174 }),
175 makeEvent({
176 id: "evt-summary-delta",
177 method: "item/reasoning/summaryTextDelta",
178 turnId: "turn-1",
179 createdAt: "2026-02-08T10:00:02.000Z",
180 }),
181 makeEvent({
182 id: "evt-agent-start",
183 method: "item/started",
184 turnId: "turn-1",
185 createdAt: "2026-02-08T10:00:03.000Z",
186 payload: {
187 item: { id: "item-msg", type: "agentMessage", text: "Working..." },
188 },
189 }),
190 makeEvent({
191 id: "evt-agent-complete",
192 method: "item/completed",
193 turnId: "turn-1",
194 createdAt: "2026-02-08T10:00:04.000Z",
195 payload: {
196 item: {
197 id: "item-msg",
198 type: "agentMessage",
199 text: "Done response",
200 },
201 },
202 }),
203 makeEvent({
204 id: "evt-tool-start",
205 method: "item/started",
206 turnId: "turn-1",
207 createdAt: "2026-02-08T10:00:05.000Z",
208 payload: {
209 item: { id: "item-tool", type: "tool_call", command: "ls -la" },
210 },
211 }),
212 makeEvent({
213 id: "evt-tool-complete",
214 method: "item/completed",
215 turnId: "turn-1",
216 createdAt: "2026-02-08T10:00:06.000Z",
217 payload: {
218 item: { id: "item-tool", type: "tool_call", command: "ls -la" },
219 },
220 }),
221 makeEvent({
222 id: "evt-turn-complete",
223 method: "turn/completed",
224 turnId: "turn-1",
225 createdAt: "2026-02-08T10:00:08.000Z",
226 payload: { turn: { id: "turn-1", status: "completed" } },
227 }),
228 ],
229 "turn-1",
230 );
231
232 expect(entries.map((entry) => entry.label)).toEqual([
233 "Turn complete in 8.0s",
234 "Tool call complete",
235 "Tool call",
236 ]);
237 });
238
239 it("preserves full tool-call detail text without data truncation", () => {
240 const longCommand =
241 '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"';
242 const entries = deriveWorkLogEntries(
243 [
244 makeEvent({
245 id: "evt-tool-start",
246 method: "item/started",
247 turnId: "turn-1",
248 createdAt: "2026-02-08T10:00:05.000Z",
249 payload: {
250 item: { id: "item-tool", type: "tool_call", command: longCommand },
251 },
252 }),
253 ],
254 "turn-1",
255 );
256
257 expect(entries[0]?.detail).toBe(longCommand);
258 });
259
260 it("can derive work-log entries across all turns when not scoped", () => {
261 const entries = deriveWorkLogEntries(
262 [
263 makeEvent({
264 id: "evt-turn-1-tool",
265 method: "item/started",
266 turnId: "turn-1",
267 createdAt: "2026-02-08T10:00:01.000Z",
268 payload: { item: { type: "tool_call", command: "ls -la" } },
269 }),
270 makeEvent({
271 id: "evt-turn-2-tool",
272 method: "item/started",
273 turnId: "turn-2",
274 createdAt: "2026-02-08T10:00:02.000Z",
275 payload: { item: { type: "tool_call", command: "pwd" } },
276 }),
277 ],
278 undefined,
279 );
280
281 expect(entries).toHaveLength(2);
282 expect(entries.map((entry) => entry.detail)).toEqual(["pwd", "ls -la"]);
283 });
284});
285
286describe("evolveSession", () => {
287 it("updates thread id when thread starts", () => {
288 const previous = makeSession();
289 const next = evolveSession(
290 previous,
291 makeEvent({
292 method: "thread/started",
293 createdAt: "2026-02-08T10:01:00.000Z",
294 payload: { thread: { id: "thread-1" } },
295 }),
296 );
297
298 expect(next.threadId).toBe("thread-1");
299 expect(next.updatedAt).toBe("2026-02-08T10:01:00.000Z");
300 });
301
302 it("moves to running and records active turn on turn start", () => {
303 const previous = makeSession();
304 const next = evolveSession(
305 previous,
306 makeEvent({
307 method: "turn/started",
308 turnId: "turn-1",
309 createdAt: "2026-02-08T10:02:00.000Z",
310 }),
311 );
312
313 expect(next.status).toBe("running");
314 expect(next.activeTurnId).toBe("turn-1");
315 expect(next.updatedAt).toBe("2026-02-08T10:02:00.000Z");
316 });
317
318 it("returns to ready and clears active turn on successful completion", () => {
319 const previous = makeSession({
320 status: "running",
321 activeTurnId: "turn-1",
322 lastError: "older error",
323 });
324 const next = evolveSession(
325 previous,
326 makeEvent({
327 method: "turn/completed",
328 createdAt: "2026-02-08T10:03:00.000Z",
329 payload: { turn: { id: "turn-1", status: "completed" } },
330 }),
331 );
332
333 expect(next.status).toBe("ready");
334 expect(next.activeTurnId).toBeUndefined();
335 expect(next.lastError).toBe("older error");
336 });
337
338 it("marks session as error when turn fails", () => {
339 const previous = makeSession({
340 status: "running",
341 activeTurnId: "turn-1",
342 });
343 const next = evolveSession(
344 previous,
345 makeEvent({
346 method: "turn/completed",
347 createdAt: "2026-02-08T10:03:00.000Z",
348 payload: {
349 turn: { id: "turn-1", status: "failed", error: { message: "boom" } },
350 },
351 }),
352 );
353
354 expect(next.status).toBe("error");
355 expect(next.activeTurnId).toBeUndefined();
356 expect(next.lastError).toBe("boom");
357 });
358
359 it("moves to error on runtime error events", () => {
360 const previous = makeSession();
361 const next = evolveSession(
362 previous,
363 makeEvent({
364 kind: "error",
365 method: "runtime/error",
366 createdAt: "2026-02-08T10:04:00.000Z",
367 message: "runtime failure",
368 }),
369 );
370
371 expect(next.status).toBe("error");
372 expect(next.lastError).toBe("runtime failure");
373 });
374
375 it("closes session on close lifecycle events", () => {
376 const previous = makeSession({
377 status: "running",
378 activeTurnId: "turn-1",
379 });
380 const next = evolveSession(
381 previous,
382 makeEvent({
383 method: "session/closed",
384 createdAt: "2026-02-08T10:05:00.000Z",
385 message: "closed",
386 }),
387 );
388
389 expect(next.status).toBe("closed");
390 expect(next.activeTurnId).toBeUndefined();
391 expect(next.lastError).toBe("closed");
392 });
393});
394
395describe("applyEventToMessages", () => {
396 it("handles start/delta/completed flow for assistant messages", () => {
397 const activeAssistantItemRef = { current: null as string | null };
398 const started = applyEventToMessages(
399 [],
400 makeEvent({
401 method: "item/started",
402 createdAt: "2026-02-08T10:00:00.000Z",
403 payload: {
404 item: {
405 id: "item-1",
406 type: "agentMessage",
407 text: "Hello",
408 },
409 },
410 }),
411 activeAssistantItemRef,
412 );
413
414 expect(started).toEqual([
415 {
416 id: "item-1",
417 role: "assistant",
418 text: "Hello",
419 createdAt: "2026-02-08T10:00:00.000Z",
420 streaming: true,
421 },
422 ]);
423 expect(activeAssistantItemRef.current).toBe("item-1");
424
425 const withDelta = applyEventToMessages(
426 started,
427 makeEvent({
428 method: "item/agentMessage/delta",
429 createdAt: "2026-02-08T10:00:01.000Z",
430 itemId: "item-1",
431 textDelta: " world",
432 }),
433 activeAssistantItemRef,
434 );
435 expect(withDelta[0]?.text).toBe("Hello world");
436 expect(withDelta[0]?.streaming).toBe(true);
437
438 const completed = applyEventToMessages(
439 withDelta,
440 makeEvent({
441 method: "item/completed",
442 createdAt: "2026-02-08T10:00:02.000Z",
443 payload: {
444 item: {
445 id: "item-1",
446 type: "agentMessage",
447 text: "Hello world!",
448 },
449 },
450 }),
451 activeAssistantItemRef,
452 );
453 expect(completed[0]?.text).toBe("Hello world!");
454 expect(completed[0]?.streaming).toBe(false);
455 expect(activeAssistantItemRef.current).toBeNull();
456 });
457
458 it("supports out-of-order delta before started/completed", () => {
459 const activeAssistantItemRef = { current: null as string | null };
460 const withDelta = applyEventToMessages(
461 [],
462 makeEvent({
463 method: "item/agentMessage/delta",
464 createdAt: "2026-02-08T10:00:01.000Z",
465 itemId: "item-2",
466 textDelta: "Partial",
467 }),
468 activeAssistantItemRef,
469 );
470 expect(withDelta[0]?.id).toBe("item-2");
471 expect(withDelta[0]?.text).toBe("Partial");
472 expect(withDelta[0]?.streaming).toBe(true);
473 expect(activeAssistantItemRef.current).toBe("item-2");
474
475 const completed = applyEventToMessages(
476 withDelta,
477 makeEvent({
478 method: "item/completed",
479 createdAt: "2026-02-08T10:00:02.000Z",
480 payload: {
481 item: {
482 id: "item-2",
483 type: "agentMessage",
484 text: "Partial + final",
485 },
486 },
487 }),
488 activeAssistantItemRef,
489 );
490
491 expect(completed[0]?.text).toBe("Partial + final");
492 expect(completed[0]?.streaming).toBe(false);
493 });
494
495 it("clears all streaming flags when turn completes", () => {
496 const previous: ChatMessage[] = [
497 {
498 id: "m-user",
499 role: "user",
500 text: "Hi",
501 createdAt: "2026-02-08T10:00:00.000Z",
502 streaming: false,
503 },
504 {
505 id: "m-assistant",
506 role: "assistant",
507 text: "Typing",
508 createdAt: "2026-02-08T10:00:01.000Z",
509 streaming: true,
510 },
511 ];
512
513 const next = applyEventToMessages(
514 previous,
515 makeEvent({
516 method: "turn/completed",
517 }),
518 { current: null },
519 );
520
521 expect(next.every((entry) => entry.streaming === false)).toBe(true);
522 });
523});