@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-desktop.js
CHANGED
|
@@ -33,14 +33,14 @@ import { readFileSync } from "node:fs";
|
|
|
33
33
|
import { checkActions } from "./actions.js";
|
|
34
34
|
import { readPng } from "./conformance/png.js";
|
|
35
35
|
import { MACOS_GROUPS, ORGO_MACOS_IMAGE, ORGO_WINDOWS_IMAGE, WINDOWS_GROUPS } from "./descriptors/orgo.js";
|
|
36
|
-
import { EPOCH_LABEL, RUN_HASH_LABEL, RUN_LABEL, runHash } from "./labels.js";
|
|
36
|
+
import { EPOCH_LABEL, OWNER_LABEL, OWNER_SERVER_LABEL, ownerLabels, RUN_HASH_LABEL, RUN_LABEL, runHash } from "./labels.js";
|
|
37
37
|
import { checkMount } from "./mount.js";
|
|
38
38
|
import { ORGO_API_URL, orgoRest } from "./orgo-channel.js";
|
|
39
|
-
import { COMPUTER_ID, filterProblem, labelProblem, labelsOf, NAME_MAX, nameParts, orgoName, refProblem, sameComputer, show, withDeadline, } from "./orgo-names.js";
|
|
39
|
+
import { COMPUTER_ID, claimProblem, andSettled, inTurn, filterProblem, labelProblem, labelsOf, NAME_MAX, NO_DEADLINE, nameParts, orgoMemoryLedger, orgoName, ownedListing, ownerHashOfLabels, ownerReadBack, refProblem, sameComputer, settleEntry, show, renewEntry, takeEntry, withDeadline, } from "./orgo-names.js";
|
|
40
40
|
import { encodePng } from "./phone-harness.js";
|
|
41
41
|
import { MAX_CHUNK_BYTES, MAX_LIST_ENTRIES, MAX_OUTPUT_BYTES, PROTOCOL_VERSION, } from "./protocol.js";
|
|
42
42
|
import { checkGpu, checkTenure, runLifetimeMs, } from "./provider.js";
|
|
43
|
-
import { failFor, ok } from "./result.js";
|
|
43
|
+
import { failFor, ok, vendorFail } from "./result.js";
|
|
44
44
|
import { daemonWatch } from "./screen-watch.js";
|
|
45
45
|
import { listTested } from "./test-connection.js";
|
|
46
46
|
/** This module's fail: a vendor failure names Orgo (CP-1's one vendor code). */
|
|
@@ -153,7 +153,8 @@ const FILES_SCRIPT = [
|
|
|
153
153
|
"$ErrorActionPreference = 'Stop'",
|
|
154
154
|
"$p = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('@@REQUEST@@')) | ConvertFrom-Json",
|
|
155
155
|
"function Say($o) { [Console]::Out.Write(($o | ConvertTo-Json -Compress -Depth 6)) }",
|
|
156
|
-
"
|
|
156
|
+
"# A refusal leaves nothing of the request behind: a write's uploaded copy goes with it.",
|
|
157
|
+
"function Refuse($kind, $words) { if ($p.staged) { Remove-Item -LiteralPath $p.staged -Force -ErrorAction SilentlyContinue }; Say @{ error = $kind; message = $words }; exit 2 }",
|
|
157
158
|
"function IsLink($i) { $i.LinkType -eq 'SymbolicLink' -or $i.LinkType -eq 'Junction' }",
|
|
158
159
|
"function StatOf($i) {",
|
|
159
160
|
" $t = if (IsLink $i) { 'symlink' } elseif ($i.PSIsContainer) { 'dir' } else { 'file' }",
|
|
@@ -282,6 +283,9 @@ const FILES_SCRIPT = [
|
|
|
282
283
|
" }",
|
|
283
284
|
"} catch {",
|
|
284
285
|
" $e = $_.Exception",
|
|
286
|
+
" # A .NET method's error (a file Open, WriteAllBytes) reaches here wrapped in PowerShell's MethodInvocationException:",
|
|
287
|
+
" # its code is the error inside.",
|
|
288
|
+
" while ($e.InnerException -and $e.GetType().Name -match 'MethodInvocation|TargetInvocation') { $e = $e.InnerException }",
|
|
285
289
|
" $kind = switch -regex ($e.GetType().Name) { 'NotFound' { 'not_found' } 'UnauthorizedAccess|Security' { 'denied' } 'PathTooLong' { 'limit' } default { 'invalid' } }",
|
|
286
290
|
" Refuse $kind $e.Message",
|
|
287
291
|
"}",
|
|
@@ -339,9 +343,12 @@ export class OrgoDesktopProvider {
|
|
|
339
343
|
* the run's input moves it, and the live view's own client (daemonWatch) draws the cursor there.
|
|
340
344
|
*/
|
|
341
345
|
#pointers = new Map();
|
|
346
|
+
#ledger;
|
|
342
347
|
/** Each computer's extend in flight, by id: the next waits for it, so two never read the same old deadline. */
|
|
343
348
|
#extending = new Map();
|
|
344
|
-
|
|
349
|
+
/** Each agent's computer's attach or detach in flight, by id: the next waits for it, so two never overlap here. */
|
|
350
|
+
#owning = new Map();
|
|
351
|
+
constructor({ os, apiKey, workspaceId, apiUrl = ORGO_API_URL, fetch, maxLifetimeMs = MAX_LIFETIME_MS, timeouts = {}, daemonVersion = VERSION, ledger = orgoMemoryLedger() }) {
|
|
345
352
|
if (os !== "windows" && os !== "macos")
|
|
346
353
|
throw new Error(`os is "windows" or "macos" for an Orgo desktop Orgo's API answers, not ${JSON.stringify(os)}`);
|
|
347
354
|
if (typeof apiKey !== "string" || apiKey === "")
|
|
@@ -370,6 +377,7 @@ export class OrgoDesktopProvider {
|
|
|
370
377
|
this.#maxLifetimeMs = maxLifetimeMs;
|
|
371
378
|
this.#timeouts = merged;
|
|
372
379
|
this.#daemonVersion = daemonVersion;
|
|
380
|
+
this.#ledger = ledger;
|
|
373
381
|
}
|
|
374
382
|
/** A client of the computer, opened: its screen read, its hello this provider's own version, or a refusal in words. */
|
|
375
383
|
async #open(ref, signal) {
|
|
@@ -391,7 +399,9 @@ export class OrgoDesktopProvider {
|
|
|
391
399
|
/**
|
|
392
400
|
* daemon none: the provider answers every method over Orgo's API. lifetime janitor: the deadline is in the name, and
|
|
393
401
|
* on Windows Orgo's end (whole hours) is the backstop. network open alone, never read back from inside: Orgo has no
|
|
394
|
-
* egress control and no computerd runs here to probe from inside. release delete alone
|
|
402
|
+
* egress control and no computerd runs here to probe from inside. release delete. persistence on Windows alone: an
|
|
403
|
+
* agent's computer detaches to its disk (Orgo's stop keeps files, apps and settings, not memory), so it does not
|
|
404
|
+
* resume where it was. A Mac takes no owner: Orgo stops none, so it cannot be detached.
|
|
395
405
|
*/
|
|
396
406
|
capabilities() {
|
|
397
407
|
return {
|
|
@@ -405,6 +415,7 @@ export class OrgoDesktopProvider {
|
|
|
405
415
|
groups: [...this.#profile.groups],
|
|
406
416
|
maxLifetimeMs: this.#maxLifetimeMs,
|
|
407
417
|
release: ["delete"],
|
|
418
|
+
...(this.#profile.os === "windows" ? { persistence: { keepsState: true, resume: false } } : {}),
|
|
408
419
|
};
|
|
409
420
|
}
|
|
410
421
|
check(spec) {
|
|
@@ -415,7 +426,7 @@ export class OrgoDesktopProvider {
|
|
|
415
426
|
const profile = this.#profile;
|
|
416
427
|
if (image !== profile.image)
|
|
417
428
|
refuse("image", `this Orgo provider makes ${profile.os} desktops: its image is ${JSON.stringify(profile.image)}`);
|
|
418
|
-
refusals.push(...checkTenure(given, this.capabilities(),
|
|
429
|
+
refusals.push(...checkTenure(given, this.capabilities(), `Orgo ${profile.os}`));
|
|
419
430
|
refusals.push(...checkGpu(given.gpu, this.capabilities()));
|
|
420
431
|
if (network !== "open")
|
|
421
432
|
refuse("network", NETWORK);
|
|
@@ -431,7 +442,7 @@ export class OrgoDesktopProvider {
|
|
|
431
442
|
}
|
|
432
443
|
refusals.push(...checkMount(mount, []));
|
|
433
444
|
for (const [field, value] of Object.entries(given)) {
|
|
434
|
-
if (SPEC_FIELDS.has(field) || field === "mount" || value === undefined)
|
|
445
|
+
if (SPEC_FIELDS.has(field) || field === "mount" || field === "owner" || value === undefined)
|
|
435
446
|
continue;
|
|
436
447
|
if (field === "user")
|
|
437
448
|
refuse(field, `an Orgo ${profile.os} computer's programs run as Orgo's own user; a spec cannot name another`);
|
|
@@ -451,11 +462,13 @@ export class OrgoDesktopProvider {
|
|
|
451
462
|
if (signal?.aborted)
|
|
452
463
|
return fail("aborted", "create was aborted before Orgo was called");
|
|
453
464
|
const profile = this.#profile;
|
|
454
|
-
|
|
465
|
+
// An agent's own computer outlives the run: no deadline, no Orgo end, the owner in its name and its labels.
|
|
466
|
+
const owner = spec.owner;
|
|
467
|
+
const labels = { ...spec.labels, ...(owner === undefined ? {} : ownerLabels(owner)) };
|
|
455
468
|
const now = Date.now();
|
|
456
|
-
const deadline = new Date(now + runLifetimeMs(spec)).toISOString();
|
|
457
|
-
const maxDeadline = new Date(now + this.#maxLifetimeMs).toISOString();
|
|
458
|
-
const name = orgoName(labels, randomBytes(6).toString("hex"), new Date(deadline));
|
|
469
|
+
const deadline = owner === undefined ? new Date(now + runLifetimeMs(spec)).toISOString() : NO_DEADLINE;
|
|
470
|
+
const maxDeadline = owner === undefined ? new Date(now + this.#maxLifetimeMs).toISOString() : NO_DEADLINE;
|
|
471
|
+
const name = orgoName(labels, randomBytes(6).toString("hex"), owner === undefined ? new Date(deadline) : undefined, owner);
|
|
459
472
|
if (name.length > NAME_MAX || nameParts(name) === undefined)
|
|
460
473
|
return fail("invalid", `the name ${name} is not one Orgo holds: at most ${NAME_MAX} of letters, digits and dashes`);
|
|
461
474
|
const ram = spec.memoryMb === undefined ? profile.ram[0] : spec.memoryMb / 1024;
|
|
@@ -467,7 +480,7 @@ export class OrgoDesktopProvider {
|
|
|
467
480
|
ram,
|
|
468
481
|
...(cpu === undefined ? {} : { cpu }),
|
|
469
482
|
// Orgo's end, the vendor's backstop where it has one: whole hours, deleting the computer. The janitor ends it on time.
|
|
470
|
-
...(profile.ends ? { ends_in_hours: Math.max(1, Math.ceil(runLifetimeMs(spec) / HOUR)) } : {}),
|
|
483
|
+
...(profile.ends && owner === undefined ? { ends_in_hours: Math.max(1, Math.ceil(runLifetimeMs(spec) / HOUR)) } : {}),
|
|
471
484
|
};
|
|
472
485
|
const made = await this.#rest.request("POST", "/computers", { body, timeoutMs: this.#timeouts.createMs, ...(signal ? { signal } : {}) });
|
|
473
486
|
if (!made.ok) {
|
|
@@ -480,6 +493,12 @@ export class OrgoDesktopProvider {
|
|
|
480
493
|
if (typeof id !== "string" || !COMPUTER_ID.test(id))
|
|
481
494
|
return fail("vendor", `Orgo's create answered no computer id${await this.#deleteNamed(name)}`);
|
|
482
495
|
const ref = { id, name };
|
|
496
|
+
// An agent's computer's labels go in the ledger at once, so list finds it as the agent's whatever happens next.
|
|
497
|
+
if (owner !== undefined) {
|
|
498
|
+
const kept = await this.#keep({ id, labels });
|
|
499
|
+
if (!kept.ok)
|
|
500
|
+
return this.#abandon(ref, kept.error);
|
|
501
|
+
}
|
|
483
502
|
const file = { name, labels, deadline, maxDeadline, image: profile.image, ...(cpu === undefined ? {} : { cpus: cpu }), memoryMb: ram * 1024 };
|
|
484
503
|
const prepared = await this.#prepare(ref, made.value.status, file, signal);
|
|
485
504
|
if (signal?.aborted)
|
|
@@ -601,6 +620,54 @@ export class OrgoDesktopProvider {
|
|
|
601
620
|
}
|
|
602
621
|
return `; the computer named ${name}: ${said.join("; ")}`;
|
|
603
622
|
}
|
|
623
|
+
/** An agent's computer's ledger entry, put; a ledger that fails is a failure in words. */
|
|
624
|
+
async #keep(entry) {
|
|
625
|
+
try {
|
|
626
|
+
await this.#ledger.put(entry);
|
|
627
|
+
return ok(undefined);
|
|
628
|
+
}
|
|
629
|
+
catch (error) {
|
|
630
|
+
return vendorFail("orgo", `the ledger of agents' computers could not be written: ${error instanceof Error ? error.message : String(error)}`);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
/** Every agent's computer's ledger entry, by id. */
|
|
634
|
+
async #entries() {
|
|
635
|
+
try {
|
|
636
|
+
return ok(new Map((await this.#ledger.list()).map((entry) => [entry.id, entry])));
|
|
637
|
+
}
|
|
638
|
+
catch (error) {
|
|
639
|
+
return vendorFail("orgo", `the ledger of agents' computers could not be read: ${error instanceof Error ? error.message : String(error)}`);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
/** A deleted computer's ledger entry, gone; best effort, since the computer is gone whatever the ledger says. */
|
|
643
|
+
async #forget(id) {
|
|
644
|
+
try {
|
|
645
|
+
await this.#ledger.remove(id);
|
|
646
|
+
}
|
|
647
|
+
catch {
|
|
648
|
+
// A stale entry names a computer Orgo no longer has: list never shows it.
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* An agent's computer deleted under `claim` (ReleaseContext): its entry taken first, only while it carries exactly the
|
|
653
|
+
* claim's run and epoch and no attach or detach holds it (takeEntry, only), so a claim that made it and lost it never
|
|
654
|
+
* deletes it from under a later claim. A delete Orgo refused lets the entry go again; one Orgo took forgot it already.
|
|
655
|
+
*/
|
|
656
|
+
#claimedDelete(ref, name, claim, signal) {
|
|
657
|
+
const owner = nameParts(name)?.owner;
|
|
658
|
+
if (owner === undefined)
|
|
659
|
+
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`));
|
|
660
|
+
return inTurn(this.#owning, ref.id, async () => {
|
|
661
|
+
const taken = await takeEntry(this.#ledger, ref, owner, claim, "being deleted", { only: true });
|
|
662
|
+
if (!taken.ok)
|
|
663
|
+
return taken;
|
|
664
|
+
const deleted = await this.#delete(ref, signal);
|
|
665
|
+
if (deleted.ok)
|
|
666
|
+
return deleted;
|
|
667
|
+
const left = await this.#entries();
|
|
668
|
+
return left.ok && !left.value.has(ref.id) ? deleted : andSettled(deleted, await settleEntry(this.#ledger, ref, taken.value));
|
|
669
|
+
});
|
|
670
|
+
}
|
|
604
671
|
/** DELETE, tried again while Orgo answers 409 computer_waking, then the wait until Orgo no longer has the computer. */
|
|
605
672
|
async #delete(ref, signal) {
|
|
606
673
|
const { requestMs, pollMs, goneMs } = this.#timeouts;
|
|
@@ -614,6 +681,7 @@ export class OrgoDesktopProvider {
|
|
|
614
681
|
return deleted;
|
|
615
682
|
await pause(pollMs);
|
|
616
683
|
}
|
|
684
|
+
await this.#forget(ref.id);
|
|
617
685
|
const until = Date.now() + goneMs;
|
|
618
686
|
let last = "unread";
|
|
619
687
|
for (;;) {
|
|
@@ -667,7 +735,7 @@ export class OrgoDesktopProvider {
|
|
|
667
735
|
const { client, hello } = opened.value;
|
|
668
736
|
return ok({ client, hello, close: async () => client.close() });
|
|
669
737
|
}
|
|
670
|
-
async attach(ref, { signal } = {}) {
|
|
738
|
+
async attach(ref, { signal, claim } = {}) {
|
|
671
739
|
const given = (typeof ref === "object" && ref !== null ? ref : {});
|
|
672
740
|
if ((given.id === undefined) === (given.labels === undefined))
|
|
673
741
|
return fail("invalid", "attach takes { id } or { labels }, one of them");
|
|
@@ -679,7 +747,7 @@ export class OrgoDesktopProvider {
|
|
|
679
747
|
return read.error.code === "not_found" ? fail("not_found", `no Orgo computer has the id ${given.id}`) : read;
|
|
680
748
|
if (!this.#ours(read.value))
|
|
681
749
|
return fail("not_found", `no Orgo ${this.#profile.os} computer of this provider has the id ${given.id}`);
|
|
682
|
-
return this.#attached(read.value, signal);
|
|
750
|
+
return this.#attached(read.value, signal, claim);
|
|
683
751
|
}
|
|
684
752
|
const problem = filterProblem(given.labels);
|
|
685
753
|
if (problem)
|
|
@@ -698,34 +766,112 @@ export class OrgoDesktopProvider {
|
|
|
698
766
|
const found = await this.#own(listed.value[0].ref, signal);
|
|
699
767
|
if (!found.ok)
|
|
700
768
|
return found;
|
|
701
|
-
return this.#attached(found.value, signal);
|
|
769
|
+
return this.#attached(found.value, signal, claim);
|
|
702
770
|
}
|
|
703
771
|
/**
|
|
704
772
|
* The check of a computer attach did not make: its labels file held to its name, who its programs run as, and the
|
|
705
|
-
* hello its screen answers. A computer that does not run is refused: reading it would wake it.
|
|
773
|
+
* hello its screen answers. A computer that does not run is refused: reading it would wake it. An agent's own computer
|
|
774
|
+
* is the exception: attaching it is asking for it. Its ledger entry is taken for the claim first, busy (takeEntry),
|
|
775
|
+
* before anything is done to the computer, so a claim of the same run below the epoch it carries is fenced however the
|
|
776
|
+
* two attaches interleave; one that was detached is then started (#wake); under a claim it carries the claim's run and
|
|
777
|
+
* epoch from then on; and it is marked held in the ledger last (settleEntry), by this attach alone, while the entry is
|
|
778
|
+
* still the one it holds. An attach that started it and then fails stops it again, as it was found (#putBack). This
|
|
779
|
+
* process's attaches and detaches of one agent's computer are taken in turn; another process's are refused while this
|
|
780
|
+
* one holds the entry, so no attach or detach stops another's computer under it.
|
|
781
|
+
*/
|
|
782
|
+
async #attached(record, signal, claim) {
|
|
783
|
+
const ref = { id: record.id, name: record.name };
|
|
784
|
+
const parts = nameParts(record.name);
|
|
785
|
+
if (parts.owner === undefined) {
|
|
786
|
+
if (claim !== undefined)
|
|
787
|
+
return fail("refused", claimProblem(ref.name, undefined, undefined, claim));
|
|
788
|
+
if (record.status !== "running")
|
|
789
|
+
return fail("refused", `${ref.name} is ${record.status}: its labels file and screen are read from inside, and a read would wake it`);
|
|
790
|
+
return this.#checked(record, parts, signal);
|
|
791
|
+
}
|
|
792
|
+
const owner = parts.owner;
|
|
793
|
+
return inTurn(this.#owning, ref.id, async (waited) => {
|
|
794
|
+
// Read again after waiting: the attach or detach before this one may have started or stopped it.
|
|
795
|
+
const now = waited ? await this.#own(ref, signal) : ok(record);
|
|
796
|
+
if (!now.ok)
|
|
797
|
+
return now;
|
|
798
|
+
const taken = await takeEntry(this.#ledger, ref, owner, claim, "being attached");
|
|
799
|
+
if (!taken.ok)
|
|
800
|
+
return taken;
|
|
801
|
+
const lease = taken.value;
|
|
802
|
+
const woke = now.value.status !== "running";
|
|
803
|
+
const attached = await this.#started(now.value, parts, lease, woke, signal, claim);
|
|
804
|
+
if (!attached.ok && woke)
|
|
805
|
+
return this.#putBack(ref, lease, attached.error);
|
|
806
|
+
// Marked held (no longer detached) once attached; a failed attach that woke nothing leaves it as it was.
|
|
807
|
+
return andSettled(attached, await settleEntry(this.#ledger, ref, lease, attached.ok ? ({ detachedAt: _held, ...held }) => held : undefined));
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
/**
|
|
811
|
+
* An agent's own computer, its entry taken: started when it was not running; under a claim, its labels file carries
|
|
812
|
+
* the claim's run and epoch from now on, as the ledger does; then checked.
|
|
813
|
+
*/
|
|
814
|
+
async #started(record, parts, lease, woke, signal, claim) {
|
|
815
|
+
const ref = { id: record.id, name: record.name };
|
|
816
|
+
if (woke) {
|
|
817
|
+
const woken = await this.#wake(ref, record.status, signal);
|
|
818
|
+
if (!woken.ok)
|
|
819
|
+
return woken;
|
|
820
|
+
}
|
|
821
|
+
if (claim !== undefined) {
|
|
822
|
+
// Its labels file is written under the claim only while this attach still holds the entry, however long the wake took.
|
|
823
|
+
if (woke) {
|
|
824
|
+
const renewed = await renewEntry(this.#ledger, ref, lease, "being attached");
|
|
825
|
+
if (!renewed.ok)
|
|
826
|
+
return renewed;
|
|
827
|
+
}
|
|
828
|
+
const read = await this.#readLabels(ref, signal);
|
|
829
|
+
if (!read.ok)
|
|
830
|
+
return read;
|
|
831
|
+
const written = await this.#writeLabels(ref, { ...read.value, labels: lease.entry.labels }, signal);
|
|
832
|
+
if (!written.ok)
|
|
833
|
+
return written;
|
|
834
|
+
}
|
|
835
|
+
return this.#checked(record, parts, signal);
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* An agent's own computer this attach started, stopped again once the attach fails, best effort, as release detach
|
|
839
|
+
* stops it, so it is left as it was found; its ledger entry still says when it was detached (takeEntry kept it). One
|
|
840
|
+
* another claim has taken since is that claim's, and runs on. Never signalled: it outlives an abort.
|
|
706
841
|
*/
|
|
707
|
-
async #
|
|
842
|
+
async #putBack(ref, lease, error) {
|
|
843
|
+
const renewed = await renewEntry(this.#ledger, ref, lease, "being put back to sleep by an attach that failed");
|
|
844
|
+
if (!renewed.ok)
|
|
845
|
+
return fail(error.code, `${error.message}; ${ref.name} runs on, not stopped again: ${renewed.error.message}`);
|
|
846
|
+
const stopped = await this.#stop(ref);
|
|
847
|
+
const failed = fail(error.code, `${error.message}; ${ref.name}, which this attach started, ${stopped.ok ? "was stopped again, as it was found" : `could not be stopped again (${stopped.error.message})`}`);
|
|
848
|
+
return andSettled(failed, await settleEntry(this.#ledger, ref, lease));
|
|
849
|
+
}
|
|
850
|
+
/** The labels file held to the name, who its programs run as, and the hello its screen answers: #attached's check. */
|
|
851
|
+
async #checked(record, parts, signal) {
|
|
708
852
|
const ref = { id: record.id, name: record.name };
|
|
709
|
-
if (record.status !== "running")
|
|
710
|
-
return fail("refused", `${ref.name} is ${record.status}: its labels file and screen are read from inside, and a read would wake it`);
|
|
711
853
|
const file = await this.#readLabels(ref, signal);
|
|
712
854
|
if (!file.ok)
|
|
713
855
|
return file;
|
|
714
856
|
const found = [];
|
|
715
|
-
const parts = nameParts(record.name);
|
|
716
857
|
const { labels } = file.value;
|
|
717
858
|
if (typeof file.value.name !== "string" || !sameComputer(file.value.name, record.name))
|
|
718
859
|
found.push(`name: Orgo's is ${record.name}, the labels file's ${show(file.value.name)}`);
|
|
860
|
+
if ((parts.owner ?? "") !== ownerHashOfLabels(labels))
|
|
861
|
+
found.push(`owner: the name's owner hash ${show(parts.owner)} is not the labels file's owner ${show(labels[OWNER_LABEL])}`);
|
|
719
862
|
const run = labels[RUN_LABEL];
|
|
720
|
-
|
|
863
|
+
// An agent's own computer is no run's: its name carries no run or epoch, whatever run made it.
|
|
864
|
+
if (parts.owner === undefined && (parts.run ?? "") !== (typeof run === "string" && run !== "" ? runHash(run) : ""))
|
|
721
865
|
found.push(`run: the name's run hash ${show(parts.run)} is not the labels file's run ${show(run)}`);
|
|
722
|
-
if ((parts.epoch ?? "") !== (labels[EPOCH_LABEL] ?? ""))
|
|
866
|
+
if (parts.owner === undefined && (parts.epoch ?? "") !== (labels[EPOCH_LABEL] ?? ""))
|
|
723
867
|
found.push(`epoch: the name says ${show(parts.epoch)}, the labels file ${show(labels[EPOCH_LABEL])}`);
|
|
724
868
|
if (file.value.image !== this.#profile.image)
|
|
725
869
|
found.push(`image: the labels file says ${show(file.value.image)}, not ${this.#profile.image}`);
|
|
726
|
-
const deadline = new Date(file.value.deadline);
|
|
727
|
-
if (Number.isNaN(deadline.getTime()))
|
|
870
|
+
const deadline = parts.owner === undefined ? new Date(file.value.deadline) : undefined;
|
|
871
|
+
if (deadline !== undefined && Number.isNaN(deadline.getTime()))
|
|
728
872
|
found.push(`deadline: the labels file reads ${show(file.value.deadline)}, not a time`);
|
|
873
|
+
if (parts.owner !== undefined && file.value.deadline !== NO_DEADLINE)
|
|
874
|
+
found.push(`deadline: an agent's own computer has none, and the labels file reads ${show(file.value.deadline)}`);
|
|
729
875
|
if (found.length > 0)
|
|
730
876
|
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("; ")}`);
|
|
731
877
|
const user = await this.#shell(ref.id, userCommand(this.#profile), this.#timeouts.execMs, signal);
|
|
@@ -746,11 +892,44 @@ export class OrgoDesktopProvider {
|
|
|
746
892
|
network: "open",
|
|
747
893
|
...(file.value.cpus === undefined ? {} : { cpus: file.value.cpus }),
|
|
748
894
|
...(file.value.memoryMb === undefined ? {} : { memoryMb: file.value.memoryMb }),
|
|
749
|
-
deadline,
|
|
895
|
+
...(deadline === undefined ? {} : { deadline }),
|
|
750
896
|
kind: "desktop",
|
|
897
|
+
...ownerReadBack(labels),
|
|
751
898
|
},
|
|
752
899
|
});
|
|
753
900
|
}
|
|
901
|
+
/**
|
|
902
|
+
* An agent's own computer that a detach stopped, started again for its attach: Orgo's start (a Windows computer comes
|
|
903
|
+
* back from its disk), then running, then taking a command. A status no start brings back is refused in words.
|
|
904
|
+
*/
|
|
905
|
+
async #wake(ref, status, signal) {
|
|
906
|
+
if (status === "frozen" || status === "stopped") {
|
|
907
|
+
const started = await this.#rest.request("POST", `/computers/${encodeURIComponent(ref.id)}/start`, { body: {}, timeoutMs: this.#timeouts.createMs, ...(signal ? { signal } : {}) });
|
|
908
|
+
if (!started.ok)
|
|
909
|
+
return fail(started.error.code, `${ref.name} could not be started for its attach: ${started.error.message}`);
|
|
910
|
+
}
|
|
911
|
+
else if (!RUNNING.has(status)) {
|
|
912
|
+
return fail("refused", `${ref.name} is ${status}: no start brings it back`);
|
|
913
|
+
}
|
|
914
|
+
const running = await this.#untilRunning(ref, signal);
|
|
915
|
+
if (!running.ok)
|
|
916
|
+
return running;
|
|
917
|
+
return this.#untilAnswering(ref, signal);
|
|
918
|
+
}
|
|
919
|
+
/** Orgo's bash call taking a command, tried within createMs: a computer Orgo reads running may still be booting. */
|
|
920
|
+
async #untilAnswering(ref, signal) {
|
|
921
|
+
const until = Date.now() + this.#timeouts.createMs;
|
|
922
|
+
for (;;) {
|
|
923
|
+
const ran = await this.#shell(ref.id, "exit 0", this.#timeouts.execMs, signal);
|
|
924
|
+
if (ran.ok)
|
|
925
|
+
return ok(undefined);
|
|
926
|
+
if (signal?.aborted)
|
|
927
|
+
return fail("aborted", `the wait for ${ref.name} to take a command was aborted`);
|
|
928
|
+
if (Date.now() >= until)
|
|
929
|
+
return fail(ran.error.code, `${ref.name} did not take a command within ${seconds(this.#timeouts.createMs)}: ${ran.error.message}`);
|
|
930
|
+
await pause(this.#timeouts.pollMs, signal);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
754
933
|
/**
|
|
755
934
|
* Every computer of this provider in the workspace that carries all of these labels. The run and the epoch are read
|
|
756
935
|
* from the name, with nothing run in a computer, so a stopped one is listed too; any other label is read from the
|
|
@@ -764,16 +943,30 @@ export class OrgoDesktopProvider {
|
|
|
764
943
|
if (!listed.ok)
|
|
765
944
|
return listed;
|
|
766
945
|
const others = Object.keys(labels).filter((key) => key !== RUN_LABEL && key !== EPOCH_LABEL);
|
|
946
|
+
const ours = listed.value.filter((record) => nameParts(record.name) !== undefined && this.#ours(record));
|
|
947
|
+
// An agent's computers' labels are the ledger's: read once, only when the workspace holds one.
|
|
948
|
+
const owned = ours.some((record) => nameParts(record.name)?.owner !== undefined) ? await this.#entries() : ok(new Map());
|
|
949
|
+
if (!owned.ok)
|
|
950
|
+
return owned;
|
|
767
951
|
const infos = [];
|
|
768
|
-
for (const record of
|
|
952
|
+
for (const record of ours) {
|
|
769
953
|
const parts = nameParts(record.name);
|
|
770
|
-
|
|
954
|
+
const ref = { id: record.id, name: record.name };
|
|
955
|
+
const state = RUNNING.has(record.status) ? "running" : "stopped";
|
|
956
|
+
// An agent's computer: every label from its ledger entry, with nothing run inside, whatever its state; no deadline.
|
|
957
|
+
if (parts.owner !== undefined) {
|
|
958
|
+
const view = ownedListing(owned.value.get(record.id), labels);
|
|
959
|
+
if (view === undefined)
|
|
960
|
+
continue;
|
|
961
|
+
infos.push({ ref, deadline: undefined, state, network: "open", ...view });
|
|
962
|
+
continue;
|
|
963
|
+
}
|
|
964
|
+
if (labels[OWNER_LABEL] !== undefined || labels[OWNER_SERVER_LABEL] !== undefined)
|
|
771
965
|
continue;
|
|
772
966
|
if (labels[RUN_LABEL] !== undefined && parts.run !== runHash(labels[RUN_LABEL]))
|
|
773
967
|
continue;
|
|
774
968
|
if (labels[EPOCH_LABEL] !== undefined && parts.epoch !== labels[EPOCH_LABEL])
|
|
775
969
|
continue;
|
|
776
|
-
const ref = { id: record.id, name: record.name };
|
|
777
970
|
// The name keeps the run's hash, not the run: listed with no run to match, the hash is given, as the Linux
|
|
778
971
|
// desktop's list gives it, so a caller (serve's janitor) can still tell whose run a computer is.
|
|
779
972
|
let read = {
|
|
@@ -790,7 +983,7 @@ export class OrgoDesktopProvider {
|
|
|
790
983
|
continue;
|
|
791
984
|
read = { ...file.value.labels };
|
|
792
985
|
}
|
|
793
|
-
infos.push({ ref, labels: read, deadline, state
|
|
986
|
+
infos.push({ ref, labels: read, deadline, state, network: "open" });
|
|
794
987
|
}
|
|
795
988
|
return ok(infos);
|
|
796
989
|
}
|
|
@@ -810,26 +1003,73 @@ export class OrgoDesktopProvider {
|
|
|
810
1003
|
return found;
|
|
811
1004
|
return ok("open");
|
|
812
1005
|
}
|
|
813
|
-
/**
|
|
814
|
-
|
|
1006
|
+
/**
|
|
1007
|
+
* delete: idempotent, a computer Orgo no longer has is done; it waits until Orgo no longer has it. detach, for an
|
|
1008
|
+
* agent's own Windows computer alone: Orgo's stop archives its disk, and it reads frozen. A Mac has no agent's
|
|
1009
|
+
* computer to detach (Orgo's stop takes none). No stop: a run's computer is deleted. Either forgets the pointer.
|
|
1010
|
+
*/
|
|
1011
|
+
async release(ref, how, { signal, claim } = {}) {
|
|
815
1012
|
const problem = refProblem(ref);
|
|
816
1013
|
if (problem)
|
|
817
1014
|
return fail("invalid", problem);
|
|
818
|
-
if (how !== "stop" && how !== "delete")
|
|
819
|
-
return fail("invalid", `release is "stop" or "
|
|
1015
|
+
if (how !== "stop" && how !== "delete" && how !== "detach")
|
|
1016
|
+
return fail("invalid", `release is "stop", "delete" or "detach", not ${String(how)}`);
|
|
820
1017
|
if (how === "stop")
|
|
821
|
-
return fail("unsupported", "an Orgo computer has no stop that this provider declares: release it with delete");
|
|
1018
|
+
return fail("unsupported", "an Orgo computer has no stop that this provider declares: release it with delete, or detach an agent's own");
|
|
822
1019
|
const read = await this.#get(ref.id);
|
|
823
1020
|
if (!read.ok)
|
|
824
|
-
return read.error.code === "not_found" ? ok(undefined) : read;
|
|
1021
|
+
return read.error.code === "not_found" && how === "delete" ? ok(undefined) : read;
|
|
825
1022
|
if (read.value.status === "deleted")
|
|
826
|
-
return ok(undefined);
|
|
1023
|
+
return how === "delete" ? ok(undefined) : fail("not_found", `${ref.name} is deleted: there is nothing to detach`);
|
|
827
1024
|
if (!this.#ours(read.value, ref.name))
|
|
828
1025
|
return fail("not_found", `no Orgo ${this.#profile.os} computer of this provider is ${ref.name} (${ref.id}), so it is left as it is`);
|
|
829
|
-
|
|
830
|
-
|
|
1026
|
+
if (how === "delete")
|
|
1027
|
+
return this.#dropPointer(ref, await (claim === undefined ? this.#delete(ref, signal) : this.#claimedDelete(ref, read.value.name, claim, signal)));
|
|
1028
|
+
const owner = nameParts(read.value.name)?.owner;
|
|
1029
|
+
if (owner === undefined)
|
|
1030
|
+
return fail("refused", `${ref.name} is a run's computer, not an agent's own: it is deleted at its release, never detached`);
|
|
1031
|
+
return this.#dropPointer(ref, await this.#detach(ref, owner, read.value.status, signal));
|
|
1032
|
+
}
|
|
1033
|
+
/** A released computer's pointer is no longer where any client moved it: the next client starts with none. */
|
|
1034
|
+
#dropPointer(ref, released) {
|
|
1035
|
+
if (released.ok)
|
|
831
1036
|
this.#pointers.delete(ref.id);
|
|
832
|
-
return
|
|
1037
|
+
return released;
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* An agent's own Windows computer kept with its disk: stopped, read back frozen, and marked detached in the ledger. In
|
|
1041
|
+
* turn with this process's attaches, and holding the ledger entry from before the stop until it is marked detached
|
|
1042
|
+
* (takeEntry, settleEntry), so no attach starts it meanwhile and an attach under way refuses this detach. `status`:
|
|
1043
|
+
* Orgo's, read before the turn; `owner`: the name's owner hash.
|
|
1044
|
+
*/
|
|
1045
|
+
async #detach(ref, owner, status, signal) {
|
|
1046
|
+
return inTurn(this.#owning, ref.id, async (waited) => {
|
|
1047
|
+
const taken = await takeEntry(this.#ledger, ref, owner, undefined, "being detached");
|
|
1048
|
+
if (!taken.ok)
|
|
1049
|
+
return taken;
|
|
1050
|
+
const now = waited ? await this.#own(ref, signal) : ok({ status });
|
|
1051
|
+
const stopped = !now.ok ? now : now.value.status === "frozen" ? ok(undefined) : await this.#stop(ref, signal);
|
|
1052
|
+
return andSettled(stopped, await settleEntry(this.#ledger, ref, taken.value, stopped.ok ? (entry) => ({ ...entry, detachedAt: entry.detachedAt ?? new Date().toISOString() }) : undefined));
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
/** Orgo's stop, the computer's disk archived, then the wait until Orgo reads it frozen. */
|
|
1056
|
+
async #stop(ref, signal) {
|
|
1057
|
+
const stopped = await this.#rest.request("POST", `/computers/${encodeURIComponent(ref.id)}/stop`, { timeoutMs: this.#timeouts.createMs, ...(signal ? { signal } : {}) });
|
|
1058
|
+
if (!stopped.ok)
|
|
1059
|
+
return fail(stopped.error.code, `${ref.name} could not be stopped for its agent: ${stopped.error.message}`);
|
|
1060
|
+
const until = Date.now() + this.#timeouts.goneMs;
|
|
1061
|
+
for (;;) {
|
|
1062
|
+
const read = await this.#get(ref.id, signal);
|
|
1063
|
+
if (!read.ok)
|
|
1064
|
+
return read;
|
|
1065
|
+
if (read.value.status === "frozen")
|
|
1066
|
+
return ok(undefined);
|
|
1067
|
+
if (Date.now() >= until)
|
|
1068
|
+
return fail("timeout", `${ref.name} was not stopped ${seconds(this.#timeouts.goneMs)} after its stop; Orgo last read ${read.value.status}`);
|
|
1069
|
+
await pause(this.#timeouts.pollMs, signal);
|
|
1070
|
+
if (signal?.aborted)
|
|
1071
|
+
return fail("aborted", `the wait for ${ref.name} to stop was aborted`);
|
|
1072
|
+
}
|
|
833
1073
|
}
|
|
834
1074
|
/**
|
|
835
1075
|
* Moves the deadline to now + ttlMs, never earlier and never past the most the labels file holds: Orgo's end first
|
|
@@ -837,16 +1077,7 @@ export class OrgoDesktopProvider {
|
|
|
837
1077
|
* extends are taken in turn.
|
|
838
1078
|
*/
|
|
839
1079
|
extend(ref, ttlMs, context = {}) {
|
|
840
|
-
|
|
841
|
-
const before = this.#extending.get(key) ?? Promise.resolve();
|
|
842
|
-
const turn = before.then(() => this.#extend(ref, ttlMs, context));
|
|
843
|
-
const settled = turn.then(() => undefined, () => undefined);
|
|
844
|
-
this.#extending.set(key, settled);
|
|
845
|
-
void settled.then(() => {
|
|
846
|
-
if (this.#extending.get(key) === settled)
|
|
847
|
-
this.#extending.delete(key);
|
|
848
|
-
});
|
|
849
|
-
return turn;
|
|
1080
|
+
return inTurn(this.#extending, typeof ref?.id === "string" ? ref.id : "", () => this.#extend(ref, ttlMs, context));
|
|
850
1081
|
}
|
|
851
1082
|
async #extend(ref, ttlMs, { signal } = {}) {
|
|
852
1083
|
const problem = refProblem(ref);
|
|
@@ -857,6 +1088,8 @@ export class OrgoDesktopProvider {
|
|
|
857
1088
|
const found = await this.#own(ref, signal);
|
|
858
1089
|
if (!found.ok)
|
|
859
1090
|
return found;
|
|
1091
|
+
if (nameParts(found.value.name)?.owner !== undefined)
|
|
1092
|
+
return fail("refused", `${ref.name} is an agent's own computer: it has no deadline to move`);
|
|
860
1093
|
if (found.value.status !== "running")
|
|
861
1094
|
return fail("refused", `${ref.name} is ${found.value.status}, not running`);
|
|
862
1095
|
const file = await this.#readLabels(ref, signal);
|
|
@@ -1105,7 +1338,10 @@ export class OrgoDesktopClient {
|
|
|
1105
1338
|
rgba.set([red, green, blue, 255], (y * width + x) * 4);
|
|
1106
1339
|
}
|
|
1107
1340
|
}
|
|
1108
|
-
bytes = encodePng(rgba, width, rect);
|
|
1341
|
+
bytes = await encodePng(rgba, width, rect);
|
|
1342
|
+
// close() may have run while the region was deflating: hold nothing for a closed connection.
|
|
1343
|
+
if (this.#closed)
|
|
1344
|
+
return fail("gone", "the connection to the computer is closed");
|
|
1109
1345
|
}
|
|
1110
1346
|
const id = randomBytes(6).toString("hex");
|
|
1111
1347
|
this.#held = { id, bytes };
|
|
@@ -1565,8 +1801,14 @@ export class OrgoDesktopClient {
|
|
|
1565
1801
|
}
|
|
1566
1802
|
const script = FILES_SCRIPT.replace("@@REQUEST@@", Buffer.from(JSON.stringify(request), "utf8").toString("base64"));
|
|
1567
1803
|
const ran = await bashCall(this.rest, this.computer, script, 120_000, signal);
|
|
1568
|
-
if (!ran.ok)
|
|
1569
|
-
|
|
1804
|
+
if (!ran.ok || ran.value.timedOut) {
|
|
1805
|
+
// The script that removes an uploaded copy did not run (or was cut off, or hit Orgo's time limit): it is removed here, once, unbounded by the
|
|
1806
|
+
// call's own signal, which may be the abort itself.
|
|
1807
|
+
if (typeof request.staged === "string")
|
|
1808
|
+
await bashCall(this.rest, this.computer, `Remove-Item -LiteralPath ${psQuote(request.staged)} -Force -ErrorAction SilentlyContinue`, 30_000).catch(() => undefined);
|
|
1809
|
+
if (!ran.ok)
|
|
1810
|
+
return signal.aborted ? refuse("aborted", `files.${op} was aborted`) : ran;
|
|
1811
|
+
}
|
|
1570
1812
|
let answer;
|
|
1571
1813
|
try {
|
|
1572
1814
|
answer = JSON.parse(ran.value.stdout.trim() || "null");
|