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.

67.5 KB
Raw Blame
import re
from typing import Dict, List, Optional, Set, Tuple
from urllib.parse import urlsplit

from loguru import logger

from local_deep_research.text_optimization.citation_formatter import (
    is_line_breaking_char,
    LDR_APPENDED_SOURCES_SENTINEL,
)
from .url_utils import (
    CHUNK_DISPLAY_KEY,
    FETCHED_TITLE_KEY,
    _is_wayback_replay_url,
    canonical_url_key,
    library_display_url,
    preferred_chunk_display,
)


LANGUAGE_CODE_MAP = {
    "english": "en",
    "french": "fr",
    "german": "de",
    "spanish": "es",
    "italian": "it",
    "japanese": "ja",
    "chinese": "zh",
    "hindi": "hi",
    "arabic": "ar",
    "bengali": "bn",
    "portuguese": "pt",
    "russian": "ru",
    "korean": "ko",
}


def remove_think_tags(text: str) -> str:
    # NOTE: Fresh LLM responses from get_llm() are already <think>-stripped
    # centrally by ProcessingLLMWrapper (config/llm_config.py). Use this only on
    # text NOT from a fresh wrapped invoke (accumulated/concatenated text, or
    # agent/bind_tools output that bypasses the wrapper).
    # Remove paired <think>...</think> tags
    text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL)
    # Remove any orphaned opening or closing think tags
    text = re.sub(r"</think>", "", text)
    text = re.sub(r"<think>", "", text)
    return text.strip()


# Sentinel values used by the journal reputation filter alongside the
# numeric 1-10 quality scores. Distinguish structurally different
# "not scored" cases so the renderer can show the user *why* the tag
# isn't a numeric quality tier:
#
# - QUALITY_PENDING: reference DB hadn't finished building when the
#   search ran (first-search-during-install case).
# - QUALITY_PREPRINT: result has no journal_ref at all (pure arxiv
#   preprint or similar); there's no venue to score. Distinct from
#   "venue unknown to our catalog" (that becomes score 3, rendered
#   as Unranked).
QUALITY_PENDING = "pending"
QUALITY_PREPRINT = "preprint"


def _format_quality_tag(quality) -> str:
    """Format a journal quality score as a compact tag for source lists.

    The output is plaintext / Markdown. **Do NOT** render the containing
    string through a template filter like ``{{ foo|safe }}`` or
    ``DOMPurify.sanitize(..., {ALLOWED_TAGS:['a']})`` without first HTML-
    escaping the surrounding title — the tag itself is safe, but a
    downstream caller that concatenates ``title + quality_tag`` and
    emits the result as HTML will leak any tags in ``title`` (XSS).

    See :func:`_format_quality_tag_html` for the HTML-safe variant.

    Accepts int | None for scored journals, plus the string sentinels
    ``QUALITY_PENDING`` and ``QUALITY_PREPRINT``. Every numeric value
    in VALID_QUALITY_SCORES has its own explicit branch so a bad
    scoring-logic change can't silently rebucket a score — unexpected
    values fall through to a debug tag that shows the raw value.
    """
    if quality is None:
        return ""
    if quality == QUALITY_PENDING:
        return (
            " [journal quality data is downloading in the background; "
            "by the time you open /metrics/journals it may already "
            "be complete — re-run this search in a minute to get "
            "real quality scores]"
        )
    if quality == QUALITY_PREPRINT:
        # No venue at all (arxiv preprint / working paper / dataset).
        # Distinct from score 3 ("we looked and didn't find the
        # venue") — here there's nothing *to* look up.
        return " [preprint — not in journal catalog]"
    # Numeric tiers. Explicit per-score branches instead of ``>=``
    # ranges so boundary changes can't silently shift a bucket.
    if quality == 10:
        return " [Q1 ★★★★★]"
    # KNOWN-DEFERRED: quality == 9 is a dead branch —
    # constants.VALID_QUALITY_SCORES excludes 9 and the filter rejects
    # any LLM output of that value. Kept defensively so a future change
    # to VALID_QUALITY_SCORES does not require editing the formatter.
    # Post-merge candidate for removal together with any score-9
    # reintroduction work.
    if quality == 9:
        return " [Q1 ★★★★★]"
    if quality == 8:
        return " [Q1 ★★★★]"
    if quality == 7:
        return " [Q1 ★★★★]"
    if quality == 6:
        return " [Q2 ★★★]"
    if quality == 5:
        return " [Q2 ★★★]"
    if quality == 4:
        # JOURNAL_QUALITY_DEFAULT — venue found in the catalog but
        # with no h-index / quartile / DOAJ signal.
        return " [Unranked ★]"
    if quality == 3:
        # Low-confidence fallback — venue didn't match any tier. We
        # don't know the journal, not "we know it's low-quality".
        return " [Unranked ★]"
    if quality == 2:
        return " [Q4 ★]"
    if quality == 1:
        # Predatory. Usually auto-removed before this renderer sees
        # it, but surfaces if whitelisted or the threshold is 1.
        return " [Q4 ★]"
    # Out-of-set value — VALID_QUALITY_SCORES gates the inputs so this
    # is unreachable in normal operation. Show the raw value so bad
    # data surfaces visibly instead of silently bucketing into Q4.
    return f" [quality={quality!r}]"


def _format_quality_tag_html(quality, *, title: str = "") -> str:
    """HTML-safe wrapper for :func:`_format_quality_tag`.

    Callers that render search-result titles + quality tags into an
    HTML page must use this variant and pass the raw ``title`` so both
    are escaped together. The quality tag itself is plaintext, but the
    brackets and stars are safe to emit verbatim — the danger is the
    untrusted ``title`` that a downstream HTML template may concatenate
    alongside the tag.

    Returns:
        ``"{escaped_title}{quality_tag}"`` where ``escaped_title`` is
        HTML-escaped with ``html.escape(..., quote=True)`` so quotes,
        angle brackets, and ampersands are rendered as text.
    """
    import html as _html

    return _html.escape(title, quote=True) + _format_quality_tag(quality)


def extract_links_from_search_results(search_results: List[Dict]) -> List[Dict]:
    """
    Extracts links and titles from a list of search result dictionaries.

    Each dictionary is expected to have at least the keys "title" and "link".

    Returns a list of dictionaries with 'title' and 'url' keys.
    """
    links = []
    if not search_results:
        return links

    for result in search_results:
        try:
            # Ensure we handle None values safely before calling strip()
            title = result.get("title", "")
            url = result.get("link", "")
            index = result.get("index", "")

            # Apply strip() only if the values are not None
            title = title.strip() if title is not None else ""
            url = url.strip() if url is not None else ""
            index = index.strip() if index is not None else ""

            if title and url:
                link = {
                    "title": title,
                    "url": url,
                    "index": index,
                    "journal_quality": result.get("journal_quality"),
                }
                # Preserve citation-relevant fields from search engines
                # so they reach the database (previously lost here)
                for key in (
                    "doi",
                    "authors",
                    "published",
                    "publication_date",
                    "year",
                    "date",
                    "volume",
                    "issue",
                    "pages",
                    "journal_ref",
                    "journal",
                    "venue",
                    "publisher",
                    "source_type",
                    # Keep verified capture provenance through the live
                    # Sources rebuild, including its archived-date suffix.
                    "original_url",
                    "requested_url",
                    "timestamp",
                    "formatted_date",
                    "openalex_source_id",
                    "source",
                    "source_engine",
                    "pmid",
                    "pmcid",
                    "arxiv_id",
                    "isbn",
                    "citations",
                    "is_open_access",
                    "abstract",
                    "metadata",
                    # The collector's own page title. This rebuild drops
                    # every key not named here, so the live report path
                    # (``_format_citations`` -> here -> the renderer) lost
                    # it while a direct render kept it.
                    FETCHED_TITLE_KEY,
                ):
                    val = result.get(key)
                    if val is not None:
                        link[key] = val
                links.append(link)
        except Exception:
            # Log the specific error for debugging
            logger.exception("Error extracting link from result")
            continue
    return links


def _sanitize_sources_field(value: str) -> str:
    """Flatten a value being rendered into the Sources block.

    Titles come from search-result metadata — i.e. from whatever a page
    calls itself — and were previously rendered verbatim, so a crafted
    title could forge a whole extra numbered citation pointing anywhere.
    URLs get the same treatment because the non-library canonical-key
    fallback returns arbitrary scheme-less paths unchanged.

    Control characters are replaced with a space rather than dropped, so a
    forged ``\n[9] Fake`` degrades to visible text on the same line instead
    of silently vanishing.
    """
    if not value:
        return value
    return "".join(
        " " if is_line_breaking_char(ch) else ch for ch in value
    ).strip()


def _owned_chunk_display(recorded: object, canon: str) -> str | None:
    """Return a recorded chunk anchor only if it names *this* citation.

    .. note::
        This check is the PRIMARY control, not defence in depth. Only
        ``SearchResultsCollector`` strips a producer-supplied key at
        ingest, and it exists solely in the LangGraph strategy —
        ``source_based``, ``focused_iteration`` and
        ``topic_organization`` all extend ``all_links_of_system`` with
        raw engine dicts. So on those paths a key present here is
        producer-supplied by construction, and the residual is bounded to
        what this function permits: a wrong chunk or view segment of the
        correctly-identified document, never a foreign document or an
        arbitrary URL.

    Shape validation alone is not enough. The value is preferred over the
    entry's own url, so a well-formed anchor for a DIFFERENT document —
    which arrives for free, since ``add_results`` copies the engine's dict
    — would render and persist under this citation. The writer refuses to
    record a foreign anchor; the reader has to refuse to read one, or the
    two disagree about which spelling is authoritative.

    Comparing canonical keys is exact here: ``canonical_url_key`` collapses
    every view of a library document onto one key, so an anchor for the
    same document matches and an anchor for any other does not.
    """
    if not isinstance(recorded, str):
        return None
    display = preferred_chunk_display(recorded)
    if display is None:
        return None
    return display if canonical_url_key(display) == canon else None


def source_url_field(link: Dict) -> object:
    """The field a source's identity is read from: ``link`` first.

    ``SearchResultsCollector`` keys citations on
    ``_citation_dedup_key(result["link"])``, and records the authoritative
    chunk anchor or normalized URL in ``link``. A result carrying BOTH
    fields with divergent values (e.g. ``link`` carrying the collector's
    rebuilt anchor while ``url`` retains an anchor-less engine string) must
    consistently read ``link`` so the entry groups under its intended chunk
    anchor rather than fanning out into an unintended second group.

    ``link`` wins because it is what the collector keys on, what
    ``_format_results`` shows the agent next to the ``[N]`` marker, and
    what ``find_by_url`` resolves — i.e. it is what the citation index
    actually means. ``url`` remains the fallback for the raw engine dicts
    the non-LangGraph strategies extend ``all_links_of_system`` with,
    some of which set only that key.
    """
    return link.get("link") or link.get("url") or ""


# ---------------------------------------------------------------------------
# Uncited-source filter engine (#5379).
#
# Heuristic parser: extracts citation signals (numeric bracket indices,
# tagged citations, "Source N" phrases, inline URLs) from report prose so
# the final ``## Sources`` bibliography can drop accumulated search hits
# that were never cited. Over-inclusion is safe (extra bibliography line);
# under-inclusion silently drops a cited source, so every ambiguous or
# indeterminate case falls back to the full list in ``fallback`` mode.
# ---------------------------------------------------------------------------

_MAX_RANGE_SPAN = 1000
_MAX_TOTAL_EXPANSION = 5000
_MAX_RANGE_ENDPOINT_DIGITS = 18
# This bounds candidate expansion work, not the report or its context. On
# exhaustion the parser becomes indeterminate and keeps the full bibliography.
_MAX_URL_CANDIDATE_CHARS = 1_000_000


class _URLCandidateBudgetExceeded(Exception):
    """The citation parser must fall back before expanding more candidates."""


class _URLCandidateBudget:
    def __init__(self):
        self.remaining = _MAX_URL_CANDIDATE_CHARS

    def consume(self, characters: int) -> None:
        if characters > self.remaining:
            raise _URLCandidateBudgetExceeded
        self.remaining -= characters


_FENCE_MARKERS = ("```", "~~~")

# Ordinary prose tokens that must NOT force the indeterminate fallback:
# ordinals ("1st", "2nd", "3rd"), units with g-z suffixes ("5x", "100m"),
# multi-letter suffixes ("12px"), percentages ("50%"). Single bracket
# tokens of digits immediately followed by such suffixes are prose, not
# attempted citations. Only contents with list/range syntax (",", "-",
# "–", "—", "and", "&") look like attempted citation syntax and may
# trigger indeterminate.
#
# Single-letter a-f suffixes ("3a", "4b", "12c") are DELIBERATELY excluded:
# they are regex-identical to sub-reference citations, so they stay on the
# safe (indeterminate → fallback) side instead of being silently ignored.
_PROSE_TOKEN_RE = re.compile(r"\d+[A-Za-z%]+")
_SUBREF_SINGLE_LETTERS = frozenset("abcdefABCDEF")
_LIST_SYNTAX_RE = re.compile(r",|[\-–—]|&|\band\b", re.IGNORECASE)

# Single/double ASCII + CJK full-width brackets with numeric content:
# [N], [N, M], [[N]], 【N】, [N-M], [N–M], [ 1, 2 ] (outer spaces allowed).
# Letters excluded so [GPT-4]/[COVID-19] never match here
# (handled/rejected as tagged).
_BRACKET_NUMERIC_RE = re.compile(
    r"[\[【]\[?\^?\s*([0-9][0-9\s,\-–—]*[0-9]|[0-9])\s*\]?[\]】]"
)

# Tagged citations: [arxiv-1], [arxiv.org-2], [openai.com-2]. Tag must
# contain a letter or dot so pure ranges ([1-3]) never match as tag "1".
_TAGGED_CITATION_RE = re.compile(r"\[(?P<tag>[A-Za-z0-9._/\-]+)-(?P<idx>\d+)\]")

# "Source 1", "Sources 1 and 2", "sources 1, 2 & 3", "Sources 1-3"
# Optional ":"/"#" separators ("Sources: 1, 2", "Source #2") are accepted.
# (case-insensitive). Dash/en-dash/em-dash ranges are part of the capture
# so "Sources 1-3" parses the whole range instead of just the "1" prefix
# (which previously kept only source 1 with no fallback).
_SOURCE_PHRASE_RE = re.compile(
    r"(?i)\bsources?(?:\s*[:#]\s*|\s+)(?P<nums>\d+(?:\s*[\-–—]\s*\d+)?"
    r"(?:\s*(?:,|and|&)\s*\d+(?:\s*[\-–—]\s*\d+)?)*)"
)

# Broad capture; paren-balance + trailing-punct peeling happens per match.
# Bracketed IP-literal authorities need their own branch: brackets delimit
# the host here, while brackets elsewhere still delimit prose citations.
_IP_LITERAL_AUTHORITY = (
    r"https?://(?:[^\s<>\[\]\"/?#@]*@)?\[[^\s<>\[\]\"/?#@]+\]"
)
_IP_LITERAL_URL_RE = re.compile(_IP_LITERAL_AUTHORITY, re.IGNORECASE)

# Replay targets can contain IP authorities and bracketed path/query values.
# A replay-specific scanner retains nested pairs while unmatched closing
# brackets still delimit surrounding prose, as they do for ordinary URLs.
_WAYBACK_REPLAY_PREFIX = (
    r"https?://web\.archive\.org(?::(?:80|443))?/web/"
    r"[0-9]{14}(?:id_|if_)?/https?://"
)
_WAYBACK_REPLAY_PREFIX_RE = re.compile(_WAYBACK_REPLAY_PREFIX, re.IGNORECASE)
_WAYBACK_REPLAY_URL = _WAYBACK_REPLAY_PREFIX + r'[^\s<>"]+'

# In addition to absolute URLs and markdown-link destinations
# (``](/library/…)``), bare library routes (``/library/document/…``,
# ``/lib/document/…``) are in scope by design: prose commonly mentions
# them outside link syntax, and dropping a bare-path mention is the same
# citation-loss class as dropping a markdown destination.
_ORDINARY_ABSOLUTE_URL = (
    rf"{_IP_LITERAL_AUTHORITY}[^\s<>\[\]\"]*"
    r"|https?://[^\s<>\[\]\"]+"
)
_RELATIVE_CITATION_URL = (
    r"|\]\([ \t]*(?P<relative>/[^\s<>\"]*)"
    r"|(?P<bare_library>/(?:library|lib)/document/[^\s<>\[\]\"]+)"
)
_URL_RE = re.compile(
    rf"(?P<absolute>{_WAYBACK_REPLAY_URL}|{_ORDINARY_ABSOLUTE_URL})"
    + _RELATIVE_CITATION_URL,
    re.IGNORECASE,
)
# Find a replay prefix before scanning its target. Capturing the whole
# remaining suffix here would rescan adjacent wrapped links quadratically.
_URL_START_RE = re.compile(
    rf"(?P<absolute>{_WAYBACK_REPLAY_PREFIX}|{_ORDINARY_ABSOLUTE_URL})"
    + _RELATIVE_CITATION_URL,
    re.IGNORECASE,
)

# ``canonical_url_key`` lowercases scheme/host but keeps path case, and the
# library-route parser matches ``/library/document/`` case-sensitively
# (like the Flask route and ``library_resolver``). The prose regex above
# captures ``/LIBRARY/Document/…`` case variants, so normalize just the
# route prefix to lowercase before keying — the doc id itself keeps its
# case. Without this the mention is captured but never matches its link.
_BARE_LIBRARY_PREFIX_RE = re.compile(
    r"^/(?:library|lib)/document/", re.IGNORECASE
)


def _normalize_bare_library_prefix(candidate: str) -> str:
    """Lowercase only the ``/library|lib/document/`` route prefix."""
    return _BARE_LIBRARY_PREFIX_RE.sub("/library/document/", candidate)


# Sentence-final + CJK punctuation stripped from prose URLs. Deliberately
# excludes closing delimiters ( ) ] } > ' " ` ) — those are balanced
# separately so a balanced ")" before a final "." survives.
_URL_SENTENCE_PUNCT = ".,;:!?。、,!?;:「」『』、…·—–"
_URL_WRAPPER_PUNCT = "'\"`]}>】”’»》“‘«›"
_URL_WRAPPERS = {
    "(": ")",
    "[": "]",
    "{": "}",
    "【": "】",
    "「": "」",
    "『": "』",
    "《": "》",
    "〈": "〉",
    "“": "”",
    "„": "“",
    "‘": "’",
    "‚": "‘",
    "«": "»",
    "»": "«",
    "‹": "›",
    "'": "'",
    '"': '"',
    "`": "`",
    "*": "*",
    "_": "_",
}
_MARKDOWN_URL_END_RE = re.compile(
    r"[ \t\r\n]*(?:(?:\"(?:\\.|[^\"\\])*\"|'(?:\\.|[^'\\])*'"
    r"|\((?:\\.|[^()\\])*\))[ \t\r\n]*)?\)"
)
_MARKDOWN_URL_ESCAPE_RE = re.compile(r"\\([!-/:-@\[-`{-~])")

UNCITED_SOURCES_MODE_KEY = "report.uncited_sources_mode"
UNCITED_SOURCES_MODES = ("fallback", "strict", "disabled")
UNCITED_SOURCES_DEFAULT = "fallback"


def resolve_uncited_sources_mode(raw=None, settings_snapshot=None) -> str:
    """Normalize the ``report.uncited_sources_mode`` setting.

    ``raw`` is an explicitly provided value (e.g. from a strategy's
    settings snapshot, already unwrapped). ``None`` resolves from
    ``settings_snapshot`` (when given) or the thread settings context,
    defaulting to ``"fallback"`` when no context is bound. Unknown values
    degrade to ``"fallback"`` so a stale or hand-edited setting can never
    wipe out a bibliography. Settings lookup failures also degrade to the
    default: rendering a report must never crash on a preference read.
    """
    if raw is None:
        try:
            from ..config.thread_settings import get_setting_from_snapshot

            raw = get_setting_from_snapshot(
                UNCITED_SOURCES_MODE_KEY,
                UNCITED_SOURCES_DEFAULT,
                settings_snapshot=settings_snapshot,
            )
        except Exception:
            return UNCITED_SOURCES_DEFAULT
    if isinstance(raw, dict):
        raw = raw.get("value", UNCITED_SOURCES_DEFAULT)
    if isinstance(raw, str) and raw.strip() in UNCITED_SOURCES_MODES:
        return raw.strip()
    return UNCITED_SOURCES_DEFAULT


def _strip_fence_prefix(s: str) -> str:
    """Strip Markdown quote/list prefixes from a line-start string.

    Handles ``>``, ``-``/``*``/``+``, ``1.``/``1)`` nesting (e.g.
    ``> - ```js``). Capped at 32 iterations so a hostile single line
    of ``\"1. \"`` repetitions cannot turn the per-line slice into a
    quadratic scan; realistic nesting is single digits.
    """
    for _ in range(32):
        if s.startswith(">"):
            s = s[1:].lstrip(" \t")
            continue
        if len(s) >= 2 and s[0] in "-*+" and s[1] in " \t":
            i = 1
            while i < len(s) and s[i] in " \t":
                i += 1
            s = s[i:]
            continue
        m = re.match(r"\d+[.)][ \t]+", s)
        if m:
            s = s[m.end() :]
            continue
        break
    return s


def _is_fence_line(line: str, marker: str) -> bool:
    """True when ``line`` can *open* a ``marker`` code block.

    A fence is recognized at the start of a line after optional
    spaces/tabs indent *and* optional Markdown quote/list prefixes
    (``>``, ``-``/``*``/``+``, ``1.``/``1)`` — e.g. ``> ```json`` or
    ``- ```js``). Those shapes are how models quote tool output and
    search results; ignoring the opener while counting a later plain
    ````` closer pairs the closer with the next real opener and strips
    real citations between them (source loss in the default mode).

    A line whose stripped remainder starts with ``marker`` but contains
    a second ``marker`` *after the initial fence run* (````` decorate
    [8] ``` end``) is a decorative single-line run, not an opener, and
    returns False so it never opens a block. A longer single run
    (`` `````` ``) has no second marker after its run and still opens.
    Closers are deliberately not vetted here — see
    :func:`_is_fence_closer_line`.
    """
    content = line.rstrip("\r\n")
    s = _strip_fence_prefix(content.lstrip(" \t"))
    if not s.startswith(marker):
        return False
    # Initial fence run (3+ backticks/tildes): a longer single run is
    # one fence, not two.
    fence_char = marker[0]
    run = 0
    while run < len(s) and s[run] == fence_char:
        run += 1
    # Second marker after the run → decorative single-line run.
    if marker in s[run:]:
        return False
    return True


def _is_fence_closer_line(line: str, marker: str) -> bool:
    """True when ``line`` can *close* an open ``marker`` code block.

    Same prefix rule as the opener, but with no double-marker veto: a
    closer carrying trailing text (````` done ``` ``) or a longer fence
    (`` `````` `` closing a `` ``` `` block) must still close. Vetoing
    it here pairs the opener with the *next* opener and strips real
    prose between them with no indeterminate fallback.
    """
    content = line.rstrip("\r\n")
    s = _strip_fence_prefix(content.lstrip(" \t"))
    return s.startswith(marker)


def _find_fence_positions(prose: str, marker: str) -> list[int]:
    """Return line-start offsets of ``marker`` fence *openers*.

    Recognizes line-anchored markers plus blockquoted/bulleted forms
    (``> ```json``, ``- ```js``) via :func:`_is_fence_line`; inline
    decorative runs and single-line double-marker runs are not openers.
    Closers (which carry no veto) are paired statefully in
    :func:`_strip_marker_blocks`, not by blind consecutive pairing.
    """
    positions: list[int] = []
    offset = 0
    for line in prose.splitlines(keepends=True):
        if _is_fence_line(line, marker):
            positions.append(offset)
        offset += len(line)
    return positions


def _strip_marker_blocks(prose: str, marker: str) -> tuple[str, bool]:
    """Strip paired ``marker`` blocks; True dangling if odd.

    Stateful pairing: the double-marker veto applies only to *openers*
    (so ````` decorate [8] ``` end`` never opens), while any
    line-anchored ``marker`` *closes* an open block (so ````` done ```
    `` and six-backtick `` `````` `` closers still close instead of
    letting the opener pair across real prose). Full opener-through-
    closer lines are excluded; an unterminated block returns the
    original prose with dangling True so the caller falls back to the
    full bibliography.
    """
    lines = prose.splitlines(keepends=True)
    if not lines:
        return prose, False
    spans: list[tuple[int, int]] = []
    in_block = False
    block_start = 0
    for i, line in enumerate(lines):
        if not in_block:
            if _is_fence_line(line, marker):
                in_block = True
                block_start = i
        else:
            if _is_fence_closer_line(line, marker):
                in_block = False
                spans.append((block_start, i))
    if in_block:
        return prose, True
    if not spans:
        return prose, False
    exclude: set[int] = set()
    for span_start, span_end in spans:
        for i in range(span_start, span_end + 1):
            exclude.add(i)
    return "".join(
        line for i, line in enumerate(lines) if i not in exclude
    ), False


def _strip_fenced_code(prose: str) -> tuple[str, bool]:
    """Strip fenced code blocks (``` and ~~~); dangling either → indeterminate.

    Fence lines include blockquoted/bulleted forms (``> ```json``,
    ``- ```js``) so a quoted opener pairs with its closer instead of
    letting a later plain marker pair across real prose. Inline
    decorative runs (``~~~ config [2] ~~~``) and single-line
    double-marker runs (```` ``` decorate [8] ``` end``) never *open* a
    block, but any line-anchored marker still *closes* an open block
    (so ````` done ``` `` and six-backtick closers close rather than
    pairing the opener across real prose). Each marker type pairs only
    with itself so a ``` block never swallows across a ~~~ fence. A
    dangling (unterminated) marker of either type marks the parse
    indeterminate instead of stripping half the report (a single stray
    fence in quoted web content would otherwise swallow real citations
    between it and the next block).
    """
    indeterminate = False
    text = prose
    for marker in _FENCE_MARKERS:
        text, dangling = _strip_marker_blocks(text, marker)
        if dangling:
            indeterminate = True
    return text, indeterminate


def _is_all_uppercase_tag(tag: str) -> bool:
    letters = [ch for ch in tag if ch.isalpha()]
    return bool(letters) and all(ch.isupper() for ch in letters)


def _is_ignorable_prose_singleton(content: str) -> bool:
    """True when a digit-led singleton is ordinary prose, not a sub-ref."""
    if "%" in content:
        return True
    m = re.fullmatch(r"\d+([A-Za-z]+)", content)
    if m is None:
        return True
    suffix = m.group(1)
    if len(suffix) >= 2:
        return True
    return suffix not in _SUBREF_SINGLE_LETTERS


def _is_malformed_numeric_content(content: str) -> bool:
    """True when a digit-led bracket looks like a broken citation list.

    Ordinary prose singletons (ordinals ``2nd``/``1st``, ``g``-``z`` units
    ``5x``/``100m``, multi-letter ``12px``, percentages ``50%``) are
    explicitly NOT malformed: they are digits immediately followed by a
    safe suffix with no list/range syntax, so they are ignored instead of
    forcing the full-bibliography fallback (which would defeat ``strict``
    mode's cited-only contract). Single-letter ``a``-``f`` suffixes
    (``3a``/``4b``/``12c``) are sub-reference-shaped and stay indeterminate
    so fallback preserves the bibliography. Only contents containing
    list/range syntax (``,``, ``-``/``–``/``—``, ``and``/``&``) look like
    attempted citation syntax; a bad chunk there
    (``[1,abc,3]``, ``[1-abc]``) stays indeterminate so fallback preserves
    the bibliography. Other singletons without list syntax (``[1 abc]``,
    ``[1 and 2]``) also stay indeterminate — ignoring them would risk
    silently dropping an intended citation.
    """
    content = content.strip()
    if not content or not content[0].isascii() or not content[0].isdigit():
        return False
    if not _LIST_SYNTAX_RE.search(content):
        # Single token, no list/range syntax. Prose shapes (digits +
        # letters/% suffix) are ignored; anything else falls through to
        # the chunk check below (e.g. "1 abc" has a space so it does not
        # match _PROSE_TOKEN_RE and is still indeterminate).
        if _PROSE_TOKEN_RE.fullmatch(content):
            if _is_ignorable_prose_singleton(content):
                return False
    for chunk in content.split(","):
        chunk = chunk.strip()
        if not chunk:
            return True
        if chunk.isascii() and chunk.isdigit():
            continue
        if re.fullmatch(r"[0-9]+\s*[\-–—]\s*[0-9]+", chunk):
            continue
        return True
    return False


def _peel_url_candidate(raw: str) -> str:
    """Peel punctuation and unmatched delimiters from an ambiguous prose URL.

    Sentence punctuation is peeled BEFORE balancing delimiters: a balanced
    Wikipedia-style ``...(lang).`` first loses the final ``.`` leaving a
    balanced ``...(lang)`` which is kept. The old order stripped ``.`` and
    ``)`` together via a single ``rstrip``, turning the balanced URL into
    the unbalanced ``...(lang`` and dropping the source.
    """
    url = raw
    authority_match = _IP_LITERAL_URL_RE.match(url)
    # Parentheses in credentials or an IPvFuture host belong to the
    # authority, so they cannot balance path or prose-wrapper parentheses.
    suffix = url[authority_match.end() :] if authority_match else url
    unmatched_closing = max(0, suffix.count(")") - suffix.count("("))
    unmatched_brackets = max(0, url.count("]") - url.count("["))
    end = len(url)
    while end:
        trailing = url[end - 1]
        if trailing == "]" and not unmatched_brackets:
            # A balanced closing bracket belongs to an IP-literal host.
            break
        if trailing in _URL_SENTENCE_PUNCT or trailing in _URL_WRAPPER_PUNCT:
            if trailing == "]":
                unmatched_brackets -= 1
            end -= 1
            continue
        if trailing == ")" and unmatched_closing:
            unmatched_closing -= 1
            end -= 1
            continue
        break
    return url[:end]


def _url_citation_candidates(
    match: re.Match, budget: Optional[_URLCandidateBudget] = None
) -> Set[str]:
    """Preserve exact destinations; keep ambiguous bare URL interpretations."""
    if budget is None:
        budget = _URLCandidateBudget()
    group = next(
        name
        for name in ("absolute", "relative", "bare_library")
        if match.group(name) is not None
    )
    raw = match.group(group)
    budget.consume(len(raw))
    start = match.start(group)
    text = match.string
    following = text[match.end() : match.end() + 1]
    if start and text[start - 1] == "<" and following == ">":
        # Angle brackets delimit URL content, including punctuation.
        return {_MARKDOWN_URL_ESCAPE_RE.sub(r"\1", raw)}
    if start and text[start - 1] == '"' and following == '"':
        # Quotes delimit the URL, but sentence punctuation inside them may
        # belong to the surrounding sentence; keep both readings.
        budget.consume(len(raw))
        return {raw, _peel_url_candidate(raw)}

    prefix = start
    while prefix and text[prefix - 1] in " \t\r\n":
        prefix -= 1
    if text[max(0, prefix - 2) : prefix] == "](":
        depth = 0
        escaped = False
        for position, char in enumerate(raw):
            if escaped:
                escaped = False
            elif char == "\\":
                escaped = True
            elif char == "(":
                depth += 1
            elif char == ")":
                if not depth:
                    budget.consume(position)
                    return {_MARKDOWN_URL_ESCAPE_RE.sub(r"\1", raw[:position])}
                depth -= 1
        if not depth and _MARKDOWN_URL_END_RE.match(text, match.end()):
            return {_MARKDOWN_URL_ESCAPE_RE.sub(r"\1", raw)}

    # Remove one sentence mark outside any surrounding wrappers. Further
    # punctuation, underscores and stars may be significant URL content.
    if raw and raw[-1] in _URL_SENTENCE_PUNCT:
        budget.consume(len(raw) - 1)
        url = raw[:-1]
    else:
        url = raw
    wrapped = False
    for _ in range(32):
        if not start:
            break
        closer = _URL_WRAPPERS.get(text[start - 1])
        if not closer or not url.endswith(closer):
            break
        authority = _IP_LITERAL_URL_RE.match(url)
        if closer == "]" and authority and authority.end() == len(url):
            # The captured bracket closes the host, not an outer wrapper.
            break
        if (
            closer in "])"
            and _WAYBACK_REPLAY_PREFIX_RE.match(url)
            and url.count(text[start - 1]) == url.count(closer)
        ):
            # The replay branch excludes an unmatched outer closer, so a
            # balanced final bracket/parenthesis belongs to the target.
            break
        budget.consume(len(url) - len(closer))
        url = url[: -len(closer)]
        start -= 1
        wrapped = True
    if wrapped:
        # The wrapper closer is prose, but any sentence mark it hides may
        # be URL content or sentence punctuation; keep both readings so
        # ``"https://x.example/a."`` cites ``a`` and ``a.`` alike.
        budget.consume(len(url))
        return {url, url.rstrip(_URL_SENTENCE_PUNCT)}

    candidates = {raw, url}
    if _WAYBACK_REPLAY_PREFIX_RE.match(url):
        # A bare replay followed immediately by [N] can spell either a
        # target suffix or a prose reference. Keep both readings; exact
        # angle/Markdown destinations already returned above.
        # Find the final contiguous run without retaining a match object
        # for every bracket. Charge each interpretation BEFORE slicing it:
        # preserving all prefixes as strings otherwise takes quadratic space.
        run_start = None
        previous_end = -1
        for reference in _BRACKET_NUMERIC_RE.finditer(url):
            if reference.start() != previous_end:
                run_start = reference.start()
            previous_end = reference.end()
        if previous_end == len(url) and run_start is not None:
            for reference in _BRACKET_NUMERIC_RE.finditer(url, run_start):
                end = reference.start()
                budget.consume(end)
                candidates.add(url[:end])
    # A run of three or more trailing sentence marks reads as an
    # ellipsis-sized prose unit (``See https://x.example/a... Also``), so
    # the fully-peeled reading stays plausible too. Shorter runs peel
    # exactly one mark: a sentence contributes a single terminator, and
    # peeling deeper would strip real URL content (``Read
    # https://x.example/a..`` where the path genuinely ends in ``.``).
    peeled_end = len(raw)
    while peeled_end and raw[peeled_end - 1] in _URL_SENTENCE_PUNCT:
        peeled_end -= 1
    if len(raw) - peeled_end >= 3:
        budget.consume(peeled_end)
        candidates.add(raw[:peeled_end])
    authority = _IP_LITERAL_URL_RE.match(url)
    suffix = url[authority.end() :] if authority else url
    if suffix.count(")") > suffix.count("("):
        # A closing parenthesis can be URL content or belong to prose
        # whose opening parenthesis precedes intervening words.
        budget.consume(len(raw))
        candidates.add(_peel_url_candidate(raw))
    return candidates


def _has_malformed_numeric_brackets(text: str) -> bool:
    """Check whole bracket contents in one pass, including long/multiline ones."""
    start: Optional[int] = None
    for position, char in enumerate(text):
        if char in "[【":
            if start is None or position == start:
                start = position + 1
        elif char in "]】" and start is not None:
            content = text[start:position].lstrip()
            if content.startswith("^"):
                content = content[1:]
            if _is_malformed_numeric_content(content):
                return True
            start = None
    return False


def _mask_replay_ip_literal(match: re.Match) -> str:
    """Mask only a parseable IP authority inside a verified replay target."""
    authority = match.group(0)
    try:
        _ = urlsplit(authority).port
    except ValueError:
        return authority
    return authority.replace("[", "(").replace("]", ")")


def _citation_url_matches(text: str) -> List[re.Match]:
    """Scan replay targets once, retaining balanced URL brackets/wrappers."""
    matches: List[re.Match] = []
    position = 0
    while (match := _URL_START_RE.search(text, position)) is not None:
        raw = match.group("absolute")
        if raw and _WAYBACK_REPLAY_PREFIX_RE.match(raw):
            prefix = match.start("absolute")
            while prefix and text[prefix - 1] in " \t\r\n":
                prefix -= 1
            parenthesized = bool(prefix and text[prefix - 1] == "(")
            bracket_depth = 0
            paren_depth = 0
            escaped = False
            end = match.end()
            while end < len(text):
                character = text[end]
                if character.isspace() or character in '<>"':
                    break
                if escaped:
                    escaped = False
                elif parenthesized and character == "\\":
                    escaped = True
                elif character == "[":
                    bracket_depth += 1
                elif character == "]":
                    if not bracket_depth:
                        break
                    bracket_depth -= 1
                elif parenthesized and character == "(":
                    paren_depth += 1
                elif parenthesized and character == ")":
                    if not paren_depth:
                        break
                    paren_depth -= 1
                end += 1
            bounded = _URL_RE.match(text, match.start(), end)
            if bounded is not None:
                match = bounded
        matches.append(match)
        position = match.end()
    return matches


def _mask_ip_literal_authority(
    match: re.Match, budget: Optional[_URLCandidateBudget] = None
) -> str:
    """Keep valid captured URL hosts out of the numeric citation parser."""
    url = match.group("absolute")
    if not url:
        return match.group(0)
    authority_match = _IP_LITERAL_URL_RE.match(url)
    if authority_match is None:
        if _WAYBACK_REPLAY_PREFIX_RE.match(url):
            for candidate in _url_citation_candidates(match, budget):
                try:
                    if not _is_wayback_replay_url(
                        candidate, urlsplit(candidate)
                    ):
                        continue
                except ValueError:
                    continue
                # Only IP-host brackets are masked. Path/query brackets
                # and surrounding numeric or malformed references remain
                # visible to the normal citation parser.
                return _IP_LITERAL_URL_RE.sub(_mask_replay_ip_literal, url)
        return url
    for candidate in _url_citation_candidates(match, budget):
        if _IP_LITERAL_URL_RE.match(candidate) is None:
            # A malformed Markdown destination can end before its host.
            continue
        try:
            # Accessing port rejects malformed or out-of-range ports.
            _ = urlsplit(candidate).port
        except ValueError:
            continue
        break
    else:
        # An invalid authority must not hide an ambiguous citation bracket.
        return url
    authority = authority_match.group(0)
    # Retain URL text so masking cannot join citation phrases or ranges
    # across an authority. Non-whitespace delimiters also keep a zone ID
    # ending in "Sources" separate from any number following the URL.
    masked_authority = authority.replace("[", "(").replace("]", ")")
    return masked_authority + url[authority_match.end() :]


def _parse_bracket_content(
    content: str, indices: Set[int], state: dict
) -> None:
    """Expand comma lists + dash/en-dash ranges into ``indices``.

    Sets ``state["indeterminate"]`` when any range exceeds span, digit,
    or aggregate budgets instead of keeping endpoints-only (which would
    suppress the safety-net fallback with a partial result).
    """
    for chunk in content.split(","):
        chunk = chunk.strip()
        if not chunk:
            continue
        m = re.fullmatch(r"(\d+)\s*[\-–—]\s*(\d+)", chunk)
        if m:
            lo_s, hi_s = m.group(1), m.group(2)
            if (
                len(lo_s) > _MAX_RANGE_ENDPOINT_DIGITS
                or len(hi_s) > _MAX_RANGE_ENDPOINT_DIGITS
            ):
                state["indeterminate"] = True
                continue
            try:
                lo, hi = int(lo_s), int(hi_s)
            except ValueError:
                state["indeterminate"] = True
                continue
            if lo > hi:
                lo, hi = hi, lo
            if hi - lo > _MAX_RANGE_SPAN:
                state["indeterminate"] = True
                continue
            if state["expanded"] + (hi - lo + 1) > _MAX_TOTAL_EXPANSION:
                state["indeterminate"] = True
                continue
            for i in range(lo, hi + 1):
                indices.add(i)
            state["expanded"] += hi - lo + 1
        elif chunk.isascii() and chunk.isdigit():
            if len(chunk) > _MAX_RANGE_ENDPOINT_DIGITS:
                state["indeterminate"] = True
                continue
            try:
                indices.add(int(chunk))
            except ValueError:
                state["indeterminate"] = True
        else:
            continue


def extract_cited_indices_and_urls(
    prose: str,
) -> Tuple[Set[int], Set[str], bool]:
    """Parse citation signals from report body prose.

    Returns ``(cited_indices, cited_url_keys, indeterminate)`` where
    ``cited_url_keys`` are ``canonical_url_key`` values. ``indeterminate``
    is True when a range, bracket, or fence is ambiguous or the shared URL
    candidate-work budget is exhausted. The result must then be treated as
    unreliable (caller falls back to the full list without truncating prose).

    Recognized signals: numeric brackets (outer spaces allowed:
    ``[ 1, 2 ]``), tagged citations, ``Source N`` / ``Sources: N`` /
    ``Source #N`` phrases, absolute URLs,
    markdown-link library destinations (``](/library/…)``) AND bare
    library routes (``/library/document/…``, ``/lib/document/…``) mentioned
    outside link syntax. Route-prefix case is normalized for both bare
    mentions and markdown destinations, so ``/LIBRARY/Document/…``
    retains like the lowercase form. Fenced code (``` and ~~~,
    including blockquoted/bulleted forms like ``> ```json``) is stripped;
    inline runs and single-line double-marker runs never swallow
    citations; a dangling fence of either type marks the parse
    indeterminate.

    Ordinary prose singletons (ordinals ``2nd``, ``g``-``z`` units
    ``5x``/``100m``, multi-letter ``12px``, percentages ``50%``) are
    ignored — they never set indeterminate — so normal writing does not
    force the full-bibliography fallback in ``strict`` mode.
    Single-letter ``a``-``f`` suffixes (``3a``) stay indeterminate as
    sub-reference-shaped. Only digit-led brackets with list/range syntax
    that fail to parse (``[1,abc,3]``) are indeterminate.

    Heuristic: over-includes on years/array indices; over-inclusion is
    harmless because the fallback keeps extra lines, while
    under-inclusion drops cited sources.
    """
    indices: Set[int] = set()
    url_keys: Set[str] = set()
    state = {"indeterminate": False, "expanded": 0}
    candidate_budget = _URLCandidateBudget()
    if not prose or not prose.strip():
        return indices, url_keys, False

    text, fence_indeterminate = _strip_fenced_code(prose)
    if fence_indeterminate:
        state["indeterminate"] = True

    # Keep the original text for URL extraction. A digit-led IPv6 address
    # must not look like a broken [N] citation and force all sources back in.
    # Use the same capture boundaries as URL extraction below. An IPv6
    # literal inside another URL's path or query is not a separate host.
    url_matches = _citation_url_matches(text)
    citation_parts: List[str] = []
    position = 0
    try:
        for match in url_matches:
            citation_parts.append(text[position : match.start()])
            citation_parts.append(
                _mask_ip_literal_authority(match, candidate_budget)
            )
            position = match.end()
    except _URLCandidateBudgetExceeded:
        return indices, url_keys, True
    citation_parts.append(text[position:])
    citation_text = "".join(citation_parts)

    for m in _BRACKET_NUMERIC_RE.finditer(citation_text):
        _parse_bracket_content(m.group(1), indices, state)

    if _has_malformed_numeric_brackets(citation_text):
        state["indeterminate"] = True

    for m in _TAGGED_CITATION_RE.finditer(citation_text):
        tag, idx_s = m.group("tag"), m.group("idx")
        # Check this after matching instead of using overlapping greedy
        # groups: those take quadratic time on long malformed brackets.
        if not re.search(r"[A-Za-z.]", tag):
            continue
        if _is_all_uppercase_tag(tag):
            continue
        if len(idx_s) > _MAX_RANGE_ENDPOINT_DIGITS:
            # Implausibly large tag index (no bibliography has 10^18
            # entries): clearly prose/hostile, not a citation. Ignore
            # instead of marking indeterminate so a single token like
            # [a-999…] cannot force the fallback for every reader.
            continue
        try:
            indices.add(int(idx_s))
        except ValueError:
            state["indeterminate"] = True

    for m in _SOURCE_PHRASE_RE.finditer(citation_text):
        # Expand comma lists AND dash ranges ("Sources 1-3") with the same
        # budget-checked helper as bracket citations, so "1-3" yields
        # {1, 2, 3} instead of just endpoints. "and"/"&" normalize to ",".
        nums = m.group("nums")
        nums_norm = re.sub(r"\s*(?:and|&)\s*", ",", nums, flags=re.IGNORECASE)
        _parse_bracket_content(nums_norm, indices, state)

    for m in url_matches:
        try:
            candidates = _url_citation_candidates(m, candidate_budget)
        except _URLCandidateBudgetExceeded:
            return indices, url_keys, True
        # Case-variant library routes (``/LIBRARY/Document/d2``): the regex
        # captures them (bare mentions and markdown destinations) but the
        # canonical key is prefix-case-sensitive, so also key the
        # prefix-normalized spelling. Applies to both branches — a
        # markdown destination is the stronger citation signal and must
        # not lose while the bare mention of the same route retains.
        for base in list(candidates):
            if _BARE_LIBRARY_PREFIX_RE.match(base):
                try:
                    # /lib/ can expand to /library/; account before copying.
                    candidate_budget.consume(len(base) + 4)
                except _URLCandidateBudgetExceeded:
                    return indices, url_keys, True
                candidates.add(_normalize_bare_library_prefix(base))
        for candidate in candidates:
            if not candidate:
                continue
            try:
                key = canonical_url_key(candidate)
            except Exception:
                continue
            if key:
                url_keys.add(key)

    return indices, url_keys, state["indeterminate"]


def filter_cited_links(
    all_links: List[Dict],
    prose: Optional[str],
    mode: str = "fallback",
) -> List[Dict]:
    """Keep only links cited in ``prose`` (by index or URL).

    Modes: ``fallback`` (default) returns ``all_links`` when extraction
    is empty/indeterminate or nothing matches; ``strict`` returns only
    matches (possibly empty); ``disabled`` returns ``all_links``.
    Indeterminate input falls back to ``all_links`` in every mode.
    ``None`` resolves from the ``report.uncited_sources_mode`` setting.
    """
    mode = resolve_uncited_sources_mode(mode)
    if not all_links:
        return all_links
    if mode == "disabled" or not prose or not prose.strip():
        return all_links

    cited_indices, cited_urls, indeterminate = extract_cited_indices_and_urls(
        prose
    )
    if indeterminate:
        logger.warning(
            "filter_cited_links: indeterminate citation parse; "
            "falling back to all {} links",
            len(all_links),
        )
        return all_links
    if not cited_indices and not cited_urls:
        if mode == "fallback":
            logger.warning(
                "filter_cited_links: no citations parsed from prose "
                "({} links); falling back to full bibliography",
                len(all_links),
            )
            return all_links
        return []

    kept: List[Dict] = []
    for link in all_links:
        if not isinstance(link, dict):
            continue
        idx = link.get("index", "")
        try:
            idx_int = int(str(idx).strip())
        except (ValueError, TypeError):
            idx_int = None
        if idx_int is not None and idx_int in cited_indices:
            kept.append(link)
            continue
        raw = source_url_field(link)
        if isinstance(raw, str) and raw:
            try:
                key = canonical_url_key(raw)
            except Exception:
                key = ""
            if key and key in cited_urls:
                kept.append(link)

    if not kept and mode == "fallback":
        logger.warning(
            "filter_cited_links: {} cited indices / {} cited urls matched "
            "nothing in {} links; falling back to full bibliography",
            len(cited_indices),
            len(cited_urls),
            len(all_links),
        )
        return all_links
    return kept


def count_distinct_sources(all_links: List[Dict]) -> int:
    """How many distinct sources a report cites (document-level count).

    ``len(all_links_of_system)`` counts OCCURRENCES, not sources: the
    LangGraph collector stores one entry per ``(url, snippet)`` pair, and
    ``source_based`` / ``focused_iteration`` / ``topic_organization``
    extend the list with raw engine dicts and no URL dedup at all. Both
    shapes put one source in the list several times, so every "N sources"
    number must group at the document level rather than take a length.

    Counts by canonical URL key, so distinct library documents are counted
    once regardless of how many distinct chunks or views are cited.
    The relation across reporting layers is:
        distinct sources <= bibliography lines <= citation indices.
    """
    seen: set[str] = set()
    for link in all_links or []:
        if not isinstance(link, dict):
            continue
        raw = source_url_field(link)
        if not isinstance(raw, str):
            continue
        canon = canonical_url_key(raw)
        if canon:
            seen.add(canon)
    return len(seen)


def format_links_to_markdown(
    all_links: List[Dict],
    prose: Optional[str] = None,
    uncited_mode: Optional[str] = "fallback",
) -> str:
    parts: list[str] = []
    uncited_mode = resolve_uncited_sources_mode(uncited_mode)
    if prose and uncited_mode != "disabled":
        all_links = filter_cited_links(all_links, prose, mode=uncited_mode)
    logger.info(f"Formatting {len(all_links)} links to markdown...")

    if all_links:
        # Group and display ordinary URLs in canonical form. Timestamped
        # Wayback replays keep their complete spelling: their embedded
        # target path/query identifies the historical resource.
        # Group links by (canonical URL, chunk display URL).
        # Canonical key remains per-document so count_distinct_sources,
        # MCP sources, and news metrics do not inflate, while grouping on
        # chunk anchor ensures distinct cited chunks (/chunks#chunk-N) each
        # render their own Sources entry with their own anchor. Unanchored
        # views of the same document (e.g. /pdf and base route) merge together;
        # an unanchored /pdf view deliberately renders on its own line when
        # cited alongside chunks (reversing #5685's collapse so anchorless
        # indices honestly link to /pdf rather than mis-pointing at an arbitrary chunk).
        # Non-library sources display their canonical URL, including the
        # exact replay spelling preserved by canonical_url_key.
        url_to_indices: dict[tuple[str, str], list] = {}
        group_to_title: dict[tuple[str, str], str] = {}
        group_to_capture_date: dict[tuple[str, str], str] = {}
        # Keyed by canonical URL alone, unlike its neighbours. A document
        # cited through both its chunk anchor and its bare URL renders one
        # line per group, and its page title is the same on both; keying
        # this by the group left whichever line did not hold the key
        # showing the search listing's spelling.
        doc_to_fetched_title: dict[str, str] = {}
        group_to_quality: dict[tuple[str, str], int] = {}
        group_to_collection: dict[tuple[str, str], str] = {}
        group_to_display: dict[tuple[str, str], str] = {}
        for link in all_links:
            # Same skip as count_distinct_sources, so a malformed entry
            # costs one bibliography line rather than the whole block.
            if not isinstance(link, dict):
                continue
            raw = source_url_field(link)
            # Skipped, not coerced. These dicts reach here straight from
            # engine output on the non-LangGraph strategies, and
            # canonical_url_key raises on a non-str. Stringifying instead
            # renders a Python repr as a clickable URL and can merge two
            # distinct sources onto one citation — and it would disagree
            # with ``_citation_dedup_key``, which refuses a non-str link
            # outright and whose docstring requires the two to group
            # identically.
            if not isinstance(raw, str):
                continue
            canon = canonical_url_key(raw)
            if not canon:
                continue
            chunk_disp = preferred_chunk_display(raw) or _owned_chunk_display(
                link.get(CHUNK_DISPLAY_KEY), canon
            )
            if chunk_disp:
                key = (canon, chunk_disp)
                disp = chunk_disp
            else:
                key = (canon, "")
                disp = library_display_url(raw) or canon

            url_to_indices.setdefault(key, []).append(link.get("index", ""))
            group_to_title.setdefault(key, link.get("title", "Untitled"))
            if link.get("source_type") == "archive":
                capture_date = link.get("formatted_date")
                if isinstance(capture_date, str) and capture_date.strip():
                    group_to_capture_date.setdefault(key, capture_date.strip())
            # A page's own title, recorded by the fetch ingest path beside a
            # citation whose stored title a search engine scraped. First
            # non-empty wins, as for quality and collection below.
            if canon not in doc_to_fetched_title:
                fetched = link.get(FETCHED_TITLE_KEY)
                if isinstance(fetched, str) and fetched.strip():
                    doc_to_fetched_title[canon] = fetched.strip()
            # Prefer /pdf over bare /library/document/<id> for unanchored display
            curr_disp = group_to_display.get(key)
            if curr_disp is None or (
                curr_disp.endswith(canon) and "/pdf" in disp
            ):
                group_to_display[key] = disp

            # Track journal quality per group (first non-None wins)
            if key not in group_to_quality and link.get("journal_quality"):
                group_to_quality[key] = link["journal_quality"]
            # First non-empty collection name wins (mirrors title/quality).
            # Note: per-group collection tracking is display-only today and
            # anticipates #5722 encoding collection identity into chunk anchors.
            if key not in group_to_collection:
                metadata = link.get("metadata") or {}
                if not isinstance(metadata, dict):
                    metadata = {}
                collection = metadata.get("collection_name")
                if collection:
                    group_to_collection[key] = str(collection)

        # Emit each unique source once, in first-seen order.
        seen: set[tuple[str, str]] = set()
        for link in all_links:
            if not isinstance(link, dict):
                continue
            raw = source_url_field(link)
            if not isinstance(raw, str):
                continue
            canon = canonical_url_key(raw)
            if not canon:
                continue
            chunk_disp = preferred_chunk_display(raw) or _owned_chunk_display(
                link.get(CHUNK_DISPLAY_KEY), canon
            )
            key = (canon, chunk_disp) if chunk_disp else (canon, "")
            if key in seen:
                continue
            title = doc_to_fetched_title.get(canon) or group_to_title[key]
            # Coerced for the same reason the url is skipped: it comes
            # from the same engine dict, and ``.replace`` below raises on
            # a non-str, taking the whole Sources block with it.
            if title is not None and not isinstance(title, str):
                title = str(title)
            capture_date = group_to_capture_date.get(key)
            if capture_date:
                suffix = f" (archived {capture_date})"
                if not (title or "").endswith(suffix):
                    title = f"{title or 'Untitled'}{suffix}"
            if title:
                title = _sanitize_sources_field(
                    title.replace(LDR_APPENDED_SOURCES_SENTINEL, "")
                )
            # Indices arrive as int (from strategy enumeration) or str (from
            # _build_sources_markdown's fallback). Coerce so dedup collapses
            # 1 and "1", and sorted() doesn't TypeError on mixed types.
            indices = sorted(
                {str(i) for i in url_to_indices[key]},
                # ``isdigit()`` is True for non-ASCII digit characters that
                # ``int()`` rejects — e.g. the superscript "\u00b9" — so it
                # alone would raise ValueError here and crash the whole
                # bibliography. Require ASCII before converting.
                key=lambda s: (
                    (0, int(s)) if s.isascii() and s.isdigit() else (1, s)
                ),
            )
            # Sanitised like the title, collection and URL. Indices are
            # LDR-internal enumerations today, but ``_create_documents``
            # PRESERVES a pre-existing ``index`` on a raw engine dict and
            # ``_build_sources_markdown`` reads it back out of persisted
            # ``original_data`` — so "no engine emits this key" is a
            # coincidence, not a control.
            #
            # ``quality_tag`` below is the one field still interpolated
            # raw. It reaches the renderer by the same persisted route,
            # and is safe only incidentally: the fall-through branch that
            # builds it uses ``repr()``, which escapes newlines and
            # non-printables. Incidental, not designed — do not remove
            # that ``repr()`` without sanitising here instead.
            indices = [_sanitize_sources_field(i) for i in indices]
            indices_str = f"[{', '.join(indices)}]"
            quality_tag = _format_quality_tag(group_to_quality.get(key))
            collection = group_to_collection.get(key, "")
            if collection:
                collection = _sanitize_sources_field(
                    collection.replace(LDR_APPENDED_SOURCES_SENTINEL, "")
                )
            collection_line = (
                f"   Collection: {collection}\n" if collection else ""
            )
            display = group_to_display[key]
            parts.append(
                f"{indices_str} {title}{quality_tag} "
                f"(source nr: {', '.join(map(str, indices))})\n"
                f"   URL: {_sanitize_sources_field(display)}\n"
                f"{collection_line}"
                f"\n"
            )
            seen.add(key)

        parts.append("\n")

    return "".join(parts)


def format_findings(
    findings_list: List[Dict],
    synthesized_content: str,
    questions_by_iteration: Dict[int, List[str]],
    uncited_mode: Optional[str] = None,
) -> str:
    """Format findings into a detailed text output.

    Args:
        findings_list: List of finding dictionaries
        synthesized_content: The synthesized content from the LLM.
        questions_by_iteration: Dictionary mapping iteration numbers to lists of questions
        uncited_mode: ``report.uncited_sources_mode`` override. ``None``
            (default) resolves from settings. Only the global source
            dumps (top list and ``## ALL SOURCES`` footer) are filtered,
            using the synthesized content as the citation prose;
            per-section ``SOURCES USED IN THIS SECTION`` lists stay
            unfiltered.

    Returns:
        str: Formatted text output
    """
    logger.info(
        f"Inside format_findings utility. Findings count: {len(findings_list)}, Questions iterations: {len(questions_by_iteration)}"
    )
    parts: list[str] = []
    mode = resolve_uncited_sources_mode(uncited_mode)
    prose = (
        synthesized_content
        if isinstance(synthesized_content, str)
        else str(synthesized_content or "")
    )

    # Extract all sources from findings
    all_links = []
    for finding in findings_list:
        search_results = finding.get("search_results", [])
        if search_results:
            try:
                links = extract_links_from_search_results(search_results)
                all_links.extend(links)
            except Exception:
                logger.exception("Error processing search results/links")

    # Start with the synthesized content (passed as synthesized_content)
    parts.append(f"{synthesized_content}\n\n")

    # Add sources section after synthesized content if sources exist.
    # Filtered by the synthesized prose; per-section lists below stay
    # unfiltered.
    parts.append(
        format_links_to_markdown(all_links, prose=prose, uncited_mode=mode)
    )

    parts.append("\n\n")  # Separator after synthesized content

    # Add Search Questions by Iteration section
    if questions_by_iteration:
        parts.append("## SEARCH QUESTIONS BY ITERATION\n")
        parts.append("\n")
        for iter_num, questions in questions_by_iteration.items():
            parts.append(f"\n #### Iteration {iter_num}:\n")
            for i, q in enumerate(questions, 1):
                parts.append(f"{i}. {q}\n")
        parts.append("\n\n\n")
    else:
        logger.warning("No questions by iteration found to format.")

    # Add Detailed Findings section
    if findings_list:
        parts.append("## DETAILED FINDINGS\n\n")
        logger.info(f"Formatting {len(findings_list)} detailed finding items.")

        for idx, finding in enumerate(findings_list):
            logger.debug(
                f"Formatting finding item {idx}. Keys: {list(finding.keys())}"
            )
            # Use .get() for safety
            phase = finding.get("phase", "Unknown Phase")
            content = finding.get("content", "No content available.")
            search_results = finding.get("search_results", [])

            # Phase header
            parts.append(f"\n### {phase}\n\n\n")

            question_displayed = False
            # If this is a follow-up phase, try to show the corresponding question
            if isinstance(phase, str) and phase.startswith("Follow-up"):
                try:
                    phase_parts = phase.replace(
                        "Follow-up Iteration ", ""
                    ).split(".")
                    if len(phase_parts) == 2:
                        iteration = int(phase_parts[0])
                        question_index = int(phase_parts[1]) - 1
                        if (
                            iteration in questions_by_iteration
                            and 0
                            <= question_index
                            < len(questions_by_iteration[iteration])
                        ):
                            parts.append(
                                f"#### {questions_by_iteration[iteration][question_index]}\n\n"
                            )
                            question_displayed = True
                        else:
                            logger.warning(
                                f"Could not find matching question for phase: {phase}"
                            )
                    else:
                        logger.warning(
                            f"Could not parse iteration/index from phase: {phase}"
                        )
                except ValueError:
                    logger.warning(
                        f"Could not parse iteration/index from phase: {phase}"
                    )
            # Handle Sub-query phases from IterDRAG strategy
            elif isinstance(phase, str) and phase.startswith("Sub-query"):
                try:
                    # Extract the index number from "Sub-query X"
                    query_index = int(phase.replace("Sub-query ", "")) - 1
                    # In IterDRAG, sub-queries are stored in iteration 0
                    if 0 in questions_by_iteration and query_index < len(
                        questions_by_iteration[0]
                    ):
                        parts.append(
                            f"#### {questions_by_iteration[0][query_index]}\n\n"
                        )
                        question_displayed = True
                    else:
                        logger.warning(
                            f"Could not find matching question for phase: {phase}"
                        )
                except ValueError:
                    logger.warning(
                        f"Could not parse question index from phase: {phase}"
                    )

            # If the question is in the finding itself, display it
            if (
                not question_displayed
                and "question" in finding
                and finding["question"]
            ):
                parts.append(f"### SEARCH QUESTION:\n{finding['question']}\n\n")

            # Content
            parts.append(f"\n\n{content}\n\n")

            # Search results if they exist
            if search_results:
                try:
                    links = extract_links_from_search_results(search_results)
                    if links:
                        parts.append("### SOURCES USED IN THIS SECTION:\n")
                        parts.append(format_links_to_markdown(links) + "\n\n")
                except Exception:
                    logger.exception(
                        f"Error processing search results/links for finding {idx}"
                    )
            else:
                logger.debug(f"No search_results found for finding item {idx}.")

            parts.append(f"{'_' * 80}\n\n")
    else:
        logger.warning("No detailed findings found to format.")

    # Add summary of all sources at the end, filtered the same way.
    if all_links:
        parts.append("## ALL SOURCES:\n")
        parts.append(
            format_links_to_markdown(all_links, prose=prose, uncited_mode=mode)
        )
    else:
        logger.info("No unique sources found across all findings to list.")

    logger.info("Finished format_findings utility.")
    return "".join(parts)