~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
"""`get_settings_manager` must tell "no database" apart from "something broke".
The fallback to anonymous defaults is correct for exactly one condition: a
resolved user who has no database. It used to be reached by `except
RuntimeError`, which also caught two conditions where defaults are not a
degraded answer but a wrong one:
* the background-thread guard in `get_db_session`, where returning defaults
means a worker silently reads a configuration that is not the user's; and
* any failure inside `db_manager.get_session` -- a corrupt encrypted database,
a keyring error -- where every setting silently reads as its default. A
setting the user switched *on* reads as off, and nothing is logged.
This branch changed which of those can occur: the guard moved from Flask's
`has_app_context()` to a contextvar, so the raise surface under that unchanged
`except` is not the one it was written against. These tests pin the
discrimination itself rather than any one message.
"""
import threading
from unittest.mock import patch
import pytest
from local_deep_research.exceptions import NoUserDatabaseError
from local_deep_research.utilities import db_utils
class TestOnlyTheNoDatabaseCaseFallsBack:
def test_a_user_without_a_database_gets_anonymous_defaults(self):
"""The one case the fallback is for. It must keep working."""
with patch.object(
db_utils,
"get_db_session",
side_effect=NoUserDatabaseError("No database found for user bob"),
):
manager = db_utils.get_settings_manager(username="bob")
assert manager is not None, (
"a user with no database must still get a settings manager on "
"defaults -- this is the degraded path, not an error path"
)
def test_a_broken_database_is_not_reported_as_defaults(self):
"""A corrupt/unopenable database must not read as 'all defaults'.
This is the dangerous one: silently substituting defaults means a
setting the user turned on reads as off, with nothing logged.
"""
boom = RuntimeError("file is not a database")
with patch.object(db_utils, "get_db_session", side_effect=boom):
with pytest.raises(RuntimeError, match="file is not a database"):
db_utils.get_settings_manager(username="bob")
def test_the_background_thread_guard_is_not_swallowed(self):
"""A worker with no request context must fail, not get defaults.
The codebase warns in several places that ambient
`get_settings_manager()` is unsafe from workers (#3453). Swallowing the
guard is what made that unsafety silent.
"""
guard = RuntimeError(
"Database access attempted from background thread 'Worker-1' "
"(ID: 123) with no request context."
)
with patch.object(db_utils, "get_db_session", side_effect=guard):
with pytest.raises(RuntimeError, match="background thread"):
db_utils.get_settings_manager(username="bob")
class TestTheExceptionKeepsItsOldContract:
def test_it_is_still_a_runtime_error(self):
"""Existing callers and tests catch RuntimeError; that must hold.
`tests/utilities/test_db_utils.py` matches on RuntimeError("No database
found"). Narrowing the raise to a subclass is only safe because of this.
"""
assert issubclass(NoUserDatabaseError, RuntimeError)
def test_the_real_call_path_raises_the_narrow_type(self):
"""Not just the type -- the site that means "no database" uses it."""
with patch.object(
db_utils.db_manager, "get_session", return_value=None
):
with pytest.raises(NoUserDatabaseError, match="No database found"):
db_utils._get_cached_user_session(
"nodbuser", f"probe-{threading.get_ident()}"
)