fix(healing): normalize history timestamps to UTC in heal flap gate

This commit is contained in:
patrick committed 2026-03-06 22:31:24 -05:00
1 parent 1013b05790
commit f5615faeae
2 files changed
+19 -3

No files matched your search

+9 -3
View File
@@ -1,7 +1,7 @@
import logging import logging
import os import os
import time 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): 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 return True
ts_str, msg = last.split(" - ", 1) 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 = "" last_fingerprint = ""
marker = "drift_fingerprint=" marker = "drift_fingerprint="
if marker in msg: if marker in msg:
@@ -68,7 +74,7 @@ def should_heal(history_file, drift_fingerprint, min_interval=900):
return True return True
# If healed recently AND nothing changed, suppress # 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: if last_fingerprint and last_fingerprint == drift_fingerprint:
logging.warning( logging.warning(
"Heal suppressed (recent identical drift fingerprint)." "Heal suppressed (recent identical drift fingerprint)."
+10
View File
@@ -74,6 +74,16 @@ class HealGateTests(unittest.TestCase):
self.assertFalse(should_heal(path, "tail123", min_interval=900)) 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): class PostHealVerificationTests(unittest.TestCase):
@patch("healing.time.sleep", return_value=None) @patch("healing.time.sleep", return_value=None)