.agents/design/core/ai/sandbox/index.md
状态:当前实现
最后核对:2026-07-16
Agent Sandbox 为 Agent 提供隔离的 Linux 运行环境、文件系统和工具调用能力,同时维护物理实例、业务归属、Provider 生命周期、归档恢复和 Skill 部署。
Sandbox 不负责 Agent 的模型循环、Workflow 调度或 Skill 版本创建。Agent Loop 只持有已经准备好的 SandboxClient,具体实例管理留在 Sandbox 模块。
目录:packages/service/core/ai/sandbox
interface
对 Workflow、API、Skill Edit 暴露稳定入口
|
application
runtime / toolCall / file / resource / archive 编排
|
infrastructure
instance repository / provider adapter / runtime profile / volume
|
provider: opensandbox | sealosdevbox | e2b
约束:
interface/* 引用,不直接依赖 infrastructure。SandboxClient 只用于运行态,可能创建或恢复实例。业务归属统一使用:
sourceType:当前支持 App 和 Skill Edit。sourceId:App id 或 Skill id。userId/chatId:按场景补充会话维度。sandboxId:Provider 侧的物理资源标识,不替代业务归属。当前寻址规则:
| 场景 | sandboxId | 归属 |
|---|---|---|
| App chat | hash(sourceId-userId-chatId) 的前 16 位 | sourceType=app,保留 userId/chatId |
| Skill Edit | 固定 edit-debug sandbox id | sourceType=skillEdit,sourceId 为 skillId |
| Chat Agent Helper | 不支持 Sandbox | 调用时直接报错 |
agent_sandbox_instances 使用 (provider, sandboxId) 唯一索引,并为 source、状态和归档查询建立索引。
旧 appId、type 和 metadata.skillId 字段只用于 4.15.0-beta6 迁移脚本及历史数据识别。新运行态写入和业务查询只使用 sourceType/sourceId,不能新增旧字段兼容分支。
当前 Provider:
opensandboxsealosdevboxe2binfrastructure/provider/runtimeProfile 负责把 Provider 映射为默认镜像、工作目录、HOME、环境变量和创建参数。业务层不能根据 Provider 名称自行拼这些值。
当使用 Sandbox 时必须配置 AGENT_SANDBOX_PROVIDER;未知或缺失 Provider 显式报错。
getSandboxClient 的流程是:
SandboxClient,写入或刷新 running 实例记录。prepareAgentSandboxRuntime 在此之前执行团队 Sandbox 权限检查,并根据标准 chat source 计算 sandboxId。
同一 sandbox 的初始化通过 Redis lease 串行化:
agent-sandbox:init:<sandboxId>
锁覆盖文件部署、entrypoint 和 Skill 扫描,避免并发请求交错修改同一工作区。租约会自动续期,获取失败转换为标准 initializing 错误。
Sandbox 初始化使用 prepareSandbox(context, ...steps) 顺序组合步骤。可复用步骤包括:
user_files。SKILL.md 和读取当前工作目录。具体场景只组合需要的 step,不在通用 prepare 层读取业务数据库。
entrypoint.sh。versionId 记录,只执行一次。当前系统工具集合:
sandbox_shellsandbox_read_filesandbox_write_filesandbox_edit_filesandbox_grepsandbox_findsandbox_lssandbox_get_file_url工具定义和名称位于 packages/global/core/ai/sandbox,执行实现在 application/toolCall。runSandboxTools 统一完成 JSON 参数解析、Zod 校验、工具选择和标准结果转换。
文件/命令输出遵循共享裁剪规则;read file 使用 offset/limit,内容搜索和路径搜索分别使用 grep/find,已经不存在旧 sandbox_search 兼容入口。
普通 Agent runtime 可以把选中的已发布 Skill 版本注入 Sandbox:
SKILL.md,把名称、描述和路径写入 Agent reminder。Skill Edit 复用编辑器 Sandbox 中的当前工作区,不把编辑中的内容当成已发布版本重新下载。
内置 Skill 同步到 Sandbox HOME 下的 .fastgpt/skills/<name>,不进入用户 workspace、编辑器树、导出包或发布包。同步状态按文件内容 etag 记录,内容未变化时跳过覆盖。
Sandbox 文件、ticket、preview、keepalive 等 API 位于 projects/app/src/pages/api/core/ai/sandbox。API 边界负责:
parseApiInput 校验请求。sourceType/sourceId。内部 runtime 接口假定调用方已经完成业务权限检查,但仍会检查团队 Sandbox 能力。
| 能力 | 路径 |
|---|---|
| Runtime 接口 | packages/service/core/ai/sandbox/interface/runtime.ts |
| Tool 接口 | packages/service/core/ai/sandbox/interface/toolCall |
| Runtime client | packages/service/core/ai/sandbox/application/runtime/client.ts |
| 初始化 pipeline | packages/service/core/ai/sandbox/application/runtime/prepare.ts |
| Skill runtime | packages/service/core/ai/sandbox/application/runtime/skill |
| 资源服务 | packages/service/core/ai/sandbox/application/resource.ts |
| 归档服务 | packages/service/core/ai/sandbox/application/archive.ts |
| 实例仓储 | packages/service/core/ai/sandbox/infrastructure/instance |
| Provider profile | packages/service/core/ai/sandbox/infrastructure/provider/runtimeProfile |
Sandbox 改动应按影响范围覆盖: