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

session-logic.test.ts

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