Skip to content

The agent that grows with you

refactor(tools): compact cronjob tool modules — fold pause/resume into dispatch table, inline gateway-loop lookup, drop dead re-exports, tighten docs

Teknium34789f79f76e
diff --git a/tools/cronjob_job_args.py b/tools/cronjob_job_args.py
index 70e029a28f2..80ec1ef32ce 100644
--- a/tools/cronjob_job_args.py
+++ b/tools/cronjob_job_args.py
@@ -1,5 +1,5 @@
-"""Cron job argument normalization, validation and result shaping
-(extracted from tools/cronjob_tools.py; re-exported there)."""
+"""Cron job argument normalization, validation and result shaping (re-exported by
+tools/cronjob_tools.py)."""
import logging
from typing import Any, Dict, List, Optional, Union
@@ -17,48 +17,37 @@ def _origin_from_env() -> Optional[Dict[str, str]]:
if not (origin_platform and origin_chat_id):
return None
thread_id = get_session_env("HERMES_SESSION_THREAD_ID") or None
- # Slack stamps every TOP-LEVEL message's own id as the session thread (a
- # per-message session KEY, not a conversation location). Persisting it
- # would pin all future deliveries inside an ephemeral thread, so a thread
- # id equal to the creating message's id is synthetic and dropped; a real
- # in-thread creation (thread == parent's id != this message) keeps it.
+ # Slack stamps every TOP-LEVEL message's own id as the session thread (a per-message
+ # KEY, not a location); persisting it would pin all future deliveries inside an
+ # ephemeral thread, so thread == creating message id is synthetic and dropped.
if thread_id and origin_platform == "slack":
message_id = get_session_env("HERMES_SESSION_MESSAGE_ID") or None
if message_id and str(thread_id) == str(message_id):
logger.debug(
"Cron origin: dropping synthetic per-message Slack "
- "thread_id=%s (== creation message id)", thread_id,
- )
+ "thread_id=%s (== creation message id)", thread_id)
thread_id = None
if thread_id:
logger.debug(
"Cron origin captured thread_id=%s for %s:%s",
- thread_id, origin_platform, origin_chat_id,
- )
+ thread_id, origin_platform, origin_chat_id)
return {
"platform": origin_platform,
"chat_id": origin_chat_id,
"chat_name": get_session_env("HERMES_SESSION_CHAT_NAME") or None,
"thread_id": thread_id,
- # Lets an opt-in delivery mirror resolve the exact participant's
- # session in per-user-isolated group chats (parity with send_message).
- "user_id": get_session_env("HERMES_SESSION_USER_ID") or None, # harmless for DMs
- # Workspace/server scope (Slack team, Discord guild...). Slack session
- # keys embed it, so a continuable cron seed built without it would
- # create a row no scoped reply ever resolves to.
+ # Lets a delivery mirror resolve the participant's session in per-user-isolated groups.
+ "user_id": get_session_env("HERMES_SESSION_USER_ID") or None,
+ # Workspace/server scope (Slack team, Discord guild...): Slack session keys embed it,
+ # so a continuable cron seed built without it would never resolve a scoped reply.
"scope_id": get_session_env("HERMES_SESSION_SCOPE_ID") or None,
}
def _local_delivery_notice(job: Dict[str, Any], user_deliver: Optional[str]) -> Optional[str]:
- """Notice when a created job won't deliver anywhere.
-
- CLI/TUI sessions have no capturable origin, so deliver='origin' (or an
- omitted deliver) yields a job whose output is saved but never delivered.
- Surface that at create time rather than silently dropping the user's
- "tell me when it runs" intent. None when the user explicitly asked for
- ``local`` or the job resolves to a real target.
- """
+ """Notice when a created job won't deliver anywhere: CLI/TUI sessions have no capturable
+ origin, so deliver='origin' (or omitted) saves output but never delivers it. None when the
+ user explicitly asked for ``local`` or the job resolves to a real target."""
if (user_deliver or "").strip().lower() == "local":
return None
try:
@@ -66,8 +55,7 @@ def _local_delivery_notice(job: Dict[str, Any], user_deliver: Optional[str]) ->
if _resolve_delivery_targets(job):
return None
- except Exception:
- # Resolution unavailable — fall back to the origin signal.
+ except Exception: # resolution unavailable — fall back to the origin signal
if job.get("origin"):
return None
return (
@@ -75,13 +63,12 @@ def _local_delivery_notice(job: Dict[str, Any], user_deliver: Optional[str]) ->
"cronjob(action='list')) but will NOT be delivered back into this "
"session — CLI/TUI sessions have no live-delivery channel. To be "
"notified when it runs, recreate or update the job with deliver set to "
- "a gateway-connected platform, e.g. deliver='telegram' or deliver='all'."
- )
+ "a gateway-connected platform, e.g. deliver='telegram' or deliver='all'.")
def _mode_guidance_notes(job: Dict[str, Any], user_deliver: Optional[str]) -> List[str]:
- """Mode-specific guidance echoed once in the create/update response
- (instead of in the schema, which is paid for on every API call)."""
+ """Mode guidance echoed once in the create/update response (not in the schema, which is
+ paid for on every API call)."""
notes: List[str] = []
if job.get("monitor_script") or job.get("monitor_url"):
notes.append(
@@ -90,28 +77,22 @@ def _mode_guidance_notes(job: Dict[str, Any], user_deliver: Optional[str]) -> Li
"(silent no_change tick), changed output injects a MONITOR CHANGE "
"DETECTED diff into the prompt. The first tick always runs as "
"baseline. The source must emit STABLE output (no timestamps, no "
- "random ordering) or every tick will look changed."
- )
+ "random ordering) or every tick will look changed.")
if job.get("no_agent"):
notes.append(
"no_agent mode: stdout is delivered verbatim; EMPTY stdout sends "
"nothing at all (watchdog pattern — script should stay quiet when "
"there is nothing to report). Non-zero exit or timeout sends an "
- "error alert. prompt/skills are ignored."
- )
+ "error alert. prompt/skills are ignored.")
_deliver = (user_deliver or "").strip().lower()
if _deliver:
if "all" in _deliver.split(","):
notes.append(
"deliver='all' resolves at fire time and never includes "
"bot-chat targets — channels connected later are picked up "
- "automatically."
- )
+ "automatically.")
if _deliver.startswith("bot-chat:"):
- notes.append(
- "Targeting another profile's Bot Chat costs that bot an agent "
- "turn per run."
- )
+ notes.append("Targeting another profile's Bot Chat costs that bot an agent turn per run.")
# platform:chat_id with no thread segment loses topic targeting.
for target in _deliver.split(","):
parts = target.strip().split(":")
@@ -119,13 +100,11 @@ def _mode_guidance_notes(job: Dict[str, Any], user_deliver: Optional[str]) -> Li
len(parts) == 2
and parts[0] not in ("bot-chat", "sms")
and parts[1]
- and not parts[1].startswith("#")
- ):
+ and not parts[1].startswith("#")):
notes.append(
f"deliver target '{target.strip()}' has no :thread_id "
"segment — on thread/topic platforms the delivery lands in "
- "the main chat, not a topic."
- )
+ "the main chat, not a topic.")
break
return notes
@@ -133,18 +112,12 @@ def _mode_guidance_notes(job: Dict[str, Any], user_deliver: Optional[str]) -> Li
def _split_monitor_arg(
monitor: Optional[str],
monitor_script: Optional[str],
- monitor_url: Optional[str],
-) -> tuple:
- """Resolve the single model-facing ``monitor`` field into the stored
- ``(monitor_script, monitor_url)`` pair.
-
- Shape decides transport: http(s):// is a URL, anything else a script path
- (a legal script path can never start with a URL scheme). Storage keeps the
- two fields separate — interface merge, not a storage migration.
- Update semantics: None = unchanged, '' = clear; setting one source clears
- the other so switching transports never trips mutual exclusion. An
- explicit ``monitor`` wins over the legacy alias fields.
- """
+ monitor_url: Optional[str]) -> tuple:
+ """Resolve the model-facing ``monitor`` field into the stored ``(monitor_script,
+ monitor_url)`` pair. http(s):// is a URL, anything else a script path (a legal script path
+ never starts with a URL scheme). None = unchanged, '' = clear; setting one source clears
+ the other so switching transports never trips mutual exclusion; an explicit ``monitor``
+ wins over the legacy alias fields."""
if monitor is None:
return monitor_script, monitor_url
value = monitor.strip()
@@ -193,12 +166,8 @@ def _normalize_optional_job_value(value: Optional[Any], *, strip_trailing_slash:
def _normalize_deliver_param(value: Any) -> Optional[str]:
- """Canonical string form of ``deliver``; None for None/empty.
-
- MCP clients / scripts may pass a list (``["telegram"]``); stored as-is the
- scheduler's ``str(deliver).split(",")`` would yield the literal
- ``"['telegram']"``. Flatten at the API boundary.
- """
+ """Canonical string form of ``deliver``; None for None/empty. MCP clients may pass a list
+ (``["telegram"]``) which the scheduler's ``str(deliver).split(",")`` would mangle."""
if value is None:
return None
if isinstance(value, (list, tuple)):
@@ -207,13 +176,9 @@ def _normalize_deliver_param(value: Any) -> Optional[str]:
def _validate_bot_chat_deliver(deliver: Optional[str]) -> Optional[str]:
- """Validate ``bot-chat[:<profile>]`` deliver elements at create time.
-
- Bot Chat delivery is machine-local: the profile must exist where the
- scheduler fires (Desktop multi-gateway rosters may show same-named profiles
- from other machines). Fail loudly here rather than as a per-run delivery error.
- Returns an error string or None.
- """
+ """Validate ``bot-chat[:<profile>]`` deliver elements at create time: Bot Chat delivery is
+ machine-local, so the profile must exist where the scheduler fires (Desktop rosters may
+ show same-named profiles from other machines). Returns an error string or None."""
if not deliver:
return None
try:
@@ -234,21 +199,16 @@ def _validate_bot_chat_deliver(deliver: Optional[str]) -> Optional[str]:
f"bot-chat delivery profile '{profile_arg}' not found on this "
"gateway's machine. Bot Chat delivery is machine-local — use a "
"profile that exists here (hermes profile list), or omit the "
- "name (deliver='bot-chat') for the job's own profile."
- )
+ "name (deliver='bot-chat') for the job's own profile.")
return None
def _resolve_cron_context_deliver(deliver: Optional[str]) -> Optional[str]:
- """Resolve ``origin`` to a concrete target for creates made FROM a cron run.
-
- The creating session is ephemeral, so by fire time there is no origin to
- resolve. Non-cron sessions: returned unchanged. Cron sessions: ``origin``
- (or an omitted value) becomes the creating run's ``platform:chat_id[:thread]``
- from the HERMES_CRON_AUTO_DELIVER_* contextvars, or ``local`` when the
- creating run has no concrete target; other elements pass through verbatim.
- Without this the scheduler would fall back to guessing a home channel.
- """
+ """Resolve ``origin`` to a concrete target for creates made FROM a cron run (the creating
+ session is ephemeral, so by fire time there is no origin). Non-cron sessions: unchanged.
+ Cron sessions: ``origin`` (or omitted) becomes the creating run's ``platform:chat_id[:thread]``
+ from HERMES_CRON_AUTO_DELIVER_*, or ``local`` when it has no concrete target; other
+ elements pass through. Otherwise the scheduler would guess a home channel."""
from gateway.session_context import get_session_env
from utils import is_truthy_value
@@ -271,45 +231,33 @@ def _resolve_cron_context_deliver(deliver: Optional[str]) -> Optional[str]:
def _validate_cron_base_url(
- provider: Optional[Any], base_url: Optional[Any]
-) -> Optional[str]:
- """Reject pairing a named provider's stored credential with an off-host base_url.
-
- A prompt-injected job could name a real provider plus an attacker
- base_url; at fire time the provider's stored key would be sent there
- (credential exfil). Allowed: no override; bare 'custom' (pure BYOK, key
- derived from the base_url itself); an override whose host matches the
- named provider's own endpoint. Everything else fails closed.
- Returns an error string if blocked, else None.
- """
+ provider: Optional[Any], base_url: Optional[Any]) -> Optional[str]:
+ """Reject pairing a named provider's stored credential with an off-host base_url (a
+ prompt-injected job could exfil the key). Allowed: no override; bare 'custom' (pure BYOK,
+ key derived from the base_url); an override whose host matches the named provider's own
+ endpoint. Everything else fails closed. Returns an error string if blocked, else None."""
bu = _normalize_optional_job_value(base_url, strip_trailing_slash=True)
if not bu:
return None
prov = _normalize_optional_job_value(provider)
- if not prov:
- # No provider inherits the default provider's stored key — same primitive.
+ if not prov: # no provider inherits the default provider's stored key — same primitive
return (
"base_url override requires an explicit provider. Set provider to a "
- "configured custom provider to use a custom endpoint."
- )
+ "configured custom provider to use a custom endpoint.")
try:
from hermes_cli.runtime_provider import (
has_named_custom_provider,
resolve_requested_provider,
- _get_named_custom_provider,
- )
+ _get_named_custom_provider)
from hermes_cli.auth import PROVIDER_REGISTRY
from utils import base_url_host_matches, base_url_hostname
except Exception:
return f"Unable to validate base_url override for provider {prov!r}; refused."
- if prov.lower() == "custom":
- # Pure BYOK: key comes from a pool keyed by THIS base_url or host-gated
- # env vars, never an arbitrary stored secret.
+ if prov.lower() == "custom": # pure BYOK: key keyed by THIS base_url, never a stored secret
return None
if has_named_custom_provider(prov):
- # A NAMED custom provider carries a STORED key that the runtime still
- # sends to an override base_url — require the configured host.
+ # A NAMED custom provider's STORED key is still sent to an override base_url.
try:
cp = _get_named_custom_provider(prov)
except Exception:
@@ -320,8 +268,7 @@ def _validate_cron_base_url(
return (
f"base_url {bu!r} is not allowed for provider {prov!r}. A named "
f"custom provider's stored credential may only be sent to its own "
- f"configured endpoint ({cfg_host or 'unknown'})."
- )
+ f"configured endpoint ({cfg_host or 'unknown'}).")
try:
resolved = resolve_requested_provider(prov)
except Exception:
@@ -330,19 +277,16 @@ def _validate_cron_base_url(
known_host = base_url_hostname(getattr(pconfig, "inference_base_url", "") if pconfig else "")
if known_host and base_url_host_matches(bu, known_host):
return None
- # Fail closed: covers named providers with stored credentials AND
- # aliases/unknown names we cannot host-match.
+ # Fail closed: named providers with stored credentials AND unknown names we cannot host-match.
return (
f"base_url {bu!r} is not allowed for provider {prov!r}. A named "
f"provider's stored credential may only be sent to its own endpoint; "
- f'use a configured custom provider (provider="custom") for a custom base_url.'
- )
+ f'use a configured custom provider (provider="custom") for a custom base_url.')
def _validate_cron_script_path(script: Optional[str]) -> Optional[str]:
- """Scripts must be relative paths resolving within HERMES_HOME/scripts/
- (absolute / ~ / drive-letter paths rejected — prompt-injection guard).
- Returns an error string if blocked, else None; empty = clearing, OK."""
+ """Scripts must be relative paths within HERMES_HOME/scripts/ (absolute / ~ / drive-letter
+ rejected — prompt-injection guard). Error string if blocked, else None; empty = clear."""
if not script or not script.strip():
return None
@@ -353,8 +297,7 @@ def _validate_cron_script_path(script: Optional[str]) -> Optional[str]:
return (
f"Script path must be relative to ~/.hermes/scripts/. "
f"Got absolute or home-relative path: {raw!r}. "
- f"Place scripts in ~/.hermes/scripts/ and use just the filename."
- )
+ f"Place scripts in ~/.hermes/scripts/ and use just the filename.")
from tools.path_security import validate_within_dir
@@ -367,10 +310,8 @@ def _validate_cron_script_path(script: Optional[str]) -> Optional[str]:
def _apply_continuity(
context_from: Optional[Union[str, List[str]]],
- continuity: bool,
-) -> Optional[List[str]]:
- """continuity=True ensures "self" is in context_from; False removes any
- "self" entry. Other entries are preserved untouched."""
+ continuity: bool) -> Optional[List[str]]:
+ """continuity=True ensures "self" is in context_from; False removes it; others untouched."""
refs = _clean_str_list(context_from)
has_self = any(r.lower() == "self" for r in refs)
if continuity and not has_self:
@@ -381,9 +322,8 @@ def _apply_continuity(
def _validate_context_from_refs(refs: List[Any]) -> Optional[str]:
- """Error string if any non-"self" ref names a job that doesn't exist.
- ("self" resolves to the job's own id at run time, so it can't be checked
- against the store — the job doesn't exist yet at create time.)"""
+ """Error string if any non-"self" ref names a missing job ("self" resolves to the job's
+ own id at run time, so it can't be checked — the job doesn't exist yet at create)."""
from cron.jobs import get_job as _get_job
for ref_id in refs:
if isinstance(ref_id, str) and ref_id.strip().lower() == "self":
@@ -391,17 +331,14 @@ def _validate_context_from_refs(refs: List[Any]) -> Optional[str]:
if not _get_job(ref_id):
return (
f"context_from job '{ref_id}' not found. "
- "Use cronjob(action='list') to see available jobs."
- )
+ "Use cronjob(action='list') to see available jobs.")
return None
-# Optional fields echoed by _format_job only when truthy on the job record
-# (order matters: it is the JSON key order).
+# Optional fields echoed by _format_job only when truthy (order = JSON key order).
_FORMAT_JOB_OPTIONAL_KEYS = (
"script", "reasoning_effort", "monitor_script", "monitor_url",
- "monitor_state", "no_agent", "enabled_toolsets", "workdir",
-)
+ "monitor_state", "no_agent", "enabled_toolsets", "workdir")
def _format_job(job: Dict[str, Any]) -> Dict[str, Any]:
@@ -451,9 +388,8 @@ def _format_job(job: Dict[str, Any]) -> Dict[str, Any]:
def _gateway_liveness_notice(plural: bool = False) -> dict:
- """``gateway_running``/``warning`` payload via the shared CLI helper so the
- CLI and this tool agree on what "scheduler active" means. False -> warning
- (builtin ticker has no gateway process), None -> probe failed."""
+ """``gateway_running``/``warning`` payload via the shared CLI helper so CLI and tool agree
+ on "scheduler active". False -> warning (no gateway process), None -> probe failed."""
try:
from hermes_cli.cron import _builtin_gateway_liveness
@@ -468,7 +404,6 @@ def _gateway_liveness_notice(plural: bool = False) -> dict:
f"The Hermes gateway is not running — {subject} "
"but will NOT fire until the gateway is started "
"(hermes gateway install / hermes gateway start). "
- "Tell the user the task is scheduled but not active yet."
- ),
+ "Tell the user the task is scheduled but not active yet."),
}
return {"gateway_running": None if _gw is None else True}
diff --git a/tools/cronjob_prompt_scan.py b/tools/cronjob_prompt_scan.py
index 12906ff1784..20ca81fd226 100644
--- a/tools/cronjob_prompt_scan.py
+++ b/tools/cronjob_prompt_scan.py
@@ -1,23 +1,18 @@
-"""Cron prompt threat scanning (extracted from tools/cronjob_tools.py).
-
-Two surfaces, two pattern sets: the small user-authored prompt gets the strict
-set; the assembled prompt (with skill bodies) gets only prose-proof directives.
-"""
+"""Cron prompt threat scanning: the small user-authored prompt gets the strict pattern set;
+the assembled prompt (with skill bodies) gets only prose-proof directives."""
import logging
import re
-# Single source of truth shared with the install-time scanner (skills_guard):
-# a narrower cron-local copy once let obfuscated directives (invisible math
-# operators, directional isolates) slip past this runtime tripwire.
+# Single source of truth shared with the install-time scanner (skills_guard): a narrower
+# cron-local copy once let obfuscated directives slip past this runtime tripwire.
from tools.threat_patterns import INVISIBLE_CHARS as _CRON_INVISIBLE_CHARS
# Logger parity with the origin module (these functions used to log there).
logger = logging.getLogger("tools.cronjob_tools")
-# Strict patterns — user prompt only. A directive-shaped cron prompt has no
-# business containing `cat ~/.hermes/.env` or `rm -rf /`; there it is a
-# smoking gun, not prose.
+# Strict patterns — user prompt only. A directive-shaped cron prompt has no business
+# containing `cat ~/.hermes/.env` or `rm -rf /`; there it is a smoking gun, not prose.
_CRON_THREAT_PATTERNS = [
(r'ignore\s+(?:\w+\s+)*(?:previous|all|above|prior)\s+(?:\w+\s+)*instructions', "prompt_injection"),
(r'do\s+not\s+tell\s+the\s+user', "deception_hide"),
@@ -29,15 +24,14 @@ _CRON_THREAT_PATTERNS = [
(r'rm\s+-rf\s+/', "destructive_root_rm"),
]
-# Looser set — assembled prompt with skills attached. Command-shape patterns are
-# dropped because skill markdown (security postmortems, runbooks) legitimately
-# *describes* those commands; skill bodies are already vetted at install time,
-# so this is only a tripwire for unambiguous injection directives.
+# Looser set for the assembled prompt: command-shape patterns are dropped because skill
+# markdown (postmortems, runbooks) legitimately *describes* those commands and skill bodies
+# are vetted at install time — only unambiguous injection directives remain.
_CRON_SKILL_ASSEMBLED_PATTERNS = _CRON_THREAT_PATTERNS[:4]
_CRON_SECRET_VAR_RE = r'\$\{?\w*(?:KEY|TOKEN|SECRET|PASSWORD|CREDENTIAL|API)\w*\}?'
-# Obvious leak paths only: secret in the destination URL, in a POST/form body,
-# or in an Authorization header to an arbitrary host.
+# Obvious leak paths only: secret in the destination URL, in a POST/form body, or in an
+# Authorization header to an arbitrary host.
_CRON_EXFIL_COMMAND_PATTERNS = [
(rf'curl\s+[^\n]*https?://[^\s"\'`]*{_CRON_SECRET_VAR_RE}', "exfil_curl_url"),
(rf'wget\s+[^\n]*https?://[^\s"\'`]*{_CRON_SECRET_VAR_RE}', "exfil_wget_url"),
@@ -46,27 +40,12 @@ _CRON_EXFIL_COMMAND_PATTERNS = [
(rf'curl\s+[^\n]*(?:-H|--header)\s+["\']Authorization:\s*(?:Bearer|token)\s+{_CRON_SECRET_VAR_RE}["\']', "exfil_curl_auth_header"),
]
-_BLOCKED_PATTERN_MSG = (
- "Blocked: prompt matches threat pattern '{pid}'. Cron prompts must not "
- "contain injection or exfiltration payloads."
-)
-
-# U+200D (ZWJ) is a required part of many emoji sequences (👨‍👩‍👧, 🏳️‍🌈).
-# Block it between plain text, allow it inside an emoji grapheme cluster.
-_EMOJI_NEIGHBOUR_CP_RANGES = (
- (0x1F000, 0x1FFFF),
- (0x2600, 0x27BF),
- (0x2300, 0x23FF),
- (0x1F1E6, 0x1F1FF),
- (0x20E3, 0x20E3),
-)
+# U+200D (ZWJ) is a required part of many emoji sequences (👨‍👩‍👧, 🏳️‍🌈): block it
+# between plain text, allow it inside an emoji grapheme cluster.
+_EMOJI_NEIGHBOUR_CP_RANGES = ((0x1F000, 0x1FFFF), (0x2600, 0x27BF), (0x2300, 0x23FF), (0x1F1E6, 0x1F1FF), (0x20E3, 0x20E3))
_VARIATION_SELECTOR_CP = 0xFE0F
-def _is_emoji_cp(cp: int) -> bool:
- return any(lo <= cp <= hi for lo, hi in _EMOJI_NEIGHBOUR_CP_RANGES)
-
-
def _zwj_has_emoji_neighbour(text: str, idx: int) -> bool:
"""True when the ZWJ at text[idx] sits between emoji codepoints (skipping VS16)."""
left = idx - 1
@@ -75,23 +54,21 @@ def _zwj_has_emoji_neighbour(text: str, idx: int) -> bool:
right = idx + 1
while right < len(text) and ord(text[right]) == _VARIATION_SELECTOR_CP:
right += 1
- return (
- left >= 0 and right < len(text)
- and _is_emoji_cp(ord(text[left]))
- and _is_emoji_cp(ord(text[right]))
+ if left < 0 or right >= len(text):
+ return False
+ return all(
+ any(lo <= ord(text[pos]) <= hi for lo, hi in _EMOJI_NEIGHBOUR_CP_RANGES) for pos in (left, right)
)
def _strip_cron_safe_constructs(prompt: str) -> str:
- """Scrub the bundled GitHub skill's `Authorization: token $GITHUB_TOKEN` +
- api.github.com curl so it doesn't trip the auth-header exfil rule.
-
- re.sub scrubs EVERY occurrence (a job loading several GitHub skills has
- many). The trailing ``[^\\s;&|$`]*`` consumes only the URL path — never
- separators or subshell openers — so a payload smuggled onto the same line
- still gets scanned. Host must be exactly api.github.com followed by ``/``,
- whitespace, quote, or end: lookalike authorities (api.github.com.evil.com,
- [email protected]) fall through to the exfil detectors.
+ """Scrub the bundled GitHub skill's `Authorization: token $GITHUB_TOKEN` + api.github.com
+ curl so it doesn't trip the auth-header exfil rule.
+
+ re.sub scrubs EVERY occurrence. The trailing ``[^\\s;&|$`]*`` consumes only the URL path —
+ never separators or subshell openers — so a payload smuggled onto the same line still gets
+ scanned. Host must be exactly api.github.com followed by ``/``, whitespace, quote, or end:
+ lookalike authorities (api.github.com.evil.com, [email protected]) fall through.
"""
return re.sub(
rf'curl\s+[^\n;&|$`]*(?:-H|--header)\s+["\']Authorization:\s*token\s+{_CRON_SECRET_VAR_RE}["\']'
@@ -102,22 +79,11 @@ def _strip_cron_safe_constructs(prompt: str) -> str:
)
-def _check_invisible_unicode(prompt: str) -> str:
- """Error string if the prompt holds invisible-unicode markers (emoji ZWJ allowed).
- Reports the first offender in ``_CRON_INVISIBLE_CHARS`` order."""
- removed = set(_strip_invisible_unicode(prompt)[1])
- for char in _CRON_INVISIBLE_CHARS:
- if f"U+{ord(char):04X}" in removed:
- return f"Blocked: prompt contains invisible unicode U+{ord(char):04X} (possible injection)."
- return ""
-
-
def _strip_invisible_unicode(prompt: str) -> tuple[str, list[str]]:
"""Strip invisible-unicode chars, keeping ZWJ inside legitimate emoji.
- Returns ``(cleaned, sorted U+XXXX labels removed)``. Used for the
- skills-attached path, where a stray zero-width space in vetted skill
- content should be sanitized rather than permanently kill the job.
+ Returns ``(cleaned, sorted U+XXXX labels removed)``. The skills-attached path sanitizes
+ (a stray zero-width space in vetted skill content must not permanently kill the job).
"""
if not prompt:
return prompt, []
@@ -135,26 +101,29 @@ def _first_pattern_error(text: str, *pattern_sets) -> str:
for patterns in pattern_sets:
for pattern, pid in patterns:
if re.search(pattern, text, re.IGNORECASE):
- return _BLOCKED_PATTERN_MSG.format(pid=pid)
+ return (
+ f"Blocked: prompt matches threat pattern '{pid}'. Cron prompts must not "
+ "contain injection or exfiltration payloads."
+ )
return ""
def _scan_cron_prompt(prompt: str) -> str:
- """Strict scan of the USER-SUPPLIED prompt (create/update + runtime
- defense-in-depth). Returns an error string when blocked, else ""."""
+ """Strict scan of the USER-SUPPLIED prompt (create/update + runtime defense-in-depth).
+ Returns an error string when blocked, else "". Invisible unicode is reported first, in
+ ``_CRON_INVISIBLE_CHARS`` order (emoji ZWJ allowed)."""
prompt_to_scan = _strip_cron_safe_constructs(prompt)
- return _check_invisible_unicode(prompt_to_scan) or _first_pattern_error(
- prompt_to_scan, _CRON_THREAT_PATTERNS, _CRON_EXFIL_COMMAND_PATTERNS
- )
+ removed = set(_strip_invisible_unicode(prompt_to_scan)[1])
+ for char in _CRON_INVISIBLE_CHARS:
+ if f"U+{ord(char):04X}" in removed:
+ return f"Blocked: prompt contains invisible unicode U+{ord(char):04X} (possible injection)."
+ return _first_pattern_error(prompt_to_scan, _CRON_THREAT_PATTERNS, _CRON_EXFIL_COMMAND_PATTERNS)
def _scan_cron_skill_assembled(assembled: str) -> tuple[str, str]:
- """Loose scan of the ASSEMBLED prompt (skill content included).
-
- Invisible unicode is SANITIZED (stripped + logged), not blocked — the hard
- block stays on raw user prompts, the actual injection surface. Returns
- ``(cleaned_prompt, error)`` with ``error`` empty when it passed.
- """
+ """Loose scan of the ASSEMBLED prompt (skill content included). Invisible unicode is
+ SANITIZED (stripped + logged), not blocked — the hard block stays on raw user prompts,
+ the actual injection surface. Returns ``(cleaned_prompt, error)``, error "" when passed."""
cleaned, removed = _strip_invisible_unicode(assembled)
if removed:
logger.warning(
@@ -162,5 +131,4 @@ def _scan_cron_skill_assembled(assembled: str) -> tuple[str, str]:
"char(s) (%s) from vetted skill content",
len(removed), ", ".join(removed),
)
- error = _first_pattern_error(_strip_cron_safe_constructs(cleaned), _CRON_SKILL_ASSEMBLED_PATTERNS)
- return cleaned, error
+ return cleaned, _first_pattern_error(_strip_cron_safe_constructs(cleaned), _CRON_SKILL_ASSEMBLED_PATTERNS)
diff --git a/tools/cronjob_tools.py b/tools/cronjob_tools.py
index 4da0f64e8de..2efe325a643 100644
--- a/tools/cronjob_tools.py
+++ b/tools/cronjob_tools.py
@@ -1,9 +1,5 @@
-"""
-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.
-"""
+"""Cron job management tool: one compressed action-oriented `cronjob_manage` tool
+(schema/context bloat avoided); `cronjob()` stays callable for direct Python callers."""
import contextlib
import json
@@ -18,15 +14,11 @@ from hermes_constants import display_hermes_home
logger = logging.getLogger(__name__)
-# Heartbeat cadence that keeps the calling agent's inactivity watchdog at bay
-# while a manual `cronjob(action="run")` executes synchronously in-process
-# (mirrors tools/environments/base.py::touch_activity_if_due; comfortably
-# below the default HERMES_AGENT_TIMEOUT).
+# Heartbeat cadence keeping the caller's inactivity watchdog at bay while a manual
+# `cronjob(action="run")` executes in-process (comfortably below HERMES_AGENT_TIMEOUT).
_CRON_RUN_HEARTBEAT_INTERVAL = 10.0
-
-# Hard ceiling on the heartbeat: with HERMES_CRON_TIMEOUT=0 (unlimited) a truly
-# hung run would otherwise mask the gateway watchdog forever. Past this, the
-# heartbeat stops and the gateway watchdog regains authority over the turn.
+# Hard ceiling: with HERMES_CRON_TIMEOUT=0 a truly hung run would otherwise mask the
+# gateway watchdog forever; past this the heartbeat stops and the watchdog regains authority.
_CRON_RUN_HEARTBEAT_CEILING = 6 * 3600.0
sys.path.insert(0, str(Path(__file__).parent.parent))
@@ -43,17 +35,12 @@ from cron.jobs import (
remove_job,
resolve_job_ref,
resume_job,
- update_job,
-)
-from tools.cronjob_prompt_scan import ( # noqa: F401 (re-exported)
- _CRON_EXFIL_COMMAND_PATTERNS,
+ update_job)
+from tools.cronjob_prompt_scan import ( # noqa: F401 (re-exported; tests/scheduler import via this module)
_CRON_INVISIBLE_CHARS,
- _CRON_SKILL_ASSEMBLED_PATTERNS,
- _CRON_THREAT_PATTERNS,
_scan_cron_prompt,
- _scan_cron_skill_assembled,
-)
-from tools.cronjob_job_args import ( # noqa: F401 (re-exported)
+ _scan_cron_skill_assembled)
+from tools.cronjob_job_args import ( # noqa: F401 (re-exported; tests/scheduler import via this module)
_apply_continuity,
_canonical_skills,
_clean_str_list,
@@ -70,13 +57,16 @@ from tools.cronjob_job_args import ( # noqa: F401 (re-exported)
_validate_bot_chat_deliver,
_validate_context_from_refs,
_validate_cron_base_url,
- _validate_cron_script_path,
-)
+ _validate_cron_script_path)
+from tools.registry import registry, tool_error
+
+
+def _dumps(payload: Dict[str, Any]) -> str:
+ return json.dumps(payload, indent=2)
def _notify_provider_jobs_changed_safe() -> None:
- """Tell the active cron scheduler provider the job set changed (no-op for
- the built-in). Best-effort — never lets a provider error break the tool."""
+ """Tell the active scheduler provider the job set changed; best-effort, never raises."""
try:
from cron.scheduler import _notify_provider_jobs_changed
_notify_provider_jobs_changed()
@@ -99,44 +89,55 @@ def _relay_fronted_delivery_platforms(job: Dict[str, Any]) -> set:
return set()
try:
from cron.scheduler import _resolve_delivery_targets
-
targets = _resolve_delivery_targets(job) or []
except Exception:
return set()
return {t.get("platform") for t in targets if t.get("platform")} & fronted
-def _forward_relay_fronted_run(
- job: Dict[str, Any], extra_prompt: Optional[str] = None
-) -> Optional[str]:
- """Forward a manual run to the gateway when it targets a relay-fronted
- platform and this process has no live relay adapter.
+def _api_server_base_url() -> str:
+ """``http://host:port`` of the local api_server, mirroring its bind resolution
+ (extra.host -> API_SERVER_HOST -> 127.0.0.1); a wildcard bind listens on loopback too."""
+ import os
+
+ port_raw = os.getenv("API_SERVER_PORT", "").strip()
+ try:
+ port = int(port_raw) if port_raw else 8642
+ except ValueError:
+ port = 8642
+ try:
+ from hermes_cli.config import cfg_get, load_config_readonly
+ host = str(cfg_get(load_config_readonly(), "platforms", "api_server", "extra", "host", default="") or "").strip()
+ except Exception:
+ host = ""
+ host = host or os.getenv("API_SERVER_HOST", "").strip()
+ if not host or host in ("0.0.0.0", "::", "*"):
+ host = "127.0.0.1"
+ if ":" in host and not host.startswith("["):
+ host = f"[{host}]" # bare IPv6 literal
+ return f"http://{host}:{port}"
+
+
+def _forward_relay_fronted_run(job: Dict[str, Any], extra_prompt: Optional[str] = None) -> Optional[str]:
+ """Forward a manual run to the gateway when it targets a relay-fronted platform.
- Relay-fronted delivery has no standalone sender — the gateway's live relay
- adapter is the only path, reached via ``POST /api/jobs/{id}/run`` (which
- marks the job due for the gateway ticker; ``extra_prompt`` rides in the
- body). Returns a JSON result string when forwarding engages, else None to
- fall through to the normal in-process run.
+ Relay-fronted delivery has no standalone sender — the gateway's live relay adapter is
+ the only path, reached via ``POST /api/jobs/{id}/run`` (marks the job due for the
+ gateway ticker; ``extra_prompt`` rides in the body). Returns a JSON result string when
+ forwarding engages, else None to fall through to the normal in-process run.
"""
if not _relay_fronted_delivery_platforms(job):
return None
- url = f"{_api_server_base_url()}/api/jobs/{job['id']}/run"
-
from agent.secret_scope import get_secret
key = get_secret("API_SERVER_KEY", "") or ""
try:
import httpx
-
resp = httpx.post(
- url,
- headers={"Authorization": f"Bearer {key}"},
- json=({"prompt": extra_prompt} if extra_prompt else {}),
- timeout=10.0,
- )
+ f"{_api_server_base_url()}/api/jobs/{job['id']}/run", headers={"Authorization": f"Bearer {key}"},
+ json=({"prompt": extra_prompt} if extra_prompt else {}), timeout=10.0)
except Exception:
resp = None
-
if resp is not None and resp.status_code < 300:
return _dumps({
"success": True,
@@ -144,58 +145,22 @@ def _forward_relay_fronted_run(
"note": (
"This job targets a relay-fronted platform; it was dispatched "
"to the running gateway, whose live relay adapter owns that "
- "delivery."
- ),
+ "delivery."),
})
return _dumps({
"success": False,
"error": (
"This job targets a relay-fronted platform, which has no "
"standalone sender. Start the gateway — its ticker will "
- "deliver the job on schedule via the live relay adapter."
- ),
+ "deliver the job on schedule via the live relay adapter."),
})
-def _api_server_base_url() -> str:
- """``http://host:port`` of the local api_server, mirroring its bind
- resolution (extra.host -> API_SERVER_HOST -> 127.0.0.1); a wildcard bind
- listens on loopback too."""
- import os
-
- port_raw = os.getenv("API_SERVER_PORT", "").strip()
- try:
- port = int(port_raw) if port_raw else 8642
- except ValueError:
- port = 8642
- try:
- from hermes_cli.config import cfg_get, load_config_readonly
-
- host = str(
- cfg_get(
- load_config_readonly(), "platforms", "api_server", "extra", "host",
- default="",
- )
- or ""
- ).strip()
- except Exception:
- host = ""
- if not host:
- host = os.getenv("API_SERVER_HOST", "").strip()
- if not host or host in ("0.0.0.0", "::", "*"):
- host = "127.0.0.1"
- if ":" in host and not host.startswith("["):
- host = f"[{host}]" # bare IPv6 literal
- return f"http://{host}:{port}"
-
-
def _manual_run_delivery_note(deliver: str, refreshed: Dict[str, Any]) -> str:
- """Parenthetical delivery note for a manual run's completion summary,
- following the refreshed record's ``last_delivery_error`` so the summary
- never claims success over a failed post-run delivery."""
- # Falsy deliver ("", stored JSON null) is normalized to "local" at fire
- # time -> read as saved-locally. Whitespace-only values fall through to
- # the error check so the fire-time "no delivery target" error surfaces.
+ """Parenthetical delivery note for a manual run's summary; follows the refreshed record's
+ ``last_delivery_error`` so the summary never claims success over a failed delivery."""
+ # Falsy deliver ("", stored JSON null) is normalized to "local" at fire time -> saved
+ # locally. Whitespace-only values fall through so the fire-time "no target" error surfaces.
if not deliver or deliver == "local":
return " (output saved locally only)"
err = str(refreshed.get("last_delivery_error") or "").strip()
@@ -206,17 +171,15 @@ def _manual_run_delivery_note(deliver: str, refreshed: Dict[str, Any]) -> str:
_ALREADY_RUNNING_ERROR = (
"Job is already running (a scheduler tick or another "
- "manual run is executing it); not started again."
-)
+ "manual run is executing it); not started again.")
def _claim_for_manual_run(job_id: str, log_label: str):
"""At-most-once claim shared by the sync and background run paths.
- Returns ``(claimed_job, None)`` on success, else ``(None, error_dict)``
- where the dict has the ``_execute_job_now`` result shape. A lost claim is
- labelled precisely: claim_job_for_fire also returns False for paused /
- disabled / missing jobs, which must not read as "already being fired".
+ Returns ``(claimed_job, None)`` or ``(None, error_dict)`` in the ``_execute_job_now``
+ result shape. A lost claim is labelled precisely: claim_job_for_fire also returns False
+ for paused / disabled / missing jobs, which must not read as "already being fired".
"""
try:
claimed_job = claim_job_for_fire(job_id, return_job=True)
@@ -232,58 +195,30 @@ def _claim_for_manual_run(job_id: str, log_label: str):
return None, {"claimed": False, "success": False, "error": reason}
except Exception as e:
logger.error("Failed to claim cron job %s for %s: %s", job_id, log_label, e)
- try:
+ with contextlib.suppress(Exception):
mark_job_run(job_id, False, str(e))
- except Exception:
- pass
return None, {"claimed": True, "success": False, "error": str(e)}
-def _execute_job_now(
- job: Dict[str, Any], extra_prompt: Optional[str] = None
-) -> Dict[str, Any]:
- """Execute a cron job immediately, outside the scheduler tick.
-
- Claims first via ``claim_job_for_fire`` (the same CAS the ticker uses, so a
- concurrent tick cannot double-fire and next_run_at advances), then fires
- through the shared ``run_one_job`` body so delivery / [SILENT] handling
- cannot drift between paths.
- Returns {"claimed": bool, "success": bool, "error": str|None}.
- """
+def _execute_job_now(job: Dict[str, Any], extra_prompt: Optional[str] = None) -> Dict[str, Any]:
+ """Run a job now, outside the scheduler tick: claim via ``claim_job_for_fire`` (the ticker's
+ CAS, so a concurrent tick cannot double-fire and next_run_at advances), then fire through
+ the shared ``run_one_job`` body. Returns {"claimed", "success", "error"}."""
claimed_job, err = _claim_for_manual_run(job["id"], "immediate run")
- if err is not None:
- return err
- return _run_claimed_job(claimed_job, extra_prompt=extra_prompt)
-
-
-def _gateway_adapters_and_loop():
- """Live gateway adapter map + event loop when running inside the gateway
- process, else ``(None, None)``. Manual runs from a gateway agent must
- deliver on the loop that owns clients such as Matrix/aiohttp (a standalone
- asyncio.run() loop breaks them)."""
- runner_ref = getattr(sys.modules.get("gateway.run"), "_gateway_runner_ref", None)
- runner = runner_ref() if callable(runner_ref) else None
- if runner is None:
- return None, None
- return getattr(runner, "adapters", None), getattr(runner, "_gateway_loop", None)
+ return err if err is not None else _run_claimed_job(claimed_job, extra_prompt=extra_prompt)
@contextlib.contextmanager
def _run_heartbeat(job_name: str):
- """Heartbeat into the caller's activity tracker while a manual run
- executes; stops and joins the thread on exit.
-
- A manual run executes synchronously on the caller's thread and can take
- minutes; without tool activity the gateway inactivity watchdog would kill
- the parent turn. Best-effort: no callback -> no thread, unchanged behavior.
- """
+ """Heartbeat into the caller's activity tracker while a manual run executes (minutes,
+ synchronously on the caller's thread — without tool activity the gateway inactivity
+ watchdog would kill the parent turn). Best-effort: no callback -> no thread."""
stop = threading.Event()
thread = None
try:
from tools.environments.base import get_activity_callback
-
- # Capture on THIS thread: the callback is thread-local (installed by
- # the tool executor), so a freshly spawned thread cannot read it.
+ # Capture on THIS thread: the callback is thread-local (installed by the tool
+ # executor), so a freshly spawned thread cannot read it.
activity_cb = get_activity_callback()
except Exception:
activity_cb = None
@@ -293,14 +228,11 @@ def _run_heartbeat(job_name: str):
while not stop.wait(_CRON_RUN_HEARTBEAT_INTERVAL):
elapsed = time.monotonic() - started
if elapsed > _CRON_RUN_HEARTBEAT_CEILING:
- # A run this long with an unlimited child watchdog is
- # likely wedged — stop masking the gateway watchdog.
+ # A run this long with an unlimited child watchdog is likely wedged.
logger.warning(
"cronjob run heartbeat ceiling reached for job "
- "'%s' (%.0fs) — stopping heartbeat; gateway "
- "watchdog regains authority",
- job_name, elapsed,
- )
+ "'%s' (%.0fs) — stopping heartbeat; gateway watchdog regains authority",
+ job_name, elapsed)
return
try:
activity_cb(f"cronjob: running job '{job_name}' ({int(elapsed)}s elapsed)")
@@ -318,28 +250,19 @@ def _run_heartbeat(job_name: str):
thread.join(timeout=_CRON_RUN_HEARTBEAT_INTERVAL + 1)
-def _run_claimed_job(
- job: Dict[str, Any], extra_prompt: Optional[str] = None
-) -> Dict[str, Any]:
- """Fire an already-claimed job through the shared ``run_one_job`` body.
-
- Split from ``_execute_job_now`` so the background path can take the claim
- synchronously (reporting paused/already-firing immediately) and hand the
- run to a worker. Returns {"claimed": True, "success": bool, "error": ...}.
- """
+def _run_claimed_job(job: Dict[str, Any], extra_prompt: Optional[str] = None) -> Dict[str, Any]:
+ """Fire an already-claimed job through the shared ``run_one_job`` body (split from
+ ``_execute_job_now`` so the background path can claim synchronously and hand the run
+ to a worker). Returns {"claimed": True, "success": bool, "error": ...}."""
job_id = job["id"]
_registered = False
fire_owner = None
try:
- from cron.scheduler import (
- release_running_job,
- run_one_job,
- try_register_running_job,
- )
+ from cron.scheduler import release_running_job, run_one_job, try_register_running_job
- # In-flight dedupe: the fire claim's TTL is routinely outlived by real
- # jobs, so register in the scheduler's shared running set (same guard
- # the ticker uses; also visible to the gateway shutdown drain).
+ # In-flight dedupe: the fire claim's TTL is routinely outlived by real jobs, so
+ # register in the scheduler's shared running set (same guard the ticker uses;
+ # also visible to the gateway shutdown drain).
if not try_register_running_job(job_id):
return {"claimed": True, "success": False, "error": _ALREADY_RUNNING_ERROR}
_registered = True
@@ -347,60 +270,49 @@ def _run_claimed_job(
claim = job.get("fire_claim")
fire_owner = str(claim.get("by") or "") if isinstance(claim, dict) else None
- adapters, gateway_loop = _gateway_adapters_and_loop()
+ # Inside the gateway process deliver on the loop that owns clients such as
+ # Matrix/aiohttp (a standalone asyncio.run() loop breaks them).
+ runner_ref = getattr(sys.modules.get("gateway.run"), "_gateway_runner_ref", None)
+ runner = runner_ref() if callable(runner_ref) else None
+ adapters = getattr(runner, "adapters", None) if runner is not None else None
+ gateway_loop = getattr(runner, "_gateway_loop", None) if runner is not None else None
try:
- # run_one_job records last_run_at/last_status via mark_job_run;
- # `job` is the owner-bearing claimed snapshot, so terminal writes
- # stay fenced by that owner.
+ # run_one_job records last_run_at/last_status via mark_job_run; `job` is the
+ # owner-bearing claimed snapshot, so terminal writes stay fenced by that owner.
with _run_heartbeat(str(job.get("name") or job_id)):
- processed = run_one_job(
- job, adapters=adapters, loop=gateway_loop,
- extra_prompt=extra_prompt,
- )
+ processed = run_one_job(job, adapters=adapters, loop=gateway_loop, extra_prompt=extra_prompt)
finally:
_registered = False
release_running_job(job_id)
refreshed = get_job(job_id) or {}
last_status = refreshed.get("last_status")
- # "delivery_failed": the run succeeded but output never reached the
- # user — not a success for the caller; surface last_delivery_error.
+ # "delivery_failed": the run succeeded but output never reached the user — not a
+ # success for the caller; surface last_delivery_error.
run_error = refreshed.get("last_error")
if last_status == "delivery_failed" and not run_error:
run_error = refreshed.get("last_delivery_error")
- return {
- "claimed": True,
- "success": bool(processed and last_status == "ok"),
- "error": run_error,
- }
-
+ return {"claimed": True, "success": bool(processed and last_status == "ok"), "error": run_error}
except Exception as e:
logger.error("Failed to execute cron job %s immediately: %s", job_id, e)
if _registered:
- # We raised before the run's own release (e.g. heartbeat setup);
- # don't leave the job marked in-flight. Only release registrations
- # WE took — a bare discard could erase a ticker-owned entry.
- try:
+ # Raised before the run's own release (e.g. heartbeat setup): don't leave the
+ # job marked in-flight. Only release registrations WE took — a bare discard
+ # could erase a ticker-owned entry.
+ with contextlib.suppress(Exception):
release_running_job(job_id)
- except Exception:
- pass
- try:
+ with contextlib.suppress(Exception):
mark_job_run(job_id, False, str(e), expected_fire_owner=fire_owner)
- except Exception:
- pass
return {"claimed": True, "success": False, "error": str(e)}
def _latest_job_output_excerpt(job_id: str, max_chars: int = 2000) -> Optional[str]:
- """Best-effort excerpt of the job's most recent saved output file, for the
- background-run completion block (parent sees what the job produced). Never raises."""
+ """Excerpt of the job's most recent saved output file for the background completion
+ block (parent sees what the job produced). Never raises."""
try:
from cron.jobs import get_cron_output_dir
- out_dir = get_cron_output_dir() / job_id
- files = sorted(out_dir.glob("*.md"))
- if not files:
- return None
- text = files[-1].read_text(encoding="utf-8", errors="replace").strip()
+ files = sorted((get_cron_output_dir() / job_id).glob("*.md"))
+ text = files[-1].read_text(encoding="utf-8", errors="replace").strip() if files else ""
if not text:
return None
if len(text) > max_chars:
@@ -411,46 +323,39 @@ def _latest_job_output_excerpt(job_id: str, max_chars: int = 2000) -> Optional[s
def _reap_stale_executions(job_name: str) -> None:
- """Reap execution rows left 'claimed'/'running' by a dead owner process
- (e.g. a prior one-shot `hermes cron run` that exited mid-run). The ticker
- does this at its own startup; one-shot invocations have no such moment, so
- a stale claim would block every later manual run. Only provably-dead
- owners are reaped. Best-effort self-heal: must not block dispatch."""
+ """Reap execution rows left 'claimed'/'running' by a provably-dead owner (e.g. a prior
+ one-shot `hermes cron run` that died mid-run). The ticker does this at startup; one-shot
+ invocations have no such moment, so a stale claim would block every later manual run.
+ Best-effort self-heal: must not block dispatch."""
try:
from cron.executions import recover_interrupted_executions
_reclaimed = recover_interrupted_executions()
if _reclaimed:
logger.warning(
- "Reclaimed %d stale cron execution(s) from dead owner(s) "
- "before dispatching job '%s'",
- _reclaimed,
- job_name,
- )
+ "Reclaimed %d stale cron execution(s) from dead owner(s) before dispatching job '%s'",
+ _reclaimed, job_name)
except Exception as _reap_exc:
logger.debug("Stale execution reclaim failed: %s", _reap_exc)
def _background_session_key(session_id: Optional[str]) -> str:
- """Routing key for a detached completion, captured on THIS thread
- (contextvars don't cross the pool). Empty string = no durable consumer."""
+ """Routing key for a detached completion, captured on THIS thread (contextvars don't
+ cross the pool). Empty string = no durable consumer."""
try:
from tools.approval import get_current_session_key
session_key = get_current_session_key(default="")
except Exception:
session_key = ""
- if not session_key and session_id:
- # CLI path: the approval contextvar is only bound during gateway/TUI
- # turns; the CLI drain filters completions by the durable session id,
- # and an empty key would fail closed (completion never claimable).
- session_key = str(session_id)
- return session_key
+ # CLI path: the approval contextvar is only bound during gateway/TUI turns; the CLI
+ # drain filters completions by the durable session id, and an empty key would fail
+ # closed (completion never claimable).
+ return session_key or (str(session_id) if session_id else "")
def _manual_run_completion(
- res: Dict[str, Any], job_id: str, job_name: str, deliver: str, started_at: float
-) -> Dict[str, Any]:
+ res: Dict[str, Any], job_id: str, job_name: str, deliver: str, started_at: float) -> Dict[str, Any]:
"""Async-delegation completion block for a finished background manual run."""
duration = round(time.time() - started_at, 2)
refreshed = get_job(job_id) or {}
@@ -458,15 +363,13 @@ def _manual_run_completion(
f"Cron job '{job_name}' ({job_id}) finished its manual run.",
f"Result: {'ok' if res.get('success') else 'FAILED'}"
+ (f" — {res.get('error')}" if res.get("error") else ""),
- f"Delivery target: {deliver}"
- + _manual_run_delivery_note(deliver, refreshed),
+ f"Delivery target: {deliver}" + _manual_run_delivery_note(deliver, refreshed),
]
if refreshed.get("next_run_at"):
lines.append(f"Next scheduled run: {refreshed['next_run_at']}")
excerpt = _latest_job_output_excerpt(job_id)
if excerpt:
- lines.append("--- JOB OUTPUT ---")
- lines.append(excerpt)
+ lines += ["--- JOB OUTPUT ---", excerpt]
return {
"status": "completed" if res.get("success") else "error",
"summary": "\n".join(lines),
@@ -477,26 +380,23 @@ def _manual_run_completion(
def _try_dispatch_background_run(
- job: Dict[str, Any], session_id: Optional[str] = None,
- extra_prompt: Optional[str] = None,
+ job: Dict[str, Any], session_id: Optional[str] = None, extra_prompt: Optional[str] = None,
) -> Optional[Dict[str, Any]]:
"""Claim ``job`` now, then fire it on the async-delegation daemon executor.
- A cron job is a full agent run (minutes to hours); running it inline made
- the parent turn uninterruptible and serialized batches. This dispatches
- like ``delegate_task``'s background mode: the tool returns a handle, and a
- ``type="async_delegation"`` completion event re-enters the conversation as
- a fresh turn (keeps role alternation legal and the prompt cache intact).
- The claim is taken SYNCHRONOUSLY so unrunnable jobs report immediately.
-
- Returns None when background delivery is unavailable on this runtime
- (caller falls back to the sync path); ``{"claimed": False, ...}`` on a lost
- claim; ``{"claimed": True, "dispatched": True, "delegation_id": ...}`` when
- running in the background; ``{"claimed": True, "dispatched": False, ...}``
- when the pool was full and the run executed inline (claim already taken).
+ A cron job is a full agent run (minutes to hours); inline it made the parent turn
+ uninterruptible. Dispatches like ``delegate_task``'s background mode: the tool returns a
+ handle and a ``type="async_delegation"`` completion re-enters the conversation as a
+ fresh turn (role alternation legal, prompt cache intact). The claim is taken
+ SYNCHRONOUSLY so unrunnable jobs report immediately.
+
+ Returns None when background delivery is unavailable (caller falls back to the sync
+ path); ``{"claimed": False, ...}`` on a lost claim; ``{"claimed": True, "dispatched":
+ True, "delegation_id"}`` when running in the background; ``{"claimed": True,
+ "dispatched": False, ...}`` when the pool was full and the run executed inline.
"""
- # Finite sessions cannot route a detached result back after the turn ends
- # — mirror delegate_task's gate and fall back to sync execution.
+ # Finite sessions cannot route a detached result back after the turn ends — mirror
+ # delegate_task's gate and fall back to sync execution.
try:
from gateway.session_context import async_delivery_supported
@@ -509,17 +409,15 @@ def _try_dispatch_background_run(
job_name = str(job.get("name") or job_id)
_reap_stale_executions(job_name)
- # Routing capture BEFORE the claim: with no routable session there is no
- # durable consumer for a detached completion, so we must not
- # claim-and-dispatch. Direct Python callers (`hermes cron run`, tests):
- # process exits right after the tool returns, so run synchronously.
+ # Routing capture BEFORE the claim: with no routable session there is no durable
+ # consumer for a detached completion, so we must not claim-and-dispatch. Direct Python
+ # callers (`hermes cron run`, tests) exit right after the tool returns -> run sync.
session_key = _background_session_key(session_id)
if not session_key:
return None
- # Best-effort early dedupe so a mid-run job reports in THIS tool response
- # instead of as a delayed error completion. The authoritative (atomic)
- # check is try_register_running_job inside _run_claimed_job.
+ # Best-effort early dedupe so a mid-run job reports in THIS tool response instead of
+ # as a delayed error completion (authoritative check: try_register_running_job).
try:
from cron.scheduler import get_running_job_ids
@@ -543,17 +441,12 @@ def _try_dispatch_background_run(
pass
try:
- from tools.async_delegation import (
- _current_origin_session_id,
- dispatch_async_delegation,
- )
+ from tools.async_delegation import _current_origin_session_id, dispatch_async_delegation
origin_session_id = _current_origin_session_id()
except Exception as e:
logger.warning(
- "cronjob run: async delegation registry unavailable (%s); "
- "running job '%s' inline.", e, job_name,
- )
+ "cronjob run: async delegation registry unavailable (%s); running job '%s' inline.", e, job_name)
result = _run_claimed_job(claimed_job, extra_prompt=extra_prompt)
result["dispatched"] = False
return result
@@ -566,8 +459,8 @@ def _try_dispatch_background_run(
max_async = 3
started_at = time.time()
- # Canonicalize with the scheduler's own normalizer (falsy -> "local", list
- # -> comma string), reading the claimed snapshot the run actually executes.
+ # Canonicalize with the scheduler's own normalizer (falsy -> "local", list -> comma
+ # string), reading the claimed snapshot the run actually executes.
from cron.scheduler import _normalize_deliver_value
deliver = _normalize_deliver_value(claimed_job.get("deliver", "local"))
@@ -580,8 +473,7 @@ def _try_dispatch_background_run(
goal=f"Manual run of cron job '{job_name}' ({job_id})",
context=(
"Triggered via cronjob(action='run'). The job executed in its own "
- "fresh cron session; this block reports its outcome."
- ),
+ "fresh cron session; this block reports its outcome."),
toolsets=None,
role="cron_run",
model=job.get("model"),
@@ -590,22 +482,15 @@ def _try_dispatch_background_run(
runner=_runner,
origin_ui_session_id=origin_ui_session_id,
origin_session_id=origin_session_id,
- max_async_children=max_async,
- )
-
+ max_async_children=max_async)
if dispatch.get("status") == "dispatched":
- return {
- "claimed": True,
- "dispatched": True,
- "delegation_id": dispatch.get("delegation_id"),
- }
-
- # Pool at capacity (or submit failure): the claim is already taken and
- # must not be stranded — run inline exactly as the legacy path did.
+ return {"claimed": True, "dispatched": True, "delegation_id": dispatch.get("delegation_id")}
+
+ # Pool at capacity (or submit failure): the claim is already taken and must not be
+ # stranded — run inline exactly as the legacy path did.
logger.info(
"cronjob run: background pool unavailable (%s); running job '%s' inline.",
- dispatch.get("error", "rejected"), job_name,
- )
+ dispatch.get("error", "rejected"), job_name)
result = _run_claimed_job(job, extra_prompt=extra_prompt)
result["dispatched"] = False
return result
@@ -616,8 +501,12 @@ def _try_dispatch_background_run(
# job record for job-bound actions) and returns the JSON result string.
# ---------------------------------------------------------------------------
-def _dumps(payload: Dict[str, Any]) -> str:
- return json.dumps(payload, indent=2)
+def _with_guidance(result: Dict[str, Any], job: Dict[str, Any], deliver: Optional[str]) -> Dict[str, Any]:
+ """Attach mode/delivery guidance (create and update echo the same notes)."""
+ _notes = _mode_guidance_notes(job, deliver)
+ if _notes:
+ result["guidance"] = _notes
+ return result
def _action_create(a: Dict[str, Any]) -> str:
@@ -627,8 +516,7 @@ def _action_create(a: Dict[str, Any]) -> str:
return tool_error("schedule is required for create", success=False)
canonical_skills = _canonical_skills(a["skill"], a["skills"])
_no_agent = bool(a["no_agent"])
- # no_agent=True -> the script IS the job (prompt/skills optional);
- # otherwise at least one of prompt/skills is required.
+ # no_agent=True -> the script IS the job (prompt/skills optional); else prompt or skills.
if _no_agent:
if not script:
return tool_error(
@@ -636,27 +524,23 @@ def _action_create(a: Dict[str, Any]) -> str:
"the script is the job. In no_agent mode the LLM is "
"skipped entirely: prompt and skills are ignored, "
"non-empty stdout is delivered verbatim, empty stdout "
- "sends nothing (watchdog pattern), and a non-zero "
- "exit or timeout sends an error alert.",
- success=False,
- )
+ "sends nothing (watchdog pattern), and a non-zero exit or timeout sends an error alert.",
+ success=False)
elif not prompt and not canonical_skills:
return tool_error("create requires either prompt or at least one skill", success=False)
error = (
(prompt and _scan_cron_prompt(prompt))
or (script and _validate_cron_script_path(script))
or (a["monitor_script"] and _validate_cron_script_path(a["monitor_script"]))
- # A model-supplied base_url must not route a named provider's stored
- # credential to an attacker endpoint.
+ # A model-supplied base_url must not route a named provider's stored credential
+ # to an attacker endpoint.
or _validate_cron_base_url(a["provider"], a["base_url"])
# bot-chat targets are machine-local: fail the CREATE, not the run.
or _validate_bot_chat_deliver(deliver)
- # failure_deliver shares deliver's grammar and validators (NS-788).
+ # failure_deliver shares deliver's grammar and validators.
or _validate_bot_chat_deliver(_normalize_deliver_param(a["failure_deliver"]))
or (a["context_from"] and _validate_context_from_refs(
- [a["context_from"]] if isinstance(a["context_from"], str) else a["context_from"]
- ))
- )
+ [a["context_from"]] if isinstance(a["context_from"], str) else a["context_from"])))
if error:
return tool_error(error, success=False)
@@ -664,10 +548,7 @@ def _action_create(a: Dict[str, Any]) -> str:
if a["continuity"] is not None:
context_from = _apply_continuity(context_from, a["continuity"])
- from cron.scheduler import (
- CronSchedulerRegistrationError,
- create_job_with_scheduler_registration,
- )
+ from cron.scheduler import CronSchedulerRegistrationError, create_job_with_scheduler_registration
try:
job = create_job_with_scheduler_registration(
@@ -689,13 +570,10 @@ def _action_create(a: Dict[str, Any]) -> str:
attach_to_session=a["attach_to_session"],
monitor_script=_normalize_optional_job_value(a["monitor_script"]),
monitor_url=_normalize_optional_job_value(a["monitor_url"]),
- # CLI-only lane: deliberately absent from CRONJOB_SCHEMA and the
- # model dispatch — models do not make model-config decisions.
+ # CLI-only lane: absent from CRONJOB_SCHEMA and the model dispatch — models
+ # do not make model-config decisions.
reasoning_effort=a["reasoning_effort"],
- failure_deliver=_resolve_cron_context_deliver(
- _normalize_deliver_param(a["failure_deliver"])
- ),
- )
+ failure_deliver=_resolve_cron_context_deliver(_normalize_deliver_param(a["failure_deliver"])))
except CronSchedulerRegistrationError as exc:
_partial = exc.to_dict()
return tool_error(_partial.pop("error"), success=False, **_partial)
@@ -703,9 +581,8 @@ def _action_create(a: Dict[str, Any]) -> str:
_local_notice = _local_delivery_notice(job, deliver)
if _local_notice:
_create_message = f"{_create_message} {_local_notice}"
- # The builtin ticker lives in the gateway process: a job created with no
- # gateway running is stored but never fires — tell the model (the CLI
- # already warns; the agent path otherwise reports a clean success).
+ # The builtin ticker lives in the gateway process: a job created with no gateway
+ # running is stored but never fires — tell the model (the CLI already warns).
_result = {
"success": True,
"job_id": job["id"],
@@ -723,14 +600,6 @@ def _action_create(a: Dict[str, Any]) -> str:
return _dumps(_with_guidance(_result, job, deliver))
-def _with_guidance(result: Dict[str, Any], job: Dict[str, Any], deliver: Optional[str]) -> Dict[str, Any]:
- """Attach mode/delivery guidance (create and update echo the same notes)."""
- _notes = _mode_guidance_notes(job, deliver)
- if _notes:
- result["guidance"] = _notes
- return result
-
-
def _action_list(a: Dict[str, Any]) -> str:
jobs = [_format_job(job) for job in list_jobs(include_disabled=a["include_disabled"])]
_result = {"success": True, "count": len(jobs), "jobs": jobs}
@@ -748,22 +617,10 @@ def _action_remove(job: Dict[str, Any], a: Dict[str, Any]) -> str:
return _dumps({
"success": True,
"message": f"Cron job '{job['name']}' removed.",
- "removed_job": {
- "id": job_id,
- "name": job["name"],
- "schedule": job.get("schedule_display"),
- },
+ "removed_job": {"id": job_id, "name": job["name"], "schedule": job.get("schedule_display")},
})
-def _action_pause(job: Dict[str, Any], a: Dict[str, Any]) -> str:
- return _job_state_result(pause_job(job["id"], reason=a["reason"]))
-
-
-def _action_resume(job: Dict[str, Any], a: Dict[str, Any]) -> str:
- return _job_state_result(resume_job(job["id"]))
-
-
def _job_state_result(updated: Dict[str, Any]) -> str:
_notify_provider_jobs_changed_safe()
return _dumps({"success": True, "job": _format_job(updated)})
@@ -776,19 +633,16 @@ def _refreshed_job_view(job_id: str) -> Dict[str, Any]:
def _action_run(job: Dict[str, Any], a: Dict[str, Any]) -> str:
job_id = job["id"]
- # `prompt` on run is transient per-fire context appended to the stored
- # prompt, never persisted; same strict scan as stored prompts.
+ # `prompt` on run is transient per-fire context appended to the stored prompt, never
+ # persisted; same strict scan as stored prompts.
extra_prompt = a["prompt"] or None
if extra_prompt:
scan_error = _scan_cron_prompt(extra_prompt)
if scan_error:
return tool_error(scan_error, success=False)
- # A manual run must actually run even with no ticker active. Preferred:
- # background dispatch (handle now, outcome as a completion event); falls
- # back to inline execution when the runtime can't receive completions.
- bg = _try_dispatch_background_run(
- job, session_id=a["session_id"], extra_prompt=extra_prompt
- )
+ # A manual run must actually run even with no ticker active. Preferred: background
+ # dispatch (handle now, outcome as a completion event); inline fallback otherwise.
+ bg = _try_dispatch_background_run(job, session_id=a["session_id"], extra_prompt=extra_prompt)
if bg is not None and bg.get("dispatched"):
_notify_provider_jobs_changed_safe()
result = _refreshed_job_view(job_id)
@@ -802,21 +656,19 @@ def _action_run(job: Dict[str, Any], a: Dict[str, Any]) -> str:
"The job is running in the background. You and the "
"user can keep working; its outcome re-enters the "
"conversation as a new message when it finishes. "
- "Do not wait or poll — just continue."
- ),
+ "Do not wait or poll — just continue."),
})
if bg is not None:
exec_result = bg # terminal result: claim lost or inline fallback
else:
- # Relay-fronted manual run: no live adapter here — forward to the
- # running gateway, whose adapter owns that delivery.
+ # Relay-fronted manual run: no live adapter here — forward to the running gateway.
forwarded = _forward_relay_fronted_run(job, extra_prompt=extra_prompt)
if forwarded is not None:
return forwarded
exec_result = _execute_job_now(job, extra_prompt=extra_prompt)
- # A claimed direct run advances next_run_at and may race an external
- # provider's one-shot for the same occurrence; a lost consumed fire cannot
- # re-arm itself, so reconcile after the run has persisted its final state.
+ # A claimed direct run advances next_run_at and may race an external provider's
+ # one-shot for the same occurrence; a lost consumed fire cannot re-arm itself, so
+ # reconcile after the run has persisted its final state.
claimed = exec_result.get("claimed", False)
if claimed:
_notify_provider_jobs_changed_safe()
@@ -825,8 +677,7 @@ def _action_run(job: Dict[str, Any], a: Dict[str, Any]) -> str:
result["execution_success"] = exec_result.get("success", False)
if not claimed:
result["execution_skipped"] = exec_result.get("error") or (
- "Already being fired by the scheduler; not run again."
- )
+ "Already being fired by the scheduler; not run again.")
elif exec_result.get("error"):
result["execution_error"] = exec_result["error"]
return _dumps({"success": True, "job": result})
@@ -846,8 +697,8 @@ def _update_core_fields(job: Dict[str, Any], a: Dict[str, Any], updates: Dict[st
return scan_error
updates["prompt"] = prompt
if a["name"] is not None and a["name"].strip():
- # Blank name is a no-op, not a clear: a model re-sending the whole
- # schema with type-default empties must not wipe untouched fields.
+ # Blank name is a no-op, not a clear: a model re-sending the whole schema with
+ # type-default empties must not wipe untouched fields.
updates["name"] = a["name"]
if deliver is not None:
bot_chat_error = _validate_bot_chat_deliver(_normalize_deliver_param(deliver))
@@ -855,10 +706,9 @@ def _update_core_fields(job: Dict[str, Any], a: Dict[str, Any], updates: Dict[st
return bot_chat_error
updates["deliver"] = _resolve_cron_context_deliver(_normalize_deliver_param(deliver))
if a["failure_deliver"] is not None:
- # '' clears the override (job falls back to deliver on failures);
- # non-empty values share deliver's validation AND its cron-context
- # origin resolution (a job created from inside a cron run must never
- # store literal 'origin' — same rule as deliver).
+ # '' clears the override (failures fall back to deliver); non-empty values share
+ # deliver's validation AND its cron-context origin resolution (a job created from
+ # inside a cron run must never store literal 'origin').
_norm_fd = _normalize_deliver_param(a["failure_deliver"])
if _norm_fd:
bot_chat_error = _validate_bot_chat_deliver(_norm_fd)
@@ -879,10 +729,9 @@ def _update_core_fields(job: Dict[str, Any], a: Dict[str, Any], updates: Dict[st
if a["reasoning_effort"] is not None:
# CLI-only lane; update_job validates, empty string clears the pin.
updates["reasoning_effort"] = a["reasoning_effort"]
- # Re-validate the EFFECTIVE provider/base_url on EVERY update: a job
- # persisted before this guard may already hold an unsafe pair, and editing
- # an unrelated field must not leave it schedulable. Merging this update's
- # values over the stored job lets an operator remediate in the same call.
+ # Re-validate the EFFECTIVE provider/base_url on EVERY update: a job persisted before
+ # this guard may hold an unsafe pair, and editing an unrelated field must not leave it
+ # schedulable. Merging this update over the stored job lets an operator remediate.
return _validate_cron_base_url(_pick(updates, job, "provider"), _pick(updates, job, "base_url"))
@@ -899,24 +748,19 @@ def _update_script_fields(job: Dict[str, Any], a: Dict[str, Any], updates: Dict[
if monitor_url is not None:
updates["monitor_url"] = _normalize_optional_job_value(monitor_url) if monitor_url else None
if (monitor_script is not None or monitor_url is not None) and (
- _pick(updates, job, "monitor_script") and _pick(updates, job, "monitor_url")
- ):
- return (
- "monitor_script and monitor_url are mutually exclusive — "
- "clear one before setting the other."
- )
+ _pick(updates, job, "monitor_script") and _pick(updates, job, "monitor_url")):
+ return("monitor_script and monitor_url are mutually exclusive — clear one before setting the other.")
return None
def _update_context_from(job: Dict[str, Any], a: Dict[str, Any], updates: Dict[str, Any]) -> Optional[str]:
- """context_from / continuity: empty string / list clears; otherwise every
- ref must exist. Stored as a list (or None) to match create_job()."""
+ """context_from / continuity: empty string / list clears; otherwise every ref must
+ exist. Stored as a list (or None) to match create_job()."""
context_from, continuity = a["context_from"], a["continuity"]
if context_from is None and continuity is None:
return None
if context_from is None:
- # continuity-only update: start from the job's stored refs.
- context_from = list(job.get("context_from") or [])
+ context_from = list(job.get("context_from") or []) # continuity-only update
refs = _clean_str_list(context_from)
if continuity is not None:
refs = _apply_continuity(refs, continuity) or []
@@ -938,18 +782,15 @@ def _update_run_fields(job: Dict[str, Any], a: Dict[str, Any], updates: Dict[str
# Empty string clears; otherwise update_job() validates/normalizes.
updates["workdir"] = _normalize_optional_job_value(a["workdir"]) or None
if a["no_agent"] is not None:
- # Flipping to True needs a script on the job or in this same update,
- # otherwise the next tick would error out.
+ # Flipping to True needs a script on the job or in this same update.
target_no_agent = bool(a["no_agent"])
if target_no_agent and not _pick(updates, job, "script"):
return (
"Cannot set no_agent=True on a job without a script. "
- "Set `script` in the same update, or on the job first."
- )
+ "Set `script` in the same update, or on the job first.")
updates["no_agent"] = target_no_agent
if a["repeat"] is not None:
- # Shared chokepoint coerces string forms ('forever'/'once'/'3') and
- # 0/negative values (a bare `repeat <= 0` raised TypeError on strings).
+ # Shared chokepoint coerces string forms ('forever'/'once'/'3') and 0/negative.
from cron.jobs import normalize_repeat_value
repeat_state = dict(job.get("repeat") or {})
repeat_state["times"] = normalize_repeat_value(a["repeat"])
@@ -980,16 +821,15 @@ def _action_update(job: Dict[str, Any], a: Dict[str, Any]) -> str:
_notify_provider_jobs_changed_safe()
# An update can switch modes or delivery — echo the same guidance as create.
return _dumps(_with_guidance(
- {"success": True, "job": _format_job(updated)}, updated, _normalize_deliver_param(a["deliver"])
- ))
+ {"success": True, "job": _format_job(updated)}, updated, _normalize_deliver_param(a["deliver"])))
# Actions that need no job_id, and job-bound actions (job resolved first).
_JOBLESS_ACTIONS = {"create": _action_create, "list": _action_list}
_JOB_ACTIONS = {
"remove": _action_remove,
- "pause": _action_pause,
- "resume": _action_resume,
+ "pause": lambda job, a: _job_state_result(pause_job(job["id"], reason=a["reason"])),
+ "resume": lambda job, a: _job_state_result(resume_job(job["id"])),
"run": _action_run,
"run_now": _action_run,
"trigger": _action_run,
@@ -1006,12 +846,7 @@ def _resolve_job_or_error(job_id: str):
"success": False,
"error": str(exc),
"matches": [
- {
- "id": m["id"],
- "name": m.get("name"),
- "schedule": m.get("schedule_display"),
- "next_run_at": m.get("next_run_at"),
- }
+ {"id": m["id"], "name": m.get("name"), "schedule": m.get("schedule_display"), "next_run_at": m.get("next_run_at")}
for m in exc.matches
],
})
@@ -1049,32 +884,26 @@ def cronjob(
reasoning_effort: Optional[str] = None,
failure_deliver: Optional[Union[str, List[str]]] = None,
task_id: str = None,
- session_id: Optional[str] = None,
-) -> str:
+ session_id: Optional[str] = None) -> str:
"""Unified cron job management tool."""
a = dict(locals())
del a["task_id"] # unused but kept for handler signature compatibility
-
try:
normalized = (action or "").strip().lower()
-
handler = _JOBLESS_ACTIONS.get(normalized)
if handler is not None:
return handler(a)
-
if not job_id:
return tool_error(f"job_id is required for action '{normalized}'", success=False)
- # Job resolution precedes the action check (an unknown action on a
- # missing job reports the missing job) — preserved ordering.
+ # Job resolution precedes the action check (an unknown action on a missing job
+ # reports the missing job) — preserved ordering.
job, error = _resolve_job_or_error(job_id)
if error is not None:
return error
-
handler = _JOB_ACTIONS.get(normalized)
if handler is None:
return tool_error(f"Unknown cron action '{action}'", success=False)
return handler(job, a)
-
except Exception as e:
return tool_error(str(e), success=False)
@@ -1167,9 +996,8 @@ Jobs run in a fresh session with no current-chat context, so prompts must be sel
def check_cronjob_requirements() -> bool:
- """Available in interactive CLI mode and gateway/messaging platforms (the
- scheduler is internal; no crontab needed). Flags must be explicitly truthy
- via the shared ``env_var_enabled`` helper."""
+ """Available in interactive CLI mode and gateway/messaging platforms (the scheduler is
+ internal; no crontab needed). Flags must be explicitly truthy via ``env_var_enabled``."""
from utils import env_var_enabled
return (
@@ -1179,15 +1007,10 @@ def check_cronjob_requirements() -> bool:
)
-# --- Registry ---
-from tools.registry import registry, tool_error
-
-
-# Agent-facing arguments forwarded verbatim to cronjob(). model / provider /
-# base_url are intentionally NOT in this list: per-job inference pins are
-# user-owned (dashboard, `hermes cron create/edit --model`, or hand-edited
-# jobs). The agent must not be able to point unattended spend at a different
-# model. Programmatic callers of cronjob() itself retain the parameters.
+# Agent-facing arguments forwarded verbatim to cronjob(). model / provider / base_url are
+# intentionally NOT here: per-job inference pins are user-owned (dashboard, `hermes cron
+# create/edit --model`, hand-edited jobs) — the agent must not point unattended spend at a
+# different model. Programmatic callers of cronjob() itself retain the parameters.
_HANDLER_FORWARDED_ARGS = (
"job_id", "prompt", "schedule", "name", "repeat", "deliver", "failure_deliver", "skill", "skills",
"reason", "script", "context_from", "continuity", "enabled_toolsets", "workdir",
@@ -1196,12 +1019,9 @@ _HANDLER_FORWARDED_ARGS = (
def _cronjob_handler(args, **kw):
- """Model-tool dispatch for ``cronjob``: resolves the one model-facing
- ``monitor`` field into the stored ``monitor_script``/``monitor_url`` pair
- (legacy field names still accepted for older transcripts)."""
- _mon_script, _mon_url = _split_monitor_arg(
- args.get("monitor"), args.get("monitor_script"), args.get("monitor_url")
- )
+ """Model-tool dispatch: resolves the one model-facing ``monitor`` field into the stored
+ ``monitor_script``/``monitor_url`` pair (legacy field names still accepted)."""
+ _mon_script, _mon_url = _split_monitor_arg(args.get("monitor"), args.get("monitor_script"), args.get("monitor_url"))
return cronjob(
action=args.get("action", ""),
include_disabled=args.get("include_disabled", True),