Skip to content

The agent that grows with you

fix(terminal): trailing-& conversion never tells the model to re-launch the process

Teknium390b1e38d2e0
diff --git a/tests/tools/test_terminal_trailing_amp_auto_background.py b/tests/tools/test_terminal_trailing_amp_auto_background.py
index 18da3d98b2a..126733c4624 100644
--- a/tests/tools/test_terminal_trailing_amp_auto_background.py
+++ b/tests/tools/test_terminal_trailing_amp_auto_background.py
@@ -66,3 +66,21 @@ def test_ambiguous_backgrounding_is_still_refused_unchanged(tmp_path, command, e
patch("tools.terminal_tool._acquire_env", side_effect=AssertionError("must not run")):
result = json.loads(terminal_tool(command=command))
assert expected in result["error"]
+
+
+def test_converted_command_never_gets_relaunch_hint_even_with_another_hint(tmp_path, monkeypatch):
+ """A converted `&` command whose spawn also earns another hint (CI-poller shape) still carries
+ the conversion note and never the silent-background "re-launch" advice (a second copy)."""
+ from tools.terminal_tool import terminal_tool
+ from tools.terminal_tool_background import _HOMEBREW_CI_POLLER_HINT, _SILENT_BACKGROUND_HINT
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "hh"))
+ with patch("tools.terminal_tool._get_env_config", return_value=_env_config(tmp_path)), \
+ patch("tools.terminal_tool._start_cleanup_thread"), \
+ patch("tools.terminal_tool._check_all_guards", return_value={"approved": True}):
+ result = json.loads(terminal_tool(command="echo statusCheckRollup >/dev/null &"))
+
+ assert result.get("error") is None, result
+ assert "trailing & was converted to background=true" in result["auto_backgrounded"]
+ assert _HOMEBREW_CI_POLLER_HINT in result["hint"]
+ assert _SILENT_BACKGROUND_HINT not in result["hint"]
diff --git a/tools/terminal_tool.py b/tools/terminal_tool.py
index 594ee075a51..0b0eacedd7b 100644
--- a/tools/terminal_tool.py
+++ b/tools/terminal_tool.py
@@ -1185,28 +1185,6 @@ def _with_promoted_note(result_json: str, requested_timeout: int) -> str:
return json.dumps(data, ensure_ascii=False)
-_AUTO_BACKGROUND_NOTE = (
- "Ran in the background (the trailing & was converted to background=true; Hermes tracks the "
- "process). Do NOT re-run it. Use process_manage (poll/log/wait/kill, session_id above) to check it."
-)
-
-
-def _with_auto_background_note(result_json: str) -> str:
- """Attach the trailing-`&` conversion note to a spawn result (unchanged on error). The silent-
- background hint is dropped: it says to re-launch with notify, which would start a second copy."""
- try:
- data = json.loads(result_json)
- except (TypeError, ValueError):
- return result_json
- if not isinstance(data, dict) or data.get("error"):
- return result_json
- from tools.terminal_tool_background import _SILENT_BACKGROUND_HINT
- if data.get("hint") == _SILENT_BACKGROUND_HINT:
- data.pop("hint")
- data["auto_backgrounded"] = _AUTO_BACKGROUND_NOTE
- return json.dumps(data, ensure_ascii=False)
-
-
def _acquire_env(plan: _ExecPlan, task_id: Optional[str]) -> Any:
"""Cached env for the task, else create it under the per-task creation lock.
@@ -1497,11 +1475,10 @@ def terminal_tool(
completion_output_chars=_completion_output_chars,
heartbeat_seconds=heartbeat,
persist_on_release=persist_on_release,
+ auto_backgrounded=auto_bg is not None and plan.promoted_from_foreground_timeout is None,
)
if plan.promoted_from_foreground_timeout is not None:
result = _with_promoted_note(result, plan.promoted_from_foreground_timeout)
- elif auto_bg is not None:
- result = _with_auto_background_note(result)
return _metered(None if _host_local else plan, result)
return _metered(None if _host_local else plan, _run_foreground(
command, env, plan,
diff --git a/tools/terminal_tool_background.py b/tools/terminal_tool_background.py
index 2b4579b991e..dda1a5dd460 100644
--- a/tools/terminal_tool_background.py
+++ b/tools/terminal_tool_background.py
@@ -47,6 +47,12 @@ _HOMEBREW_CI_POLLER_HINT = (
'loops.'
)
+# A foreground call whose only backgrounding was a trailing `&` is spawned as background=true.
+_AUTO_BACKGROUND_NOTE = (
+ "Ran in the background (the trailing & was converted to background=true; Hermes tracks the "
+ "process). Do NOT re-run it. Use process_manage (poll/log/wait/kill, session_id above) to check it."
+)
+
_ASYNC_UNSUPPORTED_NOTE = (
'notify_on_complete / watch_patterns are not available in this session — it cannot receive '
'an async completion after the turn ends (a one-shot runner such as `hermes -z`, a cron '
@@ -148,9 +154,13 @@ def spawn_background_process(
heartbeat_seconds: int = 0,
persist_on_release: bool = False,
mounted_host: Optional[str] = None,
+ auto_backgrounded: bool = False,
) -> str:
"""Spawn *command* as a tracked background process and return the JSON result.
+ ``auto_backgrounded`` marks a trailing-`&` conversion: the result carries the conversion note
+ and skips the silent-background hint, whose "re-launch" advice would start a second copy.
+
Never inline-polls ``is_interrupted()``: the spawn detaches and returns
exit_code 0 immediately, so the stale-interrupt kill cannot occur here.
"""
@@ -178,7 +188,9 @@ def spawn_background_process(
result_data["approval"] = approval_note
if pty_disabled_reason:
result_data["pty_note"] = pty_disabled_reason
- if not notify_on_complete and not watch_patterns:
+ if auto_backgrounded:
+ result_data["auto_backgrounded"] = _AUTO_BACKGROUND_NOTE
+ elif not notify_on_complete and not watch_patterns:
result_data["hint"] = _SILENT_BACKGROUND_HINT
if command and _looks_like_homebrew_ci_poller(command):
existing = result_data.get("hint", "")