ai-sdk-sandbox-gce 0.2.1 → 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,11 @@
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
+
3
9
  ## 0.2.1
4
10
 
5
11
  ### Patch Changes
@@ -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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-sdk-sandbox-gce",
3
- "version": "0.2.1",
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",
@@ -60,8 +60,8 @@
60
60
  "@repo/build": "0.0.0",
61
61
  "@repo/eslint-config": "0.0.0",
62
62
  "@repo/prettier-config": "0.0.0",
63
- "@repo/vitest-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",