ai-sdk-sandbox-gce 0.2.0 → 0.2.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 CHANGED
@@ -1,5 +1,18 @@
1
1
  # ai-sdk-sandbox-gce
2
2
 
3
+ ## 0.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 40ccefb: A template whose image takes more than five minutes to save is no longer lost. Node.js's fetch gave up on the save call after five minutes, so the client removed the builder while Compute Engine was still making the image from its disk, and the session failed with `fetch failed`. When the call is cut on the way, by the network or by Cloud Run's front end, the client now asks the service for the template until it is ready, for up to 30 minutes, and removes the builder only after that. A refusal from the service is still reported at once.
8
+
9
+ ## 0.2.1
10
+
11
+ ### Patch Changes
12
+
13
+ - ec23275: Git over HTTPS takes the credentials brokered for its server, such as a repository of `ai-sdk-harness-workspace`. Its requests went through the proxy's TLS tunnel, which the proxy cannot read, so the server got the placeholder and refused the fetch. The agent now sends them to the proxy's routes, through a `url.<proxy>/https/.insteadOf https://` setting added after the command's own `GIT_CONFIG_*` entries. Build the sandbox image again with this version.
14
+ - bd8f6da: The proxy's startup script in the README upgrades the package when its pinned version changes, and restarts the proxy on it: it used to install the package only on a machine that had none, so a proxy kept its first version. The README says how to hand the new script to the machine.
15
+
3
16
  ## 0.2.0
4
17
 
5
18
  ### Minor Changes
package/README.md CHANGED
@@ -141,10 +141,15 @@ for network in /run/systemd/network/*.network; do
141
141
  printf '[DHCPv4]\nUseDNS=false\n' > "/etc/systemd/network/$(basename "$network").d/no-dhcp-dns.conf"
142
142
  done
143
143
  systemctl restart systemd-networkd systemd-resolved
144
- if ! command -v ai-sdk-sandbox-gce >/dev/null; then
144
+ if ! command -v npm >/dev/null; then
145
145
  curl -fsSL https://deb.nodesource.com/setup_24.x | bash -
146
146
  apt-get install -y nodejs
147
+ fi
148
+ # Installed again whenever the pinned version changes: the proxy then restarts on it, below.
149
+ restart=false
150
+ if ! npm ls --global ai-sdk-sandbox-gce@0.2.0 >/dev/null 2>&1; then
147
151
  npm install --global ai-sdk-sandbox-gce@0.2.0
152
+ restart=true
148
153
  fi
149
154
  # Its logs, the refusals among them, sent to Cloud Logging by the Ops Agent.
150
155
  if [ ! -d /opt/google-cloud-ops-agent ]; then
@@ -177,19 +182,28 @@ DynamicUser=yes
177
182
  [Install]
178
183
  WantedBy=multi-user.target
179
184
  UNIT
180
- # Started by systemd at boot, before this script: restarted only when its unit changed, since a
181
- # restart forgets the sandboxes registered meanwhile.
182
185
  if ! cmp -s /tmp/ai-sdk-sandbox-proxy.service /etc/systemd/system/ai-sdk-sandbox-proxy.service; then
183
186
  mv /tmp/ai-sdk-sandbox-proxy.service /etc/systemd/system/ai-sdk-sandbox-proxy.service
184
187
  systemctl daemon-reload
185
- systemctl enable ai-sdk-sandbox-proxy
188
+ restart=true
189
+ fi
190
+ systemctl enable ai-sdk-sandbox-proxy
191
+ # Started by systemd at boot, before this script: restarted only when its package or its unit
192
+ # changed, since a restart forgets the sandboxes registered meanwhile.
193
+ if [ "$restart" = true ]; then
186
194
  systemctl restart ai-sdk-sandbox-proxy
187
195
  else
188
- systemctl enable --now ai-sdk-sandbox-proxy
196
+ systemctl start ai-sdk-sandbox-proxy
189
197
  fi
190
198
  ```
191
199
 
192
- Pin `ai-sdk-sandbox-gce` to the version your application depends on, here and in the two images below: every side checks that the others speak its protocol.
200
+ Pin `ai-sdk-sandbox-gce` to the version your application depends on, here and in the two images below: every side checks that the others speak its protocol. To upgrade the proxy, change the version in the script, hand it to the machine again, and reboot it, or let the service start it with the next sandbox if it is stopped:
201
+
202
+ ```bash
203
+ gcloud compute instances add-metadata sandbox-proxy --zone=$ZONE \
204
+ --metadata-from-file=startup-script=proxy-startup.sh
205
+ gcloud compute instances reset sandbox-proxy --zone=$ZONE
206
+ ```
193
207
 
194
208
  The proxy need not run when no sandbox does: set `SANDBOX_PROXY_INSTANCE=sandbox-proxy` on the service (step 4), which then starts it with the first sandbox, booting alongside it, and stops it once none runs. Its startup script runs again at each boot, and its registrations, held in memory, are made again by the sandboxes that start.
195
209
 
@@ -447,6 +461,8 @@ The real credentials of a harness, an API key or a subscription token, never ent
447
461
  3. The API's client in the sandbox is pointed at the proxy, in plain HTTP inside the VPC, through its base URL variable: `ANTHROPIC_BASE_URL=https://api.anthropic.com` becomes `http://10.8.16.2:3128/https/api.anthropic.com`.
448
462
  4. The proxy swaps the placeholder for the real value and sends the request on over HTTPS. The answer comes back without it, even when the API echoes it in a header.
449
463
 
464
+ Git over HTTPS goes the same way: the agent hands every command a `url.<proxy>/https/.insteadOf https://` setting, after the command's own `GIT_CONFIG_*` entries, so that a credential brokered for a Git server, such as a repository of [`ai-sdk-harness-workspace`](https://ai-sdk-harness.pages.dev/docs/packages/harness-workspace), is swapped in too. Through the `HTTPS_PROXY` tunnel, which the proxy cannot read, the server would get the placeholder.
465
+
450
466
  The service routes `ANTHROPIC_BASE_URL` (Claude Code) and `OPENAI_BASE_URL` (Codex) by default; add others with `baseUrls`. With a subscription, the refresh token never leaves your application: only the short-lived access token reaches the proxy.
451
467
 
452
468
  | Protection | What it prevents |
@@ -52,7 +52,8 @@ async function build(client, { template, setup, network, image, requiredCommands
52
52
  await runSetup(session, setup, abortSignal);
53
53
  await assertRequiredCommands(session, requiredCommands, abortSignal);
54
54
  await template.prepare({ session: session.restricted(), abortSignal });
55
- await client.saveTemplate(builder, id, abortSignal);
55
+ // Removed below only once saved: its disk is what the template's image is made of.
56
+ await client.saveTemplate(builder, id, { image, abortSignal });
56
57
  return id;
57
58
  }
58
59
  finally {
@@ -64,7 +64,15 @@ export declare class SandboxServiceClient {
64
64
  /** Replaces the hosts the sandbox may reach, those the service allows every sandbox aside. */
65
65
  setAllowedHosts(name: string, allowedHosts: readonly string[]): Promise<void>;
66
66
  /** Saves the running sandbox as the template `id`. */
67
- saveTemplate(name: string, id: string, abortSignal?: AbortSignal): Promise<void>;
67
+ /**
68
+ * Saves the sandbox `name` as the template `id`, made on `image`. Making the image of its disk
69
+ * takes minutes, sometimes longer than fetch waits for an answer: a call cut on the way, which
70
+ * the service goes on with, is followed by asking for the template until it is ready.
71
+ */
72
+ saveTemplate(name: string, id: string, { image, abortSignal }?: {
73
+ image?: string;
74
+ abortSignal?: AbortSignal;
75
+ }): Promise<void>;
68
76
  /** Whether the template `id` exists, made on the image `image` (the service's default without). */
69
77
  templateExists(id: string, image?: string, abortSignal?: AbortSignal): Promise<boolean>;
70
78
  deleteTemplate(id: string, image?: string): Promise<void>;
@@ -12,6 +12,24 @@ const templatePath = (id, image) => image === undefined
12
12
  const TRANSIENT_STATUSES = new Set([429, 500, 502, 503, 504]);
13
13
  /** How long to wait before each new attempt of a call that may be sent again. */
14
14
  const RETRY_DELAYS_MS = [200, 500, 1000, 2000];
15
+ /** How long a template whose save was cut on the way may take to be ready. */
16
+ const TEMPLATE_SAVE_TIMEOUT_MS = 30 * 60_000;
17
+ /** The waits between two questions about a template being saved: soon at first, then every 10 s. */
18
+ function* templatePolls() {
19
+ yield* [1000, 2000, 5000];
20
+ for (;;)
21
+ yield 10_000;
22
+ }
23
+ /** A call cut on the way, by the network or by Cloud Run's front end, which the service may go on with. */
24
+ function isCut(error) {
25
+ return (error instanceof TypeError ||
26
+ (error instanceof GceSandboxServiceError && [502, 503, 504].includes(error.status)));
27
+ }
28
+ /** A call that failed on the way rather than at the service: the network, or Cloud Run's front end. */
29
+ function isTransient(error) {
30
+ return (error instanceof TypeError ||
31
+ (error instanceof GceSandboxServiceError && TRANSIENT_STATUSES.has(error.status)));
32
+ }
15
33
  /**
16
34
  * The HTTP API of the sandbox service (`ai-sdk-sandbox-gce serve`). Each call carries an
17
35
  * identity token when there is one: Cloud Run's IAM checks it. Nothing else of this process goes
@@ -110,8 +128,30 @@ export class SandboxServiceClient {
110
128
  await this.call('PUT', `v1/sandboxes/${name}/network`, { body: { allowedHosts } });
111
129
  }
112
130
  /** Saves the running sandbox as the template `id`. */
113
- async saveTemplate(name, id, abortSignal) {
114
- await this.call('POST', `v1/sandboxes/${name}/template`, { body: { id }, abortSignal });
131
+ /**
132
+ * Saves the sandbox `name` as the template `id`, made on `image`. Making the image of its disk
133
+ * takes minutes, sometimes longer than fetch waits for an answer: a call cut on the way, which
134
+ * the service goes on with, is followed by asking for the template until it is ready.
135
+ */
136
+ async saveTemplate(name, id, { image, abortSignal } = {}) {
137
+ const request = `POST /v1/sandboxes/${name}/template`;
138
+ try {
139
+ await this.call('POST', `v1/sandboxes/${name}/template`, { body: { id }, abortSignal });
140
+ return;
141
+ }
142
+ catch (error) {
143
+ if (abortSignal?.aborted === true || !isCut(error))
144
+ throw error;
145
+ }
146
+ const deadline = Date.now() + TEMPLATE_SAVE_TIMEOUT_MS;
147
+ for (const delay of templatePolls()) {
148
+ if (await this.retried(() => this.templateExists(id, image, abortSignal)))
149
+ return;
150
+ if (Date.now() + delay > deadline)
151
+ break;
152
+ await sleep(delay, undefined, { signal: abortSignal });
153
+ }
154
+ throw new GceSandboxServiceError(request, 504, `the template ${id} was not ready ${TEMPLATE_SAVE_TIMEOUT_MS / 60_000} min after the call was cut.`);
115
155
  }
116
156
  /** Whether the template `id` exists, made on the image `image` (the service's default without). */
117
157
  async templateExists(id, image, abortSignal) {
@@ -156,9 +196,7 @@ export class SandboxServiceClient {
156
196
  return await call();
157
197
  }
158
198
  catch (error) {
159
- const transient = error instanceof TypeError ||
160
- (error instanceof GceSandboxServiceError && TRANSIENT_STATUSES.has(error.status));
161
- if (!transient)
199
+ if (!isTransient(error))
162
200
  throw error;
163
201
  }
164
202
  await sleep(delay);
@@ -13,8 +13,9 @@ export interface CommandSettings {
13
13
  export declare function noProxy(config: AgentConfig): string;
14
14
  /**
15
15
  * The variables a command runs with: the sandbox's, its base URLs routed through the egress proxy,
16
- * then the command's own. A command's own value for a base URL variable is routed too: a harness
17
- * that points its client at the real API still goes through the proxy. Before the service configured
18
- * the agent, commands get no proxy: the firewall leaves them no way out.
16
+ * then the command's own, and git's HTTPS sent to the proxy's routes. A command's own value for a
17
+ * base URL variable is routed too: a harness that points its client at the real API still goes
18
+ * through the proxy. Before the service configured the agent, commands get no proxy: the firewall
19
+ * leaves them no way out.
19
20
  */
20
21
  export declare function commandEnvironment(settings: CommandSettings, config: AgentConfig | undefined, env?: Readonly<Record<string, string>>): Record<string, string>;
@@ -14,9 +14,10 @@ export function noProxy(config) {
14
14
  }
15
15
  /**
16
16
  * The variables a command runs with: the sandbox's, its base URLs routed through the egress proxy,
17
- * then the command's own. A command's own value for a base URL variable is routed too: a harness
18
- * that points its client at the real API still goes through the proxy. Before the service configured
19
- * the agent, commands get no proxy: the firewall leaves them no way out.
17
+ * then the command's own, and git's HTTPS sent to the proxy's routes. A command's own value for a
18
+ * base URL variable is routed too: a harness that points its client at the real API still goes
19
+ * through the proxy. Before the service configured the agent, commands get no proxy: the firewall
20
+ * leaves them no way out.
20
21
  */
21
22
  export function commandEnvironment(settings, config, env = {}) {
22
23
  const variables = {
@@ -46,5 +47,22 @@ export function commandEnvironment(settings, config, env = {}) {
46
47
  for (const [name, value] of Object.entries(env)) {
47
48
  variables[name] = name in config.baseUrls ? route(value) : value;
48
49
  }
49
- return variables;
50
+ return withGitRoutes(variables, origin);
51
+ }
52
+ /**
53
+ * Git over HTTPS through the proxy's routes rather than a TLS tunnel, after the command's own
54
+ * `GIT_CONFIG_*` entries: the proxy then reads its requests, and swaps in a credential brokered for
55
+ * the Git server, which a tunnel would carry to it as the placeholder.
56
+ */
57
+ function withGitRoutes(variables, origin) {
58
+ const count = variables['GIT_CONFIG_COUNT'] ?? '0';
59
+ // A count git would refuse: the command fails on it whatever is added.
60
+ if (!/^\d+$/.test(count))
61
+ return variables;
62
+ return {
63
+ ...variables,
64
+ GIT_CONFIG_COUNT: String(Number(count) + 1),
65
+ [`GIT_CONFIG_KEY_${count}`]: `url.${origin}/https/.insteadOf`,
66
+ [`GIT_CONFIG_VALUE_${count}`]: 'https://',
67
+ };
50
68
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-sdk-sandbox-gce",
3
- "version": "0.2.0",
3
+ "version": "0.2.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",
@@ -59,9 +59,9 @@
59
59
  "zod": "^4.6.7",
60
60
  "@repo/build": "0.0.0",
61
61
  "@repo/eslint-config": "0.0.0",
62
- "@repo/vitest-config": "0.0.0",
63
62
  "@repo/prettier-config": "0.0.0",
64
- "@repo/typescript-config": "0.0.0"
63
+ "@repo/typescript-config": "0.0.0",
64
+ "@repo/vitest-config": "0.0.0"
65
65
  },
66
66
  "author": {
67
67
  "name": "Fabien Pasquet",