@parcha/agentrun-dsl 0.1.0-beta.124 → 0.1.0-beta.125

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.
@@ -8,13 +8,7 @@ export declare function deniedGlobalReferences(src: string): string[];
8
8
  * Syntax validity does not prove that the expression produces a function or a valid patch. */
9
9
  export declare function compileTransformSyntax(src: string): void;
10
10
  export declare function compileTransform(src: string): (value: any, ctx?: any) => any;
11
- /** The files a code node's program reads and writes, in the directory it runs in (transformProgram). */
12
- export declare const TRANSFORM_FILES: {
13
- readonly program: "run.mjs";
14
- readonly input: "in.json";
15
- readonly output: "out.json";
16
- };
17
- /** What a code node's program writes to its output file: the function's value (`undefined: true` for undefined, which
11
+ /** What a code node's script answers (transformScript): the function's value (`undefined: true` for undefined, which
18
12
  * JSON has no word for), or the error it threw, in compileTransform's words. */
19
13
  export type TransformOutcome = {
20
14
  readonly ok: true;
@@ -25,10 +19,11 @@ export type TransformOutcome = {
25
19
  readonly error: string;
26
20
  };
27
21
  /**
28
- * A code node as a standalone Node.js program (ES module), for a host that runs code nodes in an isolated computer
29
- * rather than in its own process. Run with `node run.mjs` in a directory holding `in.json` ({ state, ctx }), it builds
30
- * the function exactly as compileTransform does (the same factory text, denied globals and synchronous checks), calls
31
- * it once with the state and ctx, and writes a TransformOutcome to `out.json`. A value JSON can't carry (a BigInt, a
32
- * cycle, or a function or symbol, which JSON would drop without a word) is an error. The program writes nothing else and reads no environment: the isolation is the computer's.
22
+ * A code node as one script for a JavaScript engine of the host's own, for a host that runs code nodes in an isolated
23
+ * engine (a WebAssembly one) rather than in its own process. The script is a function expression: called with one
24
+ * string, the JSON of `{ state, ctx }`, it builds the function exactly as compileTransform does (the same factory text,
25
+ * denied globals and synchronous checks), calls it once, and returns one string, the JSON of a TransformOutcome. A value
26
+ * JSON can't carry (a BigInt, a cycle, or a function or symbol, which JSON would drop without a word) is an error. It
27
+ * uses only the language itself: no module, no host object, nothing in or out but the two strings.
33
28
  */
34
- export declare function transformProgram(src: string): string;
29
+ export declare function transformScript(src: string): string;
package/dist/code-exec.js CHANGED
@@ -140,9 +140,9 @@ const EMPTY_SOURCE = "code transform source must be a non-empty string";
140
140
  const ASYNC_FUNCTION = "code transforms are synchronous; async functions are not supported";
141
141
  const THENABLE = "code transforms are synchronous; returned a Promise or thenable";
142
142
  /** The start of the error a code node's program gives for a value JSON would drop silently (a function, a symbol). */
143
- const NOT_JSON = "a code node run in an isolated computer must return JSON data, and its value holds a ";
143
+ const NOT_JSON = "a code node must return JSON data, and its value holds a ";
144
144
  /** The factory's body: `src` evaluated once, strict, under the denied globals' parameters. Both compileTransform and
145
- * transformProgram build their function from this one text, so a code node means the same wherever it runs. */
145
+ * transformScript build their function from this one text, so a code node means the same wherever it runs. */
146
146
  const factoryBody = (src) => `"use strict";\nconst __fn = (${src});\nif (typeof __fn !== "function") throw new Error("code must evaluate to a single (value) => value function");\nreturn __fn;`;
147
147
  function createTransformFactory(src) {
148
148
  if (typeof src !== "string" || !src.trim())
@@ -177,46 +177,44 @@ export function compileTransform(src) {
177
177
  return result;
178
178
  };
179
179
  }
180
- /** The files a code node's program reads and writes, in the directory it runs in (transformProgram). */
181
- export const TRANSFORM_FILES = { program: "run.mjs", input: "in.json", output: "out.json" };
182
180
  /**
183
- * A code node as a standalone Node.js program (ES module), for a host that runs code nodes in an isolated computer
184
- * rather than in its own process. Run with `node run.mjs` in a directory holding `in.json` ({ state, ctx }), it builds
185
- * the function exactly as compileTransform does (the same factory text, denied globals and synchronous checks), calls
186
- * it once with the state and ctx, and writes a TransformOutcome to `out.json`. A value JSON can't carry (a BigInt, a
187
- * cycle, or a function or symbol, which JSON would drop without a word) is an error. The program writes nothing else and reads no environment: the isolation is the computer's.
181
+ * A code node as one script for a JavaScript engine of the host's own, for a host that runs code nodes in an isolated
182
+ * engine (a WebAssembly one) rather than in its own process. The script is a function expression: called with one
183
+ * string, the JSON of `{ state, ctx }`, it builds the function exactly as compileTransform does (the same factory text,
184
+ * denied globals and synchronous checks), calls it once, and returns one string, the JSON of a TransformOutcome. A value
185
+ * JSON can't carry (a BigInt, a cycle, or a function or symbol, which JSON would drop without a word) is an error. It
186
+ * uses only the language itself: no module, no host object, nothing in or out but the two strings.
188
187
  */
189
- export function transformProgram(src) {
188
+ export function transformScript(src) {
190
189
  if (typeof src !== "string" || !src.trim())
191
190
  throw new Error(EMPTY_SOURCE);
192
191
  const text = JSON.stringify;
193
192
  return [
194
- `import { readFileSync, writeFileSync } from "node:fs";`,
195
- `import { isAsyncFunction, isPromise } from "node:util/types";`,
196
- `const DENIED = ${text(DENIED_GLOBALS)};`,
197
- `const at = (name) => new URL("./" + name, import.meta.url);`,
198
- `let outcome;`,
199
- `try {`,
200
- ` const input = JSON.parse(readFileSync(at(${text(TRANSFORM_FILES.input)}), "utf8"));`,
201
- ` let factory;`,
202
- ` try { factory = new Function(...DENIED, ${text(factoryBody(src))}); }`,
203
- ` catch (e) { throw new Error("code transform failed to compile: " + (e?.message || e)); }`,
204
- ` const fn = factory(...DENIED.map(() => undefined));`,
205
- ` if (isAsyncFunction(fn)) throw new Error(${text(ASYNC_FUNCTION)});`,
206
- ` const result = fn(input.state, input.ctx);`,
207
- ` if (isPromise(result)) { void Promise.prototype.then.call(result, undefined, () => {}); throw new Error(${text(THENABLE)}); }`,
208
- ` if (result && typeof result.then === "function") throw new Error(${text(THENABLE)});`,
209
- ` outcome = result === undefined ? { ok: true, undefined: true } : { ok: true, value: result };`,
210
- ` const kept = outcome;`,
211
- ` outcome = JSON.stringify(outcome, function (key, value) {`,
212
- ` if (typeof value !== "function" && typeof value !== "symbol") return value;`,
213
- ` const where = this === kept && key === "value" ? "" : " at " + JSON.stringify(key);`,
214
- ` throw new Error(${text(NOT_JSON)} + typeof value + where);`,
215
- ` });`,
216
- `} catch (e) {`,
217
- ` outcome = JSON.stringify({ ok: false, error: String(e?.message ?? e) });`,
218
- `}`,
219
- `writeFileSync(at(${text(TRANSFORM_FILES.output)}), outcome);`,
220
- ``,
193
+ `(function (input) {`,
194
+ ` "use strict";`,
195
+ ` var DENIED = ${text(DENIED_GLOBALS)};`,
196
+ ` var MakeFunction = Function, OwnPromise = Promise, stringify = JSON.stringify, parse = JSON.parse;`,
197
+ ` var AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;`,
198
+ ` var AsyncGeneratorFunction = Object.getPrototypeOf(async function* () {}).constructor;`,
199
+ ` try {`,
200
+ ` var given = parse(input);`,
201
+ ` var factory;`,
202
+ ` try { factory = MakeFunction.apply(null, DENIED.concat([${text(factoryBody(src))}])); }`,
203
+ ` catch (e) { throw new Error("code transform failed to compile: " + ((e && e.message) || e)); }`,
204
+ ` var fn = factory.apply(null, DENIED.map(function () { return undefined; }));`,
205
+ ` if (fn instanceof AsyncFunction || fn instanceof AsyncGeneratorFunction) throw new Error(${text(ASYNC_FUNCTION)});`,
206
+ ` var result = fn(given.state, given.ctx);`,
207
+ ` if (result instanceof OwnPromise) { OwnPromise.prototype.then.call(result, undefined, function () {}); throw new Error(${text(THENABLE)}); }`,
208
+ ` if (result && typeof result.then === "function") throw new Error(${text(THENABLE)});`,
209
+ ` var kept = result === undefined ? { ok: true, undefined: true } : { ok: true, value: result };`,
210
+ ` return stringify(kept, function (key, value) {`,
211
+ ` if (typeof value !== "function" && typeof value !== "symbol") return value;`,
212
+ ` var where = this === kept && key === "value" ? "" : " at " + stringify(key);`,
213
+ ` throw new Error(${text(NOT_JSON)} + typeof value + where);`,
214
+ ` });`,
215
+ ` } catch (e) {`,
216
+ ` return stringify({ ok: false, error: String(e && e.message !== undefined ? e.message : e) });`,
217
+ ` }`,
218
+ `})`,
221
219
  ].join("\n");
222
220
  }
package/dist/index.d.ts CHANGED
@@ -16,7 +16,7 @@ export type { DecisionProvider, DecisionCapabilities, DecisionInput, DecisionReq
16
16
  export { inspectWorkflow, formatWorkflowTree } from "./inspection.js";
17
17
  export type { WorkflowInspection, WorkflowInspectionNode } from "./inspection.js";
18
18
  export { runWorkflowSlice, assertWorkflowCapabilities, desugarWorkflow, stageSchemaForNode, submissionSchemaForNode, mergeStageDelta, REPORT_SCHEMA, SHELL_RESULT_SCHEMA, childSteps, declaredWrites, artifactNodeIsProse, schemaProblems, HOST_STATE_KEY, normalizeStringNullsForSchema, placeholderValue, placeholdersOf, } from "./workflow.js";
19
- export { compileTransform, compileTransformSyntax, deniedGlobalReferences, TRANSFORM_FILES, type TransformOutcome, transformProgram } from "./code-exec.js";
19
+ export { compileTransform, compileTransformSyntax, deniedGlobalReferences, type TransformOutcome, transformScript } from "./code-exec.js";
20
20
  export { resolveSchemaForWorkflow } from "./schema-references.js";
21
21
  export { patternRisk, PATTERN_SCREEN_LIMITS } from "./pattern-screen.js";
22
22
  export { submitParameters } from "./submit-envelope.js";
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ childSteps, declaredWrites, artifactNodeIsProse, schemaProblems, HOST_STATE_KEY,
14
14
  normalizeStringNullsForSchema,
15
15
  // Interpolation's placeholders in a string and its lookup order for one, for a host check of what a step reads.
16
16
  placeholderValue, placeholdersOf, } from "./workflow.js";
17
- export { compileTransform, compileTransformSyntax, deniedGlobalReferences, TRANSFORM_FILES, transformProgram } from "./code-exec.js";
17
+ export { compileTransform, compileTransformSyntax, deniedGlobalReferences, transformScript } from "./code-exec.js";
18
18
  export { resolveSchemaForWorkflow } from "./schema-references.js";
19
19
  export { patternRisk, PATTERN_SCREEN_LIMITS } from "./pattern-screen.js";
20
20
  export { submitParameters } from "./submit-envelope.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcha/agentrun-dsl",
3
- "version": "0.1.0-beta.124",
3
+ "version": "0.1.0-beta.125",
4
4
  "description": "Composable, typed workflows for agents",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",