disk 0.8.23 → 0.9.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.
@@ -111,7 +111,7 @@ function deriveS3BaseUrl(controlBaseUrl) {
111
111
  }
112
112
  //#endregion
113
113
  //#region src/version.ts
114
- const VERSION = "0.8.23";
114
+ const VERSION = "0.9.0";
115
115
  const USER_AGENT = `archil-js/${VERSION}`;
116
116
  //#endregion
117
117
  //#region src/client.ts
@@ -151,6 +151,7 @@ async function unwrapPage(promise) {
151
151
  */
152
152
  async function unwrapEmpty(promise) {
153
153
  const { data: body, error, response } = await promise;
154
+ if (!error && response.status === 204) return;
154
155
  if (error || !body) throw new ArchilApiError(error?.error ?? `API request failed with status ${response.status}`, response.status);
155
156
  if (!body.success) throw new ArchilApiError(body.error ?? "Unknown API error", response.status);
156
157
  }
@@ -166,6 +167,9 @@ var Disk = class {
166
167
  createdAt;
167
168
  fsHandlerStatus;
168
169
  lastAccessed;
170
+ activeDataBytes;
171
+ totalDataBytes;
172
+ /** @deprecated Use {@link activeDataBytes}. */
169
173
  dataSize;
170
174
  monthlyUsage;
171
175
  mounts;
@@ -192,7 +196,9 @@ var Disk = class {
192
196
  this.createdAt = data.createdAt;
193
197
  this.fsHandlerStatus = data.fsHandlerStatus;
194
198
  this.lastAccessed = data.lastAccessed;
195
- this.dataSize = data.dataSize;
199
+ this.activeDataBytes = data.activeDataBytes;
200
+ this.totalDataBytes = data.totalDataBytes;
201
+ this.dataSize = data.activeDataBytes;
196
202
  this.monthlyUsage = data.monthlyUsage;
197
203
  this.mounts = data.mounts;
198
204
  this.metrics = data.metrics;
@@ -214,7 +220,8 @@ var Disk = class {
214
220
  createdAt: this.createdAt,
215
221
  fsHandlerStatus: this.fsHandlerStatus,
216
222
  lastAccessed: this.lastAccessed,
217
- dataSize: this.dataSize,
223
+ activeDataBytes: this.activeDataBytes,
224
+ totalDataBytes: this.totalDataBytes,
218
225
  monthlyUsage: this.monthlyUsage,
219
226
  mounts: this.mounts,
220
227
  metrics: this.metrics,
@@ -408,7 +415,10 @@ var Disk = class {
408
415
  *
409
416
  * Optional `mode` / `uid` / `gid` set the POSIX attributes of the published
410
417
  * file (e.g. `{ mode: 0o644, uid: 1000, gid: 1000 }` for a non-root agent
411
- * sandbox). Defaults are server-side (currently `root:root` mode `0644`).
418
+ * sandbox). Missing parent directories inherit `uid` / `gid` with mode
419
+ * `0755`; existing directories remain unchanged. A key ending in `/` creates
420
+ * an explicit directory marker whose leaf uses the requested attributes.
421
+ * Defaults are `root:root 0644` for files and `root:root 0755` for directories.
412
422
  *
413
423
  * @param key Path on the disk (e.g. "reports/2026-01/data.json")
414
424
  * @param body Contents as a string, Uint8Array/Buffer, or ArrayBuffer
@@ -480,7 +490,8 @@ var Disk = class {
480
490
  *
481
491
  * When the object does not yet exist, optional `mode` / `uid` / `gid` set the
482
492
  * POSIX attributes of the newly created file (same headers as
483
- * {@link putObject}); they have no effect on an append to an existing object.
493
+ * {@link putObject}); missing parents inherit `uid` / `gid` with mode `0755`.
494
+ * Existing files and directories are unchanged.
484
495
  *
485
496
  * @param key Path on the disk (e.g. "logs/app.log")
486
497
  * @param body Bytes to append (string, Uint8Array/Buffer, or ArrayBuffer)
@@ -676,7 +687,7 @@ var Disk = class {
676
687
  ({error, response} = await call(path, init));
677
688
  } catch (transportError) {
678
689
  if (attempt < maxRetries) {
679
- await sleep(s3RetryDelayMs(attempt));
690
+ await sleep$1(s3RetryDelayMs(attempt));
680
691
  continue;
681
692
  }
682
693
  throw transportError;
@@ -684,7 +695,7 @@ var Disk = class {
684
695
  if (!response.ok) {
685
696
  if (isTransientS3Status(response.status) && attempt < maxRetries) {
686
697
  await response.body?.cancel().catch(() => {});
687
- await sleep(s3RetryDelayMs(attempt));
698
+ await sleep$1(s3RetryDelayMs(attempt));
688
699
  continue;
689
700
  }
690
701
  const message = typeof error === "string" ? error : error ? JSON.stringify(error) : "";
@@ -919,7 +930,7 @@ function s3RetryDelayMs(attempt) {
919
930
  const ceiling = Math.min(S3_RETRY_CAP_MS, S3_RETRY_BASE_MS * 2 ** attempt);
920
931
  return Math.random() * ceiling;
921
932
  }
922
- function sleep(ms) {
933
+ function sleep$1(ms) {
923
934
  return new Promise((resolve) => setTimeout(resolve, ms));
924
935
  }
925
936
  /** S3's minimum size for every multipart part but the last (5 MiB). */
@@ -1134,39 +1145,12 @@ var Disks = class {
1134
1145
  };
1135
1146
  }
1136
1147
  };
1148
+ //#endregion
1149
+ //#region src/sandbox.ts
1137
1150
  const POLL_INTERVAL_MS = 500;
1138
- /** @internal */
1139
- function validateTimeoutMs(timeoutMs) {
1140
- if (!Number.isFinite(timeoutMs) || timeoutMs < 0) throw new RangeError("timeoutMs must be a finite, non-negative number");
1151
+ function sleep() {
1152
+ return new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
1141
1153
  }
1142
- /**
1143
- * The requested SDK-side wait expired. The remote sandbox operation continues;
1144
- * `latest` is the same `Sandbox`/`SandboxExec` the operation was called on,
1145
- * updated in place to the last state observed before the deadline.
1146
- */
1147
- var SandboxWaitTimeoutError = class extends ArchilError {
1148
- operation;
1149
- timeoutMs;
1150
- latest;
1151
- constructor(operation, timeoutMs, latest) {
1152
- const subject = operation === "exec" ? "sandbox exec to complete" : `sandbox to ${operation}`;
1153
- super(`Timed out after ${timeoutMs}ms waiting for ${subject}`, 408, "SANDBOX_WAIT_TIMEOUT");
1154
- this.name = "SandboxWaitTimeoutError";
1155
- this.operation = operation;
1156
- this.timeoutMs = timeoutMs;
1157
- this.latest = latest;
1158
- }
1159
- };
1160
- /** The sandbox entered an inactive state before startup completed. */
1161
- var SandboxStartError = class extends ArchilError {
1162
- latest;
1163
- constructor(sandbox) {
1164
- const detail = sandbox.exitReason ? `: ${sandbox.exitReason}` : "";
1165
- super(`Sandbox entered ${sandbox.status} before it started${detail}`, 409, "SANDBOX_START_FAILED");
1166
- this.name = "SandboxStartError";
1167
- this.latest = sandbox;
1168
- }
1169
- };
1170
1154
  var SandboxExec = class {
1171
1155
  sandboxId;
1172
1156
  id;
@@ -1233,11 +1217,75 @@ var SandboxExec = class {
1233
1217
  return this._apply(data);
1234
1218
  }
1235
1219
  };
1236
- var Sandbox = class {
1220
+ var SandboxPty = class SandboxPty {
1221
+ _socket;
1222
+ _completion;
1223
+ /** @internal */
1224
+ constructor(socket, onData) {
1225
+ this._socket = socket;
1226
+ this._completion = new Promise((resolve) => {
1227
+ socket.addEventListener("message", (event) => {
1228
+ if (typeof event.data === "string") onData?.(event.data);
1229
+ });
1230
+ socket.addEventListener("close", (event) => {
1231
+ const match = /^process exited with code (-?\d+)$/.exec(event.reason);
1232
+ resolve({ exitCode: match ? Number(match[1]) : void 0 });
1233
+ }, { once: true });
1234
+ });
1235
+ }
1236
+ /** @internal */
1237
+ static connect(url, command, options) {
1238
+ return new Promise((resolve, reject) => {
1239
+ const socket = new WebSocket(url);
1240
+ const handleError = () => reject(/* @__PURE__ */ new Error("Interactive exec connection failed"));
1241
+ socket.addEventListener("error", handleError, { once: true });
1242
+ socket.addEventListener("open", () => {
1243
+ socket.removeEventListener("error", handleError);
1244
+ const pty = new SandboxPty(socket, options.onData);
1245
+ pty._send({
1246
+ type: "resize",
1247
+ cols: options.cols ?? 80,
1248
+ rows: options.rows ?? 24
1249
+ });
1250
+ const quotedCommand = `'${command.replaceAll("'", `'"'"'`)}'`;
1251
+ pty._send({
1252
+ type: "input",
1253
+ data: `eval ${quotedCommand}; exit $?\n`
1254
+ });
1255
+ resolve(pty);
1256
+ }, { once: true });
1257
+ });
1258
+ }
1259
+ async sendInput(data) {
1260
+ this._send({
1261
+ type: "input",
1262
+ data
1263
+ });
1264
+ }
1265
+ async resize(size) {
1266
+ this._send({
1267
+ type: "resize",
1268
+ ...size
1269
+ });
1270
+ }
1271
+ wait() {
1272
+ return this._completion;
1273
+ }
1274
+ close() {
1275
+ this._socket.close();
1276
+ }
1277
+ _send(message) {
1278
+ this._socket.send(JSON.stringify(message));
1279
+ }
1280
+ };
1281
+ var Sandbox = class Sandbox {
1237
1282
  id;
1283
+ name;
1238
1284
  status;
1239
1285
  vcpuCount;
1240
1286
  memSizeMiB;
1287
+ baseImage;
1288
+ platform;
1241
1289
  maxTtlSeconds;
1242
1290
  maxConcurrentExecs;
1243
1291
  endpoints;
@@ -1257,9 +1305,12 @@ var Sandbox = class {
1257
1305
  /** @internal Overwrite this sandbox's fields in place from a fresh wire snapshot. */
1258
1306
  _apply(data) {
1259
1307
  this.id = data.sandbox_id;
1308
+ this.name = data.name;
1260
1309
  this.status = data.status;
1261
1310
  this.vcpuCount = data.vcpu_count;
1262
1311
  this.memSizeMiB = data.mem_size_mib;
1312
+ this.baseImage = data.base_image;
1313
+ this.platform = data.platform;
1263
1314
  this.maxTtlSeconds = data.max_ttl_seconds;
1264
1315
  this.maxConcurrentExecs = data.max_concurrent_execs;
1265
1316
  this.endpoints = data.endpoints?.map((endpoint) => ({ ...endpoint }));
@@ -1274,9 +1325,12 @@ var Sandbox = class {
1274
1325
  toJSON() {
1275
1326
  return {
1276
1327
  id: this.id,
1328
+ name: this.name,
1277
1329
  status: this.status,
1278
1330
  vcpuCount: this.vcpuCount,
1279
1331
  memSizeMiB: this.memSizeMiB,
1332
+ baseImage: this.baseImage,
1333
+ platform: this.platform,
1280
1334
  maxTtlSeconds: this.maxTtlSeconds,
1281
1335
  maxConcurrentExecs: this.maxConcurrentExecs,
1282
1336
  endpoints: this.endpoints?.map((endpoint) => ({ ...endpoint })),
@@ -1295,79 +1349,67 @@ var Sandbox = class {
1295
1349
  }
1296
1350
  /** Start this sandbox. */
1297
1351
  async start(options = {}) {
1298
- const timeoutMs = options.timeoutMs ?? 3e4;
1299
- validateTimeoutMs(timeoutMs);
1300
- const deadline = Date.now() + timeoutMs;
1301
1352
  const data = await unwrap(this._client.POST("/api/sandboxes/{sid}/start", { params: {
1302
1353
  path: { sid: this.id },
1303
- query: { wait: false }
1354
+ query: { wait: options.wait ?? true }
1304
1355
  } }));
1305
1356
  this._apply(data);
1306
- return waitForSandboxStart(this, deadline, timeoutMs);
1357
+ return options.wait === false ? this : waitForSandboxStart(this);
1307
1358
  }
1308
1359
  /** Stop this sandbox. */
1309
1360
  async stop(options = {}) {
1310
- const timeoutMs = options.timeoutMs ?? 3e4;
1311
- validateTimeoutMs(timeoutMs);
1312
- const deadline = Date.now() + timeoutMs;
1313
1361
  const data = await unwrap(this._client.POST("/api/sandboxes/{sid}/stop", { params: { path: { sid: this.id } } }));
1314
1362
  this._apply(data);
1315
- for (;;) {
1316
- if ([
1317
- "stopped",
1318
- "exited",
1319
- "failed"
1320
- ].includes(this.status)) return this;
1321
- if (this.status !== "stopping") throw new ArchilError(`Sandbox entered ${this.status} before it stopped`, 409, "SANDBOX_STOP_FAILED");
1322
- const remaining = deadline - Date.now();
1323
- if (remaining <= 0) throw new SandboxWaitTimeoutError("stop", timeoutMs, this);
1324
- await new Promise((resolve) => setTimeout(resolve, Math.min(POLL_INTERVAL_MS, remaining)));
1325
- await this.refresh();
1326
- }
1363
+ return options.wait === false ? this : waitWhileSandboxStatus(this, "stopping");
1327
1364
  }
1328
1365
  /** Pause this sandbox, preserving its CPU and memory state. */
1329
1366
  async pause(options = {}) {
1330
- const timeoutMs = options.timeoutMs ?? 3e4;
1331
- validateTimeoutMs(timeoutMs);
1332
- const deadline = Date.now() + timeoutMs;
1333
1367
  const data = await unwrap(this._client.POST("/api/sandboxes/{sid}/pause", { params: { path: { sid: this.id } } }));
1334
1368
  this._apply(data);
1335
- for (;;) {
1336
- if (this.status === "paused") return this;
1337
- if (this.status !== "pausing") throw new ArchilError(`Sandbox entered ${this.status} before it paused`, 409, "SANDBOX_PAUSE_FAILED");
1338
- const remaining = deadline - Date.now();
1339
- if (remaining <= 0) throw new SandboxWaitTimeoutError("pause", timeoutMs, this);
1340
- await new Promise((resolve) => setTimeout(resolve, Math.min(POLL_INTERVAL_MS, remaining)));
1341
- await this.refresh();
1342
- }
1369
+ return options.wait === false ? this : waitWhileSandboxStatus(this, "pausing");
1343
1370
  }
1344
1371
  /** Resume this sandbox from its preserved CPU and memory state. */
1345
1372
  async resume(options = {}) {
1346
- const timeoutMs = options.timeoutMs ?? 3e4;
1347
- validateTimeoutMs(timeoutMs);
1348
- const deadline = Date.now() + timeoutMs;
1349
1373
  const data = await unwrap(this._client.POST("/api/sandboxes/{sid}/resume", { params: {
1350
1374
  path: { sid: this.id },
1351
- query: { wait: false }
1375
+ query: { wait: options.wait ?? true }
1352
1376
  } }));
1353
1377
  this._apply(data);
1354
- for (;;) {
1355
- if (this.status === "running") return this;
1356
- if (this.status !== "pending") throw new ArchilError(`Sandbox entered ${this.status} before it resumed`, 409, "SANDBOX_RESUME_FAILED");
1357
- const remaining = deadline - Date.now();
1358
- if (remaining <= 0) throw new SandboxWaitTimeoutError("resume", timeoutMs, this);
1359
- await new Promise((resolve) => setTimeout(resolve, Math.min(POLL_INTERVAL_MS, remaining)));
1360
- await this.refresh();
1361
- }
1378
+ return options.wait === false ? this : waitForSandboxStart(this);
1379
+ }
1380
+ /** Create an isolated writable branch from this sandbox's current state. */
1381
+ async fork(options = {}) {
1382
+ const data = await unwrap(this._client.POST("/api/sandboxes/{sid}/fork", {
1383
+ params: {
1384
+ path: { sid: this.id },
1385
+ query: { wait: options.wait ?? true }
1386
+ },
1387
+ body: options.name === void 0 ? void 0 : { name: options.name }
1388
+ }));
1389
+ const fork = new Sandbox(data, this._client);
1390
+ return options.wait === false ? fork : waitForSandboxStart(fork);
1391
+ }
1392
+ /** Create a short-lived signed WebSocket URL for an interactive shell. */
1393
+ async createConnection() {
1394
+ const data = await unwrap(this._client.POST("/api/sandboxes/{sid}/connections", { params: { path: { sid: this.id } } }));
1395
+ return {
1396
+ url: data.url,
1397
+ expiresAt: new Date(data.expires_at)
1398
+ };
1399
+ }
1400
+ /** Delete this sandbox and its backing disk. */
1401
+ async delete() {
1402
+ await unwrapEmpty(this._client.DELETE("/api/sandboxes/{sid}", { params: { path: { sid: this.id } } }));
1362
1403
  }
1363
1404
  async exec(command, options = {}) {
1364
- const timeoutMs = options.timeoutMs ?? 3e4;
1365
- validateTimeoutMs(timeoutMs);
1366
- const deadline = Date.now() + timeoutMs;
1405
+ if ("pty" in options) {
1406
+ const connection = await this.createConnection();
1407
+ return SandboxPty.connect(connection.url, command, options);
1408
+ }
1367
1409
  const exec = new SandboxExec(await unwrap(this._client.POST("/api/sandboxes/{sid}/execs", {
1368
1410
  params: {
1369
1411
  path: { sid: this.id },
1370
- query: { wait: false }
1412
+ query: { wait: options.wait ?? true }
1371
1413
  },
1372
1414
  body: {
1373
1415
  command,
@@ -1376,14 +1418,12 @@ var Sandbox = class {
1376
1418
  timeout_seconds: options.timeoutSeconds
1377
1419
  }
1378
1420
  })), this._client);
1379
- if (exec.status !== "running") return exec;
1380
- for (;;) {
1381
- const remaining = deadline - Date.now();
1382
- if (remaining <= 0) throw new SandboxWaitTimeoutError("exec", timeoutMs, exec);
1383
- await new Promise((resolve) => setTimeout(resolve, Math.min(POLL_INTERVAL_MS, remaining)));
1421
+ if (options.wait === false) return exec;
1422
+ while (exec.status === "running") {
1423
+ await sleep();
1384
1424
  await exec.refresh();
1385
- if (exec.status !== "running") return exec;
1386
1425
  }
1426
+ return exec;
1387
1427
  }
1388
1428
  async listExecs() {
1389
1429
  return ((await unwrap(this._client.GET("/api/sandboxes/{sid}/execs", { params: { path: { sid: this.id } } })))?.execs ?? []).map((exec) => new SandboxExec(exec, this._client));
@@ -1401,16 +1441,16 @@ var Sandbox = class {
1401
1441
  } } })), this._client);
1402
1442
  }
1403
1443
  };
1404
- /** @internal Poll `sandbox` in place until it is running (or the deadline passes). */
1405
- async function waitForSandboxStart(sandbox, deadline, timeoutMs) {
1406
- for (;;) {
1407
- if (sandbox.status === "running") return sandbox;
1408
- if (sandbox.status !== "pending") throw new SandboxStartError(sandbox);
1409
- const remaining = deadline - Date.now();
1410
- if (remaining <= 0) throw new SandboxWaitTimeoutError("start", timeoutMs, sandbox);
1411
- await new Promise((resolve) => setTimeout(resolve, Math.min(POLL_INTERVAL_MS, remaining)));
1444
+ /** @internal Continue waiting if the server returned before startup completed. */
1445
+ async function waitForSandboxStart(sandbox) {
1446
+ return waitWhileSandboxStatus(sandbox, "pending");
1447
+ }
1448
+ async function waitWhileSandboxStatus(sandbox, status) {
1449
+ while (sandbox.status === status) {
1450
+ await sleep();
1412
1451
  await sandbox.refresh();
1413
1452
  }
1453
+ return sandbox;
1414
1454
  }
1415
1455
  //#endregion
1416
1456
  //#region src/sandboxes.ts
@@ -1430,10 +1470,8 @@ var Sandboxes = class {
1430
1470
  return new Sandbox(await unwrap(this._client.GET("/api/sandboxes/{sid}", { params: { path: { sid: id } } })), this._client);
1431
1471
  }
1432
1472
  async create(request = {}, options = {}) {
1433
- const timeoutMs = options.timeoutMs ?? 3e4;
1434
- validateTimeoutMs(timeoutMs);
1435
- const deadline = Date.now() + timeoutMs;
1436
1473
  const body = {
1474
+ name: request.name,
1437
1475
  vcpu_count: request.vcpuCount,
1438
1476
  mem_size_mib: request.memSizeMiB,
1439
1477
  base_image: request.baseImage,
@@ -1441,10 +1479,11 @@ var Sandboxes = class {
1441
1479
  max_ttl_seconds: request.maxTtlSeconds,
1442
1480
  max_concurrent_execs: request.maxConcurrentExecs
1443
1481
  };
1444
- return waitForSandboxStart(new Sandbox(await unwrap(this._client.POST("/api/sandboxes", {
1445
- params: { query: { wait: false } },
1482
+ const sandbox = new Sandbox(await unwrap(this._client.POST("/api/sandboxes", {
1483
+ params: { query: { wait: options.wait ?? true } },
1446
1484
  body
1447
- })), this._client), deadline, timeoutMs);
1485
+ })), this._client);
1486
+ return options.wait === false ? sandbox : waitForSandboxStart(sandbox);
1448
1487
  }
1449
1488
  };
1450
1489
  //#endregion
@@ -1839,4 +1878,4 @@ function workspace(mounts) {
1839
1878
  return archil().workspace(mounts);
1840
1879
  }
1841
1880
  //#endregion
1842
- export { DiskMultipart as C, ArchilApiError as D, VERSION as E, ArchilError as O, Disk as S, USER_AGENT as T, Sandbox as _, deleteApiKey as a, SandboxWaitTimeoutError as b, getSandbox as c, listSandboxes as d, workspace as f, Sandboxes as g, Tokens as h, createSandbox as i, ArchilS3Error as k, listApiKeys as l, Workspace as m, createApiKey as n, exec as o, Archil as p, createDisk as r, getDisk as s, configure as t, listDisks as u, SandboxExec as v, effectiveUploadPartSize as w, Disks as x, SandboxStartError as y };
1881
+ export { effectiveUploadPartSize as C, ArchilError as D, ArchilApiError as E, ArchilS3Error as O, DiskMultipart as S, VERSION as T, Sandbox as _, deleteApiKey as a, Disks as b, getSandbox as c, listSandboxes as d, workspace as f, Sandboxes as g, Tokens as h, createSandbox as i, listApiKeys as l, Workspace as m, createApiKey as n, exec as o, Archil as p, createDisk as r, getDisk as s, configure as t, listDisks as u, SandboxExec as v, USER_AGENT as w, Disk as x, SandboxPty as y };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "disk",
3
- "version": "0.8.23",
3
+ "version": "0.9.0",
4
4
  "description": "Pure-JS client and CLI for Archil disks",
5
5
  "keywords": [
6
6
  "archil",
@@ -38,7 +38,7 @@
38
38
  "LICENSE"
39
39
  ],
40
40
  "dependencies": {
41
- "@archildata/api-types": "^0.0.9",
41
+ "@archildata/api-types": "^0.0.16",
42
42
  "commander": "^14.0.3",
43
43
  "fast-xml-parser": "^5.7.0",
44
44
  "openapi-fetch": "^0.13.5",