Skip to content

The agent that grows with you

refactor(cron): one claim-TTL headroom constant shared by the job store and the ledger

kshitijk4poor2d0867695da0
diff --git a/cron/constants.py b/cron/constants.py
index 33cd96e5260..0e4649f0817 100644
--- a/cron/constants.py
+++ b/cron/constants.py
@@ -11,3 +11,7 @@ FIRE_CLAIM_TTL_SECONDS = 300
# ``next_run_at`` (the fire scheduler's clock runs ahead of ours). Claims that early still own
# the slot; only claims further ahead are off-tick manual/dashboard fires.
FIRE_CLAIM_SKEW_SECONDS = 60
+# Multiplier over HERMES_CRON_TIMEOUT for claim TTLs: the timeout is an *inactivity* limit, not a
+# wall-clock cap, so healthy runs may legitimately exceed it and a TTL of exactly the timeout
+# would expire live claims.
+CLAIM_TTL_INACTIVITY_HEADROOM = 3
diff --git a/cron/executions.py b/cron/executions.py
index 3ac83336127..cc4c0c54ff3 100644
--- a/cron/executions.py
+++ b/cron/executions.py
@@ -20,6 +20,7 @@ from typing import Any, Dict, Iterator, List, Optional
from hermes_constants import get_hermes_home
from hermes_time import now as _hermes_now
+from cron.constants import CLAIM_TTL_INACTIVITY_HEADROOM
# Optional test override. Production resolves the path at transaction time so dashboard operations
# that temporarily enter another profile cannot leak that profile's records into the import-time
@@ -29,10 +30,6 @@ MAX_TERMINAL_EXECUTIONS = 1000
HANDOFF_ADOPTION_GRACE_SECONDS = 30.0
# Floor for the live-owner stale-claim bound (#115692); see _live_owner_stale_after_seconds.
LIVE_OWNER_STALE_CLAIM_FLOOR_SECONDS = 7200.0
-# Headroom over the inactivity timeout, mirroring cron/jobs.py::_oneshot_run_claim_ttl_seconds:
-# HERMES_CRON_TIMEOUT is an *inactivity* limit, not a wall-clock cap, so healthy runs may
-# legitimately exceed it.
-_LIVE_OWNER_STALE_CLAIM_HEADROOM = 3
_TERMINAL_STATES = ("completed", "failed", "unknown")
_lock = threading.RLock()
_PROCESS_ID = uuid.uuid4().hex
@@ -157,7 +154,7 @@ def _live_owner_stale_after_seconds() -> Optional[float]:
if not math.isfinite(inactivity) or inactivity <= 0:
return None
return max(
- inactivity * _LIVE_OWNER_STALE_CLAIM_HEADROOM,
+ inactivity * CLAIM_TTL_INACTIVITY_HEADROOM,
float(_get_script_timeout()),
LIVE_OWNER_STALE_CLAIM_FLOOR_SECONDS,
)
diff --git a/cron/jobs.py b/cron/jobs.py
index 5a749440e3a..fceda2f33ee 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -28,7 +28,7 @@ except ImportError: # pragma: no cover - non-Windows
from datetime import datetime, timedelta, timezone
from pathlib import Path
from hermes_constants import get_hermes_home
-from cron.constants import FIRE_CLAIM_SKEW_SECONDS, FIRE_CLAIM_TTL_SECONDS
+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 typing import Optional, Dict, List, Any, Callable, Set, Tuple, Union, Collection
@@ -188,10 +188,8 @@ def get_cron_output_dir() -> Path:
# mid-run.
ONESHOT_RUN_CLAIM_TTL_SECONDS = 1800
-# Derived TTL = inactivity timeout × this headroom. The TTL only recovers a claim left by a tick
-# that DIED mid-run; the timeout is an *inactivity* limit, not a wall-clock cap, so healthy runs may
-# legitimately exceed it — hence the headroom.
-_ONESHOT_RUN_CLAIM_TTL_HEADROOM = 3
+# Derived TTL = inactivity timeout × CLAIM_TTL_INACTIVITY_HEADROOM (cron/constants.py). The TTL
+# only recovers a claim left by a tick that DIED mid-run.
_DEFAULT_CRON_INACTIVITY_TIMEOUT = 600.0
@@ -206,7 +204,7 @@ def _oneshot_run_claim_ttl_seconds() -> float:
timeout = _DEFAULT_CRON_INACTIVITY_TIMEOUT
if timeout <= 0:
return float(ONESHOT_RUN_CLAIM_TTL_SECONDS)
- return max(timeout * _ONESHOT_RUN_CLAIM_TTL_HEADROOM, float(ONESHOT_RUN_CLAIM_TTL_SECONDS))
+ return max(timeout * CLAIM_TTL_INACTIVITY_HEADROOM, float(ONESHOT_RUN_CLAIM_TTL_SECONDS))
def _job_running_in_this_process(job_id: str) -> bool: