workflowRunOverlay.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // dwf 实时叠加视图的纯选择器:workflowRuns 的一条 run + 静态因果图 → statuses / animatedEdges,
  2. // 以及车道 → actor 实例的解析(phase 5 的 transcript 下钻消费它)。
  3. // 见 docs/dynamic-workflow/presentation.md「Node status」「状态聚合」与 apps/zcode-cli/packages/dynamic-workflow/docs/analysis.md 的
  4. // 「Joining runtime instances」(live view v1 不展开,按 site id 关联并在实例上聚合)以及
  5. // 「Joining runtime instances」(带 source 的拷贝再按 actor 车道收窄)。
  6. import { describe, expect, it } from "vitest";
  7. import type { WorkflowRunState } from "@zcode/shared/zcode-protocol-v4";
  8. import {
  9. workflowRunActorsForLane,
  10. workflowRunOverlay,
  11. } from "../src/components/workflow-graph/run-status.js";
  12. import type { WorkflowCausalityGraphData } from "../src/components/workflow-graph/types.js";
  13. const graph: WorkflowCausalityGraphData = {
  14. steps: [
  15. { id: "ask#1", kind: "ask", label: "plan", lane: "actor#1" },
  16. { id: "ask#2", kind: "ask", label: "write", lane: "actor#2" },
  17. { id: "world-read#1", kind: "world-read", label: "glob", lane: "workspace" },
  18. ],
  19. lanes: [{ id: "actor#1", name: "planner" }, { id: "actor#2" }, { id: "workspace" }],
  20. edges: [{ from: "ask#1", to: "ask#2" }],
  21. };
  22. function runWith(
  23. nodes: WorkflowRunState["nodes"],
  24. overrides: Partial<WorkflowRunState> = {},
  25. ): WorkflowRunState {
  26. return {
  27. runId: "dwfrun-1",
  28. status: "running",
  29. usage: { spentTokens: 0, nodesUsed: nodes.length },
  30. actors: [],
  31. nodes,
  32. lastEventSequence: nodes.length,
  33. ...overrides,
  34. };
  35. }
  36. describe("workflowRunOverlay 相位映射", () => {
  37. it("executing / repairing / nudged 全部映射成 running", () => {
  38. for (const phase of ["executing", "repairing", "nudged"] as const) {
  39. const { statuses } = workflowRunOverlay(
  40. runWith([{ siteId: "ask#1", ordinal: 1, phase }]),
  41. graph,
  42. );
  43. expect(statuses["ask#1"]).toBe("running");
  44. }
  45. });
  46. it("queued / dispatched / waiting 是 pending:还没有请求在 provider 那里跑", () => {
  47. for (const phase of ["queued", "dispatched", "waiting"] as const) {
  48. const { statuses } = workflowRunOverlay(
  49. runWith([{ siteId: "ask#1", ordinal: 1, phase }]),
  50. graph,
  51. );
  52. expect(statuses["ask#1"]).toBe("pending");
  53. }
  54. });
  55. it("settled ok 是 done,cached 命中同样是 done", () => {
  56. expect(
  57. workflowRunOverlay(
  58. runWith([{ siteId: "ask#1", ordinal: 1, phase: "settled", outcome: "ok" }]),
  59. graph,
  60. ).statuses["ask#1"],
  61. ).toBe("done");
  62. expect(
  63. workflowRunOverlay(
  64. runWith([{ siteId: "ask#1", ordinal: 1, phase: "settled", outcome: "ok", cached: true }]),
  65. graph,
  66. ).statuses["ask#1"],
  67. ).toBe("done");
  68. });
  69. it("settled failed 与 settled cancelled 都是 failed", () => {
  70. for (const outcome of ["failed", "cancelled"] as const) {
  71. const { statuses } = workflowRunOverlay(
  72. runWith([{ siteId: "ask#1", ordinal: 1, phase: "settled", outcome }]),
  73. graph,
  74. );
  75. expect(statuses["ask#1"]).toBe("failed");
  76. }
  77. });
  78. it("没有实例的 step 没有条目:缺席不是状态,不写成 pending", () => {
  79. const { statuses } = workflowRunOverlay(runWith([]), graph);
  80. expect(statuses).toEqual({});
  81. // 只观察到一个站点时表里只有它:控制流没走的站点不会以 pending 出现。
  82. expect(
  83. workflowRunOverlay(
  84. runWith([{ siteId: "ask#1", ordinal: 1, phase: "settled", outcome: "ok" }]),
  85. graph,
  86. ).statuses,
  87. ).toEqual({ "ask#1": "done" });
  88. });
  89. it("无 run(静态渲染)时不产出任何状态", () => {
  90. expect(workflowRunOverlay(undefined, graph)).toEqual({ statuses: {}, animatedEdges: false });
  91. });
  92. });
  93. describe("workflowRunOverlay 实例聚合(叠加视图必须坍缩)", () => {
  94. it("running 优先于 failed:读者第一个问题是「还在动吗」", () => {
  95. const { statuses } = workflowRunOverlay(
  96. runWith([
  97. { siteId: "ask#2", ordinal: 3, phase: "settled", outcome: "failed" },
  98. { siteId: "ask#2", ordinal: 4, phase: "executing" },
  99. ]),
  100. graph,
  101. );
  102. expect(statuses["ask#2"]).toBe("running");
  103. });
  104. it("无 running 时任一 failed 即 failed", () => {
  105. const { statuses } = workflowRunOverlay(
  106. runWith([
  107. { siteId: "ask#2", ordinal: 1, phase: "settled", outcome: "ok" },
  108. { siteId: "ask#2", ordinal: 2, phase: "settled", outcome: "failed" },
  109. ]),
  110. graph,
  111. );
  112. expect(statuses["ask#2"]).toBe("failed");
  113. });
  114. it("全部结算且全部 ok 才是 done", () => {
  115. const { statuses } = workflowRunOverlay(
  116. runWith([
  117. { siteId: "ask#2", ordinal: 1, phase: "settled", outcome: "ok" },
  118. { siteId: "ask#2", ordinal: 2, phase: "settled", outcome: "ok", cached: true },
  119. ]),
  120. graph,
  121. );
  122. expect(statuses["ask#2"]).toBe("done");
  123. });
  124. it("既有已结算又有排队的实例是 running:开始了、没结束,既不谎报 done 也不谎报没开始", () => {
  125. const { statuses } = workflowRunOverlay(
  126. runWith([
  127. { siteId: "ask#2", ordinal: 1, phase: "settled", outcome: "ok" },
  128. { siteId: "ask#2", ordinal: 2, phase: "queued" },
  129. ]),
  130. graph,
  131. );
  132. expect(statuses["ask#2"]).toBe("running");
  133. // 已失败 + 排队同样是 running(running 优先于 failed 的旧裁决)。
  134. expect(
  135. workflowRunOverlay(
  136. runWith([
  137. { siteId: "ask#2", ordinal: 1, phase: "settled", outcome: "failed" },
  138. { siteId: "ask#2", ordinal: 2, phase: "queued" },
  139. ]),
  140. graph,
  141. ).statuses["ask#2"],
  142. ).toBe("running");
  143. });
  144. it("全部在排队时才是 pending", () => {
  145. const { statuses } = workflowRunOverlay(
  146. runWith([
  147. { siteId: "ask#2", ordinal: 1, phase: "queued" },
  148. { siteId: "ask#2", ordinal: 2, phase: "dispatched" },
  149. ]),
  150. graph,
  151. );
  152. expect(statuses["ask#2"]).toBe("pending");
  153. });
  154. it("不认识的 site id 被忽略(运行期图的节点集合绝不变化)", () => {
  155. const { statuses } = workflowRunOverlay(
  156. runWith([{ siteId: "ask#99", ordinal: 1, phase: "executing" }]),
  157. graph,
  158. );
  159. expect(statuses).toEqual({});
  160. });
  161. });
  162. describe("workflowRunOverlay animatedEdges", () => {
  163. it("有 step 在跑时开启边动画", () => {
  164. expect(
  165. workflowRunOverlay(runWith([{ siteId: "ask#1", ordinal: 1, phase: "executing" }]), graph)
  166. .animatedEdges,
  167. ).toBe(true);
  168. });
  169. it("终态 run 关闭边动画", () => {
  170. expect(
  171. workflowRunOverlay(
  172. runWith([{ siteId: "ask#1", ordinal: 1, phase: "settled", outcome: "ok" }], {
  173. status: "completed",
  174. }),
  175. graph,
  176. ).animatedEdges,
  177. ).toBe(false);
  178. });
  179. });
  180. // may-set 车道展开后的图:一个站点两张卡片,各归其候选车道,`source` 是它们共同的站点 id。
  181. // 见 apps/zcode-cli/packages/dynamic-workflow/docs/analysis.md「Joining runtime instances」。
  182. const expandedGraph: WorkflowCausalityGraphData = {
  183. steps: [
  184. {
  185. id: "ask#1~actor#1",
  186. source: "ask#1",
  187. kind: "ask",
  188. label: "ask",
  189. lane: "actor#1",
  190. lanes: ["actor#1", "actor#2"],
  191. repeat: "serial",
  192. },
  193. {
  194. id: "ask#1~actor#2",
  195. source: "ask#1",
  196. kind: "ask",
  197. label: "ask",
  198. lane: "actor#2",
  199. lanes: ["actor#1", "actor#2"],
  200. repeat: "serial",
  201. },
  202. { id: "ask#2", kind: "ask", label: "judge", lane: "actor#3" },
  203. ],
  204. lanes: [{ id: "actor#1", name: "black" }, { id: "actor#2", name: "white" }, { id: "actor#3" }],
  205. edges: [{ from: "ask#1~actor#1", to: "ask#1~actor#2", back: true }],
  206. };
  207. describe("workflowRunOverlay 展开拷贝的车道路由", () => {
  208. it("某一候选车道上的实例只点亮该候选的拷贝", () => {
  209. const { statuses } = workflowRunOverlay(
  210. runWith([{ siteId: "ask#1", ordinal: 1, phase: "executing", actorSiteId: "actor#1" }]),
  211. expandedGraph,
  212. );
  213. expect(statuses["ask#1~actor#1"]).toBe("running");
  214. expect(statuses["ask#1~actor#2"]).toBeUndefined();
  215. });
  216. it("同一站点的两条车道各自聚合,互不串味", () => {
  217. const { statuses } = workflowRunOverlay(
  218. runWith([
  219. {
  220. siteId: "ask#1",
  221. ordinal: 1,
  222. phase: "settled",
  223. outcome: "failed",
  224. actorSiteId: "actor#1",
  225. },
  226. { siteId: "ask#1", ordinal: 2, phase: "executing", actorSiteId: "actor#2" },
  227. ]),
  228. expandedGraph,
  229. );
  230. expect(statuses["ask#1~actor#1"]).toBe("failed");
  231. expect(statuses["ask#1~actor#2"]).toBe("running");
  232. });
  233. it("actorSiteId 缺席的实例聚合进该 source 的全部拷贝(退化成旧单卡的过度点亮,不是死图)", () => {
  234. const { statuses } = workflowRunOverlay(
  235. runWith([{ siteId: "ask#1", ordinal: 1, phase: "executing" }]),
  236. expandedGraph,
  237. );
  238. expect(statuses["ask#1~actor#1"]).toBe("running");
  239. expect(statuses["ask#1~actor#2"]).toBe("running");
  240. });
  241. it("落在候选之外车道的实例不点亮任何拷贝(拷贝的车道就是它的全部主张)", () => {
  242. const { statuses } = workflowRunOverlay(
  243. runWith([{ siteId: "ask#1", ordinal: 1, phase: "executing", actorSiteId: "actor#9" }]),
  244. expandedGraph,
  245. );
  246. expect(statuses["ask#1~actor#1"]).toBeUndefined();
  247. expect(statuses["ask#1~actor#2"]).toBeUndefined();
  248. });
  249. it("孤儿拷贝:运行时从未选中的候选在 run 终局后仍无条目(画面上空心,模型上是缺席)", () => {
  250. const { animatedEdges, statuses } = workflowRunOverlay(
  251. runWith(
  252. [{ siteId: "ask#1", ordinal: 1, phase: "settled", outcome: "ok", actorSiteId: "actor#1" }],
  253. {
  254. status: "completed",
  255. },
  256. ),
  257. expandedGraph,
  258. );
  259. expect(statuses).toEqual({ "ask#1~actor#1": "done" });
  260. expect(animatedEdges).toBe(false);
  261. });
  262. it("拷贝 id 不是站点 id:按拷贝 id 关联的实例一个也收不到", () => {
  263. const { statuses } = workflowRunOverlay(
  264. runWith([
  265. { siteId: "ask#1~actor#1", ordinal: 1, phase: "executing", actorSiteId: "actor#1" },
  266. ]),
  267. expandedGraph,
  268. );
  269. expect(statuses["ask#1~actor#1"]).toBeUndefined();
  270. });
  271. });
  272. // ——— 追记 2026-09-10「叠加视图也按出生阶段收窄」:阶段拷贝按实例的 phaseName 收 ———
  273. describe("workflowRunOverlay 阶段拷贝的阶段路由", () => {
  274. // 一个 ask 站点被 5 个阶段认领:5 份拷贝、同一个 source、同一条车道。
  275. const NAMES = ["调研", "起草", "校对", "复核", "收尾"];
  276. const phasedGraph: WorkflowCausalityGraphData = {
  277. steps: NAMES.map((_, i) => ({
  278. id: `ask#1~phase#${i + 1}`,
  279. kind: "ask" as const,
  280. label: "review",
  281. lane: "actor#1",
  282. phase: `phase#${i + 1}`,
  283. source: "ask#1",
  284. })),
  285. lanes: [{ id: "actor#1", name: "worker" }],
  286. edges: [],
  287. phases: NAMES.map((name, i) => ({ id: `phase#${i + 1}`, name })),
  288. };
  289. const copies = (statuses: Record<string, string | undefined>) =>
  290. NAMES.map((_, i) => statuses[`ask#1~phase#${i + 1}`]);
  291. it("带戳的实例只点亮出生阶段那份拷贝:第 1 阶段在跑、第 2 阶段已结算,其余无条目", () => {
  292. const { statuses } = workflowRunOverlay(
  293. runWith([
  294. {
  295. siteId: "ask#1",
  296. ordinal: 1,
  297. phase: "executing",
  298. actorSiteId: "actor#1",
  299. phaseName: "调研",
  300. },
  301. {
  302. siteId: "ask#1",
  303. ordinal: 2,
  304. phase: "executing",
  305. actorSiteId: "actor#1",
  306. phaseName: "调研",
  307. },
  308. {
  309. siteId: "ask#1",
  310. ordinal: 21,
  311. phase: "settled",
  312. outcome: "ok",
  313. actorSiteId: "actor#1",
  314. phaseName: "起草",
  315. },
  316. ]),
  317. phasedGraph,
  318. );
  319. expect(copies(statuses)).toEqual(["running", "done", undefined, undefined, undefined]);
  320. });
  321. it("无戳的 run 点亮全部拷贝:旧 CLI / 旧 run 的过度点亮逐字节保持", () => {
  322. const { statuses } = workflowRunOverlay(
  323. runWith([{ siteId: "ask#1", ordinal: 1, phase: "executing", actorSiteId: "actor#1" }]),
  324. phasedGraph,
  325. );
  326. expect(copies(statuses)).toEqual(["running", "running", "running", "running", "running"]);
  327. });
  328. it("戳匹配不到任何 display 阶段 → 全部拷贝:宁可多亮也不把一个在跑的实例藏起来", () => {
  329. const { statuses } = workflowRunOverlay(
  330. runWith([
  331. {
  332. siteId: "ask#1",
  333. ordinal: 1,
  334. phase: "executing",
  335. actorSiteId: "actor#1",
  336. phaseName: "别处",
  337. },
  338. ]),
  339. phasedGraph,
  340. );
  341. expect(copies(statuses)).toEqual(["running", "running", "running", "running", "running"]);
  342. });
  343. it("run 有词汇而实例无戳 → 只落无名阶段;这张图没有无名阶段,于是退回全部", () => {
  344. const { statuses } = workflowRunOverlay(
  345. runWith([
  346. {
  347. siteId: "ask#1",
  348. ordinal: 1,
  349. phase: "executing",
  350. actorSiteId: "actor#1",
  351. phaseName: "调研",
  352. },
  353. {
  354. siteId: "ask#1",
  355. ordinal: 2,
  356. phase: "settled",
  357. outcome: "failed",
  358. actorSiteId: "actor#1",
  359. },
  360. ]),
  361. phasedGraph,
  362. );
  363. // 无戳的 failed 落到全部五份;第 1 份还叠着一个 running,running 优先。
  364. expect(copies(statuses)).toEqual(["running", "failed", "failed", "failed", "failed"]);
  365. });
  366. });
  367. describe("workflowRunOverlay 不带 source 的 step 关联行为不变", () => {
  368. it("不做车道收窄:>4 候选回退的单卡在别的车道上跑也照常点亮", () => {
  369. // 回退单卡(`lanes` 超过展开上限)画在 lanes[0],实例却可能落在任一候选车道上——
  370. // 对不带 source 的 step 收窄车道等于把这类 step 的状态永久熄灭。
  371. const { statuses } = workflowRunOverlay(
  372. runWith([{ siteId: "ask#2", ordinal: 1, phase: "executing", actorSiteId: "actor#7" }]),
  373. expandedGraph,
  374. );
  375. expect(statuses["ask#2"]).toBe("running");
  376. });
  377. it("全无 source 的图上,带 actorSiteId 的实例与旧行为逐字节一致", () => {
  378. const nodes: WorkflowRunState["nodes"] = [
  379. { siteId: "ask#1", ordinal: 1, phase: "executing", actorSiteId: "actor#1" },
  380. { siteId: "ask#2", ordinal: 1, phase: "settled", outcome: "failed", actorSiteId: "actor#2" },
  381. { siteId: "world-read#1", ordinal: 1, phase: "settled", outcome: "ok" },
  382. ];
  383. expect(workflowRunOverlay(runWith(nodes), graph)).toEqual(
  384. workflowRunOverlay(
  385. runWith(nodes.map(({ actorSiteId: _actorSiteId, ...node }) => node)),
  386. graph,
  387. ),
  388. );
  389. });
  390. });
  391. describe("workflowRunActorsForLane", () => {
  392. const actors: WorkflowRunState["actors"] = [
  393. { siteId: "actor#2", ordinal: 2, status: "running", sessionId: "sess-b" },
  394. { siteId: "actor#2", ordinal: 1, status: "waiting", sessionId: "sess-a" },
  395. { siteId: "actor#1", ordinal: 1, status: "running", name: "planner", sessionId: "sess-p" },
  396. ];
  397. const run = runWith([], { actors });
  398. it("按车道 site id 过滤并按 ordinal 升序(fan-out 车道族的选择器顺序稳定)", () => {
  399. expect(workflowRunActorsForLane(run, "actor#2").map((actor) => actor.ordinal)).toEqual([1, 2]);
  400. });
  401. it("单实例车道返回一个 actor,其会话 id 即 transcript 下钻的入口", () => {
  402. const resolved = workflowRunActorsForLane(run, "actor#1");
  403. expect(resolved).toHaveLength(1);
  404. expect(resolved[0]?.sessionId).toBe("sess-p");
  405. });
  406. it("workspace / unknown 合成车道上没有会话,返回空", () => {
  407. expect(workflowRunActorsForLane(run, "workspace")).toEqual([]);
  408. expect(workflowRunActorsForLane(run, "unknown")).toEqual([]);
  409. expect(workflowRunActorsForLane(undefined, "actor#1")).toEqual([]);
  410. });
  411. });