Skip to content

Commit 3dee063

Browse files
MaxFreedomPollardteknium1
authored andcommitted
fix(cli): dispatch /background inline instead of queuing it behind the turn
/background (/bg, /btw) exists to start independent work while the current turn keeps running. Typed while the agent was busy it went into _pending_input like ordinary input, and process_loop is blocked inside self.chat() for the whole run, so the background task only started once the foreground turn had finished. That is the one moment it was not needed. /steer had the identical problem and was fixed the same way, by dispatching inline on the UI thread. The command's own CommandDef already declares busy_policy="dispatch"; the gateway honours that, the classic CLI never consulted it. The foreground turn is untouched: no interrupt, no steer, and ordinary non-slash input keeps following the configured busy-input behaviour.
1 parent 221be76 commit 3dee063

2 files changed

Lines changed: 181 additions & 0 deletions

File tree

‎cli.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9595,6 +9595,37 @@ def _should_handle_steer_command_inline(self, text: str, has_images: bool = Fals
95959595
except Exception:
95969596
return False
95979597

9598+
def _should_handle_background_command_inline(
9599+
self, text: str, has_images: bool = False
9600+
) -> bool:
9601+
"""Return True when /background should be dispatched while the agent runs.
9602+
9603+
Same queue problem /steer had. ``/background`` (``/bg``, ``/btw``)
9604+
exists to start independent work *without* waiting for the current
9605+
turn, but a slash command typed while the agent is busy goes into
9606+
``_pending_input``, and ``process_loop`` is blocked inside
9607+
``self.chat()`` for the whole run. The background task therefore only
9608+
starts once the foreground turn has finished, which is the one moment
9609+
it was not needed.
9610+
9611+
The command's own ``CommandDef`` already declares
9612+
``busy_policy="dispatch"``; the gateway honours that, the classic CLI
9613+
never consulted it. Dispatching inline on the UI thread starts the
9614+
background session immediately and leaves the foreground turn running
9615+
untouched: no interrupt, no steer.
9616+
"""
9617+
if not text or has_images or not _looks_like_slash_command(text):
9618+
return False
9619+
if not getattr(self, "_agent_running", False):
9620+
return False
9621+
try:
9622+
from hermes_cli.commands import resolve_command
9623+
base = text.split(None, 1)[0].lower().lstrip('/')
9624+
cmd = resolve_command(base)
9625+
return bool(cmd and cmd.name == "background")
9626+
except Exception:
9627+
return False
9628+
95989629
def _output_console(self):
95999630
"""Use prompt_toolkit-safe Rich rendering once the TUI is live."""
96009631
if getattr(self, "_app", None):
@@ -15202,6 +15233,24 @@ def handle_enter(event):
1520215233
event.app.invalidate()
1520315234
return
1520415235

15236+
# Same treatment for /background (/bg, /btw) while the agent is
15237+
# running. Queuing it defeats the entire point of the command:
15238+
# process_loop is blocked inside self.chat(), so the background
15239+
# task would only start once the foreground turn it was meant to
15240+
# run alongside has already finished (#75221). The foreground
15241+
# turn is left alone: no interrupt, no steer.
15242+
if self._should_handle_background_command_inline(
15243+
text, has_images=has_images
15244+
):
15245+
self.process_command(text)
15246+
event.app.current_buffer.reset(append_to_history=True)
15247+
# Repaint for the same reason as the /steer branch above:
15248+
# process_command() prints through patch_stdout and never
15249+
# invalidates the app, so the submitted text can linger in
15250+
# the input area looking unsent.
15251+
event.app.invalidate()
15252+
return
15253+
1520515254
# Snapshot and clear attached images
1520615255
images = list(self._attached_images)
1520715256
self._attached_images.clear()
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
"""Regression tests for classic-CLI mid-run /background dispatch.
2+
3+
Background
4+
----------
5+
``/background`` (``/bg``, ``/btw``) exists to start independent work while
6+
the current turn keeps running. Typed while the agent was busy it went into
7+
``self._pending_input`` like ordinary input, and ``process_loop`` is blocked
8+
inside ``self.chat()`` for the whole run, so the background task only started
9+
once the foreground turn had finished. That is the one moment it was not
10+
needed (#75221).
11+
12+
``/steer`` had the identical problem and was fixed by dispatching inline on
13+
the UI thread; the command's own ``CommandDef`` already declares
14+
``busy_policy="dispatch"``, which the gateway honours and the classic CLI
15+
never consulted.
16+
17+
These tests exercise the detector without starting a prompt_toolkit app,
18+
mirroring tests/cli/test_cli_steer_busy_path.py.
19+
"""
20+
21+
from __future__ import annotations
22+
23+
import importlib
24+
import sys
25+
from unittest.mock import MagicMock, patch
26+
27+
28+
def _make_cli():
29+
"""Create a HermesCLI instance with prompt_toolkit stubbed out."""
30+
_clean_config = {
31+
"model": {
32+
"default": "anthropic/claude-opus-4.6",
33+
"base_url": "https://openrouter.ai/api/v1",
34+
"provider": "auto",
35+
},
36+
"display": {"compact": False, "tool_progress": "all"},
37+
"agent": {},
38+
"terminal": {"env_type": "local"},
39+
}
40+
clean_env = {"LLM_MODEL": "", "HERMES_MAX_ITERATIONS": ""}
41+
prompt_toolkit_stubs = {
42+
"prompt_toolkit": MagicMock(),
43+
"prompt_toolkit.history": MagicMock(),
44+
"prompt_toolkit.styles": MagicMock(),
45+
"prompt_toolkit.patch_stdout": MagicMock(),
46+
"prompt_toolkit.application": MagicMock(),
47+
"prompt_toolkit.layout": MagicMock(),
48+
"prompt_toolkit.layout.processors": MagicMock(),
49+
"prompt_toolkit.filters": MagicMock(),
50+
"prompt_toolkit.layout.dimension": MagicMock(),
51+
"prompt_toolkit.layout.menus": MagicMock(),
52+
"prompt_toolkit.widgets": MagicMock(),
53+
"prompt_toolkit.key_binding": MagicMock(),
54+
"prompt_toolkit.completion": MagicMock(),
55+
"prompt_toolkit.formatted_text": MagicMock(),
56+
"prompt_toolkit.auto_suggest": MagicMock(),
57+
}
58+
with patch.dict(sys.modules, prompt_toolkit_stubs), patch.dict(
59+
"os.environ", clean_env, clear=False
60+
):
61+
import cli as _cli_mod
62+
63+
_cli_mod = importlib.reload(_cli_mod)
64+
with patch.object(_cli_mod, "get_tool_definitions", return_value=[]), patch.dict(
65+
_cli_mod.__dict__, {"CLI_CONFIG": _clean_config}
66+
):
67+
return _cli_mod.HermesCLI()
68+
69+
70+
class TestBackgroundInlineDetector:
71+
def test_detects_background_when_agent_running(self):
72+
cli = _make_cli()
73+
cli._agent_running = True
74+
assert cli._should_handle_background_command_inline(
75+
"/background inspect the test failures"
76+
) is True
77+
78+
def test_detects_both_aliases(self):
79+
cli = _make_cli()
80+
cli._agent_running = True
81+
assert cli._should_handle_background_command_inline("/bg do work") is True
82+
assert cli._should_handle_background_command_inline("/btw do work") is True
83+
84+
def test_ignores_background_when_agent_idle(self):
85+
"""Idle input falls through to the normal process_loop dispatch."""
86+
cli = _make_cli()
87+
cli._agent_running = False
88+
assert cli._should_handle_background_command_inline("/bg do work") is False
89+
90+
def test_ignores_non_slash_input(self):
91+
cli = _make_cli()
92+
cli._agent_running = True
93+
assert cli._should_handle_background_command_inline("bg without slash") is False
94+
assert cli._should_handle_background_command_inline("") is False
95+
96+
def test_ignores_other_slash_commands(self):
97+
cli = _make_cli()
98+
cli._agent_running = True
99+
assert cli._should_handle_background_command_inline("/steer hello") is False
100+
assert cli._should_handle_background_command_inline("/queue hello") is False
101+
assert cli._should_handle_background_command_inline("/stop") is False
102+
103+
def test_ignores_background_with_attached_images(self):
104+
"""Image payloads take the normal path."""
105+
cli = _make_cli()
106+
cli._agent_running = True
107+
assert cli._should_handle_background_command_inline(
108+
"/bg look at this", has_images=True
109+
) is False
110+
111+
def test_case_and_whitespace_tolerant(self):
112+
cli = _make_cli()
113+
cli._agent_running = True
114+
assert cli._should_handle_background_command_inline("/BG do work") is True
115+
116+
117+
class TestBackgroundBusyPolicyContract:
118+
"""The registry already declares the intent this detector implements."""
119+
120+
def test_background_declares_dispatch_while_busy(self):
121+
from hermes_cli.commands import resolve_command
122+
123+
cmd = resolve_command("background")
124+
assert cmd is not None
125+
assert cmd.busy_policy == "dispatch"
126+
127+
def test_aliases_resolve_to_background(self):
128+
from hermes_cli.commands import resolve_command
129+
130+
for alias in ("bg", "btw"):
131+
cmd = resolve_command(alias)
132+
assert cmd is not None and cmd.name == "background"

0 commit comments

Comments
 (0)