@juicesharp/rpiv-workflow 2.2.0 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +134 -91
- package/loop-parallel.ts +15 -15
- package/loop-waves.ts +3 -3
- package/loop.ts +54 -35
- 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 +66 -26
- package/runner/errors.ts +1 -1
- package/runner/failure.ts +22 -23
- package/runner/preflight.ts +20 -20
- 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 +198 -133
- package/runner/run-stage.ts +111 -88
- package/runner/runner.ts +4 -4
- package/runner/script-stage.ts +17 -17
- package/sessions/bash-strikes.ts +9 -9
- package/sessions/extraction.ts +135 -77
- package/sessions/halt-routing.ts +14 -14
- package/sessions/index.ts +1 -1
- package/sessions/reattach.ts +10 -10
- package/sessions/sessions.ts +93 -53
- 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/validate/stage-rules.ts +42 -17
package/loop-kinds.ts
CHANGED
|
@@ -36,7 +36,7 @@ import { isPanel, type Judge, type PanelJudge, panelMembers, resolveJudgePrompt
|
|
|
36
36
|
import { MSG_LOOP_CURSOR_CORRUPT } from "./messages.js";
|
|
37
37
|
import { finalizeOutput, isFailedOutput, type Output, type OutputMeta } from "./output.js";
|
|
38
38
|
import { invariantPreflight } from "./stage-errors.js";
|
|
39
|
-
import type { RunContext, RunState,
|
|
39
|
+
import type { RunContext, RunState, StageSessionContext, UnitRef, WorkflowHostContext } from "./types.js";
|
|
40
40
|
|
|
41
41
|
// ---------------------------------------------------------------------------
|
|
42
42
|
// Cursor + entry vocabulary (kind-agnostic, shared live + fold)
|
|
@@ -96,21 +96,21 @@ export function buildLoopEntry(
|
|
|
96
96
|
*/
|
|
97
97
|
export interface LoopDeps {
|
|
98
98
|
/** Dispatch one unit through the standard stage-session path. */
|
|
99
|
-
|
|
99
|
+
executeStageSession: (ctx: WorkflowHostContext, s: StageSessionContext) => Promise<void>;
|
|
100
100
|
/**
|
|
101
101
|
* Resume the chain after the loop finishes — receives the loop node's REAL
|
|
102
102
|
* name. `Promise<unknown>` so the walk's `ChainOutcome`-returning composed
|
|
103
103
|
* advance plugs in directly (the driver only awaits settlement).
|
|
104
104
|
*/
|
|
105
105
|
advanceAfter: (
|
|
106
|
-
|
|
106
|
+
hostCtx: WorkflowHostContext,
|
|
107
107
|
completedName: string,
|
|
108
108
|
completedIdx: number,
|
|
109
109
|
run: RunContext,
|
|
110
110
|
) => Promise<unknown>;
|
|
111
111
|
/** Re-capture the outcome's pre-stage snapshot per unit (ctx + stage name for the fail-soft warning). */
|
|
112
112
|
captureSnapshot: (
|
|
113
|
-
|
|
113
|
+
hostCtx: WorkflowHostContext,
|
|
114
114
|
stageName: string,
|
|
115
115
|
def: StageDef,
|
|
116
116
|
idx: number,
|
|
@@ -118,7 +118,7 @@ export interface LoopDeps {
|
|
|
118
118
|
) => Promise<unknown>;
|
|
119
119
|
/** Record the terminal failure when `onCap: "halt"` trips — verify-worded for verify stages. */
|
|
120
120
|
haltLoop: (
|
|
121
|
-
|
|
121
|
+
hostCtx: WorkflowHostContext,
|
|
122
122
|
run: RunContext,
|
|
123
123
|
e: Pick<LoopEntry, "name" | "def">,
|
|
124
124
|
count: number,
|
|
@@ -126,14 +126,14 @@ export interface LoopDeps {
|
|
|
126
126
|
) => Promise<void>;
|
|
127
127
|
/** Record a mid-flight run abort at the loop seam (FAIL_WORKFLOW_ABORTED).
|
|
128
128
|
* Keeps the drivers free of engine imports; wired to `recordAbortedAtSeam`. */
|
|
129
|
-
recordAborted: (
|
|
129
|
+
recordAborted: (hostCtx: WorkflowHostContext, name: string, run: RunContext) => Promise<void>;
|
|
130
130
|
/** Funnel an UNEXPECTED worker rejection (not a workflow halt) to a
|
|
131
131
|
* terminal-failure row + onStageError, terminating state WITHOUT re-throwing,
|
|
132
132
|
* so entry() resolves and onWorkflowEnd still fires. Wraps recordEntryThrow.
|
|
133
133
|
* The unit's identity (`unit` + its `skill`) lands as STRUCTURED row fields —
|
|
134
134
|
* the row's `stage` stays the parent graph identity. */
|
|
135
135
|
recordWorkerThrow: (
|
|
136
|
-
|
|
136
|
+
hostCtx: WorkflowHostContext,
|
|
137
137
|
unit: UnitRef,
|
|
138
138
|
skill: string,
|
|
139
139
|
run: RunContext,
|
|
@@ -353,7 +353,7 @@ export type NextStep =
|
|
|
353
353
|
* key to `(index, memberIndex)` for assess panels — within one round all member
|
|
354
354
|
* rows share `index`, so the member dimension is what tells them apart.
|
|
355
355
|
*/
|
|
356
|
-
export interface
|
|
356
|
+
export interface GenerationGuardContext {
|
|
357
357
|
loop: LoopDef;
|
|
358
358
|
entryArtifact: Artifact | undefined;
|
|
359
359
|
cursor: LoopCursor;
|
|
@@ -414,7 +414,7 @@ export interface SequentialStrategy extends LoopKindStrategy {
|
|
|
414
414
|
* (role, unitIndex) arithmetic matched. Returns false on drift. May invoke
|
|
415
415
|
* user fns — a throw becomes drift in the caller (`guarded`).
|
|
416
416
|
*/
|
|
417
|
-
guardExpectation(gen:
|
|
417
|
+
guardExpectation(gen: GenerationGuardContext, row: UnitRowFacts, cwd: string, state: RunState): Promise<boolean>;
|
|
418
418
|
/**
|
|
419
419
|
* Resume re-entry pending-work probe — never dispatches; gates the
|
|
420
420
|
* announce only. The iterate arm re-pulls `next()` at the cursor (the
|
|
@@ -475,7 +475,7 @@ const shouldCollectAll = (loop: LoopDef): boolean => loop.kind === "fanout" && !
|
|
|
475
475
|
* fan-out index (the same value `unit.index` carries). */
|
|
476
476
|
const laneIndexFor = (loop: LoopDef, index: number): number | undefined => (loop.kind === "fanout" ? index : undefined);
|
|
477
477
|
|
|
478
|
-
/** Factored unit
|
|
478
|
+
/** Factored unit StageSessionContext — shared by the sequential `dispatchUnit` (loop.ts)
|
|
479
479
|
* and the parallel `dispatchUnitDetached` (loop-parallel.ts); differs only in
|
|
480
480
|
* `onSuccess` and the threaded `signal`. Populates the per-child execution
|
|
481
481
|
* controls (lane/model/signal). The sequential path passes `run.signal`
|
|
@@ -488,8 +488,8 @@ export function buildUnitSession(
|
|
|
488
488
|
run: RunContext,
|
|
489
489
|
snapshot: unknown,
|
|
490
490
|
signal: AbortSignal | undefined,
|
|
491
|
-
onSuccess:
|
|
492
|
-
):
|
|
491
|
+
onSuccess: StageSessionContext["onSuccess"],
|
|
492
|
+
): StageSessionContext {
|
|
493
493
|
return {
|
|
494
494
|
cwd: run.cwd,
|
|
495
495
|
runId: run.runId,
|
|
@@ -607,85 +607,8 @@ const assessStrategy: SequentialStrategy = {
|
|
|
607
607
|
// carries no marker, so live and resume can't disagree about flavoring.
|
|
608
608
|
async pull(e, cursor, cap, run) {
|
|
609
609
|
const loop = e.loop as AssessLoop;
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
const lp = lastProduceOf(cursor, e.name); // a judge step always follows a completed produce
|
|
613
|
-
// Expand the judge slot to its members and dispatch the member the
|
|
614
|
-
// panel sub-state points at. A single judge is the panel of one
|
|
615
|
-
// (`memberIndex` undefined, member 0 the only member), so its transition
|
|
616
|
-
// is byte-identical to today; a panel walks member 0..N-1 across
|
|
617
|
-
// successive judge-phase pulls, `advanceCursor` bumping
|
|
618
|
-
// `cursor.panel.memberIndex` between them.
|
|
619
|
-
const memberIndex = isPanel(loop.judge) ? (cursor.panel?.memberIndex ?? 0) : undefined;
|
|
620
|
-
const judge = panelMembers(loop.judge)[memberIndex ?? 0]!;
|
|
621
|
-
const judgeSkill = judge.skill ?? (isVerify ? `${e.name}-verify` : `${e.name}-judge`);
|
|
622
|
-
let prompt: string;
|
|
623
|
-
if (judge.skill !== undefined) {
|
|
624
|
-
// produces-validation guarantees the producer emitted an artifact —
|
|
625
|
-
// a miss is the same corrupted-cursor class as a missing produce.
|
|
626
|
-
if (!lp.artifact) {
|
|
627
|
-
const msg = MSG_LOOP_CURSOR_CORRUPT(e.name, "judge skill dispatch found no produced artifact");
|
|
628
|
-
throw invariantPreflight(e.name, msg);
|
|
629
|
-
}
|
|
630
|
-
prompt = `/skill:${judge.skill} ${handleToString(lp.artifact.handle)}`;
|
|
631
|
-
} else {
|
|
632
|
-
prompt = await resolveJudgePrompt(judge.prompt!, {
|
|
633
|
-
cwd: run.cwd,
|
|
634
|
-
output: lp.output,
|
|
635
|
-
entryArtifact: e.entryArtifact,
|
|
636
|
-
state: run.state,
|
|
637
|
-
round: cursor.index,
|
|
638
|
-
});
|
|
639
|
-
}
|
|
640
|
-
const label = judgeUnitLabel(cursor.index, isVerify, memberIndex);
|
|
641
|
-
return {
|
|
642
|
-
kind: "unit",
|
|
643
|
-
role: isVerify ? "verify" : "judge",
|
|
644
|
-
tag: label,
|
|
645
|
-
// Panel member rows carry an `id` so they are identity-bearing for the
|
|
646
|
-
// resume join (`unitId` = the `#{memberIndex}` tag); a single judge keeps
|
|
647
|
-
// identity `(role, round)` with no `id`, exactly as before.
|
|
648
|
-
id: memberIndex === undefined ? undefined : label,
|
|
649
|
-
label,
|
|
650
|
-
skill: judgeSkill,
|
|
651
|
-
prompt,
|
|
652
|
-
def: judgeStageDef(judge),
|
|
653
|
-
};
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
// produce — done wins over cap (one code path for live + resume fast-advance)
|
|
657
|
-
if (cursor.lastVerdict !== undefined && loop.done(cursor.lastVerdict)) return { kind: "complete" };
|
|
658
|
-
if (cursor.index >= cap) return { kind: "cap", count: cursor.index };
|
|
659
|
-
// Producer dispatch is skill XOR prompt (the judge arm's posture). For
|
|
660
|
-
// prompt dispatch the message is sent raw: round 0 resolves the stage's own
|
|
661
|
-
// `prompt` at dispatch time (re-resolved on resume — the PromptFn joins the
|
|
662
|
-
// loop determinism contract), and retry rounds send feedForward's output as
|
|
663
|
-
// the COMPLETE message (there is no skill to prefix an arg onto).
|
|
664
|
-
const isPrompt = e.def.prompt !== undefined;
|
|
665
|
-
// `lastVerdict` is only set by a completed judge, which also bumps `index` —
|
|
666
|
-
// so `index - 1 ≥ 0` whenever feedForward runs (round 0 takes entryArgs).
|
|
667
|
-
const arg =
|
|
668
|
-
cursor.lastVerdict !== undefined
|
|
669
|
-
? loop.feedForward({
|
|
670
|
-
cwd: run.cwd,
|
|
671
|
-
output: lastProduceOf(cursor, e.name).output,
|
|
672
|
-
verdict: cursor.lastVerdict,
|
|
673
|
-
round: cursor.index - 1,
|
|
674
|
-
state: run.state,
|
|
675
|
-
})
|
|
676
|
-
: isPrompt
|
|
677
|
-
? await resolveStagePrompt(e.def.prompt!, run.cwd, run.state)
|
|
678
|
-
: e.entryArgs;
|
|
679
|
-
const label = isVerify ? `a${cursor.index}·attempt` : `r${cursor.index}·produce`;
|
|
680
|
-
return {
|
|
681
|
-
kind: "unit",
|
|
682
|
-
role: "produce",
|
|
683
|
-
tag: label,
|
|
684
|
-
label,
|
|
685
|
-
skill: e.skill,
|
|
686
|
-
prompt: isPrompt ? arg : `/skill:${e.skill} ${arg}`,
|
|
687
|
-
def: e.def,
|
|
688
|
-
};
|
|
610
|
+
if (cursor.phase === "judge") return dispatchPanelMember(loop, cursor, e, run);
|
|
611
|
+
return dispatchProduce(loop, cursor, e, cap, run);
|
|
689
612
|
},
|
|
690
613
|
// The kind-agnostic (role, unitIndex) arithmetic already matched in the
|
|
691
614
|
// caller. Full-check extras:
|
|
@@ -720,6 +643,126 @@ const assessStrategy: SequentialStrategy = {
|
|
|
720
643
|
},
|
|
721
644
|
};
|
|
722
645
|
|
|
646
|
+
/**
|
|
647
|
+
* The produce phase's termination check — a set `lastVerdict` that the loop's
|
|
648
|
+
* `done` predicate accepts. Named so the done-wins-over-cap ordering in
|
|
649
|
+
* `dispatchProduce` reads as intent.
|
|
650
|
+
*/
|
|
651
|
+
const isDone = (loop: AssessLoop, cursor: LoopCursor): boolean =>
|
|
652
|
+
cursor.lastVerdict !== undefined && loop.done(cursor.lastVerdict);
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* Judge/verify phase dispatch — the panel-member arm of `assessStrategy.pull`.
|
|
656
|
+
* Expand the judge slot to its members and dispatch the member the panel
|
|
657
|
+
* sub-state points at. A single judge is the panel of one (`memberIndex`
|
|
658
|
+
* undefined, member 0 the only member), so its transition is byte-identical to
|
|
659
|
+
* today; a panel walks member 0..N-1 across successive judge-phase pulls,
|
|
660
|
+
* `advanceCursor` bumping `cursor.panel.memberIndex` between them.
|
|
661
|
+
*/
|
|
662
|
+
async function dispatchPanelMember(
|
|
663
|
+
loop: AssessLoop,
|
|
664
|
+
cursor: LoopCursor,
|
|
665
|
+
e: LoopEntry,
|
|
666
|
+
run: RunContext,
|
|
667
|
+
): Promise<NextStep> {
|
|
668
|
+
const isVerify = e.def.verify !== undefined;
|
|
669
|
+
const lp = lastProduceOf(cursor, e.name); // a judge step always follows a completed produce
|
|
670
|
+
const memberIndex = isPanel(loop.judge) ? (cursor.panel?.memberIndex ?? 0) : undefined;
|
|
671
|
+
const judge = panelMembers(loop.judge)[memberIndex ?? 0]!;
|
|
672
|
+
const judgeSkill = judge.skill ?? (isVerify ? `${e.name}-verify` : `${e.name}-judge`);
|
|
673
|
+
let prompt: string;
|
|
674
|
+
if (judge.skill !== undefined) {
|
|
675
|
+
// produces-validation guarantees the producer emitted an artifact —
|
|
676
|
+
// a miss is the same corrupted-cursor class as a missing produce.
|
|
677
|
+
if (!lp.artifact) {
|
|
678
|
+
const msg = MSG_LOOP_CURSOR_CORRUPT(e.name, "judge skill dispatch found no produced artifact");
|
|
679
|
+
throw invariantPreflight(e.name, msg);
|
|
680
|
+
}
|
|
681
|
+
prompt = `/skill:${judge.skill} ${handleToString(lp.artifact.handle)}`;
|
|
682
|
+
} else {
|
|
683
|
+
prompt = await resolveJudgePrompt(judge.prompt!, {
|
|
684
|
+
cwd: run.cwd,
|
|
685
|
+
output: lp.output,
|
|
686
|
+
entryArtifact: e.entryArtifact,
|
|
687
|
+
state: run.state,
|
|
688
|
+
round: cursor.index,
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
const label = judgeUnitLabel(cursor.index, isVerify, memberIndex);
|
|
692
|
+
return {
|
|
693
|
+
kind: "unit",
|
|
694
|
+
role: isVerify ? "verify" : "judge",
|
|
695
|
+
tag: label,
|
|
696
|
+
// Panel member rows carry an `id` so they are identity-bearing for the
|
|
697
|
+
// resume join (`unitId` = the `#{memberIndex}` tag); a single judge keeps
|
|
698
|
+
// identity `(role, round)` with no `id`, exactly as before.
|
|
699
|
+
id: memberIndex === undefined ? undefined : label,
|
|
700
|
+
label,
|
|
701
|
+
skill: judgeSkill,
|
|
702
|
+
prompt,
|
|
703
|
+
def: judgeStageDef(judge),
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Produce phase dispatch — the producer arm of `assessStrategy.pull`. Done wins
|
|
709
|
+
* over cap (one code path for live + resume fast-advance). Producer dispatch is
|
|
710
|
+
* skill XOR prompt (the judge arm's posture). For prompt dispatch the message is
|
|
711
|
+
* sent raw: round 0 resolves the stage's own `prompt` at dispatch time
|
|
712
|
+
* (re-resolved on resume — the PromptFn joins the loop determinism contract),
|
|
713
|
+
* and retry rounds send feedForward's output as the COMPLETE message (there is no
|
|
714
|
+
* skill to prefix an arg onto).
|
|
715
|
+
*/
|
|
716
|
+
async function dispatchProduce(
|
|
717
|
+
loop: AssessLoop,
|
|
718
|
+
cursor: LoopCursor,
|
|
719
|
+
e: LoopEntry,
|
|
720
|
+
cap: number,
|
|
721
|
+
run: RunContext,
|
|
722
|
+
): Promise<NextStep> {
|
|
723
|
+
if (isDone(loop, cursor)) return { kind: "complete" };
|
|
724
|
+
if (cursor.index >= cap) return { kind: "cap", count: cursor.index };
|
|
725
|
+
const isPrompt = e.def.prompt !== undefined;
|
|
726
|
+
const arg = await resolveProduceArg(loop, cursor, e, run);
|
|
727
|
+
const isVerify = e.def.verify !== undefined;
|
|
728
|
+
const label = isVerify ? `a${cursor.index}·attempt` : `r${cursor.index}·produce`;
|
|
729
|
+
return {
|
|
730
|
+
kind: "unit",
|
|
731
|
+
role: "produce",
|
|
732
|
+
tag: label,
|
|
733
|
+
label,
|
|
734
|
+
skill: e.skill,
|
|
735
|
+
prompt: isPrompt ? arg : `/skill:${e.skill} ${arg}`,
|
|
736
|
+
def: e.def,
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
/**
|
|
741
|
+
* The producer's next-round arg — the three-way verdict/retry/init resolution:
|
|
742
|
+
* a retry round (a completed judge set `lastVerdict`) runs `feedForward`; round 0
|
|
743
|
+
* of a prompt stage resolves the stage's own `prompt`; round 0 of a skill stage
|
|
744
|
+
* takes the loop's entryArgs. `lastVerdict` is only set by a completed judge,
|
|
745
|
+
* which also bumps `index` — so `index - 1 ≥ 0` whenever feedForward runs
|
|
746
|
+
* (round 0 takes entryArgs). The async wrap preserves the await asymmetry:
|
|
747
|
+
* `feedForward` is a sync `(ctx) => string` (its bare `return` is wrapped by
|
|
748
|
+
* this async helper into a resolved promise), while `resolveStagePrompt` is
|
|
749
|
+
* `Promise<string>` and is awaited.
|
|
750
|
+
*/
|
|
751
|
+
async function resolveProduceArg(loop: AssessLoop, cursor: LoopCursor, e: LoopEntry, run: RunContext): Promise<string> {
|
|
752
|
+
const isPrompt = e.def.prompt !== undefined;
|
|
753
|
+
return cursor.lastVerdict !== undefined
|
|
754
|
+
? loop.feedForward({
|
|
755
|
+
cwd: run.cwd,
|
|
756
|
+
output: lastProduceOf(cursor, e.name).output,
|
|
757
|
+
verdict: cursor.lastVerdict,
|
|
758
|
+
round: cursor.index - 1,
|
|
759
|
+
state: run.state,
|
|
760
|
+
})
|
|
761
|
+
: isPrompt
|
|
762
|
+
? await resolveStagePrompt(e.def.prompt!, run.cwd, run.state)
|
|
763
|
+
: e.entryArgs;
|
|
764
|
+
}
|
|
765
|
+
|
|
723
766
|
/**
|
|
724
767
|
* One strategy per kind — the `Record` shape turns "added a `LoopDef` arm,
|
|
725
768
|
* forgot the strategy" into a compile error. Keyed value types are preserved
|
package/loop-parallel.ts
CHANGED
|
@@ -78,7 +78,7 @@ const fanoutUnitRef = (e: LoopEntry, i: number): UnitRef => {
|
|
|
78
78
|
* cycle-throw from `computeWaveLevels` can't leak it.
|
|
79
79
|
*/
|
|
80
80
|
export async function runFanoutGeneration(
|
|
81
|
-
|
|
81
|
+
hostCtx: WorkflowHostContext,
|
|
82
82
|
e: LoopEntry,
|
|
83
83
|
cursor: LoopCursor,
|
|
84
84
|
run: RunContext,
|
|
@@ -111,14 +111,14 @@ export async function runFanoutGeneration(
|
|
|
111
111
|
}
|
|
112
112
|
const detach = () => run.signal?.removeEventListener("abort", onRunAbort);
|
|
113
113
|
|
|
114
|
-
await dispatchGeneration(
|
|
114
|
+
await dispatchGeneration(hostCtx, e, cursor, run, deps, order, genAbort, idToIndex);
|
|
115
115
|
detach(); // generation settled — drop the run-lifetime listener BEFORE the tail
|
|
116
116
|
|
|
117
117
|
// The same two gates the wave loop ran between levels, now once at the end: a
|
|
118
118
|
// fail-fast halt already terminated state inside the worker and aborted its
|
|
119
119
|
// siblings; a run abort drained the semaphore and rejected every later acquire.
|
|
120
120
|
if (run.state.termination.status !== "running") return;
|
|
121
|
-
if (run.signal?.aborted) return deps.recordAborted(
|
|
121
|
+
if (run.signal?.aborted) return deps.recordAborted(hostCtx, e.name, run); // mid-flight abort → FAIL_WORKFLOW_ABORTED
|
|
122
122
|
return finalTail();
|
|
123
123
|
}
|
|
124
124
|
|
|
@@ -129,7 +129,7 @@ export async function runFanoutGeneration(
|
|
|
129
129
|
* `recordWorkerThrow`; allSettled guarantees every unit settled.
|
|
130
130
|
*/
|
|
131
131
|
async function dispatchGeneration(
|
|
132
|
-
|
|
132
|
+
hostCtx: WorkflowHostContext,
|
|
133
133
|
e: LoopEntry,
|
|
134
134
|
cursor: LoopCursor,
|
|
135
135
|
run: RunContext,
|
|
@@ -143,14 +143,14 @@ async function dispatchGeneration(
|
|
|
143
143
|
// `concurrency: 1`); floored at 1, never raised above the host cap.
|
|
144
144
|
const loopCap = e.loop.kind === "fanout" ? e.loop.concurrency : undefined;
|
|
145
145
|
const sem = new Semaphore(
|
|
146
|
-
Math.max(1, Math.min(loopCap ??
|
|
146
|
+
Math.max(1, Math.min(loopCap ?? hostCtx.maxConcurrency, hostCtx.maxConcurrency)),
|
|
147
147
|
genAbort.signal,
|
|
148
148
|
); // drains queued units on either abort
|
|
149
149
|
|
|
150
150
|
// One readiness latch per DISPATCHED unit, index-addressed. A dep with no latch is
|
|
151
151
|
// already settled: either it is outside `ops` (a resume whose prior invocation filled
|
|
152
152
|
// the slot) or it is dangling (`computeWaveLevels` treats those as satisfied too —
|
|
153
|
-
// `
|
|
153
|
+
// `ensureUnitDeps` owns the dangling report).
|
|
154
154
|
const latch = new Array<Promise<void> | undefined>(e.units!.length);
|
|
155
155
|
const open = new Array<(() => void) | undefined>(e.units!.length);
|
|
156
156
|
for (const i of ops)
|
|
@@ -173,9 +173,9 @@ async function dispatchGeneration(
|
|
|
173
173
|
// Resolve `--upstream`-style dep-artifact injection from the slots this
|
|
174
174
|
// unit's deps just filled. Empty when the loop sets no flag or no deps.
|
|
175
175
|
const suffix = depArtifactSuffix(e, cursor, i, idToIndex);
|
|
176
|
-
const out = await sem.run(() => dispatchUnitDetached(
|
|
176
|
+
const out = await sem.run(() => dispatchUnitDetached(hostCtx, e, i, run, deps, genAbort.signal, suffix));
|
|
177
177
|
// ONE synchronous block, and its order is load-bearing. (1) A fail-fast
|
|
178
|
-
// unit's worker terminated state via
|
|
178
|
+
// unit's worker terminated state via recordFatalFailure — fire genAbort
|
|
179
179
|
// so in-flight siblings get session.abort()'d NOW and no dependent released
|
|
180
180
|
// below can still slip into the semaphore. (2) Fold BEFORE the latch opens:
|
|
181
181
|
// a dependent's `depArtifactSuffix` reads `cursor.slots`, so an early
|
|
@@ -187,7 +187,7 @@ async function dispatchGeneration(
|
|
|
187
187
|
foldFanoutCompletion(run.state, cursor, e.def, e.name, i, e.units!.length, out);
|
|
188
188
|
} catch (reason) {
|
|
189
189
|
// aborted / never-started → unfilled slot (resume re-dispatches)
|
|
190
|
-
if (!isAbortError(reason)) await deps.recordWorkerThrow(
|
|
190
|
+
if (!isAbortError(reason)) await deps.recordWorkerThrow(hostCtx, fanoutUnitRef(e, i), e.skill, run, reason);
|
|
191
191
|
} finally {
|
|
192
192
|
// Dependents proceed even when this unit failed — `depArtifactSuffix` skips
|
|
193
193
|
// a failed/unfilled slot, so they design blind for it rather than stalling.
|
|
@@ -202,7 +202,7 @@ async function dispatchGeneration(
|
|
|
202
202
|
* dep whose slot is filled with a NON-FAILED output. A failed/sentinel or still-unfilled
|
|
203
203
|
* dep slot is SKIPPED (the dependent designs blind for that dep) — so a failed upstream
|
|
204
204
|
* degrades gracefully instead of injecting a broken path; synthesize stays the backstop.
|
|
205
|
-
* Dangling ids never reach here (`
|
|
205
|
+
* Dangling ids never reach here (`ensureUnitDeps` rejected them at the live entry); the
|
|
206
206
|
* `undefined` guard is defensive.
|
|
207
207
|
*/
|
|
208
208
|
function depArtifactSuffix(e: LoopEntry, cursor: LoopCursor, index: number, idToIndex: Map<string, number>): string {
|
|
@@ -228,7 +228,7 @@ function depArtifactSuffix(e: LoopEntry, cursor: LoopCursor, index: number, idTo
|
|
|
228
228
|
* from the pre-wave dispatcher: collect-all unit failure → sentinel; fail-fast halt →
|
|
229
229
|
* placement sentinel + graceful return; ABORT → throws WorkflowAbortError → unfilled slot.) */
|
|
230
230
|
async function dispatchUnitDetached(
|
|
231
|
-
|
|
231
|
+
hostCtx: WorkflowHostContext,
|
|
232
232
|
e: LoopEntry,
|
|
233
233
|
index: number,
|
|
234
234
|
run: RunContext,
|
|
@@ -239,16 +239,16 @@ async function dispatchUnitDetached(
|
|
|
239
239
|
if (signal?.aborted) throw new WorkflowAbortError(); // never open a child after abort; isAbortError → unfilled slot
|
|
240
240
|
const u = fanoutUnitAt(e, index, promptSuffix);
|
|
241
241
|
await run.lifecycle.fire(
|
|
242
|
-
|
|
242
|
+
hostCtx,
|
|
243
243
|
"onUnitStart",
|
|
244
244
|
skillStageRef(e.name, e.stageIdx + 1, u.skill),
|
|
245
245
|
{ role: u.role, index, unitId: u.id, label: u.label, skill: u.skill },
|
|
246
246
|
lifecycleCtxFor(run),
|
|
247
247
|
);
|
|
248
|
-
const snapshot = await deps.captureSnapshot(
|
|
248
|
+
const snapshot = await deps.captureSnapshot(hostCtx, e.name, u.def, e.stageIdx, run);
|
|
249
249
|
let captured: Output | undefined;
|
|
250
|
-
await deps.
|
|
251
|
-
|
|
250
|
+
await deps.executeStageSession(
|
|
251
|
+
hostCtx,
|
|
252
252
|
buildUnitSession(e, u, index, run, snapshot, signal, (_child, output) => {
|
|
253
253
|
captured = output;
|
|
254
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 ?? []) {
|