
    ix              +       .   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&dTd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.	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dUd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e*         d,ed
ef*d-Z0d.d/d0d1d2d3d1d4d3d1d5d3d1d6d3d1d7d3d8d9d3d1d:d3d;d<d1id=d>d0d?d1d@d3d1dAd3dBd$gdCd1dD e             dEd3dFddGdHd;d<d1idId>d;d<d1idJd>d1dKd3dLdgdMdNZ1d
e*fdOZ2ddPl3m4Z4m5Z5  e4j6        d.d.e1dQ e2dRS           dS )Vz
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/piyush/.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}|dk    r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providercustomr   )load_config)rW   dictrR   rZ   hermes_cli.configrg   	Exception)ra   
model_nameprovider_namerg   cfg	model_cfgs         r6   _resolve_model_overridero   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   =AC 
CCFstrip_trailing_slashvaluerq   c                    | d S t          |                                           }|r|                    d          }|pd S )NrQ   )rX   rZ   rstrip)rr   rq   r_   s      r6   _normalize_optional_job_valueru      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   )rr   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"<   |                      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   no_agent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
wwz "!z
ww!"" =%();%<!"
wwy +	NyMr8   actionr   r   r   rK   r   include_disabledrd   re   r   reasoncontext_fromr   r   r   task_idc                    ~	 | pd                                                                 }|dk    r|st          dd          S t          ||	          }t	          |          }|r|st          dd          S n|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    r5t1          |          }t          j        dt'          |          d-d          S |d/v r5t3          |          }t          j        dt'          |          d-d          S |d0k    ri } |'t          |          }|rt          |d          S || d1<   ||| d<   |t          |          | d<   |	|$t          ||	          }|| d<   |r|d         nd| d<   |
t          |
          | d2<   |t          |          | d3<   |t          |d          | d4<   |:|r"t          |          }|rt          |d          S |rt          |          nd| d5<   |t          |t                    r,|                                 r|                                 gng }nd6 |D             }|r-dd	lm} |D ]$} ||          st          d
| dd          c S %|pd| d7<   ||pd| d8<   |t          |          pd| d9<   |Wt	          |          }!|!rAd5| v r|                     d5          n|                    d5          }"|"st          d:d          S |!| d;<   |8|dk    rdn|}#t5          |                    d<          pi           }$|#|$d=<   |$| d<<   |Pt7          |          }%|%| d><   |%                    d?|          | d<   |                    d@          dAk    r
dB| d@<   d| dC<   | st          dDd          S t9          ||           }t          j        dt'          |          d-d          S t          dE|  d"d          S # t:          $ r(}&t          t          |&          d          cY d}&~&S d}&~&ww xY w)Fz!Unified cron job management tool.r,   createzschedule is required for createF)successuF   create with no_agent=True requires a script — the script is the job.z3create requires either prompt or at least one skillr   )r   zcontext_from job 'z>' not found. Use cronjob(action='list') to see available jobs.Trp   N)r(   r   r   rK   r   originrU   rd   re   r   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 rx   )r   )rz   rI   s     r6   r|   zcronjob.<locals>.<listcomp>k  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 rx   ry   )rz   js     r6   r|   zcronjob.<locals>.<listcomp>  s9    SSSqCFFLLNNSCFFLLNNSSSr8   r   r   r   ziCannot set no_agent=True on a job without a script. Set `script` in the same update, or on the job first.r   rK   rL   r   displayr   r   r   r   zNo updates provided.zUnknown cron action ')rZ   lower
tool_errorr`   boolr7   r   	cron.jobsr   rW   rX   r
   r   rH   ru   jsondumpsrR   rS   r   r   r   r   r   r   r   rh   r   r   rj   )'r   r   r(   r   r   rK   r   r   rT   rU   rd   re   r   r   r   r   r   r   r   r   r]   canonical_skills	_no_agent
scan_errorscript_error_get_jobrefsref_idrI   r   removedupdatedupdatestarget_no_agenteffective_scriptnormalized_repeatrepeat_stateparsed_schedulees'                                          r6   cronjobr     s{	   . 	R1l))++1133
!! T!"CUSSSS0??XI  	l %1 %     l&6 l%&[ejkkkk 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	"# #'x.." @HG@S@Sw{{8'<'<'<Y\Y`Y`aiYjYj$+ )T$)      
 '6
#!,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Z7 3Z7 9Z7 #Z7 3#Z7 AZ7 DZ7 7AZ7 >Z7 -Z7 +Z7 /A Z7 0<Z7 -:Z7 (8Z7 !,Z7 B+Z7 :BZ7 A:Z7 B#Z7 ,5Z7 "Z7 7
[)[$[)$[)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), 'all' (fan out to every connected home channel), or platform:chat_id:thread_id for a specific destination. Combine with comma: 'origin,all' delivers to the origin plus every other connected channel. Examples: 'telegram:-1001234567890:17585', 'discord:#engineering', 'sms:+15551234567', 'all'. WARNING: 'platform:chat_id' without :thread_id loses topic targeting. 'all' resolves at fire time, so a job created before a channel was wired up will pick it up automatically once connected.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.u   Provider name (e.g. 'openrouter', 'anthropic', or 'custom:<name>' for a provider defined in custom_providers config — always include the ':<name>' suffix, never pass the bare 'custom'). 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requireda3  Optional path to a script that runs each tick. In the default mode its stdout is injected into the agent's prompt as context (data-collection / change-detection pattern). With no_agent=True, the script IS the job and its stdout is delivered verbatim (classic watchdog pattern). Relative paths resolve under zx/scripts/. ``.sh``/``.bash`` extensions run via bash, everything else via Python. On update, pass empty string to clear.booleanu_  Default: False (LLM-driven job — the agent runs the prompt each tick). Set True to skip the LLM entirely: the scheduler just runs ``script`` on schedule and delivers its stdout verbatim. No tokens, no agent loop, no model override honoured. 

REQUIREMENTS when True: ``script`` MUST be set (``prompt`` and ``skills`` are ignored). 

DELIVERY SEMANTICS when True: (a) non-empty stdout is sent verbatim as the message; (b) EMPTY stdout means SILENT — nothing is sent to the user and they won't see anything happened, so design your script to stay quiet when there's nothing to report (the watchdog pattern); (c) non-zero exit / timeout sends an error alert so a broken watchdog can't fail silently. 

WHEN TO USE True: recurring script-only pings where the script itself produces the exact message text (memory/disk/GPU watchdogs, threshold alerts, heartbeats, CI notifications, API pollers with a fixed output shape). WHEN TO USE False (default): anything that needs reasoning — summarize a feed, draft a daily briefing, pick interesting items, rephrase data for a human, follow conditional logic based on content.)r   defaultr   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   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)r   osgetenvrx   r8   r6   check_cronjob_requirementsr   h  sJ     
	&'' 	(9-..	(9&''  r8   )registryr   c                 f      t                               d                    f fd	            S )Nrd   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          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   r   rx   )r   rR   )_moargskws    r6   <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  XXn---!X" "4555#X$ ###%X& *%%%'X( y!!!)X r8   )ro   rR   )r   r   s   ``r6   r   r   ~  sJ     !+B488GCTCT+U+U ! ! ! ! ! !  	  	 r8   u   ⏰)r   toolsetschemahandlercheck_fnemojirc   )NNNNNNFNNNNNNNNNNNN)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~   ro   r   ru   r   r   r   intr   CRONJOB_SCHEMAr   tools.registryr   r   registerrx   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 ' ' ' 'D Y^   # QU bjknbo    C HSM    *$x} $# $ $ $ $N T#s(^  S#X        J ! " !""&""  48,0!#)k1 k1k1SMk1 SMk1 sm	k1
 3-k1 SMk1 c]k1 k1 C=k1 T#Yk1 C=k1 smk1 smk1 SMk1 SMk1  5d3i01!k1" tCy)#k1$ c]%k1& tn'k1( )k1* 	+k1 k1 k1 k1` \$  !Y 
 !L 
 !  f 
 !d 
 != 
 "u 
 !  s 
   (+  i  !  h !) (O! !
 !)'i 	 	 %I   !  e  Vi  Vi  Vk  Vk   e   e   e 
 " ]	 &   (+?	    (+  @! ! !  S	 s]
 ]
| JAa a'u upD      0 / / / / / / /  		 	, (
7     r8   