@tangle-network/agent-app 0.46.17 → 0.46.19

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-HDQWY5AY.js";
116
116
  import "../chunk-TH7L265V.js";
117
117
  import "../chunk-LWSJK546.js";
118
118
  import "../chunk-P5PIAQVS.js";
@@ -511,15 +511,24 @@ function isSandboxApiBearerAuthFailure(diagnostics) {
511
511
  return /^\/v1\/sandboxes\/[^/?#]+(?:\/(?!runtime(?:[/?#]|$))[^?#]*)?(?:[?#].*)?$/.test(endpointPath);
512
512
  });
513
513
  }
514
+ function isSandboxBackingContainerMissingMessage(message) {
515
+ return /host-agent startcontainer failed \(404\):/i.test(message) && /container not found/i.test(message) && /"code"\s*:\s*"not_found"/i.test(message);
516
+ }
514
517
  function isSandboxApiSandboxMissingFailure(diagnostics) {
515
518
  return diagnostics.causes.some((cause) => {
516
519
  const status = typeof cause.status === "number" ? cause.status : typeof cause.status === "string" ? Number.parseInt(cause.status, 10) : void 0;
517
- if (status !== 404) return false;
518
520
  if (cause.origin !== "sandbox-api") return false;
519
521
  if (typeof cause.endpoint !== "string") return false;
520
522
  const endpointPath = sandboxApiEndpointPath(cause.endpoint);
521
523
  if (!endpointPath) return false;
522
- return /^\/v1\/sandboxes\/[^/?#]+(?:\/(?!runtime(?:[/?#]|$))[^?#]*)?(?:[?#].*)?$/.test(endpointPath);
524
+ const sandboxResource = /^\/v1\/sandboxes\/[^/?#]+(?:\/(?!runtime(?:[/?#]|$))[^?#]*)?(?:[?#].*)?$/.test(endpointPath);
525
+ if (!sandboxResource) return false;
526
+ if (status === 404) return true;
527
+ if (status !== 500 || !/^\/v1\/sandboxes\/[^/?#]+\/resume(?:[?#].*)?$/.test(endpointPath)) {
528
+ return false;
529
+ }
530
+ const message = typeof cause.message === "string" ? cause.message : "";
531
+ return isSandboxBackingContainerMissingMessage(message);
523
532
  });
524
533
  }
525
534
  function isSandboxHostCapacityFailure(diagnostics) {
@@ -1605,14 +1614,29 @@ async function isReusableBox(box, harness, probe) {
1605
1614
  else livenessVerifiedAt.delete(box.id);
1606
1615
  return alive;
1607
1616
  }
1617
+ function stoppedBoxResumeError(box, cause) {
1618
+ const error = cause instanceof Error ? cause : new Error(String(cause));
1619
+ if (!isSandboxBackingContainerMissingMessage(error.message)) return cause;
1620
+ const status = typeof cause === "object" && cause !== null ? cause.status : void 0;
1621
+ if (typeof status !== "number" && typeof status !== "string") return cause;
1622
+ const code = error.code;
1623
+ const wrapped = new Error(error.message, { cause: error });
1624
+ wrapped.name = error.name;
1625
+ return Object.assign(wrapped, {
1626
+ status,
1627
+ origin: "sandbox-api",
1628
+ endpoint: `/v1/sandboxes/${encodeURIComponent(box.id)}/resume`,
1629
+ ...typeof code === "number" || typeof code === "string" ? { code } : {}
1630
+ });
1631
+ }
1608
1632
  async function resumeStoppedBox(box, timeoutMs, onProgress) {
1609
1633
  try {
1610
1634
  livenessVerifiedAt.delete(box.id);
1611
1635
  await box.resume({ timeoutMs });
1612
1636
  await box.waitFor("running", { timeoutMs, ...onProgress ? { onProgress } : {} });
1613
1637
  return ok(box);
1614
- } catch (err) {
1615
- return fail(err);
1638
+ } catch (cause) {
1639
+ return fail(stoppedBoxResumeError(box, cause));
1616
1640
  }
1617
1641
  }
1618
1642
  async function recoverUnresponsiveBox(client, box, harness, probe, stage, name, resumeTimeout, onProgress) {
@@ -1714,16 +1738,28 @@ function normalizedEgressPolicy(policy) {
1714
1738
  }
1715
1739
  return JSON.stringify(canonicalizeJson(normalized));
1716
1740
  }
1741
+ function existingBoxEgressError(operation, box, stage, name, cause) {
1742
+ const error = cause instanceof Error ? cause : new Error(String(cause));
1743
+ const status = error.status;
1744
+ const suffix = operation === "migration" ? "/egress" : "";
1745
+ return Object.assign(
1746
+ new Error(`egress policy ${operation} failed on ${stage} box ${name}: ${error.message}`, {
1747
+ cause: error
1748
+ }),
1749
+ {
1750
+ origin: "sandbox-api",
1751
+ endpoint: `/v1/sandboxes/${encodeURIComponent(box.id)}${suffix}`,
1752
+ ...typeof status === "number" || typeof status === "string" ? { status } : {}
1753
+ }
1754
+ );
1755
+ }
1717
1756
  async function assertExistingBoxEgress(box, desired, migrate, stage, name) {
1718
1757
  if (!desired) return;
1719
1758
  let current;
1720
1759
  try {
1721
1760
  current = await box.egress.get();
1722
1761
  } 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
- });
1762
+ throw existingBoxEgressError("read", box, stage, name, cause);
1727
1763
  }
1728
1764
  const matchingPolicy = normalizedEgressPolicy(current.policy) === normalizedEgressPolicy(desired);
1729
1765
  const explicitSource = current.source !== "platform";
@@ -1736,10 +1772,7 @@ async function assertExistingBoxEgress(box, desired, migrate, stage, name) {
1736
1772
  if (migratedPolicy && migrated.source !== "platform") return;
1737
1773
  current = migrated;
1738
1774
  } 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
- });
1775
+ throw existingBoxEgressError("migration", box, stage, name, cause);
1743
1776
  }
1744
1777
  }
1745
1778
  throw new SandboxEgressPolicyMismatchError(
@@ -1790,12 +1823,36 @@ function isUnbringableBoxError(error) {
1790
1823
  const diagnostics = serializeSandboxProvisioningError(error);
1791
1824
  return isSandboxHostCapacityFailure(diagnostics) || isSandboxBoxConfigFailure(diagnostics);
1792
1825
  }
1826
+ async function requestSandboxReplacement(recover, failure, label) {
1827
+ if (!recover) throw failure.error;
1828
+ const recovery = await recover(failure);
1829
+ if (!recovery.succeeded) throw recovery.error;
1830
+ if (!recovery.value) throw failure.error;
1831
+ const replacementBoxKey = recovery.value.replacementBoxKey.trim();
1832
+ if (!replacementBoxKey || replacementBoxKey === failure.boxKey) {
1833
+ throw new Error(
1834
+ `${label} must return a fresh replacement box key for ${failure.boxKey}`,
1835
+ { cause: failure.error }
1836
+ );
1837
+ }
1838
+ return { ...recovery.value, replacementBoxKey };
1839
+ }
1840
+ async function requestMissingSandboxReplacement(shell, failure) {
1841
+ const diagnostics = serializeSandboxProvisioningError(failure.error);
1842
+ if (!isSandboxApiSandboxMissingFailure(diagnostics)) throw failure.error;
1843
+ return requestSandboxReplacement(
1844
+ shell.recoverMissingSandbox,
1845
+ failure,
1846
+ "missing sandbox recovery"
1847
+ );
1848
+ }
1793
1849
  async function provisionWorkspaceSandbox(shell, options) {
1794
1850
  const { workspaceId, userId, harness, forceNew, onProgress, billingOwnerId } = options;
1795
1851
  const resolved = await resolveWorkspaceSandboxClient(shell, workspaceId, userId);
1796
1852
  const { scope, client } = resolved;
1797
1853
  let name = resolved.name;
1798
1854
  let recoveryRestore;
1855
+ let replacementRequested = false;
1799
1856
  const resources = shell.resources ?? DEFAULT_SANDBOX_RESOURCES;
1800
1857
  const resumeTimeout = shell.provisionTimeoutMs ?? DEFAULT_PROVISION_TIMEOUT_MS;
1801
1858
  const existing = await listRunning(client, name);
@@ -1807,21 +1864,35 @@ async function provisionWorkspaceSandbox(shell, options) {
1807
1864
  throw new Error(`forceNew: sandbox ${name} could not be deleted`, { cause: dropped.error });
1808
1865
  }
1809
1866
  } 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);
1867
+ try {
1868
+ const ready = await refreshRuntimeConnection(client, found);
1869
+ if (await isReusableBox(ready, harness, shell.livenessProbe)) {
1870
+ return await finalizeExistingBox(shell, client, ready, "reused", name, workspaceId, userId, harness, scope);
1871
+ }
1872
+ const recovered = await recoverUnresponsiveBox(
1873
+ client,
1874
+ ready,
1875
+ harness,
1876
+ shell.livenessProbe,
1877
+ "reused",
1878
+ name,
1879
+ resumeTimeout,
1880
+ onProgress
1881
+ );
1882
+ return await finalizeExistingBox(shell, client, recovered, "reused", name, workspaceId, userId, harness, scope);
1883
+ } catch (cause) {
1884
+ const error = cause instanceof Error ? cause : new Error(String(cause));
1885
+ const recovery = await requestMissingSandboxReplacement(shell, {
1886
+ box: found,
1887
+ error,
1888
+ scope,
1889
+ boxKey: name,
1890
+ stage: "reused"
1891
+ });
1892
+ name = recovery.replacementBoxKey;
1893
+ recoveryRestore = recovery.restore;
1894
+ replacementRequested = true;
1813
1895
  }
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
1896
  } else {
1826
1897
  const dropped = await deleteBox(found);
1827
1898
  if (!dropped.succeeded) {
@@ -1832,46 +1903,48 @@ async function provisionWorkspaceSandbox(shell, options) {
1832
1903
  }
1833
1904
  }
1834
1905
  }
1835
- if (!forceNew && shell.resumeStopped !== false) {
1906
+ if (!replacementRequested && !forceNew && shell.resumeStopped !== false) {
1836
1907
  const stopped = await listStopped(client, name);
1837
1908
  if (!stopped.succeeded) throw stopped.error;
1838
1909
  if (stopped.value) {
1839
1910
  const resumed = await resumeStoppedBox(stopped.value, resumeTimeout, onProgress);
1840
1911
  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;
1912
+ const recovery = await requestSandboxReplacement(
1913
+ shell.recoverStoppedSandbox,
1914
+ { box: stopped.value, error: resumed.error, scope, boxKey: name },
1915
+ "stopped sandbox recovery"
1916
+ );
1917
+ name = recovery.replacementBoxKey;
1918
+ recoveryRestore = recovery.restore;
1859
1919
  } 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);
1920
+ try {
1921
+ const box2 = await refreshRuntimeConnection(client, resumed.value);
1922
+ if (await isReusableBox(box2, harness, shell.livenessProbe)) {
1923
+ return await finalizeExistingBox(shell, client, box2, "resumed", name, workspaceId, userId, harness, scope);
1924
+ }
1925
+ const recovered = await recoverUnresponsiveBox(
1926
+ client,
1927
+ box2,
1928
+ harness,
1929
+ shell.livenessProbe,
1930
+ "resumed",
1931
+ name,
1932
+ resumeTimeout,
1933
+ onProgress
1934
+ );
1935
+ return await finalizeExistingBox(shell, client, recovered, "resumed", name, workspaceId, userId, harness, scope);
1936
+ } catch (cause) {
1937
+ const error = cause instanceof Error ? cause : new Error(String(cause));
1938
+ const recovery = await requestMissingSandboxReplacement(shell, {
1939
+ box: stopped.value,
1940
+ error,
1941
+ scope,
1942
+ boxKey: name,
1943
+ stage: "resumed"
1944
+ });
1945
+ name = recovery.replacementBoxKey;
1946
+ recoveryRestore = recovery.restore;
1863
1947
  }
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
1948
  }
1876
1949
  }
1877
1950
  }
@@ -2399,4 +2472,4 @@ export {
2399
2472
  isTerminalPromptEvent,
2400
2473
  detectInteractiveQuestion
2401
2474
  };
2402
- //# sourceMappingURL=chunk-RXHGOLZR.js.map
2475
+ //# sourceMappingURL=chunk-HDQWY5AY.js.map