From 4e8a4d6717ef12fc89842204249cc1b1f07fa951 Mon Sep 17 00:00:00 2001 From: patrick Date: Mon, 19 Oct 2026 18:04:24 -0400 Subject: [PATCH] feat(reporter): count heals the gate suppresses A drift whose fingerprint matches a heal from the last heal_min_interval seconds was counted as a drift event but never as a heal attempt, so the 24h counters showed a drift with no heal and no failure. Decide on the heal before recording the check and report heals_suppressed_24h and heals_suppressed_total, so every drift event is either a heal attempt or a suppressed heal. The suppression warning now names the earlier heal that did not hold. Co-Authored-By: Claude Opus 5.5 --- README.md | 5 +++++ healing.py | 4 +++- main.py | 16 +++++++++------- reporter.py | 18 ++++++++++++++++++ tests/test_reporter.py | 18 +++++++++++++++--- 5 files changed, 50 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a04ce3f..57fc0e0 100644 --- a/README.md +++ b/README.md @@ -168,8 +168,13 @@ Exposes metrics such as: - `heals_total` - `heal_failures_total` - `drift_events_total` +- `heals_suppressed_total` - rolling 24h counters for drift and healing activity +Every drift event leads to either a heal attempt or a suppressed heal (`heals_suppressed_24h`): the +heal gate skips a drift whose fingerprint matches a heal from the last `heal_min_interval` seconds, +which means that earlier heal did not hold. + ### `main.py` Orchestrates the full reconcile loop. diff --git a/healing.py b/healing.py index c994d90..65815a3 100644 --- a/healing.py +++ b/healing.py @@ -77,7 +77,9 @@ def should_heal(history_file, drift_fingerprint, min_interval=900): 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)." + "Heal suppressed (recent identical drift fingerprint): " + f"the heal at {last_time.isoformat()} did not hold " + f"(fingerprint={drift_fingerprint})." ) return False diff --git a/main.py b/main.py index 23cd790..255325e 100644 --- a/main.py +++ b/main.py @@ -97,6 +97,13 @@ def main(): ) if stop_event.is_set(): break + # Decide on the heal before recording the check, so a drift the gate + # suppresses is counted as such rather than silently lacking a heal. + heal_approved = is_synced or should_heal( + reporter.get_history_file(), + drift.get("fingerprint", ""), + min_interval=settings.heal_min_interval, + ) reporter.update_status( is_synced, count, @@ -106,6 +113,7 @@ def main(): last_drift_fingerprint=drift.get("fingerprint", ""), checks_increment=1, drift_events_increment=0 if is_synced else 1, + heal_suppressed=not heal_approved, ) if is_synced: @@ -125,13 +133,7 @@ def main(): f"extra={drift['extra_count']}, metadata={drift['metadata_drift_count']})." ) - history_file = reporter.get_history_file() - - if should_heal( - history_file, - drift.get("fingerprint", ""), - min_interval=settings.heal_min_interval, - ): + if heal_approved: logging.warning("Healing approved.") heal_result, post_drift = execute_heal_and_verify( auditor, diff --git a/reporter.py b/reporter.py index 7833243..f80b5d3 100644 --- a/reporter.py +++ b/reporter.py @@ -54,6 +54,7 @@ class LocalStateReporter: "drift_heal_attempts": max(0, int(metrics.get("drift_heal_attempts", 0))), "heals": max(0, int(metrics.get("heals", 0))), "heal_failures": max(0, int(metrics.get("heal_failures", 0))), + "heals_suppressed": max(0, int(metrics.get("heals_suppressed", 0))), } return normalized @@ -63,6 +64,7 @@ class LocalStateReporter: now_epoch, drift_events_increment, heal_result, + heal_suppressed=False, ): current_hour = int(now_epoch // 3600) min_hour = current_hour - 23 @@ -83,6 +85,7 @@ class LocalStateReporter: "drift_heal_attempts": 0, "heals": 0, "heal_failures": 0, + "heals_suppressed": 0, }, ) @@ -92,11 +95,14 @@ class LocalStateReporter: bucket["heals"] += 1 if heal_result != "success": bucket["heal_failures"] += 1 + if heal_suppressed: + bucket["heals_suppressed"] += 1 drift_events_24h = sum(v["drift_events"] for v in buckets.values()) drift_heal_attempts_24h = sum(v["drift_heal_attempts"] for v in buckets.values()) heals_24h = sum(v["heals"] for v in buckets.values()) heal_failures_24h = sum(v["heal_failures"] for v in buckets.values()) + heals_suppressed_24h = sum(v["heals_suppressed"] for v in buckets.values()) return ( buckets, @@ -104,6 +110,7 @@ class LocalStateReporter: drift_heal_attempts_24h, heals_24h, heal_failures_24h, + heals_suppressed_24h, ) def update_status( @@ -120,9 +127,13 @@ class LocalStateReporter: checks_increment=0, drift_events_increment=0, heal_result=None, + heal_suppressed=False, ): """ Writes the current health snapshot to a JSON file. + + heal_suppressed marks a drift the heal gate skipped because the same drift was + healed moments ago, so every drift event is either a heal attempt or a suppressed heal. """ previous = self._load_previous_state() effective_heal_result = ( @@ -159,6 +170,9 @@ class LocalStateReporter: drift_heal_attempts_total += 1 if heal_result != "success": heal_failures_total += 1 + heals_suppressed_total = max(0, self._get_int(previous, "heals_suppressed_total", 0)) + if heal_suppressed: + heals_suppressed_total += 1 ( rolling_24h_buckets, @@ -166,11 +180,13 @@ class LocalStateReporter: drift_heal_attempts_24h, heals_24h, heal_failures_24h, + heals_suppressed_24h, ) = self._roll_24h_counters( previous=previous, now_epoch=now_epoch, drift_events_increment=drift_events_increment, heal_result=heal_result, + heal_suppressed=heal_suppressed, ) uptime_seconds = max(0, int(now_epoch - self.started_at_epoch)) @@ -197,6 +213,8 @@ class LocalStateReporter: "heal_failures_24h": heal_failures_24h, "heals_total": heals_total, "heal_failures_total": heal_failures_total, + "heals_suppressed_24h": heals_suppressed_24h, + "heals_suppressed_total": heals_suppressed_total, "_rolling_24h_buckets": rolling_24h_buckets, } diff --git a/tests/test_reporter.py b/tests/test_reporter.py index ce5b353..2fccbe4 100644 --- a/tests/test_reporter.py +++ b/tests/test_reporter.py @@ -97,16 +97,25 @@ class ReporterStateTests(unittest.TestCase): reporter = LocalStateReporter(state_file=state_file, node_name="n1") reporter.update_status(False, 5, checks_increment=1, drift_events_increment=1) - reporter.update_status(False, 5, checks_increment=1, drift_events_increment=1) - reporter.update_status(False, 5, heal_result="failed") + reporter.update_status(False, 5, heal_result="success") + reporter.update_status( + False, 5, checks_increment=1, drift_events_increment=1, heal_suppressed=True + ) with open(state_file, "r") as f: payload = json.load(f) self.assertEqual(2, payload["drift_events_total"]) self.assertEqual(1, payload["drift_heal_attempts_total"]) + self.assertEqual(1, payload["heals_suppressed_total"]) self.assertEqual(2, payload["drift_events_24h"]) self.assertEqual(1, payload["drift_heal_attempts_24h"]) + self.assertEqual(1, payload["heals_suppressed_24h"]) + # Every drift is accounted for: a heal attempt or a suppressed heal. + self.assertEqual( + payload["drift_events_24h"], + payload["drift_heal_attempts_24h"] + payload["heals_suppressed_24h"], + ) @patch("reporter.time.time") def test_update_status_rolls_24h_window(self, time_mock): @@ -117,7 +126,9 @@ class ReporterStateTests(unittest.TestCase): # Initial write at hour 1000. time_mock.return_value = 1000 * 3600 - reporter.update_status(False, 1, drift_events_increment=1, heal_result="failed") + reporter.update_status( + False, 1, drift_events_increment=1, heal_result="failed", heal_suppressed=True + ) # New write 25 hours later; previous hour should be pruned from 24h view. time_mock.return_value = (1000 + 25) * 3600 @@ -130,6 +141,7 @@ class ReporterStateTests(unittest.TestCase): self.assertEqual(0, payload["drift_heal_attempts_24h"]) self.assertEqual(0, payload["heals_24h"]) self.assertEqual(0, payload["heal_failures_24h"]) + self.assertEqual(0, payload["heals_suppressed_24h"]) def test_update_status_preserves_last_heal_timestamp_when_no_new_heal(self): with tempfile.TemporaryDirectory() as tmp_dir: