~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
"""
Failure Classification System with Inheritance
Provides base classes and specific failure types for download attempts.
Uses inheritance to organize different failure categories and their retry behavior.
"""
from abc import ABC
from datetime import datetime, timedelta, UTC
import re
from typing import Optional
from urllib.parse import urlparse
from loguru import logger
from ...security import (
failure_reason_for_log,
url_authority_without_userinfo,
)
class BaseFailure(ABC):
"""Base class for all failure types with common functionality"""
def __init__(
self,
error_type: str,
message: str,
retry_after: Optional[timedelta] = None,
):
"""
Initialize a failure classification.
Args:
error_type: Machine-readable error identifier
message: Human-readable error description
retry_after: When this failure can be retried (None = permanent)
"""
self.error_type = error_type
self.message = message
self.retry_after = retry_after
self.created_at = datetime.now(UTC)
logger.debug(
f"Created {self.__class__.__name__}: {error_type} - {message}"
)
def is_permanent(self) -> bool:
"""Check if this is a permanent failure (never retry)"""
return self.retry_after is None
class PermanentFailure(BaseFailure):
"""Resources that should never be retried"""
def __init__(self, error_type: str, message: str):
super().__init__(error_type, message, retry_after=None)
class TemporaryFailure(BaseFailure):
"""Resources that can be retried after cooldown"""
def __init__(self, error_type: str, message: str, cooldown: timedelta):
super().__init__(error_type, message, retry_after=cooldown)
class RateLimitFailure(TemporaryFailure):
"""Domain-specific rate limit handling with longer cooldowns"""
def __init__(self, domain: str, details: str = ""):
# Domain-specific cooldown periods
domain_cooldowns = {
"arxiv.org": timedelta(
hours=6
), # General arXiv rate limiting (reCAPTCHA handled separately)
"pubmed.ncbi.nlm.nih.gov": timedelta(hours=2), # PubMed rate limits
"biorxiv.org": timedelta(hours=6), # BioRxiv rate limits
"semanticscholar.org": timedelta(
hours=4
), # Semantic Scholar rate limits
"researchgate.net": timedelta(hours=12), # ResearchGate rate limits
"default": timedelta(
hours=1
), # Default cooldown for unknown domains
}
# Exact authority first, then a parent-domain match: real URL
# authorities carry subdomains ("www.semanticscholar.org"), which an
# exact-match lookup would drop to the default cooldown. The authority
# is normalised for the lookup only (case, an explicit port, a
# trailing root dot); the message keeps the domain as given.
host = (domain or "").strip().lower()
if host.startswith("["):
host = host.split("]", 1)[0] + "]"
else:
host = host.rsplit(":", 1)[0] if ":" in host else host
host = host.rstrip(".")
cooldown = domain_cooldowns.get(host)
if cooldown is None:
cooldown = next(
(
value
for key, value in domain_cooldowns.items()
if key != "default" and host.endswith("." + key)
),
domain_cooldowns["default"],
)
message = f"Rate limited by {domain}"
if details:
message += f" - {details}"
super().__init__("rate_limited", message, cooldown)
self.domain = domain
class OperatorFixableFailure(TemporaryFailure):
"""A refusal that only a change to the operator's own settings cures.
The source served the file; a local setting (the PDF storage size
limit) refused it. Repeating the download changes nothing until the
operator changes that setting, and it says nothing about the source, so
the status tracker does not treat it as a failing source (see
``ResourceStatusTracker._apply_failure``): every refusal holds the
resource back for the same ``cooldown``, so a bulk run does not fetch
the file again right away, but it never escalates along
``compute_retry_cooldown``'s schedule, is never promoted to
``permanently_failed`` and does not count toward ``MAX_TOTAL_RETRIES``.
Once the operator changes the setting, the resource is eligible again
after one ``cooldown``, however often it was refused before.
"""
# Caller-safe skip-reason tokens emitted by
# ``security.client_safe_errors.client_safe_download_message`` (PR #6564).
# ``RetryManager.record_attempt`` receives the token as the string
# ``details`` (with ``error_type`` collapsed to the literal ``"str"``), so
# the classifier must recognise the token vocabulary in *both* fields —
# otherwise ``network_timeout`` falls through to ``unknown_error`` (1h +
# "Unclassified error" warning) where the pre-token text classified as
# ``timeout`` (30-min). All entries stay Temporary (no retryable -> terminal
# flip); the token itself is preserved as ``error_type`` so the UI
# distinction survives. Every token that ``CLIENT_SAFE_DOWNLOAD_MESSAGES``
# can emit needs an entry here (a test sweeps the whole vocabulary). The
# ones listed in ``_OPERATOR_FIXABLE_TOKENS`` are refused by the operator's
# own settings rather than failed by the source.
_CLIENT_SAFE_TOKEN_FAILURES: dict[str, tuple[str, str, timedelta]] = {
"network_timeout": ("timeout", "Request timed out", timedelta(minutes=30)),
"network_unavailable": (
"network_error",
"Network connectivity issue",
timedelta(minutes=5),
),
"network_reset": (
"network_error",
"Network connectivity issue",
timedelta(minutes=5),
),
"http_error": ("http_error", "HTTP error", timedelta(hours=1)),
"network_redirect_limit": (
"network_redirect_limit",
"Too many redirects",
timedelta(hours=1),
),
"database_constraint": (
"database_constraint",
"Database constraint violation",
timedelta(hours=1),
),
"database_unavailable": (
"database_unavailable",
"Database temporarily unavailable",
timedelta(hours=1),
),
"database_error": (
"database_error",
"Database error",
timedelta(hours=1),
),
"database_data_error": (
"database_data_error",
"Database data error",
timedelta(hours=1),
),
"database_interface_error": (
"database_interface_error",
"Database interface error",
timedelta(hours=1),
),
"database_invalid_request": (
"database_invalid_request",
"Database invalid request",
timedelta(hours=1),
),
"pdf_parse_failed": (
"pdf_parse_failed",
"PDF parsing failed",
timedelta(hours=1),
),
# The configured PDF storage size limit refuses the PDF (see
# ``download_service.PDFStorageLimitError``). ``download_resource``
# returns it, without downloading anything, for a document that keeps
# its text because the limit refused its PDF, and does not record it
# with the retry manager: the limit itself decides when the PDF is
# downloaded again, and a cooldown would also hold back the text routes.
# The entry keeps the token classified, as operator-fixable, for any
# caller that does record it: held back a day at a time, never
# escalated or made permanent.
"pdf_storage_size_limit": (
"pdf_storage_size_limit",
"PDF exceeds the configured storage size limit",
timedelta(days=1),
),
}
# Tokens in ``_CLIENT_SAFE_TOKEN_FAILURES`` that name a refusal by the
# operator's own settings. They classify as ``OperatorFixableFailure``: no
# escalation, no permanent failure, no retry-budget use.
_OPERATOR_FIXABLE_TOKENS = frozenset({"pdf_storage_size_limit"})
_OPEN_ACCESS_HOSTS = frozenset(
{
"arxiv.org",
"biorxiv.org",
"medrxiv.org",
"mdpi.com",
"plos.org",
"biomedcentral.com",
"frontiersin.org",
"pmc.ncbi.nlm.nih.gov",
"europepmc.org",
# The Semantic Scholar API is bot-block-prone (Cloudflare-fronted)
# and names no subscription of its own; a 403 there is an access
# block, not a paywall (#7588).
"semanticscholar.org",
}
)
_OPEN_ACCESS_BLOCK_REASON = (
"Open access source refused the automated download (403)"
)
_DESTINATION_FORBIDDEN_REASON = (
"Access denied (403) - destination refused the download"
)
def _is_open_access_source(url: str) -> bool:
"""Recognize public article hosts and their subdomains."""
try:
hostname = urlparse(url).hostname
except ValueError:
return False
return bool(
hostname
and any(
hostname == host or hostname.endswith("." + host)
for host in _OPEN_ACCESS_HOSTS
)
)
def access_denied_reason(url: str) -> str:
"""Describe the terminal response's 403 without returning its URL.
These fixed reasons retain the destination's classification through PDF
and text-only retry recording, whose resource URL stays canonical even
when the request followed a redirect.
"""
return (
_OPEN_ACCESS_BLOCK_REASON
if _is_open_access_source(url)
else _DESTINATION_FORBIDDEN_REASON
)
_PAYWALL_PATTERNS = (
"requires login",
"subscription",
"paywall",
"requires authentication",
)
class FailureClassifier:
"""Classifies download failures into appropriate types based on error patterns"""
def classify_failure(
self,
error_type: str,
status_code: Optional[int] = None,
url: str = "",
details: str = "",
) -> BaseFailure:
"""
Classify a download failure based on error information.
Args:
error_type: Error type identifier
status_code: HTTP status code if available
url: URL that failed
details: Additional error details
Returns:
Appropriate failure classification
"""
details_lower = (details or "").lower()
pdf_extraction_wrapper = "pdf extraction failed: "
unwrapped_details_lower = (
details_lower[len(pdf_extraction_wrapper) :]
if details_lower.startswith(pdf_extraction_wrapper)
else details_lower
)
# A downloader that inspected the terminal response can classify a
# redirect's destination more accurately than the canonical source
# URL. Keep its fixed reason across retry-recording paths.
if (
status_code in (None, 403)
and unwrapped_details_lower == _DESTINATION_FORBIDDEN_REASON.lower()
):
return PermanentFailure("forbidden", "Access forbidden (403)")
# A named anti-bot challenge (reCAPTCHA or a generic captcha wall) is
# the deliberate 3-day cooldown — it outranks the one-hour OA-403
# access-block rescue below, which would otherwise shorten it.
recaptcha_detected = (
"recaptcha" in details_lower or "captcha" in details_lower
)
# A public article host can refuse an automated fetch without the
# article requiring a subscription. Text-only extraction paths retain
# the diagnostic but do not carry the structured HTTP status.
access_denied = "access denied" in details_lower or re.search(
r"\b(?:http|status(?: code)?|error code)\s+403\b|\(403\)"
r"|403\s+forbidden",
details_lower,
)
if (status_code == 403 or (status_code is None and access_denied)) and (
unwrapped_details_lower == _OPEN_ACCESS_BLOCK_REASON.lower()
or _is_open_access_source(url)
):
if recaptcha_detected:
return TemporaryFailure(
"recaptcha_protection",
"Anti-bot protection active, retry after 3 days",
timedelta(days=3),
)
return TemporaryFailure(
"oa_bot_block",
_OPEN_ACCESS_BLOCK_REASON,
timedelta(hours=1),
)
# HTTP Status Code classifications
if status_code:
if status_code == 202:
return TemporaryFailure(
"processing",
"Request accepted but not yet ready (202)",
timedelta(minutes=30),
)
if status_code == 404:
return PermanentFailure("not_found", "Resource not found (404)")
if status_code == 403:
return PermanentFailure("forbidden", "Access forbidden (403)")
if status_code == 410:
return PermanentFailure(
"gone", "Resource permanently removed (410)"
)
if status_code == 429:
# netloc minus userinfo: the domain lands in the failure
# message, which is logged and persisted.
domain = (
url_authority_without_userinfo(url) if url else "unknown"
)
return RateLimitFailure(domain, details)
if status_code == 503:
return TemporaryFailure(
"server_error",
"Service temporarily unavailable (503)",
timedelta(hours=1),
)
# Caller-safe token vocabulary (PR #6564). Tokens arrive as the
# ``details`` string with ``error_type == "str"`` via
# ``RetryManager.record_attempt`` (or as ``error_type`` directly
# once callers pass it through), so check both fields. Exact match
# only — free-text details must keep falling through to the pattern
# branches below.
for candidate in (error_type, details):
token = (candidate or "").strip().lower()
if not token:
continue
if token in _CLIENT_SAFE_TOKEN_FAILURES:
err, msg, cooldown = _CLIENT_SAFE_TOKEN_FAILURES[token]
if token in _OPERATOR_FIXABLE_TOKENS:
return OperatorFixableFailure(err, msg, cooldown)
return TemporaryFailure(err, msg, cooldown)
if token.startswith("filesystem_error:"):
return TemporaryFailure(
"filesystem_error",
"Filesystem error",
timedelta(hours=1),
)
if token.startswith("download_error:"):
cls_name = token.split(":", 1)[1].strip()
if "timeout" in cls_name or "timed out" in cls_name:
return TemporaryFailure(
"timeout", "Request timed out", timedelta(minutes=30)
)
if (
"connection" in cls_name
or "network" in cls_name
or "reset" in cls_name
):
return TemporaryFailure(
"network_error",
"Network connectivity issue",
timedelta(minutes=5),
)
return TemporaryFailure(
f"download_error:{cls_name}",
f"Download failed ({cls_name})",
timedelta(hours=1),
)
# Error message pattern classifications
error_lower = error_type.lower()
# Rate-limit wording only stands in for a missing status code: with a
# status present, the status branches above already decided. Any named
# anti-bot challenge ("captcha" text, on any host) skips this check, so
# the arXiv branch below can still apply its 3-day cooldown, and paywall
# wording wins so a journal name such as "Journal of Rate Limiting" in
# "Article requires subscription to ..." stays a paywall. URLs are
# blanked first so a path or host like ".../rate-limiting-paper" is not
# read as the diagnostic.
is_paywall_text = any(
pattern in details_lower for pattern in _PAYWALL_PATTERNS
)
if (
status_code is None
and not recaptcha_detected
and not is_paywall_text
and re.search(
r"\b(?:http|status(?: code)?|error code)\s+429\b"
r"|\brate[ -]limit(?:ed|ing)?\b|\(429\)",
re.sub(r"[a-z][a-z0-9+.-]*://\S*", " ", details_lower),
)
):
domain = url_authority_without_userinfo(url) if url else "unknown"
return RateLimitFailure(domain, details)
# GenericDownloader's neutral diagnostic identifies a format mismatch;
# it is not evidence that the source itself is inaccessible. Keep this
# exact so actual access failures retain their existing classifications.
# download_service._fallback_pdf_extraction (the download_as_text path)
# wraps the downloader's reason as "PDF extraction failed: {reason}"
# before it reaches here, so strip that one known wrapper prefix and
# still require an exact match on what's left.
html_not_pdf_reason = "source is an html page, not a pdf"
if unwrapped_details_lower == html_not_pdf_reason:
return TemporaryFailure(
"html_not_pdf",
"Source is an HTML page, not a PDF",
timedelta(hours=1),
)
# arXiv specific patterns
if "arxiv" in error_lower or "arxiv" in details_lower:
if recaptcha_detected:
return TemporaryFailure(
"recaptcha_protection",
"Anti-bot protection active, retry after 3 days",
timedelta(days=3),
)
if "not a pdf file" in details_lower:
return PermanentFailure(
"incompatible_format", "Content is not a PDF file"
)
if (
"html" in details_lower
and "application/pdf" not in details_lower
):
return PermanentFailure(
"incompatible_format",
"Content returned HTML instead of PDF",
)
# Login/subscription/paywall errors — these are permanent
if is_paywall_text:
return PermanentFailure(
"paywall_or_login",
"Requires login or subscription",
)
# HTML content instead of PDF (non-arXiv) — permanent
if "text/html" in details_lower and (
"unexpected content type" in details_lower
or "not a pdf" in details_lower
):
return PermanentFailure(
"incompatible_format",
"Content returned HTML instead of PDF",
)
# Access denied patterns in error messages (without status code)
if (
"access denied" in details_lower
or "403 forbidden" in details_lower
or "http 403" in details_lower
):
return PermanentFailure(
"forbidden", "Access denied based on error message"
)
# Queued/processing patterns in error messages (without status code)
if (
"error code 202" in details_lower
or "http 202" in details_lower
or "status code 202" in details_lower
):
return TemporaryFailure(
"processing",
"Request accepted but not yet ready based on error message",
timedelta(minutes=30),
)
# Common timeout and network errors
if "timeout" in error_lower or "timed out" in details_lower:
return TemporaryFailure(
"timeout", "Request timed out", timedelta(minutes=30)
)
if "connection" in error_lower or "network" in error_lower:
return TemporaryFailure(
"network_error",
"Network connectivity issue",
timedelta(minutes=5),
)
# Fetch that failed after a URL was resolved; last so narrower branches win
if "download failed" in details_lower:
return TemporaryFailure(
"download_failed",
"Resolved URL could not be downloaded",
timedelta(hours=1),
)
# Default to temporary failure with 1-hour cooldown
logger.warning(
f"[FAILURE_CLASSIFIER] Unclassified error: {error_type} - "
f"{failure_reason_for_log(details)}"
)
return TemporaryFailure(
"unknown_error", f"Unknown error: {error_type}", timedelta(hours=1)
)
def classify_from_exception(
self, exception: Exception, url: str = ""
) -> BaseFailure:
"""Classify failure from exception object"""
error_type = type(exception).__name__
details = str(exception)
return self.classify_failure(error_type, details=details, url=url)