~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
"""Context-overflow API properties recovered from the four deleted Flask suites.
Ported from ``tests/web/routes/test_context_overflow_{api,api_http,coverage,logic}.py``
(all deleted by the FastAPI migration). ``tests/web/routers/test_context_overflow_contract.py``
is a strong PARTIAL successor — it pins the happy-path response shape against a
seeded SQLite DB — but it never touches any of the following, so deleting each
guard leaves it green:
* the low end of the pagination clamp (``per_page`` 0 / negative) and multi-page
``total_pages`` arithmetic,
* the ``7d`` / ``3m`` / ``1y`` / ``all`` period branches,
* the zero/NULL states (``truncation_rate`` with no context rows,
``avg_tokens_truncated`` with no truncated rows, an all-NULL token row),
* the non-truncated and non-Ollama arms of the chart-data token formula,
* ``chart_data``'s ``provider`` / ``research_phase`` / ``response_time_ms``,
* ``model_token_stats`` (``min_prompt`` and the ``or 0`` fallbacks),
``phase_breakdown``, ``recent_truncated``'s raw ``truncation_ratio``,
``model_stats.avg_context_limit``'s falsy->None arm,
* ``current_context_window``,
* the "/metrics suffix is not a route" pin,
* BOTH handlers' ``except`` -> 500 arms.
The originals drove the route with a fully mocked SQLAlchemy session. That is
no longer possible: the handler now delegates its aggregates to
``get_context_overflow_truncation_summary``, which executes real SQL and
``int()``s the row. Every test below therefore runs the handler's real SQL over
a seeded in-memory database (the technique the contract successor established),
which is a strictly stronger substrate than the mocks it replaces.
"""
from contextlib import contextmanager
from datetime import UTC, datetime, timedelta
from unittest.mock import patch
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from local_deep_research.database.models import Base
from local_deep_research.database.models.metrics import TokenUsage
MODULE = "local_deep_research.web.routers.context_overflow_api"
OVERVIEW_URL = "/api/context-overflow"
RESEARCH_URL = "/api/research/{research_id}/context-overflow"
_NOW = datetime.now(UTC)
@pytest.fixture()
def auth_client():
"""TestClient authenticated as ``testuser`` via dependency override."""
from local_deep_research.web.dependencies.auth import require_auth
from local_deep_research.web.fastapi_app import app
app.dependency_overrides[require_auth] = lambda: "testuser"
try:
yield TestClient(app)
finally:
app.dependency_overrides.pop(require_auth, None)
@contextmanager
def _seeded_db(*rows):
"""Patch the router's ``get_user_db_session`` with a seeded SQLite DB.
StaticPool shares the single in-memory connection between the seeding
session and the route handler (which runs in the TestClient threadpool),
so the route executes its REAL aggregation SQL.
"""
engine = create_engine(
"sqlite://",
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
seed = Session()
seed.add_all(rows)
seed.commit()
seed.close()
@contextmanager
def _ctx(username=None, password=None, **kwargs):
with Session() as session:
yield session
with patch(f"{MODULE}.get_user_db_session", _ctx):
yield
@contextmanager
def _exploding_db():
"""Patch ``get_user_db_session`` with a context manager that raises."""
@contextmanager
def _ctx(*args, **kwargs):
raise RuntimeError("simulated DB failure")
yield # pragma: no cover
with patch(f"{MODULE}.get_user_db_session", _ctx):
yield
def _usage(**overrides) -> TokenUsage:
defaults = dict(
research_id="res-1",
timestamp=_NOW - timedelta(hours=1),
model_provider="openai",
model_name="gpt-4",
prompt_tokens=100,
completion_tokens=50,
total_tokens=150,
context_limit=8192,
context_truncated=False,
tokens_truncated=0,
research_phase="search",
response_time_ms=500,
)
defaults.update(overrides)
return TokenUsage(**defaults)
# ---------------------------------------------------------------------------
# Pagination clamping / arithmetic
# ---------------------------------------------------------------------------
class TestPaginationLogic:
"""Pagination parameter clamping and page arithmetic."""
def test_per_page_zero_clamped_to_1(self, auth_client):
with _seeded_db(_usage()):
resp = auth_client.get(OVERVIEW_URL + "?per_page=0")
assert resp.status_code == 200
assert resp.json()["pagination"]["per_page"] == 1
def test_per_page_negative_clamped_to_1(self, auth_client):
with _seeded_db(_usage()):
resp = auth_client.get(OVERVIEW_URL + "?per_page=-5")
assert resp.status_code == 200
assert resp.json()["pagination"]["per_page"] == 1
def test_page_2_with_items(self, auth_client):
"""page=2, per_page=10 with 15 total → correct pagination metadata."""
rows = [
_usage(timestamp=_NOW - timedelta(minutes=i)) for i in range(15)
]
with _seeded_db(*rows):
resp = auth_client.get(OVERVIEW_URL + "?page=2&per_page=10")
assert resp.status_code == 200
pagination = resp.json()["pagination"]
assert pagination["page"] == 2
assert pagination["per_page"] == 10
assert pagination["total_count"] == 15
assert pagination["total_pages"] == 2
# Page 2 of 15 at 10-per-page holds the remaining 5 rows — proves the
# offset is actually applied, not just echoed in the metadata.
assert len(resp.json()["all_requests"]) == 5
# ---------------------------------------------------------------------------
# Period branches
# ---------------------------------------------------------------------------
class TestPeriods:
"""Every whitelisted period value selects its own time window.
Each case pairs a period with a row just inside and just outside that
window, so both "the period was dropped from the whitelist" (silently
falling back to 30d) and "the wrong timedelta was used" fail.
"""
@pytest.mark.parametrize(
("period", "age_days", "expected"),
[
("7d", 3, 1),
("7d", 20, 0),
("30d", 20, 1),
("30d", 60, 0),
("3m", 60, 1),
("3m", 200, 0),
("1y", 200, 1),
("1y", 500, 0),
("all", 500, 1),
],
)
def test_period_window(self, auth_client, period, age_days, expected):
row = _usage(timestamp=_NOW - timedelta(days=age_days))
with _seeded_db(row):
resp = auth_client.get(OVERVIEW_URL + f"?period={period}")
assert resp.status_code == 200
data = resp.json()
assert data["status"] == "success"
assert data["overview"]["total_requests"] == expected
assert len(data["chart_data"]) == expected
def test_long_period_still_returns_series(self, auth_client):
"""3m/1y take the .limit(500) sampling arm rather than .limit(1000)."""
with _seeded_db(_usage()):
resp = auth_client.get(OVERVIEW_URL + "?period=3m")
assert resp.status_code == 200
assert len(resp.json()["chart_data"]) == 1
# ---------------------------------------------------------------------------
# Empty / null states
# ---------------------------------------------------------------------------
class TestEmptyNullStates:
"""Zero and NULL edge cases must not become NaN, None or a 500."""
def test_zero_records_overview_all_zeros(self, auth_client):
with _seeded_db():
resp = auth_client.get(OVERVIEW_URL)
assert resp.status_code == 200
data = resp.json()
assert data["overview"]["total_requests"] == 0
assert data["overview"]["truncation_rate"] == 0
assert data["chart_data"] == []
def test_requests_with_context_zero_truncation_rate_zero(self, auth_client):
"""requests_with_context=0 → truncation_rate=0 (not a ZeroDivisionError)."""
rows = [_usage(context_limit=None) for _ in range(5)]
with _seeded_db(*rows):
resp = auth_client.get(OVERVIEW_URL)
assert resp.status_code == 200
data = resp.json()
assert data["overview"]["total_requests"] == 5
assert data["overview"]["requests_with_context_data"] == 0
assert data["overview"]["truncation_rate"] == 0
def test_avg_tokens_truncated_none_becomes_zero(self, auth_client):
"""No truncated rows → SQL AVG is NULL → the field reports 0."""
with _seeded_db(_usage(), _usage()):
resp = auth_client.get(OVERVIEW_URL)
assert resp.status_code == 200
assert resp.json()["overview"]["avg_tokens_truncated"] == 0
def test_all_context_truncated_false(self, auth_client):
"""All records have context_truncated=False → truncated_requests=0."""
rows = [_usage(context_truncated=False) for _ in range(3)]
with _seeded_db(*rows):
resp = auth_client.get(OVERVIEW_URL)
assert resp.status_code == 200
overview = resp.json()["overview"]
assert overview["truncated_requests"] == 0
assert overview["truncation_rate"] == 0
def test_all_null_token_fields_no_crash(self, auth_client):
"""A zeroed row with a NULL tokens_truncated renders as zeros, not a 500.
``prompt_tokens``/``completion_tokens``/``total_tokens`` are
``nullable=False`` on TokenUsage, so the original's all-NULL row was
only constructible as a Mock. Zeros exercise the same falsy arms of
the formula, and ``tokens_truncated`` (nullable) is left NULL so the
``or 0`` guard is genuinely under test.
"""
row = _usage(
prompt_tokens=0,
completion_tokens=0,
total_tokens=0,
tokens_truncated=None,
context_limit=None,
)
with _seeded_db(row):
resp = auth_client.get(OVERVIEW_URL)
assert resp.status_code == 200
chart = resp.json()["chart_data"]
assert len(chart) == 1
assert chart[0]["original_prompt_tokens"] == 0
assert chart[0]["tokens_truncated"] == 0
# ---------------------------------------------------------------------------
# chart_data token formula
# ---------------------------------------------------------------------------
class TestChartDataTokenFormula:
"""``original_prompt_tokens`` reconstruction, all four arms."""
def test_ollama_prompt_eval_count_used_when_present(self, auth_client):
"""ollama_prompt_eval_count present → used instead of prompt_tokens."""
row = _usage(
prompt_tokens=100,
ollama_prompt_eval_count=80,
context_truncated=False,
)
with _seeded_db(row):
resp = auth_client.get(OVERVIEW_URL)
assert resp.status_code == 200
chart = resp.json()["chart_data"]
assert len(chart) == 1
assert chart[0]["ollama_prompt_tokens"] == 80
# original = 80 (ollama used), no truncation
assert chart[0]["original_prompt_tokens"] == 80
def test_ollama_none_falls_back_to_prompt_tokens(self, auth_client):
"""ollama_prompt_eval_count=None → falls back to prompt_tokens."""
row = _usage(
prompt_tokens=100,
ollama_prompt_eval_count=None,
context_truncated=False,
)
with _seeded_db(row):
resp = auth_client.get(OVERVIEW_URL)
assert resp.status_code == 200
chart = resp.json()["chart_data"]
assert chart[0]["original_prompt_tokens"] == 100
def test_truncated_adds_tokens_truncated(self, auth_client):
"""context_truncated=True → original = prompt + tokens_truncated."""
row = _usage(
prompt_tokens=100,
ollama_prompt_eval_count=None,
context_truncated=True,
tokens_truncated=500,
)
with _seeded_db(row):
resp = auth_client.get(OVERVIEW_URL)
assert resp.status_code == 200
chart = resp.json()["chart_data"]
assert chart[0]["original_prompt_tokens"] == 600 # 100 + 500
def test_not_truncated_no_addition(self, auth_client):
"""context_truncated=False → original = prompt (no addition)."""
row = _usage(
prompt_tokens=200,
context_truncated=False,
tokens_truncated=0,
)
with _seeded_db(row):
resp = auth_client.get(OVERVIEW_URL)
assert resp.status_code == 200
chart = resp.json()["chart_data"]
assert chart[0]["original_prompt_tokens"] == 200
assert chart[0]["truncated"] is False
class TestChartDataFields:
"""chart_data carries provider / research_phase / response_time_ms."""
def test_chart_data_includes_provider_phase_and_response_time(
self, auth_client
):
row = _usage(
model_provider="ollama",
model_name="llama3",
research_phase="analysis",
response_time_ms=1500,
)
with _seeded_db(row):
resp = auth_client.get(OVERVIEW_URL)
point = resp.json()["chart_data"][0]
assert point["provider"] == "ollama"
assert point["research_phase"] == "analysis"
assert point["response_time_ms"] == 1500
def test_chart_data_fields_handle_none(self, auth_client):
"""The nullable chart fields pass NULL straight through.
``model_provider`` is ``nullable=False`` on TokenUsage, so its None
arm is unreachable with a real row (the original reached it only with
a Mock); the two genuinely nullable columns are pinned here.
"""
row = _usage(
research_phase=None,
response_time_ms=None,
context_limit=None,
)
with _seeded_db(row):
resp = auth_client.get(OVERVIEW_URL)
point = resp.json()["chart_data"][0]
assert point["research_phase"] is None
assert point["response_time_ms"] is None
assert point["context_limit"] is None
# ---------------------------------------------------------------------------
# model_token_stats / phase_breakdown / model_stats / recent_truncated
# ---------------------------------------------------------------------------
class TestModelTokenStatsFields:
"""model_token_stats fields added in the context-overflow rework."""
def test_min_prompt_field_present(self, auth_client):
"""min_prompt is computed from func.min(prompt_tokens)."""
rows = [
_usage(
model_name="llama3",
model_provider="ollama",
prompt_tokens=200,
response_time_ms=1200,
),
_usage(
model_name="llama3",
model_provider="ollama",
prompt_tokens=3000,
response_time_ms=1200,
),
]
with _seeded_db(*rows):
resp = auth_client.get(OVERVIEW_URL)
stats = resp.json()["model_token_stats"]
assert len(stats) == 1
assert stats[0]["min_prompt"] == 200
assert stats[0]["max_prompt"] == 3000
assert stats[0]["avg_response_time_ms"] == 1200
def test_null_aggregates_default_to_zero(self, auth_client):
"""avg_response_time_ms falls back to 0 when the SQL AVG is NULL.
``prompt_tokens`` is ``nullable=False``, so ``min_prompt``'s own
``or 0`` arm cannot be reached with a real row (the original reached
it with a Mock); ``response_time_ms`` IS nullable, so the same
fallback is pinned through it.
"""
row = _usage(
model_name="llama3",
model_provider="ollama",
prompt_tokens=0,
response_time_ms=None,
)
with _seeded_db(row):
resp = auth_client.get(OVERVIEW_URL)
stats = resp.json()["model_token_stats"]
assert len(stats) == 1
assert stats[0]["min_prompt"] == 0
assert stats[0]["avg_response_time_ms"] == 0
class TestPhaseBreakdown:
"""phase_breakdown rows are serialised, with a NULL-phase fallback."""
def test_model_token_stats_and_phase_breakdown_populated(self, auth_client):
row = _usage(
model_name="claude-3",
model_provider="anthropic",
research_phase="synthesis",
total_tokens=900,
)
with _seeded_db(row):
resp = auth_client.get(OVERVIEW_URL)
data = resp.json()
assert len(data["model_token_stats"]) == 1
assert data["model_token_stats"][0]["model"] == "claude-3"
assert data["model_token_stats"][0]["provider"] == "anthropic"
assert len(data["phase_breakdown"]) == 1
assert data["phase_breakdown"][0]["phase"] == "synthesis"
assert data["phase_breakdown"][0]["count"] == 1
assert data["phase_breakdown"][0]["total_tokens"] == 900
def test_null_phase_reported_as_unknown(self, auth_client):
with _seeded_db(_usage(research_phase=None)):
resp = auth_client.get(OVERVIEW_URL)
phases = [row["phase"] for row in resp.json()["phase_breakdown"]]
assert phases == ["unknown"]
class TestModelStats:
"""model_stats truncation aggregation and its falsy avg_context_limit arm."""
def test_avg_context_limit_falsy_reported_as_none(self, auth_client):
"""A zero average context limit is reported as None, not 0."""
with _seeded_db(_usage(context_limit=0), _usage(context_limit=0)):
resp = auth_client.get(OVERVIEW_URL)
stats = resp.json()["model_stats"]
assert len(stats) == 1
assert stats[0]["avg_context_limit"] is None
def test_avg_context_limit_rounded_when_present(self, auth_client):
with _seeded_db(_usage(context_limit=4096), _usage(context_limit=8192)):
resp = auth_client.get(OVERVIEW_URL)
stats = resp.json()["model_stats"]
assert stats[0]["avg_context_limit"] == 6144
class TestRecentTruncated:
"""recent_truncated rows are formatted correctly in the response."""
def test_recent_truncated_entry_shape(self, auth_client):
row = _usage(
context_truncated=True,
tokens_truncated=512,
truncation_ratio=0.0625,
ollama_prompt_eval_count=None,
)
with _seeded_db(row):
resp = auth_client.get(OVERVIEW_URL)
entries = resp.json()["recent_truncated"]
assert len(entries) == 1
assert entries[0]["tokens_truncated"] == 512
# recent_truncated reports the RAW ratio; only all_requests scales it
# to a percentage. Pinning both stops the two from drifting together.
assert entries[0]["truncation_ratio"] == pytest.approx(0.0625)
assert resp.json()["all_requests"][0][
"truncation_ratio"
] == pytest.approx(6.25)
class TestCurrentContextWindow:
"""The response carries the user's configured local context window.
The original pinned this against a hand-built dict, so it could never go
red. Here the value is seeded into the settings table the handler reads
and parametrised over two values, so both "the key was dropped" and "the
key was hardcoded" fail.
"""
@pytest.mark.parametrize("configured", [8192, 4096])
def test_current_context_window_reflects_the_setting(
self, auth_client, configured
):
from local_deep_research.database.models import Setting
from local_deep_research.database.models.settings import SettingType
setting = Setting(
key="llm.local_context_window_size",
value=configured,
type=SettingType.LLM,
name="Context window",
category="llm",
)
with _seeded_db(_usage(), setting):
resp = auth_client.get(OVERVIEW_URL)
body = resp.json()
assert "current_context_window" in body
assert int(body["current_context_window"]) == configured
# ---------------------------------------------------------------------------
# Route registration
# ---------------------------------------------------------------------------
class TestContextOverflowApiRoutes:
def test_metrics_url_with_suffix_is_not_registered(self, auth_client):
"""The metrics endpoint is mounted at /api/context-overflow (no
/metrics suffix). A GET to /api/context-overflow/metrics therefore
must 404 — pinning this so a future URL prefix change can't silently
move the route."""
resp = auth_client.get("/api/context-overflow/metrics")
assert resp.status_code == 404, resp.status_code
# ---------------------------------------------------------------------------
# Error paths
# ---------------------------------------------------------------------------
class TestErrorPaths:
"""Both handlers' ``except`` arms — the shape the dashboard reads."""
def test_overview_db_exception_returns_500(self, auth_client):
with _exploding_db():
resp = auth_client.get(OVERVIEW_URL)
assert resp.status_code == 500
data = resp.json()
assert data["status"] == "error"
assert "context overflow metrics" in data["message"]
def test_research_db_exception_returns_500(self, auth_client):
with _exploding_db():
resp = auth_client.get(RESEARCH_URL.format(research_id="r1"))
assert resp.status_code == 500
data = resp.json()
assert data["status"] == "error"
assert "context overflow data" in data["message"]
# ---------------------------------------------------------------------------
# GET /api/research/{id}/context-overflow
# ---------------------------------------------------------------------------
class TestResearchContextOverflow:
def test_none_research_phase_maps_to_unknown(self, auth_client):
"""research_phase=None is bucketed under 'unknown'."""
with _seeded_db(_usage(research_id="r1", research_phase=None)):
resp = auth_client.get(RESEARCH_URL.format(research_id="r1"))
assert resp.status_code == 200
assert "unknown" in resp.json()["data"]["phase_stats"]
def test_no_context_limit_on_any_entry(self, auth_client):
"""When no entry has a context_limit, overview.context_limit is None."""
with _seeded_db(_usage(research_id="r1", context_limit=None)):
resp = auth_client.get(RESEARCH_URL.format(research_id="r1"))
assert resp.status_code == 200
assert resp.json()["data"]["overview"]["context_limit"] is None
def test_requests_list_contains_correct_fields(self, auth_client):
"""Each entry in requests[] includes all expected keys."""
row = _usage(
research_id="r1",
ollama_prompt_eval_count=42,
calling_function="my_fn",
response_time_ms=300,
tokens_truncated=0,
context_truncated=False,
)
with _seeded_db(row):
resp = auth_client.get(RESEARCH_URL.format(research_id="r1"))
assert resp.status_code == 200
entry = resp.json()["data"]["requests"][0]
for key in [
"timestamp",
"phase",
"prompt_tokens",
"completion_tokens",
"total_tokens",
"context_limit",
"context_truncated",
"tokens_truncated",
"ollama_prompt_eval_count",
"calling_function",
"response_time_ms",
]:
assert key in entry, f"missing key: {key}"
assert entry["ollama_prompt_eval_count"] == 42
assert entry["calling_function"] == "my_fn"
assert entry["response_time_ms"] == 300
assert entry["context_truncated"] is False
def test_phase_stats_accumulation(self, auth_client):
"""Phase stats accumulate counts and token sums per phase."""
rows = [
_usage(
research_id="r1",
research_phase="search",
total_tokens=100,
prompt_tokens=80,
completion_tokens=20,
),
_usage(
research_id="r1",
research_phase="search",
total_tokens=120,
prompt_tokens=90,
completion_tokens=30,
context_truncated=True,
tokens_truncated=50,
),
_usage(
research_id="r1",
research_phase="synthesis",
total_tokens=200,
prompt_tokens=160,
completion_tokens=40,
),
]
with _seeded_db(*rows):
resp = auth_client.get(RESEARCH_URL.format(research_id="r1"))
assert resp.status_code == 200
phase_stats = resp.json()["data"]["phase_stats"]
assert phase_stats["search"]["count"] == 2
assert phase_stats["search"]["truncated_count"] == 1
assert phase_stats["search"]["total_tokens"] == 220
assert phase_stats["synthesis"]["count"] == 1
assert phase_stats["synthesis"]["truncated_count"] == 0