@parcha/agentrun-computer 0.0.0-placeholder → 0.1.0-beta.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +8 -0
- package/README.md +2400 -1
- package/box/Dockerfile +69 -0
- package/box/agentrun-archil +168 -0
- package/box/build-daytona.py +26 -0
- package/box/build-e2b.mjs +32 -0
- package/box/build-modal.py +17 -0
- package/dist/actions.d.ts +18 -0
- package/dist/actions.js +139 -0
- package/dist/browser.d.ts +151 -0
- package/dist/browser.js +305 -0
- package/dist/browserbase.d.ts +139 -0
- package/dist/browserbase.js +408 -0
- package/dist/child.d.ts +40 -0
- package/dist/child.js +127 -0
- package/dist/client.d.ts +22 -0
- package/dist/client.js +136 -0
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/conformance/archil.d.ts +68 -0
- package/dist/conformance/archil.js +152 -0
- package/dist/conformance/browserbase.d.ts +35 -0
- package/dist/conformance/browserbase.js +45 -0
- package/dist/conformance/cases/apps.d.ts +2 -0
- package/dist/conformance/cases/apps.js +155 -0
- package/dist/conformance/cases/browser.d.ts +4 -0
- package/dist/conformance/cases/browser.js +105 -0
- package/dist/conformance/cases/drift.d.ts +2 -0
- package/dist/conformance/cases/drift.js +64 -0
- package/dist/conformance/cases/exec.d.ts +2 -0
- package/dist/conformance/cases/exec.js +125 -0
- package/dist/conformance/cases/files.d.ts +2 -0
- package/dist/conformance/cases/files.js +294 -0
- package/dist/conformance/cases/health.d.ts +2 -0
- package/dist/conformance/cases/health.js +71 -0
- package/dist/conformance/cases/index.d.ts +9 -0
- package/dist/conformance/cases/index.js +37 -0
- package/dist/conformance/cases/input.d.ts +12 -0
- package/dist/conformance/cases/input.js +109 -0
- package/dist/conformance/cases/isolation.d.ts +21 -0
- package/dist/conformance/cases/isolation.js +92 -0
- package/dist/conformance/cases/lifetime.d.ts +2 -0
- package/dist/conformance/cases/lifetime.js +64 -0
- package/dist/conformance/cases/mount.d.ts +2 -0
- package/dist/conformance/cases/mount.js +252 -0
- package/dist/conformance/cases/process.d.ts +2 -0
- package/dist/conformance/cases/process.js +240 -0
- package/dist/conformance/cases/provider.d.ts +2 -0
- package/dist/conformance/cases/provider.js +302 -0
- package/dist/conformance/cases/screen.d.ts +9 -0
- package/dist/conformance/cases/screen.js +230 -0
- package/dist/conformance/cases/settings.d.ts +57 -0
- package/dist/conformance/cases/settings.js +142 -0
- package/dist/conformance/cases/touch.d.ts +2 -0
- package/dist/conformance/cases/touch.js +117 -0
- package/dist/conformance/cases/util.d.ts +7 -0
- package/dist/conformance/cases/util.js +8 -0
- package/dist/conformance/cdp.d.ts +13 -0
- package/dist/conformance/cdp.js +88 -0
- package/dist/conformance/cli.d.ts +29 -0
- package/dist/conformance/cli.js +231 -0
- package/dist/conformance/daytona-sweep.d.ts +18 -0
- package/dist/conformance/daytona-sweep.js +118 -0
- package/dist/conformance/daytona.d.ts +83 -0
- package/dist/conformance/daytona.js +107 -0
- package/dist/conformance/docker.d.ts +38 -0
- package/dist/conformance/docker.js +70 -0
- package/dist/conformance/drift-daemon.d.ts +8 -0
- package/dist/conformance/drift-daemon.js +41 -0
- package/dist/conformance/e2b-diagnose.d.ts +16 -0
- package/dist/conformance/e2b-diagnose.js +61 -0
- package/dist/conformance/e2b-sweep.d.ts +18 -0
- package/dist/conformance/e2b-sweep.js +125 -0
- package/dist/conformance/e2b.d.ts +74 -0
- package/dist/conformance/e2b.js +111 -0
- package/dist/conformance/fixture.d.ts +65 -0
- package/dist/conformance/fixture.js +98 -0
- package/dist/conformance/index.d.ts +8 -0
- package/dist/conformance/index.js +8 -0
- package/dist/conformance/kernel.d.ts +31 -0
- package/dist/conformance/kernel.js +40 -0
- package/dist/conformance/kind.d.ts +23 -0
- package/dist/conformance/kind.js +58 -0
- package/dist/conformance/modal-sweep.d.ts +22 -0
- package/dist/conformance/modal-sweep.js +190 -0
- package/dist/conformance/modal.d.ts +69 -0
- package/dist/conformance/modal.js +76 -0
- package/dist/conformance/orgo.d.ts +41 -0
- package/dist/conformance/orgo.js +69 -0
- package/dist/conformance/phone-harness.d.ts +61 -0
- package/dist/conformance/phone-harness.js +241 -0
- package/dist/conformance/png.d.ts +16 -0
- package/dist/conformance/png.js +132 -0
- package/dist/conformance/report.d.ts +2 -0
- package/dist/conformance/report.js +86 -0
- package/dist/conformance/runner.d.ts +11 -0
- package/dist/conformance/runner.js +585 -0
- package/dist/conformance/tar.d.ts +13 -0
- package/dist/conformance/tar.js +105 -0
- package/dist/conformance/types.d.ts +257 -0
- package/dist/conformance/types.js +24 -0
- package/dist/daemon-copy.d.ts +40 -0
- package/dist/daemon-copy.js +259 -0
- package/dist/daytona.d.ts +178 -0
- package/dist/daytona.js +1484 -0
- package/dist/desktop.d.ts +24 -0
- package/dist/desktop.js +68 -0
- package/dist/docker/egress-main.d.ts +1 -0
- package/dist/docker/egress-main.js +68 -0
- package/dist/docker/egress-proxy.d.ts +53 -0
- package/dist/docker/egress-proxy.js +450 -0
- package/dist/docker/egress-table.d.ts +39 -0
- package/dist/docker/egress-table.js +197 -0
- package/dist/docker/egress.d.ts +109 -0
- package/dist/docker/egress.js +318 -0
- package/dist/docker/image.d.ts +49 -0
- package/dist/docker/image.js +302 -0
- package/dist/docker/stage.d.ts +21 -0
- package/dist/docker/stage.js +290 -0
- package/dist/docker/volume.d.ts +24 -0
- package/dist/docker/volume.js +67 -0
- package/dist/docker.d.ts +150 -0
- package/dist/docker.js +1200 -0
- package/dist/drift.d.ts +200 -0
- package/dist/drift.js +416 -0
- package/dist/e2b.d.ts +335 -0
- package/dist/e2b.js +1515 -0
- package/dist/entry.d.ts +2 -0
- package/dist/entry.js +24 -0
- package/dist/files.d.ts +50 -0
- package/dist/files.js +208 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +26 -0
- package/dist/isolation.d.ts +29 -0
- package/dist/isolation.js +71 -0
- package/dist/janitor.d.ts +50 -0
- package/dist/janitor.js +135 -0
- package/dist/kernel.d.ts +113 -0
- package/dist/kernel.js +358 -0
- package/dist/kinds.d.ts +79 -0
- package/dist/kinds.js +133 -0
- package/dist/labels.d.ts +23 -0
- package/dist/labels.js +50 -0
- package/dist/modal.d.ts +302 -0
- package/dist/modal.js +1374 -0
- package/dist/mount-archil.d.ts +164 -0
- package/dist/mount-archil.js +471 -0
- package/dist/mount.d.ts +57 -0
- package/dist/mount.js +104 -0
- package/dist/orgo-channel.d.ts +96 -0
- package/dist/orgo-channel.js +391 -0
- package/dist/orgo.d.ts +139 -0
- package/dist/orgo.js +1006 -0
- package/dist/phone-harness.d.ts +185 -0
- package/dist/phone-harness.js +1420 -0
- package/dist/processes.d.ts +24 -0
- package/dist/processes.js +40 -0
- package/dist/protocol.d.ts +729 -0
- package/dist/protocol.js +54 -0
- package/dist/provider.d.ts +245 -0
- package/dist/provider.js +37 -0
- package/dist/providers.d.ts +68 -0
- package/dist/providers.js +25 -0
- package/dist/result.d.ts +15 -0
- package/dist/result.js +2 -0
- package/dist/screen-watch.d.ts +150 -0
- package/dist/screen-watch.js +490 -0
- package/dist/test-connection.d.ts +17 -0
- package/dist/test-connection.js +26 -0
- package/dist/wait.d.ts +23 -0
- package/dist/wait.js +65 -0
- package/package.json +64 -1
package/dist/entry.d.ts
ADDED
package/dist/entry.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// How a command in this package decides it is the one being run: `if (isCommand(import.meta)) ...`. Where Node sets
|
|
2
|
+
// meta.main (22.18 and later on 22, 24.2 and later) the answer is that value. Where it does not (Node 23, 24.0-24.1,
|
|
3
|
+
// which this package's published engines allow) it compares the realpath of the script Node was given with the
|
|
4
|
+
// module's own: Node resolves a module's URL through symlinks and leaves the script's path as given (macOS's temp
|
|
5
|
+
// directory is a symlink), so only the realpath of both sides is safe. This package keeps to itself and imports nothing
|
|
6
|
+
// from the repository's scripts/, so it has its own copy of that rule here, the one place it reads the script's path.
|
|
7
|
+
import { realpathSync } from "node:fs";
|
|
8
|
+
import { resolve } from "node:path";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
const real = (path) => {
|
|
11
|
+
try {
|
|
12
|
+
return realpathSync(path);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return resolve(path);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
/** Whether the module whose `import.meta` this is runs as the command; entry is the script Node was given. */
|
|
19
|
+
export function isCommand(meta, entry = process.argv[1]) {
|
|
20
|
+
const main = meta.main;
|
|
21
|
+
if (typeof main === "boolean")
|
|
22
|
+
return main;
|
|
23
|
+
return entry !== undefined && real(entry) === real(fileURLToPath(meta.url));
|
|
24
|
+
}
|
package/dist/files.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type ComputerClient, type TarPushEndResult } from "./protocol.js";
|
|
2
|
+
import { type Result } from "./result.js";
|
|
3
|
+
export interface PulledTar {
|
|
4
|
+
/**
|
|
5
|
+
* The archive's bytes, one tar.pull.read each time the loop asks, so nothing is read ahead of it. A pull that fails
|
|
6
|
+
* throws its error's message, with the error as the cause.
|
|
7
|
+
*/
|
|
8
|
+
chunks: AsyncIterable<Uint8Array>;
|
|
9
|
+
/** How the pull ended: settled once the loop ends, is left, or the signal aborts. */
|
|
10
|
+
done: Promise<Result<{
|
|
11
|
+
bytes: number;
|
|
12
|
+
}>>;
|
|
13
|
+
}
|
|
14
|
+
/** Reads a whole file, MAX_CHUNK_BYTES per call. */
|
|
15
|
+
export declare function readFile(client: ComputerClient, path: string, options?: {
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
}): Promise<Result<Uint8Array>>;
|
|
18
|
+
/**
|
|
19
|
+
* Writes a whole file: the first call replaces it (with parents), the rest write at their offsets. A mode is set with
|
|
20
|
+
* the last chunk; until then the file also has owner write, so a read-only mode does not refuse the chunks after the
|
|
21
|
+
* first.
|
|
22
|
+
*/
|
|
23
|
+
export declare function writeFile(client: ComputerClient, path: string, bytes: Uint8Array, options?: {
|
|
24
|
+
mode?: number;
|
|
25
|
+
parents?: boolean;
|
|
26
|
+
signal?: AbortSignal;
|
|
27
|
+
}): Promise<Result<{
|
|
28
|
+
size: number;
|
|
29
|
+
}>>;
|
|
30
|
+
/**
|
|
31
|
+
* Extracts a tar archive into dir in the computer (made when missing), sent in chunks of MAX_CHUNK_BYTES, each after
|
|
32
|
+
* the last one's reply. An entry outside dir is refused (`denied`) and ends the push; what was written before it stays.
|
|
33
|
+
* A push that begins is always ended, aborted or not, so it never holds one of the connection's 8 pushes. The source is
|
|
34
|
+
* the push's: unless it is read to its end, it is let go (its iterator's `return()`) however the push ends, so a pull
|
|
35
|
+
* piped in is ended too.
|
|
36
|
+
*/
|
|
37
|
+
export declare function pushTar(client: ComputerClient, dir: string, tar: Uint8Array | AsyncIterable<Uint8Array>, options?: {
|
|
38
|
+
signal?: AbortSignal;
|
|
39
|
+
}): Promise<Result<TarPushEndResult>>;
|
|
40
|
+
/**
|
|
41
|
+
* Pulls the tar of a file or directory in the computer, a chunk each time the loop asks. The pull is ended, which frees
|
|
42
|
+
* it in the daemon, when the archive ends, the loop is left, a read fails, or the signal aborts.
|
|
43
|
+
*/
|
|
44
|
+
export declare function pullTarStream(client: ComputerClient, path: string, options?: {
|
|
45
|
+
signal?: AbortSignal;
|
|
46
|
+
}): PulledTar;
|
|
47
|
+
/** Pulls the tar of a file or directory in the computer, whole. */
|
|
48
|
+
export declare function pullTar(client: ComputerClient, path: string, options?: {
|
|
49
|
+
signal?: AbortSignal;
|
|
50
|
+
}): Promise<Result<Uint8Array>>;
|
package/dist/files.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// Files in and out of a computer past one call's limit: whole files in chunks, and tar archives both ways.
|
|
2
|
+
import { MAX_CHUNK_BYTES } from "./protocol.js";
|
|
3
|
+
import { fail, ok } from "./result.js";
|
|
4
|
+
const STOPPED = Symbol("stopped");
|
|
5
|
+
const base64 = (bytes) => Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("base64");
|
|
6
|
+
/** Reads a whole file, MAX_CHUNK_BYTES per call. */
|
|
7
|
+
export async function readFile(client, path, options = {}) {
|
|
8
|
+
const parts = [];
|
|
9
|
+
let offset = 0;
|
|
10
|
+
for (;;) {
|
|
11
|
+
const read = await client.call("files.read", { path, offset, length: MAX_CHUNK_BYTES }, options);
|
|
12
|
+
if (!read.ok)
|
|
13
|
+
return read;
|
|
14
|
+
const data = Buffer.from(read.value.data, "base64");
|
|
15
|
+
parts.push(data);
|
|
16
|
+
offset += data.length;
|
|
17
|
+
if (read.value.eof || data.length === 0)
|
|
18
|
+
return ok(Buffer.concat(parts));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Writes a whole file: the first call replaces it (with parents), the rest write at their offsets. A mode is set with
|
|
23
|
+
* the last chunk; until then the file also has owner write, so a read-only mode does not refuse the chunks after the
|
|
24
|
+
* first.
|
|
25
|
+
*/
|
|
26
|
+
export async function writeFile(client, path, bytes, options = {}) {
|
|
27
|
+
const { mode, parents, signal } = options;
|
|
28
|
+
let offset = 0;
|
|
29
|
+
let size = 0;
|
|
30
|
+
do {
|
|
31
|
+
const data = base64(bytes.subarray(offset, offset + MAX_CHUNK_BYTES));
|
|
32
|
+
const last = offset + MAX_CHUNK_BYTES >= bytes.length;
|
|
33
|
+
const firstMode = mode === undefined || last ? mode : mode | 0o200;
|
|
34
|
+
const params = offset === 0 ? { path, data, mode: firstMode, parents } : { path, data, offset, mode: last ? mode : undefined };
|
|
35
|
+
const wrote = await client.call("files.write", params, { signal });
|
|
36
|
+
if (!wrote.ok)
|
|
37
|
+
return wrote;
|
|
38
|
+
size = wrote.value.size;
|
|
39
|
+
offset += MAX_CHUNK_BYTES;
|
|
40
|
+
} while (offset < bytes.length);
|
|
41
|
+
return ok({ size });
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Extracts a tar archive into dir in the computer (made when missing), sent in chunks of MAX_CHUNK_BYTES, each after
|
|
45
|
+
* the last one's reply. An entry outside dir is refused (`denied`) and ends the push; what was written before it stays.
|
|
46
|
+
* A push that begins is always ended, aborted or not, so it never holds one of the connection's 8 pushes. The source is
|
|
47
|
+
* the push's: unless it is read to its end, it is let go (its iterator's `return()`) however the push ends, so a pull
|
|
48
|
+
* piped in is ended too.
|
|
49
|
+
*/
|
|
50
|
+
export async function pushTar(client, dir, tar, options = {}) {
|
|
51
|
+
const { signal } = options;
|
|
52
|
+
const source = tar instanceof Uint8Array
|
|
53
|
+
? (async function* () {
|
|
54
|
+
yield tar;
|
|
55
|
+
})()
|
|
56
|
+
: tar;
|
|
57
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
58
|
+
let open = true;
|
|
59
|
+
const next = () => iterator.next().then((step) => {
|
|
60
|
+
if (step.done)
|
|
61
|
+
open = false;
|
|
62
|
+
return step;
|
|
63
|
+
}, (error) => {
|
|
64
|
+
open = false;
|
|
65
|
+
throw error;
|
|
66
|
+
});
|
|
67
|
+
// A source that stalls must not hold the push open past an abort: each piece races the signal.
|
|
68
|
+
let onAbort = () => { };
|
|
69
|
+
const stopped = new Promise((resolve) => {
|
|
70
|
+
onAbort = () => resolve(STOPPED);
|
|
71
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
72
|
+
});
|
|
73
|
+
try {
|
|
74
|
+
if (signal?.aborted)
|
|
75
|
+
return fail("aborted", `the push into ${dir} was aborted`);
|
|
76
|
+
const begun = await client.call("tar.push.begin", { path: dir });
|
|
77
|
+
if (!begun.ok)
|
|
78
|
+
return begun;
|
|
79
|
+
const { id } = begun.value;
|
|
80
|
+
if (signal?.aborted)
|
|
81
|
+
return abandon(client, id, fail("aborted", `the push into ${dir} was aborted`));
|
|
82
|
+
const pending = Buffer.alloc(MAX_CHUNK_BYTES);
|
|
83
|
+
let filled = 0;
|
|
84
|
+
const send = async () => {
|
|
85
|
+
const wrote = await client.call("tar.push.write", { id, data: base64(pending.subarray(0, filled)) }, options);
|
|
86
|
+
filled = 0;
|
|
87
|
+
// A refused write ended the push in the daemon; an aborted one did not.
|
|
88
|
+
return !wrote.ok && wrote.error.code === "aborted" ? abandon(client, id, wrote) : wrote;
|
|
89
|
+
};
|
|
90
|
+
try {
|
|
91
|
+
for (;;) {
|
|
92
|
+
const step = signal ? await Promise.race([next(), stopped]) : await next();
|
|
93
|
+
if (step === STOPPED)
|
|
94
|
+
return abandon(client, id, fail("aborted", `the push into ${dir} was aborted`));
|
|
95
|
+
if (step.done)
|
|
96
|
+
break;
|
|
97
|
+
const piece = step.value;
|
|
98
|
+
for (let at = 0; at < piece.length;) {
|
|
99
|
+
const take = Math.min(MAX_CHUNK_BYTES - filled, piece.length - at);
|
|
100
|
+
pending.set(piece.subarray(at, at + take), filled);
|
|
101
|
+
filled += take;
|
|
102
|
+
at += take;
|
|
103
|
+
if (filled === MAX_CHUNK_BYTES) {
|
|
104
|
+
const wrote = await send();
|
|
105
|
+
if (!wrote.ok)
|
|
106
|
+
return wrote;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
return abandon(client, id, fail("invalid", `the tar source failed: ${error instanceof Error ? error.message : String(error)}`));
|
|
113
|
+
}
|
|
114
|
+
if (filled > 0) {
|
|
115
|
+
const wrote = await send();
|
|
116
|
+
if (!wrote.ok)
|
|
117
|
+
return wrote;
|
|
118
|
+
}
|
|
119
|
+
if (signal?.aborted)
|
|
120
|
+
return abandon(client, id, fail("aborted", `the push into ${dir} was aborted`));
|
|
121
|
+
return client.call("tar.push.end", { id }, options);
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
signal?.removeEventListener("abort", onAbort);
|
|
125
|
+
if (open)
|
|
126
|
+
letGo(iterator);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/** Calls a source's return() once, without waiting on it (a stalled source may never answer); its errors are dropped. */
|
|
130
|
+
function letGo(iterator) {
|
|
131
|
+
try {
|
|
132
|
+
void Promise.resolve(iterator.return?.()).catch(() => { });
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
// A return() that throws at once has nothing more to let go.
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/** Ends a push this side gave up on, then returns why. */
|
|
139
|
+
async function abandon(client, id, why) {
|
|
140
|
+
await client.call("tar.push.end", { id });
|
|
141
|
+
return why;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Pulls the tar of a file or directory in the computer, a chunk each time the loop asks. The pull is ended, which frees
|
|
145
|
+
* it in the daemon, when the archive ends, the loop is left, a read fails, or the signal aborts.
|
|
146
|
+
*/
|
|
147
|
+
export function pullTarStream(client, path, options = {}) {
|
|
148
|
+
const { signal } = options;
|
|
149
|
+
let settle;
|
|
150
|
+
const done = new Promise((resolve) => (settle = resolve));
|
|
151
|
+
const aborted = { code: "aborted", message: `the pull of ${path} was aborted` };
|
|
152
|
+
const thrown = (error) => new Error(error.message, { cause: error });
|
|
153
|
+
async function* chunks() {
|
|
154
|
+
if (signal?.aborted) {
|
|
155
|
+
settle({ ok: false, error: aborted });
|
|
156
|
+
throw thrown(aborted);
|
|
157
|
+
}
|
|
158
|
+
const begun = await client.call("tar.pull.begin", { path });
|
|
159
|
+
if (!begun.ok) {
|
|
160
|
+
settle(begun);
|
|
161
|
+
throw thrown(begun.error);
|
|
162
|
+
}
|
|
163
|
+
const { id } = begun.value;
|
|
164
|
+
const end = () => client.call("tar.pull.end", { id });
|
|
165
|
+
const onAbort = () => void end().then(() => settle({ ok: false, error: aborted }));
|
|
166
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
167
|
+
let ended = fail("aborted", `the loop over the pull of ${path} was left before its end`);
|
|
168
|
+
let bytes = 0;
|
|
169
|
+
try {
|
|
170
|
+
for (;;) {
|
|
171
|
+
// An aborted signal, even one aborted while the pull began, fails this read before it is sent.
|
|
172
|
+
const read = await client.call("tar.pull.read", { id }, { signal });
|
|
173
|
+
if (!read.ok) {
|
|
174
|
+
ended = read;
|
|
175
|
+
throw thrown(read.error);
|
|
176
|
+
}
|
|
177
|
+
const data = Buffer.from(read.value.data, "base64");
|
|
178
|
+
bytes += data.length;
|
|
179
|
+
if (data.length > 0)
|
|
180
|
+
yield data;
|
|
181
|
+
if (read.value.eof) {
|
|
182
|
+
ended = ok({ bytes });
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
finally {
|
|
188
|
+
signal?.removeEventListener("abort", onAbort);
|
|
189
|
+
await end();
|
|
190
|
+
settle(ended);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return { chunks: chunks(), done };
|
|
194
|
+
}
|
|
195
|
+
/** Pulls the tar of a file or directory in the computer, whole. */
|
|
196
|
+
export async function pullTar(client, path, options = {}) {
|
|
197
|
+
const { chunks, done } = pullTarStream(client, path, options);
|
|
198
|
+
const parts = [];
|
|
199
|
+
try {
|
|
200
|
+
for await (const chunk of chunks)
|
|
201
|
+
parts.push(chunk);
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
// done says why.
|
|
205
|
+
}
|
|
206
|
+
const ended = await done;
|
|
207
|
+
return ended.ok ? ok(Buffer.concat(parts)) : ended;
|
|
208
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export * from "./protocol.js";
|
|
2
|
+
export * from "./result.js";
|
|
3
|
+
export * from "./provider.js";
|
|
4
|
+
export * from "./mount.js";
|
|
5
|
+
export { type CallOptions, DaemonClient, type DaemonStream } from "./client.js";
|
|
6
|
+
export { type ActionFailure, type CheckActionsOptions, checkActions, isKey } from "./actions.js";
|
|
7
|
+
export { pullTar, pullTarStream, pushTar, readFile, writeFile, type PulledTar } from "./files.js";
|
|
8
|
+
export { listProcesses, readAll, type ReadAllResult } from "./processes.js";
|
|
9
|
+
export { type ActionMapper, createActionMapper, daemonWatch, redactWords, type ScreenFrame, type ScreenFrames, type ScreenWatch, ScreenWatchError, type ScreenWatchHandlers, toAction, type ViewerEvent, type WatchEndCode, watchEndCode, type WatchOptions, watchScreen, } from "./screen-watch.js";
|
|
10
|
+
export { type DockerDesktopOptions, type DockerEgressOptions, DockerProvider, type DockerProviderOptions } from "./docker.js";
|
|
11
|
+
export { type DockerVolume, VOLUME_SUBPATH_ENGINE } from "./docker/volume.js";
|
|
12
|
+
export { DEFAULT_DOCKER_IMAGE, DOCKER_BASE, DOCKER_RECIPE, DOCKER_RECIPE_HAS, ensureImage, type EnsureImageOptions, IMAGE_REPOSITORY, IMAGE_TIMEOUT_MS, type ImageSource, imageSourceProblem, type Making, RECIPE_LABEL, recipeHash, recipeTag, } from "./docker/image.js";
|
|
13
|
+
export { EGRESS_IMAGE, EGRESS_LABEL, EGRESS_MOUNT, EGRESS_SOCKET } from "./docker/egress.js";
|
|
14
|
+
export { DAYTONA_DEFAULT_SNAPSHOT, DaytonaProvider, type DaytonaProviderOptions, type DaytonaTimeouts, type WebSocketFactory, type WebSocketLike } from "./daytona.js";
|
|
15
|
+
export { ModalProvider, type ModalApp, type ModalByteSink, type ModalCreateParams, type ModalImage, type ModalProcess, type ModalProviderOptions, type ModalSandbox, type ModalSdk, type ModalTimeouts, } from "./modal.js";
|
|
16
|
+
export { ARCHIL_HELPER, ARCHIL_MOUNT_LABEL, ARCHIL_SYNC_SOCKET, ARCHIL_TOOL_UID, ARCHIL_TOOL_USER, ARCHIL_VERSION, type ArchilMount, type ArchilMountOptions, type ArchilRoot, type ProviderNote, archilLabels, archilMount, archilReadBack, archilRefusals, archilRelease, claimEpoch, helperSha256, mountDrift, mountWords, type RecordedArchil, readArchilMount, recordedArchil, recordedMount, syncingClient, } from "./mount-archil.js";
|
|
17
|
+
export { BROWSER_IMAGE, type BrowserStream, BrowserVendorError, CREATE_LABEL, causeOf, memoryUrls, RECOVER_MS, type ReleaseConfirm, type RestReply, type UrlStore, } from "./browser.js";
|
|
18
|
+
export { BROWSERBASE_API_URL, BROWSERBASE_MAX_LIFETIME_MS, type BrowserbaseApi, type BrowserbaseDebug, BrowserbaseProvider, type BrowserbaseProviderOptions, type BrowserbaseSession, type BrowserbaseSessionSettings, browserbaseRest, } from "./browserbase.js";
|
|
19
|
+
export { KERNEL_API_URL, KERNEL_MAX_LIFETIME_MS, type KernelBrowserSettings, KernelProvider, type KernelProviderOptions, type KernelRequest, kernelRest } from "./kernel.js";
|
|
20
|
+
export { DAEMON_IN_COMPUTER, drift, helloDrift, hostOf, inspectDrift, mountOf, networkOf, type DockerHost, type DockerInfo, type DockerInspect, type DriftExpected, type HelloExpected } from "./drift.js";
|
|
21
|
+
export { type ReleasedOlder, releaseOlder, sweep, type SweepOptions, type Swept } from "./janitor.js";
|
|
22
|
+
export { EPOCH_LABEL, epochOf, epochProblem, RUN_LABEL, runLabels, runProblem } from "./labels.js";
|
|
23
|
+
export { waitUntil, type WaitOptions } from "./wait.js";
|
|
24
|
+
export { type ConformanceModule, isProviderId, type ProviderEnv, providerFor, type ProviderId, providerIds, type ProviderModule, providerModule, type ProviderOptions, providers, } from "./providers.js";
|
|
25
|
+
export type { PhoneLedger, PhoneLedgerEntry } from "./phone-harness.js";
|
|
26
|
+
export { type ComputerSettingKind, type KindCapabilities, kindOf, type ProviderEntry, type ProviderKey, PROVIDERS, SETTING_KIND_NAMES, SETTING_KINDS, type SettingKindSpec, serves, servingRefusals, type TestConnection, type TestConnectionOptions, } from "./kinds.js";
|
|
27
|
+
export * from "./conformance/index.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export * from "./protocol.js";
|
|
2
|
+
export * from "./result.js";
|
|
3
|
+
export * from "./provider.js";
|
|
4
|
+
export * from "./mount.js";
|
|
5
|
+
export { DaemonClient } from "./client.js";
|
|
6
|
+
export { checkActions, isKey } from "./actions.js";
|
|
7
|
+
export { pullTar, pullTarStream, pushTar, readFile, writeFile } from "./files.js";
|
|
8
|
+
export { listProcesses, readAll } from "./processes.js";
|
|
9
|
+
export { createActionMapper, daemonWatch, redactWords, ScreenWatchError, toAction, watchEndCode, watchScreen, } from "./screen-watch.js";
|
|
10
|
+
export { DockerProvider } from "./docker.js";
|
|
11
|
+
export { VOLUME_SUBPATH_ENGINE } from "./docker/volume.js";
|
|
12
|
+
export { DEFAULT_DOCKER_IMAGE, DOCKER_BASE, DOCKER_RECIPE, DOCKER_RECIPE_HAS, ensureImage, IMAGE_REPOSITORY, IMAGE_TIMEOUT_MS, imageSourceProblem, RECIPE_LABEL, recipeHash, recipeTag, } from "./docker/image.js";
|
|
13
|
+
export { EGRESS_IMAGE, EGRESS_LABEL, EGRESS_MOUNT, EGRESS_SOCKET } from "./docker/egress.js";
|
|
14
|
+
export { DAYTONA_DEFAULT_SNAPSHOT, DaytonaProvider } from "./daytona.js";
|
|
15
|
+
export { ModalProvider, } from "./modal.js";
|
|
16
|
+
export { ARCHIL_HELPER, ARCHIL_MOUNT_LABEL, ARCHIL_SYNC_SOCKET, ARCHIL_TOOL_UID, ARCHIL_TOOL_USER, ARCHIL_VERSION, archilLabels, archilMount, archilReadBack, archilRefusals, archilRelease, claimEpoch, helperSha256, mountDrift, mountWords, readArchilMount, recordedArchil, recordedMount, syncingClient, } from "./mount-archil.js";
|
|
17
|
+
export { BROWSER_IMAGE, BrowserVendorError, CREATE_LABEL, causeOf, memoryUrls, RECOVER_MS, } from "./browser.js";
|
|
18
|
+
export { BROWSERBASE_API_URL, BROWSERBASE_MAX_LIFETIME_MS, BrowserbaseProvider, browserbaseRest, } from "./browserbase.js";
|
|
19
|
+
export { KERNEL_API_URL, KERNEL_MAX_LIFETIME_MS, KernelProvider, kernelRest } from "./kernel.js";
|
|
20
|
+
export { DAEMON_IN_COMPUTER, drift, helloDrift, hostOf, inspectDrift, mountOf, networkOf } from "./drift.js";
|
|
21
|
+
export { releaseOlder, sweep } from "./janitor.js";
|
|
22
|
+
export { EPOCH_LABEL, epochOf, epochProblem, RUN_LABEL, runLabels, runProblem } from "./labels.js";
|
|
23
|
+
export { waitUntil } from "./wait.js";
|
|
24
|
+
export { isProviderId, providerFor, providerIds, providerModule, providers, } from "./providers.js";
|
|
25
|
+
export { kindOf, PROVIDERS, SETTING_KIND_NAMES, SETTING_KINDS, serves, servingRefusals, } from "./kinds.js";
|
|
26
|
+
export * from "./conformance/index.js";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ComputerClient } from "./protocol.js";
|
|
2
|
+
import { type Result } from "./result.js";
|
|
3
|
+
/** The uid:gid the programs of a remote computer run as by default: the box image's agentrun-tool, no sudo. */
|
|
4
|
+
export declare const TOOL_USER = "2510:2510";
|
|
5
|
+
/** Why a remote provider refuses network open or an allowlist, in words. */
|
|
6
|
+
export declare const OWN_NETWORK_ONLY = "a remote computer takes network \"none\" only: its programs run in a network of their own, their loopback alone, so the vendor's agent in it (which runs commands as root) is out of their reach; open and allowlists wait for a proxy out of that network";
|
|
7
|
+
/** The interfaces /proc/<pid>/net/dev lists, in its order; undefined when the text is not that file. */
|
|
8
|
+
export declare function netDevInterfaces(text: string): string[] | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* The drift of a computer's programs' network from their own loopback alone, in words: none when the live interfaces
|
|
11
|
+
* (up or addressed) of the daemon's network namespace are `lo` alone, else what they are (or that they could not be read).
|
|
12
|
+
*/
|
|
13
|
+
export declare function ownNetworkDrift(interfaces: string[] | undefined): string[];
|
|
14
|
+
/** What computerd probe reports of its own network namespace: the live interfaces, the quiet ones, the raised ones. */
|
|
15
|
+
export interface OwnNetworkReport {
|
|
16
|
+
interfaces?: unknown;
|
|
17
|
+
quiet?: unknown;
|
|
18
|
+
raised?: unknown;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The drift of a probe report from the own network, in words: its live interfaces `lo` alone (a kernel's quiet tunnels,
|
|
22
|
+
* down and addressless, are no drift), and none a program of the computer's user could bring up.
|
|
23
|
+
*/
|
|
24
|
+
export declare function probeOwnNetworkDrift(report: OwnNetworkReport): string[];
|
|
25
|
+
/**
|
|
26
|
+
* A computer's own network read back as its user sees it: computerd's probe, with no target, through the daemon's
|
|
27
|
+
* exec.run (as the computer's user), and its drift (probeOwnNetworkDrift).
|
|
28
|
+
*/
|
|
29
|
+
export declare function readOwnNetwork(client: ComputerClient, timeoutMs: number, signal?: AbortSignal): Promise<Result<string[]>>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// What a remote computer's programs are cut off from, as the providers ask for it and read it back. A remote vendor runs
|
|
2
|
+
// its own agent in the computer beside them (Daytona's toolbox, E2B's envd), one that runs commands as root or with sudo
|
|
3
|
+
// for whoever reaches it. So every remote provider starts init with --own-network: init, still root, refuses to start
|
|
4
|
+
// while another process holds the programs' uid, moves itself into a network namespace whose loopback alone is up or
|
|
5
|
+
// addressed, and refuses vsock and io_uring to every thread (daemon/internal/isolate). A kernel with the tunnel modules
|
|
6
|
+
// loaded also gives that namespace its fallback tunnels (tunl0, gre0, sit0, ...), down with no address; no program can
|
|
7
|
+
// raise them, holding no CAP_NET_ADMIN, and the read-back here proves it through computerd's probe. Network "none" is
|
|
8
|
+
// all such a computer takes until a proxy carries open and allowlists out of that namespace.
|
|
9
|
+
import { DAEMON_IN_COMPUTER } from "./drift.js";
|
|
10
|
+
import { fail, ok } from "./result.js";
|
|
11
|
+
/** The uid:gid the programs of a remote computer run as by default: the box image's agentrun-tool, no sudo. */
|
|
12
|
+
export const TOOL_USER = "2510:2510";
|
|
13
|
+
/** Why a remote provider refuses network open or an allowlist, in words. */
|
|
14
|
+
export const OWN_NETWORK_ONLY = 'a remote computer takes network "none" only: its programs run in a network of their own, their loopback alone, so the vendor\'s agent in it (which runs commands as root) is out of their reach; open and allowlists wait for a proxy out of that network';
|
|
15
|
+
/** The interfaces /proc/<pid>/net/dev lists, in its order; undefined when the text is not that file. */
|
|
16
|
+
export function netDevInterfaces(text) {
|
|
17
|
+
const lines = text.split("\n");
|
|
18
|
+
if (!lines[0]?.startsWith("Inter-|") || !lines[1]?.includes("face |"))
|
|
19
|
+
return undefined;
|
|
20
|
+
const names = [];
|
|
21
|
+
for (const line of lines.slice(2)) {
|
|
22
|
+
if (line.trim() === "")
|
|
23
|
+
continue;
|
|
24
|
+
const at = line.indexOf(":");
|
|
25
|
+
if (at < 0)
|
|
26
|
+
return undefined;
|
|
27
|
+
names.push(line.slice(0, at).trim());
|
|
28
|
+
}
|
|
29
|
+
return names;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The drift of a computer's programs' network from their own loopback alone, in words: none when the live interfaces
|
|
33
|
+
* (up or addressed) of the daemon's network namespace are `lo` alone, else what they are (or that they could not be read).
|
|
34
|
+
*/
|
|
35
|
+
export function ownNetworkDrift(interfaces) {
|
|
36
|
+
if (interfaces === undefined)
|
|
37
|
+
return ["daemon network: declared its own (lo alone), read back nothing it could read"];
|
|
38
|
+
if (interfaces.length === 1 && interfaces[0] === "lo")
|
|
39
|
+
return [];
|
|
40
|
+
return [`daemon network: declared its own (lo alone), read back ${interfaces.length === 0 ? "no interface" : interfaces.join(", ")}`];
|
|
41
|
+
}
|
|
42
|
+
const names = (value) => (Array.isArray(value) && value.every((one) => typeof one === "string") ? value : undefined);
|
|
43
|
+
/**
|
|
44
|
+
* The drift of a probe report from the own network, in words: its live interfaces `lo` alone (a kernel's quiet tunnels,
|
|
45
|
+
* down and addressless, are no drift), and none a program of the computer's user could bring up.
|
|
46
|
+
*/
|
|
47
|
+
export function probeOwnNetworkDrift(report) {
|
|
48
|
+
const found = ownNetworkDrift(names(report.interfaces));
|
|
49
|
+
const raised = names(report.raised) ?? [];
|
|
50
|
+
if (raised.length > 0)
|
|
51
|
+
found.push(`daemon network: a program of the computer's user brought up ${raised.join(", ")}, which no program of its own network may`);
|
|
52
|
+
return found;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* A computer's own network read back as its user sees it: computerd's probe, with no target, through the daemon's
|
|
56
|
+
* exec.run (as the computer's user), and its drift (probeOwnNetworkDrift).
|
|
57
|
+
*/
|
|
58
|
+
export async function readOwnNetwork(client, timeoutMs, signal) {
|
|
59
|
+
const ran = await client.call("exec.run", { argv: [DAEMON_IN_COMPUTER, "probe"], timeoutMs }, signal ? { signal } : undefined);
|
|
60
|
+
if (!ran.ok)
|
|
61
|
+
return fail(ran.error.code, `reading the computer's own network back (computerd probe): ${ran.error.message}`);
|
|
62
|
+
const out = Buffer.from(ran.value.stdout, "base64").toString("utf8");
|
|
63
|
+
if (ran.value.exitCode !== 0)
|
|
64
|
+
return fail("refused", `computerd probe, reading the computer's own network back, exited ${ran.value.exitCode}: ${Buffer.from(ran.value.stderr, "base64").toString("utf8").trim().slice(0, 300)}`);
|
|
65
|
+
try {
|
|
66
|
+
return ok(probeOwnNetworkDrift(JSON.parse(out)));
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return fail("refused", `computerd probe, reading the computer's own network back, wrote what is not its report: ${out.slice(0, 200)}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { CallContext, ComputerProvider, ComputerRef } from "./provider.js";
|
|
2
|
+
import { type Result } from "./result.js";
|
|
3
|
+
export interface SweepOptions {
|
|
4
|
+
/** Only computers with all of these labels; default every computer the provider made. */
|
|
5
|
+
labels?: Record<string, string>;
|
|
6
|
+
/** The time to compare deadlines with; default now. */
|
|
7
|
+
now?: Date;
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
}
|
|
10
|
+
export interface Swept {
|
|
11
|
+
/** The names of the computers deleted. */
|
|
12
|
+
deleted: string[];
|
|
13
|
+
/** How many were left, their deadline still ahead. */
|
|
14
|
+
kept: number;
|
|
15
|
+
/** For a provider with sweepLeftovers: what its computers left behind that the sweep removed, by name. */
|
|
16
|
+
leftovers?: string[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Deletes every computer the provider lists whose deadline has passed, or whose deadline is missing or unreadable
|
|
20
|
+
* (fail closed); leaves the rest. A deadline more than the provider's maxLifetimeMs past now (with a minute for clock
|
|
21
|
+
* skew) counts as unreadable: no computer the provider made can carry one. Only for a provider that declares lifetime
|
|
22
|
+
* "janitor"; a caller runs it on a schedule. A computer that cannot be deleted does not stop the sweep: the result is
|
|
23
|
+
* then an error naming it and what was deleted. A provider with sweepLeftovers then removes, with the same labels and
|
|
24
|
+
* time, what its computers left behind (Docker: egress proxies and volumes whose computer is gone).
|
|
25
|
+
*/
|
|
26
|
+
export declare function sweep(provider: ComputerProvider, { labels, now, signal }?: SweepOptions): Promise<Result<Swept>>;
|
|
27
|
+
export interface ReleasedOlder {
|
|
28
|
+
/** The run's computers deleted: each with a lower epoch, and each that carries no epoch it can read. */
|
|
29
|
+
released: ComputerRef[];
|
|
30
|
+
/** The run's computers of this very epoch, left as they are. */
|
|
31
|
+
kept: ComputerRef[];
|
|
32
|
+
/** Of those released, each whose epoch label was missing or not an epoch, with what was wrong: it could not prove it is current. */
|
|
33
|
+
unproven: Array<{
|
|
34
|
+
ref: ComputerRef;
|
|
35
|
+
problem: string;
|
|
36
|
+
}>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A takeover's first step, for a new claim of run at epoch: deletes every computer of the run labelled with a lower
|
|
40
|
+
* epoch, and every one of the run whose epoch label is missing or unreadable, since it cannot prove it is current. The
|
|
41
|
+
* run's computers of this epoch are kept. A computer of the run with a higher epoch means a later claim holds the run:
|
|
42
|
+
* the call is refused as fenced before it releases anything. It lists by the run's label, and still holds each listed
|
|
43
|
+
* computer to it: a computer of another run is never touched, whatever list gives.
|
|
44
|
+
*
|
|
45
|
+
* Every release is attempted. When any fails, the result is an error naming each computer left and why, and what was
|
|
46
|
+
* released: a partial cleanup is never a success, so a caller revokes nothing and mounts nothing after it. An aborted
|
|
47
|
+
* call starts no further release and says which ran. It revokes no mount: that is the root helper's, inside the
|
|
48
|
+
* computer, and a deleted computer holds none.
|
|
49
|
+
*/
|
|
50
|
+
export declare function releaseOlder(provider: ComputerProvider, run: string, epoch: number, { signal }?: CallContext): Promise<Result<ReleasedOlder>>;
|
package/dist/janitor.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// The janitor: ends computers whose deadline has passed, for a provider whose vendor does not end them itself; and, on a
|
|
2
|
+
// takeover, the run's computers of an older claim.
|
|
3
|
+
import { epochOf, epochProblem, RUN_LABEL, runProblem } from "./labels.js";
|
|
4
|
+
import { fail, ok } from "./result.js";
|
|
5
|
+
/** Clock skew a deadline may carry past now + maxLifetimeMs before it counts as unreadable. */
|
|
6
|
+
const SKEW_MS = 60_000;
|
|
7
|
+
/**
|
|
8
|
+
* Deletes every computer the provider lists whose deadline has passed, or whose deadline is missing or unreadable
|
|
9
|
+
* (fail closed); leaves the rest. A deadline more than the provider's maxLifetimeMs past now (with a minute for clock
|
|
10
|
+
* skew) counts as unreadable: no computer the provider made can carry one. Only for a provider that declares lifetime
|
|
11
|
+
* "janitor"; a caller runs it on a schedule. A computer that cannot be deleted does not stop the sweep: the result is
|
|
12
|
+
* then an error naming it and what was deleted. A provider with sweepLeftovers then removes, with the same labels and
|
|
13
|
+
* time, what its computers left behind (Docker: egress proxies and volumes whose computer is gone).
|
|
14
|
+
*/
|
|
15
|
+
export async function sweep(provider, { labels = {}, now, signal } = {}) {
|
|
16
|
+
const { lifetime, maxLifetimeMs } = provider.capabilities();
|
|
17
|
+
if (lifetime !== "janitor") {
|
|
18
|
+
return fail("refused", `the ${provider.id} provider declares lifetime "${lifetime}": its vendor ends its computers itself, so the janitor does not sweep them`);
|
|
19
|
+
}
|
|
20
|
+
if (now !== undefined && !(now instanceof Date && !Number.isNaN(now.getTime())))
|
|
21
|
+
return fail("invalid", "now must be a Date");
|
|
22
|
+
const at = (now ?? new Date()).getTime();
|
|
23
|
+
// A provider that declares no cap has no over-cap line: the janitor never deletes on a rule it cannot read.
|
|
24
|
+
const cap = Number.isSafeInteger(maxLifetimeMs) && maxLifetimeMs > 0 ? maxLifetimeMs + SKEW_MS : Number.POSITIVE_INFINITY;
|
|
25
|
+
if (signal?.aborted)
|
|
26
|
+
return fail("aborted", "the sweep was aborted before it began");
|
|
27
|
+
const listed = await provider.list(labels, { signal });
|
|
28
|
+
if (!listed.ok)
|
|
29
|
+
return listed;
|
|
30
|
+
const deleted = [];
|
|
31
|
+
const failed = [];
|
|
32
|
+
let firstError;
|
|
33
|
+
let kept = 0;
|
|
34
|
+
for (const { ref, deadline } of listed.value) {
|
|
35
|
+
const time = deadline instanceof Date ? deadline.getTime() : Number.NaN;
|
|
36
|
+
if (!Number.isNaN(time) && time > at && time <= at + cap) {
|
|
37
|
+
kept++;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (signal?.aborted)
|
|
41
|
+
return fail("aborted", `the sweep was aborted; it had deleted ${deleted.join(", ") || "none"}`);
|
|
42
|
+
const released = await provider.release(ref, "delete");
|
|
43
|
+
if (released.ok)
|
|
44
|
+
deleted.push(ref.name);
|
|
45
|
+
else {
|
|
46
|
+
firstError ??= released.error;
|
|
47
|
+
failed.push(`${ref.name}: ${released.error.message}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// Then what the computers left behind (a provider's own parts whose computer is gone), after the computers, since a
|
|
51
|
+
// computer released above takes its own parts with it.
|
|
52
|
+
let leftovers;
|
|
53
|
+
let leftBehind;
|
|
54
|
+
if (provider.sweepLeftovers) {
|
|
55
|
+
if (signal?.aborted)
|
|
56
|
+
return fail("aborted", `the sweep was aborted; it had deleted ${deleted.join(", ") || "none"}`);
|
|
57
|
+
const left = await provider.sweepLeftovers({ labels, now: new Date(at), signal });
|
|
58
|
+
if (left.ok)
|
|
59
|
+
leftovers = left.value;
|
|
60
|
+
else {
|
|
61
|
+
firstError ??= left.error;
|
|
62
|
+
leftBehind = left.error.message;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (firstError) {
|
|
66
|
+
const words = [...(failed.length > 0 ? [`could not delete ${failed.join("; ")}`] : []), ...(leftBehind === undefined ? [] : [`could not remove what its computers left behind: ${leftBehind}`])];
|
|
67
|
+
return fail(firstError.code, `the sweep deleted ${deleted.join(", ") || "none"} but ${words.join(", and ")}`);
|
|
68
|
+
}
|
|
69
|
+
return ok({ deleted, kept, ...(leftovers === undefined ? {} : { leftovers }) });
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* A takeover's first step, for a new claim of run at epoch: deletes every computer of the run labelled with a lower
|
|
73
|
+
* epoch, and every one of the run whose epoch label is missing or unreadable, since it cannot prove it is current. The
|
|
74
|
+
* run's computers of this epoch are kept. A computer of the run with a higher epoch means a later claim holds the run:
|
|
75
|
+
* the call is refused as fenced before it releases anything. It lists by the run's label, and still holds each listed
|
|
76
|
+
* computer to it: a computer of another run is never touched, whatever list gives.
|
|
77
|
+
*
|
|
78
|
+
* Every release is attempted. When any fails, the result is an error naming each computer left and why, and what was
|
|
79
|
+
* released: a partial cleanup is never a success, so a caller revokes nothing and mounts nothing after it. An aborted
|
|
80
|
+
* call starts no further release and says which ran. It revokes no mount: that is the root helper's, inside the
|
|
81
|
+
* computer, and a deleted computer holds none.
|
|
82
|
+
*/
|
|
83
|
+
export async function releaseOlder(provider, run, epoch, { signal } = {}) {
|
|
84
|
+
const problem = runProblem(run) ?? epochProblem(epoch);
|
|
85
|
+
if (problem !== undefined)
|
|
86
|
+
return fail("invalid", problem);
|
|
87
|
+
const what = `the release of run ${JSON.stringify(run)}'s computers below epoch ${epoch}`;
|
|
88
|
+
if (signal?.aborted)
|
|
89
|
+
return fail("aborted", `${what} was aborted before it began`);
|
|
90
|
+
const listed = await provider.list({ [RUN_LABEL]: run }, { signal });
|
|
91
|
+
if (!listed.ok)
|
|
92
|
+
return fail(listed.error.code, `could not list run ${JSON.stringify(run)}'s computers: ${listed.error.message}`);
|
|
93
|
+
const older = [];
|
|
94
|
+
const kept = [];
|
|
95
|
+
const unproven = [];
|
|
96
|
+
const newer = [];
|
|
97
|
+
for (const { ref, labels } of listed.value) {
|
|
98
|
+
if (labels?.[RUN_LABEL] !== run)
|
|
99
|
+
continue;
|
|
100
|
+
const read = epochOf(labels);
|
|
101
|
+
if (!read.ok) {
|
|
102
|
+
older.push({ ref });
|
|
103
|
+
unproven.push({ ref, problem: read.error.message });
|
|
104
|
+
}
|
|
105
|
+
else if (read.value < epoch)
|
|
106
|
+
older.push({ ref, epoch: read.value });
|
|
107
|
+
else if (read.value === epoch)
|
|
108
|
+
kept.push(ref);
|
|
109
|
+
else
|
|
110
|
+
newer.push(`${ref.name} at epoch ${read.value}`);
|
|
111
|
+
}
|
|
112
|
+
if (newer.length > 0) {
|
|
113
|
+
return fail("refused", `fenced: run ${JSON.stringify(run)} has ${newer.join(", ")}, above this claim's epoch ${epoch}; a later claim holds the run, so nothing was released`);
|
|
114
|
+
}
|
|
115
|
+
const released = [];
|
|
116
|
+
const failed = [];
|
|
117
|
+
let firstError;
|
|
118
|
+
const done = () => `it released ${released.map((ref) => ref.name).join(", ") || "none"}${failed.length > 0 ? `, and could not release ${failed.join("; ")}` : ""}`;
|
|
119
|
+
for (const [at, { ref, epoch: was }] of older.entries()) {
|
|
120
|
+
if (signal?.aborted) {
|
|
121
|
+
const left = older.slice(at).map((one) => one.ref.name);
|
|
122
|
+
return fail("aborted", `${what} was aborted: ${done()}; it did not start ${left.join(", ")}`);
|
|
123
|
+
}
|
|
124
|
+
const result = await provider.release(ref, "delete");
|
|
125
|
+
if (result.ok)
|
|
126
|
+
released.push(ref);
|
|
127
|
+
else {
|
|
128
|
+
firstError ??= result.error;
|
|
129
|
+
failed.push(`${ref.name} (${was === undefined ? "no epoch" : `epoch ${was}`}): ${result.error.message}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (firstError)
|
|
133
|
+
return fail(firstError.code, `${what} is incomplete: ${done()}`);
|
|
134
|
+
return ok({ released, kept, unproven });
|
|
135
|
+
}
|