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

entities.test.ts

625 lines20.1 KB

1import {
2 EnvironmentId,
3 MessageId,
4 NodeId,
5 ProviderDriverKind,
6 ProviderSessionId,
7 ProviderThreadId,
8 RunId,
9 RuntimeRequestId,
10 TurnItemId,
11} from "@t3tools/contracts";
12import * as DateTime from "effect/DateTime";
13import { describe, expect, it } from "vite-plus/test";
14​
15import { presentThreadShell, resolveThreadWorkingStartedAt } from "./models.ts";
16import { v2Projection, v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
17import { deriveLatestThreadRun, deriveThreadRuntime } from "./threadExecution.ts";
18import { isThreadWorking } from "./threadInbox.ts";
19import { derivePendingThreadRequests } from "./threadRequests.ts";
20import { canSnooze, effectiveSnoozed } from "./threadSettled.ts";
21​
22const environmentId = EnvironmentId.make("environment-v2");
23​
24describe("V2 client presentation", () => {
25 it("stops showing the branch pull request once it is unlinked", () => {
26 const branchPullRequest = {
27 projectId: v2ThreadShell.projectId,
28 repository: "pingdotgg/t3code",
29 number: 7,
30 url: "https://github.com/pingdotgg/t3code/pull/7",
31 };
32 const link = {
33 host: "github.com",
34 repository: "pingdotgg/t3code",
35 number: 7,
36 url: branchPullRequest.url,
37 source: "manual" as const,
38 linkedAt: "2026-06-20T00:00:00.000Z",
39 snapshot: null,
40 stack: null,
41 };
42 const shell = (source: "manual" | "stack-dismissed") =>
43 presentThreadShell(environmentId, {
44 ...v2ThreadShell,
45 branchPullRequest,
46 pullRequests: [{ ...link, source }],
47 });
48 expect(shell("manual").branchPullRequest).toEqual(branchPullRequest);
49 expect(shell("stack-dismissed").branchPullRequest).toBeNull();
50 });
51​
52 it("an async question allows snooze and wakes an older snooze", () => {
53 const runId = RunId.make("run-async-question");
54 const running = {
55 ...v2ThreadShell,
56 snoozedAt: DateTime.makeUnsafe("2026-06-19T23:59:00.000Z"),
57 snoozedUntil: DateTime.makeUnsafe("2026-06-20T01:00:00.000Z"),
58 latestRunId: runId,
59 activeRunId: runId,
60 status: "running" as const,
61 pendingRuntimeRequest: {
62 id: RuntimeRequestId.make("request-async-question"),
63 kind: "user_input" as const,
64 createdAt: v2ThreadShell.updatedAt,
65 blocking: false,
66 },
67 };
68 const active = presentThreadShell(environmentId, running);
69 expect(active.hasPendingUserInput).toBe(true);
70 expect(isThreadWorking(active)).toBe(false);
71 expect(canSnooze(active, { now: "2026-06-20T00:05:00.000Z" })).toBe(true);
72​
73 const completed = presentThreadShell(environmentId, {
74 ...running,
75 activeRunId: null,
76 status: "completed",
77 });
78 expect(completed.hasPendingUserInput).toBe(true);
79 expect(isThreadWorking(completed)).toBe(false);
80 expect(effectiveSnoozed(active, { now: "2026-06-20T00:05:00.000Z" })).toBe(false);
81 });
82​
83 it("moves a running thread with any question out of Working", () => {
84 for (const capability of [{ blocking: false }, { blocking: true }, {}]) {
85 const shell = presentThreadShell(environmentId, {
86 ...v2ThreadShell,
87 latestRunId: RunId.make("run-blocking-question"),
88 activeRunId: RunId.make("run-blocking-question"),
89 status: "running",
90 pendingRuntimeRequest: {
91 id: RuntimeRequestId.make("request-blocking-question"),
92 kind: "user_input",
93 createdAt: v2ThreadShell.updatedAt,
94 ...capability,
95 },
96 });
97 expect(shell.hasPendingUserInput).toBe(true);
98 expect(isThreadWorking(shell)).toBe(false);
99 }
100 });
101​
102 it("parks a thread that never ran at idle while a pull request watch holds it", () => {
103 const watch = {
104 taskId: "pull-request-watch:github.com/acme/app#1",
105 kind: "monitor" as const,
106 awaited: true as const,
107 };
108 const watched = presentThreadShell(environmentId, {
109 ...v2ThreadShell,
110 latestRunId: null,
111 activeProviderThreadId: null,
112 activeRunId: null,
113 status: "idle",
114 pendingBackgroundTasks: [watch],
115 });
116 expect(watched.runtime).toMatchObject({ status: "idle", activeRunId: null });
117 expect(
118 presentThreadShell(environmentId, {
119 ...v2ThreadShell,
120 latestRunId: null,
121 activeProviderThreadId: null,
122 status: "idle",
123 pendingBackgroundTasks: [],
124 }).runtime,
125 ).toBeNull();
126 });
127​
128 it("keeps an imported thread that never ran out of Working", () => {
129 const imported = presentThreadShell(environmentId, {
130 ...v2ThreadShell,
131 historyOrigin: "v1_import",
132 activeProviderThreadId: ProviderThreadId.make("provider-thread-imported"),
133 latestRunId: null,
134 activeRunId: null,
135 status: "idle",
136 pendingBackgroundTasks: [],
137 });
138 expect(isThreadWorking(imported)).toBe(false);
139 });
140​
141 it.each([
142 { kinds: ["command"], expected: "completed" },
143 { kinds: ["command", "subagent"], expected: "idle" },
144 { kinds: ["background_task"], expected: "idle" },
145 ] as const)("presents a completed shell with $kinds as $expected", ({ kinds, expected }) => {
146 const pendingBackgroundTasks = kinds.map((kind, index) => ({
147 taskId: `bg-${index}`,
148 kind,
149 }));
150 const shell = presentThreadShell(environmentId, {
151 ...v2ThreadShell,
152 latestRunId: RunId.make("run-completed"),
153 activeRunId: null,
154 status: "completed",
155 pendingBackgroundTasks,
156 });
157​
158 expect(shell.latestRun?.status).toBe("completed");
159 expect(shell.runtime).toMatchObject({ status: expected, activeRunId: null });
160 expect(shell.pendingBackgroundTasks).toEqual(pendingBackgroundTasks);
161 });
162​
163 it.each(["running", "waiting"] as const)(
164 "preserves shell %s while only commands remain in the roster",
165 (status) => {
166 const runId = RunId.make("run-command");
167 const shell = presentThreadShell(environmentId, {
168 ...v2ThreadShell,
169 latestRunId: runId,
170 activeRunId: runId,
171 status,
172 pendingBackgroundTasks: [{ taskId: "dev-server", kind: "command" }],
173 });
174​
175 expect(shell.latestRun?.status).toBe(status);
176 expect(shell.runtime).toMatchObject({ status, activeRunId: runId });
177 expect(shell.pendingBackgroundTasks).toEqual([{ taskId: "dev-server", kind: "command" }]);
178 },
179 );
180​
181 it("keeps a failed latest run failed while background tasks are still pending", () => {
182 const shell = presentThreadShell(environmentId, {
183 ...v2ThreadShell,
184 latestRunId: RunId.make("run-failed"),
185 activeRunId: null,
186 status: "failed",
187 lastError: "Provider turn failed",
188 pendingBackgroundTasks: [
189 { taskId: "bg-1", description: "Watch build", kind: "monitor", awaited: true },
190 ],
191 });
192​
193 expect(shell.runtime).toMatchObject({ status: "failed", lastError: "Provider turn failed" });
194 expect(shell.pendingBackgroundTasks).toHaveLength(1);
195 });
196​
197 it("keeps an older active run visible over a newer cancelled run", () => {
198 const activeRunId = RunId.make("run-active");
199 const cancelledRunId = RunId.make("run-cancelled");
200 const shell = presentThreadShell(environmentId, {
201 ...v2ThreadShell,
202 latestRunId: cancelledRunId,
203 latestRunStartedAt: null,
204 latestRunCompletedAt: DateTime.makeUnsafe("2026-06-20T01:05:00.000Z"),
205 activeRunId,
206 activityRunStatus: "running",
207 status: "cancelled",
208 pendingBackgroundTasks: [],
209 });
210​
211 expect(shell.latestRun).toMatchObject({ runId: cancelledRunId, status: "cancelled" });
212 expect(shell.runtime).toMatchObject({
213 status: "running",
214 activeRunId,
215 });
216 });
217​
218 it("keeps a post-settlement background roster ahead of activity status", () => {
219 const activeRunId = RunId.make("run-active");
220 const shell = presentThreadShell(environmentId, {
221 ...v2ThreadShell,
222 latestRunId: RunId.make("run-cancelled"),
223 activeRunId,
224 activityRunStatus: "running",
225 status: "cancelled",
226 pendingBackgroundTasks: [
227 { taskId: "bg-activity", description: "background work", kind: "background_task" },
228 ],
229 });
230​
231 expect(shell.runtime).toMatchObject({ status: "idle", activeRunId });
232 });
233​
234 it("parks runtime idle over stale shell running when the roster is nonempty", () => {
235 const runId = RunId.make("run-stale-running");
236 const shell = presentThreadShell(environmentId, {
237 ...v2ThreadShell,
238 latestRunId: runId,
239 activeRunId: runId,
240 status: "running",
241 pendingBackgroundTasks: [
242 { taskId: "bg-1", description: "Watch build", kind: "monitor", awaited: true },
243 ],
244 });
245​
246 expect(shell.latestRun).toMatchObject({ runId, status: "running" });
247 expect(shell.runtime).toMatchObject({
248 status: "idle",
249 activeRunId: runId,
250 });
251 expect(shell.pendingBackgroundTasks).toEqual([
252 { taskId: "bg-1", description: "Watch build", kind: "monitor", awaited: true },
253 ]);
254 });
255​
256 it("derives execution summaries without wrapping or copying the projection", () => {
257 const runId = RunId.make("run-1");
258 const now = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
259 const projection = {
260 ...v2Projection,
261 runs: [
262 {
263 id: runId,
264 threadId: v2Projection.thread.id,
265 ordinal: 1,
266 providerInstanceId: v2Projection.thread.providerInstanceId,
267 modelSelection: v2Projection.thread.modelSelection,
268 providerThreadId: null,
269 userMessageId: MessageId.make("message-user"),
270 rootNodeId: null,
271 activeAttemptId: null,
272 status: "running" as const,
273 requestedAt: now,
274 startedAt: now,
275 completedAt: null,
276 checkpointId: null,
277 contextHandoffId: null,
278 },
279 ],
280 messages: [
281 {
282 id: MessageId.make("message-user"),
283 threadId: v2Projection.thread.id,
284 runId,
285 nodeId: null,
286 role: "user" as const,
287 text: "Hello",
288 attachments: [],
289 streaming: false,
290 createdBy: "user" as const,
291 creationSource: "web" as const,
292 createdAt: now,
293 updatedAt: now,
294 },
295 ],
296 updatedAt: now,
297 };
298​
299 expect(deriveLatestThreadRun(projection)).toMatchObject({
300 runId,
301 status: "running",
302 requestedAt: "2026-06-20T01:00:00.000Z",
303 assistantMessageId: null,
304 });
305 expect(deriveThreadRuntime(projection)).toMatchObject({
306 status: "running",
307 activeRunId: runId,
308 providerInstanceId: projection.thread.providerInstanceId,
309 });
310 for (const status of ["queued", "cancelled"] as const) {
311 const later = DateTime.add(now, { hours: 1 });
312 const latest = {
313 ...projection.runs[0]!,
314 id: RunId.make("newer-run"),
315 ordinal: 2,
316 status,
317 requestedAt: later,
318 startedAt: null,
319 completedAt: status === "cancelled" ? later : null,
320 };
321 const detail = { ...projection, runs: [...projection.runs, latest], updatedAt: later };
322 const shell = presentThreadShell(environmentId, {
323 ...v2ThreadShell,
324 latestRunId: latest.id,
325 latestRunStartedAt: null,
326 latestRunRequestedAt: later,
327 latestRunCompletedAt: latest.completedAt,
328 status,
329 activeRunId: runId,
330 activityRunStatus: "running",
331 activityRunStartedAt: now,
332 updatedAt: later,
333 });
334 expect(resolveThreadWorkingStartedAt(shell)).toBe(DateTime.formatIso(now));
335 expect(
336 resolveThreadWorkingStartedAt({
337 latestRun: deriveLatestThreadRun(detail),
338 runtime: deriveThreadRuntime(detail),
339 }),
340 ).toBe(resolveThreadWorkingStartedAt(shell));
341 const stopped = {
342 ...detail,
343 runs: detail.runs.map((run) => ({
344 ...run,
345 status: "completed" as const,
346 completedAt: later,
347 })),
348 };
349 expect(
350 resolveThreadWorkingStartedAt({
351 latestRun: deriveLatestThreadRun(stopped),
352 runtime: deriveThreadRuntime(stopped),
353 }),
354 ).toBeNull();
355 }
356 });
357​
358 it("parks waiting runtime for a post-settlement roster without hiding active running work", () => {
359 const runId = RunId.make("run-background-presentation");
360 const now = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
361 const run = {
362 id: runId,
363 threadId: v2Projection.thread.id,
364 ordinal: 1,
365 providerInstanceId: v2Projection.thread.providerInstanceId,
366 modelSelection: v2Projection.thread.modelSelection,
367 providerThreadId: null,
368 userMessageId: MessageId.make("message-background-presentation"),
369 rootNodeId: null,
370 activeAttemptId: null,
371 status: "waiting" as const,
372 requestedAt: now,
373 startedAt: now,
374 completedAt: null,
375 checkpointId: null,
376 contextHandoffId: null,
377 };
378 const backgroundItem = {
379 id: TurnItemId.make("item-background-subagent"),
380 threadId: v2Projection.thread.id,
381 runId,
382 nodeId: null,
383 providerThreadId: null,
384 providerTurnId: null,
385 nativeItemRef: null,
386 parentItemId: null,
387 ordinal: 0,
388 status: "running" as const,
389 title: "Background review",
390 startedAt: now,
391 completedAt: null,
392 updatedAt: now,
393 type: "subagent" as const,
394 subagentId: NodeId.make("subagent-review"),
395 origin: "provider_native" as const,
396 driver: ProviderDriverKind.make("codex"),
397 providerInstanceId: v2Projection.thread.providerInstanceId,
398 childThreadId: null,
399 prompt: "Review the changes",
400 result: null,
401 };
402​
403 expect(
404 deriveThreadRuntime({
405 ...v2Projection,
406 runs: [run],
407 turnItems: [backgroundItem],
408 }),
409 ).toMatchObject({
410 status: "idle",
411 activeRunId: null,
412 });
413 expect(
414 deriveThreadRuntime({
415 ...v2Projection,
416 runs: [{ ...run, status: "running" as const }],
417 turnItems: [backgroundItem],
418 }),
419 ).toMatchObject({
420 status: "running",
421 activeRunId: runId,
422 });
423 expect(
424 deriveThreadRuntime({
425 ...v2Projection,
426 runs: [{ ...run, status: "failed" as const, completedAt: now }],
427 turnItems: [backgroundItem],
428 }),
429 ).toMatchObject({ status: "failed", activeRunId: null });
430 const commandItem = {
431 ...backgroundItem,
432 id: TurnItemId.make("item-background-command"),
433 type: "command_execution" as const,
434 input: "npm run dev",
435 };
436 expect(
437 deriveThreadRuntime({ ...v2Projection, runs: [run], turnItems: [commandItem] }),
438 ).toMatchObject({ status: "waiting", activeRunId: null });
439​
440 for (const [turnItems, status] of [
441 [[commandItem], "completed"],
442 [[backgroundItem], "idle"],
443 [[commandItem, backgroundItem], "idle"],
444 ] as const) {
445 expect(
446 deriveThreadRuntime({
447 ...v2Projection,
448 runs: [{ ...run, status: "completed", completedAt: now }],
449 turnItems,
450 }),
451 ).toMatchObject({ status, activeRunId: null });
452 }
453​
454 for (const held of [{ kind: "monitor", awaited: true }, { kind: "background_task" }] as const) {
455 const providerThread = {
456 id: ProviderThreadId.make("provider-thread-background"),
457 driver: ProviderDriverKind.make("claudeCode"),
458 providerInstanceId: v2Projection.thread.providerInstanceId,
459 providerSessionId: null,
460 appThreadId: v2Projection.thread.id,
461 ownerNodeId: null,
462 nativeThreadRef: null,
463 nativeConversationHeadRef: null,
464 status: "idle" as const,
465 firstRunOrdinal: 1,
466 lastRunOrdinal: 1,
467 handoffIds: [],
468 forkedFrom: null,
469 pendingBackgroundTasks: [{ taskId: "background-task", ...held }],
470 createdAt: now,
471 updatedAt: now,
472 };
473 for (const status of ["completed", "failed"] as const) {
474 expect(
475 deriveThreadRuntime({
476 ...v2Projection,
477 runs: [{ ...run, status, completedAt: now }],
478 providerThreads: [providerThread],
479 }),
480 ).toMatchObject({ status: status === "failed" ? "failed" : "idle", activeRunId: null });
481 }
482 }
483 });
484​
485 it("joins pending request entities to their native turn-item display data", () => {
486 const now = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
487 const requestId = RuntimeRequestId.make("request-approval");
488 const item = {
489 id: TurnItemId.make("item-approval"),
490 threadId: v2Projection.thread.id,
491 runId: null,
492 nodeId: NodeId.make("node-root"),
493 providerThreadId: null,
494 providerTurnId: null,
495 nativeItemRef: null,
496 parentItemId: null,
497 ordinal: 0,
498 status: "completed" as const,
499 title: null,
500 startedAt: now,
501 completedAt: now,
502 updatedAt: now,
503 type: "approval_request" as const,
504 requestId,
505 requestKind: "command" as const,
506 prompt: "Allow command?",
507 };
508 const projection = {
509 ...v2Projection,
510 runtimeRequests: [
511 {
512 id: requestId,
513 nodeId: NodeId.make("node-root"),
514 providerTurnId: null,
515 nativeRequestRef: null,
516 kind: "command" as const,
517 status: "pending" as const,
518 responseCapability: {
519 type: "not_resumable" as const,
520 reason: "provider disconnected",
521 },
522 createdAt: now,
523 resolvedAt: null,
524 },
525 ],
526 turnItems: [item],
527 updatedAt: now,
528 };
529​
530 expect(derivePendingThreadRequests(projection).approvals).toEqual([
531 {
532 requestId,
533 requestKind: "command",
534 createdAt: "2026-06-20T01:00:00.000Z",
535 detail: "Allow command?",
536 responseCapability: "not_resumable",
537 },
538 ]);
539 expect(derivePendingThreadRequests(projection).userInputs).toEqual([]);
540 });
541​
542 it("preserves structured-question selection mode and defaults legacy payloads", () => {
543 const now = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
544 const multiRequestId = RuntimeRequestId.make("request-multi-select");
545 const legacyRequestId = RuntimeRequestId.make("request-legacy-single-select");
546 const runtimeRequests = [multiRequestId, legacyRequestId].map((id) => ({
547 id,
548 nodeId: NodeId.make(`node-${id}`),
549 providerTurnId: null,
550 nativeRequestRef: null,
551 kind: "user_input" as const,
552 status: "pending" as const,
553 responseCapability: {
554 type: "live" as const,
555 providerSessionId: ProviderSessionId.make("provider-session-questions"),
556 },
557 createdAt: now,
558 resolvedAt: null,
559 }));
560 const common = {
561 threadId: v2Projection.thread.id,
562 runId: null,
563 providerThreadId: null,
564 providerTurnId: null,
565 nativeItemRef: null,
566 parentItemId: null,
567 ordinal: 0,
568 status: "completed" as const,
569 title: null,
570 startedAt: now,
571 completedAt: now,
572 updatedAt: now,
573 type: "user_input_request" as const,
574 };
575 const projection = {
576 ...v2Projection,
577 runtimeRequests,
578 turnItems: [
579 {
580 ...common,
581 id: TurnItemId.make("item-multi-select"),
582 nodeId: NodeId.make("node-request-multi-select"),
583 requestId: multiRequestId,
584 questions: [
585 {
586 id: "regions",
587 header: "Regions",
588 question: "Which regions?",
589 options: [
590 { label: "US", description: "United States" },
591 { label: "EU", description: "European Union" },
592 ],
593 multiSelect: true,
594 },
595 ],
596 },
597 {
598 ...common,
599 id: TurnItemId.make("item-legacy-single-select"),
600 nodeId: NodeId.make("node-request-legacy-single-select"),
601 requestId: legacyRequestId,
602 questions: [
603 {
604 id: "target",
605 header: "Target",
606 question: "Which target?",
607 options: [{ label: "Staging", description: "Staging environment" }],
608 },
609 ],
610 },
611 ],
612 updatedAt: now,
613 };
614​
615 expect(
616 derivePendingThreadRequests(projection).userInputs.map((input) => ({
617 requestId: input.requestId,
618 multiSelect: input.questions[0]?.multiSelect,
619 })),
620 ).toEqual([
621 { requestId: multiRequestId, multiSelect: true },
622 { requestId: legacyRequestId, multiSelect: false },
623 ]);
624 });
625});