operon-sandbox 0.1.0-alpha.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 +55 -0
- package/README.md +51 -0
- package/dist/cloudflare/cf-api.d.ts +115 -0
- package/dist/cloudflare/cf-api.js +13 -0
- package/dist/cloudflare/lifecycle.d.ts +64 -0
- package/dist/cloudflare/lifecycle.js +88 -0
- package/dist/cloudflare/machine.d.ts +87 -0
- package/dist/cloudflare/machine.js +342 -0
- package/dist/e2b/e2b-api.d.ts +84 -0
- package/dist/e2b/e2b-api.js +10 -0
- package/dist/e2b/lifecycle.d.ts +83 -0
- package/dist/e2b/lifecycle.js +120 -0
- package/dist/e2b/machine.d.ts +69 -0
- package/dist/e2b/machine.js +281 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +5 -0
- package/dist/shared/remote-file-ops.d.ts +38 -0
- package/dist/shared/remote-file-ops.js +66 -0
- package/dist/shared/sandbox-machine.d.ts +38 -0
- package/dist/shared/sandbox-machine.js +97 -0
- package/dist/types.d.ts +39 -0
- package/dist/types.js +2 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yingjie Qiao
|
|
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.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
-------------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
THIRD-PARTY LICENSES
|
|
27
|
+
|
|
28
|
+
Portions of this software were adapted from kimi-code, which is distributed
|
|
29
|
+
under the MIT License. Its copyright and permission notice is reproduced below.
|
|
30
|
+
|
|
31
|
+
-------------------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
kimi-code
|
|
34
|
+
|
|
35
|
+
MIT License
|
|
36
|
+
|
|
37
|
+
Copyright (c) 2026 Moonshot AI
|
|
38
|
+
|
|
39
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
40
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
41
|
+
in the Software without restriction, including without limitation the rights
|
|
42
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
43
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
44
|
+
furnished to do so, subject to the following conditions:
|
|
45
|
+
|
|
46
|
+
The above copyright notice and this permission notice shall be included in all
|
|
47
|
+
copies or substantial portions of the Software.
|
|
48
|
+
|
|
49
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
50
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
51
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
52
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
53
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
54
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
55
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# operon-sandbox
|
|
2
|
+
|
|
3
|
+
Host-side sandbox lifecycle for [operon-agents](https://github.com/nickqiaoo/operon-agents): create,
|
|
4
|
+
snapshot, pause and destroy a vendor sandbox, and hand the framework a `Machine` driven by the
|
|
5
|
+
vendor SDK directly — native per-command timeouts, incremental output, and a real kill.
|
|
6
|
+
|
|
7
|
+
Supported vendors: **E2B** and **Cloudflare**.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { Sandbox } from "e2b";
|
|
11
|
+
import { createHarness } from "operon-agents";
|
|
12
|
+
import { E2BWorkspace } from "operon-sandbox";
|
|
13
|
+
|
|
14
|
+
// The host owns the sandbox lifecycle; the framework only ever sees a Machine.
|
|
15
|
+
const workspace = await E2BWorkspace.open({ sandbox: Sandbox, template: "node20" });
|
|
16
|
+
|
|
17
|
+
const harness = createHarness({
|
|
18
|
+
model,
|
|
19
|
+
machine: workspace.machine, // tools now run inside the sandbox
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// … run sessions …
|
|
23
|
+
|
|
24
|
+
await harness.close();
|
|
25
|
+
await workspace.snapshot(); // snapshot / pause / kill stays with you
|
|
26
|
+
await workspace.kill();
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Reconnect to an existing sandbox later by passing the saved state back in:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
const state = workspace.state(); // { sandboxId }
|
|
33
|
+
const again = await E2BWorkspace.open({ sandbox: Sandbox }, state);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Vendor SDKs are optional peer dependencies — install `e2b` or `@cloudflare/sandbox` for the ones you
|
|
37
|
+
actually use.
|
|
38
|
+
|
|
39
|
+
Nothing else about the agent changes: sessions, events, permissions and tools are identical to a
|
|
40
|
+
local run. That is the point of the `Machine` seam — where a command executes is an adapter choice,
|
|
41
|
+
not a mode the engine knows about.
|
|
42
|
+
|
|
43
|
+
The lifecycle deliberately stays with the host rather than the framework, because only the host
|
|
44
|
+
knows whether a sandbox should be snapshotted for later, paused to stop billing, or destroyed.
|
|
45
|
+
|
|
46
|
+
See [`examples/managed-agents`](https://github.com/nickqiaoo/operon-agents/tree/main/examples/managed-agents)
|
|
47
|
+
for a full server that swaps a sandbox in behind an HTTP control plane.
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
MIT
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The slice of `@cloudflare/sandbox`'s `SandboxClient` this adapter calls, described
|
|
3
|
+
* structurally so the package stays an optional peer and tests can inject a fake.
|
|
4
|
+
*
|
|
5
|
+
* This is the ROUTE transport (`new SandboxClient({ baseUrl })`), which is what a Node
|
|
6
|
+
* process gets. It is deliberately not `getSandbox()` — that one needs a
|
|
7
|
+
* `DurableObjectNamespace` and only exists inside a Worker. Consequences of that choice,
|
|
8
|
+
* all visible below: every call carries a `sessionId`, incremental output arrives as an SSE
|
|
9
|
+
* stream rather than an `onOutput` callback, and tunnels (hence public port URLs) are
|
|
10
|
+
* unavailable because the SDK implements them on the RPC transport only.
|
|
11
|
+
*/
|
|
12
|
+
export interface CloudflareSandboxClient {
|
|
13
|
+
readonly commands: CloudflareCommandsApi;
|
|
14
|
+
readonly processes: CloudflareProcessesApi;
|
|
15
|
+
readonly files: CloudflareFilesApi;
|
|
16
|
+
readonly git?: CloudflareGitApi;
|
|
17
|
+
readonly backup?: CloudflareBackupApi;
|
|
18
|
+
}
|
|
19
|
+
export interface CloudflareExecOptions {
|
|
20
|
+
timeoutMs?: number;
|
|
21
|
+
env?: Record<string, string | undefined>;
|
|
22
|
+
cwd?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface CloudflareExecuteResponse {
|
|
25
|
+
stdout: string;
|
|
26
|
+
stderr: string;
|
|
27
|
+
exitCode: number;
|
|
28
|
+
}
|
|
29
|
+
export interface CloudflareCommandsApi {
|
|
30
|
+
/** One round trip, native `timeoutMs`, no handle to cancel with. */
|
|
31
|
+
execute(command: string, sessionId: string, options?: CloudflareExecOptions): Promise<CloudflareExecuteResponse>;
|
|
32
|
+
}
|
|
33
|
+
export interface CloudflareProcessStartResult {
|
|
34
|
+
processId: string;
|
|
35
|
+
pid?: number;
|
|
36
|
+
}
|
|
37
|
+
export interface CloudflareProcessInfoResult {
|
|
38
|
+
process: {
|
|
39
|
+
id: string;
|
|
40
|
+
status: string;
|
|
41
|
+
exitCode?: number;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export interface CloudflareProcessesApi {
|
|
45
|
+
/**
|
|
46
|
+
* Starts a command and returns immediately with a `processId` — the handle that makes
|
|
47
|
+
* cancellation real, which `commands.execute` cannot give us.
|
|
48
|
+
*/
|
|
49
|
+
startProcess(command: string, sessionId: string, options?: CloudflareExecOptions & {
|
|
50
|
+
processId?: string;
|
|
51
|
+
autoCleanup?: boolean;
|
|
52
|
+
}): Promise<CloudflareProcessStartResult>;
|
|
53
|
+
getProcess(processId: string): Promise<CloudflareProcessInfoResult>;
|
|
54
|
+
killProcess(processId: string): Promise<unknown>;
|
|
55
|
+
killAllProcesses?(): Promise<unknown>;
|
|
56
|
+
/** SSE stream of `LogEvent` records: `stdout` / `stderr` / `exit` / `error`. */
|
|
57
|
+
streamProcessLogs(processId: string): Promise<ReadableStream<Uint8Array>>;
|
|
58
|
+
}
|
|
59
|
+
/** One decoded `LogEvent` from `streamProcessLogs`. */
|
|
60
|
+
export interface CloudflareLogEvent {
|
|
61
|
+
type: "stdout" | "stderr" | "exit" | "error";
|
|
62
|
+
data?: string;
|
|
63
|
+
exitCode?: number;
|
|
64
|
+
}
|
|
65
|
+
export interface CloudflareFileEntry {
|
|
66
|
+
name: string;
|
|
67
|
+
/** Present on directory listings; the SDK's own `FileInfo` shape varies by version. */
|
|
68
|
+
type?: string;
|
|
69
|
+
isDirectory?: boolean;
|
|
70
|
+
size?: number;
|
|
71
|
+
modifiedAt?: string;
|
|
72
|
+
}
|
|
73
|
+
export interface CloudflareFilesApi {
|
|
74
|
+
mkdir(path: string, sessionId: string, options?: {
|
|
75
|
+
recursive?: boolean;
|
|
76
|
+
}): Promise<unknown>;
|
|
77
|
+
/** `encoding: "base64"` is the only way to move bytes over this transport. */
|
|
78
|
+
writeFile(path: string, content: string, sessionId: string, options?: {
|
|
79
|
+
encoding?: string;
|
|
80
|
+
}): Promise<unknown>;
|
|
81
|
+
readFile(path: string, sessionId: string, options?: {
|
|
82
|
+
encoding?: string;
|
|
83
|
+
}): Promise<{
|
|
84
|
+
content: string;
|
|
85
|
+
}>;
|
|
86
|
+
listFiles(path: string, sessionId: string, options?: {
|
|
87
|
+
recursive?: boolean;
|
|
88
|
+
includeHidden?: boolean;
|
|
89
|
+
}): Promise<{
|
|
90
|
+
files: readonly CloudflareFileEntry[];
|
|
91
|
+
}>;
|
|
92
|
+
exists?(path: string, sessionId: string): Promise<{
|
|
93
|
+
exists: boolean;
|
|
94
|
+
}>;
|
|
95
|
+
}
|
|
96
|
+
export interface CloudflareGitApi {
|
|
97
|
+
/** Native shallow clone — cheaper and more precise than shelling out to `git`. */
|
|
98
|
+
checkout(repoUrl: string, sessionId: string, options?: {
|
|
99
|
+
branch?: string;
|
|
100
|
+
targetDir?: string;
|
|
101
|
+
depth?: number;
|
|
102
|
+
timeoutMs?: number;
|
|
103
|
+
}): Promise<unknown>;
|
|
104
|
+
}
|
|
105
|
+
export interface CloudflareBackupApi {
|
|
106
|
+
/** squashfs archive of a directory — Cloudflare's stand-in for a vendor snapshot. */
|
|
107
|
+
createArchive(dir: string, archivePath: string, sessionId: string, options?: {
|
|
108
|
+
excludes?: string[];
|
|
109
|
+
gitignore?: boolean;
|
|
110
|
+
}): Promise<unknown>;
|
|
111
|
+
restoreArchive(dir: string, archivePath: string, sessionId: string): Promise<unknown>;
|
|
112
|
+
}
|
|
113
|
+
/** Read a `SandboxClient` through a function so a workspace can swap the instance underneath. */
|
|
114
|
+
export type CloudflareClientRef = () => CloudflareSandboxClient;
|
|
115
|
+
//# sourceMappingURL=cf-api.d.ts.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The slice of `@cloudflare/sandbox`'s `SandboxClient` this adapter calls, described
|
|
3
|
+
* structurally so the package stays an optional peer and tests can inject a fake.
|
|
4
|
+
*
|
|
5
|
+
* This is the ROUTE transport (`new SandboxClient({ baseUrl })`), which is what a Node
|
|
6
|
+
* process gets. It is deliberately not `getSandbox()` — that one needs a
|
|
7
|
+
* `DurableObjectNamespace` and only exists inside a Worker. Consequences of that choice,
|
|
8
|
+
* all visible below: every call carries a `sessionId`, incremental output arrives as an SSE
|
|
9
|
+
* stream rather than an `onOutput` callback, and tunnels (hence public port URLs) are
|
|
10
|
+
* unavailable because the SDK implements them on the RPC transport only.
|
|
11
|
+
*/
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=cf-api.js.map
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { SandboxWorkspace } from "../types.ts";
|
|
2
|
+
import type { CloudflareSandboxClient } from "./cf-api.ts";
|
|
3
|
+
import { CloudflareMachine, type CloudflareMachineOptions } from "./machine.ts";
|
|
4
|
+
export interface CloudflareWorkspaceOptions extends CloudflareMachineOptions {
|
|
5
|
+
/**
|
|
6
|
+
* A `SandboxClient` from `@cloudflare/sandbox`, constructed against YOUR Worker:
|
|
7
|
+
* `new SandboxClient({ baseUrl })`. The Worker must expose the SDK's routes (wrap your
|
|
8
|
+
* handlers with `bridge()` from `@cloudflare/sandbox/bridge`).
|
|
9
|
+
*/
|
|
10
|
+
readonly client: CloudflareSandboxClient;
|
|
11
|
+
/** Workspace root that snapshots capture and restore. Defaults to the machine's cwd. */
|
|
12
|
+
readonly root?: string;
|
|
13
|
+
/** Prepare a fresh workspace (clone a repo, drop config files). Runs only on creation. */
|
|
14
|
+
readonly prepare?: (machine: CloudflareMachine) => Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Host-side lifecycle for a Cloudflare sandbox.
|
|
18
|
+
*
|
|
19
|
+
* Two things differ from a VM-style vendor and are worth knowing before you pick this one:
|
|
20
|
+
*
|
|
21
|
+
* 1. **Snapshots are archives, not sandbox images.** Cloudflare has no "clone the whole
|
|
22
|
+
* sandbox" primitive, so `snapshot()` writes a squashfs archive of the workspace root and
|
|
23
|
+
* `restore()` unpacks it back over that root. The sandbox instance never changes — which
|
|
24
|
+
* makes `restore` cheaper than E2B's (no replacement instance, no dead handles), but the
|
|
25
|
+
* snapshot only covers the workspace tree, not installed packages outside it.
|
|
26
|
+
* 2. **There is no pause.** `pause()` returns `false`; the sandbox keeps running until the
|
|
27
|
+
* container's own idle policy retires it or you `kill()` it.
|
|
28
|
+
*/
|
|
29
|
+
export declare class CloudflareWorkspace implements SandboxWorkspace {
|
|
30
|
+
readonly machine: CloudflareMachine;
|
|
31
|
+
private readonly options;
|
|
32
|
+
private readonly root;
|
|
33
|
+
private snapshotSeq;
|
|
34
|
+
private constructor();
|
|
35
|
+
static open(options: CloudflareWorkspaceOptions): Promise<CloudflareWorkspace>;
|
|
36
|
+
/** The session id every vendor call is scoped to — Cloudflare's unit of sandbox identity. */
|
|
37
|
+
get id(): string;
|
|
38
|
+
/**
|
|
39
|
+
* Clone a repo through the vendor's native git support when available, which is both faster
|
|
40
|
+
* and more precise than spawning `git` (it takes `depth` and `branch` directly).
|
|
41
|
+
* Returns false when this deployment exposes no git client, so the caller can fall back to
|
|
42
|
+
* `materializeWorkspace`'s `git_repo` entry rather than assume it worked.
|
|
43
|
+
*/
|
|
44
|
+
checkout(repoUrl: string, options?: {
|
|
45
|
+
branch?: string;
|
|
46
|
+
targetDir?: string;
|
|
47
|
+
depth?: number;
|
|
48
|
+
}): Promise<boolean>;
|
|
49
|
+
/**
|
|
50
|
+
* Archive the workspace root. `undefined` when the deployment exposes no backup client —
|
|
51
|
+
* the caller then knows to fall back instead of believing a snapshot exists.
|
|
52
|
+
*/
|
|
53
|
+
snapshot(): Promise<string | undefined>;
|
|
54
|
+
/** Unpack an archive back over the workspace root. The machine stays valid throughout. */
|
|
55
|
+
restore(snapshotId: string): Promise<void>;
|
|
56
|
+
/** Cloudflare containers have no suspend primitive — the sandbox is still running. */
|
|
57
|
+
pause(): Promise<boolean>;
|
|
58
|
+
/**
|
|
59
|
+
* Kill everything running in the sandbox. The container itself is owned by the Durable
|
|
60
|
+
* Object and retires on its own idle policy, so this stops the work, not the deployment.
|
|
61
|
+
*/
|
|
62
|
+
kill(): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=lifecycle.d.ts.map
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { CloudflareMachine } from "./machine.js";
|
|
2
|
+
/** Where squashfs archives live inside the sandbox when no explicit path is given. */
|
|
3
|
+
const SNAPSHOT_DIR = "/tmp/operon-snapshots";
|
|
4
|
+
/**
|
|
5
|
+
* Host-side lifecycle for a Cloudflare sandbox.
|
|
6
|
+
*
|
|
7
|
+
* Two things differ from a VM-style vendor and are worth knowing before you pick this one:
|
|
8
|
+
*
|
|
9
|
+
* 1. **Snapshots are archives, not sandbox images.** Cloudflare has no "clone the whole
|
|
10
|
+
* sandbox" primitive, so `snapshot()` writes a squashfs archive of the workspace root and
|
|
11
|
+
* `restore()` unpacks it back over that root. The sandbox instance never changes — which
|
|
12
|
+
* makes `restore` cheaper than E2B's (no replacement instance, no dead handles), but the
|
|
13
|
+
* snapshot only covers the workspace tree, not installed packages outside it.
|
|
14
|
+
* 2. **There is no pause.** `pause()` returns `false`; the sandbox keeps running until the
|
|
15
|
+
* container's own idle policy retires it or you `kill()` it.
|
|
16
|
+
*/
|
|
17
|
+
export class CloudflareWorkspace {
|
|
18
|
+
machine;
|
|
19
|
+
options;
|
|
20
|
+
root;
|
|
21
|
+
snapshotSeq = 0;
|
|
22
|
+
constructor(options) {
|
|
23
|
+
this.options = options;
|
|
24
|
+
this.machine = new CloudflareMachine(options.client, options);
|
|
25
|
+
this.root = options.root ?? this.machine.getcwd();
|
|
26
|
+
}
|
|
27
|
+
static async open(options) {
|
|
28
|
+
const workspace = new CloudflareWorkspace(options);
|
|
29
|
+
await workspace.machine.mkdir(workspace.root, { parents: true });
|
|
30
|
+
if (options.prepare !== undefined)
|
|
31
|
+
await options.prepare(workspace.machine);
|
|
32
|
+
return workspace;
|
|
33
|
+
}
|
|
34
|
+
/** The session id every vendor call is scoped to — Cloudflare's unit of sandbox identity. */
|
|
35
|
+
get id() {
|
|
36
|
+
return this.options.sessionId ?? "default";
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Clone a repo through the vendor's native git support when available, which is both faster
|
|
40
|
+
* and more precise than spawning `git` (it takes `depth` and `branch` directly).
|
|
41
|
+
* Returns false when this deployment exposes no git client, so the caller can fall back to
|
|
42
|
+
* `materializeWorkspace`'s `git_repo` entry rather than assume it worked.
|
|
43
|
+
*/
|
|
44
|
+
async checkout(repoUrl, options = {}) {
|
|
45
|
+
if (this.options.client.git === undefined)
|
|
46
|
+
return false;
|
|
47
|
+
await this.options.client.git.checkout(repoUrl, this.id, {
|
|
48
|
+
depth: 1,
|
|
49
|
+
...options,
|
|
50
|
+
});
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Archive the workspace root. `undefined` when the deployment exposes no backup client —
|
|
55
|
+
* the caller then knows to fall back instead of believing a snapshot exists.
|
|
56
|
+
*/
|
|
57
|
+
async snapshot() {
|
|
58
|
+
const backup = this.options.client.backup;
|
|
59
|
+
if (backup === undefined)
|
|
60
|
+
return undefined;
|
|
61
|
+
// Sequence rather than a timestamp: deterministic, and two snapshots in the same
|
|
62
|
+
// millisecond cannot collide onto one archive.
|
|
63
|
+
this.snapshotSeq += 1;
|
|
64
|
+
const archivePath = `${SNAPSHOT_DIR}/snap-${String(this.snapshotSeq)}.sqfs`;
|
|
65
|
+
await this.machine.mkdir(SNAPSHOT_DIR, { parents: true });
|
|
66
|
+
await backup.createArchive(this.root, archivePath, this.id, { gitignore: false });
|
|
67
|
+
return archivePath;
|
|
68
|
+
}
|
|
69
|
+
/** Unpack an archive back over the workspace root. The machine stays valid throughout. */
|
|
70
|
+
async restore(snapshotId) {
|
|
71
|
+
const backup = this.options.client.backup;
|
|
72
|
+
if (backup === undefined)
|
|
73
|
+
throw new Error("This Cloudflare deployment exposes no backup client to restore from.");
|
|
74
|
+
await backup.restoreArchive(this.root, snapshotId, this.id);
|
|
75
|
+
}
|
|
76
|
+
/** Cloudflare containers have no suspend primitive — the sandbox is still running. */
|
|
77
|
+
async pause() {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Kill everything running in the sandbox. The container itself is owned by the Durable
|
|
82
|
+
* Object and retires on its own idle policy, so this stops the work, not the deployment.
|
|
83
|
+
*/
|
|
84
|
+
async kill() {
|
|
85
|
+
await this.options.client.processes.killAllProcesses?.().catch(() => undefined);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=lifecycle.js.map
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { DirEntry, Environment, Machine, ByteRange, RunCommandOptions, RunCommandResult } from "operon-agents-core";
|
|
2
|
+
import type { CloudflareClientRef, CloudflareSandboxClient } from "./cf-api.ts";
|
|
3
|
+
import { SandboxMachine } from "../shared/sandbox-machine.ts";
|
|
4
|
+
export interface CloudflareMachineOptions {
|
|
5
|
+
readonly cwd?: string;
|
|
6
|
+
/** Cloudflare scopes every call to a session id; one machine, one session. */
|
|
7
|
+
readonly sessionId?: string;
|
|
8
|
+
readonly shellPath?: string;
|
|
9
|
+
/** Applied when `RunCommandOptions.timeoutMs` is absent. */
|
|
10
|
+
readonly defaultTimeoutMs?: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A `Machine` backed directly by `@cloudflare/sandbox`'s route-transport client.
|
|
14
|
+
*
|
|
15
|
+
* Direct rather than through a general sandbox abstraction for the same reason as the E2B
|
|
16
|
+
* adapter: the vendor natively provides a per-command timeout, incremental output and a real
|
|
17
|
+
* kill, and a lowest-common-denominator layer in between can express none of the three.
|
|
18
|
+
*
|
|
19
|
+
* Backend limits, surfaced rather than papered over:
|
|
20
|
+
* - No public port URLs. The SDK implements tunnels on the RPC transport only, so
|
|
21
|
+
* `exposedPortUrl` truthfully returns `undefined` here instead of inventing a host.
|
|
22
|
+
* - `listFiles` reports directory-or-not, with no symlink kind, so `DirEntry.kind` is not
|
|
23
|
+
* strict lstat semantics (a symlink reads as its target's kind — see `listDir`).
|
|
24
|
+
* - No stat API, so `fileInfo` costs one `stat(1)` command.
|
|
25
|
+
* - Bytes move as base64: the transport takes a string, and `writeFileStream` is RPC-only.
|
|
26
|
+
*/
|
|
27
|
+
export declare class CloudflareMachine extends SandboxMachine {
|
|
28
|
+
readonly name = "cloudflare";
|
|
29
|
+
readonly osEnv: Environment;
|
|
30
|
+
private readonly clientRef;
|
|
31
|
+
private readonly cwd;
|
|
32
|
+
private readonly sessionId;
|
|
33
|
+
private readonly defaultTimeoutMs;
|
|
34
|
+
constructor(client: CloudflareClientRef | CloudflareSandboxClient, options?: CloudflareMachineOptions);
|
|
35
|
+
private get client();
|
|
36
|
+
pathClass(): "posix";
|
|
37
|
+
normpath(path: string): string;
|
|
38
|
+
gethome(): string;
|
|
39
|
+
getcwd(): string;
|
|
40
|
+
withCwd(cwd: string): Machine;
|
|
41
|
+
/**
|
|
42
|
+
* Two paths, because the vendor splits the capability in two:
|
|
43
|
+
*
|
|
44
|
+
* - `commands.execute` is ONE round trip with a native timeout, but hands back no handle,
|
|
45
|
+
* so nothing can cancel it mid-flight.
|
|
46
|
+
* - `processes.startProcess` returns a `processId` immediately — the handle that makes
|
|
47
|
+
* `killProcess` (and therefore abort, and a cap that actually stops the command) real —
|
|
48
|
+
* with output arriving as an SSE log stream.
|
|
49
|
+
*
|
|
50
|
+
* So a plain "run this and give me the output" takes the cheap path, and anything that
|
|
51
|
+
* asked for cancellation, incremental output or an output cap takes the process path. The
|
|
52
|
+
* caller states intent once; which mechanism delivers it stays in here.
|
|
53
|
+
*/
|
|
54
|
+
run(argv: readonly string[], options?: RunCommandOptions): Promise<RunCommandResult>;
|
|
55
|
+
/** Cheap path: one request, native timeout, no cancellation. */
|
|
56
|
+
private runViaExecute;
|
|
57
|
+
/** Cancellable path: a processId to kill, plus an SSE log stream for incremental output. */
|
|
58
|
+
private runViaProcess;
|
|
59
|
+
/** Real termination, reported honestly: `false` means the command may still be running. */
|
|
60
|
+
private kill;
|
|
61
|
+
private exitCodeOf;
|
|
62
|
+
/**
|
|
63
|
+
* One `listFiles` round trip — the vendor listing already says directory-or-not, so no
|
|
64
|
+
* stat per entry.
|
|
65
|
+
*
|
|
66
|
+
* Caveat: it carries no symlink kind, so a symlink reads as its target's kind. As on E2B,
|
|
67
|
+
* `DirEntry.kind` here is not strict lstat semantics; callers that must distinguish a
|
|
68
|
+
* symlink stat explicitly. Mapped as reported rather than guessed at.
|
|
69
|
+
*/
|
|
70
|
+
listDir(path: string): Promise<readonly DirEntry[]>;
|
|
71
|
+
mkdir(path: string, options?: {
|
|
72
|
+
parents?: boolean;
|
|
73
|
+
existOk?: boolean;
|
|
74
|
+
}): Promise<void>;
|
|
75
|
+
readBytes(path: string, range?: ByteRange): Promise<Buffer>;
|
|
76
|
+
protected writeBytesRaw(path: string, data: Buffer): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Always `undefined` on this transport: the SDK implements tunnels over RPC only, and the
|
|
79
|
+
* route transport's `tunnels` accessor throws. Saying so plainly beats inventing a URL that
|
|
80
|
+
* would not resolve.
|
|
81
|
+
*/
|
|
82
|
+
exposedPortUrl(): Promise<string | undefined>;
|
|
83
|
+
protected resolve(path: string): string;
|
|
84
|
+
}
|
|
85
|
+
/** Cloudflare takes a command STRING; quote every argv element so nothing is re-split. */
|
|
86
|
+
export declare function shellJoin(argv: readonly string[]): string;
|
|
87
|
+
//# sourceMappingURL=machine.d.ts.map
|