@parcha/agentrun-computer 0.1.0-beta.39 → 0.1.0-beta.41
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 +56 -21
- package/box/Dockerfile +11 -58
- package/box/README.md +15 -2
- package/box/build-daytona.py +4 -2
- package/box/build-e2b.mjs +1 -1
- package/box/install.sh +148 -0
- package/box/recipe.mjs +12 -10
- package/dist/agent37.js +2 -1
- package/dist/attach.d.ts +46 -1
- package/dist/attach.js +48 -4
- package/dist/box-image.d.ts +2 -0
- package/dist/box-image.js +10 -0
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/computerd.json +2 -2
- package/dist/conformance/cases/isolation.js +17 -1
- package/dist/conformance/cases/lifetime.js +30 -0
- package/dist/conformance/cases/provider.js +63 -1
- package/dist/conformance/cli.js +27 -5
- package/dist/conformance/daytona.d.ts +25 -0
- package/dist/conformance/daytona.js +26 -1
- package/dist/conformance/exec-stream.d.ts +37 -7
- package/dist/conformance/exec-stream.js +349 -89
- package/dist/conformance/report.js +3 -0
- package/dist/conformance/runner.js +134 -12
- package/dist/conformance/sweep.d.ts +7 -2
- package/dist/conformance/sweep.js +17 -7
- package/dist/conformance/types.d.ts +42 -0
- package/dist/daytona.d.ts +8 -1
- package/dist/daytona.js +126 -101
- package/dist/descriptor.d.ts +22 -1
- package/dist/descriptor.js +18 -0
- package/dist/descriptors/daytona.d.ts +12 -3
- package/dist/descriptors/daytona.js +15 -7
- package/dist/descriptors/index.d.ts +8 -1
- package/dist/e2b.js +1 -1
- package/dist/host.d.ts +91 -26
- package/dist/host.js +5 -0
- package/dist/hosted/framed-exec.d.ts +38 -0
- package/dist/hosted/framed-exec.js +119 -0
- package/dist/hosted/process.d.ts +13 -5
- package/dist/hosted/process.js +39 -14
- package/dist/hosted/provider.d.ts +48 -6
- package/dist/hosted/provider.js +435 -64
- package/dist/hosted/rest.d.ts +29 -0
- package/dist/hosted/rest.js +91 -6
- package/dist/hosted/scripts.d.ts +58 -0
- package/dist/hosted/scripts.js +138 -1
- package/dist/hosted/ssh-exec.d.ts +7 -6
- package/dist/hosted/ssh-exec.js +35 -52
- package/dist/hosted/values.d.ts +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/kinds.d.ts +2 -0
- package/dist/modal.js +21 -8
- package/dist/providers.d.ts +4 -2
- package/package.json +1 -1
package/dist/hosted/provider.js
CHANGED
|
@@ -11,9 +11,12 @@ import { readFileSync } from "node:fs";
|
|
|
11
11
|
import { resolve } from "node:path";
|
|
12
12
|
import { fileURLToPath } from "node:url";
|
|
13
13
|
import { isDeepStrictEqual } from "node:util";
|
|
14
|
+
import { ATTACH_PATH, DAEMON_PATH } from "../attach.js";
|
|
15
|
+
import { isBoxImage } from "../box-image.js";
|
|
14
16
|
import { CreateRequests, endedWords, otherSpecWords, readBackDiffer } from "../create-requests.js";
|
|
15
17
|
import { desktopOf, startDisplay } from "../desktop.js";
|
|
16
18
|
import { COMPUTER_LABEL, DAEMON_IN_COMPUTER, DEADLINE_LABEL, helloDrift, probeNetworkDrift, probeNotes, probeReportOf, show, WORK } from "../drift.js";
|
|
19
|
+
import { BACKSTOP_MARGIN_MS } from "../host.js";
|
|
17
20
|
import { readOwnNetwork, TOOL_USER } from "../isolation.js";
|
|
18
21
|
import { releaseOlder } from "../janitor.js";
|
|
19
22
|
import { RUN_LABEL } from "../labels.js";
|
|
@@ -24,10 +27,10 @@ import { failFor, ok } from "../result.js";
|
|
|
24
27
|
import { daemonWatch } from "../screen-watch.js";
|
|
25
28
|
import { countTested } from "../test-connection.js";
|
|
26
29
|
import { waitUntil } from "../wait.js";
|
|
27
|
-
import { bounded, CALL_SLACK_MS, hostCall, openBridge, pause, rootOf, runToExit, seconds, uploadThroughExec } from "./process.js";
|
|
28
|
-
import { isWhole, packLabels } from "./values.js";
|
|
30
|
+
import { bounded, CALL_SLACK_MS, hostCall, OUTPUT_MAX, openBridge, pause, rootOf, runToExit, seconds, uploadThroughExec, words } from "./process.js";
|
|
31
|
+
import { isWhole, PACKED_NAME, packLabels, unpackLabels } from "./values.js";
|
|
29
32
|
import { EGRESS_POLICY, EGRESS_PROXY_USER, EGRESS_RELAY, HOSTED_ALLOW, NETWORK_LABEL, networkFormOf, networkOfForm, networkRefusal, throughProxy } from "./network.js";
|
|
30
|
-
import { ARCHIL_GATE, closedSocketsOf, daemonDrift, hostedCommands, installerOf } from "./scripts.js";
|
|
33
|
+
import { ARCHIL_GATE, closedSocketsOf, daemonDrift, hostedCommands, initFlags, installerOf, LABEL_FILE, labelsCommand } from "./scripts.js";
|
|
31
34
|
const VERSION = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version;
|
|
32
35
|
export const NAME_LABEL = `${COMPUTER_LABEL}.name`;
|
|
33
36
|
/** The image create was asked for: a vendor may report another name for it (a template's id). */
|
|
@@ -63,9 +66,23 @@ const ARCHIL_USER = `${ARCHIL_TOOL_UID}:${ARCHIL_TOOL_UID}`;
|
|
|
63
66
|
const RELEASE_POLL_MAX_MS = 5_000;
|
|
64
67
|
const LISTED_POLL_MS = 150;
|
|
65
68
|
const LISTED_ROUNDS = 2;
|
|
66
|
-
const LISTED_POLL_MAX_MS = 1_000;
|
|
67
69
|
const LISTEN_RETRY_MS = 250;
|
|
68
70
|
const START_POLL_MS = 500;
|
|
71
|
+
/**
|
|
72
|
+
* With labels kept in the machine: what a machine of a request's name with no label file must be older than, past the
|
|
73
|
+
* longest a create takes to write the file (#unfiledMs), before a sweep of the name takes it: another instance's longer
|
|
74
|
+
* timeouts, and the vendor's clock against this one's.
|
|
75
|
+
*/
|
|
76
|
+
const UNFILED_MARGIN_MS = 300_000;
|
|
77
|
+
/**
|
|
78
|
+
* The computerd build a hosted machine of arch runs: HostedProvider's default daemonPath, and the file serve's daemon
|
|
79
|
+
* route serves, so the sha256 a dial machine checks (AGENTRUN_DAEMON_SHA256) is this file's.
|
|
80
|
+
*/
|
|
81
|
+
export function hostedDaemonPath(arch) {
|
|
82
|
+
return fileURLToPath(new URL(`../computerd-linux-${arch}`, import.meta.url));
|
|
83
|
+
}
|
|
84
|
+
/** The dial creates in flight per request name, keyed by the server's AttachHub (#inTurn). */
|
|
85
|
+
const DIAL_TURNS = new WeakMap();
|
|
69
86
|
/** The longest a Node timer or AbortSignal.timeout holds. */
|
|
70
87
|
const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
71
88
|
/** The longest timeout the adapter takes: the race of a call bounded by it must still fit a Node timer. */
|
|
@@ -158,6 +175,8 @@ export class HostedProvider {
|
|
|
158
175
|
#uid;
|
|
159
176
|
#gid;
|
|
160
177
|
#timeouts;
|
|
178
|
+
/** What foundBy reads with: the timeouts, onNote, which hears of a machine a listing leaves out, and the image option. */
|
|
179
|
+
#finding;
|
|
161
180
|
#probeTargets;
|
|
162
181
|
#onNote;
|
|
163
182
|
#archilEgress;
|
|
@@ -166,8 +185,10 @@ export class HostedProvider {
|
|
|
166
185
|
#requests = new CreateRequests();
|
|
167
186
|
/** Each computer's last extend, by machine id, while one runs: the next waits for it. */
|
|
168
187
|
#extending = new Map();
|
|
188
|
+
/** A dial host's server: what its machines reach, and what holds their tokens and sockets. */
|
|
189
|
+
#dial;
|
|
169
190
|
constructor(host, options) {
|
|
170
|
-
const { kind = "linux", desktop = {}, maxLifetimeMs = host.facts.maxLifetimeMs, daemonPath =
|
|
191
|
+
const { kind = "linux", desktop = {}, maxLifetimeMs = host.facts.maxLifetimeMs, daemonPath = hostedDaemonPath(host.facts.arch), daemonUser = TOOL_USER, timeouts = {}, probeTargets = DEFAULT_PROBE_TARGETS, onNote, archilEgress, image, } = options;
|
|
171
192
|
const { id, label, capabilities } = host.descriptor;
|
|
172
193
|
if (kind !== "desktop" && kind !== "linux")
|
|
173
194
|
throw new Error(`kind is desktop or linux, not ${String(kind)}`);
|
|
@@ -184,6 +205,23 @@ export class HostedProvider {
|
|
|
184
205
|
if (!isWhole(maxLifetimeMs) || maxLifetimeMs <= 0 || maxLifetimeMs > host.facts.maxLifetimeMs) {
|
|
185
206
|
throw new Error(`maxLifetimeMs must be a whole number of ms above 0 and at most ${host.facts.maxLifetimeMs} ms, the longest ${label} keeps a computer`);
|
|
186
207
|
}
|
|
208
|
+
if (host.facts.connect === "dial") {
|
|
209
|
+
// Every connection goes over the socket the machine dials: nothing reaches into it from here.
|
|
210
|
+
if (host.facts.labels === "machine")
|
|
211
|
+
throw new Error(`${label}'s machines dial this server (facts.connect "dial") and keep no labels (facts.labels "machine"), which are read through exec: keep its labels in vendor tags or packed`);
|
|
212
|
+
if (host.exec || host.upload)
|
|
213
|
+
throw new Error(`${label}'s machines dial this server (facts.connect "dial"), so its host has no exec and no upload: leave them out`);
|
|
214
|
+
if (kind === "desktop")
|
|
215
|
+
throw new Error(`${label}'s machines dial this server (facts.connect "dial"), and agentrun-dial starts init with no display: make it kind linux`);
|
|
216
|
+
}
|
|
217
|
+
// A machine whose create was cut off before its labels were written carries none the janitor lists: only the vendor's
|
|
218
|
+
// timer ends it. And the most a computer may live, which bounds an extend, would be read from inside the machine.
|
|
219
|
+
if (host.facts.labels === "machine" && host.facts.lifetime !== "vendor") {
|
|
220
|
+
throw new Error(`${label} keeps no labels (facts.labels "machine"), so its vendor's timer must end its machines: a machine whose create stopped before its labels were written has none the janitor lists. Declare lifetime "vendor"`);
|
|
221
|
+
}
|
|
222
|
+
if (host.facts.labels === "machine" && host.extend) {
|
|
223
|
+
throw new Error(`${label} keeps no labels (facts.labels "machine") and has extend: the most a computer may live, which bounds an extend, would be read from a file inside the machine. Leave extend out: its computers end at the deadline create gave them`);
|
|
224
|
+
}
|
|
187
225
|
const user = USER.exec(daemonUser);
|
|
188
226
|
if (user === null || Number(user[1]) > MAX_ID || Number(user[2]) > MAX_ID)
|
|
189
227
|
throw new Error(`the daemon user ${daemonUser} must be uid:gid in numbers, such as 1000:1000`);
|
|
@@ -208,7 +246,11 @@ export class HostedProvider {
|
|
|
208
246
|
this.#maxLifetimeMs = maxLifetimeMs;
|
|
209
247
|
[this.#uid, this.#gid] = [String(Number(user[1])), String(Number(user[2]))];
|
|
210
248
|
this.#timeouts = merged;
|
|
249
|
+
if (image !== undefined && (typeof image !== "string" || image === ""))
|
|
250
|
+
throw new Error(`image names the image this provider's computers start from, not ${JSON.stringify(image)}`);
|
|
251
|
+
this.#finding = { ...merged, onNote, image };
|
|
211
252
|
this.#probeTargets = [...probeTargets];
|
|
253
|
+
this.#dial = host.facts.connect === "dial" ? options.dial : undefined;
|
|
212
254
|
// Extend only where the end can follow it: the host moves its timer or its deadline label.
|
|
213
255
|
if (host.extend)
|
|
214
256
|
this.extend = (ref, ttlMs, context) => this.#extendSerial(ref, ttlMs, context?.signal);
|
|
@@ -231,7 +273,7 @@ export class HostedProvider {
|
|
|
231
273
|
groups: [...this.#configuration.groups],
|
|
232
274
|
maxLifetimeMs: this.#maxLifetimeMs,
|
|
233
275
|
release: ["delete"],
|
|
234
|
-
mount:
|
|
276
|
+
mount: descriptor.mounts.filter((kind) => !(this.#dialing && kind === "archil")),
|
|
235
277
|
vendorAgentPorts: [...facts.vendorAgentPorts],
|
|
236
278
|
...(gpu === undefined ? {} : { gpu: structuredClone(gpu) }),
|
|
237
279
|
...(facts.user === undefined ? {} : { user: [...facts.user] }),
|
|
@@ -246,7 +288,9 @@ export class HostedProvider {
|
|
|
246
288
|
const label = this.#label;
|
|
247
289
|
// An Archil mount's own rules hold only where the provider takes one; elsewhere checkMount's refusal says why alone.
|
|
248
290
|
const archil = mount?.kind === "archil" && (capabilities.mount ?? []).includes("archil");
|
|
249
|
-
|
|
291
|
+
// A machine that dials takes no Archil mount yet: its helper's steps run as root through exec, which it has none of.
|
|
292
|
+
const unmounted = this.#dialing ? { ...this.host.descriptor.unmounted, archil: `${label}'s machines dial this server, and a machine that dials takes no Archil mount yet` } : this.host.descriptor.unmounted;
|
|
293
|
+
refusals.push(...checkMount(mount, capabilities.mount ?? [], unmounted), ...(archil ? archilRefusals(given) : []));
|
|
250
294
|
const daemonUser = `${this.#uid}:${this.#gid}`;
|
|
251
295
|
const asked = this.#takesUser ? given.user : undefined;
|
|
252
296
|
if (asked !== undefined) {
|
|
@@ -268,6 +312,10 @@ export class HostedProvider {
|
|
|
268
312
|
}
|
|
269
313
|
if (typeof image !== "string" || image === "" || image.includes("\0"))
|
|
270
314
|
refuse("image", `an image is required: the name of ${a(label)} image`);
|
|
315
|
+
else if (this.#keeps && !ourImage({ image }, this.#finding.image)) {
|
|
316
|
+
// list reads labels only in a machine of our image, so a computer of another would never be found again.
|
|
317
|
+
refuse("image", `${a(label)} computer keeps its labels inside the machine, and list reads them only in one of this provider's image option or a box image, so ${image} is refused: ${this.#finding.image === undefined ? "ask for a box image" : `ask for ${this.#finding.image} or a box image`}`);
|
|
318
|
+
}
|
|
271
319
|
refusals.push(...checkTenure(given, capabilities, label));
|
|
272
320
|
// Only the forms the descriptor declares, each one this computer enforces; any other is refused, never widened.
|
|
273
321
|
const networkProblem = networkRefusal(label, capabilities.network, network);
|
|
@@ -299,6 +347,97 @@ export class HostedProvider {
|
|
|
299
347
|
refusals.push(...this.host.check(given));
|
|
300
348
|
return refusals;
|
|
301
349
|
}
|
|
350
|
+
/** Whether the host's machines dial this server (facts.connect "dial"): every connection goes over that socket. */
|
|
351
|
+
get #dialing() {
|
|
352
|
+
return this.host.facts.connect === "dial";
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Where a dial host's machines reach this server: its attach route as ws(s) and its daemon route, from dial.url, an
|
|
356
|
+
* https address (http only on 127.0.0.1, for tests); or the refusal of a create, in words, where it has none or where
|
|
357
|
+
* the provider was given no dial (no hub, so no process to take the machine's socket).
|
|
358
|
+
*/
|
|
359
|
+
#dialed() {
|
|
360
|
+
if (this.#dial === undefined) {
|
|
361
|
+
return { problem: `${this.#label} machines dial in to this server, and this process has no attach hub: start them from serve's own process` };
|
|
362
|
+
}
|
|
363
|
+
const url = this.#dial.url;
|
|
364
|
+
const at = URL.canParse(url ?? "") ? new URL(url) : undefined;
|
|
365
|
+
if (at === undefined || !(at.protocol === "https:" || (at.protocol === "http:" && at.hostname === "127.0.0.1"))) {
|
|
366
|
+
return {
|
|
367
|
+
problem: `${this.#label} machines dial this server, and it has no public https address: set AGENTRUN_PUBLIC_URL to one the internet reaches (a tunnel such as cloudflared or tailscale funnel works), or use a provider whose host has exec`,
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
// From the parsed address, whose scheme and host are lowercase: "HTTPS://Example.com/" is https://example.com.
|
|
371
|
+
const path = at.pathname.replace(/\/+$/, "");
|
|
372
|
+
return {
|
|
373
|
+
attach: `${at.protocol === "https:" ? "wss:" : "ws:"}//${at.host}${path}${ATTACH_PATH}`,
|
|
374
|
+
daemon: `${at.origin}${path}${DAEMON_PATH}?arch=${this.host.facts.arch}`,
|
|
375
|
+
dial: this.#dial,
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* A dial machine's token revoked, then its socket closed, best effort: the token first, so a redial while the revoke
|
|
380
|
+
* is written can't stay attached after the close. A token not revoked is refused past backstopAt.
|
|
381
|
+
*/
|
|
382
|
+
async #revoke(name) {
|
|
383
|
+
const dial = this.#dial;
|
|
384
|
+
if (dial === undefined)
|
|
385
|
+
return;
|
|
386
|
+
const { requestMs } = this.#timeouts;
|
|
387
|
+
const said = (why) => this.#onNote?.({ computer: name, note: `its machine token could not be revoked (${why}): it is refused past the machine's backstop either way` });
|
|
388
|
+
let timer;
|
|
389
|
+
try {
|
|
390
|
+
// Bounded by requestMs: a store that never answers must not hold the socket open or the vendor's delete back.
|
|
391
|
+
const late = await Promise.race([dial.tokens.revoke(name).then(() => false), new Promise((done) => (timer = setTimeout(done, requestMs, true)))]);
|
|
392
|
+
if (late)
|
|
393
|
+
said(`the token store did not answer within ${requestMs / 1000} s`);
|
|
394
|
+
}
|
|
395
|
+
catch (error) {
|
|
396
|
+
said(words(error));
|
|
397
|
+
}
|
|
398
|
+
finally {
|
|
399
|
+
clearTimeout(timer);
|
|
400
|
+
dial.hub.revoke(name, `${name} was deleted`);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
/** Whether the host keeps no labels (facts.labels "machine"): the adapter keeps them in the machine, at LABEL_FILE. */
|
|
404
|
+
get #keeps() {
|
|
405
|
+
return this.host.facts.labels === "machine";
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* The longest a create takes from the vendor's create to the label file: its own call and the wait for the machine to
|
|
409
|
+
* run (createMs each), the listing of the name between them (listMs, and a read), and the file's write (execMs), plus
|
|
410
|
+
* UNFILED_MARGIN_MS.
|
|
411
|
+
*/
|
|
412
|
+
get #unfiledMs() {
|
|
413
|
+
const { createMs, listMs, requestMs, execMs } = this.#timeouts;
|
|
414
|
+
return 2 * createMs + listMs + requestMs + execMs + UNFILED_MARGIN_MS;
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* With labels kept in the machine, a machine with no label file is the request's whose name it has (Machine.name, the
|
|
418
|
+
* request's own name, never a bare computer- prefix), and a sweep of the name takes it only once it is older than
|
|
419
|
+
* #unfiledMs: before that, another instance's create of the request may still be writing its file (provider.md,
|
|
420
|
+
* Idempotent create). A machine with no createdAt is never old enough: the vendor's timer ends it.
|
|
421
|
+
*/
|
|
422
|
+
#unfiled(machine, name) {
|
|
423
|
+
if (!this.#keeps || machine.labels[COMPUTER_LABEL] !== undefined || machine.name !== name)
|
|
424
|
+
return undefined;
|
|
425
|
+
const age = machine.createdAt instanceof Date ? Date.now() - machine.createdAt.getTime() : Number.NaN;
|
|
426
|
+
return age > this.#unfiledMs ? "old" : "young";
|
|
427
|
+
}
|
|
428
|
+
/** Writes labels to the machine's label file, root's alone, and answers them as the file reads back. */
|
|
429
|
+
async #keep(ref, labels, signal) {
|
|
430
|
+
const what = `writing ${ref.name}'s labels to ${LABEL_FILE}`;
|
|
431
|
+
const json = Buffer.from(JSON.stringify(labels));
|
|
432
|
+
const wrote = await runToExit(this.host, ref.id, labelsCommand(json.length), { stdin: [json], ms: this.#timeouts.execMs, ...(signal ? { signal } : {}), what });
|
|
433
|
+
if (!wrote.ok)
|
|
434
|
+
return wrote;
|
|
435
|
+
const read = wrote.value.exitCode === 0 ? labelMapOf(wrote.value.stdout) : undefined;
|
|
436
|
+
if (read === undefined)
|
|
437
|
+
return this.#fail("refused", `${what} failed (exit ${wrote.value.exitCode}): ${`${wrote.value.stdout} ${wrote.value.stderr}`.trim().slice(0, 300)}`);
|
|
438
|
+
filedOf(this.host).set(ref.id, read);
|
|
439
|
+
return ok(read);
|
|
440
|
+
}
|
|
302
441
|
/** Whether the host takes a spec's user (facts.user): then a computer's daemon runs as the one a spec names. */
|
|
303
442
|
get #takesUser() {
|
|
304
443
|
return this.host.facts.user !== undefined;
|
|
@@ -313,15 +452,21 @@ export class HostedProvider {
|
|
|
313
452
|
* they fit.
|
|
314
453
|
*/
|
|
315
454
|
#labelsOver(spec) {
|
|
316
|
-
const
|
|
455
|
+
const room = this.host.facts.labels ?? {};
|
|
317
456
|
// Stand-ins as long as create's name and deadlines and as mixed, fixed so a spec's answer never changes (attach and a
|
|
318
457
|
// retry check the spec again): a packed map deflates them as it does create's, give or take a byte, and create packs
|
|
319
|
-
// its own before the host's call (#
|
|
458
|
+
// its own before the host's call (#stored).
|
|
320
459
|
const name = "computer-5f3a9c1e7b2d";
|
|
321
460
|
const labels = this.#labelsOf(spec, name, "2026-11-16T04:15:29.481Z", "2026-11-17T04:05:29.481Z");
|
|
461
|
+
if (room === "machine") {
|
|
462
|
+
// The label file is read back whole only up to what a run keeps of its output.
|
|
463
|
+
const json = Buffer.byteLength(JSON.stringify(labels));
|
|
464
|
+
return json <= OUTPUT_MAX ? undefined : { bytes: true, why: `${a(this.#label)} computer keeps its labels in the machine, at most ${OUTPUT_MAX} bytes as JSON, and this computer's would come to ${json}: ${shorterWords(labels)}` };
|
|
465
|
+
}
|
|
466
|
+
const { count, bytes: max, packed } = room;
|
|
322
467
|
if (packed) {
|
|
323
|
-
const
|
|
324
|
-
return
|
|
468
|
+
const fit = this.#stored(labels, name);
|
|
469
|
+
return "problem" in fit ? { bytes: true, why: fit.problem } : undefined;
|
|
325
470
|
}
|
|
326
471
|
const keys = Object.keys(labels);
|
|
327
472
|
if (count !== undefined && keys.length > count) {
|
|
@@ -337,10 +482,13 @@ export class HostedProvider {
|
|
|
337
482
|
return undefined;
|
|
338
483
|
return { bytes: true, why: `${a(this.#label)} computer's labels come to at most ${max} bytes, names and values together, and this computer's would come to ${bytes} (${sizes.map(([key, size]) => `${key} ${size}`).join(", ")}): ${shorterWords(labels)}` };
|
|
339
484
|
}
|
|
340
|
-
/**
|
|
341
|
-
#
|
|
342
|
-
const
|
|
343
|
-
|
|
485
|
+
/** The labels as the host is given them: packed where it declares facts.labels.packed, else as they are; or why they don't fit packed, in words. */
|
|
486
|
+
#stored(labels, name) {
|
|
487
|
+
const room = packedRoom(this.host);
|
|
488
|
+
if (room === undefined)
|
|
489
|
+
return { labels };
|
|
490
|
+
const fit = packLabels(labels, name, room);
|
|
491
|
+
return "problem" in fit ? { problem: `${a(this.#label)} computer keeps its labels packed, and ${fit.problem}: ${shorterWords(labels)}` } : fit;
|
|
344
492
|
}
|
|
345
493
|
/** A host call bounded by ms, the caller's signal when given. */
|
|
346
494
|
#call(what, run, ms, signal) {
|
|
@@ -354,6 +502,10 @@ export class HostedProvider {
|
|
|
354
502
|
* for the first and answers what it does.
|
|
355
503
|
*/
|
|
356
504
|
async create(spec, context) {
|
|
505
|
+
// Before any call to the vendor, so a machine that could never reach this server is never made or billed.
|
|
506
|
+
const dialed = this.#dialing ? this.#dialed() : undefined;
|
|
507
|
+
if (dialed !== undefined && "problem" in dialed)
|
|
508
|
+
return this.#fail("refused", dialed.problem);
|
|
357
509
|
const refusals = this.check(spec);
|
|
358
510
|
return this.#requests.create(spec, context, {
|
|
359
511
|
refuse: (code, message) => this.#fail(code, message),
|
|
@@ -361,9 +513,37 @@ export class HostedProvider {
|
|
|
361
513
|
// The checks #again makes of a computer listed already, made here of the spec in flight: what it gives is shared
|
|
362
514
|
// only with a create that asks the same.
|
|
363
515
|
userOf: (asked) => this.#userOf(asked),
|
|
364
|
-
make: (name) => this.#createOnce(spec, name, context.signal),
|
|
516
|
+
make: (name) => (dialed === undefined ? this.#createOnce(spec, name, context.signal) : this.#inTurn(dialed.dial.hub, name, context.signal, () => this.#createOnce(spec, name, context.signal))),
|
|
365
517
|
});
|
|
366
518
|
}
|
|
519
|
+
/**
|
|
520
|
+
* A dial create of one request after any other of it on this server, whichever provider instance makes it. A
|
|
521
|
+
* machine's token and socket go by the request's name, so two machines of one request must never dial at once: the
|
|
522
|
+
* second create finds the first's computer instead (#createOnce). One server process is what dial asks for. A caller
|
|
523
|
+
* that aborts while it waits stops waiting; the turn after it still waits for the create before it.
|
|
524
|
+
*/
|
|
525
|
+
async #inTurn(hub, name, signal, make) {
|
|
526
|
+
const turns = DIAL_TURNS.get(hub) ?? new Map();
|
|
527
|
+
DIAL_TURNS.set(hub, turns);
|
|
528
|
+
const before = turns.get(name) ?? Promise.resolve();
|
|
529
|
+
let done = () => { };
|
|
530
|
+
const mine = before.then(() => new Promise((resolve) => (done = resolve)));
|
|
531
|
+
turns.set(name, mine);
|
|
532
|
+
try {
|
|
533
|
+
if ((await untilAborted(before.then(() => true), [signal])) === undefined) {
|
|
534
|
+
return this.#fail("aborted", "create was aborted while another create of this request was in flight");
|
|
535
|
+
}
|
|
536
|
+
return await make();
|
|
537
|
+
}
|
|
538
|
+
finally {
|
|
539
|
+
// Once before has settled: a turn that stopped waiting holds the next one no longer than the create before it.
|
|
540
|
+
void before.then(() => done());
|
|
541
|
+
void mine.then(() => {
|
|
542
|
+
if (turns.get(name) === mine)
|
|
543
|
+
turns.delete(name);
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
}
|
|
367
547
|
/** A request's computer given up on or deleted: its request is refused from now on. */
|
|
368
548
|
#end(name) {
|
|
369
549
|
this.#requests.end(name);
|
|
@@ -382,10 +562,15 @@ export class HostedProvider {
|
|
|
382
562
|
}
|
|
383
563
|
/** taken: the vendor refused this create's name as one a machine has (exists), so the name's machine is read once more. */
|
|
384
564
|
async #createOnce(spec, name, signal, taken) {
|
|
385
|
-
const listed = await foundBy(this.host, "the listing by the request's name", byName(name), this.#
|
|
565
|
+
const listed = await foundBy(this.host, "the listing by the request's name", byName(name), this.#finding, signal);
|
|
386
566
|
if (!listed.ok)
|
|
387
567
|
return listed;
|
|
388
|
-
|
|
568
|
+
// With labels kept in the machine, a young one of the name with none written yet is this request's still being made
|
|
569
|
+
// (#again waits for its labels); an old one never got them, and is deleted.
|
|
570
|
+
const found = listed.value.filter((machine) => ours(machine.labels, name) || this.#unfiled(machine, name) === "young");
|
|
571
|
+
if (found.length === 0 && listed.value.some((machine) => this.#unfiled(machine, name) === "old")) {
|
|
572
|
+
return this.#fail("exists", `${name} was made for this request and its labels were never written: create with a new request id${await this.#deleteNamed(name)}`);
|
|
573
|
+
}
|
|
389
574
|
if (found.length > 1)
|
|
390
575
|
return this.#fail("exists", `${found.length} machines carry the name ${name} (${found.map((m) => m.id).join(", ")}): create with a new request id`);
|
|
391
576
|
if (found.length === 1)
|
|
@@ -431,7 +616,7 @@ export class HostedProvider {
|
|
|
431
616
|
* same listing. Its value is the machine kept, or undefined when mine holds the name alone (or nothing is listed yet).
|
|
432
617
|
*/
|
|
433
618
|
async #converge(name, mine, signal) {
|
|
434
|
-
const listed = await foundBy(this.host, "the listing by the request's name", byName(name), this.#
|
|
619
|
+
const listed = await foundBy(this.host, "the listing by the request's name", byName(name), this.#finding, signal);
|
|
435
620
|
if (!listed.ok)
|
|
436
621
|
return listed;
|
|
437
622
|
const holders = listed.value.filter((machine) => ours(machine.labels, name) && alive(machine.state));
|
|
@@ -469,7 +654,11 @@ export class HostedProvider {
|
|
|
469
654
|
const deadline = deadlineOf(machine);
|
|
470
655
|
if (deadline !== undefined && deadline.getTime() <= Date.now())
|
|
471
656
|
return this.#fail("exists", endedWords(name));
|
|
472
|
-
|
|
657
|
+
// With labels kept in the machine: the create making it has not written them yet.
|
|
658
|
+
const unwritten = machine.state === "running" && this.#unfiled(machine, name) !== undefined;
|
|
659
|
+
if (unwritten)
|
|
660
|
+
last = { code: "refused", message: "has no labels written" };
|
|
661
|
+
if (machine.state === "running" && !unwritten) {
|
|
473
662
|
const differ = this.#labelDiffer(spec, name, machine.labels, "read back");
|
|
474
663
|
if (differ.length > 0)
|
|
475
664
|
return this.#fail("exists", otherSpecWords(name, "was made", differ));
|
|
@@ -539,9 +728,9 @@ export class HostedProvider {
|
|
|
539
728
|
const maxDeadline = new Date(now + this.#maxLifetimeMs).toISOString();
|
|
540
729
|
const labels = this.#labelsOf(spec, name, deadline, maxDeadline);
|
|
541
730
|
// check() packed stand-ins; these deflate a byte apart from them, so the host is never given labels that don't fit.
|
|
542
|
-
const
|
|
543
|
-
if (
|
|
544
|
-
return this.#fail("refused", `labels: ${
|
|
731
|
+
const stored = this.#stored(labels, name);
|
|
732
|
+
if ("problem" in stored)
|
|
733
|
+
return this.#fail("refused", `labels: ${stored.problem}`);
|
|
545
734
|
const archil = spec.mount?.kind === "archil";
|
|
546
735
|
const user = this.#userOf(spec);
|
|
547
736
|
const commands = hostedCommands({
|
|
@@ -559,16 +748,22 @@ export class HostedProvider {
|
|
|
559
748
|
rootOnly: this.host.facts.rootOnly ?? [],
|
|
560
749
|
network: formOf(spec.network),
|
|
561
750
|
});
|
|
751
|
+
const backstopAt = new Date(now + this.#maxLifetimeMs + BACKSTOP_MARGIN_MS).toISOString();
|
|
752
|
+
const boot = this.#dialing ? await this.#boot(name, backstopAt, { deadline, user, maxDeadline, network: formOf(spec.network) }, sha256) : undefined;
|
|
753
|
+
if (boot !== undefined && !boot.ok)
|
|
754
|
+
return boot;
|
|
562
755
|
const request = {
|
|
563
756
|
name,
|
|
564
757
|
image: spec.image,
|
|
565
|
-
labels,
|
|
758
|
+
labels: stored.labels,
|
|
566
759
|
lifetimeMs,
|
|
760
|
+
backstopAt,
|
|
567
761
|
// The machine has egress where its tools' network is open or a list, which init's proxy holds to the public
|
|
568
762
|
// internet (./network.ts), and where an Archil computer's root reaches Archil. Its tools' none is init's own
|
|
569
|
-
// either way, read back by the probe from inside, as the tool user.
|
|
570
|
-
egress: archil || throughProxy(spec.network),
|
|
571
|
-
...(this.host.facts.daemon === "pid1" ? { command: commands.entrypoint } : {}),
|
|
763
|
+
// either way, read back by the probe from inside, as the tool user. A dial machine's root reaches this server.
|
|
764
|
+
egress: archil || throughProxy(spec.network) || boot !== undefined,
|
|
765
|
+
...(this.host.facts.daemon === "pid1" && boot === undefined ? { command: commands.entrypoint } : {}),
|
|
766
|
+
...(boot === undefined ? {} : { boot: { env: boot.value } }),
|
|
572
767
|
...(spec.gpu === undefined ? {} : { gpu: spec.gpu }),
|
|
573
768
|
...(spec.cpus === undefined ? {} : { cpus: spec.cpus }),
|
|
574
769
|
...(spec.memoryMb === undefined ? {} : { memoryMb: spec.memoryMb }),
|
|
@@ -579,6 +774,8 @@ export class HostedProvider {
|
|
|
579
774
|
// after this gave up is deleted by its id; one that fails after it, by its name where it may have made one.
|
|
580
775
|
const created = await hostCall(this.host, `${this.#label}'s create`, (ctx) => this.host.create(request, { timeoutMs: ctx.timeoutMs }), createMs, signal, (made) => made.ok ? this.#kill(made.value.id) : mayHaveMade(made.error.code) ? this.#deleteNamed(name) : undefined);
|
|
581
776
|
if (!created.ok) {
|
|
777
|
+
// A machine of this create's that lands after this never dials: its token goes with the create.
|
|
778
|
+
await this.#revoke(name);
|
|
582
779
|
const { code, message } = created.error;
|
|
583
780
|
if (code === "aborted" || code === "timeout") {
|
|
584
781
|
this.#end(name);
|
|
@@ -593,6 +790,29 @@ export class HostedProvider {
|
|
|
593
790
|
this.#requests.made(name);
|
|
594
791
|
return ok({ ref: { id: created.value.id, name }, made: { deadline, maxDeadline, labels, daemon, sha256, commands, user } });
|
|
595
792
|
}
|
|
793
|
+
/**
|
|
794
|
+
* A dial machine's environment (MachineRequest.boot.env, spec/computer/attach.md): its own token, minted here to stop
|
|
795
|
+
* at backstopAt, where it dials and fetches the daemon, the daemon's sha256, and init's flags as start gives them.
|
|
796
|
+
*/
|
|
797
|
+
async #boot(name, backstopAt, init, sha256) {
|
|
798
|
+
const dialed = this.#dialed();
|
|
799
|
+
if ("problem" in dialed)
|
|
800
|
+
return this.#fail("refused", dialed.problem);
|
|
801
|
+
let token;
|
|
802
|
+
try {
|
|
803
|
+
token = await dialed.dial.tokens.mint(name, { kind: this.#kind, os: "linux", arch: this.host.facts.arch, expiresAt: new Date(backstopAt) });
|
|
804
|
+
}
|
|
805
|
+
catch (error) {
|
|
806
|
+
return this.#fail("refused", `minting ${name}'s machine token failed, so ${this.#label} was not called: ${words(error)}`);
|
|
807
|
+
}
|
|
808
|
+
return ok({
|
|
809
|
+
AGENTRUN_ATTACH_URL: dialed.attach,
|
|
810
|
+
AGENTRUN_ATTACH_TOKEN: token,
|
|
811
|
+
AGENTRUN_DAEMON_URL: dialed.daemon,
|
|
812
|
+
AGENTRUN_DAEMON_SHA256: sha256,
|
|
813
|
+
AGENTRUN_INIT_ARGS: initFlags(init).join(" "),
|
|
814
|
+
});
|
|
815
|
+
}
|
|
596
816
|
/** Deletes a machine create gives up on, then waits, under the caller's signal, until get answers none for it. */
|
|
597
817
|
async #abandon(ref, error, signal, mounted = false) {
|
|
598
818
|
this.#end(ref.name);
|
|
@@ -611,48 +831,62 @@ export class HostedProvider {
|
|
|
611
831
|
* Every machine carrying a name create gave, deleted and read back gone: for a create that may have made one whose
|
|
612
832
|
* id never came (mayHaveMade), never after a refusal, where a machine under the name is another's. The
|
|
613
833
|
* listing shows a new machine a moment after the vendor makes it, so it is read for the name until one shows or
|
|
614
|
-
* listedMs passes.
|
|
834
|
+
* listedMs passes, as create's own wait reads it. With labels kept in the machine, one with no label file yet is taken
|
|
835
|
+
* only once it is old (#unfiled). Best effort, in words.
|
|
615
836
|
*/
|
|
616
837
|
async #deleteNamed(name) {
|
|
617
838
|
const { listedMs, pollMs } = this.#timeouts;
|
|
618
839
|
const until = Date.now() + listedMs;
|
|
619
|
-
let
|
|
620
|
-
for (;;) {
|
|
621
|
-
const listed = await foundBy(this.host, "the listing by name", byName(name), this.#
|
|
840
|
+
let named;
|
|
841
|
+
for (let delay = Math.min(LISTED_POLL_MS, pollMs);; delay = Math.min(delay * 2, pollMs)) {
|
|
842
|
+
const listed = await foundBy(this.host, "the listing by name", byName(name), this.#finding);
|
|
622
843
|
if (!listed.ok)
|
|
623
844
|
return `; a machine named ${name} may be left (${listed.error.message}), which ends at its deadline`;
|
|
624
|
-
|
|
625
|
-
if (
|
|
845
|
+
named = listed.value.filter((machine) => ours(machine.labels, name) || this.#unfiled(machine, name) !== undefined);
|
|
846
|
+
if (named.length > 0 || Date.now() >= until)
|
|
626
847
|
break;
|
|
627
|
-
await pause(Math.min(
|
|
848
|
+
await pause(Math.min(delay, until - Date.now()));
|
|
628
849
|
}
|
|
629
|
-
|
|
850
|
+
const found = named.filter((machine) => this.#unfiled(machine, name) !== "young");
|
|
851
|
+
const young = named.length - found.length;
|
|
852
|
+
const left = young === 0 ? "" : `; ${young} machine${young === 1 ? "" : "s"} named ${name} with no labels written yet, younger than ${seconds(this.#unfiledMs)}, left: another create of this request may still be making it`;
|
|
853
|
+
if (named.length === 0)
|
|
630
854
|
return `; no machine named ${name} was listed within ${seconds(listedMs)}, and any it made ends at its deadline`;
|
|
855
|
+
if (found.length === 0)
|
|
856
|
+
return left;
|
|
631
857
|
const said = [];
|
|
632
858
|
for (const machine of found) {
|
|
633
859
|
const { gone } = await this.#remove({ id: machine.id, name }, undefined);
|
|
634
860
|
said.push(gone.ok ? `${machine.id} was deleted and read back gone` : `${machine.id} may be left (${gone.error.message}), which ends at its deadline`);
|
|
635
861
|
}
|
|
636
|
-
return `; the machine named ${name} it made: ${said.join("; ")}`;
|
|
862
|
+
return `; the machine named ${name} it made: ${said.join("; ")}${left}`;
|
|
637
863
|
}
|
|
638
864
|
/** The host's delete, never signalled: a delete that cleans up after an abort must outlive it. Not found is done. */
|
|
639
865
|
async #kill(id) {
|
|
640
866
|
const killed = await this.#call(`the delete of ${id}`, (ctx) => this.host.delete(id, ctx), this.#timeouts.requestMs);
|
|
641
|
-
|
|
867
|
+
if (!killed.ok && killed.error.code !== "not_found")
|
|
868
|
+
return killed;
|
|
869
|
+
filedOf(this.host).delete(id);
|
|
870
|
+
return ok(undefined);
|
|
642
871
|
}
|
|
643
872
|
/** The pause between two reads of a machine that is starting: the next read comes after it, within createMs. */
|
|
644
873
|
#startPollMs() {
|
|
645
874
|
return Math.min(START_POLL_MS, this.#timeouts.pollMs * 10);
|
|
646
875
|
}
|
|
647
|
-
/**
|
|
648
|
-
|
|
649
|
-
|
|
876
|
+
/**
|
|
877
|
+
* The host's get, bounded by requestMs; a machine the vendor reports as not found is none. Labels kept in the machine
|
|
878
|
+
* are read from its file once and then as last read.
|
|
879
|
+
*/
|
|
880
|
+
async #get(id, signal) {
|
|
881
|
+
const read = await readMachine(this.host, id, this.#timeouts.requestMs, signal);
|
|
882
|
+
return read.ok ? labelled(this.host, read.value, this.#timeouts.requestMs, signal, false) : read;
|
|
650
883
|
}
|
|
651
884
|
/** The machine running, read while it starts (within createMs); gone or failed is refused in words. */
|
|
652
885
|
async #running(ref, signal) {
|
|
653
886
|
const until = Date.now() + this.#timeouts.createMs;
|
|
654
887
|
for (;;) {
|
|
655
|
-
|
|
888
|
+
// Its labels are read after this, as written (#prepare).
|
|
889
|
+
const read = await readMachine(this.host, ref.id, this.#timeouts.requestMs, signal);
|
|
656
890
|
if (!read.ok)
|
|
657
891
|
return read;
|
|
658
892
|
const machine = read.value;
|
|
@@ -692,9 +926,19 @@ export class HostedProvider {
|
|
|
692
926
|
const running = await this.#running(ref, signal);
|
|
693
927
|
if (!running.ok)
|
|
694
928
|
return running;
|
|
695
|
-
|
|
929
|
+
// A host that keeps no labels: they go in the machine first, and are read back from there.
|
|
930
|
+
const kept = this.#keeps ? await this.#keep(ref, labels, signal) : ok(running.value.labels);
|
|
931
|
+
if (!kept.ok)
|
|
932
|
+
return kept;
|
|
933
|
+
const drift = this.#machineDrift({ ...running.value, labels: kept.value }, labels);
|
|
696
934
|
if (drift.length > 0)
|
|
697
935
|
return this.#fail("refused", `it did not match its spec: ${drift.join("; ")}`);
|
|
936
|
+
const gpu = spec.gpu !== undefined;
|
|
937
|
+
const want = { user, isolation: (gpu ? this.#configuration.gpu?.isolation : undefined) ?? this.host.facts.isolation, gpu };
|
|
938
|
+
// A dial machine's agentrun-dial fetched the daemon, held it to AGENTRUN_DAEMON_SHA256 (this create's daemon's) and
|
|
939
|
+
// started init itself: the drift check holds by that sha, so nothing is installed or read back here.
|
|
940
|
+
if (this.#dialing)
|
|
941
|
+
return this.#start(spec, ref, commands, want, signal);
|
|
698
942
|
// Before anything of this create's is in the machine: what root runs is root's alone (the Archil helper's sha256,
|
|
699
943
|
// read at the mount's preflight, is read after this).
|
|
700
944
|
if ((this.host.facts.rootOnly ?? []).length > 0) {
|
|
@@ -726,9 +970,7 @@ export class HostedProvider {
|
|
|
726
970
|
// The run's folder, after the daemon is in place (serve runs it) and before init starts: no tool program runs before
|
|
727
971
|
// it is mounted. A folder mounted for a computer create then gives up on is let go before the delete, never signalled.
|
|
728
972
|
const started = async () => {
|
|
729
|
-
const
|
|
730
|
-
const isolation = (gpu ? this.#configuration.gpu?.isolation : undefined) ?? this.host.facts.isolation;
|
|
731
|
-
const done = await this.#start(spec, ref, commands, { user, isolation, gpu }, signal);
|
|
973
|
+
const done = await this.#start(spec, ref, commands, want, signal);
|
|
732
974
|
return done.ok && installer !== undefined ? ok([installer, ...done.value]) : done;
|
|
733
975
|
};
|
|
734
976
|
if (spec.mount?.kind !== "archil")
|
|
@@ -753,7 +995,7 @@ export class HostedProvider {
|
|
|
753
995
|
/** The rest of #prepare from the daemon's install on: init started, its first connection checked, the listing waited for. */
|
|
754
996
|
async #start(spec, ref, commands, want, signal) {
|
|
755
997
|
const closed = [];
|
|
756
|
-
if (this.host.facts.daemon === "child") {
|
|
998
|
+
if (this.host.facts.daemon === "child" && !this.#dialing) {
|
|
757
999
|
// The session buses in /tmp, closed to the computer's user before any program of its runs.
|
|
758
1000
|
const shut = await runToExit(this.host, ref.id, commands.sockets, { ms: this.#timeouts.execMs, ...(signal ? { signal } : {}), what: `closing the session buses in /tmp in ${ref.name}` });
|
|
759
1001
|
if (!shut.ok)
|
|
@@ -769,9 +1011,9 @@ export class HostedProvider {
|
|
|
769
1011
|
if (init.value.exitCode !== 0)
|
|
770
1012
|
return this.#fail("refused", `starting the daemon failed (exit ${init.value.exitCode}): ${`${init.value.stdout} ${init.value.stderr}`.trim().slice(0, 500)}`);
|
|
771
1013
|
}
|
|
772
|
-
const opened = await this.#openWhenListening(ref, signal);
|
|
1014
|
+
const opened = this.#dialing ? await this.#whenDialed(ref, signal) : await this.#openWhenListening(ref, signal);
|
|
773
1015
|
if (!opened.ok) {
|
|
774
|
-
if (opened.error.code === "aborted")
|
|
1016
|
+
if (opened.error.code === "aborted" || this.#dialing)
|
|
775
1017
|
return opened;
|
|
776
1018
|
const why = await this.#initWords(ref);
|
|
777
1019
|
return this.#fail(opened.error.code, `the daemon did not answer: ${opened.error.message}${why ? `; init wrote: ${why}` : ""}`);
|
|
@@ -871,6 +1113,14 @@ export class HostedProvider {
|
|
|
871
1113
|
/** One stdio bridge into the machine, after hello, unchecked; its client says so once the machine is deleted. */
|
|
872
1114
|
async #open(ref, signal, helloMs = this.#timeouts.helloMs) {
|
|
873
1115
|
const { requestMs } = this.#timeouts;
|
|
1116
|
+
if (this.#dialing) {
|
|
1117
|
+
// A dial machine's connection is over the socket it dialed, which the hub holds under its name: a drop fails the
|
|
1118
|
+
// calls in flight gone, and the first call after a redial is refused gone (attach.ts, AttachHub.connection).
|
|
1119
|
+
const connection = this.#dial?.hub.connection(ref.name);
|
|
1120
|
+
if (connection === undefined)
|
|
1121
|
+
return this.#fail("gone", `${ref.name} is not attached to this server now: its computerd dials back on its own, so try again shortly`);
|
|
1122
|
+
return ok(deletedSaid(this.host, ref, this.#label, requestMs, connection));
|
|
1123
|
+
}
|
|
874
1124
|
const opened = await openBridge(this.host, ref.id, { argv: hostedCommands().stdio, name: ref.name, version: VERSION, requestMs, helloMs, ...(signal ? { signal } : {}) });
|
|
875
1125
|
return opened.ok ? ok(deletedSaid(this.host, ref, this.#label, requestMs, opened.value)) : opened;
|
|
876
1126
|
}
|
|
@@ -884,6 +1134,28 @@ export class HostedProvider {
|
|
|
884
1134
|
await pause(LISTEN_RETRY_MS, signal);
|
|
885
1135
|
}
|
|
886
1136
|
}
|
|
1137
|
+
/**
|
|
1138
|
+
* A dial machine's first connection: its socket waited for in the hub, from its start, within createMs. One that never
|
|
1139
|
+
* dials (a server it cannot reach, a daemon whose sha256 it refused, a hello the hub refused) fails timeout, naming the
|
|
1140
|
+
* address it was given; create then deletes it.
|
|
1141
|
+
*/
|
|
1142
|
+
async #whenDialed(ref, signal) {
|
|
1143
|
+
const { createMs } = this.#timeouts;
|
|
1144
|
+
const until = Date.now() + createMs;
|
|
1145
|
+
for (;;) {
|
|
1146
|
+
const opened = await this.#open(ref, signal);
|
|
1147
|
+
if (opened.ok)
|
|
1148
|
+
return opened;
|
|
1149
|
+
if (Date.now() + this.#startPollMs() >= until) {
|
|
1150
|
+
const dialed = this.#dialed();
|
|
1151
|
+
const to = "problem" in dialed ? "this server" : dialed.attach;
|
|
1152
|
+
return this.#fail("timeout", `${ref.name} did not dial ${to} within ${seconds(createMs)}: the machine must reach that address, and run agentrun-dial (the box image's entrypoint while AGENTRUN_ATTACH_URL is set)`);
|
|
1153
|
+
}
|
|
1154
|
+
await pause(this.#startPollMs(), signal);
|
|
1155
|
+
if (signal?.aborted)
|
|
1156
|
+
return this.#fail("aborted", `create was aborted while it waited for ${ref.name} to dial`);
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
887
1159
|
/**
|
|
888
1160
|
* Its tools' network as they meet it, against the one create made: the probe, as the computer's own user. Under none,
|
|
889
1161
|
* no target answers. Under open or a list, still none answers directly (their own loopback alone), init's relay
|
|
@@ -941,12 +1213,13 @@ export class HostedProvider {
|
|
|
941
1213
|
/**
|
|
942
1214
|
* Create waits until the listing shows the new computer on two consecutive polls at least listedStableMs apart (and on
|
|
943
1215
|
* every poll between), so list() shows it once create returns: a listing that showed it once and then not starts over.
|
|
1216
|
+
* The polls are 150 ms apart at first, the pause doubling up to pollMs, so a vendor slow to list costs a few calls.
|
|
944
1217
|
*/
|
|
945
1218
|
async #waitListed(ref, signal) {
|
|
946
1219
|
const { listedMs, listMs, pollMs, listedStableMs } = this.#timeouts;
|
|
947
1220
|
const until = Date.now() + listedMs;
|
|
948
1221
|
let since;
|
|
949
|
-
for (let hits = 0;;) {
|
|
1222
|
+
for (let hits = 0, delay = Math.min(LISTED_POLL_MS, pollMs);; delay = Math.min(delay * 2, pollMs)) {
|
|
950
1223
|
const listed = await listedBy(this.host, "the listing by name", byName(ref.name), listMs, signal);
|
|
951
1224
|
if (!listed.ok)
|
|
952
1225
|
return listed;
|
|
@@ -960,7 +1233,7 @@ export class HostedProvider {
|
|
|
960
1233
|
return ok(undefined);
|
|
961
1234
|
if (Date.now() >= until)
|
|
962
1235
|
return this.#fail("timeout", `the computer was not listed within ${seconds(listedMs)} of its creation`);
|
|
963
|
-
await pause(Math.min(
|
|
1236
|
+
await pause(Math.min(delay, until - Date.now()), signal);
|
|
964
1237
|
if (signal?.aborted)
|
|
965
1238
|
return this.#fail("aborted", "create was aborted while it waited for the listing");
|
|
966
1239
|
}
|
|
@@ -1116,7 +1389,10 @@ export class HostedProvider {
|
|
|
1116
1389
|
*/
|
|
1117
1390
|
async testConnection({ signal } = {}) {
|
|
1118
1391
|
const listed = await listedBy(this.host, "the listing", { [COMPUTER_LABEL]: "1" }, this.#timeouts.listMs, signal);
|
|
1119
|
-
|
|
1392
|
+
// A host that keeps no labels lists them by their image, and by their name where the vendor keeps one.
|
|
1393
|
+
const keptOurs = (machine) => ourImage(machine, this.#finding.image) && (machine.name === undefined || NAME.test(machine.name));
|
|
1394
|
+
const counted = (machine) => (this.#keeps ? keptOurs(machine) : ours(machine.labels)) && alive(machine.state);
|
|
1395
|
+
return countTested(this.#label, listed.ok ? ok({ count: listed.value.filter(counted).length }) : listed);
|
|
1120
1396
|
}
|
|
1121
1397
|
/**
|
|
1122
1398
|
* Every computer this provider made that carries all of these labels, running or stopped: the listing finds them, and
|
|
@@ -1128,7 +1404,7 @@ export class HostedProvider {
|
|
|
1128
1404
|
if (problem)
|
|
1129
1405
|
return this.#fail("invalid", problem);
|
|
1130
1406
|
const { listMs } = this.#timeouts;
|
|
1131
|
-
const listed = await foundBy(this.host, "the listing", { ...labels, [COMPUTER_LABEL]: "1" }, this.#
|
|
1407
|
+
const listed = await foundBy(this.host, "the listing", { ...labels, [COMPUTER_LABEL]: "1" }, this.#finding, signal);
|
|
1132
1408
|
if (!listed.ok)
|
|
1133
1409
|
return listed.error.code === "timeout" ? this.#fail("timeout", `list did not finish within ${seconds(listMs)}: ${listed.error.message}`) : listed;
|
|
1134
1410
|
const infos = [];
|
|
@@ -1211,7 +1487,8 @@ export class HostedProvider {
|
|
|
1211
1487
|
/**
|
|
1212
1488
|
* Moves the deadline to now + ttlMs, refusing one earlier than it is (before any vendor call), never past create's time
|
|
1213
1489
|
* plus the cap (init's maximum): the host's extend first (its timer or its deadline label), then the end it reads back
|
|
1214
|
-
* (get, deadlineOf), then init's own deadline, to that end. The deadline returned is the one init took.
|
|
1490
|
+
* (get, deadlineOf), then init's own deadline, to that end. The deadline returned is the one init took. A host that
|
|
1491
|
+
* keeps labels in the machine has no extend (the constructor).
|
|
1215
1492
|
*/
|
|
1216
1493
|
async #extend(ref, ttlMs, signal) {
|
|
1217
1494
|
const found = await this.#own(ref, signal);
|
|
@@ -1285,8 +1562,11 @@ export class HostedProvider {
|
|
|
1285
1562
|
const found = await this.#get(ref.id);
|
|
1286
1563
|
if (!found.ok)
|
|
1287
1564
|
return found;
|
|
1288
|
-
if (found.value === undefined)
|
|
1565
|
+
if (found.value === undefined) {
|
|
1566
|
+
// Gone already (deleted by hand, or the vendor's timer): its token and socket still go.
|
|
1567
|
+
await this.#revoke(ref.name);
|
|
1289
1568
|
return ok(undefined);
|
|
1569
|
+
}
|
|
1290
1570
|
if (!ours(found.value.labels, ref.name))
|
|
1291
1571
|
return this.#fail("not_found", `no ${this.#label} computer is ${ref.name} (${ref.id}): the machine is not this provider's, so it is left as it is`);
|
|
1292
1572
|
this.#end(ref.name);
|
|
@@ -1304,6 +1584,7 @@ export class HostedProvider {
|
|
|
1304
1584
|
*/
|
|
1305
1585
|
async #remove(ref, machine, signal) {
|
|
1306
1586
|
const letGo = await this.#letGoOf(ref, machine, signal);
|
|
1587
|
+
await this.#revoke(ref.name);
|
|
1307
1588
|
const killed = await this.#kill(ref.id);
|
|
1308
1589
|
return { letGo, gone: killed.ok ? await this.#settle(ref, signal) : killed };
|
|
1309
1590
|
}
|
|
@@ -1329,39 +1610,129 @@ export class HostedProvider {
|
|
|
1329
1610
|
}
|
|
1330
1611
|
/** The labels a listing by one computer's name asks for. */
|
|
1331
1612
|
const byName = (name) => ({ [COMPUTER_LABEL]: "1", [NAME_LABEL]: name });
|
|
1613
|
+
/** The room of a host that keeps its labels packed (facts.labels.packed), or undefined. */
|
|
1614
|
+
function packedRoom(host) {
|
|
1615
|
+
const room = host.facts.labels;
|
|
1616
|
+
return room !== "machine" && room?.packed ? room : undefined;
|
|
1617
|
+
}
|
|
1618
|
+
/** A machine as the host read it, with a packed host's labels unpacked: none where it holds no packed map (not the adapter's). */
|
|
1619
|
+
const unpacked = (host, machine) => (packedRoom(host) === undefined ? machine : { ...machine, labels: unpackLabels(machine.labels) ?? {} });
|
|
1332
1620
|
/**
|
|
1333
1621
|
* The host's listing by labels, bounded by ms: only the machines that carry every one of them, since a vendor's listing
|
|
1334
|
-
* may answer more (MachineHost.list).
|
|
1622
|
+
* may answer more (MachineHost.list). A packed host is asked for PACKED_NAME alone, the name's where one is asked, and
|
|
1623
|
+
* its machines' labels unpacked. A host that keeps labels in the machine lists none: its machines of another name are
|
|
1624
|
+
* left out, and the rest kept for foundBy to read.
|
|
1335
1625
|
*/
|
|
1336
1626
|
export async function listedBy(host, what, labels, ms, signal) {
|
|
1337
|
-
const
|
|
1338
|
-
|
|
1627
|
+
const name = labels[NAME_LABEL];
|
|
1628
|
+
const asked = packedRoom(host) === undefined ? labels : name === undefined ? {} : { [PACKED_NAME]: name };
|
|
1629
|
+
const listed = await hostCall(host, what, (ctx) => host.list(asked, ctx), ms, signal);
|
|
1630
|
+
const keeps = (machine) => (host.facts.labels === "machine" ? name === undefined || machine.name === undefined || machine.name === name : carries(machine, labels));
|
|
1631
|
+
return listed.ok ? ok(listed.value.map((machine) => unpacked(host, machine)).filter(keeps)) : listed;
|
|
1632
|
+
}
|
|
1633
|
+
/** Whether a machine's image is ours: the provider's image option, or a box image by its name rule (isBoxImage). */
|
|
1634
|
+
const ourImage = (machine, image) => machine.image !== undefined && (machine.image === image || isBoxImage(machine.image));
|
|
1635
|
+
/** With labels kept in the machine: each machine's label map as its file was last read, by host and machine id. */
|
|
1636
|
+
const filed = new WeakMap();
|
|
1637
|
+
function filedOf(host) {
|
|
1638
|
+
let known = filed.get(host);
|
|
1639
|
+
if (known === undefined)
|
|
1640
|
+
filed.set(host, (known = new Map()));
|
|
1641
|
+
return known;
|
|
1642
|
+
}
|
|
1643
|
+
/** A label file's JSON as a label map, or undefined where it is not one. */
|
|
1644
|
+
function labelMapOf(text) {
|
|
1645
|
+
try {
|
|
1646
|
+
const map = JSON.parse(text);
|
|
1647
|
+
return typeof map === "object" && map !== null && !Array.isArray(map) && Object.values(map).every((value) => typeof value === "string") ? map : undefined;
|
|
1648
|
+
}
|
|
1649
|
+
catch {
|
|
1650
|
+
return undefined;
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
/**
|
|
1654
|
+
* A machine of a host that keeps labels in the machine, with the labels its file holds: read through exec while it runs
|
|
1655
|
+
* (fresh), within ms, else as last read, or none. No file (exit 3) is none; a read that fails otherwise, or does not end
|
|
1656
|
+
* within ms, is an error, never none (foundBy leaves the machine out of its listing, in words). Another host's machine is
|
|
1657
|
+
* as read.
|
|
1658
|
+
*/
|
|
1659
|
+
async function labelled(host, machine, ms, signal, fresh) {
|
|
1660
|
+
if (host.facts.labels !== "machine" || machine === undefined)
|
|
1661
|
+
return ok(machine);
|
|
1662
|
+
const known = filedOf(host);
|
|
1663
|
+
const held = known.get(machine.id);
|
|
1664
|
+
if (machine.state !== "running" || (held !== undefined && !fresh))
|
|
1665
|
+
return ok({ ...machine, labels: { ...held } });
|
|
1666
|
+
const what = `reading the labels in ${machine.id}`;
|
|
1667
|
+
const read = await runToExit(host, machine.id, labelsCommand(), { ms, ...(signal ? { signal } : {}), what });
|
|
1668
|
+
// Gone since the vendor's read: as a read that finds none.
|
|
1669
|
+
if (!read.ok)
|
|
1670
|
+
return read.error.code === "not_found" ? ok(undefined) : read;
|
|
1671
|
+
if (read.value.exitCode === 3)
|
|
1672
|
+
return ok({ ...machine, labels: {} });
|
|
1673
|
+
const labels = read.value.exitCode === 0 ? labelMapOf(read.value.stdout) : undefined;
|
|
1674
|
+
if (labels === undefined)
|
|
1675
|
+
return failFor(host.descriptor.id)("vendor", `${what} failed (exit ${read.value.exitCode}): ${`${read.value.stdout} ${read.value.stderr}`.trim().slice(0, 300)}`);
|
|
1676
|
+
known.set(machine.id, labels);
|
|
1677
|
+
return ok({ ...machine, labels: { ...labels } });
|
|
1339
1678
|
}
|
|
1340
1679
|
/** Whether a machine carries every one of these labels. */
|
|
1341
1680
|
const carries = (machine, labels) => Object.entries(labels).every(([key, value]) => machine.labels?.[key] === value);
|
|
1342
1681
|
/**
|
|
1343
1682
|
* The listing finds, get answers (MachineHost.list): the machines the host's listing by labels names, each read now by
|
|
1344
|
-
* readMachine, within requestMs
|
|
1345
|
-
* not every label
|
|
1683
|
+
* readMachine, within requestMs, and its label file where the host keeps labels in the machine. One the vendor no longer
|
|
1684
|
+
* has (not found) is dropped, and so is one whose get carries not every label, but a machine of the name asked for whose
|
|
1685
|
+
* labels are not written yet. A label file's read that fails or does not end within requestMs leaves its machine out of
|
|
1686
|
+
* this listing alone, told through onNote by its id: one wedged machine never holds up the rest. A listing by a
|
|
1687
|
+
* request's name is the exception: what is left of it may be the request's own, so such a read fails it, as a get that
|
|
1688
|
+
* fails fails the whole. The label file is read only in a machine whose listed image is ours (ourImage): another's is
|
|
1689
|
+
* left out unread, and one that shows no image is left out in words.
|
|
1346
1690
|
*/
|
|
1347
|
-
export async function foundBy(host, what, labels, { listMs, requestMs }, signal) {
|
|
1691
|
+
export async function foundBy(host, what, labels, { listMs, requestMs, onNote, image }, signal) {
|
|
1348
1692
|
const listed = await listedBy(host, what, labels, listMs, signal);
|
|
1349
1693
|
if (!listed.ok)
|
|
1350
1694
|
return listed;
|
|
1351
|
-
const
|
|
1695
|
+
const name = labels[NAME_LABEL];
|
|
1696
|
+
// With labels in the machine, the label read runs only in a machine of our image: never in another's.
|
|
1697
|
+
const readable = (machine) => {
|
|
1698
|
+
if (host.facts.labels !== "machine")
|
|
1699
|
+
return true;
|
|
1700
|
+
if (machine.image === undefined)
|
|
1701
|
+
onNote?.({ computer: machine.id, note: "its vendor record shows no image, so its labels are not read: this listing leaves it out" });
|
|
1702
|
+
return ourImage(machine, image);
|
|
1703
|
+
};
|
|
1704
|
+
const reads = await Promise.all(listed.value.filter(readable).map(async ({ id }) => {
|
|
1705
|
+
const read = await readMachine(host, id, requestMs, signal);
|
|
1706
|
+
if (!read.ok)
|
|
1707
|
+
return read;
|
|
1708
|
+
const inside = await labelled(host, read.value, requestMs, signal, true);
|
|
1709
|
+
// A listing by a request's name is left with machines of that name or of none (listedBy), and either may be the
|
|
1710
|
+
// request's own: one that can't be read fails the listing, so its create never makes a second.
|
|
1711
|
+
if (inside.ok || inside.error.code === "aborted" || name !== undefined)
|
|
1712
|
+
return inside;
|
|
1713
|
+
onNote?.({ computer: id, note: `${inside.error.message}; this listing leaves it out, and the next one reads it again` });
|
|
1714
|
+
return ok(undefined);
|
|
1715
|
+
}));
|
|
1716
|
+
// With labels in the machine, one of the name asked for with none written yet is kept too, for the caller to judge.
|
|
1717
|
+
const unfiled = (machine) => host.facts.labels === "machine" && name !== undefined && machine.name === name && machine.labels[COMPUTER_LABEL] === undefined;
|
|
1352
1718
|
const found = [];
|
|
1353
1719
|
for (const read of reads) {
|
|
1354
1720
|
if (!read.ok)
|
|
1355
1721
|
return read;
|
|
1356
|
-
if (read.value !== undefined && carries(read.value, labels))
|
|
1722
|
+
if (read.value !== undefined && (carries(read.value, labels) || unfiled(read.value)))
|
|
1357
1723
|
found.push(read.value);
|
|
1358
1724
|
}
|
|
1359
1725
|
return ok(found);
|
|
1360
1726
|
}
|
|
1361
|
-
/**
|
|
1727
|
+
/**
|
|
1728
|
+
* The host's get of one machine, bounded by ms, a packed host's labels unpacked: a machine the vendor reports as not
|
|
1729
|
+
* found is none, never an error.
|
|
1730
|
+
*/
|
|
1362
1731
|
async function readMachine(host, id, ms, signal) {
|
|
1363
1732
|
const read = await hostCall(host, `the read of ${id}`, (ctx) => host.get(id, ctx), ms, signal);
|
|
1364
|
-
|
|
1733
|
+
if (!read.ok)
|
|
1734
|
+
return read.error.code === "not_found" ? ok(undefined) : read;
|
|
1735
|
+
return ok(read.value === undefined ? undefined : unpacked(host, read.value));
|
|
1365
1736
|
}
|
|
1366
1737
|
/** What reading answers, or undefined once a signal aborts first: a caller that stopped waiting never waits on a read. */
|
|
1367
1738
|
function untilAborted(reading, signals) {
|