zcodeProtocolV4SessionWorkflowActivity.test.ts 11 KB

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