Skip to content

Agent Zero AI framework

Fix document query index reuse

Alessandro94065dbe8602
diff --git a/plugins/_document_query/helpers/document_query.py b/plugins/_document_query/helpers/document_query.py
index 4a7fa897a..05dd112e4 100644
--- a/plugins/_document_query/helpers/document_query.py
+++ b/plugins/_document_query/helpers/document_query.py
@@ -72,15 +72,30 @@ def _load_config(agent: Agent) -> dict:
class DocumentQueryStore:
"""FAISS Store for document query results."""
+ CONTEXT_DATA_KEY = "_document_query_store"
DEFAULT_CHUNK_SIZE = 1000
DEFAULT_CHUNK_OVERLAP = 100
DEFAULT_MAX_INDEX_CHUNKS = 1200
+ _GET_LOCK = threading.RLock()
- @staticmethod
- def get(agent: Agent):
+ @classmethod
+ def get(cls, agent: Agent):
if not agent or not agent.config:
raise ValueError("Agent and agent config must be provided")
- return DocumentQueryStore(agent)
+
+ context = getattr(agent, "context", None)
+ if context is None:
+ return cls(agent)
+
+ with cls._GET_LOCK:
+ store = context.get_data(cls.CONTEXT_DATA_KEY, recursive=False)
+ if not isinstance(store, cls):
+ store = cls(agent)
+ context.set_data(cls.CONTEXT_DATA_KEY, store, recursive=False)
+ else:
+ store.agent = agent
+ store.config = _load_config(agent)
+ return store
def __init__(self, agent: Agent):
self.agent = agent
diff --git a/tests/test_document_query_plugin.py b/tests/test_document_query_plugin.py
index e3ece0ebd..1009edc45 100644
--- a/tests/test_document_query_plugin.py
+++ b/tests/test_document_query_plugin.py
@@ -1,13 +1,19 @@
from __future__ import annotations
import asyncio
+import sys
from pathlib import Path
import pytest
from PIL import Image
+ROOT = Path(__file__).resolve().parents[1]
+if str(ROOT) not in sys.path:
+ sys.path.insert(0, str(ROOT))
+
from plugins._document_query import hooks as document_query_hooks
from plugins._document_query.helpers.fetch import FetchedDocument, fetch_public_resource
+import plugins._document_query.helpers.document_query as document_query_module
from plugins._document_query.helpers.document_query import (
DocumentQueryHelper,
DocumentQueryStore,
@@ -19,9 +25,6 @@ from plugins._document_query.helpers.parsers.liteparse import LiteParseParser
from plugins._document_query.helpers.parsers.text import TextParser
-ROOT = Path(__file__).resolve().parents[1]
-
-
def run_async(coro):
with asyncio.Runner() as runner:
return runner.run(coro)
@@ -52,6 +55,52 @@ class CountingAsyncParser(BaseParser):
return document.uri
+class _StoreContext:
+ def __init__(self, context_id: str):
+ self.id = context_id
+ self.data = {}
+
+ def get_data(self, key: str, recursive: bool = True):
+ return self.data.get(key)
+
+ def set_data(self, key: str, value, recursive: bool = True):
+ self.data[key] = value
+
+
+class _StoreAgent:
+ def __init__(self, context_id: str):
+ self.config = object()
+ self.context = _StoreContext(context_id)
+
+
+class _FakeVectorDB:
+ def __init__(self):
+ self.docs = []
+
+ async def insert_documents(self, docs):
+ ids = []
+ for doc in docs:
+ doc_id = f"doc-{len(self.docs)}"
+ doc.metadata["id"] = doc_id
+ ids.append(doc_id)
+ self.docs.append(doc)
+ return ids
+
+ async def search_by_metadata(self, filter: str, limit: int = 0):
+ document_uri = filter.split("'", 2)[1]
+ docs = [
+ doc
+ for doc in self.docs
+ if doc.metadata.get("document_uri") == document_uri
+ ]
+ return docs[:limit] if limit > 0 else docs
+
+ async def delete_documents_by_ids(self, ids: list[str]):
+ removed = [doc for doc in self.docs if doc.metadata.get("id") in ids]
+ self.docs = [doc for doc in self.docs if doc.metadata.get("id") not in ids]
+ return removed
+
+
def test_fetch_file_detects_mimetype_and_reads_once(tmp_path):
document = tmp_path / "notes.txt"
document.write_text("hello\nworld\n", encoding="utf-8")
@@ -196,6 +245,41 @@ def test_document_query_allows_uncapped_index_chunks():
assert len(chunks) > 10
+def test_document_query_store_reuses_vector_db_per_context(monkeypatch):
+ monkeypatch.setattr(
+ document_query_module,
+ "_load_config",
+ lambda _agent: {
+ "chunk_size": 100,
+ "chunk_overlap": 10,
+ "max_index_chunks": 20,
+ },
+ )
+ monkeypatch.setattr(
+ DocumentQueryStore,
+ "init_vector_db",
+ lambda _self: _FakeVectorDB(),
+ )
+
+ agent = _StoreAgent("ctx-one")
+ store = DocumentQueryStore.get(agent)
+
+ success, ids = run_async(
+ store.add_document("alpha beta gamma " * 20, "/tmp/book.txt")
+ )
+ second_store = DocumentQueryStore.get(agent)
+
+ assert success is True
+ assert ids
+ assert second_store is store
+ assert second_store.vector_db is store.vector_db
+ assert run_async(second_store.document_exists("/tmp/book.txt")) is True
+
+ isolated_store = DocumentQueryStore.get(_StoreAgent("ctx-two"))
+ assert isolated_store is not store
+ assert run_async(isolated_store.document_exists("/tmp/book.txt")) is False
+
+
def test_document_query_thumbnail_matches_plugin_hub_limits():
thumbnail = ROOT / "plugins" / "_document_query" / "webui" / "thumbnail.jpg"