~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
"""Real agent-loop and search-tool regressions for engine availability."""
import json
import time
from concurrent.futures import ThreadPoolExecutor
from importlib import resources
from types import SimpleNamespace
from threading import Barrier, Event, Lock, get_ident
from unittest.mock import Mock, patch
import pytest
import requests
from langchain.agents import create_agent
from langchain.agents.middleware import ModelRequest
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import (
AIMessage,
HumanMessage,
SystemMessage,
ToolMessage,
)
from langchain_core.tools import tool as make_langchain_tool
from langchain_core.outputs import ChatGeneration, ChatResult
from pydantic import Field
from local_deep_research.advanced_search_system.strategies import (
langgraph_agent_strategy as strategy_module,
)
from local_deep_research.advanced_search_system.tools.search_availability import (
NoResearchToolsAvailable,
SearchAvailabilityMiddleware,
run_search,
wait_for_cooldown,
)
from local_deep_research.exceptions import ResearchTerminatedException
from local_deep_research.web_search_engines.engine_availability import (
SearchEngineAvailability,
SearchFailure,
)
from local_deep_research.web_search_engines.rate_limiting import RateLimitError
from local_deep_research.web_search_engines.search_engine_base import (
BaseSearchEngine,
)
FACTORY = "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
class ScriptedModel(BaseChatModel):
responses: list
seen_tools: list = Field(default_factory=list)
@property
def _llm_type(self):
return "availability-test"
def bind_tools(self, tools, **kwargs):
return self.bind(test_tool_names=[t.name for t in tools])
def _generate(self, messages, stop=None, run_manager=None, **kwargs):
self.seen_tools.append(kwargs.get("test_tool_names", []))
return ChatResult(
generations=[ChatGeneration(message=self.responses.pop(0))]
)
def make_tool(name, tracker, model=None, collector=None):
collector = collector or strategy_module.SearchResultsCollector([])
if name == "web_search":
return strategy_module._make_web_search_tool(
"searxng", model or Mock(), {}, collector, availability=tracker
)
return strategy_module._make_specialized_search_tool(
name.removeprefix("search_"),
"Search a provider",
model or Mock(),
{},
collector,
availability=tracker,
)
@pytest.fixture
def clock():
return SimpleNamespace(now=0.0)
@pytest.fixture
def tracker(clock):
return SearchEngineAvailability(clock=lambda: clock.now)
@pytest.mark.parametrize("name", ["web_search", "search_searxng"])
def test_disabled_engine_is_not_recreated_by_repeated_tool_calls(name, tracker):
engine = Mock(_is_available=False)
engine.last_search_failure = SearchFailure("URL blocked", None)
with patch(FACTORY, return_value=engine) as factory:
tool = make_tool(name, tracker)
first = tool.invoke({"query": "one"})
second = tool.invoke({"query": "two"})
assert "URL blocked" in first
assert "disabled since" in second
assert "Try rephrasing" not in second
factory.assert_called_once()
engine.run.assert_not_called()
engine.close.assert_called_once()
def test_concurrent_tools_share_failure_and_skip_engine_creation(tracker):
engine = Mock(_is_available=False)
engine.last_search_failure = SearchFailure("URL blocked", None)
progress = Mock()
tracker._on_change = progress
tools = [make_tool("web_search", tracker) for _ in range(4)]
with patch(FACTORY, return_value=engine) as factory:
with ThreadPoolExecutor(max_workers=4) as pool:
results = list(pool.map(lambda t: t.invoke({"query": "q"}), tools))
# Calls already in flight may each have built the engine; the
# shared failure is recorded once and stops every later call.
calls = factory.call_count
assert "disabled since" in tools[0].invoke({"query": "later"})
assert all("disabled" in result for result in results)
assert 1 <= calls <= 4
assert factory.call_count == calls
progress.assert_called_once()
def test_zero_results_remain_available(tracker):
engine = Mock()
engine.run.return_value = []
with patch(FACTORY, return_value=engine) as factory:
tool = make_tool("web_search", tracker)
assert "Try rephrasing" in tool.invoke({"query": "one"})
assert "Try rephrasing" in tool.invoke({"query": "two"})
assert factory.call_count == 2
assert tracker.disabled() == {}
def test_partial_results_survive_optional_request_failure(tracker):
engine = Mock()
engine.last_search_failure = None
def search(query):
engine.last_search_failure = SearchFailure("Optional request failed")
return [{"title": "Useful result"}]
engine.run.side_effect = search
assert run_search("pubmed", "q", lambda: engine, tracker) == [
{"title": "Useful result"}
]
assert tracker.disabled() == {}
def test_constructor_exception_is_contained_and_cooled_down(tracker):
factory = Mock(side_effect=ConnectionError("secret"))
assert "connect" in run_search("searxng", "q", factory, tracker)
assert "secret" not in run_search("searxng", "q", factory, tracker)
factory.assert_called_once()
def test_model_schema_changes_after_failure_and_cooldown(tracker, clock):
model = ScriptedModel(
responses=[
AIMessage(
content="",
tool_calls=[
{"name": "web_search", "args": {"query": "q"}, "id": "1"}
],
),
AIMessage(
content="",
tool_calls=[
{"name": "search_pubmed", "args": {"query": "q"}, "id": "2"}
],
),
AIMessage(content="Findings [1]"),
]
)
tools = [
make_tool(name, tracker, model)
for name in ["web_search", "search_pubmed"]
]
def factory(*, engine_name, **kwargs):
engine = Mock()
if engine_name == "searxng":
engine.run.side_effect = RateLimitError("limit", retry_after=300)
else:
def search(query):
clock.now = 300
return [{"title": "Result", "link": "https://example.org"}]
engine.run.side_effect = search
return engine
with patch(FACTORY, side_effect=factory):
agent = create_agent(
model=model,
tools=tools,
middleware=[SearchAvailabilityMiddleware(tracker, "searxng")],
)
result = agent.invoke(
{"messages": [{"role": "user", "content": "Research"}]}
)
assert result["messages"][-1].content == "Findings [1]"
assert model.seen_tools == [
["web_search", "search_pubmed"],
["search_pubmed"],
["web_search", "search_pubmed"],
]
def test_all_engines_disabled_never_calls_model_without_tools(tracker):
"""Even without history, an empty tool list would mean memory answers."""
tracker.disable("searxng", SearchFailure("blocked", None))
model = ScriptedModel(responses=[AIMessage(content="From memory")])
tool = make_tool("web_search", tracker, model)
agent = create_agent(
model=model,
tools=[tool],
middleware=[SearchAvailabilityMiddleware(tracker, "searxng")],
)
with pytest.raises(NoResearchToolsAvailable, match="blocked"):
agent.invoke({"messages": [{"role": "user", "content": "Research"}]})
assert model.seen_tools == []
def test_subagent_uses_shared_availability_in_real_model_call(tracker):
tracker.disable("searxng", SearchFailure("blocked", None))
model = ScriptedModel(responses=[AIMessage(content="Subtopic findings")])
with (
patch.object(strategy_module, "build_fetch_tool", return_value=None),
patch.object(
strategy_module,
"_load_specialized_engine_tools",
return_value=[make_tool("search_pubmed", tracker)],
),
):
tool = strategy_module._make_research_subtopic_tool(
"searxng",
model,
{},
strategy_module.SearchResultsCollector([]),
3,
availability=tracker,
)
assert "Subtopic findings" in tool.invoke({"subtopics": ["one"]})
assert model.seen_tools == [["fetch_archive", "search_pubmed"]]
def test_heartbeat_shows_only_working_choices_and_explains_disabled(
tracker, clock
):
strategy = strategy_module.LangGraphAgentStrategy(
model=Mock(),
search=Mock(),
settings_snapshot={"search.tool": "searxng"},
all_links_of_system=[{"link": "https://example.org"}],
)
strategy.engine_availability = tracker
strategy._tool_names = ["web_search", "search_pubmed", "fetch_content"]
strategy.collector.record_fetch()
tracker.disable("searxng", SearchFailure("URL blocked", 300))
message = strategy._heartbeat_message(3)
active, disabled = message.split(" · Disabled engines: ")
assert "PubMed" in active and "page fetching" in active
assert "1 page fetch attempt" in active
assert "SearXNG" not in active
assert "SearXNG" in disabled and "URL blocked" in disabled
assert "for 300s" in disabled and "retry available at" in disabled
clock.now = 300
assert "SearXNG" in strategy._heartbeat_message(4)
assert "Disabled engines" not in strategy._heartbeat_message(4)
def test_primary_known_failure_is_seeded_before_model_selection():
strategy = strategy_module.LangGraphAgentStrategy(
model=Mock(),
search=Mock(last_search_failure=SearchFailure("URL blocked", None)),
settings_snapshot={"search.tool": "searxng"},
)
with (
patch.object(strategy, "_build_egress_context", return_value=None),
patch.object(strategy, "_build_library_resolver", return_value=None),
patch.object(
strategy_module, "_load_specialized_engine_tools", return_value=[]
),
):
strategy._build_tools()
assert "searxng" in strategy.engine_availability.disabled()
@pytest.mark.asyncio
async def test_async_middleware_filters_and_preserves_system_metadata(tracker):
tracker.disable("searxng", SearchFailure("blocked", None))
request = ModelRequest(
model=Mock(),
messages=[],
tools=[
make_tool("web_search", tracker),
make_tool("search_pubmed", tracker),
],
system_message=SystemMessage(
content=[{"type": "text", "text": "Research"}],
additional_kwargs={"test_metadata": True},
),
)
captured = []
async def handler(filtered):
captured.append(filtered)
return "answer"
result = await SearchAvailabilityMiddleware(
tracker, "searxng"
).awrap_model_call(request, handler)
assert result == "answer"
assert [t.name for t in captured[0].tools] == ["search_pubmed"]
assert captured[0].system_message.additional_kwargs == {
"test_metadata": True
}
assert len(request.system_message.content) == 1
assert len(captured[0].system_message.content) == 2
def test_disabled_states_do_not_reveal_policy_filtered_engines(tracker):
tracker.disable("private_collection", SearchFailure("private", None))
request = ModelRequest(
model=Mock(), messages=[], tools=[make_tool("search_pubmed", tracker)]
)
middleware = SearchAvailabilityMiddleware(tracker, "searxng")
assert middleware.wrap_model_call(request, lambda r: r) is request
@pytest.mark.parametrize("cooldown", [None, 300])
def test_stale_delegation_does_not_start_workers_without_usable_tools(
tracker, cooldown
):
tracker.disable("searxng", SearchFailure("URL blocked", cooldown))
progress = Mock()
with (
patch.object(strategy_module, "build_fetch_tool", return_value=None),
patch.object(
strategy_module, "_load_specialized_engine_tools", return_value=[]
),
patch("langchain.agents.create_agent") as create_subagent,
):
tool = strategy_module._make_research_subtopic_tool(
"searxng",
Mock(),
{},
strategy_module.SearchResultsCollector([]),
3,
availability=tracker,
progress_callback=progress,
)
result = tool.invoke({"subtopics": ["one", "two"]})
assert "research_subtopic is unavailable" in result
assert "URL blocked" in result
assert "disabled since" in result
create_subagent.assert_not_called()
progress.assert_not_called()
@pytest.mark.parametrize("cancel_from", ["search", "progress"])
def test_cancellation_propagates_and_still_closes_engine(cancel_from):
progress = Mock()
tracker = SearchEngineAvailability(progress)
engine = Mock(last_search_failure=None)
if cancel_from == "search":
engine.run.side_effect = ResearchTerminatedException("cancelled")
else:
engine.run.side_effect = ConnectionError("offline")
progress.side_effect = ResearchTerminatedException("cancelled")
with pytest.raises(ResearchTerminatedException):
run_search("searxng", "q", lambda: engine, tracker)
engine.close.assert_called_once()
if cancel_from == "search":
assert tracker.disabled() == {}
progress.assert_not_called()
else:
progress.assert_called_once()
def test_healthy_searches_are_not_serialized(tracker):
healthy = Mock(last_search_failure=None)
healthy.run.return_value = []
assert run_search("pubmed", "probe", lambda: healthy, tracker) == []
simultaneous = Barrier(2, timeout=5)
def factory():
engine = Mock(last_search_failure=None)
def search(query):
simultaneous.wait()
return [{"title": query}]
engine.run.side_effect = search
return engine
with ThreadPoolExecutor(max_workers=2) as pool:
results = list(
pool.map(
lambda query: run_search("pubmed", query, factory, tracker),
["one", "two"],
)
)
assert results == [[{"title": "one"}], [{"title": "two"}]]
assert tracker.disabled() == {}
def test_failed_parallel_retries_after_cooldown_disable_once(clock):
progress = Mock()
tracker = SearchEngineAvailability(progress, clock=lambda: clock.now)
tracker.disable("pubmed", SearchFailure("offline", 60))
clock.now = 60
factory = Mock(side_effect=ConnectionError("still offline"))
with ThreadPoolExecutor(max_workers=4) as pool:
results = list(
pool.map(
lambda _: run_search("pubmed", "retry", factory, tracker),
range(4),
)
)
assert all("disabled since" in result for result in results)
statuses = [call.args[2]["status"] for call in progress.call_args_list]
# First disable, its expiry, then exactly one new disable.
assert statuses == ["disabled", "retry_available", "disabled"]
def test_tools_exhausted_after_tool_use_stops_instead_of_empty_tool_list(
tracker,
):
"""Tool-call history sent with no tools is rejected by some providers."""
model = ScriptedModel(
responses=[
AIMessage(
content="",
tool_calls=[
{"name": "web_search", "args": {"query": "q"}, "id": "1"}
],
),
AIMessage(content="must not be requested"),
]
)
engine = Mock(_is_available=False)
engine.last_search_failure = SearchFailure("URL blocked", None)
with patch(FACTORY, return_value=engine):
agent = create_agent(
model=model,
tools=[make_tool("web_search", tracker, model)],
middleware=[SearchAvailabilityMiddleware(tracker, "searxng")],
)
with pytest.raises(NoResearchToolsAvailable, match="disabled since"):
agent.invoke({"messages": [{"role": "user", "content": "q"}]})
assert model.seen_tools == [["web_search"]]
def test_transient_failure_on_the_only_engine_waits_instead_of_ending(
tracker, clock
):
"""A cooldown is not a run-long disable: wait, then search again."""
model = ScriptedModel(
responses=[
AIMessage(
content="",
tool_calls=[
{"name": "web_search", "args": {"query": "q"}, "id": "1"}
],
),
AIMessage(
content="",
tool_calls=[
{"name": "web_search", "args": {"query": "q"}, "id": "2"}
],
),
AIMessage(content="Findings [1]"),
]
)
engine = Mock(last_search_failure=None)
engine.run.side_effect = [
RateLimitError("limit", retry_after=300),
[{"title": "Result", "link": "https://example.org"}],
]
waits = []
def wait(engines):
waits.append((engines, tracker.seconds_until_retry(engines)))
clock.now += tracker.seconds_until_retry(engines)
with patch(FACTORY, return_value=engine):
agent = create_agent(
model=model,
tools=[make_tool("web_search", tracker, model)],
middleware=[
SearchAvailabilityMiddleware(tracker, "searxng", wait=wait)
],
)
result = agent.invoke({"messages": [{"role": "user", "content": "q"}]})
assert result["messages"][-1].content == "Findings [1]"
assert waits == [({"searxng"}, 300)]
assert model.seen_tools == [["web_search"]] * 3
@pytest.mark.parametrize("fetch_name", ["fetch_content", "fetch_archive"])
def test_cooling_engines_are_waited_for_even_when_fetch_survives(
tracker, clock, fetch_name
):
"""Fetch alone cannot find sources; a cooldown ends before it helps."""
@make_langchain_tool(fetch_name)
def fetch_content(url: str) -> str:
"""Fetch."""
return ""
tracker.disable("searxng", SearchFailure("offline", 60))
tracker.disable("pubmed", SearchFailure("Rate limit", 300))
request = ModelRequest(
model=Mock(),
messages=[HumanMessage(content="Research")],
tools=[
make_tool("web_search", tracker),
make_tool("search_pubmed", tracker),
fetch_content,
],
)
waited = []
def wait(engines):
waited.append(engines)
clock.now = 60
captured = []
SearchAvailabilityMiddleware(tracker, "searxng", wait=wait).wrap_model_call(
request, captured.append
)
assert waited == [{"searxng", "pubmed"}]
assert [t.name for t in captured[0].tools] == [
"web_search",
fetch_name,
]
def test_archive_only_cooldown_waits_for_wayback(tracker, clock):
@make_langchain_tool
def fetch_archive(url: str) -> str:
"""Read an archive capture."""
return ""
tracker.disable("wayback", SearchFailure("Archive unavailable", 60))
request = ModelRequest(model=Mock(), messages=[], tools=[fetch_archive])
waited = []
def wait(engines):
waited.append(engines)
clock.now = 60
captured = []
SearchAvailabilityMiddleware(tracker, "searxng", wait=wait).wrap_model_call(
request, captured.append
)
assert waited == [{"wayback"}]
assert [t.name for t in captured[0].tools] == ["fetch_archive"]
def test_archive_outage_keeps_search_available_without_waiting(tracker):
@make_langchain_tool
def fetch_archive(url: str) -> str:
"""Read an archive capture."""
return ""
tracker.disable("wayback", SearchFailure("Archive unavailable", 60))
request = ModelRequest(
model=Mock(),
messages=[],
tools=[make_tool("web_search", tracker), fetch_archive],
)
wait = Mock()
captured = []
SearchAvailabilityMiddleware(tracker, "searxng", wait=wait).wrap_model_call(
request, captured.append
)
wait.assert_not_called()
assert [t.name for t in captured[0].tools] == ["web_search"]
assert "Archive unavailable" in captured[0].system_message.content
def test_archive_recovers_after_discovery_is_permanently_disabled(
tracker, clock
):
@make_langchain_tool
def fetch_archive(url: str) -> str:
"""Read an archive capture."""
return ""
tracker.disable("searxng", SearchFailure("Search unavailable", None))
tracker.disable("wayback", SearchFailure("Archive unavailable", 60))
request = ModelRequest(
model=Mock(),
messages=[HumanMessage(content="Read https://example.org in 2020")],
tools=[make_tool("web_search", tracker), fetch_archive],
)
waited = []
def wait(engines):
waited.append(engines)
clock.now = 60
captured = []
SearchAvailabilityMiddleware(tracker, "searxng", wait=wait).wrap_model_call(
request, captured.append
)
assert waited == [{"searxng", "wayback"}]
assert [t.name for t in captured[0].tools] == ["fetch_archive"]
assert "Search unavailable" in captured[0].system_message.content
def test_default_wait_sleeps_until_the_soonest_cooldown(tracker, clock):
tracker.disable("searxng", SearchFailure("offline", 3))
sleeps = []
def sleep(seconds):
sleeps.append(seconds)
clock.now += seconds
from local_deep_research.advanced_search_system.tools import (
search_availability,
)
with patch.object(
search_availability, "time", SimpleNamespace(sleep=sleep)
):
request = ModelRequest(
model=Mock(), messages=[], tools=[make_tool("web_search", tracker)]
)
captured = []
SearchAvailabilityMiddleware(tracker, "searxng").wrap_model_call(
request, captured.append
)
assert sum(sleeps) == pytest.approx(3)
assert max(sleeps) <= 1.0
assert [t.name for t in captured[0].tools] == ["web_search"]
@pytest.mark.parametrize("keep_fetch", [False, True])
def test_concurrent_redisable_keeps_waiting_within_run_budget(
tracker, clock, keep_fetch
):
"""A second outage must not end research while its retry fits the budget."""
tracker.disable("searxng", SearchFailure("first outage", 300))
tools = [make_tool("web_search", tracker)]
if keep_fetch:
@make_langchain_tool
def fetch_content(url: str) -> str:
"""Fetch a known page."""
return ""
tools.append(fetch_content)
request = ModelRequest(model=Mock(), messages=[], tools=tools)
re_disabled = False
sleeps = []
def sleep(seconds):
nonlocal re_disabled
sleeps.append(seconds)
clock.now += seconds
if clock.now >= 300 and not re_disabled:
re_disabled = True
# Another in-flight search fails as the first cooldown ends.
tracker.disable("searxng", SearchFailure("second outage", 600))
captured = []
middleware = SearchAvailabilityMiddleware(
tracker,
"searxng",
wait=lambda engines: wait_for_cooldown(tracker, engines, sleep=sleep),
)
middleware.wrap_model_call(request, captured.append)
assert re_disabled
assert sum(sleeps) == pytest.approx(900)
assert [tool.name for tool in captured[0].tools] == [
tool.name for tool in tools
]
assert tracker.disabled() == {}
def test_research_subtopic_alone_is_not_sent_as_an_empty_tool_list(tracker):
"""Delegation cannot help once its sub-tools are disabled too."""
@make_langchain_tool
def research_subtopic(subtopics: list[str]) -> str:
"""Delegate."""
return ""
tracker.disable("searxng", SearchFailure("URL blocked", None))
request = ModelRequest(
model=Mock(),
messages=[
HumanMessage(content="Research"),
AIMessage(
content="",
tool_calls=[
{"name": "web_search", "args": {"query": "q"}, "id": "1"}
],
),
ToolMessage(content="failed", tool_call_id="1"),
],
tools=[make_tool("web_search", tracker), research_subtopic],
)
handler = Mock()
with pytest.raises(NoResearchToolsAvailable, match="URL blocked"):
SearchAvailabilityMiddleware(tracker, "searxng").wrap_model_call(
request, handler
)
handler.assert_not_called()
@pytest.mark.parametrize("has_sources", [True, False])
def test_strategy_ends_research_when_tools_are_exhausted(has_sources):
strategy = strategy_module.LangGraphAgentStrategy(
model=Mock(),
search=Mock(),
settings_snapshot={"search.tool": "searxng"},
)
agent = Mock()
def stream(*args, **kwargs):
# analyze_topic resets the collector first; gather during the run.
if has_sources:
strategy.collector.add_results(
[{"title": "Result", "link": "https://example.org"}]
)
raise NoResearchToolsAvailable(
"No research tools remain available:\nsearxng disabled"
)
agent.stream.side_effect = stream
with (
patch.object(strategy, "_build_tools", return_value=[Mock()]),
patch("langchain.agents.create_agent", return_value=agent),
patch.object(
strategy, "_synthesize_from_collector", return_value="Answer [1]"
) as synthesize,
patch.object(strategy, "_finalize", return_value={"ok": True}) as done,
):
result = strategy.analyze_topic("query")
if has_sources:
assert result == {"ok": True}
synthesize.assert_called_once()
assert done.call_args.args[1] == "Answer [1]"
else:
assert "Research stopped" in result["error"]
assert "searxng disabled" in result["error"]
synthesize.assert_not_called()
@pytest.mark.parametrize("own_sources", [True, False])
def test_subagent_stops_cleanly_when_tools_are_exhausted(tracker, own_sources):
collector = strategy_module.SearchResultsCollector([])
# Another agent's source must not make this subagent claim it kept any.
collector.add_results(
[{"title": "Earlier", "link": "https://example.org/earlier"}]
)
calls = [
AIMessage(
content="",
tool_calls=[
{"name": "search_pubmed", "args": {"query": "q"}, "id": str(i)}
],
)
for i in range(2 if own_sources else 1)
]
model = ScriptedModel(
responses=[*calls, AIMessage(content="must not be requested")]
)
denied = requests.HTTPError(
response=SimpleNamespace(status_code=401, headers={})
)
engine = Mock(last_search_failure=None)
engine.run.side_effect = (
[[{"title": "Own", "link": "https://example.org/own"}], denied]
if own_sources
else [denied]
)
with (
patch(FACTORY, return_value=engine),
patch.object(strategy_module, "build_fetch_tool", return_value=None),
patch.object(
strategy_module,
"_load_specialized_engine_tools",
return_value=[
make_tool("search_pubmed", tracker, model, collector)
],
),
):
tool = strategy_module._make_research_subtopic_tool(
"searxng",
model,
{"search.engine.web.wayback.agent_enabled": False},
collector,
3,
search_enabled=False,
availability=tracker,
)
result = tool.invoke({"subtopics": ["one"]})
# Only claim kept sources when this subagent gathered some.
assert ("sources gathered so far are kept" in result) is own_sources
assert ("stopped before any sources" in result) is not own_sources
assert "must not be requested" not in result
assert model.seen_tools == [["search_pubmed"]] * len(calls)
def test_primary_failure_without_engine_name_is_not_recorded():
strategy = strategy_module.LangGraphAgentStrategy(
model=Mock(),
search=Mock(last_search_failure=SearchFailure("URL blocked", None)),
settings_snapshot={"search.tool": "searxng"},
)
strategy._search_engine_name = None
with (
patch.object(strategy, "_build_egress_context", return_value=None),
patch.object(strategy, "_build_library_resolver", return_value=None),
patch.object(
strategy_module, "_load_specialized_engine_tools", return_value=[]
),
):
strategy._build_tools()
assert strategy.engine_availability.disabled() == {}
def test_search_exception_is_logged_by_type_only(tracker):
factory = Mock(side_effect=ConnectionError("https://x/?api_key=SECRET"))
with patch(
"local_deep_research.advanced_search_system.tools."
"search_availability.logger"
) as log:
run_search("searxng", "q", factory, tracker)
log.debug.assert_called_once()
log.warning.assert_not_called()
message = log.debug.call_args.args[0]
assert "ConnectionError" in message
assert "SECRET" not in message
def test_parallel_first_searches_of_an_engine_run_concurrently(tracker):
"""No first-call probe lock: a slow first search blocks nobody."""
started, release = Event(), Event()
def slow_first():
started.set()
# Bounded so a serializing regression fails instead of hanging.
assert release.wait(5)
engine = Mock(last_search_failure=None)
engine.run.return_value = [{"title": "slow"}]
return engine
fast = Mock(last_search_failure=None)
fast.run.return_value = [{"title": "fast"}]
with ThreadPoolExecutor(max_workers=1) as pool:
slow = pool.submit(run_search, "pubmed", "slow", slow_first, tracker)
assert started.wait(5)
# Runs to completion while the first search is still in flight.
assert run_search("pubmed", "fast", lambda: fast, tracker) == [
{"title": "fast"}
]
release.set()
assert slow.result(5) == [{"title": "slow"}]
assert tracker.disabled() == {}
def test_detailed_report_section_with_every_engine_disabled_stops(tracker):
"""A later section starts without history; it must not answer from memory."""
model = ScriptedModel(responses=[AIMessage(content="From memory")])
strategy = strategy_module.LangGraphAgentStrategy(
model=model,
search=Mock(last_search_failure=None),
settings_snapshot={"search.tool": "searxng"},
)
# The same strategy (and tracker) serves every section of the report;
# an earlier section disabled the only engine.
strategy.engine_availability = tracker
tracker.disable("searxng", SearchFailure("URL blocked", None))
with patch.object(
strategy,
"_build_tools",
return_value=[make_tool("web_search", tracker, model)],
):
result = strategy.analyze_topic("Section two")
assert (
"Research stopped before any sources were gathered" in result["error"]
)
assert "URL blocked" in result["error"]
assert model.seen_tools == []
def test_fetch_survives_when_every_search_engine_is_disabled(tracker):
"""Fetch-only is not terminal: it can still read URLs already known."""
@make_langchain_tool
def fetch_content(url: str) -> str:
"""Fetch."""
return ""
tracker.disable("searxng", SearchFailure("URL blocked", None))
request = ModelRequest(
model=Mock(),
messages=[HumanMessage(content="Summarize https://example.org")],
tools=[make_tool("web_search", tracker), fetch_content],
)
captured = []
SearchAvailabilityMiddleware(tracker, "searxng").wrap_model_call(
request, captured.append
)
assert [t.name for t in captured[0].tools] == ["fetch_content"]
assert "URL blocked" in captured[0].system_message.content
def _cooldown_strategy(clock, tracker):
strategy = strategy_module.LangGraphAgentStrategy(
model=Mock(),
search=Mock(last_search_failure=None),
settings_snapshot={"search.tool": "searxng"},
)
strategy.engine_availability = tracker
sleeps = []
hooks = []
class FakeTime:
"""Stands in for the strategy module's ``time`` only; sleeps advance
the tracker clock. Everything else is the real module."""
def __getattr__(self, name):
return getattr(time, name)
def sleep(self, seconds):
sleeps.append(seconds)
clock.now += seconds
for hook in hooks:
hook()
fake_time = FakeTime()
fake_time.hooks = hooks
return strategy, sleeps, fake_time
@pytest.mark.parametrize(
"cooldown,waits", [(30, True), (300, True), (600, True), (None, False)]
)
def test_waits_out_any_cooldown_but_not_a_run_long_disable(
tracker, clock, cooldown, waits
):
strategy, sleeps, fake_time = _cooldown_strategy(clock, tracker)
tracker.disable("searxng", SearchFailure("offline", cooldown))
with patch.object(strategy_module, "time", fake_time):
strategy._wait_for_cooldown({"searxng"})
assert bool(sleeps) is waits
if waits:
assert max(sleeps) <= 1.0
assert sum(sleeps) == pytest.approx(cooldown, abs=0.05)
assert tracker.disabled() == {}
else:
assert "searxng" in tracker.disabled()
def test_does_not_wait_while_another_engine_works(tracker, clock):
strategy, sleeps, fake_time = _cooldown_strategy(clock, tracker)
tracker.disable("searxng", SearchFailure("offline", 30))
with (
patch.object(strategy_module, "time", fake_time),
patch.object(strategy, "_update_progress") as progress,
):
strategy._wait_for_cooldown({"searxng", "pubmed"})
assert sleeps == []
progress.assert_not_called()
def test_cooldown_wait_honours_cancellation(tracker, clock):
strategy, sleeps, fake_time = _cooldown_strategy(clock, tracker)
tracker.disable("searxng", SearchFailure("offline", 30))
checks = []
def check_termination(context=None):
checks.append(context)
if len(checks) == 3:
raise ResearchTerminatedException("cancelled")
strategy.check_termination = check_termination
with (
patch.object(strategy_module, "time", fake_time),
pytest.raises(ResearchTerminatedException),
):
strategy._wait_for_cooldown({"searxng"})
assert len(sleeps) == 2
assert set(checks) == {strategy_module.CHECK_CONTEXT_ENTRY}
@pytest.mark.parametrize("cooldown", [300, None])
def test_cooldown_wait_is_bounded_when_engine_is_disabled_again(
tracker, clock, cooldown
):
"""A re-disable mid-wait can extend the wait, never past the run budget.
Another worker's retry fails as each cooldown ends. A temporary
re-disable keeps the waiter waiting for as long as its next retry still
fits the remaining wait budget; a run-long one ends the wait at once.
"""
strategy, sleeps, fake_time = _cooldown_strategy(clock, tracker)
first_wait = 30
tracker.disable("searxng", SearchFailure("offline", first_wait))
bound = tracker.wait_budget_remaining()
def disable_again():
# Another worker's retry fails right as the cooldown ends.
if sum(sleeps) > bound + 1: # an unbounded wait fails, not spins
raise AssertionError("cooldown wait is unbounded")
state = tracker.disabled().get("searxng")
if state is None:
tracker.disable("searxng", SearchFailure("Rate limit", cooldown))
fake_time.hooks.append(disable_again)
with patch.object(strategy_module, "time", fake_time):
strategy._wait_for_cooldown({"searxng"})
if cooldown is None:
expected = first_wait
else:
expected = first_wait + cooldown * ((bound - first_wait) // cooldown)
assert sum(sleeps) == pytest.approx(expected, abs=1)
assert sum(sleeps) <= bound
assert "searxng" in tracker.disabled()
def test_section_start_waits_then_searches(tracker, clock):
"""End to end: the section's first model call gets web_search back."""
model = ScriptedModel(responses=[AIMessage(content="Answer")])
strategy, sleeps, fake_time = _cooldown_strategy(clock, tracker)
strategy.model = model
tracker.disable("searxng", SearchFailure("offline", 20))
with (
patch.object(strategy_module, "time", fake_time),
patch.object(
strategy,
"_build_tools",
return_value=[make_tool("web_search", tracker, model)],
),
patch.object(strategy, "_finalize", return_value={"ok": True}),
):
assert strategy.analyze_topic("Section") == {"ok": True}
assert sum(sleeps) == pytest.approx(20, abs=0.05)
assert model.seen_tools == [["web_search"]]
RECORD = (
"local_deep_research.metrics.search_tracker.SearchTracker.record_search"
)
def test_skipped_search_is_recorded_in_metrics_with_reason(tracker):
"""Outages must stay visible in metrics although nothing is sent."""
engine = Mock(_is_available=False)
engine.last_search_failure = SearchFailure("URL blocked", None)
with patch(RECORD) as record:
run_search("pubmed", "first", lambda: engine, tracker)
record.assert_not_called() # the engine's own run() records failures
run_search("pubmed", "second", Mock(), tracker)
record.assert_called_once()
kwargs = record.call_args.kwargs
assert kwargs["status"] == "skipped_unavailable"
assert kwargs["query"] == "second"
assert kwargs["success"] is False
assert "URL blocked" in kwargs["error_message"]
def test_skipped_search_uses_the_engines_metrics_name(tracker):
class SemanticScholarSearchEngine(BaseSearchEngine):
def __init__(self):
self.last_search_failure = SearchFailure("Denied", None)
def _get_previews(self, query):
return []
def _get_full_content(self, items):
return items
engine = SemanticScholarSearchEngine()
wrapper = SimpleNamespace(web_search=engine, close=Mock())
with patch(RECORD) as record:
run_search("semantic_scholar", "q", lambda: wrapper, tracker)
run_search("semantic_scholar", "q", Mock(), tracker)
assert record.call_args.kwargs["engine_name"] == "semanticscholar"
def test_programmatic_mode_does_not_record_skipped_searches(tracker):
tracker.disable("pubmed", SearchFailure("URL blocked", None))
with patch(RECORD) as record:
run_search("pubmed", "q", Mock(), tracker, programmatic_mode=True)
record.assert_not_called()
def test_metrics_failure_does_not_break_the_search_tool(tracker):
tracker.disable("pubmed", SearchFailure("URL blocked", None))
with patch(RECORD, side_effect=RuntimeError("db down")):
assert "URL blocked" in run_search("pubmed", "q", Mock(), tracker)
def test_query_rejected_by_the_provider_does_not_disable_the_engine(tracker):
engine = Mock(last_search_failure=None)
engine.run.side_effect = requests.HTTPError(
response=SimpleNamespace(status_code=400, headers={})
)
result = run_search("openalex", "bad (query", lambda: engine, tracker)
assert "rejected this query (HTTP 400)" in result
assert tracker.disabled() == {}
def test_delegation_is_refused_when_subagents_could_only_fetch(tracker):
tracker.disable("searxng", SearchFailure("URL blocked", None))
@make_langchain_tool
def fetch_content(url: str) -> str:
"""Fetch."""
return ""
with (
patch.object(
strategy_module, "build_fetch_tool", return_value=fetch_content
),
patch.object(
strategy_module, "_load_specialized_engine_tools", return_value=[]
),
patch("langchain.agents.create_agent") as create_subagent,
):
tool = strategy_module._make_research_subtopic_tool(
"searxng",
Mock(),
{},
strategy_module.SearchResultsCollector([]),
3,
availability=tracker,
)
result = tool.invoke({"subtopics": ["one", "two"]})
assert "research_subtopic is unavailable" in result
assert "URL blocked" in result
create_subagent.assert_not_called()
def _always_rate_limited_run(tracker, clock, retry_after):
"""The only engine answers 429 to every search; the model keeps trying."""
from local_deep_research.advanced_search_system.tools import (
search_availability,
)
call = {
"name": "web_search",
"args": {"query": "q"},
"id": "1",
}
model = ScriptedModel(
responses=[AIMessage(content="", tool_calls=[call]) for _ in range(30)]
)
engine = Mock(last_search_failure=None)
engine.run.side_effect = RateLimitError("limit", retry_after=retry_after)
sleeps = []
def sleep(seconds):
sleeps.append(seconds)
clock.now += seconds
with (
patch(FACTORY, return_value=engine),
patch.object(search_availability, "time", SimpleNamespace(sleep=sleep)),
):
agent = create_agent(
model=model,
tools=[make_tool("web_search", tracker, model)],
middleware=[SearchAvailabilityMiddleware(tracker, "searxng")],
)
with pytest.raises(NoResearchToolsAvailable):
agent.invoke(
{"messages": [{"role": "user", "content": "q"}]},
{"recursion_limit": 101},
)
return engine, sleeps
def test_persistent_rate_limit_on_the_only_engine_is_bounded_per_run(clock):
"""Repeated 10-minute cooldowns use the shared one-hour budget."""
tracker = SearchEngineAvailability(clock=lambda: clock.now)
engine, sleeps = _always_rate_limited_run(tracker, clock, 600)
assert sum(sleeps) == pytest.approx(3600, abs=0.1)
assert engine.run.call_count == 7
state = tracker.disabled()["searxng"]
assert state.retry_at is None
assert "wait budget" in state.failure.reason
def test_repeated_cooldowns_continue_past_three_failures(clock):
tracker = SearchEngineAvailability(
clock=lambda: clock.now, wait_budget_seconds=240
)
engine, sleeps = _always_rate_limited_run(tracker, clock, 60)
assert sum(sleeps) == pytest.approx(240, abs=0.1)
assert engine.run.call_count == 5
assert "wait budget" in tracker.disabled()["searxng"].failure.reason
def test_retry_after_beyond_the_budget_is_not_waited_for(tracker, clock):
engine, sleeps = _always_rate_limited_run(tracker, clock, 7200)
assert sleeps == []
assert engine.run.call_count == 1
def test_configured_two_hour_budget_waits_for_hour_long_retry(clock):
tracker = SearchEngineAvailability(
clock=lambda: clock.now, wait_budget_seconds=7200
)
engine, sleeps = _always_rate_limited_run(tracker, clock, 3600)
assert sum(sleeps) == pytest.approx(7200, abs=0.1)
assert engine.run.call_count == 3
def test_a_successful_search_resets_the_cooldown_streak(clock):
tracker = SearchEngineAvailability(
clock=lambda: clock.now, max_consecutive_cooldowns=1
)
engine = Mock(last_search_failure=None)
engine.run.side_effect = [
RateLimitError("limit", retry_after=60),
[],
RateLimitError("limit", retry_after=60),
]
with patch(FACTORY, return_value=engine):
tool = make_tool("web_search", tracker)
tool.invoke({"query": "one"})
clock.now += 60
tool.invoke({"query": "two"}) # answered, even if empty
tool.invoke({"query": "three"})
assert tracker.disabled()["searxng"].retry_at is not None
def test_default_middleware_wait_honours_cancellation(clock):
"""Without an explicit ``wait``, the tracker's cancellation check runs."""
from local_deep_research.advanced_search_system.tools import (
search_availability,
)
checks = []
def check_cancelled():
checks.append(1)
if len(checks) == 3:
raise ResearchTerminatedException("cancelled")
tracker = SearchEngineAvailability(
clock=lambda: clock.now, check_cancelled=check_cancelled
)
tracker.disable("searxng", SearchFailure("offline", 300))
sleeps = []
def sleep(seconds):
sleeps.append(seconds)
clock.now += seconds
request = ModelRequest(
model=Mock(), messages=[], tools=[make_tool("web_search", tracker)]
)
with (
patch.object(search_availability, "time", SimpleNamespace(sleep=sleep)),
pytest.raises(ResearchTerminatedException),
):
SearchAvailabilityMiddleware(tracker, "searxng").wrap_model_call(
request, Mock()
)
assert len(sleeps) == 2
def test_one_cancel_stops_every_parallel_cooldown_wait(clock):
"""The research's termination flag is single-use.
The check that sees it first cleans the run up and clears it, as
research_service's progress_callback does, so the other waiter has to
learn of the cancel from the tracker they share.
"""
cancel, lock, consumed = Event(), Lock(), []
def check_cancelled():
with lock:
if not cancel.is_set():
return
cancel.clear()
consumed.append(get_ident())
raise ResearchTerminatedException("cancelled")
tracker = SearchEngineAvailability(
clock=lambda: clock.now, check_cancelled=check_cancelled
)
tracker.disable("searxng", SearchFailure("offline", 300))
all_waiting = Barrier(3, timeout=5)
waiting = set()
give_up = time.monotonic() + 5
def sleep(_seconds):
# Real time passes but the tracker's clock does not, so only the
# cancel can end these waits.
if get_ident() not in waiting:
waiting.add(get_ident())
all_waiting.wait()
elif time.monotonic() > give_up:
raise TimeoutError("this cooldown wait never saw the cancel")
else:
time.sleep(0.001)
with ThreadPoolExecutor(max_workers=2) as pool:
waits = [
pool.submit(wait_for_cooldown, tracker, {"searxng"}, sleep=sleep)
for _ in range(2)
]
all_waiting.wait()
cancel.set()
outcomes = [wait.exception(timeout=10) for wait in waits]
assert all(isinstance(o, ResearchTerminatedException) for o in outcomes)
assert len(consumed) == 1
def test_strategy_tracker_checks_the_research_termination_flag():
strategy = strategy_module.LangGraphAgentStrategy(
model=Mock(),
search=Mock(last_search_failure=None),
settings_snapshot={"search.tool": "searxng"},
)
with patch.object(
strategy,
"check_termination",
side_effect=ResearchTerminatedException("cancelled"),
) as check:
with pytest.raises(ResearchTerminatedException):
strategy.engine_availability.check_cancelled()
check.assert_called_once_with(strategy_module.CHECK_CONTEXT_ENTRY)
@pytest.mark.parametrize(
"configured,expected",
[("7200", 7200), (0, 0), (float("nan"), 3600), (-1, 3600)],
)
def test_strategy_uses_configured_cooldown_wait_budget(configured, expected):
strategy = strategy_module.LangGraphAgentStrategy(
model=Mock(),
search=Mock(last_search_failure=None),
settings_snapshot={
"search.tool": "searxng",
"langgraph_agent.cooldown_wait_budget_seconds": configured,
},
)
assert strategy.engine_availability.wait_budget_remaining() == expected
@pytest.mark.parametrize(
"wait_budget,timeout,expected",
[
(3600, 10800, 10800),
(7200, 7200, 9000),
(0, 60, 1800),
(3600, "bad", 7200),
# Below the setting's minimum is invalid; the minimum itself is kept
# (then raised to the cooldown budget plus 30 minutes).
(3600, 59, 7200),
(3600, 60, 5400),
],
)
def test_subagent_timeout_allows_the_full_cooldown_budget(
wait_budget, timeout, expected
):
strategy = strategy_module.LangGraphAgentStrategy(
model=Mock(),
search=Mock(last_search_failure=None),
settings_snapshot={
"search.tool": "searxng",
"langgraph_agent.cooldown_wait_budget_seconds": wait_budget,
"langgraph_agent.subagent_timeout_seconds": timeout,
},
)
assert strategy.subagent_timeout_seconds == expected
def test_subagent_timeout_setting_matches_the_strategy_bounds():
"""The stored default and minimum, the form's step grid and the
strategy's own validation must agree."""
defaults = json.loads(
(
resources.files("local_deep_research")
/ "defaults"
/ "default_settings.json"
).read_text(encoding="utf-8")
)
setting = defaults["langgraph_agent.subagent_timeout_seconds"]
assert setting["value"] == strategy_module.SUBAGENT_TIMEOUT_SECONDS
assert setting["min_value"] == strategy_module.MIN_SUBAGENT_TIMEOUT_SECONDS
# A number input steps from its min: a default off that grid reads as
# invalid in the form and its spinner snaps to a different value.
assert (setting["value"] - setting["min_value"]) % setting["step"] == 0