HomeAgents 代理Multi-Agent Sandbox

Multi-Agent Sandbox

安全的多代理协作环境

概述

Multi-Agent Sandbox 是一个专门为多代理协作设计的隔离环境。 它允许多个 AI 代理在同一个安全空间中并行工作,共享资源,相互通信, 同时保持与主系统的隔离。

这个沙箱特别适合需要多个专业代理协同完成的复杂任务,如软件开发、 数据分析流水线、内容创作工作流等。

多代理并行

多个代理同时工作,提高效率

协作通信

代理之间可以相互通信协调

安全隔离

与主系统完全隔离,安全可控

架构设计

沙箱结构

┌─────────────────────────────────────────────────────┐
│                  Multi-Agent Sandbox                 │
│                                                      │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐          │
│  │ Agent 1  │  │ Agent 2  │  │ Agent 3  │          │
│  │ 开发者   │  │ 测试员   │  │ 审查员   │          │
│  └────┬─────┘  └────┬─────┘  └────┬─────┘          │
│       │             │             │                 │
│  ┌────┴─────────────┴─────────────┴────┐           │
│  │           消息总线 (Message Bus)      │           │
│  └────┬─────────────┬─────────────┬────┘           │
│       │             │             │                 │
│  ┌────┴────┐   ┌────┴────┐   ┌────┴────┐          │
│  │共享存储 │   │ 工作空间 │   │  日志   │          │
│  └─────────┘   └─────────┘   └─────────┘          │
└─────────────────────────────────────────────────────┘

核心组件

组件功能
Agent Pool管理沙箱中的所有代理实例
Message Bus代理间通信的消息传递系统
Shared Storage代理共享的文件和数据存储
Orchestrator协调代理执行顺序和依赖
Monitor监控代理状态和资源使用

代理角色

  • Coordinator - 协调者,管理整体工作流
  • Worker - 工作者,执行具体任务
  • Reviewer - 审查者,检查其他代理的工作
  • Observer - 观察者,只读访问,用于监控

创建沙箱

快速创建

# 创建一个新的多代理沙箱
openclaw sandbox create my-project-sandbox

# 使用模板创建
openclaw sandbox create --template dev-team my-sandbox

# 指定代理数量
openclaw sandbox create --agents 3 collaborative-sandbox

配置文件创建

# ~/.openclaw/sandboxes/dev-team.yaml

name: dev-team
description: "软件开发团队沙箱"

agents:
  - name: developer
    role: worker
    skills: [coding, debugging]
    system_prompt: "你是一个经验丰富的开发者"

  - name: tester
    role: worker
    skills: [testing, qa]
    system_prompt: "你是一个严格的测试工程师"

  - name: reviewer
    role: reviewer
    skills: [code-review, best-practices]
    system_prompt: "你是一个资深代码审查员"

  - name: pm
    role: coordinator
    skills: [planning, coordination]
    system_prompt: "你是项目经理,负责协调团队"

shared_resources:
  - type: filesystem
    path: /workspace
  - type: database
    name: project-db

在对话中创建

你: /sandbox create "开发一个 REST API"

OpenClaw: 正在创建多代理沙箱...

沙箱已创建: api-dev-sandbox
├─ Agent 1: API 设计师 (architect)
├─ Agent 2: 后端开发者 (developer)
├─ Agent 3: 测试工程师 (tester)
└─ Agent 4: 文档编写者 (documenter)

共享工作空间: ~/.openclaw/sandboxes/api-dev-sandbox/
开始执行任务...

代理协作

通信模式

# 直接消息
Agent 1 → Agent 2: "请审查我的代码"

# 广播消息
Agent 1 → All: "代码已提交,等待审查"

# 订阅模式
Agent 2 订阅 "code-commit" 事件
Agent 1 发布 "code-commit" 事件 → Agent 2 收到通知

任务分配

sandbox:
  orchestration:
    # 任务队列
    task_queue:
      enabled: true
      distribution: round-robin  # round-robin, priority, capability

    # 工作流定义
    workflow:
      - step: design
        agent: architect
        output: api-spec.yaml

      - step: implement
        agent: developer
        input: api-spec.yaml
        output: src/

      - step: test
        agent: tester
        input: src/
        parallel: true

      - step: document
        agent: documenter
        input: [api-spec.yaml, src/]

协作示例

# 代码审查工作流
[Developer] 提交代码
     │
     ▼
[Reviewer] 收到通知,开始审查
     │
     ├─── 发现问题 ──▶ [Developer] 修复
     │                      │
     │                      ▼
     │              [Reviewer] 再次审查
     │
     └─── 通过 ──▶ [Tester] 开始测试
                        │
                        ▼
                   [PM] 收到完成通知

共享资源

sandbox:
  shared:
    # 共享文件系统
    filesystem:
      path: /workspace
      permissions:
        developer: read-write
        tester: read-only
        reviewer: read-only

    # 共享变量
    variables:
      project_name: "my-api"
      version: "1.0.0"

    # 共享工具
    tools:
      - git
      - npm
      - docker

隔离机制

沙箱边界

  • 网络隔离 - 沙箱有独立的网络空间
  • 文件系统隔离 - 只能访问沙箱内的文件
  • 进程隔离 - 代理进程相互隔离
  • 资源限制 - CPU、内存、磁盘配额

隔离级别

级别技术适用场景
Low进程隔离信任的内部任务
Medium命名空间隔离一般开发任务
High容器隔离不信任的代码
Maximum虚拟机隔离高安全要求

安全策略

sandbox:
  security:
    # 隔离级别
    isolation_level: high

    # 网络策略
    network:
      outbound: restricted
      allowed_hosts:
        - "api.github.com"
        - "registry.npmjs.org"

    # 文件系统策略
    filesystem:
      read_only_paths:
        - /etc
        - /usr
      blocked_paths:
        - /root
        - ~/.ssh

    # 代理间权限
    inter_agent:
      allow_direct_message: true
      allow_file_sharing: true
      require_approval_for:
        - execute_code
        - network_request

应用场景

场景 1: 软件开发团队

# 模拟完整的开发团队
sandbox:
  template: software-team
  agents:
    - product-manager: 需求分析和任务规划
    - architect: 系统设计和技术选型
    - frontend-dev: 前端开发
    - backend-dev: 后端开发
    - qa-engineer: 测试和质量保证
    - devops: 部署和运维

场景 2: 数据分析流水线

sandbox:
  template: data-pipeline
  agents:
    - data-collector: 收集原始数据
    - data-cleaner: 数据清洗
    - analyst: 统计分析
    - visualizer: 可视化
    - reporter: 生成报告

场景 3: 内容创作工坊

sandbox:
  template: content-workshop
  agents:
    - researcher: 资料收集
    - writer: 内容撰写
    - editor: 编辑校对
    - seo-specialist: SEO 优化
    - publisher: 发布管理

场景 4: 安全审计

sandbox:
  template: security-audit
  agents:
    - vulnerability-scanner: 漏洞扫描
    - code-auditor: 代码审计
    - penetration-tester: 渗透测试
    - compliance-checker: 合规检查
    - report-generator: 报告生成

配置选项

完整配置示例

# ~/.openclaw/config.yaml

sandbox:
  # 是否启用多代理沙箱
  enabled: true

  # 默认配置
  defaults:
    isolation_level: medium
    max_agents: 10
    timeout: 3600

  # 资源限制
  limits:
    memory: "4GB"
    cpu: 4
    disk: "20GB"
    network_bandwidth: "100Mbps"

  # 持久化
  persistence:
    enabled: true
    path: ~/.openclaw/sandboxes
    auto_cleanup: true
    cleanup_after: "7d"

  # 日志
  logging:
    level: info
    per_agent: true
    aggregate: true

代理配置模板

# ~/.openclaw/sandbox-templates/agent-defaults.yaml

agent_defaults:
  # 默认资源
  resources:
    memory: "512MB"
    cpu: 1

  # 默认权限
  permissions:
    file_read: true
    file_write: true
    network: false
    execute: true

  # 默认超时
  timeout: 300

  # 错误处理
  on_error: retry
  max_retries: 3

工作流配置

sandbox:
  workflow:
    # 执行模式
    mode: auto  # auto, manual, hybrid

    # 错误处理
    error_handling:
      strategy: continue  # stop, continue, rollback
      notify: coordinator

    # 检查点
    checkpoints:
      enabled: true
      interval: 300
      on_step_complete: true

管理操作

基本操作

# 列出所有沙箱
openclaw sandbox list

# 查看沙箱状态
openclaw sandbox status my-sandbox

# 启动沙箱
openclaw sandbox start my-sandbox

# 停止沙箱
openclaw sandbox stop my-sandbox

# 删除沙箱
openclaw sandbox delete my-sandbox

代理管理

# 查看沙箱中的代理
openclaw sandbox agents my-sandbox

# 添加代理到沙箱
openclaw sandbox add-agent my-sandbox --role worker

# 移除代理
openclaw sandbox remove-agent my-sandbox agent-id

# 重启特定代理
openclaw sandbox restart-agent my-sandbox agent-id

监控和调试

# 实时监控
openclaw sandbox monitor my-sandbox

# 查看日志
openclaw sandbox logs my-sandbox
openclaw sandbox logs my-sandbox --agent developer

# 查看消息
openclaw sandbox messages my-sandbox

# 进入调试模式
openclaw sandbox debug my-sandbox

快照和恢复

# 创建快照
openclaw sandbox snapshot my-sandbox --name before-test

# 列出快照
openclaw sandbox snapshots my-sandbox

# 恢复快照
openclaw sandbox restore my-sandbox before-test

# 导出沙箱
openclaw sandbox export my-sandbox --output sandbox-backup.tar.gz

# 导入沙箱
openclaw sandbox import sandbox-backup.tar.gz

资源管理

# 查看资源使用
openclaw sandbox resources my-sandbox

# 输出示例:
# Sandbox: my-sandbox
# ├─ Memory: 2.1 GB / 4 GB (52%)
# ├─ CPU: 2.3 cores / 4 cores (58%)
# ├─ Disk: 5.6 GB / 20 GB (28%)
# └─ Agents: 4 / 10 active

# 调整资源
openclaw sandbox resize my-sandbox --memory 8GB --cpu 8