control.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * harness 的活体控制面接线(`RunControlBinding`,docs/dynamic-workflow/concurrency.md
  3. * 「Two bounds on a run」):引擎构造好之后交给调用方的那个句柄,必须指向**这一世的**引擎,
  4. * 且在子进程真的跑起来之后仍然管用。
  5. *
  6. * 这里不测命令的语义(那在 @zcode/dynamic-workflow 的引擎用例里),只测接线:绑没绑、
  7. * 绑的是不是活着的那个引擎、句柄发出的改动有没有落到这个 run 的 journal 行上。
  8. */
  9. import { describe, expect, it } from "vitest";
  10. import {
  11. InMemoryJournalStore,
  12. validate,
  13. type JsonSchema,
  14. type RunEvent,
  15. type ValidateFn,
  16. } from "@zcode/dynamic-workflow";
  17. import { runWorkflowScript, type RunControlBinding } from "../src/index.js";
  18. import { AutoDriver } from "./auto-driver.js";
  19. import { askSpecsFor, TEST_CWD } from "./helpers.js";
  20. const validateFn: ValidateFn = (schema, value) => validate(schema as JsonSchema, value);
  21. const RUN = "run-control";
  22. const SCRIPT = [
  23. 'const worker = agent("worker", "You answer questions.");',
  24. 'const answer = await worker.ask("What is the answer?");',
  25. "return answer;",
  26. ].join("\n");
  27. /** 捕获 harness 交下来的句柄;`retune` 在句柄未绑时大声失败,而不是静默通过。 */
  28. function captureControl(): RunControlBinding & { retune: (n: number) => boolean } {
  29. let bound: { setMaxConcurrency(n: number): boolean } | undefined;
  30. return {
  31. bind(engine) {
  32. bound = engine;
  33. },
  34. retune(n) {
  35. if (bound === undefined) throw new Error("control was never bound to an engine");
  36. return bound.setMaxConcurrency(n);
  37. },
  38. };
  39. }
  40. describe("harness — 活体控制面绑定", () => {
  41. it("在子进程跑起来之后,句柄仍指向这一世的引擎:改动落到 journal 行与事件轨", async () => {
  42. const journal = new InMemoryJournalStore();
  43. const control = captureControl();
  44. // 子代理的 ask 一开始执行就说明引擎已经在跑;此刻用句柄改上界,然后才让它作答。
  45. const retunes: boolean[] = [];
  46. const driver = new AutoDriver(journal, {
  47. asks: { "ask#1": () => ({ type: "text", finalText: "42" }) },
  48. onStartAsk: () => {
  49. retunes.push(control.retune(3));
  50. },
  51. });
  52. const settlement = await runWorkflowScript({
  53. scriptText: SCRIPT,
  54. cwd: TEST_CWD,
  55. runId: RUN,
  56. caps: { maxConcurrency: 8 },
  57. askSpecs: askSpecsFor(SCRIPT),
  58. validate: validateFn,
  59. makeDriver: (sink) => {
  60. driver.attach(sink);
  61. return driver;
  62. },
  63. control,
  64. timeoutMs: 20000,
  65. });
  66. expect(settlement).toEqual({ status: "completed", artifact: "42" });
  67. expect(retunes).toEqual([true]);
  68. expect(journal.getRun(RUN)?.caps).toEqual({ maxConcurrency: 3 });
  69. const changed = driver.events.filter(
  70. (e): e is Extract<RunEvent, { type: "run-caps-changed" }> => e.type === "run-caps-changed",
  71. );
  72. expect(changed).toEqual([
  73. {
  74. type: "run-caps-changed",
  75. runId: RUN,
  76. caps: { maxConcurrency: 3 },
  77. previous: { maxConcurrency: 8 },
  78. },
  79. ]);
  80. });
  81. it("run 结算之后句柄是 no-op:返回 false,行与事件轨都不再变", async () => {
  82. const journal = new InMemoryJournalStore();
  83. const control = captureControl();
  84. const driver = new AutoDriver(journal, {
  85. asks: { "ask#1": () => ({ type: "text", finalText: "42" }) },
  86. });
  87. await runWorkflowScript({
  88. scriptText: SCRIPT,
  89. cwd: TEST_CWD,
  90. runId: RUN,
  91. caps: { maxConcurrency: 8 },
  92. askSpecs: askSpecsFor(SCRIPT),
  93. validate: validateFn,
  94. makeDriver: (sink) => {
  95. driver.attach(sink);
  96. return driver;
  97. },
  98. control,
  99. timeoutMs: 20000,
  100. });
  101. const eventsAtSettlement = driver.events.length;
  102. expect(control.retune(2)).toBe(false);
  103. expect(journal.getRun(RUN)?.caps).toEqual({ maxConcurrency: 8 });
  104. expect(driver.events.length).toBe(eventsAtSettlement);
  105. });
  106. });