@parcha/agentrun-computer 0.1.0-beta.37 → 0.1.0-beta.39
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 +341 -260
- package/box/Dockerfile +18 -1
- package/box/README.md +14 -0
- package/box/build-daytona.py +2 -2
- package/box/build-e2b.mjs +41 -18
- package/box/recipe.d.mts +5 -0
- package/box/recipe.mjs +43 -0
- package/dist/agent37.d.ts +18 -66
- package/dist/agent37.js +187 -464
- package/dist/box-image.d.ts +14 -0
- package/dist/box-image.js +24 -0
- package/dist/browserbase.d.ts +0 -6
- package/dist/browserbase.js +1 -5
- package/dist/client.d.ts +12 -3
- package/dist/client.js +39 -4
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/computerd.json +4 -0
- package/dist/conformance/agent37.d.ts +9 -4
- package/dist/conformance/agent37.js +23 -11
- package/dist/conformance/browserbase.js +3 -3
- package/dist/conformance/cases/apps.d.ts +6 -0
- package/dist/conformance/cases/apps.js +18 -5
- package/dist/conformance/cases/isolation.d.ts +22 -5
- package/dist/conformance/cases/isolation.js +97 -16
- package/dist/conformance/cases/mount.js +5 -3
- package/dist/conformance/cases/provider.js +41 -0
- package/dist/conformance/cli.js +72 -11
- package/dist/conformance/daytona.d.ts +9 -20
- package/dist/conformance/daytona.js +12 -21
- package/dist/conformance/e2b.js +9 -4
- package/dist/conformance/exec-stream.d.ts +52 -0
- package/dist/conformance/exec-stream.js +493 -0
- package/dist/conformance/kernel.js +2 -2
- package/dist/conformance/modal.js +1 -1
- package/dist/conformance/orgo.js +2 -2
- package/dist/conformance/sweep.js +8 -8
- package/dist/conformance/types.d.ts +2 -2
- package/dist/create-requests.d.ts +1 -1
- package/dist/daytona.d.ts +17 -133
- package/dist/daytona.js +396 -1452
- package/dist/descriptor.d.ts +11 -4
- package/dist/descriptor.js +7 -0
- package/dist/descriptors/agent37.d.ts +3 -11
- package/dist/descriptors/agent37.js +9 -10
- package/dist/descriptors/daytona.d.ts +7 -6
- package/dist/descriptors/daytona.js +14 -7
- package/dist/descriptors/docker.d.ts +13 -1
- package/dist/descriptors/docker.js +3 -1
- package/dist/descriptors/e2b.d.ts +2 -9
- package/dist/descriptors/e2b.js +8 -8
- package/dist/descriptors/index.d.ts +25 -24
- package/dist/descriptors/index.js +1 -3
- package/dist/descriptors/modal.d.ts +2 -6
- package/dist/descriptors/modal.js +8 -6
- package/dist/descriptors/orgo.js +2 -1
- package/dist/descriptors/shared.d.ts +1 -1
- package/dist/descriptors/shared.js +2 -2
- package/dist/desktop.d.ts +18 -4
- package/dist/desktop.js +89 -9
- package/dist/docker/egress-main.js +23 -15
- package/dist/docker/egress.d.ts +6 -4
- package/dist/docker/egress.js +24 -16
- package/dist/docker/inspect.d.ts +2 -2
- package/dist/docker/inspect.js +5 -5
- package/dist/docker.d.ts +6 -11
- package/dist/docker.js +13 -16
- package/dist/drift.js +4 -4
- package/dist/e2b.d.ts +22 -130
- package/dist/e2b.js +163 -243
- package/dist/host.d.ts +136 -104
- package/dist/host.js +8 -1
- package/dist/hosted/network.d.ts +23 -0
- package/dist/hosted/network.js +77 -0
- package/dist/hosted/process.d.ts +24 -8
- package/dist/hosted/process.js +80 -44
- package/dist/hosted/provider.d.ts +38 -22
- package/dist/hosted/provider.js +500 -341
- package/dist/hosted/rest.d.ts +74 -0
- package/dist/hosted/rest.js +126 -0
- package/dist/hosted/scripts.d.ts +21 -3
- package/dist/hosted/scripts.js +76 -14
- package/dist/hosted/ssh-exec.d.ts +99 -0
- package/dist/hosted/ssh-exec.js +464 -0
- package/dist/hosted/values.d.ts +26 -0
- package/dist/hosted/values.js +63 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/isolation.d.ts +0 -2
- package/dist/isolation.js +3 -4
- package/dist/kernel.d.ts +0 -6
- package/dist/kernel.js +1 -5
- package/dist/kinds.d.ts +3 -1
- package/dist/local-chrome.d.ts +0 -6
- package/dist/local-chrome.js +1 -5
- package/dist/modal.d.ts +15 -86
- package/dist/modal.js +67 -185
- package/dist/mount-archil.d.ts +13 -8
- package/dist/mount-archil.js +27 -17
- package/dist/mount.d.ts +10 -4
- package/dist/mount.js +8 -4
- package/dist/orgo.d.ts +0 -9
- package/dist/orgo.js +8 -13
- package/dist/phone-harness.d.ts +0 -6
- package/dist/phone-harness.js +0 -4
- package/dist/protocol.d.ts +4 -1
- package/dist/protocol.js +1 -0
- package/dist/provider.d.ts +2 -2
- package/dist/providers.d.ts +15 -13
- package/dist/screen-watch.js +34 -6
- package/dist/surface/api.d.ts +14 -11
- package/dist/surface/core.d.ts +23 -5
- package/dist/surface/core.js +105 -15
- package/dist/surface/index.d.ts +1 -1
- package/dist/surface/index.js +1 -1
- package/dist/surface/outline.d.ts +2 -0
- package/dist/surface/outline.js +2 -1
- package/dist/surface/script.d.ts +4 -0
- package/dist/surface/script.js +11 -3
- package/dist/ui-engine.js +19 -3
- package/package.json +2 -3
- package/box/build-agent37.mjs +0 -177
- package/dist/conformance/daytona-sweep.d.ts +0 -18
- package/dist/conformance/daytona-sweep.js +0 -126
package/dist/e2b.js
CHANGED
|
@@ -1,21 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { words } from "./hosted/process.js";
|
|
10
|
-
import { shellLine } from "./hosted/scripts.js";
|
|
11
|
-
import { failFor, ok } from "./result.js";
|
|
12
|
-
import { countTested } from "./test-connection.js";
|
|
13
|
-
/** This module's failures: one with code vendor names E2B. */
|
|
14
|
-
const fail = failFor("e2b");
|
|
1
|
+
import { boxE2bTemplate } from "./box-image.js";
|
|
2
|
+
import { E2B } from "./descriptors/e2b.js";
|
|
3
|
+
import { DEADLINE_LABEL, show } from "./drift.js";
|
|
4
|
+
import { HostError } from "./host.js";
|
|
5
|
+
import { deadlineOf, HostedProvider, isoOf, MAX_LABEL, timeoutsOf } from "./hosted/provider.js";
|
|
6
|
+
import { dateOf, DOMAIN, isWhole, sizeRefusals } from "./hosted/values.js";
|
|
7
|
+
import { PassThrough } from "node:stream";
|
|
8
|
+
import { ok } from "./result.js";
|
|
15
9
|
/** Not a literal, so no type checker or bundler resolves the SDK from this package. */
|
|
16
10
|
const E2B_PACKAGE = "e2b";
|
|
17
11
|
const DEFAULT_DOMAIN = "e2b.app";
|
|
18
|
-
const DOMAIN = /^(?=.{1,253}$)([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/;
|
|
19
12
|
/** The connection options every SDK call is given. Exported for the test that hands them to the real SDK. */
|
|
20
13
|
export function connectionOptions({ apiKey, domain }) {
|
|
21
14
|
return { apiKey, domain, apiUrl: `https://api.${domain}`, debug: false };
|
|
@@ -39,14 +32,9 @@ function ambientProblem() {
|
|
|
39
32
|
* never echoing the key.
|
|
40
33
|
*/
|
|
41
34
|
export async function create(options) {
|
|
42
|
-
const {
|
|
43
|
-
if (typeof apiKey !== "string" || apiKey === "")
|
|
44
|
-
throw new Error("options.apiKey, the E2B API key, is required: the provider reads no environment, so the caller passes it");
|
|
45
|
-
const ambient = ambientProblem();
|
|
46
|
-
if (ambient)
|
|
47
|
-
throw new Error(ambient);
|
|
35
|
+
const { Sandbox: given, ...rest } = options ?? {};
|
|
48
36
|
const Sandbox = given ?? (await import(E2B_PACKAGE)).Sandbox;
|
|
49
|
-
return new E2BProvider({ ...rest,
|
|
37
|
+
return new E2BProvider({ ...rest, Sandbox });
|
|
50
38
|
}
|
|
51
39
|
/** The conformance CLI's module for E2B: its live switch, the run's 30-minute cap, its template and the drifted twin. */
|
|
52
40
|
export const conformance = () => import("./conformance/e2b.js");
|
|
@@ -61,21 +49,26 @@ const MAX_PAGES = 100;
|
|
|
61
49
|
const PAGE = 100;
|
|
62
50
|
/** E2B's end, read back, is its start plus the timeout create gave it, to the second it rounds to (index.mjs:502-504). */
|
|
63
51
|
const END_SLACK_MS = 2_000;
|
|
64
|
-
const DEFAULT_REQUEST_MS = 30_000;
|
|
65
52
|
const PUBLIC = "publicStream is no longer an option: an E2B computer never takes public traffic, which would open every port in it to anyone with its address, and its screen is streamed through agentrun, never a vendor URL";
|
|
66
53
|
/** envd's port in the sandbox (e2b@2.63.0 dist/index.mjs:1213, ConnectionConfig.envdPort). */
|
|
67
54
|
const ENVD_PORT = 49983;
|
|
68
|
-
/**
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
55
|
+
/** envd's user for a process: root, as the SDK's authenticationHeader writes it (index.mjs:1896-1900). */
|
|
56
|
+
const ROOT = `Basic ${Buffer.from("root:").toString("base64")}`;
|
|
57
|
+
/** envd sends a keepalive event this often on a process stream, as the SDK asks (index.mjs:923, 5141). */
|
|
58
|
+
const KEEPALIVE_HEADER = "Keepalive-Ping-Interval";
|
|
59
|
+
const KEEPALIVE_S = "50";
|
|
60
|
+
/** The Connect codes envd's process calls answer (connectrpc Code), as this provider's codes. */
|
|
61
|
+
const CONNECT_CODES = { 1: "aborted", 3: "invalid", 4: "timeout", 5: "not_found", 7: "denied", 8: "limit", 16: "denied" };
|
|
62
|
+
/** What create makes root's alone in every E2B computer, before anything else (HostFacts.rootOnly). */
|
|
63
|
+
const ROOT_ONLY = ["/usr/local", "/usr/bin/envd", "/etc/inittab"];
|
|
64
|
+
/** Root's command, as envd starts it: /bin/sh by its path, no login shell, the argv as its own words. */
|
|
65
|
+
const ROOT_SHELL = "/bin/sh";
|
|
66
|
+
const ROOT_SHELL_ARGS = ["-c", 'exec "$@"', "sh"];
|
|
67
|
+
/** The PATH root's command runs with: the system's directories alone, none of /usr/local. */
|
|
68
|
+
const ROOT_PATH = "/usr/sbin:/usr/bin:/sbin:/bin";
|
|
69
|
+
/** The SDK names its errors (index.mjs:86-360); Connect's carry a code. */
|
|
75
70
|
function codeOf(error) {
|
|
76
71
|
const name = error?.name;
|
|
77
|
-
if (name === "AbortError")
|
|
78
|
-
return "aborted";
|
|
79
72
|
if (name === "SandboxNotFoundError" || name === "NotFoundError" || name === "FileNotFoundError")
|
|
80
73
|
return "not_found";
|
|
81
74
|
if (name === "AuthenticationError")
|
|
@@ -84,123 +77,44 @@ function codeOf(error) {
|
|
|
84
77
|
return "invalid";
|
|
85
78
|
if (name === "RateLimitError" || name === "NotEnoughSpaceError")
|
|
86
79
|
return "limit";
|
|
87
|
-
if (name === "
|
|
80
|
+
if (name === "SandboxUnreachableError")
|
|
88
81
|
return "timeout";
|
|
82
|
+
// envd's process stream, called through the SDK's Connect client (exec), throws Connect's own errors.
|
|
83
|
+
if (name === "ConnectError")
|
|
84
|
+
return CONNECT_CODES[error.code] ?? "vendor";
|
|
89
85
|
return "vendor";
|
|
90
86
|
}
|
|
91
87
|
/** E2B's end as create set it: its start plus the timeout create gave, by E2B's clock; undefined when unread. */
|
|
92
88
|
function createdEnd({ metadata, startedAt }) {
|
|
93
89
|
const timeout = Number(metadata[TIMEOUT_LABEL]);
|
|
94
|
-
const start = startedAt
|
|
95
|
-
return isWhole(timeout) && timeout > 0 &&
|
|
90
|
+
const start = dateOf(startedAt)?.getTime();
|
|
91
|
+
return isWhole(timeout) && timeout > 0 && start !== undefined ? start + timeout : undefined;
|
|
96
92
|
}
|
|
97
93
|
/** The earliest end a moved timer gives E2B: a second past create's end and its rounding, so deadlineOf reads it as moved. */
|
|
98
94
|
const clearOf = (created) => created + END_SLACK_MS + 1_000;
|
|
99
95
|
/**
|
|
100
|
-
* A sandbox
|
|
101
|
-
*
|
|
102
|
-
*
|
|
96
|
+
* A sandbox as the adapter reads a machine, from getInfo or the listing alike: E2B's state (a paused one is stopped), its
|
|
97
|
+
* start, its end. E2B keeps no metadata change, so once an extend moved its timer clear of create's end (extend), the
|
|
98
|
+
* deadline label reads that end, up to the most the computer may live: a restarted provider reads the extend all the same.
|
|
103
99
|
*/
|
|
104
|
-
function networkOf(info) {
|
|
105
|
-
const network = info.network ?? {};
|
|
106
|
-
if (network.egressProxy !== undefined && network.egressProxy !== null)
|
|
107
|
-
return { other: "an egress proxy is set on it" };
|
|
108
|
-
if (Array.isArray(network.allowOut) && network.allowOut.length > 0)
|
|
109
|
-
return { other: `an allowOut list (${network.allowOut.length} entries), which this provider never makes` };
|
|
110
|
-
if (network.rules !== undefined && network.rules !== null && Object.keys(network.rules).length > 0)
|
|
111
|
-
return { other: "transform rules, which this provider never makes" };
|
|
112
|
-
const denied = Array.isArray(network.denyOut) ? network.denyOut : [];
|
|
113
|
-
const blocked = info.allowInternetAccess === false || denied.includes("0.0.0.0/0");
|
|
114
|
-
if (!blocked && denied.length > 0)
|
|
115
|
-
return { other: `a denyOut list short of all traffic (${denied.join(", ")})` };
|
|
116
|
-
if (network.allowPublicTraffic !== false) {
|
|
117
|
-
return { other: network.allowPublicTraffic === true ? "public traffic is let in" : "E2B did not report allowPublicTraffic false: public traffic may be let in" };
|
|
118
|
-
}
|
|
119
|
-
return blocked ? "none" : "open";
|
|
120
|
-
}
|
|
121
|
-
/** A sandbox as the adapter reads a machine: E2B's states, its network, its template, its timer, and its create's drift. */
|
|
122
100
|
function machineOf(info) {
|
|
123
|
-
const
|
|
124
|
-
const end = info.endAt
|
|
125
|
-
const created = createdEnd({ metadata, startedAt: info.startedAt });
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const asked = metadata[IMAGE_LABEL];
|
|
131
|
-
const alias = info.name;
|
|
132
|
-
if (asked !== undefined && info.templateId !== asked && alias !== undefined && alias !== asked && !alias.startsWith(`${asked}:`)) {
|
|
133
|
-
drift.push(`template: declared ${show(asked)}, read back ${show(info.templateId)} (${alias})`);
|
|
101
|
+
const labels = { ...(info.metadata ?? {}) };
|
|
102
|
+
const end = dateOf(info.endAt);
|
|
103
|
+
const created = createdEnd({ metadata: labels, startedAt: info.startedAt });
|
|
104
|
+
// An end within END_SLACK_MS of create's is create's own, rounded: a moved timer never leaves E2B's end there (clearOf).
|
|
105
|
+
if (end !== undefined && created !== undefined && end.getTime() > created + END_SLACK_MS && isoOf(labels[DEADLINE_LABEL]) !== undefined) {
|
|
106
|
+
const most = isoOf(labels[MAX_LABEL])?.getTime() ?? Number.POSITIVE_INFINITY;
|
|
107
|
+
labels[DEADLINE_LABEL] = new Date(Math.min(end.getTime(), most)).toISOString();
|
|
134
108
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
mismatch("lifecycle autoResume", false, true);
|
|
139
|
-
// An end later than create's is a moved timer (timerMoved: an extend's, or one create refuses on a machine it just
|
|
140
|
-
// made); only an end it cannot read, or one earlier than create gave, is drift, at create and at attach alike.
|
|
141
|
-
if (Number.isNaN(end) || created === undefined)
|
|
142
|
-
drift.push(`endAt: E2B reported ${show(info.endAt)} from a start of ${show(info.startedAt)}, so it might not end the computer`);
|
|
143
|
-
else if (end < created - END_SLACK_MS)
|
|
144
|
-
drift.push(`endAt: ${info.endAt.toISOString()} is earlier than E2B's start plus the ${metadata[TIMEOUT_LABEL]} ms timeout create gave`);
|
|
145
|
-
return {
|
|
146
|
-
id: info.sandboxId,
|
|
147
|
-
labels: metadata,
|
|
148
|
-
state: info.state === "running" ? "running" : info.state === "paused" ? "stopped" : "going",
|
|
149
|
-
network: networkOf(info),
|
|
150
|
-
image: { resolved: info.templateId },
|
|
151
|
-
...(info.startedAt instanceof Date && !Number.isNaN(info.startedAt.getTime()) ? { createdAt: new Date(info.startedAt) } : {}),
|
|
152
|
-
...(Number.isNaN(end) ? {} : { endsAt: new Date(end) }),
|
|
153
|
-
// An end within END_SLACK_MS of create's is create's own, rounded: a moved timer never leaves E2B's end there (clearOf).
|
|
154
|
-
timerMoved: !Number.isNaN(end) && created !== undefined && end > created + END_SLACK_MS,
|
|
155
|
-
...(drift.length > 0 ? { drift } : {}),
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
/** A listed sandbox whose read-back E2B no longer has: still listed, so it is going, not gone. */
|
|
159
|
-
const listedOnly = (sb) => ({
|
|
160
|
-
id: sb.sandboxId,
|
|
161
|
-
labels: { ...(sb.metadata ?? {}) },
|
|
162
|
-
state: "going",
|
|
163
|
-
...(sb.startedAt instanceof Date && !Number.isNaN(sb.startedAt.getTime()) ? { createdAt: new Date(sb.startedAt) } : {}),
|
|
164
|
-
});
|
|
165
|
-
/** A stream fed by the SDK's callbacks, read as an async iterable until it ends. */
|
|
166
|
-
function feed() {
|
|
167
|
-
const queue = [];
|
|
168
|
-
let ended = false;
|
|
169
|
-
let wake;
|
|
170
|
-
const nudge = () => {
|
|
171
|
-
const waiting = wake;
|
|
172
|
-
wake = undefined;
|
|
173
|
-
waiting?.();
|
|
174
|
-
};
|
|
175
|
-
return {
|
|
176
|
-
push(bytes) {
|
|
177
|
-
if (ended)
|
|
178
|
-
return;
|
|
179
|
-
queue.push(bytes);
|
|
180
|
-
nudge();
|
|
181
|
-
},
|
|
182
|
-
end() {
|
|
183
|
-
ended = true;
|
|
184
|
-
nudge();
|
|
185
|
-
},
|
|
186
|
-
iterable: {
|
|
187
|
-
async *[Symbol.asyncIterator]() {
|
|
188
|
-
for (;;) {
|
|
189
|
-
const next = queue.shift();
|
|
190
|
-
if (next !== undefined)
|
|
191
|
-
yield next;
|
|
192
|
-
else if (ended)
|
|
193
|
-
return;
|
|
194
|
-
else
|
|
195
|
-
await new Promise((done) => (wake = done));
|
|
196
|
-
}
|
|
197
|
-
},
|
|
198
|
-
},
|
|
199
|
-
};
|
|
109
|
+
const started = dateOf(info.startedAt);
|
|
110
|
+
const state = info.state === "running" ? "running" : info.state === "paused" ? "stopped" : "gone";
|
|
111
|
+
return { id: info.sandboxId, labels, state, ...(started ? { createdAt: started } : {}), ...(end ? { endsAt: end } : {}) };
|
|
200
112
|
}
|
|
113
|
+
/** One chunk of the SDK's output into a stream, unless the stream has ended. */
|
|
114
|
+
const push = (stream, data) => void (stream.writableEnded || stream.write(data));
|
|
201
115
|
/** E2B's sandboxes as a MachineHost (../host.ts). */
|
|
202
116
|
class E2BHost {
|
|
203
|
-
|
|
117
|
+
descriptor = E2B;
|
|
204
118
|
facts = {
|
|
205
119
|
daemon: "child",
|
|
206
120
|
isolation: "microvm",
|
|
@@ -208,10 +122,10 @@ class E2BHost {
|
|
|
208
122
|
maxLifetimeMs: PRO_MAX_MS,
|
|
209
123
|
arch: "amd64",
|
|
210
124
|
vendorAgentPorts: [ENVD_PORT],
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
125
|
+
// The box template has xfwm4 (box/Dockerfile), as E2B Desktop's has: the apps an agent opens are framed and focused.
|
|
126
|
+
windowManager: true,
|
|
127
|
+
// System paths whose permissions E2B's template build sets wider than root's, on its templates and ours alike.
|
|
128
|
+
rootOnly: ROOT_ONLY,
|
|
215
129
|
};
|
|
216
130
|
#Sandbox;
|
|
217
131
|
#connection;
|
|
@@ -230,49 +144,43 @@ class E2BHost {
|
|
|
230
144
|
#api(ms, signal) {
|
|
231
145
|
return { ...this.#connection, requestTimeoutMs: ms, ...(signal ? { signal } : {}) };
|
|
232
146
|
}
|
|
233
|
-
/** An E2B template is a name or id: letters, digits and ._:/- (no shell word, no path out). */
|
|
147
|
+
/** An E2B template is a name or id: letters, digits and ._:/- (no shell word, no path out); its size is the template's. */
|
|
234
148
|
check(spec) {
|
|
235
|
-
const
|
|
236
|
-
if (typeof image === "string" && image !== "" && !TEMPLATE.test(image)) {
|
|
237
|
-
|
|
149
|
+
const refusals = sizeRefusals(spec, "an E2B computer", "template");
|
|
150
|
+
if (typeof spec.image === "string" && spec.image !== "" && !TEMPLATE.test(spec.image)) {
|
|
151
|
+
refusals.push({ field: "image", message: `an image is the name or id of an E2B template, such as "${boxE2bTemplate()}" (the box template) or "desktop" (E2B Desktop)` });
|
|
238
152
|
}
|
|
239
|
-
return
|
|
153
|
+
return refusals;
|
|
240
154
|
}
|
|
241
155
|
/**
|
|
242
156
|
* Sandbox.create with the lifetime as its timeout in the whole seconds E2B takes (index.mjs:502-504), recorded in a
|
|
243
|
-
* label for its end's arithmetic; no internet access unless the adapter asks
|
|
157
|
+
* label for its end's arithmetic; no internet access unless the adapter asks egress (an Archil computer's root, or the
|
|
158
|
+
* egress proxy of a network open or a list), no
|
|
244
159
|
* public traffic (a project's default may let it in: E2B docs, network/restrict-public-access), and kill at the
|
|
245
160
|
* timeout, never a pause that keeps the computer (index.mjs:5674-5699).
|
|
246
161
|
*/
|
|
247
162
|
async create(req, ctx) {
|
|
248
|
-
const timeoutMs = Math.ceil(
|
|
163
|
+
const timeoutMs = Math.ceil(req.lifetimeMs / 1000) * 1000;
|
|
249
164
|
const options = {
|
|
250
165
|
...this.#api(ctx.timeoutMs),
|
|
251
166
|
timeoutMs,
|
|
252
167
|
metadata: { ...req.labels, [TIMEOUT_LABEL]: String(timeoutMs) },
|
|
253
168
|
envs: {},
|
|
254
|
-
allowInternetAccess: req.
|
|
169
|
+
allowInternetAccess: req.egress,
|
|
255
170
|
network: { allowPublicTraffic: false },
|
|
256
171
|
lifecycle: { onTimeout: "kill" },
|
|
257
172
|
};
|
|
258
173
|
const sandbox = await this.#Sandbox.create(req.image, options);
|
|
259
174
|
this.#sandboxes.set(sandbox.sandboxId, sandbox);
|
|
260
|
-
return
|
|
175
|
+
return { id: sandbox.sandboxId };
|
|
261
176
|
}
|
|
262
|
-
/** getInfo; a sandbox E2B does not have (SandboxNotFoundError, index.mjs:5431) is none. */
|
|
177
|
+
/** getInfo; a sandbox E2B does not have (SandboxNotFoundError, index.mjs:5431) is not_found, which the adapter reads as none. */
|
|
263
178
|
async get(id, ctx) {
|
|
264
|
-
|
|
265
|
-
return ok(machineOf(await this.#Sandbox.getInfo(id, this.#api(ctx.timeoutMs, ctx.signal))));
|
|
266
|
-
}
|
|
267
|
-
catch (error) {
|
|
268
|
-
if (codeOf(error) === "not_found")
|
|
269
|
-
return ok(undefined);
|
|
270
|
-
throw error;
|
|
271
|
-
}
|
|
179
|
+
return machineOf(await this.#Sandbox.getInfo(id, this.#api(ctx.timeoutMs, ctx.signal)));
|
|
272
180
|
}
|
|
273
181
|
/**
|
|
274
|
-
* Every sandbox E2B lists with these metadata pairs (running and paused, index.d.mts:10289), all pages
|
|
275
|
-
*
|
|
182
|
+
* Every sandbox E2B lists with these metadata pairs (running and paused, index.d.mts:10289), all pages within
|
|
183
|
+
* ctx.timeoutMs, each as getInfo reads it (the listing carries its end too).
|
|
276
184
|
*/
|
|
277
185
|
async list(labels, ctx) {
|
|
278
186
|
const until = Date.now() + ctx.timeoutMs;
|
|
@@ -281,36 +189,20 @@ class E2BHost {
|
|
|
281
189
|
const found = new Map();
|
|
282
190
|
for (let page = 0; paginator.hasNext; page++) {
|
|
283
191
|
if (page >= MAX_PAGES)
|
|
284
|
-
|
|
192
|
+
throw new Error(`Sandbox.list ran past ${MAX_PAGES} pages`);
|
|
285
193
|
const items = await paginator.nextItems({ requestTimeoutMs: left(), ...(ctx.signal ? { signal: ctx.signal } : {}) });
|
|
286
194
|
if (!Array.isArray(items))
|
|
287
|
-
|
|
195
|
+
throw new Error("Sandbox.list: a page that is not a list of sandboxes");
|
|
288
196
|
for (const sb of items)
|
|
289
197
|
if (typeof sb?.sandboxId === "string")
|
|
290
198
|
found.set(sb.sandboxId, sb);
|
|
291
199
|
}
|
|
292
|
-
|
|
293
|
-
for (const sb of found.values()) {
|
|
294
|
-
if (Date.now() >= until)
|
|
295
|
-
return fail("timeout", `${machines.length} sandboxes were read back, ${sb.sandboxId} and any after it were not`);
|
|
296
|
-
const read = await this.get(sb.sandboxId, { timeoutMs: left(), ...(ctx.signal ? { signal: ctx.signal } : {}) });
|
|
297
|
-
if (!read.ok)
|
|
298
|
-
return read;
|
|
299
|
-
machines.push(read.value ?? listedOnly(sb));
|
|
300
|
-
}
|
|
301
|
-
return ok(machines);
|
|
200
|
+
return [...found.values()].map(machineOf);
|
|
302
201
|
}
|
|
303
|
-
/** Sandbox.kill, never signalled: a kill that cleans up after an abort must outlive it.
|
|
202
|
+
/** Sandbox.kill, never signalled: a kill that cleans up after an abort must outlive it. */
|
|
304
203
|
async delete(id, ctx) {
|
|
305
204
|
this.#sandboxes.delete(id);
|
|
306
|
-
|
|
307
|
-
await this.#Sandbox.kill(id, this.#api(ctx.timeoutMs));
|
|
308
|
-
}
|
|
309
|
-
catch (error) {
|
|
310
|
-
if (codeOf(error) !== "not_found")
|
|
311
|
-
throw error;
|
|
312
|
-
}
|
|
313
|
-
return ok(undefined);
|
|
205
|
+
await this.#Sandbox.kill(id, this.#api(ctx.timeoutMs));
|
|
314
206
|
}
|
|
315
207
|
/**
|
|
316
208
|
* A connected Sandbox for envd's commands and files: the one create gave, or Sandbox.connect, whose timeout is a
|
|
@@ -333,60 +225,100 @@ class E2BHost {
|
|
|
333
225
|
const sandbox = await this.#sandbox(id, ctx);
|
|
334
226
|
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
335
227
|
await sandbox.files.write(path, buffer, { user: "root", requestTimeoutMs: ctx.timeoutMs, ...(ctx.signal ? { signal: ctx.signal } : {}) });
|
|
336
|
-
return ok(undefined);
|
|
337
228
|
}
|
|
338
229
|
/**
|
|
339
|
-
* A
|
|
340
|
-
*
|
|
341
|
-
*
|
|
230
|
+
* A process as root through envd's Process.Start stream, the call commands.run makes (index.mjs:5123-5146) without its
|
|
231
|
+
* login shell: /bin/sh by its path runs the argv as its own words, with PATH the system's directories alone, root by the
|
|
232
|
+
* Basic header (authenticationHeader, index.mjs:1896-1900), and envd's keepalive every 50 s. Its stdout and stderr are the bytes each data event carries, passed on as they come: commands.run would
|
|
233
|
+
* decode them as UTF-8 text first. No deadline on the stream (timeoutMs 0, which the Connect transport reads as none,
|
|
234
|
+
* connect-web connect-transport.js:145-150), so envd never ends it (start.go:39-44); the start alone is bounded by
|
|
235
|
+
* ctx. Writes go one sendStdin at a time, in order; end is closeStdin; kill SIGKILLs it and lets go of its stream.
|
|
342
236
|
*/
|
|
343
|
-
async exec(id, argv,
|
|
237
|
+
async exec(id, argv, ctx) {
|
|
344
238
|
const sandbox = await this.#sandbox(id, ctx);
|
|
345
|
-
const out =
|
|
346
|
-
const err =
|
|
239
|
+
const out = new PassThrough();
|
|
240
|
+
const err = new PassThrough();
|
|
347
241
|
const requestTimeoutMs = this.#requestMs;
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
}
|
|
362
|
-
|
|
242
|
+
const stream = new AbortController();
|
|
243
|
+
const events = sandbox.commands.rpc.start({ process: { cmd: ROOT_SHELL, args: [...ROOT_SHELL_ARGS, ...argv], envs: { PATH: ROOT_PATH } }, stdin: true }, { headers: { Authorization: ROOT, [KEEPALIVE_HEADER]: KEEPALIVE_S }, signal: stream.signal, timeoutMs: 0 })[Symbol.asyncIterator]();
|
|
244
|
+
// The start, within ctx: a stream that gives no start event in time, or the caller's abort, is let go of.
|
|
245
|
+
const startAbort = () => stream.abort(ctx.signal?.reason);
|
|
246
|
+
ctx.signal?.addEventListener("abort", startAbort, { once: true });
|
|
247
|
+
const startTimer = setTimeout(() => stream.abort(), ctx.timeoutMs);
|
|
248
|
+
let pid;
|
|
249
|
+
try {
|
|
250
|
+
const first = await events.next();
|
|
251
|
+
const event = first.done ? undefined : first.value.event?.event;
|
|
252
|
+
if (event?.case !== "start")
|
|
253
|
+
throw new Error(`envd's process stream began with ${event?.case ?? "nothing"}, not its start`);
|
|
254
|
+
pid = event.value.pid;
|
|
255
|
+
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
const why = stream.signal.aborted ? (ctx.signal?.aborted ? "AbortError" : "TimeoutError") : undefined;
|
|
258
|
+
stream.abort();
|
|
259
|
+
if (why === undefined)
|
|
260
|
+
throw error;
|
|
261
|
+
throw Object.assign(new Error(why === "AbortError" ? "the exec was aborted before envd started it" : `envd did not start the process within ${ctx.timeoutMs / 1000} s`), { name: why });
|
|
262
|
+
}
|
|
263
|
+
finally {
|
|
264
|
+
clearTimeout(startTimer);
|
|
265
|
+
ctx.signal?.removeEventListener("abort", startAbort);
|
|
266
|
+
}
|
|
267
|
+
const exited = (async () => {
|
|
268
|
+
let code = -1;
|
|
269
|
+
try {
|
|
270
|
+
for (;;) {
|
|
271
|
+
const next = await events.next();
|
|
272
|
+
if (next.done)
|
|
273
|
+
break;
|
|
274
|
+
const event = next.value.event?.event;
|
|
275
|
+
if (event?.case === "data") {
|
|
276
|
+
const { output } = event.value;
|
|
277
|
+
if (output.case === "stdout")
|
|
278
|
+
push(out, output.value);
|
|
279
|
+
else if (output.case === "stderr")
|
|
280
|
+
push(err, output.value);
|
|
281
|
+
}
|
|
282
|
+
else if (event?.case === "end") {
|
|
283
|
+
code = event.value.exitCode;
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
// A stream that broke, or one kill let go of: no exit code was read.
|
|
290
|
+
}
|
|
363
291
|
out.end();
|
|
364
292
|
err.end();
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
293
|
+
stream.abort();
|
|
294
|
+
return code;
|
|
295
|
+
})();
|
|
296
|
+
return {
|
|
297
|
+
stdout: out,
|
|
298
|
+
stderr: err,
|
|
299
|
+
write: (bytes) => sandbox.commands.sendStdin(pid, bytes, { requestTimeoutMs }),
|
|
300
|
+
end: () => sandbox.commands.closeStdin(pid, { requestTimeoutMs }),
|
|
372
301
|
wait: () => exited,
|
|
373
302
|
kill: async () => {
|
|
374
|
-
await
|
|
375
|
-
|
|
303
|
+
await sandbox.commands.kill(pid, { requestTimeoutMs }).catch(() => false);
|
|
304
|
+
stream.abort();
|
|
376
305
|
out.end();
|
|
377
306
|
err.end();
|
|
378
307
|
},
|
|
379
|
-
}
|
|
308
|
+
};
|
|
380
309
|
}
|
|
381
310
|
/**
|
|
382
311
|
* Moves E2B's timer (setTimeout, index.mjs:5529-5541) in the whole seconds E2B takes: up to clear create's end, so
|
|
383
|
-
* the end reads as moved (deadlineOf), and down at the most the computer may live, so E2B's
|
|
384
|
-
* this machine's clock. A timer that would end before the deadline held now, or within
|
|
385
|
-
* as limit, and nothing moves.
|
|
312
|
+
* the end reads as moved (deadlineOf), and down at the most the computer may live (its max-deadline label), so E2B's
|
|
313
|
+
* timer never passes it by this machine's clock. A timer that would end before the deadline held now, or within
|
|
314
|
+
* create's rounding, is refused as limit, and nothing moves.
|
|
386
315
|
*/
|
|
387
|
-
async
|
|
316
|
+
async extend(id, end, ctx) {
|
|
388
317
|
const info = await this.#Sandbox.getInfo(id, this.#api(ctx.timeoutMs, ctx.signal));
|
|
389
318
|
const machine = machineOf(info);
|
|
319
|
+
const most = new Date(machine.labels[MAX_LABEL] ?? Number.NaN);
|
|
320
|
+
if (Number.isNaN(most.getTime()))
|
|
321
|
+
throw new HostError("refused", `${id}'s label ${MAX_LABEL} reads ${show(machine.labels[MAX_LABEL])}, so the most it may live is unknown; nothing moved`);
|
|
390
322
|
const now = Date.now();
|
|
391
323
|
const target = end.getTime();
|
|
392
324
|
const created = createdEnd(info);
|
|
@@ -397,45 +329,37 @@ class E2BHost {
|
|
|
397
329
|
const held = Number.isNaN(vendorEnd) ? current : Math.max(current, vendorEnd);
|
|
398
330
|
const name = info.metadata?.["agentrun.computer.name"] ?? id;
|
|
399
331
|
if (now + timerMs < held) {
|
|
400
|
-
|
|
332
|
+
throw new HostError("limit", `no whole number of seconds E2B takes moves ${name}'s end from ${new Date(held).toISOString()} to ${end.toISOString()} without passing ${most.toISOString()}, the most this computer may live; nothing moved`);
|
|
401
333
|
}
|
|
402
334
|
if (created !== undefined && now + timerMs < clearOf(created)) {
|
|
403
|
-
|
|
335
|
+
throw new HostError("limit", `${end.toISOString()} is within E2B's rounding of the end create gave ${name}, and moving E2B's end clear of it would pass ${most.toISOString()}, the most this computer may live`);
|
|
404
336
|
}
|
|
405
337
|
await this.#Sandbox.setTimeout(id, timerMs, this.#api(ctx.timeoutMs, ctx.signal));
|
|
406
|
-
return ok(undefined);
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Settings' test: one page of the account's sandboxes by the adapter's own metadata, which makes nothing. The count is
|
|
410
|
-
* read from the listing, with no getInfo of each: a busy account answers as fast, and one sandbox's read never fails a
|
|
411
|
-
* working key. A further page is "or more".
|
|
412
|
-
*/
|
|
413
|
-
async testConnection(ctx) {
|
|
414
|
-
try {
|
|
415
|
-
const paginator = this.#Sandbox.list({ ...this.#connection, requestTimeoutMs: ctx.timeoutMs, query: { metadata: { "agentrun.computer": "1" } }, limit: PAGE });
|
|
416
|
-
const items = await paginator.nextItems({ requestTimeoutMs: ctx.timeoutMs, ...(ctx.signal ? { signal: ctx.signal } : {}) });
|
|
417
|
-
if (!Array.isArray(items))
|
|
418
|
-
return countTested("E2B", fail("vendor", "Sandbox.list: a page that is not a list of sandboxes"));
|
|
419
|
-
const count = items.filter((sb) => typeof sb?.sandboxId === "string" && ours(sb.metadata)).length;
|
|
420
|
-
return countTested("E2B", ok({ count, more: paginator.hasNext }));
|
|
421
|
-
}
|
|
422
|
-
catch (error) {
|
|
423
|
-
return countTested("E2B", fail(codeOf(error), `Sandbox.list failed: ${words(error)}`));
|
|
424
|
-
}
|
|
425
338
|
}
|
|
426
339
|
}
|
|
427
340
|
/**
|
|
428
|
-
* Computers that are E2B sandboxes, on the shared adapter. A spec's image is an E2B template (its name or id)
|
|
429
|
-
*
|
|
341
|
+
* Computers that are E2B sandboxes, on the shared adapter. A spec's image is an E2B template (its name or id). A server
|
|
342
|
+
* starts every one, sandbox or desktop, from the box template prepareImage() answers: the box image (box/Dockerfile)
|
|
343
|
+
* with Archil's client and its root helper, Xvfb, xfwm4 and the desktop's apps, which the provider starts through
|
|
344
|
+
* computerd (desktop.ts). Any other template a spec names must carry what the computer's kind needs.
|
|
430
345
|
*/
|
|
431
346
|
export class E2BProvider extends HostedProvider {
|
|
347
|
+
/**
|
|
348
|
+
* The provider contract's prepareImage (its descriptor declares it): the box template the release publishes,
|
|
349
|
+
* public under its E2B team (box-image.ts boxE2bTemplate), which any E2B account starts by that name. No call: E2B
|
|
350
|
+
* starts a public template at create with nothing to pull ahead of it, so onWait is never told. A desktop starts
|
|
351
|
+
* from the same template as a sandbox: the kind is the provider's, not the image's.
|
|
352
|
+
*/
|
|
353
|
+
async prepareImage() {
|
|
354
|
+
return ok(boxE2bTemplate());
|
|
355
|
+
}
|
|
432
356
|
constructor(options) {
|
|
433
357
|
// A caller who still names the option is told it is gone, whatever its value: it once let public traffic in.
|
|
434
358
|
if (typeof options === "object" && options !== null && Object.hasOwn(options, "publicStream"))
|
|
435
359
|
throw new Error(PUBLIC);
|
|
436
360
|
const { apiKey, Sandbox, domain = DEFAULT_DOMAIN, kind = "desktop", maxLifetimeMs = HOBBY_MAX_MS, ...rest } = options;
|
|
437
361
|
if (typeof apiKey !== "string" || apiKey === "")
|
|
438
|
-
throw new Error("
|
|
362
|
+
throw new Error("options.apiKey, the E2B API key, is required: the provider reads no environment, so the caller passes it");
|
|
439
363
|
const lacks = ["create", "connect", "getInfo", "setTimeout", "kill", "list"].filter((name) => typeof Sandbox?.[name] !== "function");
|
|
440
364
|
if (lacks.length > 0)
|
|
441
365
|
throw new Error(`options.Sandbox must be the e2b SDK's Sandbox class; it lacks ${lacks.join(", ")}`);
|
|
@@ -444,10 +368,6 @@ export class E2BProvider extends HostedProvider {
|
|
|
444
368
|
throw new Error(ambient);
|
|
445
369
|
if (typeof domain !== "string" || !DOMAIN.test(domain))
|
|
446
370
|
throw new Error(`the E2B domain must be a host name such as ${DEFAULT_DOMAIN}, not ${JSON.stringify(domain)}`);
|
|
447
|
-
|
|
448
|
-
throw new Error(`maxLifetimeMs must be a whole number of ms above 0 and at most 24 h (${PRO_MAX_MS} ms), E2B Pro's longest sandbox; Hobby's is 1 h`);
|
|
449
|
-
}
|
|
450
|
-
const requestMs = rest.timeouts?.requestMs ?? DEFAULT_REQUEST_MS;
|
|
451
|
-
super(new E2BHost(Sandbox, connectionOptions({ apiKey, domain }), requestMs), { ...rest, label: "E2B", kind, maxLifetimeMs });
|
|
371
|
+
super(new E2BHost(Sandbox, connectionOptions({ apiKey, domain }), timeoutsOf(rest.timeouts).requestMs), { ...rest, kind, maxLifetimeMs });
|
|
452
372
|
}
|
|
453
373
|
}
|