ai-sdk-sandbox-gce 0.5.1 → 0.6.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/CHANGELOG.md +23 -0
- package/README.md +211 -64
- package/dist/client/errors/gce-sandbox-capacity-error.d.ts +20 -0
- package/dist/client/errors/gce-sandbox-capacity-error.js +22 -0
- package/dist/client/gce-settings.d.ts +42 -0
- package/dist/client/gce-usage.d.ts +37 -0
- package/dist/client/gce-usage.js +1 -0
- package/dist/client/lifecycle/create-session.d.ts +37 -1
- package/dist/client/lifecycle/create-session.js +77 -6
- package/dist/client/lifecycle/sandbox-template.d.ts +2 -0
- package/dist/client/lifecycle/sandbox-template.js +7 -2
- package/dist/client/session/gce-network-sandbox-session.d.ts +38 -0
- package/dist/client/session/gce-network-sandbox-session.js +56 -2
- package/dist/client/session/gce-sandbox-session.d.ts +5 -0
- package/dist/client/transport/sandbox-service-client.d.ts +28 -6
- package/dist/client/transport/sandbox-service-client.js +72 -9
- package/dist/common/http-error.d.ts +11 -1
- package/dist/common/http-error.js +3 -1
- package/dist/common/http-reply.d.ts +3 -6
- package/dist/common/http-reply.js +28 -6
- package/dist/common/logger.d.ts +17 -1
- package/dist/common/logger.js +8 -4
- package/dist/in-vm/agent-config.js +41 -5
- package/dist/in-vm/agent.d.ts +16 -0
- package/dist/in-vm/agent.js +24 -0
- package/dist/in-vm/metrics-sampler.d.ts +66 -0
- package/dist/in-vm/metrics-sampler.js +223 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +2 -1
- package/dist/protocol/agent.d.ts +3 -1
- package/dist/protocol/agent.js +3 -1
- package/dist/protocol/api.d.ts +163 -0
- package/dist/protocol/api.js +5 -0
- package/dist/protocol/labels.d.ts +16 -0
- package/dist/protocol/labels.js +37 -0
- package/dist/protocol/proxy.d.ts +23 -1
- package/dist/protocol/proxy.js +3 -1
- package/dist/proxy/admin-server.js +3 -2
- package/dist/proxy/egress-meter.d.ts +18 -0
- package/dist/proxy/egress-meter.js +37 -0
- package/dist/proxy/egress-policy.d.ts +3 -0
- package/dist/proxy/egress-proxy.js +1 -0
- package/dist/proxy/registered-sandbox.d.ts +3 -0
- package/dist/proxy/registered-sandbox.js +3 -0
- package/dist/proxy/registry.d.ts +8 -3
- package/dist/proxy/registry.js +11 -2
- package/dist/server/compute/compute-api.d.ts +41 -0
- package/dist/server/compute/compute-api.js +68 -13
- package/dist/server/http/routes.js +36 -8
- package/dist/server/sandboxes/agent-client.d.ts +11 -0
- package/dist/server/sandboxes/agent-client.js +27 -3
- package/dist/server/sandboxes/proxy-admin.d.ts +10 -3
- package/dist/server/sandboxes/proxy-admin.js +18 -3
- package/dist/server/sandboxes/sandboxes.d.ts +75 -16
- package/dist/server/sandboxes/sandboxes.js +340 -69
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
# ai-sdk-sandbox-gce
|
|
2
2
|
|
|
3
|
+
## 0.6.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- be187a8: A creation or a resume refused for `SANDBOX_MAX_RUNNING` now throws `GceSandboxCapacityError`, carrying `running`, `max` and `retryAfterSeconds`, for the caller to queue the call rather than read the message. It extends `GceSandboxServiceError`, its `status` still 429. The service answers that 429 with `code: 'SANDBOX_MAX_RUNNING'`, `running`, `max` and `retryAfterSeconds` in its body, and a `Retry-After` header.
|
|
8
|
+
- be187a8: `describeGceSandbox({ url, sandboxId })`, and `describe()` of a session, read the sandbox's machine from Compute Engine without starting it (`GET /v1/sandboxes/:name` on the service): when it was made, last started and last stopped, whether it is billed as Spot or standard (`spotFallback` says when a service of Spot machines fell back to a standard one), its machine type, the size of its disk, its image, whether it came from the warm pool, and the caller's labels.
|
|
9
|
+
- be187a8: The egress proxy counts the bytes each sandbox's connections carry, outside its machine. `usage()` of a session, or `getGceSandboxUsage()`, reads the count while the sandbox runs (`GET /v1/sandboxes/:name/usage` on the service, `GET /v1/sandboxes/:name/usage` on the proxy's administration API). The proxy keeps it in memory and forgets it with the sandbox: a stop and a deletion now answer the last count in a report (`POST …/suspend` and `DELETE /v1/sandboxes/:name` answer 200 with it, no longer 204), which `onUsageReport` is handed (by `stop()`, `destroy()` and `deleteGceSandbox()`) and `lastReport` of the session holds. Upgrade the proxy with the service: one of an earlier version counts nothing.
|
|
10
|
+
- be187a8: `createGceNetworkSandboxSession()` takes `labels`, put on the sandbox's machine and on its disk beside the service's own, for a Cloud Billing export or a budget to group their cost by. Keys starting with `ai-sdk-` are refused, and so are labels Compute Engine would refuse. A machine taken from the warm pool gets them as it is taken, and so does its disk, now labelled `ai-sdk-sandbox=pool` until then and `ai-sdk-sandbox-name=<sandbox>` after.
|
|
11
|
+
- be187a8: The service logs each creation, resume, suspension and deletion as a structured JSON line, `{ event, sandbox, at, … }`, beside its message: the machine, the image, the template, the caller's labels and the timings of a creation; the timings of a resume; the proxy's last count of a suspension or a deletion. Cloud Logging reads them as structured entries, for log-based metrics. With `LOG_FORMAT=text`, the message alone is written, as before.
|
|
12
|
+
- be187a8: The agent of each machine samples it every 30 s from `/proc` (CPU busy and stolen shares, memory in use, CPU and memory pressure, OOM kills), keeping the last day of samples on the machine's disk, across stops. `metrics()` of a session, or `getGceSandboxMetrics({ since })`, reads them while the sandbox runs (`GET /v1/sandboxes/:name/metrics?since=` on the service, `GET /v1/metrics` on the agent), and a stop or a deletion reads the last ones before the machine goes, in the `metrics` of its report. Read in the machine, they are fit for sizing, never for billing. `SANDBOX_METRICS_INTERVAL` sets the interval on the agent. Build the sandbox image again with this version to get them: an earlier agent samples nothing.
|
|
13
|
+
- be187a8: `timings` of a session says how long its creation or its resume took on the service, step by step: the egress proxy, Compute Engine making or starting the machine (or the warm pool handing one over, `fromPool`), its boot until the agent answered, then the agent's configuration, and the whole call. The service answers it with the sandbox, in the create and resume responses.
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- e1994c7: The service's Docker image is now based on Debian 13 (`node:24-trixie-slim`) and no longer contains npm, corepack or yarn: the service only runs Node.js. An image of your own built `FROM` it that calls npm must install it.
|
|
18
|
+
- be187a8: The sandbox service sends a Compute Engine call again, up to 3 times, when Compute Engine cannot be reached (a kept-alive connection closed while Cloud Run throttled the CPU), with a request id that makes a change safe to send twice; the last failure names the call and its cause instead of `fetch failed`. A sandbox created just after the last one stopped the egress proxy is registered again once the proxy is back, rather than failing with a 502 or a 500. Unexpected errors are logged with their cause.
|
|
19
|
+
|
|
20
|
+
## 0.5.2
|
|
21
|
+
|
|
22
|
+
### Patch Changes
|
|
23
|
+
|
|
24
|
+
- 455efc2: `deleteGceSandbox({ url, sandboxId })` deletes a sandbox by its id, its machine and its disk, without starting it: `destroy()` needs a session, which resumes a stopped sandbox, a minute of a billed machine only to delete it. A sandbox that does not exist, or no longer does, is no error. It is the `remove` of `sandboxPerSession()` of `ai-sdk-harness-sessions`.
|
|
25
|
+
|
|
3
26
|
## 0.5.1
|
|
4
27
|
|
|
5
28
|
### 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.
|
|
138
|
+
VERSION=0.6.0
|
|
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")"
|
|
@@ -283,7 +283,7 @@ echo "deb [signed-by=/etc/apt/keyrings/nodesource.asc] https://deb.nodesource.co
|
|
|
283
283
|
> /etc/apt/sources.list.d/nodesource.list
|
|
284
284
|
apt-get update
|
|
285
285
|
apt-get install -y nodejs
|
|
286
|
-
npm install --global ai-sdk-sandbox-gce@0.
|
|
286
|
+
npm install --global ai-sdk-sandbox-gce@0.6.0 pnpm@10
|
|
287
287
|
|
|
288
288
|
# The sandbox's user: sudo without a password, and Docker.
|
|
289
289
|
useradd --create-home --shell /bin/bash agent
|
|
@@ -311,7 +311,7 @@ The agent runs as root and starts the commands as `agent`, in its own service: `
|
|
|
311
311
|
|
|
312
312
|
### 4. The service
|
|
313
313
|
|
|
314
|
-
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.
|
|
314
|
+
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.6.0 --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.
|
|
315
315
|
|
|
316
316
|
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-`).
|
|
317
317
|
|
|
@@ -349,7 +349,7 @@ for secret in sandbox-agent-secret sandbox-proxy-token sandbox-service-token; do
|
|
|
349
349
|
--member=serviceAccount:sandboxes@$PROJECT.iam.gserviceaccount.com --role=roles/secretmanager.secretAccessor
|
|
350
350
|
done
|
|
351
351
|
|
|
352
|
-
gcloud run deploy sandboxes --region=$REGION --image=docker.io/fpasquet/ai-sdk-sandbox-gce:0.
|
|
352
|
+
gcloud run deploy sandboxes --region=$REGION --image=docker.io/fpasquet/ai-sdk-sandbox-gce:0.6.0 \
|
|
353
353
|
--service-account=sandboxes@$PROJECT.iam.gserviceaccount.com \
|
|
354
354
|
--network=sandboxes --subnet=service --vpc-egress=private-ranges-only \
|
|
355
355
|
--no-allow-unauthenticated --min-instances=0 --timeout=3600 --concurrency=1000 \
|
|
@@ -636,77 +636,83 @@ The MCP servers that [`ai-sdk-harness-plugins`](https://ai-sdk-harness.pages.dev
|
|
|
636
636
|
|
|
637
637
|
## Lifecycle
|
|
638
638
|
|
|
639
|
-
| Method | What it does
|
|
640
|
-
| -------------------------- |
|
|
641
|
-
| `release()` | Stops the processes this session started and forgets its credentials, leaving the sandbox running
|
|
642
|
-
| `killAllProcesses()` | Stops every process the machine's agent started
|
|
643
|
-
| `stop()` | Stops the machine, its disk kept. Idempotent
|
|
644
|
-
| `destroy()` | Deletes the machine and its disk. Idempotent
|
|
645
|
-
| `setNetworkPolicy(policy)` | Replaces the hosts the sandbox may reach
|
|
646
|
-
| `restricted()` | The files-and-processes view of the same sandbox, to hand to tools
|
|
647
|
-
| `fork({ ports })` | Another view of the same sandbox, with ports, processes and credentials of its own
|
|
639
|
+
| Method | What it does |
|
|
640
|
+
| -------------------------- | ----------------------------------------------------------------------------------------------------------------- |
|
|
641
|
+
| `release()` | Stops the processes this session started and forgets its credentials, leaving the sandbox running |
|
|
642
|
+
| `killAllProcesses()` | Stops every process the machine's agent started |
|
|
643
|
+
| `stop()` | Stops the machine, its disk kept. Idempotent |
|
|
644
|
+
| `destroy()` | Deletes the machine and its disk. Idempotent |
|
|
645
|
+
| `setNetworkPolicy(policy)` | Replaces the hosts the sandbox may reach |
|
|
646
|
+
| `restricted()` | The files-and-processes view of the same sandbox, to hand to tools |
|
|
647
|
+
| `fork({ ports })` | Another view of the same sandbox, with ports, processes and credentials of its own |
|
|
648
|
+
| `describe()` | The sandbox's machine, as Compute Engine describes it: see [Its machine](#its-machine) |
|
|
649
|
+
| `usage()` | What its connections carried through the proxy: see [What its connections carried](#what-its-connections-carried) |
|
|
650
|
+
| `lastReport` | The last measures the last `stop()` or `destroy()` of this view answered |
|
|
648
651
|
|
|
649
652
|
`fork()` lets several harness sessions run side by side in one machine, each with a bridge on its own port, as with `ai-sdk-sandbox-cloud-run`.
|
|
650
653
|
|
|
654
|
+
`deleteGceSandbox({ url, sandboxId })` deletes a sandbox by its id, its machine and its disk, without a session: a stopped one is deleted as it is, where `destroy()` needs a session, which resumes it, starting the machine for a minute only to delete it. A sandbox that does not exist, or no longer does, is no error. It is the `remove` of `sandboxPerSession()` of [`ai-sdk-harness-sessions`](https://ai-sdk-harness.pages.dev/docs/packages/harness-sessions#sandboxes), for the sessions closed while suspended.
|
|
655
|
+
|
|
651
656
|
## Options
|
|
652
657
|
|
|
653
658
|
`createGceNetworkSandboxSession(options)` takes every option below; `resumeGceNetworkSandboxSession(options)` takes `sandboxId`, `abortSignal` and the connection settings.
|
|
654
659
|
|
|
655
|
-
| Option | Default | Description
|
|
656
|
-
| ------------------ | ----------------- |
|
|
657
|
-
| `url` | required | _Connection._ The URL of the sandbox service
|
|
658
|
-
| `auth` | `'gcloud'` | _Connection._ `'gcloud'`, `'metadata'`, `'none'`, or a function returning an identity token
|
|
659
|
-
| `ports` | `[]` | _Connection._ Ports the harness may reach, through the service
|
|
660
|
-
| `allowedHosts` | `[]` | _Connection._ Hosts the sandbox may reach over HTTPS, on top of the service's
|
|
661
|
-
| `baseUrls` | `{}` | _Connection._ Base URL variables routed through the proxy, on top of the service's
|
|
662
|
-
| `nodeUseEnvProxy` | the service's | _Connection._ `true` sets `NODE_USE_ENV_PROXY=1` for the commands: see [Node.js and the proxy](#nodejs-and-the-proxy)
|
|
663
|
-
| `serviceToken` | none | _Connection._ The service's `SANDBOX_SERVICE_TOKEN`, when it has one
|
|
664
|
-
| `sandboxId` | `ai-sdk-<hex>` | The sandbox's name: lowercase letters, digits and dashes, 55 at most. Creation fails if it is taken
|
|
665
|
-
| `template` | none | `await agent.getSandboxTemplate()`: prepare once, boot every later sandbox from it
|
|
666
|
-
| `image` | `default` | The image the machine boots from, by the name the service gives it in `SANDBOX_IMAGES`
|
|
667
|
-
| `
|
|
668
|
-
| `
|
|
669
|
-
| `
|
|
660
|
+
| Option | Default | Description |
|
|
661
|
+
| ------------------ | ----------------- | ----------------------------------------------------------------------------------------------------------------------------- |
|
|
662
|
+
| `url` | required | _Connection._ The URL of the sandbox service |
|
|
663
|
+
| `auth` | `'gcloud'` | _Connection._ `'gcloud'`, `'metadata'`, `'none'`, or a function returning an identity token |
|
|
664
|
+
| `ports` | `[]` | _Connection._ Ports the harness may reach, through the service |
|
|
665
|
+
| `allowedHosts` | `[]` | _Connection._ Hosts the sandbox may reach over HTTPS, on top of the service's |
|
|
666
|
+
| `baseUrls` | `{}` | _Connection._ Base URL variables routed through the proxy, on top of the service's |
|
|
667
|
+
| `nodeUseEnvProxy` | the service's | _Connection._ `true` sets `NODE_USE_ENV_PROXY=1` for the commands: see [Node.js and the proxy](#nodejs-and-the-proxy) |
|
|
668
|
+
| `serviceToken` | none | _Connection._ The service's `SANDBOX_SERVICE_TOKEN`, when it has one |
|
|
669
|
+
| `sandboxId` | `ai-sdk-<hex>` | The sandbox's name: lowercase letters, digits and dashes, 55 at most. Creation fails if it is taken |
|
|
670
|
+
| `template` | none | `await agent.getSandboxTemplate()`: prepare once, boot every later sandbox from it |
|
|
671
|
+
| `image` | `default` | The image the machine boots from, by the name the service gives it in `SANDBOX_IMAGES` |
|
|
672
|
+
| `labels` | `{}` | Labels put on the machine and its disk, for a billing export: see [Usage and cost of a sandbox](#usage-and-cost-of-a-sandbox) |
|
|
673
|
+
| `requiredCommands` | `['node', 'git']` | Commands the sandbox must have once `setup` ran |
|
|
674
|
+
| `setup` | `[]` | Commands run once after creation, as the sandbox's user (who has `sudo`), saved in the template |
|
|
675
|
+
| `abortSignal` | none | Aborts the creation or the resume |
|
|
670
676
|
|
|
671
677
|
## Configuration
|
|
672
678
|
|
|
673
679
|
### The service (`serve`)
|
|
674
680
|
|
|
675
|
-
| Variable | Default | Description
|
|
676
|
-
| ---------------------------- | ---------------------------------------------------------------------------------------- |
|
|
677
|
-
| `SANDBOX_ZONE` | required | The zone of the machines
|
|
678
|
-
| `SANDBOX_IMAGE` | required | The sandbox image: a name, `family/<family>`, or `projects/<project>/global/images/…`
|
|
679
|
-
| `SANDBOX_IMAGES` | none | Other images callers may pick by name, as `NAME=image` pairs: `python=family/ai-sdk-sandbox-python`
|
|
680
|
-
| `SANDBOX_AGENT_SECRET` | required | 32 characters at least: the agents' tokens are derived from it. Keep it in Secret Manager
|
|
681
|
-
| `SANDBOX_PROXY_URL` | required | The proxy, as the machines reach it: `http://10.8.16.2:3128`
|
|
682
|
-
| `SANDBOX_PROXY_ADMIN_URL` | required | Its administration API: `http://10.8.16.2:3129`
|
|
683
|
-
| `SANDBOX_PROXY_TOKEN` | required | The proxy's token
|
|
684
|
-
| `SANDBOX_PROJECT` | the service's project | The project of the machines
|
|
685
|
-
| `SANDBOX_NETWORK` | the default network | The machines' VPC network
|
|
686
|
-
| `SANDBOX_SUBNETWORK` | none | Their subnetwork: `regions/<region>/subnetworks/<name>`
|
|
687
|
-
| `SANDBOX_NETWORK_TAGS` | `ai-sdk-sandbox` | Their network tags, which the firewall rules apply to
|
|
688
|
-
| `SANDBOX_MACHINE_TYPE` | `e2-standard-4` | Their machine type, a custom one included: `e2-custom-4-8192`
|
|
689
|
-
| `SANDBOX_DISK_GB` | `20` | The size of their disk
|
|
690
|
-
| `SANDBOX_DISK_TYPE` | `pd-balanced` | Its type
|
|
691
|
-
| `SANDBOX_SPOT` | `false` | `true` for Spot machines: cheaper, stopped when Compute Engine takes them back, resumable after
|
|
692
|
-
| `SANDBOX_SPOT_FALLBACK` | `true` | With Spot, a machine the zone has no Spot capacity for is created or started as a standard one, billed as such; `false` fails instead
|
|
693
|
-
| `SANDBOX_INSTANCE_PREFIX` | `sbx-` | Put before a sandbox's name to make its machine's, 8 characters at most
|
|
694
|
-
| `SANDBOX_AGENT_PORT` | `8100` | The port of the agents
|
|
695
|
-
| `SANDBOX_START_TIMEOUT` | `300` | Seconds a machine has to boot and its agent to answer
|
|
696
|
-
| `SANDBOX_MAX_RUNNING` | none | The most sandboxes running at once: a creation or a resume beyond it is answered with a 429. Counted at each call, so two at the same instant may both pass
|
|
697
|
-
| `SANDBOX_MAX_RUN_HOURS` | none | The longest a machine runs at a time: Compute Engine stops it then (`maxRunDuration`), disk kept, whatever the service or the application do. 30 s at least
|
|
698
|
-
| `SANDBOX_PROXY_INSTANCE` | none | The egress proxy's machine, in the sandboxes' zone: started with the first sandbox that runs, stopped once none does and no prepared machine waits for one. Always on without it
|
|
699
|
-
| `SANDBOX_PROFILE` | `cost` | `cost` or `fast`: the defaults of the two settings below, see [Speed or cost](#speed-or-cost)
|
|
700
|
-
| `SANDBOX_WARM_POOL` | `0`, `1` with `fast` | How many machines are kept booted from each template used in the last 24 h, for the next creations from it to take at once. Each one is billed all the time
|
|
701
|
-
| `SANDBOX_PROXY_ALWAYS_ON` | `false`, `true` with `fast` | `true`: the service starts the proxy's machine of `SANDBOX_PROXY_INSTANCE` with the first sandbox, and never stops it. No sandbox waits for it to boot, and it is billed all the time
|
|
702
|
-
| `SANDBOX_ALLOWED_HOSTS` | none | Hosts every sandbox may reach over HTTPS, comma-separated; `*.domain` allowed. `*`, any host, is logged as a warning at startup
|
|
703
|
-
| `SANDBOX_BASE_URLS` | `ANTHROPIC_BASE_URL=https://api.anthropic.com,OPENAI_BASE_URL=https://api.openai.com/v1` | Base URL variables routed through the proxy, as `NAME=url` pairs
|
|
704
|
-
| `SANDBOX_NODE_USE_ENV_PROXY` | `false` | `true` sets `NODE_USE_ENV_PROXY=1` for the commands of every sandbox that does not say otherwise: see [Node.js and the proxy](#nodejs-and-the-proxy)
|
|
705
|
-
| `SANDBOX_SERVICE_TOKEN` | required on Cloud Run | A secret every call must carry, beside Cloud Run's IAM
|
|
706
|
-
| `SANDBOX_INSECURE_NO_TOKEN` | `false` | `true` to run on Cloud Run without `SANDBOX_SERVICE_TOKEN`, its IAM the only lock
|
|
707
|
-
| `COMPUTE_AUTH` | `metadata` on Cloud Run, `gcloud` elsewhere | How the service gets its access tokens for the Compute Engine API
|
|
708
|
-
| `PORT`, `HOST` | `8080`, `0.0.0.0` on Cloud Run and `127.0.0.1` elsewhere | Where the service listens
|
|
709
|
-
| `LOG_FORMAT` | `json` on Cloud Run, `text` elsewhere | JSON lines for Cloud Logging, or plain text
|
|
681
|
+
| Variable | Default | Description |
|
|
682
|
+
| ---------------------------- | ---------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
683
|
+
| `SANDBOX_ZONE` | required | The zone of the machines |
|
|
684
|
+
| `SANDBOX_IMAGE` | required | The sandbox image: a name, `family/<family>`, or `projects/<project>/global/images/…` |
|
|
685
|
+
| `SANDBOX_IMAGES` | none | Other images callers may pick by name, as `NAME=image` pairs: `python=family/ai-sdk-sandbox-python` |
|
|
686
|
+
| `SANDBOX_AGENT_SECRET` | required | 32 characters at least: the agents' tokens are derived from it. Keep it in Secret Manager |
|
|
687
|
+
| `SANDBOX_PROXY_URL` | required | The proxy, as the machines reach it: `http://10.8.16.2:3128` |
|
|
688
|
+
| `SANDBOX_PROXY_ADMIN_URL` | required | Its administration API: `http://10.8.16.2:3129` |
|
|
689
|
+
| `SANDBOX_PROXY_TOKEN` | required | The proxy's token |
|
|
690
|
+
| `SANDBOX_PROJECT` | the service's project | The project of the machines |
|
|
691
|
+
| `SANDBOX_NETWORK` | the default network | The machines' VPC network |
|
|
692
|
+
| `SANDBOX_SUBNETWORK` | none | Their subnetwork: `regions/<region>/subnetworks/<name>` |
|
|
693
|
+
| `SANDBOX_NETWORK_TAGS` | `ai-sdk-sandbox` | Their network tags, which the firewall rules apply to |
|
|
694
|
+
| `SANDBOX_MACHINE_TYPE` | `e2-standard-4` | Their machine type, a custom one included: `e2-custom-4-8192` |
|
|
695
|
+
| `SANDBOX_DISK_GB` | `20` | The size of their disk |
|
|
696
|
+
| `SANDBOX_DISK_TYPE` | `pd-balanced` | Its type |
|
|
697
|
+
| `SANDBOX_SPOT` | `false` | `true` for Spot machines: cheaper, stopped when Compute Engine takes them back, resumable after |
|
|
698
|
+
| `SANDBOX_SPOT_FALLBACK` | `true` | With Spot, a machine the zone has no Spot capacity for is created or started as a standard one, billed as such; `false` fails instead |
|
|
699
|
+
| `SANDBOX_INSTANCE_PREFIX` | `sbx-` | Put before a sandbox's name to make its machine's, 8 characters at most |
|
|
700
|
+
| `SANDBOX_AGENT_PORT` | `8100` | The port of the agents |
|
|
701
|
+
| `SANDBOX_START_TIMEOUT` | `300` | Seconds a machine has to boot and its agent to answer |
|
|
702
|
+
| `SANDBOX_MAX_RUNNING` | none | The most sandboxes running at once: a creation or a resume beyond it is answered with a 429 (`GceSandboxCapacityError`, with `Retry-After`). Counted at each call, so two at the same instant may both pass |
|
|
703
|
+
| `SANDBOX_MAX_RUN_HOURS` | none | The longest a machine runs at a time: Compute Engine stops it then (`maxRunDuration`), disk kept, whatever the service or the application do. 30 s at least |
|
|
704
|
+
| `SANDBOX_PROXY_INSTANCE` | none | The egress proxy's machine, in the sandboxes' zone: started with the first sandbox that runs, stopped once none does and no prepared machine waits for one. Always on without it |
|
|
705
|
+
| `SANDBOX_PROFILE` | `cost` | `cost` or `fast`: the defaults of the two settings below, see [Speed or cost](#speed-or-cost) |
|
|
706
|
+
| `SANDBOX_WARM_POOL` | `0`, `1` with `fast` | How many machines are kept booted from each template used in the last 24 h, for the next creations from it to take at once. Each one is billed all the time |
|
|
707
|
+
| `SANDBOX_PROXY_ALWAYS_ON` | `false`, `true` with `fast` | `true`: the service starts the proxy's machine of `SANDBOX_PROXY_INSTANCE` with the first sandbox, and never stops it. No sandbox waits for it to boot, and it is billed all the time |
|
|
708
|
+
| `SANDBOX_ALLOWED_HOSTS` | none | Hosts every sandbox may reach over HTTPS, comma-separated; `*.domain` allowed. `*`, any host, is logged as a warning at startup |
|
|
709
|
+
| `SANDBOX_BASE_URLS` | `ANTHROPIC_BASE_URL=https://api.anthropic.com,OPENAI_BASE_URL=https://api.openai.com/v1` | Base URL variables routed through the proxy, as `NAME=url` pairs |
|
|
710
|
+
| `SANDBOX_NODE_USE_ENV_PROXY` | `false` | `true` sets `NODE_USE_ENV_PROXY=1` for the commands of every sandbox that does not say otherwise: see [Node.js and the proxy](#nodejs-and-the-proxy) |
|
|
711
|
+
| `SANDBOX_SERVICE_TOKEN` | required on Cloud Run | A secret every call must carry, beside Cloud Run's IAM |
|
|
712
|
+
| `SANDBOX_INSECURE_NO_TOKEN` | `false` | `true` to run on Cloud Run without `SANDBOX_SERVICE_TOKEN`, its IAM the only lock |
|
|
713
|
+
| `COMPUTE_AUTH` | `metadata` on Cloud Run, `gcloud` elsewhere | How the service gets its access tokens for the Compute Engine API |
|
|
714
|
+
| `PORT`, `HOST` | `8080`, `0.0.0.0` on Cloud Run and `127.0.0.1` elsewhere | Where the service listens |
|
|
715
|
+
| `LOG_FORMAT` | `json` on Cloud Run, `text` elsewhere | JSON lines for Cloud Logging, or plain text |
|
|
710
716
|
|
|
711
717
|
### The proxy (`proxy`)
|
|
712
718
|
|
|
@@ -731,8 +737,9 @@ The MCP servers that [`ai-sdk-harness-plugins`](https://ai-sdk-harness.pages.dev
|
|
|
731
737
|
| `SANDBOX_PATH` | `/usr/local/sbin:/usr/local/bin:/usr/sbin:…` | Their `PATH` |
|
|
732
738
|
| `SANDBOX_AGENT_PORT` | `8100` | Where the agent listens |
|
|
733
739
|
| `SANDBOX_AGENT_TOKEN` | the instance's `ai-sdk-sandbox-agent-token` | Its token: the service puts it in the instance metadata |
|
|
734
|
-
| `SANDBOX_STATE_DIR` | `/var/lib/ai-sdk-sandbox` | Where it keeps the network configuration, root's
|
|
740
|
+
| `SANDBOX_STATE_DIR` | `/var/lib/ai-sdk-sandbox` | Where it keeps the network configuration and the samples of the machine, root's |
|
|
735
741
|
| `SANDBOX_CONFIGURE_DOCKER` | `true` | `false` to leave Docker's proxy settings alone |
|
|
742
|
+
| `SANDBOX_METRICS_INTERVAL` | `30` | Seconds between two samples of the machine; `0` for none but those a stop or a deletion takes |
|
|
736
743
|
| `SANDBOX_RUN_DIR` | `/run/ai-sdk-sandbox` | Where it marks it was configured since the machine booted, emptied at each boot: an agent restarted then never powers a machine off |
|
|
737
744
|
|
|
738
745
|
A sandbox's commands run with nothing of the agent's environment: only their `PATH`, `HOME`, `USER`, the proxy and base URL variables, and the variables the command sets.
|
|
@@ -741,10 +748,150 @@ A sandbox's commands run with nothing of the agent's environment: only their `PA
|
|
|
741
748
|
|
|
742
749
|
- `GceSandboxImageError`: the image of a new sandbox lacks a command of `requiredCommands`. Carries `sandboxId` and `missing`.
|
|
743
750
|
- `GceSandboxNotFoundError`: `resumeGceNetworkSandboxSession()` found no sandbox of that name, running or stopped. Carries `sandboxId`.
|
|
751
|
+
- `GceSandboxCapacityError`: the service runs as many sandboxes as `SANDBOX_MAX_RUNNING` allows, and refused to create or resume one more. Carries `running`, `max` and `retryAfterSeconds` (also the answer's `Retry-After`): queue the call and try again after it, or stop a sandbox. A `GceSandboxServiceError` with `status` 429, so code that checks the status goes on working. The count is made at each call: two calls at the same instant may both pass, and run one sandbox more than the most.
|
|
744
752
|
- `GceSandboxServiceError`: the service answered with an error status: 409 for a name already taken, 400 for an image it does not offer or a command carrying a credential, 504 for a machine whose agent did not answer in time. Carries `request` and `status`.
|
|
745
753
|
- `HarnessSandboxAuthenticationError` (from `@ai-sdk/harness`): Cloud Run refused the call (401 or 403), or the service token is missing or wrong.
|
|
746
754
|
- `HarnessCapabilityUnsupportedError` (from `@ai-sdk/harness`): a port that is not exposed, or a network policy with CIDR rules.
|
|
747
755
|
|
|
756
|
+
## Usage and cost of a sandbox
|
|
757
|
+
|
|
758
|
+
The machine has no service account, and reaches nothing but the egress proxy: what a sandbox costs is read from outside, from the service, by the application.
|
|
759
|
+
|
|
760
|
+
### Labels
|
|
761
|
+
|
|
762
|
+
`labels` puts labels of yours on the sandbox's machine and on its disk, beside the service's own (`ai-sdk-sandbox`, `ai-sdk-sandbox-name`…): a [Cloud Billing export](https://cloud.google.com/billing/docs/how-to/export-data-bigquery) or a budget then groups their cost by them.
|
|
763
|
+
|
|
764
|
+
```ts
|
|
765
|
+
const sandbox = await createGceNetworkSandboxSession({
|
|
766
|
+
url,
|
|
767
|
+
template: await agent.getSandboxTemplate(),
|
|
768
|
+
labels: { app: 'studio', tenant: 'acme', session: 's-42' },
|
|
769
|
+
});
|
|
770
|
+
```
|
|
771
|
+
|
|
772
|
+
Keys start with a lowercase letter, then lowercase letters, digits, `_` or `-`; values take the same characters and may be empty; 63 characters at most each, 56 labels at most. Keys starting with `ai-sdk-` are the package's and are refused. A machine taken from the warm pool gets them as it is taken, and so does its disk, the pool's until then; a template's builder gets those of the creation that builds it. A resume keeps them as they are.
|
|
773
|
+
|
|
774
|
+
### Its machine
|
|
775
|
+
|
|
776
|
+
`describeGceSandbox({ url, sandboxId })`, or `describe()` of a session, reads the sandbox's machine from Compute Engine, running or stopped, without starting it:
|
|
777
|
+
|
|
778
|
+
```ts
|
|
779
|
+
import { describeGceSandbox } from 'ai-sdk-sandbox-gce';
|
|
780
|
+
|
|
781
|
+
const machine = await describeGceSandbox({ url, sandboxId: 'box' });
|
|
782
|
+
// {
|
|
783
|
+
// name: 'box', instance: 'sbx-box', zone: 'europe-west1-b', status: 'TERMINATED',
|
|
784
|
+
// creationTimestamp: '2026-11-25T10:00:00.000-08:00',
|
|
785
|
+
// lastStartTimestamp: '2026-11-25T10:00:20.000-08:00',
|
|
786
|
+
// lastStopTimestamp: '2026-11-25T11:00:00.000-08:00',
|
|
787
|
+
// provisioningModel: 'STANDARD', spotFallback: true,
|
|
788
|
+
// machineType: 'e2-standard-4', diskSizeGb: 20, image: 'default', fromPool: false,
|
|
789
|
+
// labels: { app: 'studio', tenant: 'acme', session: 's-42' },
|
|
790
|
+
// }
|
|
791
|
+
```
|
|
792
|
+
|
|
793
|
+
`provisioningModel` is what the machine is billed as: a service of Spot machines (`SANDBOX_SPOT`) creates or starts a standard one when the zone has no Spot capacity left (`SANDBOX_SPOT_FALLBACK`), and `spotFallback` says so. The machine of a sandbox taken from the warm pool (`fromPool`) was made before the sandbox took it: `creationTimestamp` is the pool's. `labels` leaves the package's own out. It throws `GceSandboxNotFoundError` once the sandbox is deleted: Compute Engine deletes the machine some 50 s after the deletion returns, and it is answered `STOPPING` meanwhile, as a stopped one is until it is `TERMINATED`, its `lastStopTimestamp` set then. A service of an earlier version answers 405.
|
|
794
|
+
|
|
795
|
+
### How long it took
|
|
796
|
+
|
|
797
|
+
`timings` of the session returned by `createGceNetworkSandboxSession()` or `resumeGceNetworkSandboxSession()` says how long the call took on the service, in milliseconds, step by step:
|
|
798
|
+
|
|
799
|
+
```ts
|
|
800
|
+
const sandbox = await createGceNetworkSandboxSession({ url, template });
|
|
801
|
+
console.log(sandbox.timings);
|
|
802
|
+
// { fromPool: true, proxyMs: 3, insertMs: 412, bootMs: 1210, agentMs: 95, totalMs: 1890 }
|
|
803
|
+
```
|
|
804
|
+
|
|
805
|
+
| Field | What it measures |
|
|
806
|
+
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------- |
|
|
807
|
+
| `fromPool` | The creation took a machine of the warm pool, booted ahead of it. Always `false` on a resume |
|
|
808
|
+
| `proxyMs` | Waiting for the egress proxy, booting when the service had stopped it (`SANDBOX_PROXY_INSTANCE`). It overlaps the steps below |
|
|
809
|
+
| `insertMs` | Compute Engine creating the machine, or the warm pool handing one over; on a resume, starting the stopped machine, or waiting for one |
|
|
810
|
+
| `bootMs` | The machine running and its guest booting, until its agent answers, while the proxy is waited for |
|
|
811
|
+
| `agentMs` | Registering the sandbox with the proxy and configuring its agent, the waits for either left out |
|
|
812
|
+
| `totalMs` | The whole call on the service, its checks included |
|
|
813
|
+
|
|
814
|
+
A template built by the creation is not counted: the creation of its builder, its setup and the image take minutes, once. A service of an earlier version answers no `timings`.
|
|
815
|
+
|
|
816
|
+
### What its connections carried
|
|
817
|
+
|
|
818
|
+
The egress proxy counts the bytes each sandbox's connections carry, outside the machine, where nothing the sandbox runs changes them: `bytesRead`, what the proxy read from the sandbox (its requests, its uploads, the TLS it sent through its tunnels), and `bytesWritten`, what it wrote to it (answers, downloads). `usage()` of a session, or `getGceSandboxUsage({ url, sandboxId })`, reads them while the sandbox runs:
|
|
819
|
+
|
|
820
|
+
```ts
|
|
821
|
+
import { getGceSandboxUsage } from 'ai-sdk-sandbox-gce';
|
|
822
|
+
|
|
823
|
+
const usage = await getGceSandboxUsage({ url, sandboxId: 'box' });
|
|
824
|
+
// { bytesRead: 48211, bytesWritten: 9134877, connections: 12, openConnections: 2,
|
|
825
|
+
// since: '2026-11-25T18:00:21.120Z' }
|
|
826
|
+
```
|
|
827
|
+
|
|
828
|
+
The count starts when the proxy registers the sandbox, at its creation or its resume from a stop, and covers the connections still open as they stand. The proxy keeps it in memory only: it forgets it with the sandbox, at a stop or a deletion, and loses it when it restarts. So **a stop and a deletion answer the last count**, read just before the proxy forgets the sandbox, in a report:
|
|
829
|
+
|
|
830
|
+
```ts
|
|
831
|
+
const sandbox = await createGceNetworkSandboxSession({
|
|
832
|
+
url,
|
|
833
|
+
// Each time a view of the sandbox stops or destroys it, ai-sdk-harness-sessions included.
|
|
834
|
+
onUsageReport: async (report) => {
|
|
835
|
+
// { name: 'box', event: 'suspend', at: '2026-11-25T19:02:00.000Z', egress: { … } }
|
|
836
|
+
await saveUsage(report);
|
|
837
|
+
},
|
|
838
|
+
});
|
|
839
|
+
await sandbox.stop();
|
|
840
|
+
console.log(sandbox.lastReport);
|
|
841
|
+
|
|
842
|
+
// Deleting a sandbox by its id reports too.
|
|
843
|
+
await deleteGceSandbox({ url, sandboxId: 'box', onUsageReport: saveUsage });
|
|
844
|
+
```
|
|
845
|
+
|
|
846
|
+
`onUsageReport` is awaited, an error it throws ignored. `lastReport` holds the report of the last `stop()` or `destroy()` of that view. Reading the count of a stopped sandbox fails with `GceSandboxServiceError` (404): its stop answered it. A proxy of an earlier version counts nothing: its reports have no `egress`, and `usage()` fails with a 404.
|
|
847
|
+
|
|
848
|
+
### What its machine went through
|
|
849
|
+
|
|
850
|
+
The agent of each machine samples it every 30 s, from `/proc` alone: the share of the CPUs busy (`cpuPercent`) and stolen by the hypervisor (`cpuStealPercent`) since the sample before, the memory in use (`memoryUsedBytes` of `memoryTotalBytes`), the pressure on both when the kernel reports it (`cpuPressure`, `memoryPressure`, from `/proc/pressure`), and the processes the kernel killed for want of memory since the boot (`oomKills`). It keeps the last day of samples in its state directory (`/var/lib/ai-sdk-sandbox/metrics.jsonl`), on the machine's disk: they outlive a stop. A machine made from a template does not inherit the samples of the machine that built it: the agent keeps only those of its own machine, by the id of the instance. `metrics()` of a session, or `getGceSandboxMetrics({ url, sandboxId, since })`, reads them while the sandbox runs, those after `since` when given:
|
|
851
|
+
|
|
852
|
+
```ts
|
|
853
|
+
import { getGceSandboxMetrics } from 'ai-sdk-sandbox-gce';
|
|
854
|
+
|
|
855
|
+
let since: string | undefined;
|
|
856
|
+
const { intervalSeconds, samples } = await getGceSandboxMetrics({ url, sandboxId: 'box', since });
|
|
857
|
+
// samples: [{ at: '2026-11-25T18:01:00.000Z', cpuPercent: 37.5, cpuStealPercent: 0, cpuCount: 4,
|
|
858
|
+
// memoryUsedBytes: 2147483648, memoryTotalBytes: 16777216000,
|
|
859
|
+
// cpuPressure: { someAvg10: 2.1, someAvg60: 1.3, fullAvg10: 0, fullAvg60: 0 },
|
|
860
|
+
// memoryPressure: { someAvg10: 0, someAvg60: 0, fullAvg10: 0, fullAvg60: 0 }, oomKills: 0 }, …]
|
|
861
|
+
since = samples.at(-1)?.at ?? since;
|
|
862
|
+
```
|
|
863
|
+
|
|
864
|
+
A stop and a deletion read the last ones before the machine goes, a sample taken then, those since the machine last started: the report handed to `onUsageReport` holds them in `metrics`, beside `egress`. The agent runs in the machine, where the sandbox's user has `sudo`: what it reports is fit for sizing the machines and `SANDBOX_MAX_RUNNING`, never for billing, which [the labels](#labels) and the proxy's count are for. An image built with an earlier version of the package samples nothing: `metrics()` fails with a 501, and the reports have no `metrics`. Build the image again to get them.
|
|
865
|
+
|
|
866
|
+
### Its logs
|
|
867
|
+
|
|
868
|
+
The service logs each creation, resume, suspension and deletion as a JSON line (`LOG_FORMAT=json`, the default on Cloud Run), which Cloud Logging reads as a structured entry, for a log-based metric or a BigQuery sink:
|
|
869
|
+
|
|
870
|
+
```json
|
|
871
|
+
{
|
|
872
|
+
"event": "create",
|
|
873
|
+
"sandbox": "box",
|
|
874
|
+
"at": "2026-11-25T18:00:21.120Z",
|
|
875
|
+
"instance": "sbx-pool-1a2b3c4d",
|
|
876
|
+
"image": "default",
|
|
877
|
+
"template": "ai-sdk-harness-template-…",
|
|
878
|
+
"labels": { "app": "studio" },
|
|
879
|
+
"timings": {
|
|
880
|
+
"fromPool": true,
|
|
881
|
+
"proxyMs": 3,
|
|
882
|
+
"insertMs": 412,
|
|
883
|
+
"bootMs": 1210,
|
|
884
|
+
"agentMs": 95,
|
|
885
|
+
"totalMs": 1890
|
|
886
|
+
},
|
|
887
|
+
"severity": "INFO",
|
|
888
|
+
"message": "box created in 1890 ms on sbx-pool-1a2b3c4d of the warm pool",
|
|
889
|
+
"time": "2026-11-25T18:00:21.120Z"
|
|
890
|
+
}
|
|
891
|
+
```
|
|
892
|
+
|
|
893
|
+
`resume` carries `instance` and `timings`; `suspend` and `delete` carry `instance`, its `status`, `egress` (the proxy's last count) and `samples` (how many samples the report holds). With `LOG_FORMAT=text`, only the message is written. A Spot machine Compute Engine took back is in its audit logs (`compute.instances.preempted`), not in the service's.
|
|
894
|
+
|
|
748
895
|
## Cost
|
|
749
896
|
|
|
750
897
|
A machine is billed by the second while it runs, and only for its disk once stopped: in `europe-west1`, an `e2-standard-4` is in the order of $0.15 an hour, Spot much less, and a stopped 20 GB balanced disk about $2 a month. The proxy machine, an `e2-small`, is about $15 a month when always on, its external address a few dollars more; with `SANDBOX_PROXY_INSTANCE`, both are billed only while a sandbox runs. The service scales to zero. Check the current [Compute Engine pricing](https://cloud.google.com/compute/vm-instance-pricing).
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { GceSandboxServiceError } from './gce-sandbox-service-error.js';
|
|
2
|
+
/**
|
|
3
|
+
* The service runs as many sandboxes as its `SANDBOX_MAX_RUNNING` allows, and refused to create or
|
|
4
|
+
* resume one more: answered with a 429, its `status`. Wait `retryAfterSeconds`, or stop a sandbox,
|
|
5
|
+
* and try again: queue the call rather than fail it. The count is made at each call, so two calls
|
|
6
|
+
* at once may both pass, and run one sandbox more than the most.
|
|
7
|
+
*/
|
|
8
|
+
export declare class GceSandboxCapacityError extends GceSandboxServiceError {
|
|
9
|
+
/** The sandboxes running, or booting, when the call was refused. */
|
|
10
|
+
readonly running: number;
|
|
11
|
+
/** The most the service runs at once. */
|
|
12
|
+
readonly max: number;
|
|
13
|
+
/** How long the service says to wait before trying again, in seconds. */
|
|
14
|
+
readonly retryAfterSeconds: number;
|
|
15
|
+
constructor(request: string, message: string, { running, max, retryAfterSeconds, }: {
|
|
16
|
+
running: number;
|
|
17
|
+
max: number;
|
|
18
|
+
retryAfterSeconds: number;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { GceSandboxServiceError } from './gce-sandbox-service-error.js';
|
|
2
|
+
/**
|
|
3
|
+
* The service runs as many sandboxes as its `SANDBOX_MAX_RUNNING` allows, and refused to create or
|
|
4
|
+
* resume one more: answered with a 429, its `status`. Wait `retryAfterSeconds`, or stop a sandbox,
|
|
5
|
+
* and try again: queue the call rather than fail it. The count is made at each call, so two calls
|
|
6
|
+
* at once may both pass, and run one sandbox more than the most.
|
|
7
|
+
*/
|
|
8
|
+
export class GceSandboxCapacityError extends GceSandboxServiceError {
|
|
9
|
+
/** The sandboxes running, or booting, when the call was refused. */
|
|
10
|
+
running;
|
|
11
|
+
/** The most the service runs at once. */
|
|
12
|
+
max;
|
|
13
|
+
/** How long the service says to wait before trying again, in seconds. */
|
|
14
|
+
retryAfterSeconds;
|
|
15
|
+
constructor(request, message, { running, max, retryAfterSeconds, }) {
|
|
16
|
+
super(request, 429, message);
|
|
17
|
+
this.name = 'GceSandboxCapacityError';
|
|
18
|
+
this.running = running;
|
|
19
|
+
this.max = max;
|
|
20
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { HarnessV1SandboxSessionCreateOptions, HarnessV1SandboxSessionResumeOptions, HarnessV1SandboxTemplate } from '@ai-sdk/harness';
|
|
2
|
+
import type { GceSandboxReport } from './gce-usage.js';
|
|
2
3
|
/**
|
|
3
4
|
* How calls to the sandbox service are authenticated. The service runs on Cloud Run, deployed with
|
|
4
5
|
* `--no-allow-unauthenticated`: each call carries a Google identity token, granted
|
|
@@ -69,6 +70,14 @@ export interface GceConnectionSettings {
|
|
|
69
70
|
* @defaultValue the service's `SANDBOX_NODE_USE_ENV_PROXY`, `false` by default
|
|
70
71
|
*/
|
|
71
72
|
nodeUseEnvProxy?: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Called with the last measures of the sandbox each time this session, or a view of it, stops or
|
|
75
|
+
* destroys it, and by `deleteGceSandbox()`: what its connections carried through the egress
|
|
76
|
+
* proxy, and what the agent of its machine sampled, which the proxy and the disk lose then. What
|
|
77
|
+
* an application that leaves the stops to `ai-sdk-harness-sessions` keeps them with. Awaited; an
|
|
78
|
+
* error it throws is ignored, the stop done all the same.
|
|
79
|
+
*/
|
|
80
|
+
onUsageReport?: (report: GceSandboxReport) => Promise<void> | void;
|
|
72
81
|
}
|
|
73
82
|
/** What a new sandbox is made of. */
|
|
74
83
|
export interface GceCreationSettings extends GceConnectionSettings {
|
|
@@ -80,6 +89,17 @@ export interface GceCreationSettings extends GceConnectionSettings {
|
|
|
80
89
|
* @defaultValue the service's `SANDBOX_IMAGE`
|
|
81
90
|
*/
|
|
82
91
|
image?: string;
|
|
92
|
+
/**
|
|
93
|
+
* Labels put on the sandbox's machine and on its disk, beside the service's own: what a Cloud
|
|
94
|
+
* Billing export, or a budget, groups their cost by. Keys start with a lowercase letter, then
|
|
95
|
+
* lowercase letters, digits, `_` or `-`, values the same but may be empty, 63 characters at most
|
|
96
|
+
* each, 56 labels at most. Keys starting with `ai-sdk-` are the package's: refused. A machine
|
|
97
|
+
* taken from the warm pool gets them as it is taken, its disk too. A template's builder gets those
|
|
98
|
+
* of the creation that builds it.
|
|
99
|
+
*
|
|
100
|
+
* @defaultValue `{}`
|
|
101
|
+
*/
|
|
102
|
+
labels?: Readonly<Record<string, string>>;
|
|
83
103
|
/**
|
|
84
104
|
* Commands the sandbox must have once `setup` ran, which may install them: a harness needs Node.js for
|
|
85
105
|
* its bridge and git for the repositories, whatever the project's language. A missing one fails
|
|
@@ -140,6 +160,28 @@ export interface GceTemplateDeletionSettings extends GceConnectionSettings {
|
|
|
140
160
|
/** Aborts the call. */
|
|
141
161
|
abortSignal?: AbortSignal;
|
|
142
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Options of `describeGceSandbox()` and of the other calls about one sandbox: the sandbox, and the
|
|
165
|
+
* service it was created through.
|
|
166
|
+
*/
|
|
167
|
+
export interface GceSandboxLookupSettings extends GceConnectionSettings {
|
|
168
|
+
/** The id the sandbox was created with. */
|
|
169
|
+
sandboxId: string;
|
|
170
|
+
/** Aborts the call. */
|
|
171
|
+
abortSignal?: AbortSignal;
|
|
172
|
+
}
|
|
173
|
+
/** Options of `getGceSandboxMetrics()`: the sandbox, and since when. */
|
|
174
|
+
export interface GceSandboxMetricsSettings extends GceSandboxLookupSettings {
|
|
175
|
+
/**
|
|
176
|
+
* Only the samples taken after this time: a `Date`, an ISO 8601 timestamp, or milliseconds since
|
|
177
|
+
* the epoch. The `at` of the last sample read, to read the next ones.
|
|
178
|
+
*
|
|
179
|
+
* @defaultValue every sample the agent keeps, the last day of them
|
|
180
|
+
*/
|
|
181
|
+
since?: Date | number | string;
|
|
182
|
+
}
|
|
183
|
+
/** Options of `deleteGceSandbox()`: the sandbox, and the service it was created through. */
|
|
184
|
+
export type GceSandboxDeletionSettings = GceSandboxLookupSettings;
|
|
143
185
|
/** Options of `createGceNetworkSandboxSession()`. */
|
|
144
186
|
export type GceNetworkSandboxSessionCreateOptions = HarnessV1SandboxSessionCreateOptions<GceCreationSettings>;
|
|
145
187
|
/** Options of `resumeGceNetworkSandboxSession()`. */
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { MetricSample, SandboxEgressUsage, SandboxInfo, SandboxMetrics, SandboxReport, SandboxTimings } from '../protocol/api.js';
|
|
2
|
+
/**
|
|
3
|
+
* A sandbox's machine, as Compute Engine describes it: when it ran, how it is billed (Spot or
|
|
4
|
+
* standard), what it is made of, and the labels it holds. What `describeGceSandbox()` and
|
|
5
|
+
* `describe()` of a session answer, running or stopped.
|
|
6
|
+
*/
|
|
7
|
+
export type GceSandboxInfo = SandboxInfo;
|
|
8
|
+
/**
|
|
9
|
+
* How long the creation or the resume of a sandbox took on the service, in milliseconds, step by
|
|
10
|
+
* step: the egress proxy, Compute Engine making or starting the machine (or taking one of the warm
|
|
11
|
+
* pool), its boot until the agent answered, then the agent's configuration. `timings` of a session.
|
|
12
|
+
*/
|
|
13
|
+
export type GceSandboxTimings = SandboxTimings;
|
|
14
|
+
/**
|
|
15
|
+
* The bytes a sandbox's connections carried through the egress proxy since its creation or its last
|
|
16
|
+
* resume from a stop, counted outside the machine: `usage()` of a session and `getGceSandboxUsage()`
|
|
17
|
+
* read it while the sandbox runs. The proxy keeps it in memory only, and forgets it with the
|
|
18
|
+
* sandbox: a suspension or a deletion answers the last one, in a {@link GceSandboxReport}.
|
|
19
|
+
*/
|
|
20
|
+
export type GceSandboxEgressUsage = SandboxEgressUsage;
|
|
21
|
+
/**
|
|
22
|
+
* The last measures of a sandbox, read by the service as it suspends or deletes it, before they are
|
|
23
|
+
* lost: what `onUsageReport` is handed when a session stops or destroys it or `deleteGceSandbox()`
|
|
24
|
+
* deletes it, and what `lastReport` of the session holds then.
|
|
25
|
+
*/
|
|
26
|
+
export type GceSandboxReport = SandboxReport;
|
|
27
|
+
/**
|
|
28
|
+
* What the agent of a sandbox's machine read of it at one instant, from `/proc`: CPU, memory, their
|
|
29
|
+
* pressure, the processes killed for want of memory. Read in the machine, where the sandbox's user
|
|
30
|
+
* has `sudo`: fit for sizing the machines, never for billing.
|
|
31
|
+
*/
|
|
32
|
+
export type GceSandboxMetricSample = MetricSample;
|
|
33
|
+
/**
|
|
34
|
+
* The samples of a sandbox's machine, oldest first, taken every `intervalSeconds`: what `metrics()`
|
|
35
|
+
* of a session and `getGceSandboxMetrics()` answer, and the `metrics` of a {@link GceSandboxReport}.
|
|
36
|
+
*/
|
|
37
|
+
export type GceSandboxMetrics = SandboxMetrics;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|