14Sandbox 与执行
L3P1
Agent 跑 untrusted code,必须 sandbox。常见方案:
Docker(最常见):容器隔离,启动 ~1s。适合 batch agent。Firecracker(AWS Lambda):microVM,启动 ~100ms,隔离强。gVisor:user-space kernel,Google 用,比 container 强,比 VM 轻。原生进程 + chroot:不推荐,逃逸面太大。
文件系统:挂载允许目录,其他 read-only 或不可见。Claude Code 在 macOS 上用 seatbelt 配置 sandbox profile,限制写到 cwd 之外。
超时:每个 tool 单独 timeout 配置。read 30s / bash 60s / network 10s 是合理的初值。命中 timeout 返回 is_error: true,模型决定要不要重试。
并发 vs 串行:read-only 工具默认并发安全。write 工具(edit_file, git_commit)按文件加锁或强制串行。网络白名单:防止 exfiltration——agent 不能把 sandbox 内的数据传到任意外部。
速查
"agent 怎么不会 rm -rf /?"——三层防护:sandbox(物理隔离)+ permission deny rule(规则拦截)+ tool description(明确说哪些是危险操作)。