workflowRunThrottle.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // dwf run 详情页的自适应并发观察面(docs/dynamic-workflow/concurrency.md「Protocol and UI」):
  2. // run 头的并发/冷却读数、事件日志的三条行、以及相位到图上四值的映射。
  3. // 全部无 jsdom:这些是纯规则,时钟由参数注入。
  4. import { describe, expect, it } from "vitest";
  5. import type { WorkflowRunNode, WorkflowRunState } from "@zcode/shared/zcode-protocol-v4";
  6. import enUS from "@/i18n/locales/en-US.js";
  7. import zhCN from "@/i18n/locales/zh-CN.js";
  8. import { workflowRunEventLines } from "@/app-shell/workflowRunPanel.js";
  9. import {
  10. throttleReasonLabel,
  11. workflowRunConcurrencyChip,
  12. workflowRunConcurrencyView,
  13. } from "@/app-shell/workflowRunThrottle.js";
  14. import { statusOfRunNode } from "@/components/workflow-graph/run-status.js";
  15. const format = (descriptor: { id: string }, values?: Record<string, string>) =>
  16. values ? `${descriptor.id}:${Object.values(values).join("/")}` : descriptor.id;
  17. const T0 = 1_700_000_000_000;
  18. function node(overrides: Partial<WorkflowRunNode> = {}): WorkflowRunNode {
  19. return {
  20. siteId: "ask#1",
  21. ordinal: 1,
  22. kind: "ask",
  23. phase: "executing",
  24. actorSiteId: "actor#1",
  25. actorOrdinal: 1,
  26. ...overrides,
  27. };
  28. }
  29. describe("原因标签(事件日志用)", () => {
  30. it("已知原因映射到短标签键,陌生原因原样显示", () => {
  31. expect(throttleReasonLabel("rate_limited", format)).toBe(
  32. "chat.toolCall.workflow.run.throttle.reason.rateLimited",
  33. );
  34. expect(throttleReasonLabel("provider_overloaded", format)).toBe(
  35. "chat.toolCall.workflow.run.throttle.reason.overloaded",
  36. );
  37. expect(throttleReasonLabel("offpeak_queued", format)).toBe(
  38. "chat.toolCall.workflow.run.throttle.reason.offpeak",
  39. );
  40. for (const reason of [
  41. "server_error",
  42. "network_error",
  43. "timeout",
  44. "stream_idle_timeout",
  45. "stale_connection",
  46. ]) {
  47. expect(throttleReasonLabel(reason, format)).toBe(
  48. "chat.toolCall.workflow.run.throttle.reason.transient",
  49. );
  50. }
  51. expect(throttleReasonLabel("something_new", format)).toBe("something_new");
  52. });
  53. });
  54. describe("workflowRunConcurrencyView:被压在自己的界之下,或有自己的界时在场", () => {
  55. it("跑在默认上 → undefined;被压下去 → cap/ceiling", () => {
  56. expect(workflowRunConcurrencyView({ cap: 14, ceiling: 14 }, T0)).toBeUndefined();
  57. expect(workflowRunConcurrencyView(undefined, T0)).toBeUndefined();
  58. expect(workflowRunConcurrencyView({ cap: 4, ceiling: 14 }, T0)).toEqual({
  59. cap: 4,
  60. ceiling: 14,
  61. });
  62. });
  63. // 两条界取小(docs/dynamic-workflow/concurrency.md「Two bounds on a run」):读数说的是这次 run
  64. // 此刻真能有几个子代理在飞,哪一条界在起作用不是它要回答的问题。
  65. it("有 limit 时显示两条界里小的那个", () => {
  66. expect(workflowRunConcurrencyView({ cap: 8, ceiling: 8, limit: 3 }, T0)).toEqual({
  67. cap: 3,
  68. ceiling: 8,
  69. });
  70. expect(workflowRunConcurrencyView({ cap: 2, ceiling: 8, limit: 3 }, T0)).toEqual({
  71. cap: 2,
  72. ceiling: 8,
  73. });
  74. // limit 缺席就是只有共享桶那一条界——与从前逐字相同。
  75. expect(workflowRunConcurrencyView({ cap: 8, ceiling: 8 }, T0)).toBeUndefined();
  76. });
  77. // `ceiling` 是默认并发 D,不是上限(docs/dynamic-workflow/concurrency.md「What the user sees」):
  78. // 共享桶可以自动长到 2D,用户也可以把 run 调到 D 之上。
  79. it("默认 run 的共享 cap 长到 D 之上:读数是 min(cap, D) = D,没有可说的", () => {
  80. expect(workflowRunConcurrencyView({ cap: 12, ceiling: 8 }, T0)).toBeUndefined();
  81. });
  82. it("调到默认之上的 run:芯片恒在,读数是 min(cap, limit)", () => {
  83. expect(workflowRunConcurrencyView({ cap: 9, ceiling: 8, limit: 20 }, T0)).toEqual({
  84. cap: 9,
  85. ceiling: 8,
  86. });
  87. expect(workflowRunConcurrencyView({ cap: 24, ceiling: 8, limit: 20 }, T0)).toEqual({
  88. cap: 20,
  89. ceiling: 8,
  90. });
  91. });
  92. it("冷却按收到时刻推 deadline,过期即缺席", () => {
  93. const concurrency = { cap: 4, ceiling: 14, cooldownMs: 20_000 };
  94. expect(workflowRunConcurrencyView(concurrency, T0)).toEqual({
  95. cap: 4,
  96. ceiling: 14,
  97. cooldownUntil: T0 + 20_000,
  98. });
  99. expect(workflowRunConcurrencyView(concurrency, T0 + 10_000)?.cooldownUntil).toBe(T0 + 20_000);
  100. expect(workflowRunConcurrencyView(concurrency, T0 + 20_000)).toEqual({ cap: 4, ceiling: 14 });
  101. });
  102. });
  103. // 芯片在场 = 读数在场 ∧ run 能配置(docs/dynamic-workflow/concurrency.md「What the user sees」)。
  104. // 芯片说的是一条界,界的控件是 Configure;控件走了芯片也走——完成的 run 上没有东西还在这条界下跑,
  105. // 被替代的 run 的界归后继管,留一个数只会被读成一个过期的实时读数。
  106. describe("workflowRunConcurrencyChip:与 Configure 同进同退", () => {
  107. function runWith(overrides: Partial<WorkflowRunState>): WorkflowRunState {
  108. return {
  109. runId: "run-a",
  110. toolCallId: "tool-a",
  111. status: "running",
  112. usage: { spentTokens: 0, nodesUsed: 0 },
  113. actors: [],
  114. nodes: [],
  115. lastEventSequence: 0,
  116. concurrency: { cap: 13, ceiling: 13, limit: 4 },
  117. ...overrides,
  118. };
  119. }
  120. it.each([
  121. ["running", runWith({ status: "running" })],
  122. ["pending", runWith({ status: "pending" })],
  123. ["stopped(可恢复,恢复后仍在这条界下跑)", runWith({ status: "stopped", stopReason: "user" })],
  124. ["errored(换设置重试的主场景)", runWith({ status: "errored" })],
  125. ])("%s 的 run 带着芯片", (_case, state) => {
  126. expect(workflowRunConcurrencyChip(state, T0)).toEqual({ cap: 4, ceiling: 13 });
  127. });
  128. it.each([
  129. ["completed", runWith({ status: "completed" })],
  130. [
  131. "superseded",
  132. runWith({ status: "stopped", stopReason: "superseded", supersededBy: "run-next" }),
  133. ],
  134. ])("%s 的 run 没有芯片,即便界仍在天花板之下", (_case, state) => {
  135. expect(workflowRunConcurrencyChip(state, T0)).toBeUndefined();
  136. });
  137. it("run 不在投影里、或跑在天花板上 → 没有芯片", () => {
  138. expect(workflowRunConcurrencyChip(undefined, T0)).toBeUndefined();
  139. expect(
  140. workflowRunConcurrencyChip(runWith({ concurrency: { cap: 13, ceiling: 13 } }), T0),
  141. ).toBeUndefined();
  142. });
  143. // 芯片的数是一条界,不是在跑的个数:字必须是界的字,两种语言都不能读成「此刻有几个」。
  144. it("两种语言都是「最大并发数 N」/「Max concurrency N」", () => {
  145. const key = "chat.toolCall.workflow.run.concurrency.label";
  146. expect((zhCN as Record<string, string>)[key]).toBe("最大并发数 {cap}");
  147. expect((enUS as Record<string, string>)[key]).toBe("Max concurrency {cap}");
  148. });
  149. });
  150. describe("相位 → 图上四值(决策 39)", () => {
  151. it("executing / repairing / nudged 是 running", () => {
  152. for (const phase of ["executing", "repairing", "nudged"] as const) {
  153. expect(statusOfRunNode(node({ phase }))).toBe("running");
  154. }
  155. });
  156. it("queued / dispatched / waiting 都是 pending:还没有请求在 provider 那里跑", () => {
  157. for (const phase of ["queued", "dispatched", "waiting"] as const) {
  158. expect(statusOfRunNode(node({ phase }))).toBe("pending");
  159. }
  160. });
  161. });
  162. describe("事件日志:三条自适应并发事件各有一行,其余仍走 unknown 兜底", () => {
  163. it("node-waiting(slot) / node-waiting(backoff) / node-executing / concurrency-changed", () => {
  164. const lines = workflowRunEventLines(
  165. [
  166. {
  167. sequence: 1,
  168. type: "node-waiting",
  169. payload: { instance: { siteId: "ask#1", ordinal: 1 }, cause: "slot" },
  170. },
  171. {
  172. sequence: 2,
  173. type: "node-waiting",
  174. payload: {
  175. instance: { siteId: "ask#1", ordinal: 1 },
  176. cause: "backoff",
  177. reason: "rate_limited",
  178. attempt: 2,
  179. delayMs: 20_500,
  180. },
  181. },
  182. {
  183. sequence: 3,
  184. type: "node-executing",
  185. payload: { instance: { siteId: "ask#1", ordinal: 1 } },
  186. },
  187. {
  188. sequence: 4,
  189. type: "concurrency-changed",
  190. payload: { key: "p/m", previous: 14, next: 7, reason: "rate_limited" },
  191. },
  192. { sequence: 5, type: "compaction", payload: { actor: { siteId: "a", ordinal: 1 } } },
  193. ],
  194. format,
  195. );
  196. expect(lines[0]).toMatchObject({
  197. label: "chat.toolCall.workflow.run.event.nodeWaitingSlot",
  198. detail: "ask#1@1",
  199. tone: "default",
  200. });
  201. expect(lines[1]).toMatchObject({
  202. label:
  203. "chat.toolCall.workflow.run.event.nodeWaitingBackoff:chat.toolCall.workflow.run.throttle.reason.rateLimited/21",
  204. detail: "ask#1@1",
  205. tone: "default",
  206. });
  207. expect(lines[2]).toMatchObject({
  208. label: "chat.toolCall.workflow.run.event.nodeExecuting",
  209. detail: "ask#1@1",
  210. });
  211. expect(lines[3]).toMatchObject({
  212. label: "chat.toolCall.workflow.run.event.concurrencyChanged:14/7",
  213. detail: "p/m",
  214. });
  215. expect(lines[4]?.label).toBe("chat.toolCall.workflow.run.event.unknown:compaction");
  216. });
  217. // run-caps-changed(docs/dynamic-workflow/concurrency.md):只改 max_concurrency 的修订就地生效,
  218. // 引擎改完 caps 发这条。与 concurrency-changed 分两行说:那条是治理器压共享桶(「并发 13 → 9」),
  219. // 这条是用户改了这次 run 自己的上限。
  220. it("run-caps-changed 各有一行;caps 读不动时数字写「?」", () => {
  221. const lines = workflowRunEventLines(
  222. [
  223. {
  224. sequence: 1,
  225. type: "run-caps-changed",
  226. payload: {
  227. caps: { maxConcurrency: 2 },
  228. previous: { maxConcurrency: 8 },
  229. concurrencyCeiling: 8,
  230. },
  231. },
  232. { sequence: 2, type: "run-caps-changed", payload: { caps: "nope", previous: 8 } },
  233. ],
  234. format,
  235. );
  236. expect(lines[0]).toMatchObject({
  237. label: "chat.toolCall.workflow.run.event.runCapsChanged:8/2",
  238. tone: "default",
  239. });
  240. expect(lines[0]).not.toHaveProperty("detail");
  241. expect(lines[1]?.label).toBe("chat.toolCall.workflow.run.event.runCapsChanged:?/?");
  242. });
  243. it("两种语言各有一句:并发上限 8 → 2 / Concurrency limit 8 → 2", () => {
  244. expect(zhCN["chat.toolCall.workflow.run.event.runCapsChanged"]).toBe(
  245. "并发上限 {previous} → {next}",
  246. );
  247. expect(enUS["chat.toolCall.workflow.run.event.runCapsChanged"]).toBe(
  248. "Concurrency limit {previous} → {next}",
  249. );
  250. });
  251. it("node-waiting 缺 cause 时按 slot 处理(没有退避细节可说)", () => {
  252. const lines = workflowRunEventLines(
  253. [
  254. {
  255. sequence: 1,
  256. type: "node-waiting",
  257. payload: { instance: { siteId: "ask#1", ordinal: 1 } },
  258. },
  259. ],
  260. format,
  261. );
  262. expect(lines[0]?.label).toBe("chat.toolCall.workflow.run.event.nodeWaitingSlot");
  263. });
  264. });