@parcha/agentrun-computer 0.1.0-beta.37 → 0.1.0-beta.39
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 +341 -260
- package/box/Dockerfile +18 -1
- package/box/README.md +14 -0
- package/box/build-daytona.py +2 -2
- package/box/build-e2b.mjs +41 -18
- package/box/recipe.d.mts +5 -0
- package/box/recipe.mjs +43 -0
- package/dist/agent37.d.ts +18 -66
- package/dist/agent37.js +187 -464
- package/dist/box-image.d.ts +14 -0
- package/dist/box-image.js +24 -0
- package/dist/browserbase.d.ts +0 -6
- package/dist/browserbase.js +1 -5
- package/dist/client.d.ts +12 -3
- package/dist/client.js +39 -4
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/computerd.json +4 -0
- 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 +6 -0
- package/dist/conformance/cases/apps.js +18 -5
- package/dist/conformance/cases/isolation.d.ts +22 -5
- package/dist/conformance/cases/isolation.js +97 -16
- package/dist/conformance/cases/mount.js +5 -3
- package/dist/conformance/cases/provider.js +41 -0
- package/dist/conformance/cli.js +72 -11
- package/dist/conformance/daytona.d.ts +9 -20
- package/dist/conformance/daytona.js +12 -21
- package/dist/conformance/e2b.js +9 -4
- package/dist/conformance/exec-stream.d.ts +52 -0
- package/dist/conformance/exec-stream.js +493 -0
- package/dist/conformance/kernel.js +2 -2
- package/dist/conformance/modal.js +1 -1
- package/dist/conformance/orgo.js +2 -2
- package/dist/conformance/sweep.js +8 -8
- package/dist/conformance/types.d.ts +2 -2
- package/dist/create-requests.d.ts +1 -1
- package/dist/daytona.d.ts +17 -133
- package/dist/daytona.js +396 -1452
- package/dist/descriptor.d.ts +11 -4
- package/dist/descriptor.js +7 -0
- package/dist/descriptors/agent37.d.ts +3 -11
- package/dist/descriptors/agent37.js +9 -10
- package/dist/descriptors/daytona.d.ts +7 -6
- package/dist/descriptors/daytona.js +14 -7
- 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 +25 -24
- 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/descriptors/shared.d.ts +1 -1
- package/dist/descriptors/shared.js +2 -2
- package/dist/desktop.d.ts +18 -4
- package/dist/desktop.js +89 -9
- 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 -11
- package/dist/docker.js +13 -16
- package/dist/drift.js +4 -4
- package/dist/e2b.d.ts +22 -130
- package/dist/e2b.js +163 -243
- package/dist/host.d.ts +136 -104
- package/dist/host.js +8 -1
- package/dist/hosted/network.d.ts +23 -0
- package/dist/hosted/network.js +77 -0
- package/dist/hosted/process.d.ts +24 -8
- package/dist/hosted/process.js +80 -44
- package/dist/hosted/provider.d.ts +38 -22
- package/dist/hosted/provider.js +500 -341
- package/dist/hosted/rest.d.ts +74 -0
- package/dist/hosted/rest.js +126 -0
- package/dist/hosted/scripts.d.ts +21 -3
- package/dist/hosted/scripts.js +76 -14
- package/dist/hosted/ssh-exec.d.ts +99 -0
- package/dist/hosted/ssh-exec.js +464 -0
- package/dist/hosted/values.d.ts +26 -0
- package/dist/hosted/values.js +63 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/isolation.d.ts +0 -2
- package/dist/isolation.js +3 -4
- package/dist/kernel.d.ts +0 -6
- package/dist/kernel.js +1 -5
- package/dist/kinds.d.ts +3 -1
- package/dist/local-chrome.d.ts +0 -6
- package/dist/local-chrome.js +1 -5
- package/dist/modal.d.ts +15 -86
- package/dist/modal.js +67 -185
- package/dist/mount-archil.d.ts +13 -8
- package/dist/mount-archil.js +27 -17
- package/dist/mount.d.ts +10 -4
- package/dist/mount.js +8 -4
- package/dist/orgo.d.ts +0 -9
- package/dist/orgo.js +8 -13
- package/dist/phone-harness.d.ts +0 -6
- package/dist/phone-harness.js +0 -4
- package/dist/protocol.d.ts +4 -1
- package/dist/protocol.js +1 -0
- package/dist/provider.d.ts +2 -2
- package/dist/providers.d.ts +15 -13
- package/dist/screen-watch.js +34 -6
- package/dist/surface/api.d.ts +14 -11
- package/dist/surface/core.d.ts +23 -5
- package/dist/surface/core.js +105 -15
- package/dist/surface/index.d.ts +1 -1
- package/dist/surface/index.js +1 -1
- package/dist/surface/outline.d.ts +2 -0
- package/dist/surface/outline.js +2 -1
- package/dist/surface/script.d.ts +4 -0
- package/dist/surface/script.js +11 -3
- package/dist/ui-engine.js +19 -3
- package/package.json +2 -3
- package/box/build-agent37.mjs +0 -177
- package/dist/conformance/daytona-sweep.d.ts +0 -18
- package/dist/conformance/daytona-sweep.js +0 -126
package/dist/hosted/provider.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// The shared machine-host adapter:
|
|
1
|
+
// The shared machine-host adapter: new HostedProvider(host, options) is a whole ComputerProvider over a vendor's
|
|
2
2
|
// MachineHost (../host.ts). The vendor answers create, get, list, delete and exec about its own machines; everything a
|
|
3
3
|
// computer is beyond that is here, once, for every vendor: the spec's check, the computer's name from its create's
|
|
4
4
|
// request id (one create per request: a retry finds the first), the daemon's upload, install and sha256 read-back,
|
|
@@ -10,27 +10,32 @@ 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 { COMPUTER_LABEL, DAEMON_IN_COMPUTER, DEADLINE_LABEL, helloDrift, probeNetworkDrift, probeNotes, probeReportOf, WORK } from "../drift.js";
|
|
16
|
-
import {
|
|
15
|
+
import { desktopOf, startDisplay } from "../desktop.js";
|
|
16
|
+
import { COMPUTER_LABEL, DAEMON_IN_COMPUTER, DEADLINE_LABEL, helloDrift, probeNetworkDrift, probeNotes, probeReportOf, show, WORK } from "../drift.js";
|
|
17
|
+
import { readOwnNetwork, TOOL_USER } from "../isolation.js";
|
|
17
18
|
import { releaseOlder } from "../janitor.js";
|
|
18
19
|
import { RUN_LABEL } from "../labels.js";
|
|
19
20
|
import { checkMount, readBackOf } from "../mount.js";
|
|
20
21
|
import { ARCHIL_MOUNT_LABEL, ARCHIL_TOOL_UID, archilEgressOf, archilLabels, archilMount, archilRefusals, archilRelease, claimEpoch, mountDrift, readArchilMount, recordedArchil as recordedArchilOf, recordedMount, syncingClient, } from "../mount-archil.js";
|
|
21
22
|
import { checkGpu, checkTenure, runLifetimeMs, } from "../provider.js";
|
|
22
|
-
import { ok } from "../result.js";
|
|
23
|
+
import { failFor, ok } from "../result.js";
|
|
23
24
|
import { daemonWatch } from "../screen-watch.js";
|
|
24
25
|
import { countTested } from "../test-connection.js";
|
|
25
26
|
import { waitUntil } from "../wait.js";
|
|
26
|
-
import { bounded, CALL_SLACK_MS,
|
|
27
|
-
import {
|
|
27
|
+
import { bounded, CALL_SLACK_MS, hostCall, openBridge, pause, rootOf, runToExit, seconds, uploadThroughExec } from "./process.js";
|
|
28
|
+
import { isWhole, packLabels } from "./values.js";
|
|
29
|
+
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";
|
|
28
31
|
const VERSION = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version;
|
|
29
32
|
export const NAME_LABEL = `${COMPUTER_LABEL}.name`;
|
|
30
33
|
/** The image create was asked for: a vendor may report another name for it (a template's id). */
|
|
31
34
|
export const IMAGE_LABEL = `${COMPUTER_LABEL}.image`;
|
|
32
35
|
/** The most extend may move the deadline to: create's time plus the cap, by this machine's clock, as init was given. */
|
|
33
36
|
export const MAX_LABEL = `${COMPUTER_LABEL}.max-deadline`;
|
|
37
|
+
/** A GPU computer's GPUs, type:count: a retry and attach read them here, so no vendor need read them back. */
|
|
38
|
+
export const GPU_LABEL = `${COMPUTER_LABEL}.gpu`;
|
|
34
39
|
const NAME = /^computer-[0-9a-f]{12}$/;
|
|
35
40
|
const MACHINE_ID = /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,255}$/;
|
|
36
41
|
const USER = /^(\d+):(\d+)$/;
|
|
@@ -51,6 +56,8 @@ const DEFAULTS = {
|
|
|
51
56
|
listedStableMs: 1_000,
|
|
52
57
|
mountMs: 120_000,
|
|
53
58
|
};
|
|
59
|
+
/** The adapter's timeouts, the caller's over its defaults: what a host reads for its own requestMs or pollMs. */
|
|
60
|
+
export const timeoutsOf = (timeouts = {}) => ({ ...DEFAULTS, ...timeouts });
|
|
54
61
|
/** The user an Archil computer's tools run as: the image's agentrun-tool, with no sudo and no capability. */
|
|
55
62
|
const ARCHIL_USER = `${ARCHIL_TOOL_UID}:${ARCHIL_TOOL_UID}`;
|
|
56
63
|
const RELEASE_POLL_MAX_MS = 5_000;
|
|
@@ -71,30 +78,30 @@ const STAGING = "/tmp/agentrun-computerd-";
|
|
|
71
78
|
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"];
|
|
72
79
|
/** A probe target: host:port, over TCP, or host:port/udp, a DNS query over UDP. */
|
|
73
80
|
const TARGET = /^(?:\[[0-9A-Fa-f:.]+\]|[A-Za-z0-9.-]+):[0-9]{1,5}(?:\/udp)?$/;
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
const show = (value) => (value === undefined ? "nothing" : JSON.stringify(value));
|
|
81
|
+
/** The spec's fields the adapter passes on or holds itself: a size goes in the request, for the host's check to refuse. */
|
|
82
|
+
const SPEC_FIELDS = new Set(["image", "lifetimeMs", "network", "labels", "mount", "gpu", "owner", "cpus", "memoryMb"]);
|
|
77
83
|
const policyWords = (policy) => (typeof policy === "string" ? policy : "other" in policy ? policy.other : JSON.stringify(policy));
|
|
78
|
-
|
|
84
|
+
/** What makes a computer's labels smaller, in words: the list only where its network is one. */
|
|
85
|
+
const shorterWords = (labels) => `give a shorter image, mount folder${labels[NETWORK_LABEL]?.startsWith("allow:") ? ", network list" : ""} or labels`;
|
|
86
|
+
const alive = (state) => state !== "gone";
|
|
87
|
+
/** The codes of a create the vendor refused, which made nothing. */
|
|
88
|
+
const REFUSED_CREATE = new Set(["invalid", "denied", "limit", "not_found", "refused", "unauthorized", "exists"]);
|
|
89
|
+
/**
|
|
90
|
+
* Whether a create that failed with code may have made a machine, so the request's name is swept. A refusal made
|
|
91
|
+
* nothing: a machine under the name then is another's (exists: another instance's create of the request), never
|
|
92
|
+
* deleted. Any other failure (the vendor's, a timeout) may have made one, answered in time or after create gave up.
|
|
93
|
+
*/
|
|
94
|
+
const mayHaveMade = (code) => !REFUSED_CREATE.has(code);
|
|
79
95
|
/** An ISO time a label holds, or undefined when it is missing or not the ISO time create writes. */
|
|
80
|
-
function isoOf(written) {
|
|
96
|
+
export function isoOf(written) {
|
|
81
97
|
const at = written === undefined ? Number.NaN : Date.parse(written);
|
|
82
98
|
return Number.isNaN(at) || new Date(at).toISOString() !== written ? undefined : new Date(at);
|
|
83
99
|
}
|
|
84
100
|
/**
|
|
85
|
-
* When a machine ends:
|
|
86
|
-
*
|
|
101
|
+
* When a machine ends: its deadline label, as the vendor reads it back. A vendor that keeps no label change reads it
|
|
102
|
+
* as the end its timer moved to (E2B's get), so a restarted provider reads an extend all the same.
|
|
87
103
|
*/
|
|
88
|
-
export
|
|
89
|
-
const written = isoOf(machine.labels[DEADLINE_LABEL]);
|
|
90
|
-
if (written === undefined)
|
|
91
|
-
return undefined;
|
|
92
|
-
const end = machine.endsAt instanceof Date ? machine.endsAt.getTime() : Number.NaN;
|
|
93
|
-
if (machine.timerMoved !== true || Number.isNaN(end))
|
|
94
|
-
return written;
|
|
95
|
-
const most = isoOf(machine.labels[MAX_LABEL]);
|
|
96
|
-
return new Date(most === undefined ? end : Math.min(end, most.getTime()));
|
|
97
|
-
}
|
|
104
|
+
export const deadlineOf = (machine) => isoOf(machine.labels[DEADLINE_LABEL]);
|
|
98
105
|
/** Only a machine this adapter made counts: its label, a name of its making, and the name asked for when one is. */
|
|
99
106
|
export const ours = (labels, name) => labels?.[COMPUTER_LABEL] === "1" && NAME.test(labels[NAME_LABEL] ?? "") && (name === undefined || labels[NAME_LABEL] === name);
|
|
100
107
|
/** The Archil mount a computer's agentrun.mount label records, or undefined when it has none. */
|
|
@@ -123,15 +130,25 @@ function specUserOf(value) {
|
|
|
123
130
|
const [uid, gid] = [Number(found[1]), Number(found[2] ?? found[1])];
|
|
124
131
|
return uid <= MAX_ID && gid <= MAX_ID ? `${uid}:${gid}` : undefined;
|
|
125
132
|
}
|
|
133
|
+
/** The GPUs a machine's GPU_LABEL records (type:count), as a spec's gpu field; none where it records none. */
|
|
134
|
+
function gpuOf(written) {
|
|
135
|
+
const at = written?.lastIndexOf(":") ?? -1;
|
|
136
|
+
if (written === undefined || at < 1)
|
|
137
|
+
return {};
|
|
138
|
+
const count = Number(written.slice(at + 1));
|
|
139
|
+
return isWhole(count) ? { gpu: { type: written.slice(0, at), count } } : {};
|
|
140
|
+
}
|
|
126
141
|
/** The caller's labels of a machine: every one but the adapter's own namespace and the Archil mount's. */
|
|
127
142
|
const callerLabels = (labels) => Object.fromEntries(Object.entries(labels).filter(([key]) => key !== COMPUTER_LABEL && !key.startsWith(`${COMPUTER_LABEL}.`) && key !== ARCHIL_MOUNT_LABEL));
|
|
128
|
-
/**
|
|
143
|
+
/** The shared adapter's provider: a ComputerProvider whose vendor is host. */
|
|
129
144
|
export class HostedProvider {
|
|
130
145
|
id;
|
|
131
146
|
version = VERSION;
|
|
132
147
|
/** The vendor's machines, for the live sweep (conformance/sweep.ts); nothing else reaches past the provider to them. */
|
|
133
148
|
host;
|
|
134
149
|
extend;
|
|
150
|
+
/** The host's descriptor's configuration of this provider's kind: its groups and GPUs. */
|
|
151
|
+
#configuration;
|
|
135
152
|
#label;
|
|
136
153
|
#fail;
|
|
137
154
|
#daemonPath;
|
|
@@ -150,15 +167,20 @@ export class HostedProvider {
|
|
|
150
167
|
/** Each computer's last extend, by machine id, while one runs: the next waits for it. */
|
|
151
168
|
#extending = new Map();
|
|
152
169
|
constructor(host, options) {
|
|
153
|
-
const {
|
|
154
|
-
|
|
170
|
+
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;
|
|
171
|
+
const { id, label, capabilities } = host.descriptor;
|
|
172
|
+
if (kind !== "desktop" && kind !== "linux")
|
|
173
|
+
throw new Error(`kind is desktop or linux, not ${String(kind)}`);
|
|
174
|
+
const configuration = capabilities.find((one) => one.kind === kind);
|
|
175
|
+
if (configuration === undefined)
|
|
176
|
+
throw new Error(`${label} offers no ${kind} computer`);
|
|
177
|
+
this.id = id;
|
|
155
178
|
this.host = host;
|
|
179
|
+
this.#configuration = configuration;
|
|
156
180
|
this.#label = label;
|
|
157
|
-
this.#fail =
|
|
181
|
+
this.#fail = failFor(id);
|
|
158
182
|
this.#onNote = onNote;
|
|
159
183
|
this.#archilEgress = archilEgress === undefined || archilEgress === "" ? undefined : archilEgressOf(archilEgress);
|
|
160
|
-
if (kind !== "desktop" && kind !== "linux")
|
|
161
|
-
throw new Error(`kind is desktop or linux, not ${String(kind)}`);
|
|
162
184
|
if (!isWhole(maxLifetimeMs) || maxLifetimeMs <= 0 || maxLifetimeMs > host.facts.maxLifetimeMs) {
|
|
163
185
|
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`);
|
|
164
186
|
}
|
|
@@ -168,7 +190,7 @@ export class HostedProvider {
|
|
|
168
190
|
// The numbers, not the text: "00" is root as much as "0" is.
|
|
169
191
|
if (Number(user[1]) === 0 || Number(user[2]) === 0)
|
|
170
192
|
throw new Error(`the daemon user ${daemonUser} is root or in root's group; ${a(label)} computer's daemon runs as neither`);
|
|
171
|
-
const merged =
|
|
193
|
+
const merged = timeoutsOf(timeouts);
|
|
172
194
|
// AbortSignal.timeout and Node's timers take whole ms up to 2^31 - 1 (a fraction throws, a longer one fires at
|
|
173
195
|
// once), and a call's race adds CALL_SLACK_MS to its bound: refused here, before any machine exists. The listing's
|
|
174
196
|
// stability window alone may be 0: two consecutive polls, with no time between them asked for.
|
|
@@ -187,29 +209,31 @@ export class HostedProvider {
|
|
|
187
209
|
[this.#uid, this.#gid] = [String(Number(user[1])), String(Number(user[2]))];
|
|
188
210
|
this.#timeouts = merged;
|
|
189
211
|
this.#probeTargets = [...probeTargets];
|
|
190
|
-
// Extend only where the
|
|
191
|
-
|
|
192
|
-
if ((lifetime === "vendor" && host.setTimer) || (lifetime === "janitor" && host.setLabels))
|
|
212
|
+
// Extend only where the end can follow it: the host moves its timer or its deadline label.
|
|
213
|
+
if (host.extend)
|
|
193
214
|
this.extend = (ref, ttlMs, context) => this.#extendSerial(ref, ttlMs, context?.signal);
|
|
194
215
|
}
|
|
195
|
-
/** The computer's facts, from the host's and the adapter's options alone. */
|
|
216
|
+
/** The computer's facts, from the descriptor, the host's facts and the adapter's options alone. */
|
|
196
217
|
capabilities() {
|
|
197
|
-
const { facts } = this.host;
|
|
218
|
+
const { facts, descriptor } = this.host;
|
|
219
|
+
const { gpu } = this.#configuration;
|
|
198
220
|
return {
|
|
199
221
|
kind: this.#kind,
|
|
200
222
|
lifetime: facts.lifetime,
|
|
201
|
-
network: [...
|
|
202
|
-
|
|
223
|
+
network: [...descriptor.network],
|
|
224
|
+
// A list is the egress proxy's, init's own (./network.ts): Docker's bounds and ports.
|
|
225
|
+
...(descriptor.network.includes("allow") ? { allow: structuredClone(HOSTED_ALLOW) } : {}),
|
|
226
|
+
// The probe and the proxy's policy from inside are the network's truth: no vendor's read of it is taken.
|
|
227
|
+
networkReadBack: false,
|
|
203
228
|
isolation: facts.isolation,
|
|
204
229
|
os: "linux",
|
|
205
230
|
daemon: facts.daemon,
|
|
206
|
-
groups: [
|
|
231
|
+
groups: [...this.#configuration.groups],
|
|
207
232
|
maxLifetimeMs: this.#maxLifetimeMs,
|
|
208
|
-
release: [
|
|
209
|
-
|
|
210
|
-
...(facts.mounts === undefined ? {} : { mount: [...facts.mounts] }),
|
|
233
|
+
release: ["delete"],
|
|
234
|
+
mount: [...descriptor.mounts],
|
|
211
235
|
vendorAgentPorts: [...facts.vendorAgentPorts],
|
|
212
|
-
...(
|
|
236
|
+
...(gpu === undefined ? {} : { gpu: structuredClone(gpu) }),
|
|
213
237
|
...(facts.user === undefined ? {} : { user: [...facts.user] }),
|
|
214
238
|
};
|
|
215
239
|
}
|
|
@@ -220,10 +244,11 @@ export class HostedProvider {
|
|
|
220
244
|
const { image, network, labels, mount } = given;
|
|
221
245
|
const capabilities = this.capabilities();
|
|
222
246
|
const label = this.#label;
|
|
223
|
-
|
|
247
|
+
// An Archil mount's own rules hold only where the provider takes one; elsewhere checkMount's refusal says why alone.
|
|
248
|
+
const archil = mount?.kind === "archil" && (capabilities.mount ?? []).includes("archil");
|
|
249
|
+
refusals.push(...checkMount(mount, capabilities.mount ?? [], this.host.descriptor.unmounted), ...(archil ? archilRefusals(given) : []));
|
|
224
250
|
const daemonUser = `${this.#uid}:${this.#gid}`;
|
|
225
|
-
const
|
|
226
|
-
const asked = this.#takesUser() ? given.user : undefined;
|
|
251
|
+
const asked = this.#takesUser ? given.user : undefined;
|
|
227
252
|
if (asked !== undefined) {
|
|
228
253
|
const user = specUserOf(asked);
|
|
229
254
|
if (user === undefined)
|
|
@@ -234,6 +259,9 @@ export class HostedProvider {
|
|
|
234
259
|
else if (archil && user !== ARCHIL_USER) {
|
|
235
260
|
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}`);
|
|
236
261
|
}
|
|
262
|
+
else if (throughProxy(network) && user.split(":").includes(String(EGRESS_PROXY_USER))) {
|
|
263
|
+
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`);
|
|
264
|
+
}
|
|
237
265
|
}
|
|
238
266
|
else if (archil && daemonUser !== ARCHIL_USER) {
|
|
239
267
|
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}`);
|
|
@@ -241,22 +269,24 @@ export class HostedProvider {
|
|
|
241
269
|
if (typeof image !== "string" || image === "" || image.includes("\0"))
|
|
242
270
|
refuse("image", `an image is required: the name of ${a(label)} image`);
|
|
243
271
|
refusals.push(...checkTenure(given, capabilities, label));
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
refuse("network",
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
272
|
+
// Only the forms the descriptor declares, each one this computer enforces; any other is refused, never widened.
|
|
273
|
+
const networkProblem = networkRefusal(label, capabilities.network, network);
|
|
274
|
+
if (networkProblem)
|
|
275
|
+
refuse("network", networkProblem);
|
|
276
|
+
const entries = labels === undefined ? [] : typeof labels === "object" && labels !== null && !Array.isArray(labels) ? Object.entries(labels) : undefined;
|
|
277
|
+
const bad = entries === undefined ? "labels must map names to strings" : entries.map(([key, value]) => labelProblem(key, value)).find(Boolean);
|
|
278
|
+
if (bad)
|
|
279
|
+
refuse("labels", bad);
|
|
280
|
+
else if (typeof image === "string" && (!archil || ["disk", "region", "folder"].every((key) => typeof mount[key] === "string"))) {
|
|
281
|
+
const over = this.#labelsOver(given);
|
|
282
|
+
if (over)
|
|
283
|
+
refuse(archil && over.bytes ? "mount" : "labels", over.why);
|
|
253
284
|
}
|
|
254
285
|
refusals.push(...checkGpu(given.gpu, capabilities, mount));
|
|
255
|
-
const taken = new Set(this.host.facts.specFields ?? []);
|
|
256
286
|
for (const [field, value] of Object.entries(given)) {
|
|
257
|
-
if (SPEC_FIELDS.has(field) ||
|
|
287
|
+
if (SPEC_FIELDS.has(field) || (field === "user" && this.#takesUser) || value === undefined)
|
|
258
288
|
continue;
|
|
259
|
-
if (field === "
|
|
289
|
+
if (field === "pids")
|
|
260
290
|
refuse(field, `${a(label)} computer has the size of its image: name an image of the size you need, not ${field}`);
|
|
261
291
|
else if (field === "user")
|
|
262
292
|
refuse(field, `${a(label)} computer's daemon runs as the provider's daemon user (${daemonUser}), never as one a spec names`);
|
|
@@ -266,24 +296,51 @@ export class HostedProvider {
|
|
|
266
296
|
refuse(field, `${a(label)} computer does not support ${field}`);
|
|
267
297
|
}
|
|
268
298
|
if (this.host.check)
|
|
269
|
-
refusals.push(...this.host.check(
|
|
299
|
+
refusals.push(...this.host.check(given));
|
|
270
300
|
return refusals;
|
|
271
301
|
}
|
|
272
|
-
/** Whether the host takes a spec's user (facts.
|
|
273
|
-
#takesUser() {
|
|
274
|
-
return
|
|
302
|
+
/** Whether the host takes a spec's user (facts.user): then a computer's daemon runs as the one a spec names. */
|
|
303
|
+
get #takesUser() {
|
|
304
|
+
return this.host.facts.user !== undefined;
|
|
275
305
|
}
|
|
276
306
|
/** Who one create's daemon runs as, uid:gid: the spec's user where the host takes one, else the provider's daemon user. */
|
|
277
307
|
#userOf(spec) {
|
|
278
|
-
return (this.#takesUser
|
|
308
|
+
return (this.#takesUser ? specUserOf(spec.user) : undefined) ?? `${this.#uid}:${this.#gid}`;
|
|
279
309
|
}
|
|
280
|
-
/**
|
|
281
|
-
|
|
282
|
-
|
|
310
|
+
/**
|
|
311
|
+
* Why a spec's labels, with the adapter's own (#labelsOf, as create gives them, each deadline an ISO time at its
|
|
312
|
+
* longest), are more, or come to more bytes, or pack into more, than the vendor takes (facts.labels); undefined when
|
|
313
|
+
* they fit.
|
|
314
|
+
*/
|
|
315
|
+
#labelsOver(spec) {
|
|
316
|
+
const { count, bytes: max, packed } = this.host.facts.labels ?? {};
|
|
317
|
+
// 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
|
+
// 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 (#packedOver).
|
|
320
|
+
const name = "computer-5f3a9c1e7b2d";
|
|
321
|
+
const labels = this.#labelsOf(spec, name, "2026-11-16T04:15:29.481Z", "2026-11-17T04:05:29.481Z");
|
|
322
|
+
if (packed) {
|
|
323
|
+
const why = this.#packedOver(labels, name);
|
|
324
|
+
return why === undefined ? undefined : { bytes: true, why };
|
|
325
|
+
}
|
|
326
|
+
const keys = Object.keys(labels);
|
|
327
|
+
if (count !== undefined && keys.length > count) {
|
|
328
|
+
// labelProblem refuses a caller's label of the adapter's names, so the two never overlap.
|
|
329
|
+
const own = keys.length - Object.keys(spec.labels ?? {}).length;
|
|
330
|
+
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` };
|
|
331
|
+
}
|
|
283
332
|
if (max === undefined)
|
|
284
333
|
return undefined;
|
|
285
|
-
const
|
|
286
|
-
|
|
334
|
+
const sizes = Object.entries(labels).map(([key, value]) => [key, Buffer.byteLength(key) + Buffer.byteLength(value)]);
|
|
335
|
+
const bytes = sizes.reduce((sum, [, size]) => sum + size, 0);
|
|
336
|
+
if (bytes <= max)
|
|
337
|
+
return undefined;
|
|
338
|
+
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
|
+
}
|
|
340
|
+
/** Why labels don't fit packed (facts.labels.packed), in words; undefined when they fit or the host keeps them as they are. */
|
|
341
|
+
#packedOver(labels, name) {
|
|
342
|
+
const fit = this.host.facts.labels?.packed ? packLabels(labels, name, this.host.facts.labels) : { labels };
|
|
343
|
+
return "problem" in fit ? `${a(this.#label)} computer keeps its labels packed, and ${fit.problem}: ${shorterWords(labels)}` : undefined;
|
|
287
344
|
}
|
|
288
345
|
/** A host call bounded by ms, the caller's signal when given. */
|
|
289
346
|
#call(what, run, ms, signal) {
|
|
@@ -315,16 +372,17 @@ export class HostedProvider {
|
|
|
315
372
|
#labelDiffer(spec, name, labels, seen) {
|
|
316
373
|
const asked = this.#labelsOf(spec, name, labels[DEADLINE_LABEL] ?? "", labels[MAX_LABEL] ?? "");
|
|
317
374
|
const differ = Object.entries(asked)
|
|
318
|
-
|
|
375
|
+
// The GPUs' label is read back as the spec's gpu, held where the user and network are (#again).
|
|
376
|
+
.filter(([key, value]) => key !== DEADLINE_LABEL && key !== MAX_LABEL && key !== GPU_LABEL && labels[key] !== value)
|
|
319
377
|
.map(([key, value]) => `label ${key}: asked ${show(value)}, ${seen} ${show(labels[key])}`);
|
|
320
378
|
const extra = Object.keys(callerLabels(labels)).filter((key) => !Object.hasOwn(asked, key));
|
|
321
379
|
if (extra.length > 0)
|
|
322
380
|
differ.push(`labels ${extra.join(", ")}: ${seen}, never asked for`);
|
|
323
381
|
return differ;
|
|
324
382
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
const listed = await this
|
|
383
|
+
/** taken: the vendor refused this create's name as one a machine has (exists), so the name's machine is read once more. */
|
|
384
|
+
async #createOnce(spec, name, signal, taken) {
|
|
385
|
+
const listed = await foundBy(this.host, "the listing by the request's name", byName(name), this.#timeouts, signal);
|
|
328
386
|
if (!listed.ok)
|
|
329
387
|
return listed;
|
|
330
388
|
const found = listed.value.filter((machine) => ours(machine.labels, name));
|
|
@@ -332,9 +390,11 @@ export class HostedProvider {
|
|
|
332
390
|
return this.#fail("exists", `${found.length} machines carry the name ${name} (${found.map((m) => m.id).join(", ")}): create with a new request id`);
|
|
333
391
|
if (found.length === 1)
|
|
334
392
|
return this.#again(spec, name, found[0], signal);
|
|
393
|
+
if (taken)
|
|
394
|
+
return taken;
|
|
335
395
|
const launched = await this.#launch(spec, name, signal);
|
|
336
396
|
if (!launched.ok)
|
|
337
|
-
return launched;
|
|
397
|
+
return launched.error.code === "exists" ? this.#createOnce(spec, name, signal, launched) : launched;
|
|
338
398
|
const { ref, made } = launched.value;
|
|
339
399
|
// From here the machine exists: every step takes the caller's signal, and a failure or an abort deletes it.
|
|
340
400
|
// Another provider instance's create of this request may have made one too (#converge): the oldest is kept.
|
|
@@ -371,7 +431,7 @@ export class HostedProvider {
|
|
|
371
431
|
* same listing. Its value is the machine kept, or undefined when mine holds the name alone (or nothing is listed yet).
|
|
372
432
|
*/
|
|
373
433
|
async #converge(name, mine, signal) {
|
|
374
|
-
const listed = await this
|
|
434
|
+
const listed = await foundBy(this.host, "the listing by the request's name", byName(name), this.#timeouts, signal);
|
|
375
435
|
if (!listed.ok)
|
|
376
436
|
return listed;
|
|
377
437
|
const holders = listed.value.filter((machine) => ours(machine.labels, name) && alive(machine.state));
|
|
@@ -384,30 +444,27 @@ export class HostedProvider {
|
|
|
384
444
|
}
|
|
385
445
|
const [kept, ...others] = holders.sort(oldestFirst(holders));
|
|
386
446
|
for (const other of others) {
|
|
387
|
-
const gone = await this.#
|
|
447
|
+
const { letGo, gone } = await this.#remove({ id: other.id, name }, other, signal);
|
|
448
|
+
if (!letGo.ok)
|
|
449
|
+
this.#onNote?.({ computer: name, note: `${other.id}, a second machine of one request, was deleted, and ${letGo.error.message}` });
|
|
388
450
|
if (!gone.ok)
|
|
389
451
|
return this.#fail(gone.error.code, `${holders.length} machines carry the name ${name}: ${kept?.id}, the oldest, is kept, and ${other.id} could not be deleted (${gone.error.message})`);
|
|
390
452
|
}
|
|
391
453
|
return ok(kept);
|
|
392
454
|
}
|
|
393
|
-
/**
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
const letGo = await this.#letGo(ref, signal);
|
|
398
|
-
if (!letGo.ok)
|
|
399
|
-
this.#onNote?.({ computer: ref.name, note: `${machine.id}, a second machine of one request, was deleted, and ${letGo.error.message}` });
|
|
400
|
-
}
|
|
401
|
-
const killed = await this.#kill(machine.id);
|
|
402
|
-
return killed.ok ? this.#settle(ref, signal) : killed;
|
|
403
|
-
}
|
|
404
|
-
/** A create of a request whose computer is listed already: the same create, the computer as it is; see create. */
|
|
455
|
+
/**
|
|
456
|
+
* A create of a request whose computer is listed already: the same create, the computer as it is, read by get (a
|
|
457
|
+
* listing may leave its network out); see create.
|
|
458
|
+
*/
|
|
405
459
|
async #again(spec, name, first, signal) {
|
|
406
460
|
const until = Date.now() + this.#timeouts.createMs;
|
|
407
|
-
|
|
461
|
+
const read = await this.#get(first.id, signal);
|
|
462
|
+
if (!read.ok)
|
|
463
|
+
return read;
|
|
464
|
+
let machine = read.value;
|
|
408
465
|
let last;
|
|
409
466
|
for (;;) {
|
|
410
|
-
if (machine === undefined ||
|
|
467
|
+
if (machine === undefined || (machine.state !== "starting" && machine.state !== "running"))
|
|
411
468
|
return this.#fail("exists", endedWords(name));
|
|
412
469
|
const deadline = deadlineOf(machine);
|
|
413
470
|
if (deadline !== undefined && deadline.getTime() <= Date.now())
|
|
@@ -417,10 +474,9 @@ export class HostedProvider {
|
|
|
417
474
|
if (differ.length > 0)
|
|
418
475
|
return this.#fail("exists", otherSpecWords(name, "was made", differ));
|
|
419
476
|
const attached = await this.#attached(machine, signal);
|
|
420
|
-
// The image
|
|
421
|
-
//
|
|
422
|
-
const
|
|
423
|
-
const drift = attached.ok ? readBackDiffer({ ...spec, user: this.#userOf(spec) }, attached.value.spec, ["user", "network", ...sizes]) : [];
|
|
477
|
+
// The image, an Archil mount and the GPUs are in the labels; the user and the network are read back. No vendor
|
|
478
|
+
// shows a drift in a size.
|
|
479
|
+
const drift = attached.ok ? readBackDiffer({ ...spec, network: networkOfForm(formOf(spec.network)), user: this.#userOf(spec) }, attached.value.spec, ["user", "network", "gpu"]) : [];
|
|
424
480
|
if (drift.length > 0)
|
|
425
481
|
return this.#fail("exists", otherSpecWords(name, "was made", drift));
|
|
426
482
|
if (attached.ok)
|
|
@@ -446,16 +502,23 @@ export class HostedProvider {
|
|
|
446
502
|
machine = read.value;
|
|
447
503
|
}
|
|
448
504
|
}
|
|
449
|
-
/**
|
|
505
|
+
/**
|
|
506
|
+
* The labels create gives a computer: the caller's, the adapter's own, and an Archil mount's (never its token). The
|
|
507
|
+
* most its deadline may reach only where the host can extend it, the label's one reader: a host with no extend (Modal,
|
|
508
|
+
* Daytona) spends none of its label bytes on it (Modal's 512 hold the box image's tag beside an Archil mount's label).
|
|
509
|
+
*/
|
|
450
510
|
#labelsOf(spec, name, deadline, maxDeadline) {
|
|
511
|
+
const network = formOf(spec.network);
|
|
451
512
|
return {
|
|
452
513
|
...spec.labels,
|
|
453
514
|
[COMPUTER_LABEL]: "1",
|
|
454
515
|
[NAME_LABEL]: name,
|
|
455
516
|
[DEADLINE_LABEL]: deadline,
|
|
456
517
|
[IMAGE_LABEL]: spec.image,
|
|
457
|
-
[MAX_LABEL]: maxDeadline,
|
|
518
|
+
...(this.host.extend ? { [MAX_LABEL]: maxDeadline } : {}),
|
|
519
|
+
...(network === "" ? {} : { [NETWORK_LABEL]: network }),
|
|
458
520
|
...(spec.mount?.kind === "archil" ? archilLabels(spec.mount) : {}),
|
|
521
|
+
...(typeof spec.gpu?.type === "string" && typeof spec.gpu.count === "number" ? { [GPU_LABEL]: `${spec.gpu.type}:${spec.gpu.count}` } : {}),
|
|
459
522
|
};
|
|
460
523
|
}
|
|
461
524
|
/** Everything create does up to the vendor's answer: the daemon read, the machine made, by its request. */
|
|
@@ -475,6 +538,10 @@ export class HostedProvider {
|
|
|
475
538
|
const deadline = new Date(now + lifetimeMs).toISOString();
|
|
476
539
|
const maxDeadline = new Date(now + this.#maxLifetimeMs).toISOString();
|
|
477
540
|
const labels = this.#labelsOf(spec, name, deadline, maxDeadline);
|
|
541
|
+
// check() packed stand-ins; these deflate a byte apart from them, so the host is never given labels that don't fit.
|
|
542
|
+
const over = this.#packedOver(labels, name);
|
|
543
|
+
if (over !== undefined)
|
|
544
|
+
return this.#fail("refused", `labels: ${over}`);
|
|
478
545
|
const archil = spec.mount?.kind === "archil";
|
|
479
546
|
const user = this.#userOf(spec);
|
|
480
547
|
const commands = hostedCommands({
|
|
@@ -489,82 +556,69 @@ export class HostedProvider {
|
|
|
489
556
|
nonce: randomBytes(16).toString("hex"),
|
|
490
557
|
waitS: Math.ceil((this.#timeouts.createMs + this.#timeouts.uploadMs + this.#timeouts.execMs + (archil ? this.#timeouts.mountMs * 3 : 0)) / 1000),
|
|
491
558
|
...(archil && this.host.facts.daemon === "pid1" ? { gate: ARCHIL_GATE } : {}),
|
|
559
|
+
rootOnly: this.host.facts.rootOnly ?? [],
|
|
560
|
+
network: formOf(spec.network),
|
|
492
561
|
});
|
|
493
|
-
const taken = new Set(this.host.facts.specFields ?? []);
|
|
494
562
|
const request = {
|
|
495
563
|
name,
|
|
496
564
|
image: spec.image,
|
|
497
565
|
labels,
|
|
498
|
-
|
|
499
|
-
//
|
|
500
|
-
//
|
|
501
|
-
|
|
566
|
+
lifetimeMs,
|
|
567
|
+
// The machine has egress where its tools' network is open or a list, which init's proxy holds to the public
|
|
568
|
+
// 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),
|
|
502
571
|
...(this.host.facts.daemon === "pid1" ? { command: commands.entrypoint } : {}),
|
|
503
572
|
...(spec.gpu === undefined ? {} : { gpu: spec.gpu }),
|
|
504
|
-
...(
|
|
505
|
-
...(
|
|
506
|
-
...(
|
|
507
|
-
...(taken.has("user") && spec.user !== undefined ? { user } : {}),
|
|
573
|
+
...(spec.cpus === undefined ? {} : { cpus: spec.cpus }),
|
|
574
|
+
...(spec.memoryMb === undefined ? {} : { memoryMb: spec.memoryMb }),
|
|
575
|
+
...(this.#takesUser && spec.user !== undefined ? { user } : {}),
|
|
508
576
|
};
|
|
509
|
-
const runtime = this.host.facts.runtime?.(request);
|
|
510
577
|
const { createMs } = this.#timeouts;
|
|
511
|
-
// Never
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
creating = Promise.resolve(this.host.create(request, { timeoutMs: createMs }));
|
|
515
|
-
}
|
|
516
|
-
catch (error) {
|
|
517
|
-
return this.#fail(this.host.errorCode(error), `${this.#label}'s create failed: ${words(error)}`);
|
|
518
|
-
}
|
|
519
|
-
const raced = await race(this.host, creating, `${this.#label}'s create`, createMs + CALL_SLACK_MS, signal);
|
|
520
|
-
const created = raced.ok ? raced.value : raced;
|
|
578
|
+
// Never signalled, only raced: an aborted call could leave a machine made whose id never came back. One that lands
|
|
579
|
+
// after this gave up is deleted by its id; one that fails after it, by its name where it may have made one.
|
|
580
|
+
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);
|
|
521
581
|
if (!created.ok) {
|
|
522
582
|
const { code, message } = created.error;
|
|
523
583
|
if (code === "aborted" || code === "timeout") {
|
|
524
|
-
// One that lands is deleted by its id; one whose answer never comes, by its name.
|
|
525
584
|
this.#end(name);
|
|
526
|
-
creating.then(async (made) => void (made.ok ? await this.#kill(made.value.id) : await this.#deleteNamed(name)), async () => void (await this.#deleteNamed(name)));
|
|
527
585
|
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`);
|
|
528
586
|
}
|
|
529
|
-
|
|
530
|
-
if (["invalid", "denied", "limit", "not_found", "refused", "unauthorized", "exists"].includes(code))
|
|
587
|
+
if (!mayHaveMade(code))
|
|
531
588
|
return created;
|
|
532
589
|
this.#end(name);
|
|
533
590
|
const swept = await this.#deleteNamed(name);
|
|
534
591
|
return this.#fail(code, `${this.#label} did not create the machine: ${message}${swept}`);
|
|
535
592
|
}
|
|
536
593
|
this.#requests.made(name);
|
|
537
|
-
return ok({ ref: { id: created.value.id, name }, made: { deadline, maxDeadline, labels, daemon, sha256, commands, user
|
|
594
|
+
return ok({ ref: { id: created.value.id, name }, made: { deadline, maxDeadline, labels, daemon, sha256, commands, user } });
|
|
538
595
|
}
|
|
539
|
-
/** Deletes a machine create gives up on, then waits, under the caller's signal, until
|
|
596
|
+
/** Deletes a machine create gives up on, then waits, under the caller's signal, until get answers none for it. */
|
|
540
597
|
async #abandon(ref, error, signal, mounted = false) {
|
|
541
598
|
this.#end(ref.name);
|
|
542
599
|
// A computer create made whole (its Archil folder mounted) lets go of the folder first, as release does.
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
await this.#letGo(ref, signal);
|
|
547
|
-
}
|
|
548
|
-
const killed = await this.#kill(ref.id);
|
|
549
|
-
const gone = killed.ok ? await this.#settle(ref, signal) : killed;
|
|
600
|
+
const read = mounted ? await this.#get(ref.id) : undefined;
|
|
601
|
+
const { gone } = await this.#remove(ref, read?.ok ? read.value : undefined, signal);
|
|
602
|
+
// The delete is never signalled, so an abort is the wait's, after the delete took.
|
|
550
603
|
const after = gone.ok
|
|
551
604
|
? "was deleted"
|
|
552
|
-
:
|
|
605
|
+
: gone.error.code === "aborted"
|
|
553
606
|
? `was deleted, and the wait for it to go was aborted; ${this.#label} ends it at its deadline if the delete did not take`
|
|
554
607
|
: `could not be deleted (${gone.error.message}); it ends at its deadline`;
|
|
555
608
|
return this.#fail(error.code, `the computer ${ref.name} ${after}: ${error.message}`);
|
|
556
609
|
}
|
|
557
610
|
/**
|
|
558
|
-
* Every machine carrying a name create gave, deleted and read back gone: for a create
|
|
611
|
+
* Every machine carrying a name create gave, deleted and read back gone: for a create that may have made one whose
|
|
612
|
+
* id never came (mayHaveMade), never after a refusal, where a machine under the name is another's. The
|
|
559
613
|
* listing shows a new machine a moment after the vendor makes it, so it is read for the name until one shows or
|
|
560
614
|
* listedMs passes. Best effort, in words.
|
|
561
615
|
*/
|
|
562
616
|
async #deleteNamed(name) {
|
|
563
|
-
const { listedMs, pollMs
|
|
617
|
+
const { listedMs, pollMs } = this.#timeouts;
|
|
564
618
|
const until = Date.now() + listedMs;
|
|
565
619
|
let found;
|
|
566
620
|
for (;;) {
|
|
567
|
-
const listed = await this
|
|
621
|
+
const listed = await foundBy(this.host, "the listing by name", byName(name), this.#timeouts);
|
|
568
622
|
if (!listed.ok)
|
|
569
623
|
return `; a machine named ${name} may be left (${listed.error.message}), which ends at its deadline`;
|
|
570
624
|
found = listed.value.filter((machine) => ours(machine.labels, name));
|
|
@@ -576,8 +630,7 @@ export class HostedProvider {
|
|
|
576
630
|
return `; no machine named ${name} was listed within ${seconds(listedMs)}, and any it made ends at its deadline`;
|
|
577
631
|
const said = [];
|
|
578
632
|
for (const machine of found) {
|
|
579
|
-
const
|
|
580
|
-
const gone = killed.ok ? await this.#settle({ id: machine.id, name }) : killed;
|
|
633
|
+
const { gone } = await this.#remove({ id: machine.id, name }, undefined);
|
|
581
634
|
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`);
|
|
582
635
|
}
|
|
583
636
|
return `; the machine named ${name} it made: ${said.join("; ")}`;
|
|
@@ -591,9 +644,9 @@ export class HostedProvider {
|
|
|
591
644
|
#startPollMs() {
|
|
592
645
|
return Math.min(START_POLL_MS, this.#timeouts.pollMs * 10);
|
|
593
646
|
}
|
|
594
|
-
/** The host's get, bounded by requestMs. */
|
|
647
|
+
/** The host's get, bounded by requestMs; a machine the vendor reports as not found is none. */
|
|
595
648
|
#get(id, signal) {
|
|
596
|
-
return
|
|
649
|
+
return readMachine(this.host, id, this.#timeouts.requestMs, signal);
|
|
597
650
|
}
|
|
598
651
|
/** The machine running, read while it starts (within createMs); gone or failed is refused in words. */
|
|
599
652
|
async #running(ref, signal) {
|
|
@@ -603,10 +656,11 @@ export class HostedProvider {
|
|
|
603
656
|
if (!read.ok)
|
|
604
657
|
return read;
|
|
605
658
|
const machine = read.value;
|
|
606
|
-
|
|
659
|
+
// A machine the vendor failed is gone in its words: the create stops in them, not at createMs.
|
|
660
|
+
if (machine?.why !== undefined && machine.state === "gone")
|
|
661
|
+
return this.#fail("vendor", `${this.#label} could not start ${ref.name}: ${machine.why}`);
|
|
662
|
+
if (machine === undefined || machine.state === "gone")
|
|
607
663
|
return this.#fail("gone", `${ref.name} was gone before it ran`);
|
|
608
|
-
if (machine.state === "failed")
|
|
609
|
-
return this.#fail("vendor", `${this.#label} could not start ${ref.name}${machine.why ? `: ${machine.why}` : ""}`);
|
|
610
664
|
if (machine.state !== "starting")
|
|
611
665
|
return ok(machine);
|
|
612
666
|
if (Date.now() + this.#startPollMs() >= until)
|
|
@@ -616,45 +670,40 @@ export class HostedProvider {
|
|
|
616
670
|
return this.#fail("aborted", "create was aborted while the machine started");
|
|
617
671
|
}
|
|
618
672
|
}
|
|
619
|
-
/**
|
|
620
|
-
|
|
621
|
-
|
|
673
|
+
/**
|
|
674
|
+
* Every way a machine, as the vendor reads it back, differs from what create asked for: its labels and its state. The
|
|
675
|
+
* rest is read from inside (the daemon, the probe). Pure: no call.
|
|
676
|
+
*/
|
|
677
|
+
#machineDrift(machine, labels) {
|
|
678
|
+
const found = [];
|
|
622
679
|
for (const [key, value] of Object.entries(labels))
|
|
623
680
|
if (machine.labels[key] !== value)
|
|
624
681
|
found.push(`label ${key}: declared ${show(value)}, read back ${show(machine.labels[key])}`);
|
|
625
|
-
if (this.host.facts.networkReadBack) {
|
|
626
|
-
// An Archil computer's vendor network is open, for root to reach Archil; its tools' none is read from inside.
|
|
627
|
-
const archil = spec.mount?.kind === "archil";
|
|
628
|
-
const vendor = archil ? "open" : spec.network;
|
|
629
|
-
const network = machine.network ?? { other: `${this.#label} read no network back` };
|
|
630
|
-
if (network !== vendor)
|
|
631
|
-
found.push(`network: declared ${policyWords(vendor)}${archil ? ` (${this.#label}'s, for root to reach Archil)` : ""}, read back ${policyWords(network)}`);
|
|
632
|
-
}
|
|
633
682
|
if (machine.state !== "running")
|
|
634
683
|
found.push(`state: declared "running", read back ${show(machine.state)}`);
|
|
635
|
-
// No extend has run on a machine create just made: a timer that reads moved ends it later than its spec.
|
|
636
|
-
if (machine.timerMoved === true)
|
|
637
|
-
found.push(`timer: declared the spec's lifetime, read back an end of ${machine.endsAt instanceof Date ? machine.endsAt.toISOString() : "nothing"}, later than create set it`);
|
|
638
684
|
return found;
|
|
639
685
|
}
|
|
640
686
|
/**
|
|
641
687
|
* Everything between the vendor's answer and a computer that passed the read-back, whose value is what the read-back
|
|
642
688
|
* recorded that does not refuse it (notes).
|
|
643
689
|
*/
|
|
644
|
-
async #prepare(spec, ref, { labels, daemon, sha256, commands, user
|
|
645
|
-
const { uploadMs, execMs
|
|
690
|
+
async #prepare(spec, ref, { labels, daemon, sha256, commands, user }, signal) {
|
|
691
|
+
const { uploadMs, execMs } = this.#timeouts;
|
|
646
692
|
const running = await this.#running(ref, signal);
|
|
647
693
|
if (!running.ok)
|
|
648
694
|
return running;
|
|
649
|
-
|
|
650
|
-
const prepare = this.host.prepare.bind(this.host);
|
|
651
|
-
const prepared = await this.#call(`${this.#label}'s settings of ${ref.name}`, (ctx) => prepare(ref.id, ctx), requestMs, signal);
|
|
652
|
-
if (!prepared.ok)
|
|
653
|
-
return prepared;
|
|
654
|
-
}
|
|
655
|
-
const drift = this.#machineDrift(spec, running.value, labels);
|
|
695
|
+
const drift = this.#machineDrift(running.value, labels);
|
|
656
696
|
if (drift.length > 0)
|
|
657
697
|
return this.#fail("refused", `it did not match its spec: ${drift.join("; ")}`);
|
|
698
|
+
// Before anything of this create's is in the machine: what root runs is root's alone (the Archil helper's sha256,
|
|
699
|
+
// read at the mount's preflight, is read after this).
|
|
700
|
+
if ((this.host.facts.rootOnly ?? []).length > 0) {
|
|
701
|
+
const closed = await runToExit(this.host, ref.id, commands.rootOnly, { ms: execMs, ...(signal ? { signal } : {}), what: `making system paths root's alone in ${ref.name}` });
|
|
702
|
+
if (!closed.ok)
|
|
703
|
+
return closed;
|
|
704
|
+
if (closed.value.exitCode !== 0)
|
|
705
|
+
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)}`);
|
|
706
|
+
}
|
|
658
707
|
const staging = commands.install[4];
|
|
659
708
|
const bytes = new Uint8Array(daemon.buffer, daemon.byteOffset, daemon.byteLength);
|
|
660
709
|
const upload = this.host.upload?.bind(this.host);
|
|
@@ -677,7 +726,9 @@ export class HostedProvider {
|
|
|
677
726
|
// The run's folder, after the daemon is in place (serve runs it) and before init starts: no tool program runs before
|
|
678
727
|
// it is mounted. A folder mounted for a computer create then gives up on is let go before the delete, never signalled.
|
|
679
728
|
const started = async () => {
|
|
680
|
-
const
|
|
729
|
+
const gpu = spec.gpu !== undefined;
|
|
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);
|
|
681
732
|
return done.ok && installer !== undefined ? ok([installer, ...done.value]) : done;
|
|
682
733
|
};
|
|
683
734
|
if (spec.mount?.kind !== "archil")
|
|
@@ -701,7 +752,17 @@ export class HostedProvider {
|
|
|
701
752
|
}
|
|
702
753
|
/** The rest of #prepare from the daemon's install on: init started, its first connection checked, the listing waited for. */
|
|
703
754
|
async #start(spec, ref, commands, want, signal) {
|
|
755
|
+
const closed = [];
|
|
704
756
|
if (this.host.facts.daemon === "child") {
|
|
757
|
+
// The session buses in /tmp, closed to the computer's user before any program of its runs.
|
|
758
|
+
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
|
+
if (!shut.ok)
|
|
760
|
+
return shut;
|
|
761
|
+
if (shut.value.exitCode !== 0)
|
|
762
|
+
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)}`);
|
|
763
|
+
const found = closedSocketsOf(shut.value.stdout);
|
|
764
|
+
if (found.length > 0)
|
|
765
|
+
closed.push(`session buses in /tmp closed to the computer's user: ${found.join(", ")}`);
|
|
705
766
|
const init = await runToExit(this.host, ref.id, commands.start, { ms: this.#timeouts.execMs, ...(signal ? { signal } : {}), what: `starting the daemon in ${ref.name}` });
|
|
706
767
|
if (!init.ok)
|
|
707
768
|
return init;
|
|
@@ -719,7 +780,7 @@ export class HostedProvider {
|
|
|
719
780
|
try {
|
|
720
781
|
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;
|
|
721
782
|
const expected = archil === undefined ? undefined : { ...archil, expected: readBackOf({ ...archil.mount, token: "" }, archil.epoch) };
|
|
722
|
-
const inside = await this.#checkInside(opened.value, ref.name, spec.network, true, want, signal, expected);
|
|
783
|
+
const inside = await this.#checkInside(opened.value, ref.name, networkOfForm(formOf(spec.network)), true, want, signal, expected);
|
|
723
784
|
if (!inside.ok)
|
|
724
785
|
return inside;
|
|
725
786
|
notes = inside.value;
|
|
@@ -728,7 +789,7 @@ export class HostedProvider {
|
|
|
728
789
|
await opened.value.close();
|
|
729
790
|
}
|
|
730
791
|
const listed = await this.#waitListed(ref, signal);
|
|
731
|
-
return listed.ok ? ok(notes) : listed;
|
|
792
|
+
return listed.ok ? ok([...closed, ...notes]) : listed;
|
|
732
793
|
}
|
|
733
794
|
/** What init wrote to its log (child) or the vendor's words of the machine (pid1): the end of it, or nothing. */
|
|
734
795
|
async #initWords(ref) {
|
|
@@ -753,7 +814,7 @@ export class HostedProvider {
|
|
|
753
814
|
async #checkInside(connection, name, network, starting, want, signal, archil) {
|
|
754
815
|
let current = connection;
|
|
755
816
|
if (starting && this.#desktop) {
|
|
756
|
-
const shown = await startDisplay(connection.client, this.#desktop, { version: VERSION, vendor: this.id, signal });
|
|
817
|
+
const shown = await startDisplay(connection.client, this.#desktop, { version: VERSION, vendor: this.id, signal, windowManager: this.host.facts.windowManager === true });
|
|
757
818
|
if (!shown.ok)
|
|
758
819
|
return shown;
|
|
759
820
|
const hello = await connection.client.call("hello", { client: `@parcha/agentrun-computer ${VERSION}` }, { signal: bounded(this.#timeouts.helloMs, signal) });
|
|
@@ -767,19 +828,17 @@ export class HostedProvider {
|
|
|
767
828
|
const drift = [...found.value];
|
|
768
829
|
const notes = [];
|
|
769
830
|
let user;
|
|
770
|
-
if (network === "
|
|
771
|
-
const
|
|
772
|
-
if (!
|
|
773
|
-
return
|
|
774
|
-
user = `${
|
|
775
|
-
drift.push(...
|
|
776
|
-
|
|
777
|
-
drift.push(...runtimeDrift(want.runtime, probe.value.kernel));
|
|
778
|
-
notes.push(...probeNotes(probe.value, network));
|
|
831
|
+
if (typeof network === "string" || !("other" in network)) {
|
|
832
|
+
const read = await this.#networkDrift(current, name, network, signal);
|
|
833
|
+
if (!read.ok)
|
|
834
|
+
return read;
|
|
835
|
+
user = `${read.value.probe.uid}:${read.value.probe.gid}`;
|
|
836
|
+
drift.push(...read.value.drift, ...runtimeDrift(want, read.value.probe.kernel));
|
|
837
|
+
notes.push(...probeNotes(read.value.probe, network));
|
|
779
838
|
}
|
|
780
839
|
// Which way init fenced its programs, as hello read it back: said once a way other than the plain one was taken.
|
|
781
840
|
if (starting && current.hello.fence === "userns-netns") {
|
|
782
|
-
notes.push(`fence: userns-netns: init made its programs' network namespace inside a user namespace of its own, as root on ${want.
|
|
841
|
+
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`);
|
|
783
842
|
}
|
|
784
843
|
const said = user === undefined ? {} : { user };
|
|
785
844
|
if (archil === undefined)
|
|
@@ -809,9 +868,11 @@ export class HostedProvider {
|
|
|
809
868
|
const [uid, gid] = user === undefined ? [] : user.split(":");
|
|
810
869
|
return { version: VERSION, ...(uid === undefined || gid === undefined ? {} : { uid, gid }), arch: this.host.facts.arch, kind, groups };
|
|
811
870
|
}
|
|
812
|
-
/** One stdio bridge into the machine, after hello, unchecked. */
|
|
813
|
-
#open(ref, signal, helloMs = this.#timeouts.helloMs) {
|
|
814
|
-
|
|
871
|
+
/** One stdio bridge into the machine, after hello, unchecked; its client says so once the machine is deleted. */
|
|
872
|
+
async #open(ref, signal, helloMs = this.#timeouts.helloMs) {
|
|
873
|
+
const { requestMs } = this.#timeouts;
|
|
874
|
+
const opened = await openBridge(this.host, ref.id, { argv: hostedCommands().stdio, name: ref.name, version: VERSION, requestMs, helloMs, ...(signal ? { signal } : {}) });
|
|
875
|
+
return opened.ok ? ok(deletedSaid(this.host, ref, this.#label, requestMs, opened.value)) : opened;
|
|
815
876
|
}
|
|
816
877
|
/** #open, tried again while the bridge finds no daemon listening, until hello or helloMs in all. */
|
|
817
878
|
async #openWhenListening(ref, signal) {
|
|
@@ -823,10 +884,44 @@ export class HostedProvider {
|
|
|
823
884
|
await pause(LISTEN_RETRY_MS, signal);
|
|
824
885
|
}
|
|
825
886
|
}
|
|
826
|
-
/**
|
|
827
|
-
|
|
887
|
+
/**
|
|
888
|
+
* Its tools' network as they meet it, against the one create made: the probe, as the computer's own user. Under none,
|
|
889
|
+
* no target answers. Under open or a list, still none answers directly (their own loopback alone), init's relay
|
|
890
|
+
* answers (the one way out, to the proxy), and the proxy's policy beside its socket holds the form create asked for.
|
|
891
|
+
*/
|
|
892
|
+
async #networkDrift(connection, name, network, signal) {
|
|
893
|
+
if (network === "none") {
|
|
894
|
+
const probe = await this.#probe(connection, name, signal);
|
|
895
|
+
return probe.ok ? ok({ drift: probeNetworkDrift(probe.value), probe: probe.value }) : probe;
|
|
896
|
+
}
|
|
897
|
+
const probe = await this.#probe(connection, name, signal, [...this.#probeTargets, EGRESS_RELAY]);
|
|
898
|
+
if (!probe.ok)
|
|
899
|
+
return probe;
|
|
900
|
+
const form = formOf(network);
|
|
901
|
+
const drift = [];
|
|
902
|
+
const direct = probe.value.reached.filter((target) => target !== EGRESS_RELAY);
|
|
903
|
+
if (direct.length > 0)
|
|
904
|
+
drift.push(`network: declared ${form} through the egress proxy alone, read back a direct answer from ${direct.join(", ")}`);
|
|
905
|
+
if (!probe.value.reached.includes(EGRESS_RELAY))
|
|
906
|
+
drift.push(`network: declared ${form} through the egress proxy, and init's relay at ${EGRESS_RELAY} did not answer from inside`);
|
|
907
|
+
const { helloMs } = this.#timeouts;
|
|
908
|
+
const policy = await connection.client.call("files.read", { path: EGRESS_POLICY }, { signal: bounded(helloMs, signal) });
|
|
909
|
+
if (!policy.ok) {
|
|
910
|
+
if (policy.error.code === "aborted")
|
|
911
|
+
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)}`);
|
|
912
|
+
drift.push(`network: declared ${form}, and the egress proxy's policy ${EGRESS_POLICY} could not be read: ${policy.error.message}`);
|
|
913
|
+
}
|
|
914
|
+
else {
|
|
915
|
+
const served = Buffer.from(policy.value.data, "base64").toString("utf8");
|
|
916
|
+
if (served !== `${form}\n`)
|
|
917
|
+
drift.push(`network: declared ${form}, the egress proxy serves ${show(served.trim().slice(0, 300))}`);
|
|
918
|
+
}
|
|
919
|
+
return ok({ drift, probe: probe.value });
|
|
920
|
+
}
|
|
921
|
+
/** `computerd probe` through the daemon, so it runs as the computer's own user: to targets, the configured ones by default. */
|
|
922
|
+
async #probe(connection, name, signal, targets = this.#probeTargets) {
|
|
828
923
|
const { probeMs } = this.#timeouts;
|
|
829
|
-
const argv = [DAEMON_IN_COMPUTER, "probe", ...
|
|
924
|
+
const argv = [DAEMON_IN_COMPUTER, "probe", ...targets.flatMap((target) => ["--connect", target])];
|
|
830
925
|
const ran = await connection.client.call("exec.run", { argv, timeoutMs: probeMs }, { signal: bounded(probeMs + CALL_SLACK_MS, signal) });
|
|
831
926
|
if (!ran.ok) {
|
|
832
927
|
if (signal?.aborted)
|
|
@@ -852,7 +947,7 @@ export class HostedProvider {
|
|
|
852
947
|
const until = Date.now() + listedMs;
|
|
853
948
|
let since;
|
|
854
949
|
for (let hits = 0;;) {
|
|
855
|
-
const listed = await this
|
|
950
|
+
const listed = await listedBy(this.host, "the listing by name", byName(ref.name), listMs, signal);
|
|
856
951
|
if (!listed.ok)
|
|
857
952
|
return listed;
|
|
858
953
|
if (listed.value.some((machine) => machine.id === ref.id)) {
|
|
@@ -874,7 +969,7 @@ export class HostedProvider {
|
|
|
874
969
|
async #own(ref, signal) {
|
|
875
970
|
const found = await this.#get(ref.id, signal);
|
|
876
971
|
if (!found.ok)
|
|
877
|
-
return found
|
|
972
|
+
return found;
|
|
878
973
|
if (found.value === undefined)
|
|
879
974
|
return this.#fail("not_found", `no ${this.#label} computer is ${ref.name} (${ref.id})`);
|
|
880
975
|
if (!ours(found.value.labels, ref.name))
|
|
@@ -900,6 +995,9 @@ export class HostedProvider {
|
|
|
900
995
|
const found = await this.#own(ref);
|
|
901
996
|
if (!found.ok)
|
|
902
997
|
return found;
|
|
998
|
+
// A machine on its way out (past its deadline or released, the vendor still deleting it) is gone, not merely stopped.
|
|
999
|
+
if (found.value.state === "gone")
|
|
1000
|
+
return this.#fail("gone", `${ref.name} passed its deadline or was released: it is going`);
|
|
903
1001
|
if (found.value.state !== "running")
|
|
904
1002
|
return this.#fail("refused", `${ref.name} is ${found.value.state}, not running: its daemon does not run`);
|
|
905
1003
|
const opened = await this.#open(ref);
|
|
@@ -923,7 +1021,7 @@ export class HostedProvider {
|
|
|
923
1021
|
return this.#fail("invalid", `${String(id)} is not ${a(this.#label)} machine id`);
|
|
924
1022
|
const found = await this.#get(id, signal);
|
|
925
1023
|
if (!found.ok)
|
|
926
|
-
return found
|
|
1024
|
+
return found;
|
|
927
1025
|
if (found.value === undefined || !ours(found.value.labels))
|
|
928
1026
|
return this.#fail("not_found", `no ${this.#label} computer has the id ${id}`);
|
|
929
1027
|
return this.#attached(found.value, signal);
|
|
@@ -959,20 +1057,12 @@ export class HostedProvider {
|
|
|
959
1057
|
if (deadline === undefined)
|
|
960
1058
|
found.push(`deadline: the label ${DEADLINE_LABEL} reads ${show(machine.labels[DEADLINE_LABEL])}, not the ISO time create writes`);
|
|
961
1059
|
const archil = recordedArchil(machine.labels);
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
const
|
|
966
|
-
const
|
|
967
|
-
// The size the vendor reads back is held to check() where the vendor takes it in a spec, and reported either way.
|
|
968
|
-
const taken = new Set(this.host.facts.specFields ?? []);
|
|
969
|
-
const cpus = machine.size?.cpus;
|
|
970
|
-
const memoryMb = machine.size?.memoryMb;
|
|
971
|
-
const sized = { ...(taken.has("cpus") && cpus !== undefined ? { cpus } : {}), ...(taken.has("memoryMb") && memoryMb !== undefined ? { memoryMb } : {}) };
|
|
972
|
-
const spec = { image, lifetimeMs: 1, network: typeof network === "object" ? "open" : network, labels: callerLabels(machine.labels), ...sized };
|
|
1060
|
+
// Its tools' network as create recorded it, read from inside (#checkInside: the probe, and the proxy's policy):
|
|
1061
|
+
// no vendor's read of it is taken.
|
|
1062
|
+
const network = networkOfForm(machine.labels[NETWORK_LABEL]);
|
|
1063
|
+
const image = machine.labels[IMAGE_LABEL] ?? "";
|
|
1064
|
+
const spec = { image, lifetimeMs: 1, network, labels: callerLabels(machine.labels) };
|
|
973
1065
|
found.push(...this.check(spec).map(({ field, message }) => `${field}: ${message}, read back from the computer`));
|
|
974
|
-
// The vendor's own create options read back, as create holds them (#machineDrift).
|
|
975
|
-
found.push(...(machine.drift ?? []));
|
|
976
1066
|
let kind;
|
|
977
1067
|
let notes = [];
|
|
978
1068
|
let mount;
|
|
@@ -984,7 +1074,7 @@ export class HostedProvider {
|
|
|
984
1074
|
try {
|
|
985
1075
|
// A host that takes a spec's user made this computer's daemon run as the one its spec named: read back, never root
|
|
986
1076
|
// (an Archil computer's is the tool user); every other runs as the provider's daemon user.
|
|
987
|
-
const want = this.#takesUser
|
|
1077
|
+
const want = this.#takesUser ? (archil === undefined ? {} : { user: ARCHIL_USER }) : { user: `${this.#uid}:${this.#gid}` };
|
|
988
1078
|
const inside = await this.#checkInside(opened.value, name, network, false, want, signal, archil);
|
|
989
1079
|
if (!inside.ok && inside.error.code !== "refused")
|
|
990
1080
|
return inside;
|
|
@@ -1009,49 +1099,41 @@ export class HostedProvider {
|
|
|
1009
1099
|
...ref,
|
|
1010
1100
|
spec: {
|
|
1011
1101
|
image,
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
network: spec.network,
|
|
1015
|
-
...(cpus === undefined ? {} : { cpus }),
|
|
1016
|
-
...(memoryMb === undefined ? {} : { memoryMb }),
|
|
1102
|
+
user: (this.#takesUser ? user : undefined) ?? `${this.#uid}:${this.#gid}`,
|
|
1103
|
+
network,
|
|
1017
1104
|
workdir: WORK,
|
|
1018
1105
|
...(deadline === undefined ? {} : { deadline }),
|
|
1019
1106
|
...(kind === undefined ? {} : { kind }),
|
|
1020
1107
|
...(mount === undefined ? {} : { mount }),
|
|
1021
|
-
...(machine.
|
|
1108
|
+
...gpuOf(machine.labels[GPU_LABEL]),
|
|
1022
1109
|
},
|
|
1023
1110
|
...(notes.length > 0 ? { notes } : {}),
|
|
1024
1111
|
});
|
|
1025
1112
|
}
|
|
1026
1113
|
/**
|
|
1027
|
-
* Settings' test:
|
|
1028
|
-
* unauthorized, a vendor not reached unreachable, the message never carrying a key.
|
|
1114
|
+
* Settings' test: one list by the adapter's label, its computers counted as the listing reads them (no get of each),
|
|
1115
|
+
* which makes nothing; a refused key is unauthorized, a vendor not reached unreachable, the message never carrying a key.
|
|
1029
1116
|
*/
|
|
1030
1117
|
async testConnection({ signal } = {}) {
|
|
1031
|
-
const
|
|
1032
|
-
|
|
1033
|
-
if (tested)
|
|
1034
|
-
return countTestedAs(this.#label, await this.#call(`${this.#label}'s test`, (ctx) => tested(ctx), requestMs, signal));
|
|
1035
|
-
const listed = await this.#call("the listing", (ctx) => this.host.list({ [COMPUTER_LABEL]: "1" }, ctx), listMs, signal);
|
|
1036
|
-
return countTested(this.#label, listed.ok ? ok({ count: listed.value.filter((machine) => ours(machine.labels)).length }) : listed);
|
|
1118
|
+
const listed = await listedBy(this.host, "the listing", { [COMPUTER_LABEL]: "1" }, this.#timeouts.listMs, signal);
|
|
1119
|
+
return countTested(this.#label, listed.ok ? ok({ count: listed.value.filter((machine) => ours(machine.labels) && alive(machine.state)).length }) : listed);
|
|
1037
1120
|
}
|
|
1038
1121
|
/**
|
|
1039
|
-
* Every computer this provider made that carries all of these labels, running or stopped
|
|
1040
|
-
*
|
|
1122
|
+
* Every computer this provider made that carries all of these labels, running or stopped: the listing finds them, and
|
|
1123
|
+
* a get of each reads its state, labels and end (foundBy). Its network is the one its label records, as create made
|
|
1124
|
+
* it (none without one); attach and network() read it from inside.
|
|
1041
1125
|
*/
|
|
1042
1126
|
async list(labels, { signal } = {}) {
|
|
1043
1127
|
const problem = filterProblem(labels);
|
|
1044
1128
|
if (problem)
|
|
1045
1129
|
return this.#fail("invalid", problem);
|
|
1046
1130
|
const { listMs } = this.#timeouts;
|
|
1047
|
-
const listed = await this
|
|
1131
|
+
const listed = await foundBy(this.host, "the listing", { ...labels, [COMPUTER_LABEL]: "1" }, this.#timeouts, signal);
|
|
1048
1132
|
if (!listed.ok)
|
|
1049
1133
|
return listed.error.code === "timeout" ? this.#fail("timeout", `list did not finish within ${seconds(listMs)}: ${listed.error.message}`) : listed;
|
|
1050
1134
|
const infos = [];
|
|
1051
1135
|
for (const machine of listed.value) {
|
|
1052
|
-
if (!ours(machine.labels) ||
|
|
1053
|
-
continue;
|
|
1054
|
-
if (machine.state !== "running" && machine.state !== "stopped")
|
|
1136
|
+
if (!ours(machine.labels) || (machine.state !== "running" && machine.state !== "stopped"))
|
|
1055
1137
|
continue;
|
|
1056
1138
|
// list runs nothing in a computer: an Archil computer's mount is the one its label records, under its epoch label.
|
|
1057
1139
|
const archil = recordedArchil(machine.labels);
|
|
@@ -1060,65 +1142,52 @@ export class HostedProvider {
|
|
|
1060
1142
|
labels: { ...machine.labels },
|
|
1061
1143
|
deadline: deadlineOf(machine),
|
|
1062
1144
|
state: machine.state,
|
|
1063
|
-
network:
|
|
1145
|
+
network: networkOfForm(machine.labels[NETWORK_LABEL]),
|
|
1064
1146
|
...(archil === undefined ? {} : { mount: archil.expected }),
|
|
1065
1147
|
});
|
|
1066
1148
|
}
|
|
1067
1149
|
return ok(infos);
|
|
1068
1150
|
}
|
|
1069
|
-
/** The
|
|
1151
|
+
/** The probe and the proxy's policy read the network back from inside. A change is refused: it is fixed at create. */
|
|
1070
1152
|
async network(ref, policy, { signal } = {}) {
|
|
1071
1153
|
const problem = this.#refProblem(ref);
|
|
1072
1154
|
if (problem)
|
|
1073
1155
|
return this.#fail("invalid", problem);
|
|
1074
1156
|
const fixed = `${a(this.#label)} computer's network is fixed at create: create a new computer`;
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
return this.#fail("refused",
|
|
1078
|
-
}
|
|
1157
|
+
const refused = policy === undefined ? undefined : networkRefusal(this.#label, this.capabilities().network, policy);
|
|
1158
|
+
if (refused)
|
|
1159
|
+
return this.#fail("refused", `${refused}; ${fixed}`);
|
|
1079
1160
|
const found = await this.#own(ref, signal);
|
|
1080
1161
|
if (!found.ok)
|
|
1081
1162
|
return found;
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
//
|
|
1085
|
-
//
|
|
1086
|
-
|
|
1087
|
-
|
|
1163
|
+
const recorded = networkOfForm(found.value.labels[NETWORK_LABEL]);
|
|
1164
|
+
let read = recorded;
|
|
1165
|
+
// Its tools' network is read from inside, as the tool user, whatever the vendor's network is (an Archil computer's
|
|
1166
|
+
// root reaches Archil, an open one's proxy the internet): the daemon's own network namespace's live interfaces are
|
|
1167
|
+
// lo alone, none can be raised (computerd probe), no probe target answers directly, and an open or listed one's
|
|
1168
|
+
// relay and proxy serve what its label records.
|
|
1169
|
+
if (found.value.state === "running" && (typeof recorded === "string" || !("other" in recorded))) {
|
|
1088
1170
|
const opened = await this.#open(ref, signal);
|
|
1089
1171
|
if (!opened.ok)
|
|
1090
1172
|
return opened;
|
|
1091
1173
|
try {
|
|
1092
|
-
const
|
|
1174
|
+
const own = await readOwnNetwork(opened.value.client, this.#timeouts.helloMs, bounded(this.#timeouts.helloMs, signal));
|
|
1175
|
+
if (!own.ok)
|
|
1176
|
+
return this.#fail(own.error.code, `${ref.name}: ${own.error.message}`);
|
|
1177
|
+
const drift = await this.#networkDrift(opened.value, ref.name, recorded, signal);
|
|
1093
1178
|
if (!drift.ok)
|
|
1094
|
-
return
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
if (!probe.ok)
|
|
1099
|
-
return probe;
|
|
1100
|
-
if (probe.value.reached.length > 0)
|
|
1101
|
-
read = { other: `its tools' network was created none, yet from inside an answer came back from ${probe.value.reached.join(", ")}` };
|
|
1179
|
+
return drift;
|
|
1180
|
+
const found = [...own.value, ...drift.value.drift];
|
|
1181
|
+
if (found.length > 0)
|
|
1182
|
+
read = { other: `its tools' network was created ${policyWords(recorded)}, yet from inside: ${found.join("; ")}` };
|
|
1102
1183
|
}
|
|
1103
1184
|
finally {
|
|
1104
1185
|
await opened.value.close();
|
|
1105
1186
|
}
|
|
1106
1187
|
}
|
|
1107
|
-
if (policy === undefined || policy
|
|
1188
|
+
if (policy === undefined || isDeepStrictEqual(networkOfForm(formOf(policy)), read))
|
|
1108
1189
|
return ok(read);
|
|
1109
|
-
return this.#fail("refused", `${fixed}: ${ref.name} reads back network ${policyWords(read)}, not ${policy}`);
|
|
1110
|
-
}
|
|
1111
|
-
/**
|
|
1112
|
-
* The tools' network as the vendor's policy shows it: the vendor's own (none where it reads none back), except for an
|
|
1113
|
-
* Archil computer, whose open vendor network is root's, to reach Archil: its tools' network is init's own, none.
|
|
1114
|
-
*/
|
|
1115
|
-
#toolsNetwork(machine) {
|
|
1116
|
-
const vendor = this.host.facts.networkReadBack ? (machine.network ?? { other: `${this.#label} read no network back` }) : "none";
|
|
1117
|
-
if (recordedArchil(machine.labels) === undefined)
|
|
1118
|
-
return vendor;
|
|
1119
|
-
if (vendor === "open" || !this.host.facts.networkReadBack)
|
|
1120
|
-
return "none";
|
|
1121
|
-
return { other: `an Archil computer whose ${this.#label} network reads ${policyWords(vendor)}, not the open one create makes for root to reach Archil` };
|
|
1190
|
+
return this.#fail("refused", `${fixed}: ${ref.name} reads back network ${policyWords(read)}, not ${policyWords(policy)}`);
|
|
1122
1191
|
}
|
|
1123
1192
|
/** One extend of a computer at a time, each reading the deadline the one before it left. */
|
|
1124
1193
|
async #extendSerial(ref, ttlMs, signal) {
|
|
@@ -1140,9 +1209,9 @@ export class HostedProvider {
|
|
|
1140
1209
|
}
|
|
1141
1210
|
}
|
|
1142
1211
|
/**
|
|
1143
|
-
* Moves the deadline to now + ttlMs, refusing one earlier than it is (before any vendor call), never past create's time
|
|
1144
|
-
* the
|
|
1145
|
-
*
|
|
1212
|
+
* Moves the deadline to now + ttlMs, refusing one earlier than it is (before any vendor call), never past create's time
|
|
1213
|
+
* 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.
|
|
1146
1215
|
*/
|
|
1147
1216
|
async #extend(ref, ttlMs, signal) {
|
|
1148
1217
|
const found = await this.#own(ref, signal);
|
|
@@ -1165,28 +1234,17 @@ export class HostedProvider {
|
|
|
1165
1234
|
}
|
|
1166
1235
|
if (current !== undefined && target === current.getTime())
|
|
1167
1236
|
return ok({ deadline: current });
|
|
1168
|
-
const
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
}
|
|
1180
|
-
end = after.value.endsAt;
|
|
1181
|
-
}
|
|
1182
|
-
else {
|
|
1183
|
-
const setLabels = this.host.setLabels?.bind(this.host);
|
|
1184
|
-
if (!setLabels)
|
|
1185
|
-
return this.#fail("unsupported", `${a(this.#label)} computer's deadline cannot move`);
|
|
1186
|
-
end = new Date(target);
|
|
1187
|
-
const moved = await this.#call(`the deadline label of ${ref.name}`, (ctx) => setLabels(ref.id, { [DEADLINE_LABEL]: end.toISOString() }, ctx), requestMs, signal);
|
|
1188
|
-
if (!moved.ok)
|
|
1189
|
-
return this.#fail(moved.error.code, `the deadline label did not move, so nothing did: ${moved.error.message}`);
|
|
1237
|
+
const extend = this.host.extend?.bind(this.host);
|
|
1238
|
+
if (!extend)
|
|
1239
|
+
return this.#fail("unsupported", `${a(this.#label)} computer's deadline cannot move`);
|
|
1240
|
+
const moved = await this.#call(`${this.#label}'s end of ${ref.name}`, (ctx) => extend(ref.id, new Date(target), ctx), this.#timeouts.requestMs, signal);
|
|
1241
|
+
if (!moved.ok)
|
|
1242
|
+
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}`);
|
|
1243
|
+
const after = await this.#get(ref.id, signal);
|
|
1244
|
+
const end = after.ok && after.value !== undefined ? deadlineOf(after.value) : undefined;
|
|
1245
|
+
if (end === undefined) {
|
|
1246
|
+
const why = !after.ok ? after.error.message : after.value === undefined ? `${ref.name} is gone` : `${ref.name} reads back no deadline`;
|
|
1247
|
+
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`);
|
|
1190
1248
|
}
|
|
1191
1249
|
const opened = await this.#open(ref, signal);
|
|
1192
1250
|
if (!opened.ok)
|
|
@@ -1207,12 +1265,12 @@ export class HostedProvider {
|
|
|
1207
1265
|
/**
|
|
1208
1266
|
* delete reads the machine back to check it is this provider's, then deletes it, both never signalled, so a release
|
|
1209
1267
|
* after an abort, or one aborted during the read, still sends the delete; it is idempotent: one the vendor no longer
|
|
1210
|
-
* has is ok. The vendor answers a delete before the machine is gone, so release then waits (#settle) until
|
|
1211
|
-
*
|
|
1268
|
+
* has is ok. The vendor answers a delete before the machine is gone, so release then waits (#settle) until get
|
|
1269
|
+
* answers none (twice running), within goneMs and the caller's signal, the one step the signal ends. A running
|
|
1212
1270
|
* Archil computer lets go of the run's folder first (archilRelease: the final sync through computerd, then checkin as
|
|
1213
1271
|
* root); the delete runs whatever that answers. A folder not let go cleanly loses nothing (every call that changed it
|
|
1214
1272
|
* synced, and the next claim revokes the hold), so it is said through onNote, and the release succeeds once the
|
|
1215
|
-
* machine is gone.
|
|
1273
|
+
* machine is gone. A computer here is deleted, never stopped: release stop is refused in words.
|
|
1216
1274
|
*/
|
|
1217
1275
|
async release(ref, how, { signal } = {}) {
|
|
1218
1276
|
const problem = this.#refProblem(ref);
|
|
@@ -1220,35 +1278,39 @@ export class HostedProvider {
|
|
|
1220
1278
|
return this.#fail("invalid", problem);
|
|
1221
1279
|
if (how === "detach")
|
|
1222
1280
|
return this.#fail("unsupported", `the ${this.#label} provider keeps no computer for an agent (it declares no persistence), so release detach is refused: release it with delete`);
|
|
1223
|
-
if (how
|
|
1224
|
-
return this.#fail("
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
return this.#fail("unsupported", `${a(this.#label)} computer has no stop that this provider declares: release it with delete`);
|
|
1281
|
+
if (how === "stop")
|
|
1282
|
+
return this.#fail("unsupported", `${a(this.#label)} computer is deleted, never stopped and kept: release it with delete`);
|
|
1283
|
+
if (how !== "delete")
|
|
1284
|
+
return this.#fail("invalid", `release is "delete", not ${String(how)}`);
|
|
1228
1285
|
const found = await this.#get(ref.id);
|
|
1229
1286
|
if (!found.ok)
|
|
1230
|
-
return found
|
|
1287
|
+
return found;
|
|
1231
1288
|
if (found.value === undefined)
|
|
1232
1289
|
return ok(undefined);
|
|
1233
1290
|
if (!ours(found.value.labels, ref.name))
|
|
1234
1291
|
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`);
|
|
1235
|
-
|
|
1236
|
-
const letGo
|
|
1237
|
-
let done;
|
|
1238
|
-
if (how === "stop" && stop)
|
|
1239
|
-
done = await this.#call(`the stop of ${ref.name}`, (ctx) => stop(ref.id, ctx), this.#timeouts.goneMs, signal);
|
|
1240
|
-
else {
|
|
1241
|
-
this.#end(ref.name);
|
|
1242
|
-
const killed = await this.#kill(ref.id);
|
|
1243
|
-
done = killed.ok ? await this.#settle(ref, signal) : killed;
|
|
1244
|
-
}
|
|
1292
|
+
this.#end(ref.name);
|
|
1293
|
+
const { letGo, gone: done } = await this.#remove(ref, found.value, signal);
|
|
1245
1294
|
if (letGo.ok)
|
|
1246
1295
|
return done;
|
|
1247
1296
|
if (!done.ok)
|
|
1248
1297
|
return this.#fail(done.error.code, `${done.error.message}; and ${letGo.error.message}`);
|
|
1249
|
-
this.#onNote?.({ computer: ref.name, note: `${ref.name} was
|
|
1298
|
+
this.#onNote?.({ computer: ref.name, note: `${ref.name} was deleted, and ${letGo.error.message}` });
|
|
1250
1299
|
return done;
|
|
1251
1300
|
}
|
|
1301
|
+
/**
|
|
1302
|
+
* The one delete: machine's folder let go first (where it is a running Archil computer, as read; none for undefined),
|
|
1303
|
+
* then the host's delete, then the wait until get answers none for it. The callers word what failed.
|
|
1304
|
+
*/
|
|
1305
|
+
async #remove(ref, machine, signal) {
|
|
1306
|
+
const letGo = await this.#letGoOf(ref, machine, signal);
|
|
1307
|
+
const killed = await this.#kill(ref.id);
|
|
1308
|
+
return { letGo, gone: killed.ok ? await this.#settle(ref, signal) : killed };
|
|
1309
|
+
}
|
|
1310
|
+
/** #letGo where machine, as read, is a running Archil computer: a stopped one holds no folder. */
|
|
1311
|
+
#letGoOf(ref, machine, signal) {
|
|
1312
|
+
return machine !== undefined && recordedArchil(machine.labels) !== undefined && machine.state === "running" ? this.#letGo(ref, signal) : Promise.resolve(ok(undefined));
|
|
1313
|
+
}
|
|
1252
1314
|
/** A running Archil computer's folder let go: the final sync through a connection when it answers, then checkin as root. */
|
|
1253
1315
|
async #letGo(ref, signal) {
|
|
1254
1316
|
const opened = await this.#open(ref, signal);
|
|
@@ -1260,46 +1322,144 @@ export class HostedProvider {
|
|
|
1260
1322
|
await opened.value.close();
|
|
1261
1323
|
}
|
|
1262
1324
|
}
|
|
1263
|
-
/**
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
* to 5 s, bounded by goneMs and the caller's signal; a failed read ends the wait with that error.
|
|
1267
|
-
*/
|
|
1268
|
-
async #settle(ref, signal) {
|
|
1269
|
-
const settled = await settleGone(this.host, ref, { ...this.#timeouts, ...(signal ? { signal } : {}) });
|
|
1270
|
-
return settled;
|
|
1325
|
+
/** Waits until the vendor reads the machine gone after its delete (settle). */
|
|
1326
|
+
#settle(ref, signal) {
|
|
1327
|
+
return settle(this.host, ref, { ...this.#timeouts, ...(signal ? { signal } : {}) });
|
|
1271
1328
|
}
|
|
1272
1329
|
}
|
|
1330
|
+
/** The labels a listing by one computer's name asks for. */
|
|
1331
|
+
const byName = (name) => ({ [COMPUTER_LABEL]: "1", [NAME_LABEL]: name });
|
|
1332
|
+
/**
|
|
1333
|
+
* 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).
|
|
1335
|
+
*/
|
|
1336
|
+
export async function listedBy(host, what, labels, ms, signal) {
|
|
1337
|
+
const listed = await hostCall(host, what, (ctx) => host.list(labels, ctx), ms, signal);
|
|
1338
|
+
return listed.ok ? ok(listed.value.filter((machine) => carries(machine, labels))) : listed;
|
|
1339
|
+
}
|
|
1340
|
+
/** Whether a machine carries every one of these labels. */
|
|
1341
|
+
const carries = (machine, labels) => Object.entries(labels).every(([key, value]) => machine.labels?.[key] === value);
|
|
1273
1342
|
/**
|
|
1274
|
-
*
|
|
1275
|
-
*
|
|
1343
|
+
* The listing finds, get answers (MachineHost.list): the machines the host's listing by labels names, each read now by
|
|
1344
|
+
* readMachine, within requestMs. One the vendor no longer has (not found) is dropped, and so is one whose get carries
|
|
1345
|
+
* not every label. A read that fails otherwise fails the whole.
|
|
1276
1346
|
*/
|
|
1277
|
-
export async function
|
|
1278
|
-
const
|
|
1347
|
+
export async function foundBy(host, what, labels, { listMs, requestMs }, signal) {
|
|
1348
|
+
const listed = await listedBy(host, what, labels, listMs, signal);
|
|
1349
|
+
if (!listed.ok)
|
|
1350
|
+
return listed;
|
|
1351
|
+
const reads = await Promise.all(listed.value.map(({ id }) => readMachine(host, id, requestMs, signal)));
|
|
1352
|
+
const found = [];
|
|
1353
|
+
for (const read of reads) {
|
|
1354
|
+
if (!read.ok)
|
|
1355
|
+
return read;
|
|
1356
|
+
if (read.value !== undefined && carries(read.value, labels))
|
|
1357
|
+
found.push(read.value);
|
|
1358
|
+
}
|
|
1359
|
+
return ok(found);
|
|
1360
|
+
}
|
|
1361
|
+
/** The host's get of one machine, bounded by ms: a machine the vendor reports as not found is none, never an error. */
|
|
1362
|
+
async function readMachine(host, id, ms, signal) {
|
|
1363
|
+
const read = await hostCall(host, `the read of ${id}`, (ctx) => host.get(id, ctx), ms, signal);
|
|
1364
|
+
return !read.ok && read.error.code === "not_found" ? ok(undefined) : read;
|
|
1365
|
+
}
|
|
1366
|
+
/** What reading answers, or undefined once a signal aborts first: a caller that stopped waiting never waits on a read. */
|
|
1367
|
+
function untilAborted(reading, signals) {
|
|
1368
|
+
const given = signals.filter((signal) => signal !== undefined);
|
|
1369
|
+
if (given.length === 0)
|
|
1370
|
+
return reading;
|
|
1371
|
+
const signal = AbortSignal.any(given);
|
|
1372
|
+
if (signal.aborted)
|
|
1373
|
+
return Promise.resolve(undefined);
|
|
1374
|
+
return new Promise((settle) => {
|
|
1375
|
+
const onAbort = () => settle(undefined);
|
|
1376
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1377
|
+
reading.then((value) => {
|
|
1378
|
+
signal.removeEventListener("abort", onAbort);
|
|
1379
|
+
settle(value);
|
|
1380
|
+
});
|
|
1381
|
+
});
|
|
1382
|
+
}
|
|
1383
|
+
/**
|
|
1384
|
+
* A connection that says its computer was deleted (the vendor's own API can delete it mid-run). Once its client answers
|
|
1385
|
+
* gone (the stream ended or failed), the machine is read once, and that one read answers every call in flight at that
|
|
1386
|
+
* moment and every later call. A machine the vendor no longer has, or reads gone with no why (going), makes each of them
|
|
1387
|
+
* not_found in these words; one still there, a failed one, or a read that failed, leaves the client's own answer, as does
|
|
1388
|
+
* a caller's abort or a close during the read. A call with no answer by its own timeout answers timeout at once, as the
|
|
1389
|
+
* client says; the machine is read beside it, and a deleted one ends the connection, so every call still waiting on the
|
|
1390
|
+
* hung transport and every later call answers not_found. A connection its caller closed is never read back.
|
|
1391
|
+
*/
|
|
1392
|
+
function deletedSaid(host, ref, label, requestMs, connection) {
|
|
1393
|
+
const { client } = connection;
|
|
1394
|
+
const closing = new AbortController();
|
|
1395
|
+
let deleted;
|
|
1396
|
+
let ended;
|
|
1397
|
+
let checking;
|
|
1398
|
+
const read = async () => {
|
|
1399
|
+
const got = await readMachine(host, ref.id, requestMs, closing.signal);
|
|
1400
|
+
// Gone with no why is being deleted; a failed machine (gone with its why) keeps the client's own answer.
|
|
1401
|
+
if (got.ok && (got.value === undefined || (got.value.state === "gone" && got.value.why === undefined)) && deleted === undefined && !closing.signal.aborted) {
|
|
1402
|
+
const how = got.value === undefined ? `${label} no longer has it` : `${label} reads it going`;
|
|
1403
|
+
deleted = { code: "not_found", message: `the computer ${ref.name} (${ref.id}) was deleted: ${how}` };
|
|
1404
|
+
// Every call still waiting on the transport settles now, with these words, as later ones do.
|
|
1405
|
+
void connection.close();
|
|
1406
|
+
}
|
|
1407
|
+
return deleted;
|
|
1408
|
+
};
|
|
1409
|
+
const close = () => {
|
|
1410
|
+
closing.abort();
|
|
1411
|
+
return connection.close();
|
|
1412
|
+
};
|
|
1413
|
+
return {
|
|
1414
|
+
...connection,
|
|
1415
|
+
client: {
|
|
1416
|
+
async call(method, params, options) {
|
|
1417
|
+
if (deleted)
|
|
1418
|
+
return { ok: false, error: deleted };
|
|
1419
|
+
const answered = await client.call(method, params, options);
|
|
1420
|
+
if (answered.ok)
|
|
1421
|
+
return answered;
|
|
1422
|
+
if (deleted)
|
|
1423
|
+
return { ok: false, error: deleted };
|
|
1424
|
+
if (closing.signal.aborted)
|
|
1425
|
+
return answered;
|
|
1426
|
+
if (answered.error.code === "timeout") {
|
|
1427
|
+
checking ??= read().finally(() => (checking = undefined));
|
|
1428
|
+
return answered;
|
|
1429
|
+
}
|
|
1430
|
+
if (answered.error.code !== "gone")
|
|
1431
|
+
return answered;
|
|
1432
|
+
const found = await untilAborted((ended ??= read()), [options?.signal, closing.signal]);
|
|
1433
|
+
return found ? { ok: false, error: found } : answered;
|
|
1434
|
+
},
|
|
1435
|
+
close() {
|
|
1436
|
+
void close();
|
|
1437
|
+
},
|
|
1438
|
+
},
|
|
1439
|
+
close,
|
|
1440
|
+
};
|
|
1441
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Waits, through waitUntil, until get answers none for a machine after its delete, twice running: the vendor no longer
|
|
1444
|
+
* has it. A machine read gone is still going (a vendor still destroying it), so it holds the wait like any other state.
|
|
1445
|
+
* A listing may show a deleted machine for a while (MachineHost.list), so it is not read. Pauses double from pollMs to
|
|
1446
|
+
* 5 s, bounded by goneMs and the caller's signal; a failed read ends the wait with that error. Shared with the live sweep.
|
|
1447
|
+
*/
|
|
1448
|
+
export async function settle(host, ref, { requestMs, pollMs, goneMs, signal }) {
|
|
1449
|
+
const fail = failFor(host.descriptor.id);
|
|
1279
1450
|
let agreed = 0;
|
|
1280
1451
|
let last = "unread";
|
|
1281
1452
|
const read = async (readSignal) => {
|
|
1282
|
-
const got = await
|
|
1283
|
-
if (!got.ok
|
|
1453
|
+
const got = await readMachine(host, ref.id, requestMs, readSignal);
|
|
1454
|
+
if (!got.ok) {
|
|
1284
1455
|
if (got.error.code === "aborted")
|
|
1285
1456
|
return { last };
|
|
1286
1457
|
return { failed: fail(got.error.code, `release of ${ref.name}: reading it back failed (${got.error.message}); it last read ${last}`) };
|
|
1287
1458
|
}
|
|
1288
|
-
const state = got.
|
|
1459
|
+
const state = got.value?.state;
|
|
1289
1460
|
last = state ?? "gone (not found)";
|
|
1290
|
-
if (state !== undefined
|
|
1291
|
-
agreed = 0;
|
|
1292
|
-
return { last };
|
|
1293
|
-
}
|
|
1294
|
-
const listed = await hostCall(host, "the listing by name", (ctx) => host.list({ [COMPUTER_LABEL]: "1", [NAME_LABEL]: ref.name }, ctx), listMs, readSignal);
|
|
1295
|
-
if (!listed.ok) {
|
|
1296
|
-
if (listed.error.code === "aborted")
|
|
1297
|
-
return { last };
|
|
1298
|
-
return { failed: fail(listed.error.code, `release of ${ref.name}: the listing failed (${listed.error.message}); it last read ${last}`) };
|
|
1299
|
-
}
|
|
1300
|
-
if (listed.value.some((machine) => machine.id === ref.id)) {
|
|
1461
|
+
if (state !== undefined) {
|
|
1301
1462
|
agreed = 0;
|
|
1302
|
-
last = `${last}, still listed`;
|
|
1303
1463
|
return { last };
|
|
1304
1464
|
}
|
|
1305
1465
|
return ++agreed >= LISTED_ROUNDS ? { settled: true } : { last };
|
|
@@ -1326,20 +1486,16 @@ function oldestFirst(machines) {
|
|
|
1326
1486
|
const byId = (a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
1327
1487
|
return (a, b) => (dated && at(a) !== at(b) ? at(a) - at(b) : byId(a, b));
|
|
1328
1488
|
}
|
|
1329
|
-
/** The probe's kernel against the
|
|
1330
|
-
function runtimeDrift(
|
|
1489
|
+
/** The probe's kernel against the isolation declared: gVisor exactly where it says gvisor, never where it says vm. */
|
|
1490
|
+
function runtimeDrift({ isolation, gpu }, kernel) {
|
|
1331
1491
|
const gvisor = /gvisor/i.test(kernel);
|
|
1332
|
-
if (
|
|
1492
|
+
if (isolation === "vm" && gvisor)
|
|
1333
1493
|
return [`runtime: declared vm, read back gVisor (the kernel reports ${JSON.stringify(kernel)})`];
|
|
1334
|
-
if (
|
|
1335
|
-
return [`runtime: declared gVisor (a GPU computer), read back another kernel (${JSON.stringify(kernel)})`];
|
|
1494
|
+
if (isolation === "gvisor" && !gvisor)
|
|
1495
|
+
return [`runtime: declared gVisor${gpu ? " (a GPU computer)" : ""}, read back another kernel (${JSON.stringify(kernel)})`];
|
|
1336
1496
|
return [];
|
|
1337
1497
|
}
|
|
1338
|
-
|
|
1339
|
-
function countTestedAs(label, tested) {
|
|
1340
|
-
return tested.ok ? tested : countTested(label, tested);
|
|
1341
|
-
}
|
|
1342
|
-
function labelProblem(key, value, rule) {
|
|
1498
|
+
function labelProblem(key, value) {
|
|
1343
1499
|
if (typeof value !== "string")
|
|
1344
1500
|
return `the label ${key} must be a string`;
|
|
1345
1501
|
if (key.includes("\0") || value.includes("\0"))
|
|
@@ -1350,15 +1506,18 @@ function labelProblem(key, value, rule) {
|
|
|
1350
1506
|
return `the label ${key} is in the provider's own namespace (${COMPUTER_LABEL})`;
|
|
1351
1507
|
if (key === ARCHIL_MOUNT_LABEL)
|
|
1352
1508
|
return `the label ${key} is the provider's own: it records an Archil computer's mount`;
|
|
1353
|
-
if (rule !== undefined && !rule.test(key))
|
|
1354
|
-
return `the label name ${JSON.stringify(key)} is not one the vendor takes (${rule.source})`;
|
|
1355
1509
|
return undefined;
|
|
1356
1510
|
}
|
|
1357
1511
|
/** "a" or "an" before a vendor's name, by its first letter. */
|
|
1358
1512
|
function a(label) {
|
|
1359
1513
|
return `${/^[AEIOU]/i.test(label) ? "an" : "a"} ${label}`;
|
|
1360
1514
|
}
|
|
1361
|
-
/**
|
|
1362
|
-
|
|
1363
|
-
|
|
1515
|
+
/** A spec's network as init's form (networkFormOf): "" for none, and for a network check() refuses. */
|
|
1516
|
+
function formOf(network) {
|
|
1517
|
+
try {
|
|
1518
|
+
return networkFormOf(network);
|
|
1519
|
+
}
|
|
1520
|
+
catch {
|
|
1521
|
+
return "";
|
|
1522
|
+
}
|
|
1364
1523
|
}
|