mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
refactor(proxy): parse HEADROOM_OUTPUT_HOLDOUT in one place
The same try/float(runtime_env.getenv('HEADROOM_OUTPUT_HOLDOUT', '0') or '0')/except ValueError -> 0.0 parse was written five times. The copies were in gateway_turn.shape_gateway_body, the Anthropic chat handler, the OpenAI chat handler, the OpenAI Responses shaper, and openai._output_shaping_holdout_fraction. Each new shaper path copied it again.
It now lives once, as output_shaper.output_holdout_fraction(), next to the other live shaper knobs that already read runtime_env. Every caller imports it alongside the shaper functions it already uses. The parse is unchanged, so behaviour is identical, including hot-reload through runtime_env.
Evidence: grep -rn HEADROOM_OUTPUT_HOLDOUT headroom | grep getenv now finds only output_shaper.py.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: claude-code:claude-opus-5-5
Headline-Session: 6e77a807-8dd4-4a77-a2fa-ddce6dc2515e
Headline-AI-Share: 38% strict / 38% weighted
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a9815bbb5f
commit
a1098e6ce9
@@ -1178,7 +1178,6 @@ def shape_gateway_body(
|
||||
and the block is re-appended deterministically. Never raises.
|
||||
"""
|
||||
try:
|
||||
from headroom.proxy import runtime_env
|
||||
from headroom.proxy.output_savings import (
|
||||
assign_arm,
|
||||
conversation_key_from_body,
|
||||
@@ -1188,6 +1187,7 @@ def shape_gateway_body(
|
||||
from headroom.proxy.output_shaper import (
|
||||
OutputShaperSettings,
|
||||
classify_turn,
|
||||
output_holdout_fraction,
|
||||
resolve_verbosity_level,
|
||||
shape_openai_chat_request,
|
||||
shape_request,
|
||||
@@ -1201,11 +1201,7 @@ def shape_gateway_body(
|
||||
)
|
||||
if not settings.enabled:
|
||||
return False
|
||||
try:
|
||||
holdout = float(runtime_env.getenv("HEADROOM_OUTPUT_HOLDOUT", "0") or "0")
|
||||
except ValueError:
|
||||
holdout = 0.0
|
||||
arm = assign_arm(conversation_key_from_body(provider_body), holdout)
|
||||
arm = assign_arm(conversation_key_from_body(provider_body), output_holdout_fraction())
|
||||
messages = provider_body.get("messages")
|
||||
turn_kind = classify_turn(messages if isinstance(messages, list) else []).value
|
||||
stratum = stratum_key(
|
||||
|
||||
@@ -3558,6 +3558,7 @@ class AnthropicHandlerMixin:
|
||||
from headroom.proxy.output_shaper import (
|
||||
OutputShaperSettings,
|
||||
classify_turn,
|
||||
output_holdout_fraction,
|
||||
resolve_verbosity_level,
|
||||
shape_request,
|
||||
)
|
||||
@@ -3571,15 +3572,8 @@ class AnthropicHandlerMixin:
|
||||
# conversation is treatment or control. This keeps the A/B
|
||||
# comparison clean AND keeps the prefix cache stable (we
|
||||
# never flip a conversation's system-prompt tail mid-stream).
|
||||
from headroom.proxy import runtime_env
|
||||
|
||||
_holdout = 0.0
|
||||
try:
|
||||
_holdout = float(runtime_env.getenv("HEADROOM_OUTPUT_HOLDOUT", "0") or "0")
|
||||
except ValueError:
|
||||
_holdout = 0.0
|
||||
_conversation = conversation_key_from_body(body)
|
||||
_arm = assign_arm(_conversation, _holdout)
|
||||
_arm = assign_arm(_conversation, output_holdout_fraction())
|
||||
|
||||
# Stratum from request features observable now (mirrors the
|
||||
# offline baseline so live and learned strata line up).
|
||||
|
||||
@@ -840,7 +840,6 @@ def _shape_openai_responses_payload(
|
||||
must not be able to break request forwarding.
|
||||
"""
|
||||
try:
|
||||
from headroom.proxy import runtime_env
|
||||
from headroom.proxy.output_savings import (
|
||||
assign_arm,
|
||||
conversation_key_from_responses_body,
|
||||
@@ -851,6 +850,7 @@ def _shape_openai_responses_payload(
|
||||
from headroom.proxy.output_shaper import (
|
||||
OutputShaperSettings,
|
||||
classify_responses_turn,
|
||||
output_holdout_fraction,
|
||||
resolve_verbosity_level,
|
||||
shape_responses_request,
|
||||
)
|
||||
@@ -859,10 +859,7 @@ def _shape_openai_responses_payload(
|
||||
if not settings.enabled:
|
||||
return [], False
|
||||
|
||||
try:
|
||||
holdout = float(runtime_env.getenv("HEADROOM_OUTPUT_HOLDOUT", "0") or "0")
|
||||
except ValueError:
|
||||
holdout = 0.0
|
||||
holdout = output_holdout_fraction()
|
||||
conversation = conversation_key_from_responses_body(payload)
|
||||
arm = assign_arm(conversation, holdout)
|
||||
|
||||
@@ -1550,15 +1547,6 @@ def _openai_responses_from_sse(sse_text: str) -> dict[str, Any] | None:
|
||||
return completed
|
||||
|
||||
|
||||
def _output_shaping_holdout_fraction() -> float:
|
||||
from headroom.proxy import runtime_env
|
||||
|
||||
try:
|
||||
return float(runtime_env.getenv("HEADROOM_OUTPUT_HOLDOUT", "0") or "0")
|
||||
except ValueError:
|
||||
return 0.0
|
||||
|
||||
|
||||
def _shape_openai_responses_for_output(
|
||||
payload: dict[str, Any],
|
||||
*,
|
||||
@@ -1578,6 +1566,7 @@ def _shape_openai_responses_for_output(
|
||||
OutputShaperSettings,
|
||||
ShapeResult,
|
||||
classify_openai_responses_input,
|
||||
output_holdout_fraction,
|
||||
resolve_verbosity_level,
|
||||
shape_openai_responses_request,
|
||||
)
|
||||
@@ -1589,7 +1578,7 @@ def _shape_openai_responses_for_output(
|
||||
|
||||
assert result.labels is not None
|
||||
key = conversation_key or conversation_key_from_body(payload)
|
||||
arm = assign_arm(key, _output_shaping_holdout_fraction())
|
||||
arm = assign_arm(key, output_holdout_fraction())
|
||||
turn_kind = classify_openai_responses_input(payload.get("input")).value
|
||||
stratum = stratum_key(
|
||||
turn_kind=turn_kind,
|
||||
@@ -4932,7 +4921,6 @@ class OpenAIHandlerMixin:
|
||||
# Mutating `body` in place is sufficient here — the outbound request
|
||||
# serializes `body` fresh, so no body-mutation tracker is needed.
|
||||
if not _bypass:
|
||||
from headroom.proxy import runtime_env
|
||||
from headroom.proxy.output_savings import (
|
||||
assign_arm,
|
||||
conversation_key_from_body,
|
||||
@@ -4943,6 +4931,7 @@ class OpenAIHandlerMixin:
|
||||
from headroom.proxy.output_shaper import (
|
||||
OutputShaperSettings,
|
||||
classify_turn,
|
||||
output_holdout_fraction,
|
||||
resolve_verbosity_level,
|
||||
shape_openai_chat_request,
|
||||
)
|
||||
@@ -4956,13 +4945,8 @@ class OpenAIHandlerMixin:
|
||||
# or control, which keeps the A/B comparison clean and the
|
||||
# provider prefix cache stable (the steering block never flips
|
||||
# mid-conversation).
|
||||
_holdout = 0.0
|
||||
try:
|
||||
_holdout = float(runtime_env.getenv("HEADROOM_OUTPUT_HOLDOUT", "0") or "0")
|
||||
except ValueError:
|
||||
_holdout = 0.0
|
||||
_conversation = conversation_key_from_body(body)
|
||||
_arm = assign_arm(_conversation, _holdout)
|
||||
_arm = assign_arm(_conversation, output_holdout_fraction())
|
||||
_turn_kind = classify_turn(body.get("messages", [])).value
|
||||
_stratum = stratum_key(
|
||||
turn_kind=_turn_kind,
|
||||
|
||||
@@ -135,6 +135,18 @@ class OutputShaperSettings:
|
||||
)
|
||||
|
||||
|
||||
def output_holdout_fraction() -> float:
|
||||
"""Fraction of conversations held out of shaping (``HEADROOM_OUTPUT_HOLDOUT``).
|
||||
|
||||
Read live, like the other shaper knobs. Unset or unparseable means 0.0 (no
|
||||
holdout); :func:`~headroom.proxy.output_savings.assign_arm` clamps the range.
|
||||
"""
|
||||
try:
|
||||
return float(runtime_env.getenv("HEADROOM_OUTPUT_HOLDOUT", "0") or "0")
|
||||
except ValueError:
|
||||
return 0.0
|
||||
|
||||
|
||||
def shaper_enabled_for(config: Any) -> bool | None:
|
||||
"""Resolve the output-shaper gate for a proxy config.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user