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: "Command run complete",
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: "Command run complete",
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("extracts changed file paths for file-change tool activities", () => {
457 const activities: OrchestrationThreadActivity[] = [
458 makeActivity({
459 id: "file-tool",
460 kind: "tool.completed",
461 summary: "File change complete",
462 payload: {
463 itemType: "file_change",
464 data: {
465 item: {
466 changes: [
467 { path: "apps/web/src/components/ChatView.tsx" },
468 { filename: "apps/web/src/session-logic.ts" },
469 ],
470 },
471 },
472 },
473 }),
474 ];
475
476 const [entry] = deriveWorkLogEntries(activities, undefined);
477 expect(entry?.changedFiles).toEqual([
478 "apps/web/src/components/ChatView.tsx",
479 "apps/web/src/session-logic.ts",
480 ]);
481 });
482});
483
484describe("deriveTimelineEntries", () => {
485 it("includes proposed plans alongside messages and work entries in chronological order", () => {
486 const entries = deriveTimelineEntries(
487 [
488 {
489 id: MessageId.makeUnsafe("message-1"),
490 role: "assistant",
491 text: "hello",
492 createdAt: "2026-02-23T00:00:01.000Z",
493 streaming: false,
494 },
495 ],
496 [
497 {
498 id: "plan:thread-1:turn:turn-1",
499 turnId: TurnId.makeUnsafe("turn-1"),
500 planMarkdown: "# Ship it",
501 createdAt: "2026-02-23T00:00:02.000Z",
502 updatedAt: "2026-02-23T00:00:02.000Z",
503 },
504 ],
505 [
506 {
507 id: "work-1",
508 createdAt: "2026-02-23T00:00:03.000Z",
509 label: "Ran tests",
510 tone: "tool",
511 },
512 ],
513 );
514
515 expect(entries.map((entry) => entry.kind)).toEqual(["message", "proposed-plan", "work"]);
516 expect(entries[1]).toMatchObject({
517 kind: "proposed-plan",
518 proposedPlan: {
519 planMarkdown: "# Ship it",
520 },
521 });
522 });
523});
524
525describe("hasToolActivityForTurn", () => {
526 it("returns false when turn id is missing", () => {
527 const activities: OrchestrationThreadActivity[] = [
528 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
529 ];
530
531 expect(hasToolActivityForTurn(activities, undefined)).toBe(false);
532 expect(hasToolActivityForTurn(activities, null)).toBe(false);
533 });
534
535 it("returns true only for matching tool activity in the target turn", () => {
536 const activities: OrchestrationThreadActivity[] = [
537 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
538 makeActivity({ id: "info-1", turnId: "turn-2", kind: "turn.completed", tone: "info" }),
539 ];
540
541 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-1"))).toBe(true);
542 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-2"))).toBe(false);
543 });
544});
545
546describe("isLatestTurnSettled", () => {
547 const latestTurn = {
548 turnId: TurnId.makeUnsafe("turn-1"),
549 startedAt: "2026-02-27T21:10:00.000Z",
550 completedAt: "2026-02-27T21:10:06.000Z",
551 } as const;
552
553 it("returns false while the same turn is still active in a running session", () => {
554 expect(
555 isLatestTurnSettled(latestTurn, {
556 orchestrationStatus: "running",
557 activeTurnId: TurnId.makeUnsafe("turn-1"),
558 }),
559 ).toBe(false);
560 });
561
562 it("returns false while any turn is running to avoid stale latest-turn banners", () => {
563 expect(
564 isLatestTurnSettled(latestTurn, {
565 orchestrationStatus: "running",
566 activeTurnId: TurnId.makeUnsafe("turn-2"),
567 }),
568 ).toBe(false);
569 });
570
571 it("returns true once the session is no longer running that turn", () => {
572 expect(
573 isLatestTurnSettled(latestTurn, {
574 orchestrationStatus: "ready",
575 activeTurnId: undefined,
576 }),
577 ).toBe(true);
578 });
579
580 it("returns false when turn timestamps are incomplete", () => {
581 expect(
582 isLatestTurnSettled(
583 {
584 turnId: TurnId.makeUnsafe("turn-1"),
585 startedAt: null,
586 completedAt: "2026-02-27T21:10:06.000Z",
587 },
588 null,
589 ),
590 ).toBe(false);
591 });
592});
593
594describe("deriveActiveWorkStartedAt", () => {
595 const latestTurn = {
596 turnId: TurnId.makeUnsafe("turn-1"),
597 startedAt: "2026-02-27T21:10:00.000Z",
598 completedAt: "2026-02-27T21:10:06.000Z",
599 } as const;
600
601 it("prefers the in-flight turn start when the latest turn is not settled", () => {
602 expect(
603 deriveActiveWorkStartedAt(
604 latestTurn,
605 {
606 orchestrationStatus: "running",
607 activeTurnId: TurnId.makeUnsafe("turn-1"),
608 },
609 "2026-02-27T21:11:00.000Z",
610 ),
611 ).toBe("2026-02-27T21:10:00.000Z");
612 });
613
614 it("falls back to sendStartedAt once the latest turn is settled", () => {
615 expect(
616 deriveActiveWorkStartedAt(
617 latestTurn,
618 {
619 orchestrationStatus: "ready",
620 activeTurnId: undefined,
621 },
622 "2026-02-27T21:11:00.000Z",
623 ),
624 ).toBe("2026-02-27T21:11:00.000Z");
625 });
626
627 it("uses sendStartedAt for a fresh send after the prior turn completed", () => {
628 expect(
629 deriveActiveWorkStartedAt(
630 {
631 turnId: TurnId.makeUnsafe("turn-1"),
632 startedAt: "2026-02-27T21:10:00.000Z",
633 completedAt: "2026-02-27T21:10:06.000Z",
634 },
635 null,
636 "2026-02-27T21:11:00.000Z",
637 ),
638 ).toBe("2026-02-27T21:11:00.000Z");
639 });
640});
641
642describe("PROVIDER_OPTIONS", () => {
643 it("keeps Claude Code and Cursor visible as unavailable placeholders in the stack base", () => {
644 const claude = PROVIDER_OPTIONS.find((option) => option.value === "claudeCode");
645 const cursor = PROVIDER_OPTIONS.find((option) => option.value === "cursor");
646 expect(PROVIDER_OPTIONS).toEqual([
647 { value: "codex", label: "Codex", available: true },
648 { value: "claudeCode", label: "Claude Code", available: false },
649 { value: "cursor", label: "Cursor", available: false },
650 ]);
651 expect(claude).toEqual({
652 value: "claudeCode",
653 label: "Claude Code",
654 available: false,
655 });
656 expect(cursor).toEqual({
657 value: "cursor",
658 label: "Cursor",
659 available: false,
660 });
661 });
662});