headroom/headroom/rtk/installer.py
Parideboy d633e8172c
fix(windows): pin UTF-8 encoding on text-mode subprocess calls (#1311)
Fixes #1310.

## Description

On Windows, `headroom` startup crashes a subprocess reader thread:

```
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 7894: character maps to <undefined>
  ... subprocess.py _readerthread -> buffer.append(fh.read())
  ... encodings/cp1252.py
```

Text-mode `subprocess` calls omit `encoding=`, so Python decodes child
output with the locale codec (**cp1252** on Windows). Children that emit
UTF-8 ??? `cbm index_repository` (indexing sources with chars like
`???`/`???`), `claude mcp get/add`, the memory-sync process ??? produce
bytes invalid in cp1252 and kill the reader thread. Linux/macOS default
to UTF-8, so it's invisible there.

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)

## Changes Made

- Add `encoding="utf-8", errors="replace"` to every text-mode
(`text=True` / `universal_newlines=True`) subprocess call in the
`headroom/` package (~50 call sites; several already had it).
- `errors="replace"` (not `ignore`) so corrupt bytes surface as `???`
rather than vanishing from parsed output.
- Add `tests/test_cli/test_subprocess_utf8_encoding.py`: an AST guard
asserting every text-mode subprocess call pins `encoding=`. The runtime
crash can't reproduce on UTF-8 CI, so the invariant is enforced at the
source level instead.

## Testing

- [x] Unit tests pass (`pytest`)
- New guard test passes (validates 51 call sites).
- `tests/test_install`, `tests/test_cli/test_mcp.py`,
`tests/test_mcp_registry` pass.
(`test_runtime_start_lock_blocks_another_process` fails on this Windows
box, but it fails identically on unmodified `main` ??? a pre-existing
`msvcrt` lock flake, unrelated.)

### Test Output

```text
> python -m pytest tests/test_cli/test_subprocess_utf8_encoding.py -q
1 passed in 0.12s

> python -m pytest tests/test_install/ tests/test_cli/test_mcp.py tests/test_mcp_registry/ -q
133 passed, 2 skipped in 15.34s
```

## Real Behavior Proof

- Environment: Windows 11, Python 3.13.13.
- Exact command / steps: Started `headroom` without `PYTHONUTF8=1` on a
repo with UTF-8 chars in indexable files. Observed the
`UnicodeDecodeError` crash. Applied the fix (pinning `encoding="utf-8"`
on all text-mode subprocess calls). Re-ran. No crash. The AST guard
enforces the invariant on CI (which runs UTF-8 locales and cannot
reproduce the cp1252 crash natively).
- Observed result: Subprocess reader threads no longer crash on UTF-8
output under cp1252 locale.
- Not tested: All third-party tools that `headroom` shells out to; each
was given `errors="replace"` as a safety net.

## Workaround for affected users (before fix is deployed)

`PYTHONUTF8=1` (PowerShell: `$env:PYTHONUTF8=1; headroom ...`).

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 12:52:49 -05:00

205 lines
6.8 KiB
Python

"""Download and install rtk binary from GitHub releases."""
from __future__ import annotations
import io
import logging
import os
import platform
import stat
import subprocess
import tarfile
import zipfile
from pathlib import Path
from urllib.request import urlopen
from headroom._subprocess import run
from . import RTK_BIN_DIR, RTK_BIN_PATH, RTK_VERSION
logger = logging.getLogger(__name__)
GITHUB_RELEASE_URL = "https://github.com/rtk-ai/rtk/releases/download"
def _detect_runtime_target_triple() -> str:
"""Detect platform and return the rtk release target triple."""
system = platform.system()
machine = platform.machine()
if system == "Darwin":
arch = "aarch64" if machine == "arm64" else "x86_64"
return f"{arch}-apple-darwin"
elif system == "Linux":
arch = "aarch64" if machine == "aarch64" else "x86_64"
suffix = "unknown-linux-gnu" if arch == "aarch64" else "unknown-linux-musl"
return f"{arch}-{suffix}"
elif system == "Windows":
return "x86_64-pc-windows-msvc"
raise RuntimeError(f"Unsupported platform: {system} {machine}")
def _get_target_triple() -> str:
"""Return the requested rtk target triple, honoring explicit overrides."""
return os.environ.get("HEADROOM_RTK_TARGET", "").strip() or _detect_runtime_target_triple()
def _binary_name_for_target(target: str) -> str:
"""Return the expected binary name for a target triple."""
return "rtk.exe" if "windows" in target else "rtk"
def _should_verify_target(target: str) -> bool:
"""Verify only when the requested target matches the current runtime."""
return target == _detect_runtime_target_triple()
def _get_download_url(version: str) -> tuple[str, str]:
"""Get download URL and extension for this platform.
Returns (url, extension) where extension is 'tar.gz' or 'zip'.
"""
target = _get_target_triple()
if "windows" in target:
ext = "zip"
else:
ext = "tar.gz"
url = f"{GITHUB_RELEASE_URL}/{version}/rtk-{target}.{ext}"
return url, ext
def download_rtk(version: str | None = None) -> Path:
"""Download rtk binary from GitHub releases.
Args:
version: Version to download (e.g., "v0.28.2"). Defaults to pinned version.
Returns:
Path to the installed binary.
Raises:
RuntimeError: If download or extraction fails.
"""
version = version or RTK_VERSION
target = _get_target_triple()
url, ext = _get_download_url(version)
target_path = RTK_BIN_DIR / _binary_name_for_target(target)
RTK_BIN_DIR.mkdir(parents=True, exist_ok=True)
logger.info("Downloading rtk %s from %s ...", version, url)
try:
# Validate URL scheme to prevent B310 warning
if not url.startswith(("http://", "https://")):
raise ValueError(f"Invalid URL scheme in {url}")
# Fail closed on TLS errors rather than executing an unverifiable download.
try:
with urlopen(url, timeout=30) as response:
data = response.read()
except Exception as download_err:
if "CERTIFICATE_VERIFY_FAILED" in str(download_err):
raise RuntimeError(
"TLS verification failed downloading rtk; fix the local trust store and retry."
) from download_err
raise
except Exception as e:
raise RuntimeError(f"Failed to download rtk from {url}: {e}") from e
# Extract binary
try:
if ext == "tar.gz":
with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as tar:
# Find the rtk binary inside the archive
for member in tar.getmembers():
if member.name.endswith("/rtk") or member.name == "rtk":
member.name = target_path.name # Flatten path
tar.extract(member, RTK_BIN_DIR)
break
else:
raise RuntimeError("rtk binary not found in archive")
elif ext == "zip":
with zipfile.ZipFile(io.BytesIO(data)) as zf:
for name in zf.namelist():
if name.endswith("rtk.exe") or name.endswith("/rtk"):
with zf.open(name) as src, open(target_path, "wb") as dst:
dst.write(src.read())
break
else:
raise RuntimeError("rtk binary not found in archive")
except (tarfile.TarError, zipfile.BadZipFile) as e:
raise RuntimeError(f"Failed to extract rtk archive: {e}") from e
# Make executable (skip on Windows — no Unix permissions)
if "windows" not in target:
target_path.chmod(target_path.stat().st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH)
if _should_verify_target(target):
try:
result = run(
[str(target_path), "--version"],
capture_output=True,
text=True,
timeout=5,
)
if result.returncode != 0:
raise RuntimeError(f"rtk verification failed: {result.stderr}")
logger.info("rtk installed: %s", result.stdout.strip())
except FileNotFoundError as e:
raise RuntimeError("rtk binary not found after extraction") from e
except subprocess.TimeoutExpired as e:
raise RuntimeError("rtk verification timed out") from e
else:
logger.info("rtk installed for target %s at %s (verification skipped)", target, target_path)
return target_path
def register_claude_hooks(rtk_path: Path | None = None) -> bool:
"""Register rtk hooks in Claude Code settings.
Runs `rtk init --global` which adds a PreToolUse hook to
~/.claude/settings.json that rewrites Bash commands through rtk.
Returns True if hooks were registered successfully.
"""
rtk_path = rtk_path or RTK_BIN_PATH
try:
result = run(
[str(rtk_path), "init", "--global", "--auto-patch"],
capture_output=True,
text=True,
timeout=10,
)
if result.returncode == 0:
logger.info("rtk hooks registered in Claude Code")
return True
else:
logger.warning("rtk init failed: %s", result.stderr)
return False
except Exception as e:
logger.warning("Failed to register rtk hooks: %s", e)
return False
def ensure_rtk(version: str | None = None) -> Path | None:
"""Ensure rtk is installed — download if needed.
Returns path to rtk binary, or None if installation failed.
"""
from . import get_rtk_path
existing = get_rtk_path()
if existing:
return existing
try:
return download_rtk(version)
except RuntimeError as e:
logger.warning("Could not install rtk: %s", e)
return None