@parcha/agentrun-computer 0.1.0-beta.62 → 0.1.0-beta.63
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 +18 -7
- package/dist/browser.d.ts +12 -8
- package/dist/browser.js +26 -18
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/computerd.json +2 -2
- package/dist/protocol.d.ts +5 -1
- package/dist/render.js +289 -79
- package/dist/surface/core.js +4 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -615,10 +615,18 @@ by its rectangle), so a longer one ends the connection before anything is alloca
|
|
|
615
615
|
desktop files of `$XDG_DATA_HOME/applications` and each of `$XDG_DATA_DIRS` (default `~/.local/share`, then
|
|
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
|
-
out.
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
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 let it write the file or a folder holding
|
|
620
|
+
one of them (a sticky folder only where it owns what is in it), or computerd runs as root. The entry is read from the
|
|
621
|
+
file that walk checked, opened without following a link and refused if another took its place, and read again for
|
|
622
|
+
each launch. An app runs what its desktop file says, so such a file, written through `files`, would run any command
|
|
623
|
+
with exec off: `apps.launch` and `apps.terminate` refuse such an app as `denied`, naming the file and why. Its `foreground` is the
|
|
624
|
+
app of the window manager's active window, else of the top window. `apps.launch({ id, url? })` takes an id, a name
|
|
625
|
+
(`Mousepad`) or a program (`mousepad`), starts the entry's `Exec` (`url` in its `%u`/`%f` field; an entry with none
|
|
626
|
+
refuses a `url` as `invalid`; a `url` is an absolute URL with a scheme, `https://…` or `file:///…`, or an absolute
|
|
627
|
+
path, `/…`, and anything else, such as a leading `-` or a relative path, is refused `invalid` and nothing starts; the
|
|
628
|
+
surface refuses a leading `-` itself, before the call leaves it) as `process.start` does with orphans `keep`, with
|
|
629
|
+
`DISPLAY`, and answers `{}` once a new window of the app shows, raised and focused (through the window manager's
|
|
622
630
|
`_NET_ACTIVE_WINDOW` when one runs, waiting up to 2 s for it to be the active window), so keys typed right after
|
|
623
631
|
reach it; none within 10 s is `timeout`, "no window within 10 s", and the app runs on. An
|
|
624
632
|
app already running with a window is brought to the front and answers at once, unless a `url` is given. A
|
|
@@ -2667,9 +2675,12 @@ newer browser has ended, the lost create's is left running for that create, at o
|
|
|
2667
2675
|
Otherwise the request's browsers are read again, every 2 s for up to half of `cleanupMs` (once at least, and sooner
|
|
2668
2676
|
when that half is under 2 s). While every newer one still runs, no converge has answered it (each such create kept its
|
|
2669
2677
|
own), and at the end it is released. On the first read as on every later one, a newer one that has ended counts before
|
|
2670
|
-
an older one that runs, since a converge whose list missed the older may have answered it.
|
|
2671
|
-
|
|
2672
|
-
|
|
2678
|
+
an older one that runs, since a converge whose list missed the older may have answered it. That keeps one held by none
|
|
2679
|
+
too: where three creates of a request ran, the newest's converge may have answered the oldest and ended its own, and
|
|
2680
|
+
the lost create's, the middle one, then runs to the vendor's lifetime; its failure says that an older one still runs.
|
|
2681
|
+
A read that fails, or that misses the lost create's browser, or a read again that misses every newer one, says nothing
|
|
2682
|
+
of who holds it and leaves it running; a first read that fails or misses it is tried again within the same half before
|
|
2683
|
+
that. The search's own retries, for a vendor that lists a browser late, wait within that
|
|
2673
2684
|
half too, so the release keeps the other half.
|
|
2674
2685
|
Browserbase is asked status by status in a session's own order (pending, then running, then the ended ones), so a
|
|
2675
2686
|
session that moves on while a list runs is found in a later answer, never missed, and one found in two answers reads as
|
package/dist/browser.d.ts
CHANGED
|
@@ -110,11 +110,14 @@ export declare function leftToConverge(mine: string, made: ReadonlyArray<MadeBro
|
|
|
110
110
|
/**
|
|
111
111
|
* Where a lost create's browser left to a converge (leftToConverge) stands when the request's browsers are read again:
|
|
112
112
|
* "unread" when the read lists neither mine nor any newer one (the first read did, so it says nothing); "release" once
|
|
113
|
-
* mine has ended; "answered" when a newer one has ended, since a converge that answers the older ends its own first
|
|
114
|
-
*
|
|
115
|
-
*
|
|
113
|
+
* mine has ended; "answered" when a newer one has ended, since a converge that answers the older ends its own first;
|
|
114
|
+
* "answered-older" when an older one also runs, since that converge may have missed the older and answered mine, or
|
|
115
|
+
* answered the older and left mine held by none; "release" when an older one runs and every newer one runs too;
|
|
116
|
+
* "running" while every newer one still runs, so no converge has answered mine yet.
|
|
116
117
|
*/
|
|
117
|
-
export declare function convergeSettled(mine: string, made: ReadonlyArray<MadeBrowser>):
|
|
118
|
+
export declare function convergeSettled(mine: string, made: ReadonlyArray<MadeBrowser>): Settled;
|
|
119
|
+
/** What a read of a lost create's request says of its browser (convergeSettled). */
|
|
120
|
+
type Settled = "answered" | "answered-older" | "running" | "release" | "unread";
|
|
118
121
|
/**
|
|
119
122
|
* Whether a create that threw may still have made a browser: anything but the vendor's own 4xx answer (a refusal, which
|
|
120
123
|
* makes nothing). A request not answered, aborted, cut short, or answered 5xx or with a body that is no browser may
|
|
@@ -131,10 +134,10 @@ export declare const RECOVER_MS = 30000;
|
|
|
131
134
|
* of the recovery's own, `ms` in all (the caller's may be what ended the create). The search, its retries for a vendor
|
|
132
135
|
* that lists late and the reads below wait at most until half of `ms` has passed, each wait RECOVER_GAP_MS or what is
|
|
133
136
|
* left of that half, so the release keeps the other half. Where `made` reads the request's browsers (ended ones
|
|
134
|
-
* included), each found browser's request is read, again after a failed
|
|
135
|
-
*
|
|
136
|
-
* kept at once, even when an older one runs, as a converge that answers it ends its own first and
|
|
137
|
-
* the older may have answered it. One left to another create's converge (leftToConverge) is otherwise read again,
|
|
137
|
+
* included), each found browser's request is read, again after a read that failed or did not list it, and one no read
|
|
138
|
+
* lists is kept: such a read says nothing of who holds it. One older than a browser of its request that has ended
|
|
139
|
+
* (convergeSettled) is kept at once, even when an older one runs, as a converge that answers it ends its own first and
|
|
140
|
+
* one whose list missed the older may have answered it; where an older one runs, the words say it may be held by none. One left to another create's converge (leftToConverge) is otherwise read again,
|
|
138
141
|
* every RECOVER_GAP_MS within that half (once at least), and kept once a newer one has ended or a read fails or misses
|
|
139
142
|
* a browser the first read listed. One whose newer browsers all still run at the end was answered by no converge (each
|
|
140
143
|
* kept its own) and is released. The words say what became of them, for the create's failure; they never carry a URL.
|
|
@@ -282,3 +285,4 @@ export declare const pointerOk: (pointer: unknown) => pointer is string;
|
|
|
282
285
|
* carry a pointer.
|
|
283
286
|
*/
|
|
284
287
|
export declare function browserSpecRefusals(session: unknown, features: BrowserFeatures, vendor: string): Refusal[];
|
|
288
|
+
export {};
|
package/dist/browser.js
CHANGED
|
@@ -122,9 +122,10 @@ export function leftToConverge(mine, made) {
|
|
|
122
122
|
/**
|
|
123
123
|
* Where a lost create's browser left to a converge (leftToConverge) stands when the request's browsers are read again:
|
|
124
124
|
* "unread" when the read lists neither mine nor any newer one (the first read did, so it says nothing); "release" once
|
|
125
|
-
* mine has ended; "answered" when a newer one has ended, since a converge that answers the older ends its own first
|
|
126
|
-
*
|
|
127
|
-
*
|
|
125
|
+
* mine has ended; "answered" when a newer one has ended, since a converge that answers the older ends its own first;
|
|
126
|
+
* "answered-older" when an older one also runs, since that converge may have missed the older and answered mine, or
|
|
127
|
+
* answered the older and left mine held by none; "release" when an older one runs and every newer one runs too;
|
|
128
|
+
* "running" while every newer one still runs, so no converge has answered mine yet.
|
|
128
129
|
*/
|
|
129
130
|
export function convergeSettled(mine, made) {
|
|
130
131
|
const own = made.find((one) => one.id === mine);
|
|
@@ -135,9 +136,10 @@ export function convergeSettled(mine, made) {
|
|
|
135
136
|
const newer = made.filter((one) => one.id !== mine && oldestBrowserFirst(own, one) < 0);
|
|
136
137
|
if (newer.length === 0)
|
|
137
138
|
return "unread";
|
|
139
|
+
const olderRuns = made.some((one) => one.id !== mine && one.live && oldestBrowserFirst(one, own) < 0);
|
|
138
140
|
if (newer.some((one) => !one.live))
|
|
139
|
-
return "answered";
|
|
140
|
-
return
|
|
141
|
+
return olderRuns ? "answered-older" : "answered";
|
|
142
|
+
return olderRuns ? "release" : "running";
|
|
141
143
|
}
|
|
142
144
|
/**
|
|
143
145
|
* Whether a create that threw may still have made a browser: anything but the vendor's own 4xx answer (a refusal, which
|
|
@@ -160,10 +162,10 @@ const RECOVER_GAP_MS = 2_000;
|
|
|
160
162
|
* of the recovery's own, `ms` in all (the caller's may be what ended the create). The search, its retries for a vendor
|
|
161
163
|
* that lists late and the reads below wait at most until half of `ms` has passed, each wait RECOVER_GAP_MS or what is
|
|
162
164
|
* left of that half, so the release keeps the other half. Where `made` reads the request's browsers (ended ones
|
|
163
|
-
* included), each found browser's request is read, again after a failed
|
|
164
|
-
*
|
|
165
|
-
* kept at once, even when an older one runs, as a converge that answers it ends its own first and
|
|
166
|
-
* the older may have answered it. One left to another create's converge (leftToConverge) is otherwise read again,
|
|
165
|
+
* included), each found browser's request is read, again after a read that failed or did not list it, and one no read
|
|
166
|
+
* lists is kept: such a read says nothing of who holds it. One older than a browser of its request that has ended
|
|
167
|
+
* (convergeSettled) is kept at once, even when an older one runs, as a converge that answers it ends its own first and
|
|
168
|
+
* one whose list missed the older may have answered it; where an older one runs, the words say it may be held by none. One left to another create's converge (leftToConverge) is otherwise read again,
|
|
167
169
|
* every RECOVER_GAP_MS within that half (once at least), and kept once a newer one has ended or a read fails or misses
|
|
168
170
|
* a browser the first read listed. One whose newer browsers all still run at the end was answered by no converge (each
|
|
169
171
|
* kept its own) and is released. The words say what became of them, for the create's failure; they never carry a URL.
|
|
@@ -188,18 +190,23 @@ export async function recoverCreate(what, find, release, sleep, ms = RECOVER_MS,
|
|
|
188
190
|
for (const id of found.value) {
|
|
189
191
|
let unanswered = false;
|
|
190
192
|
if (made !== undefined) {
|
|
191
|
-
const read = await firstRead(made, signal, sleep, settleBy, Math.max(1, Math.ceil(ms / 2 / RECOVER_GAP_MS)));
|
|
193
|
+
const read = await firstRead(id, made, signal, sleep, settleBy, Math.max(1, Math.ceil(ms / 2 / RECOVER_GAP_MS)));
|
|
192
194
|
if (read === undefined) {
|
|
193
|
-
words.push(`the ${what} it made (${id}) may be one another create of its request answered, and the request's browsers could not be read, so nothing says who holds it: it is left running, at most to its lifetime, and its labels let a sweep find it`);
|
|
195
|
+
words.push(`the ${what} it made (${id}) may be one another create of its request answered, and the request's browsers could not be read, or did not list it, so nothing says who holds it: it is left running, at most to its lifetime, and its labels let a sweep find it`);
|
|
194
196
|
continue;
|
|
195
197
|
}
|
|
196
|
-
const
|
|
198
|
+
const first = convergeSettled(id, read);
|
|
199
|
+
const answered = first === "answered" || first === "answered-older";
|
|
197
200
|
if (answered || leftToConverge(id, read)) {
|
|
198
|
-
const settled = answered ?
|
|
201
|
+
const settled = answered ? first : await settle(id, made, signal, sleep, settleBy, reads);
|
|
199
202
|
if (settled === "answered") {
|
|
200
203
|
words.push(`the ${what} it made (${id}) is older than one of its request that has ended, as a create's converge ends its own when it answers an older one: it is left running for that create, and if that create did not answer it (aborted, or failed once its own had ended, or its reply lost too) it runs to its lifetime, and its labels let a sweep find it`);
|
|
201
204
|
continue;
|
|
202
205
|
}
|
|
206
|
+
if (settled === "answered-older") {
|
|
207
|
+
words.push(`the ${what} it made (${id}) is older than one of its request that has ended, and an older one of its request still runs: the create whose own ended may have missed that one and answered this, so it is left running, and if that create answered the older instead it is held by none and runs to its lifetime; its labels let a sweep find it`);
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
203
210
|
if (settled === "unread") {
|
|
204
211
|
words.push(`the ${what} it made (${id}) is its request's oldest, which another create of the request may answer, and the request's browsers could not be read again: it is left running, at most to its lifetime, and its labels let a sweep find it`);
|
|
205
212
|
continue;
|
|
@@ -230,16 +237,17 @@ async function waitWithin(sleep, by, signal) {
|
|
|
230
237
|
return true;
|
|
231
238
|
}
|
|
232
239
|
/**
|
|
233
|
-
* The request's browsers, read at once and then again after each failed
|
|
234
|
-
*
|
|
235
|
-
* Undefined when no read
|
|
240
|
+
* The request's browsers, read at once and then again after each read that failed or did not list `mine` (which the
|
|
241
|
+
* search found, so such a read says nothing of it), `retries` times at most, each after a wait that ends no later than
|
|
242
|
+
* `by` (waitWithin): so a window shorter than two gaps still tries again while time is left. Undefined when no read
|
|
243
|
+
* listed it.
|
|
236
244
|
*/
|
|
237
|
-
async function firstRead(made, signal, sleep, by, retries) {
|
|
245
|
+
async function firstRead(mine, made, signal, sleep, by, retries) {
|
|
238
246
|
for (let read = 0; read <= retries; read++) {
|
|
239
247
|
if (read > 0 && !(await waitWithin(sleep, by, signal)))
|
|
240
248
|
break;
|
|
241
249
|
const got = await made(signal);
|
|
242
|
-
if (got.ok)
|
|
250
|
+
if (got.ok && got.value.some((one) => one.id === mine))
|
|
243
251
|
return got.value;
|
|
244
252
|
}
|
|
245
253
|
return undefined;
|
|
Binary file
|
|
Binary file
|
package/dist/computerd.json
CHANGED
package/dist/protocol.d.ts
CHANGED
|
@@ -618,7 +618,11 @@ export interface AppsInstallParams {
|
|
|
618
618
|
export interface AppsLaunchParams {
|
|
619
619
|
/** A bundle id or a package name: "com.apple.Notes", "com.android.settings". */
|
|
620
620
|
id: string;
|
|
621
|
-
/**
|
|
621
|
+
/**
|
|
622
|
+
* Open this in the app: a deep link, or a page in a browser. On Linux, an absolute URL with a scheme (https://…,
|
|
623
|
+
* file:///…) or an absolute path (/…); anything else, such as a leading "-" or a relative path, is refused `invalid`
|
|
624
|
+
* and nothing starts.
|
|
625
|
+
*/
|
|
622
626
|
url?: string;
|
|
623
627
|
/** Open it without bringing it to the front (feature apps.background). Without the feature, refused as `unsupported`. */
|
|
624
628
|
background?: boolean;
|
package/dist/render.js
CHANGED
|
@@ -9,9 +9,14 @@
|
|
|
9
9
|
// the machine's own token, which stays in the sandbox's env. The rest of the computer is the shared machine-host
|
|
10
10
|
// adapter's (./hosted/provider.ts).
|
|
11
11
|
//
|
|
12
|
-
// Render's sandboxes carry no labels: a sandbox's name (computer-<12 hex>) gives the adapter's own
|
|
13
|
-
// every label to a file in the sandbox, root's alone, which
|
|
14
|
-
//
|
|
12
|
+
// Render's sandboxes carry no labels: a sandbox's name (computer-<12 hex>-r<run hash>-e<epoch>) gives the adapter's own
|
|
13
|
+
// two and says whose run and epoch it may be, and create writes every label to a file in the sandbox, root's alone, which
|
|
14
|
+
// get and list read back through runs/sync where a label asked is not the name's, and keep for the whole process: a
|
|
15
|
+
// computer's labels never change (Render moves no timeout, so there is no extend). That read needs a healthy guest, so
|
|
16
|
+
// one that fails, or has no answer within the call's own bound, gives get the name's two: enough for release to find and
|
|
17
|
+
// terminate a wedged sandbox, never a reason it is left to bill to its timeout. A listing by a label beyond the name's
|
|
18
|
+
// fails closed on it instead, naming the sandbox: it can't tell whether that one is a match. A listing by run reads only
|
|
19
|
+
// the sandboxes whose name has the run's hash (and the epoch asked), so only the run's own can fail it.
|
|
15
20
|
//
|
|
16
21
|
// Every vendor fact below is from @renderinc/sdk 1.11.0's OpenAPI schema (dist/generated/schema.d.ts, S) or its client
|
|
17
22
|
// (dist/sandboxes/index.js, C), a live probe (11-25: root, HOME=/root, a Render kernel, egress https, and a command
|
|
@@ -19,30 +24,76 @@
|
|
|
19
24
|
// dial-conformance.mjs: the sandbox's env reaches a runs/sync command, root makes network namespaces in the microVM, the
|
|
20
25
|
// builds API imports the public ghcr.io box image with no credential, and a process escaped with setsid ends with the
|
|
21
26
|
// sandbox's release). One none of these settles says unverified.
|
|
27
|
+
import { createHash } from "node:crypto";
|
|
22
28
|
import { boxImageRef, boxRecipeHash } from "./box-image.js";
|
|
23
29
|
import { RENDER } from "./descriptors/render.js";
|
|
24
30
|
import { COMPUTER_LABEL, DEFAULT_CPUS, DEFAULT_MEMORY_MB, show } from "./drift.js";
|
|
25
31
|
import { HostError } from "./host.js";
|
|
26
|
-
import { codeOfError, orThrow, pause, seconds, until } from "./hosted/process.js";
|
|
32
|
+
import { codeOfError, orThrow, pause, seconds, until, untilAborted } from "./hosted/process.js";
|
|
27
33
|
import { HostedProvider, mayHaveMade, NAME_LABEL, timeoutsOf } from "./hosted/provider.js";
|
|
28
34
|
import { allPages, jsonOf, rest } from "./hosted/rest.js";
|
|
29
35
|
import { dialLaunch } from "./hosted/scripts.js";
|
|
30
36
|
import { dateOf, isWhole } from "./hosted/values.js";
|
|
37
|
+
import { EPOCH_LABEL, RUN_LABEL, runHash } from "./labels.js";
|
|
31
38
|
import { failFor, ok, vendorFail } from "./result.js";
|
|
39
|
+
/**
|
|
40
|
+
* The box build's find-or-import under way in this process, by workspace, key and box image (owner id, the key's
|
|
41
|
+
* keyTag, build name, image tag): every provider of the workspace and key joins it, one of another key (a wrong one's
|
|
42
|
+
* 401) never, and it is dropped once it settles. Render's own POST answers a build of the same name and digest already
|
|
43
|
+
* pending, running or succeeded (S, create-sandbox-build), so this saves the calls, not a second build.
|
|
44
|
+
*/
|
|
45
|
+
const FLIGHTS = new Map();
|
|
46
|
+
/** A key as a map key: the first 16 hex digits of its sha256, never the key. */
|
|
47
|
+
const keyTag = (apiKey) => createHash("sha256").update(apiKey, "utf8").digest("hex").slice(0, 16);
|
|
48
|
+
/** Kept, by workspace (owner id). */
|
|
49
|
+
const KEPT = new Map();
|
|
50
|
+
function keptOf(owner) {
|
|
51
|
+
let kept = KEPT.get(owner);
|
|
52
|
+
if (kept === undefined)
|
|
53
|
+
KEPT.set(owner, (kept = { labels: new Map(), ending: new Set() }));
|
|
54
|
+
return kept;
|
|
55
|
+
}
|
|
32
56
|
const API_URL = "https://api.render.com";
|
|
33
57
|
/**
|
|
34
58
|
* S: ownerId, a workspace id (tea-… for a team, usr-… for a person). Held to an id's characters alone, since it goes in
|
|
35
59
|
* every query: whether it names a workspace the key reaches is Render's answer to the first call.
|
|
36
60
|
*/
|
|
37
61
|
const OWNER_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
38
|
-
/** S: sandboxId, "sbx-1cd4gcph1rs3idesc73a2b2mg"; held to that form before it reaches a path. */
|
|
39
|
-
const SANDBOX_ID = /^sbx-[a-z0-9]{10,40}$/;
|
|
40
62
|
/** S: sandboxBuildId, sbb-…; a build's name may not start with sbb- (S, sandbox-groups/{id}/builds), so this reads as an id. */
|
|
41
63
|
const BUILD_ID = /^sbb-[a-z0-9]{10,40}$/;
|
|
42
64
|
/** A build's name, 1 to 255 characters (S); held to letters, digits, dots, underscores and hyphens, so an OCI path is not one. */
|
|
43
65
|
const BUILD_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,254}$/;
|
|
44
|
-
/**
|
|
45
|
-
|
|
66
|
+
/**
|
|
67
|
+
* A computer's sandbox name: the adapter's name for it (computer-<12 hex>, computerNameFor), then -r<its run's hash>
|
|
68
|
+
* and -e<its epoch> where its labels carry them, as Orgo's names do (orgo-names.ts): at most 57 characters of Render's 64
|
|
69
|
+
* (S, sandboxPOST name). Only a sandbox named so is one of its computers.
|
|
70
|
+
*/
|
|
71
|
+
const SANDBOX_NAME = /^(computer-[0-9a-f]{12})(?:-r([a-z2-7]{16}))?(?:-e(0|[1-9][0-9]{0,15}))?$/;
|
|
72
|
+
/** 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. */
|
|
73
|
+
function sandboxName({ name, labels }) {
|
|
74
|
+
const run = labels[RUN_LABEL];
|
|
75
|
+
const epoch = labels[EPOCH_LABEL];
|
|
76
|
+
return `${name}${typeof run === "string" && run !== "" ? `-r${runHash(run)}` : ""}${typeof epoch === "string" && /^(?:0|[1-9][0-9]{0,15})$/.test(epoch) ? `-e${epoch}` : ""}`;
|
|
77
|
+
}
|
|
78
|
+
/** 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
|
+
function nameParts(name) {
|
|
80
|
+
const match = typeof name === "string" ? SANDBOX_NAME.exec(name) : null;
|
|
81
|
+
if (match === null)
|
|
82
|
+
return undefined;
|
|
83
|
+
return { computer: match[1], ...(match[2] ? { run: match[2] } : {}), ...(match[3] !== undefined ? { epoch: match[3] } : {}) };
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Whether a sandbox so named can carry these labels: one of a name, a run or an epoch other than those asked can't, so
|
|
87
|
+
* a listing never reads its guest, and a guest that can't answer refuses no listing it could not be in. A run's own
|
|
88
|
+
* sandboxes always have its hash, so a hash another run shares (16 base32 letters, 80 bits) only adds a sandbox to be
|
|
89
|
+
* read, whose file then says its run, or which fails the listing closed: it never leaves one of the run's out.
|
|
90
|
+
*/
|
|
91
|
+
function mayCarry(parts, labels) {
|
|
92
|
+
const name = labels[NAME_LABEL];
|
|
93
|
+
const run = labels[RUN_LABEL];
|
|
94
|
+
const epoch = labels[EPOCH_LABEL];
|
|
95
|
+
return (name === undefined || parts.computer === name) && (run === undefined || parts.run === runHash(run)) && (epoch === undefined || parts.epoch === epoch);
|
|
96
|
+
}
|
|
46
97
|
/** S, sandboxPlan: the sizes a sandbox takes, smallest first. */
|
|
47
98
|
const PLANS = [
|
|
48
99
|
{ plan: "1c-2g", cpus: 1, memoryMb: 2048 },
|
|
@@ -57,6 +108,10 @@ const MAX_LIFETIME_MS = 24 * 60 * 60_000;
|
|
|
57
108
|
/** One page of a listing (S, limitParam: 1 to 100). */
|
|
58
109
|
const PAGE = 100;
|
|
59
110
|
const MAX_PAGES = 100;
|
|
111
|
+
/** How many labels files a listing reads at once: each is a run token and a runs/sync call, and a workspace's runs are rate limited (S, 429RateLimit). */
|
|
112
|
+
const LABEL_READS = 4;
|
|
113
|
+
/** S, sandboxBuildStatus: a build of these has, or will have, the box image; failed is imported again. */
|
|
114
|
+
const BUILD_LIVE = ["succeeded", "pending", "running"];
|
|
60
115
|
/** How long the box image's import may take (C's own wait for an image is 10 minutes; the box image is about 400 MB). */
|
|
61
116
|
const BUILD_MS = 30 * 60_000;
|
|
62
117
|
/** How long a failed create's terminate of the sandbox it made may take: create's own bound may be spent by then. */
|
|
@@ -139,11 +194,30 @@ function machineOf(found, labels) {
|
|
|
139
194
|
...resourcesOf(found.plan),
|
|
140
195
|
};
|
|
141
196
|
}
|
|
142
|
-
/**
|
|
143
|
-
|
|
144
|
-
|
|
197
|
+
/** Whether an id Render answered can be one path segment: any string but none, `.` and `..` (which a URL resolves away). */
|
|
198
|
+
const segmentable = (id) => typeof id === "string" && id !== "" && id !== "." && id !== "..";
|
|
199
|
+
/**
|
|
200
|
+
* A sandbox id as one path segment, encoded: Render's ids read sbx-… (S, sandboxId), but one of another form is still
|
|
201
|
+
* read and terminated by it, never left running because this host did not know the form. One that can't be a segment
|
|
202
|
+
* is no sandbox: not_found.
|
|
203
|
+
*/
|
|
204
|
+
function sandboxPath(id) {
|
|
205
|
+
if (!segmentable(id))
|
|
145
206
|
throw new HostError("not_found", `${JSON.stringify(id)} is not a Render sandbox id`);
|
|
146
|
-
return id;
|
|
207
|
+
return encodeURIComponent(id);
|
|
208
|
+
}
|
|
209
|
+
/** Each of items through `each`, at most `most` at once, the answers in the items' order. */
|
|
210
|
+
async function eachAtMost(items, most, each) {
|
|
211
|
+
const answers = new Array(items.length);
|
|
212
|
+
let next = 0;
|
|
213
|
+
const worker = async () => {
|
|
214
|
+
while (next < items.length) {
|
|
215
|
+
const at = next++;
|
|
216
|
+
answers[at] = await each(items[at]);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
await Promise.all(Array.from({ length: Math.min(most, items.length) }, worker));
|
|
220
|
+
return answers;
|
|
147
221
|
}
|
|
148
222
|
/** Render's error body (S, error): { id?, message?, code? }. */
|
|
149
223
|
function explain({ status, bytes, what }) {
|
|
@@ -177,8 +251,9 @@ class RenderHost {
|
|
|
177
251
|
#owner;
|
|
178
252
|
#fetch;
|
|
179
253
|
#pollMs;
|
|
180
|
-
/**
|
|
181
|
-
#labels
|
|
254
|
+
/** What this process keeps of the workspace's sandboxes (KEPT): every provider of the workspace shares it. */
|
|
255
|
+
#labels;
|
|
256
|
+
#ending;
|
|
182
257
|
/** The box build's name once Render read it succeeded: prepareImage asks no more. */
|
|
183
258
|
#built;
|
|
184
259
|
constructor(options) {
|
|
@@ -187,6 +262,7 @@ class RenderHost {
|
|
|
187
262
|
this.#fetch = options.fetch;
|
|
188
263
|
this.#pollMs = options.pollMs;
|
|
189
264
|
this.#api = this.#client(API_URL, () => this.#apiKey);
|
|
265
|
+
({ labels: this.#labels, ending: this.#ending } = keptOf(this.#owner));
|
|
190
266
|
}
|
|
191
267
|
/** A REST client whose every call carries `bearer()` as its Bearer token: the API key, or one run's token. */
|
|
192
268
|
#client(base, bearer) {
|
|
@@ -207,7 +283,7 @@ class RenderHost {
|
|
|
207
283
|
*/
|
|
208
284
|
async #run(id, command, ctx, once = false) {
|
|
209
285
|
const end = Date.now() + ctx.timeoutMs;
|
|
210
|
-
const minted = (await this.#call("POST", this.#owned(`/v1/sandboxes/${
|
|
286
|
+
const minted = (await this.#call("POST", this.#owned(`/v1/sandboxes/${sandboxPath(id)}/runs/sync/token`), { retry: true }, ctx));
|
|
211
287
|
const { uri, token } = minted ?? {};
|
|
212
288
|
if (typeof uri !== "string" || !uri.startsWith("https://") || typeof token !== "string" || token === "")
|
|
213
289
|
throw new HostError("vendor", `Render answered no https run URL and token for the sandbox ${id}`);
|
|
@@ -219,33 +295,60 @@ class RenderHost {
|
|
|
219
295
|
return { stdout: typeof answer.stdout === "string" ? answer.stdout : "", stderr: typeof answer.stderr === "string" ? answer.stderr : "", exit: answer.exit_code };
|
|
220
296
|
}
|
|
221
297
|
/**
|
|
222
|
-
* A sandbox's labels: the ones this
|
|
298
|
+
* A sandbox's labels: the ones this process knows, else its name's, else its labels file's, read once through runs/sync
|
|
223
299
|
* and kept. `need`: a listing's labels; when the name's hold every one asked, no file is read. A sandbox not named as a
|
|
224
|
-
* computer carries none; one not running, or with no file yet, its name's.
|
|
225
|
-
*
|
|
300
|
+
* computer carries none; one not running, one terminated from this process, or one with no file yet, its name's. The
|
|
301
|
+
* read is held to `end` (epoch ms, the call's own bound): one that fails or has no answer by then gives the name's,
|
|
302
|
+
* kept for nothing, so the next read tries again, with `unread`, why: get answers the name's (release needs no more),
|
|
303
|
+
* and a listing fails closed on it. Only the caller's own abort throws.
|
|
226
304
|
*/
|
|
227
|
-
async #labelsOf(found, ctx, need) {
|
|
228
|
-
const
|
|
229
|
-
if (
|
|
230
|
-
return {};
|
|
305
|
+
async #labelsOf(found, ctx, end, need) {
|
|
306
|
+
const parts = nameParts(found.name);
|
|
307
|
+
if (parts === undefined)
|
|
308
|
+
return { labels: {} };
|
|
231
309
|
const known = this.#labels.get(found.id);
|
|
232
310
|
if (known !== undefined)
|
|
233
|
-
return known;
|
|
234
|
-
const own = { [COMPUTER_LABEL]: "1", [NAME_LABEL]:
|
|
235
|
-
if ((need !== undefined && Object.keys(need).every((key) => key in own)) || found.status !== "running")
|
|
236
|
-
return own;
|
|
237
|
-
const
|
|
311
|
+
return { labels: known };
|
|
312
|
+
const own = { [COMPUTER_LABEL]: "1", [NAME_LABEL]: parts.computer };
|
|
313
|
+
if ((need !== undefined && Object.keys(need).every((key) => key in own)) || found.status !== "running" || this.#ending.has(found.id))
|
|
314
|
+
return { labels: own };
|
|
315
|
+
const late = { labels: own, unread: `no answer within ${seconds(ctx.timeoutMs)}` };
|
|
316
|
+
const left = end - Date.now();
|
|
317
|
+
if (left <= 0)
|
|
318
|
+
return late;
|
|
319
|
+
const bound = AbortSignal.timeout(left);
|
|
320
|
+
let file;
|
|
321
|
+
try {
|
|
322
|
+
file = await this.#labelsFile(found.id, { timeoutMs: left, signal: ctx.signal ? AbortSignal.any([ctx.signal, bound]) : bound });
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
if (ctx.signal?.aborted)
|
|
326
|
+
throw error;
|
|
327
|
+
return bound.aborted ? late : { labels: own, unread: error instanceof Error ? error.message : String(error) };
|
|
328
|
+
}
|
|
329
|
+
if (file === undefined)
|
|
330
|
+
return { labels: own };
|
|
331
|
+
const labels = { ...file, ...own };
|
|
332
|
+
this.#labels.set(found.id, labels);
|
|
333
|
+
return { labels };
|
|
334
|
+
}
|
|
335
|
+
/** The labels file read through runs/sync: undefined when it is not there yet; a read that fails throws in words. */
|
|
336
|
+
async #labelsFile(id, ctx) {
|
|
337
|
+
const read = await this.#run(id, readFile(LABELS_FILE), ctx);
|
|
238
338
|
if (read.exit === MISSING)
|
|
239
|
-
return
|
|
339
|
+
return undefined;
|
|
240
340
|
if (read.exit !== 0)
|
|
241
|
-
throw new HostError("vendor", `reading the labels file of ${
|
|
341
|
+
throw new HostError("vendor", `reading the labels file of ${id} exited ${read.exit}: ${lastWords(read)}`);
|
|
242
342
|
const parsed = jsonOf(new TextEncoder().encode(read.stdout));
|
|
243
343
|
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed) || !Object.values(parsed).every((value) => typeof value === "string")) {
|
|
244
|
-
throw new HostError("vendor", `the labels file of ${
|
|
344
|
+
throw new HostError("vendor", `the labels file of ${id} is not a JSON map of strings`);
|
|
245
345
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
346
|
+
return parsed;
|
|
347
|
+
}
|
|
348
|
+
/** A sandbox Render reads gone: its labels and its terminate are let go. */
|
|
349
|
+
#forget(id) {
|
|
350
|
+
this.#labels.delete(id);
|
|
351
|
+
this.#ending.delete(id);
|
|
249
352
|
}
|
|
250
353
|
/**
|
|
251
354
|
* Any Render build by its name or id (S: buildName, "the newest successful build with this name in the sandbox
|
|
@@ -266,16 +369,17 @@ class RenderHost {
|
|
|
266
369
|
return refusals;
|
|
267
370
|
}
|
|
268
371
|
/**
|
|
269
|
-
* POST /v1/sandboxes (S, sandboxPOST): the workspace, the request's name
|
|
270
|
-
* lifetime as timeoutSeconds (at least 60), no idle suspension (0), egress
|
|
271
|
-
* server; the computer's own programs keep to init's network), the boot env but
|
|
272
|
-
* the build. Only a 429, a refusal that made nothing, is sent again: a create that
|
|
273
|
-
* sandbox, which the adapter finds by its name. Then, once the sandbox reads running
|
|
274
|
-
* runs/sync call writes the labels file and starts agentrun-dial, the URL in its launch
|
|
275
|
-
* or ends first, a launch that fails, or a bound passed, throws: a create that made a
|
|
276
|
-
* deletes it by its name. A refusal after the sandbox exists (a run token's 429s past
|
|
277
|
-
* denied) the adapter reads as a create that made nothing (mayHaveMade), so this terminates
|
|
278
|
-
* first and throws the refusal in its own code. req.backstopAt is left: Render's timeout ends the
|
|
372
|
+
* POST /v1/sandboxes (S, sandboxPOST): the workspace, the request's name with its run's hash and epoch (sandboxName),
|
|
373
|
+
* the smallest plan holding its size, the lifetime as timeoutSeconds (at least 60), no idle suspension (0), egress
|
|
374
|
+
* allow-all (the sandbox must reach the server; the computer's own programs keep to init's network), the boot env but
|
|
375
|
+
* AGENTRUN_ATTACH_URL as its env, and the build. Only a 429, a refusal that made nothing, is sent again: a create that
|
|
376
|
+
* got no answer may have made a sandbox, which the adapter finds by its name. Then, once the sandbox reads running
|
|
377
|
+
* within ctx.timeoutMs, one runs/sync call writes the labels file and starts agentrun-dial, the URL in its launch
|
|
378
|
+
* script. A sandbox that errors or ends first, a launch that fails, or a bound passed, throws: a create that made a
|
|
379
|
+
* machine to the adapter, which deletes it by its name. A refusal after the sandbox exists (a run token's 429s past
|
|
380
|
+
* every retry, limit; a 401, denied) the adapter reads as a create that made nothing (mayHaveMade), so this terminates
|
|
381
|
+
* the sandbox by its id first and throws the refusal in its own code. req.backstopAt is left: Render's timeout ends the
|
|
382
|
+
* sandbox.
|
|
279
383
|
*/
|
|
280
384
|
async create(req, ctx) {
|
|
281
385
|
const end = Date.now() + ctx.timeoutMs;
|
|
@@ -287,7 +391,7 @@ class RenderHost {
|
|
|
287
391
|
const made = (await this.#call("POST", "/v1/sandboxes", {
|
|
288
392
|
json: {
|
|
289
393
|
ownerId: this.#owner,
|
|
290
|
-
name: req
|
|
394
|
+
name: sandboxName(req),
|
|
291
395
|
plan: planOf(req.cpus, req.memoryMb),
|
|
292
396
|
timeoutSeconds: Math.max(LEAST_TIMEOUT_S, Math.ceil(req.lifetimeMs / 1000)),
|
|
293
397
|
idleTimeoutSeconds: 0,
|
|
@@ -298,7 +402,7 @@ class RenderHost {
|
|
|
298
402
|
retry: (status) => status === 429,
|
|
299
403
|
}, ctx));
|
|
300
404
|
const id = made?.id;
|
|
301
|
-
if (
|
|
405
|
+
if (!segmentable(id))
|
|
302
406
|
throw new HostError("vendor", `POST /v1/sandboxes answered no sandbox id: ${show(id)}`);
|
|
303
407
|
this.#labels.set(id, { ...req.labels });
|
|
304
408
|
try {
|
|
@@ -339,29 +443,53 @@ class RenderHost {
|
|
|
339
443
|
await pause(this.#pollMs, ctx.signal);
|
|
340
444
|
if (ctx.signal?.aborted)
|
|
341
445
|
throw new HostError("aborted", `the wait for the sandbox ${found.id} to run was aborted`);
|
|
342
|
-
found = (await this.#call("GET", this.#owned(`/v1/sandboxes/${found.id}`), { retry: true }, { ...ctx, timeoutMs: Math.max(1, end - Date.now()) }));
|
|
446
|
+
found = (await this.#call("GET", this.#owned(`/v1/sandboxes/${sandboxPath(found.id)}`), { retry: true }, { ...ctx, timeoutMs: Math.max(1, end - Date.now()) }));
|
|
343
447
|
}
|
|
344
448
|
}
|
|
345
|
-
/**
|
|
449
|
+
/**
|
|
450
|
+
* GET /v1/sandboxes/{id}; 404 is not_found, which the adapter reads as none, and a terminated sandbox is none: either
|
|
451
|
+
* lets go of what this process kept of it. Its labels are read within this call's own bound (#labelsOf), and a read that
|
|
452
|
+
* fails gives the name's two: enough for the adapter to know its computer and release it.
|
|
453
|
+
*/
|
|
346
454
|
async get(id, ctx) {
|
|
347
|
-
const
|
|
455
|
+
const end = Date.now() + ctx.timeoutMs;
|
|
456
|
+
let found;
|
|
457
|
+
try {
|
|
458
|
+
found = (await this.#call("GET", this.#owned(`/v1/sandboxes/${sandboxPath(id)}`), { retry: true }, ctx));
|
|
459
|
+
}
|
|
460
|
+
catch (error) {
|
|
461
|
+
if (error instanceof HostError && error.code === "not_found")
|
|
462
|
+
this.#forget(id);
|
|
463
|
+
throw error;
|
|
464
|
+
}
|
|
348
465
|
if (typeof found?.status !== "string")
|
|
349
466
|
throw new HostError("vendor", `GET /v1/sandboxes/${id} answered no status`);
|
|
350
|
-
if (found.status === "terminated")
|
|
467
|
+
if (found.status === "terminated") {
|
|
468
|
+
this.#forget(id);
|
|
351
469
|
return undefined;
|
|
352
|
-
|
|
470
|
+
}
|
|
471
|
+
return machineOf(found, (await this.#labelsOf(found, ctx, end)).labels);
|
|
353
472
|
}
|
|
354
473
|
/**
|
|
355
|
-
* GET /v1/sandboxes?ownerId (S, list-sandboxes: terminated ones left out unless asked),
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
474
|
+
* 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: Render filters by an exact name alone, and a sandbox's name carries its run and epoch
|
|
476
|
+
* beside the adapter's, so the name asked is matched here. Only sandboxes named as computers whose name may carry the
|
|
477
|
+
* 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
|
+
* read from each running one's labels file (#labelsOf), LABEL_READS at once, all within this call's own bound, and only
|
|
479
|
+
* those that carry every label asked are answered. A file that can't be read by then fails the listing closed, once
|
|
480
|
+
* every read has ended, naming each such sandbox and why: the listing can't tell whether it carries the labels asked,
|
|
481
|
+
* 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
|
+
* 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
|
+
* 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 the listing no longer shows is let go: Render ended it, at its timeout or by
|
|
485
|
+
* another's hand.
|
|
360
486
|
*/
|
|
361
487
|
async list(labels, ctx) {
|
|
362
|
-
const
|
|
488
|
+
const end = Date.now() + ctx.timeoutMs;
|
|
489
|
+
// Only what was kept before the listing began is let go: a sandbox made while it ran may not be on its pages.
|
|
490
|
+
const kept = [...new Set([...this.#labels.keys(), ...this.#ending])];
|
|
363
491
|
const listed = orThrow(await allPages(async (cursor) => {
|
|
364
|
-
const page = await this.#api.json("GET", this.#api.url(this.#owned("/v1/sandboxes", { limit: String(PAGE), ...(
|
|
492
|
+
const page = await this.#api.json("GET", this.#api.url(this.#owned("/v1/sandboxes", { limit: String(PAGE), ...(cursor === undefined ? {} : { cursor }) })), { retry: true, timeoutMs: ctx.timeoutMs }, ctx.signal);
|
|
365
493
|
if (!page.ok)
|
|
366
494
|
return page;
|
|
367
495
|
if (!Array.isArray(page.value))
|
|
@@ -371,17 +499,39 @@ class RenderHost {
|
|
|
371
499
|
const last = rows.at(-1)?.cursor;
|
|
372
500
|
return ok({ items, ...(rows.length === PAGE && typeof last === "string" ? { next: last } : {}) });
|
|
373
501
|
}, MAX_PAGES, { vendor: RENDER.id, what: "Render's sandbox listing" }));
|
|
374
|
-
const
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
502
|
+
const live = listed.filter((found) => found.status !== "terminated");
|
|
503
|
+
const shown = new Set(live.map(({ id }) => id));
|
|
504
|
+
for (const id of kept)
|
|
505
|
+
if (!shown.has(id))
|
|
506
|
+
this.#forget(id);
|
|
507
|
+
const ours = live.filter((found) => {
|
|
508
|
+
const parts = nameParts(found.name);
|
|
509
|
+
return parts !== undefined && mayCarry(parts, labels);
|
|
510
|
+
});
|
|
511
|
+
const read = await eachAtMost(ours, LABEL_READS, async (found) => ({ found, ...(await this.#labelsOf(found, ctx, end, labels)) }));
|
|
512
|
+
const unread = read.flatMap(({ found, unread: why }) => (why === undefined ? [] : [`${found.id}: ${why}`]));
|
|
513
|
+
if (unread.length > 0) {
|
|
514
|
+
const asked = Object.entries(labels).filter(([key]) => key !== COMPUTER_LABEL && key !== NAME_LABEL);
|
|
515
|
+
const carry = unread.length === 1 ? "it carries" : "they carry";
|
|
516
|
+
throw new HostError("vendor", `could not read the labels of ${unread.join("; ")}; the listing can't tell whether ${carry} ${asked.map(([key, value]) => `${key}=${value}`).join(", ")}`);
|
|
517
|
+
}
|
|
518
|
+
return read.flatMap(({ found, labels: carried }) => (Object.entries(labels).every(([key, value]) => carried[key] === value) ? (machineOf(found, carried) ?? []) : []));
|
|
380
519
|
}
|
|
381
|
-
/**
|
|
520
|
+
/**
|
|
521
|
+
* POST /v1/sandboxes/{id}/terminate (S: 204, also for one already terminated); a 404 is done to the adapter. What this
|
|
522
|
+
* process kept of it stays until get reads it gone: Render may read it running a while yet, and its labels still
|
|
523
|
+
* answer. It is marked ending for every provider of the workspace, so none reads its guest meanwhile.
|
|
524
|
+
*/
|
|
382
525
|
async delete(id, ctx) {
|
|
383
|
-
|
|
384
|
-
|
|
526
|
+
try {
|
|
527
|
+
await this.#call("POST", this.#owned(`/v1/sandboxes/${sandboxPath(id)}/terminate`), { retry: true }, ctx);
|
|
528
|
+
}
|
|
529
|
+
catch (error) {
|
|
530
|
+
if (error instanceof HostError && error.code === "not_found")
|
|
531
|
+
this.#forget(id);
|
|
532
|
+
throw error;
|
|
533
|
+
}
|
|
534
|
+
this.#ending.add(id);
|
|
385
535
|
}
|
|
386
536
|
/** The end of what agentrun-dial wrote, through runs/sync; undefined where it was never started. */
|
|
387
537
|
async dialLog(id, ctx) {
|
|
@@ -435,24 +585,84 @@ class RenderHost {
|
|
|
435
585
|
}
|
|
436
586
|
/**
|
|
437
587
|
* The box image as a build of the workspace's default sandbox group (the group a create that names none uses, S),
|
|
438
|
-
* named agentrun-box-<recipe hash>, so a new recipe is a new build
|
|
439
|
-
*
|
|
440
|
-
*
|
|
441
|
-
*
|
|
442
|
-
*
|
|
588
|
+
* named agentrun-box-<recipe hash>, so a new recipe is a new build. Concurrent callers in this process share one
|
|
589
|
+
* find-or-import per workspace, key and box image (FLIGHTS), whichever provider they call through (serve makes one per
|
|
590
|
+
* claim), forgotten once it settles. `building` is told once, before the wait on an import, its own or one under way
|
|
591
|
+
* when it joined; a caller's signal ends its own wait alone: the import's wait goes on, within BUILD_MS, for the next
|
|
592
|
+
* caller.
|
|
443
593
|
*/
|
|
444
594
|
async boxBuild(building, signal) {
|
|
445
595
|
if (this.#built !== undefined)
|
|
446
596
|
return this.#built;
|
|
597
|
+
if (signal?.aborted)
|
|
598
|
+
throw new HostError("aborted", "the wait for the box build was aborted before it began");
|
|
599
|
+
const key = JSON.stringify([this.#owner, keyTag(this.#apiKey), boxBuildName(), boxImageRef()]);
|
|
600
|
+
let flight = FLIGHTS.get(key);
|
|
601
|
+
if (flight === undefined) {
|
|
602
|
+
const started = this.#fly();
|
|
603
|
+
FLIGHTS.set(key, (flight = started));
|
|
604
|
+
void started.answer.then(() => {
|
|
605
|
+
if (FLIGHTS.get(key) === started)
|
|
606
|
+
FLIGHTS.delete(key);
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
const tell = () => {
|
|
610
|
+
try {
|
|
611
|
+
building();
|
|
612
|
+
}
|
|
613
|
+
catch {
|
|
614
|
+
// The caller's listener is its own: a throw changes nothing here.
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
if (flight.importing)
|
|
618
|
+
tell();
|
|
619
|
+
else
|
|
620
|
+
flight.toTell.add(tell);
|
|
621
|
+
try {
|
|
622
|
+
const settled = await untilAborted(flight.answer, [signal]);
|
|
623
|
+
if (settled === undefined)
|
|
624
|
+
throw new HostError("aborted", "the wait for the box build was aborted; its import goes on for the next caller");
|
|
625
|
+
if ("error" in settled)
|
|
626
|
+
throw settled.error;
|
|
627
|
+
return (this.#built = settled.name);
|
|
628
|
+
}
|
|
629
|
+
finally {
|
|
630
|
+
flight.toTell.delete(tell);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
/** A find-or-import through this host's API key: it never rejects (its error is its answer). */
|
|
634
|
+
#fly() {
|
|
635
|
+
const flight = { answer: Promise.resolve({ error: undefined }), importing: false, toTell: new Set() };
|
|
636
|
+
const importing = () => {
|
|
637
|
+
flight.importing = true;
|
|
638
|
+
for (const tell of flight.toTell)
|
|
639
|
+
tell();
|
|
640
|
+
};
|
|
641
|
+
flight.answer = this.#findOrImport(importing).then((name) => ({ name }), (error) => ({ error }));
|
|
642
|
+
return flight;
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* The box build found or imported, held to no caller: a build of its name that succeeded is the answer; one pending
|
|
646
|
+
* or running (an import another call or process started, or a POST whose answer was lost) is waited for (GET
|
|
647
|
+
* …/builds?name&status=succeeded&status=pending&status=running, newest first; an array is one key per status, as C's
|
|
648
|
+
* openapi-fetch sends it). Else POST …/builds { name, sourceImage } imports the box image's public tag (S: "converts a
|
|
649
|
+
* container image into an image that sandboxes can use"), and Render's answer is read until it succeeded or failed.
|
|
650
|
+
* `importing` is told before either wait. Render pulls the public ghcr.io image with no credential (the live Core
|
|
651
|
+
* run's import took 104 s); a tag the release has not published yet is Render's 400, "source image unavailable".
|
|
652
|
+
*/
|
|
653
|
+
async #findOrImport(importing) {
|
|
447
654
|
const name = boxBuildName();
|
|
448
|
-
const ctx = { timeoutMs: 30_000
|
|
655
|
+
const ctx = { timeoutMs: 30_000 };
|
|
449
656
|
const group = await this.#defaultGroup(ctx);
|
|
450
657
|
const builds = `/v1/sandbox-groups/${group}/builds`;
|
|
451
|
-
const
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
658
|
+
const query = new URLSearchParams([["name", name], ...BUILD_LIVE.map((status) => ["status", status]), ["limit", String(PAGE)]]);
|
|
659
|
+
const listed = await this.#call("GET", `${builds}?${query}`, { retry: true }, ctx);
|
|
660
|
+
const found = (Array.isArray(listed) ? listed : []).flatMap((row) => (row?.build ? [row.build] : []));
|
|
661
|
+
if (found.some(({ status }) => status === "succeeded"))
|
|
662
|
+
return name;
|
|
663
|
+
importing();
|
|
664
|
+
let build = found.find(({ id, status }) => (status === "pending" || status === "running") && typeof id === "string" && BUILD_ID.test(id));
|
|
665
|
+
build ??= (await this.#call("POST", builds, { json: { name, sourceImage: boxImageRef() }, retry: (status) => status === 429 }, ctx));
|
|
456
666
|
const id = build?.id;
|
|
457
667
|
if (typeof id !== "string" || !BUILD_ID.test(id))
|
|
458
668
|
throw new HostError("vendor", `POST ${builds} answered no build id: ${show(id)}`);
|
|
@@ -464,8 +674,8 @@ class RenderHost {
|
|
|
464
674
|
return undefined;
|
|
465
675
|
};
|
|
466
676
|
if (!settled(build))
|
|
467
|
-
await until(`Render's build ${id}`, { end: Date.now() + BUILD_MS, pollMs: this.#pollMs
|
|
468
|
-
return
|
|
677
|
+
await until(`Render's build ${id}`, { end: Date.now() + BUILD_MS, pollMs: this.#pollMs }, async () => settled((build = (await this.#call("GET", `${builds}/${id}`, { retry: true }, ctx)))));
|
|
678
|
+
return name;
|
|
469
679
|
}
|
|
470
680
|
}
|
|
471
681
|
/** The box image's build name: agentrun-box-<recipe hash>, as the box image's own tag is. */
|
package/dist/surface/core.js
CHANGED
|
@@ -431,6 +431,10 @@ export function createSurface(client, hello, { cwd } = {}) {
|
|
|
431
431
|
case "apps.open": {
|
|
432
432
|
if (typeof args.id !== "string" || args.id === "")
|
|
433
433
|
throw new SurfaceError("invalid", "id is the app's bundle id or package name");
|
|
434
|
+
// A Linux computerd older than its own url check would put this into the app's argv as a flag (Chromium's
|
|
435
|
+
// --renderer-cmd-prefix runs a command); no platform takes one as a url.
|
|
436
|
+
if (typeof args.url === "string" && /^[ \t\r\n\v\f]*-/.test(args.url))
|
|
437
|
+
throw new SurfaceError("invalid", `url ${JSON.stringify(args.url)} starts with "-": the app would take it as a flag, not a url`);
|
|
434
438
|
await call("apps.launch", { id: args.id, ...(typeof args.url === "string" ? { url: args.url } : {}), ...(args.background === true ? { background: true } : {}) }, signal);
|
|
435
439
|
return { value: { id: args.id }, text: `opened ${args.id}`, images: [] };
|
|
436
440
|
}
|
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.63",
|
|
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",
|