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.

19.6 KB
Raw Blame
"""
Base class for all citation handlers.
"""

from abc import ABC, abstractmethod
from typing import Any, Callable, Dict, List, Optional, Sequence, Union

from langchain_core.documents import Document
from loguru import logger

from ..utilities.type_utils import unwrap_setting
from ..utilities.json_utils import _coerce_content_blocks, get_llm_response_text


class _HeadingRepetitionGuard:
    """Detects a model re-emitting its own headings instead of stopping.

    A local model that misses its EOS token can attend back to the start of
    what it just wrote and reproduce the same section cycle until the context
    window hard-clips — 30+ iterations of identical subheadings, tables and
    prose, all of which then land in the report (#6452). Nothing was watching
    the stream, so the only bound was the context window.

    The signal is a markdown heading line repeating verbatim. It is chosen
    over n-gram or block similarity because it is O(1) per line, needs no
    buffer of the text so far, and is nearly impossible to trip by accident:
    a legitimate subsection does not emit one identical ``### Heading`` four
    times. Prose repetition without repeated headings is NOT caught — the
    guard is a bound on the reported failure, not a general degeneration
    detector, and a rule loose enough to catch every loop would truncate real
    documents.

    Headings are compared after stripping trailing whitespace and leading
    ``#``/space, so ``## Costs`` and ``### Costs`` count together: the loop
    reproduces its own text, and a level change between cycles is still the
    same cycle.

    A repeated heading alone is not the signal, because a legitimate
    comparative report repeats them by design: ``## Study A`` /
    ``### Methodology`` / ``### Results``, then ``## Study B`` and the same
    two subsections again. A four-item report reaches four
    ``### Methodology`` with nothing degenerate happening, and truncating it
    is exactly the silent quality loss this guard exists to prevent.

    What separates the two is whether the document is still PRODUCING. A
    comparative report emits a heading nobody has seen before at every item
    (``## Study B``); a loop has stopped emitting new ones entirely and only
    replays what it already wrote. So the abort needs both a heading at
    ``LIMIT`` and a run of ``CONSECUTIVE_KNOWN`` headings in a row that the
    model has already emitted, and any previously-unseen heading resets that
    run. Both are O(1) per line and need no buffer of the text so far.

    That also bounds a loop repeating one heading and nothing else: every
    occurrence after the first is a known heading, so the run climbs and the
    abort follows a cycle later than for a two-heading loop.

    Lines inside a fenced code block are not headings. ``# comment`` is the
    first line of half the bash and Python snippets there are, and a technical
    report quoting four similar config blocks would otherwise trip the guard.

    Chunk boundaries fall anywhere, so a partial last line is held back until
    its newline arrives rather than being counted as its own heading.
    """

    #: Occurrences of one heading before it can end the stream. Three is
    #: reachable by a document that legitimately revisits a title (a summary
    #: repeating a section name); four is not, and a loop reaches it in two
    #: cycles.
    LIMIT = 4

    #: Headings in a row that the model has already emitted. A comparative
    #: report breaks this run at every item with a heading nobody has seen
    #: before; a loop cannot, because it has stopped producing new ones.
    CONSECUTIVE_KNOWN = 4

    def __init__(self) -> None:
        self._counts: Dict[str, int] = {}
        self._pending = ""
        self._in_fence = False
        self._consecutive_known = 0

    def feed(self, texts: Sequence[str]) -> Optional[str]:
        """Consume newly streamed text; return the repeated heading, or None."""
        for text in texts:
            self._pending += text
            *lines, self._pending = self._pending.split("\n")
            for line in lines:
                repeated = self._count(line)
                if repeated is not None:
                    return repeated
        return None

    def _count(self, line: str) -> Optional[str]:
        stripped = line.strip()
        if stripped.startswith("```"):
            self._in_fence = not self._in_fence
            return None
        if self._in_fence or not stripped.startswith("#"):
            return None
        heading = stripped.lstrip("#").strip()
        if not heading:
            return None
        seen = self._counts.get(heading, 0) + 1
        self._counts[heading] = seen
        if seen == 1:
            self._consecutive_known = 0
            return None
        self._consecutive_known += 1
        if seen < self.LIMIT:
            return None
        if self._consecutive_known < self.CONSECUTIVE_KNOWN:
            return None
        return heading


class BaseCitationHandler(ABC):
    """Abstract base class for citation handlers."""

    def __init__(self, llm, settings_snapshot=None):
        self.llm = llm
        self.settings_snapshot = settings_snapshot or {}
        self._fact_checking_logged = False
        self.stream_callback: Optional[Callable[[str], None]] = None

    def set_stream_callback(self, callback: Callable[[str], None]):
        """Set a callback that receives each streamed LLM token."""
        self.stream_callback = callback

    def _new_repetition_guard(self) -> "_HeadingRepetitionGuard":
        """The streaming degeneration guard, as a seam a subclass can widen."""
        return _HeadingRepetitionGuard()

    def _handle_chunk(self, chunk: Any, chunks: List[str]) -> None:
        """Normalize one streamed chunk, record it and push it to the callback.

        Shared by the sync and async streaming paths so the two differ only
        in the LLM call line.
        """
        text = (
            chunk
            if isinstance(chunk, str)
            else getattr(chunk, "content", str(chunk))
        )
        if isinstance(text, list):
            # Keep spaces and split reasoning tags intact until chunks join.
            text = _coerce_content_blocks(text)
        if text:
            chunks.append(text)
            # Bind to a local so mypy sees the Optional narrowed — the
            # attribute is Optional[Callable] and this helper is called
            # from both streaming paths after their own None checks.
            callback = self.stream_callback
            if callback is not None:
                try:
                    callback(text)
                except Exception:
                    logger.debug(
                        "stream_callback failed"
                    )  # Non-critical: don't break synthesis

    def _log_repetition_abort(self, heading: str, chunks: List[str]) -> None:
        """One line a reader can act on: what repeated, and how much was kept."""
        logger.warning(
            "Stream aborted after a degenerate repetition loop: the heading "
            "{!r} was emitted {} times. Keeping the {} chunk(s) produced so "
            "far; the model was re-emitting its own output instead of "
            "stopping.",
            heading,
            _HeadingRepetitionGuard.LIMIT,
            len(chunks),
        )

    def _join_chunks(self, chunks: List[str]) -> str:
        """Normalize the joined chunks exactly like the invoke() path does.

        ``.stream()`` / ``.astream()`` bypass the normalization in
        ProcessingLLMWrapper.invoke / ainvoke, so without
        this a reasoning model's ``<think>…</think>`` would leak into the
        persisted answer. The live token stream is a separate concern.
        """
        return get_llm_response_text("".join(chunks))

    def _partial_after_stream_failure(self, chunks: List[str]) -> Optional[str]:
        """Resolve a mid-stream failure: partial text, or ``None`` to fall back.

        If any chunks already crossed the wire to the client, restarting via
        a full ``invoke()`` would (a) double-bill the LLM and (b) cause the
        frontend's accumulated streamed text to diverge from the new full
        response — the chat bubble then shows partial chunks while the DB row
        carries the invoke()-result. Only fall back when nothing was emitted.
        """
        if chunks:
            logger.warning(
                "Stream errored after {} chunks; returning partial "
                "content (no invoke() fallback to avoid double-bill "
                "and UI/DB divergence)",
                len(chunks),
            )
            return self._join_chunks(chunks)
        logger.debug("Stream failed before any chunk; falling back to invoke()")
        return None

    def _invoke_with_streaming(self, prompt: str) -> str:
        """
        Invoke the LLM, streaming tokens through the callback if set.

        Falls back to a single ``invoke()`` call when no callback is
        registered or when the LLM does not support ``.stream()``.

        Stays on the synchronous LangChain API: citation handlers run 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).

        Returns:
            The complete response text.
        """
        if self.stream_callback and hasattr(self.llm, "stream"):
            chunks: List[str] = []
            guard = self._new_repetition_guard()
            try:
                for chunk in self.llm.stream(prompt):
                    before = len(chunks)
                    self._handle_chunk(chunk, chunks)
                    # Cooperative abort for unwrapped/custom LLMs whose
                    # own stream() does not check the flag (wrapped LLMs
                    # already raise per chunk via ProcessingLLMWrapper).
                    # Resolved from the thread's search context so handlers
                    # holding a raw model still stop promptly on Stop.
                    # NOTE: ResearchTerminatedException inherits
                    # BaseException, so the ``except Exception`` below lets
                    # it propagate while swallowing only lookup errors.
                    try:
                        from ..llm.cancellation import (
                            get_current_research_id,
                            raise_if_terminated,
                        )

                        _rid = get_current_research_id()
                        if _rid:
                            raise_if_terminated(_rid)
                    except Exception:
                        logger.debug(
                            "Termination-flag lookup failed (non-critical)"
                        )
                    repeated = guard.feed(chunks[before:])
                    if repeated is not None:
                        self._log_repetition_abort(repeated, chunks)
                        break
                return self._join_chunks(chunks)
            except Exception:
                partial = self._partial_after_stream_failure(chunks)
                if partial is not None:
                    return partial

        # No callback (non-chat research), stream unavailable, or the stream
        # died before the first chunk: delegate to the same normalization
        # _invoke_text uses, so <think> blocks are stripped and str/object
        # responses are handled uniformly.
        return self._invoke_text(prompt)

    async def _invoke_with_streaming_async(self, prompt: str) -> str:
        """Async counterpart of :meth:`_invoke_with_streaming`.

        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:`_invoke_with_streaming` remains the canonical production
        path on the research thread, and this must never go through
        ``asyncio.run`` or a per-thread loop (#6293). The streaming
        semantics are the sync path's, shared through the same helpers —
        including returning the PARTIAL text after a mid-stream failure
        instead of restarting with a full call.

        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 self.stream_callback and hasattr(self.llm, "astream"):
            chunks: List[str] = []
            guard = self._new_repetition_guard()
            try:
                async for chunk in self.llm.astream(prompt):
                    before = len(chunks)
                    self._handle_chunk(chunk, chunks)
                    repeated = guard.feed(chunks[before:])
                    if repeated is not None:
                        self._log_repetition_abort(repeated, chunks)
                        break
                return self._join_chunks(chunks)
            except Exception:
                partial = self._partial_after_stream_failure(chunks)
                if partial is not None:
                    return partial

        return await self._invoke_text_async(prompt)

    def _invoke_text(self, prompt: str) -> str:
        """Invoke the LLM and return normalized text.

        Handles both message objects (``.content``) and raw string responses,
        and strips ``<think>`` reasoning blocks via ``get_llm_response_text``.

        Synchronous for the same reason as :meth:`_invoke_with_streaming`.
        """
        return get_llm_response_text(self.llm.invoke(prompt))

    async def _invoke_text_async(self, prompt: str) -> str:
        """Async counterpart of :meth:`_invoke_text`.

        Additive: no production caller awaits it yet (see
        :meth:`_invoke_with_streaming_async`). Configured models expose
        ``ainvoke`` through ``ProcessingLLMWrapper``. Custom injected LLMs
        must also provide ``ainvoke`` to use this async API; it does not
        fall back to blocking synchronous invocation.
        """
        return get_llm_response_text(await self.llm.ainvoke(prompt))

    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

    def is_fact_checking_enabled(self) -> bool:
        """Check if fact-checking is enabled and log the state once."""
        enabled = self.get_setting("general.enable_fact_checking", False)
        if not self._fact_checking_logged:
            handler_name = type(self).__name__
            if enabled:
                logger.info(
                    f"[{handler_name}] Fact-checking is ENABLED — "
                    f"extra LLM call per synthesis"
                )
            else:
                logger.info(f"[{handler_name}] Fact-checking is DISABLED")
            self._fact_checking_logged = True
        return bool(enabled)

    def _get_output_instruction_prefix(self) -> str:
        """
        Get formatted output instructions from settings if present.

        This allows users to customize output language, tone, style, and formatting
        for research answers and reports. Instructions are prepended to prompts
        sent to the LLM.

        Returns:
            str: Formatted instruction prefix if custom instructions are set,
                 empty string otherwise.

        Examples:
            - "Respond in Spanish with formal academic tone"
            - "Use simple language suitable for beginners"
            - "Be concise with bullet points"
        """
        output_instructions = self.get_setting(
            "general.output_instructions", ""
        ).strip()

        if output_instructions:
            return f"User-Specified Output Style: {output_instructions}\n\n"
        return ""

    def _create_documents(
        self, search_results: Union[str, List[Dict]], nr_of_links: int = 0
    ) -> List[Document]:
        """
        Convert search results to LangChain documents format and add index
        to original search results.
        """
        documents: List[Document] = []
        if isinstance(search_results, str):
            return documents

        for i, result in enumerate(search_results):
            if isinstance(result, dict):
                # Add index to the original search result dictionary if it doesn't exist
                # This preserves indices that were already set (e.g., for topic organization)
                if "index" not in result:
                    result["index"] = str(i + nr_of_links + 1)

                content = result.get("full_content")
                if not content:
                    content = result.get("snippet", "")
                # Use the index from the result if it exists, otherwise calculate it
                doc_index = int(result.get("index", i + nr_of_links + 1))
                documents.append(
                    Document(
                        page_content=content,
                        metadata={
                            "source": result.get("link", f"source_{i + 1}"),
                            "title": result.get("title", f"Source {i + 1}"),
                            "index": doc_index,
                        },
                    )
                )
        return documents

    def _format_sources(self, documents: List[Document]) -> str:
        """Override to customize source text; keep each source's [N] first."""
        sources = []
        for doc in documents:
            source_id = doc.metadata["index"]
            sources.append(f"[{source_id}] {doc.page_content}")
        return "\n\n".join(sources)

    def _no_sources_response(self, question: str) -> Dict[str, Any]:
        """
        Explicit no-sources result returned instead of invoking the LLM.

        Prompting the LLM to "answer with citations [1], [2]…" while the
        sources section is empty makes it fall back on its training data
        and fabricate references. Handlers call this to refuse synthesis
        instead.
        """
        logger.warning(
            f"[{type(self).__name__}] No sources available for synthesis of "
            f"'{question[:100]}' — skipping LLM call to avoid fabricated "
            f"citations"
        )
        content = (
            "No sources were found for this question. The selected search "
            "engines or document collections returned no results; this can "
            "also happen when a search fails with an error (check the "
            "research logs). No answer was generated because, without "
            "sources, it would have to rely on the language model's "
            "built-in knowledge and could contain fabricated citations."
        )
        return {"content": content, "documents": []}

    @abstractmethod
    def analyze_initial(
        self, query: str, search_results: Union[str, List[Dict]]
    ) -> Dict[str, Any]:
        """Process initial analysis with citations."""
        pass

    @abstractmethod
    def analyze_followup(
        self,
        question: str,
        search_results: Union[str, List[Dict]],
        previous_knowledge: str,
        nr_of_links: int,
    ) -> Dict[str, Any]:
        """Process follow-up analysis with citations."""
        pass