Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix: docker install fails with PermissionError in readonly cache dir
chopratejas78d935fcf970
diff --git a/headroom/binaries.py b/headroom/binaries.pyindex 467ec84ee..153085787 100644--- a/headroom/binaries.py+++ b/headroom/binaries.py@@ -470,7 +470,16 @@ def ensure_tools(quiet: bool = False) -> dict[str, Path | None]:out[name] = _path_lookup(name)continueout[name] = resolve(name)- except (PlatformNotSupported, OfflineError, BinaryFetchError, Sha256Mismatch) as e:+ except (+ PlatformNotSupported,+ OfflineError,+ BinaryFetchError,+ Sha256Mismatch,+ # Catch readonly / sandboxed filesystems (e.g. containerized+ # home dirs) so proxy startup never fails because the cache dir+ # can't be created. The interceptor fall back to no-op.+ OSError,+ ) as e:out[name] = Noneif not quiet:print(f"headroom: skipping {name}: {e}", file=sys.stderr)diff --git a/headroom/cli/proxy.py b/headroom/cli/proxy.pyindex dc282fb99..303c9d6ea 100644--- a/headroom/cli/proxy.py+++ b/headroom/cli/proxy.py@@ -276,19 +276,15 @@ def proxy(click.echo(f"Details: {e}")raise SystemExit(1) from None- # Ensure bundled CLI tools (ast-grep, difftastic, scc) are present before- # the proxy starts accepting traffic. Never happens inside a live request —- # tools are downloaded once at startup if missing, then cached per-user.- from headroom.binaries import ensure_tools-- resolved_tools = ensure_tools()-# Opt-in: turn on tool_result interceptors (ast-grep Read outline, etc.).- # The TransformPipeline reads this env var at construction time.+ # Only fetch the bundled CLI tool binaries when the feature is enabled —+ # otherwise we'd pay a network round-trip and risk a readonly-FS failure+ # for capabilities the user hasn't asked for. The TransformPipeline reads+ # this env var at construction time.if intercept_tool_results:- # Validate each interceptor's critical tool actually installed. Today- # only the ast-grep Read outliner is wired; add entries here as- # interceptors gain dependencies.+ from headroom.binaries import ensure_tools++ resolved_tools = ensure_tools()critical_tools = ["ast-grep"]missing = [t for t in critical_tools if not resolved_tools.get(t)]if missing:diff --git a/tests/test_binaries.py b/tests/test_binaries.pyindex 73aa6e885..2472b4e38 100644--- a/tests/test_binaries.py+++ b/tests/test_binaries.py@@ -294,6 +294,31 @@ def test_sha256_match_passes(monkeypatch, fake_urlopen):# -------- status() ------------------------------------------------------- #+def test_ensure_tools_survives_readonly_cache_dir(monkeypatch, tmp_path):+ """Containerized / read-only home dirs must not crash proxy startup.++ Regression test for PermissionError not being caught in ensure_tools().+ """+ _set_platform(monkeypatch, sys_plat="linux", machine="x86_64", musl=False)+ monkeypatch.setattr(binaries.shutil, "which", lambda _name: None)++ # Point the cache at a path we can't create under (readonly parent).+ readonly_parent = tmp_path / "readonly"+ readonly_parent.mkdir()+ readonly_parent.chmod(0o500) # r-x: can't create children+ monkeypatch.setenv("HEADROOM_BINARIES_CACHE", str(readonly_parent / "cache"))++ try:+ # Must return a dict, not raise.+ result = binaries.ensure_tools(quiet=True)+ # Fetched tools couldn't write to cache → None. ast-grep is PyPI-only+ # so its entry depends on PATH, which is not what this test exercises.+ assert result.get("difft") is None+ assert result.get("scc") is None+ finally:+ readonly_parent.chmod(0o700) # so pytest tmp cleanup succeeds++def test_ensure_tools_partial_failure_proxy_still_starts(monkeypatch):"""If one tool fails to fetch, others still install and ensure_tools returns."""_set_platform(monkeypatch, sys_plat="darwin", machine="arm64")