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.

40.6 KB
Raw Blame
"""
Shared extraction pipeline.

Two entry points:
    extract_content(html) — HTML string in, clean text out.
    fetch_and_extract(url) — URL in, clean text out (static + JS fallback).

This is the single source of truth for content extraction in the project.
Used by HTMLDownloader, ContentFetcher, FullSearchResults, WaybackSearchEngine,
and any other code that needs clean text from a web page.

Academic URLs (arXiv, PubMed, bioRxiv, etc.) are automatically routed to
specialized downloaders first. Generic HTML runs only when that route permits it.
"""

from importlib import import_module
from typing import Any, Callable, Dict, List, NamedTuple, Optional

from bs4 import BeautifulSoup, NavigableString
from loguru import logger

from local_deep_research.utilities.arxiv import is_arxiv_paper_url

from .trafilatura_extractor import TrafilaturaExtractor
from .readability_extractor import ReadabilityExtractor
from .justext_extractor import JustextExtractor
from .newspaper_extractor import NewspaperExtractor
from .metadata_extractor import extract_metadata, metadata_to_text
from ....security.log_sanitizer import failure_reason_for_log
from ....security.ssrf_validator import redact_url_for_log
from ....security.egress.policy import PolicyDeniedError
from ....utilities.lxml_thread_safety import install_per_thread_html_parsers

# Route newspaper4k's and readabilipy's parser-less lxml.html.fromstring calls
# onto per-thread parsers. Without this, worker threads extracting different
# pages concurrently share one libxml2 xmlDict and corrupt it, aborting the
# whole process with "double free or corruption (out)". Installed at import so
# it is in place before any worker thread parses.
install_per_thread_html_parsers()


# Elements a browser lays out on a line of their own, plus the few that are not
# laid out at all but still read as a line when a document is flattened
# (`title`, `option`). The markup is the only place that boundary exists, so the
# flattener marks the end of each one.
_BLOCK_LEVEL_TAGS = (
    "address",
    "article",
    "aside",
    "blockquote",
    "caption",
    "dd",
    "details",
    "div",
    "dl",
    "dt",
    "fieldset",
    "figcaption",
    "figure",
    "footer",
    "h1",
    "h2",
    "h3",
    "h4",
    "h5",
    "h6",
    "header",
    "hgroup",
    "hr",
    "legend",
    "li",
    "main",
    "nav",
    "ol",
    "option",
    "p",
    "pre",
    "section",
    "summary",
    "table",
    "td",
    "th",
    "title",
    "tr",
    "ul",
)


_END_OF_CHILDREN = object()


def _flatten_html(soup: BeautifulSoup) -> str:
    """Flatten parsed HTML to text the way a reader sees it.

    ``get_text(separator="\n")`` puts a newline between *every* pair of strings,
    including the ones inside a sentence, so ``Hello <b>world</b>! See
    <a href="#">this</a>.`` arrives as six lines. A line break belongs only at
    the start and end of a block-level element and at a ``<br>``, so those are
    the only places one is written.

    The tree is walked once and left unchanged. Replacing each ``<br>`` in
    place scans its siblings on every call, which is quadratic on a page made
    of tens of thousands of them.
    """
    # The strings get_text() joins: script, style and template text stays out
    # exactly as it does there.
    wanted = {id(string) for string in soup.strings}
    parts: List[str] = []
    children = [iter(soup.contents)]
    open_tags: List[Optional[str]] = [None]
    while children:
        node = next(children[-1], _END_OF_CHILDREN)
        if node is _END_OF_CHILDREN:
            children.pop()
            if open_tags.pop() in _BLOCK_LEVEL_TAGS:
                parts.append("\n")
        elif isinstance(node, NavigableString):
            if id(node) in wanted:
                parts.append(node)
        elif node.name == "br":
            parts.append("\n")
        else:
            if node.name in _BLOCK_LEVEL_TAGS:
                parts.append("\n")
            children.append(iter(node.contents))
            open_tags.append(node.name)

    lines = [line.strip() for line in "".join(parts).splitlines()]
    return "\n".join(line for line in lines if line)


# --- Pipeline thresholds ---
# Minimum extracted text length to consider extraction successful
MIN_CONTENT_LENGTH = 50
# If content is shorter than this, enrich with structured metadata
# (JSON-LD, OpenGraph) — helps product pages, JS-heavy sites
METADATA_ENRICHMENT_THRESHOLD = 1000
# Boilerplate penalty per keyword when scoring extraction quality
BOILERPLATE_PENALTY = 500
# If an extractor discards more than this fraction of the previous
# extractor's output, skip it (protects non-English content)
SAFETY_DISCARD_RATIO = 0.2

# Module-level singleton extractors (avoid re-creating per call)
_trafilatura = TrafilaturaExtractor()
_readability = ReadabilityExtractor()
_justext_en = JustextExtractor(language="English")
_newspaper = NewspaperExtractor()

# Boilerplate keywords — used to penalize low-quality extraction
_BOILERPLATE_KEYWORDS = [
    "cookie",
    "sign up",
    "newsletter",
    "subscribe",
    "accept all",
    "privacy policy",
    "terms of service",
]


def _run_extractors_parallel(
    html: str, url: str
) -> tuple[str | None, str | None]:
    """Run trafilatura and newspaper4k sequentially.

    Both extractors call into lxml's C extension, which is not safe to
    share across threads — running them in a ThreadPoolExecutor caused
    Fatal Python error: Aborted during pool teardown on Python 3.14
    (the workers would deadlock in shutdown's join). Serializing the
    calls eliminates the crash; the perf cost is one extra extraction's
    worth of CPU per page, which is acceptable.

    Correction to that rationale, from a later SIGABRT (core dump showed
    ``double free or corruption (out)`` in ``htmlParseDocument``): sharing an
    lxml *parser* across threads is in fact supported — lxml serializes it with
    a per-parser lock. The real hazard is the libxml2 ``xmlDict`` that threads
    adopt from a shared parser and then intern into concurrently. So
    serializing here was never the actual fix, and it could not have been:
    it only covers two extractors within ONE page, while the corruption comes
    from different pages extracted in different threads. The real fix is
    per-thread parsers — see ``utilities.lxml_thread_safety``, installed at
    this module's import. This sequencing is retained because it is harmless
    and the Python 3.14 teardown deadlock above was a separate problem.

    The function name is preserved for backwards compatibility with
    any callers/tests that import it directly.
    """
    try:
        trafilatura_content = _trafilatura.extract(html)
    except Exception:
        logger.debug("Pipeline: trafilatura raised an exception")
        trafilatura_content = None

    try:
        newspaper_content = _newspaper.extract(html, url)
    except Exception:
        logger.debug("Pipeline: newspaper4k raised an exception")
        newspaper_content = None

    return trafilatura_content, newspaper_content


def _count_boilerplate(text: str) -> int:
    """Count boilerplate keyword occurrences in text."""
    if not text:
        return 0
    lower = text.lower()
    return sum(1 for kw in _BOILERPLATE_KEYWORDS if kw in lower)


def _quality_score(text: str) -> int:
    """Score extraction quality: length minus boilerplate penalty."""
    if not text:
        return 0
    return len(text) - (_count_boilerplate(text) * BOILERPLATE_PENALTY)


def extract_content(
    html: str,
    language: str = "English",
    min_length: int = MIN_CONTENT_LENGTH,
    url: str = "",
) -> Optional[str]:
    """Extract clean text content from HTML.

    Pipeline:
        1. trafilatura (primary — best benchmarks, multilingual, markdown)
        2. newspaper4k (parallel — strong on news/forum pages)
        → pick the higher-quality result from steps 1-2
        3. readability → justext (fallback if both above fail, with 80% safety)
        4. soup.get_text() (last resort)

    Args:
        html: Raw HTML string.
        language: Language for justext stoplist (fallback only).
        min_length: Minimum content length to accept.
        url: Source URL (improves newspaper4k extraction accuracy).

    Returns:
        Extracted plain text, or None if content is below min_length.
    """
    if not html or not html.strip():
        return None

    # Run trafilatura and newspaper4k, pick the better result. newspaper4k is
    # strong on news front pages and multi-answer threads where trafilatura
    # sometimes extracts less content.
    # No time bound here: the extractors run sequentially in-process (see
    # _run_extractors_parallel — the ThreadPoolExecutor was removed).
    trafilatura_content, newspaper_content = _run_extractors_parallel(html, url)

    traf_score = _quality_score(trafilatura_content)
    np_score = _quality_score(newspaper_content)

    if traf_score >= np_score and trafilatura_content:
        content = trafilatura_content
        winner = "trafilatura"
    elif newspaper_content:
        content = newspaper_content
        winner = "newspaper4k"
    else:
        content = trafilatura_content
        winner = "trafilatura"

    if content and len(content.strip()) >= min_length:
        logger.debug(
            f"Pipeline: {winner} extracted {len(content)} chars"
            + (
                f" (traf={len(trafilatura_content or '')}, "
                f"np4k={len(newspaper_content or '')})"
                if newspaper_content and trafilatura_content
                else ""
            )
        )
    else:
        # Fallback: readability → justext
        logger.debug(
            "Pipeline: primary extractors insufficient, using fallback"
        )

        soup = BeautifulSoup(html, "html.parser")
        for tag_name in [
            "script",
            "style",
            "iframe",
            "noscript",
            "svg",
            "form",
            "button",
            "input",
            "select",
            "textarea",
        ]:
            for tag in soup.find_all(tag_name):
                tag.decompose()
        cleaned_html = str(soup)

        justext_extractor = (
            _justext_en
            if language == "English"
            else JustextExtractor(language=language)
        )

        content = None
        prev_text_len = 0

        for extractor in [_readability, justext_extractor]:
            result = extractor.extract(
                cleaned_html if prev_text_len == 0 else content
            )
            if result and result.strip():
                result_len = len(result.strip())
                # Safety: skip if extractor discards >80% of content.
                # Compare text lengths (strip HTML tags for fair comparison
                # since readability returns HTML but justext returns text).
                if (
                    prev_text_len > 0
                    and result_len < prev_text_len * SAFETY_DISCARD_RATIO
                ):
                    logger.debug(
                        f"Pipeline: {extractor.__class__.__name__} discarded "
                        f">80% of content — skipping"
                    )
                    continue
                content = result
                # Store text-equivalent length for fair comparison
                if "<" in result:
                    prev_text_len = len(
                        BeautifulSoup(result, "html.parser").get_text()
                    )
                else:
                    prev_text_len = result_len
                logger.debug(
                    f"Pipeline: {extractor.__class__.__name__} "
                    f"returned {result_len} chars"
                )

        # Strip remaining HTML tags (e.g. readability-only mode)
        if content and "<" in content:
            content = _flatten_html(BeautifulSoup(content, "html.parser"))

        # Last resort
        if not content or len(content.strip()) < min_length:
            logger.debug("Pipeline: all extractors failed, using get_text()")
            content = _flatten_html(soup)

    if not content or len(content.strip()) < min_length:
        return None

    # Enrich with structured metadata when text extraction is thin
    # (e.g. product pages, JS-heavy sites)
    if len(content.strip()) < METADATA_ENRICHMENT_THRESHOLD:
        metadata = extract_metadata(html)
        supplement = metadata_to_text(metadata)
        if supplement and supplement.strip():
            logger.debug(
                f"Pipeline: enriching with {len(supplement)} chars "
                f"of structured metadata"
            )
            content = content.rstrip() + "\n\n" + supplement

    return content


def extract_content_with_metadata(
    html: str,
    language: str = "English",
    min_length: int = MIN_CONTENT_LENGTH,
) -> Optional[Dict[str, Any]]:
    """Extract clean text and page metadata from HTML in a single pass.

    Combines content extraction (trafilatura/readability/justext pipeline)
    with title and description extraction from HTML meta tags. This avoids
    the need for callers to do a separate BeautifulSoup parse for metadata.

    Args:
        html: Raw HTML string.
        language: Language for justext stoplist (fallback only).
        min_length: Minimum content length to accept.

    Returns:
        Dict with keys: content, title, description — or None if content
        is below min_length.
    """
    if not html or not html.strip():
        return None

    # Single parse for metadata (title, description, og:*)
    soup = BeautifulSoup(html, "html.parser")

    title = None
    if soup.title and soup.title.string:
        title = soup.title.string.strip()
    og_title = soup.find("meta", property="og:title")
    if og_title and og_title.get("content"):
        title = str(og_title["content"]).strip()

    description = None
    meta_desc = soup.find("meta", attrs={"name": "description"})
    if meta_desc and meta_desc.get("content"):
        description = str(meta_desc["content"]).strip()
    og_desc = soup.find("meta", property="og:description")
    if og_desc and og_desc.get("content"):
        description = str(og_desc["content"]).strip()

    # Extract content using the shared pipeline
    content = extract_content(html, language=language, min_length=min_length)
    if not content:
        return None

    return {
        "title": title,
        "description": description,
        "content": content,
    }


class _SpecializedResult(NamedTuple):
    content: str | None = None
    fallback_allowed: bool = True
    skip_reason: str | None = None


def _try_specialized_downloader(
    url: str,
    timeout: int = 30,
    allow_private_ips: bool = False,
    block_link_local: bool = False,
    egress_context: Any = None,
) -> _SpecializedResult:
    """Try a specialized downloader (arXiv, PubMed, etc.) for the URL.

    The result carries extracted text, whether generic HTML fallback is safe,
    and the specialist's failure reason for observability.
    """
    # Terminal ownership follows the paper identifier, not the host: a
    # non-paper arXiv page has no canonical downloader source, so denying it
    # the generic fallback would return nothing at all.
    arxiv_url = is_arxiv_paper_url(url)
    failure = _SpecializedResult(
        fallback_allowed=not arxiv_url,
        skip_reason="URL classifier is unavailable",
    )
    try:
        from local_deep_research.content_fetcher.url_classifier import (
            URLClassifier,
            URLType,
        )
    except ImportError:
        return failure

    url_type = URLClassifier.classify(url)
    arxiv_terminal = arxiv_url or url_type == URLType.ARXIV
    failure = _SpecializedResult(fallback_allowed=not arxiv_terminal)

    # Only academic URL types have specialized downloaders worth trying.
    # HTML, DOI, PDF, INVALID fall through to the generic pipeline.
    _SPECIALIZED_TYPES = {
        URLType.ARXIV,
        URLType.PUBMED,
        URLType.PMC,
        URLType.SEMANTIC_SCHOLAR,
        URLType.BIORXIV,
        URLType.MEDRXIV,
    }
    if url_type not in _SPECIALIZED_TYPES:
        return failure

    from ..base import BaseDownloader, ContentType

    # Map URL type to downloader class (lazy imports to avoid circular deps)
    downloader: BaseDownloader | None = None
    try:
        if url_type == URLType.ARXIV:
            package = __package__
            if package is None:
                return failure._replace(
                    skip_reason="Downloader package is unavailable"
                )
            downloaders_package = package.rpartition(".")[0]
            arxiv_module = import_module(f"{downloaders_package}.arxiv")
            downloader_type = getattr(arxiv_module, "ArxivDownloader", None)
            if not isinstance(downloader_type, type) or not issubclass(
                downloader_type, BaseDownloader
            ):
                return failure._replace(
                    skip_reason="arXiv downloader is unavailable"
                )
            downloader = downloader_type(timeout=timeout)
        elif url_type in (URLType.PUBMED, URLType.PMC):
            from ..pubmed import PubMedDownloader

            downloader = PubMedDownloader(timeout=timeout)
        elif url_type == URLType.SEMANTIC_SCHOLAR:
            from ..semantic_scholar import SemanticScholarDownloader

            downloader = SemanticScholarDownloader(timeout=timeout)
        elif url_type in (URLType.BIORXIV, URLType.MEDRXIV):
            from ..biorxiv import BioRxivDownloader

            downloader = BioRxivDownloader(timeout=timeout)
    except ImportError:
        logger.debug(
            f"Pipeline: specialized downloader not available for {url_type.value}"
        )
        return failure._replace(
            skip_reason=f"{url_type.value} downloader is unavailable"
        )

    if not downloader:
        return failure._replace(
            skip_reason=f"{url_type.value} downloader is unavailable"
        )

    skip_reason: str | None = None
    try:
        session = getattr(downloader, "session", None)
        if session is not None:
            session.allow_private_ips = allow_private_ips
            session.block_link_local = block_link_local
            session.egress_context = egress_context
        result = downloader.download_with_result(url, ContentType.TEXT)
        if getattr(result, "content_is_complete", True) is False:
            # This pipeline supplies full-content engines. An abstract
            # must not be promoted to a complete paper by those callers.
            return failure._replace(
                skip_reason="Specialized downloader returned only metadata"
            )
        if result.is_success and result.content:
            text = result.content.decode("utf-8", errors="replace")
            if len(text.strip()) >= MIN_CONTENT_LENGTH:
                logger.debug(
                    f"Pipeline: specialized downloader ({url_type.value}) "
                    f"returned {len(text)} chars for "
                    f"{redact_url_for_log(url)}"
                )
                return _SpecializedResult(
                    content=text,
                    fallback_allowed=not arxiv_terminal,
                )
        skip_reason = (
            result.skip_reason
            or "Specialized downloader returned no usable content"
        )
    except PolicyDeniedError:
        # Propagate egress policy denials so callers enforce per-URL policy.
        raise
    except Exception as e:
        skip_reason = "Specialized downloader failed"
        logger.opt(exception=False).debug(
            f"Pipeline: specialized downloader failed for "
            f"{redact_url_for_log(url)} ({type(e).__name__})"
        )
    finally:
        try:
            downloader.close()
        except Exception:  # noqa: silent-exception
            pass

    failure = failure._replace(skip_reason=skip_reason)
    if failure.fallback_allowed:
        logger.debug(
            f"Pipeline: specialized downloader ({url_type.value}) returned "
            f"no content for {redact_url_for_log(url)} "
            f"({failure_reason_for_log(failure.skip_reason)}), falling back to HTML pipeline"
        )
    return failure


def _is_cache_hit_allowed(
    url: str,
    allow_private_ips: bool,
    block_link_local: bool,
    _cache: object | None = None,
) -> bool:
    """Whether a cache hit for *url* may be served under the caller's grant.

    Defense-in-depth for the shared research cache: a granted call
    (``allow_private_ips=True``) may fetch and cache a loopback/private URL,
    but a later non-granted call sharing the same cache must not be served
    that entry — it must go through the downloader's own SSRF gate (which
    will refuse). Path-only library routes (``/library/...``, ``/lib/...``)
    are local DB reads, not network fetches, so they are always allowed.

    The absolute ``https://library.document/<id>`` alias is NOT exempted:
    the pipeline has no library resolver, so that spelling is a network URL
    here and must pass the same egress check as any web URL (it fails
    ``validate_url`` — unresolvable host — and correctly bypasses the cache
    instead of leaking a cached entry to a non-granted caller).

    The library exemption uses the fetch tool's strict grammar
    (32/64-hex or UUID via ``library_resolver.parse_library_url``), not
    the wider citation grammar (``is_safe_document_id`` accepts any
    ``[0-9A-Za-z_-]+`` slug): a slug-shaped route can never resolve via
    the fetch tool, so exempting it here would let the two surfaces drift.

    Fail-closed: when the SSRF validator cannot be imported the hit is
    refused (callers fall through to the downloader, which enforces its
    own gate). When *_cache* is a ``ResearchContentCache`` successful
    verdicts are memoized per-run so repeat hits do not pay a getaddrinfo
    per read and stay servable when DNS drops mid-run after a success;
    refusals are recomputed so a transient DNS blip never pins the URL
    for the rest of the run.
    """
    try:
        from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
            parse_library_url,
        )

        stripped = url.strip() if isinstance(url, str) else ""
        if stripped.startswith("/") and parse_library_url(stripped) is not None:
            return True
    except Exception:  # pragma: no cover - defensive
        logger.debug("cache grant check: library route parse failed")
    try:
        from local_deep_research.security.ssrf_validator import validate_url
    except Exception:  # pragma: no cover - defensive
        logger.debug("cache grant check: validator unavailable, refusing hit")
        return False
    # Per-run memo: same (url, grant) repeats skip DNS on success. A
    # False verdict only skips the cache fast path and is never pinned —
    # the downloader re-validates — so a transient DNS failure cannot
    # block recovery.
    _memo = getattr(_cache, "validated", None)
    if callable(_memo):
        try:
            return bool(
                _memo(
                    url,
                    allow_private_ips,
                    block_link_local,
                    lambda: validate_url(
                        url,
                        allow_private_ips=allow_private_ips,
                        block_link_local=block_link_local,
                    ),
                )
            )
        except Exception:  # pragma: no cover - defensive
            return False
    try:
        return bool(
            validate_url(
                url,
                allow_private_ips=allow_private_ips,
                block_link_local=block_link_local,
            )
        )
    except Exception:  # pragma: no cover - defensive
        return False


def fetch_and_extract(
    url: str,
    timeout: int = 30,
    language: str = "English",
    enable_js_rendering: bool = False,
    content_cache: Any = None,
    allow_private_ips: bool = False,
    block_link_local: bool = False,
    egress_context: Any = None,
) -> Optional[str]:
    """Fetch a URL and extract clean text content.

    Pipeline:
        1. Shared research content cache (when bound — the quick-report
           phase, detailed subsections and parallel subagents share one
           cache for the whole research run, so repeat URLs are served
           without a second network fetch).
        2. Specialized downloader (arXiv canonical sources, PubMed API, etc.)
        3. Static HTTP fetch → Playwright fallback (if JS needed and
           permitted and ``enable_js_rendering`` is True) → trafilatura →
           readability → justext

    Args:
        url: The URL to fetch.
        timeout: Request timeout in seconds.
        language: Language for justext stoplist.
        enable_js_rendering: When True, the HTML pipeline falls back to a
            headless browser for pages that need JavaScript. Defaults to
            False because the default Docker production image ships without
            Chromium. Limited internal benchmark comparisons (dev instances
            with Chromium vs Docker without) showed no measurable
            research-quality improvement from JS rendering, and most regular
            benchmark runs are on Docker without Chromium anyway. The
            user-facing toggle is ``web.enable_javascript_rendering``.
        content_cache: Optional shared ``ResearchContentCache``. When None,
            the cache bound to the current research (if any) is used.
        allow_private_ips: Whether the downloader may access private IPs
            after SSRF validation. The run policy, when supplied, is also
            checked on each redirect and pinned connect address. Cloud
            metadata remains blocked regardless.
        block_link_local: Keep the whole link-local range blocked even when
            ``allow_private_ips`` is True (forwarded to the downloader's
            static-fetch ``SafeSession``). No effect without the grant.
        egress_context: Optional run policy enforced on every transport hop.

    Returns:
        Extracted plain text, or None if fetch or extraction failed.
    """
    # Shared cache with coalescing: repeat URLs (quick phase vs
    # subagents, lead vs subagents) are served without a second network
    # fetch, and concurrent same-URL calls collapse to one download via
    # ``get_or_fetch``. Only successes are cached; failures retry.
    try:
        from local_deep_research.utilities.research_content_cache import (
            resolve_cache,
        )

        _cache = resolve_cache(content_cache)
    except Exception:  # pragma: no cover - defensive
        _cache = None

    def _do_fetch() -> dict[str, Any] | None:
        # No cache access in here — get_or_fetch owns coalescing.
        try:
            specialized = _try_specialized_downloader(
                url,
                timeout=timeout,
                allow_private_ips=allow_private_ips,
                block_link_local=block_link_local,
                egress_context=egress_context,
            )
        except PolicyDeniedError:
            # Single-URL fetch: propagate policy denial immediately rather than falling back.
            raise
        except Exception as e:
            logger.opt(exception=False).debug(
                f"Pipeline: specialized downloader error for "
                f"{redact_url_for_log(url)} ({type(e).__name__})"
            )
            specialized = _SpecializedResult(
                fallback_allowed=not is_arxiv_paper_url(url)
            )
        if specialized.content:
            return {"content": specialized.content, "url": url}
        if not specialized.fallback_allowed:
            logger.warning(
                "Pipeline: terminal specialized download failed for {}: {}",
                redact_url_for_log(url),
                failure_reason_for_log(
                    specialized.skip_reason or "reason unavailable"
                ),
            )
            return None
        from ..playwright_html import AutoHTMLDownloader

        downloader = AutoHTMLDownloader(
            timeout=timeout,
            language=language,
            enable_js_rendering=enable_js_rendering,
            allow_private_ips=allow_private_ips,
            block_link_local=block_link_local,
        )
        downloader.session.egress_context = egress_context
        try:
            # download() returns extracted text as UTF-8 bytes (not raw
            # HTML): AutoHTMLDownloader inherits HTMLDownloader.download()
            # which runs _fetch_html() → _extract_content().
            result = downloader.download(url)
            if result:
                return {
                    "content": result.decode("utf-8", errors="replace"),
                    "url": url,
                }
            return None
        except PolicyDeniedError:
            # Single-URL fetch: propagate policy denial directly to caller.
            raise
        except Exception as e:
            # renders them with any traceback even when diagnose=False.
            logger.opt(exception=False).error(
                f"fetch_and_extract failed for {redact_url_for_log(url)} "
                f"({type(e).__name__})"
            )
            return None
        finally:
            try:
                downloader.close()
            except Exception:
                logger.debug("Failed to close downloader in fetch_and_extract")

    if _cache is None:
        outcome = _do_fetch()
        if outcome is not None and isinstance(outcome.get("content"), str):
            return outcome["content"] or None
        return None
    if not _is_cache_hit_allowed(
        url, allow_private_ips, block_link_local, _cache
    ):
        # Grant-agnostic cache must not serve private content to a
        # non-granted caller: bypass the cache so the downloader's own
        # SSRF gate refuses (no hit, no fill).
        outcome = _do_fetch()
        if outcome is not None and isinstance(outcome.get("content"), str):
            return outcome["content"] or None
        return None
    try:
        cached = _cache.get_or_fetch(url, _do_fetch)
    except PolicyDeniedError:
        # Single-URL fetch: propagate policy denial directly to caller.
        raise
    except Exception as e:
        # Type name only -- exception texts embed the raw URL and loguru
        # renders them with any traceback even when diagnose=False.
        logger.opt(exception=False).error(
            f"fetch_and_extract failed for {redact_url_for_log(url)} "
            f"({type(e).__name__})"
        )
        return None
    if cached is not None and isinstance(cached.get("content"), str):
        return cached["content"] or None
    return None


def batch_fetch_and_extract(
    urls: List[str],
    timeout: int = 30,
    language: str = "English",
    enable_js_rendering: bool = False,
    content_cache: Any = None,
    allow_private_ips: bool = False,
    block_link_local: bool = False,
    progress_callback: Optional[Callable[[int, int], None]] = None,
    egress_context: Any = None,
) -> Dict[str, Optional[str]]:
    """Fetch multiple URLs and extract clean text from each.

    For each URL:
        1. Shared research content cache (single cache for the whole
           research run — quick phase, subsections and subagents).
        2. Try specialized downloader (arXiv, PubMed, etc.) if URL matches
        3. Use generic HTML only when the specialized route permits fallback

    Uses a single AutoHTMLDownloader (and thus a single Playwright
    browser if JS fallback is triggered) for the generic HTML URLs.

    Args:
        urls: List of URLs to fetch.
        timeout: Request timeout in seconds per URL.
        language: Language for justext stoplist.
        enable_js_rendering: When True, the HTML pipeline falls back to a
            headless browser for pages that need JavaScript. Defaults to
            False because the default Docker production image ships without
            Chromium. Limited internal benchmark comparisons (dev instances
            with Chromium vs Docker without) showed no measurable
            research-quality improvement from JS rendering, and most regular
            benchmark runs are on Docker without Chromium anyway. The
            user-facing toggle is ``web.enable_javascript_rendering``.
        content_cache: Optional shared ``ResearchContentCache``. When None,
            the cache bound to the current research (if any) is used.
        allow_private_ips: Whether the downloader may access private IPs
            after SSRF validation. The run policy, when supplied, is also
            checked on each redirect and pinned connect address. Cloud
            metadata remains blocked regardless.
        block_link_local: Keep the whole link-local range blocked even when
            ``allow_private_ips`` is True (forwarded to the downloader's
            static-fetch ``SafeSession``). No effect without the grant.
        egress_context: Optional run policy enforced on every transport hop.
        progress_callback: Optional ``(done, total)`` hook invoked once
            up front with ``(0, len(urls))`` and after each URL
            completes. Lets a long serial fetch report liveness (and
            notice cancellation) instead of going silent for minutes.
            A regular ``Exception`` raised by the hook is swallowed
            (debug-logged) so a flaky UI callback can neither corrupt an
            already-fetched result nor abort the remaining batch;
            ``BaseException`` — including ``ResearchTerminatedException``,
            which derives ``BaseException`` and therefore passes through
            the per-URL ``except Exception`` handlers untouched — still
            propagates so cancellation takes effect promptly.

    Returns:
        Dict mapping URL → extracted text (or None if failed).
    """
    try:
        from local_deep_research.utilities.research_content_cache import (
            resolve_cache,
        )

        _cache = resolve_cache(content_cache)
    except Exception:  # pragma: no cover - defensive
        _cache = None
    from ..playwright_html import AutoHTMLDownloader

    results: Dict[str, Optional[str]] = {}
    total = len(urls)
    done = 0

    def _report_progress() -> None:
        if progress_callback is not None and total:
            try:
                progress_callback(done, total)
            except Exception:
                # A flaky UI callback must not corrupt already-fetched
                # results (specialized loop: the ``except Exception``
                # below would otherwise mistake it for a downloader
                # failure and overwrite good content with the HTML
                # fallback) nor abort the remaining batch (HTML loop).
                # ``except Exception`` deliberately lets ``BaseException``
                # — including ``ResearchTerminatedException`` — propagate
                # for prompt cancellation.
                logger.debug(
                    "batch_fetch_and_extract progress_callback failed; "
                    "continuing batch"
                )

    _report_progress()

    # Shared-cache fast path first: repeat URLs (quick phase vs subagents,
    # lead vs subagents) are served without any network fetch. The fast
    # path is grant-aware: a private/loopback URL cached by a granted call
    # must not be served to a non-granted caller sharing the same cache.
    pending_urls: List[str] = []
    ungranted_urls: List[str] = []
    if _cache is not None:
        for url in urls:
            if not _is_cache_hit_allowed(
                url, allow_private_ips, block_link_local, _cache
            ):
                ungranted_urls.append(url)
                continue
            try:
                _hit = _cache.get(url)
            except Exception:  # pragma: no cover - defensive
                _hit = None
            if _hit is not None and isinstance(_hit.get("content"), str):
                results[url] = _hit["content"] or None
                done += 1
                _report_progress()
            else:
                pending_urls.append(url)
    else:
        pending_urls = list(urls)

    # Shared holder for the single generic-HTML downloader (one Playwright
    # browser when JS fallback is enabled). Lazily created on first HTML
    # fallback so specialized-only batches never pay for it.
    _holder: Dict[str, Any] = {"downloader": None}

    def _get_shared_downloader() -> Any:
        if _holder["downloader"] is None:
            dl = AutoHTMLDownloader(
                timeout=timeout,
                language=language,
                enable_js_rendering=enable_js_rendering,
                allow_private_ips=allow_private_ips,
                block_link_local=block_link_local,
            )
            dl.session.egress_context = egress_context
            _holder["downloader"] = dl
        return _holder["downloader"]

    def _download_one(target: str) -> dict[str, Any] | None:
        # No cache access — get_or_fetch owns coalescing.
        try:
            specialized = _try_specialized_downloader(
                target,
                timeout=timeout,
                allow_private_ips=allow_private_ips,
                block_link_local=block_link_local,
                egress_context=egress_context,
            )
        except PolicyDeniedError:
            # Batch fetch: record per-URL denial as None and continue processing remaining URLs.
            return None
        except Exception as e:
            logger.opt(exception=False).debug(
                f"Pipeline: specialized downloader error for "
                f"{redact_url_for_log(target)} ({type(e).__name__})"
            )
            if is_arxiv_paper_url(target):
                return None
            specialized = _SpecializedResult(fallback_allowed=True)
        if specialized.content:
            return {"content": specialized.content, "url": target}
        if not specialized.fallback_allowed:
            logger.warning(
                "Pipeline: terminal specialized download failed for {}: {}",
                redact_url_for_log(target),
                failure_reason_for_log(
                    specialized.skip_reason or "reason unavailable"
                ),
            )
            return None
        try:
            data = _get_shared_downloader().download(target)
            if data:
                return {
                    "content": data.decode("utf-8", errors="replace"),
                    "url": target,
                }
            return None
        except PolicyDeniedError:
            # Batch fetch: record per-URL denial as None and continue processing remaining URLs.
            return None
        except Exception as e:
            # Type name only -- exception texts embed the raw URL.
            logger.opt(exception=False).error(
                f"batch_fetch_and_extract failed for "
                f"{redact_url_for_log(target)} ({type(e).__name__})"
            )
            return None

    try:
        # Ungranted destinations bypass the cache entirely so a previous
        # granted fetch cannot leak private content into this call. The
        # downloader's own SSRF gate still refuses them (no fill).
        for url in ungranted_urls:
            outcome = _download_one(url)
            if outcome is not None and isinstance(outcome.get("content"), str):
                results[url] = outcome["content"] or None
            else:
                results[url] = None
            done += 1
            _report_progress()
        for url in pending_urls:
            if _cache is not None:
                try:
                    cached = _cache.get_or_fetch(
                        url, lambda u=url: _download_one(u)
                    )
                except Exception as e:
                    # Type name only -- exception texts embed the raw URL.
                    logger.opt(exception=False).error(
                        f"batch_fetch_and_extract failed for "
                        f"{redact_url_for_log(url)} ({type(e).__name__})"
                    )
                    results[url] = None
                    done += 1
                    _report_progress()
                    continue
                if cached is not None and isinstance(
                    cached.get("content"), str
                ):
                    results[url] = cached["content"] or None
                else:
                    results[url] = None
            else:
                outcome = _download_one(url)
                if outcome is not None and isinstance(
                    outcome.get("content"), str
                ):
                    results[url] = outcome["content"] or None
                else:
                    results[url] = None
            done += 1
            _report_progress()
    finally:
        if _holder["downloader"] is not None:
            try:
                _holder["downloader"].close()
            except Exception:
                logger.debug(
                    "Failed to close downloader in batch_fetch_and_extract"
                )

    return results