当 AI Agent 需要执行代码、操控桌面、运行不可信任务时,沙箱(sandbox)是隔离它与你宿主机的唯一屏障。本报告回答三个问题:cua 是否安装沙箱、当下主流方案是什么、启动开销有多大——并给出一份基于 2026 年真实基准数据的选型地图。
pip install cua 装的是 SDK。云模式零本地占用;本地模式才需要 QEMU / Lume / Docker。还有一条不用 VM 的路径——cua-driver 后台直连你的真实桌面。结论先行:cua 不强制安装任何本地虚拟化软件。它把沙箱抽象成"可插拔的运行时"——你选云、选本地、甚至完全不用 VM。
| 路径 | 本地需要装什么 | 底层运行时 | 适用场景 |
|---|---|---|---|
| 云沙箱(默认) | 无需任何 仅需 pip install cua | cua.ai 托管(远程 VM) | 最快上手,任意机器可跑 |
本地沙箱 local=True | 需 QEMU / Docker 或 macOS 上装 Lume | QEMU / Docker / Lume / Hyper-V | 数据不出本地、离线、省钱 |
| cua-driver(无 VM) | 装一个驱动 App(非虚拟机) | 直连宿主桌面(后台) | Agent 操作你真实的 App,不抢焦点 |
Sandbox = 给 agent 一台隔离的"假电脑"(VM/容器),任务再危险也不碰你的主机。cua-driver = 给 agent 一只"隐形的手",直接操作你真实桌面的 App,靠 SkyLight 私有 API 实现"后台不抢焦点"。前者要装虚拟化,后者不要——但它不提供隔离。
无论底层是 Docker 容器、QEMU 虚拟机、Lume macOS 实例还是云沙箱,上层 API 完全一致——sb.mouse.click(100,200) 在云端 us-east-1 的 VM 和本地 Docker 里行为相同。这是通过三层抽象实现的:
用户自定义的包(winget、shell 命令等),按内容哈希缓存。镜像不可变(immutable),可安全地在并发沙箱启动间共享。
每次沙箱会话新鲜创建的 ephemeral overlay——这才是沙箱真正启动的可写磁盘。任务结束即弃。
沙箱内部还跑着一个 Computer-Server(FastAPI),负责执行 UI 交互与代码;生命周期支持 start / stop / suspend / resume / checkpoint / fork,遥测会记录 sandbox_create(含耗时、OS、image kind、runtime 类型)与 sandbox_destroy。
这是 90% 的人混淆的地方。"Agent 沙箱" 在 2026 年其实分裂成两个市场,服务完全不同的需求。
给 agent 一台"无显示器的 Linux",让它跑 AI 生成的代码、装包、跑脚本。没有 GUI、没有桌面,所以轻、快。
给 agent 一台带显示器、带鼠标的完整操作系统,它要"看见"屏幕、"点击"按钮。重一个数量级,但能操作真实软件。
所有沙箱平台都建立在以下隔离原语之上,区别只是隔离强度 vs 启动速度的权衡:
| 原语 | 原理 | 隔离强度 | 典型冷启动 | 谁在用 |
|---|---|---|---|---|
| Namespace + cgroups | Linux 命名空间隔离进程视图 | 弱 | ~20ms | Docker / runc |
| microVM | 精简内核的迷你虚拟机 | 强 (VM级) | ~125ms | Firecracker / Cloud Hypervisor → E2B, Fly.io |
| 完整 VM | 标准 hypervisor + 完整 guest 内核 | 强 | 150–500ms | Kata Containers / QEMU → 全桌面沙箱 |
| 用户态内核 | 拦截并代理 syscall(无真实内核) | 中强 | ~快,运行时 I/O 有损耗 | gVisor → Modal, Daytona |
| WASM | 能力安全模型,沙箱化指令集 | 中 | 近瞬时 | Wasmtime / Wasmer(受限于系统调用) |
Agent 天生 ephemeral(短命)且 bursty(突发)——每个任务起一个沙箱、用完即弃。这把冷启动延迟推成了核心 SLA。Firecracker 卡中了甜点:VM 级别的强隔离(不像 Docker 共享内核,逃逸风险低)+ 近容器的启动速度(~125ms)。这就是 E2B / Fly.io / CodeSandbox 全都选它的原因。
"Agent 时代的 Linux 内核"会不会出现?E2B 的赌注是——会,而它要成为那个事实底座。
E2B(法律主体 FoundryLabs, Inc.,总部旧金山)由两位捷克创始人 Vasek Mlejnsky(CEO) 与 Tomas Valenta(CTO) 于 2023 年创立。它不是市场调研的产物,而是创始人的切肤之痛:
"两年前我和 Tomas 在做自己的 coding agent 时撞上一个要命的问题——起一个 VM 太慢了。" 由此他们得出判断:Agent 需要属于自己的基础设施,"不是改造现有云,而是全新的东西"。 名字 E2B = Execute to Build。底层果断选了 Firecracker——因为 agent 时代,冷启动就是命脉。
* 94% 为 E2B 自称,指 frontier agentic workflows 场景,非付费客户数。
E2B 在无头代码沙箱品类是事实标杆,但并非全能:
| 需求 | E2B 能不能打 | 更该选谁 |
|---|---|---|
| 快速给 agent 接代码执行 | 强项 | — |
| 需要 GPU / ML 推理 | 不支持 | Modal / Beam |
| 必须企业自托管 | 仅 OSS 自部署 | Daytona(gVisor,企业版) |
| 有状态 agent 极速续跑 | ~150ms | Blaxel(~25ms standby) |
| 给 agent 一台带桌面的完整 OS | 品类不符 | cua(全桌面 GUI 沙箱) |
Every sandbox platform is built on one of five isolation primitives. The choice is always the same trade-off: isolation strength vs. startup speed vs. runtime overhead. Below — the principle, the mechanism, and the cost of each.
Principle — isolate by lying, not by virtualizing. Instead of running a second kernel, you tell the one shared kernel to give a group of processes a privatized, filtered view of the system. The processes believe they own the machine; they only see what the kernel permits.
PID (own process tree), NET (own network stack + routing table), MNT (own mount tree), IPC, UTS (hostname), USER (UID remapping — a fake root), CGROUP, TIME. Created via clone() / unshare() with flags.CAP_NET_BIND_SERVICE); containers drop everything they don't need.kexec_load, keyctl, …).Principle — full hardware isolation (a real guest kernel), but amputated to boot in milliseconds. AWS built Firecracker because QEMU carried too much legacy. The bet: a Virtual Machine Monitor that emulates almost nothing, so there is nothing slow to initialize and almost nothing to exploit.
ioctl. One Firecracker process == one microVM. No BIOS, no firmware.virtio-net, virtio-block, virtio-rng, virtio-vsock, a serial console. No PCI bus enumeration, no legacy devices. Less code → smaller attack surface → faster boot.chroot into an empty root, drop privileges, apply cgroup limits, configure namespaces. Defense-in-depth around the VMM itself.virtio-net (bandwidth + pps) and virtio-block (throughput + IOPS), enforced in the VMM with zero guest cooperation. This is how multi-tenant platforms contain noisy neighbors.It hits the sweet spot: VM-grade isolation (each VM owns its guest kernel — a container escape has no meaning here) at near-container speed. For ephemeral, bursty agent workloads where cold start is the SLA, nothing else competes. Hence E2B, Fly.io, and CodeSandbox all standardize on it.
Principle — give every container its own dedicated, fully-featured virtual machine. To Kubernetes it looks and feels like a container; underneath, each pod is a real VM. You trade startup speed for the strongest boundary in the container world.
Docker / Kubernetes (CRI)
│ OCI spec
▼
kata-runtime ──► virtcontainers (sandbox abstraction layer)
│ creates & manages
▼
┌─────────────────────────────────────┐
│ QEMU / KVM Virtual Machine │ ← the actual "sandbox"
│ ┌─────────────────────────────┐ │
│ │ kata-agent (gRPC) │ │ ← runs inside the guest
│ └─────────────────────────────┘ │
└─────────────────────────────────────┘
▲ stdio (stdin/stdout/stderr)
kata-shim re-attaches to the process inside the VM
kata-proxy / vsock host↔guest control channel (vsock in ≥2.x)
docker exec / kubectl logs work transparently with in-VM processes.Principle — the untrusted workload never touches the host kernel. gVisor (Google, written in Go) implements a user-space version of the Linux kernel interface. Every syscall the application makes is intercepted and emulated in user space. The host kernel only ever sees a normal, unprivileged process issuing a handful of boring syscalls.
┌────────────────────── sandbox ──────────────────────────┐
│ Application (ring 3) │
│ │ syscall instruction → trap │
│ ▼ │
│ ┌─────────┐ emulated ┌────────┐ │
│ │ SENTRY │ ◄──────────────────► │ GOFER │ │
│ │ (user- │ 9P (9p2000.L) │ (file │ │
│ │ space │ over Unix socket │ proxy)│ │
│ │ kernel)│ └────────┘ │
│ └─────────┘ │
└──────────────────────┬──────────────────────────────────┘
│ only safe, mediated syscalls
┌──────▼──────┐
│ HOST KERNEL │ ← the app never reaches this directly
└─────────────┘
futex, mmap, scheduling) are handled entirely inside it.9p2000.L) over a local Unix domain socket.PTRACE_SYSEMU) — works on any Linux, but two context switches per syscall → slow.syscall instruction fires a hardware VM-Exit routed (via MSR_LSTAR) straight into the Sentry. Faster, and the host kernel is bypassed entirely.Principle — isolation is a property of the execution model itself, not a wrapper around it. A WASM module has no ambient authority. It cannot touch the filesystem, the network, or any memory outside its own bounds unless the host explicitly hands it a capability.
These families are increasingly stacked, not chosen: WASM modules run inside Firecracker microVMs for belt-and-suspenders. E2B runs Python processes inside Firecracker. The frontier is composing a fast outer sandbox (microVM) with a strict inner sandbox (WASM / seccomp) — minimum attack surface at every layer.
下图为 对数刻度(因为跨度太大),横向比较各类沙箱的冷启动。颜色区分品类与隔离原语。
cua 全桌面沙箱的"慢"是设计取舍而非缺陷:它要启动整个 OS + 显示服务器 + 桌面环境(Xfce/Kasm),让 agent 能"看见"屏幕。cua 自带的 cold_start_benchmark.py 把阈值定为 🟢<15s / 🟡15–60s / 🔴>60s,衡量的是"从创建到第一张截图"的完整时间——这是 GUI agent 真正关心的指标。它和 E2B 的 150ms 不在一个品类,直接比毫无意义。