Skip to content

Build and run agents you can see, understand and trust.

fix(rag): close the latest Elasticsearch PIT (#2998)

cxyyy6645511bb4d0bc
diff --git a/src/agentscope/rag/_vdb/_elasticsearch.py b/src/agentscope/rag/_vdb/_elasticsearch.py
index ff2593668..d9fc9b6ee 100644
--- a/src/agentscope/rag/_vdb/_elasticsearch.py
+++ b/src/agentscope/rag/_vdb/_elasticsearch.py
@@ -345,6 +345,7 @@ class ElasticsearchStore(VectorStoreBase):
if search_after is not None:
body["search_after"] = search_after
response = await client.search(**body)
+ pit_id = response.get("pit_id", pit_id)
hits = response["hits"]["hits"]
if not hits:
break
@@ -361,7 +362,6 @@ class ElasticsearchStore(VectorStoreBase):
index,
Chunk.model_validate(payload),
)
- pit_id = response.get("pit_id", pit_id)
search_after = hits[-1]["sort"]
finally:
await client.close_point_in_time(id=pit_id)
diff --git a/tests/rag_vdb_elasticsearch_test.py b/tests/rag_vdb_elasticsearch_test.py
index dee6f5100..883c4b693 100644
--- a/tests/rag_vdb_elasticsearch_test.py
+++ b/tests/rag_vdb_elasticsearch_test.py
@@ -326,6 +326,32 @@ class ElasticsearchStoreTest(IsolatedAsyncioTestCase):
await self.store.list_chunks("kb-1", "doc-1")
self.client.close_point_in_time.assert_awaited_once_with(id="pit-1")
+ async def test_list_chunks_closes_latest_pit_on_empty_page(self) -> None:
+ for rotated in (False, True):
+ with self.subTest(rotated=rotated):
+ self.client.close_point_in_time.reset_mock()
+ self.client.open_point_in_time.return_value = {"id": "pit-1"}
+ response = {"hits": {"hits": []}}
+ if rotated:
+ response["pit_id"] = "pit-2"
+ self.client.search.return_value = response
+ self.assertEqual(await self.store.list_chunks("kb", "doc"), [])
+ self.client.close_point_in_time.assert_awaited_once_with(
+ id="pit-2" if rotated else "pit-1",
+ )
+
+ async def test_list_chunks_closes_latest_pit_on_invalid_chunk(
+ self,
+ ) -> None:
+ self.client.open_point_in_time.return_value = {"id": "pit-1"}
+ self.client.search.return_value = {
+ "pit_id": "pit-2",
+ "hits": {"hits": [{"_source": {"chunk": {"chunk_index": 0}}}]},
+ }
+ with self.assertRaises(ValueError):
+ await self.store.list_chunks("kb", "doc")
+ self.client.close_point_in_time.assert_awaited_once_with(id="pit-2")
+
async def test_list_chunks_zero_limit_short_circuits(self) -> None:
self.assertEqual(
await self.store.list_chunks("kb-1", "doc-1", limit=0),