@parcha/agentrun-computer 0.1.0-beta.59 → 0.1.0-beta.61
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 +118 -26
- package/dist/agent37.d.ts +11 -4
- package/dist/agent37.js +212 -11
- package/dist/browser.d.ts +66 -11
- package/dist/browser.js +95 -17
- package/dist/browserbase.d.ts +18 -4
- package/dist/browserbase.js +171 -26
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/computerd.json +1 -1
- package/dist/conformance/agent37.d.ts +10 -2
- package/dist/conformance/agent37.js +8 -7
- package/dist/conformance/cases/provider.js +4 -1
- package/dist/descriptor.d.ts +27 -0
- package/dist/descriptor.js +2 -0
- package/dist/descriptors/agent37.d.ts +4 -1
- package/dist/descriptors/agent37.js +5 -2
- package/dist/descriptors/docker.d.ts +1 -0
- package/dist/descriptors/docker.js +2 -0
- package/dist/descriptors/index.d.ts +5 -1
- package/dist/host.d.ts +4 -1
- package/dist/hosted/provider.d.ts +3 -1
- package/dist/hosted/provider.js +26 -6
- package/dist/hosted/ssh-exec.d.ts +7 -1
- package/dist/hosted/ssh-exec.js +19 -14
- package/dist/hosted/ssh-key.d.ts +18 -0
- package/dist/hosted/ssh-key.js +36 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/kernel.d.ts +14 -4
- package/dist/kernel.js +153 -24
- package/dist/local-chrome.d.ts +7 -1
- package/dist/local-chrome.js +104 -8
- package/dist/modal.js +3 -1
- package/dist/orgo.d.ts +6 -0
- package/dist/orgo.js +156 -11
- package/package.json +1 -1
package/dist/local-chrome.js
CHANGED
|
@@ -18,16 +18,17 @@
|
|
|
18
18
|
// alone. It is for local and development use.
|
|
19
19
|
//
|
|
20
20
|
// Files: one directory per browser under the root (default <tmpdir>/agentrun-local-browser-<uid>, 0700), named by the
|
|
21
|
-
// browser's id:
|
|
21
|
+
// browser's id: claim.json (the pid and time of the create that made the directory), browser.json (its labels and
|
|
22
|
+
// deadline, which list and attach read), supervisor.pid, state.json (Chrome's
|
|
22
23
|
// pid and port, once it answers), the profile, the proxy's socket and two logs. A release removes all of it.
|
|
23
24
|
import { spawn } from "node:child_process";
|
|
24
|
-
import { randomBytes } from "node:crypto";
|
|
25
25
|
import { existsSync, readFileSync } from "node:fs";
|
|
26
26
|
import { lstat, mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
27
27
|
import { tmpdir } from "node:os";
|
|
28
28
|
import { join, resolve } from "node:path";
|
|
29
29
|
import { fileURLToPath } from "node:url";
|
|
30
30
|
import { browserClient, browserHello, browserSpecRefusals, carries, labelsProblem } from "./browser.js";
|
|
31
|
+
import { CreateRequests, endedWords, labelsDiffer, otherSpecWords } from "./create-requests.js";
|
|
31
32
|
import { alive, answeringPath, CHROME_FLAGS, checkChrome, findChrome, killTree, NO_CHROME, AS_ROOT, runsWith, singletonDir } from "./chrome.js";
|
|
32
33
|
import { LOCAL } from "./descriptors/local-chrome.js";
|
|
33
34
|
import { chromeEnv } from "./child-env.js";
|
|
@@ -44,6 +45,8 @@ export const LOCAL_MAX_LIFETIME_MS = 24 * 60 * 60_000;
|
|
|
44
45
|
const WINDOW = "--window-size=1280,800";
|
|
45
46
|
/** A Unix socket's path holds 107 bytes; a root whose browsers' sockets would not fit is refused. */
|
|
46
47
|
const SOCKET_PATH_MAX = 107;
|
|
48
|
+
/** The record a create writes in the directory it claimed: its process's pid and the time, before browser.json. */
|
|
49
|
+
const CLAIM_FILE = "claim.json";
|
|
47
50
|
/** How long a release waits for the supervisor, then for Chrome, to end. */
|
|
48
51
|
const END_MS = 10_000;
|
|
49
52
|
/** The networks a local browser runs on, its descriptor's: through its egress proxy, public addresses ("open"), or only
|
|
@@ -74,7 +77,8 @@ const networkRecord = (network) => {
|
|
|
74
77
|
};
|
|
75
78
|
/** A browser's network as list, attach and network() read it back from browser.json. */
|
|
76
79
|
const networkRead = (record) => (record.allow ? { allow: [...record.allow] } : "open");
|
|
77
|
-
|
|
80
|
+
/** A browser's id, its directory's name: its request's name (computerNameFor), or lb-<16 hex> for one made before ids were. */
|
|
81
|
+
const ID = /^(?:computer-[0-9a-f]{12}|lb-[0-9a-f]{16})$/;
|
|
78
82
|
const idProblem = (id) => (typeof id === "string" && ID.test(id) ? undefined : `${typeof id === "string" ? JSON.stringify(id) : String(id)} is not a local browser's id`);
|
|
79
83
|
const refOf = (id) => ({ id, name: id });
|
|
80
84
|
const SPEC_FIELDS = new Set(["image", "lifetimeMs", "network", "labels", "browser"]);
|
|
@@ -95,6 +99,13 @@ export class LocalBrowserProvider {
|
|
|
95
99
|
#env;
|
|
96
100
|
#headed;
|
|
97
101
|
#uid;
|
|
102
|
+
/** Idempotent create's in-process half: one browser per request id (provider.md). */
|
|
103
|
+
#requests = new CreateRequests();
|
|
104
|
+
/**
|
|
105
|
+
* The ids of the browsers this provider's creates answered, kept past their end: a request whose browser ended on its
|
|
106
|
+
* own (its deadline, its supervisor) is refused exists, as one released here is, never made again.
|
|
107
|
+
*/
|
|
108
|
+
#made = new Set();
|
|
98
109
|
root;
|
|
99
110
|
constructor({ executablePath, env = {}, headed = false, root, uid = process.getuid?.() } = {}) {
|
|
100
111
|
this.#executablePath = executablePath === "" ? undefined : executablePath;
|
|
@@ -184,21 +195,104 @@ export class LocalBrowserProvider {
|
|
|
184
195
|
return fail("invalid", `the local browser's root ${this.root} could not be made (${error.code ?? error.message})`);
|
|
185
196
|
}
|
|
186
197
|
}
|
|
187
|
-
/**
|
|
198
|
+
/**
|
|
199
|
+
* Starts Chrome under a supervisor of its own, waits until its DevTools socket answers, and hands back its ref: once
|
|
200
|
+
* per request (provider.md, Idempotent create). The request's name (computerNameFor) is the browser's id and its
|
|
201
|
+
* directory's name, which mkdir claims for one create alone. A request whose directory is already under the root
|
|
202
|
+
* answers that browser, from this process or another (#taken); one whose browser was released here is refused
|
|
203
|
+
* exists; a create of a request in flight here waits for the first (CreateRequests).
|
|
204
|
+
*/
|
|
188
205
|
async create(spec, context) {
|
|
189
|
-
const signal = context?.signal;
|
|
190
206
|
const refusals = this.check(spec);
|
|
191
|
-
|
|
192
|
-
|
|
207
|
+
return this.#requests.create(spec, context, {
|
|
208
|
+
refuse: (code, message) => fail(code === "refused" && refusals.some((r) => r.field === "image" && r.message.endsWith(NO_CHROME)) ? "not_found" : code, message),
|
|
209
|
+
...(refusals.length > 0 ? { refused: refusals.map(({ field, message }) => `${field}: ${message}`).join("; ") } : {}),
|
|
210
|
+
make: async (id) => {
|
|
211
|
+
const made = await this.#createNamed(spec, id, context.signal);
|
|
212
|
+
if (made.ok)
|
|
213
|
+
this.#made.add(id);
|
|
214
|
+
return made;
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The browser of a request whose directory another create claimed: answered once its Chrome answers on its DevTools
|
|
220
|
+
* socket, when it is this spec's (its labels and network, as browser.json holds them), and refused exists when it is
|
|
221
|
+
* another's, when its deadline has passed, or when its supervisor has ended with no Chrome left. Waited for within the
|
|
222
|
+
* time a create of its own takes.
|
|
223
|
+
*/
|
|
224
|
+
async #taken(spec, id, signal) {
|
|
225
|
+
const dir = this.#dir(id);
|
|
226
|
+
const wait = READY_MS + 10_000;
|
|
227
|
+
const until = Date.now() + wait;
|
|
228
|
+
for (;;) {
|
|
229
|
+
const made = await lstat(dir).then((stat) => stat, () => undefined);
|
|
230
|
+
if (made === undefined)
|
|
231
|
+
return fail("exists", endedWords(id));
|
|
232
|
+
const record = await this.#record(id);
|
|
233
|
+
if (record === undefined) {
|
|
234
|
+
// No browser.json yet: the create that claimed the directory is still making it, or it crashed. Its claim says
|
|
235
|
+
// who and when (the directory's own time where it wrote none); one whose process is gone, or older than a
|
|
236
|
+
// create takes, never finishes.
|
|
237
|
+
const claim = await readJson(join(dir, CLAIM_FILE));
|
|
238
|
+
const pid = claim?.pid;
|
|
239
|
+
const at = typeof claim?.at === "string" ? Date.parse(claim.at) : Number.NaN;
|
|
240
|
+
const since = Number.isNaN(at) ? made.mtimeMs : at;
|
|
241
|
+
if ((typeof pid === "number" && Number.isInteger(pid) && pid > 0 && !alive(pid)) || Date.now() - since > wait) {
|
|
242
|
+
return fail("exists", `${id}: an earlier create of this request did not finish: create with a new request id`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
const allow = (network) => JSON.stringify(network.allow ? [...network.allow].sort() : null);
|
|
247
|
+
const differ = labelsDiffer(spec.labels ?? {}, record.labels, "read back");
|
|
248
|
+
const asked = networkRecord(spec.network);
|
|
249
|
+
if (allow(asked) !== allow(record))
|
|
250
|
+
differ.push(`network: asked ${JSON.stringify(spec.network)}, read back ${JSON.stringify(networkRead(record))}`);
|
|
251
|
+
if (differ.length > 0)
|
|
252
|
+
return fail("exists", otherSpecWords(id, "was made", differ));
|
|
253
|
+
if (Date.parse(record.deadline) <= Date.now())
|
|
254
|
+
return fail("exists", endedWords(id));
|
|
255
|
+
const state = await this.#live(id);
|
|
256
|
+
if (state !== undefined && (await answeringPath(state.port)) === state.path)
|
|
257
|
+
return ok(refOf(id));
|
|
258
|
+
const supervisor = Number((await readFile(join(dir, SUPERVISOR_FILE), "utf8").catch(() => "")).trim());
|
|
259
|
+
if (state === undefined && Number.isInteger(supervisor) && supervisor > 0 && !runsWith(supervisor, dir))
|
|
260
|
+
return fail("exists", `${id} was made for this request and no longer runs: create with a new request id`);
|
|
261
|
+
}
|
|
262
|
+
if (Date.now() + 100 >= until)
|
|
263
|
+
return fail("timeout", `${id}, made for this request, did not answer within ${wait / 1000} s`);
|
|
264
|
+
await sleep(100);
|
|
265
|
+
if (signal?.aborted)
|
|
266
|
+
return fail("aborted", `create was aborted while ${id}, made for this request, came up`);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/** Everything create does under the request's name: the browser its directory already holds, or a new one. */
|
|
270
|
+
async #createNamed(spec, id, signal) {
|
|
193
271
|
if (signal?.aborted)
|
|
194
272
|
return fail("aborted", "create was aborted before Chrome was started");
|
|
195
273
|
const rooted = await this.#ensureRoot();
|
|
196
274
|
if (!rooted.ok)
|
|
197
275
|
return rooted;
|
|
198
|
-
const id = `lb-${randomBytes(8).toString("hex")}`;
|
|
199
276
|
const dir = join(this.root, id);
|
|
200
277
|
if (Buffer.byteLength(join(dir, EGRESS_SOCKET_FILE)) > SOCKET_PATH_MAX)
|
|
201
278
|
return fail("invalid", `the local browser's root ${this.root} is too long for a Unix socket under it: set the root option to a shorter directory`);
|
|
279
|
+
// The directory is the request's claim: made by one create alone, and one already there is the request's browser.
|
|
280
|
+
try {
|
|
281
|
+
await mkdir(dir, { mode: 0o700 });
|
|
282
|
+
}
|
|
283
|
+
catch (error) {
|
|
284
|
+
if (error.code === "EEXIST")
|
|
285
|
+
return this.#taken(spec, id, signal);
|
|
286
|
+
return fail("invalid", `the local browser's directory could not be made under ${this.root} (${error.code ?? error.message})`);
|
|
287
|
+
}
|
|
288
|
+
// The claim's record, so a create that finds the directory and no browser.json knows whether its maker still runs.
|
|
289
|
+
await writeFile(join(dir, CLAIM_FILE), JSON.stringify({ pid: process.pid, at: new Date().toISOString() }), { mode: 0o600, flag: "wx" }).catch(() => undefined);
|
|
290
|
+
// A request whose browser this provider answered before, and whose directory is gone: it ended on its own.
|
|
291
|
+
if (this.#made.has(id)) {
|
|
292
|
+
await rm(dir, { recursive: true, force: true }).catch(() => undefined);
|
|
293
|
+
this.#requests.end(id);
|
|
294
|
+
return fail("exists", endedWords(id));
|
|
295
|
+
}
|
|
202
296
|
const now = Date.now();
|
|
203
297
|
const record = {
|
|
204
298
|
provider: LOCAL.id,
|
|
@@ -427,6 +521,8 @@ export class LocalBrowserProvider {
|
|
|
427
521
|
const problem = idProblem(id);
|
|
428
522
|
if (problem)
|
|
429
523
|
return fail("invalid", problem);
|
|
524
|
+
// The request's browser is ended here: a later create of it is refused exists, never made again.
|
|
525
|
+
this.#requests.end(id);
|
|
430
526
|
const dir = this.#dir(id);
|
|
431
527
|
if (!(await lstat(dir).then(() => true, () => false)))
|
|
432
528
|
return ok(undefined);
|
package/dist/modal.js
CHANGED
|
@@ -143,7 +143,9 @@ class ModalHost {
|
|
|
143
143
|
windowManager: true,
|
|
144
144
|
// Never root: each connection is Modal's exec of the stdio bridge, as Modal's root, outside the daemon.
|
|
145
145
|
user: ["uid"],
|
|
146
|
-
|
|
146
|
+
// Packed: the server's labels (a mount, a network list, an owner, a run and its deadlines) pass 512 bytes as they
|
|
147
|
+
// are. A value holds 64 KiB, so the map is one tag. Sandboxes made before packing carry their tags as given.
|
|
148
|
+
labels: { count: MAX_TAGS, bytes: MAX_TAG_BYTES, packed: { unpacked: true } },
|
|
147
149
|
};
|
|
148
150
|
if (connect === "exec")
|
|
149
151
|
this.exec = (id, argv, ctx) => this.#exec(id, argv, ctx);
|
package/dist/orgo.d.ts
CHANGED
|
@@ -94,6 +94,12 @@ export declare class OrgoProvider implements ComputerProvider {
|
|
|
94
94
|
*/
|
|
95
95
|
capabilities(): ComputerCapabilities;
|
|
96
96
|
check(spec: ComputerSpec): Refusal[];
|
|
97
|
+
/**
|
|
98
|
+
* Makes a computer, once per request (provider.md, Idempotent create): context.requestId's name (computerNameFor)
|
|
99
|
+
* begins the computer's Orgo name, the run's hash, epoch and deadline after it. A request whose computer Orgo still
|
|
100
|
+
* holds answers that one (#held); one whose computer was deleted here is refused exists; a create of a request in
|
|
101
|
+
* flight here waits for the first (CreateRequests).
|
|
102
|
+
*/
|
|
97
103
|
create(spec: ComputerSpec, context: CreateContext): Promise<Result<CreatedComputer>>;
|
|
98
104
|
/** The live view: screen.watch on a connection of the watch's own (daemonWatch), never Orgo's VNC (proxy only). */
|
|
99
105
|
watch(ref: ComputerRef, handlers: ScreenWatchHandlers, options?: WatchOptions): Promise<Result<ScreenWatch>>;
|
package/dist/orgo.js
CHANGED
|
@@ -22,6 +22,7 @@ import { ORGO, ORGO_BASE_IMAGE } from "./descriptors/orgo.js";
|
|
|
22
22
|
import { DESKTOP_GROUPS } from "./desktop.js";
|
|
23
23
|
import { DAEMON_IN_COMPUTER, helloDrift, probeNetworkDrift, probeNotes, probeReportOf, SOCKET_DIR, WORK } from "./drift.js";
|
|
24
24
|
import { EPOCH_LABEL, OWNER_LABEL, OWNER_SERVER_LABEL, ownerLabels, RUN_HASH_LABEL, RUN_LABEL, runHash } from "./labels.js";
|
|
25
|
+
import { CreateRequests, endedWords, labelsDiffer, otherSpecWords, readBackDiffer } from "./create-requests.js";
|
|
25
26
|
import { checkMount } from "./mount.js";
|
|
26
27
|
import { ORGO_API_URL, orgoRest, orgoTerminalChannel, START_LOG } from "./orgo-channel.js";
|
|
27
28
|
import { OrgoDesktopProvider } from "./orgo-desktop.js";
|
|
@@ -67,9 +68,13 @@ const DEFAULT_DAEMON_PATH = fileURLToPath(new URL("./computerd-linux-amd64", imp
|
|
|
67
68
|
export const LABELS_FILE = "/opt/agentrun/labels.json";
|
|
68
69
|
const STAGING = "/tmp/agentrun-computerd-";
|
|
69
70
|
const LISTEN_RETRY_MS = 250;
|
|
71
|
+
/** The order a request's computers are read in (#held, #converge): the oldest first, by created_at, then id. */
|
|
72
|
+
const oldestFirst = (a, b) => String(a.created_at ?? "").localeCompare(String(b.created_at ?? "")) || a.id.localeCompare(b.id);
|
|
70
73
|
/** How often a delete that answers 409 (the computer waking) is tried again. */
|
|
71
74
|
const DELETE_TRIES = 5;
|
|
72
75
|
const SPEC_FIELDS = new Set(["image", "lifetimeMs", "network", "cpus", "memoryMb", "labels", "gpu"]);
|
|
76
|
+
/** The request's name (computerNameFor) a computer's Orgo name begins with. */
|
|
77
|
+
const requestOf = (name) => `computer-${nameParts(name)?.hex ?? ""}`;
|
|
73
78
|
/** The states in which a computer runs, or is on its way to. */
|
|
74
79
|
const RUNNING = new Set(["running", "creating", "restarting", "updating", "resuming"]);
|
|
75
80
|
/** Why a network other than "none" is refused, in the words every hosted provider uses (hosted/network.ts), and why:
|
|
@@ -192,6 +197,8 @@ export class OrgoProvider {
|
|
|
192
197
|
#extending = new Map();
|
|
193
198
|
/** Each agent's computer's attach or detach in flight, by id: the next waits for it, so two never overlap here. */
|
|
194
199
|
#owning = new Map();
|
|
200
|
+
/** Idempotent create's in-process half: one computer per request id (provider.md). */
|
|
201
|
+
#requests = new CreateRequests();
|
|
195
202
|
constructor({ apiKey, workspaceId, apiUrl = ORGO_API_URL, fetch, webSocket, channel, daemonPath = DEFAULT_DAEMON_PATH, maxLifetimeMs = MAX_LIFETIME_MS, user = "2510:2510", start = "bash", display = ":99", timeouts = {}, probeTargets = DEFAULT_PROBE_TARGETS, ledger = orgoMemoryLedger() }) {
|
|
196
203
|
if (typeof apiKey !== "string" || apiKey === "")
|
|
197
204
|
throw new Error("options.apiKey, an Orgo API key, is required");
|
|
@@ -292,11 +299,22 @@ export class OrgoProvider {
|
|
|
292
299
|
}
|
|
293
300
|
return refusals;
|
|
294
301
|
}
|
|
302
|
+
/**
|
|
303
|
+
* Makes a computer, once per request (provider.md, Idempotent create): context.requestId's name (computerNameFor)
|
|
304
|
+
* begins the computer's Orgo name, the run's hash, epoch and deadline after it. A request whose computer Orgo still
|
|
305
|
+
* holds answers that one (#held); one whose computer was deleted here is refused exists; a create of a request in
|
|
306
|
+
* flight here waits for the first (CreateRequests).
|
|
307
|
+
*/
|
|
295
308
|
async create(spec, context) {
|
|
296
|
-
const signal = context?.signal;
|
|
297
309
|
const refusals = this.check(spec);
|
|
298
|
-
|
|
299
|
-
|
|
310
|
+
return this.#requests.create(spec, context, {
|
|
311
|
+
refuse: fail,
|
|
312
|
+
...(refusals.length > 0 ? { refused: refusals.map(({ field, message }) => `${field}: ${message}`).join("; ") } : {}),
|
|
313
|
+
make: (request) => this.#createNamed(spec, request, context.signal),
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
/** Everything create does under the request's name: the computer Orgo holds under it, or a new one. */
|
|
317
|
+
async #createNamed(spec, request, signal) {
|
|
300
318
|
let daemon;
|
|
301
319
|
try {
|
|
302
320
|
daemon = readFileSync(this.#daemonPath);
|
|
@@ -318,10 +336,13 @@ export class OrgoProvider {
|
|
|
318
336
|
const maxDeadline = new Date(now + this.#maxLifetimeMs).toISOString();
|
|
319
337
|
const deadline = owner === undefined ? new Date(now + runLifetimeMs(spec)).toISOString() : maxDeadline;
|
|
320
338
|
const init = owner === undefined ? { deadline, maxDeadline } : this.#ownedInit(now);
|
|
321
|
-
// The
|
|
322
|
-
const name = orgoName(labels,
|
|
339
|
+
// The request's name first, then the janitor's deadline, so list() and the sweep read it with nothing run inside.
|
|
340
|
+
const name = orgoName(labels, request.slice("computer-".length), owner === undefined ? new Date(deadline) : undefined, owner);
|
|
323
341
|
if (name.length > NAME_MAX || nameParts(name) === undefined)
|
|
324
342
|
return fail("invalid", `the name ${name} is not one Orgo holds: at most ${NAME_MAX} of letters, digits and dashes`);
|
|
343
|
+
const held = await this.#held(spec, request, labels, signal);
|
|
344
|
+
if (!held.ok || held.value !== undefined)
|
|
345
|
+
return held;
|
|
325
346
|
const ram = spec.memoryMb === undefined ? 4 : spec.memoryMb / 1024;
|
|
326
347
|
const cpu = spec.cpus ?? CPU_FOR_RAM[ram];
|
|
327
348
|
const body = {
|
|
@@ -336,15 +357,29 @@ export class OrgoProvider {
|
|
|
336
357
|
};
|
|
337
358
|
const made = await this.#rest.request("POST", "/computers", { body, timeoutMs: this.#timeouts.createMs, ...(signal ? { signal } : {}) });
|
|
338
359
|
if (!made.ok) {
|
|
360
|
+
// A name Orgo holds already (409) is another process's create of this request in the same second: that computer
|
|
361
|
+
// is answered as a retry's would be.
|
|
362
|
+
if (made.status === 409) {
|
|
363
|
+
const theirs = await this.#held(spec, request, labels, signal);
|
|
364
|
+
return !theirs.ok || theirs.value !== undefined ? theirs : made;
|
|
365
|
+
}
|
|
339
366
|
// A create Orgo refused (a 4xx) made nothing. One whose answer never came may have: it goes by its name.
|
|
340
367
|
if (["invalid", "denied", "limit", "not_found", "refused"].includes(made.error.code))
|
|
341
368
|
return made;
|
|
369
|
+
this.#requests.end(request);
|
|
342
370
|
return fail(made.error.code, `${made.error.message}${await this.#deleteNamed(name)}`);
|
|
343
371
|
}
|
|
344
372
|
const id = made.value?.id;
|
|
345
|
-
if (typeof id !== "string" || !COMPUTER_ID.test(id))
|
|
373
|
+
if (typeof id !== "string" || !COMPUTER_ID.test(id)) {
|
|
374
|
+
this.#requests.end(request);
|
|
346
375
|
return vendorFail("orgo", `Orgo's create answered no computer id${await this.#deleteNamed(name)}`);
|
|
376
|
+
}
|
|
377
|
+
this.#requests.made(request);
|
|
347
378
|
const ref = { id, name };
|
|
379
|
+
// Another process's create of this request may have made one too (#converge): the older is kept.
|
|
380
|
+
const first = await this.#converge(spec, request, labels, made.value, signal);
|
|
381
|
+
if (first !== undefined)
|
|
382
|
+
return first;
|
|
348
383
|
// An agent's computer's labels go in the ledger at once, so list finds it as the agent's whatever happens next.
|
|
349
384
|
if (owner !== undefined) {
|
|
350
385
|
const kept = await this.#keep({ id, labels });
|
|
@@ -355,9 +390,109 @@ export class OrgoProvider {
|
|
|
355
390
|
const prepared = await this.#prepare(ref, made.value.status, file, daemon, signal, init);
|
|
356
391
|
if (signal?.aborted)
|
|
357
392
|
return this.#abandon(ref, { code: "aborted", message: "create was aborted" });
|
|
358
|
-
if (prepared.ok)
|
|
359
|
-
return
|
|
360
|
-
|
|
393
|
+
if (!prepared.ok)
|
|
394
|
+
return this.#abandon(ref, prepared.error);
|
|
395
|
+
// The last read of the workspace, once this computer is up: an older one listed only now is answered here.
|
|
396
|
+
const last = await this.#converge(spec, request, labels, made.value, signal);
|
|
397
|
+
if (last !== undefined)
|
|
398
|
+
return last;
|
|
399
|
+
return ok(prepared.value.length > 0 ? { ...ref, notes: prepared.value } : ref);
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* The one computer a request keeps. Two creates of one request in two processes can both find no computer for it
|
|
403
|
+
* (#held) and both make one: CreateRequests holds within one provider alone, and the names differ once the
|
|
404
|
+
* deadline in them does. So after Orgo's answer the workspace is read again: where an older computer of this request
|
|
405
|
+
* is there (the oldest by created_at, then id, the order #held reads), this one is deleted and the older answered
|
|
406
|
+
* through #held, as a retry's create answers it. Every process picks the same one from the same listing, so the
|
|
407
|
+
* newer creates delete their own and the oldest keeps its. Undefined when this computer is the request's oldest (or
|
|
408
|
+
* the listing fails: the create goes on with its own); an abort deletes this computer (#abandon, never under the
|
|
409
|
+
* caller's signal), and the create is aborted.
|
|
410
|
+
*/
|
|
411
|
+
async #converge(spec, request, labels, mine, signal) {
|
|
412
|
+
const listed = await this.#desktops(signal);
|
|
413
|
+
if (!listed.ok) {
|
|
414
|
+
if (listed.error.code !== "aborted" && !signal?.aborted)
|
|
415
|
+
return undefined;
|
|
416
|
+
// The caller is gone: the computer this POST made is deleted, never under the caller's signal.
|
|
417
|
+
return this.#abandon({ id: mine.id, name: mine.name }, { code: "aborted", message: "create was aborted" });
|
|
418
|
+
}
|
|
419
|
+
const hex = request.slice("computer-".length);
|
|
420
|
+
const holders = listed.value.filter((one) => nameParts(one.name)?.hex === hex && this.#ours(one) && RUNNING.has(one.status));
|
|
421
|
+
// Not listed yet, it was made just now: no older than now.
|
|
422
|
+
if (!holders.some((one) => one.id === mine.id))
|
|
423
|
+
holders.push({ ...mine, created_at: mine.created_at ?? new Date().toISOString() });
|
|
424
|
+
const [kept] = holders.sort(oldestFirst);
|
|
425
|
+
if (kept === undefined || kept.id === mine.id)
|
|
426
|
+
return undefined;
|
|
427
|
+
const ref = { id: mine.id, name: mine.name };
|
|
428
|
+
const deleted = await this.#delete(ref);
|
|
429
|
+
if (!deleted.ok)
|
|
430
|
+
return fail(deleted.error.code, `${kept.name}, the older computer of this request, is kept, and ${ref.name} could not be deleted (${deleted.error.message}); Orgo ends it at its end`);
|
|
431
|
+
const theirs = await this.#held(spec, request, labels, signal);
|
|
432
|
+
if (!theirs.ok || theirs.value !== undefined)
|
|
433
|
+
return theirs;
|
|
434
|
+
this.#requests.end(request);
|
|
435
|
+
return fail("exists", `${ref.name} was deleted for ${kept.name}, the older computer of this request, which is gone now: create with a new request id`);
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* The computer Orgo holds for this request, when it holds one: undefined when it holds none, so create makes it. It
|
|
439
|
+
* is found by the request's name, the start of every name create gives (the rest moves with the deadline), among the
|
|
440
|
+
* workspace's computers, the oldest first. Up and sound (its labels file and its daemon, as attach reads them), it is
|
|
441
|
+
* returned when it is what this spec asks for, and refused exists when it is another spec's. One still being made
|
|
442
|
+
* (by another process's create) is waited for, within the time a create of its own takes; one that no longer runs,
|
|
443
|
+
* or whose deadline has passed, is refused exists.
|
|
444
|
+
*/
|
|
445
|
+
async #held(spec, request, labels, signal) {
|
|
446
|
+
const hex = request.slice("computer-".length);
|
|
447
|
+
const { createMs, uploadMs, helloMs, probeMs, pollMs } = this.#timeouts;
|
|
448
|
+
const wait = createMs + uploadMs + helloMs + probeMs;
|
|
449
|
+
const until = Date.now() + wait;
|
|
450
|
+
let last;
|
|
451
|
+
for (;;) {
|
|
452
|
+
const listed = await this.#desktops(signal);
|
|
453
|
+
if (!listed.ok)
|
|
454
|
+
return listed;
|
|
455
|
+
const found = listed.value.filter((one) => nameParts(one.name)?.hex === hex && this.#ours(one)).sort(oldestFirst);
|
|
456
|
+
const record = found[0];
|
|
457
|
+
if (record === undefined)
|
|
458
|
+
return ok(undefined);
|
|
459
|
+
const ref = { id: record.id, name: record.name };
|
|
460
|
+
const deadline = nameParts(record.name)?.deadline;
|
|
461
|
+
if (deadline !== undefined && deadline.getTime() <= Date.now())
|
|
462
|
+
return fail("exists", endedWords(record.name));
|
|
463
|
+
if (!RUNNING.has(record.status))
|
|
464
|
+
return fail("exists", `${record.name} was made for this request and is ${record.status}, not running: create with a new request id`);
|
|
465
|
+
if (record.status === "running") {
|
|
466
|
+
const file = await this.#readLabels(ref, signal);
|
|
467
|
+
if (file.ok) {
|
|
468
|
+
const differ = [
|
|
469
|
+
...labelsDiffer(labels, file.value.labels, "read back"),
|
|
470
|
+
...readBackDiffer(spec, { image: file.value.image, user: `${this.#uid}:${this.#gid}`, network: "none", ...(file.value.cpus === undefined ? {} : { cpus: file.value.cpus }), ...(file.value.memoryMb === undefined ? {} : { memoryMb: file.value.memoryMb }) }, ["image", "cpus", "memoryMb"]),
|
|
471
|
+
];
|
|
472
|
+
if (differ.length > 0)
|
|
473
|
+
return fail("exists", otherSpecWords(record.name, "was made", differ));
|
|
474
|
+
const inside = await this.#inside(ref, signal);
|
|
475
|
+
if (inside.ok)
|
|
476
|
+
return ok(inside.value.length > 0 ? { ...ref, notes: inside.value } : ref);
|
|
477
|
+
if (inside.error.code === "aborted")
|
|
478
|
+
return inside;
|
|
479
|
+
// Its daemon is not up yet: the create that made it is still preparing it.
|
|
480
|
+
last = inside.error.message;
|
|
481
|
+
}
|
|
482
|
+
else {
|
|
483
|
+
if (file.error.code === "aborted")
|
|
484
|
+
return file;
|
|
485
|
+
last = file.error.message;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
else
|
|
489
|
+
last = `Orgo reads it ${record.status}`;
|
|
490
|
+
if (Date.now() + pollMs >= until)
|
|
491
|
+
return fail("timeout", `${record.name}, made for this request, was not up within ${seconds(wait)}: ${last}`);
|
|
492
|
+
await pause(pollMs, signal);
|
|
493
|
+
if (signal?.aborted)
|
|
494
|
+
return fail("aborted", `create was aborted while ${record.name}, made for this request, came up`);
|
|
495
|
+
}
|
|
361
496
|
}
|
|
362
497
|
/**
|
|
363
498
|
* Everything between Orgo's answer and a computer that passed the drift check: running, computerd uploaded and
|
|
@@ -609,6 +744,7 @@ export class OrgoProvider {
|
|
|
609
744
|
}
|
|
610
745
|
/** Deletes a computer create gives up on, and waits until Orgo no longer has it. Never signalled: it outlives an abort. */
|
|
611
746
|
async #abandon(ref, error) {
|
|
747
|
+
this.#requests.end(requestOf(ref.name));
|
|
612
748
|
const deleted = await this.#delete(ref);
|
|
613
749
|
return fail(error.code, `the computer ${ref.name} ${deleted.ok ? "was deleted" : `could not be deleted (${deleted.error.message}); Orgo ends it at its end`}: ${error.message}`);
|
|
614
750
|
}
|
|
@@ -1128,8 +1264,17 @@ export class OrgoProvider {
|
|
|
1128
1264
|
return how === "delete" ? ok(undefined) : fail("not_found", `${ref.name} is deleted: there is nothing to detach`);
|
|
1129
1265
|
if (!this.#ours(read.value, ref.name))
|
|
1130
1266
|
return fail("not_found", `no Orgo computer of this provider is ${ref.name} (${ref.id}), so it is left as it is`);
|
|
1131
|
-
if (how === "delete")
|
|
1132
|
-
|
|
1267
|
+
if (how === "delete") {
|
|
1268
|
+
// The request's computer is ended here: a later create of it is refused exists, never made again.
|
|
1269
|
+
if (claim === undefined) {
|
|
1270
|
+
this.#requests.end(requestOf(read.value.name));
|
|
1271
|
+
return this.#delete(ref, signal);
|
|
1272
|
+
}
|
|
1273
|
+
const deleted = await this.#claimedDelete(ref, read.value.name, claim, signal);
|
|
1274
|
+
if (deleted.ok)
|
|
1275
|
+
this.#requests.end(requestOf(read.value.name));
|
|
1276
|
+
return deleted;
|
|
1277
|
+
}
|
|
1133
1278
|
const owner = nameParts(read.value.name)?.owner;
|
|
1134
1279
|
if (owner === undefined)
|
|
1135
1280
|
return fail("refused", `${ref.name} is a run's computer, not an agent's own: it is deleted at its release, never detached`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcha/agentrun-computer",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.61",
|
|
4
4
|
"description": "The open platform's computer protocol: a daemon in every computer, and one provisioner per vendor, starting with Docker.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agentrun",
|