@parcha/agentrun-computer 0.1.0-beta.34 → 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.
Files changed (107) hide show
  1. package/README.md +251 -46
  2. package/box/Dockerfile +3 -1
  3. package/box/agentrun-archil +59 -7
  4. package/box/build-daytona.py +20 -4
  5. package/dist/browser.d.ts +73 -23
  6. package/dist/browser.js +141 -17
  7. package/dist/browserbase.d.ts +48 -25
  8. package/dist/browserbase.js +176 -29
  9. package/dist/chrome.d.ts +3 -4
  10. package/dist/chrome.js +6 -11
  11. package/dist/computerd-linux-amd64 +0 -0
  12. package/dist/computerd-linux-arm64 +0 -0
  13. package/dist/conformance/browserbase.js +1 -1
  14. package/dist/conformance/cases/provider.js +78 -3
  15. package/dist/conformance/daytona.d.ts +4 -0
  16. package/dist/conformance/daytona.js +7 -1
  17. package/dist/conformance/e2b.js +2 -1
  18. package/dist/conformance/fixture.js +24 -2
  19. package/dist/conformance/kernel.js +1 -1
  20. package/dist/conformance/kind.js +2 -1
  21. package/dist/conformance/{local.d.ts → local-chrome.d.ts} +7 -7
  22. package/dist/conformance/{local.js → local-chrome.js} +11 -11
  23. package/dist/conformance/orgo.d.ts +11 -5
  24. package/dist/conformance/orgo.js +37 -8
  25. package/dist/conformance/phone-harness.js +2 -1
  26. package/dist/conformance/report.js +3 -2
  27. package/dist/conformance/runner.js +25 -7
  28. package/dist/conformance/types.d.ts +21 -8
  29. package/dist/conformance/types.js +10 -0
  30. package/dist/conformance/windows-fixture.d.ts +1 -0
  31. package/dist/conformance/windows-fixture.js +37 -0
  32. package/dist/daytona.d.ts +16 -35
  33. package/dist/daytona.js +87 -95
  34. package/dist/descriptor.d.ts +116 -0
  35. package/dist/descriptor.js +92 -0
  36. package/dist/descriptors/browserbase.d.ts +25 -0
  37. package/dist/descriptors/browserbase.js +20 -0
  38. package/dist/descriptors/daytona.d.ts +83 -0
  39. package/dist/descriptors/daytona.js +81 -0
  40. package/dist/descriptors/docker.d.ts +12 -0
  41. package/dist/descriptors/docker.js +17 -0
  42. package/dist/descriptors/e2b.d.ts +22 -0
  43. package/dist/descriptors/e2b.js +19 -0
  44. package/dist/descriptors/index.d.ts +256 -0
  45. package/dist/descriptors/index.js +19 -0
  46. package/dist/descriptors/kernel.d.ts +21 -0
  47. package/dist/descriptors/kernel.js +17 -0
  48. package/dist/descriptors/local-chrome.d.ts +17 -0
  49. package/dist/descriptors/local-chrome.js +21 -0
  50. package/dist/descriptors/modal.d.ts +54 -0
  51. package/dist/descriptors/modal.js +48 -0
  52. package/dist/descriptors/orgo.d.ts +99 -0
  53. package/dist/descriptors/orgo.js +49 -0
  54. package/dist/descriptors/phone-harness.d.ts +33 -0
  55. package/dist/descriptors/phone-harness.js +23 -0
  56. package/dist/descriptors/shared.d.ts +7 -0
  57. package/dist/descriptors/shared.js +6 -0
  58. package/dist/desktop.d.ts +4 -4
  59. package/dist/desktop.js +4 -4
  60. package/dist/docker/egress.d.ts +5 -4
  61. package/dist/docker/egress.js +7 -6
  62. package/dist/docker/image.js +17 -15
  63. package/dist/docker/inspect.d.ts +147 -0
  64. package/dist/docker/inspect.js +335 -0
  65. package/dist/docker.d.ts +6 -6
  66. package/dist/docker.js +53 -47
  67. package/dist/drift.d.ts +3 -145
  68. package/dist/drift.js +5 -334
  69. package/dist/e2b.d.ts +2 -8
  70. package/dist/e2b.js +17 -23
  71. package/dist/index.d.ts +9 -12
  72. package/dist/index.js +14 -12
  73. package/dist/isolation.js +2 -2
  74. package/dist/janitor.d.ts +20 -2
  75. package/dist/janitor.js +67 -19
  76. package/dist/kernel.d.ts +19 -29
  77. package/dist/kernel.js +110 -32
  78. package/dist/kinds.d.ts +59 -34
  79. package/dist/kinds.js +19 -117
  80. package/dist/labels.d.ts +20 -0
  81. package/dist/labels.js +62 -1
  82. package/dist/{local.d.ts → local-chrome.d.ts} +8 -9
  83. package/dist/{local.js → local-chrome.js} +26 -19
  84. package/dist/local-supervisor.d.ts +3 -3
  85. package/dist/local-supervisor.js +3 -3
  86. package/dist/modal.d.ts +6 -25
  87. package/dist/modal.js +86 -88
  88. package/dist/mount-archil.d.ts +25 -3
  89. package/dist/mount-archil.js +43 -12
  90. package/dist/orgo-channel.js +16 -13
  91. package/dist/orgo-desktop.d.ts +162 -0
  92. package/dist/orgo-desktop.js +1856 -0
  93. package/dist/orgo-names.d.ts +178 -0
  94. package/dist/orgo-names.js +287 -0
  95. package/dist/orgo.d.ts +34 -20
  96. package/dist/orgo.js +424 -173
  97. package/dist/phone-harness.d.ts +7 -12
  98. package/dist/phone-harness.js +70 -54
  99. package/dist/protocol.d.ts +5 -0
  100. package/dist/provider.d.ts +153 -10
  101. package/dist/provider.js +48 -1
  102. package/dist/providers.d.ts +23 -24
  103. package/dist/providers.js +14 -17
  104. package/dist/result.d.ts +11 -1
  105. package/dist/result.js +11 -0
  106. package/dist/test-connection.js +2 -2
  107. package/package.json +42 -2
@@ -0,0 +1,178 @@
1
+ import type { ComputerClaim, ComputerOwner, ComputerRef } from "./provider.js";
2
+ import { type Result } from "./result.js";
3
+ /** The longest name Orgo holds. */
4
+ export declare const NAME_MAX = 80;
5
+ /** The labels file's deadline, and its most, for an agent's own computer: it has none. */
6
+ export declare const NO_DEADLINE = "none";
7
+ /**
8
+ * The hash an agent's own computer's name carries: of its owner label's value, agent:<id>, and of its server's after a
9
+ * NUL (which neither may hold) when the owner names one, so one agent id on two servers has two hashes.
10
+ */
11
+ export declare const ownerHashOf: (owner: ComputerOwner) => string;
12
+ /** The owner hash these labels say (its owner's and server's, ownerHashOf), or "" when they carry no readable owner. */
13
+ export declare function ownerHashOfLabels(labels: Record<string, string>): string;
14
+ /**
15
+ * An agent's own computer as the provider's ledger keeps it (the server's computer ledger, as the phone harness's
16
+ * sessions are kept): its labels, the owner's and the claim's among them, and when it was detached. Orgo holds no label
17
+ * of a computer, and a detached one is never woken to read its labels file, so list reads these instead.
18
+ */
19
+ export interface OrgoLedgerEntry {
20
+ /** Orgo's computer id. */
21
+ id: string;
22
+ labels: Record<string, string>;
23
+ /** When the computer was detached, ISO time; absent while a run holds it. */
24
+ detachedAt?: string;
25
+ /**
26
+ * While an attach or detach holds the computer (takeEntry to settleEntry), when its current step began, ISO time, and
27
+ * what it is doing: no other attach or detach takes it meanwhile.
28
+ */
29
+ busy?: {
30
+ since: string;
31
+ doing: string;
32
+ };
33
+ }
34
+ /** Where the provider keeps its agents' computers' labels. Each call may be sync or async. */
35
+ export interface OrgoLedger {
36
+ put(entry: OrgoLedgerEntry): void | Promise<void>;
37
+ get(id: string): OrgoLedgerEntry | undefined | Promise<OrgoLedgerEntry | undefined>;
38
+ list(): OrgoLedgerEntry[] | Promise<OrgoLedgerEntry[]>;
39
+ remove(id: string): void | Promise<void>;
40
+ /**
41
+ * entry put in place of `was` only while the kept entry is still `was` as it was read, in one step no other writer
42
+ * splits: true when it was put; false, with nothing written, when the entry changed since or is gone. Attach takes an
43
+ * agent's computer under a claim with it (takeEntry), so two claims that read one entry never both write it.
44
+ */
45
+ swap(was: OrgoLedgerEntry, entry: OrgoLedgerEntry): boolean | Promise<boolean>;
46
+ }
47
+ /** Whether two ledger entries hold the same: the same id, labels and fields, in any order. */
48
+ export declare const sameEntry: (a: OrgoLedgerEntry, b: OrgoLedgerEntry) => boolean;
49
+ /** The run and epoch these labels say, in words, for a claim that lost its computer to another. */
50
+ export declare const holderOf: (labels: Record<string, string>) => string;
51
+ /** How long a busy marker keeps every other attach and detach off the computer: the most one step of an attach or detach
52
+ * (a resume, a suspend, a stop, a daemon's replacement) is waited on, since each step renews it. A marker older is an
53
+ * attach or detach that died, and is taken over. */
54
+ export declare const BUSY_MS: number;
55
+ /**
56
+ * An agent's computer's ledger entry as one attach, detach or delete under a claim holds it, from takeEntry to
57
+ * settleEntry (a delete: until it forgets the entry): the entry as last written by it, its busy marker in force. One rule
58
+ * keeps them all apart: nothing is done to the computer but under this marker, renewed (renewEntry) right before each
59
+ * step that wakes, stops, suspends or replaces what runs in it, and only its holder settles the entry, every write a swap
60
+ * from the entry it last wrote. A holder whose swap finds the entry changed (its marker went stale and another took it,
61
+ * or it was removed) has lost it: it does nothing more to the computer, and its settle is an error, never a success.
62
+ */
63
+ export interface EntryLease {
64
+ entry: OrgoLedgerEntry;
65
+ }
66
+ /**
67
+ * An agent's computer's ledger entry taken, before anything is done to the computer, for an attach (under `claim`, held
68
+ * to it, claimProblem) or a detach (no claim): read, refused while another holds it busy, then swapped to carry a busy
69
+ * marker saying `doing` (and the claim's run and epoch) only while it is still the entry read. Two claims of one run that
70
+ * read the same entry never both write it: the one whose swap finds it changed reads it again and is held to the claim
71
+ * again, so a lower epoch never writes over a higher one and is refused (fenced). When it was detached is kept until the
72
+ * holder settles it (settleEntry), so an attach that fails leaves it detached. `only`: the entry is taken only while it
73
+ * carries exactly the claim's run and epoch (a delete under a claim, which never takes a computer another claim has).
74
+ */
75
+ export declare function takeEntry(ledger: OrgoLedger, ref: ComputerRef, ownerHash: string, claim: ComputerClaim | undefined, doing: string, { only }?: {
76
+ only?: boolean | undefined;
77
+ }): Promise<Result<EntryLease>>;
78
+ /**
79
+ * The held entry's busy marker renewed, saying `doing` from now, only while the entry is still the one this holder last
80
+ * wrote: right before each step that wakes, stops, suspends or replaces what runs in the computer, so a holder that lost
81
+ * the entry never does that step to a computer another holds.
82
+ */
83
+ export declare function renewEntry(ledger: OrgoLedger, ref: ComputerRef, lease: EntryLease, doing: string): Promise<Result<void>>;
84
+ /**
85
+ * The held entry settled by its holder alone: its busy marker cleared, with `settle` applied (an attach marks it held, no
86
+ * longer detached; a detach marks it detached), swapped only while the entry is still the one this holder last wrote. A
87
+ * swap that finds it changed is an error: the holder lost the entry, and its attach or detach does not answer ok.
88
+ */
89
+ export declare function settleEntry(ledger: OrgoLedger, ref: ComputerRef, lease: EntryLease, settle?: (entry: OrgoLedgerEntry) => OrgoLedgerEntry): Promise<Result<void>>;
90
+ /** `done`, or, where the settle after it failed, that failure: with done's own words first when done had failed too. */
91
+ export declare function andSettled<T>(done: Result<T>, settled: Result<void>): Result<T>;
92
+ /**
93
+ * `work` for one computer, taken in turn with every other of this process's for the same key (`turns` keeps each key's
94
+ * last): one starts once the one before it has settled, whatever its outcome. `work` is told whether it waited for one,
95
+ * so it reads again what that one may have changed.
96
+ */
97
+ export declare function inTurn<T>(turns: Map<string, Promise<unknown>>, key: string, work: (waited: boolean) => Promise<T>): Promise<T>;
98
+ /**
99
+ * What list() gives an agent's own computer, with nothing run inside, by filter: owned, with its ledger entry's labels
100
+ * (the owner's and the claim's run and epoch among them), its owner, and when it was detached. With no entry its labels
101
+ * are unknown: it is listed owned with no labels and no owner, to an unfiltered list alone, and the janitor, which never
102
+ * deletes a computer whose owner it cannot read, keeps it. undefined when the computer does not carry every label of
103
+ * the filter.
104
+ */
105
+ export declare function ownedListing(entry: OrgoLedgerEntry | undefined, filter: Record<string, string>): {
106
+ labels: Record<string, string>;
107
+ owned: true;
108
+ owner?: ComputerOwner;
109
+ detachedAt?: Date;
110
+ } | undefined;
111
+ /** { owner } when these labels carry a readable owner label, else nothing: for a listing or attach's read-back. */
112
+ export declare function ownerReadBack(labels: Record<string, string>): {
113
+ owner?: ComputerOwner;
114
+ };
115
+ /** Why an agent's computer is neither attached nor detached with no ledger entry, in words: its labels are unknown. */
116
+ export declare const lostEntry: (name: string) => string;
117
+ /**
118
+ * Why a run cannot take this computer under claim, in words, or undefined when it can: a run's computer (no owner) is
119
+ * its run's, never claimed; a claim of the same run below the epoch the computer carries is fenced. Nothing changes.
120
+ */
121
+ export declare function claimProblem(name: string, ownerHash: string | undefined, entry: OrgoLedgerEntry | undefined, claim: {
122
+ run: string;
123
+ epoch: number;
124
+ }): string | undefined;
125
+ /** A ledger in this process's memory: the default, enough for one process that makes and ends its computers. */
126
+ export declare function orgoMemoryLedger(): OrgoLedger;
127
+ /** An Orgo computer's id. */
128
+ export declare const COMPUTER_ID: RegExp;
129
+ /** What the labels file holds. */
130
+ export interface LabelsFile {
131
+ name: string;
132
+ labels: Record<string, string>;
133
+ deadline: string;
134
+ maxDeadline: string;
135
+ image: string;
136
+ cpus?: number;
137
+ memoryMb?: number;
138
+ }
139
+ /** What Orgo reports of a computer, as far as a provider reads it. */
140
+ export interface OrgoComputer {
141
+ id: string;
142
+ name: string;
143
+ status: string;
144
+ os?: string;
145
+ workspace_id?: string;
146
+ project_id?: string;
147
+ ends_at?: string | null;
148
+ created_at?: string;
149
+ }
150
+ export declare const show: (value: unknown) => string;
151
+ /**
152
+ * The name of a computer with these labels: computer-<hex>, then -r<the run label's hash> and -e<the epoch label> when
153
+ * the labels carry a run and an epoch that is one, then -d<the deadline> when given: whole seconds since the epoch,
154
+ * rounded up (never before init's own), in base 36, the janitor's deadline. Only those: a name is no place for a label's
155
+ * value. Everything but the deadline is the name's stable part, which a rename on extend keeps. An agent's own computer
156
+ * (owner) is computer-<hex>-o<the owner label's hash> alone, with no deadline for the janitor to end it by: the run and
157
+ * the epoch of the claim that holds it change with each attach, so they are in the ledger (OrgoLedger), not the name.
158
+ */
159
+ export declare function orgoName(labels: Record<string, string>, hex: string, deadline?: Date, owner?: ComputerOwner): string;
160
+ /** A name's stable part with this deadline. */
161
+ export declare function withDeadline(stable: string, deadline: Date): string;
162
+ /** What a name of this provider's making says, or undefined for any other name: its stable part, and its deadline. */
163
+ export declare function nameParts(name: unknown): {
164
+ hex: string;
165
+ owner?: string;
166
+ run?: string;
167
+ epoch?: string;
168
+ stable: string;
169
+ deadline?: Date;
170
+ } | undefined;
171
+ /** Whether two names are one computer's: their stable parts are the same (the deadline moves with extend). */
172
+ export declare const sameComputer: (a: string, b: string) => boolean;
173
+ export declare function labelProblem(key: string, value: unknown): string | undefined;
174
+ export declare function filterProblem(labels: unknown): string | undefined;
175
+ /** Why ref is not an Orgo computer's ref, or undefined. */
176
+ export declare function refProblem(ref: ComputerRef): string | undefined;
177
+ /** The labels file at path as written, or why it is not. */
178
+ export declare function labelsOf(text: string, path: string): LabelsFile | string;
@@ -0,0 +1,287 @@
1
+ // What every Orgo provider (the Linux desktop with computerd, orgo.ts, and the Windows and macOS desktops Orgo's API
2
+ // answers, orgo-desktop.ts) shares about a computer with nothing run inside it: its name, which carries the run, the
3
+ // epoch and the janitor's deadline; the labels a spec may give; Orgo's record of a computer; and the labels file each
4
+ // keeps in the computer, the truth attach reads.
5
+ import { COMPUTER_LABEL } from "./drift.js";
6
+ import { EPOCH_LABEL, IDLE_TTL_LABEL, OWNER_LABEL, OWNER_SERVER_LABEL, ownerLabels, ownerOf, RUN_LABEL, runHash } from "./labels.js";
7
+ import { failFor, ok, vendorFail } from "./result.js";
8
+ const fail = failFor("orgo");
9
+ /** The longest name Orgo holds. */
10
+ export const NAME_MAX = 80;
11
+ /**
12
+ * A name of this provider's making: computer-<12 hex>, then either -o<the owner's hash> (an agent's own computer, which
13
+ * outlives runs and has no deadline) or the run label's hash, the epoch label and the deadline (base 36 s).
14
+ */
15
+ const NAME = /^computer-([0-9a-f]{12})(?:-o([a-z2-7]{16})|(?:-r([a-z2-7]{16}))?(?:-e(0|[1-9][0-9]{0,15}))?(?:-d([0-9a-z]{1,11}))?)$/;
16
+ /** The labels file's deadline, and its most, for an agent's own computer: it has none. */
17
+ export const NO_DEADLINE = "none";
18
+ /**
19
+ * The hash an agent's own computer's name carries: of its owner label's value, agent:<id>, and of its server's after a
20
+ * NUL (which neither may hold) when the owner names one, so one agent id on two servers has two hashes.
21
+ */
22
+ export const ownerHashOf = (owner) => {
23
+ const labels = ownerLabels(owner);
24
+ const server = labels[OWNER_SERVER_LABEL];
25
+ return runHash(server === undefined ? labels[OWNER_LABEL] : `${labels[OWNER_LABEL]}\0${server}`);
26
+ };
27
+ /** The owner hash these labels say (its owner's and server's, ownerHashOf), or "" when they carry no readable owner. */
28
+ export function ownerHashOfLabels(labels) {
29
+ const owner = ownerOf(labels);
30
+ return owner.ok && owner.value !== undefined ? ownerHashOf(owner.value) : "";
31
+ }
32
+ /** An entry as one string, its keys and its labels' keys sorted, so two entries compare by what they hold alone. */
33
+ const canonical = (entry) => JSON.stringify(entry, (_key, value) => (value !== null && typeof value === "object" && !Array.isArray(value) ? Object.fromEntries(Object.entries(value).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) : value));
34
+ /** Whether two ledger entries hold the same: the same id, labels and fields, in any order. */
35
+ export const sameEntry = (a, b) => canonical(a) === canonical(b);
36
+ /** How often an attach reads an agent's computer's ledger entry again when another writer changed it under its swap. */
37
+ const SWAP_TRIES = 5;
38
+ /** A ledger call's value, or the ledger's failure in words: a provider's calls answer, never throw. */
39
+ async function ledgerCall(what, call) {
40
+ try {
41
+ return ok(await call());
42
+ }
43
+ catch (error) {
44
+ return vendorFail("orgo", `the ledger of agents' computers could not be ${what}: ${error instanceof Error ? error.message : String(error)}`);
45
+ }
46
+ }
47
+ /** The run and epoch these labels say, in words, for a claim that lost its computer to another. */
48
+ export const holderOf = (labels) => `run ${JSON.stringify(labels[RUN_LABEL] ?? "")} at epoch ${labels[EPOCH_LABEL] ?? "none"}`;
49
+ /** How long a busy marker keeps every other attach and detach off the computer: the most one step of an attach or detach
50
+ * (a resume, a suspend, a stop, a daemon's replacement) is waited on, since each step renews it. A marker older is an
51
+ * attach or detach that died, and is taken over. */
52
+ export const BUSY_MS = 15 * 60_000;
53
+ /** Whether an entry carries a busy marker still in force. */
54
+ const busyNow = (entry) => entry.busy !== undefined && Date.now() - Date.parse(entry.busy.since) < BUSY_MS;
55
+ /**
56
+ * An agent's computer's ledger entry taken, before anything is done to the computer, for an attach (under `claim`, held
57
+ * to it, claimProblem) or a detach (no claim): read, refused while another holds it busy, then swapped to carry a busy
58
+ * marker saying `doing` (and the claim's run and epoch) only while it is still the entry read. Two claims of one run that
59
+ * read the same entry never both write it: the one whose swap finds it changed reads it again and is held to the claim
60
+ * again, so a lower epoch never writes over a higher one and is refused (fenced). When it was detached is kept until the
61
+ * holder settles it (settleEntry), so an attach that fails leaves it detached. `only`: the entry is taken only while it
62
+ * carries exactly the claim's run and epoch (a delete under a claim, which never takes a computer another claim has).
63
+ */
64
+ export async function takeEntry(ledger, ref, ownerHash, claim, doing, { only = false } = {}) {
65
+ for (let tries = 1;; tries++) {
66
+ const read = await ledgerCall("read", () => ledger.get(ref.id));
67
+ if (!read.ok)
68
+ return read;
69
+ const entry = read.value;
70
+ if (entry === undefined)
71
+ return fail("refused", lostEntry(ref.name));
72
+ if (busyNow(entry))
73
+ return fail("refused", `${ref.name} is busy (${entry.busy?.doing}): try again in a moment`);
74
+ if (claim !== undefined) {
75
+ const fenced = only ? notClaims(ref.name, entry, claim) : claimProblem(ref.name, ownerHash, entry, claim);
76
+ if (fenced !== undefined)
77
+ return fail("refused", fenced);
78
+ }
79
+ // A busy marker older than BUSY_MS is an attach or detach that died: this one takes the entry over.
80
+ const { busy: _stale, ...unmarked } = entry;
81
+ const taken = {
82
+ ...unmarked,
83
+ ...(claim === undefined ? {} : { labels: { ...entry.labels, [RUN_LABEL]: claim.run, [EPOCH_LABEL]: String(claim.epoch) } }),
84
+ busy: { since: new Date().toISOString(), doing },
85
+ };
86
+ const swapped = await ledgerCall("written", () => ledger.swap(entry, taken));
87
+ if (!swapped.ok)
88
+ return swapped;
89
+ if (swapped.value)
90
+ return ok({ entry: taken });
91
+ if (tries >= SWAP_TRIES)
92
+ return fail("refused", `${ref.name}'s ledger entry changed under each of ${SWAP_TRIES} tries to take it; nothing changed`);
93
+ }
94
+ }
95
+ /** Why an entry that does not carry exactly `claim`'s run and epoch is not that claim's to delete, in words; else undefined. */
96
+ const notClaims = (name, entry, claim) => entry.labels[RUN_LABEL] === claim.run && entry.labels[EPOCH_LABEL] === String(claim.epoch)
97
+ ? undefined
98
+ : `fenced: ${name} carries ${holderOf(entry.labels)}, not this claim's (${holderOf({ [RUN_LABEL]: claim.run, [EPOCH_LABEL]: String(claim.epoch) })}): another claim has it, so it is left as it is`;
99
+ /** Why a holder's swap found its entry changed, in words: it has lost the computer, which it leaves as it is. */
100
+ const lostLease = (ref, lease) => `fenced: ${ref.name}'s ledger entry changed while this ${lease.entry.busy?.doing ?? "attach"} (${holderOf(lease.entry.labels)}) held it (its busy marker went stale and another took it, or it was removed): the computer is left as it is`;
101
+ /**
102
+ * The held entry's busy marker renewed, saying `doing` from now, only while the entry is still the one this holder last
103
+ * wrote: right before each step that wakes, stops, suspends or replaces what runs in the computer, so a holder that lost
104
+ * the entry never does that step to a computer another holds.
105
+ */
106
+ export async function renewEntry(ledger, ref, lease, doing) {
107
+ const renewed = { ...lease.entry, busy: { since: new Date().toISOString(), doing } };
108
+ const swapped = await ledgerCall("written", () => ledger.swap(lease.entry, renewed));
109
+ if (!swapped.ok)
110
+ return swapped;
111
+ if (!swapped.value)
112
+ return fail("refused", lostLease(ref, lease));
113
+ lease.entry = renewed;
114
+ return ok(undefined);
115
+ }
116
+ /**
117
+ * The held entry settled by its holder alone: its busy marker cleared, with `settle` applied (an attach marks it held, no
118
+ * longer detached; a detach marks it detached), swapped only while the entry is still the one this holder last wrote. A
119
+ * swap that finds it changed is an error: the holder lost the entry, and its attach or detach does not answer ok.
120
+ */
121
+ export async function settleEntry(ledger, ref, lease, settle = (entry) => entry) {
122
+ const { busy: _done, ...cleared } = lease.entry;
123
+ const settled = settle(cleared);
124
+ const swapped = await ledgerCall("written", () => ledger.swap(lease.entry, settled));
125
+ if (!swapped.ok)
126
+ return swapped;
127
+ if (!swapped.value)
128
+ return fail("refused", lostLease(ref, lease));
129
+ lease.entry = settled;
130
+ return ok(undefined);
131
+ }
132
+ /** `done`, or, where the settle after it failed, that failure: with done's own words first when done had failed too. */
133
+ export function andSettled(done, settled) {
134
+ if (settled.ok)
135
+ return done;
136
+ return done.ok ? settled : fail(done.error.code, `${done.error.message}; and ${settled.error.message}`);
137
+ }
138
+ /**
139
+ * `work` for one computer, taken in turn with every other of this process's for the same key (`turns` keeps each key's
140
+ * last): one starts once the one before it has settled, whatever its outcome. `work` is told whether it waited for one,
141
+ * so it reads again what that one may have changed.
142
+ */
143
+ export function inTurn(turns, key, work) {
144
+ const waiting = turns.get(key);
145
+ const turn = (waiting ?? Promise.resolve()).then(() => work(waiting !== undefined));
146
+ const settled = turn.then(() => undefined, () => undefined);
147
+ turns.set(key, settled);
148
+ void settled.then(() => {
149
+ if (turns.get(key) === settled)
150
+ turns.delete(key);
151
+ });
152
+ return turn;
153
+ }
154
+ /**
155
+ * What list() gives an agent's own computer, with nothing run inside, by filter: owned, with its ledger entry's labels
156
+ * (the owner's and the claim's run and epoch among them), its owner, and when it was detached. With no entry its labels
157
+ * are unknown: it is listed owned with no labels and no owner, to an unfiltered list alone, and the janitor, which never
158
+ * deletes a computer whose owner it cannot read, keeps it. undefined when the computer does not carry every label of
159
+ * the filter.
160
+ */
161
+ export function ownedListing(entry, filter) {
162
+ if (entry === undefined)
163
+ return Object.keys(filter).length === 0 ? { labels: {}, owned: true } : undefined;
164
+ if (!Object.entries(filter).every(([key, value]) => entry.labels[key] === value))
165
+ return undefined;
166
+ const detached = entry.detachedAt === undefined ? Number.NaN : Date.parse(entry.detachedAt);
167
+ return { labels: { ...entry.labels }, owned: true, ...ownerReadBack(entry.labels), ...(Number.isNaN(detached) ? {} : { detachedAt: new Date(detached) }) };
168
+ }
169
+ /** { owner } when these labels carry a readable owner label, else nothing: for a listing or attach's read-back. */
170
+ export function ownerReadBack(labels) {
171
+ const owner = ownerOf(labels);
172
+ return owner.ok && owner.value !== undefined ? { owner: owner.value } : {};
173
+ }
174
+ /** Why an agent's computer is neither attached nor detached with no ledger entry, in words: its labels are unknown. */
175
+ export const lostEntry = (name) => `${name} is an agent's computer with no entry in the provider's ledger, so its owner and labels are unknown: it is neither attached nor detached; release it with delete`;
176
+ /**
177
+ * Why a run cannot take this computer under claim, in words, or undefined when it can: a run's computer (no owner) is
178
+ * its run's, never claimed; a claim of the same run below the epoch the computer carries is fenced. Nothing changes.
179
+ */
180
+ export function claimProblem(name, ownerHash, entry, claim) {
181
+ if (ownerHash === undefined)
182
+ return `${name} is a run's computer, which belongs to its run: no other run takes it under a claim`;
183
+ const carried = entry?.labels ?? {};
184
+ const epoch = Number(carried[EPOCH_LABEL]);
185
+ if (carried[RUN_LABEL] === claim.run && Number.isSafeInteger(epoch) && claim.epoch < epoch) {
186
+ return `fenced: ${name} is held by run ${JSON.stringify(claim.run)} at epoch ${epoch}, above this claim's ${claim.epoch}; nothing changed`;
187
+ }
188
+ return undefined;
189
+ }
190
+ /** A ledger in this process's memory: the default, enough for one process that makes and ends its computers. */
191
+ export function orgoMemoryLedger() {
192
+ const entries = new Map();
193
+ return {
194
+ put: (entry) => void entries.set(entry.id, structuredClone(entry)),
195
+ get: (id) => structuredClone(entries.get(id)),
196
+ list: () => [...entries.values()].map((entry) => structuredClone(entry)),
197
+ remove: (id) => void entries.delete(id),
198
+ swap: (was, entry) => {
199
+ const kept = entries.get(entry.id);
200
+ if (kept === undefined || was.id !== entry.id || !sameEntry(kept, was))
201
+ return false;
202
+ entries.set(entry.id, structuredClone(entry));
203
+ return true;
204
+ },
205
+ };
206
+ }
207
+ /** An Orgo computer's id. */
208
+ export const COMPUTER_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/;
209
+ export const show = (value) => (value === undefined ? "nothing" : JSON.stringify(value));
210
+ /**
211
+ * The name of a computer with these labels: computer-<hex>, then -r<the run label's hash> and -e<the epoch label> when
212
+ * the labels carry a run and an epoch that is one, then -d<the deadline> when given: whole seconds since the epoch,
213
+ * rounded up (never before init's own), in base 36, the janitor's deadline. Only those: a name is no place for a label's
214
+ * value. Everything but the deadline is the name's stable part, which a rename on extend keeps. An agent's own computer
215
+ * (owner) is computer-<hex>-o<the owner label's hash> alone, with no deadline for the janitor to end it by: the run and
216
+ * the epoch of the claim that holds it change with each attach, so they are in the ledger (OrgoLedger), not the name.
217
+ */
218
+ export function orgoName(labels, hex, deadline, owner) {
219
+ if (owner !== undefined)
220
+ return `computer-${hex}-o${ownerHashOf(owner)}`;
221
+ const run = labels[RUN_LABEL];
222
+ const epoch = labels[EPOCH_LABEL];
223
+ const stable = `computer-${hex}${typeof run === "string" && run !== "" ? `-r${runHash(run)}` : ""}${typeof epoch === "string" && /^(?:0|[1-9][0-9]{0,15})$/.test(epoch) ? `-e${epoch}` : ""}`;
224
+ return deadline === undefined ? stable : withDeadline(stable, deadline);
225
+ }
226
+ /** A name's stable part with this deadline. */
227
+ export function withDeadline(stable, deadline) {
228
+ return `${stable}-d${Math.ceil(deadline.getTime() / 1000).toString(36)}`;
229
+ }
230
+ /** What a name of this provider's making says, or undefined for any other name: its stable part, and its deadline. */
231
+ export function nameParts(name) {
232
+ const match = typeof name === "string" ? NAME.exec(name) : null;
233
+ if (match === null)
234
+ return undefined;
235
+ const seconds = match[5] === undefined ? Number.NaN : Number.parseInt(match[5], 36);
236
+ return {
237
+ hex: match[1],
238
+ ...(match[2] ? { owner: match[2] } : {}),
239
+ ...(match[3] ? { run: match[3] } : {}),
240
+ ...(match[4] !== undefined ? { epoch: match[4] } : {}),
241
+ stable: name.replace(/-d[0-9a-z]+$/, ""),
242
+ ...(Number.isSafeInteger(seconds) ? { deadline: new Date(seconds * 1000) } : {}),
243
+ };
244
+ }
245
+ /** Whether two names are one computer's: their stable parts are the same (the deadline moves with extend). */
246
+ export const sameComputer = (a, b) => nameParts(a)?.stable !== undefined && nameParts(a)?.stable === nameParts(b)?.stable;
247
+ export function labelProblem(key, value) {
248
+ if (typeof value !== "string")
249
+ return `the label ${key} must be a string`;
250
+ if (key === "" || key.includes("\0") || value.includes("\0"))
251
+ return `the label ${JSON.stringify(key)} must be a non-empty name, and neither holds a NUL byte`;
252
+ if (key === COMPUTER_LABEL || key.startsWith(`${COMPUTER_LABEL}.`))
253
+ return `the label ${key} is in the provider's own namespace (${COMPUTER_LABEL})`;
254
+ if (key === OWNER_LABEL || key === OWNER_SERVER_LABEL || key === IDLE_TTL_LABEL)
255
+ return `the label ${key} is the provider's own: a spec gives its owner, and the provider labels the computer`;
256
+ return undefined;
257
+ }
258
+ export function filterProblem(labels) {
259
+ if (typeof labels !== "object" || labels === null || Array.isArray(labels))
260
+ return "labels must map names to strings";
261
+ for (const [key, value] of Object.entries(labels)) {
262
+ if (typeof value !== "string" || key === "" || key.includes("\0") || value.includes("\0"))
263
+ return `the label ${JSON.stringify(key)} must be a non-empty name and a string, with no NUL byte`;
264
+ }
265
+ return undefined;
266
+ }
267
+ /** Why ref is not an Orgo computer's ref, or undefined. */
268
+ export function refProblem(ref) {
269
+ if (typeof ref?.name !== "string" || nameParts(ref.name) === undefined)
270
+ return `${String(ref?.name)} is not the name of an Orgo computer of this provider (computer-<12 hex digits>...)`;
271
+ if (typeof ref.id !== "string" || !COMPUTER_ID.test(ref.id))
272
+ return `${String(ref.id)} is not an Orgo computer id`;
273
+ return undefined;
274
+ }
275
+ /** The labels file at path as written, or why it is not. */
276
+ export function labelsOf(text, path) {
277
+ try {
278
+ const value = JSON.parse(text);
279
+ if (typeof value?.name === "string" && typeof value.labels === "object" && value.labels !== null && typeof value.deadline === "string" && typeof value.maxDeadline === "string" && typeof value.image === "string") {
280
+ return value;
281
+ }
282
+ }
283
+ catch {
284
+ // Said below.
285
+ }
286
+ return `the labels file ${path} is not the one create writes`;
287
+ }
package/dist/orgo.d.ts CHANGED
@@ -1,14 +1,17 @@
1
1
  import type { KindCapabilities } from "./kinds.js";
2
2
  import { type MountKind } from "./mount.js";
3
3
  import { type OrgoChannel, type OrgoStart, type OrgoWebSocketFactory } from "./orgo-channel.js";
4
- import { type AttachedComputer, type CallContext, type ComputerCapabilities, type ComputerConnection, type ComputerInfo, type ComputerProvider, type ComputerRef, type ComputerSpec, type CreatedComputer, type NetworkForm, type NetworkPolicy, type NetworkSpec, type Refusal } from "./provider.js";
4
+ import { OrgoDesktopProvider, type OrgoDesktopOptions } from "./orgo-desktop.js";
5
+ import { type OrgoLedger } from "./orgo-names.js";
6
+ import { type AttachContext, type AttachedComputer, type CallContext, type ComputerCapabilities, type ComputerConnection, type ComputerInfo, type ComputerProvider, type ComputerRef, type ComputerSpec, type CreatedComputer, type NetworkPolicy, type NetworkSpec, type Refusal, type ReleaseContext, type ReleaseHow } from "./provider.js";
5
7
  import { type Result } from "./result.js";
6
8
  import { type ScreenWatch, type ScreenWatchHandlers, type WatchOptions } from "./screen-watch.js";
7
9
  /** The mount kinds an Orgo computer takes: none yet (an Archil mount needs an Orgo template that bakes the helper). */
8
10
  export declare const MOUNT: readonly MountKind[];
9
- /** The network an Orgo desktop's programs get: none, a network of their own. */
10
- export declare const NETWORK: readonly NetworkForm[];
11
- /** What a built provider declares, known without building one: one configuration, a Linux desktop (the registry's entry). */
11
+ /**
12
+ * What a built provider declares, known without building one, per configuration (its descriptor's): the Linux desktop
13
+ * with computerd (the default), then the Windows and the macOS desktops Orgo's own API answers (options.os).
14
+ */
12
15
  export declare const CAPABILITIES: readonly KindCapabilities[];
13
16
  /** The conformance CLI's module for Orgo: its live switch, the run's 1 h cap and the drifted twin. */
14
17
  export declare const conformance: () => Promise<typeof import("./conformance/orgo.js")>;
@@ -59,9 +62,12 @@ export interface OrgoProviderOptions {
59
62
  timeouts?: Partial<OrgoTimeouts>;
60
63
  /** The addresses the probe tries, host:port over TCP or host:port/udp, to show none of them answers. */
61
64
  probeTargets?: string[];
65
+ /**
66
+ * Where agents' own computers' labels are kept (the server's computer ledger, as its descriptor declares): a detached
67
+ * one is never woken to read its labels file. Default one in this process's memory.
68
+ */
69
+ ledger?: OrgoLedger;
62
70
  }
63
- /** Orgo's base desktop, no template: the image a spec names for a plain Linux desktop, and the default for kind desktop. */
64
- export declare const ORGO_BASE_IMAGE = "linux";
65
71
  /** The labels file: every label, the deadline and its most, the image and sizes, root's, readable by all. */
66
72
  export declare const LABELS_FILE = "/opt/agentrun/labels.json";
67
73
  /**
@@ -74,13 +80,6 @@ export declare const LABELS_FILE = "/opt/agentrun/labels.json";
74
80
  * nothing of its own loses a socket.
75
81
  */
76
82
  export declare const LOCKDOWN_SCRIPT: string;
77
- /**
78
- * The name of a computer with these labels: computer-<hex>, then -r<the run label's hash> and -e<the epoch label> when
79
- * the labels carry a run and an epoch that is one, then -d<the deadline> when given: whole seconds since the epoch,
80
- * rounded up (never before init's own), in base 36, the janitor's deadline. Only those: a name is no place for a label's
81
- * value. Everything but the deadline is the name's stable part, which a rename on extend keeps.
82
- */
83
- export declare function orgoName(labels: Record<string, string>, hex: string, deadline?: Date): string;
84
83
  /**
85
84
  * Orgo desktops: see the module's header. A spec's image is "linux" (Orgo's base desktop) or an Orgo template
86
85
  * (namespace/name@version); cpus and memoryMb are Orgo's sizes; the network is none.
@@ -89,13 +88,14 @@ export declare class OrgoProvider implements ComputerProvider {
89
88
  #private;
90
89
  readonly id = "orgo";
91
90
  readonly version: string;
92
- constructor({ apiKey, workspaceId, apiUrl, fetch, webSocket, channel, daemonPath, maxLifetimeMs, user, start, display, timeouts, probeTargets }: OrgoProviderOptions);
91
+ constructor({ apiKey, workspaceId, apiUrl, fetch, webSocket, channel, daemonPath, maxLifetimeMs, user, start, display, timeouts, probeTargets, ledger }: OrgoProviderOptions);
93
92
  /**
94
93
  * daemon child: Orgo's own agent is the VM's first process, and init is a command it ran. lifetime janitor: Orgo ends
95
94
  * a computer only in whole hours (ends_in_hours, the backstop), so the janitor deletes it at its exact deadline, which
96
95
  * its name carries (list() reads it with nothing run inside); init ends its programs at that deadline inside. network
97
- * none alone, read back by the probe from inside (Orgo reads no policy back). release delete alone: Orgo's stop
98
- * freezes a computer, which nothing here needs.
96
+ * none alone, read back by the probe from inside (Orgo reads no policy back). release delete, and detach for an
97
+ * agent's own computer: Orgo's suspend saves its memory and disk, so it resumes where it was (persistence resume),
98
+ * computerd init with it.
99
99
  */
100
100
  capabilities(): ComputerCapabilities;
101
101
  check(spec: ComputerSpec): Refusal[];
@@ -107,7 +107,7 @@ export declare class OrgoProvider implements ComputerProvider {
107
107
  id: string;
108
108
  } | {
109
109
  labels: Record<string, string>;
110
- }, { signal }?: CallContext): Promise<Result<AttachedComputer>>;
110
+ }, { signal, claim }?: AttachContext): Promise<Result<AttachedComputer>>;
111
111
  /**
112
112
  * Every computer of this provider in the workspace that carries all of these labels. The run and the epoch are read
113
113
  * from the name, with nothing run in a computer, so a stopped one is listed too; any other label is read from the
@@ -120,8 +120,12 @@ export declare class OrgoProvider implements ComputerProvider {
120
120
  detail: string;
121
121
  }>>;
122
122
  network(ref: ComputerRef, policy?: NetworkSpec, { signal }?: CallContext): Promise<Result<NetworkPolicy>>;
123
- /** delete: idempotent, a computer Orgo no longer has is done; it waits until Orgo no longer has it. No stop. */
124
- release(ref: ComputerRef, how: "stop" | "delete", { signal }?: CallContext): Promise<Result<void>>;
123
+ /**
124
+ * delete: idempotent, a computer Orgo no longer has is done; it waits until Orgo no longer has it. detach, for an
125
+ * agent's own computer alone: Orgo's suspend, its memory and disk saved, read back suspended. No stop: a run's
126
+ * computer is deleted.
127
+ */
128
+ release(ref: ComputerRef, how: ReleaseHow, { signal, claim }?: ReleaseContext): Promise<Result<void>>;
125
129
  /**
126
130
  * Moves the deadline to now + ttlMs, never earlier and never past the most the labels file holds (create's time plus
127
131
  * the cap): Orgo's end first, in whole hours with an Idempotency-Key, so it never ends the computer before init does;
@@ -137,5 +141,15 @@ export declare class OrgoProvider implements ComputerProvider {
137
141
  * An OrgoProvider from its options alone, its key included: it reads no environment. Serve passes apiKey from its
138
142
  * ORGO_API_KEY setting and its workspace; the conformance module reads COMPUTER_ORGO_* (settingsFromEnv).
139
143
  */
140
- export declare function create(options: OrgoProviderOptions): OrgoProvider;
144
+ export declare function create(options: OrgoProviderOptions & {
145
+ os?: "linux";
146
+ }): OrgoProvider;
147
+ export declare function create(options: OrgoDesktopOptions & {
148
+ os: "windows" | "macos";
149
+ }): OrgoDesktopProvider;
150
+ export declare function create(options: (OrgoProviderOptions & {
151
+ os?: "linux";
152
+ }) | (OrgoDesktopOptions & {
153
+ os: "windows" | "macos";
154
+ })): OrgoProvider | OrgoDesktopProvider;
141
155
  export type { OrgoChannel, OrgoStart, OrgoStream, OrgoWebSocketFactory } from "./orgo-channel.js";