~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(backup): keep unwritten purge warnings in the bounded status cache (#8092)
LearningCircuit60265ce23a84
diff --git a/changelog.d/8048.bugfix.md b/changelog.d/8048.bugfix.mdnew file mode 100644index 0000000000..a518618a1a--- /dev/null+++ b/changelog.d/8048.bugfix.md@@ -0,0 +1 @@+Backup status keeps a pending warning that old-password backups may remain when its in-memory cache is full: entries already written to the database, then other unwritten results, are dropped before an unwritten password-change purge result.diff --git a/src/local_deep_research/database/backup/backup_status.py b/src/local_deep_research/database/backup/backup_status.pyindex d8a7edd425..b0d5082164 100644--- a/src/local_deep_research/database/backup/backup_status.py+++ b/src/local_deep_research/database/backup/backup_status.py@@ -161,7 +161,23 @@ def _remember(key: tuple[str, str], attempt: dict) -> None:_recent[key] = attempt_recent.move_to_end(key)while len(_recent) > _RECENT_LIMIT:- _recent.popitem(last=False)+ # The oldest entry of the lowest retention rank; min() keeps the+ # first (oldest) of equal ranks.+ del _recent[min(_recent, key=lambda k: _retention_rank(_recent[k]))]+++def _retention_rank(entry: dict) -> int:+ """How strongly the bounded cache keeps ``entry``; higher is kept longer.++ An observation already written can be read back from the database, so+ it goes first. An unwritten purge state is kept longest: losing it would+ drop a warning that old-password backups may remain. Within a rank the+ oldest entry goes first, so when every entry holds an unwritten purge+ the newest ones are kept.+ """+ if not entry.get("purge_persisted", True):+ return 2+ return 1 if _is_pending(entry) else 0def _status_key(username: str) -> tuple[str, str]:@@ -326,7 +342,11 @@ def _is_pending(entry: dict | None) -> bool:def _persist(username: str, key, attempt: dict, direct_session=None) -> None:"""Write ``attempt``, or raise if no encrypted session is usable.- ``direct_session`` is tried first, then the open engine.+ ``direct_session`` is tried first, then the open engine. The open engine+ is avoided under the rekey sidecar only while ``direct_session`` works:+ if it fails while the caller holds the sidecar, the fallback can wait on+ that sidecar for up to its wait bound (it does not deadlock). A write+ that fails leaves the observation pending in the local cache."""saved = Noneif direct_session is not None:diff --git a/tests/database/backup/test_backup_status.py b/tests/database/backup/test_backup_status.pyindex a735fcf1db..2a39ae74d5 100644--- a/tests/database/backup/test_backup_status.py+++ b/tests/database/backup/test_backup_status.py@@ -1499,3 +1499,177 @@ def test_repair_lets_a_carried_purge_replace_an_unreadable_timestamp(assert _stored_purge(status_db) is Falsestatus._recent.clear()assert status.get_backup_status("alice")["last_attempt"]["success"] is True+++def _offline(monkeypatch):+ """Make every open-engine status write and read fail."""+ monkeypatch.setattr(+ status, "_encrypted_status_session", MagicMock(side_effect=OSError())+ )+++def _cached_users():+ return [username for _root, username in status._recent]+++def test_bounded_cache_evicts_written_entries_before_pending_ones(+ status_db, monkeypatch+):+ """#8048: an unwritten purge warning outlives newer entries that are+ already in the database or hold only an unwritten attempt."""+ monkeypatch.setattr(status, "_RECENT_LIMIT", 2)+ with monkeypatch.context() as offline:+ _offline(offline)+ status.record_backup_result("alice", True, purge_incomplete=True)+ status.record_backup_result("bob", True) # Written, so evicted first.+ _offline(monkeypatch)+ status.record_backup_result("carol", False)+ assert _cached_users() == ["alice", "carol"]+ status.record_backup_result("dave", False) # Unwritten attempts next.+ assert _cached_users() == ["alice", "dave"]+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "purge_incomplete"+ )+++def test_bounded_cache_keeps_the_newest_pending_purges(status_db, monkeypatch):+ """#8048: when every entry holds an unwritten purge, the bound still+ holds and the oldest of them goes first."""+ monkeypatch.setattr(status, "_RECENT_LIMIT", 2)+ _offline(monkeypatch)+ for username in ("alice", "bob", "carol"):+ status.record_backup_result(username, True, purge_incomplete=True)+ assert _cached_users() == ["bob", "carol"]+++_SECRET = "private-detail-8048"+++def _assert_logged_type_name_only(caplog, line, type_name):+ assert f"{line} ({type_name})" in caplog.text+ assert _SECRET not in caplog.text+++def test_direct_session_failure_logs_only_the_exception_type(+ status_db, loguru_caplog_full+):+ """#8048: the status log lines name the exception type, never its text,+ which can hold a path or a database value."""++ @contextmanager+ def direct():+ raise OSError(_SECRET)+ yield++ with loguru_caplog_full.at_level("DEBUG"):+ status.record_backup_result("alice", False, direct_session=direct)+ _assert_logged_type_name_only(+ loguru_caplog_full, "Direct backup status session unusable", "OSError"+ )+++def test_unsaved_status_logs_only_the_exception_type(+ status_db, monkeypatch, loguru_caplog_full+):+ monkeypatch.setattr(+ status,+ "_encrypted_status_session",+ MagicMock(side_effect=OSError(_SECRET)),+ )+ with loguru_caplog_full.at_level("DEBUG"):+ status.record_backup_result("alice", True, purge_incomplete=True)+ status.persist_pending_backup_status("alice")+ status.get_backup_status("alice")+ for line in (+ "Could not save encrypted backup status",+ "Pending backup status not yet saved",+ "Could not read encrypted backup status",+ ):+ _assert_logged_type_name_only(loguru_caplog_full, line, "OSError")+++def test_unformattable_status_logs_only_the_exception_type(+ status_db, monkeypatch, loguru_caplog_full+):+ monkeypatch.setattr(+ status, "_report", MagicMock(side_effect=KeyError(_SECRET))+ )+ with loguru_caplog_full.at_level("DEBUG"):+ status.get_backup_status("alice")+ _assert_logged_type_name_only(+ loguru_caplog_full, "Could not format backup status", "KeyError"+ )+++def test_row_repair_logs_only_the_exception_type(status_db, loguru_caplog_full):+ """The parse error of a corrupt row quotes the stored value."""+ status.record_backup_result("alice", True)+ _corrupt(status_db, f"completed_at = '{_SECRET}'")+ status._recent.clear()+ with loguru_caplog_full.at_level("DEBUG"):+ status.record_backup_result("alice", False)+ _assert_logged_type_name_only(+ loguru_caplog_full,+ "Repairing an unreadable backup status row",+ "ValueError",+ )++[email protected]("kind", ["symlink", "outside"])+def test_purge_containment_recheck_holds_without_is_purgeable(+ service, tmp_path, monkeypatch, kind+):+ """#8048: _is_purgeable already rejects a symlink or an entry outside+ the backup directory. With it stubbed to accept everything, the+ containment re-check in the purge loop must still leave both in place."""+ from local_deep_research.database.backup import backup_service++ outside = tmp_path / "outside"+ outside.mkdir()+ target = outside / "ldr_backup_20260101_000000.db"+ target.write_bytes(b"not-a-backup")+ if kind == "symlink":+ entry = service.backup_dir / "ldr_backup_20260101_000001.db"+ try:+ entry.symlink_to(target)+ except OSError:+ pytest.skip("symlinks are not available")+ else:+ entry = target+ real_listing = backup_service.list_backup_files++ def listing(directory, *, include_auxiliary=False):+ found = real_listing(directory, include_auxiliary=include_auxiliary)+ return [*found, target]++ monkeypatch.setattr(backup_service, "list_backup_files", listing)+ monkeypatch.setattr(service, "_is_purgeable", lambda path: True)++ assert service._purge_old_backup_files() is True+ assert entry.exists(follow_symlinks=False)+ assert target.read_bytes() == b"not-a-backup"+++def test_purge_removes_a_symlinked_salt_without_touching_its_target(+ service, tmp_path, monkeypatch+):+ """#8048: a salt companion that is a symlink is removed as a link; the+ file it points to is left alone."""+ old = service.backup_dir / "ldr_backup_20260101_000000.db"+ old.write_bytes(b"old-key-backup")+ target = tmp_path / "elsewhere.salt"+ target.write_bytes(b"keep")+ salt = service.backup_dir / "ldr_backup_20260101_000000.db.salt"+ try:+ salt.symlink_to(target)+ except OSError:+ pytest.skip("symlinks are not available")+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ assert service.purge_and_refresh().success is True+ assert not old.exists()+ assert not salt.exists(follow_symlinks=False)+ assert target.read_bytes() == b"keep"+ assert status.get_backup_status("alice")["last_attempt"]["success"] is True