@united-workforce/cli 0.5.0 → 0.6.1

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 (54) hide show
  1. package/dist/.build-fingerprint +1 -1
  2. package/dist/__tests__/config-text-renderer.test.d.ts +2 -0
  3. package/dist/__tests__/config-text-renderer.test.d.ts.map +1 -0
  4. package/dist/__tests__/config-text-renderer.test.js +137 -0
  5. package/dist/__tests__/config-text-renderer.test.js.map +1 -0
  6. package/dist/__tests__/issue-180-workflow-ref-removed.test.js +1 -1
  7. package/dist/__tests__/thread-agent-failure-suspended.test.d.ts +2 -0
  8. package/dist/__tests__/thread-agent-failure-suspended.test.d.ts.map +1 -0
  9. package/dist/__tests__/thread-agent-failure-suspended.test.js +332 -0
  10. package/dist/__tests__/thread-agent-failure-suspended.test.js.map +1 -0
  11. package/dist/__tests__/thread-join.test.d.ts +2 -0
  12. package/dist/__tests__/thread-join.test.d.ts.map +1 -0
  13. package/dist/__tests__/thread-join.test.js +77 -0
  14. package/dist/__tests__/thread-join.test.js.map +1 -0
  15. package/dist/__tests__/thread-poke.test.js +4 -1
  16. package/dist/__tests__/thread-poke.test.js.map +1 -1
  17. package/dist/__tests__/workflow-paths.test.d.ts +2 -0
  18. package/dist/__tests__/workflow-paths.test.d.ts.map +1 -0
  19. package/dist/__tests__/workflow-paths.test.js +261 -0
  20. package/dist/__tests__/workflow-paths.test.js.map +1 -0
  21. package/dist/cli.js +18 -1
  22. package/dist/cli.js.map +1 -1
  23. package/dist/commands/config.d.ts +5 -0
  24. package/dist/commands/config.d.ts.map +1 -1
  25. package/dist/commands/config.js +69 -3
  26. package/dist/commands/config.js.map +1 -1
  27. package/dist/commands/thread.d.ts +12 -0
  28. package/dist/commands/thread.d.ts.map +1 -1
  29. package/dist/commands/thread.js +183 -8
  30. package/dist/commands/thread.js.map +1 -1
  31. package/dist/commands/workflow.d.ts +1 -1
  32. package/dist/commands/workflow.d.ts.map +1 -1
  33. package/dist/commands/workflow.js +24 -4
  34. package/dist/commands/workflow.js.map +1 -1
  35. package/dist/output-mappers.d.ts.map +1 -1
  36. package/dist/output-mappers.js +1 -1
  37. package/dist/output-mappers.js.map +1 -1
  38. package/dist/store.d.ts +11 -0
  39. package/dist/store.d.ts.map +1 -1
  40. package/dist/store.js +20 -1
  41. package/dist/store.js.map +1 -1
  42. package/package.json +1 -1
  43. package/src/__tests__/config-text-renderer.test.ts +156 -0
  44. package/src/__tests__/issue-180-workflow-ref-removed.test.ts +1 -1
  45. package/src/__tests__/thread-agent-failure-suspended.test.ts +406 -0
  46. package/src/__tests__/thread-join.test.ts +103 -0
  47. package/src/__tests__/thread-poke.test.ts +4 -1
  48. package/src/__tests__/workflow-paths.test.ts +337 -0
  49. package/src/cli.ts +19 -0
  50. package/src/commands/config.ts +74 -3
  51. package/src/commands/thread.ts +233 -8
  52. package/src/commands/workflow.ts +29 -4
  53. package/src/output-mappers.ts +2 -1
  54. package/src/store.ts +25 -1
@@ -0,0 +1,156 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { formatOutput } from "../format.js";
3
+
4
+ describe("config text renderers", () => {
5
+ describe("config list", () => {
6
+ test("renders flat key-value pairs in text format", () => {
7
+ const data = {
8
+ defaultAgent: "claude-code",
9
+ agents: {
10
+ hermes: {
11
+ command: "uwf-hermes",
12
+ args: [],
13
+ },
14
+ "claude-code": {
15
+ command: "uwf-claude-code",
16
+ args: [],
17
+ },
18
+ },
19
+ concurrency: {
20
+ maxRunning: 4,
21
+ },
22
+ };
23
+
24
+ const result = formatOutput(data, "text", "config list");
25
+ expect(result).toContain("defaultAgent");
26
+ expect(result).toContain("claude-code");
27
+ expect(result).toContain("agents.hermes.command");
28
+ expect(result).toContain("uwf-hermes");
29
+ expect(result).toContain("agents.hermes.args");
30
+ expect(result).toContain("[]");
31
+ expect(result).toContain("agents.claude-code.command");
32
+ expect(result).toContain("uwf-claude-code");
33
+ expect(result).toContain("concurrency.maxRunning");
34
+ expect(result).toContain("4");
35
+ });
36
+
37
+ test("uses dot-notation for nested keys", () => {
38
+ const data = {
39
+ agents: {
40
+ hermes: {
41
+ command: "uwf-hermes",
42
+ },
43
+ },
44
+ };
45
+
46
+ const result = formatOutput(data, "text", "config list");
47
+ expect(result).toContain("agents.hermes.command");
48
+ });
49
+
50
+ test("displays array values as JSON", () => {
51
+ const data = {
52
+ agents: {
53
+ hermes: {
54
+ args: ["--flag", "--verbose"],
55
+ },
56
+ },
57
+ };
58
+
59
+ const result = formatOutput(data, "text", "config list");
60
+ expect(result).toContain('["--flag","--verbose"]');
61
+ });
62
+
63
+ test("does not throw on empty config", () => {
64
+ const result = formatOutput({}, "text", "config list");
65
+ expect(result).toBe("");
66
+ });
67
+
68
+ test("does not throw on null/undefined data", () => {
69
+ expect(() => formatOutput(null, "text", "config list")).not.toThrow();
70
+ expect(() => formatOutput(undefined, "text", "config list")).not.toThrow();
71
+ });
72
+ });
73
+
74
+ describe("config get", () => {
75
+ test("renders scalar value as bare string", () => {
76
+ const data = { value: "claude-code" };
77
+ const result = formatOutput(data, "text", "config get");
78
+ expect(result).toBe("claude-code");
79
+ });
80
+
81
+ test("renders number value as string", () => {
82
+ const data = { value: 4 };
83
+ const result = formatOutput(data, "text", "config get");
84
+ expect(result).toBe("4");
85
+ });
86
+
87
+ test("renders object value as flattened key-value pairs", () => {
88
+ const data = {
89
+ value: {
90
+ command: "uwf-hermes",
91
+ args: [],
92
+ },
93
+ };
94
+ const result = formatOutput(data, "text", "config get");
95
+ expect(result).toContain("command");
96
+ expect(result).toContain("uwf-hermes");
97
+ expect(result).toContain("args");
98
+ expect(result).toContain("[]");
99
+ });
100
+
101
+ test("does not throw on null value", () => {
102
+ expect(() => formatOutput({ value: null }, "text", "config get")).not.toThrow();
103
+ });
104
+
105
+ test("does not throw on missing value field", () => {
106
+ expect(() => formatOutput({}, "text", "config get")).not.toThrow();
107
+ });
108
+ });
109
+
110
+ describe("config set", () => {
111
+ test("renders key = value confirmation for scalar", () => {
112
+ const data = { key: "defaultAgent", value: "hermes" };
113
+ const result = formatOutput(data, "text", "config set");
114
+ expect(result).toBe("defaultAgent = hermes");
115
+ });
116
+
117
+ test("renders key = value for array values as JSON", () => {
118
+ const data = { key: "agents.hermes.args", value: ["--verbose"] };
119
+ const result = formatOutput(data, "text", "config set");
120
+ expect(result).toBe('agents.hermes.args = ["--verbose"]');
121
+ });
122
+
123
+ test("does not throw on missing key/value", () => {
124
+ expect(() => formatOutput({}, "text", "config set")).not.toThrow();
125
+ expect(() => formatOutput(null, "text", "config set")).not.toThrow();
126
+ });
127
+ });
128
+
129
+ describe("text format fallback", () => {
130
+ test("falls back to JSON pretty-print when no renderer registered", () => {
131
+ const data = { hello: "world" };
132
+ const result = formatOutput(data, "text", "unknown command");
133
+ expect(result).toBe(JSON.stringify(data, null, 2));
134
+ });
135
+
136
+ test("falls back to JSON pretty-print when commandPath is null", () => {
137
+ const data = { hello: "world" };
138
+ const result = formatOutput(data, "text", undefined);
139
+ expect(result).toBe(JSON.stringify(data, null, 2));
140
+ });
141
+ });
142
+
143
+ describe("json and yaml formats unaffected", () => {
144
+ test("json format still works with commandPath", () => {
145
+ const data = { key: "value" };
146
+ const result = formatOutput(data, "json", "config list");
147
+ expect(result).toBe(JSON.stringify(data));
148
+ });
149
+
150
+ test("yaml format still works with commandPath", () => {
151
+ const data = { key: "value" };
152
+ const result = formatOutput(data, "yaml", "config list");
153
+ expect(result).toContain("key: value");
154
+ });
155
+ });
156
+ });
@@ -31,7 +31,7 @@ describe("issue #180 — _workflowRef ghost parameter cleanup", () => {
31
31
  for (const match of source.matchAll(callRe)) {
32
32
  callSites.push(match[1]);
33
33
  }
34
- expect(callSites.length).toBe(3);
34
+ expect(callSites.length).toBe(4);
35
35
  for (const args of callSites) {
36
36
  const argCount = args
37
37
  .split(",")
@@ -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
+ });
@@ -0,0 +1,103 @@
1
+ import { mkdir, rm } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import type { CasRef, ThreadId } from "@united-workforce/protocol";
5
+ import { createThreadIndexEntry } from "@united-workforce/protocol";
6
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
7
+ import {
8
+ createMarker,
9
+ deleteMarker,
10
+ getProcessStartTime,
11
+ isThreadRunning,
12
+ } from "../background/index.js";
13
+ import { cmdThreadJoin } from "../commands/thread.js";
14
+ import { makeUwfStore, seedThread } from "./thread-test-helpers.js";
15
+
16
+ describe("cmdThreadJoin", () => {
17
+ let storageRoot: string;
18
+ let savedOcasHome: string | undefined;
19
+
20
+ beforeEach(async () => {
21
+ savedOcasHome = process.env.OCAS_HOME;
22
+ storageRoot = join(
23
+ tmpdir(),
24
+ `uwf-test-join-${Date.now()}-${Math.random().toString(36).slice(2)}`,
25
+ );
26
+ await mkdir(storageRoot, { recursive: true });
27
+ });
28
+
29
+ afterEach(async () => {
30
+ if (savedOcasHome !== undefined) {
31
+ process.env.OCAS_HOME = savedOcasHome;
32
+ } else {
33
+ delete process.env.OCAS_HOME;
34
+ }
35
+ await rm(storageRoot, { recursive: true, force: true });
36
+ });
37
+
38
+ it("throws when thread does not exist", async () => {
39
+ await makeUwfStore(storageRoot);
40
+ const threadId = "01JF0000000000NOTEXIST0" as ThreadId;
41
+ await expect(cmdThreadJoin(storageRoot, threadId, null)).rejects.toThrow(
42
+ /thread not found|process\.exit/,
43
+ );
44
+ });
45
+
46
+ it("times out when thread keeps running", async () => {
47
+ const threadId = "01JF0000000000TESTJOIN03" as ThreadId;
48
+ await makeUwfStore(storageRoot);
49
+ // Seed a thread so existence check passes
50
+ const uwf = await makeUwfStore(storageRoot);
51
+ const head = (await uwf.store.cas.put(uwf.schemas.text, "join-timeout-test")) as CasRef;
52
+ await seedThread(storageRoot, threadId, createThreadIndexEntry(head));
53
+
54
+ // Create a running marker with our PID (it will stay alive)
55
+ await createMarker(storageRoot, {
56
+ thread: threadId,
57
+ workflow: "AAAAAAAAAAAAA" as CasRef,
58
+ pid: process.pid,
59
+ startedAt: Date.now(),
60
+ processStartTime: getProcessStartTime(process.pid),
61
+ });
62
+
63
+ // Timeout after 100ms — should fail because marker never disappears
64
+ await expect(cmdThreadJoin(storageRoot, threadId, 100)).rejects.toThrow(
65
+ /join timed out|process\.exit/,
66
+ );
67
+
68
+ // Cleanup
69
+ await deleteMarker(storageRoot, threadId);
70
+ });
71
+
72
+ it("poll loop exits when marker is removed", async () => {
73
+ const threadId = "01JF0000000000TESTJOIN04" as ThreadId;
74
+ const uwf = await makeUwfStore(storageRoot);
75
+ const head = (await uwf.store.cas.put(uwf.schemas.text, "join-poll-test")) as CasRef;
76
+ await seedThread(storageRoot, threadId, createThreadIndexEntry(head));
77
+
78
+ // Create a running marker
79
+ await createMarker(storageRoot, {
80
+ thread: threadId,
81
+ workflow: "AAAAAAAAAAAAA" as CasRef,
82
+ pid: process.pid,
83
+ startedAt: Date.now(),
84
+ processStartTime: getProcessStartTime(process.pid),
85
+ });
86
+
87
+ // Confirm marker is valid
88
+ expect(await isThreadRunning(storageRoot, threadId)).not.toBeNull();
89
+
90
+ // Remove it after a short delay — simulates background worker finishing
91
+ setTimeout(() => {
92
+ deleteMarker(storageRoot, threadId);
93
+ }, 300);
94
+
95
+ // cmdThreadJoin will poll and wait. It will exit the poll loop after marker
96
+ // disappears, then try to resolve workflow from head. Our simple text node
97
+ // won't resolve, so it will fail — but the key test is that the poll loop
98
+ // DID exit (it didn't time out). We use a generous timeout to prove this.
99
+ await expect(cmdThreadJoin(storageRoot, threadId, 5000)).rejects.toThrow(
100
+ /failed to resolve workflow|process\.exit/,
101
+ );
102
+ });
103
+ });
@@ -538,7 +538,7 @@ describe("uwf thread poke - edge cases", () => {
538
538
  expect(cliOutput.suspendMessage).toBeNull();
539
539
  });
540
540
 
541
- test("6.2 agent failure leaves thread head unchanged", async () => {
541
+ test("6.2 agent failure leaves thread head unchanged and suspends thread", async () => {
542
542
  const { casDir, oldStepHash, failingAgentPath } = await setupThread();
543
543
  const result = runUwf(
544
544
  ["thread", "poke", THREAD_ID, "-p", "redo", "--agent", failingAgentPath],
@@ -550,5 +550,8 @@ describe("uwf thread poke - edge cases", () => {
550
550
  const uwf = await createUwfStore(tmpDir);
551
551
  const entry = getThread(uwf.varStore, THREAD_ID);
552
552
  expect(entry?.head).toBe(oldStepHash);
553
+ expect(entry?.status).toBe("suspended");
554
+ expect(entry?.suspendedRole).toBe("worker");
555
+ expect(entry?.suspendMessage).toContain("agent command failed");
553
556
  });
554
557
  });