zcodeProtocolV4SessionWorkflowActivity.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // sessions-index 的工作流运行摘要(sessions-index-workflow-activity.ts):
  2. // 从 workflowRuns + backgroundWorks 派生侧栏运行行要画的有界数据(docs/dynamic-workflow/presentation.md
  3. // 「The sidebar run line」)。纯函数、无时钟。
  4. import { describe, expect, it } from "vitest";
  5. import {
  6. deriveSessionWorkflowActivity,
  7. sessionWorkflowActivitySchema,
  8. type BackgroundWorkSummary,
  9. type WorkflowRunNode,
  10. type WorkflowRunState,
  11. } from "../src/zcode-protocol-v4/index.js";
  12. function run(overrides: Partial<WorkflowRunState> & { runId: string }): WorkflowRunState {
  13. return {
  14. status: "running",
  15. usage: { spentTokens: 0, nodesUsed: 0 },
  16. actors: [],
  17. nodes: [],
  18. lastEventSequence: 0,
  19. ...overrides,
  20. };
  21. }
  22. function work(
  23. overrides: Partial<BackgroundWorkSummary> & { workId: string },
  24. ): BackgroundWorkSummary {
  25. return {
  26. kind: "workflow",
  27. title: "Deep research",
  28. status: "running",
  29. startedAt: 1_000,
  30. anchorRowId: null,
  31. ...overrides,
  32. };
  33. }
  34. describe("deriveSessionWorkflowActivity", () => {
  35. it("没有 run 时缺席(旧 CLI / 普通会话什么都不变)", () => {
  36. expect(deriveSessionWorkflowActivity({ workflowRuns: undefined, backgroundWorks: [] })).toBe(
  37. undefined,
  38. );
  39. expect(
  40. deriveSessionWorkflowActivity({
  41. workflowRuns: { revision: 1, runs: [] },
  42. backgroundWorks: [work({ workId: "r-orphan" })],
  43. }),
  44. ).toBeUndefined();
  45. });
  46. it("在跑的 run:声明阶段表按序成站点,当前 running、已进入 done、其余 pending;名字与开始时刻来自后台工作", () => {
  47. const activity = deriveSessionWorkflowActivity({
  48. workflowRuns: {
  49. revision: 3,
  50. runs: [
  51. run({
  52. runId: "r1",
  53. toolCallId: "tc-1",
  54. phaseNames: ["Research", "Draft", "Review"],
  55. phases: [
  56. { name: "Research", rounds: 1 },
  57. { name: "Draft", rounds: 1 },
  58. ],
  59. currentPhase: "Draft",
  60. actors: [
  61. { siteId: "a", ordinal: 1, status: "running" },
  62. { siteId: "b", ordinal: 1, status: "running" },
  63. { siteId: "c", ordinal: 1, status: "completed" },
  64. ],
  65. }),
  66. ],
  67. },
  68. backgroundWorks: [work({ workId: "r1", title: "Deep research", startedAt: 42 })],
  69. });
  70. expect(activity).toEqual({
  71. runs: [
  72. {
  73. runId: "r1",
  74. toolCallId: "tc-1",
  75. name: "Deep research",
  76. status: "running",
  77. startedAt: 42,
  78. phases: [
  79. { name: "Research", status: "done" },
  80. { name: "Draft", status: "running" },
  81. { name: "Review", status: "pending" },
  82. ],
  83. currentPhase: "Draft",
  84. agentsWorking: 2,
  85. },
  86. ],
  87. });
  88. expect(sessionWorkflowActivitySchema.safeParse(activity).success).toBe(true);
  89. });
  90. it("没有声明表时退化为已进入的 phase(进入序);两者都没有时 phases 为空", () => {
  91. const activity = deriveSessionWorkflowActivity({
  92. workflowRuns: {
  93. revision: 1,
  94. runs: [
  95. run({
  96. runId: "r1",
  97. phases: [{ name: "plan", rounds: 1 }],
  98. currentPhase: "verify",
  99. }),
  100. run({ runId: "r2" }),
  101. ],
  102. },
  103. backgroundWorks: [],
  104. });
  105. expect(activity?.runs.map((entry) => entry.phases)).toEqual([
  106. [
  107. { name: "plan", status: "done" },
  108. { name: "verify", status: "running" },
  109. ],
  110. [],
  111. ]);
  112. expect(activity?.runs[0]?.name).toBeUndefined();
  113. expect(activity?.runs[0]?.startedAt).toBeUndefined();
  114. });
  115. it("并行站点:声明表这条路带上 alongside,裁表后指向被裁站点的引用丢掉", () => {
  116. const activity = deriveSessionWorkflowActivity({
  117. workflowRuns: {
  118. revision: 1,
  119. runs: [
  120. run({
  121. runId: "r1",
  122. phaseNames: ["A", "B", "C"],
  123. phaseAlongside: [[], [0], []],
  124. phases: [{ name: "A", rounds: 1 }],
  125. currentPhase: "B",
  126. }),
  127. ],
  128. },
  129. backgroundWorks: [],
  130. });
  131. expect(activity?.runs[0]?.phases).toEqual([
  132. { name: "A", status: "done" },
  133. { name: "B", status: "running", alongside: [0] },
  134. { name: "C", status: "pending" },
  135. ]);
  136. expect(sessionWorkflowActivitySchema.safeParse(activity).success).toBe(true);
  137. });
  138. it("退化路(没有声明表)没有 alongside:已进入的 phase 是另一个下标空间", () => {
  139. const activity = deriveSessionWorkflowActivity({
  140. workflowRuns: {
  141. revision: 1,
  142. runs: [
  143. run({
  144. runId: "r1",
  145. // 声明表缺席(旧 CLI / 无标记脚本)时下标无处可指,整个键不带。
  146. phaseAlongside: [[1], [0]],
  147. phases: [{ name: "plan", rounds: 1 }],
  148. currentPhase: "verify",
  149. }),
  150. ],
  151. },
  152. backgroundWorks: [],
  153. });
  154. expect(activity?.runs[0]?.phases).toEqual([
  155. { name: "plan", status: "done" },
  156. { name: "verify", status: "running" },
  157. ]);
  158. });
  159. // 并行阶段的灯(设计稿 Surfaces.png:A 与 B 同时在烧):控制流只记得最后一个标记,
  160. // 而 A 的子代理在 B 的标记之后仍在干活——成员节点比控制流更早说话,与卡片的规则 1 同构。
  161. function node(overrides: Partial<WorkflowRunNode> & { siteId: string }): WorkflowRunNode {
  162. return { ordinal: 1, phase: "executing", ...overrides };
  163. }
  164. it("并行的两站同时点亮:出生在已进入站的节点还在跑就把它括回 running", () => {
  165. const phases = (nodes: WorkflowRunNode[]) =>
  166. deriveSessionWorkflowActivity({
  167. workflowRuns: {
  168. revision: 1,
  169. runs: [
  170. run({
  171. runId: "r1",
  172. phaseNames: ["A", "B", "C"],
  173. phaseAlongside: [[], [0], []],
  174. phases: [
  175. { name: "A", rounds: 1 },
  176. { name: "B", rounds: 1 },
  177. ],
  178. currentPhase: "B",
  179. nodes,
  180. }),
  181. ],
  182. },
  183. backgroundWorks: [],
  184. })?.runs[0]?.phases.map((phase) => phase.status);
  185. // A 已进入、B 是当前站,A 里有一个节点还在 executing → 两站都烧。
  186. expect(phases([node({ siteId: "ask#1", phaseName: "A" })])).toEqual([
  187. "running",
  188. "running",
  189. "pending",
  190. ]);
  191. // repairing / nudged 同算在跑;dispatched / waiting 不算(模型请求还没发出去)。
  192. expect(phases([node({ siteId: "ask#1", phaseName: "A", phase: "repairing" })])?.[0]).toBe(
  193. "running",
  194. );
  195. expect(phases([node({ siteId: "ask#1", phaseName: "A", phase: "nudged" })])?.[0]).toBe(
  196. "running",
  197. );
  198. expect(phases([node({ siteId: "ask#1", phaseName: "A", phase: "dispatched" })])?.[0]).toBe(
  199. "done",
  200. );
  201. // A 的节点全结算了 → A 回到 done(节点只会说「还在跑」,不改写其他三态)。
  202. expect(
  203. phases([node({ siteId: "ask#1", phaseName: "A", phase: "settled", outcome: "ok" })]),
  204. ).toEqual(["done", "running", "pending"]);
  205. // 失败的节点同样不改灯:侧栏的失败词留给控制流。
  206. expect(
  207. phases([node({ siteId: "ask#1", phaseName: "A", phase: "settled", outcome: "failed" })]),
  208. ).toEqual(["done", "running", "pending"]);
  209. // 没有出生戳的节点(旧 run / 首个标记之前出生)什么都点不亮。
  210. expect(phases([node({ siteId: "ask#1" })])).toEqual(["done", "running", "pending"]);
  211. // 指向未列出站的戳也点不亮。
  212. expect(phases([node({ siteId: "ask#1", phaseName: "Z" })])).toEqual([
  213. "done",
  214. "running",
  215. "pending",
  216. ]);
  217. });
  218. it("run 不在跑时节点一盏灯都点不亮(终态压过一切)", () => {
  219. const activity = deriveSessionWorkflowActivity({
  220. workflowRuns: {
  221. revision: 1,
  222. runs: [
  223. run({
  224. runId: "r1",
  225. status: "stopped",
  226. stopReason: "user",
  227. phaseNames: ["A", "B"],
  228. phases: [{ name: "A", rounds: 1 }],
  229. currentPhase: "B",
  230. // settled 事件没来得及落下:停下的 run 里没有人还在干活。
  231. nodes: [{ siteId: "ask#1", ordinal: 1, phase: "executing", phaseName: "A" }],
  232. }),
  233. ],
  234. },
  235. backgroundWorks: [],
  236. });
  237. expect(activity?.runs[0]?.phases.map((phase) => phase.status)).toEqual(["done", "pending"]);
  238. });
  239. it("终态灯:completed 已进入 done;errored 当前 failed;stopped 当前 pending 并搬 stopReason", () => {
  240. const phaseNames = ["a", "b", "c"];
  241. const entered = [
  242. { name: "a", rounds: 1 },
  243. { name: "b", rounds: 1 },
  244. ];
  245. const activity = deriveSessionWorkflowActivity({
  246. workflowRuns: {
  247. revision: 1,
  248. runs: [
  249. run({
  250. runId: "done",
  251. status: "completed",
  252. phaseNames,
  253. phases: entered,
  254. currentPhase: "b",
  255. }),
  256. run({ runId: "err", status: "errored", phaseNames, phases: entered, currentPhase: "b" }),
  257. run({
  258. runId: "stop",
  259. status: "stopped",
  260. stopReason: "user",
  261. phaseNames,
  262. phases: entered,
  263. currentPhase: "b",
  264. }),
  265. ],
  266. },
  267. backgroundWorks: [],
  268. });
  269. const byId = new Map(activity!.runs.map((entry) => [entry.runId, entry]));
  270. expect(byId.get("done")?.phases.map((p) => p.status)).toEqual(["done", "done", "pending"]);
  271. expect(byId.get("err")?.phases.map((p) => p.status)).toEqual(["done", "failed", "pending"]);
  272. expect(byId.get("stop")?.phases.map((p) => p.status)).toEqual(["done", "pending", "pending"]);
  273. expect(byId.get("stop")?.stopReason).toBe("user");
  274. expect(byId.get("done")?.stopReason).toBeUndefined();
  275. });
  276. it("排序与上限:在跑的按启动序在前,已结束的按 endedAt 倒序,总数裁到 4", () => {
  277. const activity = deriveSessionWorkflowActivity({
  278. workflowRuns: {
  279. revision: 1,
  280. runs: [
  281. run({ runId: "old-done", status: "completed" }),
  282. run({ runId: "live-1", status: "pending" }),
  283. run({ runId: "new-done", status: "errored" }),
  284. run({ runId: "live-2" }),
  285. run({ runId: "no-work-done", status: "stopped" }),
  286. run({ runId: "live-3" }),
  287. ],
  288. },
  289. backgroundWorks: [
  290. work({ workId: "old-done", status: "resultPending", endedAt: 10 }),
  291. work({ workId: "new-done", status: "failed", endedAt: 20 }),
  292. ],
  293. });
  294. expect(activity?.runs.map((entry) => entry.runId)).toEqual([
  295. "live-1",
  296. "live-2",
  297. "live-3",
  298. "new-done",
  299. ]);
  300. });
  301. });