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 <noreply@anthropic.com>
This commit is contained in:
1 parent
02ca338eb7
commit
4e8a4d6717
5 files changed
+50
-11
No files matched your search
@@ -168,8 +168,13 @@ Exposes metrics such as:
|
|||||||
- `heals_total`
|
- `heals_total`
|
||||||
- `heal_failures_total`
|
- `heal_failures_total`
|
||||||
- `drift_events_total`
|
- `drift_events_total`
|
||||||
|
- `heals_suppressed_total`
|
||||||
- rolling 24h counters for drift and healing activity
|
- 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`
|
### `main.py`
|
||||||
|
|
||||||
Orchestrates the full reconcile loop.
|
Orchestrates the full reconcile loop.
|
||||||
|
|||||||
+3
-1
@@ -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 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): "
|
||||||
|
f"the heal at {last_time.isoformat()} did not hold "
|
||||||
|
f"(fingerprint={drift_fingerprint})."
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|||||||
@@ -97,6 +97,13 @@ def main():
|
|||||||
)
|
)
|
||||||
if stop_event.is_set():
|
if stop_event.is_set():
|
||||||
break
|
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(
|
reporter.update_status(
|
||||||
is_synced,
|
is_synced,
|
||||||
count,
|
count,
|
||||||
@@ -106,6 +113,7 @@ def main():
|
|||||||
last_drift_fingerprint=drift.get("fingerprint", ""),
|
last_drift_fingerprint=drift.get("fingerprint", ""),
|
||||||
checks_increment=1,
|
checks_increment=1,
|
||||||
drift_events_increment=0 if is_synced else 1,
|
drift_events_increment=0 if is_synced else 1,
|
||||||
|
heal_suppressed=not heal_approved,
|
||||||
)
|
)
|
||||||
|
|
||||||
if is_synced:
|
if is_synced:
|
||||||
@@ -125,13 +133,7 @@ def main():
|
|||||||
f"extra={drift['extra_count']}, metadata={drift['metadata_drift_count']})."
|
f"extra={drift['extra_count']}, metadata={drift['metadata_drift_count']})."
|
||||||
)
|
)
|
||||||
|
|
||||||
history_file = reporter.get_history_file()
|
if heal_approved:
|
||||||
|
|
||||||
if should_heal(
|
|
||||||
history_file,
|
|
||||||
drift.get("fingerprint", ""),
|
|
||||||
min_interval=settings.heal_min_interval,
|
|
||||||
):
|
|
||||||
logging.warning("Healing approved.")
|
logging.warning("Healing approved.")
|
||||||
heal_result, post_drift = execute_heal_and_verify(
|
heal_result, post_drift = execute_heal_and_verify(
|
||||||
auditor,
|
auditor,
|
||||||
|
|||||||
+18
@@ -54,6 +54,7 @@ class LocalStateReporter:
|
|||||||
"drift_heal_attempts": max(0, int(metrics.get("drift_heal_attempts", 0))),
|
"drift_heal_attempts": max(0, int(metrics.get("drift_heal_attempts", 0))),
|
||||||
"heals": max(0, int(metrics.get("heals", 0))),
|
"heals": max(0, int(metrics.get("heals", 0))),
|
||||||
"heal_failures": max(0, int(metrics.get("heal_failures", 0))),
|
"heal_failures": max(0, int(metrics.get("heal_failures", 0))),
|
||||||
|
"heals_suppressed": max(0, int(metrics.get("heals_suppressed", 0))),
|
||||||
}
|
}
|
||||||
return normalized
|
return normalized
|
||||||
|
|
||||||
@@ -63,6 +64,7 @@ class LocalStateReporter:
|
|||||||
now_epoch,
|
now_epoch,
|
||||||
drift_events_increment,
|
drift_events_increment,
|
||||||
heal_result,
|
heal_result,
|
||||||
|
heal_suppressed=False,
|
||||||
):
|
):
|
||||||
current_hour = int(now_epoch // 3600)
|
current_hour = int(now_epoch // 3600)
|
||||||
min_hour = current_hour - 23
|
min_hour = current_hour - 23
|
||||||
@@ -83,6 +85,7 @@ class LocalStateReporter:
|
|||||||
"drift_heal_attempts": 0,
|
"drift_heal_attempts": 0,
|
||||||
"heals": 0,
|
"heals": 0,
|
||||||
"heal_failures": 0,
|
"heal_failures": 0,
|
||||||
|
"heals_suppressed": 0,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -92,11 +95,14 @@ class LocalStateReporter:
|
|||||||
bucket["heals"] += 1
|
bucket["heals"] += 1
|
||||||
if heal_result != "success":
|
if heal_result != "success":
|
||||||
bucket["heal_failures"] += 1
|
bucket["heal_failures"] += 1
|
||||||
|
if heal_suppressed:
|
||||||
|
bucket["heals_suppressed"] += 1
|
||||||
|
|
||||||
drift_events_24h = sum(v["drift_events"] for v in buckets.values())
|
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())
|
drift_heal_attempts_24h = sum(v["drift_heal_attempts"] for v in buckets.values())
|
||||||
heals_24h = sum(v["heals"] 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())
|
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 (
|
return (
|
||||||
buckets,
|
buckets,
|
||||||
@@ -104,6 +110,7 @@ class LocalStateReporter:
|
|||||||
drift_heal_attempts_24h,
|
drift_heal_attempts_24h,
|
||||||
heals_24h,
|
heals_24h,
|
||||||
heal_failures_24h,
|
heal_failures_24h,
|
||||||
|
heals_suppressed_24h,
|
||||||
)
|
)
|
||||||
|
|
||||||
def update_status(
|
def update_status(
|
||||||
@@ -120,9 +127,13 @@ class LocalStateReporter:
|
|||||||
checks_increment=0,
|
checks_increment=0,
|
||||||
drift_events_increment=0,
|
drift_events_increment=0,
|
||||||
heal_result=None,
|
heal_result=None,
|
||||||
|
heal_suppressed=False,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Writes the current health snapshot to a JSON file.
|
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()
|
previous = self._load_previous_state()
|
||||||
effective_heal_result = (
|
effective_heal_result = (
|
||||||
@@ -159,6 +170,9 @@ class LocalStateReporter:
|
|||||||
drift_heal_attempts_total += 1
|
drift_heal_attempts_total += 1
|
||||||
if heal_result != "success":
|
if heal_result != "success":
|
||||||
heal_failures_total += 1
|
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,
|
rolling_24h_buckets,
|
||||||
@@ -166,11 +180,13 @@ class LocalStateReporter:
|
|||||||
drift_heal_attempts_24h,
|
drift_heal_attempts_24h,
|
||||||
heals_24h,
|
heals_24h,
|
||||||
heal_failures_24h,
|
heal_failures_24h,
|
||||||
|
heals_suppressed_24h,
|
||||||
) = self._roll_24h_counters(
|
) = self._roll_24h_counters(
|
||||||
previous=previous,
|
previous=previous,
|
||||||
now_epoch=now_epoch,
|
now_epoch=now_epoch,
|
||||||
drift_events_increment=drift_events_increment,
|
drift_events_increment=drift_events_increment,
|
||||||
heal_result=heal_result,
|
heal_result=heal_result,
|
||||||
|
heal_suppressed=heal_suppressed,
|
||||||
)
|
)
|
||||||
|
|
||||||
uptime_seconds = max(0, int(now_epoch - self.started_at_epoch))
|
uptime_seconds = max(0, int(now_epoch - self.started_at_epoch))
|
||||||
@@ -197,6 +213,8 @@ class LocalStateReporter:
|
|||||||
"heal_failures_24h": heal_failures_24h,
|
"heal_failures_24h": heal_failures_24h,
|
||||||
"heals_total": heals_total,
|
"heals_total": heals_total,
|
||||||
"heal_failures_total": heal_failures_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,
|
"_rolling_24h_buckets": rolling_24h_buckets,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+15
-3
@@ -97,16 +97,25 @@ class ReporterStateTests(unittest.TestCase):
|
|||||||
reporter = LocalStateReporter(state_file=state_file, node_name="n1")
|
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, checks_increment=1, drift_events_increment=1)
|
reporter.update_status(False, 5, heal_result="success")
|
||||||
reporter.update_status(False, 5, heal_result="failed")
|
reporter.update_status(
|
||||||
|
False, 5, checks_increment=1, drift_events_increment=1, heal_suppressed=True
|
||||||
|
)
|
||||||
|
|
||||||
with open(state_file, "r") as f:
|
with open(state_file, "r") as f:
|
||||||
payload = json.load(f)
|
payload = json.load(f)
|
||||||
|
|
||||||
self.assertEqual(2, payload["drift_events_total"])
|
self.assertEqual(2, payload["drift_events_total"])
|
||||||
self.assertEqual(1, payload["drift_heal_attempts_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(2, payload["drift_events_24h"])
|
||||||
self.assertEqual(1, payload["drift_heal_attempts_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")
|
@patch("reporter.time.time")
|
||||||
def test_update_status_rolls_24h_window(self, time_mock):
|
def test_update_status_rolls_24h_window(self, time_mock):
|
||||||
@@ -117,7 +126,9 @@ class ReporterStateTests(unittest.TestCase):
|
|||||||
|
|
||||||
# Initial write at hour 1000.
|
# Initial write at hour 1000.
|
||||||
time_mock.return_value = 1000 * 3600
|
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.
|
# New write 25 hours later; previous hour should be pruned from 24h view.
|
||||||
time_mock.return_value = (1000 + 25) * 3600
|
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["drift_heal_attempts_24h"])
|
||||||
self.assertEqual(0, payload["heals_24h"])
|
self.assertEqual(0, payload["heals_24h"])
|
||||||
self.assertEqual(0, payload["heal_failures_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):
|
def test_update_status_preserves_last_heal_timestamp_when_no_new_heal(self):
|
||||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||||
|
|||||||
Reference in new issue
Block a user