
    iY              
       6   d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z		 ddl
Z
n # e$ r dZ
	 ddlZn# e$ r dZY nw xY wY nw xY wddlmZ ddlmZ e	j                            d e ee          j        j                             ddlmZ ddlmZ ddlmZ  ej        e          Z e h d          Z!dd	l"m#Z#m$Z$m%Z%m&Z& d
Z' e            Z(e(dz  Z)e)dz  Z*de+dee+         fdZ,de+dee+         fdZ- e h d          Z. e h d          Z/ e h d          Z0dede1de+dz  de+ddf
dZ2d%de+dedee         fdZ3dZ4e4Z5de6fdZ7dede8e9ef         fdZ:de+defdZ;de+de8e9eeee         f         fdZ<d&d!e9de6fd"Z=ed#k    r e=d $           dS dS )'a  
Cron job scheduler - executes due jobs.

Provides tick() which checks for due jobs and runs them. The gateway
calls this every 60 seconds from a background thread.

Uses a file-based lock (~/.hermes/cron/.tick.lock) so only one tick
runs at a time if multiple processes overlap.
    NPath)Optionalget_hermes_home)load_config)now>   smsemailqqbotslackwecomfeishumatrixsignalweixindiscordwebhookdingtalktelegramwhatsapp
mattermostbluebubbleshomeassistantwecom_callback)get_due_jobsmark_job_runsave_job_outputadvance_next_runz[SILENT]cronz
.tick.lockjobreturnc                     |                      d          }|sdS |                     d          }|                     d          }|r|r|S dS )zFExtract origin info from a job, preserving any extra routing metadata.originNplatformchat_id)get)r!   r$   r%   r&   s       6/home/agentuser/.hermes/hermes-agent/cron/scheduler.py_resolve_originr)   C   s[    WWXF tzz*%%Hjj##G G 4    c                 0   |                      dd          }t          |           }|dk    rdS |dk    r|r2|d         t          |d                   |                     d          dS d	D ]z}t          j        |                                 d
d          }|rLt                              d|                      d|                      dd                    |           ||ddc S {dS d|v r|                    dd          \  }}|	                                }ddl
m}  |||          \  }}	}
|
r||	}}n|d}}	 ddlm}  |||          }|r |||          \  }}	}|r||	}}n|}n# t          $ r Y nw xY w|||dS |}|rE|                     d          |k    r,|t          |d                   |                     d          dS |	                                t          vrdS t          j        |                                 d
d          }|sdS ||ddS )zAResolve the concrete auto-delivery target for a cron job, if any.deliverlocalNr$   r%   r&   	thread_id)r%   r&   r.   )r   r   r   r   r   _HOME_CHANNEL zJJob '%s' has deliver=origin but no origin; falling back to %s home channelnameid?:   r   )_parse_target_ref)resolve_channel_name)r'   r)   strosgetenvupperloggerinfosplitlowertools.send_message_toolr6   gateway.channel_directoryr7   	Exception_KNOWN_DELIVERY_PLATFORMS)r!   r,   r$   platform_namer&   restplatform_keyr6   parsed_chat_idparsed_thread_idis_explicitr.   r7   resolvedresolved_is_explicits                  r(   _resolve_delivery_targetrL   O   s   ggi))GS!!F't( 	":.vi011#ZZ44   W 	 	Mi=#6#6#8#8 G G GLLG 
`GGFCGGD#$6$677!   !.&!%    
 t
g~~%mmC33t$**,,======8I8I,X\8]8]5(+ 	,!/1AYGG!%tYG
	FFFFFF++L'BBH 'IZIZ[giqIrIrF 02F' ')79IYGG&G 	 	 	D	 &"
 
 	
 M 
&**Z((M99%6),--K00
 
 	
 $===ti=..00???DDG t "  s   :-E( (
E54E5>   .m4a.mp3.ogg.wav.opus>   .3gp.avi.mkv.mov.mp4.webm>   .gif.jpg.png.jpeg.webpr&   media_filesmetadatac                    ddl m} |D ]n\  }}	  ||          j                                        }	|	t          v r|                     |||          }
n\|	t          v r|                     |||          }
n:|	t          v r| 	                    |||          }
n| 
                    |||          }
t          j        |
|          }|                    d          }|rQt          |d	d
          s@t                              d|                    dd          |t          |dd                     '# t$          $ r<}t                              d|                    dd          ||           Y d}~hd}~ww xY wdS )u;  Send extracted MEDIA files as native platform attachments via a live adapter.

    Routes each file to the appropriate adapter method (send_voice, send_image_file,
    send_video, send_document) based on file extension — mirroring the routing logic
    in ``BasePlatformAdapter._process_message_background``.
    r   r   )r&   
audio_pathr^   )r&   
video_pathr^   )r&   
image_pathr^   )r&   	file_pathr^      timeoutsuccessTz&Job '%s': media send failed for %s: %sr2   r3   errorunknownz%Job '%s': failed to send media %s: %sN)pathlibr   suffixr?   _AUDIO_EXTS
send_voice_VIDEO_EXTS
send_video_IMAGE_EXTSsend_image_filesend_documentasynciorun_coroutine_threadsaferesultgetattrr<   warningr'   rB   )adapterr&   r]   r^   loopr!   r   
media_path	_is_voiceextcorofutureru   es                 r(   _send_media_via_adapterr      s    !, g g
I	g$z"")//11Ck!!))'j[c)dd##))'j[c)dd##..w:`h.ii,,W
]e,ff5dDAAF]]2]..F gfi>> <GGD#&&
GFGY4W4W    	g 	g 	gNNBCGGDRUDVDVXbdeffffffff	g'g gs   DD00
E6:1E11E6contentc                 J   t          |           }|sY|                     dd          dk    r=d|                     dd           }t                              d| d         |           |S dS |d         }|d         }|                    d	          }|                     d
          pi }	|	                    d	          }
|
r;|s9t                              d| d         |
|                     dd          |           n&|r$t                              d| d         |||           ddlm} ddlm}m	} i d|j
        d|j        d|j        d|j        d|j        d|j        d|j        d|j        d|j        d|j        d|j        d|j        d|j        d|j        d|j        d|j        d |j        }|                    |                                          }|s*d!| d"}t                              d| d         |           |S 	  |            }n@# t8          $ r3}d#| }t                              d| d         |           |cY d}~S d}~ww xY w|j                            |          }|r|j        s*d$| d%}t                              d| d         |           |S d&}	 tA                      }|                    d'i                               d(d&          }n# t8          $ r Y nw xY w|rB|                     d)| d                   }|                     dd*          }d+| d,| d-| d.| d/	}n|}dd0l!m"} |#                    |          \  }}|pi                     |          }|E|B tI          |d1d2                       r'|rd	|ind}	 |%                                }d&}|rtM          j'        |(                    |||3          |          }|)                    d45          } | rHtI          | d6d&          s7tI          | d7d8          }!t                              d9| d         |||!           d:}|r|rtU          ||||||            |r%t          +                    d;| d         ||           dS n;# t8          $ r.}t                              d<| d         |||           Y d}~nd}~ww xY w |||||||=          }"	 tM          j,        |"          }#n# tZ          $ r |".                                 ddl/}$|$j0        1                    d>?          5 }%|%2                    tL          j,         |||||||=                    }|)                    d@5          }#ddd           n# 1 swxY w Y   Y nEt8          $ r9}dA| dB| dC| }t                              d| d         |           |cY d}~S d}~ww xY w|#rD|#                    d7          r/dD|#d7          }t                              d| d         |           |S t          +                    dE| d         ||           dS )Fu  
    Deliver job output to the configured target (origin chat, specific platform, etc.).

    When ``adapters`` and ``loop`` are provided (gateway is running), tries to
    use the live adapter first — this supports E2EE rooms (e.g. Matrix) where
    the standalone HTTP path cannot encrypt.  Falls back to standalone send if
    the adapter path fails or is unavailable.

    Returns None on success, or an error string on failure.
    r,   r-   z(no delivery target resolved for deliver=zJob '%s': %sr2   Nr%   r&   r.   r$   zUJob '%s': origin has thread_id=%s but delivery target lost it (deliver=%s, target=%s)z*Job '%s': delivering to %s:%s thread_id=%sr   )_send_to_platform)load_gateway_configPlatformr   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r   zunknown platform ''zfailed to load gateway config: z
platform 'z' not configured/enabledTr    wrap_responser1   r0   zCronjob Response: z

(job_id: z)
-------------

zI

To stop or manage this job, send me a new message (e.g. "stop reminder z").)BasePlatformAdapter
is_runningc                      dS )NF r   r*   r(   <lambda>z!_deliver_result.<locals>.<lambda>6  s    `e r*   )r^   <   re   rg   rh   ri   zLJob '%s': live adapter send to %s:%s failed (%s), falling back to standaloneFz-Job '%s': delivered to %s:%s via live adapterzPJob '%s': live adapter delivery to %s:%s failed (%s), falling back to standalone)r.   r]   r5   max_workersrd   zdelivery to r4   z	 failed: zdelivery error: zJob '%s': delivered to %s:%s)3rL   r'   r<   rw   debugr@   r   gateway.configr   r   TELEGRAMDISCORDSLACKWHATSAPPSIGNALMATRIX
MATTERMOSTHOMEASSISTANTDINGTALKFEISHUWECOMWECOM_CALLBACKWEIXINEMAILSMSBLUEBUBBLESQQBOTr?   rB   rh   	platformsenabledr   gateway.platforms.baser   extract_mediarv   striprs   rt   sendru   r   r=   runRuntimeErrorcloseconcurrent.futuresfuturesThreadPoolExecutorsubmit)&r!   r   adaptersry   targetmsgrD   r&   r.   r$   origin_threadr   r   r   platform_mapr%   configr   pconfigr   user_cfg	task_namejob_iddelivery_contentr   r]   cleaned_delivery_contentruntime_adaptersend_metadatatext_to_send
adapter_okr~   send_resulterrr}   ru   
concurrentpools&                                         r(   _deliver_resultr      ss    &c**F 779g&&'11ZSWWYPW=X=XZZCNN>3t9c:::Jt:&MYG

;''I WWX$"FJJ{++M 

Y 

&I}cggi&A&A6	
 	
 	
 	

 
 
8I}gy	
 	
 	

 :99999<<<<<<<<H%8# 	 	H%	
 	(/ 	(/ 	h) 	/ 	H% 	(/ 	 	(1 	(/ 	 	x|  	x+!" 	#L&  3 3 5 566H 3=333~s4y#666
$$&&   333^SY444






 ""8,,G '/ B=BBB~s4y#666

 M== VR0044_dKK     #GGFCI..	r""g g gg g g g Xa	g g g 	 # ;:::::,?,M,MN^,_,_)K)  ~2**844O"t'7<fGD,XeXe<f<f<h<h'74=Gi004	399;;LJ ' 9#((,(WW  %mmBm77 'w{It'L'L '!+w	BBCNNfD	='3   "'J  ik i'+}^bdghhh KSQUYXegnooot  	 	 	NNbD	='1       	 Xw9Q]ft  A  A  ADT"" 	/ 	/ 	/
 	

!!!!22q2AA 	/T[[.?.?'SZ\t  AJ  Xc  /d  /d  /d  e  eF]]2]..F	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/    B]BBWBBqBB^SY444






  &**W%% 222^SY444

KK.D	='RRR4s   
H 
I (III&8K 
K,+K,C Q7 7
R/$R**R/S =V7AU(V7(U,	,V7/U,	0V75	V7>.V2,V72V7x   c                  2   t           t          k    r[	 t          t          t                               } | dk    r| S n0# t          $ r# t
                              dt                      Y nw xY wt          j        dd          	                                }|rQ	 t          t          |                    } | dk    r| S n+# t          $ r t
                              d|           Y nw xY w	 t                      pi }t          |t                    r|                    di           ni }|                    d          }|$t          t          |                    } | dk    r| S n2# t          $ r%}t
                              d	|           Y d}~nd}~ww xY wt          S )
zOResolve cron pre-run script timeout from module/env/config with a safe default.r   z<Invalid patched _SCRIPT_TIMEOUT=%r; using env/config/defaultHERMES_CRON_SCRIPT_TIMEOUTr0   z;Invalid HERMES_CRON_SCRIPT_TIMEOUT=%r; using config/defaultr    script_timeout_secondsNz2Failed to load cron script timeout from config: %s)_SCRIPT_TIMEOUT_DEFAULT_SCRIPT_TIMEOUTintfloatrB   r<   rw   r9   r:   r   r   
isinstancedictr'   r   )rf   	env_valuecfgcron_cfg
configuredexcs         r(   _get_script_timeoutr   x  s   111	l%0011G{{  	l 	l 	lNNY[jkkkkk	l 	6;;AACCI e	e%	**++G{{  	e 	e 	eNNXZcddddd	e	Pmm!r*4S$*?*?G37762&&&R\\":;;
!%
++,,G{{ P P PI3OOOOOOOOP #"s:   (< *A)(A)#B; ;%C#"C#'A7E   
F*F

Fscript_pathc           	         ddl m}  |            dz  }|                    dd           |                                }t	          |                                           }|                                r|                                }n||z                                  }	 |                    |           n# t          $ r dd| d| fcY S w xY w|	                                sdd	| fS |
                                sdd
| fS t                      }	 t          j        t          j        t!          |          gdd|t!          |j                            }|j        pd                                }|j        pd                                }		 ddlm}
  |
|          } |
|	          }	n# t.          $ r Y nw xY w|j        dk    rVd|j         g}|	r|                    d|	            |r|                    d|            dd                    |          fS d|fS # t          j        $ r dd| d| fcY S t.          $ r}dd| fcY d}~S d}~ww xY w)u  Execute a cron job's data-collection script and capture its output.

    Scripts must reside within HERMES_HOME/scripts/.  Both relative and
    absolute paths are resolved and validated against this directory to
    prevent arbitrary script execution via path traversal or absolute
    path injection.

    Args:
        script_path: Path to a Python script.  Relative paths are resolved
            against HERMES_HOME/scripts/.  Absolute and ~-prefixed paths
            are also validated to ensure they stay within the scripts dir.

    Returns:
        (success, output) — on failure *output* contains the error message so the
        LLM can report the problem to the user.
    r   r   scriptsTparentsexist_okFz=Blocked: script path resolves outside the scripts directory (z): zScript not found: zScript path is not a file: )capture_outputtextrf   cwdr0   )redact_sensitive_textzScript exited with code zstderr:
zstdout:

zScript timed out after zs: zScript execution failed: N)hermes_constantsr   mkdirresolver   
expanduseris_absoluterelative_to
ValueErrorexistsis_filer   
subprocessr   sys
executabler8   parentstdoutr   stderragent.redactr   rB   
returncodeappendjoinTimeoutExpired)r   r   scripts_dirscripts_dir_resolvedrawpathscript_timeoutru   r   r   r   partsr   s                r(   _run_job_scriptr     s8   " 100000!/##i/KdT222&..00
{


&
&
(
(C
 -{{}}c!**,,
-.... 
 
 
9$9 9)49 9
 	
 	
 	

 ;;== 2141111<<>> ;:D::::(**N 8^SYY'"DK  
 
 
 -%2,,..-%2,,..	::::::**622F**622FF 	 	 	D	 !!C0ACCDE 3111222 3111222$))E****V|$ J J JIII4IIIIII 8 8 87#7777777778sb   !B7 7CCA:H# F. -H# .
F;8H# :F;;A#H# H# #I>	IIIIc           
      z   |                      dd          }|                      d          }|                      d          }|r-t          |          \  }}|r|r	d| d| }nd| }nd| d| }d	}||z   }||                      d          }|r|gng }d |D             }|s|S ddlm}	 g }
g }|D ]}t	          j         |	|                    }|                     d          st|                     d          pd| d}t                              d|                      d|                      d                    |           |                    |           t          |                     d          pd          
                                }|
r|
                    d           |
                    d| dd|g           |rEdd                    |           dd                    |           d}|
                    d|           |r|
                    dd| g           d                    |
          S )zWBuild the effective prompt for a cron job, optionally loading one or more skills first.promptr0   skillsscriptzq## Script Output
The following data was collected by a pre-run script. Use it as context for your analysis.

```
z
```

z3[Script ran successfully but produced no output.]

zQ## Script Error
The data-collection script failed. Report this to the user.

```
u  [SYSTEM: You are running as a scheduled cron job. DELIVERY: Your final response will be automatically delivered to the user — do NOT use send_message or try to deliver the output yourself. Just produce your report/output as your final response and the system handles the rest. SILENT: If there is genuinely nothing new to report, respond with exactly "[SILENT]" (nothing else) to suppress delivery. Never combine [SILENT] with content — either report your findings normally, or say [SILENT] and nothing more.]

Nskillc                     g | ]D}t          |                                          #t          |                                          ES r   )r8   r   ).0r1   s     r(   
<listcomp>z%_build_job_prompt.<locals>.<listcomp>  s=    MMM3t99??;L;LM3t99??$$MMMr*   r   )
skill_viewrg   rh   zFailed to load skill 'r   u/   Cron job '%s': skill not found, skipping — %sr1   r2   r   z#[SYSTEM: The user has invoked the "zf" skill, indicating they want you to follow its instructions. The full skill content is loaded below.]zb[SYSTEM: The following skill(s) were listed for this job but could not be found and were skipped: z, un   . Start your response with a brief notice so the user is aware, e.g.: '⚠️ Skill(s) not found and skipped: z']zPThe user has provided the following instruction alongside the skill invocation: r   )r'   r   tools.skills_toolr  jsonloadsr<   rw   r   r8   r   extendr   insert)r!   r   r   r   rg   script_output	cron_hintlegacyskill_namesr  r   skipped
skill_nameloadedrh   r   notices                    r(   _build_job_promptr    s   WWXr""FWWXF ''(##K !0!=!= 	   *    	         %    	D  F~!!#+&MMMMMK ,,,,,,EG! 
 

JJz2233zz)$$ 	JJw''Q+QJ+Q+Q+QENNLcggV\^a^e^efj^k^kNlNlnstttNN:&&&fjj++1r2288:: 	LL ij  i  i  i	
 	
 	
 	
   N!%7!3!3N N 8<yy7I7IN N N 	 	Q xbumsuuvwww99Ur*   c                 4   ddl m} d}	 ddlm}  |            }nG# t          $ r:}t
                              d|                     dd          |           Y d}~nd}~ww xY w| d         }| d         }t          |           }t          |           }d	| d
t                                          d           }	t
                              d||           t
                              d|dd                    	 |ra|d         t          j        d<   t          |d                   t          j        d<   |                    d          r|d         t          j        d<   ddlm}
 	  |
t          t$          dz            dd           n3# t&          $ r&  |
t          t$          dz            dd           Y nw xY wt)          |           }|rn|d         t          j        d<   t          |d                   t          j        d<   |                    d          "t          |d                   t          j        d<   |                     d          pt          j        d           pd!}i }	 ddl}t          t$          d"z            }t          j                            |          rt3          |          5 }|                    |          pi }ddd           n# 1 swxY w Y   |                    di           }|                     d          sCt7          |t                    r|}n+t7          |t8                    r|                    d#|          }n3# t          $ r&}t
                              d$||           Y d}~nd}~ww xY w	 dd%lm} |                    d&i           }t7          |t8                    r!|                    d'          r |d(           n# t          $ r Y nw xY wdd)lm } t          |                    d*i                               d+d!                    !                                } ||          }d}t          j        d,d!          p|                    d-d!          }|rddl"}tG          |          $                                }|%                                s
t$          |z  }|                                r	 t3          |d.d/          5 }|&                    |          }ddd           n# 1 swxY w Y   t7          |tN                    sd}n6# t          $ r)}t
                              d0|||           d}Y d}~nd}~ww xY w|                    d*i                               d1          p|                    d1          pd2}|                    d3i           }|                    d4i           pi }dd5l(m)}m*}  	 d6|                     d7          pt          j        d8          i}!|                     d9          r|                     d9          |!d:<    |di |!}"n-# t          $ r }# | |#          }$tW          |$          |#d}#~#ww xY wdd;l,m-}%  |%||||"                    d<          |"                    d9          |"                    d7          |"                    d=          |"                    d>          tO          |"                    d?          pg           d@          }&|                    dA          p|                    dB          pd}'d}(t          |&dC                             d7          pd!          !                                .                                })|)r	 ddDl/m0}*  |*|)          }+|+1                                r>|+}(t
                              dE||)te          |+3                                                     n4# t          $ r'}t
                              dF||)|           Y d}~nd}~ww xY w |di d|&d         d<|&dC                             d<          d9|&dC                             d9          d7|&dC                             d7          d=|&dC                             d=          dG|&dC                             d>          dH|&dC                             d?          dI|dJ|dK|dB|'dL|(dM|                    dN          dO|                    dP          dQ|                    dR          dS|                    dT          dUg dVdWddXddYdddZd[|	d\|},ti          t          j        d]d^                    }-|-dk    r|-nd}.d_}/tj          j6        7                    d`a          }0tq          j9                    }1|0:                    |1j;        |,j<        |          }2db}3	 |.|2=                                }4nd}4	 tj          j6        >                    |2h|/c          \  }5}6|5r|2=                                }4nXdd}7t          |,de          r<	 |,@                                }8|8                    dfdd          }7n# t          $ r Y nw xY w|7|.k    rd}3nn&# t          $ r |0A                    dbdg            w xY w|0A                    dbdg           n# |0A                    dbdg           w xY w|3r"i }9t          |,de          r&	 |,@                                }9n# t          $ r Y nw xY w|9                    dhdi          }:|9                    dfd          };|9                    dj          }<|9                    dkd          }=|9                    dId          }>t
          B                    dl||;|.|:|=|>|<pdm           t          |,dn          r|,C                    do           t          dp| dqt          |;           drt          |.           ds|:           |4                    dtd!          pd!}?|?!                                duk    rd!}?|?r|?ndu}@dv| dw| dxt                                          dy           dz|                     d{d|           d}| d~|@ d}At
                              d|           d|A|?dfdD ]"}Bt          j        F                    |Bd           #|r	 |G                    |	d           n:# t          t          f$ r&}t
                              d||           Y d}~nd}~ww xY w	 |I                                 S # t          t          f$ r&}t
                              d||           Y d}~S d}~ww xY wS # t          $ rc}t          |          jK         dt          |           }Ct
          L                    d||C           dv| d| dxt                                          dy           dz|                     d{d|           d}| d|C d}Adb|Ad!|CfcY d}~dD ]"}Bt          j        F                    |Bd           #|r	 |G                    |	d           n:# t          t          f$ r&}t
                              d||           Y d}~nd}~ww xY w	 |I                                 S # t          t          f$ r&}t
                              d||           Y d}~S d}~ww xY wS d}~ww xY w# dD ]"}Bt          j        F                    |Bd           #|r	 |G                    |	d           n:# t          t          f$ r&}t
                              d||           Y d}~nd}~ww xY w	 |I                                 w # t          t          f$ r&}t
                              d||           Y d}~w d}~ww xY ww xY w)z
    Execute a single cron job.
    
    Returns:
        Tuple of (success, full_output_doc, final_response, error_message)
    r   )AIAgentN)	SessionDBz0Job '%s': SQLite session store not available: %sr2   r3   r1   cron__z%Y%m%d_%H%M%SzRunning job '%s' (ID: %s)z
Prompt: %sd   r%   HERMES_SESSION_PLATFORMr&   HERMES_SESSION_CHAT_ID	chat_nameHERMES_SESSION_CHAT_NAME)load_dotenvz.envTzutf-8)overrideencodingzlatin-1!HERMES_CRON_AUTO_DELIVER_PLATFORM HERMES_CRON_AUTO_DELIVER_CHAT_IDr.   "HERMES_CRON_AUTO_DELIVER_THREAD_IDmodelHERMES_MODELr0   zconfig.yamldefaultz8Job '%s': failed to load config.yaml, using defaults: %s)apply_ipv4_preferencenetwork
force_ipv4)force)parse_reasoning_effortagentreasoning_effortHERMES_PREFILL_MESSAGES_FILEprefill_messages_filer)r  z8Job '%s': failed to parse prefill messages file '%s': %s	max_turnsZ   provider_routingsmart_model_routing)resolve_runtime_providerformat_runtime_provider_error	requestedproviderHERMES_INFERENCE_PROVIDERbase_urlexplicit_base_url)resolve_turn_routeapi_keyapi_modecommandargs)r"  r;  r8  r6  r<  r=  r>  fallback_providersfallback_modelruntime)	load_poolz@Job '%s': loaded credential pool for provider %s with %d entriesz3Job '%s': failed to load credential pool for %s: %sacp_commandacp_argsmax_iterationsreasoning_configprefill_messagescredential_poolproviders_allowedonlyproviders_ignoredignoreproviders_orderorderprovider_sortsortdisabled_toolsets)cronjob	messagingclarify
quiet_modeskip_context_filesskip_memoryr    
session_id
session_dbHERMES_CRON_TIMEOUTiX  g      @r5   r   Fre   g        get_activity_summaryseconds_since_activity)waitcancel_futureslast_activity_descri   current_toolapi_call_countz_Job '%s' idle for %.0fs (inactivity limit %.0fs) | last_activity=%s | iteration=%s/%s | tool=%snone	interruptzCron job timed out (inactivity)z
Cron job 'z' idle for z	s (limit u   s) — last activity: final_responsez(No response generated)z# Cron Job: z

**Job ID:** z
**Run Time:** z%Y-%m-%d %H:%M:%Sz
**Schedule:** schedule_displayzN/Az

## Prompt

z

## Response

r   zJob '%s' completed successfully)r  r  r  r  r   r!  cron_completez#Job '%s': failed to end session: %sz2Job '%s': failed to close SQLite session store: %sz: zJob '%s' failed: %sz (FAILED)

**Job ID:** z

## Error

```
z
```
r   )M	run_agentr  hermes_stater  rB   r<   r   r'   r  r)   _hermes_nowstrftimer=   r9   environr8   dotenvr  _hermes_homeUnicodeDecodeErrorrL   r:   yamlr   r   open	safe_loadr   r   rw   r   r%  r)  r   r  r   r   r   loadlisthermes_cli.runtime_providerr3  r4  r   agent.smart_model_routingr:  r?   agent.credential_poolrB  has_credentialslenentriesr   r   r   r   contextvarscopy_contextr   r   run_conversationru   r]  hasattrr[  shutdownrh   rc  TimeoutErrorr   popend_sessionKeyboardInterruptr   type__name__	exception)Dr!   r  _session_dbr  r   r   job_namer   r$   _cron_session_idr  delivery_targetr"  _cfgro  	_cfg_path_f
_model_cfgr%  _net_cfgr)  effortrF  rG  prefill_file_jsonpfpath_pfrE  prsmart_routingr3  r4  runtime_kwargsrA  r   messager:  
turn_router@  rH  runtime_providerrB  r   r*  _cron_timeout_cron_inactivity_limit_POLL_INTERVAL
_cron_pool_cron_context_cron_future_inactivity_timeoutru   doner  
_idle_secs_act	_activity
_last_desc	_secs_ago	_cur_tool_iter_n	_iter_maxrd  logged_responseoutputkey	error_msgsD                                                                       r(   run_jobr  D  s    "!!!!! K`******ikk ` ` `GQUWZI[I[]^________` YF6{Hs##FS!!FQvQQ(>(>(O(OQQ
KK+Xv>>>
KKfTcTl+++k^  	M4::4FBJ0136vi7H3I3IBJ/0zz+&& M9?9L
56 	'&&&&&	WKL6122TGTTTTT! 	W 	W 	WKL6122TIVVVVVV	W 3377 	e>Mj>YBJ:;=@QZA[=\=\BJ9:"";//;CFWbGcCdCd
?@  CBIn$=$=C 	bKKKL=899Iw~~i(( A)__ 4>>"--3D4 4 4 4 4 4 4 4 4 4 4 4 4 4 4!XXgr22
www'' A!*c22 A *#J55 A *y% @ @ 	b 	b 	bNNUW]_`aaaaaaaa	b	>>>>>>xx	2..H(D)) 2hll<.H.H 2%%D1111 	 	 	D	 	<;;;;;TXXgr**../A2FFGGMMOO11&99  y!?DDmQhjlHmHm 	,    ,''2244F%%'' /%.}} ,,fcG<<< ;+0::c??(; ; ; ; ; ; ; ; ; ; ; ; ; ; ;%&6== 0+/(  , , ,NN#]_egmopqqq'+$$$$$$,
 '2..22;??^488KCXCX^\^ XX("--!6;;Ar	
 	
 	
 	
 	
 	
 	
 	
		1SWWZ00ZBI>Y4Z4ZN wwz"" J69ggj6I6I23..@@@@GG 	1 	1 	133C88Gw''S0	1 	A@@@@@''";;y11#KK
33#KK
33#KK
33";;y11W[[006B77 
 

 "677]488DT;U;U]Y]z)488DDJKKQQSSYY[[ 	qq;;;;;; y!122'')) &*OKKZ(DLLNN++	    q q qRTZ\lnoppppppppq  
 
 
W%%
y)--i888
  	*..z:::
  	*..z:::	

  	*..z:::
 #9-11)<<<
  	*..v666
 *>
 .-
 .-
 *>
 ,O
 !ffVnnn
 !ffX...
 FF7OOO
  &&...!
" BAAA#
$ t%
&  $t'
( )
* V+
, ('-
. #{/
D bi(=sCCDD2?!2C2C'::q:II
 $022!(():E<RTZ[[#	A%-%,,..(055% 6  GD!  !-!4!4!6!6!$Ju&<== !!#(#=#=#?#?D)-2JC)P)PJJ( ! ! ! D!!%;;;.2+#$  	 	 	U4@@@	 U4@@@@JU4@@@@ 	Iu455  % : : < <II    D"';YGGJ!&>BBI!n55Imm$4a88G!&6::ILLA)%;GY#V   uk** C ABBB3X 3 3y>>3 3,/0F,G,G3 3&03 3    $4b99?R!!%>>>N -;Y..@Y(    }}%%&9::  ww)511	       	5x@@@V^T14
 	& 	&C JNN3%%%% 	^O''(8/JJJJ01 O O OBFANNNNNNNNO^!!####01 ^ ^ ^QSY[\]]]]]]]]^	^C  , , ,Aww'333q6633	.)DDD(    }}%%&9::  ww)511	        fb)++++++
 	& 	&C JNN3%%%% 	^O''(8/JJJJ01 O O OBFANNNNNNNNO^!!####01 ^ ^ ^QSY[\]]]]]]]]^	^C,0
 	& 	&C JNN3%%%% 	^O''(8/JJJJ01 O O OBFANNNNNNNNO^!!####01 ^ ^ ^QSY[\]]]]]]]]^	^s   
A0AA7A)u !#F u -F52u 4F55B/u %A	M .KM KM KA1M u 
M<M72u 7M<<u  AO u 
O u O  C#u T S8,T 8S<<T ?S< T u 
U%U	u 	UA9u A X) (u )
Y3YYDu 1A#_ u 
``<u `F<u A(i2 ,*i i2 
i$!i2 #i$$i2 1j0 2#jj0 u 0k		u "k7 6u 7
lu lFu 9st!ttt  u1uu{	&B{5{	6{ "x99y0
y++y04z		{ z;;{ {		{ (~5|~}|>9~>}~}~~-~	~~~Tverbosec           	      
   t                               dd           d}	 t          t          d          }t          r-t	          j        |t          j        t          j        z             n9t          r2t          j	        |
                                t          j        d           nH# t          t          f$ r4 t                              d           ||                                 Y dS w xY w	 t#                      }| r|st                              dt'                                          d	                     	 t          r t	          j        |t          j                   nRt          rK	 t          j	        |
                                t          j        d           n# t          t          f$ r Y nw xY w|                                 dS | rHt                              d
t'                                          d	          t/          |                     d}|D ]}	 t1          |d                    t3          |          \  }}}	}
t5          |d         |          }| rt                              d|           |r|	n!d|                    d|d                    d|
 }t9          |          }|rX|rVt:          |                                                                v r)t                              d|d         t:                     d}d}|r]	 tA          ||||          }nH# tB          $ r;}tE          |          }t          #                    d|d         |           Y d}~nd}~ww xY w|r|	sd}d}
tI          |d         ||
|           |dz  }x# tB          $ rQ}t          #                    d|d         |           tI          |d         dtE          |                     Y d}~d}~ww xY w|t          r t	          j        |t          j                   nRt          rK	 t          j	        |
                                t          j        d           n# t          t          f$ r Y nw xY w|                                 S # t          r t	          j        |t          j                   nRt          rK	 t          j	        |
                                t          j        d           n# t          t          f$ r Y nw xY w|                                 w xY w)u  
    Check and run all due jobs.
    
    Uses a file lock so only one tick runs at a time, even if the gateway's
    in-process ticker and a standalone daemon or manual tick overlap.
    
    Args:
        verbose: Whether to print status messages
        adapters: Optional dict mapping Platform → live adapter (from gateway)
        loop: Optional asyncio event loop (from gateway) for live adapter sends
    
    Returns:
        Number of jobs executed (0 if another tick is already running)
    Tr   Nwr5   u0   Tick skipped — another instance holds the lockr   z%s - No jobs duez%H:%M:%Sz%s - %s job(s) duer2   zOutput saved to: %su   ⚠️ Cron job 'r1   z
' failed:
u1   Job '%s': agent returned %s — skipping deliveryF)r   ry   zDelivery failed for job %s: %szWAgent completed but produced empty response (model error, timeout, or misconfiguration))delivery_errorzError processing job %s: %s)%	_LOCK_DIRr   rp  
_LOCK_FILEfcntlflockLOCK_EXLOCK_NBmsvcrtlockingfilenoLK_NBLCKOSErrorIOErrorr<   r   r   r   r=   ri  rj  LOCK_UNLK_UNLCKrx  r   r  r   r'   boolSILENT_MARKERr   r;   r   rB   r8   rh   r   )r  r   ry   lock_fddue_jobsexecutedr!   rg   r  rd  rh   output_filedeliver_contentshould_deliverr  der   s                    r(   tickr    sL    OOD4O000 G
z3'' 	AK!>???? 	AN7>>++V_a@@@W   GHHHMMOOOqq	A>> 	8 	KK*KMM,B,B:,N,NOOOj  	K//// 	w~~//!DDDDW%   u  	aKK,kmm.D.DZ.P.PRUV^R_R_``` +	7 +	7C*7
 !T+++9@6-c$i@@ DKK 5{CCC
 5<  #D..  BDUXU\U\]cehimenUoUo  BD  BD  }B  BD  BD!%o!6!6! +g +-?CXCXCZCZC`C`CbCb2b2bKK SUXY]U^`mnnn%*N!%! VV)8oX`gk)l)l)l$ V V V),R%Es4yRTUUUUUUUUV  v> v#GuESY~VVVVA 7 7 7:CIqIIISYs1vv666666667  	K//// 	w~~//!DDDDW%     	K//// 	w~~//!DDDDW%   s   BB# #AC('C(,AQ; (2F F/.F/AQ; C/N
LN
M#(1MNM##)NQ; 
O)AO$Q; $O))Q; 2QQ#"Q#;/T
+2ST
S2/T
1S22T
__main__)r  )NN)TNN)>__doc__rs   r   r   rz  r  loggingr9   r   r   r  ImportErrorr  rj   r   typingr   r   r  r8   __file__r   r   r   hermes_cli.configr   hermes_timer	   ri  	getLoggerr  r<   	frozensetrC   	cron.jobsr   r   r   r   r  rm  r  r  r   r)   rL   rl   rn   rp   rs  r   r   r   r   r   r   tupler  r   r  r  r  r   r*   r(   <module>r     s               				     



LLLL   E   	            
 33ttH~~,344 5 5 5 , , , , , , ) ) ) ) ) ) * * * * * *		8	$	$ &I ' ' '    T S S S S S S S S S S S
     6!	%
	 	(4. 	 	 	 	Q$ Q8D> Q Q Q Qj iAAABBiIIIJJiBBBCCgc g gPTW[P[ ghl gqu g g g g@g g g g(SV- g g g gT  )#S # # # #BN8 N8tSy)9 N8 N8 N8 N8bW4 WC W W W WtF^ F^%c3 => F^ F^ F^ F^R
` `$ `C ` ` ` `F zD s-   ) A6AA AA  AA