The Sandbox Code Executor is a secure, multi-provider execution environment designed to run arbitrary Python or JavaScript code within RAGFlow agent workflows. It provides a pluggable architecture that supports local execution, self-managed gVisor-based containers, and SaaS solutions. This system ensures that user-provided scripts are isolated from the host environment while maintaining high-performance data exchange with the workflow engine.
The sandbox system follows a provider-based pattern managed by a central provider manager. It abstracts the underlying execution technology (containers, microVMs, or processes) from the agent components.
The following diagrams illustrate how user-defined logic in the RAGFlow Canvas translates into backend execution entities and how data flows through the system.
Workflow to Execution Mapping
Sources: agent/tools/code_exec.py29 agent/tools/code_exec.py205-208 agent/tools/code_exec.py245-255
Data and Component Integration Flow
Sources: agent/tools/code_exec.py183-199 agent/tools/code_exec.py107-115
RAGFlow supports multiple sandbox backends, configured via system settings. The backend selection determines the level of isolation and resource overhead. In the Go-side implementation (internal/agent/sandbox/), the provider system handles lifecycle management for various platforms.
| Provider | Class / Service | Description |
|---|---|---|
| Self-Managed | SelfManagedProvider | A dedicated service running gVisor-protected containers, implemented in internal/agent/sandbox/self_managed.go. |
| Aliyun | AliyunCodeInterpreterProvider | Integrates with Aliyun Function Compute for serverless microVM execution. |
| Local | LocalProvider | Executes code as a local child process using the host's runtime. Intended for trusted development only. |
| E2B | E2BProvider | Utilizes the E2B sandbox cloud for secure code execution. |
Sources: agent/tools/code_exec.py205-208 internal/agent/sandbox/self_managed.go1-15
The lifecycle of a code execution task is managed through the SandboxProvider interface.
SANDBOX_MAX_MEMORY, SANDBOX_TIMEOUT).self_managed provider, this involves checking for available containers in the pool.main(**arguments) function and serializes the return value to stdout.COMPONENT_EXEC_TIMEOUT (default 60s) agent/tools/exesql.py73Sources: agent/tools/code_exec.py183-199 agent/tools/exesql.py73
The CodeExec tool enforces a strict contract between the sandbox and the engine via the build_code_exec_contract function agent/tools/code_exec.py183-199
content, actual_type, attachments, _ERROR, _ARTIFACTS, _ATTACHMENT_CONTENT, raw_result, _created_time, or _elapsed_time agent/tools/code_exec.py36-48_validate_expected_type recursively checks that the returned data matches the expected type (e.g., String, Number, Array<Object>) agent/tools/code_exec.py149-181The sandbox receives input variables resolved from the workflow context. These are passed as a dictionary to the script's entry point. The CodeExec component uses variable resolution logic to resolve and inject these variables before execution agent/tools/exesql.py102-112
For tools that interact with external databases like ExeSQL, RAGFlow implements an SSRF guard. The assert_host_is_safe function ensures that user-provided hosts do not resolve to internal, loopback, or restricted network addresses agent/tools/exesql.py122-128
The Sandbox is the backbone of the Code component, implemented by the CodeExec class agent/tools/code_exec.py29
If the code generates files (e.g., charts or dataframes), providers collect them. These are base64-encoded and returned in the execution result, then uploaded to the SANDBOX_ARTIFACT_BUCKET in MinIO agent/tools/code_exec.py33 Artifacts are assigned expiration based on SANDBOX_ARTIFACT_EXPIRE_DAYS agent/tools/code_exec.py33
In the Go agent port, the code_exec tool is registered in the global tool registry internal/agent/tool/registry.go37 The NewCodeExecTool provides the Eino-compatible interface for the canvas internal/agent/tool/registry.go37 For testing purposes, CodeExec is also represented in the fixture_stubs.go to allow e2e canvas flows to run without an active sandbox internal/agent/component/fixture_stubs.go173
The sandbox primarily supports Python and Node.js. In the agent canvas, users can select languages and configure parameters that are eventually passed to the CodeExec component. Supported languages are defined in the Language enum agent/tools/code_exec.py205-207
| Error | Root Cause | Resolution |
|---|---|---|
ContractError | Output name is empty, contains ., or is a reserved key agent/tools/code_exec.py51-62 | Rename the output variable in the Code component. |
TimeoutError | Code execution exceeded the defined timeout. | Optimize code or increase COMPONENT_EXEC_TIMEOUT in the environment agent/tools/exesql.py73 |
Type Mismatch | The script returned a type that does not match the component configuration agent/tools/code_exec.py183-192 | Update the expected type in the UI or fix the script return value. |
Sources: agent/tools/code_exec.py51-62 agent/tools/code_exec.py183-192 agent/tools/exesql.py73 internal/agent/tool/registry.go37 internal/agent/component/fixture_stubs.go173
Refresh this wiki
This wiki was recently refreshed. Please wait 4 days to refresh again.