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(auth): make partial-DB cleanup airtight (review nits from #4934) (#4947)

LearningCircuit96e3b30f1c07
diff --git a/src/local_deep_research/database/encrypted_db.py b/src/local_deep_research/database/encrypted_db.py
index f1fc792442..e356e08721 100644
--- a/src/local_deep_research/database/encrypted_db.py
+++ b/src/local_deep_research/database/encrypted_db.py
@@ -117,14 +117,27 @@ def _remove_partial_user_db_files(db_path: Path) -> None:
Never raises — cleanup must not mask the original creation error.
"""
- db_path = Path(db_path)
- for path in (
- db_path,
- get_salt_file_path(db_path),
- db_path.with_name(db_path.name + "-wal"),
- db_path.with_name(db_path.name + "-shm"),
- db_path.with_name(db_path.name + "-journal"),
- ):
+ # Derive every artifact path up front, guarded: the "Never raises"
+ # contract has to hold even if a path helper is handed something odd
+ # (e.g. a nameless path, where ``with_name``/``with_suffix`` raise), so a
+ # derivation failure is logged and swallowed rather than propagating and
+ # masking the original creation error.
+ try:
+ db_path = Path(db_path)
+ artifacts = (
+ db_path,
+ get_salt_file_path(db_path),
+ db_path.with_name(db_path.name + "-wal"),
+ db_path.with_name(db_path.name + "-shm"),
+ db_path.with_name(db_path.name + "-journal"),
+ )
+ except Exception:
+ logger.warning(
+ f"Could not derive partial user DB artifact paths: {db_path!r}"
+ )
+ return
+
+ for path in artifacts:
try:
path.unlink(missing_ok=True)
except OSError:
diff --git a/tests/auth_tests/test_auth_routes.py b/tests/auth_tests/test_auth_routes.py
index f20b9f9111..6893751ac4 100644
--- a/tests/auth_tests/test_auth_routes.py
+++ b/tests/auth_tests/test_auth_routes.py
@@ -407,6 +407,11 @@ class TestAuthRoutes:
raise RuntimeError("simulated migration failure")
return real_initialize(*args, **kwargs)
+ # Patch on the source module (not encrypted_db): create_user_database
+ # does a *function-level* `from .initialize import initialize_database`
+ # that re-reads the module attribute at call time, so patching
+ # initialize_mod.initialize_database takes effect. (A module-level
+ # import in encrypted_db would need the patch applied there instead.)
monkeypatch.setattr(
initialize_mod, "initialize_database", _flaky_initialize
)
diff --git a/tests/database/test_encrypted_db_manager.py b/tests/database/test_encrypted_db_manager.py
index 77edd90c6b..51fec2e4b3 100644
--- a/tests/database/test_encrypted_db_manager.py
+++ b/tests/database/test_encrypted_db_manager.py
@@ -1546,3 +1546,27 @@ class TestRemovePartialUserDbFiles:
# No files created — must not raise.
_remove_partial_user_db_files(tmp_path / "ldr_user_absent.db")
+
+ def test_never_raises_when_paths_cannot_be_derived(self):
+ """The 'Never raises' contract holds even when a sidecar path cannot
+ be derived — the failure is logged and swallowed, not propagated, so
+ it can never mask the original creation error it's cleaning up after.
+ """
+ from pathlib import Path
+ from unittest.mock import patch
+
+ from local_deep_research.database.encrypted_db import (
+ _remove_partial_user_db_files,
+ )
+
+ # Path("/") has an empty name, so get_salt_file_path's with_suffix()
+ # raises ValueError during path derivation. That must be swallowed
+ # (the contract) AND the function must return before the unlink loop,
+ # so it never attempts to unlink "/" or any real path. Patch unlink and
+ # assert it's never called to *prove* the early-return — a plain
+ # no-raise check couldn't (a regression reaching the loop would call
+ # unlink("/") → IsADirectoryError, which the inner except OSError would
+ # silently swallow, and the test would still pass).
+ with patch("pathlib.Path.unlink") as mock_unlink:
+ _remove_partial_user_db_files(Path("/")) # must not raise
+ mock_unlink.assert_not_called()