@parcha/agentrun-dsl 0.1.0-beta.1
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/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +67 -0
- package/dist/authoring.d.ts +72 -0
- package/dist/authoring.js +138 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +135 -0
- package/dist/code-exec.d.ts +4 -0
- package/dist/code-exec.js +45 -0
- package/dist/demo.d.ts +54 -0
- package/dist/demo.js +80 -0
- package/dist/dry-run.d.ts +19 -0
- package/dist/dry-run.js +191 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +8 -0
- package/dist/inspection.d.ts +35 -0
- package/dist/inspection.js +107 -0
- package/dist/predicates.d.ts +34 -0
- package/dist/predicates.js +43 -0
- package/dist/schema-references.d.ts +3 -0
- package/dist/schema-references.js +66 -0
- package/dist/system-one.d.ts +78 -0
- package/dist/system-one.js +193 -0
- package/dist/workflow-shape.d.ts +1 -0
- package/dist/workflow-shape.js +181 -0
- package/dist/workflow.d.ts +541 -0
- package/dist/workflow.js +2294 -0
- package/dist/zod-json-contract.d.ts +1 -0
- package/dist/zod-json-contract.js +106 -0
- package/package.json +52 -0
- package/schema/workflow.schema.json +1850 -0
package/dist/workflow.js
ADDED
|
@@ -0,0 +1,2294 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { isDeepStrictEqual, types as utilTypes } from "node:util";
|
|
3
|
+
import { predicateMatches, MECHANICAL_PREDICATE_NAMES } from "./predicates.js";
|
|
4
|
+
import { validateAnswers, answerConfidence, answersSidecar, answersToValue, compileQuestions, SYSTEM_ONE_LIMITS } from "./system-one.js";
|
|
5
|
+
import { Compile } from "typebox/compile";
|
|
6
|
+
import { compileTransform, compileTransformSyntax } from "./code-exec.js";
|
|
7
|
+
import { workflowShapeErrors } from "./workflow-shape.js";
|
|
8
|
+
import { resolveSchemaForWorkflow } from "./schema-references.js";
|
|
9
|
+
export { resolveSchemaForWorkflow } from "./schema-references.js";
|
|
10
|
+
export function artifactNodeIsProse(node) {
|
|
11
|
+
return node.type === "markdown" || node.type === "report";
|
|
12
|
+
}
|
|
13
|
+
export function desugarWorkflow(workflow) {
|
|
14
|
+
const visit = (node) => {
|
|
15
|
+
if (!node || typeof node !== "object")
|
|
16
|
+
return node;
|
|
17
|
+
if (node.node === "artifact" && artifactNodeIsProse(node)) {
|
|
18
|
+
const { node: _kind, type: _type, path: _path, ...rest } = node;
|
|
19
|
+
return { node: "report", ...rest };
|
|
20
|
+
}
|
|
21
|
+
if (node.node === "chain")
|
|
22
|
+
return { ...node, steps: (node.steps || []).map(visit) };
|
|
23
|
+
if (node.node === "parallel")
|
|
24
|
+
return { ...node, branches: (node.branches || []).map(visit) };
|
|
25
|
+
if (node.node === "map" || node.node === "loop")
|
|
26
|
+
return { ...node, body: visit(node.body) };
|
|
27
|
+
if (node.node === "route")
|
|
28
|
+
return { ...node, branches: Object.fromEntries(Object.entries(node.branches || {}).map(([k, b]) => [k, { ...b, body: visit(b?.body) }])) };
|
|
29
|
+
if (node.node === "workflow")
|
|
30
|
+
return { ...node, workflow: desugarWorkflow(node.workflow) };
|
|
31
|
+
return node;
|
|
32
|
+
};
|
|
33
|
+
if (!workflow || typeof workflow !== "object" || !workflow.root)
|
|
34
|
+
return workflow;
|
|
35
|
+
return { ...workflow, root: visit(workflow.root) };
|
|
36
|
+
}
|
|
37
|
+
export function terminalArtifactType(workflow) {
|
|
38
|
+
const steps = workflow?.root?.node === "chain" ? workflow.root.steps || [] : [workflow?.root];
|
|
39
|
+
const last = steps[steps.length - 1];
|
|
40
|
+
if (!last || last.node !== "artifact")
|
|
41
|
+
return null;
|
|
42
|
+
return last.type;
|
|
43
|
+
}
|
|
44
|
+
export const SHELL_RESULT_SCHEMA = {
|
|
45
|
+
type: "object",
|
|
46
|
+
required: ["code", "stdout", "stderr"],
|
|
47
|
+
properties: { code: { type: "integer" }, stdout: { type: "string" }, stderr: { type: "string" }, truncated: { type: "boolean" } },
|
|
48
|
+
};
|
|
49
|
+
export const REPORT_SCHEMA = {
|
|
50
|
+
type: "object",
|
|
51
|
+
additionalProperties: false,
|
|
52
|
+
required: ["report_markdown"],
|
|
53
|
+
properties: { report_markdown: { type: "string", minLength: 40, pattern: "\\S" } },
|
|
54
|
+
};
|
|
55
|
+
export const declaredWrites = (node) => {
|
|
56
|
+
const out = new Set();
|
|
57
|
+
const visit = (n) => {
|
|
58
|
+
if (!n || typeof n !== "object")
|
|
59
|
+
return;
|
|
60
|
+
if (n.node === "report")
|
|
61
|
+
out.add("report_markdown");
|
|
62
|
+
else if (typeof n.as === "string" && n.as.trim())
|
|
63
|
+
out.add(n.as);
|
|
64
|
+
if (n.node === "map")
|
|
65
|
+
return;
|
|
66
|
+
if (Array.isArray(n.steps))
|
|
67
|
+
n.steps.forEach(visit);
|
|
68
|
+
if (Array.isArray(n.branches))
|
|
69
|
+
n.branches.forEach(visit);
|
|
70
|
+
else if (n.node === "route" && n.branches && typeof n.branches === "object")
|
|
71
|
+
Object.values(n.branches).forEach((b) => visit(b?.body));
|
|
72
|
+
if (n.body)
|
|
73
|
+
visit(n.body);
|
|
74
|
+
};
|
|
75
|
+
visit(node);
|
|
76
|
+
return out;
|
|
77
|
+
};
|
|
78
|
+
const routeBodies = (node) => node.node === "route" ? Object.values(node.branches || {}).map((b) => b?.body).filter(Boolean) : [];
|
|
79
|
+
const snapshotState = (state) => {
|
|
80
|
+
try {
|
|
81
|
+
return structuredClone(state);
|
|
82
|
+
}
|
|
83
|
+
catch (cause) {
|
|
84
|
+
throw new Error("Workflow state must be structured-cloneable", { cause });
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const KINDS = new Set(["chain", "code", "agent", "decide", "extract", "report", "map", "parallel", "loop", "escalate", "call", "artifact", "workflow", "judge", "pick", "sift", "route"]);
|
|
88
|
+
export const childSteps = (child) => {
|
|
89
|
+
const root = child?.root;
|
|
90
|
+
if (!root || typeof root !== "object")
|
|
91
|
+
return null;
|
|
92
|
+
return root.node === "chain" ? (Array.isArray(root.steps) ? root.steps : null) : [root];
|
|
93
|
+
};
|
|
94
|
+
export class EffectFailure extends Error {
|
|
95
|
+
retryClass;
|
|
96
|
+
constructor(message, retryClass = null) {
|
|
97
|
+
super(message);
|
|
98
|
+
this.retryClass = retryClass;
|
|
99
|
+
this.name = "EffectFailure";
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function containsReportNode(node) {
|
|
103
|
+
if (!node || typeof node !== "object")
|
|
104
|
+
return false;
|
|
105
|
+
if (node.node === "report" || node.node === "artifact")
|
|
106
|
+
return true;
|
|
107
|
+
if (node.node === "chain")
|
|
108
|
+
return (node.steps || []).some(containsReportNode);
|
|
109
|
+
if (node.node === "parallel")
|
|
110
|
+
return (node.branches || []).some(containsReportNode);
|
|
111
|
+
if (node.node === "map" || node.node === "loop")
|
|
112
|
+
return containsReportNode(node.body);
|
|
113
|
+
if (node.node === "route")
|
|
114
|
+
return routeBodies(node).some(containsReportNode);
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
const PREDICATES = new Set([...MECHANICAL_PREDICATE_NAMES, "ask"]);
|
|
118
|
+
function predicateShapeErrors(pred) {
|
|
119
|
+
const out = [];
|
|
120
|
+
if (pred.predicate === "in") {
|
|
121
|
+
if (!String(pred.path || "").trim())
|
|
122
|
+
out.push("in needs a path");
|
|
123
|
+
if (!Array.isArray(pred.values) || !pred.values.length || pred.values.some((v) => !["string", "number", "boolean"].includes(typeof v)))
|
|
124
|
+
out.push("in needs a non-empty values list of strings, numbers, or booleans");
|
|
125
|
+
}
|
|
126
|
+
if (pred.predicate === "gte" || pred.predicate === "lt") {
|
|
127
|
+
if (!String(pred.path || "").trim())
|
|
128
|
+
out.push(`${pred.predicate} needs a path`);
|
|
129
|
+
if (typeof pred.n !== "number" || !Number.isFinite(pred.n))
|
|
130
|
+
out.push(`${pred.predicate} needs a finite number n`);
|
|
131
|
+
}
|
|
132
|
+
if (pred.predicate === "ask") {
|
|
133
|
+
if (typeof pred.instructions !== "string" || !pred.instructions.trim())
|
|
134
|
+
out.push("ask needs instructions (the yes/no question)");
|
|
135
|
+
// A yes/no gate sits above the fence: at 0.5 or below it would hold on an answer that is not a
|
|
136
|
+
// yes, and a dry run's synthesized 0.5 could satisfy it.
|
|
137
|
+
if (pred.gte !== undefined && !(typeof pred.gte === "number" && pred.gte > 0.5 && pred.gte <= 1))
|
|
138
|
+
out.push("ask.gte must be in (0.5, 1]");
|
|
139
|
+
if (pred.state !== undefined && (!pred.state || typeof pred.state !== "object" || Array.isArray(pred.state)))
|
|
140
|
+
out.push("ask.state must be an object map");
|
|
141
|
+
}
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
function resultPathMissing(shape, dotted) {
|
|
145
|
+
let cursor = shape;
|
|
146
|
+
for (const segment of dotted.split(".")) {
|
|
147
|
+
if (!cursor || typeof cursor !== "object" || Array.isArray(cursor) || !(segment in cursor))
|
|
148
|
+
return true;
|
|
149
|
+
cursor = cursor[segment];
|
|
150
|
+
}
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
export function parseCsvRows(text) {
|
|
154
|
+
const rows = [];
|
|
155
|
+
let field = "", row = [], inQuotes = false;
|
|
156
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
157
|
+
const ch = text[i];
|
|
158
|
+
if (inQuotes) {
|
|
159
|
+
if (ch === '"' && text[i + 1] === '"') {
|
|
160
|
+
field += '"';
|
|
161
|
+
i += 1;
|
|
162
|
+
}
|
|
163
|
+
else if (ch === '"')
|
|
164
|
+
inQuotes = false;
|
|
165
|
+
else
|
|
166
|
+
field += ch;
|
|
167
|
+
}
|
|
168
|
+
else if (ch === '"')
|
|
169
|
+
inQuotes = true;
|
|
170
|
+
else if (ch === ",") {
|
|
171
|
+
row.push(field);
|
|
172
|
+
field = "";
|
|
173
|
+
}
|
|
174
|
+
else if (ch === "\n" || ch === "\r") {
|
|
175
|
+
if (ch === "\r" && text[i + 1] === "\n")
|
|
176
|
+
i += 1;
|
|
177
|
+
row.push(field);
|
|
178
|
+
field = "";
|
|
179
|
+
if (row.some((c) => c.trim() !== ""))
|
|
180
|
+
rows.push(row);
|
|
181
|
+
row = [];
|
|
182
|
+
}
|
|
183
|
+
else
|
|
184
|
+
field += ch;
|
|
185
|
+
}
|
|
186
|
+
if (field !== "" || row.length) {
|
|
187
|
+
row.push(field);
|
|
188
|
+
if (row.some((c) => c.trim() !== ""))
|
|
189
|
+
rows.push(row);
|
|
190
|
+
}
|
|
191
|
+
return rows;
|
|
192
|
+
}
|
|
193
|
+
export function buildReferenceContext(files) {
|
|
194
|
+
// Null prototypes: reference filenames are external data. On a plain object a name like
|
|
195
|
+
// "__proto__" hits the prototype SETTER on assignment — the file silently vanishes from
|
|
196
|
+
// the map (and worse shapes pollute it). A null-prototype object stores it as an ordinary
|
|
197
|
+
// own key, so every filename round-trips.
|
|
198
|
+
const references = Object.create(null);
|
|
199
|
+
const referencesParsed = Object.create(null);
|
|
200
|
+
for (const [name, body] of Object.entries(files)) {
|
|
201
|
+
references[name] = body;
|
|
202
|
+
if (name.toLowerCase().endsWith(".csv")) {
|
|
203
|
+
const rows = parseCsvRows(body);
|
|
204
|
+
if (rows.length)
|
|
205
|
+
referencesParsed[name] = { header: rows[0], rows: rows.slice(1) };
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return { references, references_parsed: referencesParsed };
|
|
209
|
+
}
|
|
210
|
+
export function normalizeStringNullsForSchema(value, schema, depth = 0) {
|
|
211
|
+
if (depth > 6 || !value || typeof value !== "object" || Array.isArray(value))
|
|
212
|
+
return value;
|
|
213
|
+
const properties = schema?.properties || {};
|
|
214
|
+
for (const [key, spec] of Object.entries(properties)) {
|
|
215
|
+
if (!spec || typeof spec !== "object")
|
|
216
|
+
continue;
|
|
217
|
+
const types = Array.isArray(spec.type) ? spec.type : [spec.type];
|
|
218
|
+
const anyOf = Array.isArray(spec.anyOf) ? spec.anyOf : [];
|
|
219
|
+
const nullable = types.includes("null") || anyOf.some((variant) => variant?.type === "null");
|
|
220
|
+
const current = value[key];
|
|
221
|
+
if (nullable && typeof current === "string" && ["null", "none", "n/a"].includes(current.trim().toLowerCase())) {
|
|
222
|
+
value[key] = null;
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
const objectSpecs = [spec, ...anyOf].filter((candidate) => candidate?.type === "object" || (candidate?.properties && typeof candidate.properties === "object"));
|
|
226
|
+
for (const objectSpec of objectSpecs)
|
|
227
|
+
normalizeStringNullsForSchema(current, objectSpec, depth + 1);
|
|
228
|
+
const items = spec.type === "array" || types.includes("array") ? spec.items : anyOf.find((variant) => variant?.type === "array")?.items;
|
|
229
|
+
if (items && Array.isArray(current))
|
|
230
|
+
for (const element of current)
|
|
231
|
+
normalizeStringNullsForSchema(element, items, depth + 1);
|
|
232
|
+
}
|
|
233
|
+
return value;
|
|
234
|
+
}
|
|
235
|
+
export function workflowSha256(workflow) {
|
|
236
|
+
const stable = (v) => Array.isArray(v) ? v.map(stable) : v && typeof v === "object" ? Object.fromEntries(Object.keys(v).sort().map((k) => [k, stable(v[k])])) : v;
|
|
237
|
+
return createHash("sha256").update(JSON.stringify(stable(workflow))).digest("hex");
|
|
238
|
+
}
|
|
239
|
+
/** Bound the complete document before any recursive schema or semantic traversal. Shared
|
|
240
|
+
* objects are valid, but each occurrence consumes the budget; cycles are never JSON. */
|
|
241
|
+
export function workflowDocumentError(value) {
|
|
242
|
+
const ancestors = new WeakSet();
|
|
243
|
+
const stack = [{ value, depth: 0 }];
|
|
244
|
+
let entries = 1;
|
|
245
|
+
while (stack.length) {
|
|
246
|
+
const current = stack.pop();
|
|
247
|
+
if (current.leave) {
|
|
248
|
+
ancestors.delete(current.value);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (current.depth > 128)
|
|
252
|
+
return "workflow document exceeds nesting depth 128";
|
|
253
|
+
if (typeof current.value === "function")
|
|
254
|
+
return "workflow document must contain data, not functions";
|
|
255
|
+
if (current.value === null || typeof current.value !== "object")
|
|
256
|
+
continue;
|
|
257
|
+
if (utilTypes.isProxy(current.value))
|
|
258
|
+
return "workflow document must not contain proxies";
|
|
259
|
+
const prototype = Object.getPrototypeOf(current.value);
|
|
260
|
+
if (prototype !== Object.prototype && prototype !== Array.prototype && prototype !== null)
|
|
261
|
+
return "workflow document must contain plain objects and arrays";
|
|
262
|
+
if (ancestors.has(current.value))
|
|
263
|
+
return "workflow document contains a cycle";
|
|
264
|
+
ancestors.add(current.value);
|
|
265
|
+
stack.push({ ...current, leave: true });
|
|
266
|
+
const descriptors = Object.values(Object.getOwnPropertyDescriptors(current.value));
|
|
267
|
+
if (descriptors.some(descriptor => descriptor.get || descriptor.set))
|
|
268
|
+
return "workflow document must not contain accessors";
|
|
269
|
+
const values = descriptors.map(descriptor => descriptor.value);
|
|
270
|
+
entries += values.length;
|
|
271
|
+
if (entries > 100_000)
|
|
272
|
+
return "workflow document exceeds 100000 expanded values";
|
|
273
|
+
for (let i = values.length - 1; i >= 0; i--)
|
|
274
|
+
stack.push({ value: values[i], depth: current.depth + 1 });
|
|
275
|
+
}
|
|
276
|
+
return undefined;
|
|
277
|
+
}
|
|
278
|
+
/** Set executeCode:false for untrusted author feedback: check syntax and mechanical contracts
|
|
279
|
+
* without evaluating authored expressions or probing transforms. This is not execution admission;
|
|
280
|
+
* trusted/default validation and runtime output checks remain authoritative. */
|
|
281
|
+
export function validateWorkflow(workflow, opts) {
|
|
282
|
+
const bounded = workflowDocumentError(workflow);
|
|
283
|
+
if (bounded)
|
|
284
|
+
return { ok: false, errors: [bounded] };
|
|
285
|
+
const shapeErrors = workflowShapeErrors(workflow);
|
|
286
|
+
if (shapeErrors.length)
|
|
287
|
+
return { ok: false, errors: shapeErrors };
|
|
288
|
+
workflow = desugarWorkflow(workflow);
|
|
289
|
+
const errors = [];
|
|
290
|
+
const declaredInput = workflow.input && Object.hasOwn(workflow.schemas, workflow.input.schemaId)
|
|
291
|
+
? resolveSchemaForWorkflow(workflow, workflow.schemas[workflow.input.schemaId]) : undefined;
|
|
292
|
+
const inputKeys = opts?.input !== undefined ? Object.keys(opts.input ?? {}) : opts?.inputKeys;
|
|
293
|
+
const reachability = inputKeys ? { available: new Set(inputKeys), unknowable: false } : null;
|
|
294
|
+
// Closed, typed producers let us reject impossible nested paths without running
|
|
295
|
+
// authored code. Open schemas, unions, references and dynamic control flow stand
|
|
296
|
+
// down; this is a conservative edge check, not a second schema validator.
|
|
297
|
+
const typedOutputs = new Map();
|
|
298
|
+
const rememberOutput = (key, schema) => {
|
|
299
|
+
typedOutputs.delete(key);
|
|
300
|
+
if (schema && typeof schema === "object" && !Array.isArray(schema))
|
|
301
|
+
typedOutputs.set(key, schema);
|
|
302
|
+
};
|
|
303
|
+
for (const [key, schema] of Object.entries((declaredInput?.properties ?? {})))
|
|
304
|
+
rememberOutput(key, schema);
|
|
305
|
+
const checkTypedPath = (dotted, at) => {
|
|
306
|
+
if (!reachability || reachability.unknowable)
|
|
307
|
+
return;
|
|
308
|
+
const [head, ...tail] = dotted.split(".");
|
|
309
|
+
let schema = typedOutputs.get(head);
|
|
310
|
+
for (const segment of tail) {
|
|
311
|
+
if (!schema || typeof schema !== "object" || schema.$ref || schema.anyOf || schema.oneOf || schema.allOf || schema.if)
|
|
312
|
+
return;
|
|
313
|
+
if (schema.type === "array") {
|
|
314
|
+
// Tuple prefixes have different item contracts; leave them to runtime validation.
|
|
315
|
+
if (!/^\d+$/.test(segment) || schema.prefixItems || Array.isArray(schema.items))
|
|
316
|
+
return;
|
|
317
|
+
schema = schema.items;
|
|
318
|
+
}
|
|
319
|
+
else if (schema.type === "object") {
|
|
320
|
+
if (Object.hasOwn(schema.properties ?? {}, segment))
|
|
321
|
+
schema = schema.properties[segment];
|
|
322
|
+
else {
|
|
323
|
+
if (schema.additionalProperties === false && !schema.patternProperties)
|
|
324
|
+
errors.push(`${at}: state path "${dotted}" is excluded by the upstream schema at "${head}"; check the producer's as key and declared properties`);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
else
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
const probedSubtrees = new Map();
|
|
333
|
+
const producedFiles = new Set();
|
|
334
|
+
let mayEscalate = false;
|
|
335
|
+
// Keys written by a code node whose source uses spread syntax: a spread of probed state
|
|
336
|
+
// enumerates to {} under the tolerant proxy, so required-field checks over such shapes would
|
|
337
|
+
// false-positive — they stand down instead.
|
|
338
|
+
const spreadTaintedKeys = new Set();
|
|
339
|
+
const tolerantProbe = () => new Proxy(function () { }, {
|
|
340
|
+
get: (_t, prop) => {
|
|
341
|
+
if (prop === Symbol.toPrimitive || prop === "toString" || prop === "valueOf")
|
|
342
|
+
return () => "";
|
|
343
|
+
if (prop === Symbol.iterator)
|
|
344
|
+
return function* () { };
|
|
345
|
+
if (prop === "length")
|
|
346
|
+
return 0;
|
|
347
|
+
return tolerantProbe();
|
|
348
|
+
},
|
|
349
|
+
apply: () => tolerantProbe(),
|
|
350
|
+
has: () => true,
|
|
351
|
+
});
|
|
352
|
+
const realDataOverlay = (real) => new Proxy({}, {
|
|
353
|
+
get: (_t, prop) => {
|
|
354
|
+
if (typeof prop === "string" && real && Object.prototype.hasOwnProperty.call(real, prop))
|
|
355
|
+
return real[prop];
|
|
356
|
+
if (prop === Symbol.toPrimitive || prop === "toString" || prop === "valueOf")
|
|
357
|
+
return () => "";
|
|
358
|
+
if (prop === Symbol.iterator)
|
|
359
|
+
return function* () { };
|
|
360
|
+
return tolerantProbe();
|
|
361
|
+
},
|
|
362
|
+
has: () => true,
|
|
363
|
+
});
|
|
364
|
+
const referenceProbe = opts?.probeContext ? (() => {
|
|
365
|
+
const context = new Proxy({}, {
|
|
366
|
+
get: (_t, prop) => {
|
|
367
|
+
if (prop === "references")
|
|
368
|
+
return realDataOverlay(opts.probeContext?.references);
|
|
369
|
+
if (prop === "references_parsed")
|
|
370
|
+
return realDataOverlay(opts.probeContext?.references_parsed);
|
|
371
|
+
if (prop === Symbol.toPrimitive || prop === "toString" || prop === "valueOf")
|
|
372
|
+
return () => "";
|
|
373
|
+
return tolerantProbe();
|
|
374
|
+
},
|
|
375
|
+
has: () => true,
|
|
376
|
+
});
|
|
377
|
+
return new Proxy(function () { }, {
|
|
378
|
+
get: (_t, prop) => {
|
|
379
|
+
if (prop === "context")
|
|
380
|
+
return context;
|
|
381
|
+
if (prop === Symbol.toPrimitive || prop === "toString" || prop === "valueOf")
|
|
382
|
+
return () => "";
|
|
383
|
+
if (prop === Symbol.iterator)
|
|
384
|
+
return function* () { };
|
|
385
|
+
if (prop === "length")
|
|
386
|
+
return 0;
|
|
387
|
+
return tolerantProbe();
|
|
388
|
+
},
|
|
389
|
+
apply: () => tolerantProbe(),
|
|
390
|
+
has: () => true,
|
|
391
|
+
});
|
|
392
|
+
})() : null;
|
|
393
|
+
const probeCodePatches = (code, at) => {
|
|
394
|
+
if (opts?.executeCode === false)
|
|
395
|
+
return [];
|
|
396
|
+
let compiled;
|
|
397
|
+
try {
|
|
398
|
+
compiled = compileTransform(code);
|
|
399
|
+
}
|
|
400
|
+
catch {
|
|
401
|
+
return [];
|
|
402
|
+
}
|
|
403
|
+
const patches = [];
|
|
404
|
+
let tolerantThrew = false;
|
|
405
|
+
const fullTolerant = tolerantProbe();
|
|
406
|
+
for (const probe of [{}, fullTolerant]) {
|
|
407
|
+
try {
|
|
408
|
+
const patch = compiled(probe);
|
|
409
|
+
if (patch && typeof patch === "object" && !Array.isArray(patch) && !(patch instanceof Function))
|
|
410
|
+
patches.push(patch);
|
|
411
|
+
}
|
|
412
|
+
catch {
|
|
413
|
+
if (probe === fullTolerant)
|
|
414
|
+
tolerantThrew = true; /* state-dependent — try the next probe */
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
if (referenceProbe && at) {
|
|
418
|
+
try {
|
|
419
|
+
const patch = compiled(referenceProbe);
|
|
420
|
+
if (patch && typeof patch === "object" && !Array.isArray(patch) && !(patch instanceof Function))
|
|
421
|
+
patches.push(patch);
|
|
422
|
+
}
|
|
423
|
+
catch (thrown) {
|
|
424
|
+
// A throw HERE but not under the fully tolerant probe is a defect, not state-dependence:
|
|
425
|
+
// the only difference between the two probes is the real reference data, so the node's
|
|
426
|
+
// own fail-closed guard (or a structural mismatch with the actual files) fired. Code that
|
|
427
|
+
// also threw under the tolerant probe is state-dependent and stands down as before.
|
|
428
|
+
if (!tolerantThrew)
|
|
429
|
+
errors.push(`${at.path} (${at.label}): code node throws against the real reference data: ${thrown instanceof Error ? thrown.message : String(thrown)}`);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return patches;
|
|
433
|
+
};
|
|
434
|
+
const checkPredicatePath = (when, label, path) => {
|
|
435
|
+
if (!reachability || !when?.path)
|
|
436
|
+
return;
|
|
437
|
+
const head = String(when.path).split(".")[0];
|
|
438
|
+
const subtrees = probedSubtrees.get(head);
|
|
439
|
+
if (!subtrees?.length)
|
|
440
|
+
return; // head not from a probed code node — stand down
|
|
441
|
+
// Resolve the full dotted path inside the probed subtree; a missing segment = a predicate
|
|
442
|
+
// that can never fire against the shape the gate returns.
|
|
443
|
+
const segments = String(when.path).split(".").slice(1);
|
|
444
|
+
let missingAt;
|
|
445
|
+
for (const subtree of subtrees) {
|
|
446
|
+
let cursor = subtree;
|
|
447
|
+
let missing = false;
|
|
448
|
+
for (const segment of segments) {
|
|
449
|
+
if (typeof cursor === "function")
|
|
450
|
+
return;
|
|
451
|
+
// A computed key evaluated against the tolerant probe collapses to "" — the shape at this
|
|
452
|
+
// level is state-dependent, so the check stands down rather than false-positive.
|
|
453
|
+
if (cursor && typeof cursor === "object" && (Object.prototype.hasOwnProperty.call(cursor, "") || Object.prototype.hasOwnProperty.call(cursor, "undefined")))
|
|
454
|
+
return;
|
|
455
|
+
if (!cursor || typeof cursor !== "object" || Array.isArray(cursor) || !(segment in cursor)) {
|
|
456
|
+
missingAt = cursor;
|
|
457
|
+
missing = true;
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
cursor = cursor[segment];
|
|
461
|
+
}
|
|
462
|
+
if (!missing)
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
errors.push(`${path} (${label}): predicate path "${when.path}" is not produced by the code node that writes "${head}" (its shape has: ${missingAt && typeof missingAt === "object" ? Object.keys(missingAt).join(", ") : typeof missingAt}); the guard can never fire`);
|
|
466
|
+
};
|
|
467
|
+
const checkInterpolations = (value, label, path) => {
|
|
468
|
+
if (!reachability || reachability.unknowable || value === undefined)
|
|
469
|
+
return;
|
|
470
|
+
for (const m of JSON.stringify(value).matchAll(/\{([a-zA-Z0-9_.$]+)\}/g)) {
|
|
471
|
+
// Runtime tries a literal state.* property before the optional prefix alias.
|
|
472
|
+
// When both namespaces may exist, do not reject either possible resolution.
|
|
473
|
+
if (m[1].startsWith("state.") && reachability.available.has("state"))
|
|
474
|
+
continue;
|
|
475
|
+
const head = (m[1].startsWith("state.") ? m[1].slice(6) : m[1]).split(".")[0];
|
|
476
|
+
if (!reachability.available.has(head))
|
|
477
|
+
errors.push(`${path} (${label}): interpolates {${m[1]}} but no upstream node produces "${head}"`);
|
|
478
|
+
else
|
|
479
|
+
checkTypedPath(m[1].startsWith("state.") ? m[1].slice(6) : m[1], `${path} (${label})`);
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
const checkRequires = (node, path) => {
|
|
483
|
+
if (node.requires === undefined)
|
|
484
|
+
return;
|
|
485
|
+
if (!Array.isArray(node.requires) || node.requires.some((p) => typeof p !== "string" || !p.trim())) {
|
|
486
|
+
errors.push(`${path} (${node.label}): requires must be non-empty state paths`);
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
if (!reachability || reachability.unknowable)
|
|
490
|
+
return;
|
|
491
|
+
for (const required of node.requires) {
|
|
492
|
+
const head = String(required).split(".")[0];
|
|
493
|
+
if (!reachability.available.has(head)) {
|
|
494
|
+
errors.push(`${path} (${node.label}): requires "${required}" but no upstream node produces it; state here has only: ${[...reachability.available].join(", ")}`);
|
|
495
|
+
}
|
|
496
|
+
else
|
|
497
|
+
checkTypedPath(required, `${path} (${node.label}) requires`);
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
const checkItemsPath = (node, path) => {
|
|
501
|
+
if (!reachability || reachability.unknowable || !node.itemsPath)
|
|
502
|
+
return;
|
|
503
|
+
const head = node.itemsPath.split(".")[0];
|
|
504
|
+
if (!reachability.available.has(head))
|
|
505
|
+
errors.push(`${path} (${node.label}): itemsPath "${node.itemsPath}" has no upstream producer; read the value under the producing node's as key (or its label when as is omitted)`);
|
|
506
|
+
else
|
|
507
|
+
checkTypedPath(node.itemsPath, `${path} (${node.label}) itemsPath`);
|
|
508
|
+
};
|
|
509
|
+
if (workflow?.v !== 2)
|
|
510
|
+
errors.push("workflow.v must be 2");
|
|
511
|
+
if (!workflow?.name)
|
|
512
|
+
errors.push("workflow.name is required");
|
|
513
|
+
if (!workflow?.schemas || typeof workflow.schemas !== "object")
|
|
514
|
+
errors.push("workflow.schemas is required");
|
|
515
|
+
if (workflow?.input !== undefined && (!workflow.input || typeof workflow.input.schemaId !== "string" || !Object.hasOwn(workflow.schemas, workflow.input.schemaId)))
|
|
516
|
+
errors.push("workflow.input.schemaId must name a schema");
|
|
517
|
+
if (!workflow?.output?.schemaId || !Object.hasOwn(workflow.schemas, workflow.output.schemaId))
|
|
518
|
+
errors.push("workflow.output.schemaId must name a schema");
|
|
519
|
+
for (const [id, schema] of Object.entries(workflow?.schemas || {})) {
|
|
520
|
+
if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
|
|
521
|
+
errors.push(`schema "${id}" must be a JSON-schema object (got ${schema === null ? "null" : Array.isArray(schema) ? "array" : typeof schema})`);
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
// Fail closed on refs: typebox compiles an unresolvable $ref to a never-matching schema
|
|
525
|
+
// (no throw), which surfaces only at case time as every submission rejected. Resolve
|
|
526
|
+
// sibling refs first, then reject any ref that still points nowhere and any ref form
|
|
527
|
+
// other than #/definitions/<name> (the one form the resolver supports).
|
|
528
|
+
const refs = [];
|
|
529
|
+
const walkRefs = (value) => {
|
|
530
|
+
if (Array.isArray(value)) {
|
|
531
|
+
for (const item of value)
|
|
532
|
+
walkRefs(item);
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
if (!value || typeof value !== "object")
|
|
536
|
+
return;
|
|
537
|
+
for (const [key, child] of Object.entries(value)) {
|
|
538
|
+
if (key === "$ref" && typeof child === "string")
|
|
539
|
+
refs.push(child);
|
|
540
|
+
else
|
|
541
|
+
walkRefs(child);
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
walkRefs(schema);
|
|
545
|
+
const resolved = resolveSchemaForWorkflow(workflow, schema);
|
|
546
|
+
const definitions = resolved.definitions || {};
|
|
547
|
+
for (const ref of refs) {
|
|
548
|
+
const match = ref.match(/^#\/definitions\/(.+)$/);
|
|
549
|
+
if (!match)
|
|
550
|
+
errors.push(`schema "${id}": unsupported $ref form "${ref}" — only "#/definitions/<name>" resolves (against inline definitions or a sibling schema)`);
|
|
551
|
+
else if (!Object.hasOwn(definitions, match[1]))
|
|
552
|
+
errors.push(`schema "${id}": $ref "${ref}" resolves to no inline definition and no sibling schema`);
|
|
553
|
+
}
|
|
554
|
+
try {
|
|
555
|
+
Compile(resolved);
|
|
556
|
+
}
|
|
557
|
+
catch (e) {
|
|
558
|
+
errors.push(`schema "${id}" does not compile: ${e?.message || e}`);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
if (opts?.input !== undefined) {
|
|
562
|
+
if (!opts.input || typeof opts.input !== "object" || Array.isArray(opts.input))
|
|
563
|
+
errors.push("input must be a JSON object");
|
|
564
|
+
else if (declaredInput) {
|
|
565
|
+
try {
|
|
566
|
+
const validator = Compile(declaredInput);
|
|
567
|
+
if (!validator.Check(opts.input))
|
|
568
|
+
errors.push(...schemaProblems(validator, opts.input).map(problem => `input: ${problem}`));
|
|
569
|
+
}
|
|
570
|
+
catch { /* Schema diagnostics above retain the original compilation failure. */ }
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
const KIND_KEYS = {
|
|
574
|
+
chain: new Set(["node", "steps"]),
|
|
575
|
+
code: new Set(["node", "label", "code", "as"]),
|
|
576
|
+
// "budget" (agent) and "samples"/"voteField" (decide) are deprecated-and-ignored: tolerated
|
|
577
|
+
// for legacy v2 documents. The engine invokes the adapter once per node.
|
|
578
|
+
agent: new Set(["node", "label", "state", "instructions", "sopSection", "out", "as", "requires", "tools", "effort", "thinking", "budget", "verify", "tier"]),
|
|
579
|
+
decide: new Set(["node", "label", "state", "instructions", "sopSection", "out", "as", "requires", "tools", "effort", "thinking", "samples", "voteField", "verify", "tier"]),
|
|
580
|
+
extract: new Set(["node", "label", "state", "instructions", "sopSection", "out", "as", "requires", "tools", "effort", "thinking", "verify", "tier"]),
|
|
581
|
+
report: new Set(["node", "label", "state", "instructions", "sopSection", "requires", "tools", "effort", "thinking"]),
|
|
582
|
+
artifact: new Set(["node", "label", "state", "type", "path", "instructions", "sopSection", "requires", "tools", "effort", "thinking"]),
|
|
583
|
+
map: new Set(["node", "label", "itemsPath", "body", "as", "resultPath", "maxConcurrency"]),
|
|
584
|
+
parallel: new Set(["node", "label", "branches"]),
|
|
585
|
+
loop: new Set(["node", "label", "body", "until", "maxIters"]),
|
|
586
|
+
escalate: new Set(["node", "label", "when", "kind", "stage", "summary"]),
|
|
587
|
+
call: new Set(["node", "label", "via", "tool", "args", "code", "input", "command", "where", "out", "as", "produces", "deadline_s", "retry", "poll", "requires", "env"]),
|
|
588
|
+
workflow: new Set(["node", "label", "workflow", "input", "out", "as"]),
|
|
589
|
+
judge: new Set(["node", "label", "state", "out", "as", "requires"]),
|
|
590
|
+
pick: new Set(["node", "label", "itemsPath", "describe", "instructions", "state", "allowNone", "as", "requires"]),
|
|
591
|
+
sift: new Set(["node", "label", "itemsPath", "describe", "state", "out", "as", "keep", "requires"]),
|
|
592
|
+
route: new Set(["node", "label", "state", "instructions", "branches", "unsure", "as", "requires"]),
|
|
593
|
+
};
|
|
594
|
+
const checkStateMap = (map, label, path, field = "state") => {
|
|
595
|
+
if (map === undefined)
|
|
596
|
+
return;
|
|
597
|
+
if (!map || typeof map !== "object" || Array.isArray(map)) {
|
|
598
|
+
errors.push(`${path} (${label}): ${field} must be an object map of what the questions see`);
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
checkInterpolations(map, label, path);
|
|
602
|
+
};
|
|
603
|
+
const checkQuestionSchema = (schemaId, label, path) => {
|
|
604
|
+
if (typeof schemaId !== "string" || !Object.hasOwn(workflow.schemas, schemaId)) {
|
|
605
|
+
errors.push(`${path} (${label}): out schema "${String(schemaId)}" not in workflow.schemas`);
|
|
606
|
+
return null;
|
|
607
|
+
}
|
|
608
|
+
const compiled = compileQuestions(workflow.schemas[schemaId], workflow.schemas);
|
|
609
|
+
if (compiled.ok === false) {
|
|
610
|
+
errors.push(...compiled.errors.map((e) => `${path} (${label}): schema "${schemaId}": ${e}`));
|
|
611
|
+
return null;
|
|
612
|
+
}
|
|
613
|
+
return compiled.questions;
|
|
614
|
+
};
|
|
615
|
+
const checkPredicate = (pred, label, path, field, mechanicalOnly = false) => {
|
|
616
|
+
const name = pred?.predicate;
|
|
617
|
+
if (!pred || !PREDICATES.has(name) || (mechanicalOnly && !MECHANICAL_PREDICATE_NAMES.has(name))) {
|
|
618
|
+
errors.push(`${path} (${label}): ${field}: unknown predicate "${name}"${mechanicalOnly && name === "ask" ? " (a poll reads a value, never asks a question)" : ""}`);
|
|
619
|
+
return false;
|
|
620
|
+
}
|
|
621
|
+
errors.push(...predicateShapeErrors(pred).map((e) => `${path} (${label}): ${field}: ${e}`));
|
|
622
|
+
if (name === "ask")
|
|
623
|
+
checkStateMap(pred.state, label, path, `${field}.state`);
|
|
624
|
+
return true;
|
|
625
|
+
};
|
|
626
|
+
const walk = (node, path) => {
|
|
627
|
+
if (!node || typeof node !== "object" || !KINDS.has(node.node)) {
|
|
628
|
+
errors.push(`${path}: unknown node kind "${node?.node}"`);
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
{
|
|
632
|
+
const allowed = KIND_KEYS[node.node];
|
|
633
|
+
const extraneous = Object.keys(node).filter((key) => !allowed.has(key));
|
|
634
|
+
if (extraneous.length)
|
|
635
|
+
errors.push(`${path} (${node.label || node.node}): unknown key(s) for a ${node.node} node: ${extraneous.join(", ")}`);
|
|
636
|
+
}
|
|
637
|
+
switch (node.node) {
|
|
638
|
+
case "chain":
|
|
639
|
+
if (!Array.isArray(node.steps) || !node.steps.length)
|
|
640
|
+
errors.push(`${path}: chain needs steps`);
|
|
641
|
+
(node.steps || []).forEach((s, i) => walk(s, `${path}.steps[${i}]`));
|
|
642
|
+
return;
|
|
643
|
+
case "code":
|
|
644
|
+
try {
|
|
645
|
+
if (opts?.executeCode === false)
|
|
646
|
+
compileTransformSyntax(node.code);
|
|
647
|
+
else
|
|
648
|
+
compileTransform(node.code);
|
|
649
|
+
}
|
|
650
|
+
catch (e) {
|
|
651
|
+
errors.push(`${path} (${node.label}): ${e?.message || e}`);
|
|
652
|
+
}
|
|
653
|
+
if (reachability) {
|
|
654
|
+
const patches = probeCodePatches(node.code, { path, label: node.label });
|
|
655
|
+
const keys = new Set(patches.flatMap((patch) => Object.keys(patch)));
|
|
656
|
+
if (/\.\.\./.test(node.code))
|
|
657
|
+
for (const key of keys)
|
|
658
|
+
spreadTaintedKeys.add(key);
|
|
659
|
+
for (const key of keys) {
|
|
660
|
+
const prior = probedSubtrees.get(key) || [];
|
|
661
|
+
const preservesPrior = patches.some((patch) => !Object.prototype.hasOwnProperty.call(patch, key));
|
|
662
|
+
const values = [
|
|
663
|
+
...(preservesPrior ? prior : []),
|
|
664
|
+
...patches.flatMap((patch) => Object.prototype.hasOwnProperty.call(patch, key) ? [patch[key]] : []),
|
|
665
|
+
];
|
|
666
|
+
if (values.length) {
|
|
667
|
+
reachability.available.add(key);
|
|
668
|
+
probedSubtrees.set(key, values);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
reachability.unknowable = true;
|
|
672
|
+
}
|
|
673
|
+
return;
|
|
674
|
+
case "agent":
|
|
675
|
+
case "decide":
|
|
676
|
+
case "extract":
|
|
677
|
+
case "report":
|
|
678
|
+
if (node.state !== undefined)
|
|
679
|
+
checkStateMap(node.state, node.label, path);
|
|
680
|
+
if (!node.instructions?.trim())
|
|
681
|
+
errors.push(`${path} (${node.label}): instructions required`);
|
|
682
|
+
if (node.node !== "report" && !Object.hasOwn(workflow.schemas, node.out))
|
|
683
|
+
errors.push(`${path} (${node.label}): out schema "${node.out}" not in workflow.schemas`);
|
|
684
|
+
if (node.thinking !== undefined && !["low", "medium", "high"].includes(String(node.thinking)))
|
|
685
|
+
errors.push(`${path} (${node.label}): thinking must be low|medium|high — thinking is never off`);
|
|
686
|
+
if (node.tools !== undefined && (!Array.isArray(node.tools) || node.tools.some((t) => typeof t !== "string" || !t.trim())))
|
|
687
|
+
errors.push(`${path} (${node.label}): tools must be a list of non-empty tool names when present (an empty list disables tools)`);
|
|
688
|
+
checkRequires(node, path);
|
|
689
|
+
if (node.tier !== undefined && !["fast", "default", "strong"].includes(String(node.tier)))
|
|
690
|
+
errors.push(`${path} (${node.label}): tier must be fast|default|strong`);
|
|
691
|
+
if (node.node !== "report" && node.verify !== undefined) {
|
|
692
|
+
const v = node.verify;
|
|
693
|
+
if (!v || typeof v !== "object" || typeof v.out !== "string")
|
|
694
|
+
errors.push(`${path} (${node.label}): verify needs an out (a question schema id)`);
|
|
695
|
+
else {
|
|
696
|
+
const qs = checkQuestionSchema(v.out, node.label, path);
|
|
697
|
+
if (qs && !Object.values(qs).some((q) => q.type === "noul"))
|
|
698
|
+
errors.push(`${path} (${node.label}): verify.out "${v.out}" carries no yes/no question — a verifier decides by yes/no (a boolean named after a submission field verifies it; any other boolean is a requirement)`);
|
|
699
|
+
if (v.state !== undefined)
|
|
700
|
+
checkStateMap(v.state, node.label, path, "verify.state");
|
|
701
|
+
if (v.maxDrives !== undefined && (!Number.isInteger(v.maxDrives) || v.maxDrives < 1 || v.maxDrives > 4))
|
|
702
|
+
errors.push(`${path} (${node.label}): verify.maxDrives must be an integer 1..4`);
|
|
703
|
+
if (v.override !== undefined && (typeof v.override?.below !== "number" || v.override.below <= 0 || v.override.below >= 1))
|
|
704
|
+
errors.push(`${path} (${node.label}): verify.override.below must be in (0, 1)`);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
if (reachability && node.node === "report") {
|
|
708
|
+
reachability.available.add("report_markdown");
|
|
709
|
+
typedOutputs.delete("report_markdown");
|
|
710
|
+
}
|
|
711
|
+
else if (reachability && node.node !== "report") {
|
|
712
|
+
const key = node.as || node.label;
|
|
713
|
+
reachability.available.add(key);
|
|
714
|
+
rememberOutput(key, workflow.schemas[node.out]);
|
|
715
|
+
if (node.verify) {
|
|
716
|
+
reachability.available.add(`${key}$verify`);
|
|
717
|
+
typedOutputs.delete(`${key}$verify`);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
return;
|
|
721
|
+
case "map":
|
|
722
|
+
if (node.resultPath !== undefined && (typeof node.resultPath !== "string" || !node.resultPath.trim()))
|
|
723
|
+
errors.push(`${path} (${node.label}): resultPath must be a non-empty path in the completed item state`);
|
|
724
|
+
if (node.maxConcurrency !== undefined && (!Number.isSafeInteger(node.maxConcurrency) || node.maxConcurrency < 1))
|
|
725
|
+
errors.push(`${path} (${node.label}): maxConcurrency must be a positive safe integer`);
|
|
726
|
+
if (!node.itemsPath)
|
|
727
|
+
errors.push(`${path} (${node.label}): itemsPath required`);
|
|
728
|
+
checkItemsPath(node, path);
|
|
729
|
+
if (!node.as)
|
|
730
|
+
errors.push(`${path} (${node.label}): as required`);
|
|
731
|
+
if (containsReportNode(node.body))
|
|
732
|
+
errors.push(`${path} (${node.label}): a report node cannot live inside a map body — the report is the workflow's ONE terminal rendering (a mapped copy lands in the item results, never at state.report_markdown)`);
|
|
733
|
+
if (reachability)
|
|
734
|
+
reachability.unknowable = true;
|
|
735
|
+
walk(node.body, `${path}.body`);
|
|
736
|
+
return;
|
|
737
|
+
case "parallel":
|
|
738
|
+
if (!Array.isArray(node.branches) || node.branches.length < 2)
|
|
739
|
+
errors.push(`${path} (${node.label}): parallel needs at least two branches`);
|
|
740
|
+
if (containsReportNode(node))
|
|
741
|
+
errors.push(`${path} (${node.label}): a report node cannot live inside a parallel branch — the report is rendered once, after every branch has landed`);
|
|
742
|
+
if (reachability)
|
|
743
|
+
reachability.unknowable = true;
|
|
744
|
+
{
|
|
745
|
+
const writers = new Map();
|
|
746
|
+
(node.branches || []).forEach((branch, i) => {
|
|
747
|
+
for (const key of declaredWrites(branch)) {
|
|
748
|
+
const prior = writers.get(key);
|
|
749
|
+
if (prior !== undefined && prior !== i)
|
|
750
|
+
errors.push(`${path} (${node.label}): branches ${prior} and ${i} both write state.${key} — parallel branches must write disjoint keys`);
|
|
751
|
+
writers.set(key, i);
|
|
752
|
+
}
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
(node.branches || []).forEach((branch, i) => walk(branch, `${path}.branches[${i}]`));
|
|
756
|
+
return;
|
|
757
|
+
case "loop":
|
|
758
|
+
if (!Number.isInteger(node.maxIters) || node.maxIters < 1 || node.maxIters > 20)
|
|
759
|
+
errors.push(`${path} (${node.label}): maxIters must be 1..20`);
|
|
760
|
+
if (containsReportNode(node.body))
|
|
761
|
+
errors.push(`${path} (${node.label}): a report node cannot live inside a loop body — the report is rendered once, after the record is final`);
|
|
762
|
+
checkPredicate(node.until, node.label, path, "until");
|
|
763
|
+
if (reachability)
|
|
764
|
+
reachability.unknowable = true;
|
|
765
|
+
walk(node.body, `${path}.body`);
|
|
766
|
+
return;
|
|
767
|
+
case "escalate":
|
|
768
|
+
mayEscalate = true;
|
|
769
|
+
checkPredicate(node.when, node.label, path, "when");
|
|
770
|
+
if (!node.kind?.trim() || !node.stage?.trim() || !node.summary?.trim())
|
|
771
|
+
errors.push(`${path} (${node.label}): kind, stage, summary required`);
|
|
772
|
+
checkPredicatePath(node.when, node.label, path);
|
|
773
|
+
return;
|
|
774
|
+
case "judge": {
|
|
775
|
+
const label = node.label || "judge";
|
|
776
|
+
if (!node.state || typeof node.state !== "object" || Array.isArray(node.state) || !Object.keys(node.state).length)
|
|
777
|
+
errors.push(`${path} (${label}): state must be a non-empty object map of what the questions see`);
|
|
778
|
+
else
|
|
779
|
+
checkStateMap(node.state, label, path);
|
|
780
|
+
checkQuestionSchema(node.out, label, path);
|
|
781
|
+
if (!node.as?.trim())
|
|
782
|
+
errors.push(`${path} (${label}): as required`);
|
|
783
|
+
checkRequires({ label, requires: node.requires }, path);
|
|
784
|
+
if (reachability && node.as) {
|
|
785
|
+
reachability.available.add(node.as);
|
|
786
|
+
reachability.available.add(`${node.as}$answers`);
|
|
787
|
+
rememberOutput(node.as, workflow.schemas[node.out]);
|
|
788
|
+
typedOutputs.delete(`${node.as}$answers`);
|
|
789
|
+
if (typeof node.out === "string" && (Object.hasOwn(workflow.schemas, node.out) ? workflow.schemas[node.out] : undefined))
|
|
790
|
+
probedSubtrees.set(node.as, [shapeFromSchema(workflow.schemas[node.out])]);
|
|
791
|
+
}
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
case "pick": {
|
|
795
|
+
const label = node.label || "pick";
|
|
796
|
+
if (!node.itemsPath?.trim())
|
|
797
|
+
errors.push(`${path} (${label}): itemsPath required`);
|
|
798
|
+
checkItemsPath(node, path);
|
|
799
|
+
if (typeof node.describe !== "string" || !node.describe.trim())
|
|
800
|
+
errors.push(`${path} (${label}): describe (the option text per item, e.g. "{item.name}, born {item.dob}") required`);
|
|
801
|
+
if (typeof node.instructions !== "string" || !node.instructions.trim())
|
|
802
|
+
errors.push(`${path} (${label}): instructions (the one question) required`);
|
|
803
|
+
checkStateMap(node.state, label, path);
|
|
804
|
+
if (!node.as?.trim())
|
|
805
|
+
errors.push(`${path} (${label}): as required`);
|
|
806
|
+
if (node.allowNone !== undefined && typeof node.allowNone !== "boolean")
|
|
807
|
+
errors.push(`${path} (${label}): allowNone must be boolean`);
|
|
808
|
+
checkRequires({ label, requires: node.requires }, path);
|
|
809
|
+
if (reachability && node.as) {
|
|
810
|
+
reachability.available.add(node.as);
|
|
811
|
+
reachability.available.add(`${node.as}$answers`);
|
|
812
|
+
typedOutputs.delete(node.as);
|
|
813
|
+
typedOutputs.delete(`${node.as}$answers`);
|
|
814
|
+
probedSubtrees.set(node.as, [{ index: 0, item: "", none: false, option: "" }]);
|
|
815
|
+
}
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
818
|
+
case "sift": {
|
|
819
|
+
const label = node.label || "sift";
|
|
820
|
+
if (!node.itemsPath?.trim())
|
|
821
|
+
errors.push(`${path} (${label}): itemsPath required`);
|
|
822
|
+
checkItemsPath(node, path);
|
|
823
|
+
if (node.describe !== undefined && (typeof node.describe !== "string" || !node.describe.trim()))
|
|
824
|
+
errors.push(`${path} (${label}): describe must be a non-empty template when present`);
|
|
825
|
+
checkStateMap(node.state, label, path);
|
|
826
|
+
const compiled = checkQuestionSchema(node.out, label, path);
|
|
827
|
+
if (!node.as?.trim())
|
|
828
|
+
errors.push(`${path} (${label}): as required`);
|
|
829
|
+
if (node.keep !== undefined) {
|
|
830
|
+
const k = node.keep;
|
|
831
|
+
if (!k || typeof k !== "object" || typeof k.path !== "string" || !k.path.trim())
|
|
832
|
+
errors.push(`${path} (${label}): keep.path must name a question id (or <id>.confidence)`);
|
|
833
|
+
else if (compiled) {
|
|
834
|
+
const [id, tail] = k.path.split(".");
|
|
835
|
+
if (!Object.hasOwn(compiled, id) || (tail !== undefined && tail !== "confidence"))
|
|
836
|
+
errors.push(`${path} (${label}): keep.path "${k.path}" names no question of schema "${String(node.out)}"`);
|
|
837
|
+
else if (compiled[id].type === "choice" && tail !== "confidence")
|
|
838
|
+
errors.push(`${path} (${label}): keep.path "${k.path}" is a choice — keep on a boolean or score question, or on <id>.confidence`);
|
|
839
|
+
}
|
|
840
|
+
if (k?.gte !== undefined && !(typeof k.gte === "number" && Number.isFinite(k.gte)))
|
|
841
|
+
errors.push(`${path} (${label}): keep.gte must be a number`);
|
|
842
|
+
}
|
|
843
|
+
checkRequires({ label, requires: node.requires }, path);
|
|
844
|
+
if (reachability && node.as) {
|
|
845
|
+
reachability.available.add(node.as);
|
|
846
|
+
typedOutputs.delete(node.as);
|
|
847
|
+
probedSubtrees.set(node.as, [{ items: [], values: [], answers: [], kept: [] }]);
|
|
848
|
+
}
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
case "route": {
|
|
852
|
+
const label = node.label || "route";
|
|
853
|
+
if (!node.state || typeof node.state !== "object" || Array.isArray(node.state) || !Object.keys(node.state).length)
|
|
854
|
+
errors.push(`${path} (${label}): state must be a non-empty object map of what the question sees`);
|
|
855
|
+
else
|
|
856
|
+
checkStateMap(node.state, label, path);
|
|
857
|
+
if (typeof node.instructions !== "string" || !node.instructions.trim())
|
|
858
|
+
errors.push(`${path} (${label}): instructions (the one question) required`);
|
|
859
|
+
const names = node.branches && typeof node.branches === "object" && !Array.isArray(node.branches) ? Object.keys(node.branches) : [];
|
|
860
|
+
if (names.length < 2)
|
|
861
|
+
errors.push(`${path} (${label}): route needs at least two named branches`);
|
|
862
|
+
if (names.length > SYSTEM_ONE_LIMITS.maxChoiceOptions)
|
|
863
|
+
errors.push(`${path} (${label}): route takes at most ${SYSTEM_ONE_LIMITS.maxChoiceOptions} branches`);
|
|
864
|
+
if (node.unsure !== undefined) {
|
|
865
|
+
const u = node.unsure;
|
|
866
|
+
if (!u || typeof u.branch !== "string" || !names.includes(u.branch))
|
|
867
|
+
errors.push(`${path} (${label}): unsure.branch must name one of the branches`);
|
|
868
|
+
if (!(typeof u?.gte === "number" && u.gte > 0 && u.gte <= 1))
|
|
869
|
+
errors.push(`${path} (${label}): unsure.gte must be in (0, 1]`);
|
|
870
|
+
}
|
|
871
|
+
if (containsReportNode(node))
|
|
872
|
+
errors.push(`${path} (${label}): a report node cannot live inside a route branch — the report is rendered once, after the record is final`);
|
|
873
|
+
if (node.as !== undefined && (typeof node.as !== "string" || !node.as.trim()))
|
|
874
|
+
errors.push(`${path} (${label}): as must be a state key when present`);
|
|
875
|
+
checkRequires({ label, requires: node.requires }, path);
|
|
876
|
+
if (reachability) {
|
|
877
|
+
if (typeof node.as === "string" && node.as.trim()) {
|
|
878
|
+
reachability.available.add(node.as);
|
|
879
|
+
reachability.available.add(`${node.as}$answers`);
|
|
880
|
+
}
|
|
881
|
+
reachability.unknowable = true;
|
|
882
|
+
}
|
|
883
|
+
for (const name of names) {
|
|
884
|
+
const branch = node.branches[name];
|
|
885
|
+
if (!branch || typeof branch !== "object" || !branch.body) {
|
|
886
|
+
errors.push(`${path} (${label}): branch "${name}" needs a body`);
|
|
887
|
+
continue;
|
|
888
|
+
}
|
|
889
|
+
if (branch.criteria !== undefined && typeof branch.criteria !== "string")
|
|
890
|
+
errors.push(`${path} (${label}): branch "${name}" criteria must be a string`);
|
|
891
|
+
walk(branch.body, `${path}.branches.${name}`);
|
|
892
|
+
}
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
case "artifact": {
|
|
896
|
+
const label = node.label || "artifact";
|
|
897
|
+
const type = typeof node.type === "string" && node.type.trim() ? node.type : null;
|
|
898
|
+
if (!type)
|
|
899
|
+
errors.push(`${path} (${label}): artifact type must be a non-empty string`);
|
|
900
|
+
for (const key of ["instructions", "state", "sopSection", "tools", "effort", "thinking"])
|
|
901
|
+
if (node[key] !== undefined)
|
|
902
|
+
errors.push(`${path} (${label}): ${key} is a prose-artifact field; a ${type ?? "file"} artifact has no model in the loop`);
|
|
903
|
+
if (typeof node.path !== "string" || !node.path.trim() || node.path.startsWith("/") || node.path.split("/").includes(".."))
|
|
904
|
+
errors.push(`${path} (${label}): path must be a workspace-relative file`);
|
|
905
|
+
else if (reachability && !producedFiles.has(node.path))
|
|
906
|
+
errors.push(`${path} (${label}): path "${node.path}" is not produced by any earlier shell call (declare it in that call's produces)`);
|
|
907
|
+
checkRequires({ label, requires: node.requires }, path);
|
|
908
|
+
if (reachability) {
|
|
909
|
+
reachability.available.add("artifact");
|
|
910
|
+
typedOutputs.delete("artifact");
|
|
911
|
+
}
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
case "workflow": {
|
|
915
|
+
const label = typeof node.label === "string" && node.label.trim() ? node.label : "workflow";
|
|
916
|
+
if (typeof node.label !== "string" || !node.label.trim() || typeof node.as !== "string" || !node.as.trim())
|
|
917
|
+
errors.push(`${path} (${label}): a workflow invocation requires label and as`);
|
|
918
|
+
if (!Object.hasOwn(workflow.schemas, node.out))
|
|
919
|
+
errors.push(`${path} (${label}): out schema "${String(node.out)}" not in workflow.schemas`);
|
|
920
|
+
if (!node.input || typeof node.input !== "object" || Array.isArray(node.input))
|
|
921
|
+
errors.push(`${path} (${label}): input must be an object mapping onto the child's initial state`);
|
|
922
|
+
const child = node.workflow;
|
|
923
|
+
const steps = childSteps(child);
|
|
924
|
+
if (!child || typeof child !== "object" || !steps || !steps.length)
|
|
925
|
+
errors.push(`${path} (${label}): the child workflow must contain a root node`);
|
|
926
|
+
else {
|
|
927
|
+
if (containsReportNode(child.root))
|
|
928
|
+
errors.push(`${path} (${label}): a child cannot render a report or deliver an artifact; the parent owns the terminal`);
|
|
929
|
+
if (typeof child.input?.schemaId !== "string" || !Object.hasOwn(child.schemas, child.input.schemaId))
|
|
930
|
+
errors.push(`${path} (${label}): the child must declare input.schemaId in its own schemas`);
|
|
931
|
+
const validated = validateWorkflow(child, { executeCode: opts?.executeCode, inputKeys: Object.keys(node.input && typeof node.input === "object" ? node.input : {}) });
|
|
932
|
+
if (!validated.ok)
|
|
933
|
+
errors.push(...validated.errors.map(error => `${path} (${label}) child: ${error}`));
|
|
934
|
+
for (const step of steps)
|
|
935
|
+
if (step.node === "call" && Array.isArray(step.produces))
|
|
936
|
+
for (const file of step.produces)
|
|
937
|
+
producedFiles.add(String(file));
|
|
938
|
+
}
|
|
939
|
+
if (node.input && typeof node.input === "object")
|
|
940
|
+
checkInterpolations(node.input, label, path);
|
|
941
|
+
if (reachability && typeof node.as === "string") {
|
|
942
|
+
reachability.available.add(node.as);
|
|
943
|
+
rememberOutput(node.as, workflow.schemas[node.out]);
|
|
944
|
+
}
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
case "call": {
|
|
948
|
+
const label = node.label || "call";
|
|
949
|
+
if (!["tool", "executor", "shell"].includes(String(node.via)))
|
|
950
|
+
errors.push(`${path} (${label}): via must be tool|executor|shell`);
|
|
951
|
+
if (node.via === "tool") {
|
|
952
|
+
if (!node.tool?.trim())
|
|
953
|
+
errors.push(`${path} (${label}): via tool needs a tool address`);
|
|
954
|
+
if (node.args !== undefined && (!node.args || typeof node.args !== "object" || Array.isArray(node.args)))
|
|
955
|
+
errors.push(`${path} (${label}): args must be an object`);
|
|
956
|
+
for (const key of ["code", "input", "command", "env"])
|
|
957
|
+
if (node[key] !== undefined)
|
|
958
|
+
errors.push(`${path} (${label}): ${key} is not a via tool field`);
|
|
959
|
+
}
|
|
960
|
+
if (node.via === "executor") {
|
|
961
|
+
if (!node.code?.trim())
|
|
962
|
+
errors.push(`${path} (${label}): via executor needs a code body`);
|
|
963
|
+
if (!/\breturn\b/.test(String(node.code || "")))
|
|
964
|
+
errors.push(`${path} (${label}): the executor body must return its JSON result`);
|
|
965
|
+
if (/\b(require|process|fetch|eval|Function|import)\s*\(?/.test(String(node.code || "")))
|
|
966
|
+
errors.push(`${path} (${label}): the executor body may use only tools and input (no require/process/fetch/eval/import)`);
|
|
967
|
+
if (node.input !== undefined && (!node.input || typeof node.input !== "object" || Array.isArray(node.input)))
|
|
968
|
+
errors.push(`${path} (${label}): input must be an object`);
|
|
969
|
+
for (const key of ["tool", "args", "command", "env"])
|
|
970
|
+
if (node[key] !== undefined)
|
|
971
|
+
errors.push(`${path} (${label}): ${key} is not a via executor field`);
|
|
972
|
+
}
|
|
973
|
+
if (node.via === "shell") {
|
|
974
|
+
if (!node.command?.trim())
|
|
975
|
+
errors.push(`${path} (${label}): via shell needs a command`);
|
|
976
|
+
if (node.env !== undefined && (!node.env || typeof node.env !== "object" || Array.isArray(node.env) || Object.entries(node.env).some(([k, v]) => !/^[A-Z_][A-Z0-9_]*$/.test(k) || typeof v !== "string")))
|
|
977
|
+
errors.push(`${path} (${label}): env must map UPPER_CASE names to strings`);
|
|
978
|
+
if (node.out !== undefined)
|
|
979
|
+
errors.push(`${path} (${label}): a shell call's result is the fixed shell shape; drop out`);
|
|
980
|
+
for (const key of ["tool", "args", "code", "input"])
|
|
981
|
+
if (node[key] !== undefined)
|
|
982
|
+
errors.push(`${path} (${label}): ${key} is not a via shell field`);
|
|
983
|
+
}
|
|
984
|
+
else if (node.produces?.length) {
|
|
985
|
+
errors.push(`${path} (${label}): only a via shell call may declare produces`);
|
|
986
|
+
}
|
|
987
|
+
if (node.via !== "shell") {
|
|
988
|
+
if (!node.out)
|
|
989
|
+
errors.push(`${path} (${label}): out schema required for a ${node.via} call`);
|
|
990
|
+
else if (!Object.hasOwn(workflow.schemas, node.out))
|
|
991
|
+
errors.push(`${path} (${label}): out schema "${node.out}" not in workflow.schemas`);
|
|
992
|
+
}
|
|
993
|
+
if (!node.as?.trim())
|
|
994
|
+
errors.push(`${path} (${label}): as required`);
|
|
995
|
+
if (node.where !== undefined && node.where !== "sandbox")
|
|
996
|
+
errors.push(`${path} (${label}): where accepts only "sandbox"`);
|
|
997
|
+
if (!(Number.isFinite(node.deadline_s) && node.deadline_s > 0 && node.deadline_s <= 3600))
|
|
998
|
+
errors.push(`${path} (${label}): deadline_s must be greater than 0 and at most 3600 seconds`);
|
|
999
|
+
if (node.produces !== undefined && (!Array.isArray(node.produces) || !node.produces.length || node.produces.some((f) => typeof f !== "string" || !f.trim() || f.startsWith("/") || f.split("/").includes(".."))))
|
|
1000
|
+
errors.push(`${path} (${label}): produces must be non-empty workspace-relative paths`);
|
|
1001
|
+
else
|
|
1002
|
+
for (const f of node.produces || [])
|
|
1003
|
+
producedFiles.add(f);
|
|
1004
|
+
if (node.retry !== undefined) {
|
|
1005
|
+
const r = node.retry;
|
|
1006
|
+
if (!r || !Number.isInteger(r.attempts) || r.attempts < 1 || r.attempts > 5)
|
|
1007
|
+
errors.push(`${path} (${label}): retry.attempts must be 1..5`);
|
|
1008
|
+
if (r?.backoff_s !== undefined && !(Number.isFinite(r.backoff_s) && r.backoff_s >= 0 && r.backoff_s <= 60))
|
|
1009
|
+
errors.push(`${path} (${label}): retry.backoff_s must be 0..60`);
|
|
1010
|
+
if (r?.on !== undefined && (!Array.isArray(r.on) || r.on.some((c) => !["timeout", "http_5xx", "http_429", "connection", "exit"].includes(String(c)))))
|
|
1011
|
+
errors.push(`${path} (${label}): retry.on may name only timeout|http_5xx|http_429|connection|exit`);
|
|
1012
|
+
}
|
|
1013
|
+
if (node.poll !== undefined) {
|
|
1014
|
+
const p = node.poll;
|
|
1015
|
+
if (!p || typeof p !== "object" || Array.isArray(p))
|
|
1016
|
+
errors.push(`${path} (${label}): poll must be an object`);
|
|
1017
|
+
else {
|
|
1018
|
+
const resultShape = node.via === "shell" ? shapeFromSchema(SHELL_RESULT_SCHEMA) : node.out && (Object.hasOwn(workflow.schemas, node.out) ? workflow.schemas[node.out] : undefined) ? shapeFromSchema(workflow.schemas[node.out]) : undefined;
|
|
1019
|
+
for (const [name, pred] of [["until", p.until], ["fail_when", p.fail_when]]) {
|
|
1020
|
+
if (pred === undefined) {
|
|
1021
|
+
if (name === "until")
|
|
1022
|
+
errors.push(`${path} (${label}): poll.until is required`);
|
|
1023
|
+
continue;
|
|
1024
|
+
}
|
|
1025
|
+
if (!checkPredicate(pred, label, path, `poll.${name}`, true))
|
|
1026
|
+
continue;
|
|
1027
|
+
const predPath = pred.path;
|
|
1028
|
+
if (resultShape && typeof resultShape === "object" && predPath && resultPathMissing(resultShape, predPath))
|
|
1029
|
+
errors.push(`${path} (${label}): poll.${name} path "${predPath}" is not in the declared result shape (${Object.keys(resultShape).join(", ")}); the poll could never settle`);
|
|
1030
|
+
}
|
|
1031
|
+
if (!(Number.isFinite(p.interval_s) && p.interval_s >= 0.1 && p.interval_s <= 300))
|
|
1032
|
+
errors.push(`${path} (${label}): poll.interval_s must be 0.1..300 seconds`);
|
|
1033
|
+
if (!(Number.isFinite(p.deadline_s) && p.deadline_s >= (Number(node.deadline_s) || 0) && p.deadline_s <= 7200))
|
|
1034
|
+
errors.push(`${path} (${label}): poll.deadline_s must be at least deadline_s and at most 7200 seconds`);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
checkRequires({ label, requires: node.requires }, path);
|
|
1038
|
+
checkInterpolations({ args: node.args, input: node.input, env: node.env }, label, path);
|
|
1039
|
+
if (reachability) {
|
|
1040
|
+
reachability.available.add(node.as);
|
|
1041
|
+
rememberOutput(node.as, node.via === "shell" ? SHELL_RESULT_SCHEMA : workflow.schemas[node.out]);
|
|
1042
|
+
if (node.out && (Object.hasOwn(workflow.schemas, node.out) ? workflow.schemas[node.out] : undefined))
|
|
1043
|
+
probedSubtrees.set(node.as, [shapeFromSchema(workflow.schemas[node.out])]);
|
|
1044
|
+
else if (node.via === "shell")
|
|
1045
|
+
probedSubtrees.set(node.as, [shapeFromSchema(SHELL_RESULT_SCHEMA)]);
|
|
1046
|
+
}
|
|
1047
|
+
return;
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
};
|
|
1051
|
+
walk(workflow?.root, "root");
|
|
1052
|
+
{
|
|
1053
|
+
const countReports = (node) => {
|
|
1054
|
+
if (!node || typeof node !== "object")
|
|
1055
|
+
return 0;
|
|
1056
|
+
if (node.node === "report" || node.node === "artifact")
|
|
1057
|
+
return 1;
|
|
1058
|
+
if (node.node === "chain")
|
|
1059
|
+
return (node.steps || []).reduce((n, s) => n + countReports(s), 0);
|
|
1060
|
+
if (node.node === "parallel")
|
|
1061
|
+
return (node.branches || []).reduce((n, b) => n + countReports(b), 0);
|
|
1062
|
+
if (node.node === "map" || node.node === "loop")
|
|
1063
|
+
return countReports(node.body);
|
|
1064
|
+
if (node.node === "route")
|
|
1065
|
+
return routeBodies(node).reduce((n, b) => n + countReports(b), 0);
|
|
1066
|
+
return 0;
|
|
1067
|
+
};
|
|
1068
|
+
const total = countReports(workflow?.root);
|
|
1069
|
+
if (total > 1)
|
|
1070
|
+
errors.push(`root: at most ONE terminal node (report or artifact) — a second rendering overwrites the first at its fixed state key`);
|
|
1071
|
+
else if (total === 1) {
|
|
1072
|
+
const rootSteps = workflow?.root?.node === "chain" ? workflow.root.steps || [] : [workflow?.root];
|
|
1073
|
+
const lastStep = rootSteps[rootSteps.length - 1];
|
|
1074
|
+
if (!lastStep || (lastStep.node !== "report" && lastStep.node !== "artifact")) {
|
|
1075
|
+
errors.push(`root: the terminal node (report or artifact) must be the LAST step of the root chain — rendered earlier it ships a record that later nodes still change`);
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
{
|
|
1080
|
+
const llmNodes = [];
|
|
1081
|
+
const collect = (node) => {
|
|
1082
|
+
if (!node || typeof node !== "object")
|
|
1083
|
+
return;
|
|
1084
|
+
if (node.node === "agent" || node.node === "decide" || node.node === "extract" || node.node === "report")
|
|
1085
|
+
llmNodes.push(node);
|
|
1086
|
+
if (node.node === "chain")
|
|
1087
|
+
(node.steps || []).forEach(collect);
|
|
1088
|
+
if (node.node === "parallel")
|
|
1089
|
+
(node.branches || []).forEach(collect);
|
|
1090
|
+
if (node.node === "map" || node.node === "loop")
|
|
1091
|
+
collect(node.body);
|
|
1092
|
+
if (node.node === "route")
|
|
1093
|
+
routeBodies(node).forEach(collect);
|
|
1094
|
+
};
|
|
1095
|
+
collect(workflow?.root);
|
|
1096
|
+
if (llmNodes.some((n) => n.node === "report")) {
|
|
1097
|
+
for (const node of llmNodes) {
|
|
1098
|
+
if (node.node === "report" || node.node === "agent")
|
|
1099
|
+
continue;
|
|
1100
|
+
const properties = (Object.hasOwn(workflow.schemas, node.out) ? workflow.schemas[node.out] : undefined)?.properties;
|
|
1101
|
+
if (properties && Object.prototype.hasOwnProperty.call(properties, "report_markdown")) {
|
|
1102
|
+
errors.push(`root (${node.label}): a ${node.node} node must not emit report_markdown — the report node owns the report; drop the field from schema "${node.out}"`);
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
if (reachability && !reachability.unknowable && !mayEscalate && workflow?.output?.path) {
|
|
1108
|
+
const head = workflow.output.path.split(".")[0];
|
|
1109
|
+
if (!reachability.available.has(head))
|
|
1110
|
+
errors.push(`output: path "${workflow.output.path}" has no input or upstream producer; select the final node's as key (or label when as is omitted)`);
|
|
1111
|
+
else
|
|
1112
|
+
checkTypedPath(workflow.output.path, "output");
|
|
1113
|
+
}
|
|
1114
|
+
// Output-completeness: when the node writing the output path is a probeable code node, its
|
|
1115
|
+
// probed shape must carry every REQUIRED key of the output schema. A required field added to
|
|
1116
|
+
// the contract (e.g. self_corrections) that assemble never emits otherwise dies only at
|
|
1117
|
+
// runtime — every case fails output validation with no author-time signal. Conservative:
|
|
1118
|
+
// stands down when the output head is not code-probed, when the probe hit computed keys, or
|
|
1119
|
+
// when navigation leaves plain objects.
|
|
1120
|
+
if (reachability && workflow?.output?.path && (Object.hasOwn(workflow.schemas, workflow.output.schemaId) ? workflow.schemas[workflow.output.schemaId] : undefined)) {
|
|
1121
|
+
const outSchema = workflow.schemas[workflow.output.schemaId];
|
|
1122
|
+
const required = Array.isArray(outSchema.required) ? outSchema.required.filter((k) => typeof k === "string") : [];
|
|
1123
|
+
const segments = String(workflow.output.path).split(".");
|
|
1124
|
+
const subtrees = probedSubtrees.get(segments[0]);
|
|
1125
|
+
if (required.length && subtrees?.length && !spreadTaintedKeys.has(segments[0])) {
|
|
1126
|
+
let bestMissing = null;
|
|
1127
|
+
for (const subtree of subtrees) {
|
|
1128
|
+
let cursor = subtree;
|
|
1129
|
+
let standDown = false;
|
|
1130
|
+
for (const segment of segments.slice(1)) {
|
|
1131
|
+
if (!cursor || typeof cursor !== "object" || typeof cursor === "function" || Array.isArray(cursor)) {
|
|
1132
|
+
standDown = true;
|
|
1133
|
+
break;
|
|
1134
|
+
}
|
|
1135
|
+
if (Object.prototype.hasOwnProperty.call(cursor, "") || Object.prototype.hasOwnProperty.call(cursor, "undefined")) {
|
|
1136
|
+
standDown = true;
|
|
1137
|
+
break;
|
|
1138
|
+
}
|
|
1139
|
+
if (!(segment in cursor)) {
|
|
1140
|
+
standDown = true;
|
|
1141
|
+
break;
|
|
1142
|
+
}
|
|
1143
|
+
cursor = cursor[segment];
|
|
1144
|
+
}
|
|
1145
|
+
if (standDown || !cursor || typeof cursor !== "object" || typeof cursor === "function" || Array.isArray(cursor))
|
|
1146
|
+
continue;
|
|
1147
|
+
if (Object.prototype.hasOwnProperty.call(cursor, "") || Object.prototype.hasOwnProperty.call(cursor, "undefined"))
|
|
1148
|
+
continue;
|
|
1149
|
+
const missing = required.filter((key) => !(key in cursor));
|
|
1150
|
+
if (!missing.length) {
|
|
1151
|
+
bestMissing = null;
|
|
1152
|
+
break;
|
|
1153
|
+
}
|
|
1154
|
+
if (!bestMissing || missing.length < bestMissing.length)
|
|
1155
|
+
bestMissing = missing;
|
|
1156
|
+
}
|
|
1157
|
+
if (bestMissing?.length) {
|
|
1158
|
+
errors.push(`output: the code node writing "${workflow.output.path}" never emits required field(s) ${bestMissing.join(", ")} of schema "${workflow.output.schemaId}" — every run would fail output validation`);
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
if (!errors.length)
|
|
1163
|
+
errors.push(...workflowShapeErrors(workflow, true));
|
|
1164
|
+
return errors.length ? { ok: false, errors } : { ok: true };
|
|
1165
|
+
}
|
|
1166
|
+
function getPath(value, path) {
|
|
1167
|
+
return path.split(".").reduce((acc, key) => (acc == null ? acc : acc[key]), value);
|
|
1168
|
+
}
|
|
1169
|
+
function interpolate(template, state) {
|
|
1170
|
+
return template.replace(/\{([a-zA-Z0-9_.$]+)\}/g, (_, p) => {
|
|
1171
|
+
let v = getPath(state, p);
|
|
1172
|
+
if (v === undefined && p.startsWith("state."))
|
|
1173
|
+
v = getPath(state, p.slice(6));
|
|
1174
|
+
return v === undefined || v === null ? `(${p} unset)` : typeof v === "string" ? v : JSON.stringify(v);
|
|
1175
|
+
});
|
|
1176
|
+
}
|
|
1177
|
+
export class EscalationSignal extends Error {
|
|
1178
|
+
escalation;
|
|
1179
|
+
constructor(escalation) {
|
|
1180
|
+
super(`escalated: ${escalation.kind}`);
|
|
1181
|
+
this.escalation = escalation;
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
async function evaluatePredicate(pred, state, deps, at) {
|
|
1185
|
+
if (pred.predicate !== "ask")
|
|
1186
|
+
return { holds: predicateMatches(pred, state) };
|
|
1187
|
+
const ask = pred;
|
|
1188
|
+
const runJudge = requireJudge(deps, `${at.kind} (ask predicate)`, at.label);
|
|
1189
|
+
const asked = ask.state ? interpolateValue(ask.state, state, at.label) : promptStateOf(state);
|
|
1190
|
+
const questions = { holds: { type: "noul", instructions: ask.instructions, ...(ask.criteria ? { criteria: ask.criteria } : {}) } };
|
|
1191
|
+
const result = await runJudge({ label: at.label, kind: "ask", state: asked, questions, signal: deps.signal });
|
|
1192
|
+
validateAnswers(questions, result.answers);
|
|
1193
|
+
const answer = result.answers.holds;
|
|
1194
|
+
const p = answer?.type === "noul" ? answer.noul : NaN;
|
|
1195
|
+
if (!Number.isFinite(p))
|
|
1196
|
+
throw new Error(`${at.kind} node "${at.label}": the ask predicate got no yes/no answer`);
|
|
1197
|
+
const gte = ask.gte ?? 0.6;
|
|
1198
|
+
deps.onEvent?.({ type: "ask.evaluated", label: at.label, detail: { kind: at.kind, p_yes: +p.toFixed(4), gte, holds: p >= gte, model: result.model ?? null, cost_usd: result.cost_usd ?? null } });
|
|
1199
|
+
return { holds: p >= gte, detail: { p_yes: p, gte } };
|
|
1200
|
+
}
|
|
1201
|
+
function promptStateOf(state) {
|
|
1202
|
+
const context = state.context;
|
|
1203
|
+
if (!context || (context.references === undefined && context.references_parsed === undefined))
|
|
1204
|
+
return state;
|
|
1205
|
+
const { references: _r, references_parsed: _rp, ...rest } = context;
|
|
1206
|
+
return { ...state, context: rest };
|
|
1207
|
+
}
|
|
1208
|
+
export function schemaProblems(validator, candidate) {
|
|
1209
|
+
return [...validator.Errors(candidate)]
|
|
1210
|
+
.slice(0, 8)
|
|
1211
|
+
// Instance paths and propertyNames messages can contain source-derived keys.
|
|
1212
|
+
// Schema paths describe the authored contract instead of rejected input data.
|
|
1213
|
+
.map((error) => `${error.schemaPath || "#"}: ${error.keyword === "propertyNames"
|
|
1214
|
+
? "property names must satisfy the declared schema" : error.keyword === "~refine"
|
|
1215
|
+
? "must satisfy the declared refinement" : error.message}`);
|
|
1216
|
+
}
|
|
1217
|
+
export class WorkflowInvalidError extends Error {
|
|
1218
|
+
workflowName;
|
|
1219
|
+
problems;
|
|
1220
|
+
code = "workflow_invalid";
|
|
1221
|
+
stage = "validate";
|
|
1222
|
+
constructor(workflowName, problems) {
|
|
1223
|
+
super(`workflow "${workflowName}" is invalid: ${problems.join("; ")}`);
|
|
1224
|
+
this.workflowName = workflowName;
|
|
1225
|
+
this.problems = problems;
|
|
1226
|
+
this.name = "WorkflowInvalidError";
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
export class WorkflowInputInvalidError extends Error {
|
|
1230
|
+
workflowName;
|
|
1231
|
+
problems;
|
|
1232
|
+
code = "input_invalid";
|
|
1233
|
+
stage = "input";
|
|
1234
|
+
constructor(workflowName, problems) {
|
|
1235
|
+
super(`workflow "${workflowName}" input does not satisfy its schema: ${problems.join("; ")}`);
|
|
1236
|
+
this.workflowName = workflowName;
|
|
1237
|
+
this.problems = problems;
|
|
1238
|
+
this.name = "WorkflowInputInvalidError";
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
/** The adapter returned after its deadline. Its effect must be reconciled by the host;
|
|
1242
|
+
* the engine refuses the late result and never retries it automatically. */
|
|
1243
|
+
export class EffectDeadlineExceededError extends Error {
|
|
1244
|
+
stage;
|
|
1245
|
+
lateResult;
|
|
1246
|
+
code = "effect_deadline_exceeded";
|
|
1247
|
+
constructor(stage, lateResult) {
|
|
1248
|
+
super(`call node "${stage}" returned after its deadline; reconcile the effect before retrying`);
|
|
1249
|
+
this.stage = stage;
|
|
1250
|
+
this.lateResult = lateResult;
|
|
1251
|
+
this.name = "EffectDeadlineExceededError";
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
/** An admitted effect did not settle before cancellation or its deadline. An abort signal
|
|
1255
|
+
* cannot prove whether the external effect occurred. Reconcile before any new admission. */
|
|
1256
|
+
export class EffectOutcomeUnknownError extends Error {
|
|
1257
|
+
stage;
|
|
1258
|
+
interruption;
|
|
1259
|
+
attempt;
|
|
1260
|
+
idempotencyKey;
|
|
1261
|
+
settlement;
|
|
1262
|
+
executionPath;
|
|
1263
|
+
code = "effect_outcome_unknown";
|
|
1264
|
+
outcome = "unknown";
|
|
1265
|
+
constructor(stage, interruption, attempt, idempotencyKey, settlement, cause, executionPath) {
|
|
1266
|
+
super(`call node "${stage}" did not settle before ${interruption}; its effect outcome is unknown and must be reconciled before retrying`, { cause });
|
|
1267
|
+
this.stage = stage;
|
|
1268
|
+
this.interruption = interruption;
|
|
1269
|
+
this.attempt = attempt;
|
|
1270
|
+
this.idempotencyKey = idempotencyKey;
|
|
1271
|
+
this.settlement = settlement;
|
|
1272
|
+
this.executionPath = executionPath;
|
|
1273
|
+
this.name = "EffectOutcomeUnknownError";
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
export class WorkflowOutputInvalidError extends Error {
|
|
1277
|
+
problems;
|
|
1278
|
+
stage;
|
|
1279
|
+
code = "output_invalid";
|
|
1280
|
+
constructor(summary, problems, stage = "output") {
|
|
1281
|
+
super(problems.length ? `${summary}: ${problems.join("; ")}` : summary);
|
|
1282
|
+
this.problems = problems;
|
|
1283
|
+
this.stage = stage;
|
|
1284
|
+
this.name = "WorkflowOutputInvalidError";
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
/** Local transform failure. The cause is retained for trusted hosts, not author-facing diagnostics. */
|
|
1288
|
+
export class WorkflowCodeError extends Error {
|
|
1289
|
+
stage;
|
|
1290
|
+
code = "code_transform_failed";
|
|
1291
|
+
constructor(stage, cause) {
|
|
1292
|
+
super(`code node "${stage}" failed: ${cause instanceof Error ? cause.message : 'transform threw'}; check its state paths, synchronous function body and supported globals`, { cause });
|
|
1293
|
+
this.stage = stage;
|
|
1294
|
+
this.name = "WorkflowCodeError";
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
/** A mechanical state-contract failure, distinct from provider or authored-code errors.
|
|
1298
|
+
* Trusted hosts retain the location; a parallel collision key can be runtime-derived
|
|
1299
|
+
* and must be omitted by public diagnostics. No full state object is retained. */
|
|
1300
|
+
export class WorkflowStateError extends Error {
|
|
1301
|
+
stage;
|
|
1302
|
+
path;
|
|
1303
|
+
reason;
|
|
1304
|
+
code = "state_invalid";
|
|
1305
|
+
constructor(summary, stage, path, reason) {
|
|
1306
|
+
super(summary);
|
|
1307
|
+
this.stage = stage;
|
|
1308
|
+
this.path = path;
|
|
1309
|
+
this.reason = reason;
|
|
1310
|
+
this.name = "WorkflowStateError";
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
export function stageSchemaForNode(workflow, node) {
|
|
1314
|
+
if (node.node === "report")
|
|
1315
|
+
return REPORT_SCHEMA;
|
|
1316
|
+
return resolveSchemaForWorkflow(workflow, workflow.schemas[node.out]);
|
|
1317
|
+
}
|
|
1318
|
+
export const submissionSchemaForNode = stageSchemaForNode;
|
|
1319
|
+
const hasConcreteValue = (value) => {
|
|
1320
|
+
if (typeof value === "string")
|
|
1321
|
+
return Boolean(value.trim());
|
|
1322
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
1323
|
+
return true;
|
|
1324
|
+
if (Array.isArray(value))
|
|
1325
|
+
return value.some(hasConcreteValue);
|
|
1326
|
+
return Boolean(value && typeof value === "object" && Object.values(value).some(hasConcreteValue));
|
|
1327
|
+
};
|
|
1328
|
+
function assertNodeInputs(node, state) {
|
|
1329
|
+
const requiredPaths = [...(node.requires || [])];
|
|
1330
|
+
for (const requiredPath of requiredPaths) {
|
|
1331
|
+
if (!hasConcreteValue(getPath(state, requiredPath))) {
|
|
1332
|
+
throw new WorkflowStateError(`${node.node} node "${node.label}" requires non-empty state path "${requiredPath}" before agent construction`, node.label, requiredPath, "required_nonempty");
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
export function mergeStageDelta(state, node, result, schema) {
|
|
1337
|
+
if (node.node === "report")
|
|
1338
|
+
return { ...state, report_markdown: result.report_markdown };
|
|
1339
|
+
const properties = schema?.properties && typeof schema.properties === "object"
|
|
1340
|
+
? schema.properties
|
|
1341
|
+
: null;
|
|
1342
|
+
const delta = properties
|
|
1343
|
+
? Object.fromEntries(Object.entries(result).filter(([key]) => Object.prototype.hasOwnProperty.call(properties, key)))
|
|
1344
|
+
: result;
|
|
1345
|
+
return { ...state, [node.as || node.label]: delta };
|
|
1346
|
+
}
|
|
1347
|
+
function shapeFromSchema(schema) {
|
|
1348
|
+
const properties = schema && typeof schema === "object" ? schema.properties : undefined;
|
|
1349
|
+
if (!properties || typeof properties !== "object")
|
|
1350
|
+
return "";
|
|
1351
|
+
return Object.fromEntries(Object.entries(properties).map(([key, sub]) => [key, shapeFromSchema(sub)]));
|
|
1352
|
+
}
|
|
1353
|
+
function interpolateValue(value, state, stage = "interpolation") {
|
|
1354
|
+
if (typeof value === "string") {
|
|
1355
|
+
const whole = value.match(/^\{([a-zA-Z0-9_.$]+)\}$/);
|
|
1356
|
+
if (whole) {
|
|
1357
|
+
const p = whole[1];
|
|
1358
|
+
let v = getPath(state, p);
|
|
1359
|
+
if (v === undefined && p.startsWith("state."))
|
|
1360
|
+
v = getPath(state, p.slice(6));
|
|
1361
|
+
if (v === undefined)
|
|
1362
|
+
throw new WorkflowStateError(`interpolation path "${p}" is not present in the state`, stage, p, "missing_interpolation");
|
|
1363
|
+
return v;
|
|
1364
|
+
}
|
|
1365
|
+
return value.replace(/\{([a-zA-Z0-9_.$]+)\}/g, (_, p) => {
|
|
1366
|
+
let v = getPath(state, p);
|
|
1367
|
+
if (v === undefined && p.startsWith("state."))
|
|
1368
|
+
v = getPath(state, p.slice(6));
|
|
1369
|
+
if (v === undefined)
|
|
1370
|
+
throw new WorkflowStateError(`interpolation path "${p}" is not present in the state`, stage, p, "missing_interpolation");
|
|
1371
|
+
return typeof v === "string" ? v : JSON.stringify(v);
|
|
1372
|
+
});
|
|
1373
|
+
}
|
|
1374
|
+
if (Array.isArray(value))
|
|
1375
|
+
return value.map((v) => interpolateValue(v, state, stage));
|
|
1376
|
+
if (value && typeof value === "object")
|
|
1377
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, interpolateValue(v, state, stage)]));
|
|
1378
|
+
return value;
|
|
1379
|
+
}
|
|
1380
|
+
function canonicalJson(value) {
|
|
1381
|
+
const stable = (v) => Array.isArray(v) ? v.map(stable) : v && typeof v === "object" ? Object.fromEntries(Object.keys(v).sort().map((k) => [k, stable(v[k])])) : v;
|
|
1382
|
+
return JSON.stringify(stable(value));
|
|
1383
|
+
}
|
|
1384
|
+
const sleep = (ms, signal) => new Promise((resolve, reject) => {
|
|
1385
|
+
if (signal?.aborted) {
|
|
1386
|
+
reject(signal.reason ?? new Error("workflow aborted"));
|
|
1387
|
+
return;
|
|
1388
|
+
}
|
|
1389
|
+
const aborted = () => { clearTimeout(timer); reject(signal?.reason ?? new Error("workflow aborted")); };
|
|
1390
|
+
const timer = setTimeout(() => { signal?.removeEventListener("abort", aborted); resolve(); }, ms);
|
|
1391
|
+
signal?.addEventListener("abort", aborted, { once: true });
|
|
1392
|
+
});
|
|
1393
|
+
async function runCallNode(node, state, workflow, deps) {
|
|
1394
|
+
if (!deps.runEffect)
|
|
1395
|
+
throw new Error(`call node "${node.label}": this runner has no effect executor (deps.runEffect)`);
|
|
1396
|
+
assertNodeInputs(node, state);
|
|
1397
|
+
const input = node.via === "tool"
|
|
1398
|
+
? interpolateValue(node.args ?? {}, state, node.label)
|
|
1399
|
+
: node.via === "executor"
|
|
1400
|
+
? interpolateValue(node.input ?? {}, state, node.label)
|
|
1401
|
+
: { command: String(node.command), ...(node.env ? { env: Object.fromEntries(Object.entries(interpolateValue(node.env, state, node.label)).map(([k, v]) => [k, typeof v === "string" ? v : JSON.stringify(v)])) } : {}) };
|
|
1402
|
+
const produces = (node.produces ?? []).map((entry) => String(interpolateValue(entry, state, node.label)));
|
|
1403
|
+
// Existing flat pins retain their exact receipt identity. Newly supported composition uses
|
|
1404
|
+
// a versioned tuple including the full runtime path; its document could not run previously.
|
|
1405
|
+
const identity = deps[SCOPED_EFFECTS]
|
|
1406
|
+
? canonicalJson(["execution-path-v1", executionPath(deps), node.label, node.via, node.tool ?? node.code ?? "", input])
|
|
1407
|
+
: `${node.label}\n${node.via}\n${node.tool ?? node.code ?? ""}\n${canonicalJson(input)}`;
|
|
1408
|
+
const idempotencyKey = createHash("sha256").update(identity).digest("hex");
|
|
1409
|
+
const effectNode = produces.length ? { ...node, produces } : node;
|
|
1410
|
+
const schema = node.via === "shell" ? SHELL_RESULT_SCHEMA : resolveSchemaForWorkflow(workflow, workflow.schemas[node.out]);
|
|
1411
|
+
const validator = Compile(schema);
|
|
1412
|
+
const attempts = Math.max(1, node.retry?.attempts ?? 1);
|
|
1413
|
+
const retryOn = new Set(node.retry?.on ?? ["timeout", "http_5xx", "http_429", "connection", "exit"]);
|
|
1414
|
+
const executeOnce = async (budgetUntilMs) => {
|
|
1415
|
+
let result;
|
|
1416
|
+
let lastError;
|
|
1417
|
+
let failed = false;
|
|
1418
|
+
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
|
1419
|
+
if (deps.signal?.aborted)
|
|
1420
|
+
throw deps.signal.reason ?? new Error("workflow aborted");
|
|
1421
|
+
const remainingMs = budgetUntilMs === undefined ? Infinity : budgetUntilMs - Date.now();
|
|
1422
|
+
// A durable receipt can be retrieved after expiry. The recovery adapter checks
|
|
1423
|
+
// the original deadline before any NEW admission; it never extends the poll.
|
|
1424
|
+
// Expired receipt reads receive the node deadline so asynchronous store IO can finish.
|
|
1425
|
+
if (remainingMs <= 0 && !deps.recovery)
|
|
1426
|
+
throw new EffectFailure(`call node "${node.label}" poll exceeded its ${node.poll?.deadline_s}s deadline`, "timeout");
|
|
1427
|
+
const controller = new AbortController();
|
|
1428
|
+
const attemptMs = Math.max(1, Math.min(node.deadline_s * 1000, remainingMs <= 0 && deps.recovery ? Infinity : remainingMs));
|
|
1429
|
+
const attemptDeadlineMs = Date.now() + attemptMs;
|
|
1430
|
+
let settled;
|
|
1431
|
+
let unknownOutcome;
|
|
1432
|
+
let interruptionCheck;
|
|
1433
|
+
let resolveInterruption;
|
|
1434
|
+
const interrupted = new Promise(resolve => { resolveInterruption = resolve; });
|
|
1435
|
+
let resolveSettlement;
|
|
1436
|
+
const settlement = new Promise(resolve => { resolveSettlement = resolve; });
|
|
1437
|
+
const finish = (value) => {
|
|
1438
|
+
settled = value;
|
|
1439
|
+
resolveSettlement(value);
|
|
1440
|
+
if (unknownOutcome) {
|
|
1441
|
+
try {
|
|
1442
|
+
deps.onEvent?.({ type: "effect.late_settled", label: node.label, detail: {
|
|
1443
|
+
via: node.via, attempt, idempotency_key: idempotencyKey,
|
|
1444
|
+
interruption: unknownOutcome.interruption, ...value,
|
|
1445
|
+
} });
|
|
1446
|
+
}
|
|
1447
|
+
catch { /* The settlement promise still retains the complete adapter evidence. */ }
|
|
1448
|
+
}
|
|
1449
|
+
};
|
|
1450
|
+
const interrupt = (interruption, cause) => {
|
|
1451
|
+
if (controller.signal.aborted)
|
|
1452
|
+
return;
|
|
1453
|
+
controller.abort(cause);
|
|
1454
|
+
// Drain synchronous abort rejection and its promise reactions first. A settled
|
|
1455
|
+
// cooperative failure can honor retry policy; a still-pending adapter cannot.
|
|
1456
|
+
interruptionCheck = setImmediate(() => {
|
|
1457
|
+
if (settled)
|
|
1458
|
+
return;
|
|
1459
|
+
unknownOutcome = new EffectOutcomeUnknownError(node.label, interruption, attempt, idempotencyKey, settlement, cause, executionPath(deps));
|
|
1460
|
+
resolveInterruption({ status: "unknown", error: unknownOutcome });
|
|
1461
|
+
});
|
|
1462
|
+
};
|
|
1463
|
+
const onAbort = () => interrupt("cancelled", deps.signal?.reason);
|
|
1464
|
+
deps.signal?.addEventListener("abort", onAbort, { once: true });
|
|
1465
|
+
const timer = setTimeout(() => interrupt("deadline"), attemptMs);
|
|
1466
|
+
try {
|
|
1467
|
+
deps.onEvent?.({ type: "effect.attempt", label: node.label, detail: { via: node.via, attempt, idempotency_key: idempotencyKey } });
|
|
1468
|
+
Promise.resolve().then(() => {
|
|
1469
|
+
if (deps.signal?.aborted)
|
|
1470
|
+
throw deps.signal.reason ?? new Error("workflow aborted");
|
|
1471
|
+
if (controller.signal.aborted || Date.now() >= attemptDeadlineMs) {
|
|
1472
|
+
throw new EffectFailure(`call node "${node.label}" deadline elapsed before effect admission`, "timeout");
|
|
1473
|
+
}
|
|
1474
|
+
return deps.runEffect({ node: effectNode, schema, input, produces, attempt, idempotencyKey, signal: controller.signal });
|
|
1475
|
+
}).then(value => finish({ status: "fulfilled", value }), reason => finish({ status: "rejected", reason }));
|
|
1476
|
+
const outcome = await Promise.race([settlement, interrupted]);
|
|
1477
|
+
if (outcome.status === "unknown")
|
|
1478
|
+
throw outcome.error;
|
|
1479
|
+
if (outcome.status === "rejected")
|
|
1480
|
+
throw outcome.reason;
|
|
1481
|
+
result = outcome.value;
|
|
1482
|
+
if (deps.signal?.aborted)
|
|
1483
|
+
throw deps.signal.reason ?? new Error("workflow aborted");
|
|
1484
|
+
if (controller.signal.aborted || Date.now() >= attemptDeadlineMs)
|
|
1485
|
+
throw new EffectDeadlineExceededError(node.label, result);
|
|
1486
|
+
failed = false;
|
|
1487
|
+
lastError = undefined;
|
|
1488
|
+
break;
|
|
1489
|
+
}
|
|
1490
|
+
catch (error) {
|
|
1491
|
+
failed = true;
|
|
1492
|
+
const cutByPoll = controller.signal.aborted && budgetUntilMs !== undefined && Date.now() >= budgetUntilMs;
|
|
1493
|
+
lastError = error instanceof EffectOutcomeUnknownError ? error
|
|
1494
|
+
: deps.signal?.aborted ? deps.signal.reason ?? new Error("workflow aborted")
|
|
1495
|
+
: controller.signal.aborted && !(error instanceof EffectFailure) && !(error instanceof EffectDeadlineExceededError)
|
|
1496
|
+
? new EffectFailure(cutByPoll ? `call node "${node.label}" poll exceeded its ${node.poll?.deadline_s}s deadline` : `call node "${node.label}" exceeded its ${node.deadline_s}s deadline`, "timeout")
|
|
1497
|
+
: error;
|
|
1498
|
+
const retryClass = lastError instanceof EffectFailure ? lastError.retryClass : null;
|
|
1499
|
+
// Durable admission is uncertain after failure; only the host can reconcile it.
|
|
1500
|
+
const retryable = !deps.signal?.aborted && !deps.recovery && retryClass !== null && retryOn.has(retryClass) && attempt < attempts;
|
|
1501
|
+
deps.onEvent?.({ type: "effect.failed", label: node.label, detail: { via: node.via, attempt, retry_class: retryClass, retrying: retryable, ...(lastError instanceof EffectOutcomeUnknownError ? { outcome: "unknown", interruption: lastError.interruption, idempotency_key: idempotencyKey } : {}), message: String(lastError?.message || lastError).slice(0, 300) } });
|
|
1502
|
+
if (!retryable || (budgetUntilMs !== undefined && Date.now() >= budgetUntilMs))
|
|
1503
|
+
break;
|
|
1504
|
+
const delay = Math.min((node.retry?.backoff_s ?? 1) * 1000 * attempt, budgetUntilMs === undefined ? Infinity : Math.max(0, budgetUntilMs - Date.now()));
|
|
1505
|
+
await sleep(delay, deps.signal);
|
|
1506
|
+
}
|
|
1507
|
+
finally {
|
|
1508
|
+
clearTimeout(timer);
|
|
1509
|
+
if (interruptionCheck)
|
|
1510
|
+
clearImmediate(interruptionCheck);
|
|
1511
|
+
deps.signal?.removeEventListener("abort", onAbort);
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
if (failed)
|
|
1515
|
+
throw lastError instanceof Error ? lastError : new Error(String(lastError), { cause: lastError });
|
|
1516
|
+
return result;
|
|
1517
|
+
};
|
|
1518
|
+
const invalid = (value) => new WorkflowOutputInvalidError(`call node "${node.label}" result does not satisfy schema "${node.via === "shell" ? "shell" : node.out}"`, schemaProblems(validator, value), node.label);
|
|
1519
|
+
let result;
|
|
1520
|
+
const memoized = !node.poll && deps.memo ? await deps.memo.get(idempotencyKey, effectNode) : undefined;
|
|
1521
|
+
if (memoized !== undefined) {
|
|
1522
|
+
deps.onEvent?.({ type: "effect.memo", label: node.label, detail: { via: node.via, idempotency_key: idempotencyKey } });
|
|
1523
|
+
result = memoized;
|
|
1524
|
+
}
|
|
1525
|
+
else if (!node.poll || deps.syntheticEffects) {
|
|
1526
|
+
result = await executeOnce();
|
|
1527
|
+
}
|
|
1528
|
+
else {
|
|
1529
|
+
const startedAt = deps.recovery?.pollStartedAt(node, undefined, executionPath(deps)) ?? Date.now();
|
|
1530
|
+
const pollUntilMs = startedAt + node.poll.deadline_s * 1000;
|
|
1531
|
+
for (let iteration = 1;; iteration += 1) {
|
|
1532
|
+
result = await executeOnce(pollUntilMs);
|
|
1533
|
+
if (!validator.Check(result))
|
|
1534
|
+
throw invalid(result);
|
|
1535
|
+
const elapsedMs = Date.now() - startedAt;
|
|
1536
|
+
if (node.poll.fail_when && predicateMatches(node.poll.fail_when, result)) {
|
|
1537
|
+
deps.onEvent?.({ type: "effect.poll", label: node.label, detail: { iteration, settled: "fail_when", elapsed_ms: elapsedMs } });
|
|
1538
|
+
throw new EffectFailure(`call node "${node.label}" poll met fail_when after ${iteration} check(s): ${JSON.stringify(result).slice(0, 300)}`, null);
|
|
1539
|
+
}
|
|
1540
|
+
if (predicateMatches(node.poll.until, result)) {
|
|
1541
|
+
deps.onEvent?.({ type: "effect.poll", label: node.label, detail: { iteration, settled: "until", elapsed_ms: elapsedMs } });
|
|
1542
|
+
break;
|
|
1543
|
+
}
|
|
1544
|
+
if (!deps.recovery && elapsedMs + node.poll.interval_s * 1000 > node.poll.deadline_s * 1000) {
|
|
1545
|
+
deps.onEvent?.({ type: "effect.poll", label: node.label, detail: { iteration, settled: "deadline", elapsed_ms: elapsedMs } });
|
|
1546
|
+
throw new EffectFailure(`call node "${node.label}" poll exceeded its ${node.poll.deadline_s}s deadline after ${iteration} check(s)`, "timeout");
|
|
1547
|
+
}
|
|
1548
|
+
deps.onEvent?.({ type: "effect.poll", label: node.label, detail: { iteration, settled: null, elapsed_ms: elapsedMs } });
|
|
1549
|
+
if (deps.recovery)
|
|
1550
|
+
await deps.recovery.wait(node, node.poll.interval_s * 1000, undefined, executionPath(deps));
|
|
1551
|
+
else
|
|
1552
|
+
await sleep(node.poll.interval_s * 1000, deps.signal);
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1555
|
+
if (!validator.Check(result)) {
|
|
1556
|
+
throw new WorkflowOutputInvalidError(`call node "${node.label}" result does not satisfy schema "${node.via === "shell" ? "shell" : node.out}"`, schemaProblems(validator, result), node.label);
|
|
1557
|
+
}
|
|
1558
|
+
// Only a schema-valid completed effect is reusable. Once a recovery store is
|
|
1559
|
+
// supplied, losing its write must stop downstream effects rather than silently
|
|
1560
|
+
// claiming that this execution can safely resume.
|
|
1561
|
+
if (deps.memo && memoized === undefined && !node.poll && !deps.syntheticEffects) {
|
|
1562
|
+
await deps.memo.put(idempotencyKey, effectNode, result);
|
|
1563
|
+
}
|
|
1564
|
+
return { ...state, [node.as]: result };
|
|
1565
|
+
}
|
|
1566
|
+
function assertQuestionCount(deps, count, label) {
|
|
1567
|
+
const limit = deps.maxQuestionsPerRequest ?? 256;
|
|
1568
|
+
if (!Number.isSafeInteger(limit) || limit < 1)
|
|
1569
|
+
throw new Error("maxQuestionsPerRequest must be a positive safe integer");
|
|
1570
|
+
if (count > limit)
|
|
1571
|
+
throw new Error(`node "${label}": ${count} questions exceed maxQuestionsPerRequest (${limit}); reduce the collection/question set or raise the host limit`);
|
|
1572
|
+
}
|
|
1573
|
+
const requireJudge = (deps, kind, label) => {
|
|
1574
|
+
if (!deps.runJudge)
|
|
1575
|
+
throw new Error(`${kind} node "${label}": this runner has no System One runner (deps.runJudge)`);
|
|
1576
|
+
return async (params) => {
|
|
1577
|
+
assertQuestionCount(deps, Object.keys(params.questions).length, params.label);
|
|
1578
|
+
return deps.runJudge(params);
|
|
1579
|
+
};
|
|
1580
|
+
};
|
|
1581
|
+
function questionSetOf(workflow, node) {
|
|
1582
|
+
const schema = resolveSchemaForWorkflow(workflow, workflow.schemas[node.out]);
|
|
1583
|
+
const compiled = compileQuestions(schema, workflow.schemas);
|
|
1584
|
+
if (compiled.ok === false)
|
|
1585
|
+
throw new Error(`${node.node} node "${node.label}": ${compiled.errors.join("; ")}`);
|
|
1586
|
+
const validator = Compile(schema);
|
|
1587
|
+
return {
|
|
1588
|
+
questions: compiled.questions,
|
|
1589
|
+
decode: (answers, what = "") => {
|
|
1590
|
+
const value = answersToValue(answers, compiled.questions);
|
|
1591
|
+
if (!validator.Check(value))
|
|
1592
|
+
throw new WorkflowOutputInvalidError(`${node.node} node "${node.label}"${what} answers do not satisfy schema "${node.out}"`, schemaProblems(validator, value), node.label);
|
|
1593
|
+
return value;
|
|
1594
|
+
},
|
|
1595
|
+
};
|
|
1596
|
+
}
|
|
1597
|
+
const describeItem = (template, state, item, index) => interpolate(template, { ...state, item, item_index: index }).replace(/\s+/g, " ").trim();
|
|
1598
|
+
async function askQuestions(deps, node, asked, questions) {
|
|
1599
|
+
const runJudge = requireJudge(deps, node.node, node.label);
|
|
1600
|
+
const result = await runJudge({ label: node.label, kind: node.node, state: asked, questions, signal: deps.signal });
|
|
1601
|
+
validateAnswers(questions, result.answers);
|
|
1602
|
+
return { answers: result.answers, sidecar: answersSidecar(result.answers), model: result.model ?? null, cost_usd: result.cost_usd ?? null };
|
|
1603
|
+
}
|
|
1604
|
+
async function runJudgeNode(node, state, workflow, deps) {
|
|
1605
|
+
assertNodeInputs(node, state);
|
|
1606
|
+
const set = questionSetOf(workflow, node);
|
|
1607
|
+
const { answers, sidecar, model, cost_usd } = await askQuestions(deps, node, interpolateValue(node.state, state, node.label), set.questions);
|
|
1608
|
+
const value = set.decode(answers);
|
|
1609
|
+
deps.onEvent?.({ type: "judge.answered", label: node.label, detail: { kind: "judge", as: node.as, value, sidecar, model, cost_usd } });
|
|
1610
|
+
return { ...state, [node.as]: value, [`${node.as}$answers`]: sidecar };
|
|
1611
|
+
}
|
|
1612
|
+
async function runPickNode(node, state, deps) {
|
|
1613
|
+
assertNodeInputs(node, state);
|
|
1614
|
+
const items = getPath(state, node.itemsPath);
|
|
1615
|
+
if (!Array.isArray(items))
|
|
1616
|
+
throw new WorkflowStateError(`pick node "${node.label}": ${node.itemsPath} is not a list`, node.label, node.itemsPath, "expected_list");
|
|
1617
|
+
const NONE = "none_of_these";
|
|
1618
|
+
if (!items.length) {
|
|
1619
|
+
if (!node.allowNone)
|
|
1620
|
+
throw new WorkflowStateError(`pick node "${node.label}": ${node.itemsPath} is empty and the pick does not allow none`, node.label, node.itemsPath, "empty_selection");
|
|
1621
|
+
const value = { index: null, item: null, none: true, option: null };
|
|
1622
|
+
const sidecar = answersSidecar({});
|
|
1623
|
+
deps.onEvent?.({ type: "judge.answered", label: node.label, detail: { kind: "pick", as: node.as, empty: true, value, sidecar } });
|
|
1624
|
+
return { ...state, [node.as]: value, [`${node.as}$answers`]: sidecar };
|
|
1625
|
+
}
|
|
1626
|
+
const capacity = SYSTEM_ONE_LIMITS.maxChoiceOptions - (node.allowNone ? 1 : 0);
|
|
1627
|
+
if (items.length > capacity)
|
|
1628
|
+
throw new Error(`pick node "${node.label}": ${items.length} items exceed the ${capacity} a choice can hold — sift first`);
|
|
1629
|
+
const options = Object.fromEntries(items.map((item, i) => [`item_${i}`, describeItem(node.describe, state, item, i) || `item ${i}`]));
|
|
1630
|
+
const criteria = node.allowNone ? { ...options, [NONE]: "none of the items fits" } : options;
|
|
1631
|
+
const asked = { ...(node.state ? interpolateValue(node.state, state, node.label) : { context: promptStateOf(state) }), candidates: options };
|
|
1632
|
+
const { answers, sidecar, model, cost_usd } = await askQuestions(deps, node, asked, { pick: { type: "choice", instructions: node.instructions, criteria } });
|
|
1633
|
+
const a = answers.pick;
|
|
1634
|
+
if (!a || a.type !== "choice")
|
|
1635
|
+
throw new Error(`pick node "${node.label}": no choice came back`);
|
|
1636
|
+
const none = a.choice === NONE;
|
|
1637
|
+
const index = none ? null : Object.keys(options).indexOf(a.choice);
|
|
1638
|
+
if (index !== null && index < 0)
|
|
1639
|
+
throw new Error(`pick node "${node.label}": the choice "${a.choice}" names no item`);
|
|
1640
|
+
const value = { index, item: index === null ? null : items[index], none, option: none ? null : options[a.choice] };
|
|
1641
|
+
deps.onEvent?.({ type: "judge.answered", label: node.label, detail: { kind: "pick", as: node.as, value, sidecar, model, cost_usd } });
|
|
1642
|
+
return { ...state, [node.as]: value, [`${node.as}$answers`]: sidecar };
|
|
1643
|
+
}
|
|
1644
|
+
async function runSiftNode(node, state, workflow, deps) {
|
|
1645
|
+
assertNodeInputs(node, state);
|
|
1646
|
+
const items = getPath(state, node.itemsPath);
|
|
1647
|
+
if (!Array.isArray(items))
|
|
1648
|
+
throw new WorkflowStateError(`sift node "${node.label}": ${node.itemsPath} is not a list`, node.label, node.itemsPath, "expected_list");
|
|
1649
|
+
const set = questionSetOf(workflow, node);
|
|
1650
|
+
const ids = Object.keys(set.questions);
|
|
1651
|
+
assertQuestionCount(deps, items.length * ids.length, node.label);
|
|
1652
|
+
const [keepId, keepTail] = node.keep ? node.keep.path.split(".") : [];
|
|
1653
|
+
const values = [];
|
|
1654
|
+
const sidecars = [];
|
|
1655
|
+
const kept = [];
|
|
1656
|
+
let metering = null;
|
|
1657
|
+
if (items.length) {
|
|
1658
|
+
const base = node.state ? interpolateValue(node.state, state, node.label) : {};
|
|
1659
|
+
const named = items.map((item, i) => ({ id: `item_${i}`, ...(node.describe ? { summary: describeItem(node.describe, state, item, i) } : {}), item }));
|
|
1660
|
+
const questions = {};
|
|
1661
|
+
for (let i = 0; i < items.length; i += 1)
|
|
1662
|
+
for (const id of ids)
|
|
1663
|
+
questions[`${i}.${id}`] = { ...set.questions[id], instructions: `For \`items[${i}]\` (id item_${i}): ${set.questions[id].instructions}` };
|
|
1664
|
+
const { answers, model, cost_usd } = await askQuestions(deps, node, { ...base, items: named }, questions);
|
|
1665
|
+
metering = { model, cost_usd };
|
|
1666
|
+
for (let i = 0; i < items.length; i += 1) {
|
|
1667
|
+
const mine = Object.create(null);
|
|
1668
|
+
for (const id of ids) {
|
|
1669
|
+
const a = answers[`${i}.${id}`];
|
|
1670
|
+
if (!a)
|
|
1671
|
+
throw new Error(`sift node "${node.label}": no answer for item ${i} question "${id}"`);
|
|
1672
|
+
mine[id] = a;
|
|
1673
|
+
}
|
|
1674
|
+
values.push(set.decode(mine, ` answers for item ${i}`));
|
|
1675
|
+
sidecars.push(answersSidecar(mine));
|
|
1676
|
+
if (!node.keep) {
|
|
1677
|
+
kept.push(i);
|
|
1678
|
+
continue;
|
|
1679
|
+
}
|
|
1680
|
+
const a = mine[keepId];
|
|
1681
|
+
const measure = keepTail === "confidence" ? answerConfidence(a) : a.type === "noul" ? a.noul : a.type === "score" ? a.score : NaN;
|
|
1682
|
+
const threshold = node.keep.gte ?? (a.type === "noul" && keepTail !== "confidence" ? 0.5 : 0);
|
|
1683
|
+
if (Number.isFinite(measure) && measure >= threshold)
|
|
1684
|
+
kept.push(i);
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
const value = { items: kept.map((i) => items[i]), values, answers: sidecars, kept };
|
|
1688
|
+
deps.onEvent?.({ type: "judge.answered", label: node.label, detail: { kind: "sift", as: node.as, value, count: items.length, kept: kept.length, ...(metering ?? {}) } });
|
|
1689
|
+
return { ...state, [node.as]: value };
|
|
1690
|
+
}
|
|
1691
|
+
async function runRouteNode(node, state, workflow, deps) {
|
|
1692
|
+
assertNodeInputs(node, state);
|
|
1693
|
+
const names = Object.keys(node.branches);
|
|
1694
|
+
const criteria = Object.fromEntries(names.map((n) => [n, node.branches[n]?.criteria?.trim() || null]));
|
|
1695
|
+
const { answers, sidecar, model, cost_usd } = await askQuestions(deps, node, interpolateValue(node.state, state, node.label), { branch: { type: "choice", instructions: node.instructions, criteria } });
|
|
1696
|
+
const a = answers.branch;
|
|
1697
|
+
if (!a || a.type !== "choice" || !names.includes(a.choice))
|
|
1698
|
+
throw new Error(`route node "${node.label}": the choice "${String(a?.choice)}" names no branch`);
|
|
1699
|
+
const unsure = Boolean(node.unsure && a.confidence < node.unsure.gte);
|
|
1700
|
+
const taken = unsure ? node.unsure.branch : a.choice;
|
|
1701
|
+
const value = { branch: a.choice, taken, unsure };
|
|
1702
|
+
deps.onEvent?.({ type: "route.chosen", label: node.label, detail: { kind: "route", as: node.as ?? null, value, sidecar, model, cost_usd } });
|
|
1703
|
+
const routed = node.as ? { ...state, [node.as]: value, [`${node.as}$answers`]: sidecar } : state;
|
|
1704
|
+
return runNodeOnState(node.branches[taken].body, routed, workflow, scopeExecution(deps, "branches", taken, "body"));
|
|
1705
|
+
}
|
|
1706
|
+
const EXECUTION_PATH = Symbol("workflow.executionPath");
|
|
1707
|
+
const SCOPED_EFFECTS = Symbol("workflow.scopedEffects");
|
|
1708
|
+
const executionPath = (deps) => deps[EXECUTION_PATH] ?? "/root";
|
|
1709
|
+
const scopeExecution = (deps, ...parts) => ({
|
|
1710
|
+
...deps,
|
|
1711
|
+
[EXECUTION_PATH]: executionPath(deps) + parts.map(part => "/" + String(part).replace(/~/g, "~0").replace(/\//g, "~1")).join(""),
|
|
1712
|
+
});
|
|
1713
|
+
function withExecutionLocation(deps) {
|
|
1714
|
+
const path = executionPath(deps);
|
|
1715
|
+
return {
|
|
1716
|
+
...deps,
|
|
1717
|
+
...(deps.runNode ? { runNode: params => deps.runNode({ ...params, executionPath: params.executionPath ?? path }) } : {}),
|
|
1718
|
+
...(deps.runEffect ? { runEffect: params => deps.runEffect({ ...params, executionPath: params.executionPath ?? path }) } : {}),
|
|
1719
|
+
...(deps.runJudge ? { runJudge: params => deps.runJudge({ ...params, executionPath: params.executionPath ?? path }) } : {}),
|
|
1720
|
+
...(deps.onEvent ? { onEvent: event => deps.onEvent({ ...event, executionPath: event.executionPath ?? path }) } : {}),
|
|
1721
|
+
};
|
|
1722
|
+
}
|
|
1723
|
+
/** Older hosts only support flat child chains outside repeated/branching structure. They must
|
|
1724
|
+
* explicitly opt into location-aware recovery before newly composed graphs can execute. */
|
|
1725
|
+
function needsComposedRecovery(node, structured = false, child = false) {
|
|
1726
|
+
if (node.node === "workflow") {
|
|
1727
|
+
const steps = childSteps(node.workflow) ?? [];
|
|
1728
|
+
return structured || child || steps.some(step => !["code", "call", "agent", "decide", "extract", "escalate", "judge", "pick", "sift"].includes(step.node)) || needsComposedRecovery(node.workflow.root, false, true);
|
|
1729
|
+
}
|
|
1730
|
+
if (node.node === "chain")
|
|
1731
|
+
return node.steps.some(step => needsComposedRecovery(step, structured, child));
|
|
1732
|
+
if (node.node === "map" || node.node === "loop")
|
|
1733
|
+
return needsComposedRecovery(node.body, true, child);
|
|
1734
|
+
if (node.node === "parallel")
|
|
1735
|
+
return node.branches.some(branch => needsComposedRecovery(branch, structured, child));
|
|
1736
|
+
if (node.node === "route")
|
|
1737
|
+
return routeBodies(node).some(branch => needsComposedRecovery(branch, true, child));
|
|
1738
|
+
return false;
|
|
1739
|
+
}
|
|
1740
|
+
function scopeDepsToItem(deps, item, signal) {
|
|
1741
|
+
const recovery = deps.recovery;
|
|
1742
|
+
return {
|
|
1743
|
+
...deps,
|
|
1744
|
+
checkpoint: undefined,
|
|
1745
|
+
...(signal ? { signal } : {}),
|
|
1746
|
+
...(deps.runNode ? { runNode: params => deps.runNode({ ...params, item: params.item ?? item, ...(signal ? { signal: params.signal ?? signal } : {}) }) } : {}),
|
|
1747
|
+
...(deps.runEffect ? { runEffect: params => deps.runEffect({ ...params, item: params.item ?? item }) } : {}),
|
|
1748
|
+
...(deps.runJudge ? { runJudge: params => deps.runJudge({ ...params, item: params.item ?? item, ...(signal ? { signal: params.signal ?? signal } : {}) }) } : {}),
|
|
1749
|
+
...(recovery ? { recovery: {
|
|
1750
|
+
supportsExecutionPaths: recovery.supportsExecutionPaths,
|
|
1751
|
+
resume: (node, state, nestedItem, path) => recovery.resume(node, state, nestedItem ?? item, path),
|
|
1752
|
+
commit: (node, state, nestedItem, path) => recovery.commit(node, state, nestedItem ?? item, path),
|
|
1753
|
+
pollStartedAt: (node, nestedItem, path) => recovery.pollStartedAt(node, nestedItem ?? item, path),
|
|
1754
|
+
wait: (node, ms, nestedItem, path) => recovery.wait(node, ms, nestedItem ?? item, path),
|
|
1755
|
+
...(recovery.fail ? { fail: (node, results, error, path) => recovery.fail(node, results, error, path) } : {}),
|
|
1756
|
+
} } : {}),
|
|
1757
|
+
};
|
|
1758
|
+
}
|
|
1759
|
+
function scopeDepsToChild(deps, invocation) {
|
|
1760
|
+
deps = scopeExecution(deps, "workflow", "root");
|
|
1761
|
+
const prefix = invocation.label;
|
|
1762
|
+
const relabel = (node) => ("label" in node && typeof node.label === "string" ? { ...node, label: `${prefix}/${node.label}` } : node);
|
|
1763
|
+
const recovery = deps.recovery;
|
|
1764
|
+
return {
|
|
1765
|
+
...deps,
|
|
1766
|
+
checkpoint: undefined,
|
|
1767
|
+
...(deps.runNode ? { runNode: params => deps.runNode({ ...params, label: `${prefix}/${params.label}` }) } : {}),
|
|
1768
|
+
...(deps.runEffect ? { runEffect: params => deps.runEffect({ ...params, node: relabel(params.node) }) } : {}),
|
|
1769
|
+
...(deps.runJudge ? { runJudge: params => deps.runJudge({ ...params, label: `${prefix}/${params.label}` }) } : {}),
|
|
1770
|
+
...(recovery ? { recovery: {
|
|
1771
|
+
supportsExecutionPaths: recovery.supportsExecutionPaths,
|
|
1772
|
+
resume: (node, state, item, path) => recovery.resume(relabel(node), state, item, path),
|
|
1773
|
+
commit: (node, state, item, path) => recovery.commit(relabel(node), state, item, path),
|
|
1774
|
+
pollStartedAt: (node, item, path) => recovery.pollStartedAt(relabel(node), item, path),
|
|
1775
|
+
wait: (node, ms, item, path) => recovery.wait(relabel(node), ms, item, path),
|
|
1776
|
+
...(recovery.fail ? { fail: (node, results, error, path) => recovery.fail(relabel(node), results, error, path) } : {}),
|
|
1777
|
+
} } : {}),
|
|
1778
|
+
...(deps.onEvent ? { onEvent: event => deps.onEvent({ ...event, label: `${prefix}/${event.label}` }) } : {}),
|
|
1779
|
+
};
|
|
1780
|
+
}
|
|
1781
|
+
const STEP_KINDS = new Set(["agent", "decide", "extract", "report", "code", "call", "artifact", "workflow", "judge", "pick", "sift"]);
|
|
1782
|
+
const CHECKPOINTED_STRUCTURE_KINDS = new Set(["map", "parallel", "loop", "route"]);
|
|
1783
|
+
const guardedObservers = new WeakSet();
|
|
1784
|
+
async function runNodeOnState(node, state, workflow, deps) {
|
|
1785
|
+
deps = withExecutionLocation(deps);
|
|
1786
|
+
const observer = deps.onEvent;
|
|
1787
|
+
if (observer && !guardedObservers.has(observer)) {
|
|
1788
|
+
const guarded = event => {
|
|
1789
|
+
try {
|
|
1790
|
+
// TypeScript void callbacks can still return promises. Consume rejection without
|
|
1791
|
+
// awaiting telemetry or allowing it to replace an execution or recovery outcome.
|
|
1792
|
+
const returned = observer(event);
|
|
1793
|
+
if (returned && typeof returned.then === "function") {
|
|
1794
|
+
void Promise.resolve(returned).catch(() => { });
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
catch { /* Observation cannot control workflow delivery. */ }
|
|
1798
|
+
};
|
|
1799
|
+
guardedObservers.add(guarded);
|
|
1800
|
+
deps = { ...deps, onEvent: guarded };
|
|
1801
|
+
}
|
|
1802
|
+
if (deps.signal?.aborted)
|
|
1803
|
+
throw deps.signal.reason ?? new Error("workflow aborted");
|
|
1804
|
+
if (deps.recovery && node.node !== "chain") {
|
|
1805
|
+
const restored = await deps.recovery.resume(node, state, undefined, executionPath(deps));
|
|
1806
|
+
if (deps.signal?.aborted)
|
|
1807
|
+
throw deps.signal.reason ?? new Error("workflow aborted");
|
|
1808
|
+
if (restored !== undefined)
|
|
1809
|
+
return restored;
|
|
1810
|
+
}
|
|
1811
|
+
const label = node.label || node.node;
|
|
1812
|
+
const isStep = STEP_KINDS.has(node.node);
|
|
1813
|
+
if (isStep)
|
|
1814
|
+
deps.onEvent?.({ type: "node.start", label, detail: { kind: node.node } });
|
|
1815
|
+
const startedAt = Date.now();
|
|
1816
|
+
try {
|
|
1817
|
+
if (deps.signal?.aborted)
|
|
1818
|
+
throw deps.signal.reason ?? new Error("workflow aborted");
|
|
1819
|
+
const out = await runNodeBody(node, state, workflow, deps);
|
|
1820
|
+
if (deps.signal?.aborted)
|
|
1821
|
+
throw deps.signal.reason ?? new Error("workflow aborted");
|
|
1822
|
+
if (deps.recovery && node.node !== "chain")
|
|
1823
|
+
await deps.recovery.commit(node, out, undefined, executionPath(deps));
|
|
1824
|
+
if (deps.signal?.aborted)
|
|
1825
|
+
throw deps.signal.reason ?? new Error("workflow aborted");
|
|
1826
|
+
if ((isStep || CHECKPOINTED_STRUCTURE_KINDS.has(node.node)) && deps.checkpoint) {
|
|
1827
|
+
try {
|
|
1828
|
+
await deps.checkpoint(out, label, executionPath(deps));
|
|
1829
|
+
}
|
|
1830
|
+
catch (error) {
|
|
1831
|
+
deps.onEvent?.({ type: "checkpoint.failed", label, detail: { message: String(error?.message || error).slice(0, 300) } });
|
|
1832
|
+
if (deps.checkpointFailureMode === "required")
|
|
1833
|
+
throw error;
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1836
|
+
if (deps.signal?.aborted)
|
|
1837
|
+
throw deps.signal.reason ?? new Error("workflow aborted");
|
|
1838
|
+
if (isStep)
|
|
1839
|
+
deps.onEvent?.({ type: "node.end", label, detail: { kind: node.node, status: "ok", duration_ms: Date.now() - startedAt } });
|
|
1840
|
+
return out;
|
|
1841
|
+
}
|
|
1842
|
+
catch (error) {
|
|
1843
|
+
if (isStep)
|
|
1844
|
+
deps.onEvent?.({ type: "node.end", label, detail: { kind: node.node, status: error instanceof EscalationSignal ? "escalated" : "failed", duration_ms: Date.now() - startedAt, error: String(error?.message || error).slice(0, 300) } });
|
|
1845
|
+
throw error;
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
function uncertainEffects(error, found = new Set(), seen = new Set()) {
|
|
1849
|
+
if (seen.has(error))
|
|
1850
|
+
return found;
|
|
1851
|
+
seen.add(error);
|
|
1852
|
+
if (error instanceof EffectOutcomeUnknownError)
|
|
1853
|
+
found.add(error);
|
|
1854
|
+
else if (error instanceof AggregateError)
|
|
1855
|
+
for (const nested of error.errors)
|
|
1856
|
+
uncertainEffects(nested, found, seen);
|
|
1857
|
+
return found;
|
|
1858
|
+
}
|
|
1859
|
+
function withSiblingUncertainty(primary, uncertainties) {
|
|
1860
|
+
const retained = uncertainEffects(primary);
|
|
1861
|
+
const additional = [...uncertainties].filter(error => !retained.has(error));
|
|
1862
|
+
return additional.length
|
|
1863
|
+
? new AggregateError([primary, ...additional], "Workflow branches failed with uncertain effects; reconcile each effect before retrying", { cause: primary })
|
|
1864
|
+
: primary;
|
|
1865
|
+
}
|
|
1866
|
+
async function runNodeBody(node, state, workflow, deps) {
|
|
1867
|
+
switch (node.node) {
|
|
1868
|
+
case "chain": {
|
|
1869
|
+
let current = state;
|
|
1870
|
+
for (let i = 0; i < node.steps.length; i++)
|
|
1871
|
+
current = await runNodeOnState(node.steps[i], current, workflow, scopeExecution(deps, "steps", i));
|
|
1872
|
+
return current;
|
|
1873
|
+
}
|
|
1874
|
+
case "code": {
|
|
1875
|
+
let out;
|
|
1876
|
+
try {
|
|
1877
|
+
const fn = compileTransform(node.code);
|
|
1878
|
+
out = fn(state, { sop: deps.sop || "" });
|
|
1879
|
+
}
|
|
1880
|
+
catch (cause) {
|
|
1881
|
+
throw new WorkflowCodeError(node.label, cause);
|
|
1882
|
+
}
|
|
1883
|
+
const patch = node.as ? { [node.as]: out } : (out && typeof out === "object" && !Array.isArray(out) ? out : { [node.label]: out });
|
|
1884
|
+
deps.onEvent?.({ type: "code.patch", label: node.label, detail: patch });
|
|
1885
|
+
return { ...state, ...patch };
|
|
1886
|
+
}
|
|
1887
|
+
case "agent":
|
|
1888
|
+
case "decide":
|
|
1889
|
+
case "extract":
|
|
1890
|
+
case "report": {
|
|
1891
|
+
const stageSchema = stageSchemaForNode(workflow, node);
|
|
1892
|
+
const schema = stageSchema;
|
|
1893
|
+
const validator = Compile(schema);
|
|
1894
|
+
const { sopSections, sopSlice } = resolveSopSections(node, deps);
|
|
1895
|
+
if (deps.sop)
|
|
1896
|
+
deps.onEvent?.({ type: "sop.coverage", label: node.label, detail: { sections: sopSections.map((s) => ({ name: s, chars: sliceSop(deps.sop, s).length })), sliceChars: sopSlice.length, sopChars: deps.sop.length } });
|
|
1897
|
+
assertNodeInputs(node, state);
|
|
1898
|
+
const promptState = node.state ? interpolateValue(node.state, state, node.label) : promptStateOf(state);
|
|
1899
|
+
const verifier = node.node !== "report" && node.verify ? compileVerifier(workflow, node, state, deps) : null;
|
|
1900
|
+
let submission;
|
|
1901
|
+
submission = await deps.runNode({
|
|
1902
|
+
signal: deps.signal,
|
|
1903
|
+
kind: node.node, label: node.label,
|
|
1904
|
+
...(verifier ? { review: verifier.review } : {}),
|
|
1905
|
+
...(node.tier ? { tier: node.tier } : {}),
|
|
1906
|
+
system: [sopSlice, deps.skill, node.instructions].filter(Boolean),
|
|
1907
|
+
user: JSON.stringify(promptState),
|
|
1908
|
+
schema,
|
|
1909
|
+
...(node.effort ? { effort: node.effort } : {}),
|
|
1910
|
+
...(node.thinking ? { thinking: node.thinking } : {}),
|
|
1911
|
+
...(Array.isArray(node.tools) ? { tools: node.tools } : {}),
|
|
1912
|
+
});
|
|
1913
|
+
normalizeStringNullsForSchema(submission, schema);
|
|
1914
|
+
if (!validator.Check(submission)) {
|
|
1915
|
+
throw new WorkflowOutputInvalidError(`${node.node} node "${node.label}" submission does not satisfy schema "${node.node === "report" ? "report" : node.out}"`, schemaProblems(validator, submission), node.label);
|
|
1916
|
+
}
|
|
1917
|
+
const result = submission;
|
|
1918
|
+
const verified = verifier ? await verifier.finish(result) : null;
|
|
1919
|
+
const merged = mergeStageDelta(state, node, verified ? verified.submission : result, stageSchema);
|
|
1920
|
+
return verified ? { ...merged, [`${("as" in node && node.as) || node.label}$verify`]: verified.record } : merged;
|
|
1921
|
+
}
|
|
1922
|
+
case "map": {
|
|
1923
|
+
const items = getPath(state, node.itemsPath);
|
|
1924
|
+
if (!Array.isArray(items))
|
|
1925
|
+
throw new WorkflowStateError(`map node "${node.label}": ${node.itemsPath} is not a list`, node.label, node.itemsPath, "expected_list");
|
|
1926
|
+
const results = new Array(items.length);
|
|
1927
|
+
let cursor = 0;
|
|
1928
|
+
const width = Math.min(node.maxConcurrency ?? 4, Math.max(items.length, 1));
|
|
1929
|
+
const siblings = new AbortController();
|
|
1930
|
+
const signal = deps.signal ? AbortSignal.any([deps.signal, siblings.signal]) : siblings.signal;
|
|
1931
|
+
let failure;
|
|
1932
|
+
let failed = false;
|
|
1933
|
+
const uncertainties = new Set();
|
|
1934
|
+
await Promise.all(Array.from({ length: width }, async () => {
|
|
1935
|
+
while (!signal.aborted) {
|
|
1936
|
+
const i = cursor++;
|
|
1937
|
+
if (i >= items.length)
|
|
1938
|
+
return;
|
|
1939
|
+
// Item-local state is never a checkpoint: concurrent bodies would overwrite one
|
|
1940
|
+
// another with partial views. The map checkpoints its aggregate when it completes.
|
|
1941
|
+
// Under recovery each item is its own child: the body's session, effects and commit
|
|
1942
|
+
// carry the item, so a resumed map answers finished items from their rows and
|
|
1943
|
+
// continues the ones that were running.
|
|
1944
|
+
try {
|
|
1945
|
+
const itemState = snapshotState({ ...state, item: items[i], item_index: i });
|
|
1946
|
+
const out = await runNodeOnState(node.body, itemState, workflow, scopeDepsToItem(scopeExecution(deps, "items", i, "body"), { label: node.label, index: i }, signal));
|
|
1947
|
+
if (node.resultPath !== undefined) {
|
|
1948
|
+
const selected = node.resultPath.split(".").reduce((value, key) => value == null ? undefined : value[key], out);
|
|
1949
|
+
if (selected === undefined)
|
|
1950
|
+
throw new WorkflowStateError(`map node "${node.label}": resultPath "${node.resultPath}" is missing from item ${i}`, node.label, node.resultPath, "missing_map_result");
|
|
1951
|
+
results[i] = structuredClone(selected);
|
|
1952
|
+
}
|
|
1953
|
+
else
|
|
1954
|
+
results[i] = node.body.as ? out[node.body.as] : out;
|
|
1955
|
+
}
|
|
1956
|
+
catch (error) {
|
|
1957
|
+
uncertainEffects(error, uncertainties);
|
|
1958
|
+
if (!failed) {
|
|
1959
|
+
failed = true;
|
|
1960
|
+
failure = error;
|
|
1961
|
+
siblings.abort(error);
|
|
1962
|
+
}
|
|
1963
|
+
return;
|
|
1964
|
+
}
|
|
1965
|
+
}
|
|
1966
|
+
}));
|
|
1967
|
+
if (failed) {
|
|
1968
|
+
failure = withSiblingUncertainty(failure, uncertainties);
|
|
1969
|
+
const escalated = failure instanceof EscalationSignal;
|
|
1970
|
+
deps.onEvent?.({ type: escalated ? "map.escalated" : "map.failed", label: node.label, detail: { completed: results.reduce((n, r) => n + (r === undefined ? 0 : 1), 0), of: items.length, message: String(failure?.message || failure).slice(0, 300) } });
|
|
1971
|
+
if (!escalated && deps.recovery?.fail) {
|
|
1972
|
+
try {
|
|
1973
|
+
await deps.recovery.fail(node, results, failure, executionPath(deps));
|
|
1974
|
+
}
|
|
1975
|
+
catch (persistenceError) {
|
|
1976
|
+
throw new AggregateError([failure, persistenceError], `map node "${node.label}" failed and its partial result could not be persisted`, { cause: failure });
|
|
1977
|
+
}
|
|
1978
|
+
}
|
|
1979
|
+
throw failure;
|
|
1980
|
+
}
|
|
1981
|
+
return { ...state, [node.as]: results };
|
|
1982
|
+
}
|
|
1983
|
+
case "parallel": {
|
|
1984
|
+
// Every branch starts from the same state and runs at once; what comes back is each branch's
|
|
1985
|
+
// PATCH (the keys whose value it changed), merged in branch order. Branch-local state is never
|
|
1986
|
+
// a checkpoint (a partial view would overwrite a sibling's); the node checkpoints the merge.
|
|
1987
|
+
const baseline = snapshotState(state);
|
|
1988
|
+
const inputs = (node.branches || []).map(() => snapshotState(baseline));
|
|
1989
|
+
const siblings = new AbortController();
|
|
1990
|
+
const signal = deps.signal ? AbortSignal.any([deps.signal, siblings.signal]) : siblings.signal;
|
|
1991
|
+
let failure;
|
|
1992
|
+
let failed = false;
|
|
1993
|
+
const uncertainties = new Set();
|
|
1994
|
+
const outs = await Promise.all((node.branches || []).map(async (branch, i) => {
|
|
1995
|
+
try {
|
|
1996
|
+
return await runNodeOnState(branch, inputs[i], workflow, scopeExecution({ ...deps, signal, checkpoint: undefined }, "branches", i));
|
|
1997
|
+
}
|
|
1998
|
+
catch (error) {
|
|
1999
|
+
uncertainEffects(error, uncertainties);
|
|
2000
|
+
if (!failed) {
|
|
2001
|
+
failed = true;
|
|
2002
|
+
failure = error;
|
|
2003
|
+
siblings.abort(error);
|
|
2004
|
+
}
|
|
2005
|
+
return inputs[i];
|
|
2006
|
+
}
|
|
2007
|
+
}));
|
|
2008
|
+
if (failed)
|
|
2009
|
+
throw withSiblingUncertainty(failure, uncertainties);
|
|
2010
|
+
const merged = { ...state };
|
|
2011
|
+
const writer = new Map();
|
|
2012
|
+
outs.forEach((out, i) => {
|
|
2013
|
+
for (const [key, value] of Object.entries((out || {}))) {
|
|
2014
|
+
if (Object.hasOwn(baseline, key) && isDeepStrictEqual(value, baseline[key]))
|
|
2015
|
+
continue;
|
|
2016
|
+
const prior = writer.get(key);
|
|
2017
|
+
if (prior !== undefined)
|
|
2018
|
+
throw new WorkflowStateError(`parallel node "${node.label}": branches ${prior} and ${i} both wrote state.${key} — branches must write disjoint keys`, node.label, key, "parallel_write_conflict");
|
|
2019
|
+
writer.set(key, i);
|
|
2020
|
+
Object.defineProperty(merged, key, { value, enumerable: true, configurable: true, writable: true });
|
|
2021
|
+
}
|
|
2022
|
+
});
|
|
2023
|
+
return merged;
|
|
2024
|
+
}
|
|
2025
|
+
case "loop": {
|
|
2026
|
+
let current = state;
|
|
2027
|
+
for (let i = 0; i < node.maxIters; i += 1) {
|
|
2028
|
+
const iterationDeps = scopeExecution(deps, "iterations", i);
|
|
2029
|
+
current = await runNodeOnState(node.body, current, workflow, scopeExecution(iterationDeps, "body"));
|
|
2030
|
+
if ((await evaluatePredicate(node.until, current, withExecutionLocation(iterationDeps), { label: node.label, kind: "loop" })).holds) {
|
|
2031
|
+
deps.onEvent?.({ type: "loop.exited", label: node.label, detail: { reason: "condition_met", iterations: i + 1 } });
|
|
2032
|
+
return current;
|
|
2033
|
+
}
|
|
2034
|
+
}
|
|
2035
|
+
deps.onEvent?.({ type: "loop.exited", label: node.label, detail: { reason: "bound_reached", iterations: node.maxIters } });
|
|
2036
|
+
return current; // A following node decides what reaching the bound means for this workflow.
|
|
2037
|
+
}
|
|
2038
|
+
case "judge": {
|
|
2039
|
+
return runJudgeNode(node, state, workflow, deps);
|
|
2040
|
+
}
|
|
2041
|
+
case "pick": {
|
|
2042
|
+
return runPickNode(node, state, deps);
|
|
2043
|
+
}
|
|
2044
|
+
case "sift": {
|
|
2045
|
+
return runSiftNode(node, state, workflow, deps);
|
|
2046
|
+
}
|
|
2047
|
+
case "route": {
|
|
2048
|
+
return runRouteNode(node, state, workflow, deps);
|
|
2049
|
+
}
|
|
2050
|
+
case "artifact": {
|
|
2051
|
+
assertNodeInputs(node, state);
|
|
2052
|
+
const type = node.type;
|
|
2053
|
+
const artifact = { path: String(node.path), filename: String(node.path).split("/").pop(), type };
|
|
2054
|
+
deps.onEvent?.({ type: "artifact.declared", label: node.label, detail: artifact });
|
|
2055
|
+
return { ...state, artifact };
|
|
2056
|
+
}
|
|
2057
|
+
case "call": {
|
|
2058
|
+
return runCallNode(node, state, workflow, deps);
|
|
2059
|
+
}
|
|
2060
|
+
case "workflow": {
|
|
2061
|
+
const input = structuredClone(interpolateValue(node.input, state, node.label));
|
|
2062
|
+
const child = node.workflow;
|
|
2063
|
+
const inputValidator = Compile(resolveSchemaForWorkflow(child, child.schemas[child.input.schemaId]));
|
|
2064
|
+
if (!inputValidator.Check(input))
|
|
2065
|
+
throw new WorkflowOutputInvalidError(`workflow "${node.label}" input does not satisfy the child's schema "${child.input.schemaId}"`, schemaProblems(inputValidator, input), node.label);
|
|
2066
|
+
const result = await runWorkflow(child, input, scopeDepsToChild(deps, node));
|
|
2067
|
+
if (result.status === "escalated") {
|
|
2068
|
+
const inner = result.escalation;
|
|
2069
|
+
throw new EscalationSignal({ kind: inner.kind, stage: inner.stage, summary: inner.summary, state, label: `${node.label}/${inner.label ?? inner.stage}`, executionPath: inner.executionPath });
|
|
2070
|
+
}
|
|
2071
|
+
const validator = Compile(resolveSchemaForWorkflow(workflow, workflow.schemas[node.out]));
|
|
2072
|
+
if (!validator.Check(result.output))
|
|
2073
|
+
throw new WorkflowOutputInvalidError(`workflow "${node.label}" returned a value that does not satisfy the parent's schema "${node.out}"`, schemaProblems(validator, result.output), node.label);
|
|
2074
|
+
return { ...state, [node.as]: structuredClone(result.output) };
|
|
2075
|
+
}
|
|
2076
|
+
case "escalate": {
|
|
2077
|
+
const evaluated = await evaluatePredicate(node.when, state, deps, { label: node.label, kind: "escalate" });
|
|
2078
|
+
const fired = evaluated.holds;
|
|
2079
|
+
const pathValue = node.when?.path
|
|
2080
|
+
? String(node.when.path).split(".").reduce((acc, key) => (acc == null ? acc : acc[key]), state)
|
|
2081
|
+
: undefined;
|
|
2082
|
+
deps.onEvent?.({ type: "escalate.evaluated", label: node.label, detail: { fired, path: node.when?.path ?? null, path_resolved: pathValue !== undefined, ...(evaluated.detail ?? {}) } });
|
|
2083
|
+
if (fired) {
|
|
2084
|
+
throw new EscalationSignal({ kind: node.kind, stage: node.stage, summary: interpolate(node.summary, state), state, label: node.label, executionPath: executionPath(deps) });
|
|
2085
|
+
}
|
|
2086
|
+
return state;
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
}
|
|
2090
|
+
function sliceSop(sop, section) {
|
|
2091
|
+
const heading = sop.split(/\r?\n/).find(line => line.trimEnd() === `## ${section}`);
|
|
2092
|
+
const start = heading ? sop.indexOf(heading) : -1;
|
|
2093
|
+
if (start < 0)
|
|
2094
|
+
return "";
|
|
2095
|
+
const rest = sop.slice(start + 3);
|
|
2096
|
+
const boundary = /^ROLE\s*:/i.test(section) ? /\n## (?:ROLE\s*:|APPENDIX)/i : /\n## /;
|
|
2097
|
+
const next = rest.search(boundary);
|
|
2098
|
+
return sop.slice(start, next < 0 ? undefined : start + 3 + next);
|
|
2099
|
+
}
|
|
2100
|
+
export class WorkflowVerificationError extends Error {
|
|
2101
|
+
stage;
|
|
2102
|
+
candidate;
|
|
2103
|
+
drives;
|
|
2104
|
+
code = "WORKFLOW_VERIFICATION_FAILED";
|
|
2105
|
+
constructor(stage, candidate, drives, message) {
|
|
2106
|
+
super(`Verification failed at "${stage}": ${message}`);
|
|
2107
|
+
this.stage = stage;
|
|
2108
|
+
this.candidate = candidate;
|
|
2109
|
+
this.drives = drives;
|
|
2110
|
+
this.name = "WorkflowVerificationError";
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
function compileVerifier(workflow, node, state, deps) {
|
|
2114
|
+
const clause = node.verify;
|
|
2115
|
+
const compiled = compileQuestions(workflow.schemas[clause.out], workflow.schemas);
|
|
2116
|
+
if (compiled.ok === false)
|
|
2117
|
+
throw new Error(`${node.node} node "${node.label}": verify.out "${clause.out}": ${compiled.errors.join("; ")}`);
|
|
2118
|
+
const questions = compiled.questions;
|
|
2119
|
+
const schema = workflow.schemas[clause.out].properties ?? {};
|
|
2120
|
+
const maxDrives = clause.maxDrives ?? 2;
|
|
2121
|
+
const floor = clause.override?.below ?? 0.3;
|
|
2122
|
+
const runJudge = requireJudge(deps, "judge", `${node.label} (verify)`);
|
|
2123
|
+
const drives = [];
|
|
2124
|
+
let reviewedCandidate;
|
|
2125
|
+
let last = null;
|
|
2126
|
+
const pYes = (a) => !a ? 0 : a.type === "noul" ? a.noul : a.type === "choice" ? (a.choice === "true" ? 1 : 0) : 0;
|
|
2127
|
+
const PLACEHOLDER = /^(none|null|nil|n\/a|na|unknown|not published|not stated|not available|not found|not applicable|-)$/i;
|
|
2128
|
+
const filled = (v) => !(v === null || v === undefined || v === false || (typeof v === "string" && (v.trim() === "" || PLACEHOLDER.test(v.trim()))) || (Array.isArray(v) && v.length === 0));
|
|
2129
|
+
const outSchema = workflow.schemas[node.out ?? ""];
|
|
2130
|
+
const review = async (candidate) => {
|
|
2131
|
+
if (drives.length >= maxDrives)
|
|
2132
|
+
throw new WorkflowVerificationError(node.label, candidate, drives, `exhausted ${maxDrives} review attempts`);
|
|
2133
|
+
const sub = (candidate && typeof candidate === "object" && !Array.isArray(candidate)) ? { ...candidate } : {};
|
|
2134
|
+
if (outSchema)
|
|
2135
|
+
normalizeStringNullsForSchema(sub, outSchema);
|
|
2136
|
+
const asked = interpolateValue({ ...(clause.state ?? {}), submission: "{submission}" }, { ...state, submission: sub, ...(node.as ? { [node.as]: sub } : {}) }, node.label);
|
|
2137
|
+
const result = await runJudge({ label: `${node.label} (verify)`, kind: "judge", state: asked, questions, signal: deps.signal });
|
|
2138
|
+
validateAnswers(questions, result.answers);
|
|
2139
|
+
const answers = result.answers;
|
|
2140
|
+
const doubted = [];
|
|
2141
|
+
const unmet = [];
|
|
2142
|
+
for (const [id, q] of Object.entries(questions)) {
|
|
2143
|
+
if (q.type !== "noul")
|
|
2144
|
+
continue;
|
|
2145
|
+
const p = pYes(answers[id]);
|
|
2146
|
+
if (Object.prototype.hasOwnProperty.call(sub, id)) {
|
|
2147
|
+
if (filled(sub[id]) && p < floor)
|
|
2148
|
+
doubted.push(id);
|
|
2149
|
+
}
|
|
2150
|
+
else if (p < 0.5)
|
|
2151
|
+
unmet.push(id);
|
|
2152
|
+
}
|
|
2153
|
+
const drive = drives.length + 1;
|
|
2154
|
+
const accepted = doubted.length === 0 && unmet.length === 0;
|
|
2155
|
+
reviewedCandidate = JSON.stringify(sub);
|
|
2156
|
+
const sidecar = answersSidecar(answers);
|
|
2157
|
+
drives.push({ drive, doubted, unmet, accepted, sidecar });
|
|
2158
|
+
last = { answers, doubted, unmet };
|
|
2159
|
+
deps.onEvent?.({ type: "verify.answered", label: node.label, detail: { drive, doubted, unmet, accepted, sidecar, model: result.model ?? null, cost_usd: result.cost_usd ?? null } });
|
|
2160
|
+
if (accepted)
|
|
2161
|
+
return { accepted: true };
|
|
2162
|
+
if (drive >= maxDrives)
|
|
2163
|
+
throw new WorkflowVerificationError(node.label, sub, drives, `exhausted ${maxDrives} review attempts; unmet: ${unmet.join(", ") || "none"}; doubted: ${doubted.join(", ") || "none"}`);
|
|
2164
|
+
const line = (id) => `${id}: ${String(schema[id]?.description ?? questions[id].instructions).replace(/\s+/g, " ").trim()}`;
|
|
2165
|
+
const message = [
|
|
2166
|
+
doubted.length ? `Not supported by your evidence (remove it, or add the packet that prints it): ${doubted.map(line).join(" | ")}.` : "",
|
|
2167
|
+
unmet.length ? `Still required: ${unmet.map(line).join(" | ")}.` : "",
|
|
2168
|
+
].filter(Boolean).join(" ");
|
|
2169
|
+
return { accepted: false, message };
|
|
2170
|
+
};
|
|
2171
|
+
const finish = async (submission) => {
|
|
2172
|
+
if (reviewedCandidate !== JSON.stringify(submission) || !drives.at(-1)?.accepted) {
|
|
2173
|
+
const verdict = await review(submission);
|
|
2174
|
+
if (!verdict.accepted)
|
|
2175
|
+
throw new WorkflowVerificationError(node.label, submission, drives, verdict.message);
|
|
2176
|
+
}
|
|
2177
|
+
return { submission, record: { drives, answers: last?.answers ?? {} } };
|
|
2178
|
+
};
|
|
2179
|
+
return { review, finish };
|
|
2180
|
+
}
|
|
2181
|
+
function resolveSopSections(node, deps) {
|
|
2182
|
+
const sopSections = Array.isArray(node.sopSection) ? node.sopSection : node.sopSection ? [node.sopSection] : [];
|
|
2183
|
+
if (sopSections.length && !deps.sop)
|
|
2184
|
+
throw new Error(`${node.node} node "${node.label}": sopSection requires SOP text in deps.sop`);
|
|
2185
|
+
const slices = sopSections.map(name => ({ name, text: sliceSop(deps.sop, name) }));
|
|
2186
|
+
const missing = slices.filter(section => !section.text).map(section => section.name);
|
|
2187
|
+
if (missing.length)
|
|
2188
|
+
throw new Error(`${node.node} node "${node.label}": sopSection ${missing.map(name => `"${name}"`).join(", ")} matches no "## <section>" heading in the SOP`);
|
|
2189
|
+
return { sopSections, sopSlice: slices.map(section => section.text).join("\n\n") };
|
|
2190
|
+
}
|
|
2191
|
+
export function assertWorkflowCapabilities(node, deps, workflow) {
|
|
2192
|
+
assertQuestionCount(deps, 0, "workflow");
|
|
2193
|
+
if (workflow) {
|
|
2194
|
+
const schemaId = node.node === "judge" || node.node === "sift" ? node.out : "verify" in node ? node.verify?.out : undefined;
|
|
2195
|
+
if (schemaId) {
|
|
2196
|
+
const compiled = compileQuestions(workflow.schemas[schemaId], workflow.schemas);
|
|
2197
|
+
if (compiled.ok)
|
|
2198
|
+
assertQuestionCount(deps, Object.keys(compiled.questions).length, "label" in node ? node.label : node.node);
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
if ("sopSection" in node)
|
|
2202
|
+
resolveSopSections(node, deps);
|
|
2203
|
+
const missing = (capability) => { throw new Error(`${node.node} node "${"label" in node ? node.label : node.node}" requires ${capability}`); };
|
|
2204
|
+
if ((["agent", "decide", "extract", "report"].includes(node.node) || node.node === "artifact" && artifactNodeIsProse(node)) && !deps.runNode)
|
|
2205
|
+
missing("runNode");
|
|
2206
|
+
if (node.node === "call" && !deps.runEffect)
|
|
2207
|
+
missing("runEffect");
|
|
2208
|
+
if ((["judge", "pick", "sift", "route"].includes(node.node) || "verify" in node && node.verify ||
|
|
2209
|
+
node.node === "loop" && node.until?.predicate === "ask" ||
|
|
2210
|
+
node.node === "escalate" && node.when?.predicate === "ask") && !deps.runJudge)
|
|
2211
|
+
missing("runJudge");
|
|
2212
|
+
if (node.node === "chain")
|
|
2213
|
+
node.steps.forEach(n => assertWorkflowCapabilities(n, deps, workflow));
|
|
2214
|
+
if (node.node === "parallel")
|
|
2215
|
+
node.branches.forEach(n => assertWorkflowCapabilities(n, deps, workflow));
|
|
2216
|
+
if (node.node === "map" || node.node === "loop")
|
|
2217
|
+
assertWorkflowCapabilities(node.body, deps, workflow);
|
|
2218
|
+
if (node.node === "route")
|
|
2219
|
+
Object.values(node.branches).forEach(b => assertWorkflowCapabilities(b.body, deps, workflow));
|
|
2220
|
+
if (node.node === "workflow")
|
|
2221
|
+
assertWorkflowCapabilities(desugarWorkflow(node.workflow).root, deps, node.workflow);
|
|
2222
|
+
}
|
|
2223
|
+
export async function runWorkflow(workflow, input, deps) {
|
|
2224
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
2225
|
+
throw new WorkflowInputInvalidError(String(workflow?.name), ["input must be a JSON object"]);
|
|
2226
|
+
const valid = validateWorkflow(workflow);
|
|
2227
|
+
if (!valid.ok)
|
|
2228
|
+
throw new WorkflowInvalidError(String(workflow?.name), valid.errors);
|
|
2229
|
+
workflow = desugarWorkflow(workflow);
|
|
2230
|
+
if (workflow.input) {
|
|
2231
|
+
const validator = Compile(resolveSchemaForWorkflow(workflow, workflow.schemas[workflow.input.schemaId]));
|
|
2232
|
+
if (!validator.Check(input))
|
|
2233
|
+
throw new WorkflowInputInvalidError(workflow.name, schemaProblems(validator, input));
|
|
2234
|
+
}
|
|
2235
|
+
const reachable = validateWorkflow(workflow, { inputKeys: Object.keys(input) });
|
|
2236
|
+
if (!reachable.ok)
|
|
2237
|
+
throw new WorkflowInvalidError(workflow.name, reachable.errors);
|
|
2238
|
+
assertWorkflowCapabilities(workflow.root, deps, workflow);
|
|
2239
|
+
if (needsComposedRecovery(workflow.root))
|
|
2240
|
+
deps = { ...deps, [SCOPED_EFFECTS]: true };
|
|
2241
|
+
if (deps.recovery && !deps.recovery.supportsExecutionPaths && needsComposedRecovery(workflow.root))
|
|
2242
|
+
throw new Error("Composed child workflows require recovery.supportsExecutionPaths: true and stores keyed by executionPath");
|
|
2243
|
+
try {
|
|
2244
|
+
const state = await runNodeOnState(workflow.root, snapshotState(input), workflow, deps);
|
|
2245
|
+
const output = workflow.output.path ? getPath(state, workflow.output.path) : state;
|
|
2246
|
+
const validator = Compile(resolveSchemaForWorkflow(workflow, workflow.schemas[workflow.output.schemaId]));
|
|
2247
|
+
if (!validator.Check(output)) {
|
|
2248
|
+
throw new WorkflowOutputInvalidError(`workflow "${workflow.name}" output does not satisfy schema "${workflow.output.schemaId}"`, schemaProblems(validator, output));
|
|
2249
|
+
}
|
|
2250
|
+
return { status: "complete", state, output };
|
|
2251
|
+
}
|
|
2252
|
+
catch (error) {
|
|
2253
|
+
if (error instanceof EscalationSignal)
|
|
2254
|
+
return { status: "escalated", state: error.escalation.state, escalation: error.escalation };
|
|
2255
|
+
throw error;
|
|
2256
|
+
}
|
|
2257
|
+
}
|
|
2258
|
+
export async function runWorkflowSlice(workflow, input, focus, deps) {
|
|
2259
|
+
const valid = validateWorkflow(workflow);
|
|
2260
|
+
if (!valid.ok)
|
|
2261
|
+
throw new WorkflowInvalidError(String(workflow?.name), valid.errors);
|
|
2262
|
+
const desugared = desugarWorkflow(workflow);
|
|
2263
|
+
const root = desugared.root.node === "chain" ? desugared.root.steps : [desugared.root];
|
|
2264
|
+
const labels = root.map((n) => n.label ?? "");
|
|
2265
|
+
const find = (want) => { const exact = labels.indexOf(want); return exact >= 0 ? exact : labels.findIndex((l) => l.startsWith(want)); };
|
|
2266
|
+
const a = find(focus.from);
|
|
2267
|
+
if (a < 0)
|
|
2268
|
+
throw new Error(`workflow slice: "${focus.from}" names no root step of "${desugared.name}"`);
|
|
2269
|
+
const b = focus.to ? find(focus.to) : a;
|
|
2270
|
+
if (b < 0)
|
|
2271
|
+
throw new Error(`workflow slice: "${focus.to}" names no root step of "${desugared.name}"`);
|
|
2272
|
+
if (b < a)
|
|
2273
|
+
throw new Error(`workflow slice: "${focus.to}" comes before "${focus.from}"`);
|
|
2274
|
+
const slice = { node: "chain", steps: root.slice(a, b + 1) };
|
|
2275
|
+
const state = snapshotState({ ...input, ...(focus.seed ?? {}) });
|
|
2276
|
+
assertWorkflowCapabilities(slice, deps, desugared);
|
|
2277
|
+
if (needsComposedRecovery(desugared.root))
|
|
2278
|
+
deps = { ...deps, [SCOPED_EFFECTS]: true };
|
|
2279
|
+
if (deps.recovery && !deps.recovery.supportsExecutionPaths && needsComposedRecovery(slice))
|
|
2280
|
+
throw new Error("Composed child workflows require recovery.supportsExecutionPaths: true and stores keyed by executionPath");
|
|
2281
|
+
try {
|
|
2282
|
+
// A slice keeps the original document positions: effects must have the same identity as
|
|
2283
|
+
// the corresponding steps of the complete workflow, never renumbered from zero.
|
|
2284
|
+
let out = state;
|
|
2285
|
+
for (let i = a; i <= b; i++)
|
|
2286
|
+
out = await runNodeOnState(root[i], out, desugared, desugared.root.node === "chain" ? scopeExecution(deps, "steps", i) : deps);
|
|
2287
|
+
return { status: "complete", state: out, focus: { from: labels[a], to: labels[b] } };
|
|
2288
|
+
}
|
|
2289
|
+
catch (error) {
|
|
2290
|
+
if (error instanceof EscalationSignal)
|
|
2291
|
+
return { status: "escalated", state: error.escalation.state, escalation: error.escalation };
|
|
2292
|
+
throw error;
|
|
2293
|
+
}
|
|
2294
|
+
}
|