Agent Shell Layer#
The Agent Shell Layer is the mechanism by which Dify injects a dify.shell runtime layer into agent runs. It consists of two interlocking concerns: a global on/off flag (AGENT_SHELL_ENABLED) and a per-agent config schema that maps AgentSoulConfig fields into the DifyShellLayerConfig structure forwarded to the agent backend.
Global Flag: AGENT_SHELL_ENABLED#
The AGENT_SHELL_ENABLED boolean (default True) in AgentBackendConfig gates whether the Home, Workspace, Sandbox, and Shell runtime layers are injected at all. Both request builder call sites β workflow Agent v2 and Agent App β read dify_config.AGENT_SHELL_ENABLED and pass it as include_shell to the backend request builder. Setting this flag to False suppresses the entire shell layer regardless of per-agent config.
Per-Agent Config Schema#
Per-agent shell configuration is rooted in AgentSoulConfig, which holds three shell-relevant top-level fields:
| Field | Type | Purpose |
|---|---|---|
env | AgentSoulEnvConfig | Global env vars and secret refs for all shell commands |
sandbox | AgentSoulSandboxConfig | Sandbox provider and container config (image, CPU, working dir) |
tools.cli_tools | list[AgentCliToolConfig] | CLI tools to install in the workspace |
AgentSoulEnvConfig holds variables: list[AgentEnvVariableConfig] and secret_refs: list[AgentSecretRefConfig]. The AgentEnvVariableConfig supports multiple name aliases (name, key, env_name, variable) and a value/default field . AgentSecretRefConfig similarly supports name aliases plus value (for inline Composer-provided secrets), ref, credential_id, and provider_credential_id .
AgentSoulSandboxConfig wraps a provider string and an AgentSandboxProviderConfig with optional image, working_dir, env, and cpu fields .
AgentCliToolConfig represents one CLI tool. Key fields:
enabled: bool(defaultTrue)install_commands: list[str]β list of bootstrap commands; aliasesinstall_command,install,setup_command,commandare also acceptedenv: AgentCliToolEnvConfigβ per-tool env vars and secret refsauthorization_status,permission,pre_authorizedβ authorization gatesdangerous,dangerous_command,requires_confirmationβ risk flags;dangerous_acknowledged,dangerous_accepted,risk_accepted,approvedβ acknowledgement flags
Mapping to DifyShellLayerConfig#
The shared build_shell_layer_config function (used by both workflow and Agent App builders) maps AgentSoulConfig to DifyShellLayerConfig:
-
CLI tools β only tools passing
_cli_tool_enabledare included. A tool is excluded if:enabledisFalsepre_authorizedisFalse- Any permission/authorization field resolves to a denied state (
unauthorized,denied,forbidden,invalid,unavailable) - The tool has a danger flag (
dangerous,dangerous_command,requires_confirmationorrisk_level == "dangerous") without a corresponding acknowledgement flag (dangerous_acknowledged,dangerous_accepted,risk_accepted,approved)
-
Env vars β
_shell_env_varscombinesagent_soul.env.variablesand anysecret_refsthat carry an inlinevalue(those are promoted to plain env vars rather than secret refs, since the backend secret ref schema only accepts short backend-managed IDs) . -
Secret refs β
_shell_secret_refemits aDifyShellSecretRefConfigonly for secret refs that have no inlinevalueand carry a backend-managedref,credential_id, orprovider_credential_id. -
Name normalization β
_name_from_mappingresolves the env var name by checking the keysname,key,env_name,variablein order, returning the first non-blank string.
Runtime Behavior (DifyShellLayer)#
Once injected, the DifyShellLayer (type ID dify.shell) exposes four tools to the agent: shell_run, shell_wait, shell_input, and shell_interrupt . The layer:
- Persists only JSON-safe shell session state in
runtime_state; activeShellctlHandlelives on the layer instance during the active resource scope. - Delegates workspace lifecycle to
ShellProvisionProtocol(provision,reattach,destroy). - Sets
HOME=<shell_home_root>/<agent_id>for all shell commands, whereshell_home_rootdefaults to/homeand is configured viaDIFY_AGENT_SHELL_HOME_ROOT. - Redacts all shell output in two passes: per-job JWE token redaction, then regex-pattern redaction from
DIFY_AGENT_SHELL_REDACT_PATTERNS(server-level) andconfig.redact_patterns(per-agent) . The injected JWE token is masked as***in model-facingshell_run,shell_wait, andshell_inputobservations; raw shellctl output and files referenced byoutput_pathremain unchanged. - Optionally injects Agent Stub env vars (
DIFY_AGENT_STUB_API_BASE_URL,DIFY_AGENT_STUB_AUTH_JWE) into user-visibleshell.runjobs when validagent_stub_api_base_urland token factory are provided. Agent Stub authorization is limited to five minutes and does not refresh inside an already-running process. If a command reports that the authorization expired, users should start a new shell tool call and retry the command.
File Upload Recovery#
When using Agent Stub file commands, if file upload successfully uploads the file but creating the public URL fails, the command prints the canonical mapping and exits with an error containing an exact dify-agent file public-url <reference> retry command. Run that command from a new shell tool call to create the public URL without uploading the file again. On success, file public-url prints the complete JSON mapping containing transfer_method, reference, and public_download_url.
Key Entry Points#
| File | Role |
|---|---|
api/configs/extra/agent_backend_config.py | Defines AGENT_SHELL_ENABLED global flag |
api/models/agent_config_entities.py | Per-agent config schemas: AgentSoulConfig, AgentSoulEnvConfig, AgentSoulSandboxConfig, AgentCliToolConfig, etc. |
api/core/workflow/nodes/agent_v2/runtime_request_builder.py | build_shell_layer_config + all private validation helpers (_cli_tool_enabled, _shell_env_vars, _shell_secret_ref, etc.) |
api/core/app/apps/agent_app/runtime_request_builder.py | Agent App call site that applies AGENT_SHELL_ENABLED and build_shell_layer_config |