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.

11.0 KB
Raw Blame
"""Close idempotency and salt stability across the rekey lifecycle.

Incident context (PR #5596): eviction correctness is what keeps the
cache trustworthy -- ``close_user_database`` pops engine AND verifier
together under the lock, and ``change_password``'s rekey must not
disturb the on-disk key-derivation inputs. This file pins the two
remaining lifecycle edges:

* AE1 double-close is a no-op (the ``if username in connections`` guard
    makes the second close silently skip) -- single-threaded and for a
  user never opened; no raise, maps stay clean;
* AE2 concurrent double-close: 2 threads x 6 iterations racing closes
  of the same warm user -- no unhandled exception, the pairing invariant
  holds under ``_connections_lock`` at every sample, maps empty after,
  reopen works (round-4 O's pairing discipline applied to the close
  path);
* AF1 salt stability across rekey: the .salt bytes (sha256), its 0o600
  mode, and the absence of .salt.new/.salt.tmp leftovers are identical
  before and after change_password -- the rekey changes the FILE key,
  never the salt (set_sqlcipher_rekey reads the same salt);
* AF2 sidecars after rekey + final close: none left (the close-time WAL
  checkpoint removes them, mirroring round-4 S on the rekey path).
"""

import hashlib
import os
import stat
import threading
import uuid
from pathlib import Path

import pytest
from sqlalchemy import text

from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.sqlcipher_utils import get_salt_file_path

# 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)")

CLOSE_ITERATIONS = 6
JOIN_TIMEOUT_S = 60
BARRIER_TIMEOUT_S = 30
# Race witness floor for AE2: at least this many of the racing closes must
# have found a live cached entry. Below it the "race" degenerated to AE1's
# no-op double-close shape and pins nothing new.
MIN_EFFECTIVE_CLOSES = 1


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

    Same self-contained pattern as the round-1..5 files, with the
    sanctioned test-mode KDF knob.
    """
    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:
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_ae_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_ae_canary (id, note) VALUES (1, :note)"),
            {"note": note},
        )


def _salt_state(db_path: Path) -> tuple:
    salt_path = get_salt_file_path(db_path)
    data = salt_path.read_bytes()
    return (
        hashlib.sha256(data).hexdigest(),
        stat.S_IMODE(os.stat(salt_path).st_mode),
    )


def test_double_close_is_an_idempotent_noop(manager):
    """AE1: closing twice in a row, and closing a user who was never
    opened, neither raise nor disturb the maps."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"dblclose_{uuid.uuid4().hex[:8]}"
    password = "DoubleClosePw1!"  # noqa: S105
    engine = manager.create_user_database(username, password)
    _write_canary(engine, "gap-AE1")

    manager.close_user_database(username)
    # Second close on the just-closed user: silent no-op.
    manager.close_user_database(username)
    # Close for a user never opened on this manager: same shape.
    manager.close_user_database(f"never-opened-{uuid.uuid4().hex[:8]}")

    with manager._connections_lock:
        assert manager.connections == {}
        assert manager._password_verifiers == {}
    # Reopen still works -- the no-op closes hurt nothing.
    reopened = manager.open_user_database(username, password)
    assert reopened is not None
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_ae_canary WHERE id = 1")
            ).scalar()
            == "gap-AE1"
        )


def test_concurrent_double_close_races_stay_clean(manager):
    """AE2: two threads race closes of the same warm user. No unhandled
    exception, pairing invariant at every sample, empty maps after,
    reopen works."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"raceclose_{uuid.uuid4().hex[:8]}"
    password = "RaceClosePw1!"  # noqa: S105
    engine = manager.create_user_database(username, password)
    _write_canary(engine, "gap-AE2")

    barrier = threading.Barrier(2)
    errors = {}
    pairing_violations = []
    rounds = {}  # race witness: completed iterations per closer
    effective_closes = []  # closes that actually evicted a cached engine

    def closer(label):
        try:
            barrier.wait(timeout=BARRIER_TIMEOUT_S)
            completed = 0
            for i in range(CLOSE_ITERATIONS):
                # Reopen between closes so both threads usually find
                # something to close -- otherwise the race collapses to
                # AE1's no-op shape and proves nothing.
                if manager.open_user_database(username, password) is None:
                    manager.open_user_database(username, password)
                with manager._connections_lock:
                    was_cached = username in manager.connections
                manager.close_user_database(username)
                if was_cached:
                    effective_closes.append(f"{label}#{i}")
                completed += 1
                with manager._connections_lock:
                    engines = set(manager.connections)
                    verifiers = set(manager._password_verifiers)
                if engines != verifiers:
                    pairing_violations.append(
                        f"{label} #{i}: {engines} vs {verifiers}"
                    )
            rounds[label] = completed
        except Exception as exc:  # noqa: BLE001 - surfaced via `errors` below
            errors[label] = exc

    threads = [
        threading.Thread(target=closer, args=("a",), name="gap-ae2-a"),
        threading.Thread(target=closer, args=("b",), name="gap-ae2-b"),
    ]
    for t in threads:
        t.start()
    for t in threads:
        t.join(timeout=JOIN_TIMEOUT_S)
    assert not any(t.is_alive() for t in threads), "close race deadlocked"
    assert not errors, f"a racing closer raised: {errors}"
    assert not pairing_violations, (
        f"engine/verifier pairing broke: {pairing_violations}"
    )

    # Race witnesses: both closers ran every iteration, and the closes were
    # not all no-ops. `outcome recorded but never asserted` is how a loaded
    # runner reports green having raced nothing.
    assert rounds == {"a": CLOSE_ITERATIONS, "b": CLOSE_ITERATIONS}, (
        f"a closer did not complete its iterations: {rounds}"
    )
    assert len(effective_closes) >= MIN_EFFECTIVE_CLOSES, (
        f"only {len(effective_closes)} of the racing closes found a cached "
        f"engine (minimum {MIN_EFFECTIVE_CLOSES}) -- every close was a no-op, "
        "so this run degenerated to AE1's shape"
    )

    manager.close_user_database(username)  # final drain
    with manager._connections_lock:
        assert username not in manager.connections
        assert username not in manager._password_verifiers

    reopened = manager.open_user_database(username, password)
    assert reopened is not None
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_ae_canary WHERE id = 1")
            ).scalar()
            == "gap-AE2"
        )


def test_salt_bytes_and_mode_survive_rekey_untouched(manager):
    """AF1: change_password rekeys the file, not the salt: sha256 and
    0o600 mode identical before/after, and no .salt.new/.salt.tmp (or
    any other unexpected sibling) is left behind."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"saltrekey_{uuid.uuid4().hex[:8]}"
    old = "SaltRekeyOldPw1!"  # noqa: S105
    new = "SaltRekeyNewPw2!"  # noqa: S105
    engine = manager.create_user_database(username, old)
    _write_canary(engine, "gap-AF1")
    manager.close_user_database(username)

    db_path = manager._get_user_db_path(username)
    before = _salt_state(db_path)
    files_before = {p.name for p in manager.data_dir.iterdir()}

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

    after = _salt_state(db_path)
    assert after[0] == before[0], "the salt BYTES changed across the rekey"
    assert after[1] == before[1] == 0o600, (
        "the salt MODE changed across the rekey"
    )

    files_after = {p.name for p in manager.data_dir.iterdir()}
    unexpected = {
        name
        for name in files_after - files_before
        if ".salt.new" in name or ".salt.tmp" in name
    }
    assert not unexpected, (
        f"rekey left salt work-in-progress files: {unexpected}"
    )

    # And the rekeyed database serves its data under the new password.
    reopened = manager.open_user_database(username, new)
    assert reopened is not None
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_ae_canary WHERE id = 1")
            ).scalar()
            == "gap-AF1"
        )


def test_no_sidecars_left_after_rekey_and_final_close(manager):
    """AF2: warm with a fresh write, rekey, close -> the close-time WAL
    checkpoint leaves no -wal/-shm behind (round-4 S's property, pinned
    on the rekey path)."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"rekeywal_{uuid.uuid4().hex[:8]}"
    old = "RekeyWalOldPw1!"  # noqa: S105
    new = "RekeyWalNewPw2!"  # noqa: S105
    engine = manager.create_user_database(username, old)
    _write_canary(engine, "gap-AF2")

    # WAL mode is genuinely on while warm (round-4 S pinned this; here we
    # only need the sidecars to be plausible mid-open).
    assert manager.change_password(username, old, new) is True
    manager.close_user_database(username)

    leftovers = [
        p.name
        for p in manager.data_dir.iterdir()
        if p.name.endswith(("-wal", "-shm"))
    ]
    assert leftovers == [], (
        f"rekey + close left WAL sidecars behind: {leftovers}"
    )

    reopened = manager.open_user_database(username, new)
    assert reopened is not None
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_ae_canary WHERE id = 1")
            ).scalar()
            == "gap-AF2"
        )