@parcha/agentrun-computer 0.1.0-beta.63 → 0.1.0-beta.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -5
- package/dist/agent37.d.ts +1 -0
- package/dist/agent37.js +15 -1
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/computerd.json +2 -2
- package/dist/host.d.ts +10 -3
- package/dist/hosted/provider.d.ts +6 -4
- package/dist/hosted/provider.js +33 -14
- package/dist/modal.js +22 -5
- package/dist/provider.d.ts +3 -1
- package/dist/render.js +67 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -616,11 +616,13 @@ by its rectangle), so a longer one ends the connection before anything is alloca
|
|
|
616
616
|
`/usr/local/share` and `/usr/share`), as the Desktop Entry spec says: the apps there are, by their desktop file ids
|
|
617
617
|
(`org.xfce.mousepad`), with their names; hidden and `NoDisplay` entries, and a `TryExec` that is not there, are left
|
|
618
618
|
out. So is an app whose desktop file computerd's own user could change: on the way to it from `/`, every symbolic
|
|
619
|
-
link followed, the user owns a folder, a link or the file, or the mode bits
|
|
620
|
-
one of them (a sticky folder only where it owns what is in it), or
|
|
621
|
-
file that walk checked, opened without following a link and
|
|
622
|
-
each launch. An app runs what its desktop file says, so such a
|
|
623
|
-
with exec off: `apps.launch` and `apps.terminate` refuse such an
|
|
619
|
+
link followed, the user owns a folder, a link or the file, or the mode bits or the kernel (`faccessat2`, which reads
|
|
620
|
+
ACLs) let it write the file or a folder holding one of them (a sticky folder only where it owns what is in it), or
|
|
621
|
+
computerd runs as root. The entry is read from the file that walk checked, opened without following a link and
|
|
622
|
+
refused if another took its place, and read again for each launch. An app runs what its desktop file says, so such a
|
|
623
|
+
file, written through `files`, would run any command with exec off: `apps.launch` and `apps.terminate` refuse such an
|
|
624
|
+
app by its id as `denied`, naming the file and why (a refused file is not read, so its name finds nothing), though
|
|
625
|
+
`apps.terminate` still ends one this computerd launched before its file was refused. Its `foreground` is the
|
|
624
626
|
app of the window manager's active window, else of the top window. `apps.launch({ id, url? })` takes an id, a name
|
|
625
627
|
(`Mousepad`) or a program (`mousepad`), starts the entry's `Exec` (`url` in its `%u`/`%f` field; an entry with none
|
|
626
628
|
refuses a `url` as `invalid`; a `url` is an absolute URL with a scheme, `https://…` or `file:///…`, or an absolute
|
package/dist/agent37.d.ts
CHANGED
package/dist/agent37.js
CHANGED
|
@@ -181,7 +181,20 @@ function stateOf(status) {
|
|
|
181
181
|
return "gone";
|
|
182
182
|
return undefined;
|
|
183
183
|
}
|
|
184
|
-
/**
|
|
184
|
+
/** A positive finite figure of Agent37's answer, else undefined (absent, zero or not a number: never guessed). */
|
|
185
|
+
const figure = (value) => (typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined);
|
|
186
|
+
/** What Agent37 says an instance was given (Machine.resources), the shape it bills by: its vCPUs, its memory (GB, as MB)
|
|
187
|
+
* and its disk (GB), each where its answer says it (docs: instances/object, resources); none where it says nothing. */
|
|
188
|
+
function resourcesOf(given) {
|
|
189
|
+
const [cpus, memory, disk] = [figure(given?.cpu), figure(given?.memory), figure(given?.disk)];
|
|
190
|
+
const resources = {
|
|
191
|
+
...(cpus === undefined ? {} : { cpus }),
|
|
192
|
+
...(memory === undefined ? {} : { memoryMb: memory * 1024 }),
|
|
193
|
+
...(disk === undefined ? {} : { diskGb: disk }),
|
|
194
|
+
};
|
|
195
|
+
return Object.keys(resources).length > 0 ? { resources } : {};
|
|
196
|
+
}
|
|
197
|
+
/** An instance as the adapter reads a machine: its state (a status it does not know is gone, in words), labels, start, size. */
|
|
185
198
|
function machineOf(instance) {
|
|
186
199
|
const state = stateOf(instance.status);
|
|
187
200
|
// A failed instance always carries a why, Agent37's or ours, so create stops on it as vendor, never as gone.
|
|
@@ -194,6 +207,7 @@ function machineOf(instance) {
|
|
|
194
207
|
state: state ?? "gone",
|
|
195
208
|
...(created ? { createdAt: created } : {}),
|
|
196
209
|
...(why === undefined || (state !== undefined && state !== "gone") ? {} : { why }),
|
|
210
|
+
...resourcesOf(instance.resources),
|
|
197
211
|
};
|
|
198
212
|
}
|
|
199
213
|
/** The shape a request's cpus and memoryMb ask for, or undefined when they ask for none of Agent37's. */
|
|
Binary file
|
|
Binary file
|
package/dist/computerd.json
CHANGED
package/dist/host.d.ts
CHANGED
|
@@ -215,8 +215,9 @@ export interface Machine {
|
|
|
215
215
|
createdAt?: Date;
|
|
216
216
|
/** The vendor's words for a machine that is gone because it failed: a create stops in them. */
|
|
217
217
|
why?: string;
|
|
218
|
-
/** What the vendor's answer says the machine was given (its CPUs, memory, disk, GPUs), where it says it
|
|
219
|
-
*
|
|
218
|
+
/** What the vendor's answer says the machine was given (its CPUs, memory, disk, GPUs), where it says it, or the
|
|
219
|
+
* limits its create set where the vendor reads none back and bills by use up to them (ComputerResources): a create
|
|
220
|
+
* returns these as its computer's resources. Absent where neither says any. */
|
|
220
221
|
resources?: ComputerResources;
|
|
221
222
|
}
|
|
222
223
|
/** A process exec started: its output as it comes, its stdin, and its end. */
|
|
@@ -268,7 +269,12 @@ export interface HostFacts {
|
|
|
268
269
|
* list by, the map in parts of at most valueBytes each), unpacks those get and list answer, and asks list for that
|
|
269
270
|
* name alone; the host stores and answers them as they are. count and bytes are then the packed labels'. unpacked:
|
|
270
271
|
* the host's machines may also carry their labels as given (made before it packed): one with no packed map reads as
|
|
271
|
-
* its stored labels, and a listing by name asks for the labels as given beside the packed name.
|
|
272
|
+
* its stored labels, and a listing by name asks for the labels as given beside the packed name. named: the host's
|
|
273
|
+
* machine names carry the run and epoch labels beside the adapter's name (Render's), so a create's sweeps for the
|
|
274
|
+
* machine it made (the wait to be listed, the sweep after a failed create) ask for its run and epoch too where its
|
|
275
|
+
* spec has both, and the host can look up the one whole name; the machines kept are still those whose get carries the
|
|
276
|
+
* name's labels. Its first check and convergence ask by the name alone: they look for the request's machine whatever
|
|
277
|
+
* run or epoch it was made with.
|
|
272
278
|
*/
|
|
273
279
|
labels?: {
|
|
274
280
|
count?: number;
|
|
@@ -277,6 +283,7 @@ export interface HostFacts {
|
|
|
277
283
|
valueBytes?: number;
|
|
278
284
|
unpacked?: true;
|
|
279
285
|
};
|
|
286
|
+
named?: true;
|
|
280
287
|
};
|
|
281
288
|
/**
|
|
282
289
|
* Paths of the vendor's image that root runs or reads and whose permissions its template build sets wider than
|
|
@@ -186,15 +186,17 @@ export declare class HostedProvider implements ComputerProvider {
|
|
|
186
186
|
* may answer more (MachineHost.list). A packed host is asked for PACKED_NAME alone, the name's where one is asked, and
|
|
187
187
|
* its machines' labels unpacked. A listing by name on a host whose machines may also carry their labels as given
|
|
188
188
|
* (facts.labels.packed.unpacked) asks both ways, one after the other within ms together, and answers every machine
|
|
189
|
-
* either finds: across an upgrade one request can make one machine of each form, and convergence must see both.
|
|
189
|
+
* either finds: across an upgrade one request can make one machine of each form, and convergence must see both. `also`:
|
|
190
|
+
* labels a host that doesn't pack them is asked beside these, which only narrow its search (nameAsk): never required of
|
|
191
|
+
* a machine.
|
|
190
192
|
*/
|
|
191
|
-
export declare function listedBy(host: MachineHost, what: string, labels: Record<string, string>, ms: number, signal?: AbortSignal): Promise<Result<Machine[]>>;
|
|
193
|
+
export declare function listedBy(host: MachineHost, what: string, labels: Record<string, string>, ms: number, signal?: AbortSignal, also?: Record<string, string>): Promise<Result<Machine[]>>;
|
|
192
194
|
/**
|
|
193
195
|
* The listing finds, get answers (MachineHost.list): the machines the host's listing by labels names, each read now by
|
|
194
196
|
* readMachine, within requestMs. One the vendor no longer has (not found) is dropped, and so is one whose get carries
|
|
195
|
-
* not every label. A get that fails fails the whole listing.
|
|
197
|
+
* not every label. A get that fails fails the whole listing. `also`: as listedBy's.
|
|
196
198
|
*/
|
|
197
|
-
export declare function foundBy(host: MachineHost, what: string, labels: Record<string, string>, { listMs, requestMs }: Pick<HostedTimeouts, "listMs" | "requestMs">, signal?: AbortSignal): Promise<Result<Machine[]>>;
|
|
199
|
+
export declare function foundBy(host: MachineHost, what: string, labels: Record<string, string>, { listMs, requestMs }: Pick<HostedTimeouts, "listMs" | "requestMs">, signal?: AbortSignal, also?: Record<string, string>): Promise<Result<Machine[]>>;
|
|
198
200
|
/**
|
|
199
201
|
* Waits, through waitUntil, until get answers none for a machine after its delete, twice running: the vendor no longer
|
|
200
202
|
* has it. A machine read gone is still going (a vendor still destroying it), so it holds the wait like any other state.
|
package/dist/hosted/provider.js
CHANGED
|
@@ -554,6 +554,8 @@ export class HostedProvider {
|
|
|
554
554
|
}
|
|
555
555
|
/** taken: the vendor refused this create's name as one a machine has (exists), so the name's machine is read once more. */
|
|
556
556
|
async #createOnce(spec, name, signal, taken) {
|
|
557
|
+
// By the request's name alone, never narrowed by this spec's run and epoch (nameAsk): the request's computer made
|
|
558
|
+
// under another run or epoch is found, and refused as exists by #again, never made twice.
|
|
557
559
|
const listed = await foundBy(this.host, "the listing by the request's name", byName(name), this.#timeouts, signal);
|
|
558
560
|
if (!listed.ok)
|
|
559
561
|
return listed;
|
|
@@ -629,6 +631,8 @@ export class HostedProvider {
|
|
|
629
631
|
* more than one machine holds the name, the oldest (createdAt, or the lowest id where any has none) is kept, and every
|
|
630
632
|
* other is deleted and read back gone, its Archil folder let go first. Every instance picks the same one from the
|
|
631
633
|
* same listing. Its value is the machine kept, or undefined when mine holds the name alone (or nothing is listed yet).
|
|
634
|
+
* The listing is by the name alone, never narrowed (nameAsk): two creates of one request that ask another run or epoch
|
|
635
|
+
* must still see each other's machine, so one is kept and the other answered exists.
|
|
632
636
|
*/
|
|
633
637
|
async #converge(name, mine, signal) {
|
|
634
638
|
const listed = await foundBy(this.host, "the listing by the request's name", byName(name), this.#timeouts, signal);
|
|
@@ -791,7 +795,7 @@ export class HostedProvider {
|
|
|
791
795
|
const { createMs } = this.#timeouts;
|
|
792
796
|
// Never signalled, only raced: an aborted call could leave a machine made whose id never came back. One that lands
|
|
793
797
|
// after this gave up is deleted by its id; one that fails after it, by its name where it may have made one.
|
|
794
|
-
const created = await hostCall(this.host, `${this.#label}'s create`, (ctx) => this.host.create(request, { timeoutMs: ctx.timeoutMs }), createMs, signal, (made) => made.ok ? this.#kill(made.value.id) : mayHaveMade(made.error.code) ? this.#deleteNamed(name) : undefined);
|
|
798
|
+
const created = await hostCall(this.host, `${this.#label}'s create`, (ctx) => this.host.create(request, { timeoutMs: ctx.timeoutMs }), createMs, signal, (made) => made.ok ? this.#kill(made.value.id) : mayHaveMade(made.error.code) ? this.#deleteNamed(name, nameAsk(this.host, spec)) : undefined);
|
|
795
799
|
if (!created.ok) {
|
|
796
800
|
// A machine of this create's that lands after this never dials: its token goes with the create.
|
|
797
801
|
await this.#revoke(name);
|
|
@@ -803,7 +807,7 @@ export class HostedProvider {
|
|
|
803
807
|
if (!mayHaveMade(code))
|
|
804
808
|
return created;
|
|
805
809
|
this.#end(name);
|
|
806
|
-
const swept = await this.#deleteNamed(name);
|
|
810
|
+
const swept = await this.#deleteNamed(name, nameAsk(this.host, spec));
|
|
807
811
|
return this.#fail(code, `${this.#label} did not create the machine: ${message}${swept}`);
|
|
808
812
|
}
|
|
809
813
|
this.#requests.made(name);
|
|
@@ -855,14 +859,15 @@ export class HostedProvider {
|
|
|
855
859
|
* Every machine carrying a name create gave, deleted and read back gone: for a create that may have made one whose
|
|
856
860
|
* id never came (mayHaveMade), never after a refusal, where a machine under the name is another's. The
|
|
857
861
|
* listing shows a new machine a moment after the vendor makes it, so it is read for the name until one shows or
|
|
858
|
-
* listedMs passes, as create's own wait reads it. Best effort, in words.
|
|
862
|
+
* listedMs passes, as create's own wait reads it. Best effort, in words. `also`: this create's run and epoch
|
|
863
|
+
* (nameAsk), which a machine it made carries in its name.
|
|
859
864
|
*/
|
|
860
|
-
async #deleteNamed(name) {
|
|
865
|
+
async #deleteNamed(name, also) {
|
|
861
866
|
const { listedMs, pollMs } = this.#timeouts;
|
|
862
867
|
const until = Date.now() + listedMs;
|
|
863
868
|
let found;
|
|
864
869
|
for (let delay = Math.min(LISTED_POLL_MS, pollMs);; delay = Math.min(delay * 2, pollMs)) {
|
|
865
|
-
const listed = await foundBy(this.host, "the listing by name", byName(name), this.#timeouts);
|
|
870
|
+
const listed = await foundBy(this.host, "the listing by name", byName(name), this.#timeouts, undefined, also);
|
|
866
871
|
if (!listed.ok)
|
|
867
872
|
return `; a machine named ${name} may be left (${listed.error.message}), which ends at its deadline`;
|
|
868
873
|
found = listed.value.filter((machine) => ours(machine.labels, name));
|
|
@@ -1042,7 +1047,7 @@ export class HostedProvider {
|
|
|
1042
1047
|
finally {
|
|
1043
1048
|
await opened.value.close();
|
|
1044
1049
|
}
|
|
1045
|
-
const listed = await this.#waitListed(ref, signal);
|
|
1050
|
+
const listed = await this.#waitListed(ref, signal, nameAsk(this.host, spec));
|
|
1046
1051
|
return listed.ok ? ok([...closed, ...dialNotes, ...notes, ...listed.value]) : listed;
|
|
1047
1052
|
}
|
|
1048
1053
|
/** What init wrote to its log (child) or the vendor's words of the machine (pid1): the end of it, or nothing. */
|
|
@@ -1344,14 +1349,14 @@ export class HostedProvider {
|
|
|
1344
1349
|
* The polls are 150 ms apart at first, the pause doubling up to pollMs, so a vendor slow to list costs a few calls.
|
|
1345
1350
|
* A listing that has not settled by listedMs is no failure alone (#unlisted): the computer is read by its id.
|
|
1346
1351
|
*/
|
|
1347
|
-
async #waitListed(ref, signal) {
|
|
1352
|
+
async #waitListed(ref, signal, also) {
|
|
1348
1353
|
const { listedMs, listMs, pollMs, listedStableMs } = this.#timeouts;
|
|
1349
1354
|
const began = Date.now();
|
|
1350
1355
|
const until = began + listedMs;
|
|
1351
1356
|
let since;
|
|
1352
1357
|
const seen = { polls: 0, shown: 0 };
|
|
1353
1358
|
for (let hits = 0, delay = Math.min(LISTED_POLL_MS, pollMs);; delay = Math.min(delay * 2, pollMs)) {
|
|
1354
|
-
const listed = await listedBy(this.host, "the listing by name", byName(ref.name), listMs, signal);
|
|
1359
|
+
const listed = await listedBy(this.host, "the listing by name", byName(ref.name), listMs, signal, also);
|
|
1355
1360
|
if (!listed.ok)
|
|
1356
1361
|
return listed;
|
|
1357
1362
|
seen.polls += 1;
|
|
@@ -1767,6 +1772,18 @@ export class HostedProvider {
|
|
|
1767
1772
|
}
|
|
1768
1773
|
/** The labels a listing by one computer's name asks for. */
|
|
1769
1774
|
const byName = (name) => ({ [COMPUTER_LABEL]: "1", [NAME_LABEL]: name });
|
|
1775
|
+
/**
|
|
1776
|
+
* What a listing by a create's name asks beside byName on a host whose names carry the run and epoch (facts.labels.named):
|
|
1777
|
+
* the spec's run and epoch, where it has both, so the host can look up the whole name. Only asked, never required: a
|
|
1778
|
+
* machine of the name is kept whatever its get says of them (a sandbox before its labels file, read by its name alone).
|
|
1779
|
+
* Only the sweeps for a machine this create made ask it (#waitListed, #deleteNamed): that machine's name is the whole
|
|
1780
|
+
* name. The first check and #converge look for any machine of the request, whatever run or epoch it was made with.
|
|
1781
|
+
*/
|
|
1782
|
+
function nameAsk(host, spec) {
|
|
1783
|
+
const run = spec.labels?.[RUN_LABEL];
|
|
1784
|
+
const epoch = spec.labels?.[EPOCH_LABEL];
|
|
1785
|
+
return host.facts.labels?.named && typeof run === "string" && typeof epoch === "string" ? { [RUN_LABEL]: run, [EPOCH_LABEL]: epoch } : undefined;
|
|
1786
|
+
}
|
|
1770
1787
|
/** The room of a host that keeps its labels packed (facts.labels.packed), or undefined. */
|
|
1771
1788
|
function packedRoom(host) {
|
|
1772
1789
|
const room = host.facts.labels;
|
|
@@ -1787,12 +1804,14 @@ function unpacked(host, machine) {
|
|
|
1787
1804
|
* may answer more (MachineHost.list). A packed host is asked for PACKED_NAME alone, the name's where one is asked, and
|
|
1788
1805
|
* its machines' labels unpacked. A listing by name on a host whose machines may also carry their labels as given
|
|
1789
1806
|
* (facts.labels.packed.unpacked) asks both ways, one after the other within ms together, and answers every machine
|
|
1790
|
-
* either finds: across an upgrade one request can make one machine of each form, and convergence must see both.
|
|
1807
|
+
* either finds: across an upgrade one request can make one machine of each form, and convergence must see both. `also`:
|
|
1808
|
+
* labels a host that doesn't pack them is asked beside these, which only narrow its search (nameAsk): never required of
|
|
1809
|
+
* a machine.
|
|
1791
1810
|
*/
|
|
1792
|
-
export async function listedBy(host, what, labels, ms, signal) {
|
|
1811
|
+
export async function listedBy(host, what, labels, ms, signal, also) {
|
|
1793
1812
|
const name = labels[NAME_LABEL];
|
|
1794
1813
|
const room = packedRoom(host);
|
|
1795
|
-
const asks = room === undefined ? [labels] : name === undefined ? [{}] : room.packed?.unpacked ? [{ [PACKED_NAME]: name }, labels] : [{ [PACKED_NAME]: name }];
|
|
1814
|
+
const asks = room === undefined ? [{ ...also, ...labels }] : name === undefined ? [{}] : room.packed?.unpacked ? [{ [PACKED_NAME]: name }, labels] : [{ [PACKED_NAME]: name }];
|
|
1796
1815
|
const until = Date.now() + ms;
|
|
1797
1816
|
const found = new Map();
|
|
1798
1817
|
for (const asked of asks) {
|
|
@@ -1810,10 +1829,10 @@ const carries = (machine, labels) => Object.entries(labels).every(([key, value])
|
|
|
1810
1829
|
/**
|
|
1811
1830
|
* The listing finds, get answers (MachineHost.list): the machines the host's listing by labels names, each read now by
|
|
1812
1831
|
* readMachine, within requestMs. One the vendor no longer has (not found) is dropped, and so is one whose get carries
|
|
1813
|
-
* not every label. A get that fails fails the whole listing.
|
|
1832
|
+
* not every label. A get that fails fails the whole listing. `also`: as listedBy's.
|
|
1814
1833
|
*/
|
|
1815
|
-
export async function foundBy(host, what, labels, { listMs, requestMs }, signal) {
|
|
1816
|
-
const listed = await listedBy(host, what, labels, listMs, signal);
|
|
1834
|
+
export async function foundBy(host, what, labels, { listMs, requestMs }, signal, also) {
|
|
1835
|
+
const listed = await listedBy(host, what, labels, listMs, signal, also);
|
|
1817
1836
|
if (!listed.ok)
|
|
1818
1837
|
return listed;
|
|
1819
1838
|
const reads = await Promise.all(listed.value.map(({ id }) => readMachine(host, id, requestMs, signal)));
|
package/dist/modal.js
CHANGED
|
@@ -89,6 +89,15 @@ const CPU_REQUEST = 0.125;
|
|
|
89
89
|
const MEMORY_REQUEST_MIB = 128;
|
|
90
90
|
/** "1 core = 2 vCPUs" (guide/sandbox-resources): a spec's cpus are CPUs, Modal's cpu is physical cores. */
|
|
91
91
|
const CPUS_PER_CORE = 2;
|
|
92
|
+
/**
|
|
93
|
+
* A sandbox's size, as its computer is priced: the limits its create set (`cpuLimit` in cores, as vCPUs, and
|
|
94
|
+
* `memoryLimitMiB`), read from the very params the create sends. Modal reads no size back, and bills a sandbox by
|
|
95
|
+
* max(request, use) up to those limits, so the limit is the most its time can cost (ComputerResources).
|
|
96
|
+
*/
|
|
97
|
+
const sizeOf = (params) => ({
|
|
98
|
+
...(params.cpuLimit === undefined ? {} : { cpus: params.cpuLimit * CPUS_PER_CORE }),
|
|
99
|
+
...(params.memoryLimitMiB === undefined ? {} : { memoryMb: params.memoryLimitMiB }),
|
|
100
|
+
});
|
|
92
101
|
/**
|
|
93
102
|
* Modal's bounds over the adapter's: a create builds the image layer and waits for the sandbox to be scheduled, a
|
|
94
103
|
* terminate is asynchronous, and the daemon is streamed in through exec.
|
|
@@ -121,6 +130,9 @@ class ModalHost {
|
|
|
121
130
|
#appName;
|
|
122
131
|
/** Each sandbox create or a read gave, by id: let go of at its delete. */
|
|
123
132
|
#sandboxes = new Map();
|
|
133
|
+
/** Each sandbox's size, by id, from the limits its create set (sizeOf): this provider's creates alone, let go of at
|
|
134
|
+
* its delete. A sandbox another process made has none here, and is read with no size. */
|
|
135
|
+
#sizes = new Map();
|
|
124
136
|
#app;
|
|
125
137
|
constructor(sdk, app, connect) {
|
|
126
138
|
this.#sdk = sdk;
|
|
@@ -230,12 +242,13 @@ class ModalHost {
|
|
|
230
242
|
const image = base.dockerfileCommands([`ENV HOME=${WORK}`, `WORKDIR ${WORK}`]);
|
|
231
243
|
const sandbox = await this.#sdk.sandboxes.create((await this.#theApp(true)), image, params);
|
|
232
244
|
this.#sandboxes.set(sandbox.sandboxId, sandbox);
|
|
245
|
+
this.#sizes.set(sandbox.sandboxId, sizeOf(params));
|
|
233
246
|
return { id: sandbox.sandboxId };
|
|
234
247
|
}
|
|
235
248
|
/**
|
|
236
|
-
* The sandbox as a machine: running while poll reads null (sandbox.ts:2761-2771), its labels from its tags
|
|
237
|
-
*
|
|
238
|
-
* size or GPU back.
|
|
249
|
+
* The sandbox as a machine: running while poll reads null (sandbox.ts:2761-2771), its labels from its tags, its size
|
|
250
|
+
* the limits its create set (sizeOf) where this provider made it. One that has finished, or that Modal no longer
|
|
251
|
+
* knows, is none: a finished sandbox cannot run again. Modal reads no network, size or GPU back.
|
|
239
252
|
*/
|
|
240
253
|
async get(id, ctx) {
|
|
241
254
|
void ctx;
|
|
@@ -246,7 +259,8 @@ class ModalHost {
|
|
|
246
259
|
try {
|
|
247
260
|
if ((await sandbox.poll()) !== null)
|
|
248
261
|
return undefined;
|
|
249
|
-
|
|
262
|
+
const size = this.#sizes.get(sandbox.sandboxId);
|
|
263
|
+
return { id: sandbox.sandboxId, labels: await sandbox.getTags(), state: "running", ...(size === undefined ? {} : { resources: size }) };
|
|
250
264
|
}
|
|
251
265
|
catch (error) {
|
|
252
266
|
if (codeOf(error) === "not_found")
|
|
@@ -290,7 +304,10 @@ class ModalHost {
|
|
|
290
304
|
*/
|
|
291
305
|
async delete(id, ctx) {
|
|
292
306
|
void ctx;
|
|
293
|
-
const sandbox = await this.#sandbox(id).finally(() =>
|
|
307
|
+
const sandbox = await this.#sandbox(id).finally(() => {
|
|
308
|
+
this.#sandboxes.delete(id);
|
|
309
|
+
this.#sizes.delete(id);
|
|
310
|
+
});
|
|
294
311
|
try {
|
|
295
312
|
await sandbox.terminate();
|
|
296
313
|
}
|
package/dist/provider.d.ts
CHANGED
|
@@ -300,7 +300,9 @@ export interface ReadBackSpec {
|
|
|
300
300
|
export type ReadBackNotes = string[];
|
|
301
301
|
/**
|
|
302
302
|
* What a computer was given, as its vendor's own answer for it says (its get or create answer): the figures the vendor
|
|
303
|
-
* bills by. Each field is absent where the vendor's answer does not say it, never guessed from what was asked.
|
|
303
|
+
* bills by. Each field is absent where the vendor's answer does not say it, never guessed from what was asked. One
|
|
304
|
+
* exception: a provider whose vendor reads no size back and bills a computer by its use, up to limits its create set
|
|
305
|
+
* (Modal), gives those limits, the most the computer's time can bill.
|
|
304
306
|
*/
|
|
305
307
|
export interface ComputerResources {
|
|
306
308
|
cpus?: number;
|
package/dist/render.js
CHANGED
|
@@ -69,11 +69,43 @@ const BUILD_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,254}$/;
|
|
|
69
69
|
* (S, sandboxPOST name). Only a sandbox named so is one of its computers.
|
|
70
70
|
*/
|
|
71
71
|
const SANDBOX_NAME = /^(computer-[0-9a-f]{12})(?:-r([a-z2-7]{16}))?(?:-e(0|[1-9][0-9]{0,15}))?$/;
|
|
72
|
-
/**
|
|
73
|
-
function
|
|
72
|
+
/** The run's hash a sandbox of these labels is named with: "" for none. */
|
|
73
|
+
function runPart(labels) {
|
|
74
74
|
const run = labels[RUN_LABEL];
|
|
75
|
+
return typeof run === "string" && run !== "" ? runHash(run) : "";
|
|
76
|
+
}
|
|
77
|
+
/** The epoch a sandbox of these labels is named with: "" for none, or one that is not a whole number. */
|
|
78
|
+
function epochPart(labels) {
|
|
75
79
|
const epoch = labels[EPOCH_LABEL];
|
|
76
|
-
return
|
|
80
|
+
return typeof epoch === "string" && /^(?:0|[1-9][0-9]{0,15})$/.test(epoch) ? epoch : "";
|
|
81
|
+
}
|
|
82
|
+
/** A computer's sandbox name for create's request: its name, then its run label's hash and its epoch label where they are one. */
|
|
83
|
+
function sandboxName({ name, labels }) {
|
|
84
|
+
const run = runPart(labels);
|
|
85
|
+
const epoch = epochPart(labels);
|
|
86
|
+
return `${name}${run === "" ? "" : `-r${run}`}${epoch === "" ? "" : `-e${epoch}`}`;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The whole sandbox name a listing asks for, where it asks the adapter's name, a run and a whole-number epoch: Render's
|
|
90
|
+
* own exact name filter then finds it, no page of the workspace's other sandboxes read. Undefined where it asks less.
|
|
91
|
+
*/
|
|
92
|
+
function wholeName(labels) {
|
|
93
|
+
const name = labels[NAME_LABEL];
|
|
94
|
+
if (name === undefined || runPart(labels) === "" || epochPart(labels) === "")
|
|
95
|
+
return undefined;
|
|
96
|
+
return sandboxName({ name, labels });
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Where a labels file says another run or epoch than its sandbox's name, why, in words; undefined where it agrees. The
|
|
100
|
+
* name is Render's, set at create; the file is the guest's root's to rewrite, so one that disagrees is not read.
|
|
101
|
+
*/
|
|
102
|
+
function misnamed(parts, file) {
|
|
103
|
+
const found = [];
|
|
104
|
+
if ((parts.run ?? "") !== runPart(file))
|
|
105
|
+
found.push(`run: the name's run hash ${show(parts.run)} is not the labels file's run ${show(file[RUN_LABEL])}`);
|
|
106
|
+
if ((parts.epoch ?? "") !== epochPart(file))
|
|
107
|
+
found.push(`epoch: the name says ${show(parts.epoch)}, the labels file ${show(file[EPOCH_LABEL])}`);
|
|
108
|
+
return found.length === 0 ? undefined : found.join("; ");
|
|
77
109
|
}
|
|
78
110
|
/** What a sandbox's name says: the adapter's name, the run's hash and the epoch; undefined for a sandbox not named as a computer. */
|
|
79
111
|
function nameParts(name) {
|
|
@@ -245,6 +277,9 @@ class RenderHost {
|
|
|
245
277
|
// The sandbox dials the server: Render's runs take no stdin (S: runs/{sync,stream} take { command }), so there is
|
|
246
278
|
// no exec and no upload here. Its descriptor's (connectOf).
|
|
247
279
|
connect: RENDER.connect,
|
|
280
|
+
// A sandbox's name carries its run's hash and its epoch (sandboxName): a create's sweeps for the sandbox it made ask
|
|
281
|
+
// for both, so list asks Render for the whole name rather than reading the workspace's every page.
|
|
282
|
+
labels: { named: true },
|
|
248
283
|
};
|
|
249
284
|
#api;
|
|
250
285
|
#apiKey;
|
|
@@ -296,20 +331,23 @@ class RenderHost {
|
|
|
296
331
|
}
|
|
297
332
|
/**
|
|
298
333
|
* A sandbox's labels: the ones this process knows, else its name's, else its labels file's, read once through runs/sync
|
|
299
|
-
* and kept. `need`: a listing's labels; when the name's hold every one asked, no file is read.
|
|
300
|
-
* computer
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
334
|
+
* and kept. `need`: a listing's labels; when the name's hold every one asked, no file is read. The name's are the
|
|
335
|
+
* computer's and its name, and, for a listing by the whole name (wholeName), the run and epoch asked, which that name
|
|
336
|
+
* is made of (the run by its 80-bit hash). A sandbox not named as a computer carries none; one not running, one
|
|
337
|
+
* terminated from this process, or one with no file yet, its name's. A file whose run or epoch is not the name's
|
|
338
|
+
* (misnamed) is not read: the guest's root rewrote it. The read is held to `end` (epoch ms, the call's own bound): one
|
|
339
|
+
* that fails, disagrees with the name, or has no answer by then gives the name's, kept for nothing, so the next read
|
|
340
|
+
* tries again, with `unread`, why: get answers the name's (release needs no more), and a listing fails closed on it.
|
|
341
|
+
* Only the caller's own abort throws.
|
|
304
342
|
*/
|
|
305
|
-
async #labelsOf(found, ctx, end, need) {
|
|
343
|
+
async #labelsOf(found, ctx, end, need, whole = false) {
|
|
306
344
|
const parts = nameParts(found.name);
|
|
307
345
|
if (parts === undefined)
|
|
308
346
|
return { labels: {} };
|
|
309
347
|
const known = this.#labels.get(found.id);
|
|
310
348
|
if (known !== undefined)
|
|
311
349
|
return { labels: known };
|
|
312
|
-
const own = { [COMPUTER_LABEL]: "1", [NAME_LABEL]: parts.computer };
|
|
350
|
+
const own = { [COMPUTER_LABEL]: "1", [NAME_LABEL]: parts.computer, ...(whole && need ? { [RUN_LABEL]: need[RUN_LABEL], [EPOCH_LABEL]: need[EPOCH_LABEL] } : {}) };
|
|
313
351
|
if ((need !== undefined && Object.keys(need).every((key) => key in own)) || found.status !== "running" || this.#ending.has(found.id))
|
|
314
352
|
return { labels: own };
|
|
315
353
|
const late = { labels: own, unread: `no answer within ${seconds(ctx.timeoutMs)}` };
|
|
@@ -328,6 +366,9 @@ class RenderHost {
|
|
|
328
366
|
}
|
|
329
367
|
if (file === undefined)
|
|
330
368
|
return { labels: own };
|
|
369
|
+
const differs = misnamed(parts, file);
|
|
370
|
+
if (differs !== undefined)
|
|
371
|
+
return { labels: own, unread: `the labels file of ${found.id} does not match its name ${found.name}: ${differs}` };
|
|
331
372
|
const labels = { ...file, ...own };
|
|
332
373
|
this.#labels.set(found.id, labels);
|
|
333
374
|
return { labels };
|
|
@@ -472,8 +513,10 @@ class RenderHost {
|
|
|
472
513
|
}
|
|
473
514
|
/**
|
|
474
515
|
* GET /v1/sandboxes?ownerId (S, list-sandboxes: terminated ones left out unless asked), every page by its last item's
|
|
475
|
-
* cursor while a page is full
|
|
476
|
-
* beside the adapter's
|
|
516
|
+
* cursor while a page is full. Render filters by an exact name alone, and a sandbox's name carries its run and epoch
|
|
517
|
+
* beside the adapter's: a listing that asks all three (wholeName, as a create's sweeps for the sandbox it made do) asks
|
|
518
|
+
* Render for that name, and the name's labels hold all three, so no file is read; any other is matched here, over
|
|
519
|
+
* every page (MAX_PAGES at most, then it fails). Only sandboxes named as computers whose name may carry the
|
|
477
520
|
* labels asked (mayCarry: the adapter's name, the run's hash and the epoch); a label asked that is not the name's is
|
|
478
521
|
* read from each running one's labels file (#labelsOf), LABEL_READS at once, all within this call's own bound, and only
|
|
479
522
|
* those that carry every label asked are answered. A file that can't be read by then fails the listing closed, once
|
|
@@ -481,15 +524,17 @@ class RenderHost {
|
|
|
481
524
|
* and leaving it out would let a run's listing miss a newer epoch's fence or call a partial cleanup whole (janitor.ts
|
|
482
525
|
* releaseOlder). So only a sandbox that may be the run's can fail a listing by run: another run's is never read. A
|
|
483
526
|
* listing by the name alone reads no file, and a sandbox terminated from this process is never read, so neither fails
|
|
484
|
-
* so. What this process kept of a sandbox
|
|
485
|
-
* another's hand.
|
|
527
|
+
* so. What this process kept of a sandbox a listing of the whole workspace no longer shows is let go: Render ended it,
|
|
528
|
+
* at its timeout or by another's hand.
|
|
486
529
|
*/
|
|
487
530
|
async list(labels, ctx) {
|
|
488
531
|
const end = Date.now() + ctx.timeoutMs;
|
|
532
|
+
const whole = wholeName(labels);
|
|
489
533
|
// Only what was kept before the listing began is let go: a sandbox made while it ran may not be on its pages.
|
|
490
534
|
const kept = [...new Set([...this.#labels.keys(), ...this.#ending])];
|
|
491
535
|
const listed = orThrow(await allPages(async (cursor) => {
|
|
492
|
-
const
|
|
536
|
+
const query = { limit: String(PAGE), ...(whole === undefined ? {} : { name: whole }), ...(cursor === undefined ? {} : { cursor }) };
|
|
537
|
+
const page = await this.#api.json("GET", this.#api.url(this.#owned("/v1/sandboxes", query)), { retry: true, timeoutMs: ctx.timeoutMs }, ctx.signal);
|
|
493
538
|
if (!page.ok)
|
|
494
539
|
return page;
|
|
495
540
|
if (!Array.isArray(page.value))
|
|
@@ -500,15 +545,17 @@ class RenderHost {
|
|
|
500
545
|
return ok({ items, ...(rows.length === PAGE && typeof last === "string" ? { next: last } : {}) });
|
|
501
546
|
}, MAX_PAGES, { vendor: RENDER.id, what: "Render's sandbox listing" }));
|
|
502
547
|
const live = listed.filter((found) => found.status !== "terminated");
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
548
|
+
if (whole === undefined) {
|
|
549
|
+
const shown = new Set(live.map(({ id }) => id));
|
|
550
|
+
for (const id of kept)
|
|
551
|
+
if (!shown.has(id))
|
|
552
|
+
this.#forget(id);
|
|
553
|
+
}
|
|
507
554
|
const ours = live.filter((found) => {
|
|
508
555
|
const parts = nameParts(found.name);
|
|
509
556
|
return parts !== undefined && mayCarry(parts, labels);
|
|
510
557
|
});
|
|
511
|
-
const read = await eachAtMost(ours, LABEL_READS, async (found) => ({ found, ...(await this.#labelsOf(found, ctx, end, labels)) }));
|
|
558
|
+
const read = await eachAtMost(ours, LABEL_READS, async (found) => ({ found, ...(await this.#labelsOf(found, ctx, end, labels, whole !== undefined)) }));
|
|
512
559
|
const unread = read.flatMap(({ found, unread: why }) => (why === undefined ? [] : [`${found.id}: ${why}`]));
|
|
513
560
|
if (unread.length > 0) {
|
|
514
561
|
const asked = Object.entries(labels).filter(([key]) => key !== COMPUTER_LABEL && key !== NAME_LABEL);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcha/agentrun-computer",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.65",
|
|
4
4
|
"description": "The open platform's computer protocol: a daemon in every computer, and one provisioner per vendor, starting with Docker.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agentrun",
|