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.

42.2 KB
Raw Blame
"""Regression tests for issue #6560: Connection Pool Exhaustion in download_resource.

Validates that:
1. download_resource does NOT hold a database session during downloader.download_with_result.
2. Under bulk downloads, QueuePool connections are released across HTTP transfers so concurrent
   request handlers are not starved.
3. DownloadAttempt and document records are staged in a short transaction strictly AFTER download completion.
4. Failed downloads and network exceptions stage attempts without holding or leaking connections.
"""

import concurrent.futures
import threading
import time
import uuid
from contextlib import contextmanager
from unittest.mock import MagicMock, patch

import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import QueuePool

from local_deep_research.database.models import Base
from local_deep_research.database.models.download_tracker import (
    DownloadAttempt,
    DownloadTracker,
)
from local_deep_research.database.models.library import (
    Collection,
    Document,
    DocumentStatus,
    DownloadQueue,  # noqa: F401 - registers table on Base.metadata
    SourceType,
)
from local_deep_research.database.models.research import (
    ResearchHistory,
    ResearchResource,
)
from local_deep_research.research_library.services.download_service import (
    DownloadService,
)

MODULE = "local_deep_research.research_library.services.download_service"


@pytest.fixture
def service_stub():
    """DownloadService stub wired for testing download_resource."""
    with patch.object(DownloadService, "__init__", lambda self, *a, **kw: None):
        s = DownloadService.__new__(DownloadService)
        s.username = "test_user"
        s.password = "test_pass"
        s.library_root = "/tmp/ldr-test-library"
        s.legacy_library_root = None
        s._closed = False
        settings = MagicMock()
        settings.get_setting = lambda key, default=None: {
            "research_library.pdf_storage_mode": "database",
            "research_library.max_pdf_size_mb": 50,
        }.get(key, default)
        s.settings = settings
        s._check_url_against_policy = lambda url: (True, "ok")
        s.retry_manager = MagicMock()
        return s


def test_download_resource_does_not_hold_db_session_during_download(
    service_stub,
):
    """downloader.download_with_result must execute OUTSIDE any active database session.

    If a database session is held across download_with_result, this test asserts False.
    """
    events = []
    session_active = False

    @contextmanager
    def tracking_get_user_db_session(*args, **kwargs):
        nonlocal session_active
        session_active = True
        events.append("session_enter")
        mock_session = MagicMock()

        # Mock query return values
        mock_session.get.return_value = MagicMock(
            id=1,
            url="https://arxiv.org/abs/2401.0001",
            title="Test Paper",
            research_id="res-1",
            document_id=None,
        )
        mock_session.query.return_value.filter_by.return_value.first.return_value = None
        mock_session.query.return_value.filter_by.return_value.order_by.return_value.first.return_value = None

        try:
            yield mock_session
        finally:
            session_active = False
            events.append("session_exit")

    downloader = MagicMock()
    downloader.can_handle.return_value = True

    def mock_download(url, content_type):
        events.append("download_start")
        # CRITICAL ASSERTION: No database session may be active during network transfer
        assert not session_active, (
            "Database session was held open during downloader.download_with_result! "
            "This violates #6560 resolution plan."
        )
        events.append("download_end")
        return MagicMock(
            is_success=True,
            content=b"%PDF-1.4 mock content",
            status_code=200,
            skip_reason=None,
        )

    downloader.download_with_result.side_effect = mock_download
    service_stub.downloaders = [downloader]

    with (
        patch(
            f"{MODULE}.get_user_db_session",
            side_effect=tracking_get_user_db_session,
        ),
        patch(f"{MODULE}.PDFStorageManager") as mock_psm,
        patch(f"{MODULE}.get_source_type_id", return_value="src-1"),
        patch(f"{MODULE}.get_default_library_id", return_value="lib-1"),
    ):
        mock_psm.return_value.save_pdf.return_value = ("database", None)
        success, reason = service_stub.download_resource(1)
    assert success is True
    assert reason is None
    # Verify the lifecycle order:
    # 1. pre-checks session enters then exits
    # 2. download happens outside session
    # 3. post-download staging session enters and exits
    assert events.index("session_exit") < events.index("download_start")
    assert events.index("download_end") < len(events) - 1


def test_queue_pool_exhaustion_prevented_under_bounded_pool():
    """A bounded QueuePool (size=1) must not be exhausted while a slow download is in-flight.

    Simulates a multi-second HTTP transfer on worker 1, while worker 2 (FastAPI handler)
    checks out a session from the same pool. In the old code, worker 2 would time out (pool exhausted).
    With the fix, worker 2 succeeds immediately because worker 1 released its connection.
    """
    engine = create_engine(
        "sqlite:///:memory:",
        poolclass=QueuePool,
        pool_size=1,
        max_overflow=0,
        pool_timeout=1.0,  # 1 second timeout
        connect_args={"check_same_thread": False},
    )
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)

    # Seed required rows
    with Session() as seed_session:
        rh = ResearchHistory(
            id=str(uuid.uuid4()),
            query="q",
            mode="quick",
            status="completed",
            created_at="2026-05-09T00:00:00",
        )
        seed_session.add(rh)
        resource = ResearchResource(
            research_id=rh.id,
            title="Test Paper",
            url="https://example.com/slow_paper.pdf",
            source_type="academic",
            created_at="2026-05-09T00:00:00",
        )
        seed_session.add(resource)
        source_type = SourceType(
            id=str(uuid.uuid4()),
            name="research_download",
            display_name="Research Download",
        )
        seed_session.add(source_type)
        seed_session.commit()
        resource_id = resource.id
        source_type_id = source_type.id

    thread_local = threading.local()

    def get_thread_session():
        if not hasattr(thread_local, "session") or thread_local.session is None:
            thread_local.session = Session()
        return thread_local.session

    @contextmanager
    def production_get_user_db_session(*args, **kwargs):
        sess = get_thread_session()
        try:
            yield sess
        except Exception:
            sess.rollback()
            raise

    service = DownloadService.__new__(DownloadService)
    service.username = "test_user"
    service.password = "test_pass"
    service.library_root = "/tmp/ldr-test-library"
    service.legacy_library_root = None
    service._closed = False
    settings = MagicMock()
    settings.get_setting = lambda key, default=None: {
        "research_library.pdf_storage_mode": "database",
        "research_library.max_pdf_size_mb": 50,
    }.get(key, default)
    service.settings = settings
    service._check_url_against_policy = lambda url: (True, "ok")
    service.retry_manager = MagicMock()

    worker2_acquired_connection = False

    downloader = MagicMock()
    downloader.can_handle.return_value = True

    def slow_download(url, content_type):
        nonlocal worker2_acquired_connection
        # While download is in-flight on worker 1, simulate worker 2 (FastAPI request handler)
        # attempting to check out a database session from the pool.
        # Pool size is 1, so if worker 1 pinned the connection, worker 2 would fail with TimeoutError!
        try:
            with production_get_user_db_session() as w2_sess:
                count = w2_sess.query(ResearchResource).count()
                assert count == 1
                w2_sess.commit()
                worker2_acquired_connection = True
        except Exception as exc:
            worker2_acquired_connection = False
            raise AssertionError(
                f"Worker 2 starved by worker 1 download: {exc}"
            ) from exc

        return MagicMock(
            is_success=True,
            content=b"%PDF-1.4 downloaded data",
            status_code=200,
            skip_reason=None,
        )

    downloader.download_with_result.side_effect = slow_download
    service.downloaders = [downloader]

    with (
        patch(
            f"{MODULE}.get_user_db_session",
            side_effect=production_get_user_db_session,
        ),
        patch(f"{MODULE}.PDFStorageManager") as mock_psm,
        patch(f"{MODULE}.get_source_type_id", return_value=source_type_id),
        patch(f"{MODULE}.get_default_library_id", return_value="lib-1"),
    ):
        mock_psm.return_value.save_pdf.return_value = ("database", None)
        success, reason = service.download_resource(resource_id)

    assert success is True
    assert worker2_acquired_connection is True, (
        "Worker 2 was unable to acquire connection while worker 1 was downloading. "
        "Connection pool was exhausted."
    )
    # Assert item 2: expire_on_commit was not permanently mutated on the shared session
    assert get_thread_session().expire_on_commit is True

    # Verify attempt and document were properly persisted
    with Session() as verify_session:
        attempts = verify_session.query(DownloadAttempt).all()
        assert len(attempts) == 1
        assert attempts[0].succeeded is True
        assert attempts[0].bytes_downloaded == len(b"%PDF-1.4 downloaded data")
        docs = (
            verify_session.query(Document)
            .filter_by(resource_id=resource_id)
            .all()
        )
        assert len(docs) == 1
        assert docs[0].status == DocumentStatus.COMPLETED

    engine.dispose()


def test_queue_pool_exhaustion_prevented_on_retry_with_preexisting_tracker():
    """A pre-existing DownloadTracker (the retry/re-download path) must not pin the connection (#6564 item 1).

    Drives production session semantics (thread-local session reused without auto-commit or auto-close
    on context manager exit). Under a size-1 QueuePool, verifies that when tracker already exists,
    the pre-check transaction still releases its connection so concurrent requests are not starved.
    Also verifies session.expire_on_commit is not permanently mutated (#6564 item 2).
    """
    engine = create_engine(
        "sqlite:///:memory:",
        poolclass=QueuePool,
        pool_size=1,
        max_overflow=0,
        pool_timeout=1.0,
        connect_args={"check_same_thread": False},
    )
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)

    # Seed required rows including a pre-existing tracker
    with Session() as seed_session:
        rh = ResearchHistory(
            id=str(uuid.uuid4()),
            query="q",
            mode="quick",
            status="completed",
            created_at="2026-05-09T00:00:00",
        )
        seed_session.add(rh)
        resource = ResearchResource(
            research_id=rh.id,
            title="Retry Paper",
            url="https://example.com/retry_paper.pdf",
            source_type="academic",
            created_at="2026-05-09T00:00:00",
        )
        seed_session.add(resource)
        source_type = SourceType(
            id=str(uuid.uuid4()),
            name="research_download",
            display_name="Research Download",
        )
        seed_session.add(source_type)
        seed_session.commit()
        resource_id = resource.id
        source_type_id = source_type.id

    service = DownloadService.__new__(DownloadService)
    service.username = "test_user"
    service.password = "test_pass"
    service.library_root = "/tmp/ldr-test-library"
    service.legacy_library_root = None
    service._closed = False
    settings = MagicMock()
    settings.get_setting = lambda key, default=None: {
        "research_library.pdf_storage_mode": "database",
        "research_library.max_pdf_size_mb": 50,
    }.get(key, default)
    service.settings = settings
    service._check_url_against_policy = lambda url: (True, "ok")
    service.retry_manager = MagicMock()

    url_hash = service._get_url_hash("https://example.com/retry_paper.pdf")
    with Session() as seed_session:
        tracker = DownloadTracker(
            url="https://example.com/retry_paper.pdf",
            url_hash=url_hash,
            first_resource_id=resource_id,
            is_downloaded=False,
        )
        seed_session.add(tracker)
        seed_session.commit()

    thread_local = threading.local()

    def get_thread_session():
        if not hasattr(thread_local, "session") or thread_local.session is None:
            thread_local.session = Session()
        return thread_local.session

    @contextmanager
    def production_get_user_db_session(*args, **kwargs):
        sess = get_thread_session()
        try:
            yield sess
        except Exception:
            sess.rollback()
            raise

    worker2_acquired_connection = False
    downloader = MagicMock()
    downloader.can_handle.return_value = True

    def slow_download(url, content_type):
        nonlocal worker2_acquired_connection

        def worker2_fn():
            nonlocal worker2_acquired_connection
            try:
                with production_get_user_db_session() as w2_sess:
                    count = w2_sess.query(ResearchResource).count()
                    assert count == 1
                    w2_sess.commit()
                    worker2_acquired_connection = True
            except Exception as exc:
                worker2_acquired_connection = False
                raise AssertionError(
                    f"Worker 2 starved on retry path: {exc}"
                ) from exc

        t = threading.Thread(target=worker2_fn)
        t.start()
        t.join()

        return MagicMock(
            is_success=True,
            content=b"%PDF-1.4 downloaded data",
            status_code=200,
            skip_reason=None,
        )

    downloader.download_with_result.side_effect = slow_download
    service.downloaders = [downloader]

    with (
        patch(
            f"{MODULE}.get_user_db_session",
            side_effect=production_get_user_db_session,
        ),
        patch(f"{MODULE}.PDFStorageManager") as mock_psm,
        patch(f"{MODULE}.get_source_type_id", return_value=source_type_id),
        patch(f"{MODULE}.get_default_library_id", return_value="lib-1"),
    ):
        mock_psm.return_value.save_pdf.return_value = ("database", None)
        success, reason = service.download_resource(resource_id)

    assert success is True
    assert worker2_acquired_connection is True, (
        "Worker 2 was starved while worker 1 was downloading on retry path. "
        "The pre-existing tracker branch did not commit its pre-check transaction."
    )
    assert get_thread_session().expire_on_commit is True

    engine.dispose()


def test_download_failure_stages_attempt_after_download(service_stub):
    """When download fails (e.g. 404 / skip reason), the attempt is staged only after download completes."""
    events = []

    downloader = MagicMock()
    downloader.can_handle.return_value = True

    def fail_download(url, content_type):
        events.append("network_download_attempt")
        return MagicMock(
            is_success=False,
            content=None,
            status_code=404,
            skip_reason="HTTP 404 Not Found",
        )

    downloader.download_with_result.side_effect = fail_download
    service_stub.downloaders = [downloader]

    mock_session = MagicMock()
    mock_session.get.return_value = MagicMock(
        id=2,
        url="https://example.com/not_found.pdf",
        title="404 Paper",
        document_id=None,
    )
    mock_session.query.return_value.filter_by.return_value.first.return_value = None

    @contextmanager
    def mock_db_sess(*args, **kwargs):
        events.append("session_active")
        yield mock_session

    with patch(f"{MODULE}.get_user_db_session", side_effect=mock_db_sess):
        success, reason = service_stub.download_resource(2)

    assert success is False
    assert "404" in reason

    # Verify that DownloadAttempt was added to session
    added_attempts = [
        call.args[0]
        for call in mock_session.add.call_args_list
        if call.args and isinstance(call.args[0], DownloadAttempt)
    ]
    assert len(added_attempts) >= 1
    assert added_attempts[0].succeeded is False
    assert "404" in added_attempts[0].error_message


def test_download_network_exception_records_failure_without_session_pinning(
    service_stub,
):
    """When download_with_result raises an exception (e.g. connection reset),
    the exception is caught and failed attempt is staged without leaking sessions.
    """
    downloader = MagicMock()
    downloader.can_handle.return_value = True
    downloader.download_with_result.side_effect = ConnectionResetError(
        "Connection reset by peer"
    )
    service_stub.downloaders = [downloader]

    mock_session = MagicMock()
    mock_session.get.return_value = MagicMock(
        id=3,
        url="https://example.com/network_error.pdf",
        title="Error Paper",
        document_id=None,
    )
    mock_session.query.return_value.filter_by.return_value.first.return_value = None

    @contextmanager
    def mock_db_sess(*args, **kwargs):
        yield mock_session

    with patch(f"{MODULE}.get_user_db_session", side_effect=mock_db_sess):
        success, reason = service_stub.download_resource(3)

    assert success is False
    # Skip reason must be a caller-safe token, NOT the raw exception text.
    # sanitize_error_for_client scrubs credentials but explicitly does not
    # strip SQL text or dependency internals; the failure path now maps the
    # exception class to a fixed token (see #6564 follow-up).
    assert reason == "network_reset"
    assert "Connection reset by peer" not in reason

    added_attempts = [
        call.args[0]
        for call in mock_session.add.call_args_list
        if call.args and isinstance(call.args[0], DownloadAttempt)
    ]
    assert len(added_attempts) >= 1
    assert added_attempts[0].succeeded is False
    assert added_attempts[0].error_type == "ConnectionResetError"
    # Server-side persisted error_message keeps the diagnostic text (with
    # credential scrubbing); the caller-visible skip_reason does not.
    assert "Connection reset by peer" in added_attempts[0].error_message


def test_download_sqlalchemy_exception_does_not_leak_sql_in_skip_reason(
    service_stub,
):
    """The caller-visible skip_reason for a SQLAlchemy failure must not carry SQL text.

    Reviewer finding (post-#6564): ``sanitize_error_for_client(str(e))`` scrubbed
    credentials but left SQL text — e.g. ``"INSERT INTO document_collections ..."`` —
    verbatim, and the result flowed into retry-manager and bulk-stream SSE messages.
    The fix maps the exception class to a fixed token; this test asserts that
    mapping is actually applied, not bypassed for SQLAlchemy errors.

    Mutation: removing ``_client_safe_download_message`` and restoring the old
    ``sanitize_error_for_client(str(e))`` would put the SQL text back into the
    returned skip_reason; this test fails.
    """
    from sqlalchemy.exc import IntegrityError

    downloader = MagicMock()
    downloader.can_handle.return_value = True
    downloader.download_with_result.side_effect = IntegrityError(
        "INSERT INTO document_collections (document_id, collection_id) "
        "VALUES (?, ?, ?)",
        params=("doc-1", "coll-1"),
        orig=Exception("FOREIGN KEY constraint failed"),
    )
    service_stub.downloaders = [downloader]

    mock_session = MagicMock()
    mock_session.get.return_value = MagicMock(
        id=4,
        url="https://example.com/fk_failure.pdf",
        title="FK Paper",
        document_id=None,
    )
    mock_session.query.return_value.filter_by.return_value.first.return_value = None

    @contextmanager
    def mock_db_sess(*args, **kwargs):
        yield mock_session

    with patch(f"{MODULE}.get_user_db_session", side_effect=mock_db_sess):
        success, reason = service_stub.download_resource(4)

    assert success is False
    # Skip reason is the safe mapped token, NOT the SQL text.
    assert reason == "database_constraint"
    forbidden_substrings = [
        "INSERT INTO",
        "document_collections",
        "FOREIGN KEY",
        "VALUES",
        "doc-1",
        "coll-1",
    ]
    for needle in forbidden_substrings:
        assert needle not in reason, (
            f"Caller-visible skip_reason leaked SQL fragment {needle!r}: "
            f"{reason!r}"
        )

    # The persisted DownloadAttempt.error_message keeps the server-side
    # diagnostic (with credential scrubbing) so the operator can debug the
    # staging failure from the row — but the retry-manager and bulk-stream
    # surfaces see only the safe mapped token above.
    added_attempts = [
        call.args[0]
        for call in mock_session.add.call_args_list
        if call.args and isinstance(call.args[0], DownloadAttempt)
    ]
    assert len(added_attempts) >= 1
    assert added_attempts[0].succeeded is False
    assert added_attempts[0].error_type == "IntegrityError"


def test_text_extraction_save_failures_do_not_leak_sql_in_error(
    service_stub,
):
    """Sibling text-extraction paths must use the same fixed-token mapping.

    Reviewer finding (post-#6564 at 4abcb707): only ``_download_pdf``'s
    except path was mapped — ``_try_arxiv_text_extraction``,
    ``_try_api_text_extraction`` and ``_fallback_pdf_extraction`` shaped
    ``sanitize_error_for_client(f"Failed to save text: {str(e)}")``, which
    keeps SQL text verbatim, and ``download_as_text`` returns it into the
    bulk-stream SSE ``error`` field (text_only mode). All three now route
    through ``_client_safe_download_message``.

    Mutation: restoring ``sanitize_error_for_client(f"Failed to save text:
    {e}")`` on any of these paths puts ``INSERT INTO`` back into the
    returned error; this test fails.
    """
    from sqlalchemy.exc import IntegrityError

    def _integrity_error():
        return IntegrityError(
            "INSERT INTO document_collections (document_id, collection_id) "
            "VALUES (?, ?, ?)",
            params=("doc-1", "coll-1"),
            orig=Exception("FOREIGN KEY constraint failed"),
        )

    forbidden = [
        "INSERT INTO",
        "document_collections",
        "FOREIGN KEY",
        "VALUES",
        "doc-1",
        "coll-1",
    ]

    # _try_api_text_extraction save failure.
    session = MagicMock()
    resource = MagicMock()
    resource.url = "https://example.com/api_text"
    resource.id = 11
    resource.title = "API Text Paper With Long Title For Slicing"
    downloader = MagicMock()
    downloader.download_with_result.return_value = MagicMock(
        is_success=True, content="text from api"
    )
    with (
        patch.object(service_stub, "_get_downloader", return_value=downloader),
        patch.object(
            service_stub,
            "_save_text_with_db",
            side_effect=_integrity_error(),
        ),
    ):
        ok, err = service_stub._try_api_text_extraction(session, resource)
    assert ok is False
    assert err == "database_constraint"
    for needle in forbidden:
        assert needle not in err

    # _fallback_pdf_extraction save failure.
    session = MagicMock()
    resource = MagicMock()
    resource.url = "https://example.com/fallback_pdf"
    resource.id = 12
    resource.title = "Fallback PDF Paper With Long Title For Slicing"
    downloader = MagicMock()
    downloader.can_handle.return_value = True
    downloader.download_with_result.return_value = MagicMock(
        is_success=True, content=b"%PDF-1.4 bytes"
    )
    service_stub.downloaders = [downloader]
    with (
        patch.object(
            service_stub, "_extract_text_from_pdf", return_value="text"
        ),
        patch.object(
            service_stub,
            "_save_text_with_db",
            side_effect=_integrity_error(),
        ),
    ):
        ok, err = service_stub._fallback_pdf_extraction(session, resource)
    assert ok is False
    assert err == "database_constraint"
    for needle in forbidden:
        assert needle not in err


def test_first_time_download_persists_tracker_before_attempt_with_fk_on(
    tmp_path,
):
    """FK-ON regression pin for #6564 P1.

    On a first-time download, ``download_resource`` adds the new
    ``DownloadTracker`` and then ``session.expunge``s it so its attributes
    remain accessible without a lazy refresh. If the INSERT is still
    pending at that point, ``expunge`` cancels it and the later
    ``DownloadAttempt.url_hash`` insert fails with ``FOREIGN KEY constraint
    failed`` (production runs with ``PRAGMA foreign_keys = ON``; this
    engine mirrors that). The fix makes the tracker row durable before the
    expunge via two cooperating mechanisms: an explicit ``session.flush()``
    inside a ``session.begin_nested()`` savepoint. The savepoint's exit
    auto-flushes its writes, so the invariant is doubly defended — removing
    the explicit flush alone does NOT fail this test (verified by run), and
    removing both the savepoint wrapper and the explicit flush DOES fail it:
    the attempt INSERT hits the missing FK parent, the staging commit
    aborts, and the assertions below see 0 trackers / 0 attempts.

    The explicit flush is retained as defense-in-depth so the FK-on-first-
    download invariant is stated at this site rather than relying on
    savepoint-exit side effects (see the comment at the pre-check block).

    The earlier pool-exhaustion tests do not catch this because they run
    on engines that do not enable ``PRAGMA foreign_keys`` (SQLite default
    is OFF), so the broken INSERT silently no-ops.
    """
    db_file = tmp_path / "fk_on_test.db"
    engine = create_engine(
        f"sqlite:///{db_file}",
        connect_args={"check_same_thread": False},
    )

    # Mirror production: every connection in sqlcipher_utils turns
    # PRAGMA foreign_keys on at connect time. Without this listener the
    # SQLite default (OFF) silently turns the broken sequence into a
    # vacuous pass.
    @event.listens_for(engine, "connect")
    def _enable_fk(dbapi_connection, _connection_record):
        dbapi_connection.execute("PRAGMA foreign_keys = ON")

    Base.metadata.create_all(engine)

    # Sanity-check the pragma actually took effect on a fresh connection.
    with engine.connect() as conn:
        fk_state = conn.exec_driver_sql("PRAGMA foreign_keys").scalar_one()
        assert fk_state == 1, (
            "PRAGMA foreign_keys did not turn on — FK-ON regression test "
            "would be vacuous."
        )

    Session = sessionmaker(bind=engine)

    # Seed required rows.
    with Session() as seed_session:
        rh = ResearchHistory(
            id=str(uuid.uuid4()),
            query="q",
            mode="quick",
            status="completed",
            created_at="2026-05-09T00:00:00",
        )
        seed_session.add(rh)
        resource = ResearchResource(
            research_id=rh.id,
            title="First Download Paper",
            url="https://example.com/first_paper.pdf",
            source_type="academic",
            created_at="2026-05-09T00:00:00",
        )
        seed_session.add(resource)
        source_type = SourceType(
            id=str(uuid.uuid4()),
            name="research_download",
            display_name="Research Download",
        )
        source_type_id = source_type.id
        seed_session.add(source_type)
        # FK target for ensure_in_collection (document_collections.collection_id).
        collection = Collection(
            id="lib-1",
            name="Library",
            collection_type="default_library",
            is_default=True,
        )
        seed_session.add(collection)
        seed_session.commit()
        resource_id = resource.id

    service = DownloadService.__new__(DownloadService)
    service.username = "test_user"
    service.password = "test_pass"
    service.library_root = "/tmp/ldr-test-library"
    service.legacy_library_root = None
    service._closed = False
    settings = MagicMock()
    settings.get_setting = lambda key, default=None: {
        "research_library.pdf_storage_mode": "database",
        "research_library.max_pdf_size_mb": 50,
    }.get(key, default)
    service.settings = settings
    service._check_url_against_policy = lambda url: (True, "ok")
    service.retry_manager = MagicMock()

    downloader = MagicMock()
    downloader.can_handle.return_value = True
    downloader.download_with_result.return_value = MagicMock(
        is_success=True,
        content=b"%PDF-1.4 first download",
        status_code=200,
        skip_reason=None,
    )
    service.downloaders = [downloader]

    thread_local = threading.local()

    def get_thread_session():
        if not hasattr(thread_local, "session") or thread_local.session is None:
            thread_local.session = Session()
        return thread_local.session

    @contextmanager
    def production_get_user_db_session(*args, **kwargs):
        sess = get_thread_session()
        try:
            yield sess
        except Exception:
            sess.rollback()
            raise

    with (
        patch(
            f"{MODULE}.get_user_db_session",
            side_effect=production_get_user_db_session,
        ),
        patch(f"{MODULE}.PDFStorageManager") as mock_psm,
        patch(f"{MODULE}.get_source_type_id", return_value=source_type_id),
        patch(f"{MODULE}.get_default_library_id", return_value="lib-1"),
    ):
        mock_psm.return_value.save_pdf.return_value = ("database", None)
        success, reason = service.download_resource(resource_id)

    assert success is True, (
        f"First-time download failed under FK enforcement: {reason!r}. "
        "Likely cause: the pending DownloadTracker INSERT was cancelled "
        "by session.expunge() before commit, leaving no parent row for "
        "DownloadAttempt.url_hash (the #6564 P1 fix is the explicit "
        "session.flush() inside the begin_nested() savepoint before "
        "session.expunge). This is mutation-verified — removing both the "
        "savepoint wrapper and the explicit flush causes this test "
        "to fail with FOREIGN KEY constraint failed on download_attempts."
    )

    # On-disk assertions: tracker + attempt + document must all exist. The
    # broken sequence produces 0 trackers (expunge cancels the pending
    # INSERT) and 0 attempts (FK failure aborts the staging commit).
    with Session() as verify_session:
        trackers = verify_session.query(DownloadTracker).all()
        attempts = verify_session.query(DownloadAttempt).all()
        docs = (
            verify_session.query(Document)
            .filter_by(resource_id=resource_id)
            .all()
        )

        assert len(trackers) == 1, (
            f"Expected exactly 1 DownloadTracker row, found {len(trackers)}. "
            "The pending INSERT was likely cancelled by expunge."
        )
        assert len(attempts) == 1, (
            f"Expected exactly 1 DownloadAttempt row, found {len(attempts)}. "
            "FOREIGN KEY constraint failed on the attempt INSERT — the "
            "tracker row was not durable at the time of the attempt."
        )
        assert attempts[0].succeeded is True
        assert attempts[0].bytes_downloaded == len(b"%PDF-1.4 first download")
        assert len(docs) == 1
        assert docs[0].status == DocumentStatus.COMPLETED

    engine.dispose()


def test_pre_check_savepoint_scopes_new_tracker_write(tmp_path):
    """The new-tracker INSERT must be scoped to a SAVEPOINT.

    Reviewer finding (post-#6564): ``download_resource`` issues an
    unconditional ``session.commit()`` to release the connection back to
    QueuePool. The new-tracker INSERT is the only write the pre-check
    performs on a first-time download, and it must be enclosed in a
    savepoint so a failure INSIDE the savepoint (e.g. a future UNIQUE
    constraint conflict, an autoflush interaction, or a manual rollback
    by another block) cannot drag the caller's outer transaction with
    it.

    Without the savepoint, ``session.add(tracker)`` puts the pending
    INSERT directly on the shared thread-local session's outer
    transaction. The unconditional ``session.commit()`` at the end
    promotes it together with any other row the caller has staged — a
    structural surprise rather than a deliberate commit. None of the
    three current call sites stage rows across the call, but the
    reviewer asked for a structural guarantee.

    This test asserts the savepoint is in place by checking that the
    session's nested-transaction counter advances during the pre-check
    block. Removing the ``with session.begin_nested():`` wrapper drops
    the nested-transaction count to zero on a first-time download.
    """
    db_file = tmp_path / "savepoint_scope_test.db"
    engine = create_engine(
        f"sqlite:///{db_file}",
        connect_args={"check_same_thread": False},
    )
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)

    with Session() as seed_session:
        rh = ResearchHistory(
            id=str(uuid.uuid4()),
            query="q",
            mode="quick",
            status="completed",
            created_at="2026-05-09T00:00:00",
        )
        seed_session.add(rh)
        resource = ResearchResource(
            research_id=rh.id,
            title="Savepoint Scope Paper",
            url="https://example.com/savepoint_scope_paper.pdf",
            source_type="academic",
            created_at="2026-05-09T00:00:00",
        )
        seed_session.add(resource)
        source_type = SourceType(
            id=str(uuid.uuid4()),
            name="research_download",
            display_name="Research Download",
        )
        source_type_id = source_type.id
        seed_session.add(source_type)
        collection = Collection(
            id="lib-1",
            name="Library",
            collection_type="default_library",
            is_default=True,
        )
        seed_session.add(collection)
        seed_session.commit()
        resource_id = resource.id

    service = DownloadService.__new__(DownloadService)
    service.username = "test_user"
    service.password = "test_pass"
    service.library_root = "/tmp/ldr-test-library"
    service.legacy_library_root = None
    service._closed = False
    settings = MagicMock()
    settings.get_setting = lambda key, default=None: {
        "research_library.pdf_storage_mode": "database",
        "research_library.max_pdf_size_mb": 50,
    }.get(key, default)
    service.settings = settings
    service._check_url_against_policy = lambda url: (True, "ok")
    service.retry_manager = MagicMock()

    # Probe: count savepoint begin_nested calls during the pre-check.
    nested_count = {"value": 0}

    downloader = MagicMock()
    downloader.can_handle.return_value = True
    downloader.download_with_result.return_value = MagicMock(
        is_success=True,
        content=b"%PDF-1.4 savepoint scope",
        status_code=200,
        skip_reason=None,
    )
    service.downloaders = [downloader]

    thread_local = threading.local()

    def get_thread_session():
        if not hasattr(thread_local, "session") or thread_local.session is None:
            thread_local.session = Session()
        return thread_local.session

    @contextmanager
    def production_get_user_db_session(*args, **kwargs):
        sess = get_thread_session()
        # Wrap begin_nested so we can observe the pre-check using it.
        original = sess.begin_nested

        @contextmanager
        def counting_begin_nested(*a, **kw):
            nested_count["value"] += 1
            with original(*a, **kw) as sp:
                yield sp

        sess.begin_nested = counting_begin_nested
        try:
            yield sess
        except Exception:
            sess.rollback()
            raise
        finally:
            sess.begin_nested = original

    with (
        patch(
            f"{MODULE}.get_user_db_session",
            side_effect=production_get_user_db_session,
        ),
        patch(f"{MODULE}.PDFStorageManager") as mock_psm,
        patch(f"{MODULE}.get_source_type_id", return_value=source_type_id),
        patch(f"{MODULE}.get_default_library_id", return_value="lib-1"),
    ):
        mock_psm.return_value.save_pdf.return_value = ("database", None)
        success, reason = service.download_resource(resource_id)

    assert success is True, (
        f"download_resource failed: {reason!r}. The savepoint structure "
        "should isolate the pre-check writes."
    )

    # First-time download must have opened exactly one savepoint around
    # the new-tracker INSERT. A retry path (existing tracker) opens zero,
    # so this asserts the FIRST-DOWNLOAD case specifically — which is the
    # one with the write to scope.
    assert nested_count["value"] == 1, (
        f"Expected exactly 1 begin_nested() call during the pre-check, "
        f"got {nested_count['value']}. The new-tracker INSERT is no "
        f"longer scoped to a savepoint — caller-staged rows are no "
        f"longer isolated from the unconditional commit."
    )

    # End-to-end correctness: the new-tracker INSERT must still be durable.
    with Session() as verify_session:
        trackers = verify_session.query(DownloadTracker).all()
        attempts = verify_session.query(DownloadAttempt).all()
        assert len(trackers) == 1
        assert len(attempts) == 1
        assert attempts[0].succeeded is True

    engine.dispose()


def test_bulk_concurrent_downloads_do_not_exhaust_connection_pool(tmp_path):
    """Bulk downloads (10 concurrent workers) on a pool of max 4 connections must not starve
    concurrent database requests.
    """
    db_file = tmp_path / "bulk_test.db"
    engine = create_engine(
        f"sqlite:///{db_file}",
        poolclass=QueuePool,
        pool_size=2,
        max_overflow=2,  # Max 4 total connections
        pool_timeout=5.0,
        connect_args={"check_same_thread": False},
    )
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)

    # Seed 10 resources
    with Session() as seed_sess:
        rh = ResearchHistory(
            id=str(uuid.uuid4()),
            query="bulk",
            mode="quick",
            status="completed",
            created_at="2026-05-09T00:00:00",
        )
        seed_sess.add(rh)
        res_ids = []
        for i in range(10):
            r = ResearchResource(
                research_id=rh.id,
                title=f"Bulk Paper {i}",
                url=f"https://example.com/paper_{i}.pdf",
                source_type="academic",
                created_at="2026-05-09T00:00:00",
            )
            seed_sess.add(r)
            seed_sess.flush()
            res_ids.append(r.id)
        source_type = SourceType(
            id=str(uuid.uuid4()),
            name="research_download",
            display_name="Research Download",
        )
        source_type_id = source_type.id
        seed_sess.add(source_type)
        seed_sess.commit()

    thread_local = threading.local()

    def get_thread_session():
        if not hasattr(thread_local, "session") or thread_local.session is None:
            thread_local.session = Session()
        return thread_local.session

    @contextmanager
    def production_get_user_db_session(*args, **kwargs):
        sess = get_thread_session()
        try:
            yield sess
        except Exception:
            sess.rollback()
            raise

    def make_service():
        s = DownloadService.__new__(DownloadService)
        s.username = "test_user"
        s.password = "test_pass"
        s.library_root = "/tmp/ldr-test-library"
        s.legacy_library_root = None
        s._closed = False
        settings = MagicMock()
        settings.get_setting = lambda key, default=None: {
            "research_library.pdf_storage_mode": "database",
            "research_library.max_pdf_size_mb": 50,
        }.get(key, default)
        s.settings = settings
        s._check_url_against_policy = lambda url: (True, "ok")
        s.retry_manager = MagicMock()

        dl = MagicMock()
        dl.can_handle.return_value = True

        def download_fn(url, content_type):
            time.sleep(0.05)  # 50ms simulated HTTP transfer
            return MagicMock(
                is_success=True,
                content=f"%PDF-bulk data {url}".encode(),
                status_code=200,
                skip_reason=None,
            )

        dl.download_with_result.side_effect = download_fn
        s.downloaders = [dl]
        return s

    with (
        patch(
            f"{MODULE}.get_user_db_session",
            side_effect=production_get_user_db_session,
        ),
        patch(f"{MODULE}.PDFStorageManager") as mock_psm,
        patch(f"{MODULE}.get_source_type_id", return_value=source_type_id),
        patch(f"{MODULE}.get_default_library_id", return_value="lib-1"),
    ):
        mock_psm.return_value.save_pdf.return_value = ("database", None)

        def run_download(rid):
            svc = make_service()
            return svc.download_resource(rid)

        def run_fastapi_request():
            with production_get_user_db_session() as sess:
                count = sess.query(ResearchResource).count()
                sess.commit()
                return count

        # Run 10 downloads concurrently with 15 concurrent simulated FastAPI requests
        with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
            download_futures = [
                executor.submit(run_download, rid) for rid in res_ids
            ]
            fastapi_futures = [
                executor.submit(run_fastapi_request) for _ in range(15)
            ]

            # All FastAPI requests must complete without timing out or pool exhaustion
            for f in concurrent.futures.as_completed(fastapi_futures):
                count = f.result()
                assert count == 10

            # All downloads must succeed
            for f in concurrent.futures.as_completed(download_futures):
                success, reason = f.result()
                assert success is True, f"Download failed: {reason}"
                assert reason is None

    engine.dispose()