Files
hermes-agent/gateway/code_skew.py
alt-glitch f733de8041 refactor(paths): fifteen install-root definitions call the one owner
hermes_cli.config.get_project_root, uninstall.get_project_root, the
PROJECT_ROOT constants of status, cron, gateway, setup, tools_config and
web_server, code_skew and runtime_downgrade_exit _PROJECT_ROOT,
runtime_cwd._PACKAGE_ROOT, shared_metrics_catalog._REPO_ROOT,
desktop_update_verify.checkout_root and host_supervisor._repo_root now
return hermes_cli._startup_fast.project_root_str() instead of their own
__file__ arithmetic. Names, values and test seams are unchanged.

Still separate, on purpose: pm.paths (Docker's tool stage ships pm without
hermes_cli._startup_fast), hermes_constants and hermes_bootstrap (they load
before any package), main.py's script-mode bootstrap, the recovery closure
and stub-tree modules (_early_recovery, update_lock, update_pause_record,
venv_sync: tests and recovery load them as lone files), the lexical
linux_desktop_entry root and self_repo_guard's .git probe. The lint baseline
drops by the 15 sites.
2026-11-07 01:17:21 -05:00

60 lines
2.2 KiB
Python

"""Detect when the gateway is running stale code after a hot ``git pull``.
The gateway's ``sys.modules`` is frozen at boot. If the checkout is updated
underneath it, a first-time lazy import can resolve a freshly-pulled module
against a stale cached dependency -> ImportError. We snapshot the revision at
startup so risky callers (e.g. ``/model`` switching) can refuse with a clear
"restart the gateway" message. If the revision can't be read (non-git install,
IO error) the boot snapshot stays ``None`` and detection no-ops — never a false positive.
"""
from __future__ import annotations
from pathlib import Path
from hermes_cli._startup_fast import project_root_str
_PROJECT_ROOT = Path(project_root_str())
_boot_fingerprint: str | None = None
def _fingerprint() -> str | None:
"""Current checkout fingerprint via the CLI's worktree-aware git-rev reader
(``hermes_cli.main`` is always already imported in a gateway process)."""
try:
from hermes_cli.main import _read_git_revision_fingerprint
return _read_git_revision_fingerprint(_PROJECT_ROOT)
except Exception:
return None
def record_boot_fingerprint() -> None:
"""Snapshot the checkout revision at gateway startup (idempotent)."""
global _boot_fingerprint
if _boot_fingerprint is None:
_boot_fingerprint = _fingerprint()
def _short(fingerprint: str) -> str:
"""Render a ``git:<ref>:<sha>`` fingerprint as a compact label."""
sha = fingerprint.rsplit(":", 1)[-1]
return sha[:10] if sha and sha != "unresolved" and len(sha) > 10 else (sha or fingerprint)
def current_code_sha() -> str | None:
"""Full SHA for the checkout currently on disk, or None when unresolved."""
fingerprint = _fingerprint()
if fingerprint is None:
return None
sha = fingerprint.rsplit(":", 1)[-1]
return sha if sha and sha != "unresolved" else None
def detect_code_skew() -> tuple[str, str] | None:
"""``(boot_rev, disk_rev)`` short labels if the checkout drifted since boot, else ``None``."""
current = _fingerprint() if _boot_fingerprint is not None else None
if current is None or current == _boot_fingerprint:
return None
return _short(_boot_fingerprint), _short(current)