@juicesharp/rpiv-workflow 2.9.0 → 2.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/audit-ctx.ts +7 -0
- package/audit.ts +8 -0
- package/command-run.ts +84 -17
- package/command.ts +171 -22
- package/docs/embedding.md +4 -3
- package/docs/workflow-authoring.md +5 -4
- package/docs/workflow-basics.md +7 -4
- package/failure-memos.ts +11 -2
- package/index.ts +9 -1
- package/loop-def.ts +17 -0
- package/loop-kinds.ts +20 -1
- package/loop-parallel.ts +71 -24
- package/loops/constructors.ts +7 -0
- package/messages.ts +72 -5
- package/outcomes/collectors/directory-path.ts +4 -1
- package/outcomes/collectors/text-scan.ts +77 -13
- package/outcomes/collectors/transcript-path.ts +24 -1
- package/outcomes/collectors/url.ts +4 -1
- package/output-spec.ts +9 -0
- package/output.ts +22 -3
- package/package.json +2 -2
- package/registration.ts +2 -0
- package/runner/chain-advance.ts +140 -26
- package/runner/index.ts +7 -1
- package/runner/resume.ts +102 -9
- package/runner/run-context.ts +57 -3
- package/runner/runner.ts +21 -1
- package/sessions/extraction.ts +1 -0
- package/sessions/halt-routing.ts +5 -1
- package/stage-def.ts +54 -4
- package/state/reads.ts +14 -1
- package/state/state.ts +47 -6
- package/transcript.ts +30 -11
- package/types.ts +53 -3
- package/validate/issue.ts +10 -0
- package/validate/stage-rules.ts +30 -0
package/stage-def.ts
CHANGED
|
@@ -59,6 +59,20 @@ export type StageSchema<TIn = unknown, TOut = TIn> = StandardSchemaV1<TIn, TOut>
|
|
|
59
59
|
export const STAGE_KINDS = ["produces", "side-effect"] as const;
|
|
60
60
|
export type StageKind = (typeof STAGE_KINDS)[number];
|
|
61
61
|
|
|
62
|
+
/**
|
|
63
|
+
* The backward-jump guard's progress verdicts — what a stage's optional
|
|
64
|
+
* `progress` hook returns per decision-edge re-entry (see `StageDefBase.progress`).
|
|
65
|
+
* `"improved"` WAIVES the re-entry (the per-destination budget is untouched;
|
|
66
|
+
* the chain re-dispatches the stage); `"unchanged"` / `"regressed"` /
|
|
67
|
+
* `"unknown"` COUNT exactly as a hook-less re-entry does. The `as const`
|
|
68
|
+
* array is the single source of truth (same idiom as `STAGE_KINDS`): the
|
|
69
|
+
* literal-union type is derived from it, and the guard normalizes any
|
|
70
|
+
* off-union return (a jiti-loaded literal erased the union) to `"unknown"`
|
|
71
|
+
* via one membership check against the same array.
|
|
72
|
+
*/
|
|
73
|
+
export const PROGRESS_VALUES = ["improved", "unchanged", "regressed", "unknown"] as const;
|
|
74
|
+
export type ProgressValue = (typeof PROGRESS_VALUES)[number];
|
|
75
|
+
|
|
62
76
|
/**
|
|
63
77
|
* - `"fresh"` — the stage runs in a brand-new detached child session.
|
|
64
78
|
* - `"continue"` — the stage FORKS its predecessor's persisted child session
|
|
@@ -226,6 +240,27 @@ interface StageDefBase<TIn = unknown, TOut = unknown> {
|
|
|
226
240
|
* own outcome) — `validateWorkflow` warns when set there.
|
|
227
241
|
*/
|
|
228
242
|
inheritsArtifacts?: boolean;
|
|
243
|
+
/**
|
|
244
|
+
* Optional backward-jump progress hook, consulted ONLY when a decision
|
|
245
|
+
* edge re-enters this stage (never on first visit). The guard awaits
|
|
246
|
+
* `(state) => "improved" | "unchanged" | "regressed" | "unknown"` (sync or
|
|
247
|
+
* async) and:
|
|
248
|
+
*
|
|
249
|
+
* - `"improved"` — WAIVES the re-entry: no budget consumed, the chain
|
|
250
|
+
* re-dispatches this stage exactly as a counted re-entry under the
|
|
251
|
+
* cap would;
|
|
252
|
+
* - anything else — the re-entry COUNTS against the per-destination
|
|
253
|
+
* cap, exactly as a hook-less stage's re-entry does today.
|
|
254
|
+
*
|
|
255
|
+
* The hook is an observation, never a halt surface: a throwing hook
|
|
256
|
+
* degrades to `"unknown"` (counts, never halts by itself); an off-union
|
|
257
|
+
* return normalizes to `"unknown"`; an absent hook means every re-entry
|
|
258
|
+
* counts. Declared on the base so all three dispatch arms (skill /
|
|
259
|
+
* script / prompt) carry it; composes with `loop` / `verify` / `reads`
|
|
260
|
+
* (no exclusion rules — a function-valued hook is the only shape rule,
|
|
261
|
+
* enforced at load as `progress-not-function`).
|
|
262
|
+
*/
|
|
263
|
+
progress?: (state: RunView) => ProgressValue | Promise<ProgressValue>;
|
|
229
264
|
}
|
|
230
265
|
|
|
231
266
|
/**
|
|
@@ -415,7 +450,14 @@ export function defineWorkflow(spec: Workflow): Workflow {
|
|
|
415
450
|
interface ProducesScriptOptions<TIn = unknown, TOut = unknown>
|
|
416
451
|
extends Pick<
|
|
417
452
|
ScriptStage<TIn, TOut>,
|
|
418
|
-
|
|
453
|
+
| "outputSchema"
|
|
454
|
+
| "inputSchema"
|
|
455
|
+
| "onInvalid"
|
|
456
|
+
| "maxRetries"
|
|
457
|
+
| "validateTimeoutMs"
|
|
458
|
+
| "inheritsArtifacts"
|
|
459
|
+
| "reads"
|
|
460
|
+
| "progress"
|
|
419
461
|
> {
|
|
420
462
|
run: ProducesScriptFn<string, TOut>;
|
|
421
463
|
}
|
|
@@ -427,7 +469,7 @@ interface ProducesScriptOptions<TIn = unknown, TOut = unknown>
|
|
|
427
469
|
* (they emit no data envelope), so the retry knobs don't apply.
|
|
428
470
|
*/
|
|
429
471
|
interface ActsScriptOptions<TIn = unknown>
|
|
430
|
-
extends Pick<ScriptStage<TIn, void>, "inputSchema" | "inheritsArtifacts" | "reads"> {
|
|
472
|
+
extends Pick<ScriptStage<TIn, void>, "inputSchema" | "inheritsArtifacts" | "reads" | "progress"> {
|
|
431
473
|
run: ActsScriptFn;
|
|
432
474
|
}
|
|
433
475
|
|
|
@@ -441,7 +483,15 @@ interface ActsScriptOptions<TIn = unknown>
|
|
|
441
483
|
interface ProducesPromptOptions<TIn = unknown, TOut = unknown>
|
|
442
484
|
extends Pick<
|
|
443
485
|
PromptStage<TIn, TOut>,
|
|
444
|
-
|
|
486
|
+
| "prompt"
|
|
487
|
+
| "outputSchema"
|
|
488
|
+
| "inputSchema"
|
|
489
|
+
| "onInvalid"
|
|
490
|
+
| "maxRetries"
|
|
491
|
+
| "validateTimeoutMs"
|
|
492
|
+
| "loop"
|
|
493
|
+
| "verify"
|
|
494
|
+
| "progress"
|
|
445
495
|
> {
|
|
446
496
|
outcome: Outcome;
|
|
447
497
|
/** `"continue"` makes this a follow-up turn on a session a prior stage populated. */
|
|
@@ -454,7 +504,7 @@ interface ProducesPromptOptions<TIn = unknown, TOut = unknown>
|
|
|
454
504
|
* variant: no `outcome` (nothing collected). For a collecting side-effect
|
|
455
505
|
* prompt stage, use the bare `acts({ prompt, outcome })` field form instead.
|
|
456
506
|
*/
|
|
457
|
-
interface ActsPromptOptions<TIn = unknown> extends Pick<PromptStage<TIn, void>, "prompt" | "inputSchema"> {
|
|
507
|
+
interface ActsPromptOptions<TIn = unknown> extends Pick<PromptStage<TIn, void>, "prompt" | "inputSchema" | "progress"> {
|
|
458
508
|
/** `"continue"` makes this a follow-up turn on a session a prior stage populated. */
|
|
459
509
|
sessionPolicy?: SessionPolicy;
|
|
460
510
|
}
|
package/state/reads.ts
CHANGED
|
@@ -265,6 +265,8 @@ function recapOutcomeOf(last: WorkflowStage): RunRecap["outcome"] {
|
|
|
265
265
|
* routing row as the trail's tail: static string edges never audit, a natural
|
|
266
266
|
* chain end appends its terminal stage row after any routing row, and a resume
|
|
267
267
|
* appends new stage rows behind a prior stop. Fail-soft via `readParsedRows`.
|
|
268
|
+
* The stop row this returns is EXCLUDED from `summarizeRun`'s `routingNotes` —
|
|
269
|
+
* the stopped refinement renders its note once, as `failureReason`.
|
|
268
270
|
*/
|
|
269
271
|
function trailingRoutingStop(cwd: string, runId: string): RoutingDecision | undefined {
|
|
270
272
|
const rows = readParsedRows(cwd, runId);
|
|
@@ -288,7 +290,11 @@ function trailingRoutingStop(cwd: string, runId: string): RoutingDecision | unde
|
|
|
288
290
|
* note-less trail still names WHERE the run stopped. The refinement only
|
|
289
291
|
* applies over a completed last stage row: a failed/aborted/cancelled tail
|
|
290
292
|
* keeps its own outcome + errMsg (and by write order such a tail follows any
|
|
291
|
-
* routing row anyway).
|
|
293
|
+
* routing row anyway). The recap also carries `routingNotes` — every
|
|
294
|
+
* note-bearing FORWARD routing row, verbatim in trail order (stop-row notes
|
|
295
|
+
* excluded; the stopped refinement renders those once as `failureReason`).
|
|
296
|
+
* Set only when non-empty, on EVERY outcome — a failed run may carry
|
|
297
|
+
* earlier-hop notes (a gate explained itself before a later stage blew up).
|
|
292
298
|
*/
|
|
293
299
|
export function summarizeRun(cwd: string, runId: string): RunRecap | undefined {
|
|
294
300
|
const stages = readAllStages(cwd, runId);
|
|
@@ -302,6 +308,13 @@ export function summarizeRun(cwd: string, runId: string): RunRecap | undefined {
|
|
|
302
308
|
workflow: header?.workflow,
|
|
303
309
|
};
|
|
304
310
|
if (outcome !== "completed" && last.errMsg !== undefined) recap.failureReason = last.errMsg;
|
|
311
|
+
// Route-note recap — rides EVERY outcome. Forward rows only; a stop row's
|
|
312
|
+
// note renders once, below, as the stopped-refinement failureReason — never
|
|
313
|
+
// twice. Set only when non-empty (absent, never []).
|
|
314
|
+
const routingNotes = readRoutingDecisions(cwd, runId).flatMap((r) =>
|
|
315
|
+
r.note !== undefined && r.decision !== ROUTED_STOP ? [r.note] : [],
|
|
316
|
+
);
|
|
317
|
+
if (routingNotes.length > 0) recap.routingNotes = routingNotes;
|
|
305
318
|
if (outcome === "completed") {
|
|
306
319
|
const stop = trailingRoutingStop(cwd, runId);
|
|
307
320
|
if (stop) {
|
package/state/state.ts
CHANGED
|
@@ -142,6 +142,28 @@ export interface WorkflowStage {
|
|
|
142
142
|
* other row (`undefined` is dropped by `JSON.stringify`).
|
|
143
143
|
*/
|
|
144
144
|
collected?: true;
|
|
145
|
+
/**
|
|
146
|
+
* The failed unit's label — present ONLY on a `collected: true` row, where
|
|
147
|
+
* `recordUnitHalt` writes it so the resume fold can thread it into the
|
|
148
|
+
* rebuilt `failedOutput` sentinel's `dimension` (live sentinel and resume
|
|
149
|
+
* twin stay byte-identical). An ADDITIVE optional field in the
|
|
150
|
+
* `bashTimeoutStrikes` style: absent ⇒ `JSON.stringify` drops it ⇒
|
|
151
|
+
* byte-identical row; the fold's shape-filtered readers ignore it, so no
|
|
152
|
+
* `STATE_SCHEMA_VERSION` bump.
|
|
153
|
+
*/
|
|
154
|
+
unitLabel?: string;
|
|
155
|
+
/**
|
|
156
|
+
* The failed attempt's 1-based dispatch ordinal — present ONLY on a
|
|
157
|
+
* `collected: true` row, where `recordUnitHalt` writes it (threaded from
|
|
158
|
+
* the parallel dispatcher's per-attempt count). Consumed by the resume
|
|
159
|
+
* fold's budget predicate (`foldFanoutRow` in runner/resume.ts): an
|
|
160
|
+
* under-budget ordinal leaves the slot unfilled so the unit re-dispatches
|
|
161
|
+
* while `retryHaltedUnits` budget remains; a final-attempt (or absent)
|
|
162
|
+
* ordinal folds the sentinel, exactly as before v3. THE schema-v3 delta —
|
|
163
|
+
* collected-row fold behavior is version-gated, so v1/v2 trails refuse
|
|
164
|
+
* resume rather than mis-replay.
|
|
165
|
+
*/
|
|
166
|
+
attemptOrdinal?: number;
|
|
145
167
|
}
|
|
146
168
|
|
|
147
169
|
/**
|
|
@@ -167,13 +189,21 @@ export interface LoopCapRow {
|
|
|
167
189
|
*
|
|
168
190
|
* v2 = parallel-fanout trails: completion rows are placed by `unitIndex` (not
|
|
169
191
|
* trail order), and a `collected:true` failed row's `errMsg` rebuilds a
|
|
170
|
-
* `failedOutput` sentinel.
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
* `
|
|
192
|
+
* `failedOutput` sentinel. Still true under v3.
|
|
193
|
+
*
|
|
194
|
+
* v3 = budget-aware collected rows: a `collected:true` row carries the failed
|
|
195
|
+
* attempt's 1-based `attemptOrdinal`, and the resume fold re-dispatches the
|
|
196
|
+
* unit while `retryHaltedUnits` budget remains (an under-budget collected row
|
|
197
|
+
* leaves its slot unfilled, exactly like a pending one) instead of folding
|
|
198
|
+
* its sentinel; the retry budget is fresh per resume invocation (ordinals
|
|
199
|
+
* restart at 1, bounded only by human-initiated resumes).
|
|
200
|
+
*
|
|
201
|
+
* A v1 or v2 trail — and an absent `v`, which resolves to 1 — is rejected by
|
|
202
|
+
* `reconstructState`'s header version gate with `version-mismatch` ("start a
|
|
203
|
+
* fresh run"): there is no in-place migration (sole consumer rpiv-pi; no
|
|
204
|
+
* back-compat). Tested in `runner/resume.test.ts`.
|
|
175
205
|
*/
|
|
176
|
-
export const STATE_SCHEMA_VERSION =
|
|
206
|
+
export const STATE_SCHEMA_VERSION = 3;
|
|
177
207
|
|
|
178
208
|
/** First line of the JSONL file. */
|
|
179
209
|
export interface WorkflowHeader {
|
|
@@ -261,6 +291,17 @@ export interface RunRecap {
|
|
|
261
291
|
* carry no reason even on a terminal row.
|
|
262
292
|
*/
|
|
263
293
|
failureReason?: string;
|
|
294
|
+
/**
|
|
295
|
+
* Route-note recap: every note-bearing FORWARD routing row's `note`,
|
|
296
|
+
* verbatim, in trail order. Stop-row notes are EXCLUDED — the
|
|
297
|
+
* completed→stopped refinement renders a stop's note exactly once, as
|
|
298
|
+
* `failureReason` (`stopped at <stage>: <note>`); a trail-order echo would
|
|
299
|
+
* double-render it. Set only when at least one such row exists; absent
|
|
300
|
+
* (never `[]`) otherwise, so note-less and legacy trails project
|
|
301
|
+
* byte-identically. Rides EVERY outcome — a failed run may carry earlier-hop
|
|
302
|
+
* notes (a gate explained itself before a later stage blew up).
|
|
303
|
+
*/
|
|
304
|
+
routingNotes?: string[];
|
|
264
305
|
/**
|
|
265
306
|
* Workflow name (matches `Workflow.name` at run-time) projected from the
|
|
266
307
|
* header. `undefined` when the header row is missing or malformed (a
|
package/transcript.ts
CHANGED
|
@@ -32,19 +32,36 @@ export function classifyStop(branch: BranchEntry[], offsetStart?: number): StopS
|
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
34
|
* One content part inside an assistant message. Pi's internal union
|
|
35
|
-
* carries more variants than these
|
|
36
|
-
*
|
|
37
|
-
*
|
|
35
|
+
* carries more variants than these; we model the ones collectors walk
|
|
36
|
+
* over and let unknown parts pass through structurally (every field
|
|
37
|
+
* besides `type` is optional).
|
|
38
38
|
*
|
|
39
39
|
* - `text` parts carry user-visible markdown via `text`.
|
|
40
|
-
* -
|
|
41
|
-
*
|
|
40
|
+
* - Tool invocations carry `name` plus an argument object, under either of
|
|
41
|
+
* two spellings: `{ type: "toolCall", arguments }` — the shape
|
|
42
|
+
* `@earendil-works/pi-ai` emits (`AssistantMessage.content`) and every
|
|
43
|
+
* persisted session file carries — or `{ type: "tool_use", input }`, the
|
|
44
|
+
* Anthropic wire spelling. `iterToolUses` normalises both to `{ name, input }`;
|
|
45
|
+
* a collector never sees the difference. Recognising only one spelling
|
|
46
|
+
* silently disables every tool-argument surface (the text-scan fallback,
|
|
47
|
+
* `toolCallCollector`) against the shape the host actually produces.
|
|
42
48
|
*/
|
|
43
49
|
export type BranchContentPart = {
|
|
44
50
|
type: string;
|
|
45
51
|
text?: string;
|
|
46
52
|
name?: string;
|
|
47
53
|
input?: Record<string, unknown>;
|
|
54
|
+
arguments?: Record<string, unknown>;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** Part types that carry a tool invocation — see `BranchContentPart`. */
|
|
58
|
+
const TOOL_USE_PART_TYPES: ReadonlySet<string> = new Set(["toolCall", "tool_use"]);
|
|
59
|
+
|
|
60
|
+
/** The invocation's argument object under either spelling (`arguments` first —
|
|
61
|
+
* the live SDK shape — then `input`); `{}` when neither is an object. */
|
|
62
|
+
const toolUseInput = (part: BranchContentPart): Record<string, unknown> => {
|
|
63
|
+
const raw = part.arguments ?? part.input;
|
|
64
|
+
return raw !== null && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
48
65
|
};
|
|
49
66
|
|
|
50
67
|
export type BranchEntry = {
|
|
@@ -155,7 +172,7 @@ export function lastAssistantText(branch: BranchEntry[], offsetStart?: number):
|
|
|
155
172
|
*
|
|
156
173
|
* Reverse scan because the agent's final message is usually where the
|
|
157
174
|
* actionable path/URL lands; iterating from the tail short-circuits on
|
|
158
|
-
* the first hit. Thinking
|
|
175
|
+
* the first hit. Thinking and tool-invocation parts are ignored — only spoken
|
|
159
176
|
* `text` parts count.
|
|
160
177
|
*
|
|
161
178
|
* `offsetStart` — continue-policy stages pass the prior branch length
|
|
@@ -184,10 +201,12 @@ export function lastMatchInBranch(branch: BranchEntry[], pattern: RegExp, offset
|
|
|
184
201
|
}
|
|
185
202
|
|
|
186
203
|
/**
|
|
187
|
-
* Yield every
|
|
204
|
+
* Yield every tool-invocation part the assistant emitted in branch order
|
|
188
205
|
* (forward — the typical "what did the agent do during this stage?"
|
|
189
|
-
* scan direction)
|
|
190
|
-
*
|
|
206
|
+
* scan direction), normalised to `{ name, input }` under both spellings
|
|
207
|
+
* (`toolCall`/`arguments` — the live SDK shape — and `tool_use`/`input`; see
|
|
208
|
+
* `BranchContentPart`). Pure — no I/O. `toolCallCollector` and the text-scan
|
|
209
|
+
* fallback walk this.
|
|
191
210
|
*
|
|
192
211
|
* `offsetStart` — continue-policy stages pass the prior branch length.
|
|
193
212
|
*/
|
|
@@ -202,9 +221,9 @@ export function* iterToolUses(
|
|
|
202
221
|
const content = entry.message.content;
|
|
203
222
|
if (!Array.isArray(content)) continue;
|
|
204
223
|
for (const part of content) {
|
|
205
|
-
if (part.type
|
|
224
|
+
if (!TOOL_USE_PART_TYPES.has(part.type)) continue;
|
|
206
225
|
if (typeof part.name !== "string") continue;
|
|
207
|
-
yield { name: part.name, input: part
|
|
226
|
+
yield { name: part.name, input: toolUseInput(part) };
|
|
208
227
|
}
|
|
209
228
|
}
|
|
210
229
|
}
|
package/types.ts
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
* module are cycle-free).
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
|
-
import type { StageDef, Workflow } from "./api.js";
|
|
26
|
+
import type { ProgressValue, StageDef, Workflow } from "./api.js";
|
|
27
27
|
import type { LifecycleDispatcher, LifecycleListeners } from "./events.js";
|
|
28
28
|
import type { Artifact } from "./handle.js";
|
|
29
29
|
import type { ModelSelection, WorkflowHost, WorkflowHostContext } from "./host.js";
|
|
@@ -111,8 +111,9 @@ export interface RunState {
|
|
|
111
111
|
telemetry: {
|
|
112
112
|
/**
|
|
113
113
|
* Run-wide cumulative count of backward jumps (decision-edge routes to
|
|
114
|
-
* an already-visited stage
|
|
115
|
-
*
|
|
114
|
+
* an already-visited stage — every decision-edge re-entry, waived or
|
|
115
|
+
* counted). Never reset. The halt decision reads the per-destination
|
|
116
|
+
* `RunContext.revisits` ledger, not this total.
|
|
116
117
|
*/
|
|
117
118
|
backwardJumps: number;
|
|
118
119
|
/**
|
|
@@ -212,6 +213,15 @@ export interface RunWorkflowOptions {
|
|
|
212
213
|
host?: WorkflowHost;
|
|
213
214
|
/** Per-destination decision-edge re-entry cap. Defaults to MAX_BACKWARD_JUMPS. */
|
|
214
215
|
maxBackwardJumps?: number;
|
|
216
|
+
/**
|
|
217
|
+
* Per-destination ABSOLUTE ceiling on decision-edge re-entries — unlike
|
|
218
|
+
* the waive-aware `maxBackwardJumps` cap, every re-entry counts toward it
|
|
219
|
+
* (a `progress` verdict of "improved" waives the cap, never this), so
|
|
220
|
+
* the `maxLaps + 1`-th re-entry of one stage always halts. Defaults to
|
|
221
|
+
* MAX_LAPS; fresh per invocation (a resume starts both re-entry ledgers
|
|
222
|
+
* empty).
|
|
223
|
+
*/
|
|
224
|
+
maxLaps?: number;
|
|
215
225
|
/** Run-wide safety cap on loop units (all kinds). Defaults to MAX_ITERATIONS. */
|
|
216
226
|
maxIterations?: number;
|
|
217
227
|
/**
|
|
@@ -338,6 +348,30 @@ export interface RunContext {
|
|
|
338
348
|
* construction.
|
|
339
349
|
*/
|
|
340
350
|
revisits: Map<string, number>;
|
|
351
|
+
/**
|
|
352
|
+
* Observation-only ring of a destination's most recent `progress`
|
|
353
|
+
* verdicts (oldest → newest, bounded at 3), keyed per destination like
|
|
354
|
+
* `revisits`. Appended ONLY when the re-entered stage declares a
|
|
355
|
+
* `progress` hook — an absent hook leaves the ring empty. The trail
|
|
356
|
+
* rides the halt text (`; last progress: …`) and the routing row's
|
|
357
|
+
* guard note; it is NEVER consulted for the halt decision (the verdict
|
|
358
|
+
* itself already waived or counted the re-entry when it was recorded).
|
|
359
|
+
* Fresh on every invocation — like `revisits`, a resume starts with an
|
|
360
|
+
* empty ring.
|
|
361
|
+
*/
|
|
362
|
+
progressTrail: Map<string, ProgressValue[]>;
|
|
363
|
+
/**
|
|
364
|
+
* ABSOLUTE decision-edge re-entry count per destination — the lap ledger
|
|
365
|
+
* behind the `maxLaps` ceiling. Three counters, three taxonomies:
|
|
366
|
+
* `laps` counts EVERY re-entry (improved-waived included) and feeds only
|
|
367
|
+
* the ceiling halt; `revisits` above skips improved-waived re-entries and
|
|
368
|
+
* feeds only the cap halt — so `revisits ≤ laps` always, and both limits
|
|
369
|
+
* trip on the same re-entry only when `maxBackwardJumps ≥ maxLaps`;
|
|
370
|
+
* `state.telemetry.backwardJumps` is the run-wide cumulative total,
|
|
371
|
+
* never consulted for a halt decision. Fresh on every invocation like
|
|
372
|
+
* the other two ledgers — engine memory, never persisted to the trail.
|
|
373
|
+
*/
|
|
374
|
+
laps: Map<string, number>;
|
|
341
375
|
/**
|
|
342
376
|
* Set of bare skill names registered with Pi at workflow start (e.g.
|
|
343
377
|
* "research", "blueprint" — the `skill:` prefix is stripped). Snapshot
|
|
@@ -395,6 +429,13 @@ export interface RunContext {
|
|
|
395
429
|
*/
|
|
396
430
|
worktreeDigest?: (cwd: string) => string | undefined;
|
|
397
431
|
maxBackwardJumps: number;
|
|
432
|
+
/**
|
|
433
|
+
* Per-destination ABSOLUTE ceiling on decision-edge re-entries — see
|
|
434
|
+
* `laps`. The verdict-proof bound above the waive-aware cap: the
|
|
435
|
+
* `maxLaps + 1`-th re-entry of one stage halts whatever the `progress`
|
|
436
|
+
* verdict. Defaults to `MAX_LAPS`; fresh per invocation.
|
|
437
|
+
*/
|
|
438
|
+
maxLaps: number;
|
|
398
439
|
/**
|
|
399
440
|
* Run-wide safety cap on loop units — clamps the effective cap of EVERY
|
|
400
441
|
* loop kind (`min(loop.max, run.maxIterations)`), the backstop for a
|
|
@@ -559,6 +600,15 @@ export interface StageSessionContext extends SessionContext {
|
|
|
559
600
|
* consume to resolve the budget's ceiling). Immutable per-activation.
|
|
560
601
|
*/
|
|
561
602
|
bashTimeoutStrikes?: number;
|
|
603
|
+
/**
|
|
604
|
+
* 1-based dispatch ordinal of THIS attempt within a fanout unit's
|
|
605
|
+
* `retryHaltedUnits` window — stamped by the parallel dispatcher
|
|
606
|
+
* (`dispatchUnitDetached` threads the per-attempt count through
|
|
607
|
+
* `buildUnitSession`) and projected onto the collected halt row by
|
|
608
|
+
* `auditFor` → `recordUnitHalt`. Undefined for sequential units
|
|
609
|
+
* (iterate/assess) and single stages.
|
|
610
|
+
*/
|
|
611
|
+
attemptOrdinal?: number;
|
|
562
612
|
/**
|
|
563
613
|
* Present iff this session IS one loop unit. Pre-decorated at session
|
|
564
614
|
* construction by the driver (`stageName` carries the DISPLAY decoration;
|
package/validate/issue.ts
CHANGED
|
@@ -98,6 +98,11 @@ export const ISSUE_DEFS = {
|
|
|
98
98
|
() =>
|
|
99
99
|
"sets `inheritsArtifacts: false` on a `produces` stage — the flag is the `terminal()` factory's mechanism and is only meaningful on side-effect stages",
|
|
100
100
|
),
|
|
101
|
+
"progress-not-function": def(
|
|
102
|
+
"error",
|
|
103
|
+
() =>
|
|
104
|
+
'progress must be a function of signature `(state: RunView) => ProgressValue` — the backward-jump guard awaits it per decision-edge re-entry ("improved" waives, anything else counts); an absent hook is valid (every re-entry counts)',
|
|
105
|
+
),
|
|
101
106
|
|
|
102
107
|
// --- loop invariants ------------------------------------------------------
|
|
103
108
|
"loop-kind-unknown": def<{ kind: string; allowed: string }>(
|
|
@@ -122,6 +127,11 @@ export const ISSUE_DEFS = {
|
|
|
122
127
|
(p) =>
|
|
123
128
|
`fanout depArtifactFlag: ${JSON.stringify(p.depArtifactFlag)} — must be a non-empty string (the flag the dispatcher prefixes each dep artifact path with)`,
|
|
124
129
|
),
|
|
130
|
+
"loop-retry-halted-units-invalid": def<{ retryHaltedUnits: number }>(
|
|
131
|
+
"error",
|
|
132
|
+
(p) =>
|
|
133
|
+
`fanout retryHaltedUnits: ${p.retryHaltedUnits} — must be an integer >= 1 (each value buys one re-dispatch of a soft-halted collect-all unit; only its final attempt's output folds)`,
|
|
134
|
+
),
|
|
125
135
|
"loop-requires-produces": def<{ kind: string }>(
|
|
126
136
|
"error",
|
|
127
137
|
(p) => `${p.kind} requires kind "produces" — each unit runs an outcome collector`,
|
package/validate/stage-rules.ts
CHANGED
|
@@ -54,6 +54,7 @@ export function checkStageSemantics(w: Workflow, r: IssueReporter): void {
|
|
|
54
54
|
checkVerifyInvariants(stage, name, report);
|
|
55
55
|
checkPromptInvariants(stage, name === w.start, report);
|
|
56
56
|
checkInheritsArtifactsKind(stage, report);
|
|
57
|
+
checkProgressShape(stage, report);
|
|
57
58
|
checkScriptStageInvariants(stage, report);
|
|
58
59
|
}
|
|
59
60
|
}
|
|
@@ -85,6 +86,9 @@ function checkLoopInvariants(stage: StageDef, name: string, report: ReportFn): v
|
|
|
85
86
|
if (isInvalidDepArtifactFlag(loop)) {
|
|
86
87
|
report("loop-dep-flag-invalid", { depArtifactFlag: loop.depArtifactFlag });
|
|
87
88
|
}
|
|
89
|
+
if (isInvalidRetryHaltedUnits(loop)) {
|
|
90
|
+
report("loop-retry-halted-units-invalid", { retryHaltedUnits: loop.retryHaltedUnits });
|
|
91
|
+
}
|
|
88
92
|
// Pull loops + assess run the stage's outcome collector per unit.
|
|
89
93
|
if ((loop.kind === "iterate" || loop.kind === "assess") && stage.kind !== "produces") {
|
|
90
94
|
report("loop-requires-produces", { kind: loop.kind });
|
|
@@ -146,6 +150,17 @@ const isInvalidDepArtifactFlag = (loop: LoopDef): loop is FanoutLoop & { depArti
|
|
|
146
150
|
loop.depArtifactFlag !== undefined &&
|
|
147
151
|
(typeof loop.depArtifactFlag !== "string" || loop.depArtifactFlag.trim().length === 0);
|
|
148
152
|
|
|
153
|
+
/**
|
|
154
|
+
* A fanout's `retryHaltedUnits` budget is present and out of range — not an
|
|
155
|
+
* integer or below 1. The type guard narrows to `FanoutLoop &
|
|
156
|
+
* { retryHaltedUnits: number }` so the report call reads the field without a
|
|
157
|
+
* cast or non-null assertion.
|
|
158
|
+
*/
|
|
159
|
+
const isInvalidRetryHaltedUnits = (loop: LoopDef): loop is FanoutLoop & { retryHaltedUnits: number } =>
|
|
160
|
+
loop.kind === "fanout" &&
|
|
161
|
+
loop.retryHaltedUnits !== undefined &&
|
|
162
|
+
(!Number.isInteger(loop.retryHaltedUnits) || loop.retryHaltedUnits < 1);
|
|
163
|
+
|
|
149
164
|
/**
|
|
150
165
|
* A stable named slot: iterate and assess always (every unit/round runs the
|
|
151
166
|
* produces collector), fanout when COLLECTING (produces kind) — the decorated
|
|
@@ -362,6 +377,21 @@ function checkInheritsArtifactsKind(stage: StageDef, report: ReportFn): void {
|
|
|
362
377
|
}
|
|
363
378
|
}
|
|
364
379
|
|
|
380
|
+
/**
|
|
381
|
+
* `progress` is the optional backward-jump waiver hook, declared on
|
|
382
|
+
* `StageDefBase` so every dispatch arm carries it. Present ⇒ must be a
|
|
383
|
+
* function — the guard awaits it per decision-edge re-entry. An absent
|
|
384
|
+
* hook is valid (every re-entry counts). No exclusion rules: `progress`
|
|
385
|
+
* composes with `loop` / `verify` / `reads` by design, so this is purely a
|
|
386
|
+
* shape check (mirrors the `readsData` lint posture: jiti erases the TS
|
|
387
|
+
* type, the load gate catches a hand-rolled literal).
|
|
388
|
+
*/
|
|
389
|
+
function checkProgressShape(stage: StageDef, report: ReportFn): void {
|
|
390
|
+
if (stage.progress !== undefined && typeof stage.progress !== "function") {
|
|
391
|
+
report("progress-not-function");
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
365
395
|
/**
|
|
366
396
|
* Skillless script stages: presence of `stage.run` declares "the runner
|
|
367
397
|
* calls this TS function instead of dispatching a Pi skill." Four fields
|