@tangle-network/agent-app 0.62.0 → 0.62.2

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.
@@ -41,9 +41,9 @@ import {
41
41
  createSandboxRunTool,
42
42
  escalationHints,
43
43
  flattenHistory
44
- } from "../chunk-TCJ4TMOW.js";
44
+ } from "../chunk-ISRPOPMM.js";
45
45
  import "../chunk-F4TUZYMX.js";
46
- import "../chunk-4LBCTFBP.js";
46
+ import "../chunk-PK65B6HP.js";
47
47
  import {
48
48
  ModelFailoverExhaustedError,
49
49
  buildModelChain,
@@ -6,9 +6,11 @@ import {
6
6
  isSandboxApiSandboxMissingFailure,
7
7
  isSandboxBoxConfigFailure,
8
8
  isSandboxHostCapacityFailure,
9
+ plannedHostRestart,
10
+ readDuringPlannedHostRestart,
9
11
  sandboxTransientBackoffMs,
10
12
  serializeSandboxProvisioningError
11
- } from "./chunk-4LBCTFBP.js";
13
+ } from "./chunk-PK65B6HP.js";
12
14
  import {
13
15
  assertProfilePromptWithinBudget,
14
16
  fingerprintAgentProfile
@@ -2278,9 +2280,14 @@ function hasRecentEgress(box, desired) {
2278
2280
  const verified = checksFor(box)?.egress;
2279
2281
  return isRecent(verified?.at, TRUSTED_REUSE_TTL_MS) && verified?.key === normalizedEgressPolicy(desired);
2280
2282
  }
2281
- async function pushRuntimeEnv(box, env, measure) {
2283
+ async function pushRuntimeEnv(box, env, measure, deadline) {
2282
2284
  try {
2283
- await (measure ? measure("runtime_env_push", () => box.setRuntimeEnv(env)) : box.setRuntimeEnv(env));
2285
+ const push = () => box.setRuntimeEnv(env);
2286
+ const deliver = async () => {
2287
+ if (deadline === void 0) await push();
2288
+ else await readDuringPlannedHostRestart(push, deadline);
2289
+ };
2290
+ await (measure ? measure("runtime_env_push", deliver) : deliver());
2284
2291
  recordCheck(box, { env: { digest: runtimeEnvDigest(env), at: Date.now() } });
2285
2292
  return ok(void 0);
2286
2293
  } catch (cause) {
@@ -2322,8 +2329,9 @@ function recordVerifiedEgress(box, desired) {
2322
2329
  recordCheck(box, { egress: { key: normalizedEgressPolicy(desired), at: Date.now() } });
2323
2330
  }
2324
2331
  async function finalizeExistingBox(shell, client, box, stage, name, workspaceId, userId, harness, scope, runtimeEnv, prefetch = {}) {
2332
+ const deadline = Date.now() + (shell.provisionTimeoutMs ?? DEFAULT_PROVISION_TIMEOUT_MS);
2325
2333
  if (!prefetch.egressVerified) {
2326
- await assertExistingBoxEgress(box, shell.egressPolicy, shell.migrateEgressPolicy, stage, name, prefetch.egress);
2334
+ await assertExistingBoxEgress(box, shell.egressPolicy, shell.migrateEgressPolicy, stage, name, deadline, prefetch.egress);
2327
2335
  }
2328
2336
  const written = await materializeDeferredFilesForExistingBox(
2329
2337
  shell,
@@ -2341,10 +2349,13 @@ async function finalizeExistingBox(shell, client, box, stage, name, workspaceId,
2341
2349
  }
2342
2350
  const finalBox = written.value;
2343
2351
  if (prefetch.runtimeEnv) {
2344
- const renewed = await prefetch.runtimeEnv;
2352
+ let renewed = await prefetch.runtimeEnv;
2353
+ if (!renewed.succeeded && plannedHostRestart(renewed.error)) {
2354
+ renewed = await pushRuntimeEnv(finalBox, runtimeEnv, void 0, deadline);
2355
+ }
2345
2356
  if (!renewed.succeeded) throw renewed.error;
2346
2357
  } else if (!prefetch.envDelivered && Object.keys(runtimeEnv).length > 0) {
2347
- const renewed = await pushRuntimeEnv(finalBox, runtimeEnv);
2358
+ const renewed = await pushRuntimeEnv(finalBox, runtimeEnv, void 0, deadline);
2348
2359
  if (!renewed.succeeded) throw renewed.error;
2349
2360
  }
2350
2361
  if (shell.bootstrap) {
@@ -2390,16 +2401,18 @@ function existingBoxEgressError(operation, box, stage, name, cause) {
2390
2401
  }
2391
2402
  );
2392
2403
  }
2393
- async function assertExistingBoxEgress(box, desired, migrate, stage, name, prefetched) {
2404
+ async function assertExistingBoxEgress(box, desired, migrate, stage, name, deadline, prefetched) {
2394
2405
  if (!desired) return;
2395
2406
  let current;
2396
- if (prefetched) {
2397
- const read = await prefetched;
2398
- if (!read.succeeded) throw existingBoxEgressError("read", box, stage, name, read.error);
2407
+ const read = prefetched ? await prefetched : void 0;
2408
+ if (read?.succeeded) {
2399
2409
  current = read.value;
2400
2410
  } else {
2411
+ if (read && !read.succeeded && !plannedHostRestart(read.error)) {
2412
+ throw existingBoxEgressError("read", box, stage, name, read.error);
2413
+ }
2401
2414
  try {
2402
- current = await box.egress.get();
2415
+ current = (await readDuringPlannedHostRestart(() => box.egress.get(), deadline)).value;
2403
2416
  } catch (cause) {
2404
2417
  throw existingBoxEgressError("read", box, stage, name, cause);
2405
2418
  }
@@ -2434,31 +2447,6 @@ async function assertExistingBoxEgress(box, desired, migrate, stage, name, prefe
2434
2447
  }
2435
2448
 
2436
2449
  // src/sandbox/box-health.ts
2437
- function plannedHostRestart(error) {
2438
- const seen = /* @__PURE__ */ new Set();
2439
- for (let depth = 0; error && typeof error === "object" && depth < 8 && !seen.has(error); depth++) {
2440
- seen.add(error);
2441
- const cause = error;
2442
- if (cause.code === "HOST_AGENT_RESTARTING" && cause.status === 503) return true;
2443
- error = cause.cause;
2444
- }
2445
- return false;
2446
- }
2447
- async function readDuringPlannedHostRestart(read, deadline) {
2448
- let waited = false;
2449
- for (; ; ) {
2450
- try {
2451
- return { value: await read(), waited };
2452
- } catch (error) {
2453
- if (!plannedHostRestart(error)) throw error;
2454
- const hint = error.retryAfterMs;
2455
- const delay = typeof hint === "number" && Number.isFinite(hint) && hint > 0 ? hint : sandboxTransientBackoffMs(1);
2456
- if (Date.now() + delay >= deadline) throw error;
2457
- waited = true;
2458
- await new Promise((resolve) => setTimeout(resolve, delay));
2459
- }
2460
- }
2461
- }
2462
2450
  async function isBoxAlive(box, probe, deadline) {
2463
2451
  if (!probe) return ok(void 0);
2464
2452
  try {
@@ -3698,4 +3686,4 @@ export {
3698
3686
  deleteSecret,
3699
3687
  mintSandboxScopedToken
3700
3688
  };
3701
- //# sourceMappingURL=chunk-TCJ4TMOW.js.map
3689
+ //# sourceMappingURL=chunk-ISRPOPMM.js.map