workflowRunCardJoinFillGraph.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // 补全之后卡与侧板取图(docs/dynamic-workflow/presentation.md「Holes on the timeline」的「The model」):
  2. // run 卡取「run id 是自己的、最新的那份 display」——补全行成功且编过的图;之前是发起行的。
  3. import { describe, expect, it } from "vitest";
  4. import type { ConversationRow, WorkflowRunState } from "@zcode/shared/zcode-protocol-v4";
  5. import {
  6. buildWorkflowFillGraphByRunId,
  7. buildWorkflowGraphByToolCallId,
  8. resolveWorkflowRunGraphForRun,
  9. } from "@/v4/workflowRunCardJoin.js";
  10. import { resolveWorkflowTurnDigests } from "@/v4/workflowTurnDigests.js";
  11. let seq = 0;
  12. const GRAPH_A = {
  13. steps: [{ id: "ask#1", kind: "ask", label: "a", lane: "actor#1", phase: "phase#1" }],
  14. lanes: [{ id: "actor#1" }],
  15. participants: [{ id: "p", phase: "phase#1", lane: "actor#1", steps: ["ask#1"] }],
  16. handoffs: [],
  17. phases: [
  18. { id: "phase#1", name: "探索" },
  19. { id: "hole#1", name: "决定分组" },
  20. ],
  21. holes: [{ siteId: "hole#1", name: "决定分组", type: "Plan" }],
  22. };
  23. const GRAPH_B = {
  24. ...GRAPH_A,
  25. steps: [
  26. ...GRAPH_A.steps,
  27. {
  28. id: "hole#1/ask#1",
  29. kind: "ask",
  30. label: "b",
  31. lane: "hole#1/actor#1",
  32. phase: "hole#1/phase#1",
  33. fill: "hole#1",
  34. },
  35. ],
  36. phases: [
  37. { id: "phase#1", name: "探索" },
  38. { id: "hole#1/phase#1", name: "冒烟测试", fill: "hole#1" },
  39. ],
  40. holes: [],
  41. };
  42. function row(options: {
  43. toolCallId: string;
  44. toolName: string;
  45. input: Record<string, unknown>;
  46. status?: string;
  47. graph?: unknown;
  48. ok?: boolean;
  49. /** display 的 `fill` 块:行的名字只从这里来(transcript 不存 resolveInput 回填的 `hole`)。 */
  50. fill?: { siteId: string; name: string };
  51. }): ConversationRow {
  52. seq += 1;
  53. return {
  54. rowId: seq,
  55. turnId: "turn-1",
  56. createdAt: 1_700_000_000_000 + seq,
  57. createdAtSeq: seq,
  58. kind: "toolCall",
  59. toolCallId: options.toolCallId,
  60. toolName: options.toolName,
  61. status: options.status ?? "success",
  62. inputText: "{}",
  63. input: options.input,
  64. ...(options.graph === undefined
  65. ? {}
  66. : {
  67. display: {
  68. kind: "create_workflow",
  69. ok: options.ok ?? true,
  70. errorCount: 0,
  71. diagnostics: [],
  72. causalityGraph: options.graph,
  73. ...(options.fill === undefined ? {} : { fill: options.fill }),
  74. },
  75. }),
  76. } as unknown as ConversationRow;
  77. }
  78. const LAUNCH = row({
  79. toolCallId: "tc-create",
  80. toolName: "CreateWorkflow",
  81. input: { script: "…" },
  82. graph: GRAPH_A,
  83. });
  84. // 入参是模型写的那份(没有 `hole` 块——2026-09-28 首次接龙实测证实 transcript 不存回填的入参)。
  85. const FILL = row({
  86. toolCallId: "tc-fill",
  87. toolName: "FillWorkflowHole",
  88. input: { run_id: "run-1", hole_id: "hole#1", script: "…" },
  89. graph: GRAPH_B,
  90. fill: { siteId: "hole#1", name: "决定分组" },
  91. });
  92. function run(overrides: Partial<WorkflowRunState> = {}): WorkflowRunState {
  93. return {
  94. runId: "run-1",
  95. toolCallId: "tc-create",
  96. status: "running",
  97. usage: { spentTokens: 0, nodesUsed: 0 },
  98. actors: [],
  99. nodes: [],
  100. lastEventSequence: 0,
  101. ...overrides,
  102. };
  103. }
  104. describe("buildWorkflowFillGraphByRunId", () => {
  105. it("成功且编过的 FillWorkflowHole 行按 run_id 入表,名字来自 display 的 fill 块而不是入参", () => {
  106. const fills = buildWorkflowFillGraphByRunId([LAUNCH, FILL]);
  107. expect(fills.get("run-1")?.graph).toBe(GRAPH_B);
  108. expect(fills.get("run-1")?.holeLabels.get("hole#1")).toEqual({ name: "决定分组" });
  109. });
  110. it("编不过 / 失败 / 没有图的补全行不入表;后来的覆盖先来的、标签累积", () => {
  111. const rejected = row({
  112. toolCallId: "tc-x",
  113. toolName: "FillWorkflowHole",
  114. input: { run_id: "run-1", hole_id: "hole#1" },
  115. graph: GRAPH_B,
  116. ok: false,
  117. });
  118. const failed = row({
  119. toolCallId: "tc-y",
  120. toolName: "FillWorkflowHole",
  121. input: { run_id: "run-1", hole_id: "hole#1" },
  122. graph: GRAPH_B,
  123. status: "error",
  124. });
  125. expect(buildWorkflowFillGraphByRunId([rejected, failed]).size).toBe(0);
  126. const second = row({
  127. toolCallId: "tc-fill-2",
  128. toolName: "FillWorkflowHole",
  129. input: { run_id: "run-1", hole_id: "hole#2" },
  130. graph: GRAPH_A,
  131. fill: { siteId: "hole#2", name: "评判" },
  132. });
  133. const fills = buildWorkflowFillGraphByRunId([FILL, second]);
  134. expect(fills.get("run-1")?.graph).toBe(GRAPH_A);
  135. expect([...fills.get("run-1")!.holeLabels.keys()]).toEqual(["hole#1", "hole#2"]);
  136. });
  137. });
  138. describe("resolveWorkflowRunGraphForRun / 轮尾摘要", () => {
  139. it("补全过就取补全行的图,否则退回发起行的图", () => {
  140. const graphs = buildWorkflowGraphByToolCallId([LAUNCH, FILL]);
  141. const fills = buildWorkflowFillGraphByRunId([LAUNCH, FILL]);
  142. expect(resolveWorkflowRunGraphForRun(graphs, fills, "tc-create", "run-1", [run()])).toBe(
  143. GRAPH_B,
  144. );
  145. expect(resolveWorkflowRunGraphForRun(graphs, fills, "tc-create", "run-2", [run()])).toBe(
  146. GRAPH_A,
  147. );
  148. expect(resolveWorkflowRunGraphForRun(graphs, undefined, "tc-create", "run-1", [run()])).toBe(
  149. GRAPH_A,
  150. );
  151. });
  152. it("轮尾摘要的卡按 runId 换上补全后的图,并带上留白的名字与类型", () => {
  153. const summary = {
  154. runId: "run-1",
  155. status: "running" as const,
  156. nodesSettled: 0,
  157. nodesTotal: 0,
  158. agents: 0,
  159. run: run(),
  160. };
  161. const digests = resolveWorkflowTurnDigests(
  162. { assistantWorkRows: [LAUNCH as never] },
  163. {
  164. byToolCallId: new Map([["tc-create", summary]]),
  165. byRunId: new Map([["run-1", summary]]),
  166. graphByToolCallId: buildWorkflowGraphByToolCallId([LAUNCH, FILL]),
  167. fillGraphByRunId: buildWorkflowFillGraphByRunId([LAUNCH, FILL]),
  168. },
  169. );
  170. expect(digests).toHaveLength(1);
  171. expect(digests[0]!.graph).toBe(GRAPH_B);
  172. expect(digests[0]!.holeLabels?.get("hole#1")?.name).toBe("决定分组");
  173. });
  174. });