fix: six P2 correctness/robustness cleanups from the SDK review

- legacy call_llm: open AsyncOpenAI via `async with` so the client (and
  its HTTP connection pool) is closed instead of leaked.
- LocalBackend.add_document: fail fast with CollectionNotFoundError when
  the collection doesn't exist, before the expensive parse + LLM index
  (previously the missing FK only tripped at save time, after paying for
  the LLM work). Also raise builtin FileNotFoundError for a missing path
  instead of FileTypeError (which now means only "unsupported extension").
- Collection.query(doc_ids=None): the empty-collection guard now always
  runs — previously it was skipped once PAGEINDEX_EXPERIMENTAL_MULTIDOC
  was set. A single list_documents call serves both the guard and the
  multi-doc warning (no separate call just to decide whether to warn).
- CloudBackend.query_stream: on early consumer break / GeneratorExit,
  signal the background SSE thread to stop and force-close the response,
  so it no longer drains the whole stream in the background.

Adds regression tests for each (client closed, fail-fast on unknown
collection, FileNotFoundError, empty-check under the multidoc env flag,
single list call, early-break thread stop).

Claude-Session: https://claude.ai/code/session_01Kx5DgKbhK1N8autqXH8SmS
This commit is contained in:
mountain 2026-07-07 11:34:13 +08:00
parent 6c948a332b
commit fe36e25773
8 changed files with 128 additions and 19 deletions

View file

@ -94,3 +94,26 @@ def test_query_accepts_str_doc_id(col):
def test_query_rejects_empty_list(col):
with pytest.raises(ValueError, match="cannot be empty"):
col.query("what?", doc_ids=[])
def test_empty_collection_check_runs_even_when_multidoc_acked(monkeypatch):
from unittest.mock import MagicMock
from pageindex.collection import Collection
monkeypatch.setenv("PAGEINDEX_EXPERIMENTAL_MULTIDOC", "1")
backend = MagicMock()
backend.list_documents.return_value = []
col = Collection(name="papers", backend=backend)
with pytest.raises(ValueError, match="empty"):
col.query("q") # doc_ids=None, collection empty -> must still raise
def test_whole_collection_query_lists_documents_once(monkeypatch):
from unittest.mock import MagicMock
from pageindex.collection import Collection
monkeypatch.setenv("PAGEINDEX_EXPERIMENTAL_MULTIDOC", "1") # silence warning path
backend = MagicMock()
backend.list_documents.return_value = [{"doc_id": "d1"}, {"doc_id": "d2"}]
backend.query.return_value = "ans"
col = Collection(name="papers", backend=backend)
col.query("q")
assert backend.list_documents.call_count == 1 # single call at the collection layer