@juicesharp/rpiv-workflow 2.2.0 → 2.3.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/audit-ctx.ts +7 -7
- package/audit.ts +29 -29
- package/chain-state.ts +14 -3
- package/death-scene.ts +9 -9
- package/docs/workflow-authoring.md +2 -2
- package/events.ts +1 -1
- package/failure-memos.ts +4 -4
- package/handle.ts +16 -1
- package/host.ts +1 -1
- package/loop-def.ts +1 -1
- package/loop-kinds.ts +12 -12
- package/loop-parallel.ts +15 -15
- package/loop-waves.ts +3 -3
- package/loop.ts +32 -30
- package/messages.ts +30 -17
- package/outcomes/collectors/workspace-diff.ts +3 -3
- package/outcomes/git-commit.ts +11 -4
- package/output-spec.ts +7 -7
- package/package.json +2 -2
- package/preview.ts +8 -1
- package/registration.ts +3 -3
- package/routing.ts +1 -1
- package/runner/chain-advance.ts +49 -25
- package/runner/errors.ts +1 -1
- package/runner/failure.ts +22 -23
- package/runner/preflight.ts +3 -6
- package/runner/resolve-stage.ts +3 -3
- package/runner/resume-entry.ts +5 -5
- package/runner/resume-loop.ts +5 -5
- package/runner/resume.ts +1 -1
- package/runner/run-stage.ts +69 -64
- package/runner/runner.ts +4 -4
- package/runner/script-stage.ts +17 -17
- package/sessions/bash-strikes.ts +9 -9
- package/sessions/extraction.ts +19 -12
- package/sessions/halt-routing.ts +14 -14
- package/sessions/index.ts +1 -1
- package/sessions/reattach.ts +10 -10
- package/sessions/sessions.ts +29 -23
- package/sessions/spawn.ts +5 -5
- package/sessions/success-persist.ts +3 -3
- package/stage-def.ts +2 -2
- package/stage-errors.ts +1 -1
- package/state/index.ts +1 -1
- package/state/names.ts +1 -1
- package/state/reads.ts +1 -1
- package/state/state.ts +4 -4
- package/state/writes.ts +2 -2
- package/types.ts +16 -16
package/runner/run-stage.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Per-stage execution pipeline + the chain walk's COMPOSITION SITE.
|
|
3
3
|
*
|
|
4
|
-
* `
|
|
4
|
+
* `dispatchStage` resolves the stage once (`resolveStage` — mode + dispatch
|
|
5
5
|
* derived in one place) and switches on `mode`:
|
|
6
6
|
* - `"loop"` — the unit-loop driver (loop.ts), one session per unit;
|
|
7
7
|
* - `"script"` — `def.run` called directly (script-stage.ts);
|
|
8
8
|
* - `"prompt"`/`"skill"` — preflights → prompt prep → input validation →
|
|
9
9
|
* snapshot → one Pi session.
|
|
10
10
|
*
|
|
11
|
-
* The chain walk is mutually recursive (
|
|
12
|
-
* advanceChain → next
|
|
11
|
+
* The chain walk is mutually recursive (dispatchStage → session continuation →
|
|
12
|
+
* advanceChain → next dispatchStage); the recursion is composed HERE via
|
|
13
13
|
* injection — `advanceChain` receives `ChainDeps.runNext`, the loop driver
|
|
14
14
|
* receives `LoopDeps`, the script stage receives `advance` — so every other
|
|
15
15
|
* engine module's imports point strictly downward (zero value-import
|
|
16
16
|
* cycles; the LoopDeps precedent applied to the whole walk).
|
|
17
17
|
*
|
|
18
|
-
* `
|
|
18
|
+
* `dispatchStageOrRecordFailure` is the walk's single catch site: a throw from
|
|
19
19
|
* anywhere in the pipeline (preflights, user fns, machinery) lands a uniform
|
|
20
20
|
* JSONL failure row via failure.ts.
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
23
|
import type { StageDef, Unit } from "../api.js";
|
|
24
|
-
import { auditCtxFor, failedArgs, notifyPartialArtifacts,
|
|
24
|
+
import { auditCtxFor, failedArgs, notifyPartialArtifacts, recordFatalFailure, runIdentityOf } from "../audit.js";
|
|
25
25
|
import { currentPrimaryArtifact, resolveStagePrompt, stageEntryArgs } from "../chain-state.js";
|
|
26
26
|
import { lifecycleCtxFor, skillStageRef } from "../events.js";
|
|
27
27
|
import { failureMemoSuffix } from "../failure-memos.js";
|
|
@@ -29,7 +29,7 @@ import { formatError } from "../internal-utils.js";
|
|
|
29
29
|
import { announceLoopStart, runLoop } from "../loop.js";
|
|
30
30
|
import { freezesEntryArgsOf } from "../loop-constructors.js";
|
|
31
31
|
import { buildLoopEntry, freshCursor, type LoopDeps, type LoopEntry } from "../loop-kinds.js";
|
|
32
|
-
import {
|
|
32
|
+
import { ensureUnitDeps } from "../loop-waves.js";
|
|
33
33
|
import {
|
|
34
34
|
FAIL_LOOP_CAP_HALT,
|
|
35
35
|
FAIL_VALIDATE_GATE_SKIPPED,
|
|
@@ -38,11 +38,16 @@ import {
|
|
|
38
38
|
MSG_RESUME_SESSION_FALLBACK,
|
|
39
39
|
MSG_SNAPSHOT_FAILED,
|
|
40
40
|
} from "../messages.js";
|
|
41
|
-
import {
|
|
41
|
+
import {
|
|
42
|
+
continueStageSession,
|
|
43
|
+
executeStageSession,
|
|
44
|
+
locateSessionFile,
|
|
45
|
+
reattachStageSession,
|
|
46
|
+
} from "../sessions/index.js";
|
|
42
47
|
import { forkChildSession, reattachChildSession } from "../sessions/spawn.js";
|
|
43
48
|
import { effectiveOutputSchemaOf } from "../stage-identity.js";
|
|
44
49
|
import type { WorkflowStage } from "../state/index.js";
|
|
45
|
-
import type { RunContext,
|
|
50
|
+
import type { RunContext, StageSessionContext, WorkflowHostContext } from "../types.js";
|
|
46
51
|
import { resolveDigest } from "../worktree-digest.js";
|
|
47
52
|
import { advanceChain, type ChainDeps } from "./chain-advance.js";
|
|
48
53
|
import { type ChainOutcome, haltChain, recordAbortedAtSeam, recordEntryThrow, withStageEntryGuard } from "./failure.js";
|
|
@@ -62,7 +67,7 @@ export type { ResolvedStage } from "./resolve-stage.js";
|
|
|
62
67
|
// ---------------------------------------------------------------------------
|
|
63
68
|
|
|
64
69
|
const CHAIN_DEPS: ChainDeps = {
|
|
65
|
-
runNext: (
|
|
70
|
+
runNext: (hostCtx, name, idx, run) => dispatchStageOrRecordFailure(hostCtx, name, idx, run),
|
|
66
71
|
};
|
|
67
72
|
|
|
68
73
|
/**
|
|
@@ -71,16 +76,16 @@ const CHAIN_DEPS: ChainDeps = {
|
|
|
71
76
|
* driver advance, script stage, resume route-onward).
|
|
72
77
|
*/
|
|
73
78
|
export function advance(
|
|
74
|
-
|
|
79
|
+
hostCtx: WorkflowHostContext,
|
|
75
80
|
completedName: string,
|
|
76
81
|
completedIdx: number,
|
|
77
82
|
run: RunContext,
|
|
78
83
|
): Promise<ChainOutcome> {
|
|
79
|
-
return advanceChain(
|
|
84
|
+
return advanceChain(hostCtx, completedName, completedIdx, run, CHAIN_DEPS);
|
|
80
85
|
}
|
|
81
86
|
|
|
82
87
|
/**
|
|
83
|
-
* Wraps `
|
|
88
|
+
* Wraps `dispatchStage` so a thrown stage records a JSONL failure row attributed
|
|
84
89
|
* to the stage that actually threw — not to the prior stage in the chain.
|
|
85
90
|
* Used by `runWorkflow` (start stage), `advanceChain` (next stage, via
|
|
86
91
|
* `ChainDeps`), and the resume entries — the walk's single catch site.
|
|
@@ -88,13 +93,13 @@ export function advance(
|
|
|
88
93
|
* mid-stage `WorkflowAbortError` vs terminal entry-throw) to
|
|
89
94
|
* `withStageEntryGuard` (failure.ts).
|
|
90
95
|
*/
|
|
91
|
-
export async function
|
|
92
|
-
|
|
96
|
+
export async function dispatchStageOrRecordFailure(
|
|
97
|
+
hostCtx: WorkflowHostContext,
|
|
93
98
|
name: string,
|
|
94
99
|
idx: number,
|
|
95
100
|
run: RunContext,
|
|
96
101
|
): Promise<ChainOutcome> {
|
|
97
|
-
return withStageEntryGuard(
|
|
102
|
+
return withStageEntryGuard(hostCtx, name, run, () => dispatchStage(hostCtx, name, idx, run));
|
|
98
103
|
}
|
|
99
104
|
|
|
100
105
|
// ---------------------------------------------------------------------------
|
|
@@ -122,8 +127,8 @@ export function inputForStage(stage: ResolvedStage, run: RunContext): string {
|
|
|
122
127
|
}
|
|
123
128
|
|
|
124
129
|
/** One stage activation — dispatch on the mode derived once by `resolveStage`. */
|
|
125
|
-
export async function
|
|
126
|
-
|
|
130
|
+
export async function dispatchStage(
|
|
131
|
+
hostCtx: WorkflowHostContext,
|
|
127
132
|
currentName: string,
|
|
128
133
|
idx: number,
|
|
129
134
|
run: RunContext,
|
|
@@ -131,17 +136,17 @@ export async function runStage(
|
|
|
131
136
|
const stage = resolveStage(currentName, idx, run);
|
|
132
137
|
switch (stage.mode) {
|
|
133
138
|
case "loop":
|
|
134
|
-
return runLoopStage(
|
|
139
|
+
return runLoopStage(hostCtx, stage, idx, run);
|
|
135
140
|
case "script":
|
|
136
141
|
// Script stages skip the skill pipeline — no `/skill:` prompt, no
|
|
137
142
|
// registry check, no session, no collector snapshot. Input-schema
|
|
138
143
|
// validation still applies; the script runner owns its own status
|
|
139
144
|
// line + lifecycle fires.
|
|
140
145
|
await ensureInputValid(stage, run);
|
|
141
|
-
return runScript(
|
|
146
|
+
return runScript(hostCtx, stage, idx, run, advance);
|
|
142
147
|
case "prompt":
|
|
143
148
|
case "skill":
|
|
144
|
-
return runSingleStage(
|
|
149
|
+
return runSingleStage(hostCtx, stage, idx, run);
|
|
145
150
|
}
|
|
146
151
|
}
|
|
147
152
|
|
|
@@ -154,7 +159,7 @@ export async function runStage(
|
|
|
154
159
|
* branch (`computeBranchOffset`), resume takes it from the persisted row.
|
|
155
160
|
*/
|
|
156
161
|
async function prepareSingleStage(
|
|
157
|
-
|
|
162
|
+
hostCtx: WorkflowHostContext,
|
|
158
163
|
stage: ResolvedStage,
|
|
159
164
|
idx: number,
|
|
160
165
|
run: RunContext,
|
|
@@ -169,12 +174,12 @@ async function prepareSingleStage(
|
|
|
169
174
|
await ensureInputValid(stage, run);
|
|
170
175
|
await ensureContractInputValid(stage, run);
|
|
171
176
|
|
|
172
|
-
const snapshot = await captureStageSnapshot(
|
|
177
|
+
const snapshot = await captureStageSnapshot(hostCtx, stage.name, stage.def, idx, run);
|
|
173
178
|
return { prompt, snapshot };
|
|
174
179
|
}
|
|
175
180
|
|
|
176
181
|
/**
|
|
177
|
-
* The `
|
|
182
|
+
* The `StageSessionContext` both single-stage entries build — live and resume use
|
|
178
183
|
* the SAME continuation pair (`onSuccess` → `advance`, `onFailure` →
|
|
179
184
|
* partial-artifact recap), so a promoted/reattached stage chains onward
|
|
180
185
|
* exactly like a live one.
|
|
@@ -185,7 +190,7 @@ function buildSingleStageSession(
|
|
|
185
190
|
run: RunContext,
|
|
186
191
|
prep: { prompt: string; snapshot: unknown },
|
|
187
192
|
branchOffset: number | undefined,
|
|
188
|
-
):
|
|
193
|
+
): StageSessionContext {
|
|
189
194
|
return {
|
|
190
195
|
cwd: run.cwd,
|
|
191
196
|
runId: run.runId,
|
|
@@ -217,12 +222,12 @@ function buildSingleStageSession(
|
|
|
217
222
|
* (loop.ts) — the loop path's one-helper-for-live-and-resume exemplar.
|
|
218
223
|
*/
|
|
219
224
|
function announceSingleStageStart(
|
|
220
|
-
|
|
225
|
+
hostCtx: WorkflowHostContext,
|
|
221
226
|
run: RunContext,
|
|
222
227
|
stage: Pick<ResolvedStage, "name" | "stageNumber" | "skill">,
|
|
223
228
|
): Promise<void> {
|
|
224
229
|
return run.lifecycle.fire(
|
|
225
|
-
|
|
230
|
+
hostCtx,
|
|
226
231
|
"onStageStart",
|
|
227
232
|
skillStageRef(stage.name, stage.stageNumber, stage.skill),
|
|
228
233
|
lifecycleCtxFor(run),
|
|
@@ -239,7 +244,7 @@ function announceSingleStageStart(
|
|
|
239
244
|
* Operator resume is excluded: session-backed resume never reaches
|
|
240
245
|
* `runSingleStage`, and a cold re-run under `trigger.meta.resumedFrom` is
|
|
241
246
|
* bypassed by `isOperatorResume`. The terminal skip carries the failure memo
|
|
242
|
-
* for free through the shared `
|
|
247
|
+
* for free through the shared `recordFatalFailure` writer
|
|
243
248
|
* (`packages/rpiv-workflow/audit.ts:122`). An `undefined` digest (non-repo / git
|
|
244
249
|
* missing) ALWAYS proceeds — degrade on a missing signal, never skip.
|
|
245
250
|
*
|
|
@@ -251,7 +256,7 @@ function announceSingleStageStart(
|
|
|
251
256
|
* drift on the precedence order or the contract key.
|
|
252
257
|
*/
|
|
253
258
|
export async function gateValidationRedispatch(
|
|
254
|
-
|
|
259
|
+
hostCtx: WorkflowHostContext,
|
|
255
260
|
stage: ResolvedStage,
|
|
256
261
|
run: RunContext,
|
|
257
262
|
): Promise<boolean> {
|
|
@@ -268,8 +273,8 @@ export async function gateValidationRedispatch(
|
|
|
268
273
|
digest === baseline.digest &&
|
|
269
274
|
run.state.stagesCompleted === baseline.stagesCompleted
|
|
270
275
|
) {
|
|
271
|
-
await
|
|
272
|
-
|
|
276
|
+
await recordFatalFailure(
|
|
277
|
+
hostCtx,
|
|
273
278
|
auditCtxFor(run, stage.name, stage.skill),
|
|
274
279
|
failedArgs(FAIL_VALIDATE_GATE_SKIPPED(stage.skill)),
|
|
275
280
|
);
|
|
@@ -297,10 +302,10 @@ export async function gateValidationRedispatch(
|
|
|
297
302
|
* prompt stage (it cannot set an explicit skill — load validation forbids
|
|
298
303
|
* it), so the status/session/audit labels are correct for both without a
|
|
299
304
|
* separate label. A PromptFn throw propagates to
|
|
300
|
-
* `
|
|
305
|
+
* `dispatchStageOrRecordFailure`, which records a terminal failure.
|
|
301
306
|
*/
|
|
302
307
|
async function runSingleStage(
|
|
303
|
-
|
|
308
|
+
hostCtx: WorkflowHostContext,
|
|
304
309
|
stage: ResolvedStage,
|
|
305
310
|
idx: number,
|
|
306
311
|
run: RunContext,
|
|
@@ -309,12 +314,12 @@ async function runSingleStage(
|
|
|
309
314
|
// fail-fast an unchanged-tree re-dispatch of a schema-validated produces
|
|
310
315
|
// stage BEFORE preflights or the session open. `true` ⇒ already recorded a
|
|
311
316
|
// terminal failure row; halt here.
|
|
312
|
-
if (await gateValidationRedispatch(
|
|
317
|
+
if (await gateValidationRedispatch(hostCtx, stage, run)) return "halted";
|
|
313
318
|
|
|
314
|
-
const prep = await prepareSingleStage(
|
|
319
|
+
const prep = await prepareSingleStage(hostCtx, stage, idx, run);
|
|
315
320
|
|
|
316
321
|
// onStageStart fires after preflight, before the Pi session opens.
|
|
317
|
-
await announceSingleStageStart(
|
|
322
|
+
await announceSingleStageStart(hostCtx, run, stage);
|
|
318
323
|
|
|
319
324
|
// `continue` forks the predecessor's persisted session (`run.state.lastSession`)
|
|
320
325
|
// into a fresh child carrying its transcript; `continueStageSession` re-derives
|
|
@@ -326,13 +331,13 @@ async function runSingleStage(
|
|
|
326
331
|
const file = continueForkFile(run);
|
|
327
332
|
if (file) {
|
|
328
333
|
const s = buildSingleStageSession(stage, idx, run, prep, undefined);
|
|
329
|
-
await forkChildSession(
|
|
334
|
+
await forkChildSession(hostCtx, s, file, (child) => continueStageSession(hostCtx, child, s));
|
|
330
335
|
return "dispatched";
|
|
331
336
|
}
|
|
332
|
-
|
|
337
|
+
hostCtx.ui.notify(MSG_CONTINUE_FALLBACK(stage.skill), "info");
|
|
333
338
|
}
|
|
334
339
|
|
|
335
|
-
await
|
|
340
|
+
await executeStageSession(hostCtx, buildSingleStageSession(stage, idx, run, prep, undefined));
|
|
336
341
|
return "dispatched";
|
|
337
342
|
}
|
|
338
343
|
|
|
@@ -352,26 +357,26 @@ function continueForkFile(run: RunContext): string | null {
|
|
|
352
357
|
* (reattach), instead of re-running cold. Selected by `selectResumeEntry`
|
|
353
358
|
* when the failed trailer carries a `session` (the structured dispatch).
|
|
354
359
|
* Delegates the same classify-then-record policy as the live entry
|
|
355
|
-
* (`
|
|
360
|
+
* (`dispatchStageOrRecordFailure`) to `withStageEntryGuard` (failure.ts); the
|
|
356
361
|
* fallback ladder itself lives in `resumeWithSessionLadder` below — the
|
|
357
362
|
* resume `inner` still wraps it, so a reattached `postStage`
|
|
358
363
|
* `WorkflowAbortError` or machinery throw is classified (abort vs terminal
|
|
359
364
|
* entry-throw) by the same authority as the live path.
|
|
360
365
|
*/
|
|
361
366
|
export async function resumeStageWithSession(
|
|
362
|
-
|
|
367
|
+
hostCtx: WorkflowHostContext,
|
|
363
368
|
last: WorkflowStage,
|
|
364
369
|
idx: number,
|
|
365
370
|
run: RunContext,
|
|
366
371
|
): Promise<ChainOutcome> {
|
|
367
|
-
return withStageEntryGuard(
|
|
372
|
+
return withStageEntryGuard(hostCtx, last.stage, run, () => resumeWithSessionLadder(hostCtx, last, idx, run));
|
|
368
373
|
}
|
|
369
374
|
|
|
370
375
|
/**
|
|
371
376
|
* THE resume fallback ladder (called by `resumeStageWithSession` via
|
|
372
377
|
* `withStageEntryGuard`). Every precondition miss notifies
|
|
373
378
|
* (`MSG_RESUME_SESSION_FALLBACK`) and degrades to today's cold re-run via
|
|
374
|
-
* `
|
|
379
|
+
* `dispatchStageOrRecordFailure` — never a refusal, never a throw:
|
|
375
380
|
*
|
|
376
381
|
* 1. resolved mode must be `prompt`/`skill` (loop trailers never reach
|
|
377
382
|
* this arm — they carry `parent`; script stages are sessionless);
|
|
@@ -381,7 +386,7 @@ export async function resumeStageWithSession(
|
|
|
381
386
|
* 4. `reattachChildSession` spawns a child BOUND to the persisted file (the
|
|
382
387
|
* host opens it, does NOT replay the prompt) and `reattachStageSession`
|
|
383
388
|
* (sessions/reattach.ts) runs promotion → reattach inside it, with the
|
|
384
|
-
* SAME `
|
|
389
|
+
* SAME `StageSessionContext` the live path builds — `branchOffset` taken from
|
|
385
390
|
* the PERSISTED row (continue-policy stages), `undefined` for fresh.
|
|
386
391
|
*
|
|
387
392
|
* (The cooperative-abort pre-check — formerly step 1 here — now lives in
|
|
@@ -395,7 +400,7 @@ export async function resumeStageWithSession(
|
|
|
395
400
|
* live-session swap for the user to dismiss.
|
|
396
401
|
*/
|
|
397
402
|
async function resumeWithSessionLadder(
|
|
398
|
-
|
|
403
|
+
hostCtx: WorkflowHostContext,
|
|
399
404
|
last: WorkflowStage,
|
|
400
405
|
idx: number,
|
|
401
406
|
run: RunContext,
|
|
@@ -404,29 +409,29 @@ async function resumeWithSessionLadder(
|
|
|
404
409
|
const stage = resolveStage(last.stage, idx, run);
|
|
405
410
|
|
|
406
411
|
const fallBackCold = (why: string): Promise<ChainOutcome> => {
|
|
407
|
-
|
|
408
|
-
return
|
|
412
|
+
hostCtx.ui.notify(MSG_RESUME_SESSION_FALLBACK(stage.skill, why), "info");
|
|
413
|
+
return dispatchStageOrRecordFailure(hostCtx, last.stage, idx, run);
|
|
409
414
|
};
|
|
410
415
|
|
|
411
416
|
// Defensive: a session-backed row for a stage whose def since became a
|
|
412
417
|
// loop/script stage — the session machinery below doesn't apply.
|
|
413
418
|
if (stage.mode !== "prompt" && stage.mode !== "skill") {
|
|
414
|
-
return
|
|
419
|
+
return dispatchStageOrRecordFailure(hostCtx, last.stage, idx, run);
|
|
415
420
|
}
|
|
416
421
|
const file = locateSessionFile(ref, run.runId, run.cwd);
|
|
417
422
|
if (!file) return fallBackCold("session file not found");
|
|
418
423
|
|
|
419
|
-
const prep = await prepareSingleStage(
|
|
424
|
+
const prep = await prepareSingleStage(hostCtx, stage, idx, run);
|
|
420
425
|
const s = buildSingleStageSession(stage, idx, run, prep, ref.branchOffset);
|
|
421
426
|
|
|
422
427
|
// Same bracketing as live: onStageStart before the (re)attached child opens.
|
|
423
|
-
await announceSingleStageStart(
|
|
428
|
+
await announceSingleStageStart(hostCtx, run, stage);
|
|
424
429
|
|
|
425
430
|
// Detached reattach: spawn a child BOUND to the persisted session file (the
|
|
426
431
|
// host opens it and does NOT replay the prompt); reattachStageSession promotes
|
|
427
432
|
// from the loaded branch or nudges via resendIntoChild. Replaces the deleted
|
|
428
|
-
// live-session swap (`
|
|
429
|
-
await reattachChildSession(
|
|
433
|
+
// live-session swap (`hostCtx.switchSession`).
|
|
434
|
+
await reattachChildSession(hostCtx, s, file, (child) => reattachStageSession(hostCtx, child, s));
|
|
430
435
|
return "dispatched";
|
|
431
436
|
}
|
|
432
437
|
|
|
@@ -455,7 +460,7 @@ async function resumeWithSessionLadder(
|
|
|
455
460
|
* message is the stage's own `prompt`, resolved by the driver at dispatch.
|
|
456
461
|
*/
|
|
457
462
|
async function runLoopStage(
|
|
458
|
-
|
|
463
|
+
hostCtx: WorkflowHostContext,
|
|
459
464
|
stage: ResolvedStage,
|
|
460
465
|
idx: number,
|
|
461
466
|
run: RunContext,
|
|
@@ -468,10 +473,10 @@ async function runLoopStage(
|
|
|
468
473
|
let units: readonly Unit[] | undefined;
|
|
469
474
|
if (loop.kind === "fanout") {
|
|
470
475
|
units = await loop.units({ cwd: run.cwd, artifact: currentPrimaryArtifact(run.state), state: run.state });
|
|
471
|
-
if (units.length === 0) return runSingleStage(
|
|
476
|
+
if (units.length === 0) return runSingleStage(hostCtx, stage, idx, run);
|
|
472
477
|
// deps cycle / dangling-id → clean halt BEFORE any dispatch (the runtime unit
|
|
473
478
|
// list is invisible to the static load gate, so this is the only place it runs).
|
|
474
|
-
|
|
479
|
+
ensureUnitDeps(units, stage.name);
|
|
475
480
|
}
|
|
476
481
|
|
|
477
482
|
runLoopPreflights(stage, run);
|
|
@@ -486,8 +491,8 @@ async function runLoopStage(
|
|
|
486
491
|
},
|
|
487
492
|
);
|
|
488
493
|
|
|
489
|
-
await announceLoopStart(
|
|
490
|
-
await runLoop(
|
|
494
|
+
await announceLoopStart(hostCtx, run, entry);
|
|
495
|
+
await runLoop(hostCtx, entry, freshCursor(), run, buildLoopDeps());
|
|
491
496
|
return "dispatched";
|
|
492
497
|
}
|
|
493
498
|
|
|
@@ -497,17 +502,17 @@ async function runLoopStage(
|
|
|
497
502
|
*/
|
|
498
503
|
export function buildLoopDeps(): LoopDeps {
|
|
499
504
|
return {
|
|
500
|
-
|
|
505
|
+
executeStageSession,
|
|
501
506
|
advanceAfter: (freshCtx, name, completedIdx, ctx) => advance(freshCtx, name, completedIdx, ctx),
|
|
502
507
|
captureSnapshot: (ctx, name, def, i, r) => captureStageSnapshot(ctx, name, def, i, r),
|
|
503
508
|
haltLoop,
|
|
504
509
|
// Mid-flight run abort at the loop seam → FAIL_WORKFLOW_ABORTED.
|
|
505
|
-
recordAborted: (
|
|
510
|
+
recordAborted: (hostCtx, name, run) => recordAbortedAtSeam(hostCtx, name, run).then(() => undefined),
|
|
506
511
|
// Unexpected worker rejection → terminal-failure row, no re-throw. The
|
|
507
512
|
// unit's identity rides as a STRUCTURED field (`unit`) on the row — `stage`
|
|
508
513
|
// stays the parent graph identity, not a `name (unit N)` string.
|
|
509
|
-
recordWorkerThrow: (
|
|
510
|
-
recordEntryThrow(
|
|
514
|
+
recordWorkerThrow: (hostCtx, unit, skill, run, err) =>
|
|
515
|
+
recordEntryThrow(hostCtx, unit.parent, run, err, { ref: unit, skill }).then(() => undefined),
|
|
511
516
|
};
|
|
512
517
|
}
|
|
513
518
|
|
|
@@ -517,21 +522,21 @@ export function buildLoopDeps(): LoopDeps {
|
|
|
517
522
|
* not a loop, so "loop cap exceeded" would misattribute the failure.
|
|
518
523
|
*/
|
|
519
524
|
export async function haltLoop(
|
|
520
|
-
|
|
525
|
+
hostCtx: WorkflowHostContext,
|
|
521
526
|
run: RunContext,
|
|
522
527
|
e: Pick<LoopEntry, "name" | "def">,
|
|
523
528
|
count: number,
|
|
524
529
|
cap: number,
|
|
525
530
|
): Promise<void> {
|
|
526
531
|
const args = e.def.verify ? failedArgs(FAIL_VERIFY_FAILED(e.name, cap)) : failedArgs(FAIL_LOOP_CAP_HALT(count, cap));
|
|
527
|
-
await haltChain(
|
|
532
|
+
await haltChain(hostCtx, run, e.name, e.name, args);
|
|
528
533
|
}
|
|
529
534
|
|
|
530
535
|
/** Runs whose snapshot-failure warning already fired — one notify per run, not per stage/unit. */
|
|
531
536
|
const snapshotWarnedRuns = new WeakSet<RunContext>();
|
|
532
537
|
|
|
533
538
|
export async function captureStageSnapshot(
|
|
534
|
-
|
|
539
|
+
hostCtx: WorkflowHostContext,
|
|
535
540
|
stageName: string,
|
|
536
541
|
def: StageDef,
|
|
537
542
|
idx: number,
|
|
@@ -552,7 +557,7 @@ export async function captureStageSnapshot(
|
|
|
552
557
|
// diffing for the whole run, so the first failure warns.
|
|
553
558
|
if (!snapshotWarnedRuns.has(run)) {
|
|
554
559
|
snapshotWarnedRuns.add(run);
|
|
555
|
-
|
|
560
|
+
hostCtx.ui.notify(MSG_SNAPSHOT_FAILED(stageName, formatError(e)), "warning");
|
|
556
561
|
}
|
|
557
562
|
return undefined;
|
|
558
563
|
}
|
package/runner/runner.ts
CHANGED
|
@@ -33,6 +33,7 @@ import {
|
|
|
33
33
|
} from "../messages.js";
|
|
34
34
|
import { pruneOrphanedChildSessions } from "../sessions/index.js";
|
|
35
35
|
import {
|
|
36
|
+
appendHeader,
|
|
36
37
|
type ClaimResult,
|
|
37
38
|
claimName,
|
|
38
39
|
generateRunId,
|
|
@@ -40,7 +41,6 @@ import {
|
|
|
40
41
|
releaseName,
|
|
41
42
|
STATE_SCHEMA_VERSION,
|
|
42
43
|
type WorkflowHeader,
|
|
43
|
-
writeHeader,
|
|
44
44
|
} from "../state/index.js";
|
|
45
45
|
import { childSessionsDir } from "../state/paths.js";
|
|
46
46
|
import type { BranchEntry } from "../transcript.js";
|
|
@@ -49,7 +49,7 @@ import type { RunContext, RunWorkflowOptions, RunWorkflowResult } from "../types
|
|
|
49
49
|
import { reconstructState } from "./resume.js";
|
|
50
50
|
import { resumeRefusalError, selectResumeEntry } from "./resume-entry.js";
|
|
51
51
|
import { buildRunContext, freshRunState } from "./run-context.js";
|
|
52
|
-
import {
|
|
52
|
+
import { dispatchStageOrRecordFailure } from "./run-stage.js";
|
|
53
53
|
|
|
54
54
|
// ---------------------------------------------------------------------------
|
|
55
55
|
// Shared tail — executeRun
|
|
@@ -232,7 +232,7 @@ export async function runWorkflow(ctx: WorkflowHostContext, options: RunWorkflow
|
|
|
232
232
|
// makes the run unlistable and unresumable while its stage rows land, so a
|
|
233
233
|
// failed append rejects the start and rolls back the name claim (the index
|
|
234
234
|
// must not point at a run that never existed).
|
|
235
|
-
const headerWritten =
|
|
235
|
+
const headerWritten = appendHeader(cwd, {
|
|
236
236
|
runId,
|
|
237
237
|
workflow: workflow.name,
|
|
238
238
|
input: options.input,
|
|
@@ -269,7 +269,7 @@ export async function runWorkflow(ctx: WorkflowHostContext, options: RunWorkflow
|
|
|
269
269
|
trigger,
|
|
270
270
|
},
|
|
271
271
|
);
|
|
272
|
-
return await executeRun(execCtx, run, () =>
|
|
272
|
+
return await executeRun(execCtx, run, () => dispatchStageOrRecordFailure(execCtx, workflow.start, 0, run));
|
|
273
273
|
} finally {
|
|
274
274
|
dispose?.(); // unsubscribe the onTerminalInput tap — leaks accumulate on the TUI otherwise
|
|
275
275
|
}
|
package/runner/script-stage.ts
CHANGED
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
*/
|
|
30
30
|
|
|
31
31
|
import type { ScriptContext } from "../api.js";
|
|
32
|
-
import { auditCtxFor, failAuditWrite, failedArgs,
|
|
32
|
+
import { auditCtxFor, failAuditWrite, failedArgs, recordFatalFailure } from "../audit.js";
|
|
33
33
|
import { allocateStageNumber, persistStageSuccess } from "../audit-rows.js";
|
|
34
34
|
import { lifecycleCtxFor, scriptStageRef } from "../events.js";
|
|
35
35
|
import type { Artifact } from "../handle.js";
|
|
@@ -49,22 +49,22 @@ import type { ResolvedStage } from "./resolve-stage.js";
|
|
|
49
49
|
/**
|
|
50
50
|
* Drive a script stage: lifecycle-fire `onStageStart`, retry-loop the
|
|
51
51
|
* `run` body against `outputSchema`, then either persist + advance or
|
|
52
|
-
* record a terminal failure. Sole entry point — `
|
|
52
|
+
* record a terminal failure. Sole entry point — `dispatchStage` branches
|
|
53
53
|
* here on `mode === "script"`, passing the composed `advance`.
|
|
54
54
|
*
|
|
55
|
-
* Caller pre-conditions (held by `
|
|
55
|
+
* Caller pre-conditions (held by `dispatchStage`):
|
|
56
56
|
* - `ensureInputValid` already passed.
|
|
57
57
|
* - `mode === "script"` (a script stage cannot carry a `loop`).
|
|
58
58
|
*/
|
|
59
59
|
export async function runScript(
|
|
60
|
-
|
|
60
|
+
hostCtx: WorkflowHostContext,
|
|
61
61
|
stage: ResolvedStage,
|
|
62
62
|
idx: number,
|
|
63
63
|
run: RunContext,
|
|
64
64
|
advance: AdvanceFn,
|
|
65
65
|
): Promise<ChainOutcome> {
|
|
66
66
|
const ref = scriptStageRef(stage.name, stage.stageNumber);
|
|
67
|
-
await run.lifecycle.fire(
|
|
67
|
+
await run.lifecycle.fire(hostCtx, "onStageStart", ref, lifecycleCtxFor(run));
|
|
68
68
|
|
|
69
69
|
const scriptCtx: ScriptContext = {
|
|
70
70
|
cwd: run.cwd,
|
|
@@ -85,7 +85,7 @@ export async function runScript(
|
|
|
85
85
|
},
|
|
86
86
|
{
|
|
87
87
|
produce: async () => {
|
|
88
|
-
const invocation = await invokeRun(
|
|
88
|
+
const invocation = await invokeRun(hostCtx, stage, scriptCtx, run, stageNumber);
|
|
89
89
|
if (!invocation.ok) return { kind: "aborted", abort: "recorded" };
|
|
90
90
|
const output = finalizeOutput(
|
|
91
91
|
invocation.raw,
|
|
@@ -110,7 +110,7 @@ export async function runScript(
|
|
|
110
110
|
};
|
|
111
111
|
},
|
|
112
112
|
onRetry: async (attempt) => {
|
|
113
|
-
await run.lifecycle.fire(
|
|
113
|
+
await run.lifecycle.fire(hostCtx, "onStageRetry", ref, attempt, lifecycleCtxFor(run));
|
|
114
114
|
return { kind: "ok" };
|
|
115
115
|
},
|
|
116
116
|
},
|
|
@@ -119,8 +119,8 @@ export async function runScript(
|
|
|
119
119
|
if (result.kind === "aborted") return "halted";
|
|
120
120
|
if (result.kind === "exhausted") {
|
|
121
121
|
const failureSummary = result.failures.map(describeFailure).join("; ");
|
|
122
|
-
await
|
|
123
|
-
|
|
122
|
+
await recordFatalFailure(
|
|
123
|
+
hostCtx,
|
|
124
124
|
scriptAuditCtx(run, stage, stageNumber),
|
|
125
125
|
failedArgs(FAIL_VALIDATION_EXHAUSTED(stage.name, failureSummary)),
|
|
126
126
|
);
|
|
@@ -137,12 +137,12 @@ export async function runScript(
|
|
|
137
137
|
stage.def,
|
|
138
138
|
);
|
|
139
139
|
if (!persisted) {
|
|
140
|
-
failAuditWrite(
|
|
140
|
+
failAuditWrite(hostCtx, run.state, stage.name);
|
|
141
141
|
return "halted";
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
-
await run.lifecycle.fire(
|
|
145
|
-
return advance(
|
|
144
|
+
await run.lifecycle.fire(hostCtx, "onStageEnd", ref, output, lifecycleCtxFor(run));
|
|
145
|
+
return advance(hostCtx, stage.name, idx, run);
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
type ScriptInvocationResult =
|
|
@@ -157,7 +157,7 @@ type ScriptInvocationResult =
|
|
|
157
157
|
* failure attributed via `MSG_SCRIPT_THREW` + `ERR_SCRIPT_THREW`.
|
|
158
158
|
*/
|
|
159
159
|
async function invokeRun(
|
|
160
|
-
|
|
160
|
+
hostCtx: WorkflowHostContext,
|
|
161
161
|
stage: ResolvedStage,
|
|
162
162
|
scriptCtx: ScriptContext,
|
|
163
163
|
run: RunContext,
|
|
@@ -172,10 +172,10 @@ async function invokeRun(
|
|
|
172
172
|
return { ok: true, raw };
|
|
173
173
|
} catch (e) {
|
|
174
174
|
const reason = formatError(e);
|
|
175
|
-
// `
|
|
175
|
+
// `recordFatalFailure` fires `onStageError` itself — the caller must
|
|
176
176
|
// not fire a second time on the `ok: false` return.
|
|
177
|
-
await
|
|
178
|
-
|
|
177
|
+
await recordFatalFailure(
|
|
178
|
+
hostCtx,
|
|
179
179
|
scriptAuditCtx(run, stage, stageNumber),
|
|
180
180
|
failedArgs(FAIL_SCRIPT_THREW(stage.name, reason)),
|
|
181
181
|
);
|
|
@@ -184,7 +184,7 @@ async function invokeRun(
|
|
|
184
184
|
}
|
|
185
185
|
|
|
186
186
|
/**
|
|
187
|
-
* Build the `
|
|
187
|
+
* Build the `AuditContext`-shaped object `recordFatalFailure` needs for
|
|
188
188
|
* a script-stage halt. The `skill` field doubles as the lifecycle
|
|
189
189
|
* `onStageError` ref payload — using `stage.name` keeps the failure
|
|
190
190
|
* attribution aligned with the success row's `stage` identity.
|
package/sessions/bash-strikes.ts
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* (the resume fold's shape-filtered readers ignore it, like errMsg).
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import type {
|
|
25
|
+
import type { StageSessionContext } from "../types.js";
|
|
26
26
|
|
|
27
27
|
const DEFAULT_BASH_TIMEOUT_STRIKES = 2; // one retry of a hung command is the operator-grade default.
|
|
28
28
|
const MIN_BASH_TIMEOUT_STRIKES = 1; // at least one strike before any escalation.
|
|
@@ -87,12 +87,12 @@ export function bashTimeoutSteeringMessage(reason: string, strikesRemaining: num
|
|
|
87
87
|
|
|
88
88
|
/**
|
|
89
89
|
* Private per-activation strike budget: the mutable accounting (used counter +
|
|
90
|
-
* reasons accumulator) that previously lived as two fields on `
|
|
91
|
-
* Held in a module-level `WeakMap<
|
|
90
|
+
* reasons accumulator) that previously lived as two fields on `StageSessionContext`.
|
|
91
|
+
* Held in a module-level `WeakMap<StageSessionContext, StrikeBudget>` so the budget
|
|
92
92
|
* survives `postStage`'s tail-recursive recovery self-call
|
|
93
93
|
* (packages/rpiv-workflow/sessions/sessions.ts:134 — same `s` ⇒ same entry ⇒ the
|
|
94
94
|
* incremented counter is visible on the recursive turn) and is fresh on every
|
|
95
|
-
* new `
|
|
95
|
+
* new `StageSessionContext` for free (no entry ⇒ zero strikes). `ceiling` is resolved
|
|
96
96
|
* ONCE at first consume from the immutable `s.bashTimeoutStrikes ?? BASH_TIMEOUT_STRIKES`
|
|
97
97
|
* override and frozen into the budget.
|
|
98
98
|
*/
|
|
@@ -104,7 +104,7 @@ interface StrikeBudget {
|
|
|
104
104
|
|
|
105
105
|
// `let` (not `const`): `__resetStrikeBudgets()` reassigns this to a fresh map —
|
|
106
106
|
// `WeakMap` exposes no `.clear()`. Keyed by session identity (see StrikeBudget above).
|
|
107
|
-
let strikeBudgets: WeakMap<
|
|
107
|
+
let strikeBudgets: WeakMap<StageSessionContext, StrikeBudget> = new WeakMap();
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
110
|
* The ONLY creator of a `StrikeBudget`: lazily allocated on the first consume,
|
|
@@ -114,7 +114,7 @@ let strikeBudgets: WeakMap<StageSession, StrikeBudget> = new WeakMap();
|
|
|
114
114
|
* because `bashTimeoutStrikeHistory` runs on every successful stage inside
|
|
115
115
|
* `recordStageSuccess` (packages/rpiv-workflow/sessions/success-persist.ts:49).
|
|
116
116
|
*/
|
|
117
|
-
function budgetForConsume(s:
|
|
117
|
+
function budgetForConsume(s: StageSessionContext): StrikeBudget {
|
|
118
118
|
let b = strikeBudgets.get(s);
|
|
119
119
|
if (!b) {
|
|
120
120
|
b = { ceiling: s.bashTimeoutStrikes ?? BASH_TIMEOUT_STRIKES, used: 0, reasons: [] };
|
|
@@ -133,7 +133,7 @@ function budgetForConsume(s: StageSession): StrikeBudget {
|
|
|
133
133
|
* field-on-session implementation; the `reasons` lazy-init collapses to a plain
|
|
134
134
|
* `push` (the budget initializes `reasons: []`).
|
|
135
135
|
*/
|
|
136
|
-
export function consumeBashStrike(s:
|
|
136
|
+
export function consumeBashStrike(s: StageSessionContext, reason: string): boolean {
|
|
137
137
|
const b = budgetForConsume(s);
|
|
138
138
|
if (b.used >= b.ceiling) return false; // exhausted — mutate nothing; caller escalates.
|
|
139
139
|
b.used += 1;
|
|
@@ -149,7 +149,7 @@ export function consumeBashStrike(s: StageSession, reason: string): boolean {
|
|
|
149
149
|
* override (the former `bashStrikeCeiling` helper's job — removed — moved into
|
|
150
150
|
* the budget's `ceiling` field + this fallback), allocating nothing.
|
|
151
151
|
*/
|
|
152
|
-
export function bashStrikesRemaining(s:
|
|
152
|
+
export function bashStrikesRemaining(s: StageSessionContext): number {
|
|
153
153
|
const b = strikeBudgets.get(s);
|
|
154
154
|
const ceiling = b?.ceiling ?? s.bashTimeoutStrikes ?? BASH_TIMEOUT_STRIKES;
|
|
155
155
|
return Math.max(0, ceiling - (b?.used ?? 0));
|
|
@@ -163,7 +163,7 @@ export function bashStrikesRemaining(s: StageSession): number {
|
|
|
163
163
|
* construction (invariant 2's 1st layer), not just by the `used <= 0` guard.
|
|
164
164
|
* `reasons` lists each consumed strike's host reason, in consumption order.
|
|
165
165
|
*/
|
|
166
|
-
export function bashTimeoutStrikeHistory(s:
|
|
166
|
+
export function bashTimeoutStrikeHistory(s: StageSessionContext): { count: number; reasons: string[] } | undefined {
|
|
167
167
|
const b = strikeBudgets.get(s);
|
|
168
168
|
if (!b || b.used <= 0) return undefined;
|
|
169
169
|
return { count: b.used, reasons: b.reasons };
|