fill-workflow-hole.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. FILL_WORKFLOW_HOLE_TOOL_NAME,
  4. FillWorkflowHoleInputJsonSchema,
  5. FillWorkflowHoleInputSchema,
  6. FillWorkflowHoleOutputSchema,
  7. isFillWorkflowHoleOwnedRun,
  8. } from "../src/tools/fill-workflow-hole.js";
  9. import { CreateWorkflowOutputSchema } from "../src/tools/create-workflow.js";
  10. import { GetWorkflowRunOutputSchema } from "../src/tools/get-workflow-run.js";
  11. // docs/dynamic-workflow/launch.md「The `FillWorkflowHole` tool」:模型面是 run_id + hole_id + 恰好一个
  12. // 来源(script / path);`hole` 是 resolveInput 回填的事实块——运行时 schema 认它、模型面 JSON schema
  13. // 不列它(与 AmendWorkflow 的 `predecessor` 同一个姿态)。
  14. describe("FillWorkflowHoleInputSchema", () => {
  15. const BODY = 'const verdict = await agent("judge").ask<string>("judge it");\nreturn verdict;';
  16. it("names the tool and accepts the two model-facing shapes", () => {
  17. expect(FILL_WORKFLOW_HOLE_TOOL_NAME).toBe("FillWorkflowHole");
  18. const inline = { run_id: "dwfrun-1", hole_id: "hole#1", script: BODY };
  19. expect(FillWorkflowHoleInputSchema.parse(inline)).toEqual(inline);
  20. const file = {
  21. run_id: "dwfrun-1",
  22. hole_id: "hole#1",
  23. path: ".zcode/workflow-drafts/a.verdict.dwf.ts",
  24. };
  25. expect(FillWorkflowHoleInputSchema.parse(file)).toEqual(file);
  26. });
  27. it("requires run_id and hole_id and rejects unknown keys", () => {
  28. expect(FillWorkflowHoleInputSchema.safeParse({ hole_id: "hole#1", script: BODY }).success).toBe(
  29. false,
  30. );
  31. expect(
  32. FillWorkflowHoleInputSchema.safeParse({ run_id: "dwfrun-1", script: BODY }).success,
  33. ).toBe(false);
  34. expect(
  35. FillWorkflowHoleInputSchema.safeParse({
  36. run_id: "dwfrun-1",
  37. hole_id: "hole#1",
  38. script: BODY,
  39. body: BODY,
  40. }).success,
  41. ).toBe(false);
  42. expect(
  43. FillWorkflowHoleInputSchema.safeParse({ run_id: "dwfrun-1", hole_id: "hole#1", script: "" })
  44. .success,
  45. ).toBe(false);
  46. });
  47. it("accepts the resolved hole block and rejects a malformed one", () => {
  48. const resolved = {
  49. run_id: "dwfrun-1",
  50. hole_id: "hole#1",
  51. script: BODY,
  52. hole: {
  53. name: "决定分组",
  54. type: "Verdict",
  55. draft_path: "/repo/.zcode/workflow-drafts/triage.dwf.ts",
  56. line: 12,
  57. owned_by_this_session: true,
  58. },
  59. };
  60. expect(FillWorkflowHoleInputSchema.parse(resolved)).toEqual(resolved);
  61. expect(
  62. FillWorkflowHoleInputSchema.safeParse({
  63. ...resolved,
  64. hole: { name: "决定分组", type: "Verdict", owned_by_this_session: false },
  65. }).success,
  66. ).toBe(true);
  67. expect(
  68. FillWorkflowHoleInputSchema.safeParse({ ...resolved, hole: { name: "决定分组" } }).success,
  69. ).toBe(false);
  70. });
  71. it("does not list the hole block in the model-facing JSON schema", () => {
  72. const json = FillWorkflowHoleInputJsonSchema as {
  73. properties?: Record<string, unknown>;
  74. required?: string[];
  75. };
  76. expect(Object.keys(json.properties ?? {}).sort()).toEqual([
  77. "hole_id",
  78. "path",
  79. "run_id",
  80. "script",
  81. ]);
  82. expect(json.required?.sort()).toEqual(["hole_id", "run_id"]);
  83. });
  84. it("shares CreateWorkflow's output schema so the display rides the fill row", () => {
  85. expect(FillWorkflowHoleOutputSchema).toBe(CreateWorkflowOutputSchema);
  86. });
  87. });
  88. // 免确认的 owner 规则(docs/dynamic-workflow/launch.md「Approval」):与 AmendWorkflow 的谓词一样住在
  89. // 契约里,权限服务与 handler 读同一条。
  90. describe("isFillWorkflowHoleOwnedRun", () => {
  91. it("is true only for a hole block that says owned_by_this_session", () => {
  92. expect(isFillWorkflowHoleOwnedRun({ name: "n", type: "T", owned_by_this_session: true })).toBe(
  93. true,
  94. );
  95. expect(isFillWorkflowHoleOwnedRun({ name: "n", type: "T", owned_by_this_session: false })).toBe(
  96. false,
  97. );
  98. expect(isFillWorkflowHoleOwnedRun({ name: "n", type: "T", owned_by_this_session: "yes" })).toBe(
  99. false,
  100. );
  101. expect(isFillWorkflowHoleOwnedRun(undefined)).toBe(false);
  102. expect(isFillWorkflowHoleOwnedRun(null)).toBe(false);
  103. });
  104. });
  105. // GetWorkflowRun 的 `holes` 与 pendingQuestions 同规:零条缺席、≤32 条、strict 字段。
  106. describe("GetWorkflowRunOutputSchema.holes", () => {
  107. const base = {
  108. runId: "dwfrun-1",
  109. label: "triage",
  110. labelSource: "name",
  111. status: "running",
  112. ownedByThisSession: true,
  113. createdAt: 1,
  114. updatedAt: 2,
  115. generatedAt: 3,
  116. summary: "Running.",
  117. usage: { spentTokens: 0, nodesObserved: 0, nodesRunning: 0, nodesCompleted: 0, nodesFailed: 0 },
  118. actors: [],
  119. logTail: [],
  120. subagents: [],
  121. health: { consecutiveFailures: 0, cachedSteps: 0, pendingQuestionsKnown: true },
  122. };
  123. it("accepts waiting and filled holes", () => {
  124. const holes = [
  125. {
  126. siteId: "hole#1",
  127. ordinal: 1,
  128. name: "决定分组",
  129. type: "Verdict",
  130. state: "waiting",
  131. since: 10,
  132. },
  133. {
  134. siteId: "hole#2",
  135. ordinal: 1,
  136. name: "收尾",
  137. type: "string",
  138. state: "filled",
  139. filledAt: 20,
  140. filledBy: "sess_a",
  141. },
  142. ];
  143. expect(GetWorkflowRunOutputSchema.parse({ ...base, holes })).toMatchObject({ holes });
  144. expect(
  145. GetWorkflowRunOutputSchema.safeParse({ ...base, holes: [{ ...holes[0], state: "open" }] })
  146. .success,
  147. ).toBe(false);
  148. });
  149. });
  150. // 递归留白:一次补全的函数体里可以再留白,站点 id 形如 `hole#1/hole#1`(可更深)。入参对 id 只要求非空,
  151. // 不设任何会拒绝 `/` 的正则——模型要能把通知里的 id 原样抄回来。
  152. describe("FillWorkflowHoleInputSchema — nested hole ids", () => {
  153. it("accepts nested site ids verbatim", () => {
  154. for (const id of ["hole#1/hole#1", "hole#1/hole#1/hole#2", "hole#3/hole#1/hole#1/hole#1"]) {
  155. const input = { run_id: "dwfrun-1", hole_id: id, script: "return 1;" };
  156. expect(FillWorkflowHoleInputSchema.parse(input)).toEqual(input);
  157. }
  158. });
  159. });
  160. describe("CreateWorkflowOutputSchema.fill(补全行给自己起名的那一块)", () => {
  161. const base = { diagnostics: [], ok: true, response: "joined" };
  162. it("站点 id + 名字过 strict,草稿路径与行号可选;未知键与空名被拒;缺席照常通过", () => {
  163. expect(CreateWorkflowOutputSchema.safeParse(base).success).toBe(true);
  164. expect(
  165. CreateWorkflowOutputSchema.safeParse({
  166. ...base,
  167. fill: { siteId: "hole#1", name: "决定分组" },
  168. }).success,
  169. ).toBe(true);
  170. expect(
  171. CreateWorkflowOutputSchema.safeParse({
  172. ...base,
  173. fill: {
  174. siteId: "hole#1/hole#1",
  175. name: "第2步",
  176. draftPath: "/w/.zcode/workflow-drafts/a.dwf.ts",
  177. line: 61,
  178. },
  179. }).success,
  180. ).toBe(true);
  181. expect(
  182. CreateWorkflowOutputSchema.safeParse({ ...base, fill: { siteId: "hole#1", name: "" } })
  183. .success,
  184. ).toBe(false);
  185. expect(
  186. CreateWorkflowOutputSchema.safeParse({
  187. ...base,
  188. fill: { siteId: "hole#1", name: "x", type: "T" },
  189. }).success,
  190. ).toBe(false);
  191. });
  192. });