Skip to content

The agent that grows with you

refactor(cron): move job-mode invariants to cron/jobs_invariants.py

Tekniumc3981f19fa31
diff --git a/cron/jobs.py b/cron/jobs.py
index 136d00b25a6..1ef988ece24 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -28,9 +28,10 @@ except ImportError: # pragma: no cover - non-Windows
msvcrt = None
from datetime import datetime, timedelta, timezone, UTC
from pathlib import Path
-from hermes_constants import display_hermes_home, get_hermes_home
+from hermes_constants import get_hermes_home
from cron.constants import CLAIM_TTL_INACTIVITY_HEADROOM, FIRE_CLAIM_SKEW_SECONDS, FIRE_CLAIM_TTL_SECONDS
from cron.env_settings import cron_env_setting
+from cron.jobs_invariants import _validate_job_mode_invariants
from cron.scheduler_ownership import _claim_owner_is_dead
from cron import store_health
from typing import Optional, Dict, List, Any, Callable, Set, Tuple, Union, Collection
@@ -473,16 +474,6 @@ EMPTY_PAYLOAD_ERROR = (
"skill(s) are set. Provide a prompt, a script, or at least one skill."
)
-NO_AGENT_WITHOUT_SCRIPT_ERROR = (
- "no_agent=True requires a script — with no agent and no script "
- "there is nothing for the job to run."
-)
-
-SCRIPT_IS_COMMAND_LINE_ERROR = (
- "script must name a file under {scripts_dir} (e.g. 'watchdog.sh'), not a shell command line: "
- "{script!r} has no such file. Write the command into a script there and pass its filename."
-)
-
def job_payload_is_empty(job: dict[str, Any]) -> bool:
"""True when a job record has nothing runnable (blank prompt, no script, no skills) AND at
@@ -1773,41 +1764,6 @@ _UPDATE_FIELD_NORMALIZERS: dict[str, Callable[[Any], Any]] = {
}
-def _validate_job_mode_invariants(
- monitor_script: Optional[str],
- monitor_url: Optional[str],
- no_agent: bool,
- script: Optional[str],
-) -> None:
- """Execution-mode invariants shared by create_job and update_job (no bypass via the update
- door)."""
- if monitor_script and monitor_url:
- raise ValueError(
- "monitor_script and monitor_url are mutually exclusive — a job "
- "can only have one monitor source.")
- if (monitor_script or monitor_url) and no_agent:
- raise ValueError(
- "monitor_script/monitor_url cannot be combined with no_agent=True — "
- "the whole point of a monitor job is to suppress or wake the AGENT "
- "based on source changes. Use a plain no_agent script job instead.")
- if no_agent and not script:
- raise ValueError(NO_AGENT_WITHOUT_SCRIPT_ERROR)
- if script and _script_is_command_line(script):
- raise ValueError(SCRIPT_IS_COMMAND_LINE_ERROR.format(
- scripts_dir=display_hermes_home() + "/scripts/", script=script))
-
-
-def _script_is_command_line(script: str) -> bool:
- """A ``script`` with whitespace that resolves to no file is a command line (``echo hi``),
- not a path; run every tick it would deliver "Script not found" as the payload. A plain
- missing filename stays creatable (``hermes cron doctor`` reports it)."""
- if not any(ch.isspace() for ch in script.strip()):
- return False
- from cron.lifecycle_guard import _resolve_script_path
- path = _resolve_script_path(script)
- return path is None or not path.is_file()
-
-
def _oneshot_past_grace_error(run_at: Any) -> ValueError:
return ValueError(
f"Requested one-shot time {run_at} is more than "
diff --git a/cron/jobs_invariants.py b/cron/jobs_invariants.py
new file mode 100644
index 00000000000..2936408d5be
--- /dev/null
+++ b/cron/jobs_invariants.py
@@ -0,0 +1,53 @@
+"""Execution-mode invariants every cron job definition must satisfy.
+
+``create_job`` and ``update_job`` (``cron/jobs.py``) both run these, so an update cannot
+bypass a rule the create door enforces.
+"""
+from typing import Optional
+
+from hermes_constants import display_hermes_home
+
+NO_AGENT_WITHOUT_SCRIPT_ERROR = (
+ "no_agent=True requires a script — with no agent and no script "
+ "there is nothing for the job to run."
+)
+
+SCRIPT_IS_COMMAND_LINE_ERROR = (
+ "script must name a file under {scripts_dir} (e.g. 'watchdog.sh'), not a shell command line: "
+ "{script!r} has no such file. Write the command into a script there and pass its filename."
+)
+
+
+def _validate_job_mode_invariants(
+ monitor_script: Optional[str],
+ monitor_url: Optional[str],
+ no_agent: bool,
+ script: Optional[str],
+) -> None:
+ """Execution-mode invariants shared by create_job and update_job (no bypass via the update
+ door)."""
+ if monitor_script and monitor_url:
+ raise ValueError(
+ "monitor_script and monitor_url are mutually exclusive — a job "
+ "can only have one monitor source.")
+ if (monitor_script or monitor_url) and no_agent:
+ raise ValueError(
+ "monitor_script/monitor_url cannot be combined with no_agent=True — "
+ "the whole point of a monitor job is to suppress or wake the AGENT "
+ "based on source changes. Use a plain no_agent script job instead.")
+ if no_agent and not script:
+ raise ValueError(NO_AGENT_WITHOUT_SCRIPT_ERROR)
+ if script and _script_is_command_line(script):
+ raise ValueError(SCRIPT_IS_COMMAND_LINE_ERROR.format(
+ scripts_dir=display_hermes_home() + "/scripts/", script=script))
+
+
+def _script_is_command_line(script: str) -> bool:
+ """A ``script`` with whitespace that resolves to no file is a command line (``echo hi``),
+ not a path; run every tick it would deliver "Script not found" as the payload. A plain
+ missing filename stays creatable (``hermes cron doctor`` reports it)."""
+ if not any(ch.isspace() for ch in script.strip()):
+ return False
+ from cron.lifecycle_guard import _resolve_script_path
+ path = _resolve_script_path(script)
+ return path is None or not path.is_file()
diff --git a/cron/scheduler.py b/cron/scheduler.py
index cf25b9f3995..515b17b38c9 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -1537,7 +1537,7 @@ def _run_no_agent_job(
script_path = job.get("script")
# Legacy/hand-edited no_agent job without a script: pause it, or it re-fires every tick.
if not str(script_path or "").strip():
- from cron.jobs import NO_AGENT_WITHOUT_SCRIPT_ERROR
+ from cron.jobs_invariants import NO_AGENT_WITHOUT_SCRIPT_ERROR
return _block_and_pause_job(job_id, job_name, NO_AGENT_WITHOUT_SCRIPT_ERROR)