~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
"""Ported from ``tests/notes/test_note_factcheck_routes.py`` on main
(deleted by the FastAPI migration).
Old surface: ``web/routes/notes_routes.py`` (``_grade_note_claims`` +
``grade_note_fact_check``).
New surface: ``web/routers/notes.py`` -- both functions survive the port
byte-for-byte apart from framework plumbing, so the assertions carry over
unchanged.
Successor audit -- PARTIALLY superseded, ported whole
-----------------------------------------------------
``tests/notes/test_note_ai_service.py`` has ~25 ``test_grade_*`` tests, but
every one exercises ``NoteAIService.grade_all_claims`` (verdict mapping,
source-citation downgrade, index remapping) -- none reaches this route layer.
``tests/notes/test_notes_router_fastapi.py`` never touches the grade endpoint.
``tests/security/test_library_notes_authz_fastapi.py`` IS a partial successor
(``TestFactCheckGradeCrossResourceAuthz`` /
``TestFactCheckClaimSanitisation``) and already pins six of these twelve over
HTTP: the missing-research 404, the not-complete 409, the not-linked 404, the
per-claim truncation, the count cap, and both 400 guards.
The other SIX are pinned by nothing on the branch, and they are the ones that
protect against writing a wrong or vacuous fact-check:
* a whitespace-only report is also a 502 (the ``str(report).strip()`` half),
* the happy path: 200, the grader receiving ``(claims, report, sources)``,
the verdicts + ``note_id`` persisted under ``research_meta['fact_check']``,
and pre-existing meta keys surviving the reassign-don't-clobber merge,
* the persist-window conflict (409) when the research row is deleted between
grading and saving -- returning success there would report a fact-check
that was never stored,
* ``db.rollback()`` then re-raise on a failed commit (poisoned-session
compensation),
* sources coming from ``research_resources`` via
``get_research_source_links_batch`` rather than the legacy
``research_meta['all_links_of_system']`` key, with non-http URLs filtered,
* and that ``grade_all_claims`` is NOT called at all on the 502 paths.
The six already-covered tests are kept rather than deleted: they run at the
helper level with explicit mocks (no HTTP, no encrypted DB), which is where
the remaining six have to live anyway, and they are the positive controls the
other six lean on. Every one of the twelve was mutation-verified against a
deliberate revert of its guard.
Plumbing translation
--------------------
``_grade_note_claims`` is a plain module function on both branches and is
called directly, exactly as on main. ``grade_note_fact_check`` is now a
FastAPI endpoint with ``body=Depends(_notes_json_body)``; the decorator peel
(``__wrapped__``) that main used for ``@login_required``/``@_notes_ai_limit``
still works -- it now peels the slowapi ``@_notes_factcheck_limit`` -- and the
body is passed as the ``body`` argument instead of through a Flask test
request context.
"""
import asyncio
import json
from contextlib import contextmanager
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
from starlette.requests import Request
from local_deep_research.constants import ResearchStatus
from local_deep_research.research_library.notes.services.note_ai_service import (
NoteAIService,
)
from local_deep_research.web.routers import notes as notes_routes
from local_deep_research.web.routers.notes import MAX_CLAIM_LEN
# Sentinel: a NoteResearch link row exists (research IS linked to the note).
_LINKED = SimpleNamespace(document_id="note1", research_id="r1")
# Sentinel: caller did not ask for a different row on the persist re-query.
_UNSET = object()
def _patch_session(
monkeypatch,
research,
linked=_LINKED,
resources=(),
research_on_reload=_UNSET,
):
"""Patch get_user_db_session to yield a MODEL-AWARE session.
``query(ResearchHistory)`` resolves to ``research``; ``query(NoteResearch)``
resolves to ``linked`` (the note<->research link row); ``query(ResearchResource)``
resolves to ``resources`` (the structured source rows the grade route reads
via get_research_source_links_batch). Routing by model -- not by call order --
means the happy-path tests genuinely exercise the linkage check (``linked``
truthy by default) instead of it passing vacuously, while a test can pass
``linked=None`` to assert the not-linked rejection.
"""
from local_deep_research.database.models import (
NoteResearch,
ResearchHistory,
ResearchResource,
)
session = MagicMock()
# _grade_note_claims queries ResearchHistory twice: once to gate on status
# and once to re-load the row for the persist. When ``research_on_reload``
# is supplied, the first query returns ``research`` (gating) and every
# later query returns ``research_on_reload`` -- pass ``None`` to simulate a
# concurrent delete between grading and persisting.
research_calls = {"n": 0}
def _query(model, *args, **kwargs):
result = MagicMock()
if model is NoteResearch:
result.filter_by.return_value.first.return_value = linked
elif model is ResearchHistory:
if research_on_reload is _UNSET:
result.filter_by.return_value.first.return_value = research
else:
def _first():
research_calls["n"] += 1
return (
research
if research_calls["n"] == 1
else research_on_reload
)
result.filter_by.return_value.first.side_effect = _first
elif model is ResearchResource:
# get_research_source_links_batch chains
# .filter().filter().order_by().all() -- make the mock
# self-chaining so the REAL batch function runs over these rows.
result.filter.return_value = result
result.order_by.return_value = result
result.all.return_value = list(resources)
else:
result.filter_by.return_value.first.return_value = None
return result
session.query.side_effect = _query
@contextmanager
def fake_session(username=None, password=None):
yield session
monkeypatch.setattr(
"local_deep_research.database.session_context.get_user_db_session",
fake_session,
)
return session
def test_grade_returns_404_when_research_missing(monkeypatch):
_patch_session(monkeypatch, None)
payload, status = asyncio.run(
notes_routes._grade_note_claims("u", "note1", "r1", ["a claim"])
)
assert status == 404
assert payload["success"] is False
def test_grade_returns_409_when_research_not_completed(monkeypatch):
research = SimpleNamespace(
status=ResearchStatus.IN_PROGRESS, research_meta={}
)
_patch_session(monkeypatch, research)
payload, status = asyncio.run(
notes_routes._grade_note_claims("u", "note1", "r1", ["a claim"])
)
assert status == 409
assert payload["status"] == ResearchStatus.IN_PROGRESS
def test_grade_rejects_research_not_linked_to_note(monkeypatch):
"""grading must verify the research is actually linked to the note,
or a client could grade an arbitrary completed research against any note."""
research = SimpleNamespace(
status=ResearchStatus.COMPLETED, research_meta={}
)
# research found, but NO NoteResearch link row -> must reject.
_patch_session(monkeypatch, research, linked=None)
payload, status = asyncio.run(
notes_routes._grade_note_claims("u", "note1", "r1", ["a claim"])
)
assert status == 404
assert "not linked" in payload["error"].lower()
def test_grade_returns_502_when_report_unavailable(monkeypatch):
"""get_report returns None for both a missing report and a swallowed read
error. Grading against an empty report would falsely mark every claim
'unverified' and persist that as success -- the route must refuse instead."""
research = SimpleNamespace(
status=ResearchStatus.COMPLETED, research_meta={}
)
_patch_session(monkeypatch, research)
storage = MagicMock()
storage.get_report.return_value = None
monkeypatch.setattr(
"local_deep_research.storage.get_report_storage",
lambda session=None, settings_snapshot=None: storage,
)
# Grader must never be invoked when there is no report.
ai = MagicMock()
monkeypatch.setattr(notes_routes, "NoteAIService", lambda username: ai)
payload, status = asyncio.run(
notes_routes._grade_note_claims("u", "note1", "r1", ["a claim"])
)
assert status == 502
assert payload["success"] is False
ai.grade_all_claims.assert_not_called()
# Nothing persisted.
assert "fact_check" not in (research.research_meta or {})
def test_grade_returns_502_when_report_blank(monkeypatch):
research = SimpleNamespace(
status=ResearchStatus.COMPLETED, research_meta={}
)
_patch_session(monkeypatch, research)
storage = MagicMock()
storage.get_report.return_value = " \n "
monkeypatch.setattr(
"local_deep_research.storage.get_report_storage",
lambda session=None, settings_snapshot=None: storage,
)
ai = MagicMock()
monkeypatch.setattr(notes_routes, "NoteAIService", lambda username: ai)
payload, status = asyncio.run(
notes_routes._grade_note_claims("u", "note1", "r1", ["a claim"])
)
assert status == 502
ai.grade_all_claims.assert_not_called()
def test_grade_completed_grades_and_persists_verdicts(monkeypatch):
research = SimpleNamespace(
status=ResearchStatus.COMPLETED,
research_meta={"iterations": 2},
)
# Sources live in the research_resources table (nothing writes the
# legacy research_meta['all_links_of_system'] key since chat-mode-v2),
# so the grade route must read them from there.
_patch_session(
monkeypatch,
research,
resources=[
SimpleNamespace(id=1, research_id="r1", title="S", url="https://s")
],
)
storage = MagicMock()
storage.get_report.return_value = "report markdown"
monkeypatch.setattr(
"local_deep_research.storage.get_report_storage",
lambda session=None, settings_snapshot=None: storage,
)
verdicts = [
{
"claim": "a claim",
"verdict": "supported",
"confidence": 90,
"reasoning": "per the report",
"sources": [{"title": "S", "url": "https://s"}],
}
]
ai = MagicMock()
ai.grade_all_claims = AsyncMock(return_value=verdicts)
monkeypatch.setattr(notes_routes, "NoteAIService", lambda username: ai)
payload, status = asyncio.run(
notes_routes._grade_note_claims("u", "note1", "r1", ["a claim"])
)
assert status == 200
assert payload["success"] is True
assert payload["verdicts"] == verdicts
# Grader received the fetched report + the table-derived sources.
ai.grade_all_claims.assert_called_once_with(
["a claim"],
"report markdown",
[{"url": "https://s", "title": "S"}],
)
# Verdicts persisted under research_meta['fact_check'].
assert research.research_meta["fact_check"]["verdicts"] == verdicts
assert research.research_meta["fact_check"]["note_id"] == "note1"
# Existing meta is preserved (reassignment merges, doesn't clobber).
assert research.research_meta["iterations"] == 2
def test_grade_reports_conflict_when_research_vanishes_before_persist(
monkeypatch,
):
"""the research row is loaded for grading, graded, then re-loaded to
persist the verdicts. If it is deleted in that window (concurrent delete),
the verdicts cannot be saved. The route must NOT return success for an
unsaved fact-check -- it reports the conflict (409) instead."""
research = SimpleNamespace(
status=ResearchStatus.COMPLETED,
research_meta={"iterations": 2},
)
# Gating query finds the row; the persist re-query finds None.
_patch_session(monkeypatch, research, research_on_reload=None)
storage = MagicMock()
storage.get_report.return_value = "report markdown"
monkeypatch.setattr(
"local_deep_research.storage.get_report_storage",
lambda session=None, settings_snapshot=None: storage,
)
verdicts = [
{
"claim": "a claim",
"verdict": "supported",
"confidence": 90,
"reasoning": "per the report",
"sources": [],
}
]
ai = MagicMock()
ai.grade_all_claims = AsyncMock(return_value=verdicts)
monkeypatch.setattr(notes_routes, "NoteAIService", lambda username: ai)
payload, status = asyncio.run(
notes_routes._grade_note_claims("u", "note1", "r1", ["a claim"])
)
assert status == 409
assert payload["success"] is False
assert "disappear" in payload["error"].lower()
# Grading still ran; nothing was persisted onto the now-absent row.
ai.grade_all_claims.assert_called_once()
assert "fact_check" not in (research.research_meta or {})
def test_grade_rolls_back_and_reraises_on_persist_commit_failure(monkeypatch):
"""if committing the verdicts fails, the route must roll back the
shared per-user request session (so a later handler doesn't reuse a
poisoned session) and re-raise rather than swallow -- handle_api_error in
the route then maps it to a 500."""
research = SimpleNamespace(
status=ResearchStatus.COMPLETED, research_meta={"iterations": 2}
)
session = _patch_session(monkeypatch, research)
# The persist block is the only commit in _grade_note_claims; make it fail.
session.commit.side_effect = RuntimeError("database is locked")
storage = MagicMock()
storage.get_report.return_value = "report markdown"
monkeypatch.setattr(
"local_deep_research.storage.get_report_storage",
lambda session=None, settings_snapshot=None: storage,
)
ai = MagicMock()
ai.grade_all_claims = AsyncMock(
return_value=[
{
"claim": "a claim",
"verdict": "supported",
"confidence": 90,
"reasoning": "per the report",
"sources": [],
}
]
)
monkeypatch.setattr(notes_routes, "NoteAIService", lambda username: ai)
with pytest.raises(RuntimeError, match="database is locked"):
asyncio.run(
notes_routes._grade_note_claims("u", "note1", "r1", ["a claim"])
)
# The shared session was rolled back before the exception propagated.
session.rollback.assert_called_once()
def test_grade_sources_come_from_resources_table_not_legacy_meta(monkeypatch):
"""Regression guard: nothing has written research_meta['all_links_of_system']
since chat-mode-v2 (#3665) -- sources persist to the research_resources
table only. Reading the legacy meta key gave the grader '(no sources)'
on every production fact-check, leaving the citation-validation /
anti-rubber-stamp checks unreachable. Even when a stale legacy key IS
present, the structured table must win."""
research = SimpleNamespace(
status=ResearchStatus.COMPLETED,
research_meta={
# Stale legacy key with DIFFERENT content -- must be ignored.
"all_links_of_system": [{"title": "stale", "url": "https://old"}],
},
)
_patch_session(
monkeypatch,
research,
resources=[
SimpleNamespace(
id=1, research_id="r1", title="Real", url="https://real"
),
# Non-http urls are filtered out by the batch helper.
SimpleNamespace(
id=2, research_id="r1", title="Local", url="file:///etc"
),
],
)
storage = MagicMock()
storage.get_report.return_value = "report markdown"
monkeypatch.setattr(
"local_deep_research.storage.get_report_storage",
lambda session=None, settings_snapshot=None: storage,
)
ai = MagicMock()
ai.grade_all_claims = AsyncMock(return_value=[])
monkeypatch.setattr(notes_routes, "NoteAIService", lambda username: ai)
payload, status = asyncio.run(
notes_routes._grade_note_claims("u", "note1", "r1", ["a claim"])
)
assert status == 200
ai.grade_all_claims.assert_called_once_with(
["a claim"],
"report markdown",
[{"url": "https://real", "title": "Real"}],
)
def _fully_unwrapped(route_fn):
"""Peel every decorator wrapper off a route so the raw handler runs
without the rate-limit stack. (On main this peeled ``@login_required``
and ``@_notes_ai_limit``; here it peels the slowapi
``@_notes_factcheck_limit``.) Walk the chain until the real function is
reached."""
fn = route_fn
while hasattr(fn, "__wrapped__"):
fn = fn.__wrapped__
return fn
def _dummy_request():
return Request(
{
"type": "http",
"method": "POST",
"path": "/notes/api/notes/n1/fact-check/r1/grade",
"headers": [],
"session": {},
}
)
class TestGradeRouteClaimSanitization:
"""grade_note_fact_check sanitizes the client-supplied ``claims`` list
BEFORE handing it to the grader: it rejects a non-list body, drops
non-str/blank entries, truncates each claim to MAX_CLAIM_LEN, caps the
count at NoteAIService.MAX_CLAIMS_PER_NOTE, and 400s when nothing
survives. These checks live only in the route body, so the
_grade_note_claims tests above don't reach them."""
def _call_grade(self, body, monkeypatch):
"""Drive the route with ``body`` and a patched grader; return
((payload, status), captured_claims) where captured_claims is None if
the grader was never reached."""
captured = {}
async def fake_grade(username, note_id, research_id, claims):
captured["claims"] = claims
return {"success": True, "verdicts": []}, 200
monkeypatch.setattr(notes_routes, "_grade_note_claims", fake_grade)
handler = _fully_unwrapped(notes_routes.grade_note_fact_check)
response = asyncio.run(
handler(
_dummy_request(),
"n1",
"r1",
username="u",
body=body,
)
)
return (
json.loads(response.body),
response.status_code,
), captured.get("claims")
def test_grade_route_truncates_and_caps_claims_before_helper(
self, monkeypatch
):
"""An over-long claim is truncated to exactly MAX_CLAIM_LEN, the
claim count is capped at MAX_CLAIMS_PER_NOTE, blanks/non-str entries
are dropped, and trailing whitespace is stripped before truncation
(so 5000 'Z' + spaces -> exactly MAX_CLAIM_LEN chars).
Catches a revert that drops ``.strip()[:MAX_CLAIM_LEN]`` (unbounded
multi-KB text into the LLM prompt) or the
``[:NoteAIService.MAX_CLAIMS_PER_NOTE]`` count cap."""
raw = []
for _ in range(NoteAIService.MAX_CLAIMS_PER_NOTE + 20):
raw.append("Z" * 5000 + " ")
raw.append(123) # non-str -> dropped
raw.append(None) # non-str -> dropped
raw.append(" ") # blank -> dropped
(payload, status), claims = self._call_grade(
{"claims": raw}, monkeypatch
)
assert status == 200
assert payload["success"] is True
# Count cap applied.
assert len(claims) == NoteAIService.MAX_CLAIMS_PER_NOTE
# Every surviving claim is a stripped str truncated to the cap.
for c in claims:
assert isinstance(c, str)
assert c == c.strip()
assert len(c) == MAX_CLAIM_LEN
# No None / int / blank entries survived.
assert all(c.strip() for c in claims)
def test_grade_route_400_when_no_valid_claims(self, monkeypatch):
"""A list that is non-empty but contains only blanks/non-str entries
sanitizes to [] -> 400, and the grader is NEVER invoked.
Catches removing the post-filter ``if not claims: 400`` guard, which
would otherwise grade [] and persist a vacuous fact-check."""
(payload, status), claims = self._call_grade(
{"claims": [None, " ", 7, "", "\t\n"]},
monkeypatch,
)
assert status == 400
assert payload["success"] is False
# Grader short-circuited before being reached.
assert claims is None
def test_grade_route_rejects_non_list_claims(self, monkeypatch):
"""A string body (not a list) is rejected with 400 rather than being
iterated char-by-char into 1-char claims.
Catches removing the ``isinstance(raw_claims, list)`` guard."""
(payload, status), claims = self._call_grade(
{"claims": "not-a-list"}, monkeypatch
)
assert status == 400
assert payload["error"] == "claims required"
assert claims is None