The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(environments): keep SDK stdin out of command argv
JoaoMarcos4479b3369333d6
diff --git a/tools/environments/daytona.py b/tools/environments/daytona.pyindex 9d585ed1035..9791a8477fc 100644--- a/tools/environments/daytona.py+++ b/tools/environments/daytona.py@@ -10,6 +10,8 @@ import mathimport osimport shleximport threading+import tempfile+import uuidfrom pathlib import Path, PurePosixPathfrom tools.environments.base import BaseEnvironment@@ -28,7 +30,7 @@ class DaytonaEnvironment(BaseEnvironment):is wired to sandbox.stop() for interrupts. Shell timeout wrapper kept (SDK timeout unreliable)."""- _stdin_mode = "heredoc"+ _stdin_mode = "payload"def __init__(self, image: str, cwd: str = "/home/daytona", timeout: int = 60, cpu: int = 1,memory: int = 5120, disk: int = 10240, persistent_filesystem: bool = True,@@ -145,11 +147,32 @@ class DaytonaEnvironment(BaseEnvironment):with lock, contextlib.suppress(Exception):sandbox.stop()- shell_cmd = f"bash {'-l ' if login else ''}-c {shlex.quote(cmd_string)}"-def exec_fn() -> tuple[str, int]:- response = sandbox.process.exec(shell_cmd, timeout=timeout)- return (response.result or "", response.exit_code)+ remote_stdin = None+ local_stdin = None+ try:+ command = cmd_string+ if stdin_data is not None:+ remote_stdin = f"/tmp/.hermes-stdin-{uuid.uuid4().hex}"+ with tempfile.NamedTemporaryFile(delete=False) as staged:+ local_stdin = staged.name+ staged.write(stdin_data.encode("utf-8"))+ sandbox.fs.upload_file(local_stdin, remote_stdin)+ quoted_stdin = shlex.quote(remote_stdin)+ command = (+ f"trap 'rm -f -- {quoted_stdin}' EXIT\n"+ f"{{\n{cmd_string}\n}} < {quoted_stdin}"+ )+ shell_cmd = f"bash {'-l ' if login else ''}-c {shlex.quote(command)}"+ response = sandbox.process.exec(shell_cmd, timeout=timeout)+ return (response.result or "", response.exit_code)+ finally:+ if local_stdin:+ with contextlib.suppress(OSError):+ os.unlink(local_stdin)+ if remote_stdin:+ with contextlib.suppress(Exception):+ sandbox.process.exec(f"rm -f -- {shlex.quote(remote_stdin)}")return _ThreadedProcessHandle(exec_fn, cancel_fn=cancel)diff --git a/tools/environments/modal.py b/tools/environments/modal.pyindex 93e03f5382c..08a85ce234a 100644--- a/tools/environments/modal.py+++ b/tools/environments/modal.py@@ -131,7 +131,7 @@ class ModalEnvironment(BaseEnvironment):"""Modal cloud execution via native Modal sandboxes: spawn-per-call via _ThreadedProcessHandlewrapping async SDK calls, cancel_fn wired to sandbox.terminate for interrupt support."""- _stdin_mode = "heredoc"+ _stdin_mode = "payload"_snapshot_timeout = 60 # Modal cold starts can be slow# Modal SDK stdin buffer limit: the command-router path allows 16 MB but the legacy server# path caps at 2 MB, so chunks stay under 2 MB and each is flushed individually via drain().@@ -253,6 +253,8 @@ class ModalEnvironment(BaseEnvironment):def exec_fn() -> tuple[str, int]:async def _do():process = await sandbox.exec.aio(*bash_argv(cmd_string, login), timeout=timeout)+ if stdin_data is not None:+ await _stream_stdin(process, stdin_data, self._STDIN_CHUNK_SIZE)stdout = _as_text(await process.stdout.read.aio())stderr = _as_text(await process.stderr.read.aio())exit_code = await process.wait.aio()diff --git a/tools/environments/vercel_sandbox.py b/tools/environments/vercel_sandbox.pyindex cb4c33b87bb..1768ab14afe 100644--- a/tools/environments/vercel_sandbox.py+++ b/tools/environments/vercel_sandbox.py@@ -14,6 +14,7 @@ import osimport shleximport threadingimport time+import uuidfrom datetime import timedeltafrom functools import cachefrom pathlib import Path@@ -162,7 +163,7 @@ def _is_terminal(status: Any) -> bool:class VercelSandboxEnvironment(BaseEnvironment):"""Vercel cloud sandbox backend."""- _stdin_mode = "heredoc"+ _stdin_mode = "payload"def __init__(self, runtime: str | None = None, cwd: str = DEFAULT_VERCEL_CWD, timeout: int = 60,cpu: float = 1, memory: int = 5120, disk: int = _DEFAULT_CONTAINER_DISK_MB,@@ -339,8 +340,8 @@ class VercelSandboxEnvironment(BaseEnvironment):def _run_bash(self, cmd_string: str, *, login: bool = False, timeout: int = 120, stdin_data: str | None = None):"""``timeout`` is enforced by the base ``_wait_for_process`` via ``cancel_fn`` (the SDK has no- per-exec timeout); ``stdin_data`` is already embedded as a heredoc by the base ``execute()``."""- del timeout, stdin_data+ per-exec timeout). Payload stdin is staged through the SDK so it never becomes a shell argv."""+ del timeoutsandbox, workspace_root, lock = self._require_sandbox(), self._workspace_root, self._lockdef cancel() -> None:@@ -348,8 +349,31 @@ class VercelSandboxEnvironment(BaseEnvironment):self._stop_sandbox(sandbox)def exec_fn() -> tuple[str, int]:- return _result_parts(- sandbox.run_command("bash", ["-lc" if login else "-c", cmd_string], cwd=workspace_root))+ remote_stdin = None+ try:+ command = cmd_string+ if stdin_data is not None:+ remote_stdin = f"/tmp/.hermes-stdin-{uuid.uuid4().hex}"+ _retry_vercel_call(+ "stdin upload",+ lambda: sandbox.write_files([{+ "path": remote_stdin,+ "content": stdin_data.encode("utf-8"),+ }]),+ attempts=_WRITE_RETRY_ATTEMPTS,+ )+ quoted_stdin = shlex.quote(remote_stdin)+ command = (+ f"trap 'rm -f -- {quoted_stdin}' EXIT\n"+ f"{{\n{cmd_string}\n}} < {quoted_stdin}"+ )+ return _result_parts(+ sandbox.run_command("bash", ["-lc" if login else "-c", command], cwd=workspace_root))+ finally:+ if remote_stdin:+ with contextlib.suppress(Exception):+ sandbox.run_command(+ "bash", ["-c", f"rm -f -- {shlex.quote(remote_stdin)}"], cwd=workspace_root)return _ThreadedProcessHandle(exec_fn, cancel_fn=cancel)def cleanup(self):