@juicesharp/rpiv-workflow 1.17.1 → 1.18.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.
- package/README.md +56 -15
- package/api.ts +1 -1
- package/audit.ts +42 -7
- package/built-ins.ts +46 -2
- package/command-run.ts +132 -0
- package/command.ts +29 -100
- package/docs/workflow-authoring.md +23 -23
- package/extension.ts +18 -0
- package/fanout.ts +11 -5
- package/host.ts +26 -13
- package/index.ts +40 -134
- package/internal-utils.ts +35 -0
- package/internal.ts +1 -1
- package/iterate.ts +20 -8
- package/lifecycle.ts +1 -1
- package/load/index.ts +5 -1
- package/messages.ts +34 -0
- package/package.json +9 -3
- package/registration.ts +113 -0
- package/runner/by-name.test.ts +95 -0
- package/runner/by-name.ts +76 -0
- package/runner/by-run-id.test.ts +115 -0
- package/runner/by-run-id.ts +92 -0
- package/runner/chain-advance.ts +22 -42
- package/runner/index.ts +10 -1
- package/runner/resume-fanout.test.ts +331 -0
- package/runner/resume-fanout.ts +136 -0
- package/runner/resume-iterate.test.ts +444 -0
- package/runner/resume-iterate.ts +110 -0
- package/runner/resume.test.ts +1130 -0
- package/runner/resume.ts +333 -0
- package/runner/runner.ts +281 -91
- package/runner/script-stage.ts +18 -51
- package/runner/stage-lifecycle.ts +35 -27
- package/sessions/extraction.ts +4 -4
- package/sessions/sessions.ts +22 -40
- package/sessions/spawn.ts +15 -11
- package/startup.ts +9 -0
- package/state/index.ts +1 -0
- package/state/reads.ts +20 -0
- package/state/state.ts +1 -0
- package/types.ts +18 -13
|
@@ -0,0 +1,1130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reconstruction tests for `reconstructState` — the pure fold that rebuilds
|
|
3
|
+
* `RunState` from a run's JSONL audit trail.
|
|
4
|
+
*
|
|
5
|
+
* Covers 8 cases:
|
|
6
|
+
* 1. Produces-only linear chain (primary + named + output + counters)
|
|
7
|
+
* 2. Side-effect stage between produces (primary preserved)
|
|
8
|
+
* 3. Terminal stage (primary cleared)
|
|
9
|
+
* 4. Failed trailing row (output NOT seeded; stagesCompleted excludes it;
|
|
10
|
+
* visited + lastStageNumber include it)
|
|
11
|
+
* 5. Empty file → no-rows refusal
|
|
12
|
+
* 6. Stage gone from workflow → stage-gone refusal
|
|
13
|
+
* 7. Decorated fanout unit rows → counters-only fold + fanoutProgress;
|
|
14
|
+
* decorated iterate unit rows → full-produces fold + iterateProgress
|
|
15
|
+
* (rolling primary, named accumulation, generation reset)
|
|
16
|
+
* 8. Named-slot append-order (array history preserved across repeated calls)
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
20
|
+
import { tmpdir } from "node:os";
|
|
21
|
+
import { join } from "node:path";
|
|
22
|
+
import { createMockSessionChain, mockAssistantMessage } from "@juicesharp/rpiv-test-utils";
|
|
23
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
24
|
+
import type { Workflow } from "../api.js";
|
|
25
|
+
import type { Artifact } from "../handle.js";
|
|
26
|
+
import { fs as fsHandle } from "../handle.js";
|
|
27
|
+
import type { Output } from "../output.js";
|
|
28
|
+
import {
|
|
29
|
+
appendStage,
|
|
30
|
+
readAllStages,
|
|
31
|
+
stateFilePath,
|
|
32
|
+
type WorkflowHeader,
|
|
33
|
+
type WorkflowStage,
|
|
34
|
+
writeHeader,
|
|
35
|
+
} from "../state/index.js";
|
|
36
|
+
import { reconstructState } from "./resume.js";
|
|
37
|
+
import { resumeWorkflow } from "./runner.js";
|
|
38
|
+
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Fixtures
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
const fakeArtifact = (path: string): Artifact => ({ handle: fsHandle(path), role: "primary" });
|
|
44
|
+
|
|
45
|
+
const fakeOutput = (artifacts: readonly Artifact[] = []): Output => ({
|
|
46
|
+
kind: "artifacts",
|
|
47
|
+
artifacts,
|
|
48
|
+
data: {},
|
|
49
|
+
meta: { stage: "test", stageNumber: 1, ts: "", runId: "" },
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Minimal named outcome for fold tests. The collector is never invoked — the
|
|
54
|
+
* reconstruct fold replays persisted rows, not live sessions — so a stub that
|
|
55
|
+
* satisfies `OutputSpec` is sufficient. `name` drives `resolvePublishName`.
|
|
56
|
+
*/
|
|
57
|
+
const makeOutcome = (
|
|
58
|
+
name: string,
|
|
59
|
+
): import("../output.js").OutputSpec<unknown, "artifact-md", Record<string, unknown>> => ({
|
|
60
|
+
name,
|
|
61
|
+
collector: { collect: () => ({ kind: "ok", artifacts: [] }) },
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
/** Minimal 3-stage produces workflow used by most tests. */
|
|
65
|
+
const linearWorkflow: Workflow = {
|
|
66
|
+
name: "test-wf",
|
|
67
|
+
start: "plan",
|
|
68
|
+
stages: {
|
|
69
|
+
plan: { kind: "produces", sessionPolicy: "fresh" },
|
|
70
|
+
build: { kind: "produces", sessionPolicy: "fresh" },
|
|
71
|
+
deploy: { kind: "produces", sessionPolicy: "fresh" },
|
|
72
|
+
},
|
|
73
|
+
edges: {
|
|
74
|
+
plan: "build",
|
|
75
|
+
build: "deploy",
|
|
76
|
+
deploy: "stop",
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/** Workflow with a side-effect stage between two produces. */
|
|
81
|
+
const sideEffectWorkflow: Workflow = {
|
|
82
|
+
name: "test-wf",
|
|
83
|
+
start: "plan",
|
|
84
|
+
stages: {
|
|
85
|
+
plan: { kind: "produces", sessionPolicy: "fresh" },
|
|
86
|
+
commit: { kind: "side-effect", sessionPolicy: "fresh" },
|
|
87
|
+
build: { kind: "produces", sessionPolicy: "fresh" },
|
|
88
|
+
},
|
|
89
|
+
edges: {
|
|
90
|
+
plan: "commit",
|
|
91
|
+
commit: "build",
|
|
92
|
+
build: "stop",
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** Workflow with a terminal stage. */
|
|
97
|
+
const terminalWorkflow: Workflow = {
|
|
98
|
+
name: "test-wf",
|
|
99
|
+
start: "plan",
|
|
100
|
+
stages: {
|
|
101
|
+
plan: { kind: "produces", sessionPolicy: "fresh" },
|
|
102
|
+
cleanup: { kind: "side-effect", sessionPolicy: "fresh", inheritsArtifacts: false },
|
|
103
|
+
},
|
|
104
|
+
edges: {
|
|
105
|
+
plan: "cleanup",
|
|
106
|
+
cleanup: "stop",
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const baseHeader: WorkflowHeader = {
|
|
111
|
+
runId: "2026-06-03_07-30-00-ab12",
|
|
112
|
+
workflow: "test-wf",
|
|
113
|
+
input: "Add dark mode",
|
|
114
|
+
ts: "2026-06-03T07:30:00Z",
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
// Test harness
|
|
119
|
+
// ---------------------------------------------------------------------------
|
|
120
|
+
|
|
121
|
+
let tmpDir: string;
|
|
122
|
+
|
|
123
|
+
beforeEach(() => {
|
|
124
|
+
tmpDir = mkdtempSync(join(tmpdir(), "rpiv-workflow-resume-"));
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
afterEach(() => {
|
|
128
|
+
rmSync(tmpDir, { recursive: true, force: true });
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
function writeRunStages(stages: WorkflowStage[]): WorkflowHeader {
|
|
132
|
+
writeHeader(tmpDir, baseHeader);
|
|
133
|
+
for (const stage of stages) {
|
|
134
|
+
appendStage(tmpDir, baseHeader.runId, stage);
|
|
135
|
+
}
|
|
136
|
+
return baseHeader;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ---------------------------------------------------------------------------
|
|
140
|
+
// Cases
|
|
141
|
+
// ---------------------------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
describe("reconstructState", () => {
|
|
144
|
+
it("produces-only linear chain: advances primary, appends named, tracks counters", () => {
|
|
145
|
+
const art1 = fakeArtifact("plans/p1.md");
|
|
146
|
+
const art2 = fakeArtifact("plans/p2.md");
|
|
147
|
+
const out1 = fakeOutput([art1]);
|
|
148
|
+
const out2 = fakeOutput([art2]);
|
|
149
|
+
|
|
150
|
+
writeRunStages([
|
|
151
|
+
{
|
|
152
|
+
stageNumber: 1,
|
|
153
|
+
stage: "plan",
|
|
154
|
+
skill: "plan",
|
|
155
|
+
status: "completed",
|
|
156
|
+
ts: "2026-06-03T07:31:00Z",
|
|
157
|
+
output: out1,
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
stageNumber: 2,
|
|
161
|
+
stage: "build",
|
|
162
|
+
skill: "build",
|
|
163
|
+
status: "completed",
|
|
164
|
+
ts: "2026-06-03T07:35:00Z",
|
|
165
|
+
output: out2,
|
|
166
|
+
},
|
|
167
|
+
]);
|
|
168
|
+
|
|
169
|
+
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
170
|
+
|
|
171
|
+
expect(result.ok).toBe(true);
|
|
172
|
+
if (!result.ok) return; // type narrow
|
|
173
|
+
|
|
174
|
+
expect(result.state.primaryArtifact).toStrictEqual(art2);
|
|
175
|
+
expect(result.state.output).toStrictEqual(out2);
|
|
176
|
+
expect(result.state.stagesCompleted).toBe(2);
|
|
177
|
+
expect(result.state.lastAllocatedStageNumber).toBe(2);
|
|
178
|
+
expect(result.state.named.plan).toEqual([out1]);
|
|
179
|
+
expect(result.state.named.build).toEqual([out2]);
|
|
180
|
+
expect(result.state.originalInput).toBe("Add dark mode");
|
|
181
|
+
expect(result.visited).toEqual(new Set(["plan", "build"]));
|
|
182
|
+
expect(result.lastStageNumber).toBe(2);
|
|
183
|
+
expect(result.rows).toHaveLength(2);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("side-effect stage between produces: primary preserved, named untouched by side-effect", () => {
|
|
187
|
+
const art1 = fakeArtifact("plans/p1.md");
|
|
188
|
+
const out1 = fakeOutput([art1]);
|
|
189
|
+
const sideOut = fakeOutput([]); // side-effect output, no artifacts
|
|
190
|
+
|
|
191
|
+
writeRunStages([
|
|
192
|
+
{
|
|
193
|
+
stageNumber: 1,
|
|
194
|
+
stage: "plan",
|
|
195
|
+
skill: "plan",
|
|
196
|
+
status: "completed",
|
|
197
|
+
ts: "2026-06-03T07:31:00Z",
|
|
198
|
+
output: out1,
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
stageNumber: 2,
|
|
202
|
+
stage: "commit",
|
|
203
|
+
skill: "commit",
|
|
204
|
+
status: "completed",
|
|
205
|
+
ts: "2026-06-03T07:32:00Z",
|
|
206
|
+
output: sideOut,
|
|
207
|
+
},
|
|
208
|
+
]);
|
|
209
|
+
|
|
210
|
+
const result = reconstructState(tmpDir, sideEffectWorkflow, baseHeader);
|
|
211
|
+
|
|
212
|
+
expect(result.ok).toBe(true);
|
|
213
|
+
if (!result.ok) return;
|
|
214
|
+
|
|
215
|
+
// Primary stays on the produces artifact
|
|
216
|
+
expect(result.state.primaryArtifact).toStrictEqual(art1);
|
|
217
|
+
expect(result.state.output).toStrictEqual(sideOut);
|
|
218
|
+
expect(result.state.stagesCompleted).toBe(2);
|
|
219
|
+
// Side-effect stage did NOT write to named
|
|
220
|
+
expect(result.state.named.plan).toEqual([out1]);
|
|
221
|
+
expect(result.state.named.commit).toBeUndefined();
|
|
222
|
+
expect(result.visited).toEqual(new Set(["plan", "commit"]));
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("terminal stage (inheritsArtifacts: false): clears primary", () => {
|
|
226
|
+
const art1 = fakeArtifact("plans/p1.md");
|
|
227
|
+
const out1 = fakeOutput([art1]);
|
|
228
|
+
const termOut = fakeOutput([]);
|
|
229
|
+
|
|
230
|
+
writeRunStages([
|
|
231
|
+
{
|
|
232
|
+
stageNumber: 1,
|
|
233
|
+
stage: "plan",
|
|
234
|
+
skill: "plan",
|
|
235
|
+
status: "completed",
|
|
236
|
+
ts: "2026-06-03T07:31:00Z",
|
|
237
|
+
output: out1,
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
stageNumber: 2,
|
|
241
|
+
stage: "cleanup",
|
|
242
|
+
status: "completed",
|
|
243
|
+
ts: "2026-06-03T07:32:00Z",
|
|
244
|
+
output: termOut,
|
|
245
|
+
},
|
|
246
|
+
]);
|
|
247
|
+
|
|
248
|
+
const result = reconstructState(tmpDir, terminalWorkflow, baseHeader);
|
|
249
|
+
|
|
250
|
+
expect(result.ok).toBe(true);
|
|
251
|
+
if (!result.ok) return;
|
|
252
|
+
|
|
253
|
+
expect(result.state.primaryArtifact).toBeUndefined();
|
|
254
|
+
expect(result.state.stagesCompleted).toBe(2);
|
|
255
|
+
// Named is never cleared by terminal
|
|
256
|
+
expect(result.state.named.plan).toEqual([out1]);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("failed trailing row: output NOT seeded, stagesCompleted excludes it, visited + lastStageNumber include it", () => {
|
|
260
|
+
const art1 = fakeArtifact("plans/p1.md");
|
|
261
|
+
const out1 = fakeOutput([art1]);
|
|
262
|
+
|
|
263
|
+
writeRunStages([
|
|
264
|
+
{
|
|
265
|
+
stageNumber: 1,
|
|
266
|
+
stage: "plan",
|
|
267
|
+
skill: "plan",
|
|
268
|
+
status: "completed",
|
|
269
|
+
ts: "2026-06-03T07:31:00Z",
|
|
270
|
+
output: out1,
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
stageNumber: 2,
|
|
274
|
+
stage: "build",
|
|
275
|
+
skill: "build",
|
|
276
|
+
status: "failed",
|
|
277
|
+
ts: "2026-06-03T07:35:00Z",
|
|
278
|
+
errMsg: "Something went wrong",
|
|
279
|
+
},
|
|
280
|
+
]);
|
|
281
|
+
|
|
282
|
+
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
283
|
+
|
|
284
|
+
expect(result.ok).toBe(true);
|
|
285
|
+
if (!result.ok) return;
|
|
286
|
+
|
|
287
|
+
// Only the completed stage counts
|
|
288
|
+
expect(result.state.stagesCompleted).toBe(1);
|
|
289
|
+
// Output is from the completed row only
|
|
290
|
+
expect(result.state.output).toStrictEqual(out1);
|
|
291
|
+
// Primary is from the completed row only
|
|
292
|
+
expect(result.state.primaryArtifact).toStrictEqual(art1);
|
|
293
|
+
// lastStageNumber INCLUDES the failed row
|
|
294
|
+
expect(result.lastStageNumber).toBe(2);
|
|
295
|
+
expect(result.state.lastAllocatedStageNumber).toBe(2);
|
|
296
|
+
// visited INCLUDES the failed row
|
|
297
|
+
expect(result.visited).toEqual(new Set(["plan", "build"]));
|
|
298
|
+
// named only has the completed stage
|
|
299
|
+
expect(result.state.named.plan).toEqual([out1]);
|
|
300
|
+
expect(result.state.named.build).toBeUndefined();
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("empty file (no stage rows): returns no-rows refusal", () => {
|
|
304
|
+
// Write header only, no stage rows
|
|
305
|
+
writeHeader(tmpDir, baseHeader);
|
|
306
|
+
|
|
307
|
+
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
308
|
+
|
|
309
|
+
expect(result.ok).toBe(false);
|
|
310
|
+
if (result.ok) return;
|
|
311
|
+
expect(result.reason).toBe("no-rows");
|
|
312
|
+
expect(result.detail).toBe(baseHeader.runId);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it("row whose stage is not in workflow.stages: returns stage-gone refusal", () => {
|
|
316
|
+
writeRunStages([
|
|
317
|
+
{
|
|
318
|
+
stageNumber: 1,
|
|
319
|
+
stage: "plan",
|
|
320
|
+
skill: "plan",
|
|
321
|
+
status: "completed",
|
|
322
|
+
ts: "2026-06-03T07:31:00Z",
|
|
323
|
+
},
|
|
324
|
+
{
|
|
325
|
+
stageNumber: 2,
|
|
326
|
+
stage: "renamed-away",
|
|
327
|
+
skill: "renamed-away",
|
|
328
|
+
status: "completed",
|
|
329
|
+
ts: "2026-06-03T07:35:00Z",
|
|
330
|
+
},
|
|
331
|
+
]);
|
|
332
|
+
|
|
333
|
+
// linearWorkflow only has plan, build, deploy — no "renamed-away"
|
|
334
|
+
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
335
|
+
|
|
336
|
+
expect(result.ok).toBe(false);
|
|
337
|
+
if (result.ok) return;
|
|
338
|
+
expect(result.reason).toBe("stage-gone");
|
|
339
|
+
expect(result.detail).toBe("renamed-away");
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("decorated fanout unit rows: folds counters-only, tracks fanoutProgress, no primary mutation", () => {
|
|
343
|
+
const planArt = fakeArtifact("plans/p1.md");
|
|
344
|
+
const planOut = fakeOutput([planArt]);
|
|
345
|
+
// Workflow: plan (produces) -> build (fanout). Build units are decorated rows.
|
|
346
|
+
const wf: Workflow = {
|
|
347
|
+
name: "test-wf",
|
|
348
|
+
start: "plan",
|
|
349
|
+
stages: {
|
|
350
|
+
plan: { kind: "produces", sessionPolicy: "fresh" },
|
|
351
|
+
build: { kind: "produces", sessionPolicy: "fresh", fanout: () => [] },
|
|
352
|
+
},
|
|
353
|
+
edges: { plan: "build", build: "stop" },
|
|
354
|
+
} as Workflow;
|
|
355
|
+
|
|
356
|
+
writeRunStages([
|
|
357
|
+
{ stageNumber: 1, stage: "plan", skill: "plan", status: "completed", ts: "t1", output: planOut },
|
|
358
|
+
{ stageNumber: 2, stage: "build (phase 1/2)", skill: "build", status: "completed", ts: "t2" },
|
|
359
|
+
{ stageNumber: 3, stage: "build (phase 2/2)", skill: "build", status: "completed", ts: "t3" },
|
|
360
|
+
]);
|
|
361
|
+
|
|
362
|
+
const result = reconstructState(tmpDir, wf, baseHeader);
|
|
363
|
+
expect(result.ok).toBe(true);
|
|
364
|
+
if (!result.ok) return;
|
|
365
|
+
|
|
366
|
+
// Counters advanced for every completed row (1 plan + 2 units)...
|
|
367
|
+
expect(result.state.stagesCompleted).toBe(3);
|
|
368
|
+
// ...but the fanout units left the primary on the pre-fanout artifact.
|
|
369
|
+
expect(result.state.primaryArtifact).toStrictEqual(planArt);
|
|
370
|
+
// Parent is visited; the decorated keys are not.
|
|
371
|
+
expect(result.visited).toEqual(new Set(["plan", "build"]));
|
|
372
|
+
// fanoutProgress carries the completed decorated strings in order.
|
|
373
|
+
expect(result.fanoutProgress.get("build")).toEqual(["build (phase 1/2)", "build (phase 2/2)"]);
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it("failed fanout unit is excluded from fanoutProgress (the k+1 resume point)", () => {
|
|
377
|
+
const wf: Workflow = {
|
|
378
|
+
name: "test-wf",
|
|
379
|
+
start: "build",
|
|
380
|
+
stages: { build: { kind: "produces", sessionPolicy: "fresh", fanout: () => [] } },
|
|
381
|
+
edges: { build: "stop" },
|
|
382
|
+
} as Workflow;
|
|
383
|
+
writeRunStages([
|
|
384
|
+
{ stageNumber: 1, stage: "build (phase 1/3)", skill: "build", status: "completed", ts: "t1" },
|
|
385
|
+
{ stageNumber: 2, stage: "build (phase 2/3)", skill: "build", status: "failed", ts: "t2", errMsg: "boom" },
|
|
386
|
+
]);
|
|
387
|
+
const result = reconstructState(tmpDir, wf, baseHeader);
|
|
388
|
+
expect(result.ok).toBe(true);
|
|
389
|
+
if (!result.ok) return;
|
|
390
|
+
expect(result.state.stagesCompleted).toBe(1);
|
|
391
|
+
expect(result.fanoutProgress.get("build")).toEqual(["build (phase 1/3)"]);
|
|
392
|
+
expect(result.visited).toEqual(new Set(["build"]));
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it("looped fanout: a second generation resets fanoutProgress to the trailing pass; stagesCompleted stays cumulative", () => {
|
|
396
|
+
// build (fanout) -> review (produces) -> build (fanout, gen 2). Units are decorated rows.
|
|
397
|
+
const wf: Workflow = {
|
|
398
|
+
name: "test-wf",
|
|
399
|
+
start: "build",
|
|
400
|
+
stages: {
|
|
401
|
+
build: { kind: "produces", sessionPolicy: "fresh", fanout: () => [] },
|
|
402
|
+
review: { kind: "produces", sessionPolicy: "fresh" },
|
|
403
|
+
},
|
|
404
|
+
edges: { build: "review", review: "build" },
|
|
405
|
+
} as Workflow;
|
|
406
|
+
writeRunStages([
|
|
407
|
+
{ stageNumber: 1, stage: "build (a)", skill: "build", status: "completed", ts: "t1" },
|
|
408
|
+
{ stageNumber: 2, stage: "build (b)", skill: "build", status: "completed", ts: "t2" },
|
|
409
|
+
{
|
|
410
|
+
stageNumber: 3,
|
|
411
|
+
stage: "review",
|
|
412
|
+
skill: "review",
|
|
413
|
+
status: "completed",
|
|
414
|
+
ts: "t3",
|
|
415
|
+
output: fakeOutput([fakeArtifact("reviews/r1.md")]),
|
|
416
|
+
},
|
|
417
|
+
// gen 2 — non-contiguous with gen 1 (a review row broke contiguity).
|
|
418
|
+
{ stageNumber: 4, stage: "build (a)", skill: "build", status: "completed", ts: "t4" },
|
|
419
|
+
{ stageNumber: 5, stage: "build (b)", skill: "build", status: "failed", ts: "t5", errMsg: "boom" },
|
|
420
|
+
]);
|
|
421
|
+
const result = reconstructState(tmpDir, wf, baseHeader);
|
|
422
|
+
expect(result.ok).toBe(true);
|
|
423
|
+
if (!result.ok) return;
|
|
424
|
+
// fanoutProgress holds ONLY the trailing generation's completed prefix (gen 2's "build (a)").
|
|
425
|
+
expect(result.fanoutProgress.get("build")).toEqual(["build (a)"]);
|
|
426
|
+
// stagesCompleted counts ALL completed units across both generations (2 + 1) + the review.
|
|
427
|
+
expect(result.state.stagesCompleted).toBe(4);
|
|
428
|
+
expect(result.visited).toEqual(new Set(["build", "review"]));
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it("decorated iterate unit rows: full-produces fold — rolls primary, accumulates named, builds iterateProgress", () => {
|
|
432
|
+
const reviewArt = fakeArtifact("reviews/r1.md");
|
|
433
|
+
const plan1 = fakeArtifact("plans/p1.md");
|
|
434
|
+
const plan2 = fakeArtifact("plans/p2.md");
|
|
435
|
+
// review (produces) -> blueprint (iterate, produces "plans"). Units are decorated rows.
|
|
436
|
+
const wf: Workflow = {
|
|
437
|
+
name: "test-wf",
|
|
438
|
+
start: "review",
|
|
439
|
+
stages: {
|
|
440
|
+
review: { kind: "produces", sessionPolicy: "fresh", outcome: makeOutcome("reviews") },
|
|
441
|
+
blueprint: { kind: "produces", sessionPolicy: "fresh", outcome: makeOutcome("plans"), iterate: () => null },
|
|
442
|
+
},
|
|
443
|
+
edges: { review: "blueprint", blueprint: "stop" },
|
|
444
|
+
} as Workflow;
|
|
445
|
+
|
|
446
|
+
writeRunStages([
|
|
447
|
+
{
|
|
448
|
+
stageNumber: 1,
|
|
449
|
+
stage: "review",
|
|
450
|
+
skill: "review",
|
|
451
|
+
status: "completed",
|
|
452
|
+
ts: "t1",
|
|
453
|
+
output: fakeOutput([reviewArt]),
|
|
454
|
+
},
|
|
455
|
+
{
|
|
456
|
+
stageNumber: 2,
|
|
457
|
+
stage: "blueprint (phase-1)",
|
|
458
|
+
skill: "blueprint",
|
|
459
|
+
status: "completed",
|
|
460
|
+
ts: "t2",
|
|
461
|
+
output: fakeOutput([plan1]),
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
stageNumber: 3,
|
|
465
|
+
stage: "blueprint (phase-2)",
|
|
466
|
+
skill: "blueprint",
|
|
467
|
+
status: "completed",
|
|
468
|
+
ts: "t3",
|
|
469
|
+
output: fakeOutput([plan2]),
|
|
470
|
+
},
|
|
471
|
+
]);
|
|
472
|
+
|
|
473
|
+
const result = reconstructState(tmpDir, wf, baseHeader);
|
|
474
|
+
expect(result.ok).toBe(true);
|
|
475
|
+
if (!result.ok) return;
|
|
476
|
+
|
|
477
|
+
// Counters advanced for every completed row (1 review + 2 units).
|
|
478
|
+
expect(result.state.stagesCompleted).toBe(3);
|
|
479
|
+
// Primary ROLLED FORWARD to the last unit (unlike fanout, which leaves it on entry).
|
|
480
|
+
expect(result.state.primaryArtifact).toStrictEqual(plan2);
|
|
481
|
+
// state.named accumulated both plans under the outcome name.
|
|
482
|
+
expect(result.state.named.plans?.map((o) => o.artifacts[0])).toEqual([plan1, plan2]);
|
|
483
|
+
// Parent is visited; decorated keys are not.
|
|
484
|
+
expect(result.visited).toEqual(new Set(["review", "blueprint"]));
|
|
485
|
+
// iterateProgress: trailing generation's accumulated (both units) + FROZEN entry artifact (the review).
|
|
486
|
+
const point = result.iterateProgress.get("blueprint")!;
|
|
487
|
+
expect(point.entryArtifact).toStrictEqual(reviewArt);
|
|
488
|
+
expect(point.accumulated.map((o) => o.artifacts[0])).toEqual([plan1, plan2]);
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
it("failed iterate unit is excluded from accumulated (the re-pull point)", () => {
|
|
492
|
+
const wf: Workflow = {
|
|
493
|
+
name: "test-wf",
|
|
494
|
+
start: "blueprint",
|
|
495
|
+
stages: {
|
|
496
|
+
blueprint: { kind: "produces", sessionPolicy: "fresh", outcome: makeOutcome("plans"), iterate: () => null },
|
|
497
|
+
},
|
|
498
|
+
edges: { blueprint: "stop" },
|
|
499
|
+
} as Workflow;
|
|
500
|
+
writeRunStages([
|
|
501
|
+
{
|
|
502
|
+
stageNumber: 1,
|
|
503
|
+
stage: "blueprint (phase-1)",
|
|
504
|
+
skill: "blueprint",
|
|
505
|
+
status: "completed",
|
|
506
|
+
ts: "t1",
|
|
507
|
+
output: fakeOutput([fakeArtifact("plans/p1.md")]),
|
|
508
|
+
},
|
|
509
|
+
{
|
|
510
|
+
stageNumber: 2,
|
|
511
|
+
stage: "blueprint (phase-2)",
|
|
512
|
+
skill: "blueprint",
|
|
513
|
+
status: "failed",
|
|
514
|
+
ts: "t2",
|
|
515
|
+
errMsg: "boom",
|
|
516
|
+
},
|
|
517
|
+
]);
|
|
518
|
+
const result = reconstructState(tmpDir, wf, baseHeader);
|
|
519
|
+
expect(result.ok).toBe(true);
|
|
520
|
+
if (!result.ok) return;
|
|
521
|
+
expect(result.state.stagesCompleted).toBe(1);
|
|
522
|
+
expect(result.iterateProgress.get("blueprint")!.accumulated).toHaveLength(1);
|
|
523
|
+
expect(result.visited).toEqual(new Set(["blueprint"]));
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
it("corrective loop: a second iterate generation resets accumulated + entry artifact, named keeps both", () => {
|
|
527
|
+
const reviewArt = fakeArtifact("reviews/r1.md");
|
|
528
|
+
const codeReviewArt = fakeArtifact("reviews/cr1.md"); // the loop-back artifact that re-enters blueprint
|
|
529
|
+
const wf: Workflow = {
|
|
530
|
+
name: "test-wf",
|
|
531
|
+
start: "review",
|
|
532
|
+
stages: {
|
|
533
|
+
review: { kind: "produces", sessionPolicy: "fresh", outcome: makeOutcome("reviews") },
|
|
534
|
+
blueprint: { kind: "produces", sessionPolicy: "fresh", outcome: makeOutcome("plans"), iterate: () => null },
|
|
535
|
+
"code-review": { kind: "produces", sessionPolicy: "fresh", outcome: makeOutcome("reviews") },
|
|
536
|
+
},
|
|
537
|
+
edges: { review: "blueprint", blueprint: "code-review", "code-review": "blueprint" },
|
|
538
|
+
} as Workflow;
|
|
539
|
+
writeRunStages([
|
|
540
|
+
{
|
|
541
|
+
stageNumber: 1,
|
|
542
|
+
stage: "review",
|
|
543
|
+
skill: "review",
|
|
544
|
+
status: "completed",
|
|
545
|
+
ts: "t1",
|
|
546
|
+
output: fakeOutput([reviewArt]),
|
|
547
|
+
},
|
|
548
|
+
{
|
|
549
|
+
stageNumber: 2,
|
|
550
|
+
stage: "blueprint (phase-1)",
|
|
551
|
+
skill: "blueprint",
|
|
552
|
+
status: "completed",
|
|
553
|
+
ts: "t2",
|
|
554
|
+
output: fakeOutput([fakeArtifact("plans/g1p1.md")]),
|
|
555
|
+
},
|
|
556
|
+
{
|
|
557
|
+
stageNumber: 3,
|
|
558
|
+
stage: "code-review",
|
|
559
|
+
skill: "code-review",
|
|
560
|
+
status: "completed",
|
|
561
|
+
ts: "t3",
|
|
562
|
+
output: fakeOutput([codeReviewArt]),
|
|
563
|
+
},
|
|
564
|
+
// gen 2 starts here — non-contiguous with gen 1.
|
|
565
|
+
{
|
|
566
|
+
stageNumber: 4,
|
|
567
|
+
stage: "blueprint (phase-1)",
|
|
568
|
+
skill: "blueprint",
|
|
569
|
+
status: "completed",
|
|
570
|
+
ts: "t4",
|
|
571
|
+
output: fakeOutput([fakeArtifact("plans/g2p1.md")]),
|
|
572
|
+
},
|
|
573
|
+
]);
|
|
574
|
+
const result = reconstructState(tmpDir, wf, baseHeader);
|
|
575
|
+
expect(result.ok).toBe(true);
|
|
576
|
+
if (!result.ok) return;
|
|
577
|
+
const point = result.iterateProgress.get("blueprint")!;
|
|
578
|
+
// Trailing generation: only gen-2's single unit, entry artifact = the code-review (loop-back primary).
|
|
579
|
+
expect(point.accumulated).toHaveLength(1);
|
|
580
|
+
expect(point.entryArtifact).toStrictEqual(codeReviewArt);
|
|
581
|
+
// state.named.plans accumulated BOTH generations.
|
|
582
|
+
expect(result.state.named.plans).toHaveLength(2);
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
it("prefix-name collision: 'build (x)' matches build, not 'build-extra'", () => {
|
|
586
|
+
const wf: Workflow = {
|
|
587
|
+
name: "test-wf",
|
|
588
|
+
start: "build",
|
|
589
|
+
stages: {
|
|
590
|
+
build: { kind: "produces", sessionPolicy: "fresh", fanout: () => [] },
|
|
591
|
+
"build-extra": { kind: "produces", sessionPolicy: "fresh", fanout: () => [] },
|
|
592
|
+
},
|
|
593
|
+
edges: { build: "build-extra", "build-extra": "stop" },
|
|
594
|
+
} as Workflow;
|
|
595
|
+
writeRunStages([
|
|
596
|
+
{ stageNumber: 1, stage: "build (phase 1/1)", skill: "build", status: "completed", ts: "t1" },
|
|
597
|
+
{ stageNumber: 2, stage: "build-extra (phase 1/1)", skill: "build-extra", status: "completed", ts: "t2" },
|
|
598
|
+
]);
|
|
599
|
+
const result = reconstructState(tmpDir, wf, baseHeader);
|
|
600
|
+
expect(result.ok).toBe(true);
|
|
601
|
+
if (!result.ok) return;
|
|
602
|
+
expect(result.fanoutProgress.get("build")).toEqual(["build (phase 1/1)"]);
|
|
603
|
+
expect(result.fanoutProgress.get("build-extra")).toEqual(["build-extra (phase 1/1)"]);
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
it("named slot accumulates across completed rows for the same key (append order)", () => {
|
|
607
|
+
// Simulate a backward-jump loop: plan runs twice, producing two outputs.
|
|
608
|
+
const art1 = fakeArtifact("plans/p1.md");
|
|
609
|
+
const art2 = fakeArtifact("plans/p2.md");
|
|
610
|
+
const out1 = fakeOutput([art1]);
|
|
611
|
+
const out2 = fakeOutput([art2]);
|
|
612
|
+
|
|
613
|
+
writeRunStages([
|
|
614
|
+
{
|
|
615
|
+
stageNumber: 1,
|
|
616
|
+
stage: "plan",
|
|
617
|
+
skill: "plan",
|
|
618
|
+
status: "completed",
|
|
619
|
+
ts: "2026-06-03T07:31:00Z",
|
|
620
|
+
output: out1,
|
|
621
|
+
},
|
|
622
|
+
{
|
|
623
|
+
stageNumber: 2,
|
|
624
|
+
stage: "plan",
|
|
625
|
+
skill: "plan",
|
|
626
|
+
status: "completed",
|
|
627
|
+
ts: "2026-06-03T07:35:00Z",
|
|
628
|
+
output: out2,
|
|
629
|
+
},
|
|
630
|
+
]);
|
|
631
|
+
|
|
632
|
+
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
633
|
+
|
|
634
|
+
expect(result.ok).toBe(true);
|
|
635
|
+
if (!result.ok) return;
|
|
636
|
+
|
|
637
|
+
// Primary is the latest artifact
|
|
638
|
+
expect(result.state.primaryArtifact).toStrictEqual(art2);
|
|
639
|
+
// Named preserves full history in order
|
|
640
|
+
expect(result.state.named.plan).toEqual([out1, out2]);
|
|
641
|
+
expect(result.state.stagesCompleted).toBe(2);
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
it("aborted row is treated like a failed row: excluded from state seeding", () => {
|
|
645
|
+
const art1 = fakeArtifact("plans/p1.md");
|
|
646
|
+
const out1 = fakeOutput([art1]);
|
|
647
|
+
|
|
648
|
+
writeRunStages([
|
|
649
|
+
{
|
|
650
|
+
stageNumber: 1,
|
|
651
|
+
stage: "plan",
|
|
652
|
+
skill: "plan",
|
|
653
|
+
status: "completed",
|
|
654
|
+
ts: "2026-06-03T07:31:00Z",
|
|
655
|
+
output: out1,
|
|
656
|
+
},
|
|
657
|
+
{
|
|
658
|
+
stageNumber: 2,
|
|
659
|
+
stage: "build",
|
|
660
|
+
skill: "build",
|
|
661
|
+
status: "aborted",
|
|
662
|
+
ts: "2026-06-03T07:35:00Z",
|
|
663
|
+
errMsg: "User cancelled",
|
|
664
|
+
},
|
|
665
|
+
]);
|
|
666
|
+
|
|
667
|
+
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
668
|
+
|
|
669
|
+
expect(result.ok).toBe(true);
|
|
670
|
+
if (!result.ok) return;
|
|
671
|
+
|
|
672
|
+
expect(result.state.stagesCompleted).toBe(1);
|
|
673
|
+
expect(result.state.output).toStrictEqual(out1);
|
|
674
|
+
expect(result.visited).toEqual(new Set(["plan", "build"]));
|
|
675
|
+
expect(result.lastStageNumber).toBe(2);
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
it("skipped row is excluded from state seeding", () => {
|
|
679
|
+
const art1 = fakeArtifact("plans/p1.md");
|
|
680
|
+
const out1 = fakeOutput([art1]);
|
|
681
|
+
|
|
682
|
+
writeRunStages([
|
|
683
|
+
{
|
|
684
|
+
stageNumber: 1,
|
|
685
|
+
stage: "plan",
|
|
686
|
+
skill: "plan",
|
|
687
|
+
status: "completed",
|
|
688
|
+
ts: "2026-06-03T07:31:00Z",
|
|
689
|
+
output: out1,
|
|
690
|
+
},
|
|
691
|
+
{
|
|
692
|
+
stageNumber: 2,
|
|
693
|
+
stage: "build",
|
|
694
|
+
skill: "build",
|
|
695
|
+
status: "skipped",
|
|
696
|
+
ts: "2026-06-03T07:35:00Z",
|
|
697
|
+
},
|
|
698
|
+
]);
|
|
699
|
+
|
|
700
|
+
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
701
|
+
|
|
702
|
+
expect(result.ok).toBe(true);
|
|
703
|
+
if (!result.ok) return;
|
|
704
|
+
|
|
705
|
+
expect(result.state.stagesCompleted).toBe(1);
|
|
706
|
+
expect(result.state.output).toStrictEqual(out1);
|
|
707
|
+
expect(result.visited).toEqual(new Set(["plan", "build"]));
|
|
708
|
+
});
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
// ---------------------------------------------------------------------------
|
|
712
|
+
// resumeWorkflow — end-to-end resume tests
|
|
713
|
+
// ---------------------------------------------------------------------------
|
|
714
|
+
|
|
715
|
+
const RPIV_ARTIFACT_PATTERN = /\.rpiv\/artifacts\/[\w.-]+\/[\w.-]+\.md/g;
|
|
716
|
+
|
|
717
|
+
/** Minimal outcome that scans assistant text for .rpiv/artifacts paths. */
|
|
718
|
+
const artifactOutcome: import("../output.js").OutputSpec<unknown, "artifact-md", Record<string, unknown>> = {
|
|
719
|
+
collector: {
|
|
720
|
+
collect: (ctx) => {
|
|
721
|
+
let lastMatch: string | undefined;
|
|
722
|
+
const start = Math.max(ctx.branchOffset ?? 0, 0);
|
|
723
|
+
for (let i = ctx.branch.length - 1; i >= start && !lastMatch; i--) {
|
|
724
|
+
const entry = ctx.branch[i]!;
|
|
725
|
+
if (entry.type !== "message" || entry.message?.role !== "assistant") continue;
|
|
726
|
+
const content = entry.message.content;
|
|
727
|
+
if (!Array.isArray(content)) continue;
|
|
728
|
+
for (let j = content.length - 1; j >= 0; j--) {
|
|
729
|
+
const part = content[j]!;
|
|
730
|
+
if (part.type === "text" && typeof part.text === "string") {
|
|
731
|
+
const matches = part.text.match(RPIV_ARTIFACT_PATTERN);
|
|
732
|
+
if (matches && matches.length > 0) {
|
|
733
|
+
lastMatch = matches[matches.length - 1];
|
|
734
|
+
break;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
if (!lastMatch) {
|
|
740
|
+
return {
|
|
741
|
+
kind: "fatal",
|
|
742
|
+
message: `${ctx.skill} finished without producing a .rpiv/artifacts path`,
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
return { kind: "ok", artifacts: [{ handle: fsHandle(lastMatch), role: "primary" }] };
|
|
746
|
+
},
|
|
747
|
+
},
|
|
748
|
+
};
|
|
749
|
+
|
|
750
|
+
/** Build a minimal 2-stage produces workflow for resume tests. */
|
|
751
|
+
const twoStageWf: Workflow = {
|
|
752
|
+
name: "resume-wf",
|
|
753
|
+
start: "plan",
|
|
754
|
+
stages: {
|
|
755
|
+
plan: { kind: "produces", sessionPolicy: "fresh", outcome: artifactOutcome },
|
|
756
|
+
build: { kind: "produces", sessionPolicy: "fresh", outcome: artifactOutcome },
|
|
757
|
+
},
|
|
758
|
+
edges: {
|
|
759
|
+
plan: "build",
|
|
760
|
+
build: "stop",
|
|
761
|
+
},
|
|
762
|
+
};
|
|
763
|
+
|
|
764
|
+
const resumeHeader: WorkflowHeader = {
|
|
765
|
+
runId: "2026-06-03_07-30-00-ab12",
|
|
766
|
+
workflow: "resume-wf",
|
|
767
|
+
input: "Add dark mode",
|
|
768
|
+
ts: "2026-06-03T07:30:00Z",
|
|
769
|
+
};
|
|
770
|
+
|
|
771
|
+
describe("resumeWorkflow", () => {
|
|
772
|
+
/** Helper: write header + stages, return the header. */
|
|
773
|
+
function writeRun(header: WorkflowHeader, stages: WorkflowStage[]): void {
|
|
774
|
+
writeHeader(tmpDir, header);
|
|
775
|
+
for (const stage of stages) {
|
|
776
|
+
appendStage(tmpDir, header.runId, stage);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/** Helper: read all stage rows from the run's JSONL. */
|
|
781
|
+
function readRunStages(runId: string): WorkflowStage[] {
|
|
782
|
+
return readAllStages(tmpDir, runId);
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/** Write an artifact file at the given relative path under tmpDir. */
|
|
786
|
+
function writeArtifact(relPath: string, content = "") {
|
|
787
|
+
const parts = relPath.split("/");
|
|
788
|
+
const dir = join(tmpDir, ...parts.slice(0, -1));
|
|
789
|
+
mkdirSync(dir, { recursive: true });
|
|
790
|
+
writeFileSync(join(tmpDir, relPath), content);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
it("failed-trailer: re-runs the failed stage and continues to completion", async () => {
|
|
794
|
+
// Stage 1 (plan) completed, stage 2 (build) failed.
|
|
795
|
+
// Resume should re-run build and complete.
|
|
796
|
+
const art1 = fakeArtifact("plans/p1.md");
|
|
797
|
+
const out1 = fakeOutput([art1]);
|
|
798
|
+
|
|
799
|
+
writeRun(resumeHeader, [
|
|
800
|
+
{
|
|
801
|
+
stageNumber: 1,
|
|
802
|
+
stage: "plan",
|
|
803
|
+
skill: "plan",
|
|
804
|
+
status: "completed",
|
|
805
|
+
ts: "2026-06-03T07:31:00Z",
|
|
806
|
+
output: out1,
|
|
807
|
+
},
|
|
808
|
+
{
|
|
809
|
+
stageNumber: 2,
|
|
810
|
+
stage: "build",
|
|
811
|
+
skill: "build",
|
|
812
|
+
status: "failed",
|
|
813
|
+
ts: "2026-06-03T07:35:00Z",
|
|
814
|
+
errMsg: "Something went wrong",
|
|
815
|
+
},
|
|
816
|
+
]);
|
|
817
|
+
|
|
818
|
+
writeArtifact(".rpiv/artifacts/builds/b1.md");
|
|
819
|
+
const chain = createMockSessionChain({
|
|
820
|
+
cwd: tmpDir,
|
|
821
|
+
steps: [{ branch: [mockAssistantMessage("Wrote .rpiv/artifacts/builds/b1.md")] }],
|
|
822
|
+
});
|
|
823
|
+
|
|
824
|
+
const result = await resumeWorkflow(chain.ctx, {
|
|
825
|
+
workflow: twoStageWf,
|
|
826
|
+
header: resumeHeader,
|
|
827
|
+
ref: "@2026-06-03_07-30-00-ab12",
|
|
828
|
+
});
|
|
829
|
+
|
|
830
|
+
expect(result.success).toBe(true);
|
|
831
|
+
expect(result.runId).toBe(resumeHeader.runId);
|
|
832
|
+
// 1 (original plan) + 1 (re-ran build) = 2
|
|
833
|
+
expect(result.stagesCompleted).toBe(2);
|
|
834
|
+
expect(result.lastArtifact).toBe(".rpiv/artifacts/builds/b1.md");
|
|
835
|
+
|
|
836
|
+
// JSONL should have: original header + plan(completed) + build(failed) + build(completed)
|
|
837
|
+
const stages = readRunStages(resumeHeader.runId);
|
|
838
|
+
expect(stages).toHaveLength(3); // 2 original + 1 new
|
|
839
|
+
expect(stages[2]).toMatchObject({
|
|
840
|
+
stage: "build",
|
|
841
|
+
status: "completed",
|
|
842
|
+
});
|
|
843
|
+
// New stage number is strictly greater than the failed row's number
|
|
844
|
+
expect(stages[2]!.stageNumber).toBeGreaterThan(stages[1]!.stageNumber);
|
|
845
|
+
|
|
846
|
+
// Dispatched the re-run with the prior stage's artifact as input
|
|
847
|
+
expect(chain.sentMessages).toEqual(["/skill:build plans/p1.md"]);
|
|
848
|
+
});
|
|
849
|
+
|
|
850
|
+
it("completed-trailer: routes onward from the last completed stage", async () => {
|
|
851
|
+
// Stage 1 (plan) completed. The last row is completed,
|
|
852
|
+
// so resume should route onward to build.
|
|
853
|
+
const art1 = fakeArtifact("plans/p1.md");
|
|
854
|
+
const out1 = fakeOutput([art1]);
|
|
855
|
+
|
|
856
|
+
writeRun(resumeHeader, [
|
|
857
|
+
{
|
|
858
|
+
stageNumber: 1,
|
|
859
|
+
stage: "plan",
|
|
860
|
+
skill: "plan",
|
|
861
|
+
status: "completed",
|
|
862
|
+
ts: "2026-06-03T07:31:00Z",
|
|
863
|
+
output: out1,
|
|
864
|
+
},
|
|
865
|
+
]);
|
|
866
|
+
|
|
867
|
+
writeArtifact(".rpiv/artifacts/builds/b1.md");
|
|
868
|
+
const chain = createMockSessionChain({
|
|
869
|
+
cwd: tmpDir,
|
|
870
|
+
steps: [{ branch: [mockAssistantMessage("Wrote .rpiv/artifacts/builds/b1.md")] }],
|
|
871
|
+
});
|
|
872
|
+
|
|
873
|
+
const result = await resumeWorkflow(chain.ctx, {
|
|
874
|
+
workflow: twoStageWf,
|
|
875
|
+
header: resumeHeader,
|
|
876
|
+
ref: "@2026-06-03_07-30-00-ab12",
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
expect(result.success).toBe(true);
|
|
880
|
+
expect(result.stagesCompleted).toBe(2);
|
|
881
|
+
|
|
882
|
+
const stages = readRunStages(resumeHeader.runId);
|
|
883
|
+
expect(stages).toHaveLength(2); // 1 original + 1 new (build)
|
|
884
|
+
expect(stages[1]).toMatchObject({ stage: "build", status: "completed" });
|
|
885
|
+
expect(stages[1]!.stageNumber).toBeGreaterThan(stages[0]!.stageNumber);
|
|
886
|
+
|
|
887
|
+
expect(chain.sentMessages).toEqual(["/skill:build plans/p1.md"]);
|
|
888
|
+
});
|
|
889
|
+
|
|
890
|
+
it("cleanly finished run: immediate stop no-op (stagesCompleted unchanged)", async () => {
|
|
891
|
+
// Both stages completed, edge is "stop". Resuming routes onward from build,
|
|
892
|
+
// which hits stop → finalizeWorkflow → success. No new stage rows.
|
|
893
|
+
const art1 = fakeArtifact("plans/p1.md");
|
|
894
|
+
const art2 = fakeArtifact("builds/b1.md");
|
|
895
|
+
const out1 = fakeOutput([art1]);
|
|
896
|
+
const out2 = fakeOutput([art2]);
|
|
897
|
+
|
|
898
|
+
writeRun(resumeHeader, [
|
|
899
|
+
{
|
|
900
|
+
stageNumber: 1,
|
|
901
|
+
stage: "plan",
|
|
902
|
+
skill: "plan",
|
|
903
|
+
status: "completed",
|
|
904
|
+
ts: "2026-06-03T07:31:00Z",
|
|
905
|
+
output: out1,
|
|
906
|
+
},
|
|
907
|
+
{
|
|
908
|
+
stageNumber: 2,
|
|
909
|
+
stage: "build",
|
|
910
|
+
skill: "build",
|
|
911
|
+
status: "completed",
|
|
912
|
+
ts: "2026-06-03T07:35:00Z",
|
|
913
|
+
output: out2,
|
|
914
|
+
},
|
|
915
|
+
]);
|
|
916
|
+
|
|
917
|
+
const chain = createMockSessionChain({ cwd: tmpDir, steps: [] });
|
|
918
|
+
|
|
919
|
+
const result = await resumeWorkflow(chain.ctx, {
|
|
920
|
+
workflow: twoStageWf,
|
|
921
|
+
header: resumeHeader,
|
|
922
|
+
ref: "@2026-06-03_07-30-00-ab12",
|
|
923
|
+
});
|
|
924
|
+
|
|
925
|
+
expect(result.success).toBe(true);
|
|
926
|
+
expect(result.stagesCompleted).toBe(2); // unchanged
|
|
927
|
+
expect(result.lastArtifact).toBe("builds/b1.md");
|
|
928
|
+
|
|
929
|
+
// No new stage rows appended
|
|
930
|
+
const stages = readRunStages(resumeHeader.runId);
|
|
931
|
+
expect(stages).toHaveLength(2);
|
|
932
|
+
|
|
933
|
+
// No new session was spawned
|
|
934
|
+
expect(chain.ctx.newSession).not.toHaveBeenCalled();
|
|
935
|
+
});
|
|
936
|
+
|
|
937
|
+
it("no-rows refusal: returns error envelope, no self-notify (caller surfaces it)", async () => {
|
|
938
|
+
// Header-only file (no stage rows)
|
|
939
|
+
writeHeader(tmpDir, resumeHeader);
|
|
940
|
+
|
|
941
|
+
const chain = createMockSessionChain({ cwd: tmpDir, steps: [] });
|
|
942
|
+
|
|
943
|
+
const result = await resumeWorkflow(chain.ctx, {
|
|
944
|
+
workflow: twoStageWf,
|
|
945
|
+
header: resumeHeader,
|
|
946
|
+
ref: "@2026-06-03_07-30-00-ab12",
|
|
947
|
+
});
|
|
948
|
+
|
|
949
|
+
expect(result.success).toBe(false);
|
|
950
|
+
expect(result.error).toMatch(/no recorded stages/);
|
|
951
|
+
expect(result.stagesCompleted).toBe(0);
|
|
952
|
+
// No runId on a no-JSONL refusal — the discriminator command uses to decide
|
|
953
|
+
// whether to notify (the stage machinery never wrote a failure row here).
|
|
954
|
+
expect(result.runId).toBeUndefined();
|
|
955
|
+
|
|
956
|
+
// Pure envelope: resumeWorkflow does NOT self-notify (mirrors runWorkflow's
|
|
957
|
+
// pre-flight rejections). The caller surfaces result.error.
|
|
958
|
+
const errorNotifies = chain.notifications.filter((n) => n.level === "error" && /no recorded stages/.test(n.msg));
|
|
959
|
+
expect(errorNotifies).toHaveLength(0);
|
|
960
|
+
|
|
961
|
+
// No new JSONL rows written on refusal
|
|
962
|
+
const stages = readRunStages(resumeHeader.runId);
|
|
963
|
+
expect(stages).toHaveLength(0);
|
|
964
|
+
});
|
|
965
|
+
|
|
966
|
+
it("stage-gone refusal: returns error envelope, no self-notify", async () => {
|
|
967
|
+
writeRun(resumeHeader, [
|
|
968
|
+
{
|
|
969
|
+
stageNumber: 1,
|
|
970
|
+
stage: "plan",
|
|
971
|
+
skill: "plan",
|
|
972
|
+
status: "completed",
|
|
973
|
+
ts: "2026-06-03T07:31:00Z",
|
|
974
|
+
},
|
|
975
|
+
{
|
|
976
|
+
stageNumber: 2,
|
|
977
|
+
stage: "removed-stage",
|
|
978
|
+
skill: "removed-stage",
|
|
979
|
+
status: "failed",
|
|
980
|
+
ts: "2026-06-03T07:35:00Z",
|
|
981
|
+
errMsg: "fail",
|
|
982
|
+
},
|
|
983
|
+
]);
|
|
984
|
+
|
|
985
|
+
const chain = createMockSessionChain({ cwd: tmpDir, steps: [] });
|
|
986
|
+
|
|
987
|
+
const result = await resumeWorkflow(chain.ctx, {
|
|
988
|
+
workflow: twoStageWf,
|
|
989
|
+
header: resumeHeader,
|
|
990
|
+
ref: "@2026-06-03_07-30-00-ab12",
|
|
991
|
+
});
|
|
992
|
+
|
|
993
|
+
expect(result.success).toBe(false);
|
|
994
|
+
expect(result.error).toMatch(/no longer exists/);
|
|
995
|
+
expect(result.runId).toBeUndefined();
|
|
996
|
+
|
|
997
|
+
const errorNotifies = chain.notifications.filter((n) => n.level === "error" && /no longer exists/.test(n.msg));
|
|
998
|
+
expect(errorNotifies).toHaveLength(0);
|
|
999
|
+
});
|
|
1000
|
+
|
|
1001
|
+
it("resume does not write a new header (append-only)", async () => {
|
|
1002
|
+
// Write a completed run, then resume it. The JSONL should have exactly
|
|
1003
|
+
// one header line (the original) — no second header was appended.
|
|
1004
|
+
const art1 = fakeArtifact("plans/p1.md");
|
|
1005
|
+
const out1 = fakeOutput([art1]);
|
|
1006
|
+
|
|
1007
|
+
writeRun(resumeHeader, [
|
|
1008
|
+
{
|
|
1009
|
+
stageNumber: 1,
|
|
1010
|
+
stage: "plan",
|
|
1011
|
+
skill: "plan",
|
|
1012
|
+
status: "completed",
|
|
1013
|
+
ts: "2026-06-03T07:31:00Z",
|
|
1014
|
+
output: out1,
|
|
1015
|
+
},
|
|
1016
|
+
]);
|
|
1017
|
+
|
|
1018
|
+
writeArtifact(".rpiv/artifacts/builds/b1.md");
|
|
1019
|
+
const chain = createMockSessionChain({
|
|
1020
|
+
cwd: tmpDir,
|
|
1021
|
+
steps: [{ branch: [mockAssistantMessage("Wrote .rpiv/artifacts/builds/b1.md")] }],
|
|
1022
|
+
});
|
|
1023
|
+
|
|
1024
|
+
await resumeWorkflow(chain.ctx, {
|
|
1025
|
+
workflow: twoStageWf,
|
|
1026
|
+
header: resumeHeader,
|
|
1027
|
+
ref: "@2026-06-03_07-30-00-ab12",
|
|
1028
|
+
});
|
|
1029
|
+
|
|
1030
|
+
// Read raw file and count header lines (lines with runId + workflow but no stageNumber)
|
|
1031
|
+
const content = readFileSync(stateFilePath(tmpDir, resumeHeader.runId), "utf-8");
|
|
1032
|
+
const lines = content.trim().split("\n");
|
|
1033
|
+
const headerLines = lines.filter((l) => {
|
|
1034
|
+
try {
|
|
1035
|
+
const p = JSON.parse(l);
|
|
1036
|
+
return typeof p.runId === "string" && typeof p.stageNumber !== "number";
|
|
1037
|
+
} catch {
|
|
1038
|
+
return false;
|
|
1039
|
+
}
|
|
1040
|
+
});
|
|
1041
|
+
expect(headerLines).toHaveLength(1);
|
|
1042
|
+
});
|
|
1043
|
+
|
|
1044
|
+
it("new stage numbers are strictly greater than all prior stage numbers", async () => {
|
|
1045
|
+
const art1 = fakeArtifact("plans/p1.md");
|
|
1046
|
+
const out1 = fakeOutput([art1]);
|
|
1047
|
+
|
|
1048
|
+
writeRun(resumeHeader, [
|
|
1049
|
+
{
|
|
1050
|
+
stageNumber: 1,
|
|
1051
|
+
stage: "plan",
|
|
1052
|
+
skill: "plan",
|
|
1053
|
+
status: "completed",
|
|
1054
|
+
ts: "2026-06-03T07:31:00Z",
|
|
1055
|
+
output: out1,
|
|
1056
|
+
},
|
|
1057
|
+
{
|
|
1058
|
+
stageNumber: 2,
|
|
1059
|
+
stage: "build",
|
|
1060
|
+
skill: "build",
|
|
1061
|
+
status: "failed",
|
|
1062
|
+
ts: "2026-06-03T07:35:00Z",
|
|
1063
|
+
errMsg: "fail",
|
|
1064
|
+
},
|
|
1065
|
+
]);
|
|
1066
|
+
|
|
1067
|
+
writeArtifact(".rpiv/artifacts/builds/b1.md");
|
|
1068
|
+
const chain = createMockSessionChain({
|
|
1069
|
+
cwd: tmpDir,
|
|
1070
|
+
steps: [{ branch: [mockAssistantMessage("Wrote .rpiv/artifacts/builds/b1.md")] }],
|
|
1071
|
+
});
|
|
1072
|
+
|
|
1073
|
+
await resumeWorkflow(chain.ctx, {
|
|
1074
|
+
workflow: twoStageWf,
|
|
1075
|
+
header: resumeHeader,
|
|
1076
|
+
ref: "@2026-06-03_07-30-00-ab12",
|
|
1077
|
+
});
|
|
1078
|
+
|
|
1079
|
+
const stages = readRunStages(resumeHeader.runId);
|
|
1080
|
+
const maxOriginal = Math.max(...stages.slice(0, 2).map((s) => s.stageNumber));
|
|
1081
|
+
const newStages = stages.slice(2);
|
|
1082
|
+
for (const s of newStages) {
|
|
1083
|
+
expect(s.stageNumber).toBeGreaterThan(maxOriginal);
|
|
1084
|
+
}
|
|
1085
|
+
});
|
|
1086
|
+
|
|
1087
|
+
it("stamps resumedFrom in trigger.meta", async () => {
|
|
1088
|
+
const art1 = fakeArtifact("plans/p1.md");
|
|
1089
|
+
const out1 = fakeOutput([art1]);
|
|
1090
|
+
|
|
1091
|
+
writeRun(resumeHeader, [
|
|
1092
|
+
{
|
|
1093
|
+
stageNumber: 1,
|
|
1094
|
+
stage: "plan",
|
|
1095
|
+
skill: "plan",
|
|
1096
|
+
status: "completed",
|
|
1097
|
+
ts: "2026-06-03T07:31:00Z",
|
|
1098
|
+
output: out1,
|
|
1099
|
+
},
|
|
1100
|
+
]);
|
|
1101
|
+
|
|
1102
|
+
writeArtifact(".rpiv/artifacts/builds/b1.md");
|
|
1103
|
+
const chain = createMockSessionChain({
|
|
1104
|
+
cwd: tmpDir,
|
|
1105
|
+
steps: [{ branch: [mockAssistantMessage("Wrote .rpiv/artifacts/builds/b1.md")] }],
|
|
1106
|
+
});
|
|
1107
|
+
|
|
1108
|
+
// Use lifecycle listeners to capture the trigger from LifecycleContext
|
|
1109
|
+
const capturedTriggers: unknown[] = [];
|
|
1110
|
+
await resumeWorkflow(chain.ctx, {
|
|
1111
|
+
workflow: twoStageWf,
|
|
1112
|
+
header: resumeHeader,
|
|
1113
|
+
ref: "@my-run-ref",
|
|
1114
|
+
lifecycle: {
|
|
1115
|
+
onWorkflowStart: (lc) => {
|
|
1116
|
+
capturedTriggers.push(lc.trigger);
|
|
1117
|
+
},
|
|
1118
|
+
},
|
|
1119
|
+
});
|
|
1120
|
+
|
|
1121
|
+
expect(capturedTriggers).toHaveLength(1);
|
|
1122
|
+
expect(capturedTriggers[0]).toMatchObject({
|
|
1123
|
+
kind: "command",
|
|
1124
|
+
name: "wf",
|
|
1125
|
+
meta: { resumedFrom: "@my-run-ref" },
|
|
1126
|
+
});
|
|
1127
|
+
});
|
|
1128
|
+
|
|
1129
|
+
// Mid-iterate resume dispatch is covered end-to-end in `resume-iterate.test.ts`.
|
|
1130
|
+
});
|