feat: add TTL-aware drift detection and stabilize healing

This commit is contained in:
patrick committed 2026-01-25 21:32:02 -05:00
1 parent c23f2b7a47
commit 14c5ac758e
3 files changed
+103 -28

No files matched your search

+53 -3
View File
@@ -6,6 +6,7 @@ from auditor import FirewallAuditor
from reporter import LocalStateReporter
from dotenv import load_dotenv
import os
from datetime import datetime, timedelta
logging.basicConfig(
level=logging.INFO,
@@ -13,6 +14,48 @@ logging.basicConfig(
filename='/root/threat-engine-agent/logs/threat-agent.log'
)
################
#### Helpers ###
################
def should_heal(history_file, current_count, min_interval=900):
"""
Prevents heal flapping by suppressing repeated no-op heals.
"""
try:
if not os.path.exists(history_file):
return True
with open(history_file) as f:
lines = [l.strip() for l in f.readlines() if "HEAL:" in l]
if not lines:
return True
last = lines[-1]
ts_str, msg = last.split(" - ", 1)
last_time = datetime.fromisoformat(ts_str)
last_count = int(msg.split("desync of")[1].split("IPs")[0])
# If healed recently AND nothing changed, suppress
if datetime.now() - last_time < timedelta(seconds=min_interval):
if last_count == current_count:
logging.warning(
f"Heal suppressed (recent identical heal: {last_count} IPs)"
)
return False
return True
except Exception as e:
logging.error(f"Heal gate failed open: {e}")
return True
################
##### Main #####
################
def main():
"""
The Orchestrator: Links Audit findings to the State Reporter.
@@ -37,9 +80,16 @@ def main():
print(f"[+] {status_msg}") # This will show up in your terminal!
if not is_synced:
logging.warning(f"Desync detected ({count} IPs in kernel). Healing...")
auditor.heal()
reporter.log_heal_event(count) # Record the event in history
logging.warning(f"Desync detected ({count} IPs in kernel).")
history_file = reporter.state_file.replace(".json", ".history")
if should_heal(history_file, count):
logging.warning("Healing approved.")
auditor.heal()
reporter.log_heal_event(count)
else:
logging.warning("Healing skipped to prevent flapping.")
else:
logging.info(f"Audit Success: {count} IPs in sync.")