@@ -2988,18 +2988,32 @@ def _count_tool_tokens(value: object) -> int:
29882988 )
29892989 from headroom .proxy .output_shaper import (
29902990 OutputShaperSettings ,
2991+ apply_verbosity_steering ,
29912992 classify_turn ,
29922993 resolve_verbosity_level ,
29932994 shape_request ,
29942995 )
29952996
2997+ # The level an established frozen prefix was built at outranks
2998+ # the CURRENT configuration. Dropping the tail from a prefix
2999+ # built with it busts the whole provider cache exactly like
3000+ # appending one to a prefix built without it — so the pin is
3001+ # read before the enablement and arm gates and replayed below
3002+ # when those no longer hold (shaper switched off live, rollout
3003+ # channel moved, holdout drift into control). Only an explicit
3004+ # request bypass, handled by the enclosing guard, may sacrifice
3005+ # the cache.
3006+ _pinned = getattr (prefix_tracker , "output_shaping_level" , None )
3007+ _pinned_established = frozen_message_count > 0 and _pinned is not None
3008+
29963009 _shaper_settings = OutputShaperSettings .from_env (
29973010 enabled = (
29983011 self .config .rollout .is_enabled ("proxy_output_shaper" )
29993012 if getattr (self .config , "rollout" , None ) is not None
30003013 else None
30013014 )
30023015 )
3016+ _arm = "control"
30033017 if _shaper_settings .enabled :
30043018 # Conversation-stable holdout assignment: a whole
30053019 # conversation is treatment or control. This keeps the A/B
@@ -3027,48 +3041,56 @@ def _count_tool_tokens(value: object) -> int:
30273041 # outcome funnel can feed the savings ledger from any path.
30283042 transforms_applied .append (stratum_label (_arm , _stratum ))
30293043
3030- if _arm == "treatment" :
3031- # The system prompt is the head of the provider's cache
3032- # prefix, so the shaping tail has to stay byte-identical
3033- # for every turn of a conversation whose frozen prefix
3034- # is established. All three drifts invalidate that whole
3035- # prefix: appending the tail to a prefix built without
3036- # it, dropping the tail from one built with it, and
3037- # swapping it for another level's text. The level the
3038- # conversation was established at is therefore pinned on
3039- # the tracker and replayed until the prefix thaws.
3040- _pinned = getattr (prefix_tracker , "output_shaping_level" , None )
3041- _level , _src = resolve_verbosity_level (_shaper_settings )
3042- if frozen_message_count > 0 and _pinned is None :
3044+ if _shaper_settings .enabled and _arm == "treatment" :
3045+ # The system prompt is the head of the provider's cache
3046+ # prefix, so the shaping tail has to stay byte-identical
3047+ # for every turn of a conversation whose frozen prefix
3048+ # is established. All three drifts invalidate that whole
3049+ # prefix: appending the tail to a prefix built without
3050+ # it, dropping the tail from one built with it, and
3051+ # swapping it for another level's text. The level the
3052+ # conversation was established at is therefore pinned on
3053+ # the tracker and replayed until the prefix thaws.
3054+ _level , _src = resolve_verbosity_level (_shaper_settings )
3055+ if frozen_message_count > 0 and _pinned is None :
3056+ logger .info (
3057+ f"[{ request_id } ] OutputShaper: skipped — frozen "
3058+ f"prefix ({ frozen_message_count } messages) predates "
3059+ "the shaping tail; preserving provider cache"
3060+ )
3061+ else :
3062+ if _pinned_established and _pinned != _level :
30433063 logger .info (
3044- f"[{ request_id } ] OutputShaper: skipped — frozen "
3045- f"prefix ({ frozen_message_count } messages) predates "
3046- "the shaping tail; preserving provider cache"
3064+ f"[{ request_id } ] OutputShaper: L{ _level } /{ _src } "
3065+ f"pinned to L{ _pinned } — the frozen prefix "
3066+ f"({ frozen_message_count } messages) carries "
3067+ "that tail"
30473068 )
3048- else :
3049- if (
3050- frozen_message_count > 0
3051- and _pinned is not None
3052- and _pinned != _level
3053- ):
3054- logger .info (
3055- f"[{ request_id } ] OutputShaper: L{ _level } /{ _src } "
3056- f"pinned to L{ _pinned } — the frozen prefix "
3057- f"({ frozen_message_count } messages) carries "
3058- "that tail"
3059- )
3060- _level , _src = _pinned , "pinned"
3061- shape_result = shape_request (
3062- body , _shaper_settings , level_override = _level
3069+ _level , _src = _pinned , "pinned"
3070+ shape_result = shape_request (body , _shaper_settings , level_override = _level )
3071+ prefix_tracker .output_shaping_level = _level
3072+ if shape_result .changed :
3073+ body_mutation_tracker .mark_mutated ("output_shaper" )
3074+ transforms_applied .extend (shape_result .labels or [])
3075+ logger .info (
3076+ f"[{ request_id } ] OutputShaper(L{ _level } /{ _src } ): "
3077+ f"{ shape_result .labels } "
30633078 )
3064- prefix_tracker .output_shaping_level = _level
3065- if shape_result .changed :
3066- body_mutation_tracker .mark_mutated ("output_shaper" )
3067- transforms_applied .extend (shape_result .labels or [])
3068- logger .info (
3069- f"[{ request_id } ] OutputShaper(L{ _level } /{ _src } ): "
3070- f"{ shape_result .labels } "
3071- )
3079+ elif _pinned_established :
3080+ # Replay only. The conversation is no longer being shaped
3081+ # (disabled, or control), but its frozen prefix already
3082+ # carries the L{_pinned} tail, so those exact bytes have to
3083+ # keep going out. Deliberately narrower than the treatment
3084+ # path: no experiment label (attribution belongs to the live
3085+ # assignment, not to a cache replay) and no effort routing
3086+ # (that lever never enters the cached prefix).
3087+ if _pinned > 0 and apply_verbosity_steering (body , _pinned ):
3088+ body_mutation_tracker .mark_mutated ("output_shaper" )
3089+ logger .info (
3090+ f"[{ request_id } ] OutputShaper: replaying pinned L{ _pinned } — "
3091+ f"the frozen prefix ({ frozen_message_count } messages) carries "
3092+ "that tail; shaping is off or in control"
3093+ )
30723094
30733095 # Unit 2: mark end of pre-upstream phase. Everything after this
30743096 # point is upstream I/O or post-response bookkeeping.
0 commit comments