v4AssistantWorkRenderItems.test.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. import { describe, expect, it } from "vitest";
  2. import type {
  3. ConversationRow,
  4. ReasoningRow,
  5. SubagentRow,
  6. ToolCallRow,
  7. } from "@zcode/shared/zcode-protocol-v4";
  8. import { buildAssistantWorkRenderItems } from "@/v4/conversationAssistantWorkItems.js";
  9. const SHOW_ALL_REASONING = { messageStreamShowReasoning: true } as const;
  10. function buildRenderItems(
  11. rows: Parameters<typeof buildAssistantWorkRenderItems>[0],
  12. reasoningVisibility: Parameters<typeof buildAssistantWorkRenderItems>[1] = SHOW_ALL_REASONING,
  13. options?: Parameters<typeof buildAssistantWorkRenderItems>[2],
  14. ) {
  15. return buildAssistantWorkRenderItems(rows, reasoningVisibility, {
  16. ...options,
  17. enableChangesGrouping: options?.enableChangesGrouping ?? true,
  18. });
  19. }
  20. function toolCall(
  21. rowId: number,
  22. toolName: string,
  23. input: unknown,
  24. overrides: Partial<ToolCallRow> = {},
  25. ): ToolCallRow {
  26. return {
  27. rowId,
  28. turnId: "turn-1",
  29. createdAt: 1_700_000_000_000 + rowId,
  30. createdAtSeq: rowId,
  31. kind: "toolCall",
  32. toolCallId: `tool-${rowId}`,
  33. toolName,
  34. status: "success",
  35. input,
  36. inputText: JSON.stringify(input),
  37. ...overrides,
  38. };
  39. }
  40. function subagent(rowId: number, overrides: Partial<SubagentRow> = {}): SubagentRow {
  41. return {
  42. rowId,
  43. turnId: "turn-1",
  44. createdAt: 1_700_000_000_000 + rowId,
  45. createdAtSeq: rowId,
  46. kind: "subagent",
  47. subagentType: "Explore",
  48. status: "running",
  49. summaryText: "正在分析工作区",
  50. childSessionId: `child-session-${rowId}`,
  51. ...overrides,
  52. };
  53. }
  54. function reasoning(rowId: number): ReasoningRow {
  55. return {
  56. rowId,
  57. turnId: "turn-1",
  58. createdAt: 1_700_000_000_000 + rowId,
  59. createdAtSeq: rowId,
  60. kind: "reasoning",
  61. state: "complete",
  62. text: `reasoning-${rowId}`,
  63. };
  64. }
  65. describe("buildAssistantWorkRenderItems", () => {
  66. it("keeps a single Explore, Terminal, or Changes tool ungrouped", () => {
  67. expect(buildRenderItems([toolCall(1, "Read", { path: "package.json" })])).toMatchObject([
  68. { kind: "row", row: { rowId: 1 } },
  69. ]);
  70. expect(buildRenderItems([toolCall(2, "Bash", { command: "pnpm test" })])).toMatchObject([
  71. { kind: "row", row: { rowId: 2 } },
  72. ]);
  73. expect(
  74. buildRenderItems([toolCall(3, "Edit", { path: "package.json", newText: "{}" })]),
  75. ).toMatchObject([{ kind: "row", row: { rowId: 3 } }]);
  76. });
  77. it("groups two consecutive tools of the same display family", () => {
  78. expect(
  79. buildRenderItems([
  80. toolCall(1, "Read", { path: "package.json" }),
  81. toolCall(2, "Grep", { pattern: "group" }),
  82. ]),
  83. ).toMatchObject([{ kind: "exploreGroup", rows: [{ rowId: 1 }, { rowId: 2 }] }]);
  84. expect(
  85. buildRenderItems([
  86. toolCall(3, "Bash", { command: "pnpm test" }),
  87. toolCall(4, "Bash", { command: "pnpm lint" }),
  88. ]),
  89. ).toMatchObject([{ kind: "executeGroup", rows: [{ rowId: 3 }, { rowId: 4 }] }]);
  90. expect(
  91. buildRenderItems([
  92. toolCall(5, "Write", { file_path: "src/a.ts", content: "a" }),
  93. toolCall(6, "Edit", {
  94. file_path: "src/b.ts",
  95. old_string: "b",
  96. new_string: "c",
  97. }),
  98. ]),
  99. ).toMatchObject([{ kind: "changesGroup", rows: [{ rowId: 5 }, { rowId: 6 }] }]);
  100. });
  101. it("marks a terminal Explore group as stopped when a child was cancelled", () => {
  102. const items = buildRenderItems([
  103. toolCall(1, "Read", { path: "package.json" }),
  104. toolCall(2, "Grep", { pattern: "group" }, { status: "cancelled" }),
  105. ]);
  106. expect(items).toMatchObject([
  107. {
  108. kind: "exploreGroup",
  109. node: { toolCall: { status: "stopped" } },
  110. rows: [{ rowId: 1 }, { rowId: 2 }],
  111. },
  112. ]);
  113. });
  114. it("does not group same-family tools across a visible tool boundary", () => {
  115. const items = buildRenderItems([
  116. toolCall(1, "Read", { path: "src/a.ts" }),
  117. toolCall(2, "Edit", { path: "src/a.ts", newText: "a" }),
  118. toolCall(3, "Read", { path: "src/b.ts" }),
  119. ]);
  120. expect(items.map((item) => item.kind)).toEqual(["row", "row", "row"]);
  121. });
  122. it("keeps reasoning outside without closing and lets an uncorrelated message close", () => {
  123. const rows = [
  124. toolCall(1, "mcp__computer-use__get_app_state", { app_ref: { pid: 691 } }),
  125. reasoning(2),
  126. {
  127. rowId: 3,
  128. turnId: "turn-1",
  129. createdAt: 1_700_000_000_003,
  130. createdAtSeq: 3,
  131. kind: "assistantText",
  132. text: "更新数据……",
  133. state: "complete",
  134. } satisfies ConversationRow,
  135. toolCall(4, "mcp__computer-use__left_click", {
  136. target: { type: "element", state_id: "s-1", index: 59 },
  137. }),
  138. toolCall(5, "Bash", { command: "pnpm test" }),
  139. ];
  140. const items = buildRenderItems(rows);
  141. expect(items.map((item) => item.kind)).toEqual(["cuaGroup", "row", "row", "cuaGroup", "row"]);
  142. const group = items[0];
  143. expect(group?.kind).toBe("cuaGroup");
  144. if (group?.kind !== "cuaGroup") throw new Error("expected CUA group");
  145. expect(group.key).toBe("cua:1");
  146. expect(group.node.toolCall.toolId).toBe("cua:tool-1");
  147. expect(group.node.toolCall.status).toBe("completed");
  148. expect(group.rows.map((row) => row.rowId)).toEqual([1]);
  149. expect(group.events.map((event) => event.row.rowId)).toEqual([1]);
  150. });
  151. it("keeps a tail CUA group active across reasoning and starts a new group after a tool boundary", () => {
  152. const first = buildRenderItems(
  153. [toolCall(1, "mcp__computer-use__list_apps", {}, { status: "running" })],
  154. SHOW_ALL_REASONING,
  155. { stageTailIsRunning: true },
  156. );
  157. const grown = buildRenderItems(
  158. [
  159. toolCall(1, "mcp__computer-use__list_apps", {}),
  160. reasoning(2),
  161. toolCall(3, "mcp__computer-use__get_app_state", {}, { status: "running" }),
  162. ],
  163. SHOW_ALL_REASONING,
  164. { stageTailIsRunning: true },
  165. );
  166. const bounded = buildRenderItems([
  167. toolCall(1, "mcp__computer-use__list_apps", {}),
  168. toolCall(2, "Read", { path: "package.json" }),
  169. toolCall(3, "mcp__computer-use__screenshot", {}),
  170. ]);
  171. const firstGroup = first[0];
  172. const grownGroup = grown[0];
  173. expect(firstGroup?.kind).toBe("cuaGroup");
  174. expect(grown.map((item) => item.kind)).toEqual(["cuaGroup", "row"]);
  175. expect(grownGroup?.kind).toBe("cuaGroup");
  176. if (firstGroup?.kind !== "cuaGroup" || grownGroup?.kind !== "cuaGroup") {
  177. throw new Error("expected stable CUA groups");
  178. }
  179. expect(grownGroup.key).toBe(firstGroup.key);
  180. expect(grownGroup.node.toolCall.toolId).toBe(firstGroup.node.toolCall.toolId);
  181. expect(grownGroup.node.toolCall.status).toBe("in_progress");
  182. expect(grownGroup.rows.map((row) => row.rowId)).toEqual([1, 3]);
  183. expect(bounded.map((item) => item.kind)).toEqual(["cuaGroup", "row", "cuaGroup"]);
  184. });
  185. it("does not group third-party computer-use names and honors the release gate", () => {
  186. const official = toolCall(1, "mcp__computer-use__wait", {});
  187. const thirdParty = toolCall(2, "mcp__third-party-computer-use__wait", {});
  188. expect(buildRenderItems([thirdParty]).map((item) => item.kind)).toEqual(["row"]);
  189. expect(
  190. buildAssistantWorkRenderItems([official], SHOW_ALL_REASONING, {
  191. enableCuaGrouping: false,
  192. }).map((item) => item.kind),
  193. ).toEqual(["row"]);
  194. });
  195. it("keeps Explore-compatible tools as individual rows when Explore grouping is disabled", () => {
  196. const rows = [
  197. toolCall(1, "Read", { path: "src/a.ts" }),
  198. toolCall(2, "Grep", { pattern: "test" }),
  199. toolCall(3, "Bash", { command: "rg test src" }),
  200. ];
  201. expect(
  202. buildAssistantWorkRenderItems(rows, SHOW_ALL_REASONING, {
  203. enableExploreGrouping: false,
  204. }).map((item) => item.kind),
  205. ).toEqual(["row", "row", "row"]);
  206. });
  207. it("keeps mutating Shell tools as individual rows when Terminal grouping is disabled", () => {
  208. const rows = [
  209. toolCall(1, "Bash", { command: "pnpm test" }),
  210. toolCall(2, "Bash", { command: "mkdir -p tmp/output" }),
  211. ];
  212. expect(
  213. buildAssistantWorkRenderItems(rows, SHOW_ALL_REASONING, {
  214. enableTerminalGrouping: false,
  215. }).map((item) => item.kind),
  216. ).toEqual(["row", "row"]);
  217. });
  218. it("keeps file-write tools as individual rows when Changes grouping uses the release default", () => {
  219. const rows = [
  220. toolCall(1, "Write", { file_path: "src/a.ts", content: "a" }),
  221. toolCall(2, "Edit", {
  222. file_path: "src/b.ts",
  223. old_string: "b",
  224. new_string: "c",
  225. }),
  226. toolCall(3, "ApplyPatch", {
  227. patch: "*** Begin Patch\n*** Add File: src/c.ts\n+c\n*** End Patch",
  228. }),
  229. ];
  230. expect(
  231. buildAssistantWorkRenderItems(rows, SHOW_ALL_REASONING).map((item) => item.kind),
  232. ).toEqual(["row", "row", "row"]);
  233. });
  234. it.each(["inputStreaming", "running"] as const)(
  235. "defers unclassified shell-family tools with status %s",
  236. (status) => {
  237. for (const toolName of ["Bash", "execute", "shell"]) {
  238. const pending = toolCall(
  239. 1,
  240. toolName,
  241. {},
  242. {
  243. input: undefined,
  244. inputText: "",
  245. status,
  246. },
  247. );
  248. expect(buildRenderItems([pending])).toEqual([]);
  249. }
  250. },
  251. );
  252. it("classifies a deferred shell tool after its command arrives", () => {
  253. const readonly = toolCall(
  254. 1,
  255. "Bash",
  256. { command: "rg test packages/ui" },
  257. { status: "inputStreaming" },
  258. );
  259. const executable = toolCall(2, "Bash", { command: "pnpm test" }, { status: "running" });
  260. expect(buildRenderItems([readonly])).toMatchObject([{ kind: "row", row: { rowId: 1 } }]);
  261. expect(buildRenderItems([executable])).toMatchObject([{ kind: "row", row: { rowId: 2 } }]);
  262. });
  263. it("keeps a running Explore tail stable while a later shell awaits its command", () => {
  264. const explore = toolCall(1, "Grep", { pattern: "renderer" });
  265. const pendingShell = toolCall(2, "Bash", undefined, {
  266. inputText: "",
  267. status: "inputStreaming",
  268. });
  269. const pendingItems = buildRenderItems([explore, pendingShell], SHOW_ALL_REASONING, {
  270. stageTailIsRunning: true,
  271. });
  272. expect(pendingItems).toHaveLength(1);
  273. expect(pendingItems[0]).toMatchObject({ kind: "row", row: { rowId: 1 } });
  274. const readonlyItems = buildRenderItems(
  275. [
  276. explore,
  277. toolCall(
  278. 2,
  279. "Bash",
  280. { command: "ls packages/ui/src/ToolCallBlocks/renderers" },
  281. { status: "running" },
  282. ),
  283. ],
  284. SHOW_ALL_REASONING,
  285. { stageTailIsRunning: true },
  286. );
  287. const readonlyExplore = readonlyItems[0];
  288. expect(readonlyExplore?.kind).toBe("exploreGroup");
  289. if (readonlyExplore?.kind !== "exploreGroup") {
  290. throw new Error("expected readonly shell to join Explore");
  291. }
  292. expect(readonlyExplore.rows.map((row) => row.rowId)).toEqual([1, 2]);
  293. expect(readonlyExplore.node.toolCall.status).toBe("in_progress");
  294. const executeItems = buildRenderItems(
  295. [explore, toolCall(2, "Bash", { command: "pnpm test" }, { status: "running" })],
  296. SHOW_ALL_REASONING,
  297. { stageTailIsRunning: true },
  298. );
  299. expect(executeItems.map((item) => item.kind)).toEqual(["row", "row"]);
  300. });
  301. it("upgrades consecutive non-explore shell calls to a stable group", () => {
  302. const initialItems = buildRenderItems([
  303. toolCall(1, "Bash", { command: "pnpm install" }, { status: "running" }),
  304. ]);
  305. const grownItems = buildRenderItems(
  306. [
  307. toolCall(1, "Bash", { command: "pnpm install" }),
  308. toolCall(2, "Bash", { command: "pnpm test" }, { status: "running" }),
  309. ],
  310. SHOW_ALL_REASONING,
  311. { stageTailIsRunning: true },
  312. );
  313. const extendedItems = buildRenderItems(
  314. [
  315. toolCall(1, "Bash", { command: "pnpm install" }),
  316. toolCall(2, "Bash", { command: "pnpm test" }),
  317. toolCall(3, "Bash", { command: "pnpm lint" }, { status: "running" }),
  318. ],
  319. SHOW_ALL_REASONING,
  320. { stageTailIsRunning: true },
  321. );
  322. const initialExecute = initialItems[0];
  323. const grownExecute = grownItems[0];
  324. const extendedExecute = extendedItems[0];
  325. expect(initialExecute?.kind).toBe("row");
  326. expect(grownExecute?.kind).toBe("executeGroup");
  327. expect(extendedExecute?.kind).toBe("executeGroup");
  328. if (grownExecute?.kind !== "executeGroup" || extendedExecute?.kind !== "executeGroup") {
  329. throw new Error("expected stable execute groups");
  330. }
  331. expect(extendedExecute.key).toBe(grownExecute.key);
  332. expect(extendedExecute.node.toolCall.toolId).toBe(grownExecute.node.toolCall.toolId);
  333. expect(grownExecute.rows.map((row) => row.rowId)).toEqual([1, 2]);
  334. expect(grownExecute.node.toolCall.status).toBe("in_progress");
  335. });
  336. it("keeps child failures on execute children and stops at explore boundaries", () => {
  337. const items = buildRenderItems([
  338. toolCall(1, "Bash", { command: "pnpm install" }),
  339. toolCall(2, "Bash", { command: "pnpm build" }, { status: "error" }),
  340. toolCall(3, "Bash", { command: "git status" }),
  341. toolCall(4, "Bash", { command: "pnpm test" }, { status: "cancelled" }),
  342. ]);
  343. expect(items.map((item) => item.kind)).toEqual(["executeGroup", "row", "row"]);
  344. const firstExecute = items[0];
  345. if (firstExecute?.kind !== "executeGroup") throw new Error("expected execute group");
  346. expect(firstExecute.node.toolCall.status).toBe("completed");
  347. expect(firstExecute.node.childToolCalls[1]?.toolCall.status).toBe("failed");
  348. expect(items[2]).toMatchObject({ kind: "row", row: { status: "cancelled" } });
  349. });
  350. it("merges execute calls across hidden later reasoning only", () => {
  351. const rows = [
  352. reasoning(1),
  353. toolCall(2, "Bash", { command: "pnpm install" }),
  354. reasoning(3),
  355. toolCall(4, "Bash", { command: "pnpm test" }),
  356. ];
  357. const hiddenItems = buildRenderItems(rows, {
  358. messageStreamShowReasoning: false,
  359. messageStreamFirstReasoningRowId: 1,
  360. });
  361. expect(hiddenItems.map((item) => item.kind)).toEqual(["row", "executeGroup"]);
  362. const hiddenExecute = hiddenItems[1];
  363. if (hiddenExecute?.kind !== "executeGroup") {
  364. throw new Error("expected merged execute group");
  365. }
  366. expect(hiddenExecute.rows.map((row) => row.rowId)).toEqual([2, 4]);
  367. expect(buildRenderItems(rows).map((item) => item.kind)).toEqual(["row", "row", "row", "row"]);
  368. });
  369. it("keeps an unclassified failed shell tool visible", () => {
  370. const failed = toolCall(
  371. 1,
  372. "Bash",
  373. {},
  374. {
  375. error: { message: "Permission denied" },
  376. input: undefined,
  377. inputText: "",
  378. status: "error",
  379. },
  380. );
  381. expect(buildRenderItems([failed])).toMatchObject([
  382. { kind: "row", row: { rowId: 1, status: "error" } },
  383. ]);
  384. });
  385. it("groups consecutive read-only tool calls into one explore item", () => {
  386. const rows = [
  387. toolCall(1, "Read", { path: "package.json" }),
  388. toolCall(2, "Bash", { command: "rg test packages/ui" }),
  389. ];
  390. const items = buildRenderItems(rows);
  391. expect(items).toHaveLength(1);
  392. expect(items[0]).toMatchObject({
  393. kind: "exploreGroup",
  394. rowId: 1,
  395. });
  396. const first = items[0];
  397. expect(first?.kind).toBe("exploreGroup");
  398. if (first?.kind !== "exploreGroup") {
  399. throw new Error("expected explore group");
  400. }
  401. expect(first.rows).toHaveLength(2);
  402. });
  403. it("upgrades read-only tools to an Explore group with stable identity", () => {
  404. const initialItems = buildRenderItems([
  405. toolCall(1, "Read", { path: "package.json" }, { status: "running" }),
  406. ]);
  407. const grownItems = buildRenderItems([
  408. toolCall(1, "Read", { path: "package.json" }, { status: "success" }),
  409. toolCall(2, "Bash", { command: "rg test packages/ui" }, { status: "running" }),
  410. ]);
  411. const extendedItems = buildRenderItems([
  412. toolCall(1, "Read", { path: "package.json" }),
  413. toolCall(2, "Bash", { command: "rg test packages/ui" }),
  414. toolCall(3, "Grep", { pattern: "renderer" }, { status: "running" }),
  415. ]);
  416. const initialExplore = initialItems[0];
  417. const grownExplore = grownItems[0];
  418. const extendedExplore = extendedItems[0];
  419. expect(initialExplore?.kind).toBe("row");
  420. expect(grownExplore?.kind).toBe("exploreGroup");
  421. expect(extendedExplore?.kind).toBe("exploreGroup");
  422. if (grownExplore?.kind !== "exploreGroup" || extendedExplore?.kind !== "exploreGroup") {
  423. throw new Error("expected stable explore groups");
  424. }
  425. expect(extendedExplore.key).toBe(grownExplore.key);
  426. expect(extendedExplore.node.toolCall.toolId).toBe(grownExplore.node.toolCall.toolId);
  427. expect(grownExplore.rows).toHaveLength(2);
  428. });
  429. it("derives Explore parent status from the running stage tail", () => {
  430. const completedItems = buildRenderItems(
  431. [
  432. toolCall(1, "Read", { path: "package.json" }),
  433. toolCall(2, "Grep", { pattern: "missing" }, { status: "error" }),
  434. ],
  435. SHOW_ALL_REASONING,
  436. { stageTailIsRunning: true },
  437. );
  438. const boundedItems = buildRenderItems([
  439. toolCall(1, "Read", { path: "missing.json" }, { status: "error" }),
  440. toolCall(2, "Grep", { pattern: "still-running" }, { status: "running" }),
  441. toolCall(3, "Bash", { command: "pnpm test" }, { status: "running" }),
  442. ]);
  443. const runningExplore = completedItems[0];
  444. const boundedExplore = boundedItems[0];
  445. expect(runningExplore?.kind).toBe("exploreGroup");
  446. expect(boundedExplore?.kind).toBe("exploreGroup");
  447. if (runningExplore?.kind !== "exploreGroup" || boundedExplore?.kind !== "exploreGroup") {
  448. throw new Error("expected explore groups");
  449. }
  450. expect(runningExplore.node.toolCall.status).toBe("in_progress");
  451. expect(runningExplore.node.childToolCalls[1]?.toolCall.status).toBe("failed");
  452. expect(boundedExplore.node.toolCall.status).toBe("completed");
  453. expect(boundedExplore.node.childToolCalls[1]?.toolCall.status).toBe("in_progress");
  454. });
  455. it("derives Execute parent status from the running stage tail", () => {
  456. const tailItems = buildRenderItems(
  457. [
  458. toolCall(1, "Bash", { command: "pnpm test" }),
  459. toolCall(2, "Bash", { command: "pnpm lint" }),
  460. ],
  461. SHOW_ALL_REASONING,
  462. { stageTailIsRunning: true },
  463. );
  464. const boundedItems = buildRenderItems(
  465. [
  466. toolCall(1, "Bash", { command: "pnpm test" }, { status: "running" }),
  467. toolCall(2, "Bash", { command: "pnpm lint" }),
  468. toolCall(3, "Read", { path: "package.json" }),
  469. ],
  470. SHOW_ALL_REASONING,
  471. { stageTailIsRunning: true },
  472. );
  473. const terminalItems = buildRenderItems([
  474. toolCall(1, "Bash", { command: "pnpm test" }, { status: "running" }),
  475. toolCall(2, "Bash", { command: "pnpm lint" }),
  476. ]);
  477. const tailExecute = tailItems[0];
  478. const boundedExecute = boundedItems[0];
  479. const terminalExecute = terminalItems[0];
  480. expect(tailExecute?.kind).toBe("executeGroup");
  481. expect(boundedExecute?.kind).toBe("executeGroup");
  482. expect(terminalExecute?.kind).toBe("executeGroup");
  483. if (
  484. tailExecute?.kind !== "executeGroup" ||
  485. boundedExecute?.kind !== "executeGroup" ||
  486. terminalExecute?.kind !== "executeGroup"
  487. ) {
  488. throw new Error("expected execute groups");
  489. }
  490. expect(tailExecute.node.toolCall.status).toBe("in_progress");
  491. expect(boundedExecute.node.toolCall.status).toBe("completed");
  492. expect(boundedExecute.node.childToolCalls[0]?.toolCall.status).toBe("in_progress");
  493. expect(terminalExecute.node.toolCall.status).toBe("completed");
  494. });
  495. it("merges explore calls across hidden later reasoning only", () => {
  496. const rows = [
  497. reasoning(1),
  498. toolCall(2, "Read", { path: "package.json" }),
  499. reasoning(3),
  500. toolCall(4, "Bash", { command: "rg test packages/ui" }),
  501. ];
  502. const hiddenReasoningItems = buildRenderItems(
  503. rows,
  504. {
  505. messageStreamShowReasoning: false,
  506. messageStreamFirstReasoningRowId: 1,
  507. },
  508. { stageTailIsRunning: true },
  509. );
  510. expect(hiddenReasoningItems.map((item) => item.kind)).toEqual(["row", "exploreGroup"]);
  511. const hiddenExplore = hiddenReasoningItems[1];
  512. expect(hiddenExplore?.kind).toBe("exploreGroup");
  513. if (hiddenExplore?.kind !== "exploreGroup") {
  514. throw new Error("expected merged explore group");
  515. }
  516. expect(hiddenExplore.rows.map((row) => row.rowId)).toEqual([2, 4]);
  517. expect(hiddenExplore.node.toolCall.status).toBe("in_progress");
  518. const visibleReasoningItems = buildRenderItems(rows, SHOW_ALL_REASONING, {
  519. stageTailIsRunning: true,
  520. });
  521. expect(visibleReasoningItems.map((item) => item.kind)).toEqual(["row", "row", "row", "row"]);
  522. });
  523. it("ends Explore and starts a Changes group for write tools", () => {
  524. const rows = [
  525. toolCall(1, "Read", { path: "package.json" }),
  526. toolCall(2, "Edit", { path: "package.json", newText: "{}" }),
  527. {
  528. rowId: 3,
  529. turnId: "turn-1",
  530. createdAt: 1_700_000_003,
  531. createdAtSeq: 3,
  532. kind: "assistantText",
  533. text: "done",
  534. state: "complete",
  535. } satisfies ConversationRow,
  536. ];
  537. const items = buildRenderItems(rows);
  538. expect(items.map((item) => item.kind)).toEqual(["row", "row", "row"]);
  539. });
  540. it("upgrades consecutive Write/Edit rows to a stable group", () => {
  541. const initial = buildRenderItems([
  542. toolCall(1, "Write", { file_path: "src/a.ts", content: "a" }),
  543. ]);
  544. const grown = buildRenderItems(
  545. [
  546. toolCall(1, "Write", { file_path: "src/a.ts", content: "a" }),
  547. toolCall(2, "Edit", {
  548. file_path: "src/b.ts",
  549. old_string: "b",
  550. new_string: "c",
  551. }),
  552. ],
  553. SHOW_ALL_REASONING,
  554. { stageTailIsRunning: true },
  555. );
  556. const extended = buildRenderItems(
  557. [
  558. toolCall(1, "Write", { file_path: "src/a.ts", content: "a" }),
  559. toolCall(2, "Edit", {
  560. file_path: "src/b.ts",
  561. old_string: "b",
  562. new_string: "c",
  563. }),
  564. toolCall(3, "Write", { file_path: "src/c.ts", content: "c" }),
  565. ],
  566. SHOW_ALL_REASONING,
  567. { stageTailIsRunning: true },
  568. );
  569. const first = initial[0];
  570. const next = grown[0];
  571. const last = extended[0];
  572. expect(first?.kind).toBe("row");
  573. expect(next?.kind).toBe("changesGroup");
  574. expect(last?.kind).toBe("changesGroup");
  575. if (next?.kind !== "changesGroup" || last?.kind !== "changesGroup")
  576. throw new Error("expected Changes groups");
  577. expect(last.key).toBe(next.key);
  578. expect(last.node.toolCall.toolId).toBe(next.node.toolCall.toolId);
  579. expect(next.rows.map((row) => row.rowId)).toEqual([1, 2]);
  580. expect(next.node.toolCall.status).toBe("in_progress");
  581. });
  582. it("completes Changes at visible boundaries without inheriting child cancellation", () => {
  583. const items = buildRenderItems([
  584. toolCall(
  585. 1,
  586. "Edit",
  587. { file_path: "src/a.ts", old_string: "a", new_string: "b" },
  588. { status: "cancelled" },
  589. ),
  590. toolCall(2, "Write", { file_path: "src/b.ts", content: "b" }),
  591. toolCall(3, "Read", { path: "src/a.ts" }),
  592. ]);
  593. const changes = items[0];
  594. expect(changes?.kind).toBe("changesGroup");
  595. if (changes?.kind !== "changesGroup") throw new Error("expected Changes group");
  596. expect(changes.node.toolCall.status).toBe("completed");
  597. expect(changes.node.childToolCalls[0]?.toolCall.status).toBe("stopped");
  598. });
  599. it("merges Changes across hidden later reasoning but splits at visible reasoning", () => {
  600. const rows = [
  601. reasoning(1),
  602. toolCall(2, "Write", { file_path: "src/a.ts", content: "a" }),
  603. reasoning(3),
  604. toolCall(4, "Edit", { file_path: "src/b.ts", old_string: "b", new_string: "c" }),
  605. ];
  606. const hidden = buildRenderItems(rows, {
  607. messageStreamShowReasoning: false,
  608. messageStreamFirstReasoningRowId: 1,
  609. });
  610. expect(hidden.map((item) => item.kind)).toEqual(["row", "changesGroup"]);
  611. expect(buildRenderItems(rows).map((item) => item.kind)).toEqual(["row", "row", "row", "row"]);
  612. });
  613. it("pairs agent tool calls with subagent rows by parentToolCallId", () => {
  614. const rows = [
  615. toolCall(1, "Agent", {
  616. prompt: "分析当前工作区",
  617. subagent_type: "Explore",
  618. }),
  619. subagent(2, { parentToolCallId: "tool-1" }),
  620. toolCall(3, "Read", { path: "package.json" }),
  621. ];
  622. const items = buildRenderItems(rows);
  623. expect(items.map((item) => item.kind)).toEqual(["agentToolCall", "row"]);
  624. const agentItem = items[0];
  625. expect(agentItem?.kind).toBe("agentToolCall");
  626. if (agentItem?.kind !== "agentToolCall") {
  627. throw new Error("expected paired agent item");
  628. }
  629. expect(agentItem.row.rowId).toBe(1);
  630. expect(agentItem.subagentRow.rowId).toBe(2);
  631. expect(agentItem).not.toHaveProperty("showSubagentOutputPreview");
  632. expect(items.some((item) => item.kind === "row" && item.row.rowId === 2)).toBe(false);
  633. });
  634. it("keeps concurrent agent details bound when subagents spawn in reverse order", () => {
  635. const rows = [
  636. toolCall(1, "Agent", { description: "测试 general-purpose agent" }),
  637. toolCall(2, "Agent", { description: "测试 Explore agent" }),
  638. toolCall(3, "Agent", { description: "测试 test-hello2 agent" }),
  639. subagent(4, {
  640. childSessionId: "child-test-hello2",
  641. parentToolCallId: "tool-3",
  642. subagentType: "test-hello2",
  643. }),
  644. subagent(5, {
  645. childSessionId: "child-explore",
  646. parentToolCallId: "tool-2",
  647. subagentType: "Explore",
  648. }),
  649. subagent(6, {
  650. childSessionId: "child-general-purpose",
  651. parentToolCallId: "tool-1",
  652. subagentType: "general-purpose",
  653. }),
  654. ];
  655. const items = buildRenderItems(rows);
  656. const pairs = items.flatMap((item) =>
  657. item.kind === "agentToolCall" ? [[item.row.toolCallId, item.subagentRow.childSessionId]] : [],
  658. );
  659. expect(pairs).toEqual([
  660. ["tool-1", "child-general-purpose"],
  661. ["tool-2", "child-explore"],
  662. ["tool-3", "child-test-hello2"],
  663. ]);
  664. });
  665. it("only falls back for one unambiguous legacy pair", () => {
  666. const onePair = buildRenderItems([
  667. toolCall(1, "Agent", { description: "旧 Agent" }),
  668. subagent(2),
  669. ]);
  670. expect(onePair).toMatchObject([
  671. {
  672. kind: "agentToolCall",
  673. row: { rowId: 1 },
  674. subagentRow: { rowId: 2 },
  675. },
  676. ]);
  677. const ambiguous = buildRenderItems([
  678. toolCall(1, "Agent", { description: "旧 Agent A" }),
  679. toolCall(2, "Agent", { description: "旧 Agent B" }),
  680. subagent(3),
  681. subagent(4),
  682. ]);
  683. expect(ambiguous.some((item) => item.kind === "agentToolCall")).toBe(false);
  684. expect(ambiguous.map((item) => item.kind)).toEqual(["row", "row", "row", "row"]);
  685. });
  686. it("keeps background subagents paired without requesting an inline output preview", () => {
  687. const rows = [
  688. toolCall(1, "Agent", {
  689. prompt: "后台分析当前工作区",
  690. run_in_background: true,
  691. subagent_type: "Explore",
  692. }),
  693. subagent(2, { backgrounded: true, parentToolCallId: "tool-1" }),
  694. ];
  695. const items = buildRenderItems(rows);
  696. expect(items).toHaveLength(1);
  697. const agentItem = items[0];
  698. expect(agentItem?.kind).toBe("agentToolCall");
  699. if (agentItem?.kind !== "agentToolCall") {
  700. throw new Error("expected paired agent item");
  701. }
  702. expect(agentItem.subagentRow.backgrounded).toBe(true);
  703. expect(agentItem).not.toHaveProperty("showSubagentOutputPreview");
  704. });
  705. });