test(agent): add coverage for drift gating and post-heal verification; remove bare except

This commit is contained in:
patrick committed 2026-02-10 22:04:57 -05:00
1 parent 461512f20a
commit 5fc5b8c114
3 files changed
+185 -37

No files matched your search

+52 -37
View File
@@ -165,6 +165,49 @@ def should_heal(history_file, drift_fingerprint, min_interval=900):
return True
def execute_heal_and_verify(auditor, reporter, count, drift, heal_verify_delay):
"""
Executes heal + post-heal verification and persists reporter state.
"""
heal_ok = auditor.heal()
heal_result = "restart_failed"
post_drift = {
"reason": "not_verified",
"missing_count": 0,
"extra_count": 0,
"metadata_drift_count": 0,
"fingerprint": "",
}
if heal_ok:
logging.info(
f"Post-heal verification in {heal_verify_delay}s "
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)
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)}"
)
else:
reporter.update_status(False, count, last_error="Bouncer restart failed")
logging.error(
f"Heal restart failed; skipping verification "
f"(pre_fingerprint={drift.get('fingerprint', '')})."
)
return heal_result, post_drift
################
##### Main #####
################
@@ -249,41 +292,13 @@ def main():
min_interval=heal_min_interval,
):
logging.warning("Healing approved.")
heal_ok = auditor.heal()
heal_result = "restart_failed"
post_drift = {
"reason": "not_verified",
"missing_count": 0,
"extra_count": 0,
"metadata_drift_count": 0,
"fingerprint": "",
}
if heal_ok:
logging.info(
f"Post-heal verification in {heal_verify_delay}s "
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)
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)}"
)
else:
reporter.update_status(False, count, last_error="Bouncer restart failed")
logging.error(
f"Heal restart failed; skipping verification "
f"(pre_fingerprint={drift.get('fingerprint', '')})."
)
heal_result, post_drift = execute_heal_and_verify(
auditor,
reporter,
count,
drift,
heal_verify_delay,
)
reporter.log_heal_event(
count,
@@ -311,8 +326,8 @@ def main():
# Ensure the reporter notes the error state
try:
reporter.update_status(False, 0, last_error=str(e))
except:
pass
except Exception as reporter_error:
logging.error(f"Reporter update failed during exception handling: {reporter_error}")
time.sleep(60)
if __name__ == "__main__":