@tangle-network/agent-app 0.63.12 → 0.63.14

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.
@@ -79,7 +79,7 @@ import {
79
79
  turnCarriesFiles,
80
80
  turnPromptText,
81
81
  withProducerStream
82
- } from "../chunk-HCCM6SHN.js";
82
+ } from "../chunk-IRD2IVX2.js";
83
83
  import "../chunk-KH4QSBNI.js";
84
84
  import {
85
85
  sniffMimeFromName
@@ -91,7 +91,7 @@ import {
91
91
  redactErrorMessage
92
92
  } from "../chunk-MH74DY2I.js";
93
93
  import "../chunk-MM6RLOXF.js";
94
- import "../chunk-U2OZPHGW.js";
94
+ import "../chunk-562GGG3L.js";
95
95
  import "../chunk-F4TUZYMX.js";
96
96
  import {
97
97
  assistantRowIdForTurn,
@@ -1105,6 +1105,51 @@ function createWorkspacePrewarmRoute(options) {
1105
1105
  };
1106
1106
  }
1107
1107
 
1108
+ // src/sandbox/request-cancellation.ts
1109
+ function awaitSandboxRead(operation, signal) {
1110
+ signal?.throwIfAborted();
1111
+ if (!signal) return operation();
1112
+ return new Promise((resolve, reject) => {
1113
+ const aborted = () => {
1114
+ signal.removeEventListener("abort", aborted);
1115
+ reject(signal.reason);
1116
+ };
1117
+ signal.addEventListener("abort", aborted, { once: true });
1118
+ Promise.resolve().then(() => {
1119
+ signal.throwIfAborted();
1120
+ return operation(signal);
1121
+ }).then(
1122
+ (value) => {
1123
+ signal.removeEventListener("abort", aborted);
1124
+ resolve(value);
1125
+ },
1126
+ (error) => {
1127
+ signal.removeEventListener("abort", aborted);
1128
+ reject(error);
1129
+ }
1130
+ );
1131
+ });
1132
+ }
1133
+ function waitForSandboxPoll(ms, signal, wait) {
1134
+ signal?.throwIfAborted();
1135
+ return new Promise((resolve, reject) => {
1136
+ let timer;
1137
+ const finish = (error) => {
1138
+ clearTimeout(timer);
1139
+ signal?.removeEventListener("abort", aborted);
1140
+ if (error !== void 0) reject(error);
1141
+ else resolve();
1142
+ };
1143
+ const aborted = () => finish(signal.reason);
1144
+ signal?.addEventListener("abort", aborted, { once: true });
1145
+ if (wait) Promise.resolve().then(() => {
1146
+ signal?.throwIfAborted();
1147
+ return wait(ms);
1148
+ }).then(() => finish(), finish);
1149
+ else timer = setTimeout(() => finish(), ms);
1150
+ });
1151
+ }
1152
+
1108
1153
  // src/sandbox/foreground-single-flight.ts
1109
1154
  var DEFAULT_FOREGROUND_PROVISION_CLAIM_TTL_SECONDS = 180;
1110
1155
  var DEFAULT_FOREGROUND_PROVISION_POLL_INTERVAL_MS = 5e3;
@@ -1168,6 +1213,7 @@ function safeErrorText(error) {
1168
1213
  }
1169
1214
  }
1170
1215
  async function runForegroundSandboxSingleFlight(options) {
1216
+ options.signal?.throwIfAborted();
1171
1217
  const key = sandboxPrewarmClaimKey(options);
1172
1218
  const ttlSeconds = positiveNumber(
1173
1219
  options.claimTtlSeconds ?? DEFAULT_FOREGROUND_PROVISION_CLAIM_TTL_SECONDS,
@@ -1177,7 +1223,7 @@ async function runForegroundSandboxSingleFlight(options) {
1177
1223
  options.pollIntervalMs ?? DEFAULT_FOREGROUND_PROVISION_POLL_INTERVAL_MS,
1178
1224
  "pollIntervalMs"
1179
1225
  );
1180
- const wait = options.wait ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
1226
+ const wait = (ms) => waitForSandboxPoll(ms, options.signal, options.wait);
1181
1227
  const emit = (event) => {
1182
1228
  try {
1183
1229
  options.onEvent?.(event);
@@ -1187,10 +1233,15 @@ async function runForegroundSandboxSingleFlight(options) {
1187
1233
  const timed = (name, work) => options.trace ? options.trace.measure(name, "sandbox", () => work(), void 0, options.traceParent) : work();
1188
1234
  const provisionAsOwner = async (lease) => {
1189
1235
  try {
1190
- return await options.provision();
1236
+ options.signal?.throwIfAborted();
1237
+ const result = await options.provision();
1238
+ options.signal?.throwIfAborted();
1239
+ return result;
1191
1240
  } finally {
1192
1241
  try {
1193
- await timed("sandbox.lease.release", () => options.claim.releaseLease(lease));
1242
+ if (!options.signal?.aborted) {
1243
+ await timed("sandbox.lease.release", () => options.claim.releaseLease(lease));
1244
+ }
1194
1245
  } catch (error) {
1195
1246
  emit({
1196
1247
  type: "release-failed",
@@ -1202,10 +1253,11 @@ async function runForegroundSandboxSingleFlight(options) {
1202
1253
  }
1203
1254
  };
1204
1255
  if (options.reuse) {
1205
- const [state, reused] = await Promise.allSettled([
1256
+ const [state, reused] = await awaitSandboxRead(() => Promise.allSettled([
1206
1257
  timed("sandbox.lease.inspect", () => options.claim.inspect(key)),
1207
- options.reuse()
1208
- ]);
1258
+ options.reuse(options.signal)
1259
+ ]), options.signal);
1260
+ options.signal?.throwIfAborted();
1209
1261
  if (reused.status === "rejected") throw reused.reason;
1210
1262
  if (reused.value !== void 0 && state.status === "fulfilled" && state.value.status !== "held") {
1211
1263
  return reused.value;
@@ -1213,13 +1265,15 @@ async function runForegroundSandboxSingleFlight(options) {
1213
1265
  }
1214
1266
  const initialLease = await timed("sandbox.lease.acquire", () => options.claim.acquireLease(key, ttlSeconds));
1215
1267
  if (initialLease) return provisionAsOwner(initialLease);
1268
+ options.signal?.throwIfAborted();
1216
1269
  emit({ type: "waiting", key, workspaceId: options.workspaceId });
1217
1270
  while (true) {
1218
1271
  await wait(pollIntervalMs);
1219
- const [peek, claim] = await Promise.all([
1220
- options.peek(),
1272
+ const [peek, claim] = await awaitSandboxRead(() => Promise.all([
1273
+ options.peek(options.signal),
1221
1274
  options.claim.inspect(key)
1222
- ]);
1275
+ ]), options.signal);
1276
+ options.signal?.throwIfAborted();
1223
1277
  if (claim.status === "held") continue;
1224
1278
  if (peek.status === "warming") {
1225
1279
  throw new SandboxFilesystemNotReadyError(options.workspaceId, "transitioning");
@@ -1811,7 +1865,8 @@ function dropTrustedBox(box) {
1811
1865
  forgetBox(box.id);
1812
1866
  return trustedBoxes.delete(box);
1813
1867
  }
1814
- function getClientFromCreds(creds) {
1868
+ function getClientFromCreds(creds, signal) {
1869
+ if (signal) return new Sandbox({ apiKey: creds.apiKey, baseUrl: creds.baseUrl, signal });
1815
1870
  const fingerprint = `${creds.apiKey} ${creds.baseUrl}`;
1816
1871
  if (_cached && _cached.fingerprint === fingerprint) return _cached.client;
1817
1872
  const client = new Sandbox({ apiKey: creds.apiKey, baseUrl: creds.baseUrl });
@@ -2086,6 +2141,7 @@ function sandboxEdgeFailed(box) {
2086
2141
  return connection?.edgeStatus === "failed" || Boolean(connection?.edgeError);
2087
2142
  }
2088
2143
  async function refreshRuntimeConnection(client, box) {
2144
+ client.signal?.throwIfAborted();
2089
2145
  let current = box;
2090
2146
  if (sandboxRuntimeUrl(current)) return current;
2091
2147
  const deadline = Date.now() + RUNTIME_CONNECTION_WAIT_MS;
@@ -2097,17 +2153,20 @@ async function refreshRuntimeConnection(client, box) {
2097
2153
  if (latest) current = latest;
2098
2154
  if (sandboxRuntimeUrl(current)) return current;
2099
2155
  } catch {
2156
+ client.signal?.throwIfAborted();
2100
2157
  }
2101
- await new Promise((resolve) => setTimeout(resolve, RUNTIME_CONNECTION_POLL_MS));
2158
+ await waitForSandboxPoll(RUNTIME_CONNECTION_POLL_MS, client.signal);
2102
2159
  }
2103
2160
  return current;
2104
2161
  }
2105
2162
  async function bestEffortRefreshRuntimeExecAuth(client, box, stage, name) {
2163
+ client.signal?.throwIfAborted();
2106
2164
  let current = box;
2107
2165
  try {
2108
2166
  await current.refresh();
2109
2167
  if (hasFreshRuntimeExecAuth(current)) return ok(current);
2110
2168
  } catch (err) {
2169
+ client.signal?.throwIfAborted();
2111
2170
  if (isRuntimeAuthRefreshDenied(err)) {
2112
2171
  return fail(
2113
2172
  new SandboxRuntimeAuthRefreshError(
@@ -2124,6 +2183,7 @@ async function bestEffortRefreshRuntimeExecAuth(client, box, stage, name) {
2124
2183
  if (latest) current = latest;
2125
2184
  if (hasFreshRuntimeExecAuth(current)) return ok(current);
2126
2185
  } catch (err) {
2186
+ client.signal?.throwIfAborted();
2127
2187
  if (isRuntimeAuthRefreshDenied(err)) {
2128
2188
  return fail(
2129
2189
  new SandboxRuntimeAuthRefreshError(
@@ -2138,6 +2198,7 @@ async function bestEffortRefreshRuntimeExecAuth(client, box, stage, name) {
2138
2198
  return ok(current);
2139
2199
  }
2140
2200
  async function refreshRuntimeExecAuth(client, box, stage, name) {
2201
+ client.signal?.throwIfAborted();
2141
2202
  let current = box;
2142
2203
  let lastError;
2143
2204
  const deadline = Date.now() + RUNTIME_CONNECTION_WAIT_MS;
@@ -2149,9 +2210,10 @@ async function refreshRuntimeExecAuth(client, box, stage, name) {
2149
2210
  if (latest) current = latest;
2150
2211
  if (hasFreshRuntimeExecAuth(current)) return ok(current);
2151
2212
  } catch (err) {
2213
+ client.signal?.throwIfAborted();
2152
2214
  lastError = err;
2153
2215
  }
2154
- await new Promise((resolve) => setTimeout(resolve, RUNTIME_CONNECTION_POLL_MS));
2216
+ await waitForSandboxPoll(RUNTIME_CONNECTION_POLL_MS, client.signal);
2155
2217
  }
2156
2218
  const detail = sandboxRuntimeUrl(current) ? "runtime exec credentials are missing or expired after refresh" : "runtime connection is missing after refresh";
2157
2219
  return fail(new SandboxRuntimeAuthRefreshError(stage, name, detail, lastError));
@@ -2319,18 +2381,20 @@ async function deleteBox(box) {
2319
2381
  return fail(err);
2320
2382
  }
2321
2383
  }
2322
- async function resolveWorkspaceSandboxClient(shell, workspaceId, userId) {
2384
+ async function resolveWorkspaceSandboxClient(shell, workspaceId, userId, signal) {
2323
2385
  const scope = { workspaceId, ...userId ? { userId } : {} };
2324
2386
  const creds = await shell.credentials(scope);
2387
+ signal?.throwIfAborted();
2325
2388
  if (!creds) throw new Error("sandbox credentials are required (apiKey/baseUrl)");
2326
2389
  return {
2327
2390
  scope,
2328
- client: getClientFromCreds(creds),
2391
+ client: getClientFromCreds(creds, signal),
2329
2392
  name: shell.boxKey ? shell.boxKey(scope) : shell.name(workspaceId)
2330
2393
  };
2331
2394
  }
2332
2395
  async function peekWorkspaceSandbox(shell, options) {
2333
- const { client, name } = await resolveWorkspaceSandboxClient(shell, options.workspaceId, options.userId);
2396
+ options.signal?.throwIfAborted();
2397
+ const { client, name } = await resolveWorkspaceSandboxClient(shell, options.workspaceId, options.userId, options.signal);
2334
2398
  const displayName = shell.name(options.workspaceId);
2335
2399
  const boxes = await client.list();
2336
2400
  const match = boxes.find((box) => box.name === name) ?? boxes.find((box) => box.name === displayName);
@@ -2710,9 +2774,14 @@ async function provisionWorkspaceSandbox(shell, options, gate, reuseOnly) {
2710
2774
  if (execTimeoutMs !== void 0 && (!Number.isInteger(execTimeoutMs) || execTimeoutMs < 100 || execTimeoutMs > 6e5)) {
2711
2775
  throw new Error("livenessProbe.execTimeoutMs must be an integer between 100 and 600000");
2712
2776
  }
2713
- const { workspaceId, userId, harness, forceNew, onProgress, billingOwnerId } = options;
2714
- const measure = (stage, operation) => options.measure ? options.measure(`sandbox.ensure.${stage}`, operation) : operation();
2715
- const resolved = await measure("credentials", () => resolveWorkspaceSandboxClient(shell, workspaceId, userId));
2777
+ const { workspaceId, userId, harness, forceNew, onProgress, billingOwnerId, signal } = options;
2778
+ const measure = async (stage, operation) => {
2779
+ signal?.throwIfAborted();
2780
+ const result = await (options.measure ? options.measure(`sandbox.ensure.${stage}`, operation) : operation());
2781
+ signal?.throwIfAborted();
2782
+ return result;
2783
+ };
2784
+ const resolved = await measure("credentials", () => resolveWorkspaceSandboxClient(shell, workspaceId, userId, signal));
2716
2785
  const { scope, client } = resolved;
2717
2786
  let name = resolved.name;
2718
2787
  let recoveryRestore;
@@ -2788,6 +2857,7 @@ async function provisionWorkspaceSandbox(shell, options, gate, reuseOnly) {
2788
2857
  );
2789
2858
  return rememberRunningBox(name, await finalizeExistingBox(shell, client, recovered, "reused", name, workspaceId, userId, harness, scope, runtimeEnv));
2790
2859
  } catch (cause) {
2860
+ signal?.throwIfAborted();
2791
2861
  if (refusal.refused) throw refusal.error;
2792
2862
  if (reuseOnly) return void 0;
2793
2863
  const error = cause instanceof Error ? cause : new Error(String(cause));
@@ -2816,6 +2886,7 @@ async function provisionWorkspaceSandbox(shell, options, gate, reuseOnly) {
2816
2886
  }
2817
2887
  if (reuseOnly) return void 0;
2818
2888
  await gate();
2889
+ signal?.throwIfAborted();
2819
2890
  if (!replacementRequested && !forceNew && shell.resumeStopped !== false) {
2820
2891
  const stopped = await measure("list_stopped", () => listStopped(client, name));
2821
2892
  if (!stopped.succeeded) throw stopped.error;
@@ -2824,6 +2895,7 @@ async function provisionWorkspaceSandbox(shell, options, gate, reuseOnly) {
2824
2895
  const runtimeEnv = await measure("runtime_env", () => resolveWorkspaceRuntimeEnv(shell, scope));
2825
2896
  const resumed = await measure("resume", () => resumeStoppedBox(stoppedBox, resumeTimeout, runtimeEnv, onProgress));
2826
2897
  if (!resumed.succeeded) {
2898
+ signal?.throwIfAborted();
2827
2899
  const recovery = await requestSandboxReplacement(
2828
2900
  shell.recoverStoppedSandbox,
2829
2901
  { box: stopped.value, error: resumed.error, scope, boxKey: name },
@@ -2849,6 +2921,7 @@ async function provisionWorkspaceSandbox(shell, options, gate, reuseOnly) {
2849
2921
  );
2850
2922
  return rememberRunningBox(name, await finalizeExistingBox(shell, client, recovered, "resumed", name, workspaceId, userId, harness, scope, runtimeEnv));
2851
2923
  } catch (cause) {
2924
+ signal?.throwIfAborted();
2852
2925
  const error = cause instanceof Error ? cause : new Error(String(cause));
2853
2926
  const recovery = await requestMissingSandboxReplacement(shell, {
2854
2927
  box: stopped.value,
@@ -2863,10 +2936,12 @@ async function provisionWorkspaceSandbox(shell, options, gate, reuseOnly) {
2863
2936
  }
2864
2937
  }
2865
2938
  }
2866
- const { payload, deferredFiles } = await prepareWorkspacePayload(shell, workspaceId, userId, scope, harness, name, recoveryRestore, resources, billingOwnerId);
2867
- let box = await measure("create", () => client.create(payload));
2939
+ signal?.throwIfAborted();
2940
+ const { payload, deferredFiles } = await prepareWorkspacePayload(shell, workspaceId, userId, scope, harness, name, recoveryRestore, resources, billingOwnerId, signal);
2941
+ let box = await measure("create", () => signal ? client.create(payload, { signal }) : client.create(payload));
2868
2942
  await measure("wait_running", () => box.waitFor("running", {
2869
2943
  timeoutMs: shell.provisionTimeoutMs ?? DEFAULT_PROVISION_TIMEOUT_MS,
2944
+ ...signal ? { signal } : {},
2870
2945
  ...onProgress ? { onProgress } : {}
2871
2946
  }));
2872
2947
  box = await measure("refresh_connection", () => refreshRuntimeConnection(client, box));
@@ -2885,8 +2960,10 @@ async function provisionWorkspaceSandbox(shell, options, gate, reuseOnly) {
2885
2960
  }
2886
2961
  return rememberRunningBox(name, box);
2887
2962
  }
2888
- async function prepareWorkspacePayload(shell, workspaceId, userId, scope, harness, name, recoveryRestore, resources, billingOwnerId) {
2963
+ async function prepareWorkspacePayload(shell, workspaceId, userId, scope, harness, name, recoveryRestore, resources, billingOwnerId, signal) {
2964
+ signal?.throwIfAborted();
2889
2965
  const connectedIntegrationIds = await shell.connectedIntegrationIds(workspaceId);
2966
+ signal?.throwIfAborted();
2890
2967
  const buildCtx = {
2891
2968
  workspaceId,
2892
2969
  connectedIntegrationIds,
@@ -2898,6 +2975,7 @@ async function prepareWorkspacePayload(shell, workspaceId, userId, scope, harnes
2898
2975
  resolveWorkspaceRuntimeEnv(shell, scope),
2899
2976
  shell.files(buildCtx)
2900
2977
  ]);
2978
+ signal?.throwIfAborted();
2901
2979
  const env = { ...creationEnv, ...runtimeEnv };
2902
2980
  const fullProfile = shell.profile({ extraFiles: files, harness });
2903
2981
  const { leanProfile, deferredFiles } = shell.deferProfileFiles ? splitDeferredProfileFiles(fullProfile) : { leanProfile: fullProfile, deferredFiles: [] };
@@ -2906,6 +2984,7 @@ async function prepareWorkspacePayload(shell, workspaceId, userId, scope, harnes
2906
2984
  let model = shell.backendModelAtCreate ? requireTransportableModel(resolveModelSelection(shell.provider), `backendModelAtCreate for ${name}`) : void 0;
2907
2985
  if (model && shell.childKeyMint && model.provider === "openai-compat") {
2908
2986
  const minted = await shell.childKeyMint(scope);
2987
+ signal?.throwIfAborted();
2909
2988
  if (minted.succeeded) model = { ...model, apiKey: minted.value };
2910
2989
  else {
2911
2990
  console.error(
@@ -2916,6 +2995,7 @@ async function prepareWorkspacePayload(shell, workspaceId, userId, scope, harnes
2916
2995
  }
2917
2996
  const storage = shell.storage?.(buildCtx);
2918
2997
  const restore = recoveryRestore === void 0 ? await shell.restore?.(buildCtx) : recoveryRestore;
2998
+ signal?.throwIfAborted();
2919
2999
  const payload = {
2920
3000
  name,
2921
3001
  image: resources.image,
@@ -2965,8 +3045,10 @@ async function ensureWorkspaceSandbox(shell, options) {
2965
3045
  return box;
2966
3046
  }
2967
3047
  async function reuseWorkspaceSandbox(shell, options) {
3048
+ options.signal?.throwIfAborted();
2968
3049
  if (options.forceNew) return void 0;
2969
3050
  await hydrateReuseState(shell, options);
3051
+ options.signal?.throwIfAborted();
2970
3052
  if (!hasRecentProbe(shell, options)) return void 0;
2971
3053
  return claimWorkspaceSandbox(shell, options, true);
2972
3054
  }
@@ -2983,9 +3065,11 @@ function spendGate(options) {
2983
3065
  };
2984
3066
  }
2985
3067
  async function claimWorkspaceSandbox(shell, options, reuseOnly) {
3068
+ options.signal?.throwIfAborted();
2986
3069
  const { trace } = options;
2987
3070
  const gate = spendGate(options);
2988
3071
  await hydrateReuseState(shell, options);
3072
+ options.signal?.throwIfAborted();
2989
3073
  if (!hasRecentProbe(shell, options)) await gate();
2990
3074
  const claim = trace?.start("sandbox.claim", "sandbox", { "workspace.harness": options.harness }, options.traceParent);
2991
3075
  const traced = trace && claim ? {
@@ -2997,6 +3081,7 @@ async function claimWorkspaceSandbox(shell, options, reuseOnly) {
2997
3081
  } : options;
2998
3082
  try {
2999
3083
  const box = await bringUpWorkspaceSandbox(shell, traced, gate, reuseOnly);
3084
+ options.signal?.throwIfAborted();
3000
3085
  if (!box) {
3001
3086
  claim?.end({ "sandbox.reuse_declined": true });
3002
3087
  return void 0;
@@ -3034,6 +3119,7 @@ async function bringUpWorkspaceSandbox(shell, options, gate, reuseOnly) {
3034
3119
  try {
3035
3120
  return await provisionWorkspaceSandbox(shell, options, gate, reuseOnly);
3036
3121
  } catch (err) {
3122
+ options.signal?.throwIfAborted();
3037
3123
  if (!shell.replaceUnbringableBox) throw err;
3038
3124
  if (options.forceNew) throw err;
3039
3125
  if (!isUnbringableBoxError(err)) throw err;
@@ -3794,4 +3880,4 @@ export {
3794
3880
  sandboxPromptRunner,
3795
3881
  createSandboxRunTool
3796
3882
  };
3797
- //# sourceMappingURL=chunk-U2OZPHGW.js.map
3883
+ //# sourceMappingURL=chunk-562GGG3L.js.map