fix(vector): address PR review round 6 + SonarCloud findings

Reviewer feedback (3 important + 3 nits):

- Wrap _ensure_keyword_payload_indexes' get_collection() call in
  try/except. The qdrant_client singleton is already assigned by the
  time this function runs, so a transient timeout/DNS failure
  propagating out left the process holding a usable client with the
  migration silently skipped on every subsequent call. Now logs ERROR
  with exc_info and returns; next process restart retries.
- Add `and "doc_id" in point.payload` guard to the four set
  comprehensions in scanner.py (indexed_doc_ids, indexed_file_ids,
  indexed_item_ids, indexed_card_ids). Previously a payload missing
  the doc_id key would raise KeyError and crash the entire scan.
- Tighten test_ensure_keyword_payload_indexes_logs_400_as_warning to
  match the per-field warning prefix exactly (`startswith("Schema
  conflict on payload index")`), so a future change adding 400s to
  the partial-failure summary surfaces here as a count mismatch.
- Add new-collection vs existing-collection context to the
  _backfill_doc_id_to_string docstring's `dimension` parameter.
- Replace the misleading "rewrote 0/N from int to str" wording when
  no rewriting was needed with "N points scanned, none required
  rewriting (collection already in str form)".
- Add test_ensure_keyword_payload_indexes_logs_and_returns_when_
  get_collection_raises mirroring the scroll-failure test.

SonarCloud (1 CRITICAL + 1 MINOR):

- Refactor _backfill_doc_id_to_string to bring cognitive complexity
  under 15 (was 19). Extracted two pure helpers: _group_int_doc_ids
  (group point IDs by stringified doc_id) and _apply_backfill_writes
  (apply set_payload calls and return rewritten count). The main
  function's scroll/loop/sentinel structure is unchanged.
- Add `await asyncio.sleep(0)` to the three async test side_effect
  helpers (_scroll_raises, _upsert_raises, _create_index) so they use
  an actual async feature (S7503). The async-callable shape is still
  required to avoid the AsyncMock unawaited-coroutine warning when
  side_effect raises.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-05-09 00:30:29 +02:00
co-authored by Claude Opus 4.7
parent c27556c332
commit 60a9882c92
3 changed files with 150 additions and 42 deletions
+4 -4
View File
@@ -230,7 +230,7 @@ async def scan_user_documents(
indexed_doc_ids = {
str(point.payload["doc_id"])
for point in (scroll_result[0] or [])
if point.payload is not None
if point.payload is not None and "doc_id" in point.payload
}
logger.debug(f"Found {len(indexed_doc_ids)} indexed documents in Qdrant")
@@ -403,7 +403,7 @@ async def scan_user_documents(
indexed_file_ids = {
str(point.payload["doc_id"])
for point in (file_scroll_result[0] or [])
if point.payload is not None
if point.payload is not None and "doc_id" in point.payload
}
logger.debug(f"Found {len(indexed_file_ids)} indexed files in Qdrant")
@@ -683,7 +683,7 @@ async def scan_news_items(
indexed_item_ids = {
str(point.payload["doc_id"])
for point in (scroll_result[0] or [])
if point.payload is not None
if point.payload is not None and "doc_id" in point.payload
}
logger.debug(f"Found {len(indexed_item_ids)} indexed news items in Qdrant")
@@ -862,7 +862,7 @@ async def scan_deck_cards(
indexed_card_ids = {
str(point.payload["doc_id"])
for point in (scroll_result[0] or [])
if point.payload is not None
if point.payload is not None and "doc_id" in point.payload
}
logger.debug(f"Found {len(indexed_card_ids)} indexed deck cards in Qdrant")