Skip to content

The agent that grows with you

refactor(environments): share the staged-stdin path and redirect prefix

kshitijk4poorbc815821f293
diff --git a/tools/environments/base.py b/tools/environments/base.py
index 4bc9f330899..900a66689f9 100644
--- a/tools/environments/base.py
+++ b/tools/environments/base.py
@@ -227,8 +227,11 @@ class BaseEnvironment(ABC):
implement ``_run_bash()`` and ``cleanup()``; the base provides ``execute()`` with
snapshot sourcing, CWD tracking, interrupt handling and timeout enforcement."""
- # Subclasses that embed stdin as a heredoc (Modal, Daytona) set this.
- _stdin_mode: str = "pipe" # "pipe" or "heredoc"
+ # How execute() hands stdin to _run_bash: "pipe" (process stdin, default),
+ # "payload" (passed through as stdin_data; the backend transports it via its
+ # SDK -- Modal/managed Modal stream it, Daytona/Vercel stage a file) or
+ # "heredoc" (embedded in the command; no built-in backend, plugins only).
+ _stdin_mode: str = "pipe" # "pipe" | "payload" | "heredoc"
# True only when commands execute on the SAME host as the Hermes process
# (LocalEnvironment); controller-host facts then describe the execution target.
@@ -431,6 +434,18 @@ class BaseEnvironment(ABC):
return (f"{{\n{command}\n}} << '{delimiter}' < <(IFS= read -r -d '' s || :; printf '%s' \"${{s%?}}\")\n"
f"{stdin_data}\n{delimiter}")
+ def _staged_stdin_path(self) -> str:
+ """Unique sandbox path for staging a payload-mode stdin file."""
+ temp_dir = self.get_temp_dir().rstrip("/") or "/"
+ return f"{temp_dir}/.hermes-stdin-{uuid.uuid4().hex}"
+
+ @staticmethod
+ def _redirect_stdin_from_file(command: str, path: str) -> str:
+ """Prefix ``command`` so the shell reads stdin from the staged ``path`` and
+ unlinks it before ``command`` runs (the shell then owns the payload)."""
+ quoted = shlex.quote(path)
+ return f"exec 0< {quoted} || exit $?\nrm -f -- {quoted} || exit $?\n{command}"
+
# --- Process lifecycle ---
def _wait_for_process(
self, proc: ProcessHandle, timeout: int = 120, *,
diff --git a/tools/environments/daytona.py b/tools/environments/daytona.py
index dd310617475..44a9ee0bac9 100644
--- a/tools/environments/daytona.py
+++ b/tools/environments/daytona.py
@@ -159,18 +159,12 @@ class DaytonaEnvironment(BaseEnvironment):
def exec_fn() -> tuple[str, int]:
command = cmd_string
if stdin_data is not None:
- temp_dir = self.get_temp_dir().rstrip("/") or "/"
- remote_stdin = f"{temp_dir}/.hermes-stdin-{uuid.uuid4().hex}"
+ remote_stdin = self._staged_stdin_path()
sandbox.fs.upload_file(stdin_data.encode("utf-8", "surrogateescape"), remote_stdin)
with lock:
state["staged"] = remote_stdin
sandbox.fs.set_file_permissions(remote_stdin, mode="600")
- quoted_stdin = shlex.quote(remote_stdin)
- command = (
- f"exec 0< {quoted_stdin} || exit $?\n"
- f"rm -f -- {quoted_stdin} || exit $?\n"
- f"{cmd_string}"
- )
+ command = self._redirect_stdin_from_file(cmd_string, remote_stdin)
shell_cmd = f"bash {'-l ' if login else ''}-c {shlex.quote(command)}"
with lock:
if state["cancelled"]:
diff --git a/tools/environments/vercel_sandbox.py b/tools/environments/vercel_sandbox.py
index 8f37d1e1e3c..49066c7f2a8 100644
--- a/tools/environments/vercel_sandbox.py
+++ b/tools/environments/vercel_sandbox.py
@@ -362,8 +362,7 @@ class VercelSandboxEnvironment(BaseEnvironment):
def exec_fn() -> tuple[str, int]:
command = cmd_string
if stdin_data is not None:
- temp_dir = self.get_temp_dir().rstrip("/") or "/"
- remote_stdin = f"{temp_dir}/.hermes-stdin-{uuid.uuid4().hex}"
+ remote_stdin = self._staged_stdin_path()
_retry_vercel_call(
"stdin upload",
lambda: sandbox.write_files([{
@@ -373,12 +372,7 @@ class VercelSandboxEnvironment(BaseEnvironment):
}]),
attempts=_WRITE_RETRY_ATTEMPTS,
)
- quoted_stdin = shlex.quote(remote_stdin)
- command = (
- f"exec 0< {quoted_stdin} || exit $?\n"
- f"rm -f -- {quoted_stdin} || exit $?\n"
- f"{cmd_string}"
- )
+ command = self._redirect_stdin_from_file(cmd_string, remote_stdin)
with lock:
state["staged"] = remote_stdin
with lock: