workflow-terminal-notification.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // ============================================================
  2. // dwf 终态通知的三终态词与 provider 停下的文案表(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md)
  3. // ============================================================
  4. // 直接钉 formatter:`<status>` 用真实终态词、`<stop-reason>` 只在 stopped 上在场、`<error>` 块
  5. // 按 providerStop.kind 选文案(原因 → 动作 → 事实行 → 原文行)、呈现指引按 reason 分叉;
  6. // legacy `Workflow`(不带 runStatus)逐字节不变。
  7. import { describe, expect, it } from "vitest";
  8. import type { DynamicWorkflowRunError } from "@zcode/contracts";
  9. import {
  10. formatTaskNotification,
  11. formatWorkflowProviderStopError,
  12. formatWorkflowStallNotification,
  13. type TaskNotificationInput,
  14. } from "../src/runtime-task/notification.js";
  15. const BASE: TaskNotificationInput = {
  16. status: "killed",
  17. summary: 'Workflow "nightly audit" was stopped.',
  18. taskId: "dwfrun-1",
  19. taskType: "local_workflow",
  20. deliveryGuidance: true,
  21. };
  22. function providerStop(
  23. overrides: Partial<NonNullable<DynamicWorkflowRunError["providerStop"]>> = {},
  24. ): DynamicWorkflowRunError {
  25. return {
  26. code: "ProviderStop",
  27. message: "Subagent turn failed: [1006] token expired",
  28. providerStop: {
  29. kind: "auth",
  30. reason: "auth_failed",
  31. providerId: "account:bigmodel-coding-plan",
  32. providerLabel: "BigModel",
  33. modelId: "GLM-5.3",
  34. providerCode: "1006",
  35. subagent: "verify@2",
  36. subagentName: "verifier",
  37. phase: "Verify",
  38. rawMessage: "[1006] token expired",
  39. ...overrides,
  40. },
  41. };
  42. }
  43. describe("formatTaskNotification(local_workflow):三终态词", () => {
  44. it("stopped(user):<status>stopped</status> + <stop-reason>user</stop-reason> + 不要恢复的指引", () => {
  45. const text = formatTaskNotification({ ...BASE, runStatus: "stopped", stopReason: "user" });
  46. expect(text).toContain("<status>stopped</status>");
  47. expect(text).toContain("<stop-reason>user</stop-reason>");
  48. expect(text).toContain("The user stopped this workflow on purpose.");
  49. expect(text).not.toContain("<stopped-by>");
  50. expect(text).not.toContain("resumable as-is");
  51. });
  52. it("stopped(interrupted):指引直接给出 ResumeWorkflowRun 与 run_id", () => {
  53. const text = formatTaskNotification({
  54. ...BASE,
  55. runStatus: "stopped",
  56. stopReason: "interrupted",
  57. failure: { code: "Interrupted", message: "the owning process exited" },
  58. error: "the owning process exited",
  59. });
  60. expect(text).toContain("<stop-reason>interrupted</stop-reason>");
  61. expect(text).toContain("<error>the owning process exited</error>");
  62. expect(text).toContain("The process that owned this run exited before it finished.");
  63. expect(text).toContain('ResumeWorkflowRun with run_id="dwfrun-1"');
  64. });
  65. it("stopped(provider):<error> 是文案表铸的整块,指引先解决原因再 resume、不要重建", () => {
  66. const text = formatTaskNotification({
  67. ...BASE,
  68. runStatus: "stopped",
  69. stopReason: "provider",
  70. failure: providerStop(),
  71. error: "Subagent turn failed: [1006] token expired",
  72. });
  73. expect(text).toContain("<stop-reason>provider</stop-reason>");
  74. expect(text).toContain("<error>\n");
  75. expect(text).toContain("Sign-in to BigModel (account:bigmodel-coding-plan) expired");
  76. expect(text).toContain('then call ResumeWorkflowRun with run_id="dwfrun-1"');
  77. expect(text).toContain(
  78. "provider=account:bigmodel-coding-plan model=GLM-5.3 subagent=verify@2 phase=Verify code=1006",
  79. );
  80. expect(text).toContain("raw: [1006] token expired");
  81. // 原文只出现在 raw 行,不再另贴一遍 message。
  82. expect(text).not.toContain("<error>Subagent turn failed");
  83. expect(text).toContain("A provider-side error stopped this run");
  84. expect(text).toContain("resolve the cause with the user before calling ResumeWorkflowRun");
  85. expect(text).toContain("Do not rebuild the workflow.");
  86. });
  87. it("errored:指引指向 AmendWorkflow,并明说 ResumeWorkflowRun 会拒绝", () => {
  88. const text = formatTaskNotification({
  89. ...BASE,
  90. status: "failed",
  91. runStatus: "errored",
  92. error: "script threw TypeError",
  93. failure: { code: "DriverError", message: "script threw TypeError" },
  94. });
  95. expect(text).toContain("<status>errored</status>");
  96. expect(text).not.toContain("<stop-reason>");
  97. expect(text).toContain("<error>script threw TypeError</error>");
  98. expect(text).toContain("The workflow script failed.");
  99. expect(text).toContain("AmendWorkflow");
  100. expect(text).toContain('run_id="dwfrun-1"');
  101. expect(text).toContain("ResumeWorkflowRun will refuse this run");
  102. });
  103. // 脚本文件(docs/dynamic-workflow/launch.md「Script files」):有文件时下一步是「编辑那个
  104. // 文件、用 `path` 修订」,没有文件才退回「改好脚本再内联提交」的老话。两句都必须逐字节钉住
  105. // ——模型照抄的正是这里的 `run_id` 与 `path`。
  106. it("errored + scriptPath:指引说出路径、`path` 参数与「不要内联粘贴」", () => {
  107. const text = formatTaskNotification({
  108. ...BASE,
  109. status: "failed",
  110. runStatus: "errored",
  111. error: "script threw TypeError",
  112. scriptPath: ".zcode/workflow-drafts/audit.dwf.ts",
  113. });
  114. expect(text).toContain(
  115. 'The run\'s script is at .zcode/workflow-drafts/audit.dwf.ts. Edit that file in place, then call AmendWorkflow (run_id="dwfrun-1", path=".zcode/workflow-drafts/audit.dwf.ts") so finished work is reused — do not paste the script inline. ResumeWorkflowRun will refuse this run: replaying the same script would fail the same way.',
  116. );
  117. // 有文件时旧话整句消失:两句并存会让模型以为内联重提交仍是一条平等的路。
  118. expect(text).not.toContain("Fix the script and submit it with AmendWorkflow");
  119. });
  120. it("errored 不带 scriptPath:旧那一句逐字节不变", () => {
  121. const text = formatTaskNotification({
  122. ...BASE,
  123. status: "failed",
  124. runStatus: "errored",
  125. error: "script threw TypeError",
  126. });
  127. expect(text).toContain(
  128. 'Fix the script and submit it with AmendWorkflow (run_id="dwfrun-1") so finished work is reused. ResumeWorkflowRun will refuse this run: replaying the same script would fail the same way.',
  129. );
  130. expect(text).not.toContain("Edit that file in place");
  131. });
  132. it("stopped(model) + scriptPath:为改脚本而停的那一句尾巴上多一句「编辑它、传 path」", () => {
  133. const text = formatTaskNotification({
  134. ...BASE,
  135. runStatus: "stopped",
  136. stopReason: "model",
  137. scriptPath: ".zcode/workflow-drafts/audit.dwf.ts",
  138. });
  139. expect(text).toContain("You stopped this workflow with TaskStop.");
  140. expect(text).toContain(
  141. "Its script is at .zcode/workflow-drafts/audit.dwf.ts: edit that file and pass `path`.",
  142. );
  143. });
  144. it("stopped(model) 不带 scriptPath:说明由模型停止、下次直接修订,不给文件路径", () => {
  145. const text = formatTaskNotification({ ...BASE, runStatus: "stopped", stopReason: "model" });
  146. expect(text).toContain("<stop-reason>model</stop-reason>");
  147. expect(text).toContain("You stopped this workflow with TaskStop.");
  148. expect(text).toContain(
  149. "(Next time, amend the running run directly: AmendWorkflow stops it for you.)",
  150. );
  151. expect(text).not.toContain("edit that file and pass");
  152. });
  153. it("scriptPath 只喂给 errored 与 stopped(model) 两支:其余终态的指引一个字不变", () => {
  154. const withPath = { ...BASE, scriptPath: ".zcode/workflow-drafts/audit.dwf.ts" };
  155. for (const input of [
  156. { ...withPath, status: "completed", runStatus: "completed" as const },
  157. { ...withPath, runStatus: "stopped" as const, stopReason: "user" as const },
  158. { ...withPath, runStatus: "stopped" as const, stopReason: "interrupted" as const },
  159. ]) {
  160. const text = formatTaskNotification(input);
  161. expect(text).not.toContain(".zcode/workflow-drafts/audit.dwf.ts");
  162. }
  163. });
  164. it("legacy Workflow(无 runStatus)逐字节沿用追踪器词汇与旧指引", () => {
  165. const text = formatTaskNotification({ ...BASE, status: "failed", error: "boom" });
  166. expect(text).toContain("<status>failed</status>");
  167. expect(text).not.toContain("<stop-reason>");
  168. expect(text).toContain("<error>boom</error>");
  169. expect(text).toContain(
  170. "if the process died (error code Interrupted), the run is resumable as-is.",
  171. );
  172. });
  173. });
  174. describe("formatWorkflowProviderStopError:按 kind 的文案表", () => {
  175. const cases: {
  176. kind: NonNullable<DynamicWorkflowRunError["providerStop"]>["kind"];
  177. first: RegExp;
  178. second: RegExp;
  179. extra?: Partial<NonNullable<DynamicWorkflowRunError["providerStop"]>>;
  180. }[] = [
  181. {
  182. kind: "auth",
  183. first:
  184. /Sign-in to BigModel \(account:bigmodel-coding-plan\) expired while subagent verifier \(phase "Verify"\) was running\./,
  185. second:
  186. /Ask the user to sign in to BigModel again, then call ResumeWorkflowRun with run_id="dwfrun-1"\. Finished steps are kept\./,
  187. },
  188. {
  189. kind: "not_configured",
  190. first:
  191. /Provider account:bigmodel-coding-plan is not configured on this machine, so subagent verifier \(phase "Verify"\) could not send its request\./,
  192. second:
  193. /configure the provider or switch this session to another model, then call ResumeWorkflowRun/,
  194. },
  195. {
  196. kind: "model_unavailable",
  197. first: /Model GLM-5\.3 is not available on BigModel \(not in the user's plan, or retired\)\./,
  198. second:
  199. /switch this session to a model the plan includes, then call ResumeWorkflowRun with run_id="dwfrun-1"\. Subagents follow the session's model\./,
  200. },
  201. {
  202. kind: "invalid_request",
  203. first:
  204. /BigModel rejected subagent verifier \(phase "Verify"\)'s request as invalid \(code 3001\)\./,
  205. second:
  206. /Switching the session to another model usually clears this; then call ResumeWorkflowRun/,
  207. extra: { providerCode: "3001", reason: "invalid_request" },
  208. },
  209. {
  210. kind: "quota",
  211. first: /BigModel reports the user's quota is exhausted \(code 1005\)\./,
  212. second:
  213. /top up or upgrade the plan, or switch to another provider, then call ResumeWorkflowRun/,
  214. extra: { providerCode: "1005", reason: "invalid_request" },
  215. },
  216. {
  217. kind: "quota",
  218. first:
  219. /BigModel reports the user's usage cap is reached \(code 1308\); it resets at 2026-09-13T12:00:00\.000Z\./,
  220. second:
  221. /Tell the user; after the reset, call ResumeWorkflowRun with run_id="dwfrun-1"\. Do not rebuild the workflow\./,
  222. extra: { providerCode: "1308", reason: "rate_limited", resetAt: Date.UTC(2026, 8, 13, 12) },
  223. },
  224. {
  225. kind: "other",
  226. first:
  227. /BigModel refused subagent verifier \(phase "Verify"\)'s request with a permanent error \(code 1314\)\./,
  228. second:
  229. /Resolve it with the user \(the raw message below says what the provider wants\), then call ResumeWorkflowRun/,
  230. extra: { providerCode: "1314", reason: "unknown" },
  231. },
  232. ];
  233. it.each(cases)(
  234. "$kind(code $extra.providerCode):原因 → 动作 → 事实行 → 原文行",
  235. ({ kind, first, second, extra }) => {
  236. const text = formatWorkflowProviderStopError(providerStop({ kind, ...extra }), "dwfrun-1");
  237. const lines = text.split("\n");
  238. expect(lines[0]).toMatch(first);
  239. expect(lines[1]).toMatch(second);
  240. expect(lines[2]).toBe(
  241. `provider=account:bigmodel-coding-plan model=GLM-5.3 subagent=verify@2 phase=Verify code=${extra?.providerCode ?? "1006"}`,
  242. );
  243. expect(lines[3]).toBe("raw: [1006] token expired");
  244. expect(lines).toHaveLength(4);
  245. },
  246. );
  247. it("每个占位都有兜底:无显示名 / 无子代理名 / 无 phase / 无原文 / 无 code", () => {
  248. const text = formatWorkflowProviderStopError(
  249. {
  250. code: "ProviderStop",
  251. message: "x",
  252. providerStop: { kind: "auth", reason: "auth_failed" },
  253. },
  254. "dwfrun-2",
  255. );
  256. expect(text.split("\n")).toEqual([
  257. "Sign-in to the provider expired while subagent a subagent was running.",
  258. 'Ask the user to sign in to the provider again, then call ResumeWorkflowRun with run_id="dwfrun-2". Finished steps are kept.',
  259. "provider=unknown model=unknown subagent=unknown code=auth_failed",
  260. ]);
  261. });
  262. it("没有 providerStop 明细时退回 message", () => {
  263. expect(formatWorkflowProviderStopError({ code: "Interrupted", message: "exited" }, "r")).toBe(
  264. "exited",
  265. );
  266. });
  267. });
  268. describe("formatWorkflowStallNotification", () => {
  269. it("说清:多久没成功、provider 在答什么、当前并发、run 仍在跑、不要动它", () => {
  270. const text = formatWorkflowStallNotification({
  271. runLabel: "nightly audit",
  272. runId: "dwfrun-1",
  273. sinceMs: 20 * 60_000,
  274. reason: "rate_limited",
  275. cap: 3,
  276. });
  277. expect(text).toContain("<workflow-stall>");
  278. expect(text).toContain("<run-id>dwfrun-1</run-id>");
  279. expect(text).toContain("<run>nightly audit</run>");
  280. expect(text).toContain("<since-ms>1200000</since-ms>");
  281. expect(text).toContain("<dominant-reason>rate_limited</dominant-reason>");
  282. expect(text).toContain("<cap>3</cap>");
  283. expect(text).toContain(
  284. "Workflow run nightly audit (dwfrun-1) has not completed a model request in 20 minutes; the provider keeps answering rate_limited and the run is retrying with backoff (current fan-out 3).",
  285. );
  286. expect(text).toContain("It is still running and needs nothing from you.");
  287. });
  288. it("reason / cap 缺席时文案退化而不空", () => {
  289. const text = formatWorkflowStallNotification({
  290. runLabel: "r",
  291. runId: "dwfrun-1",
  292. sinceMs: 90_000,
  293. });
  294. expect(text).not.toContain("<dominant-reason>");
  295. expect(text).not.toContain("<cap>");
  296. expect(text).toContain(
  297. "in 2 minutes; the provider keeps failing requests and the run is retrying with backoff.",
  298. );
  299. });
  300. });