@parcha/agentrun-computer 0.1.0-beta.35 → 0.1.0-beta.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +2 -0
- package/README.md +423 -323
- package/box/Dockerfile +3 -1
- package/box/agentrun-archil +59 -7
- package/box/build-agent37.mjs +177 -0
- package/box/build-daytona.py +20 -4
- package/dist/agent37.d.ts +165 -0
- package/dist/agent37.js +759 -0
- package/dist/attach.d.ts +146 -0
- package/dist/attach.js +469 -0
- package/dist/browser.d.ts +61 -10
- package/dist/browser.js +125 -3
- package/dist/browserbase.d.ts +46 -23
- package/dist/browserbase.js +162 -17
- package/dist/chrome.d.ts +3 -4
- package/dist/chrome.js +6 -11
- package/dist/client.d.ts +33 -1
- package/dist/client.js +53 -0
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/conformance/agent37.d.ts +96 -0
- package/dist/conformance/agent37.js +154 -0
- package/dist/conformance/cases/apps.d.ts +5 -0
- package/dist/conformance/cases/apps.js +160 -73
- package/dist/conformance/cases/health.js +14 -0
- package/dist/conformance/cases/index.js +2 -0
- package/dist/conformance/cases/provider.js +27 -1
- package/dist/conformance/cases/screen.d.ts +3 -0
- package/dist/conformance/cases/screen.js +18 -0
- package/dist/conformance/cases/settings.d.ts +29 -23
- package/dist/conformance/cases/settings.js +76 -58
- package/dist/conformance/cases/touch.js +19 -17
- package/dist/conformance/cases/ui.d.ts +2 -0
- package/dist/conformance/cases/ui.js +475 -0
- package/dist/conformance/cli.d.ts +9 -0
- package/dist/conformance/cli.js +28 -3
- package/dist/conformance/daytona.d.ts +4 -0
- package/dist/conformance/daytona.js +5 -0
- package/dist/conformance/e2b.d.ts +6 -0
- package/dist/conformance/e2b.js +9 -2
- package/dist/conformance/fixture.d.ts +1 -1
- package/dist/conformance/fixture.js +25 -1
- package/dist/conformance/modal.d.ts +17 -13
- package/dist/conformance/modal.js +18 -9
- package/dist/conformance/orgo.d.ts +3 -3
- package/dist/conformance/orgo.js +6 -8
- package/dist/conformance/phone-harness.d.ts +6 -2
- package/dist/conformance/phone-harness.js +21 -4
- package/dist/conformance/report.js +1 -1
- package/dist/conformance/runner.js +23 -4
- package/dist/conformance/sweep.d.ts +50 -0
- package/dist/conformance/sweep.js +187 -0
- package/dist/conformance/types.d.ts +11 -3
- package/dist/conformance/ui-fixture.d.ts +56 -0
- package/dist/conformance/ui-fixture.js +43 -0
- package/dist/create-requests.d.ts +51 -0
- package/dist/create-requests.js +121 -0
- package/dist/daytona.d.ts +15 -5
- package/dist/daytona.js +58 -38
- package/dist/descriptor.d.ts +29 -2
- package/dist/descriptor.js +13 -0
- package/dist/descriptors/agent37.d.ts +41 -0
- package/dist/descriptors/agent37.js +28 -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 +66 -3
- package/dist/descriptors/index.js +5 -3
- 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 +13 -1
- package/dist/descriptors/orgo.js +9 -3
- package/dist/descriptors/phone-harness.d.ts +6 -1
- package/dist/descriptors/phone-harness.js +5 -2
- package/dist/docker/egress.d.ts +4 -3
- package/dist/docker/egress.js +7 -6
- package/dist/docker.d.ts +7 -4
- package/dist/docker.js +278 -34
- package/dist/e2b.d.ts +9 -165
- package/dist/e2b.js +282 -1342
- package/dist/fake-ui-driver.d.ts +97 -0
- package/dist/fake-ui-driver.js +717 -0
- package/dist/host.d.ts +182 -0
- package/dist/host.js +1 -0
- package/dist/hosted/process.d.ts +67 -0
- package/dist/hosted/process.js +228 -0
- package/dist/hosted/provider.d.ts +146 -0
- package/dist/hosted/provider.js +1364 -0
- package/dist/hosted/scripts.d.ts +76 -0
- package/dist/{child.js → hosted/scripts.js} +93 -12
- package/dist/index.d.ts +11 -7
- package/dist/index.js +10 -7
- package/dist/janitor.d.ts +5 -4
- package/dist/janitor.js +1 -1
- package/dist/kernel.d.ts +17 -27
- package/dist/kernel.js +94 -18
- package/dist/kinds.d.ts +17 -5
- package/dist/kinds.js +20 -4
- package/dist/labels.d.ts +10 -2
- package/dist/labels.js +30 -7
- package/dist/local-chrome.d.ts +5 -5
- package/dist/local-chrome.js +10 -5
- package/dist/modal.d.ts +23 -181
- package/dist/modal.js +273 -1232
- package/dist/mount-archil.d.ts +25 -3
- package/dist/mount-archil.js +39 -8
- package/dist/orgo-channel.d.ts +3 -2
- package/dist/orgo-channel.js +5 -2
- package/dist/orgo-desktop.d.ts +23 -9
- package/dist/orgo-desktop.js +328 -66
- package/dist/orgo-names.d.ts +129 -3
- package/dist/orgo-names.js +211 -8
- package/dist/orgo-workspaces.d.ts +47 -0
- package/dist/orgo-workspaces.js +94 -0
- package/dist/orgo.d.ts +27 -10
- package/dist/orgo.js +411 -81
- package/dist/phone-harness.d.ts +31 -21
- package/dist/phone-harness.js +510 -681
- package/dist/png.d.ts +4 -0
- package/dist/png.js +27 -0
- package/dist/protocol.d.ts +338 -30
- package/dist/protocol.js +96 -3
- package/dist/provider.d.ts +97 -7
- package/dist/provider.js +24 -3
- package/dist/result.d.ts +1 -1
- package/dist/surface/api.d.ts +248 -0
- package/dist/surface/api.js +1 -0
- package/dist/surface/core.d.ts +42 -0
- package/dist/surface/core.js +393 -0
- package/dist/surface/index.d.ts +5 -0
- package/dist/surface/index.js +3 -0
- package/dist/surface/outline.d.ts +8 -0
- package/dist/surface/outline.js +50 -0
- package/dist/surface/script.d.ts +10 -0
- package/dist/surface/script.js +92 -0
- package/dist/ui-engine.d.ts +114 -0
- package/dist/ui-engine.js +798 -0
- package/package.json +17 -1
- package/dist/child.d.ts +0 -42
- package/dist/conformance/e2b-diagnose.d.ts +0 -16
- package/dist/conformance/e2b-diagnose.js +0 -61
- package/dist/conformance/e2b-sweep.d.ts +0 -18
- package/dist/conformance/e2b-sweep.js +0 -132
- package/dist/conformance/modal-sweep.d.ts +0 -22
- package/dist/conformance/modal-sweep.js +0 -213
package/dist/orgo-desktop.js
CHANGED
|
@@ -33,14 +33,15 @@ 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 {
|
|
40
|
-
import {
|
|
39
|
+
import { workspaceChoices, workspaceHolder } from "./orgo-workspaces.js";
|
|
40
|
+
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";
|
|
41
|
+
import { encodePng } from "./png.js";
|
|
41
42
|
import { MAX_CHUNK_BYTES, MAX_LIST_ENTRIES, MAX_OUTPUT_BYTES, PROTOCOL_VERSION, } from "./protocol.js";
|
|
42
43
|
import { checkGpu, checkTenure, runLifetimeMs, } from "./provider.js";
|
|
43
|
-
import { failFor, ok } from "./result.js";
|
|
44
|
+
import { failFor, ok, vendorFail } from "./result.js";
|
|
44
45
|
import { daemonWatch } from "./screen-watch.js";
|
|
45
46
|
import { listTested } from "./test-connection.js";
|
|
46
47
|
/** This module's fail: a vendor failure names Orgo (CP-1's one vendor code). */
|
|
@@ -153,7 +154,8 @@ const FILES_SCRIPT = [
|
|
|
153
154
|
"$ErrorActionPreference = 'Stop'",
|
|
154
155
|
"$p = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('@@REQUEST@@')) | ConvertFrom-Json",
|
|
155
156
|
"function Say($o) { [Console]::Out.Write(($o | ConvertTo-Json -Compress -Depth 6)) }",
|
|
156
|
-
"
|
|
157
|
+
"# A refusal leaves nothing of the request behind: a write's uploaded copy goes with it.",
|
|
158
|
+
"function Refuse($kind, $words) { if ($p.staged) { Remove-Item -LiteralPath $p.staged -Force -ErrorAction SilentlyContinue }; Say @{ error = $kind; message = $words }; exit 2 }",
|
|
157
159
|
"function IsLink($i) { $i.LinkType -eq 'SymbolicLink' -or $i.LinkType -eq 'Junction' }",
|
|
158
160
|
"function StatOf($i) {",
|
|
159
161
|
" $t = if (IsLink $i) { 'symlink' } elseif ($i.PSIsContainer) { 'dir' } else { 'file' }",
|
|
@@ -282,6 +284,9 @@ const FILES_SCRIPT = [
|
|
|
282
284
|
" }",
|
|
283
285
|
"} catch {",
|
|
284
286
|
" $e = $_.Exception",
|
|
287
|
+
" # A .NET method's error (a file Open, WriteAllBytes) reaches here wrapped in PowerShell's MethodInvocationException:",
|
|
288
|
+
" # its code is the error inside.",
|
|
289
|
+
" while ($e.InnerException -and $e.GetType().Name -match 'MethodInvocation|TargetInvocation') { $e = $e.InnerException }",
|
|
285
290
|
" $kind = switch -regex ($e.GetType().Name) { 'NotFound' { 'not_found' } 'UnauthorizedAccess|Security' { 'denied' } 'PathTooLong' { 'limit' } default { 'invalid' } }",
|
|
286
291
|
" Refuse $kind $e.Message",
|
|
287
292
|
"}",
|
|
@@ -329,7 +334,7 @@ export class OrgoDesktopProvider {
|
|
|
329
334
|
id = "orgo";
|
|
330
335
|
version = VERSION;
|
|
331
336
|
#rest;
|
|
332
|
-
#
|
|
337
|
+
#workspace;
|
|
333
338
|
#profile;
|
|
334
339
|
#maxLifetimeMs;
|
|
335
340
|
#timeouts;
|
|
@@ -339,15 +344,18 @@ export class OrgoDesktopProvider {
|
|
|
339
344
|
* the run's input moves it, and the live view's own client (daemonWatch) draws the cursor there.
|
|
340
345
|
*/
|
|
341
346
|
#pointers = new Map();
|
|
347
|
+
#ledger;
|
|
342
348
|
/** Each computer's extend in flight, by id: the next waits for it, so two never read the same old deadline. */
|
|
343
349
|
#extending = new Map();
|
|
344
|
-
|
|
350
|
+
/** Each agent's computer's attach or detach in flight, by id: the next waits for it, so two never overlap here. */
|
|
351
|
+
#owning = new Map();
|
|
352
|
+
constructor({ os, apiKey, workspaceId, apiUrl = ORGO_API_URL, fetch, maxLifetimeMs = MAX_LIFETIME_MS, timeouts = {}, daemonVersion = VERSION, ledger = orgoMemoryLedger() }) {
|
|
345
353
|
if (os !== "windows" && os !== "macos")
|
|
346
354
|
throw new Error(`os is "windows" or "macos" for an Orgo desktop Orgo's API answers, not ${JSON.stringify(os)}`);
|
|
347
355
|
if (typeof apiKey !== "string" || apiKey === "")
|
|
348
356
|
throw new Error("options.apiKey, an Orgo API key, is required");
|
|
349
|
-
if (typeof workspaceId !== "string" || !WORKSPACE_ID.test(workspaceId))
|
|
350
|
-
throw new Error("options.workspaceId, the Orgo workspace this deployment's computers live in, is
|
|
357
|
+
if (workspaceId !== undefined && (typeof workspaceId !== "string" || !WORKSPACE_ID.test(workspaceId)))
|
|
358
|
+
throw new Error("options.workspaceId, the Orgo workspace this deployment's computers live in, is an Orgo workspace id when given");
|
|
351
359
|
let url;
|
|
352
360
|
try {
|
|
353
361
|
url = new URL(apiUrl);
|
|
@@ -365,18 +373,22 @@ export class OrgoDesktopProvider {
|
|
|
365
373
|
throw new Error(`timeouts.${key} must be a whole number of ms above 0`);
|
|
366
374
|
}
|
|
367
375
|
this.#rest = orgoRest({ apiKey, apiUrl: url.href, ...(fetch ? { fetch } : {}) });
|
|
368
|
-
this.#
|
|
376
|
+
this.#workspace = workspaceHolder(workspaceId, this.#rest, merged.requestMs);
|
|
369
377
|
this.#profile = PROFILES[os];
|
|
370
378
|
this.#maxLifetimeMs = maxLifetimeMs;
|
|
371
379
|
this.#timeouts = merged;
|
|
372
380
|
this.#daemonVersion = daemonVersion;
|
|
381
|
+
this.#ledger = ledger;
|
|
373
382
|
}
|
|
374
383
|
/** A client of the computer, opened: its screen read, its hello this provider's own version, or a refusal in words. */
|
|
375
384
|
async #open(ref, signal) {
|
|
376
385
|
let pointer = this.#pointers.get(ref.id);
|
|
377
386
|
if (pointer === undefined)
|
|
378
387
|
this.#pointers.set(ref.id, (pointer = {}));
|
|
379
|
-
const
|
|
388
|
+
const workspace = await this.#workspace.read(signal);
|
|
389
|
+
if (!workspace.ok)
|
|
390
|
+
return workspace;
|
|
391
|
+
const client = new OrgoDesktopClient(this.#rest, ref.id, this.#profile, workspace.value, this.#timeouts.requestMs, pointer);
|
|
380
392
|
const hello = await client.open(this.#daemonVersion, signal);
|
|
381
393
|
if (!hello.ok) {
|
|
382
394
|
client.close();
|
|
@@ -391,7 +403,9 @@ export class OrgoDesktopProvider {
|
|
|
391
403
|
/**
|
|
392
404
|
* daemon none: the provider answers every method over Orgo's API. lifetime janitor: the deadline is in the name, and
|
|
393
405
|
* 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
|
|
406
|
+
* egress control and no computerd runs here to probe from inside. release delete. persistence on Windows alone: an
|
|
407
|
+
* agent's computer detaches to its disk (Orgo's stop keeps files, apps and settings, not memory), so it does not
|
|
408
|
+
* resume where it was. A Mac takes no owner: Orgo stops none, so it cannot be detached.
|
|
395
409
|
*/
|
|
396
410
|
capabilities() {
|
|
397
411
|
return {
|
|
@@ -405,6 +419,7 @@ export class OrgoDesktopProvider {
|
|
|
405
419
|
groups: [...this.#profile.groups],
|
|
406
420
|
maxLifetimeMs: this.#maxLifetimeMs,
|
|
407
421
|
release: ["delete"],
|
|
422
|
+
...(this.#profile.os === "windows" ? { persistence: { keepsState: true, resume: false } } : {}),
|
|
408
423
|
};
|
|
409
424
|
}
|
|
410
425
|
check(spec) {
|
|
@@ -415,7 +430,7 @@ export class OrgoDesktopProvider {
|
|
|
415
430
|
const profile = this.#profile;
|
|
416
431
|
if (image !== profile.image)
|
|
417
432
|
refuse("image", `this Orgo provider makes ${profile.os} desktops: its image is ${JSON.stringify(profile.image)}`);
|
|
418
|
-
refusals.push(...checkTenure(given, this.capabilities(),
|
|
433
|
+
refusals.push(...checkTenure(given, this.capabilities(), `Orgo ${profile.os}`));
|
|
419
434
|
refusals.push(...checkGpu(given.gpu, this.capabilities()));
|
|
420
435
|
if (network !== "open")
|
|
421
436
|
refuse("network", NETWORK);
|
|
@@ -431,7 +446,7 @@ export class OrgoDesktopProvider {
|
|
|
431
446
|
}
|
|
432
447
|
refusals.push(...checkMount(mount, []));
|
|
433
448
|
for (const [field, value] of Object.entries(given)) {
|
|
434
|
-
if (SPEC_FIELDS.has(field) || field === "mount" || value === undefined)
|
|
449
|
+
if (SPEC_FIELDS.has(field) || field === "mount" || field === "owner" || value === undefined)
|
|
435
450
|
continue;
|
|
436
451
|
if (field === "user")
|
|
437
452
|
refuse(field, `an Orgo ${profile.os} computer's programs run as Orgo's own user; a spec cannot name another`);
|
|
@@ -444,30 +459,36 @@ export class OrgoDesktopProvider {
|
|
|
444
459
|
}
|
|
445
460
|
return refusals;
|
|
446
461
|
}
|
|
447
|
-
async create(spec,
|
|
462
|
+
async create(spec, context) {
|
|
463
|
+
const signal = context?.signal;
|
|
448
464
|
const refusals = this.check(spec);
|
|
449
465
|
if (refusals.length > 0)
|
|
450
466
|
return fail("refused", refusals.map(({ field, message }) => `${field}: ${message}`).join("; "));
|
|
451
467
|
if (signal?.aborted)
|
|
452
468
|
return fail("aborted", "create was aborted before Orgo was called");
|
|
469
|
+
const workspace = await this.#workspace.read(signal);
|
|
470
|
+
if (!workspace.ok)
|
|
471
|
+
return workspace;
|
|
453
472
|
const profile = this.#profile;
|
|
454
|
-
|
|
473
|
+
// An agent's own computer outlives the run: no deadline, no Orgo end, the owner in its name and its labels.
|
|
474
|
+
const owner = spec.owner;
|
|
475
|
+
const labels = { ...spec.labels, ...(owner === undefined ? {} : ownerLabels(owner)) };
|
|
455
476
|
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));
|
|
477
|
+
const deadline = owner === undefined ? new Date(now + runLifetimeMs(spec)).toISOString() : NO_DEADLINE;
|
|
478
|
+
const maxDeadline = owner === undefined ? new Date(now + this.#maxLifetimeMs).toISOString() : NO_DEADLINE;
|
|
479
|
+
const name = orgoName(labels, randomBytes(6).toString("hex"), owner === undefined ? new Date(deadline) : undefined, owner);
|
|
459
480
|
if (name.length > NAME_MAX || nameParts(name) === undefined)
|
|
460
481
|
return fail("invalid", `the name ${name} is not one Orgo holds: at most ${NAME_MAX} of letters, digits and dashes`);
|
|
461
482
|
const ram = spec.memoryMb === undefined ? profile.ram[0] : spec.memoryMb / 1024;
|
|
462
483
|
const cpu = profile.cpus ? (spec.cpus ?? CPU_FOR_RAM[ram]) : undefined;
|
|
463
484
|
const body = {
|
|
464
|
-
workspace_id:
|
|
485
|
+
workspace_id: workspace.value,
|
|
465
486
|
name,
|
|
466
487
|
os: profile.os,
|
|
467
488
|
ram,
|
|
468
489
|
...(cpu === undefined ? {} : { cpu }),
|
|
469
490
|
// 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)) } : {}),
|
|
491
|
+
...(profile.ends && owner === undefined ? { ends_in_hours: Math.max(1, Math.ceil(runLifetimeMs(spec) / HOUR)) } : {}),
|
|
471
492
|
};
|
|
472
493
|
const made = await this.#rest.request("POST", "/computers", { body, timeoutMs: this.#timeouts.createMs, ...(signal ? { signal } : {}) });
|
|
473
494
|
if (!made.ok) {
|
|
@@ -480,6 +501,12 @@ export class OrgoDesktopProvider {
|
|
|
480
501
|
if (typeof id !== "string" || !COMPUTER_ID.test(id))
|
|
481
502
|
return fail("vendor", `Orgo's create answered no computer id${await this.#deleteNamed(name)}`);
|
|
482
503
|
const ref = { id, name };
|
|
504
|
+
// An agent's computer's labels go in the ledger at once, so list finds it as the agent's whatever happens next.
|
|
505
|
+
if (owner !== undefined) {
|
|
506
|
+
const kept = await this.#keep({ id, labels });
|
|
507
|
+
if (!kept.ok)
|
|
508
|
+
return this.#abandon(ref, kept.error);
|
|
509
|
+
}
|
|
483
510
|
const file = { name, labels, deadline, maxDeadline, image: profile.image, ...(cpu === undefined ? {} : { cpus: cpu }), memoryMb: ram * 1024 };
|
|
484
511
|
const prepared = await this.#prepare(ref, made.value.status, file, signal);
|
|
485
512
|
if (signal?.aborted)
|
|
@@ -553,8 +580,11 @@ export class OrgoDesktopProvider {
|
|
|
553
580
|
return written;
|
|
554
581
|
return written.value.exitCode === 0 ? ok(undefined) : fail("refused", `writing ${this.#profile.labels} in ${ref.name} failed (exit ${written.value.exitCode})`);
|
|
555
582
|
}
|
|
556
|
-
/** Orgo's record of a computer; not_found once Orgo no longer has it. */
|
|
583
|
+
/** Orgo's record of a computer; not_found once Orgo no longer has it. The workspace is read first, so #ours knows it. */
|
|
557
584
|
async #get(id, signal) {
|
|
585
|
+
const workspace = await this.#workspace.read(signal);
|
|
586
|
+
if (!workspace.ok)
|
|
587
|
+
return workspace;
|
|
558
588
|
const read = await this.#rest.request("GET", `/computers/${encodeURIComponent(id)}`, { timeoutMs: this.#timeouts.requestMs, ...(signal ? { signal } : {}) });
|
|
559
589
|
if (!read.ok)
|
|
560
590
|
return read;
|
|
@@ -568,7 +598,8 @@ export class OrgoDesktopProvider {
|
|
|
568
598
|
*/
|
|
569
599
|
#ours(record, name) {
|
|
570
600
|
return (nameParts(record.name) !== undefined &&
|
|
571
|
-
|
|
601
|
+
this.#workspace.id !== undefined &&
|
|
602
|
+
(record.workspace_id ?? record.project_id) === this.#workspace.id &&
|
|
572
603
|
(name === undefined || sameComputer(record.name, name)) &&
|
|
573
604
|
record.status !== "deleted" &&
|
|
574
605
|
(record.os === undefined || record.os === this.#profile.os));
|
|
@@ -601,6 +632,54 @@ export class OrgoDesktopProvider {
|
|
|
601
632
|
}
|
|
602
633
|
return `; the computer named ${name}: ${said.join("; ")}`;
|
|
603
634
|
}
|
|
635
|
+
/** An agent's computer's ledger entry, put; a ledger that fails is a failure in words. */
|
|
636
|
+
async #keep(entry) {
|
|
637
|
+
try {
|
|
638
|
+
await this.#ledger.put(entry);
|
|
639
|
+
return ok(undefined);
|
|
640
|
+
}
|
|
641
|
+
catch (error) {
|
|
642
|
+
return vendorFail("orgo", `the ledger of agents' computers could not be written: ${error instanceof Error ? error.message : String(error)}`);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
/** Every agent's computer's ledger entry, by id. */
|
|
646
|
+
async #entries() {
|
|
647
|
+
try {
|
|
648
|
+
return ok(new Map((await this.#ledger.list()).map((entry) => [entry.id, entry])));
|
|
649
|
+
}
|
|
650
|
+
catch (error) {
|
|
651
|
+
return vendorFail("orgo", `the ledger of agents' computers could not be read: ${error instanceof Error ? error.message : String(error)}`);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
/** A deleted computer's ledger entry, gone; best effort, since the computer is gone whatever the ledger says. */
|
|
655
|
+
async #forget(id) {
|
|
656
|
+
try {
|
|
657
|
+
await this.#ledger.remove(id);
|
|
658
|
+
}
|
|
659
|
+
catch {
|
|
660
|
+
// A stale entry names a computer Orgo no longer has: list never shows it.
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* An agent's computer deleted under `claim` (ReleaseContext): its entry taken first, only while it carries exactly the
|
|
665
|
+
* claim's run and epoch and no attach or detach holds it (takeEntry, only), so a claim that made it and lost it never
|
|
666
|
+
* deletes it from under a later claim. A delete Orgo refused lets the entry go again; one Orgo took forgot it already.
|
|
667
|
+
*/
|
|
668
|
+
#claimedDelete(ref, name, claim, signal) {
|
|
669
|
+
const owner = nameParts(name)?.owner;
|
|
670
|
+
if (owner === undefined)
|
|
671
|
+
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`));
|
|
672
|
+
return inTurn(this.#owning, ref.id, async () => {
|
|
673
|
+
const taken = await takeEntry(this.#ledger, ref, owner, claim, "being deleted", { only: true });
|
|
674
|
+
if (!taken.ok)
|
|
675
|
+
return taken;
|
|
676
|
+
const deleted = await this.#delete(ref, signal);
|
|
677
|
+
if (deleted.ok)
|
|
678
|
+
return deleted;
|
|
679
|
+
const left = await this.#entries();
|
|
680
|
+
return left.ok && !left.value.has(ref.id) ? deleted : andSettled(deleted, await settleEntry(this.#ledger, ref, taken.value));
|
|
681
|
+
});
|
|
682
|
+
}
|
|
604
683
|
/** DELETE, tried again while Orgo answers 409 computer_waking, then the wait until Orgo no longer has the computer. */
|
|
605
684
|
async #delete(ref, signal) {
|
|
606
685
|
const { requestMs, pollMs, goneMs } = this.#timeouts;
|
|
@@ -614,6 +693,7 @@ export class OrgoDesktopProvider {
|
|
|
614
693
|
return deleted;
|
|
615
694
|
await pause(pollMs);
|
|
616
695
|
}
|
|
696
|
+
await this.#forget(ref.id);
|
|
617
697
|
const until = Date.now() + goneMs;
|
|
618
698
|
let last = "unread";
|
|
619
699
|
for (;;) {
|
|
@@ -634,13 +714,17 @@ export class OrgoDesktopProvider {
|
|
|
634
714
|
}
|
|
635
715
|
/** The workspace's computers, as Orgo lists them. */
|
|
636
716
|
async #desktops(signal) {
|
|
637
|
-
const
|
|
717
|
+
const workspace = await this.#workspace.read(signal);
|
|
718
|
+
if (!workspace.ok)
|
|
719
|
+
return workspace;
|
|
720
|
+
const id = workspace.value;
|
|
721
|
+
const read = await this.#rest.request("GET", `/workspaces/${encodeURIComponent(id)}`, { timeoutMs: this.#timeouts.listMs, ...(signal ? { signal } : {}) });
|
|
638
722
|
if (!read.ok)
|
|
639
723
|
return read;
|
|
640
724
|
const desktops = read.value?.desktops;
|
|
641
725
|
if (!Array.isArray(desktops))
|
|
642
|
-
return fail("vendor", `Orgo's workspace ${
|
|
643
|
-
return ok(desktops.filter((one) => typeof one?.id === "string" && typeof one.name === "string").map((one) => ({ ...one, workspace_id:
|
|
726
|
+
return fail("vendor", `Orgo's workspace ${id} lists no computers array`);
|
|
727
|
+
return ok(desktops.filter((one) => typeof one?.id === "string" && typeof one.name === "string").map((one) => ({ ...one, workspace_id: id })));
|
|
644
728
|
}
|
|
645
729
|
/** The live view: screen.watch on a connection of the watch's own (daemonWatch), the client's polled screenshots. */
|
|
646
730
|
watch(ref, handlers, options) {
|
|
@@ -667,7 +751,7 @@ export class OrgoDesktopProvider {
|
|
|
667
751
|
const { client, hello } = opened.value;
|
|
668
752
|
return ok({ client, hello, close: async () => client.close() });
|
|
669
753
|
}
|
|
670
|
-
async attach(ref, { signal } = {}) {
|
|
754
|
+
async attach(ref, { signal, claim } = {}) {
|
|
671
755
|
const given = (typeof ref === "object" && ref !== null ? ref : {});
|
|
672
756
|
if ((given.id === undefined) === (given.labels === undefined))
|
|
673
757
|
return fail("invalid", "attach takes { id } or { labels }, one of them");
|
|
@@ -679,7 +763,7 @@ export class OrgoDesktopProvider {
|
|
|
679
763
|
return read.error.code === "not_found" ? fail("not_found", `no Orgo computer has the id ${given.id}`) : read;
|
|
680
764
|
if (!this.#ours(read.value))
|
|
681
765
|
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);
|
|
766
|
+
return this.#attached(read.value, signal, claim);
|
|
683
767
|
}
|
|
684
768
|
const problem = filterProblem(given.labels);
|
|
685
769
|
if (problem)
|
|
@@ -698,34 +782,112 @@ export class OrgoDesktopProvider {
|
|
|
698
782
|
const found = await this.#own(listed.value[0].ref, signal);
|
|
699
783
|
if (!found.ok)
|
|
700
784
|
return found;
|
|
701
|
-
return this.#attached(found.value, signal);
|
|
785
|
+
return this.#attached(found.value, signal, claim);
|
|
702
786
|
}
|
|
703
787
|
/**
|
|
704
788
|
* 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.
|
|
789
|
+
* hello its screen answers. A computer that does not run is refused: reading it would wake it. An agent's own computer
|
|
790
|
+
* is the exception: attaching it is asking for it. Its ledger entry is taken for the claim first, busy (takeEntry),
|
|
791
|
+
* before anything is done to the computer, so a claim of the same run below the epoch it carries is fenced however the
|
|
792
|
+
* two attaches interleave; one that was detached is then started (#wake); under a claim it carries the claim's run and
|
|
793
|
+
* epoch from then on; and it is marked held in the ledger last (settleEntry), by this attach alone, while the entry is
|
|
794
|
+
* still the one it holds. An attach that started it and then fails stops it again, as it was found (#putBack). This
|
|
795
|
+
* process's attaches and detaches of one agent's computer are taken in turn; another process's are refused while this
|
|
796
|
+
* one holds the entry, so no attach or detach stops another's computer under it.
|
|
797
|
+
*/
|
|
798
|
+
async #attached(record, signal, claim) {
|
|
799
|
+
const ref = { id: record.id, name: record.name };
|
|
800
|
+
const parts = nameParts(record.name);
|
|
801
|
+
if (parts.owner === undefined) {
|
|
802
|
+
if (claim !== undefined)
|
|
803
|
+
return fail("refused", claimProblem(ref.name, undefined, undefined, claim));
|
|
804
|
+
if (record.status !== "running")
|
|
805
|
+
return fail("refused", `${ref.name} is ${record.status}: its labels file and screen are read from inside, and a read would wake it`);
|
|
806
|
+
return this.#checked(record, parts, signal);
|
|
807
|
+
}
|
|
808
|
+
const owner = parts.owner;
|
|
809
|
+
return inTurn(this.#owning, ref.id, async (waited) => {
|
|
810
|
+
// Read again after waiting: the attach or detach before this one may have started or stopped it.
|
|
811
|
+
const now = waited ? await this.#own(ref, signal) : ok(record);
|
|
812
|
+
if (!now.ok)
|
|
813
|
+
return now;
|
|
814
|
+
const taken = await takeEntry(this.#ledger, ref, owner, claim, "being attached");
|
|
815
|
+
if (!taken.ok)
|
|
816
|
+
return taken;
|
|
817
|
+
const lease = taken.value;
|
|
818
|
+
const woke = now.value.status !== "running";
|
|
819
|
+
const attached = await this.#started(now.value, parts, lease, woke, signal, claim);
|
|
820
|
+
if (!attached.ok && woke)
|
|
821
|
+
return this.#putBack(ref, lease, attached.error);
|
|
822
|
+
// Marked held (no longer detached) once attached; a failed attach that woke nothing leaves it as it was.
|
|
823
|
+
return andSettled(attached, await settleEntry(this.#ledger, ref, lease, attached.ok ? ({ detachedAt: _held, ...held }) => held : undefined));
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
/**
|
|
827
|
+
* An agent's own computer, its entry taken: started when it was not running; under a claim, its labels file carries
|
|
828
|
+
* the claim's run and epoch from now on, as the ledger does; then checked.
|
|
829
|
+
*/
|
|
830
|
+
async #started(record, parts, lease, woke, signal, claim) {
|
|
831
|
+
const ref = { id: record.id, name: record.name };
|
|
832
|
+
if (woke) {
|
|
833
|
+
const woken = await this.#wake(ref, record.status, signal);
|
|
834
|
+
if (!woken.ok)
|
|
835
|
+
return woken;
|
|
836
|
+
}
|
|
837
|
+
if (claim !== undefined) {
|
|
838
|
+
// Its labels file is written under the claim only while this attach still holds the entry, however long the wake took.
|
|
839
|
+
if (woke) {
|
|
840
|
+
const renewed = await renewEntry(this.#ledger, ref, lease, "being attached");
|
|
841
|
+
if (!renewed.ok)
|
|
842
|
+
return renewed;
|
|
843
|
+
}
|
|
844
|
+
const read = await this.#readLabels(ref, signal);
|
|
845
|
+
if (!read.ok)
|
|
846
|
+
return read;
|
|
847
|
+
const written = await this.#writeLabels(ref, { ...read.value, labels: lease.entry.labels }, signal);
|
|
848
|
+
if (!written.ok)
|
|
849
|
+
return written;
|
|
850
|
+
}
|
|
851
|
+
return this.#checked(record, parts, signal);
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* An agent's own computer this attach started, stopped again once the attach fails, best effort, as release detach
|
|
855
|
+
* stops it, so it is left as it was found; its ledger entry still says when it was detached (takeEntry kept it). One
|
|
856
|
+
* another claim has taken since is that claim's, and runs on. Never signalled: it outlives an abort.
|
|
706
857
|
*/
|
|
707
|
-
async #
|
|
858
|
+
async #putBack(ref, lease, error) {
|
|
859
|
+
const renewed = await renewEntry(this.#ledger, ref, lease, "being put back to sleep by an attach that failed");
|
|
860
|
+
if (!renewed.ok)
|
|
861
|
+
return fail(error.code, `${error.message}; ${ref.name} runs on, not stopped again: ${renewed.error.message}`);
|
|
862
|
+
const stopped = await this.#stop(ref);
|
|
863
|
+
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})`}`);
|
|
864
|
+
return andSettled(failed, await settleEntry(this.#ledger, ref, lease));
|
|
865
|
+
}
|
|
866
|
+
/** The labels file held to the name, who its programs run as, and the hello its screen answers: #attached's check. */
|
|
867
|
+
async #checked(record, parts, signal) {
|
|
708
868
|
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
869
|
const file = await this.#readLabels(ref, signal);
|
|
712
870
|
if (!file.ok)
|
|
713
871
|
return file;
|
|
714
872
|
const found = [];
|
|
715
|
-
const parts = nameParts(record.name);
|
|
716
873
|
const { labels } = file.value;
|
|
717
874
|
if (typeof file.value.name !== "string" || !sameComputer(file.value.name, record.name))
|
|
718
875
|
found.push(`name: Orgo's is ${record.name}, the labels file's ${show(file.value.name)}`);
|
|
876
|
+
if ((parts.owner ?? "") !== ownerHashOfLabels(labels))
|
|
877
|
+
found.push(`owner: the name's owner hash ${show(parts.owner)} is not the labels file's owner ${show(labels[OWNER_LABEL])}`);
|
|
719
878
|
const run = labels[RUN_LABEL];
|
|
720
|
-
|
|
879
|
+
// An agent's own computer is no run's: its name carries no run or epoch, whatever run made it.
|
|
880
|
+
if (parts.owner === undefined && (parts.run ?? "") !== (typeof run === "string" && run !== "" ? runHash(run) : ""))
|
|
721
881
|
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] ?? ""))
|
|
882
|
+
if (parts.owner === undefined && (parts.epoch ?? "") !== (labels[EPOCH_LABEL] ?? ""))
|
|
723
883
|
found.push(`epoch: the name says ${show(parts.epoch)}, the labels file ${show(labels[EPOCH_LABEL])}`);
|
|
724
884
|
if (file.value.image !== this.#profile.image)
|
|
725
885
|
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()))
|
|
886
|
+
const deadline = parts.owner === undefined ? new Date(file.value.deadline) : undefined;
|
|
887
|
+
if (deadline !== undefined && Number.isNaN(deadline.getTime()))
|
|
728
888
|
found.push(`deadline: the labels file reads ${show(file.value.deadline)}, not a time`);
|
|
889
|
+
if (parts.owner !== undefined && file.value.deadline !== NO_DEADLINE)
|
|
890
|
+
found.push(`deadline: an agent's own computer has none, and the labels file reads ${show(file.value.deadline)}`);
|
|
729
891
|
if (found.length > 0)
|
|
730
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("; ")}`);
|
|
731
893
|
const user = await this.#shell(ref.id, userCommand(this.#profile), this.#timeouts.execMs, signal);
|
|
@@ -746,11 +908,44 @@ export class OrgoDesktopProvider {
|
|
|
746
908
|
network: "open",
|
|
747
909
|
...(file.value.cpus === undefined ? {} : { cpus: file.value.cpus }),
|
|
748
910
|
...(file.value.memoryMb === undefined ? {} : { memoryMb: file.value.memoryMb }),
|
|
749
|
-
deadline,
|
|
911
|
+
...(deadline === undefined ? {} : { deadline }),
|
|
750
912
|
kind: "desktop",
|
|
913
|
+
...ownerReadBack(labels),
|
|
751
914
|
},
|
|
752
915
|
});
|
|
753
916
|
}
|
|
917
|
+
/**
|
|
918
|
+
* An agent's own computer that a detach stopped, started again for its attach: Orgo's start (a Windows computer comes
|
|
919
|
+
* back from its disk), then running, then taking a command. A status no start brings back is refused in words.
|
|
920
|
+
*/
|
|
921
|
+
async #wake(ref, status, signal) {
|
|
922
|
+
if (status === "frozen" || status === "stopped") {
|
|
923
|
+
const started = await this.#rest.request("POST", `/computers/${encodeURIComponent(ref.id)}/start`, { body: {}, timeoutMs: this.#timeouts.createMs, ...(signal ? { signal } : {}) });
|
|
924
|
+
if (!started.ok)
|
|
925
|
+
return fail(started.error.code, `${ref.name} could not be started for its attach: ${started.error.message}`);
|
|
926
|
+
}
|
|
927
|
+
else if (!RUNNING.has(status)) {
|
|
928
|
+
return fail("refused", `${ref.name} is ${status}: no start brings it back`);
|
|
929
|
+
}
|
|
930
|
+
const running = await this.#untilRunning(ref, signal);
|
|
931
|
+
if (!running.ok)
|
|
932
|
+
return running;
|
|
933
|
+
return this.#untilAnswering(ref, signal);
|
|
934
|
+
}
|
|
935
|
+
/** Orgo's bash call taking a command, tried within createMs: a computer Orgo reads running may still be booting. */
|
|
936
|
+
async #untilAnswering(ref, signal) {
|
|
937
|
+
const until = Date.now() + this.#timeouts.createMs;
|
|
938
|
+
for (;;) {
|
|
939
|
+
const ran = await this.#shell(ref.id, "exit 0", this.#timeouts.execMs, signal);
|
|
940
|
+
if (ran.ok)
|
|
941
|
+
return ok(undefined);
|
|
942
|
+
if (signal?.aborted)
|
|
943
|
+
return fail("aborted", `the wait for ${ref.name} to take a command was aborted`);
|
|
944
|
+
if (Date.now() >= until)
|
|
945
|
+
return fail(ran.error.code, `${ref.name} did not take a command within ${seconds(this.#timeouts.createMs)}: ${ran.error.message}`);
|
|
946
|
+
await pause(this.#timeouts.pollMs, signal);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
754
949
|
/**
|
|
755
950
|
* Every computer of this provider in the workspace that carries all of these labels. The run and the epoch are read
|
|
756
951
|
* 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 +959,30 @@ export class OrgoDesktopProvider {
|
|
|
764
959
|
if (!listed.ok)
|
|
765
960
|
return listed;
|
|
766
961
|
const others = Object.keys(labels).filter((key) => key !== RUN_LABEL && key !== EPOCH_LABEL);
|
|
962
|
+
const ours = listed.value.filter((record) => nameParts(record.name) !== undefined && this.#ours(record));
|
|
963
|
+
// An agent's computers' labels are the ledger's: read once, only when the workspace holds one.
|
|
964
|
+
const owned = ours.some((record) => nameParts(record.name)?.owner !== undefined) ? await this.#entries() : ok(new Map());
|
|
965
|
+
if (!owned.ok)
|
|
966
|
+
return owned;
|
|
767
967
|
const infos = [];
|
|
768
|
-
for (const record of
|
|
968
|
+
for (const record of ours) {
|
|
769
969
|
const parts = nameParts(record.name);
|
|
770
|
-
|
|
970
|
+
const ref = { id: record.id, name: record.name };
|
|
971
|
+
const state = RUNNING.has(record.status) ? "running" : "stopped";
|
|
972
|
+
// An agent's computer: every label from its ledger entry, with nothing run inside, whatever its state; no deadline.
|
|
973
|
+
if (parts.owner !== undefined) {
|
|
974
|
+
const view = ownedListing(owned.value.get(record.id), labels);
|
|
975
|
+
if (view === undefined)
|
|
976
|
+
continue;
|
|
977
|
+
infos.push({ ref, deadline: undefined, state, network: "open", ...view });
|
|
978
|
+
continue;
|
|
979
|
+
}
|
|
980
|
+
if (labels[OWNER_LABEL] !== undefined || labels[OWNER_SERVER_LABEL] !== undefined)
|
|
771
981
|
continue;
|
|
772
982
|
if (labels[RUN_LABEL] !== undefined && parts.run !== runHash(labels[RUN_LABEL]))
|
|
773
983
|
continue;
|
|
774
984
|
if (labels[EPOCH_LABEL] !== undefined && parts.epoch !== labels[EPOCH_LABEL])
|
|
775
985
|
continue;
|
|
776
|
-
const ref = { id: record.id, name: record.name };
|
|
777
986
|
// The name keeps the run's hash, not the run: listed with no run to match, the hash is given, as the Linux
|
|
778
987
|
// desktop's list gives it, so a caller (serve's janitor) can still tell whose run a computer is.
|
|
779
988
|
let read = {
|
|
@@ -790,7 +999,7 @@ export class OrgoDesktopProvider {
|
|
|
790
999
|
continue;
|
|
791
1000
|
read = { ...file.value.labels };
|
|
792
1001
|
}
|
|
793
|
-
infos.push({ ref, labels: read, deadline, state
|
|
1002
|
+
infos.push({ ref, labels: read, deadline, state, network: "open" });
|
|
794
1003
|
}
|
|
795
1004
|
return ok(infos);
|
|
796
1005
|
}
|
|
@@ -798,6 +1007,10 @@ export class OrgoDesktopProvider {
|
|
|
798
1007
|
async testConnection(context = {}) {
|
|
799
1008
|
return listTested("Orgo", await this.list({}, context));
|
|
800
1009
|
}
|
|
1010
|
+
/** Settings' pick of ORGO_WORKSPACE_ID (workspaceId): every workspace the key's account sees, by name. Makes nothing. */
|
|
1011
|
+
keyChoices(option, { signal } = {}) {
|
|
1012
|
+
return workspaceChoices(this.#rest, option, { timeoutMs: this.#timeouts.requestMs, ...(signal ? { signal } : {}) });
|
|
1013
|
+
}
|
|
801
1014
|
/** open: what Orgo gives, the one network here; never read from inside, and a change is refused. */
|
|
802
1015
|
async network(ref, policy, { signal } = {}) {
|
|
803
1016
|
const problem = refProblem(ref);
|
|
@@ -810,26 +1023,73 @@ export class OrgoDesktopProvider {
|
|
|
810
1023
|
return found;
|
|
811
1024
|
return ok("open");
|
|
812
1025
|
}
|
|
813
|
-
/**
|
|
814
|
-
|
|
1026
|
+
/**
|
|
1027
|
+
* delete: idempotent, a computer Orgo no longer has is done; it waits until Orgo no longer has it. detach, for an
|
|
1028
|
+
* agent's own Windows computer alone: Orgo's stop archives its disk, and it reads frozen. A Mac has no agent's
|
|
1029
|
+
* computer to detach (Orgo's stop takes none). No stop: a run's computer is deleted. Either forgets the pointer.
|
|
1030
|
+
*/
|
|
1031
|
+
async release(ref, how, { signal, claim } = {}) {
|
|
815
1032
|
const problem = refProblem(ref);
|
|
816
1033
|
if (problem)
|
|
817
1034
|
return fail("invalid", problem);
|
|
818
|
-
if (how !== "stop" && how !== "delete")
|
|
819
|
-
return fail("invalid", `release is "stop" or "
|
|
1035
|
+
if (how !== "stop" && how !== "delete" && how !== "detach")
|
|
1036
|
+
return fail("invalid", `release is "stop", "delete" or "detach", not ${String(how)}`);
|
|
820
1037
|
if (how === "stop")
|
|
821
|
-
return fail("unsupported", "an Orgo computer has no stop that this provider declares: release it with delete");
|
|
1038
|
+
return fail("unsupported", "an Orgo computer has no stop that this provider declares: release it with delete, or detach an agent's own");
|
|
822
1039
|
const read = await this.#get(ref.id);
|
|
823
1040
|
if (!read.ok)
|
|
824
|
-
return read.error.code === "not_found" ? ok(undefined) : read;
|
|
1041
|
+
return read.error.code === "not_found" && how === "delete" ? ok(undefined) : read;
|
|
825
1042
|
if (read.value.status === "deleted")
|
|
826
|
-
return ok(undefined);
|
|
1043
|
+
return how === "delete" ? ok(undefined) : fail("not_found", `${ref.name} is deleted: there is nothing to detach`);
|
|
827
1044
|
if (!this.#ours(read.value, ref.name))
|
|
828
1045
|
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
|
-
|
|
1046
|
+
if (how === "delete")
|
|
1047
|
+
return this.#dropPointer(ref, await (claim === undefined ? this.#delete(ref, signal) : this.#claimedDelete(ref, read.value.name, claim, signal)));
|
|
1048
|
+
const owner = nameParts(read.value.name)?.owner;
|
|
1049
|
+
if (owner === undefined)
|
|
1050
|
+
return fail("refused", `${ref.name} is a run's computer, not an agent's own: it is deleted at its release, never detached`);
|
|
1051
|
+
return this.#dropPointer(ref, await this.#detach(ref, owner, read.value.status, signal));
|
|
1052
|
+
}
|
|
1053
|
+
/** A released computer's pointer is no longer where any client moved it: the next client starts with none. */
|
|
1054
|
+
#dropPointer(ref, released) {
|
|
1055
|
+
if (released.ok)
|
|
831
1056
|
this.#pointers.delete(ref.id);
|
|
832
|
-
return
|
|
1057
|
+
return released;
|
|
1058
|
+
}
|
|
1059
|
+
/**
|
|
1060
|
+
* An agent's own Windows computer kept with its disk: stopped, read back frozen, and marked detached in the ledger. In
|
|
1061
|
+
* turn with this process's attaches, and holding the ledger entry from before the stop until it is marked detached
|
|
1062
|
+
* (takeEntry, settleEntry), so no attach starts it meanwhile and an attach under way refuses this detach. `status`:
|
|
1063
|
+
* Orgo's, read before the turn; `owner`: the name's owner hash.
|
|
1064
|
+
*/
|
|
1065
|
+
async #detach(ref, owner, status, signal) {
|
|
1066
|
+
return inTurn(this.#owning, ref.id, async (waited) => {
|
|
1067
|
+
const taken = await takeEntry(this.#ledger, ref, owner, undefined, "being detached");
|
|
1068
|
+
if (!taken.ok)
|
|
1069
|
+
return taken;
|
|
1070
|
+
const now = waited ? await this.#own(ref, signal) : ok({ status });
|
|
1071
|
+
const stopped = !now.ok ? now : now.value.status === "frozen" ? ok(undefined) : await this.#stop(ref, signal);
|
|
1072
|
+
return andSettled(stopped, await settleEntry(this.#ledger, ref, taken.value, stopped.ok ? (entry) => ({ ...entry, detachedAt: entry.detachedAt ?? new Date().toISOString() }) : undefined));
|
|
1073
|
+
});
|
|
1074
|
+
}
|
|
1075
|
+
/** Orgo's stop, the computer's disk archived, then the wait until Orgo reads it frozen. */
|
|
1076
|
+
async #stop(ref, signal) {
|
|
1077
|
+
const stopped = await this.#rest.request("POST", `/computers/${encodeURIComponent(ref.id)}/stop`, { timeoutMs: this.#timeouts.createMs, ...(signal ? { signal } : {}) });
|
|
1078
|
+
if (!stopped.ok)
|
|
1079
|
+
return fail(stopped.error.code, `${ref.name} could not be stopped for its agent: ${stopped.error.message}`);
|
|
1080
|
+
const until = Date.now() + this.#timeouts.goneMs;
|
|
1081
|
+
for (;;) {
|
|
1082
|
+
const read = await this.#get(ref.id, signal);
|
|
1083
|
+
if (!read.ok)
|
|
1084
|
+
return read;
|
|
1085
|
+
if (read.value.status === "frozen")
|
|
1086
|
+
return ok(undefined);
|
|
1087
|
+
if (Date.now() >= until)
|
|
1088
|
+
return fail("timeout", `${ref.name} was not stopped ${seconds(this.#timeouts.goneMs)} after its stop; Orgo last read ${read.value.status}`);
|
|
1089
|
+
await pause(this.#timeouts.pollMs, signal);
|
|
1090
|
+
if (signal?.aborted)
|
|
1091
|
+
return fail("aborted", `the wait for ${ref.name} to stop was aborted`);
|
|
1092
|
+
}
|
|
833
1093
|
}
|
|
834
1094
|
/**
|
|
835
1095
|
* Moves the deadline to now + ttlMs, never earlier and never past the most the labels file holds: Orgo's end first
|
|
@@ -837,16 +1097,7 @@ export class OrgoDesktopProvider {
|
|
|
837
1097
|
* extends are taken in turn.
|
|
838
1098
|
*/
|
|
839
1099
|
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;
|
|
1100
|
+
return inTurn(this.#extending, typeof ref?.id === "string" ? ref.id : "", () => this.#extend(ref, ttlMs, context));
|
|
850
1101
|
}
|
|
851
1102
|
async #extend(ref, ttlMs, { signal } = {}) {
|
|
852
1103
|
const problem = refProblem(ref);
|
|
@@ -857,6 +1108,8 @@ export class OrgoDesktopProvider {
|
|
|
857
1108
|
const found = await this.#own(ref, signal);
|
|
858
1109
|
if (!found.ok)
|
|
859
1110
|
return found;
|
|
1111
|
+
if (nameParts(found.value.name)?.owner !== undefined)
|
|
1112
|
+
return fail("refused", `${ref.name} is an agent's own computer: it has no deadline to move`);
|
|
860
1113
|
if (found.value.status !== "running")
|
|
861
1114
|
return fail("refused", `${ref.name} is ${found.value.status}, not running`);
|
|
862
1115
|
const file = await this.#readLabels(ref, signal);
|
|
@@ -1105,7 +1358,10 @@ export class OrgoDesktopClient {
|
|
|
1105
1358
|
rgba.set([red, green, blue, 255], (y * width + x) * 4);
|
|
1106
1359
|
}
|
|
1107
1360
|
}
|
|
1108
|
-
bytes = encodePng(rgba, width, rect);
|
|
1361
|
+
bytes = await encodePng(rgba, width, rect);
|
|
1362
|
+
// close() may have run while the region was deflating: hold nothing for a closed connection.
|
|
1363
|
+
if (this.#closed)
|
|
1364
|
+
return fail("gone", "the connection to the computer is closed");
|
|
1109
1365
|
}
|
|
1110
1366
|
const id = randomBytes(6).toString("hex");
|
|
1111
1367
|
this.#held = { id, bytes };
|
|
@@ -1565,8 +1821,14 @@ export class OrgoDesktopClient {
|
|
|
1565
1821
|
}
|
|
1566
1822
|
const script = FILES_SCRIPT.replace("@@REQUEST@@", Buffer.from(JSON.stringify(request), "utf8").toString("base64"));
|
|
1567
1823
|
const ran = await bashCall(this.rest, this.computer, script, 120_000, signal);
|
|
1568
|
-
if (!ran.ok)
|
|
1569
|
-
|
|
1824
|
+
if (!ran.ok || ran.value.timedOut) {
|
|
1825
|
+
// 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
|
|
1826
|
+
// call's own signal, which may be the abort itself.
|
|
1827
|
+
if (typeof request.staged === "string")
|
|
1828
|
+
await bashCall(this.rest, this.computer, `Remove-Item -LiteralPath ${psQuote(request.staged)} -Force -ErrorAction SilentlyContinue`, 30_000).catch(() => undefined);
|
|
1829
|
+
if (!ran.ok)
|
|
1830
|
+
return signal.aborted ? refuse("aborted", `files.${op} was aborted`) : ran;
|
|
1831
|
+
}
|
|
1570
1832
|
let answer;
|
|
1571
1833
|
try {
|
|
1572
1834
|
answer = JSON.parse(ran.value.stdout.trim() || "null");
|