@veris-ai/daytona 0.2.0 → 0.3.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.js CHANGED
@@ -174,14 +174,19 @@ var ControlPlane = class {
174
174
  }
175
175
  if (!res.ok) await this.json(res, `update sandbox ${sandboxId}`);
176
176
  }
177
- /** Extend a twin's TTL so it stays in lockstep with an extended Daytona sandbox. */
178
- async extendTtl(environmentId, sandboxId, ttlMinutes) {
179
- const res = await this.request("PATCH", `/v1/environments/${environmentId}/sandboxes/${sandboxId}`, { ttl_minutes: ttlMinutes });
180
- if (res.status === 404) {
181
- throw new TwinExpiredError(`Veris sandbox ${sandboxId} not found \u2014 cannot extend TTL`, { verisSandboxId: sandboxId });
182
- }
183
- if (!res.ok && res.status !== 405) await this.json(res, `extend TTL of ${sandboxId}`);
184
- }
177
+ // There was an extendTtl() here, and it did nothing.
178
+ //
179
+ // It PATCHed { ttl_minutes } at the sandbox resource, and the server's
180
+ // UpdateSandboxRequest declares only client_base_url. Pydantic ignores
181
+ // unknown fields, so the route answered 200 having changed no TTL at all —
182
+ // a call that reports success while doing nothing, which is worse than no
183
+ // call, because the caller then believes the twin will outlive the run.
184
+ // Nothing here called it; it was API surface promising a thing that could
185
+ // not happen.
186
+ //
187
+ // Bring it back when a route exists to carry it. Until then a twin's TTL is
188
+ // decided once, by ttl_minutes at create — where this SDK already keeps it
189
+ // in step with the Daytona sandbox's own ttlMinutes.
185
190
  /** Create-time preflight: is the gateway infrastructure up, per the control plane? */
186
191
  async gatewayHealth() {
187
192
  const res = await this.request("GET", "/v1/gateway/health");
@@ -195,40 +200,162 @@ var ControlPlane = class {
195
200
  }
196
201
  };
197
202
 
203
+ // src/run-receipt.ts
204
+ import { randomUUID } from "crypto";
205
+
198
206
  // src/receipt.ts
207
+ var PAGE_LIMIT = 1e3;
208
+ var MAX_PAGES = 20;
209
+ function rowsOf(body) {
210
+ if (!body || !Array.isArray(body.requests)) {
211
+ throw new VerisError("invalid request log: expected requests array", { phase: "receipt" });
212
+ }
213
+ const rows = body.requests;
214
+ for (const r of rows) {
215
+ if (!r || !Number.isSafeInteger(r.id) || r.id <= 0 || typeof r.method !== "string" || typeof r.path !== "string" || typeof r.tier !== "string" || !(r.status === null || typeof r.status === "number")) {
216
+ throw new VerisError("invalid request log row: stable id, method, path, tier and status required", { phase: "receipt" });
217
+ }
218
+ }
219
+ return rows;
220
+ }
199
221
  function parseRequestsBody(body) {
200
- const rows = Array.isArray(body?.requests) ? body.requests : [];
201
- const entries = rows.map((r) => {
202
- const row = r;
203
- return {
204
- method: String(row.method ?? ""),
205
- path: String(row.path ?? ""),
206
- status: typeof row.status === "number" ? row.status : null
207
- };
208
- });
209
- return { count: entries.length, entries };
222
+ const rows = rowsOf(body);
223
+ const entries = rows.filter(
224
+ (r) => ["handler", "fault", "fallback", "fallback-llm", "fallback-replay"].includes(r.tier) && !/^\/veris(?:\/|$)/.test(r.path)
225
+ ).map(({ id, method, path, status, tier }) => ({ id, method, path, status, tier }));
226
+ return { count: entries.length, entries, total: rows.length };
210
227
  }
211
- async function fetchReceiptEntry(svc) {
212
- const url = `${svc.control_url}/veris/requests`;
213
- const res = await fetch(url);
228
+ async function readPage(svc, query) {
229
+ const res = await fetch(`${svc.control_url.replace(/\/$/, "")}/veris/requests?${query}`, {
230
+ signal: AbortSignal.timeout(3e4),
231
+ redirect: "error"
232
+ });
214
233
  const text = await res.text();
215
- if (!res.ok) {
216
- throw new VerisError(`could not read receipt for service '${svc.name}' (${res.status})`, {
217
- phase: "receipt",
218
- responseBody: text.slice(0, 500)
219
- });
234
+ if (!res.ok) throw new VerisError(`could not read receipt for service '${svc.name}' (${res.status})`, { phase: "receipt" });
235
+ let body;
236
+ try {
237
+ body = JSON.parse(text);
238
+ } catch {
239
+ throw new VerisError(`service '${svc.name}' returned a non-JSON receipt body`, { phase: "receipt" });
220
240
  }
221
- let raw;
241
+ return rowsOf(body);
242
+ }
243
+ async function fetchWatermark(svc) {
244
+ const rows = await readPage(svc, new URLSearchParams({ limit: "1", order: "desc" }));
245
+ if (rows.length > 1) throw new VerisError("request log ignored watermark limit", { phase: "receipt" });
246
+ return rows[0]?.id ?? 0;
247
+ }
248
+ async function fetchReceiptEntry(svc, sinceId = 0) {
249
+ if (!Number.isSafeInteger(sinceId) || sinceId < 0) throw new VerisError("invalid receipt watermark", { phase: "receipt" });
250
+ const end = await fetchWatermark(svc);
251
+ if (end < sinceId) throw new VerisError("receipt baseline invalid: log moved backwards; take a new baseline", { phase: "receipt" });
252
+ const entries = [];
253
+ const raw = [];
254
+ let mark = sinceId;
255
+ let incompleteReason;
256
+ for (let page = 0; mark < end; page++) {
257
+ if (page >= MAX_PAGES) {
258
+ incompleteReason = "page-limit";
259
+ break;
260
+ }
261
+ let rows;
262
+ try {
263
+ rows = await readPage(svc, new URLSearchParams({ limit: String(PAGE_LIMIT), order: "asc", since_id: String(mark) }));
264
+ } catch (error) {
265
+ if (!raw.length) throw error;
266
+ incompleteReason = "read-failed";
267
+ break;
268
+ }
269
+ let previous = mark;
270
+ if (!rows.length || rows.some((r) => {
271
+ const bad = r.id <= previous;
272
+ previous = r.id;
273
+ return bad;
274
+ })) {
275
+ incompleteReason = "pagination-not-progressing";
276
+ break;
277
+ }
278
+ const window = rows.filter((r) => r.id <= end);
279
+ raw.push(...window);
280
+ entries.push(...parseRequestsBody({ requests: window }).entries);
281
+ mark = rows[rows.length - 1].id;
282
+ }
283
+ entries.sort((a, b) => b.id - a.id);
284
+ return {
285
+ requests: entries.length,
286
+ controlUrl: svc.control_url,
287
+ entries,
288
+ sinceId,
289
+ untilId: end,
290
+ capped: Boolean(incompleteReason),
291
+ ...incompleteReason ? { incompleteReason } : {},
292
+ raw: { requests: raw }
293
+ };
294
+ }
295
+
296
+ // src/run-receipt.ts
297
+ var HEADER = "x-veris-receipt-baseline";
298
+ function markerOf(row) {
222
299
  try {
223
- raw = JSON.parse(text);
300
+ const headers = typeof row.request_headers === "string" ? JSON.parse(row.request_headers) : row.request_headers;
301
+ return headers?.[HEADER];
224
302
  } catch {
225
- throw new VerisError(`service '${svc.name}' returned a non-JSON receipt body`, {
226
- phase: "receipt",
227
- responseBody: text.slice(0, 500)
303
+ return void 0;
304
+ }
305
+ }
306
+ async function captureBaseline(twinId, sandboxId, services) {
307
+ const marks = await Promise.all(services.map(async (svc) => {
308
+ const marker = randomUUID();
309
+ const res = await fetch(`${svc.control_url.replace(/\/$/, "")}/veris/schema`, {
310
+ headers: { [HEADER]: marker },
311
+ signal: AbortSignal.timeout(3e4),
312
+ redirect: "error"
228
313
  });
314
+ await res.text();
315
+ if (!res.ok) throw new VerisError(`could not establish baseline for '${svc.name}' (${res.status})`, { phase: "receipt" });
316
+ for (let attempt = 0; attempt < 5; attempt++) {
317
+ const rows = await readPage(svc, new URLSearchParams({ limit: "1000", order: "desc" }));
318
+ const anchor = rows.find((r) => markerOf(r) === marker && r.tier === "control" && r.path === "/veris/schema");
319
+ if (anchor) return [svc.name, { controlUrl: svc.control_url, id: anchor.id, marker }];
320
+ await new Promise((resolve) => setTimeout(resolve, 50));
321
+ }
322
+ throw new VerisError(`baseline unavailable for '${svc.name}': trace must retain control request headers`, { phase: "receipt" });
323
+ }));
324
+ return { version: 1, twinId, sandboxId, services: Object.fromEntries(marks) };
325
+ }
326
+ async function validateBaseline(baseline, twinId, sandboxId, services) {
327
+ const invalid = () => new VerisError("receipt baseline invalid: session, services or history changed; take a new baseline before execution", { phase: "receipt" });
328
+ if (baseline.version !== 1 || baseline.twinId !== twinId || baseline.sandboxId !== sandboxId || Object.keys(baseline.services).sort().join("\0") !== services.map((s) => s.name).sort().join("\0")) throw invalid();
329
+ await Promise.all(services.map(async (svc) => {
330
+ const mark = baseline.services[svc.name];
331
+ if (!mark || mark.controlUrl !== svc.control_url || !Number.isSafeInteger(mark.id) || mark.id <= 0) throw invalid();
332
+ const rows = await readPage(svc, new URLSearchParams({ limit: "1", order: "asc", since_id: String(mark.id - 1) }));
333
+ if (rows.length !== 1 || rows[0].id !== mark.id || markerOf(rows[0]) !== mark.marker) throw invalid();
334
+ }));
335
+ }
336
+
337
+ // src/service-control.ts
338
+ async function serviceControl(svc, resource, options = {}) {
339
+ const method = options.method ?? "GET";
340
+ if (!["manual", "schema", "operations", "data", "requests"].includes(resource) || !["GET", "POST", "PATCH"].includes(method) || method !== "GET" && resource !== "data" || method === "GET" && options.body !== void 0 || !/^https?:\/\//.test(svc.control_url)) {
341
+ throw new VerisError("unsupported service control operation");
342
+ }
343
+ const url = new URL(`${svc.control_url.replace(/\/$/, "")}/veris/${resource}`);
344
+ for (const [key, value] of Object.entries(options.query ?? {})) url.searchParams.set(key, value);
345
+ const res = await fetch(url, {
346
+ method,
347
+ redirect: "error",
348
+ signal: AbortSignal.timeout(3e4),
349
+ headers: { "Content-Type": "application/json" },
350
+ body: options.body === void 0 ? void 0 : JSON.stringify(options.body)
351
+ });
352
+ const text = await res.text();
353
+ if (!res.ok) throw new VerisError(`service '${svc.name}' ${method} /veris/${resource} failed (${res.status})`);
354
+ try {
355
+ return JSON.parse(text);
356
+ } catch {
357
+ throw new VerisError(`service '${svc.name}' returned invalid control JSON`);
229
358
  }
230
- const { count, entries } = parseRequestsBody(raw);
231
- return { requests: count, controlUrl: svc.control_url, entries, raw };
232
359
  }
233
360
 
234
361
  // src/network.ts
@@ -253,6 +380,9 @@ function twinHosts(services) {
253
380
  }
254
381
  return [...hosts].sort();
255
382
  }
383
+ function directTwinHosts(services) {
384
+ return twinHosts(services.filter((s) => isHttpUrl(s.url) && !(s.routes ?? []).length));
385
+ }
256
386
  function dataPlaneHosts(services) {
257
387
  const hosts = /* @__PURE__ */ new Set();
258
388
  for (const svc of services) {
@@ -302,51 +432,30 @@ var PROCESS_CONTROLLING = /* @__PURE__ */ new Set([
302
432
  function isSafeEnvName(name) {
303
433
  return /^[A-Z][A-Z0-9_]{0,63}$/.test(name) && !PROCESS_CONTROLLING.has(name);
304
434
  }
305
- var DEFAULT_REGISTRY_HOSTS = [
306
- // JS
307
- "registry.npmjs.org",
308
- "registry.yarnpkg.com",
309
- // Python
310
- "pypi.org",
311
- "files.pythonhosted.org",
312
- // Go
313
- "proxy.golang.org",
314
- "sum.golang.org",
315
- // Rust
316
- "crates.io",
317
- "static.crates.io",
318
- "index.crates.io",
319
- // Debian/Ubuntu
320
- "deb.debian.org",
321
- "security.debian.org",
322
- "archive.ubuntu.com",
323
- "security.ubuntu.com",
324
- // Source + container hosts the above routinely redirect to
325
- "github.com",
326
- "codeload.github.com",
327
- "objects.githubusercontent.com",
328
- "raw.githubusercontent.com",
329
- "ghcr.io"
330
- ];
435
+ var IPV4_RE = /^(25[0-5]|2[0-4]\d|1?\d?\d)(\.(25[0-5]|2[0-4]\d|1?\d?\d)){3}$/;
331
436
  function buildNetwork(args) {
332
- const { services, mode, gatewayHosts, allowOut = [], allowRegistries = true } = args;
333
- if (mode === "open") return {};
334
- const domains = [
335
- ...vendorHosts(services),
336
- ...gatewayHosts,
337
- ...dataPlaneHosts(services),
338
- ...allowRegistries ? DEFAULT_REGISTRY_HOSTS : [],
339
- ...allowOut
340
- ].filter((h) => Boolean(h));
341
- return {
342
- // NOT networkBlockAll: that blocks everything including the gateway, and the
343
- // allowlist is what Daytona documents as "unbypassable network-layer
344
- // enforcement". Blocking all and then allowing is not a shape the API
345
- // offers; a non-empty domainAllowList IS the deny-by-default.
346
- domainAllowList: [...new Set(domains)].sort().join(",")
347
- };
437
+ const { mode, gatewayIps: gatewayIps2 } = args;
438
+ if (mode === "open") return { params: {} };
439
+ const ips = [...new Set(gatewayIps2)];
440
+ if (!ips.length) {
441
+ throw new VerisError(
442
+ "the control plane did not name the gateway's IP addresses (egress credential `gateway_ips`) and the gateway hostname could not be resolved, so the sandbox cannot be pinned to it. Upgrade the control plane, or run with veris.egress: 'open' (Daytona still blocks anything that bypasses the proxy).",
443
+ { phase: "credential-mint" }
444
+ );
445
+ }
446
+ const bad = ips.filter((ip) => !IPV4_RE.test(ip));
447
+ if (bad.length) {
448
+ throw new VerisError(
449
+ `the control plane returned gateway addresses that are not IPv4: ${JSON.stringify(bad)} (Daytona's networkAllowList takes IPv4 CIDRs only)`,
450
+ { phase: "credential-mint" }
451
+ );
452
+ }
453
+ return { params: { networkAllowList: ips.map((ip) => `${ip}/32`).join(",") } };
348
454
  }
349
455
 
456
+ // src/gateway.ts
457
+ import { promises as dns } from "dns";
458
+
350
459
  // src/trust.ts
351
460
  var CA_CERT_PATH = "/usr/local/share/ca-certificates/veris-ca.crt";
352
461
  var SYSTEM_BUNDLE = "/etc/ssl/certs/ca-certificates.crt";
@@ -371,9 +480,54 @@ function vendoredTrustEnv() {
371
480
  NIX_SSL_CERT_FILE: VERIS_BUNDLE,
372
481
  PERL_LWP_SSL_CA_FILE: VERIS_BUNDLE,
373
482
  CLOUDSDK_CORE_CUSTOM_CA_CERTS_FILE: VERIS_BUNDLE,
374
- NODE_EXTRA_CA_CERTS: VERIS_CA_FILE
483
+ NODE_EXTRA_CA_CERTS: VERIS_BUNDLE
375
484
  };
376
485
  }
486
+ function trustPrelude(env = vendoredTrustEnv()) {
487
+ return Object.entries(env).map(([k, v]) => `export ${k}='${v.replace(/'/g, `'\\''`)}';`).join(" ");
488
+ }
489
+ var BUNDLED_CA_FILES = [
490
+ { sdk: "pip (vendored certifi)", suffix: "pip/_vendor/certifi/cacert.pem" },
491
+ { sdk: "certifi", suffix: "certifi/cacert.pem" },
492
+ { sdk: "botocore", suffix: "botocore/cacert.pem" },
493
+ // Matches both stripe-python's site-packages layout and the stripe-ruby gem.
494
+ { sdk: "stripe", suffix: "stripe/data/ca-certificates.crt" },
495
+ { sdk: "httplib2", suffix: "httplib2/cacerts.txt" }
496
+ ];
497
+ var BUNDLED_CA_PATCH_SCRIPT = "/tmp/veris-patch-bundled-cas.sh";
498
+ var BUNDLED_CA_PATCHED_MARKER = "the Veris CA was appended to ";
499
+ function bundledCaPatchScript() {
500
+ const find = BUNDLED_CA_FILES.map((f) => `find / -xdev \\( -path /proc -o -path /sys -o -path /dev \\) -prune -o -path '*/${f.suffix}' -type f -print 2>/dev/null`).join("; ");
501
+ return [
502
+ `marker=$(sed -n 2p ${VERIS_CA_FILE} 2>/dev/null)`,
503
+ // No CA on disk means nothing to append, and appending nothing to every
504
+ // bundle in the image would be the worst available no-op.
505
+ `[ -n "$marker" ] || { echo "no ${VERIS_CA_FILE} to append" >&2; exit 1; }`,
506
+ // The whole loop is one brace group at the end of the pipe so `n` survives
507
+ // to the summary: a pipeline's last stage is a subshell, and a counter
508
+ // incremented inside a bare `while` would be gone by the time we report it.
509
+ `{ ${find}; } | sort -u | { n=0`,
510
+ `while IFS= read -r f; do`,
511
+ ` [ -w "$f" ] || continue`,
512
+ ` grep -qF "$marker" "$f" && continue`,
513
+ ` { printf '\\n'; cat ${VERIS_CA_FILE}; } >> "$f" || continue`,
514
+ ` n=$((n+1))`,
515
+ ` echo "${BUNDLED_CA_PATCHED_MARKER}$f"`,
516
+ `done`,
517
+ // Silence is the common case and it reads as "did that even run?", so say
518
+ // so. patchBundledCas() keeps only the lines above, so this costs it
519
+ // nothing.
520
+ `if [ "$n" -gt 0 ]; then echo "$n bundled CA file(s) patched"`,
521
+ `else echo "no bundled CA file needed patching \u2014 no SDK that ships its own is installed here, or they already trust the Veris CA"; fi`,
522
+ `}`
523
+ ].join("\n");
524
+ }
525
+ var NODE_TRUST_FLAG = "--use-openssl-ca";
526
+ function nodeOptionsWithTrust(existing) {
527
+ const base = (existing ?? "").trim();
528
+ if (base.split(/\s+/).includes(NODE_TRUST_FLAG)) return base;
529
+ return base ? `${base} ${NODE_TRUST_FLAG}` : NODE_TRUST_FLAG;
530
+ }
377
531
  var CA_INSTALL_CMD = [
378
532
  "update-ca-certificates",
379
533
  `(keytool -importcert -noprompt -cacerts -storepass changeit -alias veris -file ${CA_CERT_PATH} 2>/dev/null || true)`,
@@ -408,6 +562,16 @@ function gatewayProxyUrl(credential) {
408
562
  }
409
563
  return `http://${encodeURIComponent(credential.username)}:x@${credential.connect_address}`;
410
564
  }
565
+ async function gatewayIps(credential, proxyUrl) {
566
+ if (credential.gateway_ips?.length) return credential.gateway_ips;
567
+ const host = new URL(proxyUrl).hostname;
568
+ if (/^\d+\.\d+\.\d+\.\d+$/.test(host)) return [host];
569
+ try {
570
+ return await dns.resolve4(host);
571
+ } catch {
572
+ return [];
573
+ }
574
+ }
411
575
  function assertProxyUrl(raw) {
412
576
  let u;
413
577
  try {
@@ -434,6 +598,7 @@ function assertProxyUrl(raw) {
434
598
  }
435
599
  async function installCa(sandbox, caPem) {
436
600
  await sandbox.fs.uploadFile(Buffer.from(caPem, "utf8"), VERIS_CA_FILE);
601
+ await sandbox.fs.uploadFile(Buffer.from(bundledCaPatchScript(), "utf8"), BUNDLED_CA_PATCH_SCRIPT);
437
602
  const script = [
438
603
  `chmod 0644 ${VERIS_CA_FILE}`,
439
604
  // Public roots first so they keep working; ours appended. `cat` of a
@@ -459,6 +624,10 @@ async function installCa(sandbox, caPem) {
459
624
  );
460
625
  }
461
626
  }
627
+ async function patchBundledCas(sandbox) {
628
+ const r = await sh(sandbox, `sh ${BUNDLED_CA_PATCH_SCRIPT}`, 180).catch((e) => ({ exitCode: 1, result: String(e) }));
629
+ return (r.result ?? "").split("\n").filter((line) => line.startsWith(BUNDLED_CA_PATCHED_MARKER)).map((line) => line.slice(BUNDLED_CA_PATCHED_MARKER.length).trim()).filter(Boolean);
630
+ }
462
631
  async function probeCanary(sandbox, canaryHost, expectedTwinId) {
463
632
  if (!HOSTNAME_RE.test(canaryHost)) {
464
633
  throw new ReceiptIntegrityError(
@@ -512,9 +681,15 @@ var VerisApiImpl = class {
512
681
  this.ctx = ctx;
513
682
  }
514
683
  ctx;
684
+ get ownsTwin() {
685
+ return this.ctx.ownsTwin;
686
+ }
515
687
  get sandboxId() {
516
688
  return this.ctx.twinId;
517
689
  }
690
+ get environmentId() {
691
+ return this.ctx.environmentId;
692
+ }
518
693
  get mode() {
519
694
  return "gateway";
520
695
  }
@@ -540,10 +715,13 @@ var VerisApiImpl = class {
540
715
  }
541
716
  async receipt(service) {
542
717
  await probeCanary(this.ctx.sandbox, this.ctx.canaryHost, this.ctx.twinId);
543
- if (service !== void 0) return fetchReceiptEntry(await this.resolveService(service));
718
+ if (service !== void 0) {
719
+ const svc = await this.resolveService(service);
720
+ return fetchReceiptEntry(svc, this.watermark(svc.name));
721
+ }
544
722
  const services = await this.services();
545
723
  const entries = await Promise.all(
546
- services.filter((s) => isHttpUrl(s.control_url)).map(async (svc) => [svc.name, await fetchReceiptEntry(svc)])
724
+ services.filter((s) => isHttpUrl(s.control_url)).map(async (svc) => [svc.name, await fetchReceiptEntry(svc, this.watermark(svc.name))])
547
725
  );
548
726
  return {
549
727
  services: Object.fromEntries(entries),
@@ -559,10 +737,45 @@ var VerisApiImpl = class {
559
737
  leaks() {
560
738
  return ["udp-quic-possible", "ech-possible"];
561
739
  }
740
+ /** Where this service's log was when the run began. 0 reads all of it. */
741
+ watermark(service) {
742
+ return this.ctx.watermarks?.[service] ?? 0;
743
+ }
744
+ async verifyIntegrity() {
745
+ await probeCanary(this.ctx.sandbox, this.ctx.canaryHost, this.ctx.twinId);
746
+ }
747
+ async receiptBaseline() {
748
+ await this.verifyIntegrity();
749
+ return captureBaseline(
750
+ this.ctx.twinId,
751
+ this.ctx.sandbox.id,
752
+ (await this.services()).filter((s) => isHttpUrl(s.control_url))
753
+ );
754
+ }
755
+ async receiptSince(baseline, service) {
756
+ await this.verifyIntegrity();
757
+ const services = (await this.services()).filter((s) => isHttpUrl(s.control_url));
758
+ if (service !== void 0 && !services.some((s) => s.name === service)) {
759
+ throw new VerisError(`unknown HTTP service '${service}'`, { phase: "receipt" });
760
+ }
761
+ await validateBaseline(baseline, this.ctx.twinId, this.ctx.sandbox.id, services);
762
+ const selected = service === void 0 ? services : services.filter((s) => s.name === service);
763
+ const entries = await Promise.all(selected.map(async (svc) => [svc.name, await fetchReceiptEntry(svc, baseline.services[svc.name].id)]));
764
+ await validateBaseline(baseline, this.ctx.twinId, this.ctx.sandbox.id, await this.services().then((s) => s.filter((v) => isHttpUrl(v.control_url))));
765
+ return { services: Object.fromEntries(entries), mode: "gateway", integrity: "verified", leaks: this.leaks() };
766
+ }
767
+ async control(service, resource, options) {
768
+ const svc = (await this.services()).find((s) => s.name === service);
769
+ if (!svc) throw new VerisError(`unknown service '${service}'`);
770
+ return serviceControl(svc, resource, options);
771
+ }
562
772
  async assertTouched(service, match) {
563
773
  const entry = await this.receipt(service);
564
774
  const need = match?.minRequests ?? 1;
565
775
  const matched = match ? entry.entries.filter((r) => (match.method === void 0 || r.method.toUpperCase() === match.method.toUpperCase()) && (match.path === void 0 || r.path.includes(match.path))) : entry.entries;
776
+ if (matched.length < need && entry.capped) {
777
+ throw new VerisError(`receipt for '${service}' is incomplete; insufficient evidence`, { phase: "receipt" });
778
+ }
566
779
  if (matched.length < need) {
567
780
  const what = match ? `matching ${match.method ?? "ANY"} ${match.path ?? "*"} (${matched.length}/${need})` : "any intercepted requests";
568
781
  throw new VerisUntouchedError(
@@ -579,6 +792,36 @@ var VerisApiImpl = class {
579
792
  getTrustEnv() {
580
793
  return vendoredTrustEnv();
581
794
  }
795
+ /**
796
+ * The same variables as a shell prelude, for a caller that has no env map to
797
+ * fill — anything running a command through a session or a shell it did not
798
+ * build the environment for. Daytona resets SSL_CERT_FILE,
799
+ * REQUESTS_CA_BUNDLE, CURL_CA_BUNDLE and NODE_EXTRA_CA_CERTS to its own CA
800
+ * file inside the sandbox, and that file cannot verify the gateway's leaf, so
801
+ * a command that inherits them fails on certificate validation:
802
+ * `uv sync` dies with "invalid peer certificate: UnknownIssuer".
803
+ *
804
+ * sbx.process.executeCommand(`${sbx.veris.trustPrelude()} uv sync`)
805
+ */
806
+ trustPrelude() {
807
+ return trustPrelude(this.getTrustEnv());
808
+ }
809
+ /**
810
+ * Append the Veris CA to the CA bundles the code under test's own SDKs ship.
811
+ *
812
+ * The variables above reach every client that reads one. stripe-python does
813
+ * not: it passes `verify=stripe.ca_bundle_path`, so the first Stripe call
814
+ * fails with "Could not verify Stripe's SSL certificate" in a sandbox where
815
+ * curl, Node and `requests` all succeed. This patches the file itself, which
816
+ * is the Daytona-shaped version of the veris CLI's --patch-bundled-cas.
817
+ *
818
+ * Call it AFTER installing dependencies — the bundles arrive with them, so
819
+ * there is nothing to patch at create time. Safe to call again; it returns
820
+ * only the files it actually changed.
821
+ */
822
+ patchBundledCas() {
823
+ return patchBundledCas(this.ctx.sandbox);
824
+ }
582
825
  async deliverTo(target, opts = {}) {
583
826
  const url = typeof target === "number" ? (await this.ctx.sandbox.getPreviewLink(target)).url : target;
584
827
  await this.ctx.controlPlane.updateSandbox(
@@ -610,8 +853,70 @@ var VerisApiImpl = class {
610
853
  }
611
854
  };
612
855
 
856
+ // src/profile.ts
857
+ import { readFileSync } from "fs";
858
+ import { homedir } from "os";
859
+ import { join } from "path";
860
+ import { parse } from "yaml";
861
+ var DEFAULT_API_BASE = "https://svc.api.veris.ai";
862
+ function profilePath(home = homedir()) {
863
+ return join(home, ".veris", "twin.yaml");
864
+ }
865
+ function readOrUndefined(path) {
866
+ try {
867
+ return readFileSync(path, "utf8");
868
+ } catch (e) {
869
+ if (e.code === "ENOENT") return void 0;
870
+ throw e;
871
+ }
872
+ }
873
+ function parseProfileFile(text, path) {
874
+ if (text === void 0) return void 0;
875
+ let parsed;
876
+ try {
877
+ parsed = parse(text);
878
+ } catch (cause) {
879
+ throw new VerisError(`${path} is not valid YAML`, { phase: "credentials", cause });
880
+ }
881
+ if (parsed === null || parsed === void 0) return {};
882
+ if (typeof parsed !== "object" || Array.isArray(parsed)) {
883
+ throw new VerisError(`${path} is not a profile file (expected a mapping)`, { phase: "credentials" });
884
+ }
885
+ return parsed;
886
+ }
887
+ function selectProfileName(env, file) {
888
+ return env.VERIS_PROFILE || file?.active_profile || "default";
889
+ }
890
+ function resolveVerisCredentials(opts = {}) {
891
+ const env = opts.env ?? process.env;
892
+ const path = opts.path ?? profilePath();
893
+ const file = parseProfileFile((opts.readFile ?? readOrUndefined)(path), path);
894
+ const name = selectProfileName(env, file);
895
+ const profile = file?.profiles?.[name] ?? void 0;
896
+ const found = profile !== void 0 && profile !== null;
897
+ const apiKey = opts.apiKey || env.VERIS_API_KEY || (found ? profile.api_key : void 0) || void 0;
898
+ const keySource = opts.apiKey ? "option" : env.VERIS_API_KEY ? "env" : apiKey ? "profile" : "none";
899
+ const profileBase = found ? profile.api_base : void 0;
900
+ const apiBase = (opts.apiBase || env.VERIS_API_BASE || profileBase || DEFAULT_API_BASE).replace(/\/$/, "");
901
+ const baseSource = opts.apiBase ? "option" : env.VERIS_API_BASE ? "env" : profileBase ? "profile" : "default";
902
+ return { apiKey, apiBase, keySource, baseSource, profile: { name, path, found } };
903
+ }
904
+ function missingKeyWhere(c) {
905
+ const where = c.profile.found ? `profile '${c.profile.name}' in ${c.profile.path} has no api_key` : `${c.profile.path} has no profile '${c.profile.name}'`;
906
+ return `VERIS_API_KEY is not set, and ${where}`;
907
+ }
908
+ var KEY_SOURCES_HINT = "Run `veris login` (the veris CLI saves the key there; `--profile <name>` for another plane, and VERIS_PROFILE picks it here), or set VERIS_API_KEY in the environment. Get a key at https://studio.veris.ai";
909
+ function missingKeyMessage(c) {
910
+ return `no Veris API key: ${missingKeyWhere(c)}. ${KEY_SOURCES_HINT}`;
911
+ }
912
+ function requireVerisCredentials(opts = {}) {
913
+ const c = resolveVerisCredentials(opts);
914
+ if (!c.apiKey) throw new MissingCredentialsError(missingKeyMessage(c), { phase: "credentials" });
915
+ return c;
916
+ }
917
+
613
918
  // src/version.ts
614
- var SDK_VERSION = true ? "0.2.0" : "0.0.0-dev";
919
+ var SDK_VERSION = true ? "0.3.0" : "0.0.0-dev";
615
920
 
616
921
  // src/daytona.ts
617
922
  var LABEL = {
@@ -621,12 +926,24 @@ var LABEL = {
621
926
  mode: "veris_mode",
622
927
  egress: "veris_egress",
623
928
  ownsTwin: "veris_owns_twin",
624
- canaryHost: "veris_canary_host"
929
+ canaryHost: "veris_canary_host",
930
+ /** Unique to ONE create() call, and the only way back to a sandbox whose
931
+ * create threw before it could hand the object over — see reapFailedCreate.
932
+ * The twin id will not do: an attached twin is shared by every sandbox on
933
+ * it, and deleting the wrong one of those would be worse than the leak. */
934
+ createId: "veris_create_id"
625
935
  };
626
936
  var VERIS_LABEL_KEYS = Object.values(LABEL);
627
937
  function isVerisSandbox(sbx) {
628
938
  return typeof sbx.verisSandboxId === "string";
629
939
  }
940
+ function verisTwinId(sandbox) {
941
+ return sandbox.labels?.[LABEL.twinId];
942
+ }
943
+ function verisOwnsTwin(sandbox) {
944
+ const labels = sandbox.labels ?? {};
945
+ return labels[LABEL.twinId] !== void 0 && labels[LABEL.ownsTwin] !== "false";
946
+ }
630
947
  var Daytona = class extends BaseDaytona {
631
948
  verisDefaults;
632
949
  constructor(config) {
@@ -646,6 +963,7 @@ var Daytona = class extends BaseDaytona {
646
963
  const egress = v.egress ?? "strict";
647
964
  const ttlMinutes = v.ttlMinutes ?? 60;
648
965
  const ownsTwin = !v.attachSandboxId;
966
+ const createId = crypto.randomUUID();
649
967
  const twin = await this.provisionTwin(controlPlane, v, coords, ttlMinutes);
650
968
  const cleanupTwin = async () => {
651
969
  if (ownsTwin) await controlPlane.deleteTwin(twin.environment_id, twin.id).catch(() => {
@@ -653,6 +971,8 @@ var Daytona = class extends BaseDaytona {
653
971
  };
654
972
  let sandbox;
655
973
  let credential;
974
+ let watermarks = {};
975
+ let daytonaAsked = false;
656
976
  try {
657
977
  credential = await controlPlane.mintEgressCredential(twin.environment_id, twin.id);
658
978
  if (!credential) {
@@ -669,17 +989,19 @@ var Daytona = class extends BaseDaytona {
669
989
  }
670
990
  const services = twin.services?.length ? twin.services : await controlPlane.services(twin.id);
671
991
  const proxyUrl = gatewayProxyUrl(credential);
672
- const network = buildNetwork({
673
- services,
674
- mode: egress,
675
- // The gateway has to be reachable or nothing is: taken from the URL we
676
- // are actually going to use, so the two can never disagree.
677
- gatewayHosts: [new URL(proxyUrl).hostname, credential.canary_host].filter(Boolean),
678
- allowOut: v.allowOut,
679
- allowRegistries: v.allowRegistries
680
- });
992
+ const network = buildNetwork({ mode: egress, gatewayIps: await gatewayIps(credential, proxyUrl) });
993
+ watermarks = await readWatermarks(services);
681
994
  const verisManaged = {
682
995
  ...v.installCa !== false ? sanitizeTrustEnv(void 0) : {},
996
+ // Node reads none of the variables above once Daytona has overwritten
997
+ // them; the flag makes it read OpenSSL's store, which the CA install
998
+ // fills. Appended, not replaced: a caller's own NODE_OPTIONS keep working.
999
+ ...v.installCa !== false ? { NODE_OPTIONS: nodeOptionsWithTrust(rest.envVars?.NODE_OPTIONS) } : {},
1000
+ // Node ignores HTTPS_PROXY unless told to, and Daytona blocks anything
1001
+ // that dials out directly — so without this every plain `https.get`
1002
+ // and `fetch` in Node 24+ dies with ECONNRESET instead of reaching
1003
+ // the gateway. Older Node needs a proxy agent; the variable is inert there.
1004
+ NODE_USE_ENV_PROXY: "1",
683
1005
  ...v.dataPlaneEnv !== false ? dataPlaneEnv(services) : {},
684
1006
  VERIS_SANDBOX_ID: twin.id
685
1007
  };
@@ -694,20 +1016,23 @@ var Daytona = class extends BaseDaytona {
694
1016
  [LABEL.egress]: egress,
695
1017
  [LABEL.ownsTwin]: String(ownsTwin),
696
1018
  [LABEL.mode]: "gateway",
697
- [LABEL.canaryHost]: credential.canary_host
1019
+ [LABEL.canaryHost]: credential.canary_host,
1020
+ [LABEL.createId]: createId
698
1021
  },
699
- ...network,
700
- // 3. Where Daytona forwards everything the allowlist permits. Chained,
701
- // not advisory: an unreachable gateway makes allowed traffic 502
702
- // rather than quietly going direct.
1022
+ ...network.params,
1023
+ // 3. Where Daytona forwards everything. Chained, not advisory: an
1024
+ // unreachable gateway makes traffic 502 rather than quietly going
1025
+ // direct.
703
1026
  outboundProxyUrl: proxyUrl,
704
1027
  ttlMinutes: rest.ttlMinutes ?? ttlMinutes
705
1028
  };
1029
+ daytonaAsked = true;
706
1030
  sandbox = await this.baseCreate(createParams, options);
707
1031
  } catch (cause) {
1032
+ const failed = daytonaAsked ? await reapFailedCreate((labels) => super.list({ labels }), createId) : void 0;
708
1033
  await cleanupTwin();
709
1034
  if (cause instanceof VerisError) throw cause;
710
- throw new VerisError("Daytona sandbox create failed", {
1035
+ throw new VerisError(sandboxCreateMessage(cause, failed, rest), {
711
1036
  phase: "sandbox-create",
712
1037
  verisSandboxId: twin.id,
713
1038
  cause
@@ -728,7 +1053,8 @@ var Daytona = class extends BaseDaytona {
728
1053
  twinId: twin.id,
729
1054
  egress,
730
1055
  ownsTwin,
731
- canaryHost: credential.canary_host
1056
+ canaryHost: credential.canary_host,
1057
+ watermarks
732
1058
  });
733
1059
  }
734
1060
  /**
@@ -760,9 +1086,10 @@ var Daytona = class extends BaseDaytona {
760
1086
  const labels = sandbox.labels ?? {};
761
1087
  const twinId = labels[LABEL.twinId];
762
1088
  if (!twinId) return sandbox;
763
- const apiKey = this.verisDefaults.apiKey ?? process.env.VERIS_API_KEY;
1089
+ const creds = resolveVerisCredentials({ apiKey: this.verisDefaults.apiKey, apiBase: this.verisDefaults.apiBase });
1090
+ const apiKey = creds.apiKey;
764
1091
  if (!apiKey) return sandbox;
765
- const trustedBase = this.verisDefaults.apiBase ?? process.env.VERIS_API_BASE;
1092
+ const trustedBase = creds.baseSource === "default" ? void 0 : creds.apiBase;
766
1093
  const labelBase = labels[LABEL.apiBase];
767
1094
  if (trustedBase && labelBase && labelBase !== trustedBase) {
768
1095
  throw new VerisError(
@@ -844,19 +1171,48 @@ var Daytona = class extends BaseDaytona {
844
1171
  };
845
1172
  var daytona_default = Daytona;
846
1173
  function resolveCoordinates(v) {
847
- const apiKey = v.apiKey ?? process.env.VERIS_API_KEY;
848
- if (!apiKey) {
849
- throw new MissingCredentialsError(
850
- "no Veris API key: set VERIS_API_KEY in your environment, or pass veris.apiKey. Get one at https://studio.veris.ai",
851
- { phase: "credentials" }
852
- );
853
- }
1174
+ const creds = requireVerisCredentials({ apiKey: v.apiKey, apiBase: v.apiBase });
854
1175
  return {
855
- apiKey,
1176
+ apiKey: creds.apiKey,
856
1177
  environmentId: v.environmentId ?? process.env.VERIS_ENVIRONMENT_ID,
857
- apiBase: (v.apiBase ?? process.env.VERIS_API_BASE ?? "https://svc.api.veris.ai").replace(/\/$/, "")
1178
+ apiBase: creds.apiBase
858
1179
  };
859
1180
  }
1181
+ async function reapFailedCreate(list, createId) {
1182
+ try {
1183
+ for await (const sandbox of list({ [LABEL.createId]: createId })) {
1184
+ if (sandbox.labels?.[LABEL.createId] !== createId) continue;
1185
+ if (!sandbox.errorReason) await sandbox.refreshData().catch(() => {
1186
+ });
1187
+ const found = { id: sandbox.id, state: sandbox.state, errorReason: sandbox.errorReason };
1188
+ return { ...found, deleted: await sandbox.delete().then(() => true, () => false) };
1189
+ }
1190
+ } catch {
1191
+ }
1192
+ return void 0;
1193
+ }
1194
+ function sandboxCreateMessage(cause, failed, params = {}) {
1195
+ const base = "Daytona sandbox create failed";
1196
+ if (!failed) return base;
1197
+ const reason = failed.errorReason?.trim().replace(/[.\s]+$/, "");
1198
+ const parts = [reason ? `${base}: ${reason}.` : `${base}.`];
1199
+ const image = typeof params.image === "string" ? params.image : void 0;
1200
+ if (image && failed.state === "build_failed") {
1201
+ parts.push(
1202
+ `Daytona builds "${image}" on its own servers and pulls it from a registry, so an image that exists only in your local Docker daemon is not one it can see.`
1203
+ );
1204
+ }
1205
+ parts.push(failed.deleted ? `The half-built sandbox ${failed.id} was deleted.` : `The half-built sandbox ${failed.id} could not be deleted \u2014 remove it with \`veris-daytona teardown ${failed.id}\`.`);
1206
+ const raw = cause instanceof Error ? cause.message : "";
1207
+ if (raw) parts.push(`(Daytona SDK: ${raw})`);
1208
+ return parts.join(" ");
1209
+ }
1210
+ async function readWatermarks(services) {
1211
+ const marks = await Promise.all(
1212
+ services.filter((s) => isHttpUrl(s.control_url)).map(async (svc) => [svc.name, await fetchWatermark(svc).catch(() => 0)])
1213
+ );
1214
+ return Object.fromEntries(marks);
1215
+ }
860
1216
  function stripVeris(params) {
861
1217
  const { veris: _veris, ...rest } = params ?? {};
862
1218
  return rest;
@@ -868,12 +1224,83 @@ function reserveLabels(labels) {
868
1224
  }
869
1225
  return out;
870
1226
  }
1227
+
1228
+ // src/daytona-key.ts
1229
+ var DAYTONA_API_URL = "https://app.daytona.io/api";
1230
+ var DELETE_SANDBOXES = "delete:sandboxes";
1231
+ function daytonaApiUrl(env = process.env) {
1232
+ return (env.DAYTONA_API_URL || env.DAYTONA_SERVER_URL || DAYTONA_API_URL).replace(/\/$/, "");
1233
+ }
1234
+ async function fetchDaytonaKey(apiKey, apiUrl = daytonaApiUrl()) {
1235
+ try {
1236
+ const res = await fetch(`${apiUrl}/api-keys/current`, {
1237
+ headers: { Authorization: `Bearer ${apiKey}` }
1238
+ });
1239
+ if (!res.ok) return void 0;
1240
+ const body = await res.json();
1241
+ if (!Array.isArray(body.permissions)) return void 0;
1242
+ return {
1243
+ name: typeof body.name === "string" ? body.name : "",
1244
+ permissions: body.permissions.filter((p) => typeof p === "string")
1245
+ };
1246
+ } catch {
1247
+ return void 0;
1248
+ }
1249
+ }
1250
+ function canDeleteSandboxes(key) {
1251
+ return key === void 0 ? void 0 : key.permissions.includes(DELETE_SANDBOXES);
1252
+ }
1253
+ function describeKey(key) {
1254
+ const name = key.name ? `"${key.name}" ` : "";
1255
+ return `${name}(permissions: ${key.permissions.join(", ") || "none"})`;
1256
+ }
1257
+ var DELETE_PERMISSION_FIX = 'create a key with the "delete sandboxes" permission at https://app.daytona.io/dashboard/keys';
1258
+ function cannotTeardownWarning(key, autoStopMinutes, autoDeleteMinutes) {
1259
+ return `DAYTONA_API_KEY ${describeKey(key)} lacks \`${DELETE_SANDBOXES}\`, so \`veris-daytona teardown\` will be refused for the sandbox about to be created, and \`run\` cannot delete its own. The box still stops after ${autoStopMinutes} idle minutes and is deleted ${autoDeleteMinutes} minutes after it stops. For a teardown that works, ${DELETE_PERMISSION_FIX} and use it instead.`;
1260
+ }
1261
+
1262
+ // src/teardown.ts
1263
+ var TEARDOWN_USAGE = `usage: veris-daytona teardown <daytona-sandbox-id>
1264
+
1265
+ Deletes the Daytona sandbox. The id is the one \`veris-daytona provision\`
1266
+ printed as daytonaSandboxId \u2014 not the Veris twin's id.
1267
+
1268
+ The twin follows the rule it was created under: one this package created is
1269
+ deleted with the sandbox, one it attached to (\`provision\`, or \`run --sandbox\`)
1270
+ is yours and is left running. Which of the two happened is printed.
1271
+
1272
+ needs: DAYTONA_API_KEY with the ${DELETE_SANDBOXES} permission \u2014 a key made with
1273
+ "write sandboxes" alone is refused, and the refusal says when Daytona will stop
1274
+ and delete the box on its own. To reach a twin that must be deleted, a Veris
1275
+ key too: VERIS_API_KEY, or the profile \`veris login\` saved.
1276
+
1277
+ exit code: 0 when the sandbox is deleted; 1 when there is no such sandbox, or
1278
+ the key may not delete one.`;
1279
+ function isPermissionDenied(e) {
1280
+ return typeof e === "object" && e !== null && e.statusCode === 403;
1281
+ }
1282
+ function sandboxBrakes(r) {
1283
+ const stop = r.autoStopMinutes && r.autoStopMinutes > 0 ? `stops after ${r.autoStopMinutes} idle minutes` : "never stops on its own";
1284
+ const del = r.autoDeleteMinutes === void 0 || r.autoDeleteMinutes < 0 ? "is never auto-deleted" : r.autoDeleteMinutes === 0 ? "is deleted as soon as it stops" : `is deleted ${r.autoDeleteMinutes} minutes after it stops`;
1285
+ const destroy = r.expiresAt ? `, and is destroyed at ${r.expiresAt} whatever state it is in` : "";
1286
+ return `${stop} and ${del}${destroy}`;
1287
+ }
1288
+ function teardownRefusedMessage(r) {
1289
+ const key = r.key ? `this DAYTONA_API_KEY ${describeKey(r.key)} lacks the \`${DELETE_SANDBOXES}\` permission` : `Daytona answered 403 (Access denied), which is what a DAYTONA_API_KEY without the \`${DELETE_SANDBOXES}\` permission gets \u2014 the key's own record could not be read to confirm it`;
1290
+ const twin = r.twinId === void 0 ? "" : r.ownsTwin ? ` Its twin ${r.twinId} was deleted before Daytona refused, so nothing is left on the Veris side.` : ` Twin ${r.twinId} is yours and was not touched.`;
1291
+ return `Daytona refused to delete sandbox ${r.sandboxId}: ${key}. The sandbox is still there: it ${sandboxBrakes(r)}.${twin} To delete it sooner, ${DELETE_PERMISSION_FIX}, then run \`veris-daytona teardown ${r.sandboxId}\` with it \u2014 or delete the sandbox in the Daytona dashboard.`;
1292
+ }
871
1293
  export {
1294
+ BUNDLED_CA_FILES,
1295
+ BUNDLED_CA_PATCH_SCRIPT,
872
1296
  CA_CERT_PATH,
873
1297
  ControlPlane,
874
- DEFAULT_REGISTRY_HOSTS,
1298
+ DAYTONA_API_URL,
1299
+ DEFAULT_API_BASE,
1300
+ DELETE_SANDBOXES,
875
1301
  Daytona,
876
1302
  MissingCredentialsError,
1303
+ NODE_TRUST_FLAG,
877
1304
  ReceiptIntegrityError,
878
1305
  SDK_VERSION,
879
1306
  SYSTEM_BUNDLE,
@@ -887,13 +1314,32 @@ export {
887
1314
  VerisGatewayUnreachableError,
888
1315
  VerisUntouchedError,
889
1316
  assertHttpControlPlane,
1317
+ buildNetwork,
1318
+ canDeleteSandboxes,
1319
+ cannotTeardownWarning,
890
1320
  dataPlaneHosts,
1321
+ daytonaApiUrl,
891
1322
  daytona_default as default,
1323
+ directTwinHosts,
1324
+ fetchDaytonaKey,
892
1325
  fetchManual,
1326
+ gatewayIps,
893
1327
  gatewayProxyUrl,
1328
+ isPermissionDenied,
894
1329
  isVerisSandbox,
1330
+ missingKeyMessage,
1331
+ nodeOptionsWithTrust,
1332
+ patchBundledCas,
1333
+ profilePath,
1334
+ requireVerisCredentials,
1335
+ resolveVerisCredentials,
1336
+ sandboxBrakes,
1337
+ teardownRefusedMessage,
1338
+ trustPrelude,
895
1339
  twinHosts,
896
1340
  vendorHosts,
897
- vendoredTrustEnv
1341
+ vendoredTrustEnv,
1342
+ verisOwnsTwin,
1343
+ verisTwinId
898
1344
  };
899
1345
  //# sourceMappingURL=index.js.map