Skip to content

The agent that grows with you

fix(terminal): report a failed sandbox background launch as an error

Tekniumcd818a4d054d
diff --git a/tests/tools/test_process_registry.py b/tests/tools/test_process_registry.py
index f62f7be51bc..7b2518e73a7 100644
--- a/tests/tools/test_process_registry.py
+++ b/tests/tools/test_process_registry.py
@@ -927,15 +927,12 @@ class TestSpawnEnvSanitization:
with patch("tools.process_registry.threading.Thread", return_value=fake_thread), \
patch.object(registry, "_write_checkpoint"):
- session = registry.spawn_via_env(env, "echo hello")
+ with pytest.raises(RuntimeError, match="exited 2 without a PID: syntax error"):
+ registry.spawn_via_env(env, "echo hello")
- assert session.exited is True
- assert session.exit_code == 2
- assert session.pid is None
- assert session.output_buffer == "syntax error"
fake_thread.start.assert_not_called()
# A failed launch must not be exposed as a running/tracked session.
- assert session.id not in registry._running
+ assert not registry._running and not registry._finished
def test_env_poller_quotes_temp_paths_with_spaces(self, registry):
session = _make_session(sid="proc_space")
diff --git a/tests/tools/test_terminal_background_spawn_failure.py b/tests/tools/test_terminal_background_spawn_failure.py
new file mode 100644
index 00000000000..9e6ccc1dbbb
--- /dev/null
+++ b/tests/tools/test_terminal_background_spawn_failure.py
@@ -0,0 +1,47 @@
+"""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
+
+
[email protected]("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
diff --git a/tools/process_registry.py b/tools/process_registry.py
index af1257ac88c..944a8742944 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -1291,24 +1291,16 @@ class ProcessRegistry(ProcessTerminationMixin, ProcessCheckpointMixin, ProcessOu
f"( nohup bash -lc {q(command)} > {q(log_path)} 2>&1; "
f"rc=$?; printf '%s\\n' \"$rc\" > {q(exit_path)} ) & "
f"echo $! > {q(pid_path)} && cat {q(pid_path)}")
- try:
- result = env.execute(bg_command, timeout=timeout, rewrite_compound_background=False)
- output = result.get("output", "").strip()
- session.pid = next((int(ln) for ln in map(str.strip, output.splitlines()) if ln.isdigit()), None)
- # No PID from the wrapper (syntax error, broken redirect): a failed launch,
- # not a fake running session.
- if session.pid is None:
- session.mark_exited(int(result.get("returncode", -1)) or -1, "failed_start", "failed_start")
- session.output_buffer = output
- except Exception as e:
- session.mark_exited(-1, "failed_start", "failed_start")
- session.output_buffer = f"Failed to start: {e}"
- if session.exited:
- with self._lock:
- self._prune_if_needed()
- else:
- self._track_started(
- session, self._env_poller_loop, f"proc-poller-{session.id}", (env, log_path, pid_path, exit_path))
+ result = env.execute(bg_command, timeout=timeout, rewrite_compound_background=False)
+ output = result.get("output", "").strip()
+ session.pid = next((int(ln) for ln in map(str.strip, output.splitlines()) if ln.isdigit()), None)
+ # No PID from the wrapper (no bash in the image, sandbox gone, broken redirect): the
+ # command never started, so raise like a failed Popen instead of returning a session.
+ if session.pid is None:
+ raise RuntimeError(f"launch wrapper exited {result.get('returncode')} without a PID: "
+ f"{output or '(no output)'}")
+ self._track_started(
+ session, self._env_poller_loop, f"proc-poller-{session.id}", (env, log_path, pid_path, exit_path))
return session
# ----- Reader / Poller Threads -----