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 deriveActivePlanState,
6 PROVIDER_OPTIONS,
7 derivePendingApprovals,
8 derivePendingUserInputs,
9 deriveTimelineEntries,
10 deriveWorkLogEntries,
11 findLatestProposedPlan,
12 hasToolActivityForTurn,
13 isLatestTurnSettled,
14} from "./session-logic";
15
16function makeActivity(overrides: {
17 id?: string;
18 createdAt?: string;
19 kind?: string;
20 summary?: string;
21 tone?: OrchestrationThreadActivity["tone"];
22 payload?: Record<string, unknown>;
23 turnId?: string;
24 sequence?: number;
25}): OrchestrationThreadActivity {
26 const payload = overrides.payload ?? {};
27 return {
28 id: EventId.makeUnsafe(overrides.id ?? crypto.randomUUID()),
29 createdAt: overrides.createdAt ?? "2026-02-23T00:00:00.000Z",
30 kind: overrides.kind ?? "tool.started",
31 summary: overrides.summary ?? "Tool call",
32 tone: overrides.tone ?? "tool",
33 payload,
34 turnId: overrides.turnId ? TurnId.makeUnsafe(overrides.turnId) : null,
35 ...(overrides.sequence !== undefined ? { sequence: overrides.sequence } : {}),
36 };
37}
38
39describe("derivePendingApprovals", () => {
40 it("tracks open approvals and removes resolved ones", () => {
41 const activities: OrchestrationThreadActivity[] = [
42 makeActivity({
43 id: "approval-open",
44 createdAt: "2026-02-23T00:00:01.000Z",
45 kind: "approval.requested",
46 summary: "Command approval requested",
47 tone: "approval",
48 payload: {
49 requestId: "req-1",
50 requestKind: "command",
51 detail: "bun run lint",
52 },
53 }),
54 makeActivity({
55 id: "approval-close",
56 createdAt: "2026-02-23T00:00:02.000Z",
57 kind: "approval.resolved",
58 summary: "Approval resolved",
59 tone: "info",
60 payload: { requestId: "req-2" },
61 }),
62 makeActivity({
63 id: "approval-closed-request",
64 createdAt: "2026-02-23T00:00:01.500Z",
65 kind: "approval.requested",
66 summary: "File-change approval requested",
67 tone: "approval",
68 payload: { requestId: "req-2", requestKind: "file-change" },
69 }),
70 ];
71
72 expect(derivePendingApprovals(activities)).toEqual([
73 {
74 requestId: "req-1",
75 requestKind: "command",
76 createdAt: "2026-02-23T00:00:01.000Z",
77 detail: "bun run lint",
78 },
79 ]);
80 });
81
82 it("maps canonical requestType payloads into pending approvals", () => {
83 const activities: OrchestrationThreadActivity[] = [
84 makeActivity({
85 id: "approval-open-request-type",
86 createdAt: "2026-02-23T00:00:01.000Z",
87 kind: "approval.requested",
88 summary: "Command approval requested",
89 tone: "approval",
90 payload: {
91 requestId: "req-request-type",
92 requestType: "command_execution_approval",
93 detail: "pwd",
94 },
95 }),
96 ];
97
98 expect(derivePendingApprovals(activities)).toEqual([
99 {
100 requestId: "req-request-type",
101 requestKind: "command",
102 createdAt: "2026-02-23T00:00:01.000Z",
103 detail: "pwd",
104 },
105 ]);
106 });
107
108 it("clears stale pending approvals when provider reports unknown pending request", () => {
109 const activities: OrchestrationThreadActivity[] = [
110 makeActivity({
111 id: "approval-open-stale",
112 createdAt: "2026-02-23T00:00:01.000Z",
113 kind: "approval.requested",
114 summary: "Command approval requested",
115 tone: "approval",
116 payload: {
117 requestId: "req-stale-1",
118 requestKind: "command",
119 },
120 }),
121 makeActivity({
122 id: "approval-failed-stale",
123 createdAt: "2026-02-23T00:00:02.000Z",
124 kind: "provider.approval.respond.failed",
125 summary: "Provider approval response failed",
126 tone: "error",
127 payload: {
128 requestId: "req-stale-1",
129 detail: "Unknown pending permission request: req-stale-1",
130 },
131 }),
132 ];
133
134 expect(derivePendingApprovals(activities)).toEqual([]);
135 });
136});
137
138describe("derivePendingUserInputs", () => {
139 it("tracks open structured prompts and removes resolved ones", () => {
140 const activities: OrchestrationThreadActivity[] = [
141 makeActivity({
142 id: "user-input-open",
143 createdAt: "2026-02-23T00:00:01.000Z",
144 kind: "user-input.requested",
145 summary: "User input requested",
146 tone: "info",
147 payload: {
148 requestId: "req-user-input-1",
149 questions: [
150 {
151 id: "sandbox_mode",
152 header: "Sandbox",
153 question: "Which mode should be used?",
154 options: [
155 {
156 label: "workspace-write",
157 description: "Allow workspace writes only",
158 },
159 ],
160 },
161 ],
162 },
163 }),
164 makeActivity({
165 id: "user-input-resolved",
166 createdAt: "2026-02-23T00:00:02.000Z",
167 kind: "user-input.resolved",
168 summary: "User input submitted",
169 tone: "info",
170 payload: {
171 requestId: "req-user-input-2",
172 answers: {
173 sandbox_mode: "workspace-write",
174 },
175 },
176 }),
177 makeActivity({
178 id: "user-input-open-2",
179 createdAt: "2026-02-23T00:00:01.500Z",
180 kind: "user-input.requested",
181 summary: "User input requested",
182 tone: "info",
183 payload: {
184 requestId: "req-user-input-2",
185 questions: [
186 {
187 id: "approval",
188 header: "Approval",
189 question: "Continue?",
190 options: [
191 {
192 label: "yes",
193 description: "Continue execution",
194 },
195 ],
196 },
197 ],
198 },
199 }),
200 ];
201
202 expect(derivePendingUserInputs(activities)).toEqual([
203 {
204 requestId: "req-user-input-1",
205 createdAt: "2026-02-23T00:00:01.000Z",
206 questions: [
207 {
208 id: "sandbox_mode",
209 header: "Sandbox",
210 question: "Which mode should be used?",
211 options: [
212 {
213 label: "workspace-write",
214 description: "Allow workspace writes only",
215 },
216 ],
217 },
218 ],
219 },
220 ]);
221 });
222});
223
224describe("deriveActivePlanState", () => {
225 it("returns the latest plan update for the active turn", () => {
226 const activities: OrchestrationThreadActivity[] = [
227 makeActivity({
228 id: "plan-old",
229 createdAt: "2026-02-23T00:00:01.000Z",
230 kind: "turn.plan.updated",
231 summary: "Plan updated",
232 tone: "info",
233 turnId: "turn-1",
234 payload: {
235 explanation: "Initial plan",
236 plan: [{ step: "Inspect code", status: "pending" }],
237 },
238 }),
239 makeActivity({
240 id: "plan-latest",
241 createdAt: "2026-02-23T00:00:02.000Z",
242 kind: "turn.plan.updated",
243 summary: "Plan updated",
244 tone: "info",
245 turnId: "turn-1",
246 payload: {
247 explanation: "Refined plan",
248 plan: [{ step: "Implement Codex user input", status: "inProgress" }],
249 },
250 }),
251 ];
252
253 expect(deriveActivePlanState(activities, TurnId.makeUnsafe("turn-1"))).toEqual({
254 createdAt: "2026-02-23T00:00:02.000Z",
255 turnId: "turn-1",
256 explanation: "Refined plan",
257 steps: [{ step: "Implement Codex user input", status: "inProgress" }],
258 });
259 });
260});
261
262describe("findLatestProposedPlan", () => {
263 it("prefers the latest proposed plan for the active turn", () => {
264 expect(
265 findLatestProposedPlan(
266 [
267 {
268 id: "plan:thread-1:turn:turn-1",
269 turnId: TurnId.makeUnsafe("turn-1"),
270 planMarkdown: "# Older",
271 createdAt: "2026-02-23T00:00:01.000Z",
272 updatedAt: "2026-02-23T00:00:01.000Z",
273 },
274 {
275 id: "plan:thread-1:turn:turn-1",
276 turnId: TurnId.makeUnsafe("turn-1"),
277 planMarkdown: "# Latest",
278 createdAt: "2026-02-23T00:00:01.000Z",
279 updatedAt: "2026-02-23T00:00:02.000Z",
280 },
281 {
282 id: "plan:thread-1:turn:turn-2",
283 turnId: TurnId.makeUnsafe("turn-2"),
284 planMarkdown: "# Different turn",
285 createdAt: "2026-02-23T00:00:03.000Z",
286 updatedAt: "2026-02-23T00:00:03.000Z",
287 },
288 ],
289 TurnId.makeUnsafe("turn-1"),
290 ),
291 ).toEqual({
292 id: "plan:thread-1:turn:turn-1",
293 turnId: "turn-1",
294 planMarkdown: "# Latest",
295 createdAt: "2026-02-23T00:00:01.000Z",
296 updatedAt: "2026-02-23T00:00:02.000Z",
297 });
298 });
299
300 it("falls back to the most recently updated proposed plan", () => {
301 const latestPlan = findLatestProposedPlan(
302 [
303 {
304 id: "plan:thread-1:turn:turn-1",
305 turnId: TurnId.makeUnsafe("turn-1"),
306 planMarkdown: "# First",
307 createdAt: "2026-02-23T00:00:01.000Z",
308 updatedAt: "2026-02-23T00:00:01.000Z",
309 },
310 {
311 id: "plan:thread-1:turn:turn-2",
312 turnId: TurnId.makeUnsafe("turn-2"),
313 planMarkdown: "# Latest",
314 createdAt: "2026-02-23T00:00:02.000Z",
315 updatedAt: "2026-02-23T00:00:03.000Z",
316 },
317 ],
318 null,
319 );
320
321 expect(latestPlan?.planMarkdown).toBe("# Latest");
322 });
323});
324
325describe("deriveWorkLogEntries", () => {
326 it("omits tool started entries and keeps completed entries", () => {
327 const activities: OrchestrationThreadActivity[] = [
328 makeActivity({
329 id: "tool-complete",
330 createdAt: "2026-02-23T00:00:03.000Z",
331 summary: "Tool call complete",
332 kind: "tool.completed",
333 }),
334 makeActivity({
335 id: "tool-start",
336 createdAt: "2026-02-23T00:00:02.000Z",
337 summary: "Tool call",
338 kind: "tool.started",
339 }),
340 ];
341
342 const entries = deriveWorkLogEntries(activities, undefined);
343 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
344 });
345
346 it("filters by turn id when provided", () => {
347 const activities: OrchestrationThreadActivity[] = [
348 makeActivity({ id: "turn-1", turnId: "turn-1", summary: "Tool call", kind: "tool.started" }),
349 makeActivity({
350 id: "turn-2",
351 turnId: "turn-2",
352 summary: "Tool call complete",
353 kind: "tool.completed",
354 }),
355 makeActivity({ id: "no-turn", summary: "Checkpoint captured", tone: "info" }),
356 ];
357
358 const entries = deriveWorkLogEntries(activities, TurnId.makeUnsafe("turn-2"));
359 expect(entries.map((entry) => entry.id)).toEqual(["turn-2"]);
360 });
361
362 it("omits checkpoint captured info entries", () => {
363 const activities: OrchestrationThreadActivity[] = [
364 makeActivity({
365 id: "checkpoint",
366 createdAt: "2026-02-23T00:00:01.000Z",
367 summary: "Checkpoint captured",
368 tone: "info",
369 }),
370 makeActivity({
371 id: "tool-complete",
372 createdAt: "2026-02-23T00:00:02.000Z",
373 summary: "Command run complete",
374 tone: "tool",
375 kind: "tool.completed",
376 }),
377 ];
378
379 const entries = deriveWorkLogEntries(activities, undefined);
380 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
381 });
382
383 it("orders work log by activity sequence when present", () => {
384 const activities: OrchestrationThreadActivity[] = [
385 makeActivity({
386 id: "second",
387 createdAt: "2026-02-23T00:00:03.000Z",
388 sequence: 2,
389 summary: "Tool call complete",
390 kind: "tool.completed",
391 }),
392 makeActivity({
393 id: "first",
394 createdAt: "2026-02-23T00:00:04.000Z",
395 sequence: 1,
396 summary: "Tool call complete",
397 kind: "tool.completed",
398 }),
399 ];
400
401 const entries = deriveWorkLogEntries(activities, undefined);
402 expect(entries.map((entry) => entry.id)).toEqual(["first", "second"]);
403 });
404});
405
406describe("deriveTimelineEntries", () => {
407 it("includes proposed plans alongside messages and work entries in chronological order", () => {
408 const entries = deriveTimelineEntries(
409 [
410 {
411 id: MessageId.makeUnsafe("message-1"),
412 role: "assistant",
413 text: "hello",
414 createdAt: "2026-02-23T00:00:01.000Z",
415 streaming: false,
416 },
417 ],
418 [
419 {
420 id: "plan:thread-1:turn:turn-1",
421 turnId: TurnId.makeUnsafe("turn-1"),
422 planMarkdown: "# Ship it",
423 createdAt: "2026-02-23T00:00:02.000Z",
424 updatedAt: "2026-02-23T00:00:02.000Z",
425 },
426 ],
427 [
428 {
429 id: "work-1",
430 createdAt: "2026-02-23T00:00:03.000Z",
431 label: "Ran tests",
432 tone: "tool",
433 },
434 ],
435 );
436
437 expect(entries.map((entry) => entry.kind)).toEqual(["message", "proposed-plan", "work"]);
438 expect(entries[1]).toMatchObject({
439 kind: "proposed-plan",
440 proposedPlan: {
441 planMarkdown: "# Ship it",
442 },
443 });
444 });
445});
446
447describe("hasToolActivityForTurn", () => {
448 it("returns false when turn id is missing", () => {
449 const activities: OrchestrationThreadActivity[] = [
450 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
451 ];
452
453 expect(hasToolActivityForTurn(activities, undefined)).toBe(false);
454 expect(hasToolActivityForTurn(activities, null)).toBe(false);
455 });
456
457 it("returns true only for matching tool activity in the target turn", () => {
458 const activities: OrchestrationThreadActivity[] = [
459 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
460 makeActivity({ id: "info-1", turnId: "turn-2", kind: "turn.completed", tone: "info" }),
461 ];
462
463 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-1"))).toBe(true);
464 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-2"))).toBe(false);
465 });
466});
467
468describe("isLatestTurnSettled", () => {
469 const latestTurn = {
470 turnId: TurnId.makeUnsafe("turn-1"),
471 startedAt: "2026-02-27T21:10:00.000Z",
472 completedAt: "2026-02-27T21:10:06.000Z",
473 } as const;
474
475 it("returns false while the same turn is still active in a running session", () => {
476 expect(
477 isLatestTurnSettled(latestTurn, {
478 orchestrationStatus: "running",
479 activeTurnId: TurnId.makeUnsafe("turn-1"),
480 }),
481 ).toBe(false);
482 });
483
484 it("returns false while any turn is running to avoid stale latest-turn banners", () => {
485 expect(
486 isLatestTurnSettled(latestTurn, {
487 orchestrationStatus: "running",
488 activeTurnId: TurnId.makeUnsafe("turn-2"),
489 }),
490 ).toBe(false);
491 });
492
493 it("returns true once the session is no longer running that turn", () => {
494 expect(
495 isLatestTurnSettled(latestTurn, {
496 orchestrationStatus: "ready",
497 activeTurnId: undefined,
498 }),
499 ).toBe(true);
500 });
501
502 it("returns false when turn timestamps are incomplete", () => {
503 expect(
504 isLatestTurnSettled(
505 {
506 turnId: TurnId.makeUnsafe("turn-1"),
507 startedAt: null,
508 completedAt: "2026-02-27T21:10:06.000Z",
509 },
510 null,
511 ),
512 ).toBe(false);
513 });
514});
515
516describe("PROVIDER_OPTIONS", () => {
517 it("advertises codex and cursor on the cursor branch while keeping Claude Code as coming soon", () => {
518 const claude = PROVIDER_OPTIONS.find((option) => option.value === "claudeCode");
519 const cursor = PROVIDER_OPTIONS.find((option) => option.value === "cursor");
520 expect(claude).toEqual({
521 value: "claudeCode",
522 label: "Claude Code",
523 available: false,
524 });
525 expect(cursor).toEqual({
526 value: "cursor",
527 label: "Cursor Agent",
528 available: true,
529 });
530 });
531});