workflow-terminal-notification.test.ts 15 KB

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