mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
refactor(graph): drop the bespoke codebase-memory-mcp downloader nothing calls (#4102)
## Description `headroom/graph/installer.py` had its own downloader for the codebase-memory-mcp binary: `download_cbm` and `ensure_cbm`, plus platform, asset-name and zip helpers. `headroom wrap` stopped calling `ensure_cbm()` indca9853e(#1230), when tokensave and Serena replaced codebase-memory-mcp in wrap. Since then the only product user of the module is the `--code-graph` watcher (`graph/watcher.py:121`). It calls only `get_cbm_path()`. The downloader duplicated `headroom.binaries` (the same `tools.json` pin, the same verification), and only its tests kept it alive. This PR keeps `get_cbm_path`, `_installed_name`, `CBM_BIN_DIR` and `CBM_BIN_NAME`, and removes the rest. Evidence: - `grep -rnw` for `download_cbm`, `ensure_cbm`, `_detect_platform`, `_asset_filename`, `_extract_from_zip`, `CBM_VERSION`, `CBM_REPO` and `GITHUB_RELEASE_URL` found only `installer.py` and tests. The search covered `headroom crates plugins sdk docs wiki scripts benchmarks examples e2e .github` and the consumer repos. - `git show dca9853e` shows the removed `from headroom.graph.installer import ensure_cbm, get_cbm_path` in `cli/wrap.py`. - `installer.py` has no `__all__`. `headroom/graph/__init__.py` re-exports nothing. Neither is documented. **`binaries.resolve` vs `get_cbm_path` check (requested before deleting).** - `binaries.resolve("codebase-memory-mcp")` installs to `binaries.cache_dir()/codebase-memory-mcp-<version>-<platform>/codebase-memory-mcp`. The same goes for `headroom tools install` and `ensure_tools()` at proxy startup with `--intercept-tool-results`. On macOS that is `~/Library/Caches/headroom/bin/...`; elsewhere it is the platform cache dir or `HEADROOM_BINARIES_CACHE`. - `get_cbm_path()` looks on `PATH` and then in `~/.local/bin` only. - So a binary installed through `headroom.binaries` is **not** found by the watcher unless that cache dir is on `PATH`. This gap already exists on main and this PR does not change it. - The removed `download_cbm` did install into `~/.local/bin`, but nothing has called it since #1230. - A follow-up could make `get_cbm_path()` also try `binaries.which("codebase-memory-mcp")`. I left that out because it is a behaviour change. Refs #4136 ## Type of Change - [ ] 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 - [x] Code refactoring (no functional changes) ## Changes Made - `headroom/graph/installer.py`: - Remove `download_cbm`, `ensure_cbm`, `_detect_platform`, `_asset_filename` and `_extract_from_zip`. - Remove the constants `CBM_VERSION`, `CBM_REPO` and `GITHUB_RELEASE_URL`, and the imports only they used. - The module docstring now says what the module does: it locates the binary. - `headroom/offline.py`: the docstring lists codebase-memory-mcp under release-binary downloads. - Tests: - `tests/test_graph.py`: drop the platform, download and ensure tests. - `tests/test_graph_installer.py`: rewrite the real-PATH-lookup test so it creates the binary directly instead of through `download_cbm`, and keep the off-PATH test. - `tests/test_binaries_refusal_reaches_callers.py`: drop the two `ensure_cbm` tests. - `tests/test_offline_egress_chokepoint.py`: drop `test_cbm_download_opens_no_socket`. - The watcher tests are unchanged. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [ ] New tests added for new functionality - [x] Manual testing performed ### Test Output ```text $ pytest -q -p no:cacheprovider tests/test_graph.py tests/test_graph_installer.py tests/test_binaries_refusal_reaches_callers.py tests/test_offline_egress_chokepoint.py tests/test_litellm_optional.py tests/test_binaries.py FAILED tests/test_offline_egress_chokepoint.py::TestHeadroomCloudCompressionOffline::test_asgi_middleware_opens_no_socket FAILED tests/test_offline_egress_chokepoint.py::TestHeadroomCloudCompressionOffline::test_litellm_callback_opens_no_socket ======================== 2 failed, 188 passed in 16.14s ======================== # The same two fail on a clean origin/main (2 failed, 141 passed for that file), so they are pre-existing and unrelated. $ mypy headroom/graph/installer.py headroom/graph/watcher.py --ignore-missing-imports Success: no issues found in 2 source files $ uvx ruff@0.16.8 check <changed files> All checks passed! ``` ## Real Behavior Proof - Environment: macOS, Python 3.12.13, repo .venv, branch checkout at0cbd67d6(basea9815bbb), temp HOME / HEADROOM_WORKSPACE_DIR / CODEX_HOME, offline. - Exact command / steps: - Ran the shared smoke harness with `--code-graph`. It starts a real proxy from this checkout against a local fake upstream and sends one Anthropic and one OpenAI request with a compressible tool result. - Ran a direct script with a stub `codebase-memory-mcp` in the temp `~/.local/bin`. It calls `get_cbm_path` and starts the real `CodeGraphWatcher`, then repeats both with the binary removed. - Ran the graph, installer, binaries and offline-egress tests. - Observed result: - With `--code-graph`, the proxy starts and both requests return HTTP 200 with savings. The temp HOME has no binary, so the watcher stays off, which is unchanged and logged at DEBUG only. - With the stub present, `get_cbm_path` finds it and the watcher starts. Without it, both return None/False. - 188 tests pass. The 2 failures are pre-existing on main. - Not tested: Indexing with a real codebase-memory-mcp binary. `get_cbm_path` and the watcher are unchanged. ```text $ bash smoke.sh <worktree> --code-graph branch: cleanup/memory-cache-graph-downloader @0cbd67d6health: 200 POST /v1/messages -> HTTP200 smoke-ok POST /v1/chat/completions -> HTTP200 smoke-ok stats: requests=2 failed=0 tokens_saved=13574 savings_percent=99.82 proxy.log WARNING/ERROR lines: 2 headroom.proxy - WARNING - event=proxy_offline_mode air-gap active — every connection Headroom initiates is refused (telemetry, update check, license reporter, model/tokenizer/binary/dataset downloads, remote Kompress, OTLP/Langfuse export, Copilot auth, subscription polling, OpenAI embedders, Headroom Cloud compression). Still allowed on purpose: forwarding your requests to the upstream you 4 headroom.transforms.kompress_compressor - WARNING - Kompress: artifact prefetch found no usable file for chopratejas/kompress-v2-base 4 headroom.transforms.kompress_compressor - WARNING - Kompress: artifact prefetch refused for chopratejas/kompress-v2-base (HEADROOM_OFFLINE is set): HEADROOM_OFFLINE is set: refusing outbound network access for HuggingFace download of chopratejas/kompress-v2-base/onnx/kompress-int8-wo.onnx to huggingface.co. Unset HEADROOM_OFFLINE, or turn off the feature that needs this connection. $ HOME=$TMP/home HEADROOM_WORKSPACE_DIR=$TMP/ws CODEX_HOME=$TMP/codex python -u -c "<get_cbm_path(); CodeGraphWatcher(project_dir).start(); remove stub; repeat>" get_cbm_path() -> $TMP/home/.local/bin/codebase-memory-mcp watcher.start() -> True without binary: get_cbm_path() -> None ; start() -> False ``` ## Runtime Rollout Safety - Rollout-managed feature(s): None. - Minimum rollout channel: N/A; dead-code removal. - Stable/default behavior changed: No. Nothing called the removed downloader. - Kill switch / disable path: N/A. - Unsafe override required: No. - Qualification impact: None. - Rollback path: Revert this commit. ## 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 - [ ] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] 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 did **not** edit `CHANGELOG.md` — it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this) ## Screenshots (if applicable) N/A. ## Additional Notes Part of maintainer-requested cleanup #4136 (dead code, duplication, debuggability), split into small PRs for review. Follow-ups, out of scope here: - the install-location gap described above; - `binaries.verify_download_bytes`, whose only product caller was the removed `download_cbm`. It is now used only by `tests/test_binaries.py`, and its docstring still names codebase-memory-mcp. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
6d23895dc0
commit
30d23fb799
+5
-153
@@ -1,74 +1,20 @@
|
||||
"""Download and install codebase-memory-mcp binary from GitHub releases."""
|
||||
"""Locate the codebase-memory-mcp binary used by the code-graph watcher.
|
||||
|
||||
The binary is looked up on PATH, then in ``~/.local/bin``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import logging
|
||||
import platform
|
||||
import shutil
|
||||
import stat
|
||||
import tarfile
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
from urllib.request import urlopen
|
||||
|
||||
from headroom.offline import guard_egress
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
CBM_VERSION = "v0.8.1"
|
||||
CBM_REPO = "DeusData/codebase-memory-mcp"
|
||||
CBM_BIN_DIR = Path.home() / ".local" / "bin"
|
||||
CBM_BIN_NAME = "codebase-memory-mcp"
|
||||
|
||||
GITHUB_RELEASE_URL = f"https://github.com/{CBM_REPO}/releases/download"
|
||||
|
||||
|
||||
def _detect_platform() -> str:
|
||||
"""Detect platform and return the release asset suffix."""
|
||||
system = platform.system().lower()
|
||||
machine = platform.machine().lower()
|
||||
|
||||
if system == "darwin":
|
||||
arch = "arm64" if machine == "arm64" else "amd64"
|
||||
return f"darwin-{arch}"
|
||||
elif system == "linux":
|
||||
arch = "arm64" if machine in ("aarch64", "arm64") else "amd64"
|
||||
return f"linux-{arch}"
|
||||
elif system == "windows":
|
||||
return "windows-amd64"
|
||||
|
||||
raise RuntimeError(f"Unsupported platform: {system} {machine}")
|
||||
|
||||
|
||||
def _asset_filename(plat: str) -> str:
|
||||
"""Return the release asset name for ``plat``, with the extension tools.json pins.
|
||||
|
||||
Upstream publishes a .zip for Windows and a .tar.gz for every other platform.
|
||||
"""
|
||||
from headroom.binaries import _tool_entry
|
||||
|
||||
stem = f"codebase-memory-mcp-{plat}"
|
||||
for asset in _tool_entry("codebase-memory-mcp").get("assets", {}).values():
|
||||
name = str(asset.get("url", "")).rsplit("/", 1)[-1]
|
||||
if name in (f"{stem}.tar.gz", f"{stem}.zip"):
|
||||
return name
|
||||
return f"{stem}.tar.gz"
|
||||
|
||||
|
||||
def _extract_from_zip(data: bytes, target_path: Path) -> None:
|
||||
"""Write the binary member of a .zip release archive to ``target_path``."""
|
||||
wanted = (CBM_BIN_NAME, f"{CBM_BIN_NAME}.exe")
|
||||
with zipfile.ZipFile(io.BytesIO(data)) as archive:
|
||||
for info in archive.infolist():
|
||||
if not info.is_dir() and info.filename.rsplit("/", 1)[-1] in wanted:
|
||||
target_path.write_bytes(archive.read(info))
|
||||
return
|
||||
raise RuntimeError("codebase-memory-mcp binary not found in archive")
|
||||
|
||||
|
||||
def _installed_name(plat: str) -> str:
|
||||
"""Return the file name to install the binary under for ``plat``.
|
||||
"""Return the binary's file name on ``plat``.
|
||||
|
||||
On Windows, ``shutil.which`` (and so a PATH lookup) only finds a binary
|
||||
whose name ends in a PATHEXT extension such as ``.exe``.
|
||||
@@ -89,97 +35,3 @@ def get_cbm_path() -> Path | None:
|
||||
return installed
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def download_cbm(version: str | None = None) -> Path:
|
||||
"""Download codebase-memory-mcp binary from GitHub releases.
|
||||
|
||||
Returns path to installed binary.
|
||||
"""
|
||||
version = version or CBM_VERSION
|
||||
plat = _detect_platform()
|
||||
filename = _asset_filename(plat)
|
||||
url = f"{GITHUB_RELEASE_URL}/{version}/{filename}"
|
||||
guard_egress("codebase-memory-mcp binary download", url)
|
||||
|
||||
CBM_BIN_DIR.mkdir(parents=True, exist_ok=True)
|
||||
target_path = CBM_BIN_DIR / _installed_name(plat)
|
||||
|
||||
logger.info("Downloading codebase-memory-mcp %s for %s ...", version, plat)
|
||||
|
||||
try:
|
||||
if not url.startswith(("http://", "https://")):
|
||||
raise ValueError(f"Invalid URL: {url}")
|
||||
|
||||
with urlopen(url, timeout=60) as response: # noqa: S310
|
||||
data = response.read()
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Failed to download codebase-memory-mcp from {url}: {e}") from e
|
||||
|
||||
from headroom.binaries import verify_download_bytes
|
||||
|
||||
verify_download_bytes(data, url=url, name="codebase-memory-mcp")
|
||||
|
||||
# Extract binary from the .zip (Windows) or .tar.gz (everything else)
|
||||
try:
|
||||
if filename.endswith(".zip"):
|
||||
_extract_from_zip(data, target_path)
|
||||
else:
|
||||
with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as tar:
|
||||
for member in tar.getmembers():
|
||||
if member.name.endswith(CBM_BIN_NAME) or member.name == CBM_BIN_NAME:
|
||||
member.name = target_path.name
|
||||
tar.extract(member, CBM_BIN_DIR, filter="data")
|
||||
break
|
||||
else:
|
||||
raise RuntimeError("codebase-memory-mcp binary not found in archive")
|
||||
except (tarfile.TarError, zipfile.BadZipFile) as e:
|
||||
raise RuntimeError(f"Failed to extract archive: {e}") from e
|
||||
|
||||
# Make executable
|
||||
target_path.chmod(target_path.stat().st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH)
|
||||
|
||||
# Verify
|
||||
try:
|
||||
from headroom._subprocess import run
|
||||
|
||||
result = run(
|
||||
[str(target_path), "--version"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
if result.returncode == 0:
|
||||
ver = result.stdout.strip()
|
||||
logger.info("Installed: %s", ver)
|
||||
else:
|
||||
logger.warning("Binary installed but version check failed")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return target_path
|
||||
|
||||
|
||||
def ensure_cbm() -> Path | None:
|
||||
"""Ensure codebase-memory-mcp is available. Download if needed.
|
||||
|
||||
Returns path to binary, or None if download failed.
|
||||
"""
|
||||
existing = get_cbm_path()
|
||||
if existing:
|
||||
return existing
|
||||
|
||||
# Local import to match this module's lazy-import style (see download_cbm).
|
||||
from headroom.binaries import UnpinnedDownload
|
||||
|
||||
try:
|
||||
return download_cbm()
|
||||
except RuntimeError as e:
|
||||
logger.warning("Failed to install codebase-memory-mcp: %s", e)
|
||||
return None
|
||||
except UnpinnedDownload as e:
|
||||
# Documented contract is "path, or None if the download failed", and a
|
||||
# refusal is a failure to install -- the feature is simply unavailable.
|
||||
# Deliberately NOT widened to BinaryError: Sha256Mismatch is a tamper
|
||||
# signal and must keep propagating rather than becoming a quiet None.
|
||||
logger.warning("Refusing to install codebase-memory-mcp: %s", e)
|
||||
return None
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ deployment exists to carry.
|
||||
Refused (this is not a sample — it is the rule, and the meta-test enforces it):
|
||||
the telemetry beacon, the update check, the license/usage reporter, OTLP and
|
||||
Langfuse export, HuggingFace/Kompress/fastembed model downloads, release-binary
|
||||
and codebase-memory-mcp downloads, eval dataset downloads and the provider SDK
|
||||
downloads (codebase-memory-mcp among them), eval dataset downloads and the provider SDK
|
||||
clients the eval harness drives, GitHub Copilot device-flow auth and token
|
||||
exchange, the Anthropic / Codex / Copilot subscription pollers, the OpenAI
|
||||
embedders, both Headroom Cloud compression integrations, and the TLS
|
||||
|
||||
@@ -59,39 +59,6 @@ def test_tools_install_keeps_going_and_still_sets_an_exit_code(_refuse) -> None:
|
||||
assert result.exit_code == 1, result.output
|
||||
|
||||
|
||||
def test_ensure_cbm_honours_its_documented_none_contract(monkeypatch) -> None:
|
||||
"""`ensure_cbm` promises "path, or None if the download failed"."""
|
||||
from headroom.graph import installer
|
||||
|
||||
monkeypatch.setattr(installer, "get_cbm_path", lambda: None)
|
||||
|
||||
def _raise():
|
||||
raise binaries.UnpinnedDownload("refusing unpinned download codebase-memory-mcp")
|
||||
|
||||
monkeypatch.setattr(installer, "download_cbm", _raise)
|
||||
|
||||
assert installer.ensure_cbm() is None
|
||||
|
||||
|
||||
def test_a_tamper_signal_still_propagates_from_ensure_cbm(monkeypatch) -> None:
|
||||
"""The None contract must not swallow a sha256 mismatch.
|
||||
|
||||
An unpinned asset is a misconfiguration; a mismatch is evidence of tampering
|
||||
and has to stay loud rather than becoming a quiet "feature unavailable".
|
||||
"""
|
||||
from headroom.graph import installer
|
||||
|
||||
monkeypatch.setattr(installer, "get_cbm_path", lambda: None)
|
||||
|
||||
def _raise():
|
||||
raise binaries.Sha256Mismatch("sha256 mismatch for codebase-memory-mcp")
|
||||
|
||||
monkeypatch.setattr(installer, "download_cbm", _raise)
|
||||
|
||||
with pytest.raises(binaries.Sha256Mismatch):
|
||||
installer.ensure_cbm()
|
||||
|
||||
|
||||
def test_the_escape_hatch_warning_is_not_printed_twice(monkeypatch, capsys) -> None:
|
||||
"""With no handlers configured, logging's lastResort already writes stderr.
|
||||
|
||||
|
||||
@@ -1,65 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import json
|
||||
import subprocess
|
||||
import tarfile
|
||||
from pathlib import Path
|
||||
from types import ModuleType, SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from headroom.graph import installer, watcher
|
||||
|
||||
|
||||
def _build_archive(member_name: str = installer.CBM_BIN_NAME) -> bytes:
|
||||
payload = io.BytesIO()
|
||||
with tarfile.open(fileobj=payload, mode="w:gz") as tar:
|
||||
data = b"#!/bin/sh\necho version\n"
|
||||
info = tarfile.TarInfo(name=member_name)
|
||||
info.size = len(data)
|
||||
tar.addfile(info, io.BytesIO(data))
|
||||
return payload.getvalue()
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, data: bytes) -> None:
|
||||
self._data = data
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc, tb) -> None:
|
||||
return None
|
||||
|
||||
def read(self) -> bytes:
|
||||
return self._data
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("system", "machine", "expected"),
|
||||
[
|
||||
("Darwin", "arm64", "darwin-arm64"),
|
||||
("Darwin", "x86_64", "darwin-amd64"),
|
||||
("Linux", "aarch64", "linux-arm64"),
|
||||
("Linux", "arm64", "linux-arm64"),
|
||||
("Linux", "x86_64", "linux-amd64"),
|
||||
("Windows", "AMD64", "windows-amd64"),
|
||||
],
|
||||
)
|
||||
def test_detect_platform_variants(monkeypatch, system: str, machine: str, expected: str) -> None:
|
||||
monkeypatch.setattr(installer.platform, "system", lambda: system)
|
||||
monkeypatch.setattr(installer.platform, "machine", lambda: machine)
|
||||
assert installer._detect_platform() == expected
|
||||
|
||||
|
||||
def test_detect_platform_rejects_unknown_system(monkeypatch) -> None:
|
||||
monkeypatch.setattr(installer.platform, "system", lambda: "Solaris")
|
||||
monkeypatch.setattr(installer.platform, "machine", lambda: "sparc")
|
||||
with pytest.raises(RuntimeError, match="Unsupported platform"):
|
||||
installer._detect_platform()
|
||||
|
||||
|
||||
def test_get_cbm_path_prefers_path_then_install_dir(monkeypatch, tmp_path: Path) -> None:
|
||||
on_path = tmp_path / "on-path"
|
||||
installed = tmp_path / installer.CBM_BIN_NAME
|
||||
@@ -76,108 +24,6 @@ def test_get_cbm_path_prefers_path_then_install_dir(monkeypatch, tmp_path: Path)
|
||||
assert installer.get_cbm_path() is None
|
||||
|
||||
|
||||
def test_download_cbm_success_and_verification_paths(monkeypatch, tmp_path: Path) -> None:
|
||||
# v1.2.3 is not in the registry and the mock archive would not match a pin
|
||||
# anyway; this test is about the install mechanics, not verification.
|
||||
monkeypatch.setenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", "1")
|
||||
monkeypatch.setattr(installer, "CBM_BIN_DIR", tmp_path)
|
||||
monkeypatch.setattr(installer, "_detect_platform", lambda: "linux-amd64")
|
||||
monkeypatch.setattr(
|
||||
installer, "urlopen", lambda url, timeout=60: FakeResponse(_build_archive())
|
||||
)
|
||||
|
||||
run_calls: list[list[str]] = []
|
||||
|
||||
def fake_run(command, **kwargs):
|
||||
run_calls.append(command)
|
||||
return SimpleNamespace(returncode=1, stdout="")
|
||||
|
||||
monkeypatch.setattr("subprocess.run", fake_run)
|
||||
path = installer.download_cbm(version="v1.2.3")
|
||||
assert path == tmp_path / installer.CBM_BIN_NAME
|
||||
assert path.exists()
|
||||
assert run_calls == [[str(path), "--version"]]
|
||||
|
||||
monkeypatch.setattr(
|
||||
"subprocess.run", lambda *args, **kwargs: (_ for _ in ()).throw(RuntimeError("boom"))
|
||||
)
|
||||
assert installer.download_cbm(version="v1.2.3") == path
|
||||
|
||||
monkeypatch.setattr(
|
||||
"subprocess.run",
|
||||
lambda *args, **kwargs: SimpleNamespace(returncode=0, stdout="cbm v1.2.3\n"),
|
||||
)
|
||||
assert installer.download_cbm(version="v1.2.3") == path
|
||||
|
||||
|
||||
def test_download_cbm_invalid_url_download_failure_and_extract_errors(
|
||||
monkeypatch, tmp_path: Path
|
||||
) -> None:
|
||||
# These failure modes are reached with an overridden (off-registry) release
|
||||
# host, so verification would refuse them before the code under test runs.
|
||||
monkeypatch.setenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", "1")
|
||||
monkeypatch.setattr(installer, "CBM_BIN_DIR", tmp_path)
|
||||
monkeypatch.setattr(installer, "_detect_platform", lambda: "linux-amd64")
|
||||
|
||||
monkeypatch.setattr(installer, "GITHUB_RELEASE_URL", "ftp://example.test/releases")
|
||||
with pytest.raises(RuntimeError, match="Invalid URL"):
|
||||
installer.download_cbm()
|
||||
|
||||
monkeypatch.setattr(installer, "GITHUB_RELEASE_URL", "https://example.test/releases")
|
||||
monkeypatch.setattr(
|
||||
installer,
|
||||
"urlopen",
|
||||
lambda url, timeout=60: (_ for _ in ()).throw(OSError("network down")),
|
||||
)
|
||||
with pytest.raises(RuntimeError, match="Failed to download codebase-memory-mcp"):
|
||||
installer.download_cbm()
|
||||
|
||||
monkeypatch.setattr(
|
||||
installer,
|
||||
"urlopen",
|
||||
lambda url, timeout=60: FakeResponse(_build_archive("some/other-binary")),
|
||||
)
|
||||
with pytest.raises(RuntimeError, match="binary not found in archive"):
|
||||
installer.download_cbm()
|
||||
|
||||
monkeypatch.setattr(installer, "urlopen", lambda url, timeout=60: FakeResponse(b"not a tar"))
|
||||
with pytest.raises(RuntimeError, match="Failed to extract archive"):
|
||||
installer.download_cbm()
|
||||
|
||||
|
||||
def test_download_cbm_refuses_an_off_registry_version(monkeypatch, tmp_path: Path) -> None:
|
||||
"""A version override points at a URL with no pin — refuse it (A-7).
|
||||
|
||||
The default CBM_VERSION is pinned for every platform in tools.json, so only
|
||||
an override reaches this path.
|
||||
"""
|
||||
from headroom import binaries
|
||||
|
||||
monkeypatch.delenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", raising=False)
|
||||
monkeypatch.setattr(installer, "CBM_BIN_DIR", tmp_path)
|
||||
monkeypatch.setattr(installer, "_detect_platform", lambda: "linux-amd64")
|
||||
monkeypatch.setattr(
|
||||
installer, "urlopen", lambda url, timeout=60: FakeResponse(_build_archive())
|
||||
)
|
||||
|
||||
with pytest.raises(binaries.BinaryError) as exc:
|
||||
installer.download_cbm(version="v9.9.9")
|
||||
assert type(exc.value) is binaries.UnpinnedDownload
|
||||
assert not (tmp_path / installer.CBM_BIN_NAME).exists()
|
||||
|
||||
|
||||
def test_ensure_cbm_uses_existing_or_returns_none_on_failure(monkeypatch, tmp_path: Path) -> None:
|
||||
existing = tmp_path / installer.CBM_BIN_NAME
|
||||
monkeypatch.setattr(installer, "get_cbm_path", lambda: existing)
|
||||
assert installer.ensure_cbm() == existing
|
||||
|
||||
monkeypatch.setattr(installer, "get_cbm_path", lambda: None)
|
||||
monkeypatch.setattr(
|
||||
installer, "download_cbm", lambda: (_ for _ in ()).throw(RuntimeError("nope"))
|
||||
)
|
||||
assert installer.ensure_cbm() is None
|
||||
|
||||
|
||||
def test_code_graph_watcher_init_start_stop_and_event_filtering(
|
||||
monkeypatch, tmp_path: Path
|
||||
) -> None:
|
||||
|
||||
@@ -1,132 +1,33 @@
|
||||
"""Release asset selection and extraction for the codebase-memory-mcp installer."""
|
||||
"""Locating the codebase-memory-mcp binary for the code-graph watcher."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import platform
|
||||
import shutil
|
||||
import tarfile
|
||||
import zipfile
|
||||
import stat
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from headroom import binaries
|
||||
from headroom.graph import installer
|
||||
|
||||
|
||||
class FakeResponse(io.BytesIO):
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc, tb) -> None:
|
||||
return None
|
||||
|
||||
|
||||
def _zip_archive(member_name: str, data: bytes = b"MZ fake binary") -> bytes:
|
||||
payload = io.BytesIO()
|
||||
with zipfile.ZipFile(payload, "w") as archive:
|
||||
archive.writestr(member_name, data)
|
||||
return payload.getvalue()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("plat", "extension"),
|
||||
[
|
||||
("darwin-arm64", ".tar.gz"),
|
||||
("darwin-amd64", ".tar.gz"),
|
||||
("linux-arm64", ".tar.gz"),
|
||||
("linux-amd64", ".tar.gz"),
|
||||
("windows-amd64", ".zip"),
|
||||
],
|
||||
)
|
||||
def test_asset_filename_matches_the_registry_asset(plat: str, extension: str) -> None:
|
||||
filename = installer._asset_filename(plat)
|
||||
|
||||
assert filename == f"codebase-memory-mcp-{plat}{extension}"
|
||||
pinned_urls = {
|
||||
asset["url"] for asset in binaries._tool_entry("codebase-memory-mcp")["assets"].values()
|
||||
}
|
||||
assert f"{installer.GITHUB_RELEASE_URL}/{installer.CBM_VERSION}/{filename}" in pinned_urls
|
||||
|
||||
|
||||
@pytest.mark.parametrize("member_name", ["codebase-memory-mcp.exe", "codebase-memory-mcp"])
|
||||
def test_download_cbm_on_windows_fetches_and_extracts_the_zip(
|
||||
monkeypatch, tmp_path: Path, member_name: str
|
||||
) -> None:
|
||||
monkeypatch.setenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", "1")
|
||||
monkeypatch.setattr(installer, "CBM_BIN_DIR", tmp_path)
|
||||
monkeypatch.setattr(installer, "_detect_platform", lambda: "windows-amd64")
|
||||
requested: list[str] = []
|
||||
|
||||
def fake_urlopen(url: str, timeout: int = 60) -> FakeResponse:
|
||||
requested.append(url)
|
||||
return FakeResponse(_zip_archive(member_name))
|
||||
|
||||
monkeypatch.setattr(installer, "urlopen", fake_urlopen)
|
||||
probes: list[list[str]] = []
|
||||
|
||||
def fake_run(command: list[str], **kwargs) -> SimpleNamespace:
|
||||
probes.append(command)
|
||||
return SimpleNamespace(returncode=1, stdout="")
|
||||
|
||||
monkeypatch.setattr("headroom._subprocess.run", fake_run)
|
||||
|
||||
path = installer.download_cbm()
|
||||
|
||||
assert requested == [
|
||||
f"{installer.GITHUB_RELEASE_URL}/{installer.CBM_VERSION}/"
|
||||
"codebase-memory-mcp-windows-amd64.zip"
|
||||
]
|
||||
# A PATH lookup on Windows only finds the binary through its .exe extension.
|
||||
assert path == tmp_path / "codebase-memory-mcp.exe"
|
||||
assert path.read_bytes() == b"MZ fake binary"
|
||||
assert probes == [[str(path), "--version"]]
|
||||
|
||||
|
||||
def _host_release_archive(filename: str) -> bytes:
|
||||
"""Build a release archive in the format ``download_cbm`` requests on this host."""
|
||||
if filename.endswith(".zip"):
|
||||
return _zip_archive("codebase-memory-mcp.exe")
|
||||
payload = io.BytesIO()
|
||||
with tarfile.open(fileobj=payload, mode="w:gz") as tar:
|
||||
data = b"#!/bin/sh\necho codebase-memory-mcp test\n"
|
||||
info = tarfile.TarInfo(name="codebase-memory-mcp")
|
||||
info.size = len(data)
|
||||
tar.addfile(info, io.BytesIO(data))
|
||||
return payload.getvalue()
|
||||
|
||||
|
||||
def test_installed_binary_is_found_by_a_real_path_lookup(monkeypatch, tmp_path: Path) -> None:
|
||||
"""The installed file must be what ``shutil.which`` resolves on this host.
|
||||
"""A binary named the way ``get_cbm_path`` expects must be what
|
||||
``shutil.which`` resolves on this host.
|
||||
|
||||
Nothing about the file name is mocked: on Windows this needs the .exe
|
||||
extension, elsewhere the extensionless name with its executable bit.
|
||||
"""
|
||||
bin_dir = tmp_path / "bin"
|
||||
monkeypatch.setenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", "1")
|
||||
bin_dir.mkdir()
|
||||
path = bin_dir / installer._installed_name(platform.system().lower())
|
||||
path.write_bytes(b"#!/bin/sh\necho codebase-memory-mcp test\n")
|
||||
path.chmod(path.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
|
||||
monkeypatch.setattr(installer, "CBM_BIN_DIR", bin_dir)
|
||||
monkeypatch.setattr(
|
||||
installer,
|
||||
"urlopen",
|
||||
lambda url, timeout=60: FakeResponse(_host_release_archive(url.rsplit("/", 1)[-1])),
|
||||
)
|
||||
probes: list[list[str]] = []
|
||||
|
||||
def fake_run(command: list[str], **kwargs) -> SimpleNamespace:
|
||||
probes.append(command)
|
||||
return SimpleNamespace(returncode=1, stdout="")
|
||||
|
||||
monkeypatch.setattr("headroom._subprocess.run", fake_run)
|
||||
|
||||
path = installer.download_cbm()
|
||||
|
||||
assert probes == [[str(path), "--version"]]
|
||||
monkeypatch.setenv("PATH", str(bin_dir))
|
||||
found = shutil.which("codebase-memory-mcp")
|
||||
assert found is not None
|
||||
assert Path(found).samefile(path)
|
||||
assert Path(probes[0][0]).samefile(found)
|
||||
assert installer.get_cbm_path() == Path(found)
|
||||
|
||||
|
||||
@@ -146,19 +47,3 @@ def test_get_cbm_path_finds_the_installed_binary_off_path(monkeypatch, tmp_path:
|
||||
assert installer.get_cbm_path() is None
|
||||
(bin_dir / "codebase-memory-mcp").write_bytes(b"#!/bin/sh\n")
|
||||
assert installer.get_cbm_path() == bin_dir / "codebase-memory-mcp"
|
||||
|
||||
|
||||
def test_download_cbm_zip_errors(monkeypatch, tmp_path: Path) -> None:
|
||||
monkeypatch.setenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", "1")
|
||||
monkeypatch.setattr(installer, "CBM_BIN_DIR", tmp_path)
|
||||
monkeypatch.setattr(installer, "_detect_platform", lambda: "windows-amd64")
|
||||
|
||||
monkeypatch.setattr(
|
||||
installer, "urlopen", lambda url, timeout=60: FakeResponse(_zip_archive("README.md"))
|
||||
)
|
||||
with pytest.raises(RuntimeError, match="binary not found in archive"):
|
||||
installer.download_cbm()
|
||||
|
||||
monkeypatch.setattr(installer, "urlopen", lambda url, timeout=60: FakeResponse(b"not a zip"))
|
||||
with pytest.raises(RuntimeError, match="Failed to extract archive"):
|
||||
installer.download_cbm()
|
||||
|
||||
@@ -1698,7 +1698,7 @@ class TestDoctorNetworkOffline:
|
||||
|
||||
|
||||
class TestInstallDownloadsOffline:
|
||||
"""`headroom install`'s two release downloads.
|
||||
"""`headroom install`'s release download.
|
||||
|
||||
`binaries.py` already had `HEADROOM_BINARIES_OFFLINE`, which is exactly the
|
||||
kind of second, differently-named flag an operator should not have to
|
||||
@@ -1718,13 +1718,6 @@ class TestInstallDownloadsOffline:
|
||||
)
|
||||
assert _refused_hostname(excinfo.value) == "github.com"
|
||||
|
||||
def test_cbm_download_opens_no_socket(self, offline: None, no_sockets: None) -> None:
|
||||
from headroom.graph import installer
|
||||
|
||||
with pytest.raises(OfflineEgressBlocked) as excinfo:
|
||||
installer.download_cbm()
|
||||
assert "HEADROOM_OFFLINE" in str(excinfo.value)
|
||||
|
||||
def test_the_binaries_specific_switch_still_wins_first(
|
||||
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user