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

entities.test.ts

802 lines25.9 KB

1import {
2 EnvironmentId,
3 MessageId,
4 NodeId,
5 ProviderDriverKind,
6 ProviderInstanceId,
7 ProviderSessionId,
8 ProviderThreadId,
9 RunId,
10 RuntimeRequestId,
11 TurnItemId,
12} from "@t3tools/contracts";
13import * as DateTime from "effect/DateTime";
14import { describe, expect, it } from "vite-plus/test";
15​
16import {
17 InvalidScopedProjectRefCollectionKeyError,
18 InvalidScopedThreadKeyError,
19 parseProjectRefCollectionKey,
20 parseThreadKey,
21} from "./entities.ts";
22import {
23 presentThreadShell,
24 resolveThreadProviderStack,
25 resolveThreadWorkingStartedAt,
26} from "./models.ts";
27import { v2Projection, v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
28import { deriveLatestThreadRun, deriveThreadRuntime } from "./threadExecution.ts";
29import { isThreadWorking } from "./threadInbox.ts";
30import { derivePendingThreadRequests } from "./threadRequests.ts";
31import { canSnooze, effectiveSnoozed } from "./threadSettled.ts";
32​
33const environmentId = EnvironmentId.make("environment-v2");
34​
35describe("scoped entity keys", () => {
36 it("preserves an invalid thread key as structured error data", () => {
37 const key = "missing-thread-key-separator";
38 let error: unknown;
39​
40 try {
41 parseThreadKey(key);
42 } catch (cause) {
43 error = cause;
44 }
45​
46 expect(error).toEqual(new InvalidScopedThreadKeyError({ key }));
47 });
48​
49 it("preserves malformed project reference collection input and its cause", () => {
50 const key = "not-json";
51 let error: unknown;
52​
53 try {
54 parseProjectRefCollectionKey(key);
55 } catch (cause) {
56 error = cause;
57 }
58​
59 expect(error).toBeInstanceOf(InvalidScopedProjectRefCollectionKeyError);
60 expect(error).toMatchObject({ key, cause: expect.anything() });
61 });
62​
63 it("rejects invalid project reference collection shapes", () => {
64 const key = JSON.stringify([["environment-1"]]);
65​
66 expect(() => parseProjectRefCollectionKey(key)).toThrowError(
67 InvalidScopedProjectRefCollectionKeyError,
68 );
69 });
70});
71​
72describe("V2 client presentation", () => {
73 it("presents shell timestamps and status without constructing V1 state", () => {
74 const shell = presentThreadShell(environmentId, v2ThreadShell);
75 expect(shell.environmentId).toBe(environmentId);
76 expect(shell.createdAt).toBe("2026-06-20T00:00:00.000Z");
77 expect(shell.runtime).toBeNull();
78 expect(shell.source).toBe(v2ThreadShell);
79 });
80​
81 it("preserves active ordering and both pull-request sources", () => {
82 const linkedPullRequest = {
83 projectId: v2ThreadShell.projectId,
84 repository: "pingdotgg/t3code",
85 number: 42,
86 url: "https://github.com/pingdotgg/t3code/pull/42",
87 };
88 const branchPullRequest = { ...linkedPullRequest, number: 43 };
89 const shell = presentThreadShell(environmentId, {
90 ...v2ThreadShell,
91 activeOrderKey: "m",
92 linkedPullRequest,
93 branchPullRequest,
94 });
95​
96 expect(shell.activeOrderKey).toBe("m");
97 expect(shell.linkedPullRequest).toEqual(linkedPullRequest);
98 expect(shell.branchPullRequest).toEqual(branchPullRequest);
99 });
100​
101 it("presents provider errors carried by failed thread shells", () => {
102 const runId = RunId.make("run-failed");
103 const shell = presentThreadShell(environmentId, {
104 ...v2ThreadShell,
105 latestRunId: runId,
106 status: "failed",
107 lastError: "provider process exited",
108 });
109​
110 expect(shell.runtime).toMatchObject({
111 status: "failed",
112 lastError: "provider process exited",
113 });
114 });
115​
116 it("keeps async questions in Working until the run completes", () => {
117 const runId = RunId.make("run-async-question");
118 const running = {
119 ...v2ThreadShell,
120 snoozedAt: DateTime.makeUnsafe("2026-06-19T23:59:00.000Z"),
121 snoozedUntil: DateTime.makeUnsafe("2026-06-20T01:00:00.000Z"),
122 latestRunId: runId,
123 activeRunId: runId,
124 status: "running" as const,
125 pendingRuntimeRequest: {
126 id: RuntimeRequestId.make("request-async-question"),
127 kind: "user_input" as const,
128 createdAt: v2ThreadShell.updatedAt,
129 blocking: false,
130 },
131 };
132 const active = presentThreadShell(environmentId, running);
133 expect(active.hasPendingUserInput).toBe(false);
134 expect(isThreadWorking(active)).toBe(true);
135 expect(canSnooze(active, { now: "2026-06-20T00:05:00.000Z" })).toBe(false);
136​
137 const completed = presentThreadShell(environmentId, {
138 ...running,
139 activeRunId: null,
140 status: "completed",
141 });
142 expect(completed.hasPendingUserInput).toBe(true);
143 expect(isThreadWorking(completed)).toBe(false);
144 expect(effectiveSnoozed(active, { now: "2026-06-20T00:05:00.000Z" })).toBe(false);
145 });
146​
147 it("keeps blocking and legacy questions out of Working", () => {
148 for (const capability of [{ blocking: true }, {}]) {
149 const shell = presentThreadShell(environmentId, {
150 ...v2ThreadShell,
151 latestRunId: RunId.make("run-blocking-question"),
152 activeRunId: RunId.make("run-blocking-question"),
153 status: "running",
154 pendingRuntimeRequest: {
155 id: RuntimeRequestId.make("request-blocking-question"),
156 kind: "user_input",
157 createdAt: v2ThreadShell.updatedAt,
158 ...capability,
159 },
160 });
161 expect(shell.hasPendingUserInput).toBe(true);
162 expect(isThreadWorking(shell)).toBe(false);
163 }
164 });
165​
166 it("preserves shell run timestamps for sidebar activity clocks", () => {
167 const runId = RunId.make("run-working-clock");
168 const requestedAt = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
169 const startedAt = DateTime.makeUnsafe("2026-06-20T01:00:02.000Z");
170 const shell = presentThreadShell(environmentId, {
171 ...v2ThreadShell,
172 latestRunId: runId,
173 latestRunRequestedAt: requestedAt,
174 latestRunStartedAt: startedAt,
175 latestRunCompletedAt: null,
176 activeRunId: runId,
177 status: "running",
178 updatedAt: DateTime.makeUnsafe("2026-06-20T01:04:45.000Z"),
179 });
180​
181 expect(shell.latestRun).toMatchObject({
182 runId,
183 status: "running",
184 requestedAt: "2026-06-20T01:00:00.000Z",
185 startedAt: "2026-06-20T01:00:02.000Z",
186 completedAt: null,
187 });
188 });
189​
190 it("parks presented runtime at idle when a settled shell has pending background tasks", () => {
191 const runId = RunId.make("run-completed");
192 const shell = presentThreadShell(environmentId, {
193 ...v2ThreadShell,
194 latestRunId: runId,
195 activeRunId: null,
196 status: "completed",
197 pendingBackgroundTasks: [{ taskId: "bg-1", description: "Watch build", kind: "monitor" }],
198 });
199​
200 expect(shell.latestRun).toMatchObject({ runId, status: "completed" });
201 expect(shell.runtime).toMatchObject({
202 status: "idle",
203 activeRunId: null,
204 });
205 expect(shell.pendingBackgroundTasks).toEqual([
206 { taskId: "bg-1", description: "Watch build", kind: "monitor" },
207 ]);
208 });
209​
210 it("parks a thread that never ran at idle while a pull request watch holds it", () => {
211 const watch = { taskId: "pull-request-watch:github.com/acme/app#1", kind: "monitor" as const };
212 const watched = presentThreadShell(environmentId, {
213 ...v2ThreadShell,
214 latestRunId: null,
215 activeProviderThreadId: null,
216 activeRunId: null,
217 status: "idle",
218 pendingBackgroundTasks: [watch],
219 });
220 expect(watched.runtime).toMatchObject({ status: "idle", activeRunId: null });
221 expect(
222 presentThreadShell(environmentId, {
223 ...v2ThreadShell,
224 latestRunId: null,
225 activeProviderThreadId: null,
226 status: "idle",
227 pendingBackgroundTasks: [],
228 }).runtime,
229 ).toBeNull();
230 });
231​
232 it.each([
233 { kinds: ["command"], expected: "completed" },
234 { kinds: ["command", "subagent"], expected: "idle" },
235 { kinds: ["background_task"], expected: "idle" },
236 ] as const)("presents a completed shell with $kinds as $expected", ({ kinds, expected }) => {
237 const pendingBackgroundTasks = kinds.map((kind, index) => ({
238 taskId: `bg-${index}`,
239 kind,
240 }));
241 const shell = presentThreadShell(environmentId, {
242 ...v2ThreadShell,
243 latestRunId: RunId.make("run-completed"),
244 activeRunId: null,
245 status: "completed",
246 pendingBackgroundTasks,
247 });
248​
249 expect(shell.latestRun?.status).toBe("completed");
250 expect(shell.runtime).toMatchObject({ status: expected, activeRunId: null });
251 expect(shell.pendingBackgroundTasks).toEqual(pendingBackgroundTasks);
252 });
253​
254 it.each(["running", "waiting"] as const)(
255 "preserves shell %s while only commands remain in the roster",
256 (status) => {
257 const runId = RunId.make("run-command");
258 const shell = presentThreadShell(environmentId, {
259 ...v2ThreadShell,
260 latestRunId: runId,
261 activeRunId: runId,
262 status,
263 pendingBackgroundTasks: [{ taskId: "dev-server", kind: "command" }],
264 });
265​
266 expect(shell.latestRun?.status).toBe(status);
267 expect(shell.runtime).toMatchObject({ status, activeRunId: runId });
268 expect(shell.pendingBackgroundTasks).toEqual([{ taskId: "dev-server", kind: "command" }]);
269 },
270 );
271​
272 it("keeps a failed latest run failed while background tasks are still pending", () => {
273 const shell = presentThreadShell(environmentId, {
274 ...v2ThreadShell,
275 latestRunId: RunId.make("run-failed"),
276 activeRunId: null,
277 status: "failed",
278 lastError: "Provider turn failed",
279 pendingBackgroundTasks: [{ taskId: "bg-1", description: "Watch build", kind: "monitor" }],
280 });
281​
282 expect(shell.runtime).toMatchObject({ status: "failed", lastError: "Provider turn failed" });
283 expect(shell.pendingBackgroundTasks).toHaveLength(1);
284 });
285​
286 it("stacks earlier provider owners behind the current one, newest history first to go", () => {
287 const codex = ProviderInstanceId.make("codex");
288 const claude = ProviderInstanceId.make("claude");
289 const cursor = ProviderInstanceId.make("cursor");
290 const grok = ProviderInstanceId.make("grok");
291 const shell = presentThreadShell(environmentId, {
292 ...v2ThreadShell,
293 providerInstanceId: grok,
294 modelSelection: { instanceId: grok, model: "grok-4" },
295 providerInstanceHistory: [codex, claude, cursor, grok],
296 });
297​
298 expect(resolveThreadProviderStack(shell)).toEqual([claude, cursor, grok]);
299 expect(
300 resolveThreadProviderStack({ ...shell, providerInstanceHistory: [codex, grok] }),
301 ).toEqual([codex, grok]);
302 expect(resolveThreadProviderStack({ ...shell, providerInstanceHistory: [] })).toEqual([grok]);
303 expect(
304 presentThreadShell(environmentId, { ...v2ThreadShell, providerInstanceHistory: undefined })
305 .providerInstanceHistory,
306 ).toEqual([]);
307 });
308​
309 it("keeps terminal runtime completed when there are no pending background tasks", () => {
310 const runId = RunId.make("run-completed");
311 const shell = presentThreadShell(environmentId, {
312 ...v2ThreadShell,
313 latestRunId: runId,
314 activeRunId: null,
315 status: "completed",
316 pendingBackgroundTasks: [],
317 });
318​
319 expect(shell.latestRun).toMatchObject({ runId, status: "completed" });
320 expect(shell.runtime).toMatchObject({
321 status: "completed",
322 activeRunId: null,
323 });
324 });
325​
326 it("keeps runtime running when there is no background roster", () => {
327 const runId = RunId.make("run-running");
328 const shell = presentThreadShell(environmentId, {
329 ...v2ThreadShell,
330 latestRunId: runId,
331 activeRunId: runId,
332 status: "running",
333 pendingBackgroundTasks: [],
334 });
335​
336 expect(shell.latestRun).toMatchObject({ runId, status: "running" });
337 expect(shell.runtime).toMatchObject({
338 status: "running",
339 activeRunId: runId,
340 });
341 });
342​
343 it("keeps an older active run visible over a newer cancelled run", () => {
344 const activeRunId = RunId.make("run-active");
345 const cancelledRunId = RunId.make("run-cancelled");
346 const shell = presentThreadShell(environmentId, {
347 ...v2ThreadShell,
348 latestRunId: cancelledRunId,
349 latestRunStartedAt: null,
350 latestRunCompletedAt: DateTime.makeUnsafe("2026-06-20T01:05:00.000Z"),
351 activeRunId,
352 activityRunStatus: "running",
353 status: "cancelled",
354 pendingBackgroundTasks: [],
355 });
356​
357 expect(shell.latestRun).toMatchObject({ runId: cancelledRunId, status: "cancelled" });
358 expect(shell.runtime).toMatchObject({
359 status: "running",
360 activeRunId,
361 });
362 });
363​
364 it("keeps an older waiting run visible over a newer cancelled run", () => {
365 const shell = presentThreadShell(environmentId, {
366 ...v2ThreadShell,
367 latestRunId: RunId.make("run-cancelled"),
368 activeRunId: null,
369 activityRunStatus: "waiting",
370 status: "cancelled",
371 pendingBackgroundTasks: [],
372 });
373​
374 expect(shell.runtime).toMatchObject({ status: "waiting", activeRunId: null });
375 });
376​
377 it("keeps a post-settlement background roster ahead of activity status", () => {
378 const activeRunId = RunId.make("run-active");
379 const shell = presentThreadShell(environmentId, {
380 ...v2ThreadShell,
381 latestRunId: RunId.make("run-cancelled"),
382 activeRunId,
383 activityRunStatus: "running",
384 status: "cancelled",
385 pendingBackgroundTasks: [
386 { taskId: "bg-activity", description: "background work", kind: "background_task" },
387 ],
388 });
389​
390 expect(shell.runtime).toMatchObject({ status: "idle", activeRunId });
391 });
392​
393 it("parks runtime idle over stale shell running when the roster is nonempty", () => {
394 const runId = RunId.make("run-stale-running");
395 const shell = presentThreadShell(environmentId, {
396 ...v2ThreadShell,
397 latestRunId: runId,
398 activeRunId: runId,
399 status: "running",
400 pendingBackgroundTasks: [{ taskId: "bg-1", description: "Watch build", kind: "monitor" }],
401 });
402​
403 expect(shell.latestRun).toMatchObject({ runId, status: "running" });
404 expect(shell.runtime).toMatchObject({
405 status: "idle",
406 activeRunId: runId,
407 });
408 expect(shell.pendingBackgroundTasks).toEqual([
409 { taskId: "bg-1", description: "Watch build", kind: "monitor" },
410 ]);
411 });
412​
413 it("parks runtime idle over stale checkpoint waiting when the roster is nonempty", () => {
414 const runId = RunId.make("run-stale-waiting");
415 const shell = presentThreadShell(environmentId, {
416 ...v2ThreadShell,
417 latestRunId: runId,
418 activeRunId: runId,
419 status: "waiting",
420 pendingBackgroundTasks: [{ taskId: "bg-2", description: "Watch build", kind: "monitor" }],
421 });
422​
423 expect(shell.latestRun).toMatchObject({ runId, status: "waiting" });
424 expect(shell.runtime).toMatchObject({
425 status: "idle",
426 activeRunId: runId,
427 });
428 expect(shell.pendingBackgroundTasks).toEqual([
429 { taskId: "bg-2", description: "Watch build", kind: "monitor" },
430 ]);
431 });
432​
433 it("derives execution summaries without wrapping or copying the projection", () => {
434 const runId = RunId.make("run-1");
435 const now = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
436 const projection = {
437 ...v2Projection,
438 runs: [
439 {
440 id: runId,
441 threadId: v2Projection.thread.id,
442 ordinal: 1,
443 providerInstanceId: v2Projection.thread.providerInstanceId,
444 modelSelection: v2Projection.thread.modelSelection,
445 providerThreadId: null,
446 userMessageId: MessageId.make("message-user"),
447 rootNodeId: null,
448 activeAttemptId: null,
449 status: "running" as const,
450 requestedAt: now,
451 startedAt: now,
452 completedAt: null,
453 checkpointId: null,
454 contextHandoffId: null,
455 },
456 ],
457 messages: [
458 {
459 id: MessageId.make("message-user"),
460 threadId: v2Projection.thread.id,
461 runId,
462 nodeId: null,
463 role: "user" as const,
464 text: "Hello",
465 attachments: [],
466 streaming: false,
467 createdBy: "user" as const,
468 creationSource: "web" as const,
469 createdAt: now,
470 updatedAt: now,
471 },
472 ],
473 updatedAt: now,
474 };
475​
476 expect(deriveLatestThreadRun(projection)).toMatchObject({
477 runId,
478 status: "running",
479 requestedAt: "2026-06-20T01:00:00.000Z",
480 assistantMessageId: null,
481 });
482 expect(deriveThreadRuntime(projection)).toMatchObject({
483 status: "running",
484 activeRunId: runId,
485 providerInstanceId: projection.thread.providerInstanceId,
486 });
487 for (const status of ["queued", "cancelled"] as const) {
488 const later = DateTime.add(now, { hours: 1 });
489 const latest = {
490 ...projection.runs[0]!,
491 id: RunId.make("newer-run"),
492 ordinal: 2,
493 status,
494 requestedAt: later,
495 startedAt: null,
496 completedAt: status === "cancelled" ? later : null,
497 };
498 const detail = { ...projection, runs: [...projection.runs, latest], updatedAt: later };
499 const shell = presentThreadShell(environmentId, {
500 ...v2ThreadShell,
501 latestRunId: latest.id,
502 latestRunStartedAt: null,
503 latestRunRequestedAt: later,
504 latestRunCompletedAt: latest.completedAt,
505 status,
506 activeRunId: runId,
507 activityRunStatus: "running",
508 activityRunStartedAt: now,
509 updatedAt: later,
510 });
511 expect(resolveThreadWorkingStartedAt(shell)).toBe(DateTime.formatIso(now));
512 expect(
513 resolveThreadWorkingStartedAt({
514 latestRun: deriveLatestThreadRun(detail),
515 runtime: deriveThreadRuntime(detail),
516 }),
517 ).toBe(resolveThreadWorkingStartedAt(shell));
518 const stopped = {
519 ...detail,
520 runs: detail.runs.map((run) => ({
521 ...run,
522 status: "completed" as const,
523 completedAt: later,
524 })),
525 };
526 expect(
527 resolveThreadWorkingStartedAt({
528 latestRun: deriveLatestThreadRun(stopped),
529 runtime: deriveThreadRuntime(stopped),
530 }),
531 ).toBeNull();
532 }
533 });
534​
535 it("parks waiting runtime for a post-settlement roster without hiding active running work", () => {
536 const runId = RunId.make("run-background-presentation");
537 const now = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
538 const run = {
539 id: runId,
540 threadId: v2Projection.thread.id,
541 ordinal: 1,
542 providerInstanceId: v2Projection.thread.providerInstanceId,
543 modelSelection: v2Projection.thread.modelSelection,
544 providerThreadId: null,
545 userMessageId: MessageId.make("message-background-presentation"),
546 rootNodeId: null,
547 activeAttemptId: null,
548 status: "waiting" as const,
549 requestedAt: now,
550 startedAt: now,
551 completedAt: null,
552 checkpointId: null,
553 contextHandoffId: null,
554 };
555 const backgroundItem = {
556 id: TurnItemId.make("item-background-subagent"),
557 threadId: v2Projection.thread.id,
558 runId,
559 nodeId: null,
560 providerThreadId: null,
561 providerTurnId: null,
562 nativeItemRef: null,
563 parentItemId: null,
564 ordinal: 0,
565 status: "running" as const,
566 title: "Background review",
567 startedAt: now,
568 completedAt: null,
569 updatedAt: now,
570 type: "subagent" as const,
571 subagentId: NodeId.make("subagent-review"),
572 origin: "provider_native" as const,
573 driver: ProviderDriverKind.make("codex"),
574 providerInstanceId: v2Projection.thread.providerInstanceId,
575 childThreadId: null,
576 prompt: "Review the changes",
577 result: null,
578 };
579​
580 expect(
581 deriveThreadRuntime({
582 ...v2Projection,
583 runs: [run],
584 turnItems: [backgroundItem],
585 }),
586 ).toMatchObject({
587 status: "idle",
588 activeRunId: null,
589 });
590 expect(
591 deriveThreadRuntime({
592 ...v2Projection,
593 runs: [{ ...run, status: "running" as const }],
594 turnItems: [backgroundItem],
595 }),
596 ).toMatchObject({
597 status: "running",
598 activeRunId: runId,
599 });
600 expect(
601 deriveThreadRuntime({
602 ...v2Projection,
603 runs: [{ ...run, status: "failed" as const, completedAt: now }],
604 turnItems: [backgroundItem],
605 }),
606 ).toMatchObject({ status: "failed", activeRunId: null });
607 const commandItem = {
608 ...backgroundItem,
609 id: TurnItemId.make("item-background-command"),
610 type: "command_execution" as const,
611 input: "npm run dev",
612 };
613 expect(
614 deriveThreadRuntime({ ...v2Projection, runs: [run], turnItems: [commandItem] }),
615 ).toMatchObject({ status: "waiting", activeRunId: null });
616​
617 for (const [turnItems, status] of [
618 [[commandItem], "completed"],
619 [[backgroundItem], "idle"],
620 [[commandItem, backgroundItem], "idle"],
621 ] as const) {
622 expect(
623 deriveThreadRuntime({
624 ...v2Projection,
625 runs: [{ ...run, status: "completed", completedAt: now }],
626 turnItems,
627 }),
628 ).toMatchObject({ status, activeRunId: null });
629 }
630​
631 for (const kind of ["monitor", "background_task"] as const) {
632 const providerThread = {
633 id: ProviderThreadId.make("provider-thread-background"),
634 driver: ProviderDriverKind.make("claudeCode"),
635 providerInstanceId: v2Projection.thread.providerInstanceId,
636 providerSessionId: null,
637 appThreadId: v2Projection.thread.id,
638 ownerNodeId: null,
639 nativeThreadRef: null,
640 nativeConversationHeadRef: null,
641 status: "idle" as const,
642 firstRunOrdinal: 1,
643 lastRunOrdinal: 1,
644 handoffIds: [],
645 forkedFrom: null,
646 pendingBackgroundTasks: [{ taskId: "background-task", kind }],
647 createdAt: now,
648 updatedAt: now,
649 };
650 for (const status of ["completed", "failed"] as const) {
651 expect(
652 deriveThreadRuntime({
653 ...v2Projection,
654 runs: [{ ...run, status, completedAt: now }],
655 providerThreads: [providerThread],
656 }),
657 ).toMatchObject({ status: status === "failed" ? "failed" : "idle", activeRunId: null });
658 }
659 }
660 });
661​
662 it("joins pending request entities to their native turn-item display data", () => {
663 const now = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
664 const requestId = RuntimeRequestId.make("request-approval");
665 const item = {
666 id: TurnItemId.make("item-approval"),
667 threadId: v2Projection.thread.id,
668 runId: null,
669 nodeId: NodeId.make("node-root"),
670 providerThreadId: null,
671 providerTurnId: null,
672 nativeItemRef: null,
673 parentItemId: null,
674 ordinal: 0,
675 status: "completed" as const,
676 title: null,
677 startedAt: now,
678 completedAt: now,
679 updatedAt: now,
680 type: "approval_request" as const,
681 requestId,
682 requestKind: "command" as const,
683 prompt: "Allow command?",
684 };
685 const projection = {
686 ...v2Projection,
687 runtimeRequests: [
688 {
689 id: requestId,
690 nodeId: NodeId.make("node-root"),
691 providerTurnId: null,
692 nativeRequestRef: null,
693 kind: "command" as const,
694 status: "pending" as const,
695 responseCapability: {
696 type: "not_resumable" as const,
697 reason: "provider disconnected",
698 },
699 createdAt: now,
700 resolvedAt: null,
701 },
702 ],
703 turnItems: [item],
704 updatedAt: now,
705 };
706​
707 expect(derivePendingThreadRequests(projection).approvals).toEqual([
708 {
709 requestId,
710 requestKind: "command",
711 createdAt: "2026-06-20T01:00:00.000Z",
712 detail: "Allow command?",
713 responseCapability: "not_resumable",
714 },
715 ]);
716 expect(derivePendingThreadRequests(projection).userInputs).toEqual([]);
717 });
718​
719 it("preserves structured-question selection mode and defaults legacy payloads", () => {
720 const now = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
721 const multiRequestId = RuntimeRequestId.make("request-multi-select");
722 const legacyRequestId = RuntimeRequestId.make("request-legacy-single-select");
723 const runtimeRequests = [multiRequestId, legacyRequestId].map((id) => ({
724 id,
725 nodeId: NodeId.make(`node-${id}`),
726 providerTurnId: null,
727 nativeRequestRef: null,
728 kind: "user_input" as const,
729 status: "pending" as const,
730 responseCapability: {
731 type: "live" as const,
732 providerSessionId: ProviderSessionId.make("provider-session-questions"),
733 },
734 createdAt: now,
735 resolvedAt: null,
736 }));
737 const common = {
738 threadId: v2Projection.thread.id,
739 runId: null,
740 providerThreadId: null,
741 providerTurnId: null,
742 nativeItemRef: null,
743 parentItemId: null,
744 ordinal: 0,
745 status: "completed" as const,
746 title: null,
747 startedAt: now,
748 completedAt: now,
749 updatedAt: now,
750 type: "user_input_request" as const,
751 };
752 const projection = {
753 ...v2Projection,
754 runtimeRequests,
755 turnItems: [
756 {
757 ...common,
758 id: TurnItemId.make("item-multi-select"),
759 nodeId: NodeId.make("node-request-multi-select"),
760 requestId: multiRequestId,
761 questions: [
762 {
763 id: "regions",
764 header: "Regions",
765 question: "Which regions?",
766 options: [
767 { label: "US", description: "United States" },
768 { label: "EU", description: "European Union" },
769 ],
770 multiSelect: true,
771 },
772 ],
773 },
774 {
775 ...common,
776 id: TurnItemId.make("item-legacy-single-select"),
777 nodeId: NodeId.make("node-request-legacy-single-select"),
778 requestId: legacyRequestId,
779 questions: [
780 {
781 id: "target",
782 header: "Target",
783 question: "Which target?",
784 options: [{ label: "Staging", description: "Staging environment" }],
785 },
786 ],
787 },
788 ],
789 updatedAt: now,
790 };
791​
792 expect(
793 derivePendingThreadRequests(projection).userInputs.map((input) => ({
794 requestId: input.requestId,
795 multiSelect: input.questions[0]?.multiSelect,
796 })),
797 ).toEqual([
798 { requestId: multiRequestId, multiSelect: true },
799 { requestId: legacyRequestId, multiSelect: false },
800 ]);
801 });
802});