Skip to content

The agent that grows with you

fix(tui): the stdio gateway's RPC pipe never reaches a child on Windows

Tekniumbe95d40a7e93
diff --git a/tests/tui_gateway/test_entry_rpc_stdin_windows.py b/tests/tui_gateway/test_entry_rpc_stdin_windows.py
new file mode 100644
index 00000000000..7ee904bcd17
--- /dev/null
+++ b/tests/tui_gateway/test_entry_rpc_stdin_windows.py
@@ -0,0 +1,51 @@
+"""The stdio TUI gateway's RPC pipe never reaches a child launched without ``stdin=`` on Windows.
+
+``subprocess`` gives such a child a duplicate of the process's standard input, so the gateway
+reads RPC from a private handle and leaves NUL as standard input (``_private_rpc_stdin_windows``).
+"""
+
+from __future__ import annotations
+
+import os
+import subprocess
+import sys
+import textwrap
+from pathlib import Path
+
+import pytest
+
+pytestmark = pytest.mark.platforms("windows")
+
+REPO_ROOT = Path(__file__).resolve().parents[2]
+
+_GATEWAY = textwrap.dedent("""
+ import subprocess, sys
+ from tui_gateway.entry import _close_rpc_stdin_on_exec
+ _close_rpc_stdin_on_exec()
+ try:
+ child = subprocess.run(
+ [sys.executable, "-c", "import sys; sys.stdout.write(repr(sys.stdin.read()))"],
+ capture_output=True, text=True, timeout=20)
+ seen = child.stdout
+ except subprocess.TimeoutExpired:
+ seen = "TIMEOUT"
+ print("child saw:", seen, flush=True)
+ print("gateway read:", repr(sys.stdin.readline()), flush=True)
+""")
+
+
+def test_child_without_stdin_gets_nul_and_the_gateway_keeps_its_requests(tmp_path: Path) -> None:
+ env = {**os.environ, "PYTHONPATH": str(REPO_ROOT), "HERMES_HOME": str(tmp_path / "home")}
+ proc = subprocess.Popen([sys.executable, "-c", _GATEWAY], cwd=tmp_path, env=env,
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
+ assert proc.stdin is not None
+ proc.stdin.write('{"id": "req-1"}\n')
+ proc.stdin.flush() # the pipe stays open, as the TUI's does
+ try:
+ out, err = proc.communicate(timeout=90)
+ except subprocess.TimeoutExpired:
+ proc.kill()
+ out, err = proc.communicate()
+ seen = out + err # importing the gateway points sys.stdout at stderr: stdout is its RPC channel
+ assert "child saw: ''" in seen, f"a child without stdin= read the RPC pipe:\n{seen[-3000:]}"
+ assert "gateway read: '{\"id\": \"req-1\"}\\n'" in seen, f"the gateway lost its request:\n{seen[-3000:]}"
diff --git a/tui_gateway/entry.py b/tui_gateway/entry.py
index 864a6296dde..ff2f9a899d3 100644
--- a/tui_gateway/entry.py
+++ b/tui_gateway/entry.py
@@ -41,9 +41,11 @@ def _close_rpc_stdin_on_exec() -> None:
close-on-exec, so a dependency that launches a child without ``stdin=``
could otherwise consume a request before this process reads it.
- Windows does not provide the POSIX FD_CLOEXEC contract used here; retain
- its existing descriptor behavior rather than changing its launch paths.
+ Windows has no close-on-exec for it: see ``_private_rpc_stdin_windows``.
"""
+ if os.name == "nt":
+ _private_rpc_stdin_windows()
+ return
if os.name != "posix":
return
try:
@@ -54,6 +56,34 @@ def _close_rpc_stdin_on_exec() -> None:
logger.debug("could not mark TUI RPC stdin close-on-exec", exc_info=True)
+def _private_rpc_stdin_windows() -> None:
+ """Read RPC from a private handle and give the process a NUL standard input.
+
+ ``subprocess`` hands a child launched without ``stdin=`` an inheritable
+ duplicate of ``STD_INPUT_HANDLE`` (``Popen._get_handles``), so marking fd 0
+ non-inheritable keeps nothing out. That duplicate is the RPC pipe: such a
+ child can read requests meant for us, and a ``git`` child started while the
+ main loop holds its synchronous ``readline`` on the pipe never returned (the
+ version probe in ``_session_info``, past its 3 s timeout), so the session's
+ agent build never finished and no turn ran.
+ """
+ import ctypes
+ import msvcrt
+
+ try:
+ stdin = sys.stdin
+ rpc = os.fdopen(os.dup(stdin.fileno()), "r", encoding=stdin.encoding,
+ errors=stdin.errors, newline=None) # os.dup: non-inheritable
+ null_fd = os.open(os.devnull, os.O_RDONLY)
+ except (OSError, ValueError, AttributeError):
+ logger.debug("could not give the TUI gateway a private RPC stdin", exc_info=True)
+ return
+ os.dup2(null_fd, 0)
+ os.close(null_fd)
+ ctypes.windll.kernel32.SetStdHandle(-10, msvcrt.get_osfhandle(0)) # type: ignore[attr-defined] # STD_INPUT_HANDLE
+ sys.stdin = rpc
+
+
def _install_sidecar_publisher() -> None:
"""Mirror every dispatcher emit to the dashboard sidebar via WS when set (best-effort)."""
url = os.environ.get("HERMES_TUI_SIDECAR_URL")