workflowTurnDigests.test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // 轮尾 run 卡的解析(docs/dynamic-workflow/presentation.md「The run card」):凡点名了 runId 的来源都出一张卡
  2. // ——直接启动轮按元数据、CreateWorkflow 按 toolCallId、ResumeWorkflowRun 按 runId;图按 run 的发起
  3. // toolCallId 到图表里取;联接不到活投影的来源出中性卡(summary 缺席);同 run 一轮一张。
  4. import { describe, expect, it } from "vitest";
  5. import type { ToolCallRow, WorkflowLaunchMeta } from "@zcode/shared/zcode-protocol-v4";
  6. import type { WorkflowRunCardSummary } from "@/ToolCallBlocks/shared.js";
  7. import { resolveWorkflowTurnDigests } from "@/v4/workflowTurnDigests.js";
  8. const GRAPH = {
  9. steps: [{ id: "ask#1", kind: "ask", label: "a", line: 1, column: 21, lane: "actor#1" }],
  10. lanes: [{ id: "actor#1", name: "a", line: 1, column: 11 }],
  11. participants: [{ id: "unphased:actor#1", phase: "unphased", lane: "actor#1", steps: ["ask#1"] }],
  12. handoffs: [],
  13. };
  14. function toolCallRow(
  15. rowId: number,
  16. toolName: string,
  17. input: unknown,
  18. overrides: Partial<ToolCallRow> = {},
  19. ): ToolCallRow {
  20. return {
  21. rowId,
  22. turnId: "turn",
  23. createdAt: 1_700_000_000_000 + rowId,
  24. createdAtSeq: rowId,
  25. kind: "toolCall",
  26. toolCallId: `tool-${rowId}`,
  27. toolName,
  28. status: "success",
  29. input,
  30. inputText: JSON.stringify(input),
  31. ...overrides,
  32. };
  33. }
  34. function summary(runId: string, toolCallId: string): WorkflowRunCardSummary {
  35. return { runId, toolCallId, status: "running", nodesSettled: 0, nodesTotal: 1 };
  36. }
  37. const CREATE = toolCallRow(5, "CreateWorkflow", { name: "implement-verify", script: "phase('a')" });
  38. const GRAPHS = new Map([["tool-5", GRAPH]]);
  39. const LAUNCH: WorkflowLaunchMeta = {
  40. runId: "run-l",
  41. toolCallId: "launch-abc",
  42. name: "deep-research",
  43. scope: "project",
  44. path: "/w/.zcode/workflows/deep-research.dwf.ts",
  45. };
  46. describe("resolveWorkflowTurnDigests", () => {
  47. // 设置轮(docs/dynamic-workflow/presentation.md「The settings turn」):与直接启动同一份元数据,多一块
  48. // `amend`;卡照旧是新 run 的卡,卡上方那一行的素材(改了什么、那一轮的时刻)随卡带出去。
  49. it("设置轮:一张新 run 的卡,带着 amend 与那一轮的时刻;普通启动轮没有这一块", () => {
  50. const amend = {
  51. predecessorRunId: "run-a",
  52. maxConcurrency: { from: 13, to: 4 },
  53. ceiling: 13,
  54. };
  55. const settingsTurn: WorkflowLaunchMeta = {
  56. runId: "run-b",
  57. toolCallId: "settings-1",
  58. name: "deep-research",
  59. amend,
  60. };
  61. const digests = resolveWorkflowTurnDigests(
  62. { workflowLaunch: settingsTurn, assistantWorkRows: [], startedAt: 1_700_000_000_000 },
  63. { byRunId: new Map([["run-b", summary("run-b", "settings-1")]]) },
  64. );
  65. expect(digests).toHaveLength(1);
  66. expect(digests[0]).toMatchObject({
  67. key: "launch:settings-1",
  68. runId: "run-b",
  69. toolCallId: "settings-1",
  70. settings: { amend, at: 1_700_000_000_000 },
  71. });
  72. const launch = resolveWorkflowTurnDigests(
  73. { workflowLaunch: LAUNCH, assistantWorkRows: [], startedAt: 1 },
  74. {},
  75. );
  76. expect(launch[0]).not.toHaveProperty("settings");
  77. });
  78. // 就地生效的设置轮(docs/dynamic-workflow/presentation.md「The settings turn」):`amend` 不带
  79. // predecessorRunId——它点名的 run 就是自己,卡已经在它启动的那一轮里了。只出行、不出卡。
  80. it("就地生效的设置轮:只出那一行(rowOnly),不再出第二张卡", () => {
  81. const amend = { maxConcurrency: { from: 13, to: 4 }, ceiling: 13 };
  82. const retune: WorkflowLaunchMeta = {
  83. runId: "run-l",
  84. toolCallId: "settings-2",
  85. name: "deep-research",
  86. amend,
  87. };
  88. const digests = resolveWorkflowTurnDigests(
  89. { workflowLaunch: retune, assistantWorkRows: [], startedAt: 1_700_000_000_000 },
  90. { byRunId: new Map([["run-l", summary("run-l", "launch-abc")]]) },
  91. );
  92. expect(digests).toHaveLength(1);
  93. expect(digests[0]).toMatchObject({
  94. key: "launch:settings-2",
  95. runId: "run-l",
  96. rowOnly: true,
  97. settings: { amend, at: 1_700_000_000_000 },
  98. });
  99. });
  100. // 只出行的那条来源没有占掉「一轮一张卡」的名额:同一轮里真正启动了这条 run 的来源照常出卡。
  101. it("rowOnly 不挡同一轮里真正的发起来源出卡", () => {
  102. const retune: WorkflowLaunchMeta = {
  103. runId: "run-1",
  104. toolCallId: "settings-3",
  105. amend: { maxConcurrency: { from: 13, to: 4 } },
  106. };
  107. const digests = resolveWorkflowTurnDigests(
  108. { workflowLaunch: retune, assistantWorkRows: [CREATE] },
  109. { byToolCallId: new Map([["tool-5", summary("run-1", "tool-5")]]) },
  110. );
  111. expect(digests).toHaveLength(2);
  112. expect(digests[0]).toMatchObject({ rowOnly: true, runId: "run-1" });
  113. expect(digests[1]?.runId).toBe("run-1");
  114. expect(digests[1]).not.toHaveProperty("rowOnly");
  115. });
  116. // 就地生效的 AmendWorkflow 工具行(docs/dynamic-workflow/concurrency.md):它没有铸新 run,所以
  117. // 按 toolCallId 什么都联接不到,轮尾也就没有卡——那条 run 的卡在它启动的那一轮里。行本身退成
  118. // 一条设置行(create-workflow 渲染器按 display.retune 走)。
  119. it("就地生效的 AmendWorkflow 行不出轮尾卡:它没有自己的 run", () => {
  120. const retuneRow = toolCallRow(7, "AmendWorkflow", { run_id: "run-1", max_concurrency: 4 });
  121. const digests = resolveWorkflowTurnDigests(
  122. { assistantWorkRows: [retuneRow] },
  123. { byRunId: new Map([["run-1", summary("run-1", "tool-5")]]) },
  124. );
  125. expect(digests).toEqual([]);
  126. });
  127. it("联接到 run 的 CreateWorkflow 行成一张卡:名字从行上读,图按发起 toolCallId 到图表取", () => {
  128. const byToolCallId = new Map([["tool-5", summary("run-1", "tool-5")]]);
  129. const digests = resolveWorkflowTurnDigests(
  130. { assistantWorkRows: [CREATE] },
  131. { byToolCallId, graphByToolCallId: GRAPHS },
  132. );
  133. expect(digests).toHaveLength(1);
  134. expect(digests[0]).toMatchObject({
  135. key: "5:tool-5",
  136. name: "implement-verify",
  137. runId: "run-1",
  138. toolCallId: "tool-5",
  139. summary: { runId: "run-1" },
  140. });
  141. expect(digests[0]!.graph?.steps).toHaveLength(1);
  142. });
  143. it("没联接到 run 的 CreateWorkflow 行(被拒绝 / 编不过)没有卡:行本身不点名 runId;图表没有它的图时卡无图", () => {
  144. expect(resolveWorkflowTurnDigests({ assistantWorkRows: [CREATE] }, {})).toEqual([]);
  145. const digests = resolveWorkflowTurnDigests(
  146. { assistantWorkRows: [CREATE] },
  147. { byToolCallId: new Map([["tool-5", summary("run-2", "tool-5")]]) },
  148. );
  149. expect(digests).toHaveLength(1);
  150. expect(digests[0]!.graph).toBeUndefined();
  151. });
  152. it("ResumeWorkflowRun 行按 runId 联接,图取自 run 的发起行;同一轮里已有同 run 的 Create 卡时并入、不开第二张", () => {
  153. const resume = toolCallRow(
  154. 7,
  155. "ResumeWorkflowRun",
  156. { runId: "run-1" },
  157. { display: { kind: "resume_workflow_run", runId: "run-1" } as ToolCallRow["display"] },
  158. );
  159. const byRunId = new Map([["run-1", summary("run-1", "tool-5")]]);
  160. const alone = resolveWorkflowTurnDigests(
  161. { assistantWorkRows: [resume] },
  162. { byRunId, graphByToolCallId: GRAPHS },
  163. );
  164. expect(alone).toHaveLength(1);
  165. expect(alone[0]).toMatchObject({ name: undefined, runId: "run-1", toolCallId: "tool-7" });
  166. // 图是 run 的属性:resume 卡与发起行同一张图。
  167. expect(alone[0]!.graph?.steps).toHaveLength(1);
  168. const both = resolveWorkflowTurnDigests(
  169. { assistantWorkRows: [CREATE, resume] },
  170. { byToolCallId: new Map([["tool-5", summary("run-1", "tool-5")]]), byRunId },
  171. );
  172. expect(both).toHaveLength(1);
  173. expect(both[0]!.toolCallId).toBe("tool-5");
  174. });
  175. it("联接不到活投影的 ResumeWorkflowRun 行仍出卡(中性):summary 缺席、发起行不可知故无图", () => {
  176. const resume = toolCallRow(
  177. 7,
  178. "ResumeWorkflowRun",
  179. { runId: "run-gone" },
  180. { display: { kind: "resume_workflow_run", runId: "run-gone" } as ToolCallRow["display"] },
  181. );
  182. const digests = resolveWorkflowTurnDigests(
  183. { assistantWorkRows: [resume] },
  184. { graphByToolCallId: GRAPHS },
  185. );
  186. expect(digests).toEqual([
  187. {
  188. graph: undefined,
  189. key: "7:tool-7",
  190. name: undefined,
  191. runId: "run-gone",
  192. summary: undefined,
  193. toolCallId: "tool-7",
  194. },
  195. ]);
  196. });
  197. it("直接启动轮:元数据自带 runId / toolCallId / 名字,按 runId 联接,图按 toolCallId 取;排在工具行之前", () => {
  198. const byRunId = new Map([["run-l", summary("run-l", "launch-abc")]]);
  199. const digests = resolveWorkflowTurnDigests(
  200. { workflowLaunch: LAUNCH, assistantWorkRows: [] },
  201. { byRunId, graphByToolCallId: new Map([["launch-abc", GRAPH]]) },
  202. );
  203. expect(digests).toHaveLength(1);
  204. expect(digests[0]).toMatchObject({
  205. key: "launch:launch-abc",
  206. name: "deep-research",
  207. runId: "run-l",
  208. toolCallId: "launch-abc",
  209. summary: { runId: "run-l" },
  210. });
  211. expect(digests[0]!.graph?.steps).toHaveLength(1);
  212. // run 不在投影里(淘汰 / 冷恢复):仍出卡,summary 缺席。
  213. const cold = resolveWorkflowTurnDigests({ workflowLaunch: LAUNCH, assistantWorkRows: [] }, {});
  214. expect(cold).toHaveLength(1);
  215. expect(cold[0]).toMatchObject({ runId: "run-l", summary: undefined, graph: undefined });
  216. });
  217. it("非工具行与不相干的工具行一律略过", () => {
  218. const digests = resolveWorkflowTurnDigests(
  219. {
  220. assistantWorkRows: [
  221. toolCallRow(1, "Read", { file_path: "a" }),
  222. {
  223. rowId: 2,
  224. turnId: "turn",
  225. createdAt: 2,
  226. createdAtSeq: 2,
  227. kind: "reasoning",
  228. state: "complete",
  229. text: "…",
  230. },
  231. ],
  232. },
  233. { byToolCallId: new Map([["tool-9", summary("run-9", "tool-9")]]) },
  234. );
  235. expect(digests).toEqual([]);
  236. });
  237. });