workflowPhaseModel.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. IMPLICIT_PHASE_ID,
  4. participantsOfPhase,
  5. withImplicitPhase,
  6. } from "../src/components/workflow-graph/participant-model.js";
  7. import {
  8. collapsePhaseStatus,
  9. findPhase,
  10. hasPhaseVocabulary,
  11. phaseMembers,
  12. } from "../src/components/workflow-graph/phase-model.js";
  13. import { phaseDisplayName, phaseNameMatches } from "../src/components/workflow-graph/phase-name.js";
  14. import type {
  15. StepRunStatus,
  16. WorkflowCausalityGraphData,
  17. WorkflowStepData,
  18. } from "../src/components/workflow-graph/types.js";
  19. /**
  20. * 阶段层的纯选择器。这一层决定的事都是「画面说不说谎」级别的:无标记脚本合成哪一个模块、
  21. * 一个模块里有哪些卡、模块灯怎么折叠。下钻过滤(`filterGraphToPhase`)与名册(`phaseActors`)
  22. * 随 step 级渲染退役(docs/dynamic-workflow/presentation.md)——不要照旧把它们补回来。
  23. */
  24. function step(
  25. overrides: Partial<WorkflowStepData> & { id: string; lane: string },
  26. ): WorkflowStepData {
  27. return { kind: "ask", label: overrides.id, ...overrides };
  28. }
  29. // 一段两阶段的图:preflight 里一次 world-read 加一次 ask,gate 里两个 actor 各一次 ask。
  30. const GRAPH: WorkflowCausalityGraphData = {
  31. exits: ["phase#2"],
  32. handoffs: [
  33. { from: "phase#1:workspace", to: "phase#1:actor#1" },
  34. { from: "phase#2:actor#1", to: "phase#2:actor#2", types: ["Plan"] },
  35. ],
  36. lanes: [
  37. { id: "actor#1", line: 3, name: "optimizer" },
  38. { id: "actor#2", line: 4, name: "referee" },
  39. { id: "workspace" },
  40. ],
  41. participants: [
  42. { id: "phase#1:workspace", lane: "workspace", phase: "phase#1", steps: ["world-read#1"] },
  43. { id: "phase#1:actor#1", lane: "actor#1", phase: "phase#1", steps: ["ask#1"] },
  44. { id: "phase#2:actor#1", lane: "actor#1", phase: "phase#2", steps: ["ask#2"] },
  45. { id: "phase#2:actor#2", lane: "actor#2", phase: "phase#2", steps: ["ask#3"] },
  46. ],
  47. phaseEdges: [{ from: "phase#1", to: "phase#2" }],
  48. phases: [
  49. { id: "phase#1", line: 2, name: "preflight" },
  50. { id: "phase#2", line: 9, name: "gate" },
  51. ],
  52. sink: ["ask#2", "ask#3"],
  53. steps: [
  54. step({ id: "world-read#1", kind: "world-read", lane: "workspace", phase: "phase#1" }),
  55. step({ id: "ask#1", lane: "actor#1", phase: "phase#1" }),
  56. step({ id: "ask#2", lane: "actor#1", phase: "phase#2" }),
  57. step({ id: "ask#3", lane: "actor#2", phase: "phase#2" }),
  58. ],
  59. };
  60. /** 分析器对无标记脚本的输出:参与者恒带 `unphased`,阶段词汇表整体缺席。 */
  61. const MARKERLESS: WorkflowCausalityGraphData = {
  62. handoffs: [{ from: "unphased:actor#1", to: "unphased:actor#2" }],
  63. lanes: [
  64. { id: "actor#1", name: "planner" },
  65. { id: "actor#2", name: "reviewer" },
  66. ],
  67. participants: [
  68. { id: "unphased:actor#1", lane: "actor#1", phase: "unphased", steps: ["ask#1"] },
  69. { id: "unphased:actor#2", lane: "actor#2", phase: "unphased", steps: ["ask#2"] },
  70. ],
  71. sink: ["ask#2"],
  72. steps: [step({ id: "ask#1", lane: "actor#1" }), step({ id: "ask#2", lane: "actor#2" })],
  73. };
  74. /** 假 formatter:策略本身与文案无关,所以直接回 key,断言读起来也更明确。 */
  75. const formatMessage = (descriptor: { id: string }): string => descriptor.id;
  76. describe("phase vocabulary", () => {
  77. it("is absent when the payload carries no phases at all", () => {
  78. expect(hasPhaseVocabulary(MARKERLESS)).toBe(false);
  79. });
  80. it("is absent when the vocabulary is present but empty", () => {
  81. // 整体降级(阶段数超上限)与「作者一个标记都没写」在 UI 眼里必须是同一件事。
  82. expect(hasPhaseVocabulary({ ...GRAPH, phaseEdges: [], phases: [] })).toBe(false);
  83. });
  84. it("is present as soon as the author said something", () => {
  85. expect(hasPhaseVocabulary(GRAPH)).toBe(true);
  86. });
  87. });
  88. describe("withImplicitPhase", () => {
  89. it("synthesizes a single `workflow` phase for a marker-less graph and rehomes everything into it", () => {
  90. const board = withImplicitPhase(MARKERLESS);
  91. expect(board.phases).toEqual([{ id: IMPLICIT_PHASE_ID }]);
  92. expect(board.phaseEdges).toEqual([]);
  93. // 有返回物 → 控制流在隐式阶段之后正常完成。
  94. expect(board.exits).toEqual([IMPLICIT_PHASE_ID]);
  95. expect(board.participants.map((participant) => participant.phase)).toEqual([
  96. IMPLICIT_PHASE_ID,
  97. IMPLICIT_PHASE_ID,
  98. ]);
  99. // 卡 id 不变:`unphased` 只是分析器的占位前缀,运行状态与检视器仍按原 id 找。
  100. expect(board.participants.map((participant) => participant.id)).toEqual([
  101. "unphased:actor#1",
  102. "unphased:actor#2",
  103. ]);
  104. expect(board.steps.every((member) => member.phase === IMPLICIT_PHASE_ID)).toBe(true);
  105. // 其余载荷原样:交接、车道、返回物。
  106. expect(board.handoffs).toEqual(MARKERLESS.handoffs);
  107. expect(board.lanes).toBe(MARKERLESS.lanes);
  108. expect(board.sink).toEqual(["ask#2"]);
  109. expect(hasPhaseVocabulary(board)).toBe(true);
  110. });
  111. it("has no exit when the script returns nothing", () => {
  112. const { sink: _sink, ...noSink } = MARKERLESS;
  113. expect(withImplicitPhase(noSink).exits).toEqual([]);
  114. expect(withImplicitPhase({ ...MARKERLESS, sink: [] }).exits).toEqual([]);
  115. });
  116. it("returns a graph with vocabulary untouched — by reference, so memo holds", () => {
  117. expect(withImplicitPhase(GRAPH)).toBe(GRAPH);
  118. // 空词汇表也算无:合成隐式阶段,而不是画一张空的阶段图。
  119. const empty = { ...GRAPH, phaseEdges: [], phases: [] };
  120. expect(withImplicitPhase(empty).phases).toEqual([{ id: IMPLICIT_PHASE_ID }]);
  121. });
  122. it("is idempotent", () => {
  123. const once = withImplicitPhase(MARKERLESS);
  124. expect(withImplicitPhase(once)).toBe(once);
  125. });
  126. it("names the implicit phase at render time, never in the payload", () => {
  127. expect(phaseDisplayName({ id: IMPLICIT_PHASE_ID }, formatMessage)).toBe(
  128. "chat.toolCall.workflow.graph.phase.workflow",
  129. );
  130. });
  131. });
  132. describe("participantsOfPhase", () => {
  133. it("buckets by phase, keeping the payload's hand-off order", () => {
  134. expect(participantsOfPhase(GRAPH, "phase#1").map((participant) => participant.id)).toEqual([
  135. "phase#1:workspace",
  136. "phase#1:actor#1",
  137. ]);
  138. expect(participantsOfPhase(GRAPH, "phase#2").map((participant) => participant.id)).toEqual([
  139. "phase#2:actor#1",
  140. "phase#2:actor#2",
  141. ]);
  142. });
  143. it("is empty for a phase with no cards, and for an unknown phase", () => {
  144. expect(participantsOfPhase(GRAPH, "phase#404")).toEqual([]);
  145. // 分析器给的 `unphased` 在无标记图上不是阶段:先合成隐式阶段才有卡可分。
  146. expect(participantsOfPhase(MARKERLESS, IMPLICIT_PHASE_ID)).toEqual([]);
  147. expect(participantsOfPhase(withImplicitPhase(MARKERLESS), IMPLICIT_PHASE_ID)).toHaveLength(2);
  148. });
  149. });
  150. describe("phase membership and naming", () => {
  151. it("buckets steps by their phase, keeping the phase table's order", () => {
  152. const members = phaseMembers(GRAPH);
  153. expect([...members.keys()]).toEqual(["phase#1", "phase#2"]);
  154. expect(members.get("phase#1")?.map((member) => member.id)).toEqual(["world-read#1", "ask#1"]);
  155. expect(members.get("phase#2")?.map((member) => member.id)).toEqual(["ask#2", "ask#3"]);
  156. });
  157. it("finds a phase by id for the inspector's naming material", () => {
  158. expect(findPhase(GRAPH, "phase#2")?.name).toBe("gate");
  159. expect(findPhase(GRAPH, "phase#404")).toBeUndefined();
  160. expect(findPhase(MARKERLESS, IMPLICIT_PHASE_ID)).toBeUndefined();
  161. });
  162. it("shows the author's word", () => {
  163. expect(phaseDisplayName(GRAPH.phases![1]!, formatMessage)).toBe("gate");
  164. expect(phaseDisplayName(GRAPH.phases![0]!, formatMessage)).toBe("preflight");
  165. });
  166. it("localizes the synthetic `unphased` phase, which has no name of its own", () => {
  167. expect(phaseDisplayName({ id: "unphased" }, formatMessage)).toBe(
  168. "chat.toolCall.workflow.graph.phase.unphased",
  169. );
  170. });
  171. });
  172. describe("phase status re-collapse", () => {
  173. const members = [step({ id: "a", lane: "actor#1" }), step({ id: "b", lane: "actor#1" })];
  174. const collapse = (a: StepRunStatus, b: StepRunStatus) => collapsePhaseStatus(members, { a, b });
  175. it("reports running first — the reader's first question is whether it still moves", () => {
  176. expect(collapse("failed", "running")).toBe("running");
  177. expect(collapse("done", "running")).toBe("running");
  178. });
  179. it("reports failed once nothing runs any more", () => {
  180. expect(collapse("done", "failed")).toBe("failed");
  181. });
  182. it("reports done only when every member settled cleanly; settled + queued is running", () => {
  183. expect(collapse("done", "done")).toBe("done");
  184. expect(collapse("done", "pending")).toBe("running");
  185. });
  186. it("ignores members without an entry: a branch the run did not take is absent, not pending", () => {
  187. expect(collapsePhaseStatus(members, { a: "done" })).toBe("done");
  188. });
  189. it("has no dot at all in the static view", () => {
  190. expect(collapsePhaseStatus(members, undefined)).toBeUndefined();
  191. // 叠加在场但这个阶段一个成员都没被观察到:同样不点亮,而不是谎报 pending。
  192. expect(collapsePhaseStatus(members, {})).toBeUndefined();
  193. });
  194. });
  195. /**
  196. * display 名 ↔ 运行时名的唯一关联规则(docs/dynamic-workflow/presentation.md
  197. * 2026-09-09「名字关联」):进入记录、`currentPhase`、实例的出生戳三处共用它。
  198. */
  199. describe("phaseNameMatches", () => {
  200. const long = "x".repeat(128);
  201. it("matches the same word", () => {
  202. expect(phaseNameMatches("attempt", "attempt")).toBe(true);
  203. expect(phaseNameMatches("attempt", "gate")).toBe(false);
  204. });
  205. it("falls back to a prefix only when the display name really was truncated", () => {
  206. // display 名恰好顶到上界(128)→ 线上的完整名以它开头就算同一个阶段。
  207. expect(phaseNameMatches(long, `${long}yyy`)).toBe(true);
  208. // 没顶到上界的短名不做前缀匹配:「计划」不认「计划修复」。
  209. expect(phaseNameMatches("计划", "计划修复")).toBe(false);
  210. // 方向不能反:线上名是被截断的那一侧时不匹配。
  211. expect(phaseNameMatches(`${long}yyy`, long)).toBe(false);
  212. });
  213. it("says no when either side has no name — matching needs two names", () => {
  214. // 「无名」不是一个可匹配的名字;无名 display 阶段与无戳实例的配对是 `phasesOf` 的事。
  215. expect(phaseNameMatches(undefined, "attempt")).toBe(false);
  216. expect(phaseNameMatches("attempt", undefined)).toBe(false);
  217. expect(phaseNameMatches(undefined, undefined)).toBe(false);
  218. });
  219. });