From f5615faeaef612aec9540b796c87874a0f941c4f Mon Sep 17 00:00:00 2001 From: patrick Date: Fri, 6 Mar 2026 22:31:24 -0500 Subject: [PATCH] fix(healing): normalize history timestamps to UTC in heal flap gate --- healing.py | 12 +++++++++--- tests/test_agent.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/healing.py b/healing.py index 6131ca2..f7bcefd 100644 --- a/healing.py +++ b/healing.py @@ -1,7 +1,7 @@ import logging import os import time -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone def _read_last_heal_line(history_file, chunk_size=4096, max_scan_bytes=262144): @@ -58,7 +58,13 @@ def should_heal(history_file, drift_fingerprint, min_interval=900): return True ts_str, msg = last.split(" - ", 1) - last_time = datetime.fromisoformat(ts_str) + parsed_last_time = datetime.fromisoformat(ts_str) + if parsed_last_time.tzinfo is None: + # Legacy history lines were written as local naive timestamps. + local_tz = datetime.now().astimezone().tzinfo or timezone.utc + last_time = parsed_last_time.replace(tzinfo=local_tz).astimezone(timezone.utc) + else: + last_time = parsed_last_time.astimezone(timezone.utc) last_fingerprint = "" marker = "drift_fingerprint=" if marker in msg: @@ -68,7 +74,7 @@ def should_heal(history_file, drift_fingerprint, min_interval=900): return True # If healed recently AND nothing changed, suppress - if datetime.now() - last_time < timedelta(seconds=min_interval): + if datetime.now(timezone.utc) - last_time < timedelta(seconds=min_interval): if last_fingerprint and last_fingerprint == drift_fingerprint: logging.warning( "Heal suppressed (recent identical drift fingerprint)." diff --git a/tests/test_agent.py b/tests/test_agent.py index 5d5b4ca..46f379b 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -74,6 +74,16 @@ class HealGateTests(unittest.TestCase): self.assertFalse(should_heal(path, "tail123", min_interval=900)) + def test_should_heal_handles_timezone_aware_history_timestamp(self): + now_utc = datetime.now().astimezone().astimezone().isoformat() + with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp: + tmp.write( + f"{now_utc} - HEAL: drift_fingerprint=aware123 heal_result=success post_drift_fingerprint=\n" + ) + path = tmp.name + + self.assertFalse(should_heal(path, "aware123", min_interval=900)) + class PostHealVerificationTests(unittest.TestCase): @patch("healing.time.sleep", return_value=None)