@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
package/registration.ts
CHANGED
|
@@ -9,7 +9,9 @@
|
|
|
9
9
|
|
|
10
10
|
export {
|
|
11
11
|
type ActsScriptFn,
|
|
12
|
+
type AssessLoop,
|
|
12
13
|
acts,
|
|
14
|
+
type CapPolicy,
|
|
13
15
|
type DefineRouteOptions,
|
|
14
16
|
defineRoute,
|
|
15
17
|
defineWorkflow,
|
|
@@ -18,44 +20,59 @@ export {
|
|
|
18
20
|
type EdgeTarget,
|
|
19
21
|
type FanoutContext,
|
|
20
22
|
type FanoutFn,
|
|
21
|
-
type
|
|
23
|
+
type FanoutLoop,
|
|
24
|
+
type FeedForwardContext,
|
|
25
|
+
fanin,
|
|
22
26
|
gate,
|
|
23
27
|
type IterateContext,
|
|
24
28
|
type IterateFn,
|
|
25
|
-
type
|
|
29
|
+
type IterateLoop,
|
|
30
|
+
type JudgedRepetition,
|
|
31
|
+
type LoopDef,
|
|
32
|
+
type MatchOptions,
|
|
33
|
+
type MatchValue,
|
|
26
34
|
marksReadsData,
|
|
35
|
+
match,
|
|
27
36
|
ON_INVALID_VALUES,
|
|
28
37
|
type OnInvalid,
|
|
29
38
|
type ProducesScriptFn,
|
|
30
39
|
type PromptFn,
|
|
40
|
+
type PromptStage,
|
|
31
41
|
produces,
|
|
32
42
|
READS_DATA,
|
|
43
|
+
type ResultProjection,
|
|
33
44
|
type ScriptContext,
|
|
45
|
+
type ScriptStage,
|
|
34
46
|
SESSION_POLICIES,
|
|
35
47
|
type SessionPolicy,
|
|
48
|
+
type SkillStage,
|
|
36
49
|
STAGE_KINDS,
|
|
37
50
|
STOP,
|
|
38
51
|
type StageDef,
|
|
39
52
|
type StageKind,
|
|
53
|
+
type StageRead,
|
|
40
54
|
type StageSchema,
|
|
41
55
|
terminal,
|
|
56
|
+
type Unit,
|
|
57
|
+
type UnitRole,
|
|
58
|
+
type UnitSelector,
|
|
59
|
+
type VerifySpec,
|
|
42
60
|
type Workflow,
|
|
43
61
|
} from "./api.js";
|
|
44
62
|
export { registerBuiltIns, registerBuiltInsProvider } from "./built-ins.js";
|
|
63
|
+
// The shared dispatch predicate (and SkillStage type guard) — public so
|
|
64
|
+
// extension points that key on a stage's skill identity (outcome derivers,
|
|
65
|
+
// contract tooling) apply the same gate the loader/validator/harvest use.
|
|
66
|
+
export { isDispatchingStage } from "./chain-state.js";
|
|
45
67
|
export {
|
|
46
|
-
type
|
|
47
|
-
|
|
48
|
-
type
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
type
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
type SpeccedFanoutFn,
|
|
55
|
-
type SpeccedIterateFn,
|
|
56
|
-
type StageShape,
|
|
57
|
-
type UnitSelector,
|
|
58
|
-
} from "./control-flow.js";
|
|
68
|
+
type LifecycleContext,
|
|
69
|
+
type LifecycleListeners,
|
|
70
|
+
type LoopCapInfo,
|
|
71
|
+
type LoopStartInfo,
|
|
72
|
+
registerLifecycle,
|
|
73
|
+
type StageRef,
|
|
74
|
+
type UnitEvent,
|
|
75
|
+
} from "./events.js";
|
|
59
76
|
export {
|
|
60
77
|
type Artifact,
|
|
61
78
|
type ArtifactHandle,
|
|
@@ -65,7 +82,12 @@ export {
|
|
|
65
82
|
opaque,
|
|
66
83
|
url,
|
|
67
84
|
} from "./handle.js";
|
|
68
|
-
export type {
|
|
85
|
+
export type {
|
|
86
|
+
ModelSelection,
|
|
87
|
+
WorkflowHost,
|
|
88
|
+
WorkflowHostContext,
|
|
89
|
+
WorkflowSessionContext,
|
|
90
|
+
} from "./host.js";
|
|
69
91
|
// Only the contract data types are public — they're referenced by kept public
|
|
70
92
|
// signatures (`JsonSchemaCapable` ← `typeboxSchema`, `JsonSchemaObject` ←
|
|
71
93
|
// `ConsumesSpec`/`ProducesSpec.data`, `SchemaCompatResult` ← `canCompose`). The
|
|
@@ -73,9 +95,50 @@ export type { WorkflowHost, WorkflowHostContext, WorkflowSessionContext } from "
|
|
|
73
95
|
// consumer needs them); internal callers import them from their source modules
|
|
74
96
|
// directly.
|
|
75
97
|
export type { JsonSchemaCapable, JsonSchemaObject } from "./json-schema.js";
|
|
76
|
-
export {
|
|
98
|
+
export {
|
|
99
|
+
type AnyJudge,
|
|
100
|
+
type FoldFn,
|
|
101
|
+
isPanel,
|
|
102
|
+
type Judge,
|
|
103
|
+
type JudgeContext,
|
|
104
|
+
type JudgePromptFn,
|
|
105
|
+
judge,
|
|
106
|
+
judgeShapeIssues,
|
|
107
|
+
type NamedOutcome,
|
|
108
|
+
type PanelJudge,
|
|
109
|
+
type PromptJudge,
|
|
110
|
+
panelMembers,
|
|
111
|
+
type SkillJudge,
|
|
112
|
+
} from "./judge.js";
|
|
77
113
|
export type { ConfigLayer, Issue, LoadedWorkflows, LoadIssue, OverlayPaths } from "./load/index.js";
|
|
78
114
|
export { aliasSkills, loadWorkflows, projectOverlayPaths, userOverlayPaths } from "./load/index.js";
|
|
115
|
+
export {
|
|
116
|
+
type AnyJudgeSpec,
|
|
117
|
+
all,
|
|
118
|
+
any,
|
|
119
|
+
assess,
|
|
120
|
+
DEFAULT_ASSESS_MAX,
|
|
121
|
+
describeFlow,
|
|
122
|
+
fanout,
|
|
123
|
+
iterate,
|
|
124
|
+
type JudgeSpec,
|
|
125
|
+
judgeSlotSpecOf,
|
|
126
|
+
judgeSpecOf,
|
|
127
|
+
type LoopSpec,
|
|
128
|
+
loopSpecOf,
|
|
129
|
+
majority,
|
|
130
|
+
PANEL_VERDICT,
|
|
131
|
+
PANEL_VERDICT_OUTCOME,
|
|
132
|
+
type PanelJudgeSpec,
|
|
133
|
+
type PanelSpec,
|
|
134
|
+
type PanelVerdict,
|
|
135
|
+
panel,
|
|
136
|
+
panelShapeIssues,
|
|
137
|
+
panelSpecOf,
|
|
138
|
+
type StageShape,
|
|
139
|
+
verify,
|
|
140
|
+
verifyShapeIssues,
|
|
141
|
+
} from "./loop-constructors.js";
|
|
79
142
|
export {
|
|
80
143
|
type DirectoryPathCollectorOpts,
|
|
81
144
|
directoryPathCollector,
|
|
@@ -100,21 +163,24 @@ export {
|
|
|
100
163
|
type WorkspaceDiffSnapshot,
|
|
101
164
|
workspaceDiffCollector,
|
|
102
165
|
} from "./outcomes/index.js";
|
|
103
|
-
export type {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
166
|
+
export type { Output, OutputMeta, RunView, Verdict } from "./output.js";
|
|
167
|
+
// Producer-side surface — `output-spec.ts` is the ONE canonical import path
|
|
168
|
+
// for collector/parser/outcome authoring types (`output.ts` keeps only the
|
|
169
|
+
// envelope). `OutputSpec` is the deprecated pre-rename alias of `Outcome`.
|
|
170
|
+
export {
|
|
171
|
+
type ArtifactCollector,
|
|
172
|
+
type ArtifactParser,
|
|
173
|
+
type CollectCtx,
|
|
174
|
+
type CollectResult,
|
|
175
|
+
defineCollector,
|
|
176
|
+
defineParser,
|
|
177
|
+
type Outcome,
|
|
178
|
+
type OutputSpec,
|
|
179
|
+
type ParseCtx,
|
|
180
|
+
type ParseResult,
|
|
181
|
+
type SnapshotCtx,
|
|
182
|
+
} from "./output-spec.js";
|
|
183
|
+
export { eq, gt, gte, lt, lte, type NumericPredicate, type Predicate } from "./predicates.js";
|
|
118
184
|
export type {
|
|
119
185
|
ConsumesReadSpec,
|
|
120
186
|
ConsumesSpec,
|
|
@@ -126,27 +192,42 @@ export type {
|
|
|
126
192
|
} from "./skill-contract.js";
|
|
127
193
|
export {
|
|
128
194
|
canCompose,
|
|
195
|
+
getBucketKindMappings,
|
|
129
196
|
legalNextSkills,
|
|
130
197
|
type OutcomeDeriverFn,
|
|
198
|
+
registerBucketKindMapping,
|
|
131
199
|
registerCompositionComparator,
|
|
132
200
|
registerOutcomeDeriver,
|
|
133
201
|
registerSkillContracts,
|
|
134
202
|
registerSkillContractsProvider,
|
|
135
203
|
} from "./skill-contracts/index.js";
|
|
136
204
|
export {
|
|
205
|
+
type LoopCapRow,
|
|
137
206
|
listArtifacts,
|
|
138
207
|
listRuns,
|
|
139
208
|
type RunSummary,
|
|
140
209
|
readHeader,
|
|
141
210
|
readLastStage,
|
|
211
|
+
readLoopCaps,
|
|
142
212
|
resolveRun,
|
|
143
|
-
|
|
144
|
-
stateFilePath
|
|
213
|
+
// Storage layout stays private: `runFileFor` is the one OPAQUE path
|
|
214
|
+
// projection; `runsDir`/`stateFilePath` moved to the test-only internal
|
|
215
|
+
// subpath so external code can't synthesize layout-coupled paths.
|
|
216
|
+
runFileFor,
|
|
217
|
+
type SessionRef,
|
|
218
|
+
STATE_SCHEMA_VERSION,
|
|
145
219
|
type WorkflowHeader,
|
|
146
220
|
type WorkflowStage,
|
|
147
221
|
} from "./state/index.js";
|
|
148
222
|
export { DEFAULT_TRIGGER, type RunTrigger } from "./triggers.js";
|
|
149
223
|
export { typeboxSchema } from "./typebox-adapter.js";
|
|
150
|
-
|
|
224
|
+
// `RunState` is deliberately NOT here — it is runner-private; user contexts
|
|
225
|
+
// use the deep-readonly `RunView`. Test fixtures that must construct one
|
|
226
|
+
// import it from `@juicesharp/rpiv-workflow/internal`.
|
|
227
|
+
export type { RunTermination } from "./types.js";
|
|
151
228
|
export { type SchemaValidationFailure, validateOutputData } from "./validate-output.js";
|
|
152
|
-
export {
|
|
229
|
+
export {
|
|
230
|
+
type ValidationIssueCode,
|
|
231
|
+
validateWorkflow,
|
|
232
|
+
type WorkflowValidationIssue,
|
|
233
|
+
} from "./validate-workflow.js";
|
package/routing-dsl.ts
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The routing DSL — edge vocabulary (`EdgeFn` / `EdgeTarget` / `STOP`) and
|
|
3
|
+
* the route builders (`defineRoute`, `gate`) with their structural markers
|
|
4
|
+
* (`READS_DATA`, `ROUTE_NOTE`). Split out of api.ts: how the graph
|
|
5
|
+
* routes is one concept; what a stage is, is another. Runtime edge
|
|
6
|
+
* EXECUTION lives in routing.ts — this module is authoring-surface only.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { markSymbol, readSymbol, requireNonEmptyString, throwInvalid } from "./internal-utils.js";
|
|
10
|
+
import type { Output, RunView } from "./output.js";
|
|
11
|
+
import type { NumericPredicate } from "./predicates.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Runtime context handed to an `EdgeFn`. The sole context shape for both
|
|
15
|
+
* data-reading and state-only routes (the single `defineRoute` path covers
|
|
16
|
+
* both via `opts.readsData`).
|
|
17
|
+
*/
|
|
18
|
+
export interface EdgeContext {
|
|
19
|
+
output: Output | undefined;
|
|
20
|
+
state: RunView;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Body-type alias for hand-rolled route picks. Internal — users wrap via
|
|
25
|
+
* `defineRoute`, which returns an `EdgeFn` (this alias plus a `.targets`
|
|
26
|
+
* field).
|
|
27
|
+
*/
|
|
28
|
+
type EdgePredicate = (ctx: EdgeContext) => string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A function that picks the next stage name given current state + output.
|
|
32
|
+
* Optional `targets` field lets graph introspectors enumerate possible
|
|
33
|
+
* returns — `gate` and other built-in route builders populate it.
|
|
34
|
+
*/
|
|
35
|
+
export type EdgeFn = EdgePredicate & { targets?: readonly string[] };
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Terminal edge sentinel. Single source of truth for the `"stop"` literal
|
|
39
|
+
* embedded in `EdgeTarget`; `validate-workflow.ts` + `routing.ts` import this rather
|
|
40
|
+
* than re-declaring the string.
|
|
41
|
+
*/
|
|
42
|
+
export const STOP = "stop" as const;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* What an `edges` entry resolves to: another stage name (auto-edge), the
|
|
46
|
+
* terminal sentinel `STOP`, or a function chosen at run-time.
|
|
47
|
+
*/
|
|
48
|
+
export type EdgeTarget = string | typeof STOP | EdgeFn;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Marker attached to EdgeFns that read from `output.data`.
|
|
52
|
+
* `validate-workflow.ts:checkPredicateSchemas` warns when a stage feeds a
|
|
53
|
+
* marked route but has no `outputSchema` — routing on un-validated data
|
|
54
|
+
* is a defect.
|
|
55
|
+
*
|
|
56
|
+
* Default is "marked": `defineRoute(targets, fn)` auto-marks. Opt out by
|
|
57
|
+
* passing `{ readsData: false }` for the rare route that consults only
|
|
58
|
+
* `state` or `output.meta`.
|
|
59
|
+
*
|
|
60
|
+
* Exported as a `Symbol.for` so it survives `import` boundaries cleanly.
|
|
61
|
+
*/
|
|
62
|
+
export const READS_DATA: unique symbol = Symbol.for("rpiv.workflow.readsData");
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* True iff `fn` was wrapped by `defineRoute(...)` with the data-reading
|
|
66
|
+
* marker attached (the default; opt out via `{ readsData: false }`). The
|
|
67
|
+
* validator uses this to decide whether an `EdgeFn`'s source stage must
|
|
68
|
+
* declare an `outputSchema` — data-reading routes need a validated output
|
|
69
|
+
* shape; state-only routes don't.
|
|
70
|
+
*
|
|
71
|
+
* Delegates the symbol-keyed read to `readSymbol` (`internal-utils.ts`) — the
|
|
72
|
+
* single home for the function-object symbol-access cast — so consumers read
|
|
73
|
+
* intent, not mechanics. The `READS_DATA`/`ROUTE_NOTE`/`CANONICAL_FOLD` family
|
|
74
|
+
* all route through the same `readSymbol`/`markSymbol` pair.
|
|
75
|
+
*/
|
|
76
|
+
export function marksReadsData(fn: EdgeFn): boolean {
|
|
77
|
+
return Boolean(readSymbol<boolean>(fn, READS_DATA));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Options for `defineRoute`. */
|
|
81
|
+
export interface DefineRouteOptions {
|
|
82
|
+
/**
|
|
83
|
+
* Whether the route reads `output.data` (the default). Set to `false` for
|
|
84
|
+
* routes that consult only `state` or `output.meta` so the load-time
|
|
85
|
+
* outputSchema lint doesn't warn the source stage lacks an
|
|
86
|
+
* `outputSchema` (a state-derived route has no data-shape contract to
|
|
87
|
+
* validate).
|
|
88
|
+
*/
|
|
89
|
+
readsData?: boolean;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Promote a hand-rolled `EdgePredicate` to an `EdgeFn` by structurally
|
|
94
|
+
* attaching the set of possible returns. `validate-workflow.ts` requires every
|
|
95
|
+
* EdgeFn to carry `.targets` so reachability and load-time edge-target
|
|
96
|
+
* checks see every branch; this factory is the only blessed way to author
|
|
97
|
+
* a multi-branch route.
|
|
98
|
+
*
|
|
99
|
+
* Auto-marks the returned EdgeFn with `READS_DATA` so the outputSchema lint
|
|
100
|
+
* fires when the source stage has no `outputSchema`. If the route consults
|
|
101
|
+
* only `state` / `output.meta` and never reads `output.data`, pass
|
|
102
|
+
* `{ readsData: false }` to suppress the lint.
|
|
103
|
+
*
|
|
104
|
+
* Throws if `targets` is empty — a route that can't return anything
|
|
105
|
+
* declared is by definition a bug.
|
|
106
|
+
*/
|
|
107
|
+
export function defineRoute(targets: readonly string[], fn: EdgePredicate, opts?: DefineRouteOptions): EdgeFn {
|
|
108
|
+
if (targets.length === 0) {
|
|
109
|
+
throwInvalid("defineRoute", "targets must declare at least one possible return value");
|
|
110
|
+
}
|
|
111
|
+
// Fresh delegating wrapper — NEVER mutate the caller's function. Reusing
|
|
112
|
+
// one predicate across two defineRoute calls must not alias their targets
|
|
113
|
+
// (the second call would overwrite the first's), and `readsData: false`
|
|
114
|
+
// must not inherit a marker a prior call attached.
|
|
115
|
+
const wrapped: EdgeFn = (ctx) => fn(ctx);
|
|
116
|
+
wrapped.targets = [...targets];
|
|
117
|
+
if (opts?.readsData !== false) markSymbol(wrapped, READS_DATA, true);
|
|
118
|
+
return wrapped;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Symbol under which an `EdgeFn` records a note about its most recent pick —
|
|
123
|
+
* today: `gate`'s "no branch matched, fallback fired" diagnostic. The runner's
|
|
124
|
+
* routing audit reads-and-clears it via `takeRouteNote` right after invoking
|
|
125
|
+
* the edge (same tick — single-threaded, no other decision can interleave)
|
|
126
|
+
* and persists it on the `RoutingDecision` row's `note`.
|
|
127
|
+
*
|
|
128
|
+
* Framework plumbing, not authoring surface — NOT re-exported from
|
|
129
|
+
* `registration.ts`. `Symbol.for` so it survives import boundaries, matching
|
|
130
|
+
* `READS_DATA`.
|
|
131
|
+
*/
|
|
132
|
+
export const ROUTE_NOTE: unique symbol = Symbol.for("rpiv.workflow.routeNote");
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Read-and-clear the note an `EdgeFn` attached to its most recent invocation.
|
|
136
|
+
* Returns undefined when the edge recorded nothing (the common case).
|
|
137
|
+
*/
|
|
138
|
+
export function takeRouteNote(fn: EdgeFn): string | undefined {
|
|
139
|
+
const note = readSymbol<string>(fn, ROUTE_NOTE);
|
|
140
|
+
if (note !== undefined) markSymbol(fn, ROUTE_NOTE, undefined);
|
|
141
|
+
return note;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Matches the keys JS engines hoist and reorder ahead of string keys
|
|
146
|
+
* (canonical array indices). `gate` evaluates branches in declaration order,
|
|
147
|
+
* so an integer-like stage name would silently change match priority.
|
|
148
|
+
*/
|
|
149
|
+
const INTEGER_LIKE_KEY = /^\d+$/;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Shared branch-key guards for `gate`/`match`: at least one branch must be
|
|
153
|
+
* declared, and no key may be integer-like (`"2"`) — JS hoists array-index keys
|
|
154
|
+
* ahead of declaration order, silently reordering match priority. `factory`
|
|
155
|
+
* names the throwing builder; `renameHint` is the builder-specific tail of the
|
|
156
|
+
* integer-key message (gate offers `otherwise`, match does not).
|
|
157
|
+
*/
|
|
158
|
+
function validateBranchKeys(factory: "gate" | "match", branchTargets: string[], renameHint: string): void {
|
|
159
|
+
if (branchTargets.length === 0) {
|
|
160
|
+
throwInvalid(factory, "branches must declare at least one possible return value");
|
|
161
|
+
}
|
|
162
|
+
for (const key of branchTargets) {
|
|
163
|
+
if (INTEGER_LIKE_KEY.test(key)) {
|
|
164
|
+
throwInvalid(
|
|
165
|
+
factory,
|
|
166
|
+
`branch key "${key}" is integer-like — JS reorders such keys ahead of declaration order, silently changing match priority. ${renameHint}`,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Conditional routing keyed on a numeric field in `output.data`. Each
|
|
174
|
+
* branch's predicate is evaluated against `Number(output.data[field])` in
|
|
175
|
+
* declaration order; the first matching branch wins. When no predicate
|
|
176
|
+
* matches (including a missing or non-numeric field, which coerces to `NaN`),
|
|
177
|
+
* the EXPLICIT `otherwise` branch is taken and the routing-audit row carries
|
|
178
|
+
* a `note` saying the fallback fired — no-match is a visible event, not a
|
|
179
|
+
* silent property of declaration order.
|
|
180
|
+
*
|
|
181
|
+
* ```ts
|
|
182
|
+
* gate("blockers_count", { revise: gt(0), commit: eq(0) }, "commit")
|
|
183
|
+
* // value > 0 → "revise"
|
|
184
|
+
* // value = 0 → "commit"
|
|
185
|
+
* // value < 0, missing, NaN → "commit" (otherwise; audit row notes the fallback)
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* Branch keys must not be integer-like (`"2"`): JS object literals hoist
|
|
189
|
+
* array-index keys ahead of declaration order, which would silently reorder
|
|
190
|
+
* match priority — rejected at construction.
|
|
191
|
+
*
|
|
192
|
+
* Built on `defineRoute` so the `.targets` metadata is attached structurally
|
|
193
|
+
* (the `otherwise` target included) and the `READS_DATA` marker auto-applies
|
|
194
|
+
* (routing reads `output.data`).
|
|
195
|
+
*/
|
|
196
|
+
export function gate(field: string, branches: Record<string, NumericPredicate>, otherwise: string): EdgeFn {
|
|
197
|
+
const branchTargets = Object.keys(branches);
|
|
198
|
+
validateBranchKeys("gate", branchTargets, "Rename the stage or route to it via `otherwise`.");
|
|
199
|
+
requireNonEmptyString(
|
|
200
|
+
otherwise,
|
|
201
|
+
"gate",
|
|
202
|
+
"an explicit `otherwise` branch is required — the no-match fallback must be deliberate",
|
|
203
|
+
);
|
|
204
|
+
const targets = [...new Set([...branchTargets, otherwise])];
|
|
205
|
+
const route: EdgeFn = defineRoute(targets, ({ output }) => {
|
|
206
|
+
const value = Number((output?.data as Record<string, unknown> | undefined)?.[field]);
|
|
207
|
+
for (const target of branchTargets) {
|
|
208
|
+
if (branches[target]!(value)) return target;
|
|
209
|
+
}
|
|
210
|
+
markSymbol(route, ROUTE_NOTE, `gate("${field}"): no branch matched value ${value} — fell back to "${otherwise}"`);
|
|
211
|
+
return otherwise;
|
|
212
|
+
});
|
|
213
|
+
return route;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** A discrete value a `match` branch compares its field against (strict `===`). */
|
|
217
|
+
export type MatchValue = string | number | boolean;
|
|
218
|
+
|
|
219
|
+
/** Options for `match`. */
|
|
220
|
+
export interface MatchOptions {
|
|
221
|
+
/**
|
|
222
|
+
* Stage to route to when no branch value matches. Optional: when omitted, an
|
|
223
|
+
* unmatched value TERMINATES the chain (`STOP`). Either way the no-match is a
|
|
224
|
+
* visible event — the routing-audit row carries a `note`. Provide a fallback
|
|
225
|
+
* to keep the run going on an unexpected value (the `triage` shape).
|
|
226
|
+
*/
|
|
227
|
+
fallback?: string;
|
|
228
|
+
/**
|
|
229
|
+
* Read the matched field from a NAMED CHANNEL's latest output
|
|
230
|
+
* (`state.named[from].at(-1).data[field]`) instead of the stage's projected
|
|
231
|
+
* `output.data`. This is how a `match` routes on a panel's PUBLISHED verdict
|
|
232
|
+
* (the `<stage>-panel` channel) — the fold lands on a channel, never on the
|
|
233
|
+
* stage's projected output (which stays the producer artifact). A
|
|
234
|
+
* channel-sourced route validates the channel's data, not the source stage's
|
|
235
|
+
* output, so it does NOT mark the source stage `READS_DATA`.
|
|
236
|
+
*/
|
|
237
|
+
from?: string;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Conditional routing keyed on an ENUM field — the string/boolean companion to
|
|
242
|
+
* the numeric `gate`. Each branch maps a target stage to the discrete value that
|
|
243
|
+
* routes to it; the field is compared by strict `===` in declaration order and
|
|
244
|
+
* the first match wins:
|
|
245
|
+
*
|
|
246
|
+
* ```ts
|
|
247
|
+
* match("severity", { escalate: "p0", fix: "p1", backlog: "p2" })
|
|
248
|
+
* match("tie", { escalate: true }, { fallback: "keep", from: "review-panel" })
|
|
249
|
+
* ```
|
|
250
|
+
*
|
|
251
|
+
* Sourcing (`opts.from`) lets a `match` branch on a panel's published verdict —
|
|
252
|
+
* `match("tie", …, { from: "<stage>-panel" })` reads the folded `PANEL_VERDICT`
|
|
253
|
+
* off its channel, the disagreement-routing companion to `panel()`.
|
|
254
|
+
*
|
|
255
|
+
* No-match handling is EXPLICIT, never silent: with an `opts.fallback` the
|
|
256
|
+
* unmatched value routes there; without one it terminates (`STOP`). Both record
|
|
257
|
+
* a routing-audit `note`.
|
|
258
|
+
*
|
|
259
|
+
* Each enum value must map to exactly one stage (a duplicate value is ambiguous
|
|
260
|
+
* → construction error), and branch keys must not be integer-like (`"2"`) — JS
|
|
261
|
+
* hoists array-index keys ahead of declaration order, silently reordering match
|
|
262
|
+
* priority. Built on `defineRoute`, so `.targets` is attached structurally
|
|
263
|
+
* (reachability BFS sees every branch incl. the fallback) and `READS_DATA`
|
|
264
|
+
* auto-applies (unless `from` sources a channel instead of the stage output).
|
|
265
|
+
*/
|
|
266
|
+
export function match(field: string, branches: Record<string, MatchValue>, opts?: MatchOptions): EdgeFn {
|
|
267
|
+
const branchTargets = Object.keys(branches);
|
|
268
|
+
validateBranchKeys("match", branchTargets, "Rename the stage.");
|
|
269
|
+
const claimedBy = new Map<string, string>();
|
|
270
|
+
for (const key of branchTargets) {
|
|
271
|
+
// Type-tag the value so 0/"0"/false stay distinct when deduping.
|
|
272
|
+
const value = branches[key]!;
|
|
273
|
+
const valueKey = `${typeof value}:${String(value)}`;
|
|
274
|
+
const prior = claimedBy.get(valueKey);
|
|
275
|
+
if (prior !== undefined) {
|
|
276
|
+
throwInvalid(
|
|
277
|
+
"match",
|
|
278
|
+
`value ${JSON.stringify(value)} is claimed by both "${prior}" and "${key}" — each enum value must map to exactly one stage`,
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
claimedBy.set(valueKey, key);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const fallback = opts?.fallback;
|
|
285
|
+
if (fallback !== undefined && (typeof fallback !== "string" || fallback.length === 0)) {
|
|
286
|
+
requireNonEmptyString(fallback, "match", "`fallback`, when provided, must be a non-empty stage name");
|
|
287
|
+
}
|
|
288
|
+
const noMatch = fallback ?? STOP;
|
|
289
|
+
const targets = [...new Set([...branchTargets, noMatch])];
|
|
290
|
+
|
|
291
|
+
const from = opts?.from;
|
|
292
|
+
const route: EdgeFn = defineRoute(
|
|
293
|
+
targets,
|
|
294
|
+
({ output, state }) => {
|
|
295
|
+
// `.at(-1)` on a PRE-SIZED produces-fanout channel could read a
|
|
296
|
+
// pending (`undefined`) or failed-sentinel tail. Safe by ordering: routes
|
|
297
|
+
// fire at `finishLoop`, AFTER `foldFanoutCompletion` has filled every
|
|
298
|
+
// settled slot, so the channel is never observed half-filled here.
|
|
299
|
+
const source =
|
|
300
|
+
from !== undefined
|
|
301
|
+
? (state.named[from]?.at(-1)?.data as Record<string, unknown> | undefined)
|
|
302
|
+
: (output?.data as Record<string, unknown> | undefined);
|
|
303
|
+
const raw = source?.[field];
|
|
304
|
+
for (const target of branchTargets) {
|
|
305
|
+
if (raw === branches[target]) return target;
|
|
306
|
+
}
|
|
307
|
+
markSymbol(
|
|
308
|
+
route,
|
|
309
|
+
ROUTE_NOTE,
|
|
310
|
+
`match("${field}"): value ${JSON.stringify(raw ?? null)} matched no branch — ${
|
|
311
|
+
fallback ? `fell back to "${fallback}"` : `terminated (no fallback)`
|
|
312
|
+
}`,
|
|
313
|
+
);
|
|
314
|
+
return noMatch;
|
|
315
|
+
},
|
|
316
|
+
// A channel-sourced match validates the CHANNEL's data, not the source
|
|
317
|
+
// stage's projected output — so don't demand the source stage carry an
|
|
318
|
+
// outputSchema. A stage-output match reads `output.data` (default mark).
|
|
319
|
+
from !== undefined ? { readsData: false } : undefined,
|
|
320
|
+
);
|
|
321
|
+
return route;
|
|
322
|
+
}
|
package/routing.ts
CHANGED
|
@@ -8,12 +8,19 @@
|
|
|
8
8
|
* routing layer detected a violation (an `EdgeFn` body threw, or an
|
|
9
9
|
* `EdgeFn` returned an undeclared target).
|
|
10
10
|
*
|
|
11
|
+
* `{ kind: "stop" }` (and "terminal stages" above/below) is the GRAPH-SINK
|
|
12
|
+
* sense — a stage with no outgoing edge OR an explicit `STOP`. Unrelated to
|
|
13
|
+
* the `terminal()` stage factory (stage-def.ts) and to "terminal failure"
|
|
14
|
+
* run-outcome prose (audit.ts). See the glossary on `stage-def.ts`'s
|
|
15
|
+
* `terminal` export. The `kind: "stop"` literal is untouched.
|
|
16
|
+
*
|
|
11
17
|
* Errors are returned, not thrown. The caller (runner) switches on
|
|
12
18
|
* `kind` and routes `"err"` through `recordTerminalFailure` — same as
|
|
13
19
|
* any other halt site.
|
|
14
20
|
*/
|
|
15
21
|
|
|
16
22
|
import { type EdgeContext, type EdgeFn, STOP, type Workflow } from "./api.js";
|
|
23
|
+
import { formatError } from "./internal-utils.js";
|
|
17
24
|
|
|
18
25
|
/**
|
|
19
26
|
* Three-way return from `nextStage`. Matches the convention established by
|
|
@@ -50,6 +57,46 @@ export function edgeIsDecision(workflow: Workflow, current: string): boolean {
|
|
|
50
57
|
return typeof workflow.edges[current] === "function";
|
|
51
58
|
}
|
|
52
59
|
|
|
60
|
+
/**
|
|
61
|
+
* The not-taken targets of a DECISION edge that are pure RECOVERY arms — a
|
|
62
|
+
* stage whose own out-edge is a STRING edge pointing at an already-visited
|
|
63
|
+
* stage, i.e. taking it would loop BACK into covered territory rather than make
|
|
64
|
+
* forward progress.
|
|
65
|
+
*
|
|
66
|
+
* Used by the progress bridge (`rpiv-pi` lane-progress): when a gate takes its
|
|
67
|
+
* pass arm, the failure arm (build's `slice-fix` / `plan-fix` / `code-fix`) is bypassed for good
|
|
68
|
+
* on this path, so it counts toward "distinct nodes covered" — the bar reaches
|
|
69
|
+
* full WHILE the terminal stage runs instead of freezing one-below until the
|
|
70
|
+
* `onWorkflowEnd` snap. Surfaced as the `onRoute` lifecycle event's `bypassed`
|
|
71
|
+
* argument.
|
|
72
|
+
*
|
|
73
|
+
* Deliberately narrow: a not-taken target is credited ONLY when its successor
|
|
74
|
+
* is a string edge to an already-visited stage. A FORWARD arm (its successor
|
|
75
|
+
* not yet visited) is merely deferred, not bypassed — crediting it would push
|
|
76
|
+
* the numerator AHEAD of the stages actually reached. An arm with a gate/
|
|
77
|
+
* terminal out-edge is left uncredited (conservative; never over-counts).
|
|
78
|
+
*
|
|
79
|
+
* Returns distinct names; `chosen`, `STOP`, and already-visited arms are
|
|
80
|
+
* excluded, so the credited set never inflates the numerator past the reachable
|
|
81
|
+
* total.
|
|
82
|
+
*/
|
|
83
|
+
export function bypassedRecoveryArms(
|
|
84
|
+
workflow: Workflow,
|
|
85
|
+
from: string,
|
|
86
|
+
chosen: string,
|
|
87
|
+
visited: ReadonlySet<string>,
|
|
88
|
+
): string[] {
|
|
89
|
+
const edge = workflow.edges[from];
|
|
90
|
+
if (typeof edge !== "function" || !Array.isArray(edge.targets)) return [];
|
|
91
|
+
const out: string[] = [];
|
|
92
|
+
for (const t of edge.targets) {
|
|
93
|
+
if (t === chosen || t === STOP || visited.has(t)) continue;
|
|
94
|
+
const succ = workflow.edges[t]; // one-hop loop-back test
|
|
95
|
+
if (typeof succ === "string" && visited.has(succ)) out.push(t);
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
|
|
53
100
|
// ---------------------------------------------------------------------------
|
|
54
101
|
// Internals
|
|
55
102
|
// ---------------------------------------------------------------------------
|
|
@@ -62,10 +109,9 @@ function invokeEdgeFn(
|
|
|
62
109
|
try {
|
|
63
110
|
return { kind: "ok", value: fn(ctx) };
|
|
64
111
|
} catch (e) {
|
|
65
|
-
const msg = e instanceof Error ? e.message : String(e);
|
|
66
112
|
return {
|
|
67
113
|
kind: "err",
|
|
68
|
-
reason: `workflow edge function at "${current}" threw: ${
|
|
114
|
+
reason: `workflow edge function at "${current}" threw: ${formatError(e)}`,
|
|
69
115
|
};
|
|
70
116
|
}
|
|
71
117
|
}
|
package/runner/by-name.ts
CHANGED
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
|
|
22
22
|
import type { WorkflowHostContext } from "../host.js";
|
|
23
23
|
import { findWorkflow, loadWorkflows } from "../load/index.js";
|
|
24
|
-
import {
|
|
24
|
+
import type { RunWorkflowOptions, RunWorkflowResult } from "../types.js";
|
|
25
|
+
import { runWorkflow } from "./runner.js";
|
|
25
26
|
|
|
26
27
|
/**
|
|
27
28
|
* Options for `runWorkflowByName` — the full `RunWorkflowOptions` surface
|
package/runner/by-run-id.ts
CHANGED
|
@@ -36,7 +36,8 @@ import type { WorkflowHostContext } from "../host.js";
|
|
|
36
36
|
import { findWorkflow, loadWorkflows } from "../load/index.js";
|
|
37
37
|
import { MSG_LOAD_ABORTED, MSG_RESUME_WORKFLOW_GONE, MSG_RUN_NOT_FOUND } from "../messages.js";
|
|
38
38
|
import { resolveRun } from "../state/index.js";
|
|
39
|
-
import
|
|
39
|
+
import type { RunWorkflowResult } from "../types.js";
|
|
40
|
+
import { type ResumeWorkflowOptions, resumeWorkflow } from "./runner.js";
|
|
40
41
|
|
|
41
42
|
/**
|
|
42
43
|
* Options for `resumeWorkflowByRunId` — the full `ResumeWorkflowOptions` surface
|