dynamic-workflow-run-holes.test.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * 留白的宿主侧纯合成规则(dynamic-workflow-run-holes.ts;docs/execution-engine.md「Holes」):
  3. * 编译期事实表、`run-launched.holes` 的下标、快照投影(`waiting` 只听引擎的)与补全诊断的落点映射。
  4. * 端到端(引擎停驻 → 补全 → 草稿改写)在 dynamic-workflow-run-service.test.ts 的留白那组用例里。
  5. */
  6. import { describe, expect, it } from "vitest";
  7. import {
  8. collectSites,
  9. createWorkflowProgram,
  10. holeSiteId,
  11. spliceHoleBody,
  12. type StoredEvent,
  13. } from "@zcode/dynamic-workflow";
  14. import {
  15. compiledHolesOf,
  16. mapFillDiagnostics,
  17. openHoleIndexes,
  18. phaseNamesFromEvents,
  19. projectRunHoles,
  20. type CompiledHole,
  21. } from "../src/app/dynamic-workflow-run-holes.js";
  22. const HOLE_SCRIPT = [
  23. 'phase("准备");',
  24. 'const a = await agent("worker").ask<{ text: string }>("do the thing");',
  25. 'const v = await hole<string>("裁决", `decide on ${a.text}`);',
  26. 'phase("收尾");',
  27. 'const b = await agent("closer").ask<{ text: string }>("close");',
  28. "return v + b.text;",
  29. ].join("\n");
  30. // 留白 id 是名字键(docs/analysis.md「Sites」):按名字取,不写死哈希。
  31. const VERDICT = holeSiteId("裁决");
  32. const HOLES: CompiledHole[] = [
  33. { siteId: VERDICT, name: "裁决", type: "string", line: 3, open: true },
  34. ];
  35. function stored(sequence: number, event: StoredEvent["event"], timeCreated?: number): StoredEvent {
  36. return { sequence, event, ...(timeCreated === undefined ? {} : { timeCreated }) };
  37. }
  38. describe("compiledHolesOf", () => {
  39. it("reads name, type argument text, call line and openness off the site table", () => {
  40. const table = collectSites(createWorkflowProgram(HOLE_SCRIPT));
  41. expect(compiledHolesOf(table)).toEqual(HOLES);
  42. });
  43. it("a filled hole is no longer open, and its body's sites carry the hole prefix", () => {
  44. const table = collectSites(createWorkflowProgram(HOLE_SCRIPT));
  45. const spliced = spliceHoleBody(HOLE_SCRIPT, table, VERDICT, 'return "ok";');
  46. const filled = collectSites(createWorkflowProgram(spliced!.text));
  47. expect(compiledHolesOf(filled)).toEqual([{ ...HOLES[0], open: false }]);
  48. });
  49. });
  50. describe("openHoleIndexes", () => {
  51. it("indexes the declared phase table by the names of the open holes", () => {
  52. expect(openHoleIndexes(["准备", "裁决", "收尾"], HOLES)).toEqual([1]);
  53. });
  54. it("is absent without a phase table, without open holes, or when nothing matches", () => {
  55. expect(openHoleIndexes(undefined, HOLES)).toBeUndefined();
  56. expect(openHoleIndexes(["准备", "裁决"], [{ ...HOLES[0]!, open: false }])).toBeUndefined();
  57. expect(openHoleIndexes(["准备", "收尾"], HOLES)).toBeUndefined();
  58. });
  59. });
  60. describe("phaseNamesFromEvents", () => {
  61. it("takes the declared table, then the effective table of the last fill", () => {
  62. const events = [
  63. stored(0, { type: "run-launched", inputId: "i", phaseNames: ["准备", "裁决"], holes: [1] }),
  64. stored(1, {
  65. type: "hole-filled",
  66. siteId: VERDICT,
  67. filledAt: 5,
  68. phaseNames: ["准备", "裁决", "复核"],
  69. }),
  70. ];
  71. expect(phaseNamesFromEvents(events)).toEqual(["准备", "裁决", "复核"]);
  72. expect(phaseNamesFromEvents([events[0]!])).toEqual(["准备", "裁决"]);
  73. expect(phaseNamesFromEvents([])).toBeUndefined();
  74. });
  75. });
  76. describe("projectRunHoles", () => {
  77. const reached = stored(
  78. 3,
  79. { type: "hole-reached", instance: { siteId: VERDICT, ordinal: 1 }, name: "裁决", prompt: "p" },
  80. 1_000,
  81. );
  82. it("waiting only while the engine says the instance is parked, enriched with type, line and neighbours", () => {
  83. const holes = projectRunHoles({
  84. events: [reached],
  85. openHoles: [{ siteId: VERDICT, ordinal: 1, name: "裁决", since: 1_000 }],
  86. holes: HOLES,
  87. phaseNames: ["准备", "裁决", "收尾"],
  88. });
  89. expect(holes).toEqual([
  90. {
  91. siteId: VERDICT,
  92. ordinal: 1,
  93. name: "裁决",
  94. type: "string",
  95. state: "waiting",
  96. since: 1_000,
  97. line: 3,
  98. before: "准备",
  99. after: "收尾",
  100. },
  101. ]);
  102. });
  103. it("a reached hole nobody is parked at (dead process) is not reported as waiting", () => {
  104. expect(
  105. projectRunHoles({
  106. events: [reached],
  107. openHoles: undefined,
  108. holes: HOLES,
  109. phaseNames: undefined,
  110. }),
  111. ).toEqual([]);
  112. expect(
  113. projectRunHoles({ events: [reached], openHoles: [], holes: HOLES, phaseNames: undefined }),
  114. ).toEqual([]);
  115. });
  116. it("filled comes from the hole-filled event, with the filler and the effective table's neighbours", () => {
  117. const filled = stored(4, {
  118. type: "hole-filled",
  119. siteId: VERDICT,
  120. filledAt: 2_000,
  121. filledBy: "sess_main",
  122. phaseNames: ["准备", "裁决", "复核", "收尾"],
  123. });
  124. const holes = projectRunHoles({
  125. events: [reached, filled],
  126. openHoles: [],
  127. holes: [{ ...HOLES[0]!, open: false }],
  128. phaseNames: ["准备", "裁决", "复核", "收尾"],
  129. });
  130. expect(holes).toEqual([
  131. {
  132. siteId: VERDICT,
  133. ordinal: 1,
  134. name: "裁决",
  135. type: "string",
  136. state: "filled",
  137. filledAt: 2_000,
  138. filledBy: "sess_main",
  139. line: 3,
  140. before: "准备",
  141. after: "复核",
  142. },
  143. ]);
  144. });
  145. it("omits line / neighbours without a compile product or phase table, and falls back on the type", () => {
  146. const holes = projectRunHoles({
  147. events: [reached],
  148. openHoles: [{ siteId: VERDICT, ordinal: 1, name: "裁决", since: 1 }],
  149. holes: undefined,
  150. phaseNames: undefined,
  151. });
  152. expect(holes).toEqual([
  153. {
  154. siteId: VERDICT,
  155. ordinal: 1,
  156. name: "裁决",
  157. type: "unknown",
  158. state: "waiting",
  159. since: 1_000,
  160. },
  161. ]);
  162. });
  163. it("one row per reached ordinal (a hole under a loop), capped at 32", () => {
  164. const events = Array.from({ length: 40 }, (_, index) =>
  165. stored(index, {
  166. type: "hole-reached",
  167. instance: { siteId: VERDICT, ordinal: index + 1 },
  168. name: "裁决",
  169. }),
  170. );
  171. const openHoles = events.map((_, index) => ({
  172. siteId: VERDICT,
  173. ordinal: index + 1,
  174. name: "裁决",
  175. since: 0,
  176. }));
  177. const holes = projectRunHoles({ events, openHoles, holes: HOLES, phaseNames: undefined });
  178. expect(holes).toHaveLength(32);
  179. expect(holes.map((hole) => hole.ordinal)).toEqual(Array.from({ length: 32 }, (_, i) => i + 1));
  180. });
  181. });
  182. describe("mapFillDiagnostics", () => {
  183. const table = collectSites(createWorkflowProgram(HOLE_SCRIPT));
  184. const body = ['const x: number = "no";', 'return "v";'].join("\n");
  185. const spliced = spliceHoleBody(HOLE_SCRIPT, table, VERDICT, body)!;
  186. it("a diagnostic inside the spliced body is reported in the body's own lines and columns", () => {
  187. // 拼接后函数体第 1 行在脚本第 4 行,前面垫了 2 格缩进(调用在第 0 列)。
  188. const [mapped] = mapFillDiagnostics({
  189. diagnostics: [{ line: 4, column: 21, message: "not a number", code: 2322 }],
  190. effectiveText: spliced.text,
  191. body,
  192. insertedAtLine: spliced.insertedAtLine,
  193. insertedLines: spliced.insertedLines,
  194. });
  195. expect(mapped).toEqual({
  196. inFill: true,
  197. line: 1,
  198. column: 19,
  199. message: "not a number",
  200. code: 2322,
  201. });
  202. });
  203. it("a diagnostic after the splice is shifted back onto the draft's lines", () => {
  204. // 有效脚本里 `phase("收尾")` 在第 7 行(3 行函数体 + 右花括号);草稿里它仍在第 4 行。
  205. const [mapped] = mapFillDiagnostics({
  206. diagnostics: [{ line: 7, column: 1, message: "elsewhere", code: 9005 }],
  207. effectiveText: spliced.text,
  208. body,
  209. insertedAtLine: spliced.insertedAtLine,
  210. insertedLines: spliced.insertedLines,
  211. });
  212. expect(mapped).toEqual({ inFill: false, line: 4, column: 1, message: "elsewhere", code: 9005 });
  213. });
  214. it("the closing brace line and lines before the splice map to the draft unchanged", () => {
  215. const mapped = mapFillDiagnostics({
  216. diagnostics: [
  217. { line: 6, column: 1, message: "brace", code: 1 },
  218. { line: 2, column: 5, message: "before", code: 2 },
  219. ],
  220. effectiveText: spliced.text,
  221. body,
  222. insertedAtLine: spliced.insertedAtLine,
  223. insertedLines: spliced.insertedLines,
  224. });
  225. expect(mapped).toEqual([
  226. { inFill: false, line: 3, column: 1, message: "brace", code: 1 },
  227. { inFill: false, line: 2, column: 5, message: "before", code: 2 },
  228. ]);
  229. });
  230. });