@juicesharp/rpiv-workflow 1.19.0 → 1.20.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 +81 -8
- package/api.ts +18 -703
- package/audit-rows.ts +136 -0
- package/audit.ts +139 -126
- package/built-ins.ts +20 -45
- package/chain-state.ts +169 -0
- package/command-run.ts +31 -3
- package/command.ts +116 -20
- package/docs/workflow-authoring.md +322 -61
- package/{lifecycle.ts → events.ts} +119 -35
- package/host.ts +30 -0
- package/index.ts +25 -81
- package/internal-utils.ts +92 -78
- package/internal.ts +21 -1
- package/judge.ts +229 -0
- package/layers.ts +10 -15
- package/load/alias.ts +6 -12
- package/load/cache.ts +9 -6
- package/load/index.ts +16 -109
- package/load/issues.ts +31 -0
- package/load/legacy.ts +102 -0
- package/load/merge.ts +2 -2
- package/load/shape-guards.test.ts +4 -1
- package/load/shape-guards.ts +3 -6
- package/load/skill-contract-phase.ts +94 -0
- package/loop-constructors.ts +623 -0
- package/loop-def.ts +240 -0
- package/loop-kinds.ts +493 -0
- package/loop.ts +304 -0
- package/messages.ts +225 -157
- package/outcomes/collectors/directory-path.ts +7 -3
- package/outcomes/collectors/require-opt.ts +11 -0
- package/outcomes/collectors/tool-call.test.ts +3 -1
- package/outcomes/collectors/tool-call.ts +8 -3
- package/outcomes/collectors/transcript-path.ts +2 -3
- package/outcomes/collectors/union.test.ts +54 -0
- package/outcomes/collectors/union.ts +28 -9
- package/outcomes/collectors/url.ts +7 -0
- package/outcomes/collectors/workspace-diff.test.ts +13 -0
- package/outcomes/collectors/workspace-diff.ts +21 -14
- package/outcomes/exec.ts +17 -0
- package/outcomes/git-commit.test.ts +76 -30
- package/outcomes/git-commit.ts +139 -100
- package/outcomes/index.ts +1 -0
- package/outcomes/parsers/json-body.ts +2 -2
- package/outcomes/side-effect.ts +3 -3
- package/output-spec.ts +36 -10
- package/output.ts +40 -20
- package/package.json +13 -7
- package/predicates.ts +11 -8
- package/preview.ts +77 -17
- package/registration.ts +109 -35
- package/routing-dsl.ts +310 -0
- package/routing.ts +2 -2
- package/runner/by-name.ts +2 -1
- package/runner/by-run-id.ts +2 -1
- package/runner/chain-advance.ts +59 -44
- package/runner/errors.ts +33 -0
- package/runner/failure.ts +109 -0
- package/runner/header-write-failure.test.ts +64 -0
- package/runner/index.ts +7 -13
- package/runner/input-validation.ts +11 -21
- package/runner/preflight.ts +173 -0
- package/runner/resolve-stage.ts +68 -0
- package/runner/resume-entry.ts +101 -0
- package/runner/resume-loop.test.ts +1410 -0
- package/runner/resume-loop.ts +110 -0
- package/runner/resume-session.test.ts +400 -0
- package/runner/resume.test.ts +881 -206
- package/runner/resume.ts +281 -261
- package/runner/run-context.ts +161 -0
- package/runner/run-stage.ts +456 -0
- package/runner/runner.ts +59 -400
- package/runner/script-stage.ts +103 -105
- package/schema-compat.ts +13 -1
- package/sessions/extraction.ts +53 -31
- package/sessions/index.ts +3 -1
- package/sessions/locate.test.ts +77 -0
- package/sessions/locate.ts +90 -0
- package/sessions/reattach.ts +63 -0
- package/sessions/sessions.ts +130 -127
- package/skill-contract.ts +22 -25
- package/skill-contracts/composition.ts +88 -37
- package/skill-contracts/{registries.ts → extension-points.ts} +45 -12
- package/skill-contracts/harvest.ts +2 -3
- package/skill-contracts/index.ts +15 -8
- package/skill-contracts/registry.ts +33 -50
- package/stage-def.ts +527 -0
- package/startup.ts +3 -1
- package/state/index.ts +8 -1
- package/state/names.ts +91 -65
- package/state/paths.ts +12 -0
- package/state/raw.ts +78 -0
- package/state/reads.ts +103 -85
- package/state/resolve.ts +73 -0
- package/state/state.ts +112 -15
- package/state/writes.ts +29 -12
- package/transcript.ts +24 -0
- package/triggers.ts +11 -3
- package/types.ts +202 -44
- package/validate/contract-compat.ts +193 -0
- package/validate/graph.ts +125 -0
- package/validate/issue.ts +314 -0
- package/validate/stage-rules.ts +420 -0
- package/validate-output.ts +69 -1
- package/validate-workflow.ts +55 -759
- package/control-flow.ts +0 -135
- package/fanout.ts +0 -102
- package/iterate.ts +0 -155
- package/runner/resume-fanout.test.ts +0 -331
- package/runner/resume-fanout.ts +0 -136
- package/runner/resume-iterate.test.ts +0 -444
- package/runner/resume-iterate.ts +0 -111
- package/runner/stage-lifecycle.ts +0 -500
package/runner/resume.test.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Reconstruction tests for `reconstructState` — the
|
|
3
|
-
* `RunState` from a run's JSONL audit trail.
|
|
2
|
+
* Reconstruction tests for `reconstructState` — the ONE async fold that
|
|
3
|
+
* rebuilds `RunState` from a run's JSONL audit trail.
|
|
4
4
|
*
|
|
5
|
-
* Covers
|
|
5
|
+
* Covers:
|
|
6
6
|
* 1. Produces-only linear chain (primary + named + output + counters)
|
|
7
7
|
* 2. Side-effect stage between produces (primary preserved)
|
|
8
8
|
* 3. Terminal stage (primary cleared)
|
|
@@ -10,20 +10,28 @@
|
|
|
10
10
|
* visited + lastStageNumber include it)
|
|
11
11
|
* 5. Empty file → no-rows refusal
|
|
12
12
|
* 6. Stage gone from workflow → stage-gone refusal
|
|
13
|
-
* 7.
|
|
14
|
-
*
|
|
15
|
-
*
|
|
13
|
+
* 7. Loop-unit rows keyed on the STRUCTURED `parent` field — fanout entry
|
|
14
|
+
* projection at generation close, iterate "last" projection + trailing
|
|
15
|
+
* cursor, assess channel separation + transient judge roll, full-row
|
|
16
|
+
* drift guard, and the legacy-row (`parent`-less) stage-gone refusal
|
|
16
17
|
* 8. Named-slot append-order (array history preserved across repeated calls)
|
|
18
|
+
*
|
|
19
|
+
* End-to-end loop-resume DISPATCH (re-run failed+remaining units, finished
|
|
20
|
+
* no-op silence, the assess pending paths, back-edge generation reset) lives
|
|
21
|
+
* in `resume-loop.test.ts`.
|
|
17
22
|
*/
|
|
18
23
|
|
|
19
|
-
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
24
|
+
import { appendFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
20
25
|
import { tmpdir } from "node:os";
|
|
21
26
|
import { join } from "node:path";
|
|
22
27
|
import { createMockSessionChain, mockAssistantMessage } from "@juicesharp/rpiv-test-utils";
|
|
23
28
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
24
|
-
import type
|
|
29
|
+
import { type FanoutFn, type IterateFn, produces, type Workflow } from "../api.js";
|
|
25
30
|
import type { Artifact } from "../handle.js";
|
|
26
|
-
import { fs as fsHandle } from "../handle.js";
|
|
31
|
+
import { fs as fsHandle, handleToString } from "../handle.js";
|
|
32
|
+
import { judge } from "../judge.js";
|
|
33
|
+
import { assess, fanout, iterate, majority, panel, verify } from "../loop-constructors.js";
|
|
34
|
+
import { advanceCursor, freshCursor } from "../loop-kinds.js";
|
|
27
35
|
import type { Output } from "../output.js";
|
|
28
36
|
import {
|
|
29
37
|
appendStage,
|
|
@@ -52,11 +60,11 @@ const fakeOutput = (artifacts: readonly Artifact[] = []): Output => ({
|
|
|
52
60
|
/**
|
|
53
61
|
* Minimal named outcome for fold tests. The collector is never invoked — the
|
|
54
62
|
* reconstruct fold replays persisted rows, not live sessions — so a stub that
|
|
55
|
-
* satisfies `
|
|
63
|
+
* satisfies `Outcome` is sufficient. `name` drives `resolvePublishName`.
|
|
56
64
|
*/
|
|
57
65
|
const makeOutcome = (
|
|
58
66
|
name: string,
|
|
59
|
-
): import("../output.js").
|
|
67
|
+
): import("../output-spec.js").Outcome<unknown, "artifact-md", Record<string, unknown>> & { name: string } => ({
|
|
60
68
|
name,
|
|
61
69
|
collector: { collect: () => ({ kind: "ok", artifacts: [] }) },
|
|
62
70
|
});
|
|
@@ -136,12 +144,97 @@ function writeRunStages(stages: WorkflowStage[]): WorkflowHeader {
|
|
|
136
144
|
return baseHeader;
|
|
137
145
|
}
|
|
138
146
|
|
|
147
|
+
/** A structured loop-unit row (the machine channel — `stage` is display only). */
|
|
148
|
+
const fanoutUnitRow = (
|
|
149
|
+
parent: string,
|
|
150
|
+
unitId: string,
|
|
151
|
+
unitIndex: number,
|
|
152
|
+
num: number,
|
|
153
|
+
output: Output | undefined,
|
|
154
|
+
status: WorkflowStage["status"] = "completed",
|
|
155
|
+
): WorkflowStage => ({
|
|
156
|
+
session: null,
|
|
157
|
+
stageNumber: num,
|
|
158
|
+
stage: `${parent} (${unitId})`,
|
|
159
|
+
skill: parent,
|
|
160
|
+
status,
|
|
161
|
+
ts: `t${num}`,
|
|
162
|
+
parent,
|
|
163
|
+
role: "produce",
|
|
164
|
+
unitId,
|
|
165
|
+
unitIndex,
|
|
166
|
+
...(output ? { output } : {}),
|
|
167
|
+
...(status === "failed" ? { errMsg: "boom" } : {}),
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
const assessProduceRow = (parent: string, round: number, num: number, output: Output): WorkflowStage => ({
|
|
171
|
+
session: null,
|
|
172
|
+
stageNumber: num,
|
|
173
|
+
stage: `${parent} (r${round}·produce)`,
|
|
174
|
+
skill: parent,
|
|
175
|
+
status: "completed",
|
|
176
|
+
ts: `t${num}`,
|
|
177
|
+
parent,
|
|
178
|
+
role: "produce",
|
|
179
|
+
unitIndex: round,
|
|
180
|
+
output,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const assessJudgeRow = (
|
|
184
|
+
parent: string,
|
|
185
|
+
judgeSkill: string,
|
|
186
|
+
round: number,
|
|
187
|
+
num: number,
|
|
188
|
+
output: Output,
|
|
189
|
+
): WorkflowStage => ({
|
|
190
|
+
session: null,
|
|
191
|
+
stageNumber: num,
|
|
192
|
+
stage: `${parent} (r${round}·judge)`,
|
|
193
|
+
skill: judgeSkill,
|
|
194
|
+
status: "completed",
|
|
195
|
+
ts: `t${num}`,
|
|
196
|
+
parent,
|
|
197
|
+
role: "judge",
|
|
198
|
+
unitIndex: round,
|
|
199
|
+
output,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
/** A panel member's judge row — identity-bearing (`#{memberIndex}` in `unitId`). */
|
|
203
|
+
const assessPanelJudgeRow = (
|
|
204
|
+
parent: string,
|
|
205
|
+
judgeSkill: string,
|
|
206
|
+
round: number,
|
|
207
|
+
memberIndex: number,
|
|
208
|
+
num: number,
|
|
209
|
+
output: Output,
|
|
210
|
+
): WorkflowStage => ({
|
|
211
|
+
session: null,
|
|
212
|
+
stageNumber: num,
|
|
213
|
+
stage: `${parent} (r${round}·judge#${memberIndex})`,
|
|
214
|
+
skill: judgeSkill,
|
|
215
|
+
status: "completed",
|
|
216
|
+
ts: `t${num}`,
|
|
217
|
+
parent,
|
|
218
|
+
role: "judge",
|
|
219
|
+
unitId: `r${round}·judge#${memberIndex}`,
|
|
220
|
+
unitIndex: round,
|
|
221
|
+
output,
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
/** A member verdict Output carrying `{ done }` data the per-member `pred` reads. */
|
|
225
|
+
const memberVerdict = (path: string, isDone: boolean): Output => ({
|
|
226
|
+
kind: "artifacts",
|
|
227
|
+
artifacts: [fakeArtifact(path)],
|
|
228
|
+
data: { done: isDone },
|
|
229
|
+
meta: { stage: "test", stageNumber: 1, ts: "", runId: "" },
|
|
230
|
+
});
|
|
231
|
+
|
|
139
232
|
// ---------------------------------------------------------------------------
|
|
140
233
|
// Cases
|
|
141
234
|
// ---------------------------------------------------------------------------
|
|
142
235
|
|
|
143
236
|
describe("reconstructState", () => {
|
|
144
|
-
it("produces-only linear chain: advances primary, appends named, tracks counters", () => {
|
|
237
|
+
it("produces-only linear chain: advances primary, appends named, tracks counters", async () => {
|
|
145
238
|
const art1 = fakeArtifact("plans/p1.md");
|
|
146
239
|
const art2 = fakeArtifact("plans/p2.md");
|
|
147
240
|
const out1 = fakeOutput([art1]);
|
|
@@ -149,6 +242,7 @@ describe("reconstructState", () => {
|
|
|
149
242
|
|
|
150
243
|
writeRunStages([
|
|
151
244
|
{
|
|
245
|
+
session: null,
|
|
152
246
|
stageNumber: 1,
|
|
153
247
|
stage: "plan",
|
|
154
248
|
skill: "plan",
|
|
@@ -157,6 +251,7 @@ describe("reconstructState", () => {
|
|
|
157
251
|
output: out1,
|
|
158
252
|
},
|
|
159
253
|
{
|
|
254
|
+
session: null,
|
|
160
255
|
stageNumber: 2,
|
|
161
256
|
stage: "build",
|
|
162
257
|
skill: "build",
|
|
@@ -166,7 +261,7 @@ describe("reconstructState", () => {
|
|
|
166
261
|
},
|
|
167
262
|
]);
|
|
168
263
|
|
|
169
|
-
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
264
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
170
265
|
|
|
171
266
|
expect(result.ok).toBe(true);
|
|
172
267
|
if (!result.ok) return; // type narrow
|
|
@@ -183,13 +278,14 @@ describe("reconstructState", () => {
|
|
|
183
278
|
expect(result.rows).toHaveLength(2);
|
|
184
279
|
});
|
|
185
280
|
|
|
186
|
-
it("side-effect stage between produces: primary preserved, named untouched by side-effect", () => {
|
|
281
|
+
it("side-effect stage between produces: primary preserved, named untouched by side-effect", async () => {
|
|
187
282
|
const art1 = fakeArtifact("plans/p1.md");
|
|
188
283
|
const out1 = fakeOutput([art1]);
|
|
189
284
|
const sideOut = fakeOutput([]); // side-effect output, no artifacts
|
|
190
285
|
|
|
191
286
|
writeRunStages([
|
|
192
287
|
{
|
|
288
|
+
session: null,
|
|
193
289
|
stageNumber: 1,
|
|
194
290
|
stage: "plan",
|
|
195
291
|
skill: "plan",
|
|
@@ -198,6 +294,7 @@ describe("reconstructState", () => {
|
|
|
198
294
|
output: out1,
|
|
199
295
|
},
|
|
200
296
|
{
|
|
297
|
+
session: null,
|
|
201
298
|
stageNumber: 2,
|
|
202
299
|
stage: "commit",
|
|
203
300
|
skill: "commit",
|
|
@@ -207,7 +304,7 @@ describe("reconstructState", () => {
|
|
|
207
304
|
},
|
|
208
305
|
]);
|
|
209
306
|
|
|
210
|
-
const result = reconstructState(tmpDir, sideEffectWorkflow, baseHeader);
|
|
307
|
+
const result = await reconstructState(tmpDir, sideEffectWorkflow, baseHeader);
|
|
211
308
|
|
|
212
309
|
expect(result.ok).toBe(true);
|
|
213
310
|
if (!result.ok) return;
|
|
@@ -222,13 +319,14 @@ describe("reconstructState", () => {
|
|
|
222
319
|
expect(result.visited).toEqual(new Set(["plan", "commit"]));
|
|
223
320
|
});
|
|
224
321
|
|
|
225
|
-
it("terminal stage (inheritsArtifacts: false): clears primary", () => {
|
|
322
|
+
it("terminal stage (inheritsArtifacts: false): clears primary", async () => {
|
|
226
323
|
const art1 = fakeArtifact("plans/p1.md");
|
|
227
324
|
const out1 = fakeOutput([art1]);
|
|
228
325
|
const termOut = fakeOutput([]);
|
|
229
326
|
|
|
230
327
|
writeRunStages([
|
|
231
328
|
{
|
|
329
|
+
session: null,
|
|
232
330
|
stageNumber: 1,
|
|
233
331
|
stage: "plan",
|
|
234
332
|
skill: "plan",
|
|
@@ -237,6 +335,7 @@ describe("reconstructState", () => {
|
|
|
237
335
|
output: out1,
|
|
238
336
|
},
|
|
239
337
|
{
|
|
338
|
+
session: null,
|
|
240
339
|
stageNumber: 2,
|
|
241
340
|
stage: "cleanup",
|
|
242
341
|
status: "completed",
|
|
@@ -245,7 +344,7 @@ describe("reconstructState", () => {
|
|
|
245
344
|
},
|
|
246
345
|
]);
|
|
247
346
|
|
|
248
|
-
const result = reconstructState(tmpDir, terminalWorkflow, baseHeader);
|
|
347
|
+
const result = await reconstructState(tmpDir, terminalWorkflow, baseHeader);
|
|
249
348
|
|
|
250
349
|
expect(result.ok).toBe(true);
|
|
251
350
|
if (!result.ok) return;
|
|
@@ -256,12 +355,13 @@ describe("reconstructState", () => {
|
|
|
256
355
|
expect(result.state.named.plan).toEqual([out1]);
|
|
257
356
|
});
|
|
258
357
|
|
|
259
|
-
it("failed trailing row: output NOT seeded, stagesCompleted excludes it, visited + lastStageNumber include it", () => {
|
|
358
|
+
it("failed trailing row: output NOT seeded, stagesCompleted excludes it, visited + lastStageNumber include it", async () => {
|
|
260
359
|
const art1 = fakeArtifact("plans/p1.md");
|
|
261
360
|
const out1 = fakeOutput([art1]);
|
|
262
361
|
|
|
263
362
|
writeRunStages([
|
|
264
363
|
{
|
|
364
|
+
session: null,
|
|
265
365
|
stageNumber: 1,
|
|
266
366
|
stage: "plan",
|
|
267
367
|
skill: "plan",
|
|
@@ -270,6 +370,7 @@ describe("reconstructState", () => {
|
|
|
270
370
|
output: out1,
|
|
271
371
|
},
|
|
272
372
|
{
|
|
373
|
+
session: null,
|
|
273
374
|
stageNumber: 2,
|
|
274
375
|
stage: "build",
|
|
275
376
|
skill: "build",
|
|
@@ -279,7 +380,7 @@ describe("reconstructState", () => {
|
|
|
279
380
|
},
|
|
280
381
|
]);
|
|
281
382
|
|
|
282
|
-
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
383
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
283
384
|
|
|
284
385
|
expect(result.ok).toBe(true);
|
|
285
386
|
if (!result.ok) return;
|
|
@@ -300,11 +401,11 @@ describe("reconstructState", () => {
|
|
|
300
401
|
expect(result.state.named.build).toBeUndefined();
|
|
301
402
|
});
|
|
302
403
|
|
|
303
|
-
it("empty file (no stage rows): returns no-rows refusal", () => {
|
|
404
|
+
it("empty file (no stage rows): returns no-rows refusal", async () => {
|
|
304
405
|
// Write header only, no stage rows
|
|
305
406
|
writeHeader(tmpDir, baseHeader);
|
|
306
407
|
|
|
307
|
-
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
408
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
308
409
|
|
|
309
410
|
expect(result.ok).toBe(false);
|
|
310
411
|
if (result.ok) return;
|
|
@@ -312,9 +413,112 @@ describe("reconstructState", () => {
|
|
|
312
413
|
expect(result.detail).toBe(baseHeader.runId);
|
|
313
414
|
});
|
|
314
415
|
|
|
315
|
-
it("row
|
|
416
|
+
it("REFUSES (malformed-row) instead of skipping a stage-shaped row that fails the deep guard (T9)", async () => {
|
|
417
|
+
// Fault injection: the trail's failure row lost its valid `status` (torn
|
|
418
|
+
// write, foreign writer). Pre-fix the shallow guard skipped it and the
|
|
419
|
+
// fold replayed the run as if `build` never ran — a resume would route
|
|
420
|
+
// onward past the stage that actually failed.
|
|
421
|
+
writeRunStages([
|
|
422
|
+
{
|
|
423
|
+
session: null,
|
|
424
|
+
stageNumber: 1,
|
|
425
|
+
stage: "plan",
|
|
426
|
+
skill: "plan",
|
|
427
|
+
status: "completed",
|
|
428
|
+
ts: "t1",
|
|
429
|
+
output: fakeOutput(),
|
|
430
|
+
},
|
|
431
|
+
]);
|
|
432
|
+
appendFileSync(
|
|
433
|
+
stateFilePath(tmpDir, baseHeader.runId),
|
|
434
|
+
`${JSON.stringify({ stageNumber: 2, stage: "build", skill: "build", status: "exploded", ts: "t2" })}\n`,
|
|
435
|
+
"utf-8",
|
|
436
|
+
);
|
|
437
|
+
|
|
438
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
439
|
+
expect(result.ok).toBe(false);
|
|
440
|
+
if (result.ok) return;
|
|
441
|
+
expect(result.reason).toBe("malformed-row");
|
|
442
|
+
expect(result.detail).toContain('stage row 2 ("build")');
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it("REFUSES (malformed-row) a pre-session-provenance row — no `session` key", async () => {
|
|
446
|
+
// A dev-local file written before session provenance landed: stage rows
|
|
447
|
+
// carry no `session` key. v1 never shipped, so the shape was redefined
|
|
448
|
+
// in place — the strict guard refuses; the remedy is wiping
|
|
449
|
+
// `.rpiv/workflows/runs/`.
|
|
450
|
+
writeRunStages([
|
|
451
|
+
{
|
|
452
|
+
session: null,
|
|
453
|
+
stageNumber: 1,
|
|
454
|
+
stage: "plan",
|
|
455
|
+
skill: "plan",
|
|
456
|
+
status: "completed",
|
|
457
|
+
ts: "t1",
|
|
458
|
+
output: fakeOutput(),
|
|
459
|
+
},
|
|
460
|
+
]);
|
|
461
|
+
appendFileSync(
|
|
462
|
+
stateFilePath(tmpDir, baseHeader.runId),
|
|
463
|
+
`${JSON.stringify({ stageNumber: 2, stage: "build", skill: "build", status: "failed", ts: "t2", errMsg: "x" })}\n`,
|
|
464
|
+
"utf-8",
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
468
|
+
expect(result.ok).toBe(false);
|
|
469
|
+
if (result.ok) return;
|
|
470
|
+
expect(result.reason).toBe("malformed-row");
|
|
471
|
+
expect(result.detail).toContain('stage row 2 ("build")');
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
it("REFUSES (version-mismatch) a header written under an unknown schema version (T5)", async () => {
|
|
475
|
+
writeRunStages([
|
|
476
|
+
{
|
|
477
|
+
session: null,
|
|
478
|
+
stageNumber: 1,
|
|
479
|
+
stage: "plan",
|
|
480
|
+
skill: "plan",
|
|
481
|
+
status: "completed",
|
|
482
|
+
ts: "t1",
|
|
483
|
+
output: fakeOutput(),
|
|
484
|
+
},
|
|
485
|
+
]);
|
|
486
|
+
|
|
487
|
+
const result = await reconstructState(tmpDir, linearWorkflow, { ...baseHeader, v: 2 });
|
|
488
|
+
expect(result.ok).toBe(false);
|
|
489
|
+
if (result.ok) return;
|
|
490
|
+
expect(result.reason).toBe("version-mismatch");
|
|
491
|
+
expect(result.detail).toContain("schema v2");
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it("treats an absent header `v` as version 1 and resumes (T5 back-compat rule)", async () => {
|
|
495
|
+
// baseHeader deliberately carries no `v` — files written before the
|
|
496
|
+
// field existed must keep resuming.
|
|
497
|
+
expect(baseHeader.v).toBeUndefined();
|
|
498
|
+
writeRunStages([
|
|
499
|
+
{
|
|
500
|
+
session: null,
|
|
501
|
+
stageNumber: 1,
|
|
502
|
+
stage: "plan",
|
|
503
|
+
skill: "plan",
|
|
504
|
+
status: "completed",
|
|
505
|
+
ts: "t1",
|
|
506
|
+
output: fakeOutput(),
|
|
507
|
+
},
|
|
508
|
+
]);
|
|
509
|
+
|
|
510
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
511
|
+
expect(result.ok).toBe(true);
|
|
512
|
+
|
|
513
|
+
// An explicit v: 1 resumes identically.
|
|
514
|
+
const explicit = await reconstructState(tmpDir, linearWorkflow, { ...baseHeader, v: 1 });
|
|
515
|
+
expect(explicit.ok).toBe(true);
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
it("row whose stage is not in workflow.stages: returns stage-gone refusal", async () => {
|
|
316
519
|
writeRunStages([
|
|
317
520
|
{
|
|
521
|
+
session: null,
|
|
318
522
|
stageNumber: 1,
|
|
319
523
|
stage: "plan",
|
|
320
524
|
skill: "plan",
|
|
@@ -322,6 +526,7 @@ describe("reconstructState", () => {
|
|
|
322
526
|
ts: "2026-06-03T07:31:00Z",
|
|
323
527
|
},
|
|
324
528
|
{
|
|
529
|
+
session: null,
|
|
325
530
|
stageNumber: 2,
|
|
326
531
|
stage: "renamed-away",
|
|
327
532
|
skill: "renamed-away",
|
|
@@ -331,7 +536,7 @@ describe("reconstructState", () => {
|
|
|
331
536
|
]);
|
|
332
537
|
|
|
333
538
|
// linearWorkflow only has plan, build, deploy — no "renamed-away"
|
|
334
|
-
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
539
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
335
540
|
|
|
336
541
|
expect(result.ok).toBe(false);
|
|
337
542
|
if (result.ok) return;
|
|
@@ -339,112 +544,123 @@ describe("reconstructState", () => {
|
|
|
339
544
|
expect(result.detail).toBe("renamed-away");
|
|
340
545
|
});
|
|
341
546
|
|
|
342
|
-
|
|
547
|
+
// -------------------------------------------------------------------------
|
|
548
|
+
// Loop-unit rows — keyed on the STRUCTURED `parent` field
|
|
549
|
+
// -------------------------------------------------------------------------
|
|
550
|
+
|
|
551
|
+
it("fanout generation: units publish to the named channel; generation close restores the entry primary", async () => {
|
|
343
552
|
const planArt = fakeArtifact("plans/p1.md");
|
|
344
|
-
const
|
|
345
|
-
|
|
553
|
+
const u1 = fakeArtifact("builds/b1.md");
|
|
554
|
+
const u2 = fakeArtifact("builds/b2.md");
|
|
555
|
+
const tail = fakeArtifact("deploys/d1.md");
|
|
556
|
+
const units: FanoutFn = () => [
|
|
557
|
+
{ prompt: "u1", label: "phase 1/2", id: "phase-1" },
|
|
558
|
+
{ prompt: "u2", label: "phase 2/2", id: "phase-2" },
|
|
559
|
+
];
|
|
346
560
|
const wf: Workflow = {
|
|
347
561
|
name: "test-wf",
|
|
348
562
|
start: "plan",
|
|
349
563
|
stages: {
|
|
350
|
-
plan: {
|
|
351
|
-
build: {
|
|
564
|
+
plan: produces({ outcome: makeOutcome("plans") }),
|
|
565
|
+
build: produces({ outcome: makeOutcome("builds"), loop: fanout({ units }) }),
|
|
566
|
+
deploy: produces({ outcome: makeOutcome("deploys") }),
|
|
352
567
|
},
|
|
353
|
-
edges: { plan: "build", build: "stop" },
|
|
568
|
+
edges: { plan: "build", build: "deploy", deploy: "stop" },
|
|
354
569
|
} as Workflow;
|
|
355
570
|
|
|
356
571
|
writeRunStages([
|
|
357
|
-
{
|
|
358
|
-
|
|
359
|
-
|
|
572
|
+
{
|
|
573
|
+
session: null,
|
|
574
|
+
stageNumber: 1,
|
|
575
|
+
stage: "plan",
|
|
576
|
+
skill: "plan",
|
|
577
|
+
status: "completed",
|
|
578
|
+
ts: "t1",
|
|
579
|
+
output: fakeOutput([planArt]),
|
|
580
|
+
},
|
|
581
|
+
fanoutUnitRow("build", "phase-1", 0, 2, fakeOutput([u1])),
|
|
582
|
+
fanoutUnitRow("build", "phase-2", 1, 3, fakeOutput([u2])),
|
|
583
|
+
{
|
|
584
|
+
session: null,
|
|
585
|
+
stageNumber: 4,
|
|
586
|
+
stage: "deploy",
|
|
587
|
+
skill: "deploy",
|
|
588
|
+
status: "completed",
|
|
589
|
+
ts: "t4",
|
|
590
|
+
output: fakeOutput([tail]),
|
|
591
|
+
},
|
|
360
592
|
]);
|
|
361
593
|
|
|
362
|
-
const result = reconstructState(tmpDir, wf, baseHeader);
|
|
594
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
363
595
|
expect(result.ok).toBe(true);
|
|
364
596
|
if (!result.ok) return;
|
|
365
597
|
|
|
366
|
-
//
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
598
|
+
// Generation closed before deploy → primary restored to the entry (planArt),
|
|
599
|
+
// then deploy rolled it forward to its own artifact.
|
|
600
|
+
expect(result.state.primaryArtifact).toStrictEqual(tail);
|
|
601
|
+
// Both units published into the builds channel (every unit collects).
|
|
602
|
+
expect(result.state.named.builds?.map((o) => o.artifacts[0])).toEqual([u1, u2]);
|
|
603
|
+
// Counters: plan + 2 units + deploy.
|
|
604
|
+
expect(result.state.stagesCompleted).toBe(4);
|
|
370
605
|
// Parent is visited; the decorated keys are not.
|
|
371
|
-
expect(result.visited).toEqual(new Set(["plan", "build"]));
|
|
372
|
-
//
|
|
373
|
-
expect(result.
|
|
606
|
+
expect(result.visited).toEqual(new Set(["plan", "build", "deploy"]));
|
|
607
|
+
// Generation closed → no trailing open generation.
|
|
608
|
+
expect(result.trailing).toBeUndefined();
|
|
609
|
+
expect(result.drift).toBeUndefined();
|
|
374
610
|
});
|
|
375
611
|
|
|
376
|
-
it("
|
|
612
|
+
it("fanout drift: a recomputed unit id differs from a recorded row → ok with drift set, state still applied", async () => {
|
|
613
|
+
const units: FanoutFn = () => [{ prompt: "x", label: "task", id: "task-1" }];
|
|
377
614
|
const wf: Workflow = {
|
|
378
615
|
name: "test-wf",
|
|
379
616
|
start: "build",
|
|
380
|
-
stages: { build: {
|
|
617
|
+
stages: { build: produces({ outcome: makeOutcome("builds"), loop: fanout({ units }) }) },
|
|
381
618
|
edges: { build: "stop" },
|
|
382
619
|
} as Workflow;
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
const result = reconstructState(tmpDir, wf, baseHeader);
|
|
620
|
+
|
|
621
|
+
writeRunStages([fanoutUnitRow("build", "phase-1", 0, 1, fakeOutput([fakeArtifact("builds/b1.md")]))]);
|
|
622
|
+
|
|
623
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
388
624
|
expect(result.ok).toBe(true);
|
|
389
625
|
if (!result.ok) return;
|
|
626
|
+
|
|
627
|
+
// Drift recorded against the parent; the fold still applied the row so state is complete.
|
|
628
|
+
expect(result.drift?.parent).toBe("build");
|
|
629
|
+
expect(result.drift?.errMsg).toMatch(/deterministic/);
|
|
390
630
|
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
631
|
});
|
|
394
632
|
|
|
395
|
-
it("
|
|
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;
|
|
633
|
+
it("legacy decorated row without `parent` refuses stage-gone (pre-redesign run, no migration)", async () => {
|
|
406
634
|
writeRunStages([
|
|
407
|
-
{ stageNumber: 1, stage: "build (
|
|
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" },
|
|
635
|
+
{ session: null, stageNumber: 1, stage: "build (phase 1/2)", skill: "build", status: "completed", ts: "t1" },
|
|
420
636
|
]);
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
expect(result.
|
|
426
|
-
|
|
427
|
-
expect(result.state.stagesCompleted).toBe(4);
|
|
428
|
-
expect(result.visited).toEqual(new Set(["build", "review"]));
|
|
637
|
+
|
|
638
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
639
|
+
expect(result.ok).toBe(false);
|
|
640
|
+
if (result.ok) return;
|
|
641
|
+
expect(result.reason).toBe("stage-gone");
|
|
642
|
+
expect(result.detail).toBe("build (phase 1/2)");
|
|
429
643
|
});
|
|
430
644
|
|
|
431
|
-
it("
|
|
645
|
+
it("iterate generation: rolls the primary forward; trailing open generation carries the accumulated cursor", async () => {
|
|
432
646
|
const reviewArt = fakeArtifact("reviews/r1.md");
|
|
433
|
-
const
|
|
434
|
-
const
|
|
435
|
-
|
|
647
|
+
const p1 = fakeArtifact("plans/p1.md");
|
|
648
|
+
const p2 = fakeArtifact("plans/p2.md");
|
|
649
|
+
const next: IterateFn = ({ index }) =>
|
|
650
|
+
index >= 2 ? null : { prompt: `p${index + 1}`, label: `phase ${index + 1}`, id: `phase-${index + 1}` };
|
|
436
651
|
const wf: Workflow = {
|
|
437
652
|
name: "test-wf",
|
|
438
653
|
start: "review",
|
|
439
654
|
stages: {
|
|
440
|
-
review: {
|
|
441
|
-
blueprint: {
|
|
655
|
+
review: produces({ outcome: makeOutcome("reviews") }),
|
|
656
|
+
blueprint: produces({ outcome: makeOutcome("plans"), loop: iterate({ next }) }),
|
|
442
657
|
},
|
|
443
658
|
edges: { review: "blueprint", blueprint: "stop" },
|
|
444
659
|
} as Workflow;
|
|
445
660
|
|
|
446
661
|
writeRunStages([
|
|
447
662
|
{
|
|
663
|
+
session: null,
|
|
448
664
|
stageNumber: 1,
|
|
449
665
|
stage: "review",
|
|
450
666
|
skill: "review",
|
|
@@ -452,158 +668,252 @@ describe("reconstructState", () => {
|
|
|
452
668
|
ts: "t1",
|
|
453
669
|
output: fakeOutput([reviewArt]),
|
|
454
670
|
},
|
|
455
|
-
|
|
456
|
-
|
|
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
|
-
},
|
|
671
|
+
fanoutUnitRow("blueprint", "phase-1", 0, 2, fakeOutput([p1])),
|
|
672
|
+
fanoutUnitRow("blueprint", "phase-2", 1, 3, fakeOutput([p2])),
|
|
471
673
|
]);
|
|
472
674
|
|
|
473
|
-
const result = reconstructState(tmpDir, wf, baseHeader);
|
|
675
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
474
676
|
expect(result.ok).toBe(true);
|
|
475
677
|
if (!result.ok) return;
|
|
476
678
|
|
|
477
|
-
//
|
|
478
|
-
expect(result.state.
|
|
479
|
-
|
|
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.
|
|
679
|
+
// Primary rolled forward to the last unit (iterate units roll, unlike fanout).
|
|
680
|
+
expect(result.state.primaryArtifact).toStrictEqual(p2);
|
|
681
|
+
expect(result.state.named.plans?.map((o) => o.artifacts[0])).toEqual([p1, p2]);
|
|
484
682
|
expect(result.visited).toEqual(new Set(["review", "blueprint"]));
|
|
485
|
-
//
|
|
486
|
-
|
|
487
|
-
expect(
|
|
488
|
-
expect(
|
|
683
|
+
// Trailing open generation carries the reconstructed driver cursor + FROZEN entry.
|
|
684
|
+
expect(result.trailing?.parent).toBe("blueprint");
|
|
685
|
+
expect(result.trailing?.cursor.index).toBe(2);
|
|
686
|
+
expect(result.trailing?.cursor.accumulated.map((o) => o.artifacts[0])).toEqual([p1, p2]);
|
|
687
|
+
expect(result.trailing?.entryArtifact).toStrictEqual(reviewArt);
|
|
688
|
+
expect(result.drift).toBeUndefined();
|
|
489
689
|
});
|
|
490
690
|
|
|
491
|
-
it("
|
|
691
|
+
it("assess generation: producer + judge rows publish to separate channels; close restores the last producer pair", async () => {
|
|
692
|
+
const p0 = fakeArtifact("tasks/t0.md");
|
|
693
|
+
const v0 = fakeArtifact("verdicts/v0.json");
|
|
694
|
+
const p1 = fakeArtifact("tasks/t1.md");
|
|
695
|
+
const v1 = fakeArtifact("verdicts/v1.json");
|
|
696
|
+
const g1 = fakeArtifact("gates/g1.md");
|
|
697
|
+
const loop = assess({
|
|
698
|
+
judge: judge({ skill: "grade", outcome: makeOutcome("verdict") }),
|
|
699
|
+
done: () => false,
|
|
700
|
+
feedForward: () => "more",
|
|
701
|
+
});
|
|
492
702
|
const wf: Workflow = {
|
|
493
703
|
name: "test-wf",
|
|
494
|
-
start: "
|
|
704
|
+
start: "breakdown",
|
|
495
705
|
stages: {
|
|
496
|
-
|
|
706
|
+
breakdown: produces({ outcome: makeOutcome("tasks"), loop }),
|
|
707
|
+
gate: produces({ outcome: makeOutcome("gates") }),
|
|
497
708
|
},
|
|
498
|
-
edges: {
|
|
709
|
+
edges: { breakdown: "gate", gate: "stop" },
|
|
499
710
|
} as Workflow;
|
|
711
|
+
|
|
500
712
|
writeRunStages([
|
|
713
|
+
assessProduceRow("breakdown", 0, 1, fakeOutput([p0])),
|
|
714
|
+
assessJudgeRow("breakdown", "grade", 0, 2, fakeOutput([v0])),
|
|
715
|
+
assessProduceRow("breakdown", 1, 3, fakeOutput([p1])),
|
|
716
|
+
assessJudgeRow("breakdown", "grade", 1, 4, fakeOutput([v1])),
|
|
501
717
|
{
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
718
|
+
session: null,
|
|
719
|
+
stageNumber: 5,
|
|
720
|
+
stage: "gate",
|
|
721
|
+
skill: "gate",
|
|
505
722
|
status: "completed",
|
|
506
|
-
ts: "
|
|
507
|
-
output: fakeOutput([
|
|
508
|
-
},
|
|
509
|
-
{
|
|
510
|
-
stageNumber: 2,
|
|
511
|
-
stage: "blueprint (phase-2)",
|
|
512
|
-
skill: "blueprint",
|
|
513
|
-
status: "failed",
|
|
514
|
-
ts: "t2",
|
|
515
|
-
errMsg: "boom",
|
|
723
|
+
ts: "t5",
|
|
724
|
+
output: fakeOutput([g1]),
|
|
516
725
|
},
|
|
517
726
|
]);
|
|
518
|
-
|
|
727
|
+
|
|
728
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
519
729
|
expect(result.ok).toBe(true);
|
|
520
730
|
if (!result.ok) return;
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
expect(result.
|
|
731
|
+
|
|
732
|
+
// Producers and verdicts live in distinct named channels.
|
|
733
|
+
expect(result.state.named.tasks?.map((o) => o.artifacts[0])).toEqual([p0, p1]);
|
|
734
|
+
expect(result.state.named.verdict?.map((o) => o.artifacts[0])).toEqual([v0, v1]);
|
|
735
|
+
// Generation closed before gate → "last" projects the last producer (p1), THEN gate rolled it.
|
|
736
|
+
expect(result.state.primaryArtifact).toStrictEqual(g1);
|
|
737
|
+
// Every completed row of either phase bumped the counter (2 produce + 2 judge + gate).
|
|
738
|
+
expect(result.state.stagesCompleted).toBe(5);
|
|
739
|
+
expect(result.visited).toEqual(new Set(["breakdown", "gate"]));
|
|
740
|
+
expect(result.trailing).toBeUndefined();
|
|
741
|
+
expect(result.drift).toBeUndefined();
|
|
524
742
|
});
|
|
525
743
|
|
|
526
|
-
it("
|
|
527
|
-
const
|
|
528
|
-
const
|
|
744
|
+
it("assess × panel: each member verdict publishes to its OWN channel; the fold publishes to `<stage>-panel`", async () => {
|
|
745
|
+
const p0 = fakeArtifact("tasks/t0.md");
|
|
746
|
+
const outA = memberVerdict("verdicts/v0a.json", true);
|
|
747
|
+
const outB = memberVerdict("verdicts/v0b.json", true);
|
|
748
|
+
const outC = memberVerdict("verdicts/v0c.json", false); // 2-of-3 pass → majority pass
|
|
749
|
+
const loop = assess({
|
|
750
|
+
judge: panel({
|
|
751
|
+
members: [
|
|
752
|
+
judge({ skill: "grade-a", outcome: makeOutcome("verdict-a") }),
|
|
753
|
+
judge({ skill: "grade-b", outcome: makeOutcome("verdict-b") }),
|
|
754
|
+
judge({ skill: "grade-c", outcome: makeOutcome("verdict-c") }),
|
|
755
|
+
],
|
|
756
|
+
fold: majority((v) => Boolean((v.data as { done?: boolean }).done)),
|
|
757
|
+
}),
|
|
758
|
+
done: (v) => Boolean((v.data as { pass?: boolean }).pass),
|
|
759
|
+
feedForward: () => "more",
|
|
760
|
+
});
|
|
529
761
|
const wf: Workflow = {
|
|
530
762
|
name: "test-wf",
|
|
531
|
-
start: "
|
|
532
|
-
stages: {
|
|
533
|
-
|
|
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" },
|
|
763
|
+
start: "breakdown",
|
|
764
|
+
stages: { breakdown: produces({ outcome: makeOutcome("tasks"), loop }) },
|
|
765
|
+
edges: { breakdown: "stop" },
|
|
538
766
|
} as Workflow;
|
|
767
|
+
|
|
539
768
|
writeRunStages([
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
769
|
+
assessProduceRow("breakdown", 0, 1, fakeOutput([p0])),
|
|
770
|
+
assessPanelJudgeRow("breakdown", "grade-a", 0, 0, 2, outA),
|
|
771
|
+
assessPanelJudgeRow("breakdown", "grade-b", 0, 1, 3, outB),
|
|
772
|
+
assessPanelJudgeRow("breakdown", "grade-c", 0, 2, 4, outC),
|
|
773
|
+
]);
|
|
774
|
+
|
|
775
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
776
|
+
expect(result.ok).toBe(true);
|
|
777
|
+
if (!result.ok) return;
|
|
778
|
+
expect(result.drift).toBeUndefined();
|
|
779
|
+
|
|
780
|
+
// Each member rolled onto its OWN channel — the fold replays each row through
|
|
781
|
+
// THAT member's def (`panelMembers[memberIndex]`), never member 0's. Pre-fix,
|
|
782
|
+
// members b + c silently landed on `verdict-a`.
|
|
783
|
+
expect(result.state.named["verdict-a"]).toEqual([outA]);
|
|
784
|
+
expect(result.state.named["verdict-b"]).toEqual([outB]);
|
|
785
|
+
expect(result.state.named["verdict-c"]).toEqual([outC]);
|
|
786
|
+
// The folded verdict — recomputed-and-republished at the same transition the
|
|
787
|
+
// live driver folds — is a data-only Output (no artifact) carrying the
|
|
788
|
+
// canonical PANEL_VERDICT shape, on the per-stage `<stage>-panel` channel.
|
|
789
|
+
const folded = result.state.named["breakdown-panel"];
|
|
790
|
+
expect(folded).toHaveLength(1);
|
|
791
|
+
expect(folded?.[0]?.artifacts).toEqual([]);
|
|
792
|
+
expect(folded?.[0]?.data).toEqual({ pass: true, votes: { pass: 2, fail: 1 }, agreement: 2 / 3, tie: false });
|
|
793
|
+
});
|
|
794
|
+
|
|
795
|
+
it("assess × panel: a THROWING fold becomes drift (recorded terminal failure), never an unguarded rejection", async () => {
|
|
796
|
+
// The fold runs the author `panel.fold` via `advanceCursor` on the LAST
|
|
797
|
+
// member. It lives in `reconstructState`, which `resumeWorkflow` awaits
|
|
798
|
+
// BEFORE `executeRun` brackets the lifecycle — so before the guard a throw
|
|
799
|
+
// escaped as a raw promise rejection (no JSONL failure row, no
|
|
800
|
+
// `onWorkflowEnd`). It must instead land as drift: this call RESOLVES
|
|
801
|
+
// (ok=true, drift set) rather than rejecting.
|
|
802
|
+
const p0 = fakeArtifact("tasks/t0.md");
|
|
803
|
+
const outA = memberVerdict("verdicts/v0a.json", true);
|
|
804
|
+
const outB = memberVerdict("verdicts/v0b.json", true);
|
|
805
|
+
const outC = memberVerdict("verdicts/v0c.json", false);
|
|
806
|
+
const loop = assess({
|
|
807
|
+
judge: panel({
|
|
808
|
+
members: [
|
|
809
|
+
judge({ skill: "grade-a", outcome: makeOutcome("verdict-a") }),
|
|
810
|
+
judge({ skill: "grade-b", outcome: makeOutcome("verdict-b") }),
|
|
811
|
+
judge({ skill: "grade-c", outcome: makeOutcome("verdict-c") }),
|
|
812
|
+
],
|
|
813
|
+
// Raw fold (requires an `outcome`) that throws on the last-member fold.
|
|
814
|
+
fold: () => {
|
|
815
|
+
throw new Error("fold exploded");
|
|
816
|
+
},
|
|
817
|
+
outcome: makeOutcome("breakdown-panel"),
|
|
818
|
+
}),
|
|
819
|
+
done: (v) => Boolean((v.data as { pass?: boolean }).pass),
|
|
820
|
+
feedForward: () => "more",
|
|
821
|
+
});
|
|
822
|
+
const wf: Workflow = {
|
|
823
|
+
name: "test-wf",
|
|
824
|
+
start: "breakdown",
|
|
825
|
+
stages: { breakdown: produces({ outcome: makeOutcome("tasks"), loop }) },
|
|
826
|
+
edges: { breakdown: "stop" },
|
|
827
|
+
} as Workflow;
|
|
828
|
+
|
|
829
|
+
writeRunStages([
|
|
830
|
+
assessProduceRow("breakdown", 0, 1, fakeOutput([p0])),
|
|
831
|
+
assessPanelJudgeRow("breakdown", "grade-a", 0, 0, 2, outA),
|
|
832
|
+
assessPanelJudgeRow("breakdown", "grade-b", 0, 1, 3, outB),
|
|
833
|
+
assessPanelJudgeRow("breakdown", "grade-c", 0, 2, 4, outC),
|
|
573
834
|
]);
|
|
574
|
-
|
|
835
|
+
|
|
836
|
+
// Resolves with drift set — the fold did not reject out of reconstructState.
|
|
837
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
575
838
|
expect(result.ok).toBe(true);
|
|
576
839
|
if (!result.ok) return;
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
840
|
+
expect(result.drift?.parent).toBe("breakdown");
|
|
841
|
+
expect(result.drift?.errMsg).toMatch(/fold exploded/);
|
|
842
|
+
// State is still complete up to the throw: the last member's verdict applied
|
|
843
|
+
// (on its OWN channel) BEFORE `advanceCursor` ran the throwing fold.
|
|
844
|
+
expect(result.state.named["verdict-c"]).toEqual([outC]);
|
|
845
|
+
// The fold never produced a value, so no folded verdict was published.
|
|
846
|
+
expect(result.state.named["breakdown-panel"]).toBeUndefined();
|
|
583
847
|
});
|
|
584
848
|
|
|
585
|
-
it("
|
|
849
|
+
it("REPLAY PARITY (C1): the fold's trailing cursor is byte-equal to a live cursor advanced over the same outputs", async () => {
|
|
850
|
+
// `advanceCursor` is the ONE cursor state machine; this test pins the
|
|
851
|
+
// contract that the fold and the live driver advance identically. The
|
|
852
|
+
// trail ends mid-assess (produce, judge, produce — generation open) so
|
|
853
|
+
// the fold returns its reconstructed cursor for comparison.
|
|
854
|
+
const p0 = fakeOutput([fakeArtifact("tasks/t0.md")]);
|
|
855
|
+
const v0 = fakeOutput([fakeArtifact("verdicts/v0.json")]);
|
|
856
|
+
const p1 = fakeOutput([fakeArtifact("tasks/t1.md")]);
|
|
857
|
+
const loop = assess({
|
|
858
|
+
judge: judge({ skill: "grade", outcome: makeOutcome("verdict") }),
|
|
859
|
+
done: () => false,
|
|
860
|
+
feedForward: () => "more",
|
|
861
|
+
});
|
|
586
862
|
const wf: Workflow = {
|
|
587
863
|
name: "test-wf",
|
|
588
|
-
start: "
|
|
589
|
-
stages: {
|
|
590
|
-
|
|
591
|
-
"build-extra": { kind: "produces", sessionPolicy: "fresh", fanout: () => [] },
|
|
592
|
-
},
|
|
593
|
-
edges: { build: "build-extra", "build-extra": "stop" },
|
|
864
|
+
start: "breakdown",
|
|
865
|
+
stages: { breakdown: produces({ outcome: makeOutcome("tasks"), loop }) },
|
|
866
|
+
edges: { breakdown: "stop" },
|
|
594
867
|
} as Workflow;
|
|
868
|
+
|
|
595
869
|
writeRunStages([
|
|
596
|
-
|
|
597
|
-
|
|
870
|
+
assessProduceRow("breakdown", 0, 1, p0),
|
|
871
|
+
assessJudgeRow("breakdown", "grade", 0, 2, v0),
|
|
872
|
+
assessProduceRow("breakdown", 1, 3, p1),
|
|
598
873
|
]);
|
|
599
|
-
|
|
874
|
+
|
|
875
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
876
|
+
expect(result.ok).toBe(true);
|
|
877
|
+
if (!result.ok) return;
|
|
878
|
+
expect(result.drift).toBeUndefined();
|
|
879
|
+
|
|
880
|
+
// The live driver's exact sequence for the same three completed units.
|
|
881
|
+
const live = freshCursor();
|
|
882
|
+
advanceCursor(live, "produce", p0, loop);
|
|
883
|
+
advanceCursor(live, "judge", v0, loop);
|
|
884
|
+
advanceCursor(live, "produce", p1, loop);
|
|
885
|
+
|
|
886
|
+
expect(JSON.stringify(result.trailing?.cursor)).toBe(JSON.stringify(live));
|
|
887
|
+
});
|
|
888
|
+
|
|
889
|
+
it("REPLAY PARITY (C1): fanout trailing cursor matches the live transition", async () => {
|
|
890
|
+
const u1 = fakeOutput([fakeArtifact("builds/b1.md")]);
|
|
891
|
+
const units: FanoutFn = () => [
|
|
892
|
+
{ prompt: "u1", label: "phase 1/2", id: "phase-1" },
|
|
893
|
+
{ prompt: "u2", label: "phase 2/2", id: "phase-2" },
|
|
894
|
+
];
|
|
895
|
+
const loop = fanout({ units });
|
|
896
|
+
const wf: Workflow = {
|
|
897
|
+
name: "test-wf",
|
|
898
|
+
start: "build",
|
|
899
|
+
stages: { build: produces({ outcome: makeOutcome("builds"), loop }) },
|
|
900
|
+
edges: { build: "stop" },
|
|
901
|
+
} as Workflow;
|
|
902
|
+
|
|
903
|
+
writeRunStages([fanoutUnitRow("build", "phase-1", 0, 1, u1)]);
|
|
904
|
+
|
|
905
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
600
906
|
expect(result.ok).toBe(true);
|
|
601
907
|
if (!result.ok) return;
|
|
602
|
-
expect(result.
|
|
603
|
-
|
|
908
|
+
expect(result.drift).toBeUndefined();
|
|
909
|
+
|
|
910
|
+
const live = freshCursor();
|
|
911
|
+
advanceCursor(live, "produce", u1, loop);
|
|
912
|
+
|
|
913
|
+
expect(JSON.stringify(result.trailing?.cursor)).toBe(JSON.stringify(live));
|
|
604
914
|
});
|
|
605
915
|
|
|
606
|
-
it("named slot accumulates across completed rows for the same key (append order)", () => {
|
|
916
|
+
it("named slot accumulates across completed rows for the same key (append order)", async () => {
|
|
607
917
|
// Simulate a backward-jump loop: plan runs twice, producing two outputs.
|
|
608
918
|
const art1 = fakeArtifact("plans/p1.md");
|
|
609
919
|
const art2 = fakeArtifact("plans/p2.md");
|
|
@@ -612,6 +922,7 @@ describe("reconstructState", () => {
|
|
|
612
922
|
|
|
613
923
|
writeRunStages([
|
|
614
924
|
{
|
|
925
|
+
session: null,
|
|
615
926
|
stageNumber: 1,
|
|
616
927
|
stage: "plan",
|
|
617
928
|
skill: "plan",
|
|
@@ -620,6 +931,7 @@ describe("reconstructState", () => {
|
|
|
620
931
|
output: out1,
|
|
621
932
|
},
|
|
622
933
|
{
|
|
934
|
+
session: null,
|
|
623
935
|
stageNumber: 2,
|
|
624
936
|
stage: "plan",
|
|
625
937
|
skill: "plan",
|
|
@@ -629,7 +941,7 @@ describe("reconstructState", () => {
|
|
|
629
941
|
},
|
|
630
942
|
]);
|
|
631
943
|
|
|
632
|
-
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
944
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
633
945
|
|
|
634
946
|
expect(result.ok).toBe(true);
|
|
635
947
|
if (!result.ok) return;
|
|
@@ -641,12 +953,13 @@ describe("reconstructState", () => {
|
|
|
641
953
|
expect(result.state.stagesCompleted).toBe(2);
|
|
642
954
|
});
|
|
643
955
|
|
|
644
|
-
it("aborted row is treated like a failed row: excluded from state seeding", () => {
|
|
956
|
+
it("aborted row is treated like a failed row: excluded from state seeding", async () => {
|
|
645
957
|
const art1 = fakeArtifact("plans/p1.md");
|
|
646
958
|
const out1 = fakeOutput([art1]);
|
|
647
959
|
|
|
648
960
|
writeRunStages([
|
|
649
961
|
{
|
|
962
|
+
session: null,
|
|
650
963
|
stageNumber: 1,
|
|
651
964
|
stage: "plan",
|
|
652
965
|
skill: "plan",
|
|
@@ -655,6 +968,7 @@ describe("reconstructState", () => {
|
|
|
655
968
|
output: out1,
|
|
656
969
|
},
|
|
657
970
|
{
|
|
971
|
+
session: null,
|
|
658
972
|
stageNumber: 2,
|
|
659
973
|
stage: "build",
|
|
660
974
|
skill: "build",
|
|
@@ -664,7 +978,7 @@ describe("reconstructState", () => {
|
|
|
664
978
|
},
|
|
665
979
|
]);
|
|
666
980
|
|
|
667
|
-
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
981
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
668
982
|
|
|
669
983
|
expect(result.ok).toBe(true);
|
|
670
984
|
if (!result.ok) return;
|
|
@@ -675,12 +989,13 @@ describe("reconstructState", () => {
|
|
|
675
989
|
expect(result.lastStageNumber).toBe(2);
|
|
676
990
|
});
|
|
677
991
|
|
|
678
|
-
it("skipped row is excluded from state seeding", () => {
|
|
992
|
+
it("skipped row is excluded from state seeding", async () => {
|
|
679
993
|
const art1 = fakeArtifact("plans/p1.md");
|
|
680
994
|
const out1 = fakeOutput([art1]);
|
|
681
995
|
|
|
682
996
|
writeRunStages([
|
|
683
997
|
{
|
|
998
|
+
session: null,
|
|
684
999
|
stageNumber: 1,
|
|
685
1000
|
stage: "plan",
|
|
686
1001
|
skill: "plan",
|
|
@@ -689,6 +1004,7 @@ describe("reconstructState", () => {
|
|
|
689
1004
|
output: out1,
|
|
690
1005
|
},
|
|
691
1006
|
{
|
|
1007
|
+
session: null,
|
|
692
1008
|
stageNumber: 2,
|
|
693
1009
|
stage: "build",
|
|
694
1010
|
skill: "build",
|
|
@@ -697,7 +1013,7 @@ describe("reconstructState", () => {
|
|
|
697
1013
|
},
|
|
698
1014
|
]);
|
|
699
1015
|
|
|
700
|
-
const result = reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
1016
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
701
1017
|
|
|
702
1018
|
expect(result.ok).toBe(true);
|
|
703
1019
|
if (!result.ok) return;
|
|
@@ -715,7 +1031,7 @@ describe("reconstructState", () => {
|
|
|
715
1031
|
const RPIV_ARTIFACT_PATTERN = /\.rpiv\/artifacts\/[\w.-]+\/[\w.-]+\.md/g;
|
|
716
1032
|
|
|
717
1033
|
/** Minimal outcome that scans assistant text for .rpiv/artifacts paths. */
|
|
718
|
-
const artifactOutcome: import("../output.js").
|
|
1034
|
+
const artifactOutcome: import("../output-spec.js").Outcome<unknown, "artifact-md", Record<string, unknown>> = {
|
|
719
1035
|
collector: {
|
|
720
1036
|
collect: (ctx) => {
|
|
721
1037
|
let lastMatch: string | undefined;
|
|
@@ -761,6 +1077,354 @@ const twoStageWf: Workflow = {
|
|
|
761
1077
|
},
|
|
762
1078
|
};
|
|
763
1079
|
|
|
1080
|
+
// --- verify row builders (siblings of assessProduceRow/assessJudgeRow) ---
|
|
1081
|
+
|
|
1082
|
+
const verifyAttemptRow = (parent: string, attempt: number, num: number, output: Output): WorkflowStage => ({
|
|
1083
|
+
session: null,
|
|
1084
|
+
stageNumber: num,
|
|
1085
|
+
stage: `${parent} (a${attempt}·attempt)`,
|
|
1086
|
+
skill: parent,
|
|
1087
|
+
status: "completed",
|
|
1088
|
+
ts: `t${num}`,
|
|
1089
|
+
parent,
|
|
1090
|
+
role: "produce",
|
|
1091
|
+
unitIndex: attempt,
|
|
1092
|
+
output,
|
|
1093
|
+
});
|
|
1094
|
+
|
|
1095
|
+
const verifyVerdictRow = (
|
|
1096
|
+
parent: string,
|
|
1097
|
+
judgeSkill: string,
|
|
1098
|
+
attempt: number,
|
|
1099
|
+
num: number,
|
|
1100
|
+
output: Output,
|
|
1101
|
+
): WorkflowStage => ({
|
|
1102
|
+
session: null,
|
|
1103
|
+
stageNumber: num,
|
|
1104
|
+
stage: `${parent} (a${attempt}·verify)`,
|
|
1105
|
+
skill: judgeSkill,
|
|
1106
|
+
status: "completed",
|
|
1107
|
+
ts: `t${num}`,
|
|
1108
|
+
parent,
|
|
1109
|
+
role: "verify",
|
|
1110
|
+
unitIndex: attempt,
|
|
1111
|
+
output,
|
|
1112
|
+
});
|
|
1113
|
+
|
|
1114
|
+
describe("reconstructState — verify generations", () => {
|
|
1115
|
+
const pass = (v: Output) => Boolean((v.data as { done?: boolean }).done);
|
|
1116
|
+
const gateVerify = () => verify({ judge: judge({ skill: "grade", outcome: makeOutcome("verdict") }), done: pass });
|
|
1117
|
+
const retryVerify = () =>
|
|
1118
|
+
verify({
|
|
1119
|
+
judge: judge({ skill: "grade", outcome: makeOutcome("verdict") }),
|
|
1120
|
+
done: pass,
|
|
1121
|
+
feedForward: () => "again",
|
|
1122
|
+
max: 3,
|
|
1123
|
+
});
|
|
1124
|
+
|
|
1125
|
+
it("closed verify generation: attempts + verdicts publish to separate channels; close restores the producer pair", async () => {
|
|
1126
|
+
const a0 = fakeArtifact("impl/i0.md");
|
|
1127
|
+
const v0 = fakeArtifact("verdicts/v0.json");
|
|
1128
|
+
const a1 = fakeArtifact("impl/i1.md");
|
|
1129
|
+
const v1 = fakeArtifact("verdicts/v1.json");
|
|
1130
|
+
const g1 = fakeArtifact("gates/g1.md");
|
|
1131
|
+
const wf: Workflow = {
|
|
1132
|
+
name: "test-wf",
|
|
1133
|
+
start: "build",
|
|
1134
|
+
stages: {
|
|
1135
|
+
build: produces({ outcome: makeOutcome("impl"), verify: retryVerify() }),
|
|
1136
|
+
gate: produces({ outcome: makeOutcome("gates") }),
|
|
1137
|
+
},
|
|
1138
|
+
edges: { build: "gate", gate: "stop" },
|
|
1139
|
+
} as Workflow;
|
|
1140
|
+
|
|
1141
|
+
writeRunStages([
|
|
1142
|
+
verifyAttemptRow("build", 0, 1, fakeOutput([a0])),
|
|
1143
|
+
verifyVerdictRow("build", "grade", 0, 2, fakeOutput([v0])),
|
|
1144
|
+
verifyAttemptRow("build", 1, 3, fakeOutput([a1])),
|
|
1145
|
+
verifyVerdictRow("build", "grade", 1, 4, fakeOutput([v1])),
|
|
1146
|
+
{
|
|
1147
|
+
session: null,
|
|
1148
|
+
stageNumber: 5,
|
|
1149
|
+
stage: "gate",
|
|
1150
|
+
skill: "gate",
|
|
1151
|
+
status: "completed",
|
|
1152
|
+
ts: "t5",
|
|
1153
|
+
output: fakeOutput([g1]),
|
|
1154
|
+
},
|
|
1155
|
+
]);
|
|
1156
|
+
|
|
1157
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
1158
|
+
expect(result.ok).toBe(true);
|
|
1159
|
+
if (!result.ok) return;
|
|
1160
|
+
|
|
1161
|
+
expect(result.state.named.impl?.map((o) => o.artifacts[0])).toEqual([a0, a1]);
|
|
1162
|
+
expect(result.state.named.verdict?.map((o) => o.artifacts[0])).toEqual([v0, v1]);
|
|
1163
|
+
// Generation closed before gate → "last" projects the last attempt (a1), THEN gate rolled it.
|
|
1164
|
+
expect(result.state.primaryArtifact).toStrictEqual(g1);
|
|
1165
|
+
expect(result.state.stagesCompleted).toBe(5);
|
|
1166
|
+
expect(result.trailing).toBeUndefined();
|
|
1167
|
+
expect(result.drift).toBeUndefined();
|
|
1168
|
+
});
|
|
1169
|
+
|
|
1170
|
+
it("trailing pending verify: a completed-attempt trailer reconstructs cursor phase 'judge'", async () => {
|
|
1171
|
+
const a0 = fakeArtifact("impl/i0.md");
|
|
1172
|
+
const wf: Workflow = {
|
|
1173
|
+
name: "test-wf",
|
|
1174
|
+
start: "build",
|
|
1175
|
+
stages: { build: produces({ outcome: makeOutcome("impl"), verify: gateVerify() }) },
|
|
1176
|
+
edges: { build: "stop" },
|
|
1177
|
+
} as Workflow;
|
|
1178
|
+
|
|
1179
|
+
writeRunStages([verifyAttemptRow("build", 0, 1, fakeOutput([a0]))]);
|
|
1180
|
+
|
|
1181
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
1182
|
+
expect(result.ok).toBe(true);
|
|
1183
|
+
if (!result.ok) return;
|
|
1184
|
+
|
|
1185
|
+
// The synthesized loop is recovered (no stage-gone refusal) and the
|
|
1186
|
+
// cursor says: attempt 0 done, verdict pending.
|
|
1187
|
+
expect(result.trailing?.parent).toBe("build");
|
|
1188
|
+
expect(result.trailing?.cursor.phase).toBe("judge");
|
|
1189
|
+
expect(result.trailing?.cursor.index).toBe(0);
|
|
1190
|
+
expect(result.trailing?.cursor.lastProduce?.artifact).toStrictEqual(a0);
|
|
1191
|
+
expect(result.drift).toBeUndefined();
|
|
1192
|
+
});
|
|
1193
|
+
|
|
1194
|
+
it("role drift: a verdict row carrying role 'judge' on a verify stage trips the guard", async () => {
|
|
1195
|
+
const a0 = fakeArtifact("impl/i0.md");
|
|
1196
|
+
const v0 = fakeArtifact("verdicts/v0.json");
|
|
1197
|
+
const wf: Workflow = {
|
|
1198
|
+
name: "test-wf",
|
|
1199
|
+
start: "build",
|
|
1200
|
+
stages: { build: produces({ outcome: makeOutcome("impl"), verify: gateVerify() }) },
|
|
1201
|
+
edges: { build: "stop" },
|
|
1202
|
+
} as Workflow;
|
|
1203
|
+
|
|
1204
|
+
writeRunStages([
|
|
1205
|
+
verifyAttemptRow("build", 0, 1, fakeOutput([a0])),
|
|
1206
|
+
// Wrong role for a verify stage — the driver would have emitted "verify".
|
|
1207
|
+
assessJudgeRow("build", "grade", 0, 2, fakeOutput([v0])),
|
|
1208
|
+
]);
|
|
1209
|
+
|
|
1210
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
1211
|
+
expect(result.ok).toBe(true);
|
|
1212
|
+
if (!result.ok) return;
|
|
1213
|
+
expect(result.drift?.parent).toBe("build");
|
|
1214
|
+
});
|
|
1215
|
+
|
|
1216
|
+
it("entryArgs freeze: a reads projection is frozen at generation open (a trailing verdict's transient roll can't leak in)", async () => {
|
|
1217
|
+
const d0 = fakeArtifact("design/d0.md");
|
|
1218
|
+
const a0 = fakeArtifact("impl/i0.md");
|
|
1219
|
+
const v0 = fakeArtifact("verdicts/v0.json");
|
|
1220
|
+
const wf: Workflow = {
|
|
1221
|
+
name: "test-wf",
|
|
1222
|
+
start: "design",
|
|
1223
|
+
stages: {
|
|
1224
|
+
design: produces({ outcome: makeOutcome("design") }),
|
|
1225
|
+
build: produces({ outcome: makeOutcome("impl"), reads: ["design"], verify: retryVerify() }),
|
|
1226
|
+
},
|
|
1227
|
+
edges: { design: "build", build: "stop" },
|
|
1228
|
+
} as Workflow;
|
|
1229
|
+
|
|
1230
|
+
writeRunStages([
|
|
1231
|
+
{
|
|
1232
|
+
session: null,
|
|
1233
|
+
stageNumber: 1,
|
|
1234
|
+
stage: "design",
|
|
1235
|
+
skill: "design",
|
|
1236
|
+
status: "completed",
|
|
1237
|
+
ts: "t1",
|
|
1238
|
+
output: fakeOutput([d0]),
|
|
1239
|
+
},
|
|
1240
|
+
verifyAttemptRow("build", 0, 2, fakeOutput([a0])),
|
|
1241
|
+
verifyVerdictRow("build", "grade", 0, 3, fakeOutput([v0])), // fail verdict — generation still open
|
|
1242
|
+
]);
|
|
1243
|
+
|
|
1244
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
1245
|
+
expect(result.ok).toBe(true);
|
|
1246
|
+
if (!result.ok) return;
|
|
1247
|
+
|
|
1248
|
+
expect(result.trailing?.entryArgs).toBe(`--design ${handleToString(d0.handle)}`);
|
|
1249
|
+
});
|
|
1250
|
+
|
|
1251
|
+
it("truncated trail: a reads-bearing verify generation with no producer row yields entryArgs undefined", async () => {
|
|
1252
|
+
const a0 = fakeArtifact("impl/i0.md");
|
|
1253
|
+
const wf: Workflow = {
|
|
1254
|
+
name: "test-wf",
|
|
1255
|
+
start: "design",
|
|
1256
|
+
stages: {
|
|
1257
|
+
design: produces({ outcome: makeOutcome("design") }),
|
|
1258
|
+
build: produces({ outcome: makeOutcome("impl"), reads: ["design"], verify: gateVerify() }),
|
|
1259
|
+
},
|
|
1260
|
+
edges: { design: "build", build: "stop" },
|
|
1261
|
+
} as Workflow;
|
|
1262
|
+
|
|
1263
|
+
// The design row is gone — only the attempt row survives.
|
|
1264
|
+
writeRunStages([verifyAttemptRow("build", 0, 1, fakeOutput([a0]))]);
|
|
1265
|
+
|
|
1266
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
1267
|
+
expect(result.ok).toBe(true);
|
|
1268
|
+
if (!result.ok) return;
|
|
1269
|
+
expect(result.trailing?.entryArgs).toBeUndefined();
|
|
1270
|
+
});
|
|
1271
|
+
});
|
|
1272
|
+
|
|
1273
|
+
// ---------------------------------------------------------------------------
|
|
1274
|
+
// lastChainIndex (C16) — the fold reconstructs the chain index instead of
|
|
1275
|
+
// reusing the allocator's stageNumber (which counts every loop-unit row, so
|
|
1276
|
+
// the two diverge past any loop: a 10-unit loop made status show "stage 14/5").
|
|
1277
|
+
// ---------------------------------------------------------------------------
|
|
1278
|
+
|
|
1279
|
+
describe("reconstructState — lastChainIndex", () => {
|
|
1280
|
+
it("linear trail: one activation per row", async () => {
|
|
1281
|
+
writeRunStages([
|
|
1282
|
+
{
|
|
1283
|
+
session: null,
|
|
1284
|
+
stageNumber: 1,
|
|
1285
|
+
stage: "plan",
|
|
1286
|
+
skill: "plan",
|
|
1287
|
+
status: "completed",
|
|
1288
|
+
ts: "t1",
|
|
1289
|
+
output: fakeOutput(),
|
|
1290
|
+
},
|
|
1291
|
+
{ session: null, stageNumber: 2, stage: "build", skill: "build", status: "failed", ts: "t2", errMsg: "boom" },
|
|
1292
|
+
]);
|
|
1293
|
+
|
|
1294
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
1295
|
+
expect(result.ok).toBe(true);
|
|
1296
|
+
if (!result.ok) return;
|
|
1297
|
+
expect(result.lastChainIndex).toBe(1);
|
|
1298
|
+
});
|
|
1299
|
+
|
|
1300
|
+
it("a loop generation counts as ONE activation, however many unit rows it wrote", async () => {
|
|
1301
|
+
const units: FanoutFn = () => [
|
|
1302
|
+
{ prompt: "u1", label: "phase 1/3", id: "phase-1" },
|
|
1303
|
+
{ prompt: "u2", label: "phase 2/3", id: "phase-2" },
|
|
1304
|
+
{ prompt: "u3", label: "phase 3/3", id: "phase-3" },
|
|
1305
|
+
];
|
|
1306
|
+
const wf: Workflow = {
|
|
1307
|
+
name: "test-wf",
|
|
1308
|
+
start: "plan",
|
|
1309
|
+
stages: {
|
|
1310
|
+
plan: produces({ outcome: makeOutcome("plans") }),
|
|
1311
|
+
build: produces({ outcome: makeOutcome("builds"), loop: fanout({ units }) }),
|
|
1312
|
+
deploy: produces({ outcome: makeOutcome("deploys") }),
|
|
1313
|
+
},
|
|
1314
|
+
edges: { plan: "build", build: "deploy", deploy: "stop" },
|
|
1315
|
+
} as Workflow;
|
|
1316
|
+
|
|
1317
|
+
writeRunStages([
|
|
1318
|
+
{
|
|
1319
|
+
session: null,
|
|
1320
|
+
stageNumber: 1,
|
|
1321
|
+
stage: "plan",
|
|
1322
|
+
skill: "plan",
|
|
1323
|
+
status: "completed",
|
|
1324
|
+
ts: "t1",
|
|
1325
|
+
output: fakeOutput(),
|
|
1326
|
+
},
|
|
1327
|
+
fanoutUnitRow("build", "phase-1", 0, 2, fakeOutput()),
|
|
1328
|
+
fanoutUnitRow("build", "phase-2", 1, 3, fakeOutput()),
|
|
1329
|
+
fanoutUnitRow("build", "phase-3", 2, 4, fakeOutput()),
|
|
1330
|
+
{
|
|
1331
|
+
session: null,
|
|
1332
|
+
stageNumber: 5,
|
|
1333
|
+
stage: "deploy",
|
|
1334
|
+
skill: "deploy",
|
|
1335
|
+
status: "failed",
|
|
1336
|
+
ts: "t5",
|
|
1337
|
+
errMsg: "boom",
|
|
1338
|
+
},
|
|
1339
|
+
]);
|
|
1340
|
+
|
|
1341
|
+
const result = await reconstructState(tmpDir, wf, baseHeader);
|
|
1342
|
+
expect(result.ok).toBe(true);
|
|
1343
|
+
if (!result.ok) return;
|
|
1344
|
+
// plan(0) → build generation(1) → deploy(2). Pre-fix this was
|
|
1345
|
+
// `last.stageNumber - 1` = 4.
|
|
1346
|
+
expect(result.lastChainIndex).toBe(2);
|
|
1347
|
+
});
|
|
1348
|
+
|
|
1349
|
+
it("a resume re-run of a failed stage keeps its activation index", async () => {
|
|
1350
|
+
writeRunStages([
|
|
1351
|
+
{
|
|
1352
|
+
session: null,
|
|
1353
|
+
stageNumber: 1,
|
|
1354
|
+
stage: "plan",
|
|
1355
|
+
skill: "plan",
|
|
1356
|
+
status: "completed",
|
|
1357
|
+
ts: "t1",
|
|
1358
|
+
output: fakeOutput(),
|
|
1359
|
+
},
|
|
1360
|
+
{ session: null, stageNumber: 2, stage: "build", skill: "build", status: "failed", ts: "t2", errMsg: "boom" },
|
|
1361
|
+
// resumed: build re-ran (same activation), then deploy failed
|
|
1362
|
+
{
|
|
1363
|
+
session: null,
|
|
1364
|
+
stageNumber: 3,
|
|
1365
|
+
stage: "build",
|
|
1366
|
+
skill: "build",
|
|
1367
|
+
status: "completed",
|
|
1368
|
+
ts: "t3",
|
|
1369
|
+
output: fakeOutput(),
|
|
1370
|
+
},
|
|
1371
|
+
{
|
|
1372
|
+
session: null,
|
|
1373
|
+
stageNumber: 4,
|
|
1374
|
+
stage: "deploy",
|
|
1375
|
+
skill: "deploy",
|
|
1376
|
+
status: "failed",
|
|
1377
|
+
ts: "t4",
|
|
1378
|
+
errMsg: "boom",
|
|
1379
|
+
},
|
|
1380
|
+
]);
|
|
1381
|
+
|
|
1382
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
1383
|
+
expect(result.ok).toBe(true);
|
|
1384
|
+
if (!result.ok) return;
|
|
1385
|
+
// plan(0) → build(1, re-run folds into the same activation) → deploy(2).
|
|
1386
|
+
expect(result.lastChainIndex).toBe(2);
|
|
1387
|
+
});
|
|
1388
|
+
|
|
1389
|
+
it("a backward-jump revisit of a COMPLETED stage is a new activation", async () => {
|
|
1390
|
+
writeRunStages([
|
|
1391
|
+
{
|
|
1392
|
+
session: null,
|
|
1393
|
+
stageNumber: 1,
|
|
1394
|
+
stage: "plan",
|
|
1395
|
+
skill: "plan",
|
|
1396
|
+
status: "completed",
|
|
1397
|
+
ts: "t1",
|
|
1398
|
+
output: fakeOutput(),
|
|
1399
|
+
},
|
|
1400
|
+
{
|
|
1401
|
+
session: null,
|
|
1402
|
+
stageNumber: 2,
|
|
1403
|
+
stage: "build",
|
|
1404
|
+
skill: "build",
|
|
1405
|
+
status: "completed",
|
|
1406
|
+
ts: "t2",
|
|
1407
|
+
output: fakeOutput(),
|
|
1408
|
+
},
|
|
1409
|
+
// decision edge routed back to plan (retry loop) — distinct activation
|
|
1410
|
+
{
|
|
1411
|
+
session: null,
|
|
1412
|
+
stageNumber: 3,
|
|
1413
|
+
stage: "plan",
|
|
1414
|
+
skill: "plan",
|
|
1415
|
+
status: "completed",
|
|
1416
|
+
ts: "t3",
|
|
1417
|
+
output: fakeOutput(),
|
|
1418
|
+
},
|
|
1419
|
+
]);
|
|
1420
|
+
|
|
1421
|
+
const result = await reconstructState(tmpDir, linearWorkflow, baseHeader);
|
|
1422
|
+
expect(result.ok).toBe(true);
|
|
1423
|
+
if (!result.ok) return;
|
|
1424
|
+
expect(result.lastChainIndex).toBe(2);
|
|
1425
|
+
});
|
|
1426
|
+
});
|
|
1427
|
+
|
|
764
1428
|
const resumeHeader: WorkflowHeader = {
|
|
765
1429
|
runId: "2026-06-03_07-30-00-ab12",
|
|
766
1430
|
workflow: "resume-wf",
|
|
@@ -798,6 +1462,7 @@ describe("resumeWorkflow", () => {
|
|
|
798
1462
|
|
|
799
1463
|
writeRun(resumeHeader, [
|
|
800
1464
|
{
|
|
1465
|
+
session: null,
|
|
801
1466
|
stageNumber: 1,
|
|
802
1467
|
stage: "plan",
|
|
803
1468
|
skill: "plan",
|
|
@@ -806,6 +1471,7 @@ describe("resumeWorkflow", () => {
|
|
|
806
1471
|
output: out1,
|
|
807
1472
|
},
|
|
808
1473
|
{
|
|
1474
|
+
session: null,
|
|
809
1475
|
stageNumber: 2,
|
|
810
1476
|
stage: "build",
|
|
811
1477
|
skill: "build",
|
|
@@ -855,6 +1521,7 @@ describe("resumeWorkflow", () => {
|
|
|
855
1521
|
|
|
856
1522
|
writeRun(resumeHeader, [
|
|
857
1523
|
{
|
|
1524
|
+
session: null,
|
|
858
1525
|
stageNumber: 1,
|
|
859
1526
|
stage: "plan",
|
|
860
1527
|
skill: "plan",
|
|
@@ -897,6 +1564,7 @@ describe("resumeWorkflow", () => {
|
|
|
897
1564
|
|
|
898
1565
|
writeRun(resumeHeader, [
|
|
899
1566
|
{
|
|
1567
|
+
session: null,
|
|
900
1568
|
stageNumber: 1,
|
|
901
1569
|
stage: "plan",
|
|
902
1570
|
skill: "plan",
|
|
@@ -905,6 +1573,7 @@ describe("resumeWorkflow", () => {
|
|
|
905
1573
|
output: out1,
|
|
906
1574
|
},
|
|
907
1575
|
{
|
|
1576
|
+
session: null,
|
|
908
1577
|
stageNumber: 2,
|
|
909
1578
|
stage: "build",
|
|
910
1579
|
skill: "build",
|
|
@@ -966,6 +1635,7 @@ describe("resumeWorkflow", () => {
|
|
|
966
1635
|
it("stage-gone refusal: returns error envelope, no self-notify", async () => {
|
|
967
1636
|
writeRun(resumeHeader, [
|
|
968
1637
|
{
|
|
1638
|
+
session: null,
|
|
969
1639
|
stageNumber: 1,
|
|
970
1640
|
stage: "plan",
|
|
971
1641
|
skill: "plan",
|
|
@@ -973,6 +1643,7 @@ describe("resumeWorkflow", () => {
|
|
|
973
1643
|
ts: "2026-06-03T07:31:00Z",
|
|
974
1644
|
},
|
|
975
1645
|
{
|
|
1646
|
+
session: null,
|
|
976
1647
|
stageNumber: 2,
|
|
977
1648
|
stage: "removed-stage",
|
|
978
1649
|
skill: "removed-stage",
|
|
@@ -1006,6 +1677,7 @@ describe("resumeWorkflow", () => {
|
|
|
1006
1677
|
|
|
1007
1678
|
writeRun(resumeHeader, [
|
|
1008
1679
|
{
|
|
1680
|
+
session: null,
|
|
1009
1681
|
stageNumber: 1,
|
|
1010
1682
|
stage: "plan",
|
|
1011
1683
|
skill: "plan",
|
|
@@ -1047,6 +1719,7 @@ describe("resumeWorkflow", () => {
|
|
|
1047
1719
|
|
|
1048
1720
|
writeRun(resumeHeader, [
|
|
1049
1721
|
{
|
|
1722
|
+
session: null,
|
|
1050
1723
|
stageNumber: 1,
|
|
1051
1724
|
stage: "plan",
|
|
1052
1725
|
skill: "plan",
|
|
@@ -1055,6 +1728,7 @@ describe("resumeWorkflow", () => {
|
|
|
1055
1728
|
output: out1,
|
|
1056
1729
|
},
|
|
1057
1730
|
{
|
|
1731
|
+
session: null,
|
|
1058
1732
|
stageNumber: 2,
|
|
1059
1733
|
stage: "build",
|
|
1060
1734
|
skill: "build",
|
|
@@ -1090,6 +1764,7 @@ describe("resumeWorkflow", () => {
|
|
|
1090
1764
|
|
|
1091
1765
|
writeRun(resumeHeader, [
|
|
1092
1766
|
{
|
|
1767
|
+
session: null,
|
|
1093
1768
|
stageNumber: 1,
|
|
1094
1769
|
stage: "plan",
|
|
1095
1770
|
skill: "plan",
|
|
@@ -1126,5 +1801,5 @@ describe("resumeWorkflow", () => {
|
|
|
1126
1801
|
});
|
|
1127
1802
|
});
|
|
1128
1803
|
|
|
1129
|
-
// Mid-
|
|
1804
|
+
// Mid-loop resume dispatch (fanout/iterate/assess) is covered end-to-end in `resume-loop.test.ts`.
|
|
1130
1805
|
});
|