@juicesharp/rpiv-workflow 2.3.0 → 2.4.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/docs/embedding.md +1 -1
- package/docs/workflow-basics.md +1 -1
- package/loop-kinds.ts +122 -79
- package/loop.ts +22 -5
- package/package.json +2 -2
- package/runner/chain-advance.ts +28 -12
- package/runner/preflight.ts +17 -14
- package/runner/resume.ts +198 -133
- package/runner/run-stage.ts +45 -27
- package/sessions/extraction.ts +117 -66
- package/sessions/sessions.ts +66 -32
- package/validate/stage-rules.ts +42 -17
package/sessions/extraction.ts
CHANGED
|
@@ -234,12 +234,115 @@ function shouldValidateOutput(s: StageSessionContext, output: Output): boolean {
|
|
|
234
234
|
return !!(effectiveOutputSchemaOf(s.stage, s.stageName, s.skillContracts) && output.data !== undefined);
|
|
235
235
|
}
|
|
236
236
|
|
|
237
|
-
interface RetryDeps {
|
|
237
|
+
export interface RetryDeps {
|
|
238
238
|
outcome: Outcome;
|
|
239
239
|
collectCtx: CollectContext;
|
|
240
240
|
finalize: (parts: { kind: string; artifacts: readonly Artifact[]; data: unknown }) => Output;
|
|
241
241
|
}
|
|
242
242
|
|
|
243
|
+
/**
|
|
244
|
+
* Validation-retry mechanism-1 gate: did the worktree stay byte-identical
|
|
245
|
+
* between the failed validate and the post-fix re-read? An UNCHANGED digest
|
|
246
|
+
* means the agent's fix touched nothing observable (tracked files OR
|
|
247
|
+
* gitignored artifacts under `.rpiv/artifacts/`, both hashed by
|
|
248
|
+
* `computeWorktreeDigest`), so the next `produce` → `validate` cycle would
|
|
249
|
+
* re-run the same failing validation — fail fast instead of looping.
|
|
250
|
+
*
|
|
251
|
+
* An `undefined` baseline (non-repo / git missing — `resolveDigest` degrades
|
|
252
|
+
* to `undefined`) ALWAYS proceeds: degrade on a missing signal, never skip.
|
|
253
|
+
*/
|
|
254
|
+
export const worktreeUnchangedSince = (baselineDigest: string | undefined, s: StageSessionContext): boolean =>
|
|
255
|
+
baselineDigest !== undefined && resolveDigest(s.worktreeDigest, s.cwd) === baselineDigest;
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* The retry-loop `produce` hook: re-production of the stage's output.
|
|
259
|
+
*
|
|
260
|
+
* `attempt === 0` is the fast-path — the initial output was already produced
|
|
261
|
+
* and contract-checked before the retry loop opened (`produceAndValidateOutput`
|
|
262
|
+
* ran `runOutcome` + `enforceCompletionContract` once), so it returns the
|
|
263
|
+
* `initial` Output WITHOUT re-collection or re-checking the contract.
|
|
264
|
+
*
|
|
265
|
+
* `attempt > 0` re-reads the branch (the agent's fix landed as new messages),
|
|
266
|
+
* re-runs the collector → parser pipeline (`runOutcome`), and re-checks the
|
|
267
|
+
* completion contract. A fatal collector/parser/contract result maps onto
|
|
268
|
+
* the engine's `{ kind: "aborted" }` arm (the loop halts with that message).
|
|
269
|
+
*/
|
|
270
|
+
export async function produceAttempt(
|
|
271
|
+
ctx: WorkflowSessionContext,
|
|
272
|
+
s: StageSessionContext,
|
|
273
|
+
deps: RetryDeps,
|
|
274
|
+
initial: Output,
|
|
275
|
+
attempt: number,
|
|
276
|
+
): Promise<{ kind: "ok"; value: Output } | { kind: "aborted"; abort: Fatal }> {
|
|
277
|
+
if (attempt === 0) return { kind: "ok", value: initial };
|
|
278
|
+
const retryBranch = readBranch(ctx);
|
|
279
|
+
const retryCtx: CollectContext = { ...deps.collectCtx, branch: retryBranch };
|
|
280
|
+
const reRun = await runOutcome(deps.outcome, retryCtx, deps.finalize);
|
|
281
|
+
if (reRun.kind === "fatal") return { kind: "aborted", abort: reRun };
|
|
282
|
+
const contract = enforceCompletionContract(s.stage, s.skill, reRun.output);
|
|
283
|
+
if (contract.kind === "fatal") return { kind: "aborted", abort: contract };
|
|
284
|
+
return { kind: "ok", value: contract.output };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* The retry-loop `validate` hook: run the output schema against the produced
|
|
289
|
+
* data, guarded by `timeoutMs` (the same `validateTimeoutMs` budget that
|
|
290
|
+
* bounds `askAgentToFix`). Maps `validateOrFatal`'s `Fatal` outcome (a
|
|
291
|
+
* thrown/rejected/timed-out schema) onto the engine's `{ kind: "aborted" }`
|
|
292
|
+
* arm so a schema failure halts the loop instead of re-prompting the agent.
|
|
293
|
+
*/
|
|
294
|
+
export async function validateOutput(
|
|
295
|
+
schema: StageSchema,
|
|
296
|
+
skill: string,
|
|
297
|
+
timeoutMs: number,
|
|
298
|
+
output: Output,
|
|
299
|
+
): Promise<{ kind: "ok"; result: ValidationResult } | { kind: "aborted"; abort: Fatal }> {
|
|
300
|
+
const validation = await validateOrFatal(schema, output.data, skill, timeoutMs);
|
|
301
|
+
if (validation.kind === "fatal") return { kind: "aborted", abort: validation };
|
|
302
|
+
return { kind: "ok", result: validation.result };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* The retry-loop `onRetry` hook: fired between a failed validate and the next
|
|
307
|
+
* produce (`attempt` is 1-based). Sequence:
|
|
308
|
+
* 1. Capture the worktree digest AT THE FAILED VALIDATE — nothing mutates
|
|
309
|
+
* the tree between `validate` failing and `onRetry` opening, so this
|
|
310
|
+
* equals the digest at the failure. Captured per-retry (NOT once before
|
|
311
|
+
* the loop): a later retry's "before" state is the tree AFTER the prior
|
|
312
|
+
* retry's fix, so a single pre-loop baseline would compare against a
|
|
313
|
+
* stale tree and false-abort every later retry.
|
|
314
|
+
* 2. Fire `onStageRetry` before the agent is re-prompted (the ref shares the
|
|
315
|
+
* activation's allocator number via `currentStageRef` so listeners
|
|
316
|
+
* correlate retry ↔ end; graph position `stageIndex + 1` diverges past
|
|
317
|
+
* any loop).
|
|
318
|
+
* 3. Re-prompt the agent (`askAgentToFix`); a throw becomes an abort.
|
|
319
|
+
* 4. Gate on `worktreeUnchangedSince` — if the fix changed nothing
|
|
320
|
+
* observable, abort with the unchanged-worktree message instead of
|
|
321
|
+
* re-running the same failing validation.
|
|
322
|
+
*/
|
|
323
|
+
export async function handleRetry(
|
|
324
|
+
ctx: WorkflowSessionContext,
|
|
325
|
+
s: StageSessionContext,
|
|
326
|
+
attempt: number,
|
|
327
|
+
failures: SchemaValidationFailure[],
|
|
328
|
+
timeoutMs: number,
|
|
329
|
+
): Promise<{ kind: "ok" } | { kind: "aborted"; abort: Fatal }> {
|
|
330
|
+
const baselineDigest = resolveDigest(s.worktreeDigest, s.cwd);
|
|
331
|
+
await s.lifecycle.fire(ctx, "onStageRetry", currentStageRef(s), attempt, lifecycleCtxFromSession(s));
|
|
332
|
+
try {
|
|
333
|
+
await askAgentToFix(ctx, s, attempt, failures, timeoutMs);
|
|
334
|
+
} catch (e) {
|
|
335
|
+
return { kind: "aborted", abort: { kind: "fatal", message: formatError(e) } };
|
|
336
|
+
}
|
|
337
|
+
if (worktreeUnchangedSince(baselineDigest, s)) {
|
|
338
|
+
return {
|
|
339
|
+
kind: "aborted",
|
|
340
|
+
abort: { kind: "fatal", message: ERR_VALIDATE_RETRY_UNCHANGED(s.skill) },
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
return { kind: "ok" };
|
|
344
|
+
}
|
|
345
|
+
|
|
243
346
|
async function retryUntilValid(
|
|
244
347
|
ctx: WorkflowSessionContext,
|
|
245
348
|
s: StageSessionContext,
|
|
@@ -260,78 +363,26 @@ async function retryUntilValid(
|
|
|
260
363
|
MAX_VALIDATION_RETRY_TIMEOUT_MS,
|
|
261
364
|
);
|
|
262
365
|
|
|
263
|
-
// The retry policy
|
|
366
|
+
// The retry policy delegates to the SHARED `runValidationRetryLoop` engine
|
|
264
367
|
// (validate-output.ts) — the same produce → validate → retry structure the
|
|
265
|
-
// script path runs. The
|
|
266
|
-
//
|
|
267
|
-
//
|
|
268
|
-
//
|
|
269
|
-
//
|
|
270
|
-
//
|
|
271
|
-
// - `
|
|
272
|
-
//
|
|
273
|
-
// - `onRetry` → `onStageRetry` + `askAgentToFix`; a throw becomes an abort.
|
|
368
|
+
// script path runs. The three hooks are short named delegations:
|
|
369
|
+
// - `produceAttempt` — `produce(0)` returns the already-produced `initial`;
|
|
370
|
+
// `produce(n>0)` re-reads the branch, re-runs the collector/parser, and
|
|
371
|
+
// re-checks the completion contract.
|
|
372
|
+
// - `validateOutput` — `validateOrFatal`, mapping its `Fatal` onto the
|
|
373
|
+
// engine's `{ kind: "aborted" }` abort arm.
|
|
374
|
+
// - `handleRetry` — `onStageRetry` + `askAgentToFix`; a throw becomes an
|
|
375
|
+
// abort, and the `worktreeUnchangedSince` gate aborts a no-op fix.
|
|
274
376
|
// `failFast` mirrors the script path's stop-flag polarity —
|
|
275
377
|
// `(onInvalid ?? "retry") === "halt"` is byte-identical to today's
|
|
276
378
|
// `onInvalid !== "halt"` loop-continue condition, just expressed as the
|
|
277
379
|
// engine's stop flag. Total productions stay bounded by `maxRetries + 1`.
|
|
278
380
|
const outcome = await runValidationRetryLoop<Output, Fatal>(
|
|
381
|
+
{ maxRetries, failFast: (s.stage.onInvalid ?? "retry") === "halt" },
|
|
279
382
|
{
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
{
|
|
284
|
-
produce: async (attempt) => {
|
|
285
|
-
// `produce(0)` returns the already-produced initial — no re-collection,
|
|
286
|
-
// no contract re-check (both ran before the loop opened).
|
|
287
|
-
if (attempt === 0) return { kind: "ok", value: initial };
|
|
288
|
-
const retryBranch = readBranch(ctx);
|
|
289
|
-
const retryCtx: CollectContext = { ...deps.collectCtx, branch: retryBranch };
|
|
290
|
-
const reRun = await runOutcome(deps.outcome, retryCtx, deps.finalize);
|
|
291
|
-
if (reRun.kind === "fatal") return { kind: "aborted", abort: reRun };
|
|
292
|
-
const contract = enforceCompletionContract(s.stage, s.skill, reRun.output);
|
|
293
|
-
if (contract.kind === "fatal") return { kind: "aborted", abort: contract };
|
|
294
|
-
return { kind: "ok", value: contract.output };
|
|
295
|
-
},
|
|
296
|
-
validate: async (output) => {
|
|
297
|
-
const validation = await validateOrFatal(schema, output.data, s.skill, timeoutMs);
|
|
298
|
-
if (validation.kind === "fatal") return { kind: "aborted", abort: validation };
|
|
299
|
-
return { kind: "ok", result: validation.result };
|
|
300
|
-
},
|
|
301
|
-
onRetry: async (attempt, failures) => {
|
|
302
|
-
// Validation-retry mechanism-1: capture the worktree digest AT THE FAILED
|
|
303
|
-
// VALIDATE before the agent is re-prompted. Nothing mutates the tree
|
|
304
|
-
// between `validate` failing and `onRetry` opening, so this equals
|
|
305
|
-
// the digest at the failure. Captured per-retry (NOT once before the
|
|
306
|
-
// loop): a later retry's "before" state is the tree AFTER the prior
|
|
307
|
-
// retry's fix, so a single pre-loop baseline would compare against a
|
|
308
|
-
// stale tree and false-abort every later retry.
|
|
309
|
-
const baselineDigest = resolveDigest(s.worktreeDigest, s.cwd);
|
|
310
|
-
// onStageRetry fires before the agent is re-prompted; `attempt` is 1-based.
|
|
311
|
-
// Ref shares the activation's ALLOCATOR number (currentStageRef) so
|
|
312
|
-
// listeners can correlate this retry with the end/error event it
|
|
313
|
-
// belongs to — graph position (`stageIndex + 1`) diverges past any loop.
|
|
314
|
-
await s.lifecycle.fire(ctx, "onStageRetry", currentStageRef(s), attempt, lifecycleCtxFromSession(s));
|
|
315
|
-
try {
|
|
316
|
-
await askAgentToFix(ctx, s, attempt, failures, timeoutMs);
|
|
317
|
-
} catch (e) {
|
|
318
|
-
return { kind: "aborted", abort: { kind: "fatal", message: formatError(e) } };
|
|
319
|
-
}
|
|
320
|
-
// The agent was asked to fix but changed nothing observable in the
|
|
321
|
-
// worktree (tracked files OR gitignored artifacts under
|
|
322
|
-
// `.rpiv/artifacts/`, both hashed by `computeWorktreeDigest`), so the
|
|
323
|
-
// `produce(attempt)` re-read + re-validate cycle would just re-run the
|
|
324
|
-
// same failing validation — fail fast instead of looping. An unchanged
|
|
325
|
-
// `undefined` digest (non-repo / git missing) ALWAYS proceeds: degrade
|
|
326
|
-
// on a missing signal, never skip.
|
|
327
|
-
if (baselineDigest !== undefined && resolveDigest(s.worktreeDigest, s.cwd) === baselineDigest) {
|
|
328
|
-
return {
|
|
329
|
-
kind: "aborted",
|
|
330
|
-
abort: { kind: "fatal", message: ERR_VALIDATE_RETRY_UNCHANGED(s.skill) },
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
return { kind: "ok" };
|
|
334
|
-
},
|
|
383
|
+
produce: (attempt) => produceAttempt(ctx, s, deps, initial, attempt),
|
|
384
|
+
validate: (output) => validateOutput(schema, s.skill, timeoutMs, output),
|
|
385
|
+
onRetry: (attempt, failures) => handleRetry(ctx, s, attempt, failures, timeoutMs),
|
|
335
386
|
},
|
|
336
387
|
);
|
|
337
388
|
|
package/sessions/sessions.ts
CHANGED
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
26
|
import { WorkflowAbortError } from "../internal-utils.js";
|
|
27
|
+
import type { SessionRef } from "../state/index.js";
|
|
27
28
|
import { type BranchEntry, classifyStop, readBranch, readSessionRef, type StopSignal } from "../transcript.js";
|
|
28
29
|
import type { StageSessionContext, WorkflowHostContext, WorkflowSessionContext } from "../types.js";
|
|
29
30
|
import { bashStrikesRemaining, bashTimeoutSteeringMessage, consumeBashStrike } from "./bash-strikes.js";
|
|
@@ -100,36 +101,10 @@ export async function postStage(
|
|
|
100
101
|
): Promise<void> {
|
|
101
102
|
const session = readSessionRef(child, offset);
|
|
102
103
|
const outcome = readSessionOutcome(child, offset);
|
|
103
|
-
// Abort
|
|
104
|
-
//
|
|
105
|
-
//
|
|
106
|
-
|
|
107
|
-
// (a) no `collected:true` row is written (else the resume fold marks the unit
|
|
108
|
-
// "don't re-dispatch" → permanent work loss), (b) the parallel fold's
|
|
109
|
-
// `isAbortError` branch leaves the slot unfilled, and (c) resume re-dispatches
|
|
110
|
-
// the unit cleanly.
|
|
111
|
-
//
|
|
112
|
-
// A genuine run/user abort (`s.signal` fired) ALWAYS re-dispatches on resume, so it
|
|
113
|
-
// takes the throw unconditionally. An `aborted` stop with the signal cold is examined
|
|
114
|
-
// for a watchdog tool-timeout (`child.toolTimeout`): the host aborted a runaway bash,
|
|
115
|
-
// which must route to the soft-halt gate (collect-all unit survives; else terminal
|
|
116
|
-
// fail) — NOT WorkflowAbortError, which would re-run the same runaway command on resume.
|
|
117
|
-
if (s.signal?.aborted) throw new WorkflowAbortError();
|
|
118
|
-
if (outcome.stop === "aborted") {
|
|
119
|
-
const timeout = child.toolTimeout?.();
|
|
120
|
-
if (timeout) {
|
|
121
|
-
// Strike-based recovery: a watchdog tool-timeout is a recoverable tool
|
|
122
|
-
// event INSIDE the live child. While strikes remain, the single-caller
|
|
123
|
-
// helper resets → re-prompts → tail-recurses postStage (offset threaded
|
|
124
|
-
// verbatim); exhaustion falls through to the UNCHANGED soft-halt/terminal
|
|
125
|
-
// seam below, where the failure-row writers (memo + death-scene) fire for free.
|
|
126
|
-
if (consumeBashStrike(s, timeout.reason)) {
|
|
127
|
-
return retryStageAfterBashStrike(observerCtx, child, s, offset, timeout.reason);
|
|
128
|
-
}
|
|
129
|
-
return haltStageOrSoftHalt(observerCtx, s, { kind: "timeout", reason: timeout.reason }, session);
|
|
130
|
-
}
|
|
131
|
-
throw new WorkflowAbortError();
|
|
132
|
-
}
|
|
104
|
+
// Abort classification and strike recovery are owned by a single helper that
|
|
105
|
+
// returns "continue" when it has handled the turn (strike retry or soft-halt),
|
|
106
|
+
// so postStage reaches its happy-path switch only for a clean stop.
|
|
107
|
+
if ((await classifyAndHandleAbort(observerCtx, child, s, outcome, session, offset)) === "continue") return;
|
|
133
108
|
// Every halt below routes through the single `haltStageOrSoftHalt` gate: a
|
|
134
109
|
// fanout unit marked `collectAll` records a NON-terminal failed row + a sentinel
|
|
135
110
|
// slot instead of halting the run; everything else takes the arm's fail-fast
|
|
@@ -157,7 +132,66 @@ export async function postStage(
|
|
|
157
132
|
}
|
|
158
133
|
|
|
159
134
|
// ===========================================================================
|
|
160
|
-
//
|
|
135
|
+
// ABORT CLASSIFIER — postStage's single abort dispatch + its predicates
|
|
136
|
+
// ===========================================================================
|
|
137
|
+
|
|
138
|
+
/** An `aborted` stop classification (the SDK resolves `prompt()` with `stopReason:"aborted"`). */
|
|
139
|
+
const isAbortedStop = (outcome: SessionOutcome): boolean => outcome.stop === "aborted";
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The watchdog tool-timeout verdict, if any. Read ONCE and returned so the
|
|
143
|
+
* `reason` threads through without a second read (the strike arm clears the
|
|
144
|
+
* verdict, so a re-read would see `undefined`).
|
|
145
|
+
*/
|
|
146
|
+
const watchdogTimeoutOf = (child: WorkflowSessionContext): { reason: string } | undefined => child.toolTimeout?.();
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Classify and route an abort. Abort surfaces as a STOP CLASSIFICATION, not a
|
|
150
|
+
* promise rejection: `session.abort()` makes the SDK RESOLVE `prompt()` with a
|
|
151
|
+
* `stopReason:"aborted"` transcript message, so an aborted in-flight child runs
|
|
152
|
+
* straight into here. A genuine abort throws BEFORE haltStage/softHaltUnit/any
|
|
153
|
+
* row write so: (a) no `collected:true` row is written (else the resume fold
|
|
154
|
+
* marks the unit "don't re-dispatch" → permanent work loss), (b) the parallel
|
|
155
|
+
* fold's `isAbortError` branch leaves the slot unfilled, and (c) resume
|
|
156
|
+
* re-dispatches the unit cleanly.
|
|
157
|
+
*
|
|
158
|
+
* Returns `"continue"` once the turn is owned (strike retry or soft-halt),
|
|
159
|
+
* telling the caller to stop; returns `void` (no abort) so the caller proceeds
|
|
160
|
+
* to the happy-path switch.
|
|
161
|
+
*
|
|
162
|
+
* Exported ONLY for the co-located direct tests (`sessions/sessions.test.ts` —
|
|
163
|
+
* a non-exported member cannot be imported there; the `extraction.ts` retry
|
|
164
|
+
* hooks set the precedent). Absent from the `sessions/index.ts` barrel, so the
|
|
165
|
+
* package surface is unchanged.
|
|
166
|
+
*/
|
|
167
|
+
export async function classifyAndHandleAbort(
|
|
168
|
+
observerCtx: WorkflowHostContext,
|
|
169
|
+
child: WorkflowSessionContext,
|
|
170
|
+
s: StageSessionContext,
|
|
171
|
+
outcome: SessionOutcome,
|
|
172
|
+
session: SessionRef | null,
|
|
173
|
+
offset: number | undefined,
|
|
174
|
+
): Promise<"continue" | undefined> {
|
|
175
|
+
// Level 1 — genuine run/user abort: the signal fired, so always re-dispatch on resume.
|
|
176
|
+
if (s.signal?.aborted) throw new WorkflowAbortError();
|
|
177
|
+
// Level 2 — not an abort stop: hand back to the caller's happy-path switch.
|
|
178
|
+
if (!isAbortedStop(outcome)) return;
|
|
179
|
+
// Level 3 — examine a watchdog tool-timeout; an aborted stop with the signal cold
|
|
180
|
+
// and no watchdog is a genuine abort that re-dispatches on resume.
|
|
181
|
+
const timeout = watchdogTimeoutOf(child);
|
|
182
|
+
if (!timeout) throw new WorkflowAbortError();
|
|
183
|
+
// Level 4 — a watchdog tool-timeout is recoverable: consume a strike and retry, or
|
|
184
|
+
// route exhaustion through the soft-halt gate (collect-all survives; else terminal).
|
|
185
|
+
if (consumeBashStrike(s, timeout.reason)) {
|
|
186
|
+
await retryStageAfterBashStrike(observerCtx, child, s, offset, timeout.reason);
|
|
187
|
+
} else {
|
|
188
|
+
await haltStageOrSoftHalt(observerCtx, s, { kind: "timeout", reason: timeout.reason }, session);
|
|
189
|
+
}
|
|
190
|
+
return "continue";
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// ===========================================================================
|
|
194
|
+
// STRIKE-ARM RECOVERY — single-caller helper for the abort classifier's watchdog arm
|
|
161
195
|
// ===========================================================================
|
|
162
196
|
|
|
163
197
|
/**
|
|
@@ -166,7 +200,7 @@ export async function postStage(
|
|
|
166
200
|
* verbatim — the exact shape the continue body uses). Called ONLY when
|
|
167
201
|
* `consumeBashStrike(s, reason)` returned true, so this helper performs the
|
|
168
202
|
* reset → re-prompt → tail-recurse sequence byte-identically to the inline arm
|
|
169
|
-
* it replaces; strike exhaustion stays routed by the caller (`
|
|
203
|
+
* it replaces; strike exhaustion stays routed by the caller (`classifyAndHandleAbort`) to
|
|
170
204
|
* the UNCHANGED `haltStageOrSoftHalt({ kind: "timeout" })` seam, where the
|
|
171
205
|
* failure-row writers (memo + death-scene artifact) fire for free.
|
|
172
206
|
*/
|
package/validate/stage-rules.ts
CHANGED
|
@@ -11,7 +11,16 @@
|
|
|
11
11
|
* (different dependency footprint entirely).
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
type FanoutLoop,
|
|
16
|
+
LOOP_KINDS,
|
|
17
|
+
type LoopDef,
|
|
18
|
+
ON_INVALID_VALUES,
|
|
19
|
+
SESSION_POLICIES,
|
|
20
|
+
STAGE_KINDS,
|
|
21
|
+
type StageDef,
|
|
22
|
+
type Workflow,
|
|
23
|
+
} from "../api.js";
|
|
15
24
|
import { type AnyJudge, isPanel } from "../judge.js";
|
|
16
25
|
import {
|
|
17
26
|
forEachJudgeChannel,
|
|
@@ -70,30 +79,17 @@ function checkLoopInvariants(stage: StageDef, name: string, report: ReportFn): v
|
|
|
70
79
|
if (loop.max !== undefined && (!Number.isInteger(loop.max) || loop.max < 1)) {
|
|
71
80
|
report("loop-max-invalid", { max: loop.max });
|
|
72
81
|
}
|
|
73
|
-
if (
|
|
74
|
-
loop.kind === "fanout" &&
|
|
75
|
-
loop.concurrency !== undefined &&
|
|
76
|
-
(!Number.isInteger(loop.concurrency) || loop.concurrency < 1)
|
|
77
|
-
) {
|
|
82
|
+
if (isInvalidConcurrency(loop)) {
|
|
78
83
|
report("loop-concurrency-invalid", { concurrency: loop.concurrency });
|
|
79
84
|
}
|
|
80
|
-
if (
|
|
81
|
-
loop.kind === "fanout" &&
|
|
82
|
-
loop.depArtifactFlag !== undefined &&
|
|
83
|
-
(typeof loop.depArtifactFlag !== "string" || loop.depArtifactFlag.trim().length === 0)
|
|
84
|
-
) {
|
|
85
|
+
if (isInvalidDepArtifactFlag(loop)) {
|
|
85
86
|
report("loop-dep-flag-invalid", { depArtifactFlag: loop.depArtifactFlag });
|
|
86
87
|
}
|
|
87
88
|
// Pull loops + assess run the stage's outcome collector per unit.
|
|
88
89
|
if ((loop.kind === "iterate" || loop.kind === "assess") && stage.kind !== "produces") {
|
|
89
90
|
report("loop-requires-produces", { kind: loop.kind });
|
|
90
91
|
}
|
|
91
|
-
|
|
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) {
|
|
92
|
+
if (requiresOutcomeName(loop, stage) && !stage.outcome?.name) {
|
|
97
93
|
report("loop-outcome-name-required");
|
|
98
94
|
}
|
|
99
95
|
|
|
@@ -129,6 +125,35 @@ function checkLoopInvariants(stage: StageDef, name: string, report: ReportFn): v
|
|
|
129
125
|
checkVerdictChannels(slot, name, stage, "assess-verdict-channel-collision", report);
|
|
130
126
|
}
|
|
131
127
|
|
|
128
|
+
/**
|
|
129
|
+
* A fanout's `concurrency` ceiling is present and out of range — not an
|
|
130
|
+
* integer or below 1. The type guard narrows to `FanoutLoop &
|
|
131
|
+
* { concurrency: number }` so the report call reads the field without a cast
|
|
132
|
+
* or non-null assertion.
|
|
133
|
+
*/
|
|
134
|
+
const isInvalidConcurrency = (loop: LoopDef): loop is FanoutLoop & { concurrency: number } =>
|
|
135
|
+
loop.kind === "fanout" &&
|
|
136
|
+
loop.concurrency !== undefined &&
|
|
137
|
+
(!Number.isInteger(loop.concurrency) || loop.concurrency < 1);
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A fanout's `depArtifactFlag` is present but not a non-empty string. The type
|
|
141
|
+
* guard narrows to `FanoutLoop & { depArtifactFlag: string }` so the report
|
|
142
|
+
* call reads the field without a cast or non-null assertion.
|
|
143
|
+
*/
|
|
144
|
+
const isInvalidDepArtifactFlag = (loop: LoopDef): loop is FanoutLoop & { depArtifactFlag: string } =>
|
|
145
|
+
loop.kind === "fanout" &&
|
|
146
|
+
loop.depArtifactFlag !== undefined &&
|
|
147
|
+
(typeof loop.depArtifactFlag !== "string" || loop.depArtifactFlag.trim().length === 0);
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* A stable named slot: iterate and assess always (every unit/round runs the
|
|
151
|
+
* produces collector), fanout when COLLECTING (produces kind) — the decorated
|
|
152
|
+
* display string must never split the accumulation slot.
|
|
153
|
+
*/
|
|
154
|
+
const requiresOutcomeName = (loop: LoopDef, stage: StageDef): boolean =>
|
|
155
|
+
loop.kind === "iterate" || loop.kind === "assess" || (loop.kind === "fanout" && stage.kind === "produces");
|
|
156
|
+
|
|
132
157
|
/**
|
|
133
158
|
* Verdict-channel collisions for a judge SLOT (`AnyJudge`) — workflow-level
|
|
134
159
|
* because it needs the producer's publish identity
|