mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
refactor(proxy): remove unreferenced and test-only helpers
Four small proxy helpers had no product caller: - tenant_key._get_auth_mode_str: no reference anywhere, tests included. - tool_schema_compaction.invalidate_cache: no reference anywhere; the consumers that import this module use other names. - proxy_mode_policy.normalize_proxy_mode_value: a one-line wrapper over normalize_proxy_mode_decision(...).normalized used only by two tests, which now call normalize_proxy_mode_decision directly and keep their alias coverage. - rate_limit_policy.stale_bucket_keys: used only by its own test, removed with it; rate_limiter.py does not call it. Evidence: grep -w for each name across headroom, crates, plugins, sdk, examples, benchmarks, scripts, e2e, docs, wiki, .github, pyproject.toml, tests and the consumer repos finds only the definitions and the tests above. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a9815bbb5f
commit
a56df51688
@@ -56,12 +56,3 @@ def normalize_proxy_mode_decision(
|
||||
normalized=normalized,
|
||||
alias_used=key != normalized,
|
||||
)
|
||||
|
||||
|
||||
def normalize_proxy_mode_value(
|
||||
mode: str | None,
|
||||
*,
|
||||
default: str = PROXY_MODE_TOKEN,
|
||||
) -> str:
|
||||
"""Return only the canonical proxy mode value."""
|
||||
return normalize_proxy_mode_decision(mode, default=default).normalized
|
||||
|
||||
@@ -36,14 +36,3 @@ def consume_from_bucket(
|
||||
|
||||
wait_seconds = (required - available_tokens) * (60.0 / rate_per_minute)
|
||||
return False, available_tokens, wait_seconds
|
||||
|
||||
|
||||
def stale_bucket_keys(
|
||||
last_updates: dict[str, float],
|
||||
*,
|
||||
now: float,
|
||||
stale_after_seconds: float,
|
||||
) -> list[str]:
|
||||
"""Return bucket keys whose last update is older than the stale threshold."""
|
||||
stale_before = now - stale_after_seconds
|
||||
return [key for key, last_update in last_updates.items() if last_update < stale_before]
|
||||
|
||||
@@ -245,26 +245,6 @@ def _get_header(request: Any, name: str) -> str:
|
||||
return str(value)
|
||||
|
||||
|
||||
def _get_auth_mode_str(request: Any) -> str:
|
||||
"""Read ``request.state.auth_mode`` as a string, or ``""`` if unset.
|
||||
|
||||
F1 set this to an :class:`AuthMode` enum (a ``str`` subclass) on
|
||||
every request. We don't import ``AuthMode`` here to keep this
|
||||
module independent of the classifier — we only need the string
|
||||
value, and the enum's ``str`` parent serializes transparently.
|
||||
"""
|
||||
state = getattr(request, "state", None)
|
||||
if state is None:
|
||||
return ""
|
||||
auth_mode = getattr(state, "auth_mode", None)
|
||||
if auth_mode is None:
|
||||
return ""
|
||||
# AuthMode subclasses str so ``str(auth_mode)`` returns the enum
|
||||
# name's value (``"payg"`` / ``"oauth"`` / ``"subscription"``) —
|
||||
# not the Python repr.
|
||||
return str(auth_mode)
|
||||
|
||||
|
||||
def _sanitize_tenant_key(raw: str) -> str:
|
||||
"""Sanitize a tenant_key candidate. Returns ``""`` on rejection.
|
||||
|
||||
|
||||
@@ -193,12 +193,6 @@ def _cache_put(key: str, compacted: dict[str, Any], before: int, after: int) ->
|
||||
_compaction_cache[key] = (compacted, before, after)
|
||||
|
||||
|
||||
def invalidate_cache() -> None:
|
||||
"""Clear the compaction cache (e.g. on config change)."""
|
||||
with _cache_lock:
|
||||
_compaction_cache.clear()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Layer 1: annotation-key compaction
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -65,12 +65,12 @@ def test_token_mode_is_still_reachable() -> None:
|
||||
from headroom.cli.install import deploy, install_apply
|
||||
from headroom.proxy.proxy_mode_policy import (
|
||||
PROXY_MODE_TOKEN,
|
||||
normalize_proxy_mode_value,
|
||||
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_value("token") == PROXY_MODE_TOKEN
|
||||
assert normalize_proxy_mode_value("token_headroom") == PROXY_MODE_TOKEN
|
||||
assert normalize_proxy_mode_decision("token").normalized == PROXY_MODE_TOKEN
|
||||
assert normalize_proxy_mode_decision("token_headroom").normalized == PROXY_MODE_TOKEN
|
||||
|
||||
@@ -6,7 +6,6 @@ from headroom.proxy.proxy_mode_policy import (
|
||||
PROXY_MODE_CACHE,
|
||||
PROXY_MODE_TOKEN,
|
||||
normalize_proxy_mode_decision,
|
||||
normalize_proxy_mode_value,
|
||||
)
|
||||
|
||||
|
||||
@@ -47,6 +46,6 @@ def test_decision_uses_default_and_marks_unknown_for_invalid_mode() -> None:
|
||||
assert decision.unknown is True
|
||||
|
||||
|
||||
def test_value_helper_returns_only_canonical_mode() -> None:
|
||||
assert normalize_proxy_mode_value("token_savings") == PROXY_MODE_TOKEN
|
||||
assert normalize_proxy_mode_value("cache_mode") == PROXY_MODE_CACHE
|
||||
def test_aliases_normalize_to_canonical_mode() -> None:
|
||||
assert normalize_proxy_mode_decision("token_savings").normalized == PROXY_MODE_TOKEN
|
||||
assert normalize_proxy_mode_decision("cache_mode").normalized == PROXY_MODE_CACHE
|
||||
|
||||
@@ -5,7 +5,6 @@ from __future__ import annotations
|
||||
from headroom.proxy.rate_limit_policy import (
|
||||
consume_from_bucket,
|
||||
refilled_tokens,
|
||||
stale_bucket_keys,
|
||||
)
|
||||
|
||||
|
||||
@@ -95,11 +94,3 @@ def test_consume_from_bucket_debt_delays_the_next_request() -> None:
|
||||
assert allowed is False
|
||||
assert remaining == -132_000
|
||||
assert wait_seconds == 79.8
|
||||
|
||||
|
||||
def test_stale_bucket_keys_returns_only_old_buckets() -> None:
|
||||
assert stale_bucket_keys(
|
||||
{"fresh": 950, "edge": 400, "stale": 399},
|
||||
now=1000,
|
||||
stale_after_seconds=600,
|
||||
) == ["stale"]
|
||||
|
||||
Reference in New Issue
Block a user