~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
feat(notes): log version-prune events in _prune_versions_in_session (#5498)
LearningCircuit73f614cb8169
diff --git a/changelog.d/5487.misc.md b/changelog.d/5487.misc.mdnew file mode 100644index 0000000000..00400d96cd--- /dev/null+++ b/changelog.d/5487.misc.md@@ -0,0 +1 @@+Note version pruning now emits a single production-retained `logger.info` with the ordinary and bookend counts actually pruned, making silent version loss observable without DB forensics.diff --git a/src/local_deep_research/research_library/notes/services/note_service.py b/src/local_deep_research/research_library/notes/services/note_service.pyindex def8757423..a974625f92 100644--- a/src/local_deep_research/research_library/notes/services/note_service.py+++ b/src/local_deep_research/research_library/notes/services/note_service.py@@ -995,6 +995,10 @@ class NoteService:# background task.version_id: Optional[str] = Noneversion_created = False+ # (0, 0) means "prune wasn't run this call" as well as "ran+ # but nothing was over cap" — both cases skip the post-commit+ # log the same way, so a single sentinel default covers both.+ prune_counts: "tuple[int, int]" = (0, 0)# The version-snapshot INSERT is flushed inside# _create_version_snapshot_in_session, so a concurrent# identical-content save's UNIQUE(uix_note_version_content)@@ -1017,7 +1021,15 @@ class NoteService:change_type=NoteChangeType.MANUAL_SAVE.value,change_summary=None,)- self._prune_versions_in_session(session, note_id)+ # Counts are only bound here, not logged — the+ # delete is merely flushed, and this whole block can+ # still roll back below (IntegrityError recovery) or+ # in the outer handler. Logging happens after+ # session.commit() succeeds, so the log never claims+ # a prune a rollback undid.+ prune_counts = self._prune_versions_in_session(+ session, note_id+ )if content_changed or _append_reparse_only:# In append-reparse-only mode `content` is None (the body@@ -1043,6 +1055,11 @@ class NoteService:)session.commit()+ # Only reachable once the transaction holding the prune+ # deletes has actually committed — see+ # _prune_versions_in_session for why this can't log+ # from inside the prune itself.+ self._log_version_prune(note_id, prune_counts)except IntegrityError as exc:# Concurrency: two same-note saves carrying byte-identical# content both pass the in-session dedup pre-check in@@ -2514,13 +2531,24 @@ class NoteService:def _prune_versions_in_session(self, session: Session, note_id: str- ) -> None:+ ) -> "tuple[int, int]":"""Prune ordinary and restore-bookend versions to independent caps.Caller is responsible for committing. Used by both ``update_note``(after its in-session snapshot write) and ``restore_with_bookends``(which adds PRE_RESTORE + RESTORE rows without consuming the ordinaryversion budget).++ Returns ``(ordinary_pruned, bookend_pruned)`` — the counts of rows+ deleted in *this* call. The deletes are only flushed here, not+ committed: if the caller's transaction later rolls back, these+ rows survive. Callers must NOT log these counts themselves until+ after their own ``session.commit()`` succeeds — logging here (at+ flush time) would claim a prune that a subsequent rollback undoes,+ turning the one operator-facing signal for version loss into a+ false positive on exactly the failure paths it exists to surface.+ See ``update_note`` and ``restore_with_bookends`` for the+ post-commit logging call."""ordinary_total = (session.query(NoteVersion)@@ -2567,6 +2595,29 @@ class NoteService:for row in oldest_bookends:session.delete(row)session.flush()+ ordinary_pruned = max(excess, 0)+ bookend_pruned = max(bookend_excess, 0)+ return (ordinary_pruned, bookend_pruned)++ def _log_version_prune(+ self, note_id: str, prune_counts: "tuple[int, int]"+ ) -> None:+ """Log a prune event. Call ONLY after the caller's transaction has+ committed successfully — see ``_prune_versions_in_session`` for why.++ Emits aggregate counts only (never note content or titles), and+ only when at least one row was actually pruned, so log volume+ stays at most one line per save/restore that triggers a prune —+ not one line per pruned version.+ """+ ordinary_pruned, bookend_pruned = prune_counts+ if ordinary_pruned or bookend_pruned:+ logger.info(+ "note version prune: note_id={} ordinary={} bookend={}",+ note_id,+ ordinary_pruned,+ bookend_pruned,+ )def restore_with_bookends(self, note_id: str, version_id: str@@ -2643,8 +2694,12 @@ class NoteService:)# Prune to cap; with bookends the count can exceed the- # cap by 2 in a single transaction.- self._prune_versions_in_session(session, note_id)+ # cap by 2 in a single transaction. The deletes are only+ # flushed here — this whole transaction can still roll+ # back below (stale-reindex or link-reparse failure), so+ # the counts are logged only after session.commit()+ # succeeds, not here. See _prune_versions_in_session.+ prune_counts = self._prune_versions_in_session(session, note_id)# Stale-vector fix (mirrors update_note): a restore changes# content/title, so mark its embeddings stale across both@@ -2670,6 +2725,7 @@ class NoteService:)session.commit()+ self._log_version_prune(note_id, prune_counts)return (True, None)diff --git a/tests/notes/test_note_stress.py b/tests/notes/test_note_stress.pyindex 0aaff1720f..6ac296d250 100644--- a/tests/notes/test_note_stress.py+++ b/tests/notes/test_note_stress.py@@ -684,6 +684,87 @@ class TestNoteVersionCap:assert "INITIAL" not in titlesassert titles == {"edit1", "edit2", "edit3"}+ def test_prune_emits_info_log_with_counts(+ self, patched_session, sample_note, monkeypatch+ ):+ """Pruning returns the ordinary and bookend counts actually pruned,+ and ``_log_version_prune`` turns those counts into a single+ ``logger.info`` call, so silent version loss becomes observable.++ ``_prune_versions_in_session`` itself must NOT log: its deletes are+ only flushed, not committed, so logging there would claim a prune+ that a caller's later rollback could undo. The log only fires once+ the caller's transaction has actually committed (see+ ``update_note`` / ``restore_with_bookends``, which call+ ``_log_version_prune`` after ``session.commit()`` succeeds).+ """+ from datetime import datetime, timedelta, timezone++ from local_deep_research.database.models import (+ NoteChangeType,+ NoteVersion,+ )+ from local_deep_research.research_library.notes.services import (+ note_service as note_service_mod,+ )+ from local_deep_research.research_library.notes.services.note_service import (+ NoteService,+ )++ # Small caps so a handful of rows exceeds both pools.+ monkeypatch.setattr(note_service_mod, "MAX_VERSIONS_PER_NOTE", 3)+ monkeypatch.setattr(note_service_mod, "MAX_BOOKEND_VERSIONS", 2)++ base = datetime(2024, 1, 1, tzinfo=timezone.utc)+ for i in range(5): # 2 over the ordinary cap+ patched_session.add(+ NoteVersion(+ id=str(uuid.uuid4()),+ document_id=sample_note.id,+ title=f"o{i}",+ content=f"oc{i}",+ tags=[],+ change_type=NoteChangeType.MANUAL_SAVE.value,+ content_hash=f"oh{i}",+ created_at=base + timedelta(minutes=i),+ )+ )+ for i in range(4): # 2 over the bookend cap+ patched_session.add(+ NoteVersion(+ id=str(uuid.uuid4()),+ document_id=sample_note.id,+ title=f"b{i}",+ content=f"bc{i}",+ tags=[],+ change_type=NoteChangeType.PRE_RESTORE.value,+ content_hash=f"bh{i}",+ created_at=base + timedelta(minutes=i),+ )+ )+ patched_session.commit()++ mock_logger = MagicMock()+ monkeypatch.setattr(note_service_mod, "logger", mock_logger)++ service = NoteService("test_user")+ prune_counts = service._prune_versions_in_session(+ patched_session, sample_note.id+ )++ # No log yet — the caller hasn't committed. This is the fix for+ # the false-positive-on-rollback defect: the prune function only+ # hands back counts, it never logs them itself.+ assert prune_counts == (2, 2)+ mock_logger.info.assert_not_called()++ service._log_version_prune(sample_note.id, prune_counts)++ mock_logger.info.assert_called_once()+ template, *rest = mock_logger.info.call_args.args+ assert "note version prune" in template+ assert rest == [sample_note.id, 2, 2]+def test_prune_counts_ordinary_versions_separately_from_bookends(self, patched_session, sample_note, monkeypatch):