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(candidate-exploration): propagate Flask app context to progressive-explorer search workers (#5123)

Ehsande5790dca914
diff --git a/changelog.d/5079.bugfix.md b/changelog.d/5079.bugfix.md
new file mode 100644
index 0000000000..5f5c086e2a
--- /dev/null
+++ b/changelog.d/5079.bugfix.md
@@ -0,0 +1 @@
+Progressive-explorer searches now run inside the request's Flask app context. Previously `ProgressiveExplorer._parallel_search` dispatched searches to worker threads with no app context propagated, so any search touching `current_app`/`g` raised "Working outside of application context"; the error was swallowed per query and the explorer silently returned no results. It now passes `context_factory=thread_context` like the source-based and focused-iteration strategies.
diff --git a/src/local_deep_research/advanced_search_system/candidate_exploration/progressive_explorer.py b/src/local_deep_research/advanced_search_system/candidate_exploration/progressive_explorer.py
index 5d27fc131e..78bfab784d 100644
--- a/src/local_deep_research/advanced_search_system/candidate_exploration/progressive_explorer.py
+++ b/src/local_deep_research/advanced_search_system/candidate_exploration/progressive_explorer.py
@@ -8,6 +8,7 @@ from typing import Dict, List, Set, Tuple
from loguru import logger
from ...utilities.thread_context import preserve_research_context
+from ...utilities.threading_utils import thread_context
from ..parallel_search import run_parallel_searches
@@ -248,9 +249,19 @@ class ProgressiveExplorer:
# Create context-preserving wrapper for the search function
context_aware_search = preserve_research_context(search_query)
- # Run searches in parallel
+ # Run searches in parallel. thread_context (the factory, not a call)
+ # is invoked once per query so each worker thread gets its OWN fresh
+ # Flask app context, required so current_app / g stay accessible
+ # inside self.search_engine.run, matching SourceBasedSearchStrategy
+ # and FocusedIterationStrategy. Without it, a search that touches
+ # current_app raises "Working outside of application context" in the
+ # worker, which the per-query handler above swallows into an empty
+ # result.
completed = run_parallel_searches(
- queries, context_aware_search, max_workers=max_workers
+ queries,
+ context_aware_search,
+ max_workers=max_workers,
+ context_factory=thread_context,
)
results.extend(completed)
diff --git a/tests/advanced_search_system/candidate_exploration/test_progressive_explorer.py b/tests/advanced_search_system/candidate_exploration/test_progressive_explorer.py
index 3a0aed1a67..674f3cfbf1 100644
--- a/tests/advanced_search_system/candidate_exploration/test_progressive_explorer.py
+++ b/tests/advanced_search_system/candidate_exploration/test_progressive_explorer.py
@@ -597,3 +597,50 @@ class TestParallelSearch:
results = explorer._parallel_search(["query1"], max_workers=1)
assert results[0][1] == []
+
+ def test_propagates_flask_app_context_into_workers(self):
+ """Regression for #5079: workers must carry the Flask app context.
+
+ _parallel_search fans queries out across a ThreadPoolExecutor. If the
+ request's Flask app context is not propagated, any search that touches
+ ``current_app`` / ``g`` raises "Working outside of application context"
+ inside the worker; ``search_query`` swallows that into an empty result,
+ so the explorer silently returns nothing. Driven from inside a real
+ Flask request context, with a search that reads ``current_app``, every
+ query must come back with its result.
+
+ Before the fix (no ``context_factory``) this returns empty payloads
+ because each worker's ``current_app`` access fails. Passing
+ ``context_factory=thread_context`` returns both results.
+
+ The real context helpers are left unmocked here on purpose: they are
+ what carries the context across the thread boundary.
+ """
+ from flask import Flask, current_app
+ from local_deep_research.advanced_search_system.candidate_exploration.progressive_explorer import (
+ ProgressiveExplorer,
+ )
+
+ app = Flask(__name__)
+ app.config["LDR_MARKER"] = "reachable"
+
+ def run(query):
+ # Mirror the real search path reading a Flask context-local from
+ # inside the worker thread.
+ return [
+ {"title": query, "marker": current_app.config["LDR_MARKER"]}
+ ]
+
+ mock_engine = Mock()
+ mock_engine.run.side_effect = run
+
+ explorer = ProgressiveExplorer(mock_engine, Mock())
+
+ with app.test_request_context("/"):
+ results = explorer._parallel_search(["q1", "q2"], max_workers=2)
+
+ flattened = [item for _, payload in results for item in payload]
+
+ assert len(flattened) == 2
+ assert {item["title"] for item in flattened} == {"q1", "q2"}
+ assert all(item["marker"] == "reachable" for item in flattened)