@b3os/sdk 0.1.0 → 0.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.
Files changed (225) hide show
  1. package/README.md +172 -42
  2. package/dist/cjs/core/bytes.d.ts +30 -0
  3. package/dist/cjs/core/bytes.js +115 -0
  4. package/dist/cjs/core/bytes.js.map +1 -0
  5. package/dist/cjs/core/client.d.ts +80 -3
  6. package/dist/cjs/core/client.js +236 -33
  7. package/dist/cjs/core/client.js.map +1 -1
  8. package/dist/cjs/core/errors.d.ts +16 -4
  9. package/dist/cjs/core/errors.js +9 -6
  10. package/dist/cjs/core/errors.js.map +1 -1
  11. package/dist/cjs/core/handle.d.ts +36 -0
  12. package/dist/cjs/core/handle.js +97 -0
  13. package/dist/cjs/core/handle.js.map +1 -0
  14. package/dist/cjs/core/index.d.ts +8 -4
  15. package/dist/cjs/core/index.js +14 -1
  16. package/dist/cjs/core/index.js.map +1 -1
  17. package/dist/cjs/core/login.d.ts +24 -0
  18. package/dist/cjs/core/login.js +93 -0
  19. package/dist/cjs/core/login.js.map +1 -0
  20. package/dist/cjs/core/namespace.d.ts +1 -1
  21. package/dist/cjs/core/namespace.js +1 -1
  22. package/dist/cjs/core/op.d.ts +11 -1
  23. package/dist/cjs/core/timing.d.ts +1 -1
  24. package/dist/cjs/core/timing.js +8 -1
  25. package/dist/cjs/core/timing.js.map +1 -1
  26. package/dist/cjs/core/traces-api.d.ts +42 -0
  27. package/dist/cjs/core/traces-api.js +54 -0
  28. package/dist/cjs/core/traces-api.js.map +1 -0
  29. package/dist/cjs/core/traces.d.ts +120 -0
  30. package/dist/cjs/core/traces.js +306 -0
  31. package/dist/cjs/core/traces.js.map +1 -0
  32. package/dist/cjs/core/transport.js +1 -1
  33. package/dist/cjs/core/transport.js.map +1 -1
  34. package/dist/cjs/core/util.d.ts +15 -3
  35. package/dist/cjs/core/util.js +54 -2
  36. package/dist/cjs/core/util.js.map +1 -1
  37. package/dist/cjs/core/ws.d.ts +4 -0
  38. package/dist/cjs/core/ws.js +7 -4
  39. package/dist/cjs/core/ws.js.map +1 -1
  40. package/dist/cjs/generated/client.d.ts +2 -2
  41. package/dist/cjs/generated/client.js +2 -2
  42. package/dist/cjs/generated/errors.d.ts +1 -1
  43. package/dist/cjs/generated/errors.js +42 -0
  44. package/dist/cjs/generated/errors.js.map +1 -1
  45. package/dist/cjs/generated/ids.d.ts +12 -0
  46. package/dist/cjs/generated/ids.js +3 -0
  47. package/dist/cjs/generated/ids.js.map +1 -1
  48. package/dist/cjs/generated/jobs.js +1 -1
  49. package/dist/cjs/generated/ns/actions.d.ts +48 -6
  50. package/dist/cjs/generated/ns/actions.js +59 -6
  51. package/dist/cjs/generated/ns/actions.js.map +1 -1
  52. package/dist/cjs/generated/ns/infer.d.ts +185 -54
  53. package/dist/cjs/generated/ns/infer.js +28 -74
  54. package/dist/cjs/generated/ns/infer.js.map +1 -1
  55. package/dist/cjs/generated/ns/keys.d.ts +14 -1
  56. package/dist/cjs/generated/ns/keys.js +24 -1
  57. package/dist/cjs/generated/ns/keys.js.map +1 -1
  58. package/dist/cjs/generated/ns/render.d.ts +115 -6
  59. package/dist/cjs/generated/ns/render.js +135 -7
  60. package/dist/cjs/generated/ns/render.js.map +1 -1
  61. package/dist/cjs/generated/ns/requests.d.ts +3 -1
  62. package/dist/cjs/generated/ns/requests.js +3 -1
  63. package/dist/cjs/generated/ns/requests.js.map +1 -1
  64. package/dist/cjs/generated/ns/routines.d.ts +131 -9
  65. package/dist/cjs/generated/ns/routines.js +186 -9
  66. package/dist/cjs/generated/ns/routines.js.map +1 -1
  67. package/dist/cjs/generated/ns/sandboxes.d.ts +327 -31
  68. package/dist/cjs/generated/ns/sandboxes.js +417 -29
  69. package/dist/cjs/generated/ns/sandboxes.js.map +1 -1
  70. package/dist/cjs/generated/ns/vision.d.ts +133 -14
  71. package/dist/cjs/generated/ns/vision.js +158 -12
  72. package/dist/cjs/generated/ns/vision.js.map +1 -1
  73. package/dist/cjs/generated/ns/webhooks.d.ts +8 -5
  74. package/dist/cjs/generated/ns/webhooks.js +8 -5
  75. package/dist/cjs/generated/ns/webhooks.js.map +1 -1
  76. package/dist/cjs/generated/types.d.ts +2750 -328
  77. package/dist/cjs/helpers/agents.d.ts +95 -0
  78. package/dist/cjs/helpers/agents.js +205 -0
  79. package/dist/cjs/helpers/agents.js.map +1 -0
  80. package/dist/cjs/helpers/render.d.ts +15 -0
  81. package/dist/cjs/helpers/render.js +74 -0
  82. package/dist/cjs/helpers/render.js.map +1 -0
  83. package/dist/cjs/helpers/routines.d.ts +61 -0
  84. package/dist/cjs/helpers/routines.js +234 -0
  85. package/dist/cjs/helpers/routines.js.map +1 -0
  86. package/dist/cjs/helpers/sandboxes.d.ts +237 -0
  87. package/dist/cjs/helpers/sandboxes.js +608 -0
  88. package/dist/cjs/helpers/sandboxes.js.map +1 -0
  89. package/dist/cjs/index.d.ts +4 -1
  90. package/dist/cjs/index.js +8 -5
  91. package/dist/cjs/index.js.map +1 -1
  92. package/dist/cjs/routines.d.ts +1 -0
  93. package/dist/cjs/routines.js +21 -0
  94. package/dist/cjs/routines.js.map +1 -0
  95. package/dist/cjs/webhooks.js.map +1 -1
  96. package/dist/esm/core/bytes.d.ts +30 -0
  97. package/dist/esm/core/bytes.js +111 -0
  98. package/dist/esm/core/bytes.js.map +1 -0
  99. package/dist/esm/core/client.d.ts +80 -3
  100. package/dist/esm/core/client.js +233 -31
  101. package/dist/esm/core/client.js.map +1 -1
  102. package/dist/esm/core/errors.d.ts +16 -4
  103. package/dist/esm/core/errors.js +9 -6
  104. package/dist/esm/core/errors.js.map +1 -1
  105. package/dist/esm/core/handle.d.ts +36 -0
  106. package/dist/esm/core/handle.js +93 -0
  107. package/dist/esm/core/handle.js.map +1 -0
  108. package/dist/esm/core/index.d.ts +8 -4
  109. package/dist/esm/core/index.js +6 -2
  110. package/dist/esm/core/index.js.map +1 -1
  111. package/dist/esm/core/login.d.ts +24 -0
  112. package/dist/esm/core/login.js +85 -0
  113. package/dist/esm/core/login.js.map +1 -0
  114. package/dist/esm/core/namespace.d.ts +1 -1
  115. package/dist/esm/core/namespace.js +1 -1
  116. package/dist/esm/core/op.d.ts +11 -1
  117. package/dist/esm/core/timing.d.ts +1 -1
  118. package/dist/esm/core/timing.js +8 -1
  119. package/dist/esm/core/timing.js.map +1 -1
  120. package/dist/esm/core/traces-api.d.ts +42 -0
  121. package/dist/esm/core/traces-api.js +50 -0
  122. package/dist/esm/core/traces-api.js.map +1 -0
  123. package/dist/esm/core/traces.d.ts +120 -0
  124. package/dist/esm/core/traces.js +295 -0
  125. package/dist/esm/core/traces.js.map +1 -0
  126. package/dist/esm/core/transport.js +1 -1
  127. package/dist/esm/core/transport.js.map +1 -1
  128. package/dist/esm/core/util.d.ts +15 -3
  129. package/dist/esm/core/util.js +48 -1
  130. package/dist/esm/core/util.js.map +1 -1
  131. package/dist/esm/core/ws.d.ts +4 -0
  132. package/dist/esm/core/ws.js +7 -4
  133. package/dist/esm/core/ws.js.map +1 -1
  134. package/dist/esm/generated/client.d.ts +2 -2
  135. package/dist/esm/generated/client.js +2 -2
  136. package/dist/esm/generated/errors.d.ts +1 -1
  137. package/dist/esm/generated/errors.js +42 -0
  138. package/dist/esm/generated/errors.js.map +1 -1
  139. package/dist/esm/generated/ids.d.ts +12 -0
  140. package/dist/esm/generated/ids.js +3 -0
  141. package/dist/esm/generated/ids.js.map +1 -1
  142. package/dist/esm/generated/jobs.js +1 -1
  143. package/dist/esm/generated/ns/actions.d.ts +48 -6
  144. package/dist/esm/generated/ns/actions.js +59 -6
  145. package/dist/esm/generated/ns/actions.js.map +1 -1
  146. package/dist/esm/generated/ns/infer.d.ts +185 -54
  147. package/dist/esm/generated/ns/infer.js +28 -74
  148. package/dist/esm/generated/ns/infer.js.map +1 -1
  149. package/dist/esm/generated/ns/keys.d.ts +14 -1
  150. package/dist/esm/generated/ns/keys.js +24 -1
  151. package/dist/esm/generated/ns/keys.js.map +1 -1
  152. package/dist/esm/generated/ns/render.d.ts +115 -6
  153. package/dist/esm/generated/ns/render.js +135 -7
  154. package/dist/esm/generated/ns/render.js.map +1 -1
  155. package/dist/esm/generated/ns/requests.d.ts +3 -1
  156. package/dist/esm/generated/ns/requests.js +3 -1
  157. package/dist/esm/generated/ns/requests.js.map +1 -1
  158. package/dist/esm/generated/ns/routines.d.ts +131 -9
  159. package/dist/esm/generated/ns/routines.js +186 -9
  160. package/dist/esm/generated/ns/routines.js.map +1 -1
  161. package/dist/esm/generated/ns/sandboxes.d.ts +327 -31
  162. package/dist/esm/generated/ns/sandboxes.js +417 -29
  163. package/dist/esm/generated/ns/sandboxes.js.map +1 -1
  164. package/dist/esm/generated/ns/vision.d.ts +133 -14
  165. package/dist/esm/generated/ns/vision.js +158 -12
  166. package/dist/esm/generated/ns/vision.js.map +1 -1
  167. package/dist/esm/generated/ns/webhooks.d.ts +8 -5
  168. package/dist/esm/generated/ns/webhooks.js +8 -5
  169. package/dist/esm/generated/ns/webhooks.js.map +1 -1
  170. package/dist/esm/generated/types.d.ts +2750 -328
  171. package/dist/esm/helpers/agents.d.ts +95 -0
  172. package/dist/esm/helpers/agents.js +199 -0
  173. package/dist/esm/helpers/agents.js.map +1 -0
  174. package/dist/esm/helpers/render.d.ts +15 -0
  175. package/dist/esm/helpers/render.js +70 -0
  176. package/dist/esm/helpers/render.js.map +1 -0
  177. package/dist/esm/helpers/routines.d.ts +61 -0
  178. package/dist/esm/helpers/routines.js +226 -0
  179. package/dist/esm/helpers/routines.js.map +1 -0
  180. package/dist/esm/helpers/sandboxes.d.ts +237 -0
  181. package/dist/esm/helpers/sandboxes.js +600 -0
  182. package/dist/esm/helpers/sandboxes.js.map +1 -0
  183. package/dist/esm/index.d.ts +4 -1
  184. package/dist/esm/index.js +6 -5
  185. package/dist/esm/index.js.map +1 -1
  186. package/dist/esm/routines.d.ts +1 -0
  187. package/dist/esm/routines.js +5 -0
  188. package/dist/esm/routines.js.map +1 -0
  189. package/dist/esm/webhooks.js.map +1 -1
  190. package/package.json +14 -3
  191. package/src/core/bytes.ts +112 -0
  192. package/src/core/client.ts +285 -33
  193. package/src/core/errors.ts +27 -10
  194. package/src/core/handle.ts +107 -0
  195. package/src/core/index.ts +17 -2
  196. package/src/core/login.ts +98 -0
  197. package/src/core/namespace.ts +1 -1
  198. package/src/core/op.ts +11 -1
  199. package/src/core/timing.ts +7 -2
  200. package/src/core/traces-api.ts +80 -0
  201. package/src/core/traces.ts +375 -0
  202. package/src/core/transport.ts +1 -1
  203. package/src/core/util.ts +53 -3
  204. package/src/core/ws.ts +11 -4
  205. package/src/generated/client.ts +2 -2
  206. package/src/generated/errors.ts +84 -0
  207. package/src/generated/ids.ts +3 -0
  208. package/src/generated/jobs.ts +1 -1
  209. package/src/generated/ns/actions.ts +69 -6
  210. package/src/generated/ns/infer.ts +229 -61
  211. package/src/generated/ns/keys.ts +25 -1
  212. package/src/generated/ns/render.ts +172 -7
  213. package/src/generated/ns/requests.ts +3 -1
  214. package/src/generated/ns/routines.ts +205 -9
  215. package/src/generated/ns/sandboxes.ts +493 -34
  216. package/src/generated/ns/vision.ts +168 -14
  217. package/src/generated/ns/webhooks.ts +8 -5
  218. package/src/generated/types.ts +3034 -472
  219. package/src/helpers/agents.ts +300 -0
  220. package/src/helpers/render.ts +91 -0
  221. package/src/helpers/routines.ts +278 -0
  222. package/src/helpers/sandboxes.ts +788 -0
  223. package/src/index.ts +29 -5
  224. package/src/routines.ts +5 -0
  225. package/src/webhooks.ts +5 -1
@@ -1,10 +1,13 @@
1
1
  // Generated by packages/sdk/scripts/generate.mjs from packages/spec/dist. Do not edit; run `pnpm generate`.
2
2
  import { Namespace } from "../../core/namespace.js";
3
3
  import { openSocket, PtySession, ticketedUrl } from "../../core/ws.js";
4
+ import { launchManagedAgentsSession, startCodexExecServer, startManagedAgentsWorker, } from "../../helpers/agents.js";
5
+ import { createSandboxSafely, Sandbox, startService, waitUntilReady, } from "../../helpers/sandboxes.js";
4
6
  const listSandboxesOp = {
5
7
  id: "sandboxesListSandboxes",
6
8
  method: "GET",
7
9
  path: "/v1/sandboxes",
10
+ deepObject: ["metadata"],
8
11
  timeoutMs: 10000,
9
12
  scope: "sandboxes:read",
10
13
  };
@@ -71,21 +74,42 @@ const getSandboxOp = {
71
74
  timeoutMs: 10000,
72
75
  scope: "sandboxes:read",
73
76
  };
77
+ const setEgressOp = {
78
+ id: "sandboxesSetEgress",
79
+ method: "POST",
80
+ path: "/v1/sandboxes/{sandbox_id}/egress",
81
+ params: ["sandbox_id"],
82
+ body: "json",
83
+ timeoutMs: 10000,
84
+ scope: "sandboxes:write",
85
+ };
74
86
  const execOp = {
75
87
  id: "sandboxesExec",
76
88
  method: "POST",
77
89
  path: "/v1/sandboxes/{sandbox_id}/exec",
78
90
  params: ["sandbox_id"],
79
91
  body: "json",
92
+ idem: true,
93
+ callerKey: true,
80
94
  stream: "sse",
81
95
  timeoutMs: 3600000,
82
96
  scope: "sandboxes:run",
83
97
  };
98
+ const deleteFileOp = {
99
+ id: "sandboxesDeleteFile",
100
+ method: "DELETE",
101
+ path: "/v1/sandboxes/{sandbox_id}/files",
102
+ params: ["sandbox_id"],
103
+ flags: ["recursive"],
104
+ timeoutMs: 60000,
105
+ scope: "sandboxes:run",
106
+ };
84
107
  const readFileOp = {
85
108
  id: "sandboxesReadFile",
86
109
  method: "GET",
87
110
  path: "/v1/sandboxes/{sandbox_id}/files",
88
111
  params: ["sandbox_id"],
112
+ flags: ["list"],
89
113
  timeoutMs: 60000,
90
114
  scope: "sandboxes:run",
91
115
  };
@@ -94,11 +118,38 @@ const writeFileOp = {
94
118
  method: "PUT",
95
119
  path: "/v1/sandboxes/{sandbox_id}/files",
96
120
  params: ["sandbox_id"],
121
+ flags: ["mkdir"],
97
122
  body: "bytes",
98
123
  contentType: "application/octet-stream",
99
124
  timeoutMs: 60000,
100
125
  scope: "sandboxes:run",
101
126
  };
127
+ const makeDirectoryOp = {
128
+ id: "sandboxesMakeDirectory",
129
+ method: "POST",
130
+ path: "/v1/sandboxes/{sandbox_id}/files/mkdir",
131
+ params: ["sandbox_id"],
132
+ body: "json",
133
+ timeoutMs: 30000,
134
+ scope: "sandboxes:run",
135
+ };
136
+ const moveFileOp = {
137
+ id: "sandboxesMoveFile",
138
+ method: "POST",
139
+ path: "/v1/sandboxes/{sandbox_id}/files/move",
140
+ params: ["sandbox_id"],
141
+ body: "json",
142
+ timeoutMs: 60000,
143
+ scope: "sandboxes:run",
144
+ };
145
+ const statFileOp = {
146
+ id: "sandboxesStatFile",
147
+ method: "GET",
148
+ path: "/v1/sandboxes/{sandbox_id}/files/stat",
149
+ params: ["sandbox_id"],
150
+ timeoutMs: 30000,
151
+ scope: "sandboxes:run",
152
+ };
102
153
  const forkSandboxOp = {
103
154
  id: "sandboxesForkSandbox",
104
155
  method: "POST",
@@ -107,7 +158,6 @@ const forkSandboxOp = {
107
158
  body: "json",
108
159
  idem: true,
109
160
  timeoutMs: 30000,
110
- stretch: true,
111
161
  scope: "sandboxes:write",
112
162
  };
113
163
  const streamLogsOp = {
@@ -162,6 +212,23 @@ const openPtyOp = {
162
212
  timeoutMs: 3600000,
163
213
  scope: "sandboxes:run",
164
214
  };
215
+ const waitReadyOp = {
216
+ id: "sandboxesWaitReady",
217
+ method: "POST",
218
+ path: "/v1/sandboxes/{sandbox_id}/ready",
219
+ params: ["sandbox_id"],
220
+ body: "json",
221
+ timeoutMs: 65000,
222
+ scope: "sandboxes:run",
223
+ };
224
+ const listRequestsOp = {
225
+ id: "sandboxesListRequests",
226
+ method: "GET",
227
+ path: "/v1/sandboxes/{sandbox_id}/requests",
228
+ params: ["sandbox_id"],
229
+ timeoutMs: 15000,
230
+ scope: "sandboxes:read",
231
+ };
165
232
  const resumeSandboxOp = {
166
233
  id: "sandboxesResumeSandbox",
167
234
  method: "POST",
@@ -189,6 +256,14 @@ const createSnapshotOp = {
189
256
  timeoutMs: 65000,
190
257
  scope: "sandboxes:write",
191
258
  };
259
+ const getStatsOp = {
260
+ id: "sandboxesGetStats",
261
+ method: "GET",
262
+ path: "/v1/sandboxes/{sandbox_id}/stats",
263
+ params: ["sandbox_id"],
264
+ timeoutMs: 10000,
265
+ scope: "sandboxes:read",
266
+ };
192
267
  const createTicketOp = {
193
268
  id: "sandboxesCreateTicket",
194
269
  method: "POST",
@@ -217,9 +292,14 @@ export const sandboxesOps = {
217
292
  getTemplate: getTemplateOp,
218
293
  deleteSandbox: deleteSandboxOp,
219
294
  getSandbox: getSandboxOp,
295
+ setEgress: setEgressOp,
220
296
  exec: execOp,
297
+ deleteFile: deleteFileOp,
221
298
  readFile: readFileOp,
222
299
  writeFile: writeFileOp,
300
+ makeDirectory: makeDirectoryOp,
301
+ moveFile: moveFileOp,
302
+ statFile: statFileOp,
223
303
  forkSandbox: forkSandboxOp,
224
304
  streamLogs: streamLogsOp,
225
305
  pauseSandbox: pauseSandboxOp,
@@ -227,9 +307,12 @@ export const sandboxesOps = {
227
307
  listProcesses: listProcessesOp,
228
308
  killProcess: killProcessOp,
229
309
  openPty: openPtyOp,
310
+ waitReady: waitReadyOp,
311
+ listRequests: listRequestsOp,
230
312
  resumeSandbox: resumeSandboxOp,
231
313
  runCode: runCodeOp,
232
314
  createSnapshot: createSnapshotOp,
315
+ getStats: getStatsOp,
233
316
  createTicket: createTicketOp,
234
317
  setTimeout: setTimeoutOp,
235
318
  };
@@ -240,6 +323,10 @@ export class SandboxesAPI extends Namespace {
240
323
  *
241
324
  * Lists sandboxes in this project, newest first, including stopped ones from the last 7 days.
242
325
  *
326
+ * `metadata[<key>]=<value>` (up to 4 pairs) keeps only sandboxes whose `metadata` holds every pair given, with
327
+ * exactly that value: `GET /v1/sandboxes?metadata[customer]=acme&metadata[ticket]=ENG-1423`. A sandbox whose
328
+ * create lost its answer (a dropped connection) is found this way by a tag it was created with.
329
+ *
243
330
  * `GET /v1/sandboxes` · scope `sandboxes:read` · served by the node
244
331
  */
245
332
  listSandboxes(params, options) {
@@ -258,25 +345,83 @@ export class SandboxesAPI extends Namespace {
258
345
  * explicitly. `api.spend_cap_usd` may not exceed your own spend cap, and `null` means your cap applies; scopes
259
346
  * or a cap beyond yours are refused with `403 forbidden`, `reason: scope_escalation`.
260
347
  *
348
+ * Code in a sandbox reaches other sandboxes through the sandboxes API only within its own sandbox's reach,
349
+ * except where this paragraph says. When it creates one (its `api` grant holds `sandboxes:write`), the new
350
+ * sandbox's `egress` defaults to the creating sandbox's own policy, and a policy that reaches further is
351
+ * refused with `403 forbidden`, `reason: egress_escalation`, as is a public port unless the creating sandbox's
352
+ * egress is `public` (restoring a snapshot and forking are bound the same way). Every operation that sends
353
+ * anything into another sandbox's microVM, to its guest or its ports, answers such code only for a sandbox
354
+ * within its reach: one whose egress is within its own sandbox's, where a sandbox with a public port or with
355
+ * a preview URL that may still serve counts as `public` (the internet reaches those ports whatever its egress),
356
+ * otherwise `403 forbidden`, `reason: egress_escalation`; and one whose `api` grant holds no scope and no
357
+ * connection its own lacks, otherwise `403 forbidden`, `reason: scope_escalation`. These operations are
358
+ * commands (buffered, streamed or in the background) and `run_code`, file reads, listings and writes, the
359
+ * process list, signals, logs, terminals, readiness probes it sets, and port previews (`POST
360
+ * …/ports/{port}/url` and the preview route itself, which the in-VM API also serves). Reach is checked when a
361
+ * call starts, and what stays open is cut when the change that takes the other sandbox out of the caller's
362
+ * reach is made (its egress is widened, a preview is minted for it, the calling sandbox's egress is narrowed or
363
+ * it stops), whether or not the caller is still reading: the node then drops the call's connection into the
364
+ * other sandbox, so the guest kills a buffered or streamed command (its process group) and hangs up a
365
+ * terminal's shell, and nothing either side sends after the change arrives. Data the other sandbox sent before
366
+ * the cut may still reach the caller from the node's buffers; then a buffered command or code run answers `403
367
+ * forbidden`, `reason: egress_escalation`, a streamed command ends with an `exec.exit` event carrying that
368
+ * error, log streams end, file reads end with an error and file writes answer that `403`, a terminal closes
369
+ * with code 1008, and a preview request or WebSocket is cut. What the caller already placed in the other
370
+ * sandbox stays there and no later change reaches it: background processes it started keep running, a readiness
371
+ * probe it set keeps probing, and files it wrote (including the part of a write that arrived before a cut)
372
+ * stay. The sandbox objects it reads carry no preview URL (`ports[].url` is `null`) for sandboxes beyond its
373
+ * reach. The control plane (get, list, pause, resume, timeout, snapshot, delete, waiting on a probe already
374
+ * set, and narrowing another sandbox's egress) is not bound this way, and neither are its own sandbox or
375
+ * templates. Other products reach the network on their own terms: a grant of their scopes (`vision:*`,
376
+ * `actions:*`, `infer:*`, ...) lets code in the sandbox send data out through them, so give a sandbox whose
377
+ * egress is `deny` none of those. Egress `deny` and `allowlist` stop network exfiltration, not timing channels:
378
+ * sandboxes on one node share physical CPU cores, the last-level cache, memory bandwidth and the storage path,
379
+ * so cooperating code in any two sandboxes on a node can signal each other at a low bit rate through CPU-cache,
380
+ * CPU-contention or storage timing (OpenAI described such channels between microVMs:
381
+ * https://openai.com/index/securing-our-frontier-model-training-environment/).
382
+ *
261
383
  * Inside the sandbox the environment also has `B3OS_API_URL=http://127.0.0.1:7700`, `B3OS_SANDBOX_ID`,
262
384
  * `B3OS_PROJECT` and `B3OS_PORT_BASE_PATH=/sbx/<host_id>`. `B3OS_PORT_BASE_PATH` is only the fixed start of
263
385
  * every port preview path on `usercontent.b3os.org`, not a full base path: the full prefix of a request,
264
386
  * `/sbx/<host_id>/<port>/<token>`, arrives with it in `X-Forwarded-Prefix` (`sandboxesProxyPortGet`).
265
387
  *
388
+ * When `api.scopes` include `infer:run` (named, or through `infer:*` or `*`), the environment also points the
389
+ * stock OpenAI and Anthropic SDKs at the node's models: `OPENAI_BASE_URL` and `ANTHROPIC_BASE_URL` are
390
+ * `http://127.0.0.1:7700/v1/infer`, `OPENAI_API_KEY` and `ANTHROPIC_API_KEY` are the placeholder
391
+ * `b3os-sandbox` (the sandbox's grant authenticates the call and the key is ignored), and `B3OS_MODEL` names
392
+ * the node's default model (`b3os/qwen3.5-9b` in the beta). A value you set in `env` for any of them wins.
393
+ * Restores from a snapshot get the same, and forks keep their source's.
394
+ *
266
395
  * `timing` phases: `validate_ms`, then `claim_ms` and `resume_ms` for a pre-warmed microVM from the pool, or
267
396
  * `restore_ms` when none was parked (a restore on demand, or from a snapshot), then `bind_ms` (the identity
268
397
  * and environment reach the guest agent), and `total_ms`.
269
398
  *
270
399
  * A create that has started finishes even if the client disconnects before the response: the sandbox then
271
- * runs (and is billed) until its timeout or a delete, and `GET /v1/sandboxes` lists it with the `metadata`
272
- * it was created with. After a disconnect, look for it there before you retry: a retry is a new create, also
273
- * with the same `Idempotency-Key`, because the key is released when the client goes away before the
274
- * response. Fork, snapshot, pause and resume also finish after a disconnect.
400
+ * runs (and is billed) until its timeout or a delete. Its answer is stored under the `Idempotency-Key` (the
401
+ * SDKs send one), so a retry with that key gets the same sandbox with `Idempotent-Replayed: true`, or `409
402
+ * idempotency_request_in_progress` while the create still runs. Without a key a retry is a new create:
403
+ * `GET /v1/sandboxes` lists the first one with the `metadata` it was created with. Fork, snapshot, pause and
404
+ * resume also finish after a disconnect.
405
+ *
406
+ * A sandbox keeps running when the node's daemon restarts: its microVM runs outside the daemon, which adopts
407
+ * it again when it starts (its processes, files and environment are untouched). Connections that go through
408
+ * the daemon (streamed execs, PTY sessions, log streams, port preview connections, the sandbox's outbound
409
+ * connections and its calls to `http://127.0.0.1:7700`) drop at the restart and can be opened again.
410
+ *
411
+ * Disk: a sandbox's disk is its own copy-on-write copy of the template's (or snapshot's) disk; it is deleted
412
+ * when the sandbox ends and never given to another sandbox. The disks of an organization's running and paused
413
+ * sandboxes on a node add up to at most `sandboxes.disk_total_max` (each sandbox counting its `disk_gib`;
414
+ * beta: `sandboxes.concurrent` times `sandboxes.disk_max`, 160 GiB, so at the beta limits it never refuses a
415
+ * create the concurrency limit allows); past it a create is refused with `422 limit_exceeded`. While the
416
+ * node's disk store is nearly full, creates are refused with `503 capacity_unavailable` and `Retry-After`.
417
+ *
418
+ * Returns the sandbox object (`SandboxInfo`). `create()` sends the same request and returns a `Sandbox`
419
+ * handle instead (`exec`, `files`, `ports`), which `await using` deletes when the scope ends.
275
420
  *
276
421
  * `POST /v1/sandboxes` · scope `sandboxes:write` · served by the node
277
422
  */
278
423
  createSandbox(body, options) {
279
- return this._client._call(createSandboxOp, { body }, options);
424
+ return createSandboxSafely((b, o) => this._client._call(createSandboxOp, { body: b }, o), body, options, this._client.maxRetries);
280
425
  }
281
426
  /**
282
427
  * List snapshots
@@ -311,9 +456,10 @@ export class SandboxesAPI extends Namespace {
311
456
  /**
312
457
  * List templates
313
458
  *
314
- * Lists the templates sandboxes can start from (`base`, `code-interpreter`, `agent`, and on nodes that build it
315
- * `browser`: Chrome already running in the snapshot, a VM-isolated browser with DevTools on port 9222) with their
316
- * contents and defaults.
459
+ * Lists the templates sandboxes can start from (`base`, `code-interpreter`, `agent`, and `browser` where a node
460
+ * builds it, which the beta node does not yet: Chrome already running in the snapshot, a VM-isolated browser
461
+ * with DevTools on port 9222) with their OS, runtimes, capabilities, defaults and `size_classes` (the sizes kept
462
+ * pre-warmed). It does not list a template's packages.
317
463
  *
318
464
  * `GET /v1/sandboxes/templates` · scope `sandboxes:read` · served by the node
319
465
  */
@@ -350,6 +496,26 @@ export class SandboxesAPI extends Namespace {
350
496
  getSandbox(sandboxId, options) {
351
497
  return this._client._call(getSandboxOp, { path: [sandboxId] }, options);
352
498
  }
499
+ /**
500
+ * Change a sandbox's egress policy
501
+ *
502
+ * Replaces the sandbox's `egress` policy (mode and allow list) while it runs or is paused. New outbound
503
+ * connections and DNS answers follow the new policy at once, and open connections it does not allow (SOCKS5,
504
+ * HTTP CONNECT and transparently redirected TCP alike) are closed: under `allowlist`, a connection stays open
505
+ * only if the host name it was opened for is on the new list (one opened by address alone is closed); under
506
+ * `deny`, every connection closes. Private, loopback, tailnet and metadata addresses stay refused in every
507
+ * mode. A sandbox cannot change its own policy (its in-VM API gets `403 forbidden`, `reason: self`, whatever
508
+ * its grant); call from outside it. Code in a sandbox may only narrow another sandbox's policy, to one within
509
+ * both that sandbox's current policy and its own (otherwise `403 forbidden`, `reason: egress_escalation`). A
510
+ * change cuts the streams code in other sandboxes holds open into this sandbox, and those code in this sandbox
511
+ * holds into others, once their target is no longer within the caller's reach (`sandboxesCreateSandbox`); the
512
+ * processes and files such code placed stay.
513
+ *
514
+ * `POST /v1/sandboxes/{sandbox_id}/egress` · scope `sandboxes:write` · served by the node
515
+ */
516
+ setEgress(sandboxId, body, options) {
517
+ return this._client._call(setEgressOp, { path: [sandboxId], body }, options);
518
+ }
353
519
  exec(sandboxId, body, options = {}) {
354
520
  if (options.stream) {
355
521
  return this._client._postStream(execOp, { path: [sandboxId], body }, options, {
@@ -359,8 +525,23 @@ export class SandboxesAPI extends Namespace {
359
525
  }
360
526
  return this._client._call(execOp, { path: [sandboxId], body }, options);
361
527
  }
528
+ /**
529
+ * Delete a file or directory
530
+ *
531
+ * Deletes the file, symlink or empty directory at `path`; with `recursive=1`, a directory and everything in
532
+ * it. A symlink is deleted itself, never its target, and a recursive delete removes the symlinks it finds
533
+ * without following them. A path that is `/` or ends in `.` or `..` is refused, as `rm` refuses it. A
534
+ * recursive delete of a large tree goes on inside the sandbox to its end even when the answer times out.
535
+ * Sandboxes whose template image predates this operation answer `409 guest_outdated`; `rm` through
536
+ * `sandboxesExec` works there.
537
+ *
538
+ * `DELETE /v1/sandboxes/{sandbox_id}/files` · scope `sandboxes:run` · served by the node
539
+ */
540
+ deleteFile(sandboxId, params, options) {
541
+ return this._client._call(deleteFileOp, { path: [sandboxId], query: params }, options);
542
+ }
362
543
  readFile(sandboxId, params, options) {
363
- return params.list === 1
544
+ return params.list === true || params.list === 1
364
545
  ? this._client._call(readFileOp, { path: [sandboxId], query: params }, options, "application/json", "json")
365
546
  : this._client._call(readFileOp, { path: [sandboxId], query: params }, options, "application/octet-stream", "bytes");
366
547
  }
@@ -368,27 +549,71 @@ export class SandboxesAPI extends Namespace {
368
549
  * Write a file
369
550
  *
370
551
  * Writes the request body to `path`, replacing any existing file, creating parent directories with `mkdir=1`.
371
- * Returns the file's entry. Up to 64 MiB per request.
552
+ * Returns the file's entry. Up to 64 MiB per request. The file is written as the body arrives, so a write that
553
+ * fails part-way leaves the part that arrived. From inside another sandbox, a write still streaming when this
554
+ * sandbox leaves the caller's reach is cut: nothing sent after that is written, and the answer is `403
555
+ * forbidden`, `reason: egress_escalation` (`sandboxesCreateSandbox`).
372
556
  *
373
557
  * `PUT /v1/sandboxes/{sandbox_id}/files` · scope `sandboxes:run` · served by the node
374
558
  */
375
559
  writeFile(sandboxId, params, data, options) {
376
560
  return this._client._call(writeFileOp, { path: [sandboxId], query: params, body: data }, options);
377
561
  }
562
+ /**
563
+ * Create a directory
564
+ *
565
+ * Creates the directory at `path` with `mode`, and any missing parents, like `mkdir -p`; each new directory
566
+ * is owned like the one it is made in. Answers `201` with the entry when it created the directory and `200`
567
+ * when a directory (or a symlink to one) was already there; a file at `path` is `409 file_exists`. Sandboxes
568
+ * whose template image predates this operation answer `409 guest_outdated`.
569
+ *
570
+ * `POST /v1/sandboxes/{sandbox_id}/files/mkdir` · scope `sandboxes:run` · served by the node
571
+ */
572
+ makeDirectory(sandboxId, body, options) {
573
+ return this._client._call(makeDirectoryOp, { path: [sandboxId], body }, options);
574
+ }
575
+ /**
576
+ * Move or rename a file or directory
577
+ *
578
+ * Moves `from` to `to` and returns the entry at `to`. An existing `to` is kept and the move refused with
579
+ * `409 file_exists` unless `overwrite` is true; a directory replaces only an empty directory. The directory
580
+ * `to` goes in must exist. Across filesystems inside the sandbox (from `/tmp` to `/workspace`, say) a
581
+ * regular file is copied and its source removed; a directory is refused there. Sandboxes whose template
582
+ * image predates this operation answer `409 guest_outdated`.
583
+ *
584
+ * `POST /v1/sandboxes/{sandbox_id}/files/move` · scope `sandboxes:run` · served by the node
585
+ */
586
+ moveFile(sandboxId, body, options) {
587
+ return this._client._call(moveFileOp, { path: [sandboxId], body }, options);
588
+ }
589
+ /**
590
+ * Describe a file or directory
591
+ *
592
+ * Returns the entry at `path` without reading it: a directory's own entry (where `list=1` on
593
+ * `sandboxesReadFile` lists what is in it), and a symlink as a symlink with its `target`. Sandboxes whose
594
+ * template image predates this operation answer `409 guest_outdated`.
595
+ *
596
+ * `GET /v1/sandboxes/{sandbox_id}/files/stat` · scope `sandboxes:run` · served by the node
597
+ */
598
+ statFile(sandboxId, params, options) {
599
+ return this._client._call(statFileOp, { path: [sandboxId], query: params }, options);
600
+ }
378
601
  /**
379
602
  * Fork a sandbox
380
603
  *
381
604
  * Creates `count` new running sandboxes that share the source's memory and disk copy-on-write, each with a fresh
382
- * identity (its own id, entropy, machine ID and clock; the source's environment, secrets, files and running
383
- * processes carry over). The source must have been created with `forkable: true` (forkable sandboxes are
384
- * restored for themselves rather than claimed from a pool, and track the pages they change); forks are
385
- * forkable too. The source pauses while its changed memory is written (`pause`, `base`, `snapshot`, `export`
386
- * phases in `Server-Timing`), then runs on; like a snapshot this resets its open connections to the outside.
387
- * Each fork counts against your sandbox concurrency.
605
+ * identity (its own id, kernel entropy, machine ID, hostname and clock; the source's environment, secrets, files
606
+ * and running processes carry over, the processes with their in-memory random state, except that the `browser`
607
+ * template's Chrome is reset unless `clone_reset` is `none`, see below). The source must have been created with
608
+ * `forkable: true` (forkable sandboxes are restored for themselves rather than claimed from a pool, and track
609
+ * the pages they change); forks are forkable too. The source pauses while its changed memory is written
610
+ * (`pause`, `base`, `snapshot`, `export` phases in `Server-Timing`), then runs on; like a snapshot this resets
611
+ * its open connections to the outside.
612
+ * Each fork counts against your sandbox concurrency and, with the source's `disk_gib`, against
613
+ * `sandboxes.disk_total_max` (`422 limit_exceeded`). While the node's snapshot or disk store is nearly full,
614
+ * forks are refused with `503 capacity_unavailable`.
388
615
  *
389
616
  * `POST /v1/sandboxes/{sandbox_id}/fork` · scope `sandboxes:write` · served by the node
390
- *
391
- * Stretch: in the contract, but may answer `501 not_implemented` on this deployment.
392
617
  */
393
618
  forkSandbox(sandboxId, body, options) {
394
619
  return this._client._call(forkSandboxOp, { path: [sandboxId], body }, options);
@@ -399,6 +624,8 @@ export class SandboxesAPI extends Namespace {
399
624
  * Server-sent events with the output of background processes: `log.stdout` and `log.stderr` per chunk and
400
625
  * `process.exited` when one ends. Starts with up to 1 MiB of buffered output per process, then follows new
401
626
  * output until the sandbox stops (or immediately ends with `follow=false`). Filter to one process with `pid`.
627
+ * From inside another sandbox (or on a ticket minted there), the stream ends if this sandbox leaves the
628
+ * caller's reach (`sandboxesCreateSandbox`).
402
629
  * Authenticate with the `Authorization` header or a ticket from `POST /v1/sandboxes/{sandbox_id}/ticket`
403
630
  * (`?ticket=`, for `EventSource`).
404
631
  *
@@ -413,9 +640,17 @@ export class SandboxesAPI extends Namespace {
413
640
  /**
414
641
  * Pause a sandbox
415
642
  *
416
- * Freezes the sandbox's vCPUs and keeps its memory and disk on the node. A paused sandbox bills no CPU time
417
- * and still counts allocated memory; it still expires at `expires_at`. Resume continues exactly where it
418
- * stopped. Pausing a paused sandbox returns it unchanged.
643
+ * Freezes the sandbox's vCPUs and keeps its memory and disk on the node. A paused sandbox bills nothing:
644
+ * `sandboxes.vcpu_ms` counts CPU used and `sandboxes.gib_ms` counts memory only while the sandbox runs. It
645
+ * still counts toward `sandboxes.concurrent` and still expires at `expires_at`. Resume continues exactly
646
+ * where it stopped; connections that were open across the pause (the guest's outbound TCP, port preview
647
+ * WebSockets, PTY sessions) keep working. Pausing a paused sandbox returns it unchanged.
648
+ *
649
+ * A workflow that holds a sandbox across a long wait (a routine's `wait` or `sleep` step) pauses it and
650
+ * resumes it before its next exec, so the wait bills no sandbox time.
651
+ *
652
+ * `timing` phases: `admit` (from the request head to the VMM call: middleware, lookup, the sandbox's operation
653
+ * lock and the memory meter), `pause` (the VMM freezing the vCPUs) and `record` (the new status recorded).
419
654
  *
420
655
  * `POST /v1/sandboxes/{sandbox_id}/pause` · scope `sandboxes:write` · served by the node
421
656
  */
@@ -431,7 +666,18 @@ export class SandboxesAPI extends Namespace {
431
666
  * then, with no cookie or other credential. For a public port the response is the sandbox's long-lived URL
432
667
  * (`ports[].url`) and `ttl_s` is ignored. The port must be declared in `ports` when the sandbox is created. The
433
668
  * app sees the URL's base path in `X-Forwarded-Prefix` on every request (token format and forwarding:
434
- * `sandboxesProxyPortGet`).
669
+ * `sandboxesProxyPortGet`). Code in a sandbox gets a URL only for its own sandbox and sandboxes within its reach
670
+ * (otherwise `403 forbidden`, `reason: egress_escalation` or `scope_escalation`; `sandboxesCreateSandbox`): a
671
+ * preview carries requests to the port, so it would reach as far as that sandbox. Anyone with the URL reaches
672
+ * the port, so from the first mint until the token's expiry plus an hour the sandbox counts as `public` egress
673
+ * to code in other sandboxes; every request let in on the token has ended by then (at most an hour after it was
674
+ * let in, its request body included, and never past the token's expiry plus an hour: `sandboxesProxyPortGet`).
675
+ * A mint therefore takes the sandbox out of the reach of code in sandboxes whose egress is not `public` and
676
+ * cuts the streams such code holds open into it, and such code gets no further URL for it. Such code may mint
677
+ * the first URL while the sandbox is within its reach, but it can use that URL only through the usercontent
678
+ * origin, which its egress must let it reach (the in-VM preview route refuses it). An `allowlist` that names
679
+ * the usercontent origin reaches every preview URL its code learns, of any sandbox, so in practice it counts as
680
+ * `public`.
435
681
  *
436
682
  * `POST /v1/sandboxes/{sandbox_id}/ports/{port}/url` · scope `sandboxes:run` · served by the node
437
683
  */
@@ -464,7 +710,21 @@ export class SandboxesAPI extends Namespace {
464
710
  * Opens a WebSocket attached to a new pseudo-terminal running the user's login shell (or `cmd`). Binary frames
465
711
  * carry terminal bytes in both directions. Text frames carry JSON control messages: send
466
712
  * `{"type": "resize", "cols": 120, "rows": 40}`; the server sends `{"type": "exit", "code": 0}` before it closes.
467
- * Authenticate with the `Authorization` header or a ticket from `POST /v1/sandboxes/{sandbox_id}/ticket`.
713
+ * Authenticate with the `Authorization` header or a ticket from `POST /v1/sandboxes/{sandbox_id}/ticket`. From
714
+ * inside another sandbox (or on a ticket minted there), the terminal closes with code 1008, reason
715
+ * `egress_escalation`, if this sandbox leaves the caller's reach (`sandboxesCreateSandbox`); input sent after
716
+ * that is not delivered, and the shell is hung up.
717
+ *
718
+ * With `session=new` the terminal can outlive its socket: the server's first frame is
719
+ * `{"type": "session", "id": "pts_...", "resumed": false}`. If the socket closes while the shell is still
720
+ * running (a page reload, a dropped network), the shell keeps running for 5 minutes and its last 128 KiB of
721
+ * output are kept. `session=<id>` on the same sandbox attaches to it again: the first frame says
722
+ * `"resumed": true`, a binary frame replays the kept output from its first whole line, and the terminal takes
723
+ * the new `cols` and `rows`. A second socket on a session takes it over, and the first closes with code 4001,
724
+ * reason `attached_elsewhere`. An id that is unknown, expired or another sandbox's opens a new terminal
725
+ * (`"resumed": false`, a new id). Send `{"type": "end"}` to hang a kept terminal up at once instead of leaving
726
+ * it to expire. Without `session` the shell is hung up when the socket closes. From inside another sandbox
727
+ * `session` is ignored.
468
728
  *
469
729
  * `GET /v1/sandboxes/{sandbox_id}/pty` · scope `sandboxes:run` · served by the node
470
730
  */
@@ -472,6 +732,47 @@ export class SandboxesAPI extends Namespace {
472
732
  const { url, protocols } = await ticketedUrl(this._client, openPtyOp, createTicketOp, { path: [sandboxId], query: params }, options, false);
473
733
  return new PtySession(await openSocket(url, protocols, options.signal));
474
734
  }
735
+ /**
736
+ * Wait until a sandbox is ready
737
+ *
738
+ * Waits up to `wait_ms` for the sandbox's readiness probe (`readiness` on create) to pass, and answers with
739
+ * its state: `ready` (with `time_to_ready_ms`, from the create's start to the first passing try), `failed`
740
+ * (it did not pass within its `timeout_s`), or `pending` when `wait_ms` ran out first (call again). With
741
+ * `port` or `cmd` in the body the call sets that probe first, replacing the sandbox's earlier one, so a
742
+ * client can start a server with an exec and then wait for its port in one more call. The SDKs'
743
+ * `waitUntilReady` (`wait_until_ready`) is a different wait: the client runs its probes through exec, and it
744
+ * also takes HTTP-status and log-line probes. A sandbox with no probe, and none in the body, is `400`.
745
+ * Probes run inside the
746
+ * sandbox (a connection to `127.0.0.1:<port>`, or the command) and bill like any other use of it. From inside
747
+ * another sandbox, setting a probe needs this sandbox to be within the caller's reach
748
+ * (`sandboxesCreateSandbox`; otherwise `403 forbidden`, `reason: egress_escalation` or `scope_escalation`);
749
+ * waiting on the probe already set does not. A probe set that way keeps probing if this sandbox later leaves
750
+ * the caller's reach.
751
+ *
752
+ * The node runs the probe: the sandbox's `readiness`, or `port` or `cmd` in the body. `waitUntilReady()`
753
+ * probes from this client instead and also takes HTTP-status and log-line probes.
754
+ *
755
+ * `POST /v1/sandboxes/{sandbox_id}/ready` · scope `sandboxes:run` · served by the node
756
+ */
757
+ waitReady(sandboxId, body, options) {
758
+ return this._client._call(waitReadyOp, { path: [sandboxId], body }, options);
759
+ }
760
+ /**
761
+ * List the requests a sandbox made
762
+ *
763
+ * Lists the requests code inside this sandbox made to B3OS in the last 7 days (its model calls, scrapes,
764
+ * searches and calls to other sandboxes), newest first, from the node's request log. These are the records
765
+ * `GET /v1/requests?sandbox_id=` lists, for a caller that may read the sandbox: it needs only `sandboxes:read`,
766
+ * not `platform:read`, and never returns another sandbox's records. It takes the same paging and the
767
+ * `product`, `operation_id`, `status`, `trace_id` and `since` filters; any other query parameter is
768
+ * `400 invalid_request`. Requests made to the sandbox's public ports are not listed. A sandbox stopped more
769
+ * than 7 days ago is not found.
770
+ *
771
+ * `GET /v1/sandboxes/{sandbox_id}/requests` · scope `sandboxes:read` · served by the node
772
+ */
773
+ listRequests(sandboxId, params, options) {
774
+ return this._client._list(listRequestsOp, { path: [sandboxId], query: params }, options);
775
+ }
475
776
  /**
476
777
  * Resume a sandbox
477
778
  *
@@ -479,6 +780,9 @@ export class SandboxesAPI extends Namespace {
479
780
  * running sandbox returns it unchanged. A resumed sandbox is billed again (`sandboxes.gib_ms`), so a resume is
480
781
  * admitted like a create (credits, spend cap).
481
782
  *
783
+ * `timing` phases: `admit`, `resume` (the VMM running the vCPUs again) and `record`, as for pause. A resume
784
+ * costs more the longer the sandbox stayed paused (the measured numbers are on the benchmarks page).
785
+ *
482
786
  * `POST /v1/sandboxes/{sandbox_id}/resume` · scope `sandboxes:write` · served by the node
483
787
  */
484
788
  resumeSandbox(sandboxId, options) {
@@ -489,7 +793,8 @@ export class SandboxesAPI extends Namespace {
489
793
  *
490
794
  * Runs code in the sandbox's Jupyter kernel and returns its rich results (text, images, HTML, JSON), output
491
795
  * and any error. State persists between calls, like cells in a notebook. Needs a sandbox created from the
492
- * `code-interpreter` template.
796
+ * `code-interpreter` template. From inside another sandbox, a run is cut if this sandbox leaves the caller's
797
+ * reach before it answers (`403 forbidden`, `reason: egress_escalation`; `sandboxesCreateSandbox`).
493
798
  *
494
799
  * `POST /v1/sandboxes/{sandbox_id}/run_code` · scope `sandboxes:run` · served by the node
495
800
  */
@@ -500,10 +805,17 @@ export class SandboxesAPI extends Namespace {
500
805
  * Snapshot a sandbox
501
806
  *
502
807
  * Saves the sandbox's memory and disk as a snapshot you can create new sandboxes from. The sandbox keeps
503
- * running (it pauses for the copy). New sandboxes restored from a snapshot get a fresh identity, so two of them
504
- * never share random state. Writing the snapshot resets the sandbox's open connections to the outside (open
505
- * PTY sessions, streaming execs and log streams, port preview connections, outbound TCP connections); new
506
- * ones work as soon as the call returns.
808
+ * running (it pauses for the copy). The call answers, and the snapshot is listed `ready`, only once its files
809
+ * are written to the node's disk (`sync` in `timing`); the sandbox runs again before that. Each sandbox
810
+ * restored from a snapshot gets fresh kernel entropy, a new machine ID and hostname and the current time, but
811
+ * the processes it brings back keep their memory, yours and the template's (Chrome, in the `browser`
812
+ * template): a random generator such a process held at the snapshot (a browser's or Node.js's `Math.random`,
813
+ * a TLS library's) starts in the same state in every one of them unless B3OS resets it. B3OS reseeds the code
814
+ * kernel's Python `random` module and NumPy global generator, and resets the `browser` template's Chrome by
815
+ * default (`clone_reset` on the create) where the node's image of that template has the reset. Start your own
816
+ * such processes after the restore or restart them. Writing the snapshot resets the sandbox's open connections
817
+ * to the outside (open PTY sessions, streaming execs and log streams, port preview connections, outbound TCP
818
+ * connections); new ones work as soon as the call returns.
507
819
  *
508
820
  * Snapshots are kept 7 days. An organization keeps at most `sandboxes.snapshots_max` snapshots (beta: 20)
509
821
  * per node, counting at most `sandboxes.snapshot_storage_max` (beta: 40 GiB). Each snapshot counts its
@@ -517,6 +829,21 @@ export class SandboxesAPI extends Namespace {
517
829
  createSnapshot(sandboxId, body, options) {
518
830
  return this._client._call(createSnapshotOp, { path: [sandboxId], body }, options);
519
831
  }
832
+ /**
833
+ * Read what a sandbox uses now
834
+ *
835
+ * CPU, memory, disk and process counts of a running sandbox, read now and run no command in it. `cpu.percent`
836
+ * is the share of all the sandbox's vCPUs that was busy over the last `cpu.interval_ms`: since your previous
837
+ * stats read of this sandbox when that was 1 second to 5 minutes ago, else over 250 ms. Memory is the guest
838
+ * kernel's own count (`MemTotal` and `MemAvailable`). `disk` (the root filesystem) and `processes.total` are
839
+ * null in a sandbox whose image predates them. Poll it no faster than every 2 seconds. A paused or stopped
840
+ * sandbox answers `409 sandbox_not_running`.
841
+ *
842
+ * `GET /v1/sandboxes/{sandbox_id}/stats` · scope `sandboxes:read` · served by the node
843
+ */
844
+ getStats(sandboxId, options) {
845
+ return this._client._call(getStatsOp, { path: [sandboxId] }, options);
846
+ }
520
847
  /**
521
848
  * Create a stream ticket
522
849
  *
@@ -541,5 +868,66 @@ export class SandboxesAPI extends Namespace {
541
868
  setTimeout(sandboxId, body, options) {
542
869
  return this._client._call(setTimeoutOp, { path: [sandboxId], body }, options);
543
870
  }
871
+ /**
872
+ * Creates a sandbox (`POST /v1/sandboxes`, the same request as `createSandbox`) and returns its
873
+ * {@link Sandbox} handle: `exec`, `files`, `ports`, `snapshot`, `fork` and the lifecycle calls.
874
+ * `await using sbx = await b3os.sandboxes.create()` deletes it when the scope ends unless `sbx.detach()`.
875
+ * `createSandbox` sends the same request and returns only the object (`SandboxInfo`).
876
+ */
877
+ create(body, options) {
878
+ // The handle's ports.screenshot and ports.scrape use the client's render and vision namespaces.
879
+ const root = this._client;
880
+ const client = {
881
+ sandboxes: this,
882
+ get render() {
883
+ return root.render;
884
+ },
885
+ get vision() {
886
+ return root.vision;
887
+ },
888
+ };
889
+ return Sandbox.create(client, body, options);
890
+ }
891
+ /**
892
+ * Waits until a sandbox is ready: a TCP port accepts connections (`{ port }`), an HTTP GET answers
893
+ * (`{ http }`), a command exits 0 (`{ exec }`) or a background process prints a line (`{ log, pid }`). The
894
+ * first probe runs at once, later ones back off from 25 to 250 ms; `timeoutMs` (default 60 s) bounds the
895
+ * wait, and `pid` fails it at once when that process exits. Resolves with the client-side time to ready.
896
+ * This client runs the probes through `exec`, and nothing is set on the sandbox; `waitReady` asks the node
897
+ * to probe instead (a port or a command, kept as the sandbox's readiness probe).
898
+ */
899
+ waitUntilReady(sandboxId, probe, options) {
900
+ return waitUntilReady(this, sandboxId, probe, options);
901
+ }
902
+ /**
903
+ * Starts a background process (a dev server, a worker) and, with `ready`, waits until it is up; the process is
904
+ * stopped again when it exits or misses the deadline first. `previewPort` also mints its preview URL.
905
+ */
906
+ startService(sandboxId, options) {
907
+ return startService(this, sandboxId, options);
908
+ }
909
+ /**
910
+ * Claude Managed Agents, self-hosted environment: an always-on `ant beta:worker poll` in a sandbox. A new one
911
+ * (from the `agent` template) gets the environment key as a sandbox secret and egress limited to the Anthropic
912
+ * API; an existing one (`sandboxId`) keeps its own secrets and egress (see HarnessSandboxOptions).
913
+ */
914
+ startManagedAgentsWorker(options) {
915
+ return startManagedAgentsWorker(this, options);
916
+ }
917
+ /**
918
+ * Claude Managed Agents, one sandbox per session: runs `ant beta:worker run` for a work item your WorkPoller
919
+ * claimed (restore a prepared `snapshot` to boot the harness per session; `on_timeout: "pause"` freezes it
920
+ * between turns).
921
+ */
922
+ launchManagedAgentsSession(options) {
923
+ return launchManagedAgentsSession(this, options);
924
+ }
925
+ /**
926
+ * A Codex exec-server registered as a remote environment (`codex exec-server --remote`), from inside a sandbox:
927
+ * one outbound connection, no inbound port.
928
+ */
929
+ startCodexExecServer(options) {
930
+ return startCodexExecServer(this, options);
931
+ }
544
932
  }
545
933
  //# sourceMappingURL=sandboxes.js.map