mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-27 14:17:10 -04:00
## Description
Headroom exposes plenty of *quantitative* compression telemetry (token
counts, ratios, `headroom perf`, `/metrics`) but no way to actually
**see what the compressor changed** in the content. That makes it hard
to trust compression or debug a quality regression ("did it drop
something I cared about?").
This adds a `headroom inspect` command (the issue's Option 1). It reads
the proxy's existing loopback `/transformations/feed` endpoint — which
already carries the pre/post-compression message snapshots when the
proxy runs with `--log-messages` — and renders, per request, the
original vs compressed content for each message with the changed
segments highlighted. No new dependencies (stdlib `difflib`).
```
headroom inspect # inspect the most recent request
headroom inspect --last 5 # the 5 most recent
headroom inspect --full # include unchanged messages
headroom inspect --format json # raw feed for offline tooling
```
Per request it shows the model, per-request token counts + savings, the
transforms applied, and a colorized unified diff of each changed message
(red = removed, green = added). Clear errors when no proxy is reachable
or when the proxy wasn't started with `--log-messages`.
Side-by-side / interactive rendering (the fuller form of Option 1) can
follow as a polish pass; this lands the core "see what changed"
capability on data Headroom already captures.
Closes #1267
## Type of Change
- [x] New feature (non-breaking change that adds functionality)
## Changes Made
- `headroom/cli/inspect.py`: new `inspect` command +
content-flattening/diff-render helpers.
- `headroom/cli/__init__.py`, `headroom/cli/main.py`: register the
command.
- `tests/test_cli_inspect.py`: unit tests (content extraction, the
no-proxy / no-`--log-messages` / empty-feed paths, text render, json
output).
- `wiki/cli.md`: document the command + options.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check`)
- [x] New tests added
### Test Output
```text
$ pytest tests/test_cli_inspect.py -q
7 passed
$ ruff check headroom/cli/ tests/test_cli_inspect.py
All checks passed!
```
## Real Behavior Proof
- Environment: repo main @ HEAD, local venv
- Exact command / steps: invoked the `inspect` command against a mocked
`/transformations/feed` payload (one request, a user message with a line
removed by SmartCrusher).
- Observed result: header shows `req-1 gpt-4o`, `tokens 100 → 40 (saved
60, 60.0%)`, `transforms: SmartCrusher`, and a unified diff with the
removed line on the original side; no-proxy and missing-`--log-messages`
cases raise actionable errors; `--format json` emits the raw feed.
- Not tested: live end-to-end against a real proxy with `--log-messages`
(the data source — the feed endpoint — is exercised via the mocked
payload that mirrors its shape).
## 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 made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove the feature works
114 lines
3.7 KiB
Python
114 lines
3.7 KiB
Python
"""Main CLI entry point for Headroom."""
|
|
|
|
import click
|
|
|
|
CLI_CONTEXT_SETTINGS = {"help_option_names": ["--help", "-?"]}
|
|
|
|
|
|
def get_version() -> str:
|
|
"""Get the current version."""
|
|
try:
|
|
from headroom._version import __version__
|
|
|
|
return __version__
|
|
except ImportError:
|
|
return "unknown"
|
|
|
|
|
|
@click.group(context_settings=CLI_CONTEXT_SETTINGS)
|
|
@click.version_option(get_version(), "--version", "-v", prog_name="headroom")
|
|
@click.pass_context
|
|
def main(ctx: click.Context) -> None:
|
|
"""Headroom - The Context Optimization Layer for LLM Applications.
|
|
|
|
Manage memories, run the optimization proxy, and analyze metrics.
|
|
|
|
\b
|
|
Examples:
|
|
headroom proxy Start the optimization proxy
|
|
headroom memory list List stored memories
|
|
headroom memory stats Show memory statistics
|
|
headroom update Update Headroom to the latest release
|
|
"""
|
|
ctx.ensure_object(dict)
|
|
|
|
# Apply file-backed settings (settings.json) to the process environment
|
|
# BEFORE Click parses any subcommand's ``envvar=`` options (Click resolves
|
|
# those when it builds the subcommand context, which happens after this
|
|
# group callback runs). ``os.environ.setdefault`` keeps explicit shell
|
|
# exports authoritative over the stored file. Fail-open so a corrupt
|
|
# settings.json can never block the CLI.
|
|
try:
|
|
from headroom import settings_store
|
|
|
|
settings_store.apply_to_environ(settings_store.load())
|
|
except Exception: # noqa: BLE001 — settings load must never break the CLI
|
|
pass
|
|
|
|
# Fire a rate-limited, opt-out background check for newer releases so other
|
|
# surfaces (e.g. the proxy banner) can show an "update available" notice.
|
|
# Never blocks, never raises; skipped for `update` (it checks explicitly).
|
|
if ctx.invoked_subcommand != "update":
|
|
try:
|
|
from headroom.update_check import maybe_check_async
|
|
|
|
maybe_check_async()
|
|
except Exception: # noqa: BLE001 — update check must never break the CLI
|
|
pass
|
|
|
|
|
|
# Import subcommands - these register themselves with the main group
|
|
def _register_commands() -> None:
|
|
"""Register all subcommand groups."""
|
|
from . import (
|
|
agent_savings, # noqa: F401
|
|
audit, # noqa: F401
|
|
capture, # noqa: F401
|
|
copilot_auth, # noqa: F401
|
|
doctor, # noqa: F401
|
|
evals, # noqa: F401
|
|
init, # noqa: F401
|
|
inspect, # noqa: F401
|
|
install, # noqa: F401
|
|
learn, # noqa: F401
|
|
mcp, # noqa: F401
|
|
output_savings, # noqa: F401
|
|
perf, # noqa: F401
|
|
proxy, # noqa: F401
|
|
recover, # noqa: F401
|
|
savings, # noqa: F401
|
|
tools, # noqa: F401
|
|
update, # noqa: F401
|
|
wrap, # noqa: F401
|
|
)
|
|
|
|
# Memory CLI requires numpy/hnswlib — optional
|
|
try:
|
|
from . import memory # noqa: F401
|
|
except ImportError:
|
|
pass
|
|
|
|
|
|
_register_commands()
|
|
|
|
|
|
def _apply_help_aliases(command: click.Command) -> None:
|
|
"""Ensure `-?` works everywhere in the Click command tree."""
|
|
context_settings = dict(command.context_settings or {})
|
|
help_option_names = list(context_settings.get("help_option_names", []))
|
|
if "--help" not in help_option_names:
|
|
help_option_names.append("--help")
|
|
if "-?" not in help_option_names:
|
|
help_option_names.append("-?")
|
|
context_settings["help_option_names"] = help_option_names
|
|
command.context_settings = context_settings
|
|
|
|
if isinstance(command, click.Group):
|
|
for child in command.commands.values():
|
|
_apply_help_aliases(child)
|
|
|
|
|
|
_apply_help_aliases(main)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|