← Back to Engineering Blog

Pause & Resume for Ephemeral AI Agent Sandboxes Without Leaking API Keys

How we built ~111ms warm-pool gVisor sandboxes for coding agents with built-in WebSocket exec, a credential-injecting egress proxy (600s streaming), and ~1s S3 snapshot pause & resume.

ai-agentssandboxeskubernetesgvisorsecurity

When you run coding agents inside ephemeral cloud containers, three practical problems show up immediately:

  1. Cold starts kill UX: Waiting 5–10 seconds for a container to schedule before an agent can run ls feels sluggish.
  2. Giving raw LLM API keys to untrusted agent code is dangerous: Any prompt injection or rogue dependency inside the sandbox can cat environment variables and exfiltrate your Anthropic or OpenAI keys.
  3. Keeping idle agent Pods alive across long human-in-the-loop pauses wastes RAM and concurrency slots: If a user pauses a coding session overnight—or for a week—you want to free the Pod's CPU and memory while keeping /tmp workspace state intact.

To solve all three without reinventing Kubernetes orchestration, we built Ephemeral Agent Sessions on top of upstream kubernetes-sigs/agent-sandbox (v1.0.12) (Sandbox, SandboxTemplate, SandboxWarmPool, SandboxClaim) and sandbox-router-go—plus three thin platform components.

1. ~111 ms WarmPool Claims & session_only Tool Containers

Developers can mark any Dockerfile project with --session-only (session_only: true). It uses our standard container build pipeline—letting you pack claude, codex, bun, python, git, or gh into a container—while keeping Deployment/web at 0 replicas with zero public web endpoint and zero idle-gateway overhead.

Instead of spinning up Service or Ingress objects per session, sandbox-router (2 replicas with an Informer cache and Ed25519 Scoped-Token v2 auth) routes straight to the claimed Pod's IP. In live WarmPool E2E runs, claiming a fresh gVisor (runsc) sandbox with readOnlyRootFilesystem: true, UID 1000, capabilities.drop: ["ALL"], automountServiceAccountToken: false, and strict Cilium L3/L4 isolation took 0.111s (111 ms), while the WarmPool controller immediately replenished capacity behind it.

2. Built-in Exec (pangea-execd): HTTP NDJSON & Bidirectional WebSockets

Normally, every team building agent sandboxes ends up writing a bespoke HTTP runner inside their Dockerfile. With exec_mode: "builtin", an initContainer (pangea-execd-init) injects a single static Go binary (pangea-execd) into /run/pangea-exec on port 8080, giving any tool image instant remote execution:

  • POST /_pangea/exec: Supports synchronous JSON ({exit_code, stdout, stderr}) as well as stream: true HTTP NDJSON streaming.
  • wss://sandbox.pangea.host/_pangea/exec/ws: Full RFC 6455 bidirectional WebSocket (spawn, stdin, stdin_eof, signal client messages; started, stdout, stderr, exit, error server events)—ideal for interactive coding agents.
  • PUT / GET /_pangea/files?path=...: Scoped read/write file helpers locked inside /tmp with path-traversal and symlink-escape rejection.

3. Credential-Injecting Outbound Egress Proxy (Zero Raw Secrets Inside)

Here is the core rule for running agent code safely: raw project_secrets must never enter the sandbox container's environment variables.

Session pools accept up to 8 egress_proxies routes (upstream, secret_key, header, prefix, env_url, env_token). When a sandbox boots:

  • pangea-worker mints a short-lived Ed25519-signed proxy token (pga_pxy_...) into env_token and points env_url at the in-cluster pangea-egress-proxy Service on port 8080.
  • Cilium CiliumClusterwideNetworkPolicy permits egress to that proxy port while blocking direct Control Plane or LAN access.
  • When the agent calls its LLM SDK, pangea-egress-proxy verifies the pga_pxy_... signature and revocation version, decrypts the real API key outside the sandbox, attaches the upstream auth header, strips pga_pxy_..., blocks private-IP/metadata SSRF (follow_redirects=False), and streams responses back—with a 600s streaming read timeout so long reasoning/tool calls never drop mid-stream.

If a sandbox gets compromised, the attacker only ever sees a scoped, revocable pga_pxy_... proxy token—never your raw API key.

4. 1.04s Pause & Resume Across WarmPool Pods (1–30 Day S3 Snapshots)

Released in pangea-cli 0.1.61a19 (pangea sessions pause / resume), pausing a session saves workspace state while giving cluster capacity back immediately:

  1. On pause (1.04s measured E2E): pangea-worker pulls a bounded, traversal-checked /_pangea/archive/pack tar.gz snapshot of /tmp directories through sandbox-router, uploads it to S3 (1–30 day configurable TTL with automated GC), and deletes the Pod and SandboxClaim. Both Pod CPU/RAM (session_reserved in pangea usage) and concurrency slots free up instantly while paused.
  2. On resume (1.04s measured E2E): Pangea claims a fresh WarmPool Pod, downloads the snapshot from S3, verifies its SHA-256 checksum, unpacks into /tmp via /_pangea/archive/unpack, and hands back a ready session—on a completely different WarmPool Pod with byte-for-byte identical /tmp files.

Building on upstream agent-sandbox kept our platform glue small while giving AI coding agents sub-second warm starts, proxy-isolated credentials, and ~1-second pause/resume across days.