Open History to see the last commit
1import {
2 EventId,
3 MessageId,
4 ThreadId,
5 TurnId,
6 type OrchestrationThreadActivity,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "vitest";
9
10import {
11 deriveCompletionDividerBeforeEntryId,
12 deriveActiveWorkStartedAt,
13 deriveActivePlanState,
14 PROVIDER_OPTIONS,
15 derivePendingApprovals,
16 derivePendingUserInputs,
17 deriveTimelineEntries,
18 deriveWorkLogEntries,
19 findLatestProposedPlan,
20 findSidebarProposedPlan,
21 hasActionableProposedPlan,
22 hasToolActivityForTurn,
23 isLatestTurnSettled,
24} from "./session-logic";
25
26function makeActivity(overrides: {
27 id?: string;
28 createdAt?: string;
29 kind?: string;
30 summary?: string;
31 tone?: OrchestrationThreadActivity["tone"];
32 payload?: Record<string, unknown>;
33 turnId?: string;
34 sequence?: number;
35}): OrchestrationThreadActivity {
36 const payload = overrides.payload ?? {};
37 return {
38 id: EventId.makeUnsafe(overrides.id ?? crypto.randomUUID()),
39 createdAt: overrides.createdAt ?? "2026-02-23T00:00:00.000Z",
40 kind: overrides.kind ?? "tool.started",
41 summary: overrides.summary ?? "Tool call",
42 tone: overrides.tone ?? "tool",
43 payload,
44 turnId: overrides.turnId ? TurnId.makeUnsafe(overrides.turnId) : null,
45 ...(overrides.sequence !== undefined ? { sequence: overrides.sequence } : {}),
46 };
47}
48
49describe("derivePendingApprovals", () => {
50 it("tracks open approvals and removes resolved ones", () => {
51 const activities: OrchestrationThreadActivity[] = [
52 makeActivity({
53 id: "approval-open",
54 createdAt: "2026-02-23T00:00:01.000Z",
55 kind: "approval.requested",
56 summary: "Command approval requested",
57 tone: "approval",
58 payload: {
59 requestId: "req-1",
60 requestKind: "command",
61 detail: "bun run lint",
62 },
63 }),
64 makeActivity({
65 id: "approval-close",
66 createdAt: "2026-02-23T00:00:02.000Z",
67 kind: "approval.resolved",
68 summary: "Approval resolved",
69 tone: "info",
70 payload: { requestId: "req-2" },
71 }),
72 makeActivity({
73 id: "approval-closed-request",
74 createdAt: "2026-02-23T00:00:01.500Z",
75 kind: "approval.requested",
76 summary: "File-change approval requested",
77 tone: "approval",
78 payload: { requestId: "req-2", requestKind: "file-change" },
79 }),
80 ];
81
82 expect(derivePendingApprovals(activities)).toEqual([
83 {
84 requestId: "req-1",
85 requestKind: "command",
86 createdAt: "2026-02-23T00:00:01.000Z",
87 detail: "bun run lint",
88 },
89 ]);
90 });
91
92 it("maps canonical requestType payloads into pending approvals", () => {
93 const activities: OrchestrationThreadActivity[] = [
94 makeActivity({
95 id: "approval-open-request-type",
96 createdAt: "2026-02-23T00:00:01.000Z",
97 kind: "approval.requested",
98 summary: "Command approval requested",
99 tone: "approval",
100 payload: {
101 requestId: "req-request-type",
102 requestType: "command_execution_approval",
103 detail: "pwd",
104 },
105 }),
106 ];
107
108 expect(derivePendingApprovals(activities)).toEqual([
109 {
110 requestId: "req-request-type",
111 requestKind: "command",
112 createdAt: "2026-02-23T00:00:01.000Z",
113 detail: "pwd",
114 },
115 ]);
116 });
117
118 it("clears stale pending approvals when provider reports unknown pending request", () => {
119 const activities: OrchestrationThreadActivity[] = [
120 makeActivity({
121 id: "approval-open-stale",
122 createdAt: "2026-02-23T00:00:01.000Z",
123 kind: "approval.requested",
124 summary: "Command approval requested",
125 tone: "approval",
126 payload: {
127 requestId: "req-stale-1",
128 requestKind: "command",
129 },
130 }),
131 makeActivity({
132 id: "approval-failed-stale",
133 createdAt: "2026-02-23T00:00:02.000Z",
134 kind: "provider.approval.respond.failed",
135 summary: "Provider approval response failed",
136 tone: "error",
137 payload: {
138 requestId: "req-stale-1",
139 detail: "Unknown pending permission request: req-stale-1",
140 },
141 }),
142 ];
143
144 expect(derivePendingApprovals(activities)).toEqual([]);
145 });
146
147 it("clears stale pending approvals when the backend marks them stale after restart", () => {
148 const activities: OrchestrationThreadActivity[] = [
149 makeActivity({
150 id: "approval-open-stale-restart",
151 createdAt: "2026-02-23T00:00:01.000Z",
152 kind: "approval.requested",
153 summary: "Command approval requested",
154 tone: "approval",
155 payload: {
156 requestId: "req-stale-restart-1",
157 requestKind: "command",
158 },
159 }),
160 makeActivity({
161 id: "approval-failed-stale-restart",
162 createdAt: "2026-02-23T00:00:02.000Z",
163 kind: "provider.approval.respond.failed",
164 summary: "Provider approval response failed",
165 tone: "error",
166 payload: {
167 requestId: "req-stale-restart-1",
168 detail:
169 "Stale pending approval request: req-stale-restart-1. Provider callback state does not survive app restarts or recovered sessions. Restart the turn to continue.",
170 },
171 }),
172 ];
173
174 expect(derivePendingApprovals(activities)).toEqual([]);
175 });
176});
177
178describe("derivePendingUserInputs", () => {
179 it("tracks open structured prompts and removes resolved ones", () => {
180 const activities: OrchestrationThreadActivity[] = [
181 makeActivity({
182 id: "user-input-open",
183 createdAt: "2026-02-23T00:00:01.000Z",
184 kind: "user-input.requested",
185 summary: "User input requested",
186 tone: "info",
187 payload: {
188 requestId: "req-user-input-1",
189 questions: [
190 {
191 id: "sandbox_mode",
192 header: "Sandbox",
193 question: "Which mode should be used?",
194 options: [
195 {
196 label: "workspace-write",
197 description: "Allow workspace writes only",
198 },
199 ],
200 },
201 ],
202 },
203 }),
204 makeActivity({
205 id: "user-input-resolved",
206 createdAt: "2026-02-23T00:00:02.000Z",
207 kind: "user-input.resolved",
208 summary: "User input submitted",
209 tone: "info",
210 payload: {
211 requestId: "req-user-input-2",
212 answers: {
213 sandbox_mode: "workspace-write",
214 },
215 },
216 }),
217 makeActivity({
218 id: "user-input-open-2",
219 createdAt: "2026-02-23T00:00:01.500Z",
220 kind: "user-input.requested",
221 summary: "User input requested",
222 tone: "info",
223 payload: {
224 requestId: "req-user-input-2",
225 questions: [
226 {
227 id: "approval",
228 header: "Approval",
229 question: "Continue?",
230 options: [
231 {
232 label: "yes",
233 description: "Continue execution",
234 },
235 ],
236 },
237 ],
238 },
239 }),
240 ];
241
242 expect(derivePendingUserInputs(activities)).toEqual([
243 {
244 requestId: "req-user-input-1",
245 createdAt: "2026-02-23T00:00:01.000Z",
246 questions: [
247 {
248 id: "sandbox_mode",
249 header: "Sandbox",
250 question: "Which mode should be used?",
251 options: [
252 {
253 label: "workspace-write",
254 description: "Allow workspace writes only",
255 },
256 ],
257 },
258 ],
259 },
260 ]);
261 });
262
263 it("clears stale pending user-input prompts when the provider reports an orphaned request", () => {
264 const activities: OrchestrationThreadActivity[] = [
265 makeActivity({
266 id: "user-input-open-stale",
267 createdAt: "2026-02-23T00:00:01.000Z",
268 kind: "user-input.requested",
269 summary: "User input requested",
270 tone: "info",
271 payload: {
272 requestId: "req-user-input-stale-1",
273 questions: [
274 {
275 id: "sandbox_mode",
276 header: "Sandbox",
277 question: "Which mode should be used?",
278 options: [
279 {
280 label: "workspace-write",
281 description: "Allow workspace writes only",
282 },
283 ],
284 },
285 ],
286 },
287 }),
288 makeActivity({
289 id: "user-input-failed-stale",
290 createdAt: "2026-02-23T00:00:02.000Z",
291 kind: "provider.user-input.respond.failed",
292 summary: "Provider user input response failed",
293 tone: "error",
294 payload: {
295 requestId: "req-user-input-stale-1",
296 detail:
297 "Stale pending user-input request: req-user-input-stale-1. Provider callback state does not survive app restarts or recovered sessions. Restart the turn to continue.",
298 },
299 }),
300 ];
301
302 expect(derivePendingUserInputs(activities)).toEqual([]);
303 });
304});
305
306describe("deriveActivePlanState", () => {
307 it("returns the latest plan update for the active turn", () => {
308 const activities: OrchestrationThreadActivity[] = [
309 makeActivity({
310 id: "plan-old",
311 createdAt: "2026-02-23T00:00:01.000Z",
312 kind: "turn.plan.updated",
313 summary: "Plan updated",
314 tone: "info",
315 turnId: "turn-1",
316 payload: {
317 explanation: "Initial plan",
318 plan: [{ step: "Inspect code", status: "pending" }],
319 },
320 }),
321 makeActivity({
322 id: "plan-latest",
323 createdAt: "2026-02-23T00:00:02.000Z",
324 kind: "turn.plan.updated",
325 summary: "Plan updated",
326 tone: "info",
327 turnId: "turn-1",
328 payload: {
329 explanation: "Refined plan",
330 plan: [{ step: "Implement Codex user input", status: "inProgress" }],
331 },
332 }),
333 ];
334
335 expect(deriveActivePlanState(activities, TurnId.makeUnsafe("turn-1"))).toEqual({
336 createdAt: "2026-02-23T00:00:02.000Z",
337 turnId: "turn-1",
338 explanation: "Refined plan",
339 steps: [{ step: "Implement Codex user input", status: "inProgress" }],
340 });
341 });
342});
343
344describe("findLatestProposedPlan", () => {
345 it("prefers the latest proposed plan for the active turn", () => {
346 expect(
347 findLatestProposedPlan(
348 [
349 {
350 id: "plan:thread-1:turn:turn-1",
351 turnId: TurnId.makeUnsafe("turn-1"),
352 planMarkdown: "# Older",
353 implementedAt: null,
354 implementationThreadId: null,
355 createdAt: "2026-02-23T00:00:01.000Z",
356 updatedAt: "2026-02-23T00:00:01.000Z",
357 },
358 {
359 id: "plan:thread-1:turn:turn-1",
360 turnId: TurnId.makeUnsafe("turn-1"),
361 planMarkdown: "# Latest",
362 implementedAt: null,
363 implementationThreadId: null,
364 createdAt: "2026-02-23T00:00:01.000Z",
365 updatedAt: "2026-02-23T00:00:02.000Z",
366 },
367 {
368 id: "plan:thread-1:turn:turn-2",
369 turnId: TurnId.makeUnsafe("turn-2"),
370 planMarkdown: "# Different turn",
371 implementedAt: null,
372 implementationThreadId: null,
373 createdAt: "2026-02-23T00:00:03.000Z",
374 updatedAt: "2026-02-23T00:00:03.000Z",
375 },
376 ],
377 TurnId.makeUnsafe("turn-1"),
378 ),
379 ).toEqual({
380 id: "plan:thread-1:turn:turn-1",
381 turnId: "turn-1",
382 planMarkdown: "# Latest",
383 implementedAt: null,
384 implementationThreadId: null,
385 createdAt: "2026-02-23T00:00:01.000Z",
386 updatedAt: "2026-02-23T00:00:02.000Z",
387 });
388 });
389
390 it("falls back to the most recently updated proposed plan", () => {
391 const latestPlan = findLatestProposedPlan(
392 [
393 {
394 id: "plan:thread-1:turn:turn-1",
395 turnId: TurnId.makeUnsafe("turn-1"),
396 planMarkdown: "# First",
397 implementedAt: null,
398 implementationThreadId: null,
399 createdAt: "2026-02-23T00:00:01.000Z",
400 updatedAt: "2026-02-23T00:00:01.000Z",
401 },
402 {
403 id: "plan:thread-1:turn:turn-2",
404 turnId: TurnId.makeUnsafe("turn-2"),
405 planMarkdown: "# Latest",
406 implementedAt: null,
407 implementationThreadId: null,
408 createdAt: "2026-02-23T00:00:02.000Z",
409 updatedAt: "2026-02-23T00:00:03.000Z",
410 },
411 ],
412 null,
413 );
414
415 expect(latestPlan?.planMarkdown).toBe("# Latest");
416 });
417});
418
419describe("hasActionableProposedPlan", () => {
420 it("returns true for an unimplemented proposed plan", () => {
421 expect(
422 hasActionableProposedPlan({
423 id: "plan-1",
424 turnId: TurnId.makeUnsafe("turn-1"),
425 planMarkdown: "# Plan",
426 implementedAt: null,
427 implementationThreadId: null,
428 createdAt: "2026-02-23T00:00:00.000Z",
429 updatedAt: "2026-02-23T00:00:01.000Z",
430 }),
431 ).toBe(true);
432 });
433
434 it("returns false for a proposed plan already implemented elsewhere", () => {
435 expect(
436 hasActionableProposedPlan({
437 id: "plan-1",
438 turnId: TurnId.makeUnsafe("turn-1"),
439 planMarkdown: "# Plan",
440 implementedAt: "2026-02-23T00:00:02.000Z",
441 implementationThreadId: ThreadId.makeUnsafe("thread-implement"),
442 createdAt: "2026-02-23T00:00:00.000Z",
443 updatedAt: "2026-02-23T00:00:02.000Z",
444 }),
445 ).toBe(false);
446 });
447});
448
449describe("findSidebarProposedPlan", () => {
450 it("prefers the running turn source proposed plan when available on the same thread", () => {
451 expect(
452 findSidebarProposedPlan({
453 threads: [
454 {
455 id: ThreadId.makeUnsafe("thread-1"),
456 proposedPlans: [
457 {
458 id: "plan-1",
459 turnId: TurnId.makeUnsafe("turn-plan"),
460 planMarkdown: "# Source plan",
461 implementedAt: "2026-02-23T00:00:03.000Z",
462 implementationThreadId: ThreadId.makeUnsafe("thread-2"),
463 createdAt: "2026-02-23T00:00:01.000Z",
464 updatedAt: "2026-02-23T00:00:02.000Z",
465 },
466 ],
467 },
468 {
469 id: ThreadId.makeUnsafe("thread-2"),
470 proposedPlans: [
471 {
472 id: "plan-2",
473 turnId: TurnId.makeUnsafe("turn-other"),
474 planMarkdown: "# Latest elsewhere",
475 implementedAt: null,
476 implementationThreadId: null,
477 createdAt: "2026-02-23T00:00:04.000Z",
478 updatedAt: "2026-02-23T00:00:05.000Z",
479 },
480 ],
481 },
482 ],
483 latestTurn: {
484 turnId: TurnId.makeUnsafe("turn-implementation"),
485 sourceProposedPlan: {
486 threadId: ThreadId.makeUnsafe("thread-1"),
487 planId: "plan-1",
488 },
489 },
490 latestTurnSettled: false,
491 threadId: ThreadId.makeUnsafe("thread-1"),
492 }),
493 ).toEqual({
494 id: "plan-1",
495 turnId: "turn-plan",
496 planMarkdown: "# Source plan",
497 implementedAt: "2026-02-23T00:00:03.000Z",
498 implementationThreadId: "thread-2",
499 createdAt: "2026-02-23T00:00:01.000Z",
500 updatedAt: "2026-02-23T00:00:02.000Z",
501 });
502 });
503
504 it("falls back to the latest proposed plan once the turn is settled", () => {
505 expect(
506 findSidebarProposedPlan({
507 threads: [
508 {
509 id: ThreadId.makeUnsafe("thread-1"),
510 proposedPlans: [
511 {
512 id: "plan-1",
513 turnId: TurnId.makeUnsafe("turn-plan"),
514 planMarkdown: "# Older",
515 implementedAt: null,
516 implementationThreadId: null,
517 createdAt: "2026-02-23T00:00:01.000Z",
518 updatedAt: "2026-02-23T00:00:02.000Z",
519 },
520 {
521 id: "plan-2",
522 turnId: TurnId.makeUnsafe("turn-latest"),
523 planMarkdown: "# Latest",
524 implementedAt: null,
525 implementationThreadId: null,
526 createdAt: "2026-02-23T00:00:03.000Z",
527 updatedAt: "2026-02-23T00:00:04.000Z",
528 },
529 ],
530 },
531 ],
532 latestTurn: {
533 turnId: TurnId.makeUnsafe("turn-implementation"),
534 sourceProposedPlan: {
535 threadId: ThreadId.makeUnsafe("thread-1"),
536 planId: "plan-1",
537 },
538 },
539 latestTurnSettled: true,
540 threadId: ThreadId.makeUnsafe("thread-1"),
541 })?.planMarkdown,
542 ).toBe("# Latest");
543 });
544});
545
546describe("deriveWorkLogEntries", () => {
547 it("omits tool started entries and keeps completed entries", () => {
548 const activities: OrchestrationThreadActivity[] = [
549 makeActivity({
550 id: "tool-complete",
551 createdAt: "2026-02-23T00:00:03.000Z",
552 summary: "Tool call complete",
553 kind: "tool.completed",
554 }),
555 makeActivity({
556 id: "tool-start",
557 createdAt: "2026-02-23T00:00:02.000Z",
558 summary: "Tool call",
559 kind: "tool.started",
560 }),
561 ];
562
563 const entries = deriveWorkLogEntries(activities, undefined);
564 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
565 });
566
567 it("omits task start and completion lifecycle entries", () => {
568 const activities: OrchestrationThreadActivity[] = [
569 makeActivity({
570 id: "task-start",
571 createdAt: "2026-02-23T00:00:01.000Z",
572 kind: "task.started",
573 summary: "default task started",
574 tone: "info",
575 }),
576 makeActivity({
577 id: "task-progress",
578 createdAt: "2026-02-23T00:00:02.000Z",
579 kind: "task.progress",
580 summary: "Updating files",
581 tone: "info",
582 }),
583 makeActivity({
584 id: "task-complete",
585 createdAt: "2026-02-23T00:00:03.000Z",
586 kind: "task.completed",
587 summary: "Task completed",
588 tone: "info",
589 }),
590 ];
591
592 const entries = deriveWorkLogEntries(activities, undefined);
593 expect(entries.map((entry) => entry.id)).toEqual(["task-progress"]);
594 });
595
596 it("filters by turn id when provided", () => {
597 const activities: OrchestrationThreadActivity[] = [
598 makeActivity({ id: "turn-1", turnId: "turn-1", summary: "Tool call", kind: "tool.started" }),
599 makeActivity({
600 id: "turn-2",
601 turnId: "turn-2",
602 summary: "Tool call complete",
603 kind: "tool.completed",
604 }),
605 makeActivity({ id: "no-turn", summary: "Checkpoint captured", tone: "info" }),
606 ];
607
608 const entries = deriveWorkLogEntries(activities, TurnId.makeUnsafe("turn-2"));
609 expect(entries.map((entry) => entry.id)).toEqual(["turn-2"]);
610 });
611
612 it("omits checkpoint captured info entries", () => {
613 const activities: OrchestrationThreadActivity[] = [
614 makeActivity({
615 id: "checkpoint",
616 createdAt: "2026-02-23T00:00:01.000Z",
617 summary: "Checkpoint captured",
618 tone: "info",
619 }),
620 makeActivity({
621 id: "tool-complete",
622 createdAt: "2026-02-23T00:00:02.000Z",
623 summary: "Ran command",
624 tone: "tool",
625 kind: "tool.completed",
626 }),
627 ];
628
629 const entries = deriveWorkLogEntries(activities, undefined);
630 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
631 });
632
633 it("omits ExitPlanMode lifecycle entries once the plan card is shown", () => {
634 const activities: OrchestrationThreadActivity[] = [
635 makeActivity({
636 id: "exit-plan-updated",
637 createdAt: "2026-02-23T00:00:01.000Z",
638 kind: "tool.updated",
639 summary: "Tool call",
640 payload: {
641 detail: 'ExitPlanMode: {"allowedPrompts":[{"tool":"Bash","prompt":"run tests"}]}',
642 },
643 }),
644 makeActivity({
645 id: "exit-plan-completed",
646 createdAt: "2026-02-23T00:00:02.000Z",
647 kind: "tool.completed",
648 summary: "Tool call",
649 payload: {
650 detail: "ExitPlanMode: {}",
651 },
652 }),
653 makeActivity({
654 id: "real-work-log",
655 createdAt: "2026-02-23T00:00:03.000Z",
656 kind: "tool.completed",
657 summary: "Ran command",
658 payload: {
659 itemType: "command_execution",
660 detail: "Bash: bun test",
661 },
662 }),
663 ];
664
665 const entries = deriveWorkLogEntries(activities, undefined);
666 expect(entries.map((entry) => entry.id)).toEqual(["real-work-log"]);
667 });
668
669 it("orders work log by activity sequence when present", () => {
670 const activities: OrchestrationThreadActivity[] = [
671 makeActivity({
672 id: "second",
673 createdAt: "2026-02-23T00:00:03.000Z",
674 sequence: 2,
675 summary: "Tool call complete",
676 kind: "tool.completed",
677 }),
678 makeActivity({
679 id: "first",
680 createdAt: "2026-02-23T00:00:04.000Z",
681 sequence: 1,
682 summary: "Tool call complete",
683 kind: "tool.completed",
684 }),
685 ];
686
687 const entries = deriveWorkLogEntries(activities, undefined);
688 expect(entries.map((entry) => entry.id)).toEqual(["first", "second"]);
689 });
690
691 it("extracts command text for command tool activities", () => {
692 const activities: OrchestrationThreadActivity[] = [
693 makeActivity({
694 id: "command-tool",
695 kind: "tool.completed",
696 summary: "Ran command",
697 payload: {
698 itemType: "command_execution",
699 data: {
700 item: {
701 command: ["bun", "run", "lint"],
702 },
703 },
704 },
705 }),
706 ];
707
708 const [entry] = deriveWorkLogEntries(activities, undefined);
709 expect(entry?.command).toBe("bun run lint");
710 });
711
712 it("keeps compact Codex tool metadata used for icons and labels", () => {
713 const activities: OrchestrationThreadActivity[] = [
714 makeActivity({
715 id: "tool-with-metadata",
716 kind: "tool.completed",
717 summary: "bash",
718 payload: {
719 itemType: "command_execution",
720 title: "bash",
721 status: "completed",
722 detail: '{ "dev": "vite dev --port 3000" } <exited with exit code 0>',
723 data: {
724 item: {
725 command: ["bun", "run", "dev"],
726 result: {
727 content: '{ "dev": "vite dev --port 3000" } <exited with exit code 0>',
728 exitCode: 0,
729 },
730 },
731 },
732 },
733 }),
734 ];
735
736 const [entry] = deriveWorkLogEntries(activities, undefined);
737 expect(entry).toMatchObject({
738 command: "bun run dev",
739 detail: '{ "dev": "vite dev --port 3000" }',
740 itemType: "command_execution",
741 toolTitle: "bash",
742 });
743 });
744
745 it("extracts changed file paths for file-change tool activities", () => {
746 const activities: OrchestrationThreadActivity[] = [
747 makeActivity({
748 id: "file-tool",
749 kind: "tool.completed",
750 summary: "File change",
751 payload: {
752 itemType: "file_change",
753 data: {
754 item: {
755 changes: [
756 { path: "apps/web/src/components/ChatView.tsx" },
757 { filename: "apps/web/src/session-logic.ts" },
758 ],
759 },
760 },
761 },
762 }),
763 ];
764
765 const [entry] = deriveWorkLogEntries(activities, undefined);
766 expect(entry?.changedFiles).toEqual([
767 "apps/web/src/components/ChatView.tsx",
768 "apps/web/src/session-logic.ts",
769 ]);
770 });
771
772 it("collapses repeated lifecycle updates for the same tool call into one entry", () => {
773 const activities: OrchestrationThreadActivity[] = [
774 makeActivity({
775 id: "tool-update-1",
776 createdAt: "2026-02-23T00:00:01.000Z",
777 kind: "tool.updated",
778 summary: "Tool call",
779 payload: {
780 itemType: "dynamic_tool_call",
781 title: "Tool call",
782 detail: 'Read: {"file_path":"/tmp/app.ts"}',
783 },
784 }),
785 makeActivity({
786 id: "tool-update-2",
787 createdAt: "2026-02-23T00:00:02.000Z",
788 kind: "tool.updated",
789 summary: "Tool call",
790 payload: {
791 itemType: "dynamic_tool_call",
792 title: "Tool call",
793 detail: 'Read: {"file_path":"/tmp/app.ts"}',
794 data: {
795 item: {
796 command: ["sed", "-n", "1,40p", "/tmp/app.ts"],
797 },
798 },
799 },
800 }),
801 makeActivity({
802 id: "tool-complete",
803 createdAt: "2026-02-23T00:00:03.000Z",
804 kind: "tool.completed",
805 summary: "Tool call completed",
806 payload: {
807 itemType: "dynamic_tool_call",
808 title: "Tool call",
809 detail: 'Read: {"file_path":"/tmp/app.ts"}',
810 },
811 }),
812 ];
813
814 const entries = deriveWorkLogEntries(activities, undefined);
815
816 expect(entries).toHaveLength(1);
817 expect(entries[0]).toMatchObject({
818 id: "tool-complete",
819 createdAt: "2026-02-23T00:00:03.000Z",
820 label: "Tool call completed",
821 detail: 'Read: {"file_path":"/tmp/app.ts"}',
822 command: "sed -n 1,40p /tmp/app.ts",
823 itemType: "dynamic_tool_call",
824 toolTitle: "Tool call",
825 });
826 });
827
828 it("keeps separate tool entries when an identical call starts after the prior one completed", () => {
829 const activities: OrchestrationThreadActivity[] = [
830 makeActivity({
831 id: "tool-1-update",
832 createdAt: "2026-02-23T00:00:01.000Z",
833 kind: "tool.updated",
834 summary: "Tool call",
835 payload: {
836 itemType: "dynamic_tool_call",
837 title: "Tool call",
838 detail: 'Read: {"file_path":"/tmp/app.ts"}',
839 },
840 }),
841 makeActivity({
842 id: "tool-1-complete",
843 createdAt: "2026-02-23T00:00:02.000Z",
844 kind: "tool.completed",
845 summary: "Tool call completed",
846 payload: {
847 itemType: "dynamic_tool_call",
848 title: "Tool call",
849 detail: 'Read: {"file_path":"/tmp/app.ts"}',
850 },
851 }),
852 makeActivity({
853 id: "tool-2-update",
854 createdAt: "2026-02-23T00:00:03.000Z",
855 kind: "tool.updated",
856 summary: "Tool call",
857 payload: {
858 itemType: "dynamic_tool_call",
859 title: "Tool call",
860 detail: 'Read: {"file_path":"/tmp/app.ts"}',
861 },
862 }),
863 makeActivity({
864 id: "tool-2-complete",
865 createdAt: "2026-02-23T00:00:04.000Z",
866 kind: "tool.completed",
867 summary: "Tool call completed",
868 payload: {
869 itemType: "dynamic_tool_call",
870 title: "Tool call",
871 detail: 'Read: {"file_path":"/tmp/app.ts"}',
872 },
873 }),
874 ];
875
876 const entries = deriveWorkLogEntries(activities, undefined);
877
878 expect(entries.map((entry) => entry.id)).toEqual(["tool-1-complete", "tool-2-complete"]);
879 });
880
881 it("collapses same-timestamp lifecycle rows even when completed sorts before updated by id", () => {
882 const activities: OrchestrationThreadActivity[] = [
883 makeActivity({
884 id: "z-update-earlier",
885 createdAt: "2026-02-23T00:00:01.000Z",
886 kind: "tool.updated",
887 summary: "Tool call",
888 payload: {
889 itemType: "dynamic_tool_call",
890 title: "Tool call",
891 detail: 'Read: {"file_path":"/tmp/app.ts"}',
892 },
893 }),
894 makeActivity({
895 id: "a-complete-same-timestamp",
896 createdAt: "2026-02-23T00:00:02.000Z",
897 kind: "tool.completed",
898 summary: "Tool call",
899 payload: {
900 itemType: "dynamic_tool_call",
901 title: "Tool call",
902 detail: 'Read: {"file_path":"/tmp/app.ts"}',
903 },
904 }),
905 makeActivity({
906 id: "z-update-same-timestamp",
907 createdAt: "2026-02-23T00:00:02.000Z",
908 kind: "tool.updated",
909 summary: "Tool call",
910 payload: {
911 itemType: "dynamic_tool_call",
912 title: "Tool call",
913 detail: 'Read: {"file_path":"/tmp/app.ts"}',
914 },
915 }),
916 ];
917
918 const entries = deriveWorkLogEntries(activities, undefined);
919
920 expect(entries).toHaveLength(1);
921 expect(entries[0]?.id).toBe("a-complete-same-timestamp");
922 });
923});
924
925describe("deriveTimelineEntries", () => {
926 it("includes proposed plans alongside messages and work entries in chronological order", () => {
927 const entries = deriveTimelineEntries(
928 [
929 {
930 id: MessageId.makeUnsafe("message-1"),
931 role: "assistant",
932 text: "hello",
933 createdAt: "2026-02-23T00:00:01.000Z",
934 streaming: false,
935 },
936 ],
937 [
938 {
939 id: "plan:thread-1:turn:turn-1",
940 turnId: TurnId.makeUnsafe("turn-1"),
941 planMarkdown: "# Ship it",
942 implementedAt: null,
943 implementationThreadId: null,
944 createdAt: "2026-02-23T00:00:02.000Z",
945 updatedAt: "2026-02-23T00:00:02.000Z",
946 },
947 ],
948 [
949 {
950 id: "work-1",
951 createdAt: "2026-02-23T00:00:03.000Z",
952 label: "Ran tests",
953 tone: "tool",
954 },
955 ],
956 );
957
958 expect(entries.map((entry) => entry.kind)).toEqual(["message", "proposed-plan", "work"]);
959 expect(entries[1]).toMatchObject({
960 kind: "proposed-plan",
961 proposedPlan: {
962 planMarkdown: "# Ship it",
963 implementedAt: null,
964 implementationThreadId: null,
965 },
966 });
967 });
968
969 it("anchors the completion divider to latestTurn.assistantMessageId before timestamp fallback", () => {
970 const entries = deriveTimelineEntries(
971 [
972 {
973 id: MessageId.makeUnsafe("assistant-earlier"),
974 role: "assistant",
975 text: "progress update",
976 createdAt: "2026-02-23T00:00:01.000Z",
977 streaming: false,
978 },
979 {
980 id: MessageId.makeUnsafe("assistant-final"),
981 role: "assistant",
982 text: "final answer",
983 createdAt: "2026-02-23T00:00:01.000Z",
984 streaming: false,
985 },
986 ],
987 [],
988 [],
989 );
990
991 expect(
992 deriveCompletionDividerBeforeEntryId(entries, {
993 assistantMessageId: MessageId.makeUnsafe("assistant-final"),
994 startedAt: "2026-02-23T00:00:00.000Z",
995 completedAt: "2026-02-23T00:00:02.000Z",
996 }),
997 ).toBe("assistant-final");
998 });
999});
1000
1001describe("deriveWorkLogEntries context window handling", () => {
1002 it("excludes context window updates from the work log", () => {
1003 const entries = deriveWorkLogEntries(
1004 [
1005 makeActivity({
1006 id: "context-1",
1007 turnId: "turn-1",
1008 kind: "context-window.updated",
1009 summary: "Context window updated",
1010 tone: "info",
1011 }),
1012 makeActivity({
1013 id: "tool-1",
1014 turnId: "turn-1",
1015 kind: "tool.completed",
1016 summary: "Ran command",
1017 tone: "tool",
1018 }),
1019 ],
1020 TurnId.makeUnsafe("turn-1"),
1021 );
1022
1023 expect(entries).toHaveLength(1);
1024 expect(entries[0]?.label).toBe("Ran command");
1025 });
1026
1027 it("keeps context compaction activities as normal work log entries", () => {
1028 const entries = deriveWorkLogEntries(
1029 [
1030 makeActivity({
1031 id: "compaction-1",
1032 turnId: "turn-1",
1033 kind: "context-compaction",
1034 summary: "Context compacted",
1035 tone: "info",
1036 }),
1037 ],
1038 TurnId.makeUnsafe("turn-1"),
1039 );
1040
1041 expect(entries).toHaveLength(1);
1042 expect(entries[0]?.label).toBe("Context compacted");
1043 });
1044});
1045
1046describe("hasToolActivityForTurn", () => {
1047 it("returns false when turn id is missing", () => {
1048 const activities: OrchestrationThreadActivity[] = [
1049 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
1050 ];
1051
1052 expect(hasToolActivityForTurn(activities, undefined)).toBe(false);
1053 expect(hasToolActivityForTurn(activities, null)).toBe(false);
1054 });
1055
1056 it("returns true only for matching tool activity in the target turn", () => {
1057 const activities: OrchestrationThreadActivity[] = [
1058 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
1059 makeActivity({ id: "info-1", turnId: "turn-2", kind: "turn.completed", tone: "info" }),
1060 ];
1061
1062 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-1"))).toBe(true);
1063 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-2"))).toBe(false);
1064 });
1065});
1066
1067describe("isLatestTurnSettled", () => {
1068 const latestTurn = {
1069 turnId: TurnId.makeUnsafe("turn-1"),
1070 startedAt: "2026-02-27T21:10:00.000Z",
1071 completedAt: "2026-02-27T21:10:06.000Z",
1072 } as const;
1073
1074 it("returns false while the same turn is still active in a running session", () => {
1075 expect(
1076 isLatestTurnSettled(latestTurn, {
1077 orchestrationStatus: "running",
1078 activeTurnId: TurnId.makeUnsafe("turn-1"),
1079 }),
1080 ).toBe(false);
1081 });
1082
1083 it("returns false while any turn is running to avoid stale latest-turn banners", () => {
1084 expect(
1085 isLatestTurnSettled(latestTurn, {
1086 orchestrationStatus: "running",
1087 activeTurnId: TurnId.makeUnsafe("turn-2"),
1088 }),
1089 ).toBe(false);
1090 });
1091
1092 it("returns true once the session is no longer running that turn", () => {
1093 expect(
1094 isLatestTurnSettled(latestTurn, {
1095 orchestrationStatus: "ready",
1096 activeTurnId: undefined,
1097 }),
1098 ).toBe(true);
1099 });
1100
1101 it("returns false when turn timestamps are incomplete", () => {
1102 expect(
1103 isLatestTurnSettled(
1104 {
1105 turnId: TurnId.makeUnsafe("turn-1"),
1106 startedAt: null,
1107 completedAt: "2026-02-27T21:10:06.000Z",
1108 },
1109 null,
1110 ),
1111 ).toBe(false);
1112 });
1113});
1114
1115describe("deriveActiveWorkStartedAt", () => {
1116 const latestTurn = {
1117 turnId: TurnId.makeUnsafe("turn-1"),
1118 startedAt: "2026-02-27T21:10:00.000Z",
1119 completedAt: "2026-02-27T21:10:06.000Z",
1120 } as const;
1121
1122 it("prefers the in-flight turn start when the latest turn is not settled", () => {
1123 expect(
1124 deriveActiveWorkStartedAt(
1125 latestTurn,
1126 {
1127 orchestrationStatus: "running",
1128 activeTurnId: TurnId.makeUnsafe("turn-1"),
1129 },
1130 "2026-02-27T21:11:00.000Z",
1131 ),
1132 ).toBe("2026-02-27T21:10:00.000Z");
1133 });
1134
1135 it("falls back to sendStartedAt once the latest turn is settled", () => {
1136 expect(
1137 deriveActiveWorkStartedAt(
1138 latestTurn,
1139 {
1140 orchestrationStatus: "ready",
1141 activeTurnId: undefined,
1142 },
1143 "2026-02-27T21:11:00.000Z",
1144 ),
1145 ).toBe("2026-02-27T21:11:00.000Z");
1146 });
1147
1148 it("uses sendStartedAt for a fresh send after the prior turn completed", () => {
1149 expect(
1150 deriveActiveWorkStartedAt(
1151 {
1152 turnId: TurnId.makeUnsafe("turn-1"),
1153 startedAt: "2026-02-27T21:10:00.000Z",
1154 completedAt: "2026-02-27T21:10:06.000Z",
1155 },
1156 null,
1157 "2026-02-27T21:11:00.000Z",
1158 ),
1159 ).toBe("2026-02-27T21:11:00.000Z");
1160 });
1161});
1162
1163describe("PROVIDER_OPTIONS", () => {
1164 it("advertises Claude as available while keeping Cursor as a placeholder", () => {
1165 const claude = PROVIDER_OPTIONS.find((option) => option.value === "claudeAgent");
1166 const cursor = PROVIDER_OPTIONS.find((option) => option.value === "cursor");
1167 expect(PROVIDER_OPTIONS).toEqual([
1168 { value: "codex", label: "Codex", available: true },
1169 { value: "claudeAgent", label: "Claude", available: true },
1170 { value: "cursor", label: "Cursor", available: false },
1171 ]);
1172 expect(claude).toEqual({
1173 value: "claudeAgent",
1174 label: "Claude",
1175 available: true,
1176 });
1177 expect(cursor).toEqual({
1178 value: "cursor",
1179 label: "Cursor",
1180 available: false,
1181 });
1182 });
1183});