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.

94.9 KB
Raw Blame
"""
Library Management Service

Handles querying and managing the downloaded document library:
- Search and filter documents
- Get statistics and analytics
- Manage collections and favorites
- Handle file operations
"""

from pathlib import Path
from typing import Dict, List, Optional
from urllib.parse import urlparse

from loguru import logger
from sqlalchemy import and_, or_, func, case, select
from sqlalchemy.orm import aliased, defer, object_session

from ...constants import FILE_PATH_SENTINELS, FILE_PATH_TEXT_ONLY
from ...utilities.arxiv import is_arxiv_paper_url
from ...database.models.download_tracker import DownloadTracker
from ...database.models.library import (
    Collection,
    Document,
    DocumentBlob,
    DocumentCollection,
    DocumentStatus,
)
from ...database.models.metrics import ResearchRating
from ...database.models.research import ResearchHistory, ResearchResource
from ...database.session_context import get_user_db_session
from ...config.paths import get_library_directory
from ...utilities.db_utils import get_settings_manager
from ...utilities.formatting import short_id
from ...utilities.resource_utils import safe_close
from ...security import url_authority_without_userinfo
from ...utilities.sql_utils import escape_like as _escape_like
from ..utils import (
    document_awaits_pdf_clause,
    document_counts_as_downloaded,
    find_tracker_by_url,
    find_tracker_for_document,
    get_absolute_path_from_settings,
    open_file_location,
    resource_document_clause,
)

# Filter dropdowns (library page + ``/api/research-list``) load research
# sessions into a client-side ``<select>``. Bound the query so a very large
# history cannot pull every row into memory / the DOM on each page load
# (#4560). The query already projects to three small columns, so this is a
# safety cap rather than a crash fix; realistic histories are far smaller, and
# comprehensive large-scale filtering is a server-side-search follow-up.
_DROPDOWN_RESEARCH_LIMIT = 1000

# ``get_unique_domains`` scans one URL row per downloaded document to build the
# domain filter. Stream it in batches (``yield_per``) rather than materializing
# every row at once, so a very large library cannot exhaust memory (#4560). The
# distinct-domain set it accumulates is small regardless of library size.
_DOMAIN_SCAN_BATCH_SIZE = 1000

# Python str.strip() whitespace, including Unicode separators and the four
# ASCII information separators. SQLite's default trim() removes only spaces.
_STRIP_WHITESPACE = (
    "\t\n\v\f\r\x1c\x1d\x1e\x1f \x85\u00a0\u1680"
    "\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007"
    "\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000"
)


def _names_a_file(file_path: Optional[str]) -> bool:
    """Whether a ``file_path`` value names a file on disk.

    ``None``, ``"database"`` and the sentinels (``blob_deleted``,
    ``text_only_not_stored``, ``metadata_only``) name no file.
    """
    return (
        bool(file_path)
        and file_path != "database"
        and file_path not in FILE_PATH_SENTINELS
    )


def _nonempty_file_size(path: Path) -> int:
    """Size of ``path`` in bytes, or 0 when the file vanishes mid-race.

    The sync checks ``is_file()`` and ``stat()`` in separate calls, so a
    file deleted in between must not abort the whole run. That race
    surfaces as ``FileNotFoundError`` (the file is gone) or
    ``NotADirectoryError`` (its parent is), and both read as 0 — exactly
    the pre-race outcome for a file that is already gone. Other
    ``OSError`` s are NOT converted: ``is_file()`` raises for EACCES,
    EIO and ESTALE on Python 3.12/3.13, and an unreadable-but-present
    file is not a missing file — the sync should still abort and roll
    back rather than treat existing PDFs as vanished.
    """
    try:
        return path.stat().st_size if path.is_file() else 0
    except (FileNotFoundError, NotADirectoryError):
        return 0


def _tracker_describes_storage(
    tracker_path: Optional[str], doc: Document
) -> bool:
    """Whether an identifying tracker's path names where ``doc`` stores its PDF.

    The sync asks this only for a tracker whose content hash or explicit
    document link identifies ``doc`` (``find_tracker_for_document``):
    URL-equivalent addresses can hold different PDF versions, so a URL
    match alone does not say whose file a tracker names. A download that
    stores a PDF writes to the tracker where it stored it:

    * filesystem storage: the file's path, which
      ``PDFStorageManager.save_pdf`` also writes to the document, so the
      two are equal (rows written before database storage was added,
      8d7bb38d1a, copied the tracker's path into the document);
    * database storage: ``"database"``, while ``save_pdf`` labels the
      document ``storage_mode`` "database" and leaves its ``file_path``
      None, as it has since database storage was added (8d7bb38d1a).
      ``upgrade_to_pdf`` labels a document it stores a PDF in the same
      way, and the content-hash dedup of ``_download_pdf`` writes
      ``"database"`` for a matched document with that label too.

    So the tracker describes a document when it names the document's own
    path. An identifying tracker can still carry a stale path, and the
    document's own path wins over it. A document without a path of its
    own, a database-stored one or a legacy row that kept its path only in
    the tracker, takes the path of the tracker that identifies it. Neither
    ``"database"`` nor the document's label says that a blob was stored (a
    failed-download placeholder keeps the column's ``"database"``
    default), so the sync still checks the document's own blob.
    """
    if not doc.file_path:
        return True
    return tracker_path == doc.file_path


def _missing_file_entry(
    doc: Document, stored_path: Optional[str], resolved_path
) -> Dict:
    """The sync's ``missing_files`` entry for a document whose file is gone.

    ``POST /library/api/sync-library`` returns these entries to the client.
    ``path`` is the stored path, which is relative to the user's library
    root (the library listing returns the same value as ``file_path``), or
    ``"invalid"`` when it does not resolve inside that root
    (``resolved_path`` is ``None``). For a document whose PDF its
    identifying tracker records as stored in the database, or that is
    itself labelled as stored there, the stored value is ``"database"``: a
    fixed label that names no file, returned whatever ``resolved_path`` is.
    It is never ``resolved_path`` itself: the absolute path is the server's
    directory layout, which ``/library/api/check-downloads`` and the
    library listing withhold from the client (#6462).
    """
    return {
        "id": doc.id,
        "title": doc.title,
        "path": stored_path
        if stored_path == "database" or (resolved_path and stored_path)
        else "invalid",
        "url": doc.original_url,
    }


def _counts_as_downloaded():
    """SQL condition: a ``Document`` the Download Manager counts as downloaded.

    A completed document, unless it awaits its PDF (see
    ``document_awaits_pdf``): "Sync library" kept its text after its PDF
    file went missing and reported the file missing, and "Download All New"
    queues it, so the Download Manager counts it as available to download.
    Without a matching document (the outer join's NULL row) the condition is
    not true, so nothing is counted. ``document_counts_as_downloaded``
    (``research_library.utils``) is the same rule for a loaded
    ``Document``.
    """
    return and_(
        Document.status == DocumentStatus.COMPLETED,
        ~document_awaits_pdf_clause(),
    )


def _sql_falsy(column):
    """SQL for Python's ``not value`` on a nullable text column: NULL or ''."""
    return or_(column.is_(None), column == "")


def _fallback_resource_id(document_model):
    """Select one stable resource when no primary resource exists."""
    return (
        select(func.min(ResearchResource.id))
        .where(ResearchResource.document_id == document_model.id)
        .correlate(document_model)
        .scalar_subquery()
    )


def _display_resource_id(document_model):
    """Match the listing's primary-FK preference and stable fallback."""
    primary_id = (
        select(ResearchResource.id)
        .where(ResearchResource.id == document_model.resource_id)
        .correlate(document_model)
        .scalar_subquery()
    )
    return func.coalesce(primary_id, _fallback_resource_id(document_model))


class LibraryService:
    """Service for managing and querying the document library."""

    def __init__(self, username: str):
        """Initialize library service for a user."""
        self.username = username

    def _has_blob_in_db(self, session, document_id: str) -> bool:
        """Check if a PDF blob exists in the database for a document."""
        return (
            session.query(DocumentBlob.document_id)
            .filter_by(document_id=document_id)
            .first()
            is not None
        )

    def _sync_pdf_blob_size(self, session, document_id: str) -> Optional[int]:
        """Read size only: None is absent, zero is a confirmed empty PDF.

        Keep _has_blob_in_db's existence contract unchanged for other callers.
        """
        return (
            session.query(func.length(DocumentBlob.pdf_binary))
            .filter_by(document_id=document_id)
            .scalar()
        )

    def _remaining_content(
        self, session, doc: Document, settings_manager
    ) -> Optional[str]:
        """What a download stores itself, whatever its URL's tracker names.

        Returns ``"pdf"`` when nonempty PDF bytes are still stored (a blob
        in the database, or a file at the document's own ``file_path``),
        ``"text"`` when only its extracted text is left, and ``None`` when
        nothing is.

        The document's ``storage_mode`` and its tracker's ``file_path`` can
        both be stale: a re-download in "none" mode clears the tracker path
        but keeps the stored PDF, and a failed-download placeholder keeps
        the ``"database"`` column default after text is saved into it. The
        tracker, which every document with the URL shares, can also name
        another document's file. So this checks what is actually stored.
        Deleting a document that holds any of it would also delete that
        content, its chunks and its vectors.
        """
        if (self._sync_pdf_blob_size(session, doc.id) or 0) > 0:
            return "pdf"
        if _names_a_file(doc.file_path):
            resolved = get_absolute_path_from_settings(
                doc.file_path, self.username, settings_manager=settings_manager
            )
            if resolved and _nonempty_file_size(resolved):
                return "pdf"
        has_text = (
            session.query(Document.id)
            .filter(
                Document.id == doc.id,
                Document.text_content.isnot(None),
                func.trim(Document.text_content, _STRIP_WHITESPACE) != "",
            )
            .first()
            is not None
        )
        return "text" if has_text else None

    def _keep_text_report_missing_pdf(
        self,
        doc: Document,
        tracker,
        stored_path: Optional[str],
        resolved_path,
        stats: Dict,
    ) -> None:
        """Keep a document's text, drop its dangling PDF and report it.

        For a research download whose PDF file is gone but whose extracted
        text is not, and that still has its research resource
        (``resource_id``), from which the download manager can download
        the PDF again. The document keeps its row, text, collection links,
        chunks and vectors. It is labelled a PDF and stored the way
        text-only documents are (``file_type`` "pdf", ``storage_mode``
        "none", ``file_path`` ``FILE_PATH_TEXT_ONLY``), so the library no
        longer offers a PDF that is not there, and ``tracker`` is reset
        to not downloaded unless it is None (see ``_clear_missing_pdf``).
        It is counted in ``files_missing`` and listed in ``missing_files``
        under ``stored_path``, the path that named the missing file: the
        document's own path or, for a document without one, the path of the
        tracker that identifies it (see ``_missing_file_entry``), and
        ``document_awaits_pdf`` holds for it, so the download manager
        downloads its PDF into it again.

        The label is set here because what went missing was a PDF: the
        sync calls this only for a missing file that the document's own
        ``file_path`` names, or that the identifying tracker of a document
        without a path of its own names, and the file paths of documents and
        trackers come only from PDF storage. A
        failed-download placeholder that a PDF download completed before
        ``_download_pdf`` labelled such documents "pdf" is still labelled
        "unknown", and would otherwise never be downloaded again.
        """
        doc.file_type = "pdf"
        doc.mime_type = "application/pdf"
        self._clear_missing_pdf(doc, tracker, stats)
        stats["files_missing"] += 1
        stats["missing_files"].append(
            _missing_file_entry(doc, stored_path, resolved_path)
        )

    def _clear_missing_pdf(self, doc: Document, tracker, stats: Dict) -> None:
        """Make a document whose PDF file is gone a text-only document.

        It is stored the way text-only documents are (``storage_mode``
        "none", ``file_path`` ``FILE_PATH_TEXT_ONLY``), and ``tracker``,
        unless it is None, is reset to not downloaded, so neither names the
        missing file any more. The sync passes the tracker whose content
        hash or explicit document link identifies this document (see
        ``find_tracker_for_document``) when it names this document's path,
        supplies the path of a document without one of its own, or has no
        path. It passes None when no tracker identifies the document, or
        when the identifying tracker names another path than the
        document's own (see ``_tracker_describes_storage``); that tracker is
        left as it is. The reset counts in ``trackers_updated`` only when it
        changes the tracker, because a tracker without a path can be reset
        already. A tracker identifies at most one document, so the sync
        changes it at most once.

        The sync calls this directly, without reporting the document, for
        a download that keeps its extracted text but no longer has its
        research resource (``resource_id`` is NULL, e.g. after
        ``delete_resource``). No download path asks for such a document's
        PDF (``document_awaits_pdf`` needs the resource), and it counts in
        ``files_found``, as every text-only document does. A later download
        of the same PDF bytes for another resource still reaches it through
        the content-hash match in ``DownloadService._download_pdf``. In a
        storage mode that keeps PDFs, that match stores the PDF into a
        matched document stored without one (``storage_mode`` "none").
        Left naming the missing file, the document made that match copy the
        dangling path into the new resource's tracker and store nothing.
        """
        doc.storage_mode = "none"
        doc.file_path = FILE_PATH_TEXT_ONLY
        session = object_session(doc)
        if session is not None:
            # Only remove the blob row while it is still empty. A concurrent
            # download may write real bytes into it between the earlier size
            # read and this delete; the empty-blob predicate keeps those bytes.
            session.query(DocumentBlob).filter(
                DocumentBlob.document_id == doc.id,
                func.coalesce(func.length(DocumentBlob.pdf_binary), 0) == 0,
            ).delete(synchronize_session=False)
        if tracker is not None and (
            tracker.is_downloaded or tracker.file_path is not None
        ):
            tracker.is_downloaded = False
            tracker.file_path = None
            stats["trackers_updated"] += 1

    def _get_safe_absolute_path(self, file_path: str) -> Optional[str]:
        """
        Get the absolute path for a file, safely handling invalid paths.

        Args:
            file_path: Relative file path from library root

        Returns:
            Absolute path as string, or None if path is invalid/unsafe
        """
        if not file_path or file_path in FILE_PATH_SENTINELS:
            return None
        abs_path = get_absolute_path_from_settings(file_path, self.username)
        return str(abs_path) if abs_path else None

    def _is_arxiv_url(self, url: str) -> bool:
        """Whether the URL names one specific arXiv paper.

        Paper identity, not host membership (#5617): every routing decision,
        the downloader's ``can_handle`` and the citation source label follow
        :func:`extract_arxiv_id`. A listing such as
        ``https://arxiv.org/list/cs.AI/recent`` is on the host but carries no
        identifier, so reporting ``is_arxiv: true`` for it told the UI
        something the pipeline disagreed with (#6413).
        """
        return is_arxiv_paper_url(url)

    def _is_pubmed_url(self, url: str) -> bool:
        """Check if URL is from PubMed or NCBI domains."""
        try:
            parsed = urlparse(url)
            hostname = parsed.hostname
            if not hostname:
                return False

            # Check for pubmed.ncbi.nlm.nih.gov
            if hostname == "pubmed.ncbi.nlm.nih.gov":
                return True

            # Check for ncbi.nlm.nih.gov with PMC path
            if hostname == "ncbi.nlm.nih.gov" and "/pmc" in parsed.path:
                return True

            # Check for pubmed in subdomain
            if "pubmed" in hostname:
                return True

            return False
        except Exception:
            return False

    def _apply_date_filter(self, query, model_class, date_filter: str):
        """Apply date range filter based on processed_at timestamp."""
        from datetime import datetime, timedelta, timezone

        now = datetime.now(timezone.utc)
        if date_filter == "today":
            cutoff = now.replace(hour=0, minute=0, second=0, microsecond=0)
        elif date_filter == "week":
            cutoff = now - timedelta(days=7)
        elif date_filter == "month":
            cutoff = now - timedelta(days=30)
        else:
            return query
        return query.filter(model_class.processed_at >= cutoff)

    def _apply_domain_filter(self, query, model_class, domain: str):
        """Apply domain filter to query for Document.

        The dropdown is fully data-driven (populated from get_unique_domains),
        so the filter is a generic substring match against original_url.
        """
        pattern = f"%{_escape_like(domain)}%"
        return query.filter(model_class.original_url.like(pattern, escape="\\"))

    def _apply_search_filter(self, query, model_class, search_query: str):
        """Apply the search filter to a Document query.

        Matches the document's title, authors and DOI, the title of a linked
        research resource, and the stored filename where that filename is
        the name the document is listed under. ``ilike`` folds case for
        ASCII letters only: on SQLite it is ``lower(...) LIKE lower(...)``.

        ``get_documents`` lists a document as ``document_title``, which is
        ``doc.title or (resource.title if resource else doc.filename)``, and
        the Library card falls back to ``file_name`` when that is empty;
        ``file_name`` is the filename unless the document has a real file
        path. So the filename is the listed name only when the document has
        no title and either no linked resource, or a linked resource with no
        title and no real file path. Any other stored filename is not
        searched: a titled document (every Zotero item) is listed under its
        title, and a document listed under a resource title is found by that
        title.

        The caller must outer-join ``ResearchResource`` (``get_documents``
        does).
        """
        search_pattern = f"%{_escape_like(search_query)}%"
        listed_under_filename = and_(
            _sql_falsy(model_class.title),
            or_(
                ResearchResource.id.is_(None),
                and_(
                    ResearchResource.id == _display_resource_id(model_class),
                    _sql_falsy(ResearchResource.title),
                    or_(
                        _sql_falsy(model_class.file_path),
                        model_class.file_path.in_(FILE_PATH_SENTINELS),
                    ),
                ),
            ),
        )
        return query.filter(
            or_(
                model_class.title.ilike(search_pattern, escape="\\"),
                and_(
                    listed_under_filename,
                    model_class.filename.ilike(search_pattern, escape="\\"),
                ),
                model_class.authors.ilike(search_pattern, escape="\\"),
                model_class.doi.ilike(search_pattern, escape="\\"),
                ResearchResource.title.ilike(search_pattern, escape="\\"),
            )
        )

    def get_library_stats(self) -> Dict:
        """Get overall library statistics."""
        with get_user_db_session(self.username) as session:
            # Get document counts
            total_docs = session.query(Document).count()
            total_pdfs = (
                session.query(Document).filter_by(file_type="pdf").count()
            )

            # Get size stats
            size_result = session.query(
                func.sum(Document.file_size),
                func.avg(Document.file_size),
            ).first()

            total_size = size_result[0] or 0
            avg_size = size_result[1] or 0

            # Get research stats
            research_count = session.query(
                func.count(func.distinct(Document.research_id))
            ).scalar()

            # Get domain stats - count unique domains from URLs
            # Extract domain from original_url using SQL functions
            from sqlalchemy import case, func as sql_func

            # Count unique domains by extracting them from URLs
            domain_subquery = session.query(
                sql_func.distinct(
                    case(
                        (
                            Document.original_url.like("%arxiv.org%"),
                            "arxiv.org",
                        ),
                        (
                            Document.original_url.like("%pubmed%"),
                            "pubmed",
                        ),
                        (
                            Document.original_url.like("%ncbi.nlm.nih.gov%"),
                            "pubmed",
                        ),
                        else_="other",
                    )
                )
            ).subquery()

            domain_count = (
                session.query(sql_func.count())
                .select_from(domain_subquery)
                .scalar()
            )

            # Get download tracker stats
            pending_downloads = (
                session.query(DownloadTracker)
                .filter_by(is_downloaded=False)
                .count()
            )

            return {
                "total_documents": total_docs,
                "total_pdfs": total_pdfs,
                "total_size_bytes": total_size,
                "total_size_mb": total_size / (1024 * 1024)
                if total_size
                else 0,
                "average_size_mb": avg_size / (1024 * 1024) if avg_size else 0,
                "research_sessions": research_count,
                "unique_domains": domain_count,
                "pending_downloads": pending_downloads,
                "storage_path": self._get_storage_path(),
            }

    def count_documents(
        self,
        research_id: Optional[str] = None,
        domain: Optional[str] = None,
        collection_id: Optional[str] = None,
        date_filter: Optional[str] = None,
    ) -> int:
        """Count documents matching the given filters (for pagination)."""
        with get_user_db_session(self.username) as session:
            from ...database.library_init import get_default_library_id

            if not collection_id:
                collection_id = get_default_library_id(self.username)

            q = (
                session.query(func.count(Document.id))
                .join(
                    DocumentCollection,
                    Document.id == DocumentCollection.document_id,
                )
                .filter(DocumentCollection.collection_id == collection_id)
                .filter(Document.status == "completed")
            )

            if research_id:
                q = q.filter(Document.research_id == research_id)
            if domain:
                q = self._apply_domain_filter(q, Document, domain)
            if date_filter:
                q = self._apply_date_filter(q, Document, date_filter)

            return q.scalar() or 0

    def get_documents(
        self,
        research_id: Optional[str] = None,
        domain: Optional[str] = None,
        file_type: Optional[str] = None,
        favorites_only: bool = False,
        search_query: Optional[str] = None,
        collection_id: Optional[str] = None,
        date_filter: Optional[str] = None,
        limit: int = 100,
        offset: int = 0,
    ) -> List[Dict]:
        """
        Get documents with filtering options.

        Returns enriched document information with research details.
        """
        with get_user_db_session(self.username) as session:
            # Get default Library collection ID if not specified
            from ...database.library_init import get_default_library_id

            if not collection_id:
                collection_id = get_default_library_id(self.username)

            logger.info(
                f"[LibraryService] Getting documents for collection_id: {collection_id}, research_id: {research_id}, domain: {domain}"
            )

            all_documents = []

            # Step 1: subquery to get paginated document IDs.
            # Pagination and sorting happen here on Document alone,
            # avoiding non-determinism from outer-joining related tables.
            doc_subq = (
                session.query(Document.id)
                .join(
                    DocumentCollection,
                    Document.id == DocumentCollection.document_id,
                )
                .filter(DocumentCollection.collection_id == collection_id)
            )

            # Apply filters
            if research_id:
                doc_subq = doc_subq.filter(Document.research_id == research_id)

            if domain:
                doc_subq = self._apply_domain_filter(doc_subq, Document, domain)

            if date_filter:
                doc_subq = self._apply_date_filter(
                    doc_subq, Document, date_filter
                )

            if file_type:
                doc_subq = doc_subq.filter(Document.file_type == file_type)

            if favorites_only:
                doc_subq = doc_subq.filter(Document.favorite.is_(True))

            if search_query:
                # _apply_search_filter references ResearchResource.title,
                # so we must outerjoin it here. DISTINCT prevents fan-out
                # from the outerjoin duplicating Document IDs.
                doc_subq = doc_subq.outerjoin(
                    ResearchResource,
                    (Document.resource_id == ResearchResource.id)
                    | (ResearchResource.document_id == Document.id),
                ).distinct()
                doc_subq = self._apply_search_filter(
                    doc_subq, Document, search_query
                )

            # Filter to only completed documents
            doc_subq = doc_subq.filter(Document.status == "completed")

            # Sort at SQL level (SQLite-safe NULL handling)
            doc_subq = doc_subq.order_by(
                case((Document.processed_at.isnot(None), 0), else_=1),
                Document.processed_at.desc(),
            )

            # Apply SQL-level pagination
            doc_subq = doc_subq.offset(offset).limit(limit)
            doc_id_subq = doc_subq.subquery()

            # Step 2: join the paginated document IDs with related tables.
            # Use two separate outer joins for ResearchResource to avoid
            # the OR-condition join that can fan out to multiple rows:
            #   - ResourceByFK: matched via Document.resource_id (primary FK)
            #   - ResourceByDoc: matched via ResearchResource.document_id
            # Prefer ResourceByFK; otherwise use the earliest back-linked
            # resource, matching the filename search's display selection.
            ResourceByFK = aliased(ResearchResource)
            ResourceByDoc = aliased(ResearchResource)

            query = (
                session.query(
                    Document,
                    ResourceByFK,
                    ResourceByDoc,
                    ResearchHistory,
                    DocumentCollection,
                )
                .join(doc_id_subq, Document.id == doc_id_subq.c.id)
                .join(
                    DocumentCollection,
                    Document.id == DocumentCollection.document_id,
                )
                .outerjoin(
                    ResourceByFK,
                    Document.resource_id == ResourceByFK.id,
                )
                .outerjoin(
                    ResourceByDoc,
                    ResourceByDoc.id == _fallback_resource_id(Document),
                )
                .outerjoin(
                    ResearchHistory,
                    Document.research_id == ResearchHistory.id,
                )
                .filter(DocumentCollection.collection_id == collection_id)
                # Re-apply sort so final results are ordered
                .order_by(
                    case((Document.processed_at.isnot(None), 0), else_=1),
                    Document.processed_at.desc(),
                )
            )

            # Execute query
            results = query.all()
            logger.info(
                f"[LibraryService] Found {len(results)} documents in collection {collection_id}"
            )

            # Batch-check blob existence to avoid N+1 queries
            doc_ids = [row[0].id for row in results]
            blob_ids = set()
            if doc_ids:
                blob_ids = {
                    r[0]
                    for r in session.query(DocumentBlob.document_id)
                    .filter(DocumentBlob.document_id.in_(doc_ids))
                    .all()
                }

            # Process results, defensively deduplicating by doc.id.
            #
            # Resolve one settings manager for the whole page (lazily, on the
            # first document that actually needs a path resolved) instead of
            # letting get_absolute_path_from_settings() fall back to the
            # ambient get_settings_manager() per iteration (up to `limit`
            # times). Each ambient call opens a DB session via
            # get_db_session(), which is now tracked in a per-thread owner
            # registry (utilities/db_utils.py) until this request's cleanup
            # boundary — so on a busy multi-user instance, enough process-wide
            # LRU evictions between iterations can pin one pooled connection
            # per evicted-and-recreated session for the rest of the request
            # (see changelog/#6095 follow-up). Creating it once here bounds
            # that to at most one session for this call, mirroring the
            # pattern already used in DownloadService (self.settings,
            # threaded through get_absolute_path_from_settings calls).
            seen_doc_ids = set()
            settings_manager = None
            try:
                for (
                    doc,
                    res_by_fk,
                    res_by_doc,
                    research,
                    doc_collection,
                ) in results:
                    if doc.id in seen_doc_ids:
                        continue
                    seen_doc_ids.add(doc.id)
                    # Prefer the resource matched via Document.resource_id FK;
                    # fall back to the one matched via ResearchResource.document_id.
                    resource = res_by_fk or res_by_doc
                    # Determine availability flags - use Document.file_path directly.
                    # Resolved here and NOT returned: the absolute path is the server's
                    # own directory layout, which /library/api/check-downloads already
                    # withholds for the same reason (#6462). Callers fetch the file
                    # through /document/{id}/pdf, so they never need it.
                    file_absolute_path = None
                    if (
                        doc.file_path
                        and doc.file_path not in FILE_PATH_SENTINELS
                    ):
                        if settings_manager is None:
                            settings_manager = get_settings_manager(
                                username=self.username
                            )
                        abs_path = get_absolute_path_from_settings(
                            doc.file_path,
                            self.username,
                            settings_manager=settings_manager,
                        )
                        if abs_path:
                            file_absolute_path = str(abs_path)

                    # Check if PDF is available (filesystem OR database)
                    has_pdf = bool(file_absolute_path)
                    # NULL storage_mode predates the column: a blob then
                    # means database storage, as infer_storage_mode() says.
                    if not has_pdf and doc.storage_mode in (None, "database"):
                        has_pdf = doc.id in blob_ids
                    has_text_db = bool(doc.text_content)  # Text now in Document

                    # Use DocumentCollection from query results
                    has_rag_indexed = (
                        doc_collection.indexed if doc_collection else False
                    )
                    rag_chunk_count = (
                        doc_collection.chunk_count if doc_collection else 0
                    )

                    all_documents.append(
                        {
                            "id": doc.id,
                            "resource_id": doc.resource_id,
                            "research_id": doc.research_id,
                            # Document info
                            "document_title": doc.title
                            or (resource.title if resource else doc.filename),
                            "authors": doc.authors,
                            "published_date": doc.published_date,
                            "doi": doc.doi,
                            "arxiv_id": doc.arxiv_id,
                            "pmid": doc.pmid,
                            # File info
                            "file_path": doc.file_path,
                            "file_name": Path(doc.file_path).name
                            if doc.file_path
                            and doc.file_path not in FILE_PATH_SENTINELS
                            else doc.filename,
                            "file_size": doc.file_size,
                            "file_type": doc.file_type,
                            # URLs
                            "original_url": doc.original_url,
                            "domain": self._extract_domain(doc.original_url)
                            if doc.original_url
                            else "User Upload",
                            # Status
                            "download_status": doc.status or "completed",
                            "downloaded_at": doc.processed_at.isoformat()
                            if doc.processed_at
                            else (
                                doc.uploaded_at.isoformat()
                                if hasattr(doc, "uploaded_at")
                                and doc.uploaded_at
                                else None
                            ),
                            "favorite": doc.favorite
                            if hasattr(doc, "favorite")
                            else False,
                            "tags": doc.tags if hasattr(doc, "tags") else [],
                            # Research info (None for user uploads)
                            "research_title": research.title
                            or research.query[:80]
                            if research
                            else "User Upload",
                            "research_query": research.query
                            if research
                            else None,
                            "research_mode": research.mode
                            if research
                            else None,
                            "research_date": research.created_at
                            if research
                            else None,
                            # Classification flags
                            "is_arxiv": self._is_arxiv_url(doc.original_url)
                            if doc.original_url
                            else False,
                            "is_pubmed": self._is_pubmed_url(doc.original_url)
                            if doc.original_url
                            else False,
                            "is_pdf": doc.file_type == "pdf",
                            # Availability flags
                            "has_pdf": has_pdf,
                            "has_text_db": has_text_db,
                            "has_rag_indexed": has_rag_indexed,
                            "rag_chunk_count": rag_chunk_count,
                        }
                    )

                # Sorting and pagination are now handled at SQL level
                return all_documents
            finally:
                safe_close(
                    settings_manager, "settings manager", allow_none=True
                )

    def get_all_collections(self) -> List[Dict]:
        """Get all collections with document and indexed document counts."""
        with get_user_db_session(self.username) as session:
            # Query collections with document counts and indexed counts
            results = (
                session.query(
                    Collection,
                    func.count(DocumentCollection.document_id).label(
                        "document_count"
                    ),
                    func.count(
                        case(
                            (
                                DocumentCollection.indexed == True,  # noqa: E712
                                DocumentCollection.document_id,
                            ),
                            else_=None,
                        )
                    ).label("indexed_document_count"),
                )
                .outerjoin(
                    DocumentCollection,
                    Collection.id == DocumentCollection.collection_id,
                )
                .group_by(Collection.id)
                .order_by(Collection.is_default.desc(), Collection.name)
                .all()
            )

            logger.info(f"[LibraryService] Found {len(results)} collections")

            collections = []
            for collection, doc_count, indexed_count in results:
                logger.debug(
                    f"[LibraryService] Collection: {collection.name} (ID: {collection.id}), documents: {doc_count}, indexed: {indexed_count}"
                )
                collections.append(
                    {
                        "id": collection.id,
                        "name": collection.name,
                        "description": collection.description,
                        "is_default": collection.is_default,
                        "document_count": doc_count or 0,
                        "indexed_document_count": indexed_count or 0,
                    }
                )

            return collections

    def get_research_list_for_dropdown(self) -> List[Dict]:
        """Get minimal research session info for filter dropdowns.

        Returns only id, title, and query — no joins or aggregates — for at
        most the ``_DROPDOWN_RESEARCH_LIMIT`` most-recent sessions, so a very
        large history cannot load every row into memory / the DOM (#4560).
        """
        with get_user_db_session(self.username) as session:
            results = (
                session.query(
                    ResearchHistory.id,
                    ResearchHistory.title,
                    ResearchHistory.query,
                )
                .order_by(ResearchHistory.created_at.desc())
                .limit(_DROPDOWN_RESEARCH_LIMIT)
                .all()
            )
            return [
                {"id": r.id, "title": r.title, "query": r.query}
                for r in results
            ]

    def get_research_list_with_stats(
        self,
        limit: int = 0,
        offset: int = 0,
    ) -> List[Dict]:
        """Get research sessions with download statistics.

        ``downloaded_count`` leaves out a document that awaits its PDF (see
        ``_counts_as_downloaded``).

        Args:
            limit: Maximum number of results (0 = no limit, for backwards compat).
            offset: Number of rows to skip. Only applied when limit > 0.
        """
        with get_user_db_session(self.username) as session:
            # Query research sessions with resource counts
            query = (
                session.query(
                    ResearchHistory,
                    func.count(func.distinct(ResearchResource.id)).label(
                        "total_resources"
                    ),
                    func.count(
                        func.distinct(
                            case(
                                (_counts_as_downloaded(), Document.id),
                                else_=None,
                            )
                        )
                    ).label("downloaded_count"),
                    func.count(
                        func.distinct(
                            case(
                                (
                                    ResearchResource.url.like("%.pdf")
                                    | ResearchResource.url.like("%arxiv.org%")
                                    | ResearchResource.url.like(
                                        "%ncbi.nlm.nih.gov/pmc%"
                                    ),
                                    ResearchResource.id,
                                ),
                                else_=None,
                            )
                        )
                    ).label("downloadable_count"),
                )
                .outerjoin(
                    ResearchResource,
                    ResearchHistory.id == ResearchResource.research_id,
                )
                .outerjoin(
                    Document,
                    resource_document_clause(),
                )
                .group_by(ResearchHistory.id)
                .order_by(ResearchHistory.created_at.desc())
            )

            # Apply SQL-level pagination when limit is set
            if limit > 0:
                query = query.offset(offset).limit(limit)

            results = query.all()

            # Preload all ratings to avoid N+1 queries
            research_ids = [r[0].id for r in results]
            all_ratings = (
                session.query(ResearchRating)
                .filter(ResearchRating.research_id.in_(research_ids))
                .all()
                if research_ids
                else []
            )
            ratings_by_research = {r.research_id: r for r in all_ratings}

            # Batch domain queries to avoid N+1 (same pattern as ratings)
            domain_case = case(
                (
                    ResearchResource.url.like("%arxiv.org%"),
                    "arxiv.org",
                ),
                (ResearchResource.url.like("%pubmed%"), "pubmed"),
                (
                    ResearchResource.url.like("%ncbi.nlm.nih.gov%"),
                    "pubmed",
                ),
                else_="other",
            )
            all_domains = (
                session.query(
                    ResearchResource.research_id,
                    domain_case.label("domain"),
                    func.count().label("count"),
                )
                .filter(ResearchResource.research_id.in_(research_ids))
                .group_by(ResearchResource.research_id, domain_case)
                .all()
                if research_ids
                else []
            )
            domains_by_research: Dict[str, list] = {}
            for rid, domain, count in all_domains:
                domains_by_research.setdefault(rid, []).append((domain, count))

            research_list = []
            for (
                research,
                total_resources,
                downloaded_count,
                downloadable_count,
            ) in results:
                # Get rating from preloaded dict
                rating = ratings_by_research.get(research.id)

                # Get domain breakdown from preloaded dict
                domains = domains_by_research.get(research.id, [])

                research_list.append(
                    {
                        "id": research.id,
                        "title": research.title,
                        "query": research.query,
                        "mode": research.mode,
                        "status": research.status,
                        "created_at": research.created_at,
                        "duration_seconds": research.duration_seconds,
                        "total_resources": total_resources or 0,
                        "downloaded_count": downloaded_count or 0,
                        "downloadable_count": downloadable_count or 0,
                        "rating": rating.rating if rating else None,
                        "top_domains": [(d, c) for d, c in domains if d],
                    }
                )

            return research_list

    def get_download_manager_summary_stats(self) -> Dict:
        """Get aggregate download stats across ALL research sessions.

        This is a lightweight query that only returns totals — used for
        the download-manager header so stats remain accurate regardless
        of which page the user is viewing. A document that awaits its PDF
        does not count as downloaded (see ``_counts_as_downloaded``): it is
        left out of ``already_downloaded``, so it is not subtracted from
        ``available_to_download``.
        """
        with get_user_db_session(self.username) as session:
            row = (
                session.query(
                    func.count(func.distinct(ResearchHistory.id)).label(
                        "total_researches"
                    ),
                    func.count(func.distinct(ResearchResource.id)).label(
                        "total_resources"
                    ),
                    func.count(
                        func.distinct(
                            case(
                                (_counts_as_downloaded(), Document.id),
                                else_=None,
                            )
                        )
                    ).label("downloaded_count"),
                    func.count(
                        func.distinct(
                            case(
                                (
                                    ResearchResource.url.like("%.pdf")
                                    | ResearchResource.url.like("%arxiv.org%")
                                    | ResearchResource.url.like(
                                        "%ncbi.nlm.nih.gov/pmc%"
                                    ),
                                    ResearchResource.id,
                                ),
                                else_=None,
                            )
                        )
                    ).label("downloadable_count"),
                )
                .select_from(ResearchHistory)
                .outerjoin(
                    ResearchResource,
                    ResearchHistory.id == ResearchResource.research_id,
                )
                .outerjoin(
                    Document,
                    resource_document_clause(),
                )
                .one()
            )

            total_researches = row.total_researches or 0
            total_resources = row.total_resources or 0
            downloaded = row.downloaded_count or 0
            downloadable = row.downloadable_count or 0

            return {
                "total_researches": total_researches,
                "total_resources": total_resources,
                "already_downloaded": downloaded,
                "available_to_download": max(downloadable - downloaded, 0),
            }

    def get_pdf_previews_batch(
        self, research_ids: List, limit_per_research: int = 10
    ) -> Dict[str, Dict]:
        """Batch-fetch PDF documents and domain breakdowns for multiple research sessions.

        Returns a dict keyed by research_id with:
            - "pdf_sources": list of document dicts (capped at limit_per_research),
              each with "document_title", "domain", "file_type" and
              "downloaded" (whether the Download Manager shows it as
              downloaded, with its "Downloaded" badge)
            - "domains": dict of domain -> {total, pdfs, downloaded}

        "downloaded" follows the rule of the page's other counts (see
        ``document_counts_as_downloaded``): a completed document counts,
        unless it awaits its PDF.
        """
        if not research_ids:
            return {}

        with get_user_db_session(self.username) as session:
            results = (
                session.query(Document, ResearchResource)
                .outerjoin(
                    ResearchResource,
                    (Document.resource_id == ResearchResource.id)
                    | (ResearchResource.document_id == Document.id),
                )
                .filter(
                    Document.research_id.in_(research_ids),
                    Document.file_type == "pdf",
                )
                .order_by(Document.processed_at.desc())
                .limit(limit_per_research * len(research_ids))
                .all()
            )

            previews: Dict[str, Dict] = {}
            seen_doc_ids: set = set()
            for doc, resource in results:
                if doc.id in seen_doc_ids:
                    continue
                seen_doc_ids.add(doc.id)

                rid = doc.research_id
                if rid not in previews:
                    previews[rid] = {"pdf_sources": [], "domains": {}}

                entry = previews[rid]

                # Domain breakdown (within the SQL LIMIT budget); authority
                # minus userinfo, which the response would otherwise carry.
                # The helper never raises and returns "" when unparseable.
                domain = "unknown"
                if resource and resource.url:
                    domain = (
                        url_authority_without_userinfo(resource.url)
                        or "unknown"
                    )
                elif doc.original_url:
                    domain = (
                        url_authority_without_userinfo(doc.original_url)
                        or "unknown"
                    )

                if domain not in entry["domains"]:
                    entry["domains"][domain] = {
                        "total": 0,
                        "pdfs": 0,
                        "downloaded": 0,
                    }
                entry["domains"][domain]["total"] += 1
                if doc.file_type == "pdf":
                    entry["domains"][domain]["pdfs"] += 1
                downloaded = document_counts_as_downloaded(doc)
                if downloaded:
                    entry["domains"][domain]["downloaded"] += 1

                # PDF sources preview (capped)
                if len(entry["pdf_sources"]) < limit_per_research:
                    title = "Untitled"
                    if resource and resource.title:
                        title = resource.title
                    elif doc.filename:
                        title = doc.filename

                    entry["pdf_sources"].append(
                        {
                            "document_title": title,
                            "domain": domain,
                            "file_type": doc.file_type,
                            "downloaded": downloaded,
                        }
                    )

            return previews

    def get_document_by_id(self, doc_id: str) -> Optional[Dict]:
        """
        Get a specific document by its ID.

        Returns document information with file path.
        """
        with get_user_db_session(self.username) as session:
            # Find document - use outer joins to support both research downloads and user uploads
            result = (
                session.query(Document, ResearchResource, ResearchHistory)
                .outerjoin(
                    ResearchResource,
                    (Document.resource_id == ResearchResource.id)
                    | (ResearchResource.document_id == Document.id),
                )
                .outerjoin(
                    ResearchHistory,
                    Document.research_id == ResearchHistory.id,
                )
                .filter(Document.id == doc_id)
                .first()
            )

            if result:
                # Found document
                doc, resource, research = result

                # Get RAG indexing status across all collections
                doc_collections = (
                    session.query(DocumentCollection, Collection)
                    .join(Collection)
                    .filter(DocumentCollection.document_id == doc_id)
                    .all()
                )

                # Check if indexed in any collection
                has_rag_indexed = any(
                    dc.indexed for dc, coll in doc_collections
                )
                total_chunks = sum(
                    dc.chunk_count for dc, coll in doc_collections if dc.indexed
                )

                # Build collections list
                collections_list = [
                    {
                        "id": coll.id,
                        "name": coll.name,
                        "indexed": dc.indexed,
                        "chunk_count": dc.chunk_count,
                    }
                    for dc, coll in doc_collections
                ]

                # Calculate word count from text content
                word_count = (
                    len(doc.text_content.split()) if doc.text_content else 0
                )

                # Check if PDF is available (database OR filesystem)
                has_pdf = bool(
                    doc.file_path and doc.file_path not in FILE_PATH_SENTINELS
                )
                # NULL storage_mode predates the column: a blob then means
                # database storage, as infer_storage_mode() says.
                if not has_pdf and doc.storage_mode in (None, "database"):
                    has_pdf = self._has_blob_in_db(session, doc.id)

                return {
                    "id": doc.id,
                    "resource_id": doc.resource_id,
                    "research_id": doc.research_id,
                    "document_title": doc.title
                    or (resource.title if resource else doc.filename),
                    "original_url": doc.original_url
                    or (resource.url if resource else None),
                    "file_path": doc.file_path,
                    "file_name": Path(doc.file_path).name
                    if doc.file_path
                    and doc.file_path not in FILE_PATH_SENTINELS
                    else doc.filename,
                    "file_size": doc.file_size,
                    "file_type": doc.file_type,
                    "mime_type": doc.mime_type,
                    "domain": self._extract_domain(resource.url)
                    if resource
                    else "User Upload",
                    # The status's string value, which the details page
                    # compares with 'completed' and prints: a loaded
                    # DocumentStatus member never equals that string.
                    "download_status": doc.status.value,
                    "downloaded_at": doc.processed_at.isoformat()
                    if doc.processed_at
                    and hasattr(doc.processed_at, "isoformat")
                    else str(doc.processed_at)
                    if doc.processed_at
                    else (
                        doc.uploaded_at.isoformat()
                        if hasattr(doc, "uploaded_at") and doc.uploaded_at
                        else None
                    ),
                    "favorite": doc.favorite
                    if hasattr(doc, "favorite")
                    else False,
                    "tags": doc.tags if hasattr(doc, "tags") else [],
                    "research_title": research.query[:100]
                    if research
                    else "User Upload",
                    "research_created_at": research.created_at
                    if research and isinstance(research.created_at, str)
                    else research.created_at.isoformat()
                    if research and research.created_at
                    else None,
                    # Document fields
                    "is_pdf": doc.file_type == "pdf",
                    "has_pdf": has_pdf,
                    "has_text_db": bool(doc.text_content),
                    "has_rag_indexed": has_rag_indexed,
                    "rag_chunk_count": total_chunks,
                    "word_count": word_count,
                    "collections": collections_list,
                }

            # Not found
            return None

    def toggle_favorite(self, document_id: str) -> bool:
        """Toggle favorite status of a document."""
        with get_user_db_session(self.username) as session:
            doc = session.get(Document, document_id)
            if doc:
                doc.favorite = not doc.favorite
                session.commit()
                return doc.favorite
            return False

    def delete_document(self, document_id: str) -> bool:
        """Delete a document from library (file and database entry).

        Refuses notes: they have their own deletion API
        (DELETE /api/notes/<id>) that performs version/link/synthesis
        cleanup. Hard-deleting a note here would bypass that and destroy
        the note's history. Defense-in-depth — the routed handler is the
        guarded DocumentDeletionService, but this is a public method.
        """
        from ...database.models.library import SourceType

        with get_user_db_session(self.username) as session:
            doc = session.get(Document, document_id)
            if not doc:
                return False

            note_source = (
                session.query(SourceType).filter_by(name="note").first()
            )
            if note_source and doc.source_type_id == note_source.id:
                logger.warning(
                    f"Refused to delete note {short_id(document_id)} via the "
                    "library document API; use DELETE /api/notes/<id>."
                )
                return False

            # A canonical URL can cover several historical PDF versions.
            # Use this document's storage; a tracker is only a legacy fallback
            # when its content hash or document link proves ownership.
            tracker = find_tracker_for_document(session, doc)
            stored_path = doc.file_path
            if (
                not stored_path
                and tracker
                and doc.storage_mode != "none"
                and not self._has_blob_in_db(session, doc.id)
            ):
                # A blob-backed or explicitly text-only document must not
                # borrow a stale physical tracker path. A legacy row with
                # neither a path nor a blob can still fall back to the
                # verified tracker's path, as the library sync does.
                stored_path = tracker.file_path

            # Delete physical file. Own-root only: never resolve via the
            # legacy shared root, where a colliding relative path can be
            # another tenant's file (#5521), consistent with the delete
            # sites in document_deletion.py / PDFStorageManager.delete_pdf.
            if stored_path and stored_path not in (
                *FILE_PATH_SENTINELS,
                "database",
            ):
                try:
                    file_path = get_absolute_path_from_settings(
                        stored_path,
                        self.username,
                    )
                    if file_path and file_path.is_file():
                        file_path.unlink()
                        logger.info(f"Deleted file: {file_path}")
                except Exception:
                    logger.exception("Failed to delete file")

            # Update tracker
            if tracker:
                tracker.is_downloaded = False
                tracker.file_path = None

            # Delete document and all related records
            from ..deletion.utils.cascade_helper import CascadeHelper

            CascadeHelper.delete_document_completely(session, document_id)
            session.commit()

        # Post-commit: purge the removed document's FAISS vectors + chunk rows
        # (delete_document_completely leaves DocumentChunk rows, so without this
        # the deleted content keeps surfacing in collection search). Best-effort;
        # the DB delete already committed. Mirrors sync_library_with_filesystem.
        try:
            from ..deletion.services.document_deletion import (
                DocumentDeletionService,
            )
            from ...database.models.library import DocumentChunk

            with get_user_db_session(self.username) as purge_session:
                coll_rows = (
                    purge_session.query(DocumentChunk.collection_name)
                    .filter_by(source_type="document", source_id=document_id)
                    .distinct()
                    .all()
                )
            collection_ids = [
                row[0].removeprefix("collection_")
                for row in coll_rows
                if row[0]
            ]
            DocumentDeletionService(self.username)._purge_document_rag(
                document_id, collection_ids, full_delete=True
            )
        except Exception:
            logger.opt(exception=True).error(
                "Failed to purge removed document {} (delete already committed)",
                document_id,
            )
        return True

    def open_file_location(self, document_id: str) -> bool:
        """Open the folder containing the document."""
        with get_user_db_session(self.username) as session:
            doc = session.get(Document, document_id)
            if not doc:
                return False

            tracker = None
            if doc.original_url:
                # B2 compat (#6758): match legacy-hash rows.
                tracker = find_tracker_by_url(session, doc.original_url)

            if tracker and tracker.file_path:
                # Resolve only within the per-user root (traversal + symlink safe).
                validated_path = get_absolute_path_from_settings(
                    tracker.file_path, self.username
                )
                if validated_path and validated_path.is_file():
                    return open_file_location(str(validated_path))
                logger.warning("Path validation failed or file missing")
                return False

            return False

    def get_unique_domains(self) -> List[str]:
        """Get sorted list of unique netlocs from all document URLs.

        Streams the URL column in batches (``yield_per``) and accumulates
        netlocs into a set, so a very large library is never fully
        materialized in memory at once (#4560). The query already projects to
        the single ``original_url`` column.
        """
        with get_user_db_session(self.username) as session:
            netlocs = set()
            rows = (
                session.query(Document.original_url)
                .filter(Document.original_url.isnot(None))
                .yield_per(_DOMAIN_SCAN_BATCH_SIZE)
            )
            for (url,) in rows:
                domain = self._extract_domain(url)
                if domain:
                    netlocs.add(domain)
            return sorted(netlocs)

    def _extract_domain(self, url: Optional[str]) -> str:
        """Extract domain from URL.

        The authority minus userinfo: a stored URL's ``user:pass@`` would
        otherwise reach the library page's domain filter and the
        document payloads. The domain filter's substring match still
        finds a credentialed URL by its host. ``""`` for a ``None``
        (``ResearchResource.url`` is nullable), unparseable or
        ambiguous-authority URL; never raises.
        """
        return url_authority_without_userinfo(url)

    def _get_storage_path(self) -> str:
        """Get library storage path from settings (respects LDR_DATA_DIR)."""
        settings = get_settings_manager()
        return str(
            Path(
                settings.get_setting(
                    "research_library.storage_path",
                    str(get_library_directory()),
                )
            )
            .expanduser()
            .resolve()
        )

    def sync_library_with_filesystem(self) -> Dict:
        """
        Sync library database with filesystem.
        Check which PDF files exist and update database accordingly.

        Every document with the same URL shares that URL's tracker, and
        URL-equivalent addresses (``paper.pdf`` and
        ``paper.pdf?utm_source=old``) can hold different PDF versions, each
        in a document of its own. So a tracker supplies a path for a
        document, or is reset for it, only when its content hash or
        explicit document link identifies the document, with contradictory
        identity fields rejected (see ``find_tracker_for_document``), and
        only when it names where the document stores its PDF (see
        ``_tracker_describes_storage``): the document's own path, which wins
        over any other tracker path, or for a document without a path of
        its own, the tracker's ``"database"`` or legacy path. Each document
        is judged by what it stores itself, and a tracker that does not
        describe the document is left unchanged.

        A download whose PDF file is missing is handled by what it still
        stores (see ``_remaining_content``):

        * its PDF, as a database blob or a file at its own ``file_path``:
          kept unchanged;
        * only its extracted text, while its own ``file_path`` (or, without
          one, its identifying tracker's path) names a file, and it still
          has its research resource: kept with its text but made text-only,
          its identifying tracker reset if it names that file or no file,
          and counted in ``files_missing`` and ``missing_files``, so that
          the download manager downloads its PDF again (see
          ``_keep_text_report_missing_pdf``);
        * only its extracted text, while a file is named as above, with no
          research resource (e.g. a download whose resource was deleted):
          kept with its text and made text-only, its identifying tracker
          reset as above, and counted in ``files_found``. No download path
          asks for its PDF, so it is not reported, but a later download of
          the same PDF bytes stores the PDF in it (see
          ``_clear_missing_pdf``);
        * only its extracted text, with no file named (a "database" label
          without a blob): kept unchanged;
        * nothing: deleted, so that its URL can be downloaded again, and
          counted in ``files_missing``. Its identifying tracker is reset if
          it names the document's file. If that tracker is ``"database"``,
          it is reset only when, once every document has been checked, no
          other completed document under that tracker still holds a PDF
          blob, whether the tracker identifies that document or not: until
          then the URL's PDF is still stored in the database, as the
          tracker records.

        Left alone before any of these checks: a document whose own
        ``file_path`` is a sentinel, such as ``blob_deleted`` after "Remove
        PDF to save space", and a document whose ``storage_mode`` is "none"
        (a text-only document), whatever a tracker names. A document without
        a path of its own that no identifying tracker gives one is left
        alone too, unless it is labelled as stored in the database: its
        missing or ambiguous metadata cannot prove which file is absent.

        ``missing_files`` lists every document counted in ``files_missing``
        by the path stored for it, relative to the library root, never by
        the absolute path on the server (see ``_missing_file_entry``): the
        path that named its missing file, ``"database"`` for a document
        whose PDF was to be a database blob, or ``"invalid"`` for a path
        outside the library root.

        ``trackers_updated`` counts the trackers the sync changed, each
        once (see ``_clear_missing_pdf``).

        Returns:
            Statistics about the sync operation
        """
        with get_user_db_session(self.username) as session:
            # Sync only research downloads — uploads have no original_url
            # and no DownloadTracker, so they don't belong in this routine.
            # Don't load the large text_content body: this sync loop only
            # reads original_url/id/title (and may delete the row), so loading
            # every completed document's full text would needlessly exhaust
            # memory on a large library (#4560).
            documents = (
                session.query(Document)
                .filter_by(status=DocumentStatus.COMPLETED)
                .filter(Document.original_url.isnot(None))
                .options(defer(Document.text_content))
                .all()
            )

            stats = {
                "total_documents": len(documents),
                "files_found": 0,
                "files_missing": 0,
                "trackers_updated": 0,
                "missing_files": [],
            }
            # Documents deleted below — their FAISS vectors + chunk rows are
            # purged AFTER this transaction commits (see the post-commit block).
            removed_doc_ids: List[str] = []
            # The documents checked under each "database" tracker, by
            # tracker id, and the "database" trackers whose document was
            # deleted below. Whether such a tracker is reset is decided after
            # the loop, once every document under it has been checked.
            database_tracker_documents: Dict[int, List[str]] = {}
            emptied_database_trackers: Dict[int, DownloadTracker] = {}

            # Sync documents with filesystem.
            #
            # Resolve one settings manager for the whole sync (lazily, on the
            # first document that actually needs a path resolved) instead of
            # letting get_absolute_path_from_settings() fall back to the
            # ambient get_settings_manager() per document (this is a sync
            # `def`, so a large library means one iteration per completed
            # document, unbounded). Each ambient call opens a DB session that
            # is now tracked in a per-thread owner registry
            # (utilities/db_utils.py) until this request's cleanup boundary,
            # so enough process-wide LRU evictions between iterations can pin
            # one pooled connection per evicted-and-recreated session for the
            # rest of the request (see changelog/#6095 follow-up). Creating
            # it once here bounds that to at most one session, mirroring
            # DownloadService's self.settings pattern.
            settings_manager = None
            try:
                for doc in documents:
                    # A URL has one tracker, shared by every document with
                    # that URL: the documents of two researches that cite
                    # the same paper, or a failed-download placeholder that
                    # "Extract Text to DB" completed with text next to the
                    # document that holds the PDF. URL-equivalent addresses,
                    # such as ``paper.pdf`` and ``paper.pdf?utm_source=old``,
                    # can also hold different PDF versions, each in a
                    # document of its own. So a URL match alone does not say
                    # whose file a tracker names: a tracker supplies a path
                    # for this document, or is reset for it, only when its
                    # content hash or explicit document link identifies the
                    # document (find_tracker_for_document). The census of
                    # "database" trackers below also counts every document
                    # under the URL's tracker (``url_tracker``). B2 compat
                    # (#6758): both lookups match legacy-hash rows too.
                    url_tracker = find_tracker_by_url(session, doc.original_url)
                    tracker = find_tracker_for_document(session, doc)
                    for database_tracker in (url_tracker, tracker):
                        if (
                            database_tracker is not None
                            and database_tracker.file_path == "database"
                        ):
                            database_tracker_documents.setdefault(
                                database_tracker.id, []
                            ).append(doc.id)
                    # Explicit text-only mode and a sentinel path record a
                    # PDF left out on purpose. Sentinel paths
                    # (``blob_deleted``, ``text_only_not_stored``,
                    # ``metadata_only``) name no file by design: resolving
                    # one as a relative path always misses and would run
                    # ``delete_document_completely`` on the text/chunks/
                    # vectors the sentinel was protecting (issue #6760
                    # follow-up). "Remove PDF to save space"
                    # (delete_blob_only) writes ``blob_deleted`` to the
                    # document but leaves the tracker naming the copy it
                    # removed. That PDF was removed on purpose, so it is
                    # neither missing nor to be downloaded again. Such a
                    # document borrows no tracker path, not even from a
                    # tracker that identifies it.
                    if (
                        doc.storage_mode == "none"
                        or doc.file_path in FILE_PATH_SENTINELS
                    ):
                        stats["files_found"] += 1
                        continue
                    # The tracker's path is the one written last. It
                    # describes this document when it names the document's
                    # own path, which every download that stores a PDF file
                    # writes to both. A document without a path of its own,
                    # a database-stored one (its tracker records
                    # "database") or a legacy row, takes the path of the
                    # tracker that identifies it (see
                    # _tracker_describes_storage). Any other document is
                    # judged by what it stores itself, in the ``else``
                    # branch, and leaves the tracker alone.
                    # Judged by another document's file, it was counted
                    # found while its own file was missing, or labelled a
                    # PDF, made text-only and reported missing for a file it
                    # never had, which the download manager then downloaded
                    # into it as a second copy.
                    if (
                        tracker is not None
                        and tracker.file_path
                        and not _tracker_describes_storage(
                            tracker.file_path, doc
                        )
                    ):
                        tracker = None

                    if tracker and tracker.file_path:
                        # The tracker identifies this document and names
                        # where it stores its PDF: its own path, the
                        # database, or, for a document without a path of its
                        # own, the tracker's legacy path. A sentinel there
                        # names no file either: leave the document alone, as
                        # above.
                        if tracker.file_path in FILE_PATH_SENTINELS:
                            stats["files_found"] += 1
                            continue
                        # Database-stored PDFs have no filesystem file to
                        # sync — the blob is the source of truth. A present
                        # blob counts as found. ``DocumentBlob`` is the
                        # module's import: importing it here would make it a
                        # local name of the whole function, which the
                        # post-loop check below could read only after this
                        # branch had run.
                        if (
                            tracker.file_path == "database"
                            or doc.storage_mode == "database"
                        ):
                            if (
                                self._sync_pdf_blob_size(session, doc.id) or 0
                            ) > 0:
                                stats["files_found"] += 1
                                continue
                        # Check if the file exists in the per-user root.
                        if settings_manager is None:
                            settings_manager = get_settings_manager(
                                username=self.username
                            )
                        file_path = get_absolute_path_from_settings(
                            tracker.file_path,
                            self.username,
                            settings_manager=settings_manager,
                        )
                        # "database" names no file: a file of that name in
                        # the library root is not this document's PDF.
                        if (
                            _names_a_file(tracker.file_path)
                            and file_path
                            and _nonempty_file_size(file_path)
                        ):
                            stats["files_found"] += 1
                            continue
                        # The document's file is gone, or its blob is. What
                        # else it stores decides.
                        remaining = self._remaining_content(
                            session, doc, settings_manager
                        )
                        if remaining == "text" and (
                            _names_a_file(tracker.file_path)
                            or self._sync_pdf_blob_size(session, doc.id) == 0
                        ):
                            # An absent blob can be an intentional text-only
                            # placeholder. A present but empty PDF is damaged
                            # storage and must permit the native refill writer.
                            text_tracker = tracker
                            if tracker.file_path == "database":
                                emptied_database_trackers[tracker.id] = tracker
                                text_tracker = None
                            # Only the text is left of a PDF that was a
                            # file. Keep the text and clear the dangling
                            # PDF reference.
                            if doc.resource_id is not None:
                                # Its resource can download the PDF again:
                                # report the PDF missing, under the stored
                                # path that named the missing file.
                                self._keep_text_report_missing_pdf(
                                    doc,
                                    text_tracker,
                                    tracker.file_path,
                                    file_path,
                                    stats,
                                )
                            else:
                                # No download path asks for its PDF, but a
                                # later download of the same bytes stores
                                # the PDF in it (see _clear_missing_pdf).
                                self._clear_missing_pdf(
                                    doc, text_tracker, stats
                                )
                                stats["files_found"] += 1
                        elif remaining is not None:
                            # Its PDF is still stored as a blob, or its text
                            # is left and its path is the "database" label,
                            # which names no file. Keep it and its tracker
                            # unchanged.
                            stats["files_found"] += 1
                        else:
                            # File missing or path invalid - mark for re-download
                            stats["files_missing"] += 1
                            stats["missing_files"].append(
                                _missing_file_entry(
                                    doc, tracker.file_path, file_path
                                )
                            )

                            if tracker.file_path == "database":
                                # "database" records the URL's PDF for every
                                # document of the URL stored there, and
                                # another one can still hold its PDF blob.
                                # Decided after the loop.
                                emptied_database_trackers[tracker.id] = tracker
                            else:
                                # Reset tracker
                                tracker.is_downloaded = False
                                tracker.file_path = None
                                stats["trackers_updated"] += 1

                            # Delete the document entry so it can be re-queued
                            from ..deletion.utils.cascade_helper import (
                                CascadeHelper,
                            )

                            CascadeHelper.delete_document_completely(
                                session, doc.id
                            )
                            removed_doc_ids.append(doc.id)
                    else:
                        # No tracker identifies the document, its tracker has
                        # no path, or its tracker names another path than
                        # the document's own: another file, or "database"
                        # for a document with a file path. Only the
                        # document's own tracker without a path is passed
                        # on; otherwise ``tracker`` is None. A document
                        # without a path of its own that is not labelled as
                        # stored in the database names no storage to check,
                        # e.g. a pre-upgrade row with an unset (NULL)
                        # storage_mode. That missing or ambiguous metadata
                        # cannot prove which file is absent, so the document
                        # is left alone.
                        if not doc.file_path and doc.storage_mode != "database":
                            stats["files_found"] += 1
                            continue
                        # Any other label can be stale, e.g. a lost tracker
                        # path after a "none"-mode re-download, or the
                        # "database" default on a placeholder that later got
                        # text. What the document still stores decides.
                        if settings_manager is None and doc.file_path:
                            settings_manager = get_settings_manager(
                                username=self.username
                            )
                        remaining = self._remaining_content(
                            session, doc, settings_manager
                        )
                        if remaining == "text" and (
                            _names_a_file(doc.file_path)
                            or self._sync_pdf_blob_size(session, doc.id) == 0
                        ):
                            # Its own PDF file is gone and only the text is
                            # left. Keep the text and clear the dangling
                            # PDF reference.
                            if doc.resource_id is not None:
                                # Its resource can download the PDF again:
                                # report the PDF missing.
                                self._keep_text_report_missing_pdf(
                                    doc,
                                    tracker,
                                    doc.file_path or "database",
                                    (
                                        get_absolute_path_from_settings(
                                            doc.file_path,
                                            self.username,
                                            settings_manager=settings_manager,
                                        )
                                        if _names_a_file(doc.file_path)
                                        else None
                                    ),
                                    stats,
                                )
                            else:
                                # No download path asks for its PDF, but a
                                # later download of the same bytes stores
                                # the PDF in it (see _clear_missing_pdf).
                                self._clear_missing_pdf(doc, tracker, stats)
                                stats["files_found"] += 1
                            continue
                        if remaining is not None:
                            # Its PDF is still stored, or its text is left
                            # and it names no file (e.g. the "database"
                            # default on a placeholder that later got text).
                            stats["files_found"] += 1
                            continue
                        # Nothing of it is left: delete it so that its URL
                        # can be downloaded again. A file its own path names
                        # is reported missing, as the branch above reports
                        # one that the tracker names. A document whose PDF
                        # was to be a database blob (its label or its path
                        # says "database") is reported as ``"database"``, as
                        # the branch above reports one that a "database"
                        # tracker describes.
                        stats["files_missing"] += 1
                        if _names_a_file(doc.file_path):
                            stats["missing_files"].append(
                                _missing_file_entry(
                                    doc,
                                    doc.file_path,
                                    get_absolute_path_from_settings(
                                        doc.file_path,
                                        self.username,
                                        settings_manager=settings_manager,
                                    ),
                                )
                            )
                        elif (
                            doc.storage_mode == "database"
                            or doc.file_path == "database"
                        ):
                            stats["missing_files"].append(
                                _missing_file_entry(doc, "database", None)
                            )
                        from ..deletion.utils.cascade_helper import (
                            CascadeHelper,
                        )

                        CascadeHelper.delete_document_completely(
                            session, doc.id
                        )
                        removed_doc_ids.append(doc.id)
            finally:
                safe_close(
                    settings_manager, "settings manager", allow_none=True
                )

            # A "database" tracker records that its URL's PDF is stored in
            # the database, which can hold it for several documents of the
            # URL. The one it identifies was deleted above with nothing
            # left. While another still holds a PDF blob, the record is true
            # and the tracker is left as it is; otherwise it is reset. A
            # document the tracker does not identify cannot reset it, but its
            # blob still keeps the record true. Every document under the
            # tracker has been checked by now, so the order in which the
            # sync read them does not change whether it is reset.
            if emptied_database_trackers:
                holders = (
                    session.query(Document)
                    .join(DocumentBlob, DocumentBlob.document_id == Document.id)
                    .filter(
                        Document.original_url.isnot(None),
                        func.length(DocumentBlob.pdf_binary) > 0,
                    )
                    .options(defer(Document.text_content))
                    .all()
                )
                for holder in holders:
                    # Use the same canonical/legacy lookups as the completed
                    # loop. A URL's shared record can remain true for another
                    # version, but only an identifying tracker can be reset.
                    for held_tracker in (
                        find_tracker_by_url(session, holder.original_url),
                        find_tracker_for_document(session, holder),
                    ):
                        if held_tracker is not None:
                            database_tracker_documents.setdefault(
                                held_tracker.id, []
                            ).append(holder.id)
            removed = set(removed_doc_ids)
            for tracker_id, tracker in emptied_database_trackers.items():
                others = [
                    document_id
                    for document_id in database_tracker_documents[tracker_id]
                    if document_id not in removed
                ]
                if others and (
                    session.query(DocumentBlob.document_id)
                    .filter(
                        DocumentBlob.document_id.in_(others),
                        func.length(DocumentBlob.pdf_binary) > 0,
                    )
                    .first()
                    is not None
                ):
                    continue
                tracker.is_downloaded = False
                tracker.file_path = None
                stats["trackers_updated"] += 1

            session.commit()
            logger.info(
                f"Library sync completed: {stats['files_found']} found, {stats['files_missing']} missing"
            )

        # Post-commit: purge the removed documents' FAISS vectors + chunk rows.
        # delete_document_completely leaves DocumentChunk rows (no FK), so
        # without this the removed doc's vectors and chunk_text linger and keep
        # surfacing in collection search. Reuse DocumentDeletionService's
        # post-commit purge (per-collection vector removal + guaranteed chunk-row
        # backstop), run after the session above has closed.
        if removed_doc_ids:
            from ..deletion.services.document_deletion import (
                DocumentDeletionService,
            )
            from ...database.models.library import DocumentChunk

            deleter = DocumentDeletionService(self.username)
            for doc_id in removed_doc_ids:
                # Best-effort per document: the sync's DB deletes already
                # committed, so a purge hiccup here must not raise out of the
                # sync (which would report the whole operation as failed) nor
                # abort the remaining documents' purges.
                try:
                    with get_user_db_session(self.username) as purge_session:
                        coll_rows = (
                            purge_session.query(DocumentChunk.collection_name)
                            .filter_by(source_type="document", source_id=doc_id)
                            .distinct()
                            .all()
                        )
                    collection_ids = [
                        row[0].removeprefix("collection_")
                        for row in coll_rows
                        if row[0]
                    ]
                    deleter._purge_document_rag(
                        doc_id, collection_ids, full_delete=True
                    )
                except Exception:
                    logger.opt(exception=True).error(
                        "Failed to purge removed document {} after library sync "
                        "(delete already committed)",
                        doc_id,
                    )

        return stats

    def mark_for_redownload(self, document_ids: List[str]) -> int:
        """
        Mark specific documents for re-download.

        Args:
            document_ids: List of document IDs to mark for re-download

        Returns:
            Number of documents marked
        """
        with get_user_db_session(self.username) as session:
            count = 0
            for doc_id in document_ids:
                doc = session.get(Document, doc_id)
                if doc:
                    if not doc.original_url:
                        # Uploads have no source URL and no DownloadTracker,
                        # so re-download is not meaningful for them.
                        logger.warning(
                            f"Skipping mark-for-redownload on {doc_id[:8]}…: "
                            "document has no original_url (likely user upload)"
                        )
                        continue
                    # Get tracker and reset it. B2 compat (#6758): match
                    # legacy-hash rows so a stale tracker is not left
                    # is_downloaded=True while the document is PENDING.
                    tracker = find_tracker_by_url(session, doc.original_url)

                    if tracker:
                        tracker.is_downloaded = False
                        tracker.file_path = None

                    # Mark document as pending
                    doc.status = DocumentStatus.PENDING
                    count += 1

            session.commit()
            logger.info(f"Marked {count} documents for re-download")
            return count