~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
import time
from datetime import datetime, UTC
from typing import (
Any,
Callable,
Dict,
List,
Optional,
Protocol,
runtime_checkable,
)
from langchain_core.language_models import BaseLLM
from ...config.search_config import QUALITY_CHECK_DDG_URLS
from ..engine_availability import SearchFailure, engine_failure
from ...research_library.downloaders.extraction import (
batch_fetch_and_extract,
)
from ...security.egress.policy import EgressScope, PolicyDeniedError
from ...security.log_sanitizer import scrub_error
from ...security.secure_logging import logger
from ...security.ssrf_validator import redact_url_for_log, validate_url
from ...utilities.js_rendering import (
read_js_rendering_setting as _read_js_rendering_setting,
)
from ...utilities.json_utils import extract_json, get_llm_response_text
from ...utilities.type_utils import resolve_boolean_setting
from ...utilities.llm_utils import invoke_llm_sync
@runtime_checkable
class _Invokable(Protocol):
def invoke(self, query: str) -> Any: ...
# Minimum seconds between bulk-fetch heartbeat milestones. Each UI
# emit is a DB row + socket event, so per-page emits would spam the
# research log on a 46-page fetch; the start announcement is always
# emitted and the completion milestone ("From the web …") follows the
# fetch, so heartbeats only fill the silence in between.
_FETCH_HEARTBEAT_SECONDS = 15.0
def make_fetch_progress_adapter(progress_callback):
"""Adapt bulk-fetch ``(done, total)`` progress to UI milestones.
Shared by the pipeline-strategy wiring (``AdvancedSearchSystem`` →
``FullSearchResults``) and the langgraph-agent tool factories, so
both surfaces throttle identically.
Returns a hook for engine page-fetch progress that:
* announces the fetch upfront (``done == 0``) so a minutes-long
fetch never looks stalled,
* emits a heartbeat at most every ``_FETCH_HEARTBEAT_SECONDS``,
* stays silent on the final page — the completion milestone
("From the web …") follows immediately,
* performs a silent termination check on every page (no UI
emission): each check doubles as a cancellation check, because
the UI callback raises ``ResearchTerminatedException`` when the
user cancels — and that derives ``BaseException``, so it
propagates through the fetch pipeline's ``except Exception``
handlers untouched. Cancellation during a long fetch therefore
takes effect promptly instead of only after the whole batch.
``progress_callback`` is the 3-arg UI callback
``(message, percent, metadata)``; ``None`` disables everything.
"""
state = {"last_emit": 0.0}
def _on_fetch_progress(done: int, total: int) -> None:
if progress_callback is None or total <= 0:
return
# Silent cancellation check on every page — no UI/logging.
progress_callback(
"Checking termination status",
None,
{"phase": "termination_check"},
)
if done >= total:
return
now = time.monotonic()
if done > 0 and now - state["last_emit"] < _FETCH_HEARTBEAT_SECONDS:
return
state["last_emit"] = now
if done <= 0:
progress_callback(
f"📄 Fetching full content for {total} pages — "
"this can take a minute…",
None,
{"phase": "fetch", "type": "milestone", "total": total},
)
else:
progress_callback(
f"📄 Fetching page content ({done}/{total})…",
None,
{
"phase": "fetch",
"type": "milestone",
"done": done,
"total": total,
},
)
return _on_fetch_progress
# Egress scopes whose ``evaluate_url`` admits every http(s) host the SSRF
# validator admits (UNPROTECTED lifts only the scope restriction; the hard
# SSRF and cloud-metadata blocks stay and the download pipeline enforces
# those itself on every destination). Under any other scope the run policy
# restricts destinations by address class (PUBLIC_ONLY: no private hosts;
# PRIVATE_ONLY / STRICT: no public hosts; BOTH: classified hosts only),
# so the operator's private-result grant is withheld there from both result
# URL validation and the pipeline. The pipeline now checks each redirect and
# pinned address against the run policy; browser fallback is disabled for
# restricted scopes because Chromium resolves its own DNS. ADAPTIVE never
# reaches this module (resolved at context construction); an unknown scope
# fails closed.
_SCOPES_WITHOUT_HOST_CLASS_RESTRICTION = frozenset({EgressScope.UNPROTECTED})
class FullSearchResults:
def __init__(
self,
llm: Optional[BaseLLM],
web_search: _Invokable,
output_format: str = "list",
language: str = "English",
max_results: int = 10,
region: str = "wt-wt",
time: Optional[str] = "y",
safesearch: str | int = "Moderate",
settings_snapshot: Optional[Dict] = None,
egress_context: Optional[Any] = None,
allow_private_ips: bool = False,
content_cache: Optional[Any] = None,
progress_callback: Optional[Callable[[int, int], None]] = None,
):
self.llm = llm
self.output_format = output_format
self.language = language
self.max_results = max_results
self.region = region
self.time = time
self.safesearch = safesearch
self.web_search = web_search
self.settings_snapshot = settings_snapshot
# The parent engine's resolved result-fetch grant, handed over by
# the factory when it builds this wrapper
# (``BaseSearchEngine.allow_private_result_fetch``; e.g. SearXNG:
# instance approved for private egress AND the env-only
# ``search.allow_private_result_fetch`` opt-in). When True, SSRF
# validation of result URLs may pass private/loopback IPs; the
# default stays False so ungated/public engines never fetch internal
# URLs. It applies (to that validation and to the download
# pipeline) only when the run policy imposes no host-class
# restriction,
# i.e. without a run context or under ``unprotected``
# (``_effective_allow_private_ips``).
# Link-local and the cloud-metadata literals in
# ALWAYS_BLOCKED_METADATA_IPS stay blocked regardless (issue #2477).
self.allow_private_ips = allow_private_ips
# Optional ``(done, total)`` liveness hook for the bulk page
# fetch below (see ``batch_fetch_and_extract``). Threaded in by
# the pipeline strategy (``AdvancedSearchSystem`` → ``get_search``)
# and by agent search tools so a minutes-long fetch emits progress
# instead of silence; ``None`` disables it with zero overhead.
self.progress_callback = progress_callback
# Set by the factory when the parent engine is gated against a
# specific scope; used to evaluate per-URL fetches below.
self.egress_context = egress_context
self.content_cache = content_cache
# One operator-visible line per wrapper when the grant is set but
# the run scope keeps it from applying
# (``_effective_allow_private_ips``).
self._grant_withheld_logged = False
def _resolve_cache(self) -> Optional[Any]:
if self.content_cache is not None:
return self.content_cache
try:
from ...utilities.research_content_cache import get_current_cache
except Exception: # pragma: no cover - defensive
return None
try:
return get_current_cache()
except Exception: # pragma: no cover - defensive
return None
def set_fetch_progress_callback(self, hook) -> None:
"""Late-bind the ``(done, total)`` bulk-fetch hook.
Used by ``AdvancedSearchSystem.set_progress_callback`` when the UI
callback arrives after engine construction.
"""
self.progress_callback = hook
@property
def last_search_failure(self) -> SearchFailure | None:
"""Expose provider failures through the optional full-content wrapper."""
return engine_failure(self.web_search)
def _effective_allow_private_ips(self) -> bool:
"""The private result-fetch grant as it applies to this run.
Used for BOTH the SSRF validation of result URLs in ``run`` /
``_get_full_content`` and the download pipeline. The pipeline
checks each redirect and pinned address against the run scope. The
private-result grant remains limited to no run context at all,
or a scope in ``_SCOPES_WITHOUT_HOST_CLASS_RESTRICTION``
(``unprotected``). Under every other scope the grant is withheld
from both, so a private result URL is refused by ``validate_url``
exactly as if the operator had not set the opt-in. That refusal
happens before ``evaluate_url`` runs, so it does not consume the
run's ``MAX_DENIED_FETCHES_PER_RUN`` quota. Hosts the SSRF validator
admits but the policy cannot classify public (including benchmark
and documentation hosts) are refused uncounted by ``_refuse_private_uncounted`` instead, under
the same scopes (every scope but ``unprotected``).
"""
if not self.allow_private_ips:
return False
egress_ctx = self.egress_context
if egress_ctx is None:
return True
scope = getattr(egress_ctx, "scope", None)
if scope in _SCOPES_WITHOUT_HOST_CLASS_RESTRICTION:
return True
if not getattr(self, "_grant_withheld_logged", False):
self._grant_withheld_logged = True
scope_name = getattr(scope, "value", scope)
logger.info(
"full_search: private result-fetch grant "
"(LDR_SEARCH_ALLOW_PRIVATE_RESULT_FETCH) not applied under "
f"egress scope {scope_name}; it takes effect only under the "
"unprotected scope, so private result URLs stay blocked"
)
return False
@staticmethod
def _refuse_private_uncounted(url: str, egress_ctx: Any) -> bool:
"""Drop a result URL whose host the policy cannot classify public,
uncounted, under every scope that withholds the private-fetch grant.
Private result URLs stay blocked under every scope but
``unprotected`` (``_effective_allow_private_ips``). ``validate_url``
refuses most of them, but its ``PRIVATE_IP_RANGES`` is narrower than
the policy's classifier: it also admits benchmark, documentation and
other special-purpose address ranges. ``evaluate_url`` counts
denials under every scope, so a page of these URLs could exhaust
the run's ``MAX_DENIED_FETCHES_PER_RUN`` quota and block later public
fetches. It may admit private hosts under ``strict`` /
``private_only`` / ``both``. So they are refused
here, before ``evaluate_url``, through the policy's own
non-counting ``host_not_classified_public`` (same host
classification as ``evaluate_url``), under every scope except the
ones in ``_SCOPES_WITHOUT_HOST_CLASS_RESTRICTION``; an unknown scope
fails closed. A URL with an empty host is refused too; an unresolvable
host or a DNS timeout is classified public (the classifier's fail-safe)
and is left to ``validate_url`` and ``evaluate_url``.
This uncounted check covers the result URL. Static redirects and
pinned connect addresses receive the transport's SSRF and run-scope
checks. Browser fallback is disabled under restricted scopes; when
available under Both or Unprotected, it retains SSRF checks.
"""
scope = getattr(egress_ctx, "scope", None)
if scope in _SCOPES_WITHOUT_HOST_CLASS_RESTRICTION:
return False
from ...security.egress.policy import host_not_classified_public
if not host_not_classified_public(url, egress_ctx):
return False
scope_name = getattr(scope, "value", scope)
logger.bind(policy_audit=True).warning(
"full_search URL denied by egress policy (non-public result "
"host, not counted toward the denied-fetch quota)",
url=redact_url_for_log(url),
scope=scope_name,
reason=f"nonpublic_result_host_{scope_name}",
)
return True
def _url_quality_prompt(self, results: List[Dict], query: str) -> str:
"""Build the URL-quality prompt shared by the sync and async paths."""
now = datetime.now(UTC)
current_time = now.strftime("%Y-%m-%d")
return f"""ONLY Return a JSON array. The response contains no letters. Evaluate these URLs for:
1. Timeliness (today: {current_time})
2. Factual accuracy (cross-reference major claims)
3. Source reliability (prefer official company websites, established news outlets)
4. Direct relevance to query: {query}
URLs to evaluate:
{results}
Return a JSON array of indices (0-based) for sources that meet ALL criteria.
ONLY Return a JSON array of indices (0-based) and nothing else. No letters.
Example response: \n[0, 2, 4]\n\n"""
def _keep_selected(self, results: List[Dict], response: Any) -> List[Dict]:
"""Turn the LLM's index array into the filtered result list."""
response_text = get_llm_response_text(response)
good_indices = extract_json(response_text, expected_type=list)
if good_indices is None:
good_indices = []
return [r for i, r in enumerate(results) if i in good_indices]
def _url_filter_error_message(self, error: Exception) -> str:
"""Build the scrubbed log message for an LLM-side filter failure."""
safe_msg = scrub_error(error)
return f"URL filtering error ({type(error).__name__}): {safe_msg}"
def _unfiltered_fallback(self, results: List[Dict]) -> List[Dict]:
"""Fall back to unfiltered results after an LLM-side filter failure.
The ``logger.exception(...)`` call itself stays in each
``except Exception as e:`` block below (sync and async) rather than
here, so the caught exception stays visible to
``.pre-commit-hooks/check-sensitive-logging.py`` — it tracks
``except ... as name`` bindings, and a helper taking the exception
as a plain parameter falls outside what it can see.
"""
logger.warning(
"URL quality filter unavailable — returning {} unfiltered "
"results as fallback",
len(results),
)
return results # Fall back to original results on LLM error
def check_urls(self, results: List[Dict], query: str) -> List[Dict]:
"""Filter results down to the URLs the LLM judges usable.
Stays on the synchronous LangChain API: FullSearchResults runs inside
per-run research threads with no event loop, and langchain's async
httpx client is process-cached and loop-bound, so driving the async
core on a throwaway per-call loop would break or re-send every call
after the first (see #6293).
"""
if not results:
return results
prompt = self._url_quality_prompt(results, query)
try:
if self.llm is None:
return results
return self._keep_selected(
results, invoke_llm_sync(self.llm, prompt)
)
except PolicyDeniedError:
# The URL-quality LLM was denied by egress policy (e.g. a cloud
# LLM under require_local / PRIVATE_ONLY). Do NOT fall through to
# the unfiltered-results fallback below — that would let the run
# proceed despite the user's policy refusing the LLM. Fail closed.
raise
except Exception as e:
safe_msg = self._url_filter_error_message(e)
logger.exception(safe_msg)
return self._unfiltered_fallback(results)
async def _check_urls_async(
self, results: List[Dict], query: str
) -> List[Dict]:
"""Async counterpart of :meth:`check_urls`.
Additive: no production caller awaits it yet. It is intended to be
submitted to the process's one shared event loop via
``asyncio.run_coroutine_threadsafe`` (ADR-0013,
docs/decisions/0013-research-runs-keep-daemon-thread.md); which
thread hosts that loop is not decided yet (#6467).
:meth:`check_urls` remains the canonical production path on the
research thread, and this must never go through ``asyncio.run`` or
a per-thread loop (#6293).
Not loop-safe yet, for three reasons: ``TokenCountingCallback``
writes call-usage rows to the user's encrypted database from the
loop's default executor rather than the daemon thread (rule 5;
#6294 is related); the egress audit context is thread-local and
must be passed in and re-armed on the loop side; and the submit
helper must run the coroutine in an explicit minimal context so it
does not inherit the daemon thread's ambient search context,
including the password (rule 4).
"""
if not results:
return results
prompt = self._url_quality_prompt(results, query)
try:
if self.llm is None:
return results
return self._keep_selected(results, await self.llm.ainvoke(prompt))
except PolicyDeniedError:
# Fail closed exactly like the sync path: an egress-policy denial
# must not fall through to the unfiltered-results fallback.
raise
except Exception as e:
safe_msg = self._url_filter_error_message(e)
logger.exception(safe_msg)
return self._unfiltered_fallback(results)
def run(
self, query: str, research_context: Dict[str, Any] | None = None
) -> List[Dict[str, Any]]:
# ``research_context`` is accepted for signature parity with
# ``BaseSearchEngine.run`` and its other overrides (e.g. SearXNG,
# arXiv), which callers such as ``FocusedIterationStrategy``
# invoke positionally by keyword. This engine derives everything
# it needs from ``query`` and its own settings, so the value is
# deliberately unused.
# Step 1: Get search results
search_results = self.web_search.invoke(query)
if not isinstance(search_results, list):
raise ValueError("Expected the search results in list format.")
# Step 2: Filter URLs using LLM if enabled
should_check_urls = resolve_boolean_setting(
self.settings_snapshot,
"search.quality_check_urls",
default=QUALITY_CHECK_DDG_URLS,
)
if should_check_urls and self.llm is not None:
logger.info(
"Evaluating search result URLs with LLM before fetching full content..."
)
filtered_results = self.check_urls(search_results, query)
else:
filtered_results = search_results
# Extract URLs from filtered results
urls = [
result.get("link")
for result in filtered_results
if result.get("link")
]
if not urls:
logger.error("\n === NO VALID LINKS ===\n")
return []
# SSRF-validate + egress-scope-validate each URL. SSRF only checks
# IP class; scope enforcement is the separate axis that blocks
# public hosts under PRIVATE_ONLY / STRICT.
evaluate_url_fn = None
egress_ctx = self.egress_context
if egress_ctx is not None:
from ...security.egress.policy import evaluate_url as _ev_url
evaluate_url_fn = _ev_url
allow_private = self._effective_allow_private_ips()
safe_urls: List[str] = []
for url in urls:
if url is None:
continue
# block_link_local=True is the carve-out inside the private-IP
# grant: the rest of the link-local range may host provider
# metadata beyond ALWAYS_BLOCKED_METADATA_IPS and no legitimate
# result page lives there. No effect
# without the grant, where link-local is already blocked.
if not validate_url(
url,
allow_private_ips=allow_private,
block_link_local=True,
):
logger.warning(
"SSRF validation blocked URL from full content fetch: "
f"{redact_url_for_log(url)}."
)
continue
if egress_ctx is not None and self._refuse_private_uncounted(
url, egress_ctx
):
continue
if evaluate_url_fn is not None and egress_ctx is not None:
url_decision = evaluate_url_fn(url, egress_ctx)
if not url_decision.allowed:
logger.bind(policy_audit=True).warning(
"full_search URL denied by egress policy",
url=redact_url_for_log(url),
scope=egress_ctx.scope.value,
reason=url_decision.reason,
)
continue
safe_urls.append(url)
if not safe_urls:
logger.warning(
"All URLs were blocked by SSRF validation — returning results "
"without full content. This can happen when search results "
"point to internal/private network addresses."
)
for result in filtered_results:
result["full_content"] = None
return filtered_results
# Fetch and extract all pages — specialized downloaders (arXiv,
# PubMed, etc.) are tried first, with HTML crawling as fallback for
# every type but arXiv paper URLs, which their downloader owns
# outright: for those a failure is terminal and yields no content.
# Other arXiv-host pages are not owned and do crawl.
logger.info(
f"Full search: fetching and extracting full content for {len(safe_urls)} pages..."
)
url_to_content = batch_fetch_and_extract(
safe_urls,
language=self.language,
enable_js_rendering=_read_js_rendering_setting(
self.settings_snapshot
),
content_cache=self._resolve_cache(),
allow_private_ips=allow_private,
block_link_local=True,
egress_context=egress_ctx,
progress_callback=self.progress_callback,
)
nr_full_text = sum(1 for v in url_to_content.values() if v)
for result in filtered_results:
link = result.get("link")
result["full_content"] = url_to_content.get(link) if link else None
result["full_content_is_complete"] = bool(result["full_content"])
logger.info(f"Full search: retrieved content from {nr_full_text} pages")
return filtered_results
def _get_full_content(
self, relevant_items: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
"""Fetch and attach full content to an existing list of items."""
evaluate_url_fn = None
egress_ctx = self.egress_context
if egress_ctx is not None:
from ...security.egress.policy import evaluate_url as _ev_url
evaluate_url_fn = _ev_url
allow_private = self._effective_allow_private_ips()
urls: List[str] = []
for item in relevant_items:
link = item.get("link")
if link is None:
continue
if not validate_url(
link,
allow_private_ips=allow_private,
block_link_local=True,
):
logger.warning(
"SSRF validation blocked URL from full content fetch: "
f"{redact_url_for_log(link)}."
)
continue
if egress_ctx is not None and self._refuse_private_uncounted(
link, egress_ctx
):
continue
if evaluate_url_fn is not None and egress_ctx is not None:
url_decision = evaluate_url_fn(link, egress_ctx)
if not url_decision.allowed:
logger.bind(policy_audit=True).warning(
"full_search _get_full_content URL denied by egress policy",
url=redact_url_for_log(link),
scope=egress_ctx.scope.value,
reason=url_decision.reason,
)
continue
urls.append(link)
if not urls:
for item in relevant_items:
item["full_content"] = None
return relevant_items
try:
url_to_content = batch_fetch_and_extract(
urls,
language=self.language,
enable_js_rendering=_read_js_rendering_setting(
self.settings_snapshot
),
content_cache=self._resolve_cache(),
allow_private_ips=allow_private,
block_link_local=True,
egress_context=egress_ctx,
progress_callback=self.progress_callback,
)
except PolicyDeniedError:
raise
except Exception as e:
safe_msg = scrub_error(e)
logger.exception(
f"Error fetching full content ({type(e).__name__}): {safe_msg}"
)
for item in relevant_items:
item["full_content"] = None
return relevant_items
for item in relevant_items:
link = item.get("link")
item["full_content"] = url_to_content.get(link) if link else None
item["full_content_is_complete"] = bool(item["full_content"])
return relevant_items
def close(self) -> None:
"""Close the underlying search engine and any held resources."""
from ...utilities.resource_utils import safe_close
if hasattr(self, "web_search") and self.web_search is not None:
safe_close(
self.web_search,
"wrapped web search engine",
closing_optional=True,
)
def __enter__(self):
"""Support context manager usage."""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Cleanup on context exit."""
self.close()
return False
def invoke(self, query: str) -> Any:
return self.run(query)
def __call__(self, query: str) -> Any:
return self.invoke(query)