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.

fix(journal): resolve the Tier 4 cache upsert by name_lower, not raw name (#6483)

Huang-404-Q1bb38c4aa272
diff --git a/changelog.d/6483.bugfix.md b/changelog.d/6483.bugfix.md
new file mode 100644
index 0000000000..709ca527b8
--- /dev/null
+++ b/changelog.d/6483.bugfix.md
@@ -0,0 +1,19 @@
+Fixed the Tier 4 journal-quality cache silently dropping scores when two
+spellings of the same journal fold together (`"Nature"` vs `"nature"`).
+`Journal` carries two independent unique constraints, `UNIQUE(name)` and
+`uq_journals_name_lower`, and both the upsert's lookups and the cache read
+were keyed on the raw `name`: a folded-equal spelling missed the stored row,
+tripped the second constraint on insert, and missed again on the recovery
+re-fetch, discarding the LLM score with a "row not found on re-fetch" warning.
+The cache read had the same miss, so an alternate spelling re-ran the
+expensive Tier 4 analysis the cache exists to avoid and then landed on the
+existing row anyway. All three lookups now resolve by
+`name_lower=normalize_name(name)`, the folded identity both constraints agree
+on, so a collision on either one resolves against the same row.
+
+The pre-insert check, the post-collision re-fetch and the cache read now all
+resolve by that folded key, and the re-fetch matches the raw name as well: a
+row whose ``name_lower`` is NULL (the column is nullable and only migration
+0006 backfills it) misses the folded check, trips ``UNIQUE(name)`` instead, and
+would be missed again by a folded-only re-fetch. The update repairs the NULL so
+the row converges.
diff --git a/src/local_deep_research/advanced_search_system/filters/journal_reputation_filter.py b/src/local_deep_research/advanced_search_system/filters/journal_reputation_filter.py
index 3dff411159..528d4476f2 100644
--- a/src/local_deep_research/advanced_search_system/filters/journal_reputation_filter.py
+++ b/src/local_deep_research/advanced_search_system/filters/journal_reputation_filter.py
@@ -35,6 +35,7 @@ from typing import Any, Dict, List, Optional
from langchain_core.language_models.chat_models import BaseChatModel
from loguru import logger
+from sqlalchemy import or_
from sqlalchemy.orm import Session
from ...utilities.type_utils import unwrap_setting
@@ -868,9 +869,25 @@ JOURNAL INFORMATION:
now = int(time.time())
model_id = get_model_identifier(self.model)
+ # The folded identity both unique constraints agree on, computed once —
+ # every lookup and both writes below have to use the same value.
+ name_lower = normalize_name(name)
with session_ctx as db_session:
- journal = db_session.query(Journal).filter_by(name=name).first()
+ # Resolve by name_lower, not by raw name. Journal carries two
+ # independent unique constraints — UNIQUE(name) and
+ # uq_journals_name_lower — so a lookup keyed on raw name misses a
+ # row that only the second constraint covers ("Nature" vs
+ # "nature"): the select falls through to the insert branch, the
+ # insert trips uq_journals_name_lower, and the recovery re-fetch
+ # below — keyed the same way — misses too, dropping the score.
+ # name_lower is the folded identity both constraints agree on, so
+ # a collision on either one resolves against the same existing row.
+ journal = (
+ db_session.query(Journal)
+ .filter_by(name_lower=name_lower)
+ .first()
+ )
if journal is not None:
journal.quality = quality
journal.score_source = "llm"
@@ -882,7 +899,7 @@ JOURNAL INFORMATION:
# migration backfill (0006:257) uses the same normalization;
# divergence produces silent cache misses and, on migration,
# UNIQUE violations that abort the upgrade.
- journal.name_lower = normalize_name(name)
+ journal.name_lower = name_lower
try:
db_session.commit()
except Exception:
@@ -896,7 +913,7 @@ JOURNAL INFORMATION:
try:
journal = Journal(
name=name,
- name_lower=normalize_name(name),
+ name_lower=name_lower,
quality=quality,
score_source="llm",
quality_model=model_id,
@@ -910,8 +927,27 @@ JOURNAL INFORMATION:
except IntegrityError:
sp.rollback()
- # Competing writer inserted first; re-fetch and update.
- journal = db_session.query(Journal).filter_by(name=name).first()
+ # Competing writer inserted first; re-fetch and update. Both
+ # constraints are matched, not just the folded one: the row that won
+ # may carry a different raw spelling (so a raw-name lookup would miss
+ # it on exactly the name_lower collision that landed us here), and a
+ # row whose name_lower is NULL -- the column is nullable and only
+ # migration 0006 backfills it -- misses the name_lower-keyed
+ # pre-insert check in the first place, so its insert trips
+ # UNIQUE(name) instead and a name_lower-only re-fetch would miss it
+ # again. That is the same dropped score this method exists to
+ # prevent, entered from the other side. The update below repairs the
+ # NULL so the row converges.
+ journal = (
+ db_session.query(Journal)
+ .filter(
+ or_(
+ Journal.name_lower == name_lower,
+ Journal.name == name,
+ )
+ )
+ .first()
+ )
if journal is None:
# Genuinely unexpected — UNIQUE violation with no row.
logger.warning(
@@ -923,7 +959,7 @@ JOURNAL INFORMATION:
journal.score_source = "llm"
journal.quality_analysis_time = now
journal.quality_model = model_id
- journal.name_lower = normalize_name(name)
+ journal.name_lower = name_lower
try:
db_session.commit()
except Exception:
@@ -1065,7 +1101,12 @@ JOURNAL INFORMATION:
with session_ctx as session:
cached = (
session.query(Journal)
- .filter_by(name=journal_name)
+ # Keyed on name_lower like the write path, and for the
+ # same reason: a raw-name lookup misses a row that only
+ # uq_journals_name_lower covers, so an alternate
+ # spelling would skip the cache and re-run the
+ # expensive Tier 4 analysis it exists to avoid.
+ .filter_by(name_lower=normalize_name(journal_name))
.filter(Journal.score_source == "llm")
.filter(
Journal.quality_model
diff --git a/tests/advanced_search_system/filters/test_journal_reputation_filter_edges.py b/tests/advanced_search_system/filters/test_journal_reputation_filter_edges.py
index b55380782c..51827f710d 100644
--- a/tests/advanced_search_system/filters/test_journal_reputation_filter_edges.py
+++ b/tests/advanced_search_system/filters/test_journal_reputation_filter_edges.py
@@ -8,6 +8,7 @@ or writes outside pytest's ``tmp_path``.
import pathlib
import threading
+import time
from datetime import timedelta
from pathlib import Path
from typing import Final
@@ -384,3 +385,257 @@ def test_invalid_db_ready_probe_fails_soft_to_pending(tmp_path: Path) -> None:
)
assert filtered[0]["journal_quality"] == "pending"
+
+
+_ENGINES: list = []
+
+
[email protected](autouse=True)
+def _dispose_test_engines():
+ """Dispose every in-memory engine the helpers below open."""
+ yield
+ while _ENGINES:
+ _ENGINES.pop().dispose()
+
+
+def _real_journal_session_ctx():
+ """A real in-memory user DB carrying the ``journals`` table, plus a
+ context manager yielding a session.
+
+ The mocked-session test above cannot reach the defect these cases
+ cover: it needs the real ``UNIQUE(name)`` / ``UNIQUE(name_lower)``
+ pair to actually fire.
+ """
+ from contextlib import contextmanager
+
+ from sqlalchemy import create_engine
+ from sqlalchemy.orm import sessionmaker
+
+ from local_deep_research.database.models.journal import Journal
+
+ engine = create_engine("sqlite:///:memory:")
+ _ENGINES.append(engine)
+ Journal.__table__.create(engine)
+ session_factory = sessionmaker(bind=engine)
+
+ @contextmanager
+ def _ctx():
+ session = session_factory()
+ try:
+ yield session
+ finally:
+ session.close()
+
+ return _ctx, session_factory
+
+
+def _session_whose_first_lookup_misses(session_factory):
+ """A session whose first ``Journal`` query is forced to miss.
+
+ Stands in for a competing writer that inserted the row after our pre-insert
+ check had already run: the check legitimately returns nothing, the insert
+ collides, and the recovery re-fetch is the half that has to find the row.
+ """
+ from contextlib import contextmanager
+
+ from local_deep_research.database.models.journal import Journal
+
+ @contextmanager
+ def _ctx():
+ session = session_factory()
+ real_query = session.query
+ state = {"missed": False}
+
+ def query(*args, **kwargs):
+ q = real_query(*args, **kwargs)
+ if not state["missed"]:
+ state["missed"] = True
+ # An impossible predicate, so the pre-insert check sees no row.
+ return q.filter(Journal.id == -1)
+ return q
+
+ session.query = query
+ try:
+ yield session
+ finally:
+ session.close()
+
+ return _ctx
+
+
+def test_recovery_refetch_finds_a_competing_writers_row() -> None:
+ """The post-IntegrityError re-fetch resolves a row it never saw.
+
+ The pre-insert check misses (the competing writer's row landed after it),
+ so the insert collides on uq_journals_name_lower and only the recovery
+ re-fetch can find the winner — which carries a different raw spelling.
+ Without the folded key there the score is dropped with the "row not found
+ on re-fetch" warning, the same loss this change fixes on the write side.
+ """
+ from local_deep_research.database.models.journal import Journal
+
+ ctx, session_factory = _real_journal_session_ctx()
+ filter_obj = _bare_filter()
+
+ with patch(f"{MODULE}.get_model_identifier", return_value="model-v1"):
+ # The competing writer's row, stored under the other spelling.
+ filter_obj._save_journal_to_db_inner(ctx(), name="Nature", quality=8)
+ filter_obj._save_journal_to_db_inner(
+ _session_whose_first_lookup_misses(session_factory)(),
+ name="nature",
+ quality=3,
+ )
+
+ with session_factory() as session:
+ rows = session.query(Journal).all()
+
+ assert len(rows) == 1, (
+ "the recovery re-fetch must resolve against the competing writer's row, "
+ f"not insert a second one: {[(r.name, r.name_lower) for r in rows]}"
+ )
+ assert rows[0].quality == 3, (
+ "the score was dropped instead of updating the row the competing "
+ f"writer had already inserted (left at {rows[0].quality})"
+ )
+
+
+def test_recovery_refetch_finds_a_row_with_no_name_lower() -> None:
+ """A row whose name_lower is NULL is still found by the recovery re-fetch.
+
+ ``name_lower`` is nullable and only migration 0006 backfills it, so a row
+ the backfill missed makes the name_lower-keyed pre-insert check miss and
+ sends the insert into UNIQUE(name) instead. The recovery has to match that
+ row too, or the score is dropped exactly as before — and the update repairs
+ the NULL so the row converges.
+ """
+ from local_deep_research.database.models.journal import Journal
+
+ ctx, session_factory = _real_journal_session_ctx()
+
+ # A legacy row: the same name, no folded key.
+ with session_factory() as session:
+ session.add(
+ Journal(
+ name="Nature",
+ name_lower=None,
+ quality=8,
+ score_source="llm",
+ quality_model="model-v1",
+ quality_analysis_time=int(time.time()),
+ )
+ )
+ session.commit()
+
+ filter_obj = _bare_filter()
+ with patch(f"{MODULE}.get_model_identifier", return_value="model-v1"):
+ filter_obj._save_journal_to_db_inner(ctx(), name="Nature", quality=3)
+
+ with session_factory() as session:
+ rows = session.query(Journal).all()
+
+ assert len(rows) == 1, (
+ f"expected the existing row to be updated, got {[r.name for r in rows]}"
+ )
+ assert rows[0].quality == 3, (
+ f"the score was dropped instead of updating the NULL-name_lower row "
+ f"(left at {rows[0].quality})"
+ )
+ assert rows[0].name_lower == "nature", (
+ "the update should repair the folded key"
+ )
+
+
+def test_cache_read_matches_a_folded_equivalent_spelling() -> None:
+ """A score cached under one spelling is served for another.
+
+ The cache *read* used the same raw-name filter as the write path, so an
+ alternate spelling missed the row, skipped the cache, and ran the Tier 4
+ analysis the cache exists to avoid — only to land on the existing row
+ afterwards.
+ """
+ ctx, _ = _real_journal_session_ctx()
+ filter_obj = _bare_filter()
+ filter_obj._JournalReputationFilter__db_session = ctx
+
+ with patch(f"{MODULE}.get_model_identifier", return_value="model-v1"):
+ filter_obj._save_journal_to_db_inner(ctx(), name="Nature", quality=8)
+ score = filter_obj._JournalReputationFilter__score_journal("nature", {})
+
+ assert score == (8, "llm"), (
+ "the folded-equal spelling missed the cache instead of serving the "
+ f"cached Tier 4 score (got {score!r})"
+ )
+
+
+def test_case_folded_alternate_spelling_updates_the_existing_row() -> None:
+ """A second spelling that NFKC-lower-folds onto an existing row must
+ update that row rather than silently dropping its score.
+
+ ``Journal`` carries two independent unique constraints — ``UNIQUE(name)``
+ and ``uq_journals_name_lower``. The upsert used to select by *raw* ``name``,
+ so ``"nature"`` missed the ``"Nature"`` row and went to the insert branch,
+ which tripped ``uq_journals_name_lower``; the recovery re-fetch was keyed on
+ the same raw name and missed too, so the score was discarded with a
+ "row not found on re-fetch" warning. That was precisely the failure the
+ method's own docstring claims the savepoint + re-fetch fixed — it only
+ covered the ``UNIQUE(name)`` half.
+ """
+ from local_deep_research.database.models.journal import Journal
+
+ ctx, session_factory = _real_journal_session_ctx()
+ filter_obj = _bare_filter()
+
+ with patch(f"{MODULE}.get_model_identifier", return_value="model-v1"):
+ filter_obj._save_journal_to_db_inner(ctx(), name="Nature", quality=8)
+ filter_obj._save_journal_to_db_inner(ctx(), name="nature", quality=3)
+
+ with session_factory() as session:
+ rows = session.query(Journal).all()
+
+ assert len(rows) == 1, (
+ "spellings that fold to the same name_lower must resolve to one row, "
+ f"got {[(r.name, r.name_lower) for r in rows]}"
+ )
+ assert rows[0].quality == 3, (
+ "the second score was dropped instead of updating the existing row "
+ f"(left at {rows[0].quality})"
+ )
+
+
+def test_same_raw_name_still_updates_in_place() -> None:
+ """Regression guard: the original raw-name upsert path is unchanged."""
+ from local_deep_research.database.models.journal import Journal
+
+ ctx, session_factory = _real_journal_session_ctx()
+ filter_obj = _bare_filter()
+
+ with patch(f"{MODULE}.get_model_identifier", return_value="model-v1"):
+ filter_obj._save_journal_to_db_inner(ctx(), name="Nature", quality=8)
+ filter_obj._save_journal_to_db_inner(ctx(), name="Nature", quality=5)
+
+ with session_factory() as session:
+ rows = session.query(Journal).all()
+
+ assert len(rows) == 1
+ assert rows[0].quality == 5
+
+
+def test_distinct_journals_still_get_their_own_rows() -> None:
+ """Regression guard: resolving by name_lower must not merge genuinely
+ different journals."""
+ from local_deep_research.database.models.journal import Journal
+
+ ctx, session_factory = _real_journal_session_ctx()
+ filter_obj = _bare_filter()
+
+ with patch(f"{MODULE}.get_model_identifier", return_value="model-v1"):
+ filter_obj._save_journal_to_db_inner(ctx(), name="Nature", quality=8)
+ filter_obj._save_journal_to_db_inner(ctx(), name="Science", quality=2)
+
+ with session_factory() as session:
+ rows = session.query(Journal).order_by(Journal.name).all()
+
+ assert [(r.name, r.quality) for r in rows] == [
+ ("Nature", 8),
+ ("Science", 2),
+ ]