@vms.dev/eve 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +34 -2
- package/index.d.ts +62 -0
- package/index.js +632 -0
- package/package.json +39 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 vms.dev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @vms.dev/eve
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The [vms.dev](https://vms.dev) sandbox provider for [eve](https://github.com/vercel/eve) 1.0. Each eve session gets its own vms.dev sandbox. The `bash`, `read_file`, `write_file`, and `edit_file` tools of the agent run in that sandbox.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @vms.dev/eve
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The package runs where eve runs: on Node.js 24 or later, or on Bun.
|
|
12
|
+
|
|
13
|
+
## Use
|
|
14
|
+
|
|
15
|
+
Save this code in `agent/sandbox.ts`:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { VmsSandbox } from "@vms.dev/eve";
|
|
19
|
+
import { defineSandbox } from "eve/sandbox";
|
|
20
|
+
|
|
21
|
+
export const environment = VmsSandbox.environment({ size: "small" });
|
|
22
|
+
|
|
23
|
+
export default defineSandbox(() => environment.open());
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The provider reads the API key from the `VMS_API_KEY` environment variable.
|
|
27
|
+
|
|
28
|
+
## Docs
|
|
29
|
+
|
|
30
|
+
- [Run eve agents in sandboxes](https://vms.dev/docs/sandboxes/eve)
|
|
31
|
+
- [eve provider reference](https://vms.dev/docs/reference/eve-provider)
|
|
32
|
+
|
|
33
|
+
## License
|
|
34
|
+
|
|
35
|
+
MIT
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { SandboxSize } from "@vms.dev/sdk";
|
|
2
|
+
import { SandboxConnection, SandboxProviderDefinition } from "eve/sandbox/provider";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
type VmsSandboxOptions = {
|
|
5
|
+
apiKey?: string;
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
fetch?: typeof fetch;
|
|
8
|
+
namespace?: string;
|
|
9
|
+
size?: SandboxSize;
|
|
10
|
+
labels?: Record<string, string>;
|
|
11
|
+
env?: Record<string, string>;
|
|
12
|
+
workspaceRoot?: string;
|
|
13
|
+
stopAfterSeconds?: number;
|
|
14
|
+
stopWhenIdleSeconds?: number | null;
|
|
15
|
+
waitTimeoutMs?: number;
|
|
16
|
+
};
|
|
17
|
+
type VmsSandboxOpenOptions = {
|
|
18
|
+
size?: SandboxSize;
|
|
19
|
+
labels?: Record<string, string>;
|
|
20
|
+
};
|
|
21
|
+
export declare const DEFAULT_SIZE: SandboxSize;
|
|
22
|
+
export declare const DEFAULT_WORKSPACE_ROOT = "/workspace";
|
|
23
|
+
export declare const DEFAULT_STOP_AFTER_SECONDS = 3600;
|
|
24
|
+
export declare const DEFAULT_STOP_WHEN_IDLE_SECONDS = 60;
|
|
25
|
+
export declare const DEFAULT_WAIT_TIMEOUT_MS = 120000;
|
|
26
|
+
type VmsSandboxExtensions = {
|
|
27
|
+
readonly sandboxId: string;
|
|
28
|
+
};
|
|
29
|
+
type VmsSandboxConnection = SandboxConnection<VmsSandboxExtensions>;
|
|
30
|
+
type VmsSetupFile = {
|
|
31
|
+
path: string;
|
|
32
|
+
content: string;
|
|
33
|
+
};
|
|
34
|
+
type VmsSetupStep = {
|
|
35
|
+
kind: "files";
|
|
36
|
+
files: VmsSetupFile[];
|
|
37
|
+
} | {
|
|
38
|
+
kind: "remove";
|
|
39
|
+
path: string;
|
|
40
|
+
force: boolean;
|
|
41
|
+
recursive: boolean;
|
|
42
|
+
} | {
|
|
43
|
+
kind: "command";
|
|
44
|
+
command: string;
|
|
45
|
+
workingDirectory: string;
|
|
46
|
+
env: Record<string, string>;
|
|
47
|
+
exitCode: number;
|
|
48
|
+
};
|
|
49
|
+
type VmsSandboxArtifact = {
|
|
50
|
+
steps: VmsSetupStep[];
|
|
51
|
+
};
|
|
52
|
+
export declare const PREPARE_LABEL = "eve-prepare";
|
|
53
|
+
export declare const SANDBOX_NAME_PREFIX = "eve-";
|
|
54
|
+
declare const stateSchema: z.ZodObject<{
|
|
55
|
+
id: z.ZodString;
|
|
56
|
+
name: z.ZodString;
|
|
57
|
+
}, z.core.$strip>;
|
|
58
|
+
type VmsSandboxState = z.infer<typeof stateSchema>;
|
|
59
|
+
export declare function sandboxNameOf(sessionId: string, namespace?: string): string;
|
|
60
|
+
export declare const vmsSandboxProvider: SandboxProviderDefinition<VmsSandboxOptions, VmsSandboxOpenOptions | undefined, VmsSandboxArtifact, VmsSandboxState, VmsSandboxExtensions>;
|
|
61
|
+
export declare const VmsSandbox: import("eve/sandbox/provider").SandboxProvider<import("eve/sandbox/provider").WithPrepareOption<VmsSandboxOptions, import("eve/sandbox").SandboxSession & VmsSandboxExtensions>, VmsSandboxOpenOptions | undefined, import("eve/sandbox").SandboxSession & VmsSandboxExtensions>;
|
|
62
|
+
export type { VmsSandboxArtifact, VmsSandboxConnection, VmsSandboxExtensions, VmsSandboxOpenOptions, VmsSandboxOptions, VmsSandboxState, VmsSetupFile, VmsSetupStep };
|
package/index.js
ADDED
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
import { Vms, VmsError } from "@vms.dev/sdk";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { SANDBOX_AGENT_ENV, SandboxSessionGoneError, defineSandboxProvider } from "eve/sandbox/provider";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
// src/options.ts
|
|
6
|
+
const DEFAULT_SIZE = "small";
|
|
7
|
+
const DEFAULT_WORKSPACE_ROOT = "/workspace";
|
|
8
|
+
const DEFAULT_STOP_AFTER_SECONDS = 3600;
|
|
9
|
+
const DEFAULT_STOP_WHEN_IDLE_SECONDS = 60;
|
|
10
|
+
const DEFAULT_WAIT_TIMEOUT_MS = 12e4;
|
|
11
|
+
function absoluteRoot(root) {
|
|
12
|
+
if (!root.startsWith("/")) throw new TypeError(`The workspace root ${root} is not an absolute path, such as /workspace.`);
|
|
13
|
+
return root.replace(/(.)\/+$/, "$1");
|
|
14
|
+
}
|
|
15
|
+
function settingsOf(options) {
|
|
16
|
+
let vms = null;
|
|
17
|
+
return {
|
|
18
|
+
client() {
|
|
19
|
+
vms ??= new Vms({
|
|
20
|
+
apiKey: options.apiKey,
|
|
21
|
+
baseUrl: options.baseUrl,
|
|
22
|
+
fetch: options.fetch
|
|
23
|
+
});
|
|
24
|
+
return vms;
|
|
25
|
+
},
|
|
26
|
+
namespace: options.namespace ?? "",
|
|
27
|
+
size: options.size ?? "small",
|
|
28
|
+
labels: options.labels ?? {},
|
|
29
|
+
env: options.env ?? {},
|
|
30
|
+
workspaceRoot: absoluteRoot(options.workspaceRoot ?? "/workspace"),
|
|
31
|
+
stopAfterSeconds: options.stopAfterSeconds ?? 3600,
|
|
32
|
+
stopWhenIdleSeconds: options.stopWhenIdleSeconds === void 0 ? 60 : options.stopWhenIdleSeconds,
|
|
33
|
+
waitTimeoutMs: options.waitTimeoutMs ?? 12e4
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
// src/process.ts
|
|
37
|
+
const SIGNAL_NUMBERS = {
|
|
38
|
+
SIGHUP: 1,
|
|
39
|
+
SIGINT: 2,
|
|
40
|
+
SIGQUIT: 3,
|
|
41
|
+
SIGKILL: 9,
|
|
42
|
+
SIGUSR1: 10,
|
|
43
|
+
SIGUSR2: 12,
|
|
44
|
+
SIGTERM: 15
|
|
45
|
+
};
|
|
46
|
+
const MAX_REATTACHES = 5;
|
|
47
|
+
function exitCodeOf(exit) {
|
|
48
|
+
if (exit.exitCode !== null) return exit.exitCode;
|
|
49
|
+
return exit.signal ? 128 + SIGNAL_NUMBERS[exit.signal] : 1;
|
|
50
|
+
}
|
|
51
|
+
function bytesOf$1(event) {
|
|
52
|
+
if (event.encoding === "utf8") return new TextEncoder().encode(event.data);
|
|
53
|
+
return Uint8Array.from(atob(event.data), (character) => character.charCodeAt(0));
|
|
54
|
+
}
|
|
55
|
+
function outputStream() {
|
|
56
|
+
let controller = null;
|
|
57
|
+
let done = false;
|
|
58
|
+
return {
|
|
59
|
+
stream: new ReadableStream({
|
|
60
|
+
start(started) {
|
|
61
|
+
controller = started;
|
|
62
|
+
},
|
|
63
|
+
cancel() {
|
|
64
|
+
done = true;
|
|
65
|
+
}
|
|
66
|
+
}),
|
|
67
|
+
push(bytes) {
|
|
68
|
+
if (!done) controller?.enqueue(bytes);
|
|
69
|
+
},
|
|
70
|
+
end() {
|
|
71
|
+
if (done) return;
|
|
72
|
+
done = true;
|
|
73
|
+
controller?.close();
|
|
74
|
+
},
|
|
75
|
+
fail(error) {
|
|
76
|
+
if (done) return;
|
|
77
|
+
done = true;
|
|
78
|
+
controller?.error(error);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function ignoreFinishedCommand(error) {
|
|
83
|
+
if (error instanceof VmsError && (error.status === 404 || error.status === 409)) return;
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
async function readUntilExit(vms, run, signal, onEvent) {
|
|
87
|
+
let events = run;
|
|
88
|
+
let lastSeq = -1;
|
|
89
|
+
for (let reattaches = 0;; reattaches += 1) {
|
|
90
|
+
try {
|
|
91
|
+
for await (const event of events) {
|
|
92
|
+
lastSeq = event.seq;
|
|
93
|
+
onEvent(event);
|
|
94
|
+
if (event.type === "exit") return event;
|
|
95
|
+
}
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (signal?.aborted || reattaches >= MAX_REATTACHES) throw error;
|
|
98
|
+
}
|
|
99
|
+
signal?.throwIfAborted();
|
|
100
|
+
if (reattaches >= MAX_REATTACHES) throw new Error(`The output of the command ${run.commandId} stopped before the command exited.`);
|
|
101
|
+
events = await vms.attach(run.sandboxId, run.commandId, {
|
|
102
|
+
after: lastSeq,
|
|
103
|
+
signal
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async function spawnProcess(vms, sandboxId, request) {
|
|
108
|
+
const run = await vms.exec(sandboxId, request.command, {
|
|
109
|
+
cwd: request.workingDirectory,
|
|
110
|
+
env: request.env,
|
|
111
|
+
stdin: request.stdin,
|
|
112
|
+
signal: request.signal
|
|
113
|
+
});
|
|
114
|
+
const stdout = outputStream();
|
|
115
|
+
const stderr = outputStream();
|
|
116
|
+
const kill = () => run.kill().then(() => void 0, ignoreFinishedCommand);
|
|
117
|
+
const aborted = () => {
|
|
118
|
+
kill().catch(() => void 0);
|
|
119
|
+
};
|
|
120
|
+
request.signal?.addEventListener("abort", aborted, { once: true });
|
|
121
|
+
const exited = readUntilExit(vms, run, request.signal, (event) => {
|
|
122
|
+
if (event.type === "stdout") stdout.push(bytesOf$1(event));
|
|
123
|
+
if (event.type === "stderr") stderr.push(bytesOf$1(event));
|
|
124
|
+
}).then((exit) => {
|
|
125
|
+
stdout.end();
|
|
126
|
+
stderr.end();
|
|
127
|
+
return { exitCode: exitCodeOf(exit) };
|
|
128
|
+
}).catch((error) => {
|
|
129
|
+
stdout.fail(error);
|
|
130
|
+
stderr.fail(error);
|
|
131
|
+
throw error;
|
|
132
|
+
}).finally(() => {
|
|
133
|
+
request.signal?.removeEventListener("abort", aborted);
|
|
134
|
+
});
|
|
135
|
+
exited.catch(() => void 0);
|
|
136
|
+
return {
|
|
137
|
+
stdin: request.stdin ? new WritableStream({
|
|
138
|
+
write: (chunk) => run.writeStdin(new Uint8Array(chunk)),
|
|
139
|
+
close: () => run.closeStdin(),
|
|
140
|
+
abort: () => run.closeStdin().catch(ignoreFinishedCommand)
|
|
141
|
+
}) : void 0,
|
|
142
|
+
stdout: stdout.stream,
|
|
143
|
+
stderr: stderr.stream,
|
|
144
|
+
wait: () => exited,
|
|
145
|
+
kill
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
// src/sandboxes.ts
|
|
149
|
+
const GONE_STATES = [
|
|
150
|
+
"deleting",
|
|
151
|
+
"deleted",
|
|
152
|
+
"failed"
|
|
153
|
+
];
|
|
154
|
+
const RESUMABLE_STATES = ["stopped", "error"];
|
|
155
|
+
const STARTING_STATES = ["creating", "starting"];
|
|
156
|
+
const NAME_HOLDING_STATES = [
|
|
157
|
+
"creating",
|
|
158
|
+
"starting",
|
|
159
|
+
"running",
|
|
160
|
+
"stopping",
|
|
161
|
+
"stopped",
|
|
162
|
+
"error"
|
|
163
|
+
];
|
|
164
|
+
const NOT_RUNNING_CODES = /* @__PURE__ */ new Set([
|
|
165
|
+
"sandbox_not_running",
|
|
166
|
+
"sandbox_stopping",
|
|
167
|
+
"sandbox_starting"
|
|
168
|
+
]);
|
|
169
|
+
const STOPPING_POLL_MS = 250;
|
|
170
|
+
const WAIT_INTERVAL_MS = 500;
|
|
171
|
+
const UNREACHABLE_ATTEMPTS = 4;
|
|
172
|
+
const UNREACHABLE_PAUSE_MS = 500;
|
|
173
|
+
const IGNORED_AUTO_STOP_STATUSES = /* @__PURE__ */ new Set([
|
|
174
|
+
400,
|
|
175
|
+
404,
|
|
176
|
+
409
|
|
177
|
+
]);
|
|
178
|
+
async function retryUnreachable(work, signal) {
|
|
179
|
+
for (let attempt = 1;; attempt += 1) try {
|
|
180
|
+
return await work();
|
|
181
|
+
} catch (error) {
|
|
182
|
+
if (!(error instanceof VmsError) || error.code !== "unavailable" || attempt >= UNREACHABLE_ATTEMPTS) throw error;
|
|
183
|
+
await pause(UNREACHABLE_PAUSE_MS * attempt, signal);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function isNotRunning(error) {
|
|
187
|
+
return error instanceof VmsError && NOT_RUNNING_CODES.has(error.code);
|
|
188
|
+
}
|
|
189
|
+
function isMissing(error) {
|
|
190
|
+
return error instanceof VmsError && error.status === 404;
|
|
191
|
+
}
|
|
192
|
+
function goneError(id, reason) {
|
|
193
|
+
return new SandboxSessionGoneError(`The vms.dev sandbox ${id} ${reason}.`);
|
|
194
|
+
}
|
|
195
|
+
function pause(ms, signal) {
|
|
196
|
+
return new Promise((resolve, reject) => {
|
|
197
|
+
const timer = setTimeout(done, ms);
|
|
198
|
+
function done() {
|
|
199
|
+
signal?.removeEventListener("abort", aborted);
|
|
200
|
+
resolve();
|
|
201
|
+
}
|
|
202
|
+
function aborted() {
|
|
203
|
+
clearTimeout(timer);
|
|
204
|
+
reject(signal?.reason);
|
|
205
|
+
}
|
|
206
|
+
signal?.addEventListener("abort", aborted, { once: true });
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
function waitForRunning(settings, id, timeoutMs = settings.waitTimeoutMs, signal) {
|
|
210
|
+
return settings.client().waitUntilRunning(id, {
|
|
211
|
+
timeoutMs,
|
|
212
|
+
intervalMs: WAIT_INTERVAL_MS,
|
|
213
|
+
signal
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
async function getOrGone(vms, id, signal) {
|
|
217
|
+
try {
|
|
218
|
+
return await vms.getSandbox({ id }, { signal });
|
|
219
|
+
} catch (error) {
|
|
220
|
+
if (error instanceof Error && isMissing(error)) throw goneError(id, "does not exist");
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async function resume(settings, id, signal) {
|
|
225
|
+
try {
|
|
226
|
+
await settings.client().updateSandbox({
|
|
227
|
+
id,
|
|
228
|
+
body: {
|
|
229
|
+
state: "running",
|
|
230
|
+
stopAfterSeconds: settings.stopAfterSeconds
|
|
231
|
+
}
|
|
232
|
+
}, { signal });
|
|
233
|
+
return true;
|
|
234
|
+
} catch (error) {
|
|
235
|
+
if (!(error instanceof VmsError)) throw error;
|
|
236
|
+
if (error.code === "snapshot_expired") throw goneError(id, "has no saved files left");
|
|
237
|
+
if (error.status === 404) throw goneError(id, "does not exist");
|
|
238
|
+
if (error.code === "sandbox_stopping") return false;
|
|
239
|
+
if (error.code === "no_capacity") {
|
|
240
|
+
await pause((error.retryAfterSeconds ?? 1) * 1e3, signal);
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
throw error;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
async function wake(settings, id, signal) {
|
|
247
|
+
const vms = settings.client();
|
|
248
|
+
const deadline = Date.now() + settings.waitTimeoutMs;
|
|
249
|
+
while (true) {
|
|
250
|
+
signal?.throwIfAborted();
|
|
251
|
+
const sandbox = await getOrGone(vms, id, signal);
|
|
252
|
+
if (sandbox.state === "running") return sandbox;
|
|
253
|
+
if (GONE_STATES.includes(sandbox.state)) throw goneError(id, `is ${sandbox.state}`);
|
|
254
|
+
const started = STARTING_STATES.includes(sandbox.state) || RESUMABLE_STATES.includes(sandbox.state) && await resume(settings, id, signal);
|
|
255
|
+
const left = deadline - Date.now();
|
|
256
|
+
if (left <= 0) throw new Error(`The vms.dev sandbox ${id} is still ${sandbox.state} after ${settings.waitTimeoutMs} ms.`);
|
|
257
|
+
if (started) return waitForRunning(settings, id, left, signal);
|
|
258
|
+
await pause(Math.min(STOPPING_POLL_MS, left), signal);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
async function changeAutoStop(settings, id, seconds) {
|
|
262
|
+
const stopAt = new Date(Date.now() + seconds * 1e3).toISOString();
|
|
263
|
+
try {
|
|
264
|
+
await settings.client().updateSandbox({
|
|
265
|
+
id,
|
|
266
|
+
body: { stopAt }
|
|
267
|
+
});
|
|
268
|
+
} catch (error) {
|
|
269
|
+
if (error instanceof VmsError && IGNORED_AUTO_STOP_STATUSES.has(error.status)) return;
|
|
270
|
+
throw error;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
function stopsSoon(settings, sandbox) {
|
|
274
|
+
if (sandbox.stopAt === null) return false;
|
|
275
|
+
return Date.parse(sandbox.stopAt) - Date.now() < settings.stopAfterSeconds * 1e3 / 2;
|
|
276
|
+
}
|
|
277
|
+
function keepRunning(settings, id) {
|
|
278
|
+
return changeAutoStop(settings, id, settings.stopAfterSeconds);
|
|
279
|
+
}
|
|
280
|
+
async function stopWhenIdle(settings, id) {
|
|
281
|
+
if (settings.stopWhenIdleSeconds === null) return;
|
|
282
|
+
if (settings.stopWhenIdleSeconds === 0) {
|
|
283
|
+
await stop(settings, id);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
await changeAutoStop(settings, id, settings.stopWhenIdleSeconds);
|
|
287
|
+
}
|
|
288
|
+
async function stop(settings, id) {
|
|
289
|
+
const vms = settings.client();
|
|
290
|
+
try {
|
|
291
|
+
const sandbox = await vms.getSandbox({ id });
|
|
292
|
+
if (STARTING_STATES.includes(sandbox.state)) await waitForRunning(settings, id);
|
|
293
|
+
else if (sandbox.state !== "running") return;
|
|
294
|
+
await vms.updateSandbox({
|
|
295
|
+
id,
|
|
296
|
+
body: { state: "stopped" }
|
|
297
|
+
});
|
|
298
|
+
} catch (error) {
|
|
299
|
+
if (error instanceof VmsError && (error.status === 404 || error.status === 409)) return;
|
|
300
|
+
throw error;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async function findByName(settings, name, signal) {
|
|
304
|
+
return (await settings.client().listSandboxes({
|
|
305
|
+
name,
|
|
306
|
+
state: NAME_HOLDING_STATES,
|
|
307
|
+
limit: 1
|
|
308
|
+
}, { signal })).data[0] ?? null;
|
|
309
|
+
}
|
|
310
|
+
async function createWhenRoom(settings, body) {
|
|
311
|
+
const vms = settings.client();
|
|
312
|
+
const deadline = Date.now() + settings.waitTimeoutMs;
|
|
313
|
+
while (true) try {
|
|
314
|
+
return await vms.createSandbox({ body });
|
|
315
|
+
} catch (error) {
|
|
316
|
+
if (!(error instanceof VmsError) || error.code !== "no_capacity") throw error;
|
|
317
|
+
const wait = (error.retryAfterSeconds ?? 1) * 1e3;
|
|
318
|
+
if (Date.now() + wait > deadline) throw error;
|
|
319
|
+
await pause(wait, void 0);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
async function createNamed(settings, name, labels, size) {
|
|
323
|
+
const vms = settings.client();
|
|
324
|
+
try {
|
|
325
|
+
return {
|
|
326
|
+
sandbox: await createWhenRoom(settings, {
|
|
327
|
+
size,
|
|
328
|
+
name,
|
|
329
|
+
labels,
|
|
330
|
+
stopAfterSeconds: settings.stopAfterSeconds
|
|
331
|
+
}),
|
|
332
|
+
created: true
|
|
333
|
+
};
|
|
334
|
+
} catch (error) {
|
|
335
|
+
if (!(error instanceof VmsError) || error.code !== "sandbox_name_taken") throw error;
|
|
336
|
+
const holder = error.sandboxId ?? (await findByName(settings, name))?.id;
|
|
337
|
+
if (!holder) throw error;
|
|
338
|
+
return {
|
|
339
|
+
sandbox: await vms.getSandbox({ id: holder }),
|
|
340
|
+
created: false
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
async function deleteQuietly(settings, id, signal) {
|
|
345
|
+
try {
|
|
346
|
+
await settings.client().deleteSandbox({ id }, { signal });
|
|
347
|
+
} catch (error) {
|
|
348
|
+
if (error instanceof Error && isMissing(error)) return;
|
|
349
|
+
throw error;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
// src/connection.ts
|
|
353
|
+
function isMissingPath(error) {
|
|
354
|
+
return error instanceof VmsError && error.status === 404;
|
|
355
|
+
}
|
|
356
|
+
async function runToEnd(settings, sandboxId, argv, signal) {
|
|
357
|
+
const result = await (await retryUnreachable(() => settings.client().exec(sandboxId, argv, { signal }), signal)).wait();
|
|
358
|
+
if (result.exitCode !== 0) throw new Error(result.stderr.trim() || `The command ${argv.join(" ")} exited with ${result.exitCode ?? result.signal}.`);
|
|
359
|
+
}
|
|
360
|
+
function makeWorkspaceRoot(settings, sandboxId) {
|
|
361
|
+
return runToEnd(settings, sandboxId, [
|
|
362
|
+
"mkdir",
|
|
363
|
+
"-p",
|
|
364
|
+
"--",
|
|
365
|
+
settings.workspaceRoot
|
|
366
|
+
]);
|
|
367
|
+
}
|
|
368
|
+
async function writeAll(settings, sandboxId, options) {
|
|
369
|
+
const vms = settings.client();
|
|
370
|
+
const [only, ...others] = options.files;
|
|
371
|
+
if (!only) return;
|
|
372
|
+
if (others.length === 0) {
|
|
373
|
+
await vms.updateFile({
|
|
374
|
+
id: sandboxId,
|
|
375
|
+
path: only.path,
|
|
376
|
+
body: new Uint8Array(only.content)
|
|
377
|
+
}, { signal: options.signal });
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
await vms.writeFiles(sandboxId, options.files.map((file) => ({
|
|
381
|
+
path: file.path,
|
|
382
|
+
content: file.content
|
|
383
|
+
})), { signal: options.signal });
|
|
384
|
+
}
|
|
385
|
+
function connectionOf(settings, sandboxId) {
|
|
386
|
+
const vms = settings.client();
|
|
387
|
+
let stopped = false;
|
|
388
|
+
async function inRunningSandbox(work, signal) {
|
|
389
|
+
if (stopped) {
|
|
390
|
+
await wake(settings, sandboxId, signal);
|
|
391
|
+
stopped = false;
|
|
392
|
+
}
|
|
393
|
+
try {
|
|
394
|
+
return await retryUnreachable(work, signal);
|
|
395
|
+
} catch (error) {
|
|
396
|
+
if (!(error instanceof Error) || !isNotRunning(error)) throw error;
|
|
397
|
+
await wake(settings, sandboxId, signal);
|
|
398
|
+
return retryUnreachable(work, signal);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return {
|
|
402
|
+
extensions: { sandboxId },
|
|
403
|
+
workspaceRoot: settings.workspaceRoot,
|
|
404
|
+
spawn: (options) => inRunningSandbox(() => spawnProcess(vms, sandboxId, {
|
|
405
|
+
command: options.command,
|
|
406
|
+
workingDirectory: options.workingDirectory,
|
|
407
|
+
env: {
|
|
408
|
+
...SANDBOX_AGENT_ENV,
|
|
409
|
+
...settings.env,
|
|
410
|
+
...options.env
|
|
411
|
+
},
|
|
412
|
+
stdin: options.stdin === true,
|
|
413
|
+
signal: options.signal
|
|
414
|
+
}), options.signal),
|
|
415
|
+
readFile: (options) => inRunningSandbox(async () => {
|
|
416
|
+
try {
|
|
417
|
+
return (await vms.getFile({
|
|
418
|
+
id: sandboxId,
|
|
419
|
+
path: options.path
|
|
420
|
+
}, { signal: options.signal })).body ?? /* @__PURE__ */ new Uint8Array();
|
|
421
|
+
} catch (error) {
|
|
422
|
+
if (error instanceof Error && isMissingPath(error)) return null;
|
|
423
|
+
throw error;
|
|
424
|
+
}
|
|
425
|
+
}, options.signal),
|
|
426
|
+
writeFiles: (options) => inRunningSandbox(() => writeAll(settings, sandboxId, options), options.signal),
|
|
427
|
+
removePath: (options) => inRunningSandbox(async () => {
|
|
428
|
+
if (!options.recursive) {
|
|
429
|
+
await runToEnd(settings, sandboxId, [
|
|
430
|
+
"rm",
|
|
431
|
+
options.force ? "-df" : "-d",
|
|
432
|
+
"--",
|
|
433
|
+
options.path
|
|
434
|
+
], options.signal);
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
try {
|
|
438
|
+
await vms.deleteFile({
|
|
439
|
+
id: sandboxId,
|
|
440
|
+
path: options.path
|
|
441
|
+
}, { signal: options.signal });
|
|
442
|
+
} catch (error) {
|
|
443
|
+
if (error instanceof Error && isMissingPath(error) && options.force) return;
|
|
444
|
+
throw error;
|
|
445
|
+
}
|
|
446
|
+
}, options.signal),
|
|
447
|
+
async stop() {
|
|
448
|
+
stopped = true;
|
|
449
|
+
await stop(settings, sandboxId);
|
|
450
|
+
},
|
|
451
|
+
async close() {
|
|
452
|
+
if (stopped) return;
|
|
453
|
+
await stopWhenIdle(settings, sandboxId);
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
// src/setup.ts
|
|
458
|
+
const PREPARE_LABEL = "eve-prepare";
|
|
459
|
+
const BASE64_CHUNK = 32768;
|
|
460
|
+
function base64Of(bytes) {
|
|
461
|
+
let binary = "";
|
|
462
|
+
for (let start = 0; start < bytes.length; start += BASE64_CHUNK) binary += String.fromCharCode(...bytes.subarray(start, start + BASE64_CHUNK));
|
|
463
|
+
return btoa(binary);
|
|
464
|
+
}
|
|
465
|
+
function bytesOf(base64) {
|
|
466
|
+
return Uint8Array.from(atob(base64), (character) => character.charCodeAt(0));
|
|
467
|
+
}
|
|
468
|
+
function recordingConnection(connection, steps) {
|
|
469
|
+
return {
|
|
470
|
+
workspaceRoot: connection.workspaceRoot,
|
|
471
|
+
readFile: (options) => connection.readFile(options),
|
|
472
|
+
async writeFiles(options) {
|
|
473
|
+
await connection.writeFiles(options);
|
|
474
|
+
steps.push({
|
|
475
|
+
kind: "files",
|
|
476
|
+
files: options.files.map((file) => ({
|
|
477
|
+
path: file.path,
|
|
478
|
+
content: base64Of(file.content)
|
|
479
|
+
}))
|
|
480
|
+
});
|
|
481
|
+
},
|
|
482
|
+
async removePath(options) {
|
|
483
|
+
await connection.removePath(options);
|
|
484
|
+
steps.push({
|
|
485
|
+
kind: "remove",
|
|
486
|
+
path: options.path,
|
|
487
|
+
force: options.force === true,
|
|
488
|
+
recursive: options.recursive === true
|
|
489
|
+
});
|
|
490
|
+
},
|
|
491
|
+
async spawn(options) {
|
|
492
|
+
if (options.stdin) throw new Error("A setup command cannot read stdin, because each new sandbox runs it again.");
|
|
493
|
+
const step = {
|
|
494
|
+
kind: "command",
|
|
495
|
+
command: options.command,
|
|
496
|
+
workingDirectory: options.workingDirectory,
|
|
497
|
+
env: { ...options.env },
|
|
498
|
+
exitCode: 0
|
|
499
|
+
};
|
|
500
|
+
const process = await connection.spawn(options);
|
|
501
|
+
steps.push(step);
|
|
502
|
+
const exited = Promise.resolve(process.wait()).then((exit) => {
|
|
503
|
+
step.exitCode = exit.exitCode;
|
|
504
|
+
return exit;
|
|
505
|
+
});
|
|
506
|
+
exited.catch(() => void 0);
|
|
507
|
+
return {
|
|
508
|
+
stdout: process.stdout,
|
|
509
|
+
stderr: process.stderr,
|
|
510
|
+
wait: () => exited,
|
|
511
|
+
kill: () => process.kill()
|
|
512
|
+
};
|
|
513
|
+
},
|
|
514
|
+
stop: async () => void 0
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
async function prepareSetup(settings, context) {
|
|
518
|
+
if (context.isEmpty) return { steps: [] };
|
|
519
|
+
const sandbox = await createWhenRoom(settings, {
|
|
520
|
+
size: settings.size,
|
|
521
|
+
labels: {
|
|
522
|
+
...settings.labels,
|
|
523
|
+
[PREPARE_LABEL]: "true"
|
|
524
|
+
},
|
|
525
|
+
stopAfterSeconds: settings.stopAfterSeconds
|
|
526
|
+
});
|
|
527
|
+
try {
|
|
528
|
+
context.log(`recording the setup in the vms.dev sandbox ${sandbox.id}`);
|
|
529
|
+
await waitForRunning(settings, sandbox.id);
|
|
530
|
+
await makeWorkspaceRoot(settings, sandbox.id);
|
|
531
|
+
const steps = [];
|
|
532
|
+
await context.populate(recordingConnection(connectionOf(settings, sandbox.id), steps));
|
|
533
|
+
return { steps };
|
|
534
|
+
} finally {
|
|
535
|
+
await deleteQuietly(settings, sandbox.id);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
async function runStep(connection, step) {
|
|
539
|
+
const process = await connection.spawn({
|
|
540
|
+
command: step.command,
|
|
541
|
+
workingDirectory: step.workingDirectory,
|
|
542
|
+
env: step.env
|
|
543
|
+
});
|
|
544
|
+
const [, stderr, exit] = await Promise.all([
|
|
545
|
+
new Response(process.stdout).arrayBuffer(),
|
|
546
|
+
new Response(process.stderr).text(),
|
|
547
|
+
process.wait()
|
|
548
|
+
]);
|
|
549
|
+
if (exit.exitCode !== step.exitCode) throw new Error(`The setup command "${step.command}" exited with ${exit.exitCode}, but it exited with ${step.exitCode} in prepare. ${stderr.trim()}`.trim());
|
|
550
|
+
}
|
|
551
|
+
async function runSetup(connection, artifact) {
|
|
552
|
+
for (const step of artifact.steps) {
|
|
553
|
+
if (step.kind === "files") await connection.writeFiles({ files: step.files.map((file) => ({
|
|
554
|
+
path: file.path,
|
|
555
|
+
content: bytesOf(file.content)
|
|
556
|
+
})) });
|
|
557
|
+
if (step.kind === "remove") await connection.removePath({
|
|
558
|
+
path: step.path,
|
|
559
|
+
force: step.force,
|
|
560
|
+
recursive: step.recursive
|
|
561
|
+
});
|
|
562
|
+
if (step.kind === "command") await runStep(connection, step);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
// src/provider.ts
|
|
566
|
+
const SANDBOX_NAME_PREFIX = "eve-";
|
|
567
|
+
const NAME_HASH_LENGTH = 32;
|
|
568
|
+
const stateSchema = z.object({
|
|
569
|
+
id: z.string().regex(/^sbx_[0-9a-z]+$/),
|
|
570
|
+
name: z.string()
|
|
571
|
+
});
|
|
572
|
+
function sandboxNameOf(sessionId, namespace = "") {
|
|
573
|
+
const hash = createHash("sha256").update(namespace).update("\0").update(sessionId).digest("hex").slice(0, NAME_HASH_LENGTH);
|
|
574
|
+
return `${SANDBOX_NAME_PREFIX}${hash}`;
|
|
575
|
+
}
|
|
576
|
+
async function openSandbox(settings, id) {
|
|
577
|
+
if (stopsSoon(settings, await wake(settings, id))) await keepRunning(settings, id);
|
|
578
|
+
return id;
|
|
579
|
+
}
|
|
580
|
+
async function createSandbox(settings, name, open, artifact) {
|
|
581
|
+
const { sandbox, created } = await createNamed(settings, name, {
|
|
582
|
+
...settings.labels,
|
|
583
|
+
...open?.labels
|
|
584
|
+
}, open?.size ?? settings.size);
|
|
585
|
+
if (!created) return openSandbox(settings, sandbox.id);
|
|
586
|
+
try {
|
|
587
|
+
await waitForRunning(settings, sandbox.id);
|
|
588
|
+
await makeWorkspaceRoot(settings, sandbox.id);
|
|
589
|
+
await runSetup(connectionOf(settings, sandbox.id), artifact);
|
|
590
|
+
return sandbox.id;
|
|
591
|
+
} catch (error) {
|
|
592
|
+
await deleteQuietly(settings, sandbox.id);
|
|
593
|
+
throw error;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
const vmsSandboxProvider = {
|
|
597
|
+
name: "vms",
|
|
598
|
+
stateProtocolVersion: 1,
|
|
599
|
+
environment(options) {
|
|
600
|
+
const settings = settingsOf(options);
|
|
601
|
+
return {
|
|
602
|
+
prepare: (context) => prepareSetup(settings, context),
|
|
603
|
+
async start(context, open, artifact) {
|
|
604
|
+
const name = sandboxNameOf(context.session.id, settings.namespace);
|
|
605
|
+
const found = await findByName(settings, name);
|
|
606
|
+
const id = found ? await openSandbox(settings, found.id) : await createSandbox(settings, name, open, artifact);
|
|
607
|
+
return {
|
|
608
|
+
connection: connectionOf(settings, id),
|
|
609
|
+
state: {
|
|
610
|
+
id,
|
|
611
|
+
name
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
},
|
|
615
|
+
async resume(_context, _artifact, state) {
|
|
616
|
+
const parsed = stateSchema.safeParse(state);
|
|
617
|
+
if (!parsed.success) throw new SandboxSessionGoneError("The saved state does not name a vms.dev sandbox.");
|
|
618
|
+
return connectionOf(settings, await openSandbox(settings, parsed.data.id));
|
|
619
|
+
},
|
|
620
|
+
async onSessionEnd(context, state, end) {
|
|
621
|
+
const ids = /* @__PURE__ */ new Set();
|
|
622
|
+
const parsed = stateSchema.safeParse(state);
|
|
623
|
+
if (parsed.success) ids.add(parsed.data.id);
|
|
624
|
+
const found = await findByName(settings, sandboxNameOf(context.session.id, settings.namespace), end.signal);
|
|
625
|
+
if (found) ids.add(found.id);
|
|
626
|
+
for (const id of ids) await deleteQuietly(settings, id, end.signal);
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
};
|
|
631
|
+
const VmsSandbox = defineSandboxProvider(vmsSandboxProvider);
|
|
632
|
+
export { DEFAULT_SIZE, DEFAULT_STOP_AFTER_SECONDS, DEFAULT_STOP_WHEN_IDLE_SECONDS, DEFAULT_WAIT_TIMEOUT_MS, DEFAULT_WORKSPACE_ROOT, PREPARE_LABEL, SANDBOX_NAME_PREFIX, VmsSandbox, sandboxNameOf, vmsSandboxProvider };
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vms.dev/eve",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The vms.dev sandbox provider for eve: each eve session runs in its own vms.dev sandbox.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://vms.dev/docs/sandboxes/eve",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/vms-dev-org/cli.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"vms.dev",
|
|
13
|
+
"eve",
|
|
14
|
+
"sandbox",
|
|
15
|
+
"agent"
|
|
16
|
+
],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=24"
|
|
21
|
+
},
|
|
22
|
+
"main": "./index.js",
|
|
23
|
+
"types": "./index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./index.d.ts",
|
|
27
|
+
"default": "./index.js"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@vms.dev/sdk": "^0.1.0",
|
|
33
|
+
"zod": "^4.6.7"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"eve": "^1.0.0"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
}
|
|
41
|
+
}
|