@juicesharp/rpiv-workflow 2.1.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 +16 -8
- package/audit-rows.ts +10 -0
- package/audit.ts +48 -26
- package/chain-state.ts +26 -1
- package/death-scene.ts +232 -0
- package/docs/embedding.md +1 -1
- package/docs/workflow-authoring.md +2 -2
- package/events.ts +3 -27
- package/execution-host.ts +9 -0
- package/failure-memos.ts +74 -0
- package/handle.ts +16 -1
- package/host.ts +13 -1
- package/internal.ts +1 -0
- package/loop-def.ts +9 -5
- package/loop-kinds.ts +16 -13
- package/loop-parallel.ts +116 -95
- package/loop-waves.ts +3 -3
- package/loop.ts +46 -43
- package/messages.ts +53 -14
- package/outcomes/collectors/workspace-diff.ts +3 -3
- package/outcomes/git-commit.ts +11 -4
- package/output-spec.ts +7 -7
- package/package.json +5 -2
- package/preview.ts +8 -1
- package/registration.ts +8 -3
- package/routing.ts +1 -41
- package/runner/chain-advance.ts +50 -32
- 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-context.ts +13 -3
- package/runner/run-stage.ts +134 -60
- package/runner/runner.ts +19 -9
- package/runner/script-stage.ts +17 -17
- package/sessions/bash-strikes.ts +182 -0
- package/sessions/extraction.ts +51 -37
- package/sessions/halt-routing.ts +18 -13
- package/sessions/index.ts +1 -1
- package/sessions/reattach.ts +10 -10
- package/sessions/sessions.ts +63 -19
- package/sessions/spawn.ts +5 -5
- package/sessions/success-persist.ts +9 -3
- package/stage-def.ts +2 -2
- package/stage-errors.ts +1 -1
- package/stage-identity.ts +43 -4
- package/state/index.ts +1 -1
- package/state/names.ts +1 -1
- package/state/reads.ts +1 -1
- package/state/state.ts +14 -4
- package/state/writes.ts +2 -2
- package/transcript.ts +30 -0
- package/types.ts +105 -15
- package/worktree-digest.ts +148 -0
package/loop-parallel.ts
CHANGED
|
@@ -1,30 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* loop-parallel.ts — bounded-parallel, dependency-
|
|
3
|
-
*
|
|
4
|
-
* —
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* to the pre-wave flat dispatch.
|
|
2
|
+
* loop-parallel.ts — bounded-parallel, dependency-GATED fanout dispatch. Every active
|
|
3
|
+
* unit is dispatched into ONE generation-wide semaphore and awaits ITS OWN `Unit.deps`
|
|
4
|
+
* — not a topological level — so a dependent opens the instant its deps have filled
|
|
5
|
+
* their slots, even while unrelated siblings are still running. Kahn levels
|
|
6
|
+
* (`computeWaveLevels`, loop-waves.ts) remain the CYCLE GUARD and supply the dispatch
|
|
7
|
+
* ORDER (level asc, index asc); they are no longer a barrier. A deps-free fanout is a
|
|
8
|
+
* flat dispatch, and a cap-1 fanout (`implement`) dispatches in exactly declared order.
|
|
10
9
|
*
|
|
11
|
-
*
|
|
12
|
-
* (
|
|
13
|
-
*
|
|
14
|
-
* `lastAllocatedStageNumber`, `stagesCompleted`, `output`, `primaryArtifact`
|
|
15
|
-
* CONCURRENTLY across siblings. That is safe — every such mutator is a synchronous,
|
|
16
|
-
* `await`-free read-modify-write (JS run-to-completion makes each atomic), and every
|
|
17
|
-
* order-sensitive result is re-derived by the index-addressed fold or overwritten by
|
|
18
|
-
* `projectResult` at close.
|
|
10
|
+
* Deps are awaited BEFORE the semaphore is acquired — a waiting dependent never holds a
|
|
11
|
+
* slot, so hold-and-wait (and with it deadlock) cannot arise; the DAG is acyclic by the
|
|
12
|
+
* guard above.
|
|
19
13
|
*
|
|
20
|
-
*
|
|
14
|
+
* CONCURRENCY MODEL — why a per-unit fold is safe. Each unit folds itself through
|
|
15
|
+
* `foldFanoutCompletion` in its own continuation, so the cursor is now advanced from
|
|
16
|
+
* many continuations rather than one serial post-`allSettled` pass. That is safe for the
|
|
17
|
+
* same reason `run.state` already was: every mutator is a synchronous, `await`-free
|
|
18
|
+
* read-modify-write (JS run-to-completion makes each atomic), AND the fold is
|
|
19
|
+
* COMMUTATIVE by construction — `slots[i]` is index-addressed, `filledCount` is
|
|
20
|
+
* recomputed by `reduce` over the whole array (never incremented), and `lastProduce` is
|
|
21
|
+
* derived by `lastNonFailedSlot` scanning slots high→low. Declared order therefore
|
|
22
|
+
* survives completion order without a barrier. The one genuinely order-sensitive write,
|
|
23
|
+
* `state.primaryArtifact`, is last-writer-wins during dispatch either way (each worker's
|
|
24
|
+
* `postStage` already races it) and is overwritten by `projectResult` at close. The
|
|
25
|
+
* durable trail is likewise completion-ordered already, and resume places by
|
|
26
|
+
* `row.unitIndex`, never by trail order.
|
|
27
|
+
*
|
|
28
|
+
* `runFanoutGeneration` is the ONE orchestrator both the live entry (`runFanoutParallel`)
|
|
21
29
|
* and the resume re-dispatch (`runFanoutResume`) — both thin wrappers in loop.ts —
|
|
22
30
|
* degenerate to: each passes its `active` operand set (live: `0..dispatchCount-1`;
|
|
23
31
|
* resume: the still-pending indices) and a `finalTail` for the path-specific completion
|
|
24
32
|
* (live: hitCap-vs-finishLoop on the cap; resume: always finishLoop). It owns ONE
|
|
25
|
-
* per-generation `genAbort`
|
|
26
|
-
*
|
|
27
|
-
* each with `active`, and runs `dispatchWave` per non-empty level.
|
|
33
|
+
* per-generation `genAbort` (so a fail-fast halt or run-abort cancels in-flight siblings
|
|
34
|
+
* AND rejects every queued acquire), computes the order, and runs the generation.
|
|
28
35
|
*
|
|
29
36
|
* This module is a downward leaf: it consumes the shared loop foundation
|
|
30
37
|
* (`loop-kinds.ts`, `loop-waves.ts`) and never imports loop.ts back (loop.ts → here only).
|
|
@@ -58,20 +65,20 @@ const fanoutUnitRef = (e: LoopEntry, i: number): UnitRef => {
|
|
|
58
65
|
};
|
|
59
66
|
|
|
60
67
|
/**
|
|
61
|
-
* THE
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* (live: hitCap-vs-finishLoop; resume: finishLoop)
|
|
68
|
+
* THE generation orchestrator — owns ONE per-generation `AbortController` and ONE
|
|
69
|
+
* semaphore across every active unit. Computes the Kahn levels over the full unit list
|
|
70
|
+
* (the cycle guard) and flattens them into the dispatch ORDER, intersected with `active`
|
|
71
|
+
* (live: the first-cap indices; resume: the still-pending indices). Runs `finalTail`
|
|
72
|
+
* (live: hitCap-vs-finishLoop; resume: finishLoop) once the generation settles.
|
|
66
73
|
*
|
|
67
74
|
* The `genAbort` fires on EITHER (a) run-level abort (Ctrl-C, `run.signal` — propagated
|
|
68
|
-
* below) OR (b) the first fail-fast unit halt inside `
|
|
69
|
-
* dropped BEFORE `finalTail` (which runs the downstream chain) so it never accumulates
|
|
75
|
+
* below) OR (b) the first fail-fast unit halt inside `dispatchGeneration`. The listener
|
|
76
|
+
* is dropped BEFORE `finalTail` (which runs the downstream chain) so it never accumulates
|
|
70
77
|
* across stages. Levels are computed BEFORE the listener is wired, so a defensive
|
|
71
78
|
* cycle-throw from `computeWaveLevels` can't leak it.
|
|
72
79
|
*/
|
|
73
|
-
export async function
|
|
74
|
-
|
|
80
|
+
export async function runFanoutGeneration(
|
|
81
|
+
hostCtx: WorkflowHostContext,
|
|
75
82
|
e: LoopEntry,
|
|
76
83
|
cursor: LoopCursor,
|
|
77
84
|
run: RunContext,
|
|
@@ -80,14 +87,19 @@ export async function runFanoutWaves(
|
|
|
80
87
|
finalTail: () => Promise<void> | void,
|
|
81
88
|
): Promise<void> {
|
|
82
89
|
const activeSet = new Set(active);
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
//
|
|
87
|
-
const
|
|
90
|
+
// Levels earn their keep twice over, and neither use is a barrier: `computeWaveLevels`
|
|
91
|
+
// THROWS `invariantPreflight` on a dependency cycle (the defensive guard), and its
|
|
92
|
+
// flattening is the dispatch order — level asc, index asc, i.e. the order the wave
|
|
93
|
+
// scheduler used, so a cap-1 fanout stays byte-identical.
|
|
94
|
+
const order = computeWaveLevels(e.units!, e.name)
|
|
95
|
+
.flat()
|
|
96
|
+
.filter((i) => activeSet.has(i));
|
|
97
|
+
// The dep→index map is resolved unconditionally now: dep GATING needs it whether or
|
|
98
|
+
// not `depArtifactFlag` is set. `depArtifactSuffix` keeps its own flag guard.
|
|
99
|
+
const idToIndex = unitIdIndex(e.units!);
|
|
88
100
|
// No active units (e.g. a resume with everything already filled) — still close the
|
|
89
101
|
// loop via the tail (projection + advance), matching today's empty/all-done path.
|
|
90
|
-
if (
|
|
102
|
+
if (order.length === 0) return finalTail();
|
|
91
103
|
|
|
92
104
|
const genAbort = new AbortController();
|
|
93
105
|
// Name the handler so it can be REMOVED once this generation settles — run.signal
|
|
@@ -99,81 +111,90 @@ export async function runFanoutWaves(
|
|
|
99
111
|
}
|
|
100
112
|
const detach = () => run.signal?.removeEventListener("abort", onRunAbort);
|
|
101
113
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
if (run.signal?.aborted) {
|
|
113
|
-
detach();
|
|
114
|
-
return deps.recordAborted(curCtx, e.name, run); // mid-flight abort → FAIL_WORKFLOW_ABORTED
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
detach(); // all waves settled cleanly — drop the run-lifetime listener BEFORE the tail
|
|
114
|
+
await dispatchGeneration(hostCtx, e, cursor, run, deps, order, genAbort, idToIndex);
|
|
115
|
+
detach(); // generation settled — drop the run-lifetime listener BEFORE the tail
|
|
116
|
+
|
|
117
|
+
// The same two gates the wave loop ran between levels, now once at the end: a
|
|
118
|
+
// fail-fast halt already terminated state inside the worker and aborted its
|
|
119
|
+
// siblings; a run abort drained the semaphore and rejected every later acquire.
|
|
120
|
+
if (run.state.termination.status !== "running") return;
|
|
121
|
+
if (run.signal?.aborted) return deps.recordAborted(hostCtx, e.name, run); // mid-flight abort → FAIL_WORKFLOW_ABORTED
|
|
118
122
|
return finalTail();
|
|
119
123
|
}
|
|
120
124
|
|
|
121
125
|
/**
|
|
122
|
-
* Dispatch
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
* runs the tail once, after the last wave). NEVER throws — an unexpected worker
|
|
127
|
-
* rejection lands a terminal-failure row via `recordWorkerThrow`; allSettled
|
|
128
|
-
* guarantees every unit settled.
|
|
126
|
+
* Dispatch the whole generation through a shared semaphore + the per-generation
|
|
127
|
+
* `genAbort`, each unit gated on ITS OWN deps and folding itself at its DECLARED index.
|
|
128
|
+
* NEVER throws — an unexpected worker rejection lands a terminal-failure row via
|
|
129
|
+
* `recordWorkerThrow`; allSettled guarantees every unit settled.
|
|
129
130
|
*/
|
|
130
|
-
async function
|
|
131
|
-
|
|
131
|
+
async function dispatchGeneration(
|
|
132
|
+
hostCtx: WorkflowHostContext,
|
|
132
133
|
e: LoopEntry,
|
|
133
134
|
cursor: LoopCursor,
|
|
134
135
|
run: RunContext,
|
|
135
136
|
deps: LoopDeps,
|
|
136
137
|
ops: readonly number[],
|
|
137
138
|
genAbort: AbortController,
|
|
138
|
-
idToIndex: Map<string, number
|
|
139
|
+
idToIndex: Map<string, number>,
|
|
139
140
|
): Promise<void> {
|
|
140
141
|
const failFast = isFailFast(e.loop);
|
|
141
142
|
// A fanout may cap its own concurrency BELOW the host cap (`implement`'s
|
|
142
143
|
// `concurrency: 1`); floored at 1, never raised above the host cap.
|
|
143
144
|
const loopCap = e.loop.kind === "fanout" ? e.loop.concurrency : undefined;
|
|
144
145
|
const sem = new Semaphore(
|
|
145
|
-
Math.max(1, Math.min(loopCap ??
|
|
146
|
+
Math.max(1, Math.min(loopCap ?? hostCtx.maxConcurrency, hostCtx.maxConcurrency)),
|
|
146
147
|
genAbort.signal,
|
|
147
148
|
); // drains queued units on either abort
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
149
|
+
|
|
150
|
+
// One readiness latch per DISPATCHED unit, index-addressed. A dep with no latch is
|
|
151
|
+
// already settled: either it is outside `ops` (a resume whose prior invocation filled
|
|
152
|
+
// the slot) or it is dangling (`computeWaveLevels` treats those as satisfied too —
|
|
153
|
+
// `ensureUnitDeps` owns the dangling report).
|
|
154
|
+
const latch = new Array<Promise<void> | undefined>(e.units!.length);
|
|
155
|
+
const open = new Array<(() => void) | undefined>(e.units!.length);
|
|
156
|
+
for (const i of ops)
|
|
157
|
+
latch[i] = new Promise<void>((resolve) => {
|
|
158
|
+
open[i] = resolve;
|
|
159
|
+
});
|
|
160
|
+
const depLatches = (i: number): Promise<void>[] =>
|
|
161
|
+
(e.units![i]!.deps ?? []).flatMap((d) => {
|
|
162
|
+
const di = idToIndex.get(d);
|
|
163
|
+
const l = di === undefined ? undefined : latch[di];
|
|
164
|
+
return l ? [l] : [];
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
await Promise.allSettled(
|
|
168
|
+
ops.map(async (i) => {
|
|
169
|
+
// Await THIS unit's deps — never a whole level — and do it BEFORE acquiring a
|
|
170
|
+
// slot, so a waiting dependent never holds one.
|
|
171
|
+
await Promise.all(depLatches(i));
|
|
172
|
+
try {
|
|
173
|
+
// Resolve `--upstream`-style dep-artifact injection from the slots this
|
|
174
|
+
// unit's deps just filled. Empty when the loop sets no flag or no deps.
|
|
175
|
+
const suffix = depArtifactSuffix(e, cursor, i, idToIndex);
|
|
176
|
+
const out = await sem.run(() => dispatchUnitDetached(hostCtx, e, i, run, deps, genAbort.signal, suffix));
|
|
177
|
+
// ONE synchronous block, and its order is load-bearing. (1) A fail-fast
|
|
178
|
+
// unit's worker terminated state via recordFatalFailure — fire genAbort
|
|
179
|
+
// so in-flight siblings get session.abort()'d NOW and no dependent released
|
|
180
|
+
// below can still slip into the semaphore. (2) Fold BEFORE the latch opens:
|
|
181
|
+
// a dependent's `depArtifactSuffix` reads `cursor.slots`, so an early
|
|
182
|
+
// release would make it design blind for this dep.
|
|
183
|
+
if (failFast && run.state.termination.status !== "running") genAbort.abort();
|
|
184
|
+
cursor.ranThisInvocation++;
|
|
185
|
+
// index-addressed placement (shared with the resume fold) so declared order
|
|
186
|
+
// survives parallel completion + dep gating + resume.
|
|
187
|
+
foldFanoutCompletion(run.state, cursor, e.def, e.name, i, e.units!.length, out);
|
|
188
|
+
} catch (reason) {
|
|
189
|
+
// aborted / never-started → unfilled slot (resume re-dispatches)
|
|
190
|
+
if (!isAbortError(reason)) await deps.recordWorkerThrow(hostCtx, fanoutUnitRef(e, i), e.skill, run, reason);
|
|
191
|
+
} finally {
|
|
192
|
+
// Dependents proceed even when this unit failed — `depArtifactSuffix` skips
|
|
193
|
+
// a failed/unfilled slot, so they design blind for it rather than stalling.
|
|
194
|
+
open[i]?.();
|
|
195
|
+
}
|
|
161
196
|
}),
|
|
162
197
|
);
|
|
163
|
-
// `ops[k]` maps the k-th settled result back to its DECLARED unit index.
|
|
164
|
-
for (let k = 0; k < settled.length; k++) {
|
|
165
|
-
const r = settled[k]!;
|
|
166
|
-
const i = ops[k]!;
|
|
167
|
-
if (r.status === "rejected") {
|
|
168
|
-
if (isAbortError(r.reason)) continue; // aborted / never-started → unfilled slot (resume re-dispatches)
|
|
169
|
-
await deps.recordWorkerThrow(curCtx, fanoutUnitRef(e, i), e.skill, run, r.reason);
|
|
170
|
-
continue;
|
|
171
|
-
}
|
|
172
|
-
cursor.ranThisInvocation++;
|
|
173
|
-
// index-addressed placement (shared with the resume fold) so declared order
|
|
174
|
-
// survives parallel completion + waves + resume.
|
|
175
|
-
foldFanoutCompletion(run.state, cursor, e.def, e.name, i, e.units!.length, r.value);
|
|
176
|
-
}
|
|
177
198
|
}
|
|
178
199
|
|
|
179
200
|
/**
|
|
@@ -181,7 +202,7 @@ async function dispatchWave(
|
|
|
181
202
|
* dep whose slot is filled with a NON-FAILED output. A failed/sentinel or still-unfilled
|
|
182
203
|
* dep slot is SKIPPED (the dependent designs blind for that dep) — so a failed upstream
|
|
183
204
|
* degrades gracefully instead of injecting a broken path; synthesize stays the backstop.
|
|
184
|
-
* Dangling ids never reach here (`
|
|
205
|
+
* Dangling ids never reach here (`ensureUnitDeps` rejected them at the live entry); the
|
|
185
206
|
* `undefined` guard is defensive.
|
|
186
207
|
*/
|
|
187
208
|
function depArtifactSuffix(e: LoopEntry, cursor: LoopCursor, index: number, idToIndex: Map<string, number>): string {
|
|
@@ -207,7 +228,7 @@ function depArtifactSuffix(e: LoopEntry, cursor: LoopCursor, index: number, idTo
|
|
|
207
228
|
* from the pre-wave dispatcher: collect-all unit failure → sentinel; fail-fast halt →
|
|
208
229
|
* placement sentinel + graceful return; ABORT → throws WorkflowAbortError → unfilled slot.) */
|
|
209
230
|
async function dispatchUnitDetached(
|
|
210
|
-
|
|
231
|
+
hostCtx: WorkflowHostContext,
|
|
211
232
|
e: LoopEntry,
|
|
212
233
|
index: number,
|
|
213
234
|
run: RunContext,
|
|
@@ -218,16 +239,16 @@ async function dispatchUnitDetached(
|
|
|
218
239
|
if (signal?.aborted) throw new WorkflowAbortError(); // never open a child after abort; isAbortError → unfilled slot
|
|
219
240
|
const u = fanoutUnitAt(e, index, promptSuffix);
|
|
220
241
|
await run.lifecycle.fire(
|
|
221
|
-
|
|
242
|
+
hostCtx,
|
|
222
243
|
"onUnitStart",
|
|
223
244
|
skillStageRef(e.name, e.stageIdx + 1, u.skill),
|
|
224
245
|
{ role: u.role, index, unitId: u.id, label: u.label, skill: u.skill },
|
|
225
246
|
lifecycleCtxFor(run),
|
|
226
247
|
);
|
|
227
|
-
const snapshot = await deps.captureSnapshot(
|
|
248
|
+
const snapshot = await deps.captureSnapshot(hostCtx, e.name, u.def, e.stageIdx, run);
|
|
228
249
|
let captured: Output | undefined;
|
|
229
|
-
await deps.
|
|
230
|
-
|
|
250
|
+
await deps.executeStageSession(
|
|
251
|
+
hostCtx,
|
|
231
252
|
buildUnitSession(e, u, index, run, snapshot, signal, (_child, output) => {
|
|
232
253
|
captured = output;
|
|
233
254
|
return Promise.resolve();
|
package/loop-waves.ts
CHANGED
|
@@ -44,8 +44,8 @@ export function unitIdIndex(units: readonly Unit[]): Map<string, number> {
|
|
|
44
44
|
* (unlevellable) node ⇒ a dependency cycle ⇒ `invariantPreflight`.
|
|
45
45
|
*
|
|
46
46
|
* Dangling deps (an id matching no unit) are SKIPPED here (treated as satisfied) so
|
|
47
|
-
* a single missing id can't masquerade as a cycle; `
|
|
48
|
-
* reports dangling refs. At the live entry `
|
|
47
|
+
* a single missing id can't masquerade as a cycle; `ensureUnitDeps` is the one that
|
|
48
|
+
* reports dangling refs. At the live entry `ensureUnitDeps` runs first, so a cycle
|
|
49
49
|
* reaching here is post-validation/defensive; the throw still lands a clean halt.
|
|
50
50
|
*/
|
|
51
51
|
export function computeWaveLevels(units: readonly Unit[], stage: string): number[][] {
|
|
@@ -86,7 +86,7 @@ export function computeWaveLevels(units: readonly Unit[], stage: string): number
|
|
|
86
86
|
* `invariantPreflight` halt attributed to `stage`. Cheap; runs before dispatch so the
|
|
87
87
|
* dispatcher never has to throw. Cycle detection delegates to `computeWaveLevels`.
|
|
88
88
|
*/
|
|
89
|
-
export function
|
|
89
|
+
export function ensureUnitDeps(units: readonly Unit[], stage: string): void {
|
|
90
90
|
const ids = new Set(units.map(unitTagOf));
|
|
91
91
|
for (const u of units) {
|
|
92
92
|
for (const d of u.deps ?? []) {
|
package/loop.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* a degenerate assess loop; verify-aware code is presentation only
|
|
12
12
|
* (role/label flavoring keyed on `e.def.verify` inside the assess strategy).
|
|
13
13
|
*
|
|
14
|
-
* Every unit runs `
|
|
14
|
+
* Every unit runs `executeStageSession` with a pre-decorated session: `stageName`
|
|
15
15
|
* carries the DISPLAY decoration (`decorateStage`), `unit` carries the
|
|
16
16
|
* machine identity that lands in the row's `parent`/`role`/`unitId`/`unitIndex`
|
|
17
17
|
* fields and the `onUnitStart`/`onUnitEnd` payloads.
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* Continuation-style: each unit's `onSuccess` advances the cursor and
|
|
20
20
|
* re-enters `step`. Everything is awaited up the stack, so a throw from a
|
|
21
21
|
* user fn (`units`/`next`/`feedForward`/`done`/judge prompt) propagates to
|
|
22
|
-
* `
|
|
22
|
+
* `dispatchStageOrRecordFailure`'s single catch — a thrown `StagePreflightError`
|
|
23
23
|
* (the `haltPreflight` consumer contract) keeps its own attribution.
|
|
24
24
|
*
|
|
25
25
|
* Capture semantics (the post-refactor bug class — pinned):
|
|
@@ -54,7 +54,7 @@ import {
|
|
|
54
54
|
presentedKindOf,
|
|
55
55
|
sequentialStrategyOf,
|
|
56
56
|
} from "./loop-kinds.js";
|
|
57
|
-
import {
|
|
57
|
+
import { runFanoutGeneration } from "./loop-parallel.js";
|
|
58
58
|
import { MSG_LOOP_CAP_ADVANCE, MSG_LOOP_ZERO_UNITS } from "./messages.js";
|
|
59
59
|
import { appendLoopCap } from "./state/index.js";
|
|
60
60
|
import type { RunContext, WorkflowHostContext } from "./types.js";
|
|
@@ -67,14 +67,14 @@ import type { RunContext, WorkflowHostContext } from "./types.js";
|
|
|
67
67
|
* aligned by sharing this one helper.
|
|
68
68
|
*/
|
|
69
69
|
export async function announceLoopStart(
|
|
70
|
-
|
|
70
|
+
hostCtx: WorkflowHostContext,
|
|
71
71
|
run: RunContext,
|
|
72
72
|
e: Pick<LoopEntry, "stageIdx" | "name" | "skill" | "def" | "loop" | "units">,
|
|
73
73
|
): Promise<void> {
|
|
74
74
|
const ref = skillStageRef(e.name, e.stageIdx + 1, e.skill);
|
|
75
|
-
await run.lifecycle.fire(
|
|
75
|
+
await run.lifecycle.fire(hostCtx, "onStageStart", ref, lifecycleCtxFor(run));
|
|
76
76
|
await run.lifecycle.fire(
|
|
77
|
-
|
|
77
|
+
hostCtx,
|
|
78
78
|
"onLoopStart",
|
|
79
79
|
ref,
|
|
80
80
|
{ kind: presentedKindOf(e.def, e.loop), ...(e.units ? { units: e.units } : {}) },
|
|
@@ -90,7 +90,7 @@ const isPristine = (cursor: LoopCursor): boolean =>
|
|
|
90
90
|
|
|
91
91
|
/** Run (or resume) one loop generation. The caller fired onStageStart/onLoopStart. */
|
|
92
92
|
export async function runLoop(
|
|
93
|
-
|
|
93
|
+
hostCtx: WorkflowHostContext,
|
|
94
94
|
e: LoopEntry,
|
|
95
95
|
cursor: LoopCursor,
|
|
96
96
|
run: RunContext,
|
|
@@ -103,20 +103,20 @@ export async function runLoop(
|
|
|
103
103
|
// fold. iterate/assess keep the sequential step()/advanceCursor.
|
|
104
104
|
// (Live entry is always pristine; resume routes to runFanoutResume directly.)
|
|
105
105
|
if (e.loop.kind === "fanout" && loopStrategyOf(e.loop.kind).parallelizable && isPristine(cursor)) {
|
|
106
|
-
return runFanoutParallel(
|
|
106
|
+
return runFanoutParallel(hostCtx, e, cursor, cap, run, deps);
|
|
107
107
|
}
|
|
108
|
-
await step(
|
|
108
|
+
await step(hostCtx, e, cursor, cap, run, deps);
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
/**
|
|
112
|
-
* Live bounded-parallel fanout entry — thin wrapper over `
|
|
113
|
-
* (loop-parallel.ts). Dispatches the first `cap` units (`0..dispatchCount-1`)
|
|
114
|
-
*
|
|
115
|
-
* `hitCap
|
|
116
|
-
*
|
|
112
|
+
* Live bounded-parallel fanout entry — thin wrapper over `runFanoutGeneration`
|
|
113
|
+
* (loop-parallel.ts). Dispatches the first `cap` units (`0..dispatchCount-1`), each
|
|
114
|
+
* gated on its own deps; on completion picks the cap policy: over-cap units trip
|
|
115
|
+
* `hitCap`, otherwise the loop finishes. iterate/assess never reach here (not
|
|
116
|
+
* parallelizable).
|
|
117
117
|
*/
|
|
118
118
|
function runFanoutParallel(
|
|
119
|
-
|
|
119
|
+
hostCtx: WorkflowHostContext,
|
|
120
120
|
e: LoopEntry,
|
|
121
121
|
cursor: LoopCursor,
|
|
122
122
|
cap: number,
|
|
@@ -124,8 +124,10 @@ function runFanoutParallel(
|
|
|
124
124
|
deps: LoopDeps,
|
|
125
125
|
): Promise<void> {
|
|
126
126
|
const active = Array.from({ length: Math.min(e.units!.length, cap) }, (_u, i) => i);
|
|
127
|
-
return
|
|
128
|
-
e.units!.length > cap
|
|
127
|
+
return runFanoutGeneration(hostCtx, e, cursor, run, deps, active, () =>
|
|
128
|
+
e.units!.length > cap
|
|
129
|
+
? hitCap(hostCtx, e, cursor, cap, cap, run, deps)
|
|
130
|
+
: finishLoop(hostCtx, e, cursor, run, deps),
|
|
129
131
|
);
|
|
130
132
|
}
|
|
131
133
|
|
|
@@ -136,13 +138,14 @@ export function pendingFanoutIndices(cursor: LoopCursor, total: number): number[
|
|
|
136
138
|
return out;
|
|
137
139
|
}
|
|
138
140
|
|
|
139
|
-
/** Resume re-dispatch — thin wrapper over `
|
|
140
|
-
* fanout units
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
141
|
+
/** Resume re-dispatch — thin wrapper over `runFanoutGeneration`. Runs the still-pending
|
|
142
|
+
* fanout units, each gated on its own deps, folding each at its declared index (so
|
|
143
|
+
* completed slots keep their position), and always finishes (the live run already
|
|
144
|
+
* settled the cap policy). A ≥3-level DAG where the roots completed but their
|
|
145
|
+
* dependents aborted re-dispatches correctly: already-filled slots are skipped (not in
|
|
146
|
+
* `pending`) and so carry no readiness latch — a pending unit whose dep is outside
|
|
147
|
+
* `pending` treats it as satisfied and reads its filled slot, while a pending unit
|
|
148
|
+
* whose dep is ALSO pending still waits for it.
|
|
146
149
|
* Pending fanout units COLD re-dispatch (a fresh child each) — fanout units are
|
|
147
150
|
* idempotent (each writes its own distinct artifact at its declared index), so a
|
|
148
151
|
* partial in-flight session is discarded rather than reattached; this matches
|
|
@@ -150,14 +153,14 @@ export function pendingFanoutIndices(cursor: LoopCursor, total: number): number[
|
|
|
150
153
|
* `locateSessionFile` serve the SINGLE-STAGE session-backed reattach path
|
|
151
154
|
* (run-stage.ts), not this fanout re-dispatch. */
|
|
152
155
|
export function runFanoutResume(
|
|
153
|
-
|
|
156
|
+
hostCtx: WorkflowHostContext,
|
|
154
157
|
e: LoopEntry,
|
|
155
158
|
cursor: LoopCursor,
|
|
156
159
|
run: RunContext,
|
|
157
160
|
deps: LoopDeps,
|
|
158
161
|
pending: readonly number[],
|
|
159
162
|
): Promise<void> {
|
|
160
|
-
return
|
|
163
|
+
return runFanoutGeneration(hostCtx, e, cursor, run, deps, pending, () => finishLoop(hostCtx, e, cursor, run, deps));
|
|
161
164
|
}
|
|
162
165
|
|
|
163
166
|
// ---------------------------------------------------------------------------
|
|
@@ -165,7 +168,7 @@ export function runFanoutResume(
|
|
|
165
168
|
// ---------------------------------------------------------------------------
|
|
166
169
|
|
|
167
170
|
async function step(
|
|
168
|
-
|
|
171
|
+
hostCtx: WorkflowHostContext,
|
|
169
172
|
e: LoopEntry,
|
|
170
173
|
cursor: LoopCursor,
|
|
171
174
|
cap: number,
|
|
@@ -173,14 +176,14 @@ async function step(
|
|
|
173
176
|
deps: LoopDeps,
|
|
174
177
|
): Promise<void> {
|
|
175
178
|
const next = await sequentialStrategyOf(e.loop.kind).pull(e, cursor, cap, run);
|
|
176
|
-
if (next.kind === "complete") return finishLoop(
|
|
177
|
-
if (next.kind === "cap") return hitCap(
|
|
178
|
-
return dispatchUnit(
|
|
179
|
+
if (next.kind === "complete") return finishLoop(hostCtx, e, cursor, run, deps);
|
|
180
|
+
if (next.kind === "cap") return hitCap(hostCtx, e, cursor, next.count, cap, run, deps);
|
|
181
|
+
return dispatchUnit(hostCtx, e, cursor, next, cap, run, deps);
|
|
179
182
|
}
|
|
180
183
|
|
|
181
184
|
/** Dispatch one unit session; the onSuccess continuation advances the cursor and re-enters step. */
|
|
182
185
|
async function dispatchUnit(
|
|
183
|
-
|
|
186
|
+
hostCtx: WorkflowHostContext,
|
|
184
187
|
e: LoopEntry,
|
|
185
188
|
cursor: LoopCursor,
|
|
186
189
|
u: Extract<NextStep, { kind: "unit" }>,
|
|
@@ -189,17 +192,17 @@ async function dispatchUnit(
|
|
|
189
192
|
deps: LoopDeps,
|
|
190
193
|
): Promise<void> {
|
|
191
194
|
await run.lifecycle.fire(
|
|
192
|
-
|
|
195
|
+
hostCtx,
|
|
193
196
|
"onUnitStart",
|
|
194
197
|
skillStageRef(e.name, e.stageIdx + 1, u.skill),
|
|
195
198
|
{ role: u.role, index: cursor.index, unitId: u.id, label: u.label, skill: u.skill },
|
|
196
199
|
lifecycleCtxFor(run),
|
|
197
200
|
);
|
|
198
201
|
|
|
199
|
-
const snapshot = await deps.captureSnapshot(
|
|
202
|
+
const snapshot = await deps.captureSnapshot(hostCtx, e.name, u.def, e.stageIdx, run);
|
|
200
203
|
|
|
201
|
-
await deps.
|
|
202
|
-
|
|
204
|
+
await deps.executeStageSession(
|
|
205
|
+
hostCtx,
|
|
203
206
|
buildUnitSession(e, u, cursor.index, run, snapshot, run.signal, (freshCtx, output) => {
|
|
204
207
|
cursor.ranThisInvocation++;
|
|
205
208
|
// Fanout owns its channel + cursor through the index-addressed
|
|
@@ -293,7 +296,7 @@ export function projectResult(
|
|
|
293
296
|
* progress channel); a resumed finished loop stays silent too.
|
|
294
297
|
*/
|
|
295
298
|
async function finishLoop(
|
|
296
|
-
|
|
299
|
+
hostCtx: WorkflowHostContext,
|
|
297
300
|
e: LoopEntry,
|
|
298
301
|
cursor: LoopCursor,
|
|
299
302
|
run: RunContext,
|
|
@@ -301,14 +304,14 @@ async function finishLoop(
|
|
|
301
304
|
): Promise<void> {
|
|
302
305
|
projectResult(e.loop, e.entryPair, cursor, run.state);
|
|
303
306
|
if (cursor.ranThisInvocation === 0 && cursor.accumulated.length === 0 && e.loop.kind === "iterate") {
|
|
304
|
-
|
|
307
|
+
hostCtx.ui.notify(MSG_LOOP_ZERO_UNITS(e.skill), "warning");
|
|
305
308
|
}
|
|
306
|
-
await deps.advanceAfter(
|
|
309
|
+
await deps.advanceAfter(hostCtx, e.name, e.stageIdx, run);
|
|
307
310
|
}
|
|
308
311
|
|
|
309
312
|
/** Cap trip: "halt" → terminal failure; "advance" → durable telemetry + event + projected advance. */
|
|
310
313
|
async function hitCap(
|
|
311
|
-
|
|
314
|
+
hostCtx: WorkflowHostContext,
|
|
312
315
|
e: LoopEntry,
|
|
313
316
|
cursor: LoopCursor,
|
|
314
317
|
count: number,
|
|
@@ -316,15 +319,15 @@ async function hitCap(
|
|
|
316
319
|
run: RunContext,
|
|
317
320
|
deps: LoopDeps,
|
|
318
321
|
): Promise<void> {
|
|
319
|
-
if (e.loop.onCap === "halt") return deps.haltLoop(
|
|
322
|
+
if (e.loop.onCap === "halt") return deps.haltLoop(hostCtx, run, e, count, cap);
|
|
320
323
|
appendLoopCap(run.cwd, run.runId, { type: "loop-cap", stage: e.name, count, max: cap, ts: nowIso() });
|
|
321
|
-
|
|
324
|
+
hostCtx.ui.notify(MSG_LOOP_CAP_ADVANCE(e.skill, cap), "warning");
|
|
322
325
|
await run.lifecycle.fire(
|
|
323
|
-
|
|
326
|
+
hostCtx,
|
|
324
327
|
"onLoopCap",
|
|
325
328
|
skillStageRef(e.name, e.stageIdx + 1, e.skill),
|
|
326
329
|
{ kind: e.loop.kind, count, max: cap, policy: "advance" as const },
|
|
327
330
|
lifecycleCtxFor(run),
|
|
328
331
|
);
|
|
329
|
-
return finishLoop(
|
|
332
|
+
return finishLoop(hostCtx, e, cursor, run, deps);
|
|
330
333
|
}
|