Skip to content

Commit c8786cb

Browse files
fix: await async memory search endpoint (#40)
Use the async store API so saved memories can be read without blocking or tripping the event-loop guard. Add a regression test for the readback path. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 9c9172a commit c8786cb

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

sample_api/async_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ async def get_user_memories(user_id: str):
198198

199199
try:
200200
# Search for all memories for this user
201-
search_results = memory_store.search(namespace)
201+
search_results = await memory_store.asearch(namespace)
202202

203203
for item in search_results:
204204
# Parse the memory data

tests/test_async_api.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Regression tests for the sample async API."""
2+
3+
from types import SimpleNamespace
4+
from unittest.mock import AsyncMock, Mock
5+
6+
import pytest
7+
8+
9+
@pytest.mark.asyncio
10+
async def test_get_user_memories_uses_async_store_search(monkeypatch: pytest.MonkeyPatch) -> None:
11+
"""The async endpoint must not call the store's event-loop-blocking sync API."""
12+
from openchatbi.tool import memory as memory_module
13+
from sample_api.async_api import get_user_memories
14+
15+
item = SimpleNamespace(
16+
key="profile",
17+
value={"text": "prefers bar charts"},
18+
created_at="2026-08-15T00:00:00Z",
19+
updated_at="2026-08-15T00:00:00Z",
20+
)
21+
memory_store = Mock()
22+
memory_store.asearch = AsyncMock(return_value=[item])
23+
memory_store.search.side_effect = AssertionError("synchronous search must not be called")
24+
25+
async def get_store():
26+
return memory_store
27+
28+
monkeypatch.setattr(memory_module, "get_async_memory_store", get_store)
29+
30+
response = await get_user_memories("user-1")
31+
32+
memory_store.asearch.assert_awaited_once_with(("memories", "user-1"))
33+
memory_store.search.assert_not_called()
34+
assert response == {
35+
"user_id": "user-1",
36+
"total_memories": 1,
37+
"memories": [
38+
{
39+
"key": "profile",
40+
"content": {"text": "prefers bar charts"},
41+
"namespace": "('memories', 'user-1')",
42+
"created_at": "2026-08-15T00:00:00Z",
43+
"updated_at": "2026-08-15T00:00:00Z",
44+
}
45+
],
46+
}

0 commit comments

Comments
 (0)