~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Route-level half of issue #4691: the download-queue claim, driven end to
end against a REAL SQLite database.
Ported from ``tests/research_library/routes/test_download_bulk_concurrent_claim_4691.py``
(809 lines, deleted in the Flask->FastAPI migration).
``tests/web/routers/test_download_queue_claim_4691.py`` recovered the helper
half of that file -- ``_claim_download_queue_item`` /
``_release_download_queue_item`` / ``_finalize_download_queue_item`` called
directly on a real engine. What it does NOT cover is whether the ROUTES use
those helpers correctly, and whether the OTHER writers of
``DownloadQueue.status`` respect a claim they did not take. A claim is only
worth as much as every other writer of that column respects, and each of the
three reset paths below can hand a row a bulk stream is downloading to a
second downloader:
* ``download_bulk`` itself -- claim BEFORE the download, finalise after, and
release on any raise in the post-claim window.
* ``queue_all_undownloaded`` (``library.py``, the
``status != PENDING and status != PROCESSING`` reset).
* ``download_source`` (``library.py``, the ``status != PROCESSING`` reset,
which returns 409 when it matches zero rows).
The two ``!= PROCESSING`` reset guards are the specific gap. On the branch
``test_library_download_outcomes.py::test_a_lost_claim_is_a_409_not_a_completion``
looks like it pins the ``download_source`` one, but its
``_call_download_source`` helper takes a ``queue_entry=`` argument that no
test ever passes: with ``queue_entry=None`` the handler takes the
"create a new row" branch and never reaches the reset at all, so that test
pins the CLAIM-lost path and leaves the RESET-lost path untested. Deleting
``LibraryDownloadQueue.status != DocumentStatus.PROCESSING`` from either
reset leaves every test on the branch green.
Every test here uses a real on-disk SQLite database, deliberately: a
``MagicMock`` session with a canned ``update.return_value`` *tells the code
its claim won* and cannot fail when the SQL predicate stops being atomic or
the status filter is dropped -- which is the entire content of the fix.
"""
import asyncio
import json
from contextlib import contextmanager
from datetime import UTC, datetime
from unittest.mock import MagicMock, patch
import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.exc import OperationalError
from sqlalchemy.orm import sessionmaker
from starlette.requests import Request
from local_deep_research.database.models import Base
from local_deep_research.database.models.library import (
DocumentStatus,
DownloadQueue as LibraryDownloadQueue,
)
from local_deep_research.database.models.research import (
ResearchHistory,
ResearchResource,
)
from local_deep_research.web.routers import library as library_module
from local_deep_research.web.routers.library import (
download_bulk,
download_source,
queue_all_undownloaded,
)
RESEARCH_ID = "r1"
ARXIV_URL = "https://arxiv.org/abs/2401.0001"
# ---------------------------------------------------------------------------
# Real-database fixture plumbing
# ---------------------------------------------------------------------------
def _engine_with_pending_row(tmp_path, name, request):
"""A real on-disk SQLite DB seeded with one PENDING download-queue row.
On disk rather than ``:memory:`` so a second Session sees the first
session's committed claim; an in-memory database is per-connection and
the concurrency modelled here would pass vacuously.
Returns ``(Session factory, queue_item_id, resource_id)``.
"""
engine = create_engine(f"sqlite:///{tmp_path}/{name}.db")
request.addfinalizer(engine.dispose)
Base.metadata.create_all(engine)
session_factory = sessionmaker(bind=engine)
def tracked_session_factory(*args, **kwargs):
session = session_factory(*args, **kwargs)
request.addfinalizer(session.close)
return session
session = tracked_session_factory()
session.add(
ResearchHistory(
id=RESEARCH_ID,
query="test query",
mode="quick",
status="completed",
created_at="2026-05-09T00:00:00",
)
)
resource = ResearchResource(
research_id=RESEARCH_ID,
title="Test Paper",
url=ARXIV_URL,
source_type="academic",
created_at="2026-05-09T00:00:00",
)
session.add(resource)
session.commit()
row = LibraryDownloadQueue(
resource_id=resource.id,
research_id=RESEARCH_ID,
status=DocumentStatus.PENDING,
)
session.add(row)
session.commit()
queue_item_id = row.id
resource_id = resource.id
session.close()
return tracked_session_factory, queue_item_id, resource_id
def _set_status(Session, queue_item_id, status, claimed_at=None):
session = Session()
values = {LibraryDownloadQueue.status: status}
if claimed_at is not None:
values[LibraryDownloadQueue.last_attempt_at] = claimed_at
session.query(LibraryDownloadQueue).filter_by(id=queue_item_id).update(
values, synchronize_session=False
)
session.commit()
session.close()
def _status_of(Session, queue_item_id):
session = Session()
row = session.get(LibraryDownloadQueue, queue_item_id)
status = None if row is None else row.status
session.close()
return status
def _session_patch(Session):
"""Patch ``library.get_user_db_session`` onto the real engine."""
@contextmanager
def fake_db_session(*args, **kwargs):
with Session() as session:
yield session
return patch.object(
library_module, "get_user_db_session", side_effect=fake_db_session
)
# ---------------------------------------------------------------------------
# Request builders (the handlers are called directly -- none of the branches
# under test touch the request beyond its JSON body and session)
# ---------------------------------------------------------------------------
def _post_json_request(payload, path="/library/api/download-bulk"):
body = json.dumps(payload).encode("utf-8")
async def receive():
return {"type": "http.request", "body": body, "more_body": False}
return Request(
{
"type": "http",
"method": "POST",
"path": path,
"headers": [(b"content-type", b"application/json")],
"query_string": b"",
"session": {"session_id": "sid"},
},
receive,
)
def _drain_sse(response):
"""Consume a ``StreamingResponse`` body to completion and return the text.
The SSE generator is lazy: the queue work under test only runs while the
body is being consumed, so nothing is asserted until this returns.
"""
async def _collect():
chunks = []
async for chunk in response.body_iterator:
chunks.append(
chunk if isinstance(chunk, bytes) else str(chunk).encode()
)
return b"".join(chunks).decode()
return asyncio.run(_collect())
def _run_download_bulk(Session, mode="pdf", ds_mock=None, extra_patches=()):
"""Drive one POST /library/api/download-bulk against the real database."""
if ds_mock is None:
ds_mock = MagicMock()
ds_mock.queue_research_downloads.return_value = 0
ds_mock.download_resource.return_value = (True, None)
ds_mock.download_as_text.return_value = (True, None)
patches = [
_session_patch(Session),
patch.object(
library_module, "get_authenticated_user_password", return_value="pw"
),
patch.object(library_module, "DownloadService", return_value=ds_mock),
*extra_patches,
]
started = [p.start() for p in patches]
del started
try:
response = asyncio.run(
download_bulk(
_post_json_request(
{"research_ids": [RESEARCH_ID], "mode": mode}
),
username="testuser",
)
)
body = _drain_sse(response)
finally:
for p in reversed(patches):
p.stop()
return ds_mock, body
# ---------------------------------------------------------------------------
# download_bulk: the claim is taken BEFORE the download
# ---------------------------------------------------------------------------
def test_download_bulk_claims_pending_row_before_downloading(tmp_path, request):
"""The row must be PROCESSING by the time ``download_resource`` runs, so a
concurrent stream querying PENDING cannot also grab it. A row still PENDING
when the download starts is exactly the #4691 double-processing window.
"""
Session, queue_item_id, _ = _engine_with_pending_row(
tmp_path, "claim_before", request
)
observed = {}
def fake_download_resource(resource_id):
# An independent session models the concurrent stream: read the row's
# status as it stands the moment the download begins.
other = Session()
observed["status_at_download"] = other.get(
LibraryDownloadQueue, queue_item_id
).status
other.close()
return (True, None)
ds_mock = MagicMock()
ds_mock.queue_research_downloads.return_value = 0
ds_mock.download_resource.side_effect = fake_download_resource
ds_mock, body = _run_download_bulk(Session, ds_mock=ds_mock)
assert ds_mock.download_resource.called, body
assert observed.get("status_at_download") is DocumentStatus.PROCESSING, (
"download_bulk must claim the row (PENDING -> PROCESSING) before "
"calling download_resource so a concurrent stream can't also process "
f"it; saw {observed.get('status_at_download')} (issue #4691)."
)
def test_text_only_bulk_finalizes_claim(tmp_path, request):
"""A successful text extraction must leave the row COMPLETED, not stranded.
``download_as_text`` and its call tree never write ``DownloadQueue`` and
nothing in ``src/`` reaps PROCESSING rows, so without the finalise the
claim is permanent.
"""
Session, queue_item_id, _ = _engine_with_pending_row(
tmp_path, "text_only", request
)
ds_mock = MagicMock()
ds_mock.queue_research_downloads.return_value = 0
# Faithful to the real call tree: it never touches DownloadQueue.
ds_mock.download_as_text.return_value = (True, None)
ds_mock, body = _run_download_bulk(
Session, mode="text_only", ds_mock=ds_mock
)
assert ds_mock.download_as_text.called, body
assert _status_of(Session, queue_item_id) is DocumentStatus.COMPLETED, (
"a claimed row must reach a terminal status after a successful "
"text_only extraction; download_as_text never writes the queue, so "
"the claim is stranded in PROCESSING forever (issue #4691)."
)
def test_text_only_bulk_failure_returns_row_to_pending(tmp_path, request):
"""A failed extraction returns the row to PENDING, matching the pre-fix
behaviour where the row simply stayed retryable."""
Session, queue_item_id, _ = _engine_with_pending_row(
tmp_path, "text_fail", request
)
ds_mock = MagicMock()
ds_mock.queue_research_downloads.return_value = 0
ds_mock.download_as_text.return_value = (False, "no text available")
_run_download_bulk(Session, mode="text_only", ds_mock=ds_mock)
assert _status_of(Session, queue_item_id) is DocumentStatus.PENDING
def test_pdf_mode_terminal_status_is_not_overwritten(tmp_path, request):
"""pdf-mode is unchanged: ``download_resource`` records its own terminal
status, so the finalise must be a no-op there. A row it marked FAILED must
not be rewritten to COMPLETED by a truthy return."""
Session, queue_item_id, _ = _engine_with_pending_row(
tmp_path, "pdf_noop", request
)
def fake_download_resource(resource_id):
# Model download_resource's own terminal write.
_set_status(Session, queue_item_id, DocumentStatus.FAILED)
return (True, None)
ds_mock = MagicMock()
ds_mock.queue_research_downloads.return_value = 0
ds_mock.download_resource.side_effect = fake_download_resource
_run_download_bulk(Session, ds_mock=ds_mock)
assert _status_of(Session, queue_item_id) is DocumentStatus.FAILED, (
"the finalise is scoped to PROCESSING so it must not overwrite a "
"terminal status download_resource already recorded."
)
def test_download_bulk_does_not_strand_a_claim_when_title_is_null(
tmp_path, request
):
"""``ResearchResource.title`` is a nullable Text column, so
``resource.title[:50]`` raises TypeError for a resource whose title was
never populated. That lookup must sit INSIDE the try that releases the
claim; outside it, the row stays PROCESSING forever because nothing in
``src/`` reaps PROCESSING rows and every reset path refuses to touch them.
"""
Session, queue_item_id, resource_id = _engine_with_pending_row(
tmp_path, "strand_null_title", request
)
session = Session()
session.query(ResearchResource).filter_by(id=resource_id).update(
{"title": None}
)
session.commit()
session.close()
_run_download_bulk(Session)
# Assert the FINAL state: "claimed at download time" and "not stranded
# afterwards" are different properties, and only this one catches a leak.
assert _status_of(Session, queue_item_id) is DocumentStatus.COMPLETED, (
"a null title must not strand the claim: the row should reach a "
"terminal status like any other successful download (issue #4691)."
)
def test_download_bulk_releases_the_claim_when_post_claim_work_raises(
tmp_path, request
):
"""The null title above is one instance of a structural window, not the
whole of it, so the fix is guaranteed cleanup rather than a null-check on
that one expression.
Fault-inject a raise into the post-claim resource lookup and assert the
claim is released, which is the property that has to hold for *any* raise
in that window.
"""
Session, queue_item_id, _ = _engine_with_pending_row(
tmp_path, "release_post_claim", request
)
class _NotAnEntity:
"""Not a mapped class, so the post-claim ``session.get(...)`` raises.
The specific exception does not matter; what is under test is that
*any* raise in the post-claim window releases the claim."""
_run_download_bulk(
Session,
extra_patches=(
patch.object(library_module, "ResearchResource", _NotAnEntity),
),
)
assert _status_of(Session, queue_item_id) is DocumentStatus.PENDING, (
"a raise in the post-claim window must release the claim back to "
"PENDING so the row stays retryable, rather than leaving it stuck in "
"PROCESSING (issue #4691)."
)
def test_status_column_round_trips_processing(tmp_path, request):
"""Guard against a schema change silently making the claim a no-op: a
fresh PENDING row must actually be flippable to PROCESSING at DB level."""
Session, queue_item_id, _ = _engine_with_pending_row(
tmp_path, "roundtrip", request
)
session = Session()
row = session.get(LibraryDownloadQueue, queue_item_id)
assert row.status is DocumentStatus.PENDING
row.status = DocumentStatus.PROCESSING
session.commit()
session.expire_all()
assert (
session.get(LibraryDownloadQueue, queue_item_id).status
is DocumentStatus.PROCESSING
)
session.close()
# ---------------------------------------------------------------------------
# queue_all_undownloaded -- the `!= PROCESSING` reset guard (library.py)
# ---------------------------------------------------------------------------
def _resource_filter_mock(resource_ids):
"""A ResourceFilter that lets every listed resource through the retry
policy, so ``queue_all_undownloaded`` reaches its queue-reset branch."""
instance = MagicMock()
instance.filter_downloadable_resources.return_value = [
MagicMock(resource_id=rid, can_retry=True, reason="ok")
for rid in resource_ids
]
instance.get_filter_summary.return_value = MagicMock(
to_dict=MagicMock(return_value={}),
permanently_failed_count=0,
temporarily_failed_count=0,
)
instance.get_skipped_resources_info.return_value = []
return instance
def _run_queue_all_undownloaded(Session, resource_ids):
patches = [
_session_patch(Session),
patch.object(
library_module, "get_authenticated_user_password", return_value="pw"
),
patch.object(
library_module,
"ResourceFilter",
return_value=_resource_filter_mock(resource_ids),
),
]
for p in patches:
p.start()
try:
return queue_all_undownloaded(
_post_json_request({}, path="/library/api/queue-all-undownloaded"),
username="testuser",
)
finally:
for p in reversed(patches):
p.stop()
def test_queue_all_undownloaded_does_not_reset_an_in_flight_claim(
tmp_path, request
):
"""``queue_all_undownloaded`` resets any row that is not PENDING back to
PENDING. PROCESSING satisfies ``!= PENDING``, so without the second
``!= PROCESSING`` clause it un-claims a row a ``download_bulk`` stream is
downloading right now, and the freed row is immediately re-claimable: the
#4691 double-processing window, reopened from a different route.
Its own selection query (outerjoin Document, filter ``Document.id.is_(None)``)
SELECTS in-flight rows, because a download in progress has not written a
completed Document yet.
"""
Session, queue_item_id, resource_id = _engine_with_pending_row(
tmp_path, "qau_inflight", request
)
# A concurrent download_bulk stream holds the claim.
_set_status(
Session, queue_item_id, DocumentStatus.PROCESSING, datetime.now(UTC)
)
result = _run_queue_all_undownloaded(Session, [resource_id])
assert result["success"] is True, result
assert _status_of(Session, queue_item_id) is DocumentStatus.PROCESSING, (
"queue_all_undownloaded must leave a PROCESSING row alone: resetting "
"it to PENDING un-claims a download another stream is running and "
"lets a second stream re-claim the same row (issue #4691)."
)
def test_queue_all_undownloaded_still_resets_a_failed_row(tmp_path, request):
"""Discriminator for the test above: the guard must be narrow. A FAILED row
is not in flight, so the documented retry-on-every-run behaviour still
resets it. Without this, "never reset anything" would pass the test above.
"""
Session, queue_item_id, resource_id = _engine_with_pending_row(
tmp_path, "qau_failed", request
)
_set_status(Session, queue_item_id, DocumentStatus.FAILED)
result = _run_queue_all_undownloaded(Session, [resource_id])
assert _status_of(Session, queue_item_id) is DocumentStatus.PENDING
assert result["queued"] >= 1, result
@pytest.mark.parametrize("claimed_at", [None, datetime(2000, 1, 1, tzinfo=UTC)])
def test_queue_all_undownloaded_recovers_a_restart_stranded_claim(
tmp_path, request, claimed_at
):
Session, queue_item_id, resource_id = _engine_with_pending_row(
tmp_path, "qau_stale", request
)
_set_status(Session, queue_item_id, DocumentStatus.PROCESSING, claimed_at)
result = _run_queue_all_undownloaded(Session, [resource_id])
assert result["queued"] == 1
assert _status_of(Session, queue_item_id) is DocumentStatus.PENDING
def test_queue_all_undownloaded_preserves_a_claim_replaced_after_read(
tmp_path, request
):
Session, queue_item_id, resource_id = _engine_with_pending_row(
tmp_path, "qau_reclaimed", request
)
_set_status(Session, queue_item_id, DocumentStatus.PROCESSING)
original_filter = library_module._resettable_download_queue_status
def claim_before_update(observed_entry):
_set_status(
Session, queue_item_id, DocumentStatus.PROCESSING, datetime.now(UTC)
)
return original_filter(observed_entry)
with patch.object(
library_module,
"_resettable_download_queue_status",
side_effect=claim_before_update,
):
_run_queue_all_undownloaded(Session, [resource_id])
assert _status_of(Session, queue_item_id) is DocumentStatus.PROCESSING
# ---------------------------------------------------------------------------
# download_source -- the `!= PROCESSING` reset guard (library.py)
# ---------------------------------------------------------------------------
def _run_download_source(Session, ds_mock=None):
if ds_mock is None:
ds_mock = MagicMock()
ds_mock.download_resource.return_value = (True, None)
context = MagicMock()
context.__enter__.return_value = ds_mock
context.__exit__.return_value = False
patches = [
_session_patch(Session),
patch.object(
library_module, "get_authenticated_user_password", return_value="pw"
),
patch.object(library_module, "DownloadService", return_value=context),
patch.object(
library_module, "get_document_for_resource", return_value=None
),
]
for p in patches:
p.start()
try:
response = asyncio.run(
download_source(
_post_json_request(
{"research_id": RESEARCH_ID, "url": ARXIV_URL},
path="/library/api/download-source",
),
username="testuser",
)
)
finally:
for p in reversed(patches):
p.stop()
return response, ds_mock
def test_download_source_does_not_reset_an_in_flight_claim(tmp_path, request):
"""``download_source`` resets the existing queue row to PENDING and then
downloads immediately in the request thread. Its only early return is
for a Document that counts as downloaded (``document_counts_as_downloaded``:
completed, and not awaiting its PDF), which is inert during the race
window: an in-flight bulk download has not written a completed Document
yet. So the reset itself must refuse a PROCESSING row -- the row stays
PROCESSING, no second download starts, and the caller gets 409.
This is the reset-lost path. ``test_library_download_outcomes.py``'s
``test_a_lost_claim_is_a_409_not_a_completion`` passes ``claim_result=0``
with ``queue_entry=None``, which takes the create-a-new-row branch and
exercises the CLAIM-lost path instead; deleting the ``!= PROCESSING``
filter here leaves it green.
"""
Session, queue_item_id, _ = _engine_with_pending_row(
tmp_path, "dsrc_inflight", request
)
_set_status(
Session, queue_item_id, DocumentStatus.PROCESSING, datetime.now(UTC)
)
response, ds_mock = _run_download_source(Session)
assert hasattr(response, "status_code"), (
"download_source returned a success dict for a row another stream has "
f"claimed, instead of a 409 JSONResponse: {response!r}"
)
body = json.loads(response.body)
assert response.status_code == 409, body
assert body == {
"success": False,
"message": "Download already in progress",
}
# download_resource is asserted NOT to run, so mocking it cannot hide a
# status write: a call that never happens writes nothing either way.
assert not ds_mock.download_resource.called, (
"download_source must not start a second download of a resource a "
f"concurrent stream has already claimed (issue #4691); got {body}"
)
assert _status_of(Session, queue_item_id) is DocumentStatus.PROCESSING, (
"download_source must not un-claim an in-flight PROCESSING row."
)
@pytest.mark.parametrize(
"start_status",
[DocumentStatus.PENDING, DocumentStatus.FAILED, DocumentStatus.COMPLETED],
ids=lambda s: s.value,
)
def test_download_source_still_resets_and_downloads_a_row_not_in_flight(
tmp_path, request, start_status
):
"""Discriminator for the test above: the ``!= PROCESSING`` guard must be
narrow. Any non-PROCESSING row is reset, claimed and downloaded exactly as
before -- otherwise "409 on everything" would satisfy the refusal test.
"""
Session, queue_item_id, _ = _engine_with_pending_row(
tmp_path, f"dsrc_{start_status.value}", request
)
_set_status(Session, queue_item_id, start_status)
response, ds_mock = _run_download_source(Session)
assert response == {"success": True, "message": "Download completed"}, (
response
)
assert ds_mock.download_resource.called
@pytest.mark.parametrize("claimed_at", [None, datetime(2000, 1, 1, tzinfo=UTC)])
def test_download_source_recovers_a_restart_stranded_claim(
tmp_path, request, claimed_at
):
Session, queue_item_id, _ = _engine_with_pending_row(
tmp_path, "dsrc_stale", request
)
_set_status(Session, queue_item_id, DocumentStatus.PROCESSING, claimed_at)
response, service = _run_download_source(Session)
assert response == {"success": True, "message": "Download completed"}
service.download_resource.assert_called_once()
def test_download_source_refuses_a_claim_replaced_after_read(tmp_path, request):
Session, queue_item_id, _ = _engine_with_pending_row(
tmp_path, "dsrc_reclaimed", request
)
_set_status(Session, queue_item_id, DocumentStatus.PROCESSING)
original_filter = library_module._resettable_download_queue_status
def claim_before_update(observed_entry):
_set_status(
Session, queue_item_id, DocumentStatus.PROCESSING, datetime.now(UTC)
)
return original_filter(observed_entry)
with patch.object(
library_module,
"_resettable_download_queue_status",
side_effect=claim_before_update,
):
response, service = _run_download_source(Session)
assert response.status_code == 409
service.download_resource.assert_not_called()
assert _status_of(Session, queue_item_id) is DocumentStatus.PROCESSING
def test_download_source_does_not_refresh_resource_after_claim(
tmp_path, request
):
"""A failed refresh after a committed claim must not strand PROCESSING."""
Session, queue_item_id, resource_id = _engine_with_pending_row(
tmp_path, "dsrc_expired_resource", request
)
with Session() as session:
engine = session.get_bind()
claim_committed = False
original_claim = library_module._claim_download_queue_item
def claim(*args):
nonlocal claim_committed
claim_committed = original_claim(*args)
return claim_committed
def fail_resource_refresh(
connection, cursor, statement, parameters, context, executemany
):
if (
claim_committed
and statement.lstrip().upper().startswith("SELECT")
and "research_resources" in statement
):
raise OperationalError(
statement, parameters, RuntimeError("resource refresh failed")
)
event.listen(engine, "before_cursor_execute", fail_resource_refresh)
try:
with patch.object(
library_module, "_claim_download_queue_item", side_effect=claim
):
response, service = _run_download_source(Session)
finally:
event.remove(engine, "before_cursor_execute", fail_resource_refresh)
assert response == {"success": True, "message": "Download completed"}
service.download_resource.assert_called_once_with(resource_id)
assert _status_of(Session, queue_item_id) == DocumentStatus.COMPLETED
# ---------------------------------------------------------------------------
# queue_research_downloads -- the pre-pass `!= PROCESSING` reset guard
# (research_library/services/download_service.py)
# ---------------------------------------------------------------------------
#
# ``download_bulk`` calls ``queue_research_downloads`` unconditionally on every
# run (#4685), so its reset branch is the third writer of the status column.
# The branch's own successor
# (``tests/research_library/services/test_download_service_coverage.py::
# TestQueueResearchDownloads::test_in_flight_queue_entry_is_not_reset``) drives
# it with a ``MagicMock`` session whose ``update.return_value`` is hardcoded to
# ``0`` -- the mock *tells the code* the guarded UPDATE matched nothing, so
# deleting ``status != DocumentStatus.PROCESSING`` from the filter leaves it
# green. These two run the real service against the real engine, where the row
# count is produced by SQLite.
def _run_queue_research_downloads(Session):
from local_deep_research.research_library.services import (
download_service as ds_mod,
)
@contextmanager
def fake_db_session(*args, **kwargs):
with Session() as session:
yield session
with (
patch.object(
ds_mod, "get_user_db_session", side_effect=fake_db_session
),
# RetryManager opens the real user DB in the constructor and plays no
# part in the pre-pass under test.
patch.object(ds_mod, "RetryManager", MagicMock()),
patch.object(
ds_mod.DownloadService,
"_check_url_against_policy",
return_value=(True, None),
),
patch.object(
ds_mod.DownloadService, "_is_downloadable", return_value=True
),
):
service = ds_mod.DownloadService("testuser", "pw")
return service.queue_research_downloads(RESEARCH_ID, collection_id="c1")
def test_queue_research_downloads_leaves_in_flight_row_alone(tmp_path, request):
"""The pre-pass must not un-claim a row another stream is downloading.
Its reset branch gates on "no PENDING queue row and no COMPLETED
Document", which a PROCESSING row satisfies, so an unguarded reset flips
an in-flight row back to PENDING and the second stream claims and
re-downloads it: issue #4691, narrowed to the in-flight window.
"""
Session, queue_item_id, _ = _engine_with_pending_row(
tmp_path, "prepass_inflight", request
)
# Stream 1 has claimed the row and is downloading it right now.
_set_status(Session, queue_item_id, DocumentStatus.PROCESSING)
queued = _run_queue_research_downloads(Session)
assert _status_of(Session, queue_item_id) is DocumentStatus.PROCESSING, (
"queue_research_downloads must leave a PROCESSING row claimed; "
"resetting it to PENDING lets a second bulk stream re-download a "
"resource the first stream is still downloading (issue #4691)."
)
assert queued == 0, (
f"an in-flight row must not be counted as newly queued; queued={queued}"
)
def test_queue_research_downloads_still_resets_failed_row(tmp_path, request):
"""Discriminator: the guard must be narrow. A FAILED row is not in flight,
so the documented retry-on-every-bulk-run behaviour (#4685) still resets
it -- otherwise "never reset anything" would pass the test above.
"""
Session, queue_item_id, _ = _engine_with_pending_row(
tmp_path, "prepass_failed", request
)
_set_status(Session, queue_item_id, DocumentStatus.FAILED)
queued = _run_queue_research_downloads(Session)
assert _status_of(Session, queue_item_id) is DocumentStatus.PENDING
assert queued == 1