Skip to content

Agent Zero AI framework

Group multiline terminal commands

Alessandro9837ff34329c
diff --git a/plugins/_code_execution/AGENTS.md b/plugins/_code_execution/AGENTS.md
index 1f79ed4ee..8b6a38dbb 100644
--- a/plugins/_code_execution/AGENTS.md
+++ b/plugins/_code_execution/AGENTS.md
@@ -14,6 +14,7 @@
## Local Contracts
- Keep session concurrency, timeout, streaming, and reset behavior predictable.
+- Execute multi-line terminal input as one current-shell compound so intermediate prompts cannot mark queued work complete; preserve `cd`, exports, and other shell state.
- Terminal reset/close must not hang on foreground commands or shells that ignore SIGTERM.
- Explicitly target local versus SSH execution runtimes.
- Do not hardcode secrets, SSH credentials, or local user paths.
diff --git a/plugins/_code_execution/README.md b/plugins/_code_execution/README.md
index 9c8c114d4..54f27f2cd 100644
--- a/plugins/_code_execution/README.md
+++ b/plugins/_code_execution/README.md
@@ -17,6 +17,7 @@ This plugin provides the code execution tool used by agents for development task
- **Persistent shells**
- Maintains per-session shell state in agent data so subsequent calls can reuse the same terminal session.
+ - Groups multi-line terminal input in the current shell so only the final prompt marks the command complete.
- **Multiple runtimes**
- Dispatches requests based on `runtime`: `terminal`, `python`, `nodejs`, `output`, or `reset`.
- **Remote execution support**
diff --git a/plugins/_code_execution/tools/code_execution_tool.py b/plugins/_code_execution/tools/code_execution_tool.py
index 7441001d6..6b6e58e75 100644
--- a/plugins/_code_execution/tools/code_execution_tool.py
+++ b/plugins/_code_execution/tools/code_execution_tool.py
@@ -27,6 +27,14 @@ def _is_closed_pty_error(exc: BaseException) -> bool:
return False
+def _group_multiline_command(command: str, powershell: bool = False) -> str:
+ body = command.rstrip("\n")
+ if "\n" not in body:
+ return body
+ opener = ". {" if powershell else "{"
+ return f"{opener}\n{body}\n}}"
+
+
@dataclass
class ShellWrap:
id: int
@@ -165,6 +173,9 @@ class CodeExecution(Tool):
+ self.format_command_for_output(command)
+ "\n\n"
)
+ command = _group_multiline_command(
+ command, powershell=runtime.is_windows() and not cfg["ssh_enabled"]
+ )
return await self.terminal_session(cfg, session, command, reset, prefix)
async def terminal_session(
diff --git a/tests/test_code_execution_pager.py b/tests/test_code_execution_pager.py
index a4f2d2325..a5ef70b19 100644
--- a/tests/test_code_execution_pager.py
+++ b/tests/test_code_execution_pager.py
@@ -8,6 +8,7 @@ import asyncio
from plugins._code_execution.helpers import shell_local, shell_ssh
from plugins._code_execution.helpers.tty_session import TTYSession
+from plugins._code_execution.tools.code_execution_tool import _group_multiline_command
def test_local_env_disables_pagers_and_preserves_existing():
@@ -35,6 +36,14 @@ def test_ssh_command_disables_pagers():
assert "PAGER=cat" in shell_ssh.PAGER_DISABLE_COMMAND
+def test_multiline_terminal_commands_are_one_current_shell_compound():
+ assert _group_multiline_command("pwd") == "pwd"
+ assert _group_multiline_command("cd /tmp\npwd") == "{\ncd /tmp\npwd\n}"
+ assert _group_multiline_command("$env:FOO='bar'\n$env:FOO", powershell=True) == (
+ ". {\n$env:FOO='bar'\n$env:FOO\n}"
+ )
+
+
def test_tty_close_kills_term_resistant_process():
async def run():
session = TTYSession("bash -lc 'trap \"\" TERM; sleep 30'")