@genroc/eval-node 0.0.0-edge.aff7b37 → 0.0.0-edge.ca3207b

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/dist/realm.js ADDED
@@ -0,0 +1,113 @@
1
+ // The evaluation realm. One Worker per execution: a fresh global object per script, and a
2
+ // thread the host can kill mid-loop — the only thing that bounds a synchronous busy loop.
3
+ // eval.ts owns the budget and does the killing; nothing here knows about time.
4
+ //
5
+ // Everything that touches the script's VALUE lives on this side of the boundary — loading,
6
+ // classifying, serialising — because this is the only realm the value exists in.
7
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
8
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
9
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
10
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
11
+ });
12
+ }
13
+ return path;
14
+ };
15
+ import { registerHooks } from "node:module";
16
+ import { parentPort } from "node:worker_threads";
17
+ // The script is IMPORTED as a module under a URL of our own, not compiled from a string: its
18
+ // frames then carry the author's own line numbers, and an `import` of a node builtin resolves
19
+ // the way it does everywhere else.
20
+ const SCRIPT_URL = "script:main";
21
+ const STACK_BYTES = 2_048;
22
+ // The only channel a load hook has to the source. Written per request and read once — a realm
23
+ // evaluates one script and is then discarded, so no second execution can observe it.
24
+ let source = "";
25
+ registerHooks({
26
+ resolve: (specifier, context, next) => specifier === SCRIPT_URL ? { url: SCRIPT_URL, shortCircuit: true } : next(specifier, context),
27
+ load: (url, context, next) => url === SCRIPT_URL ? { format: "module", source, shortCircuit: true } : next(url, context),
28
+ });
29
+ /** Keeps the frames that are the script's own. Everything below the last of them is runner
30
+ * plumbing the author cannot act on, and V8 puts this file's path in it. */
31
+ function scriptStack(err) {
32
+ if (!(err instanceof Error) || typeof err.stack !== "string")
33
+ return undefined;
34
+ const lines = err.stack.split("\n");
35
+ let last = 0;
36
+ for (let i = 0; i < lines.length; i++)
37
+ if (lines[i].includes(SCRIPT_URL))
38
+ last = i;
39
+ const stack = lines.slice(0, last + 1).join("\n");
40
+ return stack.length > STACK_BYTES ? stack.slice(0, STACK_BYTES) : stack;
41
+ }
42
+ function describe(err, kind) {
43
+ if (err instanceof Error) {
44
+ return { kind, name: err.name, message: err.message, stack: scriptStack(err) };
45
+ }
46
+ // A script may throw a non-Error (`throw {code: "x"}`), so name/message must not assume one.
47
+ return { kind, name: "Thrown", message: safeText(err) };
48
+ }
49
+ function safeText(v) {
50
+ try {
51
+ return typeof v === "string" ? v : JSON.stringify(v) ?? String(v);
52
+ }
53
+ catch {
54
+ return String(v);
55
+ }
56
+ }
57
+ /** A module that will not parse, or names an import nothing resolves, is broken code — only
58
+ * editing it helps. Anything else thrown by the import is the module's top level running,
59
+ * which is the script throwing. */
60
+ function unloadable(err) {
61
+ const code = err?.code;
62
+ return err instanceof SyntaxError || (typeof code === "string" && code.startsWith("ERR_MODULE"));
63
+ }
64
+ async function run(req) {
65
+ source = req.code;
66
+ let main;
67
+ try {
68
+ main = (await import(__rewriteRelativeImportExtension(SCRIPT_URL))).default;
69
+ }
70
+ catch (err) {
71
+ return { ok: false, failure: describe(err, unloadable(err) ? "compile_error" : "threw") };
72
+ }
73
+ if (typeof main !== "function") {
74
+ const got = main === undefined ? "no default export" : `a ${typeof main}`;
75
+ return {
76
+ ok: false,
77
+ failure: {
78
+ kind: "compile_error",
79
+ name: "NoDefaultExport",
80
+ message: `a script must export default a function; this one has ${got}`,
81
+ },
82
+ };
83
+ }
84
+ let value;
85
+ try {
86
+ value = await main(req.input);
87
+ }
88
+ catch (err) {
89
+ return { ok: false, failure: describe(err, "threw") };
90
+ }
91
+ try {
92
+ // undefined stringifies to undefined, not "undefined"; an empty body is how genroc
93
+ // spells null, which is the right reading of a script that returned nothing.
94
+ return { ok: true, body: value === undefined ? "" : JSON.stringify(value) ?? "" };
95
+ }
96
+ catch (err) {
97
+ return { ok: false, failure: describe(err, "nonserializable") };
98
+ }
99
+ }
100
+ // The realm's stdio is a pipe to the host thread, and eval.ts terminates this thread the moment
101
+ // the reply lands — so whatever a script wrote last is still in the pipe when it dies. An empty
102
+ // write's callback fires once the queue ahead of it has drained, which makes the reply a barrier
103
+ // for `console` and a direct process.stdout.write alike: both are this stream.
104
+ function flush(stream) {
105
+ return new Promise((resolve) => stream.write("", () => resolve()));
106
+ }
107
+ // Non-null because this module only ever runs as a worker entry point; a null port here
108
+ // would mean eval.ts loaded it as a plain module, which nothing does.
109
+ parentPort.on("message", async (req) => {
110
+ const reply = await run(req);
111
+ await Promise.all([flush(process.stdout), flush(process.stderr)]);
112
+ parentPort.postMessage(reply);
113
+ });
package/dist/worker.js ADDED
@@ -0,0 +1,285 @@
1
+ #!/usr/bin/env node
2
+ // The queue worker: claims parked `external` script tasks from genroc, evaluates each in its
3
+ // own realm, and answers. This is the whole genroc-facing half — eval.ts and realm.ts know
4
+ // nothing about the queue, which is what keeps the containment strategy swappable.
5
+ //
6
+ // See README.md for the contract, and specs/external-task-queue.md for the queue itself.
7
+ import { readFileSync } from "node:fs";
8
+ import { evaluate } from "./eval.js";
9
+ const SERVER = (process.env.GENROC_SERVER ?? "http://localhost:8448").replace(/\/$/, "");
10
+ const WORKER_ID = process.env.WORKER_ID ?? `evaluator-${process.pid}`;
11
+ // The credential, when the server runs with --auth token. A worker needs exactly the `worker`
12
+ // permission — the four queue verbs plus GET /api/objects — so mint it scoped rather than
13
+ // handing a worker an admin token: this is the credential most likely to sit on a machine you
14
+ // trust least. specs/api-auth.md §5.
15
+ //
16
+ // Sent as a header rather than in the URL because Node's fetch REFUSES a URL carrying
17
+ // credentials ("Request cannot be constructed from a URL that includes credentials"), so the
18
+ // basic-auth-in-the-URL trick that works for genctl is not available here.
19
+ // GENROC_TOKEN_FILE is the mounted-secret shape: a credential in a file rather than an
20
+ // environment variable, so it stays out of `docker inspect` and out of the process environment
21
+ // any child inherits. The inline variable wins when both are set.
22
+ const TOKEN = process.env.GENROC_TOKEN ??
23
+ (process.env.GENROC_TOKEN_FILE ? readFileSync(process.env.GENROC_TOKEN_FILE, "utf8").trim() : "");
24
+ const authHeaders = TOKEN ? { authorization: `Bearer ${TOKEN}` } : {};
25
+ // Concurrency is the worker's to set, and that is the point of pulling: under the old fetch
26
+ // shape genroc decided how many scripts ran at once (--max-concurrent, default 200) and the
27
+ // evaluator accepted every one of them. Here it claims what it can run and no more, so a
28
+ // backlog is a queue rather than 200 threads fighting over a core.
29
+ const CONCURRENCY = Number(process.env.CONCURRENCY ?? 4);
30
+ const POLL_MS = Number(process.env.POLL_MS ?? 250);
31
+ // The visibility timeout. Short, and renewed while work is in flight: a worker that dies
32
+ // should return its task quickly rather than holding it for the whole budget.
33
+ const LEASE_MS = Number(process.env.LEASE_MS ?? 30_000);
34
+ const RENEW_MS = Math.max(1_000, Math.floor(LEASE_MS / 3));
35
+ const PROCESS_FILTER = process.env.PROCESS ?? "";
36
+ const TASK_FILTER = process.env.TASK ?? "";
37
+ // Values too large to ship inline are listed rather than carried, and a bundle is exactly that:
38
+ // one object shared by every instance of a definition version, fetched once instead of copied
39
+ // into each task. A ref is a content hash, so it is immutable — the cache never invalidates.
40
+ const objectCache = new Map();
41
+ async function fetchObject(ref) {
42
+ const cached = objectCache.get(ref);
43
+ if (cached !== undefined)
44
+ return cached;
45
+ // Left to throw on purpose: this runs while a task is IN FLIGHT, and a task whose input
46
+ // cannot be fetched must fail rather than silently run against a missing value. The caller
47
+ // releases the claim, so the task returns to the queue.
48
+ const res = await fetch(`${SERVER}/api/objects/${encodeURIComponent(ref)}`, { headers: authHeaders });
49
+ if (!res.ok)
50
+ throw new Error(`fetch object ${ref}: HTTP ${res.status}`);
51
+ const { data } = (await res.json());
52
+ let value;
53
+ try {
54
+ value = JSON.parse(data);
55
+ }
56
+ catch {
57
+ value = data;
58
+ }
59
+ objectCache.set(ref, value);
60
+ return value;
61
+ }
62
+ /** Put each listed value back where its path says it belongs. Paths are arrays of keys, so this
63
+ * needs no parser: the whole reason they are not JSON Pointer strings. */
64
+ async function resolveObjects(job) {
65
+ let input = job.input;
66
+ for (const e of job.objects ?? []) {
67
+ const value = await fetchObject(e.ref);
68
+ if (e.path.length === 0) {
69
+ input = value;
70
+ continue;
71
+ }
72
+ // The path is rooted at the entry and starts with "input", which is the value being rebuilt.
73
+ const rest = e.path[0] === "input" ? e.path.slice(1) : e.path;
74
+ if (rest.length === 0) {
75
+ input = value;
76
+ continue;
77
+ }
78
+ let cur = input;
79
+ for (let i = 0; i < rest.length - 1; i++)
80
+ cur = cur?.[rest[i]];
81
+ if (cur)
82
+ cur[rest[rest.length - 1]] = value;
83
+ }
84
+ return input;
85
+ }
86
+ async function call(path, body) {
87
+ // A network error is a REPLY, not a throw. A worker outlives the server it polls — a
88
+ // restart, a rolling deploy, a container coming up before genroc is listening — and an
89
+ // unhandled rejection here kills it for a condition the next poll would clear. Status 0
90
+ // says "never reached the server", which is distinct from anything genroc answers.
91
+ let res;
92
+ try {
93
+ res = await fetch(SERVER + path, {
94
+ method: "POST",
95
+ headers: { "content-type": "application/json", ...authHeaders },
96
+ body: JSON.stringify(body),
97
+ });
98
+ }
99
+ catch (err) {
100
+ return { ok: false, status: 0, data: { error: `${SERVER} unreachable: ${err.message}` } };
101
+ }
102
+ const text = await res.text();
103
+ let data = null;
104
+ try {
105
+ data = text ? JSON.parse(text) : null;
106
+ }
107
+ catch {
108
+ data = { error: text };
109
+ }
110
+ return { ok: res.ok, status: res.status, data };
111
+ }
112
+ /** The task input IS an EvalRequest: `code` required, `input` and `timeout_ms` optional. A task
113
+ * whose input is not that shape is the definition's fault, not the script's, and is reported
114
+ * as a compile_error — the nearest permanent kind, since no retry can fix the definition. */
115
+ function asEvalRequest(input) {
116
+ if (typeof input !== "object" || input === null)
117
+ return "the task input is not an object";
118
+ const r = input;
119
+ if (typeof r.code !== "string")
120
+ return "the task input has no `code` string";
121
+ return {
122
+ code: r.code,
123
+ input: r.input,
124
+ timeout_ms: typeof r.timeout_ms === "number" ? r.timeout_ms : undefined,
125
+ };
126
+ }
127
+ const inFlight = new Map();
128
+ let running = true;
129
+ // Whether the last claim reached genroc. A worker polls several times a second, so an
130
+ // unreachable server would otherwise emit a line per poll — thousands during a restart, which
131
+ // buries the one line that mattered. Announce the TRANSITIONS instead: going away, and coming
132
+ // back. Silence in between is the report that nothing changed.
133
+ let serverReachable = true;
134
+ async function claim(n) {
135
+ const { ok, status, data } = await call("/api/external-tasks/claim", {
136
+ worker_id: WORKER_ID,
137
+ limit: n,
138
+ lease_ms: LEASE_MS,
139
+ ...(PROCESS_FILTER ? { process: PROCESS_FILTER } : {}),
140
+ ...(TASK_FILTER ? { task: TASK_FILTER } : {}),
141
+ });
142
+ if (!ok) {
143
+ // A credential problem is not transient, and polling through it looks like a healthy
144
+ // worker that never picks anything up — the worst shape for an operator to debug. Exit
145
+ // instead, so a supervisor restarts it and the failure is visible where it happened.
146
+ if (status === 401 || status === 403) {
147
+ console.error(`claim rejected (${status}): ${JSON.stringify(data)}\n` +
148
+ `The server requires authentication. Set GENROC_TOKEN to a token with the 'worker' ` +
149
+ `permission — mint one with: genctl token create --perms worker --label evaluator -q`);
150
+ process.exit(1);
151
+ }
152
+ // status 0 is "never reached the server" (see call): a restart, a rolling deploy, a
153
+ // network blip. Not an error to act on — the next poll clears it — so it is reported once
154
+ // and then waited out.
155
+ if (status === 0) {
156
+ if (serverReachable) {
157
+ serverReachable = false;
158
+ console.error(`genroc at ${SERVER} is unreachable — ${data?.error ?? ""}. ` +
159
+ `Still polling every ${POLL_MS}ms; work resumes when it comes back.`);
160
+ }
161
+ return [];
162
+ }
163
+ console.error(`claim failed: ${JSON.stringify(data)}`);
164
+ return [];
165
+ }
166
+ if (!serverReachable) {
167
+ serverReachable = true;
168
+ console.error(`genroc at ${SERVER} is reachable again — resuming.`);
169
+ }
170
+ return (data?.items ?? []);
171
+ }
172
+ async function release(token) {
173
+ const { ok, data } = await call("/api/external-tasks/release", { token });
174
+ if (!ok)
175
+ console.error(`release failed: ${JSON.stringify(data)}`);
176
+ }
177
+ /** answer submits the outcome. A refusal is NOT retried with a different one: the definition
178
+ * declared a contract this worker does not satisfy (an undeclared code, a payload that does
179
+ * not fit `raises`), and guessing again would only pick a second wrong answer. Release it, so
180
+ * the task returns to the queue and an operator sees it waiting rather than silently gone. */
181
+ async function answer(token, outcome) {
182
+ const { ok, data } = await call("/api/external-tasks/resolve", { token, ...outcome });
183
+ if (ok)
184
+ return;
185
+ console.error(`genroc refused the outcome for ${token}: ${JSON.stringify(data)}`);
186
+ await release(token);
187
+ }
188
+ async function run(job) {
189
+ let resolved;
190
+ try {
191
+ resolved = await resolveObjects(job);
192
+ }
193
+ catch (err) {
194
+ // The values are there or they are not; this is the runner failing to read them, not the
195
+ // script failing, so hand the task back for someone else rather than reporting an outcome.
196
+ console.error(`resolving objects for ${job.token}: ${err instanceof Error ? err.message : String(err)}`);
197
+ await release(job.token);
198
+ return;
199
+ }
200
+ const req = asEvalRequest(resolved);
201
+ if (typeof req === "string") {
202
+ await answer(job.token, {
203
+ error: { code: "compile_error", message: req, data: { name: "BadTaskInput" } },
204
+ });
205
+ return;
206
+ }
207
+ let result;
208
+ try {
209
+ result = await evaluate(req);
210
+ }
211
+ catch (err) {
212
+ // The RUNNER faulted, not the script — the one class where a retry can help. There is no
213
+ // error code for it on purpose: releasing the claim is how a queue spells "retryable", and
214
+ // it puts the task in front of a different worker instead of burning the definition's
215
+ // on_error budget on this one's bad day.
216
+ console.error(`evaluator fault on ${job.token}: ${err instanceof Error ? err.message : String(err)}`);
217
+ await release(job.token);
218
+ return;
219
+ }
220
+ if (result.ok) {
221
+ // `body` is JSON text produced inside the realm; an empty body is a script that returned
222
+ // nothing, which genroc reads as null.
223
+ await answer(job.token, { result: result.body === "" ? null : JSON.parse(result.body) });
224
+ return;
225
+ }
226
+ const f = result.failure;
227
+ await answer(job.token, {
228
+ error: {
229
+ // The failure KIND is the code, so an on_error rule branches on what went wrong without
230
+ // reading a payload. Every kind is permanent; see eval.ts.
231
+ code: f.kind,
232
+ message: f.message,
233
+ data: { name: f.name, ...(f.stack ? { stack: f.stack } : {}) },
234
+ },
235
+ });
236
+ }
237
+ async function renewLoop() {
238
+ while (running) {
239
+ await new Promise((r) => setTimeout(r, RENEW_MS));
240
+ const tokens = [...inFlight.keys()];
241
+ if (!tokens.length)
242
+ continue;
243
+ const { ok, data } = await call("/api/external-tasks/renew", {
244
+ worker_id: WORKER_ID,
245
+ tokens,
246
+ lease_ms: LEASE_MS,
247
+ });
248
+ // A short count means a claim lapsed and was taken over. Nothing to do about it — the work
249
+ // continues and its answer will be refused — but say so, because it is the signal that
250
+ // LEASE_MS is too short for what these scripts actually take.
251
+ if (ok && data?.renewed < tokens.length) {
252
+ console.error(`renewed ${data.renewed}/${tokens.length} claims; a lease lapsed under load`);
253
+ }
254
+ }
255
+ }
256
+ async function pollLoop() {
257
+ while (running) {
258
+ const free = CONCURRENCY - inFlight.size;
259
+ const jobs = free > 0 ? await claim(free) : [];
260
+ for (const job of jobs) {
261
+ inFlight.set(job.token, job);
262
+ void run(job).finally(() => inFlight.delete(job.token));
263
+ }
264
+ // Only idle when there was nothing to take: a full queue should be drained at the speed the
265
+ // realms allow, not at the poll interval.
266
+ if (jobs.length === 0)
267
+ await new Promise((r) => setTimeout(r, POLL_MS));
268
+ }
269
+ }
270
+ async function shutdown(signal) {
271
+ if (!running)
272
+ return;
273
+ running = false;
274
+ const tokens = [...inFlight.keys()];
275
+ console.log(`${signal}: releasing ${tokens.length} claim(s)`);
276
+ // Hand work back rather than letting it sit out its lease. The evaluations still running are
277
+ // abandoned, which is exactly what the release says: nobody answered.
278
+ await Promise.all(tokens.map(release));
279
+ process.exit(0);
280
+ }
281
+ process.on("SIGINT", () => void shutdown("SIGINT"));
282
+ process.on("SIGTERM", () => void shutdown("SIGTERM"));
283
+ console.log(`evaluator worker ${WORKER_ID} polling ${SERVER} (concurrency=${CONCURRENCY}, lease=${LEASE_MS}ms)`);
284
+ void renewLoop();
285
+ void pollLoop();
package/eval.ts CHANGED
@@ -1,6 +1,7 @@
1
- // Evaluation core: run a code string in its OWN realm and classify every outcome into one of
2
- // the failure kinds in README.md. Nothing here knows about genroc — worker.ts is the only
3
- // thing that talks to the queue — so this stays testable and the containment stays swappable.
1
+ // Evaluation core: import a script module in its OWN realm, call its default export, and
2
+ // classify every outcome into one of the failure kinds in README.md. Nothing here knows about
3
+ // genroc — worker.ts is the only thing that talks to the queue — so this stays testable and
4
+ // the containment stays swappable.
4
5
  //
5
6
  // The containment is a Worker per execution (realm.ts). It is what makes the budget real:
6
7
  // a synchronous busy loop never yields, so no in-process timer can interrupt it, and only a
@@ -9,6 +10,7 @@
9
10
  import { Worker } from "node:worker_threads";
10
11
 
11
12
  export type EvalRequest = {
13
+ /** An ES module whose default export is the function to run; `input` is its argument. */
12
14
  code: string;
13
15
  input?: unknown;
14
16
  timeout_ms?: number;
@@ -38,7 +40,12 @@ export type WorkerRequest = { code: string; input?: unknown };
38
40
  export type WorkerReply = EvalResult;
39
41
 
40
42
  const DEFAULT_TIMEOUT_MS = 5_000;
41
- const REALM_URL = new URL("./realm.ts", import.meta.url);
43
+ // Resolved from THIS file's own extension: run from a checkout it is `.ts`, and from the
44
+ // published package `.js`, because Node will not strip types under node_modules.
45
+ const REALM_URL = new URL(
46
+ import.meta.url.endsWith(".ts") ? "./realm.ts" : "./realm.js",
47
+ import.meta.url,
48
+ );
42
49
 
43
50
  /** Thrown, not returned: a realm that fails to start is the RUNNER faulting, which worker.ts
44
51
  * answers by releasing the claim rather than by reporting an outcome. A script fault is a
package/import.ts CHANGED
@@ -1,3 +1,4 @@
1
+ #!/usr/bin/env node
1
2
  // The code-phase resolver: manifest on stdin, `{"code": [...]}` on stdout, non-zero exit
2
3
  // with the diagnostic on stderr. genctl never parses TypeScript and this never parses YAML
3
4
  // — the manifest is the whole contract. See specs/source-resolution.md.
@@ -71,13 +72,16 @@ function tsType(s: Schema | undefined, used: Set<string>): string {
71
72
  if (Array.isArray(s.enum)) {
72
73
  return s.enum.map((v: unknown) => JSON.stringify(v)).join(" | ") || "never";
73
74
  }
74
- if (Array.isArray(s.anyOf)) return union(s.anyOf.map((a: Schema) => tsType(a, used)));
75
- if (Array.isArray(s.oneOf)) return union(s.oneOf.map((a: Schema) => tsType(a, used)));
75
+ if (Array.isArray(s.anyOf))
76
+ return union(s.anyOf.map((a: Schema) => tsType(a, used)));
77
+ if (Array.isArray(s.oneOf))
78
+ return union(s.oneOf.map((a: Schema) => tsType(a, used)));
76
79
  if (Array.isArray(s.allOf)) {
77
80
  return s.allOf.map((a: Schema) => tsType(a, used)).join(" & ") || "unknown";
78
81
  }
79
82
 
80
- const types: string[] = s.type === undefined ? [] : Array.isArray(s.type) ? s.type : [s.type];
83
+ const types: string[] =
84
+ s.type === undefined ? [] : Array.isArray(s.type) ? s.type : [s.type];
81
85
  if (types.length === 0) {
82
86
  // The top type: `{}` means unknown, not "an empty object". specs/unknown-type.md.
83
87
  return s.properties ? objectType(s, used) : "unknown";
@@ -110,8 +114,13 @@ function objectType(s: Schema, used: Set<string>): string {
110
114
  const required = new Set<string>(s.required ?? []);
111
115
  const lines: string[] = [];
112
116
  for (const [key, sub] of Object.entries(props)) {
113
- const doc = typeof sub.description === "string" ? ` /** ${sub.description} */\n` : "";
114
- lines.push(`${doc} ${propKey(key)}${required.has(key) ? "" : "?"}: ${tsType(sub, used)};`);
117
+ const doc =
118
+ typeof sub.description === "string"
119
+ ? ` /** ${sub.description} */\n`
120
+ : "";
121
+ lines.push(
122
+ `${doc} ${propKey(key)}${required.has(key) ? "" : "?"}: ${tsType(sub, used)};`,
123
+ );
115
124
  }
116
125
  if (s.additionalProperties && typeof s.additionalProperties === "object") {
117
126
  lines.push(` [key: string]: ${tsType(s.additionalProperties, used)};`);
@@ -127,9 +136,13 @@ function union(parts: string[]): string {
127
136
 
128
137
  const IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
129
138
  const propKey = (k: string) => (IDENT.test(k) ? k : JSON.stringify(k));
130
- const identifier = (n: string) => (IDENT.test(n) ? n : `Def_${n.replace(/[^A-Za-z0-9_$]/g, "_")}`);
139
+ const identifier = (n: string) =>
140
+ IDENT.test(n) ? n : `Def_${n.replace(/[^A-Za-z0-9_$]/g, "_")}`;
131
141
 
132
- function deref(s: Schema | undefined, defs: Record<string, Schema>): Schema | undefined {
142
+ function deref(
143
+ s: Schema | undefined,
144
+ defs: Record<string, Schema>,
145
+ ): Schema | undefined {
133
146
  let cur = s;
134
147
  for (let i = 0; cur && typeof cur.$ref === "string" && i < 16; i++) {
135
148
  cur = defs[cur.$ref.replace(/^#\/\$defs\//, "")];
@@ -141,7 +154,10 @@ function deref(s: Schema | undefined, defs: Record<string, Schema>): Schema | un
141
154
  * `{code, input, timeout_ms, …}`, and only its `input` field is bound as the script's
142
155
  * parameter. genroc cannot know that; this file owns the evaluator's wire contract, so
143
156
  * the navigation belongs here rather than in genctl. */
144
- function scriptInput(site: Site, defs: Record<string, Schema>): Schema | undefined {
157
+ function scriptInput(
158
+ site: Site,
159
+ defs: Record<string, Schema>,
160
+ ): Schema | undefined {
145
161
  const action = deref(site.input, defs);
146
162
  const bound = action?.properties?.input;
147
163
  return bound ?? site.input;
@@ -188,7 +204,10 @@ function typesPathFor(scriptPath: string): string {
188
204
  /** The nearest tsconfig above the script — the one the author's editor already reads. Two
189
205
  * different configs mean a red editor over a clean apply, or the reverse. The walk stops at
190
206
  * the project root: above it is not this project. */
191
- async function nearestTsconfig(from: string, root: string): Promise<string | null> {
207
+ async function nearestTsconfig(
208
+ from: string,
209
+ root: string,
210
+ ): Promise<string | null> {
192
211
  for (let dir = from; ; dir = dirname(dir)) {
193
212
  const candidate = join(dir, "tsconfig.json");
194
213
  if (await exists(candidate)) return candidate;
@@ -231,12 +250,18 @@ async function typecheck(root: string, sites: Site[]): Promise<void> {
231
250
  // base config there is nothing to opt in with, so the default stays none.
232
251
  ...(base ? {} : { types: [] }),
233
252
  },
234
- files: group.flatMap((s) => [relative(dir, s.path), relative(dir, typesPathFor(s.path))]),
253
+ files: group.flatMap((s) => [
254
+ relative(dir, s.path),
255
+ relative(dir, typesPathFor(s.path)),
256
+ ]),
235
257
  // `files` overrides the base's, but a base `include` survives beside it and would
236
258
  // drag the author's whole tree in, to be checked under the worker lib.
237
259
  include: [],
238
260
  };
239
- const configPath = join(dir, groups.size === 1 ? "tsconfig.json" : `tsconfig.${n++}.json`);
261
+ const configPath = join(
262
+ dir,
263
+ groups.size === 1 ? "tsconfig.json" : `tsconfig.${n++}.json`,
264
+ );
240
265
  await write(configPath, JSON.stringify(config, null, 2));
241
266
  await runTsc(root, configPath);
242
267
  }
@@ -284,7 +309,10 @@ const transpile: Plugin = {
284
309
  },
285
310
  };
286
311
 
287
- const BUILTIN = new Set([...builtinModules, ...builtinModules.map((m) => `node:${m}`)]);
312
+ const BUILTIN = new Set([
313
+ ...builtinModules,
314
+ ...builtinModules.map((m) => `node:${m}`),
315
+ ]);
288
316
 
289
317
  /** Resolves imports through TYPESCRIPT, using the same config the typecheck ran under, so a
290
318
  * `paths` alias that compiles also bundles. Reimplementing `paths` here would be a second
@@ -295,25 +323,37 @@ function tsResolve(configPath: string | null): Plugin {
295
323
  let options: ts.CompilerOptions = {};
296
324
  if (configPath) {
297
325
  const read = ts.readConfigFile(configPath, ts.sys.readFile);
298
- options = ts.parseJsonConfigFileContent(read.config ?? {}, ts.sys, dirname(configPath)).options;
326
+ options = ts.parseJsonConfigFileContent(
327
+ read.config ?? {},
328
+ ts.sys,
329
+ dirname(configPath),
330
+ ).options;
299
331
  }
300
332
  return {
301
333
  name: "genroc-ts-resolve",
302
334
  resolveId(source, importer) {
303
335
  if (!importer || BUILTIN.has(source)) return null;
304
- const { resolvedModule } = ts.resolveModuleName(source, importer, options, ts.sys);
305
- if (!resolvedModule || resolvedModule.isExternalLibraryImport) return null;
306
- return resolvedModule.resolvedFileName.endsWith(".d.ts") ? null : resolvedModule.resolvedFileName;
336
+ const { resolvedModule } = ts.resolveModuleName(
337
+ source,
338
+ importer,
339
+ options,
340
+ ts.sys,
341
+ );
342
+ if (!resolvedModule || resolvedModule.isExternalLibraryImport)
343
+ return null;
344
+ return resolvedModule.resolvedFileName.endsWith(".d.ts")
345
+ ? null
346
+ : resolvedModule.resolvedFileName;
307
347
  },
308
348
  };
309
349
  }
310
350
 
311
- /** Bundles to CJS and wraps it as an async function BODY, which is what /eval compiles.
312
- * The runtime stays unchanged: bundling is entirely the importer's job, and the string it
313
- * produces is self-contained, so a definition version pins its code forever. */
351
+ /** Bundles to a self-contained ES module, which is what the evaluator imports: the default
352
+ * export it calls is the author's own, so nothing wraps or rewrites the code between the two.
353
+ * Bundling is entirely the importer's job, so a definition version pins its code forever. */
314
354
  async function bundle(site: Site, root: string): Promise<string> {
315
- // Builtins are EXTERNALISED as `require` calls that worker.ts satisfies. Anything else
316
- // unresolved is a REFUSAL, not an external: rollup's default is to leave it as a require
355
+ // Builtins are EXTERNALISED as imports the realm resolves natively. Anything else
356
+ // unresolved is a REFUSAL, not an external: rollup's default is to leave it as an import
317
357
  // of a module that will not be there, which bundles clean and fails at runtime.
318
358
  const built = await rollup({
319
359
  input: site.path,
@@ -329,23 +369,26 @@ async function bundle(site: Site, root: string): Promise<string> {
329
369
  ],
330
370
  onwarn(warning) {
331
371
  if (warning.code === "UNRESOLVED_IMPORT") {
332
- die(`${site.path}: cannot resolve ${warning.exporter ?? "an import"} — is it installed?`);
372
+ die(
373
+ `${site.path}: cannot resolve ${warning.exporter ?? "an import"} — is it installed?`,
374
+ );
333
375
  }
334
376
  },
335
- }).catch((e: unknown) => die(`${site.path}: ${e instanceof Error ? e.message : String(e)}`));
377
+ }).catch((e: unknown) =>
378
+ die(`${site.path}: ${e instanceof Error ? e.message : String(e)}`),
379
+ );
336
380
 
337
- const { output } = await built.generate({ format: "cjs", exports: "auto", inlineDynamicImports: true });
381
+ const { output } = await built.generate({
382
+ format: "es",
383
+ inlineDynamicImports: true,
384
+ });
338
385
  await built.close();
339
- const cjs = output[0].code;
340
- return [
341
- "var module = { exports: {} }, exports = module.exports;",
342
- cjs,
343
- "var __genroc_main = module.exports.default ?? module.exports;",
344
- 'if (typeof __genroc_main !== "function") {',
345
- ` throw new Error(${JSON.stringify(`${site.path} has no default export function`)});`,
346
- "}",
347
- "return await __genroc_main(input);",
348
- ].join("\n");
386
+ // Refused here rather than in the realm: the evaluator can only report it against a running
387
+ // instance, and the file it names is on this machine.
388
+ if (!output[0].exports.includes("default")) {
389
+ die(`${site.path}: a script must \`export default\` the function to run`);
390
+ }
391
+ return output[0].code;
349
392
  }
350
393
 
351
394
  // ── main ───────────────────────────────────────────────────────────────────────
@@ -358,14 +401,16 @@ const stdin: string = await new Promise((resolve, reject) => {
358
401
  process.stdin.on("error", reject);
359
402
  });
360
403
  const manifest = JSON.parse(stdin) as Manifest;
361
- if (!manifest || !Array.isArray(manifest.sites)) die("stdin is not a genroc resolver manifest");
404
+ if (!manifest || !Array.isArray(manifest.sites))
405
+ die("stdin is not a genroc resolver manifest");
362
406
 
363
407
  // One script at two sites with different input types is a refusal, not a union: the union
364
408
  // is sound and would typecheck a body that is wrong at one of the sites.
365
409
  const byPath = new Map<string, Site>();
366
410
  for (const site of manifest.sites) {
367
411
  const seen = byPath.get(site.path);
368
- const defsOf = (x: Site) => (manifest.schemas[x.process]?.$defs ?? {}) as Record<string, Schema>;
412
+ const defsOf = (x: Site) =>
413
+ (manifest.schemas[x.process]?.$defs ?? {}) as Record<string, Schema>;
369
414
  if (
370
415
  seen &&
371
416
  JSON.stringify(scriptInput(seen, defsOf(seen))) !==
@@ -380,7 +425,10 @@ for (const site of manifest.sites) {
380
425
  }
381
426
 
382
427
  for (const site of byPath.values()) {
383
- const defs = (manifest.schemas[site.process]?.$defs ?? {}) as Record<string, Schema>;
428
+ const defs = (manifest.schemas[site.process]?.$defs ?? {}) as Record<
429
+ string,
430
+ Schema
431
+ >;
384
432
  await write(typesPathFor(site.path), declarations(site, defs));
385
433
  }
386
434