The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cron): re-probe croniter after a transient ImportError instead of latching
liuhao1024a46ebee6f2c9
diff --git a/cron/jobs.py b/cron/jobs.pyindex ac9e8944541..50f507a5733 100644--- a/cron/jobs.py+++ b/cron/jobs.py@@ -8,6 +8,7 @@ from dataclasses import dataclass, fieldimport jsonimport loggingimport shutil+import sysimport threadingimport timeimport os@@ -45,7 +46,13 @@ HAS_CRONITER: Optional[bool] = Nonedef _ensure_croniter() -> bool:- """Import croniter on first use; honor a pre-set HAS_CRONITER override."""+ """Import croniter on first use; honor a pre-set HAS_CRONITER override.++ An ImportError is NOT latched: caching False would pin a single transient failure+ (wrong interpreter, shadowed path) for the whole process lifetime, leaving every+ recurring job's next_run_at None until a gateway restart (#127182). Stay None and+ re-probe on the next call — the finder fails fast when the package is genuinely+ absent, and due-scan recovery re-arms the schedules as soon as the import succeeds."""global croniter, HAS_CRONITERif HAS_CRONITER is None:try:@@ -53,7 +60,7 @@ def _ensure_croniter() -> bool:croniter = _croniterHAS_CRONITER = Trueexcept ImportError:- HAS_CRONITER = False+ passreturn bool(HAS_CRONITER)@@ -1185,8 +1192,9 @@ def compute_next_run(schedule: Dict[str, Any], last_run_at: Optional[str] = Nonelogger.warning("Cannot compute next run for cron schedule %r: 'croniter' is ""not installed. croniter is a core dependency as of v0.9.x; "- "reinstall hermes-agent or run 'pip install croniter' in your runtime env.",- expr)+ "reinstall hermes-agent or run 'pip install croniter' in your runtime env "+ "(interpreter: %s).",+ expr, sys.executable)return None# Anchor cron matching to the CONFIGURED IANA timezone's WALL CLOCK,# not to the UTC offset carried by ``base_time``. croniter ignoresdiff --git a/tests/cron/test_jobs.py b/tests/cron/test_jobs.pyindex 027e47b6ed7..e464d07b8ce 100644--- a/tests/cron/test_jobs.py+++ b/tests/cron/test_jobs.py@@ -876,6 +876,46 @@ class TestMarkJobRun:assert updated["last_error"]assert "croniter" in updated["last_error"].lower()+ def test_transient_croniter_import_error_not_latched(self, tmp_cron_dir, monkeypatch):+ """Regression test for issue #127182.++ A single transient croniter ImportError must not latch HAS_CRONITER=False for+ the process lifetime: once the import succeeds again (wrong interpreter+ restarted, shadowed path fixed), _ensure_croniter() has to report True again+ so compute_next_run() and the due-scan recovery can re-arm recurring jobs+ without a gateway restart.+ """+ pytest.importorskip("croniter") # need it to make the import succeed again+ import builtins++ import cron.jobs as jobs_mod++ job = create_job(prompt="Recurring", schedule="0 7,15,23 * * *")+ assert job["schedule"]["kind"] == "cron"++ # Simulate the transient failure window: the probe import raises once.+ real_import = builtins.__import__++ def failing_import(name, *args, **kwargs):+ if name == "croniter":+ raise ImportError("No module named 'croniter'")+ return real_import(name, *args, **kwargs)++ monkeypatch.setattr(builtins, "__import__", failing_import)+ monkeypatch.setattr(jobs_mod, "croniter", None)+ monkeypatch.setattr(jobs_mod, "HAS_CRONITER", None)+ assert jobs_mod._ensure_croniter() is False+ assert jobs_mod.compute_next_run(job["schedule"]) is None++ # Window over (import works again): WITHOUT resetting HAS_CRONITER — the probe's+ # cached outcome must be re-evaluated, not latched.+ monkeypatch.setattr(builtins, "__import__", real_import)+ assert jobs_mod._ensure_croniter() is True, (+ "a transient ImportError was latched: every recurring job would stay "+ "next_run_at=None until a gateway restart"+ )+ assert jobs_mod.compute_next_run(job["schedule"]) is not None+class TestAdvanceNextRun:"""Tests for advance_next_run() — crash-safety for recurring jobs."""