@sandboxrouter/sdk 0.8.2 → 0.8.3

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/bench.d.ts CHANGED
@@ -11,7 +11,9 @@ export interface BenchRow {
11
11
  }
12
12
  /**
13
13
  * Cold start = time from create() call to a usable sandbox. Exec = round trip of `echo ok`.
14
- * Measured from wherever you run it, so network distance to each provider counts.
14
+ * Measured from wherever you run it, so network distance to each provider counts. Every provider is
15
+ * asked for the same size (2 vCPU / 4 GB unless `spec` says otherwise), except ones whose default
16
+ * image fixes the size, which run at that size.
15
17
  */
16
18
  export declare function bench(opts: {
17
19
  router?: Router;
package/dist/bench.js CHANGED
@@ -1,14 +1,18 @@
1
- import { CATALOG, fixedSize, hourlyCost } from "./catalog.js";
1
+ import { CATALOG, DEFAULT_MEMORY_GB, DEFAULT_VCPU, fixedSize, hourlyCost } from "./catalog.js";
2
2
  import { loadBench, median, saveBench } from "./bench-store.js";
3
3
  import { Router } from "./router.js";
4
4
  /**
5
5
  * Cold start = time from create() call to a usable sandbox. Exec = round trip of `echo ok`.
6
- * Measured from wherever you run it, so network distance to each provider counts.
6
+ * Measured from wherever you run it, so network distance to each provider counts. Every provider is
7
+ * asked for the same size (2 vCPU / 4 GB unless `spec` says otherwise), except ones whose default
8
+ * image fixes the size, which run at that size.
7
9
  */
8
10
  export async function bench(opts) {
9
11
  const router = opts.router ?? new Router();
10
12
  const runs = opts.runs ?? 3;
11
- const spec = opts.spec ?? { timeoutMinutes: 5 };
13
+ // The CLI passes unset flags as undefined: drop them so they don't wipe the defaults.
14
+ const given = Object.fromEntries(Object.entries(opts.spec ?? {}).filter(([, v]) => v !== undefined));
15
+ const spec = { vcpu: DEFAULT_VCPU, memoryGb: DEFAULT_MEMORY_GB, timeoutMinutes: 5, ...given };
12
16
  const ids = opts.providers ?? router.registered().filter((id) => router.isConfigured(id));
13
17
  const log = opts.onProgress ?? (() => { });
14
18
  const stored = loadBench(opts.benchFile);
@@ -23,7 +27,7 @@ export async function bench(opts) {
23
27
  let box;
24
28
  try {
25
29
  const t0 = performance.now();
26
- box = await router.create({ ...fixedSize(id), ...spec }, { provider: id, fallback: false });
30
+ box = await router.create(sized(id, spec), { provider: id, fallback: false });
27
31
  createMs.push(Math.round(performance.now() - t0));
28
32
  const t1 = performance.now();
29
33
  const r = await box.exec("echo ok");
@@ -46,13 +50,17 @@ export async function bench(opts) {
46
50
  failures,
47
51
  medianCreateMs: median(createMs),
48
52
  medianExecMs: median(execMs),
49
- hourlyUsd: hourlyCost(id, { ...fixedSize(id), ...spec }),
53
+ hourlyUsd: hourlyCost(id, sized(id, spec)),
50
54
  lastError,
51
55
  });
52
56
  }
53
57
  saveBench(stored, opts.benchFile);
54
58
  return rows.sort((a, b) => (a.medianCreateMs ?? Infinity) - (b.medianCreateMs ?? Infinity));
55
59
  }
60
+ /** The spec a provider is benched at: the asked size, or its fixed default size if its image has one. */
61
+ function sized(id, spec) {
62
+ return { ...spec, ...fixedSize(id, spec) };
63
+ }
56
64
  export function benchMarkdown(rows) {
57
65
  const name = (id) => CATALOG.find((c) => c.id === id).name;
58
66
  const ms = (n) => (n === undefined ? "n/a" : `${n} ms`);
package/dist/cli.js CHANGED
@@ -309,7 +309,7 @@ async function main() {
309
309
  console.error(`\n${r.provider} last error: ${r.lastError}`);
310
310
  if (values.share) {
311
311
  const version = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
312
- const report = makeReport(rows, { version, runs: rows[0].runs, spec, label: values.label, by });
312
+ const report = makeReport(rows, { version, runs: rows[0].runs, spec: { ...spec, vcpu: spec.vcpu ?? DEFAULT_VCPU, memoryGb: spec.memoryGb ?? DEFAULT_MEMORY_GB }, label: values.label, by });
313
313
  const left = rows.filter((r) => !benchmarksPublishable(r.provider)).map((r) => r.provider);
314
314
  if (left.length)
315
315
  console.log(`\nleft out of the shared report: ${left.join(", ")} (their terms don't allow publishing benchmarks)`);
@@ -41,8 +41,13 @@ export interface LeaderRow {
41
41
  vcpu: number;
42
42
  memoryGb: number;
43
43
  };
44
+ /** Date (ISO) of the newest run behind this row. */
45
+ measuredAt: string;
44
46
  }
45
- /** Median of each submission's medians, so one heavy submitter can't skew a provider. */
47
+ /**
48
+ * Each provider's latest run from each location (report label), then the median across locations, so
49
+ * an old run can't outvote a newer one and one heavy submitter can't skew a provider.
50
+ */
46
51
  export declare function aggregate(reports: BenchReport[]): LeaderRow[];
47
52
  /** One self-contained HTML page: a sorted table with an inline bar per measure, each on its own scale. */
48
53
  export declare function renderLeaderboard(reports: BenchReport[]): string;
@@ -40,14 +40,27 @@ export function loadReports(dir) {
40
40
  return out;
41
41
  }
42
42
  const KNOWN = new Set(CATALOG.map((c) => c.id));
43
- /** Median of each submission's medians, so one heavy submitter can't skew a provider. */
43
+ /**
44
+ * Each provider's latest run from each location (report label), then the median across locations, so
45
+ * an old run can't outvote a newer one and one heavy submitter can't skew a provider.
46
+ */
44
47
  export function aggregate(reports) {
48
+ const latest = new Map();
49
+ for (const rep of reports) {
50
+ for (const r of rep.rows) {
51
+ if (!KNOWN.has(r.provider))
52
+ continue;
53
+ const key = `${r.provider}\u0000${rep.label ?? ""}`;
54
+ const seen = latest.get(key);
55
+ if (!seen || rep.date > seen.date)
56
+ latest.set(key, { row: r, date: rep.date });
57
+ }
58
+ }
45
59
  const by = new Map();
46
- for (const rep of reports)
47
- for (const r of rep.rows)
48
- if (KNOWN.has(r.provider))
49
- by.set(r.provider, [...(by.get(r.provider) ?? []), r]);
50
- const rows = [...by.entries()].map(([id, rs]) => {
60
+ for (const x of latest.values())
61
+ by.set(x.row.provider, [...(by.get(x.row.provider) ?? []), x]);
62
+ const rows = [...by.entries()].map(([id, xs]) => {
63
+ const rs = xs.map((x) => x.row);
51
64
  const runs = rs.reduce((n, r) => n + r.runs, 0);
52
65
  const ok = rs.reduce((n, r) => n + (r.runs - r.failures), 0);
53
66
  return {
@@ -59,6 +72,7 @@ export function aggregate(reports) {
59
72
  successRate: runs ? ok / runs : 0,
60
73
  hourlyUsd: hourlyCost(id, fittingSpec(id)),
61
74
  size: fittingSpec(id),
75
+ measuredAt: xs.map((x) => x.date).sort().at(-1),
62
76
  };
63
77
  });
64
78
  return rows.sort((a, b) => (a.medianCreateMs ?? Infinity) - (b.medianCreateMs ?? Infinity));
package/dist/router.d.ts CHANGED
@@ -80,6 +80,15 @@ export interface RouterOptions {
80
80
  factor?: number;
81
81
  minSeconds?: number;
82
82
  };
83
+ /**
84
+ * What a provider's 401 or 403 (or a missing provider package) does to a create. "stop" (default):
85
+ * the create fails, because with your own keys that is a key or setup you need to fix, and moving the
86
+ * work elsewhere would hide it. "fallback": try the next provider. For services that run sandboxes on
87
+ * their own accounts, where a revoked key, a frozen workspace or a provider's creation cap is the
88
+ * service's problem, not the caller's. 400, 404 and 422 still stop: those are usually the request.
89
+ * Either way the failed try is in `attempts`.
90
+ */
91
+ accountErrors?: "stop" | "fallback";
83
92
  }
84
93
  /** Desktop-only providers. Filled in as adapters are added. */
85
94
  export declare function defaultComputerProviders(): ComputerProvider[];
@@ -95,6 +104,7 @@ export declare class Router {
95
104
  private readonly live;
96
105
  private computerOnly;
97
106
  private readonly quick;
107
+ private readonly accountErrors;
98
108
  /** The local record of sandboxes this router created, or undefined when turned off. */
99
109
  readonly ledger?: Ledger;
100
110
  constructor(opts?: RouterOptions);
package/dist/router.js CHANGED
@@ -104,6 +104,7 @@ export class Router {
104
104
  live = new Map();
105
105
  computerOnly;
106
106
  quick;
107
+ accountErrors;
107
108
  /** The local record of sandboxes this router created, or undefined when turned off. */
108
109
  ledger;
109
110
  constructor(opts = {}) {
@@ -119,6 +120,7 @@ export class Router {
119
120
  opts.billedVsList ?? Object.fromEntries(Object.entries(BILLED_VS_LIST).map(([id, b]) => [id, b.ratio]));
120
121
  this.computerOnly = opts.computers ?? (opts.providers ? [] : defaultComputerProviders());
121
122
  this.quick = opts.quickFallback === false ? false : { factor: 5, minSeconds: 10, ...opts.quickFallback };
123
+ this.accountErrors = opts.accountErrors ?? "stop";
122
124
  }
123
125
  /** Registered and has credentials. False for providers this router doesn't know. */
124
126
  isConfigured(id) {
@@ -271,7 +273,9 @@ export class Router {
271
273
  const status = errorStatus(err);
272
274
  attempts.push({ provider: c.provider, ok: false, ms: Date.now() - t0, error: err.message, ...(status ? { status } : {}) });
273
275
  const setup = err instanceof SetupError || (status !== undefined && SETUP_ERRORS.has(status));
274
- if (route.fallback !== "any" && setup) {
276
+ // On the router's own accounts, a key, permission or install problem is ours: move on.
277
+ const account = err instanceof SetupError || status === 401 || status === 403;
278
+ if (route.fallback !== "any" && setup && !(this.accountErrors === "fallback" && account)) {
275
279
  const msg = err.message.replace(/[.\s]+$/, "");
276
280
  throw new RouterError(`${msg.startsWith(c.provider) ? msg : `${c.provider}: ${msg}`}. This looks like a setup problem with ${c.provider} (key, image, request or a missing package), ` +
277
281
  `so the router stopped instead of moving the work to another provider; pass fallback: "any" to fall back on every error`, attempts, "setup");
package/dist/types.d.ts CHANGED
@@ -136,7 +136,8 @@ export declare class ProviderError extends Error {
136
136
  }
137
137
  /**
138
138
  * A problem with local setup, such as a provider SDK that isn't installed or a missing setting:
139
- * something to fix here, not a provider being down. The router stops on it instead of falling back.
139
+ * something to fix here, not a provider being down. The router stops on it instead of falling back, unless it
140
+ * was made with `accountErrors: "fallback"`.
140
141
  */
141
142
  export declare class SetupError extends Error {
142
143
  constructor(message: string);
package/dist/types.js CHANGED
@@ -21,7 +21,8 @@ export class ProviderError extends Error {
21
21
  }
22
22
  /**
23
23
  * A problem with local setup, such as a provider SDK that isn't installed or a missing setting:
24
- * something to fix here, not a provider being down. The router stops on it instead of falling back.
24
+ * something to fix here, not a provider being down. The router stops on it instead of falling back, unless it
25
+ * was made with `accountErrors: "fallback"`.
25
26
  */
26
27
  export class SetupError extends Error {
27
28
  constructor(message) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sandboxrouter/sdk",
3
- "version": "0.8.2",
3
+ "version": "0.8.3",
4
4
  "description": "One API for every sandbox provider: route AI agents' code by price, speed or capability, with automatic fallback. Free and open source.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",