workflow-store.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import type { WorkflowRunSnapshot } from "@zcode/contracts";
  6. import {
  7. createNodeWorkflowDefinitionStore,
  8. createNodeWorkflowStore,
  9. getDefaultWorkflowDefinitionsRoot,
  10. getDefaultWorkflowRoot,
  11. } from "../src/workflow/index.js";
  12. describe("Node workflow store", () => {
  13. it("defaults to the CLI workflows directory", () => {
  14. expect(getDefaultWorkflowRoot()).toContain(join(".zcode", "cli", "workflows"));
  15. expect(getDefaultWorkflowDefinitionsRoot()).toContain(
  16. join(".zcode", "cli", "workflows", "definitions"),
  17. );
  18. });
  19. it("persists snapshots, events, graph records, artifacts, and index", async () => {
  20. const tempRoot = await mkdtemp(join(tmpdir(), "zcode-workflow-store-"));
  21. const store = createNodeWorkflowStore({ rootDir: tempRoot });
  22. const snapshot: WorkflowRunSnapshot = {
  23. activities: [],
  24. artifacts: [],
  25. createdAt: "2026-05-05T00:00:00.000Z",
  26. cwd: "/repo",
  27. graph: {
  28. edges: [],
  29. nodes: [],
  30. },
  31. kind: "expert",
  32. phaseOrder: ["clarify"],
  33. phases: [
  34. {
  35. phase: "clarify",
  36. status: "pending",
  37. },
  38. ],
  39. runId: "wf_test",
  40. schemaVersion: 1,
  41. status: "running",
  42. strategy: {
  43. clarify: {
  44. confidenceThreshold: 0.8,
  45. maxRounds: 3,
  46. minRounds: 1,
  47. },
  48. executor: {
  49. drainingChangeHours: 1,
  50. frontierTarget: 3,
  51. maxConcurrentLoops: 2,
  52. maxConsecutiveErrors: 3,
  53. maxPlannerRuns: 10,
  54. },
  55. finalCritic: {
  56. maxIterations: 3,
  57. },
  58. reactLoop: {
  59. maxRounds: 30,
  60. },
  61. },
  62. task: "work",
  63. updatedAt: "2026-05-05T00:00:00.000Z",
  64. };
  65. try {
  66. await store.writeSnapshot(snapshot);
  67. await store.appendEvent({
  68. kind: "expert",
  69. runId: "wf_test",
  70. timestamp: "2026-05-05T00:00:01.000Z",
  71. type: "run_started",
  72. });
  73. await store.appendGraphRecord("wf_test", {
  74. nodeId: "phase:clarify",
  75. phase: "clarify",
  76. recordType: "op",
  77. runId: "wf_test",
  78. status: "active",
  79. timestamp: "2026-05-05T00:00:02.000Z",
  80. type: "update_status",
  81. });
  82. const artifact = await store.writeArtifact("wf_test", "artifacts/clarify.md", "# ok\n");
  83. expect(artifact.relativePath).toBe("artifacts/clarify.md");
  84. expect((await store.readRun("wf_test"))?.task).toBe("work");
  85. expect((await store.readLatestRun({ cwd: "/repo", kind: "expert" }))?.runId).toBe("wf_test");
  86. expect(await store.readEvents("wf_test")).toHaveLength(1);
  87. expect(await readFile(join(tempRoot, "runs", "wf_test", "graph.jsonl"), "utf8")).toContain(
  88. "update_status",
  89. );
  90. expect((await store.listRuns({ cwd: "/repo", kind: "expert" }))[0]?.runId).toBe("wf_test");
  91. } finally {
  92. await rm(tempRoot, { force: true, recursive: true });
  93. }
  94. });
  95. it("loads workflow definitions from the definitions directory", async () => {
  96. const tempRoot = await mkdtemp(join(tmpdir(), "zcode-workflow-definitions-"));
  97. const definitionsDir = join(tempRoot, "definitions");
  98. await mkdir(definitionsDir, { recursive: true });
  99. await writeFile(
  100. join(definitionsDir, "audit.json"),
  101. `${JSON.stringify(createDefinition("audit", "1"), null, 2)}\n`,
  102. "utf8",
  103. );
  104. const store = createNodeWorkflowDefinitionStore({ rootDir: tempRoot });
  105. try {
  106. await expect(store.readDefinition("../audit")).rejects.toThrow("file-safe name");
  107. const definition = await store.readDefinition("audit");
  108. expect(definition?.definitionId).toBe("audit");
  109. expect(definition?.definitionVersion).toBe("1");
  110. expect((await store.listDefinitions()).map((item) => item.definitionId)).toEqual(["audit"]);
  111. } finally {
  112. await rm(tempRoot, { force: true, recursive: true });
  113. }
  114. });
  115. it("rejects malformed workflow definition files before runtime creation", async () => {
  116. const tempRoot = await mkdtemp(join(tmpdir(), "zcode-workflow-definitions-"));
  117. const definitionsDir = join(tempRoot, "definitions");
  118. await mkdir(definitionsDir, { recursive: true });
  119. await writeFile(
  120. join(definitionsDir, "broken.json"),
  121. `${JSON.stringify({ ...createDefinition("broken", "1"), phaseOrder: ["missing"] })}\n`,
  122. "utf8",
  123. );
  124. const store = createNodeWorkflowDefinitionStore({ rootDir: tempRoot });
  125. try {
  126. await expect(store.readDefinition("broken")).rejects.toThrow("unknown phase");
  127. } finally {
  128. await rm(tempRoot, { force: true, recursive: true });
  129. }
  130. });
  131. });
  132. function createDefinition(definitionId: string, definitionVersion: string) {
  133. return {
  134. definitionId,
  135. definitionVersion,
  136. kind: definitionId,
  137. phaseOrder: ["inspect", "complete"],
  138. phases: [
  139. {
  140. behavior: "agent",
  141. description: "Inspect the workspace.",
  142. phase: "inspect",
  143. title: "Inspect",
  144. },
  145. {
  146. behavior: "complete",
  147. description: "Write the report.",
  148. phase: "complete",
  149. title: "Complete",
  150. },
  151. ],
  152. strategy: {
  153. clarify: {
  154. confidenceThreshold: 0.8,
  155. maxRounds: 3,
  156. minRounds: 1,
  157. },
  158. executor: {
  159. drainingChangeHours: 1,
  160. frontierTarget: 3,
  161. maxConcurrentLoops: 2,
  162. maxConsecutiveErrors: 3,
  163. maxPlannerRuns: 10,
  164. },
  165. finalCritic: {
  166. maxIterations: 3,
  167. },
  168. reactLoop: {
  169. maxRounds: 30,
  170. },
  171. },
  172. title: `${definitionId} workflow`,
  173. };
  174. }