@blaxel/core 0.3.13-preview.270 → 0.3.17

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.
Files changed (39) hide show
  1. package/dist/cjs/.tsbuildinfo +1 -1
  2. package/dist/cjs/client/sdk.gen.js +105 -3
  3. package/dist/cjs/common/settings.js +2 -2
  4. package/dist/cjs/common/shell.js +18 -0
  5. package/dist/cjs/sandbox/filesystem/filesystem.js +31 -11
  6. package/dist/cjs/sandbox/filesystem/filesystem.test.js +207 -2
  7. package/dist/cjs/sandbox/sandbox.js +124 -0
  8. package/dist/cjs/types/client/sdk.gen.d.ts +31 -1
  9. package/dist/cjs/types/client/types.gen.d.ts +368 -7
  10. package/dist/cjs/types/common/shell.d.ts +13 -0
  11. package/dist/cjs/types/sandbox/sandbox.d.ts +56 -1
  12. package/dist/cjs/types/sandbox/schedule.d.ts +2 -2
  13. package/dist/cjs-browser/.tsbuildinfo +1 -1
  14. package/dist/cjs-browser/client/sdk.gen.js +105 -3
  15. package/dist/cjs-browser/common/settings.js +2 -2
  16. package/dist/cjs-browser/common/shell.js +18 -0
  17. package/dist/cjs-browser/sandbox/filesystem/filesystem.js +31 -11
  18. package/dist/cjs-browser/sandbox/filesystem/filesystem.test.js +207 -2
  19. package/dist/cjs-browser/sandbox/sandbox.js +124 -0
  20. package/dist/cjs-browser/types/client/sdk.gen.d.ts +31 -1
  21. package/dist/cjs-browser/types/client/types.gen.d.ts +368 -7
  22. package/dist/cjs-browser/types/common/shell.d.ts +13 -0
  23. package/dist/cjs-browser/types/sandbox/sandbox.d.ts +56 -1
  24. package/dist/cjs-browser/types/sandbox/schedule.d.ts +2 -2
  25. package/dist/esm/.tsbuildinfo +1 -1
  26. package/dist/esm/client/sdk.gen.js +96 -0
  27. package/dist/esm/common/settings.js +2 -2
  28. package/dist/esm/common/shell.js +15 -0
  29. package/dist/esm/sandbox/filesystem/filesystem.js +31 -11
  30. package/dist/esm/sandbox/filesystem/filesystem.test.js +208 -3
  31. package/dist/esm/sandbox/sandbox.js +125 -1
  32. package/dist/esm-browser/.tsbuildinfo +1 -1
  33. package/dist/esm-browser/client/sdk.gen.js +96 -0
  34. package/dist/esm-browser/common/settings.js +2 -2
  35. package/dist/esm-browser/common/shell.js +15 -0
  36. package/dist/esm-browser/sandbox/filesystem/filesystem.js +31 -11
  37. package/dist/esm-browser/sandbox/filesystem/filesystem.test.js +208 -3
  38. package/dist/esm-browser/sandbox/sandbox.js +125 -1
  39. package/package.json +1 -1
@@ -1616,6 +1616,22 @@ export const updateSandbox = (options) => {
1616
1616
  }
1617
1617
  });
1618
1618
  };
1619
+ /**
1620
+ * Archive sandbox
1621
+ * Archives a sandbox. The changes its filesystem accumulated over its image are exported to the archive store, then the sandbox is shut down while its definition is kept, so it can be recreated later with the same disk. Memory and running processes are not preserved, processes are started again from their configuration when the sandbox is unarchived.
1622
+ */
1623
+ export const archiveSandbox = (options) => {
1624
+ return (options.client ?? _heyApiClient).post({
1625
+ security: [
1626
+ {
1627
+ scheme: 'bearer',
1628
+ type: 'http'
1629
+ }
1630
+ ],
1631
+ url: '/sandboxes/{sandboxName}/archive',
1632
+ ...options
1633
+ });
1634
+ };
1619
1635
  /**
1620
1636
  * Fork sandbox
1621
1637
  * Forks a sandbox into a new sandbox or application. When forking to a sandbox, the target must not already exist (409 if it does). When forking to an application, a new revision is added if the app already exists, or a new application is created. This is a WIP endpoint — the full implementation depends on the execution plane.
@@ -1932,6 +1948,38 @@ export const deleteSandboxSnapshot = (options) => {
1932
1948
  ...options
1933
1949
  });
1934
1950
  };
1951
+ /**
1952
+ * Restore sandbox from snapshot
1953
+ * Restores a sandbox to one of its own snapshots. The running sandbox is torn down and rebuilt from the snapshot under the same name and URLs, so everything it held since the snapshot was taken is lost unless it was itself snapshotted.
1954
+ */
1955
+ export const restoreSandboxSnapshot = (options) => {
1956
+ return (options.client ?? _heyApiClient).post({
1957
+ security: [
1958
+ {
1959
+ scheme: 'bearer',
1960
+ type: 'http'
1961
+ }
1962
+ ],
1963
+ url: '/sandboxes/{sandboxName}/snapshots/{snapshotId}/restore',
1964
+ ...options
1965
+ });
1966
+ };
1967
+ /**
1968
+ * Unarchive sandbox
1969
+ * Recreates an archived sandbox from its archive. The recreated sandbox runs the current generation whatever generation it was archived from, restores the archived filesystem before anything starts, and starts the saved processes again with new process identifiers.
1970
+ */
1971
+ export const unarchiveSandbox = (options) => {
1972
+ return (options.client ?? _heyApiClient).post({
1973
+ security: [
1974
+ {
1975
+ scheme: 'bearer',
1976
+ type: 'http'
1977
+ }
1978
+ ],
1979
+ url: '/sandboxes/{sandboxName}/unarchive',
1980
+ ...options
1981
+ });
1982
+ };
1935
1983
  /**
1936
1984
  * Get sandbox by external ID
1937
1985
  * Returns the most recent non-terminated sandbox matching the given external ID. If no active sandbox is found, returns 404.
@@ -1948,6 +1996,54 @@ export const getSandboxByExternalId = (options) => {
1948
1996
  ...options
1949
1997
  });
1950
1998
  };
1999
+ /**
2000
+ * List Workspace Schedule Executions
2001
+ * Returns schedule execution submissions across the workspace, newest first by default. Status describes submission acceptance, not process completion. since and until are inclusive RFC 3339 bounds on creation time.
2002
+ */
2003
+ export const listScheduleExecutions = (options) => {
2004
+ return (options?.client ?? _heyApiClient).get({
2005
+ security: [
2006
+ {
2007
+ scheme: 'bearer',
2008
+ type: 'http'
2009
+ }
2010
+ ],
2011
+ url: '/schedule-executions',
2012
+ ...options
2013
+ });
2014
+ };
2015
+ /**
2016
+ * List Workspace Schedules
2017
+ * Returns schedule definitions across the workspace, newest first by default.
2018
+ */
2019
+ export const listSchedules = (options) => {
2020
+ return (options?.client ?? _heyApiClient).get({
2021
+ security: [
2022
+ {
2023
+ scheme: 'bearer',
2024
+ type: 'http'
2025
+ }
2026
+ ],
2027
+ url: '/schedules',
2028
+ ...options
2029
+ });
2030
+ };
2031
+ /**
2032
+ * Get Sandbox Scheduling Metrics
2033
+ * Returns active sandbox and scheduling metrics for a UTC minute window. since is inclusive and until is exclusive. The default window is the last 24 hours and the maximum is 7 days. Execution status describes submission acceptance, not process completion. Execution totals begin accumulating when metrics collection is enabled and can lag recent firings briefly.
2034
+ */
2035
+ export const getSandboxScheduleMetrics = (options) => {
2036
+ return (options?.client ?? _heyApiClient).get({
2037
+ security: [
2038
+ {
2039
+ scheme: 'bearer',
2040
+ type: 'http'
2041
+ }
2042
+ ],
2043
+ url: '/schedules/metrics',
2044
+ ...options
2045
+ });
2046
+ };
1951
2047
  /**
1952
2048
  * List service accounts
1953
2049
  * Returns all service accounts in the workspace. Service accounts are machine identities for external systems to authenticate with Blaxel via OAuth or API keys.
@@ -24,8 +24,8 @@ function missingCredentialsMessage() {
24
24
  return "No Blaxel credentials found. Set the BL_API_KEY and BL_WORKSPACE environment variables, or run `bl login`.";
25
25
  }
26
26
  // Build info - these placeholders are replaced at build time by build:replace-imports
27
- const BUILD_VERSION = "0.3.13-preview.270";
28
- const BUILD_COMMIT = "615fb5551f22447c530bcaef346d68dc79590758";
27
+ const BUILD_VERSION = "0.3.17";
28
+ const BUILD_COMMIT = "3e03a965bdb4d5aa9d2c5105cd1f4d8018198df2";
29
29
  const BUILD_SENTRY_DSN = "https://fd5e60e1c9820e1eef5ccebb84a07127@o4508714045276160.ingest.us.sentry.io/4510465864564736";
30
30
  const BLAXEL_API_VERSION = "2026-04-28";
31
31
  // Bun < 1.3.11 never sends connection-level WINDOW_UPDATE: the pooled h2
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Quote a string so a POSIX shell reads it as one literal argument.
3
+ *
4
+ * The sandbox process API accepts a single command string, which the server
5
+ * runs as `sh -c <command>`. Any value interpolated into that string is parsed
6
+ * by the shell, so an unquoted path containing `;`, `|`, `&&`, `$()`, backticks
7
+ * or `>` would execute as its own command. Single quotes suppress every form of
8
+ * shell expansion; the only character they cannot contain is a single quote
9
+ * itself, which is closed, escaped, and reopened.
10
+ *
11
+ * This is the TypeScript equivalent of Python's `shlex.quote`.
12
+ */
13
+ export function shellQuote(value) {
14
+ return `'${value.replace(/'/g, `'\\''`)}'`;
15
+ }
@@ -2,12 +2,13 @@ import { fs } from "../../common/node.js";
2
2
  import { settings } from "../../common/settings.js";
3
3
  import { withUploadSlot } from "../../common/h2fetch.js";
4
4
  import { isTransientResetError, retryOnTransientReset } from "../../common/transient-retry.js";
5
+ import { shellQuote } from "../../common/shell.js";
5
6
  import { SandboxAction } from "../action.js";
6
7
  import { deleteFilesystemByPath, deleteFilesystemMultipartByUploadIdAbort, getFilesystemByPath, getFilesystemContentSearchByPath, getFilesystemFindByPath, getFilesystemSearchByPath, getWatchFilesystemByPath, postFilesystemMultipartByUploadIdComplete, postFilesystemMultipartInitiateByPath, putFilesystemByPath, putFilesystemMultipartByUploadIdPart } from "../client/index.js";
7
8
  // Multipart upload constants
8
9
  const MULTIPART_THRESHOLD = 5 * 1024 * 1024; // 5MB
9
10
  const CHUNK_SIZE = 5 * 1024 * 1024; // 5MB per part
10
- const MAX_PARALLEL_UPLOADS = 3; // Number of parallel part uploads
11
+ const MAX_PARALLEL_UPLOADS = 3; // Maximum workers when the H2 upload cap is disabled
11
12
  // The transient-reset classifier and retry loop live in
12
13
  // common/transient-retry.ts, shared with the idempotent sandbox-op retry
13
14
  // (read/list/etc.) so every path judges "transient" identically. These aliases
@@ -343,8 +344,10 @@ export class SandboxFileSystem extends SandboxAction {
343
344
  });
344
345
  }
345
346
  async cp(source, destination, { maxWait = 180000 } = {}) {
347
+ // Quote both paths so the shell that runs this command treats them as
348
+ // single literal arguments instead of interpreting metacharacters in them.
346
349
  let process = await this.process.exec({
347
- command: `cp -r ${source} ${destination}`,
350
+ command: `cp -r ${shellQuote(source)} ${shellQuote(destination)}`,
348
351
  });
349
352
  process = await this.process.wait(process.pid, { maxWait, interval: 100 });
350
353
  if (process.status === "failed") {
@@ -498,11 +501,19 @@ export class SandboxFileSystem extends SandboxAction {
498
501
  // no h2Domain the parts go over globalThis.fetch on separate connections,
499
502
  // so the shared-connection cap does not apply.
500
503
  const h2Domain = this.sandbox?.h2Domain;
501
- // Upload parts in batches for parallel processing
502
- for (let i = 0; i < numParts; i += MAX_PARALLEL_UPLOADS) {
503
- const batch = [];
504
- for (let j = 0; j < MAX_PARALLEL_UPLOADS && i + j < numParts; j++) {
505
- const partNumber = i + j + 1;
504
+ // Keep a bounded worker queue full instead of waiting at fixed batch
505
+ // boundaries. The upload gate remains authoritative across files; its
506
+ // default of 2 also bounds this queue on H2.
507
+ const h2UploadLimit = h2Domain ? settings.maxConcurrentUploadH2Requests : 0;
508
+ const workerCount = Math.min(numParts, h2UploadLimit > 0 ? Math.min(MAX_PARALLEL_UPLOADS, h2UploadLimit) : MAX_PARALLEL_UPLOADS);
509
+ let nextPartNumber = 1;
510
+ let stopped = false;
511
+ let uploadError;
512
+ const uploadNextPart = async () => {
513
+ while (!stopped) {
514
+ const partNumber = nextPartNumber++;
515
+ if (partNumber > numParts)
516
+ return;
506
517
  const start = (partNumber - 1) * CHUNK_SIZE;
507
518
  const end = Math.min(start + CHUNK_SIZE, size);
508
519
  const chunk = blob.slice(start, end);
@@ -510,11 +521,20 @@ export class SandboxFileSystem extends SandboxAction {
510
521
  // concurrent part streams, not retry sequences; freed during backoff.
511
522
  const doPart = () => this.uploadPart(uploadId, partNumber, chunk);
512
523
  const partWithSlot = h2Domain ? () => withUploadSlot(h2Domain, doPart) : doPart;
513
- batch.push(retryOnTransient(partWithSlot));
524
+ try {
525
+ parts.push(await retryOnTransient(partWithSlot));
526
+ }
527
+ catch (error) {
528
+ if (!stopped) {
529
+ stopped = true;
530
+ uploadError = error;
531
+ }
532
+ }
514
533
  }
515
- // Wait for batch to complete
516
- const batchResults = await Promise.all(batch);
517
- parts.push(...batchResults);
534
+ };
535
+ await Promise.all(Array.from({ length: workerCount }, () => uploadNextPart()));
536
+ if (stopped) {
537
+ throw uploadError;
518
538
  }
519
539
  // Sort parts by partNumber to ensure correct order
520
540
  parts.sort((a, b) => (a.partNumber ?? 0) - (b.partNumber ?? 0));
@@ -1,9 +1,61 @@
1
- import { describe, expect, it } from "vitest";
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+ import { settings } from "../../common/settings.js";
2
3
  import { SandboxFileSystem } from "./filesystem.js";
3
4
  const waitForPartUpload = () => new Promise((resolve) => setTimeout(resolve, 5));
5
+ function deferred() {
6
+ let resolve;
7
+ const promise = new Promise((resolvePromise) => {
8
+ resolve = resolvePromise;
9
+ });
10
+ return { promise, resolve };
11
+ }
4
12
  describe("SandboxFileSystem multipart upload", () => {
5
- it("limits concurrent part uploads", async () => {
13
+ afterEach(() => {
14
+ delete settings.config.maxConcurrentUploadH2Requests;
15
+ delete settings.config.fsPartRetries;
16
+ vi.useRealTimers();
17
+ vi.restoreAllMocks();
18
+ });
19
+ it("keeps an H2 upload slot busy across former batch boundaries for 100 MiB", async () => {
20
+ settings.config.maxConcurrentUploadH2Requests = 2;
21
+ const filesystem = Object.create(SandboxFileSystem.prototype);
22
+ filesystem.sandbox = { h2Domain: "eng-3585.test" };
23
+ const started = [deferred(), deferred(), deferred(), deferred()];
24
+ const release = [deferred(), deferred(), deferred(), deferred()];
25
+ let completedParts = [];
26
+ filesystem.initiateMultipartUpload = () => Promise.resolve({ uploadId: "upload-1" });
27
+ filesystem.uploadPart = async (_uploadId, partNumber) => {
28
+ if (partNumber <= 4) {
29
+ started[partNumber - 1].resolve();
30
+ await release[partNumber - 1].promise;
31
+ }
32
+ return { partNumber, etag: `etag-${partNumber}` };
33
+ };
34
+ filesystem.completeMultipartUpload = (_uploadId, parts) => {
35
+ completedParts = parts;
36
+ return Promise.resolve({ message: "ok" });
37
+ };
38
+ filesystem.abortMultipartUpload = () => Promise.resolve({ message: "aborted" });
39
+ const upload = filesystem.uploadWithMultipart("/tmp/large-file.bin", { size: 100 * 1024 * 1024, slice: () => ({}) });
40
+ await Promise.all([started[0].promise, started[1].promise]);
41
+ release[0].resolve();
42
+ await started[2].promise;
43
+ release[2].resolve();
44
+ const crossedBatchBoundary = await Promise.race([
45
+ started[3].promise.then(() => true),
46
+ new Promise((resolve) => setTimeout(() => resolve(false), 25)),
47
+ ]);
48
+ release[1].resolve();
49
+ await started[3].promise;
50
+ release[3].resolve();
51
+ await upload;
52
+ expect(crossedBatchBoundary).toBe(true);
53
+ expect(completedParts).toHaveLength(20);
54
+ });
55
+ it("limits concurrent H2 part uploads to the existing cap", async () => {
56
+ settings.config.maxConcurrentUploadH2Requests = 2;
6
57
  const filesystem = Object.create(SandboxFileSystem.prototype);
58
+ filesystem.sandbox = { h2Domain: "concurrency.test" };
7
59
  let inFlight = 0;
8
60
  let maxInFlight = 0;
9
61
  const uploadedParts = [];
@@ -24,8 +76,161 @@ describe("SandboxFileSystem multipart upload", () => {
24
76
  filesystem.abortMultipartUpload = () => Promise.resolve({ message: "aborted" });
25
77
  const blob = new Blob([new Uint8Array(16 * 1024 * 1024)]);
26
78
  await filesystem.uploadWithMultipart("/tmp/large-file.bin", blob);
27
- expect(maxInFlight).toBeLessThanOrEqual(3);
79
+ expect(maxInFlight).toBe(2);
28
80
  expect(uploadedParts.sort((a, b) => a - b)).toEqual([1, 2, 3, 4]);
29
81
  expect(completedParts.map((part) => part.partNumber)).toEqual([1, 2, 3, 4]);
30
82
  });
83
+ it("uploads every byte exactly once across chunk boundaries", async () => {
84
+ const filesystem = Object.create(SandboxFileSystem.prototype);
85
+ const partSize = 5 * 1024 * 1024;
86
+ const source = new Uint8Array(partSize + 17);
87
+ for (let index = 0; index < source.length; index++) {
88
+ source[index] = index % 251;
89
+ }
90
+ const uploadedChunks = new Map();
91
+ let completedParts = [];
92
+ filesystem.initiateMultipartUpload = () => Promise.resolve({ uploadId: "upload-bytes" });
93
+ filesystem.uploadPart = async (_uploadId, partNumber, fileBlob) => {
94
+ uploadedChunks.set(partNumber, new Uint8Array(await fileBlob.arrayBuffer()));
95
+ return { partNumber, etag: `etag-${partNumber}` };
96
+ };
97
+ filesystem.completeMultipartUpload = (_uploadId, parts) => {
98
+ completedParts = parts;
99
+ return Promise.resolve({ message: "ok" });
100
+ };
101
+ filesystem.abortMultipartUpload = () => Promise.resolve({ message: "aborted" });
102
+ await filesystem.uploadWithMultipart("/tmp/bytes.bin", new Blob([source]));
103
+ const received = new Uint8Array(source.length);
104
+ let offset = 0;
105
+ for (const partNumber of [1, 2]) {
106
+ const chunk = uploadedChunks.get(partNumber);
107
+ expect(chunk).toBeDefined();
108
+ received.set(chunk, offset);
109
+ offset += chunk.length;
110
+ }
111
+ let mismatch = -1;
112
+ for (let index = 0; index < source.length; index++) {
113
+ if (received[index] !== source[index]) {
114
+ mismatch = index;
115
+ break;
116
+ }
117
+ }
118
+ expect([...uploadedChunks.values()].map((chunk) => chunk.length)).toEqual([partSize, 17]);
119
+ expect(offset).toBe(source.length);
120
+ expect(mismatch).toBe(-1);
121
+ expect(completedParts.map((part) => part.partNumber)).toEqual([1, 2]);
122
+ });
123
+ it("retries a transient part failure without duplicating completed parts", async () => {
124
+ vi.useFakeTimers();
125
+ vi.spyOn(Math, "random").mockReturnValue(0);
126
+ settings.config.fsPartRetries = 1;
127
+ const filesystem = Object.create(SandboxFileSystem.prototype);
128
+ const attempts = new Map();
129
+ let completedParts = [];
130
+ filesystem.initiateMultipartUpload = () => Promise.resolve({ uploadId: "upload-retry" });
131
+ filesystem.uploadPart = (_uploadId, partNumber) => {
132
+ const attempt = (attempts.get(partNumber) ?? 0) + 1;
133
+ attempts.set(partNumber, attempt);
134
+ if (partNumber === 1 && attempt === 1) {
135
+ return Promise.reject(Object.assign(new Error("connection reset"), { code: "ECONNRESET" }));
136
+ }
137
+ return Promise.resolve({ partNumber, etag: `etag-${partNumber}` });
138
+ };
139
+ filesystem.completeMultipartUpload = (_uploadId, parts) => {
140
+ completedParts = parts;
141
+ return Promise.resolve({ message: "ok" });
142
+ };
143
+ filesystem.abortMultipartUpload = () => Promise.resolve({ message: "aborted" });
144
+ const upload = filesystem.uploadWithMultipart("/tmp/retry.bin", new Blob([new Uint8Array(6 * 1024 * 1024)]));
145
+ await vi.runAllTimersAsync();
146
+ await upload;
147
+ expect(Object.fromEntries(attempts)).toEqual({ 1: 2, 2: 1 });
148
+ expect(completedParts.map((part) => part.partNumber)).toEqual([1, 2]);
149
+ });
150
+ it("stops assigning queued parts after a part failure", async () => {
151
+ settings.config.maxConcurrentUploadH2Requests = 2;
152
+ const filesystem = Object.create(SandboxFileSystem.prototype);
153
+ filesystem.sandbox = { h2Domain: "cancellation.test" };
154
+ const releaseFirstPart = deferred();
155
+ const secondPartStarted = deferred();
156
+ const failure = new Error("part 2 failed");
157
+ const startedParts = [];
158
+ const events = [];
159
+ filesystem.initiateMultipartUpload = () => Promise.resolve({ uploadId: "upload-cancel" });
160
+ filesystem.uploadPart = async (_uploadId, partNumber) => {
161
+ startedParts.push(partNumber);
162
+ if (partNumber === 1) {
163
+ await releaseFirstPart.promise;
164
+ events.push("part-1-finished");
165
+ return { partNumber, etag: "etag-1" };
166
+ }
167
+ secondPartStarted.resolve();
168
+ events.push("part-2-failed");
169
+ throw failure;
170
+ };
171
+ filesystem.completeMultipartUpload = () => {
172
+ throw new Error("completion must not run");
173
+ };
174
+ filesystem.abortMultipartUpload = () => {
175
+ events.push("aborted");
176
+ return Promise.resolve({ message: "aborted" });
177
+ };
178
+ const upload = filesystem.uploadWithMultipart("/tmp/cancel.bin", new Blob([new Uint8Array(16 * 1024 * 1024)]));
179
+ await secondPartStarted.promise;
180
+ await new Promise((resolve) => setTimeout(resolve, 0));
181
+ releaseFirstPart.resolve();
182
+ await expect(upload).rejects.toBe(failure);
183
+ expect(startedParts).toEqual([1, 2]);
184
+ expect(events).toEqual(["part-2-failed", "part-1-finished", "aborted"]);
185
+ });
186
+ it("keeps sliced part data bounded by the active worker count", async () => {
187
+ settings.config.maxConcurrentUploadH2Requests = 2;
188
+ const filesystem = Object.create(SandboxFileSystem.prototype);
189
+ filesystem.sandbox = { h2Domain: "memory.test" };
190
+ let unsettledSlices = 0;
191
+ let maxUnsettledSlices = 0;
192
+ let totalSlices = 0;
193
+ const fakeBlob = {
194
+ size: 100 * 1024 * 1024,
195
+ slice(start, end) {
196
+ totalSlices += 1;
197
+ unsettledSlices += 1;
198
+ maxUnsettledSlices = Math.max(maxUnsettledSlices, unsettledSlices);
199
+ return { size: end - start };
200
+ },
201
+ };
202
+ filesystem.initiateMultipartUpload = () => Promise.resolve({ uploadId: "upload-memory" });
203
+ filesystem.uploadPart = async (_uploadId, partNumber) => {
204
+ await waitForPartUpload();
205
+ unsettledSlices -= 1;
206
+ return { partNumber, etag: `etag-${partNumber}` };
207
+ };
208
+ filesystem.completeMultipartUpload = () => Promise.resolve({ message: "ok" });
209
+ filesystem.abortMultipartUpload = () => Promise.resolve({ message: "aborted" });
210
+ await filesystem.uploadWithMultipart("/tmp/100mb.bin", fakeBlob);
211
+ expect(totalSlices).toBe(20);
212
+ expect(maxUnsettledSlices).toBe(2);
213
+ expect(unsettledSlices).toBe(0);
214
+ });
215
+ it("aborts an abandoned multipart upload and preserves the part failure", async () => {
216
+ settings.config.maxConcurrentUploadH2Requests = 1;
217
+ const filesystem = Object.create(SandboxFileSystem.prototype);
218
+ filesystem.sandbox = { h2Domain: "cleanup.test" };
219
+ const partFailure = new Error("part failed");
220
+ const abortFailure = new Error("abort failed");
221
+ const aborts = [];
222
+ const consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined);
223
+ filesystem.initiateMultipartUpload = () => Promise.resolve({ uploadId: "upload-cleanup" });
224
+ filesystem.uploadPart = () => Promise.reject(partFailure);
225
+ filesystem.completeMultipartUpload = () => {
226
+ throw new Error("completion must not run");
227
+ };
228
+ filesystem.abortMultipartUpload = (uploadId) => {
229
+ aborts.push(uploadId);
230
+ return Promise.reject(abortFailure);
231
+ };
232
+ await expect(filesystem.uploadWithMultipart("/tmp/cleanup.bin", new Blob([new Uint8Array(6 * 1024 * 1024)]))).rejects.toBe(partFailure);
233
+ expect(aborts).toEqual(["upload-cleanup"]);
234
+ expect(consoleError).toHaveBeenCalledWith("Failed to abort multipart upload:", abortFailure);
235
+ });
31
236
  });
@@ -1,4 +1,4 @@
1
- import { createSandbox, createSandboxSnapshot, deleteSandbox, deleteSandboxSnapshot, forkSandbox, getSandbox, getSandboxByExternalId, listSandboxes, listSandboxSnapshots, updateSandbox } from "../client/index.js";
1
+ import { archiveSandbox, createSandbox, createSandboxSnapshot, deleteSandbox, deleteSandboxSnapshot, forkSandbox, getSandbox, getSandboxByExternalId, listSandboxes, listSandboxSnapshots, restoreSandboxSnapshot, unarchiveSandbox, updateSandbox } from "../client/index.js";
2
2
  import { logger } from "../common/logger.js";
3
3
  import { backoffDelayMs } from "../common/transient-retry.js";
4
4
  import { createPaginatedList } from "../common/pagination.js";
@@ -31,6 +31,23 @@ const TRANSIENT_SANDBOX_STATUSES = new Set([
31
31
  ]);
32
32
  const TRANSIENT_STATUS_MAX_WAIT_MS = 30_000;
33
33
  const TRANSIENT_STATUS_POLL_MS = 500;
34
+ // Archiving a filesystem, and restoring it, take as long as that filesystem is
35
+ // big — minutes for a few gigabytes.
36
+ const ARCHIVE_MAX_WAIT_MS = 1_800_000;
37
+ const ARCHIVE_WAIT_POLL_MS = 2_000;
38
+ // An archive is done when the sandbox is ARCHIVED; it is still under way while
39
+ // the record holds one of these.
40
+ const ARCHIVING_STATUSES = new Set(["ARCHIVING"]);
41
+ // A restore is done when the sandbox is DEPLOYED again; the instance is recreated
42
+ // before the archived filesystem is written back over its image.
43
+ const UNARCHIVING_STATUSES = new Set(["UNARCHIVING", "DEPLOYING", "BUILDING", "UPLOADING"]);
44
+ // The status the sandbox holds before the operation moves it, tolerated only
45
+ // while the operation is starting: an archive that fails hands the sandbox back
46
+ // as DEPLOYED and a restore that fails leaves it ARCHIVED, so reading the entry
47
+ // status once the operation has begun means it is over, not still running.
48
+ const ARCHIVE_ENTRY_STATUS = "DEPLOYED";
49
+ const UNARCHIVE_ENTRY_STATUS = "ARCHIVED";
50
+ const ARCHIVE_ENTRY_MAX_WAIT_MS = 30_000;
34
51
  // A create that outlives the edge's 60s origin-read timeout gets a 504 from
35
52
  // CloudFront while the control plane keeps deploying the sandbox for up to
36
53
  // 300s (ENG-3662 timeout ladder inversion). The 504 body is edge HTML with no
@@ -170,6 +187,24 @@ export class SandboxInstance {
170
187
  throwOnError: true,
171
188
  });
172
189
  }
190
+ /**
191
+ * Restore this sandbox to one of its own snapshots. The sandbox keeps its
192
+ * name, its URLs and its previews: the running instance is torn down and
193
+ * rebuilt from the snapshot, so everything written since it was taken is
194
+ * lost unless it was snapshotted too.
195
+ *
196
+ * The restore is asked for without waiting on the guest, the same way a fork
197
+ * is: connections to a sandbox still resuming are retried by the gateway.
198
+ *
199
+ * @param snapshotId - ID of the snapshot to restore this sandbox to.
200
+ */
201
+ async restore(snapshotId) {
202
+ const { data } = await restoreSandboxSnapshot({
203
+ path: { sandboxName: this.metadata.name, snapshotId },
204
+ throwOnError: true,
205
+ });
206
+ return data;
207
+ }
173
208
  /**
174
209
  * Fork this sandbox into a new sandbox or application.
175
210
  *
@@ -318,6 +353,95 @@ export class SandboxInstance {
318
353
  }
319
354
  return instance;
320
355
  }
356
+ /**
357
+ * Archive a sandbox: keep its filesystem, stop the sandbox.
358
+ *
359
+ * The filesystem changes made over the image are exported to the archive store
360
+ * and the sandbox is shut down; memory and running processes are lost, and the
361
+ * saved processes start again from their configuration when the sandbox is
362
+ * unarchived. The export runs in the background: this waits until the sandbox
363
+ * is ARCHIVED, pass `{ wait: false }` to return as soon as it is launched.
364
+ */
365
+ static async archive(sandboxName, options = {}) {
366
+ const { data } = await archiveSandbox({
367
+ path: { sandboxName },
368
+ throwOnError: true,
369
+ });
370
+ return SandboxInstance.waitForArchiveStatus(sandboxName, data, "ARCHIVED", ARCHIVING_STATUSES, ARCHIVE_ENTRY_STATUS, "archive", options);
371
+ }
372
+ /**
373
+ * Archive this sandbox: keep its filesystem, stop the sandbox.
374
+ *
375
+ * @see SandboxInstance.archive
376
+ */
377
+ async archive(options = {}) {
378
+ const instance = await SandboxInstance.archive(this.metadata.name, options);
379
+ this.refreshFrom(instance);
380
+ return this;
381
+ }
382
+ /**
383
+ * Recreate an archived sandbox from its archive.
384
+ *
385
+ * The sandbox is started again from its image, and the archived filesystem is
386
+ * written back over it. The sandbox answers, and its terminal is reachable, while the archived
387
+ * filesystem is written back over its image. This waits until the restore is
388
+ * done and the saved processes are running again; pass `{ wait: false }` to
389
+ * return while the sandbox is still UNARCHIVING.
390
+ */
391
+ static async unarchive(sandboxName, options = {}) {
392
+ const { data } = await unarchiveSandbox({
393
+ path: { sandboxName },
394
+ throwOnError: true,
395
+ });
396
+ return SandboxInstance.waitForArchiveStatus(sandboxName, data, "DEPLOYED", UNARCHIVING_STATUSES, UNARCHIVE_ENTRY_STATUS, "unarchive", options);
397
+ }
398
+ /**
399
+ * Recreate this sandbox from its archive.
400
+ *
401
+ * @see SandboxInstance.unarchive
402
+ */
403
+ async unarchive(options = {}) {
404
+ const instance = await SandboxInstance.unarchive(this.metadata.name, options);
405
+ this.refreshFrom(instance);
406
+ return this;
407
+ }
408
+ // The subsystems (fs, process, previews, ...) hold the configuration object
409
+ // this instance was built with, so a refresh writes into it rather than
410
+ // replacing it. Anything the read did not carry, the forced URL of a session
411
+ // above all, is kept.
412
+ refreshFrom(instance) {
413
+ Object.assign(this.sandbox, instance.sandbox);
414
+ }
415
+ static async waitForArchiveStatus(sandboxName, launched, target, pending, entry, action, { wait = true, maxWait = ARCHIVE_MAX_WAIT_MS, interval = ARCHIVE_WAIT_POLL_MS }) {
416
+ if (!wait || launched.status === target) {
417
+ return SandboxInstance.attachH2Session(new SandboxInstance(launched));
418
+ }
419
+ const deadline = Date.now() + maxWait;
420
+ // The status the sandbox is given back at is tolerated while the operation
421
+ // turns into a status change, but never past the wait the caller asked for.
422
+ const entryDeadline = Date.now() + Math.min(ARCHIVE_ENTRY_MAX_WAIT_MS, maxWait);
423
+ let started = false;
424
+ for (;;) {
425
+ await new Promise((resolve) => setTimeout(resolve, interval));
426
+ const instance = await SandboxInstance.get(sandboxName);
427
+ const status = instance.status;
428
+ if (status === target) {
429
+ return instance;
430
+ }
431
+ if (pending.has(status ?? "")) {
432
+ started = true;
433
+ }
434
+ else if (status === entry && !started && Date.now() < entryDeadline) {
435
+ continue;
436
+ }
437
+ if (!pending.has(status ?? "")) {
438
+ throw new Error(`Sandbox ${sandboxName} is ${status} while it should ${action}`);
439
+ }
440
+ if (Date.now() >= deadline) {
441
+ throw new Error(`Sandbox ${sandboxName} is still ${status} after waiting ${Math.round(maxWait / 1000)}s for it to ${action}`);
442
+ }
443
+ }
444
+ }
321
445
  static async get(sandboxName) {
322
446
  const { data } = await getSandbox({
323
447
  path: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaxel/core",
3
- "version": "0.3.13-preview.270",
3
+ "version": "0.3.17",
4
4
  "description": "Blaxel Core SDK for TypeScript",
5
5
  "license": "MIT",
6
6
  "author": "Blaxel, INC (https://blaxel.ai)",