~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
"""Real SQLCipher recovery with no access to the original data directory."""
import hashlib
import os
import shutil
from datetime import UTC, datetime, timedelta
from pathlib import Path
from unittest.mock import MagicMock, call, patch
import pytest
from local_deep_research.database import sqlcipher_compat, sqlcipher_utils
from local_deep_research.database.backup import backup_service
from local_deep_research.database.backup.backup_service import BackupService
from local_deep_research.database.sqlcipher_utils import (
create_database_salt,
create_sqlcipher_connection,
get_salt_file_path,
set_sqlcipher_rekey,
)
pytest.importorskip("sqlcipher3")
PASSWORD = "recovery-test-password"
NEW_PASSWORD = "recovery-test-new-password"
@pytest.fixture
def source(tmp_path, monkeypatch):
monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path / "live"))
service = BackupService("family-member", PASSWORD, max_backups=3)
return service
def count_key_derivations():
"""Count PBKDF2 key derivations; every full verification needs one."""
return patch.object(
sqlcipher_utils,
"_get_key_from_password",
wraps=sqlcipher_utils._get_key_from_password,
)
def shift_mtime(path, seconds):
"""Move a file's modification time, which also changes its identity."""
status = path.stat()
os.utime(
path,
ns=(status.st_atime_ns, status.st_mtime_ns + seconds * 1_000_000_000),
)
def create_source(service, *, legacy=False):
if not legacy:
create_database_salt(service.db_path)
conn = create_sqlcipher_connection(
service.db_path, PASSWORD, creation_mode=True
)
conn.execute("PRAGMA journal_mode=WAL")
conn.execute("PRAGMA wal_autocheckpoint=0")
conn.execute("CREATE TABLE research (id INTEGER PRIMARY KEY, report TEXT)")
conn.execute("INSERT INTO research VALUES (1, 'family research report')")
conn.commit()
return conn
@pytest.mark.parametrize(
"legacy", [False, True], ids=["per-user-salt", "legacy"]
)
def test_recovery_uses_only_archived_backup_and_password(
source, tmp_path, legacy
):
"""Export committed WAL data, destroy the source, restore and write again."""
conn = create_source(source, legacy=legacy)
try:
assert Path(str(source.db_path) + "-wal").stat().st_size > 0
result = source.create_backup()
assert result.success, result.error
finally:
conn.close()
archive = tmp_path / "archive"
shutil.copytree(source.backup_dir, archive)
backup = archive / result.backup_path.name
salt = get_salt_file_path(backup)
assert salt.exists() is not legacy
if not legacy:
assert (
salt.read_bytes() == get_salt_file_path(source.db_path).read_bytes()
)
assert salt.stat().st_mode & 0o777 == 0o600
assert backup.read_bytes()[:16] != b"SQLite format 3\x00"
shutil.rmtree(tmp_path / "live")
assert not source.db_path.parent.exists()
assert source._verify_backup(backup)
# Follow the documented offline restore procedure into an empty directory.
destination = tmp_path / "restored" / source.db_filename
destination.parent.mkdir()
shutil.copy2(backup, destination)
if salt.exists():
shutil.copy2(salt, get_salt_file_path(destination))
restored = create_sqlcipher_connection(destination, PASSWORD)
try:
assert restored.execute("SELECT * FROM research").fetchall() == [
(1, "family research report")
]
assert restored.execute("PRAGMA quick_check").fetchone() == ("ok",)
restored.execute("INSERT INTO research VALUES (2, 'new report')")
restored.commit()
finally:
restored.close()
reopened = create_sqlcipher_connection(destination, PASSWORD)
try:
assert reopened.execute("SELECT COUNT(*) FROM research").fetchone() == (
2,
)
finally:
reopened.close()
with pytest.raises(Exception):
create_sqlcipher_connection(destination, "wrong-password")
@pytest.mark.parametrize(
"damage", ["missing", "mismatched", "truncated", "symlink"]
)
def test_backup_verification_never_uses_source_salt(source, tmp_path, damage):
conn = create_source(source)
conn.close()
result = source.create_backup()
assert result.success, result.error
backup = result.backup_path
salt = get_salt_file_path(backup)
original = backup.read_bytes()
if damage == "missing":
salt.unlink()
elif damage == "mismatched":
salt.write_bytes(b"x" * 32)
elif damage == "truncated":
salt.write_bytes(b"x")
else:
external = tmp_path / "external.salt"
external.write_bytes(salt.read_bytes())
salt.unlink()
salt.symlink_to(external)
assert get_salt_file_path(source.db_path).exists()
assert not source._verify_backup(backup)
assert backup.read_bytes() == original
def test_daily_limit_replaces_old_backup_without_its_salt(source):
conn = create_source(source)
conn.close()
first = source.create_backup()
assert first.success, first.error
get_salt_file_path(first.backup_path).unlink()
second = source.create_backup()
assert second.success, second.error
assert second.backup_path != first.backup_path
assert source._verify_backup(second.backup_path)
# A complete backup still enforces the once-per-day rule.
assert source.create_backup().backup_path == second.backup_path
def test_daily_limit_reuses_a_verified_backup_without_verifying_again(
source, monkeypatch
):
conn = create_source(source)
conn.close()
first = source.create_backup()
assert first.success, first.error
with patch.object(
source, "_verify_backup", wraps=source._verify_backup
) as verify:
# Creation ran the full verification; unchanged files need no repeat.
assert source.create_backup().backup_path == first.backup_path
assert verify.call_count == 0
# A restarted process has no record, so it verifies the backup once.
monkeypatch.setattr(backup_service, "_backup_verifications", {})
assert source.create_backup().backup_path == first.backup_path
assert source.create_backup().backup_path == first.backup_path
verify.assert_called_once_with(first.backup_path)
@pytest.mark.parametrize("change", ["replaced", "touched"])
@pytest.mark.parametrize("changed", ["database", "salt"])
def test_daily_limit_verifies_a_changed_backup_again(source, changed, change):
conn = create_source(source)
conn.close()
first = source.create_backup()
assert first.success, first.error
target = first.backup_path
if changed == "salt":
target = get_salt_file_path(target)
status = target.stat()
if change == "replaced":
# Same bytes, size and timestamps in a new file, as copying the
# backup back from external storage would leave it.
copy = target.with_name(target.name + ".copy")
shutil.copy2(target, copy)
os.replace(copy, target)
assert target.stat().st_ino != status.st_ino
else:
os.utime(
target,
ns=(status.st_atime_ns, status.st_mtime_ns + 1_000_000_000),
)
with patch.object(
source, "_verify_backup", wraps=source._verify_backup
) as verify:
assert source.create_backup().backup_path == first.backup_path
assert source.create_backup().backup_path == first.backup_path
# Verified once after the change, then reused while unchanged again.
verify.assert_called_once_with(first.backup_path)
@pytest.mark.parametrize("damage", ["salt-missing", "salt-mismatched"])
def test_later_sign_ins_derive_no_key_beside_a_failing_backup(source, damage):
"""A failing same-day backup kept by retention costs later sign-ins nothing.
On upgrade day, for example, an earlier backup made without its salt
fails, a replacement is created, and retention keeps both.
"""
conn = create_source(source)
conn.close()
earlier = source.create_backup()
assert earlier.success, earlier.error
shift_mtime(earlier.backup_path, -60)
salt = get_salt_file_path(earlier.backup_path)
if damage == "salt-missing":
salt.unlink()
else:
salt.write_bytes(b"x" * 32)
replacement = source.create_backup()
assert replacement.success, replacement.error
assert replacement.backup_path != earlier.backup_path
assert earlier.backup_path.exists()
with count_key_derivations() as derive:
for _ in range(2):
later = source.create_backup()
assert later.backup_path == replacement.backup_path
assert derive.call_count == 0
def test_unremembered_backups_are_verified_newest_first_until_one_passes(
source,
):
conn = create_source(source)
conn.close()
oldest, middle, newest = [
source.create_backup(force=True) for _ in range(3)
]
assert all(r.success for r in (oldest, middle, newest))
get_salt_file_path(newest.backup_path).write_bytes(b"x" * 32)
# New timestamps, in creation order, leave no result remembered.
for minutes, result in enumerate((newest, middle, oldest), start=1):
shift_mtime(result.backup_path, -60 * minutes)
with patch.object(
source, "_verify_backup", wraps=source._verify_backup
) as verify:
assert source.create_backup().backup_path == middle.backup_path
# The oldest is never verified: the middle backup already passed.
assert verify.call_args_list == [
call(newest.backup_path),
call(middle.backup_path),
]
# Both results are remembered, so the next sign-in verifies nothing.
assert source.create_backup().backup_path == middle.backup_path
# A remembered pass also comes before a newer backup that has no
# remembered result.
shift_mtime(newest.backup_path, 1)
assert source.create_backup().backup_path == middle.backup_path
assert verify.call_count == 2
@pytest.mark.parametrize("changed", ["database", "salt"])
def test_a_known_failing_backup_is_checked_again_only_once_changed(
source, changed
):
conn = create_source(source)
conn.close()
first = source.create_backup()
assert first.success, first.error
salt = get_salt_file_path(first.backup_path)
salt.write_bytes(b"x" * 32)
target = first.backup_path if changed == "database" else salt
# No room for a replacement, so the failing backup stays the only one.
with (
patch("shutil.disk_usage", return_value=MagicMock(free=0)),
patch.object(
source, "_verify_backup", wraps=source._verify_backup
) as verify,
count_key_derivations() as derive,
):
for _ in range(2):
assert not source.create_backup().success
# Checked once; the unchanged failure is remembered.
assert (verify.call_count, derive.call_count) == (1, 1)
shift_mtime(target, 1)
assert not source.create_backup().success
assert (verify.call_count, derive.call_count) == (2, 2)
def test_backup_without_salt_of_a_salted_database_derives_no_key(
source, loguru_caplog
):
conn = create_source(source)
conn.close()
result = source.create_backup()
assert result.success, result.error
get_salt_file_path(result.backup_path).unlink()
with (
count_key_derivations() as derive,
loguru_caplog.at_level("WARNING"),
):
assert not source._verify_backup(result.backup_path)
assert derive.call_count == 0
assert "has no salt file, but its database has one" in loguru_caplog.text
assert "legacy shared salt" not in loguru_caplog.text
def test_legacy_backup_without_salt_still_verifies(source):
conn = create_source(source, legacy=True)
conn.close()
result = source.create_backup()
assert result.success, result.error
assert not get_salt_file_path(source.db_path).exists()
assert not get_salt_file_path(result.backup_path).exists()
with count_key_derivations() as derive:
assert source._verify_backup(result.backup_path)
# A database without its own salt still uses the legacy derivation.
assert derive.call_count == 1
@pytest.mark.parametrize(
"moment", ["before-size-check", "before-open", "while-open"]
)
def test_backup_removed_during_its_check_counts_as_absent(
source, monkeypatch, moment
):
conn = create_source(source)
conn.close()
first = source.create_backup()
assert first.success, first.error
target = first.backup_path
# A new timestamp, so the daily check verifies the backup again.
shift_mtime(target, 1)
removed = []
def remove_once(path):
# As another process deleting the backup would.
if Path(path) == target and not removed:
removed.append(path)
BackupService._remove_backup_files(target)
if moment == "before-size-check":
is_file = Path.is_file
def checked_then_removed(path, *args, **kwargs):
found = is_file(path, *args, **kwargs)
remove_once(path)
return found
monkeypatch.setattr(Path, "is_file", checked_then_removed)
elif moment == "before-open":
# SQLite then creates an empty database at the removed path.
get_module = sqlcipher_compat.get_sqlcipher_module
def removed_then_opened():
remove_once(target)
return get_module()
monkeypatch.setattr(
sqlcipher_compat, "get_sqlcipher_module", removed_then_opened
)
else:
set_key = backup_service.set_sqlcipher_key
def keyed_then_removed(cursor, password, db_path=None):
set_key(cursor, password, db_path=db_path)
remove_once(db_path)
monkeypatch.setattr(
backup_service, "set_sqlcipher_key", keyed_then_removed
)
result = source.create_backup()
assert removed
assert result.success, result.error
assert result.backup_path != target
assert source._verify_backup(result.backup_path)
def test_backup_whose_salt_is_removed_during_its_check_does_not_count(
source, monkeypatch
):
conn = create_source(source)
conn.close()
first = source.create_backup()
assert first.success, first.error
target = first.backup_path
# A new timestamp, so the daily check verifies the backup again.
shift_mtime(target, 1)
set_key = backup_service.set_sqlcipher_key
removed = []
def keyed_then_salt_removed(cursor, password, db_path=None):
set_key(cursor, password, db_path=db_path)
# As another process deleting the salt once the key is derived:
# the check still passes, but the backup is no longer complete.
if Path(db_path) == target and not removed:
removed.append(db_path)
get_salt_file_path(target).unlink()
monkeypatch.setattr(
backup_service, "set_sqlcipher_key", keyed_then_salt_removed
)
result = source.create_backup()
assert removed
assert result.success, result.error
assert result.backup_path != target
assert source._verify_backup(result.backup_path)
def test_export_removed_during_its_verification_is_never_published(
source, monkeypatch
):
conn = create_source(source)
conn.close()
first = source.create_backup()
assert first.success, first.error
source.max_backups = 1
before = {p.name: p.read_bytes() for p in source.backup_dir.iterdir()}
get_module = sqlcipher_compat.get_sqlcipher_module
removed = []
def remove_finished_export():
# Runs before each SQLCipher open. Once the export holds data, the
# next open is its verification: remove it first, as another
# process would, so SQLite opens an empty database in its place.
for export in source.backup_dir.glob("ldr_backup_*.db.tmp"):
if export.stat().st_size and not removed:
removed.append(export)
BackupService._remove_backup_files(export)
return get_module()
monkeypatch.setattr(
sqlcipher_compat, "get_sqlcipher_module", remove_finished_export
)
result = source.create_backup(force=True)
assert removed
assert not result.success
# No empty backup was published, so retention kept the earlier one.
assert {
p.name: p.read_bytes() for p in source.backup_dir.iterdir()
} == before
@pytest.mark.parametrize(
"moment", ["during-verification", "before-publication", "between-renames"]
)
def test_export_whose_salt_disappears_is_never_published(
source, monkeypatch, moment
):
conn = create_source(source)
conn.close()
first = source.create_backup()
assert first.success, first.error
source.max_backups = 1
before = {p.name: p.read_bytes() for p in source.backup_dir.iterdir()}
removed = []
def remove_salt(database):
# As another process deleting the new backup's salt would.
salt = get_salt_file_path(database)
if not removed and salt.exists():
removed.append(salt.name)
salt.unlink()
if moment == "during-verification":
set_key = backup_service.set_sqlcipher_key
def keyed_then_removed(cursor, password, db_path=None):
set_key(cursor, password, db_path=db_path)
remove_salt(Path(db_path))
monkeypatch.setattr(
backup_service, "set_sqlcipher_key", keyed_then_removed
)
elif moment == "before-publication":
verify = BackupService._verify_and_identify
def verified_then_removed(self, path):
outcome = verify(self, path)
remove_salt(path)
return outcome
monkeypatch.setattr(
BackupService, "_verify_and_identify", verified_then_removed
)
rename = Path.rename
renamed = []
def recorded_rename(path, target):
renamed.append(path.name)
if moment == "between-renames" and path.name.endswith(".db.tmp"):
# The salt is published first: remove it from its final name
# just before the database follows.
remove_salt(Path(target))
return rename(path, target)
monkeypatch.setattr(Path, "rename", recorded_rename)
result = source.create_backup(force=True)
assert removed
assert not result.success
# A missing salt stops publication before the database is renamed; a
# salt that goes after its own rename gets the database removed again.
database_renames = [n for n in renamed if n.endswith(".db.tmp")]
expected = 1 if moment == "between-renames" else 0
assert len(database_renames) == expected, renamed
# No incomplete backup is left, so retention kept the earlier pair.
assert {
p.name: p.read_bytes() for p in source.backup_dir.iterdir()
} == before
assert source._verify_backup(first.backup_path)
def file_digests(directory):
"""Each file's name and SHA-256 digest; a failed comparison of these
shows no database or salt bytes."""
return {
path.name: hashlib.sha256(path.read_bytes()).hexdigest()
for path in directory.iterdir()
}
@pytest.mark.parametrize(
"change", ["database-grows", "salt-grows", "salt-appears-beside-legacy"]
)
def test_export_whose_files_change_size_before_publication_is_removed(
source, monkeypatch, change
):
"""After its verification and before its publication, the new backup's
database grows, its salt grows, or a salt file appears beside a legacy
backup, which was verified without one. Publication checks that each
file is there with the size it had when verified, and that a backup
verified without a salt still has none, so the new backup is removed,
and retention keeps the earlier one."""
legacy = change == "salt-appears-beside-legacy"
conn = create_source(source, legacy=legacy)
conn.close()
first = source.create_backup()
assert first.success, first.error
assert get_salt_file_path(first.backup_path).exists() is not legacy
source.max_backups = 1
before = file_digests(source.backup_dir)
verify = BackupService._verify_and_identify
changed = []
def verified_then_changed(self, path):
outcome = verify(self, path)
if change == "database-grows":
target = path
elif change == "salt-grows":
target = get_salt_file_path(path)
else:
# Where the salt of the published backup would be.
target = get_salt_file_path(path.with_suffix(""))
with target.open("ab") as handle:
handle.write(b"\0")
changed.append(target.name)
return outcome
monkeypatch.setattr(
BackupService, "_verify_and_identify", verified_then_changed
)
result = source.create_backup(force=True)
assert changed
assert not result.success
assert result.error == "Backup files changed during publication"
assert file_digests(source.backup_dir) == before
assert source._verify_backup(first.backup_path)
def test_password_change_forgets_passes_verified_with_the_old_password(
source,
):
conn = create_source(source)
conn.close()
old = source.create_backup()
assert old.success, old.error
conn = create_sqlcipher_connection(source.db_path, PASSWORD)
try:
set_sqlcipher_rekey(conn, NEW_PASSWORD, db_path=source.db_path)
finally:
conn.close()
changed = BackupService(source.username, NEW_PASSWORD, max_backups=3)
# The purge deletes nothing and has no room for a new-password backup.
with (
patch.object(
BackupService,
"_remove_backup_files",
side_effect=OSError("in use"),
),
patch("shutil.disk_usage", return_value=MagicMock(free=0)),
):
refreshed = changed.purge_and_refresh()
assert not refreshed.success
assert old.backup_path.exists()
# The old-password backup must not stand in for today's backup.
replacement = changed.create_backup()
assert replacement.success, replacement.error
assert replacement.backup_path != old.backup_path
assert changed._verify_backup(replacement.backup_path)
assert not changed._verify_backup(old.backup_path)
def test_failed_publication_keeps_prior_backup_and_removes_partial_pair(source):
conn = create_source(source)
conn.close()
first = source.create_backup()
assert first.success, first.error
before = {p.name: p.read_bytes() for p in source.backup_dir.iterdir()}
rename = Path.rename
renames = []
def fail_database_publish(path, target):
# Only record here. _create_backup_impl turns any exception, an
# AssertionError included, into success=False, which this test
# expects anyway, so an assert at this point could never fail it.
target = Path(target)
renames.append((path, target, get_salt_file_path(target).exists()))
if path.name.endswith(".db.tmp"):
raise OSError("simulated publication failure")
return rename(path, target)
with patch.object(Path, "rename", fail_database_publish):
failed = source.create_backup(force=True)
assert not failed.success
assert failed.error == "simulated publication failure"
# The salt was renamed first and was in place when the database rename
# ran; reversing the two renames fails here.
assert len(renames) == 2, renames
(salt_from, salt_to, _), (db_from, db_to, salt_published) = renames
assert db_from.name.endswith(".db.tmp")
assert db_to == db_from.with_suffix("")
assert (salt_from, salt_to) == (
get_salt_file_path(db_from),
get_salt_file_path(db_to),
)
assert salt_published
assert {
p.name: p.read_bytes() for p in source.backup_dir.iterdir()
} == before
assert source._verify_backup(first.backup_path)
def test_salt_write_failure_keeps_prior_backup(source):
conn = create_source(source)
conn.close()
first = source.create_backup()
assert first.success, first.error
before = sorted(p.name for p in source.backup_dir.iterdir())
with patch(
"local_deep_research.database.backup.backup_service.os.fsync",
side_effect=OSError("disk full"),
):
failed = source.create_backup(force=True)
assert not failed.success
assert sorted(p.name for p in source.backup_dir.iterdir()) == before
assert source._verify_backup(first.backup_path)
def test_destination_collision_never_removes_existing_backup(source):
conn = create_source(source)
conn.close()
first = source.create_backup()
assert first.success, first.error
before = {p.name: p.read_bytes() for p in source.backup_dir.iterdir()}
temporary = first.backup_path.with_suffix(".db.tmp")
fd = os.open(temporary, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600)
with patch(
"local_deep_research.database.backup.backup_service.tempfile.mkstemp",
return_value=(fd, str(temporary)),
):
result = source.create_backup(force=True)
assert not result.success
assert {
p.name: p.read_bytes() for p in source.backup_dir.iterdir()
} == before
def test_forced_backups_are_unique_and_retention_removes_salts(source):
conn = create_source(source)
conn.close()
source.max_backups = 2
with patch(
"local_deep_research.database.backup.backup_service.datetime"
) as clock:
clock.now.return_value = datetime.now(UTC)
clock.fromtimestamp.side_effect = datetime.fromtimestamp
results = [source.create_backup(force=True) for _ in range(3)]
assert all(r.success for r in results)
assert len({r.backup_path for r in results}) == 3
assert not results[0].backup_path.exists()
assert not get_salt_file_path(results[0].backup_path).exists()
for result in results[1:]:
assert source._verify_backup(result.backup_path)
# Random suffixes do not determine which same-second backup is newest.
# Both passes are remembered, so the newer one is reported unverified.
with patch.object(
source, "_verify_backup", wraps=source._verify_backup
) as verify:
assert source.create_backup().backup_path == results[-1].backup_path
verify.assert_not_called()
# New timestamps on both leave neither with a remembered result, so the
# daily check verifies them newest first.
newest_time = datetime.now(UTC).timestamp() + 1
os.utime(results[1].backup_path, (newest_time - 60, newest_time - 60))
os.utime(results[-1].backup_path, (newest_time, newest_time))
assert source.create_backup().backup_path == results[-1].backup_path
assert len(list(source.backup_dir.iterdir())) == 4
def test_cleanup_removes_stale_interrupted_pairs_and_orphan_salts(source):
stale = (datetime.now(UTC) - timedelta(hours=2)).timestamp()
names = [
"ldr_backup_20200101_000000.db.tmp",
"ldr_backup_20200101_000000.db.tmp.salt",
"ldr_backup_20200101_000001.db.salt",
"ldr_backup_20200101_000002.db.tmp.salt",
]
for name in names:
path = source.backup_dir / name
path.write_bytes(b"incomplete")
os.utime(path, (stale, stale))
active_salt = source.backup_dir / "ldr_backup_20200101_000003.db.salt"
active_salt.write_bytes(b"recent publication")
source._cleanup_old_backups()
assert list(source.backup_dir.iterdir()) == [active_salt]
def test_purge_removes_database_and_salt_together(source):
conn = create_source(source)
conn.close()
first = source.create_backup()
assert first.success, first.error
refreshed = source.purge_and_refresh()
assert refreshed.success, refreshed.error
assert not first.backup_path.exists()
assert not get_salt_file_path(first.backup_path).exists()
assert source._verify_backup(refreshed.backup_path)