fix(vector-sync): address round-2 review — one-shot backstop, helper, caps

- scanner: gate the consent backstop with a per-(user,doc_type) one-shot
  marker so a standing admin-disable doesn't re-enqueue idempotent deletes
  every scan tick; the marker clears when the type is re-enabled. Derive
  _TEXT_BACKSTOP_DOC_TYPES from INDEXED_DOC_TYPES so new indexed types are
  covered automatically
- semantic: extract _consent_narrowed_doc_types so the search-side narrowing
  is unit-testable; add tests for restrict/intersect/disjoint/empty
- purge route: cap doc_types length (abuse guard) -> 400
- tests: one-shot + re-enable backstop, too-many-doc_types 400

Deferred (noted on PR): per-document allowed_doc_types call is cache-hot;
purge "last error wins" — both logged. SonarCloud broad-except hotspots are
deliberate (noqa BLE001), reviewable in the UI.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-06-16 01:08:30 +02:00
co-authored by Claude Opus 4.8
parent 477fb02b0a
commit 24b8000a71
6 changed files with 158 additions and 8 deletions
@@ -0,0 +1,37 @@
"""Unit tests for the search-side consent narrowing in nc_semantic_search.
The narrowing logic (intersect requested doc_types with the admin allow-set,
or restrict to the allow-set when none requested) is extracted into
``_consent_narrowed_doc_types`` so it can be tested without exercising the full
search path. ``allowed is None`` (no restriction / fail-open) is handled by the
caller skipping this helper entirely.
"""
from __future__ import annotations
from nextcloud_mcp_server.server.semantic import _consent_narrowed_doc_types
def test_none_request_restricts_to_allow_set():
# No explicit doc_types -> search exactly the allowed set (sorted).
assert _consent_narrowed_doc_types(None, frozenset({"file", "note"})) == [
"file",
"note",
]
def test_request_intersected_with_allow_set_preserves_order():
result = _consent_narrowed_doc_types(
["deck_card", "note", "file"], frozenset({"note", "file"})
)
assert result == ["note", "file"]
def test_disjoint_request_yields_empty():
# Caller short-circuits to an empty response on [].
assert _consent_narrowed_doc_types(["deck_card"], frozenset({"note"})) == []
def test_empty_allow_set_blocks_all():
assert _consent_narrowed_doc_types(None, frozenset()) == []
assert _consent_narrowed_doc_types(["note"], frozenset()) == []
@@ -151,6 +151,20 @@ def test_bad_request_when_body_not_object(mocker):
purge.assert_not_called()
def test_bad_request_when_too_many_doc_types(mocker):
_patch_token(mocker)
purge = _patch_purge(mocker)
client = TestClient(_build_app())
resp = client.post(
"/api/v1/vector-sync/purge",
json={"doc_types": [f"t{i}" for i in range(65)]},
)
assert resp.status_code == 400
purge.assert_not_called()
def test_provisioning_required_returns_428(mocker):
_patch_token(mocker, "admin")
mocker.patch(
@@ -12,11 +12,21 @@ from types import SimpleNamespace
from typing import cast
from unittest.mock import AsyncMock
import pytest
from nextcloud_mcp_server.vector import scanner as scanner_module
from nextcloud_mcp_server.vector.queue.ports import TaskProducer
from nextcloud_mcp_server.vector.scanner import _enqueue_deletes_for_disabled_types
@pytest.fixture(autouse=True)
def _clear_backstop_state():
"""The one-shot guard is module-level; reset it between tests."""
scanner_module._consent_backstop_done.clear()
yield
scanner_module._consent_backstop_done.clear()
def _producer(send: AsyncMock) -> TaskProducer:
"""A minimal stand-in for the TaskProducer protocol (only ``send`` is used)."""
return cast(TaskProducer, SimpleNamespace(send=send))
@@ -79,3 +89,42 @@ async def test_noop_when_all_text_types_allowed(monkeypatch):
)
assert queued == 0
send.assert_not_called()
async def test_one_shot_does_not_reflood_on_subsequent_scans(monkeypatch):
_patch_qdrant(monkeypatch, {"note": ["n1", "n2"]})
send = AsyncMock()
allowed = frozenset({"file"}) # note disabled
first = await _enqueue_deletes_for_disabled_types(
"alice", _producer(send), allowed, 1
)
second = await _enqueue_deletes_for_disabled_types(
"alice", _producer(send), allowed, 2
)
assert first == 2
# Standing disable: the next scan must not re-enqueue the same deletes.
assert second == 0
async def test_re_enable_then_disable_retriggers_backstop(monkeypatch):
_patch_qdrant(monkeypatch, {"note": ["n1"]})
send = AsyncMock()
disabled = frozenset({"file"})
enabled = frozenset({"file", "note"})
assert (
await _enqueue_deletes_for_disabled_types("alice", _producer(send), disabled, 1)
== 1
)
# Re-enabled: clears the one-shot marker.
assert (
await _enqueue_deletes_for_disabled_types("alice", _producer(send), enabled, 2)
== 0
)
# Disabled again: backstop fires once more.
assert (
await _enqueue_deletes_for_disabled_types("alice", _producer(send), disabled, 3)
== 1
)