engine-retune-concurrency.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * 就地改本 run 自己的并发上界(`engine.setMaxConcurrency`,docs/dynamic-workflow/concurrency.md
  3. * 「Two bounds on a run」)。这里测的是**第一条界**:调度器的在飞 ask 上界,单位是子代理。
  4. * 进程级共享 cap(单位是模型请求)住在 driver 之下,与本文件无关。
  5. *
  6. * 与 engine-concurrency-observations.test.ts 同一套 fixture 姿态,单独成文件:那份测的是
  7. * 引擎只 record 的三个观察,这份测的是引擎唯一的一条并发**命令**。
  8. */
  9. import { describe, expect, it } from "vitest";
  10. import {
  11. InMemoryJournalStore,
  12. WorkflowEngine,
  13. type AskSpec,
  14. type Caps,
  15. type InstanceRef,
  16. type RunEvent,
  17. } from "../../src/engine/index.js";
  18. import { FakeDriver, flush } from "./fake-driver.js";
  19. const RUN = "run";
  20. const inst = (siteId: string, ordinal = 1): InstanceRef => ({ siteId, ordinal });
  21. const untypedSpecs = (...siteIds: string[]): Map<string, AskSpec> =>
  22. new Map(siteIds.map((id) => [id, { typed: false } as AskSpec]));
  23. const SITES = ["ask#1", "ask#2", "ask#3"] as const;
  24. function setup(caps: Caps, journal = new InMemoryJournalStore()) {
  25. const driver = new FakeDriver(journal);
  26. const engine = new WorkflowEngine({
  27. runId: RUN,
  28. driver,
  29. caps,
  30. askSpecs: untypedSpecs(...SITES),
  31. validate: () => [],
  32. });
  33. return { journal, driver, engine };
  34. }
  35. /**
  36. * 每个 actor 一个 ask:调度器串行同一个 actor,所以「还能不能再派发一个」只由并发上界决定。
  37. * 返回每个 ask 的 promise(按站点序)。
  38. */
  39. function askEachOnItsOwnActor(engine: WorkflowEngine, count: number): Array<Promise<unknown>> {
  40. return SITES.slice(0, count).map((siteId, i) => {
  41. const actor = engine.createActor(`actor#${i + 1}`);
  42. return engine.ask(siteId, actor, `A${i + 1}`);
  43. });
  44. }
  45. const capsEvents = (driver: FakeDriver): Array<Extract<RunEvent, { type: "run-caps-changed" }>> =>
  46. driver.eventsOfType("run-caps-changed");
  47. describe("WorkflowEngine — setMaxConcurrency(就地改本 run 的并发上界)", () => {
  48. it("调低:不再派发新 ask,在飞的照常跑完", async () => {
  49. const { engine, driver } = setup({ maxConcurrency: 2 });
  50. const [p1, p2, p3] = askEachOnItsOwnActor(engine, 3);
  51. await flush();
  52. expect(driver.startAskCount()).toBe(2);
  53. expect(engine.setMaxConcurrency(1)).toBe(true);
  54. await flush();
  55. // 调低不会把已派发的 ask 召回,也不会凭空多派一个。
  56. expect(driver.startAskCount()).toBe(2);
  57. expect(driver.cancels).toEqual([]);
  58. // 一个在飞 ask 结算后仍有 1 个在飞 ⇒ 队列里的第三个 ask 不得放行。
  59. engine.askTurnEnded(inst("ask#1"), "R1");
  60. await expect(p1).resolves.toBe("R1");
  61. await flush();
  62. expect(driver.startAskCount()).toBe(2);
  63. // 降到 0 个在飞才轮到它。
  64. engine.askTurnEnded(inst("ask#2"), "R2");
  65. await expect(p2).resolves.toBe("R2");
  66. await flush();
  67. expect(driver.startAskCount()).toBe(3);
  68. engine.askTurnEnded(inst("ask#3"), "R3");
  69. await expect(p3).resolves.toBe("R3");
  70. });
  71. it("抬高:排队中的 ask 立刻派发,不必等下一次结算", async () => {
  72. const { engine, driver } = setup({ maxConcurrency: 1 });
  73. const asks = askEachOnItsOwnActor(engine, 3);
  74. await flush();
  75. expect(driver.startAskCount()).toBe(1);
  76. expect(engine.setMaxConcurrency(3)).toBe(true);
  77. await flush();
  78. expect(driver.startAskCount()).toBe(3);
  79. for (const [i, site] of SITES.entries()) engine.askTurnEnded(inst(site), `R${i + 1}`);
  80. await expect(Promise.all(asks)).resolves.toEqual(["R1", "R2", "R3"]);
  81. });
  82. it("记一条事件,带改前改后的两份 caps,并把新值写进 journal 行", () => {
  83. const { engine, driver, journal } = setup({ maxConcurrency: 8 });
  84. expect(engine.setMaxConcurrency(2)).toBe(true);
  85. expect(capsEvents(driver)).toEqual([
  86. {
  87. type: "run-caps-changed",
  88. runId: RUN,
  89. caps: { maxConcurrency: 2 },
  90. previous: { maxConcurrency: 8 },
  91. },
  92. ]);
  93. expect(journal.getRun(RUN)?.caps).toEqual({ maxConcurrency: 2 });
  94. // 只碰这一列:状态与用量不在这条写入的范围里。
  95. expect(journal.getRun(RUN)?.status).toBe("running");
  96. // 事件也进了 journal(两条轨拿到的是同一条事件)。
  97. const stored = journal
  98. .listEvents(RUN, { types: "all", reportItems: "all" })
  99. .filter((e) => e.event.type === "run-caps-changed");
  100. expect(stored).toHaveLength(1);
  101. });
  102. it("同值:返回 false,不写库也不发事件", () => {
  103. const { engine, driver, journal } = setup({ maxConcurrency: 4 });
  104. const eventsBefore = driver.events.length;
  105. expect(engine.setMaxConcurrency(4)).toBe(false);
  106. expect(driver.events.length).toBe(eventsBefore);
  107. expect(capsEvents(driver)).toEqual([]);
  108. expect(journal.getRun(RUN)?.caps).toEqual({ maxConcurrency: 4 });
  109. });
  110. it("run 已结算:返回 false,不写库也不发事件(竞态下调用方回落到一次真正的 amend)", () => {
  111. const { engine, driver, journal } = setup({ maxConcurrency: 4 });
  112. engine.complete("done");
  113. const eventsBefore = driver.events.length;
  114. expect(engine.setMaxConcurrency(1)).toBe(false);
  115. expect(driver.events.length).toBe(eventsBefore);
  116. expect(capsEvents(driver)).toEqual([]);
  117. expect(journal.getRun(RUN)?.caps).toEqual({ maxConcurrency: 4 });
  118. });
  119. it("非有限值:按落库归一拒掉,不毒化 caps_max_concurrency", () => {
  120. const { engine, driver, journal } = setup({ maxConcurrency: 4 });
  121. expect(engine.setMaxConcurrency(Number.NaN)).toBe(false);
  122. // 钳到 [1, …] 之后与当前值相同,同样是 no-op(真正的天花板钳制在调用侧)。
  123. expect(engine.setMaxConcurrency(0)).toBe(true);
  124. expect(journal.getRun(RUN)?.caps).toEqual({ maxConcurrency: 1 });
  125. expect(capsEvents(driver)).toHaveLength(1);
  126. });
  127. it("resume 读到的是改后的值,且 replay 仍然确定(命中的 ask 零派发)", async () => {
  128. const journal = new InMemoryJournalStore();
  129. const first = setup({ maxConcurrency: 8 }, journal);
  130. const actor = first.engine.createActor("actor#1");
  131. const p = first.engine.ask("ask#1", actor, "A1");
  132. await flush();
  133. expect(first.engine.setMaxConcurrency(2)).toBe(true);
  134. first.engine.askTurnEnded(inst("ask#1"), "R1");
  135. await expect(p).resolves.toBe("R1");
  136. first.engine.stop("user");
  137. // resume 的 caps 来自 journal 行(宿主侧同一条读:dynamic-workflow-run-submit 的 record.caps)。
  138. const resumedCaps = journal.getRun(RUN)!.caps;
  139. expect(resumedCaps).toEqual({ maxConcurrency: 2 });
  140. const second = setup(resumedCaps, journal);
  141. const actor2 = second.engine.createActor("actor#1");
  142. await expect(second.engine.ask("ask#1", actor2, "A1")).resolves.toBe("R1");
  143. await flush();
  144. // 命中即短路:journal 里多出的那条 run-caps-changed 不参与 replay 判定。
  145. expect(second.driver.startAskCount()).toBe(0);
  146. expect(second.driver.cachedSettleOrder()).toEqual(["ask#1@1"]);
  147. expect(second.driver.eventsOfType("run-started")[0]?.caps).toEqual({ maxConcurrency: 2 });
  148. expect(capsEvents(second.driver)).toEqual([]);
  149. });
  150. });