workflowRunDirectoryModel.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // run 目录的纯模型(docs/dynamic-workflow/presentation.md「Other places a run appears」
  2. // 与「run 目录的单一信源:journal」两行)。
  3. //
  4. // 这个模块存在的全部理由是**一个谓词只有一处实现**:任务岛页脚行上的计数与目录页里的行必须
  5. // 出自同一次过滤与同一套分桶,否则「岛说 5 条、点开只有 4 行」这种不一致随时会长出来。
  6. // 因此下面每个用例都同时断言列表与计数。
  7. import { describe, expect, it } from "vitest";
  8. import type {
  9. V4ConversationWorkflowRunSummary,
  10. WorkflowRunState,
  11. } from "@zcode/shared/zcode-protocol-v4";
  12. import {
  13. WORKFLOW_RUN_DIRECTORY_LIMIT,
  14. buildWorkflowRunDirectory,
  15. countEndedWorkflowRuns,
  16. workflowRunDirectoryRefreshKey,
  17. } from "@/v4/workflowRunDirectoryModel.js";
  18. function summary(
  19. overrides: Partial<V4ConversationWorkflowRunSummary> &
  20. Pick<V4ConversationWorkflowRunSummary, "runId">,
  21. ): V4ConversationWorkflowRunSummary {
  22. return {
  23. toolCallId: `call-${overrides.runId}`,
  24. status: "completed",
  25. resumable: false,
  26. ...overrides,
  27. };
  28. }
  29. describe("buildWorkflowRunDirectory", () => {
  30. it("按五值状态分桶:pending/running 归运行中,completed/errored/stopped 归已结束", () => {
  31. const directory = buildWorkflowRunDirectory([
  32. summary({ runId: "r-pending", status: "pending" }),
  33. summary({ runId: "r-running", status: "running" }),
  34. summary({ runId: "r-completed", status: "completed" }),
  35. summary({ runId: "r-failed", status: "errored" }),
  36. summary({ runId: "r-cancelled", status: "stopped" }),
  37. ]);
  38. expect(directory.running.map((row) => row.runId)).toEqual(["r-pending", "r-running"]);
  39. expect(directory.ended.map((row) => row.runId)).toEqual([
  40. "r-completed",
  41. "r-failed",
  42. "r-cancelled",
  43. ]);
  44. });
  45. it("缺 toolCallId 的老 run 从列表与计数一并剔除(过滤只有一处,不可能只剔一半)", () => {
  46. const summaries = [
  47. summary({ runId: "r-new" }),
  48. // `tool_call_id` 落库之前的 run:没有可开的详情页锚点。
  49. { runId: "r-old", status: "completed", resumable: false } as V4ConversationWorkflowRunSummary,
  50. {
  51. runId: "r-old-running",
  52. status: "running",
  53. resumable: false,
  54. } as V4ConversationWorkflowRunSummary,
  55. ];
  56. const directory = buildWorkflowRunDirectory(summaries);
  57. expect(directory.ended.map((row) => row.runId)).toEqual(["r-new"]);
  58. expect(directory.running).toEqual([]);
  59. expect(countEndedWorkflowRuns(summaries)).toBe(1);
  60. });
  61. it("计数恒等于已结束列表长度——这就是「岛上的 N 与页面上的行一致」那条不变量", () => {
  62. const summaries = [
  63. summary({ runId: "r-1", status: "errored" }),
  64. summary({ runId: "r-2", status: "running" }),
  65. { runId: "r-3", status: "stopped", resumable: true } as V4ConversationWorkflowRunSummary,
  66. summary({ runId: "r-4", status: "stopped" }),
  67. summary({ runId: "r-5", status: "pending" }),
  68. ];
  69. expect(countEndedWorkflowRuns(summaries)).toBe(
  70. buildWorkflowRunDirectory(summaries).ended.length,
  71. );
  72. expect(countEndedWorkflowRuns(summaries)).toBe(2);
  73. });
  74. it("不重排:顺序就是查询给的顺序(最近更新在前,排序在 CLI 存储层)", () => {
  75. const directory = buildWorkflowRunDirectory([
  76. summary({ runId: "r-newest", updatedAt: 300 }),
  77. summary({ runId: "r-middle", updatedAt: 200 }),
  78. summary({ runId: "r-oldest", updatedAt: 100 }),
  79. ]);
  80. expect(directory.ended.map((row) => row.runId)).toEqual(["r-newest", "r-middle", "r-oldest"]);
  81. });
  82. it("原样带上行要渲染的字段:label / updatedAt / stopReason / failureCode / resumable", () => {
  83. const directory = buildWorkflowRunDirectory([
  84. summary({
  85. runId: "r-1",
  86. label: "review-changes",
  87. status: "stopped",
  88. stopReason: "interrupted",
  89. failureCode: "Interrupted",
  90. failureMessage: "host restarted",
  91. updatedAt: 1_700_000_000_000,
  92. resumable: true,
  93. } as never),
  94. ]);
  95. expect(directory.ended[0]).toEqual({
  96. runId: "r-1",
  97. toolCallId: "call-r-1",
  98. label: "review-changes",
  99. status: "stopped",
  100. stopReason: "interrupted",
  101. failureCode: "Interrupted",
  102. failureMessage: "host restarted",
  103. updatedAt: 1_700_000_000_000,
  104. resumable: true,
  105. });
  106. });
  107. it("取满 limit 即判截断(提示要出现),差一条就不判", () => {
  108. const full = Array.from({ length: WORKFLOW_RUN_DIRECTORY_LIMIT }, (_unused, index) =>
  109. summary({ runId: `r-${index}` }),
  110. );
  111. expect(buildWorkflowRunDirectory(full).truncated).toBe(true);
  112. expect(buildWorkflowRunDirectory(full.slice(1)).truncated).toBe(false);
  113. });
  114. it("截断按**查询返回的**条数判,而不是过滤后的行数(否则满页老 run 会显示成「就这些」)", () => {
  115. const full = Array.from(
  116. { length: WORKFLOW_RUN_DIRECTORY_LIMIT },
  117. (_unused, index) =>
  118. ({
  119. runId: `r-old-${index}`,
  120. status: "completed",
  121. resumable: false,
  122. }) as V4ConversationWorkflowRunSummary,
  123. );
  124. const directory = buildWorkflowRunDirectory(full);
  125. expect(directory.ended).toEqual([]);
  126. expect(directory.truncated).toBe(true);
  127. });
  128. it("摘要缺席(还没查到 / 能力缺席)与空数组一样是空目录,不抛", () => {
  129. for (const input of [null, undefined, []] as const) {
  130. const directory = buildWorkflowRunDirectory(input);
  131. expect(directory.running).toEqual([]);
  132. expect(directory.ended).toEqual([]);
  133. expect(directory.truncated).toBe(false);
  134. expect(countEndedWorkflowRuns(input)).toBe(0);
  135. }
  136. });
  137. });
  138. // ── 新鲜度触发器(实测 bug:跑完的 run 不会自己挪到「已结束」)──
  139. //
  140. // 这个键的两个性质同样重要:**该动的时候动**(多一个 run / 跑完一个 run),
  141. // **不该动的时候不动**(节点级进度)。后者不是优化,是它能不能挂在一个开着的页面上的前提:
  142. // 用 `revision` 的话,一次分页读会变成一条随引擎事件走的流。
  143. describe("workflowRunDirectoryRefreshKey", () => {
  144. function run(
  145. overrides: Partial<WorkflowRunState> & Pick<WorkflowRunState, "runId">,
  146. ): WorkflowRunState {
  147. return {
  148. toolCallId: `call-${overrides.runId}`,
  149. status: "running",
  150. usage: { spentTokens: 0, nodesUsed: 0 },
  151. actors: [],
  152. nodes: [],
  153. lastEventSequence: 0,
  154. ...overrides,
  155. };
  156. }
  157. it("run 跑完时变化:这就是那一行从「运行中」挪到「已结束」的时机", () => {
  158. const before = workflowRunDirectoryRefreshKey([run({ runId: "r-1" })]);
  159. const after = workflowRunDirectoryRefreshKey([run({ runId: "r-1", status: "completed" })]);
  160. expect(after).not.toBe(before);
  161. });
  162. it("三种终态都算跑完(errored / stopped 与 completed 同样要挪段)", () => {
  163. const running = workflowRunDirectoryRefreshKey([run({ runId: "r-1" })]);
  164. for (const status of ["completed", "errored", "stopped"] as const) {
  165. expect(workflowRunDirectoryRefreshKey([run({ runId: "r-1", status })])).not.toBe(running);
  166. }
  167. });
  168. it("新起一个 run 时变化:开着页面时它要出现在「运行中」", () => {
  169. const one = workflowRunDirectoryRefreshKey([run({ runId: "r-1" })]);
  170. const two = workflowRunDirectoryRefreshKey([run({ runId: "r-1" }), run({ runId: "r-2" })]);
  171. expect(two).not.toBe(one);
  172. });
  173. it("节点级进度**不**变化——否则一次分页读会变成一条随引擎事件走的流", () => {
  174. const idle = run({ runId: "r-1" });
  175. const busy = run({
  176. runId: "r-1",
  177. nodes: [
  178. { siteId: "ask#1", ordinal: 1, phase: "settled", outcome: "ok" },
  179. { siteId: "ask#2", ordinal: 2, phase: "dispatched" },
  180. ],
  181. lastEventSequence: 42,
  182. usage: { spentTokens: 9_000, nodesUsed: 2 },
  183. });
  184. expect(workflowRunDirectoryRefreshKey([busy])).toBe(workflowRunDirectoryRefreshKey([idle]));
  185. });
  186. it("投影缺席(冷开首帧)有稳定的键,且与「一个 run 都没有」同值", () => {
  187. expect(workflowRunDirectoryRefreshKey(undefined)).toBe(workflowRunDirectoryRefreshKey([]));
  188. });
  189. });