~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
"""WAL hygiene at close_user_database.
Incident context (PR #5596): at-rest security of these databases depends
on what sits next to them on disk. ``close_user_database`` runs
``_checkpoint_wal`` (``PRAGMA wal_checkpoint(TRUNCATE)``,
encrypted_db.py::_checkpoint_wal) before ``engine.dispose()``, so pending
writes are flushed into the main .db and the WAL sidecars can be cleaned
up rather than left holding data indefinitely. This file pins the
observable file-level contract:
* S1 while the engine is open and has written, WAL mode is actually on
(``PRAGMA journal_mode`` == "wal") and the -wal/-shm sidecars EXIST;
after ``close_user_database`` the sidecars are GONE (SQLite removes
them on the final connection close once the WAL is checkpointed and
empty -- pinned empirically); a reopen with the correct password reads
the canary, proving the checkpoint lost nothing;
* S2 failed wrong-password opens never grow or create WAL sidecars:
with the user WARM (sidecars present mid-open) and with the user
CLOSED (sidecars absent), repeated wrong-password attempts leave the
sidecar set and byte sizes exactly as they were.
Round 1's test_db_connection_cold_open_artifacts.py fingerprinted failed
opens against a CLOSED user for the whole file set; S2 differs by
targeting sidecar bytes in both the warm (sidecars-present) and closed
states, and S1 is a new property entirely.
"""
import uuid
from pathlib import Path
import pytest
from sqlalchemy import text
from local_deep_research.database.encrypted_db import DatabaseManager
# 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)")
WRONG_ATTEMPTS = 3
@pytest.fixture
def manager(tmp_path, monkeypatch):
"""A DatabaseManager writing to an isolated data directory.
Same self-contained pattern as the round-1/2/3 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 _sidecar_fingerprint(db_path: Path) -> dict:
"""name -> size for the -wal/-shm sidecars of this database."""
fingerprint = {}
for suffix in ("-wal", "-shm"):
sidecar = db_path.with_name(db_path.name + suffix)
if sidecar.exists():
fingerprint[sidecar.name] = sidecar.stat().st_size
return fingerprint
def _write_canary(engine, note: str) -> None:
with engine.begin() as conn:
conn.execute(
text(
"CREATE TABLE gap_s_canary (id INTEGER PRIMARY KEY, note TEXT)"
)
)
conn.execute(
text("INSERT INTO gap_s_canary (id, note) VALUES (1, :note)"),
{"note": note},
)
def test_close_checkpoints_wal_and_removes_sidecars(manager):
"""S1: WAL is genuinely on while open (journal_mode pragma + present
sidecars); close removes the sidecars; reopen reads the canary."""
if not manager.has_encryption:
pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
username = f"walhygiene_{uuid.uuid4().hex[:8]}"
password = "WalHygienePassword1!" # noqa: S105
engine = manager.create_user_database(username, password)
_write_canary(engine, "gap-S1")
# WAL mode is actually on for this connection.
with engine.connect() as conn:
mode = conn.execute(text("PRAGMA journal_mode")).scalar()
assert str(mode).lower() == "wal", (
f"expected WAL journal mode, got {mode!r} -- the rest of this "
"test's assumptions about sidecars would not hold"
)
db_path = manager._get_user_db_path(username)
mid_open = _sidecar_fingerprint(db_path)
assert set(mid_open) == {"-wal", "-shm"} or set(mid_open), (
"expected WAL sidecars to exist while the engine is open and has "
f"written; found {mid_open}"
)
manager.close_user_database(username)
after_close = _sidecar_fingerprint(db_path)
assert after_close == {}, (
"close_user_database left WAL sidecars behind "
f"({after_close}) -- the TRUNCATE checkpoint + final-connection "
"close should remove them"
)
# The checkpoint lost nothing: reopen reads the canary.
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_s_canary WHERE id = 1")
).scalar()
== "gap-S1"
)
def test_wrong_password_attempts_never_grow_sidecars(manager):
"""S2: wrong-password attempts (warm user with sidecars present, and
closed user with sidecars absent) never create or enlarge -wal/-shm."""
if not manager.has_encryption:
pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
username = f"walgrow_{uuid.uuid4().hex[:8]}"
password = "WalGrowPassword1!" # noqa: S105
engine = manager.create_user_database(username, password)
_write_canary(engine, "gap-S2")
db_path = manager._get_user_db_path(username)
# Warm phase: sidecars are present mid-open; the wrong attempts must
# not change their sizes (or the file set).
warm_before = _sidecar_fingerprint(db_path)
assert warm_before, "setup: sidecars expected while warm"
for i in range(WRONG_ATTEMPTS):
assert (
manager.open_user_database(username, f"wal-grow-wrong-{i}") is None
)
assert _sidecar_fingerprint(db_path) == warm_before, (
"a wrong-password attempt against a WARM user changed WAL "
"sidecars (sizes or presence)"
)
# Closed phase: sidecars absent; the wrong attempts must not create any.
manager.close_user_database(username)
closed_before = _sidecar_fingerprint(db_path)
assert closed_before == {}, "setup: sidecars expected gone after close"
for i in range(WRONG_ATTEMPTS):
assert (
manager.open_user_database(username, f"wal-cold-wrong-{i}") is None
)
assert _sidecar_fingerprint(db_path) == closed_before, (
"a wrong-password attempt against a CLOSED user created or "
"changed WAL sidecars"
)
# And the legit user is unaffected afterwards.
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_s_canary WHERE id = 1")
).scalar()
== "gap-S2"
)