diff --git a/src/messenger/reconcile.py b/src/messenger/reconcile.py index 3e07569..e2457b3 100644 --- a/src/messenger/reconcile.py +++ b/src/messenger/reconcile.py @@ -18,6 +18,7 @@ What it finds and what --apply does (nothing else is ever changed): size_mismatch row 'ready', object size != size_bytes quarantine the row checksum_mismatch row 'ready', SHA-256 differs (--verify-checksums) quarantine the row restored_object row quarantined, object present and correct again remove the quarantine + (a checksum quarantine only with --verify-checksums) fresh_* the same, but younger than --grace nothing (in-flight upload) Safety: @@ -204,6 +205,10 @@ class Reconciler: self.add(Finding(problem, row.key, row.id, used=row.used)) else: self.stats.consistent += 1 # quarantined and still broken + elif row.lost_reason == "checksum_mismatch" and not self.verify: + # Same size, but this pass does not read the bytes: it cannot prove the object is the + # original again, so a checksum quarantine is lifted only by --verify-checksums. + self.stats.consistent += 1 elif row.lost_reason is not None: self.add(Finding("restored_object", row.key, row.id, used=row.used)) else: @@ -291,11 +296,13 @@ class Reconciler: async def unquarantine(self, f: Finding) -> str: async with self.conn.transaction(): cur = await self.conn.execute( - "SELECT size_bytes, sha256 FROM attachments WHERE id = %s AND lost_at IS NOT NULL FOR UPDATE", + "SELECT size_bytes, sha256, lost_reason FROM attachments WHERE id = %s AND lost_at IS NOT NULL FOR UPDATE", (f.attachment_id,)) row = await cur.fetchone() if row is None: return "skipped:row_changed" + if row[2] == "checksum_mismatch" and not self.verify: + return "skipped:needs_verify_checksums" if await self.current_problem(f.key, row[0], bytes(row[1])) is not None: return "skipped:object_still_bad" await self.conn.execute("UPDATE attachments SET lost_at = NULL, lost_reason = NULL WHERE id = %s", diff --git a/student-tests/test_reconcile.py b/student-tests/test_reconcile.py index a0a2de8..d32a750 100644 --- a/student-tests/test_reconcile.py +++ b/student-tests/test_reconcile.py @@ -155,6 +155,14 @@ def test_checksum_mode_finds_same_size_corruption(api): assert report["cost"]["s3_get_bytes"] >= len(SAMPLE) assert findings_by_key(reconcile("--apply", "--verify-checksums", *GRACE))[k]["result"] == "applied" assert attachment_row(att["id"])["lost_reason"] == "checksum_mismatch" + # A later pass WITHOUT checksums sees "same size" but cannot prove the bytes: the quarantine stays. + assert k not in findings_by_key(reconcile("--apply", *GRACE)) + assert attachment_row(att["id"])["lost_reason"] == "checksum_mismatch" + # Restoring the original and verifying lifts it. + s3_put(k, SAMPLE) + lifted = findings_by_key(reconcile("--apply", "--verify-checksums", *GRACE))[k] + assert (lifted["kind"], lifted["result"]) == ("restored_object", "applied") + assert attachment_row(att["id"])["lost_reason"] is None def test_safety_valve_refuses_mass_deletion():