mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-27 14:17:10 -04:00
## Description `headroom wrap cursor` unconditionally injected an `rtk`-usage instructions block into `.cursorrules`. rtk itself supports a native hook for Cursor (`rtk init --agent cursor`) — the same registration mechanism headroom already uses for Claude Code — which rewrites shell commands transparently with zero custom-instructions text needed. Headroom never tried that path for Cursor, so users got a redundant `.cursorrules` file duplicating guidance the native hook already provides silently. A follow-up commit hardens the switch: `register_agent_hooks` returns `True` on rtk exit 0, but some rtk builds exit 0 without writing `~/.cursor/hooks.json`. headroom now trusts the on-disk hook file, not the exit code, before skipping the `.cursorrules` fallback. Closes #756 ## 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 - `headroom/rtk/installer.py`: generalized `register_claude_hooks` into `register_agent_hooks(rtk_path, *, agent="claude")`, which passes `--agent <agent>` to `rtk init` for non-Claude agents. `register_claude_hooks` kept as a thin wrapper for backward compatibility. Added `RTK_NATIVE_HOOK_AGENTS` documenting which agents rtk supports a native hook for. - `headroom/cli/wrap.py`: `wrap cursor` now calls `register_agent_hooks(rtk_path, agent="cursor")` first, and only skips the `.cursorrules` fallback when `~/.cursor/hooks.json` is actually on disk; otherwise it falls back to `_inject_rtk_instructions(...)`. - Tests: `tests/test_rtk_installer.py` and `tests/test_cli/test_wrap_bridge.py` cover the native-hook path, the on-disk verification, and the `.cursorrules` fallback. - `CHANGELOG.md`: added an entry under `## Unreleased` / `### Fixed`. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output ```text $ python -m ruff format --check headroom/ tests/ e2e/ 953 files already formatted $ python -m ruff check headroom/cli/wrap.py headroom/rtk/installer.py tests/test_cli/test_wrap_bridge.py tests/test_rtk_installer.py All checks passed! $ python -m pytest tests/test_cli/test_wrap_bridge.py -k cursor -q 3 passed ``` ## Real Behavior Proof - Environment: Windows 11, Python 3.13, local checkout; `python -m pytest` / `ruff` run directly. - Exact command / steps: `python -m pytest tests/test_cli/test_wrap_bridge.py -k cursor -q` — the first test mocks `register_agent_hooks` to write `~/.cursor/hooks.json` and asserts `.cursorrules` is NOT created; the second mocks it to write nothing and asserts `.cursorrules` IS created with the `headroom:rtk-instructions` marker; the third exercises the explicit registration-failure fallback. - Observed result: `3 passed`. Native-hook path skips `.cursorrules` only when the hook file exists on disk; every other outcome falls back to `.cursorrules`, so Cursor always gets RTK guidance. - Not tested: real `rtk` binary writing `~/.cursor/hooks.json` end-to-end — that path is covered by the `docker-wrap-e2e` CI job, not locally. ## 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 - [x] I have updated the CHANGELOG.md if applicable ## Screenshots (if applicable) N/A — CLI-only change. ## Additional Notes Scope: rtk's native-hook-capable agents include `claude`, `cursor`, `windsurf`, `cline`, `kilocode`, `antigravity`, `pi`, `hermes`, but only `cursor` and `claude` have a corresponding `headroom wrap` subcommand today, so this fix only changes `wrap cursor` behavior. --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
106 lines
3.6 KiB
Python
106 lines
3.6 KiB
Python
"""Tests for host-target rtk installation overrides."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import stat
|
|
import tarfile
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from headroom.rtk import get_rtk_path, installer
|
|
|
|
|
|
def test_get_rtk_path_finds_windows_managed_binary(tmp_path: Path) -> None:
|
|
managed_dir = tmp_path / ".headroom" / "bin"
|
|
managed_dir.mkdir(parents=True)
|
|
managed_path = managed_dir / "rtk.exe"
|
|
managed_path.write_bytes(b"binary")
|
|
|
|
with patch("headroom.rtk.RTK_BIN_DIR", managed_dir):
|
|
with patch("headroom.rtk.RTK_BIN_PATH", managed_dir / "rtk"):
|
|
with patch("headroom.rtk.shutil.which", return_value=None):
|
|
assert get_rtk_path() == managed_path
|
|
|
|
|
|
def test_get_target_triple_uses_override(monkeypatch) -> None:
|
|
monkeypatch.setenv("HEADROOM_RTK_TARGET", "x86_64-pc-windows-msvc")
|
|
assert installer._get_target_triple() == "x86_64-pc-windows-msvc"
|
|
|
|
|
|
def test_download_rtk_skips_verify_for_non_native_target(monkeypatch, tmp_path: Path) -> None:
|
|
archive = io.BytesIO()
|
|
with tarfile.open(fileobj=archive, mode="w:gz") as tf:
|
|
info = tarfile.TarInfo(name="rtk")
|
|
payload = b"fake-binary"
|
|
info.size = len(payload)
|
|
tf.addfile(info, io.BytesIO(payload))
|
|
archive_bytes = archive.getvalue()
|
|
|
|
class _Response:
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, exc_type, exc, tb):
|
|
return False
|
|
|
|
def read(self) -> bytes:
|
|
return archive_bytes
|
|
|
|
monkeypatch.setenv("HEADROOM_RTK_TARGET", "x86_64-apple-darwin")
|
|
|
|
with patch.object(installer, "RTK_BIN_DIR", tmp_path):
|
|
with patch.object(installer, "urlopen", return_value=_Response()):
|
|
with patch.object(installer.subprocess, "run") as subprocess_run:
|
|
installed_path = installer.download_rtk("v0.42.4")
|
|
|
|
assert installed_path == tmp_path / "rtk"
|
|
assert installed_path.exists()
|
|
subprocess_run.assert_not_called()
|
|
|
|
|
|
def test_register_claude_hooks_survives_forked_daemon(tmp_path: Path) -> None:
|
|
"""rtk init that exits fast but leaves a child holding stdout must not hang.
|
|
|
|
Regression: capturing through pipes made subprocess.run drain until EOF,
|
|
which a lingering grandchild deferred past the 10s timeout even though the
|
|
hooks were already registered. Output now goes to a temp file, so we wait
|
|
only on the direct child.
|
|
"""
|
|
fake_rtk = tmp_path / "rtk"
|
|
fake_rtk.write_text("#!/bin/bash\n( sleep 30 ) &\necho done\nexit 0\n")
|
|
fake_rtk.chmod(fake_rtk.stat().st_mode | stat.S_IEXEC)
|
|
|
|
assert installer.register_claude_hooks(fake_rtk) is True
|
|
|
|
|
|
def test_register_agent_hooks_passes_agent_flag_for_non_claude(tmp_path: Path, monkeypatch) -> None:
|
|
calls: list[list[str]] = []
|
|
|
|
class FakeResult:
|
|
returncode = 0
|
|
|
|
def fake_run(args, **kwargs):
|
|
calls.append(args)
|
|
return FakeResult()
|
|
|
|
monkeypatch.setattr(installer.subprocess, "run", fake_run)
|
|
|
|
assert installer.register_agent_hooks(Path("rtk"), agent="cursor") is True
|
|
assert calls == [["rtk", "init", "--global", "--auto-patch", "--agent", "cursor"]]
|
|
|
|
|
|
def test_register_agent_hooks_omits_agent_flag_for_claude(tmp_path: Path, monkeypatch) -> None:
|
|
calls: list[list[str]] = []
|
|
|
|
class FakeResult:
|
|
returncode = 0
|
|
|
|
def fake_run(args, **kwargs):
|
|
calls.append(args)
|
|
return FakeResult()
|
|
|
|
monkeypatch.setattr(installer.subprocess, "run", fake_run)
|
|
|
|
assert installer.register_agent_hooks(Path("rtk"), agent="claude") is True
|
|
assert calls == [["rtk", "init", "--global", "--auto-patch"]]
|