@quietrobot/sandboxes 0.2.0 → 0.2.1

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/README.md CHANGED
@@ -91,7 +91,7 @@ await sbx.pause(); // memory and processes ke
91
91
  await sbx.resume();
92
92
  const snap = await sbx.snapshot("with-deps");
93
93
  const copies = await sbx.fork({ count: 3 }); // three copies of this machine, as it is now
94
- const [big] = (await sbx.fork({ cpus: 4, memoryMb: 8192 })).sandboxes; // resize: a bigger copy
94
+ const big = await sbx.resize({ cpus: 4, memoryMb: 8192 }); // running or paused: a copy at the new size, original destroyed
95
95
  await sbx.rewind({ snapshotId: snap.id }); // files back as they were in the snapshot
96
96
  const fresh = await qr.create({ snapshotId: snap.id });
97
97
  ```
@@ -106,6 +106,18 @@ const fresh = await qr.create({ snapshotId: snap.id });
106
106
  `ifExists`, `volumes`.
107
107
  - Account: `qr.snapshots()`, `qr.deleteSnapshot(id)`, `qr.reservations()`,
108
108
  `qr.usage(days)`, `qr.audit()`, `qr.secrets`, `qr.volumes`, `qr.webhooks`.
109
+ - Templates: start from one of ours with `template: "code-interpreter"` (or
110
+ `browser`, `desktop`, `coding-agent`, `fullstack-js`, `go`, `rust`,
111
+ `postgres`), or build your own from a Dockerfile:
112
+ `await qr.createTemplate({ name: "my-agent", dockerfile })` waits for the
113
+ build; also `qr.templates()`, `qr.template(name)`, `qr.rebuildTemplate()`,
114
+ `qr.updateTemplate()`, `qr.deleteTemplate()`, `qr.templateBuild(id)`.
115
+ - Outbound learn mode: `sbx.learnEgress()`, run your workload,
116
+ `sbx.learnedEgress()`, then `sbx.applyLearnedEgress()` (allow only what it
117
+ reached) or `sbx.stopLearningEgress()`.
118
+ - After the fact: `sbx.report()` (or `sbx.reportPdf()`) says what the agent
119
+ did; `qr.undo(executionId)` puts the files back to just before a command;
120
+ `sbx.image()` lists the exact package versions; `sbx.setSpendCap(dollars)`.
109
121
  - Webhooks: `await verifyWebhook(secret, headers, rawBody)` checks the
110
122
  signature and returns the event, or throws.
111
123
 
package/dist/index.d.ts CHANGED
@@ -13,6 +13,51 @@ export declare const DEFAULT_PREVIEW_DOMAIN = "quietrobot.dev";
13
13
  /** The sandbox user's home folder. Relative paths (files and cwd) are relative to it. */
14
14
  export declare const HOME = "/home/sandbox";
15
15
  export type EgressMode = "deny" | "allowlist" | "open";
16
+ /** Our templates. Any name you gave one of your own templates works too. */
17
+ export type OfficialTemplate = "base" | "minimal" | "code-interpreter" | "browser" | "desktop" | "coding-agent" | "fullstack-js" | "go" | "rust" | "postgres";
18
+ /** A template you built from a Dockerfile (POST /templates). */
19
+ export interface TemplateInfo {
20
+ id: string;
21
+ name: string;
22
+ description: string | null;
23
+ /** ready once a build has finished; a template keeps its last good image while a rebuild runs. */
24
+ status: "building" | "ready" | "failed";
25
+ /** Which of our templates it's built on. */
26
+ base: OfficialTemplate | null;
27
+ sizeBytes: number | null;
28
+ warnings?: string[];
29
+ builds?: TemplateBuild[];
30
+ [k: string]: unknown;
31
+ }
32
+ export interface TemplateBuild {
33
+ id: string;
34
+ templateId: string | null;
35
+ status: "queued" | "building" | "sealing" | "ready" | "failed" | "cancelled";
36
+ base: string;
37
+ error: string | null;
38
+ sizeBytes: number | null;
39
+ /** The end of the build output (templateBuild() only). */
40
+ log?: string;
41
+ createdAt: string;
42
+ startedAt: string | null;
43
+ endedAt: string | null;
44
+ }
45
+ /** One of our templates, with its default size and the helper that starts its long-running part. */
46
+ export interface OfficialTemplateInfo {
47
+ name: OfficialTemplate;
48
+ title?: string;
49
+ [k: string]: unknown;
50
+ }
51
+ /** What learn mode has seen, as allow rules. */
52
+ export interface EgressSuggestions {
53
+ since: string | null;
54
+ how: "open" | "watch" | null;
55
+ suggestions: {
56
+ rule: string;
57
+ [k: string]: unknown;
58
+ }[];
59
+ [k: string]: unknown;
60
+ }
16
61
  export interface SandboxInfo {
17
62
  id: string;
18
63
  name: string | null;
@@ -182,7 +227,8 @@ export interface CodeRun {
182
227
  }
183
228
  export interface CreateOptions {
184
229
  name?: string;
185
- template?: string;
230
+ /** One of ours ("base" by default, or "code-interpreter", "browser", ...) or the name of one you built. */
231
+ template?: OfficialTemplate | (string & {});
186
232
  /** standard = shared CPU (cheaper), performance = dedicated CPU (Pay as you go with a card). */
187
233
  cpuClass?: "standard" | "performance";
188
234
  cpus?: 1 | 2 | 4 | 8 | 16;
@@ -369,6 +415,48 @@ export declare class Sandboxes {
369
415
  deleteSnapshot(id: string): Promise<SnapshotInfo>;
370
416
  reservations(): Promise<Reservation[]>;
371
417
  execution(id: string): Promise<Execution>;
418
+ /** Your templates, and ours that can start now. */
419
+ templates(): Promise<{
420
+ templates: TemplateInfo[];
421
+ official: OfficialTemplateInfo[];
422
+ }>;
423
+ /** One of your templates by id or name, with its recent builds. */
424
+ template(idOrName: string): Promise<TemplateInfo>;
425
+ /**
426
+ * Builds your own template from a Dockerfile. Its last stage must start FROM
427
+ * one of ours (quietrobot/base, quietrobot/browser, ...). There is no build
428
+ * context: fetch files with RUN or ADD a URL. Builds are billed at Standard
429
+ * rates. wait: true (the default) waits until the build is ready or fails.
430
+ */
431
+ createTemplate(opts: {
432
+ name: string;
433
+ dockerfile: string;
434
+ description?: string;
435
+ warmPool?: number;
436
+ wait?: boolean;
437
+ timeoutMs?: number;
438
+ }): Promise<TemplateInfo>;
439
+ /** Builds a template again, with a new Dockerfile or the same one. */
440
+ rebuildTemplate(idOrName: string, dockerfile?: string): Promise<TemplateBuild>;
441
+ updateTemplate(idOrName: string, changes: {
442
+ description?: string | null;
443
+ warmPool?: number;
444
+ }): Promise<TemplateInfo>;
445
+ deleteTemplate(idOrName: string): Promise<TemplateInfo>;
446
+ /** A build and the end of its log. */
447
+ templateBuild(id: string): Promise<TemplateBuild>;
448
+ /** Waits until a template's build is ready (returns it) or failed (throws with the build's error). Default 30 minutes. */
449
+ waitForTemplate(idOrName: string, timeoutMs?: number): Promise<TemplateInfo>;
450
+ /**
451
+ * Undoes a command: puts the sandbox's files back as they were just before
452
+ * it ran. Works for commands that were held for approval or ran with sudo,
453
+ * which save the files first (a setting on the dashboard's Policy page);
454
+ * others answer 409. The current files are saved as a snapshot first.
455
+ */
456
+ undo(executionId: string): Promise<{
457
+ sandbox: SandboxInfo;
458
+ savedSnapshot: SnapshotInfo | null;
459
+ }>;
372
460
  /** This month's usage and limits, or the last `days` days. */
373
461
  usage(days?: number): Promise<Record<string, unknown>>;
374
462
  audit(opts?: {
@@ -542,6 +630,17 @@ export declare class Sandbox {
542
630
  sandboxes: Sandbox[];
543
631
  errors: unknown[];
544
632
  }>;
633
+ /**
634
+ * Gives this sandbox a new size. A running or paused sandbox can't change size in
635
+ * place, so this copies it (fork with the new size), destroys the original and
636
+ * returns the copy: a new id, the same files, name and rules. Processes and memory
637
+ * aren't carried over. A paused original gives a paused copy.
638
+ */
639
+ resize(size: {
640
+ cpuClass?: CreateOptions["cpuClass"];
641
+ cpus?: CreateOptions["cpus"];
642
+ memoryMb?: number;
643
+ }): Promise<Sandbox>;
545
644
  /** Puts the files back as they were in a snapshot (the latest one by default). saveCurrent keeps a snapshot of now first. */
546
645
  rewind(opts?: {
547
646
  snapshotId?: string;
@@ -559,6 +658,28 @@ export declare class Sandbox {
559
658
  allow?: string[];
560
659
  deny?: string[];
561
660
  }): Promise<SandboxInfo>;
661
+ /**
662
+ * Learn mode: records the hosts the sandbox reaches so you can turn them
663
+ * into allow rules. Then read learnedEgress() and either applyLearnedEgress()
664
+ * or stopLearningEgress() to put the old rules back.
665
+ */
666
+ learnEgress(): Promise<SandboxInfo>;
667
+ learnedEgress(): Promise<EgressSuggestions>;
668
+ /** Stops learn mode and puts the old rules back. */
669
+ stopLearningEgress(): Promise<SandboxInfo>;
670
+ /** Stops learn mode and allows only these hosts (by default, every suggestion). */
671
+ applyLearnedEgress(allow?: string[]): Promise<SandboxInfo>;
672
+ /**
673
+ * Caps what this sandbox may spend, in dollars (null for none). An API key
674
+ * may only lower the cap; raising or removing it needs a person in the dashboard.
675
+ */
676
+ setSpendCap(capUsd: number | null): Promise<SandboxInfo>;
677
+ /** What the agent did: counts, every approval and who decided it, hosts reached and blocked, files and the timeline. Written from the audit log, no AI model. */
678
+ report(): Promise<Record<string, unknown>>;
679
+ /** The same report as a PDF file. */
680
+ reportPdf(): Promise<Uint8Array>;
681
+ /** The sandbox's image and the exact package versions in it. */
682
+ image(): Promise<Record<string, unknown>>;
562
683
  /** Resets the deadline to now + timeoutSec. */
563
684
  setTimeout(timeoutSec: number): Promise<SandboxInfo>;
564
685
  /** CPU, memory, disk, runtime, storage and egress, with readings over the last `hours` (default 24, up to 168). */
package/dist/index.js CHANGED
@@ -138,6 +138,66 @@ export class Sandboxes {
138
138
  async execution(id) {
139
139
  return this.request("GET", `/executions/${enc(id)}`);
140
140
  }
141
+ /** Your templates, and ours that can start now. */
142
+ async templates() {
143
+ return this.request("GET", "/templates");
144
+ }
145
+ /** One of your templates by id or name, with its recent builds. */
146
+ async template(idOrName) {
147
+ return this.request("GET", `/templates/${enc(idOrName)}`);
148
+ }
149
+ /**
150
+ * Builds your own template from a Dockerfile. Its last stage must start FROM
151
+ * one of ours (quietrobot/base, quietrobot/browser, ...). There is no build
152
+ * context: fetch files with RUN or ADD a URL. Builds are billed at Standard
153
+ * rates. wait: true (the default) waits until the build is ready or fails.
154
+ */
155
+ async createTemplate(opts) {
156
+ const { wait = true, timeoutMs, ...body } = opts;
157
+ const t = await this.request("POST", "/templates", body);
158
+ return wait ? this.waitForTemplate(t.id, timeoutMs) : t;
159
+ }
160
+ /** Builds a template again, with a new Dockerfile or the same one. */
161
+ async rebuildTemplate(idOrName, dockerfile) {
162
+ return this.request("POST", `/templates/${enc(idOrName)}/builds`, dockerfile === undefined ? {} : { dockerfile });
163
+ }
164
+ async updateTemplate(idOrName, changes) {
165
+ return this.request("PATCH", `/templates/${enc(idOrName)}`, changes);
166
+ }
167
+ async deleteTemplate(idOrName) {
168
+ return this.request("DELETE", `/templates/${enc(idOrName)}`);
169
+ }
170
+ /** A build and the end of its log. */
171
+ async templateBuild(id) {
172
+ return this.request("GET", `/template-builds/${enc(id)}`);
173
+ }
174
+ /** Waits until a template's build is ready (returns it) or failed (throws with the build's error). Default 30 minutes. */
175
+ async waitForTemplate(idOrName, timeoutMs = 30 * 60_000) {
176
+ const deadline = Date.now() + timeoutMs;
177
+ let delay = 2000;
178
+ for (;;) {
179
+ const t = await this.template(idOrName);
180
+ if (t.status === "ready")
181
+ return t;
182
+ if (t.status === "failed") {
183
+ const b = t.builds?.[0];
184
+ throw new SandboxesError(422, "build_failed", b?.error ?? `Template ${t.name} failed to build`);
185
+ }
186
+ if (Date.now() > deadline)
187
+ throw new SandboxesError(408, "build_timeout", `Template ${t.name} is still building`);
188
+ await sleep(delay);
189
+ delay = Math.min(delay * 1.5, 10_000);
190
+ }
191
+ }
192
+ /**
193
+ * Undoes a command: puts the sandbox's files back as they were just before
194
+ * it ran. Works for commands that were held for approval or ran with sudo,
195
+ * which save the files first (a setting on the dashboard's Policy page);
196
+ * others answer 409. The current files are saved as a snapshot first.
197
+ */
198
+ async undo(executionId) {
199
+ return this.request("POST", `/executions/${enc(executionId)}/undo`);
200
+ }
141
201
  /** This month's usage and limits, or the last `days` days. */
142
202
  async usage(days) {
143
203
  return this.request("GET", `/usage${query({ days })}`);
@@ -475,6 +535,25 @@ export class Sandbox {
475
535
  const r = await this.client.request("POST", this.path("/fork"), opts);
476
536
  return { snapshot: r.snapshot, sandboxes: r.sandboxes.map((s) => new Sandbox(this.client, s)), errors: r.errors ?? [] };
477
537
  }
538
+ /**
539
+ * Gives this sandbox a new size. A running or paused sandbox can't change size in
540
+ * place, so this copies it (fork with the new size), destroys the original and
541
+ * returns the copy: a new id, the same files, name and rules. Processes and memory
542
+ * aren't carried over. A paused original gives a paused copy.
543
+ */
544
+ async resize(size) {
545
+ if (!size.cpuClass && !size.cpus && !size.memoryMb)
546
+ throw new Error("resize: give cpuClass, cpus or memoryMb");
547
+ const wasPaused = (await this.refresh()).status === "paused";
548
+ const r = await this.fork({ name: this.info.name ?? undefined, cpuClass: size.cpuClass, cpus: size.cpus, memoryMb: size.memoryMb });
549
+ const copy = r.sandboxes[0];
550
+ if (!copy)
551
+ throw new Error("resize: the resized copy couldn't be started; the original is unchanged");
552
+ await this.destroy();
553
+ if (wasPaused)
554
+ await copy.pause();
555
+ return copy;
556
+ }
478
557
  /** Puts the files back as they were in a snapshot (the latest one by default). saveCurrent keeps a snapshot of now first. */
479
558
  async rewind(opts = {}) {
480
559
  const r = await this.client.request("POST", this.path("/rewind"), opts);
@@ -490,6 +569,50 @@ export class Sandbox {
490
569
  this.info = await this.client.request("PUT", this.path("/egress"), egress);
491
570
  return this.info;
492
571
  }
572
+ /**
573
+ * Learn mode: records the hosts the sandbox reaches so you can turn them
574
+ * into allow rules. Then read learnedEgress() and either applyLearnedEgress()
575
+ * or stopLearningEgress() to put the old rules back.
576
+ */
577
+ async learnEgress() {
578
+ this.info = await this.client.request("POST", this.path("/egress/learn"));
579
+ return this.info;
580
+ }
581
+ async learnedEgress() {
582
+ return this.client.request("GET", this.path("/egress/learn"));
583
+ }
584
+ /** Stops learn mode and puts the old rules back. */
585
+ async stopLearningEgress() {
586
+ this.info = await this.client.request("DELETE", this.path("/egress/learn"));
587
+ return this.info;
588
+ }
589
+ /** Stops learn mode and allows only these hosts (by default, every suggestion). */
590
+ async applyLearnedEgress(allow) {
591
+ if (!allow)
592
+ allow = (await this.learnedEgress()).suggestions.map((s) => s.rule);
593
+ this.info = await this.client.request("POST", this.path("/egress/learn/apply"), { allow });
594
+ return this.info;
595
+ }
596
+ /**
597
+ * Caps what this sandbox may spend, in dollars (null for none). An API key
598
+ * may only lower the cap; raising or removing it needs a person in the dashboard.
599
+ */
600
+ async setSpendCap(capUsd) {
601
+ this.info = await this.client.request("PUT", this.path("/spend-cap"), { capUsd });
602
+ return this.info;
603
+ }
604
+ /** What the agent did: counts, every approval and who decided it, hosts reached and blocked, files and the timeline. Written from the audit log, no AI model. */
605
+ async report() {
606
+ return this.client.request("GET", this.path("/report"));
607
+ }
608
+ /** The same report as a PDF file. */
609
+ async reportPdf() {
610
+ return this.client.request("GET", this.path(`/report${query({ format: "pdf" })}`), undefined, true);
611
+ }
612
+ /** The sandbox's image and the exact package versions in it. */
613
+ async image() {
614
+ return this.client.request("GET", this.path("/image"));
615
+ }
493
616
  /** Resets the deadline to now + timeoutSec. */
494
617
  async setTimeout(timeoutSec) {
495
618
  this.info = await this.client.request("POST", this.path("/timeout"), { timeoutSec });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quietrobot/sandboxes",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "TypeScript client for Quiet Robot Sandboxes: governed sandboxes for AI agents",
5
5
  "keywords": [
6
6
  "sandbox",