~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
"""Deletion-path log lines must not print ``...`` after an untruncated id.
``f"{some_id[:8]}..."`` renders ``dockey...`` for a six-character id, so an
operator cannot tell a shortened id from a complete one (#5583). Every such
site on a deletion path renders its ids through ``short_id`` instead.
This module does two things:
* a tree-wide static guard that fails when the inline ``<id>[:N]`` plus
``...`` shape comes back, in the whole deletion package and in any
deletion-named function elsewhere in ``src``, so reverting any single
site is caught and not only the one that has a log assertion;
* behavioural pins for the cascade helper, which runs inside every document
delete.
"""
import ast
import re
from pathlib import Path
from typing import NamedTuple
from unittest.mock import MagicMock, patch
import pytest
import local_deep_research
from local_deep_research.research_library.deletion.utils.cascade_helper import (
CascadeHelper,
)
SRC_ROOT = Path(local_deep_research.__file__).resolve().parent
DELETION_PACKAGE = ("research_library", "deletion")
# Functions whose name says they delete or clean something up. Code in them
# is a deletion path wherever it lives (chat, sessions, the library service).
DELETION_FUNCTION = re.compile(
r"delet|remov|purg|destroy|cleanup|clean_up", re.IGNORECASE
)
ELLIPSES = ("...", "…")
class Hit(NamedTuple):
lineno: int
scope: tuple # enclosing classes and functions, outermost first
functions: tuple # the enclosing functions only
expression: str
def _is_prefix_slice(node):
"""True for ``expr[:n]`` and ``expr[0:n]``, whatever the bound."""
if not isinstance(node, ast.Subscript) or not isinstance(
node.slice, ast.Slice
):
return False
lower, upper, step = node.slice.lower, node.slice.upper, node.slice.step
starts_at_zero = lower is None or (
isinstance(lower, ast.Constant) and lower.value == 0
)
return starts_at_zero and upper is not None and step is None
def _starts_with_ellipsis(node):
return (
isinstance(node, ast.Constant)
and isinstance(node.value, str)
and node.value.startswith(ELLIPSES)
)
def _last_operand(node):
"""The right-most operand of a ``+`` chain (``a + b + c`` gives ``c``)."""
while isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add):
node = node.right
return node
class _EllipsisFinder(ast.NodeVisitor):
"""Collect ``<expr>[:n]`` immediately followed by an ellipsis literal.
Two shapes are flagged: an f-string replacement field holding the slice
whose next literal part starts with ``...``, and ``slice + "..."``. A
``slice + "..."`` that is a branch of a conditional expression is not
flagged, because that is the explicitly guarded (correct) inline form.
"""
def __init__(self):
self.scope = []
self.functions = []
self.hits = []
self._guarded = set()
def _enter_function(self, node):
self.scope.append(node.name)
self.functions.append(node.name)
self.generic_visit(node)
self.functions.pop()
self.scope.pop()
def visit_ClassDef(self, node):
self.scope.append(node.name)
self.generic_visit(node)
self.scope.pop()
visit_FunctionDef = _enter_function
visit_AsyncFunctionDef = _enter_function
def _hit(self, sliced):
self.hits.append(
Hit(
sliced.lineno,
tuple(self.scope),
tuple(self.functions),
ast.unparse(sliced),
)
)
def visit_JoinedStr(self, node):
for part, following in zip(node.values, node.values[1:]):
if (
isinstance(part, ast.FormattedValue)
and _is_prefix_slice(part.value)
and _starts_with_ellipsis(following)
):
self._hit(part.value)
self.generic_visit(node)
def visit_IfExp(self, node):
for branch in (node.body, node.orelse):
if isinstance(branch, ast.BinOp):
self._guarded.add(id(branch))
self.generic_visit(node)
def visit_BinOp(self, node):
if (
isinstance(node.op, ast.Add)
and id(node) not in self._guarded
and _starts_with_ellipsis(node.right)
):
sliced = _last_operand(node.left)
if _is_prefix_slice(sliced):
self._hit(sliced)
self.generic_visit(node)
def find_ellipsis_hits(source):
"""Every ``<expr>[:n]`` in ``source`` that is followed by an ellipsis."""
finder = _EllipsisFinder()
finder.visit(ast.parse(source))
return finder.hits
def find_violations(src_root):
"""Flagged expressions on deletion paths under ``src_root``."""
violations = []
for path in sorted(Path(src_root).rglob("*.py")):
relative = path.relative_to(src_root)
in_deletion_package = relative.parts[:2] == DELETION_PACKAGE
for hit in find_ellipsis_hits(path.read_text(encoding="utf-8")):
if in_deletion_package or any(
DELETION_FUNCTION.search(name) for name in hit.functions
):
where = ".".join(hit.scope) or "<module>"
violations.append(
f"{relative.as_posix()}:{hit.lineno} in {where}: "
f"{hit.expression} is followed by a literal ellipsis"
)
return violations
class TestDeletionPathsNeverAppendEllipsisToAnIdSlice:
"""The guard over the real source tree."""
def test_no_site_prints_an_unconditional_ellipsis_after_an_id_slice(self):
violations = find_violations(SRC_ROOT)
assert not violations, (
"A deletion path logs a sliced value followed by a hard-coded "
"'...', so ids that were never truncated read as truncated "
"(#5583). Render it with short_id(value, n) from "
"local_deep_research.utilities.formatting instead:\n "
+ "\n ".join(violations)
)
def test_guard_is_not_vacuous(self):
"""The deletion package is where the guard expects it to be."""
package = SRC_ROOT.joinpath(*DELETION_PACKAGE)
modules = {path.name for path in package.rglob("*.py")}
assert {
"cascade_helper.py",
"collection_deletion.py",
"document_deletion.py",
} <= modules
class TestFinderRecognisesTheShape:
"""The guard itself: what it flags and what it must leave alone."""
@pytest.mark.parametrize(
"source",
[
pytest.param('f"gone {doc_id[:8]}..."', id="fstring"),
pytest.param('f"gone {doc_id[:8]}...: ok"', id="fstring-then-text"),
pytest.param('f"gone {doc_id[:8]}…"', id="unicode-ellipsis"),
pytest.param('f"gone {doc_id[0:8]}..."', id="explicit-zero-start"),
pytest.param('f"gone {doc_id[:12]}..."', id="other-width"),
pytest.param('f"gone {doc_id[:WIDTH]}..."', id="named-width"),
pytest.param('f"gone {doc.id[:8]}..."', id="attribute-id"),
pytest.param('f"gone {str(doc_id)[:8]}..."', id="call-result"),
pytest.param('f"gone {doc_id[:8]!r}..."', id="conversion"),
pytest.param('doc_id[:8] + "..."', id="concatenation"),
pytest.param(
'"gone " + doc_id[:8] + "..."', id="chained-concatenation"
),
pytest.param('doc_id[:8] + "…"', id="concatenation-unicode"),
pytest.param(
'f"gone {a[:8]}... and {b[:8]}..."', id="two-in-one-message"
),
],
)
def test_flags_an_inline_slice_followed_by_an_ellipsis(self, source):
assert find_ellipsis_hits(f"def delete_it(doc_id):\n {source}\n")
@pytest.mark.parametrize(
"source",
[
pytest.param('f"gone {short_id(doc_id)}"', id="helper"),
pytest.param('f"gone {doc_id[:8]}"', id="slice-without-ellipsis"),
pytest.param('f"gone {doc_id}..."', id="ellipsis-without-slice"),
pytest.param('f"gone {doc_id[2:8]}..."', id="not-a-prefix-slice"),
pytest.param('f"gone {doc_id[:8]} ..."', id="space-before-dots"),
pytest.param(
"f\"gone {doc_id[:8]}{'...' if len(doc_id) > 8 else ''}\"",
id="conditional-inside-the-field",
),
pytest.param(
'doc_id[:8] + "..." if len(doc_id) > 8 else doc_id',
id="guarded-concatenation",
),
pytest.param('doc_id[:8] + "-gone"', id="other-suffix"),
pytest.param(
'"gone " + doc_id[:8] + "-done"', id="chained-other-suffix"
),
],
)
def test_leaves_correct_code_alone(self, source):
assert not find_ellipsis_hits(f"def delete_it(doc_id):\n {source}\n")
def test_reports_the_scope_and_expression(self):
source = (
"class Svc:\n"
" def delete_it(self, doc_id):\n"
' return f"gone {doc_id[:8]}..."\n'
)
(hit,) = find_ellipsis_hits(source)
assert hit.scope == ("Svc", "delete_it")
assert hit.functions == ("delete_it",)
assert hit.expression == "doc_id[:8]"
class TestFindViolationsScope:
"""Which code the guard treats as a deletion path."""
SNIPPET = 'def {name}(doc_id):\n return f"gone {{doc_id[:8]}}..."\n'
@staticmethod
def _tree(tmp_path, files):
for relative, source in files.items():
target = tmp_path / relative
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(source, encoding="utf-8")
return tmp_path
def test_everything_in_the_deletion_package_is_scanned(self, tmp_path):
root = self._tree(
tmp_path,
{
"research_library/deletion/utils/helper.py": (
self.SNIPPET.format(name="anything")
),
"research_library/deletion/module_level.py": (
'MESSAGE = f"gone {doc_id[:8]}..."\n'
),
},
)
found = find_violations(root)
assert len(found) == 2
assert any(
"helper.py:" in line and " in anything:" in line for line in found
)
assert any("module_level.py:1 in <module>:" in line for line in found)
@pytest.mark.parametrize(
"name",
[
"delete_session",
"_delete_document_locked",
"remove_from_collection",
"purge_old_rows",
"destroy_session",
"_cleanup_chat_send_rows",
"clean_up_after_failure",
],
)
def test_deletion_named_functions_elsewhere_are_scanned(
self, tmp_path, name
):
root = self._tree(
tmp_path, {"chat/service.py": self.SNIPPET.format(name=name)}
)
assert len(find_violations(root)) == 1
def test_a_closure_inside_a_deletion_named_function_is_scanned(
self, tmp_path
):
source = (
"def delete_session(doc_id):\n"
" def _impl():\n"
' return f"gone {doc_id[:8]}..."\n'
" return _impl\n"
)
root = self._tree(tmp_path, {"web/routers/chat.py": source})
assert len(find_violations(root)) == 1
@pytest.mark.parametrize(
"name", ["create_session", "send_message", "get_session", "_impl"]
)
def test_other_functions_elsewhere_are_left_alone(self, tmp_path, name):
root = self._tree(
tmp_path, {"chat/service.py": self.SNIPPET.format(name=name)}
)
assert find_violations(root) == []
def test_a_deletion_named_class_alone_does_not_make_a_path(self, tmp_path):
source = (
"class DeletionRecord:\n"
" def describe(self, doc_id):\n"
' return f"gone {doc_id[:8]}..."\n'
)
root = self._tree(tmp_path, {"models/record.py": source})
assert find_violations(root) == []
CASCADE_MODULE = (
"local_deep_research.research_library.deletion.utils.cascade_helper"
)
def _debug_message(call):
"""Run ``call`` and return the one debug message the helper logged."""
with patch(f"{CASCADE_MODULE}.logger") as mock_logger:
call()
assert mock_logger.debug.call_count == 1
return mock_logger.debug.call_args.args[0]
def _delete_chunks(doc_id):
session = MagicMock()
session.query.return_value.filter.return_value.delete.return_value = 3
return lambda: CascadeHelper.delete_document_chunks(session, doc_id)
def _delete_blob(doc_id):
session = MagicMock()
blob = MagicMock()
blob.pdf_binary = b"1234"
session.query.return_value.filter_by.return_value.first.return_value = blob
return lambda: CascadeHelper.delete_document_blob(session, doc_id)
def _delete_completely(doc_id):
session = MagicMock()
session.query.return_value.filter_by.return_value.delete.return_value = 1
return lambda: CascadeHelper.delete_document_completely(session, doc_id)
CASCADE_LOGGING_CALLS = [
pytest.param(_delete_chunks, id="delete_document_chunks"),
pytest.param(_delete_blob, id="delete_document_blob"),
pytest.param(_delete_completely, id="delete_document_completely"),
]
class TestCascadeHelperLogsIdsWithoutMisleadingEllipsis:
"""CascadeHelper runs inside every document delete (#5583)."""
@pytest.mark.parametrize("make_call", CASCADE_LOGGING_CALLS)
@pytest.mark.parametrize("doc_id", ["dockey", "12345678"])
def test_an_id_that_was_never_truncated_gets_no_ellipsis(
self, make_call, doc_id
):
message = _debug_message(make_call(doc_id))
assert doc_id in message
assert f"{doc_id}..." not in message
@pytest.mark.parametrize("make_call", CASCADE_LOGGING_CALLS)
def test_a_truncated_id_still_ends_in_an_ellipsis(self, make_call):
message = _debug_message(make_call("0123456789abcdef"))
assert "01234567..." in message
assert "0123456789abcdef" not in message