@tangle-network/agent-app 0.60.38 → 0.60.40

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.
@@ -0,0 +1,560 @@
1
+ // src/sandbox/recovery.ts
2
+ var EGRESS_PROXY_RECOVERY_REQUIRED = "EGRESS_PROXY_RECOVERY_REQUIRED";
3
+ var EGRESS_PROXY_RECOVERY_PHASE = "egress_proxy_recovery";
4
+ var WORKSPACE_SANDBOX_MISSING = "WORKSPACE_SANDBOX_MISSING";
5
+ var WORKSPACE_SANDBOX_HOST_EXHAUSTED = "WORKSPACE_SANDBOX_HOST_EXHAUSTED";
6
+ var WORKSPACE_SANDBOX_UNRECOVERABLE = "WORKSPACE_SANDBOX_UNRECOVERABLE";
7
+ var WORKSPACE_SANDBOX_SNAPSHOT_MAX_AGE_MS = 24 * 60 * 60 * 1e3;
8
+ var CODES = {
9
+ [EGRESS_PROXY_RECOVERY_REQUIRED]: { snapshotUsable: true },
10
+ [WORKSPACE_SANDBOX_MISSING]: { snapshotUsable: false },
11
+ [WORKSPACE_SANDBOX_HOST_EXHAUSTED]: { snapshotUsable: false },
12
+ [WORKSPACE_SANDBOX_UNRECOVERABLE]: { snapshotUsable: false }
13
+ };
14
+ var ACTIONS = {
15
+ confirmation_required: { replacementChosen: false, canDecide: true },
16
+ deletion_declined: { replacementChosen: false, canDecide: true },
17
+ replacement_authorized: { replacementChosen: false },
18
+ snapshot_replacement_authorized: { replacementChosen: false },
19
+ replacement_started: { replacementChosen: true, completedAs: "replacement_completed" },
20
+ replacement_completed: { replacementChosen: true },
21
+ snapshot_replacement_started: { replacementChosen: true, completedAs: "snapshot_replacement_completed" },
22
+ snapshot_restore_failed: { replacementChosen: true, completedAs: "snapshot_replacement_completed" },
23
+ snapshot_replacement_completed: { replacementChosen: true },
24
+ missing_replacement_started: { replacementChosen: true, completedAs: "missing_replacement_completed" },
25
+ missing_replacement_completed: { replacementChosen: true },
26
+ unrecoverable_replacement_started: { replacementChosen: true, completedAs: "unrecoverable_replacement_completed" },
27
+ unrecoverable_replacement_completed: { replacementChosen: true }
28
+ };
29
+ var WorkspaceSandboxRecoveryRequiredError = class extends Error {
30
+ code = EGRESS_PROXY_RECOVERY_REQUIRED;
31
+ status = 409;
32
+ phase = EGRESS_PROXY_RECOVERY_PHASE;
33
+ recovery;
34
+ constructor(recovery, cause) {
35
+ super(workspaceSandboxRecoveryMessage(recovery), { cause });
36
+ this.name = "WorkspaceSandboxRecoveryRequiredError";
37
+ this.recovery = recovery;
38
+ }
39
+ };
40
+ function isRecord(value) {
41
+ return typeof value === "object" && value !== null;
42
+ }
43
+ function asNonEmptyString(value) {
44
+ return typeof value === "string" && value.trim().length > 0 ? value : void 0;
45
+ }
46
+ function errorChain(error) {
47
+ const pending = [error];
48
+ const visited = /* @__PURE__ */ new Set();
49
+ const chain = [];
50
+ while (pending.length > 0) {
51
+ const current = pending.shift();
52
+ if (current === void 0 || current === null || visited.has(current)) continue;
53
+ visited.add(current);
54
+ chain.push(current);
55
+ if (!isRecord(current)) continue;
56
+ if (current.cause !== void 0) pending.push(current.cause);
57
+ if (Array.isArray(current.errors)) pending.push(...current.errors);
58
+ }
59
+ return chain;
60
+ }
61
+ function isEgressProxyRecoveryRequiredError(error) {
62
+ return errorChain(error).some(
63
+ (current) => isRecord(current) && current.code === EGRESS_PROXY_RECOVERY_REQUIRED
64
+ );
65
+ }
66
+ function isWorkspaceSandboxSnapshotRestoreError(error) {
67
+ return errorChain(error).some((current) => {
68
+ const message = current instanceof Error ? current.message : isRecord(current) && typeof current.message === "string" ? current.message : "";
69
+ return /snapshot|fromSnapshot|restore/i.test(message);
70
+ });
71
+ }
72
+ function assessWorkspaceSandboxSnapshot(snapshot, sandboxId, now = Date.now()) {
73
+ if (!snapshot) return { availability: "missing", freshness: "unknown" };
74
+ const createdAt = Date.parse(snapshot.createdAt);
75
+ const ageMs = now - createdAt;
76
+ const isFresh = snapshot.fromSandboxId === sandboxId && Number.isFinite(createdAt) && ageMs >= 0 && ageMs <= WORKSPACE_SANDBOX_SNAPSHOT_MAX_AGE_MS;
77
+ return isFresh ? { availability: "available", freshness: "fresh", snapshot } : { availability: "stale", freshness: "stale", snapshot };
78
+ }
79
+ function isSnapshotAssessment(value) {
80
+ if (!isRecord(value)) return false;
81
+ const availability = value.availability;
82
+ const freshness = value.freshness;
83
+ return (availability === "available" || availability === "missing" || availability === "stale") && (freshness === "fresh" || freshness === "stale" || freshness === "unknown");
84
+ }
85
+ function isWorkspaceSandboxRecoveryAction(value) {
86
+ return typeof value === "string" && Object.hasOwn(ACTIONS, value);
87
+ }
88
+ function isWorkspaceSandboxRecoveryCode(value) {
89
+ return typeof value === "string" && Object.hasOwn(CODES, value);
90
+ }
91
+ function isWorkspaceSandboxRecoveryState(value) {
92
+ if (!isRecord(value)) return false;
93
+ return isWorkspaceSandboxRecoveryCode(value.code) && !!asNonEmptyString(value.sandboxId) && !!asNonEmptyString(value.detectedAt) && isSnapshotAssessment(value.snapshot) && isWorkspaceSandboxRecoveryAction(value.action);
94
+ }
95
+ function workspaceSandboxRecoveryFromError(error) {
96
+ for (const current of errorChain(error)) {
97
+ if (!isRecord(current) || !isWorkspaceSandboxRecoveryState(current.recovery)) continue;
98
+ return current.recovery;
99
+ }
100
+ return void 0;
101
+ }
102
+ function workspaceSandboxRecoveryMessage(recovery) {
103
+ if (recovery.code === WORKSPACE_SANDBOX_MISSING) {
104
+ return "The sandbox behind this workspace no longer exists on the platform. A replacement is being provisioned and your saved work is restored into it.";
105
+ }
106
+ if (!CODES[recovery.code].snapshotUsable) {
107
+ return "The sandbox behind this workspace could not be started again. A replacement is being provisioned and your saved work is restored into it.";
108
+ }
109
+ if (recovery.action === "deletion_declined") {
110
+ return "Sandbox recovery remains paused because replacement was declined. The stopped sandbox has not been deleted.";
111
+ }
112
+ if (recovery.action === "confirmation_required") {
113
+ const snapshotReason = recovery.snapshot.availability === "missing" ? "No restorable sandbox snapshot is available." : "The available sandbox snapshot is stale.";
114
+ return `Sandbox recovery requires owner confirmation before replacement. ${snapshotReason} The stopped sandbox has not been deleted.`;
115
+ }
116
+ if (recovery.action === "snapshot_restore_failed") {
117
+ return "Sandbox replacement could not restore its verified snapshot, and an empty fallback was not created because it could lose workspace state.";
118
+ }
119
+ return "Sandbox recovery is required before chat can continue. The stopped sandbox has not been deleted.";
120
+ }
121
+ function workspaceSandboxRecoveryRecommendedActions(recovery) {
122
+ if (!CODES[recovery.code].snapshotUsable) {
123
+ return ["Retry after the replacement sandbox finishes provisioning."];
124
+ }
125
+ if (recovery.action === "deletion_declined") {
126
+ return ["Keep the stopped sandbox for support or ask a workspace owner to authorize replacement."];
127
+ }
128
+ if (recovery.action === "confirmation_required") {
129
+ return ["An owner can explicitly replace the sandbox after accepting loss of unsnapshotted sandbox-local changes."];
130
+ }
131
+ if (recovery.action === "snapshot_restore_failed") {
132
+ return ["Retry snapshot restoration or contact support. An empty replacement is not created automatically."];
133
+ }
134
+ return ["Retry after sandbox recovery completes."];
135
+ }
136
+ function workspaceSandboxRecoveryDiagnostic(recovery) {
137
+ return {
138
+ sandboxId: recovery.sandboxId,
139
+ recoveryCode: recovery.code,
140
+ snapshotAvailability: recovery.snapshot.availability,
141
+ snapshotFreshness: recovery.snapshot.freshness,
142
+ selectedRecoveryAction: recovery.action,
143
+ replacementSandboxId: recovery.replacementSandboxId
144
+ };
145
+ }
146
+ function preferredWorkspaceSandboxRecoveryBoxKey(recovery) {
147
+ if (!recovery?.replacementBoxKey) return void 0;
148
+ return ACTIONS[recovery.action].replacementChosen ? recovery.replacementBoxKey : void 0;
149
+ }
150
+ function shouldRestoreWorkspaceSandboxRecovery(recovery) {
151
+ if (!recovery) return false;
152
+ if (!CODES[recovery.code].snapshotUsable) return false;
153
+ return !!preferredWorkspaceSandboxRecoveryBoxKey(recovery) && recovery.snapshot.availability === "available";
154
+ }
155
+ function createWorkspaceSandboxRecoveryManager(store) {
156
+ async function transition(workspaceId, expected, next) {
157
+ if (store.compareAndSet) {
158
+ if (!await store.compareAndSet(workspaceId, expected, next))
159
+ throw new Error("Sandbox recovery changed; reload before retrying its transition");
160
+ } else {
161
+ await store.write(workspaceId, next);
162
+ }
163
+ return next;
164
+ }
165
+ return {
166
+ read: (workspaceId) => store.read(workspaceId),
167
+ record: (workspaceId, recovery) => store.write(workspaceId, recovery),
168
+ async decide({ workspaceId, sandboxId, decision, replacementBoxKey }) {
169
+ const current = await store.read(workspaceId);
170
+ if (!current || current.sandboxId !== sandboxId || !("canDecide" in ACTIONS[current.action])) return void 0;
171
+ if (decision === "replace" && !asNonEmptyString(replacementBoxKey))
172
+ throw new Error("Sandbox replacement authorization requires a fresh replacement box key");
173
+ return transition(workspaceId, current, {
174
+ ...current,
175
+ action: decision === "replace" ? current.snapshot.availability === "available" ? "snapshot_replacement_authorized" : "replacement_authorized" : "deletion_declined",
176
+ confirmedAt: (/* @__PURE__ */ new Date()).toISOString(),
177
+ ...decision === "replace" ? { replacementBoxKey } : {}
178
+ });
179
+ },
180
+ async complete({ workspaceId, replacementSandboxId, replacementBoxKey }) {
181
+ const current = await store.read(workspaceId);
182
+ if (!current || !asNonEmptyString(current.replacementBoxKey)) return void 0;
183
+ if (replacementBoxKey !== void 0 && current.replacementBoxKey !== replacementBoxKey) return void 0;
184
+ const policy = ACTIONS[current.action];
185
+ if (!("completedAs" in policy)) return void 0;
186
+ if (!asNonEmptyString(replacementSandboxId)) throw new Error("A replacement sandbox id is required");
187
+ return transition(workspaceId, current, { ...current, action: policy.completedAs, replacementSandboxId });
188
+ }
189
+ };
190
+ }
191
+ var WORKSPACE_SANDBOX_RECOVERY_ACTIONS = Object.keys(
192
+ ACTIONS
193
+ );
194
+ var WORKSPACE_SANDBOX_RECOVERY_CODES = Object.keys(
195
+ CODES
196
+ );
197
+
198
+ // src/sandbox/diagnostics.ts
199
+ var SAFE_ERROR_FIELDS = [
200
+ "code",
201
+ "status",
202
+ "phase",
203
+ "endpoint",
204
+ "origin",
205
+ "retryAfterMs",
206
+ "sidecarVersion",
207
+ "containerImage"
208
+ ];
209
+ function isRecord2(value) {
210
+ return typeof value === "object" && value !== null;
211
+ }
212
+ function asSafeScalar(value) {
213
+ if (typeof value === "string" && value.length > 0) return redactDiagnosticText(value);
214
+ if (typeof value === "number" && Number.isFinite(value)) return value;
215
+ return void 0;
216
+ }
217
+ function redactDiagnosticText(value) {
218
+ const secretKeyPattern = "(?:(?:[A-Z0-9]+_)*(?:api_?key|auth_?token|access_?token|refresh_?token|key|token|password|secret|credential)|apiKey|authToken|accessToken|refreshToken)";
219
+ return value.replace(/(['"]authorization['"]\s*:\s*)(['"])[^'"]*\2/gi, "$1$2[REDACTED]$2").replace(/\b(authorization\s*[:=]\s*)(['"])[^'"]*\2/gi, "$1$2[REDACTED]$2").replace(/\b(authorization\s*[:=]\s*)Digest\b[^;}\n]*(?:\r?\n[ \t]+[^;}\n]*)*/gi, "$1[REDACTED]").replace(/\b(authorization\s*[:=]\s*)(?:Basic|Bearer|Negotiate)\s+[^\s;,&}\n]+(?:\r?\n[ \t]+[^\s;,&}\n]+)*/gi, "$1[REDACTED]").replace(new RegExp(`\\b(authorization\\s*[:=]\\s*)(?!(?:Basic|Bearer|Digest|Negotiate)\\b)([A-Za-z][A-Za-z0-9._-]+)\\s+(?!(?:authorization|${secretKeyPattern})\\s*[:=]|['"](?:authorization|${secretKeyPattern})['"]\\s*:)([^}\\n]*(?:\\r?\\n[ \\t]+[^}\\n]*)*)`, "gi"), (_match, prefix, _scheme, valuePart) => {
220
+ if (valuePart.includes(";") || /\r?\n[ \t]+/.test(valuePart)) return `${prefix}[REDACTED]`;
221
+ const tokenMatch = valuePart.match(/^\S+(.*)$/);
222
+ return `${prefix}[REDACTED]${tokenMatch?.[1] ?? ""}`;
223
+ }).replace(/\b(authorization\s*[:=]\s*)[A-Za-z][\w.-]+\s+[^\s;,&}\n]+(?=[;&}\n]|$)/gi, "$1[REDACTED]").replace(/\b(authorization\s*[:=]\s*)(?!\[REDACTED\])[^'"\s&}]+(?:[;,][^'"\s&}]+)*/gi, "$1[REDACTED]").replace(/\bBearer\s+[A-Za-z0-9._~+/-]+=*/gi, "Bearer [REDACTED]").replace(/\bsk-[A-Za-z0-9_-]{6,}\b/g, "sk-[REDACTED]").replace(
224
+ /([?&][^=&#\s]*(?:token|key|secret|password|authorization)[^=&#\s]*=)[^&#\s]+/gi,
225
+ "$1[REDACTED]"
226
+ ).replace(
227
+ new RegExp(`(['"])(${secretKeyPattern})\\1\\s*:\\s*(['"])[^'"]+\\3`, "gi"),
228
+ "$1$2$1:$3[REDACTED]$3"
229
+ ).replace(
230
+ new RegExp(`\\b(${secretKeyPattern})\\s*([:=])(\\s*)(['"]?)(?!Bearer\\b)[^'"\\s;,&}]+(?:[;,]\\s*[^'"\\s;,&}]+)*`, "gi"),
231
+ "$1$2$3$4[REDACTED]"
232
+ );
233
+ }
234
+ function readMessage(value) {
235
+ if (value instanceof Error) return redactDiagnosticText(value.message);
236
+ if (typeof value === "string" && value.length > 0) return redactDiagnosticText(value);
237
+ if (typeof value === "number" && Number.isFinite(value)) return String(value);
238
+ if (typeof value === "boolean") return String(value);
239
+ if (!isRecord2(value)) return void 0;
240
+ const message = value.message;
241
+ return typeof message === "string" && message.length > 0 ? redactDiagnosticText(message) : void 0;
242
+ }
243
+ function readName(value) {
244
+ if (value instanceof Error) return redactDiagnosticText(value.name);
245
+ if (!isRecord2(value)) return void 0;
246
+ const name = value.name;
247
+ return typeof name === "string" && name.length > 0 ? redactDiagnosticText(name) : void 0;
248
+ }
249
+ function readCause(value) {
250
+ if (!isRecord2(value)) return void 0;
251
+ return value.cause;
252
+ }
253
+ function serializeSandboxProvisioningError(error, options = {}) {
254
+ const maxDepth = options.maxDepth ?? 6;
255
+ const causes = [];
256
+ const seen = /* @__PURE__ */ new Set();
257
+ let truncatedAtDepth;
258
+ let cycle = false;
259
+ let current = error;
260
+ let depth = 0;
261
+ for (; depth < maxDepth && current !== void 0 && current !== null; depth += 1) {
262
+ if (seen.has(current)) {
263
+ cycle = true;
264
+ causes.push({
265
+ name: "CauseChainCycle",
266
+ message: `cause chain cycle detected after ${depth} entries`
267
+ });
268
+ break;
269
+ }
270
+ if (isRecord2(current)) seen.add(current);
271
+ const cause = {};
272
+ const name = readName(current);
273
+ const message = readMessage(current);
274
+ if (name) cause.name = name;
275
+ if (message) cause.message = message;
276
+ if (isRecord2(current)) {
277
+ for (const field of SAFE_ERROR_FIELDS) {
278
+ const safeValue = asSafeScalar(current[field]);
279
+ if (safeValue !== void 0) {
280
+ Object.assign(cause, { [field]: safeValue });
281
+ }
282
+ }
283
+ }
284
+ if (Object.keys(cause).length > 0) causes.push(cause);
285
+ current = readCause(current);
286
+ }
287
+ if (!cycle && isRecord2(current) && seen.has(current)) {
288
+ cycle = true;
289
+ causes.push({
290
+ name: "CauseChainCycle",
291
+ message: `cause chain cycle detected after ${depth} entries`
292
+ });
293
+ } else if (current !== void 0 && current !== null && depth >= maxDepth) {
294
+ truncatedAtDepth = maxDepth;
295
+ causes.push({
296
+ name: "CauseChainTruncated",
297
+ message: `cause chain truncated after ${maxDepth} entries`
298
+ });
299
+ }
300
+ return {
301
+ message: readMessage(error) ?? "Sandbox unavailable",
302
+ causes,
303
+ truncated: truncatedAtDepth !== void 0,
304
+ truncatedAtDepth,
305
+ cycle
306
+ };
307
+ }
308
+ function formatSandboxProvisioningSupportDetails(diagnostics) {
309
+ const actionableCause = diagnostics.causes.find(
310
+ (cause) => cause.code !== void 0 || cause.status !== void 0 || cause.phase !== void 0 || cause.endpoint !== void 0 || cause.origin !== void 0 || cause.retryAfterMs !== void 0 || cause.sidecarVersion !== void 0 || cause.containerImage !== void 0
311
+ ) ?? diagnostics.causes[1];
312
+ if (!actionableCause) return "Support details: no nested sandbox cause details were available.";
313
+ const details = [
314
+ typeof actionableCause.name === "string" ? redactDiagnosticText(actionableCause.name) : void 0,
315
+ actionableCause.code !== void 0 ? `code=${actionableCause.code}` : void 0,
316
+ actionableCause.status !== void 0 ? `status=${actionableCause.status}` : void 0,
317
+ actionableCause.phase !== void 0 ? `phase=${redactDiagnosticText(actionableCause.phase)}` : void 0,
318
+ actionableCause.endpoint !== void 0 ? `endpoint=${redactDiagnosticText(actionableCause.endpoint)}` : void 0,
319
+ actionableCause.origin !== void 0 ? `origin=${redactDiagnosticText(actionableCause.origin)}` : void 0,
320
+ actionableCause.retryAfterMs !== void 0 ? `retryAfterMs=${actionableCause.retryAfterMs}` : void 0,
321
+ actionableCause.sidecarVersion !== void 0 ? `sidecarVersion=${redactDiagnosticText(actionableCause.sidecarVersion)}` : void 0,
322
+ actionableCause.containerImage !== void 0 ? `containerImage=${redactDiagnosticText(actionableCause.containerImage)}` : void 0,
323
+ typeof actionableCause.message === "string" ? redactDiagnosticText(actionableCause.message) : void 0
324
+ ].filter(Boolean);
325
+ if (details.length === 0) return "Support details: no nested sandbox cause details were available.";
326
+ return `Support details: ${details.join("; ")}`;
327
+ }
328
+ function isSandboxAuthFailure(diagnostics) {
329
+ return diagnostics.causes.some((cause) => {
330
+ const code = typeof cause.code === "string" ? cause.code.toUpperCase() : void 0;
331
+ const status2 = typeof cause.status === "number" ? cause.status : typeof cause.status === "string" ? Number.parseInt(cause.status, 10) : void 0;
332
+ const name = typeof cause.name === "string" ? cause.name.toLowerCase() : "";
333
+ const message = typeof cause.message === "string" ? cause.message.toLowerCase() : "";
334
+ return code === "AUTH_ERROR" || status2 === 401 || name.includes("autherror") || message.includes("missing or invalid authentication");
335
+ });
336
+ }
337
+ function isSandboxApiBearerAuthFailure(diagnostics) {
338
+ return diagnostics.causes.some((cause) => {
339
+ const status2 = typeof cause.status === "number" ? cause.status : typeof cause.status === "string" ? Number.parseInt(cause.status, 10) : void 0;
340
+ if (status2 !== 401) return false;
341
+ if (cause.origin !== "sandbox-api") return false;
342
+ if (typeof cause.endpoint !== "string") return false;
343
+ const endpointPath = sandboxApiEndpointPath(cause.endpoint);
344
+ if (!endpointPath || !/^\/v1(?:[/?#]|$)/.test(endpointPath)) return false;
345
+ return !/^\/v1\/sandboxes\/[^/?#]+\/runtime(?:[/?#]|$)/.test(endpointPath);
346
+ });
347
+ }
348
+ var SANDBOX_BACKING_CONTAINER_MISSING_CODE = "BACKING_CONTAINER_MISSING";
349
+ function isLegacySandboxBackingContainerMissingMessage(message) {
350
+ return /host-agent startcontainer failed \(404\):/i.test(message) && /container not found/i.test(message) && /"code"\s*:\s*"not_found"/i.test(message);
351
+ }
352
+ function isSandboxApiSandboxMissingFailure(diagnostics) {
353
+ return diagnostics.causes.some((cause) => {
354
+ const status2 = typeof cause.status === "number" ? cause.status : typeof cause.status === "string" ? Number.parseInt(cause.status, 10) : void 0;
355
+ if (cause.origin !== "sandbox-api") return false;
356
+ if (typeof cause.endpoint !== "string") return false;
357
+ const endpointPath = sandboxApiEndpointPath(cause.endpoint);
358
+ if (!endpointPath) return false;
359
+ const sandboxResource = /^\/v1\/sandboxes\/[^/?#]+(?:\/(?!runtime(?:[/?#]|$))[^?#]*)?(?:[?#].*)?$/.test(endpointPath);
360
+ if (!sandboxResource) return false;
361
+ if (status2 === 404) return true;
362
+ if (status2 !== 500 || !/^\/v1\/sandboxes\/[^/?#]+\/resume(?:[?#].*)?$/.test(endpointPath)) {
363
+ return false;
364
+ }
365
+ const code = typeof cause.code === "string" ? cause.code.toUpperCase() : void 0;
366
+ if (code === SANDBOX_BACKING_CONTAINER_MISSING_CODE) return true;
367
+ if (code !== void 0 && code !== "SERVER_ERROR") return false;
368
+ const message = typeof cause.message === "string" ? cause.message : "";
369
+ return isLegacySandboxBackingContainerMissingMessage(message);
370
+ });
371
+ }
372
+ function isSandboxHostCapacityFailure(diagnostics) {
373
+ return diagnostics.causes.some((cause) => {
374
+ if (cause.origin !== "sandbox-api") return false;
375
+ if (typeof cause.message !== "string") return false;
376
+ return /host has no available slot|host capacity reservation failed/i.test(cause.message);
377
+ });
378
+ }
379
+ function isSandboxBoxConfigFailure(diagnostics) {
380
+ return diagnostics.causes.some((cause) => {
381
+ if (cause.origin !== "sandbox-api") return false;
382
+ if (cause.code === "SANDBOX_ATTRIBUTION_MISMATCH") return true;
383
+ if (typeof cause.message !== "string") return false;
384
+ return /has no recorded egress policy|cannot rebuild the proxy config/i.test(cause.message);
385
+ });
386
+ }
387
+ function isSandboxApiTransientFailure(diagnostics) {
388
+ if (isSandboxApiSandboxMissingFailure(diagnostics) || isSandboxBoxConfigFailure(diagnostics) || isSandboxHostCapacityFailure(diagnostics)) return false;
389
+ return diagnostics.causes.some((cause) => {
390
+ const status2 = typeof cause.status === "number" ? cause.status : typeof cause.status === "string" ? Number.parseInt(cause.status, 10) : void 0;
391
+ if (status2 !== 500 && status2 !== 502 && status2 !== 503 && status2 !== 504) return false;
392
+ if (cause.origin !== "sandbox-api" || typeof cause.endpoint !== "string") return false;
393
+ const endpointPath = sandboxApiEndpointPath(cause.endpoint);
394
+ if (!endpointPath || !/^\/v1(?:[/?#]|$)/.test(endpointPath)) return false;
395
+ return !/^\/v1\/sandboxes\/[^/?#]+\/runtime(?:[/?#]|$)/.test(endpointPath);
396
+ });
397
+ }
398
+ function sandboxApiEndpointPath(endpoint) {
399
+ if (endpoint.startsWith("/")) return endpoint;
400
+ try {
401
+ const url = new URL(endpoint);
402
+ return `${url.pathname}${url.search}${url.hash}`;
403
+ } catch {
404
+ return null;
405
+ }
406
+ }
407
+ function formatSandboxProvisioningUserMessage(diagnostics) {
408
+ if (diagnostics.causes.some((cause) => cause.code === EGRESS_PROXY_RECOVERY_REQUIRED)) {
409
+ return "Sandbox recovery is required before chat can continue. The stopped sandbox has not been deleted.";
410
+ }
411
+ if (diagnostics.causes.some((cause) => cause.code === "vault.hydration_incomplete")) {
412
+ return "I couldn't finish copying your Vault into the sandbox, so I stopped rather than work from a partial copy. The copy resumes where it left off \u2014 try again in a moment.";
413
+ }
414
+ if (isSandboxApiTransientFailure(diagnostics)) {
415
+ return "The sandbox service didn't answer just now. Send your message again in a moment.";
416
+ }
417
+ if (isSandboxApiBearerAuthFailure(diagnostics)) {
418
+ return "I'm unable to reconnect to the sandbox because its sandbox API credential was rejected. Another request may have rotated the bearer used for this operation.";
419
+ }
420
+ if (isSandboxAuthFailure(diagnostics)) {
421
+ return "I'm unable to reconnect to the sandbox because its runtime authentication failed. This can happen when an existing sandbox is reused with stale credentials.";
422
+ }
423
+ if (diagnostics.causes.some((cause) => cause.code === "PAYLOAD_TOO_LARGE" || cause.code === "FILE_TOO_LARGE" || cause.status === 413 || cause.status === "413")) {
424
+ if (diagnostics.causes.some((cause) => cause.message?.startsWith("writeProfileFilesToBox"))) {
425
+ return "A workspace file is too large to copy into the sandbox. The support details name the file.";
426
+ }
427
+ return "An attachment is too large for the sandbox to accept. Use a smaller file and try again.";
428
+ }
429
+ if (diagnostics.causes.some((cause) => cause.code === "UPLOAD_BUDGET_EXHAUSTED")) {
430
+ return "Too much attachment data is staged in the sandbox at once. Retry shortly.";
431
+ }
432
+ return "I'm unable to connect to the sandbox right now. Send your message again in a moment; if it keeps failing, share the support details with the team.";
433
+ }
434
+
435
+ // src/sandbox/transient-failure.ts
436
+ var SANDBOX_TRANSIENT_DEADLINE_MS = 15 * 6e4;
437
+ function sandboxTransientBackoffMs(attempt) {
438
+ return Math.min(6e4, 5e3 * 2 ** Math.max(0, attempt - 1));
439
+ }
440
+ function record(value) {
441
+ return value !== null && typeof value === "object" ? value : void 0;
442
+ }
443
+ function causeChain(error) {
444
+ const chain = [];
445
+ let current = record(error);
446
+ for (let depth = 0; current && depth < 8 && !chain.includes(current); depth += 1) {
447
+ chain.push(current);
448
+ current = record(current.cause);
449
+ }
450
+ return chain;
451
+ }
452
+ function status(value) {
453
+ if (typeof value === "number") return value;
454
+ if (typeof value === "string" && /^\d{3}$/.test(value)) return Number(value);
455
+ return void 0;
456
+ }
457
+ var FILESYSTEM_NOT_READY = /filesystem incarnation is not ready/i;
458
+ var RUN_DISPATCH_ENDPOINT = /\/runtime\/agents\/run\/stream(?:[?#]|$)/;
459
+ var RUN_DISPATCH_REFUSAL_MESSAGE = /\/runtime\/agents\/run\/stream: error code: (50[234])\b/;
460
+ function classifySandboxTransientFailure(error) {
461
+ for (const cause of causeChain(error)) {
462
+ const message = typeof cause.message === "string" ? cause.message : "";
463
+ if (cause.code === "FILESYSTEM_INCARNATION_NOT_READY" || FILESYSTEM_NOT_READY.test(message)) {
464
+ return { code: "sandbox.filesystem_not_ready", message: "Sandbox filesystem incarnation is not ready" };
465
+ }
466
+ const code = status(cause.status);
467
+ const endpoint = typeof cause.endpoint === "string" ? cause.endpoint : "";
468
+ const refusal = (code === 502 || code === 503 || code === 504) && RUN_DISPATCH_ENDPOINT.test(endpoint) ? code : RUN_DISPATCH_REFUSAL_MESSAGE.exec(message)?.[1];
469
+ if (refusal) {
470
+ return { code: "sandbox.dispatch_refused", message: `The sandbox gateway refused the run dispatch with ${refusal}` };
471
+ }
472
+ }
473
+ if (isSandboxApiTransientFailure(serializeSandboxProvisioningError(error))) {
474
+ return { code: "sandbox.control_plane_transient", message: "A Sandbox API call failed with a server error" };
475
+ }
476
+ return null;
477
+ }
478
+ var SandboxTransientDeadlineError = class extends Error {
479
+ code;
480
+ firstSeenAt;
481
+ attempts;
482
+ constructor(failure, firstSeenAt, attempts, cause) {
483
+ const minutes = Math.round(SANDBOX_TRANSIENT_DEADLINE_MS / 6e4);
484
+ super(`${failure.message}; it persisted for more than ${minutes} minutes over ${attempts} attempts`, cause === void 0 ? void 0 : { cause });
485
+ this.name = "SandboxTransientDeadlineError";
486
+ this.code = failure.code;
487
+ this.firstSeenAt = firstSeenAt;
488
+ this.attempts = attempts;
489
+ }
490
+ };
491
+ var defaultSleep = (ms, signal) => new Promise((resolve, reject) => {
492
+ if (signal?.aborted) return reject(signal.reason);
493
+ const timer = setTimeout(resolve, ms);
494
+ signal?.addEventListener("abort", () => {
495
+ clearTimeout(timer);
496
+ reject(signal.reason);
497
+ }, { once: true });
498
+ });
499
+ async function retrySandboxTransient(attempt, options = {}) {
500
+ const now = options.now ?? Date.now;
501
+ const sleep = options.sleep ?? defaultSleep;
502
+ const deadlineMs = options.deadlineMs ?? SANDBOX_TRANSIENT_DEADLINE_MS;
503
+ let firstSeenAt;
504
+ for (let attempts = 1; ; attempts += 1) {
505
+ try {
506
+ return await attempt();
507
+ } catch (error) {
508
+ const failure = classifySandboxTransientFailure(error);
509
+ if (!failure) throw error;
510
+ const at = now();
511
+ firstSeenAt ??= at;
512
+ const delayMs = sandboxTransientBackoffMs(attempts);
513
+ if (at + delayMs - firstSeenAt > deadlineMs) throw new SandboxTransientDeadlineError(failure, firstSeenAt, attempts, error);
514
+ options.onRetry?.(failure, attempts, delayMs);
515
+ await sleep(delayMs, options.signal);
516
+ }
517
+ }
518
+ }
519
+
520
+ export {
521
+ EGRESS_PROXY_RECOVERY_REQUIRED,
522
+ EGRESS_PROXY_RECOVERY_PHASE,
523
+ WORKSPACE_SANDBOX_MISSING,
524
+ WORKSPACE_SANDBOX_HOST_EXHAUSTED,
525
+ WORKSPACE_SANDBOX_UNRECOVERABLE,
526
+ WORKSPACE_SANDBOX_SNAPSHOT_MAX_AGE_MS,
527
+ WorkspaceSandboxRecoveryRequiredError,
528
+ isEgressProxyRecoveryRequiredError,
529
+ isWorkspaceSandboxSnapshotRestoreError,
530
+ assessWorkspaceSandboxSnapshot,
531
+ isWorkspaceSandboxRecoveryAction,
532
+ isWorkspaceSandboxRecoveryCode,
533
+ isWorkspaceSandboxRecoveryState,
534
+ workspaceSandboxRecoveryFromError,
535
+ workspaceSandboxRecoveryMessage,
536
+ workspaceSandboxRecoveryRecommendedActions,
537
+ workspaceSandboxRecoveryDiagnostic,
538
+ preferredWorkspaceSandboxRecoveryBoxKey,
539
+ shouldRestoreWorkspaceSandboxRecovery,
540
+ createWorkspaceSandboxRecoveryManager,
541
+ WORKSPACE_SANDBOX_RECOVERY_ACTIONS,
542
+ WORKSPACE_SANDBOX_RECOVERY_CODES,
543
+ serializeSandboxProvisioningError,
544
+ formatSandboxProvisioningSupportDetails,
545
+ isSandboxAuthFailure,
546
+ isSandboxApiBearerAuthFailure,
547
+ SANDBOX_BACKING_CONTAINER_MISSING_CODE,
548
+ isLegacySandboxBackingContainerMissingMessage,
549
+ isSandboxApiSandboxMissingFailure,
550
+ isSandboxHostCapacityFailure,
551
+ isSandboxBoxConfigFailure,
552
+ isSandboxApiTransientFailure,
553
+ formatSandboxProvisioningUserMessage,
554
+ SANDBOX_TRANSIENT_DEADLINE_MS,
555
+ sandboxTransientBackoffMs,
556
+ classifySandboxTransientFailure,
557
+ SandboxTransientDeadlineError,
558
+ retrySandboxTransient
559
+ };
560
+ //# sourceMappingURL=chunk-4LBCTFBP.js.map