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
@@ -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
)