headroom/tests/test_cli/test_wrap_rtk_on_path.py
Manmit Singh 140cb05fbc
fix(rtk): link managed rtk onto PATH instead of mutating the hook (#1698)
## Description

`headroom wrap claude` / `headroom update` patched
`~/.claude/hooks/rtk-rewrite.sh` after `rtk init --global --auto-patch`
wrote it. `rtk` bakes the expected SHA-256 of the canonical hook into
itself, so the post-write mutation trips its integrity guard — `rtk
verify` reports `hook integrity check FAILED … RTK will not execute` and
rtk hard-refuses to run. The patch also only absolutized the `rtk`
inside the hook, but `rtk rewrite` emits a bare `rtk` on stdout at
runtime that still needs PATH resolution, so the original silent-no-op
(#487) was never actually fixed. This leaves the hook untouched and
instead links the managed binary onto PATH.

Closes #1631

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- Removed `_patch_rtk_hook_absolute_path` (mutated the canonical hook →
broke rtk's SHA-256 integrity guard).
- Added `_ensure_rtk_on_path`: symlinks the Headroom-managed `rtk` into
a PATH dir (prefers `~/.local/bin`) so the bare `rtk` that `rtk rewrite`
emits resolves, leaving the hook byte-for-byte as `rtk init` wrote it.
- No-op when a `rtk` already resolves on PATH, on Windows, or when no
writable PATH dir exists; never clobbers an existing real file or
foreign binary.
- Rewrote the test module (`test_wrap_rtk_on_path.py`) for the new
behavior.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

```text
$ .venv/bin/python -m pytest tests/test_cli/test_wrap_rtk_on_path.py -q
collected 7 items
tests/test_cli/test_wrap_rtk_on_path.py .......                          [100%]
============================== 7 passed in 0.25s ===============================

$ .venv/bin/ruff check headroom/cli/wrap.py tests/test_cli/test_wrap_rtk_on_path.py
All checks passed!
```

## Real Behavior Proof

- Environment: macOS arm64, Python 3.14, repo `.venv`, rtk hook-version
2 (matches reporter's rtk 0.28.2 setup).
- Exact command / steps: `.venv/bin/python -m pytest
tests/test_cli/test_wrap_rtk_on_path.py -q` — covers: no-op when rtk
already on PATH, symlink created into a PATH dir when missing,
`~/.local/bin` preferred + created on demand, idempotent second run,
existing-file not clobbered (falls through to next dir), no-op on
Windows and when no writable PATH dir exists.
- Observed result: 7 passed; the canonical hook file is never written,
so rtk's baked-in SHA-256 stays valid and `rtk verify` no longer fails.
- Not tested: live end-to-end `rtk verify` PASS on a machine with rtk
installed (no rtk binary in CI sandbox); logic mirrors the reporter's
verified manual fix (symlink managed rtk into a PATH dir + untouched
canonical hook).

## Review Readiness

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

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] I have updated the CHANGELOG.md if applicable

## Additional Notes

Type checking / docs / CHANGELOG left unchecked: no public API or docs
change, and CHANGELOG is release-managed. The fix is confined to
`wrap.py`'s rtk setup path.
2026-07-07 12:23:26 -05:00

127 lines
4.6 KiB
Python

"""Tests for ``_ensure_rtk_on_path``.
``rtk init --global --auto-patch`` writes ``~/.claude/hooks/rtk-rewrite.sh``,
and ``rtk rewrite`` emits a bare ``rtk`` token at runtime that the hook feeds
back to the shell — so bare ``rtk`` must resolve on PATH. Since
``~/.headroom/bin`` is not on PATH by default, that lookup fails and token
compression never runs (issue #487).
The earlier fix rewrote the generated hook to hard-code rtk's absolute path,
but that mutates the hook after ``rtk init`` bakes in its expected SHA-256, so
rtk's integrity guard rejects it (issue #1631). ``_ensure_rtk_on_path`` instead
leaves the canonical hook untouched and links the managed binary into a PATH
directory so bare ``rtk`` resolves.
"""
from __future__ import annotations
from pathlib import Path
import pytest
from headroom.cli import wrap
from headroom.cli.wrap import _ensure_rtk_on_path
@pytest.fixture
def rtk_binary(tmp_path: Path) -> Path:
managed = tmp_path / ".headroom" / "bin" / "rtk"
managed.parent.mkdir(parents=True)
managed.write_text("#!/bin/sh\n")
managed.chmod(0o755)
return managed
def test_noop_when_rtk_already_on_path(rtk_binary: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(wrap.sys, "platform", "linux")
monkeypatch.setattr(wrap.shutil, "which", lambda _cmd: "/usr/bin/rtk")
assert _ensure_rtk_on_path(rtk_binary, path_dirs=["/usr/bin"]) is None
def test_noop_on_windows(rtk_binary: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(wrap.sys, "platform", "win32")
assert _ensure_rtk_on_path(rtk_binary, path_dirs=["C:\\bin"]) is None
def test_links_into_path_dir_when_missing(
rtk_binary: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(wrap.sys, "platform", "linux")
monkeypatch.setattr(wrap.shutil, "which", lambda _cmd: None)
bindir = tmp_path / "path-bin"
bindir.mkdir()
link = _ensure_rtk_on_path(rtk_binary, path_dirs=[str(bindir)])
assert link == bindir / "rtk"
assert link.is_symlink()
assert link.resolve() == rtk_binary.resolve()
def test_prefers_local_bin(
rtk_binary: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(wrap.sys, "platform", "linux")
monkeypatch.setattr(wrap.shutil, "which", lambda _cmd: None)
home = tmp_path / "home"
monkeypatch.setattr(wrap.Path, "home", classmethod(lambda _cls: home))
other = tmp_path / "other-bin"
other.mkdir()
local_bin = home / ".local" / "bin"
# ~/.local/bin does not exist yet but is on PATH — it is created on demand
# and preferred over the other writable dir.
link = _ensure_rtk_on_path(rtk_binary, path_dirs=[str(other), str(local_bin)])
assert link == local_bin / "rtk"
assert link.is_symlink()
def test_idempotent_second_run(
rtk_binary: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(wrap.sys, "platform", "linux")
monkeypatch.setattr(wrap.shutil, "which", lambda _cmd: None)
bindir = tmp_path / "path-bin"
bindir.mkdir()
first = _ensure_rtk_on_path(rtk_binary, path_dirs=[str(bindir)])
second = _ensure_rtk_on_path(rtk_binary, path_dirs=[str(bindir)])
assert first == second == bindir / "rtk"
assert second.resolve() == rtk_binary.resolve()
def test_does_not_clobber_existing_file(
rtk_binary: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(wrap.sys, "platform", "linux")
monkeypatch.setattr(wrap.shutil, "which", lambda _cmd: None)
occupied = tmp_path / "occupied-bin"
occupied.mkdir()
foreign = occupied / "rtk"
foreign.write_text("#!/bin/sh\n# a different rtk\n")
fallback = tmp_path / "fallback-bin"
fallback.mkdir()
link = _ensure_rtk_on_path(rtk_binary, path_dirs=[str(occupied), str(fallback)])
# The real file is left untouched; the link lands in the next writable dir.
assert foreign.read_text() == "#!/bin/sh\n# a different rtk\n"
assert not foreign.is_symlink()
assert link == fallback / "rtk"
assert link.is_symlink()
def test_noop_when_no_writable_path_dir(
rtk_binary: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(wrap.sys, "platform", "linux")
monkeypatch.setattr(wrap.shutil, "which", lambda _cmd: None)
home = tmp_path / "home"
monkeypatch.setattr(wrap.Path, "home", classmethod(lambda _cls: home))
# Only a non-existent, non-preferred dir on PATH — nothing to link into.
assert _ensure_rtk_on_path(rtk_binary, path_dirs=[str(tmp_path / "ghost")]) is None