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.

44.6 KB
Raw Blame
"""
Service for managing research sources/resources in the database.

This service handles saving and retrieving sources from research
in a proper relational way using the ResearchResource table.
"""

from typing import List, Dict, Any, Optional
from datetime import datetime, UTC
from loguru import logger
from sqlalchemy import or_
import numbers

from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session, load_only

from ...database.models import (
    Journal,
    Paper,
    PaperAppearance,
    ResearchResource,
    ResearchHistory,
)
from ...database.session_context import get_user_db_session
from ...utilities.citation_normalizer import normalize_citation
from ...utilities.url_utils import (
    CHUNK_DISPLAY_KEY,
    canonical_url_key,
    library_display_url,
    preferred_chunk_display,
)


def _as_text(value: object) -> str:
    """Coerce a stored-dict value to ``str`` for slicing and ``len()``.

    Every read in the per-source loop comes from a JSON blob that
    round-tripped through the database, so an ``int`` or ``list`` in a
    text field is possible. Slicing one raises inside the broad
    ``except`` below, which rolls back and drops the whole citation with
    no message — the same silent-loss failure the url guards prevent.
    """
    if value is None:
        return ""
    if isinstance(value, str):
        return value
    try:
        return str(value)
    except Exception:
        # A coercion helper on a silent-drop path must not be able to
        # raise: an object whose ``__str__`` fails would otherwise take
        # out the exception handler that reports the failure, and with it
        # every sibling source in the batch.
        return ""


# Every field in this loop that is read as text — by normalize_citation,
# by a slice, as a dict key, or as a Text column. Coercing them once at
# the top of the loop is what stops the "guard the read a review named,
# miss its sibling" cycle: nine commits guarded url, then snippet, then
# ct_matched, then title, while pmid, arxiv_id and normalize_citation's
# own re-read of url stayed raw and kept dropping the citation.
_TEXT_FIELDS = (
    "url",
    "link",
    "title",
    "name",
    "snippet",
    "content_preview",
    "description",
    "source_type",
    "source_engine",
    "source",
    "container_title",
    "container-title",
    "journal",
    "venue",
    "journal_ref",
    "journal_name_matched",
    "volume",
    "issue",
    "pages",
    "publisher",
)


# Identifier fields. These are NOT text-coerced: they feed ``unique``
# columns on ``Paper`` and the dedup SELECT that decides whether two
# engines found the same paper. ``_extract_doi`` documents and tests a
# LIST doi (it takes ``doi[0]``), so stringifying one writes
# ``"['10.1/x', '10.2/y']"`` into a unique key — worse than the drop it
# replaced, because it is permanent and defeats dedup. Unwrap instead.
_IDENTIFIER_FIELDS = ("doi", "pmid", "pmcid", "arxiv_id")


# The two fields whose contents ``_parse_authors_list`` turns into author
# records. Only inside these does an empty name key mean "junk author".
_AUTHOR_LIST_KEYS = ("authors", "authors_csl")

# Author fields whose emptiness produces a junk author record.
# ``name``/``display_name`` are the two ``citation_normalizer._parse_name``
# strips — ``display_name`` is the OpenAlex shape, missing from an earlier
# spelling of this tuple. ``family``/``given``/``suffix`` are copied
# verbatim by ``_parse_authors_list``'s CSL branch, which tests only
# ``"family" in author``, so an empty one of those is embedded in the
# stored csl_json rather than ignored. ``literal`` is deliberately absent:
# ``_parse_name`` only ever EMITS it, never reads it from input, and an
# earlier spelling carried it (plus two more) as keys that did nothing.
_AUTHOR_NAME_KEYS = ("name", "display_name", "family", "given", "suffix")


# Where the walk currently is, relative to an author record. A bool was
# not enough: it could say "inside authors" but not "inside an author
# record's OWN keys", so once set it stayed set for every descendant and
# deleted an empty ``given`` from ``authors[0].affiliation`` or an
# OpenAlex ``institutions[].display_name`` — neither of which
# ``_parse_authors_list`` ever reads.
_AUTHORS_OUTSIDE = 0
_AUTHORS_LIST = 1  # this value IS an authors/authors_csl list
_AUTHORS_RECORD = 2  # this value IS one author record


def _json_text_safe(
    value: object, _depth: int = 0, _authors: int = _AUTHORS_OUTSIDE
) -> object:
    """Recursively make *value* safe to serialize and to read as text.

    The ingest boundary is the TREE, not the top-level dict. ``metadata``
    and ``authors`` come from the same untrusted producer, are read by the
    same ``normalize_citation`` call, and are serialized into the same JSON
    column — so a ``set`` under ``metadata["journal"]`` or an ``int`` under
    ``authors[0]["name"]`` raises exactly where a top-level one used to.

    ``_authors`` scopes the empty-name drop to an author record's OWN
    keys, which are the only ones ``_parse_authors_list`` reads. Anything
    nested below them is inert data the producer may want preserved.
    """
    if _depth > 6:
        return str(value)
    if value is None or isinstance(value, (str, int, float, bool)):
        return value
    if isinstance(value, dict):
        in_record = _authors == _AUTHORS_RECORD
        # Author name fields are coerced ONCE and reused. Calling
        # ``_as_text`` separately in the filter and the value expression
        # meant a non-deterministic ``__str__`` could pass the filter on
        # one call and write "" from the next.
        coerced = (
            {
                str(k): _as_text(v)
                for k, v in value.items()
                if k in _AUTHOR_NAME_KEYS
            }
            if in_record
            else {}
        )
        return {
            str(k): (
                coerced[str(k)]
                if in_record and k in _AUTHOR_NAME_KEYS
                # Descending out of the record: re-arm only on another
                # authors key, never inherit.
                else _json_text_safe(
                    v,
                    _depth + 1,
                    _AUTHORS_LIST
                    if k in _AUTHOR_LIST_KEYS
                    else _AUTHORS_OUTSIDE,
                )
            )
            for k, v in value.items()
            # An author name that coerces to nothing is DROPPED, not kept.
            # ``_parse_name(None)`` raises and ``_parse_name("")``
            # synthesises ``{"literal": ""}``; the CSL branch of
            # ``_parse_authors_list`` copies ``family``/``given``/``suffix``
            # verbatim, so an empty one of those lands as a junk author in
            # the stored csl_json just the same. ``.strip()`` because
            # ``_parse_name`` strips before its own emptiness test, so a
            # whitespace-only name produces the same junk.
            if not (
                in_record
                and k in _AUTHOR_NAME_KEYS
                and not (coerced.get(str(k)) or "").strip()
            )
        }
    if isinstance(value, (list, tuple)):
        # The ELEMENTS of an authors list are the author records.
        child = (
            _AUTHORS_RECORD if _authors == _AUTHORS_LIST else _AUTHORS_OUTSIDE
        )
        return [_json_text_safe(v, _depth + 1, child) for v in value]
    return str(value)


# SQLite's INTEGER column is 64-bit. Python's int is not, so a value can
# pass ``int()`` inside citation_normalizer and still raise OverflowError
# at flush() — and because that is not an IntegrityError, the per-source
# SAVEPOINT retry does not apply and the Session is left rolled back, so
# every LATER source in the batch is lost too and nothing commits.
_SQLITE_INT_MIN = -(2**63)
_SQLITE_INT_MAX = 2**63 - 1


def _coerce_year(value: object) -> object:
    """Drop a year that cannot be stored, rather than losing the batch."""
    if value is None or isinstance(value, bool):
        return value
    # Duck-typed on the operation, not on a type list. ``_parse_date`` does
    # ``int(raw_year)``, which accepts anything with ``__int__`` — numpy
    # scalars, Decimal, Fraction, even UUID — and this file's own comments
    # name numpy as an expected producer. Gating on ``(int, float, str)``
    # let every one of those past the bound and into flush().
    try:
        as_int = int(float(value) if isinstance(value, float) else value)
    except (ValueError, OverflowError, TypeError, ArithmeticError):
        # Not int()-able, so ``_parse_date`` cannot overflow on it either:
        # leave strings for its regex, drop anything else.
        return value if isinstance(value, str) else None
    return value if _SQLITE_INT_MIN <= as_int <= _SQLITE_INT_MAX else None


def _coerce_identifier(value: object) -> object:
    """Return *value* fit for a ``unique`` identifier column, else ``None``.

    Unwrap-or-DROP, never unwrap-or-stringify. These feed ``Paper.doi``,
    ``.pmid`` and ``.arxiv_id`` and the dedup SELECT, where a repr is worse
    than absence twice over: it is permanent, and two DIFFERENT papers
    carrying the same unparseable value match each other and collapse into
    one row. Absence just means "this engine gave us no identifier", which
    ``_extract_doi`` already handles by falling through to its next
    channel.

    A list is unwrapped once and its element re-checked, so ``[["10.1/x"]]``
    drops rather than stringifying the inner list.
    """
    if isinstance(value, (list, tuple)):
        value = value[0] if value else None
    if value is None or isinstance(value, str):
        return value or None
    # Integral-ness, not ``isinstance(int)``: a PMID legitimately arrives
    # as an int, and numpy.int64 / Decimal("12345") are integers that are
    # NOT int subclasses. Rejecting them dropped real identifiers and
    # broke dedup — the same harm as stringifying, in the other direction.
    # ``str()`` on a large int is exact, so there is nothing to protect
    # against here.
    # A numpy boolean is neither a Python ``bool`` nor a
    # ``numbers.Real``/``Integral``, so it slips both gates while
    # ``int()``-ing to 1 and comparing equal to it — a True in a pmid
    # field was stored as the identifier "1".
    #
    # Matched on the dtype kind rather than the type name: numpy 1.x calls
    # the scalar ``bool_`` and numpy 2.x calls it ``bool``, so a name check
    # written against either version silently misses the other.
    if isinstance(value, bool):
        return None
    try:
        # ``getattr`` only swallows a MISSING attribute; a ``.kind``
        # property that raises would propagate out of a coercion helper
        # that every other step in this function keeps total.
        if getattr(getattr(value, "dtype", None), "kind", None) == "b":
            return None
    except Exception:
        return None
    # ``numbers.Real`` rather than ``isinstance(value, float)``: numpy's
    # float32 is not a float subclass, so an exactly-integral one slipped
    # through as an identifier. Engines send ints and strings, never reals.
    if isinstance(value, numbers.Real) and not isinstance(
        value, numbers.Integral
    ):
        return None
    try:
        as_int = int(value)
    except (ValueError, OverflowError, TypeError, ArithmeticError):
        return None
    # Exactness. ``numbers.Integral`` first, because ``as_int != value``
    # relies on a symmetric ``__eq__``: numpy ints and Decimal have one,
    # so both are accepted. An object that defines ``__int__`` but neither
    # registers as Integral nor compares equal to its own integer is
    # DROPPED — deliberately. This feeds a unique column, and a value that
    # cannot be shown equal to the integer it claims to be is not one to
    # guess at. So this is int()-ability plus provable exactness, not
    # int()-ability alone.
    if not isinstance(value, numbers.Integral) and as_int != value:
        return None
    return str(as_int) if as_int else None


def normalize_source_fields(source: dict) -> dict:
    """Return a copy of *source* with its text fields coerced to ``str``.

    Engine dicts reach this service from arbitrary producers — a LangChain
    retriever's ``Document.metadata`` is whatever the user put there — and
    a non-str reaching a slice, a dict key, a regex or a Text column raises
    inside the per-source ``except``, which rolls back and drops the whole
    citation with only a counter to show for it.

    A falsy non-str becomes ``""``, not its repr: ``{"link": []}`` should
    keep skipping the source, as it did before any coercion existed, rather
    than persisting the string ``"[]"`` and rendering it as a link.

    Typed fields are left alone — ``authors`` stays a list, ``year`` an
    int, ``score`` a float — and only a non-dict ``metadata`` is replaced,
    since callers index into it.
    """
    normalized = dict(source)
    for field in _TEXT_FIELDS:
        value = normalized.get(field)
        if value is None or isinstance(value, str):
            continue
        normalized[field] = _as_text(value) if value else ""
    for field in ("year", "publication_year"):
        if field in normalized:
            normalized[field] = _coerce_year(normalized[field])

    for field in _IDENTIFIER_FIELDS:
        # ``in``, not ``.get``: materialising ``doi: None`` on every plain
        # web result changes what ``original_data`` stores for rows that
        # never had an academic identity.
        if field not in normalized:
            continue
        normalized[field] = _coerce_identifier(normalized[field])

    if "metadata" in normalized:
        metadata = normalized.get("metadata")
        normalized["metadata"] = (
            _json_text_safe(metadata) if isinstance(metadata, dict) else {}
        )

    # D2: ``authors_csl`` is read BEFORE ``authors`` by
    # ``normalize_citation``, in the same expression, and is NASA ADS's
    # primary author channel — normalizing only one of the two left the
    # other reaching the same json.dumps.
    for field in _AUTHOR_LIST_KEYS:
        value = normalized.get(field)
        if isinstance(value, (list, tuple)):
            normalized[field] = _json_text_safe(
                list(value), _authors=_AUTHORS_LIST
            )

    # D1: ``external_ids`` is the second DOI source ``_extract_doi``
    # documents. Leaving it raw put a list DOI into the unique Paper.doi
    # by the very channel the identifier unwrap above exists to protect.
    for field in ("external_ids", "externalIds"):
        value = normalized.get(field)
        if value is not None:
            normalized[field] = (
                _json_text_safe(value) if isinstance(value, dict) else {}
            )
            for key in ("DOI", "doi"):
                if key in normalized[field]:
                    normalized[field][key] = _coerce_identifier(
                        normalized[field][key]
                    )
    return normalized


def select_source_url(source: dict) -> object:
    """Choose the URL to persist for *source*.

    Extracted so the test suite can exercise THIS function. It previously
    lived inline and was pinned by a test that reimplemented it; the mirror
    stayed faithful right up until it faithfully mirrored a bug, and went
    green while the hole was open.

    Fails CLOSED on a non-str url: an earlier version made the isinstance
    check one conjunct of the rejection condition, so a non-str url made
    the whole ``and``-chain false, the ownership check never ran, and a
    recorded anchor naming a DIFFERENT document was persisted unchecked.
    """
    own_url = source.get("url", "") or source.get("link", "")
    own_key = canonical_url_key(own_url) if isinstance(own_url, str) else None

    raw_key = source.get(CHUNK_DISPLAY_KEY)
    recorded = (
        preferred_chunk_display(raw_key) if isinstance(raw_key, str) else None
    )
    # No usable key for this entry means no way to prove ownership, so the
    # recorded anchor is refused rather than trusted.
    if recorded and (own_key is None or canonical_url_key(recorded) != own_key):
        recorded = None

    own_anchor = (
        preferred_chunk_display(own_url) if isinstance(own_url, str) else None
    )
    # Normalise before falling back to the raw string. Without this the
    # absolute alias is persisted as typed, and the ``:443``/userinfo
    # spellings — which the alias parser accepts as the same document but
    # ``library_resolver`` refuses, since it compares netloc exactly —
    # become dead links in ``research_resources.url``. Returns None for a
    # non-library URL, so external sources keep their own spelling.
    normalised = (
        library_display_url(own_url) if isinstance(own_url, str) else None
    )
    return own_anchor or recorded or normalised or own_url


class ResearchSourcesService:
    """Service for managing research sources in the database."""

    @staticmethod
    def save_research_sources(
        research_id: str,
        sources: List[Dict[str, Any]],
        username: Optional[str] = None,
    ) -> int:
        """
        Save sources from research to the ResearchResource table.

        Args:
            research_id: The UUID of the research
            sources: List of source dictionaries with url, title, snippet, etc.
            username: Username for database access

        Returns:
            Number of sources saved
        """
        if not sources:
            logger.info(f"No sources to save for research {research_id}")
            return 0

        saved_count = 0
        # Failed-source counter. The per-source try/except below catches
        # broad exceptions to keep one bad source from killing the batch,
        # but without this counter the caller had no way to distinguish
        # "all N saved" from "some silently dropped". Emitted in the
        # final log line so admins can spot save-failure trends.
        failed_count = 0

        try:
            with get_user_db_session(username) as db_session:
                # First check if resources already exist for this research
                existing = (
                    db_session.query(ResearchResource)
                    .filter_by(research_id=research_id)
                    .count()
                )

                if existing > 0:
                    logger.info(
                        f"Research {research_id} already has {existing} resources, skipping save"
                    )
                    return int(existing)

                # Save each source as a ResearchResource.
                # Each source runs inside a SAVEPOINT so a per-source
                # failure can be rolled back cleanly without losing
                # any previously saved sources in this batch.
                # Per-batch memoization: container_title → journal_id
                # avoids redundant Journal lookups when multiple sources
                # share the same venue (common in topic-focused searches).
                journal_id_cache: Dict[Optional[str], Optional[int]] = {}
                for source in sources:
                    sp = None
                    try:
                        # Once, before anything reads it — see
                        # normalize_source_fields.
                        source = normalize_source_fields(source)
                        # Extract fields from various possible formats
                        # A chunk-anchored spelling recorded by the
                        # collector wins. After canonical-key dedup only
                        # one entry per library document survives, and it
                        # may be the anchor-less view — persisting that
                        # loses the #chunk-<n> permanently, since this row
                        # is what later renders the source.
                        # Validated, not trusted: this value is written
                        # to the database and later rendered as a link, so
                        # an unchecked read would let whatever set the key
                        # choose the stored URL.
                        # ``_as_text`` here, not just at the slices: these
                        # three reach ``Text`` columns, and a list or dict
                        # raises at ``flush()`` rather than at the read —
                        # so guarding the read alone only moved the
                        # exception, and the citation was dropped just the
                        # same.
                        url = _as_text(select_source_url(source))
                        title = _as_text(
                            source.get("title", "") or source.get("name", "")
                        )
                        # Coerced, not assumed: these come from the same
                        # stored dict as the url, and a non-str raises
                        # inside the per-source ``except`` below — which
                        # drops the whole citation silently.
                        snippet = _as_text(
                            source.get("snippet", "")
                            or source.get("content_preview", "")
                            or source.get("description", "")
                        )
                        source_type = _as_text(source.get("source_type", "web"))

                        # Skip if no URL
                        if not url:
                            continue

                        # Start savepoint for this source — any rollback
                        # inside this block (including the IntegrityError
                        # retry path below) only affects this source.
                        sp = db_session.begin_nested()

                        # Create resource record.
                        # Sanitize the source dict before embedding it in
                        # resource_metadata — raw engine dicts can contain
                        # non-JSON-serializable values (nested objects,
                        # numpy types, affiliation sub-dicts, etc.) which
                        # would crash json.dumps() at flush time.
                        safe_source = _json_safe(source)
                        resource = ResearchResource(
                            research_id=research_id,
                            title=title or "Untitled",
                            url=url,
                            content_preview=snippet[:1000]
                            if snippet
                            else None,  # Limit preview length
                            source_type=source_type,
                            resource_metadata={
                                "added_at": datetime.now(UTC).isoformat(),
                                "original_data": safe_source,
                            },
                            created_at=datetime.now(UTC).isoformat(),
                        )

                        db_session.add(resource)
                        db_session.flush()  # Get resource.id for FK

                        # Create or reuse Paper for academic sources
                        citation_fields = normalize_citation(source)
                        if citation_fields:
                            source_engine = citation_fields.pop(
                                "source_engine", None
                            )
                            # Try to link to existing Journal record
                            # (container_title stays in citation_fields so
                            # it ends up in the metadata blob for citation
                            # export). Memoized per batch to avoid repeat
                            # lookups for the same venue.
                            ct = citation_fields.get("container_title")
                            # Coerced before use as a dict key: CSL
                            # ``container-title`` is legitimately an array,
                            # and an unhashable value raises here — 34
                            # lines before the guard on the same value.
                            ct = _as_text(ct) or None
                            if ct in journal_id_cache:
                                journal_id = journal_id_cache[ct]
                            else:
                                journal_id = _resolve_journal_id(db_session, ct)
                                journal_id_cache[ct] = journal_id

                            # Separate indexed columns from metadata blob.
                            # Only doi/arxiv_id/pmid/journal_id/
                            # container_title/year are real columns on
                            # Paper; everything else is bundled into
                            # the metadata JSON blob. Quality is NOT
                            # stored per-Paper — the dashboard resolves
                            # it live (Tier 4: journals.quality via
                            # container_title lookup; Tier 1-3: bundled
                            # reference DB) so a re-scored journal
                            # propagates automatically.
                            # container_title: prefer the filter's
                            # cleaned matched name (what actually keyed
                            # the successful score); fall back to the raw
                            # CSL container_title if the filter didn't
                            # run (e.g. journal_reputation disabled).
                            #
                            # .pop() removes it from citation_fields so it
                            # doesn't end up duplicated in paper_metadata
                            # JSON. The Paper column is the sole source
                            # of truth; CSL-JSON export already captured
                            # the raw value inside citation_fields[
                            # "csl_json"] during normalize_citation.
                            ct_raw = citation_fields.pop(
                                "container_title", None
                            )
                            ct_matched = (
                                source.get("journal_name_matched") or ct_raw
                            )
                            # ``or None`` like its sibling above: an empty
                            # container_title must stay NULL, or every
                            # venue-less paper forms an empty-string group
                            # in the journal metrics instead of being
                            # excluded by ``isnot(None)``.
                            ct_matched = _as_text(ct_matched) or None
                            if ct_matched and len(ct_matched) > 500:
                                logger.debug(
                                    f"Truncating container_title to 500 "
                                    f"chars: {ct_matched[:80]}..."
                                )
                                ct_matched = ct_matched[:500]
                            # `year` intentionally stays in citation_fields
                            # (JSON blob) AND is copied to the indexed column.
                            # The JSON blob remains the CSL-JSON source of
                            # truth; the column is a denormalized index
                            # surface for dashboard year queries.
                            indexed = {
                                "doi": citation_fields.pop("doi", None),
                                "arxiv_id": citation_fields.pop(
                                    "arxiv_id", None
                                ),
                                "pmid": citation_fields.pop("pmid", None),
                                "journal_id": journal_id,
                                "container_title": ct_matched,
                                "year": citation_fields.get("year"),
                            }

                            # Dedup: find existing paper by DOI/arxiv/pmid.
                            # The UNIQUE constraints on doi/arxiv_id/pmid
                            # prevent duplicates from concurrent writers,
                            # but we still need to handle the race where
                            # our SELECT missed and another writer's
                            # INSERT succeeds first — catch IntegrityError
                            # and re-query.
                            paper = _find_existing_paper(db_session, indexed)
                            if paper is not None:
                                _merge_identifiers(
                                    paper, indexed, citation_fields
                                )
                            else:
                                paper = Paper(
                                    **indexed,
                                    paper_metadata=citation_fields or None,
                                )
                                db_session.add(paper)
                                try:
                                    db_session.flush()
                                except IntegrityError:
                                    # Concurrent writer inserted same
                                    # paper. Roll back this SAVEPOINT
                                    # only (not the whole batch), then
                                    # restart a nested one and re-fetch
                                    # the existing row for merging.
                                    sp.rollback()
                                    sp = db_session.begin_nested()
                                    # After savepoint rollback we also
                                    # need to re-create the resource
                                    # since its flush was undone.
                                    resource = ResearchResource(
                                        research_id=research_id,
                                        title=title or "Untitled",
                                        url=url,
                                        content_preview=snippet[:1000]
                                        if snippet
                                        else None,
                                        source_type=source_type,
                                        resource_metadata={
                                            "added_at": datetime.now(
                                                UTC
                                            ).isoformat(),
                                            "original_data": safe_source,
                                        },
                                        created_at=datetime.now(
                                            UTC
                                        ).isoformat(),
                                    )
                                    db_session.add(resource)
                                    db_session.flush()
                                    paper = _find_existing_paper(
                                        db_session, indexed
                                    )
                                    if paper is None:
                                        # Truly unexpected — concurrent
                                        # writer's row is gone.
                                        raise
                                    _merge_identifiers(
                                        paper, indexed, citation_fields
                                    )

                            # Link paper to this resource
                            appearance = PaperAppearance(
                                paper_id=paper.id,
                                resource_id=resource.id,
                                source_engine=source_engine,
                            )
                            db_session.add(appearance)

                        # Commit the savepoint so this source's writes
                        # persist even if a later source fails.
                        sp.commit()
                        saved_count += 1

                    except Exception:
                        # Roll back just this source's savepoint; earlier
                        # sources in the batch stay committed at the
                        # outer transaction level.
                        #
                        # Unconditional, NOT gated on ``sp.is_active``: a
                        # failure inside flush() deactivates the savepoint,
                        # so the guard skipped the rollback in exactly the
                        # case that needs it. The Session then stayed in
                        # its failed state, every later source raised
                        # PendingRollbackError, and the final commit()
                        # raised out of the function — one bad source lost
                        # the whole batch and the caller got an exception
                        # instead of a reduced count.
                        if sp is not None:
                            try:
                                sp.rollback()
                            except Exception:
                                # Already unwound; nothing further to undo
                                # for this source.
                                logger.debug(
                                    "savepoint rollback was already done"
                                )
                        failed_count += 1
                        # ``_as_text``: the only unguarded read of url
                        # left, and it runs when something has ALREADY
                        # failed — an exception raised here escapes the
                        # handler and loses the sibling sources too.
                        logger.exception(
                            "Failed to save source "
                            + _as_text(source.get("url", "unknown"))
                        )
                        continue

                # Commit all resources
                if saved_count > 0:
                    db_session.commit()
                    if failed_count > 0:
                        logger.warning(
                            f"Saved {saved_count} sources for research "
                            f"{research_id} — {failed_count} source(s) "
                            f"failed and were skipped (see earlier "
                            f"ERROR logs for per-source stack traces)"
                        )
                    else:
                        logger.info(
                            f"Saved {saved_count} sources for research {research_id}"
                        )
                elif failed_count > 0:
                    logger.warning(
                        f"No sources saved for research {research_id} — "
                        f"all {failed_count} sources in the batch failed "
                        f"(see earlier ERROR logs for per-source stack "
                        f"traces)"
                    )

        except Exception:
            logger.exception("Error saving research sources")
            raise

        return saved_count

    @staticmethod
    def get_research_sources(
        research_id: str, username: Optional[str] = None
    ) -> List[Dict[str, Any]]:
        """
        Get all sources for a research from the database.

        Args:
            research_id: The UUID of the research
            username: Username for database access

        Returns:
            List of source dictionaries
        """
        sources = []

        try:
            with get_user_db_session(username) as db_session:
                resources = (
                    db_session.query(ResearchResource)
                    .filter_by(research_id=research_id)
                    .order_by(ResearchResource.id.asc())
                    .all()
                )

                for resource in resources:
                    sources.append(
                        {
                            "id": resource.id,
                            "url": resource.url,
                            "title": resource.title,
                            "snippet": resource.content_preview,
                            "content_preview": resource.content_preview,
                            "source_type": resource.source_type,
                            "metadata": resource.resource_metadata or {},
                            "created_at": resource.created_at,
                        }
                    )

                logger.info(
                    f"Retrieved {len(sources)} sources for research {research_id}"
                )

        except Exception:
            logger.exception("Error retrieving research sources")
            raise

        return sources

    @staticmethod
    def update_research_with_sources(
        research_id: str,
        all_links_of_system: List[Dict[str, Any]],
        username: Optional[str] = None,
    ) -> bool:
        """
        Update a completed research with its sources.
        This should be called when research completes.

        Args:
            research_id: The UUID of the research
            all_links_of_system: List of all sources found during research
            username: Username for database access

        Returns:
            True if successful
        """
        try:
            # Save sources to ResearchResource table
            saved_count = ResearchSourcesService.save_research_sources(
                research_id, all_links_of_system, username
            )

            # Also update the research metadata to include source count
            with get_user_db_session(username) as db_session:
                research = (
                    db_session.query(ResearchHistory)
                    .filter_by(id=research_id)
                    .first()
                )

                if research:
                    if not research.research_meta:
                        research.research_meta = {}

                    # Update metadata with source information
                    research.research_meta["sources_count"] = saved_count
                    research.research_meta["has_sources"] = saved_count > 0

                    db_session.commit()
                    logger.info(
                        f"Updated research {research_id} with {saved_count} sources"
                    )
                    return True
                logger.warning(
                    f"Research {research_id} not found for source update"
                )
                return False

        except Exception:
            logger.exception("Error updating research with sources")
            return False


def _json_safe(value: Any, _depth: int = 0, _seen: Optional[set] = None) -> Any:
    """Recursively coerce a value into a JSON-serializable form.

    Used before embedding arbitrary engine result dicts into JSON
    columns. Non-primitive values (datetime, date, set, tuple,
    custom objects) are converted to strings or dropped. This is a
    last-resort sanitizer — callers should still prefer structured
    whitelisting (e.g., Paper.paper_metadata only stores known CSL
    fields).

    Depth limit and cycle detection prevent RecursionError on
    pathological input (circular dict/list references).
    """
    # Depth limit as a belt-and-braces guard
    if _depth > 32:
        return str(value)

    # JSON primitives pass through unchanged
    if value is None or isinstance(value, (bool, int, float, str)):
        return value

    # Container cycle detection via id() tracking
    if isinstance(value, (dict, list, tuple, set, frozenset)):
        if _seen is None:
            _seen = set()
        if id(value) in _seen:
            return "<circular>"
        _seen = _seen | {id(value)}

    if isinstance(value, dict):
        return {
            str(k): _json_safe(v, _depth + 1, _seen)
            for k, v in value.items()
            if isinstance(k, (str, int, float, bool))
        }
    if isinstance(value, (list, tuple, set, frozenset)):
        return [_json_safe(v, _depth + 1, _seen) for v in value]
    # datetime/date and anything else: coerce to string
    return str(value)


def _resolve_journal_id(
    db_session: Session, container_title: Optional[str]
) -> Optional[int]:
    """Look up a Journal record by name. Returns journal.id or None.

    The journal reputation filter writes Journal rows using the
    cleaned journal name as returned by its regex cleanup (NFKC-
    normalized, whitespace-stripped, but NOT lowercased). We match
    against that by applying the same NFKC+strip normalization here
    and using a case-insensitive comparison so mismatched capitalization
    in the container_title doesn't break the lookup.
    """
    if not container_title:
        return None
    import unicodedata

    name_norm = unicodedata.normalize("NFKC", container_title).strip()
    # Query name_lower, not func.lower(name): expression-wrapping the
    # indexed column forces a full scan.
    row = (
        db_session.query(Journal.id)
        .filter(Journal.name_lower == name_norm.lower())
        .first()
    )
    return row[0] if row else None


def _find_existing_paper(
    db_session: Session, fields: dict
) -> Optional["Paper"]:
    """Find an existing Paper by any of DOI, arXiv ID, or PMID.

    Issues a single OR-query across all provided identifiers so that a
    caller with multiple IDs doesn't miss dedup because the first one
    (e.g. DOI) is absent from the stored row but a later one (e.g.
    arXiv) would have matched. The previous waterfall short-circuited
    on the first non-null input and never tried the remaining IDs.

    Uses load_only to skip the ``paper_metadata`` JSON blob on the
    dedup lookup — we only need the identifier columns. The blob is
    lazy-loaded if the caller later touches ``paper.paper_metadata``.
    """
    id_only = load_only(
        Paper.id,
        Paper.doi,
        Paper.arxiv_id,
        Paper.pmid,
        Paper.journal_id,
    )

    conditions = []
    doi = fields.get("doi")
    arxiv_id = fields.get("arxiv_id")
    pmid = fields.get("pmid")
    if doi:
        conditions.append(Paper.doi == doi)
    if arxiv_id:
        conditions.append(Paper.arxiv_id == arxiv_id)
    if pmid:
        conditions.append(Paper.pmid == pmid)

    if not conditions:
        return None

    matches = (
        db_session.query(Paper).options(id_only).filter(or_(*conditions)).all()
    )

    if not matches:
        return None
    if len(matches) == 1:
        return matches[0]

    # Multiple distinct rows matched different identifiers of the same
    # incoming record. This indicates a prior mismerge; deterministic
    # tie-break on oldest (lowest) id so repeat runs don't oscillate.
    winner = min(matches, key=lambda p: p.id)
    logger.warning(
        f"Paper dedup conflict on {sorted(k for k in ('doi', 'arxiv_id', 'pmid') if fields.get(k))}: "
        f"{len(matches)} rows (ids {sorted(m.id for m in matches)}); "
        f"using id {winner.id}. Manual review recommended."
    )
    return winner


def _merge_identifiers(paper: "Paper", indexed: dict, metadata: dict) -> None:
    """Enrich an existing Paper with identifiers from a new encounter.

    E.g., an ArXiv paper later found via OpenAlex gains a DOI.

    Args:
        paper: The existing Paper row to enrich.
        indexed: New values for the real columns (doi, arxiv_id,
            pmid, journal_id). Only applied if the column is
            currently empty.
        metadata: Additional bibliographic fields (pmcid, authors,
            csl_json, etc.) to merge into paper.paper_metadata. Only
            keys that aren't already present in the existing blob
            are added — first write wins, to preserve the original
            enrichment.
    """
    # Indexed columns — first write wins. Avoids churning rows when
    # the same paper turns up across many research sessions with
    # slightly different scoring / cleaned names.
    if indexed.get("doi") and not paper.doi:
        paper.doi = indexed["doi"]
    if indexed.get("arxiv_id") and not paper.arxiv_id:
        paper.arxiv_id = indexed["arxiv_id"]
    if indexed.get("pmid") and not paper.pmid:
        paper.pmid = indexed["pmid"]
    if indexed.get("journal_id") and not paper.journal_id:
        paper.journal_id = indexed["journal_id"]
    if indexed.get("container_title") and not paper.container_title:
        paper.container_title = indexed["container_title"]
    if indexed.get("year") is not None and paper.year is None:
        paper.year = indexed["year"]

    # Metadata blob — merge any missing keys.
    # IMPORTANT: we must build a NEW dict and reassign the attribute so
    # that SQLAlchemy's plain JSON column marks it dirty. In-place
    # mutation of the existing dict is not detected without
    # MutableDict.as_mutable() — which this column does not use, to
    # stay consistent with other JSON columns in the project.
    if metadata:
        existing = dict(paper.paper_metadata) if paper.paper_metadata else {}
        changed = False
        for key, value in metadata.items():
            if value is not None and key not in existing:
                existing[key] = value
                changed = True
        if changed:
            paper.paper_metadata = existing or None