workflow-run-control.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * run 的活体控制面与它在 `retuneConcurrency` 里的两个后果
  3. * (docs/dynamic-workflow/concurrency.md「The control path」):
  4. * - **还没绑上引擎的 run 一律 not_live,值绝不缓冲**:一个 `pending`、引擎尚未构造出来的 run
  5. * 收下这条命令、等引擎起来再补一次,就是在 UI 只承诺 `running` 就地生效的地方偷偷多承诺一次;
  6. * - 引擎先、闸门后:引擎的布尔值就是这次命令的裁决,它说没改,闸门的上界一动都不动。
  7. */
  8. import { describe, expect, it } from "vitest";
  9. import { InMemoryJournalStore } from "@zcode/dynamic-workflow";
  10. import { retuneRunConcurrency } from "../src/app/dynamic-workflow-run-retune.js";
  11. import type { RunRegistryEntry } from "../src/app/dynamic-workflow-run-observation.js";
  12. import { createWorkflowRunControl } from "../src/app/workflow-run-control.js";
  13. const RUN = "dwfrun-control";
  14. const DEFAULT_CONCURRENCY = 8;
  15. /** 只记调用的假引擎;`verdict` 决定它是否承认这次真的改了。 */
  16. function fakeEngine(verdict = true) {
  17. const calls: number[] = [];
  18. return {
  19. calls,
  20. engine: {
  21. setMaxConcurrency: (next: number) => {
  22. calls.push(next);
  23. return verdict;
  24. },
  25. },
  26. };
  27. }
  28. /** 只记上界的假闸门。 */
  29. function fakeGate() {
  30. const limits: number[] = [];
  31. return { limits, gate: { setLimit: (next: number) => limits.push(next) } };
  32. }
  33. /** 一个最小的在飞条目:retune 只读 terminal / control / maxConcurrency 三样。 */
  34. function liveEntry(control: RunRegistryEntry["control"], maxConcurrency?: number): RunRegistryEntry {
  35. return {
  36. controller: new AbortController(),
  37. startedAt: new Date(),
  38. cwd: "/tmp",
  39. scriptText: "",
  40. settlement: Promise.resolve({ status: "stopped", reason: "user" }) as never,
  41. ...(control === undefined ? {} : { control }),
  42. ...(maxConcurrency === undefined ? {} : { maxConcurrency }),
  43. };
  44. }
  45. function contextWith(entry: RunRegistryEntry) {
  46. const journal = new InMemoryJournalStore();
  47. journal.createRun({ runId: RUN, caps: { maxConcurrency: 4 }, spentTokens: 0, status: "running" });
  48. return { defaultConcurrency: () => DEFAULT_CONCURRENCY, journal, runs: new Map([[RUN, entry]]) };
  49. }
  50. describe("workflow run control", () => {
  51. it("还没绑上引擎:setMaxConcurrency 回 false,闸门不动,值也绝不缓冲", () => {
  52. const control = createWorkflowRunControl();
  53. const { gate, limits } = fakeGate();
  54. control.bindSeatGate(gate);
  55. expect(control.setMaxConcurrency(2)).toBe(false);
  56. expect(limits).toEqual([]);
  57. // 引擎稍后才起来:刚才那条命令**不该**在这一刻被补上。
  58. const { calls, engine } = fakeEngine();
  59. control.bind(engine);
  60. expect(calls).toEqual([]);
  61. expect(limits).toEqual([]);
  62. // 之后的显式调用照常生效。
  63. expect(control.setMaxConcurrency(2)).toBe(true);
  64. expect(calls).toEqual([2]);
  65. expect(limits).toEqual([2]);
  66. });
  67. it("引擎说没改(已结算 / 值没变)时闸门的上界一动不动", () => {
  68. const control = createWorkflowRunControl();
  69. const { gate, limits } = fakeGate();
  70. const { calls, engine } = fakeEngine(false);
  71. control.bind(engine);
  72. control.bindSeatGate(gate);
  73. expect(control.setMaxConcurrency(2)).toBe(false);
  74. expect(calls).toEqual([2]);
  75. expect(limits).toEqual([]);
  76. });
  77. it("没有闸门的装配也照常改(只剩调度器一个执行点)", () => {
  78. const control = createWorkflowRunControl();
  79. const { calls, engine } = fakeEngine();
  80. control.bind(engine);
  81. expect(control.setMaxConcurrency(3)).toBe(true);
  82. expect(calls).toEqual([3]);
  83. });
  84. // `pending` 且引擎还没构造出来的 run:端口必须回 not_live,而不是收下来等引擎起来再补。
  85. // 调用方据此走一次真正的修订——那条路本来就会处理一个还没开跑的前驱。
  86. it("retuneConcurrency 对着一个未绑定的控制面回 not_live,并且什么都没写", () => {
  87. const control = createWorkflowRunControl();
  88. const entry = liveEntry(control, 4);
  89. const ctx = contextWith(entry);
  90. expect(retuneRunConcurrency(ctx, { maxConcurrency: 2, runId: RUN })).toEqual({
  91. ok: false,
  92. reason: "not_live",
  93. current: 4,
  94. });
  95. expect(entry.maxConcurrency).toBe(4);
  96. expect(ctx.journal.getRun(RUN)?.caps).toEqual({ maxConcurrency: 4 });
  97. expect(ctx.journal.listEvents(RUN, { types: "all", reportItems: "all" })).toEqual([]);
  98. });
  99. it("retuneConcurrency 对着没有控制面的老条目同样回 not_live", () => {
  100. const ctx = contextWith(liveEntry(undefined, 4));
  101. expect(retuneRunConcurrency(ctx, { maxConcurrency: 2, runId: RUN })).toEqual({
  102. ok: false,
  103. reason: "not_live",
  104. });
  105. });
  106. });