From e2aac4863a4f8e2d4eea944e57da455bd6797feb Mon Sep 17 00:00:00 2001 From: chopratejas Date: Fri, 13 Mar 2026 22:02:47 -0700 Subject: [PATCH] Add wrap commands for Codex/Cursor/Aider with rtk instructions, fix savings metrics - headroom wrap codex: injects rtk instructions into AGENTS.md - headroom wrap cursor: injects into .cursorrules, prints config steps - headroom wrap aider: injects into CONVENTIONS.md, sets both env vars - All with --no-rtk flag to skip rtk setup Unified savings metrics across three layers: - cli_filtering: tokens avoided by rtk before reaching context - compression: tokens removed by proxy (SmartCrusher, etc.) - prefix_cache: provider cache discount with honest attribution Fix flaky test_process_stats_collected when psutil not installed. --- headroom/cli/wrap.py | 462 ++++++++++++++++++++--- headroom/proxy/server.py | 113 +++++- tests/test_memory_tracker_integration.py | 21 +- 3 files changed, 527 insertions(+), 69 deletions(-) diff --git a/headroom/cli/wrap.py b/headroom/cli/wrap.py index 744d3ac10..f42b9d9ad 100644 --- a/headroom/cli/wrap.py +++ b/headroom/cli/wrap.py @@ -2,6 +2,9 @@ Usage: headroom wrap claude # Start proxy + rtk + claude + headroom wrap codex # Start proxy + OpenAI Codex CLI + headroom wrap aider # Start proxy + aider + headroom wrap cursor # Start proxy + print Cursor config instructions headroom wrap claude --no-rtk # Without rtk hooks headroom wrap claude --port 9999 # Custom proxy port headroom wrap claude -- --model opus # Pass args to claude @@ -112,6 +115,184 @@ def _setup_rtk(verbose: bool = False) -> Path | None: return rtk_path +# 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. +RTK_INSTRUCTIONS_BLOCK = """\ + +# RTK (Rust Token Killer) - Token-Optimized Commands + +When running shell commands, **always prefix with `rtk`**. This reduces context +usage by 60-90% with zero behavior change. If rtk has no filter for a command, +it passes through unchanged — so it is always safe to use. + +## Key Commands +```bash +# Git (59-80% savings) +rtk git status rtk git diff rtk git log + +# Files & Search (60-75% savings) +rtk ls rtk read rtk grep +rtk find rtk diff + +# Test (90-99% savings) — shows failures only +rtk pytest tests/ rtk cargo test rtk test + +# Build & Lint (80-90% savings) — shows errors only +rtk tsc rtk lint rtk cargo build +rtk prettier --check rtk mypy rtk ruff check + +# Analysis (70-90% savings) +rtk err rtk log rtk json +rtk summary rtk deps rtk env + +# GitHub (26-87% savings) +rtk gh pr view rtk gh run list rtk gh issue list + +# Infrastructure (85% savings) +rtk docker ps rtk kubectl get rtk docker logs + +# Package managers (70-90% savings) +rtk pip list rtk pnpm install rtk npm run