b4fbd6fe9f
Deploy Site / deploy-vercel (push) Has been skipped
Deploy Site / deploy-docs (push) Has been skipped
Build Skills Index / build-index (push) Has been skipped
CI / Deny unrelated histories (push) Has been skipped
CI / Detect affected areas (push) Successful in 27m35s
CI / OSV scan (push) Failing after 4s
CI / Build&Test Docker image (push) Successful in 9s
CI / Supply-chain scan (push) Has been skipped
CI / Lint Docker scripts (push) Failing after 5m13s
CI / Check contributors (push) Failing after 12m8s
CI / Docs Site (push) Failing after 12m8s
CI / TypeScript (push) Failing after 12m8s
CI / Python lints (push) Failing after 12m9s
CI / Python tests (push) Failing after 12m9s
CI / Check uv.lock (push) Failing after 23m22s
CI / CI timing report (push) Has been cancelled
Build Skills Index / trigger-deploy (push) Has been cancelled
CI / All required checks pass (push) Has been cancelled
65 lines
2.3 KiB
Python
65 lines
2.3 KiB
Python
"""Detect when the gateway is running stale code after a hot ``git pull``.
|
|
|
|
The gateway is a single long-lived process; its ``sys.modules`` is frozen at
|
|
boot. If the checkout is updated underneath it (a manual ``git pull``, or the
|
|
window before ``hermes update``'s graceful restart fires), a first-time lazy
|
|
import on a new code path can resolve a freshly-pulled consumer module against a
|
|
stale cached dependency -> ImportError (see
|
|
``tests/test_stale_utils_module_import.py`` for the exact failure).
|
|
|
|
We snapshot the checkout revision at gateway startup and compare on demand, so
|
|
risky callers (e.g. ``/model`` switching) can refuse with a clear "restart the
|
|
gateway" message instead of crashing on a cryptic import error.
|
|
|
|
If the revision can't be read (non-git install, IO error), the boot snapshot
|
|
stays ``None`` and skew detection no-ops — it never produces a false positive.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
_PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
|
_boot_fingerprint: str | None = None
|
|
|
|
|
|
def _fingerprint() -> str | None:
|
|
"""Current checkout fingerprint, reusing the CLI's git-rev reader.
|
|
|
|
``hermes_cli.main`` is always already imported in a gateway process (it's
|
|
the entry point), so this import is free and avoids duplicating the
|
|
worktree-aware ref resolution.
|
|
"""
|
|
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]
|
|
if sha and sha != "unresolved" and len(sha) > 10:
|
|
return sha[:10]
|
|
return sha or fingerprint
|
|
|
|
|
|
def detect_code_skew() -> tuple[str, str] | None:
|
|
"""Return ``(boot_rev, disk_rev)`` short labels if the checkout drifted
|
|
since boot, else ``None``."""
|
|
if _boot_fingerprint is None:
|
|
return None
|
|
current = _fingerprint()
|
|
if current is None or current == _boot_fingerprint:
|
|
return None
|
|
return _short(_boot_fingerprint), _short(current)
|