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

threadActivity.test.ts

1,093 lines33.2 KB

1import { describe, expect, it } from "vite-plus/test";
2import { codexFeedbackMessage } from "@t3tools/client-runtime/state/threads";
3​
4import {
5 EventId,
6 MessageId,
7 ProjectId,
8 ProviderInstanceId,
9 ThreadId,
10 TurnId,
11 type OrchestrationThread,
12 type OrchestrationThreadActivity,
13} from "@t3tools/contracts";
14​
15import {
16 buildPendingUserInputAnswers,
17 buildThreadFeed,
18 derivePendingApprovals,
19 deriveThreadFeedPresentation,
20 isPendingUserInputOptionSelected,
21 setPendingUserInputCustomAnswer,
22 togglePendingUserInputOptionSelection,
23 type ThreadFeedActivity,
24 type ThreadFeedEntry,
25} from "./threadActivity";
26​
27describe("Codex feedback pseudo-messages", () => {
28 it("keeps pending and completed feedback messages in the mobile thread body", () => {
29 const pending = {
30 id: MessageId.make("feedback-command"),
31 command: "/feedback The agent stopped early.",
32 createdAt: "2026-08-23T00:00:00.000Z",
33 status: "uploading" as const,
34 };
35 const entries = [codexFeedbackMessage(pending), codexFeedbackMessage(pending, "assistant")].map(
36 (message) => ({
37 type: "message" as const,
38 id: message.id,
39 createdAt: message.createdAt,
40 message,
41 }),
42 );
43​
44 expect(deriveThreadFeedPresentation(entries, null, new Set())).toEqual(entries);
45 expect(entries[1]?.message.text).toBe("Sending feedback to OpenAI...");
46​
47 const completed = codexFeedbackMessage(
48 { ...pending, status: "sent", feedbackId: "codex-thread-1" },
49 "assistant",
50 );
51 expect(completed.text).toContain("codex-thread-1");
52 });
53});
54​
55const singleSelectQuestion = {
56 id: "runtime",
57 header: "Runtime",
58 question: "Which runtime should be used?",
59 options: [
60 { label: "Go", description: "One binary" },
61 { label: "Node.js", description: "Reuse TypeScript" },
62 ],
63 multiSelect: false,
64} as const;
65​
66const multiSelectQuestion = {
67 id: "scope",
68 header: "Scope",
69 question: "Which data should be collected?",
70 options: [
71 { label: "Orders", description: "Receipts" },
72 { label: "Listings", description: "Inventory" },
73 ],
74 multiSelect: true,
75} as const;
76​
77describe("pending user input answers", () => {
78 it("replaces single-select options and toggles multi-select options", () => {
79 expect(
80 togglePendingUserInputOptionSelection(
81 singleSelectQuestion,
82 { selectedOptionLabels: ["Go"] },
83 "Node.js",
84 ),
85 ).toEqual({ customAnswer: "", selectedOptionLabels: ["Node.js"] });
86​
87 const orders = togglePendingUserInputOptionSelection(multiSelectQuestion, undefined, "Orders");
88 const ordersAndListings = togglePendingUserInputOptionSelection(
89 multiSelectQuestion,
90 orders,
91 "Listings",
92 );
93 expect(ordersAndListings).toEqual({
94 customAnswer: "",
95 selectedOptionLabels: ["Orders", "Listings"],
96 });
97 expect(
98 togglePendingUserInputOptionSelection(multiSelectQuestion, ordersAndListings, "Orders"),
99 ).toEqual({ customAnswer: "", selectedOptionLabels: ["Listings"] });
100​
101 const paddedOrders = togglePendingUserInputOptionSelection(
102 multiSelectQuestion,
103 undefined,
104 " Orders ",
105 );
106 expect(paddedOrders).toEqual({ customAnswer: "", selectedOptionLabels: ["Orders"] });
107 expect(
108 togglePendingUserInputOptionSelection(multiSelectQuestion, paddedOrders, " Orders "),
109 ).toEqual({ customAnswer: "" });
110 });
111​
112 it("builds array answers for multi-select questions", () => {
113 expect(
114 buildPendingUserInputAnswers([singleSelectQuestion, multiSelectQuestion], {
115 runtime: { selectedOptionLabels: ["Go"] },
116 scope: { selectedOptionLabels: ["Orders", "Listings"] },
117 }),
118 ).toEqual({
119 runtime: "Go",
120 scope: ["Orders", "Listings"],
121 });
122 });
123​
124 it("clears selected options while a custom answer is active", () => {
125 expect(
126 setPendingUserInputCustomAnswer(
127 { selectedOptionLabels: ["Orders", "Listings"] },
128 "Orders first",
129 ),
130 ).toEqual({ customAnswer: "Orders first" });
131 });
132​
133 it("matches selected chips against normalized option labels", () => {
134 expect(
135 isPendingUserInputOptionSelected({ selectedOptionLabels: ["Orders"] }, " Orders "),
136 ).toBe(true);
137 expect(
138 isPendingUserInputOptionSelected(
139 { selectedOptionLabels: ["Orders"], customAnswer: "Orders first" },
140 " Orders ",
141 ),
142 ).toBe(false);
143 });
144});
145​
146describe("pending approvals", () => {
147 it("keeps app access approvals and persistence choices from remote environments", () => {
148 const options = [
149 { decision: "decline", label: "Decline" },
150 { decision: "acceptAlways", label: "Always allow Safari" },
151 { decision: "accept", label: "Approve" },
152 ];
153 const activity = makeActivity({
154 id: EventId.make("approval-safari"),
155 kind: "approval.requested",
156 summary: "App access approval requested",
157 createdAt: "2026-08-24T00:00:00.000Z",
158 payload: {
159 requestId: "req-safari",
160 requestType: "mcp_elicitation_approval",
161 detail: "Allow ChatGPT to use Safari?",
162 appName: "Safari",
163 options,
164 },
165 });
166​
167 expect(derivePendingApprovals([activity])).toEqual([
168 {
169 requestId: "req-safari",
170 requestKind: "mcp-elicitation",
171 createdAt: "2026-08-24T00:00:00.000Z",
172 detail: "Allow ChatGPT to use Safari?",
173 appName: "Safari",
174 options,
175 },
176 ]);
177 });
178​
179 it("removes an app access approval after a remote client rejects it", () => {
180 const requested = makeActivity({
181 id: EventId.make("approval-safari-open"),
182 kind: "approval.requested",
183 summary: "App access approval requested",
184 createdAt: "2026-08-24T00:00:00.000Z",
185 payload: { requestId: "req-safari", requestKind: "mcp-elicitation" },
186 });
187 const resolved = makeActivity({
188 id: EventId.make("approval-safari-resolved"),
189 kind: "approval.resolved",
190 summary: "Approval resolved",
191 createdAt: "2026-08-24T00:00:01.000Z",
192 payload: { requestId: "req-safari", decision: "decline" },
193 });
194​
195 expect(derivePendingApprovals([requested, resolved])).toEqual([]);
196 });
197});
198​
199function makeActivity(
200 input: Partial<OrchestrationThreadActivity> &
201 Pick<OrchestrationThreadActivity, "id" | "kind" | "summary" | "createdAt">,
202): OrchestrationThreadActivity {
203 return {
204 tone: "info",
205 payload: {},
206 turnId: null,
207 ...input,
208 };
210​
211function makeThread(
212 input: Partial<OrchestrationThread> & Pick<OrchestrationThread, "id" | "projectId" | "title">,
213): OrchestrationThread {
214 return {
215 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
216 runtimeMode: "full-access",
217 interactionMode: "default",
218 branch: null,
219 worktreePath: null,
220 latestTurn: null,
221 createdAt: "2026-04-01T00:00:00.000Z",
222 updatedAt: "2026-04-01T00:00:00.000Z",
223 archivedAt: null,
224 deletedAt: null,
225 messages: [],
226 proposedPlans: [],
227 activities: [],
228 checkpoints: [],
229 session: null,
230 ...input,
231 settledOverride: input.settledOverride ?? null,
232 settledAt: input.settledAt ?? null,
233 };
235​
236describe("buildThreadFeed", () => {
237 it("keeps older local feedback before newer messages returned by the server", () => {
238 const submission = {
239 id: MessageId.make("feedback-command-ordering"),
240 command: "/feedback The agent stopped early.",
241 createdAt: "2026-08-23T00:00:01.000Z",
242 status: "sent" as const,
243 feedbackId: "codex-thread-1",
244 };
245 const laterMessage = {
246 id: MessageId.make("later-server-message"),
247 role: "assistant" as const,
248 text: "Newer server response",
249 turnId: null,
250 createdAt: "2026-08-23T00:00:02.000Z",
251 updatedAt: "2026-08-23T00:00:02.000Z",
252 streaming: false,
253 };
254 const thread = makeThread({
255 id: ThreadId.make("thread-feedback-ordering"),
256 projectId: ProjectId.make("project-1"),
257 title: "Feedback ordering",
258 messages: [laterMessage],
259 });
260​
261 const feed = buildThreadFeed(thread, {
262 localMessages: [
263 codexFeedbackMessage(submission),
264 codexFeedbackMessage(submission, "assistant"),
265 ],
266 });
267​
268 expect(feed.map((entry) => entry.id)).toEqual([
269 "feedback-command-ordering",
270 "feedback-command-ordering:feedback",
271 "later-server-message",
272 ]);
273 });
274​
275 it("keeps historic work entries attributed to their turns", () => {
276 const thread = makeThread({
277 id: ThreadId.make("thread-1"),
278 projectId: ProjectId.make("project-1"),
279 title: "Runtime warning thread",
280 latestTurn: {
281 turnId: TurnId.make("turn-latest"),
282 state: "running",
283 requestedAt: "2026-04-01T00:00:00.000Z",
284 startedAt: "2026-04-01T00:00:01.000Z",
285 completedAt: null,
286 assistantMessageId: null,
287 },
288 activities: [
289 makeActivity({
290 id: EventId.make("activity-old"),
291 kind: "runtime.warning",
292 summary: "Runtime warning",
293 createdAt: "2026-04-01T00:00:02.000Z",
294 turnId: TurnId.make("turn-old"),
295 payload: {
296 message: "Old warning",
297 },
298 }),
299 makeActivity({
300 id: EventId.make("activity-latest"),
301 kind: "runtime.warning",
302 summary: "Runtime warning",
303 createdAt: "2026-04-01T00:00:03.000Z",
304 turnId: TurnId.make("turn-latest"),
305 payload: {
306 message: "Latest warning",
307 },
308 }),
309 ],
310 });
311​
312 const feed = buildThreadFeed(thread);
313 expect(feed).toMatchObject([
314 {
315 type: "activity-group",
316 turnId: "turn-old",
317 activities: [{ id: "activity-old", turnId: "turn-old" }],
318 },
319 {
320 type: "activity-group",
321 turnId: "turn-latest",
322 activities: [{ id: "activity-latest", turnId: "turn-latest" }],
323 },
324 ]);
325 });
326​
327 it("drops runtime warnings with no displayable content", () => {
328 const thread = makeThread({
329 id: ThreadId.make("thread-noise"),
330 projectId: ProjectId.make("project-1"),
331 title: "Warning noise thread",
332 activities: [
333 makeActivity({
334 id: EventId.make("activity-noise"),
335 kind: "runtime.warning",
336 summary: "Claude system message 'background_tasks_changed' (no displayable text content)",
337 createdAt: "2026-04-01T00:00:02.000Z",
338 turnId: TurnId.make("turn-1"),
339 }),
340 makeActivity({
341 id: EventId.make("activity-signal"),
342 kind: "runtime.warning",
343 summary: "Reconnecting... 2/5",
344 createdAt: "2026-04-01T00:00:03.000Z",
345 turnId: TurnId.make("turn-1"),
346 }),
347 ],
348 });
349​
350 const feed = buildThreadFeed(thread);
351 expect(feed).toMatchObject([
352 {
353 type: "activity-group",
354 activities: [{ id: "activity-signal" }],
355 },
356 ]);
357 });
358​
359 it("collapses matching tool lifecycle rows like desktop", () => {
360 const thread = makeThread({
361 id: ThreadId.make("thread-2"),
362 projectId: ProjectId.make("project-1"),
363 title: "Collapsed tools",
364 latestTurn: {
365 turnId: TurnId.make("turn-1"),
366 state: "completed",
367 requestedAt: "2026-04-01T00:00:00.000Z",
368 startedAt: "2026-04-01T00:00:01.000Z",
369 completedAt: "2026-04-01T00:00:03.000Z",
370 assistantMessageId: null,
371 },
372 activities: [
373 makeActivity({
374 id: EventId.make("tool-updated"),
375 kind: "tool.updated",
376 tone: "tool",
377 summary: "Run tests",
378 createdAt: "2026-04-01T00:00:01.000Z",
379 turnId: TurnId.make("turn-1"),
380 payload: {
381 title: "Run tests",
382 itemType: "command_execution",
383 detail: "/bin/zsh -lc 'bun run test'",
384 },
385 }),
386 makeActivity({
387 id: EventId.make("tool-completed"),
388 kind: "tool.completed",
389 tone: "tool",
390 summary: "Run tests completed",
391 createdAt: "2026-04-01T00:00:02.000Z",
392 turnId: TurnId.make("turn-1"),
393 payload: {
394 title: "Run tests",
395 itemType: "command_execution",
396 detail: "/bin/zsh -lc 'bun run test'",
397 },
398 }),
399 ],
400 });
401​
402 const feed = buildThreadFeed(thread);
403 const group = feed[0];
404​
405 expect(group).toMatchObject({
406 type: "activity-group",
407 });
408 if (!group || group.type !== "activity-group") {
409 return;
410 }
411​
412 expect(group.activities).toHaveLength(1);
413 expect(group.activities[0]).toMatchObject({
414 id: "tool-updated",
415 createdAt: "2026-04-01T00:00:01.000Z",
416 turnId: "turn-1",
417 summary: "Run tests",
418 detail: "bun run test",
419 canExpand: true,
420 icon: "command",
421 toolLike: true,
422 status: "success",
423 });
424 expect(group.activities[0]?.getFullDetail()).toBe("/bin/zsh -lc 'bun run test'");
425 expect(group.activities[0]?.getCopyText()).toBe(
426 "Run tests\nbun run test\n/bin/zsh -lc 'bun run test'",
427 );
428 });
429​
430 it("keeps MCP inputs available to expanded mobile work rows", () => {
431 const turnId = TurnId.make("turn-mcp");
432 const thread = makeThread({
433 id: ThreadId.make("thread-mcp"),
434 projectId: ProjectId.make("project-1"),
435 title: "Expandable MCP call",
436 latestTurn: {
437 turnId,
438 state: "completed",
439 requestedAt: "2026-04-01T00:00:00.000Z",
440 startedAt: "2026-04-01T00:00:01.000Z",
441 completedAt: "2026-04-01T00:00:03.000Z",
442 assistantMessageId: null,
443 },
444 activities: [
445 makeActivity({
446 id: EventId.make("mcp-completed"),
447 kind: "tool.completed",
448 tone: "tool",
449 summary: "Call repository tool",
450 createdAt: "2026-04-01T00:00:02.000Z",
451 turnId,
452 payload: {
453 title: "Call repository tool",
454 itemType: "mcp_tool_call",
455 detail: "repository.search",
456 status: "completed",
457 data: {
458 item: {
459 server: "repository",
460 tool: "search",
461 arguments: { query: "work log" },
462 },
463 },
464 },
465 }),
466 ],
467 });
468​
469 const group = buildThreadFeed(thread)[0];
470 expect(group).toMatchObject({ type: "activity-group" });
471 if (!group || group.type !== "activity-group") {
472 return;
473 }
474​
475 expect(group.activities[0]?.icon).toBe("wrench");
476 expect(group.activities[0]?.getFullDetail()).toContain('"query": "work log"');
477 expect(group.activities[0]?.getFullDetail()).toContain("repository.search");
478 });
479​
480 it("defers large tool output expansion until a work row is opened or copied", () => {
481 let serializedToolOutputs = 0;
482 const activities = Array.from({ length: 5_000 }, (_, index) =>
483 makeActivity({
484 id: EventId.make(`large-tool-${index}`),
485 kind: "tool.completed",
486 tone: "tool",
487 summary: `Tool ${index}`,
488 createdAt: new Date(Date.UTC(2026, 3, 1, 0, 0, index)).toISOString(),
489 payload: {
490 title: `Tool ${index}`,
491 itemType: "mcp_tool_call",
492 status: "completed",
493 data: {
494 item: {
495 toJSON: () => {
496 serializedToolOutputs += 1;
497 return { output: "x".repeat(32_768) };
498 },
499 },
500 },
501 },
502 }),
503 );
504 const thread = makeThread({
505 id: ThreadId.make("thread-large-tools"),
506 projectId: ProjectId.make("project-1"),
507 title: "Large tools",
508 activities,
509 });
510​
511 const feed = buildThreadFeed(thread);
512 expect(serializedToolOutputs).toBe(0);
513​
514 const group = feed[0];
515 expect(group).toMatchObject({ type: "activity-group" });
516 if (!group || group.type !== "activity-group") {
517 return;
518 }
519​
520 expect(group.activities).toHaveLength(5_000);
521 expect(group.activities[0]?.getFullDetail()).toContain('"output"');
522 expect(serializedToolOutputs).toBe(1);
523 expect(group.activities[0]?.getCopyText()).toContain('"output"');
524 expect(serializedToolOutputs).toBe(1);
525 });
526​
527 it("keeps the first and terminal assistant messages visible around settled work", () => {
528 const turnId = TurnId.make("turn-1");
529 const thread = makeThread({
530 id: ThreadId.make("thread-3"),
531 projectId: ProjectId.make("project-1"),
532 title: "Folded work",
533 latestTurn: {
534 turnId,
535 state: "completed",
536 requestedAt: "2026-04-01T00:00:00.000Z",
537 startedAt: "2026-04-01T00:00:01.000Z",
538 completedAt: "2026-04-01T00:00:18.000Z",
539 assistantMessageId: MessageId.make("assistant-final"),
540 },
541 messages: [
542 {
543 id: MessageId.make("assistant-first"),
544 role: "assistant",
545 text: "Synthetic deployment checklist\n1. Confirm the deployment is ready.",
546 turnId,
547 streaming: false,
548 createdAt: "2026-04-01T00:00:02.000Z",
549 updatedAt: "2026-04-01T00:00:03.000Z",
550 },
551 {
552 id: MessageId.make("assistant-final"),
553 role: "assistant",
554 text: "Done.",
555 turnId,
556 streaming: false,
557 createdAt: "2026-04-01T00:00:17.000Z",
558 updatedAt: "2026-04-01T00:00:18.000Z",
559 },
560 ],
561 activities: [
562 makeActivity({
563 id: EventId.make("tool-completed"),
564 kind: "tool.completed",
565 tone: "tool",
566 summary: "Read files",
567 createdAt: "2026-04-01T00:00:05.000Z",
568 turnId,
569 payload: {
570 title: "Read files",
571 itemType: "file_read",
572 status: "completed",
573 },
574 }),
575 ],
576 });
577​
578 const feed = buildThreadFeed(thread);
579 const collapsed = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set());
580 expect(collapsed.map((entry) => entry.id)).toEqual([
581 "assistant-first",
582 "turn-fold:turn-1",
583 "assistant-final",
584 ]);
585 expect(collapsed[1]).toMatchObject({
586 type: "turn-fold",
587 label: "Worked for 17s",
588 expanded: false,
589 });
590​
591 const expanded = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set([turnId]));
592 expect(expanded.map((entry) => entry.id)).toEqual([
593 "assistant-first",
594 "turn-fold:turn-1",
595 "work-toggle:work-group:tool-completed",
596 "assistant-final",
597 ]);
598 });
599​
600 it("folds assistant messages between the first and terminal messages", () => {
601 const turnId = TurnId.make("turn-1");
602 const thread = makeThread({
603 id: ThreadId.make("thread-middle-message"),
604 projectId: ProjectId.make("project-1"),
605 title: "Bounded narration",
606 latestTurn: {
607 turnId,
608 state: "completed",
609 requestedAt: "2026-04-01T00:00:00.000Z",
610 startedAt: "2026-04-01T00:00:01.000Z",
611 completedAt: "2026-04-01T00:00:06.000Z",
612 assistantMessageId: MessageId.make("assistant-final"),
613 },
614 messages: [
615 {
616 id: MessageId.make("assistant-first"),
617 role: "assistant",
618 text: "The main result is ready.",
619 turnId,
620 streaming: false,
621 createdAt: "2026-04-01T00:00:01.000Z",
622 updatedAt: "2026-04-01T00:00:02.000Z",
623 },
624 {
625 id: MessageId.make("assistant-middle"),
626 role: "assistant",
627 text: "I am checking one more detail.",
628 turnId,
629 streaming: false,
630 createdAt: "2026-04-01T00:00:03.000Z",
631 updatedAt: "2026-04-01T00:00:04.000Z",
632 },
633 {
634 id: MessageId.make("assistant-final"),
635 role: "assistant",
636 text: "Verification finished.",
637 turnId,
638 streaming: false,
639 createdAt: "2026-04-01T00:00:05.000Z",
640 updatedAt: "2026-04-01T00:00:06.000Z",
641 },
642 ],
643 });
644​
645 const feed = buildThreadFeed(thread);
646 const rows = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set());
647​
648 expect(rows.map((entry) => entry.id)).toEqual([
649 "assistant-first",
650 "turn-fold:turn-1",
651 "assistant-final",
652 ]);
653 });
654​
655 it("measures a steer-superseded turn from its user boundary through trailing work", () => {
656 const firstTurnId = TurnId.make("turn-1");
657 const secondTurnId = TurnId.make("turn-2");
658 const thread = makeThread({
659 id: ThreadId.make("thread-steered"),
660 projectId: ProjectId.make("project-1"),
661 title: "Steered work",
662 latestTurn: {
663 turnId: secondTurnId,
664 state: "running",
665 requestedAt: "2026-04-01T00:00:14.000Z",
666 startedAt: "2026-04-01T00:00:14.000Z",
667 completedAt: null,
668 assistantMessageId: MessageId.make("assistant-next"),
669 },
670 messages: [
671 {
672 id: MessageId.make("user-1"),
673 role: "user",
674 text: "Do it once more.",
675 turnId: null,
676 streaming: false,
677 createdAt: "2026-04-01T00:00:00.000Z",
678 updatedAt: "2026-04-01T00:00:00.000Z",
679 },
680 {
681 id: MessageId.make("assistant-commentary"),
682 role: "assistant",
683 text: "Kicking off call 1.",
684 turnId: firstTurnId,
685 streaming: false,
686 createdAt: "2026-04-01T00:00:09.000Z",
687 updatedAt: "2026-04-01T00:00:09.000Z",
688 },
689 {
690 id: MessageId.make("user-2"),
691 role: "user",
692 text: "Actually do 15.",
693 turnId: null,
694 streaming: false,
695 createdAt: "2026-04-01T00:00:14.000Z",
696 updatedAt: "2026-04-01T00:00:14.000Z",
697 },
698 {
699 id: MessageId.make("assistant-next"),
700 role: "assistant",
701 text: "One down - adjusting.",
702 turnId: secondTurnId,
703 streaming: true,
704 createdAt: "2026-04-01T00:00:17.000Z",
705 updatedAt: "2026-04-01T00:00:17.000Z",
706 },
707 ],
708 activities: [
709 makeActivity({
710 id: EventId.make("work-1"),
711 kind: "tool.completed",
712 tone: "tool",
713 summary: "Ran command",
714 createdAt: "2026-04-01T00:00:12.000Z",
715 turnId: firstTurnId,
716 payload: {
717 title: "Ran command",
718 itemType: "command_execution",
719 status: "completed",
720 },
721 }),
722 ],
723 });
724​
725 const feed = buildThreadFeed(thread);
726 const collapsed = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set());
727 expect(collapsed.find((entry) => entry.type === "turn-fold")).toMatchObject({
728 turnId: firstTurnId,
729 label: "Worked for 12s",
730 });
731 });
732​
733 it("keeps an active turn expanded and classifies error-shaped tool output", () => {
734 const turnId = TurnId.make("turn-running");
735 const thread = makeThread({
736 id: ThreadId.make("thread-4"),
737 projectId: ProjectId.make("project-1"),
738 title: "Running work",
739 latestTurn: {
740 turnId,
741 state: "running",
742 requestedAt: "2026-04-01T00:00:00.000Z",
743 startedAt: "2026-04-01T00:00:01.000Z",
744 completedAt: null,
745 assistantMessageId: null,
746 },
747 activities: [
748 makeActivity({
749 id: EventId.make("tool-succeeded"),
750 kind: "tool.completed",
751 tone: "tool",
752 summary: "Run command",
753 createdAt: "2026-04-01T00:00:04.000Z",
754 turnId,
755 payload: {
756 title: "Run command",
757 itemType: "command_execution",
758 detail: "done",
759 status: "completed",
760 },
761 }),
762 makeActivity({
763 id: EventId.make("tool-failed"),
764 kind: "tool.completed",
765 tone: "tool",
766 summary: "Run command",
767 createdAt: "2026-04-01T00:00:05.000Z",
768 turnId,
769 payload: {
770 title: "Run command",
771 itemType: "command_execution",
772 detail: "zsh: command not found: nope",
773 status: "completed",
774 },
775 }),
776 ],
777 });
778​
779 const feed = buildThreadFeed(thread);
780 expect(deriveThreadFeedPresentation(feed, thread.latestTurn, new Set())).toMatchObject([
781 {
782 type: "work-toggle",
783 summary: "Ran 2 commands",
784 hiddenCount: 2,
785 hasFailure: true,
786 },
787 ]);
788 expect(feed[0]).toMatchObject({
789 type: "activity-group",
790 activities: [{ status: "success" }, { status: "failure" }],
791 });
792 expect(
793 deriveThreadFeedPresentation(
794 feed,
795 thread.latestTurn,
796 new Set(),
797 new Set(["work-group:tool-succeeded"]),
798 ).map((entry) => entry.id),
799 ).toEqual(["work-toggle:work-group:tool-succeeded", "tool-succeeded", "tool-failed"]);
800 });
801​
802 it("models work-log overflow as list rows", () => {
803 const activity = (
804 id: string,
805 createdAt: string,
806 status: ThreadFeedActivity["status"] = "success",
807 ): ThreadFeedActivity => ({
808 id,
809 createdAt,
810 turnId: null,
811 summary: `Tool ${id}`,
812 detail: null,
813 canExpand: false,
814 getFullDetail: () => null,
815 getCopyText: () => id,
816 icon: "command",
817 toolLike: true,
818 status,
819 workEntry: {
820 id,
821 createdAt,
822 turnId: null,
823 label: `Tool ${id}`,
824 command: `command ${id}`,
825 tone: "tool",
826 },
827 });
828 const feed: ThreadFeedEntry[] = [
829 {
830 type: "activity-group",
831 id: "work-group-1",
832 createdAt: "2026-04-01T00:00:01.000Z",
833 turnId: null,
834 activities: [
835 activity("activity-1", "2026-04-01T00:00:01.000Z"),
836 activity("activity-neutral", "2026-04-01T00:00:02.000Z", "neutral"),
837 activity("activity-2", "2026-04-01T00:00:03.000Z"),
838 activity("activity-3", "2026-04-01T00:00:04.000Z"),
839 ],
840 },
841 ];
842​
843 const collapsed = deriveThreadFeedPresentation(feed, null, new Set());
844 expect(collapsed.map((entry) => entry.id)).toEqual(["work-toggle:work-group:activity-1"]);
845 expect(collapsed[0]).toMatchObject({
846 type: "work-toggle",
847 groupId: "work-group:activity-1",
848 hiddenCount: 3,
849 expanded: false,
850 summary: "Ran 3 commands",
851 });
852​
853 const expanded = deriveThreadFeedPresentation(
854 feed,
855 null,
856 new Set(),
857 new Set(["work-group:activity-1"]),
858 );
859 expect(expanded.map((entry) => entry.id)).toEqual([
860 "work-toggle:work-group:activity-1",
861 "activity-1",
862 "activity-2",
863 "activity-3",
864 ]);
865 expect(expanded[0]).toMatchObject({
866 type: "work-toggle",
867 expanded: true,
868 });
869 });
870​
871 it("keeps live state on the active uninterrupted tool run", () => {
872 const turnId = TurnId.make("turn-live-tools");
873 const activity = (
874 id: string,
875 status: ThreadFeedActivity["status"],
876 lifecycleStatus: ThreadFeedActivity["lifecycleStatus"],
877 tone: "tool" | "error" = "tool",
878 command?: string,
879 ): ThreadFeedActivity => ({
880 id,
881 createdAt: `2026-04-01T00:00:0${id.at(-1)}.000Z`,
882 turnId,
883 summary: `Tool ${id}`,
884 detail: null,
885 canExpand: false,
886 getFullDetail: () => null,
887 getCopyText: () => id,
888 icon: "command",
889 toolLike: true,
890 status,
891 lifecycleStatus,
892 workEntry: {
893 id,
894 createdAt: `2026-04-01T00:00:0${id.at(-1)}.000Z`,
895 turnId,
896 label: `Tool ${id}`,
897 tone,
898 toolLifecycleStatus: lifecycleStatus,
899 ...(command ? { command, itemType: "command_execution" as const } : {}),
900 },
901 });
902 const feed: ThreadFeedEntry[] = [
903 {
904 type: "activity-group",
905 id: "activity-1",
906 createdAt: "2026-04-01T00:00:01.000Z",
907 turnId,
908 activities: [
909 activity("activity-1", "success", "completed"),
910 activity("activity-2", "failure", "failed", "error"),
911 activity("activity-3", "success", "completed", "tool", "sudo -u root pnpm test"),
912 ],
913 },
914 ];
915 const latestTurn = {
916 turnId,
917 state: "running" as const,
918 requestedAt: "2026-04-01T00:00:00.000Z",
919 startedAt: "2026-04-01T00:00:00.000Z",
920 completedAt: null,
921 assistantMessageId: null,
922 };
923​
924 const rows = deriveThreadFeedPresentation(
925 feed,
926 latestTurn,
927 new Set(),
928 new Set(),
929 latestTurn.startedAt,
930 );
931 expect(rows.slice(0, 3).map((entry) => [entry.id, entry.type])).toEqual([
932 ["work-toggle:work-group:activity-1", "work-toggle"],
933 ["activity-2", "activity-group"],
934 ["work-live:work-group:activity-3", "work-toggle"],
935 ]);
936 expect(rows.slice(0, 3).map((entry) => entry.type === "work-toggle" && entry.live)).toEqual([
937 false,
938 false,
939 true,
940 ]);
941 expect(rows[2]).toMatchObject({
942 summary: "Running pnpm",
943 summaryKind: "command",
944 live: true,
945 shimmer: false,
946 });
947 });
948​
949 it("does not revive cached in-progress tools after work stops", () => {
950 const turnId = TurnId.make("turn-stale-tool");
951 const feed: ThreadFeedEntry[] = [
952 {
953 type: "activity-group",
954 id: "stale-tool",
955 createdAt: "2026-04-01T00:00:01.000Z",
956 turnId,
957 activities: [
958 {
959 id: "stale-tool",
960 createdAt: "2026-04-01T00:00:01.000Z",
961 turnId,
962 summary: "Running tests",
963 detail: null,
964 canExpand: false,
965 getFullDetail: () => null,
966 getCopyText: () => "",
967 icon: "command",
968 toolLike: true,
969 status: "neutral",
970 lifecycleStatus: "inProgress",
971 workEntry: {
972 id: "stale-tool",
973 createdAt: "2026-04-01T00:00:01.000Z",
974 turnId,
975 label: "Running tests",
976 tone: "tool",
977 toolLifecycleStatus: "inProgress",
978 },
979 },
980 ],
981 },
982 ];
983 const latestTurn = {
984 turnId,
985 state: "running" as const,
986 requestedAt: "2026-04-01T00:00:00.000Z",
987 startedAt: "2026-04-01T00:00:00.000Z",
988 completedAt: null,
989 assistantMessageId: null,
990 };
991​
992 expect(deriveThreadFeedPresentation(feed, latestTurn, new Set())).toEqual([]);
993 expect(
994 deriveThreadFeedPresentation(feed, latestTurn, new Set(), new Set(), latestTurn.startedAt),
995 ).toMatchObject([{ type: "work-toggle", live: true, shimmer: true }]);
996 });
997​
998 it("collapses interleaved tool lifecycles by call identity", () => {
999 const turnId = TurnId.make("turn-parallel-tools");
1000 const toolActivity = (
1001 id: string,
1002 toolCallId: string,
1003 kind: "tool.updated" | "tool.completed",
1004 status: "inProgress" | "completed",
1005 detail: string,
1006 nestedId = false,
1007 ) =>
1008 makeActivity({
1009 id: EventId.make(id),
1010 kind,
1011 tone: "tool",
1012 summary: `Run ${toolCallId} command`,
1013 createdAt: `2026-04-01T00:00:0${id.at(-1)}.000Z`,
1014 turnId,
1015 payload: {
1016 ...(nestedId ? { data: { toolCallId } } : { toolCallId }),
1017 itemType: "command_execution",
1018 status,
1019 detail,
1020 },
1021 });
1022 const thread = makeThread({
1023 id: ThreadId.make("thread-parallel-tools"),
1024 projectId: ProjectId.make("project-1"),
1025 title: "Parallel tools",
1026 activities: [
1027 toolActivity("call-a-1", "call-a", "tool.updated", "inProgress", "starting"),
1028 toolActivity("call-b-2", "call-b", "tool.updated", "inProgress", "starting", true),
1029 toolActivity("call-a-3", "call-a", "tool.completed", "completed", "first output"),
1030 toolActivity("call-b-4", "call-b", "tool.completed", "completed", "second output", true),
1031 ],
1032 });
1033​
1034 const feed = buildThreadFeed(thread);
1035 const activityGroup = feed.find((entry) => entry.type === "activity-group");
1036 expect(activityGroup).toMatchObject({
1037 type: "activity-group",
1038 activities: [
1039 { id: "call-a-1", lifecycleStatus: "completed", detail: "first output" },
1040 { id: "call-b-2", lifecycleStatus: "completed", detail: "second output" },
1041 ],
1042 });
1043 expect(
1044 deriveThreadFeedPresentation(feed, null, new Set([turnId])).find(
1045 (entry) => entry.type === "work-toggle",
1046 ),
1047 ).toMatchObject({
1048 type: "work-toggle",
1049 hiddenCount: 2,
1050 summary: "Ran 2 commands",
1051 live: false,
1052 });
1053 });
1054});
1055​
1056describe("quiet timeline: nested agents", () => {
1057 it("keeps a nested agent's terminal row but hides its background work", () => {
1058 const thread = makeThread({
1059 id: ThreadId.make("thread-nested"),
1060 projectId: ProjectId.make("project-1"),
1061 title: "Nested agents",
1062 activities: [
1063 // A subagent's own shell: internal, covered by the owner's liveness.
1064 makeActivity({
1065 id: EventId.make("shell-done"),
1066 kind: "task.completed",
1067 summary: "Task completed",
1068 createdAt: "2026-04-01T00:00:02.000Z",
1069 payload: { taskId: "sh-1", agentId: "owner", agentKind: "background" },
1070 }),
1071 // A nested AGENT's completion: mobile has no Agents sheet, so this
1072 // terminal row is the only signal it ever finished.
1073 makeActivity({
1074 id: EventId.make("nested-done"),
1075 kind: "task.completed",
1076 summary: "Task completed",
1077 createdAt: "2026-04-01T00:00:03.000Z",
1078 payload: { taskId: "n-1", agentId: "owner", agentKind: "agent" },
1079 }),
1080 ],
1081 });
1082​
1083 const feed = buildThreadFeed(thread);
1084 const ids = feed.flatMap((entry) =>
1085 entry.type === "activity-group" ? entry.activities.map((row) => row.id) : [],
1086 );
1087 expect(ids).toContain("nested-done");
1088 expect(ids).not.toContain("shell-done");
1089 expect(deriveThreadFeedPresentation(feed, null, new Set())).toMatchObject([
1090 { type: "activity-group", id: "nested-done" },
1091 ]);
1092 });
1093});