Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix: convert Session.load() and get_context_for_search() to async to prevent deadlock (#235)

Sense_wang390e088b0456
diff --git a/openviking/client/local.py b/openviking/client/local.py
index 749069e62..208d7312b 100644
--- a/openviking/client/local.py
+++ b/openviking/client/local.py
@@ -188,7 +188,7 @@ class LocalClient(BaseClient):
session = None
if session_id:
session = self._service.sessions.session(session_id)
- session.load()
+ await session.load()
return await self._service.search.search(
query=query,
target_uri=target_uri,
@@ -237,7 +237,7 @@ class LocalClient(BaseClient):
async def get_session(self, session_id: str) -> Dict[str, Any]:
"""Get session details."""
session = self._service.sessions.session(session_id)
- session.load()
+ await session.load()
return {
"session_id": session.session_id,
"user": session.user.to_dict(),
@@ -255,7 +255,7 @@ class LocalClient(BaseClient):
async def add_message(self, session_id: str, role: str, content: str) -> Dict[str, Any]:
"""Add a message to a session."""
session = self._service.sessions.session(session_id)
- session.load()
+ await session.load()
session.add_message(role, content)
return {
"session_id": session_id,
diff --git a/openviking/server/routers/search.py b/openviking/server/routers/search.py
index 02e20029f..06387d641 100644
--- a/openviking/server/routers/search.py
+++ b/openviking/server/routers/search.py
@@ -82,7 +82,7 @@ async def search(
session = None
if request.session_id:
session = service.sessions.session(request.session_id)
- session.load()
+ await session.load()
result = await service.search.search(
query=request.query,
diff --git a/openviking/server/routers/sessions.py b/openviking/server/routers/sessions.py
index b36b76574..1daa9e9b9 100644
--- a/openviking/server/routers/sessions.py
+++ b/openviking/server/routers/sessions.py
@@ -67,7 +67,7 @@ async def get_session(
"""Get session details."""
service = get_service()
session = service.sessions.session(session_id)
- session.load()
+ await session.load()
return Response(
status="ok",
result={
@@ -120,7 +120,7 @@ async def add_message(
"""Add a message to a session."""
service = get_service()
session = service.sessions.session(session_id)
- session.load()
+ await session.load()
session.add_message(request.role, [TextPart(text=request.content)])
return Response(
status="ok",
diff --git a/openviking/service/search_service.py b/openviking/service/search_service.py
index fd6b47a37..0b88746ee 100644
--- a/openviking/service/search_service.py
+++ b/openviking/service/search_service.py
@@ -60,7 +60,7 @@ class SearchService:
session_info = None
if session:
- session_info = session.get_context_for_search(query)
+ session_info = await session.get_context_for_search(query)
return await viking_fs.search(
query=query,
diff --git a/openviking/service/session_service.py b/openviking/service/session_service.py
index 209385040..fb9de26c6 100644
--- a/openviking/service/session_service.py
+++ b/openviking/service/session_service.py
@@ -128,7 +128,7 @@ class SessionService:
"""
self._ensure_initialized()
session = self.session(session_id)
- session.load()
+ await session.load()
return session.commit()
async def extract(self, session_id: str) -> List[Any]:
@@ -145,7 +145,7 @@ class SessionService:
raise NotInitializedError("SessionCompressor")
session = self.session(session_id)
- session.load()
+ await session.load()
return await self._session_compressor.extract_long_term_memories(
messages=session.messages,
diff --git a/openviking/session/session.py b/openviking/session/session.py
index 30f9490df..401f3ea44 100644
--- a/openviking/session/session.py
+++ b/openviking/session/session.py
@@ -90,13 +90,13 @@ class Session:
logger.info(f"Session created: {self.session_id} for user {self.user}")
- def load(self):
+ async def load(self):
"""Load session data from storage."""
if self._loaded:
return
try:
- content = run_async(self._viking_fs.read_file(f"{self._session_uri}/messages.jsonl"))
+ content = await self._viking_fs.read_file(f"{self._session_uri}/messages.jsonl")
self._messages = [
Message.from_dict(json.loads(line))
for line in content.strip().split("\n")
@@ -108,7 +108,7 @@ class Session:
# Restore compression_index (scan history directory)
try:
- history_items = run_async(self._viking_fs.ls(f"{self._session_uri}/history"))
+ history_items = await self._viking_fs.ls(f"{self._session_uri}/history")
archives = [
item["name"] for item in history_items if item["name"].startswith("archive_")
]
@@ -298,7 +298,7 @@ class Session:
logger.info(f"Updated active_count for {updated} contexts/skills")
return updated
- def get_context_for_search(
+ async def get_context_for_search(
self, query: str, max_archives: int = 3, max_messages: int = 20
) -> Dict[str, Any]:
"""Get session context for intent analysis.
@@ -319,7 +319,7 @@ class Session:
summaries = []
if self.compression.compression_index > 0:
try:
- history_items = run_async(self._viking_fs.ls(f"{self._session_uri}/history"))
+ history_items = await self._viking_fs.ls(f"{self._session_uri}/history")
query_lower = query.lower()
# Collect all archives with relevance scores
@@ -329,7 +329,7 @@ class Session:
if name and name.startswith("archive_"):
overview_uri = f"{self._session_uri}/history/{name}/.overview.md"
try:
- overview = run_async(self._viking_fs.read_file(overview_uri))
+ overview = await self._viking_fs.read_file(overview_uri)
# Calculate relevance by keyword matching
score = 0
if query_lower in overview.lower():
diff --git a/tests/integration/test_full_workflow.py b/tests/integration/test_full_workflow.py
index ab4aa7366..daf8c77ca 100644
--- a/tests/integration/test_full_workflow.py
+++ b/tests/integration/test_full_workflow.py
@@ -128,7 +128,7 @@ class TestSessionWorkflow:
# 2. Reload session
session2 = client.session(session_id=session_id)
- session2.load()
+ await session2.load()
# 3. Continue conversation
session2.add_message("user", [TextPart("Second message")])
diff --git a/tests/server/test_api_sessions.py b/tests/server/test_api_sessions.py
index ed97639f3..78b66f62f 100644
--- a/tests/server/test_api_sessions.py
+++ b/tests/server/test_api_sessions.py
@@ -115,7 +115,7 @@ async def test_add_message_persistence_regression(
# Verify stored message content survives load/decode.
session = service.sessions.session(session_id)
- session.load()
+ await session.load()
assert len(session.messages) == 2
assert session.messages[0].content == "Message A"
assert session.messages[1].content == "Message B"
diff --git a/tests/session/test_session_context.py b/tests/session/test_session_context.py
index ceccf3698..593326380 100644
--- a/tests/session/test_session_context.py
+++ b/tests/session/test_session_context.py
@@ -13,14 +13,14 @@ class TestGetContextForSearch:
async def test_get_context_basic(self, session_with_messages: Session):
"""Test basic context retrieval"""
- context = session_with_messages.get_context_for_search(query="testing help")
+ context = await session_with_messages.get_context_for_search(query="testing help")
assert isinstance(context, dict)
assert "summaries" in context or "recent_messages" in context
async def test_get_context_with_max_messages(self, session_with_messages: Session):
"""Test limiting max messages"""
- context = session_with_messages.get_context_for_search(query="test", max_messages=2)
+ context = await session_with_messages.get_context_for_search(query="test", max_messages=2)
assert isinstance(context, dict)
if "recent_messages" in context:
@@ -38,13 +38,13 @@ class TestGetContextForSearch:
# Add more messages
session.add_message("user", [TextPart("Second message")])
- context = session.get_context_for_search(query="test", max_archives=1)
+ context = await session.get_context_for_search(query="test", max_archives=1)
assert isinstance(context, dict)
async def test_get_context_empty_session(self, session: Session):
"""Test getting context from empty session"""
- context = session.get_context_for_search(query="test")
+ context = await session.get_context_for_search(query="test")
assert isinstance(context, dict)
@@ -63,6 +63,6 @@ class TestGetContextForSearch:
session.add_message("user", [TextPart("New message after commit")])
# Getting context should include archive summary
- context = session.get_context_for_search(query="test")
+ context = await session.get_context_for_search(query="test")
assert isinstance(context, dict)
diff --git a/tests/session/test_session_lifecycle.py b/tests/session/test_session_lifecycle.py
index a48c5d127..12afea944 100644
--- a/tests/session/test_session_lifecycle.py
+++ b/tests/session/test_session_lifecycle.py
@@ -52,7 +52,7 @@ class TestSessionLoad:
# Create new session instance and load
new_session = client.session(session_id=session_id)
- new_session.load()
+ await new_await session.load()
# Verify messages loaded
assert len(new_session.messages) > 0
@@ -60,7 +60,7 @@ class TestSessionLoad:
async def test_load_nonexistent_session(self, client: AsyncOpenViking):
"""Test loading nonexistent session"""
session = client.session(session_id="nonexistent_session_xyz")
- session.load()
+ await session.load()
# Nonexistent session should be empty after loading
assert len(session.messages) == 0