Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { ThreadId, TurnItemId, type OrchestrationV2TurnItem } from "@t3tools/contracts";
4import * as DateTime from "effect/DateTime";
5import { T3_MCP_TOOL_NAMES } from "@t3tools/shared/t3McpToolPresentation";
6
7import {
8 commandDetailRepeatsCommand,
9 extractCommandOutputText,
10 liveThoughtLine,
11 resolveViewedImageAsset,
12 resolveWorkEntryToolPresentation,
13 summarizeToolGroup,
14 toolGroupAction,
15 toolGroupSummaryKind,
16 toolItemForDisplay,
17 type WorkLogPresentationEntry,
18 type WorkLogToolLifecycleStatus,
19 workEntryViewedImagePath,
20 workEntryIndicatesToolFailure,
21 workEntryDisplayIndicatesToolFailure,
22 workEntryIndicatesToolSuccess,
23} from "./presentation.js";
24
25function commandItem(
26 fields: Partial<Extract<OrchestrationV2TurnItem, { type: "command_execution" }>> = {},
27): OrchestrationV2TurnItem {
28 return {
29 id: TurnItemId.make("command"),
30 threadId: ThreadId.make("thread"),
31 runId: null,
32 nodeId: null,
33 providerThreadId: null,
34 providerTurnId: null,
35 nativeItemRef: null,
36 parentItemId: null,
37 ordinal: 1,
38 status: "completed",
39 title: null,
40 startedAt: null,
41 completedAt: null,
42 updatedAt: DateTime.makeUnsafe("2026-09-08T00:00:00.000Z"),
43 type: "command_execution",
44 input: 'rg "command not found"',
45 exitCode: 0,
46 ...fields,
47 };
48}
49
50describe("workEntryIndicatesToolFailure", () => {
51 const base = {
52 id: "w1",
53 createdAt: "2026-01-01T00:00:00.000Z",
54 label: "Read",
55 };
56
57 it.each([
58 [{ outputIndicatesFailure: true }, true],
59 [{ exitCode: 2 }, true],
60 [{ output: "sh: missing-command: command not found" }, true],
61 [{ output: "Found 3 matches" }, false],
62 [{ output: `${"x".repeat(32_768)} command not found` }, false],
63 [{}, false],
64 ] as const)(
65 "preserves command failure state after removing displayed output: %j",
66 (fields, failed) => {
67 const structuredPayload = commandItem(fields);
68 const entry: WorkLogPresentationEntry = {
69 ...base,
70 tone: "tool",
71 itemType: "command_execution",
72 toolLifecycleStatus: "completed",
73 structuredPayload,
74 };
75 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(failed);
76 expect(workEntryIndicatesToolSuccess(entry)).toBe(!failed);
77 expect(JSON.stringify(toolItemForDisplay(structuredPayload))).not.toContain('"output":');
78 },
79 );
80
81 it("ignores failure phrases in reasoning text", () => {
82 const entry: WorkLogPresentationEntry = {
83 ...base,
84 label: "Thinking",
85 tone: "thinking",
86 itemType: "reasoning",
87 toolLifecycleStatus: "completed",
88 detail: "That exit code 2 is just from listing the deleted directory, which is expected.",
89 };
90 expect(workEntryIndicatesToolFailure(entry)).toBe(false);
91 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
92 });
93
94 it("is true for error tone", () => {
95 expect(
96 workEntryIndicatesToolFailure({
97 ...base,
98 tone: "error",
99 detail: "nothing special",
100 }),
101 ).toBe(true);
102 });
103
104 it("is true when lifecycle says failed even if detail is empty", () => {
105 expect(
106 workEntryIndicatesToolFailure({
107 ...base,
108 tone: "tool",
109 toolLifecycleStatus: "failed",
110 }),
111 ).toBe(true);
112 });
113
114 it("detects file-not-found style tool output with completed lifecycle", () => {
115 expect(
116 workEntryIndicatesToolFailure({
117 ...base,
118 tone: "tool",
119 toolLifecycleStatus: "completed",
120 detail: "File not found: C:\\foo\\nonexistent.ts",
121 }),
122 ).toBe(true);
123 });
124
125 it("detects glob no files and PowerShell command errors", () => {
126 expect(
127 workEntryIndicatesToolFailure({
128 ...base,
129 label: "Glob",
130 tone: "tool",
131 detail: "No files found",
132 }),
133 ).toBe(true);
134 expect(
135 workEntryIndicatesToolFailure({
136 ...base,
137 label: "Bash",
138 tone: "tool",
139 detail:
140 "The term 'this_is_not_a_command' is not recognized as the name of a cmdlet, function, script file, or operable program.",
141 }),
142 ).toBe(true);
143 });
144
145 it("is false for successful completed tools", () => {
146 expect(
147 workEntryIndicatesToolFailure({
148 ...base,
149 tone: "tool",
150 toolLifecycleStatus: "completed",
151 detail: "Found 3 matching files",
152 }),
153 ).toBe(false);
154 });
155
156 it("does not treat error text in a command as rendered failure", () => {
157 const entry = {
158 ...base,
159 label: "Ran command",
160 tone: "tool",
161 toolLifecycleStatus: "completed",
162 command: 'rg "file not found"',
163 detail: "Found 3 matches",
164 } satisfies WorkLogPresentationEntry;
165
166 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
167
168 expect(workEntryIndicatesToolFailure(entry)).toBe(true);
169 expect(workEntryDisplayIndicatesToolFailure({ ...entry, detail: "File not found" })).toBe(true);
170 });
171
172 it("treats successful tool rows as success candidates", () => {
173 expect(
174 workEntryIndicatesToolSuccess({
175 ...base,
176 tone: "tool",
177 toolLifecycleStatus: "completed",
178 detail: "ok",
179 }),
180 ).toBe(true);
181 expect(
182 workEntryIndicatesToolSuccess({
183 ...base,
184 tone: "tool",
185 toolLifecycleStatus: "inProgress",
186 detail: "…",
187 }),
188 ).toBe(false);
189 expect(workEntryIndicatesToolSuccess({ ...base, tone: "thinking", detail: "…" })).toBe(false);
190 expect(
191 workEntryIndicatesToolSuccess({ ...base, tone: "tool", toolLifecycleStatus: "stopped" }),
192 ).toBe(false);
193 expect(
194 workEntryIndicatesToolSuccess({ ...base, tone: "tool", toolLifecycleStatus: "idle" }),
195 ).toBe(false);
196 });
197
198 it("does not run heuristics on non-tool info rows", () => {
199 expect(
200 workEntryIndicatesToolFailure({
201 ...base,
202 label: "Context compacted",
203 tone: "info",
204 detail: "File not found in conversation",
205 }),
206 ).toBe(false);
207 });
208});
209
210describe("summarizeToolGroup", () => {
211 const entry = (
212 id: string,
213 overrides: Partial<WorkLogPresentationEntry>,
214 ): WorkLogPresentationEntry => ({
215 id,
216 createdAt: "2026-09-01T00:00:00Z",
217 label: "Tool call",
218 tone: "tool",
219 ...overrides,
220 });
221
222 it("excludes reasoning from mixed tool counts and icons", () => {
223 const thought = entry("thought", {
224 itemType: "reasoning",
225 tone: "thinking",
226 detail: "Check the source",
227 });
228 const command = entry("command", { itemType: "command_execution", command: "vp test run" });
229 expect(summarizeToolGroup([thought, command, { ...thought, id: "thought-2" }])).toEqual({
230 summary: "Ran 1 command",
231 hasFailure: false,
232 });
233 expect(toolGroupSummaryKind([thought, command])).toBe("command");
234 expect(summarizeToolGroup([thought]).summary).toBe("Thought");
235 expect(summarizeToolGroup([thought, { ...thought, id: "thought-2" }]).summary).toBe(
236 "Thought (×2)",
237 );
238 expect(toolGroupSummaryKind([thought])).toBe("reasoning");
239 });
240
241 it("counts created threads alongside adjacent commands", () => {
242 expect(
243 summarizeToolGroup([
244 entry("command", { itemType: "command_execution", command: "vp test run" }),
245 entry("created", {
246 itemType: "thread_created",
247
248 label: "Created thread",
249 }),
250 ]).summary,
251 ).toBe("Ran 1 command and created 1 thread");
252 });
253
254 it("deduplicates named sources ahead of ordinary actions", () => {
255 const source = { key: "browser-use:chrome", name: "Chrome", kind: "integration" as const };
256 expect(
257 summarizeToolGroup([
258 entry("open", { label: "Open page", toolSource: source }),
259 entry("inspect", { label: "Inspect page", toolSource: source }),
260 entry("command", {
261 label: "Ran command",
262 itemType: "command_execution",
263 command: "git status",
264 }),
265 ]).summary,
266 ).toBe("Used Chrome integration and ran 1 command");
267 });
268
269 it.each([
270 [["Foo", "Foo"], "Used Foo integration and ran 1 command"],
271 [["Foo", "Bar", "Foo"], "Used Foo and Bar integrations and ran 1 command"],
272 ] as const)(
273 "uses the displayed source names for integration plurality: %j",
274 (names, expected) => {
275 expect(
276 summarizeToolGroup([
277 ...names.map((name, index) =>
278 entry(`integration-${index}`, {
279 toolSource: { key: `integration-${index}`, name, kind: "integration" },
280 }),
281 ),
282 entry("command", {
283 itemType: "command_execution",
284 command: "git status",
285 }),
286 ]).summary,
287 ).toBe(expected);
288 },
289 );
290
291 it("omits the integration suffix for special browser and computer sources", () => {
292 expect(
293 summarizeToolGroup([
294 entry("inspect", {
295 label: "Inspect page",
296 toolSource: { key: "browser-use", name: "Browser", kind: "browser" },
297 }),
298 entry("click", {
299 label: "Click",
300 toolSource: { key: "computer-use", name: "Computer Use", kind: "computer" },
301 }),
302 ]).summary,
303 ).toBe("Used Browser and Computer Use");
304 });
305
306 it("names Computer Use once when native-app and fallback sources share its label", () => {
307 expect(
308 summarizeToolGroup([
309 entry("inspect-app", {
310 label: "Inspect app",
311 toolSource: {
312 key: "native-app:com.t3tools.t3code",
313 name: "Computer Use",
314 kind: "computer",
315 },
316 }),
317 entry("failed-sky", {
318 label: "Restrict the breakpoint to Git actions",
319 toolSource: { key: "computer-use", name: "Computer Use", kind: "computer" },
320 }),
321 ]).summary,
322 ).toBe("Used Computer Use");
323 });
324});
325
326describe("resolveWorkEntryToolPresentation", () => {
327 it("presents and summarizes every T3 tool using the same structured identity", () => {
328 for (const tool of T3_MCP_TOOL_NAMES) {
329 const entry: WorkLogPresentationEntry = {
330 id: tool,
331 createdAt: "2026-09-19T00:00:00.000Z",
332 tone: "tool",
333 label: "Custom provider title",
334 toolData: { server: "t3-code", tool },
335 toolLifecycleStatus: "completed",
336 itemType: "dynamic_tool",
337 toolSource: { key: "t3-code", name: "T3 Code", kind: "integration" },
338 };
339 const presentation = resolveWorkEntryToolPresentation(entry);
340 expect(presentation, tool).not.toBeNull();
341 expect(presentation?.displayName, tool).not.toContain(tool);
342 const summary = summarizeToolGroup([entry]);
343 expect(summary.summary, tool).not.toMatch(/Used (?:1 tool|T3 Code integration)/);
344 expect(summary.hasFailure, tool).toBe(false);
345 const failed = { ...entry, toolLifecycleStatus: "failed" as const };
346 expect(resolveWorkEntryToolPresentation(failed)?.displayName, tool).toMatch(/^Failed to /);
347 expect(summarizeToolGroup([failed]).hasFailure, tool).toBe(true);
348 expect(summarizeToolGroup([failed]).summary, tool).toMatch(
349 /^(?:Tried to |Requested thread creation)/,
350 );
351 }
352 });
353
354 it.each([
355 ["t3_project_list", "Listing projects", "Listed projects"],
356 ["t3_project_clone", "Cloning a repository", "Cloned a repository"],
357 ["t3_project_create", "Registering a project", "Registered a project"],
358 ["t3_thread_launch", "Launching a project thread", "Launched a project thread"],
359 ["t3_queue_edit", "Editing a queued message", "Edited a queued message"],
360 ["t3_pending_request_respond", "Answering pending questions", "Answered pending questions"],
361 ["t3_thread_configure", "Setting thread model", "Set thread model"],
362 ["t3_thread_fork", "Forking this thread", "Requested a fork of this thread"],
363 ["t3_thread_send_attachments", "Sending attachments", "Sent attachments"],
364 ["run_scheduled_task_now", "Running a scheduled task", "Requested a run of a scheduled task"],
365 ["t3_workflow_stop", "Stopping a workflow", "Requested a stop for a workflow"],
366 ])("labels %s through its lifecycle", (tool, running, completed) => {
367 expect(resolveWorkEntryToolPresentation({ label: `T3-code.${tool}` })?.displayName).toBe(
368 running,
369 );
370 expect(
371 resolveWorkEntryToolPresentation({
372 label: `T3-code.${tool}`,
373 toolLifecycleStatus: "completed",
374 })?.displayName,
375 ).toBe(completed);
376 });
377
378 it("distinguishes an interrupted unwatch from a completed unwatch", () => {
379 const entry = { label: "T3-code.unwatch_pull_request" };
380 expect(
381 (["inProgress", "stopped", "completed"] as const).map(
382 (toolLifecycleStatus) =>
383 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus })?.displayName,
384 ),
385 ).toEqual([
386 "Unwatching a pull request",
387 "Stopped unwatching a pull request",
388 "Stopped watching a pull request",
389 ]);
390 });
391
392 it("describes starting review-guide writing in rows and groups without outcomes", () => {
393 const entry = {
394 id: "guides",
395 createdAt: "2026-10-07T00:00:00.000Z",
396 tone: "tool",
397 label: "T3-code.write_pull_request_guides",
398 toolLifecycleStatus: "completed",
399 } satisfies WorkLogPresentationEntry;
400 expect({
401 row: resolveWorkEntryToolPresentation(entry)?.displayName,
402 single: summarizeToolGroup([entry]).summary,
403 repeated: summarizeToolGroup([entry, { ...entry, id: "guides-2" }]).summary,
404 }).toEqual({
405 row: "Started writing review guides",
406 single: "Started writing review guides",
407 repeated: "Started writing review guides 2 times",
408 });
409 });
410
411 it("describes delegated-task cancellation as an ongoing action then a request", () => {
412 const entry = { label: "T3-code.task_cancel" };
413 expect(
414 (["inProgress", "completed"] as const).map(
415 (toolLifecycleStatus) =>
416 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus })?.displayName,
417 ),
418 ).toEqual(["Canceling a delegated task", "Requested cancellation of a delegated task"]);
419 });
420
421 it("summarizes workflow stop targets separately from their parent thread", () => {
422 const entry = {
423 id: "workflow-stop",
424 createdAt: "2026-10-07T00:00:00.000Z",
425 tone: "tool",
426 label: "Custom title",
427 itemType: "dynamic_tool",
428 toolLifecycleStatus: "completed",
429 toolData: {
430 server: "t3-code",
431 tool: "t3_workflow_stop",
432 arguments: { threadId: "parent", subagentId: "workflow-1" },
433 result: {},
434 },
435 } satisfies WorkLogPresentationEntry;
436 expect(
437 summarizeToolGroup([
438 entry,
439 entry,
440 {
441 ...entry,
442 toolData: {
443 ...entry.toolData,
444 arguments: { threadId: "parent", subagentId: "workflow-2" },
445 },
446 },
447 {
448 ...entry,
449 toolData: {
450 ...entry.toolData,
451 tool: "t3_thread_interrupt",
452 arguments: { threadId: "parent" },
453 },
454 },
455 ]),
456 ).toEqual({
457 summary: "Requested stops for 2 workflows and requested interrupts for 1 thread",
458 hasFailure: false,
459 });
460 });
461
462 it("summarizes project tools from MCP arguments and results without claiming failed effects", () => {
463 const entry: WorkLogPresentationEntry = {
464 id: "clone",
465 createdAt: "2026-09-19T00:00:00.000Z",
466 tone: "tool",
467 label: "Custom title",
468 itemType: "dynamic_tool",
469 toolLifecycleStatus: "completed",
470 toolData: {
471 server: "t3-code",
472 tool: "t3_project_clone",
473 arguments: { url: "https://github.com/acme/repo" },
474 result: { cwd: "/tmp/repo" },
475 },
476 };
477 const list = { ...entry, toolData: { server: "t3-code", tool: "t3_project_list" } };
478 expect(summarizeToolGroup([list, entry])).toEqual({
479 summary: "Listed projects 1 time and cloned 1 repository",
480 hasFailure: false,
481 });
482 const failed = {
483 ...entry,
484 toolData: { toolName: "T3-code.t3_project_clone", rawOutput: { isError: true } },
485 };
486 expect(summarizeToolGroup([entry, failed])).toEqual({
487 summary: "Cloned 1 repository",
488 hasFailure: true,
489 });
490 });
491
492 it("does not summarize a foreign structured identity as T3 work", () => {
493 const entry: WorkLogPresentationEntry = {
494 id: "foreign",
495 createdAt: "2026-09-19T00:00:00.000Z",
496 tone: "tool",
497 label: "t3_project_clone",
498 toolLifecycleStatus: "completed",
499 toolData: { server: "another-server", tool: "t3_project_clone" },
500 };
501 expect(summarizeToolGroup([entry]).summary).toBe("Used 1 tool");
502 });
503
504 it("shows returned MCP errors as failures even in the live activity row", () => {
505 const entry: WorkLogPresentationEntry = {
506 id: "clone",
507 createdAt: "2026-09-19T00:00:00.000Z",
508 tone: "tool",
509 label: "T3-code.t3_project_clone",
510 toolLifecycleStatus: "inProgress",
511 itemType: "dynamic_tool",
512 toolData: { output: { isError: true } },
513 };
514 expect(resolveWorkEntryToolPresentation(entry)?.displayName).toBe(
515 "Failed to clone a repository",
516 );
517 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(true);
518 expect(workEntryIndicatesToolSuccess(entry)).toBe(false);
519 const childFailure = {
520 ...entry,
521 label: "T3-code.task_status",
522 toolLifecycleStatus: "completed" as const,
523 toolData: { output: { taskId: "child", status: "failed", summary: "command not found" } },
524 };
525 expect(workEntryDisplayIndicatesToolFailure(childFailure)).toBe(false);
526 expect(workEntryIndicatesToolSuccess(childFailure)).toBe(true);
527 });
528 it.each([
529 "mcp__t3-code__preview_click",
530 "mcp__t3_code__preview_click",
531 "mcp__t3code__preview_click",
532 "T3-code.preview_click",
533 "t3-code · preview_click completed",
534 "t3_code/preview_click",
535 "preview_click",
536 ])("recognizes browser tool names across providers: %s", (label) => {
537 expect(resolveWorkEntryToolPresentation({ label })).toEqual({
538 displayName: "Clicking in the preview browser",
539 icon: "browser",
540 });
541 });
542
543 it("labels device tools with the device icon", () => {
544 expect(
545 resolveWorkEntryToolPresentation({
546 label: "mcp__t3-code__device_open",
547 toolLifecycleStatus: "completed",
548 }),
549 ).toEqual({ displayName: "Opened a device in the Device panel", icon: "device" });
550 expect(resolveWorkEntryToolPresentation({ label: "t3-code · device_screenshot" })).toEqual({
551 displayName: "Taking a screenshot of the device",
552 icon: "device",
553 });
554 });
555
556 it("uses structured MCP identity when the provider supplies a custom title", () => {
557 expect(
558 resolveWorkEntryToolPresentation({
559 label: "Tool call complete",
560 toolTitle: "Inspect the current page",
561 toolData: { server: "t3-code", tool: "preview_snapshot", result: { title: "Example" } },
562 }),
563 ).toEqual({ displayName: "Taking a snapshot of the preview page", icon: "browser" });
564 });
565
566 it.each([
567 ["inProgress", "Clicking in the preview browser"],
568 ["completed", "Clicked in the preview browser"],
569 ["failed", "Failed to click in the preview browser"],
570 ["declined", "Declined to click in the preview browser"],
571 ["stopped", "Stopped clicking in the preview browser"],
572 ["unknown", "Clicking in the preview browser"],
573 ] as const)("describes the tool's own %s state", (toolLifecycleStatus, displayName) => {
574 expect(
575 resolveWorkEntryToolPresentation({
576 label: "T3-code.preview_click",
577 toolLifecycleStatus: toolLifecycleStatus as WorkLogToolLifecycleStatus,
578 }),
579 ).toEqual({ displayName, icon: "browser" });
580 });
581
582 it("uses the summary's state only when the provider omitted a lifecycle status", () => {
583 const entry = { label: "T3-code.preview_click" };
584 expect(resolveWorkEntryToolPresentation(entry, "inProgress")?.displayName).toBe(
585 "Clicking in the preview browser",
586 );
587 expect(resolveWorkEntryToolPresentation(entry, "completed")?.displayName).toBe(
588 "Clicked in the preview browser",
589 );
590 expect(
591 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" }, "inProgress")
592 ?.displayName,
593 ).toBe("Clicked in the preview browser");
594 expect(
595 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "failed" }, "completed")
596 ?.displayName,
597 ).toBe("Failed to click in the preview browser");
598 });
599
600 it.each([
601 ["preview_type", "Typing in the preview browser", "Typed in the preview browser"],
602 [
603 "preview_set_appearance",
604 "Setting preview browser appearance",
605 "Set preview browser appearance",
606 ],
607 [
608 "preview_snapshot",
609 "Taking a snapshot of the preview page",
610 "Took a snapshot of the preview page",
611 ],
612 [
613 "preview_recording_stop",
614 "Stopping recording the preview browser",
615 "Stopped recording the preview browser",
616 ],
617 ["t3_thread_read", "Reading a T3 thread", "Read a T3 thread"],
618 ["t3_thread_send", "Sending to a T3 thread", "Sent to a T3 thread"],
619 [
620 "t3_worktree_handoff",
621 "Handing off thread to a git worktree",
622 "Handed off thread to a git worktree",
623 ],
624 ])("preserves verb forms and the rest of %s's label", (tool, running, completed) => {
625 const entry = { label: `t3-code.${tool}` };
626 expect(
627 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "inProgress" })
628 ?.displayName,
629 ).toBe(running);
630 expect(
631 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" })?.displayName,
632 ).toBe(completed);
633 });
634
635 it("keeps T3 branding for non-browser tools and falls back to the original tool label", () => {
636 expect(
637 resolveWorkEntryToolPresentation({
638 label: "mcp__t3_code__task_status",
639 toolTitle: "Check the child task",
640 }),
641 ).toEqual({ displayName: "Getting delegated task status", icon: "t3-code" });
642 });
643
644 it("does not brand unknown tools or another server's matching tool name", () => {
645 for (const label of [
646 "mcp__github__preview_click",
647 "t3-code.unknown_tool",
648 "t3-code.toString",
649 "Search files",
650 ]) {
651 expect(resolveWorkEntryToolPresentation({ label })).toBeNull();
652 }
653 expect(
654 resolveWorkEntryToolPresentation({
655 label: "preview_click",
656 toolData: { server: "another-server", tool: "preview_click" },
657 }),
658 ).toBeNull();
659 });
660});
661
662describe("browser group summaries", () => {
663 const summarizeGroupLabel = (entries: ReadonlyArray<WorkLogPresentationEntry>) =>
664 summarizeToolGroup(entries).summary;
665 const browserEntry: WorkLogPresentationEntry = {
666 id: "browser",
667 createdAt: "2026-09-01T00:00:00Z",
668 label: "MCP tool call",
669 toolData: { server: "t3-code", tool: "preview_click" },
670 itemType: "dynamic_tool",
671 toolLifecycleStatus: "completed",
672 tone: "tool",
673 };
674 const commandEntry: WorkLogPresentationEntry = {
675 id: "command",
676 createdAt: "2026-09-01T00:00:00Z",
677 label: "Ran command",
678 command: "/bin/bash -lc 'vp test run'",
679 itemType: "command_execution",
680 toolLifecycleStatus: "completed",
681 tone: "tool",
682 };
683
684 it.each([1, 18])("counts %s browser calls separately from generic tools", (count) => {
685 const entries = Array.from({ length: count }, (_, index) => ({
686 ...browserEntry,
687 toolCallId: `browser-${index}`,
688 }));
689 expect(summarizeGroupLabel(entries)).toBe(
690 `Used browser ${count} ${count === 1 ? "time" : "times"}`,
691 );
692 expect(toolGroupSummaryKind(entries)).toBe("browser");
693 });
694
695 it("combines command and browser counts in a single sentence", () => {
696 const entries = [
697 ...Array.from({ length: 4 }, () => commandEntry),
698 ...Array.from({ length: 15 }, () => browserEntry),
699 ];
700 expect(summarizeGroupLabel(entries)).toBe("Ran 4 commands and used browser 15 times");
701 expect(toolGroupSummaryKind(entries)).toBe("mixed");
702 });
703
704 it("preserves first-seen action ordering alongside non-browser tools", () => {
705 expect(
706 summarizeGroupLabel([
707 browserEntry,
708 commandEntry,
709 {
710 ...browserEntry,
711 toolData: { server: "t3-code", tool: "task_status" },
712 },
713 ]),
714 ).toBe("Used browser 1 time, ran 1 command, and performed 1 other action");
715 });
716
717 it("recognizes Claude browser identity without treating script metadata as a shell command", () => {
718 expect(
719 summarizeGroupLabel([
720 {
721 ...browserEntry,
722 command: "node inspect-page.js",
723 toolData: { toolName: "mcp__t3_code__preview_evaluate" },
724 },
725 ]),
726 ).toBe("Used browser 1 time");
727 });
728
729 it("keeps foreign tools and web searches out of the browser count", () => {
730 expect(
731 summarizeGroupLabel([
732 browserEntry,
733 {
734 ...browserEntry,
735 label: "preview_click",
736 toolData: { server: "another-server", tool: "preview_click" },
737 },
738 {
739 id: "search",
740 createdAt: "2026-09-01T00:00:00Z",
741 label: "Search",
742 tone: "tool",
743 itemType: "web_search",
744 },
745 ]),
746 ).toBe("Used browser 1 time, searched the web 1 time, and performed 1 other action");
747 });
748
749 it("keeps browser screenshots in the browser count while preserving their image path", () => {
750 const entry = { ...browserEntry, viewedImagePath: "/workspace/page.png" };
751 expect(summarizeGroupLabel([entry])).toBe("Used browser 1 time");
752 expect(workEntryViewedImagePath(entry)).toBe("/workspace/page.png");
753 });
754});
755
756describe("command work-log details", () => {
757 it("extracts Claude result blocks and projected output", () => {
758 expect(
759 extractCommandOutputText({
760 result: {
761 content: [
762 { type: "text", text: "first" },
763 { type: "text", text: "second" },
764 ],
765 },
766 }),
767 ).toBe("first\nsecond");
768 expect(extractCommandOutputText({ rawOutput: { content: "projected summary" } })).toBe(
769 "projected summary",
770 );
771 });
772
773 it("only removes a detail with the matching tool-name prefix", () => {
774 expect(
775 commandDetailRepeatsCommand({
776 detail: "Bash: printf hello",
777 command: "printf hello",
778 rawCommand: null,
779 toolName: "Bash",
780 data: { toolName: "Bash", command: "printf hello" },
781 }),
782 ).toBe(true);
783 expect(
784 commandDetailRepeatsCommand({
785 detail: "warning: printf hello",
786 command: "printf hello",
787 rawCommand: null,
788 toolName: "Bash",
789 data: { toolName: "Bash", command: "printf hello" },
790 }),
791 ).toBe(false);
792 });
793
794 it("treats an ingestion-truncated echo of a long command as a repeat", () => {
795 const command = `git add -A && git commit -m "${"x".repeat(200)}"`;
796 const truncated = `Bash: ${command}`.slice(0, 177) + "...";
797 expect(
798 commandDetailRepeatsCommand({
799 detail: truncated,
800 command,
801 rawCommand: null,
802 toolName: "Bash",
803 data: { toolName: "Bash", command },
804 }),
805 ).toBe(true);
806 expect(
807 commandDetailRepeatsCommand({
808 detail: "Bash: printf hello...",
809 command: "printf goodbye",
810 rawCommand: null,
811 toolName: "Bash",
812 data: { toolName: "Bash", command: "printf goodbye" },
813 }),
814 ).toBe(false);
815 });
816
817 it("treats ACP command echoes as synthetic even without a tool kind", () => {
818 expect(
819 commandDetailRepeatsCommand({
820 detail: "pnpm test",
821 command: "pnpm test",
822 rawCommand: null,
823 toolName: undefined,
824 data: { toolCallId: "tool-1", command: "pnpm test" },
825 }),
826 ).toBe(true);
827 expect(
828 commandDetailRepeatsCommand({
829 detail: "pnpm test",
830 command: "pnpm test",
831 rawCommand: null,
832 toolName: undefined,
833 data: { command: "pnpm test" },
834 }),
835 ).toBe(false);
836 });
837});
838
839describe("workEntryViewedImagePath", () => {
840 const entry = {
841 id: "entry-1",
842 createdAt: "2026-01-01T00:00:00Z",
843 label: "Read",
844 tone: "tool",
845 } as const;
846
847 it("returns a single image path from supported read entries", () => {
848 expect(
849 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: " assets/a.png " }),
850 ).toBe("assets/a.png");
851 expect(
852 workEntryViewedImagePath({
853 ...entry,
854 itemType: "dynamic_tool",
855 toolTitle: "Read file",
856 detail: "C:\\workspace\\a.webp",
857 }),
858 ).toBe("C:\\workspace\\a.webp");
859 expect(
860 workEntryViewedImagePath({
861 ...entry,
862 itemType: "dynamic_tool",
863 detail: 'Read: {"file_path":"truncated..."}',
864 viewedImagePath: " /workspace/reference image.webp ",
865 }),
866 ).toBe("/workspace/reference image.webp");
867 });
868
869 it("rejects non-image, multi-line, and non-read details", () => {
870 expect(
871 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: "a.txt" }),
872 ).toBeNull();
873 expect(
874 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: "a.png\nb.png" }),
875 ).toBeNull();
876 expect(workEntryViewedImagePath({ ...entry, detail: "a.png" })).toBeNull();
877 });
878});
879
880describe("toolGroupAction", () => {
881 it("groups legacy Claude image reads with other reads", () => {
882 expect(
883 toolGroupAction({
884 id: "legacy-read",
885 createdAt: "2026-09-01T00:00:00Z",
886 label: "Tool call",
887 tone: "tool",
888 itemType: "dynamic_tool",
889 viewedImagePath: "/workspace/reference.png",
890 }),
891 ).toBe("read");
892 });
893
894 it("groups Claude Read and Grep from toolName, not file contents", () => {
895 expect(
896 toolGroupAction({
897 id: "read",
898 createdAt: "2026-09-01T00:00:00Z",
899 label: "Read",
900 tone: "tool",
901 itemType: "dynamic_tool",
902 toolTitle: "Read",
903 toolData: { input: { file_path: "src/env.ts" } },
904 structuredPayload: {
905 type: "dynamic_tool",
906 toolName: "Read",
907 input: { file_path: "src/env.ts" },
908 } as NonNullable<WorkLogPresentationEntry["structuredPayload"]>,
909 }),
910 ).toBe("read");
911 expect(
912 toolGroupAction({
913 id: "grep",
914 createdAt: "2026-09-01T00:00:00Z",
915 label: "Grep",
916 tone: "tool",
917 itemType: "dynamic_tool",
918 toolTitle: "Grep",
919 toolData: { input: { pattern: "TODO", path: "apps/web" } },
920 structuredPayload: {
921 type: "dynamic_tool",
922 toolName: "Grep",
923 input: { pattern: "TODO", path: "apps/web" },
924 } as NonNullable<WorkLogPresentationEntry["structuredPayload"]>,
925 }),
926 ).toBe("code-search");
927 });
928});
929
930describe("resolveViewedImageAsset", () => {
931 const threadId = ThreadId.make("thread-1");
932
933 it("serves t3 attachment paths in place like any other host path", () => {
934 const path = "/Users/demo/.t3/dev/attachments/11111111-1111-4111-8111-111111111111.png";
935 expect(resolveViewedImageAsset(path, { threadId, workspaceRoot: "/workspace" })).toEqual({
936 resource: { _tag: "media-file", threadId, path },
937 alt: "11111111-1111-4111-8111-111111111111.png",
938 srcFragment: "",
939 });
940 });
941
942 it("normalizes workspace image sources", () => {
943 expect(
944 resolveViewedImageAsset("screens/logo.svg?v=2#mark", {
945 threadId,
946 workspaceRoot: "/workspace",
947 }),
948 ).toEqual({
949 resource: {
950 _tag: "media-file",
951 threadId,
952 path: "/workspace/screens/logo.svg",
953 },
954 alt: "logo.svg",
955 srcFragment: "#mark",
956 });
957 expect(resolveViewedImageAsset("https://example.com/logo.png", { threadId })).toBeNull();
958 });
959});
960
961describe("pull request tool presentation", () => {
962 it.each([
963 "mcp__t3-code__link_pull_request",
964 "mcp__t3_code__link_pull_request",
965 "T3-code · link_pull_request",
966 "t3code/link_pull_request",
967 "link_pull_request",
968 ])("recognizes the native linking tool: %s", (label) => {
969 const entry: WorkLogPresentationEntry = {
970 id: "link",
971 createdAt: "2026-09-10T00:00:00.000Z",
972 label,
973 tone: "tool",
974 toolLifecycleStatus: "completed",
975 };
976 expect(resolveWorkEntryToolPresentation(entry)).toMatchObject({
977 displayName: "Linked a pull request",
978 icon: "pull-request",
979 });
980 expect(toolGroupAction(entry)).toBe("link-pr");
981 });
982
983 it.each([
984 ["inProgress", "Linking PR #42"],
985 ["completed", "Linked PR #42"],
986 ["failed", "Failed to link PR #42"],
987 ["declined", "Declined to link PR #42"],
988 ["stopped", "Stopped linking PR #42"],
989 ] as const)("describes the target and %s status", (toolLifecycleStatus, displayName) => {
990 expect(
991 resolveWorkEntryToolPresentation({
992 label: "MCP tool call",
993 toolTitle: "Custom title",
994 toolLifecycleStatus,
995 toolData: {
996 server: "t3-code",
997 tool: "link_pull_request",
998 arguments: { url: "https://github.com/acme/web/pull/42" },
999 },
1000 })?.displayName,
1001 ).toBe(displayName);
1002 });
1003
1004 it("recognizes unlink targets supplied as repository and number", () => {
1005 expect(
1006 resolveWorkEntryToolPresentation({
1007 label: "MCP tool call",
1008 toolLifecycleStatus: "completed",
1009 toolData: {
1010 toolName: "mcp__t3-code__unlink_pull_request",
1011 rawInput: { repository: "acme/web", number: 42 },
1012 },
1013 }),
1014 ).toMatchObject({ displayName: "Unlinked PR #42", icon: "pull-request", action: "unlink-pr" });
1015 });
1016
1017 it("summarizes native PR work separately from ordinary tools and integration metadata", () => {
1018 const link: WorkLogPresentationEntry = {
1019 id: "link",
1020 createdAt: "2026-09-10T00:00:00.000Z",
1021 label: "T3-code · link_pull_request",
1022 tone: "tool",
1023 itemType: "dynamic_tool",
1024 toolLifecycleStatus: "completed",
1025 toolSource: { key: "t3-code", name: "T3 Code", kind: "integration" },
1026 };
1027 const list: WorkLogPresentationEntry = {
1028 ...link,
1029 label: "T3-code · list_thread_pull_requests",
1030 };
1031 expect(summarizeToolGroup([link, link, list]).summary).toBe(
1032 "Linked 2 pull requests and checked linked pull requests",
1033 );
1034 expect(summarizeToolGroup([{ ...link, label: "T3-code · unlink_pull_request" }]).summary).toBe(
1035 "Unlinked 1 pull request",
1036 );
1037 expect(toolGroupSummaryKind([link, link, list])).toBe("pull-request");
1038 expect(summarizeToolGroup([list, list]).summary).toBe("Checked linked pull requests 2 times");
1039 expect(
1040 resolveWorkEntryToolPresentation({ label: "mcp__another-server__link_pull_request" }),
1041 ).toBeNull();
1042 });
1043});
1044
1045describe("device group summaries", () => {
1046 const deviceEntry = (tool: string): WorkLogPresentationEntry => ({
1047 id: tool,
1048 createdAt: "2026-09-10T00:00:00.000Z",
1049 label: "MCP tool call",
1050 toolData: { server: "t3-code", tool },
1051 itemType: "dynamic_tool",
1052 toolLifecycleStatus: "completed",
1053 tone: "tool",
1054 });
1055
1056 it.each(["device_list", "device_open", "device_screenshot", "device_close"])(
1057 "recognizes %s as device controls",
1058 (tool) => {
1059 const entry = deviceEntry(tool);
1060 expect(summarizeToolGroup([entry]).summary).toBe("Used device controls 1 time");
1061 expect(toolGroupSummaryKind([entry])).toBe("device");
1062 },
1063 );
1064
1065 it("summarizes device calls alongside shell commands", () => {
1066 expect(
1067 summarizeToolGroup([
1068 {
1069 id: "command",
1070 createdAt: "2026-09-10T00:00:00.000Z",
1071 label: "Ran command",
1072 itemType: "command_execution",
1073 command: "pwd",
1074 tone: "tool",
1075 },
1076 deviceEntry("device_list"),
1077 deviceEntry("device_open"),
1078 ]).summary,
1079 ).toBe("Ran 1 command and used device controls 2 times");
1080 });
1081
1082 it("recognizes Claude tool names and preserves screenshot previews", () => {
1083 const entry = {
1084 ...deviceEntry("device_screenshot"),
1085 toolData: { toolName: "mcp__t3_code__device_screenshot" },
1086 viewedImagePath: "/workspace/device.png",
1087 };
1088 expect(summarizeToolGroup([entry]).summary).toBe("Used device controls 1 time");
1089 expect(workEntryViewedImagePath(entry)).toBe("/workspace/device.png");
1090 });
1091
1092 it("does not classify another server's tools as T3 device controls", () => {
1093 expect(
1094 summarizeToolGroup([
1095 {
1096 ...deviceEntry("device_open"),
1097 toolData: { server: "another-server", tool: "device_open" },
1098 },
1099 ]).summary,
1100 ).toBe("Used 1 tool");
1101 });
1102});
1103
1104describe("liveThoughtLine", () => {
1105 it("keeps the first sentence, including a closing quote", () => {
1106 expect(
1107 liveThoughtLine(
1108 'Found the cause: the repo has no commits, so `git worktree add` fails with "invalid reference: main." Now checking the UI.',
1109 ),
1110 ).toBe(
1111 'Found the cause: the repo has no commits, so git worktree add fails with "invalid reference: main."',
1112 );
1113 });
1114
1115 it("does not cut at dots inside file names or long dashes", () => {
1116 expect(
1117 liveThoughtLine("I read ThreadLaunchService.ts \u2014 it skips the fetch. Next step."),
1118 ).toBe("I read ThreadLaunchService.ts \u2014 it skips the fetch.");
1119 });
1120
1121 it("uses a bold-only opening line as the whole line", () => {
1122 expect(
1123 liveThoughtLine("**Narrowing dispatch files**\n\nI should check the adapter. Then more."),
1124 ).toBe("Narrowing dispatch files");
1125 expect(liveThoughtLine("**Narrowing dispatch files**\r\n\r\nI should check the adapter.")).toBe(
1126 "Narrowing dispatch files",
1127 );
1128 });
1129
1130 it("returns unpunctuated text whole and flattens markdown", () => {
1131 expect(liveThoughtLine("- Checking [the docs](https://x.dev) for **limits**")).toBe(
1132 "Checking the docs for limits",
1133 );
1134 expect(liveThoughtLine("This is *really* ~~not~~ _fine_ in snake_case_names.")).toBe(
1135 "This is really not fine in snake_case_names.",
1136 );
1137 expect(liveThoughtLine(" ")).toBe("");
1138 });
1139});