Open History to see the last commit
1import { EventId, MessageId, TurnId, type OrchestrationThreadActivity } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3
4import {
5 deriveActiveWorkStartedAt,
6 deriveActivePlanState,
7 PROVIDER_OPTIONS,
8 derivePendingApprovals,
9 derivePendingUserInputs,
10 deriveTimelineEntries,
11 deriveWorkLogEntries,
12 findLatestProposedPlan,
13 hasToolActivityForTurn,
14 isLatestTurnSettled,
15} from "./session-logic";
16
17function makeActivity(overrides: {
18 id?: string;
19 createdAt?: string;
20 kind?: string;
21 summary?: string;
22 tone?: OrchestrationThreadActivity["tone"];
23 payload?: Record<string, unknown>;
24 turnId?: string;
25 sequence?: number;
26}): OrchestrationThreadActivity {
27 const payload = overrides.payload ?? {};
28 return {
29 id: EventId.makeUnsafe(overrides.id ?? crypto.randomUUID()),
30 createdAt: overrides.createdAt ?? "2026-02-23T00:00:00.000Z",
31 kind: overrides.kind ?? "tool.started",
32 summary: overrides.summary ?? "Tool call",
33 tone: overrides.tone ?? "tool",
34 payload,
35 turnId: overrides.turnId ? TurnId.makeUnsafe(overrides.turnId) : null,
36 ...(overrides.sequence !== undefined ? { sequence: overrides.sequence } : {}),
37 };
38}
39
40describe("derivePendingApprovals", () => {
41 it("tracks open approvals and removes resolved ones", () => {
42 const activities: OrchestrationThreadActivity[] = [
43 makeActivity({
44 id: "approval-open",
45 createdAt: "2026-02-23T00:00:01.000Z",
46 kind: "approval.requested",
47 summary: "Command approval requested",
48 tone: "approval",
49 payload: {
50 requestId: "req-1",
51 requestKind: "command",
52 detail: "bun run lint",
53 },
54 }),
55 makeActivity({
56 id: "approval-close",
57 createdAt: "2026-02-23T00:00:02.000Z",
58 kind: "approval.resolved",
59 summary: "Approval resolved",
60 tone: "info",
61 payload: { requestId: "req-2" },
62 }),
63 makeActivity({
64 id: "approval-closed-request",
65 createdAt: "2026-02-23T00:00:01.500Z",
66 kind: "approval.requested",
67 summary: "File-change approval requested",
68 tone: "approval",
69 payload: { requestId: "req-2", requestKind: "file-change" },
70 }),
71 ];
72
73 expect(derivePendingApprovals(activities)).toEqual([
74 {
75 requestId: "req-1",
76 requestKind: "command",
77 createdAt: "2026-02-23T00:00:01.000Z",
78 detail: "bun run lint",
79 },
80 ]);
81 });
82
83 it("maps canonical requestType payloads into pending approvals", () => {
84 const activities: OrchestrationThreadActivity[] = [
85 makeActivity({
86 id: "approval-open-request-type",
87 createdAt: "2026-02-23T00:00:01.000Z",
88 kind: "approval.requested",
89 summary: "Command approval requested",
90 tone: "approval",
91 payload: {
92 requestId: "req-request-type",
93 requestType: "command_execution_approval",
94 detail: "pwd",
95 },
96 }),
97 ];
98
99 expect(derivePendingApprovals(activities)).toEqual([
100 {
101 requestId: "req-request-type",
102 requestKind: "command",
103 createdAt: "2026-02-23T00:00:01.000Z",
104 detail: "pwd",
105 },
106 ]);
107 });
108
109 it("clears stale pending approvals when provider reports unknown pending request", () => {
110 const activities: OrchestrationThreadActivity[] = [
111 makeActivity({
112 id: "approval-open-stale",
113 createdAt: "2026-02-23T00:00:01.000Z",
114 kind: "approval.requested",
115 summary: "Command approval requested",
116 tone: "approval",
117 payload: {
118 requestId: "req-stale-1",
119 requestKind: "command",
120 },
121 }),
122 makeActivity({
123 id: "approval-failed-stale",
124 createdAt: "2026-02-23T00:00:02.000Z",
125 kind: "provider.approval.respond.failed",
126 summary: "Provider approval response failed",
127 tone: "error",
128 payload: {
129 requestId: "req-stale-1",
130 detail: "Unknown pending permission request: req-stale-1",
131 },
132 }),
133 ];
134
135 expect(derivePendingApprovals(activities)).toEqual([]);
136 });
137});
138
139describe("derivePendingUserInputs", () => {
140 it("tracks open structured prompts and removes resolved ones", () => {
141 const activities: OrchestrationThreadActivity[] = [
142 makeActivity({
143 id: "user-input-open",
144 createdAt: "2026-02-23T00:00:01.000Z",
145 kind: "user-input.requested",
146 summary: "User input requested",
147 tone: "info",
148 payload: {
149 requestId: "req-user-input-1",
150 questions: [
151 {
152 id: "sandbox_mode",
153 header: "Sandbox",
154 question: "Which mode should be used?",
155 options: [
156 {
157 label: "workspace-write",
158 description: "Allow workspace writes only",
159 },
160 ],
161 },
162 ],
163 },
164 }),
165 makeActivity({
166 id: "user-input-resolved",
167 createdAt: "2026-02-23T00:00:02.000Z",
168 kind: "user-input.resolved",
169 summary: "User input submitted",
170 tone: "info",
171 payload: {
172 requestId: "req-user-input-2",
173 answers: {
174 sandbox_mode: "workspace-write",
175 },
176 },
177 }),
178 makeActivity({
179 id: "user-input-open-2",
180 createdAt: "2026-02-23T00:00:01.500Z",
181 kind: "user-input.requested",
182 summary: "User input requested",
183 tone: "info",
184 payload: {
185 requestId: "req-user-input-2",
186 questions: [
187 {
188 id: "approval",
189 header: "Approval",
190 question: "Continue?",
191 options: [
192 {
193 label: "yes",
194 description: "Continue execution",
195 },
196 ],
197 },
198 ],
199 },
200 }),
201 ];
202
203 expect(derivePendingUserInputs(activities)).toEqual([
204 {
205 requestId: "req-user-input-1",
206 createdAt: "2026-02-23T00:00:01.000Z",
207 questions: [
208 {
209 id: "sandbox_mode",
210 header: "Sandbox",
211 question: "Which mode should be used?",
212 options: [
213 {
214 label: "workspace-write",
215 description: "Allow workspace writes only",
216 },
217 ],
218 },
219 ],
220 },
221 ]);
222 });
223});
224
225describe("deriveActivePlanState", () => {
226 it("returns the latest plan update for the active turn", () => {
227 const activities: OrchestrationThreadActivity[] = [
228 makeActivity({
229 id: "plan-old",
230 createdAt: "2026-02-23T00:00:01.000Z",
231 kind: "turn.plan.updated",
232 summary: "Plan updated",
233 tone: "info",
234 turnId: "turn-1",
235 payload: {
236 explanation: "Initial plan",
237 plan: [{ step: "Inspect code", status: "pending" }],
238 },
239 }),
240 makeActivity({
241 id: "plan-latest",
242 createdAt: "2026-02-23T00:00:02.000Z",
243 kind: "turn.plan.updated",
244 summary: "Plan updated",
245 tone: "info",
246 turnId: "turn-1",
247 payload: {
248 explanation: "Refined plan",
249 plan: [{ step: "Implement Codex user input", status: "inProgress" }],
250 },
251 }),
252 ];
253
254 expect(deriveActivePlanState(activities, TurnId.makeUnsafe("turn-1"))).toEqual({
255 createdAt: "2026-02-23T00:00:02.000Z",
256 turnId: "turn-1",
257 explanation: "Refined plan",
258 steps: [{ step: "Implement Codex user input", status: "inProgress" }],
259 });
260 });
261});
262
263describe("findLatestProposedPlan", () => {
264 it("prefers the latest proposed plan for the active turn", () => {
265 expect(
266 findLatestProposedPlan(
267 [
268 {
269 id: "plan:thread-1:turn:turn-1",
270 turnId: TurnId.makeUnsafe("turn-1"),
271 planMarkdown: "# Older",
272 createdAt: "2026-02-23T00:00:01.000Z",
273 updatedAt: "2026-02-23T00:00:01.000Z",
274 },
275 {
276 id: "plan:thread-1:turn:turn-1",
277 turnId: TurnId.makeUnsafe("turn-1"),
278 planMarkdown: "# Latest",
279 createdAt: "2026-02-23T00:00:01.000Z",
280 updatedAt: "2026-02-23T00:00:02.000Z",
281 },
282 {
283 id: "plan:thread-1:turn:turn-2",
284 turnId: TurnId.makeUnsafe("turn-2"),
285 planMarkdown: "# Different turn",
286 createdAt: "2026-02-23T00:00:03.000Z",
287 updatedAt: "2026-02-23T00:00:03.000Z",
288 },
289 ],
290 TurnId.makeUnsafe("turn-1"),
291 ),
292 ).toEqual({
293 id: "plan:thread-1:turn:turn-1",
294 turnId: "turn-1",
295 planMarkdown: "# Latest",
296 createdAt: "2026-02-23T00:00:01.000Z",
297 updatedAt: "2026-02-23T00:00:02.000Z",
298 });
299 });
300
301 it("falls back to the most recently updated proposed plan", () => {
302 const latestPlan = findLatestProposedPlan(
303 [
304 {
305 id: "plan:thread-1:turn:turn-1",
306 turnId: TurnId.makeUnsafe("turn-1"),
307 planMarkdown: "# First",
308 createdAt: "2026-02-23T00:00:01.000Z",
309 updatedAt: "2026-02-23T00:00:01.000Z",
310 },
311 {
312 id: "plan:thread-1:turn:turn-2",
313 turnId: TurnId.makeUnsafe("turn-2"),
314 planMarkdown: "# Latest",
315 createdAt: "2026-02-23T00:00:02.000Z",
316 updatedAt: "2026-02-23T00:00:03.000Z",
317 },
318 ],
319 null,
320 );
321
322 expect(latestPlan?.planMarkdown).toBe("# Latest");
323 });
324});
325
326describe("deriveWorkLogEntries", () => {
327 it("omits tool started entries and keeps completed entries", () => {
328 const activities: OrchestrationThreadActivity[] = [
329 makeActivity({
330 id: "tool-complete",
331 createdAt: "2026-02-23T00:00:03.000Z",
332 summary: "Tool call complete",
333 kind: "tool.completed",
334 }),
335 makeActivity({
336 id: "tool-start",
337 createdAt: "2026-02-23T00:00:02.000Z",
338 summary: "Tool call",
339 kind: "tool.started",
340 }),
341 ];
342
343 const entries = deriveWorkLogEntries(activities, undefined);
344 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
345 });
346
347 it("omits task start and completion lifecycle entries", () => {
348 const activities: OrchestrationThreadActivity[] = [
349 makeActivity({
350 id: "task-start",
351 createdAt: "2026-02-23T00:00:01.000Z",
352 kind: "task.started",
353 summary: "default task started",
354 tone: "info",
355 }),
356 makeActivity({
357 id: "task-progress",
358 createdAt: "2026-02-23T00:00:02.000Z",
359 kind: "task.progress",
360 summary: "Updating files",
361 tone: "info",
362 }),
363 makeActivity({
364 id: "task-complete",
365 createdAt: "2026-02-23T00:00:03.000Z",
366 kind: "task.completed",
367 summary: "Task completed",
368 tone: "info",
369 }),
370 ];
371
372 const entries = deriveWorkLogEntries(activities, undefined);
373 expect(entries.map((entry) => entry.id)).toEqual(["task-progress"]);
374 });
375
376 it("filters by turn id when provided", () => {
377 const activities: OrchestrationThreadActivity[] = [
378 makeActivity({ id: "turn-1", turnId: "turn-1", summary: "Tool call", kind: "tool.started" }),
379 makeActivity({
380 id: "turn-2",
381 turnId: "turn-2",
382 summary: "Tool call complete",
383 kind: "tool.completed",
384 }),
385 makeActivity({ id: "no-turn", summary: "Checkpoint captured", tone: "info" }),
386 ];
387
388 const entries = deriveWorkLogEntries(activities, TurnId.makeUnsafe("turn-2"));
389 expect(entries.map((entry) => entry.id)).toEqual(["turn-2"]);
390 });
391
392 it("omits checkpoint captured info entries", () => {
393 const activities: OrchestrationThreadActivity[] = [
394 makeActivity({
395 id: "checkpoint",
396 createdAt: "2026-02-23T00:00:01.000Z",
397 summary: "Checkpoint captured",
398 tone: "info",
399 }),
400 makeActivity({
401 id: "tool-complete",
402 createdAt: "2026-02-23T00:00:02.000Z",
403 summary: "Ran command",
404 tone: "tool",
405 kind: "tool.completed",
406 }),
407 ];
408
409 const entries = deriveWorkLogEntries(activities, undefined);
410 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
411 });
412
413 it("orders work log by activity sequence when present", () => {
414 const activities: OrchestrationThreadActivity[] = [
415 makeActivity({
416 id: "second",
417 createdAt: "2026-02-23T00:00:03.000Z",
418 sequence: 2,
419 summary: "Tool call complete",
420 kind: "tool.completed",
421 }),
422 makeActivity({
423 id: "first",
424 createdAt: "2026-02-23T00:00:04.000Z",
425 sequence: 1,
426 summary: "Tool call complete",
427 kind: "tool.completed",
428 }),
429 ];
430
431 const entries = deriveWorkLogEntries(activities, undefined);
432 expect(entries.map((entry) => entry.id)).toEqual(["first", "second"]);
433 });
434
435 it("extracts command text for command tool activities", () => {
436 const activities: OrchestrationThreadActivity[] = [
437 makeActivity({
438 id: "command-tool",
439 kind: "tool.completed",
440 summary: "Ran command",
441 payload: {
442 itemType: "command_execution",
443 data: {
444 item: {
445 command: ["bun", "run", "lint"],
446 },
447 },
448 },
449 }),
450 ];
451
452 const [entry] = deriveWorkLogEntries(activities, undefined);
453 expect(entry?.command).toBe("bun run lint");
454 });
455
456 it("keeps compact Codex tool metadata used for icons and labels", () => {
457 const activities: OrchestrationThreadActivity[] = [
458 makeActivity({
459 id: "tool-with-metadata",
460 kind: "tool.completed",
461 summary: "bash",
462 payload: {
463 itemType: "command_execution",
464 title: "bash",
465 status: "completed",
466 detail: '{ "dev": "vite dev --port 3000" } <exited with exit code 0>',
467 data: {
468 item: {
469 command: ["bun", "run", "dev"],
470 result: {
471 content: '{ "dev": "vite dev --port 3000" } <exited with exit code 0>',
472 exitCode: 0,
473 },
474 },
475 },
476 },
477 }),
478 ];
479
480 const [entry] = deriveWorkLogEntries(activities, undefined);
481 expect(entry).toMatchObject({
482 command: "bun run dev",
483 detail: '{ "dev": "vite dev --port 3000" }',
484 itemType: "command_execution",
485 toolTitle: "bash",
486 });
487 });
488
489 it("extracts changed file paths for file-change tool activities", () => {
490 const activities: OrchestrationThreadActivity[] = [
491 makeActivity({
492 id: "file-tool",
493 kind: "tool.completed",
494 summary: "File change",
495 payload: {
496 itemType: "file_change",
497 data: {
498 item: {
499 changes: [
500 { path: "apps/web/src/components/ChatView.tsx" },
501 { filename: "apps/web/src/session-logic.ts" },
502 ],
503 },
504 },
505 },
506 }),
507 ];
508
509 const [entry] = deriveWorkLogEntries(activities, undefined);
510 expect(entry?.changedFiles).toEqual([
511 "apps/web/src/components/ChatView.tsx",
512 "apps/web/src/session-logic.ts",
513 ]);
514 });
515});
516
517describe("deriveTimelineEntries", () => {
518 it("includes proposed plans alongside messages and work entries in chronological order", () => {
519 const entries = deriveTimelineEntries(
520 [
521 {
522 id: MessageId.makeUnsafe("message-1"),
523 role: "assistant",
524 text: "hello",
525 createdAt: "2026-02-23T00:00:01.000Z",
526 streaming: false,
527 },
528 ],
529 [
530 {
531 id: "plan:thread-1:turn:turn-1",
532 turnId: TurnId.makeUnsafe("turn-1"),
533 planMarkdown: "# Ship it",
534 createdAt: "2026-02-23T00:00:02.000Z",
535 updatedAt: "2026-02-23T00:00:02.000Z",
536 },
537 ],
538 [
539 {
540 id: "work-1",
541 createdAt: "2026-02-23T00:00:03.000Z",
542 label: "Ran tests",
543 tone: "tool",
544 },
545 ],
546 );
547
548 expect(entries.map((entry) => entry.kind)).toEqual(["message", "proposed-plan", "work"]);
549 expect(entries[1]).toMatchObject({
550 kind: "proposed-plan",
551 proposedPlan: {
552 planMarkdown: "# Ship it",
553 },
554 });
555 });
556});
557
558describe("hasToolActivityForTurn", () => {
559 it("returns false when turn id is missing", () => {
560 const activities: OrchestrationThreadActivity[] = [
561 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
562 ];
563
564 expect(hasToolActivityForTurn(activities, undefined)).toBe(false);
565 expect(hasToolActivityForTurn(activities, null)).toBe(false);
566 });
567
568 it("returns true only for matching tool activity in the target turn", () => {
569 const activities: OrchestrationThreadActivity[] = [
570 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
571 makeActivity({ id: "info-1", turnId: "turn-2", kind: "turn.completed", tone: "info" }),
572 ];
573
574 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-1"))).toBe(true);
575 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-2"))).toBe(false);
576 });
577});
578
579describe("isLatestTurnSettled", () => {
580 const latestTurn = {
581 turnId: TurnId.makeUnsafe("turn-1"),
582 startedAt: "2026-02-27T21:10:00.000Z",
583 completedAt: "2026-02-27T21:10:06.000Z",
584 } as const;
585
586 it("returns false while the same turn is still active in a running session", () => {
587 expect(
588 isLatestTurnSettled(latestTurn, {
589 orchestrationStatus: "running",
590 activeTurnId: TurnId.makeUnsafe("turn-1"),
591 }),
592 ).toBe(false);
593 });
594
595 it("returns false while any turn is running to avoid stale latest-turn banners", () => {
596 expect(
597 isLatestTurnSettled(latestTurn, {
598 orchestrationStatus: "running",
599 activeTurnId: TurnId.makeUnsafe("turn-2"),
600 }),
601 ).toBe(false);
602 });
603
604 it("returns true once the session is no longer running that turn", () => {
605 expect(
606 isLatestTurnSettled(latestTurn, {
607 orchestrationStatus: "ready",
608 activeTurnId: undefined,
609 }),
610 ).toBe(true);
611 });
612
613 it("returns false when turn timestamps are incomplete", () => {
614 expect(
615 isLatestTurnSettled(
616 {
617 turnId: TurnId.makeUnsafe("turn-1"),
618 startedAt: null,
619 completedAt: "2026-02-27T21:10:06.000Z",
620 },
621 null,
622 ),
623 ).toBe(false);
624 });
625});
626
627describe("deriveActiveWorkStartedAt", () => {
628 const latestTurn = {
629 turnId: TurnId.makeUnsafe("turn-1"),
630 startedAt: "2026-02-27T21:10:00.000Z",
631 completedAt: "2026-02-27T21:10:06.000Z",
632 } as const;
633
634 it("prefers the in-flight turn start when the latest turn is not settled", () => {
635 expect(
636 deriveActiveWorkStartedAt(
637 latestTurn,
638 {
639 orchestrationStatus: "running",
640 activeTurnId: TurnId.makeUnsafe("turn-1"),
641 },
642 "2026-02-27T21:11:00.000Z",
643 ),
644 ).toBe("2026-02-27T21:10:00.000Z");
645 });
646
647 it("falls back to sendStartedAt once the latest turn is settled", () => {
648 expect(
649 deriveActiveWorkStartedAt(
650 latestTurn,
651 {
652 orchestrationStatus: "ready",
653 activeTurnId: undefined,
654 },
655 "2026-02-27T21:11:00.000Z",
656 ),
657 ).toBe("2026-02-27T21:11:00.000Z");
658 });
659
660 it("uses sendStartedAt for a fresh send after the prior turn completed", () => {
661 expect(
662 deriveActiveWorkStartedAt(
663 {
664 turnId: TurnId.makeUnsafe("turn-1"),
665 startedAt: "2026-02-27T21:10:00.000Z",
666 completedAt: "2026-02-27T21:10:06.000Z",
667 },
668 null,
669 "2026-02-27T21:11:00.000Z",
670 ),
671 ).toBe("2026-02-27T21:11:00.000Z");
672 });
673});
674
675describe("PROVIDER_OPTIONS", () => {
676 it("keeps Claude Code and Cursor visible as unavailable placeholders in the stack base", () => {
677 const claude = PROVIDER_OPTIONS.find((option) => option.value === "claudeCode");
678 const cursor = PROVIDER_OPTIONS.find((option) => option.value === "cursor");
679 expect(PROVIDER_OPTIONS).toEqual([
680 { value: "codex", label: "Codex", available: true },
681 { value: "claudeCode", label: "Claude Code", available: false },
682 { value: "cursor", label: "Cursor", available: false },
683 ]);
684 expect(claude).toEqual({
685 value: "claudeCode",
686 label: "Claude Code",
687 available: false,
688 });
689 expect(cursor).toEqual({
690 value: "cursor",
691 label: "Cursor",
692 available: false,
693 });
694 });
695});