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(notes): preserve ordinary version retention (#5438)

Ziyang Guo7f365553a27e
diff --git a/changelog.d/4964.bugfix.md b/changelog.d/4964.bugfix.md
new file mode 100644
index 0000000000..f44488c9cb
--- /dev/null
+++ b/changelog.d/4964.bugfix.md
@@ -0,0 +1 @@
+Keep restore audit bookends from reducing the retained pool of ordinary note versions.
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.py
index 4d4c77aa44..016486fc96 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
@@ -2515,23 +2515,23 @@ class NoteService:
def _prune_versions_in_session(
self, session: Session, note_id: str
) -> None:
- """Prune oldest versions over MAX_VERSIONS_PER_NOTE.
+ """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 can exceed the cap by 2 in one transaction due to
- PRE_RESTORE + RESTORE).
+ (which adds PRE_RESTORE + RESTORE rows without consuming the ordinary
+ version budget).
"""
- total = (
- session.query(NoteVersion).filter_by(document_id=note_id).count()
+ ordinary_total = (
+ session.query(NoteVersion)
+ .filter_by(document_id=note_id)
+ .filter(NoteVersion.change_type.notin_(_AUDIT_BOOKEND_TYPES))
+ .count()
)
- excess = total - MAX_VERSIONS_PER_NOTE
+ excess = ordinary_total - MAX_VERSIONS_PER_NOTE
if excess > 0:
- # Audit bookends (PRE_RESTORE / RESTORE) are excluded from
- # the prune pool so the "user restored here" trail survives
- # heavy editing. If the non-bookend pool can't absorb the
- # excess, the cap is allowed to drift up slightly — keeping
- # audit history is more important than the exact ceiling.
+ # Audit bookends have their own cap below and never reduce the
+ # number of ordinary snapshots retained for the user.
oldest = (
session.query(NoteVersion)
.filter_by(document_id=note_id)
diff --git a/tests/notes/test_post_review_bugfixes.py b/tests/notes/test_post_review_bugfixes.py
index c0060b5b51..3c5c9d7b9b 100644
--- a/tests/notes/test_post_review_bugfixes.py
+++ b/tests/notes/test_post_review_bugfixes.py
@@ -806,24 +806,16 @@ class TestPruneVersionsPreservesBookends:
"bookend-ceiling branch."
)
- def test_prune_keeps_all_bookends_when_excess_exceeds_nonbookend_pool(
+ def test_prune_counts_ordinary_versions_separately_from_bookends(
self, db_session, note_source_type, patched_service, monkeypatch
):
- """When the non-bookend prunable pool is SMALLER than the excess,
- the cap is intentionally allowed to drift up rather than sacrificing
- audit bookends. The first prune branch only deletes non-bookend rows
- (limit(excess)), so it can't reach the cap; bookends are never
- eligible. This pins that drift behaviour.
- """
+ """Bookends must not consume the ordinary-version retention budget."""
from local_deep_research.research_library.notes.services import (
note_service as note_service_mod,
)
- # Raise the bookend ceiling so the SECOND (ceiling) branch never
- # fires and delete any bookend — we want to isolate the first
- # branch's drift behaviour.
- monkeypatch.setattr(note_service_mod, "MAX_VERSIONS_PER_NOTE", 100)
- monkeypatch.setattr(note_service_mod, "MAX_BOOKEND_VERSIONS", 200)
+ monkeypatch.setattr(note_service_mod, "MAX_VERSIONS_PER_NOTE", 5)
+ monkeypatch.setattr(note_service_mod, "MAX_BOOKEND_VERSIONS", 3)
note_id = str(uuid.uuid4())
db_session.add(
@@ -841,10 +833,8 @@ class TestPruneVersionsPreservesBookends:
db_session.commit()
base = datetime.now(timezone.utc) - timedelta(days=2)
- # 110 bookends + 2 AUTO_SAVE = 112 total, excess = 12, but the
- # non-bookend prunable pool is only 2 (< 12) — drift is forced.
bookend_ids = []
- for i in range(110):
+ for i in range(3):
change_type = (
NoteChangeType.RESTORE.value
if i % 2 == 0
@@ -863,8 +853,8 @@ class TestPruneVersionsPreservesBookends:
db_session.add(v)
bookend_ids.append(v.id)
- auto_ids = []
- for j in range(2):
+ ordinary_ids = []
+ for j in range(5):
v = NoteVersion(
id=str(uuid.uuid4()),
document_id=note_id,
@@ -876,7 +866,7 @@ class TestPruneVersionsPreservesBookends:
created_at=base + timedelta(seconds=1000 + j),
)
db_session.add(v)
- auto_ids.append(v.id)
+ ordinary_ids.append(v.id)
db_session.commit()
patched_service._prune_versions_in_session(db_session, note_id)
@@ -889,30 +879,9 @@ class TestPruneVersionsPreservesBookends:
.all()
}
- # (1) every bookend survives — bookends are never pruned.
- for bid in bookend_ids:
- assert bid in surviving, (
- "bookends must never be pruned; removing the "
- ".notin_(bookend_types) filter at note_service.py:1708 "
- "would make them eligible and flip this assertion."
- )
-
- # (2) the 2 AUTO_SAVE rows are consumed (the whole non-bookend pool;
- # limit(excess) caps deletion at the 2 available).
- for aid in auto_ids:
- assert aid not in surviving, (
- "the entire non-bookend pool should be consumed before "
- "drift is allowed."
- )
-
- # (3) surviving total is STILL > MAX_VERSIONS_PER_NOTE (100): the cap
- # is intentionally allowed to drift up rather than sacrificing
- # audit bookends.
- assert len(surviving) == 110, (
- "cap is allowed to drift up (110 > MAX_VERSIONS_PER_NOTE=100) "
- "rather than deleting audit bookends to hit the exact ceiling."
- )
- assert len(surviving) > 100
+ assert set(ordinary_ids).issubset(surviving)
+ assert set(bookend_ids).issubset(surviving)
+ assert len(surviving) == 8
class TestDeleteNoteRagCleanup: