~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
from __future__ import annotations
import threading
from collections.abc import Generator
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from pathlib import Path
from typing import Literal, assert_never
from unittest.mock import MagicMock, patch
import pytest
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 acquire_rekey_lock
from local_deep_research.web.auth import connection_cleanup as cleanup_module
from local_deep_research.web.auth.connection_cleanup import (
cleanup_idle_connections,
)
@dataclass(frozen=True, slots=True)
class PausedRekey:
manager: DatabaseManager
stale_engine: MagicMock
def _new_manager(data_dir: Path) -> DatabaseManager:
with patch.object(
DatabaseManager, "_check_encryption_available", return_value=True
):
manager = DatabaseManager()
manager.data_dir = data_dir
return manager
def _existing_db(manager: DatabaseManager, username: str) -> Path:
db_path = manager._get_user_db_path(username)
db_path.touch()
return db_path
def _initialize_epoch(db_path: Path) -> str:
with acquire_rekey_lock(db_path):
return rekey_lock_module.read_or_initialize_rekey_epoch(db_path)
def _cache_current_engine(
manager: DatabaseManager, username: str = "alice"
) -> tuple[Path, MagicMock]:
db_path = _existing_db(manager, username)
epoch = _initialize_epoch(db_path)
engine = MagicMock()
manager._cache_connection(
username,
manager._make_connection_cache_entry(engine, "password", epoch),
)
return db_path, engine
@pytest.fixture
def paused_rekey(tmp_path: Path) -> Generator[PausedRekey, None, None]:
stale_manager = _new_manager(tmp_path)
db_path = _existing_db(stale_manager, "alice")
original_epoch = _initialize_epoch(db_path)
stale_engine = MagicMock()
stale_manager._cache_connection(
"alice",
stale_manager._make_connection_cache_entry(
stale_engine, "old-password", original_epoch
),
)
sidecar_acquired = threading.Event()
release_sidecar = threading.Event()
def hold_pending_sidecar() -> None:
with acquire_rekey_lock(db_path):
pending_epoch = rekey_lock_module.begin_rekey_epoch(db_path)
sidecar_acquired.set()
assert release_sidecar.wait(timeout=5)
rekey_lock_module.commit_rekey_epoch(db_path, pending_epoch)
rekey_lock_module.clear_pending_rekey_epoch(db_path, pending_epoch)
with ThreadPoolExecutor(max_workers=1) as executor:
sidecar_owner = executor.submit(hold_pending_sidecar)
assert sidecar_acquired.wait(timeout=5)
try:
yield PausedRekey(stale_manager, stale_engine)
finally:
release_sidecar.set()
sidecar_owner.result(timeout=5)
def test_close_user_database_disposes_only_during_rekey(
paused_rekey: PausedRekey,
) -> None:
# Given a cached old-key engine while another worker holds the rekey sidecar
manager = paused_rekey.manager
stale_engine = paused_rekey.stale_engine
with patch.object(
manager, "_checkpoint_wal", wraps=manager._checkpoint_wal
) as checkpoint:
# When explicit close runs during the paused rekey
manager.close_user_database("alice")
# Then close detaches and disposes without opening the stale engine
checkpoint.assert_not_called()
stale_engine.connect.assert_not_called()
stale_engine.dispose.assert_called_once_with()
assert "alice" not in manager.connections
def test_close_all_databases_disposes_only_during_rekey(
paused_rekey: PausedRekey,
) -> None:
# Given a cached old-key engine while another worker holds the rekey sidecar
manager = paused_rekey.manager
stale_engine = paused_rekey.stale_engine
with patch.object(
manager, "_checkpoint_wal", wraps=manager._checkpoint_wal
) as checkpoint:
# When shutdown closes every cached database
manager.close_all_databases()
# Then shutdown disposes without opening the stale engine
checkpoint.assert_not_called()
stale_engine.connect.assert_not_called()
stale_engine.dispose.assert_called_once_with()
assert not manager.connections
assert not manager._password_verifiers
assert not manager._connection_epochs
def test_periodic_pool_reset_skips_live_rekey_sidecar(
paused_rekey: PausedRekey,
) -> None:
# Given an active user whose cached old-key engine needs a periodic pool reset
manager = paused_rekey.manager
stale_engine = paused_rekey.stale_engine
stale_engine.pool.checkedout.return_value = 0
session_manager = MagicMock()
session_manager.get_active_usernames.return_value = {"alice"}
previous_dispose_time = cleanup_module._last_dispose_time
cleanup_module._last_dispose_time = -1e9
try:
with (
patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
),
patch(
"local_deep_research.web.auth.connection_cleanup.cleanup_dead_threads"
),
patch(
"local_deep_research.web.auth.connection_cleanup._count_open_fds",
return_value=0,
),
patch.object(
manager, "_checkpoint_wal", wraps=manager._checkpoint_wal
) as checkpoint,
):
# When the periodic scheduler resets cached pools during rekey
cleanup_idle_connections(session_manager, manager)
finally:
cleanup_module._last_dispose_time = previous_dispose_time
# Then live sidecar contention skips reset without touching the cached engine
checkpoint.assert_not_called()
stale_engine.connect.assert_not_called()
stale_engine.dispose.assert_not_called()
assert manager.connections["alice"] is stale_engine
def test_periodic_pool_reset_counts_only_successful_disposals(
tmp_path: Path,
) -> None:
# Given two current-generation engines where one pool disposal fails
manager = _new_manager(tmp_path)
engines = {"alice": MagicMock(), "bob": MagicMock()}
engines["alice"].dispose.side_effect = RuntimeError("dispose failed")
for username, engine in engines.items():
db_path = _existing_db(manager, username)
epoch = _initialize_epoch(db_path)
manager._cache_connection(
username,
manager._make_connection_cache_entry(engine, "password", epoch),
)
with patch.object(manager, "_checkpoint_wal") as checkpoint:
# When the manager resets all current cached pools
disposed = manager.reset_connection_pools()
# Then both are attempted, only the successful reset is counted, and caches stay
assert disposed == 1
assert checkpoint.call_count == 2
engines["alice"].dispose.assert_called_once_with()
engines["bob"].dispose.assert_called_once_with()
assert manager.get_connected_usernames() == {"alice", "bob"}
def test_periodic_pool_reset_skips_replaced_engine_identity(
tmp_path: Path,
) -> None:
# Given a cached generation that is replaced after the reset snapshot
manager = _new_manager(tmp_path)
db_path = _existing_db(manager, "alice")
epoch = _initialize_epoch(db_path)
old_engine = MagicMock()
replacement_engine = MagicMock()
manager._cache_connection(
"alice",
manager._make_connection_cache_entry(old_engine, "password", epoch),
)
real_acquire = acquire_rekey_lock
def replace_before_sidecar(path: Path, *, blocking: bool = False):
manager._cache_connection(
"alice",
manager._make_connection_cache_entry(
replacement_engine, "password", epoch
),
)
return real_acquire(path, blocking=blocking)
with (
patch(
"local_deep_research.database.encrypted_db.acquire_rekey_lock",
side_effect=replace_before_sidecar,
),
patch.object(manager, "_checkpoint_wal") as checkpoint,
):
# When periodic reset reaches the snapshotted generation
disposed = manager.reset_connection_pools()
# Then it does not checkpoint or dispose either mismatched identity
assert disposed == 0
checkpoint.assert_not_called()
old_engine.dispose.assert_not_called()
replacement_engine.dispose.assert_not_called()
assert manager.connections["alice"] is replacement_engine
def test_periodic_reset_skips_lifecycle_contention(tmp_path: Path) -> None:
# Given another accessor owns the stable lifecycle lock
manager = _new_manager(tmp_path)
_, engine = _cache_current_engine(manager)
lifecycle_lock = manager._get_rekey_lock("alice")
lifecycle_lock.acquire()
try:
# When periodic reset attempts nonblocking ownership
disposed = manager.reset_connection_pools()
finally:
lifecycle_lock.release()
# Then contention skips without checkpointing or disposing
assert disposed == 0
engine.connect.assert_not_called()
engine.dispose.assert_not_called()
assert manager.connections["alice"] is engine
def test_periodic_reset_skips_sidecar_contention(tmp_path: Path) -> None:
# Given another process owns the stable sidecar lock
manager = _new_manager(tmp_path)
db_path, engine = _cache_current_engine(manager)
with acquire_rekey_lock(db_path):
# When periodic reset attempts nonblocking ownership
disposed = manager.reset_connection_pools()
# Then contention skips without checkpointing or disposing
assert disposed == 0
engine.connect.assert_not_called()
engine.dispose.assert_not_called()
assert manager.connections["alice"] is engine
def test_accessor_winner_makes_concurrent_reset_skip(tmp_path: Path) -> None:
# Given an accessor owns the exact current generation inside its claim
manager = _new_manager(tmp_path)
_, engine = _cache_current_engine(manager)
accessor_started = threading.Event()
release_accessor = threading.Event()
def hold_claim() -> bool:
with manager._acquire_current_connection_generation(
"alice"
) as generation:
accessor_started.set()
assert release_accessor.wait(timeout=5)
return generation is not None
with ThreadPoolExecutor(max_workers=1) as executor:
accessor = executor.submit(hold_claim)
assert accessor_started.wait(timeout=5)
try:
# When reset races the accessor's lifecycle ownership
disposed = manager.reset_connection_pools()
finally:
release_accessor.set()
assert accessor.result(timeout=5) is True
# Then reset skips and never disposes the generation in active use
assert disposed == 0
engine.dispose.assert_not_called()
def test_integrity_scan_runs_after_the_claim_is_released(
tmp_path: Path,
) -> None:
# Given an integrity check paused inside its (slow) database scan
manager = _new_manager(tmp_path)
_, engine = _cache_current_engine(manager)
connection = engine.connect.return_value.__enter__.return_value
quick_check = MagicMock()
quick_check.fetchone.return_value = ("ok",)
cipher_check = MagicMock()
cipher_check.__iter__.return_value = iter(())
scan_started = threading.Event()
release_scan = threading.Event()
def execute(_statement):
if not scan_started.is_set():
scan_started.set()
assert release_scan.wait(timeout=10)
return quick_check
return cipher_check
connection.execute.side_effect = execute
with ThreadPoolExecutor(max_workers=2) as executor:
integrity = executor.submit(manager.check_database_integrity, "alice")
assert scan_started.wait(timeout=5)
try:
# When another probe for the same user (the shape the shared
# log-drain thread and every request take) runs meanwhile
probe = executor.submit(manager.is_user_connected, "alice")
# Then it completes without waiting for the scan: the scan no
# longer holds the lifecycle lock, sidecar gate or flock
probe_connected = probe.result(timeout=3)
lifecycle_free = manager._get_rekey_lock("alice").acquire(
blocking=False
)
if lifecycle_free:
manager._get_rekey_lock("alice").release()
finally:
release_scan.set()
assert integrity.result(timeout=5) is True
assert probe_connected is True
assert lifecycle_free is True
# The scan still ran on the engine the claim validated
engine.connect.assert_called_once_with()
engine.dispose.assert_not_called()
def test_reset_winner_serializes_following_accessor(tmp_path: Path) -> None:
# Given reset owns the current generation and is paused in disposal
manager = _new_manager(tmp_path)
_, engine = _cache_current_engine(manager)
reset_started = threading.Event()
release_reset = threading.Event()
reset_action_complete = threading.Event()
def controlled_dispose() -> None:
reset_started.set()
assert release_reset.wait(timeout=5)
reset_action_complete.set()
engine.dispose.side_effect = controlled_dispose
with (
patch.object(manager, "_checkpoint_wal"),
ThreadPoolExecutor(max_workers=2) as executor,
):
reset = executor.submit(manager.reset_connection_pools)
assert reset_started.wait(timeout=5)
real_claim = manager._claim_connection_generation
def claim_after_reset(generation, state):
assert reset_action_complete.is_set()
return real_claim(generation, state)
with patch.object(
manager,
"_claim_connection_generation",
side_effect=claim_after_reset,
):
# When an accessor snapshots while reset still owns lifecycle
connected = executor.submit(manager.is_user_connected, "alice")
release_reset.set()
assert reset.result(timeout=5) == 1
assert connected.result(timeout=5) is True
# Then exactly one reset occurred and the current generation stayed attached
engine.dispose.assert_called_once_with()
assert manager.connections["alice"] is engine
@pytest.mark.parametrize("transition", ["stale", "pending"])
def test_stale_or_pending_reset_detaches_before_single_disposal(
tmp_path: Path,
transition: Literal["stale", "pending"],
) -> None:
# Given reset snapshots a cache generation fenced by durable state
manager = _new_manager(tmp_path)
db_path, engine = _cache_current_engine(manager)
with acquire_rekey_lock(db_path):
match transition:
case "stale":
rekey_lock_module.advance_rekey_epoch(db_path)
case "pending":
rekey_lock_module.begin_rekey_epoch(db_path)
case unreachable:
assert_never(unreachable)
with patch.object(manager, "_checkpoint_wal") as checkpoint:
# When periodic reset runs twice against that fenced generation
first_disposed = manager.reset_connection_pools()
second_disposed = manager.reset_connection_pools()
# Then exact detachment gives one owner one dispose without checkpoint
assert first_disposed == 1
assert second_disposed == 0
checkpoint.assert_not_called()
engine.connect.assert_not_called()
engine.dispose.assert_called_once_with()
assert "alice" not in manager.connections
def _make_epoch_file_unreadable(db_path: Path) -> None:
# A directory where the epoch file should be: the sidecar still opens,
# but reading the epoch raises IsADirectoryError, which the reader
# reports as RekeyEpochUnreadableError (the same outcome as EMFILE
# right after the sidecar took the last fd, or EIO).
epoch_file = db_path.with_name(f"{db_path.name}.rekey.epoch")
epoch_file.unlink()
epoch_file.mkdir()
def test_periodic_reset_keeps_engine_when_epoch_read_fails(
tmp_path: Path,
) -> None:
# Given a current cached engine whose epoch file cannot be read
manager = _new_manager(tmp_path)
db_path, engine = _cache_current_engine(manager)
_make_epoch_file_unreadable(db_path)
with patch.object(manager, "_checkpoint_wal") as checkpoint:
# When the periodic reset sweeps it
disposed = manager.reset_connection_pools()
# Then nothing was read, so nothing proves it stale: the sweep skips it
# and keeps the engine (the next successful probe re-checks the epoch)
assert disposed == 0
checkpoint.assert_not_called()
engine.connect.assert_not_called()
engine.dispose.assert_not_called()
assert manager.connections["alice"] is engine
def test_close_still_disposes_when_epoch_read_fails(tmp_path: Path) -> None:
# Given a current cached engine whose epoch file cannot be read
manager = _new_manager(tmp_path)
db_path, engine = _cache_current_engine(manager)
_make_epoch_file_unreadable(db_path)
with patch.object(manager, "_checkpoint_wal") as checkpoint:
# When the user's database is closed explicitly
manager.close_user_database("alice")
# Then it is still detached and disposed, without a WAL checkpoint
checkpoint.assert_not_called()
engine.connect.assert_not_called()
engine.dispose.assert_called_once_with()
assert "alice" not in manager.connections
def test_periodic_reset_detaches_engine_when_epoch_is_malformed(
tmp_path: Path,
) -> None:
# Given a current cached engine whose epoch file was read but is not
# a canonical epoch token (state that does not vouch, not a read fault)
manager = _new_manager(tmp_path)
db_path, engine = _cache_current_engine(manager)
db_path.with_name(f"{db_path.name}.rekey.epoch").write_text("not-a-uuid")
with patch.object(manager, "_checkpoint_wal") as checkpoint:
disposed = manager.reset_connection_pools()
# Then the generation is stale: detached and disposed once, unchecked
assert disposed == 1
checkpoint.assert_not_called()
engine.connect.assert_not_called()
engine.dispose.assert_called_once_with()
assert "alice" not in manager.connections