@parcha/agentrun-computer 0.1.0-beta.36 → 0.1.0-beta.38
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/NOTICE +2 -0
- package/README.md +547 -478
- package/box/Dockerfile +18 -1
- package/box/README.md +13 -0
- package/box/build-agent37.mjs +158 -0
- package/box/build-daytona.py +2 -2
- package/box/build-e2b.mjs +41 -18
- package/box/recipe.mjs +43 -0
- package/dist/agent37-ssh.d.ts +89 -0
- package/dist/agent37-ssh.js +445 -0
- package/dist/agent37.d.ts +104 -0
- package/dist/agent37.js +402 -0
- package/dist/attach.d.ts +146 -0
- package/dist/attach.js +469 -0
- package/dist/browserbase.d.ts +2 -10
- package/dist/browserbase.js +3 -6
- package/dist/client.d.ts +45 -4
- package/dist/client.js +92 -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 +96 -0
- package/dist/conformance/agent37.js +154 -0
- package/dist/conformance/cases/apps.d.ts +11 -0
- package/dist/conformance/cases/apps.js +173 -73
- package/dist/conformance/cases/health.js +14 -0
- package/dist/conformance/cases/index.js +2 -0
- package/dist/conformance/cases/isolation.d.ts +7 -5
- package/dist/conformance/cases/isolation.js +44 -14
- package/dist/conformance/cases/mount.js +1 -1
- package/dist/conformance/cases/provider.js +27 -1
- package/dist/conformance/cases/screen.d.ts +3 -0
- package/dist/conformance/cases/screen.js +18 -0
- package/dist/conformance/cases/settings.d.ts +29 -23
- package/dist/conformance/cases/settings.js +76 -58
- package/dist/conformance/cases/touch.js +19 -17
- package/dist/conformance/cases/ui.d.ts +2 -0
- package/dist/conformance/cases/ui.js +475 -0
- package/dist/conformance/cli.d.ts +9 -0
- package/dist/conformance/cli.js +94 -9
- package/dist/conformance/daytona.d.ts +9 -20
- package/dist/conformance/daytona.js +12 -21
- package/dist/conformance/e2b.d.ts +6 -0
- package/dist/conformance/e2b.js +9 -2
- package/dist/conformance/exec-stream.d.ts +45 -0
- package/dist/conformance/exec-stream.js +486 -0
- package/dist/conformance/fixture.d.ts +1 -1
- package/dist/conformance/fixture.js +25 -1
- package/dist/conformance/modal.d.ts +17 -13
- package/dist/conformance/modal.js +18 -9
- package/dist/conformance/orgo.d.ts +3 -3
- package/dist/conformance/orgo.js +6 -8
- package/dist/conformance/phone-harness.d.ts +6 -2
- package/dist/conformance/phone-harness.js +21 -4
- package/dist/conformance/report.js +1 -1
- package/dist/conformance/runner.js +23 -4
- package/dist/conformance/sweep.d.ts +50 -0
- package/dist/conformance/sweep.js +187 -0
- package/dist/conformance/types.d.ts +13 -5
- package/dist/conformance/ui-fixture.d.ts +56 -0
- package/dist/conformance/ui-fixture.js +43 -0
- package/dist/create-requests.d.ts +51 -0
- package/dist/create-requests.js +121 -0
- package/dist/daytona.d.ts +20 -133
- package/dist/daytona.js +405 -1449
- package/dist/descriptor.d.ts +11 -1
- package/dist/descriptor.js +9 -0
- package/dist/descriptors/agent37.d.ts +41 -0
- package/dist/descriptors/agent37.js +28 -0
- package/dist/descriptors/daytona.d.ts +6 -5
- package/dist/descriptors/daytona.js +9 -5
- package/dist/descriptors/index.d.ts +42 -7
- package/dist/descriptors/index.js +4 -2
- package/dist/descriptors/orgo.d.ts +2 -1
- package/dist/descriptors/orgo.js +3 -2
- package/dist/descriptors/phone-harness.d.ts +1 -1
- package/dist/descriptors/phone-harness.js +2 -2
- 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.d.ts +7 -10
- package/dist/docker.js +272 -34
- package/dist/drift.js +4 -4
- package/dist/e2b.d.ts +16 -290
- package/dist/e2b.js +230 -1393
- package/dist/fake-ui-driver.d.ts +97 -0
- package/dist/fake-ui-driver.js +717 -0
- package/dist/host.d.ts +201 -0
- package/dist/host.js +8 -0
- package/dist/hosted/process.d.ts +81 -0
- package/dist/hosted/process.js +259 -0
- package/dist/hosted/provider.d.ts +160 -0
- package/dist/hosted/provider.js +1415 -0
- package/dist/hosted/rest.d.ts +66 -0
- package/dist/hosted/rest.js +121 -0
- package/dist/hosted/scripts.d.ts +82 -0
- package/dist/{child.js → hosted/scripts.js} +118 -12
- package/dist/hosted/values.d.ts +5 -0
- package/dist/hosted/values.js +8 -0
- package/dist/index.d.ts +7 -3
- package/dist/index.js +7 -4
- package/dist/kernel.d.ts +2 -10
- package/dist/kernel.js +3 -6
- package/dist/kinds.d.ts +11 -2
- package/dist/kinds.js +20 -4
- package/dist/local-chrome.d.ts +2 -10
- package/dist/local-chrome.js +3 -6
- package/dist/modal.d.ts +22 -257
- package/dist/modal.js +183 -1280
- package/dist/mount-archil.d.ts +8 -4
- package/dist/mount-archil.js +22 -11
- package/dist/mount.d.ts +10 -4
- package/dist/mount.js +8 -4
- package/dist/orgo-channel.d.ts +3 -2
- package/dist/orgo-channel.js +5 -2
- package/dist/orgo-desktop.d.ts +9 -4
- package/dist/orgo-desktop.js +33 -13
- package/dist/orgo-workspaces.d.ts +47 -0
- package/dist/orgo-workspaces.js +94 -0
- package/dist/orgo.d.ts +10 -13
- package/dist/orgo.js +32 -21
- package/dist/phone-harness.d.ts +31 -28
- package/dist/phone-harness.js +510 -702
- package/dist/png.d.ts +4 -0
- package/dist/png.js +27 -0
- package/dist/protocol.d.ts +339 -33
- package/dist/protocol.js +97 -3
- package/dist/provider.d.ts +36 -8
- package/dist/provider.js +16 -0
- package/dist/providers.d.ts +11 -12
- package/dist/result.d.ts +1 -1
- package/dist/screen-watch.js +34 -6
- package/dist/surface/api.d.ts +260 -0
- package/dist/surface/api.js +1 -0
- package/dist/surface/core.d.ts +55 -0
- package/dist/surface/core.js +475 -0
- package/dist/surface/index.d.ts +5 -0
- package/dist/surface/index.js +3 -0
- package/dist/surface/outline.d.ts +10 -0
- package/dist/surface/outline.js +51 -0
- package/dist/surface/script.d.ts +10 -0
- package/dist/surface/script.js +94 -0
- package/dist/ui-engine.d.ts +114 -0
- package/dist/ui-engine.js +814 -0
- package/package.json +18 -3
- package/dist/child.d.ts +0 -42
- package/dist/conformance/daytona-sweep.d.ts +0 -18
- package/dist/conformance/daytona-sweep.js +0 -126
- package/dist/conformance/e2b-diagnose.d.ts +0 -16
- package/dist/conformance/e2b-diagnose.js +0 -61
- package/dist/conformance/e2b-sweep.d.ts +0 -18
- package/dist/conformance/e2b-sweep.js +0 -132
- package/dist/conformance/modal-sweep.d.ts +0 -22
- package/dist/conformance/modal-sweep.js +0 -213
package/dist/docker.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// The Docker provisioner: every call through the docker CLI with execFile or spawn, never a shell.
|
|
2
2
|
import { execFile, spawn } from "node:child_process";
|
|
3
|
-
import {
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
4
|
import { readFileSync, statSync } from "node:fs";
|
|
5
5
|
import { resolve } from "node:path";
|
|
6
6
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
@@ -20,14 +20,11 @@ import { addressBytes } from "./docker/egress-table.js";
|
|
|
20
20
|
import { checkMount, WORKSPACE } from "./mount.js";
|
|
21
21
|
import { checkGpu, checkTenure, runLifetimeMs, } from "./provider.js";
|
|
22
22
|
import { failFor, ok, vendorFail } from "./result.js";
|
|
23
|
+
import { CreateRequests, endedWords, otherSpecWords } from "./create-requests.js";
|
|
23
24
|
import { daemonWatch } from "./screen-watch.js";
|
|
24
25
|
import { countTested } from "./test-connection.js";
|
|
25
26
|
/** This module's failures: one with code vendor names Docker. */
|
|
26
27
|
const fail = failFor("docker");
|
|
27
|
-
/** The mount kinds a Docker computer takes, as capabilities().mount declares them: a bind of a folder on this host. */
|
|
28
|
-
export const MOUNT = DOCKER.mounts;
|
|
29
|
-
/** The machine kind and groups of each configuration (its descriptor's): a linux computer, and a desktop. */
|
|
30
|
-
export const CAPABILITIES = DOCKER.capabilities;
|
|
31
28
|
/**
|
|
32
29
|
* Docker's architecture as `docker version` reports it (amd64, arm64), within timeoutMs, or an error in words. The docker
|
|
33
30
|
* CLI runs with this process's environment, as every call of the provider does, so it asks the Docker the provider uses.
|
|
@@ -99,11 +96,23 @@ const FIXED = "a Docker computer's network is fixed at create; create a new comp
|
|
|
99
96
|
const NETWORKS = 'network is required: "none", "open" or { allow: [names or CIDRs] }';
|
|
100
97
|
/** How often create asks whether the egress proxy is ready. */
|
|
101
98
|
const EGRESS_POLL_MS = 100;
|
|
99
|
+
/** How long a create of a request whose computer another create is still making waits for it, and how often it looks:
|
|
100
|
+
* the image is on the host by then, so what is left is docker run, the display and the read-back. */
|
|
101
|
+
const REQUEST_WAIT_MS = 120_000;
|
|
102
|
+
const REQUEST_POLL_MS = 500;
|
|
102
103
|
/** After a docker run cut off before its own exit: how long, at most, the leftovers are removed and looked for again,
|
|
103
104
|
* how often, and for how long in a row the looks must find none of them before they count as gone (#settle). */
|
|
104
105
|
const SETTLE_MS = 10_000;
|
|
105
106
|
const SETTLE_EVERY_MS = 500;
|
|
106
107
|
const SETTLE_QUIET_MS = 3_000;
|
|
108
|
+
/** The label naming which create made a computer's container: after its own docker run failed or was cut off, a create
|
|
109
|
+
* removes the container it made, never one another create of the same request made under the name. */
|
|
110
|
+
const ATTEMPT_LABEL = "agentrun.computer.attempt";
|
|
111
|
+
/** The label naming the spec's own labels (a JSON array of their keys, sorted): a container's labels also hold its image's,
|
|
112
|
+
* so only this says which a create asked for, and a retry of the request that drops one is another spec (#differs). */
|
|
113
|
+
const ASKED_LABEL = "agentrun.computer.labels";
|
|
114
|
+
/** What #settle reads of each container it looks for: its id, its name, and its labels (whose attempt it is). */
|
|
115
|
+
const SETTLE_FORMAT = '{"Id":{{json .Id}},"Name":{{json .Name}},"Config":{"Labels":{{json .Config.Labels}}}}';
|
|
107
116
|
/**
|
|
108
117
|
* Why #settle could not show its leftovers gone within SETTLE_MS, in words that always name them: `there`, what its
|
|
109
118
|
* last look still found; or, when the last look found none but the quiet window was not complete, every name it
|
|
@@ -122,6 +131,8 @@ const INFO_FORMAT = '{"Architecture":{{json .Architecture}},"SecurityOptions":{{
|
|
|
122
131
|
const isNumber = (value) => typeof value === "number" && Number.isFinite(value);
|
|
123
132
|
const isWhole = (value) => typeof value === "number" && Number.isSafeInteger(value);
|
|
124
133
|
const noSuchContainer = (stderr) => /no such container/i.test(stderr);
|
|
134
|
+
/** Docker's words for a docker run whose --name another container holds. */
|
|
135
|
+
const nameTaken = (stderr) => /Conflict\. The container name "[^"]*" is already in use/.test(stderr);
|
|
125
136
|
const nameOf = (report) => (report.Name ?? "").replace(/^\//, "");
|
|
126
137
|
/** Only a container this provider made counts: its label, and a name of its making. */
|
|
127
138
|
const ours = (report) => report.Config?.Labels?.[COMPUTER_LABEL] === "1" && NAME.test(nameOf(report));
|
|
@@ -137,6 +148,8 @@ export class DockerProvider {
|
|
|
137
148
|
#desktop;
|
|
138
149
|
#image;
|
|
139
150
|
#egress;
|
|
151
|
+
/** Each create in flight, and the names of the computers this provider deleted (create-requests.ts). */
|
|
152
|
+
#requests = new CreateRequests();
|
|
140
153
|
/** Why this provider's own interfaces are not the Docker host's, in words, or none: read once, for the first allowlist
|
|
141
154
|
* with no egress.hostAddresses (#elsewhere). */
|
|
142
155
|
#elsewhere;
|
|
@@ -205,7 +218,7 @@ export class DockerProvider {
|
|
|
205
218
|
user: ["root", "uid"],
|
|
206
219
|
release: ["stop", "delete"],
|
|
207
220
|
stopDeletes: false,
|
|
208
|
-
mount: [...
|
|
221
|
+
mount: [...DOCKER.mounts],
|
|
209
222
|
};
|
|
210
223
|
}
|
|
211
224
|
/**
|
|
@@ -303,11 +316,115 @@ export class DockerProvider {
|
|
|
303
316
|
}
|
|
304
317
|
return refusals;
|
|
305
318
|
}
|
|
306
|
-
|
|
319
|
+
/**
|
|
320
|
+
* Makes a computer, once per request (provider.md, Idempotent create): context.requestId names the container
|
|
321
|
+
* (computerNameFor). A request Docker holds a container for already answers that one (#taken); one whose computer
|
|
322
|
+
* was deleted here is refused exists; a create of a request in flight here waits for the first (CreateRequests).
|
|
323
|
+
*/
|
|
324
|
+
async create(spec, context) {
|
|
307
325
|
const refusals = this.check(spec);
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
326
|
+
return this.#requests.create(spec, context, {
|
|
327
|
+
refuse: fail,
|
|
328
|
+
...(refusals.length > 0 ? { refused: refusals.map(({ field, message }) => `${field}: ${message}`).join("; ") } : {}),
|
|
329
|
+
userOf: (asked) => this.#userFor(asked),
|
|
330
|
+
make: (name) => this.#createNamed(spec, name, context.signal),
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
/** The user a spec's computer runs as: the spec's, or the provider's. */
|
|
334
|
+
#userFor(spec) {
|
|
335
|
+
return userOf(spec.user) ?? this.#user;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* How spec differs from report, the container Docker holds under its request's name: every way create's own drift
|
|
339
|
+
* check would find it differs from spec (inspectDrift: each label asked, the image, user, network, mount, sizes and
|
|
340
|
+
* workdir among them), and each label the computer was made with that spec does not ask for (ASKED_LABEL). Empty
|
|
341
|
+
* when it is the same.
|
|
342
|
+
*/
|
|
343
|
+
async #differs(spec, report, signal) {
|
|
344
|
+
const host = await this.host({ signal });
|
|
345
|
+
if (!host.ok)
|
|
346
|
+
return host;
|
|
347
|
+
const proxy = await this.#proxyOf(report, signal);
|
|
348
|
+
if (!proxy.ok)
|
|
349
|
+
return proxy;
|
|
350
|
+
const daemon = this.#daemon();
|
|
351
|
+
if (!daemon.ok)
|
|
352
|
+
return daemon;
|
|
353
|
+
const expected = this.#expected(host.value, report.Config?.Labels?.[DEADLINE_LABEL] ?? "", this.#userFor(spec), this.#mountedDaemon(report) ?? daemon.value, proxy.value);
|
|
354
|
+
const labels = report.Config?.Labels ?? {};
|
|
355
|
+
let made;
|
|
356
|
+
try {
|
|
357
|
+
made = JSON.parse(labels[ASKED_LABEL] ?? "");
|
|
358
|
+
}
|
|
359
|
+
catch {
|
|
360
|
+
made = undefined;
|
|
361
|
+
}
|
|
362
|
+
const asked = Object.keys(spec.labels ?? {});
|
|
363
|
+
const dropped = Array.isArray(made)
|
|
364
|
+
? made.filter((key) => typeof key === "string" && !asked.includes(key)).map((key) => `label ${key}: not declared, made with ${JSON.stringify(labels[key])}`)
|
|
365
|
+
: [`label ${ASKED_LABEL}: the spec's label keys, read back ${JSON.stringify(labels[ASKED_LABEL])}`];
|
|
366
|
+
return ok([...inspectDrift(spec, report, expected), ...dropped]);
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* A create of a request whose name Docker holds already: its docker run (of the computer, or of the allowlist's proxy)
|
|
370
|
+
* found the name taken, by a create of the same request in another process or before this one. A container of
|
|
371
|
+
* another spec, one stopped, or one past its deadline is refused exists; one running is returned once attach reads it
|
|
372
|
+
* as create made it, and one not there yet is waited for, within REQUEST_WAIT_MS. None by then is exists: a create
|
|
373
|
+
* that took the name and removed what it made has ended.
|
|
374
|
+
*/
|
|
375
|
+
async #taken(spec, name, signal) {
|
|
376
|
+
const until = Date.now() + REQUEST_WAIT_MS;
|
|
377
|
+
let last;
|
|
378
|
+
for (;;) {
|
|
379
|
+
const found = await this.#inspect([name], signal);
|
|
380
|
+
if (!found.ok)
|
|
381
|
+
return found;
|
|
382
|
+
const report = found.value.find((one) => nameOf(one) === name);
|
|
383
|
+
if (report !== undefined) {
|
|
384
|
+
if (!ours(report))
|
|
385
|
+
return fail("exists", `a container named ${name} is on the Docker host and this provider did not make it: create with a new request id`);
|
|
386
|
+
const status = report.State?.Status;
|
|
387
|
+
const deadline = Date.parse(report.Config?.Labels?.[DEADLINE_LABEL] ?? "");
|
|
388
|
+
if (status === "exited" || status === "dead" || status === "removing" || deadline <= Date.now())
|
|
389
|
+
return fail("exists", endedWords(name));
|
|
390
|
+
if (report.State?.Running === true) {
|
|
391
|
+
const attached = await this.attach({ id: name }, { signal });
|
|
392
|
+
if (attached.ok) {
|
|
393
|
+
// Up and sound: returned only when it is the computer this spec asks for, as create would have made it.
|
|
394
|
+
const differ = await this.#differs(spec, report, signal);
|
|
395
|
+
if (!differ.ok)
|
|
396
|
+
return differ;
|
|
397
|
+
if (differ.value.length > 0)
|
|
398
|
+
return fail("exists", otherSpecWords(name, "was made", differ.value));
|
|
399
|
+
// Still the one read, running and within its deadline after those reads: attach checks a stopped
|
|
400
|
+
// container by inspect alone, so one that stopped or ended since would pass it.
|
|
401
|
+
const again = await this.#inspect([name], signal);
|
|
402
|
+
if (!again.ok)
|
|
403
|
+
return again;
|
|
404
|
+
const now = again.value.find((one) => nameOf(one) === name);
|
|
405
|
+
if (now === undefined || now.Id !== report.Id || now.State?.Running !== true || Date.parse(now.Config?.Labels?.[DEADLINE_LABEL] ?? "") <= Date.now())
|
|
406
|
+
return fail("exists", endedWords(name));
|
|
407
|
+
return ok({ id: attached.value.id, name });
|
|
408
|
+
}
|
|
409
|
+
if (attached.error.code === "aborted")
|
|
410
|
+
return attached;
|
|
411
|
+
// Its display or its daemon is not up yet: the first create is still making it.
|
|
412
|
+
last = attached.error;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
if (Date.now() + REQUEST_POLL_MS >= until) {
|
|
416
|
+
if (report === undefined)
|
|
417
|
+
return fail("exists", endedWords(name));
|
|
418
|
+
return fail("timeout", `${name}, made for this request, was not up within ${REQUEST_WAIT_MS / 1000} s${last ? `: ${last.message}` : ""}`);
|
|
419
|
+
}
|
|
420
|
+
await sleep(REQUEST_POLL_MS, undefined, { signal }).catch(() => undefined);
|
|
421
|
+
if (signal?.aborted)
|
|
422
|
+
return fail("aborted", `create was aborted while ${name}, made for this request, came up`);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
/** Everything create does under the request's name: the computer Docker holds under it, or a new one. */
|
|
426
|
+
async #createNamed(spec, name, signal) {
|
|
427
|
+
const user = this.#userFor(spec);
|
|
311
428
|
const bound = spec.mount?.kind === "bind" ? spec.mount.path : undefined;
|
|
312
429
|
const daemon = this.#daemon();
|
|
313
430
|
if (!daemon.ok)
|
|
@@ -379,20 +496,28 @@ export class DockerProvider {
|
|
|
379
496
|
}
|
|
380
497
|
if (signal?.aborted)
|
|
381
498
|
return fail("aborted", "create was aborted before docker run");
|
|
382
|
-
|
|
499
|
+
// This create's own mark on the container it runs: what a failed or cut-off run leaves is removed by it, never a
|
|
500
|
+
// container another create of the request made under the name.
|
|
501
|
+
const attempt = randomUUID();
|
|
383
502
|
// The lifetime counts from here: what the proxy and docker run take comes out of it.
|
|
384
503
|
const lifetimeFrom = Date.now();
|
|
385
504
|
const deadline = new Date(lifetimeFrom + runLifetimeMs(spec)).toISOString();
|
|
386
505
|
if (allow !== undefined) {
|
|
387
506
|
// The proxy first, ready before the computer exists: a computer never runs with an allowlist it cannot reach.
|
|
388
|
-
const started = await this.#startEgress(name, user, deadline, allow, spec.labels ?? {}, signal);
|
|
507
|
+
const started = await this.#startEgress(name, user, deadline, allow, spec.labels ?? {}, attempt, signal);
|
|
389
508
|
if (!started.ok) {
|
|
390
|
-
|
|
509
|
+
// Another create of this request, in another process, runs its proxy: what it makes is the request's.
|
|
510
|
+
if (nameTaken(started.error.message))
|
|
511
|
+
return this.#taken(spec, name, signal);
|
|
512
|
+
// The computer was never run by this create: only the proxy and volume it made go, never a container under the
|
|
513
|
+
// name, nor a proxy or volume another create of the request made.
|
|
514
|
+
const removed = await this.#removeProxy(name, attempt);
|
|
391
515
|
const after = removed.ok ? "its proxy and volume were removed" : `its proxy and volume could not be removed (${removed.error.message})`;
|
|
392
516
|
return fail(started.error.code, `${started.error.message}; the computer ${name} was never run, and ${after}`);
|
|
393
517
|
}
|
|
394
518
|
}
|
|
395
|
-
const
|
|
519
|
+
const asked = JSON.stringify(Object.keys(spec.labels ?? {}).sort());
|
|
520
|
+
const args = ["run", "--detach", "--name", name, "--label", `${COMPUTER_LABEL}=1`, "--label", `${DEADLINE_LABEL}=${deadline}`, "--label", `${ATTEMPT_LABEL}=${attempt}`, "--label", `${ASKED_LABEL}=${asked}`];
|
|
396
521
|
for (const [key, value] of Object.entries(spec.labels ?? {}))
|
|
397
522
|
args.push("--label", `${key}=${value}`);
|
|
398
523
|
const memoryMb = spec.memoryMb ?? DEFAULT_MEMORY_MB;
|
|
@@ -425,17 +550,32 @@ export class DockerProvider {
|
|
|
425
550
|
args.push(spec.image, "init", "--deadline", deadline, ...(allow === undefined ? [] : [RELAY_FLAG, EGRESS_SOCKET]));
|
|
426
551
|
const started = await this.#run(args, signal);
|
|
427
552
|
const ranMs = Date.now() - lifetimeFrom;
|
|
553
|
+
if (started.ok && started.value.exitCode !== 0 && nameTaken(started.value.stderr)) {
|
|
554
|
+
// Another create of this request, in another process, ran first: its computer is the request's, and this one's
|
|
555
|
+
// proxy, made for a computer that never ran, goes.
|
|
556
|
+
if (allow !== undefined)
|
|
557
|
+
await this.#removeProxy(name, attempt);
|
|
558
|
+
return this.#taken(spec, name, signal);
|
|
559
|
+
}
|
|
428
560
|
if (!started.ok || started.value.exitCode !== 0) {
|
|
429
561
|
// A run that failed or was aborted may still have made the container; an allowlist's proxy and volume go with it.
|
|
430
|
-
|
|
562
|
+
// Only what this create made goes: a container another create of the request made under the name stays (a retry
|
|
563
|
+
// cut off before Docker answered that the name is taken).
|
|
564
|
+
const released = await this.#removeAttempt(name, attempt);
|
|
431
565
|
if (started.ok)
|
|
432
566
|
return vendorFail("docker", `docker run failed: ${started.value.stderr.trim()}`);
|
|
433
567
|
// Cut off before its own exit: the daemon may still make the computer after release looked, and then its egress
|
|
434
568
|
// volume with it (#settle).
|
|
435
|
-
const left = await this.#settle([name], allow === undefined ? [] : [egressNameOf(name)]);
|
|
569
|
+
const left = await this.#settle([name], allow === undefined ? [] : [egressNameOf(name)], attempt);
|
|
436
570
|
const what = allow === undefined ? `the computer ${name}` : `the computer ${name}, its proxy and its volume`;
|
|
437
571
|
const removed = allow === undefined ? "was removed, if Docker made it after" : "were removed, if Docker made them after";
|
|
438
|
-
const words = !released.ok
|
|
572
|
+
const words = !released.ok
|
|
573
|
+
? `${what} could not all be removed: ${released.error.message}`
|
|
574
|
+
: released.value
|
|
575
|
+
? `the computer ${name} that another create of this request made is left as it is${allow === undefined ? "" : `, and this create's proxy and volume ${left === undefined ? "were removed" : `could not all be removed: ${left}`}`}`
|
|
576
|
+
: left === undefined
|
|
577
|
+
? `${what} ${removed}`
|
|
578
|
+
: `${what} could not all be removed: ${left}`;
|
|
439
579
|
return fail(started.error.code, `${started.error.message === "docker run was aborted" ? "create was aborted during docker run" : started.error.message}; ${words}`);
|
|
440
580
|
}
|
|
441
581
|
const ref = { id: started.value.stdout.trim(), name };
|
|
@@ -500,8 +640,12 @@ export class DockerProvider {
|
|
|
500
640
|
* confirmed gone, in words (settleWords). A create later than that is the janitor's: sweepLeftovers takes it at the
|
|
501
641
|
* next sweep, past #leftoverGraceMs, which stays the backstop.
|
|
502
642
|
*/
|
|
503
|
-
async #settle(containers, volumes) {
|
|
643
|
+
async #settle(containers, volumes, attempt) {
|
|
504
644
|
const bound = AbortSignal.timeout(SETTLE_MS);
|
|
645
|
+
// Only what that create made (ATTEMPT_LABEL) is removed and waited for: a container or a volume another create of the
|
|
646
|
+
// request made under the name is not this create's leftover.
|
|
647
|
+
const mine = (reports) => reports.filter((one) => one.Config?.Labels?.[ATTEMPT_LABEL] === attempt);
|
|
648
|
+
const look = () => (containers.length === 0 ? Promise.resolve(ok([])) : this.#inspect(containers, bound, SETTLE_FORMAT));
|
|
505
649
|
// When the looks first found none of them in the current run of empty looks; there, what the last look found.
|
|
506
650
|
let quietSince;
|
|
507
651
|
// Each named as what it is: a container and a volume may share a name (the egress proxy's).
|
|
@@ -511,23 +655,24 @@ export class DockerProvider {
|
|
|
511
655
|
let there = checked;
|
|
512
656
|
while (!bound.aborted) {
|
|
513
657
|
// --volumes, as release removes a computer: a late container of an image that declares a VOLUME has its own.
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
const
|
|
658
|
+
// By id, the one looked at: never whatever holds the name by the time rm runs.
|
|
659
|
+
const seen = await look();
|
|
660
|
+
if (seen.ok)
|
|
661
|
+
for (const one of mine(seen.value))
|
|
662
|
+
await this.#run(["rm", "--force", "--volumes", String(one.Id)], bound);
|
|
663
|
+
const owned = await this.#volumesOf(volumes, attempt, bound);
|
|
664
|
+
if (owned.ok)
|
|
665
|
+
for (const volume of owned.value)
|
|
666
|
+
await this.#run(["volume", "rm", "--force", volume], bound);
|
|
667
|
+
const found = await look();
|
|
668
|
+
const listed = await this.#volumesOf(volumes, attempt, bound);
|
|
520
669
|
// A look the bound cut off says nothing: what the last whole look found stands.
|
|
521
670
|
if (bound.aborted)
|
|
522
671
|
break;
|
|
523
|
-
// A read that fails
|
|
524
|
-
const
|
|
525
|
-
const
|
|
526
|
-
|
|
527
|
-
there = [
|
|
528
|
-
...containers.filter((name) => foundNames.includes(name)).map(containerWords),
|
|
529
|
-
...volumes.filter((name) => listedText === undefined || listedText.split("\n").some((line) => line.includes(name))).map(volumeWords),
|
|
530
|
-
];
|
|
672
|
+
// A read that fails counts as all still there.
|
|
673
|
+
const foundNames = found.ok ? mine(found.value).map(nameOf) : containers;
|
|
674
|
+
const listedNames = listed.ok ? listed.value : volumes;
|
|
675
|
+
there = [...containers.filter((name) => foundNames.includes(name)).map(containerWords), ...volumes.filter((name) => listedNames.includes(name)).map(volumeWords)];
|
|
531
676
|
if (there.length > 0)
|
|
532
677
|
quietSince = undefined;
|
|
533
678
|
else {
|
|
@@ -550,8 +695,10 @@ export class DockerProvider {
|
|
|
550
695
|
* bound, until the proxy logs its ready line. A proxy that ends first fails with its own last words. The caller
|
|
551
696
|
* removes whatever was made when this fails.
|
|
552
697
|
*/
|
|
553
|
-
async #startEgress(name, user, deadline, allow,
|
|
698
|
+
async #startEgress(name, user, deadline, allow, specLabels, attempt, signal) {
|
|
554
699
|
const proxy = egressNameOf(name);
|
|
700
|
+
// The proxy and its volume carry the create's attempt too: its cleanup removes only what it made.
|
|
701
|
+
const labels = { ...specLabels, [ATTEMPT_LABEL]: attempt };
|
|
555
702
|
const volume = await this.#run(egressVolumeArgs(name, user, deadline, labels), signal);
|
|
556
703
|
if (!volume.ok)
|
|
557
704
|
return volume;
|
|
@@ -562,7 +709,7 @@ export class DockerProvider {
|
|
|
562
709
|
const ran = await this.#run(args, signal);
|
|
563
710
|
if (!ran.ok) {
|
|
564
711
|
// Cut off before its own exit: the daemon may still make the proxy after the caller's cleanup looked (#settle).
|
|
565
|
-
const left = await this.#settle([proxy], [proxy]);
|
|
712
|
+
const left = await this.#settle([proxy], [proxy], attempt);
|
|
566
713
|
const words = left === undefined ? "the proxy and its volume, if Docker made them after, were removed" : `the proxy and its volume could not all be removed: ${left}`;
|
|
567
714
|
return fail(ran.error.code, `${ran.error.message === "docker run was aborted" ? "create was aborted during docker run of the egress proxy" : ran.error.message}; ${words}`);
|
|
568
715
|
}
|
|
@@ -1089,6 +1236,97 @@ export class DockerProvider {
|
|
|
1089
1236
|
return fail("invalid", `${ref.name} is not the name of a Docker computer (computer-<12 hex digits>)`);
|
|
1090
1237
|
if (how !== "stop" && how !== "delete")
|
|
1091
1238
|
return fail("invalid", `release is "stop" or "delete", not ${String(how)}`);
|
|
1239
|
+
// A deleted computer's request is spent: the same request id again is refused, never made a second time.
|
|
1240
|
+
if (how === "delete")
|
|
1241
|
+
this.#requests.end(ref.name);
|
|
1242
|
+
return this.#remove(ref, how);
|
|
1243
|
+
}
|
|
1244
|
+
/**
|
|
1245
|
+
* An allowlist proxy and its volume that one create made for a computer it never ran: each only when it carries that
|
|
1246
|
+
* create's attempt (ATTEMPT_LABEL) with the computer's egress label, found by both as release finds a proxy, the
|
|
1247
|
+
* proxy removed by its id. One another create of the request made stays. Bounded by SETTLE_MS, never the caller's
|
|
1248
|
+
* signal, which may have ended.
|
|
1249
|
+
*/
|
|
1250
|
+
async #removeProxy(name, attempt) {
|
|
1251
|
+
const proxy = egressNameOf(name);
|
|
1252
|
+
const bound = AbortSignal.timeout(SETTLE_MS);
|
|
1253
|
+
const filters = ["--filter", `label=${EGRESS_LABEL}=${name}`, "--filter", `label=${ATTEMPT_LABEL}=${attempt}`];
|
|
1254
|
+
const lines = (text) => text.split("\n").map((line) => line.trim()).filter(Boolean);
|
|
1255
|
+
const found = await this.#run(["ps", "-a", "--no-trunc", ...filters, "--format", "{{.ID}}"], bound);
|
|
1256
|
+
if (!found.ok)
|
|
1257
|
+
return found;
|
|
1258
|
+
if (found.value.exitCode !== 0)
|
|
1259
|
+
return vendorFail("docker", `docker ps for ${proxy} failed: ${found.value.stderr.trim()}`);
|
|
1260
|
+
for (const id of lines(found.value.stdout)) {
|
|
1261
|
+
const container = await this.#run(["rm", "--force", id], bound);
|
|
1262
|
+
if (!container.ok)
|
|
1263
|
+
return container;
|
|
1264
|
+
if (container.value.exitCode !== 0 && !noSuchContainer(container.value.stderr))
|
|
1265
|
+
return vendorFail("docker", `docker rm ${proxy} failed: ${container.value.stderr.trim()}`);
|
|
1266
|
+
}
|
|
1267
|
+
const volumes = await this.#run(["volume", "ls", ...filters, "--format", "{{.Name}}"], bound);
|
|
1268
|
+
if (!volumes.ok)
|
|
1269
|
+
return volumes;
|
|
1270
|
+
if (volumes.value.exitCode !== 0)
|
|
1271
|
+
return vendorFail("docker", `docker volume ls for ${proxy} failed: ${volumes.value.stderr.trim()}`);
|
|
1272
|
+
for (const volume of lines(volumes.value.stdout)) {
|
|
1273
|
+
const removed = await this.#run(["volume", "rm", "--force", volume], bound);
|
|
1274
|
+
if (!removed.ok)
|
|
1275
|
+
return removed;
|
|
1276
|
+
if (removed.value.exitCode !== 0)
|
|
1277
|
+
return vendorFail("docker", `docker volume rm ${volume} failed: ${removed.value.stderr.trim()}`);
|
|
1278
|
+
}
|
|
1279
|
+
return ok(undefined);
|
|
1280
|
+
}
|
|
1281
|
+
/** Of the volumes named, the ones there that carry the attempt (ATTEMPT_LABEL); one that is not there is left out. */
|
|
1282
|
+
async #volumesOf(names, attempt, signal) {
|
|
1283
|
+
if (names.length === 0)
|
|
1284
|
+
return ok([]);
|
|
1285
|
+
const ran = await this.#run(["volume", "inspect", "--format", "{{json .}}", ...names], signal);
|
|
1286
|
+
if (!ran.ok)
|
|
1287
|
+
return ran;
|
|
1288
|
+
const errors = ran.value.stderr.split("\n").filter((line) => line.trim() !== "" && !/no such volume/i.test(line));
|
|
1289
|
+
if (ran.value.exitCode !== 0 && errors.length > 0)
|
|
1290
|
+
return vendorFail("docker", `docker volume inspect failed: ${errors.join("; ")}`);
|
|
1291
|
+
const mine = [];
|
|
1292
|
+
for (const line of ran.value.stdout.split("\n").filter((one) => one.trim() !== "")) {
|
|
1293
|
+
let volume;
|
|
1294
|
+
try {
|
|
1295
|
+
volume = JSON.parse(line);
|
|
1296
|
+
}
|
|
1297
|
+
catch {
|
|
1298
|
+
return vendorFail("docker", `docker volume inspect wrote a line that is not JSON: ${line.slice(0, 200)}`);
|
|
1299
|
+
}
|
|
1300
|
+
if (typeof volume.Name === "string" && volume.Labels?.[ATTEMPT_LABEL] === attempt)
|
|
1301
|
+
mine.push(volume.Name);
|
|
1302
|
+
}
|
|
1303
|
+
return ok(mine);
|
|
1304
|
+
}
|
|
1305
|
+
/**
|
|
1306
|
+
* What one create made under the name, removed after its computer's docker run failed or was cut off: the container
|
|
1307
|
+
* that carries its attempt (ATTEMPT_LABEL), and the allowlist proxy and volume it made (#removeProxy). A container
|
|
1308
|
+
* another create of the request made under the name, in another process or before this one, is left as it is, with
|
|
1309
|
+
* its proxy and volume: true then. The look is bounded (SETTLE_MS), never the caller's signal, which may have ended.
|
|
1310
|
+
*/
|
|
1311
|
+
async #removeAttempt(name, attempt) {
|
|
1312
|
+
const found = await this.#inspect([name], AbortSignal.timeout(SETTLE_MS));
|
|
1313
|
+
if (!found.ok)
|
|
1314
|
+
return found;
|
|
1315
|
+
const report = found.value.find((one) => nameOf(one) === name);
|
|
1316
|
+
const mine = report?.Config?.Labels?.[ATTEMPT_LABEL] === attempt;
|
|
1317
|
+
// By the id looked at, never by the name, which another create may hold by the time rm runs.
|
|
1318
|
+
if (mine) {
|
|
1319
|
+
const removed = await this.#run(["rm", "--force", "--volumes", String(report.Id)], AbortSignal.timeout(SETTLE_MS));
|
|
1320
|
+
if (!removed.ok)
|
|
1321
|
+
return removed;
|
|
1322
|
+
if (removed.value.exitCode !== 0 && !noSuchContainer(removed.value.stderr))
|
|
1323
|
+
return vendorFail("docker", `docker rm ${name} failed: ${removed.value.stderr.trim()}`);
|
|
1324
|
+
}
|
|
1325
|
+
const proxy = await this.#removeProxy(name, attempt);
|
|
1326
|
+
return proxy.ok ? ok(report !== undefined && !mine) : proxy;
|
|
1327
|
+
}
|
|
1328
|
+
/** release's work, which create also does for what it made before its computer ran (its request is not spent). */
|
|
1329
|
+
async #remove(ref, how) {
|
|
1092
1330
|
const args = how === "stop" ? ["stop", ref.name] : ["rm", "--force", "--volumes", ref.name];
|
|
1093
1331
|
const ran = await this.#run(args);
|
|
1094
1332
|
if (!ran.ok)
|
package/dist/drift.js
CHANGED
|
@@ -64,10 +64,10 @@ export function probeNotes(probe, network) {
|
|
|
64
64
|
/** Every way hello differs from what the provider expects. */
|
|
65
65
|
export function helloDrift(hello, expected) {
|
|
66
66
|
const found = [];
|
|
67
|
-
//
|
|
68
|
-
//
|
|
69
|
-
if (hello?.protocol !==
|
|
70
|
-
found.push(`hello: the daemon speaks protocol ${text(hello
|
|
67
|
+
// Exact match (spec/computer/README.md, "Versioning"): the provider ships its daemon, so another protocol version or
|
|
68
|
+
// daemon version, older or newer, is another wire. A hello that names no protocol is refused too.
|
|
69
|
+
if (hello?.protocol !== PROTOCOL_VERSION) {
|
|
70
|
+
found.push(`hello: the daemon speaks protocol ${text(hello?.protocol)}, expected exactly ${PROTOCOL_VERSION}`);
|
|
71
71
|
}
|
|
72
72
|
if (hello?.version !== expected.version)
|
|
73
73
|
found.push(`hello: the daemon reports version ${text(hello?.version)}, expected ${expected.version}`);
|