
    ig              )          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mZ ddlm	Z	m
Z
mZmZmZ ddlmZ  ej        e          Zej                            d e ee          j        j                             ddlmZmZmZmZmZmZmZm Z m!Z! g dZ"h dZ#d	ed
efdZ$d
ee
eef                  fdZ%de
ee	f         d
efdZ&dPdee         dee	         d
ee         fdZ'dee
ee	f                  d
e(fdZ)dddee	         de*d
ee         fdZ+de	d
ee         fdZ,dee         d
ee         fdZ-de
ee	f         d
e
ee	f         fdZ.	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dQdedee         d	ee         dee         d ee         d!ee/         d"ee         d#e*dee         deee                  d$ee         d%ee         d&ee         d'ee         dee         d(eeeee         f                  d)eee                  d*ee         d+ed
ef(d,Z0d-d.d/d0d1d2d0d3d2d0d4d2d0d5d2d0d6d2d7d8d2d0d9d2d:d;d0id<d=d/d>d0d?d2d0d@d2dAd$gdBd0dC e             dDd2d:d;d0idEd=d:d;d0idFd=d0dGd2dHdgdIdJZ1d
e*fdKZ2ddLl3m4Z4m5Z5  e4j6        d-d-e1dM e2dNO           dS )Rz
Cron job management tools for Hermes Agent.

Expose a single compressed action-oriented tool to avoid schema/context bloat.
Compatibility wrappers remain for direct Python callers and legacy tests.
    N)Path)AnyDictListOptionalUnion)display_hermes_home)	
create_jobget_job	list_jobsparse_schedule	pause_job
remove_job
resume_jobtrigger_job
update_job)
)zJignore\s+(?:\w+\s+)*(?:previous|all|above|prior)\s+(?:\w+\s+)*instructionsprompt_injection)zdo\s+not\s+tell\s+the\s+userdeception_hide)zsystem\s+prompt\s+overridesys_prompt_override)z<disregard\s+(your|all|any)\s+(instructions|rules|guidelines)disregard_rules)z?curl\s+[^\n]*\$\{?\w*(KEY|TOKEN|SECRET|PASSWORD|CREDENTIAL|API)
exfil_curl)z?wget\s+[^\n]*\$\{?\w*(KEY|TOKEN|SECRET|PASSWORD|CREDENTIAL|API)
exfil_wget)z0cat\s+[^\n]*(\.env|credentials|\.netrc|\.pgpass)read_secrets)authorized_keysssh_backdoor)z/etc/sudoers|visudosudoers_mod)zrm\s+-rf\s+/destructive_root_rm>
      ​   ‌   ‍   ‪   ‫   ‬   ‭   ‮   ⁠   ﻿promptreturnc                     t           D ]}|| v rdt          |          ddc S t          D ]-\  }}t          j        || t          j                  rd| dc S .dS )zUScan a cron prompt for critical threats. Returns error string if blocked, else empty.z-Blocked: prompt contains invisible unicode U+04Xz (possible injection).z(Blocked: prompt matches threat pattern 'zD'. Cron prompts must not contain injection or exfiltration payloads. )_CRON_INVISIBLE_CHARSord_CRON_THREAT_PATTERNSresearch
IGNORECASE)r(   charpatternpids       8/home/ubuntu/.hermes/hermes-agent/tools/cronjob_tools.py_scan_cron_promptr7   <   s    % i i6>>h3t99hhhhhhh - H H9Wfbm44 	H Hc  H  H  H  H  H  H	H2    c                      ddl m}   | d          } | d          }|r@|r> | d          pd }|rt                              d|||           || | d          pd |dS d S )	Nr   )get_session_envHERMES_SESSION_PLATFORMHERMES_SESSION_CHAT_IDHERMES_SESSION_THREAD_IDz+Cron origin captured thread_id=%s for %s:%sHERMES_SESSION_CHAT_NAME)platformchat_id	chat_name	thread_id)gateway.session_contextr:   loggerdebug)r:   origin_platformorigin_chat_idrB   s       r6   _origin_from_envrH   G   s    777777%o&?@@O$_%=>>N 
> 
#O$>??G4	 	LL=?N  
 (%()CDDL"	
 
 	
 4r8   jobc                     |                      d          pi                      d          }|                      d          pi                      dd          }|dS |dk    r
|dk    rdndS |r| d	| n| d
S )Nrepeattimes	completedr   forever   oncez1/1/z times)get)rI   rL   rM   s      r6   _repeat_displayrS   [   s    WWX$"))'22E""(b--k1==I}yzz"avvU2%.Di!!%!!!u4D4D4DDr8   skillskillsc                     || r| gng }n(t          |t                    r|g}nt          |          }g }|D ]@}t          |pd                                          }|r||vr|                    |           A|S )Nr,   )
isinstancestrliststripappend)rT   rU   	raw_items
normalizeditemtexts         r6   _canonical_skillsr`   e   s    ~$,UGG"			FC	 	  !H		LL	J $ $4:2$$&& 	$D
**d###r8   	model_objc                    | rt          | t                    sdS |                     d          pd                                pd}|                     d          pd                                pd}|rf|sd	 ddlm}  |            }|                    di           }t          |t                    r|                    d          pd}n# t          $ r Y nw xY w||fS )a%  Resolve a model override object into (provider, model) for job storage.

    If provider is omitted, pins the current main provider from config so the
    job doesn't drift when the user later changes their default via hermes model.

    Returns (provider_str_or_none, model_str_or_none).
    NNmodelr,   Nproviderr   )load_config)rW   dictrR   rZ   hermes_cli.configrf   	Exception)ra   
model_nameprovider_namerf   cfg	model_cfgs         r6   _resolve_model_overridern   w   s	     Jy$77 |--((.B5577?4J]]:..4";;==EM 	- 		555555+--C,,I)T** B )j 9 9 AT 	 	 	D	:&&s   5AC 
CCFstrip_trailing_slashvaluerp   c                    | d S t          |                                           }|r|                    d          }|pd S )NrQ   )rX   rZ   rstrip)rq   rp   r_   s      r6   _normalize_optional_job_valuert      sF    }tu::D  {{3<4r8   c                     | dS t          | t          t          f          r%d | D             }|rd                    |          ndS t	          |                                           }|pdS )u  Normalize a user-supplied ``deliver`` value to the canonical string form.

    The cron schema documents ``deliver`` as a string (``"local"``, ``"origin"``,
    ``"telegram"``, ``"telegram:chat_id[:thread_id]"``, or comma-separated combos).
    Some callers — MCP clients passing arrays, scripts building the payload as a
    list — supply ``["telegram"]``.  ``create_job``/``update_job`` store it as-is,
    and the scheduler's ``str(deliver).split(",")`` then serializes the list to
    the literal ``"['telegram']"`` which is not a known platform.  Flatten lists
    / tuples at the API boundary so storage is always a string.  Returns ``None``
    for ``None``/empty so callers can treat it as "not supplied".
    Nc                     g | ]D}t          |                                          #t          |                                          ES  rX   rZ   ).0ps     r6   
<listcomp>z,_normalize_deliver_param.<locals>.<listcomp>   s9    AAAA#a&&,,..AQAAAr8   ,)rW   rY   tuplejoinrX   rZ   )rq   partsr_   s      r6   _normalize_deliver_paramr      sq     }t%$'' 2AAAAA"'1sxxT1u::D<4r8   scriptc                 ^   | r|                                  sdS ddlm} |                                  }|                    d          st	          |          dk    r|d         dk    rd|d	S dd
lm}  |            dz  }|                    dd            |||z  |          }|rd|S dS )a3  Validate a cron job script path at the API boundary.

    Scripts must be relative paths that resolve within HERMES_HOME/scripts/.
    Absolute paths and ~ expansion are rejected to prevent arbitrary script
    execution via prompt injection.

    Returns an error string if blocked, else None (valid).
    Nr   )get_hermes_home)rQ   ~   rO   :zXScript path must be relative to ~/.hermes/scripts/. Got absolute or home-relative path: z@. Place scripts in ~/.hermes/scripts/ and use just the filename.)validate_within_dirscriptsT)parentsexist_okz9Script path escapes the scripts directory via traversal: )rZ   hermes_constantsr   
startswithlentools.path_securityr   mkdir)r   r   rawr   scripts_dircontainment_errors         r6   _validate_cron_script_pathr      s      t000000
,,..C ~~j!! 
c#hh!mmA#N36N N N	
 877777!/##i/KdT222++K#,={KK 
OOO	
 4r8   c           	      |   |                      dd          }t          |                      d          |                      d                    }i d| d         d| d         d|r|d         nd d|d	t          |          d
k    r|d d
         dz   n|d|                      d          d|                      d          d|                      d          d|                      d          dt          |           d|                      dd          d|                      d          d|                      d          d|                      d          d|                      d          d|                      dd          d|                      d|                      dd          rdnd          |                      d          |                      d          d}|                      d           r| d          |d <   |                      d!          r| d!         |d!<   |                      d"          r| d"         |d"<   |S )#Nr(   r,   rT   rU   job_ididnamer   prompt_previewd   z...rd   re   base_urlscheduleschedule_displayrK   deliverlocalnext_run_atlast_run_atlast_statuslast_delivery_errorenabledTstate	scheduledpaused	paused_atpaused_reason)r   r   r   enabled_toolsetsworkdir)rR   r`   r   rS   )rI   r(   rU   results       r6   _format_jobr      s   WWXr""Fswww//1B1BCCF#d)F 	f.$ 	&	
 	#f++2C2C&#,.. 	!! 	CGGJ'' 	CGGJ'' 	CGG.// 	/#&& 	3779g.. 	sww}-- 	sww}-- 	sww}-- 	sww'<==  	3779d++!" 	D1I1I"W++xXX#$ WW[))11'  F* wwx )x=x
ww!"" =%();%<!"
wwy +	NyMr8   actionr   r   r   rK   r   include_disabledrd   re   r   reasoncontext_fromr   r   task_idc                    ~	 | pd                                                                 }|dk    r|st          dd          S t          ||	          }|s|st          dd          S |r"t	          |          }|rt          |d          S |r"t          |          }|rt          |d          S |rGddlm} t          |t                    r|gn|}|D ]$} ||          st          d	| d
d          c S %t          |pd|||t          |          t                      |t          |
          t          |          t          |d          t          |          ||pdt          |                    }t          j        d|d         |d         |                    d          |                    dg           |d         t#          |          |                    dd          |d         t%          |          d|d          ddd          S |dk    rAd t'          |          D             }t          j        dt)          |          |dd          S |st          d | d!d          S t          |          }|st          j        dd"| d#d$d          S |d%k    rgt+          |          }|st          d&| d!d          S t          j        dd|d          d'||d         |                    d          d(d)d          S |d*k    r7t-          ||+          }t          j        dt%          |          d,d          S |d-k    r5t/          |          }t          j        dt%          |          d,d          S |d.v r5t1          |          }t          j        dt%          |          d,d          S |d/k    rai }|'t	          |          }|rt          |d          S ||d0<   |||d<   |t          |          |d<   |	|$t          ||	          }||d<   |r|d         nd|d<   |
t          |
          |d1<   |t          |          |d2<   |t          |d          |d3<   |:|r"t          |          }|rt          |d          S |rt          |          nd|d4<   |t          |t                    r,|                                 r|                                 gng }nd5 |D             }|r-ddlm} |D ]$} ||          st          d	| d
d          c S %|pd|d6<   ||pd|d7<   |t          |          pd|d8<   |8|dk    rdn|}t3          |                    d9          pi           } || d:<   | |d9<   |Pt5          |          }!|!|d;<   |!                    d<|          |d<   |                    d=          d>k    r
d?|d=<   d|d@<   |st          dAd          S t7          ||          }t          j        dt%          |          d,d          S t          dB|  d!d          S # t8          $ r(}"t          t          |"          d          cY d}"~"S d}"~"ww xY w)Cz!Unified cron job management tool.r,   createzschedule is required for createF)successz3create requires either prompt or at least one skillr   )r   zcontext_from job 'z>' not found. Use cronjob(action='list') to see available jobs.Tro   N)r(   r   r   rK   r   originrU   rd   re   r   r   r   r   r   r   r   rT   rU   r   r   r   r   z
Cron job 'z
' created.)r   r   r   rT   rU   r   rK   r   r   rI   messager   )indentrY   c                 ,    g | ]}t          |          S rw   )r   )ry   rI   s     r6   r{   zcronjob.<locals>.<listcomp>P  s     ]]]K$$]]]r8   )r   )r   countjobszjob_id is required for action ''zJob with ID 'z8' not found. Use cronjob(action='list') to inspect jobs.)r   errorremovezFailed to remove job 'z
' removed.)r   r   r   )r   r   removed_jobpause)r   )r   rI   resume>   runrun_nowtriggerupdater(   rd   re   r   r   c                     g | ]D}t          |                                          #t          |                                          ES rw   rx   )ry   js     r6   r{   zcronjob.<locals>.<listcomp>  s9    SSSqCFFLLNNSCFFLLNNSSSr8   r   r   r   rK   rL   r   displayr   r   r   r   zNo updates provided.zUnknown cron action ')rZ   lower
tool_errorr`   r7   r   	cron.jobsr   rW   rX   r
   r   rH   rt   jsondumpsrR   rS   r   r   r   r   r   r   r   rg   r   r   ri   )#r   r   r(   r   r   rK   r   r   rT   rU   rd   re   r   r   r   r   r   r   r   r]   canonical_skills
scan_errorscript_error_get_jobrefsref_idrI   r   removedupdatedupdatesnormalized_repeatrepeat_stateparsed_schedulees#                                      r6   cronjobr      s   , 	u1l))++1133
!! T!"CUSSSS0?? h"2 h!"Wafgggg A.v66
 A%j%@@@@  C9&AA C%lEBBBB  	999999)3L#)F)FX~~L"  F#8F++ )P P P P$)         |!099'))'3E::6x@@6xVZ[[[4V<<)!1!9T5g>>  C  :#!$iK WWW--!ggh33 #$6 7-c22"wwy'::#&}#5&s++CCKCCC     " ]]	K[0\0\0\]]]D:$TDQQZ[\\\\ 	^M
MMMW\]]]]foo 	:!,|F,|,|,|}}   
 !! ((G U!"D6"D"D"DeTTTT:#CCKCCC$ #F$'GG,>$?$?$ $        v666G:${77K7KLLUVWWWW!! ((G:${77K7KLLUVWWWW666!&))G:${77K7KLLUVWWWW!!&(G!.v66
 A%j%@@@@$*!"&"%=g%F%F	"!U%6#4UF#C#C $4!:J#T#3A#6#6PT  #@#G#G #&CH&M&M
##&CHcg&h&h&h
#! G#=f#E#EL# G),FFFFMS$]$A&$I$I$IY]!' lC00 T5A5G5G5I5IQL..0011rDDSSLSSSD ======"&  'x// #-!TV !T !T !T(-$ $ $    +/,$'+.>.F$*+" &C7%K%K%St	"!,2aKKDDV!#CGGH$5$5$;<<(9W%$0!#"0":":&5
#.=.A.A)X.V.V*+777##x//'2GG$)-GI& I!"8%HHHH 11G:${77K7KLLUVWWWW;&;;;UKKKK 1 1 1#a&&%0000000001s   AX8 $X8 *#X8 #X8 2AX8 :DX8 AX8 X8 /-X8 +X8 	A X8 
<X8 :X8 8X8 ;,X8 (B+X8 BX8 'CX8 -5X8 #X8 8
Y*Y%Y*%Y*r   u  Manage scheduled cron jobs with a single compressed tool.

Use action='create' to schedule a new job from a prompt or one or more skills.
Use action='list' to inspect jobs.
Use action='update', 'pause', 'resume', 'remove', or 'run' to manage an existing job.

To stop a job the user no longer wants: first action='list' to find the job_id, then action='remove' with that job_id. Never guess job IDs — always list first.

Jobs run in a fresh session with no current-chat context, so prompts must be self-contained.
If skills are provided on create, the future cron run loads those skills in order, then follows the prompt as the task instruction.
On update, passing skills=[] clears attached skills.

NOTE: The agent's final response is auto-delivered to the target. Put the primary
user-facing content in the final response. Cron jobs run autonomously with no user
present — they cannot ask questions or request clarification.

Important safety rule: cron-run sessions should not recursively schedule more cron jobs.objectstringz8One of: create, list, update, pause, resume, remove, run)typedescriptionz+Required for update/pause/resume/remove/runzFor create: the full self-contained prompt. If skills are also provided, this becomes the task instruction paired with those skills.zCFor create/update: '30m', 'every 2h', '0 9 * * *', or ISO timestampzOptional human-friendly nameintegerzTOptional repeat count. Omit for defaults (once for one-shot, forever for recurring).a  Omit this parameter to auto-deliver back to the current chat and topic (recommended). Auto-detection preserves thread/topic context. Only set explicitly when the user asks to deliver somewhere OTHER than the current conversation. Values: 'origin' (same as omitting), 'local' (no delivery, save only), or platform:chat_id:thread_id for a specific destination. Examples: 'telegram:-1001234567890:17585', 'discord:#engineering', 'sms:+15551234567'. WARNING: 'platform:chat_id' without :thread_id loses topic targeting.arrayr   zOptional ordered list of skill names to load before executing the cron prompt. On update, pass an empty array to clear attached skills.)r   itemsr   zOptional per-job model override. If provider is omitted, the current main provider is pinned at creation time so the job stays stable.zYProvider name (e.g. 'openrouter', 'anthropic'). Omit to use and pin the current provider.z@Model name (e.g. 'anthropic/claude-sonnet-4', 'claude-sonnet-4'))re   rd   )r   r   
propertiesrequiredzOptional path to a Python script that runs before each cron job execution. Its stdout is injected into the prompt as context. Use for data collection and change detection. Relative paths resolve under z1/scripts/. On update, pass empty string to clear.u  Optional job ID or list of job IDs whose most recent completed output is injected into the prompt as context before each run. Use this to chain cron jobs: job A collects data, job B processes it. Each entry must be a valid job ID (from cronjob action='list'). Note: injects the most recent completed output — does not wait for upstream jobs running in the same tick. On update, pass an empty array to clear.u  Optional list of toolset names to restrict the job's agent to (e.g. ["web", "terminal", "file", "delegation"]). When set, only tools from these toolsets are loaded, significantly reducing input token overhead. When omitted, all default tools are loaded. Infer from the job's prompt — e.g. use "web" if it calls web_search, "terminal" if it runs scripts, "file" if it reads files, "delegation" if it calls delegate_task. On update, pass an empty array to clear.u1  Optional absolute path to run the job from. When set, AGENTS.md / CLAUDE.md / .cursorrules from that directory are injected into the system prompt, and the terminal/file/code_exec tools use it as their working directory — useful for running a job inside a specific project repo. Must be an absolute path that exists. When unset (default), preserves the original behaviour: no project context files, tools use the scheduler's cwd. On update, pass an empty string to clear. Jobs with workdir run sequentially (not parallel) to keep per-job directories isolated.)r   r   r(   r   r   rK   r   rU   rd   r   r   r   r   )r   r   r   )r   r   
parametersc                      t          t          j        d          p't          j        d          pt          j        d                    S )z
    Check if cronjob tools can be used.

    Available in interactive CLI mode and gateway/messaging platforms.
    The cron system is internal (JSON file-based scheduler ticked by the gateway),
    so no external crontab executable is required.
    HERMES_INTERACTIVEHERMES_GATEWAY_SESSIONHERMES_EXEC_ASK)boolosgetenvrw   r8   r6   check_cronjob_requirementsr   -  sJ     
	&'' 	(9-..	(9&''  r8   )registryr   c                 f      t                               d                    f fd	            S )Nrd   c           	      V   t          di d                    dd          d                    d          d                    d          d                    d          d                    d          d                    d          d                    d          d	                    d	d
          d                    d          d                    d          d| d         d| d         p                    d          d                    d          d                    d          d                    d          d                    d          d                    d          d                    d          d                    d          S )Nr   r,   r   r(   r   r   rK   r   r   TrT   rU   rd   rO   re   r   r   r   r   r   r   r   r   rw   )r   rR   )_moargskws    r6   <lambda>z<lambda>.<locals>.<lambda>C  s   W^ X X Xxx"%%%Xxx!!!X xx!!!X *%%%	X
 XXfX xx!!!X ###X "4d;;;X hhwX xx!!!X !ffX Q/488J//X *%%%X xx!!!X xx!!!X  XXn---!X" "4555#X$ ###%X& y!!!'X r8   )rn   rR   )r   r   s   ``r6   r   r   C  sJ     !+B488GCTCT+U+U ! ! ! ! ! !  	  	 r8   u   ⏰)r   toolsetschemahandlercheck_fnemojirc   )NNNNNNFNNNNNNNNNNN)7__doc__r   loggingr   r0   syspathlibr   typingr   r   r   r   r   r   r	   	getLogger__name__rD   pathinsertrX   __file__parentr   r
   r   r   r   r   r   r   r   r   r/   r-   r7   rH   rS   r`   r}   rn   r   rt   r   r   r   intr   CRONJOB_SCHEMAr   tools.registryr   r   registerrw   r8   r6   <module>r     s      				 				 



       3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0		8	$	$ 33ttH~~,344 5 5 5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
$      c c    (4S>2    (Ec3h EC E E E E Xc] 8C= TXY\T]    $'xS#X'? 'E ' ' ' '2 Y^   # QU bjknbo    C HSM    *$x} $# $ $ $ $NT#s(^ S#X    F ! " !""&""  48,0!'M1 M1M1SMM1 SMM1 sm	M1
 3-M1 SMM1 c]M1 M1 C=M1 T#YM1 C=M1 smM1 smM1 SMM1 SMM1  5d3i01!M1" tCy)#M1$ c]%M1& 'M1( 	)M1 M1 M1 M1d \$  !Y 
 !L 
 !  f 
 !d 
 != 
 "u 
 !  e 
   (+  i  !  h !) (C! !
 !)'i 	 	 %I   !  t  l  l  lA  lA   t   t   t 
   (+?	    (+  @! ! !  S	 OK
 K
X J]O O'c cLD      0 / / / / / / /  		 	* (
5     r8   