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.

4.0 KB
Raw Blame
"""Retry timestamps must survive the DB round-trip timezone-aware.

SQLite does not persist tzinfo. A bare ``sqlalchemy.DateTime`` column
therefore reads back NAIVE even when every writer stores
``datetime.now(UTC)`` — which every writer here does
(``status_tracker.py`` lines ~136, ~149, ~157, ~174).

That produced a silent failure, not a visible one:

  * ``ResourceStatusTracker.get_resource_status()`` returned
    ``retry_after_timestamp.isoformat()`` of a naive value;
  * ``RetryManager`` parsed it back and computed
    ``retry_time - datetime.now(UTC)``, which raises
    ``TypeError: can't subtract offset-naive and offset-aware datetimes``;
  * that call sits inside a bare ``except Exception: logger.debug(...)``,
    so the error was swallowed and ``estimated_wait`` simply stayed
    ``None`` — the retry ETA never appeared in the UI and nothing logged
    at a visible level.

``can_retry()`` had already grown a local ``.replace(tzinfo=UTC)`` patch
for the same root cause, which is why one code path worked and the other
did not. The columns are now ``UtcDateTime`` (as the rest of the app's
models already were), fixing every reader at once — including rows
written before the change, since ``UtcDateTime`` assumes UTC on read.

These tests pin the round-trip and the arithmetic that depends on it. A
revert to a bare ``DateTime`` fails the first test immediately.
"""

from datetime import UTC, datetime, timedelta

import pytest
import sqlalchemy as sa
from sqlalchemy.orm import Session

from local_deep_research.library.download_management.models import (
    Base,
    ResourceDownloadStatus,
)


@pytest.fixture
def session():
    engine = sa.create_engine("sqlite://")
    Base.metadata.create_all(engine)
    with Session(engine) as s:
        yield s


def _store(session, **kwargs):
    row = ResourceDownloadStatus(
        resource_id=1, status="temporarily_failed", **kwargs
    )
    session.add(row)
    session.commit()
    session.expunge_all()
    return session.query(ResourceDownloadStatus).first()


class TestRetryTimestampsRoundTripAware:
    def test_retry_after_timestamp_reads_back_aware(self, session):
        """The column that broke the ETA calculation."""
        stored = _store(
            session,
            retry_after_timestamp=datetime.now(UTC) + timedelta(minutes=5),
        )
        assert stored.retry_after_timestamp.tzinfo is not None
        assert stored.retry_after_timestamp.utcoffset() == timedelta(0)

    @pytest.mark.parametrize(
        "field",
        ["last_attempt_at", "permanent_failure_at"],
    )
    def test_sibling_retry_columns_read_back_aware(self, session, field):
        """The two columns alongside it, same declaration, same hazard."""
        stored = _store(session, **{field: datetime.now(UTC)})
        assert getattr(stored, field).tzinfo is not None

    @pytest.mark.parametrize("field", ["created_at", "updated_at"])
    def test_audit_columns_read_back_aware(self, session, field):
        """created_at/updated_at default to aware values via
        ``partial(datetime.now, UTC)`` and must not be silently naive
        either."""
        stored = _store(session)
        assert getattr(stored, field) is not None
        assert getattr(stored, field).tzinfo is not None


class TestRetryEtaArithmetic:
    def test_isoformat_round_trip_supports_subtraction_from_now(self, session):
        """The exact computation RetryManager performs.

        Reproduces ``retry_manager.py``'s
        ``datetime.fromisoformat(status_info["retry_after_timestamp"])
        - datetime.now(UTC)``. With a naive column this raised TypeError
        and the ETA was silently dropped; it must now yield a real
        positive delta.
        """
        stored = _store(
            session,
            retry_after_timestamp=datetime.now(UTC) + timedelta(minutes=5),
        )
        serialized = stored.retry_after_timestamp.isoformat()

        estimated_wait = datetime.fromisoformat(serialized) - datetime.now(UTC)

        assert estimated_wait > timedelta(0)
        assert estimated_wait <= timedelta(minutes=5)