workflowRunCardJoin.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // 工具卡 → dwf run 的联接(docs/dynamic-workflow/presentation.md「Joining cards to runs」那一行)。
  2. //
  3. // 值从裸 runId 加宽成 `{runId, status, nodesSettled, nodesTotal}`:聊天区的紧凑卡要直接
  4. // 渲染状态词与步数进度,所以联接一次就把摘要算完,卡片不再自己去翻投影。
  5. //
  6. // 计数语义写明:**动态工作流没有静态总数**,`nodesTotal` 是**已排程**(observed)节点数,
  7. // 进度读作「已排程的里结算了几个」,绝不冒充全程百分比。
  8. import { describe, expect, it } from "vitest";
  9. import type { WorkflowRunNode, WorkflowRunState } from "@zcode/shared/zcode-protocol-v4";
  10. import { workflowRunStepCounts } from "@zcode/shared/zcode-protocol-v4";
  11. import {
  12. buildWorkflowGraphByToolCallId,
  13. buildWorkflowRunByRunId,
  14. buildWorkflowRunByToolCallId,
  15. buildWorkflowRunPendingQuestionsByRunId,
  16. resolveWorkflowRunOpenToolCallId,
  17. } from "@/v4/workflowRunCardJoin.js";
  18. import type { ConversationRow } from "@zcode/shared/zcode-protocol-v4";
  19. function node(
  20. overrides: Partial<WorkflowRunNode> & Pick<WorkflowRunNode, "phase">,
  21. ): WorkflowRunNode {
  22. return { siteId: "ask#1", ordinal: 1, ...overrides };
  23. }
  24. function run(overrides: Partial<WorkflowRunState> = {}): WorkflowRunState {
  25. return {
  26. runId: "dwfrun-1",
  27. toolCallId: "tool-wf-1",
  28. status: "running",
  29. usage: { spentTokens: 0, nodesUsed: 0 },
  30. actors: [],
  31. nodes: [],
  32. lastEventSequence: 0,
  33. ...overrides,
  34. };
  35. }
  36. describe("buildWorkflowRunByToolCallId", () => {
  37. it("按 toolCallId 建表,并带上卡片要渲染的 runId 与状态", () => {
  38. const map = buildWorkflowRunByToolCallId([run({ runId: "dwfrun-7", status: "completed" })]);
  39. expect(map.get("tool-wf-1")).toMatchObject({
  40. runId: "dwfrun-7",
  41. status: "completed",
  42. nodesSettled: 0,
  43. nodesTotal: 0,
  44. });
  45. });
  46. it("nodesSettled 只数 settled 相位,nodesTotal 数全部已排程节点", () => {
  47. const map = buildWorkflowRunByToolCallId([
  48. run({
  49. nodes: [
  50. node({ phase: "settled", outcome: "ok" }),
  51. node({ ordinal: 2, phase: "settled", outcome: "failed" }),
  52. // 取消同样是结算:终态就是终态,进度不该把它算成「还在跑」。
  53. node({ ordinal: 3, phase: "settled", outcome: "cancelled" }),
  54. node({ ordinal: 4, phase: "dispatched" }),
  55. node({ ordinal: 5, phase: "repairing" }),
  56. node({ ordinal: 6, phase: "nudged" }),
  57. node({ ordinal: 7, phase: "queued" }),
  58. ],
  59. }),
  60. ]);
  61. expect(map.get("tool-wf-1")).toMatchObject({ nodesSettled: 3, nodesTotal: 7 });
  62. });
  63. it("一个节点都还没排程时给 0/0 而不是缺席", () => {
  64. const map = buildWorkflowRunByToolCallId([run({ status: "pending" })]);
  65. expect(map.get("tool-wf-1")).toMatchObject({
  66. nodesSettled: 0,
  67. nodesTotal: 0,
  68. status: "pending",
  69. });
  70. });
  71. it("没有 toolCallId 的 run 不进表(它没有可点的卡片)", () => {
  72. const map = buildWorkflowRunByToolCallId([
  73. run({ runId: "dwfrun-headless", toolCallId: undefined }),
  74. ]);
  75. expect(map.size).toBe(0);
  76. });
  77. it("多条 run 各自成键", () => {
  78. const map = buildWorkflowRunByToolCallId([
  79. run({ runId: "dwfrun-1", toolCallId: "tool-a", nodes: [node({ phase: "settled" })] }),
  80. run({ runId: "dwfrun-2", toolCallId: "tool-b", status: "errored" }),
  81. ]);
  82. expect(map.size).toBe(2);
  83. expect(map.get("tool-a")).toMatchObject({ runId: "dwfrun-1", nodesSettled: 1, nodesTotal: 1 });
  84. expect(map.get("tool-b")).toMatchObject({ runId: "dwfrun-2", status: "errored" });
  85. });
  86. it("活投影命中时整条 run 随摘要带上:卡片内联的时间线要灯、药丸与墨迹", () => {
  87. // docs/dynamic-workflow/presentation.md「Joining cards to runs」:renderer 仍然不翻投影,
  88. // 联接一次把整条 run 交给它。
  89. const state = run({ actors: [{ siteId: "actor#1", ordinal: 1, status: "running" }] });
  90. const map = buildWorkflowRunByToolCallId([state]);
  91. expect(map.get("tool-wf-1")?.run).toBe(state);
  92. });
  93. it("可恢复性只读投影的 resumable 状态位,不按 status 推导", () => {
  94. // docs/dynamic-workflow/presentation.md:CLI 在 run-settled 载荷上按 resume 门裁定,
  95. // reducer 搬运;一个 stopped 但没带该位的 run(旧 CLI)不可恢复。
  96. expect(
  97. buildWorkflowRunByToolCallId([run({ status: "stopped", resumable: true })]).get("tool-wf-1")
  98. ?.resumable,
  99. ).toBe(true);
  100. expect(
  101. buildWorkflowRunByToolCallId([
  102. run({ status: "stopped", stopReason: "interrupted", resumable: true }),
  103. ]).get("tool-wf-1")?.resumable,
  104. ).toBe(true);
  105. expect(
  106. buildWorkflowRunByToolCallId([run({ status: "stopped" })]).get("tool-wf-1")?.resumable,
  107. ).toBeUndefined();
  108. expect(
  109. buildWorkflowRunByRunId([run({ status: "stopped", resumable: true })]).get("dwfrun-1")
  110. ?.resumable,
  111. ).toBe(true);
  112. });
  113. it("投影里还没有 workflowRuns 时返回空表", () => {
  114. expect(buildWorkflowRunByToolCallId(undefined).size).toBe(0);
  115. expect(buildWorkflowRunByToolCallId([]).size).toBe(0);
  116. });
  117. });
  118. // 计数规则本身住在 @zcode/shared(表内 + 表外,唯一实现);这里钉的是**建表读的是它**——
  119. // 联接表与状态胶囊、时间线摘要必须报同一个数,此前三处各数一遍,撞过界的 run 在三个地方
  120. // 显示三个都偏小的数字。
  121. describe("步数来源", () => {
  122. it("建表的两个计数就是 workflowRunStepCounts 的两个数", () => {
  123. const sample = run({
  124. nodes: [node({ phase: "settled" }), node({ ordinal: 2, phase: "nudged" })],
  125. });
  126. const fromMap = buildWorkflowRunByToolCallId([sample]).get("tool-wf-1");
  127. expect({ settled: fromMap?.nodesSettled, total: fromMap?.nodesTotal }).toEqual(
  128. workflowRunStepCounts(sample),
  129. );
  130. });
  131. it("撞界后没进表的实例照样算进卡片的步数", () => {
  132. const sample = run({
  133. nodes: [node({ phase: "settled" }), node({ ordinal: 2, phase: "queued" })],
  134. usage: { spentTokens: 0, nodesUsed: 2, nodesUnlisted: 40, nodesUnlistedSettled: 31 },
  135. truncated: true,
  136. });
  137. expect(buildWorkflowRunByToolCallId([sample]).get("tool-wf-1")).toMatchObject({
  138. nodesSettled: 32,
  139. nodesTotal: 42,
  140. });
  141. expect(buildWorkflowRunByRunId([sample]).get("dwfrun-1")).toMatchObject({
  142. nodesSettled: 32,
  143. nodesTotal: 42,
  144. });
  145. });
  146. });
  147. // byRunId 联接(docs/dynamic-workflow/presentation.md「Joining cards to runs」二轮):
  148. // ResumeWorkflowRun 行的 display 带 runId,投影的 toolCallId 跨 resume 沿用原始 CreateWorkflow
  149. // 行,所以 resume 行只能按 runId 联接同一份投影。
  150. describe("buildWorkflowRunByRunId", () => {
  151. it("按 runId 建表,摘要与 byToolCallId 表同构(同一份投影的两个索引)", () => {
  152. const map = buildWorkflowRunByRunId([
  153. run({
  154. runId: "dwfrun-resumed",
  155. status: "running",
  156. nodes: [
  157. node({ phase: "settled", outcome: "ok" }),
  158. node({ ordinal: 2, phase: "settled", outcome: "ok" }),
  159. node({ ordinal: 3, phase: "dispatched" }),
  160. ],
  161. }),
  162. ]);
  163. expect(map.get("dwfrun-resumed")).toMatchObject({
  164. runId: "dwfrun-resumed",
  165. status: "running",
  166. nodesSettled: 2,
  167. nodesTotal: 3,
  168. toolCallId: "tool-wf-1",
  169. });
  170. });
  171. it("没有 toolCallId 的 run 也进 byRunId 表(resume 联接不依赖 create 行的键)", () => {
  172. const map = buildWorkflowRunByRunId([run({ runId: "dwfrun-x", toolCallId: undefined })]);
  173. expect(map.get("dwfrun-x")?.runId).toBe("dwfrun-x");
  174. });
  175. });
  176. // bug 修复的钉子(spec「工具卡 display」三轮修订):打开请求的 toolCallId 必须是**发起
  177. // CreateWorkflow 行**的 id——WorkflowRunSidePane 拿它找 causalityGraph 与脚本;resume 行
  178. // 自己的 id 会让详情页落「可见历史里没有这张工作流图」。
  179. describe("byRunId 摘要的 toolCallId 传播", () => {
  180. it("摘要携带投影里 run 的 toolCallId(原始 create 行 id,跨 resume 不变)", () => {
  181. const map = buildWorkflowRunByRunId([run({ runId: "dwfrun-r", toolCallId: "tool-create-1" })]);
  182. expect(map.get("dwfrun-r")?.toolCallId).toBe("tool-create-1");
  183. });
  184. it("投影 run 无 toolCallId 时摘要不带该键(回落语义由 resolve helper 表达)", () => {
  185. const map = buildWorkflowRunByRunId([run({ runId: "dwfrun-x", toolCallId: undefined })]);
  186. expect(map.get("dwfrun-x")?.toolCallId).toBeUndefined();
  187. });
  188. });
  189. describe("resolveWorkflowRunOpenToolCallId", () => {
  190. it("resume 行点开:摘要带投影 id 时恒用投影值(点中行的 id 不是发起行)", () => {
  191. expect(
  192. resolveWorkflowRunOpenToolCallId("tool-resume-1", {
  193. runId: "dwfrun-r",
  194. status: "running",
  195. nodesSettled: 0,
  196. nodesTotal: 0,
  197. toolCallId: "tool-create-1",
  198. }),
  199. ).toBe("tool-create-1");
  200. });
  201. it("create 行点开:摘要 id 与点中行 id 本就相等,结果不变", () => {
  202. expect(
  203. resolveWorkflowRunOpenToolCallId("tool-create-1", {
  204. runId: "dwfrun-r",
  205. status: "running",
  206. nodesSettled: 0,
  207. nodesTotal: 0,
  208. toolCallId: "tool-create-1",
  209. }),
  210. ).toBe("tool-create-1");
  211. });
  212. it("摘要缺席回落点中行的 id(既有行为)", () => {
  213. expect(
  214. resolveWorkflowRunOpenToolCallId("tool-create-1", {
  215. runId: "dwfrun-r",
  216. status: "running",
  217. nodesSettled: 0,
  218. nodesTotal: 0,
  219. }),
  220. ).toBe("tool-create-1");
  221. expect(resolveWorkflowRunOpenToolCallId("tool-create-1", undefined)).toBe("tool-create-1");
  222. });
  223. });
  224. describe("buildWorkflowRunPendingQuestionsByRunId", () => {
  225. it("键在场 ⟺ run 在投影里,值是停驻 qid 集合(含/空两态)", () => {
  226. const map = buildWorkflowRunPendingQuestionsByRunId([
  227. run({
  228. runId: "dwfrun-waiting",
  229. pendingQuestions: [
  230. { qid: "q_a", question: "问题 A" },
  231. { qid: "q_b", question: "问题 B" },
  232. ],
  233. }),
  234. // pendingQuestions 零条时整个键缺席(schema 惯例)→ 坍缩成空 Set = Answered。
  235. run({ runId: "dwfrun-answered" }),
  236. ]);
  237. expect(map.get("dwfrun-waiting")?.has("q_a")).toBe(true);
  238. expect(map.get("dwfrun-waiting")?.has("q_b")).toBe(true);
  239. expect(map.has("dwfrun-answered")).toBe(true);
  240. expect(map.get("dwfrun-answered")?.size).toBe(0);
  241. // run 不在场 → 键缺席(中性 Question,绝不谎报 Answered)。
  242. expect(map.has("dwfrun-absent")).toBe(false);
  243. });
  244. it("投影缺席 → 空表", () => {
  245. expect(buildWorkflowRunPendingQuestionsByRunId(undefined).size).toBe(0);
  246. });
  247. });
  248. // 发起 toolCallId → 图(docs/dynamic-workflow/presentation.md「The run card」):图是 run 的属性,按发起行找,
  249. // 不问它挂在 CreateWorkflow 工具行上还是直接启动轮的元数据上;空图按无图。
  250. describe("buildWorkflowGraphByToolCallId", () => {
  251. const GRAPH = {
  252. steps: [{ id: "ask#1", kind: "ask", label: "a", line: 1, column: 21, lane: "actor#1" }],
  253. lanes: [{ id: "actor#1", name: "a", line: 1, column: 11 }],
  254. participants: [
  255. { id: "unphased:actor#1", phase: "unphased", lane: "actor#1", steps: ["ask#1"] },
  256. ],
  257. handoffs: [],
  258. };
  259. const display = { kind: "create_workflow", ok: true, errorCount: 0, diagnostics: [] } as const;
  260. const base = { turnId: "turn", createdAt: 1, createdAtSeq: 1 };
  261. it("CreateWorkflow 工具行按自己的 toolCallId 入表;空图与非工作流 display 不入", () => {
  262. const rows = [
  263. {
  264. ...base,
  265. rowId: 1,
  266. kind: "toolCall",
  267. toolCallId: "tool-1",
  268. toolName: "CreateWorkflow",
  269. status: "success",
  270. inputText: "",
  271. display: { ...display, causalityGraph: GRAPH },
  272. },
  273. {
  274. ...base,
  275. rowId: 2,
  276. kind: "toolCall",
  277. toolCallId: "tool-2",
  278. toolName: "CreateWorkflow",
  279. status: "success",
  280. inputText: "",
  281. display: { ...display, causalityGraph: { ...GRAPH, steps: [] } },
  282. },
  283. {
  284. ...base,
  285. rowId: 3,
  286. kind: "toolCall",
  287. toolCallId: "tool-3",
  288. toolName: "ResumeWorkflowRun",
  289. status: "success",
  290. inputText: "",
  291. display: { kind: "resume_workflow_run", runId: "run-1" },
  292. },
  293. ] as unknown as ConversationRow[];
  294. const table = buildWorkflowGraphByToolCallId(rows);
  295. expect([...table.keys()]).toEqual(["tool-1"]);
  296. expect(table.get("tool-1")?.steps).toHaveLength(1);
  297. });
  298. it("直接启动轮:turnHeader 与 userInput 各带同一份元数据,按元数据的 toolCallId 入表一次", () => {
  299. const workflowLaunch = {
  300. runId: "run-l",
  301. toolCallId: "launch-abc",
  302. name: "deep-research",
  303. scope: "project",
  304. path: "/w/.zcode/workflows/deep-research.dwf.ts",
  305. display: { ...display, causalityGraph: GRAPH },
  306. };
  307. const rows = [
  308. {
  309. ...base,
  310. rowId: 1,
  311. kind: "turnHeader",
  312. origin: "workflowLaunch",
  313. executionKind: "controlOnly",
  314. state: "completedSuccess",
  315. startedAt: 1,
  316. workflowLaunch,
  317. },
  318. { ...base, rowId: 2, kind: "userInput", text: "…", origin: "workflowLaunch", workflowLaunch },
  319. { ...base, rowId: 3, kind: "userInput", text: "hi", origin: "realUser" },
  320. ] as unknown as ConversationRow[];
  321. const table = buildWorkflowGraphByToolCallId(rows);
  322. expect([...table.keys()]).toEqual(["launch-abc"]);
  323. expect(buildWorkflowGraphByToolCallId(undefined).size).toBe(0);
  324. });
  325. });