@genroc/eval-node 0.0.0-edge.4348678 → 0.0.0-edge.46dbe379
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/LICENSE +202 -0
- package/README.md +58 -34
- package/dist/eval.js +25 -4
- package/dist/import.js +130 -105
- package/dist/realm.js +66 -75
- package/dist/worker.js +39 -14
- package/eval.ts +25 -4
- package/import.ts +217 -126
- package/package.json +1 -1
- package/realm.ts +61 -77
- package/worker.ts +48 -17
package/dist/worker.js
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
// nothing about the queue, which is what keeps the containment strategy swappable.
|
|
5
5
|
//
|
|
6
6
|
// See README.md for the contract, and specs/external-task-queue.md for the queue itself.
|
|
7
|
-
import {
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
import { Cancelled, evaluate } from "./eval.js";
|
|
8
9
|
const SERVER = (process.env.GENROC_SERVER ?? "http://localhost:8448").replace(/\/$/, "");
|
|
9
10
|
const WORKER_ID = process.env.WORKER_ID ?? `evaluator-${process.pid}`;
|
|
10
11
|
// The credential, when the server runs with --auth token. A worker needs exactly the `worker`
|
|
@@ -15,7 +16,11 @@ const WORKER_ID = process.env.WORKER_ID ?? `evaluator-${process.pid}`;
|
|
|
15
16
|
// Sent as a header rather than in the URL because Node's fetch REFUSES a URL carrying
|
|
16
17
|
// credentials ("Request cannot be constructed from a URL that includes credentials"), so the
|
|
17
18
|
// basic-auth-in-the-URL trick that works for genctl is not available here.
|
|
18
|
-
|
|
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() : "");
|
|
19
24
|
const authHeaders = TOKEN ? { authorization: `Bearer ${TOKEN}` } : {};
|
|
20
25
|
// Concurrency is the worker's to set, and that is the point of pulling: under the old fetch
|
|
21
26
|
// shape genroc decided how many scripts ran at once (--max-concurrent, default 200) and the
|
|
@@ -57,15 +62,15 @@ async function fetchObject(ref) {
|
|
|
57
62
|
/** Put each listed value back where its path says it belongs. Paths are arrays of keys, so this
|
|
58
63
|
* needs no parser: the whole reason they are not JSON Pointer strings. */
|
|
59
64
|
async function resolveObjects(job) {
|
|
60
|
-
let input = job.
|
|
65
|
+
let input = job.external_input;
|
|
61
66
|
for (const e of job.objects ?? []) {
|
|
62
67
|
const value = await fetchObject(e.ref);
|
|
63
68
|
if (e.path.length === 0) {
|
|
64
69
|
input = value;
|
|
65
70
|
continue;
|
|
66
71
|
}
|
|
67
|
-
// The path is rooted at the entry and starts with "
|
|
68
|
-
const rest = e.path[0] === "
|
|
72
|
+
// The path is rooted at the entry and starts with "external_input", the value being rebuilt.
|
|
73
|
+
const rest = e.path[0] === "external_input" ? e.path.slice(1) : e.path;
|
|
69
74
|
if (rest.length === 0) {
|
|
70
75
|
input = value;
|
|
71
76
|
continue;
|
|
@@ -180,7 +185,7 @@ async function answer(token, outcome) {
|
|
|
180
185
|
console.error(`genroc refused the outcome for ${token}: ${JSON.stringify(data)}`);
|
|
181
186
|
await release(token);
|
|
182
187
|
}
|
|
183
|
-
async function run(job) {
|
|
188
|
+
async function run(job, signal) {
|
|
184
189
|
let resolved;
|
|
185
190
|
try {
|
|
186
191
|
resolved = await resolveObjects(job);
|
|
@@ -201,9 +206,17 @@ async function run(job) {
|
|
|
201
206
|
}
|
|
202
207
|
let result;
|
|
203
208
|
try {
|
|
204
|
-
result = await evaluate(req);
|
|
209
|
+
result = await evaluate(req, signal);
|
|
205
210
|
}
|
|
206
211
|
catch (err) {
|
|
212
|
+
// Cancelled is not a fault: the process was stopped while this ran. The claim still goes
|
|
213
|
+
// back — the row is terminal, so nothing re-claims it, and the release is what stops it
|
|
214
|
+
// waiting out a lease nobody is serving.
|
|
215
|
+
if (err instanceof Cancelled) {
|
|
216
|
+
console.log(`cancelled: ${job.token}`);
|
|
217
|
+
await release(job.token);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
207
220
|
// The RUNNER faulted, not the script — the one class where a retry can help. There is no
|
|
208
221
|
// error code for it on purpose: releasing the claim is how a queue spells "retryable", and
|
|
209
222
|
// it puts the task in front of a different worker instead of burning the definition's
|
|
@@ -240,11 +253,22 @@ async function renewLoop() {
|
|
|
240
253
|
tokens,
|
|
241
254
|
lease_ms: LEASE_MS,
|
|
242
255
|
});
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
//
|
|
246
|
-
|
|
247
|
-
|
|
256
|
+
if (!ok)
|
|
257
|
+
continue;
|
|
258
|
+
// Cancelled: the process was stopped, so the evaluation is abandoned and the claim handed
|
|
259
|
+
// back. Aborting first is the point of the list — waiting for the script to finish would
|
|
260
|
+
// keep burning a core on work an operator has already stopped. The release rides run()'s
|
|
261
|
+
// own catch, so nothing is released twice.
|
|
262
|
+
for (const token of (data?.cancelled ?? [])) {
|
|
263
|
+
inFlight.get(token)?.abort.abort();
|
|
264
|
+
}
|
|
265
|
+
// Lost: someone else holds this claim now. The work continues and its answer will be
|
|
266
|
+
// refused — that is not fixable here — but say so, because it is the signal that LEASE_MS
|
|
267
|
+
// is too short for what these scripts actually take. Deliberately NOT released: the claim
|
|
268
|
+
// is the new holder's, and releasing would bump the epoch out from under it.
|
|
269
|
+
const lost = (data?.lost ?? []);
|
|
270
|
+
if (lost.length) {
|
|
271
|
+
console.error(`lost ${lost.length}/${tokens.length} claims; a lease lapsed under load`);
|
|
248
272
|
}
|
|
249
273
|
}
|
|
250
274
|
}
|
|
@@ -253,8 +277,9 @@ async function pollLoop() {
|
|
|
253
277
|
const free = CONCURRENCY - inFlight.size;
|
|
254
278
|
const jobs = free > 0 ? await claim(free) : [];
|
|
255
279
|
for (const job of jobs) {
|
|
256
|
-
|
|
257
|
-
|
|
280
|
+
const abort = new AbortController();
|
|
281
|
+
inFlight.set(job.token, { job, abort });
|
|
282
|
+
void run(job, abort.signal).finally(() => inFlight.delete(job.token));
|
|
258
283
|
}
|
|
259
284
|
// Only idle when there was nothing to take: a full queue should be drained at the speed the
|
|
260
285
|
// realms allow, not at the poll interval.
|
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;
|
|
@@ -55,14 +57,32 @@ class RealmFault extends Error {
|
|
|
55
57
|
}
|
|
56
58
|
}
|
|
57
59
|
|
|
58
|
-
|
|
60
|
+
/** Thrown when `signal` aborts: the work was cancelled server-side, so there is no outcome to
|
|
61
|
+
* report and worker.ts must release rather than answer. Distinct from RealmFault because the
|
|
62
|
+
* runner did not fault -- nothing is wrong, the answer is simply no longer wanted. */
|
|
63
|
+
export class Cancelled extends Error {
|
|
64
|
+
constructor() {
|
|
65
|
+
super("cancelled");
|
|
66
|
+
this.name = "Cancelled";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export async function evaluate(req: EvalRequest, signal?: AbortSignal): Promise<EvalResult> {
|
|
59
71
|
const budget = typeof req.timeout_ms === "number" ? req.timeout_ms : DEFAULT_TIMEOUT_MS;
|
|
60
72
|
|
|
61
73
|
const worker = new Worker(REALM_URL);
|
|
62
74
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
75
|
+
let onAbort: (() => void) | undefined;
|
|
63
76
|
try {
|
|
64
77
|
return await new Promise<EvalResult>((resolve, reject) => {
|
|
65
78
|
timer = setTimeout(() => resolve(timedOut(budget)), budget);
|
|
79
|
+
// The abort is wired to the same promise as the budget, so both settle through the one
|
|
80
|
+
// finally below -- which is what guarantees the thread is gone before either returns.
|
|
81
|
+
if (signal) {
|
|
82
|
+
if (signal.aborted) reject(new Cancelled());
|
|
83
|
+
onAbort = () => reject(new Cancelled());
|
|
84
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
85
|
+
}
|
|
66
86
|
worker.once("message", (reply: WorkerReply) => resolve(reply));
|
|
67
87
|
// A script may end its own realm (`process.exit()`), which is not a throw and would
|
|
68
88
|
// otherwise present as a hang until the budget expired. Our own terminate() raises
|
|
@@ -73,6 +93,7 @@ export async function evaluate(req: EvalRequest): Promise<EvalResult> {
|
|
|
73
93
|
});
|
|
74
94
|
} finally {
|
|
75
95
|
clearTimeout(timer);
|
|
96
|
+
if (signal && onAbort) signal.removeEventListener("abort", onAbort);
|
|
76
97
|
// Awaited, and the whole point: on the timeout path a thread is still burning a core, and
|
|
77
98
|
// resolving before it is gone would report an evaluation the machine is still running.
|
|
78
99
|
await worker.terminate();
|