Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

18.8 KB
Raw Blame
"""
Base class for all search strategies.
Defines the common interface and shared functionality for different search approaches.
"""

from abc import ABC, abstractmethod
from typing import Any, Callable, Optional

from loguru import logger

from ...utilities.type_utils import unwrap_setting

# Context slugs for check_termination() call sites. Passed as the
# ``context`` argument and surfaced in the callback metadata; tests import
# them to cancel at one specific check. Keep source and tests on these
# constants rather than repeating the literals.
CHECK_CONTEXT_ENTRY = "entry"
CHECK_CONTEXT_ITERATION_START = "iteration_start"
CHECK_CONTEXT_POST_SEARCH = "post_search"
CHECK_CONTEXT_PRE_SYNTHESIS = "pre_synthesis"
CHECK_CONTEXT_PRE_ANALYSIS = "pre_analysis"
CHECK_CONTEXT_REFINEMENT_LOOP = "refinement_loop"
CHECK_CONTEXT_AGENT_STREAM = "agent_stream"
CHECK_CONTEXT_FALLBACK_SYNTHESIS = "fallback_synthesis"

# Final-synthesis text for a citation result that is truthy but carries no
# usable ``content`` / ``response`` (think-only model replies stringify to
# ""). Deliberately unlike the strategies' "No relevant results found" text,
# so a degraded synthesis cannot be mistaken for a search that found nothing
# (#6940). It says nothing about sources: there may be none, and a report
# that lists only cited sources would omit them anyway.
NO_USABLE_SYNTHESIS_MESSAGE = "Final synthesis produced no usable text."


# Bounds for progress events that carry an expandable detail in
# ``metadata["content"]`` (chat renders it beneath the one-line message in
# the click-to-expand step row; the chat route persists it per step, so it
# must stay bounded). Shared by every strategy that emits step detail —
# the LangGraph agent strategy imports these for its observation events.
_OBSERVATION_PREVIEW_MAX_CHARS = 150
_OBSERVATION_DETAIL_MAX_CHARS = 4000


def _cap_detail(text: str) -> str:
    """Bound an expandable-detail string, marking the cut with an ellipsis."""
    if len(text) > _OBSERVATION_DETAIL_MAX_CHARS:
        return text[:_OBSERVATION_DETAIL_MAX_CHARS] + " …"
    return text


def _flatten(text: str) -> str:
    r"""Collapse every whitespace run in *text* to a single space.

    Handles ``\n``, ``\r\n``, bare ``\r`` and tabs alike — text reaching
    these emitters is externally sourced (scraped page titles, PDF
    metadata, LLM-generated questions, pasted user queries) and any of
    them would otherwise break a single-line contract.
    """
    return " ".join(str(text).split())


def _bullet(item: str) -> str:
    """Render one detail-list entry as exactly one ``•`` line.

    The step message states a count ("Found 3 results", "Searching with
    5 questions") and the expandable detail lists that many bullets. An
    embedded newline in a single entry would split it across two visual
    lines — the second with no bullet marker, reading as a separate,
    malformed entry — so the list no longer matches the stated count.
    """
    return f"• {_flatten(item)}"


class BaseSearchStrategy(ABC):
    """Abstract base class for all search strategies."""

    def __init__(
        self,
        all_links_of_system=None,
        settings_snapshot=None,
        questions_by_iteration=None,
        search_original_query: bool = True,
        content_cache=None,
    ):
        """Initialize the base strategy with common attributes.

        Args:
            all_links_of_system: List to store all discovered links
            settings_snapshot: Settings snapshot for configuration
            questions_by_iteration: Dictionary of questions by iteration
            search_original_query: Whether to include the original query in the first iteration
            content_cache: Single shared ``ResearchContentCache`` for the
                whole research run (quick phase, subsections, lead +
                subagents). When None, a fresh cache is created so repeat
                URLs within this strategy still share content.
        """
        # Log strategy initialization
        strategy_name = self.__class__.__name__
        logger.info(f"Initializing strategy: {strategy_name}")
        logger.debug(
            f"Strategy {strategy_name} - search_original_query: {search_original_query}"
        )

        self.progress_callback: (
            Callable[[str, int | None, dict[str, Any]], None] | None
        ) = None
        # Create a new dict if None is provided (avoiding mutable default argument)
        self.questions_by_iteration = (
            questions_by_iteration if questions_by_iteration is not None else {}
        )
        # Create a new list if None is provided (avoiding mutable default argument)
        self.all_links_of_system = (
            all_links_of_system if all_links_of_system is not None else []
        )
        self.settings_snapshot = settings_snapshot or {}
        self.search_original_query = search_original_query
        if content_cache is None:
            try:
                from ...utilities.research_content_cache import (
                    ResearchContentCache,
                )

                content_cache = ResearchContentCache()
            except Exception:  # pragma: no cover - defensive
                content_cache = None
        self.content_cache = content_cache

    def get_content_cache(self):
        """Return the strategy's shared research content cache.

        Falls back to the cache bound to the current research (if any) so
        strategies constructed without an explicit cache still share with
        the rest of the run.
        """
        if getattr(self, "content_cache", None) 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 close(self):
        """Release any persistent resources held by this strategy.

        Override this method if your strategy creates persistent resources
        in __init__ (e.g. ThreadPoolExecutor, HTTP sessions).

        Currently, two strategies need this:
        - ConstraintParallelStrategy: holds search_executor +
          evaluation_executor
        - ConcurrentDualConfidenceStrategy: holds evaluation_executor

        The default source-based strategy uses context-managed ('with')
        executors that clean up automatically, so no override is needed.

        Note: This is called by AdvancedSearchSystem.close() which is
        called in run_research_process()'s finally block. Strategies
        should also clean up in their own finally blocks (as they already
        do) — this close() is a second line of defense.
        """
        pass

    def get_setting(self, key: str, default=None):
        """Get a setting value from the snapshot."""
        if key in self.settings_snapshot:
            return unwrap_setting(self.settings_snapshot[key])
        return default

    @staticmethod
    def first_usable_text(
        citation_result: dict[str, Any], default: str | None = None
    ) -> str | None:
        """Return the first usable text field of a citation result.

        Centralizes the assembly predicate the strategy sites share: a
        candidate from ``content`` / ``response`` counts only when it is a
        non-empty, non-whitespace ``str`` (think-only model replies
        stringify to ``""``), so a blank one cannot hide a valid sibling
        field. Returns ``default`` when neither field is usable (#6941).
        """
        return next(
            (
                candidate
                for candidate in (
                    citation_result.get("content"),
                    citation_result.get("response"),
                )
                if isinstance(candidate, str) and candidate.strip()
            ),
            default,
        )

    def set_progress_callback(
        self, callback: Callable[[str, int | None, dict[str, Any]], None]
    ) -> None:
        """Set a callback function to receive progress updates."""
        self.progress_callback = callback

    def check_termination(self, context: str | None = None) -> None:
        """Check if the research has been cancelled.

        Reuses the existing ``progress_callback`` rather than a dedicated
        termination-check callable.  This avoids duplicating the ~15 wiring
        points across the codebase (search_system, research_service,
        research_functions, and every nested strategy) where
        ``set_progress_callback`` is already propagated.  A dedicated
        ``set_termination_check`` method could be introduced later if the
        callback-based approach becomes a maintenance burden.

        The callback in ``research_service.py`` recognises the
        ``"termination_check"`` phase and returns immediately after the
        flag check -- no UI logging or socket emission occurs.

        Args:
            context: Slug naming the call site — one of the module-level
                ``CHECK_CONTEXT_*`` constants (e.g.
                ``CHECK_CONTEXT_PRE_SYNTHESIS``). Included in the callback
                metadata so tests and diagnostics can target one specific
                check instead of counting callback invocations.
        """
        if self.progress_callback:
            logger.debug("Checking termination status")
            metadata: dict[str, Any] = {"phase": "termination_check"}
            if context:
                metadata["context"] = context
            self.progress_callback(
                "Checking termination status",
                None,
                metadata,
            )

    def _update_progress(
        self,
        message: str,
        progress_percent: Optional[int] = None,
        metadata: Optional[dict[str, Any]] = None,
    ) -> None:
        """Send a progress update via the callback if available."""
        if self.progress_callback:
            self.progress_callback(message, progress_percent, metadata or {})

    @abstractmethod
    def analyze_topic(self, query: str) -> dict[str, Any]:
        """
        Analyze a topic using the strategy's specific approach.

        Args:
            query: The research query to analyze

        Returns:
            Dict containing:
            - findings: List of research findings
            - iterations: Number of iterations completed
            - questions: Questions generated by iteration
            - formatted_findings: Formatted output
            - current_knowledge: Accumulated knowledge
            - error: Optional error message
        """
        pass

    def _validate_search_engine(self) -> bool:
        """
        Validate that the search engine is available and configured.

        Returns:
            bool: True if search engine is available, False otherwise
        """
        if not hasattr(self, "search") or self.search is None:
            error_msg = "Error: No search engine available. Please check your configuration."
            self._update_progress(
                error_msg,
                100,
                {
                    "phase": "error",
                    "error": "No search engine available",
                    "status": "failed",
                },
            )
            return False
        return True

    def _emit_question_generation_progress(
        self,
        iteration: int,
        progress_percent: int,
        source_count: int = 0,
        query: str = "",
    ) -> None:
        """
        Emit progress update for question generation phase.

        Uses the ``search_planning`` step phase so the event becomes a
        persisted, click-to-expand row in chat mode. The message stays one
        line. For iterations after the first, the previous iteration's
        questions (no "... and N more" sampling) ride in
        ``metadata["content"]`` as the expandable detail, capped at
        ``_OBSERVATION_DETAIL_MAX_CHARS`` with an ellipsis marker; the
        first iteration has no detail.

        Args:
            iteration: Current iteration number
            progress_percent: Current progress percentage
            source_count: Number of sources found (for iteration > 1)
            query: The original research query (for iteration 1)
        """
        if iteration == 1:
            # Flatten whitespace (chat's textarea allows Shift+Enter) so
            # the step row stays a genuine one-liner, then cap with a
            # visible truncation marker.
            flat_query = _flatten(query)
            shown_query = (
                flat_query
                if len(flat_query) <= _OBSERVATION_PREVIEW_MAX_CHARS
                else flat_query[:_OBSERVATION_PREVIEW_MAX_CHARS] + "…"
            )
            self._update_progress(
                f'Planning initial search questions for: "{shown_query}"',
                progress_percent,
                {
                    "phase": "search_planning",
                    "type": "milestone",
                    "iteration": iteration,
                },
            )
        else:
            prev_questions = self.questions_by_iteration.get(iteration - 1, [])
            metadata = {
                "phase": "search_planning",
                "type": "milestone",
                "iteration": iteration,
                "source_count": source_count,
            }
            if prev_questions:
                metadata["content"] = _cap_detail(
                    "Previous questions:\n"
                    + "\n".join(_bullet(q) for q in prev_questions)
                )
            self._update_progress(
                f"Generating search questions from {source_count} sources "
                f"(iteration {iteration})",
                progress_percent,
                metadata,
            )

    def _create_error_response(self, error: str) -> dict[str, Any]:
        """Build a standardized error result.

        Used by ``focused_iteration_strategy`` (the other historical
        consumer, ``mcp_strategy``, has been removed). Both ``questions``
        and ``questions_by_iteration`` keys are included because the two
        consumers read different keys historically — keeping both avoids
        breaking downstream readers of either key.
        """
        return {
            "findings": [],
            "iterations": 0,
            "questions": {},
            "questions_by_iteration": {},
            "formatted_findings": f"Error: {error}",
            "current_knowledge": "",
            "error": error,
        }

    def _format_citations(
        self, content: str, search_results: list[dict[str, Any]]
    ) -> str:
        """Append a Markdown ``## Sources`` bibliography to ``content``.

        Used by ``langgraph_agent_strategy`` (passes
        ``self.collector.results``); the other historical consumer,
        ``mcp_strategy``, has been removed.
        Only sources cited in ``content`` are listed, honoring
        ``report.uncited_sources_mode`` from the settings snapshot.
        Returns ``content`` unchanged when no links can be extracted.
        """
        if not search_results:
            return content
        try:
            from ...utilities.search_utilities import (
                extract_links_from_search_results,
                format_links_to_markdown,
                resolve_uncited_sources_mode,
            )

            all_links = extract_links_from_search_results(search_results)
            if not all_links:
                return content
            mode = resolve_uncited_sources_mode(
                self.get_setting("report.uncited_sources_mode")
            )
            sources_markdown = format_links_to_markdown(
                all_links, prose=content, uncited_mode=mode
            )
            if not sources_markdown:
                return content
            return f"{content}\n\n## Sources\n\n{sources_markdown}"
        except Exception:
            logger.exception("Failed to format source links")
            return content

    def _emit_searching_progress(
        self,
        iteration: int,
        questions: list[str],
        progress_percent: int,
    ) -> None:
        """
        Emit progress update for searching phase with questions as milestone.

        Uses the ``search`` step phase so the event becomes a persisted,
        click-to-expand row in chat mode. The message stays one line; the
        question list (no "... and N more" sampling) rides in
        ``metadata["content"]`` as the expandable detail, capped at
        ``_OBSERVATION_DETAIL_MAX_CHARS`` with an ellipsis marker (no
        detail when there are no questions).

        Args:
            iteration: Current iteration number
            questions: List of questions being searched
            progress_percent: Current progress percentage
        """
        metadata = {
            "phase": "search",
            "type": "milestone",
            "iteration": iteration,
            "questions": questions,
        }
        if questions:
            metadata["content"] = _cap_detail(
                "\n".join(_bullet(q) for q in questions)
            )
        self._update_progress(
            f"Searching with {len(questions)} question"
            f"{'s' if len(questions) != 1 else ''} (iteration {iteration})",
            progress_percent,
            metadata,
        )

    def _emit_search_results_observation(
        self,
        iteration: int,
        questions: list[str],
        results: list[dict[str, Any]],
        progress_percent: int,
    ) -> None:
        """
        Emit an ``observation`` step summarizing what an iteration's
        searches returned.

        Mirrors the LangGraph strategy's per-tool-result observations: a
        one-line message plus the found sources (title + URL bullets) in
        ``metadata["content"]``, so the chat step row expands to show what
        was actually found. No citation numbers — indices are assigned
        later, after filtering/reindexing.

        Args:
            iteration: Current iteration number
            questions: The questions that were searched
            results: Raw search-result dicts from this iteration
            progress_percent: Current progress percentage
        """
        metadata: dict[str, Any] = {
            "phase": "observation",
            "iteration": iteration,
        }
        lines = []
        for r in results:
            if not isinstance(r, dict):
                continue
            title = r.get("title") or "Untitled"
            link = r.get("link", r.get("url", ""))
            lines.append(_bullet(f"{title} ({link})" if link else title))
        if lines:
            metadata["content"] = _cap_detail("\n".join(lines))
        self._update_progress(
            f"📄 Found {len(results)} result{'s' if len(results) != 1 else ''} "
            f"from {len(questions)} search"
            f"{'es' if len(questions) != 1 else ''} (iteration {iteration})",
            progress_percent,
            metadata,
        )