~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
"""SERVICE-LAYER contracts for ``NoteService``, against a REAL on-disk DB.
Scope is the service beneath ``web/routers/notes.py`` --
``research_library/notes/services/note_service.py`` and the note models.
The ROUTER (dependency ordering, the dead 413 path, JSON body handling)
is audited elsewhere and is deliberately NOT duplicated here: no FastAPI
app is booted, no TestClient exists in this file, every assertion drives
``NoteService`` directly.
Why a real database and not a ``MagicMock`` session: a mock cannot show
that a version row landed, that a cascade removed exactly one note's
history, or that a deliberately-seeded sibling survived.
Why ON-DISK and not ``:memory:``: in-memory SQLite is per-connection, so
a second connection cannot observe the first's committed state. The
concurrency tests below depend on two independent connections seeing
each other's commits -- on ``:memory:`` they would pass vacuously. Every
assertion reads through ``db.fresh(user)``, a brand-new session on the
same file, never through the session the service mutated.
``PRAGMA foreign_keys = ON`` and ``journal_mode = WAL`` are applied to
the test engines because production applies both on every connection
(``sqlcipher_utils.apply_performance_pragmas``). Without the FK pragma
every ``ondelete="CASCADE"`` on ``note_versions`` / ``note_references``
/ ``note_links`` is inert and the deletion cleanup under test would not
exist -- ``delete_note`` issues a bare ``session.delete(document)`` and
the model relationships are all ``passive_deletes=True``, i.e. it
delegates the entire cleanup to the database.
OWNERSHIP MODEL, since it is not obvious from the method signatures:
``NoteService`` never filters by a user column. Every one of its 34
session opens is ``get_user_db_session(self.username)``, and each user
has a SEPARATE database file. Isolation is therefore physical, not a
WHERE clause. The fixture models that faithfully with one engine per
user; a shared-engine fixture would test nothing.
Two characterization tests are named ``test_defect_*``. They pin
behaviour that is arguably WRONG, so that a future fix breaks them
loudly instead of silently:
* ``test_defect_annotation_anchor_served_unvalidated_after_target_edit``
* ``test_defect_concurrent_tag_edit_snapshots_a_phantom_state``
The 50 MB content cap is exercised by temporarily lowering the module
constant and asserting the service's own check fires -- no 50 MB payload
is ever allocated.
"""
import hashlib
import uuid
from collections import defaultdict
from contextlib import contextmanager
import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker
from local_deep_research.database.models import (
Base,
Document,
SourceType,
)
from local_deep_research.database.models.note import (
NoteLink,
NoteReference,
NoteVersion,
)
from local_deep_research.research_library.notes.services import (
note_service as note_service_module,
)
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
ALICE = "alice_notes"
BOB = "bob_notes"
_BOOKENDS = ("pre_restore", "restore")
# ---------------------------------------------------------------------------
# Harness
# ---------------------------------------------------------------------------
class _NotesDB:
"""One real on-disk SQLite file per user, plus the seams the service
needs stubbed.
The patched ``get_user_db_session`` hands out ONE long-lived session
per user and never closes it -- which is what production does (the
real context manager returns a reused THREAD-LOCAL session). It also
rolls back on entry at depth 0, mirroring
``thread_local_session.ThreadSessionManager.get_session``'s
stale-lock rollback for a non-nested call; without that the reused
session would keep a stale identity map across service calls and the
concurrency findings below would be artefacts of the fixture rather
than of the code.
"""
def __init__(self, root):
self._root = root
self._engines = {}
self._sessions = {}
self._overrides = {}
self._depth = defaultdict(int)
def engine(self, user):
if user not in self._engines:
path = self._root / f"{user}.db"
engine = create_engine(f"sqlite:///{path}")
@event.listens_for(engine, "connect")
def _pragmas(dbapi_connection, _record):
# Mirrors apply_performance_pragmas() in production.
cur = dbapi_connection.cursor()
cur.execute("PRAGMA foreign_keys = ON")
cur.execute("PRAGMA journal_mode = WAL")
cur.execute("PRAGMA busy_timeout = 3000")
cur.close()
Base.metadata.create_all(engine)
self._engines[user] = engine
session = sessionmaker(bind=engine)()
session.add(
SourceType(
id=str(uuid.uuid4()),
name="note",
display_name="Note",
description="User-created notes",
icon="sticky-note",
)
)
session.commit()
self._sessions[user] = session
return self._engines[user]
def _service_session(self, user):
self.engine(user)
return self._overrides.get(user) or self._sessions[user]
def fresh(self, user):
"""A brand-new session on the same file -- assertions only."""
return sessionmaker(bind=self.engine(user))()
def service(self, user):
return NoteService(user)
@contextmanager
def concurrent_session(self, user):
"""Bind the service to a SECOND, independent connection for the
duration of the block, so a nested service call commits from a
genuinely different transaction."""
other = sessionmaker(bind=self.engine(user))()
self._overrides[user] = other
try:
yield other
finally:
self._overrides.pop(user, None)
other.close()
@contextmanager
def session_cm(self, username=None, password=None, session_id=None):
session = self._service_session(username)
if self._depth[username] == 0:
session.rollback()
self._depth[username] += 1
try:
yield session
except Exception:
session.rollback()
raise
finally:
self._depth[username] -= 1
# -- seeding -----------------------------------------------------------
def add_library_document(self, user, doc_id, title, text):
"""A NON-note Document -- the only legal annotation target."""
session = self.fresh(user)
source_type = (
session.query(SourceType).filter_by(name="library_doc").first()
)
if source_type is None:
source_type = SourceType(
id=str(uuid.uuid4()),
name="library_doc",
display_name="Library Document",
description="Downloaded document",
icon="file",
)
session.add(source_type)
session.commit()
session.add(
Document(
id=doc_id,
source_type_id=source_type.id,
document_hash=hashlib.sha256(doc_id.encode()).hexdigest(),
file_size=len(text.encode("utf-8")),
file_type="pdf",
title=title,
text_content=text,
)
)
session.commit()
session.close()
def rewrite_document_text(self, user, doc_id, new_text):
"""Rewrite a library document's extracted text IN PLACE.
Not a test-only fiction: ``zotero/sync_service.py`` does exactly
this on re-sync (``existing.text_content = text``), gated only on
the document being exclusive to that Zotero mapping -- a check
that knows nothing about ``NoteReference`` anchors.
"""
session = self.fresh(user)
document = session.query(Document).filter_by(id=doc_id).first()
document.text_content = new_text
document.character_count = len(new_text)
session.commit()
session.close()
# -- reads -------------------------------------------------------------
def versions(self, user, note_id):
session = self.fresh(user)
rows = [
(v.id, v.change_type.value, v.title, v.content, list(v.tags or []))
for v in session.query(NoteVersion)
.filter_by(document_id=note_id)
.order_by(NoteVersion.created_at.asc(), NoteVersion.id.asc())
.all()
]
session.close()
return rows
def ordinary_versions(self, user, note_id):
return [
r for r in self.versions(user, note_id) if r[1] not in _BOOKENDS
]
def bookend_versions(self, user, note_id):
return [r for r in self.versions(user, note_id) if r[1] in _BOOKENDS]
def body(self, user, note_id):
session = self.fresh(user)
document = session.query(Document).filter_by(id=note_id).first()
value = None if document is None else document.text_content
session.close()
return value
def titles(self, user):
session = self.fresh(user)
values = sorted(d.title for d in session.query(Document).all())
session.close()
return values
def reference_rows(self, user):
session = self.fresh(user)
rows = sorted(
(r.note_id, r.target_document_id, r.target_research_id, r.quote)
for r in session.query(NoteReference).all()
)
session.close()
return rows
def link_rows(self, user):
session = self.fresh(user)
rows = sorted(
(link.source_document_id, link.target_document_id, link.link_text)
for link in session.query(NoteLink).all()
)
session.close()
return rows
def dispose(self):
for session in self._sessions.values():
session.close()
for engine in self._engines.values():
engine.dispose()
@pytest.fixture
def db(tmp_path, monkeypatch):
harness = _NotesDB(tmp_path)
monkeypatch.setattr(
note_service_module, "get_user_db_session", harness.session_cm
)
# No request thread here, so the AI change-summary worker can never
# get a DB password; make that explicit rather than depending on the
# ambient absence of a request context. update_note then logs and
# skips the submit, so no background thread touches the test DB.
monkeypatch.setattr(
note_service_module,
"_capture_request_db_password",
lambda username: None,
)
harness.engine(ALICE)
harness.engine(BOB)
try:
yield harness
finally:
harness.dispose()
# ---------------------------------------------------------------------------
# NOTE_CONTENT_MAX_BYTES is enforced by the SERVICE, not only the route
# ---------------------------------------------------------------------------
def test_content_cap_is_50mb_and_measured_in_bytes_not_characters(db):
"""The cap fires inside the service, on BYTES, with no route in play.
The route's 413 path is dead code (its cap equals the middleware's),
so this check is the only thing standing between a direct service
caller and an unbounded ``text_content``. The cap is lowered for the
test rather than allocating 50 MB; ``_assert_content_size`` reads the
module global at call time, so the lowered value exercises the real
branch.
The 6-character payload is 18 BYTES. A character-based cap would let
it through.
"""
alice = db.service(ALICE)
survivor = alice.create_note(title="Survivor", content="keep me", tags=[])
monkey_payload = "あ" * 6
assert len(monkey_payload) == 6
assert len(monkey_payload.encode("utf-8")) == 18
original = note_service_module.NOTE_CONTENT_MAX_BYTES
note_service_module.NOTE_CONTENT_MAX_BYTES = 16
try:
with pytest.raises(ValueError, match="exceeds maximum size"):
alice.create_note(title="Rejected", content=monkey_payload)
with pytest.raises(ValueError, match="exceeds maximum size"):
alice.update_note(survivor, content=monkey_payload)
finally:
note_service_module.NOTE_CONTENT_MAX_BYTES = original
assert note_service_module.NOTE_CONTENT_MAX_BYTES == 50 * 1024 * 1024
# The rejection happened BEFORE any write: no half-created note, and
# the seeded sibling is byte-identical with no extra version row.
assert db.titles(ALICE) == ["Survivor"]
assert db.body(ALICE, survivor) == "keep me"
assert len(db.versions(ALICE, survivor)) == 1
def test_content_cap_boundary_is_strictly_greater_than(db):
"""Exactly-at-cap is accepted; one byte over is refused. Pins the
comparison direction so a future ``>=`` typo is caught."""
alice = db.service(ALICE)
note_id = alice.create_note(title="Boundary", content="seed", tags=[])
original = note_service_module.NOTE_CONTENT_MAX_BYTES
note_service_module.NOTE_CONTENT_MAX_BYTES = 16
try:
assert alice.update_note(note_id, content="A" * 16) is True
with pytest.raises(ValueError, match="exceeds maximum size"):
alice.update_note(note_id, content="B" * 17)
finally:
note_service_module.NOTE_CONTENT_MAX_BYTES = original
# The refused 17-byte save left the accepted 16-byte one intact.
assert db.body(ALICE, note_id) == "A" * 16
@pytest.mark.parametrize("bad", [123, None, b"bytes", ["list"], {"d": 1}])
def test_non_string_content_is_a_valueerror_not_an_attributeerror(db, bad):
"""A truthy non-string body passes the route's ``if not content``
guard; without the service's type check ``.encode()`` raised
AttributeError, surfacing as an opaque 500 instead of a 400."""
alice = db.service(ALICE)
with pytest.raises(ValueError, match="content must be a string"):
alice.create_note(title="Bad", content=bad)
assert db.titles(ALICE) == []
# ---------------------------------------------------------------------------
# Ownership
# ---------------------------------------------------------------------------
def test_another_users_note_id_is_refused_by_every_mutating_method(db):
"""Bob cannot read, edit, restore or delete Alice's note.
Bob owns a REAL note of his own, seeded for exactly this purpose: it
proves the refusals below are isolation, not a service that happens
to fail on everything.
"""
alice = db.service(ALICE)
bob = db.service(BOB)
alice_note = alice.create_note(
title="Alice private", content="alice secret body", tags=["a"]
)
alice.update_note(alice_note, content="alice secret body v2")
alice_version_id = db.versions(ALICE, alice_note)[0][0]
bob_note = bob.create_note(title="Bob own", content="bob body", tags=["b"])
assert bob.get_note(alice_note) is None
assert bob.note_exists(alice_note) is False
assert bob.update_note(alice_note, content="pwned") is False
assert bob.restore_with_bookends(alice_note, alice_version_id) == (
False,
"note_not_found",
)
assert bob.get_note_collections(alice_note) == []
assert bob.get_note_research(alice_note) == []
assert bob.delete_note(alice_note) is False
# Positive control: the same calls on Bob's OWN note all work.
assert bob.get_note(bob_note)["title"] == "Bob own"
assert bob.note_exists(bob_note) is True
assert bob.update_note(bob_note, content="bob body v2") is True
assert db.body(BOB, bob_note) == "bob body v2"
# And Alice is completely unharmed.
assert db.body(ALICE, alice_note) == "alice secret body v2"
assert db.titles(ALICE) == ["Alice private"]
assert len(db.versions(ALICE, alice_note)) == 2
# Alice never gained a row from Bob's attempts, and vice versa.
assert db.titles(BOB) == ["Bob own"]
def test_restore_refuses_a_version_id_belonging_to_a_sibling_note(db):
"""Cross-NOTE (not cross-user) version confusion: restoring note A to
note B's version id must fail, or one note's history could overwrite
another's body."""
alice = db.service(ALICE)
target = alice.create_note(title="Target", content="target v1", tags=[])
alice.update_note(target, content="target v2")
sibling = alice.create_note(title="Sibling", content="sibling v1", tags=[])
sibling_version_id = db.versions(ALICE, sibling)[0][0]
assert alice.restore_with_bookends(target, sibling_version_id) == (
False,
"version_not_found",
)
assert db.body(ALICE, target) == "target v2"
assert db.bookend_versions(ALICE, target) == []
# Control: that same version id restores the note it belongs to.
assert alice.restore_with_bookends(sibling, sibling_version_id) == (
True,
None,
)
assert db.body(ALICE, sibling) == "sibling v1"
# ---------------------------------------------------------------------------
# Versioning
# ---------------------------------------------------------------------------
def test_create_writes_initial_and_each_state_edit_appends_one_version(db):
"""One version per edit of the VERSIONED state (title, content,
tags). ``favorite``/pinned is not versioned state and must not
manufacture a snapshot."""
alice = db.service(ALICE)
note_id = alice.create_note(title="T1", content="c1", tags=["x"])
assert [(r[1], r[2], r[3], r[4]) for r in db.versions(ALICE, note_id)] == [
("initial", "T1", "c1", ["x"])
]
alice.update_note(note_id, content="c2")
alice.update_note(note_id, title="T2")
alice.update_note(note_id, tags=["x", "y"])
assert [(r[1], r[2], r[3], r[4]) for r in db.versions(ALICE, note_id)] == [
("initial", "T1", "c1", ["x"]),
("manual_save", "T1", "c2", ["x"]),
("manual_save", "T2", "c2", ["x"]),
("manual_save", "T2", "c2", ["x", "y"]),
]
before = db.versions(ALICE, note_id)
assert alice.update_note(note_id, favorite=True) is True
assert db.versions(ALICE, note_id) == before
assert alice.get_note(note_id)["pinned"] is True
def test_resaving_an_identical_state_is_deduped_not_double_versioned(db):
alice = db.service(ALICE)
note_id = alice.create_note(title="T", content="body", tags=["t"])
assert alice.update_note(note_id, content="changed") is True
assert len(db.versions(ALICE, note_id)) == 2
assert alice.update_note(note_id, content="changed") is True
assert len(db.versions(ALICE, note_id)) == 2
def test_a_full_state_revert_leaves_no_trace_in_history(db):
"""Documented consequence of hashing the whole (title, content, tags)
triple: reverting a note to an EXACT earlier state is absorbed by the
dedup check, so the document body changes but history gains no row.
The revert is still reflected in the note itself -- nothing is lost,
but the history cannot tell you the revert happened.
"""
alice = db.service(ALICE)
note_id = alice.create_note(title="T", content="v1", tags=["k"])
alice.update_note(note_id, content="v2")
assert len(db.versions(ALICE, note_id)) == 2
assert (
alice.update_note(note_id, title="T", content="v1", tags=["k"]) is True
)
assert db.body(ALICE, note_id) == "v1"
assert len(db.versions(ALICE, note_id)) == 2
def test_restore_actually_restores_and_writes_both_audit_bookends(db):
alice = db.service(ALICE)
note_id = alice.create_note(title="T1", content="v1 body", tags=["a"])
alice.update_note(note_id, title="T2", content="v2 body", tags=["b"])
alice.update_note(note_id, content="v3 body")
assert db.body(ALICE, note_id) == "v3 body"
first = db.versions(ALICE, note_id)[0]
assert first[3] == "v1 body"
assert alice.restore_with_bookends(note_id, first[0]) == (True, None)
# The document really moved back -- read through a fresh connection.
restored = alice.get_note(note_id)
assert restored["content"] == "v1 body"
assert restored["title"] == "T1"
assert restored["tags"] == ["a"]
bookends = {(r[1], r[3]) for r in db.bookend_versions(ALICE, note_id)}
assert bookends == {("pre_restore", "v3 body"), ("restore", "v1 body")}
# The ordinary history is untouched by the restore.
assert [r[3] for r in db.ordinary_versions(ALICE, note_id)] == [
"v1 body",
"v2 body",
"v3 body",
]
def test_version_prune_caps_ordinary_rows_and_spares_bookends_and_siblings(db):
"""FIFO prune of ordinary snapshots must not eat the restore audit
trail, and must not touch another note's history.
``MAX_VERSIONS_PER_NOTE`` is lowered instead of issuing 101 saves;
``_prune_versions_in_session`` reads the module global at call time.
"""
alice = db.service(ALICE)
note_id = alice.create_note(title="T", content="c0", tags=[])
sibling = alice.create_note(title="S", content="s0", tags=[])
for i in range(1, 4):
alice.update_note(sibling, content=f"s{i}")
assert len(db.versions(ALICE, sibling)) == 4
initial_version_id = db.versions(ALICE, note_id)[0][0]
assert alice.restore_with_bookends(note_id, initial_version_id) == (
True,
None,
)
assert len(db.bookend_versions(ALICE, note_id)) == 2
original = note_service_module.MAX_VERSIONS_PER_NOTE
note_service_module.MAX_VERSIONS_PER_NOTE = 3
try:
for i in range(1, 8):
alice.update_note(note_id, content=f"c{i}")
finally:
note_service_module.MAX_VERSIONS_PER_NOTE = original
ordinary = db.ordinary_versions(ALICE, note_id)
assert len(ordinary) == 3
# FIFO: the NEWEST three survive, the oldest (including 'initial')
# were dropped.
assert [r[3] for r in ordinary] == ["c5", "c6", "c7"]
assert "initial" not in {r[1] for r in ordinary}
# The bookends were excluded from that prune...
assert {r[1] for r in db.bookend_versions(ALICE, note_id)} == {
"pre_restore",
"restore",
}
# ...and the sibling's history is entirely untouched.
assert [r[3] for r in db.versions(ALICE, sibling)] == [
"s0",
"s1",
"s2",
"s3",
]
def test_bookend_pool_has_its_own_independent_cap(db):
"""Repeated restores must not grow ``note_versions`` without bound,
and capping them must not consume the ordinary budget."""
alice = db.service(ALICE)
note_id = alice.create_note(title="T", content="c0", tags=[])
alice.update_note(note_id, content="c1")
sibling = alice.create_note(title="S", content="s0", tags=[])
target_version_id = db.ordinary_versions(ALICE, note_id)[0][0]
original = note_service_module.MAX_BOOKEND_VERSIONS
note_service_module.MAX_BOOKEND_VERSIONS = 2
try:
for _ in range(4):
assert alice.restore_with_bookends(note_id, target_version_id) == (
True,
None,
)
finally:
note_service_module.MAX_BOOKEND_VERSIONS = original
assert len(db.bookend_versions(ALICE, note_id)) == 2
assert len(db.ordinary_versions(ALICE, note_id)) == 2
assert len(db.versions(ALICE, sibling)) == 1
# ---------------------------------------------------------------------------
# Annotation anchors
# ---------------------------------------------------------------------------
_TARGET_TEXT = (
"Chapter one. The alpha result holds under load. Chapter two ends."
)
def _seed_annotation(db, user):
db.add_library_document(user, "LIBDOC", "Paper", _TARGET_TEXT)
service = db.service(user)
note_id = service.create_note_for_document(
"LIBDOC",
title="Annotation",
content="my comment",
quote="The alpha result holds",
prefix="Chapter one. ",
suffix=" under load",
)
return service, note_id
def test_editing_the_annotating_note_leaves_its_anchor_byte_identical(db):
"""The GOOD half of the anchoring story.
Anchors live on ``NoteReference`` and address the TARGET's text, not
the note's own body, so rewriting the note -- including edits that
shift every character in it -- cannot move the highlight. Pinned so a
future refactor that starts deriving anchors from note content is
caught.
"""
service, note_id = _seed_annotation(db, ALICE)
before = service.get_annotations_for_target(document_id="LIBDOC")
assert len(before) == 1
assert (
service.update_note(
note_id,
title="Renamed annotation",
content="A completely rewritten and much longer comment body "
"that shifts every offset inside the note itself.",
tags=["moved"],
)
is True
)
# The note really did change...
assert db.body(ALICE, note_id).startswith("A completely rewritten")
assert len(db.versions(ALICE, note_id)) == 2
after = service.get_annotations_for_target(document_id="LIBDOC")
assert len(after) == 1
assert (after[0]["quote"], after[0]["prefix"], after[0]["suffix"]) == (
before[0]["quote"],
before[0]["prefix"],
before[0]["suffix"],
)
def test_defect_annotation_anchor_served_unvalidated_after_target_edit(db):
"""DEFECT (characterization). An anchor whose target text has changed
is still served verbatim, with nothing in the payload that could
reveal it is stale -- so the highlight can land on a DIFFERENT, even
contradicting, passage.
The design rests on targets being immutable ("library documents'
extracted text is immutable", ``create_note_for_document``). Nothing
enforces that: ``zotero/sync_service.py`` rewrites
``Document.text_content`` in place on re-sync, gated only on the
document being exclusive to that Zotero mapping -- a check with no
knowledge of ``NoteReference``.
After such a rewrite this test shows, on a real database:
* ``get_annotations_for_target`` returns the SAME quote/prefix/
suffix -- no re-validation, no staleness flag, no offset, no
target-text hash;
* the stored ``prefix`` no longer precedes the quote in the target,
i.e. the recorded context is now WRONG;
* the quote nevertheless still occurs exactly once, and the
renderer's anchor engine (``annotation_surface.js``
``findQuoteIndex``) returns ``hits[0]`` without consulting
prefix/suffix whenever there is exactly one hit -- so the comment
silently re-attaches to the new sentence.
A dropped annotation would be visible. This one is not.
"""
service, note_id = _seed_annotation(db, ALICE)
before = service.get_annotations_for_target(document_id="LIBDOC")[0]
# No field in the payload could tell a client the anchor is stale.
assert set(before) == {
"note_id",
"quote",
"prefix",
"suffix",
"created_at",
"note_title",
"comment_preview",
}
rewritten = (
"Preamble added by re-extraction. Chapter zero. "
"Contradiction: The alpha result holds only in vacuum. The end."
)
db.rewrite_document_text(ALICE, "LIBDOC", rewritten)
after = service.get_annotations_for_target(document_id="LIBDOC")
assert len(after) == 1
assert after[0]["note_id"] == note_id
# Served byte-identical despite the target having been rewritten.
assert (after[0]["quote"], after[0]["prefix"], after[0]["suffix"]) == (
before["quote"],
before["prefix"],
before["suffix"],
)
assert set(after[0]) == set(before)
quote = after[0]["quote"]
index = rewritten.find(quote)
# Exactly one occurrence -> the renderer takes it without checking
# the context it was stored with.
assert index != -1
assert rewritten.find(quote, index + 1) == -1
# ...and that context is now wrong: the recorded prefix does not
# precede the quote any more.
stored_prefix = after[0]["prefix"]
assert (
rewritten[max(0, index - len(stored_prefix)) : index] != stored_prefix
)
# The passage the comment now highlights says the opposite.
assert "only in vacuum" in rewritten[index : index + len(quote) + 20]
def test_anchor_quote_is_never_validated_against_the_target(db):
"""An anchor whose quote does not occur in the target at all is
accepted and stored. Pins that validation is entirely absent at write
time, not merely at read time.
A control anchor that DOES occur is seeded alongside, so this is not
just "the service accepts everything": both land, and the service
cannot tell them apart.
"""
db.add_library_document(ALICE, "LIBDOC", "Paper", _TARGET_TEXT)
service = db.service(ALICE)
good = service.create_note_for_document(
"LIBDOC", title="Good", content="c", quote="The alpha result holds"
)
bogus = service.create_note_for_document(
"LIBDOC",
title="Bogus",
content="c",
quote="this phrase appears nowhere in the target",
)
annotations = service.get_annotations_for_target(document_id="LIBDOC")
assert {a["note_id"] for a in annotations} == {good, bogus}
quotes = {a["quote"] for a in annotations}
assert "this phrase appears nowhere in the target" in quotes
assert all(a["quote"] in _TARGET_TEXT for a in annotations) is False
assert service.has_annotation(bogus, document_id="LIBDOC") is True
def test_notes_are_refused_as_annotation_targets_but_documents_are_not(db):
"""The only guard that DOES exist against anchor drift: you cannot
annotate a note, because note bodies are mutable.
Paired with a control non-note target of identical shape that IS
accepted, so the refusal is about note-ness and not about the call
failing generally.
"""
db.add_library_document(ALICE, "LIBDOC", "Paper", _TARGET_TEXT)
service = db.service(ALICE)
a_note = service.create_note(title="A note", content=_TARGET_TEXT, tags=[])
with pytest.raises(ValueError, match="notes cannot be annotated"):
service.create_note_for_document(a_note, quote="The alpha result holds")
with pytest.raises(LookupError, match="Document not found"):
service.create_note_for_document(
"no-such-document", quote="The alpha result holds"
)
# The refused calls created no orphan note.
assert db.titles(ALICE) == ["A note", "Paper"]
assert db.reference_rows(ALICE) == []
# Control: the same call against the non-note target succeeds.
accepted = service.create_note_for_document(
"LIBDOC", title="Accepted", quote="The alpha result holds"
)
assert db.reference_rows(ALICE) == [
(accepted, "LIBDOC", None, "The alpha result holds")
]
# ---------------------------------------------------------------------------
# Concurrency
# ---------------------------------------------------------------------------
def _interleave(monkeypatch, run_other):
"""Run ``run_other`` exactly once, from a second connection, at the
point inside ``update_note`` between its read of the document and its
commit. Deterministic stand-in for two request threads racing --
``_create_version_snapshot_in_session`` is called after the row is
loaded and before ``session.commit()``.
"""
original = NoteService._create_version_snapshot_in_session
fired = []
def hooked(
self,
session,
note_id,
title,
content,
tags,
change_type,
change_summary=None,
):
if not fired:
fired.append(1)
run_other()
return original(
self,
session,
note_id,
title,
content,
tags,
change_type,
change_summary,
)
monkeypatch.setattr(
NoteService, "_create_version_snapshot_in_session", hooked
)
return fired
def test_concurrent_content_saves_are_last_write_wins_with_history_intact(
db, monkeypatch
):
"""PINNED: there is NO optimistic concurrency control on notes.
Two overlapping content saves both report success; the later commit
wins the document body outright, with no conflict error and no
signal to either caller. That is acceptable ONLY because the loser's
text survives as a version row and can be restored -- this test pins
both halves, so a change to either is caught.
"""
alice = db.service(ALICE)
note_id = alice.create_note(title="T", content="base", tags=[])
def writer_b():
with db.concurrent_session(ALICE):
assert (
NoteService(ALICE).update_note(note_id, content="B-writes-this")
is True
)
fired = _interleave(monkeypatch, writer_b)
assert alice.update_note(note_id, content="A-writes-this") is True
assert fired == [1]
# Last write wins -- B's body is gone from the document.
assert db.body(ALICE, note_id) == "A-writes-this"
# But B's save is NOT lost: it is in history, and restorable.
contents = [r[3] for r in db.versions(ALICE, note_id)]
assert contents == ["base", "B-writes-this", "A-writes-this"]
b_version_id = db.versions(ALICE, note_id)[1][0]
assert alice.restore_with_bookends(note_id, b_version_id) == (True, None)
assert db.body(ALICE, note_id) == "B-writes-this"
def test_defect_concurrent_tag_edit_snapshots_a_phantom_state(db, monkeypatch):
"""DEFECT (characterization): a tag-only save concurrent with a
content save writes a version row for a state the note NEVER had, and
restoring that row silently reverts the content save.
Mechanism: ``update_note`` snapshots ``document.title /
.text_content / .tags`` from the row IT loaded. When a content save
on another connection commits in between, the tag-only writer's
in-memory ``text_content`` is stale. SQLAlchemy only UPDATEs dirty
columns, so the document body is correctly preserved -- the immediate
write is safe. The fabricated snapshot is what is dangerous: it pairs
the OLD body with the NEW tags, and it is offered to the user as a
restorable point in their history.
So the lost update is not prevented, it is DEFERRED into version
history, where it is indistinguishable from a genuine snapshot.
"""
alice = db.service(ALICE)
note_id = alice.create_note(title="T", content="ORIGINAL", tags=["old"])
def content_writer():
with db.concurrent_session(ALICE):
assert (
NoteService(ALICE).update_note(note_id, content="B-NEW-CONTENT")
is True
)
fired = _interleave(monkeypatch, content_writer)
assert alice.update_note(note_id, tags=["new"]) is True
assert fired == [1]
# Immediate state is correct: the concurrent body survived, and the
# tag change applied.
assert db.body(ALICE, note_id) == "B-NEW-CONTENT"
assert alice.get_note(note_id)["tags"] == ["new"]
states = [(r[3], r[4]) for r in db.versions(ALICE, note_id)]
assert ("ORIGINAL", ["old"]) in states
assert ("B-NEW-CONTENT", ["old"]) in states
# The phantom: old body + new tags. This pairing never existed.
assert ("ORIGINAL", ["new"]) in states
phantom_id = [
r[0]
for r in db.versions(ALICE, note_id)
if (r[3], r[4]) == ("ORIGINAL", ["new"])
][0]
assert alice.restore_with_bookends(note_id, phantom_id) == (True, None)
# Restoring the phantom silently threw away the concurrent save.
assert db.body(ALICE, note_id) == "ORIGINAL"
# ---------------------------------------------------------------------------
# Deletion cleanup
# ---------------------------------------------------------------------------
def test_delete_note_removes_its_versions_annotations_and_links_only(db):
"""Deleting a note must clean up its version history, its annotation
anchors and its wiki-links -- and nothing else.
``delete_note`` issues a bare ``session.delete(document)``; every
note relationship is ``passive_deletes=True``, so the entire cleanup
is the database's ``ON DELETE CASCADE``. Each assertion is paired
with a sibling note seeded for exactly this purpose whose rows must
survive.
"""
db.add_library_document(ALICE, "LIBDOC", "Paper", _TARGET_TEXT)
alice = db.service(ALICE)
link_target = alice.create_note(
title="LinkTarget", content="pointed at", tags=[]
)
doomed = alice.create_note_for_document(
"LIBDOC",
title="Doomed",
content="doomed body [[LinkTarget]]",
quote="The alpha result holds",
)
alice.update_note(doomed, content="doomed body v2 [[LinkTarget]]")
survivor = alice.create_note_for_document(
"LIBDOC",
title="Survivor",
content="survivor body [[LinkTarget]]",
quote="Chapter two ends",
)
alice.update_note(survivor, content="survivor body v2 [[LinkTarget]]")
assert len(db.versions(ALICE, doomed)) == 2
assert len(db.versions(ALICE, survivor)) == 2
assert len(db.reference_rows(ALICE)) == 2
assert sorted(row[0] for row in db.link_rows(ALICE)) == sorted(
[doomed, survivor]
)
assert alice.delete_note(doomed) is True
# Target gone, every row of it gone...
assert db.body(ALICE, doomed) is None
assert db.versions(ALICE, doomed) == []
assert alice.get_note(doomed) is None
assert [row for row in db.reference_rows(ALICE) if row[0] == doomed] == []
assert [row for row in db.link_rows(ALICE) if row[0] == doomed] == []
assert alice.has_annotation(doomed, document_id="LIBDOC") is False
# ...and the annotation disappeared from the target's panel.
remaining = alice.get_annotations_for_target(document_id="LIBDOC")
assert [a["note_id"] for a in remaining] == [survivor]
# ...while the seeded sibling kept everything.
assert db.body(ALICE, survivor) == "survivor body v2 [[LinkTarget]]"
assert len(db.versions(ALICE, survivor)) == 2
assert db.link_rows(ALICE) == [(survivor, link_target, "LinkTarget")]
assert db.reference_rows(ALICE) == [
(survivor, "LIBDOC", None, "Chapter two ends")
]
assert alice.has_annotation(survivor, document_id="LIBDOC") is True
# The link TARGET and the annotated document are untouched.
assert sorted(db.titles(ALICE)) == [
"LinkTarget",
"Paper",
"Survivor",
]
# ---------------------------------------------------------------------------
# Default Notes collection creation and the search-engine cache (#6609)
# ---------------------------------------------------------------------------
def test_creating_default_notes_collection_invalidates_engine_cache(
db, monkeypatch
):
"""The first notes access creates the default Notes collection; that
creation must drop the user's cached collection-engine list so a
research started within the 30 s TTL offers it as a tool (#6609)."""
calls = []
monkeypatch.setattr(
"local_deep_research.web_search_engines.search_engines_config."
"invalidate_collection_engines_cache",
lambda username=None: calls.append(username),
)
alice = db.service(ALICE)
collection_id = alice.get_notes_collection_id()
assert collection_id is not None
assert calls == [ALICE]
# Second resolution finds the existing collection: no creation, no
# further cache churn.
assert alice.get_notes_collection_id() == collection_id
assert calls == [ALICE]