@united-workforce/cli 0.7.0 → 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 (111) hide show
  1. package/README.md +32 -5
  2. package/dist/.build-fingerprint +1 -0
  3. package/dist/__tests__/broker-step-active-turns.test.d.ts +20 -0
  4. package/dist/__tests__/broker-step-active-turns.test.d.ts.map +1 -0
  5. package/dist/__tests__/broker-step-active-turns.test.js +428 -0
  6. package/dist/__tests__/broker-step-active-turns.test.js.map +1 -0
  7. package/dist/__tests__/broker-step-turn-chain-phase2.test.d.ts +13 -0
  8. package/dist/__tests__/broker-step-turn-chain-phase2.test.d.ts.map +1 -0
  9. package/dist/__tests__/broker-step-turn-chain-phase2.test.js +429 -0
  10. package/dist/__tests__/broker-step-turn-chain-phase2.test.js.map +1 -0
  11. package/dist/__tests__/e2e-broker-step-suspend.test.d.ts +18 -0
  12. package/dist/__tests__/e2e-broker-step-suspend.test.d.ts.map +1 -0
  13. package/dist/__tests__/e2e-broker-step-suspend.test.js +313 -0
  14. package/dist/__tests__/e2e-broker-step-suspend.test.js.map +1 -0
  15. package/dist/__tests__/e2e-thread-resume-timeout-suspend.test.d.ts +28 -0
  16. package/dist/__tests__/e2e-thread-resume-timeout-suspend.test.d.ts.map +1 -0
  17. package/dist/__tests__/e2e-thread-resume-timeout-suspend.test.js +322 -0
  18. package/dist/__tests__/e2e-thread-resume-timeout-suspend.test.js.map +1 -0
  19. package/dist/__tests__/log-tag-validity.test.d.ts +2 -0
  20. package/dist/__tests__/log-tag-validity.test.d.ts.map +1 -0
  21. package/dist/__tests__/log-tag-validity.test.js +110 -0
  22. package/dist/__tests__/log-tag-validity.test.js.map +1 -0
  23. package/dist/__tests__/setup-agent-discovery.test.js +23 -23
  24. package/dist/__tests__/setup-agent-discovery.test.js.map +1 -1
  25. package/dist/__tests__/step-show-json.test.js +5 -5
  26. package/dist/__tests__/step-show-json.test.js.map +1 -1
  27. package/dist/__tests__/step-show-text.test.d.ts +2 -0
  28. package/dist/__tests__/step-show-text.test.d.ts.map +1 -0
  29. package/dist/__tests__/step-show-text.test.js +192 -0
  30. package/dist/__tests__/step-show-text.test.js.map +1 -0
  31. package/dist/__tests__/step-turns-cli-subprocess.test.d.ts +21 -0
  32. package/dist/__tests__/step-turns-cli-subprocess.test.d.ts.map +1 -0
  33. package/dist/__tests__/step-turns-cli-subprocess.test.js +356 -0
  34. package/dist/__tests__/step-turns-cli-subprocess.test.js.map +1 -0
  35. package/dist/__tests__/step-turns-panorama-phase3.test.d.ts +21 -0
  36. package/dist/__tests__/step-turns-panorama-phase3.test.d.ts.map +1 -0
  37. package/dist/__tests__/step-turns-panorama-phase3.test.js +476 -0
  38. package/dist/__tests__/step-turns-panorama-phase3.test.js.map +1 -0
  39. package/dist/__tests__/step-turns.test.d.ts +24 -0
  40. package/dist/__tests__/step-turns.test.d.ts.map +1 -0
  41. package/dist/__tests__/step-turns.test.js +646 -0
  42. package/dist/__tests__/step-turns.test.js.map +1 -0
  43. package/dist/__tests__/store-turn-chain.test.d.ts +2 -0
  44. package/dist/__tests__/store-turn-chain.test.d.ts.map +1 -0
  45. package/dist/__tests__/store-turn-chain.test.js +341 -0
  46. package/dist/__tests__/store-turn-chain.test.js.map +1 -0
  47. package/dist/__tests__/thread-list-limit-offset.test.d.ts +24 -0
  48. package/dist/__tests__/thread-list-limit-offset.test.d.ts.map +1 -0
  49. package/dist/__tests__/thread-list-limit-offset.test.js +254 -0
  50. package/dist/__tests__/thread-list-limit-offset.test.js.map +1 -0
  51. package/dist/__tests__/thread-list-template-ms-date.test.js +7 -2
  52. package/dist/__tests__/thread-list-template-ms-date.test.js.map +1 -1
  53. package/dist/__tests__/thread.test.js +28 -14
  54. package/dist/__tests__/thread.test.js.map +1 -1
  55. package/dist/cli.js +910 -344
  56. package/dist/cli.js.map +1 -1
  57. package/dist/commands/broker-step.d.ts +10 -3
  58. package/dist/commands/broker-step.d.ts.map +1 -1
  59. package/dist/commands/broker-step.js +231 -27
  60. package/dist/commands/broker-step.js.map +1 -1
  61. package/dist/commands/prompt.d.ts.map +1 -1
  62. package/dist/commands/prompt.js +42 -50
  63. package/dist/commands/prompt.js.map +1 -1
  64. package/dist/commands/setup.d.ts +6 -4
  65. package/dist/commands/setup.d.ts.map +1 -1
  66. package/dist/commands/setup.js +16 -26
  67. package/dist/commands/setup.js.map +1 -1
  68. package/dist/commands/step.d.ts +48 -1
  69. package/dist/commands/step.d.ts.map +1 -1
  70. package/dist/commands/step.js +496 -3
  71. package/dist/commands/step.js.map +1 -1
  72. package/dist/output-mappers.d.ts +8 -0
  73. package/dist/output-mappers.d.ts.map +1 -1
  74. package/dist/output-mappers.js +72 -18
  75. package/dist/output-mappers.js.map +1 -1
  76. package/dist/schemas.d.ts +3 -0
  77. package/dist/schemas.d.ts.map +1 -1
  78. package/dist/schemas.js +17 -3
  79. package/dist/schemas.js.map +1 -1
  80. package/dist/store.d.ts +147 -1
  81. package/dist/store.d.ts.map +1 -1
  82. package/dist/store.js +254 -1
  83. package/dist/store.js.map +1 -1
  84. package/dist/text-renderers.d.ts.map +1 -1
  85. package/dist/text-renderers.js +27 -2
  86. package/dist/text-renderers.js.map +1 -1
  87. package/package.json +7 -6
  88. package/src/__tests__/broker-step-active-turns.test.ts +509 -0
  89. package/src/__tests__/broker-step-turn-chain-phase2.test.ts +525 -0
  90. package/src/__tests__/e2e-broker-step-suspend.test.ts +351 -0
  91. package/src/__tests__/e2e-thread-resume-timeout-suspend.test.ts +360 -0
  92. package/src/__tests__/log-tag-validity.test.ts +124 -0
  93. package/src/__tests__/setup-agent-discovery.test.ts +23 -23
  94. package/src/__tests__/step-show-json.test.ts +5 -5
  95. package/src/__tests__/step-show-text.test.ts +236 -0
  96. package/src/__tests__/step-turns-cli-subprocess.test.ts +411 -0
  97. package/src/__tests__/step-turns-panorama-phase3.test.ts +579 -0
  98. package/src/__tests__/step-turns.test.ts +734 -0
  99. package/src/__tests__/store-turn-chain.test.ts +386 -0
  100. package/src/__tests__/thread-list-limit-offset.test.ts +305 -0
  101. package/src/__tests__/thread-list-template-ms-date.test.ts +7 -2
  102. package/src/__tests__/thread.test.ts +29 -15
  103. package/src/cli.ts +1056 -483
  104. package/src/commands/broker-step.ts +315 -38
  105. package/src/commands/prompt.ts +42 -50
  106. package/src/commands/setup.ts +16 -28
  107. package/src/commands/step.ts +655 -3
  108. package/src/output-mappers.ts +99 -21
  109. package/src/schemas.ts +32 -2
  110. package/src/store.ts +297 -2
  111. package/src/text-renderers.ts +35 -2
@@ -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
+ });