@juicesharp/rpiv-workflow 1.19.0 → 1.20.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.
Files changed (114) hide show
  1. package/README.md +81 -8
  2. package/api.ts +18 -703
  3. package/audit-rows.ts +136 -0
  4. package/audit.ts +139 -126
  5. package/built-ins.ts +20 -45
  6. package/chain-state.ts +169 -0
  7. package/command-run.ts +31 -3
  8. package/command.ts +116 -20
  9. package/docs/workflow-authoring.md +322 -61
  10. package/{lifecycle.ts → events.ts} +119 -35
  11. package/host.ts +30 -0
  12. package/index.ts +25 -81
  13. package/internal-utils.ts +92 -78
  14. package/internal.ts +21 -1
  15. package/judge.ts +229 -0
  16. package/layers.ts +10 -15
  17. package/load/alias.ts +6 -12
  18. package/load/cache.ts +9 -6
  19. package/load/index.ts +16 -109
  20. package/load/issues.ts +31 -0
  21. package/load/legacy.ts +102 -0
  22. package/load/merge.ts +2 -2
  23. package/load/shape-guards.test.ts +4 -1
  24. package/load/shape-guards.ts +3 -6
  25. package/load/skill-contract-phase.ts +94 -0
  26. package/loop-constructors.ts +623 -0
  27. package/loop-def.ts +240 -0
  28. package/loop-kinds.ts +493 -0
  29. package/loop.ts +304 -0
  30. package/messages.ts +225 -157
  31. package/outcomes/collectors/directory-path.ts +7 -3
  32. package/outcomes/collectors/require-opt.ts +11 -0
  33. package/outcomes/collectors/tool-call.test.ts +3 -1
  34. package/outcomes/collectors/tool-call.ts +8 -3
  35. package/outcomes/collectors/transcript-path.ts +2 -3
  36. package/outcomes/collectors/union.test.ts +54 -0
  37. package/outcomes/collectors/union.ts +28 -9
  38. package/outcomes/collectors/url.ts +7 -0
  39. package/outcomes/collectors/workspace-diff.test.ts +13 -0
  40. package/outcomes/collectors/workspace-diff.ts +21 -14
  41. package/outcomes/exec.ts +17 -0
  42. package/outcomes/git-commit.test.ts +76 -30
  43. package/outcomes/git-commit.ts +139 -100
  44. package/outcomes/index.ts +1 -0
  45. package/outcomes/parsers/json-body.ts +2 -2
  46. package/outcomes/side-effect.ts +3 -3
  47. package/output-spec.ts +36 -10
  48. package/output.ts +40 -20
  49. package/package.json +13 -7
  50. package/predicates.ts +11 -8
  51. package/preview.ts +77 -17
  52. package/registration.ts +109 -35
  53. package/routing-dsl.ts +310 -0
  54. package/routing.ts +2 -2
  55. package/runner/by-name.ts +2 -1
  56. package/runner/by-run-id.ts +2 -1
  57. package/runner/chain-advance.ts +59 -44
  58. package/runner/errors.ts +33 -0
  59. package/runner/failure.ts +109 -0
  60. package/runner/header-write-failure.test.ts +64 -0
  61. package/runner/index.ts +7 -13
  62. package/runner/input-validation.ts +11 -21
  63. package/runner/preflight.ts +173 -0
  64. package/runner/resolve-stage.ts +68 -0
  65. package/runner/resume-entry.ts +101 -0
  66. package/runner/resume-loop.test.ts +1410 -0
  67. package/runner/resume-loop.ts +110 -0
  68. package/runner/resume-session.test.ts +400 -0
  69. package/runner/resume.test.ts +881 -206
  70. package/runner/resume.ts +281 -261
  71. package/runner/run-context.ts +161 -0
  72. package/runner/run-stage.ts +456 -0
  73. package/runner/runner.ts +59 -400
  74. package/runner/script-stage.ts +103 -105
  75. package/schema-compat.ts +13 -1
  76. package/sessions/extraction.ts +53 -31
  77. package/sessions/index.ts +3 -1
  78. package/sessions/locate.test.ts +77 -0
  79. package/sessions/locate.ts +90 -0
  80. package/sessions/reattach.ts +63 -0
  81. package/sessions/sessions.ts +130 -127
  82. package/skill-contract.ts +22 -25
  83. package/skill-contracts/composition.ts +88 -37
  84. package/skill-contracts/{registries.ts → extension-points.ts} +45 -12
  85. package/skill-contracts/harvest.ts +2 -3
  86. package/skill-contracts/index.ts +15 -8
  87. package/skill-contracts/registry.ts +33 -50
  88. package/stage-def.ts +527 -0
  89. package/startup.ts +3 -1
  90. package/state/index.ts +8 -1
  91. package/state/names.ts +91 -65
  92. package/state/paths.ts +12 -0
  93. package/state/raw.ts +78 -0
  94. package/state/reads.ts +103 -85
  95. package/state/resolve.ts +73 -0
  96. package/state/state.ts +112 -15
  97. package/state/writes.ts +29 -12
  98. package/transcript.ts +24 -0
  99. package/triggers.ts +11 -3
  100. package/types.ts +202 -44
  101. package/validate/contract-compat.ts +193 -0
  102. package/validate/graph.ts +125 -0
  103. package/validate/issue.ts +314 -0
  104. package/validate/stage-rules.ts +420 -0
  105. package/validate-output.ts +69 -1
  106. package/validate-workflow.ts +55 -759
  107. package/control-flow.ts +0 -135
  108. package/fanout.ts +0 -102
  109. package/iterate.ts +0 -155
  110. package/runner/resume-fanout.test.ts +0 -331
  111. package/runner/resume-fanout.ts +0 -136
  112. package/runner/resume-iterate.test.ts +0 -444
  113. package/runner/resume-iterate.ts +0 -111
  114. package/runner/stage-lifecycle.ts +0 -500
package/loop-def.ts ADDED
@@ -0,0 +1,240 @@
1
+ /**
2
+ * Loop vocabulary — the data shapes behind `StageDef.loop` and
3
+ * `StageDef.verify`, AS TYPES. Constructors live in loop-constructors.ts
4
+ * (`fanout()` / `iterate()` / `assess()` / `verify()`); the per-kind runtime
5
+ * strategy table lives in loop-kinds.ts. Split out of api.ts so the
6
+ * loop concept has one home instead of accreting into the stage module.
7
+ *
8
+ * Leaf-ish: imports only types from judge/output/handle.
9
+ */
10
+
11
+ import type { Artifact } from "./handle.js";
12
+ import type { AnyJudge } from "./judge.js";
13
+ import type { Output, RunView } from "./output.js";
14
+
15
+ /**
16
+ * One unit of loop work — the single unit type for all three loop kinds.
17
+ * `prompt` is the body sent to the skill; `label` is the human display tag
18
+ * woven into the status line, the per-unit toast, and the decorated row
19
+ * display; `id` is the stable audit identity written to the row's `unitId`
20
+ * field (falls back to `label`). Post-hoc tooling and the resume drift guard
21
+ * join on `id ?? label` — set `id` when `label` may be reworded.
22
+ */
23
+ export interface Unit {
24
+ prompt: string;
25
+ label: string;
26
+ id?: string;
27
+ }
28
+
29
+ /**
30
+ * Push-model unit source: all units computed up front, each run blind to the
31
+ * others. Empty return ⇒ no loop (fall through to the single-stage path).
32
+ * Throws ⇒ stage halts, attributed to this stage (a thrown
33
+ * `StagePreflightError` keeps its own attribution — the `haltPreflight`
34
+ * consumer contract).
35
+ */
36
+ export type FanoutFn = (ctx: FanoutContext) => readonly Unit[] | Promise<readonly Unit[]>;
37
+
38
+ export interface FanoutContext {
39
+ cwd: string;
40
+ /** Primary artifact inherited from upstream (undefined when the loop stage is the entry point). */
41
+ artifact: Artifact | undefined;
42
+ state: RunView;
43
+ }
44
+
45
+ /**
46
+ * Pull-model unit source: invoked once per unit, each call receiving the
47
+ * validated `Output`s of every prior unit in this generation. Return the next
48
+ * unit, or `null`/`undefined` to terminate the loop.
49
+ *
50
+ * RESUME CONTRACT (all loop kinds): the unit source must be deterministic
51
+ * w.r.t. the fold-replayed `RunState` at the unit boundary + this
52
+ * generation's accumulated outputs. The resume fold re-calls it at every
53
+ * folded boundary and refuses on drift.
54
+ */
55
+ export type IterateFn = (ctx: IterateContext) => Unit | null | Promise<Unit | null>;
56
+
57
+ export interface IterateContext {
58
+ cwd: string;
59
+ /** Stage-entry primary, FROZEN across every unit (never rolls forward). */
60
+ artifact: Artifact | undefined;
61
+ state: RunView;
62
+ /** Validated Outputs of this generation's completed units, in order. */
63
+ accumulated: readonly Output[];
64
+ /** 0-based index of the unit about to run (== accumulated.length). */
65
+ index: number;
66
+ }
67
+
68
+ /** Context handed to `AssessLoop.feedForward` and `VerifySpec.feedForward`. */
69
+ export interface FeedForwardContext {
70
+ cwd: string;
71
+ /** Producer output just judged. */
72
+ output: Output;
73
+ /** Judge verdict (incl. feedback). */
74
+ verdict: Output;
75
+ /** 0-based round index of the round just judged (== the attempt index for verify). */
76
+ round: number;
77
+ state: RunView;
78
+ }
79
+
80
+ /**
81
+ * The shared "repeat under a model judge" vocabulary — ONE set of field names
82
+ * for the two judged-repetition shapes (`AssessLoop` rounds, `VerifySpec`
83
+ * attempts). The runtime already admits they're one concept (verify desugars
84
+ * to a degenerate assess loop); this base keeps their vocabularies from
85
+ * drifting again (`pass`/`done`, `maxAttempts`/`max` were two spellings of
86
+ * the same two knobs).
87
+ */
88
+ export interface JudgedRepetition {
89
+ /**
90
+ * The judge SLOT — a single `Judge` or an N-member `PanelJudge` (a single
91
+ * judge is the panel of one). Widened to `AnyJudge` so `assess`/`verify`
92
+ * compose a panel with zero per-site code; the runtime expands it through
93
+ * `panelMembers` at the one judge-dispatch site.
94
+ */
95
+ judge: AnyJudge;
96
+ /**
97
+ * Sync TS reading the model-made verdict. `true` → the repetition stops
98
+ * and the chain advances with the last producer pair. RESUME CONTRACT:
99
+ * recomputed on resume (never persisted) — must be deterministic w.r.t.
100
+ * the verdict `Output`. A `true` on the final round/attempt is a normal
101
+ * completion (the predicate wins over the cap).
102
+ */
103
+ done: (verdict: Output) => boolean;
104
+ /**
105
+ * Builds the next round/attempt's prompt arg from the just-judged
106
+ * producer output + verdict. On a prompt-dispatch stage the returned
107
+ * string is the COMPLETE retry message (sent raw — there is no skill to
108
+ * prefix an arg onto).
109
+ */
110
+ feedForward?: (ctx: FeedForwardContext) => string;
111
+ /** Repetition cap. Integer >= 1; clamped by `run.maxIterations` at runtime. */
112
+ max?: number;
113
+ }
114
+
115
+ /**
116
+ * Per-stage post-condition judge — the data shape behind `StageDef.verify`.
117
+ * After each attempt of the stage completes (collected, validated, persisted),
118
+ * the judge session grades the attempt's primary artifact; `done(verdict)`
119
+ * true → verified, the chain advances with the attempt's producer pair;
120
+ * false → a fresh retry attempt (prompt arg built by `feedForward`) up to
121
+ * `max` attempts, then a terminal "verification failed" halt. Author via the
122
+ * `verify()` constructor (loop-constructors.ts), which validates at construction;
123
+ * load-time validation re-checks hand-rolled literals through the same
124
+ * `verifyShapeIssues` rule source.
125
+ *
126
+ * Field semantics on top of the shared `JudgedRepetition` vocabulary:
127
+ * - `done` — the pass predicate (`true` = verified).
128
+ * - `max` — total attempt budget; each attempt is a FRESH session running
129
+ * the full produce→validate→persist cycle. Orthogonal to
130
+ * `stage.maxRetries` (the cheap in-session schema-fix budget, which stays
131
+ * live inside every attempt). Default 1 (gate-only).
132
+ * - `feedForward` — REQUIRED when `max > 1` (without it the retried prompt
133
+ * would be byte-identical to the original and the model would have no
134
+ * signal about why it failed); never called when `max` is 1.
135
+ *
136
+ * Runtime: the runner desugars the field into a degenerate assess loop
137
+ * (`max: max ?? 1`, `onCap: "halt"`, `result: "last"`) run by the ONE loop
138
+ * driver. Attempts and verdicts land as unit rows (`role: "produce"` /
139
+ * `role: "verify"`, `unitIndex` = 0-based attempt); the verdict publishes
140
+ * durably to `state.named[judge.outcome.name]` (so declarative fallback
141
+ * routing over `EdgeContext.state.named` works); and `projectResult`
142
+ * restores the last attempt's producer pair before the chain advances —
143
+ * downstream stages never inherit the verdict.
144
+ */
145
+ export type VerifySpec = JudgedRepetition;
146
+
147
+ /**
148
+ * Role a unit row/event carries. Every main-work unit — fanout unit, iterate
149
+ * unit, assess producer, verify attempt — is `"produce"`; assess judge
150
+ * sub-steps are `"judge"`; verify verdict sub-steps are `"verify"`. The
151
+ * `result: "last"` projection and the resume fold's apply rule key on this.
152
+ */
153
+ export type UnitRole = "produce" | "judge" | "verify";
154
+
155
+ /**
156
+ * What happens when a loop hits its effective cap (`min(max, run.maxIterations)`):
157
+ * `"halt"` — terminal failure (mirrors the backward-jump guard);
158
+ * `"advance"` — soft-stop: warn, land a `{type:"loop-cap"}` telemetry row,
159
+ * fire `onLoopCap`, keep the projected result, advance downstream.
160
+ */
161
+ export type CapPolicy = "halt" | "advance";
162
+
163
+ /**
164
+ * What the loop leaves in `{state.output, state.primaryArtifact}` — the PAIR
165
+ * is governed as one (routing + downstream prompts read both):
166
+ * `"entry"` — restore the pair captured at loop entry (fanout default;
167
+ * reproduces routing-sees-upstream);
168
+ * `"last"` — the last completed `role: "produce"` unit's pair (iterate /
169
+ * assess default; zero produce units degrades to entry).
170
+ * Applied at ONE point — loop advance — by the live driver and the resume
171
+ * fold's generation close identically. Mid-loop transient rolls are accepted.
172
+ */
173
+ export type ResultProjection = "entry" | "last";
174
+
175
+ /**
176
+ * How a stage's work is split into units, AS DATA. The framework never
177
+ * interprets `by`/`pattern` — they're introspection hints for agents and the
178
+ * `checkFanoutSource` lint.
179
+ */
180
+ export interface UnitSelector {
181
+ by: string;
182
+ pattern?: string;
183
+ meta?: Record<string, unknown>;
184
+ }
185
+
186
+ /** Introspectable data common to all three loop kinds. */
187
+ interface LoopCommon {
188
+ /** The named channel the units are split FROM (a `consumes` signal). */
189
+ source?: string;
190
+ /** How units are detected (opaque convention). */
191
+ unit?: UnitSelector;
192
+ /** Cardinality ceiling; clamped by `run.maxIterations` at runtime. */
193
+ max?: number;
194
+ onCap: CapPolicy;
195
+ result: ResultProjection;
196
+ }
197
+
198
+ /** Parallel-shaped push loop (units still run sequentially today). */
199
+ export interface FanoutLoop extends LoopCommon {
200
+ kind: "fanout";
201
+ units: FanoutFn;
202
+ }
203
+
204
+ /** Sequential accumulating pull loop. */
205
+ export interface IterateLoop extends LoopCommon {
206
+ kind: "iterate";
207
+ next: IterateFn;
208
+ }
209
+
210
+ /**
211
+ * Model-judged until-done loop: producer→judge rounds. Shares the
212
+ * `JudgedRepetition` vocabulary with `VerifySpec`; here `max` (the round cap)
213
+ * and `feedForward` are REQUIRED — the `assess()` constructor defaults `max`
214
+ * to 8 and rejects a missing `feedForward`.
215
+ */
216
+ export interface AssessLoop extends LoopCommon, JudgedRepetition {
217
+ kind: "assess";
218
+ /** Round cap — REQUIRED here (the `assess()` constructor defaults it to 8). */
219
+ max: number;
220
+ /** Builds the next producer prompt arg from the just-judged round's pair. */
221
+ feedForward: (ctx: FeedForwardContext) => string;
222
+ }
223
+
224
+ /**
225
+ * The single loop field's value — a DATA object with function-valued fields,
226
+ * introspectable by construction (project with `loopSpecOf`). Author via the
227
+ * `fanout()` / `iterate()` / `assess()` constructors (loop-constructors.ts), which
228
+ * validate at construction and fill kind-specific defaults.
229
+ */
230
+ export type LoopDef = FanoutLoop | IterateLoop | AssessLoop;
231
+
232
+ /**
233
+ * Runtime enumeration of `LoopDef["kind"]` — exported from where `LoopDef`
234
+ * lives so the validator (kind whitelist) and the per-kind strategy table
235
+ * (loop-kinds.ts) consume one list. The `satisfies` pin rejects a stray
236
+ * member; completeness is enforced by the strategy table's
237
+ * `Record<LoopDef["kind"], …>` shape (a new union arm fails compilation
238
+ * there first).
239
+ */
240
+ export const LOOP_KINDS = ["fanout", "iterate", "assess"] as const satisfies readonly LoopDef["kind"][];