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.

15.1 KB
Raw Blame
"""Hostile-snapshot record-count ceilings for the journal-quality datasets.

Threat model (the module's own — see ``db.py``'s ``_as_number``
docstring and ``_openalex_common.py``'s manifest-allowlist comment):
a compromised dataset origin. The manifest self-declares
``meta.content_length`` and ``meta.md5``, so the existing transport
checks pass by construction for a hostile origin, and the
decompressed-char caps permit ~23M minimal records per OpenAlex
partition (~183 chars each against the 4 GiB default cap) — which
accumulates into a multi-gigabyte in-process dict and OOM-kills the
server long before any *floor* ("suspiciously few records") fires.

These contracts pin the missing upper bound on *entry count*: every
fetcher refuses to accumulate entries past its ceiling, checked
*inside* the accumulation loop before each entry is stored — the
counting-reader tests assert that at most ceiling+1 rows are consumed
when the ceiling trips, so a check moved after the loop (or a reader
that materialises every row first) fails them. For institutions every
growing structure (ids, ROR index, name index including alternative
names) counts against a combined ceiling. Realistic fleet-sized
payloads continue to pass.

What these ceilings do NOT bound: the length of individual fields
(limited only by the OpenAlex per-line cap or the CSV body size), and
the whole-body buffering of the DOAJ, JabRef and predatory responses,
which are held in memory whole and limited only by safe_get's
transport size cap.
"""

from __future__ import annotations

import gzip
import hashlib
import io
import json
from unittest.mock import MagicMock

import pytest

from local_deep_research.journal_quality.data_sources import (
    doaj,
    jabref,
    openalex,
)
from local_deep_research.journal_quality.data_sources.base import (
    HostileSnapshotError,
)
from local_deep_research.journal_quality.data_sources import (
    institutions as inst_mod,
)
from local_deep_research.journal_quality.data_sources import (
    predatory as pred_mod,
)
from local_deep_research.security import safe_requests


def _gz_records(records: list[dict]) -> bytes:
    buf = io.BytesIO()
    with gzip.GzipFile(fileobj=buf, mode="wb") as gz:
        for rec in records:
            gz.write(json.dumps(rec, separators=(",", ":")).encode() + b"\n")
    return buf.getvalue()


def _response(content: bytes) -> MagicMock:
    resp = MagicMock()
    resp.raise_for_status = MagicMock()
    resp.content = content
    return resp


def _text_response(text: str) -> MagicMock:
    resp = MagicMock()
    resp.raise_for_status = MagicMock()
    resp.text = text
    resp.content = text.encode("utf-8")
    return resp


def _manifest_for(part: bytes, record_count: int) -> MagicMock:
    """Manifest whose single partition self-certifies (length + md5)."""
    manifest = {
        "files": [
            {
                "url": "s3://openalex/data/jsonl/x/part_0.gz",
                "meta": {
                    "content_length": len(part),
                    "md5": hashlib.md5(part, usedforsecurity=False).hexdigest(),
                    "record_count": record_count,
                },
            }
        ]
    }
    resp = MagicMock()
    resp.raise_for_status = MagicMock()
    resp.json = MagicMock(return_value=manifest)
    return resp


def _patch_safe_get(monkeypatch, responses: list[MagicMock]) -> dict:
    """Patch safe_get_with_retries; return the kwargs it was called with."""
    calls = {"kwargs": None}
    seq = list(responses)

    def fake(url, **kwargs):
        calls["kwargs"] = kwargs
        return seq.pop(0)

    monkeypatch.setattr(safe_requests, "safe_get_with_retries", fake)
    return calls


def _openalex_record(i: int) -> dict:
    return {
        "id": f"https://openalex.org/S{i:09d}",
        "display_name": f"Journal {i}",
        "type": "journal",
        "cited_by_count": 1,
        "summary_stats": {"h_index": 1, "2yr_mean_citedness": 0.1},
    }


def _counting_partitions(orig, counter: dict):
    """Wrap ``iter_partitions`` so every record pulled is counted."""

    def wrapped(*args, **kwargs):
        for idx, total, records in orig(*args, **kwargs):
            yield idx, total, _counted(records, counter)

    return wrapped


def _counted(rows, counter: dict):
    for row in rows:
        counter["rows"] += 1
        yield row


def _counting_factory(factory, counter: dict):
    """Wrap a csv reader factory so every parsed row is counted."""

    def make(*args, **kwargs):
        return _counted(factory(*args, **kwargs), counter)

    return make


class TestProductionCeilings:
    def test_production_ceiling_values(self):
        """The behavioural tests below monkeypatch small ceilings; pin
        the shipped values here so they cannot silently drift."""
        assert openalex._MAX_OPENALEX_SOURCES == 1_000_000
        assert inst_mod._MAX_INSTITUTIONS == 500_000
        assert inst_mod._MAX_INSTITUTION_INDEX_ENTRIES == 2_500_000
        assert doaj._MAX_DOAJ_JOURNALS == 200_000
        assert jabref._MAX_JABREF_ABBREVS == 500_000
        assert pred_mod._MAX_PREDATORY_PER_LIST == 50_000


class TestOpenAlexSourcesCeiling:
    def test_refuses_past_the_record_ceiling(self, tmp_path, monkeypatch):
        """A hostile partition carrying more records than the ceiling is
        refused after at most ceiling+1 records are consumed.

        Every existing control passes for this payload: the s3://openalex
        allowlist, the self-declared content_length and md5, the schema
        fields, and the decompressed-char caps. Only the record-count
        ceiling stops the accumulation.
        """
        monkeypatch.setattr(openalex, "_MAX_OPENALEX_SOURCES", 100)
        counter = {"rows": 0}
        monkeypatch.setattr(
            openalex,
            "iter_partitions",
            _counting_partitions(openalex.iter_partitions, counter),
        )
        records = [_openalex_record(i) for i in range(500)]
        part = _gz_records(records)
        _patch_safe_get(
            monkeypatch,
            [_manifest_for(part, len(records)), _response(part)],
        )
        with pytest.raises(HostileSnapshotError, match="suspiciously many"):
            openalex.OpenAlexSource().fetch(tmp_path)
        assert counter["rows"] <= 101

    def test_fleet_sized_snapshot_still_passes(self, tmp_path, monkeypatch):
        """A realistic snapshot (above the 10K floor, far below the
        ceiling) is accepted unchanged."""
        records = [_openalex_record(i) for i in range(10_000)]
        part = _gz_records(records)
        _patch_safe_get(
            monkeypatch,
            [_manifest_for(part, len(records)), _response(part)],
        )
        count = openalex.OpenAlexSource().fetch(tmp_path)
        assert count == 10_000
        assert (tmp_path / "openalex_sources.json.gz").exists()


class TestInstitutionsCeiling:
    @staticmethod
    def _serve(monkeypatch, records: list[dict]) -> None:
        part = _gz_records(records)
        _patch_safe_get(
            monkeypatch,
            [_manifest_for(part, len(records)), _response(part)],
        )

    def test_refuses_past_the_record_ceiling(self, tmp_path, monkeypatch):
        monkeypatch.setattr(inst_mod, "_MAX_INSTITUTIONS", 100)
        counter = {"rows": 0}
        monkeypatch.setattr(
            inst_mod,
            "iter_partitions",
            _counting_partitions(inst_mod.iter_partitions, counter),
        )
        self._serve(
            monkeypatch,
            [
                {
                    "id": f"https://openalex.org/I{i}",
                    "display_name": f"Inst {i}",
                }
                for i in range(500)
            ],
        )
        with pytest.raises(HostileSnapshotError, match="suspiciously many"):
            inst_mod.InstitutionSource().fetch(tmp_path)
        assert counter["rows"] <= 101

    def test_repeated_id_with_fresh_indexes_is_refused(
        self, tmp_path, monkeypatch
    ):
        """One id repeated with a fresh ror/display_name each time never
        grows ``institutions`` but grows both indexes; the combined
        index-entry ceiling refuses it."""
        monkeypatch.setattr(inst_mod, "_MAX_INSTITUTION_INDEX_ENTRIES", 100)
        counter = {"rows": 0}
        monkeypatch.setattr(
            inst_mod,
            "iter_partitions",
            _counting_partitions(inst_mod.iter_partitions, counter),
        )
        self._serve(
            monkeypatch,
            [
                {
                    "id": "https://openalex.org/I1",
                    "ror": f"https://ror.org/0r{i:07d}",
                    "display_name": f"Inst {i}",
                }
                for i in range(1_000)
            ],
        )
        with pytest.raises(HostileSnapshotError, match="index entries"):
            inst_mod.InstitutionSource().fetch(tmp_path)
        # Two index entries per record: the ceiling trips after ~50.
        assert counter["rows"] <= 51

    def test_huge_alternatives_list_is_refused_mid_record(
        self, tmp_path, monkeypatch
    ):
        """A single record with a huge ``display_name_alternatives`` list
        is refused inside the alternatives loop, before the list is
        indexed in full."""
        monkeypatch.setattr(inst_mod, "_MAX_INSTITUTION_INDEX_ENTRIES", 100)
        calls = {"rows": 0}
        orig_normalize = inst_mod.normalize_name

        def counting_normalize(name):
            calls["rows"] += 1
            return orig_normalize(name)

        monkeypatch.setattr(inst_mod, "normalize_name", counting_normalize)
        self._serve(
            monkeypatch,
            [
                {
                    "id": "https://openalex.org/I1",
                    "display_name": "Inst",
                    "display_name_alternatives": [
                        f"Alt {i}" for i in range(10_000)
                    ],
                }
            ],
        )
        with pytest.raises(HostileSnapshotError, match="index entries"):
            inst_mod.InstitutionSource().fetch(tmp_path)
        # primary name + at most ceiling alternative names normalised.
        assert calls["rows"] <= 102

    def test_fleet_sized_snapshot_still_passes(self, tmp_path, monkeypatch):
        records = [
            {
                "id": f"https://openalex.org/I{i}",
                "ror": f"https://ror.org/0r{i:07d}",
                "display_name": f"Inst {i}",
                "display_name_alternatives": [f"Alt A {i}", f"Alt B {i}"],
            }
            for i in range(50_000)
        ]
        self._serve(monkeypatch, records)
        count = inst_mod.InstitutionSource().fetch(tmp_path)
        assert count == 50_000


class TestDOAJCeiling:
    @staticmethod
    def _csv(n: int) -> str:
        rows = ["Journal title,Journal ISSN (print version),Publisher"]
        # Unique 8-digit ISSN per row: journals are keyed by ISSN, so a
        # repeated ISSN would collapse rows and under-count.
        rows += [f"Journal {i},{i:08d},Pub {i}" for i in range(n)]
        return "\n".join(rows)

    def test_refuses_past_the_record_ceiling(self, tmp_path, monkeypatch):
        monkeypatch.setattr(doaj, "_MAX_DOAJ_JOURNALS", 100)
        counter = {"rows": 0}
        monkeypatch.setattr(
            doaj.csv,
            "DictReader",
            _counting_factory(doaj.csv.DictReader, counter),
        )
        _patch_safe_get(monkeypatch, [_text_response(self._csv(500))])
        with pytest.raises(HostileSnapshotError, match="suspiciously many"):
            doaj.DOAJSource().fetch(tmp_path)
        # Checked inside the loop: refused on row ceiling+1, not after
        # all 500 rows were accumulated.
        assert counter["rows"] <= 101

    def test_fleet_sized_csv_still_passes(self, tmp_path, monkeypatch):
        _patch_safe_get(monkeypatch, [_text_response(self._csv(5_000))])
        count = doaj.DOAJSource().fetch(tmp_path)
        assert count == 5_000


class TestJabRefCeiling:
    @staticmethod
    def _csv(n: int, tag: str = "f0") -> str:
        rows = ["Full Name,Abbreviation"]
        rows += [f"Journal Full {i} {tag},J Full {i} {tag}" for i in range(n)]
        return "\n".join(rows)

    def test_refuses_past_the_record_ceiling(self, tmp_path, monkeypatch):
        monkeypatch.setattr(jabref, "_MAX_JABREF_ABBREVS", 100)
        counter = {"rows": 0}
        monkeypatch.setattr(
            jabref.csv, "reader", _counting_factory(jabref.csv.reader, counter)
        )
        # The fetcher loops over all 14 upstream CSVs; serve the hostile
        # payload for the first and empty-but-valid CSVs for the rest.
        hostile = _text_response(self._csv(500))
        rest = [_text_response("Full Name,Abbreviation") for _ in range(13)]
        _patch_safe_get(monkeypatch, [hostile] + rest)
        with pytest.raises(HostileSnapshotError, match="suspiciously many"):
            jabref.JabRefSource().fetch(tmp_path)
        # The header row also parses as a pair, so the ceiling trips on
        # row ceiling+1 counting the header — never after all 501 rows.
        assert counter["rows"] <= 101

    def test_fleet_sized_csv_still_passes(self, tmp_path, monkeypatch):
        responses = [
            _text_response(self._csv(500, tag=f"f{f}")) for f in range(14)
        ]
        _patch_safe_get(monkeypatch, responses)
        count = jabref.JabRefSource().fetch(tmp_path)
        # 14 files x 500 unique abbreviations; the shared header row
        # ("Full Name,Abbreviation") parses as one extra pair.
        assert count == 500 * 14 + 1


class TestPredatoryCeiling:
    @staticmethod
    def _csv(n: int, name_col: str) -> str:
        rows = [f"{name_col},url"]
        rows += [
            f"Predatory {name_col} {i},https://evil.example/{i}"
            for i in range(n)
        ]
        return "\n".join(rows)

    def test_refuses_past_the_per_list_ceiling(self, tmp_path, monkeypatch):
        monkeypatch.setattr(pred_mod, "_MAX_PREDATORY_PER_LIST", 100)
        counter = {"rows": 0}
        monkeypatch.setattr(
            pred_mod.csv,
            "DictReader",
            _counting_factory(pred_mod.csv.DictReader, counter),
        )
        big_journals = self._csv(500, "name")
        ok_publishers = self._csv(10, "name")
        ok_hijacked = self._csv(10, "hijacked")
        _patch_safe_get(
            monkeypatch,
            [
                _text_response(ok_publishers),
                _text_response(big_journals),
                _text_response(ok_hijacked),
            ],
        )
        with pytest.raises(HostileSnapshotError, match="suspiciously many"):
            pred_mod.PredatorySource().fetch(tmp_path)
        # 10 publisher rows, then the journals list trips on row
        # ceiling+1: rows are parsed lazily and checked before storing,
        # so the remaining hostile rows are never materialised.
        assert counter["rows"] <= 10 + 101

    def test_fleet_sized_csvs_still_pass(self, tmp_path, monkeypatch):
        pubs = self._csv(400, "name")
        journals = self._csv(400, "name")
        hijacked = self._csv(400, "hijacked")
        _patch_safe_get(
            monkeypatch,
            [
                _text_response(pubs),
                _text_response(journals),
                _text_response(hijacked),
            ],
        )
        count = pred_mod.PredatorySource().fetch(tmp_path)
        assert count == 1_200