@parcha/agentrun-dsl 0.1.0-beta.116 → 0.1.0-beta.117

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,3 +8,27 @@ 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
18
+ * JSON has no word for), or the error it threw, in compileTransform's words. */
19
+ export type TransformOutcome = {
20
+ readonly ok: true;
21
+ readonly value?: unknown;
22
+ readonly undefined?: true;
23
+ } | {
24
+ readonly ok: false;
25
+ readonly error: string;
26
+ };
27
+ /**
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.
33
+ */
34
+ export declare function transformProgram(src: string): string;
package/dist/code-exec.js CHANGED
@@ -136,12 +136,20 @@ export function deniedGlobalReferences(src) {
136
136
  }
137
137
  return DENIED_GLOBALS.filter((name) => found.has(name));
138
138
  }
139
+ const EMPTY_SOURCE = "code transform source must be a non-empty string";
140
+ const ASYNC_FUNCTION = "code transforms are synchronous; async functions are not supported";
141
+ const THENABLE = "code transforms are synchronous; returned a Promise or thenable";
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 ";
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. */
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;`;
139
147
  function createTransformFactory(src) {
140
148
  if (typeof src !== "string" || !src.trim())
141
- throw new Error("code transform source must be a non-empty string");
149
+ throw new Error(EMPTY_SOURCE);
142
150
  let factory;
143
151
  try {
144
- factory = new Function(...DENIED_GLOBALS, `"use strict";\nconst __fn = (${src});\nif (typeof __fn !== "function") throw new Error("code must evaluate to a single (value) => value function");\nreturn __fn;`);
152
+ factory = new Function(...DENIED_GLOBALS, factoryBody(src));
145
153
  }
146
154
  catch (e) {
147
155
  throw new Error(`code transform failed to compile: ${e?.message || e}`);
@@ -156,16 +164,59 @@ export function compileTransformSyntax(src) {
156
164
  export function compileTransform(src) {
157
165
  const fn = createTransformFactory(src)(...DENIED_GLOBALS.map(() => undefined));
158
166
  if (isAsyncFunction(fn))
159
- throw new Error("code transforms are synchronous; async functions are not supported");
167
+ throw new Error(ASYNC_FUNCTION);
160
168
  return (value, ctx) => {
161
169
  const result = fn(value, ctx);
162
170
  if (isPromise(result)) {
163
171
  void Promise.prototype.then.call(result, undefined, () => { });
164
- throw new Error("code transforms are synchronous; returned a Promise or thenable");
172
+ throw new Error(THENABLE);
165
173
  }
166
174
  if (result && typeof result.then === "function") {
167
- throw new Error("code transforms are synchronous; returned a Promise or thenable");
175
+ throw new Error(THENABLE);
168
176
  }
169
177
  return result;
170
178
  };
171
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
+ /**
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.
188
+ */
189
+ export function transformProgram(src) {
190
+ if (typeof src !== "string" || !src.trim())
191
+ throw new Error(EMPTY_SOURCE);
192
+ const text = JSON.stringify;
193
+ 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
+ ``,
221
+ ].join("\n");
222
+ }
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 } from "./code-exec.js";
19
+ export { compileTransform, compileTransformSyntax, deniedGlobalReferences, TRANSFORM_FILES, type TransformOutcome, transformProgram } 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 } from "./code-exec.js";
17
+ export { compileTransform, compileTransformSyntax, deniedGlobalReferences, TRANSFORM_FILES, transformProgram } 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.116",
3
+ "version": "0.1.0-beta.117",
4
4
  "description": "Composable, typed workflows for agents",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",