@tangle-network/agent-app 0.63.18 → 0.63.20
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.
- package/README.md +3 -3
- package/dist/{DesignCanvas-IQKYAEC4.js → DesignCanvas-CFNV5JGQ.js} +6 -6
- package/dist/DesignCanvasEditor-KH4BDUE3.js +19 -0
- package/dist/agent-enrollment/drizzle.js +3 -3
- package/dist/agent-enrollment/index.js +3 -3
- package/dist/agent-profiles/d1.js +2 -2
- package/dist/agent-profiles/index.js +2 -2
- package/dist/agent-profiles/switch.d.ts +1 -1
- package/dist/agent-profiles/switch.js +1 -1
- package/dist/app-auth/index.js +3 -3
- package/dist/app-auth/index.js.map +1 -1
- package/dist/chat-routes/hybrid-producer.d.ts +1 -1
- package/dist/chat-routes/index.js +14 -14
- package/dist/chat-routes/model-failover-stream.d.ts +1 -1
- package/dist/chat-routes/turn-prefetch.d.ts +1 -1
- package/dist/{chunk-Z4RD5UF7.js → chunk-2PCNHHN2.js} +2 -2
- package/dist/{chunk-I6BZVIRH.js → chunk-3CI35I4X.js} +1 -1
- package/dist/chunk-3CI35I4X.js.map +1 -0
- package/dist/{chunk-ISP6ZH3G.js → chunk-3Q3UEWKK.js} +1 -1
- package/dist/chunk-3Q3UEWKK.js.map +1 -0
- package/dist/{chunk-VDYEVCQQ.js → chunk-5WSSH3X5.js} +2 -2
- package/dist/chunk-5WSSH3X5.js.map +1 -0
- package/dist/{chunk-WP2GYFZ4.js → chunk-ALIWB2P3.js} +1 -1
- package/dist/{chunk-WP2GYFZ4.js.map → chunk-ALIWB2P3.js.map} +1 -1
- package/dist/{chunk-6GX3UHQW.js → chunk-B5YYKYMM.js} +10 -10
- package/dist/chunk-B5YYKYMM.js.map +1 -0
- package/dist/{chunk-BTKI4XMM.js → chunk-CPWMBYLQ.js} +2 -2
- package/dist/{chunk-NUWSPR5O.js → chunk-G3LZITCA.js} +2 -2
- package/dist/{chunk-PK65B6HP.js → chunk-H4JL74GP.js} +165 -7
- package/dist/chunk-H4JL74GP.js.map +1 -0
- package/dist/{chunk-PIPNDZ6H.js → chunk-I4CFBLSJ.js} +3 -3
- package/dist/{chunk-IX6UVHXY.js → chunk-JKECSYDB.js} +2 -2
- package/dist/{chunk-BI5AZAOO.js → chunk-JUQMVD4U.js} +2 -2
- package/dist/{chunk-5GSZGTPJ.js → chunk-KA6LD4OI.js} +3 -3
- package/dist/{chunk-KH4QSBNI.js → chunk-KADVQCAS.js} +4 -4
- package/dist/{chunk-MT6ZTQGD.js → chunk-N5RWYR5N.js} +1 -1
- package/dist/chunk-N5RWYR5N.js.map +1 -0
- package/dist/{chunk-7VTGB4DM.js → chunk-OSMMEMDH.js} +1 -1
- package/dist/chunk-OSMMEMDH.js.map +1 -0
- package/dist/{chunk-5FAKBTCK.js → chunk-OYFA7BAK.js} +1 -1
- package/dist/chunk-OYFA7BAK.js.map +1 -0
- package/dist/{chunk-7EDNPQHW.js → chunk-OZDMIYES.js} +1 -1
- package/dist/chunk-OZDMIYES.js.map +1 -0
- package/dist/{chunk-CPVGRQ43.js → chunk-QFIVY6WA.js} +5 -5
- package/dist/{chunk-ZLX4BA27.js → chunk-QUT3T2IN.js} +1 -1
- package/dist/chunk-QUT3T2IN.js.map +1 -0
- package/dist/{chunk-UGFCLD32.js → chunk-SFR7FPWL.js} +2 -2
- package/dist/{chunk-OODZCBSM.js → chunk-SJ73F7HD.js} +2 -2
- package/dist/{chunk-5UQJ7F3P.js → chunk-UULUBF5J.js} +1 -1
- package/dist/chunk-UULUBF5J.js.map +1 -0
- package/dist/{chunk-ZWJ3SZ2G.js → chunk-VCG4DQC4.js} +2 -2
- package/dist/{chunk-LRYY3OX7.js → chunk-VEPII3YP.js} +4 -4
- package/dist/chunk-VEPII3YP.js.map +1 -0
- package/dist/{chunk-A3BJDXPX.js → chunk-VOXKDO4T.js} +13 -7
- package/dist/chunk-VOXKDO4T.js.map +1 -0
- package/dist/{chunk-4FEH6266.js → chunk-W3KXRU5N.js} +2 -2
- package/dist/{chunk-AVBNS64P.js → chunk-W6G2HF6M.js} +1 -1
- package/dist/chunk-W6G2HF6M.js.map +1 -0
- package/dist/{chunk-4GINUJPF.js → chunk-WRCMVBUJ.js} +1 -1
- package/dist/{chunk-4GINUJPF.js.map → chunk-WRCMVBUJ.js.map} +1 -1
- package/dist/{chunk-UTL6TEN6.js → chunk-WTNW4NFS.js} +3 -3
- package/dist/{chunk-E3AOUTIR.js → chunk-XGFGSZS5.js} +2 -2
- package/dist/chunk-XGFGSZS5.js.map +1 -0
- package/dist/{chunk-EKXG4JSN.js → chunk-YRWUJKW5.js} +2 -2
- package/dist/chunk-YRWUJKW5.js.map +1 -0
- package/dist/{chunk-CQGEGPY7.js → chunk-ZNE4NYU7.js} +2 -2
- package/dist/design-canvas/index.js +3 -3
- package/dist/design-canvas-react/engine.js +5 -5
- package/dist/design-canvas-react/index.js +8 -8
- package/dist/design-canvas-react/lazy.js +1 -1
- package/dist/hosted-agent/application.js +2 -2
- package/dist/interactions/index.js +2 -2
- package/dist/journeys/cli.js +1 -1
- package/dist/journeys/index.js +1 -1
- package/dist/journeys/index.js.map +1 -1
- package/dist/journeys/score.d.ts +1 -1
- package/dist/journeys/scoreboard.d.ts +1 -2
- package/dist/journeys/send.d.ts +1 -2
- package/dist/launch-invariants/auth-lookup-cache.d.ts +1 -1
- package/dist/launch-invariants/catalog.d.ts +3 -3
- package/dist/launch-invariants/cli.js +3 -3
- package/dist/launch-invariants/index.d.ts +1 -1
- package/dist/launch-invariants/index.js +3 -3
- package/dist/launch-invariants/index.js.map +1 -1
- package/dist/launch-invariants/limit-alarms.d.ts +1 -1
- package/dist/launch-invariants/pasted-code.d.ts +1 -1
- package/dist/launch-invariants/schedule.d.ts +1 -1
- package/dist/launch-invariants/settlement.d.ts +2 -2
- package/dist/launch-invariants/sized-reads.d.ts +1 -1
- package/dist/launch-invariants/testing.js +3 -3
- package/dist/model-resolution/index.js +1 -1
- package/dist/openui/index.js +1 -1
- package/dist/platform/billable-turn-admission.d.ts +1 -1
- package/dist/platform/delegated-keys.d.ts +2 -2
- package/dist/platform/delegated-turn-keys.d.ts +1 -1
- package/dist/platform/index.js +5 -5
- package/dist/platform/index.js.map +1 -1
- package/dist/preflight/cli.js +1 -1
- package/dist/preflight/index.d.ts +1 -1
- package/dist/preflight/index.js +1 -1
- package/dist/preset-cloudflare/index.js +7 -6
- package/dist/preset-cloudflare/index.js.map +1 -1
- package/dist/public-consultation/index.d.ts +1 -1
- package/dist/public-consultation/index.js +12 -12
- package/dist/rollout/cli.js +1 -1
- package/dist/rollout/d1.js +1 -1
- package/dist/rollout/index.d.ts +1 -1
- package/dist/rollout/index.js +1 -1
- package/dist/runtime/index.js +3 -3
- package/dist/runtime/router-turn.d.ts +3 -3
- package/dist/runtime/router-turn.js +2 -2
- package/dist/runtime/turn-knowledge.d.ts +1 -1
- package/dist/sandbox/index.d.ts +1 -0
- package/dist/sandbox/index.js +7 -5
- package/dist/sandbox/model.d.ts +1 -1
- package/dist/sandbox/prewarm-claim-d1.d.ts +1 -1
- package/dist/sandbox/prewarm.d.ts +2 -2
- package/dist/sandbox/recovery-original.d.ts +25 -0
- package/dist/sandbox/recovery.d.ts +18 -0
- package/dist/sandbox/reuse-state.d.ts +1 -1
- package/dist/sandbox/terminal-connection.d.ts +4 -4
- package/dist/sandbox/warm-lease.d.ts +2 -2
- package/dist/sandbox/warm-receipts.d.ts +1 -1
- package/dist/sequences/index.js +2 -2
- package/dist/signoff/app-defaults.d.ts +1 -1
- package/dist/signoff/cli.js +2 -2
- package/dist/signoff/index.js +2 -2
- package/dist/signoff/surface-ladder.d.ts +2 -2
- package/dist/signoff/workspace-surface.d.ts +1 -1
- package/dist/stream/index.js +2 -2
- package/dist/stream/turn-buffer.d.ts +1 -1
- package/dist/turn-health/index.js +1 -1
- package/dist/turn-health/index.js.map +1 -1
- package/dist/turn-stream/adapters.d.ts +1 -1
- package/dist/turn-stream/core.d.ts +1 -1
- package/dist/turn-stream/index.js +2 -2
- package/dist/turn-trace/index.js +1 -1
- package/dist/turn-trace/otel.d.ts +1 -1
- package/dist/web/core.d.ts +2 -2
- package/dist/web/index.js +2 -2
- package/dist/workspace-export/index.js.map +1 -1
- package/package.json +2 -2
- package/dist/DesignCanvasEditor-O7OCZTGC.js +0 -19
- package/dist/chunk-5FAKBTCK.js.map +0 -1
- package/dist/chunk-5UQJ7F3P.js.map +0 -1
- package/dist/chunk-6GX3UHQW.js.map +0 -1
- package/dist/chunk-7EDNPQHW.js.map +0 -1
- package/dist/chunk-7VTGB4DM.js.map +0 -1
- package/dist/chunk-A3BJDXPX.js.map +0 -1
- package/dist/chunk-AVBNS64P.js.map +0 -1
- package/dist/chunk-E3AOUTIR.js.map +0 -1
- package/dist/chunk-EKXG4JSN.js.map +0 -1
- package/dist/chunk-I6BZVIRH.js.map +0 -1
- package/dist/chunk-ISP6ZH3G.js.map +0 -1
- package/dist/chunk-LRYY3OX7.js.map +0 -1
- package/dist/chunk-MT6ZTQGD.js.map +0 -1
- package/dist/chunk-PK65B6HP.js.map +0 -1
- package/dist/chunk-VDYEVCQQ.js.map +0 -1
- package/dist/chunk-ZLX4BA27.js.map +0 -1
- /package/dist/{DesignCanvas-IQKYAEC4.js.map → DesignCanvas-CFNV5JGQ.js.map} +0 -0
- /package/dist/{DesignCanvasEditor-O7OCZTGC.js.map → DesignCanvasEditor-KH4BDUE3.js.map} +0 -0
- /package/dist/{chunk-Z4RD5UF7.js.map → chunk-2PCNHHN2.js.map} +0 -0
- /package/dist/{chunk-BTKI4XMM.js.map → chunk-CPWMBYLQ.js.map} +0 -0
- /package/dist/{chunk-NUWSPR5O.js.map → chunk-G3LZITCA.js.map} +0 -0
- /package/dist/{chunk-PIPNDZ6H.js.map → chunk-I4CFBLSJ.js.map} +0 -0
- /package/dist/{chunk-IX6UVHXY.js.map → chunk-JKECSYDB.js.map} +0 -0
- /package/dist/{chunk-BI5AZAOO.js.map → chunk-JUQMVD4U.js.map} +0 -0
- /package/dist/{chunk-5GSZGTPJ.js.map → chunk-KA6LD4OI.js.map} +0 -0
- /package/dist/{chunk-KH4QSBNI.js.map → chunk-KADVQCAS.js.map} +0 -0
- /package/dist/{chunk-CPVGRQ43.js.map → chunk-QFIVY6WA.js.map} +0 -0
- /package/dist/{chunk-UGFCLD32.js.map → chunk-SFR7FPWL.js.map} +0 -0
- /package/dist/{chunk-OODZCBSM.js.map → chunk-SJ73F7HD.js.map} +0 -0
- /package/dist/{chunk-ZWJ3SZ2G.js.map → chunk-VCG4DQC4.js.map} +0 -0
- /package/dist/{chunk-4FEH6266.js.map → chunk-W3KXRU5N.js.map} +0 -0
- /package/dist/{chunk-UTL6TEN6.js.map → chunk-WTNW4NFS.js.map} +0 -0
- /package/dist/{chunk-CQGEGPY7.js.map → chunk-ZNE4NYU7.js.map} +0 -0
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/turn-stream/core.ts","../src/turn-stream/cancellation.ts","../src/chat-routes/stale-turn-lock.ts","../src/turn-stream/adapters.ts"],"sourcesContent":["/**\n * Channel tokens, lock records, workspace signals, and durable event keys.\n * Interactive turn replay belongs to the sandbox session gateway.\n * An optional turn-event backend stores durable rows and a running-turn index.\n * This core uses WebCrypto and plain data without a Cloudflare runtime import.\n */\n\nimport { base64UrlEncodeText, constantTimeEqual, hmacSha256Base64Url } from '../crypto/web-token'\n\n// ── events ──────────────────────────────────────────────────────────────────\n\n/** One event on a turn-stream channel. Workspace-signal events\n * (`thread.created`, `thread.activity`, product events) carry no `seq` —\n * ordering is delivery order on the DO's single-threaded event loop. */\nexport interface TurnStreamEvent {\n type: string\n data?: unknown\n timestamp: number\n seq?: never\n}\n\n// ── channel-bound capability tokens (issue #746) ─────────────────────────────\n//\n// The DO's endpoints are not publicly routed, but \"safe because nothing\n// forwards to it\" is convention, not enforcement: one accidental\n// `stub.fetch(publicRequest)` and every channel's lock, turn events, and\n// signals are cross-tenant writable. Every request the DO serves must now\n// carry a short-lived HMAC capability token minted for THIS DO's channel\n// name — a valid token for one channel is refused by every other, and a\n// token without the secret cannot exist. Worker code mints with the same\n// secret via the adapters (`authSecret`) and the WS upgrade forwarder;\n// nothing else needs to know the format.\n\n/** Default capability lifetime. Tokens are minted per worker request and\n * verified immediately; a WS token gates only the upgrade handshake — an\n * accepted socket lives until it closes. */\nexport const TURN_STREAM_TOKEN_TTL_MS = 60_000\n\n/** Request header the adapters attach (and the DO accepts). The worker also\n * attaches it when forwarding an authorized browser WebSocket handshake. */\nexport const TURN_STREAM_TOKEN_HEADER = 'x-turn-stream-token'\n\n/** Server control authority is separate from the channel capabilities used by existing viewer forwarders. */\nexport interface TurnStreamTokenClaims {\n purpose: 'turn-control' | 'turn-cancel' | 'turn-recovery'\n identity: TurnLockReleaseInput & { turnStreamId: string }\n}\n\nfunction encodeTurnStreamClaims(claims: TurnStreamTokenClaims): string {\n const { identity, purpose } = claims\n if (!['turn-control', 'turn-cancel', 'turn-recovery'].includes(purpose)\n || !identity || !['thread', 'workspace'].includes(identity.scope)\n || [identity.workspaceId, identity.threadId, identity.executionId, identity.lockId, identity.turnStreamId]\n .some(value => typeof value !== 'string' || !value.trim())) throw new Error('Invalid turn-stream control claims')\n return base64UrlEncodeText(JSON.stringify([purpose, identity.workspaceId, identity.threadId, identity.scope,\n identity.executionId, identity.lockId, identity.turnStreamId]))\n}\n\n/** Legacy v1 channel capabilities remain unchanged. Optional v2 claims bind\n * server-only purpose and exact owner identity inside the same signed payload. */\nexport async function mintTurnStreamToken(\n channelName: string,\n secret: string,\n now: () => number = Date.now,\n ttlMs: number = TURN_STREAM_TOKEN_TTL_MS,\n claims?: TurnStreamTokenClaims,\n): Promise<string> {\n if (!secret || secret.length < 32) throw new Error('turn-stream token secret must be at least 32 characters')\n const expiresAt = now() + ttlMs\n const payload = `${claims ? 'v2' : 'v1'}.${base64UrlEncodeText(channelName)}.${expiresAt.toString(36)}${claims ? `.${encodeTurnStreamClaims(claims)}` : ''}`\n return `${payload}.${await hmacSha256Base64Url(payload, secret)}`\n}\n\n/** Verify a token for `channelName`: constant-time MAC compare with the\n * exact channel name embedded in the signed payload, plus freshness. A\n * token minted for any other channel — or a tampered expiry — is false. */\nexport async function verifyTurnStreamToken(\n channelName: string,\n token: string,\n secret: string,\n now: () => number = Date.now,\n claims?: TurnStreamTokenClaims,\n): Promise<boolean> {\n if (!secret || secret.length < 32) return false\n const parts = token.split('.')\n const version = parts[0]\n if (!(version === 'v1' && parts.length === 4 || version === 'v2' && parts.length === 5)) return false\n const name = parts[1]!\n const expires36 = parts[2]!\n const mac = parts.at(-1)!\n if (claims) {\n try { if (version !== 'v2' || parts[3] !== encodeTurnStreamClaims(claims)) return false }\n catch { return false }\n }\n if (name !== base64UrlEncodeText(channelName)) return false\n if (!/^[0-9a-z]+$/.test(expires36)) return false\n const expiresAt = parseInt(expires36, 36)\n if (!Number.isSafeInteger(expiresAt) || expiresAt <= now()) return false\n const expected = await hmacSha256Base64Url(parts.slice(0, -1).join('.'), secret)\n return constantTimeEqual(mac, expected)\n}\n\n// ── channel keys ────────────────────────────────────────────────────────────\n//\n// One DO instance per channel key. Four families:\n// thread `${workspaceId}:${threadId}` — thread viewer signals.\n// workspace `${workspaceId}` — sidebar activity + thread.created\n// + every turn lock: the workspace-scope lock and each\n// thread-scope lock, under separate storage keys.\n// turn `turn:${turnId}` — durable turn-event rows + status\n// (the TurnEventStore contract; replay survives DO eviction).\n// scope `scope:${scopeId}` — running-turn index for a thread,\n// backing `TurnEventStore.listRunning`.\n\n/** Define the scope level for acquiring a turn lock within thread or workspace contexts */\nexport type TurnLockScope = 'thread' | 'workspace'\n\n/** Channel for a thread's viewer signals. */\nexport function threadChannelKey(workspaceId: string, threadId: string): string {\n return `${workspaceId}:${threadId}`\n}\n\n/** Channel for workspace signals and the workspace-scoped lock. */\nexport function workspaceChannelKey(workspaceId: string): string {\n return workspaceId\n}\n\n/** The channel a lock lives on: the workspace channel, for both scopes.\n * Workspace-scope locks serialize every thread in the workspace (one shared\n * sandbox) and thread-scope locks serialize one thread (router lane); they\n * stay independent because each has its own storage key\n * ({@link turnLockStorageKey}). A thread lock on its own channel created a\n * new Durable Object on every thread's first turn, 0.8-1.9 s inside Creative's\n * admission on staging (2026-10-20) against an object that already exists. */\nexport function turnLockChannelKey(workspaceId: string, _threadId: string, _scope: TurnLockScope): string {\n return workspaceChannelKey(workspaceId)\n}\n\n/** The storage key a lock lives under inside its channel's instance: the\n * workspace-scope lock under `TURN_STREAM_STORAGE_KEYS.lock`, each thread's\n * lock under `TURN_STREAM_STORAGE_KEYS.threadLockPrefix` + its thread id. */\nexport function turnLockStorageKey(scope: TurnLockScope, threadId: string): string {\n return scope === 'workspace' ? TURN_STREAM_STORAGE_KEYS.lock : `${TURN_STREAM_STORAGE_KEYS.threadLockPrefix}${threadId}`\n}\n\n/** Channel for the optional Durable Object turn-event backend. */\nexport function turnStorageChannelKey(turnId: string): string {\n return `turn:${turnId}`\n}\n\n/** Running-turn index for the optional Durable Object turn-event backend. */\nexport function scopeIndexChannelKey(scopeId: string): string {\n return `scope:${scopeId}`\n}\n\n/** Recent thread-created markers kept for late-connecting sidebars. */\nexport const MAX_RECENT_CREATED = 50\n\n/** A responding marker older than this is treated as stale, so a dropped\n * `end` broadcast can't leave a permanently-stuck \"responding\" dot. */\nexport const ACTIVITY_TTL_MS = 15 * 60 * 1000\n\n/** Remove responding markers older than ttlMs; return their thread IDs. */\nexport function pruneStaleThreads(active: Map<string, number>, now: number, ttlMs: number): string[] {\n const removed: string[] = []\n for (const [threadId, startedAt] of active) {\n if (now - startedAt > ttlMs) {\n active.delete(threadId)\n removed.push(threadId)\n }\n }\n return removed\n}\n\n// ── chat-turn lock record + fences ──────────────────────────────────────────\n\n/** Fallback lifetime for an unreleased lock. Use stale-lock reconciliation to\n * recover earlier when execution state proves that its holder stopped. */\nexport const TURN_LOCK_TTL_MS = 30 * 60 * 1000\n\n/** The stored single-flight lock. Field-compatible with the reference\n * consumer's `ChatTurnLock` so adoption is a swap, not a migration. */\nexport interface DurableTurnLock {\n workspaceId: string\n threadId: string\n scope: TurnLockScope\n executionId: string\n lockId: string\n startedAt: number\n expiresAt: number\n turnId?: string\n /** The turn released this lock, but a product-owned post-turn task (e.g.\n * file persistence reading the box) is still running, so the release is\n * parked on the lock until the task settles. Written only through the DO's\n * defer seam — the base package never sets it on its own. */\n releasePending?: boolean\n /** Optional Router control, owned by this exact lock generation. Stop never releases the lock. */\n cancellation?: { turnStreamId: string; requestedAt?: number }\n}\n\n/** Define input parameters required to acquire a turn-based lock in a workspace thread */\nexport interface TurnLockAcquireInput {\n workspaceId: string\n threadId: string\n scope: TurnLockScope\n executionId: string\n lockId: string\n turnId?: string\n cancellation?: { turnStreamId: string }\n}\n\n/** Resolve the result of attempting to acquire a turn lock indicating success or active lock status */\nexport type TurnLockAcquireResult =\n | { acquired: true; lock: DurableTurnLock }\n | { acquired: false; active: DurableTurnLock }\n\n/** Define input parameters required to release a turn lock in a specific workspace thread */\nexport interface TurnLockReleaseInput {\n workspaceId: string\n threadId: string\n scope: TurnLockScope\n executionId: string\n lockId: string\n}\n\n/** Fenced out-of-band release (stop button, stale-lock reconciliation). The\n * fences make it refuse a SUCCESSOR lock: `interruptedAt` must not precede\n * the lock's own start, and when either side names a turn, both must name\n * the same one. */\nexport interface TurnLockInterruptedReleaseInput {\n workspaceId: string\n threadId: string\n /** Try only this scope; omit to try workspace then thread. */\n scope?: TurnLockScope\n interruptedAt: number\n turnId?: string\n /** Only after the existing stale policy positively proves this exact Router owner and its children abandoned. */\n recovery?: TurnLockReleaseInput & { turnStreamId: string }\n}\n\n/** `stored` is what the DO read from storage; expired locks are dead. */\nexport function activeTurnLock(stored: DurableTurnLock | undefined, now: number): DurableTurnLock | null {\n if (!stored) return null\n // Locks written before the scope field existed default to thread scope.\n const lock = stored.scope ? stored : { ...stored, scope: 'thread' as const }\n return lock.expiresAt > now ? lock : null\n}\n\n/** Create a durable turn lock object with timing and scope based on input parameters */\nexport function createTurnLock(input: TurnLockAcquireInput, now: number, ttlMs = TURN_LOCK_TTL_MS): DurableTurnLock {\n return {\n workspaceId: input.workspaceId,\n threadId: input.threadId,\n scope: input.scope,\n executionId: input.executionId,\n lockId: input.lockId,\n startedAt: now,\n expiresAt: now + ttlMs,\n ...(input.turnId ? { turnId: input.turnId } : {}),\n ...(input.cancellation ? { cancellation: { turnStreamId: input.cancellation.turnStreamId } } : {}),\n }\n}\n\n/** A cooperative release matches the execution and, when supplied, lock ID. */\nexport function turnLockMatchesRelease(\n active: DurableTurnLock,\n input: { executionId: string; lockId?: string },\n): boolean {\n if (active.executionId !== input.executionId) return false\n if (input.lockId && active.lockId !== input.lockId) return false\n return true\n}\n\n/**\n * The interrupted/stale release fence. `interruptedAt` is the instant the\n * releasing evidence was observed (the stop click, the stale-lock probe) —\n * a lock STARTED after that instant is a successor the evidence says nothing\n * about, so it survives. When the lock recorded a client turnId, the release\n * must name the same turn; a lock without one refuses a turn-specific\n * release (it cannot prove it is that turn).\n */\nexport function interruptedReleaseApplies(\n active: DurableTurnLock,\n input: { threadId: string; interruptedAt: number; turnId?: string },\n): boolean {\n if (active.threadId !== input.threadId) return false\n if (active.startedAt > input.interruptedAt) return false\n if (active.turnId) {\n if (active.turnId !== input.turnId) return false\n } else if (input.turnId) {\n return false\n }\n return true\n}\n\n// ── wire contract (adapter ↔ DO endpoint paths) ─────────────────────────────\n//\n// Kept byte-identical to the reference consumer's DO where an endpoint\n// existed there, so a product deletes its fork by re-pointing a binding, not\n// by re-speaking a protocol.\n\n/** Provide constant paths for managing chat turn streams and locks */\nexport const TURN_STREAM_PATHS = {\n broadcast: '/broadcast',\n lockAcquire: '/chat-turn-lock/acquire',\n /** Read the active lock without changing it; also brings the object up before a turn needs it. */\n lockPeek: '/chat-turn-lock/peek',\n lockRelease: '/chat-turn-lock/release',\n lockReleaseInterrupted: '/chat-turn-lock/release-interrupted',\n turnEventsAppend: '/turn-events/append',\n turnEventsRead: '/turn-events/read',\n turnStatusSet: '/turn-status/set',\n turnStatusGet: '/turn-status/get',\n scopeStatusSet: '/turn-scope/set',\n scopeRunningList: '/turn-scope/running',\n} as const\n\n/** Storage keys inside a DO instance. Exported for subclass coexistence —\n * a product extending the DO must not collide with these. */\nexport const TURN_STREAM_STORAGE_KEYS = {\n lock: 'chatTurnLock',\n threadLockPrefix: 'chatTurnLock:thread:',\n activeThreads: 'activeThreads',\n turnStatus: 'turnStatus',\n turnScope: 'turnScopeIndex',\n turnEventPrefix: 'turnEvent:',\n} as const\n\n/** Zero-padded seq so DO storage `list({ prefix })` returns rows in replay\n * order without a sort. 10 digits holds any realistic turn. */\nexport function turnEventStorageKey(seq: number): string {\n return `${TURN_STREAM_STORAGE_KEYS.turnEventPrefix}${String(seq).padStart(10, '0')}`\n}\n","import type { TurnStreamAuth, TurnStreamNamespaceLike } from './adapters'\nimport { TURN_STREAM_PATHS, TURN_STREAM_TOKEN_HEADER, mintTurnStreamToken, turnLockChannelKey } from './core'\nimport type { TurnCancellationIdentity } from './cancellation-do'\nexport type { TurnCancellationIdentity } from './cancellation-do'\n\n/** Per-owner observation only. Closing it never changes or releases the durable lock. */\nexport interface TurnCancellationConnection {\n signal: AbortSignal\n close(): void\n}\n\ninterface ControlSocket extends EventTarget {\n accept(): void\n send(value: string): void\n close(code?: number, reason?: string): void\n}\n\nexport class TurnCancellationRequestedError extends Error {\n readonly code = 'turn_cancel_requested'\n constructor() { super('Cancellation was requested for this turn'); this.name = 'TurnCancellationRequestedError' }\n}\n\nexport class TurnControlUnavailableError extends Error {\n readonly code = 'turn_control_unavailable'\n constructor(message = 'The durable turn control connection was lost') { super(message); this.name = 'TurnControlUnavailableError' }\n}\n\nasync function cancellationRequest(namespace: TurnStreamNamespaceLike, identity: TurnCancellationIdentity,\n auth: TurnStreamAuth, upgrade: boolean,\n) {\n const key = turnLockChannelKey(identity.workspaceId, identity.threadId, identity.scope)\n const url = new URL(`https://turn-stream.internal${upgrade ? '/' : TURN_STREAM_PATHS.lockReleaseInterrupted}`)\n const headers = new Headers({ [TURN_STREAM_TOKEN_HEADER]: await mintTurnStreamToken(key, auth.secret, undefined, undefined,\n { purpose: upgrade ? 'turn-control' : 'turn-cancel', identity }) })\n if (upgrade) {\n for (const [name, value] of Object.entries(identity)) url.searchParams.set(name, value)\n url.searchParams.set('sessionId', identity.threadId)\n url.searchParams.set('cancellation', '1')\n headers.set('Upgrade', 'websocket')\n } else headers.set('Content-Type', 'application/json')\n return namespace.get(namespace.idFromName(key)).fetch(url.href, { method: upgrade ? 'GET' : 'POST', headers,\n ...(upgrade ? {} : { body: JSON.stringify({ ...identity, cancel: true }) }) })\n}\n\n/** The product authorizes Stop first. This exact fenced request records intent; it never claims work stopped. */\nexport async function requestDurableTurnCancellation(namespace: TurnStreamNamespaceLike, identity: TurnCancellationIdentity,\n auth: TurnStreamAuth,\n): Promise<{ requested: boolean; reason?: 'stale-owner' }> {\n const response = await cancellationRequest(namespace, identity, auth, false)\n if (!response.ok) throw new Error(`Turn cancellation failed (HTTP ${response.status})`)\n return response.json()\n}\n\n/** One server-owned connection on the existing authenticated DO transport, with retained cancellation replay. */\nexport async function connectDurableTurnCancellation(namespace: TurnStreamNamespaceLike, identity: TurnCancellationIdentity,\n auth: TurnStreamAuth,\n): Promise<TurnCancellationConnection> {\n const response = await cancellationRequest(namespace, identity, auth, true) as Response & { webSocket?: ControlSocket }\n if (response.status !== 101 || !response.webSocket) throw new TurnControlUnavailableError(`Turn control connection failed (HTTP ${response.status})`)\n const socket = response.webSocket\n const controller = new AbortController()\n let closing = false\n let ready = false\n let resolveReady!: () => void\n let rejectReady!: (error: Error) => void\n const connected = new Promise<void>((resolve, reject) => { resolveReady = resolve; rejectReady = reject })\n const fail = () => {\n if (closing) return\n const error = new TurnControlUnavailableError()\n if (!ready) rejectReady(error)\n controller.abort(error)\n }\n const close = () => { closing = true; clearTimeout(timer); try { socket.close(1000, 'Owner observation ended') } catch { /* Already closed. */ } }\n const timer = setTimeout(() => { if (!ready) { fail(); close() } }, 10_000)\n socket.addEventListener('message', event => {\n let message: { type?: string; cancellationRequested?: boolean }\n try { message = JSON.parse(String((event as MessageEvent).data)) } catch { fail(); close(); return }\n if (message.type === 'turn.control.released') {\n // A normal owner closes before its own cooperative release. Any release\n // still observed here removes this producer's authority to continue I/O.\n fail()\n close(); return\n }\n if (message.type === 'turn.control.ownership-lost') { fail(); close(); return }\n if (message.type !== 'turn.control' || typeof message.cancellationRequested !== 'boolean') { fail(); close(); return }\n if (message.cancellationRequested) controller.abort(new TurnCancellationRequestedError())\n ready = true\n clearTimeout(timer)\n resolveReady()\n })\n socket.addEventListener('close', fail)\n socket.addEventListener('error', fail)\n try { socket.accept(); socket.send(JSON.stringify({ type: 'sync' })); await connected }\n catch (error) { close(); throw error }\n return { signal: controller.signal, close }\n}\n","import type { TurnCancellationIdentity } from '../turn-stream/cancellation'\n\n/**\n * Recovery policy for a `ChatTurnLock` whose holder died.\n *\n * `createChatTurnRoutes` takes the lock as a seam (`acquire`/`release`) and a\n * lock is a single-flight guard: while it is held, a second turn on the same\n * scope is refused. Products give it a TTL measured in tens of minutes, so a\n * turn that dies without releasing wedges chat for that whole window. Every\n * app on the seam inherits that wedge, which is why the way OUT of it is\n * policy this package owns rather than something each app rediscovers.\n *\n * The policy takes PROBES, not clients: it imports no sandbox SDK, opens no\n * connection, and knows nothing about how a product finds its box or talks to\n * a sidecar. That is what makes the rules testable and what keeps the concrete\n * probes — which box key, which session id, which sidecar endpoint — in the\n * product.\n *\n * A supplied exact Router owner probe replaces the native rules: live and\n * unknown hold; positive outer + child abandonment observes the same terminal\n * grace and exact release fence. A native session cannot attest to a Router owner.\n *\n * The native rules, in precedence order:\n *\n * 1. The session probe answered and the execution is TERMINAL ⇒ release, once\n * the lock is past a short grace period. The authority on \"is this turn\n * still running\" is whatever is actually running it; a terminal verdict is\n * proof the lock outlived its turn — but only if the verdict is about THIS\n * turn, which is what the grace buys (see\n * {@link DEFAULT_TERMINAL_TURN_LOCK_GRACE_MS}).\n * 2. The session probe answered and the execution is LIVE ⇒ hold, always.\n * Nothing below may override this. The lock is doing exactly its job.\n * 3. The probes could not reach that authority at all — the sandbox could not\n * be listed, is gone, is not running, or its session probe failed ⇒ fall\n * back on the physical argument: an execution runs INSIDE the box, so a box\n * that is not there is running nothing, and the lock is releasable. Without\n * this fallback the recovery would depend on the very subsystem whose\n * failure produced the stale lock.\n *\n * Rule 3 is gated on a grace period because it is an inference, not an\n * observation — see {@link DEFAULT_STALE_TURN_LOCK_GRACE_MS}.\n */\n\n/** Where the box is, as far as the caller can see. `state` on `not-running`\n * is the platform's own status string, carried through for the log. */\nexport type StaleTurnLockSandboxProbeResult =\n | { status: 'running' }\n | { status: 'absent' }\n | { status: 'not-running'; state?: string }\n\n/** What the thing running the turn says about it. `terminal: false` means an\n * execution is LIVE — the strongest signal in the policy. `diagnostics` rides\n * through to the result and the logs unread. */\nexport type StaleTurnLockSessionProbeResult =\n | { reachable: true; terminal: boolean; diagnostics?: Record<string, unknown> }\n | { reachable: false; reason?: string }\n\n/** A positive abandoned verdict covers the exact outer producer and every registered child/effect owner. */\nexport interface StaleTurnLockOwnerProbeResult {\n identity: TurnCancellationIdentity\n status: 'live' | 'abandoned' | 'unknown'\n diagnostics?: Record<string, unknown>\n}\n\n/**\n * Minimum age a lock must reach before the \"sandbox unreachable ⇒ nothing can\n * be running\" fallback may force-release it.\n *\n * The lock is acquired BEFORE the box is ensured, so during a cold workspace's\n * first turn there is a real window in which the lock is held and no box exists\n * yet — indistinguishable, from a peek, from a box that vanished. The grace\n * period has to outlast that window (create + bootstrap + whatever the product\n * hydrates) or a concurrent request steals the lock from a turn that is merely\n * still provisioning. Five minutes clears observed cold starts with room to\n * spare while cutting the worst case from a TTL-length wedge down to five\n * minutes. Raising it makes recovery slower; lowering it risks stealing a lock\n * mid-provision.\n */\nexport const DEFAULT_STALE_TURN_LOCK_GRACE_MS = 5 * 60 * 1000\n\n/**\n * Minimum age a lock must reach before a TERMINAL session verdict may release\n * it.\n *\n * The session probe is keyed on the THREAD, not on the execution the lock\n * holds: a sidecar that has nothing running reports `terminal` with\n * `activeExecutionId: null`, so there is no id to match the lock against. The\n * lock, meanwhile, is acquired BEFORE the box is ensured and before the\n * execution registers with the sidecar. Between those two moments a second\n * request that reconciles the lock asks the sidecar about a turn it has not\n * heard of yet and gets back the PREVIOUS turn's terminal state — proof about\n * the wrong execution. Releasing on that verdict hands the second request a\n * lock the first one is still using, which is two concurrent turns on a scope\n * whose single-flight guard just voted for itself.\n *\n * One minute covers the acquire → box-ensure → sidecar-registration window on\n * a warm box (the cold-box case is Rule 3's, and has its own, much longer\n * grace). Deliberately NOT\n * {@link DEFAULT_STALE_TURN_LOCK_GRACE_MS}: this branch has a positive\n * observation behind it, so it should recover fast, and stretching it to five\n * minutes would leave a genuinely dead turn wedged for the whole window that\n * the session probe exists to shortcut. Raising it delays recovery from a\n * crashed turn; lowering it narrows the registration window it protects.\n */\nexport const DEFAULT_TERMINAL_TURN_LOCK_GRACE_MS = 60 * 1000\n\n/** Resolve options for probing and releasing stale TURN locks based on lock start time and sandbox state */\nexport interface ReconcileStaleTurnLockOptions {\n /** When the held lock was acquired (epoch ms). The grace period is measured\n * from here, so it must be the LOCK's start, not the turn's. */\n lockStartedAt: number\n /** Router ownership replaces native-only inference. Missing/uncertain outer or child evidence must stay unknown. */\n owner?: {\n identity: TurnCancellationIdentity\n probe(identity: TurnCancellationIdentity): Promise<StaleTurnLockOwnerProbeResult>\n }\n /** Is the box there and running? Never provisions — a peek, not an ensure.\n * A throw is treated as unreachable, same as `absent`. */\n probeSandbox(): Promise<StaleTurnLockSandboxProbeResult>\n /** Ask the running box whether the execution is still live. Only called when\n * `probeSandbox` reported `running`. A throw is treated as unreachable. */\n probeSession(): Promise<StaleTurnLockSessionProbeResult>\n /** Release the lock, fenced by the instant the releasing evidence was\n * observed. `fence.observedAt` is snapshotted BEFORE the probe that\n * justified the release, so a store that can compare it against the held\n * lock's start refuses to delete a SUCCESSOR lock acquired while the probe\n * was in flight. A store that cannot make that comparison may ignore the\n * fence, but must not substitute its own `Date.now()` — that timestamp is\n * by construction newer than any successor and makes the check vacuous.\n *\n * Returns whether the release actually landed — `false` when the lock was\n * already gone (someone else got there first), which is reported, never\n * treated as a release. */\n release(fence: { observedAt: number }): boolean | Promise<boolean>\n /** Override {@link DEFAULT_STALE_TURN_LOCK_GRACE_MS} (Rule 3's fallback). */\n graceMs?: number\n /** Override {@link DEFAULT_TERMINAL_TURN_LOCK_GRACE_MS} (Rule 1's release). */\n terminalGraceMs?: number\n /** Identity fields merged into every log line (workspace, thread, execution\n * id — whatever makes the entry findable in the product's logs). */\n context?: Record<string, unknown>\n /** Defaults to `console.warn`. Both the withheld and the force-released\n * branches log; a force-release is never silent. */\n log?(message: string, meta: Record<string, unknown>): void\n /** Injectable clock, for tests. */\n now?(): number\n}\n\n/** Describe the outcome of reconciling a stale turn lock including release status and diagnostics */\nexport interface ReconcileStaleTurnLockResult {\n released: boolean\n /** Why the policy decided what it did — the probe's own diagnostics on the\n * reachable path, the unreachable reason and lock age on the fallback. */\n diagnostics: Record<string, unknown>\n}\n\nfunction messageOf(err: unknown): string {\n return err instanceof Error ? err.message : String(err)\n}\n\n/**\n * Decide whether a held lock is stale and, if so, release it.\n *\n * Never provisions and never mutates anything but the lock: a reconciliation\n * attempt on a cold workspace leaves it cold.\n */\nexport async function reconcileStaleTurnLock(\n options: ReconcileStaleTurnLockOptions,\n): Promise<ReconcileStaleTurnLockResult> {\n if (options.owner) return reconcileTurnOwner(options)\n let sandbox: StaleTurnLockSandboxProbeResult\n try {\n sandbox = await options.probeSandbox()\n } catch (err) {\n return forceReleaseUnreachable(options, {\n unreachableReason: 'SANDBOX_PROBE_FAILED',\n unreachableDetail: messageOf(err),\n })\n }\n if (sandbox.status !== 'running') {\n return forceReleaseUnreachable(options, {\n unreachableReason: sandbox.status === 'absent' ? 'SANDBOX_ABSENT' : 'SANDBOX_NOT_RUNNING',\n ...(sandbox.status === 'not-running' && sandbox.state !== undefined\n ? { sandboxState: sandbox.state }\n : {}),\n })\n }\n\n // Snapshot the clock BEFORE the probe and use that one instant for BOTH the\n // grace decision and the release fence: the release is only valid against the\n // lock as it was when the state was observed, not after an arbitrarily slow\n // round trip. Re-reading the clock after the probe would let a slow round\n // trip age the lock past the grace on paper, and would hand the store a fence\n // newer than a successor lock acquired meanwhile.\n const observedAt = (options.now ?? Date.now)()\n let session: StaleTurnLockSessionProbeResult\n try {\n session = await options.probeSession()\n } catch (err) {\n return forceReleaseUnreachable(options, {\n unreachableReason: 'SESSION_PROBE_FAILED',\n unreachableDetail: messageOf(err),\n })\n }\n if (!session.reachable) {\n return forceReleaseUnreachable(options, {\n unreachableReason: 'SESSION_UNREACHABLE',\n ...(session.reason !== undefined ? { sessionProbeError: session.reason } : {}),\n })\n }\n\n const diagnostics: Record<string, unknown> = {\n sandboxReachable: true,\n sessionTerminal: session.terminal,\n ...(session.diagnostics ?? {}),\n }\n // The authority answered and says the execution is live: the lock is doing\n // its job. Nothing below this point may override that.\n if (!session.terminal) return { released: false, diagnostics }\n\n // Terminal, but about WHICH execution? The probe is thread-keyed, so a lock\n // younger than the registration window may be reading the previous turn's\n // verdict — hold until it is old enough that the verdict has to be its own.\n const terminalGraceMs = options.terminalGraceMs ?? DEFAULT_TERMINAL_TURN_LOCK_GRACE_MS\n const lockAgeMs = observedAt - options.lockStartedAt\n if (lockAgeMs < terminalGraceMs) {\n const log = options.log ?? ((message, meta) => console.warn(message, meta))\n const withheld = {\n ...diagnostics,\n lockAgeMs,\n terminalReleaseGraceMs: terminalGraceMs,\n terminalReleaseWithheld: 'LOCK_WITHIN_TERMINAL_GRACE_PERIOD',\n }\n log('[chat-routes] stale turn lock held: terminal verdict but lock is younger than the registration window', {\n ...(options.context ?? {}),\n ...withheld,\n })\n return { released: false, diagnostics: withheld }\n }\n\n const released = await options.release({ observedAt })\n return {\n released,\n diagnostics: { ...diagnostics, lockAgeMs, terminalReleaseGraceMs: terminalGraceMs, released, observedAt },\n }\n}\n\nasync function reconcileTurnOwner(options: ReconcileStaleTurnLockOptions): Promise<ReconcileStaleTurnLockResult> {\n const owner = options.owner!\n // The observation fence is captured before the authority read, as on the native path.\n const observedAt = (options.now ?? Date.now)()\n let evidence: StaleTurnLockOwnerProbeResult\n try { evidence = await owner.probe(owner.identity) }\n catch (error) { return { released: false, diagnostics: { ownerStatus: 'unknown', ownerProbeError: messageOf(error) } } }\n const keys = ['workspaceId', 'threadId', 'scope', 'executionId', 'lockId', 'turnStreamId'] as const\n if (!evidence?.identity || keys.some(key => evidence.identity[key] !== owner.identity[key])) {\n return { released: false, diagnostics: { ownerStatus: 'unknown', releaseWithheld: 'OWNER_IDENTITY_MISMATCH' } }\n }\n const status = evidence.status === 'abandoned' || evidence.status === 'live' ? evidence.status : 'unknown'\n const diagnostics = { ...(evidence.diagnostics ?? {}), ownerStatus: status }\n if (status !== 'abandoned') return { released: false, diagnostics }\n const terminalGraceMs = options.terminalGraceMs ?? DEFAULT_TERMINAL_TURN_LOCK_GRACE_MS\n const lockAgeMs = observedAt - options.lockStartedAt\n if (lockAgeMs < terminalGraceMs) return { released: false, diagnostics: {\n ...diagnostics, lockAgeMs, terminalReleaseGraceMs: terminalGraceMs, releaseWithheld: 'LOCK_WITHIN_TERMINAL_GRACE_PERIOD',\n } }\n const released = await options.release({ observedAt })\n return { released, diagnostics: { ...diagnostics, lockAgeMs, terminalReleaseGraceMs: terminalGraceMs, observedAt, released } }\n}\n\n/**\n * The fallback: the session authority could not attest to the execution's\n * state because the box is gone, stopped, or unreachable. Release the lock\n * once it is old enough that it cannot belong to a turn still provisioning its\n * box — and say so either way, with the reason, the lock's age, and the grace\n * period it was measured against.\n *\n * One clock read (`at`) serves as the age gate AND the release fence here too,\n * for the same reason the terminal branch uses `observedAt` for both.\n */\nasync function forceReleaseUnreachable(\n options: ReconcileStaleTurnLockOptions,\n reason: Record<string, unknown> & { unreachableReason: string },\n): Promise<ReconcileStaleTurnLockResult> {\n const log = options.log ?? ((message, meta) => console.warn(message, meta))\n const graceMs = options.graceMs ?? DEFAULT_STALE_TURN_LOCK_GRACE_MS\n const at = (options.now ?? Date.now)()\n const lockAgeMs = at - options.lockStartedAt\n const diagnostics: Record<string, unknown> = {\n ...reason,\n sandboxReachable: false,\n lockAgeMs,\n forceReleaseGraceMs: graceMs,\n }\n\n if (lockAgeMs < graceMs) {\n log('[chat-routes] stale turn lock held: sandbox unreachable but lock is inside the grace period', {\n ...(options.context ?? {}),\n ...diagnostics,\n })\n return { released: false, diagnostics: { ...diagnostics, forceReleaseWithheld: 'LOCK_WITHIN_GRACE_PERIOD' } }\n }\n\n const released = await options.release({ observedAt: at })\n log('[chat-routes] force-released stale turn lock: sandbox unreachable, no turn can be executing', {\n ...(options.context ?? {}),\n ...diagnostics,\n released,\n })\n return { released, diagnostics: { ...diagnostics, forceReleased: released } }\n}\n","/**\n * Worker adapters for durable turn storage, single-flight locks, and workspace signals.\n * Namespace interfaces remain structural so Cloudflare bindings and the memory harness\n * run the same authenticated request path.\n * Products provide workspace authorization and their binding's TURN_STREAM_AUTH_SECRET.\n */\n\nimport { connectDurableTurnCancellation, type TurnCancellationConnection, type TurnCancellationIdentity } from './cancellation'\nimport { reconcileStaleTurnLock, type ReconcileStaleTurnLockOptions, type StaleTurnLockOwnerProbeResult } from '../chat-routes/stale-turn-lock'\nimport type { TurnEventStore, TurnStatus } from '../stream/turn-buffer'\nimport {\n TURN_STREAM_PATHS,\n TURN_STREAM_TOKEN_HEADER,\n mintTurnStreamToken,\n scopeIndexChannelKey,\n turnLockChannelKey,\n turnStorageChannelKey,\n workspaceChannelKey,\n type DurableTurnLock,\n type TurnLockAcquireResult,\n type TurnLockReleaseInput,\n type TurnLockInterruptedReleaseInput,\n type TurnLockScope,\n type TurnStreamEvent,\n type TurnStreamTokenClaims,\n} from './core'\n\n// ── structural namespace ────────────────────────────────────────────────────\n\n/** Resolve a stub interface for handling fetch requests with optional initialization parameters */\nexport interface TurnStreamStubLike {\n fetch(input: Request | string, init?: RequestInit): Promise<Response>\n}\n\n/** Cloudflare placement hints. A hint affects only a channel's first creation, never an existing object's location. */\nexport type TurnStreamLocationHint = 'wnam' | 'enam' | 'sam' | 'weur' | 'eeur' | 'apac' | 'apac-ne' | 'apac-se' | 'oc' | 'afr' | 'me'\n\n/** The default matches the application D1 region unless its binding descriptor names another region. */\nexport const DEFAULT_TURN_LOCK_LOCATION_HINT: TurnStreamLocationHint = 'enam'\n\n/** The surface of `DurableObjectNamespace` the adapters use. */\nexport interface TurnStreamNamespaceLike {\n idFromName(name: string): unknown\n get(id: unknown, options?: { locationHint?: string }): TurnStreamStubLike\n}\n\nconst INTERNAL_ORIGIN = 'https://turn-stream.internal'\n\n/** The HMAC secret shared between this worker and its `TurnStreamDO` binding.\n * Every adapter call mints a channel-bound capability token from it; the DO\n * refuses requests without one (issue #746). Products pass the same value\n * the DO resolves — `env.TURN_STREAM_AUTH_SECRET`. */\nexport interface TurnStreamAuth {\n secret: string\n}\n\nasync function postJson<T>(\n namespace: TurnStreamNamespaceLike,\n channelKey: string,\n path: string,\n body: unknown,\n auth: TurnStreamAuth,\n claims?: TurnStreamTokenClaims,\n): Promise<{ status: number; body: T }> {\n const stub = namespace.get(namespace.idFromName(channelKey))\n const response = await stub.fetch(`${INTERNAL_ORIGIN}${path}`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n [TURN_STREAM_TOKEN_HEADER]: await mintTurnStreamToken(channelKey, auth.secret, undefined, undefined, claims),\n },\n body: JSON.stringify(body),\n })\n return { status: response.status, body: (await response.json()) as T }\n}\n\nasync function postJsonOk<T>(\n namespace: TurnStreamNamespaceLike,\n channelKey: string,\n path: string,\n body: unknown,\n auth: TurnStreamAuth,\n claims?: TurnStreamTokenClaims,\n): Promise<T> {\n const result = await postJson<T>(namespace, channelKey, path, body, auth, claims)\n if (result.status !== 200) {\n throw new Error(`turn-stream ${path} failed with status ${result.status}`)\n }\n return result.body\n}\n\n// ── TurnEventStore adapter (the real turnStore) ─────────────────────────────\n\n/**\n * Optional Durable Object backend for {@link TurnEventStore}.\n * Each turn has its own named instance; a per-scope index tracks running turns.\n * Products choose this adapter or the D1 backend independently of their lock\n * and workspace-signal transport.\n */\nexport function createDurableObjectTurnEventStore(\n namespace: TurnStreamNamespaceLike,\n auth: TurnStreamAuth,\n): TurnEventStore {\n return {\n async append(turnId, events) {\n if (!events.length) return\n await postJsonOk(namespace, turnStorageChannelKey(turnId), TURN_STREAM_PATHS.turnEventsAppend, { events }, auth)\n },\n async read(turnId, fromSeq) {\n const body = await postJsonOk<{ events: Array<{ seq: number; event: string }> }>(\n namespace,\n turnStorageChannelKey(turnId),\n TURN_STREAM_PATHS.turnEventsRead,\n { fromSeq },\n auth,\n )\n return body.events\n },\n async setStatus(turnId, status, scopeId, options) {\n // This store keeps no update time to preserve or compare; refuse rather than write unguarded.\n if (options?.preserveUpdatedAt || options?.onlyIfRunningBefore !== undefined) {\n throw new Error('The turn-stream Durable Object store does not support guarded or time-preserving status writes')\n }\n await postJsonOk(namespace, turnStorageChannelKey(turnId), TURN_STREAM_PATHS.turnStatusSet, { status }, auth)\n if (scopeId) {\n await postJsonOk(namespace, scopeIndexChannelKey(scopeId), TURN_STREAM_PATHS.scopeStatusSet, {\n turnId,\n status,\n }, auth)\n }\n },\n async getStatus(turnId) {\n const body = await postJsonOk<{ status: TurnStatus | null }>(\n namespace,\n turnStorageChannelKey(turnId),\n TURN_STREAM_PATHS.turnStatusGet,\n {},\n auth,\n )\n return body.status\n },\n async listRunning(scopeId) {\n const body = await postJsonOk<{ running: string[] }>(\n namespace,\n scopeIndexChannelKey(scopeId),\n TURN_STREAM_PATHS.scopeRunningList,\n {},\n auth,\n )\n return body.running\n },\n }\n}\n\n// ── lock primitives (usable outside the route seam too) ─────────────────────\n\n/** Define input parameters required to acquire a durable turn lock in a workspace thread context */\nexport interface AcquireDurableTurnLockInput {\n workspaceId: string\n threadId: string\n scope: TurnLockScope\n executionId: string\n turnId?: string\n /** Supply to reclaim/retry with a stable id; default mints a UUID. */\n lockId?: string\n cancellation?: { turnStreamId: string }\n}\n\n/** Acquire a durable turn lock in the specified namespace with given input parameters */\nexport async function acquireDurableTurnLock(\n namespace: TurnStreamNamespaceLike,\n input: AcquireDurableTurnLockInput,\n auth: TurnStreamAuth,\n): Promise<TurnLockAcquireResult> {\n const lockId = input.lockId ?? crypto.randomUUID()\n const key = turnLockChannelKey(input.workspaceId, input.threadId, input.scope)\n const result = await postJson<TurnLockAcquireResult>(namespace, key, TURN_STREAM_PATHS.lockAcquire, {\n ...input,\n lockId,\n }, auth)\n if (result.status !== 200 && result.status !== 409) {\n throw new Error(`turn-stream lock acquire failed with status ${result.status}`)\n }\n return result.body\n}\n\n/**\n * Read a channel's active turn lock without changing it.\n *\n * The first request to a Durable Object that has never been used creates it,\n * which measured 1.3-2.4 s for a thread's first turn in GTM production\n * (2026-10-10) against 0.05-0.33 s once it exists; every lock now lives on the\n * workspace channel, which exists after the workspace's first turn. A peek\n * sent as soon as a turn is admitted wakes that object if it was evicted.\n */\nexport async function peekDurableTurnLock(\n namespace: TurnStreamNamespaceLike,\n input: { workspaceId: string } & ({ scope: 'workspace'; threadId?: string } | { scope: TurnLockScope; threadId: string }),\n auth: TurnStreamAuth,\n): Promise<DurableTurnLock | null> {\n const key = turnLockChannelKey(input.workspaceId, input.threadId ?? '', input.scope)\n const result = await postJson<{ active?: DurableTurnLock | null }>(namespace, key, TURN_STREAM_PATHS.lockPeek, {\n scope: input.scope,\n threadId: input.threadId,\n }, auth)\n if (result.status !== 200) throw new Error(`turn-stream lock peek failed with status ${result.status}`)\n return result.body.active ?? null\n}\n\n/** Release a durable turn lock and indicate if the release was successful or deferred */\nexport async function releaseDurableTurnLock(\n namespace: TurnStreamNamespaceLike,\n input: TurnLockReleaseInput,\n auth: TurnStreamAuth,\n): Promise<{ released: boolean; deferred?: boolean }> {\n const key = turnLockChannelKey(input.workspaceId, input.threadId, input.scope)\n return postJsonOk(namespace, key, TURN_STREAM_PATHS.lockRelease, input, auth)\n}\n\n/** Define input parameters to release an interrupted durable turn lock in a workspace or thread */\nexport type ReleaseInterruptedDurableTurnLockInput = TurnLockInterruptedReleaseInput\n\n/** Fenced out-of-band release — the DO refuses a successor lock (started\n * after `interruptedAt`) and a turnId mismatch. Returns whether any scope\n * released. */\nexport async function releaseInterruptedDurableTurnLock(\n namespace: TurnStreamNamespaceLike,\n input: ReleaseInterruptedDurableTurnLockInput,\n auth: TurnStreamAuth,\n): Promise<boolean> {\n const scopes: readonly TurnLockScope[] = input.scope ? [input.scope] : ['workspace', 'thread']\n for (const scope of scopes) {\n const key = turnLockChannelKey(input.workspaceId, input.threadId, scope)\n const result = await postJsonOk<{ released: boolean }>(namespace, key, TURN_STREAM_PATHS.lockReleaseInterrupted, {\n scope,\n threadId: input.threadId,\n interruptedAt: input.interruptedAt,\n ...(input.turnId ? { turnId: input.turnId } : {}),\n ...(input.recovery ? { recovery: input.recovery } : {}),\n }, auth, input.recovery ? { purpose: 'turn-recovery', identity: input.recovery } : undefined)\n if (result.released) return true\n }\n return false\n}\n\n// ── stale-lock reconciliation against the DO ────────────────────────────────\n\n/** Define options to reconcile stale durable turn locks with context, namespace, workspace, and active lock details */\nexport interface ReconcileStaleDurableTurnLockOptions\n extends Pick<\n ReconcileStaleTurnLockOptions,\n 'probeSandbox' | 'probeSession' | 'graceMs' | 'terminalGraceMs' | 'context' | 'log' | 'now'\n > {\n namespace: TurnStreamNamespaceLike\n auth: TurnStreamAuth\n workspaceId: string\n threadId: string\n /** The lock the acquire attempt was refused on. */\n active: DurableTurnLock\n /** Cancellable Router locks require positive exact outer + child quiescence; native-only probes cannot attest it. */\n probeOwner?(identity: TurnCancellationIdentity): Promise<StaleTurnLockOwnerProbeResult>\n}\n\n/**\n * `/chat-routes`' `reconcileStaleTurnLock` policy wired to the DO: the\n * product supplies the probes (which box, what its session says), the policy\n * decides, and a release lands as a FENCED interrupted release —\n * `interruptedAt` is the policy's `fence.observedAt`, so a successor lock\n * acquired while the probes were in flight survives by construction.\n */\nexport async function reconcileStaleDurableTurnLock(\n options: ReconcileStaleDurableTurnLockOptions,\n): Promise<{ released: boolean; diagnostics: Record<string, unknown> }> {\n const { namespace, auth, workspaceId, threadId, active, probeOwner, ...policy } = options\n const owner = active.cancellation ? { workspaceId: active.workspaceId, threadId: active.threadId, scope: active.scope,\n executionId: active.executionId, lockId: active.lockId, turnStreamId: active.cancellation.turnStreamId } : undefined\n return reconcileStaleTurnLock({\n ...policy,\n lockStartedAt: active.startedAt,\n ...(owner ? { owner: { identity: owner,\n probe: probeOwner ?? (async (identity: TurnCancellationIdentity) => ({ identity, status: 'unknown' as const })) } } : {}),\n release: (fence) =>\n releaseInterruptedDurableTurnLock(namespace, {\n workspaceId,\n threadId: active.threadId,\n scope: active.scope,\n interruptedAt: fence.observedAt,\n ...(active.turnId ? { turnId: active.turnId } : {}),\n ...(owner ? { recovery: owner } : {}),\n }, auth),\n })\n}\n\n// ── ChatTurnLock adapter (the turnLock seam) ────────────────────────────────\n\n/** The subset of `ChatTurnProduceArgs` the lock adapter reads — structural,\n * so this module needs no import from `/chat-routes/turn-routes` (which\n * would drag the `agent-runtime` peer into every `/turn-stream` consumer). */\nexport interface TurnLockSeamArgs<TContext> {\n identity: { tenantId: string; sessionId: string }\n executionId: string\n turnStreamId?: string\n context: TContext\n body: { turnId?: string }\n}\n\n/** Verdict shape of the vertical's `turnLock.acquire`. */\nexport type TurnLockSeamResult =\n | { acquired: true; handle?: unknown; cancellation?: TurnCancellationConnection }\n | { acquired: false; response: Response }\n\n/** Define options for creating a durable turn lock with customizable scope and identification methods */\nexport interface CreateDurableTurnLockOptions<TContext> {\n /** New channels use this hint; existing channel IDs and locations are preserved. */\n locationHint?: TurnStreamLocationHint\n namespace: TurnStreamNamespaceLike\n /** Capability tokens for the DO; pass the worker's\n * `env.TURN_STREAM_AUTH_SECRET`. The DO validates every request. */\n auth: TurnStreamAuth\n /** Which lane serializes this turn: `'workspace'` (shared sandbox — one\n * turn per workspace) or `'thread'` (router lane — one turn per thread). */\n scopeOf(args: TurnLockSeamArgs<TContext>): TurnLockScope\n /** Opt the existing lock into durable Stop. Leave native/durable completion lanes on their existing owner. */\n cancellationOf?(args: TurnLockSeamArgs<TContext>): boolean\n /** Override the execution id the lock records (e.g. a follow-up reclaiming\n * the execution it already dispatched). Default: the turn's own. */\n lockExecutionIdOf?(args: TurnLockSeamArgs<TContext>): string | undefined\n /** Client turn id recorded on the lock (fences interrupted releases to the\n * right turn). Default: the request body's `turnId`. */\n clientTurnIdOf?(args: TurnLockSeamArgs<TContext>): string | undefined\n /**\n * Attempt stale-lock recovery after a refused acquire. Return whether the\n * held lock was released (the adapter then retries the acquire once).\n * Products wire {@link reconcileStaleDurableTurnLock} with their sandbox +\n * session probes here. Omit → a refused acquire is final.\n */\n reconcile?(\n args: TurnLockSeamArgs<TContext>,\n active: DurableTurnLock,\n ): Promise<{ released: boolean; diagnostics?: Record<string, unknown> }>\n /** Build the refusal `Response`. Default: a 409 with the shared body shape\n * (`code`, `message`, lock identity + age, reconcile diagnostics). */\n onRefused?(active: DurableTurnLock, diagnostics: Record<string, unknown> | undefined): Response\n}\n\nfunction defaultRefusalResponse(\n active: DurableTurnLock,\n diagnostics: Record<string, unknown> | undefined,\n): Response {\n const workspaceConflict = active.scope === 'workspace'\n return Response.json(\n {\n error: {\n code: workspaceConflict ? 'workspace_turn_in_flight' : 'chat_turn_in_flight',\n message: workspaceConflict\n ? 'A chat turn is already running for this workspace. Wait for it to finish before starting another.'\n : 'A chat turn is already running for this thread. Reconnect to the active response or wait for it to finish.',\n threadId: active.threadId,\n executionId: active.executionId,\n ...(active.turnId ? { turnId: active.turnId } : {}),\n lockStartedAt: active.startedAt,\n lockAgeMs: Date.now() - active.startedAt,\n ...(diagnostics ? { diagnostics } : {}),\n },\n },\n { status: 409 },\n )\n}\n\n/**\n * The vertical's `turnLock` seam on the shared DO: dual-scope single-flight\n * acquire (with one reconcile-then-retry pass when the product supplies a\n * stale-lock reconciler) and cooperative release on settle. The returned\n * object satisfies `createChatTurnRoutes`' `ChatTurnLock<TContext>`\n * structurally.\n */\nexport function createDurableTurnLock<TContext>(options: CreateDurableTurnLockOptions<TContext>): {\n acquire(args: TurnLockSeamArgs<TContext>): Promise<TurnLockSeamResult>\n release(handle: unknown): Promise<void>\n warm(target: { tenantId: string; threadId?: string }): Promise<void>\n} {\n const namespace: TurnStreamNamespaceLike = {\n idFromName: name => options.namespace.idFromName(name),\n get: (id, init) => options.namespace.get(id, {\n ...init, locationHint: options.locationHint ?? DEFAULT_TURN_LOCK_LOCATION_HINT,\n }),\n }\n return {\n // Every lock lives on the workspace channel, so this wakes that object\n // (evicted after idle) before the turn's acquire reaches it.\n async warm(target) {\n await peekDurableTurnLock(namespace, target.threadId\n ? { workspaceId: target.tenantId, threadId: target.threadId, scope: 'thread' }\n : { workspaceId: target.tenantId, scope: 'workspace' }, options.auth)\n },\n async acquire(args) {\n const workspaceId = args.identity.tenantId\n const threadId = args.identity.sessionId\n const scope = options.scopeOf(args)\n const executionId = options.lockExecutionIdOf?.(args) ?? args.executionId\n const turnId = options.clientTurnIdOf ? options.clientTurnIdOf(args) : args.body.turnId\n const cancellable = options.cancellationOf?.(args) === true\n if (cancellable && !args.turnStreamId) throw new Error('Durable cancellation requires the route turnStreamId')\n const input: AcquireDurableTurnLockInput = {\n workspaceId,\n threadId,\n scope,\n executionId,\n ...(turnId ? { turnId } : {}),\n ...(cancellable ? { cancellation: { turnStreamId: args.turnStreamId! } } : {}),\n }\n\n let acquired = await acquireDurableTurnLock(namespace, input, options.auth)\n let diagnostics: Record<string, unknown> | undefined\n if (!acquired.acquired && options.reconcile) {\n const reconciled = await options.reconcile(args, acquired.active)\n diagnostics = reconciled.diagnostics\n if (reconciled.released) {\n acquired = await acquireDurableTurnLock(namespace, input, options.auth)\n }\n }\n if (!acquired.acquired) {\n return {\n acquired: false,\n response: (options.onRefused ?? defaultRefusalResponse)(acquired.active, diagnostics),\n }\n }\n const handle: TurnLockReleaseInput = {\n workspaceId,\n threadId,\n scope,\n executionId,\n lockId: acquired.lock.lockId,\n }\n if (!cancellable) return { acquired: true, handle }\n try {\n const cancellation = await connectDurableTurnCancellation(options.namespace,\n { ...handle, turnStreamId: args.turnStreamId! }, options.auth)\n return { acquired: true, handle, cancellation }\n } catch (error) {\n // No producer may run without its control connection. Release only this newly acquired generation.\n await releaseDurableTurnLock(options.namespace, handle, options.auth)\n throw error\n }\n },\n async release(handle) {\n if (!handle) return\n await releaseDurableTurnLock(namespace, handle as TurnLockReleaseInput, options.auth)\n },\n }\n}\n\n// ── broadcast helpers (products wire these into onEvent) ────────────────────\n\n/** Coarse per-workspace marker that a thread's turn started / ended — drives\n * a sidebar \"agent responding\" indicator subscribed once per workspace. */\nexport async function broadcastWorkspaceActivity(\n namespace: TurnStreamNamespaceLike,\n workspaceId: string,\n threadId: string,\n phase: 'start' | 'end',\n auth: TurnStreamAuth,\n): Promise<void> {\n try {\n await postJson(namespace, workspaceChannelKey(workspaceId), TURN_STREAM_PATHS.broadcast, {\n type: 'thread.activity',\n timestamp: Date.now(),\n data: { threadId, phase, workspaceId, sessionId: workspaceId },\n } satisfies TurnStreamEvent, auth)\n } catch {\n // Best-effort: the indicator is non-critical UI.\n }\n}\n\n/** Per-workspace marker that a new thread was created, so an already-open\n * history list prepends it without a reload. */\nexport async function broadcastThreadCreated(\n namespace: TurnStreamNamespaceLike,\n workspaceId: string,\n thread: { threadId: string; title: string },\n auth: TurnStreamAuth,\n): Promise<void> {\n try {\n await postJson(namespace, workspaceChannelKey(workspaceId), TURN_STREAM_PATHS.broadcast, {\n type: 'thread.created',\n timestamp: Date.now(),\n data: { threadId: thread.threadId, title: thread.title, workspaceId, sessionId: workspaceId },\n } satisfies TurnStreamEvent, auth)\n } catch {\n // Best-effort: the loader reconciles on the next revalidation.\n }\n}\n\n// ── WebSocket upgrade forwarder (worker entry) ──────────────────────────────\n\n/** Represent success or failure of a TURN stream upgrade authorization with optional response data */\nexport type TurnStreamUpgradeAuthorization = { ok: true } | { ok: false; response: Response }\n\n/** Define options for creating a TURN stream upgrade handler including namespace, path, and authorization logic */\nexport interface CreateTurnStreamUpgradeHandlerOptions {\n namespace: TurnStreamNamespaceLike\n /** HMAC secret shared with the DO binding (`env.TURN_STREAM_AUTH_SECRET`).\n * After the product `authorize` passes, the handler mints a channel-bound\n * capability token and the DO verifies it — the browser never holds it. */\n authSecret: string\n /** The worker route serving the stream. Default `/api/session-stream`. */\n path?: string\n /** Viewer access check (session cookie → workspace membership). */\n authorize(\n request: Request,\n target: { workspaceId: string },\n ): Promise<TurnStreamUpgradeAuthorization>\n}\n\n/**\n * The worker-entry WebSocket forwarder — WORKSPACE channel only (the\n * per-thread variant went with the deprecated rebroadcast lane; a thread's\n * live turn replay is the sandbox session gateway's job). Call BEFORE the\n * app router (a router loader cannot return a 101); returns `null` for\n * requests that are not a WebSocket upgrade on the configured path.\n *\n * GET {path}?workspaceId=... → workspace channel (sidebar signals)\n *\n * After the 101, the client sends `{type:'sync'}` and receives the\n * current-activity + recent-threads snapshot, then live signals.\n *\n * Security: the product's `authorize` decides whether THIS caller may see the\n * workspace's signals; the channel-bound token this handler then mints is\n * what the DO itself verifies — two independent gates, and the browser never\n * learns the token (it rides the forwarded upgrade request only).\n */\nexport function createTurnStreamUpgradeHandler(\n options: CreateTurnStreamUpgradeHandlerOptions,\n): (request: Request) => Promise<Response | null> {\n const path = options.path ?? '/api/session-stream'\n if (!options.authSecret || options.authSecret.length < 32) {\n throw new Error('createTurnStreamUpgradeHandler requires authSecret (>= 32 chars) — the DO fails closed without tokens')\n }\n return async (request) => {\n const url = new URL(request.url)\n if (url.pathname !== path || request.headers.get('Upgrade')?.toLowerCase() !== 'websocket') return null\n\n if (url.searchParams.has('threadId')) {\n return new Response('Thread replay uses the sandbox session gateway or chat replay route', { status: 400 })\n }\n const workspaceId = url.searchParams.get('workspaceId')\n if (!workspaceId) return new Response('Missing workspaceId', { status: 400 })\n\n const auth = await options.authorize(request, { workspaceId })\n if (!auth.ok) return auth.response\n\n const key = workspaceChannelKey(workspaceId)\n const stub = options.namespace.get(options.namespace.idFromName(key))\n\n const forwardUrl = new URL(request.url)\n forwardUrl.searchParams.set('sessionId', workspaceId)\n forwardUrl.searchParams.set('scope', 'workspace')\n forwardUrl.searchParams.delete('token')\n const headers = new Headers(request.headers)\n headers.set(TURN_STREAM_TOKEN_HEADER, await mintTurnStreamToken(key, options.authSecret))\n return stub.fetch(new Request(new Request(forwardUrl, request), { headers }))\n }\n}\n"],"mappings":";;;;;;;AAoCO,IAAM,2BAA2B;AAIjC,IAAM,2BAA2B;AAQxC,SAAS,uBAAuB,QAAuC;AACrE,QAAM,EAAE,UAAU,QAAQ,IAAI;AAC9B,MAAI,CAAC,CAAC,gBAAgB,eAAe,eAAe,EAAE,SAAS,OAAO,KACjE,CAAC,YAAY,CAAC,CAAC,UAAU,WAAW,EAAE,SAAS,SAAS,KAAK,KAC7D,CAAC,SAAS,aAAa,SAAS,UAAU,SAAS,aAAa,SAAS,QAAQ,SAAS,YAAY,EACtG,KAAK,WAAS,OAAO,UAAU,YAAY,CAAC,MAAM,KAAK,CAAC,EAAG,OAAM,IAAI,MAAM,oCAAoC;AACpH,SAAO,oBAAoB,KAAK,UAAU;AAAA,IAAC;AAAA,IAAS,SAAS;AAAA,IAAa,SAAS;AAAA,IAAU,SAAS;AAAA,IACpG,SAAS;AAAA,IAAa,SAAS;AAAA,IAAQ,SAAS;AAAA,EAAY,CAAC,CAAC;AAClE;AAIA,eAAsB,oBACpB,aACA,QACA,MAAoB,KAAK,KACzB,QAAgB,0BAChB,QACiB;AACjB,MAAI,CAAC,UAAU,OAAO,SAAS,GAAI,OAAM,IAAI,MAAM,yDAAyD;AAC5G,QAAM,YAAY,IAAI,IAAI;AAC1B,QAAM,UAAU,GAAG,SAAS,OAAO,IAAI,IAAI,oBAAoB,WAAW,CAAC,IAAI,UAAU,SAAS,EAAE,CAAC,GAAG,SAAS,IAAI,uBAAuB,MAAM,CAAC,KAAK,EAAE;AAC1J,SAAO,GAAG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MAAM,IAAI,IAAI,QAAQ,GAAG;AAC/B,QAAI,IAAI,aAAa,QAAQ,QAAQ,QAAQ,IAAI,SAAS,GAAG,YAAY,MAAM,YAAa,QAAO;AAEnG,QAAI,IAAI,aAAa,IAAI,UAAU,GAAG;AACpC,aAAO,IAAI,SAAS,uEAAuE,EAAE,QAAQ,IAAI,CAAC;AAAA,IAC5G;AACA,UAAM,cAAc,IAAI,aAAa,IAAI,aAAa;AACtD,QAAI,CAAC,YAAa,QAAO,IAAI,SAAS,uBAAuB,EAAE,QAAQ,IAAI,CAAC;AAE5E,UAAM,OAAO,MAAM,QAAQ,UAAU,SAAS,EAAE,YAAY,CAAC;AAC7D,QAAI,CAAC,KAAK,GAAI,QAAO,KAAK;AAE1B,UAAM,MAAM,oBAAoB,WAAW;AAC3C,UAAM,OAAO,QAAQ,UAAU,IAAI,QAAQ,UAAU,WAAW,GAAG,CAAC;AAEpE,UAAM,aAAa,IAAI,IAAI,QAAQ,GAAG;AACtC,eAAW,aAAa,IAAI,aAAa,WAAW;AACpD,eAAW,aAAa,IAAI,SAAS,WAAW;AAChD,eAAW,aAAa,OAAO,OAAO;AACtC,UAAM,UAAU,IAAI,QAAQ,QAAQ,OAAO;AAC3C,YAAQ,IAAI,0BAA0B,MAAM,oBAAoB,KAAK,QAAQ,UAAU,CAAC;AACxF,WAAO,KAAK,MAAM,IAAI,QAAQ,IAAI,QAAQ,YAAY,OAAO,GAAG,EAAE,QAAQ,CAAC,CAAC;AAAA,EAC9E;AACF;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/sandbox/recovery.ts","../src/sandbox/diagnostics.ts","../src/sandbox/transient-failure.ts"],"sourcesContent":["/**\n * What an app does after the platform hands back a sandbox it cannot use.\n *\n * `replaceUnbringableBox` (see `./index`) decides whether to abandon a dead box.\n * This decides everything around that: which box key the next attempt uses,\n * whether the old box's snapshot is safe to restore from, whether an owner has\n * to confirm the loss first, and what the person waiting is told. It is\n * bookkeeping, not I/O — the app supplies storage through\n * {@link WorkspaceSandboxRecoveryStore}.\n *\n * ── Why the tables ──────────────────────────────────────────────────────────\n * Every fact about an action or a code lives in ACTIONS/CODES below, and the\n * types are DERIVED from those tables. That is deliberate, and it is the whole\n * reliability argument for this module.\n *\n * The hand-maintained alternative — a union type plus a separate `x === 'a' ||\n * x === 'b' || …` list per question — silently drops anything not on the list.\n * A recovery recorded under an unlisted action is written to storage, read\n * back, discarded as malformed, and the workspace re-provisions the box it just\n * abandoned. Nothing throws. The fix looks correct, ships, and does nothing.\n * That failure was hit twice inside one change before this module existed.\n *\n * With the tables, adding an action or code is a compile error until every\n * question about it is answered. There is no list to forget.\n */\n\n/** The box exists and still holds unsnapshotted state, so discarding it is an\n * owner's decision, not the runtime's. */\nexport const EGRESS_PROXY_RECOVERY_REQUIRED = 'EGRESS_PROXY_RECOVERY_REQUIRED'\nexport const EGRESS_PROXY_RECOVERY_PHASE = 'egress_proxy_recovery'\n/** The platform no longer has the box. Nothing to confirm, delete, or restore. */\nexport const WORKSPACE_SANDBOX_MISSING = 'WORKSPACE_SANDBOX_MISSING'\n/** The box exists but its host has no free slot, so it can never be resumed\n * where it is. A replacement can be placed on a host with room. */\nexport const WORKSPACE_SANDBOX_HOST_EXHAUSTED = 'WORKSPACE_SANDBOX_HOST_EXHAUSTED'\n/** The platform ran its own recovery, failed, and asked for a replacement.\n * Covers every way a box ends up unbringable with no more specific cause. */\nexport const WORKSPACE_SANDBOX_UNRECOVERABLE = 'WORKSPACE_SANDBOX_UNRECOVERABLE'\n\nexport const WORKSPACE_SANDBOX_SNAPSHOT_MAX_AGE_MS = 24 * 60 * 60 * 1000\n\n/**\n * Every recovery cause, and the one thing the runtime must know about each:\n * whether the box it names can still be read from.\n *\n * `snapshotUsable: false` is not a preference. A snapshot is addressed by the\n * sandbox it was taken from, so when that sandbox cannot be started the restore\n * fails exactly the way the resume did — and an app that tried it would turn a\n * recoverable workspace into a stuck one.\n */\nconst CODES = {\n [EGRESS_PROXY_RECOVERY_REQUIRED]: { snapshotUsable: true },\n [WORKSPACE_SANDBOX_MISSING]: { snapshotUsable: false },\n [WORKSPACE_SANDBOX_HOST_EXHAUSTED]: { snapshotUsable: false },\n [WORKSPACE_SANDBOX_UNRECOVERABLE]: { snapshotUsable: false },\n} as const\n\nexport type WorkspaceSandboxRecoveryCode = keyof typeof CODES\n\n/**\n * Every recovery action, and whether it means a replacement box has been\n * CHOSEN — the single question that decides which box key the next provisioning\n * attempt uses.\n *\n * `replacementChosen: false` covers the states where a key may be recorded but\n * must not be used yet: an owner has been asked and has not answered, or has\n * answered no. Handing back a key in those states replaces a box the owner\n * declined to lose.\n */\nconst ACTIONS = {\n confirmation_required: { replacementChosen: false, canDecide: true },\n deletion_declined: { replacementChosen: false, canDecide: true },\n replacement_authorized: { replacementChosen: false },\n snapshot_replacement_authorized: { replacementChosen: false },\n replacement_started: { replacementChosen: true, completedAs: 'replacement_completed' },\n replacement_completed: { replacementChosen: true },\n snapshot_replacement_started: { replacementChosen: true, completedAs: 'snapshot_replacement_completed' },\n snapshot_restore_failed: { replacementChosen: true, completedAs: 'snapshot_replacement_completed' },\n snapshot_replacement_completed: { replacementChosen: true },\n missing_replacement_started: { replacementChosen: true, completedAs: 'missing_replacement_completed' },\n missing_replacement_completed: { replacementChosen: true },\n unrecoverable_replacement_started: { replacementChosen: true, completedAs: 'unrecoverable_replacement_completed' },\n unrecoverable_replacement_completed: { replacementChosen: true },\n} as const\n\nexport type WorkspaceSandboxRecoveryAction = keyof typeof ACTIONS\n\nexport type WorkspaceSandboxSnapshotAvailability = 'available' | 'missing' | 'stale'\nexport type WorkspaceSandboxSnapshotFreshness = 'fresh' | 'stale' | 'unknown'\n\n/** A snapshot the app took of a box, addressed by the box it came from. */\nexport interface WorkspaceSandboxSnapshot {\n fromSandboxId: string\n createdAt: string\n [key: string]: unknown\n}\n\nexport interface WorkspaceSandboxSnapshotAssessment<Snapshot extends WorkspaceSandboxSnapshot = WorkspaceSandboxSnapshot> {\n availability: WorkspaceSandboxSnapshotAvailability\n freshness: WorkspaceSandboxSnapshotFreshness\n snapshot?: Snapshot\n}\n\nexport interface WorkspaceSandboxRecoveryState<Snapshot extends WorkspaceSandboxSnapshot = WorkspaceSandboxSnapshot> {\n code: WorkspaceSandboxRecoveryCode\n sandboxId: string\n detectedAt: string\n snapshot: WorkspaceSandboxSnapshotAssessment<Snapshot>\n action: WorkspaceSandboxRecoveryAction\n replacementBoxKey?: string\n replacementSandboxId?: string\n confirmedAt?: string\n}\n\nexport type WorkspaceSandboxRecoveryDecision = 'replace' | 'decline'\n\n/** Raised when chat cannot continue until an owner decides about the box. */\nexport class WorkspaceSandboxRecoveryRequiredError extends Error {\n readonly code = EGRESS_PROXY_RECOVERY_REQUIRED\n readonly status = 409\n readonly phase = EGRESS_PROXY_RECOVERY_PHASE\n readonly recovery: WorkspaceSandboxRecoveryState\n\n constructor(recovery: WorkspaceSandboxRecoveryState, cause: Error) {\n super(workspaceSandboxRecoveryMessage(recovery), { cause })\n this.name = 'WorkspaceSandboxRecoveryRequiredError'\n this.recovery = recovery\n }\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null\n}\n\nfunction asNonEmptyString(value: unknown): string | undefined {\n return typeof value === 'string' && value.trim().length > 0 ? value : undefined\n}\n\n/** Walk `cause` and `errors` breadth-first, cycle-safe. */\nfunction errorChain(error: unknown): unknown[] {\n const pending = [error]\n const visited = new Set<unknown>()\n const chain: unknown[] = []\n\n while (pending.length > 0) {\n const current = pending.shift()\n if (current === undefined || current === null || visited.has(current)) continue\n visited.add(current)\n chain.push(current)\n if (!isRecord(current)) continue\n if (current.cause !== undefined) pending.push(current.cause)\n if (Array.isArray(current.errors)) pending.push(...current.errors)\n }\n\n return chain\n}\n\nexport function isEgressProxyRecoveryRequiredError(error: unknown): boolean {\n return errorChain(error).some((current) =>\n isRecord(current) && current.code === EGRESS_PROXY_RECOVERY_REQUIRED,\n )\n}\n\nexport function isWorkspaceSandboxSnapshotRestoreError(error: unknown): boolean {\n return errorChain(error).some((current) => {\n const message = current instanceof Error\n ? current.message\n : isRecord(current) && typeof current.message === 'string'\n ? current.message\n : ''\n return /snapshot|fromSnapshot|restore/i.test(message)\n })\n}\n\n/**\n * Judge a snapshot against the box being replaced.\n *\n * Fresh means BOTH that it came from this exact sandbox and that it is inside\n * the age bound — a snapshot from a different box restores someone else's\n * filesystem, which is worse than starting empty.\n */\nexport function assessWorkspaceSandboxSnapshot<Snapshot extends WorkspaceSandboxSnapshot>(\n snapshot: Snapshot | undefined,\n sandboxId: string,\n now = Date.now(),\n): WorkspaceSandboxSnapshotAssessment<Snapshot> {\n if (!snapshot) return { availability: 'missing', freshness: 'unknown' }\n\n const createdAt = Date.parse(snapshot.createdAt)\n const ageMs = now - createdAt\n const isFresh = snapshot.fromSandboxId === sandboxId\n && Number.isFinite(createdAt)\n && ageMs >= 0\n && ageMs <= WORKSPACE_SANDBOX_SNAPSHOT_MAX_AGE_MS\n\n return isFresh\n ? { availability: 'available', freshness: 'fresh', snapshot }\n : { availability: 'stale', freshness: 'stale', snapshot }\n}\n\nfunction isSnapshotAssessment(value: unknown): value is WorkspaceSandboxSnapshotAssessment {\n if (!isRecord(value)) return false\n const availability = value.availability\n const freshness = value.freshness\n return (availability === 'available' || availability === 'missing' || availability === 'stale')\n && (freshness === 'fresh' || freshness === 'stale' || freshness === 'unknown')\n}\n\nexport function isWorkspaceSandboxRecoveryAction(\n value: unknown,\n): value is WorkspaceSandboxRecoveryAction {\n return typeof value === 'string' && Object.hasOwn(ACTIONS, value)\n}\n\nexport function isWorkspaceSandboxRecoveryCode(\n value: unknown,\n): value is WorkspaceSandboxRecoveryCode {\n return typeof value === 'string' && Object.hasOwn(CODES, value)\n}\n\nexport function isWorkspaceSandboxRecoveryState(\n value: unknown,\n): value is WorkspaceSandboxRecoveryState {\n if (!isRecord(value)) return false\n return isWorkspaceSandboxRecoveryCode(value.code)\n && !!asNonEmptyString(value.sandboxId)\n && !!asNonEmptyString(value.detectedAt)\n && isSnapshotAssessment(value.snapshot)\n && isWorkspaceSandboxRecoveryAction(value.action)\n}\n\nexport function workspaceSandboxRecoveryFromError(\n error: unknown,\n): WorkspaceSandboxRecoveryState | undefined {\n for (const current of errorChain(error)) {\n if (!isRecord(current) || !isWorkspaceSandboxRecoveryState(current.recovery)) continue\n return current.recovery\n }\n return undefined\n}\n\n/** What to tell the person waiting. Never names the product, so an app can\n * surface it verbatim. */\nexport function workspaceSandboxRecoveryMessage(recovery: WorkspaceSandboxRecoveryState): string {\n if (recovery.code === WORKSPACE_SANDBOX_MISSING) {\n 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.'\n }\n if (!CODES[recovery.code].snapshotUsable) {\n return 'The sandbox behind this workspace could not be started again. A replacement is being provisioned and your saved work is restored into it.'\n }\n if (recovery.action === 'deletion_declined') {\n return 'Sandbox recovery remains paused because replacement was declined. The stopped sandbox has not been deleted.'\n }\n if (recovery.action === 'confirmation_required') {\n const snapshotReason = recovery.snapshot.availability === 'missing'\n ? 'No restorable sandbox snapshot is available.'\n : 'The available sandbox snapshot is stale.'\n return `Sandbox recovery requires owner confirmation before replacement. ${snapshotReason} The stopped sandbox has not been deleted.`\n }\n if (recovery.action === 'snapshot_restore_failed') {\n return 'Sandbox replacement could not restore its verified snapshot, and an empty fallback was not created because it could lose workspace state.'\n }\n return 'Sandbox recovery is required before chat can continue. The stopped sandbox has not been deleted.'\n}\n\nexport function workspaceSandboxRecoveryRecommendedActions(\n recovery: WorkspaceSandboxRecoveryState,\n): string[] {\n if (!CODES[recovery.code].snapshotUsable) {\n return ['Retry after the replacement sandbox finishes provisioning.']\n }\n if (recovery.action === 'deletion_declined') {\n return ['Keep the stopped sandbox for support or ask a workspace owner to authorize replacement.']\n }\n if (recovery.action === 'confirmation_required') {\n return ['An owner can explicitly replace the sandbox after accepting loss of unsnapshotted sandbox-local changes.']\n }\n if (recovery.action === 'snapshot_restore_failed') {\n return ['Retry snapshot restoration or contact support. An empty replacement is not created automatically.']\n }\n return ['Retry after sandbox recovery completes.']\n}\n\n/** Flat key/value shape for a log line — no nesting, no secrets. */\nexport function workspaceSandboxRecoveryDiagnostic(\n recovery: WorkspaceSandboxRecoveryState,\n): Record<string, string | undefined> {\n return {\n sandboxId: recovery.sandboxId,\n recoveryCode: recovery.code,\n snapshotAvailability: recovery.snapshot.availability,\n snapshotFreshness: recovery.snapshot.freshness,\n selectedRecoveryAction: recovery.action,\n replacementSandboxId: recovery.replacementSandboxId,\n }\n}\n\n/**\n * The box key the next provisioning attempt should use, or undefined to keep\n * using the workspace's own key.\n *\n * Reads {@link ACTIONS} rather than a hand-kept list, because the failure mode\n * of a hand-kept list here is invisible: the key is recorded, silently ignored,\n * and provisioning goes back to the box the app just decided to abandon.\n */\nexport function preferredWorkspaceSandboxRecoveryBoxKey(\n recovery: WorkspaceSandboxRecoveryState | undefined,\n): string | undefined {\n if (!recovery?.replacementBoxKey) return undefined\n return ACTIONS[recovery.action].replacementChosen ? recovery.replacementBoxKey : undefined\n}\n\n/** Whether the replacement should be restored from the old box's snapshot. */\nexport function shouldRestoreWorkspaceSandboxRecovery(\n recovery: WorkspaceSandboxRecoveryState | undefined,\n): boolean {\n if (!recovery) return false\n if (!CODES[recovery.code].snapshotUsable) return false\n return !!preferredWorkspaceSandboxRecoveryBoxKey(recovery)\n && recovery.snapshot.availability === 'available'\n}\n\n/** One current recovery per workspace, stored by the application.\n * Concurrent writers should supply compareAndSet. Legacy write-only stores\n * remain supported but must serialize decisions and completion themselves. */\nexport interface WorkspaceSandboxRecoveryStore<Snapshot extends WorkspaceSandboxSnapshot = WorkspaceSandboxSnapshot> {\n read: (workspaceId: string) => Promise<WorkspaceSandboxRecoveryState<Snapshot> | undefined>\n write: (workspaceId: string, recovery: WorkspaceSandboxRecoveryState<Snapshot>) => Promise<void>\n /** Commit a transition only while the exact recovery read above remains current. */\n compareAndSet?: (\n workspaceId: string,\n expected: WorkspaceSandboxRecoveryState<Snapshot>,\n recovery: WorkspaceSandboxRecoveryState<Snapshot>,\n ) => Promise<boolean>\n}\n\nexport interface WorkspaceSandboxRecoveryManager<Snapshot extends WorkspaceSandboxSnapshot = WorkspaceSandboxSnapshot> {\n read: WorkspaceSandboxRecoveryStore<Snapshot>['read']\n record: WorkspaceSandboxRecoveryStore<Snapshot>['write']\n /** Only a pending/declined recovery for this sandbox accepts a new decision.\n * Replacement requires a fresh, nonblank key; decline needs no key. */\n decide: (args: {\n workspaceId: string\n sandboxId: string\n } & (\n | { decision: 'replace'; replacementBoxKey: string }\n | { decision: 'decline'; replacementBoxKey?: string }\n )) => Promise<WorkspaceSandboxRecoveryState<Snapshot> | undefined>\n /** Only a started replacement with a chosen key may be completed. */\n complete: (args: {\n workspaceId: string\n replacementSandboxId: string\n /** The key used by this provisioning attempt, not a freshly read successor. */\n replacementBoxKey?: string\n }) => Promise<WorkspaceSandboxRecoveryState<Snapshot> | undefined>\n}\n\n/** Bind canonical recovery policy to the application's existing storage. */\nexport function createWorkspaceSandboxRecoveryManager<Snapshot extends WorkspaceSandboxSnapshot>(\n store: WorkspaceSandboxRecoveryStore<Snapshot>,\n): WorkspaceSandboxRecoveryManager<Snapshot> {\n type State = WorkspaceSandboxRecoveryState<Snapshot>\n async function transition(workspaceId: string, expected: State, next: State): Promise<State> {\n if (store.compareAndSet) {\n if (!await store.compareAndSet(workspaceId, expected, next))\n throw new Error('Sandbox recovery changed; reload before retrying its transition')\n } else {\n await store.write(workspaceId, next)\n }\n return next\n }\n\n return {\n read: workspaceId => store.read(workspaceId),\n record: (workspaceId, recovery) => store.write(workspaceId, recovery),\n async decide({ workspaceId, sandboxId, decision, replacementBoxKey }) {\n const current = await store.read(workspaceId)\n if (!current || current.sandboxId !== sandboxId || !('canDecide' in ACTIONS[current.action])) return undefined\n if (decision === 'replace' && !asNonEmptyString(replacementBoxKey))\n throw new Error('Sandbox replacement authorization requires a fresh replacement box key')\n return transition(workspaceId, current, {\n ...current,\n action: decision === 'replace'\n ? (current.snapshot.availability === 'available' ? 'snapshot_replacement_authorized' : 'replacement_authorized')\n : 'deletion_declined',\n confirmedAt: new Date().toISOString(),\n ...(decision === 'replace' ? { replacementBoxKey } : {}),\n })\n },\n async complete({ workspaceId, replacementSandboxId, replacementBoxKey }) {\n const current = await store.read(workspaceId)\n if (!current || !asNonEmptyString(current.replacementBoxKey)) return undefined\n if (replacementBoxKey !== undefined && current.replacementBoxKey !== replacementBoxKey) return undefined\n const policy = ACTIONS[current.action]\n if (!('completedAs' in policy)) return undefined\n if (!asNonEmptyString(replacementSandboxId)) throw new Error('A replacement sandbox id is required')\n return transition(workspaceId, current, { ...current, action: policy.completedAs, replacementSandboxId })\n },\n }\n}\n\n/** Every declared action, for exhaustiveness tests in apps and here. */\nexport const WORKSPACE_SANDBOX_RECOVERY_ACTIONS = Object.keys(\n ACTIONS,\n) as readonly WorkspaceSandboxRecoveryAction[]\n\n/** Every declared cause. */\nexport const WORKSPACE_SANDBOX_RECOVERY_CODES = Object.keys(\n CODES,\n) as readonly WorkspaceSandboxRecoveryCode[]\n","/**\n * Read a sandbox provisioning failure without leaking what it carries.\n *\n * Provisioning errors arrive as deep `cause` chains from the sandbox API, the\n * runtime sidecar, and the app's own vault code, and they routinely carry\n * bearer tokens and signed URLs in their messages. Every app on this shell\n * needs the same three things from one: a redacted shape it can log, a\n * classification it can act on, and a sentence it can show a person. Before\n * this module each app either wrote its own or — far more often — wrote none,\n * and surfaced the raw error or a generic apology.\n *\n * The classifiers are deliberately narrow. `isSandboxApiSandboxMissingFailure`\n * does not fire on a 404 from inside a live box; `isSandboxHostCapacityFailure`\n * does not fire on capacity wording from any origin but the sandbox API. A\n * classifier that over-matches turns a transient failure into a box deletion.\n */\nimport { EGRESS_PROXY_RECOVERY_REQUIRED } from './recovery'\n\nconst SAFE_ERROR_FIELDS = [\n 'code',\n 'status',\n 'phase',\n 'endpoint',\n 'origin',\n 'retryAfterMs',\n 'sidecarVersion',\n 'containerImage',\n] as const\n\nexport interface SafeSandboxErrorCause {\n name?: string\n message?: string\n code?: string | number\n status?: string | number\n phase?: string\n endpoint?: string\n origin?: string\n retryAfterMs?: number\n sidecarVersion?: string\n containerImage?: string\n}\n\nexport interface SafeSandboxErrorDiagnostics {\n message: string\n causes: SafeSandboxErrorCause[]\n truncated: boolean\n truncatedAtDepth?: number\n cycle: boolean\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null\n}\n\nfunction asSafeScalar(value: unknown): string | number | undefined {\n if (typeof value === 'string' && value.length > 0) return redactDiagnosticText(value)\n if (typeof value === 'number' && Number.isFinite(value)) return value\n return undefined\n}\n\nfunction redactDiagnosticText(value: string): string {\n const secretKeyPattern = '(?:(?:[A-Z0-9]+_)*(?:api_?key|auth_?token|access_?token|refresh_?token|key|token|password|secret|credential)|apiKey|authToken|accessToken|refreshToken)'\n return value\n .replace(/(['\"]authorization['\"]\\s*:\\s*)(['\"])[^'\"]*\\2/gi, '$1$2[REDACTED]$2')\n .replace(/\\b(authorization\\s*[:=]\\s*)(['\"])[^'\"]*\\2/gi, '$1$2[REDACTED]$2')\n .replace(/\\b(authorization\\s*[:=]\\s*)Digest\\b[^;}\\n]*(?:\\r?\\n[ \\t]+[^;}\\n]*)*/gi, '$1[REDACTED]')\n .replace(/\\b(authorization\\s*[:=]\\s*)(?:Basic|Bearer|Negotiate)\\s+[^\\s;,&}\\n]+(?:\\r?\\n[ \\t]+[^\\s;,&}\\n]+)*/gi, '$1[REDACTED]')\n .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: string, _scheme: string, valuePart: string) => {\n if (valuePart.includes(';') || /\\r?\\n[ \\t]+/.test(valuePart)) return `${prefix}[REDACTED]`\n const tokenMatch = valuePart.match(/^\\S+(.*)$/)\n return `${prefix}[REDACTED]${tokenMatch?.[1] ?? ''}`\n })\n .replace(/\\b(authorization\\s*[:=]\\s*)[A-Za-z][\\w.-]+\\s+[^\\s;,&}\\n]+(?=[;&}\\n]|$)/gi, '$1[REDACTED]')\n .replace(/\\b(authorization\\s*[:=]\\s*)(?!\\[REDACTED\\])[^'\"\\s&}]+(?:[;,][^'\"\\s&}]+)*/gi, '$1[REDACTED]')\n .replace(/\\bBearer\\s+[A-Za-z0-9._~+/-]+=*/gi, 'Bearer [REDACTED]')\n .replace(/\\bsk-[A-Za-z0-9_-]{6,}\\b/g, 'sk-[REDACTED]')\n .replace(\n /([?&][^=&#\\s]*(?:token|key|secret|password|authorization)[^=&#\\s]*=)[^&#\\s]+/gi,\n '$1[REDACTED]',\n )\n .replace(\n new RegExp(`(['\"])(${secretKeyPattern})\\\\1\\\\s*:\\\\s*(['\"])[^'\"]+\\\\3`, 'gi'),\n '$1$2$1:$3[REDACTED]$3',\n )\n .replace(\n new RegExp(`\\\\b(${secretKeyPattern})\\\\s*([:=])(\\\\s*)(['\"]?)(?!Bearer\\\\b)[^'\"\\\\s;,&}]+(?:[;,]\\\\s*[^'\"\\\\s;,&}]+)*`, 'gi'),\n '$1$2$3$4[REDACTED]',\n )\n}\n\nfunction readMessage(value: unknown): string | undefined {\n if (value instanceof Error) return redactDiagnosticText(value.message)\n if (typeof value === 'string' && value.length > 0) return redactDiagnosticText(value)\n if (typeof value === 'number' && Number.isFinite(value)) return String(value)\n if (typeof value === 'boolean') return String(value)\n if (!isRecord(value)) return undefined\n const message = value.message\n return typeof message === 'string' && message.length > 0 ? redactDiagnosticText(message) : undefined\n}\n\nfunction readName(value: unknown): string | undefined {\n if (value instanceof Error) return redactDiagnosticText(value.name)\n if (!isRecord(value)) return undefined\n const name = value.name\n return typeof name === 'string' && name.length > 0 ? redactDiagnosticText(name) : undefined\n}\n\nfunction readCause(value: unknown): unknown {\n if (!isRecord(value)) return undefined\n return value.cause\n}\n\nexport function serializeSandboxProvisioningError(\n error: unknown,\n options: { maxDepth?: number } = {},\n): SafeSandboxErrorDiagnostics {\n const maxDepth = options.maxDepth ?? 6\n const causes: SafeSandboxErrorCause[] = []\n const seen = new Set<unknown>()\n let truncatedAtDepth: number | undefined\n let cycle = false\n\n let current: unknown = error\n let depth = 0\n for (; depth < maxDepth && current !== undefined && current !== null; depth += 1) {\n if (seen.has(current)) {\n cycle = true\n causes.push({\n name: 'CauseChainCycle',\n message: `cause chain cycle detected after ${depth} entries`,\n })\n break\n }\n if (isRecord(current)) seen.add(current)\n\n const cause: SafeSandboxErrorCause = {}\n const name = readName(current)\n const message = readMessage(current)\n if (name) cause.name = name\n if (message) cause.message = message\n\n if (isRecord(current)) {\n for (const field of SAFE_ERROR_FIELDS) {\n const safeValue = asSafeScalar(current[field])\n if (safeValue !== undefined) {\n Object.assign(cause, { [field]: safeValue })\n }\n }\n }\n\n if (Object.keys(cause).length > 0) causes.push(cause)\n current = readCause(current)\n }\n\n if (!cycle && isRecord(current) && seen.has(current)) {\n cycle = true\n causes.push({\n name: 'CauseChainCycle',\n message: `cause chain cycle detected after ${depth} entries`,\n })\n } else if (current !== undefined && current !== null && depth >= maxDepth) {\n truncatedAtDepth = maxDepth\n causes.push({\n name: 'CauseChainTruncated',\n message: `cause chain truncated after ${maxDepth} entries`,\n })\n }\n\n return {\n message: readMessage(error) ?? 'Sandbox unavailable',\n causes,\n truncated: truncatedAtDepth !== undefined,\n truncatedAtDepth,\n cycle,\n }\n}\n\nexport function formatSandboxProvisioningSupportDetails(\n diagnostics: SafeSandboxErrorDiagnostics,\n): string {\n const actionableCause = diagnostics.causes.find((cause) =>\n cause.code !== undefined\n || cause.status !== undefined\n || cause.phase !== undefined\n || cause.endpoint !== undefined\n || cause.origin !== undefined\n || cause.retryAfterMs !== undefined\n || cause.sidecarVersion !== undefined\n || cause.containerImage !== undefined,\n ) ?? diagnostics.causes[1]\n\n if (!actionableCause) return 'Support details: no nested sandbox cause details were available.'\n\n const details = [\n typeof actionableCause.name === 'string' ? redactDiagnosticText(actionableCause.name) : undefined,\n actionableCause.code !== undefined ? `code=${actionableCause.code}` : undefined,\n actionableCause.status !== undefined ? `status=${actionableCause.status}` : undefined,\n actionableCause.phase !== undefined ? `phase=${redactDiagnosticText(actionableCause.phase)}` : undefined,\n actionableCause.endpoint !== undefined ? `endpoint=${redactDiagnosticText(actionableCause.endpoint)}` : undefined,\n actionableCause.origin !== undefined ? `origin=${redactDiagnosticText(actionableCause.origin)}` : undefined,\n actionableCause.retryAfterMs !== undefined ? `retryAfterMs=${actionableCause.retryAfterMs}` : undefined,\n actionableCause.sidecarVersion !== undefined ? `sidecarVersion=${redactDiagnosticText(actionableCause.sidecarVersion)}` : undefined,\n actionableCause.containerImage !== undefined ? `containerImage=${redactDiagnosticText(actionableCause.containerImage)}` : undefined,\n typeof actionableCause.message === 'string' ? redactDiagnosticText(actionableCause.message) : undefined,\n ].filter(Boolean)\n\n if (details.length === 0) return 'Support details: no nested sandbox cause details were available.'\n return `Support details: ${details.join('; ')}`\n}\n\nexport function isSandboxAuthFailure(diagnostics: SafeSandboxErrorDiagnostics): boolean {\n return diagnostics.causes.some((cause) => {\n const code = typeof cause.code === 'string' ? cause.code.toUpperCase() : undefined\n const status = typeof cause.status === 'number'\n ? cause.status\n : typeof cause.status === 'string'\n ? Number.parseInt(cause.status, 10)\n : undefined\n const name = typeof cause.name === 'string' ? cause.name.toLowerCase() : ''\n const message = typeof cause.message === 'string' ? cause.message.toLowerCase() : ''\n\n return code === 'AUTH_ERROR'\n || status === 401\n || name.includes('autherror')\n || message.includes('missing or invalid authentication')\n })\n}\n\n/**\n * True when the Sandbox API refused the caller's bearer: a 401 from any\n * Sandbox API route, including list and query routes such as\n * `/v1/sandboxes?status=stopped`. The caller should invalidate its cached key\n * and mint a new one once.\n *\n * Runtime routes (`/v1/sandboxes/{id}/runtime/...`) are excluded: the box's\n * runtime authentication answers those, and a new Sandbox API key does not fix\n * it. `isSandboxAuthFailure` covers them.\n */\nexport function isSandboxApiBearerAuthFailure(diagnostics: SafeSandboxErrorDiagnostics): boolean {\n return diagnostics.causes.some((cause) => {\n const status = typeof cause.status === 'number'\n ? cause.status\n : typeof cause.status === 'string'\n ? Number.parseInt(cause.status, 10)\n : undefined\n if (status !== 401) return false\n if (cause.origin !== 'sandbox-api') return false\n if (typeof cause.endpoint !== 'string') return false\n const endpointPath = sandboxApiEndpointPath(cause.endpoint)\n if (!endpointPath || !/^\\/v1(?:[/?#]|$)/.test(endpointPath)) return false\n return !/^\\/v1\\/sandboxes\\/[^/?#]+\\/runtime(?:[/?#]|$)/.test(endpointPath)\n })\n}\n\n/** Machine-readable code emitted when resume lost the physical box. */\nexport const SANDBOX_BACKING_CONTAINER_MISSING_CODE = 'BACKING_CONTAINER_MISSING' as const\n\n/**\n * Legacy fallback for Sandbox API releases that only returned the nested host\n * 404 text. New callers must use `SANDBOX_BACKING_CONTAINER_MISSING_CODE`.\n */\nexport function isLegacySandboxBackingContainerMissingMessage(message: string): boolean {\n return /host-agent startcontainer failed \\(404\\):/i.test(message)\n && /container not found/i.test(message)\n && /\"code\"\\s*:\\s*\"not_found\"/i.test(message)\n}\n\n/**\n * True when the sandbox API cannot find a sandbox resource or its backing\n * container. The latter arrives as a 500 from resume with a nested host 404.\n *\n * A sandbox id is a cache of where a workspace's box lives, not the workspace's\n * identity: the platform reaps, suspends, and loses boxes as ordinary lifecycle\n * events. Callers use this to discard the dead id and provision a replacement,\n * so the match is deliberately narrow — a 404 from the runtime sidecar\n * (`/runtime/...`, a missing file or session inside a live box) is NOT this.\n */\nexport function isSandboxApiSandboxMissingFailure(diagnostics: SafeSandboxErrorDiagnostics): boolean {\n return diagnostics.causes.some((cause) => {\n const status = typeof cause.status === 'number'\n ? cause.status\n : typeof cause.status === 'string'\n ? Number.parseInt(cause.status, 10)\n : undefined\n if (cause.origin !== 'sandbox-api') return false\n if (typeof cause.endpoint !== 'string') return false\n const endpointPath = sandboxApiEndpointPath(cause.endpoint)\n if (!endpointPath) return false\n const sandboxResource = /^\\/v1\\/sandboxes\\/[^/?#]+(?:\\/(?!runtime(?:[/?#]|$))[^?#]*)?(?:[?#].*)?$/.test(endpointPath)\n if (!sandboxResource) return false\n if (status === 404) return true\n\n if (status !== 500 || !/^\\/v1\\/sandboxes\\/[^/?#]+\\/resume(?:[?#].*)?$/.test(endpointPath)) {\n return false\n }\n const code = typeof cause.code === 'string' ? cause.code.toUpperCase() : undefined\n if (code === SANDBOX_BACKING_CONTAINER_MISSING_CODE) return true\n // Older Sandbox API releases returned only the nested host 404. The SDK\n // represented that response as SERVER_ERROR, so keep this exact fallback\n // until those releases are no longer in service.\n if (code !== undefined && code !== 'SERVER_ERROR') return false\n const message = typeof cause.message === 'string' ? cause.message : ''\n return isLegacySandboxBackingContainerMissingMessage(message)\n })\n}\n\n/**\n * True when a resume failed because the host the box is pinned to cannot seat\n * it — the host's slot budget is exhausted, not the box's fault and not\n * something waiting fixes.\n *\n * A box lives on one host. When that host fills, every future resume for every\n * box on it fails identically and permanently, so a workspace whose box landed\n * on a full host is bricked until it is placed somewhere else. Callers use this\n * the same way they use {@link isSandboxApiSandboxMissingFailure}: discard the\n * dead id and provision a replacement, which the orchestrator is free to place\n * on a host with room. The workspace itself is preserved — it lives in the\n * Vault, not in the box's filesystem.\n *\n * Matched on the message because the sandbox API returns a generic\n * `SERVER_ERROR` for it; a dedicated code upstream would replace this.\n */\nexport function isSandboxHostCapacityFailure(diagnostics: SafeSandboxErrorDiagnostics): boolean {\n return diagnostics.causes.some((cause) => {\n if (cause.origin !== 'sandbox-api') return false\n if (typeof cause.message !== 'string') return false\n return /host has no available slot|host capacity reservation failed/i.test(cause.message)\n })\n}\n\n/**\n * True when a resume failed on the box's own configuration rather than on\n * anything a retry can change.\n *\n * These are permanent facts about one box. Its egress policy can be missing,\n * or its verified Platform lineage can differ from the current caller. Neither\n * creation-time fact can change on resume, so the box must be replaced.\n *\n * Narrow on purpose. A bare 500 from the sandbox API is transient far more\n * often than not, and treating one as unbringable would delete a healthy box.\n * Matched on the specific unrecoverable phrasing, and only from `sandbox-api`.\n */\nexport function isSandboxBoxConfigFailure(diagnostics: SafeSandboxErrorDiagnostics): boolean {\n return diagnostics.causes.some((cause) => {\n if (cause.origin !== 'sandbox-api') return false\n if (cause.code === 'SANDBOX_ATTRIBUTION_MISMATCH') return true\n if (typeof cause.message !== 'string') return false\n return /has no recorded egress policy|cannot rebuild the proxy config/i.test(cause.message)\n })\n}\n\n/**\n * True when the Sandbox API answered a control-plane call with a server error\n * (500, 502, 503 or 504), such as `A sandbox backend did not answer` or\n * `Failed to resume project: fetch failed`. These clear within seconds, so a\n * caller may retry once before surfacing them.\n *\n * Runtime routes are excluded (the box answers those), and so are the narrow\n * failures that need a different action: a missing box, a box whose creation\n * facts no longer hold, and host capacity.\n */\nexport function isSandboxApiTransientFailure(diagnostics: SafeSandboxErrorDiagnostics): boolean {\n if (isSandboxApiSandboxMissingFailure(diagnostics)\n || isSandboxBoxConfigFailure(diagnostics)\n || isSandboxHostCapacityFailure(diagnostics)) return false\n return diagnostics.causes.some((cause) => {\n const status = typeof cause.status === 'number'\n ? cause.status\n : typeof cause.status === 'string'\n ? Number.parseInt(cause.status, 10)\n : undefined\n if (status !== 500 && status !== 502 && status !== 503 && status !== 504) return false\n if (cause.origin !== 'sandbox-api' || typeof cause.endpoint !== 'string') return false\n const endpointPath = sandboxApiEndpointPath(cause.endpoint)\n if (!endpointPath || !/^\\/v1(?:[/?#]|$)/.test(endpointPath)) return false\n return !/^\\/v1\\/sandboxes\\/[^/?#]+\\/runtime(?:[/?#]|$)/.test(endpointPath)\n })\n}\n\nfunction sandboxApiEndpointPath(endpoint: string): string | null {\n if (endpoint.startsWith('/')) return endpoint\n try {\n const url = new URL(endpoint)\n return `${url.pathname}${url.search}${url.hash}`\n } catch {\n return null\n }\n}\n\nexport function formatSandboxProvisioningUserMessage(\n diagnostics: SafeSandboxErrorDiagnostics,\n): string {\n if (diagnostics.causes.some((cause) => cause.code === EGRESS_PROXY_RECOVERY_REQUIRED)) {\n return 'Sandbox recovery is required before chat can continue. The stopped sandbox has not been deleted.'\n }\n\n // The sandbox itself is reachable — copying the Vault into it is what did not\n // finish. Saying \"the service is unavailable\" sends people to inspect\n // infrastructure that is healthy.\n if (diagnostics.causes.some((cause) => cause.code === 'vault.hydration_incomplete')) {\n 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 — try again in a moment.'\n }\n\n if (isSandboxApiTransientFailure(diagnostics)) {\n return 'The sandbox service didn\\'t answer just now. Send your message again in a moment.'\n }\n\n if (isSandboxApiBearerAuthFailure(diagnostics)) {\n 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.'\n }\n\n if (isSandboxAuthFailure(diagnostics)) {\n 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.'\n }\n\n if (diagnostics.causes.some((cause) => (\n cause.code === 'PAYLOAD_TOO_LARGE'\n || cause.code === 'FILE_TOO_LARGE'\n || cause.status === 413\n || cause.status === '413'\n ))) {\n // Profile and workspace files reach the box through writeProfileFilesToBox;\n // naming an attachment there sends people looking for one they never sent.\n if (diagnostics.causes.some((cause) => cause.message?.startsWith('writeProfileFilesToBox'))) {\n return 'A workspace file is too large to copy into the sandbox. The support details name the file.'\n }\n return 'An attachment is too large for the sandbox to accept. Use a smaller file and try again.'\n }\n\n // Matched by code, not bare 429: the sidecar's rate limiters also answer\n // 429, and those are not attachment-staging pressure.\n if (diagnostics.causes.some((cause) => cause.code === 'UPLOAD_BUDGET_EXHAUSTED')) {\n return 'Too much attachment data is staged in the sandbox at once. Retry shortly.'\n }\n\n 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.'\n}\n","/**\n * Sandbox failures that clear on their own, and how long to wait them out.\n *\n * Both callers that meet them use this one policy: the run dispatch, which\n * resends the same execution, and the completion Workflow, which observes it.\n * A box restarting under load answers \"filesystem incarnation is not ready\"\n * for minutes (8 minutes for GTM's busiest box on 2026-10-10, 10:37 to 10:45\n * UTC), and its gateway refuses dispatches with 502 or 503 while it does.\n * Failing on the first such answer lost those turns; retrying forever would\n * hold a lock on a box that never comes back. The policy backs off from 5 s\n * to a 60 s ceiling and gives up after 15 minutes.\n */\nimport { isSandboxApiTransientFailure, serializeSandboxProvisioningError } from './diagnostics'\n\nexport type SandboxTransientFailureCode =\n /** The box is running but its filesystem incarnation is not ready yet. */\n | 'sandbox.filesystem_not_ready'\n /** The sandbox gateway answered the run dispatch with 502, 503 or 504. */\n | 'sandbox.dispatch_refused'\n /** A Sandbox API control-plane call failed with a server error. */\n | 'sandbox.control_plane_transient'\n\nexport interface SandboxTransientFailure {\n code: SandboxTransientFailureCode\n message: string\n}\n\n/** How long a transient failure may last before the caller fails. */\nexport const SANDBOX_TRANSIENT_DEADLINE_MS = 15 * 60_000\n\n/** Wait before retry `attempt` (1-based): 5 s doubling to a 60 s ceiling. */\nexport function sandboxTransientBackoffMs(attempt: number): number {\n return Math.min(60_000, 5_000 * 2 ** Math.max(0, attempt - 1))\n}\n\nfunction record(value: unknown): Record<string, unknown> | undefined {\n return value !== null && typeof value === 'object' ? value as Record<string, unknown> : undefined\n}\n\nfunction causeChain(error: unknown): Record<string, unknown>[] {\n const chain: Record<string, unknown>[] = []\n let current = record(error)\n for (let depth = 0; current && depth < 8 && !chain.includes(current); depth += 1) {\n chain.push(current)\n current = record(current.cause)\n }\n return chain\n}\n\n/** A planned host-agent swap leaves the sandbox alive; an ordinary probe failure is not that evidence. */\nexport function plannedHostRestart(error: unknown): boolean {\n return causeChain(error).some(cause => cause.code === 'HOST_AGENT_RESTARTING' && cause.status === 503)\n}\n\n/** Wait only for the platform typed planned-restart signal, within the caller budget. */\nexport async function readDuringPlannedHostRestart<T>(\n read: () => Promise<T>,\n deadline: number,\n): Promise<{ value: T; waited: boolean }> {\n let waited = false\n for (;;) {\n try {\n return { value: await read(), waited }\n } catch (error) {\n if (!plannedHostRestart(error)) throw error\n const hint = (error as { retryAfterMs?: unknown }).retryAfterMs\n const delay = typeof hint === 'number' && Number.isFinite(hint) && hint > 0\n ? hint : sandboxTransientBackoffMs(1)\n // The existing caller budget bounds retries. Each SDK request\n // settles under its own deadline before another begins. Preserve the refusal and never start a stop on\n // expiry; its code, origin and endpoint remain available to the caller.\n if (Date.now() + delay >= deadline) throw error\n waited = true\n await new Promise(resolve => setTimeout(resolve, delay))\n }\n }\n}\n\n\nfunction status(value: unknown): number | undefined {\n if (typeof value === 'number') return value\n if (typeof value === 'string' && /^\\d{3}$/.test(value)) return Number(value)\n return undefined\n}\n\nconst FILESYSTEM_NOT_READY = /filesystem incarnation is not ready/i\nconst RUN_DISPATCH_ENDPOINT = /\\/runtime\\/agents\\/run\\/stream(?:[?#]|$)/\nconst RUN_DISPATCH_REFUSAL_MESSAGE = /\\/runtime\\/agents\\/run\\/stream: error code: (50[234])\\b/\n\n/** The typed transient failure `error` carries anywhere in its cause chain, or null. */\nexport function classifySandboxTransientFailure(error: unknown): SandboxTransientFailure | null {\n for (const cause of causeChain(error)) {\n const message = typeof cause.message === 'string' ? cause.message : ''\n if (cause.code === 'FILESYSTEM_INCARNATION_NOT_READY' || FILESYSTEM_NOT_READY.test(message)) {\n return { code: 'sandbox.filesystem_not_ready', message: 'Sandbox filesystem incarnation is not ready' }\n }\n const code = status(cause.status)\n const endpoint = typeof cause.endpoint === 'string' ? cause.endpoint : ''\n const refusal = (code === 502 || code === 503 || code === 504) && RUN_DISPATCH_ENDPOINT.test(endpoint)\n ? code\n : RUN_DISPATCH_REFUSAL_MESSAGE.exec(message)?.[1]\n if (refusal) {\n return { code: 'sandbox.dispatch_refused', message: `The sandbox gateway refused the run dispatch with ${refusal}` }\n }\n }\n if (isSandboxApiTransientFailure(serializeSandboxProvisioningError(error))) {\n return { code: 'sandbox.control_plane_transient', message: 'A Sandbox API call failed with a server error' }\n }\n return null\n}\n\n/** The error a caller raises when a transient failure outlasted the deadline. */\nexport class SandboxTransientDeadlineError extends Error {\n readonly code: SandboxTransientFailureCode\n readonly firstSeenAt: number\n readonly attempts: number\n\n constructor(failure: SandboxTransientFailure, firstSeenAt: number, attempts: number, cause?: unknown) {\n const minutes = Math.round(SANDBOX_TRANSIENT_DEADLINE_MS / 60_000)\n super(`${failure.message}; it persisted for more than ${minutes} minutes over ${attempts} attempts`, cause === undefined ? undefined : { cause })\n this.name = 'SandboxTransientDeadlineError'\n this.code = failure.code\n this.firstSeenAt = firstSeenAt\n this.attempts = attempts\n }\n}\n\nexport interface SandboxTransientRetryOptions {\n /** Defaults to {@link SANDBOX_TRANSIENT_DEADLINE_MS}. */\n deadlineMs?: number\n now?: () => number\n sleep?: (ms: number, signal?: AbortSignal) => Promise<void>\n signal?: AbortSignal\n onRetry?: (failure: SandboxTransientFailure, attempt: number, delayMs: number) => void\n}\n\nconst defaultSleep = (ms: number, signal?: AbortSignal) => new Promise<void>((resolve, reject) => {\n if (signal?.aborted) return reject(signal.reason)\n const timer = setTimeout(resolve, ms)\n signal?.addEventListener('abort', () => {\n clearTimeout(timer)\n reject(signal.reason)\n }, { once: true })\n})\n\n/**\n * Run `attempt` until it succeeds, it fails with something this policy does\n * not consider transient, or a transient failure outlasts the deadline.\n */\nexport async function retrySandboxTransient<T>(\n attempt: () => Promise<T>,\n options: SandboxTransientRetryOptions = {},\n): Promise<T> {\n const now = options.now ?? Date.now\n const sleep = options.sleep ?? defaultSleep\n const deadlineMs = options.deadlineMs ?? SANDBOX_TRANSIENT_DEADLINE_MS\n let firstSeenAt: number | undefined\n for (let attempts = 1; ; attempts += 1) {\n try {\n return await attempt()\n } catch (error) {\n const failure = classifySandboxTransientFailure(error)\n if (!failure) throw error\n const at = now()\n firstSeenAt ??= at\n const delayMs = sandboxTransientBackoffMs(attempts)\n if (at + delayMs - firstSeenAt > deadlineMs) throw new SandboxTransientDeadlineError(failure, firstSeenAt, attempts, error)\n options.onRetry?.(failure, attempts, delayMs)\n await sleep(delayMs, options.signal)\n }\n }\n}\n"],"mappings":";AA4BO,IAAM,iCAAiC;AACvC,IAAM,8BAA8B;AAEpC,IAAM,4BAA4B;AAGlC,IAAM,mCAAmC;AAGzC,IAAM,kCAAkC;AAExC,IAAM,wCAAwC,KAAK,KAAK,KAAK;AAWpE,IAAM,QAAQ;AAAA,EACZ,CAAC,8BAA8B,GAAG,EAAE,gBAAgB,KAAK;AAAA,EACzD,CAAC,yBAAyB,GAAG,EAAE,gBAAgB,MAAM;AAAA,EACrD,CAAC,gCAAgC,GAAG,EAAE,gBAAgB,MAAM;AAAA,EAC5D,CAAC,+BAA+B,GAAG,EAAE,gBAAgB,MAAM;AAC7D;AAcA,IAAM,UAAU;AAAA,EACd,uBAAuB,EAAE,mBAAmB,OAAO,WAAW,KAAK;AAAA,EACnE,mBAAmB,EAAE,mBAAmB,OAAO,WAAW,KAAK;AAAA,EAC/D,wBAAwB,EAAE,mBAAmB,MAAM;AAAA,EACnD,iCAAiC,EAAE,mBAAmB,MAAM;AAAA,EAC5D,qBAAqB,EAAE,mBAAmB,MAAM,aAAa,wBAAwB;AAAA,EACrF,uBAAuB,EAAE,mBAAmB,KAAK;AAAA,EACjD,8BAA8B,EAAE,mBAAmB,MAAM,aAAa,iCAAiC;AAAA,EACvG,yBAAyB,EAAE,mBAAmB,MAAM,aAAa,iCAAiC;AAAA,EAClG,gCAAgC,EAAE,mBAAmB,KAAK;AAAA,EAC1D,6BAA6B,EAAE,mBAAmB,MAAM,aAAa,gCAAgC;AAAA,EACrG,+BAA+B,EAAE,mBAAmB,KAAK;AAAA,EACzD,mCAAmC,EAAE,mBAAmB,MAAM,aAAa,sCAAsC;AAAA,EACjH,qCAAqC,EAAE,mBAAmB,KAAK;AACjE;AAkCO,IAAM,wCAAN,cAAoD,MAAM;AAAA,EACtD,OAAO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ACpB,SACA,UAAwC,CAAC,GAC7B;AACZ,QAAM,MAAM,QAAQ,OAAO,KAAK;AAChC,QAAM,QAAQ,QAAQ,SAAS;AAC/B,QAAM,aAAa,QAAQ,cAAc;AACzC,MAAI;AACJ,WAAS,WAAW,KAAK,YAAY,GAAG;AACtC,QAAI;AACF,aAAO,MAAM,QAAQ;AAAA,IACvB,SAAS,OAAO;AACd,YAAM,UAAU,gCAAgC,KAAK;AACrD,UAAI,CAAC,QAAS,OAAM;AACpB,YAAM,KAAK,IAAI;AACf,sBAAgB;AAChB,YAAM,UAAU,0BAA0B,QAAQ;AAClD,UAAI,KAAK,UAAU,cAAc,WAAY,OAAM,IAAI,8BAA8B,SAAS,aAAa,UAAU,KAAK;AAC1H,cAAQ,UAAU,SAAS,UAAU,OAAO;AAC5C,YAAM,MAAM,SAAS,QAAQ,MAAM;AAAA,IACrC;AAAA,EACF;AACF;","names":["isRecord","status"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/launch-invariants/schedule.ts","../src/launch-invariants/limit-alarms.ts","../src/launch-invariants/pasted-code.ts"],"sourcesContent":["/**\n * Scheduled jobs that cannot take each other down.\n *\n * On 2026-10-10 at 21:00Z GTM's turn health ran out of memory while the hourly\n * sweeps shared one scheduled run, and none of the eight queued after it ran.\n * Here each scheduled invocation runs exactly one job. A cron can carry a\n * minute list (`0,1,2 * * * *`) with one job per minute, so a Worker keeps a\n * few trigger expressions while every job gets its own invocation, its own\n * memory and its own failure.\n *\n * Cloudflare hands the handler the configured string verbatim as `event.cron`,\n * so dispatch is exact-string; a tick no entry handles is reported as\n * unhandled instead of silently running nothing. {@link checkIsolatedJobs}\n * holds the table against the deployment's configured crons.\n */\nimport { LAUNCH_BUDGETS, type InvariantVerdict } from './catalog.js'\n\n/** One cron expression and the job it runs, or one job per listed minute. */\nexport type ScheduleEntry =\n | { cron: string; job: string }\n | { cron: string; minutes: Readonly<Record<number, string>> }\n\n/** One scheduled invocation: the cron, the minute for a minute-list entry, and the job it runs. */\nexport interface ScheduleSlot {\n cron: string\n minute?: number\n job: string\n}\n\nexport interface ScheduledJobInput<Ctx> {\n cron: string\n scheduledTime: Date\n context: Ctx\n /** 1 for the first try. */\n attempt: number\n}\n\nexport type ScheduledJob<Ctx> = (input: ScheduledJobInput<Ctx>) => Promise<unknown>\n\nexport interface ScheduledDispatchOptions<Ctx> {\n entries: readonly ScheduleEntry[]\n jobs: Readonly<Record<string, ScheduledJob<Ctx>>>\n /** Waits before each retry of a failed job; empty (the default) runs a job once. Jobs that retry must be idempotent. */\n retryDelaysMs?: readonly number[]\n log?: {\n info(message: string, detail?: Record<string, unknown>): void\n error(message: string, detail?: Record<string, unknown>): void\n }\n /** Tests pass a no-op. */\n sleep?: (ms: number) => Promise<void>\n}\n\nexport interface ScheduledJobOutcome {\n job: string\n ok: boolean\n attempts: number\n durationMs: number\n error?: string\n}\n\nexport interface ScheduledDispatchResult {\n handled: boolean\n job: string | null\n /** Settles when the job's last attempt ends; never rejects. */\n done: Promise<ScheduledJobOutcome | null>\n}\n\nexport interface ScheduledDispatch<Ctx> {\n readonly entries: readonly ScheduleEntry[]\n /** Every invocation the table can make. */\n slots(): ScheduleSlot[]\n /** The job a tick runs, or null when no entry handles it. */\n jobFor(cron: string, scheduledTime: number | Date): string | null\n /**\n * Run the tick's one job. With `waitUntil` the job runs in the background and\n * this returns at once; without it, this waits for the job.\n */\n dispatch(\n event: { cron: string; scheduledTime: number | Date },\n context: Ctx,\n waitUntil?: (promise: Promise<unknown>) => void,\n ): Promise<ScheduledDispatchResult>\n}\n\nexport function createScheduledDispatch<Ctx>(options: ScheduledDispatchOptions<Ctx>): ScheduledDispatch<Ctx> {\n const problems = validateEntries(options.entries, Object.keys(options.jobs))\n if (problems.length) throw new Error(`scheduled dispatch: ${problems.join('; ')}`)\n const byCron = new Map(options.entries.map((entry) => [entry.cron, entry]))\n const retryDelaysMs = options.retryDelaysMs ?? []\n const log = options.log ?? {\n info: (message: string, detail?: Record<string, unknown>) => console.log(message, detail ?? ''),\n error: (message: string, detail?: Record<string, unknown>) => console.error(message, detail ?? ''),\n }\n const sleep = options.sleep ?? ((ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)))\n\n function jobFor(cron: string, scheduledTime: number | Date): string | null {\n const entry = byCron.get(cron)\n if (!entry) return null\n if ('job' in entry) return entry.job\n const minute = new Date(scheduledTime).getUTCMinutes()\n return entry.minutes[minute] ?? null\n }\n\n async function runJob(job: string, cron: string, scheduledTime: Date, context: Ctx): Promise<ScheduledJobOutcome> {\n const run = options.jobs[job]!\n const started = Date.now()\n let lastError = ''\n for (let attempt = 1; attempt <= retryDelaysMs.length + 1; attempt += 1) {\n try {\n await run({ cron, scheduledTime, context, attempt })\n return { job, ok: true, attempts: attempt, durationMs: Date.now() - started }\n } catch (error) {\n lastError = error instanceof Error ? error.message : String(error)\n const delay = retryDelaysMs[attempt - 1]\n if (delay === undefined) break\n log.error(`[scheduled] ${job} failed; retrying`, { attempt, error: lastError })\n await sleep(delay)\n }\n }\n log.error(`[scheduled] ${job} failed`, { attempts: retryDelaysMs.length + 1, error: lastError })\n return { job, ok: false, attempts: retryDelaysMs.length + 1, durationMs: Date.now() - started, error: lastError }\n }\n\n return {\n entries: options.entries,\n slots: () => slotsOf(options.entries),\n jobFor,\n async dispatch(event, context, waitUntil) {\n const scheduledTime = new Date(event.scheduledTime)\n const job = jobFor(event.cron, scheduledTime)\n if (!job) {\n log.error(`[scheduled] no job handles cron \"${event.cron}\" at minute ${scheduledTime.getUTCMinutes()}; the configured crons and the schedule disagree`)\n return { handled: false, job: null, done: Promise.resolve(null) }\n }\n const done = runJob(job, event.cron, scheduledTime, context)\n if (waitUntil) waitUntil(done)\n else await done\n return { handled: true, job, done }\n },\n }\n}\n\n/** Every invocation a schedule table can make. */\nexport function slotsOf(entries: readonly ScheduleEntry[]): ScheduleSlot[] {\n return entries.flatMap((entry) => 'job' in entry\n ? [{ cron: entry.cron, job: entry.job }]\n : Object.entries(entry.minutes).map(([minute, job]) => ({ cron: entry.cron, minute: Number(minute), job })))\n}\n\nfunction validateEntries(entries: readonly ScheduleEntry[], jobNames: readonly string[]): string[] {\n const problems: string[] = []\n const known = new Set(jobNames)\n const seen = new Set<string>()\n for (const entry of entries) {\n if (seen.has(entry.cron)) problems.push(`cron \"${entry.cron}\" is listed twice`)\n seen.add(entry.cron)\n const fields = entry.cron.trim().split(/\\s+/)\n if (fields.length !== 5) problems.push(`cron \"${entry.cron}\" does not have five fields`)\n for (const slot of slotsOf([entry])) {\n if (!known.has(slot.job)) problems.push(`cron \"${entry.cron}\" names job \"${slot.job}\", which is not defined`)\n }\n if ('minutes' in entry) {\n const listed = minuteList(fields[0] ?? '')\n const mapped = Object.keys(entry.minutes).map(Number).sort((a, b) => a - b)\n if (!listed) problems.push(`cron \"${entry.cron}\" maps jobs by minute, so its minute field must be a list of minutes`)\n else if (listed.join(',') !== mapped.join(',')) {\n problems.push(`cron \"${entry.cron}\" lists minutes ${listed.join(',')} but maps jobs to ${mapped.join(',')}`)\n }\n }\n }\n return problems\n}\n\n/** The minutes of a plain minute list (`5` or `0,15,30`), or null for any other form. */\nfunction minuteList(field: string): number[] | null {\n if (!/^\\d{1,2}(,\\d{1,2})*$/.test(field)) return null\n const minutes = field.split(',').map(Number)\n if (minutes.some((minute) => minute > 59)) return null\n return [...new Set(minutes)].sort((a, b) => a - b)\n}\n\n// ── cron analysis ────────────────────────────────────────────────────────────\n\nfunction expandField(field: string, min: number, max: number): Set<number> | null {\n const values = new Set<number>()\n for (const part of field.split(',')) {\n const match = /^(\\*|\\d+(?:-\\d+)?)(?:\\/(\\d+))?$/.exec(part)\n if (!match) return null\n const [, range, stepText] = match\n const step = stepText ? Number(stepText) : 1\n if (!(step >= 1)) return null\n let lo = min\n let hi = max\n if (range !== '*') {\n const [a, b] = range!.split('-').map(Number)\n lo = a!\n hi = b ?? (stepText ? max : a!)\n }\n if (lo < min || hi > max || lo > hi) return null\n for (let value = lo; value <= hi; value += step) values.add(value)\n }\n return values\n}\n\n/**\n * The minutes of the day (0–1439, UTC) a cron fires, or null when it does not\n * fire every day (a day-of-month, month or weekday restriction) or cannot be read.\n */\nexport function cronDailyFirings(cron: string): Set<number> | null {\n const fields = cron.trim().split(/\\s+/)\n if (fields.length !== 5) return null\n const [minuteField, hourField, dayField, monthField, weekdayField] = fields as [string, string, string, string, string]\n if (dayField !== '*' || monthField !== '*' || (weekdayField !== '*' && weekdayField !== '?')) return null\n const minutes = expandField(minuteField, 0, 59)\n const hours = expandField(hourField, 0, 23)\n if (!minutes || !hours) return null\n const firings = new Set<number>()\n for (const hour of hours) for (const minute of minutes) firings.add(hour * 60 + minute)\n return firings\n}\n\n/** The longest wait, in minutes, between consecutive firings over a day, or null when they do not fire daily. */\nexport function longestGapMinutes(firings: Iterable<number>): number | null {\n const sorted = [...new Set(firings)].sort((a, b) => a - b)\n if (sorted.length === 0) return null\n let gap = sorted[0]! + 1440 - sorted[sorted.length - 1]!\n for (let index = 1; index < sorted.length; index += 1) gap = Math.max(gap, sorted[index]! - sorted[index - 1]!)\n return gap\n}\n\n/** The cron expressions a wrangler config declares, top level and per environment. */\nexport interface WranglerCrons {\n top: string[]\n envs: Record<string, string[]>\n}\n\n/** Read `[triggers] crons` (and `[env.<name>.triggers]`) from wrangler.toml, or `triggers.crons` from wrangler.json(c). */\nexport function parseWranglerCrons(source: string, format: 'toml' | 'json'): WranglerCrons {\n return format === 'json' ? parseJsonCrons(source) : parseTomlCrons(source)\n}\n\nfunction parseTomlCrons(source: string): WranglerCrons {\n const result: WranglerCrons = { top: [], envs: {} }\n let table = ''\n let collecting: string[] | null = null\n let buffer = ''\n for (const raw of source.split('\\n')) {\n const line = stripTomlComment(raw)\n if (collecting) {\n buffer += ` ${line}`\n if (line.includes(']')) {\n collecting.push(...quoted(buffer))\n collecting = null\n buffer = ''\n }\n continue\n }\n const header = /^\\s*\\[([^\\[\\]]+)\\]\\s*$/.exec(line)\n if (header) {\n table = header[1]!.trim()\n continue\n }\n const assignment = /^\\s*crons\\s*=\\s*(.*)$/.exec(line)\n if (!assignment) continue\n const envName = /^env\\.([^.]+)\\.triggers$/.exec(table)?.[1]\n const target = table === 'triggers' ? result.top : envName ? (result.envs[envName] ??= []) : null\n if (!target) continue\n const rest = assignment[1]!\n if (rest.includes(']')) target.push(...quoted(rest))\n else {\n collecting = target\n buffer = rest\n }\n }\n return result\n}\n\nfunction stripTomlComment(line: string): string {\n let quote: string | null = null\n for (let index = 0; index < line.length; index += 1) {\n const char = line[index]!\n if (quote) {\n if (char === quote) quote = null\n } else if (char === '\"' || char === \"'\") quote = char\n else if (char === '#') return line.slice(0, index)\n }\n return line\n}\n\nfunction quoted(text: string): string[] {\n return [...text.matchAll(/\"([^\"]*)\"|'([^']*)'/g)].map((match) => match[1] ?? match[2] ?? '')\n}\n\nfunction parseJsonCrons(source: string): WranglerCrons {\n const config = JSON.parse(stripJsonComments(source)) as {\n triggers?: { crons?: string[] }\n env?: Record<string, { triggers?: { crons?: string[] } }>\n }\n return {\n top: [...(config.triggers?.crons ?? [])],\n envs: Object.fromEntries(Object.entries(config.env ?? {})\n .filter(([, env]) => Array.isArray(env.triggers?.crons))\n .map(([name, env]) => [name, [...env.triggers!.crons!]])),\n }\n}\n\nfunction stripJsonComments(source: string): string {\n let out = ''\n let inString = false\n for (let index = 0; index < source.length; index += 1) {\n const char = source[index]!\n const next = source[index + 1]\n if (inString) {\n out += char\n if (char === '\\\\') out += source[++index] ?? ''\n else if (char === '\"') inString = false\n continue\n }\n if (char === '\"') {\n inString = true\n out += char\n } else if (char === '/' && next === '/') {\n while (index < source.length && source[index] !== '\\n') index += 1\n out += '\\n'\n } else if (char === '/' && next === '*') {\n index += 2\n while (index < source.length && !(source[index] === '*' && source[index + 1] === '/')) index += 1\n index += 1\n } else out += char\n }\n return out.replace(/,(\\s*[}\\]])/g, '$1')\n}\n\n// ── the invariant ────────────────────────────────────────────────────────────\n\n/** What one scheduled invocation was observed to run. */\nexport interface ObservedSlot {\n cron: string\n minute?: number\n /** The jobs the invocation ran; exactly one passes. */\n jobs: readonly string[]\n}\n\nexport interface IsolatedJobsInput {\n /** Every cron the deployment configures, from each wrangler environment. */\n configuredCrons: readonly string[]\n /** What each invocation runs: from {@link slotsOf} for a {@link createScheduledDispatch} table, or observed by driving the app's own handler. */\n slots: readonly ObservedSlot[]\n /** Jobs that recover and settle turns; each must run at least every 15 minutes. */\n recoveryJobs: readonly string[]\n /** Default {@link LAUNCH_BUDGETS.recoveryEveryMs}. */\n everyMs?: number\n}\n\n/** One job per invocation, the table and the deployment agree, and recovery runs at least every 15 minutes. */\nexport function checkIsolatedJobs(input: IsolatedJobsInput): InvariantVerdict {\n const everyMinutes = (input.everyMs ?? LAUNCH_BUDGETS.recoveryEveryMs) / 60_000\n const findings: string[] = []\n const configured = new Set(input.configuredCrons)\n const dispatched = new Set(input.slots.map((slot) => slot.cron))\n for (const cron of configured) if (!dispatched.has(cron)) findings.push(`configured cron \"${cron}\" runs no job: no schedule entry matches it`)\n for (const cron of dispatched) if (!configured.has(cron)) findings.push(`schedule cron \"${cron}\" is not configured, so it never runs`)\n for (const slot of input.slots) {\n const where = `cron \"${slot.cron}\"${slot.minute !== undefined ? ` at minute ${slot.minute}` : ''}`\n if (slot.jobs.length !== 1) findings.push(`${where} runs ${slot.jobs.length} jobs in one invocation (${slot.jobs.join(', ') || 'none'}); one failure ends the rest`)\n }\n const byCron = new Map<string, ObservedSlot[]>()\n for (const slot of input.slots) byCron.set(slot.cron, [...(byCron.get(slot.cron) ?? []), slot])\n for (const [cron, slots] of byCron) {\n const minuteSlots = slots.filter((slot) => slot.minute !== undefined)\n if (minuteSlots.length === 0) continue\n const listed = minuteList(cron.trim().split(/\\s+/)[0] ?? '')\n const mapped = [...new Set(minuteSlots.map((slot) => slot.minute!))].sort((a, b) => a - b)\n if (!listed || listed.join(',') !== mapped.join(',')) {\n findings.push(`cron \"${cron}\" fires at minutes ${listed?.join(',') ?? '(not a minute list)'} but jobs are mapped to ${mapped.join(',')}`)\n }\n }\n if (input.recoveryJobs.length === 0) findings.push('no turn recovery or orphan settlement job is declared')\n const gaps: Record<string, number | null> = {}\n for (const job of input.recoveryJobs) {\n const firings = new Set<number>()\n let daily = false\n for (const slot of input.slots) {\n if (slot.jobs.length !== 1 || slot.jobs[0] !== job) continue\n const fires = cronDailyFirings(slot.cron)\n if (!fires) continue\n daily = true\n for (const minuteOfDay of fires) if (slot.minute === undefined || minuteOfDay % 60 === slot.minute) firings.add(minuteOfDay)\n }\n const gap = daily ? longestGapMinutes(firings) : null\n gaps[job] = gap\n if (gap === null) findings.push(`recovery job \"${job}\" is not scheduled to run every day on its own`)\n else if (gap > everyMinutes) findings.push(`recovery job \"${job}\" can wait ${gap} minutes between runs; the most is ${everyMinutes}`)\n }\n return {\n invariant: 'isolated-jobs',\n subject: 'schedule',\n pass: findings.length === 0,\n details: findings.length ? findings : [\n `${input.slots.length} scheduled invocations, one job each, across ${configured.size} configured crons`,\n ...input.recoveryJobs.map((job) => `${job} runs at least every ${gaps[job]} minutes`),\n ],\n data: { configuredCrons: [...configured], slots: input.slots.length, recoveryGapsMinutes: gaps },\n }\n}\n","/**\n * Alarms that fire at 80% of a platform limit, before the limit fails a customer.\n *\n * On 2026-10-10 GTM recorded 19 platform-limit events (Worker memory, D1,\n * sandbox disk, snapshot count, key rate) and learned of each one after it had\n * failed someone. Each resource here has a declared limit; an observation at\n * or above 80% of it delivers a warning, and at or above the limit a critical\n * alert, through the same {@link AlertSink} the turn-health sweep uses (wrap it\n * in `createThrottledAlertSink` so a hot loop pages once).\n *\n * Where each observation comes from:\n * - `d1-rows-per-query`: {@link withD1LimitAlarms} reads `meta.rows_read` on every query.\n * - `worker-memory`: workerd exposes no heap reading, so the D1 wrapper observes\n * the bytes each response returns (the reads that overran memory today), and a\n * tail consumer reports an `exceededMemory` outcome at the full limit.\n * - `sandbox-disk` and `snapshot-count`: the Sandbox SDK's box usage and snapshot list, read on a schedule.\n * - `key-rate`: the per-key request counter the operator API keeps.\n */\nimport {\n createConsoleAlertSink,\n createMemoryThrottleStore,\n createMultiAlertSink,\n createThrottledAlertSink,\n createWebhookAlertSink,\n type AlertSink,\n type TurnHealthAlert,\n} from '../turn-health/sink.js'\nimport { tallyD1Query } from './d1-query-count.js'\nimport { LAUNCH_BUDGETS } from './catalog.js'\n\nexport type LimitResource = 'worker-memory' | 'd1-rows-per-query' | 'sandbox-disk' | 'snapshot-count' | 'key-rate'\n\nexport const LIMIT_RESOURCES: readonly LimitResource[] = [\n 'worker-memory',\n 'd1-rows-per-query',\n 'sandbox-disk',\n 'snapshot-count',\n 'key-rate',\n]\n\n/** The limit of each resource, in its own unit: bytes, rows, bytes, snapshots, requests per minute. */\nexport type LimitBudgets = Readonly<Record<LimitResource, number>>\n\nconst UNITS: Readonly<Record<LimitResource, string>> = {\n 'worker-memory': 'bytes',\n 'd1-rows-per-query': 'rows',\n 'sandbox-disk': 'bytes',\n 'snapshot-count': 'snapshots',\n 'key-rate': 'requests/min',\n}\n\nexport type LimitLevel = 'ok' | 'warning' | 'critical'\n\nexport interface LimitObservation {\n resource: LimitResource\n used: number\n limit: number\n /** used / limit. */\n share: number\n level: LimitLevel\n}\n\nexport interface LimitAlarms {\n readonly budgets: LimitBudgets\n /** Record one reading; a warning or critical reading is delivered before this resolves. */\n observe(resource: LimitResource, used: number, context?: { subject?: string; detail?: string }): Promise<LimitObservation>\n}\n\nexport interface LimitAlarmOptions {\n product: string\n budgets: LimitBudgets\n sink: AlertSink\n /** Default {@link LAUNCH_BUDGETS.alarmRatio}; a product cannot raise it past the limit itself. */\n ratio?: number\n now?: () => number\n}\n\nexport function createLimitAlarms(options: LimitAlarmOptions): LimitAlarms {\n const ratio = Math.min(options.ratio ?? LAUNCH_BUDGETS.alarmRatio, 1)\n const now = options.now ?? (() => Date.now())\n for (const resource of LIMIT_RESOURCES) {\n const limit = options.budgets[resource]\n if (!(typeof limit === 'number' && limit > 0 && Number.isFinite(limit))) {\n throw new Error(`limit alarms: ${resource} needs a positive limit`)\n }\n }\n return {\n budgets: options.budgets,\n async observe(resource, used, context) {\n const limit = options.budgets[resource]\n const share = used / limit\n const level: LimitLevel = share >= 1 ? 'critical' : share >= ratio ? 'warning' : 'ok'\n const observation = { resource, used, limit, share, level }\n if (level === 'ok') return observation\n const alert: TurnHealthAlert = {\n product: options.product,\n severity: level,\n // Keyed by product and resource only, so a throttled sink pages once per resource.\n key: `limit:${options.product}:${resource}`,\n title: `${options.product}: ${resource} at ${Math.round(share * 100)}% of its limit`,\n details: [\n `${formatAmount(used, resource)} of ${formatAmount(limit, resource)} ${UNITS[resource]}`,\n ...(context?.subject ? [`in ${context.subject}`] : []),\n ...(context?.detail ? [context.detail] : []),\n ],\n data: { resource, used, limit, share, ...(context?.subject ? { subject: context.subject } : {}) },\n at: now(),\n }\n await options.sink.deliver(alert)\n return observation\n },\n }\n}\n\nfunction formatAmount(value: number, resource: LimitResource): string {\n if (UNITS[resource] === 'bytes') return `${(value / (1024 * 1024)).toFixed(1)} MB`\n return Math.round(value).toLocaleString('en-US')\n}\n\n/** The budgets every product starts from: {@link LAUNCH_BUDGETS}' platform limits. */\nexport const DEFAULT_LIMIT_BUDGETS: LimitBudgets = {\n 'worker-memory': LAUNCH_BUDGETS.workerMemoryBytes,\n 'd1-rows-per-query': LAUNCH_BUDGETS.d1RowsPerQuery,\n 'sandbox-disk': LAUNCH_BUDGETS.sandboxDiskBytes,\n 'snapshot-count': LAUNCH_BUDGETS.snapshotCount,\n 'key-rate': LAUNCH_BUDGETS.keyRatePerMinute,\n}\n\nexport interface ProductLimitAlarmOptions {\n /** The product's name in alerts, e.g. `legal`. */\n product: string\n /** The ops webhook; without one alarms go to the console only. */\n webhookUrl?: string\n /** The few budgets this product measured differently, e.g. a larger sandbox disk. */\n overrides?: Partial<LimitBudgets>\n /** Replace the default console-and-webhook sink (tests, or an app with its own alert transport). */\n sink?: AlertSink\n}\n\n/**\n * A product's limit alarms with every default in place: {@link DEFAULT_LIMIT_BUDGETS}, the console and optional webhook\n * sink, and one page per resource every {@link LAUNCH_BUDGETS.alarmRepeatHours} hours. A product passes only what it\n * measured differently; Legal, Creative, Tax and GTM each kept a 40-line copy of this before.\n */\nexport function createProductLimitAlarms(options: ProductLimitAlarmOptions): LimitAlarms {\n const sink = options.sink ?? createThrottledAlertSink(createMultiAlertSink([\n createConsoleAlertSink((message) => console.error(message)),\n ...(options.webhookUrl ? [createWebhookAlertSink({ webhookUrl: options.webhookUrl })] : []),\n ]), { store: createMemoryThrottleStore(), windowMs: LAUNCH_BUDGETS.alarmRepeatHours * 60 * 60 * 1000 })\n return createLimitAlarms({ product: options.product, budgets: { ...DEFAULT_LIMIT_BUDGETS, ...options.overrides }, sink })\n}\n\n/** The D1 surface the wrapper measures; Cloudflare `D1Database` satisfies it. */\ninterface D1Measurable {\n prepare(sql: string): unknown\n}\n\n/**\n * The D1 binding with each query's rows read and response size observed,\n * and each round trip counted inside withD1QueryCount (chat routes scope it automatically).\n * Rows read come from `meta.rows_read` (the rows D1 scanned, not only those\n * returned). The response size is estimated from three sampled rows, so the\n * wrapper never serializes a whole response: that copy would double the memory\n * of the very read it watches.\n */\nexport function withD1LimitAlarms<D extends D1Measurable>(d1: D, alarms: LimitAlarms, options: { subject?: string } = {}): D {\n const observe = (sql: string, result: unknown) => {\n const meta = (result as { meta?: { rows_read?: unknown } } | null)?.meta\n const rowsRead = typeof meta?.rows_read === 'number' ? meta.rows_read : rowsOf(result)\n const pending: Promise<unknown>[] = []\n if (rowsRead !== null) pending.push(alarms.observe('d1-rows-per-query', rowsRead, { subject: options.subject, detail: sql }))\n const results = (result as { results?: unknown[] } | null)?.results ?? (Array.isArray(result) ? result : null)\n if (results && results.length > 0) {\n pending.push(alarms.observe('worker-memory', estimatedBytes(results), { subject: options.subject, detail: `one response: ${sql}` }))\n }\n // An alarm delivery failure must not fail the query that triggered it.\n return Promise.all(pending).catch(() => undefined)\n }\n // D1's batch() takes only its own statements, so a batch is given the unwrapped ones.\n const unwrapped = new WeakMap<object, object>()\n const statement = (target: object, sql: string): object => {\n const wrapped = new Proxy(target, {\n get(stmt, key) {\n const value = Reflect.get(stmt, key) as unknown\n if (typeof value !== 'function') return value\n if (key === 'bind') return (...params: unknown[]) => statement(value.apply(stmt, params) as object, sql)\n if (key === 'all' || key === 'raw' || key === 'run' || key === 'first') {\n return async (...args: unknown[]) => {\n tallyD1Query()\n const result: unknown = await value.apply(stmt, args)\n if (key !== 'first') await observe(sql, result)\n return result\n }\n }\n return value.bind(stmt)\n },\n })\n unwrapped.set(wrapped, target)\n return wrapped\n }\n return new Proxy(d1, {\n get(binding, key) {\n const value = Reflect.get(binding, key) as unknown\n if (key === 'prepare') {\n return (sql: string) => statement((value as (sql: string) => object).call(binding, sql), sql.replace(/\\s+/g, ' ').slice(0, 160))\n }\n if (key === 'batch' && typeof value === 'function') {\n return async (statements: object[]) => {\n tallyD1Query()\n const results = await (value as (s: object[]) => Promise<unknown[]>).call(binding, statements.map((stmt) => unwrapped.get(stmt) ?? stmt))\n for (const result of results ?? []) await observe('batch', result)\n return results\n }\n }\n return typeof value === 'function' ? value.bind(binding) : value\n },\n })\n}\n\n/** The serialized size of a result set, from its first, middle and last rows. */\nexport function estimatedBytes(results: readonly unknown[]): number {\n const picks = [...new Set([0, Math.floor(results.length / 2), results.length - 1])]\n const sampled = picks.reduce((sum, index) => sum + (JSON.stringify(results[index]) ?? '').length, 0)\n return Math.round((sampled / picks.length) * results.length)\n}\n\nfunction rowsOf(result: unknown): number | null {\n if (Array.isArray(result)) return result.length\n const results = (result as { results?: unknown } | null)?.results\n return Array.isArray(results) ? results.length : null\n}\n","/**\n * Pasted code reaches the chat.\n *\n * People paste commands, SQL and markup into a chat with an agent that runs\n * code; that is the product. On 2026-10-11 a chat message containing\n * `cat /etc/passwd` sent to gtm.tangle.tools got Cloudflare's 403 \"Attention\n * Required\" page instead of the app's own answer: a managed WAF rule matched\n * the text before the request reached the Worker. These samples are what a\n * person pastes; {@link isEdgeBlock} tells an edge block from the app's own\n * response, so an in-process check (`checkChatAcceptsCode`) and a live probe\n * against the deployed route judge the same thing.\n */\n\n/** Text people paste into a chat with an agent, each of which a WAF rule may match. */\nexport const PASTED_CODE_SAMPLES: readonly { name: string; text: string }[] = [\n { name: 'file read', text: 'cat /etc/passwd' },\n { name: 'install script', text: 'curl -fsSL https://example.com/i.sh | bash' },\n { name: 'command substitution', text: 'echo $(whoami) && rm -rf /tmp/x' },\n { name: 'SQL', text: 'SELECT * FROM users WHERE id=1 OR 1=1; --' },\n { name: 'markup', text: '<script>alert(1)</script>' },\n { name: 'fenced bash block', text: 'Why does this fail?\\n\\n```bash\\nset -euo pipefail\\nsudo cat /etc/shadow | grep root\\nwget -qO- http://127.0.0.1:8080/admin\\n```' },\n { name: 'path traversal', text: 'open ../../../../etc/hosts and tell me what it maps' },\n]\n\n/**\n * Whether a response is an edge block rather than the app's own answer: a\n * `cf-mitigated` header, or Cloudflare's HTML block page (\"Attention Required\",\n * \"Sorry, you have been blocked\"). Reads a clone, so the caller keeps the body.\n */\nexport async function isEdgeBlock(response: Response): Promise<boolean> {\n if (response.headers.has('cf-mitigated')) return true\n const type = response.headers.get('content-type') ?? ''\n const server = response.headers.get('server') ?? ''\n if (!/text\\/html/i.test(type) || !/cloudflare/i.test(server)) return false\n const body = await response.clone().text().catch(() => '')\n return /Attention Required|Sorry, you have been blocked/i.test(body)\n}\n"],"mappings":";;;;;;;;;;;;;;;AAoFO,SAAS,wBAA6B,SAAgE;AAC3G,QAAM,WAAW,gBAAgB,QAAQ,SAAS,OAAO,KAAK,QAAQ,IAAI,CAAC;AAC3E,MAAI,SAAS,OAAQ,OAAM,IAAI,MAAM,uBAAuB,SAAS,KAAK,IAAI,CAAC,EAAE;AACjF,QAAM,SAAS,IAAI,IAAI,QAAQ,QAAQ,IAAI,CAAC,UAAU,CAAC,MAAM,MAAM,KAAK,CAAC,CAAC;AAC1E,QAAM,gBAAgB,QAAQ,iBAAiB,CAAC;AAChD,QAAM,MAAM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,MAAM,kBAAkB;AAAA,EAC7C,EAAE,MAAM,kBAAkB,MAAM,6CAA6C;AAAA,EAC7E,EAAE,MAAM,wBAAwB,MAAM,kCAAkC;AAAA,EACxE,EAAE,MAAM,OAAO,MAAM,4CAA4C;AAAA,EACjE,EAAE,MAAM,UAAU,MAAM,4BAA4B;AAAA,EACpD,EAAE,MAAM,qBAAqB,MAAM,kIAAkI;AAAA,EACrK,EAAE,MAAM,kBAAkB,MAAM,sDAAsD;AACxF;AAOA,eAAsB,YAAY,UAAsC;AACtE,MAAI,SAAS,QAAQ,IAAI,cAAc,EAAG,QAAO;AACjD,QAAM,OAAO,SAAS,QAAQ,IAAI,cAAc,KAAK;AACrD,QAAM,SAAS,SAAS,QAAQ,IAAI,QAAQ,KAAK;AACjD,MAAI,CAAC,cAAc,KAAK,IAAI,KAAK,CAAC,cAAc,KAAK,MAAM,EAAG,QAAO;AACrE,QAAM,OAAO,MAAM,SAAS,MAAM,EAAE,KAAK,EAAE,MAAM,MAAM,EAAE;AACzD,SAAO,mDAAmD,KAAK,IAAI;AACrE;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/model-resolution/failover.ts"],"sourcesContent":["/**\n * Model failover — the answer to \"one upstream hit a quota wall and the customer\n * got a Bad Gateway even though the router had abundant healthy capacity\".\n *\n * This is deliberately NOT a health probe. Probing before every turn buys a\n * round-trip of latency on the happy path and still races the outage (a model\n * healthy at probe time can 502 a second later). Failover is REACTIVE: run the\n * preferred model, and on an upstream-outage signal move to the next model in\n * the chain. Zero added latency when the preferred model works.\n *\n * Two facts drive the design, both measured against a live box during the\n * 2026-07-25 Anthropic/DeepSeek outage:\n *\n * 1. An outage is NOT always a thrown error. The sandbox resolves with a\n * payload — `{ success: false, errorCode: 'provider_inference_unavailable' }`\n * — so a classifier that only inspects `catch` misses the customer-visible\n * case entirely. `isUpstreamUnavailable` inspects resolved values too.\n * 2. Catalog membership is NOT liveness. The router's `/v1/models` still listed\n * every dead model during the outage, so `validateChatModelId` admitted\n * `claude-sonnet-4-6` while every call to it returned 502. Nothing upstream\n * of the actual call can be trusted to tell you a model is serving.\n *\n * Substrate-free: the caller injects `run`, so this composes with a sandbox\n * turn, a router chat completion, or a test fake without importing any of them.\n */\n\nimport {\n checkModelPolicy,\n type ModelPolicy,\n} from '../policy-filters/index'\n\n/**\n * Error codes that mean \"this model's upstream is unavailable — a different\n * model may still work\". Deliberately excludes codes that would fail identically\n * on every model (bad request, auth, content filter): retrying those down a\n * chain burns latency and money to reach the same failure.\n */\nexport const UPSTREAM_UNAVAILABLE_CODES: readonly string[] = [\n 'provider_inference_unavailable',\n 'provider_quota_exhausted',\n 'upstream_unavailable',\n 'insufficient_quota',\n 'model_not_available',\n 'server_error',\n 'bad_gateway',\n 'service_unavailable',\n]\n\n/** HTTP statuses that indicate an upstream capacity/availability problem. */\nexport const UPSTREAM_UNAVAILABLE_STATUSES: readonly number[] = [429, 500, 502, 503, 504]\n\n/**\n * Statuses that mean the REQUEST is wrong, so every model in the chain would\n * fail on it identically. Listed explicitly rather than as \"any 4xx\" because\n * 404 is NOT one of them: a model the endpoint cannot find is model-scoped, and\n * the next model in the chain may well be there. 429 is absent for the same\n * reason in reverse — it is a capacity signal and lives in the list above.\n */\nconst REQUEST_ERROR_STATUSES: readonly number[] = [400, 401, 403, 405, 413, 422]\n\n/**\n * Message fragments emitted by real upstreams during this class of outage.\n * Matched case-insensitively as a last resort, after code and status.\n */\nconst UPSTREAM_UNAVAILABLE_MESSAGES: readonly string[] = [\n 'bad gateway',\n 'service unavailable',\n 'inference temporarily unavailable',\n 'provider inference is unavailable',\n 'insufficient balance',\n 'usage limits',\n 'quota exceeded',\n 'rate limit',\n 'overloaded',\n 'temporarily unavailable',\n // Model-SCOPED unavailability. A different model in the chain can still\n // serve, so this belongs here and not with the client errors. Measured on a\n // real box 2026-07-27: the sandbox now reports an unservable model as a\n // terminal `error` whose data is `{\"message\":\"Session error:\n // {\\\"error\\\":{\\\"name\\\":\\\"UnknownError\\\",\\\"data\\\":{\\\"message\\\":\\\"Model not\n // found: openai-compat/zai/glm-4.7.\\\"}}}\"}` — no `code`, no numeric status,\n // and none of the fragments above. The shipped live product-path proof went\n // red on exactly this: `usedFallback:false`, `failed:true`, an error row to\n // the customer where a fallback was available and would have answered.\n 'model not found',\n 'is not currently available',\n]\n\n/**\n * Textual forms an HTTP status arrives in when NO numeric field carries it.\n *\n * The case that forced this is the opaque Cloudflare 502. Captured verbatim\n * from `router.tangle.tools` on 2026-07-27 17:48:12 GMT (cf-ray\n * `a21d793899f6cef3-DEN`): `HTTP/2 502`, `content-type: text/plain;\n * charset=UTF-8`, a 16-byte body reading `error code: 502\\n`, `server:\n * cloudflare`, and **zero `x-tangle-*` headers** where a healthy 200 from the\n * same endpoint carries nine of them.\n *\n * That capture was originally read as \"zero headers means the origin never\n * executed\". It does not, and the correction matters more than the original\n * guess did. Matching client-observed cf-rays against the origin's own access\n * log (tangle-router #307) showed all 62 of those responses WERE produced by\n * the router — `content-type: application/json`, an `X-Generation-Id`, and a\n * routing trace showing the upstream had returned 200 — and Cloudflare\n * replaced the body and every response header with its own error page, because\n * an origin 502 reads to the edge as the origin having failed.\n *\n * The classifier's BEHAVIOR here was right for the wrong reason, and stays\n * exactly as it is. What changes is why: the rescue is needed not because the\n * origin is unreachable, but because a response the origin carefully\n * structured arrives at the client as sixteen unlabelled bytes. The router now\n * emits 503 instead of 502 on those paths so the body survives, but this\n * pattern must remain — it is the client's only handle on any origin 5xx the\n * edge decides to rewrite, including from a router that has not deployed the\n * fix yet.\n *\n * Every pattern below matches a string a real producer emits:\n * - `error code: 502` — Cloudflare's edge body (the capture above).\n * - `(HTTP 502)` — `src/runtime/openai-stream.ts` wrapping a non-ok response.\n * - `returned 502` — `src/runtime/model-catalog.ts`.\n * - `failed with status 502` — `src/turn-stream/adapters.ts`.\n * - `502 Bad Gateway` — an HTTP/1.1 status line. HTTP/2 carries no reason\n * phrase at all, which is precisely why matching the phrase alone (the\n * `'bad gateway'` fragment below) read the capture as a non-outage.\n */\nconst HTTP_STATUS_HINT_PATTERNS: readonly RegExp[] = [\n /\\berror\\s+code:?\\s*([1-5]\\d{2})\\b/i,\n /\\bhttp(?:\\/[\\d.]+)?[\\s:]\\s*([1-5]\\d{2})\\b/i,\n /\\bstatus(?:\\s*code)?[\\s:=]\\s*([1-5]\\d{2})\\b/i,\n /\\b(?:returned|responded(?:\\s+with)?)\\s+([1-5]\\d{2})\\b/i,\n /\\b([1-5]\\d{2})\\s+(?:bad\\s+gateway|service\\s+unavailable|gateway\\s+time-?out|internal\\s+server\\s+error|too\\s+many\\s+requests)\\b/i,\n]\n\n/**\n * The HTTP status a message states in prose, or `undefined` when it states\n * none. Deliberately NOT \"any three digits in the string\" — an unanchored digit\n * scan would read a token count or a duration as a status.\n */\nexport function readHttpStatusHint(text: string): number | undefined {\n for (const pattern of HTTP_STATUS_HINT_PATTERNS) {\n const match = pattern.exec(text)\n if (match?.[1]) return Number(match[1])\n }\n return undefined\n}\n\nfunction readString(source: Record<string, unknown>, key: string): string | undefined {\n const value = source[key]\n return typeof value === 'string' && value.trim().length > 0 ? value : undefined\n}\n\n/** Read a recognized upstream code, including the terminal message-only shape. */\nexport function readUpstreamUnavailableCode(signal: unknown): string | undefined {\n if (signal === null || typeof signal !== 'object') return undefined\n const record = signal as Record<string, unknown>\n const nested = record.error\n const nestedRecord = nested !== null && typeof nested === 'object' ? (nested as Record<string, unknown>) : undefined\n const code =\n readString(record, 'errorCode') ??\n readString(record, 'code') ??\n (nestedRecord ? (readString(nestedRecord, 'code') ?? readString(nestedRecord, 'type')) : undefined)\n if (code && UPSTREAM_UNAVAILABLE_CODES.includes(code)) return code\n\n const message =\n readString(record, 'message') ??\n readString(record, 'error') ??\n (nestedRecord ? readString(nestedRecord, 'message') : undefined)\n return message && /\\(\\s*provider_quota_exhausted\\s*\\)/i.test(message)\n ? 'provider_quota_exhausted'\n : undefined\n}\n\n/**\n * True when `signal` — a thrown error OR a resolved result payload — indicates\n * the model's upstream is unavailable and another model is worth trying.\n *\n * Checked in order of decreasing confidence: explicit code, HTTP status, then\n * message text. A resolved payload only counts as a failure when it carries an\n * explicit failure marker (`success: false`, or an `error`/`errorCode` field) —\n * a successful result is never misread as an outage.\n */\nexport function isUpstreamUnavailable(signal: unknown): boolean {\n if (signal === null || typeof signal !== 'object') return false\n const record = signal as Record<string, unknown>\n\n // A resolved payload that explicitly reports success is never an outage.\n if (record.success === true) return false\n\n const nested = record.error\n const nestedRecord = nested !== null && typeof nested === 'object' ? (nested as Record<string, unknown>) : undefined\n if (readUpstreamUnavailableCode(record)) return true\n\n for (const key of ['status', 'statusCode', 'httpStatus']) {\n const value = record[key]\n if (typeof value === 'number' && UPSTREAM_UNAVAILABLE_STATUSES.includes(value)) return true\n }\n\n const message =\n readString(record, 'message') ??\n readString(record, 'error') ??\n (nestedRecord ? readString(nestedRecord, 'message') : undefined)\n if (!message) return false\n\n // A status carried as PROSE, checked before the fragment list because it is\n // the stronger signal and it cuts BOTH ways. An edge 502 names no exception\n // type and sets no numeric field, so without this the shipped classifier read\n // `error code: 502` as a non-outage and handed the customer a Bad Gateway\n // instead of trying the next model — the exact failure the router cannot\n // rescue from inside its own origin.\n const hinted = readHttpStatusHint(message)\n if (hinted !== undefined) {\n if (UPSTREAM_UNAVAILABLE_STATUSES.includes(hinted)) return true\n // An explicit REQUEST-error status is decisive the OTHER way. A 400\n // carrying a validation message must surface rather than walk the chain,\n // even when its body happens to contain a word from the fragment list —\n // the router's own `Function tools with reasoning_effort are not supported`\n // 400 is a request-shaping bug that fails identically on every model.\n if (REQUEST_ERROR_STATUSES.includes(hinted)) return false\n }\n\n const lowered = message.toLowerCase()\n return UPSTREAM_UNAVAILABLE_MESSAGES.some((fragment) => lowered.includes(fragment))\n}\n\n/** One model tried, and how it went. */\nexport interface ModelFailoverAttempt {\n model: string\n ok: boolean\n /** Why this model was abandoned. Absent when `ok`. */\n reason?: string\n /** Recognized upstream code carried by this failed attempt. */\n errorCode?: string\n}\n\n/** The outcome of a failover run: the value plus the full attempt trail. */\nexport interface ModelFailoverResult<T> {\n value: T\n /** The model that actually produced `value`. */\n model: string\n attempts: ModelFailoverAttempt[]\n /** True when the preferred (first) model did not serve the request. */\n usedFallback: boolean\n}\n\n/** Every model in the chain failed; carries the trail for logging. */\nexport class ModelFailoverExhaustedError extends Error {\n readonly attempts: ModelFailoverAttempt[]\n /** Last recognized upstream code across the exhausted chain, when present. */\n readonly upstreamCode?: string\n constructor(attempts: ModelFailoverAttempt[]) {\n const trail = attempts.map((a) => `${a.model}: ${a.reason ?? 'failed'}`).join(' | ')\n super(`All ${attempts.length} model(s) failed. ${trail}`)\n this.name = 'ModelFailoverExhaustedError'\n this.attempts = attempts\n this.upstreamCode = [...attempts].reverse().find((attempt) => attempt.errorCode)?.errorCode\n }\n}\n\n/** Inputs to {@link runWithModelFailover}. */\nexport interface RunWithModelFailoverInput<T> {\n /** Preferred model first, then fallbacks in descending preference. */\n models: readonly string[]\n /** Executes one turn with the given model. */\n run: (model: string) => Promise<T>\n /**\n * Product admission policy applied to EVERY chain member before it runs.\n * Without this a compliant model 502ing mid-turn would silently re-run the\n * turn's data through a non-compliant fallback: availability is not\n * eligibility. A member the policy refuses is SKIPPED (recorded in the\n * attempt trail with the refusal as its reason) and the walk continues; when\n * no member is admitted the run ends in {@link ModelFailoverExhaustedError}\n * like any other exhausted chain. Fail-closed — a member carrying no policy\n * metadata is refused.\n */\n policy?: ModelPolicy\n /**\n * Classifies a resolved result as an upstream outage. Defaults to\n * {@link isUpstreamUnavailable}, which understands the sandbox's\n * `{ success: false, errorCode }` payload.\n */\n isUnavailableResult?: (result: T) => boolean\n /** Classifies a thrown error. Defaults to {@link isUpstreamUnavailable}. */\n isUnavailableError?: (error: unknown) => boolean\n /** Observability hook fired each time a model is abandoned — including a\n * policy refusal, whose `reason` is the policy's own refusal text. */\n onFallback?: (attempt: ModelFailoverAttempt, nextModel: string) => void\n}\n\nfunction describe(signal: unknown): string {\n if (signal instanceof Error) return signal.message\n if (signal !== null && typeof signal === 'object') {\n const record = signal as Record<string, unknown>\n const message = readString(record, 'error') ?? readString(record, 'message') ?? readString(record, 'errorCode')\n if (message) return message\n }\n return String(signal)\n}\n\n/**\n * Run `run` against the first model in `models` that does not report an upstream\n * outage, falling through the chain in order.\n *\n * A non-outage failure (bad request, auth, content filter) is re-thrown\n * immediately rather than retried down the chain — those fail identically on\n * every model, so walking the chain would only multiply latency and spend.\n *\n * @throws ModelFailoverExhaustedError when every model reports an outage or is\n * refused by the configured admission policy.\n */\nexport async function runWithModelFailover<T>(\n input: RunWithModelFailoverInput<T>,\n): Promise<ModelFailoverResult<T>> {\n const models = input.models.map((m) => m.trim()).filter((m) => m.length > 0)\n if (models.length === 0) throw new Error('runWithModelFailover requires at least one model')\n\n const isUnavailableResult = input.isUnavailableResult ?? ((r: T) => isUpstreamUnavailable(r))\n const isUnavailableError = input.isUnavailableError ?? isUpstreamUnavailable\n const attempts: ModelFailoverAttempt[] = []\n\n for (let index = 0; index < models.length; index += 1) {\n const model = models[index]!\n\n // Policy BEFORE run: a chain member the deployment does not admit never\n // sees the turn's data. The refusal is one failed attempt — the walk\n // continues to the next member, and an all-refused chain exhausts exactly\n // like an all-dead one.\n if (input.policy) {\n const decision = await checkModelPolicy(model, input.policy.catalog, input.policy.filter, input.policy.context)\n if (!decision.allowed) {\n const attempt: ModelFailoverAttempt = { model, ok: false, reason: decision.reason }\n attempts.push(attempt)\n const next = models[index + 1]\n if (next) input.onFallback?.(attempt, next)\n continue\n }\n }\n\n let result: T\n try {\n result = await input.run(model)\n } catch (error) {\n if (!isUnavailableError(error)) throw error\n const errorCode = readUpstreamUnavailableCode(error)\n const attempt: ModelFailoverAttempt = {\n model,\n ok: false,\n reason: describe(error),\n ...(errorCode ? { errorCode } : {}),\n }\n attempts.push(attempt)\n const next = models[index + 1]\n if (next) input.onFallback?.(attempt, next)\n continue\n }\n\n if (isUnavailableResult(result)) {\n const errorCode = readUpstreamUnavailableCode(result)\n const attempt: ModelFailoverAttempt = {\n model,\n ok: false,\n reason: describe(result),\n ...(errorCode ? { errorCode } : {}),\n }\n attempts.push(attempt)\n const next = models[index + 1]\n if (next) input.onFallback?.(attempt, next)\n continue\n }\n\n attempts.push({ model, ok: true })\n return { value: result, model, attempts, usedFallback: index > 0 }\n }\n\n throw new ModelFailoverExhaustedError(attempts)\n}\n\n/**\n * Build a failover chain: the preferred model first, then `fallbacks`, with\n * duplicates removed so a model is never retried twice in one turn.\n */\nexport function buildModelChain(preferred: string, fallbacks: readonly string[]): string[] {\n const chain: string[] = []\n for (const model of [preferred, ...fallbacks]) {\n const cleaned = typeof model === 'string' ? model.trim() : ''\n if (cleaned.length > 0 && !chain.includes(cleaned)) chain.push(cleaned)\n }\n return chain\n}\n"],"mappings":";;;;;AAqCO,IAAM,6BAAgD;AAAA,EAC3D;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAGO,IAAM,gCAAmD,CAAC,KAAK,KAAK,KAAK,KAAK,GAAG;AASxF,IAAM,yBAA4C,CAAC,KAAK,KAAK,KAAK,KAAK,KAAK,GAAG;AAM/E,IAAM,gCAAmD;AAAA,EACvD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA;AAAA,EACA;AACF;AAuCA,IAAM,4BAA+C;AAAA,EACnD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAOO,SAAS,mBAAmB,MAAkC;AACnE,aAAW,WAAW,2BAA2B;AAC/C,UAAM,QAAQ,QAAQ,KAAK,IAAI;AAC/B,QAAI,QAAQ,CAAC,EAAG,QAAO,OAAO,MAAM,CAAC,CAAC;AAAA,EACxC;AACA,SAAO;AACT;AAEA,SAAS,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AQ,OAAO,UAAU,cAAc,QAAQ,EAAE;AAAA,EACnE;AAEA,QAAM,IAAI,4BAA4B,QAAQ;AAChD;AAMO,SAAS,gBAAgB,WAAmB,WAAwC;AACzF,QAAM,QAAkB,CAAC;AACzB,aAAW,SAAS,CAAC,WAAW,GAAG,SAAS,GAAG;AAC7C,UAAM,UAAU,OAAO,UAAU,WAAW,MAAM,KAAK,IAAI;AAC3D,QAAI,QAAQ,SAAS,KAAK,CAAC,MAAM,SAAS,OAAO,EAAG,OAAM,KAAK,OAAO;AAAA,EACxE;AACA,SAAO;AACT;","names":[]}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|