Commit Graph
4 Commits
Author SHA1 Message Date
Tejas ChopraandClaude Opus 5.5 098df1c74e refactor(proxy): deprecate is_anthropic_auth (#4083)
## Description

`headroom.proxy.helpers.is_anthropic_auth` is re-exported from
`headroom.proxy.server`, so it counts as public API. Nothing in the
product calls it. Request routing (`/v1/models`, the catch-all
passthrough, rate-limit keys) uses
`headroom.providers.registry._is_anthropic_auth` instead. That
classifier also handles a capitalised `Authorization` header and Claude
Code's user-agent. The two copies have already drifted apart, and the
tests were guarding the copy that no request reaches.

This PR deprecates `is_anthropic_auth` rather than deleting it. The
function and the `server.py` re-export stay, and calling it now emits a
`DeprecationWarning` that points at the registry classifier. The routing
unit tests are repointed at the classifier the router actually uses.

Evidence: `grep -rnw is_anthropic_auth` across `headroom`, `tests`,
`docs`, `scripts`, `crates`, `sdk`, `plugins` and the private consumer
repos. It finds only the definition, the `server.py` re-export, and
`tests/test_oauth_bearer_routing.py` / `tests/test_pr208_changes.py`.

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

- `headroom/proxy/helpers.py`: `is_anthropic_auth` emits
`DeprecationWarning("is_anthropic_auth is deprecated and will be removed
in a future release; request routing uses the auth classifier in
headroom.providers.registry.")` and has a `.. deprecated::` note. Its
return values are unchanged.
- `tests/test_oauth_bearer_routing.py`: the nine `TestIsAnthropicAuth`
cases now import the live classifier, and every assertion still holds. A
new case checks that a Claude Code user-agent with a gateway bearer
token routes to Anthropic.
- `tests/test_pr208_changes.py`: the duplicate six-case
`TestIsAnthropicAuth` class is replaced by one test that asserts the
deprecation warning fires.
- Use instead: nothing outside the router needs this check. Code that
does should match the router's behaviour, which lives in
`headroom.providers.registry`.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

```text
$ pytest -q -p no:cacheprovider tests/test_oauth_bearer_routing.py tests/test_pr208_changes.py
============================== 64 passed in 7.43s ==============================
$ uvx ruff@0.16.8 check headroom/proxy/helpers.py tests/test_oauth_bearer_routing.py tests/test_pr208_changes.py
All checks passed!
$ python -W error::DeprecationWarning -c "import headroom.proxy.server"
(exit 0: importing the server stays quiet; only a call warns)
```

## Real Behavior Proof

- Environment: macOS, Python 3.12.13, branch checkout at ad2d2148, using
the repo .venv. A temp HOME, workspace and CODEX_HOME were used, with
offline mode on.
- Exact command / steps:
1. Ran the shared smoke harness. It starts a real proxy from this
checkout against a local fake upstream and sends one Anthropic request
(x-api-key) and one OpenAI request through the router.
2. Called the deprecated function through its `server.py` re-export,
with warnings shown: `python -W always -c "from headroom.proxy.server
import is_anthropic_auth; print(is_anthropic_auth({'x-api-key':
'k'}))"`.
- Observed result:
```text
branch: cleanup/proxy-core-a-deprecate-is-anthropic-auth @ ad2d2148
health: 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 (<tmp>):
(smoke dir: <tmp>)
```
```text
<string>:1: DeprecationWarning: is_anthropic_auth is deprecated and will be removed in a future release; request routing uses the auth classifier in headroom.providers.registry.
True
```
- Routing is unaffected: both providers return 200, and the proxy adds
no warnings.
  - Importing through the re-export is silent.
- A call warns once, at the caller's line, and still returns the same
result.
- Two pre-existing offline-mode and Kompress-prefetch warnings are left
out of the smoke output.
- Not tested: Third-party callers of the deprecated function. They will
see the DeprecationWarning and nothing else changes for them.

## Runtime Rollout Safety

- Rollout-managed feature(s): None.
- Minimum rollout channel: N/A; deprecation only.
- Stable/default behavior changed: No. The function returns the same
values. Only a DeprecationWarning is added, and no product code path
calls the function.
- 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
- [x] 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
- [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 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 a maintainer-requested codebase cleanup (dead code, duplication,
debuggability), split into small PRs for review. It is part of
maintainer-requested cleanup #4136.

Removing the function and the `server.py` re-export is left for a later
release, after the deprecation period. mypy was not run because no
signatures changed. The function is not in the docs, so no docs change
was needed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 09:02:20 -07:00
SwiftWing21andClaude Sonnet 4.6 3d2d894a33 fix(proxy): defer file logging install to create_app()
Move _setup_file_logging() from module import to create_app() so
importing headroom.proxy.server in tests or library contexts no longer
silently attaches a RotatingFileHandler to the user's live proxy.log.

This was discovered via PR #303: running the test suite produced
"Optimization failed: TimeoutError" entries in ~/.headroom/logs/proxy.log
because TestClient instantiations from test_proxy_anthropic_compression_diagnostics.py
inherited the module-level handler.

Add a regression test that imports the server module in a subprocess
and asserts no RotatingFileHandler is attached to the headroom logger.

Refs #303 (review thread).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-28 15:28:33 -07:00
JerrettDavisandClaude Opus 4.6 0d48d4cd58 test(pr208): add coverage for is_anthropic_auth, file logging, repro helpers, and wrap paths
Boosts patch coverage by testing uncovered code paths in the PR diff:
- is_anthropic_auth header detection (6 cases)
- _setup_file_logging with new _headroom_log_dir path + OSError handling
- repro_codex_replay: _http_to_ws_url, _classify_exit, format_summary, build_parser
- CodexWsStats.record_error and AnthropicHttpStats dataclasses
- LatencyHistogram percentile boundary conditions
- wrap.py _get_log_path using headroom.paths module

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-20 14:13:45 -05:00
JerrettDavisandClaude Opus 4.6 eb83f403d7 test: add coverage for PR #208 changes
Tests for jitter_delay_ms helper, asyncio.timeout shim, release workflow
--allow-same-version flag validation, SIGKILL fallback on Windows, and
LatencyHistogram from the repro harness script.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-20 13:57:09 -05:00