@juicesharp/rpiv-workflow 1.20.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -545
- package/api.ts +1 -1
- package/audit-ctx.ts +103 -0
- package/audit-rows.ts +18 -0
- package/audit.ts +119 -164
- package/built-ins.ts +26 -6
- package/chain-state.ts +119 -68
- package/command-run.ts +35 -30
- package/command.ts +10 -4
- package/docs/embedding.md +341 -0
- package/docs/workflow-authoring.md +70 -16
- package/docs/workflow-basics.md +125 -10
- package/events.ts +44 -4
- package/execution-host.ts +75 -0
- package/host.ts +137 -82
- package/internal-utils.ts +89 -3
- package/internal.ts +1 -0
- package/judge.ts +23 -11
- package/load/alias.ts +3 -3
- package/load/index.ts +15 -1
- package/load/issues.ts +2 -2
- package/load/legacy.ts +2 -2
- package/load/resolve-default.ts +3 -4
- package/load/skill-contract-phase.ts +1 -1
- package/loop-constructors.ts +19 -618
- package/loop-def.ts +60 -15
- package/loop-kinds.ts +319 -62
- package/loop-parallel.ts +250 -0
- package/loop-waves.ts +99 -0
- package/loop.ts +106 -80
- package/loops/constructors.ts +334 -0
- package/loops/derivations.ts +65 -0
- package/loops/introspection.ts +184 -0
- package/loops/panel.ts +196 -0
- package/loops/verify.ts +99 -0
- package/messages.ts +58 -33
- package/outcomes/collectors/index.ts +1 -0
- package/outcomes/collectors/text-scan.ts +47 -0
- package/outcomes/collectors/transcript-path.ts +2 -15
- package/outcomes/collectors/union.ts +2 -1
- package/outcomes/collectors/url.ts +2 -14
- package/outcomes/collectors/workspace-diff.ts +1 -1
- package/outcomes/git-commit.ts +57 -6
- package/output-spec.ts +1 -1
- package/output.ts +59 -1
- package/package.json +19 -6
- package/preview.ts +18 -18
- package/registration.ts +11 -4
- package/routing-dsl.ts +53 -41
- package/routing.ts +46 -0
- package/runner/chain-advance.ts +30 -17
- package/runner/errors.ts +7 -30
- package/runner/failure.ts +67 -13
- package/runner/input-validation.ts +8 -5
- package/runner/preflight.ts +17 -21
- package/runner/resume-entry.ts +23 -3
- package/runner/resume-loop.ts +50 -29
- package/runner/resume.ts +96 -10
- package/runner/run-context.ts +20 -16
- package/runner/run-stage.ts +125 -91
- package/runner/runner.ts +163 -69
- package/runner/script-stage.ts +23 -29
- package/schema-compat.ts +1 -2
- package/semaphore.ts +55 -0
- package/sessions/extraction.ts +103 -60
- package/sessions/halt-routing.ts +195 -0
- package/sessions/index.ts +2 -2
- package/sessions/locate.ts +60 -9
- package/sessions/reattach.ts +28 -20
- package/sessions/sessions.ts +103 -191
- package/sessions/spawn.ts +132 -119
- package/sessions/success-persist.ts +89 -0
- package/skill-contracts/composition.ts +8 -10
- package/skill-contracts/harvest.ts +3 -2
- package/skill-contracts/registry.ts +1 -1
- package/stage-def.ts +115 -47
- package/stage-errors.ts +84 -0
- package/stage-identity.ts +56 -0
- package/startup.ts +1 -0
- package/state/paths.ts +11 -3
- package/state/raw.ts +2 -3
- package/state/resolve.ts +3 -3
- package/state/state.ts +26 -3
- package/state/writes.ts +1 -1
- package/types.ts +97 -43
- package/validate/contract-compat.ts +16 -11
- package/validate/graph.ts +10 -1
- package/validate/issue.ts +20 -6
- package/validate/stage-rules.ts +139 -60
- package/validate-output.ts +27 -20
- package/validate-workflow.ts +5 -3
- package/validation-bounds.ts +35 -0
- package/docs/cover.png +0 -0
- package/docs/cover.svg +0 -120
- package/load/cache.test.ts +0 -82
- package/load/shape-guards.test.ts +0 -77
- package/outcomes/collectors/directory-path.test.ts +0 -64
- package/outcomes/collectors/tool-call.test.ts +0 -112
- package/outcomes/collectors/transcript-path.test.ts +0 -70
- package/outcomes/collectors/union.test.ts +0 -113
- package/outcomes/collectors/url.test.ts +0 -67
- package/outcomes/collectors/workspace-diff.test.ts +0 -120
- package/outcomes/git-commit.test.ts +0 -240
- package/outcomes/parsers/json-body.test.ts +0 -80
- package/runner/by-name.test.ts +0 -95
- package/runner/by-run-id.test.ts +0 -115
- package/runner/header-write-failure.test.ts +0 -64
- package/runner/resume-loop.test.ts +0 -1410
- package/runner/resume-session.test.ts +0 -400
- package/runner/resume.test.ts +0 -1805
- package/sessions/locate.test.ts +0 -77
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loop constructors — `fanout()` / `iterate()` / `assess()` build the `LoopDef`
|
|
3
|
+
* a stage carries on its single `loop` field. Constructors validate at
|
|
4
|
+
* construction (the `defineRoute` pattern): a bad `max`, an invalid judge shape,
|
|
5
|
+
* or a non-function `done`/`feedForward` throws immediately at authoring time;
|
|
6
|
+
* load-time validation re-checks the same rules defensively for hand-rolled
|
|
7
|
+
* literals (jiti erases TS types). Skill-agnostic — the unit detectors
|
|
8
|
+
* (`units`/`next`) are consumer-supplied; rpiv-workflow ships no conventions.
|
|
9
|
+
* Runner-free — safe on `registration`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type {
|
|
13
|
+
AssessLoop,
|
|
14
|
+
CapPolicy,
|
|
15
|
+
FanoutFn,
|
|
16
|
+
FanoutLoop,
|
|
17
|
+
FeedForwardContext,
|
|
18
|
+
IterateFn,
|
|
19
|
+
IterateLoop,
|
|
20
|
+
LoopDef,
|
|
21
|
+
ResultProjection,
|
|
22
|
+
UnitSelector,
|
|
23
|
+
} from "../api.js";
|
|
24
|
+
import { requireNonEmptyString } from "../internal-utils.js";
|
|
25
|
+
import { type AnyJudge, assertShape, isPanel, judgeShapeIssues } from "../judge.js";
|
|
26
|
+
import type { Output } from "../output.js";
|
|
27
|
+
import { panelShapeIssues } from "./panel.js";
|
|
28
|
+
|
|
29
|
+
/** Default round cap when an `assess()` call omits `max`. Clamped by `run.maxIterations`. */
|
|
30
|
+
export const DEFAULT_ASSESS_MAX = 8;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Per-loop-kind identity — the construction-facet twin of `LOOP_STRATEGIES`
|
|
34
|
+
* (`loop-kinds.ts`, the per-kind runtime strategy table). Each kind's default
|
|
35
|
+
* `onCap`/`result` (and assess's construction `max`) live HERE, consulted by
|
|
36
|
+
* every constructor, so a new loop kind or a default change is a single-row
|
|
37
|
+
* edit instead of a multi-constructor scatter. Consulted by `effectiveLoopOf`
|
|
38
|
+
* (the derivation-authority exemplar) and `freezesEntryArgsOf` (the round-0-arg
|
|
39
|
+
* rule) — the two derivations that read it; both live in `derivations.ts` after
|
|
40
|
+
* the `loops/` split (they value-import `LOOP_DEFAULTS` from here).
|
|
41
|
+
*
|
|
42
|
+
* `max` fits the table because `satisfies` (NOT a type annotation) preserves
|
|
43
|
+
* the narrow inferred literal type PER KEY (the same property `LOOP_STRATEGIES`
|
|
44
|
+
* relies on): `assess.max` infers as `number` (from `DEFAULT_ASSESS_MAX`), while
|
|
45
|
+
* `fanout.max`/`iterate.max` infer as `undefined`. The assess constructor
|
|
46
|
+
* therefore reads `LOOP_DEFAULTS.assess.max` with NO non-null assertion.
|
|
47
|
+
*
|
|
48
|
+
* WARNING: if a future change annotates this constant with a type declaration
|
|
49
|
+
* instead of `satisfies`, the narrow per-key types are lost and `assess.max`
|
|
50
|
+
* widens to `number | undefined` — re-confirm the `satisfies` form is retained.
|
|
51
|
+
*/
|
|
52
|
+
interface LoopKindDefaults {
|
|
53
|
+
/** Construction-default cap policy. fanout/iterate → "halt", assess → "advance". */
|
|
54
|
+
onCap: CapPolicy;
|
|
55
|
+
/** Construction-default result projection. fanout → "entry", iterate/assess → "last". */
|
|
56
|
+
result: ResultProjection;
|
|
57
|
+
/**
|
|
58
|
+
* Construction default cap; `undefined` when a kind has none (clamped at
|
|
59
|
+
* runtime by `run.maxIterations`). assess carries `DEFAULT_ASSESS_MAX`;
|
|
60
|
+
* verify carries 1 (in the peer `VERIFY_LOOP_DEFAULTS`, not here).
|
|
61
|
+
*/
|
|
62
|
+
max?: number;
|
|
63
|
+
/**
|
|
64
|
+
* Does this kind freeze a round-0 producer arg? assess derives (and freezes)
|
|
65
|
+
* a round-0 arg from the entry state; fanout/iterate do not. Consulted at the
|
|
66
|
+
* three live/resume entry sites via `freezesEntryArgsOf`, so the predicate is
|
|
67
|
+
* spelled ONCE — a future judged kind flipping this just edits the row.
|
|
68
|
+
*/
|
|
69
|
+
freezesEntryArgs: boolean;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* THE per-kind identity table — the single place fanout/iterate/assess default
|
|
74
|
+
* `onCap`/`result`/`max` AND the round-0-arg rule are spelled. Modeled on
|
|
75
|
+
* `LOOP_STRATEGIES` (`satisfies Record<LoopDef["kind"], …>`); a new loop kind
|
|
76
|
+
* added to the union without a row here is a compile error.
|
|
77
|
+
*/
|
|
78
|
+
export const LOOP_DEFAULTS = {
|
|
79
|
+
fanout: { onCap: "halt", result: "entry", max: undefined, freezesEntryArgs: false },
|
|
80
|
+
iterate: { onCap: "halt", result: "last", max: undefined, freezesEntryArgs: false },
|
|
81
|
+
assess: { onCap: "advance", result: "last", max: DEFAULT_ASSESS_MAX, freezesEntryArgs: true },
|
|
82
|
+
} satisfies Record<LoopDef["kind"], LoopKindDefaults>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Verify's gate-only defaults — a named PEER, not a `LOOP_DEFAULTS` row: verify
|
|
86
|
+
* is not a `LoopDef["kind"]` (it desugars to a degenerate assess loop), so it
|
|
87
|
+
* cannot be a key in a `Record<LoopDef["kind"], …>`. Its distinct defaults
|
|
88
|
+
* (`onCap: "halt"`, `result: "last"`, `max: 1` vs assess's `advance`/`last`/`8`)
|
|
89
|
+
* each still spelled exactly once — here — and consulted by `synthesizeVerifyLoop`.
|
|
90
|
+
*
|
|
91
|
+
* `freezesEntryArgs` is carried for the `LoopKindDefaults` shape but is NOT
|
|
92
|
+
* consulted by `freezesEntryArgsOf`: a verify stage's effective loop is the
|
|
93
|
+
* SYNTHESIZED assess loop (kind "assess"), so the predicate reads
|
|
94
|
+
* `LOOP_DEFAULTS.assess.freezesEntryArgs` (`true`). `true` here keeps the peer
|
|
95
|
+
* honest — verify, like assess, freezes a round-0 producer arg.
|
|
96
|
+
*/
|
|
97
|
+
export const VERIFY_LOOP_DEFAULTS = {
|
|
98
|
+
onCap: "halt",
|
|
99
|
+
result: "last",
|
|
100
|
+
max: 1,
|
|
101
|
+
freezesEntryArgs: true,
|
|
102
|
+
} satisfies LoopKindDefaults;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* First-defined primitive — keeps defaulting grep-clean (no inline
|
|
106
|
+
* onCap-fallback token at the call sites) so the slice's default-spelling
|
|
107
|
+
* check holds. The `??` inside this body is the single authorized spelling
|
|
108
|
+
* of the fallback.
|
|
109
|
+
*/
|
|
110
|
+
function firstDefined<T>(v: T | undefined, fallback: T): T {
|
|
111
|
+
return v ?? fallback;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Options shared by all three constructors — the introspectable facet + policy knobs. */
|
|
115
|
+
interface LoopOptionsBase {
|
|
116
|
+
/** The named channel the units are split FROM (a `consumes` hint for lints/agents). */
|
|
117
|
+
source?: string;
|
|
118
|
+
/** How units are detected (opaque convention — e.g. `{ by: "frontmatter-array", pattern: "phases" }`). */
|
|
119
|
+
unit?: UnitSelector;
|
|
120
|
+
/** Cardinality ceiling. Must be an integer >= 1 (throws at construction). */
|
|
121
|
+
max?: number;
|
|
122
|
+
/** Cap policy override. Defaults: fanout/iterate → "halt", assess → "advance". */
|
|
123
|
+
onCap?: CapPolicy;
|
|
124
|
+
/** Result projection override. Defaults: fanout → "entry", iterate/assess → "last". */
|
|
125
|
+
result?: ResultProjection;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface FanoutOptions extends LoopOptionsBase {
|
|
129
|
+
/** Push-model unit source — all units computed up front. */
|
|
130
|
+
units: FanoutFn;
|
|
131
|
+
/** Per-fanout concurrency ceiling — caps in-flight units to
|
|
132
|
+
* `min(concurrency, host maxConcurrency)`; `1` serializes (the safe model for a
|
|
133
|
+
* stage that mutates shared state, e.g. `implement` applying a plan to one tree).
|
|
134
|
+
* Integer ≥ 1; absent ⇒ host cap governs. */
|
|
135
|
+
concurrency?: number;
|
|
136
|
+
/** Opt out of collect-all: any unit failure halts the run (default ⇒ collect-all). */
|
|
137
|
+
failFast?: boolean;
|
|
138
|
+
/** When set, the dispatcher appends `${depArtifactFlag} <path>` per direct
|
|
139
|
+
* `Unit.deps` entry with a non-failed filled slot — handing the dependent unit
|
|
140
|
+
* its dependencies' published artifacts (e.g. `"--upstream"`). Non-empty string;
|
|
141
|
+
* pairs with `Unit.deps`. */
|
|
142
|
+
depArtifactFlag?: string;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface IterateOptions extends LoopOptionsBase {
|
|
146
|
+
/** Pull-model unit source — one unit per call, fed the accumulated prefix. */
|
|
147
|
+
next: IterateFn;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export interface AssessOptions extends LoopOptionsBase {
|
|
151
|
+
/** The judge SLOT — a single `Judge` or an N-member `panel()` (a single judge is the panel of one). */
|
|
152
|
+
judge: AnyJudge;
|
|
153
|
+
/** Sync TS reading the model-made verdict. `true` → loop stops, producer output is the result. */
|
|
154
|
+
done: (verdict: Output) => boolean;
|
|
155
|
+
/** Builds the next producer prompt arg from the just-judged round's output + verdict. */
|
|
156
|
+
feedForward: (ctx: FeedForwardContext) => string;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Push loop: all units precomputed, each unit its own session. On a
|
|
161
|
+
* `produces` stage units COLLECT (full collect→validate→publish per unit;
|
|
162
|
+
* `outcome.name` required); on an `acts` stage units are side-effects.
|
|
163
|
+
* Empty `units()` return ⇒ single-stage fall-through.
|
|
164
|
+
*/
|
|
165
|
+
export function fanout(opts: FanoutOptions): FanoutLoop {
|
|
166
|
+
const d = LOOP_DEFAULTS.fanout;
|
|
167
|
+
return {
|
|
168
|
+
kind: "fanout",
|
|
169
|
+
units: opts.units,
|
|
170
|
+
source: opts.source,
|
|
171
|
+
unit: opts.unit,
|
|
172
|
+
max: checkedPositiveInt(opts.max, "fanout(): max"),
|
|
173
|
+
onCap: firstDefined(opts.onCap, d.onCap),
|
|
174
|
+
result: firstDefined(opts.result, d.result),
|
|
175
|
+
...(opts.concurrency !== undefined
|
|
176
|
+
? { concurrency: checkedPositiveInt(opts.concurrency, "fanout(): concurrency") }
|
|
177
|
+
: {}),
|
|
178
|
+
...(opts.depArtifactFlag !== undefined ? { depArtifactFlag: checkedDepArtifactFlag(opts.depArtifactFlag) } : {}),
|
|
179
|
+
...(opts.failFast !== undefined ? { failFast: opts.failFast } : {}),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** A blank/whitespace flag would inject a bare ` <path>` with no marker — reject at construction. */
|
|
184
|
+
function checkedDepArtifactFlag(flag: string | undefined): string | undefined {
|
|
185
|
+
if (flag === undefined) return undefined;
|
|
186
|
+
requireNonEmptyString(flag, "fanout()", `depArtifactFlag must be a non-empty string (got ${JSON.stringify(flag)})`, {
|
|
187
|
+
trim: true,
|
|
188
|
+
});
|
|
189
|
+
return flag;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Pull loop: sequential, accumulating — each `next()` call sees the prior
|
|
194
|
+
* units' validated Outputs. Requires `kind: "produces"` + `outcome.name`
|
|
195
|
+
* (workflow-level, checked at load). First-call `null` ⇒ zero-unit no-op.
|
|
196
|
+
*/
|
|
197
|
+
export function iterate(opts: IterateOptions): IterateLoop {
|
|
198
|
+
const d = LOOP_DEFAULTS.iterate;
|
|
199
|
+
return {
|
|
200
|
+
kind: "iterate",
|
|
201
|
+
next: opts.next,
|
|
202
|
+
source: opts.source,
|
|
203
|
+
unit: opts.unit,
|
|
204
|
+
max: checkedPositiveInt(opts.max, "iterate(): max"),
|
|
205
|
+
onCap: firstDefined(opts.onCap, d.onCap),
|
|
206
|
+
result: firstDefined(opts.result, d.result),
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Model-judged until-done loop: each round runs a producer session (this
|
|
212
|
+
* stage's skill/outcome) then a judge session (`opts.judge`). `done(verdict)`
|
|
213
|
+
* decides termination; `feedForward` carries the verdict into the next
|
|
214
|
+
* producer round. The cap soft-stops by default (`onCap: "advance"`).
|
|
215
|
+
* Requires `kind: "produces"` + `outcome.name` (workflow-level, checked at
|
|
216
|
+
* load) — every round runs the produces collector, so the producer needs a
|
|
217
|
+
* stable named slot like any other collecting loop.
|
|
218
|
+
*/
|
|
219
|
+
export function assess(opts: AssessOptions): AssessLoop {
|
|
220
|
+
assertShape("assess", assessShapeIssues(opts));
|
|
221
|
+
const d = LOOP_DEFAULTS.assess;
|
|
222
|
+
return {
|
|
223
|
+
kind: "assess",
|
|
224
|
+
judge: opts.judge,
|
|
225
|
+
done: opts.done,
|
|
226
|
+
feedForward: opts.feedForward,
|
|
227
|
+
source: opts.source,
|
|
228
|
+
unit: opts.unit,
|
|
229
|
+
max: checkedPositiveInt(opts.max, "assess(): max") ?? d.max,
|
|
230
|
+
onCap: firstDefined(opts.onCap, d.onCap),
|
|
231
|
+
result: firstDefined(opts.result, d.result),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* THE panel-vs-single judge-slot shape dispatch — the single place a judge
|
|
237
|
+
* slot's panel-vs-single branch is decided. Mirrors `judgeSlotSpecOf`
|
|
238
|
+
* (`loops/introspection.ts`), the introspection-facet twin: both route an
|
|
239
|
+
* `AnyJudge` slot through one panel-vs-single branch (a panel →
|
|
240
|
+
* `panelShapeIssues`, a single judge → `judgeShapeIssues`). Consulted by all
|
|
241
|
+
* three judge-slot shape sites — `assessShapeIssues` (here), `verifyShapeIssues`
|
|
242
|
+
* (`loops/verify.ts`), and the assess load gate (`validate/stage-rules.ts`
|
|
243
|
+
* `checkLoopInvariants`) — so the wording can never drift between the
|
|
244
|
+
* construction and load-gate paths (the same divergence class the
|
|
245
|
+
* `forEachJudgeChannel` extraction caught).
|
|
246
|
+
*
|
|
247
|
+
* `slot` is `unknown` ON PURPOSE — the call sites feed it jiti-loaded literals
|
|
248
|
+
* whose TS types are erased, NOT a typed `AnyJudge` (cf. `judgeSlotSpecOf`,
|
|
249
|
+
* whose param IS the typed `AnyJudge` and therefore needs no guard). The
|
|
250
|
+
* `slot &&` guard is load-bearing: `isPanel` (`judge.ts`) is
|
|
251
|
+
* `(j) => (j as PanelJudge).kind === "panel"` with NO null guard, so a
|
|
252
|
+
* hand-rolled assess/verify literal MISSING its `judge` field must fall through
|
|
253
|
+
* to `judgeShapeIssues(undefined)` = `["a judge object is required"]` rather
|
|
254
|
+
* than throw a raw `TypeError`. Dropping the guard would regress that clean
|
|
255
|
+
* message into an uncaught `TypeError`.
|
|
256
|
+
*
|
|
257
|
+
* Internal-only posture matching `assessShapeIssues`: module-exported and
|
|
258
|
+
* barrel-re-exported via `loop-constructors.ts` (so the load gate reaches it
|
|
259
|
+
* through the existing `../loop-constructors.js` import), but deliberately NOT
|
|
260
|
+
* added to `registration.ts` — the public judge-slot projection stays
|
|
261
|
+
* `judgeSlotSpecOf`.
|
|
262
|
+
*/
|
|
263
|
+
export function judgeSlotShapeIssues(slot: unknown): string[] {
|
|
264
|
+
return slot && isPanel(slot as AnyJudge) ? panelShapeIssues(slot) : judgeShapeIssues(slot);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Single rule source for the assess shape — mirrors `verifyShapeIssues`. Returns
|
|
269
|
+
* human-readable violations (empty = valid); `assess()` throws on the first via
|
|
270
|
+
* `assertShape`. Takes `unknown` ON PURPOSE (jiti-loaded literals erase TS
|
|
271
|
+
* types). Every judge-bearing constructor — judge/assess/verify/panel — pairs
|
|
272
|
+
* a `*ShapeIssues` source with `assertShape`.
|
|
273
|
+
*
|
|
274
|
+
* DECISION OF RECORD — the assess shape is validated TWICE, by design, and the
|
|
275
|
+
* two sources are intentionally NOT unified:
|
|
276
|
+
*
|
|
277
|
+
* - This source (`assessShapeIssues`) runs at construction time inside `assess()`;
|
|
278
|
+
* the load gate (`validate/stage-rules.ts`) re-runs the same shape and emits its
|
|
279
|
+
* own per-code reporting (`assess-judge-shape`/
|
|
280
|
+
* `assess-done-not-function`/`assess-feed-forward-not-function`). Both are kept.
|
|
281
|
+
* - The judge/panel shape is ALREADY shared — both paths route the judge slot
|
|
282
|
+
* through `judgeShapeIssues` (single judge) / `panelShapeIssues` (panel), so
|
|
283
|
+
* that wording can never drift between them.
|
|
284
|
+
* - The ONLY part spelled twice is the two trivial `done`/`feedForward`
|
|
285
|
+
* `typeof !== "function"` predicates (one line each, here and in the load gate).
|
|
286
|
+
*
|
|
287
|
+
* Why not unify on one source? It would force either (a) a fragile
|
|
288
|
+
* `string.includes()` → issue-code mapping with no compile-time guard — renaming
|
|
289
|
+
* `` `done` `` in a message string would silently misroute to `assess-judge-shape`
|
|
290
|
+
* — or (b) a silent change to the user-visible message text the load gate emits.
|
|
291
|
+
* The cost of the kept duplication (two one-line `typeof` checks) is far smaller
|
|
292
|
+
* than either.
|
|
293
|
+
*
|
|
294
|
+
* This source stays internal-only: its sole consumer is `assess()`, and it is
|
|
295
|
+
* deliberately NOT exported from `registration.ts`, while the load gate owns the
|
|
296
|
+
* per-code surface the tests pin (`validate-workflow.test.ts:923,928`).
|
|
297
|
+
*/
|
|
298
|
+
export function assessShapeIssues(candidate: unknown): string[] {
|
|
299
|
+
if (!candidate || typeof candidate !== "object") return ["an assess object is required"];
|
|
300
|
+
const a = candidate as Partial<AssessOptions>;
|
|
301
|
+
const judgeIssues = judgeSlotShapeIssues(a.judge);
|
|
302
|
+
const issues: string[] = [...judgeIssues];
|
|
303
|
+
if (typeof a.done !== "function") {
|
|
304
|
+
issues.push("`done` must be a function deciding termination from the verdict");
|
|
305
|
+
}
|
|
306
|
+
if (typeof a.feedForward !== "function") {
|
|
307
|
+
issues.push("`feedForward` must be a function building the next producer arg");
|
|
308
|
+
}
|
|
309
|
+
return issues;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Construction-time positive-int gate for `max`/`concurrency` — the single
|
|
314
|
+
* spelling of the "integer >= 1" check across all three constructors. Throws
|
|
315
|
+
* at construction (the `defineRoute` pattern); byte-identical to the two
|
|
316
|
+
* `checkedMax`/`checkedConcurrency` near-twins it collapses.
|
|
317
|
+
*
|
|
318
|
+
* Deliberately NOT unified with the load-gate per-code reports
|
|
319
|
+
* (`validate/stage-rules.ts` `loop-max-invalid`,
|
|
320
|
+
* `loop-concurrency-invalid`) or the `loops/verify.ts` collect-all probe —
|
|
321
|
+
* those are a different layer (collect-all + per-code messaging), mirroring the
|
|
322
|
+
* `assessShapeIssues` DECISION-OF-RECORD posture. Internal-only: module-private,
|
|
323
|
+
* NOT exported from `registration.ts`.
|
|
324
|
+
*
|
|
325
|
+
* The `label` is the full origin+field prefix (e.g. `"fanout(): max"`); the
|
|
326
|
+
* throw message is `` `${label} must be an integer >= 1 (got ${value})` ``.
|
|
327
|
+
*/
|
|
328
|
+
function checkedPositiveInt(value: number | undefined, label: string): number | undefined {
|
|
329
|
+
if (value === undefined) return undefined;
|
|
330
|
+
if (!Number.isInteger(value) || value < 1) {
|
|
331
|
+
throw new Error(`${label} must be an integer >= 1 (got ${value})`);
|
|
332
|
+
}
|
|
333
|
+
return value;
|
|
334
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-stage derivations — the ONE consults the live + resume paths key off:
|
|
3
|
+
* `effectiveLoopOf` (loop-vs-verify shape), `freezesEntryArgsOf` (the round-0
|
|
4
|
+
* producer-arg rule), `judgeSlotOf` (the raw judge slot), `forEachJudgeChannel`
|
|
5
|
+
* (the verdict-channel walk). Runner-free — safe on `registration`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { LoopDef, StageDef } from "../api.js";
|
|
9
|
+
import { type AnyJudge, isPanel } from "../judge.js";
|
|
10
|
+
import { LOOP_DEFAULTS } from "./constructors.js";
|
|
11
|
+
import { panelVerdictChannel } from "./panel.js";
|
|
12
|
+
import { synthesizeVerifyLoop } from "./verify.js";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* THE round-0-entry-arg predicate — one spelling of "which kinds freeze a
|
|
16
|
+
* producer arg." Consulted by the three live/resume entry sites
|
|
17
|
+
* (`run-stage.ts`, `resume.ts`, `resume-loop.ts`) instead of re-spelling
|
|
18
|
+
* `loop.kind === "assess"`, so live and resume can no longer drift on the rule.
|
|
19
|
+
*/
|
|
20
|
+
export const freezesEntryArgsOf = (loop: LoopDef): boolean => LOOP_DEFAULTS[loop.kind].freezesEntryArgs;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* THE loop-or-verify consult — the one derivation of "does this stage run
|
|
24
|
+
* through the loop driver, and with what spec." Consulted by `tryLoop`
|
|
25
|
+
* (live), the resume fold's generation open, and `resumeLoopStage`, so the
|
|
26
|
+
* three can never disagree about a verify stage's loop shape.
|
|
27
|
+
*/
|
|
28
|
+
export function effectiveLoopOf(def: StageDef): LoopDef | undefined {
|
|
29
|
+
if (def.loop) return def.loop;
|
|
30
|
+
return def.verify ? synthesizeVerifyLoop(def.verify) : undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* THE judge-SLOT-of-stage derivation — the RAW `AnyJudge` (a single `Judge` or
|
|
35
|
+
* an N-member `PanelJudge`) a stage carries, before any member collapse.
|
|
36
|
+
* Panel-aware sites (the load gate's channel rules, `publishedNamesOf`) read
|
|
37
|
+
* the whole slot through this.
|
|
38
|
+
*/
|
|
39
|
+
export function judgeSlotOf(stage: StageDef): AnyJudge | undefined {
|
|
40
|
+
return stage.loop?.kind === "assess" ? stage.loop.judge : stage.verify?.judge;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Enumerate the verdict channels a stage's judge slot publishes, each with the
|
|
45
|
+
* skill that SIGNS it — `undefined` for the manufactured panel-fold channel
|
|
46
|
+
* (`panelVerdictChannel`), which is data-only and carries no producer contract.
|
|
47
|
+
* THE single judge-publisher walk: `publishedNamesOf` (load reachability —
|
|
48
|
+
* consumes EVERY channel) and `checkReadsChannelCompat`'s publisher index
|
|
49
|
+
* (contract adjudication — consumes only SIGNED channels) both read it, so a
|
|
50
|
+
* panel-member channel can no longer be seen by one and missed by the other.
|
|
51
|
+
*/
|
|
52
|
+
export function forEachJudgeChannel(
|
|
53
|
+
stage: StageDef,
|
|
54
|
+
name: string,
|
|
55
|
+
visit: (channel: string, signingSkill: string | undefined) => void,
|
|
56
|
+
): void {
|
|
57
|
+
const slot = judgeSlotOf(stage);
|
|
58
|
+
if (!slot) return;
|
|
59
|
+
if (isPanel(slot)) {
|
|
60
|
+
for (const m of slot.members) if (m?.outcome?.name) visit(m.outcome.name, m.skill);
|
|
61
|
+
visit(panelVerdictChannel(slot, name), undefined); // manufactured fold — unsigned
|
|
62
|
+
} else if (slot.outcome?.name) {
|
|
63
|
+
visit(slot.outcome.name, slot.skill);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loop introspection — pure-data projections of a stage's control flow + edge
|
|
3
|
+
* shape. Skill-agnostic: a workflow's structure is read entirely from attached
|
|
4
|
+
* data (no probing), so an analyzing/suggesting agent can render or reason about
|
|
5
|
+
* a flow without running it.
|
|
6
|
+
*
|
|
7
|
+
* The spec is the source of truth in the constructor — the `LoopDef` IS data
|
|
8
|
+
* with function-valued fields — so `loopSpecOf` can never lag a new loop kind.
|
|
9
|
+
* Runner-free — safe on `registration`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
type CapPolicy,
|
|
14
|
+
type EdgeFn,
|
|
15
|
+
type LoopDef,
|
|
16
|
+
type ResultProjection,
|
|
17
|
+
STOP,
|
|
18
|
+
type UnitSelector,
|
|
19
|
+
type Workflow,
|
|
20
|
+
} from "../api.js";
|
|
21
|
+
import {
|
|
22
|
+
type AnyJudge,
|
|
23
|
+
type CanonicalFoldName,
|
|
24
|
+
canonicalFoldName,
|
|
25
|
+
isPanel,
|
|
26
|
+
type Judge,
|
|
27
|
+
type PanelJudge,
|
|
28
|
+
} from "../judge.js";
|
|
29
|
+
import { readName, readsAll } from "../stage-def.js";
|
|
30
|
+
|
|
31
|
+
// ===========================================================================
|
|
32
|
+
// Introspection — per-stage control-flow + edge shape
|
|
33
|
+
// ===========================================================================
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Per-stage control-flow + edge shape, read entirely from attached data — no
|
|
37
|
+
* probing. The control-flow analogue of `legalNextSkills`: what an
|
|
38
|
+
* analyzing/suggesting agent consumes to render or reason about a flow's
|
|
39
|
+
* structure. `control.mode` covers assess.
|
|
40
|
+
*/
|
|
41
|
+
export interface StageShape {
|
|
42
|
+
stage: string;
|
|
43
|
+
skill?: string;
|
|
44
|
+
control: { mode: "single" | LoopDef["kind"]; spec?: LoopSpec };
|
|
45
|
+
/**
|
|
46
|
+
* Present iff the stage carries a `verify` post-condition. `control.mode`
|
|
47
|
+
* stays `"single"` for verify stages — verify is a stage property in the
|
|
48
|
+
* introspection model, not a loop kind (the desugar is a runtime concern).
|
|
49
|
+
* A `panel()` post-condition projects a `PanelJudgeSpec` here (still + `max`).
|
|
50
|
+
*/
|
|
51
|
+
verify?: AnyJudgeSpec & { max: number };
|
|
52
|
+
/**
|
|
53
|
+
* Present iff the stage declares `reads:`. One entry per read, in declared
|
|
54
|
+
* order, carrying the normalized channel `name` and `all` (true ⇒ a
|
|
55
|
+
* `fanin()` read that consumes EVERY accumulated entry — the fan-in barrier;
|
|
56
|
+
* false ⇒ latest-wins). Pure data — the preview layer renders the marker.
|
|
57
|
+
*/
|
|
58
|
+
reads?: ReadonlyArray<{ name: string; all: boolean }>;
|
|
59
|
+
/**
|
|
60
|
+
* Outgoing-edge shape. `mode: "terminal"` is the GRAPH-SINK sense (a stage
|
|
61
|
+
* with no outgoing edge OR an explicit `STOP`) — unrelated to the `terminal()`
|
|
62
|
+
* stage factory (stage-def.ts) and to "terminal failure" run-outcome prose
|
|
63
|
+
* (audit.ts). See the glossary on `stage-def.ts`'s `terminal` export.
|
|
64
|
+
*/
|
|
65
|
+
edge: { mode: "linear" | "route" | "terminal"; targets?: readonly string[] };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Describe a workflow's structure stage-by-stage from attached metadata alone. */
|
|
69
|
+
export function describeFlow(w: Workflow): StageShape[] {
|
|
70
|
+
return Object.entries(w.stages).map(([name, stage]) => {
|
|
71
|
+
const control: StageShape["control"] = stage.loop
|
|
72
|
+
? { mode: stage.loop.kind, spec: loopSpecOf(stage.loop) }
|
|
73
|
+
: { mode: "single" };
|
|
74
|
+
|
|
75
|
+
const target = w.edges[name];
|
|
76
|
+
let edge: StageShape["edge"];
|
|
77
|
+
if (target === undefined || target === STOP) {
|
|
78
|
+
// Graph-sink sense (no edge OR explicit STOP) — see `StageShape.edge`.
|
|
79
|
+
edge = { mode: "terminal" };
|
|
80
|
+
} else if (typeof target === "string") {
|
|
81
|
+
edge = { mode: "linear", targets: [target] };
|
|
82
|
+
} else {
|
|
83
|
+
edge = { mode: "route", targets: (target as EdgeFn).targets };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const reads = stage.reads?.map((r) => ({ name: readName(r), all: readsAll(r) }));
|
|
87
|
+
return {
|
|
88
|
+
stage: name,
|
|
89
|
+
skill: stage.skill,
|
|
90
|
+
control,
|
|
91
|
+
...(stage.verify ? { verify: { ...judgeSlotSpecOf(stage.verify.judge), max: stage.verify.max ?? 1 } } : {}),
|
|
92
|
+
...(reads?.length ? { reads } : {}),
|
|
93
|
+
edge,
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ===========================================================================
|
|
99
|
+
// Introspection — one channel for all loop kinds
|
|
100
|
+
// ===========================================================================
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Pure-data projection of a LoopDef — what `describeFlow`, `preview`, and the
|
|
104
|
+
* `checkFanoutSource` lint consume. `judge` summarises the dispatch without
|
|
105
|
+
* exposing functions: `prompt: true` means a prompt judge (the text/closure
|
|
106
|
+
* itself stays opaque).
|
|
107
|
+
*/
|
|
108
|
+
/** Pure-data judge summary — `prompt: true` means a prompt judge (the text/closure stays opaque). */
|
|
109
|
+
export interface JudgeSpec {
|
|
110
|
+
skill?: string;
|
|
111
|
+
prompt: boolean;
|
|
112
|
+
outcome: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Pure-data summary of an N-member panel — the introspection twin of
|
|
117
|
+
* `PanelJudge`. `panel` carries one `JudgeSpec` per member; `fold` is the sugar
|
|
118
|
+
* name (`majority`/`all`/`any`) for a canonical fold, `"custom"` for a raw
|
|
119
|
+
* author fold; `outcome` is the custom verdict channel, or `""` when the panel
|
|
120
|
+
* uses the canonical `<stage>-panel` default. Discriminated from `JudgeSpec` by
|
|
121
|
+
* the presence of `panel`.
|
|
122
|
+
*/
|
|
123
|
+
export interface PanelJudgeSpec {
|
|
124
|
+
panel: JudgeSpec[];
|
|
125
|
+
fold: CanonicalFoldName | "custom";
|
|
126
|
+
outcome: string;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Pure-data summary of a judge SLOT — a single judge or a panel (the introspection twin of `AnyJudge`). */
|
|
130
|
+
export type AnyJudgeSpec = JudgeSpec | PanelJudgeSpec;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Project the introspectable facet off a Judge — shared by `loopSpecOf` and the
|
|
134
|
+
* `StageShape.verify` projection so the two can't drift. Defensive on `outcome`:
|
|
135
|
+
* `validateWorkflow`'s lints run this over UNVALIDATED configs (a malformed
|
|
136
|
+
* member can lack `outcome` entirely), so a projection must never throw.
|
|
137
|
+
*/
|
|
138
|
+
export function judgeSpecOf(judge: Judge): JudgeSpec {
|
|
139
|
+
return { skill: judge.skill, prompt: judge.prompt !== undefined, outcome: judge.outcome?.name ?? "" };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Project the introspectable facet off a panel — member summaries + fold flavor
|
|
144
|
+
* + verdict channel. Defensive (like `judgeSpecOf`): a hand-rolled panel reaching
|
|
145
|
+
* a load-time lint may carry a non-array `members` or non-function `fold`.
|
|
146
|
+
*/
|
|
147
|
+
export function panelSpecOf(p: PanelJudge): PanelJudgeSpec {
|
|
148
|
+
const members = Array.isArray(p.members) ? p.members : [];
|
|
149
|
+
return {
|
|
150
|
+
panel: members.map(judgeSpecOf),
|
|
151
|
+
fold: typeof p.fold === "function" ? (canonicalFoldName(p.fold) ?? "custom") : "custom",
|
|
152
|
+
outcome: p.outcome?.name ?? "",
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Project the introspectable facet off a judge SLOT — a panel through `panelSpecOf`, a single judge through `judgeSpecOf`. */
|
|
157
|
+
export function judgeSlotSpecOf(slot: AnyJudge): AnyJudgeSpec {
|
|
158
|
+
return isPanel(slot) ? panelSpecOf(slot) : judgeSpecOf(slot);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export interface LoopSpec {
|
|
162
|
+
kind: LoopDef["kind"];
|
|
163
|
+
source?: string;
|
|
164
|
+
unit?: UnitSelector;
|
|
165
|
+
max?: number;
|
|
166
|
+
onCap: CapPolicy;
|
|
167
|
+
result: ResultProjection;
|
|
168
|
+
judge?: AnyJudgeSpec;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Project the introspectable facet off a stage's loop, or undefined for non-loop stages. */
|
|
172
|
+
export function loopSpecOf(loop: LoopDef | undefined): LoopSpec | undefined {
|
|
173
|
+
if (!loop) return undefined;
|
|
174
|
+
const base: LoopSpec = {
|
|
175
|
+
kind: loop.kind,
|
|
176
|
+
source: loop.source,
|
|
177
|
+
unit: loop.unit,
|
|
178
|
+
max: loop.max,
|
|
179
|
+
onCap: loop.onCap,
|
|
180
|
+
result: loop.result,
|
|
181
|
+
};
|
|
182
|
+
if (loop.kind === "assess") base.judge = judgeSlotSpecOf(loop.judge);
|
|
183
|
+
return base;
|
|
184
|
+
}
|