+
    iHàjQ  ã                  óØ  € R t ^ RIHt ^ RIt^ RIHtHtHtHt ^ RI	H
t
 ^ RIHt ^ RIHt ^ RIHt ^ RIHt ]P&                  ! ]4      tR	tR
tRtRRRR/R R lltRRRR/R R lltRR/R R lltR]3/R R lltR:R R lltRsR R lt RRRR/R R  llt!R!t"R"t#R# R$ lt$R% R& lt%R' R( lt&R) R* lt'R+ R, lt(R- R. lt)]t*R/ R0 lt+]"]#R1R3R2 R3 llt,R;t-R:R4 R5 llt.R6 R7 lt/R8 R9 lt0R# )<af  Native OpenAI Responses server-side compaction on supported OpenAI routes.

``context_management=[{"type": "compaction", "compact_threshold": N}]`` makes the server
summarize older context into an opaque ``compaction`` item once the input crosses N tokens.
Deliberately narrow: gpt-5.6 on api.openai.com or the ChatGPT Codex backend, plus gpt-6-astra
(and its ``-900k`` picker alias) on official Codex OAuth. The local compressor
stays armed as fallback (native threshold clamped below the local trigger); compaction items
ride the ``codex_reasoning_items`` sidecar. No transport imports (shared gate, no cycles).
)ÚannotationsN)ÚAnyÚDictÚListÚOptional)Úurlsplit)Úis_official_codex_base_url)Úis_compaction_summary_message)Úflatten_message_text)Úis_astra_modeli    i@ zgpt-5.6ÚproviderÚbase_urlc               ó(   € V ^8„  d   QhRRRRRRRR/# )é   ÚmodelúOptional[str]r   r   ÚreturnÚbool© )Úformats   "Ú</home/ubuntu/.hermes/hermes-agent/agent/native_compaction.pyÚ__annotate__r       s,   € ÷ 	ñ 	Øð	Ø'4ð	ØGTð	à	ñ	ó    c               ó
  € \         T ;'       g    RP                  4       9   ;'       g\    \        V 4      ;'       dI    T;'       g    RP                  4       P                  4       R8H  ;'       d    \	        T;'       g    R4      # )zƒPreserve gpt-5.6 eligibility; Astra (``-900k`` is a picker alias of the same wire slug)
additionally requires official Codex OAuth.Ú zopenai-codex)Ú_ELIGIBLE_MODEL_MARKERÚlowerr   Ústripr   )r   r   r   s   &$$r   Úis_native_compaction_modelr       st   € ô
 " e§k k¨r×%8Ñ%8Ó%:Ñ:÷ ð Ü�uÓ÷ 	7ð 	7Ø�^ˆ^˜×"Ñ"Ó$×*Ñ*Ó,°Ñ>÷	7ð 	7ä& x§~ ~°2Ó6ðr   Úis_codex_backendFc          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )	r   r   r   r   r   r   r   r   zdict[str, bool]r   )r   s   "r   r   r   ,   s;   € ÷ `ñ `Øð`Ø'4ð`Ø@Mð`Øhlð`àñ`r   c                óÐ   € T;'       g    RP                  4       P                  4       R8H  ;'       d    V'       * pR\        WVR7      ;'       d    T;'       g    \        WR7      /# )z¢Resolve the native-compaction capability for a runtime destination (a resolved ``False``
is distinct from "unresolved" and must survive model switches unchanged).r   ÚopenaiÚnative_compaction©r   r   ©r   )r   r   r   Úis_direct_openai_route)r   r   r   r   Údirect_defaults   $$$$ r   Ú&resolve_native_compaction_capabilitiesr(   ,   sg   € ð
 —n�n "×+Ñ+Ó-×3Ñ3Ó5¸ÑA×RÐRÈ(Äl€NØÔ!;¸EÐ_gÔ!h÷ "_ð "_Ø×]Ð]Ô0°Ô]ð`ð `r   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r   r   r   r   r   r   )r   s   "r   r   r   6   s"   € ÷ (ñ ( ]ð (Èð (ÐZ^ñ (r   c               ó´   € V'       d   R#  \        T ;'       g    R4      P                  ;'       g    RP                  4       pVR8H  #   \         d     R# i ; i)uF   True for api.openai.com or the ChatGPT Codex backend â€” nothing else.Tr   Fzapi.openai.com)r   Úhostnamer   Ú
ValueError)r   r   r+   s   &$ r   r&   r&   6   sV   € çÙðÜ˜XŸ^˜^¨Ó,×5Ñ5×;Ð;¸×BÑBÓDˆð Ð'Ñ'Ð'øô ô Úðús   ŒA šA ²A ÁAÁAÚrejectc               ó$   € V ^8„  d   QhRRRRRR/# )r   Úvaluer   r-   Útupler   úOptional[int]r   )r   s   "r   r   r   A   s!   € ÷ *ñ *˜ð *¨ð *¸]ñ *r   c               óŽ   € V e   \        W4      '       d   R#  \        V 4      pT^ 8”  d   T# R#   \        \        3 d     R# i ; i)zL``int(value)`` when it is a positive integer-like (never a bool), else None.N)Ú
isinstanceÚintÚ	TypeErrorr,   )r/   r-   Úparseds   &$ r   Ú_positive_intr7   A   sN   € à‚}œ
 5×1Ò1ÙðÜ�U“ˆð ˜a”Zˆ6Ð) TÐ)øô ”zÐ"ô Úðús   ™/ ¯AÁAc               ó$   € V ^8„  d   QhRRRRRR/# )r   Úconfigured_thresholdr   Úlocal_trigger_tokensr   r4   r   )r   s   "r   r   r   L   s"   € ÷ .ñ .°Cð .Èsð .Ð^añ .r   c                ó  € \        V4      pVf   RM3\        RV\        8”  d   V\        ,
          M\        VR,          4      4      p\        V \        \
        3R7      pVf   Ve   V# \        # Vf   V# \        R\        WC4      4      # )a  Resolve automatic mode or clamp an explicit native threshold.

Omitted/invalid follows the local compressor trigger minus the safety margin. An
explicit positive integer is absolute unless it must be clamped so native compaction
fires first. Booleans are never thresholds.
Ni   gš™™™™™é?)r-   )r7   ÚmaxÚLOCAL_TRIGGER_SAFETY_MARGINr4   r   ÚfloatÚDEFAULT_COMPACT_THRESHOLDÚmin)r9   r:   ÚlocalÚupperÚ
configureds   &&   r   Úresolve_compact_thresholdrD   L   s‡   € ô Ð.Ó/€EØ’M‰D¤sØ°eÔ>YÔ6YˆuÔ2Ö2Ô_bÐchÐknÕcnÓ_oó(q€EäÐ3¼TÄ5¸MÔJ€JØÒØÒ)ˆuÐHÔ/HÐHØ‚}ØÐÜˆu”c˜*Ó,Ó-Ð-r   c               ó   € V ^8„  d   QhRR/# )r   r   ÚNoner   )r   s   "r   r   r   a   s   € ÷ 	
ñ 	
¸tñ 	
r   c                 óP   € \         '       g   Rs \        P                  R4       R# R# )zILog once per process; the suppression itself is re-evaluated per request.Tz¸compression.checkpoint_required is enabled: server-side native compaction (context_management) is disabled for this agent so the checkpoint-aware Hermes compressor stays authoritative.N)Ú_checkpoint_suppression_loggedÚloggerÚwarningr   r   r   Ú5_warn_native_compaction_suppressed_by_checkpoint_gaterK   a   s'   € ÷ *Ó)Ø)-Ð&Ü�‰ðFö	
ñ *r   Úis_xai_responsesÚis_github_responsesc          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )	r   Úagentr   r   r   rL   rM   r   zOptional[list[dict[str, Any]]]r   )r   s   "r   r   r   m   s9   € ÷ Dñ D°ð DÈ$ð DÐbfð DØ>BðDØOmñDr   c          	     ó¬  € \        V RR4      p\        V\        4      '       d   VP                  RR4      '       g   R# \        V RR4      '       d   \        V RR4      '       g   R# \        V RR4      RJ d   \	        4        R# V'       g>   V'       g6   \        \        V R	R4      \        V R
R4      \        V RR4      R7      '       g   R# \        \        V R/ 4      P                  RR4      4      pV'       g!   \        \        V RR4      VR7      '       g   R# \        V RR4      pVe   \        VRR4      MRp\        \        V RR4      V4      pRRRV/.# )a  Return the ``context_management`` payload for this request, or None ("do not send").

Every gate is re-checked per request so a mid-session model switch or the in-session
kill switch (``agent.codex_responses_native_compaction = False``) takes effect next call.
Úruntime_capabilitiesNr#   FÚ!codex_responses_native_compactionÚcompression_enabledTÚcompression_checkpoint_requiredr   r   r   r$   ÚcapabilitiesÚopenai_native_compactionr%   Úcontext_compressorÚthreshold_tokensÚ!codex_responses_compact_thresholdÚtypeÚ
compactionÚcompact_threshold)	Úgetattrr3   ÚdictÚgetrK   r   r   r&   rD   )	rO   r   rL   rM   rU   Útrusted_proxyÚ
compressorÚlocal_triggerÚ	thresholds	   &$$$     r   Ú$native_compaction_context_managementrd   m   sD  € ô ˜5Ð"8¸$Ó?€LÜ�,¤×%Ò%¨l×.>Ñ.>Ð?RÐTY×.ZÒ.ZÙä�5Ð=¸u×EÒEÌWÐUZÐ\qÐsw×MxÒMxÙô ˆuÐ7¸Ó?À4ÓGÜ=Ô?Ùß×.Ô6PÜ��w Ó%´¸¸zÈ4Ó0PÜ˜ 
¨DÓ1÷7ó 7ñ Üœ ¨¸Ó;×?Ñ?Ð@ZÐ\aÓbÓc€MßÔ!7¼ÀÀzÐSWÓ8XÐk{×!|Ó!|Ùä˜Ð 4°dÓ;€JØEOÒE[”G˜JÐ(:¸DÔAÐae€MÜ)¬'°%Ð9\Ð^bÓ*cÐerÓs€IØ�\Ð#6¸	ÐBÐCÐCr   i ú  i }  c               ó    € V ^8„  d   QhRRRR/# )r   ÚtextÚstrr   r4   r   )r   s   "r   r   r   •   s   € ÷ /ñ /˜ð / ñ /r   c                ó2   € ^ RI Hp \        ^V! V 4      4      # )zXRetention cost of one carried-over text; never 0 so an empty item still consumes budget.)Úestimate_tokens_rough)Úagent.model_metadatari   r<   )rf   ri   s   & r   Ú_approx_tokensrk   •   s   € å:ÜˆqÑ'¨Ó-Ó.Ð.r   c               ó$   € V ^8„  d   QhRRRRRR/# )r   rf   rg   Úbudgetr4   r   r   )r   s   "r   r   r   ›   s!   € ÷ ñ ˜cð ¨3ð °3ñ r   c                ó  € ^ RI Hp V R\        ^ V4      V,           pVP                  4       '       d   V# ^ \	        V4      rTWE8  d9   WE,           ^,           ^,          p\        V RV 4      V8:  d   TpK3  V^,
          pK>  V RV # )zÌLongest prefix of ``text`` whose ``_approx_tokens`` fits ``budget``. A plain ``budget * CHARS_PER_TOKEN``
char slice would keep ~2x the budget of Cyrillic and ~4x of CJK; ASCII still gets that many chars.)ÚCHARS_PER_TOKENN)rj   ro   r<   ÚisasciiÚlenrk   )rf   rm   ro   ÚheadÚloÚhiÚmids   &&     r   Ú_head_within_budgetrv   ›   s|   € õ 5ØÐ1”�Q˜“ /Õ1Ð2€DØ‡|�|‡~‚~ØˆØ”�D“	ˆØ
Œ'Ø�w˜�{˜qÕ ˆÜ˜$˜t ˜*Ó%¨Ô/ØŠBà�q•ŠBØ��ˆ9Ðr   c               ó    € V ^8„  d   QhRRRR/# )r   Úitemr   r   r   r   )r   s   "r   r   r   ¬   s   € ÷ *ñ *˜Sð * ]ñ *r   c                óä  € \        V \        4      '       g   R# V P                  R4      pVf   RV 9   d   V P                  R4      p\        V\        4      '       d   VP	                  4       '       d   V# R# \        V\
        4      '       g   R# . pV F±  pV3p\        V\        4      '       d~   VP                  R4      pVP                  R4      ;'       g+    VP                  R4      ;'       g    VP                  R4      \        V\        4      '       d   VP                  R4      MR3pVP                  R V 4       4       K³  	  RP                  V4      pVP	                  4       '       d   V# R# )	zKMeasurable text from a Responses item (string/multipart/metadata), or None.NÚcontentÚoutput_textÚmetadatarf   Ú
input_textc              3  ó¢   "  € T FE  p\        V\        4      '       g   K  VP                  4       '       g   K3  VP                  4       x € KG  	  R # 5i©N)r3   rg   r   )Ú.0Úcs   & r   Ú	<genexpr>Ú%_extract_item_text.<locals>.<genexpr>¾   s-   é € ÐW©
 1´jÀÄC×6H”YÈQÏWÉWÏY”Y�Q—W‘W—Y�Y«
ùs   ‚AŸA·AÚ )r3   r^   r_   rg   r   ÚlistÚextendÚjoin)rx   rz   ÚpartsÚpartÚ
candidatesÚ	part_metarf   s   &      r   Ú_extract_item_textrŒ   ¬   s%  € ä�dœD×!Ò!ÙØ�h‰h�yÓ!€GØ‚˜=¨DÔ0Ø—(‘(˜=Ó)ˆÜ�'œ3×ÒØ!Ÿ-™-Ÿ/š/ˆwÐ3¨tÐ3Ü�gœt×$Ò$ÙØ€EÛˆØ!˜Gˆ
Ü�dœD×!Ò!ØŸ™ Ó,ˆIØŸ(™( 6Ó*×_Ð_¨d¯h©h°|Ó.D×_Ð_ÈÏÉÐQ^ÓH_Ü3=¸iÌ×3NÒ3N˜)Ÿ-™-¨Ô/ÐTXðZˆJà�‰ÑW©
ÓWÖWñ ð �8‰8�E‹?€DØ—:‘:—<’<ˆ4Ð) TÐ)r   c               ó    € V ^8„  d   QhRRRR/# )r   rz   r   r   r1   r   )r   s   "r   r   r   Ã   s   € ÷ Añ A Cð A¨Mñ Ar   c                óÚ   € \        V \        4      '       dC   V '       d;   \        ;QJ d    R V  4       F  '       d   K   RM	  RM! R V  4       4      '       g   R# \        R V  4       4      # )zUMeasure only the adapter-owned text-only shape, including whitespace and empty parts.c              3  óÈ   "  € T FX  p\        V\        4      ;'       d<    VP                  R 4      R8H  ;'       d     \        VP                  R4      \        4      x € KZ  	  R# 5i)rZ   r}   rf   N)r3   r^   r_   rg   ©r€   r‰   s   & r   r‚   Ú)_input_text_parts_cost.<locals>.<genexpr>Å   sZ   é € ð ?á:A°$ô 	�4œÓ÷ 	.ð 	. 4§8¡8¨FÓ#3°|Ñ#C÷ 	.ð 	.Ü�t—x‘x Ó'¬Ó-ô	.Û:Aùs   ‚A"žA"º(A"FTNc              3  óF   "  € T F  p\        VR ,          4      x € K  	  R# 5i©rf   N)rk   r�   s   & r   r‚   r‘   Ê   s   é € Ð@¹°Œ~˜d 6�l×+Ð+»ùs   ‚!)r3   r…   ÚallÚsum)rz   s   &r   Ú_input_text_parts_costr–   Ã   sU   € ä�gœt×$Ò$¯G¿3»3ñ ?á:Aó?¿3¿3º3ñ ?á:Aó?÷ <ò <ñ ÜÑ@¹Ó@Ó@Ð@r   c               ó$   € V ^8„  d   QhRRRRRR/# )r   rz   úlist[dict[str, Any]]rm   r4   r   r   )r   s   "r   r   r   Í   s#   € ÷ ñ Ð(<ð Àcð ÐNbñ r   c                óÜ   € . pV  Fc  pVR,          p\        V4      pWQ8”  d.   \        WA4      pV'       d   VP                  / VCRV/C4        V# VP                  V4       W,          pKe  	  V# )zNCopy the head of validated input_text parts without flattening their metadata.rf   )rk   rv   Úappend)rz   rm   rr   r‰   rf   ÚcostÚkepts   &&     r   Ú_truncate_input_text_partsr�   Í   su   € à€DÛˆØ�F�|ˆÜ˜dÓ#ˆØŒ=Ü& tÓ4ˆDßØ—‘Ð2˜tÐ2 V¨TÑ2Ô3Øð €Kð 	�‰�DÔØ�Šñ ð €Kr   c               ó    € V ^8„  d   QhRRRR/# ©r   rx   r   r   r   r   )r   s   "r   r   r   Ý   s   € ÷ ñ ¨ð °ñ r   c                óì   € \        V \        4      '       d   V P                  R4      MRp\        V\        4      ;'       d5    \        ;QJ d    R V 4       F  '       g   K   R# 	  R# ! R V 4       4      # )z­True for a converted Responses message with a valid ``input_image`` part (only the
adapter-owned shape counts, so empty multipart placeholders never become durable history).rz   Nc              3  ód  "  € T F¦  p\        V\        4      ;'       dŠ    \        VP                  R 4      ;'       g    R4      P	                  4       P                  4       R8H  ;'       d?    \        VP                  R4      \        4      ;'       d    VR,          P	                  4       x € K¨  	  R# 5i)rZ   r   Úinput_imageÚ	image_urlN)r3   r^   rg   r_   r   r   r�   s   & r   r‚   Ú0_has_retainable_image_content.<locals>.<genexpr>á   s�   é € ð -á]dÐUYô 	�4œÓ÷ 	Qð 	Q¤3 t§x¡x°Ó'7×'=Ð'=¸2Ó#>×#DÑ#DÓ#F×#LÑ#LÓ#NÐR_Ñ#_÷ 	Qð 	QÜ�t—x‘x Ó,¬cÓ2÷	Qð 	QØ7;¸KÕ7H×7NÑ7NÓ7Pô	QÛ]dùs   ‚B0žB0¼,B0Á)&B0Â B0TF)r3   r^   r_   r…   Úany)rx   rz   s   & r   Ú_has_retainable_image_contentr¦   Ý   sn   € ô &0°´d×%;Ò%;ˆd�h‰h�yÔ!À€GÜ�gœtÓ$÷ ð ¯«ñ -á]dó-¯¬ð ªð ¨ñ -á]dó-ó *ð r   c               ó    € V ^8„  d   QhRRRR/# rŸ   r   )r   s   "r   r   r   ì   s   € ÷ Gñ G˜cð G dñ Gr   c                óZ   € \        V \        4      ;'       d    V P                  R 4      R8H  # )rZ   r[   )r3   r^   r_   )rx   s   &r   Ú_is_compaction_itemr©   ì   s%   € Ü�dœDÓ!×FÐF d§h¡h¨vÓ&6¸,Ñ&FÐFr   Tc               ó0   € V ^8„  d   QhRRRRRRRRRR	R
R/# )r   Úitemsr˜   Úretained_user_token_budgetr4   Úretained_summary_token_budgetÚenable_summary_retentionr   Úitem_sourceszOptional[list[Any]]r   r   )r   s   "r   r   r   ð   sM   € ÷ {ñ {Øð{à #ð{ð $'ð{ð #ð	{ð :Mð	{ð
 ñ{r   c           
     ó¬  aaa€ \        V \        4      '       d	   V '       g   V # \        R \        V 4       4       RR7      pVf   V # TpV^ 8”  d*   \	        W^,
          ,          4      '       d   V^,          pK0  V RV p\        V\        4      ;'       d    \        V4      \        V 4      8H  pV'       d   VRV MR.\        V4      ,          p	. o\        ^ \        V4      4      p
\        ^ \        V4      4      o\        4       oR VVV3R llp\        \        V4      \        V	4      4       EFD  w  rÍ\        V\        4      '       g   K  V'       d…   \        V\        4      '       do   \        V4      '       d^   \        VP                  R4      4      pVP                  R4      pT! VP                  4       '       d   TMRRR	RVR9   d   TMRRV/4       Kª  RV9   d   VP                  R4      R	8w  d   KÉ  T;'       d    \        V4      pVP                  R4      R
8H  pV'       g   V'       g   EK  \        V4      pVf   V'       d   \!        V4      '       g   EK0  RpV'       d   V! Wì4       EKE  V
^ 8”  g   EKO  VP                  R4      p\#        V4      pVe   TM
\%        V4      pVV
8:  d   SP'                  V4       V
V,          p
EK   \        V\(        4      '       d'   \+        VV
4      p\-        VP                  4       4      pMGVeB   \/        VV
4      p\0        ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      pMRpV'       d   SP'                  / VCRX/C4       ^ p
EKG  	  WV^,            \        \        S4      4      ,           W^,           R ,           p\2        P5                  R\        V 4      \        V4      V
S4       V# )u`	  Restructure Responses input around the newest compaction checkpoint.

The server drops every input item preceding a replayed ``compaction`` item, erasing the
user's plaintext asks and any local-compression summary. Rebuild as::

    [checkpoint run] + [retained user & summary messages (newest-first budget)] + [post]

- The NEWEST contiguous run of checkpoints wins; relative order is preserved.
- User messages are kept verbatim within ``retained_user_token_budget``; the boundary
  message is head-truncated when it only partially fits (strings or text-only typed
  ``input_text`` parts, preserving part metadata). An oversized unsupported/mixed-content
  boundary stops older user retention rather than substituting an older ask. A recognized
  image-only user message is retained whole at one-token cost.
- Summaries are retained whole within ``retained_summary_token_budget``, never sliced
  (framing would corrupt) and never duplicated.
- ``item_sources`` (parallel to ``items``) is the raw chat message each item came from.
  Conversion can be lossy for summaries (merge-into-tail carrier â†’ typed
  ``function_call_output``; assistant carrier shadowed by a stale replay), so a source
  that is itself a canonical summary carrier is read from the SOURCE and retained as a
  synthesized ``role="assistant"`` message.
- ``enable_summary_retention`` is a test override, not a config surface.

The server drops every input item that precedes a replayed ``compaction`` item (live-verified Aug 2026),
so sending pre-checkpoint history is dead weight AND silently erases the user's plaintext asks â€”
including any local-compression summary the agent already produced, which previously vanished here
because it carries ``role="assistant"``, not ``"user"`` (#90975).
A summary is never byte/character-sliced: Hermes summaries carry structural framing (handoff prefix, end
marker, merge-into-tail delimiters) that a blind slice can corrupt, so one that doesn't fit whole is
dropped instead. A summary already retained once (identical text) is never duplicated, so repeated
checkpoints stay idempotent. - ``enable_summary_retention`` is a function-level override (used by tests
and callers that need the pre-#90975 behavior back); it is not wired to a user-facing config surface.
Without ``item_sources`` (default), retention only sees what survived conversion, matching pre-#90976
behavior (#90976).
c              3  óP   "  € T F  w  r\        V4      '       g   K  Vx € K  	  R # 5ir   )r©   )r€   Úirx   s   &  r   r‚   Ú-prune_pre_checkpoint_items.<locals>.<genexpr>  s   é € ÐRÑ$4™˜Ô8KÈD×8Q—1’1Ó$4ùs   ‚&œ
&N)Údefaultc               ó$   € V ^8„  d   QhRRRRRR/# )r   rf   r   Úretained_itemzdict[str, Any]r   rF   r   )r   s   "r   r   Ú0prune_pre_checkpoint_items.<locals>.__annotate__*  s!   € ÷ 	&ñ 	&˜mð 	&¸Nð 	&Ètñ 	&r   c                ó¶   <€ V '       d   S^ 8:  g   V S9   d   R# \        V 4      pVS8:  d.   SP                  V 4       SP                  V4       SV,          oR# R# )zURetain a summary whole when it fits the budget and is not a duplicate (never sliced).N)rk   Úaddrš   )rf   r¶   r›   Úretained_reversedÚseen_summary_textsÚsummary_remainings   && €€€r   Ú_retain_summaryÚ3prune_pre_checkpoint_items.<locals>._retain_summary*  sZ   ø€ ÷ Ð(¨AÔ-°Ð9KÔ1KÙÜ˜dÓ#ˆØÐ$Ô$Ø×"Ñ" 4Ô(Ø×$Ñ$ ]Ô3Ø Õ%Òñ %r   rz   ÚrolerZ   ÚmessageÚuserÚ	assistantr   c              3  óN   "  € T F  qR ,          P                  4       x € K  	  R# 5ir“   )r   r�   s   & r   r‚   r³   _  s   é € ÐEÁ¸ F�|×1Ñ1×3Ð3Ãùs   ‚#%TFzRPruned pre-checkpoint items: %d input -> %d retained (user_rem=%d, summary_rem=%d))rÁ   rÂ   )r3   r…   r<   Ú	enumerater©   rq   r4   ÚsetÚzipÚreversedr^   Ú_is_summary_itemr
   r_   r   rŒ   r¦   r–   rk   rš   rg   rv   r   r�   r¥   rI   Údebug)r«   r¬   r­   r®   r¯   Úlast_cpÚfirst_cpÚpreÚhas_sourcesÚpre_sourcesÚuser_remainingr½   rx   Úsourcerf   Ú	_src_roleÚ
is_summaryÚis_userrz   Ú
parts_costr›   rr   ÚkeepÚresultrº   r»   r¼   s   &&&&&                   @@@r   Úprune_pre_checkpoint_itemsr×   ð   s;  ú€ ôP �eœT×"Ò"¯%ØˆÜÑR¤I¨eÔ$4ÓRÐ\`Ôa€GØ‚ØˆØ€HØ
�QŒ,Ô.¨uÀµ\Õ/B×CÒCØ�A�Šà
�	�Ð
€CÜ˜\¬4Ó0×TÐT´S¸Ó5FÌ#ÈeË*Ñ5T€Kß8C˜\¨)¨8Ñ4È$ÈÔRUÐVYÓRZÕIZ€Kà.0ÐÜ˜œCÐ :Ó;Ó<€NÜ˜AœsÐ#@ÓAÓBÐÜ!›eÐ÷	&ò 	&ô œH S›M¬8°KÓ+@×A‰ˆÜ˜$¤×%Ò%Ù÷ $¬
°6¼4×(@Ò(@ÔEUÐV\×E]ÒE]Ü'¨¯
©
°9Ó(=Ó>ˆDØŸ
™
 6Ó*ˆIÙ D§J¡J§L¢L™D°dØ# Y°ÀYÐRgÔEg¹	ÐmxØ&¨ð.ô/ñ à�TŒ>˜dŸh™h vÓ.°)Ô;ÙØ-×HÐHÔ2BÀ4Ó2Hˆ
Ø—(‘(˜6Ó" fÑ,ˆßŸzÚÜ! $Ó'ˆØŠ<ßÔ =¸d× CÒ CÚØˆDßÙ˜D×'Ø˜a×Ø—h‘h˜yÓ)ˆGÜ/°Ó8ˆJØ!+Ò!7‘:¼^ÈDÓ=QˆDØ�~Ô%Ø!×(Ñ(¨Ô.Ø $Õ&“ä˜g¤s×+Ò+Ü.¨w¸ÓG�DÜ §
¡
£Ó-‘DØÒ+Ü5°g¸~ÓN�Dß›3ÑEÁÓEŸ3Ÿ3š3ÑEÁÓEÓE‘Dà �DßØ%×,Ñ,Ð-F°Ð-F°iÀÑ-FÔGð "#“ñc Bðf ˜g¨�kÐ*¬T´(Ð;LÓ2MÓ-NÕNÐQVÐabÕWbÐWdÐQeÕe€FÜ
‡L�LÐeÜ�U“œS ›[¨.Ð:KôMà€Mr   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Úerrorr   Ústatus_coder   r   r   )r   s   "r   r   r   u  s&   € ÷ @ñ @¨#ð @¸Cð @È4ñ @r   c                óJ  a€ \        T ;'       g    R4      P                  4       oRS9  d
   RS9  d   R#  Ve   \        V4      R8w  d   R# \
        ;QJ d#    T3R l\         4       F  '       g   K   R# 	  R# ! T3R l\         4       4      #   \        \        3 d     LUi ; i)u  True when a provider error is a STRUCTURED rejection of ``context_management``.

Drives one-shot recovery (strip, disable for the session, retry), so matching is narrow:
a transient 5xx that merely ECHOES the request must not downgrade native compaction.
Requires ``status_code`` 400 (or unknown) AND the field name with rejection language.

See #82777.
* ``status_code`` is 400 (or unknown/None â€” some transports surface only a message string; field-name
matching alone is then the best available signal, preserving pre-#82777 behavior for them), and * the
error text names ``context_management`` / ``compact_threshold`` alongside rejection language ("unknown",
"unsupported", "invalid", "unexpected", "not permitted"...). A bare field-name echo without rejection
language does not match.
r   Úcontext_managementr\   Fi�  c              3  ó,   <"  € T F	  qS9   x € K  	  R # 5ir   r   )r€   Úmarkerrf   s   & €r   r‚   Ú1is_native_compaction_rejection.<locals>.<genexpr>‹  s   øé € Ð?Ñ,> &˜Ž~Ó,>ùs   ƒT)rg   r   r4   r5   r,   r¥   Ú_REJECTION_MARKERS)rÙ   rÚ   rf   s   &&@r   Úis_native_compaction_rejectionrá   u  s‘   ø€ ô ˆu�{ˆ{˜Ó×!Ñ!Ó#€DØ 4Ô'Ð,?ÀtÔ,KÙðØÒ"¤s¨;Ó'7¸3Ô'>Ù÷ ‹3Ô?Õ,>Ó?�3Œ3Ð?Š3Ð?ˆ3Ô?Õ,>Ó?Ó?Ð?øô ”zÐ"ô Ùðús   ¶B ÂB"Â!B"c               ó    € V ^8„  d   QhRRRR/# )r   r«   r   r   r   r   )r   s   "r   r   r   Ž  s   € ÷ ñ  Sð ¨Tñ r   c                óš   € \        V \        4      ;'       d5    \        ;QJ d    R V  4       F  '       g   K   R# 	  R# ! R V  4       4      # )z¹Does this ``codex_reasoning_items`` sidecar carry a compaction checkpoint? A checkpoint is
cumulative context living in exactly one place: rewrite/discard the sidecar only after asking.c              3  óÖ   "  € T F_  p\        V4      ;'       dH    \        VP                  R 4      \        4      ;'       d!    \	        VR ,          P                  4       4      x € Ka  	  R# 5i)Úencrypted_contentN)r©   r3   r_   rg   r   r   )r€   rx   s   & r   r‚   Ú,has_compaction_checkpoint.<locals>.<genexpr>‘  sd   é € ð +ñ ˆDô 	˜DÓ!÷ 	4ð 	4Ü�t—x‘xÐ 3Ó4´cÓ:÷	4ð 	4ä�Ð)Õ*×0Ñ0Ó2Ó3ô	4ó ùs   ‚A)™&A)Á )A)TF)r3   r…   r¥   )r«   s   &r   Úhas_compaction_checkpointrç   Ž  sV   € ô �eœTÓ"÷ ð §s£sñ +ñ ó	+§s¤sð ¢sð  sñ +ñ ó	+ó (ð r   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Úprior_itemsr   Ú	new_itemsr   r˜   r   )r   s   "r   r   r   ™  s"   € ÷ 'ñ '¨sð '¸sð 'ÐG[ñ 'r   c                ó  € \        V \        4      '       d   T M. pV Uu. uF  p\        V4      '       g   K  VNK  	  pp\        V\        4      '       d   \        V4      M. p\        V4      '       g	   V'       g   V# WE,           # u upi )a  Merge ``codex_reasoning_items`` across Codex incomplete-continuation dedup.

A checkpoint on the EARLIER response is not re-emitted by the continuation, so a blind
overwrite drops the only copy: newer items win, prior checkpoints are prepended unless
the newer payload has its own.
)r3   r…   r©   rç   )ré   rê   Úpriorrx   Úkept_checkpointsÚnew_lists   &&    r   Úmerge_interim_reasoning_itemsrï   ™  so   € ô & k´4×8Ò8‰K¸b€EÙ).ÓL© Ô2EÀd×2KŸ˜©ÐÐLÜ",¨Y¼×"=Ò"=Œt�IŒÀ2€HÜ  ×*Ò*×2BØˆØÕ&Ð&ùò	 Ms
   ŸB·Br   )ÚunknownÚunsupportedÚinvalidÚ
unexpectedznot permittedznot allowedÚunrecognizedzextra fieldzno suchzbad requestznot supported)1Ú__doc__Ú
__future__r   ÚloggingÚtypingr   r   r   r   Úurllib.parser   Úagent.codex_headersr   Úagent.context_compressorr	   Úagent.message_contentr
   Úagent.reasoning_effortr   Ú	getLoggerÚ__name__rI   r=   r?   r   r   r(   r&   r   r7   rD   rH   rK   rd   Ú"RETAINED_USER_MESSAGE_TOKEN_BUDGETÚRETAINED_SUMMARY_TOKEN_BUDGETrk   rv   rŒ   r–   r�   r¦   rÈ   r©   r×   rà   rá   rç   rï   r   r   r   Ú<module>r     s&  ðñõ #ã ß ,Ó ,Ý !å :Ý BÝ 6Ý 1à	×	Ò	˜8Ó	$€ð $Ð à#Ð à"Ð ð	Ø7;ð	ØW[÷	ð`ØPTð`Øot÷`ð(ÐPU÷ (ð*°$°÷ *÷.ð$ "'Ð õ	
ðDÐinð DØEJ÷DðH &,Ð "Ø &Ð õ/õõ"*õ.Aõõ ð 1Ð õGð 'IØ)FØ%)Èt÷	{ð|Ð ÷@õ2÷'r   