workflow-terminal-notification.test.ts 14 KB

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