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.

36.8 KB
Raw Blame
"""
Unified Content Fetcher.

Provides a single interface to fetch content from various sources:
- Academic papers (arXiv, PubMed, Semantic Scholar)
- Web pages (HTML)
- Direct PDF links
"""

from typing import Any, Dict, List, Optional
from loguru import logger

from .url_classifier import URLClassifier, URLType
from ..research_library.downloaders.base import ContentType
from ..security.egress.fetch import policy_aware_validate_url
from ..security.ssrf_validator import redact_url_for_log
from ..utilities.resource_utils import safe_close

# Default maximum content length (500KB of text)
DEFAULT_MAX_CONTENT_LENGTH = 500_000

# URL types whose selected strategy owns the terminal outcome when its
# download fails.
_NO_HTML_FALLBACK = {
    URLType.ARXIV,
    URLType.HTML,
    URLType.DOI,
    URLType.INVALID,
    URLType.PDF,
}

# URL types that stay terminal even when the specialized downloader could not
# be imported at all. Only arXiv qualifies, and only because URLType.ARXIV is
# now reserved for URLs that name one paper (see URLClassifier.classify): for
# those the downloader owns the canonical HTML, PDF, and API sources, and the
# input URL (an ar5iv mirror included) is never fetched directly, so generic
# extraction of that URL is not a valid substitute. Other arXiv-host pages --
# listings, author pages, info./blog./static. subdomains, the legacy /ftp/
# PDF layout -- have no canonical source and classify as HTML or PDF, so they
# keep the generic path. Every other type keeps the import-failure fallback.
_NO_IMPORT_FALLBACK = {URLType.ARXIV}


class ContentFetcher:
    """
    Unified content fetcher that routes to appropriate downloaders.

    Automatically detects URL type and uses the best downloader.
    """

    def __init__(
        self,
        timeout: int = 30,
        language: str = "English",
        enable_js_rendering: bool = False,
        egress_context: Any = None,
        content_cache: Any = None,
        url_guard: Any = None,
        username: Optional[str] = None,
        password: Optional[str] = None,
        settings_snapshot: Optional[dict] = None,
    ):
        """
        Initialize the content fetcher.

        Args:
            timeout: Request timeout in seconds
            language: Language for justext stoplist (passed to HTML downloader)
            enable_js_rendering: When True, the HTML/DOI downloader falls back
                to a headless browser (Crawl4AI/Playwright) for pages that need
                JavaScript to render. Defaults to False because the default
                Docker production image ships without Chromium and the fallback
                otherwise wastes work on every fetch. In limited (mostly
                accidental) internal benchmark comparisons between dev
                instances that happened to have Chromium installed and routine
                Docker runs that did not, JS rendering did not measurably
                improve research quality, and most regular benchmark runs are
                on Docker without Chromium anyway — so disabling by default
                does not regress observed quality. The user-facing toggle is
                the ``web.enable_javascript_rendering`` setting.
            egress_context: Optional ``EgressContext`` from the active
                policy. When present and the run's scope is
                ``PRIVATE_ONLY``, SSRF validation permits private IPs so
                local lab deployments (Ollama at 127.0.0.1, SearXNG on
                192.168.x) can actually be reached without forcing the
                operator to set SSRF_ALLOW_PRIVATE_IPS=1 globally.
            content_cache: Optional shared ``ResearchContentCache`` for the
                whole research run. When None, the cache bound to the
                current research (if any) is used, so the quick-report
                phase, detailed subsections, lead agent and subagents
                share previously fetched pages instead of re-fetching.
            url_guard: Optional extra URL boundary checked before each
                static request, including redirects. Guarded documents use
                static extraction without browser or source enrichment.
                They never read or store page content in the shared cache;
                only its per-run SSRF validation memo is shared.
            username: Optional username for user context and library persistence.
            password: Optional database encryption password.
            settings_snapshot: Optional settings dictionary snapshot.
        """
        self.timeout = timeout
        self.language = language
        self.enable_js_rendering = enable_js_rendering
        self.egress_context = egress_context
        self.content_cache = content_cache
        self.url_guard = url_guard
        try:
            from ..utilities.thread_context import resolve_fetch_identity

            username, password, settings_snapshot = resolve_fetch_identity(
                egress_context=egress_context,
                settings_snapshot=settings_snapshot,
                username=username,
                password=password,
            )
        except Exception:
            username = username or getattr(egress_context, "username", None)
            password = password or getattr(egress_context, "password", None)
        self.username = username
        self.password = password
        self.settings_snapshot = settings_snapshot
        self._downloaders: Dict[URLType, Any] = {}

    def _resolve_cache(self) -> Any:
        """Return the effective shared cache (explicit or research-bound)."""
        if self.content_cache is not None:
            return self.content_cache
        try:
            from ..utilities.research_content_cache import get_current_cache
        except Exception:  # pragma: no cover - defensive
            return None
        try:
            return get_current_cache()
        except Exception:  # pragma: no cover - defensive
            return None

    def _get_downloader(self, url_type: URLType):
        """Get or create the appropriate downloader for a URL type."""
        if self.url_guard is not None:
            # A replay URL is saved evidence, even when its embedded target
            # resembles a specialized source. Avoid live metadata enrichment.
            url_type = URLType.HTML
        if url_type in self._downloaders:
            return self._downloaders[url_type]

        downloader_kwargs: Dict[str, Any] = {}
        if self.username is not None:
            downloader_kwargs["username"] = self.username
        if self.password is not None:
            downloader_kwargs["password"] = self.password
        if self.settings_snapshot is not None:
            downloader_kwargs["settings_snapshot"] = self.settings_snapshot

        downloader: Any = None

        if url_type == URLType.ARXIV:
            try:
                from ..research_library.downloaders.arxiv import ArxivDownloader

                downloader = ArxivDownloader(
                    timeout=self.timeout,
                    **downloader_kwargs,
                )
            except ImportError:
                logger.warning("content_fetcher.arxiv_downloader_unavailable")

        elif url_type in (URLType.PUBMED, URLType.PMC):
            try:
                from ..research_library.downloaders.pubmed import (
                    PubMedDownloader,
                )

                downloader = PubMedDownloader(
                    timeout=self.timeout,
                    **downloader_kwargs,
                )
            except ImportError:
                logger.warning("content_fetcher.pubmed_downloader_unavailable")

        elif url_type == URLType.SEMANTIC_SCHOLAR:
            try:
                from ..research_library.downloaders.semantic_scholar import (
                    SemanticScholarDownloader,
                )

                downloader = SemanticScholarDownloader(
                    timeout=self.timeout,
                    **downloader_kwargs,
                )
            except ImportError:
                logger.warning(
                    "content_fetcher.semantic_scholar_downloader_unavailable"
                )

        elif url_type in (URLType.BIORXIV, URLType.MEDRXIV):
            try:
                from ..research_library.downloaders.biorxiv import (
                    BioRxivDownloader,
                )

                downloader = BioRxivDownloader(
                    timeout=self.timeout,
                    **downloader_kwargs,
                )
            except ImportError:
                logger.warning("content_fetcher.biorxiv_downloader_unavailable")

        elif url_type == URLType.PDF:
            try:
                from ..research_library.downloaders.direct_pdf import (
                    DirectPDFDownloader,
                )

                downloader = DirectPDFDownloader(
                    timeout=self.timeout,
                    **downloader_kwargs,
                )
            except ImportError:
                logger.warning(
                    "content_fetcher.direct_pdf_downloader_unavailable"
                )

        elif url_type == URLType.HTML:
            try:
                if self.url_guard is not None:
                    # Archived evidence must stay within its capture. Static
                    # extraction avoids browser navigation and live resources.
                    from ..research_library.downloaders.html import (
                        HTMLDownloader as StaticHTMLDownloader,
                    )

                    downloader = StaticHTMLDownloader(
                        timeout=self.timeout,
                        language=self.language,
                        **downloader_kwargs,
                    )
                else:
                    from ..research_library.downloaders.playwright_html import (
                        AutoHTMLDownloader as HTMLDownloader,
                    )

                    downloader = HTMLDownloader(
                        timeout=self.timeout,
                        language=self.language,
                        enable_js_rendering=self.enable_js_rendering,
                        **downloader_kwargs,
                    )
            except ImportError:
                logger.warning("content_fetcher.html_downloader_unavailable")

        elif url_type == URLType.DOI:
            # DOI URLs typically redirect to publisher pages
            # Use HTML downloader as fallback
            try:
                from ..research_library.downloaders.playwright_html import (
                    AutoHTMLDownloader as HTMLDownloader,
                )

                downloader = HTMLDownloader(
                    timeout=self.timeout,
                    language=self.language,
                    enable_js_rendering=self.enable_js_rendering,
                )
            except ImportError:
                logger.warning("content_fetcher.html_downloader_unavailable")

        # Cache the downloader
        if downloader:
            self._apply_egress_policy_to_downloader(downloader)
            self._downloaders[url_type] = downloader

        return downloader

    def _apply_egress_policy_to_downloader(self, downloader: Any) -> None:
        """Carry the run policy into transport and relax private IPs only for
        PRIVATE_ONLY, mirroring ``policy_aware_validate_url``.

        Without this, a private/lab URL that ContentFetcher already approved
        (policy_aware_validate_url + evaluate_url both allow private hosts
        under PRIVATE_ONLY) is then rejected by the downloader's OWN strict
        SafeSession SSRF re-validation (allow_private_ips defaults to False),
        breaking PRIVATE_ONLY's documented "reach your local services" use
        case. ``SafeSession.request`` reads ``allow_private_ips`` per-request,
        so setting it post-construction takes effect. Cloud-metadata IPs stay
        blocked regardless (``is_ip_blocked`` always rejects them).
        """
        if self.url_guard is not None and downloader is not None:
            from ..security.safe_requests import SafeSession

            session = getattr(downloader, "session", None)
            if not isinstance(session, SafeSession):
                raise ValueError(
                    "Content downloader cannot enforce the URL boundary"
                )
            session.url_guard = self.url_guard
        if downloader is None or self.egress_context is None:
            return
        session = getattr(downloader, "session", None)
        if session is not None:
            session.egress_context = self.egress_context
        # Narrow except (not bare Exception): the only things that can throw
        # here are the policy import and the ctx.scope attribute access. The
        # failure direction is fail-SAFE — if we don't set allow_private_ips it
        # stays False (strict SSRF), i.e. over-restrictive, never a bypass — but
        # catching narrowly avoids masking an unrelated bug (e.g. a refactor
        # that breaks the import) while still never breaking a fetch on the
        # expected misconfiguration cases.
        try:
            from ..security.egress.policy import EgressScope

            scope = self.egress_context.scope
        except (ImportError, AttributeError):  # pragma: no cover - defensive
            logger.debug("content_fetcher.egress_scope_unavailable")
            return
        if scope == EgressScope.PRIVATE_ONLY:
            if session is not None and hasattr(session, "allow_private_ips"):
                session.allow_private_ips = True
            # The browser path is independently refused for restricted scopes;
            # this flag is retained for the pinned static HTTP path.
            if hasattr(downloader, "allow_private_ips"):
                downloader.allow_private_ips = True

    def fetch(
        self,
        url: str,
        max_length: Optional[int] = None,
        prefer_text: bool = True,
    ) -> Dict[str, Any]:
        """
        Fetch content from a URL.

        Automatically detects the URL type and uses the appropriate downloader.

        Args:
            url: The URL to fetch content from
            max_length: Maximum content length to return (chars). Defaults to 500KB.
            prefer_text: If True, prefer text extraction over PDF download

        Returns:
            Dict with:
                - status: "success" or "error"
                - content: Extracted text content
                - url: Original URL
                - source_type: Type of source (arxiv, pubmed, html, etc.)
                - title: Title if available
                - error: Error message if failed
        """
        # Apply default max_length if not specified
        if max_length is None:
            max_length = DEFAULT_MAX_CONTENT_LENGTH

        # Classify the URL
        url_type = URLClassifier.classify(url)
        source_name = URLClassifier.get_source_name(url_type)

        # Reject invalid/dangerous URLs
        if url_type == URLType.INVALID:
            return {
                "status": "error",
                "url": url,
                "source_type": source_name,
                "error": "Invalid or unsupported URL scheme (only http/https allowed)",
            }

        # SSRF validation: reject private/internal IPs before reaching downloaders.
        # Policy-aware so PRIVATE_ONLY egress scope can actually reach
        # private hosts (lab deployments) without disabling SSRF globally.
        # Per-run memo in the shared cache removes the per-fetch getaddrinfo
        # when the fetch-tool gate already validated the same (url, grant):
        # an uncached fetch otherwise resolves 3x (tool read gate, here,
        # tool put gate). Only True is pinned — a False (including a
        # transient DNS failure, which validate_url maps to False rather
        # than raising) is recomputed so recovery stays open.
        def _ssrf_ok() -> bool:
            _cache_for_gate = self._resolve_cache()
            _memo = getattr(_cache_for_gate, "validated", None)
            if callable(_memo):
                try:
                    from ..security.egress.policy import EgressScope
                except Exception:  # pragma: no cover - defensive
                    EgressScope = None  # type: ignore[assignment, misc]
                _allow_private = bool(
                    EgressScope is not None
                    and self.egress_context is not None
                    and getattr(self.egress_context, "scope", None)
                    == EgressScope.PRIVATE_ONLY
                )
                # Mirror the exact booleans policy_aware_validate_url
                # computes with: (True, True) under PRIVATE_ONLY, strict
                # (False, False) otherwise.
                _block_link_local = bool(_allow_private)
                try:
                    return bool(
                        _memo(
                            url,
                            _allow_private,
                            _block_link_local,
                            lambda: policy_aware_validate_url(
                                url, self.egress_context
                            ),
                        )
                    )
                except Exception:  # pragma: no cover - defensive
                    return False
            try:
                return bool(policy_aware_validate_url(url, self.egress_context))
            except Exception:  # pragma: no cover - defensive
                return False

        if not _ssrf_ok():
            logger.warning(
                "content_fetcher.ssrf_denied url={url}",
                url=redact_url_for_log(url),
            )
            return {
                "status": "error",
                "url": url,
                "source_type": source_name,
                "error": "URL failed security validation (blocked by SSRF protection)",
            }

        # Egress policy: reject URLs that are SSRF-OK but scope-incompatible.
        # SSRF only checks the IP class; scope enforcement is a separate axis
        # (PRIVATE_ONLY blocks public hosts, PUBLIC_ONLY blocks private hosts).
        # Centralized here so all callers (LangGraph fetch tool, MCP
        # download_content, future tools) get uniform enforcement instead of
        # each remembering to wrap the call site.
        if self.egress_context is not None:
            from ..security.egress.policy import evaluate_url

            url_decision = evaluate_url(url, self.egress_context)
            if not url_decision.allowed:
                logger.bind(policy_audit=True).warning(
                    "content_fetcher.egress_denied url={url} scope={scope} reason={reason}",
                    url=redact_url_for_log(url),
                    scope=self.egress_context.scope.value,
                    reason=url_decision.reason,
                )
                # Return a structured error rather than raise — callers in
                # the fetch path already handle dict-shaped errors.
                return {
                    "status": "error",
                    "url": url,
                    "source_type": source_name,
                    "error": (
                        f"URL refused by egress policy ({url_decision.reason})"
                    ),
                }

        logger.info(
            "content_fetcher.fetch_started url={url} source_type={source_type}",
            url=redact_url_for_log(url),
            source_type=source_name,
        )

        # Shared cache with request coalescing: repeat URLs (quick phase vs
        # subagents, lead vs subagents, web_search vs fetch_content) are
        # served from the single research-scoped cache without a second
        # download, and concurrent same-URL fetches collapse to one network
        # call via ``get_or_fetch``. Only successful (full pre-truncation)
        # text is cached, so transient errors still retry. The cache sits
        # AFTER the egress gate above — no policy bypass.
        # A guarded (archive) read neither reads nor stores cached page
        # content; only the SSRF gate above shares the run cache's
        # validation memo. An entry under the same replay URL can come from
        # an unguarded fetch whose redirects left the capture, and the
        # guarded caller needs this read's own transport failure, not a
        # cached or coalesced result.
        _cache = None if self.url_guard is not None else self._resolve_cache()
        content_type = ContentType.TEXT if prefer_text else ContentType.PDF

        def _download_once() -> Dict[str, Any] | None:
            """Download and decode *url* once (no cache access).

            Returns a cache-shaped success dict with the FULL pre-truncation
            text on success, or an error-shaped dict (no ``content``) on
            failure so ``get_or_fetch`` passes it through without caching
            while waiters retry with their own fetch. A guarded read
            re-raises download failures instead.
            """
            downloader = self._get_downloader(url_type)
            if not downloader and url_type not in _NO_IMPORT_FALLBACK:
                downloader = self._get_downloader(URLType.HTML)
            if not downloader:
                return {
                    "status": "error",
                    "url": url,
                    "source_type": source_name,
                    "error": "No suitable downloader available",
                }
            try:
                result = self._download_content(downloader, url, content_type)
                if (
                    not result.is_success
                    and self.url_guard is None
                    and url_type not in _NO_HTML_FALLBACK
                ):
                    logger.debug(
                        "content_fetcher.html_fallback_started url={url}",
                        url=redact_url_for_log(url),
                    )
                    html_downloader = self._get_downloader(URLType.HTML)
                    if html_downloader:
                        result = html_downloader.download_with_result(
                            url, content_type
                        )
                        downloader = html_downloader
                if result.is_success and result.content:
                    # Decode content — check PDF magic first (allowing
                    # leading bytes within the first 1024 per spec), then
                    # UTF-8. This runs only on the cache-miss path
                    # (_download_once), so cache hits never re-persist.
                    from ..research_library.services.download_service import (
                        _is_pdf_bytes,
                    )

                    metadata: Dict[str, Any] = {}
                    metadata_fetched = False
                    raw_pdf_bytes: Optional[bytes] = None
                    candidate_pdf = getattr(result, "pdf_content", None)
                    if isinstance(candidate_pdf, (bytes, bytearray)):
                        raw_pdf_bytes = bytes(candidate_pdf)
                    if raw_pdf_bytes is None:
                        candidate_dl_pdf = getattr(
                            downloader, "_last_pdf_content", None
                        )
                        if isinstance(candidate_dl_pdf, (bytes, bytearray)):
                            raw_pdf_bytes = bytes(candidate_dl_pdf)
                    if (
                        raw_pdf_bytes is None
                        and isinstance(result.content, (bytes, bytearray))
                        and _is_pdf_bytes(result.content)
                    ):
                        raw_pdf_bytes = bytes(result.content)

                    if raw_pdf_bytes is not None and _is_pdf_bytes(
                        raw_pdf_bytes
                    ):
                        from ..research_library.downloaders.base import (
                            BaseDownloader,
                        )

                        if raw_pdf_bytes is not result.content:
                            try:
                                content = result.content.decode("utf-8")
                            except UnicodeDecodeError:
                                content = BaseDownloader.extract_text_from_pdf(
                                    raw_pdf_bytes
                                )
                        else:
                            content = BaseDownloader.extract_text_from_pdf(
                                raw_pdf_bytes
                            )
                        if not content or not content.strip():
                            if (
                                raw_pdf_bytes.lstrip(
                                    b"\xef\xbb\xbf\x00 \t\r\n"
                                )[:4]
                                == b"%PDF"
                            ):
                                return {
                                    "status": "error",
                                    "url": url,
                                    "source_type": source_name,
                                    "error": "Could not extract text from PDF",
                                }
                            try:
                                content = raw_pdf_bytes.decode("utf-8")
                            except UnicodeDecodeError:
                                return {
                                    "status": "error",
                                    "url": url,
                                    "source_type": source_name,
                                    "error": "Could not extract text from PDF",
                                }
                        else:
                            # Single fetch-time writer (downloaders do not
                            # persist; library downloads persist
                            # synchronously in DownloadService). Fetch
                            # metadata once and reuse it below so HTML
                            # get_metadata does not re-fetch the page.
                            if hasattr(downloader, "get_metadata"):
                                try:
                                    fetched_meta = downloader.get_metadata(url)
                                    if isinstance(fetched_meta, dict):
                                        metadata = fetched_meta
                                    metadata_fetched = True
                                except Exception:
                                    logger.opt(exception=False).debug(
                                        "content_fetcher.metadata_failed url={url}",
                                        url=redact_url_for_log(url),
                                    )
                            # Persist when PDF extraction produced valid text
                            # and the payload matches PDF detection (aligning with
                            # _is_pdf_bytes to cover spec-legal PDFs with leading junk).
                            if _is_pdf_bytes(raw_pdf_bytes):
                                try:
                                    from ..research_library.services.download_service import (
                                        persist_downloaded_pdf_on_the_spot,
                                    )

                                    pdf_title = (
                                        metadata.get("title")
                                        if metadata
                                        else None
                                    )
                                    extra_urls = None
                                    final_url = getattr(
                                        downloader, "_last_final_url", None
                                    )
                                    if final_url and final_url != url:
                                        extra_urls = [final_url]
                                    persist_downloaded_pdf_on_the_spot(
                                        url=url,
                                        pdf_content=raw_pdf_bytes,
                                        text=content,
                                        title=pdf_title,
                                        username=self.username,
                                        password=self.password,
                                        settings_snapshot=self.settings_snapshot,
                                        extra_urls=extra_urls,
                                        run_in_background=True,
                                        check_setting=True,
                                    )
                                except Exception:
                                    logger.opt(exception=False).debug(
                                        "content_fetcher.persist_skipped url={url}",
                                        url=redact_url_for_log(url),
                                    )
                    else:
                        try:
                            content = result.content.decode("utf-8")
                        except UnicodeDecodeError:
                            return {
                                "status": "error",
                                "url": url,
                                "source_type": source_name,
                                "error": (
                                    "Content is not valid UTF-8 and not a PDF"
                                ),
                            }
                    if not metadata_fetched and hasattr(
                        downloader, "get_metadata"
                    ):
                        try:
                            metadata = downloader.get_metadata(url)
                        except Exception:
                            # Best-effort/optional metadata; expected to fail
                            # routinely so keep at DEBUG without exception
                            # attachment to keep raw URLs out of log sinks.
                            logger.opt(exception=False).debug(
                                "content_fetcher.metadata_failed url={url}",
                                url=redact_url_for_log(url),
                            )
                    return {
                        "content": content,
                        "content_is_complete": getattr(
                            result, "content_is_complete", True
                        )
                        is not False,
                        "title": metadata.get("title"),
                        "url": url,
                        "author": metadata.get("author"),
                        "published_date": metadata.get("published_date"),
                    }
                return {
                    "status": "error",
                    "url": url,
                    "source_type": source_name,
                    "error": result.skip_reason or "Download failed",
                }
            except Exception as e:
                if self.url_guard is not None:
                    # The guarded fetch tool classifies this failure and emits
                    # its safe message. Ordinary callers retain dict errors.
                    raise
                logger.opt(exception=False).error(
                    "content_fetcher.download_error url={url}",
                    url=redact_url_for_log(url),
                )
                return {
                    "status": "error",
                    "url": url,
                    "source_type": source_name,
                    "error": str(e),
                }

        if _cache is None:
            outcome = _download_once()
            if outcome is None or not isinstance(outcome.get("content"), str):
                if (
                    isinstance(outcome, dict)
                    and outcome.get("status") == "error"
                ):
                    return outcome
                return {
                    "status": "error",
                    "url": url,
                    "source_type": source_name,
                    "error": "Download failed",
                }
            full_content = outcome["content"] or ""
            display = full_content
            if max_length and len(display) > max_length:
                display = (
                    display[:max_length] + "\n\n[... content truncated ...]"
                )
            return {
                "status": "success",
                "content": display,
                "content_is_complete": outcome.get("content_is_complete", True),
                "url": url,
                "source_type": source_name,
                "title": outcome.get("title"),
                "author": outcome.get("author"),
                "published_date": outcome.get("published_date"),
            }

        try:
            cached = _cache.get_or_fetch(url, _download_once)
        except Exception as e:
            logger.opt(exception=False).error(
                "content_fetcher.download_error url={url}",
                url=redact_url_for_log(url),
            )
            return {
                "status": "error",
                "url": url,
                "source_type": source_name,
                "error": str(e),
            }
        if cached is None or not isinstance(cached.get("content"), str):
            if isinstance(cached, dict) and cached.get("status") == "error":
                return cached
            return {
                "status": "error",
                "url": url,
                "source_type": source_name,
                "error": "Download failed",
            }
        full_cached = cached.get("content") or ""
        display_cached = full_cached
        if max_length and len(display_cached) > max_length:
            display_cached = (
                display_cached[:max_length] + "\n\n[... content truncated ...]"
            )
        result_out: Dict[str, Any] = {
            "status": "success",
            "content": display_cached,
            "content_is_complete": cached.get("content_is_complete", True),
            "url": url,
            "source_type": source_name,
            "title": cached.get("title"),
        }
        # get_or_fetch returns the cached entry (content/title/url only);
        # when this caller was the fetcher, the full download also produced
        # author metadata — include it when available.
        if isinstance(cached.get("author"), str):
            result_out["author"] = cached.get("author")
        if cached.get("published_date") is not None:
            result_out["published_date"] = cached.get("published_date")
        return result_out

    def _download_content(self, downloader, url, content_type):
        if self.url_guard is None:
            return downloader.download_with_result(url, content_type)
        # Guarded archive reads need the actual terminal failure (including
        # Retry-After), rather than a sanitized skip reason. State is local
        # to the call, so concurrent fetches cannot overwrite each other.
        transport_errors: list[BaseException] = []
        result = downloader.download_with_result(
            url,
            content_type,
            on_transport_error=transport_errors.append,
        )
        if transport_errors:
            raise transport_errors[-1]
        return result

    def fetch_text(
        self, url: str, max_length: Optional[int] = None
    ) -> Optional[str]:
        """
        Convenience method to fetch just the text content.

        Args:
            url: The URL to fetch
            max_length: Maximum content length

        Returns:
            Text content or None if failed
        """
        result = self.fetch(url, max_length=max_length, prefer_text=True)
        if result.get("status") == "success":
            return result.get("content")
        return None

    def fetch_batch(self, urls: List[str]) -> Dict[str, Optional[str]]:
        """Fetch multiple URLs, routing each to the best downloader.

        Specialized downloaders are tried first; generic HTML extraction is
        used only for fallback-eligible URL types. arXiv handles its own
        canonical fallbacks. Downloaders are cached by URL type, so a single
        Playwright browser is shared across all HTML URLs.

        Returns:
            Dict mapping URL → extracted text (or None if failed).
        """
        return {url: self.fetch_text(url) for url in urls}

    def get_url_info(self, url: str) -> Dict[str, Any]:
        """
        Get information about a URL without downloading.

        Args:
            url: The URL to analyze

        Returns:
            Dict with url_type, source_name, and extracted_id
        """
        url_type = URLClassifier.classify(url)
        return {
            "url": url,
            "url_type": url_type.value,
            "source_name": URLClassifier.get_source_name(url_type),
            "extracted_id": URLClassifier.extract_id(url, url_type),
        }

    def close(self):
        """Close all cached downloaders and their HTTP sessions."""
        for url_type, downloader in self._downloaders.items():
            safe_close(downloader, f"downloader-{url_type.value}")
        self._downloaders.clear()

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()
        return False