+
    jHàj54  ã                   ó°   € R t ^ RIt^ RIt^ RIt^ RIHtHtHtHt ]P                  ! ]
4      tR tR tR tR tR tRtR	tRR
 ltR tR tR tRtR tR tR# )zMFence an unsettled claim in a live owner without inventing a terminal result.N)ÚRuntimeStoreErrorÚ
_admissionÚ_epochÚsettle_steer_transfersc                 ó¨   a aa€ \        S S P                  P                  SR ,          4      S4      oV VV3R lpS P                  P	                  V4      # )Útarget_session_idc                 óÔ  <€ \        V SP                  4       \        V SR ,          4      pVR,          SP                  8w  g   VR,          SR,          8w  d   \        R4      hVR,          R9   d
   VR,          # VR,          R8w  d   \        R4      hV P	                  RSR ,          34       \        V SP                  SR,          SRR7       V P	                  R	SR
,          34       R# )Úadmission_idÚowner_epochÚ
generationÚstale_generationÚstatusÚunknownÚstartedzCUPDATE session_admissions SET status='unknown' WHERE admission_id=?)Úepochr   Ú	transfersÚoutcomezBUPDATE sessions SET runtime_revision=runtime_revision+1 WHERE id=?r   >   r   Úterminal)r   r   r   r   Úexecuter   )ÚconnÚcurrentÚ	authorityÚrowr   s   & €€€ÚH/home/ubuntu/.hermes/hermes-agent/gateway/session_settlement_recovery.pyÚwriteÚ_mark_unknown.<locals>.write   sÖ   ø€ Üˆt�Y—_‘_Ô%Ü˜T 3 ~Õ#6Ó7ˆØ�=Õ! Y§_¡_Ô4¸ÀÕ8MÐQTÐUaÕQbÔ8bÜ#Ð$6Ó7Ð7Ø�8ÕÐ 7Ô7Ø˜8Õ$Ð$Ø�8Õ 	Ô)Ü#Ð$6Ó7Ð7Ø�‰ÐZÐ]`ÐaoÕ]pÐ\rÔsä˜t¨9¯?©?ÀsÈ<ÕGXØ)2¸Iõ	Gà�‰ÐYÐ\_Ð`sÕ\tÐ[vÔwÙó    )Ú
_transfersÚsessionsÚgetÚdbÚ_execute_write)r   r   r   r   s   ff @r   Ú_mark_unknownr"      sE   ú€ Ü˜9 i×&8Ñ&8×&<Ñ&<¸SÐATÕ=UÓ&VÐX[Ó\€I÷ð �<‰<×&Ñ& uÓ-Ð-r   c                óÂ   € Vf   R# ^ RI Hp VP                  P                  ;_uu_ 4        V! WR,          VR,          4      uuRRR4       #   + '       g   i     R# ; i)z\The steers handed to this claim (``prompt.steer_queued``), read once its execution returned.N)Ústeer_transfersr   r   )Úgateway.session_queued_controlsr$   Úevent_streamÚlock)r   Úliver   r$   s   &&& r   r   r       sC   € à‚|ÙÝ?Ø	×	Ñ	×	×	Ó	Ù˜yÐ.AÕ*BÀCÈÕDUÓV÷ 
 ×	×	Ó	ús   «AÁA	c              ƒ   ó   "  €   ^ RI Hp V! V \        P                  ! \        W4      4      p\        P
                  ! V4      G Rj  x€L
 #  L  \        \        P                  3 d$    \        P                  ! ^4      G Rj  x€L 
   K‡  i ; i5i)T©Útrack_mutationN)
Úgateway.session_runtime_workersr+   ÚasyncioÚ	to_threadr"   ÚshieldÚOSErrorÚsqlite3ÚErrorÚsleep)r   r   r+   Úwriters   &&  r   Úrecover_failed_settlementr5   )   sj   é € ð ð	#ÝFÙ# I¬w×/@Ò/@ÄÐPYÓ/_Ó`ˆFÜ Ÿš¨Ó/×/Ð/Ñ/øÜœŸ™Ð'ô 	#Ü—-’- Ó"×"Ô"ð	#üsG   ‚B…AA ÁAÁA Á
BÁA Á4BÂBÂBÂBÂ
BÂBc                 ó.  € R TRTRV R,          RVR,          R8X  d   RMVR,          RRRRR/P                  VR,          R	4      /pT;'       g    / P                  R
4      ;'       g    / pVP                  R4      p\        V\        4      '       gd   VR,          R	8X  dV   ^ RIHp T! T\        VP                  R4      ;'       g    R4      \        VP                  R4      ;'       g    R4      R7      p\        V\        4      '       d   WdR&   V'       d   VP                  R4      '       d   RVR&   VP                  R4      '       EdF   VP                  R4      '       d   RVR&   VP                  R4      '       d   RVR&   \        VP                  R4      \
        4      '       d   VR,          '       d   VR,          VR&   \        VP                  R4      \        4      '       d   VR,          VR&   \        VP                  R4      \
        4      '       d   VR,          '       d   VR,          VR&   \        VP                  R4      \
        4      '       d8   VR,          P                  4       '       d   VR,          P                  4       VR&   \        T;'       g    / P                  R4      \        4      '       d   ^ RIHp V! VR,          4      VR&   \        VP                  R4      \        4      '       d   / VR,          CR V R!,          /CVR&   V# )"ÚtextÚcontentr	   r   ÚinterruptedÚ	cancelledr   Ú	completedÚcompleteÚerrorÚresultÚerror_surface)Úbuild_error_surface_from_resultÚproviderÚ Úmodel)rA   rC   Úresponse_reusedTÚlocal_viewerÚresponse_previewedÚresponse_transformedÚwarningÚbilling_blockÚbillingÚfailure_reasonÚlast_reasoningÚ	reasoningÚsession_usage)Ú	tui_usageÚusageÚpersisted_turnÚsubmission_idÚ
request_id)	r   Ú
isinstanceÚdictÚagent.error_surfacer@   ÚstrÚstripÚgateway.session_usagerO   )	r   ÚsettledÚresponseÚcapturedr<   Úcaptured_resultÚsurfacer@   rO   s	   &&&&     r   Úcompletion_payloadr_   5   s¾  € ð 	�˜) X¨~¸sÀ>Õ?RØ '¨)Õ"4¸Ô"E‘;È7ÐS\ÕK]Ø�; 
¨M¸=ÐI×MÑMØ�IÕ ó)ð*€Hð  —~�~ 2×*Ñ*¨8Ó4×:Ð:¸€Oð ×!Ñ! /Ó2€GÜ�gœt×$Ò$¨°(Õ);¸wÔ)FÝGÙ1Ø¤c¨/×*=Ñ*=¸jÓ*I×*OÐ*OÈRÓ&PÜ�o×)Ñ)¨'Ó2×8Ð8°bÓ9ô;ˆô �'œ4× Ò Ø$+�Ñ!ß�O×'Ñ'Ð(9×:Ò:Ø&*ˆÐ"Ñ#Ø×Ñ˜>×*Ó*Ø×ÑÐ3×4Ò4Ø-1ˆHÐ)Ñ*Ø×ÑÐ5×6Ò6Ø/3ˆHÐ+Ñ,Ü�o×)Ñ)¨)Ó4´c×:Ò:¸Èy×?YÔ?YØ"1°)Õ"<ˆH�YÑÜ�o×)Ñ)¨/Ó:¼D×AÒAØ"1°/Õ"BˆH�YÑÜ�o×)Ñ)Ð*:Ó;¼S×AÒAÀoÐVf×FgÔFgØ)8Ð9IÕ)JˆHÐ%Ñ&Ü�o×)Ñ)Ð*:Ó;¼S×AÒAÀoÐVfÕFg×FmÑFm×FoÒFoØ$3Ð4DÕ$E×$KÑ$KÓ$MˆH�[Ñ!ô �8—>�>˜r×&Ñ& Ó7¼×>Ò>Ý3Ù% h¨Õ&?Ó@ˆ�ÑÜ�/×%Ñ%Ð&6Ó7¼×>Ò>ð&J¨Ð8HÕ(Ið &JØ&5°s¸<Õ7Hñ&JˆÐ!Ñ"à€Or   c                ó€  €  VP                   P                  VP                  R4       \        W4WV4      p\        WdR,          V4      pV'       d   VP                  R4      '       g   W‡R&   VP                  P                  VP                  V4       R#   \         d!    \        P                  RTR,          4        L”i ; i)z­Thread-safe half of terminal publication; the caller holds the stream lock. No SQLite write
happens here: loop-side readers wait on this lock, so it never spans storage I/O.Nz0Terminal control cleanup failed for admission %sr	   r   rH   )ÚcontrolsÚsnapshotÚ
session_idÚ	ExceptionÚloggerÚ	exceptionr_   Úowner_stop_noticer   r&   Úpublish)	r   r(   Úrefr   rZ   r[   r\   r<   Únotices	   &&&&&&&  r   Ú_publish_completionrk   h   sž   € ðbØ�‰×Ñ˜sŸ~™~¨tÔ4ô " #°ÓC€HÜ˜x°Õ);¸SÓA€Fß�h—l‘l 9×-Ò-ð %�ÑØ×Ñ×Ñ˜cŸn™n¨hÖ7øô ô bÜ×ÑÐKÈSÐQ_ÕM`Öaðbús   ‚&B Â(B=Â<B=zxGateway restarting: this turn was interrupted and will not be run again. Send your message again once Hermes reconnects.zxGateway shutting down: this turn was interrupted and will not be run again. Send your message again once Hermes is back.c                ó  € ^ RI HpHp VR8X  d5   T ;'       g    / P                  R4      ;'       g    / P                  R4      MRpV\        V\
        /P                  V4      pVe!   Ve   ^ RIHp V! V4      '       d	   ^ RIH	p V# V# )a½  The named notice for a turn the owner's restart/stop interrupted, or None. With the default
``agent.restart_drain_timeout: 0`` a restart interrupts in-flight turns at once; the admission
settles ``interrupted`` and is never replayed, so the viewer is told the restart did it.
An unattended ``row`` (cron, automation, webhook) has nobody to resend it, so it gets the
neutral automation copy instead of resend advice (#134889 GW-20, GW-10's rule).)Ú!_INTERRUPT_REASON_GATEWAY_RESTARTÚ"_INTERRUPT_REASON_GATEWAY_SHUTDOWNr9   r>   Úinterrupt_messageN)Úis_unattended)ÚAUTOMATION_INTERRUPTED_NOTICE)
Úgateway.runrm   rn   r   ÚRESTART_INTERRUPT_NOTICEÚSHUTDOWN_INTERRUPT_NOTICEÚ"gateway.session_unattended_unknownrp   Úagent.turn_failure_copyrq   )	r\   r   r   rm   rn   Úcauserj   rp   rq   s	   &&&      r   rg   rg   ~   s„   € ÷ bØOVÐZgÔOgˆh�nˆn˜"×!Ñ! (Ó+×1Ð1¨r×6Ñ6Ð7JÔKÐmq€EØ/Ô1IØ0Ô2KðMßMPÉSÐQVËZð àÒ˜cšoÝDÙ˜×ÒÝMØ0Ð0Ø€Mr   c                ó*  € T ;'       g    / P                  R4      ;'       g    / pVP                  R4      VP                  R4      rCVR8w  g5   \        V\        4      '       d   \        V\        4      '       d	   V'       g   R# VRVR\        V4      /3# )z¾``(notice, display_metadata)`` for a failed turn the gateway itself refused with a renderable
descriptor (``session_cron.execute``: the only producer of ``result.error_surface``), else None.r>   r?   r=   ÚfailedN)r   rT   rU   rW   )r\   r   Úvaluer^   r=   s   &&   r   Úgateway_refusal_boundaryr{   “   sz   € ð �^ˆ^˜× Ñ  Ó*×0Ð0¨b€EØ—Y‘Y˜Ó/°·±¸7Ó1CˆUØ�(Ô¤*¨W´d×";Ò";Ä:ÈeÔUX×CYÒCY×afÙØ�7˜E ?´D¸³MÐBÐBÐBr   c                ó¦   €  ^ RI Hp V! V P                  VR,          4       R#   \         d"    \        P                  RTR,          4        R# i ; i)zYTerminal media cleanup: its own write txn after publication, never under the stream lock.)Úrelease_admission_mediar	   z.Terminal media cleanup failed for admission %sN)Úgateway.session_ingress_mediar}   r    rd   re   rf   )r   r   r}   s   && r   Ú_release_mediar   �   sE   € ð`ÝIÙ 	§¡¨c°.Õ.AÖBøÜô `Ü×ÑÐIÈ3È~ÕK^×_ð`ús   ‚ $ ¤(AÁAc                ó˜  € ^ RI HpHp	 V	! W4WV4      p
VR,          pVP                  P                  ;_uu_ 4        W1P
                  V&   RRR4       \        WV4      p V! V P                  V P                  W4VWjVR7      w  VR&   VR&   VP                  P                  ;_uu_ 4        VP
                  P                  VR4       \        WW#VR,          VR,          V4       RRR4       VP                  P                  ;_uu_ 4        VP
                  P                  VR4       RRR4       \        Y4       R#   + '       g   i     Lø; i  + '       g   i     Ls; i  + '       g   i     LC; i  TP                  P                  ;_uu_ 4        TP
                  P                  TR4       RRR4       i   + '       g   i     i ; i; i)aî  Worker-thread settlement: redaction, encoding and the SQLite write never stall the owner loop.

The write runs OUTSIDE ``event_stream.lock``: attach, replay, receipts and pending publication
take that lock on the loop, and a writer held by another process (up to ``_WRITE_PATIENCE_S``)
would stall every session behind it. Ordering is kept by ``live.settling``: from before the
write until the completion frame is published, lock holders read this claim as still started,
so none sees the terminal row without its completion. ``settlement`` receives the committed
``(settled, response)`` before publication, so a publish failure is not a lost commit.
The caller publishes idle ``session.info`` on the loop afterwards (bot receipt wakeups are tasks).
)Úfinish_resultÚprepare_resultr	   N)r   r   r[   r   r>   Úpreparedr   rZ   r[   )Úgateway.session_resultsr�   r‚   r&   r'   Úsettlingr   r    r   Úpoprk   r   )r   r(   ri   r   r[   r   r\   Ú
settlementr�   r‚   rƒ   r	   r   s   &&&&&&&&     r   Úcommit_and_publishrˆ   ¦   sS  € ÷ Fá˜c¨WÓ?€HØ�~Õ&€Lð 
×	Ñ	×	×	Ó	Ø&)�‰�lÑ#÷ 
 ä˜9¨CÓ0€Ið	2Ù8EØ�L‰L 	§¡°SÐU\Ø¸)ô9EÑ5ˆ
�9Ñ˜z¨*Ñ5ð ×Ñ×#×#Ó#Ø�M‰M×Ñ˜l¨DÔ1Ü 	°¸:ÀiÕ;PÐR\Ð]gÕRhÐjrÔs÷ $ð ×Ñ×#×#Ó#Ø�M‰M×Ñ˜l¨DÔ1÷ $ä�9Ö"÷ 
 ×	ú÷ $×#ú÷ $×#ûˆT×Ñ×#×#Ó#Ø�M‰M×Ñ˜l¨DÔ1÷ $×#×#Ð#ýsT   ¹D8ÁA	E1 Â&:EÃ E1 ÄEÄ8E	ÅE	ÅE1 ÅE.	Å1G	ÆF6	Æ-	G	Æ6GÇG	c              ƒ  ó¨  "  € ^ RI Hp . \        ORN FE  p	 \        P                  ! V! V \        P
                  ! \        WW#WEWg4	      4      4      G Rj  x€L
 u # 	  R#  L
  \        \        P                  3 dU   p
T	e   RT9   d   h \        P                  RTR,          Y©4       \        P                  ! T	4      G Rj  x€L 
   Rp
?
K·  Rp
?
ii ; i5i)aÇ  Commit the captured result, retrying a transient storage failure a bounded number of times.

One locked or full write must not turn a finished answer into an ``unknown`` turn whose result
lives only in memory. Retrying is exact: a write that committed before reporting failure is
read back by ``finish_result``, and a failure after the commit (``settled`` set) never retries.
A fence refusal (``RuntimeStoreError``) is not transient and is raised at once.r*   NrZ   zFSettlement of admission %s deferred by storage (%r); retrying in %.1fsr	   )r,   r+   Ú_SETTLE_RETRY_DELAYS_Sr-   r/   r.   rˆ   r0   r1   r2   re   rH   r3   )r   r(   ri   r   r[   r   r\   r‡   r+   ÚdelayÚexcs   &&&&&&&&   r   Úsettle_with_retryr�   Ì   sº   é € õ ?Ø0Ô)Ð0¨4Ó0ˆð	'Ü Ÿš©°yÄ'×BSÒBSÜ" I°S¸xÐRZóChó )ió j÷ jò jó 1ñjøäœŸ™Ð'ô 	'ØŠ} 	¨ZÔ 7ØÜ�N‰NÐcØ˜~Õ.°ô<ä—-’- Ó&×&×&ûð	'üsM   ‚C—<A ÁAÁA ÁCÁA Á CÁ;AC
Â>CÂ?C
ÃCÃ
CÃCc                ó  € V P                   VP                  ,          pVP                  P                  ;_uu_ 4        \	        WWW4V4       RRR4       \        W4       V P                  V4       R#   + '       g   i     L.; i)zKCleanup cannot withhold a committed outcome; publication is attempted once.N)r   rc   r&   r'   rk   r   Ú_publish_pending)r   ri   r   rZ   r[   r\   r(   s   &&&&&& r   Úpublish_terminalr�   à   sZ   € à×Ñ˜cŸn™nÕ-€DØ	×	Ñ	×	×	Ó	Ü˜I¨S°wÈ(ÔS÷ 
 ä�9Ô"à×Ñ˜sÖ#÷	 
 ×	ús   ¼A1Á1B	)N)g      à?g       @)Ú__doc__r-   Úloggingr1   Úhermes_state_runtimer   r   r   r   Ú	getLoggerÚ__name__re   r"   r   r5   r_   rk   rs   rt   rg   r{   r   rˆ   rŠ   r�   r�   © r   r   Ú<module>r—      s}   ðÙ SÛ Û Û ç ^Ó ^à	×	Ò	˜8Ó	$€ò.ò*Wò	#ò0òf8ð NÐ ðLÐ ôò*Cò`ò#ðF $Ð ò'ô($r   