Skip to content

The agent that grows with you

fix(tts): hide the console window on Windows command-provider spawns

Brooklyn Nicholson72f51ba66fbb
diff --git a/tests/tools/test_tts_command_providers.py b/tests/tools/test_tts_command_providers.py
index daf11c3a95c..c4b446e9a00 100644
--- a/tests/tools/test_tts_command_providers.py
+++ b/tests/tools/test_tts_command_providers.py
@@ -368,8 +368,9 @@ class TestGenerateCommandTts:
assert result == str(out)
assert out.exists()
# The command copied the input text file over to output, so it
- # contains the original UTF-8 text.
- assert out.read_text(encoding="utf-8") == "hello world"
+ # contains the original UTF-8 text. utf-8-sig reads BOM'd and
+ # BOM-less alike (repo policy: reads utf-8-sig).
+ assert out.read_text(encoding="utf-8-sig") == "hello world"
@pytest.mark.platforms("posix") # POSIX-only timeout semantics
@@ -515,3 +516,137 @@ class TestCommandTtsEnvPassthrough:
) == ["A_KEY", "B_KEY"]
assert _command_provider_env_passthrough({}) == []
assert _command_provider_env_passthrough({"env_passthrough": "A_KEY"}) == []
+
+
+class TestCommandProviderSpawnGroupKwargs:
+ """The command-provider shell's process-group/no-console spawn kwargs.
+
+ On Windows the spawn routes through ``cmd.exe`` (``shell=True``); without
+ ``CREATE_NO_WINDOW`` that child allocates a visible console window flash on
+ every provider run from a windowless host (pythonw gateway, TUI, Desktop).
+ The platform branch is keyed on ``os.name``, so the pure helper takes the
+ platform as data (assertable on every host) and the spawn-site tests run
+ only where the branch is real (``platforms`` gating).
+ """
+
+ def test_nt_branch_hides_the_console_window(self, monkeypatch):
+ from hermes_cli import _subprocess_compat
+ from tools.tts_command_provider import provider_popen_group_kwargs
+
+ # Pin the compat module's host probe so the nt data branch is assertable
+ # on every host (the helper caches IS_WINDOWS from the real platform).
+ monkeypatch.setattr(_subprocess_compat, "IS_WINDOWS", True)
+ kwargs = provider_popen_group_kwargs("nt")
+ # CREATE_NEW_PROCESS_GROUP (0x200) | CREATE_NO_WINDOW (0x08000000):
+ # group so the idle-timeout kill can signal the tree, no window so
+ # the cmd.exe shim doesn't flash a console.
+ assert kwargs["creationflags"] == 0x08000000 | 0x00000200
+ assert "start_new_session" not in kwargs
+
+ def test_nt_branch_matches_the_shared_detach_bundle(self, monkeypatch):
+ from hermes_cli import _subprocess_compat
+ from tools.tts_command_provider import provider_popen_group_kwargs
+
+ monkeypatch.setattr(_subprocess_compat, "IS_WINDOWS", True)
+ assert provider_popen_group_kwargs("nt")["creationflags"] == (
+ _subprocess_compat.windows_detach_flags_without_breakaway()
+ )
+
+ def test_posix_branch_uses_start_new_session(self):
+ from tools.tts_command_provider import provider_popen_group_kwargs
+
+ kwargs = provider_popen_group_kwargs("posix")
+ assert kwargs == {"start_new_session": True}
+
+ @pytest.mark.platforms("windows")
+ def test_popen_kwargs_hide_console_window_windows(self, monkeypatch):
+ captured = {}
+
+ class _Stream:
+ def read(self, size):
+ return ""
+
+ class Proc:
+ returncode = 0
+ stdout = _Stream()
+ stderr = _Stream()
+
+ def wait(self, timeout=None):
+ return 0
+
+ def fake_popen(command, **kwargs):
+ captured.update(kwargs)
+ return Proc()
+
+ monkeypatch.setattr("tools.tts_command_provider.subprocess.Popen", fake_popen)
+
+ result = _run_command_tts("echo hi", timeout=1)
+
+ assert result.returncode == 0
+ # Own process group (tree-kill on idle timeout) AND a hidden console.
+ assert captured["creationflags"] & 0x08000000, "CREATE_NO_WINDOW missing"
+ assert captured["creationflags"] & 0x00000200, "CREATE_NEW_PROCESS_GROUP missing"
+ assert "start_new_session" not in captured
+
+ @pytest.mark.platforms("posix")
+ def test_popen_kwargs_start_new_session_posix(self, monkeypatch):
+ captured = {}
+
+ class _Stream:
+ def read(self, size):
+ return ""
+
+ class Proc:
+ returncode = 0
+ stdout = _Stream()
+ stderr = _Stream()
+
+ def wait(self, timeout=None):
+ return 0
+
+ def fake_popen(command, **kwargs):
+ captured.update(kwargs)
+ return Proc()
+
+ monkeypatch.setattr("tools.tts_command_provider.subprocess.Popen", fake_popen)
+
+ result = _run_command_tts("echo hi", timeout=1)
+
+ assert result.returncode == 0
+ assert captured["start_new_session"] is True
+ assert "creationflags" not in captured
+
+
+class TestIdleKillConsoleHidden:
+ """The idle-timeout process-tree kill must not flash a console either.
+
+ On Windows ``terminate_command_process_tree`` shells out to ``taskkill``
+ (a console-subprocess) without ``CREATE_NO_WINDOW`` — the same bug class
+ as the provider spawn itself, one code path later.
+ """
+
+ @pytest.mark.platforms("windows")
+ def test_idle_kill_taskkill_hides_console_windows(self, monkeypatch):
+ import tools.tts_command_provider as mod
+
+ runs = []
+ real_run = subprocess.run
+
+ def fake_run(cmd, **kwargs):
+ if "taskkill" in cmd:
+ runs.append(kwargs)
+ return real_run(["cmd", "/c", "exit", "0"], **{k: v for k, v in kwargs.items() if k != "capture_output"})
+
+ class Proc:
+ pid = 4242
+ returncode = 0
+
+ def poll(self):
+ return None
+
+ monkeypatch.setattr(mod.subprocess, "run", fake_run)
+
+ mod.terminate_command_process_tree(Proc())
+
+ assert runs, "taskkill was not invoked"
+ assert runs[0].get("creationflags", 0) & 0x08000000, "CREATE_NO_WINDOW missing on taskkill"
diff --git a/tools/tts_command_provider.py b/tools/tts_command_provider.py
index 7cd7762eaa4..399bb512750 100644
--- a/tools/tts_command_provider.py
+++ b/tools/tts_command_provider.py
@@ -101,8 +101,12 @@ def terminate_command_process_tree(proc: subprocess.Popen) -> None:
return
if os.name == "nt":
try:
+ # CREATE_NO_WINDOW: taskkill is a console-subprocess — the kill itself must not
+ # flash a window on windowless hosts, same class of defect as the spawn above.
+ from hermes_cli._subprocess_compat import windows_hide_flags
subprocess.run(["taskkill", "/F", "/T", "/PID", str(proc.pid)], stdout=subprocess.DEVNULL,
- stderr=subprocess.DEVNULL, timeout=5, stdin=subprocess.DEVNULL)
+ stderr=subprocess.DEVNULL, timeout=5, stdin=subprocess.DEVNULL,
+ creationflags=windows_hide_flags())
except Exception:
proc.kill()
return
@@ -132,6 +136,25 @@ def command_failure_detail(exc: subprocess.CalledProcessError) -> str:
return "; ".join(parts) or "no command output"
+def provider_popen_group_kwargs(os_name: str) -> Dict[str, Any]:
+ """Process-group spawn kwargs for a command-provider child, per platform.
+
+ On Windows the shell=True spawn routes through cmd.exe, a console-subsystem
+ shim: with only CREATE_NEW_PROCESS_GROUP it allocates a *visible* console
+ window flash on every provider run from a windowless host (pythonw gateway,
+ TUI, Desktop). The shared detach bundle (hermes_cli._subprocess_compat.
+ windows_detach_flags_without_breakaway) adds CREATE_NO_WINDOW so the child
+ owns a hidden console its descendants inherit instead — deliberately without
+ CREATE_BREAKAWAY_FROM_JOB: this child is foreground-owned and killed by us on
+ idle timeout, so job teardown propagation must keep working. POSIX keeps the
+ session detach (setsid) so the tree can be signalled on idle timeout.
+ """
+ from hermes_cli._subprocess_compat import windows_detach_flags_without_breakaway
+ if os_name == "nt":
+ return {"creationflags": windows_detach_flags_without_breakaway()}
+ return {"start_new_session": True}
+
+
def run_command_provider(
command: str, timeout: float, env_passthrough: Optional[list] = None,
) -> subprocess.CompletedProcess:
@@ -149,10 +172,11 @@ def run_command_provider(
value = resolve_passthrough_value(key, os.environ.get(key))
if value is not None:
scrubbed[key] = value
- # Own process group so the whole tree can be signalled on idle timeout. Lossy UTF-8 decode:
- # locale-mismatched bytes must not raise in the reader threads.
- group = ({"creationflags": getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0)} if os.name == "nt"
- else {"start_new_session": True})
+ # Own process group so the whole tree can be signalled on idle timeout; on Windows the
+ # shared bundle also hides the console (shell=True spawns via cmd.exe, which otherwise
+ # flashes a window on windowless hosts). Lossy UTF-8 decode: locale-mismatched bytes
+ # must not raise in the reader threads.
+ group = provider_popen_group_kwargs(os.name)
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True, encoding="utf-8", errors="replace", env=delegated_child_subprocess_env(scrubbed),
stdin=subprocess.DEVNULL, **group)