@parcha/agentrun-computer 0.1.0-beta.38 → 0.1.0-beta.40
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 +126 -83
- package/box/Dockerfile +5 -54
- package/box/README.md +9 -3
- package/box/build-daytona.py +2 -2
- package/box/build-e2b.mjs +1 -1
- package/box/install.sh +87 -0
- package/box/recipe.d.mts +5 -0
- package/box/recipe.mjs +12 -10
- package/dist/agent37.d.ts +17 -4
- package/dist/agent37.js +98 -17
- package/dist/box-image.d.ts +14 -0
- package/dist/box-image.js +24 -0
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/computerd.json +2 -2
- package/dist/conformance/agent37.d.ts +9 -4
- package/dist/conformance/agent37.js +23 -11
- package/dist/conformance/browserbase.js +3 -3
- package/dist/conformance/cases/apps.d.ts +1 -1
- package/dist/conformance/cases/apps.js +1 -1
- package/dist/conformance/cases/isolation.d.ts +18 -3
- package/dist/conformance/cases/isolation.js +73 -6
- package/dist/conformance/cases/lifetime.js +30 -0
- package/dist/conformance/cases/mount.js +4 -2
- package/dist/conformance/cases/provider.js +88 -1
- package/dist/conformance/cli.js +29 -9
- package/dist/conformance/e2b.js +9 -4
- package/dist/conformance/exec-stream.d.ts +44 -7
- package/dist/conformance/exec-stream.js +347 -88
- package/dist/conformance/kernel.js +2 -2
- package/dist/conformance/modal.js +1 -1
- package/dist/conformance/orgo.js +2 -2
- package/dist/conformance/report.js +3 -0
- package/dist/conformance/runner.js +127 -9
- package/dist/conformance/types.d.ts +21 -0
- package/dist/daytona.d.ts +0 -5
- package/dist/daytona.js +19 -28
- package/dist/descriptor.d.ts +5 -3
- package/dist/descriptors/agent37.d.ts +3 -11
- package/dist/descriptors/agent37.js +9 -10
- package/dist/descriptors/daytona.d.ts +1 -1
- package/dist/descriptors/daytona.js +5 -2
- package/dist/descriptors/docker.d.ts +13 -1
- package/dist/descriptors/docker.js +3 -1
- package/dist/descriptors/e2b.d.ts +2 -9
- package/dist/descriptors/e2b.js +8 -8
- package/dist/descriptors/index.d.ts +21 -19
- package/dist/descriptors/index.js +1 -3
- package/dist/descriptors/modal.d.ts +2 -6
- package/dist/descriptors/modal.js +8 -6
- package/dist/descriptors/orgo.js +2 -1
- package/dist/docker/egress-main.js +23 -15
- package/dist/docker/egress.d.ts +6 -4
- package/dist/docker/egress.js +24 -16
- package/dist/docker/inspect.d.ts +2 -2
- package/dist/docker/inspect.js +5 -5
- package/dist/docker.d.ts +6 -5
- package/dist/docker.js +12 -11
- package/dist/e2b.d.ts +12 -2
- package/dist/e2b.js +42 -19
- package/dist/host.d.ts +113 -59
- package/dist/host.js +5 -0
- package/dist/hosted/framed-exec.d.ts +49 -0
- package/dist/hosted/framed-exec.js +185 -0
- package/dist/hosted/network.d.ts +23 -0
- package/dist/hosted/network.js +77 -0
- package/dist/hosted/process.d.ts +13 -5
- package/dist/hosted/process.js +35 -12
- package/dist/hosted/provider.d.ts +16 -9
- package/dist/hosted/provider.js +475 -170
- package/dist/hosted/rest.d.ts +20 -2
- package/dist/hosted/rest.js +24 -10
- package/dist/hosted/scripts.d.ts +27 -7
- package/dist/hosted/scripts.js +77 -18
- package/dist/{agent37-ssh.d.ts → hosted/ssh-exec.d.ts} +19 -9
- package/dist/{agent37-ssh.js → hosted/ssh-exec.js} +38 -19
- package/dist/hosted/values.d.ts +21 -0
- package/dist/hosted/values.js +55 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/isolation.d.ts +0 -2
- package/dist/isolation.js +3 -4
- package/dist/modal.d.ts +8 -2
- package/dist/modal.js +21 -15
- package/dist/mount-archil.d.ts +5 -4
- package/dist/mount-archil.js +5 -6
- package/dist/orgo.js +6 -4
- package/dist/providers.d.ts +4 -1
- package/dist/surface/api.d.ts +0 -9
- package/dist/surface/core.d.ts +7 -2
- package/dist/surface/core.js +12 -4
- package/dist/surface/index.d.ts +1 -1
- package/dist/surface/index.js +1 -1
- package/dist/surface/script.d.ts +4 -0
- package/dist/surface/script.js +9 -3
- package/package.json +1 -1
- package/box/build-agent37.mjs +0 -158
package/dist/hosted/provider.js
CHANGED
|
@@ -10,10 +10,12 @@ import { createHash, randomBytes } from "node:crypto";
|
|
|
10
10
|
import { readFileSync } from "node:fs";
|
|
11
11
|
import { resolve } from "node:path";
|
|
12
12
|
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { isDeepStrictEqual } from "node:util";
|
|
13
14
|
import { CreateRequests, endedWords, otherSpecWords, readBackDiffer } from "../create-requests.js";
|
|
14
|
-
import {
|
|
15
|
+
import { desktopOf, startDisplay } from "../desktop.js";
|
|
15
16
|
import { COMPUTER_LABEL, DAEMON_IN_COMPUTER, DEADLINE_LABEL, helloDrift, probeNetworkDrift, probeNotes, probeReportOf, show, WORK } from "../drift.js";
|
|
16
|
-
import {
|
|
17
|
+
import { BACKSTOP_MARGIN_MS } from "../host.js";
|
|
18
|
+
import { readOwnNetwork, TOOL_USER } from "../isolation.js";
|
|
17
19
|
import { releaseOlder } from "../janitor.js";
|
|
18
20
|
import { RUN_LABEL } from "../labels.js";
|
|
19
21
|
import { checkMount, readBackOf } from "../mount.js";
|
|
@@ -23,15 +25,18 @@ import { failFor, ok } from "../result.js";
|
|
|
23
25
|
import { daemonWatch } from "../screen-watch.js";
|
|
24
26
|
import { countTested } from "../test-connection.js";
|
|
25
27
|
import { waitUntil } from "../wait.js";
|
|
26
|
-
import { bounded, CALL_SLACK_MS, hostCall, openBridge, pause, rootOf, runToExit, seconds, uploadThroughExec } from "./process.js";
|
|
27
|
-
import { isWhole } from "./values.js";
|
|
28
|
-
import {
|
|
28
|
+
import { bounded, CALL_SLACK_MS, hostCall, OUTPUT_MAX, openBridge, pause, rootOf, runToExit, seconds, uploadThroughExec } from "./process.js";
|
|
29
|
+
import { isWhole, packLabels } from "./values.js";
|
|
30
|
+
import { EGRESS_POLICY, EGRESS_PROXY_USER, EGRESS_RELAY, HOSTED_ALLOW, NETWORK_LABEL, networkFormOf, networkOfForm, networkRefusal, throughProxy } from "./network.js";
|
|
31
|
+
import { ARCHIL_GATE, closedSocketsOf, daemonDrift, hostedCommands, installerOf, LABEL_FILE, labelsCommand } from "./scripts.js";
|
|
29
32
|
const VERSION = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version;
|
|
30
33
|
export const NAME_LABEL = `${COMPUTER_LABEL}.name`;
|
|
31
34
|
/** The image create was asked for: a vendor may report another name for it (a template's id). */
|
|
32
35
|
export const IMAGE_LABEL = `${COMPUTER_LABEL}.image`;
|
|
33
36
|
/** The most extend may move the deadline to: create's time plus the cap, by this machine's clock, as init was given. */
|
|
34
37
|
export const MAX_LABEL = `${COMPUTER_LABEL}.max-deadline`;
|
|
38
|
+
/** A GPU computer's GPUs, type:count: a retry and attach read them here, so no vendor need read them back. */
|
|
39
|
+
export const GPU_LABEL = `${COMPUTER_LABEL}.gpu`;
|
|
35
40
|
const NAME = /^computer-[0-9a-f]{12}$/;
|
|
36
41
|
const MACHINE_ID = /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,255}$/;
|
|
37
42
|
const USER = /^(\d+):(\d+)$/;
|
|
@@ -59,9 +64,14 @@ const ARCHIL_USER = `${ARCHIL_TOOL_UID}:${ARCHIL_TOOL_UID}`;
|
|
|
59
64
|
const RELEASE_POLL_MAX_MS = 5_000;
|
|
60
65
|
const LISTED_POLL_MS = 150;
|
|
61
66
|
const LISTED_ROUNDS = 2;
|
|
62
|
-
const LISTED_POLL_MAX_MS = 1_000;
|
|
63
67
|
const LISTEN_RETRY_MS = 250;
|
|
64
68
|
const START_POLL_MS = 500;
|
|
69
|
+
/**
|
|
70
|
+
* With labels kept in the machine: what a machine of a request's name with no label file must be older than, past the
|
|
71
|
+
* longest a create takes to write the file (#unfiledMs), before a sweep of the name takes it: another instance's longer
|
|
72
|
+
* timeouts, and the vendor's clock against this one's.
|
|
73
|
+
*/
|
|
74
|
+
const UNFILED_MARGIN_MS = 300_000;
|
|
65
75
|
/** The longest a Node timer or AbortSignal.timeout holds. */
|
|
66
76
|
const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
67
77
|
/** The longest timeout the adapter takes: the race of a call bounded by it must still fit a Node timer. */
|
|
@@ -74,9 +84,20 @@ const STAGING = "/tmp/agentrun-computerd-";
|
|
|
74
84
|
const DEFAULT_PROBE_TARGETS = ["1.1.1.1:443", "8.8.8.8:443", "[2606:4700:4700::1111]:443", "1.1.1.1:53/udp", "[2606:4700:4700::1111]:53/udp"];
|
|
75
85
|
/** A probe target: host:port, over TCP, or host:port/udp, a DNS query over UDP. */
|
|
76
86
|
const TARGET = /^(?:\[[0-9A-Fa-f:.]+\]|[A-Za-z0-9.-]+):[0-9]{1,5}(?:\/udp)?$/;
|
|
77
|
-
|
|
87
|
+
/** The spec's fields the adapter passes on or holds itself: a size goes in the request, for the host's check to refuse. */
|
|
88
|
+
const SPEC_FIELDS = new Set(["image", "lifetimeMs", "network", "labels", "mount", "gpu", "owner", "cpus", "memoryMb"]);
|
|
78
89
|
const policyWords = (policy) => (typeof policy === "string" ? policy : "other" in policy ? policy.other : JSON.stringify(policy));
|
|
90
|
+
/** What makes a computer's labels smaller, in words: the list only where its network is one. */
|
|
91
|
+
const shorterWords = (labels) => `give a shorter image, mount folder${labels[NETWORK_LABEL]?.startsWith("allow:") ? ", network list" : ""} or labels`;
|
|
79
92
|
const alive = (state) => state !== "gone";
|
|
93
|
+
/** The codes of a create the vendor refused, which made nothing. */
|
|
94
|
+
const REFUSED_CREATE = new Set(["invalid", "denied", "limit", "not_found", "refused", "unauthorized", "exists"]);
|
|
95
|
+
/**
|
|
96
|
+
* Whether a create that failed with code may have made a machine, so the request's name is swept. A refusal made
|
|
97
|
+
* nothing: a machine under the name then is another's (exists: another instance's create of the request), never
|
|
98
|
+
* deleted. Any other failure (the vendor's, a timeout) may have made one, answered in time or after create gave up.
|
|
99
|
+
*/
|
|
100
|
+
const mayHaveMade = (code) => !REFUSED_CREATE.has(code);
|
|
80
101
|
/** An ISO time a label holds, or undefined when it is missing or not the ISO time create writes. */
|
|
81
102
|
export function isoOf(written) {
|
|
82
103
|
const at = written === undefined ? Number.NaN : Date.parse(written);
|
|
@@ -115,6 +136,14 @@ function specUserOf(value) {
|
|
|
115
136
|
const [uid, gid] = [Number(found[1]), Number(found[2] ?? found[1])];
|
|
116
137
|
return uid <= MAX_ID && gid <= MAX_ID ? `${uid}:${gid}` : undefined;
|
|
117
138
|
}
|
|
139
|
+
/** The GPUs a machine's GPU_LABEL records (type:count), as a spec's gpu field; none where it records none. */
|
|
140
|
+
function gpuOf(written) {
|
|
141
|
+
const at = written?.lastIndexOf(":") ?? -1;
|
|
142
|
+
if (written === undefined || at < 1)
|
|
143
|
+
return {};
|
|
144
|
+
const count = Number(written.slice(at + 1));
|
|
145
|
+
return isWhole(count) ? { gpu: { type: written.slice(0, at), count } } : {};
|
|
146
|
+
}
|
|
118
147
|
/** The caller's labels of a machine: every one but the adapter's own namespace and the Archil mount's. */
|
|
119
148
|
const callerLabels = (labels) => Object.fromEntries(Object.entries(labels).filter(([key]) => key !== COMPUTER_LABEL && !key.startsWith(`${COMPUTER_LABEL}.`) && key !== ARCHIL_MOUNT_LABEL));
|
|
120
149
|
/** The shared adapter's provider: a ComputerProvider whose vendor is host. */
|
|
@@ -124,6 +153,8 @@ export class HostedProvider {
|
|
|
124
153
|
/** The vendor's machines, for the live sweep (conformance/sweep.ts); nothing else reaches past the provider to them. */
|
|
125
154
|
host;
|
|
126
155
|
extend;
|
|
156
|
+
/** The host's descriptor's configuration of this provider's kind: its groups and GPUs. */
|
|
157
|
+
#configuration;
|
|
127
158
|
#label;
|
|
128
159
|
#fail;
|
|
129
160
|
#daemonPath;
|
|
@@ -141,20 +172,37 @@ export class HostedProvider {
|
|
|
141
172
|
#requests = new CreateRequests();
|
|
142
173
|
/** Each computer's last extend, by machine id, while one runs: the next waits for it. */
|
|
143
174
|
#extending = new Map();
|
|
175
|
+
/** Each held computer's keepalive timer, by machine id (facts.idleMs). */
|
|
176
|
+
#kept = new Map();
|
|
144
177
|
constructor(host, options) {
|
|
145
178
|
const { kind = "linux", desktop = {}, maxLifetimeMs = host.facts.maxLifetimeMs, daemonPath = fileURLToPath(new URL(`../computerd-linux-${host.facts.arch}`, import.meta.url)), daemonUser = TOOL_USER, timeouts = {}, probeTargets = DEFAULT_PROBE_TARGETS, onNote, archilEgress, } = options;
|
|
146
|
-
const { id, label } = host.descriptor;
|
|
179
|
+
const { id, label, capabilities } = host.descriptor;
|
|
180
|
+
if (kind !== "desktop" && kind !== "linux")
|
|
181
|
+
throw new Error(`kind is desktop or linux, not ${String(kind)}`);
|
|
182
|
+
const configuration = capabilities.find((one) => one.kind === kind);
|
|
183
|
+
if (configuration === undefined)
|
|
184
|
+
throw new Error(`${label} offers no ${kind} computer`);
|
|
147
185
|
this.id = id;
|
|
148
186
|
this.host = host;
|
|
187
|
+
this.#configuration = configuration;
|
|
149
188
|
this.#label = label;
|
|
150
189
|
this.#fail = failFor(id);
|
|
151
190
|
this.#onNote = onNote;
|
|
152
191
|
this.#archilEgress = archilEgress === undefined || archilEgress === "" ? undefined : archilEgressOf(archilEgress);
|
|
153
|
-
if (kind !== "desktop" && kind !== "linux")
|
|
154
|
-
throw new Error(`kind is desktop or linux, not ${String(kind)}`);
|
|
155
192
|
if (!isWhole(maxLifetimeMs) || maxLifetimeMs <= 0 || maxLifetimeMs > host.facts.maxLifetimeMs) {
|
|
156
193
|
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`);
|
|
157
194
|
}
|
|
195
|
+
const { idleMs } = host.facts;
|
|
196
|
+
if (idleMs !== undefined && (!isWhole(idleMs) || idleMs <= 0 || idleMs > MAX_TIMER_MS))
|
|
197
|
+
throw new Error(`facts.idleMs must be a whole number of ms above 0 and at most ${MAX_TIMER_MS}, not ${idleMs}`);
|
|
198
|
+
if (idleMs !== undefined && !host.keepalive) {
|
|
199
|
+
throw new Error(`${label} declares an idle timer (facts.idleMs ${idleMs}) and no keepalive, so a quiet computer would end under its run: give the host keepalive, the vendor's call that restarts the timer`);
|
|
200
|
+
}
|
|
201
|
+
// A machine whose create was cut off before its labels were written carries none the janitor lists: only a vendor's
|
|
202
|
+
// timer ends it, at its deadline (lifetime vendor) or at the backstop.
|
|
203
|
+
if (host.facts.labels === "machine" && host.facts.lifetime !== "vendor" && host.facts.backstop !== true) {
|
|
204
|
+
throw new Error(`${label} keeps no labels (facts.labels "machine"), so a vendor timer must end its machines: a machine whose create stopped before its labels were written has none the janitor lists. Declare lifetime "vendor", or set the vendor's delete timer from req.backstopAt and declare backstop: true`);
|
|
205
|
+
}
|
|
158
206
|
const user = USER.exec(daemonUser);
|
|
159
207
|
if (user === null || Number(user[1]) > MAX_ID || Number(user[2]) > MAX_ID)
|
|
160
208
|
throw new Error(`the daemon user ${daemonUser} must be uid:gid in numbers, such as 1000:1000`);
|
|
@@ -184,24 +232,27 @@ export class HostedProvider {
|
|
|
184
232
|
if (host.extend)
|
|
185
233
|
this.extend = (ref, ttlMs, context) => this.#extendSerial(ref, ttlMs, context?.signal);
|
|
186
234
|
}
|
|
187
|
-
/** The computer's facts, from the host's and the adapter's options alone. */
|
|
235
|
+
/** The computer's facts, from the descriptor, the host's facts and the adapter's options alone. */
|
|
188
236
|
capabilities() {
|
|
189
237
|
const { facts, descriptor } = this.host;
|
|
238
|
+
const { gpu } = this.#configuration;
|
|
190
239
|
return {
|
|
191
240
|
kind: this.#kind,
|
|
192
241
|
lifetime: facts.lifetime,
|
|
193
242
|
network: [...descriptor.network],
|
|
194
|
-
//
|
|
243
|
+
// A list is the egress proxy's, init's own (./network.ts): Docker's bounds and ports.
|
|
244
|
+
...(descriptor.network.includes("allow") ? { allow: structuredClone(HOSTED_ALLOW) } : {}),
|
|
245
|
+
// The probe and the proxy's policy from inside are the network's truth: no vendor's read of it is taken.
|
|
195
246
|
networkReadBack: false,
|
|
196
247
|
isolation: facts.isolation,
|
|
197
248
|
os: "linux",
|
|
198
249
|
daemon: facts.daemon,
|
|
199
|
-
groups: [
|
|
250
|
+
groups: [...this.#configuration.groups],
|
|
200
251
|
maxLifetimeMs: this.#maxLifetimeMs,
|
|
201
252
|
release: ["delete"],
|
|
202
253
|
mount: [...descriptor.mounts],
|
|
203
254
|
vendorAgentPorts: [...facts.vendorAgentPorts],
|
|
204
|
-
...(
|
|
255
|
+
...(gpu === undefined ? {} : { gpu: structuredClone(gpu) }),
|
|
205
256
|
...(facts.user === undefined ? {} : { user: [...facts.user] }),
|
|
206
257
|
};
|
|
207
258
|
}
|
|
@@ -216,7 +267,7 @@ export class HostedProvider {
|
|
|
216
267
|
const archil = mount?.kind === "archil" && (capabilities.mount ?? []).includes("archil");
|
|
217
268
|
refusals.push(...checkMount(mount, capabilities.mount ?? [], this.host.descriptor.unmounted), ...(archil ? archilRefusals(given) : []));
|
|
218
269
|
const daemonUser = `${this.#uid}:${this.#gid}`;
|
|
219
|
-
const asked = this.#takesUser
|
|
270
|
+
const asked = this.#takesUser ? given.user : undefined;
|
|
220
271
|
if (asked !== undefined) {
|
|
221
272
|
const user = specUserOf(asked);
|
|
222
273
|
if (user === undefined)
|
|
@@ -227,6 +278,9 @@ export class HostedProvider {
|
|
|
227
278
|
else if (archil && user !== ARCHIL_USER) {
|
|
228
279
|
refuse("user", `a computer with an Archil mount runs its tools as agentrun-tool, ${ARCHIL_USER}, the user its image makes with no way to root; leave user unset or ask for ${ARCHIL_USER}`);
|
|
229
280
|
}
|
|
281
|
+
else if (throughProxy(network) && user.split(":").includes(String(EGRESS_PROXY_USER))) {
|
|
282
|
+
refuse("user", `the egress proxy of a network open or a list runs as uid and gid ${EGRESS_PROXY_USER} (nobody), so its programs' user may be neither; ask for another uid, such as 1000:1000`);
|
|
283
|
+
}
|
|
230
284
|
}
|
|
231
285
|
else if (archil && daemonUser !== ARCHIL_USER) {
|
|
232
286
|
refuse("mount", `a computer with an Archil mount runs its tools as agentrun-tool, ${ARCHIL_USER}, the user its image makes with no way to root; this provider's daemon user is ${daemonUser}: make it with daemonUser ${ARCHIL_USER}`);
|
|
@@ -234,25 +288,24 @@ export class HostedProvider {
|
|
|
234
288
|
if (typeof image !== "string" || image === "" || image.includes("\0"))
|
|
235
289
|
refuse("image", `an image is required: the name of ${a(label)} image`);
|
|
236
290
|
refusals.push(...checkTenure(given, capabilities, label));
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
refuse("network",
|
|
291
|
+
// Only the forms the descriptor declares, each one this computer enforces; any other is refused, never widened.
|
|
292
|
+
const networkProblem = networkRefusal(label, capabilities.network, network);
|
|
293
|
+
if (networkProblem)
|
|
294
|
+
refuse("network", networkProblem);
|
|
241
295
|
const entries = labels === undefined ? [] : typeof labels === "object" && labels !== null && !Array.isArray(labels) ? Object.entries(labels) : undefined;
|
|
242
|
-
const bad = entries === undefined ? "labels must map names to strings" :
|
|
296
|
+
const bad = entries === undefined ? "labels must map names to strings" : entries.map(([key, value]) => labelProblem(key, value)).find(Boolean);
|
|
243
297
|
if (bad)
|
|
244
298
|
refuse("labels", bad);
|
|
245
299
|
else if (typeof image === "string" && (!archil || ["disk", "region", "folder"].every((key) => typeof mount[key] === "string"))) {
|
|
246
|
-
const over = this.#
|
|
300
|
+
const over = this.#labelsOver(given);
|
|
247
301
|
if (over)
|
|
248
|
-
refuse(archil ? "mount" : "labels", over);
|
|
302
|
+
refuse(archil && over.bytes ? "mount" : "labels", over.why);
|
|
249
303
|
}
|
|
250
304
|
refusals.push(...checkGpu(given.gpu, capabilities, mount));
|
|
251
|
-
const taken = new Set(this.host.facts.specFields ?? []);
|
|
252
305
|
for (const [field, value] of Object.entries(given)) {
|
|
253
|
-
if (SPEC_FIELDS.has(field) ||
|
|
306
|
+
if (SPEC_FIELDS.has(field) || (field === "user" && this.#takesUser) || value === undefined)
|
|
254
307
|
continue;
|
|
255
|
-
if (field === "
|
|
308
|
+
if (field === "pids")
|
|
256
309
|
refuse(field, `${a(label)} computer has the size of its image: name an image of the size you need, not ${field}`);
|
|
257
310
|
else if (field === "user")
|
|
258
311
|
refuse(field, `${a(label)} computer's daemon runs as the provider's daemon user (${daemonUser}), never as one a spec names`);
|
|
@@ -265,39 +318,93 @@ export class HostedProvider {
|
|
|
265
318
|
refusals.push(...this.host.check(given));
|
|
266
319
|
return refusals;
|
|
267
320
|
}
|
|
268
|
-
/** Whether the host
|
|
269
|
-
#
|
|
270
|
-
return
|
|
321
|
+
/** Whether the host keeps no labels (facts.labels "machine"): the adapter keeps them in the machine, at LABEL_FILE. */
|
|
322
|
+
get #keeps() {
|
|
323
|
+
return this.host.facts.labels === "machine";
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* The longest a create takes from the vendor's create to the label file: its own call and the wait for the machine to
|
|
327
|
+
* run (createMs each), the listing of the name between them (listMs, and a read), and the file's write (execMs), plus
|
|
328
|
+
* UNFILED_MARGIN_MS.
|
|
329
|
+
*/
|
|
330
|
+
get #unfiledMs() {
|
|
331
|
+
const { createMs, listMs, requestMs, execMs } = this.#timeouts;
|
|
332
|
+
return 2 * createMs + listMs + requestMs + execMs + UNFILED_MARGIN_MS;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* With labels kept in the machine, a machine with no label file is the request's whose name it has (Machine.name, the
|
|
336
|
+
* request's own name, never a bare computer- prefix), and a sweep of the name takes it only once it is older than
|
|
337
|
+
* #unfiledMs: before that, another instance's create of the request may still be writing its file (provider.md,
|
|
338
|
+
* Idempotent create). A machine with no createdAt is never old enough: the vendor's timer ends it.
|
|
339
|
+
*/
|
|
340
|
+
#unfiled(machine, name) {
|
|
341
|
+
if (!this.#keeps || machine.labels[COMPUTER_LABEL] !== undefined || machine.name !== name)
|
|
342
|
+
return undefined;
|
|
343
|
+
const age = machine.createdAt instanceof Date ? Date.now() - machine.createdAt.getTime() : Number.NaN;
|
|
344
|
+
return age > this.#unfiledMs ? "old" : "young";
|
|
345
|
+
}
|
|
346
|
+
/** Writes labels to the machine's label file, root's alone, and answers them as the file reads back. */
|
|
347
|
+
async #keep(ref, labels, signal) {
|
|
348
|
+
const what = `writing ${ref.name}'s labels to ${LABEL_FILE}`;
|
|
349
|
+
const json = Buffer.from(JSON.stringify(labels));
|
|
350
|
+
const wrote = await runToExit(this.host, ref.id, labelsCommand(json.length), { stdin: [json], ms: this.#timeouts.execMs, ...(signal ? { signal } : {}), what });
|
|
351
|
+
if (!wrote.ok)
|
|
352
|
+
return wrote;
|
|
353
|
+
const read = wrote.value.exitCode === 0 ? labelMapOf(wrote.value.stdout) : undefined;
|
|
354
|
+
if (read === undefined)
|
|
355
|
+
return this.#fail("refused", `${what} failed (exit ${wrote.value.exitCode}): ${`${wrote.value.stdout} ${wrote.value.stderr}`.trim().slice(0, 300)}`);
|
|
356
|
+
filedOf(this.host).set(ref.id, read);
|
|
357
|
+
return ok(read);
|
|
358
|
+
}
|
|
359
|
+
/** Whether the host takes a spec's user (facts.user): then a computer's daemon runs as the one a spec names. */
|
|
360
|
+
get #takesUser() {
|
|
361
|
+
return this.host.facts.user !== undefined;
|
|
271
362
|
}
|
|
272
363
|
/** Who one create's daemon runs as, uid:gid: the spec's user where the host takes one, else the provider's daemon user. */
|
|
273
364
|
#userOf(spec) {
|
|
274
|
-
return (this.#takesUser
|
|
365
|
+
return (this.#takesUser ? specUserOf(spec.user) : undefined) ?? `${this.#uid}:${this.#gid}`;
|
|
275
366
|
}
|
|
276
|
-
/**
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
367
|
+
/**
|
|
368
|
+
* Why a spec's labels, with the adapter's own (#labelsOf, as create gives them, each deadline an ISO time at its
|
|
369
|
+
* longest), are more, or come to more bytes, or pack into more, than the vendor takes (facts.labels); undefined when
|
|
370
|
+
* they fit.
|
|
371
|
+
*/
|
|
372
|
+
#labelsOver(spec) {
|
|
373
|
+
const room = this.host.facts.labels ?? {};
|
|
374
|
+
// Stand-ins as long as create's name and deadlines and as mixed, fixed so a spec's answer never changes (attach and a
|
|
375
|
+
// retry check the spec again): a packed map deflates them as it does create's, give or take a byte, and create packs
|
|
376
|
+
// its own before the host's call (#packedOver).
|
|
377
|
+
const name = "computer-5f3a9c1e7b2d";
|
|
378
|
+
const labels = this.#labelsOf(spec, name, "2026-11-16T04:15:29.481Z", "2026-11-17T04:05:29.481Z");
|
|
379
|
+
if (room === "machine") {
|
|
380
|
+
// The label file is read back whole only up to what a run keeps of its output.
|
|
381
|
+
const json = Buffer.byteLength(JSON.stringify(labels));
|
|
382
|
+
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)}` };
|
|
383
|
+
}
|
|
384
|
+
const { count, bytes: max, packed } = room;
|
|
385
|
+
if (packed) {
|
|
386
|
+
const why = this.#packedOver(labels, name);
|
|
387
|
+
return why === undefined ? undefined : { bytes: true, why };
|
|
388
|
+
}
|
|
389
|
+
const keys = Object.keys(labels);
|
|
390
|
+
if (count !== undefined && keys.length > count) {
|
|
391
|
+
// labelProblem refuses a caller's label of the adapter's names, so the two never overlap.
|
|
392
|
+
const own = keys.length - Object.keys(spec.labels ?? {}).length;
|
|
393
|
+
return { bytes: false, why: `${a(this.#label)} computer carries at most ${count} labels, the adapter's own among them, and this one's would carry ${keys.length}, ${own} of them the adapter's: give at most ${count - own} labels` };
|
|
394
|
+
}
|
|
284
395
|
if (max === undefined)
|
|
285
396
|
return undefined;
|
|
286
|
-
const own = Object.keys(this.#longestLabels(spec, false)).length;
|
|
287
|
-
return count + own > max ? `${a(this.#label)} computer takes ${max} labels, ${own} of them the provider's own: ${count} is ${count + own - max} too many` : undefined;
|
|
288
|
-
}
|
|
289
|
-
/** Why a spec's labels, with the adapter's own at their longest, come to more bytes than the vendor takes; undefined when they fit. */
|
|
290
|
-
#labelBytes(spec) {
|
|
291
|
-
const { maxBytes, measure = "pairs" } = this.host.facts.labelRules ?? {};
|
|
292
|
-
if (maxBytes === undefined)
|
|
293
|
-
return undefined;
|
|
294
|
-
const labels = this.#longestLabels(spec, true);
|
|
295
397
|
const sizes = Object.entries(labels).map(([key, value]) => [key, Buffer.byteLength(key) + Buffer.byteLength(value)]);
|
|
296
|
-
const bytes =
|
|
297
|
-
if (bytes <=
|
|
398
|
+
const bytes = sizes.reduce((sum, [, size]) => sum + size, 0);
|
|
399
|
+
if (bytes <= max)
|
|
298
400
|
return undefined;
|
|
299
|
-
|
|
300
|
-
|
|
401
|
+
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)}` };
|
|
402
|
+
}
|
|
403
|
+
/** Why labels don't fit packed (facts.labels.packed), in words; undefined when they fit or the host keeps them as they are. */
|
|
404
|
+
#packedOver(labels, name) {
|
|
405
|
+
const room = this.host.facts.labels;
|
|
406
|
+
const fit = room !== "machine" && room?.packed ? packLabels(labels, name, room) : { labels };
|
|
407
|
+
return "problem" in fit ? `${a(this.#label)} computer keeps its labels packed, and ${fit.problem}: ${shorterWords(labels)}` : undefined;
|
|
301
408
|
}
|
|
302
409
|
/** A host call bounded by ms, the caller's signal when given. */
|
|
303
410
|
#call(what, run, ms, signal) {
|
|
@@ -329,26 +436,36 @@ export class HostedProvider {
|
|
|
329
436
|
#labelDiffer(spec, name, labels, seen) {
|
|
330
437
|
const asked = this.#labelsOf(spec, name, labels[DEADLINE_LABEL] ?? "", labels[MAX_LABEL] ?? "");
|
|
331
438
|
const differ = Object.entries(asked)
|
|
332
|
-
|
|
439
|
+
// The GPUs' label is read back as the spec's gpu, held where the user and network are (#again).
|
|
440
|
+
.filter(([key, value]) => key !== DEADLINE_LABEL && key !== MAX_LABEL && key !== GPU_LABEL && labels[key] !== value)
|
|
333
441
|
.map(([key, value]) => `label ${key}: asked ${show(value)}, ${seen} ${show(labels[key])}`);
|
|
334
442
|
const extra = Object.keys(callerLabels(labels)).filter((key) => !Object.hasOwn(asked, key));
|
|
335
443
|
if (extra.length > 0)
|
|
336
444
|
differ.push(`labels ${extra.join(", ")}: ${seen}, never asked for`);
|
|
337
445
|
return differ;
|
|
338
446
|
}
|
|
339
|
-
|
|
447
|
+
/** taken: the vendor refused this create's name as one a machine has (exists), so the name's machine is read once more. */
|
|
448
|
+
async #createOnce(spec, name, signal, taken) {
|
|
340
449
|
const listed = await foundBy(this.host, "the listing by the request's name", byName(name), this.#timeouts, signal);
|
|
341
450
|
if (!listed.ok)
|
|
342
451
|
return listed;
|
|
343
|
-
|
|
452
|
+
// With labels kept in the machine, a young one of the name with none written yet is this request's still being made
|
|
453
|
+
// (#again waits for its labels); an old one never got them, and is deleted.
|
|
454
|
+
const found = listed.value.filter((machine) => ours(machine.labels, name) || this.#unfiled(machine, name) === "young");
|
|
455
|
+
if (found.length === 0 && listed.value.some((machine) => this.#unfiled(machine, name) === "old")) {
|
|
456
|
+
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)}`);
|
|
457
|
+
}
|
|
344
458
|
if (found.length > 1)
|
|
345
459
|
return this.#fail("exists", `${found.length} machines carry the name ${name} (${found.map((m) => m.id).join(", ")}): create with a new request id`);
|
|
346
460
|
if (found.length === 1)
|
|
347
461
|
return this.#again(spec, name, found[0], signal);
|
|
462
|
+
if (taken)
|
|
463
|
+
return taken;
|
|
348
464
|
const launched = await this.#launch(spec, name, signal);
|
|
349
465
|
if (!launched.ok)
|
|
350
|
-
return launched;
|
|
466
|
+
return launched.error.code === "exists" ? this.#createOnce(spec, name, signal, launched) : launched;
|
|
351
467
|
const { ref, made } = launched.value;
|
|
468
|
+
this.#keepAlive(ref);
|
|
352
469
|
// From here the machine exists: every step takes the caller's signal, and a failure or an abort deletes it.
|
|
353
470
|
// Another provider instance's create of this request may have made one too (#converge): the oldest is kept.
|
|
354
471
|
const first = await this.#converge(name, ref.id, signal);
|
|
@@ -422,15 +539,18 @@ export class HostedProvider {
|
|
|
422
539
|
const deadline = deadlineOf(machine);
|
|
423
540
|
if (deadline !== undefined && deadline.getTime() <= Date.now())
|
|
424
541
|
return this.#fail("exists", endedWords(name));
|
|
425
|
-
|
|
542
|
+
// With labels kept in the machine: the create making it has not written them yet.
|
|
543
|
+
const unwritten = machine.state === "running" && this.#unfiled(machine, name) !== undefined;
|
|
544
|
+
if (unwritten)
|
|
545
|
+
last = { code: "refused", message: "has no labels written" };
|
|
546
|
+
if (machine.state === "running" && !unwritten) {
|
|
426
547
|
const differ = this.#labelDiffer(spec, name, machine.labels, "read back");
|
|
427
548
|
if (differ.length > 0)
|
|
428
549
|
return this.#fail("exists", otherSpecWords(name, "was made", differ));
|
|
429
550
|
const attached = await this.#attached(machine, signal);
|
|
430
|
-
// The image
|
|
431
|
-
//
|
|
432
|
-
const
|
|
433
|
-
const drift = attached.ok ? readBackDiffer({ ...spec, user: this.#userOf(spec) }, attached.value.spec, ["user", "network", ...gpu]) : [];
|
|
551
|
+
// The image, an Archil mount and the GPUs are in the labels; the user and the network are read back. No vendor
|
|
552
|
+
// shows a drift in a size.
|
|
553
|
+
const drift = attached.ok ? readBackDiffer({ ...spec, network: networkOfForm(formOf(spec.network)), user: this.#userOf(spec) }, attached.value.spec, ["user", "network", "gpu"]) : [];
|
|
434
554
|
if (drift.length > 0)
|
|
435
555
|
return this.#fail("exists", otherSpecWords(name, "was made", drift));
|
|
436
556
|
if (attached.ok)
|
|
@@ -456,16 +576,23 @@ export class HostedProvider {
|
|
|
456
576
|
machine = read.value;
|
|
457
577
|
}
|
|
458
578
|
}
|
|
459
|
-
/**
|
|
579
|
+
/**
|
|
580
|
+
* The labels create gives a computer: the caller's, the adapter's own, and an Archil mount's (never its token). The
|
|
581
|
+
* most its deadline may reach only where the host can extend it, the label's one reader: a host with no extend (Modal,
|
|
582
|
+
* Daytona) spends none of its label bytes on it (Modal's 512 hold the box image's tag beside an Archil mount's label).
|
|
583
|
+
*/
|
|
460
584
|
#labelsOf(spec, name, deadline, maxDeadline) {
|
|
585
|
+
const network = formOf(spec.network);
|
|
461
586
|
return {
|
|
462
587
|
...spec.labels,
|
|
463
588
|
[COMPUTER_LABEL]: "1",
|
|
464
589
|
[NAME_LABEL]: name,
|
|
465
590
|
[DEADLINE_LABEL]: deadline,
|
|
466
591
|
[IMAGE_LABEL]: spec.image,
|
|
467
|
-
[MAX_LABEL]: maxDeadline,
|
|
592
|
+
...(this.host.extend ? { [MAX_LABEL]: maxDeadline } : {}),
|
|
593
|
+
...(network === "" ? {} : { [NETWORK_LABEL]: network }),
|
|
468
594
|
...(spec.mount?.kind === "archil" ? archilLabels(spec.mount) : {}),
|
|
595
|
+
...(typeof spec.gpu?.type === "string" && typeof spec.gpu.count === "number" ? { [GPU_LABEL]: `${spec.gpu.type}:${spec.gpu.count}` } : {}),
|
|
469
596
|
};
|
|
470
597
|
}
|
|
471
598
|
/** Everything create does up to the vendor's answer: the daemon read, the machine made, by its request. */
|
|
@@ -485,6 +612,10 @@ export class HostedProvider {
|
|
|
485
612
|
const deadline = new Date(now + lifetimeMs).toISOString();
|
|
486
613
|
const maxDeadline = new Date(now + this.#maxLifetimeMs).toISOString();
|
|
487
614
|
const labels = this.#labelsOf(spec, name, deadline, maxDeadline);
|
|
615
|
+
// check() packed stand-ins; these deflate a byte apart from them, so the host is never given labels that don't fit.
|
|
616
|
+
const over = this.#packedOver(labels, name);
|
|
617
|
+
if (over !== undefined)
|
|
618
|
+
return this.#fail("refused", `labels: ${over}`);
|
|
488
619
|
const archil = spec.mount?.kind === "archil";
|
|
489
620
|
const user = this.#userOf(spec);
|
|
490
621
|
const commands = hostedCommands({
|
|
@@ -499,44 +630,43 @@ export class HostedProvider {
|
|
|
499
630
|
nonce: randomBytes(16).toString("hex"),
|
|
500
631
|
waitS: Math.ceil((this.#timeouts.createMs + this.#timeouts.uploadMs + this.#timeouts.execMs + (archil ? this.#timeouts.mountMs * 3 : 0)) / 1000),
|
|
501
632
|
...(archil && this.host.facts.daemon === "pid1" ? { gate: ARCHIL_GATE } : {}),
|
|
502
|
-
|
|
633
|
+
rootOnly: this.host.facts.rootOnly ?? [],
|
|
634
|
+
network: formOf(spec.network),
|
|
503
635
|
});
|
|
504
|
-
const taken = new Set(this.host.facts.specFields ?? []);
|
|
505
636
|
const request = {
|
|
506
637
|
name,
|
|
507
638
|
image: spec.image,
|
|
508
639
|
labels,
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
//
|
|
512
|
-
network
|
|
640
|
+
lifetimeMs,
|
|
641
|
+
backstopAt: new Date(now + this.#maxLifetimeMs + BACKSTOP_MARGIN_MS).toISOString(),
|
|
642
|
+
// The machine has egress where its tools' network is open or a list, which init's proxy holds to the public
|
|
643
|
+
// internet (./network.ts), and where an Archil computer's root reaches Archil. Its tools' none is init's own
|
|
644
|
+
// either way, read back by the probe from inside, as the tool user.
|
|
645
|
+
egress: archil || throughProxy(spec.network),
|
|
513
646
|
...(this.host.facts.daemon === "pid1" ? { command: commands.entrypoint } : {}),
|
|
514
647
|
...(spec.gpu === undefined ? {} : { gpu: spec.gpu }),
|
|
515
|
-
...(
|
|
516
|
-
...(
|
|
517
|
-
...(
|
|
518
|
-
...(taken.has("user") && spec.user !== undefined ? { user } : {}),
|
|
648
|
+
...(spec.cpus === undefined ? {} : { cpus: spec.cpus }),
|
|
649
|
+
...(spec.memoryMb === undefined ? {} : { memoryMb: spec.memoryMb }),
|
|
650
|
+
...(this.#takesUser && spec.user !== undefined ? { user } : {}),
|
|
519
651
|
};
|
|
520
|
-
const runtime = this.host.facts.runtime?.(request);
|
|
521
652
|
const { createMs } = this.#timeouts;
|
|
522
653
|
// Never signalled, only raced: an aborted call could leave a machine made whose id never came back. One that lands
|
|
523
|
-
// after this gave up is deleted by its id; one
|
|
524
|
-
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) : this.#deleteNamed(name));
|
|
654
|
+
// after this gave up is deleted by its id; one that fails after it, by its name where it may have made one.
|
|
655
|
+
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);
|
|
525
656
|
if (!created.ok) {
|
|
526
657
|
const { code, message } = created.error;
|
|
527
658
|
if (code === "aborted" || code === "timeout") {
|
|
528
659
|
this.#end(name);
|
|
529
660
|
return this.#fail(code, `${message}; a machine it makes after this is deleted as it lands, and ${this.#label} ends it by ${deadline} either way`);
|
|
530
661
|
}
|
|
531
|
-
|
|
532
|
-
if (["invalid", "denied", "limit", "not_found", "refused", "unauthorized", "exists"].includes(code))
|
|
662
|
+
if (!mayHaveMade(code))
|
|
533
663
|
return created;
|
|
534
664
|
this.#end(name);
|
|
535
665
|
const swept = await this.#deleteNamed(name);
|
|
536
666
|
return this.#fail(code, `${this.#label} did not create the machine: ${message}${swept}`);
|
|
537
667
|
}
|
|
538
668
|
this.#requests.made(name);
|
|
539
|
-
return ok({ ref: { id: created.value.id, name }, made: { deadline, maxDeadline, labels, daemon, sha256, commands, user
|
|
669
|
+
return ok({ ref: { id: created.value.id, name }, made: { deadline, maxDeadline, labels, daemon, sha256, commands, user } });
|
|
540
670
|
}
|
|
541
671
|
/** Deletes a machine create gives up on, then waits, under the caller's signal, until get answers none for it. */
|
|
542
672
|
async #abandon(ref, error, signal, mounted = false) {
|
|
@@ -553,50 +683,104 @@ export class HostedProvider {
|
|
|
553
683
|
return this.#fail(error.code, `the computer ${ref.name} ${after}: ${error.message}`);
|
|
554
684
|
}
|
|
555
685
|
/**
|
|
556
|
-
* Every machine carrying a name create gave, deleted and read back gone: for a create
|
|
686
|
+
* Every machine carrying a name create gave, deleted and read back gone: for a create that may have made one whose
|
|
687
|
+
* id never came (mayHaveMade), never after a refusal, where a machine under the name is another's. The
|
|
557
688
|
* listing shows a new machine a moment after the vendor makes it, so it is read for the name until one shows or
|
|
558
|
-
* listedMs passes.
|
|
689
|
+
* listedMs passes, as create's own wait reads it. With labels kept in the machine, one with no label file yet is taken
|
|
690
|
+
* only once it is old (#unfiled). Best effort, in words.
|
|
559
691
|
*/
|
|
560
692
|
async #deleteNamed(name) {
|
|
561
693
|
const { listedMs, pollMs } = this.#timeouts;
|
|
562
694
|
const until = Date.now() + listedMs;
|
|
563
|
-
let
|
|
564
|
-
for (;;) {
|
|
695
|
+
let named;
|
|
696
|
+
for (let delay = Math.min(LISTED_POLL_MS, pollMs);; delay = Math.min(delay * 2, pollMs)) {
|
|
565
697
|
const listed = await foundBy(this.host, "the listing by name", byName(name), this.#timeouts);
|
|
566
698
|
if (!listed.ok)
|
|
567
699
|
return `; a machine named ${name} may be left (${listed.error.message}), which ends at its deadline`;
|
|
568
|
-
|
|
569
|
-
if (
|
|
700
|
+
named = listed.value.filter((machine) => ours(machine.labels, name) || this.#unfiled(machine, name) !== undefined);
|
|
701
|
+
if (named.length > 0 || Date.now() >= until)
|
|
570
702
|
break;
|
|
571
|
-
await pause(Math.min(
|
|
703
|
+
await pause(Math.min(delay, until - Date.now()));
|
|
572
704
|
}
|
|
573
|
-
|
|
705
|
+
const found = named.filter((machine) => this.#unfiled(machine, name) !== "young");
|
|
706
|
+
const young = named.length - found.length;
|
|
707
|
+
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`;
|
|
708
|
+
if (named.length === 0)
|
|
574
709
|
return `; no machine named ${name} was listed within ${seconds(listedMs)}, and any it made ends at its deadline`;
|
|
710
|
+
if (found.length === 0)
|
|
711
|
+
return left;
|
|
575
712
|
const said = [];
|
|
576
713
|
for (const machine of found) {
|
|
577
714
|
const { gone } = await this.#remove({ id: machine.id, name }, undefined);
|
|
578
715
|
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`);
|
|
579
716
|
}
|
|
580
|
-
return `; the machine named ${name} it made: ${said.join("; ")}`;
|
|
717
|
+
return `; the machine named ${name} it made: ${said.join("; ")}${left}`;
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Calls the host's keepalive on the machine every idleMs / 3 (facts.idleMs), from create or attach until release or
|
|
721
|
+
* the delete of a create that gave up, one call at a time: a tick while the host's last call has not settled, even
|
|
722
|
+
* past its timeout, is skipped. One that fails is told through onNote and the next tries again; a machine the vendor
|
|
723
|
+
* no longer has stops it. The timer holds no process open. An attach calls once at once too (now): a computer a
|
|
724
|
+
* restarted server finds may have been quiet for most of its idle window already.
|
|
725
|
+
*/
|
|
726
|
+
#keepAlive(ref, now = false) {
|
|
727
|
+
const { idleMs } = this.host.facts;
|
|
728
|
+
const keepalive = this.host.keepalive?.bind(this.host);
|
|
729
|
+
if (idleMs === undefined || keepalive === undefined || this.#kept.has(ref.id))
|
|
730
|
+
return;
|
|
731
|
+
let calling = false;
|
|
732
|
+
const keep = async () => {
|
|
733
|
+
if (calling)
|
|
734
|
+
return;
|
|
735
|
+
calling = true;
|
|
736
|
+
let call = Promise.resolve();
|
|
737
|
+
const kept = await this.#call(`the keepalive of ${ref.name}`, (ctx) => (call = keepalive(ref.id, ctx)), this.#timeouts.requestMs);
|
|
738
|
+
const free = () => (calling = false);
|
|
739
|
+
call.then(free, free);
|
|
740
|
+
if (kept.ok || this.#kept.get(ref.id) !== timer)
|
|
741
|
+
return;
|
|
742
|
+
if (kept.error.code === "not_found")
|
|
743
|
+
this.#unkeep(ref.id);
|
|
744
|
+
else
|
|
745
|
+
this.#onNote?.({ computer: ref.name, note: `${kept.error.message}; the next keepalive tries again in ${seconds(idleMs / 3)}` });
|
|
746
|
+
};
|
|
747
|
+
const timer = setInterval(keep, idleMs / 3);
|
|
748
|
+
timer.unref();
|
|
749
|
+
this.#kept.set(ref.id, timer);
|
|
750
|
+
if (now)
|
|
751
|
+
void keep();
|
|
752
|
+
}
|
|
753
|
+
#unkeep(id) {
|
|
754
|
+
clearInterval(this.#kept.get(id));
|
|
755
|
+
this.#kept.delete(id);
|
|
581
756
|
}
|
|
582
757
|
/** The host's delete, never signalled: a delete that cleans up after an abort must outlive it. Not found is done. */
|
|
583
758
|
async #kill(id) {
|
|
759
|
+
this.#unkeep(id);
|
|
584
760
|
const killed = await this.#call(`the delete of ${id}`, (ctx) => this.host.delete(id, ctx), this.#timeouts.requestMs);
|
|
585
|
-
|
|
761
|
+
if (!killed.ok && killed.error.code !== "not_found")
|
|
762
|
+
return killed;
|
|
763
|
+
filedOf(this.host).delete(id);
|
|
764
|
+
return ok(undefined);
|
|
586
765
|
}
|
|
587
766
|
/** The pause between two reads of a machine that is starting: the next read comes after it, within createMs. */
|
|
588
767
|
#startPollMs() {
|
|
589
768
|
return Math.min(START_POLL_MS, this.#timeouts.pollMs * 10);
|
|
590
769
|
}
|
|
591
|
-
/**
|
|
592
|
-
|
|
593
|
-
|
|
770
|
+
/**
|
|
771
|
+
* The host's get, bounded by requestMs; a machine the vendor reports as not found is none. Labels kept in the machine
|
|
772
|
+
* are read from its file once and then as last read, or now with fresh.
|
|
773
|
+
*/
|
|
774
|
+
async #get(id, signal, fresh = false) {
|
|
775
|
+
const read = await readMachine(this.host, id, this.#timeouts.requestMs, signal);
|
|
776
|
+
return read.ok ? labelled(this.host, read.value, this.#timeouts.requestMs, signal, fresh) : read;
|
|
594
777
|
}
|
|
595
778
|
/** The machine running, read while it starts (within createMs); gone or failed is refused in words. */
|
|
596
779
|
async #running(ref, signal) {
|
|
597
780
|
const until = Date.now() + this.#timeouts.createMs;
|
|
598
781
|
for (;;) {
|
|
599
|
-
|
|
782
|
+
// Its labels are read after this, as written (#prepare).
|
|
783
|
+
const read = await readMachine(this.host, ref.id, this.#timeouts.requestMs, signal);
|
|
600
784
|
if (!read.ok)
|
|
601
785
|
return read;
|
|
602
786
|
const machine = read.value;
|
|
@@ -631,14 +815,27 @@ export class HostedProvider {
|
|
|
631
815
|
* Everything between the vendor's answer and a computer that passed the read-back, whose value is what the read-back
|
|
632
816
|
* recorded that does not refuse it (notes).
|
|
633
817
|
*/
|
|
634
|
-
async #prepare(spec, ref, { labels, daemon, sha256, commands, user
|
|
818
|
+
async #prepare(spec, ref, { labels, daemon, sha256, commands, user }, signal) {
|
|
635
819
|
const { uploadMs, execMs } = this.#timeouts;
|
|
636
820
|
const running = await this.#running(ref, signal);
|
|
637
821
|
if (!running.ok)
|
|
638
822
|
return running;
|
|
639
|
-
|
|
823
|
+
// A host that keeps no labels: they go in the machine first, and are read back from there.
|
|
824
|
+
const kept = this.#keeps ? await this.#keep(ref, labels, signal) : ok(running.value.labels);
|
|
825
|
+
if (!kept.ok)
|
|
826
|
+
return kept;
|
|
827
|
+
const drift = this.#machineDrift({ ...running.value, labels: kept.value }, labels);
|
|
640
828
|
if (drift.length > 0)
|
|
641
829
|
return this.#fail("refused", `it did not match its spec: ${drift.join("; ")}`);
|
|
830
|
+
// Before anything of this create's is in the machine: what root runs is root's alone (the Archil helper's sha256,
|
|
831
|
+
// read at the mount's preflight, is read after this).
|
|
832
|
+
if ((this.host.facts.rootOnly ?? []).length > 0) {
|
|
833
|
+
const closed = await runToExit(this.host, ref.id, commands.rootOnly, { ms: execMs, ...(signal ? { signal } : {}), what: `making system paths root's alone in ${ref.name}` });
|
|
834
|
+
if (!closed.ok)
|
|
835
|
+
return closed;
|
|
836
|
+
if (closed.value.exitCode !== 0)
|
|
837
|
+
return this.#fail("refused", `making system paths root's alone failed (exit ${closed.value.exitCode}): ${`${closed.value.stdout} ${closed.value.stderr}`.trim().slice(0, 300)}`);
|
|
838
|
+
}
|
|
642
839
|
const staging = commands.install[4];
|
|
643
840
|
const bytes = new Uint8Array(daemon.buffer, daemon.byteOffset, daemon.byteLength);
|
|
644
841
|
const upload = this.host.upload?.bind(this.host);
|
|
@@ -661,7 +858,9 @@ export class HostedProvider {
|
|
|
661
858
|
// The run's folder, after the daemon is in place (serve runs it) and before init starts: no tool program runs before
|
|
662
859
|
// it is mounted. A folder mounted for a computer create then gives up on is let go before the delete, never signalled.
|
|
663
860
|
const started = async () => {
|
|
664
|
-
const
|
|
861
|
+
const gpu = spec.gpu !== undefined;
|
|
862
|
+
const isolation = (gpu ? this.#configuration.gpu?.isolation : undefined) ?? this.host.facts.isolation;
|
|
863
|
+
const done = await this.#start(spec, ref, commands, { user, isolation, gpu }, signal);
|
|
665
864
|
return done.ok && installer !== undefined ? ok([installer, ...done.value]) : done;
|
|
666
865
|
};
|
|
667
866
|
if (spec.mount?.kind !== "archil")
|
|
@@ -687,16 +886,15 @@ export class HostedProvider {
|
|
|
687
886
|
async #start(spec, ref, commands, want, signal) {
|
|
688
887
|
const closed = [];
|
|
689
888
|
if (this.host.facts.daemon === "child") {
|
|
690
|
-
// The
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
closed.push(
|
|
699
|
-
}
|
|
889
|
+
// The session buses in /tmp, closed to the computer's user before any program of its runs.
|
|
890
|
+
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}` });
|
|
891
|
+
if (!shut.ok)
|
|
892
|
+
return shut;
|
|
893
|
+
if (shut.value.exitCode !== 0)
|
|
894
|
+
return this.#fail("refused", `closing the session buses in /tmp to the computer's user failed (exit ${shut.value.exitCode}): ${`${shut.value.stdout} ${shut.value.stderr}`.trim().slice(0, 300)}`);
|
|
895
|
+
const found = closedSocketsOf(shut.value.stdout);
|
|
896
|
+
if (found.length > 0)
|
|
897
|
+
closed.push(`session buses in /tmp closed to the computer's user: ${found.join(", ")}`);
|
|
700
898
|
const init = await runToExit(this.host, ref.id, commands.start, { ms: this.#timeouts.execMs, ...(signal ? { signal } : {}), what: `starting the daemon in ${ref.name}` });
|
|
701
899
|
if (!init.ok)
|
|
702
900
|
return init;
|
|
@@ -714,7 +912,7 @@ export class HostedProvider {
|
|
|
714
912
|
try {
|
|
715
913
|
const archil = spec.mount?.kind === "archil" ? { mount: { kind: "archil", disk: spec.mount.disk, region: spec.mount.region, folder: spec.mount.folder }, epoch: claimEpoch(spec.labels) } : undefined;
|
|
716
914
|
const expected = archil === undefined ? undefined : { ...archil, expected: readBackOf({ ...archil.mount, token: "" }, archil.epoch) };
|
|
717
|
-
const inside = await this.#checkInside(opened.value, ref.name, spec.network, true, want, signal, expected);
|
|
915
|
+
const inside = await this.#checkInside(opened.value, ref.name, networkOfForm(formOf(spec.network)), true, want, signal, expected);
|
|
718
916
|
if (!inside.ok)
|
|
719
917
|
return inside;
|
|
720
918
|
notes = inside.value;
|
|
@@ -762,19 +960,17 @@ export class HostedProvider {
|
|
|
762
960
|
const drift = [...found.value];
|
|
763
961
|
const notes = [];
|
|
764
962
|
let user;
|
|
765
|
-
if (network === "
|
|
766
|
-
const
|
|
767
|
-
if (!
|
|
768
|
-
return
|
|
769
|
-
user = `${
|
|
770
|
-
drift.push(...
|
|
771
|
-
|
|
772
|
-
drift.push(...runtimeDrift(want.runtime, probe.value.kernel));
|
|
773
|
-
notes.push(...probeNotes(probe.value, network));
|
|
963
|
+
if (typeof network === "string" || !("other" in network)) {
|
|
964
|
+
const read = await this.#networkDrift(current, name, network, signal);
|
|
965
|
+
if (!read.ok)
|
|
966
|
+
return read;
|
|
967
|
+
user = `${read.value.probe.uid}:${read.value.probe.gid}`;
|
|
968
|
+
drift.push(...read.value.drift, ...runtimeDrift(want, read.value.probe.kernel));
|
|
969
|
+
notes.push(...probeNotes(read.value.probe, network));
|
|
774
970
|
}
|
|
775
971
|
// Which way init fenced its programs, as hello read it back: said once a way other than the plain one was taken.
|
|
776
972
|
if (starting && current.hello.fence === "userns-netns") {
|
|
777
|
-
notes.push(`fence: userns-netns: init made its programs' network namespace inside a user namespace of its own, as root on ${want.
|
|
973
|
+
notes.push(`fence: userns-netns: init made its programs' network namespace inside a user namespace of its own, as root on ${want.isolation ?? "this machine"} may not make one itself`);
|
|
778
974
|
}
|
|
779
975
|
const said = user === undefined ? {} : { user };
|
|
780
976
|
if (archil === undefined)
|
|
@@ -820,10 +1016,44 @@ export class HostedProvider {
|
|
|
820
1016
|
await pause(LISTEN_RETRY_MS, signal);
|
|
821
1017
|
}
|
|
822
1018
|
}
|
|
823
|
-
/**
|
|
824
|
-
|
|
1019
|
+
/**
|
|
1020
|
+
* Its tools' network as they meet it, against the one create made: the probe, as the computer's own user. Under none,
|
|
1021
|
+
* no target answers. Under open or a list, still none answers directly (their own loopback alone), init's relay
|
|
1022
|
+
* answers (the one way out, to the proxy), and the proxy's policy beside its socket holds the form create asked for.
|
|
1023
|
+
*/
|
|
1024
|
+
async #networkDrift(connection, name, network, signal) {
|
|
1025
|
+
if (network === "none") {
|
|
1026
|
+
const probe = await this.#probe(connection, name, signal);
|
|
1027
|
+
return probe.ok ? ok({ drift: probeNetworkDrift(probe.value), probe: probe.value }) : probe;
|
|
1028
|
+
}
|
|
1029
|
+
const probe = await this.#probe(connection, name, signal, [...this.#probeTargets, EGRESS_RELAY]);
|
|
1030
|
+
if (!probe.ok)
|
|
1031
|
+
return probe;
|
|
1032
|
+
const form = formOf(network);
|
|
1033
|
+
const drift = [];
|
|
1034
|
+
const direct = probe.value.reached.filter((target) => target !== EGRESS_RELAY);
|
|
1035
|
+
if (direct.length > 0)
|
|
1036
|
+
drift.push(`network: declared ${form} through the egress proxy alone, read back a direct answer from ${direct.join(", ")}`);
|
|
1037
|
+
if (!probe.value.reached.includes(EGRESS_RELAY))
|
|
1038
|
+
drift.push(`network: declared ${form} through the egress proxy, and init's relay at ${EGRESS_RELAY} did not answer from inside`);
|
|
1039
|
+
const { helloMs } = this.#timeouts;
|
|
1040
|
+
const policy = await connection.client.call("files.read", { path: EGRESS_POLICY }, { signal: bounded(helloMs, signal) });
|
|
1041
|
+
if (!policy.ok) {
|
|
1042
|
+
if (policy.error.code === "aborted")
|
|
1043
|
+
return signal?.aborted ? this.#fail("aborted", `reading ${EGRESS_POLICY} in ${name} was aborted`) : this.#fail("timeout", `reading ${EGRESS_POLICY} in ${name} did not answer within ${seconds(helloMs)}`);
|
|
1044
|
+
drift.push(`network: declared ${form}, and the egress proxy's policy ${EGRESS_POLICY} could not be read: ${policy.error.message}`);
|
|
1045
|
+
}
|
|
1046
|
+
else {
|
|
1047
|
+
const served = Buffer.from(policy.value.data, "base64").toString("utf8");
|
|
1048
|
+
if (served !== `${form}\n`)
|
|
1049
|
+
drift.push(`network: declared ${form}, the egress proxy serves ${show(served.trim().slice(0, 300))}`);
|
|
1050
|
+
}
|
|
1051
|
+
return ok({ drift, probe: probe.value });
|
|
1052
|
+
}
|
|
1053
|
+
/** `computerd probe` through the daemon, so it runs as the computer's own user: to targets, the configured ones by default. */
|
|
1054
|
+
async #probe(connection, name, signal, targets = this.#probeTargets) {
|
|
825
1055
|
const { probeMs } = this.#timeouts;
|
|
826
|
-
const argv = [DAEMON_IN_COMPUTER, "probe", ...
|
|
1056
|
+
const argv = [DAEMON_IN_COMPUTER, "probe", ...targets.flatMap((target) => ["--connect", target])];
|
|
827
1057
|
const ran = await connection.client.call("exec.run", { argv, timeoutMs: probeMs }, { signal: bounded(probeMs + CALL_SLACK_MS, signal) });
|
|
828
1058
|
if (!ran.ok) {
|
|
829
1059
|
if (signal?.aborted)
|
|
@@ -843,12 +1073,13 @@ export class HostedProvider {
|
|
|
843
1073
|
/**
|
|
844
1074
|
* Create waits until the listing shows the new computer on two consecutive polls at least listedStableMs apart (and on
|
|
845
1075
|
* every poll between), so list() shows it once create returns: a listing that showed it once and then not starts over.
|
|
1076
|
+
* The polls are 150 ms apart at first, the pause doubling up to pollMs, so a vendor slow to list costs a few calls.
|
|
846
1077
|
*/
|
|
847
1078
|
async #waitListed(ref, signal) {
|
|
848
1079
|
const { listedMs, listMs, pollMs, listedStableMs } = this.#timeouts;
|
|
849
1080
|
const until = Date.now() + listedMs;
|
|
850
1081
|
let since;
|
|
851
|
-
for (let hits = 0;;) {
|
|
1082
|
+
for (let hits = 0, delay = Math.min(LISTED_POLL_MS, pollMs);; delay = Math.min(delay * 2, pollMs)) {
|
|
852
1083
|
const listed = await listedBy(this.host, "the listing by name", byName(ref.name), listMs, signal);
|
|
853
1084
|
if (!listed.ok)
|
|
854
1085
|
return listed;
|
|
@@ -862,14 +1093,14 @@ export class HostedProvider {
|
|
|
862
1093
|
return ok(undefined);
|
|
863
1094
|
if (Date.now() >= until)
|
|
864
1095
|
return this.#fail("timeout", `the computer was not listed within ${seconds(listedMs)} of its creation`);
|
|
865
|
-
await pause(Math.min(
|
|
1096
|
+
await pause(Math.min(delay, until - Date.now()), signal);
|
|
866
1097
|
if (signal?.aborted)
|
|
867
1098
|
return this.#fail("aborted", "create was aborted while it waited for the listing");
|
|
868
1099
|
}
|
|
869
1100
|
}
|
|
870
1101
|
/** This provider's machine for ref, as the vendor reads it, or why not. */
|
|
871
|
-
async #own(ref, signal) {
|
|
872
|
-
const found = await this.#get(ref.id, signal);
|
|
1102
|
+
async #own(ref, signal, fresh = false) {
|
|
1103
|
+
const found = await this.#get(ref.id, signal, fresh);
|
|
873
1104
|
if (!found.ok)
|
|
874
1105
|
return found;
|
|
875
1106
|
if (found.value === undefined)
|
|
@@ -959,8 +1190,9 @@ export class HostedProvider {
|
|
|
959
1190
|
if (deadline === undefined)
|
|
960
1191
|
found.push(`deadline: the label ${DEADLINE_LABEL} reads ${show(machine.labels[DEADLINE_LABEL])}, not the ISO time create writes`);
|
|
961
1192
|
const archil = recordedArchil(machine.labels);
|
|
962
|
-
// Its tools' network
|
|
963
|
-
|
|
1193
|
+
// Its tools' network as create recorded it, read from inside (#checkInside: the probe, and the proxy's policy):
|
|
1194
|
+
// no vendor's read of it is taken.
|
|
1195
|
+
const network = networkOfForm(machine.labels[NETWORK_LABEL]);
|
|
964
1196
|
const image = machine.labels[IMAGE_LABEL] ?? "";
|
|
965
1197
|
const spec = { image, lifetimeMs: 1, network, labels: callerLabels(machine.labels) };
|
|
966
1198
|
found.push(...this.check(spec).map(({ field, message }) => `${field}: ${message}, read back from the computer`));
|
|
@@ -975,7 +1207,7 @@ export class HostedProvider {
|
|
|
975
1207
|
try {
|
|
976
1208
|
// A host that takes a spec's user made this computer's daemon run as the one its spec named: read back, never root
|
|
977
1209
|
// (an Archil computer's is the tool user); every other runs as the provider's daemon user.
|
|
978
|
-
const want = this.#takesUser
|
|
1210
|
+
const want = this.#takesUser ? (archil === undefined ? {} : { user: ARCHIL_USER }) : { user: `${this.#uid}:${this.#gid}` };
|
|
979
1211
|
const inside = await this.#checkInside(opened.value, name, network, false, want, signal, archil);
|
|
980
1212
|
if (!inside.ok && inside.error.code !== "refused")
|
|
981
1213
|
return inside;
|
|
@@ -996,17 +1228,18 @@ export class HostedProvider {
|
|
|
996
1228
|
found.push(`mount: ${name} is ${machine.state}, so its mount cannot be read back from inside`);
|
|
997
1229
|
if (found.length > 0)
|
|
998
1230
|
return this.#fail("refused", `the computer ${name} does not match this provider, so attach refuses it and it is left as it is: ${found.join("; ")}`);
|
|
1231
|
+
this.#keepAlive(ref, true);
|
|
999
1232
|
return ok({
|
|
1000
1233
|
...ref,
|
|
1001
1234
|
spec: {
|
|
1002
1235
|
image,
|
|
1003
|
-
user: (this.#takesUser
|
|
1236
|
+
user: (this.#takesUser ? user : undefined) ?? `${this.#uid}:${this.#gid}`,
|
|
1004
1237
|
network,
|
|
1005
1238
|
workdir: WORK,
|
|
1006
1239
|
...(deadline === undefined ? {} : { deadline }),
|
|
1007
1240
|
...(kind === undefined ? {} : { kind }),
|
|
1008
1241
|
...(mount === undefined ? {} : { mount }),
|
|
1009
|
-
...(machine.
|
|
1242
|
+
...gpuOf(machine.labels[GPU_LABEL]),
|
|
1010
1243
|
},
|
|
1011
1244
|
...(notes.length > 0 ? { notes } : {}),
|
|
1012
1245
|
});
|
|
@@ -1017,12 +1250,14 @@ export class HostedProvider {
|
|
|
1017
1250
|
*/
|
|
1018
1251
|
async testConnection({ signal } = {}) {
|
|
1019
1252
|
const listed = await listedBy(this.host, "the listing", { [COMPUTER_LABEL]: "1" }, this.#timeouts.listMs, signal);
|
|
1020
|
-
|
|
1253
|
+
// A host that keeps no labels lists them by their name alone.
|
|
1254
|
+
const counted = (machine) => (this.#keeps ? NAME.test(machine.name ?? "") : ours(machine.labels)) && alive(machine.state);
|
|
1255
|
+
return countTested(this.#label, listed.ok ? ok({ count: listed.value.filter(counted).length }) : listed);
|
|
1021
1256
|
}
|
|
1022
1257
|
/**
|
|
1023
1258
|
* Every computer this provider made that carries all of these labels, running or stopped: the listing finds them, and
|
|
1024
|
-
* a get of each reads its state, labels and end (foundBy). Its network is
|
|
1025
|
-
* it from inside.
|
|
1259
|
+
* a get of each reads its state, labels and end (foundBy). Its network is the one its label records, as create made
|
|
1260
|
+
* it (none without one); attach and network() read it from inside.
|
|
1026
1261
|
*/
|
|
1027
1262
|
async list(labels, { signal } = {}) {
|
|
1028
1263
|
const problem = filterProblem(labels);
|
|
@@ -1043,52 +1278,52 @@ export class HostedProvider {
|
|
|
1043
1278
|
labels: { ...machine.labels },
|
|
1044
1279
|
deadline: deadlineOf(machine),
|
|
1045
1280
|
state: machine.state,
|
|
1046
|
-
network:
|
|
1281
|
+
network: networkOfForm(machine.labels[NETWORK_LABEL]),
|
|
1047
1282
|
...(archil === undefined ? {} : { mount: archil.expected }),
|
|
1048
1283
|
});
|
|
1049
1284
|
}
|
|
1050
1285
|
return ok(infos);
|
|
1051
1286
|
}
|
|
1052
|
-
/** The probe
|
|
1287
|
+
/** The probe and the proxy's policy read the network back from inside. A change is refused: it is fixed at create. */
|
|
1053
1288
|
async network(ref, policy, { signal } = {}) {
|
|
1054
1289
|
const problem = this.#refProblem(ref);
|
|
1055
1290
|
if (problem)
|
|
1056
1291
|
return this.#fail("invalid", problem);
|
|
1057
1292
|
const fixed = `${a(this.#label)} computer's network is fixed at create: create a new computer`;
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
return this.#fail("refused",
|
|
1061
|
-
}
|
|
1293
|
+
const refused = policy === undefined ? undefined : networkRefusal(this.#label, this.capabilities().network, policy);
|
|
1294
|
+
if (refused)
|
|
1295
|
+
return this.#fail("refused", `${refused}; ${fixed}`);
|
|
1062
1296
|
const found = await this.#own(ref, signal);
|
|
1063
1297
|
if (!found.ok)
|
|
1064
1298
|
return found;
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
//
|
|
1068
|
-
//
|
|
1069
|
-
|
|
1299
|
+
const recorded = networkOfForm(found.value.labels[NETWORK_LABEL]);
|
|
1300
|
+
let read = recorded;
|
|
1301
|
+
// Its tools' network is read from inside, as the tool user, whatever the vendor's network is (an Archil computer's
|
|
1302
|
+
// root reaches Archil, an open one's proxy the internet): the daemon's own network namespace's live interfaces are
|
|
1303
|
+
// lo alone, none can be raised (computerd probe), no probe target answers directly, and an open or listed one's
|
|
1304
|
+
// relay and proxy serve what its label records.
|
|
1305
|
+
if (found.value.state === "running" && (typeof recorded === "string" || !("other" in recorded))) {
|
|
1070
1306
|
const opened = await this.#open(ref, signal);
|
|
1071
1307
|
if (!opened.ok)
|
|
1072
1308
|
return opened;
|
|
1073
1309
|
try {
|
|
1074
|
-
const
|
|
1310
|
+
const own = await readOwnNetwork(opened.value.client, this.#timeouts.helloMs, bounded(this.#timeouts.helloMs, signal));
|
|
1311
|
+
if (!own.ok)
|
|
1312
|
+
return this.#fail(own.error.code, `${ref.name}: ${own.error.message}`);
|
|
1313
|
+
const drift = await this.#networkDrift(opened.value, ref.name, recorded, signal);
|
|
1075
1314
|
if (!drift.ok)
|
|
1076
|
-
return
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
if (!probe.ok)
|
|
1081
|
-
return probe;
|
|
1082
|
-
if (probe.value.reached.length > 0)
|
|
1083
|
-
read = { other: `its tools' network was created none, yet from inside an answer came back from ${probe.value.reached.join(", ")}` };
|
|
1315
|
+
return drift;
|
|
1316
|
+
const found = [...own.value, ...drift.value.drift];
|
|
1317
|
+
if (found.length > 0)
|
|
1318
|
+
read = { other: `its tools' network was created ${policyWords(recorded)}, yet from inside: ${found.join("; ")}` };
|
|
1084
1319
|
}
|
|
1085
1320
|
finally {
|
|
1086
1321
|
await opened.value.close();
|
|
1087
1322
|
}
|
|
1088
1323
|
}
|
|
1089
|
-
if (policy === undefined || policy
|
|
1324
|
+
if (policy === undefined || isDeepStrictEqual(networkOfForm(formOf(policy)), read))
|
|
1090
1325
|
return ok(read);
|
|
1091
|
-
return this.#fail("refused", `${fixed}: ${ref.name} reads back network ${policyWords(read)}, not ${policy}`);
|
|
1326
|
+
return this.#fail("refused", `${fixed}: ${ref.name} reads back network ${policyWords(read)}, not ${policyWords(policy)}`);
|
|
1092
1327
|
}
|
|
1093
1328
|
/** One extend of a computer at a time, each reading the deadline the one before it left. */
|
|
1094
1329
|
async #extendSerial(ref, ttlMs, signal) {
|
|
@@ -1111,11 +1346,12 @@ export class HostedProvider {
|
|
|
1111
1346
|
}
|
|
1112
1347
|
/**
|
|
1113
1348
|
* Moves the deadline to now + ttlMs, refusing one earlier than it is (before any vendor call), never past create's time
|
|
1114
|
-
* plus the cap (init's maximum): the host's extend first (its timer or its deadline label),
|
|
1115
|
-
* (get, deadlineOf), then init's own deadline, to that end. The deadline returned is the one init took.
|
|
1349
|
+
* plus the cap (init's maximum): the host's extend first (its timer or its deadline label), and the label file where the
|
|
1350
|
+
* labels are kept in the machine, then the end it reads back (get, deadlineOf), then init's own deadline, to that end. The deadline returned is the one init took.
|
|
1116
1351
|
*/
|
|
1117
1352
|
async #extend(ref, ttlMs, signal) {
|
|
1118
|
-
|
|
1353
|
+
// Labels kept in the machine are read now: another instance may have moved the deadline.
|
|
1354
|
+
const found = await this.#own(ref, signal, true);
|
|
1119
1355
|
if (!found.ok)
|
|
1120
1356
|
return found;
|
|
1121
1357
|
const machine = found.value;
|
|
@@ -1140,12 +1376,17 @@ export class HostedProvider {
|
|
|
1140
1376
|
return this.#fail("unsupported", `${a(this.#label)} computer's deadline cannot move`);
|
|
1141
1377
|
const moved = await this.#call(`${this.#label}'s end of ${ref.name}`, (ctx) => extend(ref.id, new Date(target), ctx), this.#timeouts.requestMs, signal);
|
|
1142
1378
|
if (!moved.ok)
|
|
1143
|
-
return moved.error.code === "limit" ? moved : this.#fail(moved.error.code, `${this.#label}
|
|
1379
|
+
return moved.error.code === "limit" ? moved : this.#fail(moved.error.code, `${this.#label} did not move ${ref.name}'s end, so nothing did: ${moved.error.message}`);
|
|
1380
|
+
if (this.#keeps) {
|
|
1381
|
+
const kept = await this.#keep(ref, { ...machine.labels, [DEADLINE_LABEL]: new Date(target).toISOString() }, signal);
|
|
1382
|
+
if (!kept.ok)
|
|
1383
|
+
return this.#fail(kept.error.code, `${ref.name}'s deadline label did not move, nor did init's deadline: ${kept.error.message}`);
|
|
1384
|
+
}
|
|
1144
1385
|
const after = await this.#get(ref.id, signal);
|
|
1145
1386
|
const end = after.ok && after.value !== undefined ? deadlineOf(after.value) : undefined;
|
|
1146
1387
|
if (end === undefined) {
|
|
1147
|
-
const why = after.ok ? `${ref.name}
|
|
1148
|
-
return this.#fail(after.ok ? "gone" :
|
|
1388
|
+
const why = !after.ok ? after.error.message : after.value === undefined ? `${ref.name} is gone` : `${ref.name} reads back no deadline`;
|
|
1389
|
+
return this.#fail(!after.ok ? after.error.code : after.value === undefined ? "gone" : "refused", `${this.#label} was asked to move ${ref.name}'s end, and reading it back failed: ${why}; init's deadline did not move`);
|
|
1149
1390
|
}
|
|
1150
1391
|
const opened = await this.#open(ref, signal);
|
|
1151
1392
|
if (!opened.ok)
|
|
@@ -1186,8 +1427,10 @@ export class HostedProvider {
|
|
|
1186
1427
|
const found = await this.#get(ref.id);
|
|
1187
1428
|
if (!found.ok)
|
|
1188
1429
|
return found;
|
|
1189
|
-
if (found.value === undefined)
|
|
1430
|
+
if (found.value === undefined) {
|
|
1431
|
+
this.#unkeep(ref.id);
|
|
1190
1432
|
return ok(undefined);
|
|
1433
|
+
}
|
|
1191
1434
|
if (!ours(found.value.labels, ref.name))
|
|
1192
1435
|
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`);
|
|
1193
1436
|
this.#end(ref.name);
|
|
@@ -1232,29 +1475,82 @@ export class HostedProvider {
|
|
|
1232
1475
|
const byName = (name) => ({ [COMPUTER_LABEL]: "1", [NAME_LABEL]: name });
|
|
1233
1476
|
/**
|
|
1234
1477
|
* The host's listing by labels, bounded by ms: only the machines that carry every one of them, since a vendor's listing
|
|
1235
|
-
* may answer more (MachineHost.list).
|
|
1478
|
+
* may answer more (MachineHost.list). A host that keeps labels in the machine lists none: its machines of another name
|
|
1479
|
+
* are left out, and the rest kept for foundBy to read.
|
|
1236
1480
|
*/
|
|
1237
1481
|
export async function listedBy(host, what, labels, ms, signal) {
|
|
1238
1482
|
const listed = await hostCall(host, what, (ctx) => host.list(labels, ctx), ms, signal);
|
|
1239
|
-
|
|
1483
|
+
const name = labels[NAME_LABEL];
|
|
1484
|
+
const keeps = (machine) => (host.facts.labels === "machine" ? name === undefined || machine.name === undefined || machine.name === name : carries(machine, labels));
|
|
1485
|
+
return listed.ok ? ok(listed.value.filter(keeps)) : listed;
|
|
1486
|
+
}
|
|
1487
|
+
/** With labels kept in the machine: each machine's label map as its file was last read, by host and machine id. */
|
|
1488
|
+
const filed = new WeakMap();
|
|
1489
|
+
function filedOf(host) {
|
|
1490
|
+
let known = filed.get(host);
|
|
1491
|
+
if (known === undefined)
|
|
1492
|
+
filed.set(host, (known = new Map()));
|
|
1493
|
+
return known;
|
|
1494
|
+
}
|
|
1495
|
+
/** A label file's JSON as a label map, or undefined where it is not one. */
|
|
1496
|
+
function labelMapOf(text) {
|
|
1497
|
+
try {
|
|
1498
|
+
const map = JSON.parse(text);
|
|
1499
|
+
return typeof map === "object" && map !== null && !Array.isArray(map) && Object.values(map).every((value) => typeof value === "string") ? map : undefined;
|
|
1500
|
+
}
|
|
1501
|
+
catch {
|
|
1502
|
+
return undefined;
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
/**
|
|
1506
|
+
* A machine of a host that keeps labels in the machine, with the labels its file holds: read through exec while it runs
|
|
1507
|
+
* (fresh), else as last read, or none. No file (exit 3) is none; a read that fails otherwise is an error, never none, so
|
|
1508
|
+
* a listing never leaves out a computer it could not read. Another host's machine is as read.
|
|
1509
|
+
*/
|
|
1510
|
+
async function labelled(host, machine, ms, signal, fresh) {
|
|
1511
|
+
if (host.facts.labels !== "machine" || machine === undefined)
|
|
1512
|
+
return ok(machine);
|
|
1513
|
+
const known = filedOf(host);
|
|
1514
|
+
const held = known.get(machine.id);
|
|
1515
|
+
if (machine.state !== "running" || (held !== undefined && !fresh))
|
|
1516
|
+
return ok({ ...machine, labels: { ...held } });
|
|
1517
|
+
const what = `reading the labels in ${machine.id}`;
|
|
1518
|
+
const read = await runToExit(host, machine.id, labelsCommand(), { ms, ...(signal ? { signal } : {}), what });
|
|
1519
|
+
// Gone since the vendor's read: as a read that finds none.
|
|
1520
|
+
if (!read.ok)
|
|
1521
|
+
return read.error.code === "not_found" ? ok(undefined) : read;
|
|
1522
|
+
if (read.value.exitCode === 3)
|
|
1523
|
+
return ok({ ...machine, labels: {} });
|
|
1524
|
+
const labels = read.value.exitCode === 0 ? labelMapOf(read.value.stdout) : undefined;
|
|
1525
|
+
if (labels === undefined)
|
|
1526
|
+
return failFor(host.descriptor.id)("vendor", `${what} failed (exit ${read.value.exitCode}): ${`${read.value.stdout} ${read.value.stderr}`.trim().slice(0, 300)}`);
|
|
1527
|
+
known.set(machine.id, labels);
|
|
1528
|
+
return ok({ ...machine, labels: { ...labels } });
|
|
1240
1529
|
}
|
|
1241
1530
|
/** Whether a machine carries every one of these labels. */
|
|
1242
1531
|
const carries = (machine, labels) => Object.entries(labels).every(([key, value]) => machine.labels?.[key] === value);
|
|
1243
1532
|
/**
|
|
1244
1533
|
* The listing finds, get answers (MachineHost.list): the machines the host's listing by labels names, each read now by
|
|
1245
|
-
* readMachine, within requestMs
|
|
1246
|
-
* not every label
|
|
1534
|
+
* readMachine, within requestMs, and its label file where the host keeps labels in the machine. One the vendor no longer
|
|
1535
|
+
* has (not found) is dropped, and so is one whose get carries not every label, but a machine of the name asked for whose
|
|
1536
|
+
* labels are not written yet. A read that fails otherwise fails the whole.
|
|
1247
1537
|
*/
|
|
1248
1538
|
export async function foundBy(host, what, labels, { listMs, requestMs }, signal) {
|
|
1249
1539
|
const listed = await listedBy(host, what, labels, listMs, signal);
|
|
1250
1540
|
if (!listed.ok)
|
|
1251
1541
|
return listed;
|
|
1252
|
-
const reads = await Promise.all(listed.value.map(({ id }) =>
|
|
1542
|
+
const reads = await Promise.all(listed.value.map(async ({ id }) => {
|
|
1543
|
+
const read = await readMachine(host, id, requestMs, signal);
|
|
1544
|
+
return read.ok ? labelled(host, read.value, requestMs, signal, true) : read;
|
|
1545
|
+
}));
|
|
1546
|
+
// With labels in the machine, one of the name asked for with none written yet is kept too, for the caller to judge.
|
|
1547
|
+
const name = labels[NAME_LABEL];
|
|
1548
|
+
const unfiled = (machine) => host.facts.labels === "machine" && name !== undefined && machine.name === name && machine.labels[COMPUTER_LABEL] === undefined;
|
|
1253
1549
|
const found = [];
|
|
1254
1550
|
for (const read of reads) {
|
|
1255
1551
|
if (!read.ok)
|
|
1256
1552
|
return read;
|
|
1257
|
-
if (read.value !== undefined && carries(read.value, labels))
|
|
1553
|
+
if (read.value !== undefined && (carries(read.value, labels) || unfiled(read.value)))
|
|
1258
1554
|
found.push(read.value);
|
|
1259
1555
|
}
|
|
1260
1556
|
return ok(found);
|
|
@@ -1387,13 +1683,13 @@ function oldestFirst(machines) {
|
|
|
1387
1683
|
const byId = (a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
1388
1684
|
return (a, b) => (dated && at(a) !== at(b) ? at(a) - at(b) : byId(a, b));
|
|
1389
1685
|
}
|
|
1390
|
-
/** The probe's kernel against the
|
|
1391
|
-
function runtimeDrift(
|
|
1686
|
+
/** The probe's kernel against the isolation declared: gVisor exactly where it says gvisor, never where it says vm. */
|
|
1687
|
+
function runtimeDrift({ isolation, gpu }, kernel) {
|
|
1392
1688
|
const gvisor = /gvisor/i.test(kernel);
|
|
1393
|
-
if (
|
|
1689
|
+
if (isolation === "vm" && gvisor)
|
|
1394
1690
|
return [`runtime: declared vm, read back gVisor (the kernel reports ${JSON.stringify(kernel)})`];
|
|
1395
|
-
if (
|
|
1396
|
-
return [`runtime: declared gVisor (a GPU computer), read back another kernel (${JSON.stringify(kernel)})`];
|
|
1691
|
+
if (isolation === "gvisor" && !gvisor)
|
|
1692
|
+
return [`runtime: declared gVisor${gpu ? " (a GPU computer)" : ""}, read back another kernel (${JSON.stringify(kernel)})`];
|
|
1397
1693
|
return [];
|
|
1398
1694
|
}
|
|
1399
1695
|
function labelProblem(key, value) {
|
|
@@ -1413,3 +1709,12 @@ function labelProblem(key, value) {
|
|
|
1413
1709
|
function a(label) {
|
|
1414
1710
|
return `${/^[AEIOU]/i.test(label) ? "an" : "a"} ${label}`;
|
|
1415
1711
|
}
|
|
1712
|
+
/** A spec's network as init's form (networkFormOf): "" for none, and for a network check() refuses. */
|
|
1713
|
+
function formOf(network) {
|
|
1714
|
+
try {
|
|
1715
|
+
return networkFormOf(network);
|
|
1716
|
+
}
|
|
1717
|
+
catch {
|
|
1718
|
+
return "";
|
|
1719
|
+
}
|
|
1720
|
+
}
|