~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
"""Shared arXiv API transport policy (issue #4783).
Sole owner of the arXiv API request policy: every legacy arXiv API query
request must traverse one process-wide, monotonic, at least three-second
single-flight gate and travel through a :class:`SafeSession` with a pinned
timeout and redirects disabled.
"""
from __future__ import annotations
import threading
import time
from collections.abc import Callable, Iterable, Sequence
from contextlib import contextmanager
from dataclasses import dataclass
from datetime import datetime
from enum import StrEnum
from typing import (
TYPE_CHECKING,
Final,
Protocol,
TypeAlias,
assert_never,
override,
)
import arxiv
import requests
from requests import PreparedRequest
from ..security import SafeSession
from .resource_utils import safe_close
if TYPE_CHECKING:
from collections.abc import Generator
from requests import Response
# arXiv API Terms of Use: one connection at a time, with at least three
# seconds between request starts
# (https://info.arxiv.org/help/api/tou.html). This gate is intentionally
# process-local: it serializes request starts within a single LDR process
# only and provides no machine-wide enforcement. Deployments running
# multiple LDR processes must coordinate those processes externally.
_ARXIV_API_REQUEST_INTERVAL_SECONDS: Final = 3.0
_arxiv_api_request_lock = threading.Lock()
_last_request_started_at: float | None = None
_monotonic = time.monotonic
_sleep = time.sleep
_ARXIV_API_TIMEOUT_SECONDS: Final = 10
class ArxivSortCriterion(StrEnum):
RELEVANCE = "relevance"
LAST_UPDATED_DATE = "lastUpdatedDate"
SUBMITTED_DATE = "submittedDate"
class ArxivSortOrder(StrEnum):
ASCENDING = "ascending"
DESCENDING = "descending"
@dataclass(frozen=True, slots=True)
class ArxivQueryRequest:
query: str
max_results: int
sort_by: ArxivSortCriterion = ArxivSortCriterion.RELEVANCE
sort_order: ArxivSortOrder = ArxivSortOrder.DESCENDING
@dataclass(frozen=True, slots=True)
class ArxivIdRequest:
arxiv_id: str
type ArxivRequest = ArxivQueryRequest | ArxivIdRequest
class ArxivAuthor(Protocol):
name: str
class ArxivPaper(Protocol):
entry_id: str
title: str
summary: str
published: datetime
updated: datetime
categories: list[str]
comment: str | None
doi: str | None
journal_ref: str | None
@property
def authors(self) -> Sequence[ArxivAuthor]: ...
@property
def pdf_url(self) -> str | None: ...
_SORT_CRITERIA: Final = {
ArxivSortCriterion.RELEVANCE: arxiv.SortCriterion.Relevance,
ArxivSortCriterion.LAST_UPDATED_DATE: arxiv.SortCriterion.LastUpdatedDate,
ArxivSortCriterion.SUBMITTED_DATE: arxiv.SortCriterion.SubmittedDate,
}
_SORT_ORDERS: Final = {
ArxivSortOrder.ASCENDING: arxiv.SortOrder.Ascending,
ArxivSortOrder.DESCENDING: arxiv.SortOrder.Descending,
}
class _Readable(Protocol):
def read(self, length: int = ..., /) -> str | bytes: ...
class _ItemSource(Protocol):
def items(self) -> Iterable[tuple[_GetArgument, _GetArgument]]: ...
_GetArgument: TypeAlias = (
None
| bool
| int
| float
| str
| bytes
| PreparedRequest
| Iterable["_GetArgument"]
| _Readable
| _ItemSource
| Callable[..., "_GetArgument"]
)
@contextmanager
def arxiv_api_request_gate() -> Generator[None, None, None]:
"""NOT REENTRANT -- never enter this from code that may already
be running under the gate. The lock is a plain threading.Lock held
across both the sleep and the request body, so a nested entry
deadlocks silently rather than raising. _GatedArxivSession.get()
enters it, so anything handed that session as its transport must
not wrap its own call in the gate.
Run one arXiv API request start under the process-wide policy gate.
Entering blocks until at least ``_ARXIV_API_REQUEST_INTERVAL_SECONDS``
monotonic seconds have passed since the previous start, then stamps the
new start while holding the single-flight lock across the whole body.
Failed starts still count for spacing, and the lock is always released.
"""
global _last_request_started_at
with _arxiv_api_request_lock:
while _last_request_started_at is not None:
remaining = (
_last_request_started_at
+ _ARXIV_API_REQUEST_INTERVAL_SECONDS
- _monotonic()
)
if remaining <= 0:
break
_sleep(remaining)
_last_request_started_at = _monotonic()
yield
class _GatedArxivSession(SafeSession):
"""SafeSession routing arXiv API wire calls through the shared gate."""
@override
def get(
self,
url: str | bytes,
params: _GetArgument = None,
**kwargs: _GetArgument,
) -> Response:
request_kwargs: dict[str, _GetArgument] = {
**kwargs,
"timeout": _ARXIV_API_TIMEOUT_SECONDS,
"allow_redirects": False,
}
match url:
case str():
request_url = url
case bytes():
request_url = url.decode()
case unreachable:
assert_never(unreachable)
with arxiv_api_request_gate():
return super().request(
"GET", request_url, params=params, **request_kwargs
)
@contextmanager
def _policy_arxiv_client(
*, page_size: int | None = None
) -> Generator[arxiv.Client, None, None]:
"""Yield an ``arxiv.Client`` adapted to the shared arXiv API policy.
The client is constructed through the ``arxiv.Client`` module attribute
with ``delay_seconds=0`` because the shared gate owns spacing.
:func:`_install_gated_session` then closes the discarded default
session and pins a gated :class:`_GatedArxivSession` onto the
client, raising ``ImportError`` if the client's private ``_session``
attribute is missing or not a ``requests.Session``. The adapted
session is closed on exit for both normal and exceptional scopes.
"""
constructor_kwargs: dict[str, int] = {"delay_seconds": 0}
if page_size is not None:
constructor_kwargs["page_size"] = page_size
client = arxiv.Client(**constructor_kwargs)
adapted_session = _install_gated_session(client)
try:
yield client
finally:
safe_close(adapted_session, "adapted arXiv client session")
def _install_gated_session(client: arxiv.Client) -> _GatedArxivSession:
"""Install the gate-enforcing session on ``client`` or fail loudly.
``arxiv.Client`` keeps its HTTP transport on the private ``_session``
attribute (a ``requests.Session`` in arxiv 2.4.0, 2.4.1 and 4.0.1;
pyproject.toml does not pin the version for this guard's sake, since
the guard fails closed). A future release renaming or retyping that
attribute would leave the client's default session in place and
silently bypass the shared rate-limit gate, so the mutation is
guarded: anything other than a ``requests.Session`` at ``_session``
raises ``ImportError`` with maintainer guidance instead of shipping
an ungated client. The discarded default session is closed, a
:class:`_GatedArxivSession` is installed in its place, and it is
returned so the caller can close it on scope exit.
"""
discarded_session = getattr(client, "_session", None)
if not isinstance(discarded_session, requests.Session):
raise ImportError(
"arxiv.Client no longer exposes a requests.Session at "
"_session, so the shared arXiv rate-limit gate cannot be "
"installed. The installed arxiv release changed the client "
"internals; adapt utilities/arxiv_api.py to them, or constrain "
"arxiv to a release that keeps a requests.Session at _session "
"(2.4.0, 2.4.1 and 4.0.1 do).",
)
safe_close(discarded_session, "discarded arXiv client session")
adapted_session = _GatedArxivSession()
client._session = adapted_session
return adapted_session
def _strip_optional(text: str | None) -> str | None:
return None if text is None else text.strip()
def _strip_feed_text(paper: arxiv.Result) -> arxiv.Result:
"""Strip surrounding whitespace from the text fields LDR reads.
arxiv 2.x parsed the Atom feed with feedparser, which strips the
leading and trailing whitespace of each of these fields. arxiv 4.x
parses it with its own lxml parser and keeps the raw element text,
and the arXiv API returns some of it with surrounding whitespace
(some abstracts start with two spaces). The id, title, abstract,
comment, journal reference, DOI and author names are stripped in
place with ``str.strip()``, as feedparser stripped them, so snippets,
content and author lists carry none of the whitespace 2.x removed.
The SDK already collapses each whitespace run in the title to one
space, which leaves at most one space at each end to strip.
"""
paper.entry_id = paper.entry_id.strip()
paper.title = paper.title.strip()
paper.summary = paper.summary.strip()
paper.comment = _strip_optional(paper.comment)
paper.journal_ref = _strip_optional(paper.journal_ref)
paper.doi = _strip_optional(paper.doi)
for author in paper.authors:
author.name = author.name.strip()
return paper
def fetch_arxiv_results(request: ArxivRequest) -> list[ArxivPaper]:
"""Fetch and materialize one typed arXiv request under shared policy.
The text fields LDR reads from each result are stripped of
surrounding whitespace (``_strip_feed_text``).
"""
match request:
case ArxivQueryRequest():
search = arxiv.Search(
query=request.query,
max_results=request.max_results,
sort_by=_SORT_CRITERIA[request.sort_by],
sort_order=_SORT_ORDERS[request.sort_order],
)
with _policy_arxiv_client(page_size=request.max_results) as client:
return list[ArxivPaper](
map(_strip_feed_text, client.results(search))
)
case ArxivIdRequest():
search = arxiv.Search(id_list=[request.arxiv_id], max_results=1)
with _policy_arxiv_client() as client:
return list[ArxivPaper](
map(_strip_feed_text, client.results(search))
)
case unreachable:
assert_never(unreachable)