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.

3.2 KB
Raw Blame
"""Temp-store posture: plaintext spill files vs. memory.

Research context (SQLCipher design docs, "Temp Files": temporary files
used by ORDER BY / GROUP BY / large sorts are NOT encrypted by
SQLCipher -- they contain plaintext result data -- unless the engine
keeps them in memory; sqlite.org/pragma.html#pragma_temp_store: 0/1
allow file-backed temp storage, 2 forces MEMORY). An app doing large
sorts with temp_store < 2 writes plaintext table content into the
system temp directory.

Probed and pinned against the real manager (HOLD):

* AL1 every connection this manager builds sets
  ``PRAGMA temp_store = MEMORY`` -- ``apply_performance_pragmas``
  (sqlcipher_utils.py) issues it on every cold connect, so query
  spill stays in process memory and no plaintext temp files reach
  disk. Pinned by reading the pragma back through a live session.

AL2 (forcing a real large sort to observe spill) is deliberately not
shipped: the pragma pin is the deliverable and a spill-sized query
would blow the run-time budget for no extra signal.
"""

import uuid

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


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

    Same self-contained pattern as the round-1..6 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 test_temp_store_is_memory_on_manager_connections(manager):
    """AL1: temp_store == 2 (MEMORY) on a live manager connection --
    sort spill never becomes a plaintext temp file on disk."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"al1_{uuid.uuid4().hex[:8]}"
    password = "TempStorePw1!"  # noqa: S105
    engine = manager.create_user_database(username, password)
    with engine.begin() as conn:
        conn.execute(
            text("CREATE TABLE gap_al (id INTEGER PRIMARY KEY, v TEXT)")
        )
        conn.execute(text("INSERT INTO gap_al VALUES (1, 'gap-AL')"))

    with engine.connect() as conn:
        value = conn.execute(text("PRAGMA temp_store")).scalar()
    assert value == 2, (
        f"temp_store posture changed: observed {value!r} (was 2/MEMORY). "
        "0 or 1 would let large sorts spill plaintext result data into "
        "file-backed temp storage that SQLCipher does not encrypt -- "
        "treat as a posture regression (SQLCipher 'Temp Files' note)."
    )

    # The connection is otherwise healthy (not a stale pragma read).
    with engine.connect() as conn:
        assert conn.execute(text("SELECT v FROM gap_al")).scalar() == "gap-AL"