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 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)."