jianmu.execution¶
适用对象:工具执行扩展开发者 / 沙箱维护者 / 核心维护者
是否必读:按需
相关模块:jianmu.tool, jianmu.guard, jianmu.skill
1. 模块职责¶
jianmu.execution 负责“真正执行工具或技能”的运行时层,包括本地执行、Docker 沙箱执行、执行约束以及 runner 解析。
如果你只是在业务层声明工具,通常不需要先操作这里;如果你要控制工具运行环境、隔离策略或技能执行后端,这个模块就是主入口。
2. 适合查什么¶
- 执行约束:
ExecutionConstraints - 本地 / Docker 执行后端:
LocalSandbox、DockerSandbox - 工具执行门面:
ToolRunner - 执行事件上下文:
ExecutionEventContext
3. 使用建议¶
- 普通工具作者通常先看
jianmu.tool,而不是直接从这里入手 - 需要控制执行环境、隔离策略或技能后端时,再进入这一层
- 希望按约束自动选择执行后端时,优先看
ToolRunner.from_execution_constraints()
4. 注意事项¶
- Docker 相关对象依赖宿主环境,不是“导入即能用”
- 应把这里看成执行基础设施层,而不是普通工具作者的第一入口
5. 最小示例¶
from jianmu.execution import ExecutionConstraints
constraints = ExecutionConstraints(
mode="docker",
docker_image="python:3.12-slim",
timeout_s=30,
)
6. 常见入口¶
- 想声明沙箱约束:看
ExecutionConstraints - 想直接用本地执行:看
LocalSandbox - 想接 Docker 隔离:看
DockerSandbox - 想按约束自动解析工具 runner:看
ToolRunner.from_execution_constraints()
7. API 参考¶
约束与协议¶
ExecutionConstraints
dataclass
¶
ExecutionConstraints(
mode: Optional[str] = None,
scope: Optional[str] = None,
docker_image: Optional[str] = None,
network: Optional[str] = None,
memory: Optional[str] = None,
cpus: Optional[float] = None,
timeout_s: Optional[float] = None,
tmpfs_noexec: bool = True,
)
Describe runtime limits and backend preferences for execution.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
mode |
Optional[str]
|
Preferred execution backend such as |
scope |
Optional[str]
|
Optional scope label indicating where the constraints apply. |
docker_image |
Optional[str]
|
Optional Docker image override. |
network |
Optional[str]
|
Optional Docker network mode. |
memory |
Optional[str]
|
Optional Docker memory limit. |
cpus |
Optional[float]
|
Optional Docker CPU limit. |
timeout_s |
Optional[float]
|
Optional timeout in seconds for the execution request. |
tmpfs_noexec |
bool
|
Whether Docker tmpfs mounts should disable execution. |
JobResult
dataclass
¶
Represent the result of a sandboxed script execution.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
stdout |
str
|
Captured standard output emitted by the script. |
stderr |
str
|
Captured standard error emitted by the script. |
exit_code |
int
|
Process exit status returned by the sandbox runtime. |
artifacts |
Dict[str, bytes]
|
Output files collected from the sandbox work directory. |
SandboxRunnerProtocol
¶
Bases: Protocol
Define the execution interface shared by local and Docker sandboxes.
run_tool
async
¶
Execute a single tool call inside the sandbox backend.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
tool
|
Any
|
Tool instance to execute. |
必需 |
args
|
dict | None
|
Optional JSON-like arguments passed to the tool. |
None
|
返回:
| 类型 | 描述 |
|---|---|
Any
|
Tool-specific return value. |
源代码位于: jianmu/execution/base.py
run_job
async
¶
run_job(
script: str,
input_files: Optional[Dict[str, bytes]] = None,
timeout_s: Optional[float] = None,
) -> JobResult
Run a standalone Python script with mounted inputs.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
script
|
str
|
Python source code to execute. |
必需 |
input_files
|
Optional[Dict[str, bytes]]
|
Optional input files keyed by relative path. |
None
|
timeout_s
|
Optional[float]
|
Optional execution timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
JobResult
|
Captured process output plus any generated artifacts. |
源代码位于: jianmu/execution/base.py
本地执行¶
LocalSandbox
¶
Bases: SandboxRunnerProtocol
Local runner: execute tools directly in-process.
run_tool
async
¶
Execute a tool directly in the current Python process.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
tool
|
Any
|
Tool instance to invoke. |
必需 |
args
|
dict | None
|
Optional tool arguments. |
None
|
返回:
| 类型 | 描述 |
|---|---|
Any
|
Tool-specific return value. |
源代码位于: jianmu/execution/local.py
run_job
async
¶
run_job(
script: str,
input_files: Optional[Dict[str, bytes]] = None,
timeout_s: Optional[float] = None,
) -> JobResult
Execute a standalone Python script on the local machine.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
script
|
str
|
Python source code to run. |
必需 |
input_files
|
Optional[Dict[str, bytes]]
|
Optional input files materialized in the temp workdir. |
None
|
timeout_s
|
Optional[float]
|
Optional subprocess timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
JobResult
|
Captured process streams, exit code, and generated artifacts. |
引发:
| 类型 | 描述 |
|---|---|
TimeoutExpired
|
If the operation fails. |
源代码位于: jianmu/execution/local.py
Docker 执行¶
DockerSandbox
¶
DockerSandbox(
constraints: ExecutionConstraints | None = None,
*,
skill_dir: str | Path | None = None,
builtin_tool_names: list[str] | None = None,
)
Bases: SandboxRunnerProtocol
Sandbox runner backed by Docker.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
constraints |
Execution constraints applied to Docker calls. |
|
tool_runner |
Docker-backed runner used for tool and job execution. |
Create a sandbox facade over docker tool and skill runners.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
constraints
|
ExecutionConstraints | None
|
Optional execution constraints for all docker calls. |
None
|
skill_dir
|
str | Path | None
|
Optional skill directory used for tool discovery. |
None
|
builtin_tool_names
|
list[str] | None
|
Optional builtin tool names exposed in docker mode. |
None
|
源代码位于: jianmu/execution/docker.py
run_tool
async
¶
Execute a tool call through the docker tool runner.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
tool
|
Any
|
The tool instance or proxy to run. |
必需 |
args
|
dict | None
|
Optional dictionary of tool arguments. Defaults to None. |
None
|
返回:
| 类型 | 描述 |
|---|---|
Any
|
The execution result of the tool. |
源代码位于: jianmu/execution/docker.py
run_job
async
¶
run_job(
script: str,
input_files: Dict[str, bytes] | None = None,
timeout_s: float | None = None,
) -> JobResult
Execute a standalone Python script in Docker.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
script
|
str
|
The Python script source code to run. |
必需 |
input_files
|
Dict[str, bytes] | None
|
Optional dictionary mapping filenames to their byte content. Defaults to None. |
None
|
timeout_s
|
float | None
|
Optional execution timeout in seconds. Defaults to None. |
None
|
返回:
| 类型 | 描述 |
|---|---|
JobResult
|
A JobResult containing stdout, stderr, exit code, and generated artifacts. |
源代码位于: jianmu/execution/docker.py
工具执行门面¶
ExecutionEventContext
dataclass
¶
ExecutionEventContext(
run_id: str | None = None,
node_name: str | None = None,
tool_call_id: str | None = None,
)
Optional correlation fields attached to execution.* events.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
run_id |
str | None
|
Logical run/session identifier for the surrounding runtime. |
node_name |
str | None
|
Display name of the node that triggered execution. |
tool_call_id |
str | None
|
Provider or runtime tool-call correlation identifier. |
ToolRunner
¶
ToolRunner(
mode: str,
sandbox: Any,
*,
runtime_event_bus: RuntimeEventBus | None = None,
timeout_s: float | None = None,
)
Unified facade for executing tools through local or docker backends.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
mode |
Normalized backend mode such as |
|
_sandbox |
Concrete sandbox implementation handling execution. |
|
_runtime_event_bus |
Optional runtime-semantic event bus for execution events. |
|
_timeout_s |
Optional wall-clock timeout for one tool execution. |
Bind the runner to one concrete sandbox backend.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
mode
|
str
|
Normalized backend mode such as |
必需 |
sandbox
|
Any
|
Concrete sandbox implementation used for execution. |
必需 |
runtime_event_bus
|
RuntimeEventBus | None
|
Optional runtime-semantic event bus that receives
|
None
|
timeout_s
|
float | None
|
Optional wall-clock timeout for one tool execution. |
None
|
源代码位于: jianmu/execution/runner.py
run
async
¶
Execute one tool call through the configured sandbox backend.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
tool
|
Any
|
Tool instance or compatible callable wrapper being executed. |
必需 |
args
|
Any
|
Normalized tool arguments passed to the sandbox backend. |
必需 |
event_context
|
ExecutionEventContext | None
|
Optional correlation metadata copied onto emitted
|
None
|
返回:
| 类型 | 描述 |
|---|---|
Any
|
The tool-specific execution result returned by the sandbox. |
引发:
| 类型 | 描述 |
|---|---|
Exception
|
Re-raises backend execution failures after emitting
|
源代码位于: jianmu/execution/runner.py
from_execution_constraints
classmethod
¶
from_execution_constraints(
execution: Optional[ExecutionConstraints | dict] = None,
*,
builtin_names: Optional[Iterable[str]] = None,
skill_dir: Optional[str] = None,
runtime_event_bus: RuntimeEventBus | None = None,
) -> Optional["ToolRunner"]
Resolve a concrete tool runner from execution constraints and config.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
execution
|
Optional[ExecutionConstraints | dict]
|
Optional execution constraints or raw dictionary payload. |
None
|
builtin_names
|
Optional[Iterable[str]]
|
Optional builtin tool names used by Docker backends to expose local tool shims inside the container runtime. |
None
|
skill_dir
|
Optional[str]
|
Optional skill directory used by Docker backends when resolving skill-relative execution context. |
None
|
runtime_event_bus
|
RuntimeEventBus | None
|
Optional runtime-semantic event bus that receives
|
None
|
返回:
| 类型 | 描述 |
|---|---|
Optional['ToolRunner']
|
A configured |
Optional['ToolRunner']
|
execution mode can be resolved. |