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
+18 -4
View File
@@ -57,6 +57,23 @@ logger = logging.getLogger(__name__)
_USAGE_METADATA_MAX_DOC_TYPES = 16
def _consent_narrowed_doc_types(
doc_types: list[str] | None, allowed: frozenset[str]
) -> list[str]:
"""Apply the admin allow-set to a requested ``doc_types`` filter.
Caller has already established ``allowed is not None`` (a concrete allow-set;
``None`` means "no restriction" and is handled by skipping this call). When
no explicit ``doc_types`` are requested, restrict to the full allow-set;
otherwise intersect (preserving the caller's order). An empty result means
nothing the caller asked for is admin-approved — the caller short-circuits
to an empty response rather than falling through to an all-types search.
"""
if doc_types is None:
return sorted(allowed)
return [dt for dt in doc_types if dt in allowed]
async def record_search_usage(
*,
enabled: bool,
@@ -309,10 +326,7 @@ def configure_semantic_tools(mcp: FastMCP):
# means the admin disabled every source.
allowed = await allowed_doc_types(client, username)
if allowed is not None:
if doc_types is None:
doc_types = sorted(allowed)
else:
doc_types = [dt for dt in doc_types if dt in allowed]
doc_types = _consent_narrowed_doc_types(doc_types, allowed)
if not doc_types:
logger.info(
"Semantic search short-circuited for user %s: no requested "