From ff223785c64ba78d2bf4ed071d0eea33c1ad70fc Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 2 Sep 2026 21:26:33 -0700 Subject: [PATCH] refactor(gateway): simplify updated_at coercion and home-conflict guard --- gateway/status.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/gateway/status.py b/gateway/status.py index ab1770141b..2873c18abe 100644 --- a/gateway/status.py +++ b/gateway/status.py @@ -124,9 +124,7 @@ def recorded_gateway_home_conflicts( ``hermes_home`` return False (they prove nothing; callers pair this with PID + start-time guards). A comparison failure returns True: destructive + unprovable ownership => fail closed. """ - if not isinstance(record, dict): - return False - recorded_home = record.get("hermes_home") + recorded_home = record.get("hermes_home") if isinstance(record, dict) else None if not isinstance(recorded_home, str) or not recorded_home.strip(): return False try: @@ -207,13 +205,11 @@ _EPOCH_MIN_PLAUSIBLE = 946684800.0 # 2000-01-01T00:00:00Z def normalize_updated_at(value: Any) -> Optional[str]: """Coerce a persisted ``updated_at`` value to an RFC3339 string or ``None``. - ``/api/status`` and ``/health/detailed`` promise ``string | null``, but the file - may hold legacy epoch floats, hand edits, or corruption. ``str``: iff fromisoformat - parses (trailing ``Z`` tolerated; naive -> UTC). ``int``/``float``: epoch seconds; - before 2000-01-01, > 1 day ahead, or non-finite -> None. ``bool`` / other -> None. + ``/api/status`` and ``/health/detailed`` promise ``string | null``, but the file may hold + legacy epoch floats, hand edits, or corruption. ``str``: iff fromisoformat parses (trailing + ``Z`` tolerated; naive -> UTC). ``int``/``float``: epoch seconds; before 2000-01-01, > 1 day + ahead, or non-finite -> None. ``bool`` / other -> None. """ - if isinstance(value, bool): - return None if isinstance(value, str): raw = value.strip() # Python < 3.11 fromisoformat rejects a trailing 'Z'; tolerate it. @@ -223,10 +219,8 @@ def normalize_updated_at(value: Any) -> Optional[str]: parsed = datetime.fromisoformat(raw) except ValueError: return None - if parsed.tzinfo is None: - parsed = parsed.replace(tzinfo=timezone.utc) - return parsed.isoformat() - if isinstance(value, (int, float)): + return (parsed if parsed.tzinfo else parsed.replace(tzinfo=timezone.utc)).isoformat() + if isinstance(value, (int, float)) and not isinstance(value, bool): seconds = float(value) now = datetime.now(timezone.utc).timestamp() if not math.isfinite(seconds) or seconds < _EPOCH_MIN_PLAUSIBLE or seconds > now + 86400: