@tangle-network/agent-app 0.46.17 → 0.46.18

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.
@@ -112,7 +112,7 @@ import {
112
112
  flattenHistory,
113
113
  readSandboxBinaryBytes,
114
114
  statSandboxFileSize
115
- } from "../chunk-RXHGOLZR.js";
115
+ } from "../chunk-47LN6Q2U.js";
116
116
  import "../chunk-TH7L265V.js";
117
117
  import "../chunk-LWSJK546.js";
118
118
  import "../chunk-P5PIAQVS.js";
@@ -1714,16 +1714,28 @@ function normalizedEgressPolicy(policy) {
1714
1714
  }
1715
1715
  return JSON.stringify(canonicalizeJson(normalized));
1716
1716
  }
1717
+ function existingBoxEgressError(operation, box, stage, name, cause) {
1718
+ const error = cause instanceof Error ? cause : new Error(String(cause));
1719
+ const status = error.status;
1720
+ const suffix = operation === "migration" ? "/egress" : "";
1721
+ return Object.assign(
1722
+ new Error(`egress policy ${operation} failed on ${stage} box ${name}: ${error.message}`, {
1723
+ cause: error
1724
+ }),
1725
+ {
1726
+ origin: "sandbox-api",
1727
+ endpoint: `/v1/sandboxes/${encodeURIComponent(box.id)}${suffix}`,
1728
+ ...typeof status === "number" || typeof status === "string" ? { status } : {}
1729
+ }
1730
+ );
1731
+ }
1717
1732
  async function assertExistingBoxEgress(box, desired, migrate, stage, name) {
1718
1733
  if (!desired) return;
1719
1734
  let current;
1720
1735
  try {
1721
1736
  current = await box.egress.get();
1722
1737
  } catch (cause) {
1723
- const error = cause instanceof Error ? cause : new Error(String(cause));
1724
- throw new Error(`egress policy read failed on ${stage} box ${name}: ${error.message}`, {
1725
- cause: error
1726
- });
1738
+ throw existingBoxEgressError("read", box, stage, name, cause);
1727
1739
  }
1728
1740
  const matchingPolicy = normalizedEgressPolicy(current.policy) === normalizedEgressPolicy(desired);
1729
1741
  const explicitSource = current.source !== "platform";
@@ -1736,10 +1748,7 @@ async function assertExistingBoxEgress(box, desired, migrate, stage, name) {
1736
1748
  if (migratedPolicy && migrated.source !== "platform") return;
1737
1749
  current = migrated;
1738
1750
  } catch (cause) {
1739
- const error = cause instanceof Error ? cause : new Error(String(cause));
1740
- throw new Error(`egress policy migration failed on ${stage} box ${name}: ${error.message}`, {
1741
- cause: error
1742
- });
1751
+ throw existingBoxEgressError("migration", box, stage, name, cause);
1743
1752
  }
1744
1753
  }
1745
1754
  throw new SandboxEgressPolicyMismatchError(
@@ -1790,12 +1799,36 @@ function isUnbringableBoxError(error) {
1790
1799
  const diagnostics = serializeSandboxProvisioningError(error);
1791
1800
  return isSandboxHostCapacityFailure(diagnostics) || isSandboxBoxConfigFailure(diagnostics);
1792
1801
  }
1802
+ async function requestSandboxReplacement(recover, failure, label) {
1803
+ if (!recover) throw failure.error;
1804
+ const recovery = await recover(failure);
1805
+ if (!recovery.succeeded) throw recovery.error;
1806
+ if (!recovery.value) throw failure.error;
1807
+ const replacementBoxKey = recovery.value.replacementBoxKey.trim();
1808
+ if (!replacementBoxKey || replacementBoxKey === failure.boxKey) {
1809
+ throw new Error(
1810
+ `${label} must return a fresh replacement box key for ${failure.boxKey}`,
1811
+ { cause: failure.error }
1812
+ );
1813
+ }
1814
+ return { ...recovery.value, replacementBoxKey };
1815
+ }
1816
+ async function requestMissingSandboxReplacement(shell, failure) {
1817
+ const diagnostics = serializeSandboxProvisioningError(failure.error);
1818
+ if (!isSandboxApiSandboxMissingFailure(diagnostics)) throw failure.error;
1819
+ return requestSandboxReplacement(
1820
+ shell.recoverMissingSandbox,
1821
+ failure,
1822
+ "missing sandbox recovery"
1823
+ );
1824
+ }
1793
1825
  async function provisionWorkspaceSandbox(shell, options) {
1794
1826
  const { workspaceId, userId, harness, forceNew, onProgress, billingOwnerId } = options;
1795
1827
  const resolved = await resolveWorkspaceSandboxClient(shell, workspaceId, userId);
1796
1828
  const { scope, client } = resolved;
1797
1829
  let name = resolved.name;
1798
1830
  let recoveryRestore;
1831
+ let replacementRequested = false;
1799
1832
  const resources = shell.resources ?? DEFAULT_SANDBOX_RESOURCES;
1800
1833
  const resumeTimeout = shell.provisionTimeoutMs ?? DEFAULT_PROVISION_TIMEOUT_MS;
1801
1834
  const existing = await listRunning(client, name);
@@ -1807,21 +1840,35 @@ async function provisionWorkspaceSandbox(shell, options) {
1807
1840
  throw new Error(`forceNew: sandbox ${name} could not be deleted`, { cause: dropped.error });
1808
1841
  }
1809
1842
  } else if (found.metadata?.harness === harness) {
1810
- const ready = await refreshRuntimeConnection(client, found);
1811
- if (await isReusableBox(ready, harness, shell.livenessProbe)) {
1812
- return finalizeExistingBox(shell, client, ready, "reused", name, workspaceId, userId, harness, scope);
1843
+ try {
1844
+ const ready = await refreshRuntimeConnection(client, found);
1845
+ if (await isReusableBox(ready, harness, shell.livenessProbe)) {
1846
+ return await finalizeExistingBox(shell, client, ready, "reused", name, workspaceId, userId, harness, scope);
1847
+ }
1848
+ const recovered = await recoverUnresponsiveBox(
1849
+ client,
1850
+ ready,
1851
+ harness,
1852
+ shell.livenessProbe,
1853
+ "reused",
1854
+ name,
1855
+ resumeTimeout,
1856
+ onProgress
1857
+ );
1858
+ return await finalizeExistingBox(shell, client, recovered, "reused", name, workspaceId, userId, harness, scope);
1859
+ } catch (cause) {
1860
+ const error = cause instanceof Error ? cause : new Error(String(cause));
1861
+ const recovery = await requestMissingSandboxReplacement(shell, {
1862
+ box: found,
1863
+ error,
1864
+ scope,
1865
+ boxKey: name,
1866
+ stage: "reused"
1867
+ });
1868
+ name = recovery.replacementBoxKey;
1869
+ recoveryRestore = recovery.restore;
1870
+ replacementRequested = true;
1813
1871
  }
1814
- const recovered = await recoverUnresponsiveBox(
1815
- client,
1816
- ready,
1817
- harness,
1818
- shell.livenessProbe,
1819
- "reused",
1820
- name,
1821
- resumeTimeout,
1822
- onProgress
1823
- );
1824
- return finalizeExistingBox(shell, client, recovered, "reused", name, workspaceId, userId, harness, scope);
1825
1872
  } else {
1826
1873
  const dropped = await deleteBox(found);
1827
1874
  if (!dropped.succeeded) {
@@ -1832,46 +1879,48 @@ async function provisionWorkspaceSandbox(shell, options) {
1832
1879
  }
1833
1880
  }
1834
1881
  }
1835
- if (!forceNew && shell.resumeStopped !== false) {
1882
+ if (!replacementRequested && !forceNew && shell.resumeStopped !== false) {
1836
1883
  const stopped = await listStopped(client, name);
1837
1884
  if (!stopped.succeeded) throw stopped.error;
1838
1885
  if (stopped.value) {
1839
1886
  const resumed = await resumeStoppedBox(stopped.value, resumeTimeout, onProgress);
1840
1887
  if (!resumed.succeeded) {
1841
- if (!shell.recoverStoppedSandbox) throw resumed.error;
1842
- const recovery = await shell.recoverStoppedSandbox({
1843
- box: stopped.value,
1844
- error: resumed.error,
1845
- scope,
1846
- boxKey: name
1847
- });
1848
- if (!recovery.succeeded) throw recovery.error;
1849
- if (!recovery.value) throw resumed.error;
1850
- const replacementBoxKey = recovery.value.replacementBoxKey.trim();
1851
- if (!replacementBoxKey || replacementBoxKey === name) {
1852
- throw new Error(
1853
- `stopped sandbox recovery must return a fresh replacement box key for ${name}`,
1854
- { cause: resumed.error }
1855
- );
1856
- }
1857
- name = replacementBoxKey;
1858
- recoveryRestore = recovery.value.restore;
1888
+ const recovery = await requestSandboxReplacement(
1889
+ shell.recoverStoppedSandbox,
1890
+ { box: stopped.value, error: resumed.error, scope, boxKey: name },
1891
+ "stopped sandbox recovery"
1892
+ );
1893
+ name = recovery.replacementBoxKey;
1894
+ recoveryRestore = recovery.restore;
1859
1895
  } else {
1860
- const box2 = await refreshRuntimeConnection(client, resumed.value);
1861
- if (await isReusableBox(box2, harness, shell.livenessProbe)) {
1862
- return finalizeExistingBox(shell, client, box2, "resumed", name, workspaceId, userId, harness, scope);
1896
+ try {
1897
+ const box2 = await refreshRuntimeConnection(client, resumed.value);
1898
+ if (await isReusableBox(box2, harness, shell.livenessProbe)) {
1899
+ return await finalizeExistingBox(shell, client, box2, "resumed", name, workspaceId, userId, harness, scope);
1900
+ }
1901
+ const recovered = await recoverUnresponsiveBox(
1902
+ client,
1903
+ box2,
1904
+ harness,
1905
+ shell.livenessProbe,
1906
+ "resumed",
1907
+ name,
1908
+ resumeTimeout,
1909
+ onProgress
1910
+ );
1911
+ return await finalizeExistingBox(shell, client, recovered, "resumed", name, workspaceId, userId, harness, scope);
1912
+ } catch (cause) {
1913
+ const error = cause instanceof Error ? cause : new Error(String(cause));
1914
+ const recovery = await requestMissingSandboxReplacement(shell, {
1915
+ box: stopped.value,
1916
+ error,
1917
+ scope,
1918
+ boxKey: name,
1919
+ stage: "resumed"
1920
+ });
1921
+ name = recovery.replacementBoxKey;
1922
+ recoveryRestore = recovery.restore;
1863
1923
  }
1864
- const recovered = await recoverUnresponsiveBox(
1865
- client,
1866
- box2,
1867
- harness,
1868
- shell.livenessProbe,
1869
- "resumed",
1870
- name,
1871
- resumeTimeout,
1872
- onProgress
1873
- );
1874
- return finalizeExistingBox(shell, client, recovered, "resumed", name, workspaceId, userId, harness, scope);
1875
1924
  }
1876
1925
  }
1877
1926
  }
@@ -2399,4 +2448,4 @@ export {
2399
2448
  isTerminalPromptEvent,
2400
2449
  detectInteractiveQuestion
2401
2450
  };
2402
- //# sourceMappingURL=chunk-RXHGOLZR.js.map
2451
+ //# sourceMappingURL=chunk-47LN6Q2U.js.map