+
    jHàjL3  ã                   ó   € R t ^ RIt^ RIHt ^ RIHt ^ RIHt ^ RIt^ RI	t	^ RI
Ht ^ RIHt ]P                  ! ]4      t]! 0 Rm4      tRt^t^tR t ! R R	4      tR
 R ltR tR t ! R R4      tR tR tR tR tR# )a  ``llm.oneshot`` on the shared gateway: a session-bound one-shot borrows that session's model.

Main's sidecar looked the session up in its own table and lent its live agent's route
(``tui_gateway/session_registry._main_runtime_from_agent``); a session-less call (project ideas)
ran on the ``task`` auxiliary backend. The sidecar's table never holds an owner session, so on the
shared gateway every Desktop one-shot fell through to the task backend. The owner answers it here:
the resident agent's route, else the session's own frozen launch route.

The model call never runs on the connection's serial dispatch: like the sidecar's long-handler
pool, the reply is written by a detached task, so a slow commit message does not hold up the next
request on that socket. Nor does it run on the loop's default executor, which session resume,
socket setup and disconnect cleanup share across every connection and profile: each owner has its
own small pool, and a call past its bound is refused by name instead of queueing without limit.
N)ÚFuture)Úcopy_context)Úpartial)Ú	authorize)ÚRuntimeStoreErrorc                 ó&   € R \        \        V 4      /# )zllm.oneshot)r   Úoneshot)Ú
connections   &Ú@/home/ubuntu/.hermes/hermes-agent/gateway/session_llm_oneshot.pyÚhandlersr   $   s   € Øœ7¤7¨JÓ7Ð8Ð8ó    c                   ó<   a € ] tR t^(t o RtR tR tR tR tRt	V t
R# )Ú_Poola}  One owner's one-shot threads, its admitted calls, and the answer task of each per connection.

Its own FIFO, not a ThreadPoolExecutor: an executor keeps a cancelled queued call (its params and
copied context) until a worker dequeues it, so with every worker parked on the model a reconnect
loop would pile up payloads past the bound. Here a cancelled call leaves the queue at once.c                óÄ   € \         P                  ! 4       V n        \        4       V n        / V n        ^ V n        / V n        RV n        \        P                  ! 4       V n        R# )é    FN)Ú	threadingÚLockÚlockÚsetÚcallsÚqueueÚthreadsÚanswersÚclosedÚasyncioÚEventÚstopping)Úselfs   &r
   Ú__init__Ú_Pool.__init__/   sB   € Ü—N’NÓ$ˆŒ	Ü“UˆŒ
ØˆŒ
ØˆŒØˆŒØˆŒÜŸš›ˆŽr   c                óv  € \        4       pV P                  ;_uu_ 4        V P                  '       d   \        R4      h\	        V P
                  4      \        8¼  d   \        R\         R2R7      hV P
                  P                  V4       WP                  V&   V P                  \        8  pV ;P                  V,          un	        RRR4       VP                  V P                  4       X'       d,   ^ RIHp  V! V P                  RR7      P!                  4        V# V#   + '       g   i     L`; i  \"         d    T P                  ;_uu_ 4        T ;P                  ^,          un	        T P                  '       * pRRR4       M  + '       g   i     M; iX'       d   TP%                  4       '       d   h  T# i ; i)	z\Admit ``work`` (or refuse it: ``runtime_draining``, ``oneshot_busy``) and return its Future.Úruntime_drainingÚoneshot_busyz' one-shot generations already in flight)ÚdetailN)Úspawn_context_threadzhermes-oneshot)Úname)r   r   r   r   Úlenr   Ú	_ADMITTEDÚaddr   r   Ú_WORKERSÚadd_done_callbackÚreleaseÚagent.memory_providerr$   ÚdrainÚstartÚRuntimeErrorÚcancel)r   ÚworkÚcallÚspawnr$   Úorphaneds   &&    r
   ÚsubmitÚ_Pool.submit8   s,  € ä‹xˆØ�Y�Y‹YØ�{�{ˆ{Ü'Ð(:Ó;Ð;Ü�4—:‘:‹¤)Ô+Ü'¨Ä)ÀÐLsÐ?tÔuÐuØ�J‰J�N‰N˜4Ô Ø#�J‰J�tÑØ—L‘L¤8Ñ+ˆEØ�LŠL˜EÕ!�L÷ ð 	×Ñ˜tŸ|™|Ô,ßåBð	Ù$ T§Z¡ZÐ6FÔG×MÑMÔOð ˆˆtˆ÷/ �Yûô  ô ð —Y—Y“YØ—L’L AÕ%•LØ#'§<¡<Ô/�H÷ —Y—Y�Yú÷  §¡§¢ØøØˆðúsB   ŸB%DÃ6"D/ ÄD,	Ä/F8Å+FÅ9	F8ÆFÆF8ÆF8Æ3F8Æ7F8c                óÖ   € V P                   ;_uu_ 4        V P                  P                  V4       V P                  P	                  VR4       RRR4       R#   + '       g   i     R# ; i)zVThe call finished, or was cancelled while queued: its slot and its queued work go now.N)r   r   Údiscardr   Úpop)r   r2   s   &&r
   r+   Ú_Pool.releaseT   s;   € à�Y�Y‹YØ�J‰J×Ñ˜tÔ$Ø�J‰J�N‰N˜4 Ô&÷ �Y�YŠYús   •8AÁA(	c                óö  €  V P                   ;_uu_ 4        V P                  '       g%   V ;P                  ^,          un         RRR4       R# \        \	        V P                  4      4      pV P                  P                  V4      pRRR4       XP                  4       '       d    VP                  X! 4       4       ??K¾    + '       g   i     LA; i  \         d   pTP                  T4        Rp?L8Rp?ii ; i)TN)
r   r   r   ÚnextÚiterr9   Úset_running_or_notify_cancelÚ
set_resultÚBaseExceptionÚset_exception)r   r2   r1   Úexcs   &   r
   r-   Ú_Pool.drainZ   s°   € ØØ——“Ø—z—z�zØ—L’L AÕ%•LØ÷ ‘ô œD §¡Ó,Ó-�Ø—z‘z—~‘~ dÓ+�÷ ð ×0Ñ0×2Ò2ð,Ø—O‘O¡D£FÔ+ð ’d÷ —ûô %ô ,Ø×&Ñ& s×+Ñ+ûð,ús)   –-B?Á9B?Â%C Â?C	ÃC8ÃC3Ã3C8)r   r   r   r   r   r   r   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r5   r+   r-   Ú__static_attributes__Ú__classdictcell__©Ú__classdict__s   @r
   r   r   (   s$   ø‡ € ñcò(òò8'÷ð r   r   c                ó$   € V ^8„  d   QhR\         /# )é   Úreturn)r   )Úformats   "r
   Ú__annotate__rQ   j   s   € ÷ ñ œñ r   c                 ól   € \        V R R4      p\        V\        4      '       g   \        4       ;qn        V# )Ú_oneshot_poolN)ÚgetattrÚ
isinstancer   rS   )Ú	authorityÚpools   & r
   Ú_poolrX   j   s/   € Ü�9˜o¨tÓ4€DÜ�dœE×"Ò"Ü).«Ð0ˆÔ&Ø€Kr   c                óê   € \        V P                  RR4      p\        V\        4      '       d$   \	        VP
                  P                  4       4      MR F  w  r#W0J g   K  VP                  4        K  	  R# )zµThe socket closed: drop its queued calls and answers. A call already running in its thread
cannot be interrupted; it finishes, keeps its slot until then, and its reply is discarded.rS   N© )rT   rV   rU   r   Úlistr   Úitemsr0   )r	   rW   ÚtaskÚowners   &   r
   Úforgetr_   q   sX   € ô �:×'Ñ'¨¸$Ó?€DÜ5?ÀÄe×5LÒ5L”t˜DŸL™L×.Ñ.Ó0Ô1ÐRTÒT‰ˆØÕØ�K‰KŽMó Ur   c                ó  € \        V 4      pVP                  ;_uu_ 4        RVn        \        VP                  4      pRRR4       VP
                  P                  4        X F  pVP                  4        K  	  R#   + '       g   i     LE; i)a•  Owner shutdown (whole runtime or one profile's unserve), on the loop: refuse new calls, cancel
every queued call, and answer each outstanding request once with ``runtime_draining``. One
profile's unserve leaves the host socket open for the others, so unlike ``forget`` its requests
still get a reply. Running calls finish on their own (no SessionDB writes, only the model reply)
and that reply is dropped.TN)rX   r   r   r[   r   r   r   r0   )rV   rW   Úqueuedr2   s   &   r
   Ústoprb   z   s^   € ô �Ó€DØ	��‹ØˆŒÜ�d—j‘jÓ!ˆ÷ 
ð 	‡M�M×ÑÔÛˆØ�‰Žó ÷	 
�ús    A:Á:B
	c                   ó4   a € ] tR t^‰t o RtR tRR ltRtV tR# )ÚDeferredReplyz[A handler result answered later: ``dispatch`` hands over the request id and sends no frame.c                ó   € Wn         R # ©N©r1   )r   r1   s   &&r
   r   ÚDeferredReply.__init__Œ   s   € ØŽ	r   Nc           	     óf  aa€ \        VP                  4      oSP                  \        \	        4       P
                  V P                  4      4      o\        P                  ! 4       P                  \        SVP                  VSV4      4      pVSP                  V&   VV3R lpVP                  V4       R# )z¥Admit the call (or refuse it: ``oneshot_busy``, ``runtime_draining``) and answer it later;
``check`` is the contract's result check, run before the reply is written.c                 ó�   <€ SP                   P                  V R 4       V P                  4       '       d   SP                  4        R # R # rf   )r   r9   Ú	cancelledr0   )Údoner2   rW   s   &€€r
   ÚsettledÚ$DeferredReply.start.<locals>.settled˜   s2   ø€ Ø�L‰L×Ñ˜T 4Ô(Ø�~‰~×ÒØ—‘–ñ  r   N)rX   rV   r5   r   r   Úrunr1   r   Úget_running_loopÚcreate_taskÚ_answerÚ	transportr   r*   )r   r	   ÚridÚcheckr]   rm   r2   rW   s   &&&&  @@r
   r.   ÚDeferredReply.start�   sˆ   ù€ ô �Z×)Ñ)Ó*ˆà�{‰{œ7¤<£>×#5Ñ#5°t·y±yÓAÓBˆÜ×'Ò'Ó)×5Ñ5´g¸dÀJ×DXÑDXÐZ]Ð_cÐejÓ6kÓlˆØ'ˆ�‰�TÑö	ð 	×Ñ˜wÖ'r   rg   rf   )	rD   rE   rF   rG   rH   r   r.   rI   rJ   rK   s   @r
   rd   rd   ‰   s   ø‡ € Ùeò÷(ò (r   rd   c              ƒ  óÊ  "  € \         P                  ! V4      p\         P                  ! V P                  P	                  4       4      p \         P                  ! WV3\         P
                  R7      G Rj  x€L
   TP                  4        T P                  P                  4       '       g-   TP                  4       '       d   TP                  4       '       dN   TP                  4       '       g'   TP                  4       '       g   TP                  4        RRRTRRRR	R
RRR
///pM% TP                  4       pTe	   T! T4       RRRTRT/pTP!                  T4      G Rj  x€L
  R#  Lî  \         P                   d    TP                  4        h i ; i  TP                  4        i ; i  \         d2   p	\        P                  RT	RR7       RRRTRRRR	RT	 2//p Rp	?	L“Rp	?	ii ; i L†5i)zºWrite the request's one reply: the call's result or failure, or ``runtime_draining`` once the
owner stops first. Its socket closing cancels this task instead, and then no frame goes out.)Úreturn_whenNÚjsonrpcz2.0ÚidÚerrorÚcodei¡  Úmessager!   ÚdataÚreasonÚresultzllm.oneshot failed: %sT)Úexc_infoi¦  zone-shot generation failed: )r   Úwrap_futureÚensure_futurer   ÚwaitÚFIRST_COMPLETEDÚCancelledErrorr0   Úis_setrl   rk   Ú	exceptionr€   Ú	ExceptionÚloggerÚwarningÚwrite_async)
rW   rs   rt   r2   ru   Úreplyr   Úframer€   rB   s
   &&&&&     r
   rr   rr   Ÿ   s©  é € ô ×Ò Ó%€EÜ×$Ò$ T§]¡]×%7Ñ%7Ó%9Ó:€HðÜ�lŠl˜EÐ,¼'×:QÑ:QÔR×RÑRð
 	�‰ÔØ‡}�}×Ñ×Ò U§Z¡Z§\¢\°U·_±_×5FÒ5Fð �|‰|�~Š~ e§o¡o×&7Ò&7Ø�O‰OÔØ˜E 4¨¨gØ�D˜)Ð%7¸À(ÐL^ÐA_ð8að b‰ð		_Ø—\‘\“^ˆFØÒ Ù�f”Ø  t¨S°(¸FÐCˆEð ×
Ñ
 Ó
&×&Ò&ñ1 	SøÜ×!Ñ!ô Ø�‰ŒØðûð 	�‰Õûô ô 	_ä�N‰NÐ3°SÀ4ˆNÔHØ  t¨SØ˜v t¨YÐ:VÐWZÐV[Ð8\Ð]ð_�Eûð	_úñ
 'ùs�   ‚AG#Á*E# Á2E!Á3E# Á8/G#Â(G#Â>G#ÃG#Ã*G#Ä !G#Ä"$F" ÅG#ÅG!ÅG#Å!E# Å#'F
Æ
F ÆFÆG#Æ"GÆ-'GÇG#ÇGÇG#c                 óZ  a € ^ RI Hp \        S 4      \        ,
          '       gÀ   \        ;QJ d'    V 3R l. \
        ORN 4       F  '       g   K   RM	  RM! V 3R l. \
        ORN 4       4      '       gp   \        S P                  R4      ;'       g    / \        4      '       dA   \        ;QJ d    V 3R lR 4       F  '       g   K   RM	  RM! V 3R lR 4       4      '       d   \        R4      hS P                  R	4      ;'       g    R
P                  4       pV'       d   W!9  d   \        RR	RR7      hV'       gq   S P                  R4      ;'       g    R
P                  4       '       g@   S P                  R4      ;'       g    R
P                  4       '       g   \        RRRR7      hR# R# R# )r   )ÚPROMPT_TEMPLATESc              3   óœ   <"  € T FA  pVS9   ;'       d0    SV,          R J;'       d    \        SV,          \        4      '       * x € KC  	  R # 5irf   )rU   Ústr©Ú.0ÚkeyÚparamss   & €r
   Ú	<genexpr>Ú_validate.<locals>.<genexpr>Â   sL   øé € ð %HÙ0F¨ð &)¨F¡]×%sÐ%s°v¸cµ{È$Ð7N×%sÐ%sÔWaÐbhÐilÕbmÔorÓWsÔSsÔ%sÛ0Fùs   ƒA”A§%AÚ
session_idTFÚ	variablesc              3   óæ   <"  € T Ff  pVS9   ;'       dU    SV,          R J;'       dB    \        SV,          \        4      ;'       g#    \        SV,          \        \        34      '       * x € Kh  	  R # 5irf   )rU   ÚboolÚintÚfloatr“   s   & €r
   r—   r˜   Å   sp   øé € ð =á;�sð ˜&‘=÷ dð d V¨C¥[¸Ð%<÷ dð dÜ" 6¨#¥;´Ó5×bÐb¼ZÈÈsÍÔVYÔ[`ÐUaÓ=bÔ9bôdã;ùs   ƒA1”A1§A1Á+A1Úinvalid_paramsÚtemplateÚ zunknown one-shot template)Úfieldr#   ÚinstructionsÚinputz&needs a template or instructions/inputN)ÚtemperatureÚ
max_tokens)Úagent.oneshotr�   r   Ú_FIELDSÚanyÚ_TEXTrU   ÚgetÚdictr   Ústrip)r–   r�   r    s   f  r
   Ú	_validater®   À   sS  ø€ Ý.ÜˆF‹”g×Ô§£ô %HØ0F´%Ð0F¸Ñ0Fó%H§§¢ô %HØ0F´%Ð0F¸Ñ0Fó%H÷ "Hò "Hä˜fŸj™j¨Ó5×;Ð;¸¼T×BÒBß‹sô =á;ó=�s�sŠsô =á;ó=÷ =ò =ô  Ð 0Ó1Ð1Ø—
‘
˜:Ó&×,Ð,¨"×3Ñ3Ó5€Hß�HÔ4ÜÐ 0¸
ÐKfÔgÐgß˜VŸZ™Z¨Ó7×=Ð=¸2×DÑD×FÒFÐPV×PZÑPZÐ[bÓPc×PiÐPiÐgi×OpÑOp×OrÒOrÜÐ 0¸ÐHpÔqÐqñ PsÑF‰8r   c                óö   a aa€ ^ RI Hp S P                  V4      pVe   V! V4      oV3R l# ^ RIHp V! S P
                  S P                  VP                  ,          P                  4      oSf   R # V V3R lpV# )zä``main_runtime`` for the auxiliary client: the resident agent's live route (main's sidecar),
snapshotted here on the loop, else the session's own frozen launch route as its next turn
resolves it (read from config in the worker).)Ú_main_runtime_from_agentc                  ó   <€ S # rf   rZ   )Úruntimes   €r
   Ú<lambda>Ú"_session_runtime.<locals>.<lambda>Ø   s   ø€ ‘wr   )Úpolicy_for_sourcec                  ó   € R # rf   rZ   rZ   r   r
   r³   r´   Ü   s   € ‘tr   c                  óà   <€ ^ RI Hp  V ! SP                  SRR7      w  rR Uu/ uF$  q2P                  V4      '       g   K  W2V,          bK&  	  ppV'       d	   / VCRV/C# T;'       g    R# u upi )r   )Ú_resolve_policy_agent_runtimeF)ÚfallbackÚmodelN)ÚproviderÚbase_urlÚapi_keyÚapi_mode)Úgateway.run_turn_preparer¸   Úrunnerr«   )r¸   rº   Úkwargsr•   r²   rV   Úpolicys        €€r
   ÚresolveÚ!_session_runtime.<locals>.resolveÞ   sq   ø€ ÝJñ 6°i×6FÑ6FÈÐY^Ô_‰ˆÙ/^ÓrÑ/^¨×blÑblÐmp×bqÔ#�3˜s�Ò#Ñ/^ˆÐrß.3Ð*�'Ð*˜7 EÑ*ÐH¸¿¸ÀDÐHùò ss   £A+Á A+)	Útui_gateway.session_registryr°   ÚagentÚgateway.session_policyrµ   rÀ   Úsessionsr™   Úsource)rV   Úrefr°   rÆ   rµ   rÃ   rÂ   r²   s   f&    @@r
   Ú_session_runtimerË   Ð   so   ú€ õ FØ�O‰O˜CÓ €EØÒÙ*¨5Ó1ˆÜÐÝ8Ù˜y×/Ñ/°×1CÑ1CÀCÇNÁNÕ1S×1ZÑ1ZÓ[€FØ‚~ÙÐöIð €Nr   c              ƒ   óÒ  aaa"  € \        S4       V P                  oVP                  '       d   \        WSR 4       \	        SV4      oM‹^ RIHp V! S4       R V P                  P                  9  d   \        R4      hSP                  R4      '       d>   ^ RIHp ^ RIHp V! SR,          V! SP                  4      4      '       g   \        R4      hR oVVV3R lp\!        V4      # 5i)	zsession:read)Úrequire_servingÚpermission_deniedÚprofile)ÚPath)Úprofile_matches_homeÚprofile_mismatchc                  ó   € R # rf   rZ   rZ   r   r
   r³   Úoneshot.<locals>.<lambda>ù   s   € ™$r   c                  ó�  <€ ^ RI Hp  ^ RIHp V! S4      ;_uu_ 4        RT ! SP	                  R4      ;'       g    RSP	                  R4      ;'       g    RSP	                  R4      ;'       g    RP                  4       ;'       g    RSP	                  R	4      ;'       g    / SP	                  R
4      ;'       g    RP                  4       ;'       g    R\        SP	                  R4      ;'       g    R4      SP	                  R4      f   RM\        SR,          4      S! 4       R7      /uuRRR4       #   + '       g   i     R# ; i)r   )Úrun_oneshot)Úowner_scopeÚtextr£   r¡   r¤   r    Nrš   r]   Útitle_generationr¦   i   r¥   g333333Ó?)r£   Ú
user_inputr    rš   r]   r¦   r¥   Úmain_runtime)r§   rÖ   Úgateway.session_authoritiesr×   r«   r­   r�   rž   )rÖ   r×   rV   r–   r²   s     €€€r
   r1   Úoneshot.<locals>.workû   sû   ø€ Ý-Ý;Ù˜×#Õ#Ø™KØ#ŸZ™Z¨Ó7×=Ð=¸2È&Ï*É*ÐU\ÓJ]×JcÐJcÐacØ Ÿ*™* ZÓ0×6Ð6°B×=Ñ=Ó?×GÐGÀ4ÐSY×S]ÑS]Ð^iÓSj×SpÐSpÐnpØ—j‘j Ó(×.Ð.¨B×5Ñ5Ó7×MÐMÐ;MÜ˜vŸz™z¨,Ó7×?Ð?¸4Ó@Ø#)§:¡:¨mÓ#<Ò#D™CÌ%ÐPVÐWdÕPeÓJfÙ$›Yô(ð )÷ $×#×#Ó#ús5   ž5D4ÁD4Á.D4ÂD4Â D4Â:D4ÃD4Ã18D4Ä4E	)r®   rV   r™   r   rË   rÜ   rÍ   ÚactorÚcapabilitiesr   r«   ÚpathlibrÐ   Úhermes_cli.profilesrÑ   Ú
profile_idrd   )	r	   rÊ   r–   rÍ   rÐ   rÑ   r1   rV   r²   s	   &&f    @@r
   r   r   é   sº   úé € ÜˆfÔØ×$Ñ$€IØ
‡~‡~€~Ü�* 6¨>Ô:Ü" 9¨cÓ2‰å?Ù˜	Ô"Ø ×!1Ñ!1×!>Ñ!>Ô>Ü#Ð$7Ó8Ð8Ø�:‰:�i× Ò Ý$Ý@Ù'¨¨yÕ(9¹4À	×@TÑ@TÓ;U×VÒVÜ'Ð(:Ó;Ð;Ùˆ÷
)ô ˜ÓÐùs   …BC'Â1C'Ã!C'>	   r]   r¤   rÏ   r    rš   r¦   r™   r¥   r£   )r    r£   r¤   r]   ) rH   r   Úconcurrent.futuresr   Úcontextvarsr   Ú	functoolsr   Úloggingr   Úgateway.session_busy_controlsr   Úhermes_state_runtimer   Ú	getLoggerrD   rŠ   Ú	frozensetr¨   rª   r)   r'   r   r   rX   r_   rb   rd   rr   r®   rË   r   rZ   r   r
   Ú<module>rë      s“   ðñó Ý %Ý $Ý Û Û å 3Ý 2à	×	Ò	˜8Ó	$€á
ò 2ó 3€à5€ð €Ø€	ò9÷?ñ ?õDòò÷(ñ (ò,'òBrò ô2r   