refactor(cron): move _claim_owner_is_dead into scheduler_ownership to keep jobs.py under its size cap

Code-health ratchet: cron/jobs.py is capped at 3495 lines (its value on
main). The next commit moves the store-unwritable warning helper down into
jobs.py (gate r1 quality finding), which would push it over. Offset that by
a pure move of the self-contained claim-owner liveness probe into the
existing ownership sibling; body is byte-identical, its only caller
(_claim_is_live) late-imports it, and nothing in tests/ patches it.
This commit is contained in:
kshitijk4poor
2026-10-05 16:20:29 +05:30
committed by kshitij
parent a14bd84d74
commit e774d81fe8
2 changed files with 19 additions and 18 deletions

View File

@@ -2227,23 +2227,6 @@ def trigger_job(job_id: str, extra_prompt: Optional[str] = None) -> Optional[Dic
})
def _claim_owner_is_dead(claim: Dict[str, Any]) -> bool:
"""True when the claim's ``by`` names a process on THIS host that provably no longer exists.
``_machine_id()`` stamps ``host:pid[:token]``; a foreign host, an explicit HERMES_MACHINE_ID,
or any liveness-probe failure returns False (fail safe: only a proven death shortens the TTL)."""
parts = str(claim.get("by") or "").split(":")
if len(parts) < 2 or not parts[1].isdigit():
return False
try:
import socket
if parts[0] != socket.gethostname():
return False
from gateway.status import _pid_exists
return not _pid_exists(int(parts[1]))
except Exception:
return False
def _claim_is_live(claim: Any, now: datetime, ttl_seconds: float) -> bool:
"""True for a well-formed claim aged within ``[0, ttl)`` whose owner is not provably dead:
future-dated (clock/TZ skew) or malformed claims count as stale so they can never wedge a
@@ -2255,6 +2238,7 @@ def _claim_is_live(claim: Any, now: datetime, ttl_seconds: float) -> bool:
claimed_at = _parse_aware(claim["at"])
if claimed_at is None or not (0 <= _elapsed_seconds(now, claimed_at) < ttl_seconds):
return False
from cron.scheduler_ownership import _claim_owner_is_dead
return not _claim_owner_is_dead(claim)

View File

@@ -20,7 +20,7 @@ from __future__ import annotations
import threading
from pathlib import Path
from typing import Any, Optional, Union
from typing import Any, Dict, Optional, Union
from hermes_constants import get_hermes_home, hermes_home_key
@@ -135,3 +135,20 @@ def live_gateway_ticking(home: Optional[Union[Path, str]] = None) -> Optional[di
except Exception:
return None
return record if record_serves_profile(record, home) else None
def _claim_owner_is_dead(claim: Dict[str, Any]) -> bool:
"""True when the claim's ``by`` names a process on THIS host that provably no longer exists.
``_machine_id()`` stamps ``host:pid[:token]``; a foreign host, an explicit HERMES_MACHINE_ID,
or any liveness-probe failure returns False (fail safe: only a proven death shortens the TTL)."""
parts = str(claim.get("by") or "").split(":")
if len(parts) < 2 or not parts[1].isdigit():
return False
try:
import socket
if parts[0] != socket.gethostname():
return False
from gateway.status import _pid_exists
return not _pid_exists(int(parts[1]))
except Exception:
return False