import { describe, expect, it } from "vitest"; import { normalizeAcpUpdate } from "../src/drivers/acp/mapper.js"; import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { readTranscript, transcriptLine } from "../src/drivers/claude/transcript.js"; import { itemToHistory } from "../src/drivers/codex/mapper.js"; // Every driver reports the same generic tool detail, whatever the agent calls it. const PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="; function toolCall(updates: { sessionUpdate: string }[] | undefined) { return updates?.find((u) => u.sessionUpdate === "tool_call") as Record | undefined; } describe("tool detail", () => { it("Codex: MCP, web search, sub-agent and image generation", () => { const mcp = itemToHistory({ type: "mcpToolCall", id: "m1", server: "playwright", tool: "browser_take_screenshot", status: "completed", arguments: { fullPage: true }, result: { content: [{ type: "text", text: "saved" }, { type: "image", data: PNG, mimeType: "image/png" }] }, }); expect(toolCall(mcp?.updates)?.detail).toEqual({ type: "mcp", server: "playwright", tool: "browser_take_screenshot" }); const finish = mcp?.updates.find((u) => u.sessionUpdate === "tool_call_update") as { content?: unknown[] }; expect(finish.content).toEqual([ { type: "content", content: { type: "text", text: "saved" } }, { type: "content", content: { type: "image", mimeType: "image/png", data: PNG } }, ]); expect(toolCall(itemToHistory({ type: "webSearch", id: "w", query: "expo" })?.updates)?.detail).toEqual({ type: "web_search", query: "expo" }); const spawn = itemToHistory({ type: "collabAgentToolCall", id: "s", tool: "spawnAgent", status: "completed", prompt: "check pages", model: "gpt-5", receiverThreadIds: ["child"], }); expect(toolCall(spawn?.updates)?.detail).toEqual({ type: "subagent", action: "spawn", task: "check pages", model: "gpt-5" }); const wait = itemToHistory({ type: "collabAgentToolCall", id: "w2", tool: "wait", status: "inProgress", receiverThreadIds: ["child"] }); expect(toolCall(wait?.updates)?.detail).toMatchObject({ type: "subagent", action: "wait" }); expect(toolCall(itemToHistory({ type: "subAgentActivity", id: "a", kind: "started", agentThreadId: "child", agentPath: "/root/reviewer" })?.updates)?.detail) .toEqual({ type: "subagent", action: "spawn", task: "reviewer" }); const image = itemToHistory({ type: "imageGeneration", id: "g", status: "completed", revisedPrompt: "a cat", result: PNG }); expect(toolCall(image?.updates)?.detail).toEqual({ type: "image_generation", prompt: "a cat" }); }); it("Claude transcript: a compaction is a card, and the summary Claude continues from isn't shown as the user's", () => { const boundary = transcriptLine( JSON.stringify({ type: "system", subtype: "compact_boundary", uuid: "b1", content: "Conversation compacted", compactMetadata: { trigger: "manual", preTokens: 27232, postTokens: 2723, preservedSegment: { headUuid: "x" } }, }), ); expect(boundary.updates).toEqual([ { sessionUpdate: "tool_call", toolCallId: "compact:b1", title: "Context compacted", kind: "think", status: "completed", detail: { type: "compaction" }, rawInput: { trigger: "manual", preTokens: 27232, postTokens: 2723 }, }, ]); const summary = transcriptLine( JSON.stringify({ type: "user", uuid: "s1", isCompactSummary: true, isVisibleInTranscriptOnly: true, message: { role: "user", content: "This session is being continued from a previous conversation that ran out of context. …" }, }), ); expect(summary.updates).toEqual([]); // Other system lines (hook output, timing) stay out. expect(transcriptLine(JSON.stringify({ type: "system", subtype: "turn_duration", uuid: "d1", durationMs: 5 })).updates).toEqual([]); }); it("Claude transcript: MCP and Task tools, with tool-result images", () => { const line = (message: unknown, type = "assistant") => JSON.stringify({ type, uuid: "u", timestamp: "2026-09-30T00:00:00Z", message }); const mcp = transcriptLine(line({ id: "msg", content: [{ type: "tool_use", id: "t1", name: "mcp__playwright__browser_click", input: { ref: "e1" } }] })); expect(toolCall(mcp.updates)?.detail).toEqual({ type: "mcp", server: "playwright", tool: "browser_click" }); const task = transcriptLine(line({ id: "msg", content: [{ type: "tool_use", id: "t2", name: "Task", input: { description: "Audit pages" } }] })); expect(toolCall(task.updates)?.detail).toEqual({ type: "subagent", action: "spawn", task: "Audit pages" }); const ask = transcriptLine( line({ id: "msg", content: [{ type: "tool_use", id: "t9", name: "AskUserQuestion", input: { questions: [{ question: "Which database?", header: "DB", options: [] }, { question: "Which checks?" }] } }] }), ); expect(toolCall(ask.updates)).toMatchObject({ title: "Ask: Which database?", detail: { type: "question", text: "Which database?", more: 1 } }); const skill = transcriptLine(line({ id: "msg", content: [{ type: "tool_use", id: "t3", name: "Skill", input: { skill: "pdf" } }] })); expect(toolCall(skill.updates)?.detail).toEqual({ type: "skill", name: "pdf" }); const command = transcriptLine( line({ role: "user", content: "review\n/review\nsrc/app" }, "user"), ); const note = transcriptLine( line({ role: "user", content: "\na1\ntask1\ncompleted\n" }, "user"), ); expect(note.updates.map((u) => u.sessionUpdate)).toEqual(["tool_call_update", "ls_turn"]); expect(note.updates[0]).toMatchObject({ toolCallId: "task1", status: "completed" }); const queued = transcriptLine( JSON.stringify({ type: "attachment", uuid: "q1", timestamp: "2026-09-30T00:00:00Z", attachment: { type: "queued_command", prompt: "就是我新发的话", source_uuid: "src1", commandMode: "prompt", origin: { kind: "human" } }, }), ); expect(queued.updates).toEqual([{ sessionUpdate: "user_message_chunk", messageId: "src1", content: { type: "text", text: "就是我新发的话" } }]); const system = transcriptLine(JSON.stringify({ type: "attachment", attachment: { type: "skill_listing", content: "…" } })); expect(system.updates).toEqual([]); expect(command.updates[0]).toMatchObject({ sessionUpdate: "user_message_chunk", content: { type: "text", text: "/review src/app" } }); const result = transcriptLine( line( { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: [{ type: "image", source: { type: "base64", media_type: "image/png", data: PNG } }] }] }, "user", ), ); expect(result.updates[0]).toMatchObject({ sessionUpdate: "tool_call_update", content: [{ type: "content", content: { type: "image", data: PNG } }] }); }); it("Claude: TodoWrite shows as the plan, not as a tool call too", () => { const line = (message: unknown, type = "assistant") => JSON.stringify({ type, uuid: "u", timestamp: "2026-09-30T00:00:00Z", message }); const hidden = new Set(); const todos = { todos: [{ content: "跑测试", status: "completed", activeForm: "跑测试" }] }; const call = transcriptLine(line({ id: "msg", content: [{ type: "tool_use", id: "todo1", name: "TodoWrite", input: todos }] }), { hidden }); expect(call.updates.map((u) => u.sessionUpdate)).toEqual(["plan"]); const result = transcriptLine(line({ role: "user", content: [{ type: "tool_result", tool_use_id: "todo1", content: "Todos updated" }] }, "user"), { hidden }); expect(result.updates).toEqual([]); expect(hidden.size).toBe(0); }); it("Claude desktop app: update_plan shows as the plan, and its steps move it on", () => { const line = (message: unknown, type = "assistant") => JSON.stringify({ type, uuid: "u", timestamp: "2026-10-28T00:00:00Z", message }); const hidden = new Set(); const call = (id: string, input: unknown) => transcriptLine(line({ id: `m-${id}`, content: [{ type: "tool_use", id, name: "mcp__ccd_turn__update_plan", input }] }), { hidden }); const plan = (result: ReturnType) => (result.updates[0] as { entries: { content: string; status: string }[] }).entries.map((entry) => `${entry.status}:${entry.content}`); const first = call("p1", { items: [{ content: "读代码", status: "in_progress" }, { content: "改样式", status: "pending" }, { content: "跑测试", status: "pending" }], scope: "first_task" }); expect(first.updates.map((u) => u.sessionUpdate)).toEqual(["plan"]); expect(plan(first)).toEqual(["in_progress:读代码", "pending:改样式", "pending:跑测试"]); expect(plan(call("p2", { advance: true }))).toEqual(["completed:读代码", "in_progress:改样式", "pending:跑测试"]); expect(plan(call("p3", { done: [2], start: 3 }))).toEqual(["completed:读代码", "completed:改样式", "in_progress:跑测试"]); // Its result lines are part of the card, not calls of their own. const result = transcriptLine(line({ role: "user", content: [{ type: "tool_result", tool_use_id: "p3", content: "Plan updated" }] }, "user"), { hidden }); expect(result.updates).toEqual([]); }); it("Claude desktop app: one list keeps one id across turns; a new request's list gets its own", () => { const line = (message: unknown) => JSON.stringify({ type: "assistant", uuid: "u", timestamp: "2026-10-28T00:00:00Z", message }); const hidden = new Set(); const id = (callId: string, input: unknown) => (transcriptLine(line({ id: `m-${callId}`, content: [{ type: "tool_use", id: callId, name: "mcp__ccd_turn__update_plan", input }] }), { hidden }).updates[0] as { planId?: string }).planId; const items = (scope?: string) => ({ items: [{ content: "a", status: "in_progress" }, { content: "b", status: "pending" }], ...(scope ? { scope } : {}) }); expect(id("p1", items("first_task"))).toBe("p1"); expect(id("p2", { advance: true })).toBe("p1"); // Rewritten for the same request (or by an app that doesn't say): still that card. expect(id("p3", items("continuation"))).toBe("p1"); expect(id("p4", items())).toBe("p1"); expect(id("p5", { done: [1], start: 2 })).toBe("p1"); // The next request's list starts a card of its own, and its steps follow it. expect(id("p6", items("new_task"))).toBe("p6"); expect(id("p7", { advance: true })).toBe("p6"); // Once every step is done, the next list is a new one, whatever its scope says. expect(id("p8", { done: [1, 2] })).toBe("p6"); expect(id("p9", items("continuation"))).toBe("p9"); }); it("Claude desktop app: a step update with no list before it stays a plain call", () => { const line = JSON.stringify({ type: "assistant", uuid: "u", timestamp: "2026-10-28T00:00:00Z", message: { id: "m", content: [{ type: "tool_use", id: "p", name: "mcp__ccd_turn__update_plan", input: { advance: true } }] } }); expect(transcriptLine(line, { hidden: new Set() }).updates.map((u) => u.sessionUpdate)).toEqual(["tool_call"]); }); it("Claude: imports sub-agent transcripts nested under their Task call", () => { const dir = mkdtempSync(join(tmpdir(), "ls-sub-")); const main = join(dir, "s.jsonl"); const at = (s: number) => `2026-09-30T00:00:0${s}Z`; writeFileSync( main, [ { type: "assistant", uuid: "a", timestamp: at(1), message: { id: "m1", content: [{ type: "tool_use", id: "task1", name: "Task", input: { description: "Audit" } }] } }, { type: "user", uuid: "b", timestamp: at(5), message: { role: "user", content: [{ type: "tool_result", tool_use_id: "task1", content: "done" }] } }, ] .map((l) => JSON.stringify(l)) .join("\n") + "\n", ); mkdirSync(join(dir, "s", "subagents"), { recursive: true }); writeFileSync(join(dir, "s", "subagents", "agent-x.meta.json"), JSON.stringify({ agentType: "Explore", toolUseId: "task1" })); writeFileSync( join(dir, "s", "subagents", "agent-x.jsonl"), [ { type: "user", isSidechain: true, uuid: "c", timestamp: at(2), message: { role: "user", content: "Audit the pages" } }, { type: "assistant", isSidechain: true, uuid: "d", timestamp: at(3), message: { id: "m2", content: [{ type: "tool_use", id: "r1", name: "Read", input: { file_path: "/a.ts" } }] } }, ] .map((l) => JSON.stringify(l)) .join("\n") + "\n", ); const { updates } = readTranscript(main); expect(updates.map((u) => [u.sessionUpdate, (u as { parentToolCallId?: string }).parentToolCallId])).toEqual([ ["tool_call", undefined], ["ls_turn", "task1"], ["tool_call", "task1"], ["tool_call_update", undefined], ]); }); it("ACP: reads Claude's tool name from _meta, stays generic otherwise", () => { const claude = normalizeAcpUpdate({ sessionUpdate: "tool_call", toolCallId: "a", title: "browser_click", kind: "other", rawInput: {}, _meta: { claudeCode: { toolName: "mcp__playwright__browser_click" } }, }); expect((claude as Record).detail).toEqual({ type: "mcp", server: "playwright", tool: "browser_click" }); const child = normalizeAcpUpdate({ sessionUpdate: "agent_message_chunk", messageId: "c1", content: { type: "text", text: "hi" }, _meta: { claudeCode: { parentToolUseId: "task1" } }, }); expect(child).toMatchObject({ parentToolCallId: "task1" }); const prompt = normalizeAcpUpdate({ sessionUpdate: "user_message_chunk", content: { type: "text", text: "Audit" }, _meta: { claudeCode: { parentToolUseId: "task1" } }, }); expect(prompt).toBeUndefined(); const other = normalizeAcpUpdate({ sessionUpdate: "tool_call", toolCallId: "b", title: "Read file", kind: "read" }); expect((other as Record).detail).toBeUndefined(); }); }); describe("what clients word a call and an error by", () => { it("Claude: the tool's own name, from the transcript and from the ACP adapter", () => { const line = JSON.stringify({ type: "assistant", uuid: "u1", message: { id: "m1", content: [{ type: "tool_use", id: "g1", name: "Glob", input: { pattern: "**/*.ts" } }] } }); expect(toolCall(transcriptLine(line).updates)).toMatchObject({ name: "Glob", kind: "search" }); const acp = normalizeAcpUpdate({ sessionUpdate: "tool_call", toolCallId: "w1", title: "Write a.ts", kind: "edit", status: "pending", _meta: { claudeCode: { toolName: "Write" } } }); expect(acp).toMatchObject({ name: "Write" }); }); it("Claude: a failed request written as its reply is an error with Claude's code", () => { const line = JSON.stringify({ type: "assistant", uuid: "u2", isApiErrorMessage: true, error: "rate_limit", apiErrorStatus: 429, message: { id: "m2", model: "", stop_reason: "stop_sequence", content: [{ type: "text", text: "You've hit your session limit · resets 5am" }] } }); const updates = transcriptLine(line).updates; expect(updates).toContainEqual({ sessionUpdate: "ls_error", code: "rate_limit", message: "You've hit your session limit · resets 5am" }); expect(updates.some((update) => update.sessionUpdate === "agent_message_chunk")).toBe(false); const unknown = JSON.stringify({ type: "assistant", uuid: "u3", isApiErrorMessage: true, error: "unknown", message: { id: "m3", content: [{ type: "text", text: "API Error: 400 bad" }] } }); expect(transcriptLine(unknown).updates[0]).toMatchObject({ sessionUpdate: "ls_error", code: "api_error" }); }); it("Codex: the file a read names, resolved against the command's directory", () => { const read = itemToHistory({ type: "commandExecution", id: "r1", cwd: "/w", command: "/bin/zsh -lc 'cat src/a.ts'", status: "completed", commandActions: [{ type: "read", command: "cat src/a.ts", name: "a.ts", path: "src/a.ts" }] }); expect(toolCall(read?.updates)?.rawInput).toEqual({ command: "cat src/a.ts", cwd: "/w", path: "/w/src/a.ts" }); }); });