@parcha/agentrun-dsl 0.1.0-beta.16 → 0.1.0-beta.160

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/NOTICE +2 -0
  2. package/README.md +153 -16
  3. package/dist/author-references.d.ts +8 -0
  4. package/dist/author-references.js +8 -0
  5. package/dist/author.d.ts +4 -3
  6. package/dist/author.js +20 -11
  7. package/dist/authoring.js +5 -4
  8. package/dist/cli.js +19 -13
  9. package/dist/code-exec.d.ts +19 -0
  10. package/dist/code-exec.js +54 -5
  11. package/dist/decision-provider.d.ts +166 -0
  12. package/dist/decision-provider.js +238 -0
  13. package/dist/decision-receipts.d.ts +64 -3
  14. package/dist/decision-receipts.js +50 -8
  15. package/dist/dry-run.d.ts +35 -1
  16. package/dist/dry-run.js +131 -25
  17. package/dist/evidence.d.ts +19 -0
  18. package/dist/evidence.js +63 -0
  19. package/dist/graph.d.ts +107 -5
  20. package/dist/graph.js +284 -9
  21. package/dist/index.d.ts +17 -12
  22. package/dist/index.js +16 -10
  23. package/dist/inspection.d.ts +5 -0
  24. package/dist/inspection.js +14 -1
  25. package/dist/pattern-screen.d.ts +14 -0
  26. package/dist/pattern-screen.js +451 -0
  27. package/dist/predicates.d.ts +29 -0
  28. package/dist/predicates.js +62 -3
  29. package/dist/record.d.ts +4 -0
  30. package/dist/record.js +6 -0
  31. package/dist/recovery/binding.js +2 -1
  32. package/dist/recovery/call-key.d.ts +0 -4
  33. package/dist/recovery/call-key.js +0 -7
  34. package/dist/recovery/driver.d.ts +59 -24
  35. package/dist/recovery/driver.js +251 -73
  36. package/dist/recovery/frozen-snapshot.d.ts +54 -4
  37. package/dist/recovery/frozen-snapshot.js +44 -14
  38. package/dist/recovery/handoff-receipts.d.ts +0 -35
  39. package/dist/recovery/handoff-receipts.js +2 -44
  40. package/dist/recovery/index.d.ts +4 -8
  41. package/dist/recovery/index.js +4 -7
  42. package/dist/recovery/with-recovery.d.ts +9 -3
  43. package/dist/recovery/with-recovery.js +78 -18
  44. package/dist/submit-envelope.d.ts +9 -0
  45. package/dist/submit-envelope.js +44 -0
  46. package/dist/system-one.d.ts +21 -2
  47. package/dist/system-one.js +96 -18
  48. package/dist/testing/decision-provider.d.ts +32 -0
  49. package/dist/testing/decision-provider.js +183 -0
  50. package/dist/testing/index.d.ts +1 -0
  51. package/dist/testing/index.js +2 -0
  52. package/dist/vocabulary.d.ts +11 -3
  53. package/dist/vocabulary.js +19 -9
  54. package/dist/workflow-shape.d.ts +7 -0
  55. package/dist/workflow-shape.js +116 -70
  56. package/dist/workflow.d.ts +171 -19
  57. package/dist/workflow.js +1063 -399
  58. package/dist/zod-json-contract.js +6 -8
  59. package/package.json +12 -4
  60. package/schema/workflow.schema.json +366 -1
  61. package/skills/author/SKILL.md +6 -1
  62. package/skills/author/references/jev-decisions.md +50 -5
  63. package/skills/author/references/language.md +19 -14
  64. package/dist/recovery/effect-failure.d.ts +0 -34
  65. package/dist/recovery/effect-failure.js +0 -62
  66. package/dist/recovery/handoff-block.d.ts +0 -36
  67. package/dist/recovery/handoff-block.js +0 -148
  68. package/dist/recovery/handoff-digest.d.ts +0 -58
  69. package/dist/recovery/handoff-digest.js +0 -195
  70. package/dist/recovery/handoff-files.d.ts +0 -54
  71. package/dist/recovery/handoff-files.js +0 -162
  72. package/dist/recovery/handoff-redact.d.ts +0 -12
  73. package/dist/recovery/handoff-redact.js +0 -44
  74. package/dist/recovery/handoff.d.ts +0 -44
  75. package/dist/recovery/handoff.js +0 -46
@@ -1,18 +1,45 @@
1
1
  import { createRequire } from "node:module";
2
2
  import { Compile } from "typebox/compile";
3
- const documentSchema = createRequire(import.meta.url)("../schema/workflow.schema.json");
3
+ import { isRecord } from "./record.js";
4
+ // A named require lets deployment tracers retain the schema beside this package.
5
+ const require = createRequire(import.meta.url);
6
+ const documentSchema = require("../schema/workflow.schema.json");
4
7
  const fullValidator = Compile(documentSchema);
5
- const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
6
8
  // Diagnostics only: the full validator below remains the acceptance authority.
7
9
  // Its union errors can fill the entire error limit with unrelated node kinds.
8
10
  // Select the declared discriminator's schema, checking nested nodes separately so
9
11
  // their own discriminators also get useful errors. Never evaluate authored code.
10
12
  const diagnosticDefinitions = { ...documentSchema.definitions, WorkflowNode: {}, Workflow: {} };
11
- const nodeSchemas = new Map(documentSchema.definitions.WorkflowNode.anyOf.map((schema) => [schema.properties.node.const, schema]));
13
+ // A node kind may have more than one schema (a judged route and a route by value are both "route"): every variant is kept.
14
+ const nodeSchemas = new Map();
15
+ for (const schema of documentSchema.definitions.WorkflowNode.anyOf) {
16
+ const kind = schema.properties.node.const;
17
+ nodeSchemas.set(kind, [...(nodeSchemas.get(kind) ?? []), schema]);
18
+ }
12
19
  const diagnosticValidators = new Map();
13
- for (const schema of [documentSchema.definitions.Workflow, ...nodeSchemas.values()]) {
20
+ for (const schema of [documentSchema.definitions.Workflow, ...[...nodeSchemas.values()].flat()]) {
14
21
  diagnosticValidators.set(schema, Compile({ ...schema, definitions: diagnosticDefinitions }));
15
22
  }
23
+ /** The variant of its kind a node is: the one that accepts it, else the one it has the fewest errors against, so a judged
24
+ * route missing its state is diagnosed as a judged route, not as a route by value missing its valuePath. Nested nodes are
25
+ * opaque to a variant's validator, so the count is of the node's own fields. A tie goes to the variant that comes first in
26
+ * the schema: a bare route is diagnosed as a judged route. */
27
+ const variantOf = (variants, entry) => {
28
+ if (variants.length === 1)
29
+ return variants[0];
30
+ let closest = variants[0], fewest = Infinity;
31
+ for (const variant of variants) {
32
+ const validator = diagnosticValidators.get(variant);
33
+ if (validator.Check(entry))
34
+ return variant;
35
+ const count = [...validator.Errors(entry)].length;
36
+ if (count < fewest) {
37
+ closest = variant;
38
+ fewest = count;
39
+ }
40
+ }
41
+ return closest;
42
+ };
16
43
  const pointer = (key) => key.replace(/~/g, "~0").replace(/\//g, "~1");
17
44
  function discriminatedErrors(value) {
18
45
  const errors = [];
@@ -22,7 +49,7 @@ function discriminatedErrors(value) {
22
49
  const validator = diagnosticValidators.get(schema);
23
50
  if (!validator.Check(entry))
24
51
  for (const error of validator.Errors(entry)) {
25
- errors.push(`workflow${path}${error.instancePath || (path ? "" : "/")}: ${error.message}`);
52
+ errors.push({ path: `${path}${error.instancePath}`, message: `workflow${path}${error.instancePath || (path ? "" : "/")}: ${error.message}` });
26
53
  if (errors.length >= 8)
27
54
  return;
28
55
  }
@@ -32,9 +59,9 @@ function discriminatedErrors(value) {
32
59
  if (!schema || typeof schema !== "object" || errors.length >= 8)
33
60
  return;
34
61
  if (schema.$ref === "#/definitions/WorkflowNode") {
35
- const selected = isRecord(entry) && typeof entry.node === "string" ? nodeSchemas.get(entry.node) : undefined;
36
- if (selected)
37
- check(selected, entry, path);
62
+ const variants = isRecord(entry) && typeof entry.node === "string" ? nodeSchemas.get(entry.node) : undefined;
63
+ if (variants)
64
+ check(variantOf(variants, entry), entry, path);
38
65
  return; // Unknown/missing discriminators retain the full validator's fallback.
39
66
  }
40
67
  if (schema.$ref === "#/definitions/Workflow") {
@@ -58,128 +85,147 @@ function discriminatedErrors(value) {
58
85
  check(documentSchema.definitions.Workflow, value, "");
59
86
  return errors;
60
87
  }
88
+ /** The deepest prefix of `path` that names a value in `document`: a rule about a missing key points at its container. */
89
+ function present(document, path) {
90
+ let cursor = document;
91
+ let reached = "";
92
+ for (const raw of path.split("/").slice(1)) {
93
+ const key = raw.replace(/~1/g, "/").replace(/~0/g, "~");
94
+ if (cursor === null || typeof cursor !== "object" || !Object.hasOwn(cursor, key))
95
+ break;
96
+ cursor = cursor[key];
97
+ reached += `/${raw}`;
98
+ }
99
+ return reached;
100
+ }
61
101
  export function workflowShapeErrors(value, complete = false) {
102
+ return workflowShapeIssues(value, complete).map(issue => issue.message);
103
+ }
104
+ export function workflowShapeIssues(value, complete = false) {
105
+ const located = (issues) => issues.map(issue => ({ path: present(value, issue.path), message: issue.message }));
62
106
  if (complete) {
63
107
  if (fullValidator.Check(value))
64
108
  return [];
65
109
  const selected = discriminatedErrors(value);
66
110
  if (selected.length)
67
- return selected;
68
- return [...fullValidator.Errors(value)].slice(0, 8).map(error => `workflow${error.instancePath || "/"}: ${error.message}`);
111
+ return located(selected);
112
+ return located([...fullValidator.Errors(value)].slice(0, 8).map(error => ({ path: error.instancePath, message: `workflow${error.instancePath || "/"}: ${error.message}` })));
69
113
  }
114
+ const at = (loc, key) => ({ text: `${loc.text}.${key}`, ptr: `${loc.ptr}/${pointer(key)}` });
115
+ const index = (loc, i) => ({ text: `${loc.text}[${i}]`, ptr: `${loc.ptr}/${i}` });
70
116
  const errors = [];
71
- const fail = (path, expected) => { errors.push(`${path} must be ${expected}`); };
72
- const record = (entry, path) => {
117
+ const fail = (loc, expected) => { errors.push({ path: loc.ptr, message: `${loc.text} must be ${expected}` }); };
118
+ const record = (entry, loc) => {
73
119
  if (isRecord(entry))
74
120
  return true;
75
- fail(path, "an object");
121
+ fail(loc, "an object");
76
122
  return false;
77
123
  };
78
- const strings = (entry, path, keys) => {
124
+ const strings = (entry, loc, keys) => {
79
125
  for (const key of keys)
80
126
  if (entry[key] !== undefined && typeof entry[key] !== "string")
81
- fail(`${path}.${key}`, "a string");
127
+ fail(at(loc, key), "a string");
82
128
  };
83
- const stringList = (entry, path) => {
129
+ const stringList = (entry, loc) => {
84
130
  if (entry === undefined)
85
131
  return;
86
132
  if (!Array.isArray(entry)) {
87
- fail(path, "an array of strings");
133
+ fail(loc, "an array of strings");
88
134
  return;
89
135
  }
90
- entry.forEach((item, index) => { if (typeof item !== "string")
91
- fail(`${path}[${index}]`, "a string"); });
136
+ entry.forEach((item, i) => { if (typeof item !== "string")
137
+ fail(index(loc, i), "a string"); });
92
138
  };
93
139
  // Preserve existing diagnostics for missing, non-finite and out-of-range numbers, and
94
140
  // primitive strings such as poll.deadline_s="soon". Objects can throw during coercion.
95
- const numeric = (entry, path, keys) => {
141
+ const numeric = (entry, loc, keys) => {
96
142
  for (const key of keys) {
97
143
  const item = entry[key];
98
144
  if (item !== null && item !== undefined && !["number", "string", "boolean"].includes(typeof item))
99
- fail(`${path}.${key}`, "a number");
145
+ fail(at(loc, key), "a number");
100
146
  }
101
147
  };
102
- const objectField = (entry, path, key, visit) => {
148
+ const objectField = (entry, loc, key, visit) => {
103
149
  if (entry[key] === undefined)
104
150
  return;
105
- if (record(entry[key], `${path}.${key}`))
106
- visit?.(entry[key], `${path}.${key}`);
151
+ if (record(entry[key], at(loc, key)))
152
+ visit?.(entry[key], at(loc, key));
107
153
  };
108
- const predicate = (entry, path) => {
109
- strings(entry, path, ["predicate", "path", "key", "instructions"]);
110
- numeric(entry, path, ["n", "gte"]);
111
- objectField(entry, path, "state");
112
- objectField(entry, path, "criteria", (criteria, at) => strings(criteria, at, ["true", "false"]));
154
+ const predicate = (entry, loc) => {
155
+ strings(entry, loc, ["predicate", "path", "key", "instructions"]);
156
+ numeric(entry, loc, ["n", "gte"]);
157
+ objectField(entry, loc, "state");
158
+ objectField(entry, loc, "criteria", (criteria, here) => strings(criteria, here, ["true", "false"]));
113
159
  };
114
- const node = (entry, path) => {
160
+ const node = (entry, loc) => {
115
161
  if (entry === undefined)
116
162
  return; // missing bodies already receive semantic diagnostics
117
- if (!record(entry, path))
163
+ if (!record(entry, loc))
118
164
  return;
119
- strings(entry, path, ["node", "label", "code", "as", "out", "instructions", "itemsPath", "resultPath", "kind", "stage", "summary", "describe", "type", "path", "tool", "command", "via", "where", "effort", "thinking", "tier", "valuePath", "otherwise"]);
120
- numeric(entry, path, ["maxConcurrency", "maxIters", "expect_iters", "deadline_s"]);
165
+ strings(entry, loc, ["node", "label", "code", "as", "out", "instructions", "itemsPath", "resultPath", "kind", "stage", "summary", "describe", "type", "path", "tool", "command", "via", "where", "effort", "thinking", "tier", "valuePath", "otherwise", "onItemFailure", "failedAs"]);
166
+ numeric(entry, loc, ["maxConcurrency", "maxIters", "expect_iters", "deadline_s"]);
121
167
  for (const key of ["requires", "tools", "produces"])
122
- stringList(entry[key], `${path}.${key}`);
168
+ stringList(entry[key], at(loc, key));
123
169
  if (entry.sopSection !== undefined && typeof entry.sopSection !== "string")
124
- stringList(entry.sopSection, `${path}.sopSection`);
170
+ stringList(entry.sopSection, at(loc, "sopSection"));
125
171
  for (const key of ["state", "args", "input", "env"])
126
- objectField(entry, path, key);
172
+ objectField(entry, loc, key);
127
173
  // Host-owned and opaque: only its container is checked, never its keys or values.
128
174
  if (entry.metadata !== undefined && !isRecord(entry.metadata)) {
129
- errors.push(`${path} (${typeof entry.label === "string" && entry.label ? entry.label : String(entry.node)}): metadata must be a plain object of host markers`);
175
+ errors.push({ path: at(loc, "metadata").ptr, message: `${loc.text} (${typeof entry.label === "string" && entry.label ? entry.label : String(entry.node)}): metadata must be a plain object of host markers` });
130
176
  }
131
177
  for (const key of ["until", "when"])
132
- objectField(entry, path, key, predicate);
133
- objectField(entry, path, "verify", (verify, at) => {
134
- strings(verify, at, ["out"]);
135
- numeric(verify, at, ["maxDrives"]);
136
- objectField(verify, at, "state");
137
- objectField(verify, at, "override", (override, here) => numeric(override, here, ["below"]));
178
+ objectField(entry, loc, key, predicate);
179
+ objectField(entry, loc, "verify", (verify, here) => {
180
+ strings(verify, here, ["out"]);
181
+ numeric(verify, here, ["maxDrives"]);
182
+ objectField(verify, here, "state");
183
+ objectField(verify, here, "override", (override, inner) => numeric(override, inner, ["below"]));
138
184
  });
139
- objectField(entry, path, "keep", (keep, at) => { strings(keep, at, ["path"]); numeric(keep, at, ["gte"]); });
140
- objectField(entry, path, "unsure", (unsure, at) => { strings(unsure, at, ["branch"]); numeric(unsure, at, ["gte"]); });
141
- objectField(entry, path, "retry", (retry, at) => {
142
- numeric(retry, at, ["attempts", "backoff_s"]);
143
- stringList(retry.on, `${at}.on`);
185
+ objectField(entry, loc, "keep", (keep, here) => { strings(keep, here, ["path"]); numeric(keep, here, ["gte"]); });
186
+ objectField(entry, loc, "unsure", (unsure, here) => { strings(unsure, here, ["branch"]); numeric(unsure, here, ["gte"]); });
187
+ objectField(entry, loc, "retry", (retry, here) => {
188
+ numeric(retry, here, ["attempts", "backoff_s"]);
189
+ stringList(retry.on, at(here, "on"));
144
190
  });
145
- objectField(entry, path, "poll", (poll, at) => {
146
- numeric(poll, at, ["interval_s", "deadline_s"]);
191
+ objectField(entry, loc, "poll", (poll, here) => {
192
+ numeric(poll, here, ["interval_s", "deadline_s"]);
147
193
  for (const key of ["until", "fail_when"])
148
- objectField(poll, at, key, predicate);
194
+ objectField(poll, here, key, predicate);
149
195
  });
150
196
  if (entry.node === "chain" || entry.node === "parallel") {
151
197
  const key = entry.node === "chain" ? "steps" : "branches";
152
198
  if (entry[key] !== undefined) {
153
199
  if (!Array.isArray(entry[key]))
154
- fail(`${path}.${key}`, "an array of nodes");
200
+ fail(at(loc, key), "an array of nodes");
155
201
  else
156
- entry[key].forEach((child, index) => node(child, `${path}.${key}[${index}]`));
202
+ entry[key].forEach((child, i) => node(child, index(at(loc, key), i)));
157
203
  }
158
204
  }
159
205
  if (entry.node === "map" || entry.node === "loop")
160
- node(entry.body, `${path}.body`);
206
+ node(entry.body, at(loc, "body"));
161
207
  if (entry.node === "route")
162
- objectField(entry, path, "branches", (branches, at) => {
208
+ objectField(entry, loc, "branches", (branches, here) => {
163
209
  for (const [name, branch] of Object.entries(branches))
164
- if (record(branch, `${at}.${name}`)) {
165
- strings(branch, `${at}.${name}`, ["criteria"]);
166
- node(branch.body, `${at}.${name}.body`);
210
+ if (record(branch, at(here, name))) {
211
+ strings(branch, at(here, name), ["criteria"]);
212
+ node(branch.body, at(at(here, name), "body"));
167
213
  }
168
214
  });
169
215
  if (entry.node === "workflow")
170
- workflow(entry.workflow, `${path}.workflow`);
216
+ workflow(entry.workflow, at(loc, "workflow"));
171
217
  };
172
- const workflow = (entry, path) => {
173
- if (!record(entry, path))
218
+ const workflow = (entry, loc) => {
219
+ if (!record(entry, loc))
174
220
  return;
175
- strings(entry, path, ["name"]);
176
- record(entry.schemas, `${path}.schemas`);
177
- objectField(entry, path, "input", (input, at) => strings(input, at, ["schemaId"]));
178
- if (record(entry.output, `${path}.output`))
179
- strings(entry.output, `${path}.output`, ["schemaId", "path"]);
180
- if (record(entry.root, `${path}.root`))
181
- node(entry.root, `${path}.root`);
221
+ strings(entry, loc, ["name"]);
222
+ record(entry.schemas, at(loc, "schemas"));
223
+ objectField(entry, loc, "input", (input, here) => strings(input, here, ["schemaId"]));
224
+ if (record(entry.output, at(loc, "output")))
225
+ strings(entry.output, at(loc, "output"), ["schemaId", "path"]);
226
+ if (record(entry.root, at(loc, "root")))
227
+ node(entry.root, at(loc, "root"));
182
228
  };
183
- workflow(value, "workflow");
184
- return errors.slice(0, 8);
229
+ workflow(value, { text: "workflow", ptr: "" });
230
+ return located(errors.slice(0, 8));
185
231
  }