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.

47.4 KB
Raw Blame
"""Which backups the pending-rekey recovery purge deletes, keeps or moves.

``BackupService.purge_backups_under_superseded_key`` runs after a login
resolved a pending password change, with only the password that opened the
database. It must delete backups under the superseded key and nothing else:
a backup under the current key, a backup it cannot read (a lock), and a
backup under the current key with a damaged first page all survive. Real
SQLCipher throughout.
"""

import multiprocessing
import os
import sqlite3
import stat
import threading
import time
import uuid
from unittest.mock import patch

import pytest

from local_deep_research.config.paths import get_encrypted_database_path
from local_deep_research.database import encrypted_db
from local_deep_research.database.backup.backup_service import BackupService
from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.rekey_lock import (
    RekeyLockContentionError,
    acquire_rekey_lock,
    advance_rekey_epoch,
    begin_rekey_epoch,
    read_or_initialize_rekey_epoch,
    read_rekey_epoch_state,
)
from local_deep_research.database.sqlcipher_compat import (
    connect_sqlcipher,
    get_sqlcipher_module,
)
from local_deep_research.database.sqlcipher_utils import (
    apply_sqlcipher_pragmas,
    get_salt_file_path,
    set_sqlcipher_key,
)

sqlcipher_dbapi = pytest.importorskip("sqlcipher3.dbapi2")

OLD = "OldPurgeHorse1!"  # noqa: S105
NEW = "NewPurgeHorse2!"  # noqa: S105
LATEST = "LatestPurgeHorse3!"  # noqa: S105


@pytest.fixture
def manager(tmp_path, monkeypatch):
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    mgr = DatabaseManager()
    if not mgr.has_encryption:
        pytest.skip("SQLCipher not available")
    # The backup service resolves the database through the data directory.
    mgr.data_dir = get_encrypted_database_path()
    yield mgr
    mgr.close_all_databases()


def _user(manager):
    username = f"purge_{uuid.uuid4().hex[:8]}"
    assert manager.create_user_database(username, OLD) is not None
    return username, manager._get_user_db_path(username)


def _backup(username, password):
    result = BackupService(username=username, password=password).create_backup(
        force=True, prune=False
    )
    assert result.success, result.error
    return result.backup_path


def _completed_change_left_pending(manager, username, db_path):
    """A proven password change whose epoch commit failed (pending left)."""
    with patch.object(
        encrypted_db,
        "commit_rekey_epoch",
        side_effect=OSError(28, "No space left on device"),
    ):
        assert manager.change_password(username, OLD, NEW) is True
    assert read_rekey_epoch_state(db_path).pending_epoch is not None
    manager.close_all_databases()


def test_resolving_login_deletes_old_key_backup_and_keeps_new_key_backup(
    manager,
):
    # Given an old-key backup, then a backup made under the NEW key before
    # the login that resolves the pending change
    username, db_path = _user(manager)
    old_backup = _backup(username, OLD)
    _completed_change_left_pending(manager, username, db_path)
    new_backup = _backup(username, NEW)

    # When the user logs in with the new password
    assert manager.open_user_database(username, NEW) is not None

    # Then only the old-key backup is gone
    assert read_rekey_epoch_state(db_path).pending_epoch is None
    assert not old_backup.exists()
    assert new_backup.exists()
    assert get_salt_file_path(new_backup).exists()


def test_resolving_login_with_surviving_old_password_deletes_nothing(
    manager,
):
    # Given backups under the old key and a pending change that never applied
    username, db_path = _user(manager)
    backups = [_backup(username, OLD), _backup(username, OLD)]
    manager.close_all_databases()
    begin_rekey_epoch(db_path)

    # When the user logs in with the surviving old password
    assert manager.open_user_database(username, OLD) is not None

    # Then every backup is kept
    assert read_rekey_epoch_state(db_path).pending_epoch is None
    assert all(path.exists() for path in backups)


def test_resolving_login_keeps_a_later_password_changes_current_backup(
    manager,
):
    username, db_path = _user(manager)
    _completed_change_left_pending(manager, username, db_path)
    original_purge = manager._purge_superseded_key_backups
    later_backup = []

    def change_again_before_purge(user, password, **kwargs):
        # The recovery has finished and released its cold-open locks. A
        # second password change can complete before its cleanup resumes.
        assert read_rekey_epoch_state(db_path).pending_epoch is None
        assert manager.change_password(user, NEW, LATEST) is True
        backup = _backup(user, LATEST)
        assert (
            BackupService(username=user, password=LATEST)._classify_backup_key(
                backup
            )
            == "current"
        )
        later_backup.append(backup)
        original_purge(user, password, **kwargs)

    with (
        patch.object(
            manager,
            "_purge_superseded_key_backups",
            side_effect=change_again_before_purge,
        ),
        patch.object(manager, "_run_phase2_rekey"),
    ):
        assert manager.open_user_database(username, NEW) is not None

    # The stale recovery password would fail every page of this healthy
    # backup and incorrectly classify it as superseded without fencing.
    assert len(later_backup) == 1, "the second password change never ran"
    backup = later_backup[0]
    assert backup.exists(), "stale recovery deleted the current-key backup"
    assert get_salt_file_path(backup).exists()
    assert (
        BackupService(username=username, password=LATEST)._classify_backup_key(
            backup
        )
        == "current"
    )
    DatabaseManager._make_sqlcipher_connection(db_path, LATEST).close()


def _try_rekey_lock_in_child(db_path, outcomes):
    from pathlib import Path

    try:
        with acquire_rekey_lock(Path(db_path)):
            outcomes.put("acquired")
    except RekeyLockContentionError:
        outcomes.put("contended")


def test_recovery_holds_cross_process_rekey_lock_through_backup_deletion(
    manager,
):
    username, db_path = _user(manager)
    old_backup = _backup(username, OLD)
    _completed_change_left_pending(manager, username, db_path)
    original_remove = BackupService._remove_backup_files
    observed = []

    def remove_while_another_process_attempts_rekey(backup):
        context = multiprocessing.get_context("spawn")
        outcomes = context.Queue()
        contender = context.Process(
            target=_try_rekey_lock_in_child,
            args=(str(db_path), outcomes),
        )
        contender.start()
        try:
            observed.append(outcomes.get(timeout=20))
            contender.join(timeout=10)
            assert contender.exitcode == 0
        finally:
            if contender.is_alive():
                contender.terminate()
                contender.join(timeout=5)
            outcomes.close()
            outcomes.join_thread()
        original_remove(backup)

    with (
        patch.object(
            BackupService,
            "_remove_backup_files",
            side_effect=remove_while_another_process_attempts_rekey,
        ),
        patch.object(manager, "_run_phase2_rekey"),
    ):
        assert manager.open_user_database(username, NEW) is not None

    # Check outside the purge's best-effort handler: an assertion raised
    # inside deletion is caught there and must not make this test pass.
    assert observed == ["contended"]
    assert not old_backup.exists(), "the guarded purge never deleted anything"


@pytest.mark.parametrize(
    "state", ["different", "missing", "pending", "malformed"]
)
def test_recovery_skips_purge_without_its_stable_epoch(manager, state):
    db_path = manager._get_user_db_path("stale_recovery")
    with acquire_rekey_lock(db_path):
        epoch = read_or_initialize_rekey_epoch(db_path)
        if state == "different":
            advance_rekey_epoch(db_path)
        elif state == "missing":
            db_path.with_name(f"{db_path.name}.rekey.epoch").unlink()
        elif state == "pending":
            begin_rekey_epoch(db_path)
        else:
            db_path.with_name(f"{db_path.name}.rekey.epoch").write_text(
                "not-an-epoch", encoding="ascii"
            )

    with patch.object(
        BackupService, "purge_backups_under_superseded_key"
    ) as purge:
        manager._purge_superseded_key_backups(
            "stale_recovery", NEW, expected_epoch=epoch
        )
    purge.assert_not_called()


def test_recovery_does_not_purge_backups_of_a_different_database(manager):
    # A manager's pinned directory need not be the directory BackupService
    # resolves from configuration. Its epoch cannot authorize that purge.
    manager.data_dir = manager.data_dir / "pinned"
    username = "pinned_recovery"
    db_path = manager._get_user_db_path(username)
    with acquire_rekey_lock(db_path):
        epoch = read_or_initialize_rekey_epoch(db_path)
    assert BackupService(username=username, password=NEW).db_path != db_path

    with patch.object(
        BackupService, "purge_backups_under_superseded_key"
    ) as purge:
        manager._purge_superseded_key_backups(
            username, NEW, expected_epoch=epoch
        )
    purge.assert_not_called()


def test_purge_keeps_a_backup_it_cannot_read(manager):
    # Given an old-key backup that another connection holds locked
    username, db_path = _user(manager)
    old_backup = _backup(username, OLD)
    holder = connect_sqlcipher(
        get_sqlcipher_module(), str(old_backup), isolation_level=None
    )
    try:
        cursor = holder.cursor()
        set_sqlcipher_key(cursor, OLD, db_path=old_backup)
        apply_sqlcipher_pragmas(cursor, creation_mode=False)
        cursor.execute("PRAGMA locking_mode = EXCLUSIVE")
        cursor.execute("BEGIN EXCLUSIVE")
        cursor.execute("SELECT count(*) FROM sqlite_master").fetchone()

        # When the purge runs under the new password
        deleted = BackupService(
            username=username, password=NEW
        ).purge_backups_under_superseded_key()
    finally:
        holder.close()

    # Then the unreadable backup is kept: nothing proved it superseded
    assert deleted == 0
    assert old_backup.exists()


def test_purge_moves_a_damaged_current_key_backup_aside(manager):
    # Given a backup under the current key whose first page is damaged
    username, db_path = _user(manager)
    backup = _backup(username, OLD)
    with backup.open("r+b") as handle:
        handle.seek(200)
        byte = handle.read(1)
        handle.seek(200)
        handle.write(bytes([byte[0] ^ 0xFF]))
    service = BackupService(username=username, password=OLD)

    # When the purge runs under that same (current) password
    deleted = service.purge_backups_under_superseded_key()

    # Then it is not deleted but moved out of the backup set, owner-only
    assert deleted == 0
    assert not backup.exists()
    moved = list(backup.parent.glob(f"{backup.name}.damaged-*"))
    moved_db = [path for path in moved if not path.name.endswith(".salt")]
    assert len(moved_db) == 1
    assert stat.S_IMODE(os.stat(moved_db[0]).st_mode) == 0o600
    assert moved_db[0].with_name(f"{moved_db[0].name}.salt").exists()
    assert service.list_backups() == []
    # And a later purge leaves it alone
    assert service.purge_backups_under_superseded_key() == 0
    assert moved_db[0].exists()


def _first_call_raises(error):
    """Patch ``_classify_backup_key`` so its first call raises ``error``."""
    real = BackupService._classify_backup_key
    calls = []

    def classify(self, backup_path):
        calls.append(backup_path)
        if len(calls) == 1:
            raise error
        return real(self, backup_path)

    return patch.object(BackupService, "_classify_backup_key", classify), calls


@pytest.mark.parametrize(
    "error",
    [
        OSError(5, "Input/output error"),
        sqlite3.DatabaseError("file is not a database"),
        sqlcipher_dbapi.DatabaseError("file is not a database"),
        ValueError("unexpected salt size"),
    ],
    ids=["oserror", "sqlite3", "sqlcipher3", "valueerror"],
)
def test_purge_continues_past_a_backup_that_raises(manager, error):
    # Given two old-key backups, the first examined of which raises
    username, db_path = _user(manager)
    backups = [_backup(username, OLD), _backup(username, OLD)]
    patcher, calls = _first_call_raises(error)

    # When the purge runs under the new password
    with patcher:
        deleted = BackupService(
            username=username, password=NEW
        ).purge_backups_under_superseded_key()

    # Then the failing backup is kept and the other is still classified
    # and deleted
    assert len(calls) == 2
    assert deleted == 1
    survivors = [b for b in backups if b.exists()]
    assert survivors == [calls[0]]


def test_purge_still_surfaces_unexpected_errors(manager):
    # Given two old-key backups, the first examined of which hits a bug
    username, db_path = _user(manager)
    backups = [_backup(username, OLD), _backup(username, OLD)]
    patcher, calls = _first_call_raises(RuntimeError("bug"))

    # When the purge runs, the error is not swallowed
    with patcher, pytest.raises(RuntimeError, match="bug"):
        BackupService(
            username=username, password=NEW
        ).purge_backups_under_superseded_key()

    # And nothing was deleted on the way out
    assert all(b.exists() for b in backups)


_SERVICE_LOGGER = "local_deep_research.database.backup.backup_service.logger"


def _warnings(log):
    return [str(call.args[0]) for call in log.warning.call_args_list]


def test_purge_continues_when_a_damaged_backup_cannot_be_moved_aside(manager):
    # Given two old-key backups, the first examined classified as damaged
    username, db_path = _user(manager)
    backups = [_backup(username, OLD), _backup(username, OLD)]
    real = BackupService._classify_backup_key
    examined = []

    def classify(self, backup_path):
        examined.append(backup_path)
        return "damaged" if len(examined) == 1 else real(self, backup_path)

    # When the move-aside fails (returns None) during the purge
    with (
        patch.object(BackupService, "_classify_backup_key", classify),
        patch.object(
            BackupService, "_move_damaged_backup_aside", return_value=None
        ) as move_aside,
        patch(_SERVICE_LOGGER) as log,
    ):
        deleted = BackupService(
            username=username, password=NEW
        ).purge_backups_under_superseded_key()

    # Then the damaged backup is kept and reported, and the loop carries on
    # to delete the other superseded backup
    move_aside.assert_called_once_with(examined[0])
    assert any("could not be moved aside" in w for w in _warnings(log))
    assert len(examined) == 2
    assert deleted == 1
    assert [b for b in backups if b.exists()] == [examined[0]]


def test_purge_continues_when_deleting_a_superseded_backup_fails(manager):
    # Given two superseded backups, the first removal of which raises OSError
    username, db_path = _user(manager)
    backups = [_backup(username, OLD), _backup(username, OLD)]
    real = BackupService._remove_backup_files
    attempted = []

    def remove(backup_path):
        attempted.append(backup_path)
        if len(attempted) == 1:
            raise OSError(13, "Permission denied")
        real(backup_path)

    # When the purge runs under the new password
    with (
        patch.object(
            BackupService, "_remove_backup_files", staticmethod(remove)
        ),
        patch(_SERVICE_LOGGER) as log,
    ):
        deleted = BackupService(
            username=username, password=NEW
        ).purge_backups_under_superseded_key()

    # Then the failed deletion is reported and not counted, the backup is
    # kept, and the loop still deletes the other one
    assert any("Could not delete backup" in w for w in _warnings(log))
    assert len(attempted) == 2
    assert deleted == 1
    assert [b for b in backups if b.exists()] == [attempted[0]]


def _rekey_live_database(db_path, old, new):
    from local_deep_research.database.sqlcipher_utils import set_sqlcipher_rekey

    conn = DatabaseManager._make_sqlcipher_connection(db_path, old)
    try:
        set_sqlcipher_rekey(conn, new, db_path=db_path)
        conn.commit()
    finally:
        conn.close()


def test_current_key_backup_with_missing_salt_is_kept(manager):
    # Given a backup under the current key whose salt file is gone, so the
    # key it derives (legacy salt) fails every page
    username, _db_path = _user(manager)
    backup = _backup(username, OLD)
    get_salt_file_path(backup).unlink()
    service = BackupService(username=username, password=OLD)

    # When it is classified and the purge runs
    assert service._classify_backup_key(backup) == "unknown"
    assert service.purge_backups_under_superseded_key() == 0

    # Then it is kept: restoring the salt would make it readable
    assert backup.exists()


def test_current_key_backup_with_damaged_salt_is_kept(manager):
    # Given a backup under the current key with a same-size damaged salt
    username, _db_path = _user(manager)
    backup = _backup(username, OLD)
    salt_path = get_salt_file_path(backup)
    salt = bytearray(salt_path.read_bytes())
    salt[0] ^= 0x01
    salt_path.write_bytes(bytes(salt))
    service = BackupService(username=username, password=OLD)

    # When it is classified and the purge runs
    assert service._classify_backup_key(backup) == "unknown"
    assert service.purge_backups_under_superseded_key() == 0

    # Then it is kept
    assert backup.exists()
    assert salt_path.exists()


def test_old_password_backup_sharing_the_live_salt_is_still_deleted(manager):
    # Given an old-password backup (same salt as the live database, which
    # a rekey never changes) and the live database rekeyed to NEW
    username, db_path = _user(manager)
    backup = _backup(username, OLD)
    manager.close_all_databases()
    _rekey_live_database(db_path, OLD, NEW)
    service = BackupService(username=username, password=NEW)

    # When the purge runs under the new password
    assert service._classify_backup_key(backup) == "superseded"
    assert service.purge_backups_under_superseded_key() == 1

    # Then the old-password backup is gone
    assert not backup.exists()


def test_legacy_salt_database_old_password_backup_is_deleted(manager):
    # Given a legacy database (no .salt: the built-in shared salt) and an
    # old-password backup of it, which has no .salt either; both derive
    # their key from the same legacy salt, so a key failure is the password
    from local_deep_research.database.sqlcipher_utils import (
        create_sqlcipher_connection,
    )
    from local_deep_research.config.paths import get_user_database_filename

    username = f"legacy_{uuid.uuid4().hex[:8]}"
    db_path = get_encrypted_database_path() / get_user_database_filename(
        username
    )
    conn = create_sqlcipher_connection(db_path, OLD, creation_mode=True)
    try:
        conn.execute("CREATE TABLE notes (body TEXT)")
        conn.execute("INSERT INTO notes VALUES ('x')")
        conn.commit()
    finally:
        conn.close()
    assert not get_salt_file_path(db_path).exists()
    backup = _backup(username, OLD)
    assert not get_salt_file_path(backup).exists()
    _rekey_live_database(db_path, OLD, NEW)
    service = BackupService(username=username, password=NEW)

    # When the purge runs under the new password
    assert service._classify_backup_key(backup) == "superseded"
    assert service.purge_backups_under_superseded_key() == 1

    # Then it is deleted like any old-password backup
    assert not backup.exists()


def _quarantine_during_recovery(manager):
    username, db_path = _user(manager)
    backup = _backup(username, OLD)
    with backup.open("r+b") as handle:
        handle.seek(200)
        byte = handle.read(1)
        handle.seek(200)
        handle.write(bytes([byte[0] ^ 0xFF]))
    manager.close_all_databases()
    with acquire_rekey_lock(db_path):
        begin_rekey_epoch(db_path)
    assert manager.open_user_database(username, OLD) is not None
    quarantines = [
        path
        for path in backup.parent.glob(f"{backup.name}.damaged-*")
        if not path.name.endswith(".salt")
    ]
    assert len(quarantines) == 1
    quarantined = quarantines[0]
    assert (
        BackupService(username=username, password=OLD)._classify_backup_key(
            quarantined
        )
        == "damaged"
    )
    assert get_salt_file_path(quarantined).exists()
    return username, db_path, quarantined


def test_completed_password_change_purges_quarantined_old_key_copy(manager):
    username, db_path, quarantined = _quarantine_during_recovery(manager)
    assert manager.change_password(username, OLD, NEW) is True
    refreshed = BackupService(
        username=username, password=NEW
    ).purge_and_refresh()

    assert refreshed.success, refreshed.error
    assert not quarantined.exists()
    assert not get_salt_file_path(quarantined).exists()
    assert (
        BackupService(username=username, password=NEW)._classify_backup_key(
            refreshed.backup_path
        )
        == "current"
    )
    connection = DatabaseManager._make_sqlcipher_connection(db_path, NEW)
    connection.close()


def test_later_pending_recovery_purges_quarantined_old_key_copy(manager):
    username, db_path, quarantined = _quarantine_during_recovery(manager)
    _completed_change_left_pending(manager, username, db_path)

    assert manager.open_user_database(username, NEW) is not None

    assert not quarantined.exists()
    assert not get_salt_file_path(quarantined).exists()
    assert read_rekey_epoch_state(db_path).pending_epoch is None


def test_recovery_keeps_current_key_quarantine_without_renaming_it(manager):
    username, db_path, quarantined = _quarantine_during_recovery(manager)
    before = quarantined.read_bytes()
    manager.close_all_databases()
    with acquire_rekey_lock(db_path):
        begin_rekey_epoch(db_path)

    assert manager.open_user_database(username, OLD) is not None

    assert quarantined.read_bytes() == before
    assert get_salt_file_path(quarantined).exists()
    assert len(list(quarantined.parent.glob("*.damaged-*"))) == 2
    assert BackupService(username=username, password=OLD).list_backups() == []
    assert (
        BackupService(username=username, password=OLD).get_latest_backup()
        is None
    )


@pytest.mark.parametrize("salt_state", ["missing", "different"])
def test_recovery_keeps_quarantined_copy_without_a_matching_salt(
    manager, salt_state
):
    username, db_path, quarantined = _quarantine_during_recovery(manager)
    salt_path = get_salt_file_path(quarantined)
    if salt_state == "missing":
        salt_path.unlink()
    else:
        salt = bytearray(salt_path.read_bytes())
        salt[0] ^= 0x01
        salt_path.write_bytes(salt)
    _completed_change_left_pending(manager, username, db_path)

    assert manager.open_user_database(username, NEW) is not None

    assert quarantined.exists()
    if salt_state == "different":
        assert salt_path.exists()


def _retention_service(tmp_path, max_backups=1, max_age_days=7):
    service = BackupService(
        username="quarantine_retention",
        password=OLD,
        max_backups=max_backups,
        max_age_days=max_age_days,
    )
    service.backup_dir = tmp_path / "backups"
    service.backup_dir.mkdir()
    service.db_path = tmp_path / "unused.db"
    return service


def _quarantined_pair(service, name="fixture", age_days=0):
    path = service.backup_dir / (
        f"ldr_backup_{name}.db.damaged-20261018_170000_123456"
    )
    path.write_bytes(b"\x01" * 32)
    get_salt_file_path(path).write_bytes(b"\x02" * 16)
    modified = time.time() - age_days * 86400
    os.utime(path, (modified, modified))
    return path


def test_retention_removes_an_aged_quarantine_and_its_salt(tmp_path):
    service = _retention_service(tmp_path, max_backups=5)
    quarantined = _quarantined_pair(service, age_days=8)

    assert service._cleanup_old_backups() == 1

    assert not quarantined.exists()
    assert not get_salt_file_path(quarantined).exists()


def test_retention_cleans_only_recognized_stale_quarantine_orphan_salts(
    tmp_path,
):
    service = _retention_service(tmp_path)
    quarantined = _quarantined_pair(service)
    quarantined.unlink()
    orphan = get_salt_file_path(quarantined)
    unrelated = service.backup_dir / (
        "ldr_backup_operator.db.damaged-not-a-stamp.salt"
    )
    unrelated.write_bytes(b"\x02" * 16)
    modified = time.time() - 2 * 3600
    os.utime(orphan, (modified, modified))
    os.utime(unrelated, (modified, modified))

    assert service._cleanup_old_backups() == 0

    assert not orphan.exists()
    assert unrelated.exists()


@pytest.mark.parametrize("max_backups", [1, 2])
def test_quarantine_count_retention_preserves_usable_restore_candidate(
    tmp_path, max_backups
):
    service = _retention_service(
        tmp_path, max_backups=max_backups, max_age_days=0
    )
    usable = service.backup_dir / "ldr_backup_usable.db"
    usable.write_bytes(b"\x03" * 32)
    modified = time.time() - 3 * 86400
    os.utime(usable, (modified, modified))
    old_quarantine = _quarantined_pair(service, "older", age_days=2)
    new_quarantine = _quarantined_pair(service, "newer", age_days=1)

    assert service._cleanup_old_backups() == 3 - max_backups

    assert usable.exists(), "damaged copies displaced the usable backup"
    assert service.get_latest_backup() == usable
    assert not old_quarantine.exists()
    assert not get_salt_file_path(old_quarantine).exists()
    assert new_quarantine.exists() is (max_backups == 2)
    assert get_salt_file_path(new_quarantine).exists() is (max_backups == 2)


@pytest.mark.parametrize("operation", ["normal", "recovery", "retention"])
def test_quarantine_cleanup_does_not_follow_links_or_match_unrelated_files(
    tmp_path, operation
):
    service = _retention_service(tmp_path, max_age_days=1)
    outside = tmp_path / "outside.db"
    outside.write_bytes(b"\x04" * 32)
    linked = service.backup_dir / (
        "ldr_backup_link.db.damaged-20261018_170000_123456"
    )
    linked.symlink_to(outside)
    directory = service.backup_dir / (
        "ldr_backup_directory.db.damaged-20261018_170000_123456"
    )
    directory.mkdir()
    unrelated = (
        service.backup_dir / "ldr_backup_operator.db.damaged-not-a-stamp"
    )
    unrelated.write_bytes(b"\x05" * 32)
    before = outside.read_bytes()
    if operation == "normal":
        with patch.object(service, "_create_backup_impl"):
            service.purge_and_refresh()
    elif operation == "recovery":
        assert service.purge_backups_under_superseded_key() == 0
    else:
        modified = time.time() - 2 * 86400
        os.utime(unrelated, (modified, modified))
        assert service._cleanup_old_backups() == 0

    assert outside.read_bytes() == before
    assert linked.is_symlink()
    assert directory.is_dir()
    assert unrelated.exists()


# --- Route-side refresh after a completed password change (#7677) ----------


def test_refresh_after_change_replaces_old_key_backups(manager):
    # Given an old-key backup and a completed, published password change
    username, db_path = _user(manager)
    old_backup = _backup(username, OLD)
    assert manager.change_password(username, OLD, NEW) is True

    # When the route refreshes backups for that change
    result = manager.refresh_backups_after_password_change(username, NEW)

    # Then the old-key backup is gone and a new-key backup exists
    assert result is not None and result.success, result
    assert not old_backup.exists()
    assert result.backup_path.exists()
    assert (
        BackupService(username=username, password=NEW)._classify_backup_key(
            result.backup_path
        )
        == "current"
    )


def test_refresh_after_change_keeps_a_later_changes_fresh_backup(manager):
    # Given the first change completed and released the sidecar
    username, db_path = _user(manager)
    _backup(username, OLD)
    assert manager.change_password(username, OLD, NEW) is True
    later_backup = []

    def later_change_from_another_request():
        # A second change lands in between, then mints its fresh backup.
        # It runs on its own thread, so it cannot overwrite the first
        # change's record on this thread.
        assert manager.change_password(username, NEW, LATEST) is True
        later_backup.append(_backup(username, LATEST))

    worker = threading.Thread(target=later_change_from_another_request)
    worker.start()
    worker.join(timeout=60)
    assert len(later_backup) == 1, "the second password change never ran"

    # When the first change's route refresh finally runs
    result = manager.refresh_backups_after_password_change(username, NEW)

    # Then it stands down and the later change's backup survives
    assert result is None
    backup = later_backup[0]
    assert backup.exists(), "stale route refresh deleted the fresh backup"
    assert get_salt_file_path(backup).exists()
    assert (
        BackupService(username=username, password=LATEST)._classify_backup_key(
            backup
        )
        == "current"
    )


def test_refresh_after_change_skips_when_the_epoch_was_never_published(
    manager,
):
    # Given a proven change whose epoch commit failed (pending left behind)
    username, db_path = _user(manager)
    old_backup = _backup(username, OLD)
    with patch.object(
        encrypted_db,
        "commit_rekey_epoch",
        side_effect=OSError(28, "No space left on device"),
    ):
        assert manager.change_password(username, OLD, NEW) is True

    # When the route refreshes
    result = manager.refresh_backups_after_password_change(username, NEW)

    # Then nothing is deleted now; the next open resolves it and purges
    assert result is None
    assert old_backup.exists()
    manager.close_all_databases()
    assert manager.open_user_database(username, NEW) is not None
    assert not old_backup.exists()


def test_refresh_without_a_change_on_this_thread_does_nothing(manager):
    username, db_path = _user(manager)
    backup = _backup(username, OLD)

    with patch.object(BackupService, "purge_and_refresh") as refresh:
        assert (
            manager.refresh_backups_after_password_change(username, OLD) is None
        )
    refresh.assert_not_called()
    assert backup.exists()


def test_refresh_is_single_use_per_change(manager):
    username, db_path = _user(manager)
    assert manager.change_password(username, OLD, NEW) is True
    assert manager.refresh_backups_after_password_change(username, NEW)

    with patch.object(BackupService, "purge_and_refresh") as refresh:
        assert (
            manager.refresh_backups_after_password_change(username, NEW) is None
        )
    refresh.assert_not_called()


def test_refresh_holds_cross_process_rekey_lock_through_the_purge(manager):
    username, db_path = _user(manager)
    _backup(username, OLD)
    assert manager.change_password(username, OLD, NEW) is True
    original_remove = BackupService._remove_backup_files
    observed = []

    def remove_while_another_process_attempts_rekey(backup):
        context = multiprocessing.get_context("spawn")
        outcomes = context.Queue()
        contender = context.Process(
            target=_try_rekey_lock_in_child,
            args=(str(db_path), outcomes),
        )
        contender.start()
        try:
            observed.append(outcomes.get(timeout=20))
            contender.join(timeout=10)
            assert contender.exitcode == 0
        finally:
            if contender.is_alive():
                contender.terminate()
        original_remove(backup)

    with patch.object(
        BackupService,
        "_remove_backup_files",
        side_effect=remove_while_another_process_attempts_rekey,
    ):
        assert manager.refresh_backups_after_password_change(username, NEW)

    assert observed and set(observed) == {"contended"}


# --- Backup status after the route refresh (#7443) ---------------------------


def _stored_purge_flag(db_path, password):
    """The purge flag stored in the user database, or None if none is."""
    from local_deep_research.database.backup import backup_status
    from local_deep_research.database.sqlcipher_utils import (
        create_sqlcipher_connection,
    )

    connection = create_sqlcipher_connection(str(db_path), password)
    try:
        if not connection.execute(
            "SELECT name FROM sqlite_master WHERE name = ?",
            (backup_status._attempts.name,),
        ).fetchall():
            return None
        row = connection.execute(
            "SELECT purge_incomplete FROM user_backup_attempt_status"
        ).fetchone()
        return None if row is None else bool(row[0])
    finally:
        connection.close()


@pytest.fixture
def status_cache(monkeypatch):
    from collections import OrderedDict

    from local_deep_research.database.backup import backup_status

    monkeypatch.setattr(backup_status, "_recent", OrderedDict())
    return backup_status


def test_refresh_records_a_verified_purge_without_the_engine_status_path(
    manager, status_cache, monkeypatch
):
    """The refresh holds the rekey sidecar; the open-engine status path
    takes the same sidecar and would stall until its wait bound. The purge
    result must go through the password session without trying it."""
    from unittest.mock import MagicMock

    username, db_path = _user(manager)
    _backup(username, OLD)
    assert manager.change_password(username, OLD, NEW) is True
    engine_path = MagicMock(side_effect=AssertionError("engine status path"))
    monkeypatch.setattr(status_cache, "_encrypted_status_session", engine_path)

    result = manager.refresh_backups_after_password_change(username, NEW)

    assert result is not None and result.success, result
    engine_path.assert_not_called()
    assert _stored_purge_flag(db_path, NEW) is False


def test_skipped_refresh_records_an_incomplete_purge(manager, status_cache):
    """A change whose epoch was never published is skipped: nothing is
    removed now, so old-key backups may remain."""
    username, db_path = _user(manager)
    _backup(username, OLD)
    with patch.object(
        encrypted_db,
        "commit_rekey_epoch",
        side_effect=OSError(28, "No space left on device"),
    ):
        assert manager.change_password(username, OLD, NEW) is True

    assert manager.refresh_backups_after_password_change(username, NEW) is None
    assert _stored_purge_flag(db_path, NEW) is True


def test_unfenced_refresh_records_an_incomplete_purge(manager, status_cache):
    username, db_path = _user(manager)
    _backup(username, OLD)

    assert manager.refresh_backups_after_password_change(username, OLD) is None
    assert _stored_purge_flag(db_path, OLD) is True


def test_contended_refresh_records_an_incomplete_purge(manager, status_cache):
    username, db_path = _user(manager)
    _backup(username, OLD)
    assert manager.change_password(username, OLD, NEW) is True

    with patch.object(
        encrypted_db,
        "acquire_rekey_lock",
        side_effect=RekeyLockContentionError(
            db_path.with_name(f"{db_path.name}.rekey.lock")
        ),
    ):
        with pytest.raises(RekeyLockContentionError):
            manager.refresh_backups_after_password_change(username, NEW)
    assert _stored_purge_flag(db_path, NEW) is True


def _refresh_fault(kind, db_path, monkeypatch):
    """Install one failure of the refresh before its purge starts."""
    import errno
    from contextlib import contextmanager

    from local_deep_research.database.backup import backup_service
    from local_deep_research.database.rekey_lock import (
        RekeyEpochState,
        RekeyEpochUnreadableError,
        RekeyRecoveryRequiredError,
        RekeySidecarIOError,
        RekeySidecarOperation,
    )

    if kind == "sidecar_io":

        def unlockable(*args, **kwargs):
            raise RekeySidecarIOError(
                db_path, RekeySidecarOperation.ACQUIRE, errno.EIO
            )

        monkeypatch.setattr(encrypted_db, "acquire_rekey_lock", unlockable)
        return RekeySidecarIOError
    if kind in ("epoch_unreadable", "recovery_required"):
        error = (
            RekeyEpochUnreadableError(db_path, errno.EIO)
            if kind == "epoch_unreadable"
            else RekeyRecoveryRequiredError(db_path)
        )

        def unreadable(path):
            raise error

        monkeypatch.setattr(encrypted_db, "read_rekey_epoch_state", unreadable)
        return type(error)
    if kind == "release_after_skip":

        @contextmanager
        def failing_release(*args, **kwargs):
            yield
            raise RekeySidecarIOError(
                db_path, RekeySidecarOperation.UNLOCK, errno.EIO
            )

        monkeypatch.setattr(encrypted_db, "acquire_rekey_lock", failing_release)
        monkeypatch.setattr(
            encrypted_db,
            "read_rekey_epoch_state",
            lambda path: RekeyEpochState("a-later-epoch", None),
        )
        return RekeySidecarIOError
    assert kind == "service_setup"

    def no_backup_directory(username):
        raise PermissionError(errno.EPERM, "Operation not permitted")

    monkeypatch.setattr(
        backup_service, "get_user_backup_directory", no_backup_directory
    )
    return PermissionError


@pytest.mark.parametrize(
    "kind",
    [
        "sidecar_io",
        "epoch_unreadable",
        "recovery_required",
        "release_after_skip",
        "service_setup",
    ],
)
def test_refresh_failing_before_its_purge_records_an_incomplete_purge(
    manager, status_cache, monkeypatch, kind
):
    """#7443: any error that ends the refresh before it purged leaves the
    old-key backups in place, so it is recorded as an incomplete purge and
    re-raised. The record goes through the new password, never through the
    open-engine path (which waits on the sidecar) or a BackupService (whose
    setup may be what failed)."""
    from unittest.mock import MagicMock

    username, db_path = _user(manager)
    old_backup = _backup(username, OLD)
    assert manager.change_password(username, OLD, NEW) is True
    engine_path = MagicMock(side_effect=AssertionError("engine status path"))
    monkeypatch.setattr(status_cache, "_encrypted_status_session", engine_path)
    error_type = _refresh_fault(kind, db_path, monkeypatch)

    with pytest.raises(error_type):
        manager.refresh_backups_after_password_change(username, NEW)

    assert old_backup.exists()
    engine_path.assert_not_called()
    assert _stored_purge_flag(db_path, NEW) is True


def test_refresh_for_a_different_service_database_records_an_incomplete_purge(
    manager, status_cache, monkeypatch, tmp_path
):
    """#7443 R6: the service resolving another database is a skip."""
    from local_deep_research.database.backup import backup_service

    username, db_path = _user(manager)
    old_backup = _backup(username, OLD)
    assert manager.change_password(username, OLD, NEW) is True
    elsewhere = tmp_path / "elsewhere"
    elsewhere.mkdir()
    monkeypatch.setattr(
        backup_service, "get_encrypted_database_path", lambda: elsewhere
    )

    assert manager.refresh_backups_after_password_change(username, NEW) is None
    assert old_backup.exists()
    assert _stored_purge_flag(db_path, NEW) is True


def test_recovery_purge_setup_failure_never_uses_the_engine_status_path(
    manager, status_cache, monkeypatch
):
    """#7443: the login-recovery purge holds the rekey sidecar. A
    BackupService setup failure there must not record through the open
    engine, whose claim waits on that same sidecar (a self-stall)."""
    import errno
    from unittest.mock import MagicMock

    from local_deep_research.database.backup import backup_service

    username, db_path = _user(manager)
    old_backup = _backup(username, OLD)
    _completed_change_left_pending(manager, username, db_path)
    engine_path = MagicMock(side_effect=AssertionError("engine status path"))
    monkeypatch.setattr(status_cache, "_encrypted_status_session", engine_path)

    def no_backup_directory(username):
        raise PermissionError(errno.EPERM, "Operation not permitted")

    monkeypatch.setattr(
        backup_service, "get_user_backup_directory", no_backup_directory
    )

    assert manager.open_user_database(username, NEW) is not None
    engine_path.assert_not_called()
    assert old_backup.exists()  # The best-effort purge was skipped.


def test_release_error_after_a_purge_that_ran_adds_no_record(
    manager, status_cache, monkeypatch
):
    """#7443: once purge_and_refresh ran, it recorded its own result; an
    error releasing the sidecar afterwards must not overwrite a verified
    clean purge with "incomplete"."""
    import errno
    from contextlib import contextmanager

    from local_deep_research.database.rekey_lock import (
        RekeySidecarIOError,
        RekeySidecarOperation,
    )

    username, db_path = _user(manager)
    old_backup = _backup(username, OLD)
    assert manager.change_password(username, OLD, NEW) is True

    @contextmanager
    def release_fails(*args, **kwargs):
        yield
        raise RekeySidecarIOError(
            db_path, RekeySidecarOperation.UNLOCK, errno.EIO
        )

    monkeypatch.setattr(encrypted_db, "acquire_rekey_lock", release_fails)

    with pytest.raises(RekeySidecarIOError):
        manager.refresh_backups_after_password_change(username, NEW)
    assert not old_backup.exists()
    assert _stored_purge_flag(db_path, NEW) is False


def _publish_after_purge_contention(db_path, advance, held, attempted, pipe):
    """Publish only after the parent has actually encountered our OS lock."""
    from pathlib import Path

    try:
        with acquire_rekey_lock(Path(db_path)):
            held.set()
            if not attempted.wait(30):
                raise TimeoutError("purge never encountered the held sidecar")
            if advance:
                advance_rekey_epoch(Path(db_path))
            pipe.send(("published", read_rekey_epoch_state(Path(db_path))))
    except BaseException as error:
        pipe.send(("error", repr(error)))
        raise
    finally:
        pipe.close()


@pytest.mark.parametrize(
    "advance", [True, False], ids=["advanced", "unchanged"]
)
def test_purge_reads_epoch_after_contended_sidecar_acquisition(
    manager, advance
):
    from local_deep_research.database import rekey_lock
    from local_deep_research.database.sqlcipher_utils import (
        create_database_salt,
        create_sqlcipher_connection,
    )

    # A minimal real database avoids exporting the application's full schema.
    username = f"epoch_race_{uuid.uuid4().hex[:8]}"
    db_path = manager._get_user_db_path(username)
    create_database_salt(db_path)
    connection = create_sqlcipher_connection(db_path, OLD, creation_mode=True)
    try:
        connection.execute("CREATE TABLE notes (body TEXT)")
        connection.execute("INSERT INTO notes VALUES ('epoch fence')")
        connection.commit()
    finally:
        connection.close()
    old_backup = _backup(username, OLD)
    _rekey_live_database(db_path, OLD, NEW)
    current_backup = _backup(username, NEW)
    service = BackupService(username=username, password=NEW)
    assert (
        BackupService(username=username, password=OLD)._classify_backup_key(
            old_backup
        )
        == "current"
    )
    assert service._classify_backup_key(old_backup) == "superseded"
    assert service._classify_backup_key(current_backup) == "current"
    pairs = [old_backup, current_backup]
    files = [
        path
        for backup in pairs
        for path in (backup, get_salt_file_path(backup))
    ]
    before = {path: path.read_bytes() for path in files}
    with acquire_rekey_lock(db_path):
        expected = read_or_initialize_rekey_epoch(db_path)

    context = multiprocessing.get_context("spawn")
    held, attempted = context.Event(), context.Event()
    receiver, sender = context.Pipe(duplex=False)
    child = context.Process(
        target=_publish_after_purge_contention,
        args=(str(db_path), advance, held, attempted, sender),
    )
    events, faults, reads, warnings, lock_paths = [], [], [], [], []
    symbol = (
        "_acquire_windows_lock" if os.name == "nt" else "_acquire_posix_lock"
    )
    original_acquire = getattr(rekey_lock, symbol)
    original_read = encrypted_db.read_rekey_epoch_state
    original_warning = encrypted_db.logger.warning

    def observe_acquire(fd, lock_path, **kwargs):
        lock_paths.append(lock_path)
        try:
            result = original_acquire(fd, lock_path, **kwargs)
        except RekeyLockContentionError:
            events.append("contended")
            attempted.set()
            raise
        except BaseException as error:
            faults.append(error)
            raise
        else:
            events.append("acquired")
            return result

    def observe_read(path):
        try:
            state = original_read(path)
        except BaseException as error:
            faults.append(error)
            raise
        reads.append((list(events), state))
        return state

    def observe_warning(*args, **kwargs):
        warnings.append(args)
        return original_warning(*args, **kwargs)

    try:
        child.start()
        sender.close()
        assert held.wait(30), "child never acquired the real sidecar"
        with (
            patch.object(rekey_lock, symbol, side_effect=observe_acquire),
            patch.object(
                encrypted_db, "read_rekey_epoch_state", side_effect=observe_read
            ),
            patch.object(
                encrypted_db.logger, "warning", side_effect=observe_warning
            ),
        ):
            manager._purge_superseded_key_backups(
                username, NEW, expected_epoch=expected
            )
        assert receiver.poll(30), "child never reported durable publication"
        outcome, published = receiver.recv()
        child.join(timeout=10)
        assert child.exitcode == 0, (child.exitcode, outcome, published)
        assert outcome == "published", published
        assert not faults, faults
        assert not warnings, warnings
        assert set(lock_paths) == {
            db_path.with_name(f"{db_path.name}.rekey.lock")
        }
        assert "contended" in events, events
        assert events[-1] == "acquired", events
        assert len(reads) == 1, reads
        assert published == read_rekey_epoch_state(db_path)
        assert published.pending_epoch is None
        assert (published.committed_epoch != expected) is advance
        protected = (
            files
            if advance
            else [current_backup, get_salt_file_path(current_backup)]
        )
        for path in protected:
            assert path.exists(), f"epoch fence lost {path.name}"
            assert path.read_bytes() == before[path]
        if not advance:
            assert not old_backup.exists(), (
                "unchanged epoch never performed deletion"
            )
            assert not get_salt_file_path(old_backup).exists()
        order, state = reads[0]
        # The control must still delete under the pre-lock-read mutant; the
        # advanced case is the guardian for read ordering and preservation.
        if advance:
            assert order and order[-1] == "acquired", (
                "epoch read preceded OS lock acquisition"
            )
            assert "contended" in order, order
        assert state == published
    finally:
        attempted.set()
        if child.pid is not None:
            child.join(timeout=10)
            if child.is_alive():
                child.terminate()
                child.join(timeout=5)
            if child.is_alive():
                child.kill()
                child.join(timeout=5)
        receiver.close()
        sender.close()
        assert not child.is_alive(), "owned epoch writer survived cleanup"
        child.close()