@parcha/agentrun-computer 0.1.0-beta.35 → 0.1.0-beta.36
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 +108 -21
- package/box/Dockerfile +3 -1
- package/box/agentrun-archil +59 -7
- package/box/build-daytona.py +20 -4
- package/dist/browser.d.ts +61 -10
- package/dist/browser.js +125 -3
- package/dist/browserbase.d.ts +45 -20
- package/dist/browserbase.js +161 -17
- package/dist/chrome.d.ts +3 -4
- package/dist/chrome.js +6 -11
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/conformance/daytona.d.ts +4 -0
- package/dist/conformance/daytona.js +5 -0
- package/dist/daytona.d.ts +13 -1
- package/dist/daytona.js +55 -36
- package/dist/descriptor.d.ts +24 -2
- package/dist/descriptor.js +11 -0
- package/dist/descriptors/browserbase.d.ts +1 -0
- package/dist/descriptors/browserbase.js +2 -0
- package/dist/descriptors/daytona.d.ts +23 -2
- package/dist/descriptors/daytona.js +34 -2
- package/dist/descriptors/index.d.ts +28 -1
- package/dist/descriptors/index.js +1 -1
- package/dist/descriptors/kernel.d.ts +1 -0
- package/dist/descriptors/kernel.js +2 -0
- package/dist/descriptors/modal.d.ts +4 -0
- package/dist/descriptors/modal.js +11 -1
- package/dist/descriptors/orgo.d.ts +11 -0
- package/dist/descriptors/orgo.js +6 -1
- package/dist/descriptors/phone-harness.d.ts +5 -0
- package/dist/descriptors/phone-harness.js +3 -0
- package/dist/docker/egress.d.ts +4 -3
- package/dist/docker/egress.js +7 -6
- package/dist/docker.js +7 -5
- package/dist/e2b.js +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +4 -4
- package/dist/janitor.d.ts +5 -4
- package/dist/janitor.js +1 -1
- package/dist/kernel.d.ts +16 -24
- package/dist/kernel.js +93 -18
- package/dist/kinds.d.ts +9 -4
- package/dist/labels.d.ts +10 -2
- package/dist/labels.js +30 -7
- package/dist/local-chrome.d.ts +3 -1
- package/dist/local-chrome.js +8 -4
- package/dist/modal.d.ts +3 -3
- package/dist/modal.js +71 -57
- package/dist/mount-archil.d.ts +25 -3
- package/dist/mount-archil.js +39 -8
- package/dist/orgo-desktop.d.ts +15 -6
- package/dist/orgo-desktop.js +295 -53
- package/dist/orgo-names.d.ts +129 -3
- package/dist/orgo-names.js +211 -8
- package/dist/orgo.d.ts +18 -7
- package/dist/orgo.js +381 -69
- package/dist/phone-harness.d.ts +3 -2
- package/dist/phone-harness.js +25 -8
- package/dist/protocol.d.ts +5 -0
- package/dist/provider.d.ts +63 -1
- package/dist/provider.js +8 -3
- package/package.json +1 -1
package/dist/orgo.js
CHANGED
|
@@ -19,11 +19,11 @@ import { DaemonClient } from "./client.js";
|
|
|
19
19
|
import { ORGO, ORGO_BASE_IMAGE } from "./descriptors/orgo.js";
|
|
20
20
|
import { DESKTOP_GROUPS } from "./desktop.js";
|
|
21
21
|
import { DAEMON_IN_COMPUTER, helloDrift, probeNetworkDrift, probeNotes, probeReportOf, SOCKET_DIR, WORK } from "./drift.js";
|
|
22
|
-
import { EPOCH_LABEL, RUN_HASH_LABEL, RUN_LABEL, runHash } from "./labels.js";
|
|
22
|
+
import { EPOCH_LABEL, OWNER_LABEL, OWNER_SERVER_LABEL, ownerLabels, RUN_HASH_LABEL, RUN_LABEL, runHash } from "./labels.js";
|
|
23
23
|
import { checkMount } from "./mount.js";
|
|
24
24
|
import { ORGO_API_URL, orgoRest, orgoTerminalChannel, START_LOG } from "./orgo-channel.js";
|
|
25
25
|
import { OrgoDesktopProvider } from "./orgo-desktop.js";
|
|
26
|
-
import { COMPUTER_ID, filterProblem, labelProblem, labelsOf, NAME_MAX, nameParts, orgoName, refProblem, sameComputer, show, withDeadline, } from "./orgo-names.js";
|
|
26
|
+
import { COMPUTER_ID, filterProblem, labelProblem, labelsOf, NAME_MAX, NO_DEADLINE, nameParts, orgoMemoryLedger, orgoName, claimProblem, andSettled, inTurn, ownedListing, ownerHashOfLabels, ownerReadBack, refProblem, sameComputer, settleEntry, show, renewEntry, takeEntry, withDeadline, } from "./orgo-names.js";
|
|
27
27
|
import { checkGpu, checkTenure, runLifetimeMs, } from "./provider.js";
|
|
28
28
|
import { failFor, ok, vendorFail } from "./result.js";
|
|
29
29
|
import { daemonWatch } from "./screen-watch.js";
|
|
@@ -45,6 +45,12 @@ const VERSION = JSON.parse(readFileSync(new URL("../package.json", import.meta.u
|
|
|
45
45
|
const HOUR = 3_600_000;
|
|
46
46
|
/** Orgo's longest end: ends_in_hours is at most 720. */
|
|
47
47
|
const MAX_LIFETIME_MS = 720 * HOUR;
|
|
48
|
+
/**
|
|
49
|
+
* The most init's deadline of an agent's own computer may move to: none in practice. Its deadline is maxLifetimeMs out
|
|
50
|
+
* from create, and each attach moves it maxLifetimeMs out again (deadline.extend), so only a desktop left detached for
|
|
51
|
+
* longer than that loses its daemon, which the next attach then installs again (#replaceDaemon).
|
|
52
|
+
*/
|
|
53
|
+
const OWNED_MAX_DEADLINE = "9999-12-31T23:59:59.000Z";
|
|
48
54
|
/** Orgo's RAM sizes, in GB, and the vCPUs each takes by default. */
|
|
49
55
|
const RAM_GB = [4, 8, 16, 32];
|
|
50
56
|
const CPU_FOR_RAM = { 4: 1, 8: 2, 16: 4, 32: 6 };
|
|
@@ -84,6 +90,31 @@ const STATUS_SCRIPT = [
|
|
|
84
90
|
'if [ -S "$s" ]; then echo "socket present"; else echo "socket absent"; fi',
|
|
85
91
|
"echo \"log $(tail -c 300 \"$l\" 2>/dev/null | tr '\\n' ' ')\"",
|
|
86
92
|
].join("\n");
|
|
93
|
+
/**
|
|
94
|
+
* Stops computerd init by its exact processes (pgrep anchored to init's own command line, so neither this script nor
|
|
95
|
+
* pgrep matches itself): SIGTERM to each PID, on which init ends its programs, then up to 10 s for each to be gone (a
|
|
96
|
+
* zombie counts as gone), then SIGKILL to those left. Prints `stopped <pids>`, or `no init running`; exits 1 when one
|
|
97
|
+
* outlives the SIGKILL.
|
|
98
|
+
*/
|
|
99
|
+
const STOP_INIT_SCRIPT = [
|
|
100
|
+
"# agentrun-computer: orgo-stop-init",
|
|
101
|
+
"d=$1",
|
|
102
|
+
'pids=$(pgrep -f "^$d init") || { echo "no init running"; exit 0; }',
|
|
103
|
+
"alive() { [ -e \"/proc/$1\" ] && [ \"$(awk '{ print $3 }' \"/proc/$1/stat\" 2>/dev/null)\" != Z ]; }",
|
|
104
|
+
"kill -TERM $pids 2>/dev/null",
|
|
105
|
+
"i=0",
|
|
106
|
+
"while [ $i -lt 100 ]; do",
|
|
107
|
+
"\tleft=",
|
|
108
|
+
'\tfor p in $pids; do alive "$p" && left="$left $p"; done',
|
|
109
|
+
'\t[ -n "$left" ] || { echo "stopped" $pids; exit 0; }',
|
|
110
|
+
"\tsleep 0.1",
|
|
111
|
+
"\ti=$((i + 1))",
|
|
112
|
+
"done",
|
|
113
|
+
"kill -KILL $left 2>/dev/null",
|
|
114
|
+
"sleep 1",
|
|
115
|
+
'for p in $left; do alive "$p" && { echo "init $p outlived SIGKILL"; exit 1; }; done',
|
|
116
|
+
'echo "stopped" $pids',
|
|
117
|
+
].join("\n");
|
|
87
118
|
/**
|
|
88
119
|
* The TCP ports root listens on in Orgo's own network (its agent, its VNC, anything else of the image's), one decimal
|
|
89
120
|
* port a line, from /proc/net/tcp and tcp6 (state 0A, LISTEN): no tool of the image's is relied on but awk.
|
|
@@ -159,9 +190,12 @@ export class OrgoProvider {
|
|
|
159
190
|
#display;
|
|
160
191
|
#timeouts;
|
|
161
192
|
#probeTargets;
|
|
193
|
+
#ledger;
|
|
162
194
|
/** Each computer's extend in flight, by id: the next waits for it, so two never read the same old deadline. */
|
|
163
195
|
#extending = new Map();
|
|
164
|
-
|
|
196
|
+
/** Each agent's computer's attach or detach in flight, by id: the next waits for it, so two never overlap here. */
|
|
197
|
+
#owning = new Map();
|
|
198
|
+
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() }) {
|
|
165
199
|
if (typeof apiKey !== "string" || apiKey === "")
|
|
166
200
|
throw new Error("options.apiKey, an Orgo API key, is required");
|
|
167
201
|
if (typeof workspaceId !== "string" || !WORKSPACE_ID.test(workspaceId))
|
|
@@ -204,13 +238,15 @@ export class OrgoProvider {
|
|
|
204
238
|
this.#display = display;
|
|
205
239
|
this.#timeouts = merged;
|
|
206
240
|
this.#probeTargets = [...probeTargets];
|
|
241
|
+
this.#ledger = ledger;
|
|
207
242
|
}
|
|
208
243
|
/**
|
|
209
244
|
* daemon child: Orgo's own agent is the VM's first process, and init is a command it ran. lifetime janitor: Orgo ends
|
|
210
245
|
* a computer only in whole hours (ends_in_hours, the backstop), so the janitor deletes it at its exact deadline, which
|
|
211
246
|
* its name carries (list() reads it with nothing run inside); init ends its programs at that deadline inside. network
|
|
212
|
-
* none alone, read back by the probe from inside (Orgo reads no policy back). release delete
|
|
213
|
-
*
|
|
247
|
+
* none alone, read back by the probe from inside (Orgo reads no policy back). release delete, and detach for an
|
|
248
|
+
* agent's own computer: Orgo's suspend saves its memory and disk, so it resumes where it was (persistence resume),
|
|
249
|
+
* computerd init with it.
|
|
214
250
|
*/
|
|
215
251
|
capabilities() {
|
|
216
252
|
return {
|
|
@@ -228,13 +264,14 @@ export class OrgoProvider {
|
|
|
228
264
|
mount: [...MOUNT],
|
|
229
265
|
// Init starts with --own-network (childCommands): no tool reaches these, nor any port root listens on.
|
|
230
266
|
vendorAgentPorts: [...ORGO_AGENT_PORTS],
|
|
267
|
+
persistence: { keepsState: true, resume: true },
|
|
231
268
|
};
|
|
232
269
|
}
|
|
233
270
|
check(spec) {
|
|
234
271
|
const refusals = [];
|
|
235
272
|
const refuse = (field, message) => refusals.push({ field, message });
|
|
236
273
|
const given = (typeof spec === "object" && spec !== null ? spec : {});
|
|
237
|
-
const { image,
|
|
274
|
+
const { image, network, cpus, memoryMb, labels, mount } = given;
|
|
238
275
|
if (image !== BASE && !(typeof image === "string" && TEMPLATE.test(image)))
|
|
239
276
|
refuse("image", `an image is "${BASE}" (Orgo's base desktop) or an Orgo template, namespace/name@version`);
|
|
240
277
|
refusals.push(...checkTenure(given, this.capabilities(), "Orgo"));
|
|
@@ -253,7 +290,7 @@ export class OrgoProvider {
|
|
|
253
290
|
refusals.push(...checkMount(mount, MOUNT));
|
|
254
291
|
refusals.push(...checkGpu(given.gpu, this.capabilities()));
|
|
255
292
|
for (const [field, value] of Object.entries(given)) {
|
|
256
|
-
if (SPEC_FIELDS.has(field) || field === "mount" || value === undefined)
|
|
293
|
+
if (SPEC_FIELDS.has(field) || field === "mount" || field === "owner" || value === undefined)
|
|
257
294
|
continue;
|
|
258
295
|
if (field === "user")
|
|
259
296
|
refuse(field, `an Orgo computer's tools run as the provider's user (${this.#uid}:${this.#gid}, the remote tool user), never as one a spec names`);
|
|
@@ -279,12 +316,17 @@ export class OrgoProvider {
|
|
|
279
316
|
}
|
|
280
317
|
if (signal?.aborted)
|
|
281
318
|
return fail("aborted", "create was aborted before Orgo was called");
|
|
282
|
-
|
|
319
|
+
// An agent's own computer outlives the run: no janitor's deadline and no Orgo end, the owner in its name and labels.
|
|
320
|
+
// Init still takes one, maxLifetimeMs out, since it ends its programs at its deadline, with no most in practice:
|
|
321
|
+
// each attach moves it maxLifetimeMs out again (#ownedInit).
|
|
322
|
+
const owner = spec.owner;
|
|
323
|
+
const labels = { ...spec.labels, ...(owner === undefined ? {} : ownerLabels(owner)) };
|
|
283
324
|
const now = Date.now();
|
|
284
|
-
const deadline = new Date(now + runLifetimeMs(spec)).toISOString();
|
|
285
325
|
const maxDeadline = new Date(now + this.#maxLifetimeMs).toISOString();
|
|
326
|
+
const deadline = owner === undefined ? new Date(now + runLifetimeMs(spec)).toISOString() : maxDeadline;
|
|
327
|
+
const init = owner === undefined ? { deadline, maxDeadline } : this.#ownedInit(now);
|
|
286
328
|
// The janitor's deadline is in the name, so list() and the sweep read it with nothing run inside.
|
|
287
|
-
const name = orgoName(labels, randomBytes(6).toString("hex"), new Date(deadline));
|
|
329
|
+
const name = orgoName(labels, randomBytes(6).toString("hex"), owner === undefined ? new Date(deadline) : undefined, owner);
|
|
288
330
|
if (name.length > NAME_MAX || nameParts(name) === undefined)
|
|
289
331
|
return fail("invalid", `the name ${name} is not one Orgo holds: at most ${NAME_MAX} of letters, digits and dashes`);
|
|
290
332
|
const ram = spec.memoryMb === undefined ? 4 : spec.memoryMb / 1024;
|
|
@@ -296,7 +338,7 @@ export class OrgoProvider {
|
|
|
296
338
|
ram,
|
|
297
339
|
cpu,
|
|
298
340
|
// Orgo's end, the vendor's backstop: whole hours, deleting the computer and its disk. init ends it on time.
|
|
299
|
-
ends_in_hours: Math.max(1, Math.ceil(runLifetimeMs(spec) / HOUR)),
|
|
341
|
+
...(owner === undefined ? { ends_in_hours: Math.max(1, Math.ceil(runLifetimeMs(spec) / HOUR)) } : {}),
|
|
300
342
|
...(spec.image === BASE ? {} : { template_ref: spec.image }),
|
|
301
343
|
};
|
|
302
344
|
const made = await this.#rest.request("POST", "/computers", { body, timeoutMs: this.#timeouts.createMs, ...(signal ? { signal } : {}) });
|
|
@@ -310,8 +352,14 @@ export class OrgoProvider {
|
|
|
310
352
|
if (typeof id !== "string" || !COMPUTER_ID.test(id))
|
|
311
353
|
return vendorFail("orgo", `Orgo's create answered no computer id${await this.#deleteNamed(name)}`);
|
|
312
354
|
const ref = { id, name };
|
|
313
|
-
|
|
314
|
-
|
|
355
|
+
// An agent's computer's labels go in the ledger at once, so list finds it as the agent's whatever happens next.
|
|
356
|
+
if (owner !== undefined) {
|
|
357
|
+
const kept = await this.#keep({ id, labels });
|
|
358
|
+
if (!kept.ok)
|
|
359
|
+
return this.#abandon(ref, kept.error);
|
|
360
|
+
}
|
|
361
|
+
const file = { name, labels, deadline: owner === undefined ? deadline : NO_DEADLINE, maxDeadline: owner === undefined ? maxDeadline : NO_DEADLINE, image: spec.image, cpus: cpu, memoryMb: ram * 1024 };
|
|
362
|
+
const prepared = await this.#prepare(ref, made.value.status, file, daemon, signal, init);
|
|
315
363
|
if (signal?.aborted)
|
|
316
364
|
return this.#abandon(ref, { code: "aborted", message: "create was aborted" });
|
|
317
365
|
if (prepared.ok)
|
|
@@ -323,7 +371,7 @@ export class OrgoProvider {
|
|
|
323
371
|
* installed (its sha256 read back), Orgo's X server's path socket there, the labels file, init started, its first
|
|
324
372
|
* connection, then the daemon's report and the probe. Its value is the probe's notes.
|
|
325
373
|
*/
|
|
326
|
-
async #prepare(ref, status, file, daemon, signal) {
|
|
374
|
+
async #prepare(ref, status, file, daemon, signal, init = file) {
|
|
327
375
|
const { uploadMs, execMs } = this.#timeouts;
|
|
328
376
|
const running = status === "running" ? ok(undefined) : await this.#untilRunning(ref, signal);
|
|
329
377
|
if (!running.ok)
|
|
@@ -332,7 +380,7 @@ export class OrgoProvider {
|
|
|
332
380
|
const uploaded = await this.#channel.upload(ref.id, staging, daemon, { timeoutMs: uploadMs, ...(signal ? { signal } : {}) });
|
|
333
381
|
if (!uploaded.ok)
|
|
334
382
|
return fail(uploaded.error.code, `the daemon's upload into ${ref.name} failed: ${uploaded.error.message}`);
|
|
335
|
-
const commands = childCommands({ staging, deadline:
|
|
383
|
+
const commands = childCommands({ staging, deadline: init.deadline, user: `${this.#uid}:${this.#gid}`, display: this.#display, maxDeadline: init.maxDeadline });
|
|
336
384
|
const install = await this.#channel.run(ref.id, ["sh", "-c", commands.install], { timeoutMs: execMs, ...(signal ? { signal } : {}) });
|
|
337
385
|
if (!install.ok)
|
|
338
386
|
return install;
|
|
@@ -469,6 +517,10 @@ export class OrgoProvider {
|
|
|
469
517
|
const drift = [...found.value, ...probeNetworkDrift(probe.value)];
|
|
470
518
|
return drift.length === 0 ? ok(probeNotes(probe.value, "none")) : fail("refused", `it did not match its spec: ${drift.join("; ")}`);
|
|
471
519
|
}
|
|
520
|
+
/** Init's deadline and its most for an agent's own computer started at now: maxLifetimeMs out, and none in practice. */
|
|
521
|
+
#ownedInit(now) {
|
|
522
|
+
return { deadline: new Date(now + this.#maxLifetimeMs).toISOString(), maxDeadline: OWNED_MAX_DEADLINE };
|
|
523
|
+
}
|
|
472
524
|
#daemonExpected() {
|
|
473
525
|
const { kind, groups } = this.capabilities();
|
|
474
526
|
return { version: VERSION, uid: this.#uid, gid: this.#gid, arch: "amd64", kind, groups };
|
|
@@ -577,6 +629,54 @@ export class OrgoProvider {
|
|
|
577
629
|
}
|
|
578
630
|
return `; the computer named ${name}: ${said.join("; ")}`;
|
|
579
631
|
}
|
|
632
|
+
/** An agent's computer's ledger entry, put; a ledger that fails is a failure in words. */
|
|
633
|
+
async #keep(entry) {
|
|
634
|
+
try {
|
|
635
|
+
await this.#ledger.put(entry);
|
|
636
|
+
return ok(undefined);
|
|
637
|
+
}
|
|
638
|
+
catch (error) {
|
|
639
|
+
return vendorFail("orgo", `the ledger of agents' computers could not be written: ${error instanceof Error ? error.message : String(error)}`);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
/** Every agent's computer's ledger entry, by id. */
|
|
643
|
+
async #entries() {
|
|
644
|
+
try {
|
|
645
|
+
return ok(new Map((await this.#ledger.list()).map((entry) => [entry.id, entry])));
|
|
646
|
+
}
|
|
647
|
+
catch (error) {
|
|
648
|
+
return vendorFail("orgo", `the ledger of agents' computers could not be read: ${error instanceof Error ? error.message : String(error)}`);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
/** A deleted computer's ledger entry, gone; best effort, since the computer is gone whatever the ledger says. */
|
|
652
|
+
async #forget(id) {
|
|
653
|
+
try {
|
|
654
|
+
await this.#ledger.remove(id);
|
|
655
|
+
}
|
|
656
|
+
catch {
|
|
657
|
+
// A stale entry names a computer Orgo no longer has: list never shows it.
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* An agent's computer deleted under `claim` (ReleaseContext): its entry taken first, only while it carries exactly the
|
|
662
|
+
* claim's run and epoch and no attach or detach holds it (takeEntry, only), so a claim that made it and lost it never
|
|
663
|
+
* deletes it from under a later claim. A delete Orgo refused lets the entry go again; one Orgo took forgot it already.
|
|
664
|
+
*/
|
|
665
|
+
#claimedDelete(ref, name, claim, signal) {
|
|
666
|
+
const owner = nameParts(name)?.owner;
|
|
667
|
+
if (owner === undefined)
|
|
668
|
+
return Promise.resolve(fail("refused", `${ref.name} is a run's computer: a delete under a claim is an agent's own computer's alone, so it is left as it is`));
|
|
669
|
+
return inTurn(this.#owning, ref.id, async () => {
|
|
670
|
+
const taken = await takeEntry(this.#ledger, ref, owner, claim, "being deleted", { only: true });
|
|
671
|
+
if (!taken.ok)
|
|
672
|
+
return taken;
|
|
673
|
+
const deleted = await this.#delete(ref, signal);
|
|
674
|
+
if (deleted.ok)
|
|
675
|
+
return deleted;
|
|
676
|
+
const left = await this.#entries();
|
|
677
|
+
return left.ok && !left.value.has(ref.id) ? deleted : andSettled(deleted, await settleEntry(this.#ledger, ref, taken.value));
|
|
678
|
+
});
|
|
679
|
+
}
|
|
580
680
|
/** DELETE, tried again while Orgo answers 409 (waking), then the wait until Orgo no longer has the computer. */
|
|
581
681
|
async #delete(ref, signal) {
|
|
582
682
|
const { requestMs, pollMs, goneMs } = this.#timeouts;
|
|
@@ -589,6 +689,7 @@ export class OrgoProvider {
|
|
|
589
689
|
return deleted;
|
|
590
690
|
await pause(pollMs);
|
|
591
691
|
}
|
|
692
|
+
await this.#forget(ref.id);
|
|
592
693
|
const until = Date.now() + goneMs;
|
|
593
694
|
let last = "unread";
|
|
594
695
|
for (;;) {
|
|
@@ -640,7 +741,7 @@ export class OrgoProvider {
|
|
|
640
741
|
await opened.value.close();
|
|
641
742
|
return fail("refused", `the daemon in ${ref.name} does not match this provider: ${drift.join("; ")}`);
|
|
642
743
|
}
|
|
643
|
-
async attach(ref, { signal } = {}) {
|
|
744
|
+
async attach(ref, { signal, claim } = {}) {
|
|
644
745
|
const given = (typeof ref === "object" && ref !== null ? ref : {});
|
|
645
746
|
if ((given.id === undefined) === (given.labels === undefined))
|
|
646
747
|
return fail("invalid", "attach takes { id } or { labels }, one of them");
|
|
@@ -652,7 +753,7 @@ export class OrgoProvider {
|
|
|
652
753
|
return read.error.code === "not_found" ? fail("not_found", `no Orgo computer has the id ${given.id}`) : read;
|
|
653
754
|
if (!this.#ours(read.value))
|
|
654
755
|
return fail("not_found", `no Orgo computer of this provider has the id ${given.id}`);
|
|
655
|
-
return this.#attached(read.value, signal);
|
|
756
|
+
return this.#attached(read.value, signal, claim);
|
|
656
757
|
}
|
|
657
758
|
const problem = filterProblem(given.labels);
|
|
658
759
|
if (problem)
|
|
@@ -671,58 +772,126 @@ export class OrgoProvider {
|
|
|
671
772
|
const found = await this.#own(listed.value[0].ref, signal);
|
|
672
773
|
if (!found.ok)
|
|
673
774
|
return found;
|
|
674
|
-
return this.#attached(found.value, signal);
|
|
775
|
+
return this.#attached(found.value, signal, claim);
|
|
675
776
|
}
|
|
676
777
|
/**
|
|
677
778
|
* The drift check of a computer attach did not make: its labels file, the truth, held to its name, then its daemon
|
|
678
779
|
* and the probe, as at create. One that drifts is refused and left as it is. A computer that does not run is
|
|
679
|
-
* refused: reading it from inside would wake it.
|
|
780
|
+
* refused: reading it from inside would wake it. An agent's own computer is the exception: attaching it is asking
|
|
781
|
+
* for it. Its ledger entry is taken for the claim first, busy (takeEntry), before anything is done to the computer;
|
|
782
|
+
* one a detach suspended is then resumed (#wake), its memory and init with it; its daemon is moved on or installed
|
|
783
|
+
* again (#ownedInside); and it is marked held in the ledger last (settleEntry), by this attach alone, while the entry
|
|
784
|
+
* is still the one it holds. An attach that resumed it and then fails suspends it again, as it was found (#putBack).
|
|
785
|
+
* This process's attaches and detaches of one agent's computer are taken in turn; another process's are refused while
|
|
786
|
+
* this one holds the entry, so no attach or detach ends the programs or sleep of another's.
|
|
787
|
+
*/
|
|
788
|
+
async #attached(record, signal, claim) {
|
|
789
|
+
const ref = { id: record.id, name: record.name };
|
|
790
|
+
const parts = nameParts(record.name);
|
|
791
|
+
if (parts.owner === undefined) {
|
|
792
|
+
if (claim !== undefined)
|
|
793
|
+
return fail("refused", claimProblem(ref.name, undefined, undefined, claim));
|
|
794
|
+
if (record.status !== "running")
|
|
795
|
+
return fail("refused", `${ref.name} is ${record.status}: its labels file and daemon are read from inside, and a read would wake it`);
|
|
796
|
+
return this.#checked(record, parts, signal);
|
|
797
|
+
}
|
|
798
|
+
const owner = parts.owner;
|
|
799
|
+
return inTurn(this.#owning, ref.id, async (waited) => {
|
|
800
|
+
// Read again after waiting: the attach or detach before this one may have resumed or suspended it.
|
|
801
|
+
const now = waited ? await this.#own(ref, signal) : ok(record);
|
|
802
|
+
if (!now.ok)
|
|
803
|
+
return now;
|
|
804
|
+
const taken = await takeEntry(this.#ledger, ref, owner, claim, "being attached");
|
|
805
|
+
if (!taken.ok)
|
|
806
|
+
return taken;
|
|
807
|
+
const lease = taken.value;
|
|
808
|
+
const woke = now.value.status !== "running";
|
|
809
|
+
const attached = await this.#resumed(now.value, parts, lease, woke, signal, claim);
|
|
810
|
+
if (!attached.ok && woke)
|
|
811
|
+
return this.#putBack(ref, lease, attached.error);
|
|
812
|
+
// Marked held (no longer detached) once attached; a failed attach that woke nothing leaves it as it was.
|
|
813
|
+
return andSettled(attached, await settleEntry(this.#ledger, ref, lease, attached.ok ? ({ detachedAt: _held, ...held }) => held : undefined));
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* An agent's own computer, its entry taken: resumed when it was not running; under a claim, its labels file carries
|
|
818
|
+
* the claim's run and epoch from now on, as the ledger does; then checked.
|
|
819
|
+
*/
|
|
820
|
+
async #resumed(record, parts, lease, woke, signal, claim) {
|
|
821
|
+
const ref = { id: record.id, name: record.name };
|
|
822
|
+
if (woke) {
|
|
823
|
+
const woken = await this.#wake(ref, record.status, signal);
|
|
824
|
+
if (!woken.ok)
|
|
825
|
+
return woken;
|
|
826
|
+
}
|
|
827
|
+
if (claim !== undefined) {
|
|
828
|
+
// Its labels file is written under the claim only while this attach still holds the entry, however long the wake took.
|
|
829
|
+
if (woke) {
|
|
830
|
+
const renewed = await renewEntry(this.#ledger, ref, lease, "being attached");
|
|
831
|
+
if (!renewed.ok)
|
|
832
|
+
return renewed;
|
|
833
|
+
}
|
|
834
|
+
const read = await this.#readLabels(ref, signal);
|
|
835
|
+
if (!read.ok)
|
|
836
|
+
return read;
|
|
837
|
+
const written = await this.#writeLabels(ref, { ...read.value, labels: lease.entry.labels }, signal);
|
|
838
|
+
if (!written.ok)
|
|
839
|
+
return written;
|
|
840
|
+
}
|
|
841
|
+
return this.#checked(record, parts, signal, lease);
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* An agent's own computer this attach resumed, suspended again once the attach fails, best effort, as release detach
|
|
845
|
+
* suspends it, so it is left as it was found; its ledger entry still says when it was detached (takeEntry kept it).
|
|
846
|
+
* One another claim has taken since is that claim's, and runs on. Never signalled: it outlives an abort.
|
|
680
847
|
*/
|
|
681
|
-
async #
|
|
848
|
+
async #putBack(ref, lease, error) {
|
|
849
|
+
const renewed = await renewEntry(this.#ledger, ref, lease, "being put back to sleep by an attach that failed");
|
|
850
|
+
if (!renewed.ok)
|
|
851
|
+
return fail(error.code, `${error.message}; ${ref.name} runs on, not suspended again: ${renewed.error.message}`);
|
|
852
|
+
const suspended = await this.#suspend(ref);
|
|
853
|
+
const failed = fail(error.code, `${error.message}; ${ref.name}, which this attach resumed, ${suspended.ok ? "was suspended again, as it was found" : `could not be suspended again (${suspended.error.message})`}`);
|
|
854
|
+
return andSettled(failed, await settleEntry(this.#ledger, ref, lease));
|
|
855
|
+
}
|
|
856
|
+
/** The labels file, the truth, held to the name, then the daemon and the probe: the drift check of #attached. `lease`:
|
|
857
|
+
* an agent's computer's ledger entry as this attach holds it, for its daemon's replacement. */
|
|
858
|
+
async #checked(record, parts, signal, lease) {
|
|
682
859
|
const ref = { id: record.id, name: record.name };
|
|
683
|
-
if (record.status !== "running")
|
|
684
|
-
return fail("refused", `${ref.name} is ${record.status}: its labels file and daemon are read from inside, and a read would wake it`);
|
|
685
860
|
const file = await this.#readLabels(ref, signal);
|
|
686
861
|
if (!file.ok)
|
|
687
862
|
return file;
|
|
688
863
|
const found = [];
|
|
689
|
-
const parts = nameParts(record.name);
|
|
690
864
|
const { labels } = file.value;
|
|
691
865
|
if (typeof file.value.name !== "string" || !sameComputer(file.value.name, record.name))
|
|
692
866
|
found.push(`name: Orgo's is ${record.name}, the labels file's ${show(file.value.name)}`);
|
|
867
|
+
if ((parts.owner ?? "") !== ownerHashOfLabels(labels))
|
|
868
|
+
found.push(`owner: the name's owner hash ${show(parts.owner)} is not the labels file's owner ${show(labels[OWNER_LABEL])}`);
|
|
693
869
|
const run = labels[RUN_LABEL];
|
|
694
|
-
|
|
870
|
+
// An agent's own computer is no run's: its name carries no run or epoch, whatever run made it.
|
|
871
|
+
if (parts.owner === undefined && (parts.run ?? "") !== (typeof run === "string" && run !== "" ? runHash(run) : ""))
|
|
695
872
|
found.push(`run: the name's run hash ${show(parts.run)} is not the labels file's run ${show(run)}`);
|
|
696
|
-
if ((parts.epoch ?? "") !== (labels[EPOCH_LABEL] ?? ""))
|
|
873
|
+
if (parts.owner === undefined && (parts.epoch ?? "") !== (labels[EPOCH_LABEL] ?? ""))
|
|
697
874
|
found.push(`epoch: the name says ${show(parts.epoch)}, the labels file ${show(labels[EPOCH_LABEL])}`);
|
|
698
|
-
const deadline = new Date(file.value.deadline);
|
|
699
|
-
if (Number.isNaN(deadline.getTime()))
|
|
875
|
+
const deadline = parts.owner === undefined ? new Date(file.value.deadline) : undefined;
|
|
876
|
+
if (deadline !== undefined && Number.isNaN(deadline.getTime()))
|
|
700
877
|
found.push(`deadline: the labels file reads ${show(file.value.deadline)}, not a time`);
|
|
878
|
+
if (parts.owner !== undefined && file.value.deadline !== NO_DEADLINE)
|
|
879
|
+
found.push(`deadline: an agent's own computer has none, and the labels file reads ${show(file.value.deadline)}`);
|
|
701
880
|
let notes = [];
|
|
702
|
-
let kind;
|
|
703
881
|
if (found.length === 0) {
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
if (!
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
if (!inside.ok && inside.error.code !== "refused")
|
|
713
|
-
return inside;
|
|
714
|
-
if (!inside.ok)
|
|
715
|
-
found.push(inside.error.message.replace(/^it did not match its spec: /, ""));
|
|
716
|
-
else
|
|
717
|
-
notes = inside.value;
|
|
718
|
-
}
|
|
719
|
-
finally {
|
|
720
|
-
await opened.value.close();
|
|
721
|
-
}
|
|
722
|
-
kind = opened.value.hello.kind;
|
|
882
|
+
// lease is given for an agent's own computer alone (#resumed).
|
|
883
|
+
const inside = lease === undefined ? await this.#inside(ref, signal) : await this.#ownedInside(ref, file.value, lease, signal);
|
|
884
|
+
if (!inside.ok && inside.error.code !== "refused")
|
|
885
|
+
return inside;
|
|
886
|
+
if (!inside.ok)
|
|
887
|
+
found.push(inside.error.message.replace(/^it did not match its spec: /, ""));
|
|
888
|
+
else
|
|
889
|
+
notes = inside.value;
|
|
723
890
|
}
|
|
724
891
|
if (found.length > 0)
|
|
725
892
|
return fail("refused", `the computer ${ref.name} does not match this provider, so attach refuses it and it is left as it is: ${found.join("; ")}`);
|
|
893
|
+
// The daemon's hello passed the drift check, which holds its kind to this provider's.
|
|
894
|
+
const { kind } = this.capabilities();
|
|
726
895
|
return ok({
|
|
727
896
|
...ref,
|
|
728
897
|
spec: {
|
|
@@ -732,12 +901,110 @@ export class OrgoProvider {
|
|
|
732
901
|
...(file.value.cpus === undefined ? {} : { cpus: file.value.cpus }),
|
|
733
902
|
...(file.value.memoryMb === undefined ? {} : { memoryMb: file.value.memoryMb }),
|
|
734
903
|
workdir: WORK,
|
|
735
|
-
deadline,
|
|
736
|
-
|
|
904
|
+
...(deadline === undefined ? {} : { deadline }),
|
|
905
|
+
kind,
|
|
906
|
+
...ownerReadBack(labels),
|
|
737
907
|
},
|
|
738
908
|
...(notes.length > 0 ? { notes } : {}),
|
|
739
909
|
});
|
|
740
910
|
}
|
|
911
|
+
/** A run's computer's daemon and the probe, as at create: the probe's notes. */
|
|
912
|
+
async #inside(ref, signal) {
|
|
913
|
+
const ports = await this.#rootPorts(ref, signal);
|
|
914
|
+
if (!ports.ok)
|
|
915
|
+
return ports;
|
|
916
|
+
const opened = await this.#open(ref, signal);
|
|
917
|
+
if (!opened.ok)
|
|
918
|
+
return opened;
|
|
919
|
+
try {
|
|
920
|
+
return await this.#checkInside(opened.value, ref.name, ports.value, signal);
|
|
921
|
+
}
|
|
922
|
+
finally {
|
|
923
|
+
await opened.value.close();
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
* An agent's own computer's daemon: init's deadline moved maxLifetimeMs out (deadline.extend), then checked as at
|
|
928
|
+
* create. One that does not answer (init ended at its deadline: the desktop was detached past maxLifetimeMs), answers
|
|
929
|
+
* as another version (the package was upgraded while it was detached), or whose deadline no longer moves (init is
|
|
930
|
+
* ending) is installed and started again over the same disk (#replaceDaemon). A run's computer is never replaced
|
|
931
|
+
* (#inside): its daemon is its run's, and one that drifts refuses the attach.
|
|
932
|
+
*/
|
|
933
|
+
async #ownedInside(ref, file, lease, signal) {
|
|
934
|
+
const ports = await this.#rootPorts(ref, signal);
|
|
935
|
+
if (!ports.ok)
|
|
936
|
+
return ports;
|
|
937
|
+
const opened = await this.#open(ref, signal);
|
|
938
|
+
if (!opened.ok)
|
|
939
|
+
return opened.notListening ? this.#replaceDaemon(ref, file, lease, "its daemon did not answer (init had ended)", signal) : opened;
|
|
940
|
+
let stale;
|
|
941
|
+
try {
|
|
942
|
+
const { client, hello } = opened.value;
|
|
943
|
+
if (hello.version !== VERSION)
|
|
944
|
+
stale = `its daemon was version ${hello.version}, not this provider's ${VERSION}`;
|
|
945
|
+
else {
|
|
946
|
+
const at = new Date(Date.now() + this.#maxLifetimeMs).toISOString();
|
|
947
|
+
const extended = await client.call("deadline.extend", { at }, { signal: this.#bounded(this.#timeouts.helloMs, signal) });
|
|
948
|
+
// invalid: init's deadline is already later than at (maxLifetimeMs was lowered since), and it is kept.
|
|
949
|
+
if (!extended.ok && extended.error.code === "unsupported")
|
|
950
|
+
stale = `its init's deadline no longer moved (${extended.error.message})`;
|
|
951
|
+
else if (!extended.ok && extended.error.code !== "invalid")
|
|
952
|
+
return fail(extended.error.code, `init's deadline in ${ref.name} did not move to ${at}: ${extended.error.message}`);
|
|
953
|
+
else
|
|
954
|
+
return await this.#checkInside(opened.value, ref.name, ports.value, signal);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
finally {
|
|
958
|
+
await opened.value.close();
|
|
959
|
+
}
|
|
960
|
+
return this.#replaceDaemon(ref, file, lease, stale, signal);
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
* An agent's own computer's daemon installed and started again, its disk kept: the old init stopped by its exact
|
|
964
|
+
* processes, which ends its programs (STOP_INIT_SCRIPT), then every step create takes (#prepare: the upload, the
|
|
965
|
+
* install with its sha256 read back, the X socket, the labels file, root's ports, the lockdown, init, its first
|
|
966
|
+
* connection and the drift check). The attach's busy marker is renewed first (renewEntry), only while the ledger entry
|
|
967
|
+
* is still the one this attach holds (`lease`): an attach that lost it (its marker went stale and another took the
|
|
968
|
+
* computer) stops nothing, so an older attach never stops the init a newer one started. The probe's notes, after one
|
|
969
|
+
* that says the daemon was replaced and why.
|
|
970
|
+
*/
|
|
971
|
+
async #replaceDaemon(ref, file, lease, why, signal) {
|
|
972
|
+
let daemon;
|
|
973
|
+
try {
|
|
974
|
+
daemon = readFileSync(this.#daemonPath);
|
|
975
|
+
}
|
|
976
|
+
catch {
|
|
977
|
+
return fail("not_found", `${ref.name}'s daemon needs replacing, since ${why}, and there is no daemon binary at ${this.#daemonPath}: run npm run build:daemon -w @parcha/agentrun-computer`);
|
|
978
|
+
}
|
|
979
|
+
const renewed = await renewEntry(this.#ledger, ref, lease, "its daemon being replaced by an attach");
|
|
980
|
+
if (!renewed.ok)
|
|
981
|
+
return fail(renewed.error.code, `${ref.name}'s daemon needs replacing, since ${why}, and is left as it is: ${renewed.error.message}`);
|
|
982
|
+
const stopped = await this.#channel.run(ref.id, ["sh", "-c", STOP_INIT_SCRIPT, "sh", DAEMON_IN_COMPUTER], { timeoutMs: this.#timeouts.execMs, ...(signal ? { signal } : {}) });
|
|
983
|
+
if (!stopped.ok)
|
|
984
|
+
return stopped;
|
|
985
|
+
if (stopped.value.exitCode !== 0)
|
|
986
|
+
return fail("refused", `${ref.name}'s daemon needs replacing, since ${why}, and its old init could not be stopped (exit ${stopped.value.exitCode}): ${stopped.value.output.trim().slice(0, 200)}`);
|
|
987
|
+
const prepared = await this.#prepare(ref, "running", file, daemon, signal, this.#ownedInit(Date.now()));
|
|
988
|
+
if (!prepared.ok)
|
|
989
|
+
return fail(prepared.error.code, `replacing ${ref.name}'s daemon, since ${why}, failed: ${prepared.error.message}`);
|
|
990
|
+
return ok([`the daemon in ${ref.name} was replaced with this provider's ${VERSION}, its disk kept, since ${why}`, ...prepared.value]);
|
|
991
|
+
}
|
|
992
|
+
/**
|
|
993
|
+
* An agent's own computer that a detach suspended, resumed for its attach: Orgo's resume (its memory back, computerd
|
|
994
|
+
* init still running), then running. One stopped outside this provider comes back from its disk alone, with no init:
|
|
995
|
+
* Orgo's start, and the daemon is then installed again (#ownedInside).
|
|
996
|
+
*/
|
|
997
|
+
async #wake(ref, status, signal) {
|
|
998
|
+
const how = status === "suspended" || status === "suspending" ? "resume" : status === "frozen" || status === "stopped" ? "start" : undefined;
|
|
999
|
+
if (how === undefined && !RUNNING.has(status))
|
|
1000
|
+
return fail("refused", `${ref.name} is ${status}: no resume or start brings it back`);
|
|
1001
|
+
if (how !== undefined) {
|
|
1002
|
+
const woken = await this.#rest.request("POST", `/computers/${encodeURIComponent(ref.id)}/${how}`, { body: {}, timeoutMs: this.#timeouts.createMs, ...(signal ? { signal } : {}) });
|
|
1003
|
+
if (!woken.ok)
|
|
1004
|
+
return fail(woken.error.code, `${ref.name} could not ${how} for its attach: ${woken.error.message}`);
|
|
1005
|
+
}
|
|
1006
|
+
return this.#untilRunning(ref, signal);
|
|
1007
|
+
}
|
|
741
1008
|
/**
|
|
742
1009
|
* Every computer of this provider in the workspace that carries all of these labels. The run and the epoch are read
|
|
743
1010
|
* from the name, with nothing run in a computer, so a stopped one is listed too; any other label is read from the
|
|
@@ -751,6 +1018,10 @@ export class OrgoProvider {
|
|
|
751
1018
|
if (!listed.ok)
|
|
752
1019
|
return listed;
|
|
753
1020
|
const others = Object.keys(labels).filter((key) => key !== RUN_LABEL && key !== EPOCH_LABEL);
|
|
1021
|
+
// An agent's computers' labels are the ledger's: read once, only when the workspace holds one.
|
|
1022
|
+
const owned = listed.value.some((record) => nameParts(record.name)?.owner !== undefined) ? await this.#entries() : ok(new Map());
|
|
1023
|
+
if (!owned.ok)
|
|
1024
|
+
return owned;
|
|
754
1025
|
const infos = [];
|
|
755
1026
|
for (const record of listed.value) {
|
|
756
1027
|
const parts = nameParts(record.name);
|
|
@@ -758,12 +1029,22 @@ export class OrgoProvider {
|
|
|
758
1029
|
// (orgo-desktop.ts), never this one's to list or its janitor's to sweep.
|
|
759
1030
|
if (parts === undefined || record.status === "deleted" || (record.os !== undefined && record.os !== "linux"))
|
|
760
1031
|
continue;
|
|
1032
|
+
const ref = { id: record.id, name: record.name };
|
|
1033
|
+
const running = RUNNING.has(record.status);
|
|
1034
|
+
// An agent's computer: every label from its ledger entry, with nothing run inside, whatever its state; no deadline.
|
|
1035
|
+
if (parts.owner !== undefined) {
|
|
1036
|
+
const view = ownedListing(owned.value.get(record.id), labels);
|
|
1037
|
+
if (view === undefined)
|
|
1038
|
+
continue;
|
|
1039
|
+
infos.push({ ref, deadline: undefined, state: running ? "running" : "stopped", network: "none", ...view });
|
|
1040
|
+
continue;
|
|
1041
|
+
}
|
|
1042
|
+
if (labels[OWNER_LABEL] !== undefined || labels[OWNER_SERVER_LABEL] !== undefined)
|
|
1043
|
+
continue;
|
|
761
1044
|
if (labels[RUN_LABEL] !== undefined && parts.run !== runHash(labels[RUN_LABEL]))
|
|
762
1045
|
continue;
|
|
763
1046
|
if (labels[EPOCH_LABEL] !== undefined && parts.epoch !== labels[EPOCH_LABEL])
|
|
764
1047
|
continue;
|
|
765
|
-
const ref = { id: record.id, name: record.name };
|
|
766
|
-
const running = RUNNING.has(record.status);
|
|
767
1048
|
// The name keeps the run's hash, not the run: listed with no run to match, the hash is given, so a caller (serve's
|
|
768
1049
|
// janitor) can still tell whose run a computer is.
|
|
769
1050
|
let read = {
|
|
@@ -821,23 +1102,61 @@ export class OrgoProvider {
|
|
|
821
1102
|
await opened.value.close();
|
|
822
1103
|
}
|
|
823
1104
|
}
|
|
824
|
-
/**
|
|
825
|
-
|
|
1105
|
+
/**
|
|
1106
|
+
* delete: idempotent, a computer Orgo no longer has is done; it waits until Orgo no longer has it. detach, for an
|
|
1107
|
+
* agent's own computer alone: Orgo's suspend, its memory and disk saved, read back suspended. No stop: a run's
|
|
1108
|
+
* computer is deleted.
|
|
1109
|
+
*/
|
|
1110
|
+
async release(ref, how, { signal, claim } = {}) {
|
|
826
1111
|
const problem = refProblem(ref);
|
|
827
1112
|
if (problem)
|
|
828
1113
|
return fail("invalid", problem);
|
|
829
|
-
if (how !== "stop" && how !== "delete")
|
|
830
|
-
return fail("invalid", `release is "stop" or "
|
|
1114
|
+
if (how !== "stop" && how !== "delete" && how !== "detach")
|
|
1115
|
+
return fail("invalid", `release is "stop", "delete" or "detach", not ${String(how)}`);
|
|
831
1116
|
if (how === "stop")
|
|
832
|
-
return fail("unsupported", "an Orgo computer has no stop that this provider declares: release it with delete");
|
|
1117
|
+
return fail("unsupported", "an Orgo computer has no stop that this provider declares: release it with delete, or detach an agent's own");
|
|
833
1118
|
const read = await this.#get(ref.id);
|
|
834
1119
|
if (!read.ok)
|
|
835
|
-
return read.error.code === "not_found" ? ok(undefined) : read;
|
|
1120
|
+
return read.error.code === "not_found" && how === "delete" ? ok(undefined) : read;
|
|
836
1121
|
if (read.value.status === "deleted")
|
|
837
|
-
return ok(undefined);
|
|
1122
|
+
return how === "delete" ? ok(undefined) : fail("not_found", `${ref.name} is deleted: there is nothing to detach`);
|
|
838
1123
|
if (!this.#ours(read.value, ref.name))
|
|
839
1124
|
return fail("not_found", `no Orgo computer of this provider is ${ref.name} (${ref.id}), so it is left as it is`);
|
|
840
|
-
|
|
1125
|
+
if (how === "delete")
|
|
1126
|
+
return claim === undefined ? this.#delete(ref, signal) : this.#claimedDelete(ref, read.value.name, claim, signal);
|
|
1127
|
+
const owner = nameParts(read.value.name)?.owner;
|
|
1128
|
+
if (owner === undefined)
|
|
1129
|
+
return fail("refused", `${ref.name} is a run's computer, not an agent's own: it is deleted at its release, never detached`);
|
|
1130
|
+
// In turn with this process's attaches, and holding the ledger entry from before the suspend until it is marked
|
|
1131
|
+
// detached (takeEntry, settleEntry), so no attach resumes it meanwhile and an attach under way refuses this detach.
|
|
1132
|
+
return inTurn(this.#owning, ref.id, async (waited) => {
|
|
1133
|
+
const taken = await takeEntry(this.#ledger, ref, owner, undefined, "being detached");
|
|
1134
|
+
if (!taken.ok)
|
|
1135
|
+
return taken;
|
|
1136
|
+
const now = waited ? await this.#own(ref, signal) : ok(read.value);
|
|
1137
|
+
const suspended = !now.ok ? now : now.value.status === "suspended" ? ok(undefined) : await this.#suspend(ref, signal);
|
|
1138
|
+
return andSettled(suspended, await settleEntry(this.#ledger, ref, taken.value, suspended.ok ? (entry) => ({ ...entry, detachedAt: entry.detachedAt ?? new Date().toISOString() }) : undefined));
|
|
1139
|
+
});
|
|
1140
|
+
}
|
|
1141
|
+
/** Orgo's suspend, the computer's memory and disk saved, then the wait until Orgo reads it suspended. */
|
|
1142
|
+
async #suspend(ref, signal) {
|
|
1143
|
+
const suspended = await this.#rest.request("POST", `/computers/${encodeURIComponent(ref.id)}/suspend`, { timeoutMs: this.#timeouts.createMs, ...(signal ? { signal } : {}) });
|
|
1144
|
+
if (!suspended.ok)
|
|
1145
|
+
return fail(suspended.error.code, `${ref.name} could not be suspended for its agent: ${suspended.error.message}`);
|
|
1146
|
+
// 202 suspending: its memory is still being saved. Read until Orgo says suspended.
|
|
1147
|
+
const until = Date.now() + this.#timeouts.goneMs;
|
|
1148
|
+
for (;;) {
|
|
1149
|
+
const now = await this.#get(ref.id, signal);
|
|
1150
|
+
if (!now.ok)
|
|
1151
|
+
return now;
|
|
1152
|
+
if (now.value.status === "suspended")
|
|
1153
|
+
return ok(undefined);
|
|
1154
|
+
if (Date.now() >= until)
|
|
1155
|
+
return fail("timeout", `${ref.name} was not suspended ${seconds(this.#timeouts.goneMs)} after its suspend; Orgo last read ${now.value.status}`);
|
|
1156
|
+
await pause(this.#timeouts.pollMs, signal);
|
|
1157
|
+
if (signal?.aborted)
|
|
1158
|
+
return fail("aborted", `the wait for ${ref.name} to be suspended was aborted`);
|
|
1159
|
+
}
|
|
841
1160
|
}
|
|
842
1161
|
/**
|
|
843
1162
|
* Moves the deadline to now + ttlMs, never earlier and never past the most the labels file holds (create's time plus
|
|
@@ -847,16 +1166,7 @@ export class OrgoProvider {
|
|
|
847
1166
|
* is refused as earlier, never renaming the computer back to an earlier deadline the janitor would trust.
|
|
848
1167
|
*/
|
|
849
1168
|
extend(ref, ttlMs, context = {}) {
|
|
850
|
-
|
|
851
|
-
const before = this.#extending.get(key) ?? Promise.resolve();
|
|
852
|
-
const turn = before.then(() => this.#extend(ref, ttlMs, context));
|
|
853
|
-
const settled = turn.then(() => undefined, () => undefined);
|
|
854
|
-
this.#extending.set(key, settled);
|
|
855
|
-
void settled.then(() => {
|
|
856
|
-
if (this.#extending.get(key) === settled)
|
|
857
|
-
this.#extending.delete(key);
|
|
858
|
-
});
|
|
859
|
-
return turn;
|
|
1169
|
+
return inTurn(this.#extending, typeof ref?.id === "string" ? ref.id : "", () => this.#extend(ref, ttlMs, context));
|
|
860
1170
|
}
|
|
861
1171
|
async #extend(ref, ttlMs, { signal } = {}) {
|
|
862
1172
|
const problem = refProblem(ref);
|
|
@@ -867,6 +1177,8 @@ export class OrgoProvider {
|
|
|
867
1177
|
const found = await this.#own(ref, signal);
|
|
868
1178
|
if (!found.ok)
|
|
869
1179
|
return found;
|
|
1180
|
+
if (nameParts(found.value.name)?.owner !== undefined)
|
|
1181
|
+
return fail("refused", `${ref.name} is an agent's own computer: it has no deadline to move`);
|
|
870
1182
|
if (found.value.status !== "running")
|
|
871
1183
|
return fail("refused", `${ref.name} is ${found.value.status}, not running`);
|
|
872
1184
|
const file = await this.#readLabels(ref, signal);
|