4. 沙盒执行环境
4.1 沙盒架构
Manus 的核心创新之一是运行在隔离的沙盒环境中:
┌────────────────────────────────────────────────────┐
│ Host System │
│ ┌──────────────────────────────────────────────┐ │
│ │ Manus Orchestrator │ │
│ │ - 接收用户请求 │ │
│ │ - 分配沙盒实例 │ │
│ │ - 管理任务生命周期 │ │
│ └──────────────────────────────────────────────┘ │
│ │ │
│ ┌────────────┴────────────┐ │
│ ▼ ▼ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Sandbox #1 │ │ Sandbox #2 │ │
│ │ ┌───────────┐ │ │ ┌───────────┐ │ │
│ │ │ Linux VM │ │ │ │ Linux VM │ │ │
│ │ │ - Browser │ │ │ │ - Browser │ │ │
│ │ │ - Python │ │ │ │ - Python │ │ │
│ │ │ - Node.js │ │ │ │ - Node.js │ │ │
│ │ │ - Files │ │ │ │ - Files │ │ │
│ │ └───────────┘ │ │ └───────────┘ │ │
│ └─────────────────┘ └─────────────────┘ │
│ │
└────────────────────────────────────────────────────┘4.2 沙盒技术价值
typescript
// 沙盒带来的核心价值
const sandboxBenefits = {
// 1. 安全隔离 - 任务执行不影响主机
security: {
isolation: "完全隔离的执行环境",
noHostAccess: "无法访问主机系统资源",
networkRestriction: "可控的网络访问"
},
// 2. 错误容忍 - 出错可重置
errorTolerance: {
checkpoint: "支持状态快照",
rollback: "可回滚到之前状态",
restart: "任务失败可重新开始"
},
// 3. 无状态化 - 每次任务干净环境
stateless: {
cleanSlate: "每个任务独立环境",
noInterference: "任务间互不影响",
reproducible: "可重现的执行环境"
},
// 4. 资源控制
resourceControl: {
cpuLimit: "CPU 使用限制",
memoryLimit: "内存使用限制",
timeLimit: "执行时间限制"
}
};4.3 沙盒实现示例
typescript
class SandboxManager {
private activeSandboxes: Map<string, Sandbox> = new Map();
async createSandbox(taskId: string): Promise<Sandbox> {
const sandbox = await this.spawnVM({
image: "manus-sandbox:latest",
resources: {
cpu: 2,
memory: "4G",
disk: "20G"
},
timeout: 3600, // 1 hour max
network: {
allowOutbound: true,
allowedDomains: ["*"] // 可配置白名单
}
});
// 预装工具
await sandbox.exec("pip install pandas numpy requests beautifulsoup4");
await sandbox.exec("npm install -g playwright");
this.activeSandboxes.set(taskId, sandbox);
return sandbox;
}
async executInSandbox(
taskId: string,
command: string
): Promise<ExecutionResult> {
const sandbox = this.activeSandboxes.get(taskId);
if (!sandbox) throw new Error("Sandbox not found");
try {
const result = await sandbox.exec(command, {
timeout: 300, // 5 min per command
captureOutput: true
});
return {
success: true,
stdout: result.stdout,
stderr: result.stderr,
exitCode: result.exitCode
};
} catch (error) {
return {
success: false,
error: error.message
};
}
}
async destroySandbox(taskId: string): Promise<void> {
const sandbox = this.activeSandboxes.get(taskId);
if (sandbox) {
// 导出用户需要的文件
await sandbox.exportFiles("/output/*", `./results/${taskId}/`);
// 销毁沙盒
await sandbox.destroy();
this.activeSandboxes.delete(taskId);
}
}
}