agentscope.workspaceBubblewrapBackend
class BubblewrapBackend(BackendBase)
agentscope/workspace/_bubblewrap/_bubblewrap_backend.py:23–603Bubblewrap Backend,在隔离环境中安全执行代码和工具。位于 agentscope.workspace 包,继承自 BackendBase。核心成员包括 getcwd、exec_shell、start_process、read_file、write_file、write_stream。
Backend that executes commands inside Bubblewrap sandboxes. Declared in agentscope.workspace. Extends BackendBase.
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
def __init__(*, host_workdir: str, host_tmpdir: str, host_cache_dir: str | None = None, workdir: str = SANDBOX_WORKDIR,… | 初始化 `BubblewrapBackend` 实例。 Creates a new instance. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
async def getcwd() -> strasync | 返回 `getcwd` 结果。 Return the backend's default working directory. |
async def exec_shell(command: list[str], *, cwd: str | None = None, timeout: float | None = None) -> ExecResultasync | 执行 `exec_shell` 方法。 Run an argv list inside a fresh Bubblewrap process. |
async def start_process(command: list[str], *, cwd: str | None = None, stdin: Any = asyncio.subprocess.DEVNULL, stdout:…async | 启动 `start_process`。 Start a long-running command inside Bubblewrap. |
async def read_file(path: str) -> bytesasync | 设置读取文件。 Read a sandbox file as raw bytes. |
async def write_file(path: str, data: bytes) -> Noneasync | 执行写入文件相关操作。 Write raw bytes, refusing every symbolic-link final component. |
async def write_stream(path: str, stream: AsyncIterator[bytes]) -> Noneasync | 执行写入流相关操作。 Stream bytes into ``path``, chunk by chunk through ``cat``. |
构造器详情Constructor Detail
BubblewrapBackend
def __init__(*, host_workdir: str, host_tmpdir: str, host_cache_dir: str | None = None, workdir: str = SANDBOX_WORKDIR, share_net: bool = True, env: dict[str, str] | None = None) -> None
初始化 BubblewrapBackend 实例。
Creates a new instance.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
host_workdirkw | str | host workdir(类型 `str`)。The host workdir. |
host_tmpdirkw | str | host tmpdir(类型 `str`)。The host tmpdir. |
host_cache_dirkw默认default None | str | None | host 缓存目录(类型 `str | None`)。The host cache dir. |
workdirkw默认default SANDBOX_WORKDIR | str | workdir(类型 `str`)。The workdir. |
share_netkw默认default True | bool | share net(类型 `bool`)。The share net. |
envkw默认default None | dict[str, str] | None | env(类型 `dict[str, str] | None`)。The env. |
agentscope/workspace/_bubblewrap/_bubblewrap_backend.py:47–117方法详情Method Detail
getcwd
async def getcwd() -> str
返回 getcwd 结果。
Return the backend's default working directory.
agentscope/workspace/_bubblewrap/_bubblewrap_backend.py:119–121exec_shell
async def exec_shell(command: list[str], *, cwd: str | None = None, timeout: float | None = None) -> ExecResult
执行 exec_shell 方法。
Run an argv list inside a fresh Bubblewrap process.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
command | list[str] | 命令(类型 `list[str]`)。The command. |
cwdkw默认default None | str | None | cwd(类型 `str | None`)。The cwd. |
timeoutkw默认default None | float | None | 超时时间(类型 `float | None`)。The timeout. |
agentscope/workspace/_bubblewrap/_bubblewrap_backend.py:123–170start_process
async def start_process(command: list[str], *, cwd: str | None = None, stdin: Any = asyncio.subprocess.DEVNULL, stdout: Any = asyncio.subprocess.DEVNULL, stderr: Any = asyncio.subprocess.DEVNULL) -> asyncio.subprocess.Process
启动 start_process。
Used by :class:BubblewrapWorkspace to keep the MCP gateway as a tracked subprocess instead of daemonizing it inside the sandbox.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
command | list[str] | 命令(类型 `list[str]`)。The command. |
cwdkw默认default None | str | None | cwd(类型 `str | None`)。The cwd. |
stdinkw默认default asyncio.subprocess.DEVNULL | Any | stdin(类型 `Any`)。The stdin. |
stdoutkw默认default asyncio.subprocess.DEVNULL | Any | stdout(类型 `Any`)。The stdout. |
stderrkw默认default asyncio.subprocess.DEVNULL | Any | stderr(类型 `Any`)。The stderr. |
agentscope/workspace/_bubblewrap/_bubblewrap_backend.py:172–196read_file
async def read_file(path: str) -> bytes
设置读取文件。
Files under /workspace and /tmp are read by a command running inside Bubblewrap.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。The path. |
agentscope/workspace/_bubblewrap/_bubblewrap_backend.py:242–281write_file
async def write_file(path: str, data: bytes) -> None
执行写入文件相关操作。
Write raw bytes, refusing every symbolic-link final component.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。The path. |
data | bytes | 数据(类型 `bytes`)。The data. |
agentscope/workspace/_bubblewrap/_bubblewrap_backend.py:283–285write_stream
async def write_stream(path: str, stream: AsyncIterator[bytes]) -> None
执行写入流相关操作。
Stream bytes into path, chunk by chunk through cat.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。The path. |
stream | AsyncIterator[bytes] | 流(类型 `AsyncIterator[bytes]`)。The stream. |
agentscope/workspace/_bubblewrap/_bubblewrap_backend.py:287–293