+
    iՎ              $       >   R t ^ RIt^ RIt]P                  ! ]4      t^ RIt^ RIt^ RIH	t	H
t
 ^ RIHtHtHtHt ]! . R<O4      t^t^t^2t. R=OtR R ltR>R R lltR	 R
 ltR?R R lltR@R R lltRAR R lltRBR R lltR R ltR R lt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)/R#RR$R%RR/RR*/R+RRRR,/R-RR$R%RR/RR.//R/R./R0^RR1/R2RR3RR4/R+RRRR5/R-RR$R%RR/RR6//R/. //t^ R7I H!t! ]!PD                  ! RR8]R9 ]R:R;7       R# )Ca  
Delegate Tool -- Subagent Architecture

Spawns child AIAgent instances with isolated context, restricted toolsets,
and their own terminal sessions. Supports single-task and batch (parallel)
modes. The parent blocks until all children complete.

Each child gets:
  - A fresh conversation (no parent history)
  - Its own task_id (own terminal session, file ops cache)
  - A restricted toolset (configurable, with blocked tools always stripped)
  - A focused system prompt built from the delegated goal + context

The parent's context only sees the delegation call and the summary result,
never the child's intermediate tool calls or reasoning.
N)ThreadPoolExecutoras_completed)AnyDictListOptionaldelegate_taskc                $    V ^8  d   QhR\         /#    return)bool)formats   "0/home/ubuntu/hermes-agent/tools/delegate_tool.py__annotate__r   +   s      T     c                     R# )z<Delegation has no external requirements -- always available.T r   r   r   check_delegate_requirementsr   +   s    r   c                R    V ^8  d   QhR\         R\        \         ,          R\         /# )r   goalcontextr   )strr   )r   s   "r   r   r   0   s%      S 8C= C r   c                    RRRV  2.pV'       d+   VP                  4       '       d   VP                  RV 24       VP                  R4       RP                  V4      # )z0Build a focused system prompt for a child agent.z@You are a focused subagent working on a specific delegated task. zYOUR TASK:
z

CONTEXT:
a.  
Complete this task using the tools available to you. When finished, provide a clear, concise summary of:
- What you did
- What you found or accomplished
- Any files you created or modified
- Any issues encountered

Be thorough but concise -- your response is returned to the parent agent as a summary.
)stripappendjoin)r   r   partss   && r   _build_child_system_promptr    0   s`     	K

tfE
 7==??|G9-.	LL	%	 99Ur   c                \    V ^8  d   QhR\         \        ,          R\         \        ,          /# )r   toolsetsr   )r   r   )r   s   "r   r   r   F   s&     C C49 Cc Cr   c                H    0 RmpV  Uu. uF  q"V9  g   K  VNK  	  up# u upi )z0Remove toolsets that contain only blocked tools.>   memoryclarify
delegationcode_executionr   )r"   blocked_toolset_namests   &  r   _strip_blocked_toolsr*   F   s,      Bx!,A#AAAxBBBs   c                R    V ^8  d   QhR\         R\         R\        \        ,          /# )r   
task_index
task_countr   )intr   callable)r   s   "r   r   r   N   s-     M Ms Mc MZbckZl Mr   c                   aaaaa	 \        VRR4      o	\        VRR4      oS	'       g   S'       g   R# V^8  d   RV ^,            R2MRo^o. oR
R VVVVV	3R lllpVVV3R	 lpWCn        V# )a  Build a callback that relays child agent tool calls to the parent display.

Two display paths:
  CLI:     prints tree-view lines above the parent's delegation spinner
  Gateway: batches tool names and relays to parent's progress callback

Returns None if no display mechanism is available, in which case the
child agent runs with no progress callback (identical to current behavior).
_delegate_spinnerNtool_progress_callback[] r   c                <    V ^8  d   QhR\         R\         R\         /# )r   
event_type	tool_namepreviewr   )r   s   "r   r   4_build_child_progress_callback.<locals>.__annotate__e   s!     ) )c )c )3 )r   c                   < V R9   d^   T;'       g    T;'       g    RpS'       d>   \        V4      ^78  d   VR,          R,           MTp SP                  RS RV R24       R# R# V R	8X  d   R# S'       d   V'       d!   \        V4      ^#8  d   VR
,          R,           MT;'       g    Rp^ RIHp T! T;'       g    R4      p	RS RV	 RV 2p
V'       d   V
RV R2,          p
 SP                  V
4       S'       d`   SP                  T;'       g    R4       \        S4      S8  d3   RP                  S4      p S! RRS V 24       SP                  4        R# R# R#   \         d"   p\        P	                  RT4        Rp?R# Rp?ii ; i  \         d!   p\        P	                  RT4        Rp?LRp?ii ; i  \         d!   p\        P	                  RT4        Rp?LRp?ii ; i)	_thinkingr   :N7   Nz... u   ├─ 💭 ""zSpinner print_above failed: %sNztool.completed:N#   N)get_tool_emojiu   ├─ z  ", subagent_progress   🔀 zParent callback failed: %s)r<   zreasoning.available)
lenprint_above	Exceptionloggerdebugagent.displayrA   r   r   clear)r6   r7   r8   argskwargstextshorterA   emojilinesummary_BATCH_SIZE_batch	parent_cbprefixspinners   &&&&,       r   	_callback1_build_child_progress_callback.<locals>._callbacke   s   
 ==--i--2D/24y2~cU*4F''!F8>%(KL  )) .5#g,:KWS\E)RYR_R_]_E4"9??3EvhgeWAi[9D$ugR((B##D) MM)//r*6{k)))F+B1U6(7)3LM  * - ! FLL!A1EEF&  B=qAAB ! BLL!=qAABsH   
E $F :F; F
)FF
F8F33F8;G&G!!G&c                    < S'       d=   S'       d3   RP                  S4      p  S! RRS V  24       SP	                  4        R# R# R#   \         d!   p\        P                  RT4        Rp?L<Rp?ii ; i)z<Flush remaining batched tool names to gateway on completion.rB   rC   rD   z Parent callback flush failed: %sN)r   rG   rH   rI   rK   )rS   rP   rU   rV   rW   s     r   _flush._build_child_progress_callback.<locals>._flush   sj    ii'GD-vhwi/HI LLN  9  D?CCDs   A A3A..A3)NNN)getattrr\   )
r,   parent_agentr-   rY   r\   rT   rU   rV   rW   rX   s
   &&&  @@@@@r   _build_child_progress_callbackr`   N   sr     l$7>G&>EI9 (2A~qa #2F KF) )V r   c                   V ^8  d   QhR\         R\        R\        \        ,          R\        \        \        ,          ,          R\        \        ,          R\         R\        \        ,          R\        \        ,          R	\        \        ,          R
\        \        ,          R\        \        ,          R\        \        \        ,          ,          /# )r   r,   r   r   r"   modelmax_iterationsoverride_provideroverride_base_urloverride_api_keyoverride_api_modeoverride_acp_commandoverride_acp_args)r.   r   r   r   )r   s   "r   r   r      s     E EE
E c]E tCy!	E
 C=E E  }E  }E smE  }E #3-E  S	*Er   c           
       a" ^ RI Hp \        VRR4      pVe   \        V4      pM`V'       dJ   \	        VR4      '       d8   ^ RIpVP                   Uu0 uF  pVP                  V4      ;pf   K  VkK  	  ppM\        \        4      pV'       d&   \        V Uu. uF  pVV9   g   K  VNK  	  up4      pMDV'       d   Ve   \        V4      pM,V'       d   \        \        V4      4      pM\        \        4      p\        W4      p\        VRR4      pV'       g.   \	        VR4      '       d   VP                  P                  R4      p\        W4      o"RpS"'       d   R V"3R llpTpT;'       g    VP                  pT;'       g    \        VR	R4      pT;'       g    VP                   pT	;'       g    TpT
;'       g    \        VR
R4      pT;'       g    \        VRR4      p\#        Ve   TM\        VR. 4      ;'       g    . 4      pV! 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RR4      bR\        VRR4      bR\        VRR4      bRVbRRbRVbRRV  R2bRVP$                  bRRbRRbRRbRVbR\        VRR4      bR \        VR!R4      bR"VP&                  bR#VP(                  bR$VP*                  bR%VP,                  bR&S"bR'Rb p \        VR(R4      V n        \        VR)^ 4      ^,           V n        \	        VR*4      '       da   \        VR+R4      p!V!'       d1   V!;_uu_ 4        VP2                  P5                  V 4       RRR4       V # VP2                  P5                  V 4       V # u upi u upi   + '       g   i     T # ; i)-a  
Build a child AIAgent on the main thread (thread-safe construction).
Returns the constructed child agent without running it.

When override_* params are set (from delegation config), the child uses
those credentials instead of inheriting from the parent.  This enables
routing subagents to a different provider:model pair (e.g. cheap/fast
model on OpenRouter while the parent runs on Nous Portal).
)AIAgentenabled_toolsetsNvalid_tool_namesapi_key_client_kwargsc                (    V ^8  d   QhR\         RR/# )r   rN   r   Nr9   )r   s   "r   r   (_build_child_agent.<locals>.__annotate__   s     	L 	L# 	L$ 	Lr   c                    < V '       g   R #  S! RV 4       R #   \          d"   p\        P                  RT4        R p?R # R p?ii ; i)Nr<   z(Child thinking callback relay failed: %s)rG   rH   rI   )rN   rP   child_progress_cbs   & r   _child_thinking+_build_child_agent.<locals>._child_thinking   s=    L!+t4 LGKKLs   	 A?Aproviderapi_modeacp_commandacp_argsbase_urlrb   rc   
max_tokensreasoning_configprefill_messages
quiet_modeTephemeral_system_prompt
log_prefix
[subagent-]platformskip_context_filesskip_memoryclarify_callbackthinking_callback
session_db_session_dbparent_session_id
session_idproviders_allowedproviders_ignoredproviders_orderprovider_sortr2   iteration_budget	_print_fn_delegate_depth_active_children_active_children_lockr   )	run_agentrk   r^   sethasattrmodel_toolsrm   get_toolset_for_toolDEFAULT_TOOLSETSr*   sortedr    ro   getr`   rb   rz   listr   r   r   r   r   r   r   r   r   )#r,   r   r   r"   rb   rc   r_   rd   re   rf   rg   rh   ri   rk   parent_enabledparent_toolsetsr   nametsr)   child_toolsetschild_promptparent_api_keychild_thinking_cbrt   effective_modeleffective_providereffective_base_urleffective_api_keyeffective_api_modeeffective_acp_commandeffective_acp_argschildlockrs   s#   &&&&&&&&&&&&&                     @r   _build_child_agentr      s   4 " \+=tDN!n-	',0BCC'88
84!66t<< B8 	 

 ./-(.[(Qa?FZqq(.[\	.4-n=	-f_.EF-.>?-d<L\9d;N6F G G%4488C 7zP 	L 	L , 11|11O*UUglJPT.U*CCl.C.C(::N*UUglJPT.U0^^GL-Y]4^3D3P/W^_kmwy{W|  XC  XC  AC  E #!  $	
 $ * $ & <t< !/A4H !/A4H (  !-  
|1-  &&!"  #$ %& '( ,)* <=+, ",dC-. '88/0 '8812 %4434 #0056  178 9E< lK>EO#L2CQG!KE |/00|%<dC--44U; 
 L ))007L
 /\b 
 Ls$   M,-M,	M1$M1)M66N	c                ^    V ^8  d   QhR\         R\        R\        \        \        3,          /# )r   r,   r   r   )r.   r   r   r   )r   s   "r   r   r   %  s8     VS VSVS
VS 
#s(^VSr   c                `   \         P                  ! 4       p\        VRR4      p^ RIp\        VR\	        VP
                  4      4      p VP                  VR7      p	V'       d$   \        VR4      '       d    VP                  4        \        \         P                  ! 4       V,
          ^4      pV	P                  R4      ;'       g    RpV	P                  R	R
4      pV	P                  RR
4      pV	P                  R^ 4      pV'       d   RpMV'       d   R	pMRp. p/ pV	P                  R4      ;'       g    . p\        V\        4      '       Ed   V EF  p\        V\        4      '       g   K  VP                  R4      R8X  d   VP                  R4      ;'       g    .  Fu  pVP                  R/ 4      pRVP                  RR4      R\!        VP                  RR4      4      /pVP#                  V4       VP                  R4      pV'       g   Kp  VVV&   Kw  	  K  VP                  R4      R8X  g   K  VP                  RR4      p\%        T;'       d    RVR,          P'                  4       9   4      pR\!        V4      RV'       d   RMR/pVP                  R4      pV'       d   VP                  V4      MRpVe   VP)                  V4       EK|  V'       g   EK  VR2,          P)                  V4       EK  	  V'       d   RpMV'       d   R	pMR p\        VR!^ 4      p\        VR"^ 4      p\        VR#R4      p R$T RTR%TRTR&TR#\        V \*        4      '       d   T MRR'TR(R)\        V\,        \.        34      '       d   TM^ R*\        V\,        \.        34      '       d   TM^ /R+V/	p!VR8X  d   V	P                  RR,4      V!R&   T!^ RIp\        VRR4      p"\        V"\        4      '       d   \	        V"4      Vn        \        VR-4      '       db    \        VR.R4      p#V#'       d0   V#;_uu_ 4        VP0                  P3                  V4       RRR4       # VP0                  P3                  V4       # #   \         d"   p
\        P                  RT
4        Rp
?
ELRp
?
ii ; i  + '       g   i     # ; i  \4        \6        3 d!   p
\        P                  R/T
4        Rp
?
# Rp
?
ii ; i  \         EdS   p$\        \         P                  ! 4       T,
          ^4      p\8        P:                  ! R0T  R124       R$T RRR%RR\+        T$4      R^ R&T/u Rp$?$^ RIp\        TRR4      p"\        T"\        4      '       d   \	        T"4      Tn        \        TR-4      '       d    \        TR.R4      p#T#'       dC   T#;_uu_ 4        TP0                  P3                  T4       RRR4       #   + '       g   i     # ; iTP0                  P3                  T4       #   \4        \6        3 d!   p
\        P                  R/T
4        Rp
?
# Rp
?
ii ; i# Rp$?$ii ; i  ^ RIp\        TRR4      p"\        T"\        4      '       d   \	        T"4      Tn        \        TR-4      '       d    \        TR.R4      p#T#'       dC   T#;_uu_ 4        TP0                  P3                  T4       RRR4       i   + '       g   i     i ; iTP0                  P3                  T4       i   \4        \6        3 d!   p
\        P                  R/T
4        Rp
?
i Rp
?
ii ; ii ; i)3z]
Run a pre-built child agent. Called from within a thread.
Returns a structured result dict.
r2   N_delegate_saved_tool_names)user_messager\   z"Progress callback flush failed: %sfinal_responser   	completedFinterrupted	api_callsfailedmessagesrole	assistant
tool_callsfunctiontoolr   unknown
args_bytes	argumentsidcontenterror:NP   Nresult_bytesstatusoktool_call_idrc   session_prompt_tokenssession_completion_tokensrb   r,   rS   duration_secondsexit_reasontokensinputoutput
tool_tracez$Subagent did not produce a response.r   r   z/Could not remove child from active_children: %sr   z] failed)time	monotonicr^   r   r   _last_resolved_tool_namesrun_conversationr   r\   rG   rH   rI   roundr   
isinstancedictrE   r   r   lowerupdater   r.   floatr   remove
ValueErrorUnboundLocalErrorlogging	exception)%r,   r   r   r_   _kwargschild_startrs   r   _saved_tool_namesresultrP   durationrS   r   r   r   r   r   trace_by_idr   msgtcfnentry_ttc_idr   is_errorresult_metatargetr   _input_tokens_output_tokens_modelentrysaved_tool_namesr   excs%   &&&&,                                r   _run_single_childr   %  sP    .."K  '?F 'C $[%J%J KM@S''T': ):H!E!EF!((* )K7;**-.44"JJ{E2	jj6JJ{A.	"F !FF ,.
13::j)//Rh%%!#t,,776?k1"ww|4:::VVJ3"BFF69$=(#bff[".E*F# #))'2 "t 518K.  ; WWV_.!ggi4G#CCGws|/A/A/C$C H 'G X'4#K
  GGN3E7<[__U3$F)k2#"2--k:=  B 'K%K*K  '>B (CQG. *fwz&#66VD;*]S%L*Q*QWXJ~U|,T,T.Z[ *!
 X#ZZ1WXE'N" 	"5*FM&--489I4JK1 <!344S|-DdK$55<<UC  !1188? 5a  FA1EEFh   12 SNPQRRS=  

)K7;Jzl(;<*gtSX
 	
 	"5*FM&--489I4JK1 <!344S|-DdK$55<<UC  !1188? 12 SNPQRRS 5-

 	"5*FM&--489I4JK1 <!344S|-DdK$55<<UC  !1188? 12 SNPQRRS 5so  ,S 5Q# =S A S 
S  S 0A%S A.S 	S ,S AS 0S $S 8
S B5S R%!R=R%R%#R.R
S 
RS R"R%"R%%S6SSX7%AX2;X7<X: 
W=)WW=WW=W==X.X))X.2X77X: :A
\-[8$[		 [8\-	[[8\-[87\-8\)	\$\-$\))\-c                v   V ^8  d   QhR\         \        ,          R\         \        ,          R\         \        \        ,          ,          R\         \        \        \        \        3,          ,          ,          R\         \
        ,          R\         \        ,          R\         \        \        ,          ,          R\        /# )	r   r   r   r"   tasksrc   rx   ry   r   )r   r   r   r   r   r.   )r   s   "r   r   r     s     u u
3-uc]u tCy!u Dc3h()	u
 SMu #u tCy!u 	ur   c                   Vf   \         P                  ! RR/4      # \        VR^ 4      pV\        8  d!   \         P                  ! RR\         R2/4      # \	        4       p	V	P                  R\        4      p
T;'       g    T
p \        W4      pT'       d!   \        T\        4      '       d   TR\         pMVT '       d7   \        T \        4      '       d!   T P                  4       '       d   RT R	TR
T/.pM\         P                  ! RR/4      # T'       g   \         P                  ! RR/4      # \        T4       FJ  w  ppTP                  RR4      P                  4       '       d   K.  \         P                  ! RRT R2/4      u # 	  \        P                   ! 4       p. p\#        T4      pT Uu. uF  pTR,          R,          NK  	  pp^ RIp\        TP&                  4      p. p \        T4       F  w  pp\)        TTR,          TP                  R	4      TP                  R
4      ;'       g    TTR,          YTR,          TR,          TR,          TR,          TP                  R4      ;'       g    TTP                  R4      ;'       g    TR7      pTTn        TP-                  TTT34       K  	  TTn        T^8X  d6   T^ ,          w  ppp\/        ^ TR,          TT4      pTP-                  T4       EM^ p\        TRR4      p\1        \        R7      ;_uu_ 4       p/ p T F-  w  pppTP3                  \.        TTR,          TTR7      p!TT T!&   K/  	  \5        T 4       EF  p! T!P7                  4       p"TP-                  T"4       T^,          pT"R,          p#T#\#        T4      8  d
   TT#,          MRT# 2p$T"P                  R ^ 4      p%T"P                  RR!4      p&T&R"8X  d   R#MR$p'TT,
          p(T' R%T#^,            R&T R'T$ R(T% R)2
p)T'       d    TP;                  T)4       M\=        R*T) 24       T'       g   K  T(^ 8  g   K   TP?                  R+T( R,T(^8w  d   R-MR R.24       EK  	  RRR4       TPE                  R0 R17       T'       d   \G        TR24      '       d   TPH                  '       d   T F  p" T"R,          \#        T4      8  d   TT"R,          ,          R,          MRp+TPH                  PK                  T+T"P                  RR4      ;'       g    RT"R,          \#        T4      8  d#   \        TT"R,          ,          ^,          R3R4      MRR47       K  	  \M        \        P                   ! 4       T,
          ^4      p,\         P                  ! R5TR6T,/R7R87      #   \         d,   p\         P                  ! R\        T4      /4      u Rp?# Rp?ii ; iu upi   TTn        i ; i  \8         d,   pT T!,          p#RT#RRRRR\        T4      R^ R ^ /p" Rp?ELRp?ii ; i  \8         d    \=        R*T) 24        ELi ; i  \8         d#   p*\@        PC                  R/T*4        Rp*?*EK  Rp*?*ii ; i  + '       g   i     EL; i  \8         d     EK  i ; i)9z
Spawn one or more child agents to handle delegated tasks.

Supports two modes:
  - Single: provide goal (+ optional context, toolsets)
  - Batch:  provide tasks array [{goal, context, toolsets}, ...]

Returns JSON with results array, one entry per task.
Nr   z.delegate_task requires a parent agent context.r   z Delegation depth limit reached (z,). Subagents cannot spawn further subagents.rc   r   r   r"   z7Provide either 'goal' (single task) or 'tasks' (batch).zNo tasks provided.r   zTask z is missing a 'goal'.:N(   Nrb   rv   rz   rn   rw   rx   ry   )r,   r   r   r"   rb   rc   r_   rd   re   rf   rg   rh   ri   r1   )max_workers)r,   r   r   r_   r,   r   rS   r   r   ?r   u   ✓u   ✗z [/r4   z  (zs)z  rD   z tasksz
 remainingzSpinner update_text failed: %sc                     V R ,          # )r,   r   )rs   &r   <lambda>delegate_task.<locals>.<lambda>^  s    1\?r   )key_memory_managerr   )taskr   child_session_idresultstotal_duration_secondsF)ensure_ascii)'jsondumpsr^   	MAX_DEPTH_load_configr   DEFAULT_MAX_ITERATIONS_resolve_delegation_credentialsr   r   r   r   MAX_CONCURRENT_CHILDRENr   	enumerater   r   rE   r   r   r   r   r   r   r   submitr   r   rG   rF   printupdate_textrH   rI   sortr   r  on_delegationr   )-r   r   r"   r   rc   rx   ry   r_   depthcfgdefault_max_itereffective_max_itercredsr   	task_listir  overall_startr  n_tasksr)   task_labels_model_tools_parent_tool_nameschildrenr   _i_tr   completed_countspinner_refexecutorfuturesfuturer   idxlabeldurr   icon	remainingcompletion_linerP   
_task_goaltotal_durations-   &&&&&&&&                                     r   r   r     sO   & zz7$TUVV L"3Q7E	zz29+ >< <
  	 .Cww/1GH';;+;//B
 E4((223		*T3''DJJLLdIw
HMN	zz7$]^__zz7$89:: Y'4xx#))++::w%s2G(HIJJ ( NN$MG)nG+459a1V9S>>9K5
 'lDDE
 HDi(DAq&1V9aeeI6Fz*66heGn1"'
"3uZGX!&y!1"'
"3%&UU=%9%H%H["#%%
"3"?"?x	E 0BE,OOQ5M* )  2D.!| B"1bj%Fv l,?F,CDDG'1e!% 6!- )  #$  ( 'w/"MMOE u%1$ L),/#k2B,BC(%PSuii 2A68S1 &+ 5u5#o5	%)F"SUG1WIRwc#b"Q6#//@ B/01 ;9q=J#//%	{%y\]~ceGffp0qrG 0 Eh 	23 .?@@\EaEaEaEGL\GZ]`aj]kGkYu\':;FCqs
,,::# 99Y399rdijvdwz}  G  {H  eHWXeL6I-J1-M|]_%`  NP ;   4>>+m;Q?N::7 .  M  /zz7CH-../0 6: 2D.6 ! 	!&/C$c '!4S#Q*AE	0 % 6?"3456 % J%EqIIJa EDD@  s   V *W!AW (AW +W 'W AY'W,BY'XY'0Y':!X7Y'-AY;AY;W
 W?W
W
	WX	) X		Y'X	Y'X4	0Y'3X4	4Y'7Y$	Y	Y'Y$	$Y''Y8	;Z
Zc                0    V ^8  d   QhR\         R\         /# )r   r  r   r   )r   s   "r   r   r   u  s     V V V Vr   c                   \        V P                  R4      ;'       g    R4      P                  4       ;'       g    Rp\        V P                  R4      ;'       g    R4      P                  4       ;'       g    Rp\        V P                  R4      ;'       g    R4      P                  4       ;'       g    Rp\        V P                  R4      ;'       g    R4      P                  4       ;'       g    RpV'       dz   T;'       g&    \        P                  ! RR4      P                  4       pV'       g   \        R4      hVP                  4       pR	pR
p	RV9   d   RpRp	MRV9   d   RpRp	RVRVRVRVRV	/# V'       g   RVRRRRRRRR/#  ^ RIHp
 V
! VR7      pTP                  RR4      pT'       g   \        RT R24      hRTRTP                  R4      RTP                  R4      RTRTP                  R4      RTP                  R4      R\        TP                  R4      ;'       g    . 4      /#   \         d   p\        RT RT R24      ThRp?ii ; i)ul  Resolve credentials for subagent delegation.

If ``delegation.base_url`` is configured, subagents use that direct
OpenAI-compatible endpoint. Otherwise, if ``delegation.provider`` is
configured, the full credential bundle (base_url, api_key, api_mode,
provider) is resolved via the runtime provider system — the same path used
by CLI/gateway startup. This lets subagents run on a completely different
provider:model pair.

If neither base_url nor provider is configured, returns None values so the
child inherits everything from the parent agent.

Raises ValueError with a user-friendly message on credential failure.
rb   r   Nrv   rz   rn   OPENAI_API_KEYzeDelegation base_url is configured but no API key was found. Set delegation.api_key or OPENAI_API_KEY.customchat_completionszchatgpt.com/backend-api/codexzopenai-codexcodex_responseszapi.anthropic.com	anthropicanthropic_messagesrw   )resolve_runtime_provider)	requestedz$Cannot resolve delegation provider 'z': z. Check that the provider is configured (API key set, valid provider name), or set delegation.base_url/delegation.api_key for a direct endpoint. Available providers: openrouter, nous, zai, kimi-coding, minimax.zDelegation provider 'z]' resolved but has no API key. Set the appropriate environment variable or run 'hermes auth'.commandrL   )r   r   r   osgetenvr   r   hermes_cli.runtime_providerr<  rG   r   )r  r_   configured_modelconfigured_providerconfigured_base_urlconfigured_api_keyrn   
base_lowerrv   rw   r<  runtimer   s   &&           r   r  r  u  s    3777+11r288:BBdcggj177R8>>@HHDcggj177R8>>@HHDSWWY/5526<<>FF$ 7 7yy)2.446 	 < 
 )..0
%*j8%H(H J."H+H %+w
 	
  %t
 	
	H*5HI kk)R(G#$7#8 9M N
 	
 	!GKK
+GKK
+7GKK
+7;;y)W[[(..B/   23F2Gs3% PP Q

 	s   H- -I8IIc                $    V ^8  d   QhR\         /# r
   r4  )r   s   "r   r   r     s      d r   c                      ^ RI Hp  V P                  R/ 4      pV'       d   V#   ^ RIHp V! 4       pVP                  R/ 4      #   \         d     L-i ; i  \         d    / u # i ; i)a:  Load delegation config from CLI_CONFIG or persistent config.

Checks the runtime config (cli.py CLI_CONFIG) first, then falls back
to the persistent config (hermes_cli/config.py load_config()) so that
``delegation.model`` / ``delegation.provider`` are picked up regardless
of the entry point (CLI, gateway, cron).
)
CLI_CONFIGr&   )load_config)clirJ  r   rG   hermes_cli.configrK  )rJ  r  rK  fulls       r   r  r    sp    "nn\2.J 1}xxb))    	s'   A A A AAA&%A&r   descriptiona  Spawn one or more subagents to work on tasks in isolated contexts. Each subagent gets its own conversation, terminal session, and toolset. Only the final summary is returned -- intermediate tool results never enter your context window.

TWO MODES (one of 'goal' or 'tasks' is required):
1. Single task: provide 'goal' (+ optional context, toolsets)
2. Batch (parallel): provide 'tasks' array with up to 3 items. All run concurrently and results are returned together.

WHEN TO USE delegate_task:
- Reasoning-heavy subtasks (debugging, code review, research synthesis)
- Tasks that would flood your context with intermediate data
- Parallel independent workstreams (research A and B simultaneously)

WHEN NOT TO USE (use these instead):
- Mechanical multi-step work with no reasoning needed -> use execute_code
- Single tool call -> just call the tool directly
- Tasks needing user interaction -> subagents cannot use clarify

IMPORTANT:
- Subagents have NO memory of your conversation. Pass all relevant info (file paths, error messages, constraints) via the 'context' field.
- Subagents CANNOT call: delegate_task, clarify, memory, send_message, execute_code.
- Each subagent gets its own terminal session (separate working directory and state).
- Results are always returned as an array, one entry per task.
parameterstypeobject
propertiesr   stringzWhat the subagent should accomplish. Be specific and self-contained -- the subagent knows nothing about your conversation history.r   zBackground information the subagent needs: file paths, error messages, project structure, constraints. The more specific you are, the better the subagent performs.r"   arrayitemszToolsets to enable for this subagent. Default: inherits your enabled toolsets. Common patterns: ['terminal', 'file'] for code work, ['web'] for research, ['terminal', 'file', 'web'] for full-stack tasks.r   z	Task goalzTask-specific contextzTToolsets for this specific task. Use 'web' for network access, 'terminal' for shell.rx   zfPer-task ACP command override (e.g. 'claude'). Overrides the top-level acp_command for this task only.ry   zPer-task ACP args override.requiredmaxItemszBatch mode: up to 3 tasks to run in parallel. Each gets its own subagent with isolated context and terminal session. When provided, top-level goal/context/toolsets are ignored.rc   integerzSMax tool-calling turns per subagent (default: 50). Only set lower for simple tasks.a   Override ACP command for child agents (e.g. 'claude', 'copilot'). When set, children use ACP subprocess transport instead of inheriting the parent's transport. Enables spawning Claude Code (claude --acp --stdio) or other ACP-capable agents from any parent, including Discord/Telegram/CLI.zArguments for the ACP command (default: ['--acp', '--stdio']). Only used when acp_command is set. Example: ['--acp', '--stdio', '--model', 'claude-opus-4-6'])registryr&   c                    \        V P                  R 4      V P                  R4      V P                  R4      V P                  R4      V P                  R4      V P                  R4      V P                  R4      VP                  R4      R7      # )	r   r   r"   r   rc   rx   ry   r_   )r   r   r"   r   rc   rx   ry   r_   )r   r   )rL   kws   &,r   r  r  f  sj    }XXf#*%hhwxx 01HH]+*%VVN+ -r   u   🔀)r   toolsetschemahandlercheck_fnrQ   )r   r%   r$   send_messageexecute_code)terminalfileweb)N)   )NNNNNN)NN)NNNNNNNN)#__doc__r
  r   	getLogger__name__rH   r?  r   concurrent.futuresr   r   typingr   r   r   r   	frozensetDELEGATE_BLOCKED_TOOLSr  r  r  r   r   r    r*   r`   r   r   r   r  r  DELEGATE_TASK_SCHEMAtools.registryrZ  registerr   r   r   <module>rq     sK  "  			8	$ 	  ? , , # $    	 . 
,CM`ENVSpupVr8 O	I0 , J &(+(	
 H =+ N!FHmE\#]""G#fh%7)  ,B%
 &"H)  ,T( #"G#fh%7)+H%#$ ), AR5> 	7 c &(+u	WS
h 	BmW7s n $   	- )
r   