Skip to content

Agent Zero AI framework

connector: block shell write actions inread-only mode

Alessandrod28c21e1a070
diff --git a/plugins/_a0_connector/extensions/python/message_loop_prompts_after/_78_include_code_execution_remote.py b/plugins/_a0_connector/extensions/python/message_loop_prompts_after/_78_include_code_execution_remote.py
index a80e333cb..72ceb6174 100644
--- a/plugins/_a0_connector/extensions/python/message_loop_prompts_after/_78_include_code_execution_remote.py
+++ b/plugins/_a0_connector/extensions/python/message_loop_prompts_after/_78_include_code_execution_remote.py
@@ -4,7 +4,10 @@ from agent import LoopData
from helpers.extension import Extension
from plugins._a0_connector.helpers.exec_config import build_exec_config
-from plugins._a0_connector.helpers.ws_runtime import select_remote_exec_target_sid
+from plugins._a0_connector.helpers.ws_runtime import (
+ remote_file_metadata_for_sid,
+ select_remote_exec_target_sid,
+)
def _format_timeouts(payload: dict[str, int]) -> str:
@@ -25,9 +28,82 @@ class IncludeCodeExecutionRemote(Extension):
return
context_id = getattr(self.agent.context, "id", "")
- if not context_id or not select_remote_exec_target_sid(context_id):
+ if not context_id:
return
+ sid = select_remote_exec_target_sid(context_id, require_writes=False)
+ if not sid:
+ return
+
+ metadata = remote_file_metadata_for_sid(sid)
+ if metadata is None:
+ access_mode = "Read&Write (legacy/unknown)"
+ write_runtime_guidance = (
+ "- `runtime=terminal`, `python`, `nodejs`, and `input` are expected to be "
+ "available, but this CLI did not advertise an explicit F3 access mode.\n"
+ "- Use shell syntax that matches the remote host (for example, PowerShell on "
+ "Windows)."
+ )
+ write_runtime_examples = """```json
+{
+ "tool_name": "code_execution_remote",
+ "tool_args": {
+ "runtime": "terminal",
+ "session": 0,
+ "code": "pwd"
+ }
+}
+```
+
+```json
+{
+ "tool_name": "code_execution_remote",
+ "tool_args": {
+ "runtime": "python",
+ "session": 0,
+ "code": "import os\\nprint(os.getcwd())"
+ }
+}
+```"""
+ elif metadata.get("write_enabled"):
+ access_mode = "Read&Write"
+ write_runtime_guidance = (
+ "- `runtime=terminal`, `python`, `nodejs`, and `input` may modify files on "
+ "the remote CLI machine. Use them only when shell-backed execution is the "
+ "right tool for the job.\n"
+ "- Use shell syntax that matches the remote host (for example, PowerShell on "
+ "Windows)."
+ )
+ write_runtime_examples = """```json
+{
+ "tool_name": "code_execution_remote",
+ "tool_args": {
+ "runtime": "terminal",
+ "session": 0,
+ "code": "pwd"
+ }
+}
+```
+
+```json
+{
+ "tool_name": "code_execution_remote",
+ "tool_args": {
+ "runtime": "python",
+ "session": 0,
+ "code": "import os\\nprint(os.getcwd())"
+ }
+}
+```"""
+ else:
+ access_mode = "Read only"
+ write_runtime_guidance = (
+ "- `runtime=terminal`, `python`, `nodejs`, and `input` are disabled while "
+ "local access is Read only. Press F3 to switch the host machine to Read&Write "
+ "before starting new shell-backed work that could modify files."
+ )
+ write_runtime_examples = ""
+
exec_config = build_exec_config(agent=self.agent)
code_exec_timeouts = exec_config.get("code_exec_timeouts")
output_timeouts = exec_config.get("output_timeouts")
@@ -36,6 +112,9 @@ class IncludeCodeExecutionRemote(Extension):
prompt = self.agent.read_prompt(
"agent.extras.code_execution_remote.md",
+ access_mode=access_mode,
+ write_runtime_guidance=write_runtime_guidance,
+ write_runtime_examples=write_runtime_examples,
code_exec_timeouts=_format_timeouts(
code_exec_timeouts if isinstance(code_exec_timeouts, dict) else {}
),
diff --git a/plugins/_a0_connector/helpers/ws_runtime.py b/plugins/_a0_connector/helpers/ws_runtime.py
index 0a3ce1311..020858918 100644
--- a/plugins/_a0_connector/helpers/ws_runtime.py
+++ b/plugins/_a0_connector/helpers/ws_runtime.py
@@ -263,7 +263,7 @@ def remote_exec_metadata_for_sid(sid: str) -> dict[str, Any] | None:
}
-def select_remote_exec_target_sid(context_id: str) -> str | None:
+def select_remote_exec_target_sid(context_id: str, *, require_writes: bool = False) -> str | None:
with _state_lock:
subscribers = sorted(_context_subscriptions.get(context_id, set()))
fallback_sid: str | None = None
@@ -274,6 +274,12 @@ def select_remote_exec_target_sid(context_id: str) -> str | None:
fallback_sid = sid
continue
if metadata.enabled:
+ if require_writes:
+ file_metadata = _sid_remote_file_metadata.get(sid)
+ if file_metadata is not None and (
+ not file_metadata.enabled or not file_metadata.write_enabled
+ ):
+ continue
return sid
return fallback_sid
diff --git a/plugins/_a0_connector/prompts/agent.extras.code_execution_remote.md b/plugins/_a0_connector/prompts/agent.extras.code_execution_remote.md
index 631b65978..992343c9e 100644
--- a/plugins/_a0_connector/prompts/agent.extras.code_execution_remote.md
+++ b/plugins/_a0_connector/prompts/agent.extras.code_execution_remote.md
@@ -1,6 +1,7 @@
## code_execution_remote guidance
Remote code execution is currently available in this context through the connected CLI.
+Current local access mode: `{{access_mode}}`
Execution config:
- code execution timeouts: `{{code_exec_timeouts}}`
@@ -16,6 +17,7 @@ Execution config:
- `runtime=input` is only a deprecated compatibility alias for sending one line of keyboard input into a running shell session.
- Frontend execution may still be locally disabled in the CLI session. If so, expect a structured `{ok: false}` error instead of a fallback runtime.
- Prefer concise, self-checking commands. For multi-step work, inspect output and continue in the same session instead of restarting from scratch.
+{{write_runtime_guidance}}
Examples:
@@ -23,9 +25,8 @@ Examples:
{
"tool_name": "code_execution_remote",
"tool_args": {
- "runtime": "terminal",
- "session": 0,
- "code": "pwd && ls -la"
+ "runtime": "output",
+ "session": 0
}
}
```
@@ -34,19 +35,11 @@ Examples:
{
"tool_name": "code_execution_remote",
"tool_args": {
- "runtime": "python",
+ "runtime": "reset",
"session": 0,
- "code": "import os\nprint(os.getcwd())"
+ "reason": "Start a clean shell for the next step."
}
}
```
-```json
-{
- "tool_name": "code_execution_remote",
- "tool_args": {
- "runtime": "output",
- "session": 0
- }
-}
-```
+{{write_runtime_examples}}
diff --git a/plugins/_a0_connector/prompts/agent.system.tool.code_execution_remote.md b/plugins/_a0_connector/prompts/agent.system.tool.code_execution_remote.md
index 0b6bd32b1..9e7823439 100644
--- a/plugins/_a0_connector/prompts/agent.system.tool.code_execution_remote.md
+++ b/plugins/_a0_connector/prompts/agent.system.tool.code_execution_remote.md
@@ -9,6 +9,9 @@ subscribed CLI, so the base system prompt stays small when remote execution is n
- The CLI client must support `connector_exec_op`.
- Frontend execution may be locally disabled in the CLI session; in that case the result is
a structured `{ok: false}` error and no fallback runtime is used.
+- Mutating runtimes (`terminal`, `python`, `nodejs`, and `input`) also require the CLI
+ session to advertise local access mode `Read&Write` via F3. `output` and `reset` can
+ still be used for existing sessions while the CLI is in `Read only`.
## Arguments
- `runtime`: one of `terminal`, `python`, `nodejs`, `output`, `reset`
@@ -25,4 +28,5 @@ Runtime-specific fields:
- Session state is frontend-local and shell-backed.
- `output` is for long-running operations where a prior call returned control before the
shell reached a prompt.
+- Use shell syntax that matches the remote host (for example, PowerShell on Windows).
- The transport uses `connector_exec_op` and `connector_exec_op_result` with shared `op_id`.
diff --git a/plugins/_a0_connector/tools/code_execution_remote.py b/plugins/_a0_connector/tools/code_execution_remote.py
index 5d6f585bd..f69757283 100644
--- a/plugins/_a0_connector/tools/code_execution_remote.py
+++ b/plugins/_a0_connector/tools/code_execution_remote.py
@@ -11,6 +11,8 @@ from helpers.ws_manager import ConnectionNotFoundError, get_shared_ws_manager
from plugins._a0_connector.helpers.ws_runtime import (
clear_pending_exec_op,
+ remote_exec_metadata_for_sid,
+ remote_file_metadata_for_sid,
select_remote_exec_target_sid,
store_pending_exec_op,
subscribed_sids_for_context,
@@ -24,6 +26,10 @@ EXEC_OP_EVENT = "connector_exec_op"
class CodeExecutionRemote(Tool):
"""Send shell-backed frontend execution operations to the connected CLI machine."""
+ @staticmethod
+ def _runtime_requires_write_access(runtime: str) -> bool:
+ return runtime in {"terminal", "python", "nodejs", "input"}
+
def get_log_object(self):
import uuid
@@ -62,11 +68,36 @@ class CodeExecutionRemote(Tool):
context_id = self.agent.context.id
subscribers = subscribed_sids_for_context(context_id)
- sid = select_remote_exec_target_sid(context_id)
+ require_writes = self._runtime_requires_write_access(runtime)
+ sid = select_remote_exec_target_sid(context_id, require_writes=require_writes)
if not sid:
+ exec_enabled = False
+ write_blocked = False
+ for subscriber_sid in subscribers:
+ exec_metadata = remote_exec_metadata_for_sid(subscriber_sid)
+ if exec_metadata is None:
+ exec_enabled = True
+ continue
+ if not exec_metadata.get("enabled"):
+ continue
+ exec_enabled = True
+ if not require_writes:
+ break
+ file_metadata = remote_file_metadata_for_sid(subscriber_sid)
+ if file_metadata is not None and (
+ not file_metadata.get("enabled", True)
+ or not file_metadata.get("write_enabled")
+ ):
+ write_blocked = True
+
return Response(
message=(
- "code_execution_remote: no subscribed CLI in this context currently has "
+ "code_execution_remote: no subscribed CLI in this context currently allows "
+ "shell-backed execution that may modify local files. Press F3 to switch "
+ "the CLI to Read&Write. `runtime=output` and `runtime=reset` remain "
+ "available for existing sessions."
+ if subscribers and require_writes and exec_enabled and write_blocked
+ else "code_execution_remote: no subscribed CLI in this context currently has "
"remote execution enabled. Connect the CLI and press F4 to switch exec on."
if subscribers
else "code_execution_remote: no CLI client connected to this context. "