@juicesharp/rpiv-workflow 1.19.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -8
- package/api.ts +18 -703
- package/audit-ctx.ts +103 -0
- package/audit-rows.ts +154 -0
- package/audit.ts +158 -190
- package/built-ins.ts +40 -45
- package/chain-state.ts +220 -0
- package/command-run.ts +64 -31
- package/command.ts +123 -21
- package/docs/workflow-authoring.md +356 -63
- package/{lifecycle.ts → events.ts} +161 -37
- package/execution-host.ts +75 -0
- package/host.ts +149 -64
- package/index.ts +25 -81
- package/internal-utils.ts +171 -71
- package/internal.ts +22 -1
- package/judge.ts +241 -0
- package/layers.ts +10 -15
- package/load/alias.ts +6 -12
- package/load/cache.ts +9 -6
- package/load/index.ts +27 -106
- package/load/issues.ts +31 -0
- package/load/legacy.ts +102 -0
- package/load/merge.ts +2 -2
- package/load/resolve-default.ts +3 -4
- package/load/shape-guards.ts +3 -6
- package/load/skill-contract-phase.ts +94 -0
- package/loop-constructors.ts +24 -0
- package/loop-def.ts +285 -0
- package/loop-kinds.ts +750 -0
- package/loop-parallel.ts +250 -0
- package/loop-waves.ts +99 -0
- package/loop.ts +330 -0
- 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 +249 -156
- package/outcomes/collectors/directory-path.ts +7 -3
- package/outcomes/collectors/index.ts +1 -0
- package/outcomes/collectors/require-opt.ts +11 -0
- package/outcomes/collectors/text-scan.ts +47 -0
- package/outcomes/collectors/tool-call.ts +8 -3
- package/outcomes/collectors/transcript-path.ts +4 -18
- package/outcomes/collectors/union.ts +30 -10
- package/outcomes/collectors/url.ts +9 -14
- package/outcomes/collectors/workspace-diff.ts +21 -14
- package/outcomes/exec.ts +17 -0
- package/outcomes/git-commit.ts +190 -100
- package/outcomes/index.ts +1 -0
- package/outcomes/parsers/json-body.ts +2 -2
- package/outcomes/side-effect.ts +3 -3
- package/output-spec.ts +36 -10
- package/output.ts +98 -20
- package/package.json +27 -11
- package/predicates.ts +11 -8
- package/preview.ts +81 -21
- package/registration.ts +117 -36
- package/routing-dsl.ts +322 -0
- package/routing.ts +48 -2
- package/runner/by-name.ts +2 -1
- package/runner/by-run-id.ts +2 -1
- package/runner/chain-advance.ts +84 -56
- package/runner/errors.ts +10 -0
- package/runner/failure.ts +163 -0
- package/runner/index.ts +7 -13
- package/runner/input-validation.ts +16 -23
- package/runner/preflight.ts +169 -0
- package/runner/resolve-stage.ts +68 -0
- package/runner/resume-entry.ts +121 -0
- package/runner/resume-loop.ts +131 -0
- package/runner/resume.ts +368 -262
- package/runner/run-context.ts +165 -0
- package/runner/run-stage.ts +490 -0
- package/runner/runner.ts +202 -449
- package/runner/script-stage.ts +104 -112
- package/schema-compat.ts +12 -1
- package/semaphore.ts +55 -0
- package/sessions/extraction.ts +146 -81
- package/sessions/halt-routing.ts +195 -0
- package/sessions/index.ts +3 -1
- package/sessions/locate.ts +141 -0
- package/sessions/reattach.ts +71 -0
- package/sessions/sessions.ts +114 -199
- package/sessions/spawn.ts +132 -119
- package/sessions/success-persist.ts +89 -0
- package/skill-contract.ts +22 -25
- package/skill-contracts/composition.ts +86 -37
- package/skill-contracts/{registries.ts → extension-points.ts} +45 -12
- package/skill-contracts/harvest.ts +4 -4
- package/skill-contracts/index.ts +15 -8
- package/skill-contracts/registry.ts +33 -50
- package/stage-def.ts +595 -0
- package/stage-errors.ts +84 -0
- package/stage-identity.ts +56 -0
- package/startup.ts +4 -1
- package/state/index.ts +8 -1
- package/state/names.ts +91 -65
- package/state/paths.ts +20 -0
- package/state/raw.ts +77 -0
- package/state/reads.ts +103 -85
- package/state/resolve.ts +73 -0
- package/state/state.ts +135 -15
- package/state/writes.ts +29 -12
- package/transcript.ts +24 -0
- package/triggers.ts +11 -3
- package/types.ts +290 -78
- package/validate/contract-compat.ts +198 -0
- package/validate/graph.ts +134 -0
- package/validate/issue.ts +328 -0
- package/validate/stage-rules.ts +499 -0
- package/validate-output.ts +82 -7
- package/validate-workflow.ts +57 -759
- package/validation-bounds.ts +35 -0
- package/control-flow.ts +0 -135
- package/fanout.ts +0 -102
- package/iterate.ts +0 -155
- package/load/cache.test.ts +0 -82
- package/load/shape-guards.test.ts +0 -74
- package/outcomes/collectors/directory-path.test.ts +0 -64
- package/outcomes/collectors/tool-call.test.ts +0 -110
- package/outcomes/collectors/transcript-path.test.ts +0 -70
- package/outcomes/collectors/union.test.ts +0 -59
- package/outcomes/collectors/url.test.ts +0 -67
- package/outcomes/collectors/workspace-diff.test.ts +0 -107
- package/outcomes/git-commit.test.ts +0 -194
- 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/resume-fanout.test.ts +0 -331
- package/runner/resume-fanout.ts +0 -136
- package/runner/resume-iterate.test.ts +0 -444
- package/runner/resume-iterate.ts +0 -111
- package/runner/resume.test.ts +0 -1130
- package/runner/stage-lifecycle.ts +0 -500
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-stage semantic rules — bounds and enums the TS type system narrows at
|
|
3
|
+
* edit time but jiti erases at runtime, plus the loop/verify/prompt/script
|
|
4
|
+
* exclusion matrices and named-channel (`reads`) wiring. A user-authored
|
|
5
|
+
* config can ship any numeric `maxRetries` or any string for `onInvalid`;
|
|
6
|
+
* this pass catches them at load time.
|
|
7
|
+
*
|
|
8
|
+
* Each check is a focused helper taking the stage-bound `ReportFn` so the
|
|
9
|
+
* orchestration loop reads top-down and individual rules can be exercised in
|
|
10
|
+
* isolation. Contract/schema compatibility lives in `contract-compat.ts`
|
|
11
|
+
* (different dependency footprint entirely).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { LOOP_KINDS, ON_INVALID_VALUES, SESSION_POLICIES, STAGE_KINDS, type StageDef, type Workflow } from "../api.js";
|
|
15
|
+
import { type AnyJudge, isPanel } from "../judge.js";
|
|
16
|
+
import {
|
|
17
|
+
forEachJudgeChannel,
|
|
18
|
+
judgeSlotShapeIssues,
|
|
19
|
+
loopSpecOf,
|
|
20
|
+
panelVerdictChannel,
|
|
21
|
+
verifyShapeIssues,
|
|
22
|
+
} from "../loop-constructors.js";
|
|
23
|
+
import { readName } from "../stage-def.js";
|
|
24
|
+
import { resolvePublishName } from "../stage-identity.js";
|
|
25
|
+
import {
|
|
26
|
+
MAX_VALIDATION_RETRIES,
|
|
27
|
+
MAX_VALIDATION_RETRY_TIMEOUT_MS,
|
|
28
|
+
MIN_VALIDATION_RETRIES,
|
|
29
|
+
MIN_VALIDATION_RETRY_TIMEOUT_MS,
|
|
30
|
+
} from "../validation-bounds.js";
|
|
31
|
+
import type { IssueReporter, ReportFn } from "./issue.js";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Orchestrates the per-stage rule blocks. The orchestration loop binds the
|
|
35
|
+
* reporter to each stage once; rules that need workflow context beyond the
|
|
36
|
+
* stage (currently only "is this the start stage") receive it explicitly.
|
|
37
|
+
*/
|
|
38
|
+
export function checkStageSemantics(w: Workflow, r: IssueReporter): void {
|
|
39
|
+
for (const [name, stage] of Object.entries(w.stages)) {
|
|
40
|
+
const report = r.forStage(name);
|
|
41
|
+
checkRetryBounds(stage, report);
|
|
42
|
+
checkTimeoutBounds(stage, report);
|
|
43
|
+
checkStageEnums(stage, report);
|
|
44
|
+
checkLoopInvariants(stage, name, report);
|
|
45
|
+
checkVerifyInvariants(stage, name, report);
|
|
46
|
+
checkPromptInvariants(stage, name === w.start, report);
|
|
47
|
+
checkInheritsArtifactsKind(stage, report);
|
|
48
|
+
checkScriptStageInvariants(stage, report);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* One rule block for the single `loop` field. Constructors already threw on
|
|
54
|
+
* these at authoring time; this is the defensive load gate for hand-rolled
|
|
55
|
+
* literals (jiti erases TS types) and programmatic embedders.
|
|
56
|
+
*/
|
|
57
|
+
function checkLoopInvariants(stage: StageDef, name: string, report: ReportFn): void {
|
|
58
|
+
const loop = stage.loop;
|
|
59
|
+
if (!loop) return;
|
|
60
|
+
|
|
61
|
+
if (!(LOOP_KINDS as readonly string[]).includes(loop.kind)) {
|
|
62
|
+
report("loop-kind-unknown", { kind: loop.kind, allowed: LOOP_KINDS.join(", ") });
|
|
63
|
+
return; // kind-specific rules below would misfire on an unknown kind
|
|
64
|
+
}
|
|
65
|
+
// ONE continue rule (was three identical ones).
|
|
66
|
+
if (stage.sessionPolicy === "continue") {
|
|
67
|
+
report("loop-continue-session");
|
|
68
|
+
}
|
|
69
|
+
// Enforced for ALL loops now (was advisory-only for fanout/iterate specs).
|
|
70
|
+
if (loop.max !== undefined && (!Number.isInteger(loop.max) || loop.max < 1)) {
|
|
71
|
+
report("loop-max-invalid", { max: loop.max });
|
|
72
|
+
}
|
|
73
|
+
if (
|
|
74
|
+
loop.kind === "fanout" &&
|
|
75
|
+
loop.concurrency !== undefined &&
|
|
76
|
+
(!Number.isInteger(loop.concurrency) || loop.concurrency < 1)
|
|
77
|
+
) {
|
|
78
|
+
report("loop-concurrency-invalid", { concurrency: loop.concurrency });
|
|
79
|
+
}
|
|
80
|
+
if (
|
|
81
|
+
loop.kind === "fanout" &&
|
|
82
|
+
loop.depArtifactFlag !== undefined &&
|
|
83
|
+
(typeof loop.depArtifactFlag !== "string" || loop.depArtifactFlag.trim().length === 0)
|
|
84
|
+
) {
|
|
85
|
+
report("loop-dep-flag-invalid", { depArtifactFlag: loop.depArtifactFlag });
|
|
86
|
+
}
|
|
87
|
+
// Pull loops + assess run the stage's outcome collector per unit.
|
|
88
|
+
if ((loop.kind === "iterate" || loop.kind === "assess") && stage.kind !== "produces") {
|
|
89
|
+
report("loop-requires-produces", { kind: loop.kind });
|
|
90
|
+
}
|
|
91
|
+
// A stable named slot: iterate and assess always (every unit/round runs the
|
|
92
|
+
// produces collector), fanout when COLLECTING (produces kind) — the decorated
|
|
93
|
+
// display string must never split the accumulation slot.
|
|
94
|
+
const needsName =
|
|
95
|
+
loop.kind === "iterate" || loop.kind === "assess" || (loop.kind === "fanout" && stage.kind === "produces");
|
|
96
|
+
if (needsName && !stage.outcome?.name) {
|
|
97
|
+
report("loop-outcome-name-required");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (loop.kind !== "assess") return;
|
|
101
|
+
|
|
102
|
+
// Judge shape — SAME rule source as the judge()/panel() factories (no
|
|
103
|
+
// wording drift). The slot is an `AnyJudge`, routed through the shared
|
|
104
|
+
// `judgeSlotShapeIssues` (the panel-vs-single dispatch spelled once, here
|
|
105
|
+
// and in the assess/verify construction paths).
|
|
106
|
+
//
|
|
107
|
+
// This block is the LOAD-GATE half of the (intentionally dual) assess shape
|
|
108
|
+
// validation — see the rationale of record on `assessShapeIssues` in
|
|
109
|
+
// `loop-constructors.ts`. The judge/panel shape above is shared verbatim; the
|
|
110
|
+
// ONLY duplication is the two trivial `done`/`feedForward`
|
|
111
|
+
// `typeof !== "function"` predicates below, kept here AND in `assessShapeIssues`
|
|
112
|
+
// by accepted design (unifying would force a fragile string→code mapping or a
|
|
113
|
+
// silent message change). This block owns the per-code surface the tests pin —
|
|
114
|
+
// `assess-judge-shape` / `assess-done-not-function` /
|
|
115
|
+
// `assess-feed-forward-not-function` (`validate-workflow.test.ts:923,928`).
|
|
116
|
+
const slot = loop.judge;
|
|
117
|
+
const shapeIssues = judgeSlotShapeIssues(slot);
|
|
118
|
+
for (const issue of shapeIssues) {
|
|
119
|
+
report("assess-judge-shape", { issue });
|
|
120
|
+
}
|
|
121
|
+
if (typeof loop.done !== "function") {
|
|
122
|
+
report("assess-done-not-function");
|
|
123
|
+
}
|
|
124
|
+
if (typeof loop.feedForward !== "function") {
|
|
125
|
+
report("assess-feed-forward-not-function");
|
|
126
|
+
}
|
|
127
|
+
// Verdict-channel collisions — STAY workflow-level (need the producer's
|
|
128
|
+
// publish identity, which only the stage knows).
|
|
129
|
+
checkVerdictChannels(slot, name, stage, "assess-verdict-channel-collision", report);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Verdict-channel collisions for a judge SLOT (`AnyJudge`) — workflow-level
|
|
134
|
+
* because it needs the producer's publish identity
|
|
135
|
+
* (`resolvePublishName(stage, name)`). A single judge owns ONE verdict channel;
|
|
136
|
+
* a PANEL owns one channel per member PLUS the folded-verdict channel, and every
|
|
137
|
+
* one of them must be distinct from the others and from the producer's own
|
|
138
|
+
* slot, or two sessions clobber a single `state.named` entry. The SHAPE is
|
|
139
|
+
* checked separately through the matching rule source; this is purely the
|
|
140
|
+
* channel-namespace rule.
|
|
141
|
+
*
|
|
142
|
+
* `singleCode` (assess- vs verify-worded) is the only per-site difference; the
|
|
143
|
+
* panel codes are site-independent.
|
|
144
|
+
*/
|
|
145
|
+
function checkVerdictChannels(
|
|
146
|
+
slot: AnyJudge | undefined,
|
|
147
|
+
name: string,
|
|
148
|
+
stage: StageDef,
|
|
149
|
+
singleCode: "assess-verdict-channel-collision" | "verify-verdict-channel-collision",
|
|
150
|
+
report: ReportFn,
|
|
151
|
+
): void {
|
|
152
|
+
if (!slot) return;
|
|
153
|
+
const producer = resolvePublishName(stage, name);
|
|
154
|
+
if (!isPanel(slot)) {
|
|
155
|
+
if (slot.outcome?.name && slot.outcome.name === producer) {
|
|
156
|
+
report(singleCode, { channel: slot.outcome.name });
|
|
157
|
+
}
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
// Panel: members + fold share one namespace with the producer. First
|
|
161
|
+
// claimant of a name wins; a later claimant is the collision.
|
|
162
|
+
const claimed = new Set<string>([producer]);
|
|
163
|
+
for (const m of slot.members) {
|
|
164
|
+
const channel = m?.outcome?.name;
|
|
165
|
+
if (!channel) continue; // a member missing outcome.name is a shape issue (panelShapeIssues)
|
|
166
|
+
if (claimed.has(channel)) report("panel-member-channel-collision", { channel });
|
|
167
|
+
else claimed.add(channel);
|
|
168
|
+
}
|
|
169
|
+
const fold = panelVerdictChannel(slot, name);
|
|
170
|
+
if (claimed.has(fold)) report("panel-verdict-channel-collision", { channel: fold });
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* One rule block for the `verify` field. The `verify()` constructor already
|
|
175
|
+
* threw on shape violations at authoring time; this is the defensive load
|
|
176
|
+
* gate for hand-rolled literals (jiti erases TS types), plus the
|
|
177
|
+
* workflow-level rules that need the stage's identity. The runtime engine is
|
|
178
|
+
* the loop driver (the desugar), so the structural exclusions mirror the
|
|
179
|
+
* loop rules — but verify-worded, because the author declared a
|
|
180
|
+
* post-condition, not a loop.
|
|
181
|
+
*/
|
|
182
|
+
function checkVerifyInvariants(stage: StageDef, name: string, report: ReportFn): void {
|
|
183
|
+
const v = stage.verify;
|
|
184
|
+
if (!v) return;
|
|
185
|
+
|
|
186
|
+
// Shape — SAME rule source as the verify() factory (no wording drift).
|
|
187
|
+
for (const issue of verifyShapeIssues(v)) {
|
|
188
|
+
report("verify-shape", { issue });
|
|
189
|
+
}
|
|
190
|
+
if (stage.loop) {
|
|
191
|
+
report("verify-with-loop");
|
|
192
|
+
}
|
|
193
|
+
if (stage.run) {
|
|
194
|
+
report("verify-with-run");
|
|
195
|
+
}
|
|
196
|
+
if (stage.kind !== "produces") {
|
|
197
|
+
report("verify-requires-produces");
|
|
198
|
+
}
|
|
199
|
+
if (stage.sessionPolicy === "continue") {
|
|
200
|
+
report("verify-continue-session");
|
|
201
|
+
}
|
|
202
|
+
// Attempts publish to a stable named slot — without `outcome.name` the
|
|
203
|
+
// fallback publish key would be the DECORATED unit display string
|
|
204
|
+
// ("build (a0·attempt)"), splitting the accumulation slot per attempt
|
|
205
|
+
// (same rationale as the collecting-loop rule).
|
|
206
|
+
if (stage.kind === "produces" && !stage.run && !stage.outcome?.name) {
|
|
207
|
+
report("verify-outcome-name-required");
|
|
208
|
+
}
|
|
209
|
+
// Verdict-channel collisions — STAY workflow-level (need the producer's
|
|
210
|
+
// publish identity, which only the stage knows). Mirrors the assess rule;
|
|
211
|
+
// `checkVerdictChannels` walks panel members + fold for an `AnyJudge` slot.
|
|
212
|
+
checkVerdictChannels(v.judge, name, stage, "verify-verdict-channel-collision", report);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* ONE range probe — collapses `checkRetryBounds`/`checkTimeoutBounds`. Optional-
|
|
217
|
+
* field early-out → range predicate → `{value, min, max}` report. The bounds
|
|
218
|
+
* constants already live first-class at each call site, so the probe carries
|
|
219
|
+
* zero information not expressible as parameters.
|
|
220
|
+
*/
|
|
221
|
+
function checkRange(
|
|
222
|
+
report: ReportFn,
|
|
223
|
+
code: "max-retries-out-of-range" | "validate-timeout-out-of-range",
|
|
224
|
+
value: number | undefined,
|
|
225
|
+
min: number,
|
|
226
|
+
max: number,
|
|
227
|
+
): void {
|
|
228
|
+
if (value === undefined) return;
|
|
229
|
+
if (value < min || value > max) report(code, { value, min, max });
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* ONE enum probe — collapses the three same-shape `{value, allowed}` guards in
|
|
234
|
+
* `checkStageEnums` (onInvalid / kind / sessionPolicy). NOTE: `loop-kind-unknown`
|
|
235
|
+
* (`checkLoopInvariants`) is intentionally NOT unified — it reports `{kind,
|
|
236
|
+
* allowed}` (different param key) and carries an early `return` after the report
|
|
237
|
+
* (kind-specific rules would misfire on an unknown kind); forcing it through this
|
|
238
|
+
* helper would touch the public issue-param shape for marginal gain.
|
|
239
|
+
*/
|
|
240
|
+
function checkEnum(
|
|
241
|
+
report: ReportFn,
|
|
242
|
+
code: "on-invalid-unknown" | "stage-kind-unknown" | "session-policy-unknown",
|
|
243
|
+
value: string | undefined,
|
|
244
|
+
allowed: readonly string[],
|
|
245
|
+
): void {
|
|
246
|
+
if (value !== undefined && !allowed.includes(value)) report(code, { value, allowed: allowed.join(", ") });
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function checkRetryBounds(stage: StageDef, report: ReportFn): void {
|
|
250
|
+
checkRange(report, "max-retries-out-of-range", stage.maxRetries, MIN_VALIDATION_RETRIES, MAX_VALIDATION_RETRIES);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function checkTimeoutBounds(stage: StageDef, report: ReportFn): void {
|
|
254
|
+
checkRange(
|
|
255
|
+
report,
|
|
256
|
+
"validate-timeout-out-of-range",
|
|
257
|
+
stage.validateTimeoutMs,
|
|
258
|
+
MIN_VALIDATION_RETRY_TIMEOUT_MS,
|
|
259
|
+
MAX_VALIDATION_RETRY_TIMEOUT_MS,
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function checkStageEnums(stage: StageDef, report: ReportFn): void {
|
|
264
|
+
checkEnum(report, "on-invalid-unknown", stage.onInvalid, ON_INVALID_VALUES);
|
|
265
|
+
checkEnum(report, "stage-kind-unknown", stage.kind, STAGE_KINDS);
|
|
266
|
+
checkEnum(report, "session-policy-unknown", stage.sessionPolicy, SESSION_POLICIES);
|
|
267
|
+
if (stage.kind === "produces" && !stage.outcome && !stage.run) {
|
|
268
|
+
report("produces-without-outcome");
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* `prompt` is the raw-text dispatch — the third option alongside skill
|
|
274
|
+
* (`/skill:<name>`) and script `run`. Its invariants keep the dispatch
|
|
275
|
+
* discriminator unambiguous and the input model single:
|
|
276
|
+
*
|
|
277
|
+
* - mutually exclusive with an explicit `skill` (you're either invoking a
|
|
278
|
+
* skill or sending raw text — `skill` defaulting to the record key does
|
|
279
|
+
* NOT trip this, only an explicitly-set `skill`);
|
|
280
|
+
* - mutually exclusive with `fanout`/`iterate` loops (principled, not
|
|
281
|
+
* deferred: every unit's message comes from `units()`/`next()`, so a
|
|
282
|
+
* stage-level `prompt` would have no role). `assess` loops and `verify`
|
|
283
|
+
* COMPOSE: the stage's `prompt` is round/attempt 0's message and
|
|
284
|
+
* `feedForward` builds each retry's complete message (raw — no `/skill:`
|
|
285
|
+
* prefix to attach an arg to);
|
|
286
|
+
* - mutually exclusive with `reads` — a skill stage's `reads` auto-builds a
|
|
287
|
+
* labelled-flag arg, but a prompt stage's text is author-owned; rather than
|
|
288
|
+
* give `reads` two meanings, require the prompt to read `state.named`
|
|
289
|
+
* itself via its `PromptFn`;
|
|
290
|
+
* - a literal empty/whitespace string is a no-op dispatch → author error.
|
|
291
|
+
*
|
|
292
|
+
* (`prompt` + `run` is reported by checkScriptStageInvariants, mirroring
|
|
293
|
+
* fanout/iterate + run. `produces` + `prompt` with no `outcome` is already
|
|
294
|
+
* caught by the produces-requires-outcome rule — `prompt`, unlike `run`, is not
|
|
295
|
+
* carved out of it.)
|
|
296
|
+
*
|
|
297
|
+
* A `continue` prompt stage used as the workflow START gets a WARNING: a
|
|
298
|
+
* follow-up turn with no prior context to lean on is almost certainly an
|
|
299
|
+
* authoring mistake (the continue session would have nothing to continue).
|
|
300
|
+
*/
|
|
301
|
+
function checkPromptInvariants(stage: StageDef, isStart: boolean, report: ReportFn): void {
|
|
302
|
+
if (stage.prompt === undefined) return;
|
|
303
|
+
// The narrowed PromptStage arm pins `reads` to `never` — exactly why this
|
|
304
|
+
// gate exists: a jiti-loaded literal can still ship the field. Probe the
|
|
305
|
+
// erased shape, not the arm.
|
|
306
|
+
const erased = stage as { skill?: unknown; reads?: readonly string[] };
|
|
307
|
+
if (erased.skill !== undefined) {
|
|
308
|
+
report("prompt-with-skill");
|
|
309
|
+
}
|
|
310
|
+
if (stage.loop && stage.loop.kind !== "assess") {
|
|
311
|
+
report("prompt-with-loop", { kind: stage.loop.kind });
|
|
312
|
+
}
|
|
313
|
+
if (erased.reads?.length) {
|
|
314
|
+
report("prompt-with-reads");
|
|
315
|
+
}
|
|
316
|
+
if (typeof stage.prompt === "string" && stage.prompt.trim() === "") {
|
|
317
|
+
report("prompt-empty");
|
|
318
|
+
}
|
|
319
|
+
if (stage.sessionPolicy === "continue" && isStart) {
|
|
320
|
+
report("prompt-continue-at-start");
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* `inheritsArtifacts: false` is the `terminal()` factory's mechanism — it
|
|
326
|
+
* tells the runner to bypass upstream-artifact inheritance for a
|
|
327
|
+
* side-effect stage. Setting it on a `produces` stage is meaningless: a
|
|
328
|
+
* `produces` stage emits its own outcome and never consumes the upstream
|
|
329
|
+
* primary artifact in `inputForStage` (the first stage always uses
|
|
330
|
+
* originalInput, and inheritance only affects the prompt arg). Surface
|
|
331
|
+
* the redundancy as a warning so users don't author "off" flags they
|
|
332
|
+
* think are doing something.
|
|
333
|
+
*/
|
|
334
|
+
function checkInheritsArtifactsKind(stage: StageDef, report: ReportFn): void {
|
|
335
|
+
if (stage.inheritsArtifacts === false && stage.kind === "produces") {
|
|
336
|
+
report("inherits-artifacts-on-produces");
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Skillless script stages: presence of `stage.run` declares "the runner
|
|
342
|
+
* calls this TS function instead of dispatching a Pi skill." Four fields
|
|
343
|
+
* are categorically incompatible with that contract — fail loudly at
|
|
344
|
+
* load time so the runner branch can assume the invariant.
|
|
345
|
+
*
|
|
346
|
+
* Mutual-exclusion rules:
|
|
347
|
+
*
|
|
348
|
+
* - `skill` — the function IS the work; a skill body would never
|
|
349
|
+
* be dispatched.
|
|
350
|
+
* - `outcome` — the function returns the `Output` envelope directly;
|
|
351
|
+
* there is no transcript / tool-use stream for a
|
|
352
|
+
* collector to scan.
|
|
353
|
+
* - `loop` — a TS function can write its own loop; the runner's
|
|
354
|
+
* per-unit session machinery doesn't apply.
|
|
355
|
+
* - `sessionPolicy: "continue"` — there is no Pi session at all on a
|
|
356
|
+
* script stage; nothing to continue.
|
|
357
|
+
*
|
|
358
|
+
* Side-effect script stages with an `outputSchema` get a warning — the
|
|
359
|
+
* function returns `void`, so no data ever flows through the validator.
|
|
360
|
+
*
|
|
361
|
+
* The existing `produces` + `inheritsArtifacts: false` warning
|
|
362
|
+
* (`checkInheritsArtifactsKind`) fires uniformly for both skill and
|
|
363
|
+
* script variants — same author error, same message.
|
|
364
|
+
*/
|
|
365
|
+
function checkScriptStageInvariants(stage: StageDef, report: ReportFn): void {
|
|
366
|
+
if (!stage.run) return;
|
|
367
|
+
|
|
368
|
+
if (stage.skill !== undefined) {
|
|
369
|
+
report("script-with-skill");
|
|
370
|
+
}
|
|
371
|
+
if (stage.outcome) {
|
|
372
|
+
report("script-with-outcome");
|
|
373
|
+
}
|
|
374
|
+
if (stage.loop) {
|
|
375
|
+
report("script-with-loop");
|
|
376
|
+
}
|
|
377
|
+
if (stage.prompt !== undefined) {
|
|
378
|
+
report("script-with-prompt");
|
|
379
|
+
}
|
|
380
|
+
if (stage.sessionPolicy === "continue") {
|
|
381
|
+
report("script-continue-session");
|
|
382
|
+
}
|
|
383
|
+
if (stage.kind === "side-effect" && stage.outputSchema) {
|
|
384
|
+
report("script-side-effect-output-schema");
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Every named channel some stage in this workflow can publish: top-level
|
|
390
|
+
* `produces` publishes (`resolvePublishName` — the runtime write rule), PLUS
|
|
391
|
+
* judge verdict channels from `loop` (assess) and `verify` — judge sessions
|
|
392
|
+
* run as `produces` and publish to `judge.outcome.name` (`judgeStageDef`). A
|
|
393
|
+
* PANEL slot publishes one channel per member verdict plus the folded verdict
|
|
394
|
+
* (`panelVerdictChannel`). The scan includes verdict channels, so a downstream
|
|
395
|
+
* `reads: ["<verdict>"]` doesn't falsely error at load while the runtime
|
|
396
|
+
* `ensureNamedReads` preflight would pass.
|
|
397
|
+
*
|
|
398
|
+
* Computed ONCE by the orchestrator and threaded to both consumers
|
|
399
|
+
* (`checkReadsReferences`, `checkFanoutSource`).
|
|
400
|
+
*/
|
|
401
|
+
export function publishedNamesOf(w: Workflow): Set<string> {
|
|
402
|
+
const published = new Set<string>();
|
|
403
|
+
for (const [name, stage] of Object.entries(w.stages)) {
|
|
404
|
+
if (stage.kind === "produces") published.add(resolvePublishName(stage, name));
|
|
405
|
+
// Every judge channel (single judge, panel members, AND the folded verdict)
|
|
406
|
+
// counts for reachability — shared walk with the contract-compat index.
|
|
407
|
+
forEachJudgeChannel(stage, name, (channel) => published.add(channel));
|
|
408
|
+
}
|
|
409
|
+
return published;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Every name in a stage's `reads:` must be filled by some `produces` stage
|
|
414
|
+
* in the workflow. The publish key is `stage.outcome?.name ?? <record-key>`
|
|
415
|
+
* — same rule the runner enforces at write time (see
|
|
416
|
+
* `resolvePublishName`). Reachability isn't checked here: validating that
|
|
417
|
+
* the producer can actually reach the consumer in the edge graph is a
|
|
418
|
+
* larger graph problem and the static check is intentionally narrow —
|
|
419
|
+
* it answers "does this name correspond to something in the workflow at
|
|
420
|
+
* all?", catching typos and renames; the runtime `ensureNamedReads`
|
|
421
|
+
* preflight handles the "haven't fired yet" case.
|
|
422
|
+
*/
|
|
423
|
+
export function checkReadsReferences(w: Workflow, published: ReadonlySet<string>, r: IssueReporter): void {
|
|
424
|
+
for (const [name, stage] of Object.entries(w.stages)) {
|
|
425
|
+
if (!stage.reads?.length) continue;
|
|
426
|
+
const report = r.forStage(name);
|
|
427
|
+
for (const read of stage.reads) {
|
|
428
|
+
const channel = readName(read);
|
|
429
|
+
if (published.has(channel)) continue;
|
|
430
|
+
report("reads-unpublished", { channel });
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Load-time loop-source check (control-flow as data). A stage whose `loop`
|
|
437
|
+
* declares a `source` channel (via `fanout()`/`iterate()`/`assess()`) must have
|
|
438
|
+
* that channel published by some `produces` stage in this workflow — otherwise
|
|
439
|
+
* the stage splits over a channel nothing fills. Same publisher model as
|
|
440
|
+
* `checkReadsReferences` (`stage.outcome?.name ?? <record-key>`).
|
|
441
|
+
*
|
|
442
|
+
* WARNS (never errors): `source` is an introspective hint, and a loop without
|
|
443
|
+
* `source` degrades silently — mirrors the edge-compat posture. When the source
|
|
444
|
+
* is already in the stage's `reads`, `checkReadsReferences` owns it (errors) —
|
|
445
|
+
* skip to avoid double-reporting. The additive value is the `iterate`/closure-
|
|
446
|
+
* sourced case, which declares no `reads:` and is otherwise unchecked.
|
|
447
|
+
*/
|
|
448
|
+
const LOOP_VERB = { fanout: "fans out over", iterate: "iterates over", assess: "assesses over" } as const;
|
|
449
|
+
|
|
450
|
+
export function checkFanoutSource(w: Workflow, published: ReadonlySet<string>, r: IssueReporter): void {
|
|
451
|
+
for (const [name, stage] of Object.entries(w.stages)) {
|
|
452
|
+
const spec = loopSpecOf(stage.loop);
|
|
453
|
+
const source = spec?.source;
|
|
454
|
+
if (!source || published.has(source)) continue; // no source / satisfied → degrade
|
|
455
|
+
if (stage.reads?.some((read) => readName(read) === source)) continue; // checkReadsReferences owns this channel
|
|
456
|
+
r.forStage(name)("loop-source-unpublished", { verb: LOOP_VERB[spec.kind], source });
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Channels published by a `fanout()` stage — the producer walk of
|
|
462
|
+
* `publishedNamesOf`, narrowed to fanout loops. Built once and threaded to
|
|
463
|
+
* `checkFanoutReadHint`.
|
|
464
|
+
*
|
|
465
|
+
* The `stage.kind === "produces"` clause is LOAD-BEARING — only a COLLECTING
|
|
466
|
+
* fanout (produces kind) accumulates per-unit Outputs into a named channel; an
|
|
467
|
+
* `acts()` fanout (`kind: "side-effect"`) publishes nothing, so its name would
|
|
468
|
+
* be a false fanout channel. rpiv-pi's built-ins carry fanout loops on
|
|
469
|
+
* `acts()` implement stages while the "plans" channel they read is published by
|
|
470
|
+
* a separate `produces()` stage — relaxing this predicate would fire the hint
|
|
471
|
+
* across every shipped built-in and break the sibling package's zero-warning
|
|
472
|
+
* release gate.
|
|
473
|
+
*/
|
|
474
|
+
export function fanoutPublishedChannels(w: Workflow): Set<string> {
|
|
475
|
+
const channels = new Set<string>();
|
|
476
|
+
for (const [name, stage] of Object.entries(w.stages)) {
|
|
477
|
+
if (loopSpecOf(stage.loop)?.kind === "fanout" && stage.kind === "produces") {
|
|
478
|
+
channels.add(resolvePublishName(stage, name));
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
return channels;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Soft nudge: a bare-string read of a channel that a fanout fills reads only the
|
|
486
|
+
* last unit's output (`array.at(-1)`). Almost always the author meant `fanin()`
|
|
487
|
+
* — the fanout-and-synthesize barrier. WARNS (never errors): latest-only is
|
|
488
|
+
* legal. `fanin()` reads are already opted in (object form) — skipped.
|
|
489
|
+
*/
|
|
490
|
+
export function checkFanoutReadHint(w: Workflow, fanoutChannels: ReadonlySet<string>, r: IssueReporter): void {
|
|
491
|
+
for (const [name, stage] of Object.entries(w.stages)) {
|
|
492
|
+
if (!stage.reads?.length) continue;
|
|
493
|
+
const report = r.forStage(name);
|
|
494
|
+
for (const read of stage.reads) {
|
|
495
|
+
if (typeof read !== "string") continue; // already opted in via fanin()
|
|
496
|
+
if (fanoutChannels.has(read)) report("reads-latest-from-fanout", { channel: read });
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
package/validate-output.ts
CHANGED
|
@@ -42,13 +42,88 @@ export interface ValidationResult {
|
|
|
42
42
|
// Constants
|
|
43
43
|
// ---------------------------------------------------------------------------
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
// The policy bounds live in the dependency-free `validation-bounds.ts` leaf so
|
|
46
|
+
// the load-time validator can read them without importing this runtime module;
|
|
47
|
+
// re-exported here so this module's runtime callers keep a single import.
|
|
48
|
+
export {
|
|
49
|
+
DEFAULT_VALIDATION_RETRIES,
|
|
50
|
+
DEFAULT_VALIDATION_RETRY_TIMEOUT_MS,
|
|
51
|
+
MAX_VALIDATION_RETRIES,
|
|
52
|
+
MAX_VALIDATION_RETRY_TIMEOUT_MS,
|
|
53
|
+
MIN_VALIDATION_RETRIES,
|
|
54
|
+
MIN_VALIDATION_RETRY_TIMEOUT_MS,
|
|
55
|
+
} from "./validation-bounds.js";
|
|
48
56
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Thrown by `withTimeout` (internal-utils.ts) when the caller passes a
|
|
59
|
+
* `SchemaTimeoutError` instance as the message. Lets validation consumers
|
|
60
|
+
* distinguish a schema-evaluation timeout from inner-promise rejections via
|
|
61
|
+
* `instanceof` instead of string-identity comparison. Lives in the validation
|
|
62
|
+
* domain — it is part of the schema-validation contract, not a generic
|
|
63
|
+
* timeout utility.
|
|
64
|
+
*/
|
|
65
|
+
export class SchemaTimeoutError extends Error {}
|
|
66
|
+
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
// Retry-policy loop
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Hooks for `runValidationRetryLoop`. `H` is the caller's abort payload — a
|
|
73
|
+
* tagged value that aborts the loop immediately (extraction's fatal arm; the
|
|
74
|
+
* script path's already-recorded failure marker). Throws are NOT caught:
|
|
75
|
+
* they propagate to the caller's own catch posture (the runner's single
|
|
76
|
+
* catch site for the script path; extraction wraps inside its hooks).
|
|
77
|
+
*/
|
|
78
|
+
export interface RetryLoopHooks<T, H> {
|
|
79
|
+
/** Produce attempt `n` (0-based; 0 = the initial production). */
|
|
80
|
+
produce(attempt: number): Promise<{ kind: "ok"; value: T } | { kind: "aborted"; abort: H }>;
|
|
81
|
+
/** Validate one produced value. */
|
|
82
|
+
validate(value: T): Promise<{ kind: "ok"; result: ValidationResult } | { kind: "aborted"; abort: H }>;
|
|
83
|
+
/** Between a failed validation and the next produce. `attempt` is 1-based. */
|
|
84
|
+
onRetry(
|
|
85
|
+
attempt: number,
|
|
86
|
+
failures: SchemaValidationFailure[],
|
|
87
|
+
): Promise<{ kind: "ok" } | { kind: "aborted"; abort: H }>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type RetryLoopOutcome<T, H> =
|
|
91
|
+
| { kind: "ok"; value: T }
|
|
92
|
+
| { kind: "exhausted"; failures: SchemaValidationFailure[] }
|
|
93
|
+
| { kind: "aborted"; abort: H };
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* THE produce → validate → retry policy loop, shared by the skill path
|
|
97
|
+
* (extraction.ts — re-prompts the agent between attempts) and the script
|
|
98
|
+
* path (script-stage.ts — re-invokes the function). One structure: produce,
|
|
99
|
+
* validate, and while invalid — stop on `failFast` or a spent budget
|
|
100
|
+
* (`"exhausted"`), otherwise fire the retry hook and go again. Total
|
|
101
|
+
* productions are bounded by `maxRetries + 1`.
|
|
102
|
+
*/
|
|
103
|
+
export async function runValidationRetryLoop<T, H>(
|
|
104
|
+
policy: { maxRetries: number; failFast: boolean },
|
|
105
|
+
hooks: RetryLoopHooks<T, H>,
|
|
106
|
+
): Promise<RetryLoopOutcome<T, H>> {
|
|
107
|
+
let attempt = 0;
|
|
108
|
+
let produced = await hooks.produce(attempt);
|
|
109
|
+
if (produced.kind !== "ok") return { kind: "aborted", abort: produced.abort };
|
|
110
|
+
let validation = await hooks.validate(produced.value);
|
|
111
|
+
if (validation.kind !== "ok") return { kind: "aborted", abort: validation.abort };
|
|
112
|
+
|
|
113
|
+
while (!validation.result.valid) {
|
|
114
|
+
if (policy.failFast || attempt >= policy.maxRetries) {
|
|
115
|
+
return { kind: "exhausted", failures: validation.result.failures };
|
|
116
|
+
}
|
|
117
|
+
attempt++;
|
|
118
|
+
const retried = await hooks.onRetry(attempt, validation.result.failures);
|
|
119
|
+
if (retried.kind !== "ok") return { kind: "aborted", abort: retried.abort };
|
|
120
|
+
produced = await hooks.produce(attempt);
|
|
121
|
+
if (produced.kind !== "ok") return { kind: "aborted", abort: produced.abort };
|
|
122
|
+
validation = await hooks.validate(produced.value);
|
|
123
|
+
if (validation.kind !== "ok") return { kind: "aborted", abort: validation.abort };
|
|
124
|
+
}
|
|
125
|
+
return { kind: "ok", value: produced.value };
|
|
126
|
+
}
|
|
52
127
|
|
|
53
128
|
// ---------------------------------------------------------------------------
|
|
54
129
|
// Validation
|
|
@@ -59,7 +134,7 @@ export const MIN_VALIDATION_RETRY_TIMEOUT_MS = 1_000;
|
|
|
59
134
|
* to return synchronously or as a Promise; this function mirrors that —
|
|
60
135
|
* callers must `await` the result. Both seams that drive validation
|
|
61
136
|
* (`retryUntilValid` in extraction.ts and `ensureInputValid` in
|
|
62
|
-
* stage
|
|
137
|
+
* run-stage.ts) are async, so awaiting a sync value is free (one
|
|
63
138
|
* microtask) and async schemas (I/O-backed checks, async-by-default libs
|
|
64
139
|
* like ArkType) round-trip without a sync-only escape hatch.
|
|
65
140
|
*/
|