
    iQ              #       p   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 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%dId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e         d
ee         fdZ+de
ee	f         d
e
ee	f         fdZ,	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dJd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d
ef"d(Z.d)d*d+d,d-d.d,d/d.d,d0d.d,d1d.d,d2d.d3d4d.d,d5d.d6d7d,id8d9d+d:d,d;d.d,d<d.d=d#gd>d,d? e             d@d.dA
dgdBdCZ/d
e)fdDZ0ddEl1m2Z2m3Z3  e2j4        d)d)e/dF e0dGH           dS )Kz
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)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       ;/home/agentuser/.hermes/hermes-agent/tools/cronjob_tools.py_scan_cron_promptr6   <   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_contextr9   loggerdebug)r9   origin_platformorigin_chat_idrA   s       r5   _origin_from_envrG   G   s    777777%o&?@@O$_%=>>N 
> 
#O$>??G4	 	LL=?N  
 (%()CDDL"	
 
 	
 4r7   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)rH   rK   rL   s      r5   _repeat_displayrR   [   s    WWX$"))'22E""(b--k1==I}yzz"avvU2%.Di!!%!!!u4D4D4DDr7   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)rS   rT   	raw_items
normalizeditemtexts         r5   _canonical_skillsr_   e   s    ~$,UGG"			FC	 	  !H		LL	J $ $4:2$$&& 	$D
**d###r7   	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)rV   dictrQ   rY   hermes_cli.configre   	Exception)r`   
model_nameprovider_namere   cfg	model_cfgs         r5   _resolve_model_overriderm   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valuero   c                    | d S t          |                                           }|r|                    d          }|pd S )NrP   )rW   rY   rstrip)rp   ro   r^   s      r5   _normalize_optional_job_valuers      sF    }tu::D  {{3<4r7   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)rP   ~   rN   :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: )rY   hermes_constantsrv   
startswithlentools.path_securityrz   mkdir)rt   rv   rawrz   scripts_dircontainment_errors         r5   _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	
 4r7   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 <   |S )!Nr'   r+   rS   rT   job_ididnamer   prompt_previewd   z...rc   rd   base_urlscheduleschedule_displayrJ   deliverlocalnext_run_atlast_run_atlast_statuslast_delivery_errorenabledTstate	scheduledpaused	paused_atpaused_reason)r   r   rt   )rQ   r_   r   rR   )rH   r'   rT   results       r5   _format_jobr      sE   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Mr7   actionr   r   r   rJ   r   include_disabledrc   rd   r   reasontask_idc                 
   ~	 | pd                                                                 }|dk    rw|st          dd          S t          ||	          }|s|st          dd          S |r"t	          |          }|rt          |d          S |r"t          |          }|rt          |d          S t          |pd||||t                      |t          |
          t          |          t          |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 r5t)          |          }t          j
        dt          |          d'd          S |d*k    ri }|'t	          |          }|rt          |d          S ||d,<   |||d<   |||d<   |	|$t          ||	          }||d<   |r|d-         nd+|d<   |
t          |
          |d.<   |t          |          |d/<   |t          |d          |d0<   |:|r"t          |          }|rt          |d          S |rt          |          nd+|d1<   |8|d-k    rd+n|}t+          |                    d2          pi           }||d3<   ||d2<   |Pt-          |          }||d4<   |                    d5|          |d<   |                    d6          d7k    r
d8|d6<   d|d9<   |st          d:d          S t/          ||          }t          j
        dt          |          d'd          S t          d;|  dd          S # t0          $ r(}t          t3          |          d          cY d+}~S d+}~ww xY w)<z!Unified cron job management tool.r+   createzschedule is required for createF)successz3create requires either prompt or at least one skillTrn   )r'   r   r   rJ   r   originrT   rc   rd   r   rt   r   r   rS   rT   r   r   r   r   z
Cron job 'z
' created.)r   r   r   rS   rT   r   rJ   r   r   rH   messagerx   )indentrX   c                 ,    g | ]}t          |          S  )r   ).0rH   s     r5   
<listcomp>zcronjob.<locals>.<listcomp>%  s     ]]]K$$]]]r7   )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   rH   resume>   runrun_nowtriggerupdateNr'   r   rc   rd   r   rt   rJ   rK   r   displayr   r   r   r   zNo updates provided.zUnknown cron action ')rY   lower
tool_errorr_   r6   r   r	   rG   rs   jsondumpsrQ   rR   r   r   r   r
   r   r   r   r   rf   r   r   rh   rW   )r   r   r'   r   r   rJ   r   r   rS   rT   rc   rd   r   r   rt   r   r\   canonical_skills
scan_errorscript_errorrH   r   removedupdatedupdatesnormalized_repeatrepeat_stateparsed_schedulees                                r5   cronjobr      s   & 	N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|!'))'3E::6x@@6xVZ[[[4V<<  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%@@@@$*!"&"%,	"!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]!!,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T $T *#T #T 2C6T )AT 0T -T 5+T !A T "<T :T 8T ,T  BT B4T 5T 
T 
U)UUU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'))rd   rc   )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.)
r   r   r'   r   r   rJ   r   rT   rc   rt   )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getenvr   r7   r5   check_cronjob_requirementsr     sJ     
	&'' 	(9-..	(9&''  r7   )registryr   c                 f      t                               d                    f fd	            S )Nrc   c           	         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          S )Nr   r+   r   r'   r   r   rJ   r   r   TrS   rT   rc   rN   rd   r   r   r   rt   r   r   )r   rQ   )_moargskws    r5   <lambda>z<lambda>.<locals>.<lambda>  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  y!!!!X r7   )rm   rQ   )r   r   s   ``r5   r   r     sJ     !+B488GCTCT+U+U ! ! ! ! ! !  	  	 r7   u   ⏰)r   toolsetschemahandlercheck_fnemojirb   )NNNNNNFNNNNNNNN)5__doc__r   loggingr   r/   syspathlibr   typingr   r   r   r   r~   r   	getLogger__name__rC   pathinsertrW   __file__parent	cron.jobsr	   r
   r   r   r   r   r   r   r   r.   r,   r6   rG   rR   r_   tuplerm   r   rs   r   r   intr   CRONJOB_SCHEMAr   tools.registryr   r   registerr   r7   r5   <module>r      s      				 				 



       , , , , , , , , , , , , 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    $x} $# $ $ $ $NT#s(^ S#X    > ! " !""&""  !c1 c1c1SMc1 SMc1 sm	c1
 3-c1 SMc1 c]c1 c1 C=c1 T#Yc1 C=c1 smc1 smc1 SMc1 SMc1  !c1" 	#c1 c1 c1 c1P \$  !Y 
 !L 
 !  f 
 !d 
 != 
 "u 
 !  e 
   (+  i  !  h !) (C! !
 !)'i 	 	 %I   !  t  l  l  lA  lA   t   t   t c5
 5
l Jq9 9'M M`D      0 / / / / / / /  		 	$ (
/     r7   