ai-sdk-sandbox-gce 0.4.1 → 0.4.2
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/CHANGELOG.md +7 -0
- package/README.md +19 -5
- package/dist/client/gce-settings.d.ts +20 -0
- package/dist/client/lifecycle/create-session.d.ts +11 -1
- package/dist/client/lifecycle/create-session.js +19 -1
- package/dist/client/lifecycle/sandbox-template.d.ts +7 -0
- package/dist/client/lifecycle/sandbox-template.js +7 -0
- package/dist/client/session/file-info.d.ts +23 -0
- package/dist/client/session/file-info.js +24 -0
- package/dist/client/session/gce-sandbox-session.d.ts +4 -0
- package/dist/client/session/gce-sandbox-session.js +59 -5
- package/dist/client/transport/sandbox-service-client.d.ts +2 -1
- package/dist/client/transport/sandbox-service-client.js +3 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# ai-sdk-sandbox-gce
|
|
2
2
|
|
|
3
|
+
## 0.4.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 8341bb9: `deleteGceSandboxTemplate()` deletes the template that sandboxes created with a given `template`, `setup` and `image` boot from. A template made for a `setup` of its own, one per project say, used to outlive that setup when it changed or went, its image billed for its storage until deleted by hand. The sandboxes made from it keep their disk; the next sandbox created from the same template builds it again, without first meeting a 404 in the process that deleted it. A template that does not exist is no error.
|
|
8
|
+
- c89c8ef: Sessions implement `stat`, `listDirectory` and `deleteFile`, which `SandboxSession` requires since `@ai-sdk/provider-utils` 5.0.81. `stat` follows symbolic links and answers `null` for a path that does not exist; `listDirectory` reports a link as `symlink` without following it, answers `null` for a missing directory and rejects with `ENOTDIR` for a file; `deleteFile` deletes a link rather than its target, answers `false` for a missing file and rejects with `EISDIR` for a directory.
|
|
9
|
+
|
|
3
10
|
## 0.4.1
|
|
4
11
|
|
|
5
12
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -135,7 +135,7 @@ gcloud compute instances create sandbox-proxy --zone=$ZONE --machine-type=e2-sma
|
|
|
135
135
|
#!/bin/sh
|
|
136
136
|
set -e
|
|
137
137
|
# The version of the proxy: the one your application depends on.
|
|
138
|
-
VERSION=0.4.
|
|
138
|
+
VERSION=0.4.2
|
|
139
139
|
# Writes $2 to the file $1 when it differs, and says whether it did.
|
|
140
140
|
changed() {
|
|
141
141
|
mkdir -p "$(dirname "$1")"
|
|
@@ -282,7 +282,7 @@ echo "deb [signed-by=/etc/apt/keyrings/nodesource.asc] https://deb.nodesource.co
|
|
|
282
282
|
> /etc/apt/sources.list.d/nodesource.list
|
|
283
283
|
apt-get update
|
|
284
284
|
apt-get install -y nodejs
|
|
285
|
-
npm install --global ai-sdk-sandbox-gce@0.4.
|
|
285
|
+
npm install --global ai-sdk-sandbox-gce@0.4.2 pnpm@10
|
|
286
286
|
|
|
287
287
|
# The sandbox's user: sudo without a password, and Docker.
|
|
288
288
|
useradd --create-home --shell /bin/bash agent
|
|
@@ -310,7 +310,7 @@ The agent runs as root and starts the commands as `agent`, in its own service: `
|
|
|
310
310
|
|
|
311
311
|
### 4. The service
|
|
312
312
|
|
|
313
|
-
The service's image is published on Docker Hub with each release, `docker.io/fpasquet/ai-sdk-sandbox-gce:<version>`: Node.js and the package, built by the release workflow from the version on npm. Deploy the version your application depends on. Each image carries a provenance attestation, which `gh attestation verify oci://docker.io/fpasquet/ai-sdk-sandbox-gce:0.4.
|
|
313
|
+
The service's image is published on Docker Hub with each release, `docker.io/fpasquet/ai-sdk-sandbox-gce:<version>`: Node.js and the package, built by the release workflow from the version on npm. Deploy the version your application depends on. Each image carries a provenance attestation, which `gh attestation verify oci://docker.io/fpasquet/ai-sdk-sandbox-gce:0.4.2 --owner fpasquet` checks; its [`Dockerfile`](https://github.com/fpasquet/ai-sdk-harness/blob/main/packages/sandbox-gce/Dockerfile) builds the same image from your own registry.
|
|
314
314
|
|
|
315
315
|
Its service account gets a role of its own, with what the service calls and nothing else, limited by a condition to the names it uses: the sandboxes' machines and disks (`sbx-`), the proxy's machine, the sandbox images and templates (`ai-sdk-`).
|
|
316
316
|
|
|
@@ -348,7 +348,7 @@ for secret in sandbox-agent-secret sandbox-proxy-token sandbox-service-token; do
|
|
|
348
348
|
--member=serviceAccount:sandboxes@$PROJECT.iam.gserviceaccount.com --role=roles/secretmanager.secretAccessor
|
|
349
349
|
done
|
|
350
350
|
|
|
351
|
-
gcloud run deploy sandboxes --region=$REGION --image=docker.io/fpasquet/ai-sdk-sandbox-gce:0.4.
|
|
351
|
+
gcloud run deploy sandboxes --region=$REGION --image=docker.io/fpasquet/ai-sdk-sandbox-gce:0.4.2 \
|
|
352
352
|
--service-account=sandboxes@$PROJECT.iam.gserviceaccount.com \
|
|
353
353
|
--network=sandboxes --subnet=service --vpc-egress=private-ranges-only \
|
|
354
354
|
--no-allow-unauthenticated --min-instances=0 --timeout=3600 --concurrency=1000 \
|
|
@@ -468,6 +468,20 @@ A harness bootstraps itself in the sandbox before its first turn: Claude Code in
|
|
|
468
468
|
|
|
469
469
|
The image is named after the template's id and a digest of this package's version and the base image, a family resolved to its latest image: `ai-sdk-harness-template-<digest>-<base>`. After a new sandbox image, or a new version of the package, the first sandbox makes the template again. Delete the old images (label `ai-sdk-sandbox=template`) now and then: each costs its storage.
|
|
470
470
|
|
|
471
|
+
A template whose sandboxes are made with a `setup` of their own, one per project say, outlives that setup when it changes or goes. Delete it then, with what the sandboxes were created with:
|
|
472
|
+
|
|
473
|
+
```ts
|
|
474
|
+
import { deleteGceSandboxTemplate } from 'ai-sdk-sandbox-gce';
|
|
475
|
+
|
|
476
|
+
await deleteGceSandboxTemplate({
|
|
477
|
+
url: process.env.GCE_SANDBOX_URL!,
|
|
478
|
+
template: await agent.getSandboxTemplate(),
|
|
479
|
+
setup, // and `image`, when the sandboxes had one
|
|
480
|
+
});
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
The sandboxes made from it keep their disk. The next sandbox created from the same template, setup and image builds it again. A template that does not exist is no error.
|
|
484
|
+
|
|
471
485
|
A template is made from a sandbox no agent ever ran in: the client's throwaway one, where only `setup` and the harness's own installation run. Never save a sandbox an agent worked in as a template, through the API or otherwise: whatever the agent left on its disk, the `agent` service of the machine included, would start in every later sandbox.
|
|
472
486
|
|
|
473
487
|
Making a template takes a few minutes: the machine boots, the harness installs, Compute Engine saves the disk, and the first sandbox on a new image waits a minute more while Compute Engine spreads it. Make it when you deploy, rather than in the first user's session, with a script that creates a sandbox from the agent's template and deletes it:
|
|
@@ -714,7 +728,7 @@ To keep the bill down: stop idle sandboxes early, delete finished ones, delete o
|
|
|
714
728
|
- **Inside the VPC, traffic is plain HTTP**: the service's calls to the agents and to the proxy's administration API, real credentials included, and the sandboxes' requests to the base URL routes. Google Cloud encrypts traffic between its hosts and lets no machine listen to another's; nothing of the package adds TLS on top.
|
|
715
729
|
- **The sandbox's user has `sudo`, and Docker**: inside the machine, the agent's code can do anything. The machine is the boundary, not the user. What the agent changes on the disk, the machine's own `agent` service included, survives a stop: never hand a sandbox to another user, and never save one an agent worked in as a template.
|
|
716
730
|
- **An allowed host is a way out for data**: the allow list says where the sandbox's requests may go, not what they carry. An agent led astray by a prompt injection may push the sandbox's files to `github.com`, or publish them to a registry, with credentials of its own. Allow only the hosts a task needs; keep `*` and wildcards on shared domains (`*.googleapis.com`, `*.amazonaws.com`, `*.githubusercontent.com`, `*.cloudfront.net`) out of production, since anyone can own a bucket or a site under them. The service logs a warning when `SANDBOX_ALLOWED_HOSTS` holds `*`.
|
|
717
|
-
- **A credential goes to every request its transformation matches**: the harnesses' own transformations match the placeholder the sandbox sends, so only a request carrying it gets the real value. A transformation of yours that names a host alone puts the credential in any request to that host.
|
|
731
|
+
- **A credential goes to every request its transformation matches**: the harnesses' own transformations match the placeholder the sandbox sends, so only a request carrying it gets the real value. A transformation of yours that names a host alone puts the credential in any request to that host. An agent led astray can still use a credential, without ever reading it: cap it at the provider, a budget and the narrowest scope a token takes, as [Limiting what a credential can do](https://ai-sdk-harness.pages.dev/docs/securing-credentials#limiting-what-a-credential-can-do) shows.
|
|
718
732
|
- **The proxy holds the credentials of every running sandbox**, in memory. Keep it a machine of its own, with nothing else on it, a service account that may only read its token and write logs, and its administration port open to the service alone.
|
|
719
733
|
- **The service trusts its callers**: anyone granted `roles/run.invoker` can run commands in any sandbox. It has no notion of a user: your application decides which sandbox a user reaches. Name sandboxes after a random id, kept beside the user's session on the server, never after anything a user can guess or send. Never deploy it with `--allow-unauthenticated`; `SANDBOX_SERVICE_TOKEN` is the second lock, required on Cloud Run.
|
|
720
734
|
- **A machine stopped outside the service keeps its credentials in the proxy for a while**: a Spot machine taken back, one stopped at `SANDBOX_MAX_RUN_HOURS` or from the console. The application stopping or deleting the sandbox makes the proxy forget them at once, and so does the proxy restarting. A new machine that gets the same address takes the place of the old one in the proxy, so it never reaches them. Stop idle sandboxes from the application even when their machine already stopped.
|
|
@@ -110,6 +110,26 @@ export interface GcePrepareSettings extends GceConnectionSettings {
|
|
|
110
110
|
/** Aborts the call. */
|
|
111
111
|
abortSignal?: AbortSignal;
|
|
112
112
|
}
|
|
113
|
+
/** Options of `deleteGceSandboxTemplate()`: the template, as the sandboxes made from it were. */
|
|
114
|
+
export interface GceTemplateDeletionSettings extends GceConnectionSettings {
|
|
115
|
+
/** The template the sandboxes were created from, `await agent.getSandboxTemplate()`. */
|
|
116
|
+
template: HarnessV1SandboxTemplate;
|
|
117
|
+
/**
|
|
118
|
+
* The `setup` the sandboxes were created with: with the template, what names the template's
|
|
119
|
+
* image.
|
|
120
|
+
*
|
|
121
|
+
* @defaultValue `[]`
|
|
122
|
+
*/
|
|
123
|
+
setup?: readonly string[];
|
|
124
|
+
/**
|
|
125
|
+
* The image the sandboxes were created on, by the name the service gives it in `SANDBOX_IMAGES`.
|
|
126
|
+
*
|
|
127
|
+
* @defaultValue the service's `SANDBOX_IMAGE`
|
|
128
|
+
*/
|
|
129
|
+
image?: string;
|
|
130
|
+
/** Aborts the call. */
|
|
131
|
+
abortSignal?: AbortSignal;
|
|
132
|
+
}
|
|
113
133
|
/** Options of `createGceNetworkSandboxSession()`. */
|
|
114
134
|
export type GceNetworkSandboxSessionCreateOptions = HarnessV1SandboxSessionCreateOptions<GceCreationSettings>;
|
|
115
135
|
/** Options of `resumeGceNetworkSandboxSession()`. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { GceNetworkSandboxSessionCreateOptions, GceNetworkSandboxSessionResumeOptions, GcePrepareSettings } from '../gce-settings.js';
|
|
1
|
+
import type { GceNetworkSandboxSessionCreateOptions, GceNetworkSandboxSessionResumeOptions, GcePrepareSettings, GceTemplateDeletionSettings } from '../gce-settings.js';
|
|
2
2
|
import { GceNetworkSandboxSession } from '../session/gce-network-sandbox-session.js';
|
|
3
3
|
/**
|
|
4
4
|
* Creates a sandbox, a [Compute Engine](https://cloud.google.com/compute) virtual machine of its
|
|
@@ -28,6 +28,16 @@ export declare function createGceNetworkSandboxSession(options: GceNetworkSandbo
|
|
|
28
28
|
* nothing.
|
|
29
29
|
*/
|
|
30
30
|
export declare function prepareGceSandboxService(options: GcePrepareSettings): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Deletes the template that sandboxes created with `template`, `setup` and `image` boot from: the
|
|
33
|
+
* image the service saved of a prepared sandbox, billed for its storage until it goes. Call it once
|
|
34
|
+
* no sandbox is to be made from it any more, say when the setup it was made with changes.
|
|
35
|
+
*
|
|
36
|
+
* The sandboxes made from it keep their disk, and run, stop and resume as before. A sandbox created
|
|
37
|
+
* from the same template afterwards, by this process or another one, builds it again. Deleting a
|
|
38
|
+
* template that does not exist, or no longer does, is no error.
|
|
39
|
+
*/
|
|
40
|
+
export declare function deleteGceSandboxTemplate(options: GceTemplateDeletionSettings): Promise<void>;
|
|
31
41
|
/**
|
|
32
42
|
* Reattaches to a sandbox made by {@link createGceNetworkSandboxSession}, from this process or
|
|
33
43
|
* another one: a running sandbox as it is, a stopped one started again on its disk. Never creates
|
|
@@ -5,7 +5,7 @@ import { identityToken } from '../transport/identity-token.js';
|
|
|
5
5
|
import { SandboxServiceClient } from '../transport/sandbox-service-client.js';
|
|
6
6
|
import { abortReason } from '../utils/abort.js';
|
|
7
7
|
import { assertRequiredCommands, DEFAULT_REQUIRED_COMMANDS } from './required-commands.js';
|
|
8
|
-
import { runSetup, templateId, withTemplate } from './sandbox-template.js';
|
|
8
|
+
import { forgetTemplate, runSetup, templateId, withTemplate, } from './sandbox-template.js';
|
|
9
9
|
/**
|
|
10
10
|
* What a Compute Engine instance name takes once the service's prefix is put before it, and a URL
|
|
11
11
|
* path as it is: the instance name is at most 63 characters and never ends with a dash.
|
|
@@ -94,6 +94,24 @@ export async function prepareGceSandboxService(options) {
|
|
|
94
94
|
abortSignal,
|
|
95
95
|
});
|
|
96
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Deletes the template that sandboxes created with `template`, `setup` and `image` boot from: the
|
|
99
|
+
* image the service saved of a prepared sandbox, billed for its storage until it goes. Call it once
|
|
100
|
+
* no sandbox is to be made from it any more, say when the setup it was made with changes.
|
|
101
|
+
*
|
|
102
|
+
* The sandboxes made from it keep their disk, and run, stop and resume as before. A sandbox created
|
|
103
|
+
* from the same template afterwards, by this process or another one, builds it again. Deleting a
|
|
104
|
+
* template that does not exist, or no longer does, is no error.
|
|
105
|
+
*/
|
|
106
|
+
export async function deleteGceSandboxTemplate(options) {
|
|
107
|
+
const { template, setup = [], image, abortSignal } = options;
|
|
108
|
+
if (abortSignal?.aborted)
|
|
109
|
+
throw abortReason(abortSignal);
|
|
110
|
+
const client = clientOf(options);
|
|
111
|
+
const source = { template, setup, image };
|
|
112
|
+
forgetTemplate(client, source);
|
|
113
|
+
await client.deleteTemplate(templateId(source), image, abortSignal);
|
|
114
|
+
}
|
|
97
115
|
/**
|
|
98
116
|
* Reattaches to a sandbox made by {@link createGceNetworkSandboxSession}, from this process or
|
|
99
117
|
* another one: a running sandbox as it is, a stopped one started again on its disk. Never creates
|
|
@@ -41,6 +41,13 @@ export declare function runSetup(session: GceNetworkSandboxSession, setup: reado
|
|
|
41
41
|
* already installed.
|
|
42
42
|
*/
|
|
43
43
|
export declare function ensureTemplate(client: SandboxServiceClient, source: BuildSettings & TemplateSource, abortSignal?: AbortSignal): Promise<ReadyTemplate>;
|
|
44
|
+
/**
|
|
45
|
+
* Forgets the template of `source` on this service, built or being built: the next sandbox from it
|
|
46
|
+
* asks the service whether it exists, rather than create from it and meet a 404 first.
|
|
47
|
+
*/
|
|
48
|
+
export declare function forgetTemplate(client: SandboxServiceClient, source: TemplateSource & {
|
|
49
|
+
image?: string;
|
|
50
|
+
}): void;
|
|
44
51
|
/**
|
|
45
52
|
* Calls `create` with the template ready. This process remembers a template for as long as it runs,
|
|
46
53
|
* and the service can lose it meanwhile, to a new version or a new base image: on
|
|
@@ -58,6 +58,13 @@ export function ensureTemplate(client, source, abortSignal) {
|
|
|
58
58
|
}
|
|
59
59
|
return ready;
|
|
60
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Forgets the template of `source` on this service, built or being built: the next sandbox from it
|
|
63
|
+
* asks the service whether it exists, rather than create from it and meet a 404 first.
|
|
64
|
+
*/
|
|
65
|
+
export function forgetTemplate(client, source) {
|
|
66
|
+
building.delete(keyOf(client, source));
|
|
67
|
+
}
|
|
61
68
|
/**
|
|
62
69
|
* Calls `create` with the template ready. This process remembers a template for as long as it runs,
|
|
63
70
|
* and the service can lose it meanwhile, to a new version or a new base image: on
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Information about a path, symbolic links followed: what `stat` of a session resolves to. */
|
|
2
|
+
export interface FileStat {
|
|
3
|
+
type: 'directory' | 'file' | 'other';
|
|
4
|
+
/** Size in bytes; for a directory, what the sandbox's filesystem says. */
|
|
5
|
+
size: number;
|
|
6
|
+
}
|
|
7
|
+
/** One entry of a directory: what `listDirectory` of a session resolves to. */
|
|
8
|
+
export interface DirectoryEntry {
|
|
9
|
+
name: string;
|
|
10
|
+
/** A symbolic link is reported as such, not followed. */
|
|
11
|
+
type: 'directory' | 'file' | 'other' | 'symlink';
|
|
12
|
+
}
|
|
13
|
+
/** The output of the `STAT` script, `<type> <size>`, as a {@link FileStat}. */
|
|
14
|
+
export declare function parseStat(stdout: Uint8Array): FileStat;
|
|
15
|
+
/** The output of the `LIST` script, `<type> <name>` records each ended by a NUL, as entries. */
|
|
16
|
+
export declare function parseEntries(stdout: Uint8Array): DirectoryEntry[];
|
|
17
|
+
declare const MESSAGES: {
|
|
18
|
+
EISDIR: string;
|
|
19
|
+
ENOTDIR: string;
|
|
20
|
+
};
|
|
21
|
+
/** The error Node.js's `fs` throws for `code`, on the operation `syscall` of `path`. */
|
|
22
|
+
export declare function fileError(code: keyof typeof MESSAGES, syscall: string, path: string): Error;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** The output of the `STAT` script, `<type> <size>`, as a {@link FileStat}. */
|
|
2
|
+
export function parseStat(stdout) {
|
|
3
|
+
const [type = 'other', size = '0'] = Buffer.from(stdout).toString().trim().split(/\s+/);
|
|
4
|
+
return { type: type, size: Number(size) || 0 };
|
|
5
|
+
}
|
|
6
|
+
/** The output of the `LIST` script, `<type> <name>` records each ended by a NUL, as entries. */
|
|
7
|
+
export function parseEntries(stdout) {
|
|
8
|
+
return Buffer.from(stdout)
|
|
9
|
+
.toString()
|
|
10
|
+
.split('\0')
|
|
11
|
+
.filter((record) => record !== '')
|
|
12
|
+
.map((record) => {
|
|
13
|
+
const space = record.indexOf(' ');
|
|
14
|
+
return {
|
|
15
|
+
type: record.slice(0, space),
|
|
16
|
+
name: record.slice(space + 1),
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
const MESSAGES = { EISDIR: 'illegal operation on a directory', ENOTDIR: 'not a directory' };
|
|
21
|
+
/** The error Node.js's `fs` throws for `code`, on the operation `syscall` of `path`. */
|
|
22
|
+
export function fileError(code, syscall, path) {
|
|
23
|
+
return Object.assign(new Error(`${code}: ${MESSAGES[code]}, ${syscall} '${path}'`), { code });
|
|
24
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Experimental_SandboxSession as SandboxSession, Experimental_SandboxSpawnOptions as SpawnOptions } from '@ai-sdk/provider-utils';
|
|
2
2
|
import type { SandboxServiceClient } from '../transport/sandbox-service-client.js';
|
|
3
|
+
import type { DirectoryEntry, FileStat } from './file-info.js';
|
|
3
4
|
type ReadOptions = Parameters<SandboxSession['readFile']>[0];
|
|
4
5
|
type ReadTextOptions = Parameters<SandboxSession['readTextFile']>[0];
|
|
5
6
|
type WriteOptions = Parameters<SandboxSession['writeFile']>[0];
|
|
@@ -36,6 +37,9 @@ export declare class GceSandboxSession implements SandboxSession {
|
|
|
36
37
|
writeBinaryFile: ({ path, content, abortSignal }: WriteBinaryOptions) => Promise<void>;
|
|
37
38
|
writeFile: ({ content, ...options }: WriteOptions) => Promise<void>;
|
|
38
39
|
writeTextFile: ({ content, encoding, ...options }: WriteTextOptions) => Promise<void>;
|
|
40
|
+
stat: ({ path, abortSignal }: ReadOptions) => Promise<FileStat | null>;
|
|
41
|
+
listDirectory: ({ path, abortSignal }: ReadOptions) => Promise<DirectoryEntry[] | null>;
|
|
42
|
+
deleteFile: ({ path, abortSignal }: ReadOptions) => Promise<boolean>;
|
|
39
43
|
/** Stops every process this session started and that may still run. */
|
|
40
44
|
protected killAll(): Promise<void>;
|
|
41
45
|
/** The process `id`, its output read from `response` as it comes. */
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { posix } from 'node:path';
|
|
2
2
|
import { FrameType, readFrames, STDERR, STDOUT } from '../../protocol/frames.js';
|
|
3
3
|
import { abortReason } from '../utils/abort.js';
|
|
4
|
+
import { fileError, parseEntries, parseStat } from './file-info.js';
|
|
4
5
|
import { processInput } from './process-input.js';
|
|
5
6
|
const READ_MISSING = 44;
|
|
6
7
|
const READ_DIRECTORY = 45;
|
|
@@ -8,6 +9,31 @@ const READ_DIRECTORY = 45;
|
|
|
8
9
|
const READ = `[ -d "$0" ] && exit ${READ_DIRECTORY}; [ -e "$0" ] || exit ${READ_MISSING}; exec cat -- "$0"`;
|
|
9
10
|
/** Writes stdin to the file `$0`, creating its parent directories. */
|
|
10
11
|
const WRITE = 'mkdir -p "$(dirname "$0")" && cat > "$0"';
|
|
12
|
+
/** Exit status of {@link LIST} for a path that is not a directory. */
|
|
13
|
+
const NOT_DIRECTORY = 46;
|
|
14
|
+
/**
|
|
15
|
+
* The type of the path `$0`, symbolic links followed, then its size in bytes: `file 12`. Exits
|
|
16
|
+
* {@link READ_MISSING} for a path that does not exist.
|
|
17
|
+
*/
|
|
18
|
+
const STAT = `[ -e "$0" ] || exit ${READ_MISSING}; ` +
|
|
19
|
+
'if [ -d "$0" ]; then t=directory; elif [ -f "$0" ]; then t=file; else t=other; fi; ' +
|
|
20
|
+
's=$(stat -L -c %s -- "$0" 2>/dev/null || wc -c < "$0" 2>/dev/null || echo 0); echo "$t" $s';
|
|
21
|
+
/**
|
|
22
|
+
* Every entry of the directory `$0` as `<type> <name>`, each ended by a NUL: a name may hold any
|
|
23
|
+
* other byte. Symbolic links are reported as such, not followed. Exits {@link READ_MISSING} for a
|
|
24
|
+
* path that does not exist and {@link NOT_DIRECTORY} for one that is not a directory.
|
|
25
|
+
*/
|
|
26
|
+
const LIST = `[ -d "$0" ] || { [ -e "$0" ] && exit ${NOT_DIRECTORY}; exit ${READ_MISSING}; }; cd -- "$0" || exit 1; ` +
|
|
27
|
+
'for f in * .[!.]* ..?*; do ' +
|
|
28
|
+
'if [ -L "$f" ]; then t=symlink; elif [ -d "$f" ]; then t=directory; elif [ -f "$f" ]; then t=file; ' +
|
|
29
|
+
'elif [ -e "$f" ]; then t=other; else continue; fi; ' +
|
|
30
|
+
'printf \'%s %s\\0\' "$t" "$f"; done';
|
|
31
|
+
/**
|
|
32
|
+
* Deletes the file `$0`, a symbolic link itself rather than its target. Exits {@link READ_MISSING}
|
|
33
|
+
* for a path that does not exist and {@link READ_DIRECTORY} for a directory.
|
|
34
|
+
*/
|
|
35
|
+
const DELETE = `if [ -L "$0" ]; then :; elif [ -d "$0" ]; then exit ${READ_DIRECTORY}; elif [ ! -e "$0" ]; then exit ${READ_MISSING}; fi; ` +
|
|
36
|
+
'exec rm -f -- "$0"';
|
|
11
37
|
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
12
38
|
/** `sh -c BODY ARG`, the argument quoted, never spliced into the body. */
|
|
13
39
|
const script = (body, arg) => `sh -c '${body.replace(/'/g, `'\\''`)}' '${arg.replace(/'/g, `'\\''`)}'`;
|
|
@@ -104,11 +130,8 @@ export class GceSandboxSession {
|
|
|
104
130
|
const { exitCode, stdout, stderr } = await this.exec(script(READ, target), { abortSignal });
|
|
105
131
|
if (exitCode === READ_MISSING)
|
|
106
132
|
return null;
|
|
107
|
-
if (exitCode === READ_DIRECTORY)
|
|
108
|
-
throw
|
|
109
|
-
code: 'EISDIR',
|
|
110
|
-
});
|
|
111
|
-
}
|
|
133
|
+
if (exitCode === READ_DIRECTORY)
|
|
134
|
+
throw fileError('EISDIR', 'read', target);
|
|
112
135
|
if (exitCode !== 0)
|
|
113
136
|
throw new Error(`Could not read ${target} in the sandbox: ${stderr.trim()}`);
|
|
114
137
|
return stdout;
|
|
@@ -146,6 +169,37 @@ export class GceSandboxSession {
|
|
|
146
169
|
...options,
|
|
147
170
|
content: new Uint8Array(Buffer.from(content, encoding)),
|
|
148
171
|
});
|
|
172
|
+
stat = async ({ path, abortSignal }) => {
|
|
173
|
+
const target = this.resolve(path);
|
|
174
|
+
const { exitCode, stdout, stderr } = await this.exec(script(STAT, target), { abortSignal });
|
|
175
|
+
if (exitCode === READ_MISSING)
|
|
176
|
+
return null;
|
|
177
|
+
if (exitCode !== 0)
|
|
178
|
+
throw new Error(`Could not stat ${target} in the sandbox: ${stderr.trim()}`);
|
|
179
|
+
return parseStat(stdout);
|
|
180
|
+
};
|
|
181
|
+
listDirectory = async ({ path, abortSignal }) => {
|
|
182
|
+
const target = this.resolve(path);
|
|
183
|
+
const { exitCode, stdout, stderr } = await this.exec(script(LIST, target), { abortSignal });
|
|
184
|
+
if (exitCode === READ_MISSING)
|
|
185
|
+
return null;
|
|
186
|
+
if (exitCode === NOT_DIRECTORY)
|
|
187
|
+
throw fileError('ENOTDIR', 'scandir', target);
|
|
188
|
+
if (exitCode !== 0)
|
|
189
|
+
throw new Error(`Could not list ${target} in the sandbox: ${stderr.trim()}`);
|
|
190
|
+
return parseEntries(stdout);
|
|
191
|
+
};
|
|
192
|
+
deleteFile = async ({ path, abortSignal }) => {
|
|
193
|
+
const target = this.resolve(path);
|
|
194
|
+
const { exitCode, stderr } = await this.exec(script(DELETE, target), { abortSignal });
|
|
195
|
+
if (exitCode === READ_MISSING)
|
|
196
|
+
return false;
|
|
197
|
+
if (exitCode === READ_DIRECTORY)
|
|
198
|
+
throw fileError('EISDIR', 'unlink', target);
|
|
199
|
+
if (exitCode !== 0)
|
|
200
|
+
throw new Error(`Could not delete ${target} in the sandbox: ${stderr.trim()}`);
|
|
201
|
+
return true;
|
|
202
|
+
};
|
|
149
203
|
/** Stops every process this session started and that may still run. */
|
|
150
204
|
async killAll() {
|
|
151
205
|
const { client, name, processes } = this.handle;
|
|
@@ -84,7 +84,8 @@ export declare class SandboxServiceClient {
|
|
|
84
84
|
image?: string;
|
|
85
85
|
abortSignal?: AbortSignal;
|
|
86
86
|
}): Promise<void>;
|
|
87
|
-
|
|
87
|
+
/** Deletes the template `id` made on `image`: no error when there is none. */
|
|
88
|
+
deleteTemplate(id: string, image?: string, abortSignal?: AbortSignal): Promise<void>;
|
|
88
89
|
/**
|
|
89
90
|
* Where a port of the sandbox is reached: a WebSocket through the service. The token is read
|
|
90
91
|
* anew each time the harness connects: a bridge connection outlives a token.
|
|
@@ -177,8 +177,9 @@ export class SandboxServiceClient {
|
|
|
177
177
|
accept: [404],
|
|
178
178
|
});
|
|
179
179
|
}
|
|
180
|
-
|
|
181
|
-
|
|
180
|
+
/** Deletes the template `id` made on `image`: no error when there is none. */
|
|
181
|
+
async deleteTemplate(id, image, abortSignal) {
|
|
182
|
+
await this.call('DELETE', templatePath(id, image), { abortSignal });
|
|
182
183
|
}
|
|
183
184
|
/**
|
|
184
185
|
* Where a port of the sandbox is reached: a WebSocket through the service. The token is read
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { GceSandboxImageError } from './client/errors/gce-sandbox-image-error.js';
|
|
2
2
|
export { GceSandboxNotFoundError } from './client/errors/gce-sandbox-not-found-error.js';
|
|
3
3
|
export { GceSandboxServiceError } from './client/errors/gce-sandbox-service-error.js';
|
|
4
|
-
export type { GceAuth, GceConnectionSettings, GceCreationSettings, GceNetworkSandboxSessionCreateOptions, GceNetworkSandboxSessionResumeOptions, GcePrepareSettings, } from './client/gce-settings.js';
|
|
5
|
-
export { createGceNetworkSandboxSession, prepareGceSandboxService, resumeGceNetworkSandboxSession, } from './client/lifecycle/create-session.js';
|
|
4
|
+
export type { GceAuth, GceConnectionSettings, GceCreationSettings, GceNetworkSandboxSessionCreateOptions, GceNetworkSandboxSessionResumeOptions, GcePrepareSettings, GceTemplateDeletionSettings, } from './client/gce-settings.js';
|
|
5
|
+
export { createGceNetworkSandboxSession, deleteGceSandboxTemplate, prepareGceSandboxService, resumeGceNetworkSandboxSession, } from './client/lifecycle/create-session.js';
|
|
6
6
|
export { GCE_SANDBOX_PROVIDER_ID } from './client/provider-id.js';
|
|
7
7
|
export { GceNetworkSandboxSession } from './client/session/gce-network-sandbox-session.js';
|
|
8
8
|
export { GceSandboxSession } from './client/session/gce-sandbox-session.js';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { GceSandboxImageError } from './client/errors/gce-sandbox-image-error.js';
|
|
2
2
|
export { GceSandboxNotFoundError } from './client/errors/gce-sandbox-not-found-error.js';
|
|
3
3
|
export { GceSandboxServiceError } from './client/errors/gce-sandbox-service-error.js';
|
|
4
|
-
export { createGceNetworkSandboxSession, prepareGceSandboxService, resumeGceNetworkSandboxSession, } from './client/lifecycle/create-session.js';
|
|
4
|
+
export { createGceNetworkSandboxSession, deleteGceSandboxTemplate, prepareGceSandboxService, resumeGceNetworkSandboxSession, } from './client/lifecycle/create-session.js';
|
|
5
5
|
export { GCE_SANDBOX_PROVIDER_ID } from './client/provider-id.js';
|
|
6
6
|
export { GceNetworkSandboxSession } from './client/session/gce-network-sandbox-session.js';
|
|
7
7
|
export { GceSandboxSession } from './client/session/gce-sandbox-session.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-sdk-sandbox-gce",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Compute Engine sandboxes for AI SDK harness agents: Claude Code, Codex and friends in a virtual machine of their own on Google Cloud, Docker included, stopped between turns.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -50,16 +50,16 @@
|
|
|
50
50
|
"@ai-sdk/provider-utils": "^5.0.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@ai-sdk/harness": "^1.0.
|
|
54
|
-
"@ai-sdk/provider-utils": "^5.0.
|
|
53
|
+
"@ai-sdk/harness": "^1.0.183",
|
|
54
|
+
"@ai-sdk/provider-utils": "^5.0.81",
|
|
55
55
|
"@types/node": "^24.19.2",
|
|
56
|
-
"@vitest/coverage-v8": "^5.0
|
|
56
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
57
57
|
"typescript": "^6.0.3",
|
|
58
|
-
"vitest": "^5.0
|
|
58
|
+
"vitest": "^5.1.0",
|
|
59
59
|
"zod": "^4.6.7",
|
|
60
60
|
"@repo/build": "0.0.0",
|
|
61
|
-
"@repo/prettier-config": "0.0.0",
|
|
62
61
|
"@repo/eslint-config": "0.0.0",
|
|
62
|
+
"@repo/prettier-config": "0.0.0",
|
|
63
63
|
"@repo/typescript-config": "0.0.0",
|
|
64
64
|
"@repo/vitest-config": "0.0.0"
|
|
65
65
|
},
|