@tangle-network/agent-app 0.60.103 → 0.60.104

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.
@@ -42,7 +42,7 @@ import {
42
42
  flattenHistory,
43
43
  readSandboxBinaryBytes,
44
44
  statSandboxFileSize
45
- } from "../chunk-P2EKAQUB.js";
45
+ } from "../chunk-BTBDJNHK.js";
46
46
  import "../chunk-4LBCTFBP.js";
47
47
  import "../chunk-WVESWAJJ.js";
48
48
  import "../chunk-LWSJK546.js";
@@ -1197,6 +1197,7 @@ var DEFAULT_SANDBOX_RESOURCES = {
1197
1197
  };
1198
1198
  var _cached = null;
1199
1199
  var livenessVerifiedAt = /* @__PURE__ */ new Map();
1200
+ var verifiedEgress = /* @__PURE__ */ new Map();
1200
1201
  function getClientFromCreds(creds) {
1201
1202
  const fingerprint = `${creds.apiKey} ${creds.baseUrl}`;
1202
1203
  if (_cached && _cached.fingerprint === fingerprint) return _cached.client;
@@ -1215,6 +1216,7 @@ function getClient(shell) {
1215
1216
  function resetClientCache() {
1216
1217
  _cached = null;
1217
1218
  livenessVerifiedAt.clear();
1219
+ verifiedEgress.clear();
1218
1220
  }
1219
1221
  function buildAppToolMcpServers(options) {
1220
1222
  const entries = {};
@@ -1619,7 +1621,7 @@ function deferredCorpusHash(files) {
1619
1621
  })).sort((a, b) => a.p < b.p ? -1 : a.p > b.p ? 1 : 0);
1620
1622
  return createHash("sha256").update(JSON.stringify(norm), "utf8").digest("hex");
1621
1623
  }
1622
- async function materializeDeferredFilesForExistingBox(shell, client, box, stage, name, workspaceId, userId, harness) {
1624
+ async function materializeDeferredFilesForExistingBox(shell, client, box, stage, name, workspaceId, userId, harness, corpusMarker) {
1623
1625
  if (!shell.deferProfileFiles) return ok(box);
1624
1626
  const connectedIntegrationIds = await shell.connectedIntegrationIds(workspaceId);
1625
1627
  const buildCtx = {
@@ -1631,9 +1633,31 @@ async function materializeDeferredFilesForExistingBox(shell, client, box, stage,
1631
1633
  const fullProfile = shell.profile({ extraFiles: files, harness });
1632
1634
  const { deferredFiles } = splitDeferredProfileFiles(fullProfile);
1633
1635
  if (deferredFiles.length === 0) return ok(box);
1636
+ const corpusHash = deferredCorpusHash(deferredFiles);
1634
1637
  const stampedHash = box.metadata?.[DEFERRED_CORPUS_HASH_KEY];
1635
- if (typeof stampedHash === "string" && stampedHash === deferredCorpusHash(deferredFiles)) return ok(box);
1636
- return writeDeferredFilesWithRuntimeAuthRefresh(client, box, deferredFiles, stage, name);
1638
+ if (typeof stampedHash === "string" && stampedHash === corpusHash) return ok(box);
1639
+ if (corpusMarker && await corpusMarker === corpusHash) return ok(box);
1640
+ const written = await writeDeferredFilesWithRuntimeAuthRefresh(client, box, deferredFiles, stage, name);
1641
+ if (written.succeeded) await writeDeferredCorpusMarker(written.value, corpusHash);
1642
+ return written;
1643
+ }
1644
+ var DEFERRED_CORPUS_MARKER_PATH = ".agent-app/deferred-corpus-hash";
1645
+ var DEFERRED_CORPUS_HASH_RE = /^[0-9a-f]{64}$/;
1646
+ async function readDeferredCorpusMarker(box) {
1647
+ if (typeof box.read !== "function") return null;
1648
+ try {
1649
+ const value = (await box.read(`/home/agent/${DEFERRED_CORPUS_MARKER_PATH}`)).trim();
1650
+ return DEFERRED_CORPUS_HASH_RE.test(value) ? value : null;
1651
+ } catch {
1652
+ return null;
1653
+ }
1654
+ }
1655
+ async function writeDeferredCorpusMarker(box, corpusHash) {
1656
+ if (typeof box.fs?.writeMany !== "function") return;
1657
+ try {
1658
+ await box.fs.writeMany([{ path: DEFERRED_CORPUS_MARKER_PATH, content: corpusHash }]);
1659
+ } catch {
1660
+ }
1637
1661
  }
1638
1662
  async function listRunning(client, name) {
1639
1663
  try {
@@ -1986,8 +2010,62 @@ async function resolveWorkspaceRuntimeEnv(shell, scope) {
1986
2010
  assertEnvWithinLimits(env);
1987
2011
  return env;
1988
2012
  }
1989
- async function finalizeExistingBox(shell, client, box, stage, name, workspaceId, userId, harness, scope, runtimeEnv) {
1990
- await assertExistingBoxEgress(box, shell.egressPolicy, shell.migrateEgressPolicy, stage, name);
2013
+ function prefetchExistingBoxReads(shell, box, runtimeEnv, measure) {
2014
+ const prefetch = {};
2015
+ if (shell.egressPolicy && hasVerifiedEgress(box, shell.egressPolicy)) {
2016
+ prefetch.egressVerified = true;
2017
+ } else if (shell.egressPolicy) {
2018
+ prefetch.egress = measure("egress", async () => {
2019
+ try {
2020
+ return ok(await box.egress.get());
2021
+ } catch (cause) {
2022
+ return fail(cause);
2023
+ }
2024
+ });
2025
+ }
2026
+ if (shell.deferProfileFiles) prefetch.corpusMarker = readDeferredCorpusMarker(box);
2027
+ if (!egressGatesCredentials(shell.egressPolicy)) {
2028
+ prefetch.runtimeEnv = runtimeEnv.then(
2029
+ (env) => Object.keys(env).length === 0 ? ok(void 0) : measure("runtime_env_push", () => box.setRuntimeEnv(env)).then(() => ok(void 0), (cause) => fail(cause)),
2030
+ () => ok(void 0)
2031
+ );
2032
+ }
2033
+ return prefetch;
2034
+ }
2035
+ function egressGatesCredentials(desired) {
2036
+ return desired !== void 0 && desired.mode !== "open";
2037
+ }
2038
+ var VERIFIED_EGRESS_TTL_MS = 10 * 6e4;
2039
+ var VERIFIED_EGRESS_MAX = 1e3;
2040
+ function verifiedEgressKey(box, desired) {
2041
+ if (desired.mode !== "open" || !box.id) return null;
2042
+ const startedAt = box.startedAt instanceof Date ? box.startedAt.getTime() : Number.NaN;
2043
+ if (!Number.isFinite(startedAt)) return null;
2044
+ return `${box.id}|${startedAt}|${normalizedEgressPolicy(desired)}`;
2045
+ }
2046
+ function hasVerifiedEgress(box, desired, now = Date.now()) {
2047
+ const key = verifiedEgressKey(box, desired);
2048
+ if (!key) return false;
2049
+ const verifiedAt = verifiedEgress.get(key);
2050
+ if (verifiedAt !== void 0 && now - verifiedAt < VERIFIED_EGRESS_TTL_MS) return true;
2051
+ verifiedEgress.delete(key);
2052
+ return false;
2053
+ }
2054
+ function recordVerifiedEgress(box, desired) {
2055
+ const key = verifiedEgressKey(box, desired);
2056
+ if (!key) return;
2057
+ verifiedEgress.delete(key);
2058
+ verifiedEgress.set(key, Date.now());
2059
+ while (verifiedEgress.size > VERIFIED_EGRESS_MAX) {
2060
+ const oldest = verifiedEgress.keys().next().value;
2061
+ if (oldest === void 0) break;
2062
+ verifiedEgress.delete(oldest);
2063
+ }
2064
+ }
2065
+ async function finalizeExistingBox(shell, client, box, stage, name, workspaceId, userId, harness, scope, runtimeEnv, prefetch = {}) {
2066
+ if (!prefetch.egressVerified) {
2067
+ await assertExistingBoxEgress(box, shell.egressPolicy, shell.migrateEgressPolicy, stage, name, prefetch.egress);
2068
+ }
1991
2069
  const written = await materializeDeferredFilesForExistingBox(
1992
2070
  shell,
1993
2071
  client,
@@ -1996,13 +2074,17 @@ async function finalizeExistingBox(shell, client, box, stage, name, workspaceId,
1996
2074
  name,
1997
2075
  workspaceId,
1998
2076
  userId,
1999
- harness
2077
+ harness,
2078
+ prefetch.corpusMarker
2000
2079
  );
2001
2080
  if (!written.succeeded) {
2002
2081
  throw deferredProfileWriteFailed(stage, name, written.error);
2003
2082
  }
2004
2083
  const finalBox = written.value;
2005
- if (Object.keys(runtimeEnv).length > 0) {
2084
+ if (prefetch.runtimeEnv) {
2085
+ const renewed = await prefetch.runtimeEnv;
2086
+ if (!renewed.succeeded) throw renewed.error;
2087
+ } else if (Object.keys(runtimeEnv).length > 0) {
2006
2088
  await finalBox.setRuntimeEnv(runtimeEnv);
2007
2089
  }
2008
2090
  if (shell.bootstrap) {
@@ -2048,23 +2130,35 @@ function existingBoxEgressError(operation, box, stage, name, cause) {
2048
2130
  }
2049
2131
  );
2050
2132
  }
2051
- async function assertExistingBoxEgress(box, desired, migrate, stage, name) {
2133
+ async function assertExistingBoxEgress(box, desired, migrate, stage, name, prefetched) {
2052
2134
  if (!desired) return;
2053
2135
  let current;
2054
- try {
2055
- current = await box.egress.get();
2056
- } catch (cause) {
2057
- throw existingBoxEgressError("read", box, stage, name, cause);
2136
+ if (prefetched) {
2137
+ const read = await prefetched;
2138
+ if (!read.succeeded) throw existingBoxEgressError("read", box, stage, name, read.error);
2139
+ current = read.value;
2140
+ } else {
2141
+ try {
2142
+ current = await box.egress.get();
2143
+ } catch (cause) {
2144
+ throw existingBoxEgressError("read", box, stage, name, cause);
2145
+ }
2058
2146
  }
2059
2147
  const matchingPolicy = normalizedEgressPolicy(current.policy) === normalizedEgressPolicy(desired);
2060
2148
  const explicitSource = current.source !== "platform";
2061
- if (matchingPolicy && explicitSource) return;
2149
+ if (matchingPolicy && explicitSource) {
2150
+ recordVerifiedEgress(box, desired);
2151
+ return;
2152
+ }
2062
2153
  if (migrate) {
2063
2154
  try {
2064
2155
  await box.egress.update(desired);
2065
2156
  const migrated = await box.egress.get();
2066
2157
  const migratedPolicy = normalizedEgressPolicy(migrated.policy) === normalizedEgressPolicy(desired);
2067
- if (migratedPolicy && migrated.source !== "platform") return;
2158
+ if (migratedPolicy && migrated.source !== "platform") {
2159
+ recordVerifiedEgress(box, desired);
2160
+ return;
2161
+ }
2068
2162
  current = migrated;
2069
2163
  } catch (cause) {
2070
2164
  throw existingBoxEgressError("migration", box, stage, name, cause);
@@ -2171,8 +2265,10 @@ async function provisionWorkspaceSandbox(shell, options) {
2171
2265
  throw new Error(`forceNew: sandbox ${name} could not be deleted`, { cause: dropped.error });
2172
2266
  }
2173
2267
  } else if (found.metadata?.harness === harness) {
2268
+ const runtimeEnvMint = measure("runtime_env", () => resolveWorkspaceRuntimeEnv(shell, scope));
2269
+ const prefetch = prefetchExistingBoxReads(shell, found, runtimeEnvMint, measure);
2174
2270
  const [envResult, readyResult] = await Promise.allSettled([
2175
- measure("runtime_env", () => resolveWorkspaceRuntimeEnv(shell, scope)),
2271
+ runtimeEnvMint,
2176
2272
  measure("refresh", () => refreshRuntimeConnection(client, found))
2177
2273
  ]);
2178
2274
  if (envResult.status === "rejected") throw envResult.reason;
@@ -2181,7 +2277,7 @@ async function provisionWorkspaceSandbox(shell, options) {
2181
2277
  if (readyResult.status === "rejected") throw readyResult.reason;
2182
2278
  const ready = readyResult.value;
2183
2279
  if ((await measure("liveness", () => isReusableBox(ready, shell.livenessProbe))).succeeded) {
2184
- return await measure("finalize", () => finalizeExistingBox(shell, client, ready, "reused", name, workspaceId, userId, harness, scope, runtimeEnv2));
2280
+ return await measure("finalize", () => finalizeExistingBox(shell, client, ready, "reused", name, workspaceId, userId, harness, scope, runtimeEnv2, prefetch));
2185
2281
  }
2186
2282
  const recovered = await recoverUnresponsiveBox(
2187
2283
  client,
@@ -2292,7 +2388,7 @@ async function provisionWorkspaceSandbox(shell, options) {
2292
2388
  }
2293
2389
  }
2294
2390
  const storage = shell.storage?.(buildCtx);
2295
- const restore = recoveryRestore === void 0 ? shell.restore?.(buildCtx) : recoveryRestore;
2391
+ const restore = recoveryRestore === void 0 ? await shell.restore?.(buildCtx) : recoveryRestore;
2296
2392
  const payload = {
2297
2393
  name,
2298
2394
  image: resources.image,
@@ -3035,4 +3131,4 @@ export {
3035
3131
  isTerminalPromptEvent,
3036
3132
  detectInteractiveQuestion
3037
3133
  };
3038
- //# sourceMappingURL=chunk-P2EKAQUB.js.map
3134
+ //# sourceMappingURL=chunk-BTBDJNHK.js.map