Skip to content

The agent that grows with you

fix(tools): send CR for Enter on PTY stdin submissions

Michael Kyed25fcae452bd2
diff --git a/tests/tools/test_process_registry.py b/tests/tools/test_process_registry.py
index 7b2518e73a7..a8ead5db6c7 100644
--- a/tests/tools/test_process_registry.py
+++ b/tests/tools/test_process_registry.py
@@ -622,6 +622,34 @@ class TestStdinHelpers:
proc.stdin.close.assert_called_once()
assert result["status"] == "ok"
+ @pytest.mark.platforms("posix")
+ def test_submit_stdin_pty_mode_sends_cr(self, registry):
+ """PTY Enter is \\r — what a real keyboard sends. Raw-mode TUIs bind
+ Enter to \\r and ignore \\n; canonical-mode children still see \\n via
+ the tty driver's ICRNL mapping.
+ """
+ pty = MagicMock()
+ s = _make_session()
+ s._pty = pty
+ registry._running[s.id] = s
+
+ result = registry.submit_stdin(s.id, "hello")
+
+ pty.write.assert_called_once_with(b"hello\r")
+ assert result["status"] == "ok"
+
+ def test_submit_stdin_pipe_mode_sends_newline(self, registry):
+ proc = MagicMock()
+ proc.stdin = MagicMock()
+ s = _make_session()
+ s.process = proc
+ registry._running[s.id] = s
+
+ result = registry.submit_stdin(s.id, "hello")
+
+ proc.stdin.write.assert_called_once_with("hello\n")
+ assert result["status"] == "ok"
+
def test_close_stdin_allows_eof_driven_process_to_finish(self, registry, tmp_path):
"""PTY mode: writing data + sending EOF lets an EOF-driven child finish.
diff --git a/tools/process_registry.py b/tools/process_registry.py
index a7054a910be..ff93bd5d40f 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -2321,13 +2321,17 @@ class ProcessRegistry(ProcessTerminationMixin, ProcessCheckpointMixin, ProcessOu
return self._stdin_op(session_id, via_pty, via_pipe, {"status": "ok", "bytes_written": len(data)})
def submit_stdin(self, session_id: str, data: str = "") -> dict:
- """Send data + newline to stdin (like pressing Enter).
- On a Windows PTY, Enter is a carriage return: ConPTY treats ``\\r`` as
- end-of-line and a bare ``\\n`` through pywinpty is NOT a line terminator — the
- child's blocking line read (``readline()``, Go ``bufio.Scanner``) never returns
- and the process hangs looking healthy. ``\\r\\n`` gives it both; POSIX keeps ``\\n``."""
+ """Send data + Enter to stdin (like pressing Enter).
+ On a PTY, Enter is a carriage return, the byte a keyboard sends: raw-mode TUIs
+ (prompt_toolkit, Ink, vim) read ``\\n`` as Ctrl+J (a newline in Hermes's own prompt),
+ and canonical-mode children still get ``\\n`` through the tty's ICRNL mapping. On a
+ Windows PTY ConPTY treats ``\\r`` as end-of-line and a bare ``\\n`` through pywinpty
+ is NOT a line terminator, so a blocking line read (``readline()``, Go
+ ``bufio.Scanner``) would hang; ``\\r\\n`` gives it both. Pipes keep ``\\n``."""
session = self.get(session_id)
- return self.write_stdin(session_id, data + ("\r\n" if _IS_WINDOWS and session and session._pty else "\n"))
+ if session is not None and session._pty:
+ return self.write_stdin(session_id, data + ("\r\n" if _IS_WINDOWS else "\r"))
+ return self.write_stdin(session_id, data + "\n")
def request_close_terminal(self, session_id: str) -> dict:
"""Ask the desktop GUI to close this process's read-only terminal tab. Does NOT