refactor(proxy): remove dead aliases and wrappers from helpers, body_forwarding, forwarded_headers

When the policy modules were split out of helpers.py, helpers kept a
private alias for each policy constant and a thin wrapper for several
policy functions. Twenty of them are now read by nothing: the
_CODEX_WIRE_* constants, _should_redact_key, _redact_value, the
*_DEFAULT aliases, the SSE terminator and prefix aliases,
_merge_beta_tokens, reset_core_tools_legacy_warn_state,
get_sse_event_max_bytes, body_forwarding._PYTHON_FORWARDER_MODE_DEFAULT
and _original_body_has_signed_thinking_blocks, and
forwarded_headers._header_first. The imports that only fed them go too.

get_sse_event_max_bytes was the only reader of
HEADROOM_SSE_BUFFER_MAX_BYTES and had no caller, so that per-event cap
was never enforced. Removing it changes no behaviour.

Evidence: grep -rnw for each name across headroom, tests, crates,
plugins, sdk, docs, scripts and the consumer repos finds only the
definition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tejas Chopra
2026-10-09 18:02:50 -07:00
co-authored by Claude Opus 5.5
parent a9815bbb5f
commit b005f174e5
3 changed files with 3 additions and 89 deletions
-7
View File
@@ -23,8 +23,6 @@ _PYTHON_FORWARDER_MODE_ENV = python_forwarder_mode_policy.PYTHON_FORWARDER_MODE_
PythonForwarderMode = python_forwarder_mode_policy.PythonForwarderMode
OutboundBodySource = Literal["passthrough", "canonical", "legacy"]
_PYTHON_FORWARDER_MODE_DEFAULT = python_forwarder_mode_policy.PYTHON_FORWARDER_MODE_DEFAULT
@dataclass(frozen=True, slots=True)
class OutboundBody:
@@ -148,11 +146,6 @@ def _parse_original_body(original_body_bytes: bytes | None) -> dict[str, Any] |
return original if isinstance(original, dict) else None
def _original_body_has_signed_thinking_blocks(original_body_bytes: bytes | None) -> bool:
original = _parse_original_body(original_body_bytes)
return original is not None and has_signed_thinking_blocks(original)
#: Allow canonical re-serialization on a thinking-bearing request when every
#: thinking block is provably unchanged. **Default ON**; set this to ``0`` (or
#: ``false``/``no``/``off``) to restore the previous blanket lock.
-12
View File
@@ -59,7 +59,6 @@ from typing import TYPE_CHECKING, Any
from headroom.proxy.forwarded_policy import (
ForwardedHeaderInputs,
header_first,
normalize_ip,
parse_cidr_list,
peer_is_trusted_gateway,
@@ -152,17 +151,6 @@ def _peer_host(request: Any) -> str | None:
return getattr(client, "host", None)
def _header_first(value: str) -> str:
"""Return the leftmost element of a comma-separated header value.
``X-Forwarded-For: client, proxy1, proxy2`` → ``"client"``. Empty
input returns ``""``. We intentionally do NOT walk the chain — the
leftmost hop is the only one whose authenticity the immediate
gateway can vouch for, and beyond that we have no trust signal.
"""
return header_first(value)
def _read_header(request: Any, name: str) -> str:
"""Read a header case-insensitively, ``""`` on miss."""
headers = getattr(request, "headers", None)
+3 -70
View File
@@ -41,17 +41,14 @@ from headroom.proxy import (
from headroom.proxy.beta_header_merge import (
merge_anthropic_beta as merge_anthropic_beta,
)
from headroom.proxy.beta_header_merge import (
merge_beta_tokens,
split_beta_tokens,
)
from headroom.proxy.beta_header_merge import (
merge_openai_beta as merge_openai_beta,
)
from headroom.proxy.beta_header_merge import (
split_beta_tokens,
)
from headroom.proxy.beta_header_policy import (
BETA_HEADER_STICKY_DEFAULT,
BETA_HEADER_STICKY_ENV,
BETA_TRACKER_MAX_SESSIONS_DEFAULT,
BETA_TRACKER_MAX_SESSIONS_ENV,
BetaHeaderStickyMode,
resolve_beta_header_sticky_mode,
@@ -81,8 +78,6 @@ from headroom.proxy.ccr_marker_policy import (
)
from headroom.proxy.ccr_session_tracker import SessionCcrTracker as _SessionCcrTracker
from headroom.proxy.internal_header_policy import (
INTERNAL_HEADER_PREFIX,
STRIP_INTERNAL_HEADERS_DEFAULT,
STRIP_INTERNAL_HEADERS_ENV,
StripInternalHeadersMode,
resolve_strip_internal_headers_mode,
@@ -125,8 +120,6 @@ logger = logging.getLogger("headroom.proxy")
_CODEX_WIRE_DEBUG_ENV = "HEADROOM_CODEX_WIRE_DEBUG"
_CODEX_WIRE_DEBUG_DIR_ENV = "HEADROOM_CODEX_WIRE_DEBUG_DIR"
_CODEX_WIRE_REDACTED = wire_debug_redaction_policy.WIRE_DEBUG_REDACTED
_CODEX_WIRE_SECRET_KEYS = wire_debug_redaction_policy.WIRE_DEBUG_SECRET_KEYS
def codex_wire_debug_enabled() -> bool:
@@ -147,14 +140,6 @@ def _codex_wire_debug_dir() -> Path:
return _paths.codex_wire_debug_dir()
def _should_redact_key(key: str) -> bool:
return wire_debug_redaction_policy.should_redact_key(key)
def _redact_value(value: Any) -> Any:
return wire_debug_redaction_policy.redact_for_wire_debug(value)
def redact_for_wire_debug(value: Any) -> Any:
"""Redact obvious secrets while preserving request/response shape."""
return wire_debug_redaction_policy.redact_for_wire_debug(value)
@@ -268,7 +253,6 @@ def capture_codex_wire_debug(
# "system_prompt" option — that path is permanently retired by I2 (cache hot
# zone never modified). See REALIGNMENT/02-architecture.md §2.2.
_MEMORY_INJECTION_MODE_ENV = memory_injection_mode_policy.MEMORY_INJECTION_MODE_ENV
_MEMORY_INJECTION_MODE_DEFAULT = memory_injection_mode_policy.MEMORY_INJECTION_MODE_DEFAULT
MemoryInjectionMode = memory_injection_mode_policy.MemoryInjectionMode
@@ -1319,25 +1303,6 @@ _DECOMPRESS_CHUNK_SIZE = 64 * 1024
# Maximum SSE buffer size (10MB - prevents memory exhaustion from malformed streams)
MAX_SSE_BUFFER_SIZE = 10 * 1024 * 1024
# Per-event SSE size cap (PR-A8 / P1-8). Configurable via
# HEADROOM_SSE_BUFFER_MAX_BYTES. Guards against pathological huge events
# (a single event > 1 MB by default is treated as an upstream protocol bug
# and surfaces loudly rather than silently growing the buffer).
_SSE_EVENT_MAX_BYTES_ENV = request_limit_policy.SSE_EVENT_MAX_BYTES_ENV
_SSE_EVENT_MAX_BYTES_DEFAULT = request_limit_policy.SSE_EVENT_MAX_BYTES_DEFAULT
def get_sse_event_max_bytes() -> int:
"""Return the per-event SSE size cap.
Read at request time so operators can flip the env var without a
restart. Negative values are rejected loudly (no silent fallback).
"""
return request_limit_policy.resolve_sse_event_max_bytes(
os.environ.get(_SSE_EVENT_MAX_BYTES_ENV)
)
# Well-known OpenAI-compatible upstreams, matched by host against the
# configured ``--openai-api-url``. Used only to label the dashboard/stats
# display provider — the internal provider key stays ``openai`` so pricing
@@ -1404,7 +1369,6 @@ def resolve_display_provider(
# Configurable via HEADROOM_PROXY_BODY_TOO_LARGE_STATUS for operators who need
# to override (no expected production use; documentation knob).
_BODY_TOO_LARGE_STATUS_ENV = request_limit_policy.BODY_TOO_LARGE_STATUS_ENV
_BODY_TOO_LARGE_STATUS_DEFAULT = request_limit_policy.BODY_TOO_LARGE_STATUS_DEFAULT
def get_body_too_large_status() -> int:
@@ -1414,18 +1378,6 @@ def get_body_too_large_status() -> int:
)
_SSE_EVENT_TERMINATORS = sse_byte_buffer_policy.SSE_EVENT_TERMINATORS
def _find_sse_event_terminator(buf: bytearray) -> tuple[int, int] | None:
"""Return the earliest complete SSE event terminator in ``buf``."""
return sse_byte_buffer_policy.find_sse_event_terminator(buf)
_SSE_EVENT_LINE_PREFIX = b"event:"
_SSE_DATA_LINE_PREFIX = b"data:"
def safe_decode_for_logging(raw: bytes, *, max_bytes: int | None = None) -> str:
"""Decode bytes to a string for **log/diagnostic display only**.
@@ -2017,9 +1969,7 @@ def is_anthropic_auth(headers: dict[str, str]) -> bool:
# tell its client about its own work. This helper only filters
# request-side headers.
_INTERNAL_HEADER_PREFIX = INTERNAL_HEADER_PREFIX
_STRIP_INTERNAL_HEADERS_ENV = STRIP_INTERNAL_HEADERS_ENV
_STRIP_INTERNAL_HEADERS_DEFAULT = STRIP_INTERNAL_HEADERS_DEFAULT
def get_strip_internal_headers_mode() -> StripInternalHeadersMode:
@@ -2164,10 +2114,7 @@ def log_outbound_headers(
# explicit per realignment build constraint #4 — NOT a silent fallback.
_BETA_HEADER_STICKY_ENV = BETA_HEADER_STICKY_ENV
_BETA_HEADER_STICKY_DEFAULT = BETA_HEADER_STICKY_DEFAULT
_BETA_TRACKER_MAX_SESSIONS_ENV = BETA_TRACKER_MAX_SESSIONS_ENV
_BETA_TRACKER_MAX_SESSIONS_DEFAULT = BETA_TRACKER_MAX_SESSIONS_DEFAULT
def get_beta_header_sticky_mode() -> BetaHeaderStickyMode:
@@ -2188,9 +2135,6 @@ def get_beta_tracker_max_sessions() -> int:
_split_beta_tokens = split_beta_tokens
_merge_beta_tokens = merge_beta_tokens
class SessionBetaTracker:
"""Bounded LRU tracker of beta-header tokens observed per (provider, session).
@@ -3743,17 +3687,6 @@ _core_tools_legacy_lock = threading.Lock()
_core_tools_legacy_warned = False
def reset_core_tools_legacy_warn_state() -> None:
"""Re-arm the legacy-variable warning. Test helper only.
Without this the flag leaks between tests in a process, so a caplog
assertion on this warning passes alone and fails depending on ordering.
"""
global _core_tools_legacy_warned
with _core_tools_legacy_lock:
_core_tools_legacy_warned = False
def _core_tools_override() -> str | None:
"""Return the resident-tool override from either env var, canonical first."""
global _core_tools_legacy_warned