@parcha/agentrun-dsl 0.1.0-beta.3 → 0.1.0-beta.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +165 -21
- package/dist/author-references.d.ts +8 -0
- package/dist/author-references.js +8 -0
- package/dist/author.d.ts +16 -3
- package/dist/author.js +40 -16
- package/dist/authoring.js +2 -2
- package/dist/cli.js +17 -12
- package/dist/code-exec.d.ts +6 -0
- package/dist/code-exec.js +126 -0
- package/dist/decision-provider.d.ts +157 -0
- package/dist/decision-provider.js +230 -0
- package/dist/decision-receipts.d.ts +143 -0
- package/dist/decision-receipts.js +93 -0
- package/dist/dry-run.d.ts +14 -1
- package/dist/dry-run.js +58 -15
- package/dist/graph.d.ts +169 -0
- package/dist/graph.js +433 -0
- package/dist/index.d.ts +15 -8
- package/dist/index.js +8 -4
- package/dist/inspection.d.ts +5 -0
- package/dist/inspection.js +16 -3
- package/dist/job.d.ts +14 -0
- package/dist/job.js +5 -0
- package/dist/long-timer.d.ts +7 -0
- package/dist/long-timer.js +22 -0
- package/dist/observer-snapshot.d.ts +3 -0
- package/dist/observer-snapshot.js +46 -0
- package/dist/predicates.d.ts +10 -0
- package/dist/predicates.js +18 -0
- package/dist/recovery/binding.d.ts +10 -0
- package/dist/recovery/binding.js +43 -0
- package/dist/recovery/call-key.d.ts +13 -0
- package/dist/recovery/call-key.js +24 -0
- package/dist/recovery/canonical-hash.d.ts +7 -0
- package/dist/recovery/canonical-hash.js +22 -0
- package/dist/recovery/driver.d.ts +238 -0
- package/dist/recovery/driver.js +1337 -0
- package/dist/recovery/effect-failure.d.ts +34 -0
- package/dist/recovery/effect-failure.js +62 -0
- package/dist/recovery/errors.d.ts +31 -0
- package/dist/recovery/errors.js +51 -0
- package/dist/recovery/execution-path.d.ts +53 -0
- package/dist/recovery/execution-path.js +167 -0
- package/dist/recovery/file-store.d.ts +7 -0
- package/dist/recovery/file-store.js +131 -0
- package/dist/recovery/frozen-snapshot.d.ts +274 -0
- package/dist/recovery/frozen-snapshot.js +187 -0
- package/dist/recovery/handoff-block.d.ts +36 -0
- package/dist/recovery/handoff-block.js +148 -0
- package/dist/recovery/handoff-digest.d.ts +58 -0
- package/dist/recovery/handoff-digest.js +195 -0
- package/dist/recovery/handoff-files.d.ts +54 -0
- package/dist/recovery/handoff-files.js +162 -0
- package/dist/recovery/handoff-receipts.d.ts +42 -0
- package/dist/recovery/handoff-receipts.js +53 -0
- package/dist/recovery/handoff-redact.d.ts +12 -0
- package/dist/recovery/handoff-redact.js +44 -0
- package/dist/recovery/handoff.d.ts +44 -0
- package/dist/recovery/handoff.js +46 -0
- package/dist/recovery/index.d.ts +19 -0
- package/dist/recovery/index.js +17 -0
- package/dist/recovery/item-state.d.ts +48 -0
- package/dist/recovery/item-state.js +89 -0
- package/dist/recovery/journal.d.ts +28 -0
- package/dist/recovery/journal.js +139 -0
- package/dist/recovery/memory-store.d.ts +4 -0
- package/dist/recovery/memory-store.js +20 -0
- package/dist/recovery/store.d.ts +105 -0
- package/dist/recovery/store.js +6 -0
- package/dist/recovery/testing/conformance.d.ts +4 -0
- package/dist/recovery/testing/conformance.js +268 -0
- package/dist/recovery/testing/crash-points.d.ts +18 -0
- package/dist/recovery/testing/crash-points.js +43 -0
- package/dist/recovery/testing/crash-sweep.d.ts +65 -0
- package/dist/recovery/testing/crash-sweep.js +146 -0
- package/dist/recovery/testing/index.d.ts +3 -0
- package/dist/recovery/testing/index.js +4 -0
- package/dist/recovery/testing/preload.d.ts +1 -0
- package/dist/recovery/testing/preload.js +3 -0
- package/dist/recovery/transport-facts.d.ts +23 -0
- package/dist/recovery/transport-facts.js +46 -0
- package/dist/recovery/with-recovery.d.ts +50 -0
- package/dist/recovery/with-recovery.js +142 -0
- package/dist/submit-envelope.d.ts +9 -0
- package/dist/submit-envelope.js +40 -0
- package/dist/system-one.d.ts +40 -1
- package/dist/system-one.js +141 -5
- package/dist/testing/decision-provider.d.ts +32 -0
- package/dist/testing/decision-provider.js +183 -0
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing/index.js +2 -0
- package/dist/vocabulary.d.ts +22 -3
- package/dist/vocabulary.js +33 -10
- package/dist/workflow-shape.d.ts +7 -0
- package/dist/workflow-shape.js +115 -67
- package/dist/workflow.d.ts +269 -20
- package/dist/workflow.js +1251 -341
- package/package.json +25 -2
- package/schema/workflow.schema.json +506 -12
- package/skills/author/SKILL.md +6 -1
- package/skills/author/references/jev-decisions.md +58 -6
- package/skills/author/references/language.md +28 -19
- package/skills/author/references/sdk-authoring.md +2 -0
package/dist/workflow.js
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
|
+
import { observerSnapshot } from "./observer-snapshot.js";
|
|
1
2
|
import { createHash } from "node:crypto";
|
|
2
3
|
import { isDeepStrictEqual, types as utilTypes } from "node:util";
|
|
3
4
|
import { predicateMatches, getPath, MECHANICAL_PREDICATE_NAMES } from "./predicates.js";
|
|
4
|
-
import { WORKFLOW_NODE_KINDS, NODE_FIELDS, IGNORED_NODE_FIELDS, WORKFLOW_PREDICATES, THINKING_LEVELS, MODEL_TIERS, CALL_TRANSPORTS, CALL_RETRY_CLASSES, PROSE_ARTIFACT_TYPES } from "./vocabulary.js";
|
|
5
|
-
import { validateAnswers, answerConfidence, answersSidecar, answersToValue, compileQuestions, SYSTEM_ONE_LIMITS } from "./system-one.js";
|
|
5
|
+
import { DECISION_NODE_FIELDS, DECISION_TIMEOUT_MAX_MS, WORKFLOW_NODE_KINDS, NODE_FIELDS, HOST_NODE_FIELDS, IGNORED_NODE_FIELDS, WORKFLOW_PREDICATES, FIELD_TYPES, THINKING_LEVELS, MODEL_TIERS, AGENT_CONTEXTS, CALL_TRANSPORTS, CALL_RETRY_CLASSES, EFFECT_FAILURE_CODES, PROSE_ARTIFACT_TYPES } from "./vocabulary.js";
|
|
6
|
+
import { validateAnswers, answerConfidence, answersSidecar, answersToValue, compileQuestions, refusedQuestions, SYSTEM_ONE_LIMITS } from "./system-one.js";
|
|
7
|
+
import { chargedCost, DecisionProviderError } from "./decision-provider.js";
|
|
8
|
+
import { DECISION_ERROR_MAX, DECISION_RECEIPT_VERSION, DECISION_INTERRUPTED, DECISION_REFUSED, DECISION_STATE_MAX_BYTES } from "./decision-receipts.js";
|
|
6
9
|
import { Compile } from "typebox/compile";
|
|
7
10
|
import { compileTransform, compileTransformSyntax } from "./code-exec.js";
|
|
8
|
-
import {
|
|
11
|
+
import { workflowShapeIssues } from "./workflow-shape.js";
|
|
9
12
|
import { resolveSchemaForWorkflow } from "./schema-references.js";
|
|
10
13
|
export { resolveSchemaForWorkflow } from "./schema-references.js";
|
|
14
|
+
import { setLongTimeout } from "./long-timer.js";
|
|
15
|
+
import { isRecoveryError } from "./recovery/errors.js";
|
|
16
|
+
/** A request's error as its receipt keeps it: whole up to DECISION_ERROR_MAX characters, a longer one cut there and
|
|
17
|
+
* marked, so a host redacts it before it cuts it and holds a cut one's tail back (decision-receipts.ts). */
|
|
18
|
+
const errorOfReceipt = (text) => text.length > DECISION_ERROR_MAX ? { error: text.slice(0, DECISION_ERROR_MAX), error_truncated: true } : { error: text };
|
|
19
|
+
export const routesByValue = (node) => typeof node.valuePath === "string";
|
|
11
20
|
export function artifactNodeIsProse(node) {
|
|
12
21
|
return PROSE_ARTIFACT_TYPES.includes(node.type);
|
|
13
22
|
}
|
|
@@ -60,10 +69,17 @@ export const declaredWrites = (node) => {
|
|
|
60
69
|
return;
|
|
61
70
|
if (n.node === "report")
|
|
62
71
|
out.add("report_markdown");
|
|
63
|
-
|
|
72
|
+
// The key a generative node writes under is `as || label`, the one the interpreter uses (mergeStageDelta); a code
|
|
73
|
+
// node's patch keys are known only when it runs. The validator rejects a blank `as` or label, so the key is non-blank.
|
|
74
|
+
else if (typeof n.as === "string" && n.as !== "")
|
|
64
75
|
out.add(n.as);
|
|
65
|
-
if (n.node === "
|
|
76
|
+
else if ((n.node === "agent" || n.node === "decide" || n.node === "extract") && typeof n.label === "string" && n.label !== "")
|
|
77
|
+
out.add(n.label);
|
|
78
|
+
if (n.node === "map") {
|
|
79
|
+
if (typeof n.failedAs === "string" && n.failedAs.trim())
|
|
80
|
+
out.add(n.failedAs);
|
|
66
81
|
return;
|
|
82
|
+
}
|
|
67
83
|
if (Array.isArray(n.steps))
|
|
68
84
|
n.steps.forEach(visit);
|
|
69
85
|
if (Array.isArray(n.branches))
|
|
@@ -89,10 +105,12 @@ const loopBodyWrites = (body) => {
|
|
|
89
105
|
}
|
|
90
106
|
if (n.node === "report")
|
|
91
107
|
out.add("report_markdown");
|
|
92
|
-
else if (typeof n.as === "string" && n.as
|
|
108
|
+
else if (typeof n.as === "string" && n.as !== "")
|
|
93
109
|
out.add(n.as);
|
|
94
|
-
else if (["agent", "decide", "extract"].includes(n.node) && typeof n.label === "string")
|
|
110
|
+
else if (["agent", "decide", "extract"].includes(n.node) && typeof n.label === "string" && n.label !== "")
|
|
95
111
|
out.add(n.label);
|
|
112
|
+
if (n.node === "map" && typeof n.failedAs === "string" && n.failedAs.trim())
|
|
113
|
+
out.add(n.failedAs);
|
|
96
114
|
if (n.node === "map" || n.node === "workflow")
|
|
97
115
|
return;
|
|
98
116
|
if (Array.isArray(n.steps))
|
|
@@ -123,19 +141,28 @@ const snapshotState = (state) => {
|
|
|
123
141
|
const KINDS = new Set(WORKFLOW_NODE_KINDS);
|
|
124
142
|
const kindsMatchType = true;
|
|
125
143
|
void kindsMatchType;
|
|
126
|
-
const KIND_KEYS = Object.fromEntries(WORKFLOW_NODE_KINDS.map((kind) => [kind, new Set([...NODE_FIELDS[kind], ...(IGNORED_NODE_FIELDS[kind] ?? [])])]));
|
|
144
|
+
const KIND_KEYS = Object.fromEntries(WORKFLOW_NODE_KINDS.map((kind) => [kind, new Set([...NODE_FIELDS[kind], ...HOST_NODE_FIELDS, ...(IGNORED_NODE_FIELDS[kind] ?? [])])]));
|
|
127
145
|
export const childSteps = (child) => {
|
|
128
146
|
const root = child?.root;
|
|
129
147
|
if (!root || typeof root !== "object")
|
|
130
148
|
return null;
|
|
131
149
|
return root.node === "chain" ? (Array.isArray(root.steps) ? root.steps : null) : [root];
|
|
132
150
|
};
|
|
151
|
+
/** The code a retry class implies when the failure names none. */
|
|
152
|
+
const codeOfRetryClass = (retryClass) => retryClass === null ? null : retryClass === "timeout" ? "effect_timeout" : retryClass === "exit" ? "effect_exit" : "effect_transport";
|
|
153
|
+
/** A failed effect: its retry class (null when the failure is not transient by nature), its closed code,
|
|
154
|
+
* and what the host observed (`detail`, written onto the attempt's `effect.failed` event). */
|
|
133
155
|
export class EffectFailure extends Error {
|
|
134
156
|
retryClass;
|
|
135
|
-
|
|
136
|
-
|
|
157
|
+
code;
|
|
158
|
+
detail;
|
|
159
|
+
constructor(message, retryClass = null, options = {}) {
|
|
160
|
+
super(message, options.cause === undefined ? undefined : { cause: options.cause });
|
|
137
161
|
this.retryClass = retryClass;
|
|
138
162
|
this.name = "EffectFailure";
|
|
163
|
+
this.code = options.code === undefined ? codeOfRetryClass(retryClass) : options.code;
|
|
164
|
+
if (options.detail)
|
|
165
|
+
this.detail = options.detail;
|
|
139
166
|
}
|
|
140
167
|
}
|
|
141
168
|
function containsReportNode(node) {
|
|
@@ -168,6 +195,14 @@ function predicateShapeErrors(pred) {
|
|
|
168
195
|
if (typeof pred.n !== "number" || !Number.isFinite(pred.n))
|
|
169
196
|
out.push(`${pred.predicate} needs a finite number n`);
|
|
170
197
|
}
|
|
198
|
+
if (pred.predicate === "field_type") {
|
|
199
|
+
if (!String(pred.path || "").trim())
|
|
200
|
+
out.push("field_type needs a path");
|
|
201
|
+
if (!FIELD_TYPES.includes(pred.type))
|
|
202
|
+
out.push(`field_type needs a type of ${FIELD_TYPES.join("|")}, not ${JSON.stringify(pred.type) ?? "none"}`);
|
|
203
|
+
}
|
|
204
|
+
if (pred.predicate === "not" && (!pred.of || typeof pred.of !== "object" || Array.isArray(pred.of)))
|
|
205
|
+
out.push(`not needs "of", one mechanical predicate`);
|
|
171
206
|
if (pred.predicate === "ask") {
|
|
172
207
|
if (typeof pred.instructions !== "string" || !pred.instructions.trim())
|
|
173
208
|
out.push("ask needs instructions (the yes/no question)");
|
|
@@ -177,9 +212,54 @@ function predicateShapeErrors(pred) {
|
|
|
177
212
|
out.push("ask.gte must be in (0.5, 1]");
|
|
178
213
|
if (pred.state !== undefined && (!pred.state || typeof pred.state !== "object" || Array.isArray(pred.state)))
|
|
179
214
|
out.push("ask.state must be an object map");
|
|
215
|
+
for (const problem of decisionFieldProblems(pred))
|
|
216
|
+
out.push(`ask.${problem}`);
|
|
217
|
+
// Without a state the question sees the whole workflow state, which a token may sit in: only a given state is checked.
|
|
218
|
+
if (pred.state !== undefined)
|
|
219
|
+
for (const token of unusedImageTokens(pred.images, [pred.state, pred.instructions, pred.criteria]))
|
|
220
|
+
out.push(`ask.images token ${JSON.stringify(token)} appears in neither its state nor its question`);
|
|
221
|
+
}
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
/** The problems with a decision's provider, model and timeoutMs, each naming its field; the schema refuses an evidence
|
|
225
|
+
* mode it does not list. */
|
|
226
|
+
function decisionFieldProblems(raw) {
|
|
227
|
+
const out = [];
|
|
228
|
+
if (raw.provider !== undefined && !(typeof raw.provider === "string" && /^[a-z][a-z0-9_:.-]*$/.test(raw.provider)))
|
|
229
|
+
out.push("provider must name a decision provider: lowercase letters, digits and _ : . -");
|
|
230
|
+
if (raw.model !== undefined && !(typeof raw.model === "string" && raw.model.trim()))
|
|
231
|
+
out.push("model must be a non-empty string");
|
|
232
|
+
if (raw.timeoutMs !== undefined && !(Number.isSafeInteger(raw.timeoutMs) && raw.timeoutMs > 0 && raw.timeoutMs <= DECISION_TIMEOUT_MAX_MS)) {
|
|
233
|
+
out.push(`timeoutMs must be a whole number of milliseconds from 1 to ${DECISION_TIMEOUT_MAX_MS}`);
|
|
234
|
+
}
|
|
235
|
+
if (raw.images !== undefined) {
|
|
236
|
+
const images = raw.images;
|
|
237
|
+
if (!images || typeof images !== "object" || Array.isArray(images) || !Object.keys(images).length)
|
|
238
|
+
out.push("images must be a non-empty object map of token -> image reference");
|
|
239
|
+
else
|
|
240
|
+
for (const [token, reference] of Object.entries(images)) {
|
|
241
|
+
if (!token.trim() || /["\\\u0000-\u001f]/.test(token))
|
|
242
|
+
out.push(`images token ${JSON.stringify(token)} must be plain, non-empty text (no quotes, backslashes or control characters)`);
|
|
243
|
+
if (typeof reference !== "string" || !reference.trim())
|
|
244
|
+
out.push(`images.${token} must be an image reference: a non-empty string, such as "{item.page_image}"`);
|
|
245
|
+
}
|
|
180
246
|
}
|
|
181
247
|
return out;
|
|
182
248
|
}
|
|
249
|
+
/** The predicate under any number of `not`s: the one that reads a value. */
|
|
250
|
+
function negated(pred) {
|
|
251
|
+
while (pred && typeof pred === "object" && pred.predicate === "not")
|
|
252
|
+
pred = pred.of;
|
|
253
|
+
return pred;
|
|
254
|
+
}
|
|
255
|
+
/** The image tokens of a decision node that appear in none of `texts` (its state, its questions' text): a provider sends an
|
|
256
|
+
* image only where the request names it. */
|
|
257
|
+
function unusedImageTokens(images, texts) {
|
|
258
|
+
if (!images || typeof images !== "object" || Array.isArray(images))
|
|
259
|
+
return [];
|
|
260
|
+
const haystack = JSON.stringify(texts) ?? "";
|
|
261
|
+
return Object.keys(images).filter((token) => token.trim() && !haystack.includes(JSON.stringify(token).slice(1, -1)));
|
|
262
|
+
}
|
|
183
263
|
function resultPathMissing(shape, dotted) {
|
|
184
264
|
let cursor = shape;
|
|
185
265
|
for (const segment of dotted.split(".")) {
|
|
@@ -246,7 +326,23 @@ export function buildReferenceContext(files) {
|
|
|
246
326
|
}
|
|
247
327
|
return { references, references_parsed: referencesParsed };
|
|
248
328
|
}
|
|
249
|
-
|
|
329
|
+
/** Whether a field's schema lists the value itself: its own `const`, or a member of its own `enum`, or the same in the
|
|
330
|
+
* schema a local `$ref` names in the root schema's `definitions` or `$defs`, or in any `anyOf` or `oneOf` branch, at any
|
|
331
|
+
* depth. A schema's own listed values count beside its `$ref`, never in place of it. */
|
|
332
|
+
function listsValue(value, spec, root, hops = 0) {
|
|
333
|
+
if (hops > 16 || !spec || typeof spec !== "object")
|
|
334
|
+
return false;
|
|
335
|
+
if (spec.const === value || (Array.isArray(spec.enum) && spec.enum.includes(value)))
|
|
336
|
+
return true;
|
|
337
|
+
if (typeof spec.$ref === "string") {
|
|
338
|
+
const match = spec.$ref.match(/^#\/(definitions|\$defs)\/([^/]+)$/);
|
|
339
|
+
const target = match ? root?.[match[1]]?.[match[2]] : undefined;
|
|
340
|
+
if (target && listsValue(value, target, root, hops + 1))
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
return [spec.anyOf, spec.oneOf].some((branches) => Array.isArray(branches) && branches.some((branch) => listsValue(value, branch, root, hops + 1)));
|
|
344
|
+
}
|
|
345
|
+
export function normalizeStringNullsForSchema(value, schema, depth = 0, root = schema) {
|
|
250
346
|
if (depth > 6 || !value || typeof value !== "object" || Array.isArray(value))
|
|
251
347
|
return value;
|
|
252
348
|
const properties = schema?.properties || {};
|
|
@@ -257,17 +353,19 @@ export function normalizeStringNullsForSchema(value, schema, depth = 0) {
|
|
|
257
353
|
const anyOf = Array.isArray(spec.anyOf) ? spec.anyOf : [];
|
|
258
354
|
const nullable = types.includes("null") || anyOf.some((variant) => variant?.type === "null");
|
|
259
355
|
const current = value[key];
|
|
260
|
-
|
|
356
|
+
// A string the field's own enum or const lists exactly is one of its values, never a spelling of null.
|
|
357
|
+
const listed = listsValue(current, spec, root);
|
|
358
|
+
if (nullable && !listed && typeof current === "string" && ["null", "none", "n/a"].includes(current.trim().toLowerCase())) {
|
|
261
359
|
value[key] = null;
|
|
262
360
|
continue;
|
|
263
361
|
}
|
|
264
362
|
const objectSpecs = [spec, ...anyOf].filter((candidate) => candidate?.type === "object" || (candidate?.properties && typeof candidate.properties === "object"));
|
|
265
363
|
for (const objectSpec of objectSpecs)
|
|
266
|
-
normalizeStringNullsForSchema(current, objectSpec, depth + 1);
|
|
364
|
+
normalizeStringNullsForSchema(current, objectSpec, depth + 1, root);
|
|
267
365
|
const items = spec.type === "array" || types.includes("array") ? spec.items : anyOf.find((variant) => variant?.type === "array")?.items;
|
|
268
366
|
if (items && Array.isArray(current))
|
|
269
367
|
for (const element of current)
|
|
270
|
-
normalizeStringNullsForSchema(element, items, depth + 1);
|
|
368
|
+
normalizeStringNullsForSchema(element, items, depth + 1, root);
|
|
271
369
|
}
|
|
272
370
|
return value;
|
|
273
371
|
}
|
|
@@ -314,18 +412,63 @@ export function workflowDocumentError(value) {
|
|
|
314
412
|
}
|
|
315
413
|
return undefined;
|
|
316
414
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
415
|
+
const pointerKey = (key) => key.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
416
|
+
/** The names `retry.on` accepts: a call's retry classes and the effect-failure codes. */
|
|
417
|
+
const RETRY_ON_NAMES = [...CALL_RETRY_CLASSES, ...EFFECT_FAILURE_CODES];
|
|
418
|
+
/** Advice the validator gives without refusing: a shape that runs and costs more than it needs to. A map with no
|
|
419
|
+
* `resultPath` whose body writes no `as` returns each item's whole state, which holds the list the map ran over: the
|
|
420
|
+
* map's result, and every state after it, grow with the square of its items. */
|
|
421
|
+
function workflowWarnings(workflow) {
|
|
422
|
+
const warnings = [];
|
|
423
|
+
// Warnings are given for a document the validator refuses too, so a field may be missing or of the wrong kind.
|
|
424
|
+
const list = (value) => (Array.isArray(value) ? value : []);
|
|
425
|
+
const visit = (node) => {
|
|
426
|
+
if (!node || typeof node !== "object")
|
|
427
|
+
return;
|
|
428
|
+
switch (node.node) {
|
|
429
|
+
case "chain":
|
|
430
|
+
list(node.steps).forEach(visit);
|
|
431
|
+
break;
|
|
432
|
+
case "parallel":
|
|
433
|
+
list(node.branches).forEach(visit);
|
|
434
|
+
break;
|
|
435
|
+
case "map":
|
|
436
|
+
if (node.resultPath === undefined && node.body && typeof node.body === "object" && !node.body.as) {
|
|
437
|
+
warnings.push(`map "${node.label}": each item's result is its whole state, including ${node.itemsPath}; set resultPath (or an as in the body) for a large map`);
|
|
438
|
+
}
|
|
439
|
+
visit(node.body);
|
|
440
|
+
break;
|
|
441
|
+
case "loop":
|
|
442
|
+
visit(node.body);
|
|
443
|
+
break;
|
|
444
|
+
case "route":
|
|
445
|
+
if (node.branches && typeof node.branches === "object")
|
|
446
|
+
Object.values(node.branches).forEach((branch) => visit(branch?.body));
|
|
447
|
+
break;
|
|
448
|
+
case "workflow":
|
|
449
|
+
visit(node.workflow?.root);
|
|
450
|
+
break;
|
|
451
|
+
default: break;
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
visit(workflow.root);
|
|
455
|
+
return warnings;
|
|
456
|
+
}
|
|
320
457
|
export function validateWorkflow(workflow, opts) {
|
|
321
458
|
const bounded = workflowDocumentError(workflow);
|
|
322
459
|
if (bounded)
|
|
323
|
-
return { ok: false, errors: [bounded] };
|
|
324
|
-
const
|
|
325
|
-
if (
|
|
326
|
-
return { ok: false, errors:
|
|
460
|
+
return { ok: false, errors: [bounded], issues: [{ path: "", code: "DOCUMENT_INVALID", message: bounded }] };
|
|
461
|
+
const shapeIssues = workflowShapeIssues(workflow);
|
|
462
|
+
if (shapeIssues.length)
|
|
463
|
+
return { ok: false, errors: shapeIssues.map(issue => issue.message), issues: shapeIssues.map(issue => ({ path: issue.path, code: "SHAPE_INVALID", message: issue.message })) };
|
|
327
464
|
workflow = desugarWorkflow(workflow);
|
|
328
465
|
const errors = [];
|
|
466
|
+
const issues = [];
|
|
467
|
+
const fail = (code, path, message) => { errors.push(message); issues.push({ path, code, message }); };
|
|
468
|
+
// Each node's walk path (the text its messages begin with) to its JSON Pointer. Desugaring replaces nodes in place, so
|
|
469
|
+
// the pointer names the same node in the document as written.
|
|
470
|
+
const pointers = new Map();
|
|
471
|
+
const ptrOf = (path) => pointers.get(path) ?? "";
|
|
329
472
|
const declaredInput = workflow.input && Object.hasOwn(workflow.schemas, workflow.input.schemaId)
|
|
330
473
|
? resolveSchemaForWorkflow(workflow, workflow.schemas[workflow.input.schemaId]) : undefined;
|
|
331
474
|
const inputKeys = opts?.input !== undefined ? Object.keys(opts.input ?? {}) : opts?.inputKeys;
|
|
@@ -341,7 +484,7 @@ export function validateWorkflow(workflow, opts) {
|
|
|
341
484
|
};
|
|
342
485
|
for (const [key, schema] of Object.entries((declaredInput?.properties ?? {})))
|
|
343
486
|
rememberOutput(key, schema);
|
|
344
|
-
const checkTypedPath = (dotted, at) => {
|
|
487
|
+
const checkTypedPath = (dotted, at, node) => {
|
|
345
488
|
if (!reachability || reachability.unknowable)
|
|
346
489
|
return;
|
|
347
490
|
const [head, ...tail] = dotted.split(".");
|
|
@@ -360,7 +503,7 @@ export function validateWorkflow(workflow, opts) {
|
|
|
360
503
|
schema = schema.properties[segment];
|
|
361
504
|
else {
|
|
362
505
|
if (schema.additionalProperties === false && !schema.patternProperties)
|
|
363
|
-
|
|
506
|
+
fail("STATE_PATH_EXCLUDED", node, `${at}: state path "${dotted}" is excluded by the upstream schema at "${head}"; check the producer's as key and declared properties`);
|
|
364
507
|
return;
|
|
365
508
|
}
|
|
366
509
|
}
|
|
@@ -465,7 +608,7 @@ export function validateWorkflow(workflow, opts) {
|
|
|
465
608
|
// own fail-closed guard (or a structural mismatch with the actual files) fired. Code that
|
|
466
609
|
// also threw under the tolerant probe is state-dependent and stands down as before.
|
|
467
610
|
if (!tolerantThrew)
|
|
468
|
-
|
|
611
|
+
fail("CODE_INVALID", ptrOf(at.path), `${at.path} (${at.label}): code node throws against the real reference data: ${thrown instanceof Error ? thrown.message : String(thrown)}`);
|
|
469
612
|
}
|
|
470
613
|
}
|
|
471
614
|
return patches;
|
|
@@ -514,9 +657,36 @@ export function validateWorkflow(workflow, opts) {
|
|
|
514
657
|
for (const file of branch.files)
|
|
515
658
|
producedFiles.add(file);
|
|
516
659
|
}
|
|
517
|
-
//
|
|
660
|
+
// Every branch runs, so the state after holds what each one writes: the check goes on past the parallel, unless the state
|
|
661
|
+
// before it, or a branch's own, is not known (a code node's keys are probed, not known).
|
|
518
662
|
if (reachability)
|
|
519
|
-
reachability.unknowable =
|
|
663
|
+
reachability.unknowable = before.unknowable || branches.some((branch) => branch.unknowable);
|
|
664
|
+
};
|
|
665
|
+
/** After a route node: one branch runs, so the state after holds what every branch leaves. A key some branch may not leave
|
|
666
|
+
* is not known; a key's shape is kept only when every branch leaves the same one. A file any branch produces is counted, as
|
|
667
|
+
* before. The check goes on past the route unless the state before it, or a branch's own, is not known. */
|
|
668
|
+
const routeKnowledge = (before, branches) => {
|
|
669
|
+
restoreKnowledge(before);
|
|
670
|
+
if (!branches.length)
|
|
671
|
+
return;
|
|
672
|
+
if (reachability && branches.every((branch) => branch.available)) {
|
|
673
|
+
reachability.available = new Set([...branches[0].available].filter((key) => branches.every((branch) => branch.available.has(key))));
|
|
674
|
+
reachability.unknowable = before.unknowable || branches.some((branch) => branch.unknowable);
|
|
675
|
+
}
|
|
676
|
+
typedOutputs.clear();
|
|
677
|
+
for (const [key, value] of branches[0].typed)
|
|
678
|
+
if (branches.every((branch) => branch.typed.get(key) === value))
|
|
679
|
+
typedOutputs.set(key, value);
|
|
680
|
+
probedSubtrees.clear();
|
|
681
|
+
for (const key of branches[0].probed.keys())
|
|
682
|
+
if (branches.every((branch) => branch.probed.has(key)))
|
|
683
|
+
probedSubtrees.set(key, [...new Set(branches.flatMap((branch) => branch.probed.get(key)))]);
|
|
684
|
+
for (const branch of branches) {
|
|
685
|
+
for (const key of branch.spread)
|
|
686
|
+
spreadTaintedKeys.add(key);
|
|
687
|
+
for (const file of branch.files)
|
|
688
|
+
producedFiles.add(file);
|
|
689
|
+
}
|
|
520
690
|
};
|
|
521
691
|
const checkPredicatePath = (when, label, path) => {
|
|
522
692
|
if (!reachability || !when?.path)
|
|
@@ -549,10 +719,11 @@ export function validateWorkflow(workflow, opts) {
|
|
|
549
719
|
if (!missing)
|
|
550
720
|
return;
|
|
551
721
|
}
|
|
552
|
-
|
|
722
|
+
fail("STATE_PATH_EXCLUDED", ptrOf(path), `${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`);
|
|
553
723
|
};
|
|
554
724
|
// A mechanical predicate reads one state key; when no input key or earlier node writes it, the guard can never fire.
|
|
555
725
|
const checkPredicateHead = (pred, label, path, field, produced) => {
|
|
726
|
+
pred = negated(pred);
|
|
556
727
|
if (!reachability || reachability.unknowable || !pred || typeof pred !== "object")
|
|
557
728
|
return;
|
|
558
729
|
const available = produced ?? reachability.available;
|
|
@@ -562,9 +733,9 @@ export function validateWorkflow(workflow, opts) {
|
|
|
562
733
|
return;
|
|
563
734
|
const head = read.split(".")[0];
|
|
564
735
|
if (!available.has(head))
|
|
565
|
-
|
|
736
|
+
fail("STATE_PATH_UNPRODUCED", ptrOf(path), `${path} (${label}): ${field} reads "${read}" but no input key or earlier node produces "${head}"; the guard can never fire`);
|
|
566
737
|
else if (!produced)
|
|
567
|
-
checkTypedPath(read, `${path} (${label}) ${field}
|
|
738
|
+
checkTypedPath(read, `${path} (${label}) ${field}`, ptrOf(path));
|
|
568
739
|
};
|
|
569
740
|
const checkInterpolations = (value, label, path) => {
|
|
570
741
|
if (!reachability || reachability.unknowable || value === undefined)
|
|
@@ -576,16 +747,16 @@ export function validateWorkflow(workflow, opts) {
|
|
|
576
747
|
continue;
|
|
577
748
|
const head = (m[1].startsWith("state.") ? m[1].slice(6) : m[1]).split(".")[0];
|
|
578
749
|
if (!reachability.available.has(head))
|
|
579
|
-
|
|
750
|
+
fail("STATE_PATH_UNPRODUCED", ptrOf(path), `${path} (${label}): interpolates {${m[1]}} but no upstream node produces "${head}"`);
|
|
580
751
|
else
|
|
581
|
-
checkTypedPath(m[1].startsWith("state.") ? m[1].slice(6) : m[1], `${path} (${label})
|
|
752
|
+
checkTypedPath(m[1].startsWith("state.") ? m[1].slice(6) : m[1], `${path} (${label})`, ptrOf(path));
|
|
582
753
|
}
|
|
583
754
|
};
|
|
584
755
|
const checkRequires = (node, path) => {
|
|
585
756
|
if (node.requires === undefined)
|
|
586
757
|
return;
|
|
587
758
|
if (!Array.isArray(node.requires) || node.requires.some((p) => typeof p !== "string" || !p.trim())) {
|
|
588
|
-
|
|
759
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): requires must be non-empty state paths`);
|
|
589
760
|
return;
|
|
590
761
|
}
|
|
591
762
|
if (!reachability || reachability.unknowable)
|
|
@@ -593,10 +764,10 @@ export function validateWorkflow(workflow, opts) {
|
|
|
593
764
|
for (const required of node.requires) {
|
|
594
765
|
const head = String(required).split(".")[0];
|
|
595
766
|
if (!reachability.available.has(head)) {
|
|
596
|
-
|
|
767
|
+
fail("STATE_PATH_UNPRODUCED", ptrOf(path), `${path} (${node.label}): requires "${required}" but no upstream node produces it; state here has only: ${[...reachability.available].join(", ")}`);
|
|
597
768
|
}
|
|
598
769
|
else
|
|
599
|
-
checkTypedPath(required, `${path} (${node.label}) requires
|
|
770
|
+
checkTypedPath(required, `${path} (${node.label}) requires`, ptrOf(path));
|
|
600
771
|
}
|
|
601
772
|
};
|
|
602
773
|
const checkItemsPath = (node, path) => {
|
|
@@ -604,23 +775,23 @@ export function validateWorkflow(workflow, opts) {
|
|
|
604
775
|
return;
|
|
605
776
|
const head = node.itemsPath.split(".")[0];
|
|
606
777
|
if (!reachability.available.has(head))
|
|
607
|
-
|
|
778
|
+
fail("STATE_PATH_UNPRODUCED", ptrOf(path), `${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)`);
|
|
608
779
|
else
|
|
609
|
-
checkTypedPath(node.itemsPath, `${path} (${node.label}) itemsPath
|
|
780
|
+
checkTypedPath(node.itemsPath, `${path} (${node.label}) itemsPath`, ptrOf(path));
|
|
610
781
|
};
|
|
611
782
|
if (workflow?.v !== 2)
|
|
612
|
-
|
|
783
|
+
fail("WORKFLOW_FIELD_INVALID", "", "workflow.v must be 2");
|
|
613
784
|
if (!workflow?.name)
|
|
614
|
-
|
|
785
|
+
fail("WORKFLOW_FIELD_INVALID", "", "workflow.name is required");
|
|
615
786
|
if (!workflow?.schemas || typeof workflow.schemas !== "object")
|
|
616
|
-
|
|
787
|
+
fail("WORKFLOW_FIELD_INVALID", "", "workflow.schemas is required");
|
|
617
788
|
if (workflow?.input !== undefined && (!workflow.input || typeof workflow.input.schemaId !== "string" || !Object.hasOwn(workflow.schemas, workflow.input.schemaId)))
|
|
618
|
-
|
|
789
|
+
fail("WORKFLOW_FIELD_INVALID", "", "workflow.input.schemaId must name a schema");
|
|
619
790
|
if (!workflow?.output?.schemaId || !Object.hasOwn(workflow.schemas, workflow.output.schemaId))
|
|
620
|
-
|
|
791
|
+
fail("WORKFLOW_FIELD_INVALID", "", "workflow.output.schemaId must name a schema");
|
|
621
792
|
for (const [id, schema] of Object.entries(workflow?.schemas || {})) {
|
|
622
793
|
if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
|
|
623
|
-
|
|
794
|
+
fail("SCHEMA_INVALID", `/schemas/${pointerKey(id)}`, `schema "${id}" must be a JSON-schema object (got ${schema === null ? "null" : Array.isArray(schema) ? "array" : typeof schema})`);
|
|
624
795
|
continue;
|
|
625
796
|
}
|
|
626
797
|
// Fail closed on refs: typebox compiles an unresolvable $ref to a never-matching schema
|
|
@@ -649,25 +820,26 @@ export function validateWorkflow(workflow, opts) {
|
|
|
649
820
|
for (const ref of refs) {
|
|
650
821
|
const match = ref.match(/^#\/definitions\/(.+)$/);
|
|
651
822
|
if (!match)
|
|
652
|
-
|
|
823
|
+
fail("SCHEMA_INVALID", `/schemas/${pointerKey(id)}`, `schema "${id}": unsupported $ref form "${ref}" — only "#/definitions/<name>" resolves (against inline definitions or a sibling schema)`);
|
|
653
824
|
else if (!Object.hasOwn(definitions, match[1]))
|
|
654
|
-
|
|
825
|
+
fail("SCHEMA_INVALID", `/schemas/${pointerKey(id)}`, `schema "${id}": $ref "${ref}" resolves to no inline definition and no sibling schema`);
|
|
655
826
|
}
|
|
656
827
|
try {
|
|
657
828
|
Compile(resolved);
|
|
658
829
|
}
|
|
659
830
|
catch (e) {
|
|
660
|
-
|
|
831
|
+
fail("SCHEMA_INVALID", `/schemas/${pointerKey(id)}`, `schema "${id}" does not compile: ${e?.message || e}`);
|
|
661
832
|
}
|
|
662
833
|
}
|
|
663
834
|
if (opts?.input !== undefined) {
|
|
664
835
|
if (!opts.input || typeof opts.input !== "object" || Array.isArray(opts.input))
|
|
665
|
-
|
|
836
|
+
fail("INPUT_INVALID", "", "input must be a JSON object");
|
|
666
837
|
else if (declaredInput) {
|
|
667
838
|
try {
|
|
668
839
|
const validator = Compile(declaredInput);
|
|
669
840
|
if (!validator.Check(opts.input))
|
|
670
|
-
|
|
841
|
+
for (const problem of schemaProblems(validator, opts.input))
|
|
842
|
+
fail("INPUT_INVALID", "", `input: ${problem}`);
|
|
671
843
|
}
|
|
672
844
|
catch { /* Schema diagnostics above retain the original compilation failure. */ }
|
|
673
845
|
}
|
|
@@ -676,58 +848,107 @@ export function validateWorkflow(workflow, opts) {
|
|
|
676
848
|
if (map === undefined)
|
|
677
849
|
return;
|
|
678
850
|
if (!map || typeof map !== "object" || Array.isArray(map)) {
|
|
679
|
-
|
|
851
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): ${field} must be an object map of what the questions see`);
|
|
680
852
|
return;
|
|
681
853
|
}
|
|
682
854
|
checkInterpolations(map, label, path);
|
|
683
855
|
};
|
|
856
|
+
/** A decision node's images: each reference is interpolated as a state value is, so its paths must be produced upstream;
|
|
857
|
+
* with `texts`, each token must appear in one of them. */
|
|
858
|
+
const checkImages = (images, label, path, texts) => {
|
|
859
|
+
if (!images || typeof images !== "object" || Array.isArray(images))
|
|
860
|
+
return;
|
|
861
|
+
checkInterpolations(images, label, path);
|
|
862
|
+
if (texts)
|
|
863
|
+
for (const token of unusedImageTokens(images, texts))
|
|
864
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): images token ${JSON.stringify(token)} appears in neither the node's state nor its questions; write it where the image belongs`);
|
|
865
|
+
};
|
|
684
866
|
const checkQuestionSchema = (schemaId, label, path) => {
|
|
685
867
|
if (typeof schemaId !== "string" || !Object.hasOwn(workflow.schemas, schemaId)) {
|
|
686
|
-
|
|
868
|
+
fail("SCHEMA_UNKNOWN", ptrOf(path), `${path} (${label}): out schema "${String(schemaId)}" not in workflow.schemas`);
|
|
687
869
|
return null;
|
|
688
870
|
}
|
|
689
871
|
const compiled = compileQuestions(workflow.schemas[schemaId], workflow.schemas);
|
|
690
872
|
if (compiled.ok === false) {
|
|
691
|
-
|
|
873
|
+
for (const e of compiled.errors)
|
|
874
|
+
fail("SCHEMA_INVALID", `/schemas/${pointerKey(schemaId)}`, `${path} (${label}): schema "${schemaId}": ${e}`);
|
|
692
875
|
return null;
|
|
693
876
|
}
|
|
694
877
|
return compiled.questions;
|
|
695
878
|
};
|
|
696
|
-
|
|
879
|
+
// `within` names what takes only a mechanical predicate: a poll, or a `not`.
|
|
880
|
+
const checkPredicate = (pred, label, path, field, within) => {
|
|
697
881
|
const name = pred?.predicate;
|
|
698
|
-
|
|
699
|
-
|
|
882
|
+
// A predicate with no `predicate` field is the shape mistaken, not an unknown name: say the shape,
|
|
883
|
+
// naming a predicate the object used as a key when it did.
|
|
884
|
+
if (pred && typeof pred === "object" && !Array.isArray(pred) && name === undefined) {
|
|
885
|
+
const keyed = Object.keys(pred).find((key) => PREDICATES.has(key));
|
|
886
|
+
fail("PREDICATE_INVALID", ptrOf(path), `${path} (${label}): ${field}: a predicate names itself in its "predicate" field beside its own fields, e.g. {"predicate": "${keyed ?? "field_true"}", "path": "..."}${keyed ? `, not {"${keyed}": {...}}` : ""}`);
|
|
887
|
+
return false;
|
|
888
|
+
}
|
|
889
|
+
if (!pred || !PREDICATES.has(name)) {
|
|
890
|
+
fail("PREDICATE_INVALID", ptrOf(path), `${path} (${label}): ${field}: unknown predicate "${name}"`);
|
|
700
891
|
return false;
|
|
701
892
|
}
|
|
702
|
-
|
|
703
|
-
|
|
893
|
+
if (within && !MECHANICAL_PREDICATE_NAMES.has(name)) {
|
|
894
|
+
fail("PREDICATE_INVALID", ptrOf(path), within === "poll" ? `${path} (${label}): ${field}: unknown predicate "${name}" (a poll reads a value, never asks a question)` : `${path} (${label}): ${field}: not takes only a mechanical predicate, not "${name}"`);
|
|
895
|
+
return false;
|
|
896
|
+
}
|
|
897
|
+
const errors = predicateShapeErrors(pred);
|
|
898
|
+
for (const e of errors)
|
|
899
|
+
fail("PREDICATE_INVALID", ptrOf(path), `${path} (${label}): ${field}: ${e}`);
|
|
900
|
+
if (name === "ask") {
|
|
704
901
|
checkStateMap(pred.state, label, path, `${field}.state`);
|
|
902
|
+
checkImages(pred.images, label, path);
|
|
903
|
+
}
|
|
904
|
+
// A predicate whose shape is wrong is refused: no caller reads inside it (a poll's path, a not's of).
|
|
905
|
+
if (errors.length)
|
|
906
|
+
return false;
|
|
907
|
+
if (name === "not")
|
|
908
|
+
return checkPredicate(pred.of, label, path, `${field}.of`, "not");
|
|
705
909
|
return true;
|
|
706
910
|
};
|
|
707
|
-
const walk = (node, path) => {
|
|
911
|
+
const walk = (node, path, ptr) => {
|
|
912
|
+
// A node that is missing (a map or loop without a body) is reported on the node that should hold it.
|
|
913
|
+
pointers.set(path, node === undefined ? ptr.slice(0, ptr.lastIndexOf("/")) : ptr);
|
|
708
914
|
if (!node || typeof node !== "object" || !KINDS.has(node.node)) {
|
|
709
|
-
|
|
915
|
+
fail("NODE_KIND_UNKNOWN", ptrOf(path), `${path}: unknown node kind "${node?.node}"`);
|
|
710
916
|
return;
|
|
711
917
|
}
|
|
712
918
|
{
|
|
713
919
|
const allowed = KIND_KEYS[node.node];
|
|
714
920
|
const extraneous = Object.keys(node).filter((key) => !allowed.has(key));
|
|
715
921
|
if (extraneous.length)
|
|
716
|
-
|
|
717
|
-
//
|
|
718
|
-
// is the state key whenever `as` is omitted, so labels are held to the same rule.
|
|
719
|
-
if (typeof node.as === "string" && node.as
|
|
720
|
-
|
|
922
|
+
fail("NODE_KEY_UNKNOWN", ptrOf(path), `${path} (${node.label || node.node}): unknown key(s) for a ${node.node} node: ${extraneous.join(", ")}`);
|
|
923
|
+
// State keys that hold a `$` are engine-owned (`$host`, and a typed question's `<as>$answers` and `<as>$verify`): no node
|
|
924
|
+
// may write one, wherever the `$` is. A label is the state key whenever `as` is omitted, so labels are held to the same rule.
|
|
925
|
+
if (typeof node.as === "string" && holdsDollar(node.as))
|
|
926
|
+
fail("STATE_KEY_RESERVED", ptrOf(path), `${path} (${node.label || node.node}): "as" must not name an engine-owned "$" state key ("${node.as}")`);
|
|
721
927
|
// Only an unaliased generative or code node writes under its label; every other label is a name, not a key.
|
|
722
928
|
const labelIsKey = (node.node === "agent" || node.node === "decide" || node.node === "extract" || node.node === "code") && node.as === undefined;
|
|
723
|
-
if (labelIsKey && typeof node.label === "string" && node.label
|
|
724
|
-
|
|
929
|
+
if (labelIsKey && typeof node.label === "string" && holdsDollar(node.label))
|
|
930
|
+
fail("STATE_KEY_RESERVED", ptrOf(path), `${path} (${node.label}): label must not contain "$" — an unaliased ${node.node} node writes under its label, and "$" keys are engine-owned`);
|
|
931
|
+
// A generative or code node's write key must be a real state key, never blank: the interpreter writes under
|
|
932
|
+
// `as || label` (`as` wins when it is any non-empty string, whitespace included), so a blank or whitespace-only
|
|
933
|
+
// `as`, or a label that is the key and is whitespace-only, would write under a key no other node can name and hide
|
|
934
|
+
// a disjointness collision. (Other nodes' `as` is checked "required" in their own case.)
|
|
935
|
+
const writesUnderKey = node.node === "agent" || node.node === "decide" || node.node === "extract" || node.node === "code";
|
|
936
|
+
if (writesUnderKey && typeof node.as === "string" && !node.as.trim())
|
|
937
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label || node.node}): "as" must be a non-empty state key, not blank`);
|
|
938
|
+
if (labelIsKey && typeof node.label === "string" && !node.label.trim())
|
|
939
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.node}): label must be a non-empty state key when the node writes under it (no "as")`);
|
|
940
|
+
if (opts?.tools) {
|
|
941
|
+
const named = [...(Array.isArray(node.tools) ? node.tools : []), ...(node.node === "call" && node.via === "tool" ? [node.tool] : [])];
|
|
942
|
+
for (const tool of named)
|
|
943
|
+
if (typeof tool === "string" && !opts.tools.includes(tool))
|
|
944
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label || node.node}): tool "${tool}" is not one of the host's tools`);
|
|
945
|
+
}
|
|
725
946
|
}
|
|
726
947
|
switch (node.node) {
|
|
727
948
|
case "chain":
|
|
728
949
|
if (!Array.isArray(node.steps) || !node.steps.length)
|
|
729
|
-
|
|
730
|
-
(node.steps || []).forEach((s, i) => walk(s, `${path}.steps[${i}]`));
|
|
950
|
+
fail("STRUCTURE_INVALID", ptrOf(path), `${path}: chain needs steps`);
|
|
951
|
+
(node.steps || []).forEach((s, i) => walk(s, `${path}.steps[${i}]`, `${ptr}/steps/${i}`));
|
|
731
952
|
return;
|
|
732
953
|
case "code":
|
|
733
954
|
try {
|
|
@@ -737,7 +958,7 @@ export function validateWorkflow(workflow, opts) {
|
|
|
737
958
|
compileTransform(node.code);
|
|
738
959
|
}
|
|
739
960
|
catch (e) {
|
|
740
|
-
|
|
961
|
+
fail("CODE_INVALID", ptrOf(path), `${path} (${node.label}): ${e?.message || e}`);
|
|
741
962
|
}
|
|
742
963
|
if (reachability) {
|
|
743
964
|
const patches = probeCodePatches(node.code, { path, label: node.label });
|
|
@@ -764,33 +985,37 @@ export function validateWorkflow(workflow, opts) {
|
|
|
764
985
|
case "decide":
|
|
765
986
|
case "extract":
|
|
766
987
|
case "report":
|
|
767
|
-
|
|
768
|
-
checkStateMap(node.state, node.label, path);
|
|
988
|
+
checkStateMap(node.state, node.label, path);
|
|
769
989
|
if (!node.instructions?.trim())
|
|
770
|
-
|
|
990
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): instructions required`);
|
|
771
991
|
if (node.node !== "report" && !Object.hasOwn(workflow.schemas, node.out))
|
|
772
|
-
|
|
992
|
+
fail("SCHEMA_UNKNOWN", ptrOf(path), `${path} (${node.label}): out schema "${node.out}" not in workflow.schemas`);
|
|
773
993
|
if (node.thinking !== undefined && !THINKING_LEVELS.includes(String(node.thinking)))
|
|
774
|
-
|
|
994
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): thinking must be low|medium|high — thinking is never off`);
|
|
775
995
|
if (node.tools !== undefined && (!Array.isArray(node.tools) || node.tools.some((t) => typeof t !== "string" || !t.trim())))
|
|
776
|
-
|
|
996
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): tools must be a list of non-empty tool names when present (an empty list disables tools)`);
|
|
777
997
|
checkRequires(node, path);
|
|
778
998
|
if (node.tier !== undefined && !MODEL_TIERS.includes(String(node.tier)))
|
|
779
|
-
|
|
999
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): tier must be fast|default|strong`);
|
|
1000
|
+
if (node.node === "agent" && node.budget_usd !== undefined && !(typeof node.budget_usd === "number" && Number.isFinite(node.budget_usd) && node.budget_usd > 0))
|
|
1001
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): budget_usd must be a number of dollars greater than 0`);
|
|
1002
|
+
if (node.node === "agent" && node.max_turns !== undefined && !(Number.isSafeInteger(node.max_turns) && node.max_turns >= 1))
|
|
1003
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): max_turns must be a positive integer`);
|
|
1004
|
+
if (node.node === "agent" && node.context !== undefined && !AGENT_CONTEXTS.includes(String(node.context)))
|
|
1005
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): context must be ${AGENT_CONTEXTS.join("|")}`);
|
|
780
1006
|
if (node.node !== "report" && node.verify !== undefined) {
|
|
781
1007
|
const v = node.verify;
|
|
782
1008
|
if (!v || typeof v !== "object" || typeof v.out !== "string")
|
|
783
|
-
|
|
1009
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): verify needs an out (a question schema id)`);
|
|
784
1010
|
else {
|
|
785
1011
|
const qs = checkQuestionSchema(v.out, node.label, path);
|
|
786
1012
|
if (qs && !Object.values(qs).some((q) => q.type === "noul"))
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
errors.push(`${path} (${node.label}): verify.maxDrives must be an integer 1..4`);
|
|
1013
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${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)`);
|
|
1014
|
+
checkStateMap(v.state, node.label, path, "verify.state");
|
|
1015
|
+
if (v.maxDrives !== undefined && (!Number.isInteger(v.maxDrives) || v.maxDrives < 1))
|
|
1016
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): verify.maxDrives must be an integer >= 1`);
|
|
792
1017
|
if (v.override !== undefined && (typeof v.override?.below !== "number" || v.override.below <= 0 || v.override.below >= 1))
|
|
793
|
-
|
|
1018
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): verify.override.below must be in (0, 1)`);
|
|
794
1019
|
}
|
|
795
1020
|
}
|
|
796
1021
|
if (reachability && node.node === "report") {
|
|
@@ -809,32 +1034,44 @@ export function validateWorkflow(workflow, opts) {
|
|
|
809
1034
|
return;
|
|
810
1035
|
case "map":
|
|
811
1036
|
if (node.resultPath !== undefined && (typeof node.resultPath !== "string" || !node.resultPath.trim()))
|
|
812
|
-
|
|
1037
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): resultPath must be a non-empty path in the completed item state`);
|
|
813
1038
|
if (node.maxConcurrency !== undefined && (!Number.isSafeInteger(node.maxConcurrency) || node.maxConcurrency < 1))
|
|
814
|
-
|
|
1039
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): maxConcurrency must be a positive safe integer`);
|
|
815
1040
|
if (!node.itemsPath)
|
|
816
|
-
|
|
1041
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): itemsPath required`);
|
|
817
1042
|
checkItemsPath(node, path);
|
|
818
1043
|
if (!node.as)
|
|
819
|
-
|
|
1044
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): as required`);
|
|
1045
|
+
if (node.onItemFailure !== undefined && node.onItemFailure !== "stop" && node.onItemFailure !== "collect")
|
|
1046
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): onItemFailure must be "stop" or "collect"`);
|
|
1047
|
+
if (node.onItemFailure === "collect") {
|
|
1048
|
+
if (typeof node.failedAs !== "string" || !node.failedAs.trim())
|
|
1049
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): onItemFailure "collect" needs failedAs, the state key the map writes its failed items under`);
|
|
1050
|
+
else if (holdsDollar(node.failedAs))
|
|
1051
|
+
fail("STATE_KEY_RESERVED", ptrOf(path), `${path} (${node.label}): failedAs must not name an engine-owned "$" state key ("${node.failedAs}")`);
|
|
1052
|
+
else if (node.failedAs === node.as)
|
|
1053
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): failedAs must differ from as: the map writes its results and its failed items under two keys`);
|
|
1054
|
+
}
|
|
1055
|
+
else if (node.failedAs !== undefined)
|
|
1056
|
+
fail("NODE_KEY_UNKNOWN", ptrOf(path), `${path} (${node.label}): failedAs is written only with onItemFailure "collect"`);
|
|
820
1057
|
if (containsReportNode(node.body))
|
|
821
|
-
|
|
1058
|
+
fail("STRUCTURE_INVALID", ptrOf(path), `${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)`);
|
|
822
1059
|
if (reachability)
|
|
823
1060
|
reachability.unknowable = true;
|
|
824
|
-
walk(node.body, `${path}.body`);
|
|
1061
|
+
walk(node.body, `${path}.body`, `${ptr}/body`);
|
|
825
1062
|
return;
|
|
826
1063
|
case "parallel":
|
|
827
1064
|
if (!Array.isArray(node.branches) || node.branches.length < 2)
|
|
828
|
-
|
|
1065
|
+
fail("STRUCTURE_INVALID", ptrOf(path), `${path} (${node.label}): parallel needs at least two branches`);
|
|
829
1066
|
if (containsReportNode(node))
|
|
830
|
-
|
|
1067
|
+
fail("STRUCTURE_INVALID", ptrOf(path), `${path} (${node.label}): a report node cannot live inside a parallel branch — the report is rendered once, after every branch has landed`);
|
|
831
1068
|
{
|
|
832
1069
|
const writers = new Map();
|
|
833
1070
|
(node.branches || []).forEach((branch, i) => {
|
|
834
1071
|
for (const key of declaredWrites(branch)) {
|
|
835
1072
|
const prior = writers.get(key);
|
|
836
1073
|
if (prior !== undefined && prior !== i)
|
|
837
|
-
|
|
1074
|
+
fail("PARALLEL_WRITE_CONFLICT", ptrOf(path), `${path} (${node.label}): branches ${prior} and ${i} both write state.${key} — parallel branches must write disjoint keys`);
|
|
838
1075
|
writers.set(key, i);
|
|
839
1076
|
}
|
|
840
1077
|
});
|
|
@@ -846,20 +1083,20 @@ export function validateWorkflow(workflow, opts) {
|
|
|
846
1083
|
const after = [];
|
|
847
1084
|
(node.branches || []).forEach((branch, i) => {
|
|
848
1085
|
restoreKnowledge(before);
|
|
849
|
-
walk(branch, `${path}.branches[${i}]`);
|
|
1086
|
+
walk(branch, `${path}.branches[${i}]`, `${ptr}/branches/${i}`);
|
|
850
1087
|
after.push(snapshotKnowledge());
|
|
851
1088
|
});
|
|
852
1089
|
if (after.length)
|
|
853
1090
|
mergeKnowledge(before, after);
|
|
854
|
-
if (reachability)
|
|
855
|
-
reachability.unknowable = true;
|
|
856
1091
|
}
|
|
857
1092
|
return;
|
|
858
1093
|
case "loop":
|
|
859
|
-
if (!Number.isInteger(node.maxIters) || node.maxIters < 1
|
|
860
|
-
|
|
1094
|
+
if (!Number.isInteger(node.maxIters) || node.maxIters < 1)
|
|
1095
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): maxIters must be an integer >= 1`);
|
|
1096
|
+
if (node.expect_iters !== undefined && (!Number.isInteger(node.expect_iters) || node.expect_iters < 1))
|
|
1097
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): expect_iters must be an integer >= 1`);
|
|
861
1098
|
if (containsReportNode(node.body))
|
|
862
|
-
|
|
1099
|
+
fail("STRUCTURE_INVALID", ptrOf(path), `${path} (${node.label}): a report node cannot live inside a loop body — the report is rendered once, after the record is final`);
|
|
863
1100
|
checkPredicate(node.until, node.label, path, "until");
|
|
864
1101
|
{
|
|
865
1102
|
// `until` reads the state after the body: the keys before the loop plus every key the body writes.
|
|
@@ -869,25 +1106,28 @@ export function validateWorkflow(workflow, opts) {
|
|
|
869
1106
|
}
|
|
870
1107
|
if (reachability)
|
|
871
1108
|
reachability.unknowable = true;
|
|
872
|
-
walk(node.body, `${path}.body`);
|
|
1109
|
+
walk(node.body, `${path}.body`, `${ptr}/body`);
|
|
873
1110
|
return;
|
|
874
1111
|
case "escalate":
|
|
875
1112
|
mayEscalate = true;
|
|
876
1113
|
checkPredicate(node.when, node.label, path, "when");
|
|
877
1114
|
if (!node.kind?.trim() || !node.stage?.trim() || !node.summary?.trim())
|
|
878
|
-
|
|
1115
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${node.label}): kind, stage, summary required`);
|
|
879
1116
|
checkPredicateHead(node.when, node.label, path, "when");
|
|
880
|
-
checkPredicatePath(node.when, node.label, path);
|
|
1117
|
+
checkPredicatePath(negated(node.when), node.label, path);
|
|
881
1118
|
return;
|
|
882
1119
|
case "judge": {
|
|
883
1120
|
const label = node.label || "judge";
|
|
1121
|
+
for (const problem of decisionFieldProblems(node))
|
|
1122
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): ${problem}`);
|
|
884
1123
|
if (!node.state || typeof node.state !== "object" || Array.isArray(node.state) || !Object.keys(node.state).length)
|
|
885
|
-
|
|
1124
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): state must be a non-empty object map of what the questions see`);
|
|
886
1125
|
else
|
|
887
1126
|
checkStateMap(node.state, label, path);
|
|
888
1127
|
checkQuestionSchema(node.out, label, path);
|
|
1128
|
+
checkImages(node.images, label, path, [node.state, typeof node.out === "string" && Object.hasOwn(workflow.schemas, node.out) ? workflow.schemas[node.out] : null]);
|
|
889
1129
|
if (!node.as?.trim())
|
|
890
|
-
|
|
1130
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): as required`);
|
|
891
1131
|
checkRequires({ label, requires: node.requires }, path);
|
|
892
1132
|
if (reachability && node.as) {
|
|
893
1133
|
reachability.available.add(node.as);
|
|
@@ -901,18 +1141,21 @@ export function validateWorkflow(workflow, opts) {
|
|
|
901
1141
|
}
|
|
902
1142
|
case "pick": {
|
|
903
1143
|
const label = node.label || "pick";
|
|
1144
|
+
for (const problem of decisionFieldProblems(node))
|
|
1145
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): ${problem}`);
|
|
904
1146
|
if (!node.itemsPath?.trim())
|
|
905
|
-
|
|
1147
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): itemsPath required`);
|
|
906
1148
|
checkItemsPath(node, path);
|
|
907
1149
|
if (typeof node.describe !== "string" || !node.describe.trim())
|
|
908
|
-
|
|
1150
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): describe (the option text per item, e.g. "{item.name}, born {item.dob}") required`);
|
|
909
1151
|
if (typeof node.instructions !== "string" || !node.instructions.trim())
|
|
910
|
-
|
|
1152
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): instructions (the one question) required`);
|
|
911
1153
|
checkStateMap(node.state, label, path);
|
|
1154
|
+
checkImages(node.images, label, path, [node.state, node.instructions, node.describe]);
|
|
912
1155
|
if (!node.as?.trim())
|
|
913
|
-
|
|
1156
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): as required`);
|
|
914
1157
|
if (node.allowNone !== undefined && typeof node.allowNone !== "boolean")
|
|
915
|
-
|
|
1158
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): allowNone must be boolean`);
|
|
916
1159
|
checkRequires({ label, requires: node.requires }, path);
|
|
917
1160
|
if (reachability && node.as) {
|
|
918
1161
|
reachability.available.add(node.as);
|
|
@@ -925,28 +1168,31 @@ export function validateWorkflow(workflow, opts) {
|
|
|
925
1168
|
}
|
|
926
1169
|
case "sift": {
|
|
927
1170
|
const label = node.label || "sift";
|
|
1171
|
+
for (const problem of decisionFieldProblems(node))
|
|
1172
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): ${problem}`);
|
|
928
1173
|
if (!node.itemsPath?.trim())
|
|
929
|
-
|
|
1174
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): itemsPath required`);
|
|
930
1175
|
checkItemsPath(node, path);
|
|
931
1176
|
if (node.describe !== undefined && (typeof node.describe !== "string" || !node.describe.trim()))
|
|
932
|
-
|
|
1177
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): describe must be a non-empty template when present`);
|
|
933
1178
|
checkStateMap(node.state, label, path);
|
|
934
1179
|
const compiled = checkQuestionSchema(node.out, label, path);
|
|
1180
|
+
checkImages(node.images, label, path, [node.state, node.describe, typeof node.out === "string" && Object.hasOwn(workflow.schemas, node.out) ? workflow.schemas[node.out] : null]);
|
|
935
1181
|
if (!node.as?.trim())
|
|
936
|
-
|
|
1182
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): as required`);
|
|
937
1183
|
if (node.keep !== undefined) {
|
|
938
1184
|
const k = node.keep;
|
|
939
1185
|
if (!k || typeof k !== "object" || typeof k.path !== "string" || !k.path.trim())
|
|
940
|
-
|
|
1186
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): keep.path must name a question id (or <id>.confidence)`);
|
|
941
1187
|
else if (compiled) {
|
|
942
1188
|
const [id, tail] = k.path.split(".");
|
|
943
1189
|
if (!Object.hasOwn(compiled, id) || (tail !== undefined && tail !== "confidence"))
|
|
944
|
-
|
|
1190
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): keep.path "${k.path}" names no question of schema "${String(node.out)}"`);
|
|
945
1191
|
else if (compiled[id].type === "choice" && tail !== "confidence")
|
|
946
|
-
|
|
1192
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): keep.path "${k.path}" is a choice — keep on a boolean or score question, or on <id>.confidence`);
|
|
947
1193
|
}
|
|
948
1194
|
if (k?.gte !== undefined && !(typeof k.gte === "number" && Number.isFinite(k.gte)))
|
|
949
|
-
|
|
1195
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): keep.gte must be a number`);
|
|
950
1196
|
}
|
|
951
1197
|
checkRequires({ label, requires: node.requires }, path);
|
|
952
1198
|
if (reachability && node.as) {
|
|
@@ -958,45 +1204,87 @@ export function validateWorkflow(workflow, opts) {
|
|
|
958
1204
|
}
|
|
959
1205
|
case "route": {
|
|
960
1206
|
const label = node.label || "route";
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
if (
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
if (
|
|
975
|
-
|
|
976
|
-
if (
|
|
977
|
-
|
|
1207
|
+
const raw = node;
|
|
1208
|
+
// Two forms, never mixed: Jev chooses (state + instructions) or state already holds the choice (valuePath).
|
|
1209
|
+
const byValue = raw.valuePath !== undefined;
|
|
1210
|
+
const names = raw.branches && typeof raw.branches === "object" && !Array.isArray(raw.branches) ? Object.keys(raw.branches) : [];
|
|
1211
|
+
if (byValue) {
|
|
1212
|
+
const valuePath = raw.valuePath;
|
|
1213
|
+
if (typeof valuePath !== "string" || !valuePath.trim())
|
|
1214
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): valuePath must be the state path of the branch name`);
|
|
1215
|
+
for (const key of ["state", "instructions", "unsure", ...DECISION_NODE_FIELDS])
|
|
1216
|
+
if (raw[key] !== undefined)
|
|
1217
|
+
fail("NODE_KEY_UNKNOWN", ptrOf(path), `${path} (${label}): ${key} belongs to a route Jev chooses; a route by valuePath asks no question`);
|
|
1218
|
+
if (names.length < 2)
|
|
1219
|
+
fail("STRUCTURE_INVALID", ptrOf(path), `${path} (${label}): route needs at least two named branches`);
|
|
1220
|
+
if (names.some(name => !name.trim()))
|
|
1221
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): branch names must be non-empty`);
|
|
1222
|
+
if (raw.otherwise !== undefined && (typeof raw.otherwise !== "string" || !names.includes(raw.otherwise)))
|
|
1223
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): otherwise must name one of the branches`);
|
|
1224
|
+
if (typeof valuePath === "string" && valuePath.trim() && reachability && !reachability.unknowable) {
|
|
1225
|
+
const head = valuePath.split(".")[0];
|
|
1226
|
+
if (!reachability.available.has(head))
|
|
1227
|
+
fail("STATE_PATH_UNPRODUCED", ptrOf(path), `${path} (${label}): valuePath "${valuePath}" has no upstream producer; route by the key an earlier node writes`);
|
|
1228
|
+
else
|
|
1229
|
+
checkTypedPath(valuePath, `${path} (${label}) valuePath`, ptrOf(path));
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
else {
|
|
1233
|
+
const judged = node;
|
|
1234
|
+
for (const problem of decisionFieldProblems(raw))
|
|
1235
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): ${problem}`);
|
|
1236
|
+
if (!judged.state || typeof judged.state !== "object" || Array.isArray(judged.state) || !Object.keys(judged.state).length)
|
|
1237
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): state must be a non-empty object map of what the question sees`);
|
|
1238
|
+
else
|
|
1239
|
+
checkStateMap(judged.state, label, path);
|
|
1240
|
+
if (typeof judged.instructions !== "string" || !judged.instructions.trim())
|
|
1241
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): instructions (the one question) required`);
|
|
1242
|
+
checkImages(judged.images, label, path, [judged.state, judged.instructions, names.map((name) => judged.branches[name]?.criteria)]);
|
|
1243
|
+
if (raw.otherwise !== undefined)
|
|
1244
|
+
fail("NODE_KEY_UNKNOWN", ptrOf(path), `${path} (${label}): otherwise belongs to a route by valuePath; a route Jev chooses takes unsure for its fallback`);
|
|
1245
|
+
if (names.length < 2)
|
|
1246
|
+
fail("STRUCTURE_INVALID", ptrOf(path), `${path} (${label}): route needs at least two named branches`);
|
|
1247
|
+
if (names.length > SYSTEM_ONE_LIMITS.maxChoiceOptions)
|
|
1248
|
+
fail("STRUCTURE_INVALID", ptrOf(path), `${path} (${label}): route takes at most ${SYSTEM_ONE_LIMITS.maxChoiceOptions} branches`);
|
|
1249
|
+
if (judged.unsure !== undefined) {
|
|
1250
|
+
const u = judged.unsure;
|
|
1251
|
+
if (!u || typeof u.branch !== "string" || !names.includes(u.branch))
|
|
1252
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): unsure.branch must name one of the branches`);
|
|
1253
|
+
if (!(typeof u?.gte === "number" && u.gte > 0 && u.gte <= 1))
|
|
1254
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): unsure.gte must be in (0, 1]`);
|
|
1255
|
+
}
|
|
978
1256
|
}
|
|
979
1257
|
if (containsReportNode(node))
|
|
980
|
-
|
|
1258
|
+
fail("STRUCTURE_INVALID", ptrOf(path), `${path} (${label}): a report node cannot live inside a route branch — the report is rendered once, after the record is final`);
|
|
981
1259
|
if (node.as !== undefined && (typeof node.as !== "string" || !node.as.trim()))
|
|
982
|
-
|
|
1260
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): as must be a state key when present`);
|
|
983
1261
|
checkRequires({ label, requires: node.requires }, path);
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
reachability
|
|
1262
|
+
// The route writes its choice under `as` (and a judged route its answers under `<as>$answers`) before the branch runs.
|
|
1263
|
+
if (typeof node.as === "string" && node.as.trim())
|
|
1264
|
+
for (const key of byValue ? [node.as] : [node.as, `${node.as}$answers`]) {
|
|
1265
|
+
reachability?.available.add(key);
|
|
1266
|
+
typedOutputs.delete(key);
|
|
1267
|
+
probedSubtrees.delete(key);
|
|
988
1268
|
}
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
const
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
1269
|
+
{
|
|
1270
|
+
// Each branch starts from the state the route leaves it, never from a sibling's; the state after is what every branch leaves.
|
|
1271
|
+
const before = snapshotKnowledge();
|
|
1272
|
+
const after = [];
|
|
1273
|
+
for (const name of names) {
|
|
1274
|
+
const branch = raw.branches[name];
|
|
1275
|
+
if (!branch || typeof branch !== "object" || !branch.body) {
|
|
1276
|
+
fail("STRUCTURE_INVALID", ptrOf(path), `${path} (${label}): branch "${name}" needs a body`);
|
|
1277
|
+
continue;
|
|
1278
|
+
}
|
|
1279
|
+
if (byValue && branch.criteria !== undefined)
|
|
1280
|
+
fail("NODE_KEY_UNKNOWN", ptrOf(path), `${path} (${label}): branch "${name}" criteria are for Jev; a route by valuePath matches the branch name exactly`);
|
|
1281
|
+
else if (branch.criteria !== undefined && typeof branch.criteria !== "string")
|
|
1282
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): branch "${name}" criteria must be a string`);
|
|
1283
|
+
restoreKnowledge(before);
|
|
1284
|
+
walk(branch.body, `${path}.branches.${name}`, `${ptr}/branches/${pointerKey(name)}/body`);
|
|
1285
|
+
after.push(snapshotKnowledge());
|
|
996
1286
|
}
|
|
997
|
-
|
|
998
|
-
errors.push(`${path} (${label}): branch "${name}" criteria must be a string`);
|
|
999
|
-
walk(branch.body, `${path}.branches.${name}`);
|
|
1287
|
+
routeKnowledge(before, after);
|
|
1000
1288
|
}
|
|
1001
1289
|
return;
|
|
1002
1290
|
}
|
|
@@ -1004,14 +1292,14 @@ export function validateWorkflow(workflow, opts) {
|
|
|
1004
1292
|
const label = node.label || "artifact";
|
|
1005
1293
|
const type = typeof node.type === "string" && node.type.trim() ? node.type : null;
|
|
1006
1294
|
if (!type)
|
|
1007
|
-
|
|
1295
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): artifact type must be a non-empty string`);
|
|
1008
1296
|
for (const key of ["instructions", "state", "sopSection", "tools", "effort", "thinking"])
|
|
1009
1297
|
if (node[key] !== undefined)
|
|
1010
|
-
|
|
1298
|
+
fail("NODE_KEY_UNKNOWN", ptrOf(path), `${path} (${label}): ${key} is a prose-artifact field; a ${type ?? "file"} artifact has no model in the loop`);
|
|
1011
1299
|
if (typeof node.path !== "string" || !node.path.trim() || node.path.startsWith("/") || node.path.split("/").includes(".."))
|
|
1012
|
-
|
|
1300
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): path must be a workspace-relative file`);
|
|
1013
1301
|
else if (reachability && !producedFiles.has(node.path))
|
|
1014
|
-
|
|
1302
|
+
fail("STATE_PATH_UNPRODUCED", ptrOf(path), `${path} (${label}): path "${node.path}" is not produced by any earlier shell call (declare it in that call's produces)`);
|
|
1015
1303
|
checkRequires({ label, requires: node.requires }, path);
|
|
1016
1304
|
if (reachability) {
|
|
1017
1305
|
reachability.available.add("artifact");
|
|
@@ -1022,25 +1310,26 @@ export function validateWorkflow(workflow, opts) {
|
|
|
1022
1310
|
case "workflow": {
|
|
1023
1311
|
const label = typeof node.label === "string" && node.label.trim() ? node.label : "workflow";
|
|
1024
1312
|
if (typeof node.label !== "string" || !node.label.trim() || typeof node.as !== "string" || !node.as.trim())
|
|
1025
|
-
|
|
1313
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): a workflow invocation requires label and as`);
|
|
1026
1314
|
if (!Object.hasOwn(workflow.schemas, node.out))
|
|
1027
|
-
|
|
1315
|
+
fail("SCHEMA_UNKNOWN", ptrOf(path), `${path} (${label}): out schema "${String(node.out)}" not in workflow.schemas`);
|
|
1028
1316
|
if (!node.input || typeof node.input !== "object" || Array.isArray(node.input))
|
|
1029
|
-
|
|
1317
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): input must be an object mapping onto the child's initial state`);
|
|
1030
1318
|
else if (Object.hasOwn(node.input, HOST_STATE_KEY))
|
|
1031
|
-
|
|
1319
|
+
fail("STATE_KEY_RESERVED", ptrOf(path), `${path} (${label}): input must not contain the reserved "${HOST_STATE_KEY}" key`);
|
|
1032
1320
|
const child = node.workflow;
|
|
1033
1321
|
const steps = childSteps(child);
|
|
1034
1322
|
if (!child || typeof child !== "object" || !steps || !steps.length)
|
|
1035
|
-
|
|
1323
|
+
fail("STRUCTURE_INVALID", ptrOf(path), `${path} (${label}): the child workflow must contain a root node`);
|
|
1036
1324
|
else {
|
|
1037
1325
|
if (containsReportNode(child.root))
|
|
1038
|
-
|
|
1326
|
+
fail("STRUCTURE_INVALID", ptrOf(path), `${path} (${label}): a child cannot render a report or deliver an artifact; the parent owns the terminal`);
|
|
1039
1327
|
if (typeof child.input?.schemaId !== "string" || !Object.hasOwn(child.schemas, child.input.schemaId))
|
|
1040
|
-
|
|
1041
|
-
const validated = validateWorkflow(child, { executeCode: opts?.executeCode, inputKeys: Object.keys(node.input && typeof node.input === "object" ? node.input : {}) });
|
|
1328
|
+
fail("WORKFLOW_FIELD_INVALID", `${ptrOf(path)}/workflow`, `${path} (${label}): the child must declare input.schemaId in its own schemas`);
|
|
1329
|
+
const validated = validateWorkflow(child, { executeCode: opts?.executeCode, inputKeys: Object.keys(node.input && typeof node.input === "object" ? node.input : {}), ...(opts?.tools ? { tools: opts.tools } : {}) });
|
|
1042
1330
|
if (!validated.ok)
|
|
1043
|
-
|
|
1331
|
+
for (const issue of validated.issues)
|
|
1332
|
+
fail(issue.code, `${ptr}/workflow${issue.path}`, `${path} (${label}) child: ${issue.message}`);
|
|
1044
1333
|
for (const step of steps)
|
|
1045
1334
|
if (step.node === "call" && Array.isArray(step.produces))
|
|
1046
1335
|
for (const file of step.produces)
|
|
@@ -1057,91 +1346,91 @@ export function validateWorkflow(workflow, opts) {
|
|
|
1057
1346
|
case "call": {
|
|
1058
1347
|
const label = node.label || "call";
|
|
1059
1348
|
if (!CALL_TRANSPORTS.includes(String(node.via)))
|
|
1060
|
-
|
|
1349
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): via must be tool|executor|shell`);
|
|
1061
1350
|
if (node.via === "tool") {
|
|
1062
1351
|
if (!node.tool?.trim())
|
|
1063
|
-
|
|
1352
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): via tool needs a tool address`);
|
|
1064
1353
|
if (node.args !== undefined && (!node.args || typeof node.args !== "object" || Array.isArray(node.args)))
|
|
1065
|
-
|
|
1354
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): args must be an object`);
|
|
1066
1355
|
for (const key of ["code", "input", "command", "env"])
|
|
1067
1356
|
if (node[key] !== undefined)
|
|
1068
|
-
|
|
1357
|
+
fail("NODE_KEY_UNKNOWN", ptrOf(path), `${path} (${label}): ${key} is not a via tool field`);
|
|
1069
1358
|
}
|
|
1070
1359
|
if (node.via === "executor") {
|
|
1071
1360
|
if (!node.code?.trim())
|
|
1072
|
-
|
|
1361
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): via executor needs a code body`);
|
|
1073
1362
|
if (!/\breturn\b/.test(String(node.code || "")))
|
|
1074
|
-
|
|
1363
|
+
fail("CODE_INVALID", ptrOf(path), `${path} (${label}): the executor body must return its JSON result`);
|
|
1075
1364
|
if (/\b(require|process|fetch|eval|Function|import)\s*\(?/.test(String(node.code || "")))
|
|
1076
|
-
|
|
1365
|
+
fail("CODE_INVALID", ptrOf(path), `${path} (${label}): the executor body may use only tools and input (no require/process/fetch/eval/import)`);
|
|
1077
1366
|
if (node.input !== undefined && (!node.input || typeof node.input !== "object" || Array.isArray(node.input)))
|
|
1078
|
-
|
|
1367
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): input must be an object`);
|
|
1079
1368
|
for (const key of ["tool", "args", "command", "env"])
|
|
1080
1369
|
if (node[key] !== undefined)
|
|
1081
|
-
|
|
1370
|
+
fail("NODE_KEY_UNKNOWN", ptrOf(path), `${path} (${label}): ${key} is not a via executor field`);
|
|
1082
1371
|
}
|
|
1083
1372
|
if (node.via === "shell") {
|
|
1084
1373
|
if (!node.command?.trim())
|
|
1085
|
-
|
|
1374
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): via shell needs a command`);
|
|
1086
1375
|
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")))
|
|
1087
|
-
|
|
1376
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): env must map UPPER_CASE names to strings`);
|
|
1088
1377
|
if (node.out !== undefined)
|
|
1089
|
-
|
|
1378
|
+
fail("NODE_KEY_UNKNOWN", ptrOf(path), `${path} (${label}): a shell call's result is the fixed shell shape; drop out`);
|
|
1090
1379
|
for (const key of ["tool", "args", "code", "input"])
|
|
1091
1380
|
if (node[key] !== undefined)
|
|
1092
|
-
|
|
1381
|
+
fail("NODE_KEY_UNKNOWN", ptrOf(path), `${path} (${label}): ${key} is not a via shell field`);
|
|
1093
1382
|
}
|
|
1094
1383
|
else if (node.produces?.length) {
|
|
1095
|
-
|
|
1384
|
+
fail("NODE_KEY_UNKNOWN", ptrOf(path), `${path} (${label}): only a via shell call may declare produces`);
|
|
1096
1385
|
}
|
|
1097
1386
|
if (node.via !== "shell") {
|
|
1098
1387
|
if (!node.out)
|
|
1099
|
-
|
|
1388
|
+
fail("SCHEMA_UNKNOWN", ptrOf(path), `${path} (${label}): out schema required for a ${node.via} call`);
|
|
1100
1389
|
else if (!Object.hasOwn(workflow.schemas, node.out))
|
|
1101
|
-
|
|
1390
|
+
fail("SCHEMA_UNKNOWN", ptrOf(path), `${path} (${label}): out schema "${node.out}" not in workflow.schemas`);
|
|
1102
1391
|
}
|
|
1103
1392
|
if (!node.as?.trim())
|
|
1104
|
-
|
|
1393
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): as required`);
|
|
1105
1394
|
if (node.where !== undefined && node.where !== "sandbox")
|
|
1106
|
-
|
|
1107
|
-
if (!(Number.isFinite(node.deadline_s) && node.deadline_s > 0
|
|
1108
|
-
|
|
1395
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): where accepts only "sandbox"`);
|
|
1396
|
+
if (!(Number.isFinite(node.deadline_s) && node.deadline_s > 0))
|
|
1397
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): deadline_s is required and must be a finite number greater than 0 seconds`);
|
|
1109
1398
|
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(".."))))
|
|
1110
|
-
|
|
1399
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): produces must be non-empty workspace-relative paths`);
|
|
1111
1400
|
else
|
|
1112
1401
|
for (const f of node.produces || [])
|
|
1113
1402
|
producedFiles.add(f);
|
|
1114
1403
|
if (node.retry !== undefined) {
|
|
1115
1404
|
const r = node.retry;
|
|
1116
|
-
if (!r || !Number.isInteger(r.attempts) || r.attempts < 1
|
|
1117
|
-
|
|
1118
|
-
if (r?.backoff_s !== undefined && !(Number.isFinite(r.backoff_s) && r.backoff_s >= 0
|
|
1119
|
-
|
|
1120
|
-
if (r?.on !== undefined && (!Array.isArray(r.on) || r.on.some((c) => !
|
|
1121
|
-
|
|
1405
|
+
if (!r || !Number.isInteger(r.attempts) || r.attempts < 1)
|
|
1406
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): retry.attempts must be an integer >= 1`);
|
|
1407
|
+
if (r?.backoff_s !== undefined && !(Number.isFinite(r.backoff_s) && r.backoff_s >= 0))
|
|
1408
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): retry.backoff_s must be a finite number >= 0`);
|
|
1409
|
+
if (r?.on !== undefined && (!Array.isArray(r.on) || r.on.some((c) => !RETRY_ON_NAMES.includes(String(c)))))
|
|
1410
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): retry.on may name only ${RETRY_ON_NAMES.join("|")}`);
|
|
1122
1411
|
}
|
|
1123
1412
|
if (node.poll !== undefined) {
|
|
1124
1413
|
const p = node.poll;
|
|
1125
1414
|
if (!p || typeof p !== "object" || Array.isArray(p))
|
|
1126
|
-
|
|
1415
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): poll must be an object`);
|
|
1127
1416
|
else {
|
|
1128
1417
|
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;
|
|
1129
1418
|
for (const [name, pred] of [["until", p.until], ["fail_when", p.fail_when]]) {
|
|
1130
1419
|
if (pred === undefined) {
|
|
1131
1420
|
if (name === "until")
|
|
1132
|
-
|
|
1421
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): poll.until is required`);
|
|
1133
1422
|
continue;
|
|
1134
1423
|
}
|
|
1135
|
-
if (!checkPredicate(pred, label, path, `poll.${name}`,
|
|
1424
|
+
if (!checkPredicate(pred, label, path, `poll.${name}`, "poll"))
|
|
1136
1425
|
continue;
|
|
1137
|
-
const predPath = pred.path;
|
|
1426
|
+
const predPath = negated(pred).path;
|
|
1138
1427
|
if (resultShape && typeof resultShape === "object" && predPath && resultPathMissing(resultShape, predPath))
|
|
1139
|
-
|
|
1428
|
+
fail("STATE_PATH_EXCLUDED", ptrOf(path), `${path} (${label}): poll.${name} path "${predPath}" is not in the declared result shape (${Object.keys(resultShape).join(", ")}); the poll could never settle`);
|
|
1140
1429
|
}
|
|
1141
|
-
if (!(Number.isFinite(p.interval_s) && p.interval_s >= 0.1
|
|
1142
|
-
|
|
1143
|
-
if (!(Number.isFinite(p.deadline_s) && p.deadline_s >= (Number(node.deadline_s) || 0)
|
|
1144
|
-
|
|
1430
|
+
if (!(Number.isFinite(p.interval_s) && p.interval_s >= 0.1))
|
|
1431
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): poll.interval_s must be a finite number of at least 0.1 seconds`);
|
|
1432
|
+
if (!(Number.isFinite(p.deadline_s) && p.deadline_s >= (Number(node.deadline_s) || 0)))
|
|
1433
|
+
fail("NODE_FIELD_INVALID", ptrOf(path), `${path} (${label}): poll.deadline_s must be a finite number at least deadline_s`);
|
|
1145
1434
|
}
|
|
1146
1435
|
}
|
|
1147
1436
|
checkRequires({ label, requires: node.requires }, path);
|
|
@@ -1158,7 +1447,7 @@ export function validateWorkflow(workflow, opts) {
|
|
|
1158
1447
|
}
|
|
1159
1448
|
}
|
|
1160
1449
|
};
|
|
1161
|
-
walk(workflow?.root, "root");
|
|
1450
|
+
walk(workflow?.root, "root", "/root");
|
|
1162
1451
|
{
|
|
1163
1452
|
const countReports = (node) => {
|
|
1164
1453
|
if (!node || typeof node !== "object")
|
|
@@ -1177,39 +1466,42 @@ export function validateWorkflow(workflow, opts) {
|
|
|
1177
1466
|
};
|
|
1178
1467
|
const total = countReports(workflow?.root);
|
|
1179
1468
|
if (total > 1)
|
|
1180
|
-
|
|
1469
|
+
fail("STRUCTURE_INVALID", "/root", `root: at most ONE terminal node (report or artifact) — a second rendering overwrites the first at its fixed state key`);
|
|
1181
1470
|
else if (total === 1) {
|
|
1182
1471
|
const rootSteps = workflow?.root?.node === "chain" ? workflow.root.steps || [] : [workflow?.root];
|
|
1183
1472
|
const lastStep = rootSteps[rootSteps.length - 1];
|
|
1184
1473
|
if (!lastStep || (lastStep.node !== "report" && lastStep.node !== "artifact")) {
|
|
1185
|
-
|
|
1474
|
+
fail("STRUCTURE_INVALID", "/root", `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`);
|
|
1186
1475
|
}
|
|
1187
1476
|
}
|
|
1188
1477
|
}
|
|
1189
1478
|
{
|
|
1479
|
+
// Each placement with its pointer: one node object a TypeScript author placed twice is two nodes of the document.
|
|
1190
1480
|
const llmNodes = [];
|
|
1191
|
-
const collect = (node) => {
|
|
1481
|
+
const collect = (node, ptr) => {
|
|
1192
1482
|
if (!node || typeof node !== "object")
|
|
1193
1483
|
return;
|
|
1194
1484
|
if (node.node === "agent" || node.node === "decide" || node.node === "extract" || node.node === "report")
|
|
1195
|
-
llmNodes.push(node);
|
|
1485
|
+
llmNodes.push({ node, ptr });
|
|
1196
1486
|
if (node.node === "chain")
|
|
1197
|
-
(node.steps || []).forEach(collect);
|
|
1487
|
+
(node.steps || []).forEach((step, i) => collect(step, `${ptr}/steps/${i}`));
|
|
1198
1488
|
if (node.node === "parallel")
|
|
1199
|
-
(node.branches || []).forEach(collect);
|
|
1489
|
+
(node.branches || []).forEach((branch, i) => collect(branch, `${ptr}/branches/${i}`));
|
|
1200
1490
|
if (node.node === "map" || node.node === "loop")
|
|
1201
|
-
collect(node.body);
|
|
1491
|
+
collect(node.body, `${ptr}/body`);
|
|
1202
1492
|
if (node.node === "route")
|
|
1203
|
-
|
|
1493
|
+
for (const [name, branch] of Object.entries(node.branches || {}))
|
|
1494
|
+
if (branch?.body)
|
|
1495
|
+
collect(branch.body, `${ptr}/branches/${pointerKey(name)}/body`);
|
|
1204
1496
|
};
|
|
1205
|
-
collect(workflow?.root);
|
|
1206
|
-
if (llmNodes.some((
|
|
1207
|
-
for (const node of llmNodes) {
|
|
1497
|
+
collect(workflow?.root, "/root");
|
|
1498
|
+
if (llmNodes.some(({ node }) => node.node === "report")) {
|
|
1499
|
+
for (const { node, ptr } of llmNodes) {
|
|
1208
1500
|
if (node.node === "report" || node.node === "agent")
|
|
1209
1501
|
continue;
|
|
1210
1502
|
const properties = (Object.hasOwn(workflow.schemas, node.out) ? workflow.schemas[node.out] : undefined)?.properties;
|
|
1211
1503
|
if (properties && Object.prototype.hasOwnProperty.call(properties, "report_markdown")) {
|
|
1212
|
-
|
|
1504
|
+
fail("STRUCTURE_INVALID", ptr, `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}"`);
|
|
1213
1505
|
}
|
|
1214
1506
|
}
|
|
1215
1507
|
}
|
|
@@ -1217,9 +1509,9 @@ export function validateWorkflow(workflow, opts) {
|
|
|
1217
1509
|
if (reachability && !reachability.unknowable && !mayEscalate && workflow?.output?.path) {
|
|
1218
1510
|
const head = workflow.output.path.split(".")[0];
|
|
1219
1511
|
if (!reachability.available.has(head))
|
|
1220
|
-
|
|
1512
|
+
fail("STATE_PATH_UNPRODUCED", "", `output: path "${workflow.output.path}" has no input or upstream producer; select the final node's as key (or label when as is omitted)`);
|
|
1221
1513
|
else
|
|
1222
|
-
checkTypedPath(workflow.output.path, "output");
|
|
1514
|
+
checkTypedPath(workflow.output.path, "output", "");
|
|
1223
1515
|
}
|
|
1224
1516
|
// Output-completeness: when the node writing the output path is a probeable code node, its
|
|
1225
1517
|
// probed shape must carry every REQUIRED key of the output schema. A required field added to
|
|
@@ -1265,13 +1557,15 @@ export function validateWorkflow(workflow, opts) {
|
|
|
1265
1557
|
bestMissing = missing;
|
|
1266
1558
|
}
|
|
1267
1559
|
if (bestMissing?.length) {
|
|
1268
|
-
|
|
1560
|
+
fail("STATE_PATH_EXCLUDED", "", `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`);
|
|
1269
1561
|
}
|
|
1270
1562
|
}
|
|
1271
1563
|
}
|
|
1272
1564
|
if (!errors.length)
|
|
1273
|
-
|
|
1274
|
-
|
|
1565
|
+
for (const issue of workflowShapeIssues(workflow, true))
|
|
1566
|
+
fail("SHAPE_INVALID", issue.path, issue.message);
|
|
1567
|
+
const warnings = workflowWarnings(workflow);
|
|
1568
|
+
return errors.length ? { ok: false, errors, issues, ...(warnings.length ? { warnings } : {}) } : { ok: true, ...(warnings.length ? { warnings } : {}) };
|
|
1275
1569
|
}
|
|
1276
1570
|
function interpolate(template, state) {
|
|
1277
1571
|
return template.replace(/\{([a-zA-Z0-9_.$]+)\}/g, (_, p) => {
|
|
@@ -1292,17 +1586,18 @@ async function evaluatePredicate(pred, state, deps, at) {
|
|
|
1292
1586
|
if (pred.predicate !== "ask")
|
|
1293
1587
|
return { holds: predicateMatches(pred, state) };
|
|
1294
1588
|
const ask = pred;
|
|
1295
|
-
const runJudge = requireJudge(deps, `${at.kind} (ask predicate)`, at.label);
|
|
1296
1589
|
const asked = ask.state ? interpolateValue(ask.state, state, at.label) : promptStateOf(state);
|
|
1297
1590
|
const questions = { holds: { type: "noul", instructions: ask.instructions, ...(ask.criteria ? { criteria: ask.criteria } : {}) } };
|
|
1298
|
-
const result = await runJudge({ label: at.label, kind: "ask", state: asked, questions, signal: deps.signal });
|
|
1299
|
-
validateAnswers(questions, result.answers);
|
|
1300
|
-
const answer = result.answers.holds;
|
|
1301
|
-
const p = answer?.type === "noul" ? answer.noul : NaN;
|
|
1302
|
-
if (!Number.isFinite(p))
|
|
1303
|
-
throw new Error(`${at.kind} node "${at.label}": the ask predicate got no yes/no answer`);
|
|
1304
1591
|
const gte = ask.gte ?? 0.6;
|
|
1305
|
-
deps
|
|
1592
|
+
const decided = await decide(deps, { workflowState: state, nodeKind: `${at.kind} (ask predicate)`, kind: "ask", judgeKind: "ask", label: at.label, state: asked, questions, decision: decisionOf(ask), images: resolveImages(ask.images, state, at.label) }, (answers) => {
|
|
1593
|
+
const answer = answers.holds;
|
|
1594
|
+
const p = answer?.type === "noul" ? answer.noul : NaN;
|
|
1595
|
+
if (!Number.isFinite(p))
|
|
1596
|
+
throw new Error(`${at.kind} node "${at.label}": the ask predicate got no yes/no answer`);
|
|
1597
|
+
return { rule: { kind: "ask", gte }, action: { holds: p >= gte }, value: p };
|
|
1598
|
+
});
|
|
1599
|
+
const p = decided.value;
|
|
1600
|
+
deps.onEvent?.({ type: "ask.evaluated", label: at.label, detail: { kind: at.kind, p_yes: +p.toFixed(4), gte, holds: p >= gte, ...meteringOf(decided), decision_id: decided.id } });
|
|
1306
1601
|
return { holds: p >= gte, detail: { p_yes: p, gte } };
|
|
1307
1602
|
}
|
|
1308
1603
|
function promptStateOf(state) {
|
|
@@ -1324,12 +1619,15 @@ export function schemaProblems(validator, candidate) {
|
|
|
1324
1619
|
export class WorkflowInvalidError extends Error {
|
|
1325
1620
|
workflowName;
|
|
1326
1621
|
problems;
|
|
1622
|
+
issues;
|
|
1327
1623
|
code = "workflow_invalid";
|
|
1328
1624
|
stage = "validate";
|
|
1329
|
-
|
|
1625
|
+
/** `problems` are validateWorkflow's `errors`; `issues` its issues, one per problem in the same order. */
|
|
1626
|
+
constructor(workflowName, problems, issues = []) {
|
|
1330
1627
|
super(`workflow "${workflowName}" is invalid: ${problems.join("; ")}`);
|
|
1331
1628
|
this.workflowName = workflowName;
|
|
1332
1629
|
this.problems = problems;
|
|
1630
|
+
this.issues = issues;
|
|
1333
1631
|
this.name = "WorkflowInvalidError";
|
|
1334
1632
|
}
|
|
1335
1633
|
}
|
|
@@ -1417,6 +1715,60 @@ export class WorkflowStateError extends Error {
|
|
|
1417
1715
|
this.name = "WorkflowStateError";
|
|
1418
1716
|
}
|
|
1419
1717
|
}
|
|
1718
|
+
/** Decision provider failures a collecting map ends one item on: the item's request failed, and the others go on. Not
|
|
1719
|
+
* `configuration` or `auth` (every item would fail the same way), `aborted`, or `outcome_unknown` (which is reviewed). */
|
|
1720
|
+
const ITEM_DECISION_FAILURES = new Set(["rate_limited", "http", "invalid_response", "timeout", "invalid_request", "connection"]);
|
|
1721
|
+
/** Decision escalations a collecting map ends one item on, marked for review: the run goes on with the other items. */
|
|
1722
|
+
const ITEM_DECISION_REVIEWS = new Set([DECISION_INTERRUPTED, DECISION_REFUSED]);
|
|
1723
|
+
/** The escalation a collecting map takes as one item's review, if `error` is one. */
|
|
1724
|
+
function itemReview(error) {
|
|
1725
|
+
return error instanceof EscalationSignal && error.escalation.stage === "decision" && ITEM_DECISION_REVIEWS.has(error.escalation.kind) ? error : undefined;
|
|
1726
|
+
}
|
|
1727
|
+
/** Whether an item's failure is one a map with `onItemFailure: "collect"` ends the item on: one of the interpreter's own
|
|
1728
|
+
* typed node failures (a call that failed after its retries, output that does not match its schema, a state contract, a
|
|
1729
|
+
* code transform, a failed verification), or a host's failure that declares itself one, with a string `code` and
|
|
1730
|
+
* `itemFailure: true`. Never one whose cause chain holds an effect whose outcome is unknown, a recovery failure, an
|
|
1731
|
+
* escalation or an abort, never several failures at once, and nothing else: a new error class is never collected by
|
|
1732
|
+
* accident. */
|
|
1733
|
+
export function isItemFailure(error) {
|
|
1734
|
+
if (!(error instanceof Error) || error instanceof AggregateError || isRecoveryError(error))
|
|
1735
|
+
return false;
|
|
1736
|
+
for (let current = error, depth = 0; current && depth < 16; current = current.cause, depth += 1) {
|
|
1737
|
+
if (current instanceof EffectOutcomeUnknownError || current instanceof EscalationSignal || current instanceof AggregateError
|
|
1738
|
+
|| current.name === "AbortError")
|
|
1739
|
+
return false;
|
|
1740
|
+
}
|
|
1741
|
+
if (error instanceof EffectFailure || error instanceof WorkflowOutputInvalidError || error instanceof WorkflowStateError
|
|
1742
|
+
|| error instanceof WorkflowCodeError || error instanceof WorkflowVerificationError)
|
|
1743
|
+
return true;
|
|
1744
|
+
if (error instanceof DecisionProviderError)
|
|
1745
|
+
return ITEM_DECISION_FAILURES.has(error.code);
|
|
1746
|
+
return typeof error.code === "string" && error.itemFailure === true;
|
|
1747
|
+
}
|
|
1748
|
+
/** The failure a structure reports when several of its branches (a parallel's, a stopping map's items) fail at once: the
|
|
1749
|
+
* first, unless that is an item failure and another is not, whichever came first. An item failure never hides a failure a
|
|
1750
|
+
* collecting map must stop on. A sibling that ended on the structure's own stop (its reason, an abort, or a decision its
|
|
1751
|
+
* provider reports `aborted`, as Jev and OpenRouter report a request cancelled in flight) is not one: a run's own cancel
|
|
1752
|
+
* still stops a collecting map, which checks its signal first. */
|
|
1753
|
+
function reportedFailure(first, others) {
|
|
1754
|
+
if (!isItemFailure(first))
|
|
1755
|
+
return first;
|
|
1756
|
+
const stopped = (error) => error?.name === "AbortError" || (error instanceof DecisionProviderError && error.code === "aborted");
|
|
1757
|
+
return others.find((error) => error !== first && !stopped(error) && !isItemFailure(error)) ?? first;
|
|
1758
|
+
}
|
|
1759
|
+
/** The label of the step an error failed in, set as the error leaves the innermost step and prefixed by every child
|
|
1760
|
+
* invocation it leaves, as the child's events are: the row of a collected item names the node that failed. */
|
|
1761
|
+
const failedSteps = new WeakMap();
|
|
1762
|
+
/** A collected item's row. A failure that left no step (the map's own `resultPath` check) is the map's. */
|
|
1763
|
+
function itemFailureRow(index, error, mapLabel) {
|
|
1764
|
+
const code = error.code;
|
|
1765
|
+
return {
|
|
1766
|
+
item_index: index,
|
|
1767
|
+
label: failedSteps.get(error) ?? mapLabel,
|
|
1768
|
+
code: typeof code === "string" ? code : "effect_failed",
|
|
1769
|
+
message: String(error.message || error).slice(0, 300),
|
|
1770
|
+
};
|
|
1771
|
+
}
|
|
1420
1772
|
export function stageSchemaForNode(workflow, node) {
|
|
1421
1773
|
if (node.node === "report")
|
|
1422
1774
|
return REPORT_SCHEMA;
|
|
@@ -1493,8 +1845,8 @@ const sleep = (ms, signal) => new Promise((resolve, reject) => {
|
|
|
1493
1845
|
reject(signal.reason ?? new Error("workflow aborted"));
|
|
1494
1846
|
return;
|
|
1495
1847
|
}
|
|
1496
|
-
const aborted = () => {
|
|
1497
|
-
const timer =
|
|
1848
|
+
const aborted = () => { timer.clear(); reject(signal?.reason ?? new Error("workflow aborted")); };
|
|
1849
|
+
const timer = setLongTimeout(() => { signal?.removeEventListener("abort", aborted); resolve(); }, ms);
|
|
1498
1850
|
signal?.addEventListener("abort", aborted, { once: true });
|
|
1499
1851
|
});
|
|
1500
1852
|
async function runCallNode(node, state, workflow, deps) {
|
|
@@ -1518,7 +1870,13 @@ async function runCallNode(node, state, workflow, deps) {
|
|
|
1518
1870
|
const validator = Compile(schema);
|
|
1519
1871
|
const attempts = Math.max(1, node.retry?.attempts ?? 1);
|
|
1520
1872
|
const retryOn = new Set(node.retry?.on ?? ["timeout", "http_5xx", "http_429", "connection", "exit"]);
|
|
1521
|
-
|
|
1873
|
+
// A poll timeout reads one way wherever its deadline lands: the loop's own check, the start of an
|
|
1874
|
+
// attempt after a late wait, or an attempt the poll budget cuts. `check` counts the checks started,
|
|
1875
|
+
// including a last check that the deadline stops before its effect begins; a recovery run passes
|
|
1876
|
+
// none and keeps the text without a count.
|
|
1877
|
+
let pollTimeout;
|
|
1878
|
+
const pollExceeded = (check) => (pollTimeout = new EffectFailure(`call node "${node.label}" poll exceeded its ${node.poll?.deadline_s}s deadline${check === undefined ? "" : ` after ${check} check(s)`}`, "timeout"));
|
|
1879
|
+
const executeOnce = async (budgetUntilMs, check) => {
|
|
1522
1880
|
let result;
|
|
1523
1881
|
let lastError;
|
|
1524
1882
|
let failed = false;
|
|
@@ -1530,10 +1888,10 @@ async function runCallNode(node, state, workflow, deps) {
|
|
|
1530
1888
|
// the original deadline before any NEW admission; it never extends the poll.
|
|
1531
1889
|
// Expired receipt reads receive the node deadline so asynchronous store IO can finish.
|
|
1532
1890
|
if (remainingMs <= 0 && !deps.recovery)
|
|
1533
|
-
throw
|
|
1891
|
+
throw pollExceeded(check);
|
|
1534
1892
|
const controller = new AbortController();
|
|
1535
1893
|
const attemptMs = Math.max(1, Math.min(node.deadline_s * 1000, remainingMs <= 0 && deps.recovery ? Infinity : remainingMs));
|
|
1536
|
-
const attemptDeadlineMs =
|
|
1894
|
+
const attemptDeadlineMs = performance.now() + attemptMs;
|
|
1537
1895
|
let settled;
|
|
1538
1896
|
let unknownOutcome;
|
|
1539
1897
|
let interruptionCheck;
|
|
@@ -1569,16 +1927,29 @@ async function runCallNode(node, state, workflow, deps) {
|
|
|
1569
1927
|
};
|
|
1570
1928
|
const onAbort = () => interrupt("cancelled", deps.signal?.reason);
|
|
1571
1929
|
deps.signal?.addEventListener("abort", onAbort, { once: true });
|
|
1572
|
-
|
|
1930
|
+
// libuv may fire a timer up to 1 ms before the requested instant. Re-read the monotonic
|
|
1931
|
+
// clock and re-arm for the remainder, so the deadline never interrupts an effect early.
|
|
1932
|
+
// setLongTimeout keeps a deadline past Node's 2^31-1 ms timer limit from overflowing.
|
|
1933
|
+
let timer;
|
|
1934
|
+
const armDeadline = (ms) => {
|
|
1935
|
+
timer = setLongTimeout(() => {
|
|
1936
|
+
const remainingMs = attemptDeadlineMs - performance.now();
|
|
1937
|
+
if (remainingMs > 0)
|
|
1938
|
+
armDeadline(Math.ceil(remainingMs));
|
|
1939
|
+
else
|
|
1940
|
+
interrupt("deadline");
|
|
1941
|
+
}, ms);
|
|
1942
|
+
};
|
|
1943
|
+
armDeadline(attemptMs);
|
|
1573
1944
|
try {
|
|
1574
1945
|
deps.onEvent?.({ type: "effect.attempt", label: node.label, detail: { via: node.via, attempt, idempotency_key: idempotencyKey } });
|
|
1575
1946
|
Promise.resolve().then(() => {
|
|
1576
1947
|
if (deps.signal?.aborted)
|
|
1577
1948
|
throw deps.signal.reason ?? new Error("workflow aborted");
|
|
1578
|
-
if (controller.signal.aborted ||
|
|
1949
|
+
if (controller.signal.aborted || performance.now() >= attemptDeadlineMs) {
|
|
1579
1950
|
throw new EffectFailure(`call node "${node.label}" deadline elapsed before effect admission`, "timeout");
|
|
1580
1951
|
}
|
|
1581
|
-
return deps.runEffect({ node: effectNode, schema, input, produces, attempt, idempotencyKey, signal: controller.signal });
|
|
1952
|
+
return deps.runEffect({ node: effectNode, schema, input, produces, attempt, idempotencyKey, signal: controller.signal, ...(Number.isFinite(attemptDeadlineMs) ? { deadlineAt: attemptDeadlineMs } : {}) });
|
|
1582
1953
|
}).then(value => finish({ status: "fulfilled", value }), reason => finish({ status: "rejected", reason }));
|
|
1583
1954
|
const outcome = await Promise.race([settlement, interrupted]);
|
|
1584
1955
|
if (outcome.status === "unknown")
|
|
@@ -1588,7 +1959,7 @@ async function runCallNode(node, state, workflow, deps) {
|
|
|
1588
1959
|
result = outcome.value;
|
|
1589
1960
|
if (deps.signal?.aborted)
|
|
1590
1961
|
throw deps.signal.reason ?? new Error("workflow aborted");
|
|
1591
|
-
if (controller.signal.aborted ||
|
|
1962
|
+
if (controller.signal.aborted || performance.now() >= attemptDeadlineMs)
|
|
1592
1963
|
throw new EffectDeadlineExceededError(node.label, result);
|
|
1593
1964
|
failed = false;
|
|
1594
1965
|
lastError = undefined;
|
|
@@ -1600,19 +1971,24 @@ async function runCallNode(node, state, workflow, deps) {
|
|
|
1600
1971
|
lastError = error instanceof EffectOutcomeUnknownError ? error
|
|
1601
1972
|
: deps.signal?.aborted ? deps.signal.reason ?? new Error("workflow aborted")
|
|
1602
1973
|
: controller.signal.aborted && !(error instanceof EffectFailure) && !(error instanceof EffectDeadlineExceededError)
|
|
1603
|
-
?
|
|
1974
|
+
? (cutByPoll ? pollExceeded(check) : new EffectFailure(`call node "${node.label}" exceeded its ${node.deadline_s}s deadline`, "timeout"))
|
|
1604
1975
|
: error;
|
|
1605
1976
|
const retryClass = lastError instanceof EffectFailure ? lastError.retryClass : null;
|
|
1977
|
+
// The row's `code` is closed: an EffectFailure's code when it is one of EFFECT_FAILURE_CODES, else
|
|
1978
|
+
// null. Any other typed error's own code (an unknown outcome, a host's receipts failure) rides
|
|
1979
|
+
// beside it as `error_code`, never as `code`.
|
|
1980
|
+
const code = lastError instanceof EffectFailure && EFFECT_FAILURE_CODES.includes(lastError.code) ? lastError.code : null;
|
|
1981
|
+
const errorCode = code === null && typeof lastError?.code === "string" ? lastError.code : null;
|
|
1606
1982
|
// Durable admission is uncertain after failure; only the host can reconcile it.
|
|
1607
|
-
const retryable = !deps.signal?.aborted && !deps.recovery && retryClass !== null && retryOn.has(retryClass) && attempt < attempts;
|
|
1608
|
-
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) } });
|
|
1983
|
+
const retryable = !deps.signal?.aborted && !deps.recovery && ((retryClass !== null && retryOn.has(retryClass)) || (code !== null && retryOn.has(code))) && attempt < attempts;
|
|
1984
|
+
deps.onEvent?.({ type: "effect.failed", label: node.label, detail: { ...(lastError instanceof EffectFailure ? lastError.detail : undefined), via: node.via, attempt, code, ...(errorCode !== null ? { error_code: errorCode } : {}), retry_class: retryClass, retrying: retryable, ...(lastError instanceof EffectOutcomeUnknownError ? { outcome: "unknown", interruption: lastError.interruption, idempotency_key: idempotencyKey } : {}), message: String(lastError?.message || lastError).slice(0, 300) } });
|
|
1609
1985
|
if (!retryable || (budgetUntilMs !== undefined && Date.now() >= budgetUntilMs))
|
|
1610
1986
|
break;
|
|
1611
1987
|
const delay = Math.min((node.retry?.backoff_s ?? 1) * 1000 * attempt, budgetUntilMs === undefined ? Infinity : Math.max(0, budgetUntilMs - Date.now()));
|
|
1612
1988
|
await sleep(delay, deps.signal);
|
|
1613
1989
|
}
|
|
1614
1990
|
finally {
|
|
1615
|
-
|
|
1991
|
+
timer?.clear();
|
|
1616
1992
|
if (interruptionCheck)
|
|
1617
1993
|
clearImmediate(interruptionCheck);
|
|
1618
1994
|
deps.signal?.removeEventListener("abort", onAbort);
|
|
@@ -1635,8 +2011,19 @@ async function runCallNode(node, state, workflow, deps) {
|
|
|
1635
2011
|
else {
|
|
1636
2012
|
const startedAt = deps.recovery?.pollStartedAt(node, undefined, executionPath(deps)) ?? Date.now();
|
|
1637
2013
|
const pollUntilMs = startedAt + node.poll.deadline_s * 1000;
|
|
2014
|
+
// Each check sends one effect.poll. Outside a recovery run, a check that does not settle reports
|
|
2015
|
+
// null only once its wait ends inside the poll deadline, so a poll timeout, wherever it lands,
|
|
2016
|
+
// ends with one "deadline" event for the last check started.
|
|
2017
|
+
const pollDeadline = (iteration, elapsedMs) => deps.onEvent?.({ type: "effect.poll", label: node.label, detail: { iteration, settled: "deadline", elapsed_ms: elapsedMs } });
|
|
1638
2018
|
for (let iteration = 1;; iteration += 1) {
|
|
1639
|
-
|
|
2019
|
+
try {
|
|
2020
|
+
result = await executeOnce(pollUntilMs, deps.recovery ? undefined : iteration);
|
|
2021
|
+
}
|
|
2022
|
+
catch (error) {
|
|
2023
|
+
if (!deps.recovery && error === pollTimeout)
|
|
2024
|
+
pollDeadline(iteration, Date.now() - startedAt);
|
|
2025
|
+
throw error;
|
|
2026
|
+
}
|
|
1640
2027
|
if (!validator.Check(result))
|
|
1641
2028
|
throw invalid(result);
|
|
1642
2029
|
const elapsedMs = Date.now() - startedAt;
|
|
@@ -1649,14 +2036,28 @@ async function runCallNode(node, state, workflow, deps) {
|
|
|
1649
2036
|
break;
|
|
1650
2037
|
}
|
|
1651
2038
|
if (!deps.recovery && elapsedMs + node.poll.interval_s * 1000 > node.poll.deadline_s * 1000) {
|
|
1652
|
-
|
|
1653
|
-
throw
|
|
2039
|
+
pollDeadline(iteration, elapsedMs);
|
|
2040
|
+
throw pollExceeded(iteration);
|
|
1654
2041
|
}
|
|
1655
|
-
deps.
|
|
1656
|
-
|
|
2042
|
+
if (deps.recovery) {
|
|
2043
|
+
deps.onEvent?.({ type: "effect.poll", label: node.label, detail: { iteration, settled: null, elapsed_ms: elapsedMs } });
|
|
1657
2044
|
await deps.recovery.wait(node, node.poll.interval_s * 1000, undefined, executionPath(deps));
|
|
1658
|
-
|
|
2045
|
+
continue;
|
|
2046
|
+
}
|
|
2047
|
+
try {
|
|
1659
2048
|
await sleep(node.poll.interval_s * 1000, deps.signal);
|
|
2049
|
+
}
|
|
2050
|
+
catch (error) {
|
|
2051
|
+
// A cancel during the wait still reports the check that finished, as null.
|
|
2052
|
+
deps.onEvent?.({ type: "effect.poll", label: node.label, detail: { iteration, settled: null, elapsed_ms: elapsedMs } });
|
|
2053
|
+
throw error;
|
|
2054
|
+
}
|
|
2055
|
+
// A wait that wakes late, at or past the poll deadline, ends the poll before another check.
|
|
2056
|
+
if (Date.now() >= pollUntilMs) {
|
|
2057
|
+
pollDeadline(iteration, elapsedMs);
|
|
2058
|
+
throw pollExceeded(iteration);
|
|
2059
|
+
}
|
|
2060
|
+
deps.onEvent?.({ type: "effect.poll", label: node.label, detail: { iteration, settled: null, elapsed_ms: elapsedMs } });
|
|
1660
2061
|
}
|
|
1661
2062
|
}
|
|
1662
2063
|
if (!validator.Check(result)) {
|
|
@@ -1670,13 +2071,62 @@ async function runCallNode(node, state, workflow, deps) {
|
|
|
1670
2071
|
}
|
|
1671
2072
|
return { ...state, [node.as]: result };
|
|
1672
2073
|
}
|
|
1673
|
-
function
|
|
2074
|
+
function questionLimit(deps) {
|
|
1674
2075
|
const limit = deps.maxQuestionsPerRequest ?? 256;
|
|
1675
2076
|
if (!Number.isSafeInteger(limit) || limit < 1)
|
|
1676
2077
|
throw new Error("maxQuestionsPerRequest must be a positive safe integer");
|
|
2078
|
+
return limit;
|
|
2079
|
+
}
|
|
2080
|
+
function stateByteLimit(deps) {
|
|
2081
|
+
const limit = deps.maxStateBytesPerRequest;
|
|
2082
|
+
if (limit === undefined)
|
|
2083
|
+
return null;
|
|
2084
|
+
if (!Number.isSafeInteger(limit) || limit < 1)
|
|
2085
|
+
throw new Error("maxStateBytesPerRequest must be a positive safe integer");
|
|
2086
|
+
return limit;
|
|
2087
|
+
}
|
|
2088
|
+
function assertQuestionCount(deps, count, label) {
|
|
2089
|
+
const limit = questionLimit(deps);
|
|
2090
|
+
stateByteLimit(deps);
|
|
1677
2091
|
if (count > limit)
|
|
1678
2092
|
throw new Error(`node "${label}": ${count} questions exceed maxQuestionsPerRequest (${limit}); reduce the collection/question set or raise the host limit`);
|
|
1679
2093
|
}
|
|
2094
|
+
const utf8Bytes = (text) => Buffer.byteLength(text, "utf8");
|
|
2095
|
+
/** A split sift's requests in flight at once: the same default as `map.maxConcurrency`. A host that
|
|
2096
|
+
* needs fewer bounds its own `runJudge`. */
|
|
2097
|
+
const SIFT_REQUESTS_IN_FLIGHT = 4;
|
|
2098
|
+
/** Splits a sift's items, in order, into requests that each fit the host's question limit and, when set,
|
|
2099
|
+
* its state byte limit: the UTF-8 length of the JSON state the judge receives, as the Jev adapter
|
|
2100
|
+
* measures it. Sizes are exact: JSON of `{...base, items: [a, b]}` is JSON of `{...base, items: []}`
|
|
2101
|
+
* plus each item's JSON plus one comma between items. */
|
|
2102
|
+
function siftChunks(deps, label, questionsPerItem, base, named) {
|
|
2103
|
+
const maxQuestions = questionLimit(deps);
|
|
2104
|
+
const maxBytes = stateByteLimit(deps);
|
|
2105
|
+
if (questionsPerItem > maxQuestions)
|
|
2106
|
+
throw new Error(`node "${label}": ${questionsPerItem} questions exceed maxQuestionsPerRequest (${maxQuestions}); reduce the question set or raise the host limit`);
|
|
2107
|
+
const perRequest = Math.floor(maxQuestions / questionsPerItem);
|
|
2108
|
+
const emptyBytes = maxBytes === null ? 0 : utf8Bytes(JSON.stringify({ ...base, items: [] }));
|
|
2109
|
+
if (maxBytes !== null && emptyBytes > maxBytes)
|
|
2110
|
+
throw new Error(`sift node "${label}": its state alone, before any item, is ${emptyBytes} bytes, over maxStateBytesPerRequest (${maxBytes}); every request carries that state, so make the sift's state smaller or raise the host limit`);
|
|
2111
|
+
const chunks = [];
|
|
2112
|
+
let chunk = [], bytes = emptyBytes;
|
|
2113
|
+
for (let i = 0; i < named.length; i += 1) {
|
|
2114
|
+
const itemBytes = maxBytes === null ? 0 : utf8Bytes(JSON.stringify(named[i]));
|
|
2115
|
+
if (maxBytes !== null && emptyBytes + itemBytes > maxBytes)
|
|
2116
|
+
throw new Error(`sift node "${label}": item ${i} alone makes a ${emptyBytes + itemBytes}-byte judge state, over maxStateBytesPerRequest (${maxBytes}); send less of each item (a code step before the sift) or raise the host limit`);
|
|
2117
|
+
const grown = bytes + itemBytes + (chunk.length ? 1 : 0);
|
|
2118
|
+
if (chunk.length && (chunk.length >= perRequest || (maxBytes !== null && grown > maxBytes))) {
|
|
2119
|
+
chunks.push(chunk);
|
|
2120
|
+
chunk = [];
|
|
2121
|
+
bytes = emptyBytes;
|
|
2122
|
+
}
|
|
2123
|
+
bytes += itemBytes + (chunk.length ? 1 : 0);
|
|
2124
|
+
chunk.push(i);
|
|
2125
|
+
}
|
|
2126
|
+
if (chunk.length)
|
|
2127
|
+
chunks.push(chunk);
|
|
2128
|
+
return chunks;
|
|
2129
|
+
}
|
|
1680
2130
|
const requireJudge = (deps, kind, label) => {
|
|
1681
2131
|
if (!deps.runJudge)
|
|
1682
2132
|
throw new Error(`${kind} node "${label}": this runner has no System One runner (deps.runJudge)`);
|
|
@@ -1685,6 +2135,215 @@ const requireJudge = (deps, kind, label) => {
|
|
|
1685
2135
|
return deps.runJudge(params);
|
|
1686
2136
|
};
|
|
1687
2137
|
};
|
|
2138
|
+
const sha256Hex = (text) => createHash("sha256").update(text).digest("hex");
|
|
2139
|
+
/** A text of any value, for a digest, in which two values that differ never read the same. JSON values are
|
|
2140
|
+
* spelled as JSON with sorted keys. Everything JSON cannot say is a bare word no JSON value produces: a
|
|
2141
|
+
* bigint as its digits and `n`, `undefined`, a non-finite number by name, a date, map or set under its
|
|
2142
|
+
* type's name, an object of another class under its constructor's name, and a reference back to an
|
|
2143
|
+
* enclosing object as `~` and how far out it is. So hashing never refuses a state a host is free to hand
|
|
2144
|
+
* its judge, and a state can share a decision id only with itself. */
|
|
2145
|
+
function digestText(value, enclosing = []) {
|
|
2146
|
+
switch (typeof value) {
|
|
2147
|
+
case "string":
|
|
2148
|
+
case "boolean": return JSON.stringify(value);
|
|
2149
|
+
case "number": return Number.isFinite(value) ? JSON.stringify(value) : String(value);
|
|
2150
|
+
case "bigint": return `${value}n`;
|
|
2151
|
+
case "undefined":
|
|
2152
|
+
case "function":
|
|
2153
|
+
case "symbol": return typeof value;
|
|
2154
|
+
}
|
|
2155
|
+
if (value === null)
|
|
2156
|
+
return "null";
|
|
2157
|
+
const object = value;
|
|
2158
|
+
const out = enclosing.lastIndexOf(object);
|
|
2159
|
+
if (out !== -1)
|
|
2160
|
+
return `~${enclosing.length - 1 - out}`;
|
|
2161
|
+
const inside = [...enclosing, object];
|
|
2162
|
+
const list = (entries) => `[${entries.map((entry) => digestText(entry, inside)).join(",")}]`;
|
|
2163
|
+
if (Array.isArray(object))
|
|
2164
|
+
return list(object);
|
|
2165
|
+
if (object instanceof Date)
|
|
2166
|
+
return `Date(${object.getTime()})`;
|
|
2167
|
+
if (object instanceof Map)
|
|
2168
|
+
return `Map${list([...object])}`;
|
|
2169
|
+
if (object instanceof Set)
|
|
2170
|
+
return `Set${list([...object])}`;
|
|
2171
|
+
if (ArrayBuffer.isView(object))
|
|
2172
|
+
return `${object.constructor.name}${list([...new Uint8Array(object.buffer, object.byteOffset, object.byteLength)])}`;
|
|
2173
|
+
const prototype = Object.getPrototypeOf(object);
|
|
2174
|
+
const name = prototype === null || prototype === Object.prototype ? "" : String(prototype.constructor?.name ?? "?");
|
|
2175
|
+
const record = object;
|
|
2176
|
+
return `${name}{${Object.keys(record).sort().map((key) => `${JSON.stringify(key)}:${digestText(record[key], inside)}`).join(",")}}`;
|
|
2177
|
+
}
|
|
2178
|
+
/** A persistence failure of a decision receipt. It fails the node: the answers were never applied. */
|
|
2179
|
+
export class WorkflowDecisionRecordError extends Error {
|
|
2180
|
+
label;
|
|
2181
|
+
decisionId;
|
|
2182
|
+
constructor(label, decisionId, cause) {
|
|
2183
|
+
super(`node "${label}": the decision receipt ${decisionId} could not be recorded: ${String(cause?.message || cause).slice(0, 300)}`, { cause });
|
|
2184
|
+
this.label = label;
|
|
2185
|
+
this.decisionId = decisionId;
|
|
2186
|
+
this.name = "WorkflowDecisionRecordError";
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2189
|
+
/** The state a receipt keeps: the value, or past DECISION_STATE_MAX_BYTES of JSON, that many bytes of its JSON text. */
|
|
2190
|
+
function stateSeen(state) {
|
|
2191
|
+
let text;
|
|
2192
|
+
try {
|
|
2193
|
+
text = JSON.stringify(state);
|
|
2194
|
+
}
|
|
2195
|
+
catch {
|
|
2196
|
+
text = undefined;
|
|
2197
|
+
}
|
|
2198
|
+
if (text === undefined)
|
|
2199
|
+
return { state: null, truncated: true };
|
|
2200
|
+
const bytes = Buffer.from(text, "utf8");
|
|
2201
|
+
if (bytes.byteLength <= DECISION_STATE_MAX_BYTES)
|
|
2202
|
+
return { state: JSON.parse(text), truncated: false };
|
|
2203
|
+
// Cut on a character boundary: a partial UTF-8 sequence at the end is dropped.
|
|
2204
|
+
return { state: bytes.subarray(0, DECISION_STATE_MAX_BYTES).toString("utf8").replace(/\uFFFD$/, ""), truncated: true };
|
|
2205
|
+
}
|
|
2206
|
+
/** A decision node's images resolved against the state, as its `state` values are: each must resolve to a non-empty
|
|
2207
|
+
* string, the image reference the host loads. Undefined when the node sends none. */
|
|
2208
|
+
function resolveImages(images, state, label) {
|
|
2209
|
+
if (!images || !Object.keys(images).length)
|
|
2210
|
+
return undefined;
|
|
2211
|
+
const out = {};
|
|
2212
|
+
for (const [token, reference] of Object.entries(images)) {
|
|
2213
|
+
const ref = interpolateValue(reference, state, label);
|
|
2214
|
+
if (typeof ref !== "string" || !ref.trim())
|
|
2215
|
+
throw new WorkflowStateError(`node "${label}": image ${token} resolves to ${ref === null ? "null" : typeof ref}, not an image reference (a non-empty string)`, label, String(reference), "missing_interpolation");
|
|
2216
|
+
out[token] = ref;
|
|
2217
|
+
}
|
|
2218
|
+
return out;
|
|
2219
|
+
}
|
|
2220
|
+
/** The decision fields a node or an `ask` predicate sets, and no others. */
|
|
2221
|
+
const decisionOf = (node) => ({
|
|
2222
|
+
...(node.provider !== undefined ? { provider: node.provider } : {}), ...(node.model !== undefined ? { model: node.model } : {}),
|
|
2223
|
+
...(node.evidence !== undefined ? { evidence: node.evidence } : {}), ...(node.timeoutMs !== undefined ? { timeoutMs: node.timeoutMs } : {}),
|
|
2224
|
+
});
|
|
2225
|
+
/** One typed-question request from identity to receipt. `settle` turns validated answers into the rule,
|
|
2226
|
+
* the action and whatever the caller applies; it throws exactly what the node would throw for answers it
|
|
2227
|
+
* refuses. In order: an `answered` receipt the host's recovery holds under the request's id answers it
|
|
2228
|
+
* with no judge call and no new receipt; otherwise the judge is asked, and the receipt (answered, failed
|
|
2229
|
+
* or cancelled) is recorded and awaited before this returns or throws. */
|
|
2230
|
+
async function decide(deps, ask, settle) {
|
|
2231
|
+
const runJudge = requireJudge(deps, ask.nodeKind, ask.label);
|
|
2232
|
+
const path = executionPath(deps);
|
|
2233
|
+
const item = deps[MAP_ITEM];
|
|
2234
|
+
const signal = ask.signal ?? deps.signal;
|
|
2235
|
+
const position = ask.request ?? { index: 0, count: 1 };
|
|
2236
|
+
const state_sha256 = sha256Hex(digestText(ask.state));
|
|
2237
|
+
const choice = ask.decision ?? {};
|
|
2238
|
+
// A pinned provider or model is part of what the decision is; an unpinned one keeps the id it always had.
|
|
2239
|
+
const pin = choice.provider !== undefined || choice.model !== undefined ? [{ provider: choice.provider ?? null, model: choice.model ?? null }] : [];
|
|
2240
|
+
const images = ask.images && Object.keys(ask.images).length ? ask.images : undefined;
|
|
2241
|
+
// Images are part of what is asked, by reference: a request without any keeps the id it always had.
|
|
2242
|
+
const id = sha256Hex(canonicalJson(["decision-v1", path, ask.kind, position.index, ask.questions, state_sha256, ...pin, ...(images ? [{ images }] : [])]));
|
|
2243
|
+
// A refused answer decodes to no value. Unless the node takes refusals itself, the run escalates before `settle` sees
|
|
2244
|
+
// it: the provider gave nothing to act on, and a person decides the case.
|
|
2245
|
+
const settleAnswers = (answers) => {
|
|
2246
|
+
const refused = refusedQuestions(answers);
|
|
2247
|
+
if (refused.length && !ask.acceptsRefusal) {
|
|
2248
|
+
throw new EscalationSignal({ kind: DECISION_REFUSED, stage: "decision", label: ask.label, executionPath: path, state: ask.workflowState,
|
|
2249
|
+
summary: `The decision "${ask.label}" was refused by its provider (${refused.map((id) => `"${id}"`).join(", ")}): there is no answer to act on, so a person decides it.` });
|
|
2250
|
+
}
|
|
2251
|
+
return settle(answers);
|
|
2252
|
+
};
|
|
2253
|
+
const recorded = deps.recovery?.decision ? await deps.recovery.decision(id) : undefined;
|
|
2254
|
+
if (recorded?.status === "answered") {
|
|
2255
|
+
validateAnswers(ask.questions, recorded.answers);
|
|
2256
|
+
const { value } = settleAnswers(recorded.answers);
|
|
2257
|
+
return { id, answers: recorded.answers, value, model: recorded.model ?? null, cost_usd: 0, replayed: true,
|
|
2258
|
+
...(recorded.evidence ? { evidence: recorded.evidence } : {}), ...(recorded.provider ? { provider: recorded.provider } : {}) };
|
|
2259
|
+
}
|
|
2260
|
+
const request = { label: ask.label, kind: ask.judgeKind, ...(item ? { item } : {}), executionPath: path, signal, state: ask.state, questions: ask.questions, decisionId: id, ...choice, ...(images ? { images } : {}) };
|
|
2261
|
+
const seen = stateSeen(ask.state);
|
|
2262
|
+
/** The images a receipt names: as the host loaded them, else by token and reference alone. */
|
|
2263
|
+
const imagesOf = (result) => {
|
|
2264
|
+
if (!images)
|
|
2265
|
+
return undefined;
|
|
2266
|
+
const loaded = Array.isArray(result?.images) ? result.images : [];
|
|
2267
|
+
return Object.entries(images).map(([token, ref]) => {
|
|
2268
|
+
const digest = loaded.find((each) => each?.token === token);
|
|
2269
|
+
return { token, ref, sha256: digest?.sha256 ?? null, mime: digest?.mime ?? null, bytes: digest?.bytes ?? null };
|
|
2270
|
+
});
|
|
2271
|
+
};
|
|
2272
|
+
/** The evidence a result carries for this node: none under "off", else what the provider gave, when it gave any. */
|
|
2273
|
+
const evidenceOf = (result) => choice.evidence !== "off" && Array.isArray(result?.evidence) && result.evidence.length ? result.evidence : undefined;
|
|
2274
|
+
const providerOf = (result) => result?.provider ?? choice.provider;
|
|
2275
|
+
const startedAt = Date.now();
|
|
2276
|
+
const receipt = (result, outcome) => {
|
|
2277
|
+
const answers = result?.answers && typeof result.answers === "object" && !Array.isArray(result.answers) ? result.answers : {};
|
|
2278
|
+
const refused = refusedQuestions(answers);
|
|
2279
|
+
const source = result?.confidence_source;
|
|
2280
|
+
return {
|
|
2281
|
+
v: DECISION_RECEIPT_VERSION, id, at: new Date().toISOString(), node: ask.label, kind: ask.kind, item: item?.index ?? null, execution_path: path, request: position,
|
|
2282
|
+
status: outcome.status, state_sha256, questions: ask.questions, answers,
|
|
2283
|
+
rule: outcome.rule, action: outcome.action,
|
|
2284
|
+
model: result?.model ?? null, usage: result?.usage ?? null, cost_usd: typeof result?.cost_usd === "number" ? result.cost_usd : null, request_sha256: result?.request_sha256 ?? null,
|
|
2285
|
+
latency_ms: Date.now() - startedAt,
|
|
2286
|
+
...(refused.length ? { refused } : {}),
|
|
2287
|
+
...(source && typeof source === "object" && !Array.isArray(source) ? { confidence_source: { ...source } } : {}),
|
|
2288
|
+
...(result?.cost_basis ? { cost_basis: result.cost_basis } : {}),
|
|
2289
|
+
...(providerOf(result) ? { provider: providerOf(result) } : {}),
|
|
2290
|
+
...(evidenceOf(result) ? { evidence: evidenceOf(result) } : {}),
|
|
2291
|
+
...(images ? { images: imagesOf(result) } : {}),
|
|
2292
|
+
state: seen.state, state_truncated: seen.truncated,
|
|
2293
|
+
...(outcome.error !== undefined ? errorOfReceipt(String(outcome.error instanceof EscalationSignal ? outcome.error.escalation.summary : outcome.error?.message || outcome.error)) : {}),
|
|
2294
|
+
};
|
|
2295
|
+
};
|
|
2296
|
+
/** Records `made`; with `failure`, the request's own failure, a recorder that also fails surfaces both. */
|
|
2297
|
+
const record = async (made, result, failure) => {
|
|
2298
|
+
if (!deps.recordDecision)
|
|
2299
|
+
return;
|
|
2300
|
+
try {
|
|
2301
|
+
await deps.recordDecision(made, { request, ...(result !== undefined ? { result } : {}) });
|
|
2302
|
+
}
|
|
2303
|
+
catch (cause) {
|
|
2304
|
+
const lost = new WorkflowDecisionRecordError(ask.label, id, cause);
|
|
2305
|
+
throw failure ? new AggregateError([failure.error, lost], `${String(failure.error?.message || failure.error)}; and ${lost.message}`) : lost;
|
|
2306
|
+
}
|
|
2307
|
+
};
|
|
2308
|
+
let result;
|
|
2309
|
+
try {
|
|
2310
|
+
result = await runJudge(request);
|
|
2311
|
+
}
|
|
2312
|
+
catch (error) {
|
|
2313
|
+
// A request sent whose outcome was lost may have been answered and billed: it is booked at the cost its provider
|
|
2314
|
+
// gives, with how that was reached, never asked again, and a person decides the case. One answered with what is
|
|
2315
|
+
// rejected was billed, and is booked at its cost, or an unknown one.
|
|
2316
|
+
const lost = error instanceof DecisionProviderError && error.code === "outcome_unknown";
|
|
2317
|
+
const charge = chargedCost(error);
|
|
2318
|
+
const spent = charge !== undefined ? { answers: {}, cost_usd: charge,
|
|
2319
|
+
...(error.costBasis !== undefined ? { cost_basis: error.costBasis } : {}) } : undefined;
|
|
2320
|
+
// The status is read before the caller reacts: a request that fails on its own is `failed`, even when
|
|
2321
|
+
// its failure is what cancels the others.
|
|
2322
|
+
const failed = receipt(spent, { status: signal?.aborted && !lost ? "cancelled" : "failed", rule: null, action: null, error });
|
|
2323
|
+
ask.onFailure?.(error);
|
|
2324
|
+
await record(failed, spent, { error });
|
|
2325
|
+
if (lost) {
|
|
2326
|
+
throw new EscalationSignal({ kind: DECISION_INTERRUPTED, stage: "decision", label: ask.label, executionPath: path, state: ask.workflowState,
|
|
2327
|
+
summary: `The decision "${ask.label}" was sent and its outcome was lost (${error.message}): it may have been answered and billed, so it is not asked again, and a person decides it.` });
|
|
2328
|
+
}
|
|
2329
|
+
throw error;
|
|
2330
|
+
}
|
|
2331
|
+
let settled;
|
|
2332
|
+
try {
|
|
2333
|
+
validateAnswers(ask.questions, result.answers);
|
|
2334
|
+
if (choice.evidence === "required" && !evidenceOf(result))
|
|
2335
|
+
throw new DecisionProviderError("invalid_response", `${ask.nodeKind} node "${ask.label}" requires evidence, and its provider's answer carries none`);
|
|
2336
|
+
settled = settleAnswers(result.answers);
|
|
2337
|
+
}
|
|
2338
|
+
catch (error) {
|
|
2339
|
+
ask.onFailure?.(error);
|
|
2340
|
+
await record(receipt(result, { status: "failed", rule: null, action: null, error }), result, { error });
|
|
2341
|
+
throw error;
|
|
2342
|
+
}
|
|
2343
|
+
await record(receipt(result, { status: "answered", rule: settled.rule, action: settled.action }), result);
|
|
2344
|
+
return { id, answers: result.answers, value: settled.value, model: result.model ?? null, cost_usd: result.cost_usd ?? null, replayed: false,
|
|
2345
|
+
...(evidenceOf(result) ? { evidence: evidenceOf(result) } : {}), ...(providerOf(result) ? { provider: providerOf(result) } : {}) };
|
|
2346
|
+
}
|
|
1688
2347
|
function questionSetOf(workflow, node) {
|
|
1689
2348
|
const schema = resolveSchemaForWorkflow(workflow, workflow.schemas[node.out]);
|
|
1690
2349
|
const compiled = compileQuestions(schema, workflow.schemas);
|
|
@@ -1702,18 +2361,22 @@ function questionSetOf(workflow, node) {
|
|
|
1702
2361
|
};
|
|
1703
2362
|
}
|
|
1704
2363
|
const describeItem = (template, state, item, index) => interpolate(template, { ...state, item, item_index: index }).replace(/\s+/g, " ").trim();
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
}
|
|
2364
|
+
/** What a decision event says of the request behind it: who answered, what it cost, and that a recorded
|
|
2365
|
+
* decision answered it when one did. */
|
|
2366
|
+
const meteringOf = (decided) => ({ model: decided.model, cost_usd: decided.cost_usd,
|
|
2367
|
+
...(decided.replayed ? { replayed: true } : {}), ...(decided.provider ? { provider: decided.provider } : {}), ...(decided.evidence ? { evidence: decided.evidence } : {}) });
|
|
2368
|
+
/** A decided node's sidecar: its answers' confidences, and the evidence it carries. */
|
|
2369
|
+
const sidecarOf = (decided) => ({ ...answersSidecar(decided.answers), ...(decided.evidence ? { evidence: decided.evidence } : {}) });
|
|
1711
2370
|
async function runJudgeNode(node, state, workflow, deps) {
|
|
1712
2371
|
assertNodeInputs(node, state);
|
|
1713
2372
|
const set = questionSetOf(workflow, node);
|
|
1714
|
-
const {
|
|
1715
|
-
|
|
1716
|
-
|
|
2373
|
+
const decided = await decide(deps, { workflowState: state, decision: decisionOf(node), images: resolveImages(node.images, state, node.label), nodeKind: "judge", kind: "judge", judgeKind: "judge", label: node.label, state: interpolateValue(node.state, state, node.label), questions: set.questions }, (answers) => {
|
|
2374
|
+
const value = set.decode(answers);
|
|
2375
|
+
return { rule: { kind: "judge" }, action: value, value };
|
|
2376
|
+
});
|
|
2377
|
+
const value = decided.value;
|
|
2378
|
+
const sidecar = sidecarOf(decided);
|
|
2379
|
+
deps.onEvent?.({ type: "judge.answered", label: node.label, detail: { kind: "judge", as: node.as, value, sidecar, ...meteringOf(decided), decision_id: decided.id } });
|
|
1717
2380
|
return { ...state, [node.as]: value, [`${node.as}$answers`]: sidecar };
|
|
1718
2381
|
}
|
|
1719
2382
|
async function runPickNode(node, state, deps) {
|
|
@@ -1736,16 +2399,20 @@ async function runPickNode(node, state, deps) {
|
|
|
1736
2399
|
const options = Object.fromEntries(items.map((item, i) => [`item_${i}`, describeItem(node.describe, state, item, i) || `item ${i}`]));
|
|
1737
2400
|
const criteria = node.allowNone ? { ...options, [NONE]: "none of the items fits" } : options;
|
|
1738
2401
|
const asked = { ...(node.state ? interpolateValue(node.state, state, node.label) : { context: promptStateOf(state) }), candidates: options };
|
|
1739
|
-
const {
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
2402
|
+
const decided = await decide(deps, { workflowState: state, decision: decisionOf(node), images: resolveImages(node.images, state, node.label), nodeKind: "pick", kind: "pick", judgeKind: "pick", label: node.label, state: asked, questions: { pick: { type: "choice", instructions: node.instructions, criteria } } }, (answers) => {
|
|
2403
|
+
const a = answers.pick;
|
|
2404
|
+
if (!a || a.type !== "choice")
|
|
2405
|
+
throw new Error(`pick node "${node.label}": no choice came back`);
|
|
2406
|
+
const none = a.choice === NONE;
|
|
2407
|
+
const index = none ? null : Object.keys(options).indexOf(a.choice);
|
|
2408
|
+
if (index !== null && index < 0)
|
|
2409
|
+
throw new Error(`pick node "${node.label}": the choice "${a.choice}" names no item`);
|
|
2410
|
+
const value = { index, item: index === null ? null : items[index], none, option: none ? null : options[a.choice] };
|
|
2411
|
+
return { rule: { kind: "pick", options: Object.keys(options), none: node.allowNone ? NONE : null }, action: { index, none }, value };
|
|
2412
|
+
});
|
|
2413
|
+
const value = decided.value;
|
|
2414
|
+
const sidecar = sidecarOf(decided);
|
|
2415
|
+
deps.onEvent?.({ type: "judge.answered", label: node.label, detail: { kind: "pick", as: node.as, value, sidecar, ...meteringOf(decided), decision_id: decided.id } });
|
|
1749
2416
|
return { ...state, [node.as]: value, [`${node.as}$answers`]: sidecar };
|
|
1750
2417
|
}
|
|
1751
2418
|
async function runSiftNode(node, state, workflow, deps) {
|
|
@@ -1755,58 +2422,145 @@ async function runSiftNode(node, state, workflow, deps) {
|
|
|
1755
2422
|
throw new WorkflowStateError(`sift node "${node.label}": ${node.itemsPath} is not a list`, node.label, node.itemsPath, "expected_list");
|
|
1756
2423
|
const set = questionSetOf(workflow, node);
|
|
1757
2424
|
const ids = Object.keys(set.questions);
|
|
1758
|
-
assertQuestionCount(deps, items.length * ids.length, node.label);
|
|
1759
2425
|
const [keepId, keepTail] = node.keep ? node.keep.path.split(".") : [];
|
|
1760
2426
|
const values = [];
|
|
1761
2427
|
const sidecars = [];
|
|
1762
2428
|
const kept = [];
|
|
1763
2429
|
let metering = null;
|
|
2430
|
+
const decisionIds = [];
|
|
1764
2431
|
if (items.length) {
|
|
1765
2432
|
const base = node.state ? interpolateValue(node.state, state, node.label) : {};
|
|
2433
|
+
const images = resolveImages(node.images, state, node.label);
|
|
1766
2434
|
const named = items.map((item, i) => ({ id: `item_${i}`, ...(node.describe ? { summary: describeItem(node.describe, state, item, i) } : {}), item }));
|
|
1767
|
-
const
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
const {
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
2435
|
+
const chunks = siftChunks(deps, node.label, ids.length, base, named);
|
|
2436
|
+
const measure = keepTail === "confidence" ? "confidence" : "value";
|
|
2437
|
+
// The keep rule, as applied and as a receipt states it: the author's threshold, else 0.5 for a yes/no
|
|
2438
|
+
// value and 0 otherwise (an answer has its question's type).
|
|
2439
|
+
const keep = node.keep ? { id: keepId, measure, gte: node.keep.gte ?? (set.questions[keepId]?.type === "noul" && measure === "value" ? 0.5 : 0) } : null;
|
|
2440
|
+
// Each chunk is its own request and its own decision: item ids stay global (item_<i>), `items[j]`
|
|
2441
|
+
// indexes the chunk, and the chunk's receipt names its kept items by that position. A sift that fits
|
|
2442
|
+
// in one request sends exactly the request it always did. Several requests run at most
|
|
2443
|
+
// SIFT_REQUESTS_IN_FLIGHT at a time; the first failure stops dispatch and cancels the requests in
|
|
2444
|
+
// flight, each of which is recorded as cancelled, and is thrown once they settle. A request that had
|
|
2445
|
+
// already answered keeps its receipt; no event names it, since the sift decided nothing.
|
|
2446
|
+
const ask = (chunk, c, signal, onFailure) => {
|
|
2447
|
+
const questions = {};
|
|
2448
|
+
chunk.forEach((i, j) => { for (const id of ids)
|
|
2449
|
+
questions[`${j}.${id}`] = { ...set.questions[id], instructions: `For \`items[${j}]\` (id item_${i}): ${set.questions[id].instructions}` }; });
|
|
2450
|
+
return decide(deps, { workflowState: state, decision: decisionOf(node), images, nodeKind: "sift", kind: "sift", judgeKind: "sift", label: node.label, state: { ...base, items: chunk.map((i) => named[i]) }, questions, signal, request: { index: c, count: chunks.length, items: chunk }, ...(onFailure ? { onFailure } : {}) }, (answers) => {
|
|
2451
|
+
const decoded = [];
|
|
2452
|
+
const sides = [];
|
|
2453
|
+
const keptHere = [];
|
|
2454
|
+
chunk.forEach((i, j) => {
|
|
2455
|
+
const mine = Object.create(null);
|
|
2456
|
+
for (const id of ids) {
|
|
2457
|
+
const a = answers[`${j}.${id}`];
|
|
2458
|
+
if (!a)
|
|
2459
|
+
throw new Error(`sift node "${node.label}": no answer for item ${i} question "${id}"`);
|
|
2460
|
+
mine[id] = a;
|
|
2461
|
+
}
|
|
2462
|
+
decoded.push(set.decode(mine, ` answers for item ${i}`));
|
|
2463
|
+
sides.push(answersSidecar(mine));
|
|
2464
|
+
if (!keep) {
|
|
2465
|
+
keptHere.push(j);
|
|
2466
|
+
return;
|
|
2467
|
+
}
|
|
2468
|
+
const a = mine[keepId];
|
|
2469
|
+
const measured = keepTail === "confidence" ? answerConfidence(a) : a.type === "noul" ? a.noul : a.type === "score" ? a.score : NaN;
|
|
2470
|
+
if (Number.isFinite(measured) && measured >= keep.gte)
|
|
2471
|
+
keptHere.push(j);
|
|
2472
|
+
});
|
|
2473
|
+
return { rule: { kind: "sift", items: chunk.length, ids, keep }, action: { kept: keptHere }, value: { values: decoded, sidecars: sides, kept: keptHere.map((j) => chunk[j]) } };
|
|
2474
|
+
});
|
|
2475
|
+
};
|
|
2476
|
+
let results;
|
|
2477
|
+
if (chunks.length === 1)
|
|
2478
|
+
results = [await ask(chunks[0], 0, deps.signal)];
|
|
2479
|
+
else {
|
|
2480
|
+
const stop = new AbortController();
|
|
2481
|
+
const signal = deps.signal ? AbortSignal.any([deps.signal, stop.signal]) : stop.signal;
|
|
2482
|
+
results = new Array(chunks.length);
|
|
2483
|
+
let next = 0, failure;
|
|
2484
|
+
// The first request to fail is the sift's failure, from the moment it fails: dispatch stops and the
|
|
2485
|
+
// requests in flight are cancelled then, not when its receipt has been written. What it finally
|
|
2486
|
+
// throws (its own error, or that and a receipt that could not be recorded) replaces what it flagged.
|
|
2487
|
+
const fail = (request, error) => { if (!failure || failure.request === request)
|
|
2488
|
+
failure = { request, error }; stop.abort(); };
|
|
2489
|
+
await Promise.all(Array.from({ length: Math.min(SIFT_REQUESTS_IN_FLIGHT, chunks.length) }, async () => {
|
|
2490
|
+
while (!failure && next < chunks.length) {
|
|
2491
|
+
const c = next++;
|
|
2492
|
+
try {
|
|
2493
|
+
results[c] = await ask(chunks[c], c, signal, (error) => fail(c, error));
|
|
2494
|
+
}
|
|
2495
|
+
catch (error) {
|
|
2496
|
+
fail(c, error);
|
|
2497
|
+
}
|
|
2498
|
+
}
|
|
2499
|
+
}));
|
|
2500
|
+
if (failure)
|
|
2501
|
+
throw failure.error;
|
|
2502
|
+
}
|
|
2503
|
+
const models = new Set(results.map((r) => r.model));
|
|
2504
|
+
const providers = new Set(results.map((r) => r.provider));
|
|
2505
|
+
const evidence = results.flatMap((r) => r.evidence ?? []);
|
|
2506
|
+
metering = {
|
|
2507
|
+
model: models.size === 1 ? results[0].model : null,
|
|
2508
|
+
cost_usd: results.every((r) => r.cost_usd !== null) ? results.reduce((sum, r) => sum + r.cost_usd, 0) : null,
|
|
2509
|
+
...(chunks.length > 1 ? { requests: chunks.length } : {}),
|
|
2510
|
+
...(results.every((r) => r.replayed) ? { replayed: true } : {}),
|
|
2511
|
+
...(providers.size === 1 && results[0].provider ? { provider: results[0].provider } : {}),
|
|
2512
|
+
...(evidence.length ? { evidence } : {}),
|
|
2513
|
+
};
|
|
2514
|
+
// Chunks cover the list in order, so their results concatenate into list order. An item's sidecar carries the
|
|
2515
|
+
// evidence of the request that answered it.
|
|
2516
|
+
for (const r of results) {
|
|
2517
|
+
values.push(...r.value.values);
|
|
2518
|
+
kept.push(...r.value.kept);
|
|
2519
|
+
decisionIds.push(r.id);
|
|
2520
|
+
sidecars.push(...r.value.sidecars.map((sidecar) => r.evidence ? { ...sidecar, evidence: r.evidence } : sidecar));
|
|
1792
2521
|
}
|
|
1793
2522
|
}
|
|
1794
2523
|
const value = { items: kept.map((i) => items[i]), values, answers: sidecars, kept };
|
|
1795
|
-
deps.onEvent?.({ type: "judge.answered", label: node.label, detail: { kind: "sift", as: node.as, value, count: items.length, kept: kept.length, ...(metering ?? {}) } });
|
|
2524
|
+
deps.onEvent?.({ type: "judge.answered", label: node.label, detail: { kind: "sift", as: node.as, value, count: items.length, kept: kept.length, ...(metering ?? {}), decision_ids: decisionIds } });
|
|
1796
2525
|
return { ...state, [node.as]: value };
|
|
1797
2526
|
}
|
|
1798
2527
|
async function runRouteNode(node, state, workflow, deps) {
|
|
1799
2528
|
assertNodeInputs(node, state);
|
|
2529
|
+
if (routesByValue(node)) {
|
|
2530
|
+
// The choice is already in state: no model is asked, and nothing is coerced into a branch name.
|
|
2531
|
+
const value = getPath(state, node.valuePath);
|
|
2532
|
+
if (value !== undefined && typeof value !== "string")
|
|
2533
|
+
throw new WorkflowStateError(`route node "${node.label}": ${node.valuePath} is not a string; a route by value reads a branch name`, node.label, node.valuePath, "route_type");
|
|
2534
|
+
const matched = typeof value === "string" && Object.hasOwn(node.branches, value);
|
|
2535
|
+
const taken = matched ? value : node.otherwise;
|
|
2536
|
+
if (taken === undefined)
|
|
2537
|
+
throw new WorkflowStateError(`route node "${node.label}": ${node.valuePath} ${value === undefined ? "is missing" : "names no branch"} and the route declares no otherwise`, node.label, node.valuePath, value === undefined ? "route_missing" : "route_unknown");
|
|
2538
|
+
const selection = { value: value ?? null, taken, fallback: !matched };
|
|
2539
|
+
// The event names only a declared branch, never an unexpected source string.
|
|
2540
|
+
deps.onEvent?.({ type: "route.chosen", label: node.label, detail: { kind: "route", by: "value", as: node.as ?? null, value: { taken, fallback: !matched } } });
|
|
2541
|
+
const routed = node.as ? { ...state, [node.as]: selection } : state;
|
|
2542
|
+
return runNodeOnState(node.branches[taken].body, routed, workflow, scopeExecution(deps, "branches", taken, "body"));
|
|
2543
|
+
}
|
|
1800
2544
|
const names = Object.keys(node.branches);
|
|
1801
2545
|
const criteria = Object.fromEntries(names.map((n) => [n, node.branches[n]?.criteria?.trim() || null]));
|
|
1802
|
-
const
|
|
1803
|
-
const
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
2546
|
+
const rule = { kind: "route", branches: [...names], unsure: node.unsure ? { gte: node.unsure.gte, branch: node.unsure.branch } : null };
|
|
2547
|
+
const decided = await decide(deps, { workflowState: state, decision: decisionOf(node), images: resolveImages(node.images, state, node.label), nodeKind: "route", kind: "route", judgeKind: "route", label: node.label, state: interpolateValue(node.state, state, node.label), questions: { branch: { type: "choice", instructions: node.instructions, criteria } }, acceptsRefusal: Boolean(node.unsure) }, (answers) => {
|
|
2548
|
+
const a = answers.branch;
|
|
2549
|
+
// A refusal takes the unsure branch, as an answer with no confidence would; it chose no branch of its own.
|
|
2550
|
+
if (a?.type === "refusal" && node.unsure) {
|
|
2551
|
+
const value = { branch: null, taken: node.unsure.branch, unsure: true, refused: true };
|
|
2552
|
+
return { rule, action: value, value };
|
|
2553
|
+
}
|
|
2554
|
+
if (!a || a.type !== "choice" || !names.includes(a.choice))
|
|
2555
|
+
throw new Error(`route node "${node.label}": the choice "${String(a?.choice)}" names no branch`);
|
|
2556
|
+
const unsure = Boolean(node.unsure && a.confidence < node.unsure.gte);
|
|
2557
|
+
const value = { branch: a.choice, taken: unsure ? node.unsure.branch : a.choice, unsure };
|
|
2558
|
+
return { rule, action: value, value };
|
|
2559
|
+
});
|
|
2560
|
+
const value = decided.value;
|
|
2561
|
+
const taken = value.taken;
|
|
2562
|
+
const sidecar = sidecarOf(decided);
|
|
2563
|
+
deps.onEvent?.({ type: "route.chosen", label: node.label, detail: { kind: "route", as: node.as ?? null, value, sidecar, ...meteringOf(decided), decision_id: decided.id } });
|
|
1810
2564
|
const routed = node.as ? { ...state, [node.as]: value, [`${node.as}$answers`]: sidecar } : state;
|
|
1811
2565
|
return runNodeOnState(node.branches[taken].body, routed, workflow, scopeExecution(deps, "branches", taken, "body"));
|
|
1812
2566
|
}
|
|
@@ -1816,6 +2570,8 @@ const SCOPED_EFFECTS = Symbol("workflow.scopedEffects");
|
|
|
1816
2570
|
const MAP_ITEM = Symbol("workflow.mapItem");
|
|
1817
2571
|
/** The reserved state key host policy writes under. Engine-owned like every `$`-prefixed key: no node may name it. */
|
|
1818
2572
|
export const HOST_STATE_KEY = "$host";
|
|
2573
|
+
/** Whether a state key is engine-owned: it holds a `$` (`$host`, `<as>$answers`, `<as>$verify`). No node may declare a write to one. */
|
|
2574
|
+
const holdsDollar = (key) => key.includes("$");
|
|
1819
2575
|
const hostStateOf = (state) => {
|
|
1820
2576
|
const host = state[HOST_STATE_KEY];
|
|
1821
2577
|
return host && typeof host === "object" && !Array.isArray(host) ? host : {};
|
|
@@ -1843,7 +2599,7 @@ function withExecutionLocation(deps) {
|
|
|
1843
2599
|
...(deps.runNode ? { runNode: params => deps.runNode({ ...params, executionPath: params.executionPath ?? path }) } : {}),
|
|
1844
2600
|
...(deps.runEffect ? { runEffect: params => deps.runEffect({ ...params, executionPath: params.executionPath ?? path }) } : {}),
|
|
1845
2601
|
...(deps.runJudge ? { runJudge: params => deps.runJudge({ ...params, executionPath: params.executionPath ?? path }) } : {}),
|
|
1846
|
-
...(deps.onEvent ? { onEvent: event =>
|
|
2602
|
+
...(deps.onEvent ? { onEvent: forwardObserver(deps.onEvent, event => ({ ...event, executionPath: event.executionPath ?? path })) } : {}),
|
|
1847
2603
|
};
|
|
1848
2604
|
}
|
|
1849
2605
|
/** Older hosts only support flat child chains outside repeated/branching structure. They must
|
|
@@ -1880,6 +2636,9 @@ function scopeDepsToItem(deps, item, signal) {
|
|
|
1880
2636
|
pollStartedAt: (node, nestedItem, path) => recovery.pollStartedAt(node, nestedItem ?? item, path),
|
|
1881
2637
|
wait: (node, ms, nestedItem, path) => recovery.wait(node, ms, nestedItem ?? item, path),
|
|
1882
2638
|
...(recovery.fail ? { fail: (node, results, error, path) => recovery.fail(node, results, error, path) } : {}),
|
|
2639
|
+
...(recovery.failItem ? { failItem: (node, failure, path) => recovery.failItem(node, failure, path) } : {}),
|
|
2640
|
+
...(recovery.itemFailure ? { itemFailure: (node, path) => recovery.itemFailure(node, path) } : {}),
|
|
2641
|
+
...(recovery.decision ? { decision: id => recovery.decision(id) } : {}),
|
|
1883
2642
|
} } : {}),
|
|
1884
2643
|
};
|
|
1885
2644
|
}
|
|
@@ -1894,6 +2653,8 @@ function scopeDepsToChild(deps, invocation) {
|
|
|
1894
2653
|
...(deps.runNode ? { runNode: params => deps.runNode({ ...params, label: `${prefix}/${params.label}` }) } : {}),
|
|
1895
2654
|
...(deps.runEffect ? { runEffect: params => deps.runEffect({ ...params, node: relabel(params.node) }) } : {}),
|
|
1896
2655
|
...(deps.runJudge ? { runJudge: params => deps.runJudge({ ...params, label: `${prefix}/${params.label}` }) } : {}),
|
|
2656
|
+
...(deps.recordDecision ? { recordDecision: (receipt, exchange) => deps.recordDecision({ ...receipt, node: `${prefix}/${receipt.node}` }, { ...exchange, request: { ...exchange.request, label: `${prefix}/${exchange.request.label}` } }) } : {}),
|
|
2657
|
+
...(deps.runCode ? { runCode: (node, state, ctx) => deps.runCode(relabel(node), state, { ...ctx, label: `${prefix}/${ctx.label}` }) } : {}),
|
|
1897
2658
|
...(recovery ? { recovery: {
|
|
1898
2659
|
supportsExecutionPaths: recovery.supportsExecutionPaths,
|
|
1899
2660
|
resume: (node, state, item, path) => recovery.resume(relabel(node), state, item, path),
|
|
@@ -1901,22 +2662,41 @@ function scopeDepsToChild(deps, invocation) {
|
|
|
1901
2662
|
pollStartedAt: (node, item, path) => recovery.pollStartedAt(relabel(node), item, path),
|
|
1902
2663
|
wait: (node, ms, item, path) => recovery.wait(relabel(node), ms, item, path),
|
|
1903
2664
|
...(recovery.fail ? { fail: (node, results, error, path) => recovery.fail(relabel(node), results, error, path) } : {}),
|
|
2665
|
+
...(recovery.failItem ? { failItem: (node, failure, path) => recovery.failItem(relabel(node), failure, path) } : {}),
|
|
2666
|
+
...(recovery.itemFailure ? { itemFailure: (node, path) => recovery.itemFailure(relabel(node), path) } : {}),
|
|
2667
|
+
...(recovery.decision ? { decision: id => recovery.decision(id) } : {}),
|
|
1904
2668
|
} } : {}),
|
|
1905
|
-
...(deps.onEvent ? { onEvent: event =>
|
|
2669
|
+
...(deps.onEvent ? { onEvent: forwardObserver(deps.onEvent, event => ({ ...event, label: `${prefix}/${event.label}` })) } : {}),
|
|
1906
2670
|
};
|
|
1907
2671
|
}
|
|
1908
2672
|
const STEP_KINDS = new Set(["agent", "decide", "extract", "report", "code", "call", "artifact", "workflow", "judge", "pick", "sift"]);
|
|
1909
2673
|
const CHECKPOINTED_STRUCTURE_KINDS = new Set(["map", "parallel", "loop", "route"]);
|
|
1910
2674
|
const guardedObservers = new WeakSet();
|
|
2675
|
+
function forwardObserver(observer, transform) {
|
|
2676
|
+
const forwarded = (event) => observer(transform(event));
|
|
2677
|
+
if (guardedObservers.has(observer))
|
|
2678
|
+
guardedObservers.add(forwarded);
|
|
2679
|
+
return forwarded;
|
|
2680
|
+
}
|
|
1911
2681
|
async function runNodeOnState(node, state, workflow, deps) {
|
|
1912
2682
|
deps = withExecutionLocation(deps);
|
|
1913
2683
|
const observer = deps.onEvent;
|
|
1914
2684
|
if (observer && !guardedObservers.has(observer)) {
|
|
1915
2685
|
const guarded = event => {
|
|
2686
|
+
let detached;
|
|
2687
|
+
try {
|
|
2688
|
+
detached = deps.prepareEvent ? deps.prepareEvent(event) : observerSnapshot(event);
|
|
2689
|
+
}
|
|
2690
|
+
catch {
|
|
2691
|
+
return;
|
|
2692
|
+
}
|
|
2693
|
+
if (detached === undefined)
|
|
2694
|
+
return;
|
|
1916
2695
|
try {
|
|
1917
2696
|
// TypeScript void callbacks can still return promises. Consume rejection without
|
|
1918
2697
|
// awaiting telemetry or allowing it to replace an execution or recovery outcome.
|
|
1919
|
-
|
|
2698
|
+
// Observers receive snapshots, never objects shared with execution state.
|
|
2699
|
+
const returned = observer(detached);
|
|
1920
2700
|
if (returned && typeof returned.then === "function") {
|
|
1921
2701
|
void Promise.resolve(returned).catch(() => { });
|
|
1922
2702
|
}
|
|
@@ -1980,6 +2760,14 @@ async function runNodeOnState(node, state, workflow, deps) {
|
|
|
1980
2760
|
catch (error) {
|
|
1981
2761
|
if (isStep)
|
|
1982
2762
|
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) } });
|
|
2763
|
+
if (error !== null && typeof error === "object") {
|
|
2764
|
+
const inner = failedSteps.get(error);
|
|
2765
|
+
// A step names itself; so does a structural node whose own failure this is (its stage is its label).
|
|
2766
|
+
if (inner === undefined && (isStep || error.stage === label))
|
|
2767
|
+
failedSteps.set(error, label);
|
|
2768
|
+
else if (inner !== undefined && node.node === "workflow")
|
|
2769
|
+
failedSteps.set(error, `${label}/${inner}`);
|
|
2770
|
+
}
|
|
1983
2771
|
throw error;
|
|
1984
2772
|
}
|
|
1985
2773
|
}
|
|
@@ -2011,12 +2799,18 @@ async function runNodeBody(node, state, workflow, deps) {
|
|
|
2011
2799
|
}
|
|
2012
2800
|
case "code": {
|
|
2013
2801
|
let out;
|
|
2014
|
-
|
|
2015
|
-
const
|
|
2016
|
-
out =
|
|
2802
|
+
if (deps.runCode) {
|
|
2803
|
+
const item = deps[MAP_ITEM];
|
|
2804
|
+
out = await deps.runCode(node, state, { sop: deps.sop || "", label: node.label, executionPath: executionPath(deps), ...(item ? { item } : {}), ...(deps.signal ? { signal: deps.signal } : {}) });
|
|
2017
2805
|
}
|
|
2018
|
-
|
|
2019
|
-
|
|
2806
|
+
else {
|
|
2807
|
+
try {
|
|
2808
|
+
const fn = compileTransform(node.code);
|
|
2809
|
+
out = fn(state, { sop: deps.sop || "" });
|
|
2810
|
+
}
|
|
2811
|
+
catch (cause) {
|
|
2812
|
+
throw new WorkflowCodeError(node.label, cause);
|
|
2813
|
+
}
|
|
2020
2814
|
}
|
|
2021
2815
|
const patch = node.as ? { [node.as]: out } : (out && typeof out === "object" && !Array.isArray(out) ? out : { [node.label]: out });
|
|
2022
2816
|
if (Object.prototype.hasOwnProperty.call(patch, HOST_STATE_KEY))
|
|
@@ -2080,6 +2874,9 @@ async function runNodeBody(node, state, workflow, deps) {
|
|
|
2080
2874
|
kind: node.node, label: node.label,
|
|
2081
2875
|
...(review ? { review } : {}),
|
|
2082
2876
|
...(node.tier ? { tier: node.tier } : {}),
|
|
2877
|
+
...(node.node === "agent" && node.budget_usd !== undefined ? { budgetUsd: node.budget_usd } : {}),
|
|
2878
|
+
...(node.node === "agent" && node.max_turns !== undefined ? { maxTurns: node.max_turns } : {}),
|
|
2879
|
+
...(node.node === "agent" && node.context !== undefined ? { context: node.context } : {}),
|
|
2083
2880
|
system: [sopSlice, deps.skill, node.instructions, ...(policy?.systemBlocks && context ? policy.systemBlocks(context) : [])].filter(Boolean),
|
|
2084
2881
|
user: JSON.stringify(promptState),
|
|
2085
2882
|
schema: transportSchema,
|
|
@@ -2105,6 +2902,10 @@ async function runNodeBody(node, state, workflow, deps) {
|
|
|
2105
2902
|
if (!Array.isArray(items))
|
|
2106
2903
|
throw new WorkflowStateError(`map node "${node.label}": ${node.itemsPath} is not a list`, node.label, node.itemsPath, "expected_list");
|
|
2107
2904
|
const results = new Array(items.length);
|
|
2905
|
+
// With `collect`, an item that ends on an item failure is a row here and `null` in its slot of `as`: the slots stay
|
|
2906
|
+
// aligned with the items, so a later node reads item i's result at i.
|
|
2907
|
+
const collect = node.onItemFailure === "collect";
|
|
2908
|
+
const rows = new Array(items.length);
|
|
2108
2909
|
let cursor = 0;
|
|
2109
2910
|
const width = Math.min(node.maxConcurrency ?? 4, Math.max(items.length, 1));
|
|
2110
2911
|
const siblings = new AbortController();
|
|
@@ -2112,6 +2913,18 @@ async function runNodeBody(node, state, workflow, deps) {
|
|
|
2112
2913
|
let failure;
|
|
2113
2914
|
let failed = false;
|
|
2114
2915
|
const uncertainties = new Set();
|
|
2916
|
+
/** Failures after the first: one the map must stop on is reported over an item failure (`reportedFailure`). */
|
|
2917
|
+
const later = [];
|
|
2918
|
+
const stop = (error) => {
|
|
2919
|
+
uncertainEffects(error, uncertainties);
|
|
2920
|
+
if (!failed) {
|
|
2921
|
+
failed = true;
|
|
2922
|
+
failure = error;
|
|
2923
|
+
siblings.abort(error);
|
|
2924
|
+
}
|
|
2925
|
+
else
|
|
2926
|
+
later.push(error);
|
|
2927
|
+
};
|
|
2115
2928
|
await Promise.all(Array.from({ length: width }, async () => {
|
|
2116
2929
|
while (!signal.aborted) {
|
|
2117
2930
|
const i = cursor++;
|
|
@@ -2122,9 +2935,18 @@ async function runNodeBody(node, state, workflow, deps) {
|
|
|
2122
2935
|
// Under recovery each item is its own child: the body's session, effects and commit
|
|
2123
2936
|
// carry the item, so a resumed map answers finished items from their rows and
|
|
2124
2937
|
// continues the ones that were running.
|
|
2938
|
+
const itemDeps = scopeDepsToItem(scopeExecution(deps, "items", i, "body"), { label: node.label, index: i }, signal);
|
|
2125
2939
|
try {
|
|
2940
|
+
// A failure an earlier process committed ends the item again: its body never runs twice.
|
|
2941
|
+
const committed = collect ? await itemDeps.recovery?.itemFailure?.(node, executionPath(itemDeps)) : undefined;
|
|
2942
|
+
// A store keeps the row's three strings; a reviewed item is known again by its code.
|
|
2943
|
+
if (committed) {
|
|
2944
|
+
rows[i] = ITEM_DECISION_REVIEWS.has(committed.code) ? { ...committed, needs_review: true } : committed;
|
|
2945
|
+
results[i] = null;
|
|
2946
|
+
continue;
|
|
2947
|
+
}
|
|
2126
2948
|
const itemState = snapshotState({ ...state, item: items[i], item_index: i });
|
|
2127
|
-
const out = await runNodeOnState(node.body, itemState, workflow,
|
|
2949
|
+
const out = await runNodeOnState(node.body, itemState, workflow, itemDeps);
|
|
2128
2950
|
if (node.resultPath !== undefined) {
|
|
2129
2951
|
const selected = node.resultPath.split(".").reduce((value, key) => value == null ? undefined : value[key], out);
|
|
2130
2952
|
if (selected === undefined)
|
|
@@ -2135,20 +2957,34 @@ async function runNodeBody(node, state, workflow, deps) {
|
|
|
2135
2957
|
results[i] = node.body.as ? out[node.body.as] : out;
|
|
2136
2958
|
}
|
|
2137
2959
|
catch (error) {
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2960
|
+
// An item failure ends its item, and a decision that went to review marks it for review; the map's stop and
|
|
2961
|
+
// every other failure stop the map.
|
|
2962
|
+
const review = collect ? itemReview(error) : undefined;
|
|
2963
|
+
if (!collect || signal.aborted || !(review || isItemFailure(error))) {
|
|
2964
|
+
stop(error);
|
|
2965
|
+
return;
|
|
2143
2966
|
}
|
|
2144
|
-
|
|
2967
|
+
const row = review
|
|
2968
|
+
? { item_index: i, label: review.escalation.label ?? node.label, code: review.escalation.kind, message: String(review.escalation.summary ?? review.message).slice(0, 300), needs_review: true }
|
|
2969
|
+
: itemFailureRow(i, error, node.label);
|
|
2970
|
+
try {
|
|
2971
|
+
await itemDeps.recovery?.failItem?.(node, row, executionPath(itemDeps));
|
|
2972
|
+
}
|
|
2973
|
+
catch (persistenceError) {
|
|
2974
|
+
stop(persistenceError);
|
|
2975
|
+
return;
|
|
2976
|
+
}
|
|
2977
|
+
rows[i] = row;
|
|
2978
|
+
results[i] = null;
|
|
2979
|
+
deps.onEvent?.({ type: "map.item_failed", label: node.label, detail: { item_index: i, label: row.label, code: row.code, ...(row.needs_review ? { needs_review: true } : {}) } });
|
|
2145
2980
|
}
|
|
2146
2981
|
}
|
|
2147
2982
|
}));
|
|
2148
2983
|
if (failed) {
|
|
2149
|
-
failure = withSiblingUncertainty(failure, uncertainties);
|
|
2984
|
+
failure = withSiblingUncertainty(reportedFailure(failure, later), uncertainties);
|
|
2150
2985
|
const escalated = failure instanceof EscalationSignal;
|
|
2151
|
-
|
|
2986
|
+
const collected = rows.filter(Boolean).length;
|
|
2987
|
+
deps.onEvent?.({ type: escalated ? "map.escalated" : "map.failed", label: node.label, detail: { completed: results.reduce((n, r) => n + (r === undefined ? 0 : 1), 0) - collected, of: items.length, ...(collect ? { collected } : {}), message: String(failure?.message || failure).slice(0, 300) } });
|
|
2152
2988
|
if (!escalated && deps.recovery?.fail) {
|
|
2153
2989
|
try {
|
|
2154
2990
|
await deps.recovery.fail(node, results, failure, executionPath(deps));
|
|
@@ -2159,7 +2995,7 @@ async function runNodeBody(node, state, workflow, deps) {
|
|
|
2159
2995
|
}
|
|
2160
2996
|
throw failure;
|
|
2161
2997
|
}
|
|
2162
|
-
return { ...state, [node.as]: results };
|
|
2998
|
+
return { ...state, [node.as]: results, ...(collect ? { [node.failedAs]: rows.filter(Boolean) } : {}) };
|
|
2163
2999
|
}
|
|
2164
3000
|
case "parallel": {
|
|
2165
3001
|
// Every branch starts from the same state and runs at once; what comes back is each branch's
|
|
@@ -2172,6 +3008,7 @@ async function runNodeBody(node, state, workflow, deps) {
|
|
|
2172
3008
|
let failure;
|
|
2173
3009
|
let failed = false;
|
|
2174
3010
|
const uncertainties = new Set();
|
|
3011
|
+
const later = [];
|
|
2175
3012
|
const outs = await Promise.all((node.branches || []).map(async (branch, i) => {
|
|
2176
3013
|
try {
|
|
2177
3014
|
return await runNodeOnState(branch, inputs[i], workflow, scopeExecution({ ...deps, signal, checkpoint: undefined }, "branches", i));
|
|
@@ -2183,11 +3020,13 @@ async function runNodeBody(node, state, workflow, deps) {
|
|
|
2183
3020
|
failure = error;
|
|
2184
3021
|
siblings.abort(error);
|
|
2185
3022
|
}
|
|
3023
|
+
else
|
|
3024
|
+
later.push(error);
|
|
2186
3025
|
return inputs[i];
|
|
2187
3026
|
}
|
|
2188
3027
|
}));
|
|
2189
3028
|
if (failed)
|
|
2190
|
-
throw withSiblingUncertainty(failure, uncertainties);
|
|
3029
|
+
throw withSiblingUncertainty(reportedFailure(failure, later), uncertainties);
|
|
2191
3030
|
const merged = { ...state };
|
|
2192
3031
|
const writer = new Map();
|
|
2193
3032
|
// Host state is engine-owned and merges by delta: every branch started from the same `$host`, so an
|
|
@@ -2340,7 +3179,8 @@ function compileVerifier(workflow, node, state, deps) {
|
|
|
2340
3179
|
const schema = workflow.schemas[clause.out].properties ?? {};
|
|
2341
3180
|
const maxDrives = clause.maxDrives ?? 2;
|
|
2342
3181
|
const floor = clause.override?.below ?? 0.3;
|
|
2343
|
-
|
|
3182
|
+
// A runner with no judge refuses the node here, before its generative call.
|
|
3183
|
+
requireJudge(deps, "judge", `${node.label} (verify)`);
|
|
2344
3184
|
const drives = [];
|
|
2345
3185
|
let reviewedCandidate;
|
|
2346
3186
|
let last = null;
|
|
@@ -2355,29 +3195,36 @@ function compileVerifier(workflow, node, state, deps) {
|
|
|
2355
3195
|
if (outSchema)
|
|
2356
3196
|
normalizeStringNullsForSchema(sub, outSchema);
|
|
2357
3197
|
const asked = interpolateValue({ ...(clause.state ?? {}), submission: "{submission}" }, { ...state, submission: sub, ...(node.as ? { [node.as]: sub } : {}) }, node.label);
|
|
2358
|
-
const result = await runJudge({ label: `${node.label} (verify)`, kind: "judge", state: asked, questions, signal: deps.signal });
|
|
2359
|
-
validateAnswers(questions, result.answers);
|
|
2360
|
-
const answers = result.answers;
|
|
2361
|
-
const doubted = [];
|
|
2362
|
-
const unmet = [];
|
|
2363
|
-
for (const [id, q] of Object.entries(questions)) {
|
|
2364
|
-
if (q.type !== "noul")
|
|
2365
|
-
continue;
|
|
2366
|
-
const p = pYes(answers[id]);
|
|
2367
|
-
if (Object.prototype.hasOwnProperty.call(sub, id)) {
|
|
2368
|
-
if (filled(sub[id]) && p < floor)
|
|
2369
|
-
doubted.push(id);
|
|
2370
|
-
}
|
|
2371
|
-
else if (p < 0.5)
|
|
2372
|
-
unmet.push(id);
|
|
2373
|
-
}
|
|
2374
3198
|
const drive = drives.length + 1;
|
|
2375
|
-
const
|
|
3199
|
+
const decided = await decide(deps, { workflowState: state, nodeKind: "judge", kind: "verify", judgeKind: "judge", label: `${node.label} (verify)`, state: asked, questions, request: { index: drive - 1, count: maxDrives } }, (answers) => {
|
|
3200
|
+
const doubted = [];
|
|
3201
|
+
const unmet = [];
|
|
3202
|
+
const present = Object.keys(questions).filter((id) => Object.prototype.hasOwnProperty.call(sub, id));
|
|
3203
|
+
const thresholds = {};
|
|
3204
|
+
for (const [id, q] of Object.entries(questions)) {
|
|
3205
|
+
if (q.type !== "noul")
|
|
3206
|
+
continue;
|
|
3207
|
+
const p = pYes(answers[id]);
|
|
3208
|
+
if (Object.prototype.hasOwnProperty.call(sub, id)) {
|
|
3209
|
+
if (filled(sub[id]) && p < floor)
|
|
3210
|
+
doubted.push(id);
|
|
3211
|
+
}
|
|
3212
|
+
else if (p < 0.5)
|
|
3213
|
+
unmet.push(id);
|
|
3214
|
+
// A field the submission holds is judged at the floor, one it holds empty is not judged, and a
|
|
3215
|
+
// requirement that is not a field is judged at 0.5.
|
|
3216
|
+
thresholds[id] = !present.includes(id) ? 0.5 : filled(sub[id]) ? floor : null;
|
|
3217
|
+
}
|
|
3218
|
+
const accepted = doubted.length === 0 && unmet.length === 0;
|
|
3219
|
+
return { rule: { kind: "verify", thresholds, present }, action: { accepted, doubted, unmet }, value: { doubted, unmet, accepted } };
|
|
3220
|
+
});
|
|
3221
|
+
const answers = decided.answers;
|
|
3222
|
+
const { doubted, unmet, accepted } = decided.value;
|
|
2376
3223
|
reviewedCandidate = JSON.stringify(sub);
|
|
2377
3224
|
const sidecar = answersSidecar(answers);
|
|
2378
3225
|
drives.push({ drive, doubted, unmet, accepted, sidecar });
|
|
2379
3226
|
last = { answers, doubted, unmet };
|
|
2380
|
-
deps.onEvent?.({ type: "verify.answered", label: node.label, detail: { drive, doubted, unmet, accepted, sidecar,
|
|
3227
|
+
deps.onEvent?.({ type: "verify.answered", label: node.label, detail: { drive, doubted, unmet, accepted, sidecar, ...meteringOf(decided), decision_id: decided.id } });
|
|
2381
3228
|
if (accepted)
|
|
2382
3229
|
return { accepted: true };
|
|
2383
3230
|
if (drive >= maxDrives)
|
|
@@ -2409,6 +3256,59 @@ function resolveSopSections(node, deps) {
|
|
|
2409
3256
|
throw new Error(`${node.node} node "${node.label}": sopSection ${missing.map(name => `"${name}"`).join(", ")} matches no "## <section>" heading in the SOP`);
|
|
2410
3257
|
return { sopSections, sopSlice: slices.map(section => section.text).join("\n\n") };
|
|
2411
3258
|
}
|
|
3259
|
+
/** Refuses, before any node runs, a decision node (or an `ask` predicate) the host's providers cannot answer as asked:
|
|
3260
|
+
* a provider it lacks, `evidence: "required"` on a provider that gives none, a model it does not serve, a question type
|
|
3261
|
+
* it does not answer, or more questions than one of its requests may hold. A host that names no providers has a judge
|
|
3262
|
+
* that may honour neither a node's provider nor its timeoutMs, so a node that sets either is refused. */
|
|
3263
|
+
function checkDecisionProviders(node, deps, workflow) {
|
|
3264
|
+
const registry = deps.decisionProviders;
|
|
3265
|
+
const label = "label" in node ? node.label : node.node;
|
|
3266
|
+
const asked = [];
|
|
3267
|
+
const questionsOf = (out) => {
|
|
3268
|
+
const compiled = workflow ? compileQuestions(workflow.schemas[out], workflow.schemas) : undefined;
|
|
3269
|
+
return compiled?.ok ? Object.values(compiled.questions).map((question) => question.type) : [];
|
|
3270
|
+
};
|
|
3271
|
+
if (node.node === "judge") {
|
|
3272
|
+
const types = questionsOf(node.out);
|
|
3273
|
+
asked.push({ choice: node, types, count: types.length, what: `judge node "${label}"` });
|
|
3274
|
+
}
|
|
3275
|
+
if (node.node === "sift")
|
|
3276
|
+
asked.push({ choice: node, types: questionsOf(node.out), count: null, what: `sift node "${label}"` });
|
|
3277
|
+
if (node.node === "pick")
|
|
3278
|
+
asked.push({ choice: node, types: ["choice"], count: 1, what: `pick node "${label}"` });
|
|
3279
|
+
if (node.node === "route" && !routesByValue(node))
|
|
3280
|
+
asked.push({ choice: node, types: ["choice"], count: 1, what: `route node "${label}"` });
|
|
3281
|
+
for (const pred of [node.node === "loop" ? node.until : undefined, node.node === "escalate" ? node.when : undefined]) {
|
|
3282
|
+
if (pred?.predicate === "ask")
|
|
3283
|
+
asked.push({ choice: pred, types: ["noul"], count: 1, what: `${node.node} node "${label}" (ask predicate)` });
|
|
3284
|
+
}
|
|
3285
|
+
for (const { choice, types, count, what } of asked) {
|
|
3286
|
+
if (!registry) {
|
|
3287
|
+
if (choice.provider !== undefined)
|
|
3288
|
+
throw new Error(`${what} names the decision provider ${choice.provider}, and this host names no decision providers`);
|
|
3289
|
+
if (choice.timeoutMs !== undefined)
|
|
3290
|
+
throw new Error(`${what} sets timeoutMs, and this host names no decision providers to bound it with`);
|
|
3291
|
+
continue;
|
|
3292
|
+
}
|
|
3293
|
+
const id = choice.provider ?? registry.default;
|
|
3294
|
+
const provider = Object.hasOwn(registry.providers, id) ? registry.providers[id] : undefined;
|
|
3295
|
+
if (!provider)
|
|
3296
|
+
throw new Error(`${what} names the decision provider ${id}, which this host does not have`);
|
|
3297
|
+
const capabilities = provider.capabilities;
|
|
3298
|
+
if (choice.evidence === "required" && capabilities.evidence === "none")
|
|
3299
|
+
throw new Error(`${what} requires evidence, and the decision provider ${id} gives none`);
|
|
3300
|
+
if (choice.model !== undefined && capabilities.models && !capabilities.models.includes(choice.model))
|
|
3301
|
+
throw new Error(`${what} pins the model ${choice.model}, which the decision provider ${id} does not answer with`);
|
|
3302
|
+
const missing = [...new Set(types.filter((type) => !capabilities.types.includes(type)))];
|
|
3303
|
+
if (missing.length)
|
|
3304
|
+
throw new Error(`${what} asks ${missing.join(", ")} questions, which the decision provider ${id} does not answer`);
|
|
3305
|
+
if (count !== null && capabilities.maxQuestions !== null && count > capabilities.maxQuestions)
|
|
3306
|
+
throw new Error(`${what} asks ${count} questions, more than the ${capabilities.maxQuestions} one request to ${id} may hold`);
|
|
3307
|
+
const images = choice.images;
|
|
3308
|
+
if (images && typeof images === "object" && Object.keys(images).length && !capabilities.inputs.includes("image"))
|
|
3309
|
+
throw new Error(`${what} sends images, and the decision provider ${id} takes no image input`);
|
|
3310
|
+
}
|
|
3311
|
+
}
|
|
2412
3312
|
export function assertWorkflowCapabilities(node, deps, workflow) {
|
|
2413
3313
|
assertQuestionCount(deps, 0, "workflow");
|
|
2414
3314
|
if (workflow) {
|
|
@@ -2426,10 +3326,14 @@ export function assertWorkflowCapabilities(node, deps, workflow) {
|
|
|
2426
3326
|
missing("runNode");
|
|
2427
3327
|
if (node.node === "call" && !deps.runEffect)
|
|
2428
3328
|
missing("runEffect");
|
|
2429
|
-
if ((["judge", "pick", "sift"
|
|
3329
|
+
if ((["judge", "pick", "sift"].includes(node.node) || node.node === "route" && !routesByValue(node) || "verify" in node && node.verify ||
|
|
2430
3330
|
node.node === "loop" && node.until?.predicate === "ask" ||
|
|
2431
3331
|
node.node === "escalate" && node.when?.predicate === "ask") && !deps.runJudge)
|
|
2432
3332
|
missing("runJudge");
|
|
3333
|
+
checkDecisionProviders(node, deps, workflow);
|
|
3334
|
+
// A store that cannot commit an item's failure would run the item again in the next process.
|
|
3335
|
+
if (node.node === "map" && node.onItemFailure === "collect" && deps.recovery && !(deps.recovery.failItem && deps.recovery.itemFailure))
|
|
3336
|
+
missing("recovery.failItem and recovery.itemFailure");
|
|
2433
3337
|
if (node.node === "chain")
|
|
2434
3338
|
node.steps.forEach(n => assertWorkflowCapabilities(n, deps, workflow));
|
|
2435
3339
|
if (node.node === "parallel")
|
|
@@ -2444,18 +3348,20 @@ export function assertWorkflowCapabilities(node, deps, workflow) {
|
|
|
2444
3348
|
export async function runWorkflow(workflow, input, deps) {
|
|
2445
3349
|
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
2446
3350
|
throw new WorkflowInputInvalidError(String(workflow?.name), ["input must be a JSON object"]);
|
|
2447
|
-
|
|
3351
|
+
// A host that executes code nodes itself never has them executed here, not even as validation probes.
|
|
3352
|
+
const executeCode = deps.runCode ? false : undefined;
|
|
3353
|
+
const valid = validateWorkflow(workflow, { executeCode });
|
|
2448
3354
|
if (!valid.ok)
|
|
2449
|
-
throw new WorkflowInvalidError(String(workflow?.name), valid.errors);
|
|
3355
|
+
throw new WorkflowInvalidError(String(workflow?.name), valid.errors, valid.issues);
|
|
2450
3356
|
workflow = desugarWorkflow(workflow);
|
|
2451
3357
|
if (workflow.input) {
|
|
2452
3358
|
const validator = Compile(resolveSchemaForWorkflow(workflow, workflow.schemas[workflow.input.schemaId]));
|
|
2453
3359
|
if (!validator.Check(input))
|
|
2454
3360
|
throw new WorkflowInputInvalidError(workflow.name, schemaProblems(validator, input));
|
|
2455
3361
|
}
|
|
2456
|
-
const reachable = validateWorkflow(workflow, { inputKeys: Object.keys(input) });
|
|
3362
|
+
const reachable = validateWorkflow(workflow, { executeCode, inputKeys: Object.keys(input) });
|
|
2457
3363
|
if (!reachable.ok)
|
|
2458
|
-
throw new WorkflowInvalidError(workflow.name, reachable.errors);
|
|
3364
|
+
throw new WorkflowInvalidError(workflow.name, reachable.errors, reachable.issues);
|
|
2459
3365
|
assertWorkflowCapabilities(workflow.root, deps, workflow);
|
|
2460
3366
|
if (needsComposedRecovery(workflow.root))
|
|
2461
3367
|
deps = { ...deps, [SCOPED_EFFECTS]: true };
|
|
@@ -2465,6 +3371,8 @@ export async function runWorkflow(workflow, input, deps) {
|
|
|
2465
3371
|
throw new WorkflowInputInvalidError(workflow.name, [`input must not contain the reserved "${HOST_STATE_KEY}" key`]);
|
|
2466
3372
|
try {
|
|
2467
3373
|
const state = await runNodeOnState(workflow.root, snapshotState(input), workflow, deps);
|
|
3374
|
+
if (deps.signal?.aborted)
|
|
3375
|
+
throw deps.signal.reason ?? new Error("workflow aborted");
|
|
2468
3376
|
const host = hostStateOf(state);
|
|
2469
3377
|
const output = workflow.output.path
|
|
2470
3378
|
? getPath(state, workflow.output.path)
|
|
@@ -2482,9 +3390,9 @@ export async function runWorkflow(workflow, input, deps) {
|
|
|
2482
3390
|
}
|
|
2483
3391
|
}
|
|
2484
3392
|
export async function runWorkflowSlice(workflow, input, focus, deps) {
|
|
2485
|
-
const valid = validateWorkflow(workflow);
|
|
3393
|
+
const valid = validateWorkflow(workflow, { executeCode: deps.runCode ? false : undefined });
|
|
2486
3394
|
if (!valid.ok)
|
|
2487
|
-
throw new WorkflowInvalidError(String(workflow?.name), valid.errors);
|
|
3395
|
+
throw new WorkflowInvalidError(String(workflow?.name), valid.errors, valid.issues);
|
|
2488
3396
|
const desugared = desugarWorkflow(workflow);
|
|
2489
3397
|
const root = desugared.root.node === "chain" ? desugared.root.steps : [desugared.root];
|
|
2490
3398
|
const labels = root.map((n) => n.label ?? "");
|
|
@@ -2510,6 +3418,8 @@ export async function runWorkflowSlice(workflow, input, focus, deps) {
|
|
|
2510
3418
|
let out = state;
|
|
2511
3419
|
for (let i = a; i <= b; i++)
|
|
2512
3420
|
out = await runNodeOnState(root[i], out, desugared, desugared.root.node === "chain" ? scopeExecution(deps, "steps", i) : deps);
|
|
3421
|
+
if (deps.signal?.aborted)
|
|
3422
|
+
throw deps.signal.reason ?? new Error("workflow aborted");
|
|
2513
3423
|
return { status: "complete", state: out, focus: { from: labels[a], to: labels[b] } };
|
|
2514
3424
|
}
|
|
2515
3425
|
catch (error) {
|