workflowRunThrottle.test.ts 10.0 KB

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