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.

fix(langgraph): propagate search context (DB password) into subagent pool (#4751)

LearningCircuit7e7666ccf85b
diff --git a/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py b/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py
index 5692c0addd..c21c0d754e 100644
--- a/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py
+++ b/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py
@@ -19,6 +19,7 @@ from langchain_core.tools import tool
from langgraph.errors import GraphRecursionError
from loguru import logger
+from ...utilities.thread_context import get_search_context, search_context
from ...citation_handler import CitationHandler
from ..tools.fetch import FETCH_MODES, build_fetch_tool
from .base_strategy import BaseSearchStrategy
@@ -389,6 +390,16 @@ def _make_research_subtopic_tool(
logger.exception(f"Subagent failed for: {topic[:80]}")
return _scrub_tool_error(f"Research on '{topic}' failed: {exc}")
+ # Capture the lead thread's search context (it carries the user's DB
+ # password) so each pool worker can open the per-user ENCRYPTED database
+ # when a subagent re-creates a search engine / registers the user's
+ # document collections. stdlib ThreadPoolExecutor does NOT propagate the
+ # ContextVar — without this, a collection/library primary fails inside a
+ # subagent with "Unknown search engine 'collection_…'". This is the same
+ # gap sibling strategies (source_based, focused_iteration) close with
+ # @preserve_research_context; captured ONCE here on the lead thread.
+ captured_search_context = get_search_context()
+
def _run_subagent_with_egress(topic: str) -> str:
# threading.local is NOT inherited by ThreadPoolExecutor workers,
# so the PEP-578 audit-hook backstop armed on the parent thread is
@@ -401,6 +412,11 @@ def _make_research_subtopic_tool(
from ...security.egress.audit_hook import active_egress_context
with active_egress_context(egress_context):
+ # search_context sets the password ContextVar for this worker
+ # and clears it on exit (preventing leak across pooled tasks).
+ if captured_search_context is not None:
+ with search_context(captured_search_context):
+ return run_subagent(topic)
return run_subagent(topic)
ordered_results: dict[str, str] = {}
diff --git a/tests/strategies/test_langgraph_subagent_context.py b/tests/strategies/test_langgraph_subagent_context.py
new file mode 100644
index 0000000000..7bf070e05f
--- /dev/null
+++ b/tests/strategies/test_langgraph_subagent_context.py
@@ -0,0 +1,84 @@
+"""The LangGraph ``research_subtopic`` subagent pool must propagate the lead
+thread's search context (which carries the user's DB password) into its stdlib
+``ThreadPoolExecutor`` workers.
+
+Without it, a subagent that re-creates an engine / registers the user's
+document collections runs ``get_user_db_session`` with no password and fails to
+open the per-user ENCRYPTED database — surfacing as
+"Unknown search engine 'collection_…'". Sibling strategies close this gap with
+``@preserve_research_context``; this strategy captures the context on the lead
+thread and re-sets it per pool task.
+
+The test drives the REAL ``_make_research_subtopic_tool`` pool wiring; only the
+leaf subagent internals (``_make_web_search_tool``, ``build_fetch_tool``,
+``create_agent``) are stubbed, so the assertion is purely about what context a
+worker thread observes.
+"""
+
+from types import SimpleNamespace
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+import local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy as strat # noqa: E501
+from local_deep_research.utilities.thread_context import (
+ clear_search_context,
+ get_search_context,
+ set_search_context,
+)
+
+
[email protected](autouse=True)
+def _clean_search_context():
+ # Never let a context leak into or out of these tests.
+ clear_search_context()
+ yield
+ clear_search_context()
+
+
+def _invoke_one_subtopic(seen):
+ """Build the real research_subtopic tool with stubbed subagent internals,
+ then invoke it once. ``seen`` collects the search context observed inside
+ the pool worker (via the stubbed web-search tool factory)."""
+
+ def fake_web_search_tool(*_a, **_k):
+ # Runs INSIDE the pool worker — record the context it can see.
+ seen.append(get_search_context())
+ return SimpleNamespace(name="web_search")
+
+ agent = MagicMock()
+ agent.invoke.return_value = {"messages": [SimpleNamespace(content="ok")]}
+
+ with (
+ patch.object(strat, "_make_web_search_tool", fake_web_search_tool),
+ patch.object(strat, "build_fetch_tool", lambda *_a, **_k: None),
+ patch("langchain.agents.create_agent", return_value=agent),
+ ):
+ tool = strat._make_research_subtopic_tool(
+ search_engine_name="collection_abc",
+ model=MagicMock(),
+ settings_snapshot={"search.tool": "collection_abc"},
+ collector=MagicMock(),
+ max_sub_iterations=1,
+ )
+ tool.invoke({"subtopics": ["what is X?"]})
+
+
+class TestSubagentSearchContextPropagation:
+ def test_password_context_reaches_pool_worker(self):
+ # Lead thread has the user's DB password in the search context...
+ set_search_context({"user_password": "secret", "research_id": "r1"})
+ seen = []
+ _invoke_one_subtopic(seen)
+ # ...and the pool worker sees it (propagated), not None. This fails
+ # against the pre-fix code where the ContextVar was never carried in.
+ assert seen, "subagent web_search tool was never built"
+ assert seen[0] is not None
+ assert seen[0].get("user_password") == "secret"
+
+ def test_no_context_does_not_crash(self):
+ # No lead-thread context (programmatic/no-encryption): the pool worker
+ # simply sees None and the run proceeds without error.
+ seen = []
+ _invoke_one_subtopic(seen)
+ assert seen and seen[0] is None