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.make(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.make(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 multiSelect: true,
201 },
202 ],
203 },
204 }),
205 makeActivity({
206 id: "user-input-resolved",
207 createdAt: "2026-02-23T00:00:02.000Z",
208 kind: "user-input.resolved",
209 summary: "User input submitted",
210 tone: "info",
211 payload: {
212 requestId: "req-user-input-2",
213 answers: {
214 sandbox_mode: "workspace-write",
215 },
216 },
217 }),
218 makeActivity({
219 id: "user-input-open-2",
220 createdAt: "2026-02-23T00:00:01.500Z",
221 kind: "user-input.requested",
222 summary: "User input requested",
223 tone: "info",
224 payload: {
225 requestId: "req-user-input-2",
226 questions: [
227 {
228 id: "approval",
229 header: "Approval",
230 question: "Continue?",
231 options: [
232 {
233 label: "yes",
234 description: "Continue execution",
235 },
236 ],
237 multiSelect: false,
238 },
239 ],
240 },
241 }),
242 ];
243
244 expect(derivePendingUserInputs(activities)).toEqual([
245 {
246 requestId: "req-user-input-1",
247 createdAt: "2026-02-23T00:00:01.000Z",
248 questions: [
249 {
250 id: "sandbox_mode",
251 header: "Sandbox",
252 question: "Which mode should be used?",
253 options: [
254 {
255 label: "workspace-write",
256 description: "Allow workspace writes only",
257 },
258 ],
259 multiSelect: true,
260 },
261 ],
262 },
263 ]);
264 });
265
266 it("clears stale pending user-input prompts when the provider reports an orphaned request", () => {
267 const activities: OrchestrationThreadActivity[] = [
268 makeActivity({
269 id: "user-input-open-stale",
270 createdAt: "2026-02-23T00:00:01.000Z",
271 kind: "user-input.requested",
272 summary: "User input requested",
273 tone: "info",
274 payload: {
275 requestId: "req-user-input-stale-1",
276 questions: [
277 {
278 id: "sandbox_mode",
279 header: "Sandbox",
280 question: "Which mode should be used?",
281 options: [
282 {
283 label: "workspace-write",
284 description: "Allow workspace writes only",
285 },
286 ],
287 multiSelect: false,
288 },
289 ],
290 },
291 }),
292 makeActivity({
293 id: "user-input-failed-stale",
294 createdAt: "2026-02-23T00:00:02.000Z",
295 kind: "provider.user-input.respond.failed",
296 summary: "Provider user input response failed",
297 tone: "error",
298 payload: {
299 requestId: "req-user-input-stale-1",
300 detail:
301 "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.",
302 },
303 }),
304 ];
305
306 expect(derivePendingUserInputs(activities)).toEqual([]);
307 });
308});
309
310describe("deriveActivePlanState", () => {
311 it("returns the latest plan update for the active turn", () => {
312 const activities: OrchestrationThreadActivity[] = [
313 makeActivity({
314 id: "plan-old",
315 createdAt: "2026-02-23T00:00:01.000Z",
316 kind: "turn.plan.updated",
317 summary: "Plan updated",
318 tone: "info",
319 turnId: "turn-1",
320 payload: {
321 explanation: "Initial plan",
322 plan: [{ step: "Inspect code", status: "pending" }],
323 },
324 }),
325 makeActivity({
326 id: "plan-latest",
327 createdAt: "2026-02-23T00:00:02.000Z",
328 kind: "turn.plan.updated",
329 summary: "Plan updated",
330 tone: "info",
331 turnId: "turn-1",
332 payload: {
333 explanation: "Refined plan",
334 plan: [{ step: "Implement Codex user input", status: "inProgress" }],
335 },
336 }),
337 ];
338
339 expect(deriveActivePlanState(activities, TurnId.make("turn-1"))).toEqual({
340 createdAt: "2026-02-23T00:00:02.000Z",
341 turnId: "turn-1",
342 explanation: "Refined plan",
343 steps: [{ step: "Implement Codex user input", status: "inProgress" }],
344 });
345 });
346
347 it("falls back to the most recent plan from a previous turn", () => {
348 const activities: OrchestrationThreadActivity[] = [
349 makeActivity({
350 id: "plan-from-turn-1",
351 createdAt: "2026-02-23T00:00:01.000Z",
352 kind: "turn.plan.updated",
353 summary: "Plan updated",
354 tone: "info",
355 turnId: "turn-1",
356 payload: {
357 plan: [{ step: "Write tests", status: "completed" }],
358 },
359 }),
360 ];
361
362 // Current turn is turn-2, which has no plan activity — should fall back to turn-1's plan
363 const result = deriveActivePlanState(activities, TurnId.make("turn-2"));
364 expect(result).toEqual({
365 createdAt: "2026-02-23T00:00:01.000Z",
366 turnId: "turn-1",
367 steps: [{ step: "Write tests", status: "completed" }],
368 });
369 });
370});
371
372describe("findLatestProposedPlan", () => {
373 it("prefers the latest proposed plan for the active turn", () => {
374 expect(
375 findLatestProposedPlan(
376 [
377 {
378 id: "plan:thread-1:turn:turn-1",
379 turnId: TurnId.make("turn-1"),
380 planMarkdown: "# Older",
381 implementedAt: null,
382 implementationThreadId: null,
383 createdAt: "2026-02-23T00:00:01.000Z",
384 updatedAt: "2026-02-23T00:00:01.000Z",
385 },
386 {
387 id: "plan:thread-1:turn:turn-1",
388 turnId: TurnId.make("turn-1"),
389 planMarkdown: "# Latest",
390 implementedAt: null,
391 implementationThreadId: null,
392 createdAt: "2026-02-23T00:00:01.000Z",
393 updatedAt: "2026-02-23T00:00:02.000Z",
394 },
395 {
396 id: "plan:thread-1:turn:turn-2",
397 turnId: TurnId.make("turn-2"),
398 planMarkdown: "# Different turn",
399 implementedAt: null,
400 implementationThreadId: null,
401 createdAt: "2026-02-23T00:00:03.000Z",
402 updatedAt: "2026-02-23T00:00:03.000Z",
403 },
404 ],
405 TurnId.make("turn-1"),
406 ),
407 ).toEqual({
408 id: "plan:thread-1:turn:turn-1",
409 turnId: "turn-1",
410 planMarkdown: "# Latest",
411 implementedAt: null,
412 implementationThreadId: null,
413 createdAt: "2026-02-23T00:00:01.000Z",
414 updatedAt: "2026-02-23T00:00:02.000Z",
415 });
416 });
417
418 it("falls back to the most recently updated proposed plan", () => {
419 const latestPlan = findLatestProposedPlan(
420 [
421 {
422 id: "plan:thread-1:turn:turn-1",
423 turnId: TurnId.make("turn-1"),
424 planMarkdown: "# First",
425 implementedAt: null,
426 implementationThreadId: null,
427 createdAt: "2026-02-23T00:00:01.000Z",
428 updatedAt: "2026-02-23T00:00:01.000Z",
429 },
430 {
431 id: "plan:thread-1:turn:turn-2",
432 turnId: TurnId.make("turn-2"),
433 planMarkdown: "# Latest",
434 implementedAt: null,
435 implementationThreadId: null,
436 createdAt: "2026-02-23T00:00:02.000Z",
437 updatedAt: "2026-02-23T00:00:03.000Z",
438 },
439 ],
440 null,
441 );
442
443 expect(latestPlan?.planMarkdown).toBe("# Latest");
444 });
445});
446
447describe("hasActionableProposedPlan", () => {
448 it("returns true for an unimplemented proposed plan", () => {
449 expect(
450 hasActionableProposedPlan({
451 id: "plan-1",
452 turnId: TurnId.make("turn-1"),
453 planMarkdown: "# Plan",
454 implementedAt: null,
455 implementationThreadId: null,
456 createdAt: "2026-02-23T00:00:00.000Z",
457 updatedAt: "2026-02-23T00:00:01.000Z",
458 }),
459 ).toBe(true);
460 });
461
462 it("returns false for a proposed plan already implemented elsewhere", () => {
463 expect(
464 hasActionableProposedPlan({
465 id: "plan-1",
466 turnId: TurnId.make("turn-1"),
467 planMarkdown: "# Plan",
468 implementedAt: "2026-02-23T00:00:02.000Z",
469 implementationThreadId: ThreadId.make("thread-implement"),
470 createdAt: "2026-02-23T00:00:00.000Z",
471 updatedAt: "2026-02-23T00:00:02.000Z",
472 }),
473 ).toBe(false);
474 });
475});
476
477describe("findSidebarProposedPlan", () => {
478 it("prefers the running turn source proposed plan when available on the same thread", () => {
479 expect(
480 findSidebarProposedPlan({
481 threads: [
482 {
483 id: ThreadId.make("thread-1"),
484 proposedPlans: [
485 {
486 id: "plan-1",
487 turnId: TurnId.make("turn-plan"),
488 planMarkdown: "# Source plan",
489 implementedAt: "2026-02-23T00:00:03.000Z",
490 implementationThreadId: ThreadId.make("thread-2"),
491 createdAt: "2026-02-23T00:00:01.000Z",
492 updatedAt: "2026-02-23T00:00:02.000Z",
493 },
494 ],
495 },
496 {
497 id: ThreadId.make("thread-2"),
498 proposedPlans: [
499 {
500 id: "plan-2",
501 turnId: TurnId.make("turn-other"),
502 planMarkdown: "# Latest elsewhere",
503 implementedAt: null,
504 implementationThreadId: null,
505 createdAt: "2026-02-23T00:00:04.000Z",
506 updatedAt: "2026-02-23T00:00:05.000Z",
507 },
508 ],
509 },
510 ],
511 latestTurn: {
512 turnId: TurnId.make("turn-implementation"),
513 sourceProposedPlan: {
514 threadId: ThreadId.make("thread-1"),
515 planId: "plan-1",
516 },
517 },
518 latestTurnSettled: false,
519 threadId: ThreadId.make("thread-1"),
520 }),
521 ).toEqual({
522 id: "plan-1",
523 turnId: "turn-plan",
524 planMarkdown: "# Source plan",
525 implementedAt: "2026-02-23T00:00:03.000Z",
526 implementationThreadId: "thread-2",
527 createdAt: "2026-02-23T00:00:01.000Z",
528 updatedAt: "2026-02-23T00:00:02.000Z",
529 });
530 });
531
532 it("falls back to the latest proposed plan once the turn is settled", () => {
533 expect(
534 findSidebarProposedPlan({
535 threads: [
536 {
537 id: ThreadId.make("thread-1"),
538 proposedPlans: [
539 {
540 id: "plan-1",
541 turnId: TurnId.make("turn-plan"),
542 planMarkdown: "# Older",
543 implementedAt: null,
544 implementationThreadId: null,
545 createdAt: "2026-02-23T00:00:01.000Z",
546 updatedAt: "2026-02-23T00:00:02.000Z",
547 },
548 {
549 id: "plan-2",
550 turnId: TurnId.make("turn-latest"),
551 planMarkdown: "# Latest",
552 implementedAt: null,
553 implementationThreadId: null,
554 createdAt: "2026-02-23T00:00:03.000Z",
555 updatedAt: "2026-02-23T00:00:04.000Z",
556 },
557 ],
558 },
559 ],
560 latestTurn: {
561 turnId: TurnId.make("turn-implementation"),
562 sourceProposedPlan: {
563 threadId: ThreadId.make("thread-1"),
564 planId: "plan-1",
565 },
566 },
567 latestTurnSettled: true,
568 threadId: ThreadId.make("thread-1"),
569 })?.planMarkdown,
570 ).toBe("# Latest");
571 });
572});
573
574describe("deriveWorkLogEntries", () => {
575 it("omits tool started entries and keeps completed entries", () => {
576 const activities: OrchestrationThreadActivity[] = [
577 makeActivity({
578 id: "tool-complete",
579 createdAt: "2026-02-23T00:00:03.000Z",
580 summary: "Tool call complete",
581 kind: "tool.completed",
582 }),
583 makeActivity({
584 id: "tool-start",
585 createdAt: "2026-02-23T00:00:02.000Z",
586 summary: "Tool call",
587 kind: "tool.started",
588 }),
589 ];
590
591 const entries = deriveWorkLogEntries(activities, undefined);
592 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
593 });
594
595 it("omits task.started but shows task.progress and task.completed", () => {
596 const activities: OrchestrationThreadActivity[] = [
597 makeActivity({
598 id: "task-start",
599 createdAt: "2026-02-23T00:00:01.000Z",
600 kind: "task.started",
601 summary: "default task started",
602 tone: "info",
603 }),
604 makeActivity({
605 id: "task-progress",
606 createdAt: "2026-02-23T00:00:02.000Z",
607 kind: "task.progress",
608 summary: "Updating files",
609 tone: "info",
610 }),
611 makeActivity({
612 id: "task-complete",
613 createdAt: "2026-02-23T00:00:03.000Z",
614 kind: "task.completed",
615 summary: "Task completed",
616 tone: "info",
617 }),
618 ];
619
620 const entries = deriveWorkLogEntries(activities, undefined);
621 expect(entries.map((entry) => entry.id)).toEqual(["task-progress", "task-complete"]);
622 });
623
624 it("uses payload summary as label for task entries when available", () => {
625 const activities: OrchestrationThreadActivity[] = [
626 makeActivity({
627 id: "task-progress-with-summary",
628 createdAt: "2026-02-23T00:00:02.000Z",
629 kind: "task.progress",
630 summary: "Reasoning update",
631 tone: "info",
632 payload: { summary: "Searching for API endpoints" },
633 }),
634 ];
635
636 const entries = deriveWorkLogEntries(activities, undefined);
637 expect(entries[0]?.label).toBe("Searching for API endpoints");
638 });
639
640 it("uses payload detail as label for task.completed and preserves error tone", () => {
641 const activities: OrchestrationThreadActivity[] = [
642 makeActivity({
643 id: "task-completed-failed",
644 createdAt: "2026-02-23T00:00:03.000Z",
645 kind: "task.completed",
646 summary: "Task failed",
647 tone: "error",
648 payload: { detail: "Failed to deploy changes" },
649 }),
650 ];
651
652 const entries = deriveWorkLogEntries(activities, undefined);
653 expect(entries[0]?.label).toBe("Failed to deploy changes");
654 expect(entries[0]?.tone).toBe("error");
655 });
656
657 it("filters by turn id when provided", () => {
658 const activities: OrchestrationThreadActivity[] = [
659 makeActivity({ id: "turn-1", turnId: "turn-1", summary: "Tool call", kind: "tool.started" }),
660 makeActivity({
661 id: "turn-2",
662 turnId: "turn-2",
663 summary: "Tool call complete",
664 kind: "tool.completed",
665 }),
666 makeActivity({ id: "no-turn", summary: "Checkpoint captured", tone: "info" }),
667 ];
668
669 const entries = deriveWorkLogEntries(activities, TurnId.make("turn-2"));
670 expect(entries.map((entry) => entry.id)).toEqual(["turn-2"]);
671 });
672
673 it("omits checkpoint captured info entries", () => {
674 const activities: OrchestrationThreadActivity[] = [
675 makeActivity({
676 id: "checkpoint",
677 createdAt: "2026-02-23T00:00:01.000Z",
678 summary: "Checkpoint captured",
679 tone: "info",
680 }),
681 makeActivity({
682 id: "tool-complete",
683 createdAt: "2026-02-23T00:00:02.000Z",
684 summary: "Ran command",
685 tone: "tool",
686 kind: "tool.completed",
687 }),
688 ];
689
690 const entries = deriveWorkLogEntries(activities, undefined);
691 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
692 });
693
694 it("omits ExitPlanMode lifecycle entries once the plan card is shown", () => {
695 const activities: OrchestrationThreadActivity[] = [
696 makeActivity({
697 id: "exit-plan-updated",
698 createdAt: "2026-02-23T00:00:01.000Z",
699 kind: "tool.updated",
700 summary: "Tool call",
701 payload: {
702 detail: 'ExitPlanMode: {"allowedPrompts":[{"tool":"Bash","prompt":"run tests"}]}',
703 },
704 }),
705 makeActivity({
706 id: "exit-plan-completed",
707 createdAt: "2026-02-23T00:00:02.000Z",
708 kind: "tool.completed",
709 summary: "Tool call",
710 payload: {
711 detail: "ExitPlanMode: {}",
712 },
713 }),
714 makeActivity({
715 id: "real-work-log",
716 createdAt: "2026-02-23T00:00:03.000Z",
717 kind: "tool.completed",
718 summary: "Ran command",
719 payload: {
720 itemType: "command_execution",
721 detail: "Bash: bun test",
722 },
723 }),
724 ];
725
726 const entries = deriveWorkLogEntries(activities, undefined);
727 expect(entries.map((entry) => entry.id)).toEqual(["real-work-log"]);
728 });
729
730 it("orders work log by activity sequence when present", () => {
731 const activities: OrchestrationThreadActivity[] = [
732 makeActivity({
733 id: "second",
734 createdAt: "2026-02-23T00:00:03.000Z",
735 sequence: 2,
736 summary: "Tool call complete",
737 kind: "tool.completed",
738 }),
739 makeActivity({
740 id: "first",
741 createdAt: "2026-02-23T00:00:04.000Z",
742 sequence: 1,
743 summary: "Tool call complete",
744 kind: "tool.completed",
745 }),
746 ];
747
748 const entries = deriveWorkLogEntries(activities, undefined);
749 expect(entries.map((entry) => entry.id)).toEqual(["first", "second"]);
750 });
751
752 it("extracts command text for command tool activities", () => {
753 const activities: OrchestrationThreadActivity[] = [
754 makeActivity({
755 id: "command-tool",
756 kind: "tool.completed",
757 summary: "Ran command",
758 payload: {
759 itemType: "command_execution",
760 data: {
761 item: {
762 command: ["bun", "run", "lint"],
763 },
764 },
765 },
766 }),
767 ];
768
769 const [entry] = deriveWorkLogEntries(activities, undefined);
770 expect(entry?.command).toBe("bun run lint");
771 });
772
773 it("unwraps PowerShell command wrappers for displayed command text", () => {
774 const activities: OrchestrationThreadActivity[] = [
775 makeActivity({
776 id: "command-tool-windows-wrapper",
777 kind: "tool.completed",
778 summary: "Ran command",
779 payload: {
780 itemType: "command_execution",
781 data: {
782 item: {
783 command: "\"C:\\Program Files\\PowerShell\\7\\pwsh.exe\" -Command 'bun run lint'",
784 },
785 },
786 },
787 }),
788 ];
789
790 const [entry] = deriveWorkLogEntries(activities, undefined);
791 expect(entry?.command).toBe("bun run lint");
792 expect(entry?.rawCommand).toBe(
793 "\"C:\\Program Files\\PowerShell\\7\\pwsh.exe\" -Command 'bun run lint'",
794 );
795 });
796
797 it("unwraps PowerShell command wrappers from argv-style command payloads", () => {
798 const activities: OrchestrationThreadActivity[] = [
799 makeActivity({
800 id: "command-tool-windows-wrapper-argv",
801 kind: "tool.completed",
802 summary: "Ran command",
803 payload: {
804 itemType: "command_execution",
805 data: {
806 item: {
807 command: ["C:\\Program Files\\PowerShell\\7\\pwsh.exe", "-Command", "rg -n foo ."],
808 },
809 },
810 },
811 }),
812 ];
813
814 const [entry] = deriveWorkLogEntries(activities, undefined);
815 expect(entry?.command).toBe("rg -n foo .");
816 expect(entry?.rawCommand).toBe(
817 '"C:\\Program Files\\PowerShell\\7\\pwsh.exe" -Command "rg -n foo ."',
818 );
819 });
820
821 it("extracts command text from command detail when structured command metadata is missing", () => {
822 const activities: OrchestrationThreadActivity[] = [
823 makeActivity({
824 id: "command-tool-windows-detail-fallback",
825 kind: "tool.completed",
826 summary: "Ran command",
827 payload: {
828 itemType: "command_execution",
829 detail:
830 '"C:\\Program Files\\PowerShell\\7\\pwsh.exe" -NoLogo -NoProfile -Command \'rg -n -F "new Date()" .\' <exited with exit code 0>',
831 },
832 }),
833 ];
834
835 const [entry] = deriveWorkLogEntries(activities, undefined);
836 expect(entry?.command).toBe('rg -n -F "new Date()" .');
837 expect(entry?.rawCommand).toBe(
838 `"C:\\Program Files\\PowerShell\\7\\pwsh.exe" -NoLogo -NoProfile -Command 'rg -n -F "new Date()" .'`,
839 );
840 });
841
842 it("does not unwrap shell commands when no wrapper flag is present", () => {
843 const activities: OrchestrationThreadActivity[] = [
844 makeActivity({
845 id: "command-tool-shell-script",
846 kind: "tool.completed",
847 summary: "Ran command",
848 payload: {
849 itemType: "command_execution",
850 data: {
851 item: {
852 command: "bash script.sh",
853 },
854 },
855 },
856 }),
857 ];
858
859 const [entry] = deriveWorkLogEntries(activities, undefined);
860 expect(entry?.command).toBe("bash script.sh");
861 expect(entry?.rawCommand).toBeUndefined();
862 });
863
864 it("keeps compact Codex tool metadata used for icons and labels", () => {
865 const activities: OrchestrationThreadActivity[] = [
866 makeActivity({
867 id: "tool-with-metadata",
868 kind: "tool.completed",
869 summary: "bash",
870 payload: {
871 itemType: "command_execution",
872 title: "bash",
873 status: "completed",
874 detail: '{ "dev": "vite dev --port 3000" } <exited with exit code 0>',
875 data: {
876 item: {
877 command: ["bun", "run", "dev"],
878 result: {
879 content: '{ "dev": "vite dev --port 3000" } <exited with exit code 0>',
880 exitCode: 0,
881 },
882 },
883 },
884 },
885 }),
886 ];
887
888 const [entry] = deriveWorkLogEntries(activities, undefined);
889 expect(entry).toMatchObject({
890 command: "bun run dev",
891 detail: '{ "dev": "vite dev --port 3000" }',
892 itemType: "command_execution",
893 toolTitle: "bash",
894 });
895 });
896
897 it("extracts changed file paths for file-change tool activities", () => {
898 const activities: OrchestrationThreadActivity[] = [
899 makeActivity({
900 id: "file-tool",
901 kind: "tool.completed",
902 summary: "File change",
903 payload: {
904 itemType: "file_change",
905 data: {
906 item: {
907 changes: [
908 { path: "apps/web/src/components/ChatView.tsx" },
909 { filename: "apps/web/src/session-logic.ts" },
910 ],
911 },
912 },
913 },
914 }),
915 ];
916
917 const [entry] = deriveWorkLogEntries(activities, undefined);
918 expect(entry?.changedFiles).toEqual([
919 "apps/web/src/components/ChatView.tsx",
920 "apps/web/src/session-logic.ts",
921 ]);
922 });
923
924 it("collapses repeated lifecycle updates for the same tool call into one entry", () => {
925 const activities: OrchestrationThreadActivity[] = [
926 makeActivity({
927 id: "tool-update-1",
928 createdAt: "2026-02-23T00:00:01.000Z",
929 kind: "tool.updated",
930 summary: "Tool call",
931 payload: {
932 itemType: "dynamic_tool_call",
933 title: "Tool call",
934 detail: 'Read: {"file_path":"/tmp/app.ts"}',
935 },
936 }),
937 makeActivity({
938 id: "tool-update-2",
939 createdAt: "2026-02-23T00:00:02.000Z",
940 kind: "tool.updated",
941 summary: "Tool call",
942 payload: {
943 itemType: "dynamic_tool_call",
944 title: "Tool call",
945 detail: 'Read: {"file_path":"/tmp/app.ts"}',
946 data: {
947 item: {
948 command: ["sed", "-n", "1,40p", "/tmp/app.ts"],
949 },
950 },
951 },
952 }),
953 makeActivity({
954 id: "tool-complete",
955 createdAt: "2026-02-23T00:00:03.000Z",
956 kind: "tool.completed",
957 summary: "Tool call completed",
958 payload: {
959 itemType: "dynamic_tool_call",
960 title: "Tool call",
961 detail: 'Read: {"file_path":"/tmp/app.ts"}',
962 },
963 }),
964 ];
965
966 const entries = deriveWorkLogEntries(activities, undefined);
967
968 expect(entries).toHaveLength(1);
969 expect(entries[0]).toMatchObject({
970 id: "tool-complete",
971 createdAt: "2026-02-23T00:00:03.000Z",
972 label: "Tool call completed",
973 detail: 'Read: {"file_path":"/tmp/app.ts"}',
974 command: "sed -n 1,40p /tmp/app.ts",
975 itemType: "dynamic_tool_call",
976 toolTitle: "Tool call",
977 });
978 });
979
980 it("keeps separate tool entries when an identical call starts after the prior one completed", () => {
981 const activities: OrchestrationThreadActivity[] = [
982 makeActivity({
983 id: "tool-1-update",
984 createdAt: "2026-02-23T00:00:01.000Z",
985 kind: "tool.updated",
986 summary: "Tool call",
987 payload: {
988 itemType: "dynamic_tool_call",
989 title: "Tool call",
990 detail: 'Read: {"file_path":"/tmp/app.ts"}',
991 },
992 }),
993 makeActivity({
994 id: "tool-1-complete",
995 createdAt: "2026-02-23T00:00:02.000Z",
996 kind: "tool.completed",
997 summary: "Tool call completed",
998 payload: {
999 itemType: "dynamic_tool_call",
1000 title: "Tool call",
1001 detail: 'Read: {"file_path":"/tmp/app.ts"}',
1002 },
1003 }),
1004 makeActivity({
1005 id: "tool-2-update",
1006 createdAt: "2026-02-23T00:00:03.000Z",
1007 kind: "tool.updated",
1008 summary: "Tool call",
1009 payload: {
1010 itemType: "dynamic_tool_call",
1011 title: "Tool call",
1012 detail: 'Read: {"file_path":"/tmp/app.ts"}',
1013 },
1014 }),
1015 makeActivity({
1016 id: "tool-2-complete",
1017 createdAt: "2026-02-23T00:00:04.000Z",
1018 kind: "tool.completed",
1019 summary: "Tool call completed",
1020 payload: {
1021 itemType: "dynamic_tool_call",
1022 title: "Tool call",
1023 detail: 'Read: {"file_path":"/tmp/app.ts"}',
1024 },
1025 }),
1026 ];
1027
1028 const entries = deriveWorkLogEntries(activities, undefined);
1029
1030 expect(entries.map((entry) => entry.id)).toEqual(["tool-1-complete", "tool-2-complete"]);
1031 });
1032
1033 it("collapses same-timestamp lifecycle rows even when completed sorts before updated by id", () => {
1034 const activities: OrchestrationThreadActivity[] = [
1035 makeActivity({
1036 id: "z-update-earlier",
1037 createdAt: "2026-02-23T00:00:01.000Z",
1038 kind: "tool.updated",
1039 summary: "Tool call",
1040 payload: {
1041 itemType: "dynamic_tool_call",
1042 title: "Tool call",
1043 detail: 'Read: {"file_path":"/tmp/app.ts"}',
1044 },
1045 }),
1046 makeActivity({
1047 id: "a-complete-same-timestamp",
1048 createdAt: "2026-02-23T00:00:02.000Z",
1049 kind: "tool.completed",
1050 summary: "Tool call",
1051 payload: {
1052 itemType: "dynamic_tool_call",
1053 title: "Tool call",
1054 detail: 'Read: {"file_path":"/tmp/app.ts"}',
1055 },
1056 }),
1057 makeActivity({
1058 id: "z-update-same-timestamp",
1059 createdAt: "2026-02-23T00:00:02.000Z",
1060 kind: "tool.updated",
1061 summary: "Tool call",
1062 payload: {
1063 itemType: "dynamic_tool_call",
1064 title: "Tool call",
1065 detail: 'Read: {"file_path":"/tmp/app.ts"}',
1066 },
1067 }),
1068 ];
1069
1070 const entries = deriveWorkLogEntries(activities, undefined);
1071
1072 expect(entries).toHaveLength(1);
1073 expect(entries[0]?.id).toBe("a-complete-same-timestamp");
1074 });
1075});
1076
1077describe("deriveTimelineEntries", () => {
1078 it("includes proposed plans alongside messages and work entries in chronological order", () => {
1079 const entries = deriveTimelineEntries(
1080 [
1081 {
1082 id: MessageId.make("message-1"),
1083 role: "assistant",
1084 text: "hello",
1085 createdAt: "2026-02-23T00:00:01.000Z",
1086 streaming: false,
1087 },
1088 ],
1089 [
1090 {
1091 id: "plan:thread-1:turn:turn-1",
1092 turnId: TurnId.make("turn-1"),
1093 planMarkdown: "# Ship it",
1094 implementedAt: null,
1095 implementationThreadId: null,
1096 createdAt: "2026-02-23T00:00:02.000Z",
1097 updatedAt: "2026-02-23T00:00:02.000Z",
1098 },
1099 ],
1100 [
1101 {
1102 id: "work-1",
1103 createdAt: "2026-02-23T00:00:03.000Z",
1104 label: "Ran tests",
1105 tone: "tool",
1106 },
1107 ],
1108 );
1109
1110 expect(entries.map((entry) => entry.kind)).toEqual(["message", "proposed-plan", "work"]);
1111 expect(entries[1]).toMatchObject({
1112 kind: "proposed-plan",
1113 proposedPlan: {
1114 planMarkdown: "# Ship it",
1115 implementedAt: null,
1116 implementationThreadId: null,
1117 },
1118 });
1119 });
1120
1121 it("anchors the completion divider to latestTurn.assistantMessageId before timestamp fallback", () => {
1122 const entries = deriveTimelineEntries(
1123 [
1124 {
1125 id: MessageId.make("assistant-earlier"),
1126 role: "assistant",
1127 text: "progress update",
1128 createdAt: "2026-02-23T00:00:01.000Z",
1129 streaming: false,
1130 },
1131 {
1132 id: MessageId.make("assistant-final"),
1133 role: "assistant",
1134 text: "final answer",
1135 createdAt: "2026-02-23T00:00:01.000Z",
1136 streaming: false,
1137 },
1138 ],
1139 [],
1140 [],
1141 );
1142
1143 expect(
1144 deriveCompletionDividerBeforeEntryId(entries, {
1145 assistantMessageId: MessageId.make("assistant-final"),
1146 startedAt: "2026-02-23T00:00:00.000Z",
1147 completedAt: "2026-02-23T00:00:02.000Z",
1148 }),
1149 ).toBe("assistant-final");
1150 });
1151});
1152
1153describe("deriveWorkLogEntries context window handling", () => {
1154 it("excludes context window updates from the work log", () => {
1155 const entries = deriveWorkLogEntries(
1156 [
1157 makeActivity({
1158 id: "context-1",
1159 turnId: "turn-1",
1160 kind: "context-window.updated",
1161 summary: "Context window updated",
1162 tone: "info",
1163 }),
1164 makeActivity({
1165 id: "tool-1",
1166 turnId: "turn-1",
1167 kind: "tool.completed",
1168 summary: "Ran command",
1169 tone: "tool",
1170 }),
1171 ],
1172 TurnId.make("turn-1"),
1173 );
1174
1175 expect(entries).toHaveLength(1);
1176 expect(entries[0]?.label).toBe("Ran command");
1177 });
1178
1179 it("keeps context compaction activities as normal work log entries", () => {
1180 const entries = deriveWorkLogEntries(
1181 [
1182 makeActivity({
1183 id: "compaction-1",
1184 turnId: "turn-1",
1185 kind: "context-compaction",
1186 summary: "Context compacted",
1187 tone: "info",
1188 }),
1189 ],
1190 TurnId.make("turn-1"),
1191 );
1192
1193 expect(entries).toHaveLength(1);
1194 expect(entries[0]?.label).toBe("Context compacted");
1195 });
1196});
1197
1198describe("hasToolActivityForTurn", () => {
1199 it("returns false when turn id is missing", () => {
1200 const activities: OrchestrationThreadActivity[] = [
1201 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
1202 ];
1203
1204 expect(hasToolActivityForTurn(activities, undefined)).toBe(false);
1205 expect(hasToolActivityForTurn(activities, null)).toBe(false);
1206 });
1207
1208 it("returns true only for matching tool activity in the target turn", () => {
1209 const activities: OrchestrationThreadActivity[] = [
1210 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
1211 makeActivity({ id: "info-1", turnId: "turn-2", kind: "turn.completed", tone: "info" }),
1212 ];
1213
1214 expect(hasToolActivityForTurn(activities, TurnId.make("turn-1"))).toBe(true);
1215 expect(hasToolActivityForTurn(activities, TurnId.make("turn-2"))).toBe(false);
1216 });
1217});
1218
1219describe("isLatestTurnSettled", () => {
1220 const latestTurn = {
1221 turnId: TurnId.make("turn-1"),
1222 startedAt: "2026-02-27T21:10:00.000Z",
1223 completedAt: "2026-02-27T21:10:06.000Z",
1224 } as const;
1225
1226 it("returns false while the same turn is still active in a running session", () => {
1227 expect(
1228 isLatestTurnSettled(latestTurn, {
1229 orchestrationStatus: "running",
1230 activeTurnId: TurnId.make("turn-1"),
1231 }),
1232 ).toBe(false);
1233 });
1234
1235 it("returns false while any turn is running to avoid stale latest-turn banners", () => {
1236 expect(
1237 isLatestTurnSettled(latestTurn, {
1238 orchestrationStatus: "running",
1239 activeTurnId: TurnId.make("turn-2"),
1240 }),
1241 ).toBe(false);
1242 });
1243
1244 it("returns true once the session is no longer running that turn", () => {
1245 expect(
1246 isLatestTurnSettled(latestTurn, {
1247 orchestrationStatus: "ready",
1248 activeTurnId: undefined,
1249 }),
1250 ).toBe(true);
1251 });
1252
1253 it("returns false when turn timestamps are incomplete", () => {
1254 expect(
1255 isLatestTurnSettled(
1256 {
1257 turnId: TurnId.make("turn-1"),
1258 startedAt: null,
1259 completedAt: "2026-02-27T21:10:06.000Z",
1260 },
1261 null,
1262 ),
1263 ).toBe(false);
1264 });
1265});
1266
1267describe("deriveActiveWorkStartedAt", () => {
1268 const latestTurn = {
1269 turnId: TurnId.make("turn-1"),
1270 startedAt: "2026-02-27T21:10:00.000Z",
1271 completedAt: "2026-02-27T21:10:06.000Z",
1272 } as const;
1273
1274 it("prefers the in-flight turn start when the latest turn is not settled", () => {
1275 expect(
1276 deriveActiveWorkStartedAt(
1277 latestTurn,
1278 {
1279 orchestrationStatus: "running",
1280 activeTurnId: TurnId.make("turn-1"),
1281 },
1282 "2026-02-27T21:11:00.000Z",
1283 ),
1284 ).toBe("2026-02-27T21:10:00.000Z");
1285 });
1286
1287 it("uses the new send start while the session is running a different turn", () => {
1288 expect(
1289 deriveActiveWorkStartedAt(
1290 latestTurn,
1291 {
1292 orchestrationStatus: "running",
1293 activeTurnId: TurnId.make("turn-2"),
1294 },
1295 "2026-02-27T21:11:00.000Z",
1296 ),
1297 ).toBe("2026-02-27T21:11:00.000Z");
1298 });
1299
1300 it("falls back to sendStartedAt once the latest turn is settled", () => {
1301 expect(
1302 deriveActiveWorkStartedAt(
1303 latestTurn,
1304 {
1305 orchestrationStatus: "ready",
1306 activeTurnId: undefined,
1307 },
1308 "2026-02-27T21:11:00.000Z",
1309 ),
1310 ).toBe("2026-02-27T21:11:00.000Z");
1311 });
1312
1313 it("uses sendStartedAt for a fresh send after the prior turn completed", () => {
1314 expect(
1315 deriveActiveWorkStartedAt(
1316 {
1317 turnId: TurnId.make("turn-1"),
1318 startedAt: "2026-02-27T21:10:00.000Z",
1319 completedAt: "2026-02-27T21:10:06.000Z",
1320 },
1321 null,
1322 "2026-02-27T21:11:00.000Z",
1323 ),
1324 ).toBe("2026-02-27T21:11:00.000Z");
1325 });
1326});
1327
1328describe("PROVIDER_OPTIONS", () => {
1329 it("advertises Claude as available while keeping Cursor as a placeholder", () => {
1330 const claude = PROVIDER_OPTIONS.find((option) => option.value === "claudeAgent");
1331 const cursor = PROVIDER_OPTIONS.find((option) => option.value === "cursor");
1332 expect(PROVIDER_OPTIONS).toEqual([
1333 { value: "codex", label: "Codex", available: true },
1334 { value: "claudeAgent", label: "Claude", available: true },
1335 { value: "cursor", label: "Cursor", available: false },
1336 ]);
1337 expect(claude).toEqual({
1338 value: "claudeAgent",
1339 label: "Claude",
1340 available: true,
1341 });
1342 expect(cursor).toEqual({
1343 value: "cursor",
1344 label: "Cursor",
1345 available: false,
1346 });
1347 });
1348});