ai-sdk-sandbox-gce 0.4.2 → 0.5.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 CHANGED
@@ -1,5 +1,13 @@
1
1
  # ai-sdk-sandbox-gce
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - ff30489: The commands of a sandbox no longer get `NODE_USE_ENV_PROXY=1` by default. It sent Node.js's own `fetch` and `http` to the egress proxy, where a test suite that mocks `fetch` with MSW 3 no longer saw its requests: they reached the proxy, which refused them (`fetch failed`, `Failed to establish tunnel … 403`). Package managers, git, curl and the harnesses still leave through the proxy, as they read `HTTPS_PROXY` themselves. The agent sets the variables: build the sandbox image again on this version for the change to take effect.
8
+
9
+ BREAKING: Node.js code in a sandbox that calls an outside host with its own `fetch` or `http`, rather than through a base URL, and has no proxy support of its own, can no longer reach it. Set the variable again with `nodeUseEnvProxy: true` on the sandbox (at its creation and at each resume), or `SANDBOX_NODE_USE_ENV_PROXY=true` on the service for every sandbox; for one command, `NODE_USE_ENV_PROXY=1 <command>` or `node --use-env-proxy`.
10
+
3
11
  ## 0.4.2
4
12
 
5
13
  ### 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.2
138
+ VERSION=0.5.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")"
@@ -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.2 pnpm@10
285
+ npm install --global ai-sdk-sandbox-gce@0.5.0 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.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.
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.5.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.
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.2 \
351
+ gcloud run deploy sandboxes --region=$REGION --image=docker.io/fpasquet/ai-sdk-sandbox-gce:0.5.0 \
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 \
@@ -602,6 +602,18 @@ The machine has no external address, and the VPC firewall, applied outside the g
602
602
 
603
603
  Docker pulls images from Docker Hub through the proxy: allow `registry-1.docker.io`, `auth.docker.io`, `production.cloudflare.docker.com` and `production.cloudfront.docker.com` (Docker Hub serves layers from either), or [`ai-sdk-harness-network`](https://ai-sdk-harness.pages.dev/docs/packages/harness-network)'s presets.
604
604
 
605
+ ### Node.js and the proxy
606
+
607
+ Package managers, git, curl and the harnesses (Claude Code, Codex) read `HTTPS_PROXY` themselves. Node.js's own `fetch` and `http` only do with `NODE_USE_ENV_PROXY=1`, which the sandbox does not set by default: with it, a test suite that mocks `fetch` with [MSW](https://mswjs.io) 3 sees its requests leave for the proxy, which refuses them (`fetch failed`, `Failed to establish tunnel … 403`), MSW's `onUnhandledRequest` saying nothing. MSW 3 catches a request at its socket, and Node.js then opens that socket to the proxy, with a `CONNECT`, rather than to the host the suite mocks.
608
+
609
+ Without it, code that calls an outside host with Node.js's `fetch` and has no proxy support of its own cannot reach it. Turn it on where it is needed:
610
+
611
+ - for a sandbox: `nodeUseEnvProxy: true`, at its creation and at each resume;
612
+ - for every sandbox of the service: `SANDBOX_NODE_USE_ENV_PROXY=true`, which a sandbox's `nodeUseEnvProxy: false` turns off;
613
+ - for one command: `NODE_USE_ENV_PROXY=1 node script.mjs`, or `node --use-env-proxy script.mjs`. In a sandbox that sets it, `NODE_USE_ENV_PROXY= npm test` runs a test suite without it.
614
+
615
+ The MCP servers that [`ai-sdk-harness-plugins`](https://ai-sdk-harness.pages.dev/docs/packages/harness-plugins) runs in the sandbox get it on their own. The agent sets the variables: a sandbox image built on an earlier version of this package still gives every command `NODE_USE_ENV_PROXY=1`, until it is built again.
616
+
605
617
  ## Ports
606
618
 
607
619
  `ports` lists the ports inside the sandbox the harness may reach. `getPortEndpoint()` returns a WebSocket URL on the service, `wss://<service>/v1/sandboxes/<name>/ports/<port>`, with the identity token as a header, read anew at each connection. The service passes the connection on to the machine's agent, with the agent's token in place of the caller's, and the agent on to `127.0.0.1:<port>`. A port outside the list is refused with `HarnessCapabilityUnsupportedError`.
@@ -624,59 +636,61 @@ Docker pulls images from Docker Hub through the proxy: allow `registry-1.docker.
624
636
 
625
637
  `createGceNetworkSandboxSession(options)` takes every option below; `resumeGceNetworkSandboxSession(options)` takes `sandboxId`, `abortSignal` and the connection settings.
626
638
 
627
- | Option | Default | Description |
628
- | ------------------ | ----------------- | --------------------------------------------------------------------------------------------------- |
629
- | `url` | required | _Connection._ The URL of the sandbox service |
630
- | `auth` | `'gcloud'` | _Connection._ `'gcloud'`, `'metadata'`, `'none'`, or a function returning an identity token |
631
- | `ports` | `[]` | _Connection._ Ports the harness may reach, through the service |
632
- | `allowedHosts` | `[]` | _Connection._ Hosts the sandbox may reach over HTTPS, on top of the service's |
633
- | `baseUrls` | `{}` | _Connection._ Base URL variables routed through the proxy, on top of the service's |
634
- | `serviceToken` | none | _Connection._ The service's `SANDBOX_SERVICE_TOKEN`, when it has one |
635
- | `sandboxId` | `ai-sdk-<hex>` | The sandbox's name: lowercase letters, digits and dashes, 55 at most. Creation fails if it is taken |
636
- | `template` | none | `await agent.getSandboxTemplate()`: prepare once, boot every later sandbox from it |
637
- | `image` | `default` | The image the machine boots from, by the name the service gives it in `SANDBOX_IMAGES` |
638
- | `requiredCommands` | `['node', 'git']` | Commands the sandbox must have once `setup` ran |
639
- | `setup` | `[]` | Commands run once after creation, as the sandbox's user (who has `sudo`), saved in the template |
640
- | `abortSignal` | none | Aborts the creation or the resume |
639
+ | Option | Default | Description |
640
+ | ------------------ | ----------------- | --------------------------------------------------------------------------------------------------------------------- |
641
+ | `url` | required | _Connection._ The URL of the sandbox service |
642
+ | `auth` | `'gcloud'` | _Connection._ `'gcloud'`, `'metadata'`, `'none'`, or a function returning an identity token |
643
+ | `ports` | `[]` | _Connection._ Ports the harness may reach, through the service |
644
+ | `allowedHosts` | `[]` | _Connection._ Hosts the sandbox may reach over HTTPS, on top of the service's |
645
+ | `baseUrls` | `{}` | _Connection._ Base URL variables routed through the proxy, on top of the service's |
646
+ | `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) |
647
+ | `serviceToken` | none | _Connection._ The service's `SANDBOX_SERVICE_TOKEN`, when it has one |
648
+ | `sandboxId` | `ai-sdk-<hex>` | The sandbox's name: lowercase letters, digits and dashes, 55 at most. Creation fails if it is taken |
649
+ | `template` | none | `await agent.getSandboxTemplate()`: prepare once, boot every later sandbox from it |
650
+ | `image` | `default` | The image the machine boots from, by the name the service gives it in `SANDBOX_IMAGES` |
651
+ | `requiredCommands` | `['node', 'git']` | Commands the sandbox must have once `setup` ran |
652
+ | `setup` | `[]` | Commands run once after creation, as the sandbox's user (who has `sudo`), saved in the template |
653
+ | `abortSignal` | none | Aborts the creation or the resume |
641
654
 
642
655
  ## Configuration
643
656
 
644
657
  ### The service (`serve`)
645
658
 
646
- | Variable | Default | Description |
647
- | --------------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
648
- | `SANDBOX_ZONE` | required | The zone of the machines |
649
- | `SANDBOX_IMAGE` | required | The sandbox image: a name, `family/<family>`, or `projects/<project>/global/images/…` |
650
- | `SANDBOX_IMAGES` | none | Other images callers may pick by name, as `NAME=image` pairs: `python=family/ai-sdk-sandbox-python` |
651
- | `SANDBOX_AGENT_SECRET` | required | 32 characters at least: the agents' tokens are derived from it. Keep it in Secret Manager |
652
- | `SANDBOX_PROXY_URL` | required | The proxy, as the machines reach it: `http://10.8.16.2:3128` |
653
- | `SANDBOX_PROXY_ADMIN_URL` | required | Its administration API: `http://10.8.16.2:3129` |
654
- | `SANDBOX_PROXY_TOKEN` | required | The proxy's token |
655
- | `SANDBOX_PROJECT` | the service's project | The project of the machines |
656
- | `SANDBOX_NETWORK` | the default network | The machines' VPC network |
657
- | `SANDBOX_SUBNETWORK` | none | Their subnetwork: `regions/<region>/subnetworks/<name>` |
658
- | `SANDBOX_NETWORK_TAGS` | `ai-sdk-sandbox` | Their network tags, which the firewall rules apply to |
659
- | `SANDBOX_MACHINE_TYPE` | `e2-standard-4` | Their machine type, a custom one included: `e2-custom-4-8192` |
660
- | `SANDBOX_DISK_GB` | `20` | The size of their disk |
661
- | `SANDBOX_DISK_TYPE` | `pd-balanced` | Its type |
662
- | `SANDBOX_SPOT` | `false` | `true` for Spot machines: cheaper, stopped when Compute Engine takes them back, resumable after |
663
- | `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 |
664
- | `SANDBOX_INSTANCE_PREFIX` | `sbx-` | Put before a sandbox's name to make its machine's, 8 characters at most |
665
- | `SANDBOX_AGENT_PORT` | `8100` | The port of the agents |
666
- | `SANDBOX_START_TIMEOUT` | `300` | Seconds a machine has to boot and its agent to answer |
667
- | `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 |
668
- | `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 |
669
- | `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 |
670
- | `SANDBOX_PROFILE` | `cost` | `cost` or `fast`: the defaults of the two settings below, see [Speed or cost](#speed-or-cost) |
671
- | `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 |
672
- | `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 |
673
- | `SANDBOX_ALLOWED_HOSTS` | none | Hosts every sandbox may reach over HTTPS, comma-separated; `*.domain` allowed. `*`, any host, is logged as a warning at startup |
674
- | `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 |
675
- | `SANDBOX_SERVICE_TOKEN` | required on Cloud Run | A secret every call must carry, beside Cloud Run's IAM |
676
- | `SANDBOX_INSECURE_NO_TOKEN` | `false` | `true` to run on Cloud Run without `SANDBOX_SERVICE_TOKEN`, its IAM the only lock |
677
- | `COMPUTE_AUTH` | `metadata` on Cloud Run, `gcloud` elsewhere | How the service gets its access tokens for the Compute Engine API |
678
- | `PORT`, `HOST` | `8080`, `0.0.0.0` on Cloud Run and `127.0.0.1` elsewhere | Where the service listens |
679
- | `LOG_FORMAT` | `json` on Cloud Run, `text` elsewhere | JSON lines for Cloud Logging, or plain text |
659
+ | Variable | Default | Description |
660
+ | ---------------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
661
+ | `SANDBOX_ZONE` | required | The zone of the machines |
662
+ | `SANDBOX_IMAGE` | required | The sandbox image: a name, `family/<family>`, or `projects/<project>/global/images/…` |
663
+ | `SANDBOX_IMAGES` | none | Other images callers may pick by name, as `NAME=image` pairs: `python=family/ai-sdk-sandbox-python` |
664
+ | `SANDBOX_AGENT_SECRET` | required | 32 characters at least: the agents' tokens are derived from it. Keep it in Secret Manager |
665
+ | `SANDBOX_PROXY_URL` | required | The proxy, as the machines reach it: `http://10.8.16.2:3128` |
666
+ | `SANDBOX_PROXY_ADMIN_URL` | required | Its administration API: `http://10.8.16.2:3129` |
667
+ | `SANDBOX_PROXY_TOKEN` | required | The proxy's token |
668
+ | `SANDBOX_PROJECT` | the service's project | The project of the machines |
669
+ | `SANDBOX_NETWORK` | the default network | The machines' VPC network |
670
+ | `SANDBOX_SUBNETWORK` | none | Their subnetwork: `regions/<region>/subnetworks/<name>` |
671
+ | `SANDBOX_NETWORK_TAGS` | `ai-sdk-sandbox` | Their network tags, which the firewall rules apply to |
672
+ | `SANDBOX_MACHINE_TYPE` | `e2-standard-4` | Their machine type, a custom one included: `e2-custom-4-8192` |
673
+ | `SANDBOX_DISK_GB` | `20` | The size of their disk |
674
+ | `SANDBOX_DISK_TYPE` | `pd-balanced` | Its type |
675
+ | `SANDBOX_SPOT` | `false` | `true` for Spot machines: cheaper, stopped when Compute Engine takes them back, resumable after |
676
+ | `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 |
677
+ | `SANDBOX_INSTANCE_PREFIX` | `sbx-` | Put before a sandbox's name to make its machine's, 8 characters at most |
678
+ | `SANDBOX_AGENT_PORT` | `8100` | The port of the agents |
679
+ | `SANDBOX_START_TIMEOUT` | `300` | Seconds a machine has to boot and its agent to answer |
680
+ | `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 |
681
+ | `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 |
682
+ | `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 |
683
+ | `SANDBOX_PROFILE` | `cost` | `cost` or `fast`: the defaults of the two settings below, see [Speed or cost](#speed-or-cost) |
684
+ | `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 |
685
+ | `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 |
686
+ | `SANDBOX_ALLOWED_HOSTS` | none | Hosts every sandbox may reach over HTTPS, comma-separated; `*.domain` allowed. `*`, any host, is logged as a warning at startup |
687
+ | `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 |
688
+ | `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) |
689
+ | `SANDBOX_SERVICE_TOKEN` | required on Cloud Run | A secret every call must carry, beside Cloud Run's IAM |
690
+ | `SANDBOX_INSECURE_NO_TOKEN` | `false` | `true` to run on Cloud Run without `SANDBOX_SERVICE_TOKEN`, its IAM the only lock |
691
+ | `COMPUTE_AUTH` | `metadata` on Cloud Run, `gcloud` elsewhere | How the service gets its access tokens for the Compute Engine API |
692
+ | `PORT`, `HOST` | `8080`, `0.0.0.0` on Cloud Run and `127.0.0.1` elsewhere | Where the service listens |
693
+ | `LOG_FORMAT` | `json` on Cloud Run, `text` elsewhere | JSON lines for Cloud Logging, or plain text |
680
694
 
681
695
  ### The proxy (`proxy`)
682
696
 
@@ -59,6 +59,16 @@ export interface GceConnectionSettings {
59
59
  * @defaultValue `{}`
60
60
  */
61
61
  baseUrls?: Readonly<Record<string, string>>;
62
+ /**
63
+ * Sets `NODE_USE_ENV_PROXY=1` for the sandbox's commands: Node.js's own `fetch` and `http` then
64
+ * leave through the egress proxy, as package managers, git and the harnesses already do. Off by
65
+ * default: it breaks the test suites that mock `fetch` by its sockets, such as MSW 3, whose
66
+ * requests then reach the proxy instead of their mocks. Turn it on for code that calls an outside
67
+ * API with Node.js's `fetch` and no proxy support of its own. A command can still set it itself.
68
+ *
69
+ * @defaultValue the service's `SANDBOX_NODE_USE_ENV_PROXY`, `false` by default
70
+ */
71
+ nodeUseEnvProxy?: boolean;
62
72
  }
63
73
  /** What a new sandbox is made of. */
64
74
  export interface GceCreationSettings extends GceConnectionSettings {
@@ -44,6 +44,6 @@ export declare function deleteGceSandboxTemplate(options: GceTemplateDeletionSet
44
44
  * one: throws `GceSandboxNotFoundError` when no sandbox is named `sandboxId`.
45
45
  *
46
46
  * The service does not keep what a sandbox may reach across a suspension: pass the same `ports`,
47
- * `allowedHosts` and `baseUrls` the sandbox was created with.
47
+ * `allowedHosts`, `baseUrls` and `nodeUseEnvProxy` the sandbox was created with.
48
48
  */
49
49
  export declare function resumeGceNetworkSandboxSession(options: GceNetworkSandboxSessionResumeOptions): Promise<GceNetworkSandboxSession>;
@@ -47,14 +47,14 @@ async function createFrom(client, name, { template, ...options }) {
47
47
  * both.
48
48
  */
49
49
  export async function createGceNetworkSandboxSession(options) {
50
- const { abortSignal, template, setup = [], allowedHosts, baseUrls, image } = options;
50
+ const { abortSignal, template, setup = [], allowedHosts, baseUrls, nodeUseEnvProxy, image, } = options;
51
51
  const requiredCommands = options.requiredCommands ?? DEFAULT_REQUIRED_COMMANDS;
52
52
  if (abortSignal?.aborted)
53
53
  throw abortReason(abortSignal);
54
54
  const name = options.sandboxId ?? `ai-sdk-${randomBytes(4).toString('hex')}`;
55
55
  assertSandboxName(name);
56
56
  const client = clientOf(options);
57
- const network = { allowedHosts, baseUrls };
57
+ const network = { allowedHosts, baseUrls, nodeUseEnvProxy };
58
58
  const create = (ready) => createFrom(client, name, { ...network, image, template: ready, abortSignal });
59
59
  const description = await (template
60
60
  ? withTemplate(client, { template, setup, network, image, requiredCommands, abortSignal }, create)
@@ -118,15 +118,20 @@ export async function deleteGceSandboxTemplate(options) {
118
118
  * one: throws `GceSandboxNotFoundError` when no sandbox is named `sandboxId`.
119
119
  *
120
120
  * The service does not keep what a sandbox may reach across a suspension: pass the same `ports`,
121
- * `allowedHosts` and `baseUrls` the sandbox was created with.
121
+ * `allowedHosts`, `baseUrls` and `nodeUseEnvProxy` the sandbox was created with.
122
122
  */
123
123
  export async function resumeGceNetworkSandboxSession(options) {
124
- const { abortSignal, sandboxId, allowedHosts, baseUrls } = options;
124
+ const { abortSignal, sandboxId, allowedHosts, baseUrls, nodeUseEnvProxy } = options;
125
125
  if (abortSignal?.aborted)
126
126
  throw abortReason(abortSignal);
127
127
  assertSandboxName(sandboxId);
128
128
  const client = clientOf(options);
129
- const description = await client.resume(sandboxId, { allowedHosts, baseUrls, abortSignal });
129
+ const description = await client.resume(sandboxId, {
130
+ allowedHosts,
131
+ baseUrls,
132
+ nodeUseEnvProxy,
133
+ abortSignal,
134
+ });
130
135
  if (description === undefined)
131
136
  throw new GceSandboxNotFoundError(sandboxId);
132
137
  return sessionOf(client, description, options);
@@ -1,7 +1,7 @@
1
1
  import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
2
2
  import { join } from 'node:path';
3
3
  import { readCommandLine } from '../common/command-line.js';
4
- import { offsetOf, parseObject, readBytes, readJson, requiredString, stringRecord, } from '../common/http-body.js';
4
+ import { offsetOf, optionalBoolean, parseObject, readBytes, readJson, requiredString, stringRecord, } from '../common/http-body.js';
5
5
  import { HttpError } from '../common/http-error.js';
6
6
  import { NO_CONTENT, waitForExit } from '../common/http-reply.js';
7
7
  import { MAX_INPUT_WRITE_BYTES } from '../protocol/api.js';
@@ -19,7 +19,12 @@ function toConfig(body) {
19
19
  const proxy = requiredString(body, 'proxy');
20
20
  if (!URL.canParse(proxy))
21
21
  throw new HttpError(400, '`proxy` must be a URL.');
22
- return { proxy, baseUrls: stringRecord(body, 'baseUrls') ?? {} };
22
+ const nodeUseEnvProxy = optionalBoolean(body, 'nodeUseEnvProxy');
23
+ return {
24
+ proxy,
25
+ baseUrls: stringRecord(body, 'baseUrls') ?? {},
26
+ ...(nodeUseEnvProxy !== undefined && { nodeUseEnvProxy }),
27
+ };
23
28
  }
24
29
  /**
25
30
  * What the agent does for the service (`protocol/agent.ts`): run commands as the sandbox's user,
@@ -17,5 +17,9 @@ export declare function noProxy(config: AgentConfig): string;
17
17
  * base URL variable is routed too: a harness that points its client at the real API still goes
18
18
  * through the proxy. Before the service configured the agent, commands get no proxy: the firewall
19
19
  * leaves them no way out.
20
+ *
21
+ * `NODE_USE_ENV_PROXY` only when the sandbox asks for it: it sends Node.js's own `fetch` and `http`
22
+ * to the proxy, where a test suite that mocks them by their sockets (MSW 3) no longer sees its
23
+ * requests.
20
24
  */
21
25
  export declare function commandEnvironment(settings: CommandSettings, config: AgentConfig | undefined, env?: Readonly<Record<string, string>>): Record<string, string>;
@@ -18,6 +18,10 @@ export function noProxy(config) {
18
18
  * base URL variable is routed too: a harness that points its client at the real API still goes
19
19
  * through the proxy. Before the service configured the agent, commands get no proxy: the firewall
20
20
  * leaves them no way out.
21
+ *
22
+ * `NODE_USE_ENV_PROXY` only when the sandbox asks for it: it sends Node.js's own `fetch` and `http`
23
+ * to the proxy, where a test suite that mocks them by their sockets (MSW 3) no longer sees its
24
+ * requests.
21
25
  */
22
26
  export function commandEnvironment(settings, config, env = {}) {
23
27
  const variables = {
@@ -33,15 +37,16 @@ export function commandEnvironment(settings, config, env = {}) {
33
37
  const route = (url) => proxyRoute(url, origin) ?? url;
34
38
  const excluded = noProxy(config);
35
39
  Object.assign(variables, {
36
- // Everything else leaves through the proxy: package managers, git and Node.js read these.
40
+ // Everything else leaves through the proxy: package managers, git and the harnesses read these.
37
41
  HTTPS_PROXY: origin,
38
42
  HTTP_PROXY: origin,
39
43
  https_proxy: origin,
40
44
  http_proxy: origin,
41
45
  NO_PROXY: excluded,
42
46
  no_proxy: excluded,
43
- NODE_USE_ENV_PROXY: '1',
44
47
  });
48
+ if (config.nodeUseEnvProxy === true)
49
+ variables['NODE_USE_ENV_PROXY'] = '1';
45
50
  for (const [name, url] of Object.entries(config.baseUrls))
46
51
  variables[name] = route(url);
47
52
  for (const [name, value] of Object.entries(env)) {
@@ -30,6 +30,11 @@ export interface AgentConfig {
30
30
  proxy: string;
31
31
  /** Base URL variables, routed through the proxy, and where each really leads. */
32
32
  baseUrls: Readonly<Record<string, string>>;
33
+ /**
34
+ * Whether the commands get `NODE_USE_ENV_PROXY=1`, which sends Node.js's own `fetch` and `http`
35
+ * through the proxy. Off without it.
36
+ */
37
+ nodeUseEnvProxy?: boolean;
33
38
  }
34
39
  /** Where a sandbox's commands run: the agent's answer to its configuration. */
35
40
  export interface AgentDescription {
@@ -12,6 +12,8 @@ export interface SandboxDescription {
12
12
  export interface SandboxNetwork {
13
13
  allowedHosts?: readonly string[];
14
14
  baseUrls?: Readonly<Record<string, string>>;
15
+ /** Sets `NODE_USE_ENV_PROXY=1` for the sandbox's commands: the service's setting without it. */
16
+ nodeUseEnvProxy?: boolean;
15
17
  }
16
18
  /** One command to run in a sandbox. */
17
19
  export interface SandboxCommand {
@@ -219,6 +219,7 @@ export async function sandboxesConfig(env, logger) {
219
219
  network: {
220
220
  allowedHosts: allowedHosts(env, logger),
221
221
  baseUrls: baseUrls(env['SANDBOX_BASE_URLS']),
222
+ nodeUseEnvProxy: flag('SANDBOX_NODE_USE_ENV_PROXY', env['SANDBOX_NODE_USE_ENV_PROXY'], false),
222
223
  },
223
224
  startTimeoutMs: Number.parseInt(env['SANDBOX_START_TIMEOUT'] ?? '300', 10) * 1000,
224
225
  maxRunning: maxRunning(env['SANDBOX_MAX_RUNNING']),
@@ -1,4 +1,4 @@
1
- import { offsetOf, optionalString, readBytes, readJson, requiredString, stringList, stringRecord, transformationList, } from '../../common/http-body.js';
1
+ import { offsetOf, optionalBoolean, optionalString, readBytes, readJson, requiredString, stringList, stringRecord, transformationList, } from '../../common/http-body.js';
2
2
  import { NO_CONTENT } from '../../common/http-reply.js';
3
3
  import { PACKAGE_VERSION } from '../../common/package-version.js';
4
4
  import { MAX_INPUT_WRITE_BYTES } from '../../protocol/api.js';
@@ -9,6 +9,7 @@ const network = async (request) => {
9
9
  return {
10
10
  allowedHosts: stringList(body, 'allowedHosts'),
11
11
  baseUrls: stringRecord(body, 'baseUrls'),
12
+ nodeUseEnvProxy: optionalBoolean(body, 'nodeUseEnvProxy'),
12
13
  template: optionalString(body, 'template'),
13
14
  image: optionalString(body, 'image'),
14
15
  name: optionalString(body, 'name'),
@@ -55,8 +55,11 @@ export interface SandboxesOptions {
55
55
  };
56
56
  /** The egress proxy, as the machines reach it: `http://10.0.0.2:3128`. */
57
57
  proxyUrl: string;
58
- /** What every sandbox may reach, before what the caller adds. */
59
- network: Required<SandboxNetwork>;
58
+ /**
59
+ * What every sandbox may reach, before what the caller adds; whether its commands get
60
+ * `NODE_USE_ENV_PROXY`, when the caller does not say.
61
+ */
62
+ network: Pick<SandboxNetwork, 'nodeUseEnvProxy'> & Required<Omit<SandboxNetwork, 'nodeUseEnvProxy'>>;
60
63
  /** How long a machine has to boot and its agent to answer. */
61
64
  startTimeoutMs: number;
62
65
  /**
@@ -801,6 +801,7 @@ export class Sandboxes {
801
801
  const { home, workingDirectory } = await agent.configure({
802
802
  proxy: this.options.proxyUrl,
803
803
  baseUrls,
804
+ nodeUseEnvProxy: network.nodeUseEnvProxy ?? this.options.network.nodeUseEnvProxy,
804
805
  });
805
806
  return { name, home, workingDirectory };
806
807
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-sdk-sandbox-gce",
3
- "version": "0.4.2",
3
+ "version": "0.5.0",
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",