Skip to content

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.

Integrate codebase-memory-mcp, disable CodeCompressor

chopratejasd8a4ed3dc99d
diff --git a/headroom/cli/wrap.py b/headroom/cli/wrap.py
index c530398f0..3ef486ea9 100644
--- a/headroom/cli/wrap.py
+++ b/headroom/cli/wrap.py
@@ -182,6 +182,79 @@ def _setup_rtk(verbose: bool = False) -> Path | None:
return rtk_path
+def _setup_code_graph(verbose: bool = False) -> bool:
+ """Ensure codebase-memory-mcp is installed and project is indexed.
+
+ codebase-memory-mcp builds a knowledge graph of the codebase using
+ tree-sitter, enabling the LLM to query code structure (call chains,
+ function definitions, impact analysis) instead of reading entire files.
+
+ With Claude Code's MCP Tool Search, the 14 graph tools add ~200 tokens
+ overhead per request (not the full ~1,915) — they're lazy-loaded.
+
+ Returns True if graph is ready, False if setup failed.
+ """
+ cbm_bin = shutil.which("codebase-memory-mcp")
+ if not cbm_bin:
+ if verbose:
+ click.echo(" Code graph: codebase-memory-mcp not found, skipping")
+ click.echo(
+ " Install: curl -fsSL https://raw.githubusercontent.com/"
+ "DeusData/codebase-memory-mcp/main/install.sh | sh"
+ )
+ return False
+
+ # Index current project (fast — ~1s for most repos, idempotent)
+ project_dir = str(Path.cwd())
+ try:
+ result = subprocess.run(
+ [
+ cbm_bin,
+ "cli",
+ "index_repository",
+ json.dumps({"repo_path": project_dir, "mode": "fast"}),
+ ],
+ capture_output=True,
+ text=True,
+ timeout=30,
+ )
+ if result.returncode == 0:
+ # Parse node/edge counts from output
+ for line in result.stdout.splitlines():
+ if '"nodes"' in line:
+ try:
+ # Parse the JSON response to extract node/edge counts
+ import re
+
+ m_nodes = re.search(r'"nodes":(\d+)', line)
+ m_edges = re.search(r'"edges":(\d+)', line)
+ if m_nodes and m_edges:
+ nodes = int(m_nodes.group(1))
+ edges = int(m_edges.group(1))
+ click.echo(
+ f" Code graph: indexed ({nodes:,} symbols, "
+ f"{edges:,} relationships)"
+ )
+ else:
+ click.echo(" Code graph: indexed")
+ except (ValueError, AttributeError):
+ click.echo(" Code graph: indexed")
+ return True
+ click.echo(" Code graph: indexed")
+ return True
+ else:
+ if verbose:
+ click.echo(f" Code graph: indexing failed ({result.stderr[:100]})")
+ return False
+ except subprocess.TimeoutExpired:
+ click.echo(" Code graph: indexing timed out (will complete in background)")
+ return False
+ except Exception as e:
+ if verbose:
+ click.echo(f" Code graph: setup failed ({e})")
+ return False
+
+
# rtk instructions for tools without hook support (Codex, Cursor, Aider).
# These get injected into AGENTS.md / .cursorrules so the LLM voluntarily
# uses rtk-prefixed commands. Kept concise to minimize instruction overhead.
@@ -464,6 +537,7 @@ def _launch_tool(
*,
learn: bool = False,
agent_type: str = "unknown",
+ no_code_graph: bool = False,
backend: str | None = None,
anyllm_provider: str | None = None,
region: str | None = None,
@@ -492,6 +566,9 @@ def _launch_tool(
region=region,
)
+ if not no_code_graph:
+ _setup_code_graph(verbose=False)
+
click.echo()
click.echo(f" Launching {tool_label} (API routed through Headroom)...")
for var in env_vars_display:
@@ -779,6 +856,9 @@ def unwrap() -> None:
@wrap.command(context_settings={"ignore_unknown_options": True})
@click.option("--port", "-p", default=8787, type=int, help="Proxy port (default: 8787)")
@click.option("--no-rtk", is_flag=True, help="Skip rtk installation and hook registration")
+ "--no-code-graph", is_flag=True, help="Skip code graph indexing (codebase-memory-mcp)"
+)
@click.option("--no-proxy", is_flag=True, help="Skip proxy startup (use existing proxy)")
@click.option(
"--learn", is_flag=True, help="Enable live traffic learning (patterns saved to MEMORY.md)"
@@ -789,6 +869,7 @@ def unwrap() -> None:
def claude(
port: int,
no_rtk: bool,
+ no_code_graph: bool,
no_proxy: bool,
learn: bool,
verbose: bool,
@@ -803,11 +884,11 @@ def claude(
\b
Examples:
- headroom wrap claude # Start everything
- headroom wrap claude --resume <id> # Resume a session
- headroom wrap claude -- -p # Claude in print mode
- headroom wrap claude --port 9999 # Custom proxy port
- headroom wrap claude --no-rtk # Skip rtk (proxy only)
+ headroom wrap claude # Start everything
+ headroom wrap claude --resume <id> # Resume a session
+ headroom wrap claude -- -p # Claude in print mode
+ headroom wrap claude --no-code-graph # Skip code graph
+ headroom wrap claude --no-rtk # Skip rtk (proxy only)
"""
if prepare_only:
if not no_rtk:
@@ -841,6 +922,11 @@ def claude(
elif verbose:
click.echo(" Skipping rtk (--no-rtk)")
+ if not no_code_graph:
+ _setup_code_graph(verbose=verbose)
+ elif verbose:
+ click.echo(" Skipping code graph (--no-code-graph)")
+
click.echo()
click.echo(" Launching Claude Code (API routed through Headroom)...")
click.echo(f" ANTHROPIC_BASE_URL=http://127.0.0.1:{port}")
@@ -1019,6 +1105,9 @@ def copilot(
@wrap.command(context_settings={"ignore_unknown_options": True})
@click.option("--port", "-p", default=8787, type=int, help="Proxy port (default: 8787)")
@click.option("--no-rtk", is_flag=True, help="Skip rtk installation and AGENTS.md injection")
+ "--no-code-graph", is_flag=True, help="Skip code graph indexing (codebase-memory-mcp)"
+)
@click.option("--no-proxy", is_flag=True, help="Skip proxy startup (use existing proxy)")
@click.option(
"--learn", is_flag=True, help="Enable live traffic learning (patterns saved to AGENTS.md)"
@@ -1042,6 +1131,7 @@ def copilot(
def codex(
port: int,
no_rtk: bool,
+ no_code_graph: bool,
no_proxy: bool,
learn: bool,
backend: str | None,
@@ -1107,6 +1197,7 @@ def codex(
env_vars_display=[f"OPENAI_BASE_URL=http://127.0.0.1:{port}/v1"],
learn=learn,
agent_type="codex",
+ no_code_graph=no_code_graph,
backend=backend,
anyllm_provider=anyllm_provider,
region=region,
@@ -1121,6 +1212,9 @@ def codex(
@wrap.command(context_settings={"ignore_unknown_options": True})
@click.option("--port", "-p", default=8787, type=int, help="Proxy port (default: 8787)")
@click.option("--no-rtk", is_flag=True, help="Skip rtk installation and conventions injection")
+ "--no-code-graph", is_flag=True, help="Skip code graph indexing (codebase-memory-mcp)"
+)
@click.option("--no-proxy", is_flag=True, help="Skip proxy startup (use existing proxy)")
@click.option("--learn", is_flag=True, help="Enable live traffic learning")
@click.option(
@@ -1134,6 +1228,7 @@ def codex(
def aider(
port: int,
no_rtk: bool,
+ no_code_graph: bool,
no_proxy: bool,
learn: bool,
backend: str | None,
@@ -1193,6 +1288,7 @@ def aider(
],
learn=learn,
agent_type="aider",
+ no_code_graph=no_code_graph,
backend=backend,
anyllm_provider=anyllm_provider,
region=region,
diff --git a/headroom/proxy/models.py b/headroom/proxy/models.py
index c9a837ac7..cb9f37834 100644
--- a/headroom/proxy/models.py
+++ b/headroom/proxy/models.py
@@ -110,8 +110,8 @@ class ProxyConfig:
ccr_proactive_expansion: bool = True
ccr_max_proactive_expansions: int = 2
- # Code-aware compression
- code_aware_enabled: bool = True
+ # Code-aware compression (disabled by default — use code graph tools instead)
+ code_aware_enabled: bool = False
# Per-tool compression profiles
tool_profiles: dict[str, Any] | None = None
diff --git a/headroom/transforms/content_router.py b/headroom/transforms/content_router.py
index 7e4399a7b..c7c051887 100644
--- a/headroom/transforms/content_router.py
+++ b/headroom/transforms/content_router.py
@@ -388,7 +388,7 @@ class ContentRouterConfig:
"""
# Enable/disable specific compressors
- enable_code_aware: bool = True
+ enable_code_aware: bool = False # Disabled: use code graph MCP tools instead
enable_kompress: bool = True # Kompress: ModernBERT token compressor
enable_smart_crusher: bool = True
enable_search_compressor: bool = True
@@ -397,7 +397,7 @@ class ContentRouterConfig:
enable_image_optimizer: bool = True # Image token optimization
# Routing preferences
- prefer_code_aware_for_code: bool = True
+ prefer_code_aware_for_code: bool = False # Disabled: let code pass through unmangled
mixed_content_threshold: int = 2 # Min types to consider mixed
min_section_tokens: int = 20 # Min tokens to compress a section