Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(security): reset verification streaks in update_checksum (#7019) (#7369)

Ziyang Guobecbdbe1ed78
diff --git a/changelog.d/7019.bugfix.md b/changelog.d/7019.bugfix.md
new file mode 100644
index 0000000000..2a46a976ca
--- /dev/null
+++ b/changelog.d/7019.bugfix.md
@@ -0,0 +1 @@
+`update_checksum` now resets `consecutive_failures` and `consecutive_successes` alongside the new baseline, the same parity fix #7014 added to `record_file`. Without the reset, a legitimately modified file keeps the streaks from the previous checksum and is charged (or credited) for verifications the fresh bytes never had. `total_verifications` / `last_verified_at` / `last_verification_passed` are history about the path and are left untouched. This is the part 1 fix for #7019 (the parity gap #7014's commit explicitly tracked in its pointer comment); the part 2 (unfenced in-flight `verify_file` interleave) and part 3 (legacy rows) gaps remain tracked on the issue.
diff --git a/src/local_deep_research/security/file_integrity/integrity_manager.py b/src/local_deep_research/security/file_integrity/integrity_manager.py
index 6f72421ee1..6be831802c 100644
--- a/src/local_deep_research/security/file_integrity/integrity_manager.py
+++ b/src/local_deep_research/security/file_integrity/integrity_manager.py
@@ -365,9 +365,12 @@ class FileIntegrityManager:
argument applies to the verification streaks: the pair left in
place would describe the checksum that has just been replaced.
``record_file`` owns that invariant and resets them (see the
- comment there); this method currently has no production callers,
- and the parity fix is tracked in #7019 rather than shipped
- untested here.
+ comment there); ``update_checksum`` does the same for parity
+ (#7019) and, like ``record_file``, stamps the record with the
+ verifier's algorithm so the stored checksum and its algorithm
+ stay a consistent pair. ``total_verifications`` /
+ ``last_verified_at`` / ``last_verification_passed`` are history
+ about the path and are left untouched.
Args:
file_path: Path to file
@@ -397,9 +400,23 @@ class FileIntegrityManager:
raise ValueError(f"No integrity record exists for: {file_path}")
record.checksum = checksum
+ # The checksum was just recomputed with this verifier, so the
+ # algorithm must be re-stamped alongside it — otherwise the
+ # new digest is stored under the OLD baseline's algorithm name
+ # and verify_file would verify one algorithm with another.
+ # Mirrors record_file's existing-record branch.
+ record.algorithm = verifier.get_algorithm()
record.file_size = file_stat.st_size
record.file_mtime = file_stat.st_mtime
record.updated_at = datetime.now(UTC)
+ # The pair is mutually exclusive in _update_stats (every
+ # pass zeroes consecutive_failures and vice versa), so
+ # resetting only one would leave the other crediting the
+ # new baseline with verifications it never had. Mirrors
+ # record_file's pair reset (#7019, closes the parity gap
+ # #7014 left in its own commit's pointer comment).
+ record.consecutive_failures = 0
+ record.consecutive_successes = 0
session.commit()
logger.info(f"[FILE_INTEGRITY] Updated checksum for: {file_path}")
diff --git a/tests/security/file_integrity/test_integrity_manager.py b/tests/security/file_integrity/test_integrity_manager.py
index 4bbc616b81..3e93d09427 100644
--- a/tests/security/file_integrity/test_integrity_manager.py
+++ b/tests/security/file_integrity/test_integrity_manager.py
@@ -806,6 +806,89 @@ class TestRecordFile:
mgr.record_file(f)
+class TestUpdateChecksum:
+ """Tests for update_checksum — the legitimate-modification re-baseline
+ path. ``update_checksum`` mirrors ``record_file``'s re-baseline
+ semantics, so it must apply the same streak reset (#7019)."""
+
+ def test_resets_stale_verification_streaks(self, tmp_path):
+ """Re-baselining via update_checksum clears the streaks left by
+ the previous baseline, the parity shape #7014 added to
+ record_file.
+
+ update_checksum installs the file's CURRENT checksum as the new
+ baseline, so the pass/fail streaks accumulated against the old
+ checksum describe a file that no longer exists. Kept, they are
+ charged to the new bytes. The same argument #7014 applied to
+ record_file applies here — the only difference is the entry
+ point (legitimate modification vs initial recording) — so the
+ pair must be reset; the historical columns keep describing the
+ path and must survive.
+ """
+ from local_deep_research.security.file_integrity.integrity_manager import (
+ FileIntegrityManager,
+ )
+ from local_deep_research.security.file_integrity.base_verifier import (
+ BaseFileVerifier,
+ FileType,
+ )
+
+ class StubVerifier(BaseFileVerifier):
+ def should_verify(self, fp):
+ return True
+
+ def get_file_type(self):
+ return FileType.PDF
+
+ def allows_modifications(self):
+ return False
+
+ f = tmp_path / "legitimate-mod.bin"
+ f.write_text("modified content")
+
+ with patch(
+ "local_deep_research.security.file_integrity.integrity_manager.get_user_db_session"
+ ) as mock_gs:
+ mock_ctx = MagicMock()
+ mock_gs.return_value.__enter__ = Mock(return_value=mock_ctx)
+ mock_gs.return_value.__exit__ = Mock(return_value=False)
+ mock_ctx.query.return_value.count.return_value = 0
+ mgr = FileIntegrityManager("user", "pass")
+
+ v = StubVerifier()
+ v.calculate_checksum = Mock(return_value="freshbaseline")
+ mgr.register_verifier(v)
+
+ # The pre-existing row was verified three times against
+ # the OLD baseline (and clean twice before that); both
+ # streaks are stale once the bytes are legitimately
+ # modified, while the historical columns keep describing
+ # the path.
+ existing = Mock()
+ existing.checksum = "oldbaseline"
+ existing.algorithm = "md5"
+ existing.consecutive_failures = 3
+ existing.consecutive_successes = 2
+ existing.total_verifications = 9
+ existing.last_verified_at = "sentinel-last-verified"
+ existing.last_verification_passed = False
+ mock_ctx.query.return_value.filter_by.return_value.first.return_value = existing
+
+ mgr.update_checksum(f)
+
+ assert existing.checksum == "freshbaseline"
+ # The digest was recomputed with the registered verifier, so
+ # the algorithm must be re-stamped to match (record_file
+ # parity): the old "md5" name must not survive under a
+ # sha256 digest.
+ assert existing.algorithm == "sha256"
+ assert existing.consecutive_failures == 0
+ assert existing.consecutive_successes == 0
+ assert existing.total_verifications == 9
+ assert existing.last_verified_at == "sentinel-last-verified"
+ assert existing.last_verification_passed is False
+
+
class TestLogFailure:
"""Tests for _log_failure (audit trail)."""