@parcha/agentrun-dsl 0.1.0-beta.253 → 0.1.0-beta.255

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 CHANGED
@@ -186,7 +186,7 @@ The schema helpers resolve the owning workflow's schema catalog; `stageSchemaFor
186
186
 
187
187
  `@parcha/agentrun-dsl/graph` imports nothing, so a static viewer bundles it and a reader with no Node runs it. `workflowGraph(workflow)` returns `{ nodes, edges }` for a workflow document, computed from its JSON alone: a node is `{ path, kind, label, via? }` with `path` a JSON Pointer into the document as written (`/root/steps/1`, `/root/branches/<name>/body`, `/root/body` for a map's or a loop's body, `/root/workflow/root` for a child; the pointers `inspectWorkflow` and a validation issue use), and an edge is `{ from, to, kind }` with `kind` one of `next` (a chain's steps in order, a loop into its body), `branch` (a parallel node's branches), `route` (a route's branches), `item` (a map into its body) and `child` (a workflow node into its child's root). `nodeStatus(graph, source)` says, for each node, `done`, `working`, `failed` or `not-run`, read from what the recovery driver kept: its frame (the `pin` of the frozen snapshot), the map items' records (`agentrun.items`, keyed layout), or both as `{ pin, items }`. A run's map item or loop iteration has no node of its own: its instances count toward the body's node, and a node inside a map's or loop's body is done only once the map or loop is. Once the run has ended, pass its terminal result as `{ pin, items, result }`: an escalation (`result.status` `escalated`, with `result.escalation.executionPath` as `runWorkflow` returns it) marks the escalate node that fired and every container on its path `escalated`, since the frame holds no mark for that node, and a map or loop on that path has ended, so what finished inside it is done. Without a result the status is what it was. To read one item's record on its own, pass `{ item: "<map path>/items/<index>" }` as a third argument: that item is then the instance, and its body is done when the item is. `graphNodeOf(graph, executionPath)` names the node an execution path (an event's or a record's) ran at: `{ path, at }`, where `path` is the node's pointer and `at` lists the map items (`{ kind: "item", index }`) and loop iterations (`{ kind: "iteration", index }`) it ran in, outermost first. An execution path is the node's pointer with `items/<i>` before a map's `body` and `iterations/<i>` before a loop's; the path is read by walking the graph by node kind, so a route branch named `items` stays a branch, and a path the graph does not have is `undefined`. `graphDiff(prev, next)` says what changed between two graphs of a workflow, such as two saved revisions of one: `{ added, removed, changed, moved, edges: { added, removed } }`. A node is the same in both when its `(kind, label)` is unique in each graph, so a step inserted before others is one `added` node (its path in `next`) and the steps after it `moved` (`{ prev, next }` paths), never a run of changes; a node whose `(kind, label)` repeats, or is in one graph only, is matched by its path, and the same path with a new kind or label is `changed` (`{ prev, next, was, now }`, `was` and `now` each `{ kind, label }`). `removed` holds nodes of `prev` only, at their paths there; `rewritten` holds matched nodes whose kind and label stayed but whose own fields changed (new instructions, a new budget: `{ prev, next, kind, label }`); `edges.added` and `edges.removed` hold the edges of one graph only, each in its own graph's paths. Each graph node carries a `changeCheck` for this: 8 hex digits, the FNV-1a hash of the node's own fields as canonical JSON, its children left out as the walk leaves them out. It detects a change; it is not an identity, and not a saved revision's sha256. The pi session's text view draws the same graph.
188
188
 
189
- `@parcha/agentrun-dsl/edit` edits a workflow document as JSON, one pure function per edit, for a visual editor, a CLI or a server. It imports only the graph and the node vocabulary, so a browser bundles it, and it names a node by its graph path. `insert(doc, parentPath, slot, index, node)`, `remove(doc, path)`, `move(doc, fromPath, toParentPath, slot, index)`, `duplicate(doc, path)`, `wrap(doc, paths, kind)` (in a map, loop, route, parallel or chain), `unwrap(doc, path, branch?)`, `addBranch(doc, path, node, name?)`, `removeBranch(doc, path, branch)`, `renameBranch(doc, path, from, to)`, `setField(doc, path, field, value)`, `rename(doc, path, name)` and `renameKey(doc, path, name, options?)` each return `{ doc, moved, added, removed, changed }`. `doc` is the next document: the input is never mutated, and every subtree the edit did not touch is shared with it, byte for byte. `moved` maps each node whose path changed, old path to new; `added` and `removed` list the nodes only one of the two documents has; `changed` lists the nodes whose own fields changed. A slot is where a child sits under its parent: `steps`, `branches`, `body`, `branches/<name>/body`, `workflow/root`, or `root` under the parent path `""`; `locate(doc, path)` says which one holds a node, and `move` counts its index in the document as given, the drop point between two steps. An edit that would break the document's structure (a report inside a map, a chain with no steps, a parallel with one branch, a node of no known kind or without a required field) throws an `EditError` with a `code` from `EDIT_ERROR_CODES` and the `path` it is about, and writes nothing. Whether each reference is produced and each field filled in is still `validateWorkflow`'s to say: `wrap` gives its new container placeholders the validator flags on it (an empty `itemsPath`, `valuePath` or `until` predicate). `rename` changes a label. When the label is the key the node writes (an agent, decide, extract or code node with no `as`; a code node writes there when its code returns something other than a record), every `{placeholder}` and state path that reads the key in scope follows it, and the code and shell sources that mention it, or one of its `<key>$...` keys, are listed in `mentions`, unchanged. A renamed code node's own code is listed too when it names the key, since a record it returns with that key still writes the old name. `renameKey` renames the key a node writes the same way: its `as` when it has one, any kind's, else its label when that is the key, where it gives the document `rename` gives; `{ field: "failedAs" }` renames a map's `failedAs` instead. No edit gives a label, an `as`, a `failedAs` or a route branch a name `Object.prototype` has (`constructor`, `toString`, `__proto__` and the rest) or `prototype`: `rename`, `renameKey`, `renameBranch` and `setField` refuse one, and `insert` and `addBranch` refuse a node with one anywhere inside it; `validateWorkflow` refuses a document written with one (`NODE_FIELD_INVALID`, saying to rename it). With `{ reservedNames: "warn" }`, the check a run makes of a document already stored (`runWorkflow` and `runWorkflowSlice` make it so), each such name is a warning instead, listed in the result's `reserved` (`{ path, field, name }`): the engine reads every label, key, branch and state path as an own key, so a stored document that has one runs as written, and a read of `constructor` that nothing wrote is unset. `renameBranch` carries the route's `otherwise` and `unsure`, but not a value elsewhere that compares against the old branch name (a `field_equals` on the route's `as`, or the value a route by value reads), which the caller must change. `duplicate` copies code as it is, so a copied code node writes the same record keys as its source: duplicating a code branch of a parallel gives two branches that write one key, which the run refuses. The copy's reads of a copied code node's label follow its fresh label, so when that code returns a record naming the old label, those reads find nothing; `duplicate` lists the copy's code and shell sources that mention a key it renamed in `mentions`, as `rename` does. `editableFields(node)` and `requiredFields(node)` are the schema's fields for a node, as `setField` writes them.
189
+ `@parcha/agentrun-dsl/edit` edits a workflow document as JSON, one pure function per edit, for a visual editor, a CLI or a server. It imports only the graph and the node vocabulary, so a browser bundles it, and it names a node by its graph path. `insert(doc, parentPath, slot, index, node)`, `remove(doc, path)`, `move(doc, fromPath, toParentPath, slot, index)`, `duplicate(doc, path)`, `wrap(doc, paths, kind)` (in a map, loop, route, parallel or chain), `unwrap(doc, path, branch?)`, `addBranch(doc, path, node, name?)`, `removeBranch(doc, path, branch)`, `renameBranch(doc, path, from, to)`, `setField(doc, path, field, value)`, `rename(doc, path, name)` and `renameKey(doc, path, name, options?)` each return `{ doc, moved, added, removed, changed }`. `doc` is the next document: the input is never mutated, and every subtree the edit did not touch is shared with it, byte for byte. `moved` maps each node whose path changed, old path to new; `added` and `removed` list the nodes only one of the two documents has; `changed` lists the nodes whose own fields changed. A slot is where a child sits under its parent: `steps`, `branches`, `body`, `branches/<name>/body`, `workflow/root`, or `root` under the parent path `""`; `locate(doc, path)` says which one holds a node, and `move` counts its index in the document as given, the drop point between two steps. An edit that would break the document's structure (a report inside a map, a chain with no steps, a parallel with one branch, a node of no known kind or without a required field) throws an `EditError` with a `code` from `EDIT_ERROR_CODES` and the `path` it is about, and writes nothing. Whether each reference is produced and each field filled in is still `validateWorkflow`'s to say: `wrap` gives its new container placeholders the validator flags on it (an empty `itemsPath`, `valuePath` or `until` predicate). `rename` changes a label. When the label is the key the node writes (an agent, decide, extract or code node with no `as`; a code node writes there when its code returns something other than a record), every `{placeholder}` and state path that reads the key in scope follows it, and the code and shell sources that mention it, or one of its `<key>$...` keys, are listed in `mentions`, unchanged. A renamed code node's own code is listed too when it names the key, since a record it returns with that key still writes the old name. `renameKey` renames the key a node writes the same way: its `as` when it has one, any kind's, else its label when that is the key, where it gives the document `rename` gives; `{ field: "failedAs" }` renames a map's `failedAs` instead. No edit gives a label, an `as`, a `failedAs` or a route branch a name `Object.prototype` has (`constructor`, `toString`, `__proto__` and the rest) or `prototype`: `rename`, `renameKey`, `renameBranch` and `setField` refuse one, and `insert` and `addBranch` refuse a node with one anywhere inside it; `validateWorkflow` refuses a document written with one (`NODE_FIELD_INVALID`, saying to rename it). With `{ reservedNames: "warn" }`, the check a run makes of a document already stored (`runWorkflow` and `runWorkflowSlice` make it so), each such name is a warning instead, listed in the result's `reserved` (`{ path, field, name }`): the engine reads every label, key, branch and state path as an own key, so a stored document that has one runs as written, and a read of `constructor` that nothing wrote is unset. No edit gives a label a `/` either, since a run names a child workflow's steps `<child>/<step>` and a label `a/b` beside a child `a`'s step `b` would name two nodes: `rename`, `setField`, `insert` and `addBranch` refuse one, `duplicate` gives a copy of a stored one `_` for each `/` (`read/write` copies as `read_write_2`), and `validateWorkflow` refuses a document written with one, or warns of it under `reservedNames: "warn"` (listed in `reserved` as a `label`). A stored document whose `/` label does name a node twice is still refused where a run needs each name once (frozen recovery's duplicate-label check). `renameBranch` carries the route's `otherwise` and `unsure`, but not a value elsewhere that compares against the old branch name (a `field_equals` on the route's `as`, or the value a route by value reads), which the caller must change. `duplicate` copies code as it is, so a copied code node writes the same record keys as its source: duplicating a code branch of a parallel gives two branches that write one key, which the run refuses. The copy's reads of a copied code node's label follow its fresh label, so when that code returns a record naming the old label, those reads find nothing; `duplicate` lists the copy's code and shell sources that mention a key it renamed in `mentions`, as `rename` does. `editableFields(node)` and `requiredFields(node)` are the schema's fields for a node, as `setField` writes them.
190
190
 
191
191
  The same module answers what a property panel asks. `setBranchCriteria(doc, path, branch, criteria)` writes or removes (`undefined`) a judged route branch's criteria. `setSchema(doc, id, schema, scope?)` writes or removes a schema of the document, or of the child workflow of the workflow node at `scope`, and refuses to remove one that `schemaUsers(doc, id, scope?)` says is named: it lists the JSON Pointers of the workflow's `input.schemaId` and `output.schemaId`, each node's `out` and `verify.out`, and each `$ref` to `#/definitions/<id>` in another schema. `setWorkflowField(doc, field, value)` writes the document's own `input` or `output` (`{ schemaId }`, and the output's optional `path`), or removes the input (`undefined`); `outputNames(doc)` lists the names that `path` may read, as `namesInScope` lists a node's: the input keys and the keys written outside any map body or child workflow, a terminal's fixed key among them (`report_markdown` for a report or a prose artifact, `artifact` for a file artifact). Like `namesInScope`, it cannot list a key only a code node's returned record writes. `namesInScope(doc, path, field?)` lists the names the node at `path` may read, each `{ name, from }` with `from` the path of the node that writes it, `"input"` or `"engine"` (a map body's `item` and `item_index`), by the scope rules `rename` carries a key along; `field` asks for one field where it sees more (a map's `resultPath`, a loop's `until`, `verify`, a pick's or sift's `describe`, a workflow node's `workflow`). It cannot list a key only a code node's returned record writes, nor the keys of an input whose schema lists no properties. `readsOf(doc, path)` lists each `{placeholder}` and state path the node reads as `{ pointer, name }`, the references `rename` carries, and `openPlaceholderAt(text, offset)` finds the `{` open at a cursor and the name typed after it. `freshName(doc, base)` gives `base` or `<base>_<n>`, free in every scope (a child workflow's with no node yet included), and `placeholderNode(doc, kind)` the smallest node of a kind that holds no nodes, each required field holding its `FIELD_PLACEHOLDERS` value, which the validator flags until it is filled in.
192
192
 
package/dist/edit.js CHANGED
@@ -67,7 +67,7 @@
67
67
  // show. So is a renamed code node's own code when it names the old key: a record it returns with that key still
68
68
  // writes the old name. The validator does not see a read inside code, so that list is the only warning.
69
69
  // - rename refuses a name another node's label or key holds in its workflow, or that a reference (as `<name>$...`
70
- // too), an input or a code source in scope already uses. A label in another workflow (a sibling child's) is free. It also refuses to rename a key another node in scope or the input also writes, since it
70
+ // too), an input or a code source in scope already uses. A label in another workflow (a sibling child's) is free. No label holds "/" (a run names a child's steps `<child>/<step>`). It also refuses to rename a key another node in scope or the input also writes, since it
71
71
  // can't tell whose readers are whose.
72
72
  // - renameKey renames the key a node writes: its `as` when it has one (any kind, a map's and a decision node's
73
73
  // included), else its label when that is the key, where it gives the document rename gives; with
@@ -518,6 +518,7 @@ const nodeOf = (value, path) => {
518
518
  if (node.node === "route" && isObject(node.branches))
519
519
  for (const name of Object.keys(node.branches))
520
520
  refusePrototypeName(name, path);
521
+ refuseSlashLabel(node.label, path);
521
522
  }
522
523
  return value;
523
524
  };
@@ -594,6 +595,12 @@ const refusePrototypeName = (name, path) => {
594
595
  if (typeof name === "string" && isPrototypeName(name))
595
596
  throw new EditError("NAME_INVALID", path, `"${name}" is a name an object's prototype has`);
596
597
  };
598
+ /** Refuse a label holding "/": a run names a child workflow's steps `<child>/<step>`, so a label `a/b` beside a child
599
+ * `a`'s step `b` would name two nodes (the validator's rule too). */
600
+ const refuseSlashLabel = (name, path) => {
601
+ if (typeof name === "string" && name.includes("/"))
602
+ throw new EditError("NAME_INVALID", path, `a label holds no "/", which a run names a child workflow's steps with (<child>/<step>): ${JSON.stringify(name)}`);
603
+ };
597
604
  const renamePath = (path, rename, prefixed, at, optional = false) => {
598
605
  const prefix = prefixed && path.startsWith("state.") ? "state." : "";
599
606
  const [head, ...tail] = path.slice(prefix.length).split(".");
@@ -903,11 +910,11 @@ export function duplicate(doc, path) {
903
910
  if (entry.scope !== "" && !childNames.has(entry.scope))
904
911
  childNames.set(entry.scope, namesUsed(copy, inCopy, entry.scope));
905
912
  for (const entry of inCopy) {
906
- const key = (old) => {
913
+ const key = (old, base = old) => {
907
914
  const scope = renamed.get(entry.scope) ?? new Map();
908
915
  renamed.set(entry.scope, scope);
909
916
  if (!scope.has(old)) {
910
- const name = fresh(old, entry.scope === "" ? names : new Set([...names, ...childNames.get(entry.scope)]));
917
+ const name = fresh(base, entry.scope === "" ? names : new Set([...names, ...childNames.get(entry.scope)]));
911
918
  names.add(name);
912
919
  scope.set(old, name);
913
920
  }
@@ -915,8 +922,16 @@ export function duplicate(doc, path) {
915
922
  };
916
923
  copy = scratch.update(copy, entry.keys, (value) => {
917
924
  const node = { ...value };
918
- if (typeof node.label === "string")
919
- node.label = labelIsKey(node) ? key(node.label) : fresh(node.label, labels);
925
+ if (typeof node.label === "string") {
926
+ // A label stored with "/" (refuseSlashLabel) is copied with "_" for it, numbered as a copy (`read/write` gives
927
+ // `read_write_2`), so the copy is one a save takes and the original keeps the name it would be renamed to.
928
+ const base = node.label.replace(/\//g, "_");
929
+ if (base !== node.label) {
930
+ labels.add(base);
931
+ names.add(base);
932
+ }
933
+ node.label = labelIsKey(node) ? key(node.label, base) : fresh(base, labels);
934
+ }
920
935
  if (typeof node.label === "string") {
921
936
  labels.add(node.label);
922
937
  names.add(node.label);
@@ -1121,6 +1136,8 @@ export function setField(doc, path, field, value) {
1121
1136
  // Writing the value the field already holds is no change, whatever the name.
1122
1137
  if ((field === "label" || field === "as" || field === "failedAs") && value !== entry.node[field])
1123
1138
  refusePrototypeName(value, path);
1139
+ if (field === "label" && value !== entry.node.label)
1140
+ refuseSlashLabel(value, path);
1124
1141
  const draft = new Draft();
1125
1142
  return finish(draft, before, draft.update(before, [...entry.keys, field], () => value === undefined ? DELETE : clone(value)));
1126
1143
  }
@@ -1150,6 +1167,7 @@ export function rename(doc, path, name) {
1150
1167
  if (typeof name !== "string" || !name.trim() || name.includes("$"))
1151
1168
  throw new EditError("NAME_INVALID", path, `a label is a non-empty name with no "$": ${JSON.stringify(name)}`);
1152
1169
  refusePrototypeName(name, path);
1170
+ refuseSlashLabel(name, path);
1153
1171
  const keyed = labelIsKey(node) && typeof node.label === "string";
1154
1172
  if (keyed && !KEY_NAME.test(name))
1155
1173
  throw new EditError("NAME_INVALID", path, `the label of ${path} is the key it writes, so it takes letters, digits and "_" only: ${JSON.stringify(name)}`);
@@ -705,7 +705,9 @@ export type ValidationIssue = {
705
705
  message: string;
706
706
  };
707
707
  /** A reserved name (vocabulary.ts isPrototypeName: Object.prototype's own names, and "prototype") where a document puts a
708
- * name: a node's `label`, `as` or `failedAs`, or a route's branch. `path` is the node's JSON Pointer. */
708
+ * name: a node's `label`, `as` or `failedAs`, or a route's branch. Or a label holding "/", which a run puts between a
709
+ * child workflow's label and its steps' (`<child>/<step>`): a label `a/b` beside a child `a`'s step `b` would name two
710
+ * nodes. `path` is the node's JSON Pointer. */
709
711
  export type ReservedName = {
710
712
  path: string;
711
713
  field: "label" | "as" | "failedAs" | "branch";
@@ -732,10 +734,11 @@ export type ValidateWorkflowOptions = {
732
734
  /** The host's tool names. Given, a node that names a tool outside them (a model or artifact node's `tools`, a call
733
735
  * node's `tool`) is refused here. Absent, tool names are not checked until a node runs. */
734
736
  tools?: readonly string[];
735
- /** A reserved name as a label, `as`, `failedAs` or route branch (ReservedName). "refuse" (the default, a save's check):
736
- * each is a NODE_FIELD_INVALID issue saying to rename it. "warn" (a run's check of a document already stored): none is
737
- * an issue; each is a warning, and `reserved` lists them in document order. The engine reads every name as an own key,
738
- * so a stored document that has one runs as written. */
737
+ /** A reserved name as a label, `as`, `failedAs` or route branch, or a label holding "/" (ReservedName). "refuse" (the
738
+ * default, a save's check): each is a NODE_FIELD_INVALID issue saying to rename it. "warn" (a run's check of a document
739
+ * already stored): none is an issue; each is a warning, and `reserved` lists them in document order. The engine reads
740
+ * every name as an own key, so a stored document that has one runs as written; one whose "/" label names a node twice
741
+ * is still refused where a run needs each name once (recovery's duplicate-label check). */
739
742
  reservedNames?: "refuse" | "warn";
740
743
  };
741
744
  export declare function validateWorkflow(workflow: Workflow, opts?: ValidateWorkflowOptions): {
package/dist/workflow.js CHANGED
@@ -463,7 +463,9 @@ export function validateWorkflow(workflow, opts) {
463
463
  // Reserved names, refused where they are, or (reservedNames "warn") kept to warn of once the walk is done.
464
464
  const warnReserved = opts?.reservedNames === "warn";
465
465
  const reserved = [];
466
- const reservedWords = (field, name) => `${field} "${name}" is a reserved name, one an object's prototype has; rename it`;
466
+ const reservedWords = (field, name) => isPrototypeName(name)
467
+ ? `${field} "${name}" is a reserved name, one an object's prototype has; rename it`
468
+ : `${field} "${name}" holds "/", which a run names a child workflow's steps with (<child>/<step>); rename it`;
467
469
  const reservedAt = (at, path, shown, field, name) => {
468
470
  const message = `${path} (${shown}): ${reservedWords(field, name)}`;
469
471
  if (warnReserved)
@@ -976,6 +978,11 @@ export function validateWorkflow(workflow, opts) {
976
978
  if (isPrototypeName(name))
977
979
  reservedAt(ptrOf(path), path, node.label || "route", "branch", name);
978
980
  }
981
+ // Nor does a label hold "/": a run names a child workflow's steps `<child>/<step>`, so a label `a/b` beside a child
982
+ // `a`'s step `b` would name two nodes in its events, receipts and step records. The editor refuses one (edit.ts) and
983
+ // so does a save; a document stored with one that names no node twice still runs (reservedNames "warn").
984
+ if (typeof node.label === "string" && node.label.includes("/"))
985
+ reservedAt(ptrOf(path), path, node.label, "label", node.label);
979
986
  // A generative or code node's write key must be a real state key, never blank: the interpreter writes under
980
987
  // `as || label` (`as` wins when it is any non-empty string, whitespace included), so a blank or whitespace-only
981
988
  // `as`, or a label that is the key and is whitespace-only, would write under a key no other node can name and hide
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcha/agentrun-dsl",
3
- "version": "0.1.0-beta.253",
3
+ "version": "0.1.0-beta.255",
4
4
  "description": "Composable, typed workflows for agents",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",