The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): third pass on shutdown_watchdog/flush/forensics
Teknium0765aca884d6
diff --git a/gateway/shutdown_flush.py b/gateway/shutdown_flush.pyindex 8b211424f01..1f58fa1b844 100644--- a/gateway/shutdown_flush.py+++ b/gateway/shutdown_flush.py@@ -76,9 +76,7 @@ def _flush_value(flush_dir: Path, kind: str, session_key: str, value: Any, **extdef flush_pending_to_file(pending: Dict[str, Any], *, reason: str = "shutdown") -> int:- """Serialise non-empty ``_pending_messages`` slots (``MessageEvent`` or str) to disk; return- sessions flushed.- """+ """Serialise non-empty ``_pending_messages`` slots (``MessageEvent`` or str); return count."""if not pending:return 0flush_dir, ts, flushed = _get_flush_dir(), int(time.time()), 0@@ -105,31 +103,22 @@ def flush_overflow_to_file(overflow_by_session: Dict[str, Any], *, reason: str =continuefor seq, value in enumerate(list(events)):if value is not None:- flushed += _flush_value(- flush_dir, "overflow", session_key, value, reason=reason, ts=ts, seq=seq- )+ flushed += _flush_value(flush_dir, "overflow", session_key, value, reason=reason,+ ts=ts, seq=seq)if flushed:- logger.info(- "Flushed %d queued overflow message(s) to %s (reason=%s)", flushed, flush_dir, reason- )+ logger.info("Flushed %d queued overflow message(s) to %s (reason=%s)", flushed, flush_dir,+ reason)return flusheddef spool_dropped_transcript_message(session_id: str, message: Dict[str, Any]) -> Optional[Path]:- """Spool a cap-evicted transcript message; ``None`` on failure (callers degrade to drop-and-- log).- """+ """Spool a cap-evicted transcript message; ``None`` on failure (callers degrade to drop+log)."""try:- return _write_payload(- _get_flush_dir(),- {- "session_key": session_id,- "reason": TRANSCRIPT_CAP_DROP_REASON,- "ts": int(time.time()),- "seq": next(_TRANSCRIPT_SPOOL_SEQ),- "data": {"session_id": session_id, "message": message},- },- )+ return _write_payload(_get_flush_dir(), {+ "session_key": session_id, "reason": TRANSCRIPT_CAP_DROP_REASON, "ts": int(time.time()),+ "seq": next(_TRANSCRIPT_SPOOL_SEQ),+ "data": {"session_id": session_id, "message": message},+ })except Exception as exc:logger.debug("Failed to spool cap-dropped transcript message for %s: %s", session_id, exc)return None@@ -152,10 +141,8 @@ def drain_transcript_spool(session_id: str, replay) -> tuple[int, int]:payload = json.loads(path.read_text(encoding="utf-8"))except Exception:continue- if (- payload.get("reason") != TRANSCRIPT_CAP_DROP_REASON- or payload.get("session_key") != session_id- ):+ if (payload.get("reason") != TRANSCRIPT_CAP_DROP_REASON+ or payload.get("session_key") != session_id):continuemessage = (payload.get("data") or {}).get("message")if not isinstance(message, dict):@@ -175,10 +162,8 @@ def drain_transcript_spool(session_id: str, replay) -> tuple[int, int]:path.unlink(missing_ok=True)replayed += 1if replayed:- logger.info(- "Replayed %d spooled transcript message(s) for %s after DB recovery", replayed,- session_id,- )+ logger.info("Replayed %d spooled transcript message(s) for %s after DB recovery", replayed,+ session_id)return replayed, remaining@@ -194,9 +179,8 @@ def _serialise_value(value: Any) -> Optional[dict]:"""Convert a pending message value to a JSON-serialisable dict."""if hasattr(value, "text"): # MessageEvent-like objectresult: Dict[str, Any] = {"text": getattr(value, "text", "")}- for attr in (- "session_id", "platform", "sender_id", "sender_name", "reply_to", "media", "raw_event",- ):+ for attr in ("session_id", "platform", "sender_id", "sender_name", "reply_to", "media",+ "raw_event"):val = getattr(value, attr, None)if val is not None:result[attr] = val if _json_safe(val) else str(val)@@ -242,8 +226,7 @@ def recover_pending_to_db(session_db=None) -> int:def _recover_one_payload(session_db, path: Path, payload: Dict[str, Any]) -> bool:- """Append one flush payload to ``session_db``; False (file preserved) when structurally invalid.- """+ """Append one flush payload to ``session_db``; False (file kept) when structurally invalid."""if payload.get("reason") == TRANSCRIPT_CAP_DROP_REASON:# Cap-dropped payloads carry the full message dict keyed by session_id — replay directly.data = payload.get("data", {}) or {}@@ -252,11 +235,9 @@ def _recover_one_payload(session_db, path: Path, payload: Dict[str, Any]) -> boologger.warning("Cannot recover structurally invalid transcript spool ""file %s; preserved for manual inspection", path)return False- session_db.append_message(- session_id=spooled_sid, role=message.get("role", "unknown"),- content=message.get("content") or "",- timestamp=message.get("timestamp") or payload.get("ts"),- )+ session_db.append_message(session_id=spooled_sid, role=message.get("role", "unknown"),+ content=message.get("content") or "",+ timestamp=message.get("timestamp") or payload.get("ts"))return Truesession_key, data = payload.get("session_key", ""), payload.get("data", {})text = data.get("text", "")@@ -272,10 +253,8 @@ def _recover_one_payload(session_db, path: Path, payload: Dict[str, Any]) -> boo"session_key-to-id resolution is not available at this recovery stage. ""The message text is preserved in %s", session_key, path)return False- session_db.append_message(- session_id=session_id, role="user", content=text,- timestamp=payload.get("ts", int(time.time())),- )+ session_db.append_message(session_id=session_id, role="user", content=text,+ timestamp=payload.get("ts", int(time.time())))return True@@ -291,24 +270,17 @@ def flush_agent_history_to_file(session_id: Optional[str], history: list) -> Nonsnapshot = []for _m in history:try:- snapshot.append(- _m- if isinstance(_m, (dict, list, str, int, float, bool, type(None)))- else str(_m)- )+ plain = isinstance(_m, (dict, list, str, int, float, bool, type(None)))+ snapshot.append(_m if plain else str(_m))except Exception:continue_write_payload(flush_dir, {"reason": "shutdown-with-unpersisted-agent-history", "issue": "#72680","session_id": session_id, "count": len(snapshot), "messages": snapshot,})- logger.warning(- "Preserved %d in-memory message(s) for session %s "- "(possible FTS corruption — recover after repairing state.db)",- len(snapshot),- session_id,- )+ logger.warning("Preserved %d in-memory message(s) for session %s "+ "(possible FTS corruption — recover after repairing state.db)",+ len(snapshot), session_id)except Exception as _e:- logger.warning(- "Agent-history shutdown preservation failed for session %s: %s", session_id, _e- )+ logger.warning("Agent-history shutdown preservation failed for session %s: %s", session_id,+ _e)diff --git a/gateway/shutdown_forensics.py b/gateway/shutdown_forensics.pyindex ba9ff4f331a..7b634a99c3b 100644--- a/gateway/shutdown_forensics.py+++ b/gateway/shutdown_forensics.py@@ -83,20 +83,14 @@ def snapshot_shutdown_context(received_signal: Any = None) -> Dict[str, Any]:timestamps. Pure stdlib, never raises, never blocks."""pid, ppid = os.getpid(), os.getppid()ctx: Dict[str, Any] = {- "ts": time.time(),- "ts_monotonic": time.monotonic(),+ "ts": time.time(), "ts_monotonic": time.monotonic(),"signal": _signal_name(received_signal),"signal_num": int(received_signal) if received_signal is not None else None,- "pid": pid,- "ppid": ppid,- "parent": _proc_summary(ppid),- "self": _proc_summary(pid),+ "pid": pid, "ppid": ppid, "parent": _proc_summary(ppid), "self": _proc_summary(pid),}- # INVOCATION_ID is set by systemd units; ppid==1 also strongly suggests systemd forwarded the- # SIGTERM.- for ctx_key, env_key in (- ("systemd_invocation_id", "INVOCATION_ID"), ("systemd_journal_stream", "JOURNAL_STREAM"),- ):+ # INVOCATION_ID is set by systemd units; ppid==1 also suggests systemd forwarded the SIGTERM.+ for ctx_key, env_key in (("systemd_invocation_id", "INVOCATION_ID"),+ ("systemd_journal_stream", "JOURNAL_STREAM")):if os.environ.get(env_key):ctx[ctx_key] = os.environ[env_key]ctx["under_systemd"] = bool(os.environ.get("INVOCATION_ID")) or ppid == 1@@ -117,18 +111,16 @@ def snapshot_shutdown_context(received_signal: Any = None) -> Dict[str, Any]:raw = _read_marker(Path(hermes_home_str) / ".gateway-takeover.json")if raw is not None:ctx["takeover_marker"] = raw[:300]- ctx["takeover_marker_for_self"] = (- f'"target_pid": {pid}' in raw or f"'target_pid': {pid}" in raw- )+ ctx["takeover_marker_for_self"] = (f'"target_pid": {pid}' in raw+ or f"'target_pid': {pid}" in raw)raw = _read_marker(Path(hermes_home_str) / ".gateway-planned-stop.json")if raw is not None:ctx["planned_stop_marker"] = raw[:300]return ctx-def spawn_async_diagnostic(- log_path: Path, signal_name: str, *, timeout_seconds: float = 5.0-) -> Optional[int]:+def spawn_async_diagnostic(log_path: Path, signal_name: str, *,+ timeout_seconds: float = 5.0) -> Optional[int]:"""Fire-and-forget ``ps``-style snapshot appended to ``log_path``: a detached subprocess (own``timeout`` so a wedged ``ps`` self-cleans) rather than a blocking ``ps aux`` in the signalhandler, which can freeze the loop >2s on a busy host. Returns the subprocess PID, or ``None``@@ -154,14 +146,11 @@ def spawn_async_diagnostic(fd = os.open(str(log_path), os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o644)except OSError:return None- try:- # start_new_session: survive systemd killing our cgroup (KillMode=control-group) long enough- # to flush.+ try: # start_new_session: outlive systemd killing our cgroup (KillMode=control-group) to flushreturn subprocess.Popen(["timeout", f"{timeout_seconds:.0f}", "bash", "-c", script], stdout=fd,stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL, start_new_session=True,- close_fds=True,- ).pid+ close_fds=True).pidexcept OSError:return Nonefinally:@@ -170,16 +159,13 @@ def spawn_async_diagnostic(def format_context_for_log(ctx: Dict[str, Any]) -> str:- """Render a shutdown context dict as a single, scannable log line (parent cmdline is the key- signal).- """+ """Render a shutdown context dict as one scannable log line (parent cmdline is key)."""parent = ctx.get("parent") or {}load_str = f"{load:.2f}" if isinstance(load := ctx.get("loadavg_1m"), (int, float)) else "?"extras: List[str] = []if ctx.get("takeover_marker") is not None:- extras.append(- f"takeover_marker_present={'self' if ctx.get('takeover_marker_for_self') else 'other'}"- )+ who = 'self' if ctx.get('takeover_marker_for_self') else 'other'+ extras.append(f"takeover_marker_present={who}")if ctx.get("planned_stop_marker") is not None:extras.append("planned_stop_marker_present=yes")if ctx.get("tracer_pid"):@@ -215,29 +201,21 @@ def check_systemd_timing_alignment(unit_name: Optional[str] = Nonewith contextlib.suppress(OSError), open("/proc/self/cgroup", encoding="utf-8") as fh:for line in fh:- unit_name = next(- (p for p in reversed(line.strip().split("/")) if p.endswith(".service")), None- )+ parts = reversed(line.strip().split("/"))+ unit_name = next((p for p in parts if p.endswith(".service")), None)if unit_name:breakif (timeout_us := _systemd_timeout_stop_us(unit_name) if unit_name else None) is None:return Nonetimeout_stop_sec = timeout_us / 1_000_000.0expected = float(resolve_systemd_timeout_stop_sec(drain_timeout, cron_drain_timeout))- return {- "unit": unit_name,- "timeout_stop_sec": timeout_stop_sec,- "drain_timeout": drain_timeout,- "cron_drain_timeout": cron_drain_timeout,- "expected_min": expected,- "mismatch": timeout_stop_sec < expected,- }+ return {"unit": unit_name, "timeout_stop_sec": timeout_stop_sec, "drain_timeout": drain_timeout,+ "cron_drain_timeout": cron_drain_timeout, "expected_min": expected,+ "mismatch": timeout_stop_sec < expected}def _systemd_timeout_stop_us(unit_name: str) -> Optional[int]:- """``TimeoutStopUSec`` of ``unit_name`` in microseconds; ``--user`` first (hermes' common case),- then system.- """+ """``TimeoutStopUSec`` of ``unit_name`` in microseconds; ``--user`` first (hermes' usual)."""for flag in (["--user"], []):try:result = subprocess.run(diff --git a/gateway/shutdown_watchdog.py b/gateway/shutdown_watchdog.pyindex 00efda81f9b..9859c5cd955 100644--- a/gateway/shutdown_watchdog.py+++ b/gateway/shutdown_watchdog.py@@ -5,8 +5,7 @@ KeepAlive only restarts a *dead* process. Hence: (1) an OS-thread shutdown watchstacks and ``os._exit``s past ``restart_drain_timeout + grace``; (2) a heartbeat file at``<HERMES_HOME>/state/gateway.heartbeat`` so supervisors can tell "process alive" from "loopfrozen"; (3) a lifetime thread watchdog that hard-exits when the loop is too frozen to run its-own callbacks; (4) a self-rescheduling floor timer that keeps the selector timeout finite.-"""+own callbacks; (4) a self-rescheduling floor timer that keeps the selector timeout finite."""from __future__ import annotations@@ -35,8 +34,8 @@ DEFAULT_HEARTBEAT_INTERVAL_S = 30.0DEFAULT_LOOP_FLOOR_TIMER_INTERVAL_S = 5.0DEFAULT_LOOP_WATCHDOG_INTERVAL_S = 30.0DEFAULT_LOOP_WATCHDOG_TIMEOUT_S = 10.0-# 3 sustained misses (~90-120s of loop block) escalate; stays tight because the heartbeat-# write is off-loop. Slow loops tune gateway.loop_watchdog_* in config.yaml.+# 3 sustained misses (~90-120s of loop block) escalate; stays tight because the heartbeat write is+# off-loop. Slow loops tune gateway.loop_watchdog_* in config.yaml.DEFAULT_LOOP_WATCHDOG_MAX_STRIKES = 3_HEARTBEAT_RELATIVE = ("state", "gateway.heartbeat")_WATCHDOG_DUMP_RELATIVE = ("logs", "gateway-shutdown-watchdog.log")@@ -68,7 +67,6 @@ class _LoopFloorTimerHandle:class _LoopLivenessWatchdogHandle:- """Small lifecycle handle for the daemon liveness thread."""def __init__(self, stop_event: threading.Event, thread: threading.Thread):self._stop_event = stop_eventself.stop, self.join, self.is_alive = stop_event.set, thread.join, thread.is_alive@@ -78,10 +76,8 @@ def _arm_loop_floor_timer(loop: asyncio.AbstractEventLoop, interval: float = DEFAULT_LOOP_FLOOR_TIMER_INTERVAL_S) -> _LoopFloorTimerHandle:"""Keep at least one timer pending so selector waits remain bounded."""- resolved = _coerce_float(interval, 0.0)- return _LoopFloorTimerHandle(- loop, resolved if resolved > 0 else DEFAULT_LOOP_FLOOR_TIMER_INTERVAL_S- )+ iv = _coerce_float(interval, 0.0)+ return _LoopFloorTimerHandle(loop, iv if iv > 0 else DEFAULT_LOOP_FLOOR_TIMER_INTERVAL_S)def start_loop_liveness_watchdog(@@ -90,23 +86,11 @@ def start_loop_liveness_watchdog(max_strikes: int = DEFAULT_LOOP_WATCHDOG_MAX_STRIKES,exit_code: int = GATEWAY_SERVICE_RESTART_EXIT_CODE,) -> Optional[_LoopLivenessWatchdogHandle]:- """Start an out-of-loop watchdog that hard-exits after missed probes. The- ``gateway.loop_watchdog: false`` opt-out is enforced by the caller- (``GatewayRunner._start_loop_liveness_guards``).- """+ """Start an out-of-loop watchdog that hard-exits after missed probes. The caller+ (``GatewayRunner._start_loop_liveness_guards``) enforces the ``gateway.loop_watchdog: false``+ opt-out."""stop_event = threading.Event()- def _wait_for_probe(probe_event: threading.Event) -> Optional[bool]:- """True/False = probe answered / timed out; None = stop requested mid-wait."""- deadline = time.monotonic() + probe_timeout- while not stop_event.is_set():- remaining = deadline - time.monotonic()- if remaining <= 0:- return probe_event.is_set()- if probe_event.wait(timeout=min(remaining, 0.05)):- return True- return None-def _watchdog() -> None:strikes = 0while not stop_event.wait(timeout=probe_interval):@@ -118,15 +102,17 @@ def start_loop_liveness_watchdog(except Exception:logger.debug("Failed to schedule gateway loop liveness probe", exc_info=True)return- responded = _wait_for_probe(probe_event)- if responded is None:+ deadline = time.monotonic() + probe_timeout+ while not stop_event.is_set(): # poll so a stop() mid-wait is honoured within ~50ms+ remaining = deadline - time.monotonic()+ if remaining <= 0 or probe_event.wait(timeout=min(remaining, 0.05)):+ break+ else:return- if responded:+ if probe_event.is_set():strikes = 0continue- # Re-check stop_event between each irreversible step: a late stop() during the dump must- # win.- if stop_event.is_set():+ if stop_event.is_set(): # re-checked before each irreversible step: a late stop() winsreturnstrikes += 1if strikes < max_strikes:@@ -137,17 +123,13 @@ def start_loop_liveness_watchdog(logger.critical("Gateway event loop missed %d consecutive liveness probes; dumping all thread ""stacks and exiting with code %d so the service supervisor can restart it.",- strikes,- exit_code,- )+ strikes, exit_code)try:faulthandler.dump_traceback(all_threads=True)except Exception:logger.debug("Loop liveness faulthandler dump failed", exc_info=True)if stop_event.is_set():return- # Stamp the lifecycle sentinel so the next boot reports "watchdog hard-exit", not- # SIGKILL/OOM._mark_exited_quietly(exit_code, "loop_liveness_watchdog")os._exit(exit_code)thread = threading.Thread(target=_watchdog, daemon=True, name="gateway-loop-liveness-watchdog")@@ -177,43 +159,33 @@ def _home(home: Optional[Path]) -> Path:def get_loop_heartbeat_path(home: Optional[Path] = None) -> Path:- """Return ``<HERMES_HOME>/state/gateway.heartbeat``."""return _home(home).joinpath(*_HEARTBEAT_RELATIVE)def get_loop_tick_socket_path(home: Optional[Path] = None, pid: Optional[int] = None) -> Path:"""``<HERMES_HOME>/state/gateway.loop-tick.<pid>.sock`` — PID-suffixed so a stale node from adead process is never mistaken for this gateway's witness. Served by the loop itself- (``_tick_socket_handler``), so an answer proves the loop dispatches — what the heartbeat can't.- """- return (- _home(home)- / "state"- / f"gateway.loop-tick.{int(pid if pid is not None else os.getpid())}.sock"- )+ (``_tick_socket_handler``), so an answer proves the loop dispatches; the heartbeat cannot."""+ pid = int(pid if pid is not None else os.getpid())+ return _home(home) / "state" / f"gateway.loop-tick.{pid}.sock"def get_shutdown_watchdog_dump_path(home: Optional[Path] = None) -> Path:- """Return the faulthandler / metadata dump path for a fired watchdog."""return _home(home).joinpath(*_WATCHDOG_DUMP_RELATIVE)def write_loop_heartbeat(*, pid: Optional[int] = None, start_time: Optional[float] = None,- home: Optional[Path] = None, extra: Optional[Dict[str, Any]] = None,-) -> Path:+ home: Optional[Path] = None, extra: Optional[Dict[str, Any]] = None) -> Path:"""Atomically rewrite the loop-liveness heartbeat file; never raises.``start_time`` (process start, epoch seconds) lets supervisors detect PID reuse."""path = get_loop_heartbeat_path(home)- payload: Dict[str, Any] = {- "pid": int(pid if pid is not None else os.getpid()),- "updated_at": datetime.now(timezone.utc).isoformat(), "monotonic": time.monotonic(),- }+ payload: Dict[str, Any] = {"pid": int(pid if pid is not None else os.getpid()),+ "updated_at": datetime.now(timezone.utc).isoformat(),+ "monotonic": time.monotonic()}if start_time is not None:payload["start_time"] = float(start_time)- # Cheap memory sample: after an unclean death the last heartbeat is the closest record of memory- # pressure.- with contextlib.suppress(Exception):+ with contextlib.suppress(Exception): # after an unclean death this is the last memory recordfrom gateway.lifecycle_ledger import sample_memoryif mem := sample_memory():payload["mem"] = mem@@ -229,14 +201,12 @@ def write_loop_heartbeat(def resolve_shutdown_watchdog_delay(drain_timeout: float, *, grace_s: float = DEFAULT_SHUTDOWN_WATCHDOG_GRACE_S) -> float:"""Return the wall-clock leash for the shutdown watchdog thread."""- return _coerce_float(drain_timeout, 0.0) + _coerce_float(- grace_s, DEFAULT_SHUTDOWN_WATCHDOG_GRACE_S- )+ grace = _coerce_float(grace_s, DEFAULT_SHUTDOWN_WATCHDOG_GRACE_S)+ return _coerce_float(drain_timeout, 0.0) + grace-def _write_watchdog_dump(- dump_path: Path, *, delay_s: float, snapshot: Optional[Dict[str, Any]]-) -> None:+def _write_watchdog_dump(dump_path: Path, *, delay_s: float,+ snapshot: Optional[Dict[str, Any]]) -> None:"""Best-effort faulthandler + metadata dump before hard-exit."""try:dump_path.parent.mkdir(parents=True, exist_ok=True)@@ -253,8 +223,7 @@ def _write_watchdog_dump(fh.write("(faulthandler.dump_traceback failed)\n")fh.write("--- end dump ---\n")fh.flush()- # Also to stderr so journald/launchd capture it even if the disk is wedged.- with contextlib.suppress(Exception):+ with contextlib.suppress(Exception): # stderr too: journald/launchd get it if disk is wedgedsys.stderr.write(f"Gateway shutdown watchdog fired after {delay_s:.0f}s "f"(pid={os.getpid()}); dumping all thread stacks.\n")sys.stderr.flush()@@ -264,11 +233,10 @@ def _write_watchdog_dump(def arm_shutdown_watchdog(delay_s: float, *, done_event: Optional[threading.Event] = None,snapshot_fn: Optional[Callable[[], Dict[str, Any]]] = None, exit_code: int = 1,- dump_path: Optional[Path] = None, name: str = "gateway-shutdown-watchdog",-) -> threading.Event:- """Arm a daemon-thread hard-exit backstop for a wedged shutdown path.- Exits quietly if ``done_event`` is set within ``delay_s``, else dumps diagnostics and- ``os._exit(exit_code)``. Never raises; returns ``done_event`` for disarming."""+ dump_path: Optional[Path] = None, name: str = "gateway-shutdown-watchdog") -> threading.Event:+ """Arm a daemon-thread hard-exit backstop for a wedged shutdown path: exits quietly if+ ``done_event`` is set within ``delay_s``, else dumps diagnostics and ``os._exit(exit_code)``.+ Never raises; returns ``done_event`` for disarming."""done = done_event if done_event is not None else threading.Event()delay = _coerce_float(delay_s, DEFAULT_SHUTDOWN_WATCHDOG_GRACE_S)if delay <= 0:@@ -281,12 +249,10 @@ def arm_shutdown_watchdog(returnif done.is_set():return- snapshot: Optional[Dict[str, Any]] = None- if snapshot_fn is not None:- try:- snapshot = snapshot_fn()- except Exception as exc:- snapshot = {"snapshot_error": repr(exc)}+ try:+ snapshot = snapshot_fn() if snapshot_fn is not None else None+ except Exception as exc:+ snapshot = {"snapshot_error": repr(exc)}target = dump_path if dump_path is not None else get_shutdown_watchdog_dump_path()_write_watchdog_dump(target, delay_s=delay, snapshot=snapshot)with contextlib.suppress(Exception):@@ -296,8 +262,7 @@ def arm_shutdown_watchdog(with contextlib.suppress(Exception):stream.flush()# Mirror _exit_after_graceful_shutdown: release PID file + runtime lock BEFORE the log drain- # (locks must never be stranded), then drain the async log queue so logger.critical- # reaches the file before os._exit skips atexit.+ # (never strand locks), then drain the log queue so logger.critical lands before os._exit.with contextlib.suppress(Exception):from gateway.status import remove_pid_file, release_gateway_runtime_lockremove_pid_file()@@ -333,9 +298,7 @@ def _sweep_stale_tick_sockets(own_path: Path) -> None:try:for stale in (p for p in own_path.parent.glob("gateway.loop-tick.*.sock") if p != own_path):try:- os.kill(- int(stale.name.split(".")[-2]), 0- ) # windows-footgun: ok — POSIX-only caller+ os.kill(int(stale.name.split(".")[-2]), 0) # windows-footgun: ok — POSIX-onlyexcept (ValueError, IndexError, OSError):stale.unlink(missing_ok=True)except Exception:@@ -344,22 +307,18 @@ def _sweep_stale_tick_sockets(own_path: Path) -> None:async def loop_heartbeat_forever(*, interval_s: float = DEFAULT_HEARTBEAT_INTERVAL_S, start_time: Optional[float] = None,- home: Optional[Path] = None, should_continue: Optional[Callable[[], bool]] = None,-) -> None:- """Rewrite the loop heartbeat file on a cadence until cancelled / gated off.-- Runs on the gateway loop so a frozen loop lets the file age for monitors. The write- (``atomic_json_write`` -> ``os.fsync``) goes to a thread: inline, a stalled filesystem blocked- the loop inside its own heartbeat and the liveness watchdog killed it (WSL2 fsync stalls p99 31s- / max 112s vs a ~90-120s budget). The thread write is *awaited*, never fire-and-forget: an- unawaited task would keep the file fresh while the loop was wedged, and one in-flight write at a- time keeps a long stall from queuing a thread per interval. Off-loop, file freshness alone no- longer proves loop schedulability, so this task also arms a loop-scheduling witness- (``_tick_socket_handler``) recorded as ``loop_tick_socket``; probes must require the witness to- agree before classifying WEDGED.- """+ home: Optional[Path] = None, should_continue: Optional[Callable[[], bool]] = None) -> None:+ """Rewrite the loop heartbeat file on a cadence until cancelled / gated off. Runs on the+ gateway loop so a frozen loop lets the file age for monitors. The fsync write goes to a thread+ (inline, a stalled filesystem blocked the loop inside its own heartbeat and the liveness+ watchdog killed it: WSL2 fsync stalls p99 31s / max 112s vs a ~90-120s budget) but is *awaited*,+ never fire-and-forget: an unawaited task would keep the file fresh while the loop was wedged,+ and one in-flight write at a time keeps a long stall from queuing a thread per interval.+ Off-loop, file freshness no longer proves loop schedulability, so this task also arms a+ loop-scheduling witness (``_tick_socket_handler``, flagged as ``loop_tick_socket``); probes must+ require the witness to agree before classifying WEDGED."""interval = _coerce_float(interval_s, DEFAULT_HEARTBEAT_INTERVAL_S, floor=1.0)- # Arm the witness. Best-effort: a failed bind only disables it, and the payload flag makes+ # Arm the witness, best-effort: a failed bind only disables it and the payload flag makes# probes classify UNKNOWN, never WEDGED (drain backstop stays). asyncio AF_UNIX is POSIX-only# (ungated it raised AttributeError on native Windows), so non-POSIX binds TCP loopback and# publishes ``loop_tick_tcp_port``.@@ -369,9 +328,8 @@ async def loop_heartbeat_forever(tick_socket_path = get_loop_tick_socket_path(home)tick_socket_path.parent.mkdir(parents=True, exist_ok=True)_sweep_stale_tick_sockets(tick_socket_path)- tick_server = await asyncio.start_unix_server(- _tick_socket_handler, path=str(tick_socket_path)- )+ tick_server = await asyncio.start_unix_server(_tick_socket_handler,+ path=str(tick_socket_path))else:tick_server = await asyncio.start_server(_tick_socket_handler, host="127.0.0.1", port=0)for _s in tick_server.sockets or []:@@ -381,34 +339,22 @@ async def loop_heartbeat_forever(breakexcept Exception:tick_server = tick_tcp_port = None- logger.warning(- "Loop tick socket unavailable — liveness probes will have no "- "loop-scheduling witness and will not escalate on a stale heartbeat",- exc_info=True,- )-- async def _write_off_loop() -> None:- # write_loop_heartbeat never raises, so a failure here is an executor problem and must not- # kill the task.- try:- await asyncio.to_thread(- write_loop_heartbeat,- start_time=start_time,- home=home,- extra={- "loop_tick_socket": tick_server is not None,- "loop_tick_tcp_port": tick_tcp_port,- },- )- except Exception:- logger.debug("Loop heartbeat write failed off-loop", exc_info=True)+ logger.warning("Loop tick socket unavailable — liveness probes will have no "+ "loop-scheduling witness and will not escalate on a stale heartbeat",+ exc_info=True)+ extra = {"loop_tick_socket": tick_server is not None, "loop_tick_tcp_port": tick_tcp_port}try:- await _write_off_loop() # immediate first write so monitors see a fresh file at once- while should_continue is None or should_continue():+ while True: # first write is immediate so monitors see a fresh file at once+ try:+ await asyncio.to_thread(write_loop_heartbeat, start_time=start_time, home=home,+ extra=extra)+ except Exception: # write_loop_heartbeat never raises: executor problem, keep the task+ logger.debug("Loop heartbeat write failed off-loop", exc_info=True)+ if should_continue is not None and not should_continue():+ returnawait asyncio.sleep(interval)if should_continue is not None and not should_continue():return- await _write_off_loop()finally:if tick_server is not None:tick_server.close()