+
    lHàj{& ã                  ó\  a € 0 t $ R t^ RIHt ^ RIt^ RIt^ RIt^ RIt^ RIt^ RI	t	^ RI
t
^ RIt^ RIHt ^ RIHtHtHtHtHt ^ RIHtHt ^ RIHt ^ RIHt ^ RIHtHt ^ R	IHtH t  ]PB                  ! ]"4      t#Rs$R
]%R&   ]	PL                  ! 4       t'^ s(R]%R&   ]	PL                  ! 4       t)/ t*R]%R&   / t+R]%R&   / t,R]%R&   Rµt-^t.^2t/R¶t0Rt1^t2R·t3Rt4]	PL                  ! 4       t5Rt6Rt7]	PL                  ! 4       t8]9! 4       t:R]%R&   ]9! 4       t;R]%R&   / t<R]%R&   Rt=Rt>R t?R!t@]	PL                  ! 4       tARsBR"]%R#&   ]	P†                  ! 4       tD0 R¸mtER¹tFRºtGR»tHR¼tIR$ tJR% R& ltKR' R( ltLR) tMR* R+ ltNR, R- ltOR. R/ ltPR0 R1 ltQR2 R3 ltRR4 R5 ltSR6 R7 ltTR8 R9 ltUR: R; ltVR<R=/R> R? lltWR@R/RA RB lltXR@R/RC RD lltYRE RF ltZRG RH lt[RI RJ lt\RK RL lt]RM RN lt^RO RP lt_RQ RR lt`RS RT ltaRU RV ltbRW RX ltcRY RZ ltdR[ R\ lteR] R^ ltfR_ R` ltgRa Rb lth]i! 0 R½m4      tjR¾tkRc Rd ltlR¿Rf^R@R/Rg Rh llltmRi Rj ltnRk Rl ltoRm Rn ltpRo Rp ltqRÀRq Rr lltrRs Rt ltsRu Rv lttRw Rx ltuRy Rz ltvR{ R| ltwR} R~ ltxR R€ ltyR�RR‚RRƒR/R„ R… lltzRRR†ReR‡ReRˆRR‰].RŠR/R‹ RŒ llt{RRR†ReR‡ReRˆRR‰].R�RRŠRR�RR‚RRƒR/
RŽ R� ll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‚Rœ R� ltƒRž RŸ lt„R  R¡ lt…R¢ R£ lt†R¤ R¥ lt‡R¦ R§ ltˆRÁR¨ R© llt‰RÂRª R« lltŠRReR¬RÃ/R­ R® llt‹R¯ R° ltŒR± R² lt�R³ R´ ltŽR# )Äa   Async (background) delegation registry behind ``delegate_task(background=true)``.

The parent dispatches a subagent on a module-level daemon executor and returns a handle
immediately. On completion a ``type="async_delegation"`` event (self-contained task-source
block) is pushed onto the SHARED ``process_registry.completion_queue`` the CLI/gateway drain
while idle, so results surface as a NEW turn (never mid-turn) and inherit its de-dup and
crash-recovery wiring. Only the async lifecycle lives here; the child run is an injected ``runner``.)ÚannotationsN)ÚThreadPoolExecutor)ÚAnyÚCallableÚDictÚListÚOptional)Úget_hermes_homeÚhermes_home_key)ÚDaemonThreadPoolExecutor)Úpropagate_context_to_thread)Úworker_ledgerÚrequire_ledger_owner)Úredact_child_resultÚredact_completion_eventzOptional[ThreadPoolExecutor]Ú	_executorÚintÚ_executor_max_workerszdict[str, dict[str, Any]]Ú_recordszdict[Any, frozenset]Ú_refusedzdict[tuple, set]Ú_refused_byÚsession_keyÚparent_session_idiè  g     Àr@g      >@g      N@ÚsetÚ_offeredÚ_startup_replayedzdict[str, float]Ú_last_orphan_sweepg      |@g     À’@g      ^@zOptional[threading.Thread]Ú_monitor_threadc                 ó$   € \        4       R ,          # )zstate.db)r	   © ó    Ú;/home/ubuntu/.hermes/hermes-agent/tools/async_delegation.pyÚ_db_pathr"   k   s   € ÜÓ˜zÕ)Ð)r    c               ó   € V ^8„  d   QhRR/# )é   Úreturnúsqlite3.Connectionr   )Úformats   "r!   Ú__annotate__r(   o   s   € ÷ ñ Ð$ñ r    c            	     óä   € ^ RI Hp Hp V! 4       '       d	   V ! R4      h^ RIHp ^ RIHp ^ RIHp \        4       pV! VP                  4       V! VRR7       V! VRR	R
R\        R7      pV! V4       V# )é    )ÚWorkerPersistenceErrorÚis_worker_processÚ$worker_delegation_ledger_unavailable)Úopen_db)Úmkdir_under_hermes_home)Ú_secure_state_db_filesT)Úcreate_mainzstate.db (async_delegation)i'  FN)Údb_labelÚbusy_timeout_msÚwalÚrow_factoryÚ
initialize)Úagent.runtime_session_storer+   r,   Úhermes_cli.sqlite_utilr.   Úhermes_constantsr/   Úhermes_stater0   r"   ÚparentÚ_initialize_schema)r+   r,   r.   r/   r0   ÚpathÚconns          r!   Ú_connectr?   o   sj   € ßUÙ×ÒÙ$Ð%KÓLÐLÝ.õ 9Ý3ä‹:€Dá˜DŸK™KÔ(Ù˜4¨TÕ2á�4Ð"?ÐQWØ¨$Ô;MôO€Dá˜4Ô Ø€Kr    c               ó    € V ^8„  d   QhRRRR/# )r$   r>   r&   r%   ÚNoner   )r'   s   "r!   r(   r(   …   s   € ÷ !ñ !Ð/ð !°Dñ !r    c                ó>   € ^ RI Hp ^ RIHp V! V 4       V! V 4       R# )r*   )Úapply_durability_barriers)Úreconcile_state_schemaN)Úhermes_state_repairrC   Úhermes_state_schemarD   )r>   rC   rD   s   &  r!   r<   r<   …   s   € Ý=Ý:ñ ˜dÔ#ñ ˜4Ö r    c                 ó.   € ^ RI Hp  V ! \        4       4      # )r*   ©Útransaction)r8   rI   r?   rH   s    r!   Ú_transactionrJ   •   s   € Ý2á”x“zÓ"Ð"r    c               ó   € V ^8„  d   QhRR/# ©r$   r%   údict[str, Any]r   )r'   s   "r!   r(   r(   ›   s   € ÷ ñ  ñ r    c                 ó²   €  ^ RI Hp  \         Uu/ uF(  q! RVP                  4        2R4      ;p'       g   K&  WbK*  	  up# u upi   \         d    / u # i ; i)zÄSnapshot scope_id/user_id/user_name on the PARENT thread (the daemon worker
has no contextvars) so a restart-replayed completion can rebuild a SessionSource.
Best-effort: empty values are omitted.©Úget_session_envÚHERMES_SESSION_Ú )Úgateway.session_contextrP   Ú_ROUTING_KEYSÚupperÚ	Exception)rP   ÚkÚvs      r!   Ú_capture_routing_originrY   ›   s^   € ðÝ;Ý+Ói™m˜°_ÀÐWX×W^ÑW^ÓW`ÐVaÐEbÐdfÓ5gÐ0g°Ó0g”�’™mÑiÐiùÒiøÜô ØŠ	ðús&   ‚A �#A¸A¾A ÁA ÁAÁAc               ó    € V ^8„  d   QhRRRR/# )r$   ÚrecordrM   r%   rA   r   )r'   s   "r!   r(   r(   ¦   s   € ÷ ñ ˜nð °ñ r    c                ó„  € \        4       ;p'       d   VP                  V 4      # \        P                  ! 4       p ^ RIHp V! \
        P                  ! 4       4      p. RO\        O Uu/ uF  pWP9   g   K  WPP                  V4      bK  	  pp \
        P                  ! 4       VR&   \        ;_uu_ 4        \        4       ;_uu_ 4       pVP                  RV R,          V P                  RR4      V P                  RR4      V P                  R	4      V R
,          V\
        P                  ! 4       V\        P                   ! V4      V P                  RR4      3
4       RRR4       RRR4       \#        4        R#   \         d    Rp EL'i ; iu upi   \         d     Lôi ; i  + '       g   i     LN; i  + '       g   i     LY; i)r*   )Úget_process_start_timeNÚ	owner_cwda~  INSERT OR REPLACE INTO async_delegations
               (delegation_id, origin_session, origin_ui_session_id,
                parent_session_id, state, dispatched_at, updated_at,
                delivery_state, delivery_attempts, owner_pid,
                owner_started_at, task_json, origin_session_id)
               VALUES (?, ?, ?, ?, 'running', ?, ?, 'pending', 0, ?, ?, ?, ?)Údelegation_idr   rR   Úorigin_ui_session_idr   Údispatched_atÚorigin_session_id)	ÚgoalÚgoalsÚcontextÚtoolsetsÚroleÚmodelÚis_batchÚtask_indexesÚtask_transcripts)r   ÚdispatchÚtimeÚgateway.statusr]   ÚosÚgetpidrV   rT   ÚgetÚgetcwdÚOSErrorÚ_DB_LOCKrJ   ÚexecuteÚjsonÚdumpsÚ_prune_durable_records)r[   ÚremoteÚnowr]   Úowner_started_atÚkeyÚtask_payloadr>   s   &       r!   Ú_persist_dispatchr~   ¦   sq  € Ü“Ð €vÖ Ø�‰˜vÓ&Ð&Ü
�)Š)‹+€Cð Ý9Ù1´"·)²)³+Ó>Ðò
 Mô  Lñ  Móñ MˆCØ‰=ô 	ˆ�Z‰Z˜‹_Òñ Mð ð ðÜ$&§I¢I£Kˆ�[Ñ!÷ 
Ž”<—>”> TØ�‰ð Qð �OÕ$ f§j¡j°ÀÓ&CÀVÇZÁZÐPfÐhjÓEkØ�Z‰ZÐ+Ó,¨f°_Õ.EÀsÌBÏIÊIËKÐYiÜ�ZŠZ˜Ó% v§z¡zÐ2EÀrÓ'JðLô	M÷ "�ô Öøô) ô  ØÓð üòøô ô Ùðúç!—>ú��úsT   º!E2 Á'FÁ4FÂF Â2F/ÃBF	ÅF/Å2FÆFÆFÆFÆF,Æ'F/Æ/F?	c               ó   € V ^8„  d   QhRR/# ©r$   r%   rA   r   )r'   s   "r!   r(   r(   Ä   s   € ÷ Bñ B ñ Br    c            	     ón  € \        4        \        P                  ! 4       \        ,
          p \        ;_uu_ 4        \	        4       ;_uu_ 4       pVP                  RV 34       VP                  R4      P                  4       ^ ,          pV\        8”  d   VP                  RV\        ,
          34       VP                  R4      P                  4       ^ ,          pV\        8”  d   VP                  RV\        ,
          34       RRR4       RRR4       R#   + '       g   i     L; i  + '       g   i     R# ; i)zBBound terminal history, preferring delivered records for deletion.zQDELETE FROM async_delegations WHERE delivery_state='delivered' AND updated_at < ?zRSELECT COUNT(*) FROM async_delegations WHERE state NOT IN ('running','finalizing')aX  DELETE FROM async_delegations WHERE delegation_id IN (
                     SELECT delegation_id FROM async_delegations
                     WHERE state NOT IN ('running','finalizing')
                     ORDER BY CASE delivery_state WHEN 'delivered' THEN 0 ELSE 1 END,
                              updated_at ASC LIMIT ?
                   )z~SELECT COUNT(*) FROM async_delegations
               WHERE state NOT IN ('running','finalizing') AND delivery_state='pending'a  DELETE FROM async_delegations WHERE delegation_id IN (
                     SELECT delegation_id FROM async_delegations
                     WHERE state NOT IN ('running','finalizing') AND delivery_state='pending'
                     ORDER BY updated_at ASC LIMIT ?
                   )N)	r   rm   Ú_DURABLE_RETENTION_SECONDSrt   rJ   ru   ÚfetchoneÚ_MAX_RETAINED_COMPLETEDÚ_MAX_DURABLE_PENDING)Úcutoffr>   Úterminal_countÚpending_counts       r!   rx   rx   Ä   sð   € äÔÜ�YŠY‹[Ô5Õ5€Fß	Ž”<—>”> TØ�‰Ø_ÐbhÐajô	làŸ™Ø`óbßbjÑbjÓblÐmnõpˆàÔ3Ô3Ø�L‰Lð ð
 )Ô+BÕBÐDôFð Ÿ™ð &[ó \ß\dÑ\dÓ\fÐghõjˆàÔ/Ô/Ø�L‰Lð ð (Ô*>Õ>Ð@ô	B÷ "�‰—>—>ú��ˆús$   ¹D#ÁB2D	Ã>D#ÄD ÄD#Ä#D4	c               ó$   € V ^8„  d   QhRRRRRR/# )r$   ÚeventrM   Úresultr%   rA   r   )r'   s   "r!   r(   r(   Þ   s&   € ÷ 
Mñ 
M˜~ð 
M°~ð 
MÈ$ñ 
Mr    c                ó   € \        V 4      \        V4      r\        4       ;p'       d   VP                  W4      # \        P                  ! 4       p\
        ;_uu_ 4        \        4       ;_uu_ 4       pVP                  R V P                  RR4      V P                  RV4      V\        P                  ! V 4      \        P                  ! V4      V R,          34       RRR4       RRR4       R#   + '       g   i     L; i  + '       g   i     R# ; i)z­UPDATE async_delegations SET state=?, completed_at=?, updated_at=?,
               event_json=?, result_json=?, delivery_state='pending'
               WHERE delegation_id=?ÚstatusÚ	completedÚcompleted_atr_   N)r   r   r   Úcompleterm   rt   rJ   ru   rq   rv   rw   )rŠ   r‹   ry   rz   r>   s   &&   r!   Ú_persist_completionr‘   Þ   s²   € Ü+¨EÓ2Ô4GÈÓ4Oˆ6Ü“Ð €vÖ Ø�‰˜uÓ-Ð-Ü
�)Š)‹+€Cß	Ž”<—>”> TØ�‰ð (ð �Y‰Y�x Ó-¨u¯y©y¸ÈÓ/MÈsÜ�ZŠZ˜Ó¤§
¢
¨6Ó 2°E¸/Õ4JðLô	M÷ "�‰—>—>ú��ˆús%   ÁC<Á/A(C)	ÃC<Ã)C9Ã4C<Ã<D	c               ó$   € V ^8„  d   QhRRRRRR/# )r$   r_   ÚstrÚentryrM   r%   rA   r   )r'   s   "r!   r(   r(   ë   s&   € ÷ Iñ I Sð I°ð IÀDñ Ir    c                ó¸  € \        V4      p\        4       ;p'       d   VP                  W4      #  \        ;_uu_ 4        \	        4       ;_uu_ 4       pVP                  RV 34      P                  4       pVf    RRR4       RRR4       R# \        \        P                  ! V^ ,          ;'       g    R4      ;'       g    / 4      pVP                  R4      ;'       g    .  Uu. uF+  qfP                  R4      VP                  R4      8w  g   K)  VNK-  	  ppVP                  V4       VP                  R\        P                  ! RVRR/4      \        P                  ! 4       V 34       RRR4       RRR4       R# u upi   + '       g   i     L; i  + '       g   i     R# ; i  \         d,    \        P                  R	YP                  R4      RR
7        R# i ; i)an  Durably record ONE finished child of a still-running multi-child unit on the unit's own row, so a crash before
the unit joins loses only the children that had not finished. Stored in ``result_json`` (overwritten by the real
result at finalize); ``recover_abandoned_delegations`` replays it. Best-effort: a failed write costs recovery
fidelity, never the live result.zSSELECT result_json FROM async_delegations WHERE delegation_id=? AND state='running'Nú{}ÚresultsÚ
task_indexzbUPDATE async_delegations SET result_json=?, updated_at=? WHERE delegation_id=? AND state='running'ÚpartialTz7Async delegation %s: could not record finished child %s©Úexc_info)r   r   Úchildrt   rJ   ru   rƒ   rv   Úloadsrq   Úappendrw   rm   rV   ÚloggerÚwarning)r_   r”   ry   r>   Úrowr™   Úrr—   s   &&      r!   Úrecord_unit_childr£   ë   st  € ô
   Ó&€EÜ“Ð €vÖ Ø�|‰|˜MÓ1Ð1ðIßŽX”|—~”~¨Ø—,‘,ÐtØ -Ð/ó1ß19±³ð àŠ{Ø÷	 &�X‰Xô
 *¬$¯*ª*°S¸µV·^°^¸tÓ*D×*JÐ*JÈÓKˆGØ")§+¡+¨iÓ"8×">Ð">¸BÐ">ÓqÑ">˜QÇ%Á%ÈÓBUÐY^×YbÑYbÐcoÓYpÑBp—q�qÑ">ˆGÐqØ�N‰N˜5Ô!Ø�L‰LÐ}ÜŸ*š* i°¸)ÀTÐ%JÓKÌTÏYÊYË[ÐZgÐhôj÷ &�X‰Xùò r÷ &—~ú�X�XˆXûô ô IÜ�‰ÐPÐR_×ajÑajÐkwÓaxð  DHˆ÷  	IðIúsƒ   °F# ¾FÁ'E<	Á8FÂ F# Â
$E<	Â/E<	Â=E<	ÃE<	Ã!&E7ÄE7ÄAE<	Å%FÅ-F# Å7E<	Å<FÆFÆF 	ÆF# Æ F# Æ#2GÇGc               ó(   € V ^8„  d   QhRRRRRRRR/# )	r$   ÚtaskrM   Úresult_jsonúOptional[str]Úerrorr“   r%   zOptional[list[dict[str, Any]]]r   )r'   s   "r!   r(   r(     s0   € ÷ yñ y˜^ð y¸-ð yÐPSð yÐXvñ yr    c                óˆ  € \         P                  ! T;'       g    R4      ;'       g    / pV P                  R4      '       d/   VP                  R4      '       d   VP                  R4      '       g   R# VR,           Uu/ uF4  p\        VP                  R4      \        4      '       g   K*  VR,          VbK6  	  ppV P                  R4      ;'       g7    \        \        \        V P                  R4      ;'       g    . 4      4      4      pV Uu. uF%  quP                  V4      ;'       g    RVR	R
RRRV/NK'  	  up# u upi u upi )zaPer-task results for an abandoned unit: recorded children as they finished, the rest ``unknown``.r–   ri   r™   r—   Nr˜   rj   rd   r�   ÚunknownÚsummaryr¨   )rv   r�   rq   Ú
isinstancer   ÚlistÚrangeÚlen)r¥   r¦   r¨   r™   r¢   ÚrecordedÚindexesÚis   &&&     r!   Ú_recovered_resultsr³     s  € ä�jŠj˜×,Ð,¨Ó-×3Ð3°€GØ�H‰H�Z× Ò  W§[¡[°×%;Ò%;ÀÇÁÈI×@VÒ@VÙØ,3°IÖ,>ÓgÑ,> qÄ*ÈQÏUÉUÐS_ÓM`Ôbe×BfÔ"��,• Ò"Ñ,>€HÐgØ�h‰h�~Ó&×SÐS¬$¬u´S¸¿¹À'Ó9J×9PÐ9PÈbÓ5QÓ/RÓ*S€GÙpwÓxÑpwÐkl�L‰L˜‹O×fÐf ¨a°¸9ÀiÐQUÐW^Ð`eÐfÒfÑpwÑxÐxùò hùâxs   Á='D:Â)D:ÄD?Ä(D?c               ó   € V ^8„  d   QhRR/# )r$   r%   z$Optional[Callable[[Any, Any], bool]]r   )r'   s   "r!   r(   r(     s   € ÷ ñ Ð=ñ r    c                 ó\   aaa€  ^ RI HoHoHo R TTT3R llp T #   \         d     R# i ; i)z‘``alive(owner_pid, owner_started_at)`` over the shared drift-tolerant start-time comparator,
or None when the liveness probes cannot be imported.)Ú_pid_existsr]   Ústart_time_fingerprints_matchNc               ó   € V ^8„  d   QhRR/# )r$   r%   Úboolr   )r'   s   "r!   r(   Ú%_owner_liveness.<locals>.__annotate__  s   € ÷ nñ n˜tñ nr    c                ó¸   <€ \        V 4      ;'       dH    S! \        V 4      4      ;'       d/    VR J ;'       g#    S! TS! \        V 4      4      ;'       g    ^ 4      # ©N)r¹   r   )ÚpidÚstartedr¶   r]   r·   s   &&€€€r!   ÚaliveÚ_owner_liveness.<locals>.alive  s`   ø€ Ü�C‹y÷ nð n™[¬¨S«Ó2÷ nð nØ�tˆO×lÐlÑ<¸WÑF\Ô]`ÐadÓ]eÓFf×FkÐFkÐjkÓlð	nr    )rn   r¶   r]   r·   rV   )r¿   r¶   r]   r·   s    @@@r!   Ú_owner_livenessrÁ     s5   ú€ ðßeÑe÷nò nð €Løô ô Úðús   …
 œ+ª+c               ó   € V ^8„  d   QhRR/# ©r$   r%   r   r   )r'   s   "r!   r(   r(     s   € ÷ 1ñ 1 sñ 1r    c                 ó  € \        4        \        4       p V f   ^ # \        P                  ! 4       ^ r!\        ;_uu_ 4        \	        4       ;_uu_ 4       pVP                  R4      P                  4       pV EFW  pVw  rgr‰r«rÍrïpV ! W¼4      '       d   K  \        P                  ! T;'       g    R4      pRp\        VVV4      pV'       d%   \        R V 4       4      pRV R\        V4       R2pR	TR
VP                  R
4      ;'       g    / /p^ RIHpHp V! VR
,          4      ;p'       d   VVR&   V! VP                  R4      4      ;p'       d   VVR&   / RRbRVbRVbRVbRT;'       g    RbRV	bRVP                  RR4      bRVP                  R4      bRVP                  R4      bRVP                  R4      bRVP                  R4      bRVP                  R4      bR\!        VP                  R4      4      bRRbR RbR!VbVCV'       d   R"V/M/ CR#V
R$V/C\"         Uu/ uF&  pVP                  V4      '       g   K  VVV,          bK(  	  upCpRRR RR!VR!,          /VCV'       d   R"V/M/ Cp\%        V4      \'        V4      ppVP                  R%W\        P(                  ! V4      \        P(                  ! V4      V34       V^,          pEKZ  	  RRR4       RRR4       V# u upi   + '       g   i     L; i  + '       g   i     T# ; i)&z´Classify records whose owning process disappeared as outcome unknown; children a multi-child unit had already
recorded (``record_unit_child``) are replayed with their real results.Na%  SELECT delegation_id, origin_session, origin_ui_session_id,
                      parent_session_id, dispatched_at, owner_pid,
                      owner_started_at, task_json, origin_session_id, result_json, state
               FROM async_delegations WHERE state IN ('running','finalizing')r–   zLDelegation owner exited before recording a terminal result; outcome unknown.c              3  óT   "  € T F  qP                  R 4      R8w  g   K  ^x € K   	  R# 5i)r�   rª   N©rq   ©Ú.0r¢   s   & r!   Ú	<genexpr>Ú0recover_abandoned_delegations.<locals>.<genexpr>/  s!   é € ÐXÑ&7 ¿5¹5À»?ÈiÑ;WŸ1š1Ó&7ùs   ‚(ž
(z2Delegation owner exited before the unit finished; Ú/zJ child results were recorded and are included below, the rest are unknown.Úlast_known_statusrk   )Úgit_state_hintÚtranscript_tailsrÎ   r^   rÍ   ÚtypeÚasync_delegationr_   r   r`   rb   rR   r   rc   rd   re   rf   rg   rh   ri   r�   rª   r«   r¨   r—   ra   r�   z½UPDATE async_delegations SET state='unknown', completed_at=?,
                   updated_at=?, event_json=?, result_json=?, delivery_state='pending'
                   WHERE delegation_id=?)r   rÁ   rm   rt   rJ   ru   Úfetchallrv   r�   r³   Úsumr¯   rq   Ú%tools.async_delegation_recovery_hintsrÍ   rÎ   r¹   rT   r   r   rw   )r¿   rz   Ú	recoveredr>   Úrowsr¡   r_   r   Ú	origin_uiÚ	parent_idra   r½   r¾   Ú	task_jsonÚ
origin_sidr¦   Ú
last_stater¥   r¨   Úrecovered_resultsÚdoneÚdiagnosticsrÍ   rÎ   ÚtailsÚhintrW   rŠ   r‹   s                                r!   Úrecover_abandoned_delegationsrà     s«  € ô ÔÜÓ€EØ‚}ÙÜ—Y’Y“[ !ˆß	Ž”<—>”> TØ�|‰|ð Qó R÷ S[ÑRZÓR\ð 	ô ˆCð MPñ  JˆM¨	¸mÐRYÐfpð  @JÙ�S×"Ò"ÙÜ—:’:˜i×/Ð/¨4Ó0ˆDØbˆEÜ 2°4¸ÀeÓ LÐß ÜÑXÑ&7ÓXÓX�ØMÈdÈVÐSTÔUXÐYjÓUkÐTlð m_ð _�à.°
Ð<NÐPT×PXÑPXÐYkÓPl×PrÐPrÐprÐsˆK÷ _Ù(¨Ð5GÕ)HÓIÐIˆuÖIØ27�Ð.Ñ/Ù% d§h¡h¨{Ó&;Ó<Ð<ˆtÖ<Ø04�Ð,Ñ-ð	FØÐ*ð	FØ,;¸]ð	FØLYÐ[fð	Fà&¨	ð	Fà3FÈ
×HXÐHXÐVXð	Fð $ Yð	Fð 17¸¿¹ÀÈÓ8Lð	Fð OVÐW[×W_ÑW_Ð`gÓWhð	Fð ˜4Ÿ8™8 IÓ.ð		Fð 1;¸D¿H¹HÀZÓ<Pð		Fð SYÐZ^×ZbÑZbÐciÓZjð		Fð
 ˜Ÿ™ 'Ó*ð	Fð
 -7¼¸T¿X¹XÀjÓ=QÓ8Rð	Fð ˜)ð	Fð &/°ð	Fð 7>¸uð	Fð ITð	F÷ 6G�IÐ0Ñ1ÈBð	Fð   °Àñ	Fõ (5ÓD¡} !¸¿¹À¿”:�1�d˜1•g’:¡}ÑDð	FˆEð  	¨9°d¸GÀUÈ7Å^ð WÐWbð Wß;L˜)Ð%6Ñ7ÐRTðWˆFä3°EÓ:Ô<OÐPVÓ<W�6ˆEØ�L‰Lð ,à.1¼¿
º
À5Ó8IÌ4Ï:Ê:ÐV\ÓK]Ð_lÐ-môoð ˜�N‹IñG ÷ "�ðR Ðùò E÷C "—>ú�ŽðR Ðúsc   ¿K:ÁAK'	Â*AK'	Ä"K'	Ä*#K'	ÅK'	Å*B5K'	ÈK"È=K"ÉK'	É'A)K'	ËK:Ë"K'	Ë'K7Ë2K:Ë:L	c               ó   € V ^8„  d   QhRR/# rÃ   r   )r'   s   "r!   r(   r(   N  s   € ÷ Lñ L°Sñ Lr    c                óÒ  € ^ RI Hp V! 4       '       d   ^ # \        4       P                  4       '       g   ^ # \	        4        \
        P
                  ! 4       p\        ;_uu_ 4        \        4       ;_uu_ 4       pVP                  R4      P                  4       p\        W4WRR7      uuRRR4       uuRRR4       #   + '       g   i     M; iRRR4       R#   + '       g   i     R# ; i)u�  Enqueue durable pending completions as fresh turns after process start.
Restored events are stamped ``restored=True`` in memory only: they came from a PREVIOUS
process, so drains without an ownership filter must leave them for a consumer that can
prove ownership. Rows older than ``_MAX_COMPLETION_REPLAY_AGE_S`` are terminally dropped
instead of replaying a turn nobody is waiting on.

Every restored event is stamped ``restored=True`` (in-memory only â€” the stamp is added after the durable
payload is deserialized and is never persisted). Restored events originate from a *previous* process, so
no consumer in THIS process implicitly owns them: drain paths that run without an ownership filter (the
legacy single-session behavior) must leave them queued for a consumer that can positively prove
ownership, otherwise a brand-new session adopts a dead session's delegation results seconds after boot
(#64484).
)r,   z÷SELECT delegation_id, event_json, completed_at, dispatched_at
               FROM async_delegations
               WHERE state != 'running' AND delivery_state='pending' AND event_json IS NOT NULL
               ORDER BY completed_at, delegation_idT)ÚstartupN)r7   r,   r"   Úexistsrà   rm   rt   rJ   ru   rÑ   Ú_replay_pending)Útarget_queuer,   rz   r>   rÕ   s   &    r!   Úrestore_undelivered_completionsrç   N  sŽ   € õ >Ù×Òñ Ü‹:×Ñ×ÒÙÜ!Ô#Ü
�)Š)‹+€Cß	Ž”<—>”> TØ�|‰|ð 7ó 8÷ 9A¹»
ð 	ô ˜t¨<ÀdÔK÷ "‘>�Š—>—>ú���Šús$   Á#CÁ6-B8	Â#
CÂ8CÃCÃC&	rã   Fc               ó$   € V ^8„  d   QhRRRRRR/# )r$   rz   Úfloatrã   r¹   r%   r   r   )r'   s   "r!   r(   r(   m  s"   € ÷ #ñ #°5ð #Àdð #ÐWZñ #r    c               ó¨  € \        \        4       4      ^ reV EF}  w  rxršT	;'       g    T
pV'       dW   W;,
          \        8”  dF   V P                  RW734       \        P                  RWsV,
          R,          \        R,          4       Kq  \        ;_uu_ 4        WW3\        9   d    RRR4       K—   RRR4       \        P                  ! V4      p\        V\        4      '       d   \        V4      p\        ;_uu_ 4        V'       d   \        P                  V4       \        V\        4      '       d   RVR&   V\        9   d   RVR&   RRR4       VP                  V4       \        ;_uu_ 4        \        P                  WW34       RRR4       V^,          pEK€  	  V#   + '       g   i     Lô; i  + '       g   i     Lq; i  + '       g   i     LF; i)a)  Put each pending ``(delegation_id, event_json, completed_at, dispatched_at)`` row on ``target_queue``
stamped ``restored``, or terminally drop it past ``_MAX_COMPLETION_REPLAY_AGE_S``. Records the offer so
the orphan sweep skips the row until the copy is handed back (``return_completion_offer``).zìUPDATE async_delegations SET delivery_state='dropped',
                          delivery_claim=NULL, delivery_claimed_at=NULL,
                          updated_at=?
                   WHERE delegation_id=? AND delivery_state='pending'z|Async delegation %s: pending completion is %.1fh old (cap %.1fh); terminally dropping the replay (result remains queryable).g      ¬@NTÚrestoredÚstartup_replay)r
   r	   Ú_MAX_COMPLETION_REPLAY_AGE_Sru   rŸ   r    Ú_orphan_lockr   rv   r�   r¬   Údictr   r   ÚaddÚput)r>   rÕ   ræ   rz   rã   Úhomerë   r_   Úpayloadr�   ra   Ú	age_basisÚevts   &&&&$        r!   rå   rå   m  sk  € ô %¤_Ó%6Ó7¸ˆ(Ü?CÑ;ˆ Ø ×1Ð1 Mˆ	ß˜#�/Ô-IÔIØ�L‰Lð Ið LOÐJ^ô`ô �N‰Nð eà(°­?¸fÕ*DÔFbÐekÕFkômñ ßŽ\ØÐ$¬Ô0ð ÷	 ‰\Ø0÷ ô
 �jŠj˜Ó!ˆÜ�cœ4× Ò Ü)¨#Ó.ˆCßŽ\ßÜ!×%Ñ% mÔ4Ü˜#œt×$Ò$Ø"&��J‘Ø Ô$5Ô5Ø,0�CÐ(Ñ)÷ ð 	×Ñ˜ÔßŽ\Ü�L‰L˜$Ð.Ô/÷ à�A�‹ñ; @Dð< €O÷' �\ú÷ �\ú÷ �\ús%   ÂFÃ<A	F.Å,GÆF+Æ.F>ÇGrz   c               ó    € V ^8„  d   QhRRRR/# ©r$   rz   úOptional[float]r%   r   r   )r'   s   "r!   r(   r(   “  s   € ÷ *Añ *A°_ð *AÐPSñ *Ar    c               óÆ  € \        4       pVe   \        4       P                  4       '       g   ^ # \        4        Vf   \        P                  ! 4       MTp\        \        4       4      p\        ;_uu_ 4        \         UUu0 uF  w  rEWC8X  g   K  VkK  	  pppRRR4       \        ;_uu_ 4        \        4       ;_uu_ 4       pVP                  RV\        ,
          V\        ,
          34      P                  4       p. p	V Fw  w  rZr¼rÞpVX9   g   V! WÞ4      '       d   K  T;'       g    ^ \        8¼  d1   VP                  RW34       \         P#                  RV\        4       Kd  V	P%                  WZW¼34       Ky  	  \'        WyW4      uuRRR4       uuRRR4       # u uppi   + '       g   i     EL; i  + '       g   i     M; iRRR4       R#   + '       g   i     R# ; i)aŒ  Offer this home's completions whose owner died after THIS process started (#97202).

Startup replay (``restore_undelivered_completions``) covers owners that died before the process
started; this covers the rest while it runs. Abandoned in-flight rows are first classified by
``recover_abandoned_delegations``. A terminal row qualifies when it is pending with an event, idle
past ``_ORPHAN_STALE_S``, not under a live delivery claim, and its owner fails the shared liveness
check. A row is offered once per live in-memory copy: a consumer that discards the copy with the row
still pending hands it back for the next sweep. The consumer's ``claim_completion_delivery`` stays
the atomic cross-process gate, so two processes offering one row never both deliver it. Rows past
the delivery budget or the replay age converge to ``dropped``. Reads the current profile's ledger:
callers bind the owning profile first.Na½  SELECT delegation_id, event_json, completed_at, dispatched_at,
                      owner_pid, owner_started_at, delivery_attempts
               FROM async_delegations
               WHERE state NOT IN ('running','finalizing') AND delivery_state='pending'
                 AND event_json IS NOT NULL AND updated_at < ?
                 AND (delivery_claim IS NULL OR delivery_claimed_at < ?)
               ORDER BY completed_at, delegation_idzÚUPDATE async_delegations SET delivery_state='dropped',
                              delivery_claim=NULL, delivery_claimed_at=NULL, updated_at=?
                       WHERE delegation_id=? AND delivery_state='pending'únAsync delegation %s exhausted its %d delivery attempts; marking terminally dropped (result remains queryable).)rÁ   r"   rä   rà   rm   r
   r	   rî   r   rt   rJ   ru   Ú_ORPHAN_STALE_SÚ_CLAIM_LEASE_SrÑ   Ú_MAX_DELIVERY_ATTEMPTSrŸ   r    rž   rå   )ræ   rz   r¿   rò   r|   r_   Úofferedr>   rÕ   Úorphansró   r�   ra   r½   r¾   Úattemptss   &$              r!   Úsweep_orphaned_completionsr  “  sv  € ô Ó€EØ‚}œH›J×-Ñ-×/Ò/ÙÜ!Ô#ØšŒ$�)Š)Œ+¨#€CÜœ?Ó,Ó-€Dß	ŽÝ;CÔS¹8Ñ%7 SÀsÁ{—=�=¹8ˆÑS÷ 
ç	Ž”<—>”> TØ�|‰|ð 7ð :=¼Õ9NÐPSÔVdÕPdÐ8eóg÷ hpÑgoÓgqð 	ð ˆÛ[_ÑWˆM LÀÈxØ Ô'©5°×+>Ò+>ÙØ—�˜AÔ"8Ô8à—‘ð MàORÐNbôdô —‘ð  Xà,Ô.DôFñ Ø�N‰N˜M°LÐPÖQñ \`ô ˜t¨lÓ@÷- "‘>�Šùó T÷ 
�ˆúç!—>ú���ŠúsP   Á5
FÁ?
F
ÂF
ÂFÂ-GÃ A$F2	Ä%AF2	Æ
GÆFÆF/	Æ2GÆ=GÇG 	c               ó    € V ^8„  d   QhRRRR/# r÷   r   )r'   s   "r!   r(   r(   À  s   € ÷ ñ ¸?ð ÐVYñ r    c               ó˜  € \        \        4       4      pVf   \        P                  ! 4       MTp\        ;_uu_ 4        \
        P                  V4      pVe   W,
          \        8  d    RRR4       ^ # V\
        V&   RRR4        \        V 4      #   + '       g   i     L; i  \         d    \        P                  RRR7        ^ # i ; i)zµ``sweep_orphaned_completions`` at most once per ``ORPHAN_SWEEP_INTERVAL_S`` per home (``now`` is
monotonic), for delivery loops that tick far more often. Never raises into the loop.Nz&Orphaned async delegation sweep failedTrš   )r
   r	   rm   Ú	monotonicrî   r   rq   ÚORPHAN_SWEEP_INTERVAL_Sr  rV   rŸ   Údebug)ræ   rz   rò   Úlasts   &$  r!   Ú maybe_sweep_orphaned_completionsr  À  s    € ô œ?Ó,Ó-€DØ!škŒ$�.Š.Ô
¨s€Cß	ŽÜ!×%Ñ% dÓ+ˆØÒ ¥
Ô-DÔ DØ÷ 
‰ð $'Ô˜4Ñ ÷	 
ð
Ü)¨,Ó7Ð7÷ 
�ûô ô Ü�‰Ð=ÈˆÔMÚðús#   ½,BÁ3	BÂ
B# ÂB 	Â#"C	ÃC	c               ó$   € V ^8„  d   QhRRRRRR/# )r$   Úsqlr“   ÚparamsÚtupler%   r¹   r   )r'   s   "r!   r(   r(   Ñ  s!   € ÷ 7ñ 7˜#ð 7 uð 7°ñ 7r    c           	     ó  € \         ;_uu_ 4        \        4       ;_uu_ 4       pVP                  W4      P                  ^8H  uuRRR4       uuRRR4       #   + '       g   i     M; iRRR4       R#   + '       g   i     R# ; i)z?Run one UPDATE on the ledger; True iff exactly one row changed.N)rt   rJ   ru   Úrowcount)r
  r  r>   s   && r!   Ú_update_deliveryr  Ñ  s?   € ç	Ž”<—>”> TØ�|‰|˜CÓ(×1Ñ1°QÑ6÷ "‘>�Š—>—>ú���Šús"   �A2¢A	Á 
A2ÁA%Á A2Á2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                ó\   € \        4        \        P                  ! 4       p\        RWV 34      # )zDAtomically acknowledge successful injection of a durable completion.z–UPDATE async_delegations SET delivery_state='delivered', delivered_at=?, updated_at=?
           WHERE delegation_id=? AND delivery_state!='delivered')r   rm   r  )r_   rz   s   & r!   Úmark_completion_deliveredr  ×  s1   € äÔÜ
�)Š)‹+€CÜð	DØFIÐP]ÐE^ó`ð `r    c               ó$   € V ^8„  d   QhRRRRRR/# ©r$   r_   r“   Úclaim_idr%   r¹   r   )r'   s   "r!   r(   r(   à  s!   € ÷ !ñ !¨Sð !¸Cð !ÀDñ !r    c                ó  € \        4       ;p'       d   VP                  W4      # \        P                  ! 4       p\        ;_uu_ 4        \	        4       ;_uu_ 4       pVP                  RV 34      P                  4       pVf    RRR4       RRR4       R# VP                  RWW0V\        ,
          34      pVP                  ^8H  uuRRR4       uuRRR4       #   + '       g   i     M; iRRR4       R#   + '       g   i     R# ; i)zBClaim one pending completion across competing consumers/processes.zBSELECT delivery_state FROM async_delegations WHERE delegation_id=?NTa  UPDATE async_delegations SET delivery_claim=?, delivery_claimed_at=?,
                      delivery_attempts=delivery_attempts+1, updated_at=?
               WHERE delegation_id=? AND delivery_state='pending'
                 AND (delivery_claim IS NULL OR delivery_claimed_at < ?))	r   Úclaimrm   rt   rJ   ru   rƒ   rü   r  )r_   r  ry   rz   r>   r¡   Úcurs   &&     r!   Úclaim_completion_deliveryr  à  sº   € ä“Ð €vÖ Ø�|‰|˜MÓ4Ð4Ü
�)Š)‹+€Cß	Ž”<—>”> TØ�l‰lØPÐS`ÐRbódßdlÑdlÓdnð 	àŠ;Ø÷	 "�‰ð
 �l‰lð Lð ˜C°´nÕ0DÐEó	Gˆð
 �|‰|˜qÑ ÷ "‘>�Š—>—>ú���Šús0   ÁC3Á'C	ÂC3Â.C	Ã
C3ÃC&Ã!C3Ã3D	c               ó    € V ^8„  d   QhRRRR/# )r$   rõ   rM   r%   r¹   r   )r'   s   "r!   r(   r(   ò  s   € ÷ Zñ Z ^ð Z¸ñ Zr    c                ón   € V P                  R4      R8H  ;'       d    \        V P                  R4      4      # )a  An early per-task notice for a batch that is still running. It shares the batch's
``delegation_id`` but is NOT the durable completion: it must never claim, acknowledge or
dedup against the final result's row (independent review reproduced exactly that loss).rÏ   rÐ   Útask_failure_notice)rq   r¹   )rõ   s   &r!   Úis_interim_delegation_eventr  ò  s/   € ð �7‰7�6‹?Ð0Ñ0×YÐY´T¸#¿'¹'ÐBWÓ:XÓ5YÐYr    c               ó$   € V ^8„  d   QhRRRRRR/# )r$   rõ   rM   Úconsumerr“   r%   r§   r   )r'   s   "r!   r(   r(   ù  s&   € ÷ Tñ T˜nð T¸ð TÀñ Tr    c                óR  € \        V 4      '       d   R# V P                  R4      R8X  d%   \        V P                  R4      ;'       g    R4      MRpV'       g   R# V R\        P                  ! 4        R\
        P                  ! 4       P                   2p\        W#4      '       d   V# R# )zYClaim a durable delegation event; non-durable events (and interim notices) need no token.rR   rÏ   rÐ   r_   Ú:N)	r  rq   r“   ro   rp   ÚuuidÚuuid4Úhexr  )rõ   r   r_   r  s   &&  r!   Úclaim_event_deliveryr&  ù  s‚   € ä" 3×'Ò'ÙØ;>¿7¹7À6»?ÐN`Ô;`”C˜Ÿ™ Ó0×6Ð6°BÔ7Ðfh€MßÙØ�˜1œRŸYšY›[˜M¨¬4¯:ª:«<×+;Ñ+;Ð*<Ð=€HÜ0°×IÒIˆ8ÐSÈtÐSr    c               ó$   € V ^8„  d   QhRRRRRR/# r  r   )r'   s   "r!   r(   r(     s!   € ÷ !ñ !¨sð !¸cð !Àdñ !r    c                ó6  € \        4       ;p'       d   VP                  W4      # \        P                  ! 4       p\        ;_uu_ 4        \	        4       ;_uu_ 4       pVP                  RW0V\        34      pVP                  ^8X  d/   \        P                  RV \        4        RRR4       RRR4       R# VP                  RW0V34      pVP                  ^8H  uuRRR4       uuRRR4       #   + '       g   i     M; iRRR4       R#   + '       g   i     R# ; i)zÐRelease a failed delivery claim so another consumer may retry. Attempts are
counted at claim time; once the budget is exhausted the row converges to
terminal ``dropped`` (only pending rows replay on restart).a	  UPDATE async_delegations SET delivery_state='dropped',
                      delivery_claim=NULL, delivery_claimed_at=NULL, updated_at=?
               WHERE delegation_id=? AND delivery_state='pending'
                 AND delivery_claim=? AND delivery_attempts>=?rú   NTzÖUPDATE async_delegations SET delivery_claim=NULL,
                      delivery_claimed_at=NULL, updated_at=?
               WHERE delegation_id=? AND delivery_state='pending'
                 AND delivery_claim=?)
r   Úreleaserm   rt   rJ   ru   rý   r  rŸ   r    )r_   r  ry   rz   r>   Úcappedr  s   &&     r!   Úrelease_completion_deliveryr+    sÖ   € ô “Ð €vÖ Ø�~‰~˜mÓ6Ð6Ü
�)Š)‹+€Cß	Ž”<—>”> TØ—‘ð Bð  Ô+AÐBó	Dˆð
 �?‰?˜aÔÜ�N‰Nð Tà(Ô*@ôBð ÷ "�‰ð �l‰lð )ð ,/¸xÐ*HóJˆð �|‰|˜qÑ ÷ "‘>�Š—>—>ú���Šús1   ÁDÁAC*	Â!DÂ3"C*	Ã
DÃ*C:Ã5DÄD	c               ó$   € V ^8„  d   QhRRRRRR/# r  r   )r'   s   "r!   r(   r(     s!   € ÷ 0ñ 0¨Sð 0¸Cð 0ÀDñ 0r    c                óŠ   € \        4       ;p'       d   VP                  W4      # \        R\        P                  ! 4       W34      # )zOReturn an unadmitted completion to pending without spending a delivery attempt.zþUPDATE async_delegations SET delivery_claim=NULL,
                  delivery_claimed_at=NULL, delivery_attempts=MAX(0, delivery_attempts-1),
                  updated_at=?
           WHERE delegation_id=? AND delivery_state='pending' AND delivery_claim=?)r   Údeferr  rm   ©r_   r  ry   s   && r!   Údefer_completion_deliveryr0    sA   € ä“Ð €vÖ Ø�|‰|˜MÓ4Ð4Üð Vô 
�Š‹�mÐ.ó	0ð 0r    c               ó$   € V ^8„  d   QhRRRRRR/# r  r   )r'   s   "r!   r(   r(   (  s&   € ÷ Nñ N¨Cð N¸3ð NÀ4ñ Nr    c                óŠ   € \        4       ;p'       d   VP                  W4      # \        R\        P                  ! 4       W34      # )u,  Terminally drop a claimed completion whose target is permanently gone (the
spawning session ended at an explicit user boundary such as /new or reset).
``dropped`` â€” not ``delivered`` â€” keeps the ack honest; not ``pending`` keeps
restart recovery from replaying it into a fail-closed drop forever.zöUPDATE async_delegations SET delivery_state='dropped',
                  updated_at=?, delivery_claim=NULL,
                  delivery_claimed_at=NULL
           WHERE delegation_id=? AND delivery_state='pending'
             AND delivery_claim=?)r   Údropr  rm   r/  s   && r!   Údrop_completion_deliveryr4  (  sD   € ô
 “Ð €vÖ Ø�{‰{˜=Ó3Ð3Üð %ô (,§y¢y£{°MÐ&Ló	Nð Nr    c               ó$   € V ^8„  d   QhRRRRRR/# r  r   )r'   s   "r!   r(   r(   6  s&   € ÷ 	Kñ 	K°ð 	K¸sð 	KÀtñ 	Kr    c                óŽ   € \        4       ;p'       d   VP                  W4      # \        P                  ! 4       p\        RW3W34      # )z;Acknowledge acceptance for the consumer holding this claim.a  UPDATE async_delegations SET delivery_state='delivered',
                  delivered_at=?, updated_at=?, delivery_claim=NULL,
                  delivery_claimed_at=NULL
           WHERE delegation_id=? AND delivery_state='pending'
             AND delivery_claim=?)r   Úackrm   r  )r_   r  ry   rz   s   &&  r!   Úcomplete_completion_deliveryr8  6  sG   € ä“Ð €vÖ Ø�z‰z˜-Ó2Ð2Ü
�)Š)‹+€CÜð %ð (+°Ð&Ió	Kð Kr    c               ó    € V ^8„  d   QhRRRR/# r  r   )r'   s   "r!   r(   r(   B  s   € ÷ Cñ C¨sð CÀTñ Cr    c                óô  € \         P                   ! 4       p\        ;_uu_ 4        \        4       ;_uu_ 4       pVP                  RW \        34      pVP
                  ^8X  d/   \        P                  RV \        4        RRR4       RRR4       R# VP                  RW 34      pVP
                  ^8w  d    RRR4       RRR4       R# VP                  RV 34      P                  4       p\        ;_uu_ 4        \        P                  V 4       RRR4       \        W6W4      ^8H  uuRRR4       uuRRR4       #   + '       g   i     L3; i  + '       g   i     M; iRRR4       R#   + '       g   i     R# ; i)aX  Offer an acknowledged completion again when its receiving turn could not confirm the owner.

The row returns to pending and its event goes back on ``target_queue``, so the next delivery
re-runs the pre-flight. A row past the delivery budget converges to ``dropped`` instead, which
bounds the loop; a row without a stored event is left as it is.z UPDATE async_delegations SET delivery_state='dropped', updated_at=?
               WHERE delegation_id=? AND delivery_state='delivered' AND delivery_attempts>=?rú   NFzËUPDATE async_delegations SET delivery_state='pending',
                      delivered_at=NULL, updated_at=?
               WHERE delegation_id=? AND delivery_state='delivered' AND event_json IS NOT NULLzySELECT delegation_id, event_json, completed_at, dispatched_at
               FROM async_delegations WHERE delegation_id=?)rm   rt   rJ   ru   rý   r  rŸ   r    rÑ   rî   r   Údiscardrå   )r_   ræ   rz   r>   Ú	exhaustedÚreopenedrÕ   s   &&     r!   Úrequeue_admitted_completionr>  B  s  € ô �)Š)‹+€Cß	Ž”<—>”> TØ—L‘Lð "`àÔ!7Ð8ó:ˆ	ð ×Ñ Ô"Ü�N‰Nð Tà(Ô*@ôBð ÷ "�‰ð —<‘<ð !bð Ð ó"ˆð ×Ñ Ô!Ø÷ "�‰ð �|‰|ð ?ØANÐ@PóRßRZÑRZÓR\ð 	çŽ\Ü×%Ñ% mÔ4÷ ä˜t¨<Ó=ÀÑB÷' "‘>�Š÷" �\ú÷# "—>ú���ŠúsS   ¤E&·AE		Á=E&Â%E		Â4E&Ã/E		Ã5D6ÄE		Ä!
E&Ä6EÅE		Å	EÅE&Å&E7	c               ó$   € V ^8„  d   QhRRRRRR/# ©r$   rõ   rM   r  r“   r%   rA   r   )r'   s   "r!   r(   r(   _  s&   € ÷ Añ A ð A¸3ð AÀ4ñ Ar    c                ó&   € \        \        W4       R # r¼   )Ú_event_deliveryr8  ©rõ   r  s   &&r!   Úcomplete_event_deliveryrD  _  s   € ÜÔ0°#Ö@r    c               ó$   € V ^8„  d   QhRRRRRR/# r@  r   )r'   s   "r!   r(   r(   c  s!   € ÷ !ñ ! ð !¸#ð !À$ñ !r    c                ó<   € \        \        W4       \        V 4       R# )z—Release a failed claim for a consumer that discards its copy (the TUI poller): the row is pending
again, so it must stay eligible for the orphan sweep.N)rB  r+  Úreturn_completion_offerrC  s   &&r!   Úrelease_event_deliveryrH  c  s   € ô Ô/°Ô?Ü˜CÖ r    c               ó    € V ^8„  d   QhRRRR/# )r$   rõ   rM   r%   rA   r   )r'   s   "r!   r(   r(   j  s   € ÷ 	Yñ 	Y ð 	Y°Dñ 	Yr    c           	     ó„  € V P                  R4      R8X  d%   \        V P                  R4      ;'       g    R4      MRpV'       d   \        V 4      '       d   R# \        ;_uu_ 4        \        P                  \         Uu0 uF  q"^,          V8X  g   K  VkK  	  up4       RRR4       R# u upi   + '       g   i     R# ; i)ap  Hand an offered completion back to the orphan sweep after its in-memory copy was discarded while
the durable row stays pending, e.g. a TUI session that cannot prove it owns the event drops it (every
session poller drains one process-wide queue). The next sweep may offer the row again. Delegation ids
are unique across profiles, so this clears the offer in every home.rÏ   rÐ   r_   rR   N)rq   r“   r  rî   r   Údifference_update)rõ   r_   r|   s   &  r!   rG  rG  j  s„   € ð
 <?¿7¹7À6»?ÐN`Ô;`”C˜Ÿ™ Ó0×6Ð6°BÔ7Ðfh€MßÔ7¸×<Ò<Ùß	ŽÜ×"Ñ"µ8Ó#W±8¨CÀ1½vÈÑ?V§C C±8Ñ#WÔX÷ 
‰ùÚ#W÷ 
�ˆús$   Á&B.Á>B)ÂB)ÂB.Â)B.Â.B?	c               ó$   € V ^8„  d   QhRRRRRR/# r@  r   )r'   s   "r!   r(   r(   v  s!   € ÷ :ñ :˜^ð :°sð :¸tñ :r    c                ó    € V'       dF   VP                  R 4      R8X  d.   T ! \        VP                  R4      ;'       g    R4      V4       R# R# R# )rÏ   rÐ   r_   rR   N)rq   r“   )Úfnrõ   r  s   &&&r!   rB  rB  v  s<   € ß�C—G‘G˜F“OÐ'9Ô9Ù
Œ3ˆs�w‰w�Ó'×-Ð-¨2Ó.°Ö9ñ :�xr    c               ó    € V ^8„  d   QhRRRR/# )r$   r_   r“   r%   zOptional[dict[str, Any]]r   )r'   s   "r!   r(   r(   {  s   € ÷ Hñ H¨#ð HÐ2Jñ Hr    c                ó°  € \        4       ;p'       dC   VP                  V 4      pVe,   VP                  R4      e   / VCR\        VR,          4      /C# V# \        ;_uu_ 4        \        4       ;_uu_ 4       pVP                  RV 34      P                  4       pR R R 4       R R R 4       Xf   R # RT RV^ ,          RV^,          RV^,          RV^,          RV^,          '       d'   \        \        P                  ! V^,          4      4      MR RV^,          R	V^,          R
V^,          ;'       g    R/	#   + '       g   i     L¢; i  + '       g   i     L­; i)Nr‹   zäSELECT origin_session, state, dispatched_at, completed_at,
                      result_json, delivery_state, delivery_attempts,
                      origin_session_id
               FROM async_delegations WHERE delegation_id=?r_   Úorigin_sessionÚstatera   r�   Údelivery_stateÚdelivery_attemptsrb   rR   )
r   Úreadrq   r   rt   rJ   ru   rƒ   rv   r�   )r_   ry   Úsavedr>   r¡   s   &    r!   Úget_durable_delegationrW  {  s1  € Ü“Ð €vÖ Ø—‘˜MÓ*ˆØÒ §¡¨8Ó!4Ò!@ØL�eÐL˜XÔ':¸5À½?Ó'KÑLÐLØˆß	Ž”<—>”> TØ�l‰lð ?ð BOÐ@PóR÷ S[ÑRZÓR\ð 	÷ "�ð
 ’;ˆ4ð HØ˜Ð(8¸#¸a½&À'È3ÈqÍ6ÐSbÐdgÐhiÕdjØ˜˜A� ÐUXÐYZ×U[ÔU[Ô*=¼d¿jºjÈÈQÍÓ>PÔ*QÐaeÐgwÐy|Ð}~ÕyØ˜S �VÐ%8¸#¸a½&¿,¸,ÀBð%Hð H÷ "—>ú��ús$   Á#EÁ6"D2	ÂEÄ2EÄ=EÅE	c               ó    € V ^8„  d   QhRRRR/# )r$   Úrawr§   r%   rM   r   )r'   s   "r!   r(   r(   �  s   € ÷ 4ñ 4�mð 4¨ñ 4r    c                óž   €  V '       d   \         P                  ! V 4      M/ p\        T\        4      '       d   T# / #   \         d    / u # i ; ir¼   )rv   r�   Ú
ValueErrorr¬   rï   )rY  Úvalues   & r!   Ú_json_objectr]  �  sG   € ðß#&”—
’
˜3”¨Bˆô ˜u¤d×+Ò+ˆ5Ð3°Ð3øô ô ØŠ	ðús   ‚< Š< ¼AÁArR   Úlimitc          
     ó,   € V ^8„  d   QhRRRRRRRRRR	/# )
r$   r`   r“   r   r^  r   rz   rø   r%   úlist[dict[str, Any]]r   )r'   s   "r!   r(   r(   ˜  s4   € ÷ (ñ (Øð(Ø7:ð(ØKNð(ØZið(àñ(r    c               óè  € RV 3RV33 UUu. uF  w  rEV'       g   K  WE3NK  	  pppV'       g   . # Ve   TM\         P                   ! 4       \        ,
          pRP                  R V 4       4      p\        ;_uu_ 4        \	        4       ;_uu_ 4       p	V	P                  RV R2. R V 4       OVNVN54      P                  4       p
RRR4       RRR4       . pX
 EFe  w  rÍrïpp\        V4      \        \        V4      4      pp\        VP                  R	4      \        4      '       d!   VR	,          '       d   VP                  R	4      MVP                  R
4      ;'       g    R.p\        \        VP                  R4      ;'       g    \        \        V4      4      V4      4      p\        VP                  R4      \        4      '       d
   VR,          MVP                  R4      '       d   . MV.pV'       g9   \!        V4      P#                  4       \$        9   d   R^ RVP                  R4      /.pV EF  p\!        VP                  R4      ;'       g    T;'       g    R4      P#                  4       pV\$        9  d   KM  \        VP                  R4      \&        4      '       d   VP                  R4      M^ pVP                  R4      ;'       g    VP                  R4      pTP)                  RTRTRTR
\!        VP                  VV^ ,          4      ;'       g    R4      RV'       d   \!        V4      MRRVRV/4       EK  	  EKh  	  VRV # u uppi   + '       g   i     EL’; i  + '       g   i     ELž; i)a=  Recently failed async delegation tasks owned by a session, newest first.

The live roster forgets a child once it ends and does not survive a renderer reload, so a failed
delegation had nowhere to show (#97202). This reads the durable row instead: one entry per failed
task (a batch unit that "completed" can still carry failed tasks) with ``delegation_id``,
``task_index``, ``goal``, ``status``, ``error``, ``dispatched_at`` and ``completed_at``. Either selector claims a row:
the UI session id at dispatch, or the spawner's durable session id (survives a reload re-mint).r`   r   Nz OR c              3  ó0   "  € T F  w  rV R 2x € K  	  R# 5i)z=?Nr   )rÈ   ÚcolÚ_s   &  r!   rÉ   Ú1failed_delegations_for_session.<locals>.<genexpr>§  s   é € Ð?±Y©6¨3˜s˜e 2�J³Yùs   ‚zSELECT delegation_id, state, dispatched_at, completed_at, task_json, result_json FROM async_delegations
                WHERE (zt) AND state NOT IN ('running','finalizing') AND completed_at >= ?
                ORDER BY completed_at DESC LIMIT ?c              3  ó*   "  € T F	  w  rVx € K  	  R # 5ir¼   r   )rÈ   rd  Úvals   &  r!   rÉ   re  ­  s   é € Ð+¡‘v�q�s£ùs   ‚rd   rc   rR   rj   r—   ri   r˜   r¨   r�   r_   ra   r�   )rm   Ú_FAILURE_SURFACE_WINDOW_SÚjoinrt   rJ   ru   rÑ   r]  r   r¬   rq   r­   rï   Úzipr®   r¯   r“   ÚlowerÚ_FAILED_TASK_STATESr   rž   )r`   r   r^  rz   rc  rg  Ú	selectorsr†   Ú	owner_sqlr>   rÕ   Úfailedr_   rR  ra   r�   rØ   r¦   r¥   r‹   rd   Úgoal_forÚtasksr”   r�   Úindexr¨   s   &&$$                       r!   Úfailed_delegations_for_sessionrs  ˜  sð  € ð 
 Ð!5Ð6Ð9LÐN_Ð8`ñ-bô jñ -b¡ Üehô �#“ñ -b€Iñ jçØˆ	Ø’_‰c¬$¯)ª)«+Ô9RÕR€FØ—‘Ñ?±YÓ?Ó?€Iß	Ž”<—>”> TØ�|‰|ðØ!�{ð #3ð6ð <Ñ+¡Ó+Ð;¨VÐ;°UÑ;ó	=÷ >F¹X»Zð	 	÷ "�ð $&€FÜUYÑQˆ˜m¸9ÀkÜ# IÓ.Ô0CÄLÐQ\ÓD]Ó0^ˆfˆÜ%/°·±¸Ó0AÄ4×%HÒ%HÈTÐRYÏ]Ì]�—‘˜Ô!Ðae×aiÑaiÐjpÓaq×awÐawÐuwÐ`xˆÜœ˜DŸH™H ^Ó4×IÐI¼¼cÀ%»jÓ8IÈ5ÓQÓRˆÜ%/°·
±
¸9Ó0EÄt×%LÒ%L��yÖ!ÐX\×X`ÑX`Ðak×XlÒXlÑRTÐsyÐrzˆßœ˜U›×)Ñ)Ó+Ô/BÔBØ" A w°·
±
¸7Ó0CÐDÐEˆEÜˆEÜ˜Ÿ™ 8Ó,×;Ð;°×;Ð;¸Ó<×BÑBÓDˆFØÔ0Ô0ÙÜ/9¸%¿)¹)ÀLÓ:QÔSV×/WÒ/W�E—I‘I˜lÔ+Ð]^ˆEØ—I‘I˜gÓ&×=Ð=¨&¯*©*°WÓ*=ˆEØ�M‰MØ °¸eÀXÈvØœ˜HŸL™L¨°°aµÓ9×?Ð?¸RÓ@À'×Y^Ì3ÈuÌ:ÐdhØ °ÀðN÷ Oô ñ VZð" �&�5ˆ>Ðùó=j÷ "—>�>ú��ˆús.   ŒM�MÁ=M Â3M	ÃM ÍMÍ	M Í M1	c               ó    € V ^8„  d   QhRRRR/# )r$   Úmax_workersr   r%   r   r   )r'   s   "r!   r(   r(   Ä  s   € ÷ ñ ˜sð Ð'9ñ r    c                óÈ   € \         ;_uu_ 4        \        f   \        V RR7      sV sMV \        8”  d   V \        n        V s\        uuRRR4       #   + '       g   i     R# ; i)z¤Lazily create (or grow in place, never shrink) the shared daemon executor. Raising
``_max_workers`` is enough: the next ``submit`` spawns threads up to the new cap.Nzasync-delegate)ru  Úthread_name_prefix)Ú_executor_lockr   r   r   Ú_max_workers)ru  s   &r!   Ú_get_executorrz  Ä  sH   € ÷ 
ŽÜÒÜ0¸[Ð]mÔnˆIØ$/Ñ!ØÔ0Ô0Ø%0ŒIÔ"Ø$/Ð!Ü÷ 
��‹ús   �6AÁA!	c               ó   € V ^8„  d   QhRR/# rÃ   r   )r'   s   "r!   r(   r(   Ò  s   € ÷ Tñ T�cñ Tr    c                 ó¤   € \         ;_uu_ 4        \        R \        P                  4        4       4      uuRRR4       #   + '       g   i     R# ; i)zfNumber of live async delegation UNITS (one per completion message: a task group or an ungrouped task).c              3  ó\   "  € T F"  qP                  R 4      \        9   g   K  ^x € K$  	  R# 5i©r�   N©rq   Ú_LIVE_STATESrÇ   s   & r!   rÉ   Úactive_count.<locals>.<genexpr>Õ  s!   é € ÐSÑ/˜·5±5¸³?ÄlÑ3R—1’1Ó/ùó   ‚,¢
,N©Ú_records_lockrÒ   r   Úvaluesr   r    r!   Úactive_countr†  Ò  s(   € ç	ŽÜÑSœhŸo™oÔ/ÓSÓS÷ 
��‹úó	   �$>¾A	c               ó   € V ^8„  d   QhRR/# rÃ   r   )r'   s   "r!   r(   r(   Ø  s   € ÷ Xñ X˜3ñ Xr    c                 ó¤   € \         ;_uu_ 4        \        R \        P                  4        4       4      uuRRR4       #   + '       g   i     R# ; i)u—   Number of running child subagents (a batch of N contributes N; a batch with
no goal list counts 1) â€” the truthful observability figure, unlike slots.c              3  óL  "  € T Fš  qP                  R 4      R9   g   K  VP                  R4      '       df   \        VP                  R4      \        \        34      '       d;   VR,          '       d,   \	        VP                  R4      ;'       g
    VR,          4      M^x € Kœ  	  R# 5i)r�   ri   rd   rj   N>   ÚrunningÚ
finalizing)rq   r¬   r­   r  r¯   rÇ   s   & r!   rÉ   Ú$active_task_count.<locals>.<genexpr>Ü  s„   é € ð Xñ '�¯%©%°«/Ð=VÑ*VôeØ�u‰u�Z× Ò ¤Z°·±°g³ÄÄuÀ×%NÒ%NÐSTÐU\×S]ÔS]ô �—‘�nÓ%×3Ð3¨¨7­Ô4Øcdôeã&ùs   ‚B$žB$µ9B$Á/B$ÂB$Nrƒ  r   r    r!   Úactive_task_countrŽ  Ø  s6   € ÷ 
ŽÜñ Xô —_‘_Ô&óXó X÷ 
��‹úr‡  c               ó(   € V ^8„  d   QhRRRRRRRR/# )r$   r   r“   r`   r   r%   r­   r   )r'   s   "r!   r(   r(   â  s0   € ÷ [ñ [¨Cð [Àsð [Ð_bð [Ðgkñ [r    c           	     óØ  a€ RV3RV3RV33 UUu. uF  w  rEV'       g   K  WE3NK  	  pppV'       g   . # \         ;_uu_ 4        \        P                  4        Uau. uF_  oSP                  R4      V 9   g   K  \        ;QJ d    V3R lV 4       F  '       g   K   RM	  RM! V3R lV 4       4      '       g   K]  SNKa  	  upuuRRR4       # u uppi u upi   + '       g   i     R# ; i)	u>  Records in ``statuses`` owned by a session: any non-empty selector claims the
record â€” ``origin_ui_session_id`` (TUI tab), ``session_key`` (routing key at
dispatch), or ``parent_session_id`` (spawner's durable id â€” the right one for
gateway chats, whose session_key survives ``/new`` while the session id rotates).r`   r   r   r�   c              3  óv   <"  € T F.  w  r\        SP                  V4      ;'       g    R 4      V8H  x € K0  	  R# 5i)rR   N)r“   rq   )rÈ   ÚfieldÚwantedr¢   s   &  €r!   rÉ   Ú#_session_records.<locals>.<genexpr>î  s-   øé € ÐYÉy¹m¸eœ˜AŸE™E %›L×.Ð.¨BÓ/°6Ö9Ëyùs   ƒ#9§9TFN)r„  r   r…  rq   Úany)Ústatusesr   r`   r   r’  r“  rm  r¢   s   &&&&   `r!   Ú_session_recordsr—  â  sÊ   ø€ ð 
 Ð!5Ð6¸ÈÐ8TØ	Ð/Ð0ñ72ô =ñ 72¡] Uä5;ô !�%“ñ 72€Iñ =÷ Øˆ	ß	ŽÜ#Ÿ?™?Ô,ô [Ñ,�a°·±°h³À8Ñ0K”ß“CÔYÉyÓY—C—C’CÔYÉyÓY×Y÷ �Ñ,ñ [÷ 
Šùó=ùò[÷ 
�ˆús@   �C¡CÁCÁCÁ8
CÂCÂCÂ:CÃ CÃCÃC)	c               ó(   € V ^8„  d   QhRRRRRRRR/# )r$   r   r“   r`   r   r%   r¹   r   )r'   s   "r!   r(   r(   ñ  s0   € ÷ fñ f cð fÀcð fÐcfð fÐptñ fr    c                ó6   € \        \        \        WV4      4      # )zOWhether a session still owns any live (running/stalling/finalizing) delegation.)r¹   r—  r€  )r   r`   r   s   &&&r!   Úhas_live_for_sessionrš  ñ  s   € äÔ ¤¨{ÐRcÓdÓeÐer    c               ó   € V ^8„  d   QhRR/# ©r$   r%   r“   r   )r'   s   "r!   r(   r(   ö  s   € ÷ +ñ +˜Cñ +r    c                 óT   € R \         P                  ! 4       P                  R,           2# )Údeleg_:Né   N)r#  r$  r%  r   r    r!   Ú_new_delegation_idr   ö  s!   € Ø”D—J’J“L×$Ñ$ RÕ(Ð)Ð*Ð*r    c               ó   € V ^8„  d   QhRR/# r€   r   )r'   s   "r!   r(   r(   ú  s   € ÷  ñ   ñ  r    c                 óD  € \         P                  4        U Uu. uF$  w  rVP                  R4      \        9  g   K!  W3NK&  	  pp pVP	                  R R7       VR\        ^ \        V4      \        ,
          4        F  w  r\         P                  V R4       K  	  R# u upp i )zðDrop the oldest completed records beyond the cap. Caller holds ``_records_lock``.
``stalling``/``finalizing`` are still live: evicting one makes the late runner return hit
``_finalize``'s missing-record path and silently drop a real result.r�   c                ó†   € V ^,          P                  R4      ;'       g#    V ^,          P                  R4      ;'       g    ^ # )é   r�   ra   rÆ   )Úkvs   &r!   Ú<lambda>Ú)_prune_completed_locked.<locals>.<lambda>ÿ  s2   €  " Q¥%§)¡)¨NÓ";×"^Ð"^¸rÀ!½u¿y¹yÈÓ?Y×"^Ð"^Ð]^Ð"^r    )r|   N)	r   Úitemsrq   r€  ÚsortÚmaxr¯   r„   Úpop)Úridr¢   rŽ   rd  s       r!   Ú_prune_completed_lockedr­  ú  s€   € ô )1¯©Ô(8Ô`Ñ(8™f˜c¸A¿E¹EÀ(»OÔS_Ñ<_”�#“Ñ(8€IÑ`Ø‡N�NÑ^€NÔ_ØÐNœc !¤S¨£^Ô6MÕ%MÓNÓO‰ˆÜ�‰�S˜$Öó Pùó as
   ˜B»Bc               ó   € V ^8„  d   QhRR/# rœ  r   )r'   s   "r!   r(   r(     s   € ÷ ñ  Cñ r    c                 ó†   €  ^ RI Hp  V ! RR4      R8H  pV'       d   V ! RR4      ;'       g    R# R#   \         d     R# i ; i)a¶  Raw session id of the ORIGINATING api_server request, or ``""``. ``HERMES_SESSION_ID``
is unsafe here: building the child agent calls ``set_current_session_id(child.session_id)``
just before dispatch, so the wake would self-post into the subagent's own session. The
request-scoped ``HERMES_SESSION_CHAT_ID`` (raw X-Hermes-Session-Id on api_server) survives
child construction; on push platforms chat_id is a chat, not a session => ``""``.rO   ÚHERMES_SESSION_PLATFORMrR   Ú
api_serverÚHERMES_SESSION_CHAT_ID)rS   rP   rV   )rP   Úis_apis     r!   Ú_current_origin_session_idr´    sL   € ðÝ;Ù Ð!:¸BÓ?À<ÑOˆßHN‘Ð 8¸"Ó=×CÐCÀÐVÐTVÐVøÜô Úðús   ‚1 œ1 ¬1 ¯1 ±A ¿A c               ó$   € V ^8„  d   QhRRRRRR/# ©r$   r¨   r“   Údurationré   r%   rM   r   )r'   s   "r!   r(   r(     s&   € ÷ nñ n˜ð n¨ð n°.ñ nr    c           
     ó   € R RRRRV R^ RV/# )r�   r¨   r«   NÚ	api_callsÚduration_secondsr   ©r¨   r·  s   &&r!   Ú_single_crashr¼    s   € Ø�g˜y¨$°¸ÀÈQÐPbÐdlÐmÐmr    c               ó$   € V ^8„  d   QhRRRRRR/# r¶  r   )r'   s   "r!   r(   r(     s&   € ÷ Oñ O˜ð O uð O°ñ Or    c                ó   € R . RV RV/# )r—   r¨   Útotal_duration_secondsr   r»  s   &&r!   Ú_batch_crashrÀ    s   € Ø�r˜7 EÐ+CÀXÐNÐNr    c               ó    € V ^8„  d   QhRRRR/# )r$   ÚcombinedrM   r%   r“   r   )r'   s   "r!   r(   r(     s   € ÷ oñ o˜Nð o¨sñ or    c                óÖ   a€ V P                  R4      ;'       g    . pRoV'       dC   \        ;QJ d    V3R lV 4       F  '       d   K   RM	  RM! V3R lV 4       4      '       d   R# R# )zCBatch status: completed unless every child errored/was interrupted.r—   rŽ   c              3  óJ   <"  € T F  qP                  R 4      S9  x € K  	  R# 5ir~  rÆ   )rÈ   r¢   Úoks   & €r!   rÉ   Ú _batch_status.<locals>.<genexpr>  s   øé € Ð+]É}È!¯E©E°(«OÀ2Ö,EË}ùs   ƒ #FTr¨   )rŽ   Úsuccess)rq   Úall)rÂ  Úchild_resultsrÅ  s   & @r!   Ú_batch_statusrÊ    sL   ø€ à—L‘L Ó+×1Ð1¨r€MØ	!€Bß#¯«Ô+]É}Ó+]¯¯ªÔ+]É}Ó+]×(]Ò(]ˆ7ÐnÐcnÐnr    c               ó   € V ^8„  d   QhRR/# rL   r   )r'   s   "r!   r(   r(   "  s   € ÷ ,ñ ,˜>ñ ,r    c                 óÂ   € ^ RI Hp VP                  4       ;_uu_ 4       pV'       g   RRRR/uuRRR4       # \        R/ V B uuRRR4       #   + '       g   i     R# ; i)r*   ©Ú
retirementr�   Úrejectedr¨   z*backend is retiring; reconnect to continueNr   )Úhermes_cli.backend_retirementrÎ  ÚworkÚ_dispatch_admitted)ÚkwargsrÎ  Úadmitteds   ,  r!   Ú	_dispatchrÕ  "  sH   € Ý8à	�‰×	Ô	˜hßØ˜j¨'Ð3_Ð`÷ 
Ò	ô "Ñ+ FÑ+÷ 
×	×	Ó	ús   ŸA¸
AÁA	Úslot_keyrj   rk   c          (     óh   € V ^8„  d   QhRRRRRRRRRRR	RR
RRRRRRRRRRRRRRRRRRRRRRRRRRR/# )r$   r_   r“   rc   rd   úOptional[list[str]]re   r§   rf   rg   rh   r   r   ÚrunnerúCallable[[], dict[str, Any]]r`   rb   Úinterrupt_fnúOptional[Callable[[], None]]Úmax_async_childrenr   Úprogress_fnúOptional[Callable[[], tuple]]Úcapacity_errorrÖ  rj   úOptional[list[int]]rk   úOptional[dict[str, str]]r%   rM   r   )r'   s   "r!   r(   r(   +  sú   € ÷ XDñ XDØðXDØ!$ðXDØ-@ðXDØKXðXDà!ðXDà),ðXDà5BðXDàQTðXDð %ðXDð /KðXDð cfðXDð ð	XDð +Gð	XDð ]`ð	XDð
 /ðXDð
 ADðXDð
 P]ðXDð &ðXDð /ðXDð ñXDr    c                óÎ  a a	aaaa a!a"€ \        4        VRJpV'       d   RMRo V'       d   \        MR oV'       d   \        M\        o\        P                  ! 4       oRS RV/V'       d   R\        V4      /M/ CRTR	V'       d   \        V4      MRR
VRVRVRV
RVRV/C\        4       CRRRSRRRV/CV'       d   RR/M/ CRTRT;'       g    S /CV'       d   R\        V4      /M/ CVe   R\        V4      /M/ CR\        P                  ! 4       RRRSRR/Co!\        ;_uu_ 4        \        ;QJ d#    V!3R l\         4       F  '       g   K   RM	  R M! V!3R l\         4       4      '       d   RR!R"R#/uuRRR4       # \        P                  4        Uu0 uFA  pVP                  R4      \         9   g   K  VP                  R4      ;'       g
    VR,          kKC  	  ppS!R,          V9  d!   \#        V4      V8¼  d   RR!R"V/uuRRR4       # S!\        S &   \%        R$ \        P                  4        4       4      pRRR4       \'        S!4       \)        \+        VX4      4      pR% VVV VV V	3R& llp^ R'IHo" S"P1                  4         VP3                  \5        V4      4      pVP7                  V"3R( l4       Te   \G        4        RR,RS /# u upi   + '       g   i     L�; i  \8         dù   pS"P;                  4        \        ;_uu_ 4        \        P=                  S R4       RRR4       M  + '       g   i     M; i\        4       ;p'       d   TP?                  S 4       Mm\@        ;_uu_ 4        \C        4       ;_uu_ 4       pTPE                  R)S 34       RRR4       M  + '       g   i     M; iRRR4       M  + '       g   i     M; iRR!R"R*S  R+T 2/u Rp?# Rp?ii ; i)-a  Shared dispatch core for single (``goals is None``) and batch units. Capacity check +
record insert happen under ONE lock hold so concurrent dispatches can't both pass the check
and exceed the cap. At capacity the dispatch is REJECTED (never queued) so a runaway model
can't pile up unbounded background work. ``slot_key`` names the pool slot the unit occupies
(default: its own id); the units of one delegate_task call share the first unit's id so
splitting a call into per-group completions never consumes more capacity than the call did.Nú batchrR   c                ó8   € V P                  R 4      ;'       g    R# )r�   rŽ   rÆ   )r¢   s   &r!   r¦  Ú$_dispatch_admitted.<locals>.<lambda>=  s   € ¸¿¹¸x»×9WÐ9WÈKÐ9Wr    r_   rc   rd   re   rf   rg   rh   r   r`   rb   r   r�   r‹  ra   r�   rÛ  ri   TrÞ  rÖ  rk   rj   Ú_contextÚ_progress_tokenÚ_progress_tsÚ_interrupted_atc              3  óF   <"  € T F  qSV,          3\         9   x € K  	  R # 5ir¼   )r   )rÈ   r’  r[   s   & €r!   rÉ   Ú%_dispatch_admitted.<locals>.<genexpr>R  s   øé € ÐQÁ.¸�v˜e•}Ð%¬Ö4Ã.ùs   ƒ!FrÏ  r¨   z?This session was closed (exit or /new): no new background work.c              3  ó\   "  € T F"  qP                  R 4      \        9   g   K  ^x € K$  	  R# 5ir~  r  rÇ   s   & r!   rÉ   rì  X  s!   é € ÐYÑ$5˜q¿¹¸x»ÌLÑ9XŸšÓ$5ùr‚  c               ó   € V ^8„  d   QhRR/# r€   r   )r'   s   "r!   r(   Ú(_dispatch_admitted.<locals>.__annotate__^  s   € ÷ 5ñ 5�Tñ 5r    c                 ó>  <€ / p R p\         ;_uu_ 4        \        P                  S4      pVe'   VP                  R\        P                  ! 4       R7       RRR4        S	! 4       ;'       g    / p S! V 4      p\        SW4       R#   + '       g   i     L8; i  \
         dj   p\        P                  RS R2S4       S! \        T4      P                   RT 2\        \        P                  ! 4       S,
          ^4      4      p  Rp?L�Rp?ii ; i  \        SY4       i ; i)r¨   NT)Ú_startedré  úAsync delegationz %s crashedú: )r„  r   rq   Úupdaterm   rV   rŸ   Ú	exceptionrÏ   Ú__name__ÚroundÚ	_finalize)
r‹   r�   ÚrecÚexcÚclassifyÚcrash_resultr_   ra   ÚlabelrÙ  s
       €€€€€€r!   Ú_workerÚ#_dispatch_admitted.<locals>._worker^  sà   ø€ Ø!#ˆØˆßŽ]Ü—,‘,˜}Ó-ˆCØŠà—
‘
 D´t·y²y³{�
ÔC÷	 ð
	5Ù“X—^�^ ˆFÙ˜fÓ%ˆFô
 �m VÖ4÷ �]ûô ô 	iÜ×ÑÐ/°¨w°kÐBÀMÔRÙ!¤T¨#£Y×%7Ñ%7Ð$8¸¸3¸%Ð"@Ä%ÌÏ	Ê	ËÐVcÕHcÐefÓBgÓh�Fûð	iûô �m VÕ4ús7   ”A BÁB ÂB	ÂDÂ"ADÄD ÄDÄD ÄDrÍ  c                ó$   <€ SP                  4       # r¼   )r)  )rd  rÎ  s   &€r!   r¦  ræ  v  s   ø€ ¨:×+=Ñ+=Ô+?r    z3DELETE FROM async_delegations WHERE delegation_id=?z#Failed to schedule async delegationró  Ú
dispatched)$r   rÊ  rÀ  r¼  rm   r­   rY   rï   ÚcontextvarsÚcopy_contextr„  r•  Ú_FENCED_FIELDSr   r…  rq   Ú_ACTIVE_STATESr¯   rÒ   r~   rz  rª  rÐ  rÎ  ÚacquireÚsubmitr   Úadd_done_callbackrV   r)  r«  Úunscheduledrt   rJ   ru   Ú_ensure_stale_monitor)#r_   rc   rd   re   rf   rg   rh   r   r   rÙ  r`   rb   rÛ  rÝ  rÞ  rà  rÖ  rj   rk   ri   r¢   Úactive_slotsÚ
live_unitsÚexecutorrþ  Úfuturerú  ry   r>   rû  rü  ra   rý  r[   rÎ  s#   d$$$$$$$$d$$$$$$$$$          @@@@@@r!   rÒ  rÒ  +  s�  ÿ€ ô „OØ˜DÐ €Hß ‰H b€Eß (�}Ñ/W€Hß#+•<´€LÜ—I’I“K€Mà˜¨°ðYßS[¸'Ä4ÈÃ;Ñ9OÐacðYà�7˜J¿(¬¨X¬ÈÈfÐVZÐ\cÐejØ�{Ð$:Ð<PØÐ.Ð0CÐEVñ	Yô
 "Ó
#ðYð 	�)˜_¨m¸^ÈTØ˜ñY÷ @H¨*°dÑ);ÈRðYð S`ÐalØ�H×-Ð- ñY÷ <LÐ¤Ð%5Ó 6Ñ7ÐQSðYð 4@Ò3KˆNœD Ó.Ñ/ÐQSðYð 	”K×,Ò,Ó.à˜4 °Ð@QÐSWñ!Y€F÷" 
Žß‹3ÔQÅ.ÓQ�3�3Š3ÔQÅ.ÓQ×QÒQØ˜j¨'Ð3tÐu÷ 
Šô JRÏÉÔIZó  AÑIZÀAÐ^_×^cÑ^cÐdlÓ^mÔqÑ^Ô?˜Ÿ™˜jÓ)×?Ð?¨Q¨Õ-?Ò?ÑIZˆð  AØ�*Õ \Ô1´c¸,Ó6GÐK]Ô6]Ø˜j¨'°>ÐB÷ 
Šð #)Œ�ÑÜÑY¤H§O¡OÔ$5ÓYÓYˆ
÷ 
ô �fÔô œSÐ!3°ZÓ@ÓA€H÷5õ 5õ" 9ð ×ÑÔðeØ—‘Ô!<¸WÓ!EÓFˆØ× Ñ Ô!?Ô@ð ÒÜÔØ�l O°]ÐCÐCùò_ A÷ 
�ûôL ô 	eØ×ÑÔßŽ]Ü�L‰L˜¨Ô-÷ �]�]ˆ]úä"“_Ð$ˆ6Ö$Ø×Ñ˜}Õ-ç–œ<Ÿ>œ>¨TØ—‘ÐRÐUbÐTdÔe÷ *Ÿ>Ÿ>˜>ú———�úà˜* gÐ1TÐUZÐT[Ð[]Ð^aÐ]bÐ/cÐdÕdûð	eús¨   Ä&$KÅ!KÅ/KÅ?KÆK	Æ6K	ÇK	Ç%KÈ-KÊ/K! Ë	KËK	Ë!O$Ë,OÌ
L*Ì!	OÌ*L:Ì5:OÍ/N;ÎNÎ	N;ÎN/Î*N;Î2	OÎ;OÏOÏO$ÏO$r`   rb   rÛ  rÝ  rÞ  c               óP   € V ^8„  d   QhRRRRRRRRRRR	RR
RRRRRRRRRRRRRRR/# )r$   rc   r“   re   r§   rf   rØ  rg   rh   r   r   rÙ  rÚ  r`   rb   rÛ  rÜ  rÝ  r   rÞ  rß  r%   rM   r   )r'   s   "r!   r(   r(   †  sŒ   € ÷ ñ ØðØ(ðØ4GðØORðØ[hðàðà)6ðàGcðð ðð 8;ðð Plðð ð	ð Ifð	ð
 ñr    c                ó  € \        4       p\        R/ RVbRV bRRbRVbRVbRVbRVbR	VbR
VbRVbRVbRV	bRV
bRVbRVbRRV R2b pVR,          R8X  d3   \        P                  RYÕ;'       g    RT ;'       g    RR,          4       V# )a   Spawn ``runner`` on the daemon executor and return a handle immediately.
``session_key``/``parent_session_id`` are captured on the parent thread (the worker carries
no contextvars) and route the completion back to the spawning session.
``progress_fn() -> (token, in_tool)`` enables stale monitoring; omitted = unmonitored.
Returns ``{"status": "dispatched", "delegation_id"}`` or ``{"status": "rejected", "error"}``.r_   rc   rd   Nre   rf   rg   rh   r   r   rÙ  r`   rb   rÛ  rÝ  rÞ  rà  ú#Async delegation capacity reached (zà running). Wait for one to finish (its result will re-enter the chat), or run this task synchronously (background=false). Raise delegation.max_concurrent_children in config.yaml to allow more concurrent background subagents.r�   r  z3Dispatched async delegation %s (session_key=%s): %sú<cli>rR   :NéP   Nr   )r   rÕ  rŸ   Úinfo)rc   re   rf   rg   rh   r   r   rÙ  r`   rb   rÛ  rÝ  rÞ  r_   Úhandles   $$$$$$$$$$$$$  r!   Údispatch_async_delegationr  †  s  € ô 'Ó(€MÜò 	wÙ#ð	wÙ*.ð	wÙ6:ð	wÙDKð	wáð	wá $ð	wá,1ð	wá?Jð	wñ ,ð	wñ 5;ð	wñ 2ð		wñ FWð		wñ
 "ð	wñ
 7Ið	wñ
 Wbð	wð 2Ð2DÐ1Eð Fuñ uð	w€Fð ˆhÕ˜<Ô'Ü�‰ÐIØ!×#9Ð#9°'¸D¿J¸JÀBÈÕ;Lô	Nà€Mr    r_   c          $     ó`   € V ^8„  d   QhRRRRRRRRR	RR
RRRRRRRRRRRRRRRRRRRRRRRRR/# )r$   rd   z	list[str]re   r§   rf   rØ  rg   r“   rh   r   r   rÙ  rÚ  r`   rb   rÛ  rÜ  rÝ  r   r_   rÞ  rß  rÖ  rj   rá  rk   râ  r%   rM   r   )r'   s   "r!   r(   r(   ¢  s¶   € ÷ !ñ !Øð!Ø"/ð!Ø;Nð!ØVYð!Øboð!àð!à)6ð!àGcð!ð ð!ð 8;ð!ð Plð!ð ð	!ð KXð	!ð
 /ð!ð
 BOð!ð &ð!ð /ð!ð ñ!r    c                óî  € T;'       g    \        4       pVe   V Uu. uF  pV V,          NK  	  upM
\        V 4      p\        V4      pV^8X  d
   V^ ,          M!V R2RP                  R V 4       4      ,           p\	        R/ RVbRVbRV bRVbRVbR	VbR
VbRVbRVbRVbRVbRV	bRV
bRVbRVbRVbRVbRVbRRV R2b pVR,          R8X  d#   \
        P                  RTTT;'       g    R4       V# u upi )u™  Dispatch a fan-out unit (a whole batch, or one ``group`` of a delegate_task call) as ONE
background unit: ``runner`` runs its tasks and returns the combined ``{"results": [...],
"total_duration_seconds": N}`` dict. The unit occupies ONE async slot â€” or joins the slot named
by ``slot_key`` (in-unit parallelism is bounded separately) â€” and produces a SINGLE completion
event carrying per-task ``results``.z parallel subagents: z; c              3  ó2   "  € T F  qR ,          x € K  	  R# 5i):Né(   NNr   )rÈ   Úgs   & r!   rÉ   Ú2dispatch_async_delegation_batch.<locals>.<genexpr>´  s   é € ÐXtÑisÐdeÐ[^×Y_ÒY_Óisùs   ‚r_   rc   rd   re   rf   rg   rh   r   r   rÙ  r`   rb   rÛ  rÝ  rÞ  rÖ  rj   rk   rà  r  z¬ running). Wait for one to finish (its result will re-enter the chat), or raise delegation.max_concurrent_children in config.yaml to allow more concurrent background units.r�   r  zADispatched async delegation batch %s (%d task(s), session_key=%s)r  r   )r   r­   r¯   ri  rÕ  rŸ   r  )rd   re   rf   rg   rh   r   r   rÙ  r`   rb   rÛ  rÝ  r_   rÞ  rÖ  rj   rk   r²   Ú
unit_goalsÚnÚcombined_goalr  s   $$$$$$$$$$$$$$$$$     r!   Údispatch_async_delegation_batchr   ¢  s’  € ð "×9Ð9Ô%7Ó%9€Mà5AÒ5M¡LÓ1¡L˜q�%˜—(�(¡LÒ1ÔSWÐX]ÓS^€JÜˆJ‹€AØ%&¨!¤V�J˜q–M°A°3Ð6KÐ1LÈtÏyÉyÑXtÑisÓXtÓOtÕ1t€MÜò 
GÙ#ð
GÙ*7ð
GÙ?Dð
GÙNUð
Gáð
Gá $ð
Gá,1ð
Gá?Jð
Gñ ,ð
Gñ 5;ð
Gñ 2ð	
Gñ FWð	
Gñ
 "ð
Gñ
 7Ið
Gñ
 Wbð
Gñ
 muð
Gñ "ð
Gñ 5Eð
Gð 2Ð2DÐ1Eð FEñ Eð
G€Fð ˆhÕ˜<Ô'Ü�‰ÐWØ! 1 k×&<Ð&<°Wô	>à€Mùò# 2s   �C2c               ó(   € V ^8„  d   QhRRRRRRRR/# )r$   r_   r“   r‹   r   r�   r%   rA   r   )r'   s   "r!   r(   r(   Ç  s(   € ÷ "ñ "˜Sð "¨#ð "°sð "¸tñ "r    c                ó&  € \         ;_uu_ 4        \        P                  V 4      pVe   VP                  R4      \        9  d    RRR4       R# RVR&   \        P                  ! 4       VR&   RVR&   RVR&   \        V4      pRRR4       \        X\        V4      '       d	   V! V4      MTV4       \         ;_uu_ 4        V \        9   d   V\        V ,          R&   \        4        RRR4       R#   + '       g   i     Lt; i  + '       g   i     R# ; i)a»  Atomically claim terminal delivery, push the completion event, then mark ``status``.
``result`` is a dict or a callable receiving the record snapshot (stall path). The record
stays active ("finalizing") until durable persistence and queue publication finish; otherwise
process shutdown can kill this daemon worker after status flips but before SQLite commits.
A second call for the same id (late runner return after a forced stall) is a no-op.Nr�   rŒ  r�   rÛ  rÞ  )	r„  r   rq   r  rm   rï   Ú_push_completion_eventÚcallabler­  )r_   r‹   r�   r[   Úsnapshots   &&&  r!   rø  rø  Ç  sÏ   € ÷ 
ŽÜ—‘˜mÓ,ˆØŠ>˜VŸZ™Z¨Ó1¼ÔGØ÷ 
‰ð (ˆˆxÑÜ!%§¢£ˆˆ~ÑØ!%ˆˆ~ÑØ $ˆˆ}ÑÜ˜“<ˆ÷ 
ô ˜8¼À&×9IÒ9I¡V¨HÔ%5ÈvÐW]Ô^ß	ŽØœHÔ$Ø06ŒH�]Õ# HÑ-ÜÔ!÷ 
‰÷ 
�ú÷ 
�ˆús   �5C,Á2C,Â<&C?Ã,C<	Ã?D	c               ó(   € V ^8„  d   QhRRRRRRRR/# )r$   r[   rM   r‹   r�   r“   r%   rA   r   )r'   s   "r!   r(   r(   Ý  s0   € ÷ 4Jñ 4J >ð 4J¸>ð 4JÐSVð 4JÐ[_ñ 4Jr    c                óÔ  € \        VRR7      p\        V P                  R4      4      pV'       d   RMRp ^ RIHp T P                  R4      ;'       g    \        P                  ! 4       pT P                  R4      ;'       g    \        P                  ! 4       pT'       dt   RRRTP                  R4      ;'       g    . RTP                  R4      RTP                  R4      RTP                  R4      /TP                  R4      e   RTR,          /M/ Cp	MXRTP                  R4      RTP                  R4      RTP                  R^ 4      RTP                  R\        Y‡,
          ^4      4      /p	RRR	T P                  R	4      RT P                  RR4      RT P                  RR4      RT P                  RR4      RT P                  R4      RT P                  RR4      /T'       d   RT P                  R4      /M/ CRT P                  R4      RT P                  R4      R T P                  R 4      R!T'       d   T P                  R!4      M)TP                  R!4      ;'       g    T P                  R!4      R"T/CT	CRTRT/CT'       d   / MR#TP                  R#4      /C\         U
u/ uF$  q P                  T
4      '       g   K  Y T
,          bK&  	  up
C\         U
u/ uF  qªT9   g   K  Y¡T
,          bK  	  up
Cp \        Y±4        TP                  P                  T4       R
#   \
         d6   p\        P                  RT R2T P                  R	4      T4        R
p?R
# R
p?ii ; iu up
i u up
i   \
         d5   p\        P                  RT R$2T P                  R	4      T4        R
p?L¥R
p?ii ; i  \
         d6   p\        P                  RT R%2T P                  R	4      T4        R
p?R
# R
p?ii ; i)&a  Push a type='async_delegation' event onto the shared completion queue. Batch records
(``is_batch``) carry the per-task ``results`` list (plus live transcript paths, the
full-fidelity record of each child's run) instead of a single summary. Best-effort: failure
must not crash the worker, but it WOULD mean a silently-lost result, so we log loudly.

The queued event is the parent's in-process copy and honours ``security.redact_secrets: false``;
``_persist_completion`` re-scrubs with force for the durable row (and the worker IPC).F©Úforceri   rä  rR   ©Úprocess_registryrò  z@ %s finished but process_registry import failed; result lost: %sr_   Nra   r�   Tr—   Úlive_transcriptsr¨   r¿  Úgroupr«   r¹  rº  rÏ   rÐ   r   r`   rb   r   rc   rd   re   rf   rg   rh   r�   Úexit_reasonzo %s: durable completion write failed; delivering in-memory only (a restart may report this unit as unknown): %sz8 %s: failed to enqueue completion event; result lost: %s)r   r¹   rq   Útools.process_registryr+  rV   rŸ   r¨   rm   r÷  rT   Ú_STALL_META_KEYSr‘   Úcompletion_queuerñ   )r[   r‹   r�   ri   rý  r+  rú  ra   r�   ró   rW   rõ   s   &&&         r!   r#  r#  Ý  sº  € ô ! ¨uÔ5€FÜ�F—J‘J˜zÓ*Ó+€Hß ‰H b€EðÝ;ð
 —J‘J˜Ó/×>Ð>´4·9²9³;€MØ—:‘:˜nÓ-×<Ð<´·²³€Lßà˜˜i¨¯©°IÓ)>×)DÐ)DÀ"Ø §
¡
Ð+=Ó >ÀÈÏÉÐT[ÓI\Ø$ f§j¡jÐ1IÓ&JðWð .4¯Z©Z¸Ó-@Ò-L�˜ �Ñ)ÐRTð	W‰ð �v—z‘z )Ó,¨g°v·z±zÀ'Ó7JÈKÐY_×YcÑYcÐdoÐqrÓYsØ §
¡
Ð+=¼uÀ\ÕEaÐcdÓ?eÓ fðhˆð 	Ð" O°V·Z±ZÀÓ5Pà�v—z‘z -°Ó4Ø §
¡
Ð+AÀ2Ó FØ˜VŸZ™ZÐ(;¸RÓ@Ø˜VŸZ™ZÐ(;Ó<Ø�—
‘
˜6 2Ó&ðC÷ NV¨G°V·Z±ZÀÓ5HÑ+IÐ[]ðCð 	�6—:‘:˜iÓ(¨*°f·j±jÀÓ6LÈfÐV\×V`ÑV`ÐagÓVhØ¯�—‘˜GÔ$°v·z±zÀ'Ó7J×7aÐ7aÈfÏjÉjÐY`ÓNaØ�&ñCð $ðCð &5°mÀ^ÐUañC÷ ‰2 ¨v¯z©z¸-Ó/HÐIðCõ "/Ó
@¡˜A·*±*¸Q·-Œ<ˆ1�Q�iŠ<¡Ñ
@ðCõ "2Ó
AÑ!1˜A¸&±[Œ<ˆ1�Q�iŠ<Ñ!1Ñ
AðC€CðoÜ˜CÔ(ðJØ×)Ñ)×-Ñ-¨cÖ2øôI ô Ü�‰Ð'¨ wð /'ð 'Ø(.¯
©
°?Ó(CÀSô	Jåûðüò8 AùÚ
Aøô ô oÜ�‰Ð'¨ wð /Lð LØMSÏZÉZÐXgÓMhÐjm÷	oñ 	oûðoûô
 ô JÜ�‰Ð'¨ wð /'ð 'Ø(.¯
©
°?Ó(CÀS÷	Jò 	JûðJús_   µL ÊMÊ8MËM ËM Ë/M% Ë;N' ÌMÌ#*MÍMÍ%N$Í0*NÎN$Î'O'Î2*O"Ï"O'c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r$   r_   r“   r”   rM   Ún_tasksr   r%   rA   r   )r'   s   "r!   r(   r(     s0   € ÷ "qñ "q¨Cð "q¸ð "qÐTWð "qÐ\`ñ "qr    c               ó¨  € \        VRR7      p\        ;_uu_ 4        \        P                  V 4      pVe   VP                  R4      \        9  d    RRR4       R# \        V4      pRRR4        ^ RIHp / RRbR	R
bRR
bRTbRT bRT.bRXP                  RR4      bRTP                  RR4      bRTP                  RR4      bRTP                  R4      bRTP                  RR4      bRTP                  R4      bRTP                  R4      bRTP                  R4      bRTP                  R4      bRTP                  R4      bRRbRTP                  R4      ;'       g    \        P                  ! 4       R\        P                  ! 4       /C\         Uu/ uF$  qtP                  T4      '       g   K  YtT,          bK&  	  upCp TP                  P                  T4       R#   + '       g   i     EL€; i  \         d"   p\        P                  RY4        Rp?R# Rp?ii ; iu upi   \         d"   p\        P                  RY4        Rp?R# Rp?ii ; i)aè  Surface ONE failed child of a still-running detached batch to the parent now, instead of
when the slowest sibling finishes. In a 1,393-agent run every wave-1 child died in a 401 storm
at 08:29 and the parent learned of it at 09:36, when the batch's "unknown outcome" block finally
arrived: 66 minutes of a dead wave with nothing running. The notice rides the same
``type="async_delegation"`` event shape as the batch result (so every drain/route/format path
treats it identically) with ``task_failure_notice=True`` and a single-entry ``results`` list; the
batch record is NOT finalized and its consolidated result still arrives as before. In-process
queue only (never persisted), so the parent's copy honours ``security.redact_secrets: false``.Fr(  Nr�   r*  zTAsync delegation batch %s: task failure notice dropped (process_registry import): %srÏ   rÐ   r  Tri   r3  r_   r—   r   rR   r`   rb   r   rc   rd   re   rf   rg   rh   r‹  ra   r�   zDAsync delegation batch %s: failed to enqueue task failure notice: %s)r   r„  r   rq   r  rï   r/  r+  rV   rŸ   r¨   rm   rT   r1  rñ   )	r_   r”   r3  r[   r%  r+  rú  rW   rõ   s	   &&$      r!   Úpush_task_failure_noticer5    s|  € ô   ¨UÔ3€Eß	ŽÜ—‘˜mÓ,ˆØŠ>˜VŸZ™Z¨Ó1¼ÔGØ÷ 
‰ô ˜“<ˆ÷	 
ð
Ý;ð
FØÐ"ð
FØ$9¸4ð
FØAKÈTð
FØS\Ð^eð
Fà˜ð
Fà(1°E°7ð
Fð 	�x—|‘| M°2Ó6ð
Fð 	 §¡Ð-CÀRÓ Hð	
Fð
 	˜XŸ\™\Ð*=¸rÓBð
Fð 	˜XŸ\™\Ð*=Ó>ð
Fð 	�—‘˜V RÓ(ð
Fð +2°8·<±<ÀÓ3Hð
Fð KTÐU]×UaÑUaÐbkÓUlð
Fð 	�H—L‘L Ó,ð
Fð /5°h·l±lÀ6Ó6Jð
Fð MTÐU]×UaÑUaÐbiÓUjð
Fð 	�)ð
Fð -¨h¯l©l¸?Ó.K×.ZÐ.ZÌtÏyÊyË{Ð\jÔlp×luÒluÓlwñ
Fõ $1Ó
D¡=˜a·L±LÀ·OŒ>ˆ1�q�kŠ>¡=Ñ
Dð
F€CðqØ×)Ñ)×-Ñ-¨cÖ2÷- 
�ˆûô ô Ü�‰ÐkÐmzô  	AÝûðüò Eøô ô qÜ�‰Ð[Ð]j×pÒpûðqúsM   œ5GÁGÁ/G1 ÆH Æ.H Ç H% ÇG.	Ç1HÇ<HÈHÈ%IÈ0IÉIc               ó   € V ^8„  d   QhRR/# r€   r   )r'   s   "r!   r(   r(   :  s   € ÷  ñ  ˜tñ  r    c                 ó@  € \         ;_uu_ 4        \        e&   \        P                  4       '       d    RRR4       R# \        P	                  4        \
        P                  ! \        RRR7      s\        P                  4        RRR4       R#   + '       g   i     R# ; i)zÄStart (once) the stale-delegation monitor thread. One daemon thread serves
every dispatch; it exits when no monitorable records remain and is restarted
by the next dispatch with a ``progress_fn``.Nzasync-delegate-stale-monitorT)ÚtargetÚnameÚdaemon)	Ú_monitor_lockr   Úis_aliveÚ_monitor_stopÚclearÚ	threadingÚThreadÚ_stale_monitor_loopÚstartr   r    r!   r
  r
  :  sg   € ÷
 
ŽÜÒ&¬?×+CÑ+C×+EÒ+EØ÷ 
‰ô 	×ÑÔÜ#×*Ò*Ü&Ð-KÐTXôZˆä×ÑÔ÷ 
��Šús   �"B²B½ABÂB	c               ó   € V ^8„  d   QhRR/# )r$   rz   ré   r   )r'   s   "r!   r(   r(   H  s   € ÷ #-ñ #-˜Uñ #-r    c           
     ó*  € . . Rr2p\         P                  4        EFO  pVP                  R4      pVR8X  dG   RpYP                  R4      ;'       g    T ,
          \        8¼  d   VP	                  VR,          4       Kb  VP                  R4      pVR8w  g   Vf   K€  RpVP                  R	4      '       g   K›   V! 4       w  rxWtP                  R
4      8w  d   VP                  WpR7       KÎ  YP                  R4      ;'       g    T ,
          p	V'       d   \        M\        p
Wš8¼  g   EK  VP                  RV \        V	^4      V
\        V4      R7       VP	                  VR,          W˜34       EKR  	  WV3#   \
         d    TP                  R
4      Rr‡ LÒi ; i)a   One monitor pass over ``_records``; caller holds ``_records_lock``. Returns
``(stalled, expired, any_monitorable)``: newly-stalling ``(delegation_id, quiet_for, in_tool)``
tuples, stalling ids past the grace window, and whether anything is left to monitor.Fr�   ÚstallingTrê  r_   rÞ  r‹  rñ  rè  )rè  ré  ré  )r�   rê  Ú_stall_quiet_secondsÚ_stall_threshold_secondsÚ_stall_in_tool)r   r…  rq   Ú_STALL_GRACE_SECONDSrž   rV   rô  Ú_STALE_IN_TOOL_SECONDSÚ_STALE_IDLE_SECONDSr÷  r¹   )rz   ÚstalledÚexpiredÚany_monitorabler[   r�   rÞ  ÚtokenÚin_toolÚ	quiet_forr^  s   &          r!   Ú_sweep_stale_lockedrR  H  st  € ð )+¨B°�o€GÜ—/‘/×#ˆØ—‘˜HÓ%ˆØ�ZÔØ"ˆOØ—j‘jÐ!2Ó3×:Ð:°sÕ;Ô?SÔSØ—‘˜v oÕ6Ô7ÙØ—j‘j Ó/ˆØ�YÔ +Ò"5ÙØˆØ�z‰z˜*×%Ò%Ùð	BÙ(›]‰NˆEð
 —J‘JÐ0Ó1Ô1Ø�M‰M¨%ˆMÔBÙØŸ:™: nÓ5×<Ð<¸Õ=ˆ	ß*1Õ&Ô7JˆØ×à�M‰MØ!°3ÌUÐS\Ð^_ÓM`Ø).¼tÀG»}ð ô Nð �N‰N˜F ?Õ3°YÐH×Iñ; $ð< ˜_Ð,Ð,øô ô 	Bð $ŸZ™ZÐ(9Ó:¸E’7ð	Bús   Â6	E2Å2FÆFc               ó    € V ^8„  d   QhRRRR/# )r$   Úmsgr“   r%   r¹   r   )r'   s   "r!   r(   r(   n  s   € ÷ 	ñ 	˜Sð 	¨Dñ 	r    c                ó¢   € \        V 4      '       g   R#  V ! 4        R#   \         d&   p\        P                  ! T.TOTN5!    Rp?R# Rp?ii ; i)zLInvoke an ``interrupt_fn``; True on success, else debug-log ``msg`` (+ exc).FTN)r$  rV   rŸ   r  )rN  rT  Úargsrú  s   &&* r!   Ú_call_interruptrW  n  sD   € ä�B�<Š<ÙðÙ
ŒÙøÜô Ü�Š�SÐ%˜4Ð% Ô%Ýûðús   • žA©A	Á	Ac               ó   € V ^8„  d   QhRR/# r€   r   )r'   s   "r!   r(   r(   z  s   € ÷ ñ ˜Tñ r    c                 óH  € \         P                  \        4      '       EgH   \        P                  ! 4       p \        ;_uu_ 4        \        V 4      w  rpRRR4       X Ft  w  rEp\        P                  RWEV\        4       \        ;_uu_ 4        \        P                  V4      ;'       g    / P                  R4      pRRR4       \        XRV4       Kv  	  X F€  p\        ;_uu_ 4        \        P                  V4      ;'       g    / P                  R4      ;'       g    \        P                  ! 4       pRRR4       XP                  \        WD3R lR4       K‚  	  X'       d   EKe  R# R#   + '       g   i     EL ; i  + '       g   i     LÊ; i  + '       g   i     Le; i)aG  Sweep running delegations for stalled progress. A changed progress token refreshes the
record's timestamp; a frozen token past the idle/in-tool threshold marks the record
``stalling`` and calls ``interrupt_fn``; a ``stalling`` record still unreturned after the
grace window is force-finalized with a terminal ``stalled`` event.Nu`   Async delegation %s made no progress for %.0fs (in_tool=%s) â€” interrupting; grace window %.0fsrÛ  z.Async delegation %s stall interrupt failed: %srç  c                ó   € \        W4      # r¼   )Ú_stalled_result)rù  Úds   &&r!   r¦  Ú%_stale_monitor_loop.<locals>.<lambda>�  s
   € Ì?Ð[\ÔKbr    rL  )r=  ÚwaitÚ_STALE_CHECK_INTERVALrm   r„  rR  rŸ   r    rI  r   rq   rW  r  r  Úrunrø  )	rz   rL  rM  rN  r_   rQ  rP  rN  Úctxs	            r!   rA  rA  z  s"  € ô
 × Ñ Ô!6×7Ó7Ü�iŠi‹kˆßŽ]Ü0CÀCÓ0HÑ-ˆG˜o÷ ã18Ñ-ˆM gÜ�N‰Nð Oà(°WÔ>RôT÷ –Ü—l‘l =Ó1×7Ð7°R×<Ñ<¸^ÓL�÷ ä˜BÐ PÐR_Ö`ñ 29ó %ˆMß–Ü—|‘| MÓ2×8Ð8°b×=Ñ=¸jÓI×gÐgÌ[×MeÒMeÓMg�÷ à�G‰G”I˜}Ó.bÐdmÖnñ %÷ ŠÙñ! 8ç�]ˆ]ú÷ —ú÷ —ús*   ÁE*Â/E>Ã(5FÄFÅ*E;	Å>FÆF!c               ó    € V ^8„  d   QhRRRR/# )r$   Úevent_recordrM   r%   r“   r   )r'   s   "r!   r(   r(   ’  s   € ÷ 	<ñ 	< nð 	<¸ñ 	<r    c                óH  € RP                  \        V P                  R4      ;'       g    R4      P                  4       4      pV'       d   RVR,           R2MRp\	        V P                  R4      ;'       g    ^ 4      pV^<8¼  d   R	\        V^<,          4       R
2MRpV RV R2# )u  Human wording for a force-finalized stall. This string reaches the user (CLI timeline, Desktop
async-result card), so it names the task, how long it was silent, and what to do â€” no issue
numbers or worker internals (those stay in the log line and the stall_* metadata).Ú rc   rR   zBackground task ":Néx   NÚ"zThe background taskrF  z after z min of no progressz stopped respondingz[ and was cancelled. Nothing else was affected; ask me to run it again if you still need it.)ri  r“   rq   Úsplitré   r÷  )rc  rc   rý  ÚquietÚsilences   &    r!   Ú_stalled_error_textrk  ’  s¦   € ð �8‰8”C˜×(Ñ(¨Ó0×6Ð6°BÓ7×=Ñ=Ó?Ó@€Dß15Ð  T¥
˜|¨1Ñ-Ð;P€EÜ�,×"Ñ"Ð#9Ó:×?Ð?¸aÓ@€EØBGÈ2Ä+�œ˜e b�jÓ)Ð*Ð*=Ñ>ÐSU€GØˆgÐ(¨¨	ð 2;ð ;ð <r    c               ó$   € V ^8„  d   QhRRRRRR/# )r$   r_   r“   rc  rM   r%   r   )r'   s   "r!   r(   r(   ž  s&   € ÷ kñ k 3ð k°nð kÈñ kr    c                ó  € VP                  R4      ;'       g    \        P                  ! 4       p\        Y!P                  R4      ;'       g    T,
          ^4      p\        V4      p\        P                  RW4       VP                  R4      pRVP                  R4      RVP                  R4      R	V'       d   R
MVe   RMRR\        /pVP                  R4      '       d   / \        WC4      CVC# / \        WC4      CRRRR/CVC# )zPSynthetic terminal result for a stalling delegation whose runner never returned.r�   ra   z:Async delegation %s force-finalized as stalled after %.0fsrH  Ústalled_after_quiet_secondsrF  Ústall_threshold_secondsrG  Ústall_phaserP  NÚidleÚstall_grace_secondsri   r�   rL  r.  )	rq   rm   r÷  rk  rŸ   r¨   rI  rÀ  r¼  )r_   rc  r�   r·  r¨   Ústall_in_toolÚ
stall_metas   &&     r!   r[  r[  ž  sù   € à×#Ñ# NÓ3×BÐB´t·y²y³{€LÜ�\×%5Ñ%5°oÓ%F×%VÐ%VÈ,ÕWÐYZÓ[€HÜ Ó-€EÜ
‡L�LÐMÈ}Ôgð !×$Ñ$Ð%5Ó6€Mà% |×'7Ñ'7Ð8NÓ'OØ! <×#3Ñ#3Ð4NÓ#OØ§M‘yÀÒAZ±vÐ`dØÔ3ð	5€Jð
 ×Ñ˜
×#Ò#Ø>”,˜uÓ/Ð>°:Ð>Ð>ØjŒm˜EÓ,Ðj¨h¸	À=ÐR[ÑjÐ_iÐjÐjr    c               ó$   € V ^8„  d   QhRRRRRR/# )r$   rO  r   rz   ré   r%   zOptional[list]r   )r'   s   "r!   r(   r(   ²  s!   € ÷ ñ ¨ð °5ð ¸^ñ r    c                óæ  €  \        V 4      p. pT FÊ  p\        T\         \        34      '       d   \	        T4      ^8¼  g   TP                  R4       KB  RT^ ,          RT^,          /p\	        T4      ^8¼  dT   \        T^,          \        \        34      '       d1   \        \        RT\        T^,          4      ,
          4      ^4      TR&   TP                  T4       KÌ  	  T#   \         d     R# i ; i)zÓParse a progress token into per-child activity dicts (best-effort): delegate_tool
emits one ``(api_call_count, current_tool, last_activity_ts)`` tuple per child;
foreign token shapes degrade to ``None`` entries.Nr¹  Úcurrent_toolg        Úseconds_since_activity)
r­   Ú	TypeErrorr¬   r  r¯   rž   r   ré   r÷  rª  )rO  rz   ÚpartsÚoutÚpartr”   s   &&    r!   Ú_children_activity_from_tokenr}  ²  sÎ   € ðÜ�U“ˆð +-€CÛˆÜ˜4¤$¬ ×/Ò/´C¸³IÀ´NØ�J‰J�tÔÙØ!,¨d°1­g°~ÀtÈAÅwÐ OˆÜˆt‹9˜Œ>œj¨¨a­´3¼°,×?Ò?Ü.3´C¸¸SÄ5ÈÈaÍÃ>Õ=QÓ4RÐTUÓ.VˆEÐ*Ñ+Ø�
‰
�5Öñ ð €Jøô ô Úðús   ‚C! Ã!C0Ã/C0c               ó   € V ^8„  d   QhRR/# )r$   r%   r`  r   )r'   s   "r!   r(   r(   Æ  s   € ÷ &ñ &Ð 4ñ &r    c            
     óÔ  € \         P                   ! 4       p / p\        ;_uu_ 4        . p\        P                  4        EF  pVP	                  4        UUu/ uF)  w  rEVR
9  g   K  VP                  R4      '       d   K'  WEbK+  	  pppVP                  R4      pV\        9   df   VP                  R4      '       d   \        WR,          ,
          ^4      VR&   \        VP                  R4      4      '       d   VR,          WR,          &   VR9   d8   \         F-  w  r‰VP                  V4      f   K  VP                  V4      Wi&   K/  	  VP                  V4       EK  	  RRR4       X FT  pVP                  VP                  R4      4      p
V
f   K)   V
! 4       w  r¼\        Y°4      pTe   YÖR&   \        T4      TR	&   KV  	  V# u uppi   + '       g   i     Lr; i  \         d     K�  i ; i)a9  Snapshot of async delegations (running + recently completed) without callables or private
monitor bookkeeping; adds computed live fields for UIs (``seconds_since_progress``,
``children_activity``/``in_tool`` sampled from ``progress_fn``) and stall context once tripped.

Safe to call from any thread. See #51690.
rÞ  rd  r�   ré  Úseconds_since_progressr_   NÚchildren_activityrP  >   rÞ  rÛ  )rE  rL  )rm   r„  r   r…  r¨  Ú
startswithrq   r  r÷  r$  Ú_STALL_FIELD_MAPrž   rV   r}  r¹   )rz   Úsamplersr¨  r¢   rW   rX   Úitemr�   ÚsrcÚdstrN  rO  rP  Úactivitys                 r!   Úlist_async_delegationsr‰  Æ  s›  € ô �)Š)‹+€CØ$&€Hß	ŽØˆÜ—‘×"ˆAØ%&§W¡W¤YÔu¡Y™T˜Q°!Ð;ZÑ2Z”DÐcd×coÑcoÐps×ct”D�A’D¡YˆDÑuØ—U‘U˜8“_ˆFØœÔ'Ø—5‘5˜×(Ò(Ü5:¸3À>ÕARÕ;RÐTUÓ5V�DÐ1Ñ2Ü˜AŸE™E -Ó0×1Ò1Ø34°]Õ3C�H˜Õ/Ñ0ØÐ0Ô0ß 0‘H�CØ—u‘u˜S“zÔ-Ø$%§E¡E¨#£J˜›	ñ !1ð �L‰L˜×ñ #÷ 
ó" ˆØ�\‰\˜$Ÿ(™( ?Ó3Ó4ˆØŠ:Ùð	Ù›T‰NˆEô 1°Ó<ˆØÒØ(0Ð$Ñ%Ü˜w›-ˆˆY‹ñ ð €Lùó5 v÷ 
�ûô. ô 	Úð	úsH   ¦.GÁF?Á$F?Á=F?Â5GÂ9A0GÄ.-GÆ	GÆ?GÇG	ÇG'Ç&G'c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )	r$   Útargetsr`  Úcallerr“   ÚreasonrT  r%   r   r   )r'   s   "r!   r(   r(   ï  s2   € ÷ ñ Ð 4ð ¸cð È3ð ÐUXð Ð]`ñ r    c                ón   a€ \        V3R lV  4       4      pV'       d   \        P                  W4V4       V# )zSCall ``interrupt_fn`` on each record; log ``msg`` once; returns how many succeeded.c           	   3  ó|   <"  € T F1  p\        VP                  R 4      RSVP                  R4      4      x € K3  	  R# 5i)rÛ  z%s: %s interrupt failed: %sr_   N)rW  rq   )rÈ   r¢   rŒ  s   & €r!   rÉ   Ú%_interrupt_records.<locals>.<genexpr>ñ  s=   øé € ð áˆAô 	˜Ÿ™˜nÓ-Ð/LÈfÐVW×V[ÑV[Ð\kÓVl×mÐmÛùs   ƒ9<)rÒ   rŸ   r  )r‹  rŒ  r�  rT  Úcounts   &f&& r!   Ú_interrupt_recordsr’  ï  s2   ø€ äô áóó €E÷ Ü�‰�C Ô'Ø€Lr    c               ó    € V ^8„  d   QhRRRR/# )r$   r�  r“   r%   r   r   )r'   s   "r!   r(   r(   ù  s   € ÷ kñ k˜#ð k¨sñ kr    c                óø   € \         ;_uu_ 4        \        P                  4        Uu. uF   qP                  R4      \        9   g   K  VNK"  	  ppRRR4       \        XRV R4      # u upi   + '       g   i     L#; i)z´Signal every running async delegation to stop (``/stop``, shutdown). Returns how
many. The child still emits a completion event (status='interrupted') via the
normal finalize path.r�   NÚinterrupt_allz'Interrupted %d async delegation(s) (%s))r„  r   r…  rq   r  r’  )r�  r¢   r‹  s   &  r!   r•  r•  ù  sZ   € ÷ 
ŽÜ&Ÿo™oÔ/ÓUÑ/˜·5±5¸³?ÄnÑ3T—1�1Ñ/ˆÐU÷ 
ä˜g ¸Ð@iÓjÐjùò V÷ 
�ús"   �A)¦A$ÁA$ÁA)Á$A)Á)A9	c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )r$   r   r“   r`   r   r�  r%   r   r   )r'   s   "r!   r(   r(     s;   € ÷ pñ pØðpØ14ðpØNQðpØ`cðpàñpr    c                ó@   € \        \        WV4      p\        VRVR4      # )zˆSignal running async delegations owned by ONE ending session to stop (any
selector matches, see ``_session_records``). Returns how many.Úinterrupt_for_sessionz:Interrupted %d async delegation(s) for ending session (%s))r—  r  r’  )r   r`   r   r�  r‹  s   &&&& r!   r˜  r˜    s-   € ô
 œ~¨{ÐRcÓd€GÜØÐ(¨&Ð2nópð pr    Úparent_session_idsc               ó$   € V ^8„  d   QhRRRRRR/# )r$   rO  r   r   r“   r%   rA   r   )r'   s   "r!   r(   r(     s"   € ÷ 	?ñ 	?˜cð 	?°3ð 	?ÐW[ñ 	?r    c               ó`  € V'       d   RV30M	\        4       pVP                  R V 4       4       \        ;_uu_ 4        \        V 4       \	        V4      \
        V &   V F0  p\        P                  V\        4       4      P                  V 4       K2  	  RRR4       R#   + '       g   i     R# ; i)zÈRefuse new dispatches for ONE closing session (any selector matches) until ``allow_for_session``.
Set before ``interrupt_for_session``: a dispatch registered first is interrupted, a later one refused.r   c              3  ó<   "  € T F  q'       g   K  R V3x € K  	  R# 5i)r   Nr   )rÈ   r;   s   & r!   rÉ   Ú%refuse_for_session.<locals>.<genexpr>  s   é € ÐZÑ=O°6ÓSYÔ.Ð% vÕ.Ó=Oùs   ‚	�N)	r   rô  r„  Ú_unfenceÚ	frozensetr   r   Ú
setdefaultrð   )rO  r   r™  ÚfenceÚselectors   &$$  r!   Úrefuse_for_sessionr£    sw   € ÷ /:ˆm˜[Ð)Ñ*¼s»u€EØ	‡L�LÑZÑ=OÓZÔZß	ŽÜ�ŒÜ# EÓ*Œ�‰ÛˆHÜ×"Ñ" 8¬S«UÓ3×7Ñ7¸Ö>ñ ÷ 
��Šús   ¾ABÂB-	c               ó    € V ^8„  d   QhRRRR/# ©r$   rO  r   r%   rA   r   )r'   s   "r!   r(   r(     s   € ÷ ñ ˜Sð  Tñ r    c                ór   € \         ;_uu_ 4        \        V 4       RRR4       R#   + '       g   i     R# ; i)zYLift ONE session's refusal: a viewer attached again, or the owner retired its live state.N)r„  rž  )rO  s   &r!   Úallow_for_sessionr§    s   € ç	ŽÜ�Œ÷ 
��Šús   �%¥6	c               ó    € V ^8„  d   QhRRRR/# r¥  r   )r'   s   "r!   r(   r(     s   € ÷ &ñ &�Cð &˜Dñ &r    c                óž   € \         P                  V R4       F2  p\        V,          pVP                  V 4       V'       d   K+  \        V K4  	  R# )z7Drop *token*'s refusal; caller holds ``_records_lock``.Nr   )r   r«  r   r;  )rO  r¢  Útokenss   &  r!   rž  rž    s:   € ä—L‘L ¨Ö+ˆÜ˜XÕ&ˆØ�‰�uÔß‰vÜ˜HÒ%ó	 ,r    c               ó   € V ^8„  d   QhRR/# r€   r   )r'   s   "r!   r(   r(   '  s   € ÷ #ñ #˜$ñ #r    c                 ó   € \         ;_uu_ 4        \        e   \        P                  RR7       Rs^ sRRR4       \        P                  4        \        ;_uu_ 4        \        Rup sRRR4       X e)   V P                  4       '       d   V P                  ^R7       \        ;_uu_ 4        \        P                  4        \        P                  4        \        P                  4        RRR4       \        ;_uu_ 4        \         P                  4        \"        P                  4        \$        P                  4        RRR4       R#   + '       g   i     EL; i  + '       g   i     Lø; i  + '       g   i     LŒ; i  + '       g   i     R# ; i)z@Test-only: clear all state and tear down the executor + monitor.NF)r^  )Útimeout)rx  r   Úshutdownr   r=  r   r;  r   r<  ri  r„  r   r>  r   r   rî   r   r   r   )Úthreads    r!   Ú_reset_for_testsr°  '  sò   € ÷ 
ŽÜÒ Ü×Ñ EÐÔ*Øˆ	Ø !Ð÷	 
ô
 ×ÑÔß	ŽÜ"1°4Ðˆ�÷ 
àÒ˜fŸo™o×/Ò/Ø�‰˜AˆÔß	ŽÜ�‰ÔÜ�‰ÔÜ×ÑÔ÷ 
÷ 
ŽÜ�‰ÔÜ×ÑÔ!Ü× Ñ Ô"÷ 
‰÷ 
�ˆú÷ 
�ú÷ 
�ú÷ 
�ˆús/   �#EÁ
EÂ(=E)Ã;=E<ÅE	ÅE&	Å)E9	Å<F	)r   r   i€:	 g     A>   r‹  rE  rŒ  )r‹  rE  )Úscope_idÚuser_idÚ	user_name)rn  ro  rp  rr  ))rF  rn  )rG  ro  )rH  rs  >   r¨   ro  ÚfailurerL  r­  rª   Úinterruptedg     õ@)rR   rR   )rR   rR   rR   )r®  )rR   rR   rR   Úsession_endr   )�Ú__conditional_annotations__Ú__doc__Ú
__future__r   r  rv   Úloggingro   Úsqlite3r?  rm   r#  Úconcurrent.futuresr   Útypingr   r   r   r   r   r9   r	   r
   Útools.daemon_poolr   Útools.thread_contextr   Útools.async_delegation_workerr   r   Útools.delegation_redactionr   r   Ú	getLoggerrö  rŸ   r   Ú__annotations__ÚLockrx  r   r„  r   r   r   r  Ú_DEFAULT_MAX_ASYNC_CHILDRENr„   r‚   r…   rý   rí   rü   rt   r  rû   rî   r   r   r   r   r_  rK  rJ  rI  r;  r   ÚEventr=  r€  r  rT   r0  rƒ  r"   r?   r<   rJ   rY   r~   rx   r‘   r£   r³   rÁ   rà   rç   rå   r  r  r  r  r  r  r&  r+  r0  r4  r8  r>  rD  rH  rG  rB  rW  rŸ  rl  rh  r]  rs  rz  r†  rŽ  r—  rš  r   r­  r´  r¼  rÀ  rÊ  rÕ  rÒ  r  r   rø  r#  r5  r
  rR  rW  rA  rk  r[  r}  r‰  r’  r•  r˜  r£  r§  rž  r°  )r·  s   @r!   Ú<module>rÇ     s  øðôhõ #ã Û Û Û 	Û Û Û Û Ý 1ß 6Õ 6ç =Ý 6Ý <ß Mß Sà	×	Ò	˜8Ó	$€ð
 +/€	Ð'Ó .Ø—’Ó!€ØÐ �sÓ à—’Ó €à&(€Ð
#Ó (ð "$€Ð
Ó #Ø "€ÐÓ "Ø5€àÐ àÐ Ø-Ð ØÐ àÐ ð  +Ð à€Ø�>Š>Ó€ð Ð à€Ø�~Š~Ó€ñ “€ˆ#Ó ñ ›Ð �3Ó Ø')Ð Ð$Ó )ð Ð ØÐ ØÐ ØÐ à—’Ó €Ø.2€Ð+Ó 2Ø—’Ó!€â4€Ø(€ð 5€àsÐ ðrÐ ò
*õõ,!ò #õõõ<Bõ4
MõIõ.yõõ1õhLð>#ÈU÷ #ðL*AÀt÷ *AðZÈT÷ õ"7õ`õ!õ$ZõTõ!õ20õNõ	KõCõ:Aõ!õ	Yõ:õ
Hñ"  Ò nÓoÐ Ø'Ð õ4ñ(ØQSð(Ølp÷(ð (õXõTõXõ[÷fõ
+õ õõnõOõoõ,ðXDð
 `dðXDð )-ðXDð 26÷XDðvà9=ðð !#ðð >@ðð osðð :ð	ð im÷	ð8!à9=ð!ð !#ð!ð >@ð!ð osð!ð :ð	!ð [_ð	!ð
 26ð!ð
 RVð!ð )-ð!ð 26÷!õJ"õ,4Jõn"qõL õ#-õL	õõ0	<õkõ(õ(&õR÷k÷pð	?¸ð 	?ÐPR÷ 	?õõ&÷#r    