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.

29.1 KB
Raw Blame
"""Regression coverage for the change_password stale-cached-engine window.

Originally ``change_password`` cold-opened the user's database with the OLD
password (caching that engine together with a verifier for the old password),
rekeyed the SQLCipher file to the NEW password, and only then closed the
connection, so between the rekey and the close the cache held an engine whose
creator closure still derived the now-invalid OLD key while its verifier still
matched the OLD password; a concurrent ``open_user_database(username, old)``
could be handed that stale-key engine.

``change_password`` now never caches a connection of its own. Holding the
user's on-disk rekey sidecar lock for the whole staged sequence (the same lock
every cold open and every cache claim takes), it detaches and disposes any
cached engine BEFORE the rekey, rekeys through one uncached raw SQLCipher
connection, and publishes a new durable rekey epoch; an engine cached under a
superseded epoch is detached by the next claim instead of being handed out.
``test_change_password_evicts_cached_engine_before_raw_rekey`` pins that the
engine, its verifier and its epoch are all gone at the moment of the rekey and
that afterwards only the new password opens the database.

Surfaced by a multi-agent review of PR #5596. Extended to cover durable rekey
epochs, interrupted-rekey recovery, and fenced cache teardown.
"""

import builtins
import io
import os
import threading
import time
import uuid
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.database import encrypted_db
from local_deep_research.database import rekey_lock as rekey_lock_module
from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.rekey_lock import (
    RekeyIndeterminateError,
    RekeyStage,
    RekeyUnavailableError,
    acquire_rekey_lock,
    read_or_initialize_rekey_epoch,
    read_rekey_epoch_state,
)


@pytest.fixture
def manager(tmp_path, monkeypatch):
    """A DatabaseManager writing to an isolated data directory."""
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    mgr = DatabaseManager()
    mgr.data_dir = tmp_path
    yield mgr
    # Dispose the real SQLCipher engines these tests open (StaticPool under
    # TESTING=1 holds one live connection per engine indefinitely otherwise).
    mgr.close_all_databases()


def test_change_password_evicts_cached_engine_before_raw_rekey(
    manager, monkeypatch
):
    # Given a cached old-password engine for a real encrypted database
    if not manager.has_encryption:
        pytest.skip("SQLCipher not available; rekey path is a no-op")

    username = f"chgpwwin_{uuid.uuid4().hex[:8]}"
    old = "OldCorrectHorse1!"  # noqa: S105
    new = "NewCorrectHorse2!"  # noqa: S105

    engine = manager.create_user_database(username, old)
    assert engine is not None
    assert manager.is_user_connected(username)
    assert username in manager._password_verifiers
    assert username in manager._connection_epochs

    real_rekey = encrypted_db.set_sqlcipher_rekey
    cached_state_at_rekey = None

    def spy_rekey(conn, new_password, **kwargs):
        nonlocal cached_state_at_rekey
        cached_state_at_rekey = (
            username in manager.connections,
            username in manager._password_verifiers,
            username in manager._connection_epochs,
        )
        real_rekey(conn, new_password, **kwargs)

    monkeypatch.setattr(encrypted_db, "set_sqlcipher_rekey", spy_rekey)

    # When the password changes through one uncached raw SQLCipher connection
    assert manager.change_password(username, old, new) is True

    # Then old-key cache state was gone before rekey and credentials rotated
    assert cached_state_at_rekey == (False, False, False)
    assert username not in manager.connections
    assert username not in manager._password_verifiers
    assert username not in manager._connection_epochs

    assert manager.open_user_database(username, old) is None, (
        "the old password must not open the re-keyed database"
    )
    reopened = manager.open_user_database(username, new)
    assert reopened is not None
    assert manager._password_matches_cached(username, new)
    assert not manager._password_matches_cached(username, old)


def test_pending_recovery_accepts_real_surviving_old_password(
    manager, monkeypatch
):
    # Given SQLCipher leaves the old key intact after rekey fails post-pending
    if not manager.has_encryption:
        pytest.skip("SQLCipher not available; rekey path is a no-op")
    username = f"recoverold_{uuid.uuid4().hex[:8]}"
    old = "OldRecoveryHorse1!"  # noqa: S105
    new = "NewRecoveryHorse2!"  # noqa: S105
    engine = manager.create_user_database(username, old)
    assert engine is not None
    db_path = manager._get_user_db_path(username)
    original_epoch = read_rekey_epoch_state(db_path).committed_epoch
    real_rekey = encrypted_db.set_sqlcipher_rekey

    def fail_rekey(_connection, _new_password, *, db_path):
        raise RuntimeError("simulated pre-rekey failure")

    monkeypatch.setattr(encrypted_db, "set_sqlcipher_rekey", fail_rekey)

    # When password change fails after its pending generation is durable
    with pytest.raises(RekeyIndeterminateError) as raised:
        manager.change_password(username, old, new)
    assert raised.value.stage is RekeyStage.PHYSICAL_REKEY
    interrupted = read_rekey_epoch_state(db_path)
    assert interrupted.committed_epoch == original_epoch
    assert interrupted.pending_epoch is not None
    monkeypatch.setattr(encrypted_db, "set_sqlcipher_rekey", real_rekey)

    # Then a fresh real old-key open verifies and finalizes the pending generation
    reopened = manager.open_user_database(username, old)
    assert reopened is not None
    recovered = read_rekey_epoch_state(db_path)
    assert recovered.committed_epoch == interrupted.pending_epoch
    assert recovered.pending_epoch is None
    assert manager.open_user_database(username, new) is None


def test_pending_recovery_accepts_real_surviving_new_password(manager):
    # Given SQLCipher applies the new key but committed metadata publication fails
    if not manager.has_encryption:
        pytest.skip("SQLCipher not available; rekey path is a no-op")
    username = f"recovernew_{uuid.uuid4().hex[:8]}"
    old = "OldRecoveryHorse3!"  # noqa: S105
    new = "NewRecoveryHorse4!"  # noqa: S105
    engine = manager.create_user_database(username, old)
    assert engine is not None
    db_path = manager._get_user_db_path(username)
    original_epoch = read_rekey_epoch_state(db_path).committed_epoch

    with patch.object(
        encrypted_db,
        "commit_rekey_epoch",
        side_effect=OSError("simulated epoch publication failure"),
    ):
        # When durable candidate verification succeeds but E2 cannot publish,
        # the change is complete: the new key is proven, so it is reported
        # as such (the route then revokes every old-password credential)
        assert manager.change_password(username, old, new) is True

    interrupted = read_rekey_epoch_state(db_path)
    assert interrupted.committed_epoch == original_epoch
    assert interrupted.pending_epoch is not None

    # Then the wrong old key is an ordinary failed login (None, counted by
    # the login route's lockout) and leaves recovery state untouched
    assert manager.open_user_database(username, old) is None
    assert read_rekey_epoch_state(db_path) == interrupted

    # And the surviving real new key verifies before recovery publishes E2
    reopened = manager.open_user_database(username, new)
    assert reopened is not None
    recovered = read_rekey_epoch_state(db_path)
    assert recovered.committed_epoch == interrupted.pending_epoch
    assert recovered.pending_epoch is None
    assert manager.open_user_database(username, old) is None


def test_unpublished_epoch_after_proven_key_fences_stale_engine_and_purges_backups(
    manager, monkeypatch
):
    # Given a real database with a cached old-password engine and an
    # old-key backup
    if not manager.has_encryption:
        pytest.skip("SQLCipher not available; rekey path is a no-op")
    from local_deep_research.database.backup.backup_service import (
        BackupService,
    )

    from local_deep_research.config.paths import get_encrypted_database_path

    # The backup service resolves the database through the data directory,
    # so the manager must write where that resolution looks.
    manager.data_dir = get_encrypted_database_path()
    username = f"unpub_{uuid.uuid4().hex[:8]}"
    old = "OldUnpublishedHorse1!"  # noqa: S105
    new = "NewUnpublishedHorse2!"  # noqa: S105
    assert manager.create_user_database(username, old) is not None
    db_path = manager._get_user_db_path(username)
    old_backup = BackupService(username=username, password=old).create_backup(
        force=True, prune=False
    )
    assert old_backup.success, old_backup.error
    stale = manager.open_user_database(username, old)
    assert stale is not None
    original_epoch = read_rekey_epoch_state(db_path).committed_epoch

    # When the epoch publication fails after the new key was proven
    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

    # Then the raw file opens only with the new password
    with pytest.raises(ValueError):
        DatabaseManager._make_sqlcipher_connection(db_path, old)
    DatabaseManager._make_sqlcipher_connection(db_path, new).close()

    # And the pending generation fences any engine cached under the old
    # epoch: re-publish the stale engine as if a racing claim had kept it
    manager._cache_connection(
        username,
        manager._make_connection_cache_entry(stale, old, original_epoch),
    )
    assert manager.get_session(username) is None
    assert manager.open_user_database(username, old) is None
    pending = read_rekey_epoch_state(db_path).pending_epoch
    assert pending is not None

    # And the next open with the new password commits the pending epoch and
    # purges the backup the superseded key still opens
    assert old_backup.backup_path.exists()
    assert manager.open_user_database(username, new) is not None
    state = read_rekey_epoch_state(db_path)
    assert state.committed_epoch == pending
    assert state.pending_epoch is None
    assert not old_backup.backup_path.exists()


def test_epoch_replace_directory_fsync_failure_reconciles_real_rekey(
    manager, monkeypatch
):
    # Given a real SQLCipher database whose E2 replace becomes visible before fsync fails
    if not manager.has_encryption:
        pytest.skip("SQLCipher not available; rekey path is a no-op")
    username = f"replacefsync_{uuid.uuid4().hex[:8]}"
    old = "OldReplaceHorse5!"  # noqa: S105
    new = "NewReplaceHorse6!"  # noqa: S105
    engine = manager.create_user_database(username, old)
    assert engine is not None
    db_path = manager._get_user_db_path(username)
    epoch_path = db_path.with_name(f"{db_path.name}.rekey.epoch")
    original_epoch = read_rekey_epoch_state(db_path).committed_epoch
    real_sync_parent = rekey_lock_module._sync_parent_directory

    def fail_after_epoch_replace(path):
        if path == epoch_path:
            raise OSError("simulated directory fsync failure")
        real_sync_parent(path)

    monkeypatch.setattr(
        rekey_lock_module, "_sync_parent_directory", fail_after_epoch_replace
    )
    cleanup = MagicMock(side_effect=OSError("simulated cleanup failure"))
    monkeypatch.setattr(encrypted_db, "clear_pending_rekey_epoch", cleanup)

    # When committed publication raises after os.replace exposes E2
    assert manager.change_password(username, old, new) is True

    # Then success and E2+pending-E2 reflect the real surviving new key
    interrupted = read_rekey_epoch_state(db_path)
    assert interrupted.committed_epoch != original_epoch
    assert interrupted.pending_epoch == interrupted.committed_epoch
    cleanup.assert_called_once_with(db_path, interrupted.pending_epoch)
    with pytest.raises(ValueError):
        manager._make_sqlcipher_connection(db_path, old)
    new_connection = manager._make_sqlcipher_connection(db_path, new)
    new_connection.close()
    assert read_rekey_epoch_state(db_path) == interrupted


def test_change_password_does_not_checkpoint_stale_cache_after_epoch_advance(
    tmp_path,
):
    # Given a cached engine whose epoch advances as password change takes the sidecar
    with patch.object(
        DatabaseManager, "_check_encryption_available", return_value=True
    ):
        manager = DatabaseManager()
    manager.data_dir = tmp_path
    username = "alice"
    db_path = manager._get_user_db_path(username)
    db_path.touch()
    with acquire_rekey_lock(db_path):
        original_epoch = read_or_initialize_rekey_epoch(db_path)
    stale_engine = MagicMock()
    manager._cache_connection(
        username,
        manager._make_connection_cache_entry(
            stale_engine, "old-password", original_epoch
        ),
    )
    old_connection = MagicMock()
    new_connection = MagicMock()

    @contextmanager
    def advance_epoch_before_yield(
        path, *, blocking=False, local_wait=None, contention_retry=0.0
    ):
        with acquire_rekey_lock(
            path,
            blocking=blocking,
            local_wait=local_wait,
            contention_retry=contention_retry,
        ):
            rekey_lock_module.advance_rekey_epoch(path)
            yield

    with (
        patch.object(
            encrypted_db,
            "acquire_rekey_lock",
            advance_epoch_before_yield,
        ),
        patch.object(
            manager,
            "_make_sqlcipher_connection",
            side_effect=[old_connection, new_connection],
        ),
        patch.object(manager, "_require_full_synchronous"),
        patch.object(manager, "_pre_rekey_checkpoint"),
        patch.object(manager, "_verify_rekey_candidate"),
        patch.object(manager, "_checkpoint_wal") as checkpoint,
        patch.object(encrypted_db, "set_sqlcipher_rekey") as rekey,
    ):
        # When the raw password-change path rekeys after acquiring that sidecar
        changed = manager.change_password(
            username, "old-password", "new-password"
        )

    # Then stale cache disposal never opens the old-key engine to checkpoint it
    assert changed is True
    checkpoint.assert_not_called()
    stale_engine.connect.assert_not_called()
    stale_engine.dispose.assert_called_once_with()
    rekey.assert_called_once_with(
        old_connection, "new-password", db_path=db_path
    )
    old_connection.close.assert_called_once_with()
    new_connection.close.assert_called_once_with()


def test_live_reader_busy_checkpoint_does_not_strand_committed_rekey(
    manager, monkeypatch
):
    # Given a real old-key reader that opens its read transaction after the
    # pre-rekey checkpoint, so it pins the WAL through candidate verification
    if not manager.has_encryption:
        pytest.skip("SQLCipher not available; rekey path is a no-op")
    username = f"livereader_{uuid.uuid4().hex[:8]}"
    old = "OldReaderHorse5!"  # noqa: S105
    new = "NewReaderHorse6!"  # noqa: S105
    assert manager.create_user_database(username, old) is not None
    manager.close_user_database(username)
    db_path = manager._get_user_db_path(username)
    real_make = DatabaseManager._make_sqlcipher_connection

    def make_short_busy(path, password, **kwargs):
        # Only shortens the busy wait (db_config.busy_timeout_ms, 30 s by
        # default) of any statement that cannot finish while the reader
        # lives; every SQLCipher call stays real.
        connection = real_make(path, password, **kwargs)
        connection.execute("PRAGMA busy_timeout = 200")
        return connection

    monkeypatch.setattr(manager, "_make_sqlcipher_connection", make_short_busy)
    readers = []
    real_begin = encrypted_db.begin_rekey_epoch

    def begin_with_live_reader(path):
        reader = real_make(path, old, isolation_level=None)
        reader.execute("BEGIN")
        reader.execute("SELECT count(*) FROM sqlite_master").fetchone()
        readers.append(reader)
        return real_begin(path)

    monkeypatch.setattr(
        encrypted_db, "begin_rekey_epoch", begin_with_live_reader
    )
    try:
        # When the password changes while that reader still holds the WAL
        assert manager.change_password(username, old, new) is True

        # Then the new key is committed and active, and the old key is an
        # ordinary wrong password, all while the reader is still alive
        assert len(readers) == 1
        state = read_rekey_epoch_state(db_path)
        assert state.pending_epoch is None
        assert state.committed_epoch is not None
        assert manager.open_user_database(username, old) is None
        assert manager.open_user_database(username, new) is not None
        assert read_rekey_epoch_state(db_path) == state
    finally:
        for reader in readers:
            reader.close()


def test_reader_pinned_before_change_password_neither_refuses_nor_stalls(
    manager, monkeypatch
):
    # Given an old-key reader whose read transaction is already open over a
    # non-empty WAL when the password change starts, and a busy timeout far
    # longer than any rekey step should wait while holding the sidecar gate
    if not manager.has_encryption:
        pytest.skip("SQLCipher not available; rekey path is a no-op")
    busy_timeout_ms = 8000
    monkeypatch.setenv("LDR_DB_CONFIG_BUSY_TIMEOUT_MS", str(busy_timeout_ms))
    username = f"pinnedreader_{uuid.uuid4().hex[:8]}"
    old = "OldPinnedHorse5!"  # noqa: S105
    new = "NewPinnedHorse6!"  # noqa: S105
    assert manager.create_user_database(username, old) is not None
    manager.close_user_database(username)
    db_path = manager._get_user_db_path(username)
    make = DatabaseManager._make_sqlcipher_connection
    writer = make(db_path, old, isolation_level=None)
    reader = make(db_path, old, isolation_level=None)
    try:
        writer.execute("CREATE TABLE pinned_probe (value TEXT)")
        writer.executemany(
            "INSERT INTO pinned_probe VALUES (?)",
            [(f"row-{index}",) for index in range(50)],
        )
        writer.close()
        reader.execute("BEGIN")
        assert reader.execute(
            "SELECT count(*) FROM pinned_probe"
        ).fetchone() == (50,)

        # When the password changes while that reader keeps the WAL pinned
        started = time.monotonic()
        changed = manager.change_password(username, old, new)
        elapsed = time.monotonic() - started

        # Then the rekey commits instead of refusing at the pre-rekey
        # checkpoint, and no checkpoint waited out the full busy timeout
        assert changed is True
        assert elapsed < busy_timeout_ms / 1000 * 0.75
        state = read_rekey_epoch_state(db_path)
        assert state.pending_epoch is None
        assert state.committed_epoch is not None
        assert manager.open_user_database(username, old) is None
        assert manager.open_user_database(username, new) is not None
    finally:
        writer.close()
        reader.close()


def _sqlite_file_names(db_path):
    return {
        db_path.name,
        f"{db_path.name}-wal",
        f"{db_path.name}-shm",
        f"{db_path.name}-journal",
    }


def test_change_password_opens_no_raw_descriptor_on_the_live_database(
    manager, monkeypatch
):
    # Given a connection to the user's database that stays open in this
    # process across the password change (a checked-out session, a pinned
    # reader). Closing ANY descriptor to that file would release every
    # POSIX fcntl lock this process holds on it, SQLite's WAL-mode SHARED
    # lock included, behind SQLite's back (sqlite.org/howtocorrupt.html
    # section 2.2); another process could then delete the WAL under it.
    if not manager.has_encryption:
        pytest.skip("SQLCipher not available; rekey path is a no-op")
    username = f"nofdclose_{uuid.uuid4().hex[:8]}"
    old = "OldDescriptorHorse1!"  # noqa: S105
    new = "NewDescriptorHorse2!"  # noqa: S105
    assert manager.create_user_database(username, old) is not None
    manager.close_user_database(username)
    db_path = manager._get_user_db_path(username)
    guarded = _sqlite_file_names(db_path)
    live = DatabaseManager._make_sqlcipher_connection(
        db_path, old, isolation_level=None
    )
    opened = []
    # Every Python-level way to get a descriptor on a path. ``builtins.open``
    # and ``io.open`` are the same function object but two separate name
    # bindings: patching ``io.open`` alone does NOT intercept a bare
    # ``open(db_path, "r+b")`` (which resolves through ``builtins``), so a
    # regression that reintroduced the raw fsync that way would slip past.
    # ``Path.open`` is hooked explicitly too rather than relying on its
    # current implementation delegating to ``io.open``.
    real_os_open = os.open
    real_io_open = io.open
    real_builtins_open = builtins.open
    real_path_open = Path.open

    def _record(file):
        if not isinstance(file, int):
            opened.append(Path(os.fsdecode(file)).name)

    def recording_os_open(path, *args, **kwargs):
        _record(path)
        return real_os_open(path, *args, **kwargs)

    def recording_io_open(file, *args, **kwargs):
        _record(file)
        return real_io_open(file, *args, **kwargs)

    def recording_builtins_open(file, *args, **kwargs):
        _record(file)
        return real_builtins_open(file, *args, **kwargs)

    def recording_path_open(self, *args, **kwargs):
        _record(self)
        return real_path_open(self, *args, **kwargs)

    try:
        live.execute("SELECT count(*) FROM sqlite_master").fetchone()
        with monkeypatch.context() as patcher:
            patcher.setattr(os, "open", recording_os_open)
            patcher.setattr(io, "open", recording_io_open)
            patcher.setattr(builtins, "open", recording_builtins_open)
            patcher.setattr(Path, "open", recording_path_open)

            # When the password changes while that connection is open
            changed = manager.change_password(username, old, new)
    finally:
        live.close()

    # Then the change succeeded without any Python-level open (and so no
    # close) of the database, its WAL, SHM or journal; SQLite's own
    # syncing under synchronous=FULL provides the durability
    assert changed is True
    assert [name for name in opened if name in guarded] == []
    assert any(name.startswith(f"{db_path.name}.") for name in opened), (
        "the recorder must have seen the sidecar opens, or it hooked nothing"
    )
    assert manager.open_user_database(username, new) is not None


def test_rekey_that_silently_did_not_apply_fails_cleanly_under_old_key(
    manager, monkeypatch
):
    # Given a writer holding a write transaction on the user's database
    # for longer than the busy timeout: SQLCipher's PRAGMA rekey then
    # answers "ok" without raising and leaves the file under the OLD key
    if not manager.has_encryption:
        pytest.skip("SQLCipher not available; rekey path is a no-op")
    monkeypatch.setenv("LDR_DB_CONFIG_BUSY_TIMEOUT_MS", "300")
    username = f"silentrekey_{uuid.uuid4().hex[:8]}"
    old = "OldSilentHorse3!"  # noqa: S105
    new = "NewSilentHorse4!"  # noqa: S105
    assert manager.create_user_database(username, old) is not None
    manager.close_user_database(username)
    db_path = manager._get_user_db_path(username)
    original_epoch = read_rekey_epoch_state(db_path).committed_epoch
    writer = DatabaseManager._make_sqlcipher_connection(
        db_path, old, isolation_level=None
    )
    try:
        writer.execute("CREATE TABLE silent_probe (value TEXT)")
        writer.execute("BEGIN IMMEDIATE")
        writer.execute("INSERT INTO silent_probe VALUES ('held')")

        # When the password change runs into that writer
        with pytest.raises(RekeyUnavailableError) as raised:
            manager.change_password(username, old, new)
    finally:
        writer.rollback()
        writer.close()

    # Then it fails as retryable, not indeterminate: no pending state is
    # left for a login to repair, the old password still opens the
    # database, and the new one (which never took effect) does not
    assert raised.value.stage is RekeyStage.PHYSICAL_REKEY
    state = read_rekey_epoch_state(db_path)
    assert state.pending_epoch is None
    assert state.committed_epoch is not None
    assert state.committed_epoch != original_epoch
    assert manager.open_user_database(username, new) is None
    assert manager.open_user_database(username, old) is not None


def test_integrity_check_after_busy_checkpoint_rekey_is_not_corrupted(
    manager, monkeypatch
):
    # Given a password change whose post-rekey checkpoint a live old-key
    # reader kept busy, so the WAL holds new-key frames the main file lacks
    if not manager.has_encryption:
        pytest.skip("SQLCipher not available; rekey path is a no-op")
    username = f"integbusy_{uuid.uuid4().hex[:8]}"
    old = "OldIntegrityHorse7!"  # noqa: S105
    new = "NewIntegrityHorse8!"  # noqa: S105
    assert manager.create_user_database(username, old) is not None
    manager.close_user_database(username)
    real_make = DatabaseManager._make_sqlcipher_connection

    def make_short_busy(path, password, **kwargs):
        connection = real_make(path, password, **kwargs)
        connection.execute("PRAGMA busy_timeout = 200")
        return connection

    monkeypatch.setattr(manager, "_make_sqlcipher_connection", make_short_busy)
    readers = []
    real_begin = encrypted_db.begin_rekey_epoch

    def begin_with_live_reader(path):
        reader = real_make(path, old, isolation_level=None)
        reader.execute("BEGIN")
        reader.execute("SELECT count(*) FROM sqlite_master").fetchone()
        readers.append(reader)
        return real_begin(path)

    monkeypatch.setattr(
        encrypted_db, "begin_rekey_epoch", begin_with_live_reader
    )
    try:
        assert manager.change_password(username, old, new) is True
        assert manager.open_user_database(username, new) is not None

        # When the integrity check runs while the reader still pins the WAL
        healthy = manager.check_database_integrity(username)
    finally:
        for reader in readers:
            reader.close()

    # Then the un-checkpointed new-key frames are not reported as corruption
    assert len(readers) == 1
    assert healthy is True
    # And once the reader is gone the full check (cipher scan included)
    # still passes on the same engine
    assert manager.check_database_integrity(username) is True


def test_busy_verification_during_pending_recovery_is_not_a_wrong_password(
    manager, monkeypatch
):
    # Given a rollback-journal database left in pending-rekey state, and
    # another connection holding its EXCLUSIVE lock as the user logs in
    if not manager.has_encryption:
        pytest.skip("SQLCipher not available; rekey path is a no-op")
    monkeypatch.setenv("LDR_DB_CONFIG_JOURNAL_MODE", "DELETE")
    username = f"busypend_{uuid.uuid4().hex[:8]}"
    password = "PendingBusyHorse9!"  # noqa: S105
    assert manager.create_user_database(username, password) is not None
    manager.close_user_database(username)
    db_path = manager._get_user_db_path(username)
    with acquire_rekey_lock(db_path):
        read_or_initialize_rekey_epoch(db_path)
        pending_epoch = rekey_lock_module.begin_rekey_epoch(db_path)
    holder = DatabaseManager._make_sqlcipher_connection(
        db_path, password, isolation_level=None
    )
    holder.execute("BEGIN EXCLUSIVE")

    # The driver's own default lock wait would outlast the hold below; drop
    # it to zero so only a busy timeout set before verification waits.
    real_module = encrypted_db.get_sqlcipher_module()

    class _NoDefaultLockWait:
        def __getattr__(self, name):
            return getattr(real_module, name)

        @staticmethod
        def connect(*args, **kwargs):
            return real_module.connect(*args, timeout=0, **kwargs)

    monkeypatch.setattr(
        encrypted_db, "get_sqlcipher_module", lambda: _NoDefaultLockWait()
    )
    verifying = threading.Event()
    real_verify = encrypted_db.check_sqlcipher_key

    def verify_marking_start(cursor):
        verifying.set()
        return real_verify(cursor)

    monkeypatch.setattr(
        encrypted_db, "check_sqlcipher_key", verify_marking_start
    )

    def release_once_verification_waits():
        if verifying.wait(timeout=30):
            # Hold the real lock while the verification read waits on it
            # (SQLite's busy handler runs on wall time; no clock to fake).
            time.sleep(0.5)  # allow: unmarked-sleep
        holder.execute("COMMIT")

    releaser = threading.Thread(target=release_once_verification_waits)
    releaser.start()
    try:
        # When the CORRECT password logs in while the lock is still held
        engine = manager.open_user_database(username, password)
    finally:
        releaser.join(timeout=30)
        holder.close()

    # Then the lock is waited out rather than read as a wrong password, and
    # the pending generation is finalized
    assert verifying.is_set()
    assert engine is not None
    state = read_rekey_epoch_state(db_path)
    assert state.pending_epoch is None
    assert state.committed_epoch == pending_epoch