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.

fix(library): classify arXiv by paper identity, not by host (#6455)

Lukas Buckfdd257f761a2
diff --git a/changelog.d/6455.bugfix.md b/changelog.d/6455.bugfix.md
new file mode 100644
index 0000000000..3157b911fa
--- /dev/null
+++ b/changelog.d/6455.bugfix.md
@@ -0,0 +1,10 @@
+Library entries are now classified as arXiv by paper identity rather than by
+host. The `is_arxiv` flag follows the same `is_arxiv_paper_url` /
+`extract_arxiv_id` rule the downloader, the extraction pipeline and the URL
+classifier already use, so an arXiv listing or help page is no longer recorded
+as a paper.
+
+Stored PDF filenames go through the same parser. Old-style identifiers such as
+`cs.AI/0701001` now produce a stable name (`arxiv_cs.AI_0701001.pdf`, with the
+separator replaced) instead of being missed by the numeric-only pattern, and the
+version suffix is stripped so v1 and v2 of one paper keep sharing a name.
diff --git a/src/local_deep_research/research_library/services/library_service.py b/src/local_deep_research/research_library/services/library_service.py
index 097dbc4726..862da4bd1b 100644
--- a/src/local_deep_research/research_library/services/library_service.py
+++ b/src/local_deep_research/research_library/services/library_service.py
@@ -17,6 +17,7 @@ from sqlalchemy import or_, func, case
from sqlalchemy.orm import aliased, defer
from ...constants import FILE_PATH_SENTINELS
+from ...utilities.arxiv import is_arxiv_paper_url
from ...database.models.download_tracker import DownloadTracker
from ...database.models.library import (
Collection,
@@ -85,15 +86,16 @@ class LibraryService:
return str(abs_path) if abs_path else None
def _is_arxiv_url(self, url: str) -> bool:
- """Check if URL is from arXiv domain."""
- try:
- hostname = urlparse(url).hostname
- return bool(
- hostname
- and (hostname == "arxiv.org" or hostname.endswith(".arxiv.org"))
- )
- except Exception:
- return False
+ """Whether the URL names one specific arXiv paper.
+
+ Paper identity, not host membership (#5617): every routing decision,
+ the downloader's ``can_handle`` and the citation source label follow
+ :func:`extract_arxiv_id`. A listing such as
+ ``https://arxiv.org/list/cs.AI/recent`` is on the host but carries no
+ identifier, so reporting ``is_arxiv: true`` for it told the UI
+ something the pipeline disagreed with (#6413).
+ """
+ return is_arxiv_paper_url(url)
def _is_pubmed_url(self, url: str) -> bool:
"""Check if URL is from PubMed or NCBI domains."""
diff --git a/src/local_deep_research/research_library/services/pdf_storage_manager.py b/src/local_deep_research/research_library/services/pdf_storage_manager.py
index c6e339ae58..bbee81146e 100644
--- a/src/local_deep_research/research_library/services/pdf_storage_manager.py
+++ b/src/local_deep_research/research_library/services/pdf_storage_manager.py
@@ -18,6 +18,7 @@ from loguru import logger
from sqlalchemy.orm import Session
from ...constants import FILE_PATH_SENTINELS
+from ...utilities.arxiv import extract_arxiv_id
from ...database.models.library import Document, DocumentBlob
from ...security.path_validator import PathValidator
@@ -81,6 +82,11 @@ def resolve_pdf_storage_mode(mode):
return mode
+# `extract_arxiv_id` keeps the version (`2401.12345v2`); the stored filename
+# has never carried one, and two versions of one paper share a resource.
+_ARXIV_VERSION_SUFFIX = re.compile(r"v\d+$")
+
+
class PDFStorageManager:
"""Unified interface for PDF storage across all modes."""
@@ -490,12 +496,18 @@ class PDFStorageManager:
hostname = parsed_url.hostname or ""
timestamp = datetime.now(UTC).strftime("%Y%m%d")
- if hostname == "arxiv.org" or hostname.endswith(".arxiv.org"):
- # Extract arXiv ID
- match = re.search(r"(\d{4}\.\d{4,5})", url)
- if match:
- return f"arxiv_{match.group(1)}.pdf"
- return f"arxiv_{timestamp}_{resource_id or 'unknown'}.pdf"
+ # Paper identity rather than host membership (#6413): an arXiv URL that
+ # names no paper is handled by the generic pipeline, so naming its file
+ # `arxiv_...` claimed a provenance nothing else agreed with.
+ arxiv_id = extract_arxiv_id(url)
+ if arxiv_id:
+ # Two shapes the previous `\d{4}\.\d{4,5}` search did not produce:
+ # old-style identifiers carry a slash (`cs.AI/0701001`), which is a
+ # path separator, and every identifier may carry a version. The
+ # version is dropped so the stored name stays what it has always
+ # been for the same paper.
+ bare = _ARXIV_VERSION_SUFFIX.sub("", arxiv_id)
+ return f"arxiv_{bare.replace('/', '_')}.pdf"
if hostname == "ncbi.nlm.nih.gov" and "/pmc" in parsed_url.path:
# Extract PMC ID
diff --git a/tests/research_library/services/test_library_service.py b/tests/research_library/services/test_library_service.py
index 4bf1f461ea..77786865c0 100644
--- a/tests/research_library/services/test_library_service.py
+++ b/tests/research_library/services/test_library_service.py
@@ -37,6 +37,37 @@ class TestLibraryServiceUrlDetection:
is True
)
+ def test_is_arxiv_url_needs_a_paper_not_just_the_host(self):
+ """#5617 moved arXiv ownership from "a URL on an arXiv host" to "a URL
+ that names a paper". This consumer still answered the host question, so
+ the API reported `is_arxiv: true` for pages every routing decision
+ treats as generic (#6413)."""
+ from local_deep_research.research_library.services.library_service import (
+ LibraryService,
+ )
+
+ with patch.object(
+ LibraryService, "__init__", lambda self, username: None
+ ):
+ service = LibraryService.__new__(LibraryService)
+ service.username = "test_user"
+
+ # On the host, no identifier: the generic pipeline handles these.
+ assert (
+ service._is_arxiv_url("https://arxiv.org/list/cs.AI/recent")
+ is False
+ )
+ assert (
+ service._is_arxiv_url("https://info.arxiv.org/help/index.html")
+ is False
+ )
+ assert service._is_arxiv_url("https://arxiv.org/") is False
+ # ...and a paper URL is still a paper URL, including old-style ids.
+ assert (
+ service._is_arxiv_url("https://arxiv.org/abs/cs.AI/0701001v2")
+ is True
+ )
+
def test_is_arxiv_url_with_non_arxiv_domain(self):
"""Rejects non-arXiv URLs."""
from local_deep_research.research_library.services.library_service import (
diff --git a/tests/research_library/services/test_pdf_filename_generation.py b/tests/research_library/services/test_pdf_filename_generation.py
index 1ddc0030b6..a9159295b9 100644
--- a/tests/research_library/services/test_pdf_filename_generation.py
+++ b/tests/research_library/services/test_pdf_filename_generation.py
@@ -30,22 +30,49 @@ def manager(tmp_path):
class TestGenerateFilenameEdgeCases:
"""Edge cases not covered by test_pdf_storage_manager_edge_cases.py."""
- def test_arxiv_url_without_valid_id_fallback(self, manager):
- """arXiv URL with no matching ID pattern → fallback with timestamp."""
+ def test_arxiv_host_without_a_paper_is_not_named_arxiv(self, manager):
+ """An arXiv URL that names no paper is routed as a generic page, so
+ claiming `arxiv_` provenance in the filename asserted something the
+ pipeline disagreed with (#6413)."""
result = manager._generate_filename(
"https://arxiv.org/some/page", 42, "fallback.pdf"
)
- assert result.startswith("arxiv_")
- assert "42" in result
- assert result.endswith(".pdf")
+ assert result == "fallback.pdf"
- def test_arxiv_url_resource_id_none(self, manager):
- """arXiv URL without valid ID and resource_id=None → 'unknown'."""
+ def test_a_listing_url_is_not_named_arxiv(self, manager):
+ """The URL from the issue: on the host, no identifier."""
result = manager._generate_filename(
- "https://arxiv.org/some/page", None, "fallback.pdf"
+ "https://arxiv.org/list/cs.AI/recent", None, "fallback.pdf"
)
- assert "unknown" in result
- assert result.startswith("arxiv_")
+ assert result == "fallback.pdf"
+
+ def test_old_style_identifier_is_filename_safe(self, manager):
+ """The shared parser accepts `cs.AI/0701001v2`, which the previous
+ `\\d{4}\\.\\d{4,5}` regex silently did not. Its slash is a path
+ separator, so it must not reach the filename."""
+ result = manager._generate_filename(
+ "https://arxiv.org/abs/cs.AI/0701001v2", 7, "fallback.pdf"
+ )
+ assert result == "arxiv_cs.AI_0701001.pdf"
+ assert "/" not in result
+
+ def test_the_version_does_not_reach_the_filename(self, manager):
+ """`extract_arxiv_id` keeps the version, the stored name never has.
+ Both versions of one paper are one library resource, so v1 and v2 must
+ not land in the store as two differently named files."""
+ v1 = manager._generate_filename(
+ "https://arxiv.org/pdf/2401.12345v1", 7, "fallback.pdf"
+ )
+ v2 = manager._generate_filename(
+ "https://arxiv.org/pdf/2401.12345v2", 7, "fallback.pdf"
+ )
+ assert v1 == v2 == "arxiv_2401.12345.pdf"
+
+ def test_a_paper_url_still_carries_its_identifier(self, manager):
+ result = manager._generate_filename(
+ "https://arxiv.org/pdf/2301.00001.pdf", 7, "fallback.pdf"
+ )
+ assert result == "arxiv_2301.00001.pdf"
def test_pubmed_url_without_pmc_id(self, manager):
"""PMC URL without PMC ID → fallback with timestamp."""
@@ -72,12 +99,11 @@ class TestGenerateFilenameEdgeCases:
assert result == "fallback.pdf"
def test_arxiv_subdomain_without_valid_id(self, manager):
- """export.arxiv.org with no valid arXiv ID → fallback."""
+ """export.arxiv.org with no valid arXiv ID → the generic fallback."""
result = manager._generate_filename(
"https://export.arxiv.org/noarxivid", 5, "fallback.pdf"
)
- assert result.startswith("arxiv_")
- assert "5" in result
+ assert result == "fallback.pdf"
def test_ncbi_non_pmc_path_returns_fallback(self, manager):
"""ncbi.nlm.nih.gov but not /pmc path → returns fallback."""
diff --git a/tests/research_library/services/test_pdf_storage_manager_coverage.py b/tests/research_library/services/test_pdf_storage_manager_coverage.py
index 4d678efde6..fdf00bcc15 100644
--- a/tests/research_library/services/test_pdf_storage_manager_coverage.py
+++ b/tests/research_library/services/test_pdf_storage_manager_coverage.py
@@ -327,23 +327,20 @@ class TestGenerateFilename:
assert fn == "arxiv_2301.54321.pdf"
def test_arxiv_no_id_in_url(self, tmp_path):
- """arXiv URL without recognizable ID falls back to timestamp."""
+ """arXiv URL without a recognizable ID is a generic page (#6413)."""
mgr = _mgr(tmp_path)
fn = mgr._generate_filename(
"https://arxiv.org/some/other/path", 99, "fallback.pdf"
)
- assert fn.startswith("arxiv_")
- assert fn.endswith(".pdf")
- assert "99" in fn # resource_id used
+ assert fn == "fallback.pdf"
def test_arxiv_no_id_no_resource_id(self, tmp_path):
- """arXiv URL without ID and no resource_id uses 'unknown'."""
+ """Same without a resource id: still the generic fallback (#6413)."""
mgr = _mgr(tmp_path)
fn = mgr._generate_filename(
"https://arxiv.org/some/other/path", None, "fallback.pdf"
)
- assert "unknown" in fn
- assert fn.startswith("arxiv_")
+ assert fn == "fallback.pdf"
# -- PubMed / PMC --
diff --git a/tests/research_library/test_download_service_contracts.py b/tests/research_library/test_download_service_contracts.py
index 95707c241b..84baa3da2a 100644
--- a/tests/research_library/test_download_service_contracts.py
+++ b/tests/research_library/test_download_service_contracts.py
@@ -516,7 +516,13 @@ def test_storage_path_comes_from_server_side_values_only(
# arXiv/PMC id branches.
("https://arxiv.org.evil.example.com/pdf/2401.12345", "77.pdf"),
("https://arxiv.org/pdf/2401.12345v2", "arxiv_2401.12345.pdf"),
- ("https://arxiv.org/pdf/../../2401.12345", "arxiv_2401.12345.pdf"),
+ # The id now comes from the same parser the downloader and the
+ # citation label use, and that parser reads the PATH rather than
+ # searching the whole URL for four-dot-five digits. `/pdf/../../…`
+ # normalises to `arxiv.org/2401.12345`, which names no paper — so the
+ # file keeps the server-side name instead of claiming a provenance
+ # nothing else in the pipeline agrees with (#6413).
+ ("https://arxiv.org/pdf/../../2401.12345", "77.pdf"),
(
"https://ncbi.nlm.nih.gov/pmc/articles/PMC777/../../x",
"pmc_PMC777.pdf",