@miosa/sdk 3.0.2 → 3.2.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/dist/index.d.ts CHANGED
@@ -7432,14 +7432,27 @@ declare class Sandbox {
7432
7432
  * Returns `true` once the sandbox is ready, `false` on `event: timeout`
7433
7433
  * or when the local timeout elapses before ready.
7434
7434
  *
7435
+ * Throws {@link MiosaError} with code `SANDBOX_BOOT_FAILED` as soon as the
7436
+ * sandbox is observed in a terminal failure state (`error` or `destroyed`)
7437
+ * while waiting, instead of silently exhausting the timeout and returning
7438
+ * `false` — a permanent boot failure and "still booting, keep waiting" must
7439
+ * never look the same to the caller. The readiness SSE stream itself has no
7440
+ * dedicated failure event today (the server only ever emits
7441
+ * `ready`/`status`/`timeout`), so on `event: timeout` this makes one
7442
+ * authoritative {@link readiness} check before giving up, to catch a
7443
+ * failure that landed right at the boundary.
7444
+ *
7435
7445
  * If the SSE endpoint returns 404 (server pre-dates the streaming
7436
- * endpoint) this transparently falls back to polling
7437
- * {@link readiness} every 10 ms until ready or timeout.
7446
+ * endpoint) this transparently falls back to polling {@link readiness}
7447
+ * every 10 ms until ready, timeout, or terminal failure.
7438
7448
  */
7439
7449
  waitUntilReady(options?: {
7440
7450
  timeout?: number;
7441
7451
  stream?: boolean;
7442
7452
  }): Promise<boolean>;
7453
+ private bootFailedError;
7454
+ /** Best-effort final check so an SSE `event: timeout` does not mask a boot failure that just landed. */
7455
+ private throwIfTerminallyFailed;
7443
7456
  /**
7444
7457
  * Readiness answers from the server; `assertRunning` reads the local
7445
7458
  * snapshot. Leaving that snapshot behind meant a caller could await
package/dist/index.js CHANGED
@@ -168,7 +168,7 @@ var TokenRefreshFailedError = class extends MiosaError {
168
168
  };
169
169
 
170
170
  // src/version.ts
171
- var SDK_VERSION = "2.0.7";
171
+ var SDK_VERSION = "3.2.0";
172
172
  var SDK_USER_AGENT = `@miosa/sdk/${SDK_VERSION}`;
173
173
 
174
174
  // src/http.ts
@@ -373,7 +373,7 @@ var HttpClient = class {
373
373
  async *streamFrames(path, options = {}) {
374
374
  const method = options.method ?? "GET";
375
375
  let headers = this.baseHeaders({
376
- Accept: "text/event-stream",
376
+ Accept: "text/event-stream, application/json, */*",
377
377
  ...options.headers
378
378
  });
379
379
  let body5 = null;
@@ -8459,6 +8459,9 @@ var SANDBOX_TIER_BY_CPU = {
8459
8459
  8: { size: "large", memoryMb: 16384 },
8460
8460
  16: { size: "xl", memoryMb: 32768 }
8461
8461
  };
8462
+ function isTerminalSandboxFailure(state) {
8463
+ return state === "error" || state === "destroyed";
8464
+ }
8462
8465
  var AGENT_WORKSPACE_TIMEOUT_SEC = 86400;
8463
8466
  var AGENT_WORKSPACE_IDLE_TIMEOUT_SEC = 1800;
8464
8467
  var AGENT_WORKSPACE_SNAPSHOT_EXPIRATION_DAYS = 30;
@@ -8510,9 +8513,10 @@ function createBody(params = {}) {
8510
8513
  if (legacyPersistencePolicy) metadata.miosa_persistent = persistent;
8511
8514
  const cpuCount = params.cpuCount ?? params.cpu_count;
8512
8515
  const memoryMb = params.memoryMb ?? params.memory_mb;
8516
+ const diskSizeMb = params.diskSizeMb ?? params.disk_size_mb;
8513
8517
  const resolvedSize = params.size;
8514
8518
  if (cpuCount !== void 0 || memoryMb !== void 0) {
8515
- assertPublishedShape(cpuCount, memoryMb);
8519
+ resolveRawShape(cpuCount, memoryMb, diskSizeMb, resolvedSize);
8516
8520
  }
8517
8521
  if (snapshotExpirationSec !== void 0) {
8518
8522
  metadata.snapshot_expiration_sec = snapshotExpirationSec;
@@ -8527,7 +8531,7 @@ function createBody(params = {}) {
8527
8531
  cpu_count: cpuCount,
8528
8532
  memory_mb: memoryMb,
8529
8533
  disk_mb: params.diskMb ?? params.disk_mb,
8530
- disk_size_mb: params.diskSizeMb ?? params.disk_size_mb,
8534
+ disk_size_mb: diskSizeMb,
8531
8535
  timeout_sec: params.timeoutSec ?? params.timeout_sec ?? (legacyPersistencePolicy && persistent === true ? 86400 : void 0),
8532
8536
  idle_timeout_sec: params.idleTimeoutSec ?? params.idle_timeout_sec ?? (legacyPersistencePolicy && persistent === true ? 1800 : void 0),
8533
8537
  always_on: params.alwaysOn ?? params.always_on,
@@ -8558,17 +8562,27 @@ function createBody(params = {}) {
8558
8562
  external_project_id: params.externalProjectId ?? params.external_project_id
8559
8563
  });
8560
8564
  }
8561
- function assertPublishedShape(cpuCount, memoryMb) {
8565
+ function resolveRawShape(cpuCount, memoryMb, diskSizeMb, requestedSize) {
8562
8566
  const tier = cpuCount === void 0 ? void 0 : SANDBOX_TIER_BY_CPU[cpuCount];
8563
- if (tier && tier.memoryMb === memoryMb) return;
8567
+ const matchedSize = tier && tier.memoryMb === memoryMb ? tier.size : void 0;
8568
+ if (matchedSize === void 0 && (cpuCount === void 0 || memoryMb === void 0 || diskSizeMb === void 0)) {
8569
+ raiseUnresolvableShape(cpuCount, memoryMb, tier);
8570
+ }
8571
+ if (requestedSize !== void 0 && requestedSize !== matchedSize) {
8572
+ throw new TypeError(
8573
+ `Raw sandbox resources (${cpuCount} vCPU / ${memoryMb} MiB) do not match requested size "${requestedSize}".`
8574
+ );
8575
+ }
8576
+ }
8577
+ function raiseUnresolvableShape(cpuCount, memoryMb, tier) {
8564
8578
  const supplied = `${cpuCount ?? "?"} vCPU / ${memoryMb ?? "?"} MiB`;
8565
8579
  if (tier) {
8566
8580
  throw new TypeError(
8567
- `Unsupported cpu/memory combination (${supplied}); nearest supported is ${tier.size} (${cpuCount} vCPU / ${tier.memoryMb} MiB). Pass size: "${tier.size}" or memoryMb: ${tier.memoryMb}. Custom disk sizes are allowed on top of any tier.`
8581
+ `Unsupported cpu/memory combination (${supplied}); nearest published tier is ${tier.size} (${cpuCount} vCPU / ${tier.memoryMb} MiB). Pass size: "${tier.size}" or memoryMb: ${tier.memoryMb}, or supply cpuCount, memoryMb, and diskSizeMb together for a custom shape.`
8568
8582
  );
8569
8583
  }
8570
8584
  throw new TypeError(
8571
- `Unsupported cpu/memory combination (${supplied}). Supported tiers: xs (1/2048), small (2/4096), medium (4/8192), large (8/16384), xl (16/32768). Pass a matching cpuCount + memoryMb (any diskSizeMb is allowed) or use size.`
8585
+ `Unsupported cpu/memory combination (${supplied}). Supported tiers: xs (1/2048), small (2/4096), medium (4/8192), large (8/16384), xl (16/32768). Pass a matching cpuCount + memoryMb (any diskSizeMb is allowed), use size, or supply cpuCount, memoryMb, and diskSizeMb together for a custom shape.`
8572
8586
  );
8573
8587
  }
8574
8588
  function execBody(command, options = {}) {
@@ -9421,18 +9435,32 @@ var Sandbox = class _Sandbox {
9421
9435
  * Returns `true` once the sandbox is ready, `false` on `event: timeout`
9422
9436
  * or when the local timeout elapses before ready.
9423
9437
  *
9438
+ * Throws {@link MiosaError} with code `SANDBOX_BOOT_FAILED` as soon as the
9439
+ * sandbox is observed in a terminal failure state (`error` or `destroyed`)
9440
+ * while waiting, instead of silently exhausting the timeout and returning
9441
+ * `false` — a permanent boot failure and "still booting, keep waiting" must
9442
+ * never look the same to the caller. The readiness SSE stream itself has no
9443
+ * dedicated failure event today (the server only ever emits
9444
+ * `ready`/`status`/`timeout`), so on `event: timeout` this makes one
9445
+ * authoritative {@link readiness} check before giving up, to catch a
9446
+ * failure that landed right at the boundary.
9447
+ *
9424
9448
  * If the SSE endpoint returns 404 (server pre-dates the streaming
9425
- * endpoint) this transparently falls back to polling
9426
- * {@link readiness} every 10 ms until ready or timeout.
9449
+ * endpoint) this transparently falls back to polling {@link readiness}
9450
+ * every 10 ms until ready, timeout, or terminal failure.
9427
9451
  */
9428
9452
  async waitUntilReady(options = {}) {
9429
9453
  const timeout = options.timeout ?? 30;
9430
9454
  const stream = options.stream ?? true;
9431
9455
  if (stream) {
9432
9456
  const sseResult = await this.tryReadinessStream(timeout);
9433
- if (sseResult !== null) {
9434
- if (sseResult) await this.adoptReadyState();
9435
- return sseResult;
9457
+ if (sseResult === true) {
9458
+ await this.adoptReadyState();
9459
+ return true;
9460
+ }
9461
+ if (sseResult === false) {
9462
+ await this.throwIfTerminallyFailed();
9463
+ return false;
9436
9464
  }
9437
9465
  }
9438
9466
  const deadlineMs = Date.now() + timeout * 1e3;
@@ -9443,12 +9471,39 @@ var Sandbox = class _Sandbox {
9443
9471
  await this.adoptReadyState();
9444
9472
  return true;
9445
9473
  }
9446
- } catch {
9474
+ if (isTerminalSandboxFailure(data.state)) {
9475
+ throw this.bootFailedError(data.state);
9476
+ }
9477
+ } catch (err) {
9478
+ if (err instanceof MiosaError && err.code === "SANDBOX_BOOT_FAILED") {
9479
+ throw err;
9480
+ }
9447
9481
  }
9448
9482
  await new Promise((resolve) => setTimeout(resolve, 10));
9449
9483
  }
9450
9484
  return false;
9451
9485
  }
9486
+ bootFailedError(state) {
9487
+ return new MiosaError(
9488
+ `Sandbox ${this.id} entered terminal state '${state}' while waiting for readiness`,
9489
+ 502,
9490
+ "SANDBOX_BOOT_FAILED",
9491
+ { sandbox_id: this.id, state }
9492
+ );
9493
+ }
9494
+ /** Best-effort final check so an SSE `event: timeout` does not mask a boot failure that just landed. */
9495
+ async throwIfTerminallyFailed() {
9496
+ try {
9497
+ const data = await this.readiness();
9498
+ if (isTerminalSandboxFailure(data?.state)) {
9499
+ throw this.bootFailedError(data.state);
9500
+ }
9501
+ } catch (err) {
9502
+ if (err instanceof MiosaError && err.code === "SANDBOX_BOOT_FAILED") {
9503
+ throw err;
9504
+ }
9505
+ }
9506
+ }
9452
9507
  /**
9453
9508
  * Readiness answers from the server; `assertRunning` reads the local
9454
9509
  * snapshot. Leaving that snapshot behind meant a caller could await
@@ -9475,7 +9530,11 @@ var Sandbox = class _Sandbox {
9475
9530
  try {
9476
9531
  const headers = {
9477
9532
  Authorization: `Bearer ${this.http.apiKey}`,
9478
- Accept: "text/event-stream",
9533
+ // Combined defensively: a bare `text/event-stream` Accept header 406s
9534
+ // on some `:accepts`-guarded routes (seen on the databases stream
9535
+ // route). Offering json/`*/*` as fallbacks is free when SSE is
9536
+ // actually returned.
9537
+ Accept: "text/event-stream, application/json, */*",
9479
9538
  "User-Agent": SDK_USER_AGENT
9480
9539
  };
9481
9540
  const response = await fetch(