Open History to see the last commit
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 };
209}
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 };
234}
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-completed",
415 createdAt: "2026-04-01T00:00:02.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 "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-failed"),
750 kind: "tool.completed",
751 tone: "tool",
752 summary: "Run command",
753 createdAt: "2026-04-01T00:00:05.000Z",
754 turnId,
755 payload: {
756 title: "Run command",
757 itemType: "command_execution",
758 detail: "zsh: command not found: nope",
759 status: "completed",
760 },
761 }),
762 ],
763 });
764
765 const feed = buildThreadFeed(thread);
766 expect(deriveThreadFeedPresentation(feed, thread.latestTurn, new Set())).toEqual(feed);
767 expect(feed[0]).toMatchObject({
768 type: "activity-group",
769 activities: [{ status: "failure" }],
770 });
771 });
772
773 it("appends active work as a normal timeline row", () => {
774 const startedAt = "2026-04-01T00:00:01.000Z";
775 const presented = deriveThreadFeedPresentation([], null, new Set(), new Set(), startedAt);
776
777 expect(presented).toEqual([
778 {
779 type: "working",
780 id: "working-indicator-row",
781 createdAt: startedAt,
782 },
783 ]);
784 expect(deriveThreadFeedPresentation(presented, null, new Set())).toEqual([]);
785 });
786
787 it("models work-log overflow as list rows", () => {
788 const activity = (
789 id: string,
790 createdAt: string,
791 status: ThreadFeedActivity["status"] = "success",
792 ): ThreadFeedActivity => ({
793 id,
794 createdAt,
795 turnId: null,
796 summary: `Tool ${id}`,
797 detail: null,
798 canExpand: false,
799 getFullDetail: () => null,
800 getCopyText: () => id,
801 icon: "command",
802 toolLike: true,
803 status,
804 });
805 const feed: ThreadFeedEntry[] = [
806 {
807 type: "activity-group",
808 id: "work-group-1",
809 createdAt: "2026-04-01T00:00:01.000Z",
810 turnId: null,
811 activities: [
812 activity("activity-1", "2026-04-01T00:00:01.000Z"),
813 activity("activity-neutral", "2026-04-01T00:00:02.000Z", "neutral"),
814 activity("activity-2", "2026-04-01T00:00:03.000Z"),
815 activity("activity-3", "2026-04-01T00:00:04.000Z"),
816 ],
817 },
818 ];
819
820 const collapsed = deriveThreadFeedPresentation(feed, null, new Set());
821 expect(collapsed.map((entry) => entry.id)).toEqual(["activity-3", "work-toggle:work-group-1"]);
822 expect(collapsed[1]).toMatchObject({
823 type: "work-toggle",
824 groupId: "work-group-1",
825 hiddenCount: 2,
826 expanded: false,
827 });
828
829 const expanded = deriveThreadFeedPresentation(feed, null, new Set(), new Set(["work-group-1"]));
830 expect(expanded.map((entry) => entry.id)).toEqual([
831 "activity-1",
832 "activity-2",
833 "activity-3",
834 "work-toggle:work-group-1",
835 ]);
836 expect(expanded.at(-1)).toMatchObject({
837 type: "work-toggle",
838 expanded: true,
839 });
840 });
841});
842
843describe("quiet timeline: nested agents", () => {
844 it("keeps a nested agent's terminal row but hides its background work", () => {
845 const thread = makeThread({
846 id: ThreadId.make("thread-nested"),
847 projectId: ProjectId.make("project-1"),
848 title: "Nested agents",
849 activities: [
850 // A subagent's own shell: internal, covered by the owner's liveness.
851 makeActivity({
852 id: EventId.make("shell-done"),
853 kind: "task.completed",
854 summary: "Task completed",
855 createdAt: "2026-04-01T00:00:02.000Z",
856 payload: { taskId: "sh-1", agentId: "owner", agentKind: "background" },
857 }),
858 // A nested AGENT's completion: mobile has no Agents sheet, so this
859 // terminal row is the only signal it ever finished.
860 makeActivity({
861 id: EventId.make("nested-done"),
862 kind: "task.completed",
863 summary: "Task completed",
864 createdAt: "2026-04-01T00:00:03.000Z",
865 payload: { taskId: "n-1", agentId: "owner", agentKind: "agent" },
866 }),
867 ],
868 });
869
870 const feed = buildThreadFeed(thread);
871 const ids = feed.flatMap((entry) =>
872 entry.type === "activity-group" ? entry.activities.map((row) => row.id) : [],
873 );
874 expect(ids).toContain("nested-done");
875 expect(ids).not.toContain("shell-done");
876 });
877});