@genroc/eval-node 0.0.0-edge.d8176a7 → 0.0.0-edge.dc4ff88
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 +54 -36
- package/dist/eval.js +65 -0
- package/dist/import.js +361 -0
- package/dist/realm.js +113 -0
- package/dist/worker.js +285 -0
- package/eval.ts +11 -4
- package/import.ts +89 -39
- package/package.json +6 -4
- package/realm.ts +61 -77
- package/worker.ts +8 -1
- package/bin/import.mjs +0 -4
- package/bin/worker.mjs +0 -2
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:
|
|
2
|
-
// the failure kinds in README.md. Nothing here knows about
|
|
3
|
-
// thing that talks to the queue — so this stays testable and
|
|
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
|
-
|
|
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
|