~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
"""Regression tests for #5014: langgraph-agent subagent timeout.
The ``research_subtopic`` fan-out used a single shared
``as_completed(futures, timeout=SUBAGENT_TIMEOUT_SECONDS)`` budget for the
whole drain loop. Because ``ThreadPoolExecutor`` has no work-stealing, when
``MAX_SUBTOPICS > MAX_SUBAGENT_WORKERS`` the surplus subtopics queue and only
start executing near the end of the window -- then the shared timer killed them
and reported ``"timed out after 1800s"`` even though they had run for seconds.
The fix (Option A) gives each subagent a per-task deadline measured from its
*actual* start time, plus a generous overall safety cap that only fires for
genuinely hung tasks. The follow-up comment additionally requires exposing
``MAX_SUBAGENT_WORKERS`` as the user setting
``langgraph_agent.max_subagent_workers``.
These tests drive the REAL production drain loop with a REAL
``ThreadPoolExecutor``. To keep them fast and deterministic we patch the
module-level ``SUBAGENT_TIMEOUT_SECONDS`` to a small value (e.g. 0.2s) and let
subagents "work" by sleeping real (short) amounts of time. The per-task
deadline is therefore exercised against real wall-clock elapsed times, so the
queued-subagent false-positive and the actual-elapsed reporting are both
genuinely validated.
"""
import threading
import time
import uuid
from contextlib import contextmanager
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
from langchain.agents.middleware import ModelRequest
import local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy as mod
from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (
MAX_SUBAGENT_WORKERS,
LangGraphAgentStrategy,
SearchResultsCollector,
_make_research_subtopic_tool,
)
from local_deep_research.exceptions import ResearchTerminatedException
from local_deep_research.utilities.thread_context import search_context
from local_deep_research.web import research_state
from local_deep_research.web_search_engines.engine_availability import (
SearchEngineAvailability,
SearchFailure,
)
# Small budget so tests run in well under a second while still exercising the
# per-task timeout path.
TEST_TIMEOUT = 0.2
FACTORY = "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
def _build_tool(
max_subagent_workers=MAX_SUBAGENT_WORKERS,
sleep_map=None,
subagent_timeout_seconds=None,
):
"""Build a research_subtopic tool whose subagents sleep per ``sleep_map``.
``sleep_map``: subtopic -> seconds the subagent sleeps (simulated work).
Returns (tool, agent_mock).
"""
sleep_map = sleep_map or {}
collector = SearchResultsCollector([])
tool = _make_research_subtopic_tool(
search_engine_name="duckduckgo",
model=MagicMock(),
settings_snapshot={"search.tool": {"value": "duckduckgo"}},
collector=collector,
max_sub_iterations=8,
max_subagent_workers=max_subagent_workers,
subagent_timeout_seconds=subagent_timeout_seconds,
)
agent_mock = MagicMock()
def _fake_invoke(payload, _config=None):
topic = payload["messages"][0]["content"]
# Simulate real work so wall-clock elapsed time is meaningful.
time.sleep(sleep_map.get(topic, 0.0))
return {"messages": [MagicMock(content=f"finding for {topic}")]}
agent_mock.invoke.side_effect = _fake_invoke
return tool, agent_mock
class TestResearchSubtopicPerTaskTimeout:
"""#5014: per-subagent deadline from actual start time + user workers."""
def _run(self, sleep_map, max_subagent_workers=4):
tool, agent_mock = _build_tool(
max_subagent_workers=max_subagent_workers, sleep_map=sleep_map
)
with patch.object(
mod, "_make_web_search_tool", return_value=MagicMock()
):
with patch.object(mod, "build_fetch_tool", return_value=None):
with patch(
"langchain.agents.create_agent", return_value=agent_mock
):
with patch.object(
mod, "SUBAGENT_TIMEOUT_SECONDS", TEST_TIMEOUT
):
return tool.invoke(
{"subtopics": list(sleep_map.keys())}
)
def test_queued_subagent_not_falsely_timed_out(self):
"""Reproduce the #5014 false-positive scenario with a short budget:
5 subtopics, 4 workers, the first 4 each take most of the budget, the
5th is queued and starts late but runs only briefly. Under the OLD
shared-timer code the 5th would be flagged "timed out after <budget>"
despite running for milliseconds. Under the fix it returns a result."""
topics = [f"topic {i}" for i in range(5)]
# The first wave plus the queued task exceeds the old shared 0.2s
# drain budget, while the queued task's own 0.06s runtime stays well
# inside its individual budget. This fails against the pre-PR loop.
sleep_map = {
t: TEST_TIMEOUT * 0.9 if i < 4 else TEST_TIMEOUT * 0.3
for i, t in enumerate(topics)
}
result = self._run(sleep_map, max_subagent_workers=4)
# The queued 5th subtopic must NOT be reported as timed out.
assert "topic 4" in result
assert "Research on 'topic 4' timed out" not in result
# All five return real findings.
assert result.count("finding for topic") >= 5
def test_subagent_finishing_after_own_deadline_times_out(self):
"""One slow future among normal finishers is reported at its 0.2s
task deadline even though it finishes by the 0.4s overall cap. This is
the reviewer-requested proof that the per-task path is live."""
result = self._run(
{
"fast topic": 0.01,
"slow topic": TEST_TIMEOUT * 1.5,
},
max_subagent_workers=2,
)
assert "finding for fast topic" in result
assert "Research on 'slow topic' timed out after" in result
assert "finding for slow topic" not in result
assert "did not finish within the overall safety budget" not in result
def test_configured_timeout_reaches_the_worker_deadline(self):
"""The configured deadline replaces the module default.
The patched default (TEST_TIMEOUT) would time the 0.3s worker out;
the configured deadline is an hour. Nothing here races the clock:
the worker can never come close to the configured deadline, and a
tool that ignored it would always time out (a sleep never returns
early).
"""
tool, agent_mock = _build_tool(
sleep_map={"slow local model": TEST_TIMEOUT * 1.5},
subagent_timeout_seconds=3600,
)
with (
patch.object(
mod, "_make_web_search_tool", return_value=MagicMock()
),
patch.object(mod, "build_fetch_tool", return_value=None),
patch("langchain.agents.create_agent", return_value=agent_mock),
patch.object(mod, "SUBAGENT_TIMEOUT_SECONDS", TEST_TIMEOUT),
):
result = tool.invoke({"subtopics": ["slow local model"]})
assert "finding for slow local model" in result
assert "timed out" not in result
def test_configured_timeout_is_enforced_as_the_worker_deadline(self):
"""...and it is the deadline that applies, not just a bigger number.
The patched default is an hour while the configured deadline is
TEST_TIMEOUT; the 0.3s worker overruns it (a sleep never returns
early), whichever order the drain loop notices that in.
"""
tool, agent_mock = _build_tool(
sleep_map={"slow local model": TEST_TIMEOUT * 1.5},
subagent_timeout_seconds=TEST_TIMEOUT,
)
with (
patch.object(
mod, "_make_web_search_tool", return_value=MagicMock()
),
patch.object(mod, "build_fetch_tool", return_value=None),
patch("langchain.agents.create_agent", return_value=agent_mock),
patch.object(mod, "SUBAGENT_TIMEOUT_SECONDS", 3600),
):
result = tool.invoke({"subtopics": ["slow local model"]})
assert "Research on 'slow local model' timed out after" in result
assert f"per-subagent budget is {TEST_TIMEOUT}s" in result
assert "finding for slow local model" not in result
def test_large_configured_timeout_does_not_overflow_condition_wait(self):
tool, agent_mock = _build_tool(
sleep_map={"topic": 0.02}, subagent_timeout_seconds=1e308
)
with (
patch.object(
mod, "_make_web_search_tool", return_value=MagicMock()
),
patch.object(mod, "build_fetch_tool", return_value=None),
patch("langchain.agents.create_agent", return_value=agent_mock),
):
result = tool.invoke({"subtopics": ["topic"]})
assert "finding for topic" in result
def test_per_task_deadline_from_actual_start_not_submit(self):
"""Even when a subagent is queued for a long time, its per-task budget
is measured from its *actual* start, so a short-running queued task is
never killed. 3 tasks serialised behind 1 worker, each brief."""
topics = [f"topic {i}" for i in range(3)]
sleep_map = {t: 0.01 for t in topics}
result = self._run(sleep_map, max_subagent_workers=1)
for t in topics:
assert f"## {t}" in result
assert f"Research on '{t}' timed out" not in result
def test_overall_cap_reports_never_started_task_as_queued(self):
"""A task stuck behind a timed-out worker must not be described as
having run since drain start; its own budget never began."""
sleep_map = {
"hung topic": TEST_TIMEOUT * 6,
"queued topic": 0.01,
}
result = self._run(sleep_map, max_subagent_workers=1)
assert "Research on 'hung topic' timed out after" in result
assert (
"Research on 'queued topic' did not start before the overall "
"safety budget"
) in result
assert "(queued for " in result
assert "budget begins at task start" in result
assert "finding for queued topic" not in result
def test_max_subagent_workers_setting_is_respected(self):
"""The user setting ``langgraph_agent.max_subagent_workers`` must flow
through to the pool size used by the tool (follow-up to #5014)."""
collector = SearchResultsCollector([])
captured = {}
real_tpe = mod.ThreadPoolExecutor
def _spy_executor(max_workers=None, **kw):
captured["max_workers"] = max_workers
return real_tpe(max_workers=max_workers, **kw)
tool = _make_research_subtopic_tool(
search_engine_name="duckduckgo",
model=MagicMock(),
settings_snapshot={"search.tool": {"value": "duckduckgo"}},
collector=collector,
max_sub_iterations=8,
max_subagent_workers=7,
)
agent_mock = MagicMock()
agent_mock.invoke.return_value = {"messages": [MagicMock(content="ok")]}
with patch.object(
mod, "_make_web_search_tool", return_value=MagicMock()
):
with patch.object(mod, "build_fetch_tool", return_value=None):
with patch(
"langchain.agents.create_agent", return_value=agent_mock
):
with patch.object(mod, "ThreadPoolExecutor", _spy_executor):
tool.invoke({"subtopics": ["a", "b", "c"]})
# Pool size is min(max_subagent_workers, len(subtopics)) = min(7, 3) = 3
assert captured["max_workers"] == 3
def test_coerce_clamps_invalid_setting(self):
"""``_coerce_max_subagent_workers`` must fall back to the constant for
non-integer input and clamp out-of-range values."""
assert LangGraphAgentStrategy._coerce_max_subagent_workers("oops") == (
MAX_SUBAGENT_WORKERS
)
assert (
LangGraphAgentStrategy._coerce_max_subagent_workers(float("inf"))
== MAX_SUBAGENT_WORKERS
)
assert (
LangGraphAgentStrategy._coerce_max_subagent_workers(float("-inf"))
== MAX_SUBAGENT_WORKERS
)
assert (
LangGraphAgentStrategy._coerce_max_subagent_workers(float("nan"))
== MAX_SUBAGENT_WORKERS
)
assert LangGraphAgentStrategy._coerce_max_subagent_workers(0) == 1
assert LangGraphAgentStrategy._coerce_max_subagent_workers(-5) == 1
assert LangGraphAgentStrategy._coerce_max_subagent_workers(100) == 32
assert LangGraphAgentStrategy._coerce_max_subagent_workers(8) == 8
def test_strategy_reads_setting_default_when_absent(self):
"""With no setting present, the strategy defaults to
``MAX_SUBAGENT_WORKERS``."""
strategy = LangGraphAgentStrategy(
model=MagicMock(),
search=MagicMock(),
settings_snapshot={},
max_sub_iterations=8,
)
assert strategy.max_subagent_workers == MAX_SUBAGENT_WORKERS
def test_strategy_reads_user_setting(self):
"""A user-supplied ``langgraph_agent.max_subagent_workers`` is honoured
(when valid)."""
strategy = LangGraphAgentStrategy(
model=MagicMock(),
search=MagicMock(),
settings_snapshot={
"langgraph_agent.max_subagent_workers": {"value": 6}
},
max_sub_iterations=8,
)
assert strategy.max_subagent_workers == 6
@contextmanager
def _drain_patches(create_agent):
with (
patch.object(mod, "_make_web_search_tool", return_value=MagicMock()),
patch.object(mod, "build_fetch_tool", return_value=None),
patch("langchain.agents.create_agent", new=create_agent),
# Wake the drain loops in milliseconds, not minutes.
patch.object(mod, "SUBAGENT_POLL_SECONDS", 0.01),
):
yield
class TestResearchSubtopicDrainCancellation:
"""After a cancel, the lead must not stay blocked behind a stuck subagent.
A subagent inside a slow LLM call cannot see the cancel, and its per-task
deadline is hours away by default, so the drain loop itself has to look.
The research's termination flag is single-use: the check that sees it
first cleans the run up (``handle_termination`` -> ``cleanup_research``),
which clears it, and so does the cleanup once the lead has stopped. These
tests use the real flag registry; ``run.check_cancelled`` is that check.
"""
# Bounds for the waits below, so that a regression fails in seconds
# instead of waiting out a per-task deadline.
STOP_SECONDS = 5
STUCK_SECONDS = 30
@pytest.fixture
def run(self):
research_id = f"drain-cancel-{uuid.uuid4().hex}"
consumed = []
def check_cancelled():
# What research_service's progress_callback does when it sees
# the flag: handle_termination cleans the run up, clearing it.
if research_state.is_termination_requested(research_id):
consumed.append(threading.current_thread().name)
research_state.cleanup_research(research_id)
raise ResearchTerminatedException("cancelled")
yield SimpleNamespace(
id=research_id, check_cancelled=check_cancelled, consumed=consumed
)
research_state.clear_termination_flag(research_id)
@staticmethod
def _tool(run):
return _make_research_subtopic_tool(
search_engine_name="duckduckgo",
model=MagicMock(),
settings_snapshot={"search.tool": {"value": "duckduckgo"}},
collector=SearchResultsCollector([]),
max_sub_iterations=8,
# The strategy wires the research's consuming check in here.
availability=SearchEngineAvailability(
check_cancelled=run.check_cancelled
),
)
def _subagents(self, run, release):
"""A ``create_agent`` stand-in and what its subagents did.
Each subagent is stuck in a slow model call until ``release``; its
next model call then goes through the middleware the real agent loop
runs every model call through. A subagent whose topic starts with
"consumer" instead sees the cancel at its own check first, which
clears the flag before any drain loop looks.
"""
state = SimpleNamespace(
started=threading.Semaphore(0),
done=threading.Semaphore(0),
go=threading.Event(),
stopped=[],
model_calls=[],
)
def create_agent(*, middleware, **_kwargs):
def invoke(payload, _config=None):
topic = payload["messages"][0]["content"]
state.started.release()
try:
if topic.startswith("consumer"):
state.go.wait(timeout=self.STUCK_SECONDS)
with research_state._lock:
research_state.set_termination_flag(run.id)
run.check_cancelled()
release.wait(timeout=self.STUCK_SECONDS)
request = ModelRequest(
model=MagicMock(), messages=[], tools=[]
)
try:
middleware[0].wrap_model_call(
request,
lambda _request: state.model_calls.append(topic),
)
except ResearchTerminatedException:
state.stopped.append(topic)
raise
return {
"messages": [MagicMock(content=f"finding for {topic}")]
}
finally:
state.done.release()
agent = MagicMock()
agent.invoke.side_effect = invoke
return agent
return create_agent, state
@staticmethod
def _drain(tool, run, topic, outcomes):
"""Run one research_subtopic call on its own thread, as the lead's
tool node does for each call of a turn."""
def target():
with search_context({"research_id": run.id}):
try:
outcomes[topic] = tool.invoke({"subtopics": [topic]})
except BaseException as exc: # asserted by the caller
outcomes[topic] = exc
thread = threading.Thread(target=target, daemon=True)
thread.start()
return thread
@staticmethod
def _lead_tools(run):
"""The lead's web_search and research_subtopic, built by the strategy.
The strategy's progress callback acts like research_service's: every
call checks the flag, and the first to see it cleans the run up,
clearing the flag, and raises. ``phases`` lists the calls in order.
Call within a patch of ``_load_specialized_engine_tools``.
"""
strategy = LangGraphAgentStrategy(
model=MagicMock(),
search=MagicMock(last_search_failure=None),
settings_snapshot={"search.tool": "searxng"},
)
phases = []
def progress_callback(_message, _percent, metadata):
phases.append(metadata.get("phase"))
run.check_cancelled()
strategy.set_progress_callback(progress_callback)
with (
patch.object(strategy, "_build_egress_context", return_value=None),
patch.object(
strategy, "_build_library_resolver", return_value=None
),
patch.object(mod, "build_fetch_tool", return_value=None),
):
tools = {tool.name: tool for tool in strategy._build_tools()}
return tools["web_search"], tools["research_subtopic"], phases
def test_a_later_wake_stops_the_lead_and_leaves_the_flag_set(self, run):
"""The cancel arrives after the drain loop has woken several times.
The loop stops the lead without clearing the flag; the run's single
cleanup follows from the exception it raises.
"""
tool = self._tool(run)
release = threading.Event()
create_agent, subagents = self._subagents(run, release)
outcomes = {}
try:
with _drain_patches(create_agent):
drain = self._drain(tool, run, "stuck topic", outcomes)
assert subagents.started.acquire(timeout=self.STOP_SECONDS)
time.sleep(0.05) # several drain-loop wakes with no cancel
assert drain.is_alive()
research_state.set_termination_flag(run.id)
drain.join(timeout=self.STOP_SECONDS)
assert not drain.is_alive()
finally:
release.set()
assert subagents.done.acquire(timeout=self.STOP_SECONDS)
assert isinstance(outcomes["stuck topic"], ResearchTerminatedException)
assert run.consumed == []
assert research_state.is_termination_requested(run.id)
def test_one_cancel_stops_parallel_drain_loops_and_their_subagents(
self, run
):
"""Two research_subtopic calls of one turn wait on stuck subagents.
One cancel stops both, and once the run's cleanup has cleared the
flag, each subagent still stops at its next model call.
"""
tool = self._tool(run)
release = threading.Event()
create_agent, subagents = self._subagents(run, release)
outcomes = {}
topics = ["first batch", "second batch"]
try:
with _drain_patches(create_agent):
drains = [self._drain(tool, run, t, outcomes) for t in topics]
for _ in topics:
assert subagents.started.acquire(timeout=self.STOP_SECONDS)
research_state.set_termination_flag(run.id)
for drain in drains:
drain.join(timeout=self.STOP_SECONDS)
assert not any(drain.is_alive() for drain in drains)
for topic in topics:
assert isinstance(outcomes[topic], ResearchTerminatedException)
# Neither loop consumed the flag. The run's one cleanup, from
# run_research_process's handler for the exception, clears it.
assert run.consumed == []
assert research_state.is_termination_requested(run.id)
research_state.cleanup_research(run.id)
finally:
release.set()
for _ in topics:
assert subagents.done.acquire(timeout=self.STOP_SECONDS)
assert sorted(subagents.stopped) == topics
assert subagents.model_calls == []
def test_a_subagent_clearing_the_flag_still_stops_every_drain_loop(
self, run
):
"""A subagent's own check sees the cancel first and clears the flag,
as a search's progress update can. The other call's drain loop and
stuck subagent stop as well."""
tool = self._tool(run)
release = threading.Event()
create_agent, subagents = self._subagents(run, release)
outcomes = {}
topics = ["consumer batch", "stuck batch"]
try:
with _drain_patches(create_agent):
drains = [self._drain(tool, run, t, outcomes) for t in topics]
for _ in topics:
assert subagents.started.acquire(timeout=self.STOP_SECONDS)
subagents.go.set()
for drain in drains:
drain.join(timeout=self.STOP_SECONDS)
assert not any(drain.is_alive() for drain in drains)
for topic in topics:
assert isinstance(outcomes[topic], ResearchTerminatedException)
# The subagent's check was the run's one cleanup.
assert len(run.consumed) == 1
assert not research_state.is_termination_requested(run.id)
finally:
release.set()
for _ in topics:
assert subagents.done.acquire(timeout=self.STOP_SECONDS)
assert subagents.stopped == ["stuck batch"]
assert subagents.model_calls == []
def test_a_lead_search_clearing_the_flag_still_stops_the_drain_loop(
self, run
):
"""The lead's own web_search, in the same turn as a research_subtopic
call, sees the cancel first: its engine fails, and the progress
update reporting that disable clears the flag. The drain loop and its
stuck subagent stop as well."""
release = threading.Event()
create_agent, subagents = self._subagents(run, release)
engine = MagicMock(last_search_failure=SearchFailure("offline", 60))
outcomes = {}
try:
with patch.object(
mod, "_load_specialized_engine_tools", return_value=[]
):
web_search, research_subtopic, phases = self._lead_tools(run)
with _drain_patches(create_agent):
drain = self._drain(
research_subtopic, run, "stuck batch", outcomes
)
assert subagents.started.acquire(timeout=self.STOP_SECONDS)
# Set and consumed before any drain-loop wake can look.
with (
research_state._lock,
patch(FACTORY, return_value=engine),
):
research_state.set_termination_flag(run.id)
with pytest.raises(ResearchTerminatedException):
web_search.invoke({"query": "lead query"})
drain.join(timeout=self.STOP_SECONDS)
assert phases[-1] == "engine_availability"
assert len(run.consumed) == 1
assert not research_state.is_termination_requested(run.id)
assert not drain.is_alive()
assert isinstance(
outcomes["stuck batch"], ResearchTerminatedException
)
finally:
release.set()
assert subagents.done.acquire(timeout=self.STOP_SECONDS)
assert subagents.stopped == ["stuck batch"]
assert subagents.model_calls == []
def test_a_second_call_clearing_the_flag_still_stops_the_first(self, run):
"""A second research_subtopic call of the turn sees the cancel first,
at its start milestone, before starting any subagent. The first
call's drain loop and its stuck subagent stop as well."""
release = threading.Event()
create_agent, subagents = self._subagents(run, release)
outcomes = {}
try:
with patch.object(
mod, "_load_specialized_engine_tools", return_value=[]
):
_web_search, research_subtopic, phases = self._lead_tools(run)
with _drain_patches(create_agent):
drain = self._drain(
research_subtopic, run, "first batch", outcomes
)
assert subagents.started.acquire(timeout=self.STOP_SECONDS)
# Set and consumed before any drain-loop wake can look.
with research_state._lock:
research_state.set_termination_flag(run.id)
with pytest.raises(ResearchTerminatedException):
research_subtopic.invoke(
{"subtopics": ["second batch"]}
)
drain.join(timeout=self.STOP_SECONDS)
# The first call's milestone, then the second's.
assert phases == ["sub_research", "sub_research"]
assert len(run.consumed) == 1
assert not research_state.is_termination_requested(run.id)
assert not drain.is_alive()
assert isinstance(
outcomes["first batch"], ResearchTerminatedException
)
finally:
release.set()
assert subagents.done.acquire(timeout=self.STOP_SECONDS)
assert subagents.stopped == ["first batch"]
assert subagents.model_calls == []