@united-workforce/cli 0.6.1 → 0.8.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 (167) hide show
  1. package/README.md +120 -5
  2. package/dist/.build-fingerprint +1 -1
  3. package/dist/__tests__/agent-resolution-llm-free.test.js +9 -2
  4. package/dist/__tests__/agent-resolution-llm-free.test.js.map +1 -1
  5. package/dist/__tests__/broker-prompt.test.d.ts +10 -0
  6. package/dist/__tests__/broker-prompt.test.d.ts.map +1 -0
  7. package/dist/__tests__/broker-prompt.test.js +129 -0
  8. package/dist/__tests__/broker-prompt.test.js.map +1 -0
  9. package/dist/__tests__/broker-step-active-turns.test.d.ts +20 -0
  10. package/dist/__tests__/broker-step-active-turns.test.d.ts.map +1 -0
  11. package/dist/__tests__/broker-step-active-turns.test.js +428 -0
  12. package/dist/__tests__/broker-step-active-turns.test.js.map +1 -0
  13. package/dist/__tests__/broker-step-turn-chain-phase2.test.d.ts +13 -0
  14. package/dist/__tests__/broker-step-turn-chain-phase2.test.d.ts.map +1 -0
  15. package/dist/__tests__/broker-step-turn-chain-phase2.test.js +429 -0
  16. package/dist/__tests__/broker-step-turn-chain-phase2.test.js.map +1 -0
  17. package/dist/__tests__/config.test.js +33 -37
  18. package/dist/__tests__/config.test.js.map +1 -1
  19. package/dist/__tests__/e2e-broker-step-suspend.test.d.ts +18 -0
  20. package/dist/__tests__/e2e-broker-step-suspend.test.d.ts.map +1 -0
  21. package/dist/__tests__/e2e-broker-step-suspend.test.js +313 -0
  22. package/dist/__tests__/e2e-broker-step-suspend.test.js.map +1 -0
  23. package/dist/__tests__/e2e-broker-step.test.d.ts +13 -0
  24. package/dist/__tests__/e2e-broker-step.test.d.ts.map +1 -0
  25. package/dist/__tests__/e2e-broker-step.test.js +278 -0
  26. package/dist/__tests__/e2e-broker-step.test.js.map +1 -0
  27. package/dist/__tests__/e2e-mock-agent.test.js +1 -1
  28. package/dist/__tests__/e2e-mock-agent.test.js.map +1 -1
  29. package/dist/__tests__/e2e-thread-resume-timeout-suspend.test.d.ts +28 -0
  30. package/dist/__tests__/e2e-thread-resume-timeout-suspend.test.d.ts.map +1 -0
  31. package/dist/__tests__/e2e-thread-resume-timeout-suspend.test.js +322 -0
  32. package/dist/__tests__/e2e-thread-resume-timeout-suspend.test.js.map +1 -0
  33. package/dist/__tests__/log-tag-validity.test.d.ts +2 -0
  34. package/dist/__tests__/log-tag-validity.test.d.ts.map +1 -0
  35. package/dist/__tests__/log-tag-validity.test.js +110 -0
  36. package/dist/__tests__/log-tag-validity.test.js.map +1 -0
  37. package/dist/__tests__/setup-agent-discovery.test.js +35 -23
  38. package/dist/__tests__/setup-agent-discovery.test.js.map +1 -1
  39. package/dist/__tests__/setup-no-llm.test.js +5 -2
  40. package/dist/__tests__/setup-no-llm.test.js.map +1 -1
  41. package/dist/__tests__/step-ask.test.js +9 -6
  42. package/dist/__tests__/step-ask.test.js.map +1 -1
  43. package/dist/__tests__/step-show-json.test.js +5 -5
  44. package/dist/__tests__/step-show-json.test.js.map +1 -1
  45. package/dist/__tests__/step-show-text.test.d.ts +2 -0
  46. package/dist/__tests__/step-show-text.test.d.ts.map +1 -0
  47. package/dist/__tests__/step-show-text.test.js +192 -0
  48. package/dist/__tests__/step-show-text.test.js.map +1 -0
  49. package/dist/__tests__/step-turns-cli-subprocess.test.d.ts +21 -0
  50. package/dist/__tests__/step-turns-cli-subprocess.test.d.ts.map +1 -0
  51. package/dist/__tests__/step-turns-cli-subprocess.test.js +356 -0
  52. package/dist/__tests__/step-turns-cli-subprocess.test.js.map +1 -0
  53. package/dist/__tests__/step-turns-panorama-phase3.test.d.ts +21 -0
  54. package/dist/__tests__/step-turns-panorama-phase3.test.d.ts.map +1 -0
  55. package/dist/__tests__/step-turns-panorama-phase3.test.js +476 -0
  56. package/dist/__tests__/step-turns-panorama-phase3.test.js.map +1 -0
  57. package/dist/__tests__/step-turns.test.d.ts +24 -0
  58. package/dist/__tests__/step-turns.test.d.ts.map +1 -0
  59. package/dist/__tests__/step-turns.test.js +646 -0
  60. package/dist/__tests__/step-turns.test.js.map +1 -0
  61. package/dist/__tests__/store-turn-chain.test.d.ts +2 -0
  62. package/dist/__tests__/store-turn-chain.test.d.ts.map +1 -0
  63. package/dist/__tests__/store-turn-chain.test.js +341 -0
  64. package/dist/__tests__/store-turn-chain.test.js.map +1 -0
  65. package/dist/__tests__/thread-agent-failure-suspended.test.js +3 -3
  66. package/dist/__tests__/thread-agent-failure-suspended.test.js.map +1 -1
  67. package/dist/__tests__/thread-list-limit-offset.test.d.ts +24 -0
  68. package/dist/__tests__/thread-list-limit-offset.test.d.ts.map +1 -0
  69. package/dist/__tests__/thread-list-limit-offset.test.js +254 -0
  70. package/dist/__tests__/thread-list-limit-offset.test.js.map +1 -0
  71. package/dist/__tests__/thread-list-template-ms-date.test.js +7 -2
  72. package/dist/__tests__/thread-list-template-ms-date.test.js.map +1 -1
  73. package/dist/__tests__/thread-poke.test.js +6 -6
  74. package/dist/__tests__/thread-poke.test.js.map +1 -1
  75. package/dist/__tests__/thread-resume.test.js +2 -2
  76. package/dist/__tests__/thread-resume.test.js.map +1 -1
  77. package/dist/__tests__/thread-suspend-step.test.js +1 -1
  78. package/dist/__tests__/thread-suspend-step.test.js.map +1 -1
  79. package/dist/__tests__/thread.test.js +28 -14
  80. package/dist/__tests__/thread.test.js.map +1 -1
  81. package/dist/cli.js +910 -344
  82. package/dist/cli.js.map +1 -1
  83. package/dist/commands/broker-step.d.ts +117 -0
  84. package/dist/commands/broker-step.d.ts.map +1 -0
  85. package/dist/commands/broker-step.js +654 -0
  86. package/dist/commands/broker-step.js.map +1 -0
  87. package/dist/commands/config.d.ts.map +1 -1
  88. package/dist/commands/config.js +2 -23
  89. package/dist/commands/config.js.map +1 -1
  90. package/dist/commands/prompt.d.ts.map +1 -1
  91. package/dist/commands/prompt.js +43 -51
  92. package/dist/commands/prompt.js.map +1 -1
  93. package/dist/commands/setup.d.ts +6 -4
  94. package/dist/commands/setup.d.ts.map +1 -1
  95. package/dist/commands/setup.js +24 -27
  96. package/dist/commands/setup.js.map +1 -1
  97. package/dist/commands/step.d.ts +54 -6
  98. package/dist/commands/step.d.ts.map +1 -1
  99. package/dist/commands/step.js +484 -134
  100. package/dist/commands/step.js.map +1 -1
  101. package/dist/commands/thread.d.ts +4 -0
  102. package/dist/commands/thread.d.ts.map +1 -1
  103. package/dist/commands/thread.js +77 -151
  104. package/dist/commands/thread.js.map +1 -1
  105. package/dist/output-mappers.d.ts +8 -0
  106. package/dist/output-mappers.d.ts.map +1 -1
  107. package/dist/output-mappers.js +72 -18
  108. package/dist/output-mappers.js.map +1 -1
  109. package/dist/schemas.d.ts +3 -0
  110. package/dist/schemas.d.ts.map +1 -1
  111. package/dist/schemas.js +17 -3
  112. package/dist/schemas.js.map +1 -1
  113. package/dist/store.d.ts +147 -1
  114. package/dist/store.d.ts.map +1 -1
  115. package/dist/store.js +254 -1
  116. package/dist/store.js.map +1 -1
  117. package/dist/text-renderers.d.ts.map +1 -1
  118. package/dist/text-renderers.js +27 -2
  119. package/dist/text-renderers.js.map +1 -1
  120. package/package.json +7 -5
  121. package/src/__tests__/agent-resolution-llm-free.test.ts +14 -2
  122. package/src/__tests__/broker-prompt.test.ts +142 -0
  123. package/src/__tests__/broker-step-active-turns.test.ts +509 -0
  124. package/src/__tests__/broker-step-turn-chain-phase2.test.ts +525 -0
  125. package/src/__tests__/config.test.ts +35 -39
  126. package/src/__tests__/e2e-broker-step-suspend.test.ts +351 -0
  127. package/src/__tests__/e2e-broker-step.test.ts +320 -0
  128. package/src/__tests__/e2e-mock-agent.test.ts +1 -1
  129. package/src/__tests__/e2e-thread-resume-timeout-suspend.test.ts +360 -0
  130. package/src/__tests__/log-tag-validity.test.ts +124 -0
  131. package/src/__tests__/setup-agent-discovery.test.ts +35 -23
  132. package/src/__tests__/setup-no-llm.test.ts +5 -2
  133. package/src/__tests__/step-ask.test.ts +9 -6
  134. package/src/__tests__/step-show-json.test.ts +5 -5
  135. package/src/__tests__/step-show-text.test.ts +236 -0
  136. package/src/__tests__/step-turns-cli-subprocess.test.ts +411 -0
  137. package/src/__tests__/step-turns-panorama-phase3.test.ts +579 -0
  138. package/src/__tests__/step-turns.test.ts +734 -0
  139. package/src/__tests__/store-turn-chain.test.ts +386 -0
  140. package/src/__tests__/thread-agent-failure-suspended.test.ts +3 -3
  141. package/src/__tests__/thread-list-limit-offset.test.ts +305 -0
  142. package/src/__tests__/thread-list-template-ms-date.test.ts +7 -2
  143. package/src/__tests__/thread-poke.test.ts +6 -6
  144. package/src/__tests__/thread-resume.test.ts +2 -2
  145. package/src/__tests__/thread-suspend-step.test.ts +1 -1
  146. package/src/__tests__/thread.test.ts +29 -15
  147. package/src/cli.ts +1056 -483
  148. package/src/commands/broker-step.ts +913 -0
  149. package/src/commands/config.ts +2 -24
  150. package/src/commands/prompt.ts +43 -51
  151. package/src/commands/setup.ts +25 -29
  152. package/src/commands/step.ts +645 -176
  153. package/src/commands/thread.ts +87 -192
  154. package/src/output-mappers.ts +99 -21
  155. package/src/schemas.ts +32 -2
  156. package/src/store.ts +297 -2
  157. package/src/text-renderers.ts +35 -2
  158. package/dist/__tests__/adapter-json-roundtrip.test.d.ts +0 -2
  159. package/dist/__tests__/adapter-json-roundtrip.test.d.ts.map +0 -1
  160. package/dist/__tests__/adapter-json-roundtrip.test.js +0 -160
  161. package/dist/__tests__/adapter-json-roundtrip.test.js.map +0 -1
  162. package/dist/__tests__/spawn-agent-json.test.d.ts +0 -2
  163. package/dist/__tests__/spawn-agent-json.test.d.ts.map +0 -1
  164. package/dist/__tests__/spawn-agent-json.test.js +0 -79
  165. package/dist/__tests__/spawn-agent-json.test.js.map +0 -1
  166. package/src/__tests__/adapter-json-roundtrip.test.ts +0 -193
  167. package/src/__tests__/spawn-agent-json.test.ts +0 -100
@@ -0,0 +1,525 @@
1
+ /**
2
+ * Phase 2 (#419) — Turn chain with prev+owner fields and thread-keyed active vars.
3
+ *
4
+ * Covers the spec acceptance scenarios:
5
+ * 1. onTurn writes each turn with prev pointer and owner reference
6
+ * 2. Step-start/step-complete dual node lifecycle
7
+ * 3. Same role multi-round ownership (#412 regression test)
8
+ * 4. Thread-keyed active vars (not role-keyed)
9
+ * 5. Crash recovery isolation (new attempt gets new step-start)
10
+ * 6. Detail node has no turns array (turns self-contained via chain)
11
+ */
12
+
13
+ import { mkdtemp, rm } from "node:fs/promises";
14
+ import { tmpdir } from "node:os";
15
+ import { join } from "node:path";
16
+ import { putSchema } from "@ocas/core";
17
+ import type {
18
+ CasRef,
19
+ StepStartPayload,
20
+ ThreadId,
21
+ TurnNodePayload,
22
+ WorkflowConfig,
23
+ WorkflowPayload,
24
+ } from "@united-workforce/protocol";
25
+ import { createProcessLogger } from "@united-workforce/util";
26
+ import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
27
+ import { executeBrokerStep } from "../commands/broker-step.js";
28
+ import {
29
+ ACTIVE_TURNS_VAR_PREFIX,
30
+ activeStepVarName,
31
+ activeTurnHeadVarName,
32
+ createUwfStore,
33
+ getActiveStep,
34
+ getActiveTurnHead,
35
+ turnsOfStep,
36
+ type UwfStore,
37
+ walkTurnChain,
38
+ writeStepStart,
39
+ writeTurnNode,
40
+ } from "../store.js";
41
+
42
+ // ── SSE plumbing ─────────────────────────────────────────────────────────────
43
+
44
+ function sseFrame(id: number, event: string, data: unknown): string {
45
+ return `id: ${id}\nevent: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
46
+ }
47
+
48
+ function turnFrame(id: number, index: number, content: string): string {
49
+ return sseFrame(id, "turn", {
50
+ type: "@sumeru/turn",
51
+ value: { index, role: "assistant", content, timestamp: "", toolCalls: null },
52
+ });
53
+ }
54
+
55
+ function doneFrame(id: number, turnCount: number): string {
56
+ return sseFrame(id, "done", {
57
+ type: "@sumeru/summary",
58
+ value: { turnCount, tokens: { in: 9, out: 4 }, durationMs: 42 },
59
+ });
60
+ }
61
+
62
+ function delay(ms: number): Promise<void> {
63
+ return new Promise((resolve) => setTimeout(resolve, ms));
64
+ }
65
+
66
+ const PER_TURN_MS = 40;
67
+
68
+ function buildPacedSseResponse(frames: string[]): Response {
69
+ const encoder = new TextEncoder();
70
+ let cancelled = false;
71
+ const stream = new ReadableStream<Uint8Array>({
72
+ start(controller) {
73
+ void (async () => {
74
+ try {
75
+ for (const frame of frames) {
76
+ if (cancelled) return;
77
+ controller.enqueue(encoder.encode(frame));
78
+ await delay(PER_TURN_MS);
79
+ }
80
+ if (!cancelled) controller.close();
81
+ } catch {
82
+ // Consumer closed/cancelled the stream first
83
+ }
84
+ })();
85
+ },
86
+ cancel() {
87
+ cancelled = true;
88
+ },
89
+ });
90
+ return new Response(stream, {
91
+ status: 200,
92
+ headers: { "Content-Type": "text/event-stream; charset=utf-8" },
93
+ });
94
+ }
95
+
96
+ function buildJsonResponse(status: number, body: unknown): Response {
97
+ return new Response(JSON.stringify(body), {
98
+ status,
99
+ headers: { "Content-Type": "application/json" },
100
+ });
101
+ }
102
+
103
+ // ── Fixture ──────────────────────────────────────────────────────────────────
104
+
105
+ const ROLE_OUTPUT_SCHEMA = {
106
+ title: "coder-output",
107
+ type: "object" as const,
108
+ required: ["$status"],
109
+ properties: {
110
+ $status: { type: "string" as const, enum: ["done", "failed"] },
111
+ summary: { type: "string" as const },
112
+ },
113
+ additionalProperties: false,
114
+ };
115
+
116
+ const FINAL_TURN = `---
117
+ $status: done
118
+ summary: shipped
119
+ ---
120
+ the final answer`;
121
+
122
+ const HOST = "http://127.0.0.1:7900";
123
+ const GATEWAY = "coder-gw";
124
+ const ALIAS = "coder-agent";
125
+ const SESSION_ID = "ses_turn_chain_phase2";
126
+ const THREAD_ID = "06FDTURNCHAINPHASE2TEST01" as ThreadId;
127
+ const ROLE = "coder";
128
+
129
+ function buildConfig(): WorkflowConfig {
130
+ return {
131
+ agents: { [ALIAS]: { host: HOST, gateway: GATEWAY } },
132
+ defaultAgent: ALIAS,
133
+ agentOverrides: null,
134
+ };
135
+ }
136
+
137
+ async function buildWorkflow(uwf: UwfStore): Promise<{
138
+ workflow: WorkflowPayload;
139
+ startHash: CasRef;
140
+ }> {
141
+ const frontmatterHash = (await putSchema(uwf.store, ROLE_OUTPUT_SCHEMA)) as CasRef;
142
+ const workflow: WorkflowPayload = {
143
+ version: 1,
144
+ name: "turn-chain-wf",
145
+ description: "phase2 turn chain",
146
+ roles: {
147
+ [ROLE]: {
148
+ description: "writes code",
149
+ goal: "produce a change",
150
+ capabilities: [],
151
+ procedure: "do the work",
152
+ output: "frontmatter+body",
153
+ frontmatter: frontmatterHash,
154
+ },
155
+ },
156
+ graph: {
157
+ [ROLE]: {
158
+ done: { role: "$END", prompt: "", location: null },
159
+ },
160
+ },
161
+ };
162
+ const startHash = (await uwf.store.cas.put(uwf.schemas.startNode, {
163
+ workflow: await uwf.store.cas.put(uwf.schemas.workflow, workflow),
164
+ prompt: "task",
165
+ cwd: "/tmp/work",
166
+ })) as CasRef;
167
+ return { workflow, startHash };
168
+ }
169
+
170
+ function resolveFetchUrl(input: string | URL | Request): string {
171
+ if (typeof input === "string") return input;
172
+ if (input instanceof URL) return input.href;
173
+ return input.url;
174
+ }
175
+
176
+ function runStep(
177
+ uwf: UwfStore,
178
+ workflow: WorkflowPayload,
179
+ startHash: CasRef,
180
+ tmpDir: string,
181
+ prevHash: CasRef | null = null,
182
+ ) {
183
+ return executeBrokerStep({
184
+ storageRoot: tmpDir,
185
+ uwf,
186
+ config: buildConfig(),
187
+ workflow,
188
+ threadId: THREAD_ID,
189
+ role: ROLE,
190
+ edgePrompt: "go",
191
+ effectiveCwd: "/tmp/work",
192
+ startHash,
193
+ prevHash,
194
+ agentOverride: null,
195
+ previousAttempts: null,
196
+ plog: createProcessLogger({
197
+ storageRoot: tmpDir,
198
+ context: { thread: THREAD_ID, workflow: "turn-chain-wf" },
199
+ }),
200
+ });
201
+ }
202
+
203
+ // ── Tests ────────────────────────────────────────────────────────────────────
204
+
205
+ describe("turn chain Phase 2 (#419)", () => {
206
+ let tmpDir: string;
207
+ let casDir: string;
208
+ let savedOcasHome: string | undefined;
209
+
210
+ beforeEach(async () => {
211
+ savedOcasHome = process.env.OCAS_HOME;
212
+ tmpDir = await mkdtemp(join(tmpdir(), "turn-chain-phase2-"));
213
+ casDir = join(tmpDir, "cas");
214
+ process.env.OCAS_HOME = casDir;
215
+
216
+ vi.stubGlobal(
217
+ "fetch",
218
+ async (input: string | URL | Request, _init: RequestInit | undefined): Promise<Response> => {
219
+ const url = resolveFetchUrl(input);
220
+ if (url.endsWith(`/gateways/${GATEWAY}/sessions`)) {
221
+ return buildJsonResponse(201, {
222
+ type: "@sumeru/session",
223
+ value: { id: SESSION_ID, gateway: GATEWAY },
224
+ });
225
+ }
226
+ if (url.endsWith(`/sessions/${SESSION_ID}/messages`)) {
227
+ return buildPacedSseResponse([
228
+ turnFrame(1, 0, "First analysis"),
229
+ turnFrame(2, 1, "Continued work"),
230
+ turnFrame(3, 2, FINAL_TURN),
231
+ doneFrame(4, 3),
232
+ ]);
233
+ }
234
+ return buildJsonResponse(500, { error: "unexpected url", url });
235
+ },
236
+ );
237
+ });
238
+
239
+ afterEach(async () => {
240
+ vi.unstubAllGlobals();
241
+ if (savedOcasHome === undefined) delete process.env.OCAS_HOME;
242
+ else process.env.OCAS_HOME = savedOcasHome;
243
+ await rm(tmpDir, { recursive: true, force: true });
244
+ });
245
+
246
+ test("onTurn writes each turn with prev pointer and owner reference", async () => {
247
+ const uwf = await createUwfStore(tmpDir);
248
+ const { workflow, startHash } = await buildWorkflow(uwf);
249
+
250
+ const result = await runStep(uwf, workflow, startHash, tmpDir);
251
+ expect(result.isError).toBe(false);
252
+
253
+ // Get the turn chain head
254
+ const turnHead = getActiveTurnHead(uwf.store, THREAD_ID);
255
+ expect(turnHead).not.toBeNull();
256
+
257
+ // Walk the turn chain
258
+ const turnChain = walkTurnChain(uwf, turnHead!);
259
+ expect(turnChain).toHaveLength(3);
260
+
261
+ // Verify each turn has correct prev and owner
262
+ const turn0 = uwf.store.cas.get(turnChain[0]!)?.payload as TurnNodePayload;
263
+ const turn1 = uwf.store.cas.get(turnChain[1]!)?.payload as TurnNodePayload;
264
+ const turn2 = uwf.store.cas.get(turnChain[2]!)?.payload as TurnNodePayload;
265
+
266
+ // Turn 0: first turn, prev is null
267
+ expect(turn0.prev).toBeNull();
268
+ expect(turn0.owner).not.toBeNull();
269
+ expect(turn0.content).toBe("First analysis");
270
+
271
+ // Turn 1: prev points to turn 0
272
+ expect(turn1.prev).toBe(turnChain[0]);
273
+ expect(turn1.owner).toBe(turn0.owner);
274
+ expect(turn1.content).toBe("Continued work");
275
+
276
+ // Turn 2: prev points to turn 1
277
+ expect(turn2.prev).toBe(turnChain[1]);
278
+ expect(turn2.owner).toBe(turn0.owner);
279
+ expect(turn2.content).toBe(FINAL_TURN);
280
+
281
+ // All turns have same owner (the step-start)
282
+ expect(turn0.owner).toBe(turn1.owner);
283
+ expect(turn1.owner).toBe(turn2.owner);
284
+ });
285
+
286
+ test("step-start is written at entry and active-step var is set", async () => {
287
+ const uwf = await createUwfStore(tmpDir);
288
+ const { workflow, startHash } = await buildWorkflow(uwf);
289
+
290
+ // Capture active-step during execution
291
+ let activeStepDuringExec: CasRef | null = null;
292
+ const originalFetch = globalThis.fetch;
293
+ vi.stubGlobal(
294
+ "fetch",
295
+ async (input: string | URL | Request, init: RequestInit | undefined): Promise<Response> => {
296
+ const url = resolveFetchUrl(input);
297
+ if (url.endsWith(`/sessions/${SESSION_ID}/messages`)) {
298
+ // Sample active-step while broker is in flight
299
+ activeStepDuringExec = getActiveStep(uwf.store, THREAD_ID);
300
+ }
301
+ return (originalFetch as typeof fetch)(input, init);
302
+ },
303
+ );
304
+
305
+ const result = await runStep(uwf, workflow, startHash, tmpDir);
306
+ expect(result.isError).toBe(false);
307
+
308
+ // active-step was set during execution
309
+ expect(activeStepDuringExec).not.toBeNull();
310
+
311
+ // active-step is cleared after completion
312
+ const activeStepAfter = getActiveStep(uwf.store, THREAD_ID);
313
+ expect(activeStepAfter).toBeNull();
314
+
315
+ // Verify step-start node exists and has correct structure
316
+ const stepStartNode = uwf.store.cas.get(activeStepDuringExec!);
317
+ expect(stepStartNode).not.toBeNull();
318
+ const stepStartPayload = stepStartNode?.payload as StepStartPayload;
319
+ expect(stepStartPayload.role).toBe(ROLE);
320
+ expect(stepStartPayload.edgePrompt).toBe("go");
321
+ expect(stepStartPayload.stepIndex).toBe(0);
322
+ expect(stepStartPayload.prev).toBeNull();
323
+ expect(stepStartPayload.start).toBe(startHash);
324
+ });
325
+
326
+ test("detail node has no turns array (turns self-contained via chain)", async () => {
327
+ const uwf = await createUwfStore(tmpDir);
328
+ const { workflow, startHash } = await buildWorkflow(uwf);
329
+
330
+ const result = await runStep(uwf, workflow, startHash, tmpDir);
331
+ expect(result.isError).toBe(false);
332
+
333
+ // Get detail node
334
+ const detailNode = uwf.store.cas.get(result.detailHash);
335
+ expect(detailNode).not.toBeNull();
336
+
337
+ const detail = detailNode?.payload as Record<string, unknown>;
338
+
339
+ // Detail should have sessionId, duration, turnCount but NOT turns array
340
+ expect(detail.sessionId).toBe(SESSION_ID);
341
+ expect(typeof detail.duration).toBe("number");
342
+ expect(detail.turnCount).toBe(3);
343
+ expect(detail.turns).toBeUndefined(); // No turns array in Phase 2
344
+ });
345
+
346
+ test("thread-keyed active vars exist, role-keyed do not", async () => {
347
+ const uwf = await createUwfStore(tmpDir);
348
+ const { workflow, startHash } = await buildWorkflow(uwf);
349
+
350
+ await runStep(uwf, workflow, startHash, tmpDir);
351
+
352
+ // Thread-keyed turn head exists
353
+ const turnHeadVars = uwf.varStore.list({
354
+ exactName: activeTurnHeadVarName(THREAD_ID),
355
+ });
356
+ expect(turnHeadVars.length).toBe(1);
357
+
358
+ // Active-step var is cleared (step completed)
359
+ const activeStepVars = uwf.varStore.list({
360
+ exactName: activeStepVarName(THREAD_ID),
361
+ });
362
+ expect(activeStepVars.length).toBe(0);
363
+
364
+ // Role-keyed var is also cleared (backward compat cleanup)
365
+ const roleKeyedVars = uwf.varStore.list({
366
+ namePrefix: `${ACTIVE_TURNS_VAR_PREFIX}${THREAD_ID}/`,
367
+ });
368
+ expect(roleKeyedVars.length).toBe(0);
369
+ });
370
+
371
+ test("turnsOfStep filters turns by owner", async () => {
372
+ const uwf = await createUwfStore(tmpDir);
373
+ const { workflow, startHash } = await buildWorkflow(uwf);
374
+
375
+ const result = await runStep(uwf, workflow, startHash, tmpDir);
376
+ expect(result.isError).toBe(false);
377
+
378
+ const turnHead = getActiveTurnHead(uwf.store, THREAD_ID);
379
+ expect(turnHead).not.toBeNull();
380
+
381
+ // Get the step-start from the first turn's owner
382
+ const firstTurn = uwf.store.cas.get(walkTurnChain(uwf, turnHead!)[0]!)
383
+ ?.payload as TurnNodePayload;
384
+ const stepStartHash = firstTurn.owner!;
385
+
386
+ // turnsOfStep should return all 3 turns for this step
387
+ const stepTurns = turnsOfStep(uwf, turnHead!, stepStartHash);
388
+ expect(stepTurns).toHaveLength(3);
389
+
390
+ // A different step-start should return no turns
391
+ const otherStepStart = writeStepStart(uwf, {
392
+ role: "other",
393
+ edgePrompt: "other",
394
+ stepIndex: 1,
395
+ prev: stepStartHash,
396
+ start: startHash,
397
+ startedAtMs: Date.now(),
398
+ cwd: "/tmp",
399
+ });
400
+ const otherTurns = turnsOfStep(uwf, turnHead!, otherStepStart);
401
+ expect(otherTurns).toHaveLength(0);
402
+ });
403
+ });
404
+
405
+ describe("turn chain unit tests", () => {
406
+ let tmpDir: string;
407
+ let savedOcasHome: string | undefined;
408
+
409
+ beforeEach(async () => {
410
+ savedOcasHome = process.env.OCAS_HOME;
411
+ tmpDir = await mkdtemp(join(tmpdir(), "turn-chain-unit-"));
412
+ process.env.OCAS_HOME = join(tmpDir, "cas");
413
+ });
414
+
415
+ afterEach(async () => {
416
+ if (savedOcasHome === undefined) delete process.env.OCAS_HOME;
417
+ else process.env.OCAS_HOME = savedOcasHome;
418
+ await rm(tmpDir, { recursive: true, force: true });
419
+ });
420
+
421
+ test("same role multi-round: turns have correct owner (#412 regression)", async () => {
422
+ const uwf = await createUwfStore(tmpDir);
423
+ const startRef = (await uwf.store.cas.put(uwf.schemas.text, "thread-start")) as CasRef;
424
+
425
+ // Round 1: developer
426
+ const ss_dev1 = writeStepStart(uwf, {
427
+ role: "developer",
428
+ edgePrompt: "Implement",
429
+ stepIndex: 0,
430
+ prev: null,
431
+ start: startRef,
432
+ startedAtMs: 1000,
433
+ cwd: "/repo",
434
+ });
435
+ const t1 = writeTurnNode(uwf, { role: "assistant", content: "T1", prev: null, owner: ss_dev1 });
436
+ const t2 = writeTurnNode(uwf, { role: "assistant", content: "T2", prev: t1, owner: ss_dev1 });
437
+
438
+ // Reviewer
439
+ const ss_rev = writeStepStart(uwf, {
440
+ role: "reviewer",
441
+ edgePrompt: "Review",
442
+ stepIndex: 1,
443
+ prev: ss_dev1,
444
+ start: startRef,
445
+ startedAtMs: 2000,
446
+ cwd: "/repo",
447
+ });
448
+ const t3 = writeTurnNode(uwf, { role: "assistant", content: "T3", prev: t2, owner: ss_rev });
449
+ const t4 = writeTurnNode(uwf, { role: "assistant", content: "T4", prev: t3, owner: ss_rev });
450
+
451
+ // Round 2: developer again (same role, different step-start)
452
+ const ss_dev2 = writeStepStart(uwf, {
453
+ role: "developer",
454
+ edgePrompt: "Fix issues",
455
+ stepIndex: 2,
456
+ prev: ss_rev,
457
+ start: startRef,
458
+ startedAtMs: 3000,
459
+ cwd: "/repo",
460
+ });
461
+ const t5 = writeTurnNode(uwf, { role: "assistant", content: "T5", prev: t4, owner: ss_dev2 });
462
+
463
+ // Verify ownership
464
+ expect((uwf.store.cas.get(t1)?.payload as TurnNodePayload).owner).toBe(ss_dev1);
465
+ expect((uwf.store.cas.get(t2)?.payload as TurnNodePayload).owner).toBe(ss_dev1);
466
+ expect((uwf.store.cas.get(t3)?.payload as TurnNodePayload).owner).toBe(ss_rev);
467
+ expect((uwf.store.cas.get(t4)?.payload as TurnNodePayload).owner).toBe(ss_rev);
468
+ expect((uwf.store.cas.get(t5)?.payload as TurnNodePayload).owner).toBe(ss_dev2);
469
+
470
+ // turnsOfStep correctly filters by owner
471
+ expect(turnsOfStep(uwf, t5, ss_dev1)).toEqual([t1, t2]);
472
+ expect(turnsOfStep(uwf, t5, ss_rev)).toEqual([t3, t4]);
473
+ expect(turnsOfStep(uwf, t5, ss_dev2)).toEqual([t5]);
474
+
475
+ // Step-start chain is correct
476
+ expect((uwf.store.cas.get(ss_dev2)?.payload as StepStartPayload).prev).toBe(ss_rev);
477
+ expect((uwf.store.cas.get(ss_rev)?.payload as StepStartPayload).prev).toBe(ss_dev1);
478
+ expect((uwf.store.cas.get(ss_dev1)?.payload as StepStartPayload).prev).toBeNull();
479
+ });
480
+
481
+ test("crash recovery: new attempt gets new step-start, old turns orphaned", async () => {
482
+ const uwf = await createUwfStore(tmpDir);
483
+ const startRef = (await uwf.store.cas.put(uwf.schemas.text, "thread-start")) as CasRef;
484
+
485
+ // Attempt 1 (crashed): step-start SS1 with 2 turns
486
+ const ss1 = writeStepStart(uwf, {
487
+ role: "developer",
488
+ edgePrompt: "Implement",
489
+ stepIndex: 0,
490
+ prev: null,
491
+ start: startRef,
492
+ startedAtMs: 1000,
493
+ cwd: "/repo",
494
+ });
495
+ const t1 = writeTurnNode(uwf, { role: "assistant", content: "Old T1", prev: null, owner: ss1 });
496
+ const t2 = writeTurnNode(uwf, { role: "assistant", content: "Old T2", prev: t1, owner: ss1 });
497
+
498
+ // Attempt 2 (recovery): new step-start SS2
499
+ const ss2 = writeStepStart(uwf, {
500
+ role: "developer",
501
+ edgePrompt: "Implement",
502
+ stepIndex: 0,
503
+ prev: null, // Same prev as SS1 (recovery starts fresh)
504
+ start: startRef,
505
+ startedAtMs: 2000,
506
+ cwd: "/repo",
507
+ });
508
+
509
+ // New turns link to global chain (prev=t2) but have different owner
510
+ const t3 = writeTurnNode(uwf, { role: "assistant", content: "New T3", prev: t2, owner: ss2 });
511
+ const t4 = writeTurnNode(uwf, { role: "assistant", content: "New T4", prev: t3, owner: ss2 });
512
+
513
+ // SS1 and SS2 have different hashes
514
+ expect(ss1).not.toBe(ss2);
515
+
516
+ // Old attempt's turns belong to SS1
517
+ expect(turnsOfStep(uwf, t4, ss1)).toEqual([t1, t2]);
518
+
519
+ // New attempt's turns belong to SS2
520
+ expect(turnsOfStep(uwf, t4, ss2)).toEqual([t3, t4]);
521
+
522
+ // Walking the full chain shows all 4 turns
523
+ expect(walkTurnChain(uwf, t4)).toEqual([t1, t2, t3, t4]);
524
+ });
525
+ });
@@ -21,18 +21,15 @@ describe("config command", () => {
21
21
  return configPath;
22
22
  }
23
23
 
24
- // Sample test config — engine-only (no providers/models/defaultModel/modelOverrides)
24
+ // Sample test config — engine-only (no providers/models/defaultModel/modelOverrides).
25
+ // Phase 3 (#380) replaced the legacy {command, args} agent shape with {host, gateway}.
25
26
  const sampleConfig = `agents:
26
27
  hermes:
27
- command: uwf-hermes
28
- args:
29
- - --provider
30
- - dashscope
28
+ host: http://127.0.0.1:7900
29
+ gateway: hermes
31
30
  claude-code:
32
- command: claude-code
33
- args:
34
- - --profile
35
- - work
31
+ host: http://127.0.0.1:7901
32
+ gateway: claude-code
36
33
  defaultAgent: hermes
37
34
  `;
38
35
 
@@ -41,7 +38,7 @@ defaultAgent: hermes
41
38
  test("splits dot notation correctly", () => {
42
39
  expect(parseDotPath("a.b.c")).toEqual(["a", "b", "c"]);
43
40
  expect(parseDotPath("defaultAgent")).toEqual(["defaultAgent"]);
44
- expect(parseDotPath("agents.hermes.command")).toEqual(["agents", "hermes", "command"]);
41
+ expect(parseDotPath("agents.hermes.host")).toEqual(["agents", "hermes", "host"]);
45
42
  });
46
43
  });
47
44
 
@@ -85,7 +82,7 @@ defaultAgent: hermes
85
82
  describe("maskApiKeys", () => {
86
83
  test("returns deep clone (no mutation) — engine config has no apiKey to mask", () => {
87
84
  const config = {
88
- agents: { hermes: { command: "uwf-hermes", args: [] } },
85
+ agents: { hermes: { host: "http://127.0.0.1:7900", gateway: "hermes" } },
89
86
  defaultAgent: "hermes",
90
87
  };
91
88
  const masked = maskApiKeys(config);
@@ -153,12 +150,12 @@ defaultAgent: hermes
153
150
  }
154
151
  });
155
152
 
156
- test("retrieves array value (agents.hermes.args)", async () => {
153
+ test("retrieves nested string value (agents.hermes.host)", async () => {
157
154
  const tempDir = mkdtempSync(join(tmpdir(), "test-config-"));
158
155
  try {
159
156
  createTestConfig(tempDir, sampleConfig);
160
- const result = await cmdConfigGet(tempDir, "agents.hermes.args");
161
- expect(result).toEqual(["--provider", "dashscope"]);
157
+ const result = await cmdConfigGet(tempDir, "agents.hermes.host");
158
+ expect(result).toBe("http://127.0.0.1:7900");
162
159
  } finally {
163
160
  rmSync(tempDir, { recursive: true, force: true });
164
161
  }
@@ -208,18 +205,17 @@ defaultAgent: hermes
208
205
  }
209
206
  });
210
207
 
211
- test("sets array value for args key with valid JSON array", async () => {
208
+ test("sets nested string value (agents.hermes.host)", async () => {
212
209
  const tempDir = mkdtempSync(join(tmpdir(), "test-config-"));
213
210
  try {
214
211
  createTestConfig(tempDir, sampleConfig);
215
- const newArgs = '["--new", "--flags"]';
216
- const result = await cmdConfigSet(tempDir, "agents.hermes.args", newArgs);
212
+ const result = await cmdConfigSet(tempDir, "agents.hermes.host", "http://10.0.0.1:7900");
217
213
  expect(result).toEqual({
218
- key: "agents.hermes.args",
219
- value: ["--new", "--flags"],
214
+ key: "agents.hermes.host",
215
+ value: "http://10.0.0.1:7900",
220
216
  });
221
- const updated = await cmdConfigGet(tempDir, "agents.hermes.args");
222
- expect(updated).toEqual(["--new", "--flags"]);
217
+ const updated = await cmdConfigGet(tempDir, "agents.hermes.host");
218
+ expect(updated).toBe("http://10.0.0.1:7900");
223
219
  } finally {
224
220
  rmSync(tempDir, { recursive: true, force: true });
225
221
  }
@@ -230,8 +226,8 @@ defaultAgent: hermes
230
226
  try {
231
227
  createTestConfig(tempDir, sampleConfig);
232
228
  await cmdConfigSet(tempDir, "defaultAgent", "claude-code");
233
- const cmd = await cmdConfigGet(tempDir, "agents.hermes.command");
234
- expect(cmd).toBe("uwf-hermes");
229
+ const host = await cmdConfigGet(tempDir, "agents.hermes.host");
230
+ expect(host).toBe("http://127.0.0.1:7900");
235
231
  } finally {
236
232
  rmSync(tempDir, { recursive: true, force: true });
237
233
  }
@@ -260,29 +256,29 @@ defaultAgent: hermes
260
256
  }
261
257
  });
262
258
 
263
- test("throws error when array value is invalid JSON for args key", async () => {
259
+ test("throws error when value for unknown nested field is invalid", async () => {
264
260
  const tempDir = mkdtempSync(join(tmpdir(), "test-config-"));
265
261
  try {
266
262
  createTestConfig(tempDir, sampleConfig);
267
- await expect(
268
- cmdConfigSet(tempDir, "agents.hermes.args", "[invalid json"),
269
- ).rejects.toThrow();
263
+ await expect(cmdConfigSet(tempDir, "agents.hermes.args", "[invalid json")).rejects.toThrow(
264
+ /Unknown field/,
265
+ );
270
266
  } finally {
271
267
  rmSync(tempDir, { recursive: true, force: true });
272
268
  }
273
269
  });
274
270
 
275
- test("sets agent command (agents.claude-code.command)", async () => {
271
+ test("sets agent gateway (agents.claude-code.gateway)", async () => {
276
272
  const tempDir = mkdtempSync(join(tmpdir(), "test-config-"));
277
273
  try {
278
274
  createTestConfig(tempDir, sampleConfig);
279
- const result = await cmdConfigSet(tempDir, "agents.claude-code.command", "new-command");
275
+ const result = await cmdConfigSet(tempDir, "agents.claude-code.gateway", "new-gateway");
280
276
  expect(result).toEqual({
281
- key: "agents.claude-code.command",
282
- value: "new-command",
277
+ key: "agents.claude-code.gateway",
278
+ value: "new-gateway",
283
279
  });
284
- const updated = await cmdConfigGet(tempDir, "agents.claude-code.command");
285
- expect(updated).toBe("new-command");
280
+ const updated = await cmdConfigGet(tempDir, "agents.claude-code.gateway");
281
+ expect(updated).toBe("new-gateway");
286
282
  } finally {
287
283
  rmSync(tempDir, { recursive: true, force: true });
288
284
  }
@@ -392,12 +388,12 @@ defaultAgent: hermes
392
388
  const tempDir = mkdtempSync(join(tmpdir(), "test-config-"));
393
389
  try {
394
390
  createTestConfig(tempDir, sampleConfig);
395
- await cmdConfigSet(tempDir, "agents.hermes.command", "uwf-hermes");
396
- await cmdConfigSet(tempDir, "agents.hermes.args", '["--flag"]');
397
- const command = await cmdConfigGet(tempDir, "agents.hermes.command");
398
- const args = await cmdConfigGet(tempDir, "agents.hermes.args");
399
- expect(command).toBe("uwf-hermes");
400
- expect(args).toEqual(["--flag"]);
391
+ await cmdConfigSet(tempDir, "agents.hermes.host", "http://example:7900");
392
+ await cmdConfigSet(tempDir, "agents.hermes.gateway", "hermes-gw");
393
+ const host = await cmdConfigGet(tempDir, "agents.hermes.host");
394
+ const gateway = await cmdConfigGet(tempDir, "agents.hermes.gateway");
395
+ expect(host).toBe("http://example:7900");
396
+ expect(gateway).toBe("hermes-gw");
401
397
  } finally {
402
398
  rmSync(tempDir, { recursive: true, force: true });
403
399
  }