@tangle-network/sandbox 0.11.1 → 0.12.0-develop.20260724231304.911a687

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.
@@ -1,2 +1,2 @@
1
- import { a as TANGLE_JOBS_CONTRACT, c as AgentSandboxBlueprintAbi, d as SandboxCreateParamTypes, f as SandboxCreateResponseParamTypes, i as TANGLE_CHAIN_ID, l as ITangleJobsAbi, n as JOB_SANDBOX_CREATE, o as TANGLE_MAINNET_RPC, p as SandboxIdParamTypes, r as JOB_SANDBOX_DELETE, s as TangleSandboxClientConfig, t as TangleSandboxClient, u as JsonResponseParamTypes } from "../index-Dj8LzvId.js";
1
+ import { a as TANGLE_JOBS_CONTRACT, c as AgentSandboxBlueprintAbi, d as SandboxCreateParamTypes, f as SandboxCreateResponseParamTypes, i as TANGLE_CHAIN_ID, l as ITangleJobsAbi, n as JOB_SANDBOX_CREATE, o as TANGLE_MAINNET_RPC, p as SandboxIdParamTypes, r as JOB_SANDBOX_DELETE, s as TangleSandboxClientConfig, t as TangleSandboxClient, u as JsonResponseParamTypes } from "../index-BZgbJerm.js";
2
2
  export { AgentSandboxBlueprintAbi, ITangleJobsAbi, JOB_SANDBOX_CREATE, JOB_SANDBOX_DELETE, JsonResponseParamTypes, SandboxCreateParamTypes, SandboxCreateResponseParamTypes, SandboxIdParamTypes, TANGLE_CHAIN_ID, TANGLE_JOBS_CONTRACT, TANGLE_MAINNET_RPC, TangleSandboxClient, TangleSandboxClientConfig };
@@ -1,2 +1,2 @@
1
- import { a as TANGLE_JOBS_CONTRACT, c as ITangleJobsAbi, d as SandboxCreateResponseParamTypes, f as SandboxIdParamTypes, i as TANGLE_CHAIN_ID, l as JsonResponseParamTypes, n as JOB_SANDBOX_CREATE, o as TANGLE_MAINNET_RPC, r as JOB_SANDBOX_DELETE, s as AgentSandboxBlueprintAbi, t as TangleSandboxClient, u as SandboxCreateParamTypes } from "../tangle-DHOkbr5a.js";
1
+ import { a as TANGLE_JOBS_CONTRACT, c as ITangleJobsAbi, d as SandboxCreateResponseParamTypes, f as SandboxIdParamTypes, i as TANGLE_CHAIN_ID, l as JsonResponseParamTypes, n as JOB_SANDBOX_CREATE, o as TANGLE_MAINNET_RPC, r as JOB_SANDBOX_DELETE, s as AgentSandboxBlueprintAbi, t as TangleSandboxClient, u as SandboxCreateParamTypes } from "../tangle-DpxmcNGp.js";
2
2
  export { AgentSandboxBlueprintAbi, ITangleJobsAbi, JOB_SANDBOX_CREATE, JOB_SANDBOX_DELETE, JsonResponseParamTypes, SandboxCreateParamTypes, SandboxCreateResponseParamTypes, SandboxIdParamTypes, TANGLE_CHAIN_ID, TANGLE_JOBS_CONTRACT, TANGLE_MAINNET_RPC, TangleSandboxClient };
@@ -1,5 +1,5 @@
1
- import { p as parseErrorResponse } from "./errors-Cbs78OlF.js";
2
- import { t as SandboxInstance } from "./sandbox-DaLPTCRA.js";
1
+ import { p as parseErrorResponse } from "./errors-D9fFOWX9.js";
2
+ import { n as createSandboxInstanceFromResponse } from "./sandbox-D77F7Yja.js";
3
3
  //#region src/tangle/abi.ts
4
4
  /**
5
5
  * Tangle Contract ABI Definitions
@@ -632,6 +632,29 @@ function readString(record, keys) {
632
632
  if (typeof value === "string" && value.length > 0) return value;
633
633
  }
634
634
  }
635
+ /**
636
+ * The incarnation fields are one contract, not three independent hints.
637
+ * Keep the legacy (all absent) wire shape distinct from a partial contract so
638
+ * a readiness-only response cannot authorize a stale runtime credential.
639
+ */
640
+ function classifyFilesystemIncarnation(value) {
641
+ const rawId = value.filesystemIncarnationId;
642
+ const rawProvenance = value.filesystemIncarnationProvenance;
643
+ const rawReadiness = value.filesystemIncarnationReadiness;
644
+ const id = typeof rawId === "string" && rawId.length > 0 ? rawId : void 0;
645
+ const provenance = rawProvenance === "fresh" || rawProvenance === "restored" || rawProvenance === "unknown" ? rawProvenance : void 0;
646
+ const readiness = rawReadiness === "transitioning" || rawReadiness === "ready" ? rawReadiness : void 0;
647
+ return {
648
+ hasAny: rawId !== void 0 || rawProvenance !== void 0 || rawReadiness !== void 0,
649
+ complete: id !== void 0 && provenance !== void 0 && readiness !== void 0,
650
+ id,
651
+ provenance,
652
+ readiness
653
+ };
654
+ }
655
+ function runtimeCredentialsAllowed(incarnation) {
656
+ return !incarnation.hasAny || incarnation.complete && incarnation.readiness === "ready";
657
+ }
635
658
  function entryMetadata(entry) {
636
659
  const metadata = {
637
660
  ...entry.metadata ?? {},
@@ -642,11 +665,16 @@ function entryMetadata(entry) {
642
665
  return Object.keys(metadata).length > 0 ? metadata : void 0;
643
666
  }
644
667
  function sandboxInfoFromEntry(entry) {
668
+ const incarnation = classifyFilesystemIncarnation(entry);
669
+ const allowRuntime = runtimeCredentialsAllowed(incarnation);
645
670
  return {
646
671
  id: entry.id,
647
672
  name: entry.name,
673
+ filesystemIncarnationId: incarnation.complete ? incarnation.id : void 0,
674
+ filesystemIncarnationProvenance: incarnation.complete ? incarnation.provenance : void 0,
675
+ filesystemIncarnationReadiness: incarnation.complete ? incarnation.readiness : void 0,
648
676
  status: entry.status === "deleted" ? "stopped" : entry.status,
649
- connection: entry.runtimeUrl ? {
677
+ connection: allowRuntime && entry.runtimeUrl ? {
650
678
  runtimeUrl: entry.runtimeUrl,
651
679
  authToken: entry.runtimeToken
652
680
  } : void 0,
@@ -681,14 +709,26 @@ function sandboxEntryFromWire(fallbackId, raw) {
681
709
  "sidecarToken",
682
710
  "token"
683
711
  ]) ?? "";
712
+ const tupleField = (key) => Object.hasOwn(raw, key) ? raw[key] : void 0;
713
+ const tupleId = tupleField("filesystemIncarnationId") === void 0 ? void 0 : typeof tupleField("filesystemIncarnationId") === "string" ? tupleField("filesystemIncarnationId") : "";
714
+ const tupleProvenance = tupleField("filesystemIncarnationProvenance") === void 0 ? void 0 : tupleField("filesystemIncarnationProvenance");
715
+ const tupleReadiness = tupleField("filesystemIncarnationReadiness") === void 0 ? void 0 : tupleField("filesystemIncarnationReadiness");
716
+ const allowRuntime = runtimeCredentialsAllowed(classifyFilesystemIncarnation({
717
+ filesystemIncarnationId: tupleId,
718
+ filesystemIncarnationProvenance: tupleProvenance,
719
+ filesystemIncarnationReadiness: tupleReadiness
720
+ }));
684
721
  const rawStatus = readString(raw, ["status"]) ?? "running";
685
722
  const createdAtRaw = readString(raw, ["createdAt", "created_at"]);
686
723
  const createdAt = createdAtRaw ? new Date(createdAtRaw) : /* @__PURE__ */ new Date();
687
724
  return {
688
725
  id,
689
726
  name: readString(raw, ["name"]),
690
- runtimeUrl,
691
- runtimeToken,
727
+ filesystemIncarnationId: tupleId,
728
+ filesystemIncarnationProvenance: tupleProvenance,
729
+ filesystemIncarnationReadiness: tupleReadiness,
730
+ runtimeUrl: allowRuntime ? runtimeUrl : "",
731
+ runtimeToken: allowRuntime ? runtimeToken : "",
692
732
  status: rawStatus === "stopped" || rawStatus === "deleted" || rawStatus === "running" ? rawStatus : "running",
693
733
  createdAt,
694
734
  metadata,
@@ -712,6 +752,7 @@ var TangleSandboxClient = class {
712
752
  * Create a sandbox via on-chain job submission.
713
753
  */
714
754
  async create(options) {
755
+ if (options?.billingOwnerId !== void 0) throw new Error("billingOwnerId is supported only by the HTTP Sandbox API client");
715
756
  const createOptions = withAttestationNonce(options ?? {});
716
757
  const encoded = this.chain.encodeSandboxCreateInputs(createOptions);
717
758
  const { result: _result, callId, blockNumber } = await this.chain.submitJobAndWait(0, encoded);
@@ -728,23 +769,24 @@ var TangleSandboxClient = class {
728
769
  throw new Error("Failed to decode create job result");
729
770
  }
730
771
  else throw new Error("No result returned from create job");
731
- const runtimeUrl = responseJson.runtimeUrl ?? responseJson.sidecarUrl ?? responseJson.sidecar_url ?? "";
732
- const runtimeToken = responseJson.token ?? responseJson.runtimeToken ?? responseJson.sidecarToken ?? "";
733
- if (runtimeUrl && !runtimeToken) throw new Error("Create job returned a runtime URL without an auth token");
772
+ const wireEntry = sandboxEntryFromWire(sandboxId, responseJson);
773
+ if (wireEntry.runtimeUrl && !wireEntry.runtimeToken) throw new Error("Create job returned a runtime URL without an auth token");
774
+ const allowRuntime = runtimeCredentialsAllowed(classifyFilesystemIncarnation(wireEntry));
734
775
  const entry = {
776
+ ...wireEntry,
735
777
  id: sandboxId,
736
- name: options?.name,
737
- runtimeUrl,
738
- runtimeToken,
739
- status: "running",
740
- createdAt: /* @__PURE__ */ new Date(),
741
- metadata: createOptions.metadata,
742
- teeAttestationJson: responseJson.teeAttestationJson,
743
- teePublicKeyJson: responseJson.teePublicKeyJson,
744
- attestationNonce: attestationNonceFromOptions(createOptions)
778
+ name: options?.name ?? wireEntry.name,
779
+ runtimeUrl: allowRuntime ? wireEntry.runtimeUrl : "",
780
+ runtimeToken: allowRuntime ? wireEntry.runtimeToken : "",
781
+ status: wireEntry.status,
782
+ createdAt: wireEntry.createdAt,
783
+ metadata: createOptions.metadata ?? wireEntry.metadata,
784
+ teeAttestationJson: wireEntry.teeAttestationJson ?? responseJson.teeAttestationJson,
785
+ teePublicKeyJson: wireEntry.teePublicKeyJson ?? responseJson.teePublicKeyJson,
786
+ attestationNonce: attestationNonceFromOptions(createOptions) ?? wireEntry.attestationNonce
745
787
  };
746
788
  this.sandboxes.set(sandboxId, entry);
747
- return new SandboxInstance(this, sandboxInfoFromEntry(entry));
789
+ return createSandboxInstanceFromResponse(this, sandboxInfoFromEntry(entry));
748
790
  }
749
791
  /**
750
792
  * Get a sandbox by id. Rehydrates from the operator API when configured so
@@ -758,7 +800,7 @@ var TangleSandboxClient = class {
758
800
  throw parseErrorResponse(response.status, body, void 0, response.headers);
759
801
  }
760
802
  const data = await response.json();
761
- return new SandboxInstance(this, sandboxInfoFromEntry(sandboxEntryFromWire(id, data)));
803
+ return createSandboxInstanceFromResponse(this, sandboxInfoFromEntry(sandboxEntryFromWire(id, data)));
762
804
  }
763
805
  /**
764
806
  * Route interception for SandboxInstance lifecycle calls.
@@ -781,7 +823,7 @@ var TangleSandboxClient = class {
781
823
  if (getMatch && method === "GET") {
782
824
  const id = decodeURIComponent(requirePathGroup(getMatch, 1));
783
825
  let entry = this.sandboxes.get(id);
784
- if (!entry && this.operatorApiUrl) {
826
+ if (this.operatorApiUrl) {
785
827
  const operatorResponse = await this.fetchOperatorApi(`/api/sandboxes/${encodeURIComponent(id)}`, { method: "GET" });
786
828
  if (!operatorResponse.ok) return operatorResponse;
787
829
  entry = sandboxEntryFromWire(id, await operatorResponse.json());
@@ -861,8 +903,15 @@ var TangleSandboxClient = class {
861
903
  throw new Error(`Operation not supported via on-chain client: ${method} ${path}`);
862
904
  }
863
905
  async fetchRuntime(sandboxId, runtimePath, options) {
864
- const entry = this.sandboxes.get(sandboxId);
865
- if (!entry?.runtimeUrl) return new Response(JSON.stringify({ error: "Sandbox runtime unavailable" }), {
906
+ let entry = this.sandboxes.get(sandboxId);
907
+ if (this.operatorApiUrl) {
908
+ const operatorResponse = await this.fetchOperatorApi(`/api/sandboxes/${encodeURIComponent(sandboxId)}`, { method: "GET" });
909
+ if (!operatorResponse.ok) return operatorResponse;
910
+ entry = sandboxEntryFromWire(sandboxId, await operatorResponse.json());
911
+ this.sandboxes.set(entry.id, entry);
912
+ }
913
+ const incarnation = entry ? classifyFilesystemIncarnation(entry) : void 0;
914
+ if (!entry || !incarnation || !runtimeCredentialsAllowed(incarnation) || !entry.runtimeUrl || !entry.runtimeToken) return new Response(JSON.stringify({ error: "Sandbox runtime unavailable" }), {
866
915
  status: 503,
867
916
  headers: { "Content-Type": "application/json" }
868
917
  });