workflow-driver-world-read-args.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * world-read 实参校验(driver 侧)。见 docs/execution-engine.md 的"World reads"
  3. * 与 "Boundary B: the driver port"。
  4. *
  5. * Boundary A 的 `worldRead(siteId, op, args)` 带的是**位置实参数组**:lowering 原样打包、不看 op、
  6. * 不看元数,所以「每个 op 需要几个什么类型的实参」这道关只在 driver 这一侧
  7. * (spec 的 "Driver owns arity and validation")。这里直接调端口而不经脚本:facade 的类型签名在
  8. * 编译期就拦住了畸形调用,而这道防线要防的恰是编译期之外的接线错误——线协议、lowering,
  9. * 或者下一步新增 op 时忘了在 driver 里接上。
  10. *
  11. * 为什么独立成文件而不并入 workflow-driver.test.ts:那份用例经 helpers 在运行时 import
  12. * `@zcode/core`(真实 AgentRuntime)。本文件只碰 driver 端口本身(它对 `@zcode/core` 只有
  13. * 类型导入),因此不依赖模型侧的任何东西。
  14. */
  15. import { describe, expect, it } from "vitest";
  16. import type { ExecutionPort, FileSystemPort } from "@zcode/contracts";
  17. import { InMemoryJournalStore, type WorkflowReportSink } from "@zcode/dynamic-workflow";
  18. import { createWorkflowEscalationRegistry } from "../src/app/workflow-escalation-registry.js";
  19. import { createAgentRuntimeWorkflowDriver } from "../src/app/workflow-driver.js";
  20. /** 极简内存 fs 端口:只实现 world-read 用到的两个方法,其余被触及即失败。 */
  21. function stubFileSystemPort(glob: string[], read: string): FileSystemPort {
  22. const unsupported = (name: string) => () => {
  23. throw new Error(`stubFileSystemPort.${name} 不应被 world-read 触及`);
  24. };
  25. return {
  26. async searchFiles(request: { path: string; pattern: string }) {
  27. return {
  28. path: request.path,
  29. pattern: request.pattern,
  30. durationMs: 0,
  31. files: glob,
  32. numFiles: glob.length,
  33. truncated: false,
  34. };
  35. },
  36. async readTextFile(request: { path: string }) {
  37. return {
  38. path: request.path,
  39. content: read,
  40. encoding: "utf-8",
  41. bytesRead: read.length,
  42. sizeBytes: read.length,
  43. truncated: false,
  44. };
  45. },
  46. createDirectory: unsupported("createDirectory"),
  47. stat: unsupported("stat"),
  48. readBinaryFile: unsupported("readBinaryFile"),
  49. readTextFileRange: unsupported("readTextFileRange"),
  50. writeTextFile: unsupported("writeTextFile"),
  51. removeFile: unsupported("removeFile"),
  52. listDirectory: unsupported("listDirectory"),
  53. searchText: unsupported("searchText"),
  54. } as unknown as FileSystemPort;
  55. }
  56. /**
  57. * 世界读取用不到子进程的用例里的 ExecutionPort 占位:被触及即失败。
  58. *
  59. * deps 里 executionPort 是**必填**(见 workflow-driver.ts 的字段注释:可选会给出一条静默
  60. * 降级的运行路径),所以这里不能省;而一个会抛的占位比一个空实现好——它让"本用例其实跑了
  61. * 一次 git"变成一次显式失败,而不是一个被忽略的空结果。
  62. */
  63. function unsupportedExecutionPort(): ExecutionPort {
  64. return {
  65. run: () => {
  66. throw new Error("本用例不应触及 ExecutionPort(git.* world-read)");
  67. },
  68. };
  69. }
  70. /** 向上回报 sink:实参校验不经引擎,任一回调被触及都说明接错了。 */
  71. function noopSink(): WorkflowReportSink {
  72. const unexpected = (name: string) => (): never => {
  73. throw new Error(`noopSink.${name} 不应被触及`);
  74. };
  75. return {
  76. askSubmitAttempted: unexpected("askSubmitAttempted"),
  77. askTurnEnded: unexpected("askTurnEnded"),
  78. askProgress: unexpected("askProgress"),
  79. askStats: unexpected("askStats"),
  80. askFailed: unexpected("askFailed"),
  81. };
  82. }
  83. function makeDriver() {
  84. return createAgentRuntimeWorkflowDriver({
  85. journal: new InMemoryJournalStore(),
  86. emit: () => undefined,
  87. executionPort: unsupportedExecutionPort(),
  88. fileSystemPort: stubFileSystemPort(["a.ts"], "body"),
  89. escalationRegistry: createWorkflowEscalationRegistry(),
  90. cwd: process.cwd(),
  91. runId: "run",
  92. runtimeFactory: () => {
  93. throw new Error("实参校验不该创建 actor runtime");
  94. },
  95. })(noopSink());
  96. }
  97. describe("driver world-read argument validation", () => {
  98. it("passes well-formed positional args through to the fs port", async () => {
  99. await expect(makeDriver().executeWorldRead("glob", ["src/**"])).resolves.toEqual(["a.ts"]);
  100. await expect(makeDriver().executeWorldRead("read", ["a.ts"])).resolves.toBe("body");
  101. });
  102. it("rejects a missing argument with a structured DriverError (never a coerced path)", async () => {
  103. // `String(args[0])` 会把 undefined 变成路径 "undefined" 再去读文件,报出的 ENOENT
  104. // 指不回真正的错处——所以这里必须是一条结构化、指名 op 与实参名的拒绝。
  105. await expect(makeDriver().executeWorldRead("read", [])).rejects.toMatchObject({
  106. code: "DriverError",
  107. });
  108. await expect(makeDriver().executeWorldRead("read", [])).rejects.toThrow(/read/);
  109. await expect(makeDriver().executeWorldRead("read", [])).rejects.toThrow(/path/);
  110. });
  111. it("rejects a wrong-typed argument", async () => {
  112. await expect(makeDriver().executeWorldRead("glob", [42])).rejects.toMatchObject({
  113. code: "DriverError",
  114. });
  115. await expect(makeDriver().executeWorldRead("glob", [undefined])).rejects.toMatchObject({
  116. code: "DriverError",
  117. });
  118. await expect(makeDriver().executeWorldRead("glob", [["src/**"]])).rejects.toMatchObject({
  119. code: "DriverError",
  120. });
  121. });
  122. it("rejects extra arguments (only a wiring fault can produce them)", async () => {
  123. // facade 的 `glob(pattern: string)` 在编译期就拒绝多余实参,所以运行期出现多参
  124. // 意味着 lowering / 线协议出了岔子;静默忽略等于把一个可定位的 bug 藏成一次语义不明的读取。
  125. await expect(makeDriver().executeWorldRead("glob", ["src/**", "extra"])).rejects.toMatchObject({
  126. code: "DriverError",
  127. });
  128. });
  129. it("rejects rather than throwing synchronously (so the node settles as failed)", async () => {
  130. // 同步抛错会从 engine.worldRead 里穿出去——引擎是在准入落 journal 之后才 `.then(...)`,
  131. // 于是会留下一个永远 running 的节点。executeWorldRead 因此必须是 async。
  132. const driver = makeDriver();
  133. let promise: Promise<unknown> | undefined;
  134. let threwSynchronously = false;
  135. try {
  136. promise = driver.executeWorldRead("glob", []);
  137. } catch {
  138. threwSynchronously = true;
  139. }
  140. expect(threwSynchronously).toBe(false);
  141. await expect(promise).rejects.toMatchObject({ code: "DriverError" });
  142. });
  143. });