~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
"""Multi-iteration runs report monotonic progress that reaches 100 only at the end.
Issue #6062: source_based_strategy emitted ``iteration_progress_base + 85``
for ``filtering_complete`` after the iteration loop, i.e. 125% with 2
iterations and 136.7% with 3. The registry clamp then pinned the bar at
exactly 100 before synthesis, and ``progress == 100`` made every later
callback "final" (throttle and chat repeat-suppression bypassed) while the
answer was still being generated.
These tests drive the REAL strategy with the strategy fixtures' mocked LLM
and search, first on its own and then through the REAL progress_callback
closure of run_research_process with the real research_state registry.
Each iteration's planning step is reported with phase ``search_planning``.
The classic strategies used to report it as ``question_generation``, a
phase outside ``research_service._STEP_PHASES``, so chat mode never rendered
it; the strategies now speak the chat step vocabulary (``search_planning``,
``search``, ``observation``). Nothing else keyed on the old name (no JS
consumer, no progress-UI branch, no metrics query), so this test is the only
place that counted it and it counts the new name.
"""
import itertools
import pytest
from local_deep_research.search_system_factory import create_strategy
from local_deep_research.web import research_state
from local_deep_research.web.services import research_service
from tests.web.services.test_research_service_progress_integration import (
captured_progress_callback,
)
def _strategy(name, iterations, llm, search, snapshot):
snapshot = dict(snapshot)
snapshot["search.iterations"] = {"value": iterations, "type": "int"}
return create_strategy(
strategy_name=name,
model=llm,
search=search,
settings_snapshot=snapshot,
max_iterations=iterations,
)
def _assert_monotonic_and_bounded(values, upper):
assert values, "no progress values were emitted"
for value in values:
assert 0 <= value <= upper, values
for earlier, later in zip(values, values[1:]):
assert later >= earlier, f"progress went backwards: {values}"
@pytest.mark.parametrize("iterations", [2, 3])
@pytest.mark.parametrize("name", ["source-based", "focused-iteration"])
def test_strategy_progress_is_monotonic_and_within_bounds(
name,
iterations,
strategy_mock_llm,
strategy_mock_search,
strategy_settings_snapshot,
):
strategy = _strategy(
name,
iterations,
strategy_mock_llm,
strategy_mock_search,
strategy_settings_snapshot,
)
events = []
strategy.set_progress_callback(
lambda _message, progress, metadata: events.append(
(progress, dict(metadata or {}))
)
)
strategy.analyze_topic("What is artificial intelligence?")
phases = [metadata.get("phase") for _, metadata in events]
# Every iteration reported its planning step (``search_planning``, the
# chat step phase that replaced ``question_generation``), inside the
# band before synthesis (90).
iteration_steps = [
progress
for progress, metadata in events
if metadata.get("phase") == "search_planning"
]
assert len(iteration_steps) >= iterations, events
assert all(progress < 90 for progress in iteration_steps), events
if name == "source-based":
# The overshooting emitter must actually have run.
assert "filtering_complete" in phases, phases
assert "synthesis" in phases, phases
values = [
progress
for progress, metadata in events
if progress is not None and metadata.get("phase") != "error"
]
_assert_monotonic_and_bounded(values, 100)
# Everything before synthesis stays at or below the synthesis milestone.
if "synthesis" in phases:
synthesis_at = phases.index("synthesis")
assert all(
progress <= 90
for progress, _ in events[:synthesis_at]
if progress is not None
), events
# Per strategy: where its first iteration band starts, and the total width of
# all its iteration bands. Both report init at 5; source-based iterates over
# 5-75 (final_filtering follows at 80), focused-iteration over 10-90
# (synthesis follows at 90).
_ITERATION_BANDS = {
"source-based": (5, 70),
"focused-iteration": (10, 80),
}
@pytest.mark.parametrize("iterations", [14, 25, 60])
@pytest.mark.parametrize("name", ["source-based", "focused-iteration"])
def test_step_events_stay_in_their_band_with_many_iterations(
name,
iterations,
strategy_mock_llm,
strategy_mock_search,
strategy_settings_snapshot,
):
"""Every iteration's step events stay inside that iteration's band.
A band is ``width / iterations`` wide and nothing caps
``search.iterations``, so it narrows as iterations grow. The planning,
searching and results step events sit at offsets that only fit a wide
band. Left fixed, source-based's results event fell below its searching
event from 5 iterations and below the init milestone past 14, and
focused-iteration's did from 14 iterations, with its searching event
passing synthesis (90) from 21 -- none of which the 2- and 3-iteration
test above can show, because the offsets happen to fit there. Both
strategies are the ones that call the shared step emitters;
topic-organization forwards their events with no percentage.
"""
strategy = _strategy(
name,
iterations,
strategy_mock_llm,
strategy_mock_search,
strategy_settings_snapshot,
)
# The mocked question generator repeats itself, and an iteration with no
# new question is skipped before it searches. Give every iteration a
# fresh question so each one reaches its searching and results events.
fresh_questions = itertools.count(1)
strategy.question_generator.generate_questions = lambda **_: [
f"fresh question {next(fresh_questions)}"
]
events = []
strategy.set_progress_callback(
lambda _message, progress, metadata: events.append(
(progress, dict(metadata or {}))
)
)
strategy.analyze_topic("What is artificial intelligence?")
phases = [metadata.get("phase") for _, metadata in events]
step_phases = ("search_planning", "search", "observation")
# The run really did emit all three step events once per iteration.
for step_phase in step_phases:
assert phases.count(step_phase) == iterations, (step_phase, phases)
first_band_start, total_width = _ITERATION_BANDS[name]
band = total_width / iterations
for progress, metadata in events:
if metadata.get("phase") not in step_phases:
continue
band_start = first_band_start + (metadata["iteration"] - 1) * band
assert band_start - 1e-9 <= progress <= band_start + band + 1e-9, (
metadata["phase"],
metadata["iteration"],
progress,
band_start,
)
before_synthesis = [
progress
for progress, _ in events[: phases.index("synthesis")]
if progress is not None
]
_assert_monotonic_and_bounded(before_synthesis, 90)
@pytest.mark.parametrize("iterations", [2, 3])
def test_quick_run_reaches_100_and_is_final_only_on_completion(
iterations,
strategy_mock_llm,
strategy_mock_search,
strategy_settings_snapshot,
):
"""Real strategy -> real closure -> real registry, chat session on.
``_chat_step_decision`` receives the closure's ``is_final`` for every
active event, so wrapping it records exactly what the throttle and the
chat repeat-suppression saw.
"""
from unittest.mock import patch
research_id = f"multi-iteration-progress-{iterations}"
decisions = []
real_decision = research_service._chat_step_decision
def record_decision(phase, last_step_phase, is_final):
decisions.append((phase, is_final))
return real_decision(phase, last_step_phase, is_final)
extras = {}
with captured_progress_callback(
"quick",
run_kwargs={
"research_id": research_id,
"chat_session_id": f"sess-{research_id}",
},
extras=extras,
real_registry=True,
) as (cb, _, update_calls):
research_state.set_active_research(
research_id, {"progress": 0, "status": "in_progress"}
)
try:
update_calls.clear()
with patch.object(
research_service,
"_chat_step_decision",
side_effect=record_decision,
):
strategy = _strategy(
"source-based",
iterations,
strategy_mock_llm,
strategy_mock_search,
strategy_settings_snapshot,
)
strategy.set_progress_callback(cb)
strategy.analyze_topic("What is artificial intelligence?")
# run_research_process's quick-mode tail, in order.
cb(
"Search complete, preparing to generate summary...",
85,
{"phase": "output_generation"},
)
cb(
"Generating clean summary from research data...",
90,
{"phase": "output_generation"},
)
cb(
"Saving research report to database...",
95,
{"phase": "report_complete"},
)
in_progress = [stored for _, _, stored in update_calls]
_assert_monotonic_and_bounded(in_progress, 99)
assert max(in_progress) < 100, in_progress
# The only final event so far is report_complete itself.
assert [p for p, final in decisions if final] == [
"report_complete"
], decisions
for _, _, payload in (
c.args for c in extras["socketio"].call_args_list
):
assert payload["progress"] < 100, payload
cb(
"Research completed successfully",
100,
{
"phase": "complete",
research_service._RUN_COMPLETE_FLAG: True,
},
)
finally:
research_state.cleanup_research(research_id)
assert update_calls[-1][2] == 100
assert decisions[-1] == ("complete", True)
final_payload = extras["socketio"].call_args_list[-1].args[2]
assert final_payload["progress"] == 100
assert research_service._RUN_COMPLETE_FLAG not in final_payload
def test_strategy_complete_at_100_does_not_finish_a_quick_run():
"""The default langgraph strategy ends analyze_topic with
phase="complete" at 100, before the answer is generated."""
research_id = "strategy-complete-quick"
with captured_progress_callback(
"quick", run_kwargs={"research_id": research_id}, real_registry=True
) as (cb, state, _):
research_state.set_active_research(
research_id, {"progress": 0, "status": "in_progress"}
)
try:
cb("Synthesizing", 90, {"phase": "synthesis"})
cb("Research complete", 100, {"phase": "complete"})
assert state[0] == 90
cb("Generating", 90, {"phase": "output_generation"})
assert 90 < state[0] < 100
cb("Strategy error", 100, {"phase": "error"})
assert state[0] == research_service._IN_PROGRESS_CAP
finally:
research_state.cleanup_research(research_id)
class _BareCompleteStrategy:
"""Minimal strategy: reports progress, then a bare phase="complete" at
100 mid-run (as the langgraph strategy does before the answer exists)."""
def __init__(self, cb):
self.cb = cb
def analyze_topic(self):
self.cb("Synthesizing", 90, {"phase": "synthesis"})
self.cb("Research complete", 100, {"phase": "complete"})
@pytest.mark.parametrize("chat", [False, True])
def test_strategy_bare_complete_mid_run_is_not_final(chat):
"""A strategy's own phase="complete" at 100 is not the run's end: the
emit throttle still applies and _chat_step_decision sees is_final=False.
(Fails if ``phase == "complete"`` is added back to is_final.)"""
from unittest.mock import patch
research_id = f"bare-complete-{chat}"
decisions = []
real_decision = research_service._chat_step_decision
def record_decision(phase, last_step_phase, is_final):
decisions.append((phase, is_final))
return real_decision(phase, last_step_phase, is_final)
run_kwargs = {"research_id": research_id}
if chat:
run_kwargs["chat_session_id"] = f"sess-{research_id}"
extras = {}
with captured_progress_callback(
"quick", run_kwargs=run_kwargs, extras=extras, real_registry=True
) as (cb, state, _):
research_state.set_active_research(
research_id, {"progress": 0, "status": "in_progress"}
)
try:
with patch.object(
research_service,
"_chat_step_decision",
side_effect=record_decision,
):
_BareCompleteStrategy(cb).analyze_topic()
assert state[0] < 100
if chat:
assert ("complete", True) not in decisions, decisions
assert ("complete", False) in decisions, decisions
else:
# Back-to-back events: the second is inside the throttle
# window, so only the first (synthesis) reaches the socket.
phases = [
c.args[2]["phase"]
for c in extras["socketio"].call_args_list
]
assert phases == ["synthesis"], phases
finally:
research_state.cleanup_research(research_id)