@united-workforce/cli 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/README.md +30 -3
  2. package/dist/.build-fingerprint +1 -0
  3. package/dist/__tests__/adapter-json-roundtrip.test.js +16 -6
  4. package/dist/__tests__/adapter-json-roundtrip.test.js.map +1 -1
  5. package/dist/__tests__/concurrency.test.d.ts +2 -0
  6. package/dist/__tests__/concurrency.test.d.ts.map +1 -0
  7. package/dist/__tests__/concurrency.test.js +196 -0
  8. package/dist/__tests__/concurrency.test.js.map +1 -0
  9. package/dist/__tests__/config-text-renderer.test.d.ts +2 -0
  10. package/dist/__tests__/config-text-renderer.test.d.ts.map +1 -0
  11. package/dist/__tests__/config-text-renderer.test.js +137 -0
  12. package/dist/__tests__/config-text-renderer.test.js.map +1 -0
  13. package/dist/__tests__/e2e-mock-agent.test.js +23 -7
  14. package/dist/__tests__/e2e-mock-agent.test.js.map +1 -1
  15. package/dist/__tests__/format-text-default.test.d.ts +2 -0
  16. package/dist/__tests__/format-text-default.test.d.ts.map +1 -0
  17. package/dist/__tests__/format-text-default.test.js +43 -0
  18. package/dist/__tests__/format-text-default.test.js.map +1 -0
  19. package/dist/__tests__/format-text-registry.test.d.ts +2 -0
  20. package/dist/__tests__/format-text-registry.test.d.ts.map +1 -0
  21. package/dist/__tests__/format-text-registry.test.js +158 -0
  22. package/dist/__tests__/format-text-registry.test.js.map +1 -0
  23. package/dist/__tests__/issue-180-workflow-ref-removed.test.js +1 -1
  24. package/dist/__tests__/log-text-renderer.test.d.ts +2 -0
  25. package/dist/__tests__/log-text-renderer.test.d.ts.map +1 -0
  26. package/dist/__tests__/log-text-renderer.test.js +265 -0
  27. package/dist/__tests__/log-text-renderer.test.js.map +1 -0
  28. package/dist/__tests__/output-mapper-thread-list-startedat.test.d.ts +2 -0
  29. package/dist/__tests__/output-mapper-thread-list-startedat.test.d.ts.map +1 -0
  30. package/dist/__tests__/output-mapper-thread-list-startedat.test.js +102 -0
  31. package/dist/__tests__/output-mapper-thread-list-startedat.test.js.map +1 -0
  32. package/dist/__tests__/output-mapper-workflow-add.test.d.ts +2 -0
  33. package/dist/__tests__/output-mapper-workflow-add.test.d.ts.map +1 -0
  34. package/dist/__tests__/output-mapper-workflow-add.test.js +22 -0
  35. package/dist/__tests__/output-mapper-workflow-add.test.js.map +1 -0
  36. package/dist/__tests__/pid-recycling.test.js +9 -7
  37. package/dist/__tests__/pid-recycling.test.js.map +1 -1
  38. package/dist/__tests__/prompt.test.js +46 -4
  39. package/dist/__tests__/prompt.test.js.map +1 -1
  40. package/dist/__tests__/resolve-head-hash.test.js +8 -0
  41. package/dist/__tests__/resolve-head-hash.test.js.map +1 -1
  42. package/dist/__tests__/solve-issue-tea-worktree.test.js +3 -1
  43. package/dist/__tests__/solve-issue-tea-worktree.test.js.map +1 -1
  44. package/dist/__tests__/step-ask.test.js +9 -1
  45. package/dist/__tests__/step-ask.test.js.map +1 -1
  46. package/dist/__tests__/store-unified-threads.test.js +19 -17
  47. package/dist/__tests__/store-unified-threads.test.js.map +1 -1
  48. package/dist/__tests__/thread-agent-failure-suspended.test.d.ts +2 -0
  49. package/dist/__tests__/thread-agent-failure-suspended.test.d.ts.map +1 -0
  50. package/dist/__tests__/thread-agent-failure-suspended.test.js +332 -0
  51. package/dist/__tests__/thread-agent-failure-suspended.test.js.map +1 -0
  52. package/dist/__tests__/thread-cancel-status.test.js +19 -13
  53. package/dist/__tests__/thread-cancel-status.test.js.map +1 -1
  54. package/dist/__tests__/thread-cancel-text-renderer.test.d.ts +2 -0
  55. package/dist/__tests__/thread-cancel-text-renderer.test.d.ts.map +1 -0
  56. package/dist/__tests__/thread-cancel-text-renderer.test.js +110 -0
  57. package/dist/__tests__/thread-cancel-text-renderer.test.js.map +1 -0
  58. package/dist/__tests__/thread-join.test.d.ts +2 -0
  59. package/dist/__tests__/thread-join.test.d.ts.map +1 -0
  60. package/dist/__tests__/thread-join.test.js +77 -0
  61. package/dist/__tests__/thread-join.test.js.map +1 -0
  62. package/dist/__tests__/thread-list-filters.test.js +10 -8
  63. package/dist/__tests__/thread-list-filters.test.js.map +1 -1
  64. package/dist/__tests__/thread-list-template-ms-date.test.d.ts +2 -0
  65. package/dist/__tests__/thread-list-template-ms-date.test.d.ts.map +1 -0
  66. package/dist/__tests__/thread-list-template-ms-date.test.js +102 -0
  67. package/dist/__tests__/thread-list-template-ms-date.test.js.map +1 -0
  68. package/dist/__tests__/thread-list-workflow-corrupt.test.d.ts +2 -0
  69. package/dist/__tests__/thread-list-workflow-corrupt.test.d.ts.map +1 -0
  70. package/dist/__tests__/thread-list-workflow-corrupt.test.js +157 -0
  71. package/dist/__tests__/thread-list-workflow-corrupt.test.js.map +1 -0
  72. package/dist/__tests__/thread-poke.test.js +15 -2
  73. package/dist/__tests__/thread-poke.test.js.map +1 -1
  74. package/dist/__tests__/thread-read-xml-tags.test.js +10 -9
  75. package/dist/__tests__/thread-read-xml-tags.test.js.map +1 -1
  76. package/dist/__tests__/thread-resume.test.js +11 -1
  77. package/dist/__tests__/thread-resume.test.js.map +1 -1
  78. package/dist/__tests__/thread-start-cwd-cli.test.js +15 -3
  79. package/dist/__tests__/thread-start-cwd-cli.test.js.map +1 -1
  80. package/dist/__tests__/thread-stop-text-renderer.test.d.ts +2 -0
  81. package/dist/__tests__/thread-stop-text-renderer.test.d.ts.map +1 -0
  82. package/dist/__tests__/thread-stop-text-renderer.test.js +148 -0
  83. package/dist/__tests__/thread-stop-text-renderer.test.js.map +1 -0
  84. package/dist/__tests__/thread-suspend-step.test.js +5 -2
  85. package/dist/__tests__/thread-suspend-step.test.js.map +1 -1
  86. package/dist/__tests__/thread-test-helpers.d.ts +7 -0
  87. package/dist/__tests__/thread-test-helpers.d.ts.map +1 -1
  88. package/dist/__tests__/thread-test-helpers.js +13 -0
  89. package/dist/__tests__/thread-test-helpers.js.map +1 -1
  90. package/dist/__tests__/thread.test.js +11 -9
  91. package/dist/__tests__/thread.test.js.map +1 -1
  92. package/dist/__tests__/validate-semantic.test.js +56 -2
  93. package/dist/__tests__/validate-semantic.test.js.map +1 -1
  94. package/dist/__tests__/workflow-list-recursive.test.js +10 -7
  95. package/dist/__tests__/workflow-list-recursive.test.js.map +1 -1
  96. package/dist/__tests__/workflow-paths.test.d.ts +2 -0
  97. package/dist/__tests__/workflow-paths.test.d.ts.map +1 -0
  98. package/dist/__tests__/workflow-paths.test.js +261 -0
  99. package/dist/__tests__/workflow-paths.test.js.map +1 -0
  100. package/dist/__tests__/workflow-resolution.test.js +10 -7
  101. package/dist/__tests__/workflow-resolution.test.js.map +1 -1
  102. package/dist/__tests__/workflow-show-resolution.test.js +10 -7
  103. package/dist/__tests__/workflow-show-resolution.test.js.map +1 -1
  104. package/dist/__tests__/workflow-validate.test.js +75 -55
  105. package/dist/__tests__/workflow-validate.test.js.map +1 -1
  106. package/dist/__tests__/write-envelope.test.d.ts +2 -0
  107. package/dist/__tests__/write-envelope.test.d.ts.map +1 -0
  108. package/dist/__tests__/write-envelope.test.js +201 -0
  109. package/dist/__tests__/write-envelope.test.js.map +1 -0
  110. package/dist/cli.js +76 -36
  111. package/dist/cli.js.map +1 -1
  112. package/dist/commands/config.d.ts +5 -0
  113. package/dist/commands/config.d.ts.map +1 -1
  114. package/dist/commands/config.js +81 -3
  115. package/dist/commands/config.js.map +1 -1
  116. package/dist/commands/prompt.d.ts.map +1 -1
  117. package/dist/commands/prompt.js +42 -29
  118. package/dist/commands/prompt.js.map +1 -1
  119. package/dist/commands/setup.d.ts +9 -4
  120. package/dist/commands/setup.d.ts.map +1 -1
  121. package/dist/commands/setup.js +51 -7
  122. package/dist/commands/setup.js.map +1 -1
  123. package/dist/commands/thread.d.ts +12 -0
  124. package/dist/commands/thread.d.ts.map +1 -1
  125. package/dist/commands/thread.js +226 -9
  126. package/dist/commands/thread.js.map +1 -1
  127. package/dist/commands/workflow.d.ts +2 -2
  128. package/dist/commands/workflow.d.ts.map +1 -1
  129. package/dist/commands/workflow.js +26 -10
  130. package/dist/commands/workflow.js.map +1 -1
  131. package/dist/concurrency/concurrency.d.ts +34 -0
  132. package/dist/concurrency/concurrency.d.ts.map +1 -0
  133. package/dist/concurrency/concurrency.js +216 -0
  134. package/dist/concurrency/concurrency.js.map +1 -0
  135. package/dist/concurrency/index.d.ts +3 -0
  136. package/dist/concurrency/index.d.ts.map +1 -0
  137. package/dist/concurrency/index.js +2 -0
  138. package/dist/concurrency/index.js.map +1 -0
  139. package/dist/concurrency/types.d.ts +19 -0
  140. package/dist/concurrency/types.d.ts.map +1 -0
  141. package/dist/concurrency/types.js +2 -0
  142. package/dist/concurrency/types.js.map +1 -0
  143. package/dist/format.d.ts +69 -2
  144. package/dist/format.d.ts.map +1 -1
  145. package/dist/format.js +198 -1
  146. package/dist/format.js.map +1 -1
  147. package/dist/output-mappers.d.ts +122 -0
  148. package/dist/output-mappers.d.ts.map +1 -0
  149. package/dist/output-mappers.js +134 -0
  150. package/dist/output-mappers.js.map +1 -0
  151. package/dist/schemas.d.ts +4 -1
  152. package/dist/schemas.d.ts.map +1 -1
  153. package/dist/schemas.js +31 -4
  154. package/dist/schemas.js.map +1 -1
  155. package/dist/store.d.ts +11 -0
  156. package/dist/store.d.ts.map +1 -1
  157. package/dist/store.js +20 -1
  158. package/dist/store.js.map +1 -1
  159. package/dist/text-renderers.d.ts +30 -0
  160. package/dist/text-renderers.d.ts.map +1 -0
  161. package/dist/text-renderers.js +251 -0
  162. package/dist/text-renderers.js.map +1 -0
  163. package/dist/validate-semantic.d.ts.map +1 -1
  164. package/dist/validate-semantic.js +28 -11
  165. package/dist/validate-semantic.js.map +1 -1
  166. package/examples/brainstorm.yaml +130 -0
  167. package/examples/debate.yaml +169 -0
  168. package/examples/socratic-questioning.yaml +112 -0
  169. package/package.json +12 -11
  170. package/src/__tests__/adapter-json-roundtrip.test.ts +15 -6
  171. package/src/__tests__/concurrency.test.ts +266 -0
  172. package/src/__tests__/config-text-renderer.test.ts +156 -0
  173. package/src/__tests__/e2e-mock-agent.test.ts +45 -7
  174. package/src/__tests__/format-text-default.test.ts +49 -0
  175. package/src/__tests__/format-text-registry.test.ts +173 -0
  176. package/src/__tests__/issue-180-workflow-ref-removed.test.ts +1 -1
  177. package/src/__tests__/log-text-renderer.test.ts +294 -0
  178. package/src/__tests__/output-mapper-thread-list-startedat.test.ts +124 -0
  179. package/src/__tests__/output-mapper-workflow-add.test.ts +24 -0
  180. package/src/__tests__/pid-recycling.test.ts +9 -8
  181. package/src/__tests__/prompt.test.ts +48 -4
  182. package/src/__tests__/resolve-head-hash.test.ts +7 -0
  183. package/src/__tests__/solve-issue-tea-worktree.test.ts +3 -1
  184. package/src/__tests__/step-ask.test.ts +8 -1
  185. package/src/__tests__/store-unified-threads.test.ts +21 -18
  186. package/src/__tests__/thread-agent-failure-suspended.test.ts +406 -0
  187. package/src/__tests__/thread-cancel-status.test.ts +21 -14
  188. package/src/__tests__/thread-cancel-text-renderer.test.ts +125 -0
  189. package/src/__tests__/thread-join.test.ts +103 -0
  190. package/src/__tests__/thread-list-filters.test.ts +9 -9
  191. package/src/__tests__/thread-list-template-ms-date.test.ts +110 -0
  192. package/src/__tests__/thread-list-workflow-corrupt.test.ts +198 -0
  193. package/src/__tests__/thread-poke.test.ts +14 -2
  194. package/src/__tests__/thread-read-xml-tags.test.ts +9 -11
  195. package/src/__tests__/thread-resume.test.ts +10 -1
  196. package/src/__tests__/thread-start-cwd-cli.test.ts +15 -3
  197. package/src/__tests__/thread-stop-text-renderer.test.ts +168 -0
  198. package/src/__tests__/thread-suspend-step.test.ts +5 -2
  199. package/src/__tests__/thread-test-helpers.ts +15 -1
  200. package/src/__tests__/thread.test.ts +10 -10
  201. package/src/__tests__/validate-semantic.test.ts +59 -2
  202. package/src/__tests__/workflow-list-recursive.test.ts +9 -9
  203. package/src/__tests__/workflow-paths.test.ts +337 -0
  204. package/src/__tests__/workflow-resolution.test.ts +9 -8
  205. package/src/__tests__/workflow-show-resolution.test.ts +9 -8
  206. package/src/__tests__/workflow-validate.test.ts +78 -56
  207. package/src/__tests__/write-envelope.test.ts +257 -0
  208. package/src/cli.ts +111 -35
  209. package/src/commands/config.ts +85 -3
  210. package/src/commands/prompt.ts +42 -29
  211. package/src/commands/setup.ts +57 -7
  212. package/src/commands/thread.ts +280 -9
  213. package/src/commands/workflow.ts +32 -11
  214. package/src/concurrency/concurrency.ts +245 -0
  215. package/src/concurrency/index.ts +10 -0
  216. package/src/concurrency/types.ts +19 -0
  217. package/src/format.ts +282 -2
  218. package/src/output-mappers.ts +255 -0
  219. package/src/schemas.ts +39 -3
  220. package/src/store.ts +25 -1
  221. package/src/text-renderers.ts +355 -0
  222. package/src/validate-semantic.ts +33 -12
  223. package/LICENSE +0 -21
@@ -1,23 +1,17 @@
1
- import { mkdir, mkdtemp } from "node:fs/promises";
1
+ import { mkdtemp, rm } from "node:fs/promises";
2
2
  import { tmpdir } from "node:os";
3
3
  import { join } from "node:path";
4
4
  import type { CasRef, ThreadId } from "@united-workforce/protocol";
5
- import { describe, expect, test } from "vitest";
5
+ import { afterEach, beforeEach, describe, expect, test } from "vitest";
6
6
  import {
7
7
  completeThread,
8
- createUwfStore,
8
+ type createUwfStore,
9
9
  getThread,
10
10
  loadActiveThreads,
11
11
  loadHistoryThreads,
12
12
  setThread,
13
13
  } from "../store.js";
14
-
15
- async function makeUwfStore(storageRoot: string) {
16
- const casDir = join(storageRoot, "cas");
17
- await mkdir(casDir, { recursive: true });
18
- process.env.OCAS_HOME = casDir;
19
- return createUwfStore(storageRoot);
20
- }
14
+ import { makeUwfStore } from "./thread-test-helpers.js";
21
15
 
22
16
  async function seedThreadHead(
23
17
  uwf: Awaited<ReturnType<typeof createUwfStore>>,
@@ -26,9 +20,25 @@ async function seedThreadHead(
26
20
  return (await uwf.store.cas.put(uwf.schemas.text, label)) as CasRef;
27
21
  }
28
22
 
23
+ let tmpDir: string;
24
+ let savedOcasHome: string | undefined;
25
+
26
+ beforeEach(async () => {
27
+ savedOcasHome = process.env.OCAS_HOME;
28
+ tmpDir = await mkdtemp(join(tmpdir(), "uwf-store-test-"));
29
+ });
30
+
31
+ afterEach(async () => {
32
+ if (savedOcasHome === undefined) {
33
+ delete process.env.OCAS_HOME;
34
+ } else {
35
+ process.env.OCAS_HOME = savedOcasHome;
36
+ }
37
+ await rm(tmpDir, { recursive: true, force: true });
38
+ });
39
+
29
40
  describe("unified thread storage", () => {
30
41
  test("loadActiveThreads excludes completed threads", async () => {
31
- const tmpDir = await mkdtemp(join(tmpdir(), "uwf-active-test-"));
32
42
  const uwf = await makeUwfStore(tmpDir);
33
43
 
34
44
  const threadId1 = "01JTEST000000000000ACTIVE1" as ThreadId;
@@ -59,7 +69,6 @@ describe("unified thread storage", () => {
59
69
  });
60
70
 
61
71
  test("loadActiveThreads excludes cancelled threads", async () => {
62
- const tmpDir = await mkdtemp(join(tmpdir(), "uwf-active-test-"));
63
72
  const uwf = await makeUwfStore(tmpDir);
64
73
 
65
74
  const threadId1 = "01JTEST000000000000ACTIVE3" as ThreadId;
@@ -90,7 +99,6 @@ describe("unified thread storage", () => {
90
99
  });
91
100
 
92
101
  test("loadHistoryThreads only returns completed and cancelled", async () => {
93
- const tmpDir = await mkdtemp(join(tmpdir(), "uwf-history-test-"));
94
102
  const uwf = await makeUwfStore(tmpDir);
95
103
 
96
104
  const threadId1 = "01JTEST000000000000HISTOR1" as ThreadId;
@@ -132,7 +140,6 @@ describe("unified thread storage", () => {
132
140
  });
133
141
 
134
142
  test("completeThread marks thread as completed", async () => {
135
- const tmpDir = await mkdtemp(join(tmpdir(), "uwf-complete-test-"));
136
143
  const uwf = await makeUwfStore(tmpDir);
137
144
  const threadId = "01JTEST000000000000COMPLE1" as ThreadId;
138
145
  const head = await seedThreadHead(uwf, "active-head");
@@ -155,7 +162,6 @@ describe("unified thread storage", () => {
155
162
  });
156
163
 
157
164
  test("completeThread marks thread as cancelled", async () => {
158
- const tmpDir = await mkdtemp(join(tmpdir(), "uwf-complete-test-"));
159
165
  const uwf = await makeUwfStore(tmpDir);
160
166
  const threadId = "01JTEST000000000000COMPLE2" as ThreadId;
161
167
  const head = await seedThreadHead(uwf, "active-head");
@@ -178,7 +184,6 @@ describe("unified thread storage", () => {
178
184
  });
179
185
 
180
186
  test("completeThread clears suspend metadata", async () => {
181
- const tmpDir = await mkdtemp(join(tmpdir(), "uwf-complete-test-"));
182
187
  const uwf = await makeUwfStore(tmpDir);
183
188
  const threadId = "01JTEST000000000000COMPLE3" as ThreadId;
184
189
  const head = await seedThreadHead(uwf, "suspended-head");
@@ -201,7 +206,6 @@ describe("unified thread storage", () => {
201
206
  });
202
207
 
203
208
  test("completeThread handles non-existent thread gracefully", async () => {
204
- const tmpDir = await mkdtemp(join(tmpdir(), "uwf-complete-test-"));
205
209
  const uwf = await makeUwfStore(tmpDir);
206
210
  const threadId = "01JTEST000000000000NOEXIST" as ThreadId;
207
211
 
@@ -213,7 +217,6 @@ describe("unified thread storage", () => {
213
217
  });
214
218
 
215
219
  test("status and completedAt tags are persisted and loaded", async () => {
216
- const tmpDir = await mkdtemp(join(tmpdir(), "uwf-tags-test-"));
217
220
  const uwf = await makeUwfStore(tmpDir);
218
221
  const threadId = "01JTEST000000000000TAGTEST" as ThreadId;
219
222
  const head = await seedThreadHead(uwf, "test-head");
@@ -0,0 +1,406 @@
1
+ import { execFileSync } from "node:child_process";
2
+ import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { dirname, join } from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ import { putSchema } from "@ocas/core";
7
+ import { openStore } from "@ocas/fs";
8
+ import type { CasRef, ThreadId } from "@united-workforce/protocol";
9
+ import { afterEach, beforeEach, describe, expect, test } from "vitest";
10
+ import { registerUwfSchemas } from "../schemas.js";
11
+ import { seedThreads } from "./thread-test-helpers.js";
12
+
13
+ const OUTPUT_SCHEMA = {
14
+ type: "object" as const,
15
+ properties: {
16
+ $status: { type: "string" as const },
17
+ note: { type: "string" as const },
18
+ },
19
+ required: ["$status"],
20
+ additionalProperties: false,
21
+ };
22
+
23
+ const THREAD_ID = "01AGENTFAILSUSPEND00000" as ThreadId;
24
+
25
+ let tmpDir: string;
26
+ let savedOcasHome: string | undefined;
27
+
28
+ beforeEach(async () => {
29
+ savedOcasHome = process.env.OCAS_HOME;
30
+ tmpDir = await mkdtemp(join(tmpdir(), "cli-uwf-agent-fail-suspend-"));
31
+ });
32
+
33
+ afterEach(async () => {
34
+ if (savedOcasHome === undefined) {
35
+ delete process.env.OCAS_HOME;
36
+ } else {
37
+ process.env.OCAS_HOME = savedOcasHome;
38
+ }
39
+ await rm(tmpDir, { recursive: true, force: true });
40
+ });
41
+
42
+ type SetupResult = {
43
+ casDir: string;
44
+ startHash: CasRef;
45
+ workflowHash: CasRef;
46
+ mockAgentPath: string;
47
+ failingAgentPath: string;
48
+ recoverableFailAgentPath: string;
49
+ };
50
+
51
+ async function setupThread(): Promise<SetupResult> {
52
+ const casDir = join(tmpDir, "cas");
53
+ await mkdir(casDir, { recursive: true });
54
+
55
+ const store = await openStore(casDir);
56
+ const schemas = await registerUwfSchemas(store);
57
+ const outputSchemaHash = await putSchema(store, OUTPUT_SCHEMA);
58
+
59
+ const workflowHash = await store.cas.put(schemas.workflow, {
60
+ name: "test-agent-fail",
61
+ description: "agent failure suspend test",
62
+ roles: {
63
+ worker: {
64
+ description: "Worker role",
65
+ goal: "Work",
66
+ capabilities: [],
67
+ procedure: "work",
68
+ output: "result",
69
+ frontmatter: outputSchemaHash,
70
+ },
71
+ reviewer: {
72
+ description: "Reviewer role",
73
+ goal: "Review",
74
+ capabilities: [],
75
+ procedure: "review",
76
+ output: "result",
77
+ frontmatter: outputSchemaHash,
78
+ },
79
+ },
80
+ graph: {
81
+ $START: {
82
+ new: { role: "worker", prompt: "Start work", location: null },
83
+ resume: { role: "worker", prompt: "Resume work", location: null },
84
+ },
85
+ worker: {
86
+ ok: { role: "reviewer", prompt: "Review the work", location: null },
87
+ },
88
+ reviewer: { done: { role: "$END", prompt: "Done", location: null } },
89
+ },
90
+ });
91
+
92
+ const startHash = await store.cas.put(schemas.startNode, {
93
+ workflow: workflowHash,
94
+ prompt: "Test agent failure task",
95
+ cwd: tmpDir,
96
+ });
97
+
98
+ process.env.OCAS_HOME = casDir;
99
+
100
+ await seedThreads(tmpDir, { [THREAD_ID]: startHash });
101
+
102
+ // Build a successful step output to be used by agents
103
+ const newOutputHash = await store.cas.put(outputSchemaHash, {
104
+ $status: "ok",
105
+ note: "success output",
106
+ });
107
+ const newDetailHash = await store.cas.put(schemas.text, "success detail");
108
+ const successStepHash = await store.cas.put(schemas.stepNode, {
109
+ start: startHash,
110
+ prev: null,
111
+ role: "worker",
112
+ output: newOutputHash,
113
+ detail: newDetailHash,
114
+ agent: "mock-agent",
115
+ edgePrompt: "Start work",
116
+ startedAtMs: 1716600000000,
117
+ completedAtMs: 1716600001000,
118
+ cwd: tmpDir,
119
+ assembledPrompt: null,
120
+ usage: null,
121
+ });
122
+
123
+ // Build a failed step output (isError: true) — the agent created the CAS node but reports failure
124
+ const failedOutputHash = await store.cas.put(outputSchemaHash, {
125
+ $status: "error",
126
+ note: "validation failed",
127
+ });
128
+ const failedDetailHash = await store.cas.put(schemas.text, "failed detail");
129
+ const failedStepHash = await store.cas.put(schemas.stepNode, {
130
+ start: startHash,
131
+ prev: null,
132
+ role: "worker",
133
+ output: failedOutputHash,
134
+ detail: failedDetailHash,
135
+ agent: "mock-agent",
136
+ edgePrompt: "Start work",
137
+ startedAtMs: 1716600000000,
138
+ completedAtMs: 1716600001000,
139
+ cwd: tmpDir,
140
+ assembledPrompt: null,
141
+ usage: null,
142
+ });
143
+
144
+ const successAdapterJson = JSON.stringify({
145
+ stepHash: successStepHash,
146
+ detailHash: newDetailHash,
147
+ role: "worker",
148
+ frontmatter: { $status: "ok", note: "success output" },
149
+ body: "",
150
+ startedAtMs: 1716600000000,
151
+ completedAtMs: 1716600001000,
152
+ usage: null,
153
+ });
154
+
155
+ const failedAdapterJson = JSON.stringify({
156
+ stepHash: failedStepHash,
157
+ detailHash: failedDetailHash,
158
+ role: "worker",
159
+ frontmatter: { $status: "error", note: "validation failed" },
160
+ body: "",
161
+ startedAtMs: 1716600000000,
162
+ completedAtMs: 1716600001000,
163
+ usage: null,
164
+ isError: true,
165
+ errorMessage: "frontmatter validation exhausted retries",
166
+ });
167
+
168
+ // Mock agent that succeeds
169
+ const mockAgentPath = join(tmpDir, "mock-agent.sh");
170
+ await writeFile(mockAgentPath, `#!/bin/sh\necho '${successAdapterJson}'\n`, { mode: 0o755 });
171
+
172
+ // Agent that crashes with non-zero exit code (fatal failure)
173
+ const failingAgentPath = join(tmpDir, "failing-agent.sh");
174
+ await writeFile(failingAgentPath, `#!/bin/sh\necho "boom" >&2\nexit 7\n`, { mode: 0o755 });
175
+
176
+ // Agent that returns isError: true (recoverable failure)
177
+ const recoverableFailAgentPath = join(tmpDir, "recoverable-fail-agent.sh");
178
+ await writeFile(recoverableFailAgentPath, `#!/bin/sh\necho '${failedAdapterJson}'\n`, {
179
+ mode: 0o755,
180
+ });
181
+
182
+ const configPath = join(tmpDir, "config.yaml");
183
+ await writeFile(
184
+ configPath,
185
+ `defaultAgent: uwf-hermes\nagentOverrides: null\nagents:\n uwf-hermes:\n command: uwf-hermes\n`,
186
+ );
187
+
188
+ return {
189
+ casDir,
190
+ startHash,
191
+ workflowHash,
192
+ mockAgentPath,
193
+ failingAgentPath,
194
+ recoverableFailAgentPath,
195
+ };
196
+ }
197
+
198
+ function runUwf(
199
+ args: string[],
200
+ casDir: string,
201
+ ): { stdout: string; stderr: string; status: number } {
202
+ const cliPath = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "dist", "cli.js");
203
+ const formatArgs = args.includes("--format") ? args : ["--format", "raw-json", ...args];
204
+ try {
205
+ const stdout = execFileSync(process.execPath, [cliPath, ...formatArgs], {
206
+ encoding: "utf8",
207
+ stdio: ["ignore", "pipe", "pipe"],
208
+ env: {
209
+ ...process.env,
210
+ UWF_HOME: tmpDir,
211
+ OCAS_HOME: casDir,
212
+ },
213
+ cwd: tmpDir,
214
+ timeout: 30000,
215
+ });
216
+ return { stdout, stderr: "", status: 0 };
217
+ } catch (error) {
218
+ const err = error as NodeJS.ErrnoException & {
219
+ stdout?: string | Buffer;
220
+ stderr?: string | Buffer;
221
+ status?: number;
222
+ };
223
+ return {
224
+ stdout: typeof err.stdout === "string" ? err.stdout : (err.stdout?.toString("utf8") ?? ""),
225
+ stderr: typeof err.stderr === "string" ? err.stderr : (err.stderr?.toString("utf8") ?? ""),
226
+ status: err.status ?? 1,
227
+ };
228
+ }
229
+ }
230
+
231
+ // ── Spec 1: Recoverable agent failure (isError: true) → suspended ─────────
232
+
233
+ describe("recoverable agent failure suspends thread", () => {
234
+ test("CLI output has status=suspended when agent returns isError=true", async () => {
235
+ const { casDir, recoverableFailAgentPath } = await setupThread();
236
+ const result = runUwf(
237
+ ["thread", "exec", THREAD_ID, "--agent", recoverableFailAgentPath],
238
+ casDir,
239
+ );
240
+ // exec envelope: { threadId, workflowHash, steps: [...] }
241
+ const envelope = JSON.parse(result.stdout.trim());
242
+ const stepOutput = envelope.steps[0];
243
+ expect(stepOutput.status).toBe("suspended");
244
+ });
245
+
246
+ test("CLI output has suspendedRole set to the failing role", async () => {
247
+ const { casDir, recoverableFailAgentPath } = await setupThread();
248
+ const result = runUwf(
249
+ ["thread", "exec", THREAD_ID, "--agent", recoverableFailAgentPath],
250
+ casDir,
251
+ );
252
+ const envelope = JSON.parse(result.stdout.trim());
253
+ const stepOutput = envelope.steps[0];
254
+ expect(stepOutput.suspendedRole).toBe("worker");
255
+ });
256
+
257
+ test("CLI output has suspendMessage set to the error message", async () => {
258
+ const { casDir, recoverableFailAgentPath } = await setupThread();
259
+ const result = runUwf(
260
+ ["thread", "exec", THREAD_ID, "--agent", recoverableFailAgentPath],
261
+ casDir,
262
+ );
263
+ const envelope = JSON.parse(result.stdout.trim());
264
+ const stepOutput = envelope.steps[0];
265
+ expect(stepOutput.suspendMessage).toBe("frontmatter validation exhausted retries");
266
+ });
267
+
268
+ test("thread head is NOT advanced on recoverable failure", async () => {
269
+ const { casDir, startHash, recoverableFailAgentPath } = await setupThread();
270
+ runUwf(["thread", "exec", THREAD_ID, "--agent", recoverableFailAgentPath], casDir);
271
+ const { createUwfStore, getThread } = await import("../store.js");
272
+ const uwf = await createUwfStore(tmpDir);
273
+ const entry = getThread(uwf.varStore, THREAD_ID);
274
+ // Head should still be the start hash (not advanced)
275
+ expect(entry?.head).toBe(startHash);
276
+ });
277
+
278
+ test("thread index entry is persisted as suspended via markThreadSuspended", async () => {
279
+ const { casDir, recoverableFailAgentPath } = await setupThread();
280
+ runUwf(["thread", "exec", THREAD_ID, "--agent", recoverableFailAgentPath], casDir);
281
+ const { createUwfStore, getThread } = await import("../store.js");
282
+ const uwf = await createUwfStore(tmpDir);
283
+ const entry = getThread(uwf.varStore, THREAD_ID);
284
+ expect(entry?.status).toBe("suspended");
285
+ expect(entry?.suspendedRole).toBe("worker");
286
+ expect(entry?.suspendMessage).toBe("frontmatter validation exhausted retries");
287
+ });
288
+
289
+ test("uwf thread list --status suspended includes the thread", async () => {
290
+ const { casDir, recoverableFailAgentPath } = await setupThread();
291
+ runUwf(["thread", "exec", THREAD_ID, "--agent", recoverableFailAgentPath], casDir);
292
+ const listResult = runUwf(["thread", "list", "--status", "suspended"], casDir);
293
+ expect(listResult.stdout).toContain(THREAD_ID);
294
+ });
295
+
296
+ test("error field is included in StepOutput for backward compatibility", async () => {
297
+ const { casDir, recoverableFailAgentPath } = await setupThread();
298
+ const result = runUwf(
299
+ ["thread", "exec", THREAD_ID, "--agent", recoverableFailAgentPath],
300
+ casDir,
301
+ );
302
+ const envelope = JSON.parse(result.stdout.trim());
303
+ const stepOutput = envelope.steps[0];
304
+ // The exec envelope includes status=suspended with suspend fields;
305
+ // the internal StepOutput also carries error { stepHash, message } but
306
+ // toThreadExecPayload only maps status/suspendedRole/suspendMessage.
307
+ // Verify the mapped fields are correct.
308
+ expect(stepOutput.status).toBe("suspended");
309
+ expect(stepOutput.suspendedRole).toBe("worker");
310
+ expect(stepOutput.suspendMessage).toBe("frontmatter validation exhausted retries");
311
+ });
312
+ });
313
+
314
+ // ── Spec 2: Fatal agent failure (command crash) → suspended ───────────────
315
+
316
+ describe("fatal agent failure suspends thread", () => {
317
+ test("thread status is suspended after agent crash", async () => {
318
+ const { casDir, failingAgentPath } = await setupThread();
319
+ runUwf(["thread", "exec", THREAD_ID, "--agent", failingAgentPath], casDir);
320
+ const { createUwfStore, getThread } = await import("../store.js");
321
+ const uwf = await createUwfStore(tmpDir);
322
+ const entry = getThread(uwf.varStore, THREAD_ID);
323
+ expect(entry?.status).toBe("suspended");
324
+ });
325
+
326
+ test("thread index has suspendedRole and suspendMessage after fatal failure", async () => {
327
+ const { casDir, failingAgentPath } = await setupThread();
328
+ runUwf(["thread", "exec", THREAD_ID, "--agent", failingAgentPath], casDir);
329
+ const { createUwfStore, getThread } = await import("../store.js");
330
+ const uwf = await createUwfStore(tmpDir);
331
+ const entry = getThread(uwf.varStore, THREAD_ID);
332
+ expect(entry?.suspendedRole).toBe("worker");
333
+ expect(entry?.suspendMessage).toContain("agent command failed");
334
+ });
335
+
336
+ test("thread head is NOT advanced after fatal failure", async () => {
337
+ const { casDir, startHash, failingAgentPath } = await setupThread();
338
+ runUwf(["thread", "exec", THREAD_ID, "--agent", failingAgentPath], casDir);
339
+ const { createUwfStore, getThread } = await import("../store.js");
340
+ const uwf = await createUwfStore(tmpDir);
341
+ const entry = getThread(uwf.varStore, THREAD_ID);
342
+ expect(entry?.head).toBe(startHash);
343
+ });
344
+
345
+ test("uwf thread list --status suspended includes thread after crash", async () => {
346
+ const { casDir, failingAgentPath } = await setupThread();
347
+ runUwf(["thread", "exec", THREAD_ID, "--agent", failingAgentPath], casDir);
348
+ const listResult = runUwf(["thread", "list", "--status", "suspended"], casDir);
349
+ expect(listResult.stdout).toContain(THREAD_ID);
350
+ });
351
+
352
+ test("CLI process exits with non-zero exit code after fatal failure", async () => {
353
+ const { casDir, failingAgentPath } = await setupThread();
354
+ const result = runUwf(["thread", "exec", THREAD_ID, "--agent", failingAgentPath], casDir);
355
+ expect(result.status).not.toBe(0);
356
+ });
357
+ });
358
+
359
+ // ── Spec 3: Suspended thread from agent failure can be resumed ────────────
360
+
361
+ describe("agent-failure-suspended thread can be resumed", () => {
362
+ test("thread resume is accepted for agent-failure suspended thread", async () => {
363
+ const { casDir, recoverableFailAgentPath, mockAgentPath } = await setupThread();
364
+ // First: cause a recoverable failure → thread becomes suspended
365
+ runUwf(["thread", "exec", THREAD_ID, "--agent", recoverableFailAgentPath], casDir);
366
+ // Verify it's suspended
367
+ const { createUwfStore, getThread } = await import("../store.js");
368
+ const uwf = await createUwfStore(tmpDir);
369
+ const entry = getThread(uwf.varStore, THREAD_ID);
370
+ expect(entry?.status).toBe("suspended");
371
+
372
+ // Resume with a different (successful) agent
373
+ const resumeResult = runUwf(
374
+ [
375
+ "thread",
376
+ "resume",
377
+ THREAD_ID,
378
+ "-p",
379
+ "try again with correct params",
380
+ "--agent",
381
+ mockAgentPath,
382
+ ],
383
+ casDir,
384
+ );
385
+ expect(resumeResult.status).toBe(0);
386
+ const resumeOutput = JSON.parse(resumeResult.stdout.trim());
387
+ // After successful resume, thread should not be suspended
388
+ expect(resumeOutput.status).not.toBe("suspended");
389
+ });
390
+
391
+ test("re-failure after resume returns to suspended (not idle)", async () => {
392
+ const { casDir, recoverableFailAgentPath } = await setupThread();
393
+ // First: cause a recoverable failure → suspended
394
+ runUwf(["thread", "exec", THREAD_ID, "--agent", recoverableFailAgentPath], casDir);
395
+ // Resume with same failing agent → should suspend again
396
+ const resumeResult = runUwf(
397
+ ["thread", "resume", THREAD_ID, "-p", "try again", "--agent", recoverableFailAgentPath],
398
+ casDir,
399
+ );
400
+ // Resume with recoverable failure agent — the resume itself runs cmdThreadStepOnce
401
+ // which should report suspended status
402
+ const resumeOutput = JSON.parse(resumeResult.stdout.trim());
403
+ expect(resumeOutput.status).toBe("suspended");
404
+ expect(resumeOutput.suspendedRole).toBe("worker");
405
+ });
406
+ });
@@ -1,22 +1,16 @@
1
- import { mkdir, mkdtemp } from "node:fs/promises";
1
+ import { mkdtemp, rm } from "node:fs/promises";
2
2
  import { tmpdir } from "node:os";
3
3
  import { join } from "node:path";
4
4
  import type { CasRef, ThreadId } from "@united-workforce/protocol";
5
- import { describe, expect, test } from "vitest";
5
+ import { afterEach, beforeEach, describe, expect, test } from "vitest";
6
6
  import {
7
7
  completeThread,
8
- createUwfStore,
8
+ type createUwfStore,
9
9
  getThread,
10
10
  loadHistoryThreads,
11
11
  setThread,
12
12
  } from "../store.js";
13
-
14
- async function makeUwfStore(storageRoot: string) {
15
- const casDir = join(storageRoot, "cas");
16
- await mkdir(casDir, { recursive: true });
17
- process.env.OCAS_HOME = casDir;
18
- return createUwfStore(storageRoot);
19
- }
13
+ import { makeUwfStore } from "./thread-test-helpers.js";
20
14
 
21
15
  async function seedHistoryHead(
22
16
  uwf: Awaited<ReturnType<typeof createUwfStore>>,
@@ -25,9 +19,25 @@ async function seedHistoryHead(
25
19
  return (await uwf.store.cas.put(uwf.schemas.text, label)) as CasRef;
26
20
  }
27
21
 
22
+ let tmpDir: string;
23
+ let savedOcasHome: string | undefined;
24
+
25
+ beforeEach(async () => {
26
+ savedOcasHome = process.env.OCAS_HOME;
27
+ tmpDir = await mkdtemp(join(tmpdir(), "uwf-cancel-test-"));
28
+ });
29
+
30
+ afterEach(async () => {
31
+ if (savedOcasHome === undefined) {
32
+ delete process.env.OCAS_HOME;
33
+ } else {
34
+ process.env.OCAS_HOME = savedOcasHome;
35
+ }
36
+ await rm(tmpDir, { recursive: true, force: true });
37
+ });
38
+
28
39
  describe("thread cancel status", () => {
29
40
  test("cancelled thread has status 'cancelled'", async () => {
30
- const tmpDir = await mkdtemp(join(tmpdir(), "uwf-cancel-test-"));
31
41
  const threadId = "01JTEST000000000000CANCEL1" as ThreadId;
32
42
  const uwf = await makeUwfStore(tmpDir);
33
43
  const head = await seedHistoryHead(uwf, "cancelled-head");
@@ -48,7 +58,6 @@ describe("thread cancel status", () => {
48
58
  });
49
59
 
50
60
  test("completed thread has status 'completed'", async () => {
51
- const tmpDir = await mkdtemp(join(tmpdir(), "uwf-cancel-test-"));
52
61
  const threadId = "01JTEST000000000000CANCEL2" as ThreadId;
53
62
  const uwf = await makeUwfStore(tmpDir);
54
63
  const head = await seedHistoryHead(uwf, "completed-head");
@@ -69,7 +78,6 @@ describe("thread cancel status", () => {
69
78
  });
70
79
 
71
80
  test("loadHistoryThreads returns completed and cancelled", async () => {
72
- const tmpDir = await mkdtemp(join(tmpdir(), "uwf-cancel-test-"));
73
81
  const uwf = await makeUwfStore(tmpDir);
74
82
  const head1 = await seedHistoryHead(uwf, "head1");
75
83
  const head2 = await seedHistoryHead(uwf, "head2");
@@ -103,7 +111,6 @@ describe("thread cancel status", () => {
103
111
  });
104
112
 
105
113
  test("mixed completed and cancelled entries preserve distinct statuses", async () => {
106
- const tmpDir = await mkdtemp(join(tmpdir(), "uwf-cancel-test-"));
107
114
  const uwf = await makeUwfStore(tmpDir);
108
115
  const head1 = await seedHistoryHead(uwf, "head1");
109
116
  const head2 = await seedHistoryHead(uwf, "head2");