workflowRunPanel.test.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // dwf run 详情页的纯逻辑:Cancel 可用性、结果/失败面板判定、事件日志行摘要。
  2. // 全部无 jsdom、无组件——这些是详情页里唯一有"规则"的部分,穷举它们不需要渲染一张图。
  3. // 见 docs/dynamic-workflow/presentation.md「The run pane」「Cancel 语义」「事件日志」。
  4. import { describe, expect, it } from "vitest";
  5. import type {
  6. WorkflowRunNode,
  7. WorkflowRunState,
  8. WorkflowRunActor,
  9. } from "@zcode/shared/zcode-protocol-v4";
  10. import {
  11. isWorkflowRunCancellable,
  12. isWorkflowRunResumable,
  13. workflowActorStartState,
  14. type WorkflowActorSlotRef,
  15. workflowRunEventLines,
  16. workflowRunResultView,
  17. } from "@/app-shell/workflowRunPanel.js";
  18. // 升级问答的规则住在同族的 workflowRunQuestions.ts(拆出去是为了守住 max-lines 门),
  19. // 但它与上面那几组同属"详情页的纯规则",所以钉在同一份用例里。
  20. import { workflowRunQuestionWaitedLabel } from "@/app-shell/workflowRunQuestions.js";
  21. /** 测试用 formatMessage:原样回 id,带插值时拼成 `id:v1/v2`,便于断言取到了哪个 key。 */
  22. const format = (descriptor: { id: string }, values?: Record<string, string>) =>
  23. values ? `${descriptor.id}:${Object.values(values).join("/")}` : descriptor.id;
  24. function run(overrides: Partial<WorkflowRunState> = {}): WorkflowRunState {
  25. return {
  26. runId: "dwfrun-1",
  27. status: "running",
  28. usage: { spentTokens: 0, nodesUsed: 0 },
  29. actors: [],
  30. nodes: [],
  31. lastEventSequence: 0,
  32. ...overrides,
  33. };
  34. }
  35. describe("isWorkflowRunCancellable", () => {
  36. it("只有 running 可取消", () => {
  37. expect(isWorkflowRunCancellable(run({ status: "running" }))).toBe(true);
  38. for (const status of ["pending", "completed", "errored", "stopped"] as const) {
  39. expect(isWorkflowRunCancellable(run({ status }))).toBe(false);
  40. }
  41. });
  42. it("run 不在投影里(被淘汰 / 冷启动)时不可取消", () => {
  43. expect(isWorkflowRunCancellable(undefined)).toBe(false);
  44. });
  45. });
  46. describe("workflowRunResultView", () => {
  47. it("pending / running 没有结果面板", () => {
  48. expect(workflowRunResultView(run({ status: "pending" })).kind).toBe("none");
  49. expect(workflowRunResultView(run({ status: "running" })).kind).toBe("none");
  50. });
  51. it("completed 且无 resultPreview 时只报完成、绝不编造结果", () => {
  52. // resultPreview 本阶段恒缺席(脚本产物只在 RunSettlement.artifact 里,既不在 run-settled
  53. // 事件上也不在 journal 里)。产物的去处是会话里那条后台结果轮,详情页只能指路。
  54. const view = workflowRunResultView(run({ status: "completed" }));
  55. expect(view).toEqual({ kind: "completed" });
  56. });
  57. it("completed 且真带 resultPreview 时如实展示(schema 允许,本阶段不可达)", () => {
  58. const view = workflowRunResultView(run({ status: "completed", resultPreview: "42 files" }));
  59. expect(view).toEqual({ kind: "completed", preview: "42 files" });
  60. });
  61. it("errored / stopped 报错误原文;error 缺省时仍进错误面板;stopped 带原因词", () => {
  62. expect(workflowRunResultView(run({ status: "errored", error: "boom" }))).toEqual({
  63. kind: "error",
  64. status: "errored",
  65. message: "boom",
  66. });
  67. expect(workflowRunResultView(run({ status: "stopped" }))).toEqual({
  68. kind: "error",
  69. status: "stopped",
  70. });
  71. expect(
  72. workflowRunResultView(run({ status: "stopped", stopReason: "provider", error: "expired" })),
  73. ).toEqual({ kind: "error", status: "stopped", stopReason: "provider", message: "expired" });
  74. });
  75. it("run 缺席时是 absent,而不是伪造一个 pending", () => {
  76. expect(workflowRunResultView(undefined)).toEqual({ kind: "absent" });
  77. });
  78. });
  79. // 待答问题的等待时长(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md)。`now` 由调用方注入而不是读时钟:
  80. // 等待中的 run 恰恰不发事件,组件因此自己按固定间隔喂新的时刻,而规则留在这里可穷举。
  81. describe("workflowRunQuestionWaitedLabel", () => {
  82. const NOW = 1_756_000_000_000;
  83. const MINUTE = 60_000;
  84. it("按分钟 / 小时 / 天分档,不足一分钟说「刚刚」", () => {
  85. const at = (elapsedMs: number) => workflowRunQuestionWaitedLabel(NOW - elapsedMs, NOW, format);
  86. expect(at(0)).toBe("sidePane.time.justNow");
  87. expect(at(59_999)).toBe("sidePane.time.justNow");
  88. expect(at(MINUTE)).toBe("sidePane.time.minutesAgo:1");
  89. expect(at(42 * MINUTE)).toBe("sidePane.time.minutesAgo:42");
  90. expect(at(59 * MINUTE + 59_999)).toBe("sidePane.time.minutesAgo:59");
  91. expect(at(60 * MINUTE)).toBe("sidePane.time.hoursAgo:1");
  92. expect(at(23 * 60 * MINUTE)).toBe("sidePane.time.hoursAgo:23");
  93. expect(at(24 * 60 * MINUTE)).toBe("sidePane.time.daysAgo:1");
  94. // 阶梯止于「天」:spec 否决了超时,一个问题按设计可以无限期等下去。
  95. expect(at(9 * 24 * 60 * MINUTE)).toBe("sidePane.time.daysAgo:9");
  96. });
  97. it("askedAt 缺席时整段不渲染,而不是编一个「刚刚」出来", () => {
  98. // 老 journal 重放出的事件没有这个字段。显示一个假的时刻比不显示更坏。
  99. expect(workflowRunQuestionWaitedLabel(undefined, NOW, format)).toBeUndefined();
  100. expect(workflowRunQuestionWaitedLabel(Number.NaN, NOW, format)).toBeUndefined();
  101. });
  102. it("时钟偏斜把 askedAt 推到未来时钳到「刚刚」,绝不给出负数时长", () => {
  103. // 提问时刻由 CLI 进程铸造;远端会话下它与渲染进程根本不是同一台机器。
  104. expect(workflowRunQuestionWaitedLabel(NOW + 5 * MINUTE, NOW, format)).toBe(
  105. "sidePane.time.justNow",
  106. );
  107. });
  108. });
  109. describe("workflowRunEventLines", () => {
  110. it("把引擎实际发出的每个种类摘成可读一行,而不是 dump JSON", () => {
  111. const lines = workflowRunEventLines(
  112. [
  113. {
  114. sequence: 1,
  115. type: "run-started",
  116. payload: { runId: "dwfrun-1", caps: { maxConcurrency: 8 } },
  117. },
  118. {
  119. sequence: 2,
  120. type: "actor-created",
  121. payload: { actor: { siteId: "actor#1", ordinal: 1 }, name: "planner" },
  122. },
  123. {
  124. sequence: 3,
  125. type: "node-queued",
  126. payload: { instance: { siteId: "ask#1", ordinal: 1 }, kind: "ask" },
  127. },
  128. {
  129. sequence: 4,
  130. type: "node-dispatched",
  131. payload: { instance: { siteId: "ask#1", ordinal: 1 } },
  132. },
  133. {
  134. sequence: 5,
  135. type: "node-repairing",
  136. payload: { instance: { siteId: "ask#1", ordinal: 1 }, attempt: 2, violations: [{}, {}] },
  137. },
  138. {
  139. sequence: 6,
  140. type: "node-nudged",
  141. payload: { instance: { siteId: "ask#1", ordinal: 1 } },
  142. },
  143. {
  144. sequence: 7,
  145. type: "node-settled",
  146. payload: { instance: { siteId: "ask#1", ordinal: 1 }, outcome: "ok" },
  147. },
  148. { sequence: 8, type: "usage-updated", payload: { spentTokens: 1_234 } },
  149. { sequence: 9, type: "log", payload: { message: "scanning workspace" } },
  150. {
  151. sequence: 10,
  152. type: "import-cache-closed",
  153. payload: { instance: { siteId: "ask#2", ordinal: 1 }, cause: "mutating-tool", actorName: "fixer" },
  154. },
  155. { sequence: 11, type: "run-settled", payload: { status: "completed" } },
  156. ],
  157. format,
  158. );
  159. expect(lines.map((line) => line.label)).toEqual([
  160. "chat.toolCall.workflow.run.event.runStarted",
  161. "chat.toolCall.workflow.run.event.actorCreated",
  162. "chat.toolCall.workflow.run.event.nodeQueued",
  163. "chat.toolCall.workflow.run.event.nodeDispatched",
  164. "chat.toolCall.workflow.run.event.nodeRepairing:2",
  165. "chat.toolCall.workflow.run.event.nodeNudged",
  166. "chat.toolCall.workflow.run.event.nodeSettled:chat.toolCall.workflow.run.outcome.ok",
  167. "chat.toolCall.workflow.run.event.usageUpdated",
  168. "chat.toolCall.workflow.run.event.log",
  169. "chat.toolCall.workflow.run.event.importCacheClosed",
  170. "chat.toolCall.workflow.run.event.runSettled:chat.toolCall.workflow.run.status.completed",
  171. ]);
  172. // 身份走 detail,label 只承担本地化的那半句。
  173. expect(lines[1]!.detail).toBe("planner · actor#1@1");
  174. // 关门事件点名谁写的:子代理名 + 实例;world.run 关的门只有实例。
  175. expect(lines[9]!.detail).toBe("fixer · ask#2@1");
  176. expect(lines[2]!.detail).toBe("ask#1@1 · ask");
  177. expect(lines[3]!.detail).toBe("ask#1@1");
  178. expect(lines[8]!.detail).toBe("scanning workspace");
  179. // 没有任何一行泄漏原始 JSON。
  180. for (const line of lines) expect(line.detail ?? "").not.toContain("{");
  181. });
  182. it("cached 命中在 settled 行上明说,failed 行带错误原文并标成失败色", () => {
  183. const [cached, failed] = workflowRunEventLines(
  184. [
  185. {
  186. sequence: 1,
  187. type: "node-settled",
  188. payload: { instance: { siteId: "ask#2", ordinal: 3 }, outcome: "ok", cached: true },
  189. },
  190. {
  191. sequence: 2,
  192. type: "node-settled",
  193. payload: {
  194. instance: { siteId: "ask#2", ordinal: 4 },
  195. outcome: "failed",
  196. error: { message: "schema mismatch" },
  197. },
  198. },
  199. ],
  200. format,
  201. );
  202. expect(cached!.label).toBe(
  203. "chat.toolCall.workflow.run.event.nodeSettledCached:chat.toolCall.workflow.run.outcome.ok",
  204. );
  205. expect(cached!.tone).toBe("default");
  206. expect(failed!.detail).toBe("ask#2@4 · schema mismatch");
  207. expect(failed!.tone).toBe("failed");
  208. });
  209. /**
  210. * 升级问答(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md)。两条都不能落进 unknown 兜底:它们是这个
  211. * 特性在审计面上仅有的两条痕迹,显示成「未知事件 escalation-raised」等于没实现。
  212. */
  213. it("升级两行自成一类:qid 打头(唯一的关联键),actor 名缺席时退回站点实例", () => {
  214. const [named, anonymous, answered] = workflowRunEventLines(
  215. [
  216. {
  217. sequence: 1,
  218. type: "escalation-raised",
  219. payload: {
  220. qid: "dwfq-dwfrun1-1",
  221. actor: { siteId: "actor#1", ordinal: 1 },
  222. actorName: "poet",
  223. question: "评分上限 95,门槛 96,这个门是不是坏了?",
  224. },
  225. },
  226. {
  227. sequence: 2,
  228. type: "escalation-raised",
  229. payload: {
  230. qid: "dwfq-dwfrun1-2",
  231. actor: { siteId: "actor#2", ordinal: 3 },
  232. question: "用哪版风格指南?",
  233. },
  234. },
  235. {
  236. sequence: 3,
  237. type: "escalation-resolved",
  238. payload: { qid: "dwfq-dwfrun1-1", answer: "门确实坏了,按 95 通过。" },
  239. },
  240. ],
  241. format,
  242. );
  243. expect(named!.label).toBe("chat.toolCall.workflow.run.event.escalationRaised");
  244. expect(named!.detail).toBe("dwfq-dwfrun1-1 · poet · 评分上限 95,门槛 96,这个门是不是坏了?");
  245. // 升级不是失败:actor 没出错,它在等一个答案。
  246. expect(named!.tone).toBe("default");
  247. // 匿名 actor 退回 `site@ordinal`——这一行宁可说 actor#2@3,也不能不说是谁。
  248. expect(anonymous!.detail).toBe("dwfq-dwfrun1-2 · actor#2@3 · 用哪版风格指南?");
  249. expect(answered!.label).toBe("chat.toolCall.workflow.run.event.escalationResolved");
  250. // resolved 只有 qid 与答案:它与提问那一行通常隔着几十行,qid 是唯一能把两者接上的东西。
  251. expect(answered!.detail).toBe("dwfq-dwfrun1-1 · 门确实坏了,按 95 通过。");
  252. expect(answered!.tone).toBe("default");
  253. });
  254. it("升级行的长正文按 detail 列的既有上限截断(完整正文归待答问题区)", () => {
  255. const long = "很长的问题".repeat(100);
  256. const [raised] = workflowRunEventLines(
  257. [
  258. {
  259. sequence: 1,
  260. type: "escalation-raised",
  261. payload: { qid: "dwfq-1", actor: { siteId: "a", ordinal: 1 }, question: long },
  262. },
  263. ],
  264. format,
  265. );
  266. expect(raised!.detail!.length).toBeLessThan(long.length);
  267. expect(raised!.detail!.endsWith("…")).toBe(true);
  268. });
  269. it("升级行的字段残缺时只丢那一截,不整行空白(有界化可能削掉深层字段)", () => {
  270. const [noActor, noAnswer] = workflowRunEventLines(
  271. [
  272. {
  273. sequence: 1,
  274. type: "escalation-raised",
  275. payload: { qid: "dwfq-1", question: "还在吗?" },
  276. },
  277. { sequence: 2, type: "escalation-resolved", payload: { qid: "dwfq-1" } },
  278. ],
  279. format,
  280. );
  281. expect(noActor!.detail).toBe("dwfq-1 · 还在吗?");
  282. expect(noAnswer!.detail).toBe("dwfq-1");
  283. });
  284. it("run-settled 的出错/停止同样标成失败色", () => {
  285. const [failed, cancelled] = workflowRunEventLines(
  286. [
  287. {
  288. sequence: 1,
  289. type: "run-settled",
  290. payload: { status: "errored", error: { message: "aborted by cap" } },
  291. },
  292. { sequence: 2, type: "run-settled", payload: { status: "stopped", stopReason: "user" } },
  293. ],
  294. format,
  295. );
  296. expect(failed!.tone).toBe("failed");
  297. expect(failed!.detail).toBe("aborted by cap");
  298. expect(cancelled!.tone).toBe("failed");
  299. });
  300. /**
  301. * report 有自己的一行,不能落进 unknown 兜底:spec 的 Invariants 明写 report 既进事件日志、
  302. * 也进 Results 区(而 `log` 只进事件日志),把它显示成「未知事件 report」是自相矛盾的。
  303. */
  304. it("report 自成一行:带实例,条目只给短摘要(完整预览归 Results 区)", () => {
  305. const [text, object, empty] = workflowRunEventLines(
  306. [
  307. {
  308. sequence: 1,
  309. type: "report",
  310. payload: { instance: { siteId: "report#1", ordinal: 1 }, item: "3 stale imports" },
  311. },
  312. {
  313. sequence: 2,
  314. type: "report",
  315. payload: { instance: { siteId: "report#2", ordinal: 4 }, item: { file: "a.ts" } },
  316. },
  317. { sequence: 3, type: "report", payload: { instance: { siteId: "report#3", ordinal: 1 } } },
  318. ],
  319. format,
  320. );
  321. expect(text!.label).toBe("chat.toolCall.workflow.run.event.report");
  322. expect(text!.detail).toBe("report#1@1 · 3 stale imports");
  323. expect(text!.tone).toBe("default");
  324. // object 压成单行 JSON(事件日志一行一条,多行 JSON 会把它撑成第二个产物视图)。
  325. expect(object!.detail).toBe('report#2@4 · {"file":"a.ts"}');
  326. // item 缺席(有界化削掉 / 脚本报了 undefined)时只剩身份,绝不空白。
  327. expect(empty!.detail).toBe("report#3@1");
  328. });
  329. it("phase-entered 自成一行:名字,第二次起带轮次", () => {
  330. const [first, second, bare] = workflowRunEventLines(
  331. [
  332. { sequence: 1, type: "phase-entered", payload: { name: "Verify", ordinal: 1 } },
  333. { sequence: 2, type: "phase-entered", payload: { name: "Verify", ordinal: 2 } },
  334. { sequence: 3, type: "phase-entered", payload: {} },
  335. ],
  336. format,
  337. );
  338. expect(first!.label).toBe("chat.toolCall.workflow.run.event.phaseEntered");
  339. expect(first!.detail).toBe("Verify");
  340. expect(second!.detail).toBe("Verify · 2");
  341. expect(bare!.detail).toBeUndefined();
  342. expect(bare!.tone).toBe("default");
  343. });
  344. it("未知种类退化成 type + 截断 JSON,既不崩也不假装认识它", () => {
  345. // compaction 是引擎为长上下文压缩预留的种类(v1 从不发出)。将来它一出现,
  346. // 事件日志必须仍然显示一条有信息量的行,而不是空白或异常。
  347. const [line] = workflowRunEventLines(
  348. [{ sequence: 1, type: "compaction", payload: { actor: { siteId: "actor#1", ordinal: 1 } } }],
  349. format,
  350. );
  351. expect(line!.label).toBe("chat.toolCall.workflow.run.event.unknown:compaction");
  352. expect(line!.detail).toContain("actor#1");
  353. });
  354. it("超长未知载荷被截断,不把整条 journal 灌进 DOM", () => {
  355. const [line] = workflowRunEventLines(
  356. [{ sequence: 1, type: "mystery", payload: { blob: "x".repeat(5_000) } }],
  357. format,
  358. );
  359. expect(line!.detail!.length).toBeLessThanOrEqual(200);
  360. expect(line!.detail!.endsWith("…")).toBe(true);
  361. });
  362. it("载荷形状不符时不抛异常(有界化可能已经削掉字段)", () => {
  363. const lines = workflowRunEventLines(
  364. [
  365. { sequence: 1, type: "node-settled", payload: {} },
  366. { sequence: 2, type: "actor-created", payload: { actor: "not-a-ref" } },
  367. { sequence: 3, type: "log", payload: {} },
  368. ],
  369. format,
  370. );
  371. expect(lines).toHaveLength(3);
  372. expect(lines.every((line) => typeof line.label === "string")).toBe(true);
  373. });
  374. it("truncated 事件原样透出标记,让读者知道原始事实要去 journal 取", () => {
  375. const [line] = workflowRunEventLines(
  376. [{ sequence: 9, type: "log", payload: { message: "partial" }, truncated: true }],
  377. format,
  378. );
  379. expect(line!.truncated).toBe(true);
  380. expect(line!.sequence).toBe(9);
  381. });
  382. });
  383. function actor(overrides: Partial<WorkflowRunActor> = {}): WorkflowRunActor {
  384. return {
  385. siteId: "actor#1",
  386. ordinal: 1,
  387. sessionId: "dwf-run1-actor_1@1",
  388. status: "waiting",
  389. ...overrides,
  390. };
  391. }
  392. describe("workflowActorStartState", () => {
  393. // actor 未启动门(docs/dynamic-workflow/presentation.md「Subagent transcripts」,身份改按槽位:
  394. // docs/dynamic-workflow/presentation.md「The pill」)。三值里最要紧的是 unknown 与
  395. // notStarted 的分界:拦错一格就把一份仍然可读的 transcript 关死,漏拦一格就回到那个死错误面板。
  396. const node = (overrides: Partial<WorkflowRunNode> = {}): WorkflowRunNode => ({
  397. siteId: "ask#1",
  398. ordinal: 1,
  399. phase: "queued",
  400. actorSiteId: "actor#1",
  401. actorOrdinal: 1,
  402. ...overrides,
  403. });
  404. const slot = (overrides: Partial<WorkflowActorSlotRef> = {}): WorkflowActorSlotRef => ({
  405. runId: "dwfrun-1",
  406. siteId: "actor#1",
  407. ordinal: 1,
  408. ...overrides,
  409. });
  410. it("actor 在投影里但一个节点都没有 → notStarted,会话 id 取投影", () => {
  411. const runs = [run({ actors: [actor({ sessionId: "s-a" })] })];
  412. expect(workflowActorStartState(runs, slot())).toEqual({
  413. state: "notStarted",
  414. sessionId: "s-a",
  415. });
  416. });
  417. it("只有 queued 节点 → notStarted:队列里的节点不构成会话已落库的证据", () => {
  418. // 持久化顺序的保证挂在「派发前」(workflow-driver 先 await 会话落库再首次派发),
  419. // 所以 queued 不能开门——那正是实测里点早了的那 5 秒。
  420. const runs = [
  421. run({ actors: [actor({ sessionId: "s-a" })], nodes: [node(), node({ ordinal: 2 })] }),
  422. ];
  423. expect(workflowActorStartState(runs, slot()).state).toBe("notStarted");
  424. });
  425. it("dispatched / repairing / nudged / settled(含 cached)都算 started,订阅投影里的会话", () => {
  426. for (const phase of ["dispatched", "repairing", "nudged", "settled"] as const) {
  427. const runs = [
  428. run({
  429. actors: [actor({ sessionId: "s-a" })],
  430. nodes: [node(), node({ ordinal: 2, phase })],
  431. }),
  432. ];
  433. expect(workflowActorStartState(runs, slot())).toEqual({ state: "started", sessionId: "s-a" });
  434. }
  435. // 缓存命中直接发 node-settled(不经 queued,也不经 ensureSession):门照样打开,订阅的是
  436. // 投影里 actor 的 sessionId——reducer 让它指向持有转录的那条会话
  437. // (workflowCachedSettleAttribution.test.ts 走真实 reducer 钉住修订 run 的那一种)。
  438. const cached = [
  439. run({
  440. actors: [actor({ sessionId: "s-a" })],
  441. nodes: [node({ phase: "settled", outcome: "ok", cached: true })],
  442. }),
  443. ];
  444. expect(workflowActorStartState(cached, slot()).state).toBe("started");
  445. });
  446. it("actor 不在投影里、tab 带会话 id → unknown:run 被淘汰、冷恢复、投影整体缺席", () => {
  447. // tab 刻意没有 GC,比 8-run 淘汰活得久。这一格绝不能是 notStarted:那会把已完结 run
  448. // 的唯一持久视图关死,而直接订阅本来是能读到的。订阅目标就是 tab 带的那个 id。
  449. const expected = { state: "unknown", sessionId: "s-a" };
  450. const runs = [run({ actors: [actor({ ordinal: 2, sessionId: "s-other" })] })];
  451. expect(workflowActorStartState(runs, slot({ actorSessionId: "s-a" }))).toEqual(expected);
  452. expect(workflowActorStartState([], slot({ actorSessionId: "s-a" }))).toEqual(expected);
  453. expect(workflowActorStartState(undefined, slot({ actorSessionId: "s-a" }))).toEqual(expected);
  454. // 另一条 run 里的同名同序号不算命中:身份含 runId。
  455. const other = [
  456. run({
  457. runId: "dwfrun-2",
  458. actors: [actor({ sessionId: "s-a" })],
  459. nodes: [node({ phase: "dispatched" })],
  460. }),
  461. ];
  462. expect(workflowActorStartState(other, slot({ actorSessionId: "s-a" }))).toEqual(expected);
  463. });
  464. it("actor 不在投影里、tab 没有会话 id → notStarted:未启动药丸开的槽位,没有可订阅的东西", () => {
  465. expect(workflowActorStartState([run()], slot())).toEqual({ state: "notStarted" });
  466. expect(workflowActorStartState(undefined, slot())).toEqual({ state: "notStarted" });
  467. });
  468. it("投影里的会话 id 优先于 tab 带的;投影没有时才用 tab 的;两边都没有就只有处境", () => {
  469. const live = [
  470. run({ actors: [actor({ sessionId: "s-live" })], nodes: [node({ phase: "dispatched" })] }),
  471. ];
  472. expect(workflowActorStartState(live, slot({ actorSessionId: "s-stale" }))).toEqual({
  473. state: "started",
  474. sessionId: "s-live",
  475. });
  476. // sessionId 尚未落到投影上的 actor(schema-optional):tab 带的兜底。
  477. const bare = [
  478. run({ actors: [actor({ sessionId: undefined })], nodes: [node({ phase: "dispatched" })] }),
  479. ];
  480. expect(workflowActorStartState(bare, slot({ actorSessionId: "s-tab" }))).toEqual({
  481. state: "started",
  482. sessionId: "s-tab",
  483. });
  484. expect(workflowActorStartState(bare, slot())).toEqual({ state: "started" });
  485. });
  486. it("跨多 run 按 runId 命中正确的 actor,而不是同名同序号的邻居", () => {
  487. const runs = [
  488. run({
  489. runId: "dwfrun-1",
  490. actors: [actor({ sessionId: "s-a" })],
  491. nodes: [node({ phase: "dispatched" })],
  492. }),
  493. run({
  494. runId: "dwfrun-2",
  495. actors: [actor({ sessionId: "s-b" })],
  496. nodes: [node()],
  497. }),
  498. ];
  499. expect(workflowActorStartState(runs, slot({ runId: "dwfrun-1" }))).toEqual({
  500. state: "started",
  501. sessionId: "s-a",
  502. });
  503. // 同一个 siteId@ordinal,另一条 run:节点只在第一条 run 里派发过。
  504. expect(workflowActorStartState(runs, slot({ runId: "dwfrun-2" }))).toEqual({
  505. state: "notStarted",
  506. sessionId: "s-b",
  507. });
  508. });
  509. it("别的 actor 的节点、以及无 actor 的 world-read 节点,都不算它启动了", () => {
  510. const runs = [
  511. run({
  512. actors: [actor({ sessionId: "s-a" }), actor({ ordinal: 2, sessionId: "s-b" })],
  513. nodes: [
  514. // 同站点、另一个序号:实例是身份的一部分,序号错了就是另一个人。
  515. node({ phase: "dispatched", actorOrdinal: 2 }),
  516. // world-read 没有 actor 归属,不给任何人开门。
  517. node({
  518. siteId: "world-read#1",
  519. phase: "settled",
  520. actorSiteId: undefined,
  521. actorOrdinal: undefined,
  522. }),
  523. ],
  524. }),
  525. ];
  526. expect(workflowActorStartState(runs, slot()).state).toBe("notStarted");
  527. expect(workflowActorStartState(runs, slot({ ordinal: 2 })).state).toBe("started");
  528. });
  529. });
  530. // Resume 可用性(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md;docs/dynamic-workflow/presentation.md
  531. // 2026-09-09「冷回放」):一个信源——投影 run 的 `resumable` 状态位,由 CLI 按 resume 门的同一个
  532. // 谓词算好并经 run-settled 载荷落进状态;UI 绝不自行按 status + failureCode 推导。
  533. describe("isWorkflowRunResumable", () => {
  534. it("状态位在场 → 可恢复,无论 stopped 的原因是什么", () => {
  535. expect(isWorkflowRunResumable(run({ status: "stopped", resumable: true }))).toBe(true);
  536. expect(
  537. isWorkflowRunResumable(
  538. run({ status: "stopped", stopReason: "interrupted", resumable: true }),
  539. ),
  540. ).toBe(true);
  541. });
  542. it("状态位缺席 → 不可恢复,哪怕 status 是 stopped(旧 CLI 不发该位;宁可少一个按钮)", () => {
  543. for (const status of ["pending", "running", "completed", "errored", "stopped"] as const) {
  544. expect(isWorkflowRunResumable(run({ status }))).toBe(false);
  545. }
  546. });
  547. it("投影缺席(被淘汰)→ 不可恢复", () => {
  548. expect(isWorkflowRunResumable(undefined)).toBe(false);
  549. });
  550. });