运行时系统为 OpenHands 代理的安全代码执行提供了沙箱执行环境。它实现了一个抽象层,允许代理在本地开发、容器化环境和云平台等多种部署场景下的隔离环境中执行 shell 命令、读写文件、浏览网页和运行代码。
有关特定运行时实现的信息,请参阅 运行时实现。有关沙箱环境中操作执行的详细信息,请参阅 操作执行服务器。有关运行时镜像构建和模板化,请参阅 运行时构建。
运行时系统遵循客户端-服务器架构,其中 OpenHands 后端通过 HTTP API 与隔离的沙箱环境通信。每个运行时实现都提供安全隔离,同时为代理交互维护一致的接口。
运行时系统架构
来源:openhands/runtime/base.py92-121 openhands/runtime/impl/action_execution/action_execution_client.py59-95 openhands/runtime/action_execution_server.py166-214
运行时系统使用继承来提供一致的接口,同时允许针对不同部署环境的专业化实现。
运行时继承结构
来源:openhands/runtime/base.py92-121 openhands/runtime/impl/action_execution/action_execution_client.py59-95 openhands/runtime/impl/docker/docker_runtime.py63-135 openhands/runtime/impl/remote/remote_runtime.py37-102
运行时系统实现了一个操作-观察模式,代理在该模式下发送要在沙箱环境中执行的操作,并接收包含结果的观察。
Action 执行流程
来源:openhands/runtime/impl/action_execution/action_execution_client.py269-331 openhands/runtime/action_execution_server.py384-439 openhands/runtime/utils/bash.py1-50
该系统支持多种运行时实现,以支持不同的部署场景和安全要求。
| 运行时 | 用例 | 隔离性 | 网络访问 |
|---|---|---|---|
DockerRuntime | 生产部署 | 完全容器隔离 | 可配置 |
RemoteRuntime | 云原生部署 | 远程沙箱隔离 | API 控制 |
LocalRuntime | 开发/测试 | 进程隔离 | 主机网络 |
CLIRuntime | 直接执行 | 无隔离 | 完全主机访问 |
ModalRuntime | 无服务器执行 | Modal 沙箱隔离 | 托管 |
使用基于 HTTP 的操作执行服务器的运行时实现继承自 ActionExecutionClient,它提供了
HttpSession 进行的 HTTP 会话管理event_to_dict() 进行的操作序列化observation_from_dict() 进行的观察反序列化copy_from(), copy_to())check_if_alive())get_vscode_token())来源:openhands/runtime/impl/action_execution/action_execution_client.py66-95 openhands/runtime/impl/docker/docker_runtime.py63-135 openhands/runtime/impl/remote/remote_runtime.py37-102
基础 Runtime 类定义了所有实现必须提供的核心接口
沙箱环境中运行的 ActionExecutionServer 公开 HTTP 端点
POST /execute_action - 执行操作并返回观察结果GET /alive - 健康检查端点POST /list_files - 列出沙箱中的文件GET /download_files - 从沙箱下载文件POST /upload_file - 上传文件到沙箱GET /vscode/connection_token - 获取 VSCode 身份验证令牌来源:openhands/runtime/base.py850-876 openhands/runtime/action_execution_server.py1-75
运行时系统通过多种机制管理沙箱环境
运行时使用以下项初始化环境
_default_env_vars() 设置的默认沙箱环境变量config.sandbox.runtime_startup_env_vars 的用户提供环境变量ProviderHandler 设置的 Git 提供程序令牌工作区挂载是通过以下项配置的
config.workspace_mount_path - 主机目录路径config.workspace_mount_path_in_sandbox - 沙箱挂载点(通常是 /workspace)config.workspace_base - 工作区操作的基础目录setup_initial_env() 方法
来源:openhands/runtime/base.py195-202 openhands/runtime/base.py230-304 openhands/core/config/sandbox_config.py6-119
运行时系统支持插件架构以扩展功能
插件通过 PluginRequirement 对象指定,该对象定义了
常见的插件包括
JupyterRequirement - IPython/Jupyter notebook 支持VSCodeRequirement - VSCode 服务器集成AgentSkillsRequirement - 附加代理功能BrowserRequirement - 网页浏览功能ActionExecutionServer 在启动时初始化插件
plugin.initialize() 初始化每个插件来源: openhands/runtime/plugins/__init__.py1-50 openhands/runtime/action_execution_server.py316-328 openhands/runtime/base.py154-157
运行时系统实现了多项安全措施
use_host_network 设置配置网络访问runtime_binding_address 限制端口绑定max_memory_gb 配置内存限制来源: openhands/core/config/sandbox_config.py6-119 openhands/runtime/impl/docker/docker_runtime.py268-387 openhands/runtime/action_execution_server.py94-97