mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
## Description Four small proxy helpers have no caller in the product. This PR removes them. - `tenant_key._get_auth_mode_str` is not referenced anywhere, tests included. - `tool_schema_compaction.invalidate_cache` is not referenced anywhere either. The private consumers that import this module use other names: `compact_tools`, `compact_tool_descriptions` and `tool_desc_max_chars`. - `proxy_mode_policy.normalize_proxy_mode_value` is a one-line wrapper over `normalize_proxy_mode_decision(...).normalized`. Only two tests used it. They now call `normalize_proxy_mode_decision` directly and still check the same aliases. - `rate_limit_policy.stale_bucket_keys` was used only by its own test, which goes with it. `rate_limiter.py` does not call it. Evidence: I ran `grep -rnw <name>` over `headroom`, `crates`, `plugins`, `sdk`, `examples`, `benchmarks`, `scripts`, `e2e`, `docs`, `wiki`, `.github`, `pyproject.toml`, `tests` and the private consumer repos. It found only the definitions and the tests named above. Refs #4136 ## Type of Change - [ ] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [x] Code refactoring (no functional changes) ## Changes Made - Delete `_get_auth_mode_str` (in `tenant_key.py`), `invalidate_cache` (in `tool_schema_compaction.py`), `normalize_proxy_mode_value` (in `proxy_mode_policy.py`) and `stale_bucket_keys` (in `rate_limit_policy.py`). - `tests/test_rate_limit_policy.py`: remove the `stale_bucket_keys` test. - `tests/test_proxy_mode_policy.py` and `tests/test_install/test_proxy_mode_default.py`: call `normalize_proxy_mode_decision(...).normalized` instead of the removed wrapper. The alias coverage is unchanged. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [ ] New tests added for new functionality - [x] Manual testing performed ### Test Output ```text $ pytest -q -p no:cacheprovider tests/test_proxy_mode_policy.py tests/test_install/test_proxy_mode_default.py tests/test_rate_limit_policy.py tests/test_tenant_key.py tests/test_proxy_tenant_key_integration.py tests/test_tool_schema_compaction.py tests/test_openai_chat_tool_desc_compaction.py tests/test_proxy_modes.py ============================== 82 passed in 3.52s ============================== $ uvx ruff@0.16.8 check <7 touched files> All checks passed! $ mypy headroom/proxy/tenant_key.py headroom/proxy/tool_schema_compaction.py headroom/proxy/proxy_mode_policy.py headroom/proxy/rate_limit_policy.py --ignore-missing-imports Success: no issues found in 4 source files ``` ## Real Behavior Proof - Environment: macOS, Python 3.12.13, this branch ata56df516, using the repo .venv. Every run used a temp `HOME`, `HEADROOM_WORKSPACE_DIR` and `CODEX_HOME`, plus `HEADROOM_OFFLINE=1`. - Exact command / steps: - (1) Ran the shared smoke harness in token mode: `smoke.sh <worktree> --mode token`. It starts a real `headroom proxy --mode token` against a local fake upstream, sends one Anthropic and one OpenAI request, reads `/stats` and greps `proxy.log`. - (2) Called the live neighbours of each removed helper directly: - `normalize_proxy_mode_decision` on aliases; - `TokenBucketRateLimiter.check_request` three times at 2 requests per minute; - `compact_tools` on a tool with a `$schema` key. - Observed result: ```text (1) branch: cleanup/proxy-core-b-unused-helpers @a56df516health: 200 POST /v1/messages -> HTTP200 smoke-ok POST /v1/chat/completions -> HTTP200 smoke-ok stats: requests=2 failed=0 tokens_saved=13574 savings_percent=99.82 proxy.log WARNING/ERROR lines: only the expected air-gap ones (offline mode, Kompress not cached). (2) mode token_savings -> token mode cache_mode -> cache mode bogus -> token rate limit [True, True, False] compact_tools modified: True 95 -> 81 ``` - Not tested: A live proxy against a real provider. None of the removed functions was on any runtime path. ## Runtime Rollout Safety - Rollout-managed feature(s): None. - Minimum rollout channel: N/A; dead-code removal. - Stable/default behavior changed: No. - Kill switch / disable path: N/A. - Unsafe override required: No. - Qualification impact: None. - Rollback path: Revert this commit. ## 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 - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] 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 did **not** edit `CHANGELOG.md` — it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this) ## Screenshots (if applicable) N/A ## Additional Notes Part of maintainer-requested cleanup #4136 (dead code, duplication, debuggability), split into small PRs for review. `verbosity_controller.py` is also unused, but it is left for a maintainer decision and is not touched here. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
77 lines
3.1 KiB
Python
77 lines
3.1 KiB
Python
"""`headroom install` must default to cache mode, like `headroom proxy` does.
|
|
|
|
#1893 shipped the coding/cache posture as Headroom's out-of-box default, but it
|
|
only touched `cli/proxy.py` and `proxy/server.py` — the install path kept the
|
|
older `token` default from #1404. Since `planner.py` writes `HEADROOM_MODE` into
|
|
the install env, an installed Headroom actively *overrode* the good server default
|
|
with the cache-busting one.
|
|
|
|
Cache mode freezes prior turns and compresses only the newest delta ("~0
|
|
prefix-cache busts"); token mode rewrites frozen history, which moves the cached
|
|
prefix bytes and forces a full cold re-write. These tests pin the agreement so the
|
|
two entry points cannot drift apart again.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from headroom.install.models import DeploymentManifest
|
|
from headroom.proxy.proxy_mode_policy import PROXY_MODE_CACHE
|
|
|
|
|
|
def _mode_option_default(command) -> str:
|
|
"""The declared default of a command's ``--mode`` option."""
|
|
for param in command.params:
|
|
if param.name == "proxy_mode":
|
|
return str(param.default)
|
|
raise AssertionError(f"{command.name} has no --mode/proxy_mode option")
|
|
|
|
|
|
def test_install_apply_defaults_to_cache_mode() -> None:
|
|
from headroom.cli.install import install_apply
|
|
|
|
assert _mode_option_default(install_apply) == PROXY_MODE_CACHE
|
|
|
|
|
|
def test_deploy_defaults_to_cache_mode() -> None:
|
|
from headroom.cli.install import deploy
|
|
|
|
assert _mode_option_default(deploy) == PROXY_MODE_CACHE
|
|
|
|
|
|
def test_manifest_default_is_cache_mode() -> None:
|
|
"""A manifest that omits proxy_mode must not fall back to token."""
|
|
assert DeploymentManifest.__dataclass_fields__["proxy_mode"].default == PROXY_MODE_CACHE
|
|
|
|
|
|
def test_install_and_proxy_agree_on_the_default() -> None:
|
|
"""The whole point: both entry points land on the same posture.
|
|
|
|
`headroom proxy` resolves `mode or HEADROOM_MODE or PROXY_MODE_CACHE`, so its
|
|
default is PROXY_MODE_CACHE. Install must match, or installing Headroom
|
|
silently changes the compression posture versus running it directly.
|
|
"""
|
|
from headroom.cli.install import deploy, install_apply
|
|
|
|
assert _mode_option_default(install_apply) == _mode_option_default(deploy) == PROXY_MODE_CACHE
|
|
|
|
|
|
def test_token_mode_is_still_reachable() -> None:
|
|
"""Changing the default must not take the choice away.
|
|
|
|
The option carries no restrictive ``type``, and the normalizer still accepts
|
|
token (plus its aliases), so `--mode token` remains available to anyone who
|
|
wants maximum compression and accepts the prefix-cache busts.
|
|
"""
|
|
from headroom.cli.install import deploy, install_apply
|
|
from headroom.proxy.proxy_mode_policy import (
|
|
PROXY_MODE_TOKEN,
|
|
normalize_proxy_mode_decision,
|
|
)
|
|
|
|
for command in (install_apply, deploy):
|
|
param = next(p for p in command.params if p.name == "proxy_mode")
|
|
assert param.type.name == "text", f"{command.name} --mode became restrictive"
|
|
|
|
assert normalize_proxy_mode_decision("token").normalized == PROXY_MODE_TOKEN
|
|
assert normalize_proxy_mode_decision("token_headroom").normalized == PROXY_MODE_TOKEN
|