Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix(vector_store): don't treat an empty weaviate page as end of scan
KaifAhmad16c9eb2296def
diff --git a/semantica/vector_store/weaviate_store.py b/semantica/vector_store/weaviate_store.pyindex 6a2761d6..87ffa19d 100644--- a/semantica/vector_store/weaviate_store.py+++ b/semantica/vector_store/weaviate_store.py@@ -627,7 +627,10 @@ class WeaviateStore:Iterate over every stored object using Weaviate's UUID cursor.Paginates by the last object's UUID rather than a row offset, which is- why this exists instead of scan_vectors(offset, limit).+ why this exists instead of scan_vectors(offset, limit). An empty page+ under that cursor falls back to offset pagination once before ending+ the scan, since an empty page isn't on its own proof there's nothing+ left past it (see the inline comment below).Assumes a single unnamed vector per object, as get_vector() andfilter_by_metadata() already do. Named-vector collections return a@@ -676,6 +679,20 @@ class WeaviateStore:batch_objects = getattr(objs, "objects", None) if objs else Noneif not batch_objects:+ # An empty page in "cursor" mode isn't necessarily the end.+ # Unlike an offset, `after` has no server-issued continuation+ # value of its own - it's derived client-side from the last+ # object's uuid - so an empty page gives nothing to advance+ # it with. If Weaviate's cursor walks internal storage+ # position rather than strict uuid order, a batch can in+ # principle land entirely on a gap (e.g. tombstoned objects)+ # with live data past it, the same risk already confirmed for+ # Qdrant's scroll cursor (#1316). Offset pagination doesn't+ # have that ambiguity - it addresses live rows by position -+ # so fall back to it once to confirm before ending the scan.+ if mode == "cursor":+ mode = "offset"+ continuereturnpage_full = len(batch_objects) >= batch_sizediff --git a/tests/vector_store/test_weaviate_store.py b/tests/vector_store/test_weaviate_store.pyindex 39b67cbd..ccde42a4 100644--- a/tests/vector_store/test_weaviate_store.py+++ b/tests/vector_store/test_weaviate_store.py@@ -78,6 +78,29 @@ def test_iter_all_raises_when_cursor_stops_advancing():list(store.iter_all(batch_size=2))+@patch("semantica.vector_store.weaviate_store.WEAVIATE_AVAILABLE", True)+def test_iter_all_continues_past_empty_page_in_cursor_mode():+ """A full page followed by an empty page must not be read as the end of+ the collection: the empty page could be a gap (e.g. a window landing on+ tombstoned objects) with real data past it, the same failure mode+ already confirmed for Qdrant's scroll cursor (#1316). The `after` cursor+ has no server-issued value to advance past an empty page with, so this+ must fall back to offset pagination rather than silently stopping."""+ store = _store_with_pages(+ _page([_obj("uuid-1"), _obj("uuid-2")]), # full page, cursor -> uuid-2+ _page([]), # empty page: not the end+ _page([_obj("uuid-3")]), # real data past the gap+ )++ result = [item["id"] for item in store.iter_all(batch_size=2)]++ assert result == ["uuid-1", "uuid-2", "uuid-3"]+ calls = store.collection.query.fetch_objects.call_args_list+ assert len(calls) == 3+ assert calls[1][1]["after"] == "uuid-2" # the empty page still queried by cursor+ assert calls[2][1].get("offset") == 2 # then the fallback used position, not the cursor++@patch("semantica.vector_store.weaviate_store.WEAVIATE_AVAILABLE", True)def test_iter_all_offset_fallback_advances_across_pages():"""Regression: the offset was only set inside the except branch, so pages@@ -124,9 +147,15 @@ def test_iter_all_raises_when_no_pagination_is_supported():@patch("semantica.vector_store.weaviate_store.WEAVIATE_AVAILABLE", True)def test_iter_all_empty_collection_yields_nothing():- store = _store_with_pages(_page([]))+ """A genuinely empty collection needs two empty pages to confirm: the+ first (in cursor mode) triggers the offset fallback, and the second+ (in offset mode, which has no gap ambiguity) is what actually ends the+ scan. See test_iter_all_continues_past_empty_page_in_cursor_mode for the+ case where the first empty page is *not* the end."""+ store = _store_with_pages(_page([]), _page([]))assert list(store.iter_all()) == []+ assert store.collection.query.fetch_objects.call_count == 2@patch("semantica.vector_store.weaviate_store.WEAVIATE_AVAILABLE", True)@@ -162,7 +191,7 @@ def test_iter_all_treats_empty_vector_as_none():@patch("semantica.vector_store.weaviate_store.WEAVIATE_AVAILABLE", True)def test_iter_all_requests_vectors():"""Weaviate omits vectors unless include_vector is set."""- store = _store_with_pages(_page([]))+ store = _store_with_pages(_page([]), _page([]))list(store.iter_all(batch_size=64))