The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(cron): share one ladder decision between plan_retry and will_retry
kshitijk4poor9de3cb2a0e27
diff --git a/cron/unreachable_retry.py b/cron/unreachable_retry.pyindex 8a29c93bbe2..05dde275ebe 100644--- a/cron/unreachable_retry.py+++ b/cron/unreachable_retry.py@@ -20,6 +20,7 @@ run that reaches the model — success or not — resets the ladder. Disable witfrom __future__ import annotationsimport logging+from datetime import datetimefrom typing import Any, Dict, Optionalfrom hermes_time import now as _hermes_now@@ -67,33 +68,42 @@ def _is_recurring(job: Dict[str, Any]) -> bool:return job.get("schedule", {}).get("kind") in {"cron", "interval"}+def _ladder_instant(job: Dict[str, Any], natural_next: Optional[datetime],+ now: datetime) -> Optional[datetime]:+ """The ladder instant ``plan_retry`` parks for this flagged failure, or None when it+ parks nothing: not recurring, paused, disabled, ladder exhausted, or the schedule's+ own *natural_next* fires at or before the rung. The single source of that decision+ for ``plan_retry`` and ``will_retry``."""+ if not _is_recurring(job) or job.get("state") == "paused":+ return None+ attempt = int((job.get(STATE_KEY) or {}).get("attempt") or 0)+ if attempt >= len(RETRY_DELAYS_SECONDS) or not retry_enabled():+ return None+ # late: jobs imports this module's helpers+ from cron.jobs import _instant_at_or_before, _seconds_after++ retry_dt = _seconds_after(now, RETRY_DELAYS_SECONDS[attempt])+ if natural_next is not None and _instant_at_or_before(natural_next, retry_dt):+ return None # the natural occurrence IS the retry+ return retry_dt++def will_retry(job: Dict[str, Any]) -> bool:"""True iff ``plan_retry`` will park a re-run for this flagged failure, so the scheduler- may hold the interim notice: recurring, unpaused, not on its final finite repeat, ladder- not exhausted, enabled, and the rung strictly before the natural occurrence. Called- before ``mark_job_run``."""- if not _is_recurring(job) or job.get("state") == "paused":- return False+ may hold the interim notice. Called before ``mark_job_run``."""repeat = job.get("repeat") or {}times = repeat.get("times")- if times and times > 0 and int(repeat.get("completed") or 0) + 1 >= times:+ if times is not None and times > 0 and int(repeat.get("completed") or 0) + 1 >= times:return False # _advance_after_run completes the job; plan_retry never runs- state = job.get(STATE_KEY) or {}- attempt = int(state.get("attempt") or 0)- if attempt >= len(RETRY_DELAYS_SECONDS):- return False- if not retry_enabled():- return False- from cron.jobs import _instant_after, _parse_aware, _seconds_after, compute_next_run+ from cron.jobs import _parse_aware, compute_next_run- natural_next = _parse_aware(- compute_next_run(job.get("schedule") or {}, _hermes_now().isoformat()))+ now = _hermes_now()+ natural_next = _parse_aware(compute_next_run(job.get("schedule") or {}, now.isoformat()))if natural_next is None:# The natural occurrence is uncomputable (e.g. croniter missing): _advance_after_run# leaves the record state=error — terminal — so plan_retry never runs either.return False- retry_dt = _seconds_after(_hermes_now(), RETRY_DELAYS_SECONDS[attempt])- return _instant_after(natural_next, retry_dt)+ return _ladder_instant(job, natural_next, now) is not Nonedef clear_state(job: Dict[str, Any]) -> None:@@ -117,31 +127,22 @@ def plan_retry(job: Dict[str, Any]) -> bool:the ladder instant when that is sooner than the natural occurrence; exhausted orinapplicable cycles clear state and leave the schedule untouched. Returns True whena retry was scheduled."""- if not _is_recurring(job) or job.get("state") == "paused" or not retry_enabled():+ from cron.jobs import _parse_aware # late: jobs imports this module's helpers++ retry_dt = _ladder_instant(job, _parse_aware(job.get("next_run_at")), _hermes_now())+ attempt = int((job.get(STATE_KEY) or {}).get("attempt") or 0)+ if retry_dt is None:+ if (attempt >= len(RETRY_DELAYS_SECONDS) and _is_recurring(job)+ and job.get("state") != "paused" and retry_enabled()):+ # Ladder exhausted: fall back to the natural schedule and reset so the NEXT+ # occurrence gets a fresh ladder if the network is still down.+ logger.warning(+ "Job '%s': model unreachable after %d automatic re-runs — waiting for the "+ "scheduled occurrence at %s",+ job.get("name", job.get("id", "?")), attempt, job.get("next_run_at"))clear_state(job)return False- state = job.get(STATE_KEY) or {}- attempt = int(state.get("attempt") or 0)- if attempt >= len(RETRY_DELAYS_SECONDS):- # Ladder exhausted: fall back to the natural schedule and reset so the NEXT- # occurrence gets a fresh ladder if the network is still down.- clear_state(job)- logger.warning(- "Job '%s': model unreachable after %d automatic re-runs — waiting for the "- "scheduled occurrence at %s",- job.get("name", job.get("id", "?")), attempt, job.get("next_run_at"))- return Falsedelay = RETRY_DELAYS_SECONDS[attempt]- # late: jobs imports this module's helpers- from cron.jobs import _instant_at_or_before, _parse_aware, _seconds_after-- retry_dt = _seconds_after(_hermes_now(), delay)- natural_next = _parse_aware(job.get("next_run_at"))- if natural_next is not None and _instant_at_or_before(natural_next, retry_dt):- # The schedule fires again sooner than the ladder would — no point consuming an- # attempt; the natural occurrence IS the retry.- clear_state(job)- return Falseretry_at = retry_dt.isoformat()job[STATE_KEY] = {"attempt": attempt + 1, "at": retry_at, "expr": job["schedule"].get("expr")}job["next_run_at"] = retry_at