diff --git a/healing.py b/healing.py index 418e23b..151fa3c 100644 --- a/healing.py +++ b/healing.py @@ -105,19 +105,34 @@ def execute_heal_and_verify(auditor, reporter, count, drift, heal_verify_delay): f"(pre_fingerprint={drift.get('fingerprint', '')})." ) time.sleep(heal_verify_delay) - post_is_synced, post_count, post_drift = auditor.reconcile() - heal_result = "success" if post_is_synced else "failed" - verify_error = None if post_is_synced else "Post-heal verification failed" - reporter.update_status(post_is_synced, post_count, last_error=verify_error) + try: + post_is_synced, post_count, post_drift = auditor.reconcile() + heal_result = "success" if post_is_synced else "failed" + verify_error = None if post_is_synced else "Post-heal verification failed" + reporter.update_status(post_is_synced, post_count, last_error=verify_error) - logging.warning( - "Post-heal verification result " - f"result={heal_result} pre={drift.get('fingerprint', '')} " - f"post={post_drift.get('fingerprint', '')} " - f"missing={post_drift.get('missing_count', 0)} " - f"extra={post_drift.get('extra_count', 0)} " - f"metadata={post_drift.get('metadata_drift_count', 0)}" - ) + logging.warning( + "Post-heal verification result " + f"result={heal_result} pre={drift.get('fingerprint', '')} " + f"post={post_drift.get('fingerprint', '')} " + f"missing={post_drift.get('missing_count', 0)} " + f"extra={post_drift.get('extra_count', 0)} " + f"metadata={post_drift.get('metadata_drift_count', 0)}" + ) + except Exception as e: + heal_result = "verify_error" + reporter.update_status(False, count, last_error=f"Post-heal verify error: {e}") + post_drift = { + "reason": "verify_error", + "missing_count": 0, + "extra_count": 0, + "metadata_drift_count": 0, + "fingerprint": "", + } + logging.error( + "Post-heal verification crashed " + f"(pre_fingerprint={drift.get('fingerprint', '')}): {e}" + ) else: reporter.update_status(False, count, last_error="Bouncer restart failed") logging.error( diff --git a/tests/test_agent.py b/tests/test_agent.py index 80741f5..bfbfb88 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -15,9 +15,10 @@ class FakeReporter: class FakeAuditor: - def __init__(self, heal_ok=True, reconcile_result=None): + def __init__(self, heal_ok=True, reconcile_result=None, reconcile_exception=None): self.heal_ok = heal_ok self.reconcile_result = reconcile_result + self.reconcile_exception = reconcile_exception self.last_heal_drift = None def heal(self): @@ -28,6 +29,8 @@ class FakeAuditor: return self.heal_ok def reconcile(self): + if self.reconcile_exception: + raise self.reconcile_exception return self.reconcile_result @@ -108,6 +111,26 @@ class PostHealVerificationTests(unittest.TestCase): self.assertEqual("not_verified", returned_post_drift["reason"]) self.assertEqual([(False, 7, "Bouncer restart failed")], reporter.update_calls) + @patch("healing.time.sleep", return_value=None) + def test_execute_heal_and_verify_verify_exception(self, _sleep): + reporter = FakeReporter() + auditor = FakeAuditor(heal_ok=True, reconcile_exception=RuntimeError("boom")) + + heal_result, returned_post_drift = execute_heal_and_verify( + auditor, + reporter, + count=7, + drift={"fingerprint": "pre123"}, + heal_verify_delay=0, + ) + + self.assertEqual("verify_error", heal_result) + self.assertEqual("verify_error", returned_post_drift["reason"]) + self.assertEqual(1, len(reporter.update_calls)) + self.assertFalse(reporter.update_calls[0][0]) + self.assertEqual(7, reporter.update_calls[0][1]) + self.assertIn("Post-heal verify error: boom", reporter.update_calls[0][2]) + if __name__ == "__main__": unittest.main()