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

session-logic.test.ts

565 lines16.7 KB

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 };
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("omits task start and completion lifecycle entries", () => {
347 const activities: OrchestrationThreadActivity[] = [
348 makeActivity({
349 id: "task-start",
350 createdAt: "2026-02-23T00:00:01.000Z",
351 kind: "task.started",
352 summary: "default task started",
353 tone: "info",
354 }),
355 makeActivity({
356 id: "task-progress",
357 createdAt: "2026-02-23T00:00:02.000Z",
358 kind: "task.progress",
359 summary: "Updating files",
360 tone: "info",
361 }),
362 makeActivity({
363 id: "task-complete",
364 createdAt: "2026-02-23T00:00:03.000Z",
365 kind: "task.completed",
366 summary: "Task completed",
367 tone: "info",
368 }),
369 ];
370​
371 const entries = deriveWorkLogEntries(activities, undefined);
372 expect(entries.map((entry) => entry.id)).toEqual(["task-progress"]);
373 });
374​
375 it("filters by turn id when provided", () => {
376 const activities: OrchestrationThreadActivity[] = [
377 makeActivity({ id: "turn-1", turnId: "turn-1", summary: "Tool call", kind: "tool.started" }),
378 makeActivity({
379 id: "turn-2",
380 turnId: "turn-2",
381 summary: "Tool call complete",
382 kind: "tool.completed",
383 }),
384 makeActivity({ id: "no-turn", summary: "Checkpoint captured", tone: "info" }),
385 ];
386​
387 const entries = deriveWorkLogEntries(activities, TurnId.makeUnsafe("turn-2"));
388 expect(entries.map((entry) => entry.id)).toEqual(["turn-2"]);
389 });
390​
391 it("omits checkpoint captured info entries", () => {
392 const activities: OrchestrationThreadActivity[] = [
393 makeActivity({
394 id: "checkpoint",
395 createdAt: "2026-02-23T00:00:01.000Z",
396 summary: "Checkpoint captured",
397 tone: "info",
398 }),
399 makeActivity({
400 id: "tool-complete",
401 createdAt: "2026-02-23T00:00:02.000Z",
402 summary: "Command run complete",
403 tone: "tool",
404 kind: "tool.completed",
405 }),
406 ];
407​
408 const entries = deriveWorkLogEntries(activities, undefined);
409 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
410 });
411​
412 it("orders work log by activity sequence when present", () => {
413 const activities: OrchestrationThreadActivity[] = [
414 makeActivity({
415 id: "second",
416 createdAt: "2026-02-23T00:00:03.000Z",
417 sequence: 2,
418 summary: "Tool call complete",
419 kind: "tool.completed",
420 }),
421 makeActivity({
422 id: "first",
423 createdAt: "2026-02-23T00:00:04.000Z",
424 sequence: 1,
425 summary: "Tool call complete",
426 kind: "tool.completed",
427 }),
428 ];
429​
430 const entries = deriveWorkLogEntries(activities, undefined);
431 expect(entries.map((entry) => entry.id)).toEqual(["first", "second"]);
432 });
433});
434​
435describe("deriveTimelineEntries", () => {
436 it("includes proposed plans alongside messages and work entries in chronological order", () => {
437 const entries = deriveTimelineEntries(
438 [
439 {
440 id: MessageId.makeUnsafe("message-1"),
441 role: "assistant",
442 text: "hello",
443 createdAt: "2026-02-23T00:00:01.000Z",
444 streaming: false,
445 },
446 ],
447 [
448 {
449 id: "plan:thread-1:turn:turn-1",
450 turnId: TurnId.makeUnsafe("turn-1"),
451 planMarkdown: "# Ship it",
452 createdAt: "2026-02-23T00:00:02.000Z",
453 updatedAt: "2026-02-23T00:00:02.000Z",
454 },
455 ],
456 [
457 {
458 id: "work-1",
459 createdAt: "2026-02-23T00:00:03.000Z",
460 label: "Ran tests",
461 tone: "tool",
462 },
463 ],
464 );
465​
466 expect(entries.map((entry) => entry.kind)).toEqual(["message", "proposed-plan", "work"]);
467 expect(entries[1]).toMatchObject({
468 kind: "proposed-plan",
469 proposedPlan: {
470 planMarkdown: "# Ship it",
471 },
472 });
473 });
474});
475​
476describe("hasToolActivityForTurn", () => {
477 it("returns false when turn id is missing", () => {
478 const activities: OrchestrationThreadActivity[] = [
479 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
480 ];
481​
482 expect(hasToolActivityForTurn(activities, undefined)).toBe(false);
483 expect(hasToolActivityForTurn(activities, null)).toBe(false);
484 });
485​
486 it("returns true only for matching tool activity in the target turn", () => {
487 const activities: OrchestrationThreadActivity[] = [
488 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
489 makeActivity({ id: "info-1", turnId: "turn-2", kind: "turn.completed", tone: "info" }),
490 ];
491​
492 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-1"))).toBe(true);
493 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-2"))).toBe(false);
494 });
495});
496​
497describe("isLatestTurnSettled", () => {
498 const latestTurn = {
499 turnId: TurnId.makeUnsafe("turn-1"),
500 startedAt: "2026-02-27T21:10:00.000Z",
501 completedAt: "2026-02-27T21:10:06.000Z",
502 } as const;
503​
504 it("returns false while the same turn is still active in a running session", () => {
505 expect(
506 isLatestTurnSettled(latestTurn, {
507 orchestrationStatus: "running",
508 activeTurnId: TurnId.makeUnsafe("turn-1"),
509 }),
510 ).toBe(false);
511 });
512​
513 it("returns false while any turn is running to avoid stale latest-turn banners", () => {
514 expect(
515 isLatestTurnSettled(latestTurn, {
516 orchestrationStatus: "running",
517 activeTurnId: TurnId.makeUnsafe("turn-2"),
518 }),
519 ).toBe(false);
520 });
521​
522 it("returns true once the session is no longer running that turn", () => {
523 expect(
524 isLatestTurnSettled(latestTurn, {
525 orchestrationStatus: "ready",
526 activeTurnId: undefined,
527 }),
528 ).toBe(true);
529 });
530​
531 it("returns false when turn timestamps are incomplete", () => {
532 expect(
533 isLatestTurnSettled(
534 {
535 turnId: TurnId.makeUnsafe("turn-1"),
536 startedAt: null,
537 completedAt: "2026-02-27T21:10:06.000Z",
538 },
539 null,
540 ),
541 ).toBe(false);
542 });
543});
544​
545describe("PROVIDER_OPTIONS", () => {
546 it("keeps Claude Code and Cursor visible as unavailable placeholders in the stack base", () => {
547 const claude = PROVIDER_OPTIONS.find((option) => option.value === "claudeCode");
548 const cursor = PROVIDER_OPTIONS.find((option) => option.value === "cursor");
549 expect(PROVIDER_OPTIONS).toEqual([
550 { value: "codex", label: "Codex", available: true },
551 { value: "claudeCode", label: "Claude Code", available: false },
552 { value: "cursor", label: "Cursor", available: false },
553 ]);
554 expect(claude).toEqual({
555 value: "claudeCode",
556 label: "Claude Code",
557 available: false,
558 });
559 expect(cursor).toEqual({
560 value: "cursor",
561 label: "Cursor",
562 available: false,
563 });
564 });
565});