UNPKG

@luxorai/sandbox

Version:

TypeScript SDK for Luxor Sandbox: cloud sandboxes (microVMs) for AI agents on Luxor Agent Cloud

141 lines • 5.01 kB
import { exposeHostPort, } from "./tunnel.js"; const RETRY_REASONS = new Set([ "transport_error", "engine_terminated", "engine_draining", "timeout", ]); export async function exposeHostPortResilient(session, hostAddr, options) { const initialBackoffMs = options?.initialBackoffMs ?? 250; const maxBackoffMs = options?.maxBackoffMs ?? 10_000; const stateListeners = new Set(); const terminationListeners = new Set(); let state = "open"; let current = await exposeHostPort(session, hostAddr, options); let closed = false; let finalTerminationResolved = false; let resolveFinalTermination; let wakeReconnectSleep; const finalTermination = new Promise((resolve) => { resolveFinalTermination = resolve; }); const emitState = (event) => { for (const listener of stateListeners) listener(event); }; const finish = (termination) => { if (finalTerminationResolved) return; finalTerminationResolved = true; state = "closed"; for (const listener of terminationListeners) listener(termination); emitState({ state: "closed", reason: termination }); resolveFinalTermination(termination); }; const setCurrent = (next) => { const previousPort = current.sandboxPort; current = next; state = "open"; emitState({ state: "open", tunnel: next }); if (previousPort !== next.sandboxPort) { emitState({ state: "port-changed", previousPort, sandboxPort: next.sandboxPort }); } }; const reconnectLoop = async () => { let attempt = 0; for (;;) { let termination = await current.terminated; if (closed) return; if (!RETRY_REASONS.has(termination.reason)) { finish(termination); return; } for (;;) { attempt++; state = "reconnecting"; if (termination.reason === "engine_draining") { // Rolling deploys have already sent HTTP/2 GOAWAY, so reconnect immediately to a healthy engine. attempt = 0; } const delayMs = termination.reason === "engine_draining" ? 0 : jitteredBackoff(initialBackoffMs, maxBackoffMs, attempt); emitState({ state: "reconnecting", reason: termination, attempt, delayMs }); await sleep(delayMs, () => closed, (wake) => { wakeReconnectSleep = wake; }); wakeReconnectSleep = undefined; if (closed) return; try { const next = await exposeHostPort(session, hostAddr, options); attempt = 0; setCurrent(next); break; } catch (error) { if (closed) return; termination = { reason: "transport_error", error }; emitState({ state: "reconnecting", reason: termination, attempt, delayMs: 0, }); } } } }; reconnectLoop().catch((error) => finish({ reason: "transport_error", error })); return { endpoint() { return [current.sandboxAddress, current.sandboxPort]; }, get sandboxPort() { return current.sandboxPort; }, get sandboxAddress() { return current.sandboxAddress; }, get state() { return state; }, async close() { closed = true; wakeReconnectSleep?.(); await current.close(); finish({ reason: "sdk_closed" }); }, terminated: finalTermination, onTerminated(cb) { if (finalTerminationResolved) finalTermination.then(cb).catch(() => { }); else terminationListeners.add(cb); }, onStateChange(cb) { stateListeners.add(cb); }, }; } export async function resilientHostPortTunnel(session, host, port, options) { return exposeHostPortResilient(session, `${host}:${port}`, options); } function jitteredBackoff(initialBackoffMs, maxBackoffMs, attempt) { const base = Math.min(maxBackoffMs, initialBackoffMs * 2 ** Math.max(0, attempt - 1)); const jitter = 0.75 + Math.random() * 0.5; return Math.round(base * jitter); } async function sleep(ms, shouldStop, setWake) { if (shouldStop()) return; await new Promise((resolve) => { const timeout = setTimeout(resolve, ms); setWake(() => { clearTimeout(timeout); resolve(); }); }); } //# sourceMappingURL=tunnel-resilient.js.map