fix(vector): address PR review round 12 — bool guard + strict doc_id validation

- _group_int_doc_ids: use type(value) is not int instead of isinstance,
  since bool is an int subclass and would otherwise stringify to
  "True"/"False" and corrupt legacy payloads on backfill.
- Replace doc_id.isdigit() guards in 5 boundary sites
  (api/visualization, auth/viz_routes, search/context note/news_item/
  deck_card branches) with a shared is_valid_nextcloud_doc_id helper
  that rejects "0", leading zeros, and Unicode digit classes
  (superscripts, Arabic-Indic, Devanagari) which pass isdigit() but
  cannot be valid MySQL AUTO_INCREMENT IDs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-05-09 20:28:47 +02:00
co-authored by Claude Opus 4.7
parent f3ce46da0f
commit f9ad7dc52e
8 changed files with 133 additions and 24 deletions
+30
View File
@@ -678,6 +678,36 @@ def test_group_int_doc_ids_skips_float_and_warns(caplog):
assert "99" in msg
@pytest.mark.unit
def test_group_int_doc_ids_skips_bool_and_warns(caplog):
"""A bool doc_id is not stringified to "True"/"False"; it logs and skips.
``isinstance(True, int)`` is ``True`` because ``bool`` is a subclass of
``int`` in Python, so a naive ``isinstance(value, int)`` guard would let
a boolean payload through and write ``str(True)`` → ``"True"`` into
Qdrant. Producers never write bools, but the strict ``type(value) is
int`` guard ensures any future producer bug surfaces as a WARNING and is
not silently stringified.
"""
bool_point = SimpleNamespace(id=33, payload={"doc_id": True})
int_point = SimpleNamespace(id=42, payload={"doc_id": 7})
with caplog.at_level("WARNING", logger="nextcloud_mcp_server.vector.qdrant_client"):
by_value, scanned = _group_int_doc_ids([bool_point, int_point])
# Only the int point made it into by_value — "True" is *not* a key.
assert by_value == {"7": [42]}
assert "True" not in by_value
assert "False" not in by_value
assert scanned == 2
warnings = [r for r in caplog.records if r.levelname == "WARNING"]
assert len(warnings) == 1
msg = warnings[0].getMessage()
assert "bool" in msg
assert "33" in msg
@pytest.mark.unit
def test_group_int_doc_ids_handles_str_and_missing_silently(caplog):
"""str / missing doc_id payloads are skipped without warning.