Files
hermes-agent/tests/tools/test_terminal_background_spawn_failure.py
Teknium cd818a4d05 fix(terminal): report a failed sandbox background launch as an error
spawn_via_env (every non-local backend: docker, ssh, modal, singularity,
daytona, plugin backends such as bubblewrap) turned a launch wrapper that
produced no PID, or an env.execute that raised, into an exited
failed_start ProcessSession and returned it. spawn_background_process
treated any returned session as started, so the model was told
"Background process started" with a session_id that was never
registered (process_manage poll: not_found) and the real reason
(e.g. no bash in the image, sandbox gone) was dropped.

A launch that never started is not a session: spawn_via_env now raises
with the wrapper's exit code and output, like a failed Popen on the local
path, and spawn_background_process's existing error branch returns
exit_code -1 with that reason.
2026-10-21 17:06:34 -07:00

48 lines
1.7 KiB
Python

"""A background launch that never started must be reported as an error, not a session."""
import json
import pytest
from tools.process_registry import process_registry
from tools.terminal_tool_background import spawn_background_process
class _SandboxEnv:
"""Environment-backed (non-local) backend whose launch wrapper cannot run."""
cwd = "/root"
def __init__(self, outcome):
self._outcome = outcome
def get_temp_dir(self):
return "/tmp"
def execute(self, command, **kwargs):
if isinstance(self._outcome, Exception):
raise self._outcome
return self._outcome
@pytest.mark.parametrize("outcome, reason", [
({"output": 'exec: "bash": executable file not found in $PATH', "returncode": 127},
'exec: "bash": executable file not found'),
(RuntimeError("container 808ea027557c is not running"), "container 808ea027557c is not running"),
])
def test_failed_sandbox_launch_returns_error_not_started_session(outcome, reason):
before = set(process_registry._running) | set(process_registry._finished)
result = json.loads(spawn_background_process(
command="sleep 30", env=_SandboxEnv(outcome), env_type="docker",
effective_task_id="bg-fail", task_id="bg-fail", session_key="", workdir=None, cwd="/root",
effective_pty=False, notify_on_complete=True, watch_patterns=None, approval_note=None,
pty_disabled_reason=None,
))
assert result["exit_code"] == -1
assert "session_id" not in result
assert result["output"] != "Background process started"
assert reason in result["error"]
assert set(process_registry._running) | set(process_registry._finished) == before