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.

50.0 KB
Raw Blame
"""change_password rekey racing concurrent open_user_database(old_password).

Incident context (PR #5596): ``change_password`` used to cold-open the
database with the old password, cache that engine with a verifier for the
old password, rekey the file, and evict the engine only afterwards. Between
the rekey and the eviction a concurrent ``open_user_database(old_password)``
passed the verifier check and was handed the stale-key engine.

The durable rekey epoch closes that window structurally. ``change_password``
holds the user's on-disk rekey sidecar lock for its whole staged sequence and
detaches any cached engine BEFORE the rekey; every cold open and every cache
claim takes the same lock, and a cached generation is only handed out while
its recorded epoch is the committed durable epoch. #6565's in-process
generation counter and post-rekey conditional eviction
(``_get_rekey_epoch`` / ``_bump_rekey_epoch`` /
``_cache_connection(expected_epoch=...)`` / ``close_user_database_if_stale``)
are superseded by that fence; the tests that pinned them below now pin the
stronger behaviour, and each docstring says what changed.

``test_change_password_evicts_stale_engine.py`` pins the ordering (cache
empty at the moment of the rekey) via monkeypatched spies;
``test_login_cached_connection_password_extra.py::
test_password_change_invalidates_old_verifier_and_arms_new`` pins the
sequential after-state. This module proves the end-to-end concurrent
properties against a real SQLCipher file:

* threads calling open_user_database(old) before the rekey starts may
  receive engines (the old key is still valid then), but every such engine
  must be DEAD once the rekey completes: it must never yield a queryable
  session afterward.
* AFTER change_password returns: the old key must fail (and cache nothing),
  the new key must open and read data written under previous passwords.
* Under a stress loop of chained rekeys, no snapshot of the manager may
  ever show an engine without its verifier or vice versa (atomic
  publication), and no stale password may ever produce a usable engine.

All tests use the real DatabaseManager with real SQLCipher files under
tmp_path; no mocks on the crypto or cache layer.
"""

import threading
import time
import uuid
import warnings

import pytest
from sqlalchemy import text

from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.rekey_lock import read_rekey_epoch

# Module-level gate. Without a working SQLCipher build every test below
# is a no-op, and the dozens of in-body ``pytest.skip`` calls let a CI
# lane report this whole battery green having executed nothing. Skipping
# at import time makes the missing dependency one visible, uniform signal
# per module instead.
pytest.importorskip("sqlcipher3", reason="requires SQLCipher (encrypted mode)")

# Bounded concurrency: explicit thread counts, explicit iteration caps, a
# barrier for synchronized starts and a wall-clock budget well under 30s.
MAX_HAMMER_ITERATIONS = 25
JOIN_TIMEOUT_S = 60
BARRIER_TIMEOUT_S = 30
# Race witness floor: the hammer thread must have made at least this many
# open attempts. Below it the run raced nothing and the stale-engine
# assertions are vacuous, so it is a hard failure rather than a silent pass.
MIN_HAMMER_ATTEMPTS = 1


def _join_all(threads):
    """Join every thread against ONE shared deadline.

    A per-thread ``join(timeout=JOIN_TIMEOUT_S)`` inside a loop costs
    ``len(threads) * JOIN_TIMEOUT_S`` in the worst case. ``pyproject.toml``
    sets ``timeout = 180`` with ``timeout_method = "thread"``, and
    pytest-timeout's thread handler ends in ``os._exit(1)`` -- so when these
    tests actually find the deadlock they exist to find, a per-thread budget
    would kill the whole xdist worker (losing its coverage, then tripping
    ``--cov-fail-under``) instead of failing on the named "deadlocked"
    assertion at the call site. One shared deadline bounds the entire join at
    JOIN_TIMEOUT_S regardless of thread count, which keeps that assertion
    reachable well inside the global timeout. Each thread still gets the full
    remaining budget in wall-clock terms, because the threads run
    concurrently.
    """
    deadline = time.monotonic() + JOIN_TIMEOUT_S
    for t in threads:
        t.join(timeout=max(0.0, deadline - time.monotonic()))


@pytest.fixture
def manager(tmp_path, monkeypatch):
    """A DatabaseManager writing to an isolated data directory.

    Same self-contained pattern as the incident tests; KDF iterations are
    lowered via the sanctioned test-mode knob (see tests/conftest.py's
    ``app`` fixture) because the stress tests below perform dozens of key
    derivations (one per open/rekey attempt).
    """
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    mgr = DatabaseManager()
    mgr.data_dir = tmp_path
    yield mgr
    mgr.close_all_databases()


def _write_canary(engine, note: str) -> None:
    """Write a marker row through a real engine so rekey/data-survival is
    asserted on actual readable data, not on engine non-None-ness."""
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE IF NOT EXISTS gap_b_canary "
                "(id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_b_canary (id, note) VALUES (1, :note)"),
            {"note": note},
        )


def _engine_usable(engine) -> tuple[bool, str]:
    """Is this engine able to read encrypted pages right now?

    Returns (usable, detail); detail carries the failure repr so a failed
    invariant prints WHY the engine was (or was not) usable.

    Must read a real table, not ``SELECT 1``: under SQLCipher ``SELECT 1``
    touches no encrypted pages and succeeds even on a mis-keyed engine
    whose first table read raises ("file is not a database"). Probing only
    ``SELECT 1`` therefore measures pool liveness, not key validity. Reading
    ``sqlite_master`` forces a decrypt of schema pages, so a stale-key engine
    fails here as it would on any data read.
    """
    try:
        with engine.connect() as conn:
            conn.execute(text("SELECT name FROM sqlite_master LIMIT 1")).all()
        return True, "table read ok"
    except Exception as exc:  # noqa: BLE001 - converted into a loud assert below
        return False, repr(exc)


def test_post_change_password_old_key_dead_new_key_reads_data(manager):
    """Sequential baseline at real-DB level: after change_password, the old
    key is dead (and its failed attempt caches NOTHING), the new key opens
    and reads the canary row written under the old key (rekey preserved
    data), and no cache pollution is left behind.

    Goes beyond test_password_change_invalidates_old_verifier_and_arms_new
    (which checks open-None/engine-identity only) by asserting data
    readability through a real session and the no-residue property of the
    failed old-key open.
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"chgpw_seq_{uuid.uuid4().hex[:8]}"
    old = "OldCorrectHorse1!"  # noqa: S105
    new = "NewCorrectHorse2!"  # noqa: S105
    engine = manager.create_user_database(username, old)
    _write_canary(engine, "gap-B-seq")
    manager.close_user_database(username)

    assert manager.change_password(username, old, new) is True

    # change_password evicted everything; nothing half-cached.
    assert username not in manager.connections
    assert username not in manager._password_verifiers

    # Old key: rejected, and the rejection must not populate any cache
    # entry (a cached engine or verifier from a FAILED open would arm a
    # future bypass).
    assert manager.open_user_database(username, old) is None, (
        "the old password must not open the re-keyed database"
    )
    assert username not in manager.connections, (
        "a failed old-password open must not cache an engine"
    )
    assert username not in manager._password_verifiers, (
        "a failed old-password open must not arm a verifier"
    )

    # New key: opens, and the data written under the old key is readable.
    reopened = manager.open_user_database(username, new)
    assert reopened is not None
    with reopened.connect() as conn:
        note = conn.execute(
            text("SELECT note FROM gap_b_canary WHERE id = 1")
        ).scalar()
    assert note == "gap-B-seq", "rekey must preserve existing data"
    assert manager._password_matches_cached(username, new) is True


def test_old_password_open_racing_change_password_never_usable_after_rekey(
    manager,
):
    """The single-shot race: one thread runs change_password(old -> new)
    while another hammers open_user_database(old).

    Any engine the old-password thread manages to grab (legal only before
    the rekey lands -- the old key is genuinely valid until then) must be
    DEAD once change_password returns: a SELECT on it must fail.

    STRICTLY WEAKER than the spy test it complements, and deliberately so.
    Move ``change_password``'s cache detach from before the rekey to after
    it, and every assertion here still passes: the grabbed engine is dead
    after the rekey either way, because the rekey invalidated the key its
    creator closure derives. That is a property of SQLCipher rekey, not of
    where the detach sits.

    What this test does pin, under real threads rather than spies: no
    engine handed out under the OLD password survives a completed
    change_password, the old password no longer opens afterwards, and the
    new one reads the pre-change data. The ORDERING of the detach (cache
    empty at the moment of the rekey) is pinned by
    ``tests/security/test_change_password_evicts_stale_engine.py``.
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"chgpw_race_{uuid.uuid4().hex[:8]}"
    old = "OldCorrectHorse1!"  # noqa: S105
    new = "NewCorrectHorse2!"  # noqa: S105
    engine = manager.create_user_database(username, old)
    _write_canary(engine, "gap-B-race")
    manager.close_user_database(username)

    barrier = threading.Barrier(2)
    done = threading.Event()
    outcome = {}
    grabbed_engines = []
    errors = {}

    def rekeyer():
        try:
            barrier.wait(timeout=BARRIER_TIMEOUT_S)
            outcome["changed"] = manager.change_password(username, old, new)
        except Exception as exc:  # noqa: BLE001 - surfaced via `errors` below
            errors["rekeyer"] = exc
        finally:
            done.set()

    def old_password_hammer():
        try:
            barrier.wait(timeout=BARRIER_TIMEOUT_S)
            attempts = 0
            while not done.is_set() and attempts < MAX_HAMMER_ITERATIONS:
                attempts += 1
                engine = manager.open_user_database(username, old)
                if engine is not None:
                    grabbed_engines.append(engine)
                time.sleep(0.005)
            outcome["attempts"] = attempts
        except Exception as exc:  # noqa: BLE001 - surfaced via `errors` below
            errors["hammer"] = exc

    threads = [
        threading.Thread(target=rekeyer, name="gap-b-rekeyer"),
        threading.Thread(target=old_password_hammer, name="gap-b-hammer-old"),
    ]
    for t in threads:
        t.start()
    _join_all(threads)
    assert not any(t.is_alive() for t in threads), (
        "change_password / open_user_database deadlocked under the race"
    )
    assert not errors, f"a racing thread raised: {errors}"
    assert outcome.get("changed") is True, f"change_password outcome: {outcome}"

    # Race witness. Without it a scheduler that never gave the hammer a
    # slice leaves `grabbed_engines` empty and the core loop below asserts
    # nothing, while the test still reports green.
    attempts = outcome.get("attempts", 0)
    assert attempts >= MIN_HAMMER_ATTEMPTS, (
        f"the old-password hammer made {attempts} attempt(s) (minimum "
        f"{MIN_HAMMER_ATTEMPTS}) -- nothing was raced on this runner"
    )
    if not grabbed_engines:
        warnings.warn(
            f"stale-engine invariant not exercised: all {attempts} "
            "old-password attempts lost the pre-rekey window, so no engine "
            "was grabbed to check. The post-conditions below still ran.",
            stacklevel=1,
        )

    # Core invariant: every engine handed to the OLD password during the
    # race must be unusable now that the rekey completed.
    for index, engine in enumerate(grabbed_engines):
        usable, detail = _engine_usable(engine)
        assert not usable, (
            f"engine #{index} grabbed with the OLD password is still "
            f"usable after the rekey completed ({detail}) -- a stale-key "
            "engine escaped the eviction"
        )

    # Post-conditions: old dead, new alive and reading pre-change data.
    assert manager.open_user_database(username, old) is None
    reopened = manager.open_user_database(username, new)
    assert reopened is not None
    with reopened.connect() as conn:
        note = conn.execute(
            text("SELECT note FROM gap_b_canary WHERE id = 1")
        ).scalar()
    assert note == "gap-B-race"


def test_stress_change_password_chain_vs_open_hammers(manager):
    """Chained rekeys (pw0 -> pw1 -> ... -> pw4) racing two open-hammer
    threads: one stuck on the ORIGINAL password, one on the FINAL password.

    Invariants after stabilization:
    * every engine obtained with the ORIGINAL password during the run is
      dead (its key was rekeyed away and can never return);
    * every engine obtained with the FINAL password is keyed with the
      final password: disposed first (to force a fresh connection through
      its creator, where SQLCipher key verification runs), it still opens
      and reads the canary. Engine identity is NOT the invariant, the key
      is: any eviction between two grabs (a logout, the idle pool reset)
      legitimately makes a later open cache a second final-password
      engine;
    * no intermediate or original password can open at the end;
    * the final password opens and reads the canary written at creation;
    * the engine/verifier PAIRING invariant, sampled under the connections
      lock after each change: the set of usernames in ``connections``
      always equals the set in ``_password_verifiers``. Either map CAN
      legitimately be (re-)populated between change_password's detach
      and the snapshot by a concurrent open_user_database republishing
      engine+verifier atomically via _cache_connection -- that is not a
      publication gap. The engine-without-verifier / verifier-without-
      engine state the product guarantees cannot occur is what is
      asserted (same invariant as round 1's gap-C storm test).
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"chgpw_stress_{uuid.uuid4().hex[:8]}"
    passwords = [f"StressPass{i}!A" for i in range(5)]  # noqa: S105
    final_password = passwords[-1]
    engine = manager.create_user_database(username, passwords[0])
    _write_canary(engine, "gap-B-stress")
    manager.close_user_database(username)

    barrier = threading.Barrier(3)  # rekeyer + 2 hammers
    done = threading.Event()
    errors = {}
    grabbed = []  # (password_label, engine) pairs recorded by hammers
    grabbed_lock = threading.Lock()
    pair_violations = []  # engine/verifier pairing snapshots that broke
    hammer_attempts = {}  # race witness: per-hammer open attempt counts

    def rekeyer():
        try:
            barrier.wait(timeout=BARRIER_TIMEOUT_S)
            for i in range(len(passwords) - 1):
                changed = manager.change_password(
                    username, passwords[i], passwords[i + 1]
                )
                assert changed is True, f"change_password #{i} failed mid-chain"
                # Pairing-invariant sample: after a change returns, the
                # username sets of connections and _password_verifiers
                # must agree. A hammer thread may have legitimately
                # republished BOTH maps via _cache_connection (atomic
                # under _connections_lock), so emptiness is NOT the
                # invariant -- only the pairing is. Both maps are mutated
                # only under _connections_lock, so this snapshot cannot
                # observe a torn state.
                with manager._connections_lock:
                    engines = set(manager.connections)
                    verifiers = set(manager._password_verifiers)
                if engines != verifiers:
                    pair_violations.append((i, engines, verifiers))
        except Exception as exc:  # noqa: BLE001 - surfaced via `errors` below
            errors["rekeyer"] = exc
        finally:
            done.set()

    def hammer(label: str, password: str):
        try:
            barrier.wait(timeout=BARRIER_TIMEOUT_S)
            attempts = 0
            while not done.is_set() and attempts < MAX_HAMMER_ITERATIONS:
                attempts += 1
                engine = manager.open_user_database(username, password)
                if engine is not None:
                    with grabbed_lock:
                        grabbed.append((label, engine))
                time.sleep(0.005)
            hammer_attempts[label] = attempts
        except Exception as exc:  # noqa: BLE001 - surfaced via `errors` below
            errors[f"hammer-{label}"] = exc

    threads = [
        threading.Thread(target=rekeyer, name="gap-b-chain"),
        threading.Thread(
            target=hammer, args=("original", passwords[0]), name="gap-b-h0"
        ),
        threading.Thread(
            target=hammer, args=("final", final_password), name="gap-b-hf"
        ),
    ]
    for t in threads:
        t.start()
    _join_all(threads)
    assert not any(t.is_alive() for t in threads), "stress race deadlocked"
    assert not errors, f"a stress thread raised: {errors}"
    assert not pair_violations, (
        f"engine/verifier pairing broken: {pair_violations}"
    )

    # Race witness: both hammers must have run. Without it a scheduler that
    # starves them leaves `grabbed` empty and the engine-verdict loop below
    # iterates over nothing while the test reports green.
    assert set(hammer_attempts) == {"original", "final"}, (
        f"a hammer thread recorded no attempts: {hammer_attempts}"
    )
    assert all(n >= MIN_HAMMER_ATTEMPTS for n in hammer_attempts.values()), (
        f"hammer attempt counts below the {MIN_HAMMER_ATTEMPTS} floor: "
        f"{hammer_attempts} -- nothing was raced on this runner"
    )
    if not grabbed:
        warnings.warn(
            "engine-verdict loop not exercised: neither hammer grabbed an "
            f"engine across {hammer_attempts} attempts, so no stale/mis-keyed "
            "engine verdict was checked. The end-state assertions still ran.",
            stacklevel=1,
        )

    # Stabilized end-state, part 1: every STALE password (original + all
    # intermediates) is dead from the manager's point of view.
    for stale in passwords[:-1]:
        assert manager.open_user_database(username, stale) is None, (
            f"stale password {stale!r} still opens after the rekey chain"
        )

    # Stabilized end-state, part 2: the final password opens and reads the
    # data written before any rekey happened. The engine it gets back is
    # whichever final-password engine is currently cached (or, if no
    # hammer grabbed one after the last rekey, the freshly cold-opened one
    # we just created) -- cache identity, not "the surviving engine": the
    # verdict loop below allows more than one final-password engine to be
    # legitimate.
    reopened = manager.open_user_database(username, final_password)
    assert reopened is not None
    surviving = manager.connections.get(username)
    assert surviving is reopened, "the final-password engine must be cached"
    # Dispose first so the read goes through the engine's creator, where the
    # key is applied: a pooled connection that was rekeyed in place would
    # otherwise read the canary even if the engine's creator derives an
    # earlier password.
    reopened.dispose()
    with reopened.connect() as conn:
        note = conn.execute(
            text("SELECT note FROM gap_b_canary WHERE id = 1")
        ).scalar()
    assert note == "gap-B-stress", "rekey chain must preserve data"

    # Engine verdicts, computed AFTER stabilization so the file's key is
    # settled on the final password:
    #   * 'original': every engine handed out for the ORIGINAL password
    #     must be dead -- its key was rekeyed away and can never return.
    #   * 'final': every engine handed out for the FINAL password must be
    #     keyed with the final password. Each is disposed first, forcing a
    #     fresh connection through its creator -- where SQLCipher key
    #     verification actually runs -- and only then must open and read
    #     the canary. Without the dispose, a stale pooled connection could
    #     pass both probes vacuously: SELECT 1 never touches an encrypted
    #     page, and an engine rekeyed in place keeps a live connection
    #     that already carries the new key even though the engine's own
    #     creator still derives an earlier password. Several distinct
    #     final-password engines can legitimately exist: any eviction
    #     between two grabs (a logout, the idle pool reset) makes a later
    #     open cache a new one. Each creator still derives the final key,
    #     so none is mis-keyed.
    #     Once disposed, an engine keyed with any earlier password fails
    #     SQLCipher verification against the rekeyed file and trips this.
    #     (A final-password open cannot succeed before the final rekey: the
    #     file is keyed with an earlier password and verification fails.)
    for index, (label, engine) in enumerate(grabbed):
        if label == "original":
            usable, detail = _engine_usable(engine)
            assert not usable, (
                f"engine #{index} (hammer={label}) is still usable after "
                f"the rekey chain completed ({detail}) -- a stale-key "
                "engine escaped the eviction"
            )
        else:
            # Force a new connection through the engine's creator closure
            # -- where SQLCipher key verification runs -- before probing
            # it. Without this, an engine rekeyed in place (its live
            # pooled connection carries the new key even though its
            # creator still derives an earlier one) would pass both
            # probes on the stale connection.
            engine.dispose()
            usable, detail = _engine_usable(engine)
            assert usable, (
                f"engine #{index} (hammer={label}) cannot connect with its "
                f"key after the rekey chain completed ({detail}) -- a "
                "mis-keyed engine was handed out for the final password"
            )
            with engine.connect() as conn:
                note = conn.execute(
                    text("SELECT note FROM gap_b_canary WHERE id = 1")
                ).scalar()
            assert note == "gap-B-stress", (
                f"engine #{index} (hammer={label}) does not read the canary "
                f"({note!r}) -- a mis-keyed engine was handed out for the "
                "final password"
            )


def test_change_password_does_not_evict_concurrent_new_password_engine(
    manager,
):
    """A new-password engine published after the rekey is never evicted.

    #6565 pinned this through ``close_user_database_if_stale``, the
    post-rekey conditional eviction that let an engine with the NEW
    verifier survive while dropping one with the OLD verifier. The durable
    rekey epoch supersedes that helper and it no longer exists:
    ``change_password`` detaches the cached engine BEFORE the rekey while
    holding the sidecar lock every cold open also takes, and runs no
    eviction afterwards, so nothing it does can clobber a new-password
    engine published once it releases the sidecar. An engine with the OLD
    verifier can only exist under a superseded epoch, and the cache claim
    detaches it instead of handing it out. This test pins both halves of
    the old contract against the stronger mechanism: the new-password
    engine survives later opens (cache hits and a failed old-password
    cold open alike), and a stale old-verifier engine planted under the
    pre-rekey epoch is never returned and is detached on its first claim.
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"chgpw_conc_{uuid.uuid4().hex[:8]}"
    old = "OldCorrectHorse1!"  # noqa: S105
    new = "NewCorrectHorse2!"  # noqa: S105
    engine = manager.create_user_database(username, old)
    _write_canary(engine, "gap-B-cond")
    manager.close_user_database(username)
    db_path = manager._get_user_db_path(username)
    pre_rekey_epoch = read_rekey_epoch(db_path)
    assert pre_rekey_epoch is not None

    assert manager.change_password(username, old, new) is True
    assert username not in manager.connections
    assert read_rekey_epoch(db_path) not in (None, pre_rekey_epoch)

    # The new-password engine a concurrent open would publish once the
    # sidecar is released.
    fresh = manager.open_user_database(username, new)
    assert fresh is not None
    assert manager.connections.get(username) is fresh

    # Neither a cache hit nor a failed old-password cold open evicts it.
    assert manager.open_user_database(username, new) is fresh
    assert manager.open_user_database(username, old) is None
    assert manager.connections.get(username) is fresh, (
        "a failed old-password open must leave the surviving new-password "
        "engine cached"
    )
    assert manager._password_matches_cached(username, new) is True

    # Symmetric half: an engine carrying the OLD verifier under the
    # pre-rekey epoch (what a stale publish would plant) is never handed
    # out; its first claim detaches it.
    from sqlalchemy import create_engine

    stale = create_engine("sqlite://")
    try:
        replaced = manager._cache_connection(
            username,
            manager._make_connection_cache_entry(stale, old, pre_rekey_epoch),
        )
        assert replaced is fresh
        replaced.dispose()
        assert manager.open_user_database(username, old) is None, (
            "an old-verifier engine cached under a superseded epoch must "
            "never be returned to the old password"
        )
        assert username not in manager.connections, (
            "the stale generation must be detached on its first claim"
        )
        assert username not in manager._password_verifiers
        assert username not in manager._connection_epochs
    finally:
        stale.dispose()


def test_change_password_keeps_engine_published_after_sidecar_release(
    manager, monkeypatch
):
    """Nothing change_password runs after releasing the sidecar evicts.

    The test above publishes the new-password engine only after
    ``change_password`` has returned, so an eviction added to the tail of
    ``change_password`` (after the sidecar ``with`` block, or in its
    ``finally``) would still pass it. Here the racing
    ``open_user_database(new)`` runs, on another thread, at the exact
    moment ``change_password`` releases the rekey sidecar, while
    ``change_password`` itself is still on the stack. The engine it
    publishes must still be cached once ``change_password`` returns.
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    import contextlib

    from local_deep_research.database import encrypted_db as encrypted_db_mod

    username = f"chgpw_tail_{uuid.uuid4().hex[:8]}"
    old = "OldCorrectHorse1!"  # noqa: S105
    new = "NewCorrectHorse2!"  # noqa: S105
    engine = manager.create_user_database(username, old)
    _write_canary(engine, "gap-B-tail")
    manager.close_user_database(username)

    real_acquire = encrypted_db_mod.acquire_rekey_lock
    published = {}

    def _race_open_new():
        published["engine"] = manager.open_user_database(username, new)

    @contextlib.contextmanager
    def _acquire_then_race(*args, **kwargs):
        with real_acquire(*args, **kwargs) as handle:
            yield handle
        # Only change_password passes contention_retry; every open and
        # disposal path acquires without it.
        if "contention_retry" in kwargs and "engine" not in published:
            racer = threading.Thread(target=_race_open_new)
            racer.start()
            racer.join(timeout=JOIN_TIMEOUT_S)
            assert not racer.is_alive(), "racing open deadlocked"

    monkeypatch.setattr(
        encrypted_db_mod, "acquire_rekey_lock", _acquire_then_race
    )

    assert manager.change_password(username, old, new) is True

    fresh = published.get("engine")
    assert fresh is not None, (
        "premise: the racing new-password open must have published an "
        "engine while change_password was still running"
    )
    assert manager.connections.get(username) is fresh, (
        "change_password evicted the new-password engine a concurrent "
        "open published after the sidecar was released"
    )
    usable, detail = _engine_usable(fresh)
    assert usable, detail
    assert manager.open_user_database(username, new) is fresh


def test_cache_connection_disposes_overwritten_engine(manager, monkeypatch):
    """The engine a publish overwrites is disposed exactly once.

    #6565 disposed the orphan inside ``_cache_connection`` under
    ``_connections_lock``. With the durable epoch, ``_cache_connection``
    publishes ``(engine, verifier, epoch)`` atomically and RETURNS the
    overwritten engine instead (``None`` for a first publish or a
    same-identity republish), and every caller disposes it outside
    ``_connections_lock`` through ``_dispose_detached_connection`` (a WAL
    checkpoint plus ``dispose``). The leak #6565 fixed stays fixed; the
    dispose just moves to the caller so a slow checkpoint never runs under
    the global cache lock. This pins the primitive's return contract and,
    through the real ``open_user_database`` publish, that the orphan is
    disposed exactly once and the published engine is not disposed.
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    from sqlalchemy import create_engine
    from sqlalchemy.engine import Engine as _Engine

    import local_deep_research.database.initialize as _init_module

    username = f"chgpw_orphan_{uuid.uuid4().hex[:8]}"
    old = "OldCorrectHorse1!"  # noqa: S105
    bootstrap = manager.create_user_database(username, old)
    assert bootstrap is not None
    manager.close_user_database(username)
    epoch = read_rekey_epoch(manager._get_user_db_path(username))
    assert epoch is not None

    # Primitive contract.
    first = create_engine("sqlite://")
    second = create_engine("sqlite://")
    try:
        entry = manager._make_connection_cache_entry(first, old, epoch)
        assert manager._cache_connection(username, entry) is None, (
            "a first publish has nothing to hand back for disposal"
        )
        assert manager._cache_connection(username, entry) is None, (
            "a same-identity republish must not hand back the live engine"
        )
        assert (
            manager._cache_connection(
                username,
                manager._make_connection_cache_entry(second, old, epoch),
            )
            is first
        ), "an overwrite must hand back the engine it displaced"
        assert manager.connections[username] is second
    finally:
        manager._detach_connection(username)
        first.dispose()
        second.dispose()

    # Real caller: an engine planted while the cold open runs is the one
    # its publish overwrites.
    real_dispose = _Engine.dispose
    dispose_calls: list[int] = []

    def spy_dispose(self):
        dispose_calls.append(id(self))
        return real_dispose(self)

    monkeypatch.setattr(_Engine, "dispose", spy_dispose)
    planted = create_engine("sqlite://")
    real_initialize = _init_module.initialize_database

    planted_once = []

    def plant_then_initialize(engine):
        if not planted_once:
            planted_once.append(True)
            manager._cache_connection(
                username,
                manager._make_connection_cache_entry(planted, old, epoch),
            )
        return real_initialize(engine)

    monkeypatch.setattr(
        _init_module, "initialize_database", plant_then_initialize
    )
    published = manager.open_user_database(username, old)
    assert published is not None
    assert manager.connections[username] is published
    assert dispose_calls.count(id(planted)) == 1, (
        "open_user_database must dispose the engine its publish displaced "
        f"exactly once (saw {dispose_calls.count(id(planted))})"
    )
    assert id(published) not in dispose_calls, (
        "the published engine itself must not be disposed"
    )


@pytest.mark.parametrize(
    "corrupt_entry",
    ["not-a-tuple", ("x", "y"), (b"salt", 123)],
    ids=["non-tuple", "str-members", "int-digest"],
)
def test_verifier_matches_degrades_on_corrupt_entry(manager, corrupt_entry):
    """Pin the malformed-verifier-entry safety net on every verifier check.

    Two code paths compare a password against a cached verifier:
    ``_verifier_matches`` (the cold-open path's ``_cached_engine_trusted``,
    under the sidecar lock) and ``_connection_generation_password_matches``
    (the cache-hit claim behind ``open_user_database``'s fast path and
    ``_password_matches_cached``). Both go through
    ``_verifier_entry_matches``, so a corrupt entry degrades to no-match on
    both instead of raising ``TypeError`` / ``ValueError`` out of a login.
    The invariants that produce well-formed entries are unit-locked, so a
    corrupt entry can only arrive through external dict mutation; the
    no-match falls through to a real key-validating cold open, which
    republishes a well-formed verifier.

    (#6565 introduced the guard for ``_verifier_matches`` because its
    post-rekey conditional close called it from ``change_password``'s
    ``finally``. That close no longer exists, ``change_password`` detaches
    the cache without consulting the verifier, and the guard now matters
    for the two open paths above; the generation path had no guard and
    raised before this test covered it.)
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"chgpw_corrupt_{uuid.uuid4().hex[:8]}"
    old = "OldCorrectHorse1!"  # noqa: S105
    new = "NewCorrectHorse2!"  # noqa: S105
    engine = manager.create_user_database(username, old)
    _write_canary(engine, "gap-B-corrupt")
    manager.close_user_database(username)

    published = manager.open_user_database(username, old)
    assert published is not None
    with manager._connections_lock:
        manager._password_verifiers[username] = corrupt_entry  # type: ignore[assignment]

    # The lock-held helper degrades to no-match.
    with manager._connections_lock:
        assert manager._verifier_matches(username, old) is False
    # So does the generation claim path.
    assert manager._password_matches_cached(username, old) is False

    # A login with the correct password falls through to a key-validating
    # cold open instead of raising, and republishes a well-formed verifier.
    reopened = manager.open_user_database(username, old)
    assert reopened is not None
    assert reopened is not published
    assert manager.connections.get(username) is reopened
    assert manager._password_matches_cached(username, old) is True

    # change_password detaches the cache without consulting the verifier,
    # so a corrupt entry cannot mask its outcome either.
    with manager._connections_lock:
        manager._password_verifiers[username] = corrupt_entry  # type: ignore[assignment]
    assert manager.change_password(username, old, new) is True
    assert username not in manager._password_verifiers
    assert manager.open_user_database(username, new) is not None


def test_change_password_eviction_window_handles_concurrent_publish_deterministically(
    manager, monkeypatch
):
    """A new-password open racing the rekey waits for it, then publishes.

    #6565 pinned this window by staging its outcome by hand (rekeying the
    file outside ``change_password``, publishing a new-password engine on
    top of the stale one) and checking that its post-rekey conditional
    close left the racing engine alone. The durable rekey epoch removes the
    window instead of arbitrating it: ``change_password`` holds the user's
    sidecar lock across detach, rekey and epoch commit, and every cold open
    takes the same lock, so a racing ``open_user_database(new)`` cannot
    run against the half-rekeyed file at all. It blocks until the rekey
    commits, then opens against the new key and publishes the only engine.

    Deterministic, not a stress loop: the racing open is started from
    inside the rekey itself and must still be blocked after a bounded
    wait (a fenced open is ALWAYS blocked there, because the sidecar is
    held, so this cannot flake under the correct code). Without the fence
    it would cold-open against the still-old-keyed file and return None,
    which the final assertions catch.
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    from local_deep_research.database import encrypted_db

    username = f"chgpw_det_{uuid.uuid4().hex[:8]}"
    old = "OldCorrectHorse1!"  # noqa: S105
    new = "NewCorrectHorse2!"  # noqa: S105
    bootstrap = manager.create_user_database(username, old)
    _write_canary(bootstrap, "gap-B-det")
    manager.close_user_database(username)
    del bootstrap

    racing_result = {}
    racing_errors = {}

    def racing_open():
        try:
            racing_result["engine"] = manager.open_user_database(username, new)
        except Exception as exc:  # noqa: BLE001 - surfaced below
            racing_errors["open"] = exc

    racer = threading.Thread(target=racing_open, name="gap-b-det-racer")
    observed = {}
    real_rekey = encrypted_db.set_sqlcipher_rekey

    def rekey_with_racing_open(conn, new_password, **kwargs):
        observed["cache_at_rekey"] = username in manager.connections
        racer.start()
        racer.join(timeout=0.3)
        observed["racer_blocked"] = racer.is_alive()
        return real_rekey(conn, new_password, **kwargs)

    monkeypatch.setattr(
        encrypted_db, "set_sqlcipher_rekey", rekey_with_racing_open
    )
    assert manager.change_password(username, old, new) is True
    racer.join(timeout=JOIN_TIMEOUT_S)
    assert not racer.is_alive(), "the racing open deadlocked on the sidecar"
    assert not racing_errors, f"the racing open raised: {racing_errors}"

    assert observed == {"cache_at_rekey": False, "racer_blocked": True}, (
        "the racing new-password open must be held off for the whole rekey "
        f"by the sidecar lock: {observed}"
    )
    surviving = racing_result.get("engine")
    assert surviving is not None, (
        "the racing new-password open must succeed once the rekey commits"
    )
    assert manager.connections.get(username) is surviving
    assert manager._password_matches_cached(username, new) is True
    assert manager.open_user_database(username, old) is None
    assert manager.connections.get(username) is surviving
    with surviving.connect() as conn:
        note = conn.execute(
            text("SELECT note FROM gap_b_canary WHERE id = 1")
        ).scalar()
    assert note == "gap-B-det", "rekey must preserve data"


def test_rekey_epoch_refuses_stale_old_publish(manager):
    """An engine published under a pre-rekey epoch is never handed out.

    Interleave #6565 targeted (from review at ``c02cf47c``): an
    ``open_user_database(old)`` verifies against the still-old-keyed file,
    parks before publishing, the rekey lands, and the parked open then
    plants an OLD-key engine with a matching old verifier. #6565 refused
    that publish with an in-process integer generation compared at publish
    time (``_get_rekey_epoch`` / ``_bump_rekey_epoch`` /
    ``_cache_connection(expected_epoch=...)``), which only fenced rekeys
    run by the same process. The durable rekey epoch supersedes it, and
    those helpers no longer exist: the parked open cannot straddle the
    rekey because it holds the sidecar lock from its epoch read to its
    publish, and even an engine that does get cached under a superseded
    epoch is never trusted, because every cache claim re-reads the durable
    epoch under the sidecar and detaches a generation whose epoch is not
    the committed one. This test plants exactly the stale state the
    interleave would produce and asserts the old password still cannot
    use it, the stale generation is dropped, and the new password works.
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    from sqlalchemy import create_engine

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

    bootstrap = manager.create_user_database(username, old)
    _write_canary(bootstrap, "gap-B-epoch")
    manager.close_user_database(username)
    del bootstrap
    db_path = manager._get_user_db_path(username)

    # The parked open read this epoch before its SQLCipher round-trip.
    stale_epoch = read_rekey_epoch(db_path)
    assert stale_epoch is not None

    assert manager.change_password(username, old, new) is True
    current_epoch = read_rekey_epoch(db_path)
    assert current_epoch not in (None, stale_epoch)
    assert username not in manager.connections

    # The parked OLD-key open's publish lands after the rekey.
    parked = create_engine(f"sqlite:///{db_path.as_posix()}")
    try:
        assert (
            manager._cache_connection(
                username,
                manager._make_connection_cache_entry(parked, old, stale_epoch),
            )
            is None
        )
        assert manager.is_user_connected(username) is False, (
            "a generation cached under a superseded epoch must not count as "
            "connected"
        )
        assert username not in manager.connections, (
            "the claim must detach the stale generation"
        )
        assert manager.open_user_database(username, old) is None
        assert username not in manager.connections
        assert username not in manager._password_verifiers
    finally:
        parked.dispose()

    # End-to-end: the new password opens, reads pre-change data, and is
    # published under the current epoch.
    reopened = manager.open_user_database(username, new)
    assert reopened is not None
    assert manager._connection_epochs.get(username) == current_epoch
    with reopened.connect() as conn:
        note = conn.execute(
            text("SELECT note FROM gap_b_canary WHERE id = 1")
        ).scalar()
    assert note == "gap-B-epoch"


def test_verifier_matches_degrades_on_wrong_typed_members(manager):
    """Pin the widened malformed-verifier guard.

    The unpack-only guard degraded ``"not-a-tuple"`` but a two-tuple with
    wrong-typed members -- ``("x", "y")`` or ``(b"salt", 123)`` -- still
    raised ``TypeError`` out of ``_verifier_matches`` (via ``salt + bytes``
    or ``hmac.compare_digest(bytes, int)``), which would escape from the
    login path that consulted the verifier instead of falling through to a
    key-validating open.
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"chgpw_vmtype_{uuid.uuid4().hex[:8]}"
    old = "OldCorrectHorse1!"  # noqa: S105
    engine = manager.create_user_database(username, old)
    assert engine is not None

    try:
        with manager._connections_lock:
            manager._password_verifiers[username] = ("x", "y")  # type: ignore[assignment]
            assert manager._verifier_matches(username, old) is False

        with manager._connections_lock:
            manager._password_verifiers[username] = (b"salt", 123)  # type: ignore[assignment]
            assert manager._verifier_matches(username, old) is False

        with manager._connections_lock:
            manager._password_verifiers[username] = (123, b"expected")  # type: ignore[assignment]
            assert manager._verifier_matches(username, old) is False
    finally:
        manager.close_user_database(username)


def test_engine_usable_rejects_mis_keyed_engine():
    """Pin the ``sqlite_master``-read hardening in ``_engine_usable``.

    Under SQLCipher ``SELECT 1`` touches no encrypted pages and succeeds
    even on a mis-keyed engine whose first table read raises ("file is
    not a database"). Probing only ``SELECT 1`` therefore measures pool
    liveness, not key validity. ``_engine_usable`` must read a real table
    so a stale-key engine fails here as it would on any data read.

    This is a logic pin with a stubbed engine shaped exactly like that
    mis-keyed engine (``SELECT 1`` succeeds, ``sqlite_master`` read
    raises), so it runs with or without a SQLCipher build. Reverting the
    helper to ``SELECT 1`` makes the stub report usable and this test
    fails.
    """
    from unittest.mock import MagicMock

    def _stub_engine(*, table_ok: bool):
        engine = MagicMock()
        conn = MagicMock()
        engine.connect.return_value.__enter__.return_value = conn

        def _execute(statement, *args, **kwargs):
            sql = str(statement)
            if "sqlite_master" in sql:
                if not table_ok:
                    raise Exception("file is not a database")
                result = MagicMock()
                result.all.return_value = [("gap_b_canary",)]
                return result
            result = MagicMock()
            result.all.return_value = [(1,)]
            result.scalar.return_value = 1
            result.fetchone.return_value = (1,)
            return result

        conn.execute.side_effect = _execute
        return engine

    mis_keyed = _stub_engine(table_ok=False)
    # Premise: SELECT 1 succeeds on this shape, so it cannot be the probe.
    with mis_keyed.connect() as conn:
        assert conn.execute(text("SELECT 1")).scalar() == 1

    usable, detail = _engine_usable(mis_keyed)
    assert not usable, (
        "a mis-keyed engine must fail _engine_usable "
        f"({detail}) -- SELECT 1 succeeds on it, so only the "
        "real-table read distinguishes it"
    )

    # Control: a correctly-keyed engine (both probes succeed) is usable.
    good = _stub_engine(table_ok=True)
    usable, detail = _engine_usable(good)
    assert usable, f"correctly-keyed engine must be usable ({detail})"


def test_open_cold_refuses_stale_publish_and_disposes(manager, monkeypatch):
    """A rekey cannot land between a cold open's verification and publish.

    #6565 pinned this by bumping its in-process generation from inside
    ``initialize_database`` and asserting that ``_open_user_database_cold``
    refused the publish. With the durable epoch, the publish moved to
    ``open_user_database`` (``_open_user_database_cold`` builds and
    verifies the engine only), and the whole cold open runs under the
    sidecar lock that ``change_password`` must hold for its rekey. So the
    stronger property is pinned here with a REAL ``change_password``
    started from inside the cold open: it must stay blocked until the
    open has published, and must then detach and dispose that engine
    before rekeying, leaving the old password dead and the old engine
    unusable. (A writer that bypasses the sidecar and moves the epoch
    mid-open is covered by
    ``tests/database/test_rekey_serialization.py::``
    ``test_post_open_epoch_recheck_that_cannot_vouch_fails_closed``.)
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    import local_deep_research.database.initialize as _init_module

    username = f"chgpw_cold_{uuid.uuid4().hex[:8]}"
    old = "ColdCorrectHorse1!"  # noqa: S105
    new = "ColdCorrectHorse2!"  # noqa: S105

    bootstrap = manager.create_user_database(username, old)
    _write_canary(bootstrap, "gap-B-cold")
    manager.close_user_database(username)
    del bootstrap

    outcome = {}
    errors = {}

    def rekeyer():
        try:
            outcome["changed"] = manager.change_password(username, old, new)
        except Exception as exc:  # noqa: BLE001 - surfaced below
            errors["rekeyer"] = exc

    rekey_thread = threading.Thread(target=rekeyer, name="gap-b-cold-rekey")
    observed = {}
    real_initialize = _init_module.initialize_database

    def rekey_during_initialize(engine):
        if not observed:
            rekey_thread.start()
            rekey_thread.join(timeout=0.3)
            observed["rekey_blocked"] = rekey_thread.is_alive()
        return real_initialize(engine)

    monkeypatch.setattr(
        _init_module, "initialize_database", rekey_during_initialize
    )

    opened = manager.open_user_database(username, old)
    rekey_thread.join(timeout=JOIN_TIMEOUT_S)
    assert not rekey_thread.is_alive(), "change_password deadlocked"
    assert not errors, f"change_password raised: {errors}"
    assert observed == {"rekey_blocked": True}, (
        "change_password must wait for the in-flight cold open to publish"
    )
    assert opened is not None, (
        "the cold open verified the then-current key and must publish"
    )
    assert outcome.get("changed") is True

    # The rekey detached the engine the open published, so the old
    # password neither finds it cached nor opens the rekeyed file.
    assert manager.connections.get(username) is not opened
    assert manager.open_user_database(username, old) is None
    usable, detail = _engine_usable(opened)
    assert not usable, (
        "the engine published under the old key must be unusable after "
        f"the rekey ({detail})"
    )
    reopened = manager.open_user_database(username, new)
    assert reopened is not None
    with reopened.connect() as conn:
        note = conn.execute(
            text("SELECT note FROM gap_b_canary WHERE id = 1")
        ).scalar()
    assert note == "gap-B-cold"