mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-10 14:27:00 -04:00
## Description
Adds first-class Grok Build support to Headroom so Grok CLI sessions can
route through the local proxy for context compression and savings
tracking.
This PR introduces `headroom wrap grok-build` / `headroom unwrap
grok-build`, a `grok_build` provider slice, Grok MCP registrar support,
and install/telemetry wiring so Grok traffic is attributed correctly in
the proxy and dashboard.
Review follow-up (`9368c413`): when users already own
`[model.grok-build]` in `~/.grok/config.toml`, wrap rewrites `base_url`
in that table in place instead of appending a duplicate header (invalid
TOML).
## Type of Change
- [x] New feature (non-breaking change that adds functionality)
## Changes Made
- Added `headroom/providers/grok_build/` with runtime helpers,
reversible `~/.grok/config.toml` injection, and install env builders.
- Added `headroom wrap grok-build` and `headroom unwrap grok-build` CLI
commands.
- Added `GrokRegistrar` for Headroom MCP registration in Grok config.
- Wired `grok_build` into install planner/registry, agent savings,
telemetry, and proxy client detection (`grok/` user agent).
- **Review fix:** rewrite `base_url` inside an existing user-owned
`[model.grok-build]` table in place (`# was: …` metadata).
- Added regression tests + docs (`grok-build.mdx`, `proxy.mdx`) and
CHANGELOG entry.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] New tests added for new functionality
- [x] Manual testing performed
### Test Output
```text
$ pytest -q tests/test_provider_grok_build.py tests/test_mcp_registry/test_grok_registrar.py
============================== 12 passed in 1.13s ==============================
```
See **Screenshots** below for terminal captures (pytest, review-fix
in-place rewrite, proxy `/readyz`, unwrap).
## Real Behavior Proof
- Environment: macOS, Python 3.11.12 venv, feat/grok-build @ `9368c413`,
isolated `GROK_HOME` temp dirs, proxy port 8799
- Exact command / steps: see screenshot evidence (wrap/unwrap, in-place
table rewrite, `/readyz`)
- Observed result: see screenshots — 12 tests pass; single
`[model.grok-build]` table after wrap on pre-existing config; proxy
healthy; unwrap restores backup
- Not tested: Live interactive Grok chat with xAI auth through the proxy
## 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
- [x] 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 have updated the CHANGELOG.md if applicable
## Screenshots (if applicable)
Terminal captures from local verification (`9368c413`). Assets hosted on
fork prerelease only — **not** in the source tree.
**1. Pytest — 12 passed (incl. review-fix regression)**

**2. Review fix — in-place `[model.grok-build]` rewrite (single table,
`# was:` metadata)**

**3. Proxy health — `/readyz` healthy on port 8799**

**4. Unwrap — restores pre-wrap backup**

## Additional Notes
Screenshot assets:
https://github.com/aashishtamsya/headroom/releases/tag/pr-1629-evidence
(temporary prerelease; safe to delete after merge).
---------
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
"""Tests for pure auth and client classification policy."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from headroom.proxy.auth_policy import (
|
|
AuthMode,
|
|
AuthSignals,
|
|
classify_auth_signals,
|
|
classify_client_signals,
|
|
should_stamp_codex_client_signals,
|
|
)
|
|
|
|
|
|
def test_subscription_user_agent_wins_over_oauth_token() -> None:
|
|
signals = AuthSignals(
|
|
user_agent="claude-code/1.5.0 (linux; x86_64)",
|
|
authorization="Bearer sk-ant-oat01-abc123",
|
|
)
|
|
|
|
assert classify_auth_signals(signals) is AuthMode.SUBSCRIPTION
|
|
|
|
|
|
def test_oauth_bearer_token_shapes_are_oauth() -> None:
|
|
jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0In0.signature"
|
|
|
|
assert classify_auth_signals(AuthSignals(authorization="Bearer sk-ant-oat01-abc")) is (
|
|
AuthMode.OAUTH
|
|
)
|
|
assert classify_auth_signals(AuthSignals(authorization=f"Bearer {jwt}")) is AuthMode.OAUTH
|
|
|
|
|
|
def test_payg_key_shapes_are_payg() -> None:
|
|
assert classify_auth_signals(AuthSignals(authorization="Bearer sk-ant-api03-abc")) is (
|
|
AuthMode.PAYG
|
|
)
|
|
assert classify_auth_signals(AuthSignals(x_api_key="sk-ant-api03-abc")) is AuthMode.PAYG
|
|
assert classify_auth_signals(AuthSignals(x_goog_api_key="AIzaSyDUMMY")) is AuthMode.PAYG
|
|
|
|
|
|
def test_client_explicit_override_wins_over_user_agent() -> None:
|
|
signals = AuthSignals(user_agent="claude-code/1.2.3", x_client=" AIDER ")
|
|
|
|
assert classify_client_signals(signals) == "aider"
|
|
|
|
|
|
def test_grok_build_user_agent_is_subscription_client() -> None:
|
|
signals = AuthSignals(user_agent="grok/1.2.3")
|
|
|
|
assert classify_auth_signals(signals) is AuthMode.SUBSCRIPTION
|
|
assert classify_client_signals(signals) == "grok_build"
|
|
|
|
|
|
def test_codex_stamp_only_for_unidentified_responses_callers() -> None:
|
|
assert should_stamp_codex_client_signals("/v1/responses", AuthSignals()) is True
|
|
assert (
|
|
should_stamp_codex_client_signals(
|
|
"/v1/responses/foo",
|
|
AuthSignals(user_agent="codex-cli/0.5"),
|
|
)
|
|
is False
|
|
)
|
|
assert should_stamp_codex_client_signals("/v1/chat/completions", AuthSignals()) is False
|