From 6fb70de7ff097da3ce8854c0d4cdd77ef1e14b66 Mon Sep 17 00:00:00 2001 From: Alexey Bodanyuk <144894236+wfa22@users.noreply.github.com> Date: Sun, 25 Oct 2026 15:46:29 +0300 Subject: [PATCH] =?UTF-8?q?fix(reconcile):=20=D0=BA=D0=B0=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D1=82=D0=B8=D0=BD=20=D0=BF=D0=BE=20=D0=BA=D0=BE=D0=BD?= =?UTF-8?q?=D1=82=D1=80=D0=BE=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20=D1=81=D1=83?= =?UTF-8?q?=D0=BC=D0=BC=D0=B5=20=D1=81=D0=BD=D0=B8=D0=BC=D0=B0=D0=B5=D1=82?= =?UTF-8?q?=D1=81=D1=8F=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=85=D0=BE=D0=B4=D0=BE=D0=BC=20=D1=81=20--verify-?= =?UTF-8?q?checksums?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Найдено замером стоимости на чистом стенде: последующий --apply без проверки сумм видел совпадающий размер и снимал карантин checksum_mismatch как restored_object. Co-Authored-By: Claude Opus 5.5 --- src/messenger/reconcile.py | 9 ++++++++- student-tests/test_reconcile.py | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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():