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

session-logic.test.ts

1,106 lines32.9 KB

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