~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
"""Download access failures retain their meaning through retry recording."""
from datetime import timedelta
from unittest.mock import MagicMock, Mock, patch
import pytest
import requests
from local_deep_research.library.download_management.failure_classifier import (
FailureClassifier,
)
from local_deep_research.library.download_management.retry_manager import (
RetryManager,
)
from local_deep_research.research_library.downloaders.base import BaseDownloader
from local_deep_research.research_library.downloaders.generic import (
GenericDownloader,
)
from local_deep_research.research_library.downloaders.semantic_scholar import (
SemanticScholarDownloader,
)
PAPER_URL = "https://www.semanticscholar.org/paper/" + "a" * 40
def record_failure(result, url, *, text_only=False):
manager = RetryManager.__new__(RetryManager)
manager.failure_classifier = FailureClassifier()
manager.status_tracker = Mock()
reason = result.skip_reason
if text_only:
reason = f"PDF extraction failed: {reason}"
manager.record_attempt(
1,
(False, reason),
status_code=None if text_only else result.status_code,
url=url,
)
return manager.status_tracker.mark_failure.call_args.args[1]
@pytest.mark.parametrize("text_only", [False, True])
def test_semantic_scholar_rate_limit_remains_retryable(text_only):
with SemanticScholarDownloader() as downloader:
downloader.session.get = Mock(return_value=Mock(status_code=429))
result = downloader.download_with_result(PAPER_URL)
assert not result.is_success
assert result.status_code == 429
assert "subscription" not in result.skip_reason.lower()
failure = record_failure(result, PAPER_URL, text_only=text_only)
assert failure.error_type == "rate_limited"
assert not failure.is_permanent()
assert failure.retry_after >= timedelta(hours=1)
@pytest.mark.parametrize("status", [200, 500, 503])
def test_missing_semantic_scholar_pdf_is_not_evidence_of_a_paywall(status):
response = Mock(status_code=status)
response.json.return_value = {"openAccessPdf": None}
with SemanticScholarDownloader() as downloader:
downloader.session.get = Mock(return_value=response)
result = downloader.download_with_result(PAPER_URL)
assert not result.is_success
assert "subscription" not in result.skip_reason.lower()
assert not record_failure(result, PAPER_URL).is_permanent()
@pytest.mark.parametrize(
"error", [requests.Timeout("timeout"), requests.ConnectionError("offline")]
)
def test_semantic_scholar_lookup_error_is_retryable(error):
with SemanticScholarDownloader() as downloader:
downloader.session.get = Mock(side_effect=error)
result = downloader.download_with_result(PAPER_URL)
assert not result.is_success
assert "subscription" not in result.skip_reason.lower()
assert not record_failure(result, PAPER_URL).is_permanent()
@pytest.mark.parametrize("text_only", [False, True])
@pytest.mark.parametrize(
"host",
[
"www.mdpi.com",
"journals.plos.org",
"bmcpublichealth.biomedcentral.com",
"www.frontiersin.org",
"arxiv.org",
"www.biorxiv.org",
"www.medrxiv.org",
"pmc.ncbi.nlm.nih.gov",
"europepmc.org",
],
)
def test_open_access_403_remains_retryable(host, text_only):
url = f"https://{host}/article.pdf"
response = MagicMock(status_code=403)
response.__enter__.return_value = response
with GenericDownloader() as downloader:
downloader.session.get = Mock(return_value=response)
with patch.object(BaseDownloader, "_download_pdf", return_value=None):
result = downloader.download_with_result(url)
assert not result.is_success
assert result.status_code == 403
assert "subscription" not in result.skip_reason.lower()
failure = record_failure(result, url, text_only=text_only)
assert failure.error_type == "oa_bot_block"
assert not failure.is_permanent()
assert failure.retry_after >= timedelta(hours=1)
@pytest.mark.parametrize("status_code", [403, None])
def test_legacy_open_access_403_message_uses_retryable_classification(
status_code,
):
failure = FailureClassifier().classify_failure(
"download_failed",
status_code=status_code,
url="https://www.mdpi.com/article",
details=(
"PDF extraction failed: Access denied (403) - article requires "
"subscription or special permissions"
),
)
assert failure.error_type == "oa_bot_block"
assert not failure.is_permanent()
@pytest.mark.parametrize(
"url",
[
"https://publisher.example/article",
"https://mdpi.com.example.org/article",
"https://example.org/mdpi.com/article",
"https://[invalid/article",
"",
],
)
def test_other_forbidden_sources_keep_existing_classification(url):
failure = FailureClassifier().classify_failure(
"download_failed", status_code=403, url=url
)
assert failure.error_type == "forbidden"
assert failure.is_permanent()
def test_confirmed_subscription_stays_permanent():
failure = FailureClassifier().classify_failure(
"download_failed",
url="https://publisher.example/article",
details="Article requires subscription",
)
assert failure.error_type == "paywall_or_login"
assert failure.is_permanent()
def test_generic_downloader_rate_limit_diagnostic_is_explicit():
"""GenericDownloader names the rate limit instead of a generic error.
Revert-catcher: removing generic.py's 429 branch restores the
"Unable to access article - server returned error code 429" fall-through
and the skip_reason equality below fails.
"""
url = "https://publisher.example/article.pdf"
response = MagicMock(status_code=429)
response.__enter__.return_value = response
with GenericDownloader() as downloader:
downloader.session.get = Mock(return_value=response)
with patch.object(BaseDownloader, "_download_pdf", return_value=None):
result = downloader.download_with_result(url)
assert not result.is_success
assert result.skip_reason == "Rate limited (429) - retry later"
assert result.status_code == 429
failure = record_failure(result, url)
assert failure.error_type == "rate_limited"
assert not failure.is_permanent()
@pytest.mark.parametrize("text_only", [False, True])
def test_semantic_scholar_403_access_block_remains_retryable(text_only):
"""A 403 from the Semantic Scholar API is a bot block, not a paywall.
Revert-catcher: dropping semanticscholar.org from _OPEN_ACCESS_HOSTS
reclassifies both paths to permanent `forbidden` and the error_type
assertion below fails.
"""
with SemanticScholarDownloader() as downloader:
downloader.session.get = Mock(return_value=Mock(status_code=403))
result = downloader.download_with_result(PAPER_URL)
assert not result.is_success
assert result.status_code == 403
assert "subscription" not in result.skip_reason.lower()
failure = record_failure(result, PAPER_URL, text_only=text_only)
assert failure.error_type == "oa_bot_block"
assert not failure.is_permanent()
assert failure.retry_after >= timedelta(hours=1)
def test_fallback_403_on_semantic_scholar_host_is_an_access_block():
"""The fallback GenericDownloader's 403 on the Semantic Scholar host
classifies as a transient access block too.
The reason comes from the real downloader rather than a literal, so it is
always one the code emits. Revert-catcher: without the host in
_OPEN_ACCESS_HOSTS, access_denied_reason() returns the destination-
forbidden reason, which classifies as permanent `forbidden`, and the
error_type assertion below fails.
"""
response = MagicMock(status_code=403)
response.__enter__.return_value = response
with GenericDownloader() as downloader:
downloader.session.get = Mock(return_value=response)
with patch.object(BaseDownloader, "_download_pdf", return_value=None):
result = downloader.download_with_result(PAPER_URL)
assert not result.is_success
assert result.status_code == 403
failure = record_failure(result, PAPER_URL)
assert failure.error_type == "oa_bot_block"
assert not failure.is_permanent()
@pytest.mark.parametrize("status_code", [403, None])
def test_open_access_bare_403_forbidden_text_is_retryable(status_code):
"""Bare "403 Forbidden" diagnostics on public hosts are access blocks.
Revert-catcher: without the `403 forbidden` alternative in the rescue
regex the text-only path falls through to permanent `forbidden`.
"""
failure = FailureClassifier().classify_failure(
"download_failed",
status_code=status_code,
url="https://www.mdpi.com/article",
details="PDF extraction failed: 403 Forbidden",
)
assert failure.error_type == "oa_bot_block"
assert not failure.is_permanent()
@pytest.mark.parametrize("status_code", [403, None])
def test_recaptcha_challenge_keeps_its_three_day_cooldown(status_code):
"""A named anti-bot challenge outranks the OA-403 access-block rescue.
Revert-catcher: the bare "403 Forbidden" text matches the rescue regex,
and without recaptcha precedence the rescue shortens the deliberate
3-day recaptcha cooldown to the 1h `oa_bot_block` window.
"""
failure = FailureClassifier().classify_failure(
"download_failed",
status_code=status_code,
url="https://arxiv.org/abs/2301.12345",
details="arXiv 403 Forbidden: recaptcha challenge",
)
assert failure.error_type == "recaptcha_protection"
assert not failure.is_permanent()
assert failure.retry_after == timedelta(days=3)
def test_recaptcha_wording_beats_the_access_block_rescue_on_open_access_hosts():
"""Revert-catcher: without the rescue-side recaptcha check this becomes
the 1h `oa_bot_block` classification instead of the 3-day cooldown."""
failure = FailureClassifier().classify_failure(
"download_failed",
status_code=403,
url="https://www.mdpi.com/article",
details="Access denied (403) - recaptcha challenge",
)
assert failure.error_type == "recaptcha_protection"
assert failure.retry_after == timedelta(days=3)
def test_recaptcha_wording_beats_rate_limit_wording_on_arxiv():
"""Rate-limit wording must not shorten arXiv's 3-day reCAPTCHA cooldown.
Revert-catcher: without the `not recaptcha_detected` guard on the
rate-limit text check, "rate limited" matches first and this returns
`rate_limited` (6h for arxiv.org) before the arXiv reCAPTCHA branch is
reached, so both assertions below fail.
"""
failure = FailureClassifier().classify_failure(
"str",
url="https://arxiv.org/abs/2301.12345",
details="arXiv rate limited: recaptcha challenge",
)
assert failure.error_type == "recaptcha_protection"
assert failure.retry_after == timedelta(days=3)