workflow-lifecycle.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. import { describe, expect, it } from "vitest";
  2. import type { WorkflowRunSnapshot } from "@zcode/contracts";
  3. import {
  4. applyWorkflowGraphSeed,
  5. applyWorkflowNodePromptUpdates,
  6. cancelWorkflowSnapshot,
  7. reconcileWorkflowSnapshotForResume,
  8. reopenWorkflowGraphNode,
  9. } from "../src/workflow/lifecycle.js";
  10. import { DEFAULT_EXPERT_WORKFLOW_STRATEGY } from "../src/workflow/expert.js";
  11. describe("workflow lifecycle repair", () => {
  12. it("resets stale active workflow state before resume", () => {
  13. const result = reconcileWorkflowSnapshotForResume(createSnapshot(), {
  14. timestamp: "2026-05-06T00:00:00.000Z",
  15. });
  16. expect(result.changed).toBe(true);
  17. expect(result.nodeChanges).toEqual([
  18. { nodeId: "phase:exec", phase: "exec", status: "pending" },
  19. ]);
  20. expect(result.phaseIds).toEqual(["exec"]);
  21. expect(result.activityIds).toEqual(["act-active"]);
  22. expect(result.snapshot.graph.nodes.find((node) => node.id === "phase:exec")).toMatchObject({
  23. error: expect.stringContaining("Reset during workflow resume"),
  24. status: "pending",
  25. });
  26. expect(result.snapshot.phases.find((phase) => phase.phase === "exec")).toMatchObject({
  27. error: expect.stringContaining("Reset during workflow resume"),
  28. status: "pending",
  29. });
  30. expect(
  31. result.snapshot.activities.find((activity) => activity.activityId === "act-active"),
  32. ).toMatchObject({
  33. completedAt: "2026-05-06T00:00:00.000Z",
  34. error: expect.stringContaining("Reset during workflow resume"),
  35. status: "cancelled",
  36. });
  37. });
  38. it("cancels all non-terminal workflow state for debugger accuracy", () => {
  39. const result = cancelWorkflowSnapshot(createSnapshot(), {
  40. timestamp: "2026-05-06T00:00:00.000Z",
  41. });
  42. expect(result.snapshot.status).toBe("cancelled");
  43. expect(result.snapshot.completedAt).toBe("2026-05-06T00:00:00.000Z");
  44. expect(result.snapshot.graph.nodes.map((node) => [node.id, node.status])).toEqual([
  45. ["phase:clarify", "completed"],
  46. ["phase:exec", "cancelled"],
  47. ["phase:final_critic", "cancelled"],
  48. ]);
  49. expect(result.snapshot.phases.map((phase) => [phase.phase, phase.status])).toEqual([
  50. ["clarify", "completed"],
  51. ["exec", "cancelled"],
  52. ["final_critic", "cancelled"],
  53. ]);
  54. expect(
  55. result.snapshot.activities.map((activity) => [activity.activityId, activity.status]),
  56. ).toEqual([
  57. ["act-done", "completed"],
  58. ["act-active", "cancelled"],
  59. ]);
  60. });
  61. it("reopens terminal graph nodes with a bounded attempt counter", () => {
  62. const snapshot = createSnapshot();
  63. const result = reopenWorkflowGraphNode(
  64. {
  65. ...snapshot,
  66. graph: {
  67. ...snapshot.graph,
  68. nodes: snapshot.graph.nodes.map((node) =>
  69. node.id === "phase:clarify"
  70. ? {
  71. ...node,
  72. reopenAttempts: 1,
  73. }
  74. : node,
  75. ),
  76. },
  77. },
  78. {
  79. maxReopens: 2,
  80. nodeId: "phase:clarify",
  81. reason: "regression detected",
  82. timestamp: "2026-05-06T00:00:00.000Z",
  83. },
  84. );
  85. expect(result.changed).toBe(true);
  86. expect(result.nodeChange).toEqual({
  87. nodeId: "phase:clarify",
  88. phase: "clarify",
  89. status: "pending",
  90. });
  91. expect(result.reopenAttempts).toBe(2);
  92. expect(result.snapshot.graph.nodes.find((node) => node.id === "phase:clarify")).toMatchObject({
  93. error: "regression detected",
  94. reopenAttempts: 2,
  95. status: "pending",
  96. });
  97. expect(() =>
  98. reopenWorkflowGraphNode(result.snapshot, {
  99. maxReopens: 2,
  100. nodeId: "phase:clarify",
  101. timestamp: "2026-05-06T00:00:00.000Z",
  102. }),
  103. ).toThrow('Cannot reopen workflow node "phase:clarify": status is "pending"');
  104. });
  105. it("rejects reopen attempts past the configured cap", () => {
  106. const snapshot = createSnapshot();
  107. expect(() =>
  108. reopenWorkflowGraphNode(
  109. {
  110. ...snapshot,
  111. graph: {
  112. ...snapshot.graph,
  113. nodes: snapshot.graph.nodes.map((node) =>
  114. node.id === "phase:clarify"
  115. ? {
  116. ...node,
  117. reopenAttempts: 2,
  118. }
  119. : node,
  120. ),
  121. },
  122. },
  123. {
  124. maxReopens: 2,
  125. nodeId: "phase:clarify",
  126. timestamp: "2026-05-06T00:00:00.000Z",
  127. },
  128. ),
  129. ).toThrow('Workflow node "phase:clarify" already reopened 2x (max=2)');
  130. });
  131. it("applies workflow graph seeds with dependency edges and collections", () => {
  132. const result = applyWorkflowGraphSeed(
  133. createSnapshot(),
  134. {
  135. collections: [
  136. {
  137. collectionId: "implementation",
  138. nodeIds: ["implement_api", "validate_api"],
  139. phase: "exec",
  140. },
  141. ],
  142. edges: [],
  143. nodes: [
  144. {
  145. dependsOn: ["phase:exec"],
  146. id: "implement_api",
  147. phase: "exec",
  148. title: "Implement API",
  149. },
  150. {
  151. collectionId: "implementation",
  152. dependsOn: ["implement_api"],
  153. id: "validate_api",
  154. phase: "exec",
  155. title: "Validate API",
  156. },
  157. ],
  158. },
  159. {
  160. phase: "exec",
  161. timestamp: "2026-05-06T00:00:00.000Z",
  162. },
  163. );
  164. expect(result.changed).toBe(true);
  165. expect(result.addedNodes.map((node) => node.id)).toEqual(["implement_api", "validate_api"]);
  166. expect(result.addedEdges).toEqual([
  167. { from: "phase:exec", to: "implement_api" },
  168. { from: "implement_api", to: "validate_api" },
  169. ]);
  170. expect(result.addedCollections[0]).toMatchObject({
  171. collectionId: "implementation",
  172. nodeIds: ["implement_api", "validate_api"],
  173. phase: "exec",
  174. });
  175. expect(result.snapshot.graph.nodes.find((node) => node.id === "validate_api")).toMatchObject({
  176. kind: "task",
  177. status: "pending",
  178. });
  179. });
  180. it("rejects workflow graph seeds that would create cycles", () => {
  181. expect(() =>
  182. applyWorkflowGraphSeed(
  183. createSnapshot(),
  184. {
  185. edges: [
  186. { from: "phase:exec", to: "cycle_a" },
  187. { from: "cycle_a", to: "phase:exec" },
  188. ],
  189. nodes: [{ id: "cycle_a", title: "Cycle A" }],
  190. },
  191. {
  192. phase: "exec",
  193. timestamp: "2026-05-06T00:00:00.000Z",
  194. },
  195. ),
  196. ).toThrow("would create a cycle");
  197. });
  198. it("applies node prompt updates to existing target phase nodes", () => {
  199. const seeded = applyWorkflowGraphSeed(
  200. createSnapshot(),
  201. {
  202. nodes: [
  203. {
  204. dependsOn: ["phase:exec"],
  205. id: "implement_api",
  206. phase: "exec",
  207. title: "Implement API",
  208. },
  209. ],
  210. },
  211. {
  212. phase: "exec",
  213. timestamp: "2026-05-06T00:00:00.000Z",
  214. },
  215. ).snapshot;
  216. const result = applyWorkflowNodePromptUpdates(
  217. seeded,
  218. [
  219. {
  220. description: "Implement only the API boundary.",
  221. id: "implement_api",
  222. prompt: "Keep tests focused on the exported API behavior.",
  223. title: "Implement API boundary",
  224. },
  225. ],
  226. {
  227. phase: "exec",
  228. timestamp: "2026-05-06T00:01:00.000Z",
  229. },
  230. );
  231. expect(result.changed).toBe(true);
  232. expect(result.updatedNodes.map((node) => node.id)).toEqual(["implement_api"]);
  233. expect(result.snapshot.graph.nodes.find((node) => node.id === "implement_api")).toMatchObject({
  234. description: "Implement only the API boundary.",
  235. prompt: "Keep tests focused on the exported API behavior.",
  236. title: "Implement API boundary",
  237. });
  238. });
  239. it("rejects node prompt updates outside the target phase", () => {
  240. expect(() =>
  241. applyWorkflowNodePromptUpdates(
  242. createSnapshot(),
  243. [
  244. {
  245. id: "phase:clarify",
  246. prompt: "should not mutate clarify from exec",
  247. },
  248. ],
  249. {
  250. phase: "exec",
  251. timestamp: "2026-05-06T00:01:00.000Z",
  252. },
  253. ),
  254. ).toThrow('targets phase "exec" but node belongs to "clarify"');
  255. });
  256. });
  257. function createSnapshot(): WorkflowRunSnapshot {
  258. return {
  259. activities: [
  260. {
  261. activityId: "act-done",
  262. inputArtifactPaths: [],
  263. kind: "agent_session",
  264. nodeId: "phase:clarify",
  265. outputArtifactPaths: ["artifacts/01-clarify.md"],
  266. phase: "clarify",
  267. startedAt: "2026-05-05T00:00:00.000Z",
  268. status: "completed",
  269. },
  270. {
  271. activityId: "act-active",
  272. inputArtifactPaths: ["artifacts/01-clarify.md"],
  273. kind: "agent_session",
  274. nodeId: "phase:exec",
  275. outputArtifactPaths: [],
  276. phase: "exec",
  277. startedAt: "2026-05-05T00:01:00.000Z",
  278. status: "active",
  279. },
  280. ],
  281. artifacts: [],
  282. createdAt: "2026-05-05T00:00:00.000Z",
  283. currentPhase: "exec",
  284. cwd: "/repo",
  285. graph: {
  286. edges: [
  287. { from: "phase:clarify", to: "phase:exec" },
  288. { from: "phase:exec", to: "phase:final_critic" },
  289. ],
  290. nodes: [
  291. {
  292. dependsOn: [],
  293. id: "phase:clarify",
  294. kind: "phase",
  295. phase: "clarify",
  296. status: "completed",
  297. title: "Clarify",
  298. },
  299. {
  300. dependsOn: ["phase:clarify"],
  301. id: "phase:exec",
  302. kind: "phase",
  303. phase: "exec",
  304. status: "active",
  305. title: "Execute",
  306. },
  307. {
  308. dependsOn: ["phase:exec"],
  309. id: "phase:final_critic",
  310. kind: "phase",
  311. phase: "final_critic",
  312. status: "pending",
  313. title: "Final Critic",
  314. },
  315. ],
  316. },
  317. kind: "expert",
  318. phaseOrder: ["clarify", "exec", "final_critic"],
  319. phases: [
  320. { phase: "clarify", status: "completed" },
  321. {
  322. activityId: "act-active",
  323. phase: "exec",
  324. startedAt: "2026-05-05T00:01:00.000Z",
  325. status: "active",
  326. },
  327. { phase: "final_critic", status: "pending" },
  328. ],
  329. runId: "wf_lifecycle",
  330. schemaVersion: 1,
  331. status: "running",
  332. strategy: DEFAULT_EXPERT_WORKFLOW_STRATEGY,
  333. task: "ship lifecycle repair",
  334. updatedAt: "2026-05-05T00:01:00.000Z",
  335. };
  336. }