~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
"""Configured library-root compatibility and benchmark output containment.
The selected library policy (#7152/#7224, closing #6335) preserves existing
saved roots and operator overrides outside LDR_DATA_DIR. User imports cannot
redirect them; test_settings_bulk_import covers that write boundary with the
real settings manager. These resolver tests ensure configured roots still
work and document paths remain scoped to the user's library.
Benchmark output follows a different rule (#7417): run_benchmark resolves
its output directory inside LDR_DATA_DIR and replaces any other location.
Every configured root and canary lives under tmp_path.
"""
import os
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from local_deep_research.research_library.services.pdf_storage_manager import (
PDFStorageManager,
)
from local_deep_research.research_library.utils import (
get_absolute_path_from_settings,
get_library_storage_path,
)
def _files_below(directory: Path) -> dict[Path, bytes]:
return {
path.relative_to(directory): path.read_bytes()
for path in directory.rglob("*")
if path.is_file()
}
@pytest.fixture
def isolated_data_dir(tmp_path, monkeypatch):
data_dir = tmp_path / "ldr-data"
data_dir.mkdir()
monkeypatch.setenv("LDR_DATA_DIR", str(data_dir))
return data_dir
@pytest.fixture
def configured_storage_paths(tmp_path, monkeypatch):
home = tmp_path / "home"
home.mkdir()
monkeypatch.setenv("HOME", str(home))
workspace = tmp_path / "workspace"
workspace.mkdir()
monkeypatch.chdir(workspace)
outside = tmp_path / "outside"
outside.mkdir()
(outside / "CANARY").write_text("do not alter")
return {
"expandvars": "$HOME/configured-library",
"absolute": str(outside / "absolute-library"),
"relative": "../outside/relative-library",
}, outside
def _settings_for(storage_path: str) -> MagicMock:
settings = MagicMock()
settings.get_setting.side_effect = lambda key, default=None: (
storage_path if key == "research_library.storage_path" else default
)
return settings
@pytest.mark.parametrize("kind", ["expandvars", "absolute", "relative"])
def test_configured_storage_root_is_preserved(
isolated_data_dir, configured_storage_paths, kind
):
paths, outside = configured_storage_paths
before = _files_below(outside)
configured_base = Path(os.path.expandvars(paths[kind])).resolve()
settings = _settings_for(paths[kind])
with patch(
"local_deep_research.utilities.db_utils.get_settings_manager",
return_value=settings,
):
resolved = get_library_storage_path("alice")
assert resolved == configured_base / "alice"
assert resolved.is_dir()
assert not resolved.is_relative_to(isolated_data_dir)
assert _files_below(outside) == before
@pytest.mark.parametrize("kind", ["expandvars", "absolute", "relative"])
def test_configured_library_document_read_stays_in_user_root(
isolated_data_dir, configured_storage_paths, kind
):
paths, outside = configured_storage_paths
configured_base = Path(os.path.expandvars(paths[kind])).resolve()
legitimate = configured_base / "alice" / "pdfs" / "paper.pdf"
legitimate.parent.mkdir(parents=True)
legitimate.write_bytes(b"%PDF-safe-library")
settings = _settings_for(paths[kind])
before = _files_below(outside)
resolved = get_absolute_path_from_settings(
"pdfs/paper.pdf", username="alice", settings_manager=settings
)
assert resolved == legitimate
assert resolved.read_bytes() == b"%PDF-safe-library"
assert (
get_absolute_path_from_settings(
"../CANARY", username="alice", settings_manager=settings
)
is None
)
assert _files_below(outside) == before
def test_configured_storage_root_receives_only_its_pdf_write(
isolated_data_dir, configured_storage_paths
):
paths, outside = configured_storage_paths
settings = _settings_for(paths["absolute"])
with patch(
"local_deep_research.utilities.db_utils.get_settings_manager",
return_value=settings,
):
library_root = get_library_storage_path("alice")
document = MagicMock()
content = b"%PDF-test"
relative, size = PDFStorageManager(library_root, "filesystem").save_pdf(
content, document, MagicMock(), "paper.pdf"
)
written = library_root / relative
assert library_root == Path(paths["absolute"]) / "alice"
assert written.resolve().is_relative_to(library_root / "pdfs")
assert written.read_bytes() == content
assert size == len(content)
assert document.file_path == relative
assert document.storage_mode == "filesystem"
assert _files_below(outside) == {
Path("CANARY"): b"do not alter",
written.relative_to(outside): content,
}
def test_relative_storage_path_still_creates_a_library(tmp_path, monkeypatch):
# Pin the eager get_library_directory() default as well as the configured root.
monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path / "ldr-data"))
workspace = tmp_path / "workspace"
workspace.mkdir()
monkeypatch.chdir(workspace)
settings = _settings_for("library")
with patch(
"local_deep_research.utilities.db_utils.get_settings_manager",
return_value=settings,
):
library_root = get_library_storage_path("alice")
assert library_root == workspace / "library" / "alice"
assert library_root.is_dir()
@pytest.mark.parametrize("output_dir", ["absolute", "relative"])
def test_benchmark_writer_cannot_escape_data_directory(
isolated_data_dir, tmp_path, monkeypatch, output_dir
):
"""The real runner must not create or append results outside LDR_DATA_DIR.
This was a strict xfail until #7417's fix: run_benchmark now resolves
output_dir inside the data directory and replaces an outside value with
``benchmark_results`` there.
"""
from local_deep_research.benchmarks import runners
outside = tmp_path / "outside"
outside.mkdir()
(outside / "CANARY").write_text("do not alter")
workspace = tmp_path / "workspace"
workspace.mkdir()
monkeypatch.chdir(workspace)
requested_output = (
str(outside / "benchmark-results")
if output_dir == "absolute"
else "../outside/benchmark-results"
)
before = _files_below(outside)
with (
patch.object(
runners.DatasetRegistry, "create_dataset", side_effect=RuntimeError
),
patch.object(
runners,
"load_dataset",
return_value=[{"id": "1", "problem": "q", "answer": "a"}],
),
patch.object(
runners,
"quick_summary",
return_value={"summary": "a", "sources": []},
),
):
result = runners.run_benchmark(
"simpleqa", output_dir=requested_output, run_evaluation=False
)
assert (
Path(result["results_path"])
.resolve()
.is_relative_to(isolated_data_dir.resolve())
)
assert _files_below(outside) == before