The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: warn agents off driving interactive console TUIs via pty on Windows (#84364)
Teknium197a18314fbc
diff --git a/$tmp b/$tmpnew file mode 100644index 00000000000..e69de29bb2ddiff --git a/agent/prompt_builder.py b/agent/prompt_builder.pyindex f234c98c02d..96488fd7488 100644--- a/agent/prompt_builder.py+++ b/agent/prompt_builder.py@@ -1111,7 +1111,13 @@ _WINDOWS_BASH_SHELL_HINT = ("calls. MSYS-style paths like `/c/Users/<user>/...` work alongside ""native `C:\\Users\\<user>\\...` paths. PowerShell builtins ""(`Get-ChildItem`, `$env:FOO`, `Select-String`) will NOT work — use their "- "POSIX equivalents (`ls`, `$FOO`, `grep`)."+ "POSIX equivalents (`ls`, `$FOO`, `grep`). When answering prompts in a "+ "pty background process, use process(submit) — never process(write) "+ "with a bare trailing newline: Enter on a Windows PTY is a carriage "+ "return, and a lone `\\n` is not delivered as a line terminator, so the "+ "child's prompt silently never returns. When a CLI offers a "+ "non-interactive path (flags, `--with-token`, config files, an OAuth "+ "device flow polled with curl), prefer it over driving prompts.")diff --git a/skills/github/github-auth/SKILL.md b/skills/github/github-auth/SKILL.mdindex b5e4057426a..88e29f8d2c4 100644--- a/skills/github/github-auth/SKILL.md+++ b/skills/github/github-auth/SKILL.md@@ -162,6 +162,8 @@ If `gh` is installed, it handles both API access and git credentials in one step### Interactive Browser Login (Desktop)+> **PITFALL (agent-driven sessions on Windows):** when driving `gh auth login` through a pty background process, answer prompts with `process(submit)` — never `process(write)` with a bare `\n`. Enter on a Windows PTY (ConPTY/pywinpty) is a carriage return; a lone `\n` is not delivered as a line terminator, so gh's "Press Enter to open the browser" prompt (a blocking line read) silently never returns and the login hangs. Also note the browser may not open on the user's desktop from a background session — if they report that, fall back to the device flow below.+```bashgh auth login# Select: GitHub.com@@ -169,6 +171,45 @@ gh auth login# Authenticate via browser```+### Manual OAuth Device Flow (no TTY needed — PROVEN)++Fallback when interactive login is impractical (agent-driven sessions, no browser launch, headless). Uses gh's public OAuth client id; the user just enters a code at github.com/login/device. Scopes: `repo,read:org,gist` is the documented minimum for `gh auth login --with-token`; append `,workflow` only if you need to push workflow files.++```bash+# 1. Request a device code (gh's official client_id)+RESP=$(curl -s -X POST -H "Accept: application/json" \+ -d "client_id=178c6fc778ccc68e1d6a&scope=repo,read:org,gist" \+ https://github.com/login/device/code)+DEVICE_CODE=$(echo "$RESP" | sed 's/.*"device_code":"\([^"]*\)".*/\1/')+USER_CODE=$(echo "$RESP" | sed 's/.*"user_code":"\([^"]*\)".*/\1/')+INTERVAL=$(echo "$RESP" | sed 's/.*"interval":\([0-9]*\).*/\1/'); INTERVAL=${INTERVAL:-5}+echo "Tell the user: go to https://github.com/login/device and enter code: $USER_CODE"++# 2. Poll for the token (respect interval; +5s on slow_down; ~15 min expiry).+# Run this loop as a background process and show the user the code first.+while true; do+ sleep "$INTERVAL"+ POLL=$(curl -s -X POST -H "Accept: application/json" \+ -d "client_id=178c6fc778ccc68e1d6a&device_code=${DEVICE_CODE}&grant_type=urn:ietf:params:oauth:grant-type:device_code" \+ https://github.com/login/oauth/access_token)+ case "$POLL" in+ *access_token*)+ # Never echo the token; pipe it straight into gh.+ echo "$POLL" | sed 's/.*"access_token":"\([^"]*\)".*/\1/' | gh auth login --with-token+ gh auth setup-git+ gh auth status+ echo "LOGIN_COMPLETE"; break ;;+ *authorization_pending*) ;; # keep polling+ *slow_down*) INTERVAL=$((INTERVAL + 5)) ;; # back off per GitHub docs+ *expired_token*) echo "CODE_EXPIRED — restart the flow"; exit 1 ;;+ *access_denied*) echo "USER_DENIED"; exit 1 ;;+ *) echo "UNEXPECTED: $POLL"; exit 1 ;;+ esac+done+```++Note: on Windows winget installs, gh lands at `/c/Program Files/GitHub CLI` — add it to PATH in the same shell: `export PATH="$PATH:/c/Program Files/GitHub CLI"`.+### Token-Based Login (Headless / SSH Servers)```bashdiff --git a/tests/tools/test_process_registry.py b/tests/tools/test_process_registry.pyindex 0bd85fb542d..fd4654411b2 100644--- a/tests/tools/test_process_registry.py+++ b/tests/tools/test_process_registry.py@@ -187,6 +187,47 @@ def test_write_stdin_uses_bytes_for_posix_pty(registry):assert written == [b"hello\n"][email protected]_only+def test_submit_stdin_uses_crlf_for_windows_pty(registry):+ """Enter on a Windows PTY is a carriage return, not a bare LF.++ ConPTY cooked input only ends a line on ``\\r``; a bare ``\\n`` through+ pywinpty is never delivered to a blocking line read (Python readline,+ Go bufio.Scanner — the exact hang seen live with ``gh auth login``'s+ "Press Enter to open the browser" prompt). submit_stdin must append+ ``\\r\\n`` for Windows PTY sessions.+ """+ written = []++ class _FakePty:+ def write(self, value):+ written.append(value)++ session = _make_session(sid="pty-win-submit")+ session._pty = _FakePty()+ registry._running[session.id] = session++ result = registry.submit_stdin(session.id, "Y")++ assert result["status"] == "ok"+ assert written == ["Y\r\n"]++[email protected]_only+def test_submit_stdin_keeps_lf_for_windows_pipe(registry):+ """Non-PTY (Popen pipe) sessions keep the plain LF on Windows."""+ session = _make_session(sid="pipe-win-submit")+ fake_stdin = MagicMock()+ session.process = MagicMock()+ session.process.stdin = fake_stdin+ registry._running[session.id] = session++ result = registry.submit_stdin(session.id, "Y")++ assert result["status"] == "ok"+ fake_stdin.write.assert_called_once_with("Y\n")++# =========================================================================# Get / Poll# =========================================================================diff --git a/tools/process_registry.py b/tools/process_registry.pyindex abba9fef159..6d61b3ab690 100644--- a/tools/process_registry.py+++ b/tools/process_registry.py@@ -2169,8 +2169,26 @@ class ProcessRegistry:return {"status": "error", "error": str(e)}def submit_stdin(self, session_id: str, data: str = "") -> dict:- """Send data + newline to a running process's stdin (like pressing Enter)."""- return self.write_stdin(session_id, data + "\n")+ """Send data + newline to a running process's stdin (like pressing Enter).++ On a Windows PTY session the Enter key is a carriage return: ConPTY+ cooked input treats ``\\r`` as end-of-line, and a bare ``\\n`` written+ through pywinpty is NOT delivered as a line terminator — the child's+ blocking line read (Python ``readline()``, Go ``bufio.Scanner`` as in+ ``gh auth login``'s "Press Enter to open the browser" prompt) simply+ never returns and the process hangs while looking healthy. Verified+ empirically via pywinpty 2.0.15: ``\\n`` -> no line, ``\\r`` /+ ``\\r\\n`` -> line delivered. Use ``\\r\\n`` so the child sees both the+ Enter keypress and a conventional newline; POSIX PTYs and Popen pipes+ keep the plain ``\\n``.+ """+ session = self.get(session_id)+ is_windows_pty = bool(+ _IS_WINDOWS and session is not None+ and getattr(session, "_pty", None)+ )+ line_ending = "\r\n" if is_windows_pty else "\n"+ return self.write_stdin(session_id, data + line_ending)def request_close_terminal(self, session_id: str) -> dict:"""Ask the desktop GUI to close the read-only terminal tab mirroring this