stageflow 0.8.0 → 0.12.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 (159) hide show
  1. package/README.md +24 -14
  2. package/dist/agent/activity.d.ts +9 -0
  3. package/dist/agent/agentBackend.d.ts +32 -0
  4. package/dist/agent/agentBackend.js +38 -0
  5. package/dist/agent/claudeActivity.d.ts +15 -0
  6. package/dist/agent/claudeActivity.js +58 -0
  7. package/dist/agent/claudeAdapter.d.ts +5 -0
  8. package/dist/agent/claudeAdapter.js +301 -0
  9. package/dist/agent/claudeSession.d.ts +18 -0
  10. package/dist/agent/claudeSession.js +54 -0
  11. package/dist/agent/claudeTools.d.ts +89 -0
  12. package/dist/agent/claudeTools.js +135 -0
  13. package/dist/agent/fakeAgent.js +24 -4
  14. package/dist/agent/piAdapter.d.ts +6 -3
  15. package/dist/agent/piAdapter.js +129 -21
  16. package/dist/agent/port.d.ts +50 -1
  17. package/dist/agent/resolveAgentPort.d.ts +26 -0
  18. package/dist/agent/resolveAgentPort.js +39 -0
  19. package/dist/cli/runCommand.js +2 -2
  20. package/dist/cli/runsCommand.d.ts +22 -0
  21. package/dist/cli/runsCommand.js +773 -0
  22. package/dist/cli/validateOutput.d.ts +2 -2
  23. package/dist/cli/validateOutput.js +2 -2
  24. package/dist/cli.d.ts +1 -1
  25. package/dist/cli.js +43 -5
  26. package/dist/config/browseCatalog.js +1 -1
  27. package/dist/config/createPipeline.d.ts +2 -1
  28. package/dist/config/createPipeline.js +34 -9
  29. package/dist/config/createStage.js +14 -9
  30. package/dist/config/loadPipeline.js +10 -1
  31. package/dist/config/loadStage.js +138 -2
  32. package/dist/config/loadStageflowManifest.js +14 -1
  33. package/dist/config/mergePipelineIncludes.d.ts +1 -0
  34. package/dist/config/mergePipelineIncludes.js +14 -1
  35. package/dist/config/normalizePipelineStageEntry.d.ts +7 -2
  36. package/dist/config/normalizePipelineStageEntry.js +49 -6
  37. package/dist/config/parseCompletionContract.d.ts +8 -0
  38. package/dist/config/parseCompletionContract.js +218 -0
  39. package/dist/config/parsePreEmitChecks.d.ts +3 -0
  40. package/dist/config/parsePreEmitChecks.js +65 -0
  41. package/dist/config/pipelineNeeds.d.ts +19 -0
  42. package/dist/config/pipelineNeeds.js +151 -0
  43. package/dist/config/pipelineStageKeys.js +16 -1
  44. package/dist/config/resolveForkEmitContext.d.ts +6 -2
  45. package/dist/config/resolveForkEmitContext.js +9 -2
  46. package/dist/config/resolvePipelineDag.d.ts +2 -1
  47. package/dist/config/resolvePipelineDag.js +182 -39
  48. package/dist/config/validateCatalog.d.ts +1 -1
  49. package/dist/config/validateCompletionContract.d.ts +4 -0
  50. package/dist/config/validateCompletionContract.js +52 -0
  51. package/dist/envelope/check.d.ts +1 -1
  52. package/dist/envelope/check.js +86 -13
  53. package/dist/envelope/cloneForks.d.ts +1 -1
  54. package/dist/envelope/cloneForks.js +24 -9
  55. package/dist/envelope/feedbackLoop.d.ts +4 -0
  56. package/dist/envelope/feedbackLoop.js +31 -0
  57. package/dist/envelope/payloadSchema.d.ts +7 -1
  58. package/dist/envelope/payloadSchema.js +170 -13
  59. package/dist/envelope/preEmitChecks.d.ts +29 -0
  60. package/dist/envelope/preEmitChecks.js +57 -0
  61. package/dist/hitl/qaTrail.d.ts +25 -0
  62. package/dist/hitl/qaTrail.js +51 -0
  63. package/dist/index.d.ts +1 -1
  64. package/dist/index.js +1 -1
  65. package/dist/mcp/catalogTools.js +4 -2
  66. package/dist/mcp/controlTools.js +72 -0
  67. package/dist/mcp/waitingGates.d.ts +2 -1
  68. package/dist/mcp/waitingGates.js +17 -1
  69. package/dist/package-meta.d.ts +1 -0
  70. package/dist/package-meta.js +1 -0
  71. package/dist/projection/projectRun.d.ts +4 -1
  72. package/dist/projection/projectRun.js +7 -0
  73. package/dist/prompt/feedbackLoopContext.d.ts +2 -0
  74. package/dist/prompt/feedbackLoopContext.js +17 -0
  75. package/dist/prompt/priorEnvelope.d.ts +2 -2
  76. package/dist/prompt/priorEnvelope.js +4 -1
  77. package/dist/runstore/pipelineDagSnapshot.d.ts +1 -0
  78. package/dist/runstore/pipelineDagSnapshot.js +51 -11
  79. package/dist/runstore/port.d.ts +185 -3
  80. package/dist/runstore/port.js +42 -3
  81. package/dist/runstore/runProjection.d.ts +7 -3
  82. package/dist/runstore/runProjection.js +30 -15
  83. package/dist/runstore/sqlite/SqliteRunStore.d.ts +25 -1
  84. package/dist/runstore/sqlite/SqliteRunStore.js +626 -8
  85. package/dist/runstore/sqlite/schema.d.ts +1 -1
  86. package/dist/runstore/sqlite/schema.js +99 -0
  87. package/dist/runstore/stageInstanceId.d.ts +1 -1
  88. package/dist/runstore/stageInstanceId.js +3 -2
  89. package/dist/runstore/trackProjection.js +59 -27
  90. package/dist/runstore/verificationHistory.d.ts +19 -0
  91. package/dist/runstore/verificationHistory.js +30 -0
  92. package/dist/runtime/cloneSchedule.d.ts +40 -4
  93. package/dist/runtime/cloneSchedule.js +212 -28
  94. package/dist/runtime/completionCheckRunner.d.ts +137 -0
  95. package/dist/runtime/completionCheckRunner.js +365 -0
  96. package/dist/runtime/completionCheckoutBaseline.d.ts +3 -0
  97. package/dist/runtime/completionCheckoutBaseline.js +44 -0
  98. package/dist/runtime/envelopeRouting.d.ts +10 -3
  99. package/dist/runtime/envelopeRouting.js +146 -5
  100. package/dist/runtime/feedbackLoopCoordinator.d.ts +74 -0
  101. package/dist/runtime/feedbackLoopCoordinator.js +446 -0
  102. package/dist/runtime/feedbackLoopDecision.d.ts +44 -0
  103. package/dist/runtime/feedbackLoopDecision.js +207 -0
  104. package/dist/runtime/feedbackLoopRoute.d.ts +5 -0
  105. package/dist/runtime/feedbackLoopRoute.js +13 -0
  106. package/dist/runtime/feedbackLoopSchedule.d.ts +38 -0
  107. package/dist/runtime/feedbackLoopSchedule.js +72 -0
  108. package/dist/runtime/forkGeneration.d.ts +72 -0
  109. package/dist/runtime/forkGeneration.js +295 -0
  110. package/dist/runtime/gitCheckoutCapability.d.ts +7 -0
  111. package/dist/runtime/gitCheckoutCapability.js +97 -0
  112. package/dist/runtime/manualRecoveryState.d.ts +40 -0
  113. package/dist/runtime/manualRecoveryState.js +73 -0
  114. package/dist/runtime/pipelineScheduler.d.ts +13 -1
  115. package/dist/runtime/pipelineScheduler.js +328 -45
  116. package/dist/runtime/replayLifecycle.d.ts +107 -0
  117. package/dist/runtime/replayLifecycle.js +168 -0
  118. package/dist/runtime/runManager.d.ts +61 -1
  119. package/dist/runtime/runManager.js +501 -7
  120. package/dist/runtime/runRetryCoordinator.d.ts +8 -1
  121. package/dist/runtime/runRetryCoordinator.js +47 -10
  122. package/dist/runtime/stageAttemptBootstrap.d.ts +3 -1
  123. package/dist/runtime/stageAttemptBootstrap.js +79 -1
  124. package/dist/runtime/stageProcessLauncher.d.ts +1 -1
  125. package/dist/runtime/stageRecovery.js +1 -1
  126. package/dist/runtime/stageRunner.d.ts +4 -1
  127. package/dist/runtime/stageRunner.js +29 -3
  128. package/dist/runtime/stageWorker.js +69 -2
  129. package/dist/runtime/stageWorkerProtocol.d.ts +1 -1
  130. package/dist/runtime/verifiedStageExecution.d.ts +29 -0
  131. package/dist/runtime/verifiedStageExecution.js +119 -0
  132. package/dist/server/bootstrap.js +1 -0
  133. package/dist/server/http.js +114 -0
  134. package/dist/server/operatorResults.js +2 -0
  135. package/dist/tools/askOperator.d.ts +161 -5
  136. package/dist/tools/askOperator.js +58 -25
  137. package/dist/tools/emitStageEnvelope.d.ts +12 -12
  138. package/dist/tools/emitStageEnvelope.js +112 -24
  139. package/dist/types/completion.d.ts +43 -0
  140. package/dist/types/completion.js +1 -0
  141. package/dist/types/envelope.d.ts +18 -0
  142. package/dist/types/forkChoice.d.ts +9 -4
  143. package/dist/types/forkChoice.js +1 -1
  144. package/dist/types/pipeline.d.ts +39 -2
  145. package/dist/types/preEmitCheck.d.ts +18 -0
  146. package/dist/types/preEmitCheck.js +1 -0
  147. package/dist/types/stage.d.ts +13 -0
  148. package/dist/types/stageflowManifest.d.ts +2 -0
  149. package/dist/ui/assets/index-CXGz4nL7.js +118 -0
  150. package/dist/ui/assets/{index-C3N7MXAC.css → index-uVHiae8s.css} +1 -1
  151. package/dist/ui/index.html +2 -2
  152. package/package.json +2 -1
  153. package/skills/stageflow/references/control-surface.md +4 -2
  154. package/skills/stageflow-author/references/catalog-mapping.md +26 -2
  155. package/skills/stageflow-author/references/catalog-write-conventions.md +1 -1
  156. package/skills/stageflow-run/SKILL.md +56 -25
  157. package/skills/stageflow-run/references/mcp-call.md +2 -2
  158. package/skills/stageflow-run/references/native-question-ui.md +78 -0
  159. package/dist/ui/assets/index-CSoGDA3A.js +0 -118
@@ -1,6 +1,9 @@
1
1
  import path from "node:path";
2
2
  import { loadFailure, loadSuccess } from "./loadOutcome.js";
3
3
  import { BODY_KEYS, isAllowedPipelineStageEntryKey, } from "./pipelineStageKeys.js";
4
+ import { parseExecutionPolicy } from "./parseCompletionContract.js";
5
+ import { parsePipelineNeeds } from "./pipelineNeeds.js";
6
+ import { parseFeedbackLoopConfig } from "./resolvePipelineDag.js";
4
7
  function isPlainObject(value) {
5
8
  return typeof value === "object" && value !== null && !Array.isArray(value);
6
9
  }
@@ -130,27 +133,49 @@ export function normalizePipelineStageEntries(rawEntries, ctx) {
130
133
  },
131
134
  ]);
132
135
  }
133
- if (raw.needs !== undefined) {
134
- if (Array.isArray(raw.needs)) {
136
+ const policyOutcome = parseExecutionPolicy(raw, id);
137
+ if (!policyOutcome.ok)
138
+ return policyOutcome;
139
+ let needs;
140
+ let feedbackLoop;
141
+ if (raw.feedback_loop !== undefined) {
142
+ try {
143
+ feedbackLoop = parseFeedbackLoopConfig(raw.feedback_loop, id, ctx);
144
+ }
145
+ catch (err) {
135
146
  return loadFailure([
136
147
  {
137
148
  code: "pipeline.dag_error",
138
- message: `Pipeline ${ctx.pipelineId} (${ctx.path}): stage "${id}": needs must be a string, not an array`,
149
+ message: err instanceof Error ? err.message : String(err),
139
150
  category: "pipeline",
140
151
  pipelineId: ctx.pipelineId,
141
152
  },
142
153
  ]);
143
154
  }
144
- if (typeof raw.needs !== "string" || !raw.needs) {
155
+ }
156
+ if (raw.replay_safe !== undefined && typeof raw.replay_safe !== "boolean") {
157
+ return loadFailure([
158
+ {
159
+ code: "pipeline.dag_error",
160
+ message: `Pipeline ${ctx.pipelineId} (${ctx.path}): stage "${id}": replay_safe must be a boolean`,
161
+ category: "pipeline",
162
+ pipelineId: ctx.pipelineId,
163
+ },
164
+ ]);
165
+ }
166
+ if (raw.needs !== undefined) {
167
+ const parsedNeeds = parsePipelineNeeds(raw.needs, id);
168
+ if (!parsedNeeds.ok) {
145
169
  return loadFailure([
146
170
  {
147
171
  code: "pipeline.dag_error",
148
- message: `Pipeline ${ctx.pipelineId} (${ctx.path}): stage "${id}": needs must be a non-empty string`,
172
+ message: `Pipeline ${ctx.pipelineId} (${ctx.path}): ${parsedNeeds.message}`,
149
173
  category: "pipeline",
150
174
  pipelineId: ctx.pipelineId,
151
175
  },
152
176
  ]);
153
177
  }
178
+ needs = parsedNeeds.value;
154
179
  }
155
180
  let forkValue;
156
181
  if (raw.fork !== undefined) {
@@ -178,10 +203,22 @@ export function normalizePipelineStageEntries(rawEntries, ctx) {
178
203
  id,
179
204
  declaringPath,
180
205
  body,
181
- ...(typeof raw.needs === "string" ? { needs: raw.needs } : {}),
206
+ ...(needs !== undefined ? { needs } : {}),
182
207
  ...(forkValue !== undefined ? { fork: forkValue } : {}),
183
208
  ...(raw.clonable !== undefined ? { clonable: raw.clonable } : {}),
184
209
  ...(raw.clone_cap !== undefined ? { clone_cap: raw.clone_cap } : {}),
210
+ ...(policyOutcome.value.completion !== undefined
211
+ ? { completion: policyOutcome.value.completion }
212
+ : {}),
213
+ ...(policyOutcome.value.recovery !== undefined
214
+ ? { recovery: policyOutcome.value.recovery }
215
+ : {}),
216
+ ...(feedbackLoop !== undefined
217
+ ? { feedback_loop: feedbackLoop }
218
+ : {}),
219
+ ...(raw.replay_safe !== undefined
220
+ ? { replay_safe: raw.replay_safe }
221
+ : {}),
185
222
  ...(skill !== undefined ? { skill } : {}),
186
223
  };
187
224
  const priorPath = normalized.find((e) => e.id === id)?.declaringPath;
@@ -206,5 +243,11 @@ export function toWiringRefs(entries) {
206
243
  ...(entry.fork !== undefined ? { fork: entry.fork } : {}),
207
244
  ...(entry.clonable !== undefined ? { clonable: entry.clonable } : {}),
208
245
  ...(entry.clone_cap !== undefined ? { clone_cap: entry.clone_cap } : {}),
246
+ ...(entry.completion !== undefined ? { completion: entry.completion } : {}),
247
+ ...(entry.recovery !== undefined ? { recovery: entry.recovery } : {}),
248
+ ...(entry.feedback_loop !== undefined
249
+ ? { feedback_loop: entry.feedback_loop }
250
+ : {}),
251
+ ...(entry.replay_safe !== undefined ? { replay_safe: entry.replay_safe } : {}),
209
252
  }));
210
253
  }
@@ -0,0 +1,8 @@
1
+ import type { CompletionContract, RecoveryPolicy } from "../types/completion.js";
2
+ import { type LoadOutcome } from "./loadOutcome.js";
3
+ type ExecutionPolicy = {
4
+ completion?: CompletionContract;
5
+ recovery?: RecoveryPolicy;
6
+ };
7
+ export declare function parseExecutionPolicy(raw: Record<string, unknown>, stageId: string): LoadOutcome<ExecutionPolicy>;
8
+ export {};
@@ -0,0 +1,218 @@
1
+ import path from "node:path";
2
+ import { STAGE_GATE_KINDS } from "../types/stage.js";
3
+ import { loadFailure, loadSuccess } from "./loadOutcome.js";
4
+ function isPlainObject(value) {
5
+ return typeof value === "object" && value !== null && !Array.isArray(value);
6
+ }
7
+ function failure(stageId, field, message) {
8
+ return loadFailure([
9
+ {
10
+ code: field === "completion"
11
+ ? "pipeline.invalid_completion"
12
+ : "pipeline.invalid_recovery",
13
+ message: `Stage "${stageId}" ${field}: ${message}`,
14
+ category: "pipeline",
15
+ },
16
+ ]);
17
+ }
18
+ function hasOnlyKeys(value, allowed) {
19
+ return Object.keys(value).find((key) => !allowed.includes(key));
20
+ }
21
+ function isPositiveInteger(value) {
22
+ return typeof value === "number" && Number.isSafeInteger(value) && value > 0;
23
+ }
24
+ function isGateKind(value) {
25
+ return typeof value === "string" && STAGE_GATE_KINDS.includes(value);
26
+ }
27
+ function parsePathFields(value) {
28
+ if (value === undefined)
29
+ return undefined;
30
+ if (!Array.isArray(value) ||
31
+ value.length === 0 ||
32
+ !value.every((item) => typeof item === "string" && item.trim() !== "")) {
33
+ return undefined;
34
+ }
35
+ const fields = value.map((item) => item.trim());
36
+ return new Set(fields).size === fields.length ? fields : undefined;
37
+ }
38
+ function parseCheck(raw, stageId, index) {
39
+ if (!isPlainObject(raw)) {
40
+ return failure(stageId, "completion", `checks[${index}] must be an object`);
41
+ }
42
+ if (typeof raw.id !== "string" || raw.id.trim() === "") {
43
+ return failure(stageId, "completion", `checks[${index}].id must be a non-empty string`);
44
+ }
45
+ if (typeof raw.type !== "string") {
46
+ return failure(stageId, "completion", `checks[${index}].type must be a string`);
47
+ }
48
+ const id = raw.id.trim();
49
+ if (raw.type === "command") {
50
+ const unknown = hasOnlyKeys(raw, ["id", "type", "run", "cwd", "timeout_ms"]);
51
+ if (unknown)
52
+ return failure(stageId, "completion", `checks[${index}]: unknown key "${unknown}"`);
53
+ if (typeof raw.run !== "string" || raw.run.trim() === "") {
54
+ return failure(stageId, "completion", `checks[${index}] command check requires a non-empty run`);
55
+ }
56
+ if (raw.cwd !== undefined && (typeof raw.cwd !== "string" || raw.cwd.trim() === "")) {
57
+ return failure(stageId, "completion", `checks[${index}].cwd must be a non-empty string`);
58
+ }
59
+ if (raw.timeout_ms !== undefined && !isPositiveInteger(raw.timeout_ms)) {
60
+ return failure(stageId, "completion", `checks[${index}].timeout_ms must be a positive integer`);
61
+ }
62
+ return loadSuccess({
63
+ id,
64
+ type: "command",
65
+ run: raw.run.trim(),
66
+ ...(typeof raw.cwd === "string" ? { cwd: raw.cwd.trim() } : {}),
67
+ ...(typeof raw.timeout_ms === "number" ? { timeout_ms: raw.timeout_ms } : {}),
68
+ });
69
+ }
70
+ if (raw.type === "artifact") {
71
+ const unknown = hasOnlyKeys(raw, ["id", "type", "path", "nonempty"]);
72
+ if (unknown)
73
+ return failure(stageId, "completion", `checks[${index}]: unknown key "${unknown}"`);
74
+ if (typeof raw.path !== "string" || raw.path.trim() === "") {
75
+ return failure(stageId, "completion", `checks[${index}] artifact check requires a non-empty path`);
76
+ }
77
+ const artifactPath = raw.path.trim();
78
+ const normalizedPath = path.normalize(artifactPath);
79
+ if (path.isAbsolute(artifactPath) ||
80
+ normalizedPath === ".." ||
81
+ normalizedPath.startsWith(`..${path.sep}`)) {
82
+ return failure(stageId, "completion", `checks[${index}] artifact path must stay inside the attempt artifact directory`);
83
+ }
84
+ if (raw.nonempty !== undefined && typeof raw.nonempty !== "boolean") {
85
+ return failure(stageId, "completion", `checks[${index}].nonempty must be a boolean`);
86
+ }
87
+ return loadSuccess({
88
+ id,
89
+ type: "artifact",
90
+ path: artifactPath,
91
+ ...(typeof raw.nonempty === "boolean" ? { nonempty: raw.nonempty } : {}),
92
+ });
93
+ }
94
+ if (raw.type === "checklist") {
95
+ const unknown = hasOnlyKeys(raw, ["id", "type", "items"]);
96
+ if (unknown)
97
+ return failure(stageId, "completion", `checks[${index}]: unknown key "${unknown}"`);
98
+ if (!Array.isArray(raw.items) ||
99
+ raw.items.length === 0 ||
100
+ !raw.items.every((item) => typeof item === "string" && item.trim() !== "")) {
101
+ return failure(stageId, "completion", `checks[${index}] checklist requires a non-empty items array of strings`);
102
+ }
103
+ const items = raw.items.map((item) => item.trim());
104
+ if (new Set(items).size !== items.length) {
105
+ return failure(stageId, "completion", `checks[${index}] checklist items must be unique`);
106
+ }
107
+ return loadSuccess({ id, type: "checklist", items });
108
+ }
109
+ if (raw.type === "payload_schema") {
110
+ const unknown = hasOnlyKeys(raw, ["id", "type"]);
111
+ if (unknown)
112
+ return failure(stageId, "completion", `checks[${index}]: unknown key "${unknown}"`);
113
+ return loadSuccess({ id, type: "payload_schema" });
114
+ }
115
+ if (raw.type === "gate") {
116
+ const unknown = hasOnlyKeys(raw, ["id", "type", "kind"]);
117
+ if (unknown)
118
+ return failure(stageId, "completion", `checks[${index}]: unknown key "${unknown}"`);
119
+ if (!isGateKind(raw.kind)) {
120
+ return failure(stageId, "completion", `checks[${index}].kind must be one of ${STAGE_GATE_KINDS.join(", ")}`);
121
+ }
122
+ return loadSuccess({ id, type: "gate", kind: raw.kind });
123
+ }
124
+ if (raw.type === "checkout_changes") {
125
+ const unknown = hasOnlyKeys(raw, ["id", "type", "path_fields"]);
126
+ if (unknown)
127
+ return failure(stageId, "completion", `checks[${index}]: unknown key "${unknown}"`);
128
+ const pathFields = parsePathFields(raw.path_fields);
129
+ if (raw.path_fields !== undefined && pathFields === undefined) {
130
+ return failure(stageId, "completion", `checks[${index}].path_fields must be a unique, non-empty array of strings`);
131
+ }
132
+ return loadSuccess({
133
+ id,
134
+ type: "checkout_changes",
135
+ ...(pathFields !== undefined ? { path_fields: pathFields } : {}),
136
+ });
137
+ }
138
+ return failure(stageId, "completion", `checks[${index}].type "${raw.type}" is unsupported`);
139
+ }
140
+ function parseCompletion(raw, stageId) {
141
+ if (raw === undefined)
142
+ return loadSuccess(undefined);
143
+ if (!isPlainObject(raw))
144
+ return failure(stageId, "completion", "must be an object");
145
+ const unknown = hasOnlyKeys(raw, ["mode", "checks"]);
146
+ if (unknown)
147
+ return failure(stageId, "completion", `unknown key "${unknown}"`);
148
+ if (raw.mode !== undefined && raw.mode !== "all") {
149
+ return failure(stageId, "completion", 'mode must be "all"');
150
+ }
151
+ if (!Array.isArray(raw.checks) || raw.checks.length === 0) {
152
+ return failure(stageId, "completion", "checks must be a non-empty array");
153
+ }
154
+ const checks = [];
155
+ for (let index = 0; index < raw.checks.length; index++) {
156
+ const outcome = parseCheck(raw.checks[index], stageId, index);
157
+ if (!outcome.ok)
158
+ return outcome;
159
+ checks.push(outcome.value);
160
+ }
161
+ const duplicates = checks.find((check, index) => checks.findIndex((item) => item.id === check.id) !== index);
162
+ if (duplicates)
163
+ return failure(stageId, "completion", `check id "${duplicates.id}" is duplicated`);
164
+ return loadSuccess({ mode: "all", checks });
165
+ }
166
+ function parseRecovery(raw, stageId, hasCompletion) {
167
+ if (raw === undefined)
168
+ return loadSuccess(undefined);
169
+ if (!hasCompletion)
170
+ return failure(stageId, "recovery", "requires a completion contract");
171
+ if (!isPlainObject(raw))
172
+ return failure(stageId, "recovery", "must be an object");
173
+ const unknown = hasOnlyKeys(raw, ["mode", "max_attempts", "retry_safety", "include_failed_checks"]);
174
+ if (unknown)
175
+ return failure(stageId, "recovery", `unknown key "${unknown}"`);
176
+ if (raw.mode !== "repair" && raw.mode !== "manual") {
177
+ return failure(stageId, "recovery", 'mode must be "repair" or "manual"');
178
+ }
179
+ if (raw.retry_safety !== "idempotent" && raw.retry_safety !== "side_effecting") {
180
+ return failure(stageId, "recovery", 'retry_safety must be "idempotent" or "side_effecting"');
181
+ }
182
+ if (raw.include_failed_checks !== undefined && typeof raw.include_failed_checks !== "boolean") {
183
+ return failure(stageId, "recovery", "include_failed_checks must be a boolean");
184
+ }
185
+ if (raw.mode === "repair") {
186
+ if (!isPositiveInteger(raw.max_attempts)) {
187
+ return failure(stageId, "recovery", "repair requires max_attempts to be a positive integer");
188
+ }
189
+ if (raw.retry_safety !== "idempotent") {
190
+ return failure(stageId, "recovery", "automatic repair requires retry_safety: idempotent");
191
+ }
192
+ return loadSuccess({
193
+ mode: "repair",
194
+ max_attempts: raw.max_attempts,
195
+ retry_safety: "idempotent",
196
+ include_failed_checks: raw.include_failed_checks ?? true,
197
+ });
198
+ }
199
+ return loadSuccess({
200
+ mode: "manual",
201
+ retry_safety: raw.retry_safety,
202
+ ...(typeof raw.include_failed_checks === "boolean"
203
+ ? { include_failed_checks: raw.include_failed_checks }
204
+ : {}),
205
+ });
206
+ }
207
+ export function parseExecutionPolicy(raw, stageId) {
208
+ const completion = parseCompletion(raw.completion, stageId);
209
+ if (!completion.ok)
210
+ return completion;
211
+ const recovery = parseRecovery(raw.recovery, stageId, completion.value !== undefined);
212
+ if (!recovery.ok)
213
+ return recovery;
214
+ return loadSuccess({
215
+ ...(completion.value !== undefined ? { completion: completion.value } : {}),
216
+ ...(recovery.value !== undefined ? { recovery: recovery.value } : {}),
217
+ });
218
+ }
@@ -0,0 +1,3 @@
1
+ import type { PreEmitCheck } from "../types/preEmitCheck.js";
2
+ import { type LoadOutcome } from "./loadOutcome.js";
3
+ export declare function parsePreEmitChecks(raw: unknown, label: string): LoadOutcome<PreEmitCheck[] | undefined>;
@@ -0,0 +1,65 @@
1
+ import { STAGE_GATE_KINDS } from "../types/stage.js";
2
+ import { loadFailure, loadSuccess } from "./loadOutcome.js";
3
+ function isPlainObject(value) {
4
+ return typeof value === "object" && value !== null && !Array.isArray(value);
5
+ }
6
+ function isGateKind(value) {
7
+ return typeof value === "string" && STAGE_GATE_KINDS.includes(value);
8
+ }
9
+ function failure(label, message) {
10
+ return loadFailure([
11
+ {
12
+ code: "stage.invalid_pre_emit_checks",
13
+ message: `Invalid stage ${label}: pre_emit_checks ${message}`,
14
+ category: "stage",
15
+ },
16
+ ]);
17
+ }
18
+ function parsePreEmitCheck(raw, label, index) {
19
+ if (!isPlainObject(raw)) {
20
+ return failure(label, `[${index}] must be an object`);
21
+ }
22
+ if (typeof raw.id !== "string" || raw.id.trim() === "") {
23
+ return failure(label, `[${index}].id must be a non-empty string`);
24
+ }
25
+ if (typeof raw.type !== "string") {
26
+ return failure(label, `[${index}].type must be a string`);
27
+ }
28
+ const id = raw.id.trim();
29
+ if (raw.type === "gate") {
30
+ if (!isGateKind(raw.kind)) {
31
+ return failure(label, `[${index}].kind must be one of ${STAGE_GATE_KINDS.join(", ")}`);
32
+ }
33
+ return loadSuccess({ id, type: "gate", kind: raw.kind });
34
+ }
35
+ if (raw.type === "artifact_declared") {
36
+ if (typeof raw.basename !== "string" || raw.basename.trim() === "") {
37
+ return failure(label, `[${index}].basename must be a non-empty string`);
38
+ }
39
+ return loadSuccess({
40
+ id,
41
+ type: "artifact_declared",
42
+ basename: raw.basename.trim(),
43
+ });
44
+ }
45
+ return failure(label, `[${index}].type "${raw.type}" is unsupported`);
46
+ }
47
+ export function parsePreEmitChecks(raw, label) {
48
+ if (raw === undefined)
49
+ return loadSuccess(undefined);
50
+ if (!Array.isArray(raw) || raw.length === 0) {
51
+ return failure(label, "must be a non-empty array");
52
+ }
53
+ const checks = [];
54
+ for (let index = 0; index < raw.length; index++) {
55
+ const outcome = parsePreEmitCheck(raw[index], label, index);
56
+ if (!outcome.ok)
57
+ return outcome;
58
+ checks.push(outcome.value);
59
+ }
60
+ const duplicate = checks.find((check, index) => checks.findIndex((item) => item.id === check.id) !== index);
61
+ if (duplicate) {
62
+ return failure(label, `id "${duplicate.id}" is duplicated`);
63
+ }
64
+ return loadSuccess(checks);
65
+ }
@@ -0,0 +1,19 @@
1
+ import type { NeedTerminalState, PipelineNeedEdge, PipelineNeeds, PipelineStageRef, ResolvedPipelineStageNode } from "../types/pipeline.js";
2
+ export declare const NEED_TERMINAL_STATES: readonly NeedTerminalState[];
3
+ export declare function isNeedTerminalState(value: unknown): value is NeedTerminalState;
4
+ export declare function parsePipelineNeeds(raw: unknown, stageId: string): {
5
+ ok: true;
6
+ value: PipelineNeeds;
7
+ } | {
8
+ ok: false;
9
+ message: string;
10
+ };
11
+ export declare function toNeedEdges(needs: PipelineStageRef["needs"]): PipelineNeedEdge[];
12
+ export declare function predecessorEdges(node: Pick<ResolvedPipelineStageNode, "needs" | "needsEdges">): PipelineNeedEdge[];
13
+ export declare function hydrateResolvedNeeds(node: {
14
+ needs?: unknown;
15
+ needsEdges?: unknown;
16
+ }): {
17
+ needs: string | null;
18
+ needsEdges: PipelineNeedEdge[];
19
+ };
@@ -0,0 +1,151 @@
1
+ export const NEED_TERMINAL_STATES = [
2
+ "succeeded",
3
+ "failed",
4
+ "skipped",
5
+ ];
6
+ const NEED_TERMINAL_STATE_SET = new Set(NEED_TERMINAL_STATES);
7
+ export function isNeedTerminalState(value) {
8
+ return typeof value === "string" && NEED_TERMINAL_STATE_SET.has(value);
9
+ }
10
+ function isPlainObject(value) {
11
+ return typeof value === "object" && value !== null && !Array.isArray(value);
12
+ }
13
+ function parseNeedOn(raw, stageId, parentId) {
14
+ if (!Array.isArray(raw) || raw.length === 0) {
15
+ return {
16
+ ok: false,
17
+ message: `stage "${stageId}": needs "${parentId}": on must be a non-empty unique subset of succeeded, failed, skipped`,
18
+ };
19
+ }
20
+ const seen = new Set();
21
+ const on = [];
22
+ for (const item of raw) {
23
+ if (typeof item !== "string" || !NEED_TERMINAL_STATE_SET.has(item) || seen.has(item)) {
24
+ return {
25
+ ok: false,
26
+ message: `stage "${stageId}": needs "${parentId}": on must be a non-empty unique subset of succeeded, failed, skipped`,
27
+ };
28
+ }
29
+ seen.add(item);
30
+ on.push(item);
31
+ }
32
+ return { ok: true, value: on };
33
+ }
34
+ export function parsePipelineNeeds(raw, stageId) {
35
+ if (typeof raw === "string") {
36
+ if (!raw) {
37
+ return { ok: false, message: `stage "${stageId}": needs must be a non-empty string` };
38
+ }
39
+ return { ok: true, value: raw };
40
+ }
41
+ if (!Array.isArray(raw)) {
42
+ return {
43
+ ok: false,
44
+ message: `stage "${stageId}": needs must be a non-empty string or an array of at least two items`,
45
+ };
46
+ }
47
+ if (raw.length < 2) {
48
+ return {
49
+ ok: false,
50
+ message: `stage "${stageId}": needs array must contain at least two items`,
51
+ };
52
+ }
53
+ const edges = [];
54
+ const seen = new Set();
55
+ for (const item of raw) {
56
+ if (typeof item === "string") {
57
+ if (!item) {
58
+ return {
59
+ ok: false,
60
+ message: `stage "${stageId}": needs item must be a stage id or { id, on }`,
61
+ };
62
+ }
63
+ if (seen.has(item)) {
64
+ return {
65
+ ok: false,
66
+ message: `stage "${stageId}": needs contains duplicate id "${item}"`,
67
+ };
68
+ }
69
+ seen.add(item);
70
+ edges.push({ id: item, on: ["succeeded"] });
71
+ continue;
72
+ }
73
+ if (!isPlainObject(item)) {
74
+ return {
75
+ ok: false,
76
+ message: `stage "${stageId}": needs item must be a stage id or { id, on }`,
77
+ };
78
+ }
79
+ for (const key of Object.keys(item)) {
80
+ if (key !== "id" && key !== "on") {
81
+ return {
82
+ ok: false,
83
+ message: `stage "${stageId}": needs item: unknown key "${key}"`,
84
+ };
85
+ }
86
+ }
87
+ if (typeof item.id !== "string" || !item.id) {
88
+ return {
89
+ ok: false,
90
+ message: `stage "${stageId}": needs item must be a stage id or { id, on }`,
91
+ };
92
+ }
93
+ if (seen.has(item.id)) {
94
+ return {
95
+ ok: false,
96
+ message: `stage "${stageId}": needs contains duplicate id "${item.id}"`,
97
+ };
98
+ }
99
+ const onResult = parseNeedOn(item.on, stageId, item.id);
100
+ if (!onResult.ok)
101
+ return onResult;
102
+ seen.add(item.id);
103
+ edges.push({ id: item.id, on: onResult.value });
104
+ }
105
+ return { ok: true, value: edges };
106
+ }
107
+ export function toNeedEdges(needs) {
108
+ if (needs === undefined)
109
+ return [];
110
+ if (typeof needs === "string") {
111
+ return needs ? [{ id: needs, on: ["succeeded"] }] : [];
112
+ }
113
+ return needs.map((item) => typeof item === "string"
114
+ ? { id: item, on: ["succeeded"] }
115
+ : { id: item.id, on: item.on && item.on.length > 0 ? item.on : ["succeeded"] });
116
+ }
117
+ export function predecessorEdges(node) {
118
+ if (node.needsEdges !== undefined)
119
+ return node.needsEdges;
120
+ if (typeof node.needs === "string" && node.needs) {
121
+ return [{ id: node.needs, on: ["succeeded"] }];
122
+ }
123
+ return [];
124
+ }
125
+ export function hydrateResolvedNeeds(node) {
126
+ if (Array.isArray(node.needsEdges)) {
127
+ const edges = [];
128
+ for (const item of node.needsEdges) {
129
+ if (!isPlainObject(item) || typeof item.id !== "string" || !item.id)
130
+ continue;
131
+ const on = Array.isArray(item.on)
132
+ ? item.on.filter((state) => typeof state === "string" && NEED_TERMINAL_STATE_SET.has(state))
133
+ : ["succeeded"];
134
+ edges.push({ id: item.id, on: on.length > 0 ? on : ["succeeded"] });
135
+ }
136
+ if (typeof node.needs === "string") {
137
+ return { needs: node.needs, needsEdges: edges };
138
+ }
139
+ if (node.needs === null) {
140
+ return { needs: null, needsEdges: edges };
141
+ }
142
+ return {
143
+ needs: edges.length === 1 ? edges[0].id : null,
144
+ needsEdges: edges,
145
+ };
146
+ }
147
+ if (typeof node.needs === "string" && node.needs) {
148
+ return { needs: node.needs, needsEdges: toNeedEdges(node.needs) };
149
+ }
150
+ return { needs: null, needsEdges: [] };
151
+ }
@@ -1,9 +1,24 @@
1
- export const WIRING_KEYS = new Set(["id", "needs", "fork", "uses", "clonable", "clone_cap"]);
1
+ export const WIRING_KEYS = new Set([
2
+ "id",
3
+ "needs",
4
+ "fork",
5
+ "uses",
6
+ "clonable",
7
+ "clone_cap",
8
+ "completion",
9
+ "recovery",
10
+ "feedback_loop",
11
+ "replay_safe",
12
+ ]);
2
13
  export const BODY_KEYS = new Set([
3
14
  "system_prompt",
4
15
  "model",
5
16
  "payload_schema",
6
17
  "gate_kinds",
18
+ "pre_emit_checks",
19
+ "clone_input_schema",
20
+ "clone_actions",
21
+ "timeout_ms",
7
22
  "skill",
8
23
  ]);
9
24
  export function isPipelineStageBodyKey(key) {
@@ -1,4 +1,8 @@
1
- import type { CloneEmitContext, ForkEmitContext } from "../types/forkChoice.js";
1
+ import type { CloneAction, CloneEmitContext, ForkEmitContext } from "../types/forkChoice.js";
2
2
  import type { ResolvedPipelineDag } from "../types/pipeline.js";
3
- export declare function resolveCloneEmitContext(dag: ResolvedPipelineDag, stageId: string): CloneEmitContext | undefined;
3
+ export type ResolveCloneEmitOptions = {
4
+ allowedActions?: CloneAction[];
5
+ successorCloneInputSchemas?: Record<string, unknown>;
6
+ };
7
+ export declare function resolveCloneEmitContext(dag: ResolvedPipelineDag, stageId: string, options?: ResolveCloneEmitOptions): CloneEmitContext | undefined;
4
8
  export declare function resolveForkEmitContext(dag: ResolvedPipelineDag, stageId: string): ForkEmitContext | undefined;
@@ -1,18 +1,25 @@
1
- export function resolveCloneEmitContext(dag, stageId) {
1
+ export function resolveCloneEmitContext(dag, stageId, options) {
2
2
  const childIds = dag.childrenOf[stageId] ?? [];
3
3
  const clonableSuccessors = [];
4
4
  for (const childId of childIds) {
5
5
  const node = dag.nodes.find((n) => n.id === childId);
6
6
  if (node?.clonable === true) {
7
+ const cloneInputSchema = options?.successorCloneInputSchemas?.[childId];
7
8
  clonableSuccessors.push({
8
9
  successorId: childId,
9
10
  cloneCap: node.clone_cap,
11
+ ...(cloneInputSchema !== undefined ? { cloneInputSchema } : {}),
10
12
  });
11
13
  }
12
14
  }
13
15
  if (clonableSuccessors.length === 0)
14
16
  return undefined;
15
- return { clonableSuccessors };
17
+ return {
18
+ clonableSuccessors,
19
+ ...(options?.allowedActions !== undefined
20
+ ? { allowedActions: options.allowedActions }
21
+ : {}),
22
+ };
16
23
  }
17
24
  export function resolveForkEmitContext(dag, stageId) {
18
25
  const dagNode = dag.nodes.find((n) => n.id === stageId);
@@ -1,8 +1,9 @@
1
- import type { PipelineStageRef, PipelineStageYamlEntry, ResolvedPipelineDag } from "../types/pipeline.js";
1
+ import type { PipelineStageRef, PipelineStageYamlEntry, ResolvedPipelineDag, FeedbackLoopConfig } from "../types/pipeline.js";
2
2
  export type ResolvePipelineDagContext = {
3
3
  pipelineId: string;
4
4
  path: string;
5
5
  };
6
+ export declare function parseFeedbackLoopConfig(value: unknown, stageId: string, ctx: ResolvePipelineDagContext): FeedbackLoopConfig;
6
7
  export declare function parsePipelineStageEntries(raw: unknown, ctx: ResolvePipelineDagContext): PipelineStageYamlEntry[];
7
8
  export declare function resolvePipelineDagFromRefs(refs: PipelineStageRef[], ctx: ResolvePipelineDagContext): {
8
9
  stages: string[];