The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(pm): turn on VT processing before drawing progress on a Windows console
ethernetbd9e50707143
diff --git a/pm/cli.py b/pm/cli.pyindex 10f3af63b8b..dc290ed99c2 100644--- a/pm/cli.py+++ b/pm/cli.py@@ -60,13 +60,35 @@ def _fmt_bytes(n: int) -> str:return f"{n / (1024 * 1024):.1f} MiB"+def _enable_vt(stream) -> bool:+ """A Windows console prints ESC as a glyph (`←[2K`) until its handle opts+ in to VT processing, and PowerShell hands children a console with VT off.+ NUL also reports isatty() but is no console, so it fails here too."""+ if sys.platform != "win32":+ return True+ import ctypes+ import msvcrt+ from ctypes import wintypes++ try:+ handle = msvcrt.get_osfhandle(stream.fileno())+ except (AttributeError, OSError, ValueError):+ return False+ kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)+ mode = wintypes.DWORD()+ if not kernel32.GetConsoleMode(handle, ctypes.byref(mode)):+ return False+ vt = 0x0004 # ENABLE_VIRTUAL_TERMINAL_PROCESSING+ return bool(mode.value & vt or kernel32.SetConsoleMode(handle, mode.value | vt))++def _progress_stream():"""Stream for in-place progress, or None off a terminal. Prefer stdout;fall back to stderr because activate.ps1 pipes stdout through Out-Hostwhile stderr stays on the console."""for stream in (sys.stdout, sys.stderr):try:- if stream.isatty():+ if stream.isatty() and _enable_vt(stream):return streamexcept (AttributeError, ValueError):continuediff --git a/tests/pm/test_cli_progress.py b/tests/pm/test_cli_progress.pyindex a8acc95e711..45f3de96136 100644--- a/tests/pm/test_cli_progress.py+++ b/tests/pm/test_cli_progress.py@@ -4,6 +4,8 @@ from __future__ import annotationsimport io+import pytest+from pm import cli@@ -52,6 +54,7 @@ def test_unpack_phase_is_a_phase_line_in_both_modes(monkeypatch):def test_progress_stream_prefers_stdout_then_stderr(monkeypatch):+ monkeypatch.setattr(cli, "_enable_vt", lambda stream: True)monkeypatch.setattr(cli.sys.stdout, "isatty", lambda: False)monkeypatch.setattr(cli.sys.stderr, "isatty", lambda: False)assert cli._progress_stream() is None@@ -62,8 +65,20 @@ def test_progress_stream_prefers_stdout_then_stderr(monkeypatch):def test_interactive_tracks_any_terminal(monkeypatch):+ monkeypatch.setattr(cli, "_enable_vt", lambda stream: True)monkeypatch.setattr(cli.sys.stdout, "isatty", lambda: False)monkeypatch.setattr(cli.sys.stderr, "isatty", lambda: False)assert cli._interactive() is Falsemonkeypatch.setattr(cli.sys.stderr, "isatty", lambda: True)assert cli._interactive() is True++[email protected]("windows")+def test_windows_tty_without_a_console_gets_plain_lines(monkeypatch):+ """NUL is a character device (isatty() is True) with no console mode: an+ escape sequence there would be garbage, so progress falls back to lines."""+ with open("NUL", "w") as nul:+ assert nul.isatty()+ monkeypatch.setattr(cli.sys, "stdout", nul)+ monkeypatch.setattr(cli.sys, "stderr", nul)+ assert cli._progress_stream() is None