The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(serve): one fenced retirement loop for the skew and owner watchdogs
kshitijk4poor7533bd2756b9
diff --git a/hermes_cli/web_server_owner_exit.py b/hermes_cli/web_server_owner_exit.pyindex d8763da5a54..59500fd65f0 100644--- a/hermes_cli/web_server_owner_exit.py+++ b/hermes_cli/web_server_owner_exit.py@@ -14,16 +14,14 @@ the windows between a cleanup and the replacement's write, or another Desktop bufrom __future__ import annotations-import loggingimport threadingimport timefrom pathlib import Pathfrom typing import Callable, Optional-_log = logging.getLogger(__name__)+from hermes_cli.web_server_skew_exit import _run_retirement_watchdogDEFAULT_OWNER_POLL_S = 15.0-_CONFIRMATIONS = 2def should_retire_superseded(*, lock: Optional[dict], my_nonce: str, age_s: float) -> bool:@@ -46,31 +44,14 @@ def start_owner_watchdog(server, *, lock_path: Path, nonce: str,from hermes_cli.dashboard_procs import read_valid_backend_lockread_lock = read_valid_backend_lock- if fence is None:- from hermes_cli.backend_retirement import retirement-- fence = retirementreader: Callable[[Path], Optional[dict]] = read_lockstarted = now()- def _loop() -> None:- seen = polls = 0- while not getattr(server, "should_exit", False) and (max_polls is None or polls < max_polls):- polls += 1- lock = reader(lock_path)- if should_retire_superseded(lock=lock, my_nonce=nonce, age_s=now() - started):- seen += 1- else:- seen = 0- if seen >= _CONFIRMATIONS and lock is not None:- permit = fence.prepare()- if permit.get("ok") and fence.commit(permit.get("token")).get("ok"):- _log.warning("SSH-isolated backend %s was superseded by spawn %s; retiring.",- nonce, lock["spawnNonce"])- server.should_exit = True- return- time.sleep(poll_s)+ def _observe() -> Optional[str]:+ lock = reader(lock_path)+ if lock is None or not should_retire_superseded(lock=lock, my_nonce=nonce, age_s=now() - started):+ return None+ return f"SSH-isolated backend {nonce} was superseded by spawn {lock['spawnNonce']}; retiring."- thread = threading.Thread(target=_loop, daemon=True, name="ssh-isolated-owner-watchdog")- thread.start()- return thread+ return _run_retirement_watchdog(server, observe=_observe, fence=fence, poll_s=poll_s,+ max_polls=max_polls, thread_name="ssh-isolated-owner-watchdog")diff --git a/hermes_cli/web_server_skew_exit.py b/hermes_cli/web_server_skew_exit.pyindex 3d78a704d35..f2cfbd11a8e 100644--- a/hermes_cli/web_server_skew_exit.py+++ b/hermes_cli/web_server_skew_exit.py@@ -45,40 +45,53 @@ def _update_in_progress() -> bool:return True # cannot prove the swap is over: wait-def start_code_skew_watchdog(server, *, skew_fn: Optional[Callable[[], Skew]] = None,- update_probe: Callable[[], bool] = _update_in_progress,- fence=None, poll_s: float = DEFAULT_SKEW_POLL_S,- max_polls: Optional[int] = None) -> threading.Thread:- """Daemon thread that sets ``server.should_exit`` once the loaded code is provably stale and- the backend is provably idle. ``max_polls`` bounds the loop for tests only."""- if skew_fn is None:- from gateway.code_skew import detect_code_skew-- skew_fn = detect_code_skew+def _run_retirement_watchdog(server, *, observe: Callable[[], Optional[str]], fence, poll_s: float,+ max_polls: Optional[int], thread_name: str) -> threading.Thread:+ """Daemon thread shared by the SSH-isolated retirement watchdogs: ``observe`` returns the log+ reason while retirement is warranted (else None); after ``_CONFIRMATIONS`` consecutive reasons+ the backend exits only through the retirement fence (proven idle, admission closed first)."""if fence is None:from hermes_cli.backend_retirement import retirementfence = retirement- read_skew: Callable[[], Skew] = skew_fndef _loop() -> None:seen = polls = 0while not getattr(server, "should_exit", False) and (max_polls is None or polls < max_polls):polls += 1- skew = read_skew()- if should_retire_for_skew(skew=skew, update_in_progress=update_probe()):- seen += 1- else:- seen = 0- if seen >= _CONFIRMATIONS and skew:+ reason = observe()+ seen = seen + 1 if reason else 0+ if seen >= _CONFIRMATIONS:permit = fence.prepare()if permit.get("ok") and fence.commit(permit.get("token")).get("ok"):- _log.warning("SSH-isolated backend loaded %s but the install is now at %s; "- "retiring so its client respawns it on the new code.", *skew)+ _log.warning("%s", reason)server.should_exit = Truereturntime.sleep(poll_s)- thread = threading.Thread(target=_loop, daemon=True, name="ssh-isolated-skew-watchdog")+ thread = threading.Thread(target=_loop, daemon=True, name=thread_name)thread.start()return thread+++def start_code_skew_watchdog(server, *, skew_fn: Optional[Callable[[], Skew]] = None,+ update_probe: Callable[[], bool] = _update_in_progress,+ fence=None, poll_s: float = DEFAULT_SKEW_POLL_S,+ max_polls: Optional[int] = None) -> threading.Thread:+ """Daemon thread that sets ``server.should_exit`` once the loaded code is provably stale and+ the backend is provably idle. ``max_polls`` bounds the loop for tests only."""+ if skew_fn is None:+ from gateway.code_skew import detect_code_skew++ skew_fn = detect_code_skew+ read_skew: Callable[[], Skew] = skew_fn++ def _observe() -> Optional[str]:+ skew = read_skew()+ if not (skew and should_retire_for_skew(skew=skew, update_in_progress=update_probe())):+ return None+ return ("SSH-isolated backend loaded %s but the install is now at %s; "+ "retiring so its client respawns it on the new code." % skew)++ return _run_retirement_watchdog(server, observe=_observe, fence=fence, poll_s=poll_s,+ max_polls=max_polls, thread_name="ssh-isolated-skew-watchdog")