Refine memory system user-key flow and search output
This commit is contained in:
@ -30,7 +30,25 @@ class FakeClient:
|
||||
if path == "/memory-system/search":
|
||||
return {
|
||||
"status": "success",
|
||||
"items": [{"source": "openviking", "content": "likes latte", "score": 0.9}],
|
||||
"items": [
|
||||
{
|
||||
"source_backend": "openviking",
|
||||
"content": "likes latte",
|
||||
"score": 0.9,
|
||||
"uri": "viking://user/user-1/memories/a",
|
||||
"vector": [0.1, 0.2],
|
||||
},
|
||||
{
|
||||
"source_backend": "everos",
|
||||
"memory": "prefers warm coffee",
|
||||
"memory_type": "profile",
|
||||
"original_data": {"large": "payload"},
|
||||
},
|
||||
],
|
||||
"backends": {
|
||||
"openviking": {"status": "success", "result": {"verbose": True}},
|
||||
"everos": {"status": "success", "result": {"verbose": True}},
|
||||
},
|
||||
}
|
||||
if path.endswith("/commit"):
|
||||
return {"status": self.commit_status}
|
||||
@ -48,6 +66,8 @@ def make_provider():
|
||||
provider._endpoint = "http://127.0.0.1:1934"
|
||||
provider._user_id = "user-1"
|
||||
provider._session_id = "session-1"
|
||||
provider._commit_every_turns = 0
|
||||
provider._commit_interval_seconds = 0
|
||||
return provider
|
||||
|
||||
|
||||
@ -77,6 +97,7 @@ def test_initialize_loads_config_from_hermes_env_file(tmp_path, monkeypatch):
|
||||
for key in list(os.environ):
|
||||
if key.startswith("MEMORY_SYSTEM_"):
|
||||
monkeypatch.delenv(key, raising=False)
|
||||
monkeypatch.chdir(tmp_path)
|
||||
|
||||
class InitClient(FakeClient):
|
||||
def __init__(self, endpoint, api_key="", timeout=0):
|
||||
@ -183,7 +204,22 @@ def test_search_tool_uses_memory_system_api():
|
||||
result = json.loads(provider.handle_tool_call("memory_system_search", {"query": "coffee", "limit": 3}))
|
||||
|
||||
assert result["status"] == "success"
|
||||
assert result["items"][0]["content"] == "likes latte"
|
||||
assert result["items"] == [
|
||||
{
|
||||
"source_backend": "openviking",
|
||||
"score": 0.9,
|
||||
"text": "likes latte",
|
||||
"uri": "viking://user/user-1/memories/a",
|
||||
},
|
||||
{
|
||||
"source_backend": "everos",
|
||||
"memory_type": "profile",
|
||||
"text": "prefers warm coffee",
|
||||
},
|
||||
]
|
||||
assert "backends" not in result
|
||||
assert "vector" not in json.dumps(result)
|
||||
assert "original_data" not in json.dumps(result)
|
||||
assert provider._client.posts[-1] == (
|
||||
"/memory-system/search",
|
||||
{
|
||||
|
||||
@ -5,13 +5,34 @@ from memory_system_api.clients import EverOSMemorySystemClient, OpenVikingMemory
|
||||
|
||||
class FakeStore:
|
||||
def __init__(self):
|
||||
self.saved = {}
|
||||
self.users = {}
|
||||
self.accounts = {}
|
||||
self.sessions = []
|
||||
self.tasks = []
|
||||
|
||||
def get_user_key(self, user_id: str) -> str | None:
|
||||
return self.saved.get(user_id)
|
||||
return self.users.get(user_id)
|
||||
|
||||
def save_user_key(self, user_id: str, user_key: str) -> None:
|
||||
self.saved[user_id] = user_key
|
||||
self.users[user_id] = user_key
|
||||
|
||||
def get_account_key(self, account_id: str) -> str | None:
|
||||
return self.accounts.get(account_id)
|
||||
|
||||
def save_account_key(self, account_id: str, admin_user_id: str, account_key: str) -> None:
|
||||
self.accounts[account_id] = account_key
|
||||
|
||||
def account_key_matches(self, account_id: str, account_key: str) -> bool:
|
||||
return self.accounts.get(account_id) == account_key
|
||||
|
||||
def user_key_matches(self, user_id: str, user_key: str) -> bool:
|
||||
return self.users.get(user_id) == user_key
|
||||
|
||||
def save_session(self, user_id: str, session_id: str) -> None:
|
||||
self.sessions.append((user_id, session_id))
|
||||
|
||||
def save_task(self, user_id: str, session_id: str, task_id: str, archive_uri: str | None) -> None:
|
||||
self.tasks.append((user_id, session_id, task_id, archive_uri))
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
@ -44,13 +65,52 @@ class FakeAsyncClient:
|
||||
self.calls.append(("post", self.api_key, self.headers, path, json))
|
||||
return self.responses.pop(0)
|
||||
|
||||
async def get(self, path: str) -> FakeResponse:
|
||||
self.calls.append(("get", self.api_key, self.headers, path, None))
|
||||
return self.responses.pop(0)
|
||||
|
||||
def test_openviking_uses_root_identity_when_account_already_exists():
|
||||
|
||||
def test_openviking_rejects_unknown_user_credentials():
|
||||
store = FakeStore()
|
||||
client = OpenVikingMemorySystemClient(store=store)
|
||||
|
||||
try:
|
||||
client.credential_for_user("tom", "missing-key")
|
||||
except PermissionError as exc:
|
||||
assert "Invalid user key" in str(exc)
|
||||
else:
|
||||
raise AssertionError("expected PermissionError")
|
||||
|
||||
|
||||
def test_openviking_accepts_matching_user_credentials():
|
||||
store = FakeStore()
|
||||
store.save_user_key("tom", "tom-key")
|
||||
client = OpenVikingMemorySystemClient(store=store)
|
||||
client.root_key = "root-key"
|
||||
|
||||
credential = client.credential_for_user("tom", "tom-key", agent_id="sess-1")
|
||||
|
||||
assert credential.api_key == "tom-key"
|
||||
assert credential.account_id == "admin"
|
||||
assert credential.user_id == "tom"
|
||||
assert credential.agent_id == "sess-1"
|
||||
|
||||
|
||||
def test_openviking_create_user_initializes_admin_workspace_first():
|
||||
store = FakeStore()
|
||||
client = OpenVikingMemorySystemClient(store=store)
|
||||
client.root_key = "root-key"
|
||||
calls = []
|
||||
responses = [FakeResponse(409, {"status": "error", "error": {"code": "CONFLICT"}})]
|
||||
responses = [
|
||||
FakeResponse(
|
||||
200,
|
||||
{"status": "ok", "result": {"account_id": "admin", "admin_user_id": "admin", "user_key": "admin-key"}},
|
||||
),
|
||||
FakeResponse(
|
||||
200,
|
||||
{"status": "ok", "result": {"account_id": "admin", "user_id": "userA", "user_key": "userA-key"}},
|
||||
),
|
||||
]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
@ -58,15 +118,64 @@ def test_openviking_uses_root_identity_when_account_already_exists():
|
||||
extra_headers or {},
|
||||
)
|
||||
|
||||
credential = asyncio.run(client.ensure_user("tom"))
|
||||
result = asyncio.run(client.create_user("userA"))
|
||||
|
||||
assert credential.api_key == "root-key"
|
||||
assert credential.account_id == "tom"
|
||||
assert credential.user_id == "tom"
|
||||
assert store.saved == {}
|
||||
assert result == {"status": "ok", "result": {"account_id": "admin", "user_id": "userA", "user_key": "userA-key"}}
|
||||
assert store.accounts == {"admin": "admin-key"}
|
||||
assert store.users == {"admin": "admin-key", "userA": "userA-key"}
|
||||
assert calls == [
|
||||
(
|
||||
"post",
|
||||
"root-key",
|
||||
{},
|
||||
"/api/v1/admin/accounts",
|
||||
{"account_id": "admin", "admin_user_id": "admin"},
|
||||
),
|
||||
(
|
||||
"post",
|
||||
"root-key",
|
||||
{},
|
||||
"/api/v1/admin/accounts/admin/users",
|
||||
{"user_id": "userA", "role": "user"},
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def test_openviking_root_identity_headers_are_sent_for_session_create():
|
||||
def test_openviking_create_user_reuses_existing_admin_workspace():
|
||||
store = FakeStore()
|
||||
store.save_account_key("admin", "admin", "admin-key")
|
||||
client = OpenVikingMemorySystemClient(store=store)
|
||||
client.root_key = "root-key"
|
||||
calls = []
|
||||
responses = [
|
||||
FakeResponse(
|
||||
200,
|
||||
{"status": "ok", "result": {"account_id": "admin", "user_id": "userB", "user_key": "userB-key"}},
|
||||
)
|
||||
]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
extra_headers or {},
|
||||
)
|
||||
|
||||
result = asyncio.run(client.create_user("userB"))
|
||||
|
||||
assert result == {"status": "ok", "result": {"account_id": "admin", "user_id": "userB", "user_key": "userB-key"}}
|
||||
assert store.users == {"userB": "userB-key"}
|
||||
assert calls == [
|
||||
(
|
||||
"post",
|
||||
"root-key",
|
||||
{},
|
||||
"/api/v1/admin/accounts/admin/users",
|
||||
{"user_id": "userB", "role": "user"},
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def test_openviking_user_key_auth_is_used_for_session_create():
|
||||
client = OpenVikingMemorySystemClient(store=FakeStore())
|
||||
client.root_key = "root-key"
|
||||
calls = []
|
||||
@ -77,22 +186,121 @@ def test_openviking_root_identity_headers_are_sent_for_session_create():
|
||||
api_key,
|
||||
extra_headers or {},
|
||||
)
|
||||
credential = client.root_credential("tom")
|
||||
credential = client.user_credential("tom-key", "tom", agent_id="sess-2")
|
||||
|
||||
result = asyncio.run(client.ensure_session(credential, "sess-2"))
|
||||
|
||||
assert result == {"status": "ok", "result": {"session_id": "sess-2"}}
|
||||
assert client.store.sessions == [("tom", "sess-2")]
|
||||
assert calls == [
|
||||
(
|
||||
"post",
|
||||
"root-key",
|
||||
{"X-OpenViking-Account": "tom", "X-OpenViking-User": "tom"},
|
||||
"tom-key",
|
||||
{},
|
||||
"/api/v1/sessions",
|
||||
{"session_id": "sess-2"},
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def test_openviking_find_uses_current_identity_memory_scope():
|
||||
client = OpenVikingMemorySystemClient(store=FakeStore())
|
||||
calls = []
|
||||
responses = [FakeResponse(200, {"status": "ok", "result": {"memories": []}})]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
extra_headers or {},
|
||||
)
|
||||
credential = client.user_credential("tom-key", "tom", agent_id="sess-1")
|
||||
|
||||
result = asyncio.run(client.find(credential, "咖啡", 5))
|
||||
|
||||
assert result == {"status": "ok", "result": {"memories": []}}
|
||||
assert calls == [
|
||||
(
|
||||
"post",
|
||||
"tom-key",
|
||||
{},
|
||||
"/api/v1/search/find",
|
||||
{"query": "咖啡", "target_uri": "viking://user/tom/memories/", "limit": 5},
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def test_openviking_search_uses_session_target_uri():
|
||||
client = OpenVikingMemorySystemClient(store=FakeStore())
|
||||
calls = []
|
||||
responses = [FakeResponse(200, {"status": "ok", "result": {"memories": []}})]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
extra_headers or {},
|
||||
)
|
||||
credential = client.user_credential("tom-key", "tom", agent_id="sess-1")
|
||||
|
||||
result = asyncio.run(client.search(credential, "sess-1", "咖啡", 5))
|
||||
|
||||
assert result == {"status": "ok", "result": {"memories": []}}
|
||||
assert calls == [
|
||||
(
|
||||
"post",
|
||||
"tom-key",
|
||||
{},
|
||||
"/api/v1/search/search",
|
||||
{"query": "咖啡", "limit": 5, "session_id": "sess-1", "target_uri": "viking://user/tom/sess-1"},
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def test_openviking_commit_keeps_no_recent_live_messages():
|
||||
client = OpenVikingMemorySystemClient(store=FakeStore())
|
||||
calls = []
|
||||
responses = [
|
||||
FakeResponse(
|
||||
200,
|
||||
{
|
||||
"status": "ok",
|
||||
"result": {
|
||||
"status": "accepted",
|
||||
"task_id": "task-1",
|
||||
"archive_uri": "viking://session/tom/sess-1/history/archive_001",
|
||||
},
|
||||
},
|
||||
)
|
||||
]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
extra_headers or {},
|
||||
)
|
||||
credential = client.user_credential("tom-key", "tom")
|
||||
|
||||
result = asyncio.run(client.commit_session(credential, "sess-1"))
|
||||
|
||||
assert result == {
|
||||
"status": "ok",
|
||||
"result": {
|
||||
"status": "accepted",
|
||||
"task_id": "task-1",
|
||||
"archive_uri": "viking://session/tom/sess-1/history/archive_001",
|
||||
},
|
||||
}
|
||||
assert client.store.tasks == [("tom", "sess-1", "task-1", "viking://session/tom/sess-1/history/archive_001")]
|
||||
assert calls == [
|
||||
(
|
||||
"post",
|
||||
"tom-key",
|
||||
{},
|
||||
"/api/v1/sessions/sess-1/commit",
|
||||
{"keep_recent_count": 0},
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def test_everos_assistant_payload_does_not_use_user_id_as_sender():
|
||||
client = EverOSMemorySystemClient()
|
||||
|
||||
|
||||
@ -2,6 +2,15 @@ def test_memory_system_server_exposes_routes():
|
||||
from memory_system_api.server import app
|
||||
|
||||
paths = {route.path for route in app.routes}
|
||||
assert "/memory-system/users" in paths
|
||||
assert "/memory-system/messages" in paths
|
||||
assert "/memory-system/search" in paths
|
||||
assert "/memory-system/users/{user_id}/profile" in paths
|
||||
|
||||
|
||||
def test_memory_system_messages_does_not_require_account_key_header():
|
||||
from memory_system_api.server import app
|
||||
|
||||
route = next(route for route in app.routes if getattr(route, "path", "") == "/memory-system/messages")
|
||||
|
||||
assert all(getattr(dependency.call, "__name__", "") != "account_key_header" for dependency in route.dependant.dependencies)
|
||||
|
||||
@ -9,8 +9,19 @@ class FakeOpenViking:
|
||||
self.fail_on_append = fail_on_append
|
||||
self.calls = []
|
||||
|
||||
async def ensure_user(self, user_id: str) -> str:
|
||||
self.calls.append(("ensure_user", user_id))
|
||||
async def create_user(self, user_id: str) -> dict:
|
||||
self.calls.append(("create_user", user_id))
|
||||
return {"account_id": "admin", "user_id": user_id, "user_key": f"{user_id}-key"}
|
||||
|
||||
def credential_for_user(
|
||||
self,
|
||||
user_id: str,
|
||||
user_key: str,
|
||||
agent_id: str | None = None,
|
||||
) -> str:
|
||||
self.calls.append(("credential_for_user", user_id, user_key, agent_id))
|
||||
if user_key != f"{user_id}-key":
|
||||
raise PermissionError("Invalid user key")
|
||||
return f"key-{user_id}"
|
||||
|
||||
async def ensure_session(self, user_key: str, session_id: str) -> dict:
|
||||
@ -23,8 +34,8 @@ class FakeOpenViking:
|
||||
raise RuntimeError("openviking append failed")
|
||||
return {"message_count": len([call for call in self.calls if call[0] == "append_message"])}
|
||||
|
||||
async def find(self, user_key: str, user_id: str, query: str, limit: int) -> dict:
|
||||
self.calls.append(("find", user_key, user_id, query, limit))
|
||||
async def find(self, user_key: str, query: str, limit: int) -> dict:
|
||||
self.calls.append(("find", user_key, query, limit))
|
||||
await asyncio.sleep(0.01)
|
||||
return {"items": [{"source": "openviking-find"}]}
|
||||
|
||||
@ -33,6 +44,10 @@ class FakeOpenViking:
|
||||
await asyncio.sleep(0.01)
|
||||
return {"items": [{"source": "openviking-search"}]}
|
||||
|
||||
async def commit_session(self, user_key: str, session_id: str) -> dict:
|
||||
self.calls.append(("commit_session", user_key, session_id))
|
||||
return {"status": "ok", "result": {"task_id": "task-1", "archive_uri": "archive-1"}}
|
||||
|
||||
|
||||
class FakeEverOS:
|
||||
def __init__(self, fail_on_append: bool = False):
|
||||
@ -50,6 +65,10 @@ class FakeEverOS:
|
||||
await asyncio.sleep(0.01)
|
||||
return {"items": [{"source": f"everos-{method}"}]}
|
||||
|
||||
async def flush(self, user_id: str, session_id: str) -> dict:
|
||||
self.calls.append(("flush", user_id, session_id))
|
||||
return {"status": "flushed"}
|
||||
|
||||
|
||||
class FakeEverOSWithVector(FakeEverOS):
|
||||
async def search(self, user_id: str, session_id: str | None, query: str, method: str, limit: int) -> dict:
|
||||
@ -70,6 +89,47 @@ class FakeEverOSWithVector(FakeEverOS):
|
||||
}
|
||||
|
||||
|
||||
class FakeEverOSVerbose(FakeEverOS):
|
||||
async def search(self, user_id: str, session_id: str | None, query: str, method: str, limit: int) -> dict:
|
||||
self.calls.append(("search", user_id, session_id, query, method, limit))
|
||||
return {
|
||||
"data": {
|
||||
"episodes": [
|
||||
{
|
||||
"id": "episode-1",
|
||||
"user_id": user_id,
|
||||
"session_id": session_id,
|
||||
"timestamp": "2026-05-22T07:50:51.750000Z",
|
||||
"summary": "userB 在对话中表示自己喜欢拿铁。",
|
||||
"subject": "UserB 表达对拿铁的喜好",
|
||||
"episode": "userB 在对话中表示自己喜欢拿铁。",
|
||||
"type": "Conversation",
|
||||
"parent_id": "parent-1",
|
||||
"score": 0.72,
|
||||
"atomic_facts": [],
|
||||
}
|
||||
],
|
||||
"profiles": [],
|
||||
"raw_messages": [],
|
||||
"query": {
|
||||
"text": query,
|
||||
"method": method,
|
||||
"filters_applied": {"user_id": user_id, "session_id": session_id},
|
||||
},
|
||||
"original_data": {
|
||||
"episodes": {
|
||||
"episode-1": {
|
||||
"id": "episode-1",
|
||||
"summary": "userB 在对话中表示自己喜欢拿铁。",
|
||||
"episode": "userB 在对话中表示自己喜欢拿铁。",
|
||||
"vector_model": "Qwen3-VL-Embedding-2B",
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def test_capture_includes_exception_type_when_message_is_empty():
|
||||
service = MemorySystemService(openviking=FakeOpenViking(), everos=FakeEverOS())
|
||||
|
||||
@ -85,20 +145,64 @@ def test_capture_includes_exception_type_when_message_is_empty():
|
||||
assert response.error == "EmptyError"
|
||||
|
||||
|
||||
def test_create_user_delegates_to_openviking_only():
|
||||
openviking = FakeOpenViking()
|
||||
everos = FakeEverOS()
|
||||
service = MemorySystemService(openviking=openviking, everos=everos)
|
||||
|
||||
response = asyncio.run(service.create_user("alice"))
|
||||
|
||||
assert response.status == "success"
|
||||
assert response.account == {"account_id": "admin", "user_id": "alice", "user_key": "alice-key"}
|
||||
assert openviking.calls == [("create_user", "alice")]
|
||||
assert everos.calls == []
|
||||
|
||||
|
||||
def test_search_removes_vectors_from_items_and_backend_results():
|
||||
service = MemorySystemService(openviking=FakeOpenViking(), everos=FakeEverOSWithVector())
|
||||
|
||||
response = asyncio.run(service.search(
|
||||
SearchRequest(user_id="tom", session_id="sess-1", query="咖啡偏好", use_llm=False, limit=5)
|
||||
SearchRequest(user_id="tom", user_key="tom-key", session_id="sess-1", query="咖啡偏好", use_llm=False, limit=5),
|
||||
))
|
||||
|
||||
assert response.items == [
|
||||
{"source_backend": "openviking", "source": "openviking-find"},
|
||||
{"source_backend": "everos", "id": "episode-1"},
|
||||
{"source_backend": "everos", "memory_type": "episode", "id": "episode-1"},
|
||||
]
|
||||
assert not _has_key(response.backends["everos"].result, "vector")
|
||||
|
||||
|
||||
def test_search_returns_compact_items_and_backend_diagnostics_without_duplicate_raw_payloads():
|
||||
service = MemorySystemService(openviking=FakeOpenViking(), everos=FakeEverOSVerbose())
|
||||
|
||||
response = asyncio.run(service.search(
|
||||
SearchRequest(user_id="tom", user_key="tom-key", session_id="sess-1", query="我喜欢喝什么?", use_llm=True),
|
||||
))
|
||||
|
||||
assert response.items == [
|
||||
{"source_backend": "openviking", "source": "openviking-search"},
|
||||
{
|
||||
"source_backend": "everos",
|
||||
"memory_type": "episode",
|
||||
"id": "episode-1",
|
||||
"user_id": "tom",
|
||||
"session_id": "sess-1",
|
||||
"timestamp": "2026-05-22T07:50:51.750000Z",
|
||||
"summary": "userB 在对话中表示自己喜欢拿铁。",
|
||||
"score": 0.72,
|
||||
},
|
||||
]
|
||||
assert response.backends["everos"].result == {
|
||||
"counts": {"episodes": 1, "profiles": 0, "raw_messages": 0},
|
||||
"query": {
|
||||
"text": "我喜欢喝什么?",
|
||||
"method": "agentic",
|
||||
"filters_applied": {"user_id": "tom", "session_id": "sess-1"},
|
||||
},
|
||||
}
|
||||
assert not _has_key(response.backends["everos"].result, "original_data")
|
||||
|
||||
|
||||
def _has_key(value, key: str) -> bool:
|
||||
if isinstance(value, dict):
|
||||
return key in value or any(_has_key(item, key) for item in value.values())
|
||||
@ -115,6 +219,7 @@ def test_ingest_splits_user_and_assistant_messages():
|
||||
response = asyncio.run(service.ingest_messages(
|
||||
MessageIngestRequest(
|
||||
user_id="tom",
|
||||
user_key="tom-key",
|
||||
session_id="sess-1",
|
||||
user_message="我喜欢拿铁",
|
||||
assistant_message="我记住了",
|
||||
@ -124,7 +229,7 @@ def test_ingest_splits_user_and_assistant_messages():
|
||||
assert response.status == "success"
|
||||
assert response.message_count == 2
|
||||
assert openviking.calls == [
|
||||
("ensure_user", "tom"),
|
||||
("credential_for_user", "tom", "tom-key", "sess-1"),
|
||||
("ensure_session", "key-tom", "sess-1"),
|
||||
("append_message", "key-tom", "sess-1", "user", "我喜欢拿铁"),
|
||||
("append_message", "key-tom", "sess-1", "assistant", "我记住了"),
|
||||
@ -139,7 +244,11 @@ def test_ingest_requires_at_least_one_message():
|
||||
service = MemorySystemService(openviking=FakeOpenViking(), everos=FakeEverOS())
|
||||
|
||||
try:
|
||||
asyncio.run(service.ingest_messages(MessageIngestRequest(user_id="tom", session_id="sess-1")))
|
||||
asyncio.run(
|
||||
service.ingest_messages(
|
||||
MessageIngestRequest(user_id="tom", user_key="tom-key", session_id="sess-1"),
|
||||
)
|
||||
)
|
||||
except ValueError as exc:
|
||||
assert "at least one message" in str(exc)
|
||||
else:
|
||||
@ -150,7 +259,7 @@ def test_ingest_returns_partial_success_when_one_backend_fails():
|
||||
service = MemorySystemService(openviking=FakeOpenViking(fail_on_append=True), everos=FakeEverOS())
|
||||
|
||||
response = asyncio.run(service.ingest_messages(
|
||||
MessageIngestRequest(user_id="tom", session_id="sess-1", user_message="hello")
|
||||
MessageIngestRequest(user_id="tom", user_key="tom-key", session_id="sess-1", user_message="hello"),
|
||||
))
|
||||
|
||||
assert response.status == "partial_success"
|
||||
@ -158,13 +267,28 @@ def test_ingest_returns_partial_success_when_one_backend_fails():
|
||||
assert response.backends["everos"].status == "success"
|
||||
|
||||
|
||||
def test_commit_uses_user_key_without_account_id():
|
||||
openviking = FakeOpenViking()
|
||||
everos = FakeEverOS()
|
||||
service = MemorySystemService(openviking=openviking, everos=everos)
|
||||
|
||||
response = asyncio.run(service.commit_session("tom", "tom-key", "sess-1"))
|
||||
|
||||
assert response.status == "success"
|
||||
assert openviking.calls == [
|
||||
("credential_for_user", "tom", "tom-key", "sess-1"),
|
||||
("commit_session", "key-tom", "sess-1"),
|
||||
]
|
||||
assert everos.calls == [("flush", "tom", "sess-1")]
|
||||
|
||||
|
||||
def test_search_uses_find_and_hybrid_without_llm():
|
||||
openviking = FakeOpenViking()
|
||||
everos = FakeEverOS()
|
||||
service = MemorySystemService(openviking=openviking, everos=everos)
|
||||
|
||||
response = asyncio.run(service.search(
|
||||
SearchRequest(user_id="tom", session_id="sess-1", query="咖啡偏好", use_llm=False, limit=5)
|
||||
SearchRequest(user_id="tom", user_key="tom-key", session_id="sess-1", query="咖啡偏好", use_llm=False, limit=5),
|
||||
))
|
||||
|
||||
assert response.status == "success"
|
||||
@ -172,7 +296,8 @@ def test_search_uses_find_and_hybrid_without_llm():
|
||||
{"source_backend": "openviking", "source": "openviking-find"},
|
||||
{"source_backend": "everos", "source": "everos-hybrid"},
|
||||
]
|
||||
assert ("find", "key-tom", "tom", "咖啡偏好", 5) in openviking.calls
|
||||
assert ("credential_for_user", "tom", "tom-key", "sess-1") in openviking.calls
|
||||
assert ("find", "key-tom", "咖啡偏好", 5) in openviking.calls
|
||||
assert ("search", "tom", "sess-1", "咖啡偏好", "hybrid", 5) in everos.calls
|
||||
|
||||
|
||||
@ -182,7 +307,7 @@ def test_search_uses_search_and_agentic_with_llm():
|
||||
service = MemorySystemService(openviking=openviking, everos=everos)
|
||||
|
||||
response = asyncio.run(service.search(
|
||||
SearchRequest(user_id="tom", session_id="sess-1", query="咖啡偏好", use_llm=True, limit=5)
|
||||
SearchRequest(user_id="tom", user_key="tom-key", session_id="sess-1", query="咖啡偏好", use_llm=True, limit=5),
|
||||
))
|
||||
|
||||
assert response.status == "success"
|
||||
@ -190,5 +315,6 @@ def test_search_uses_search_and_agentic_with_llm():
|
||||
{"source_backend": "openviking", "source": "openviking-search"},
|
||||
{"source_backend": "everos", "source": "everos-agentic"},
|
||||
]
|
||||
assert ("credential_for_user", "tom", "tom-key", "sess-1") in openviking.calls
|
||||
assert ("search", "key-tom", "sess-1", "咖啡偏好", 5) in openviking.calls
|
||||
assert ("search", "tom", "sess-1", "咖啡偏好", "agentic", 5) in everos.calls
|
||||
|
||||
32
tests/test_memory_system_store.py
Normal file
32
tests/test_memory_system_store.py
Normal file
@ -0,0 +1,32 @@
|
||||
from memory_system_api.store import OpenVikingUserKeyStore
|
||||
|
||||
|
||||
def test_store_persists_openviking_user_session_and_task_metadata(tmp_path):
|
||||
db_path = tmp_path / "memory.sqlite3"
|
||||
store = OpenVikingUserKeyStore(str(db_path))
|
||||
|
||||
store.save_user_key("userA", "userA-key")
|
||||
store.save_session("userA", "sessionA1")
|
||||
store.save_task(
|
||||
user_id="userA",
|
||||
session_id="sessionA1",
|
||||
task_id="task-1",
|
||||
archive_uri="viking://session/userA/sessionA1/history/archive_001",
|
||||
)
|
||||
|
||||
reopened = OpenVikingUserKeyStore(str(db_path))
|
||||
|
||||
assert reopened.get_user_key("userA") == "userA-key"
|
||||
assert reopened.user_key_matches("userA", "userA-key")
|
||||
assert reopened.get_session("userA", "sessionA1") == {
|
||||
"user_id": "userA",
|
||||
"session_id": "sessionA1",
|
||||
"latest_task_id": "task-1",
|
||||
"latest_archive_uri": "viking://session/userA/sessionA1/history/archive_001",
|
||||
}
|
||||
assert reopened.get_task("task-1") == {
|
||||
"task_id": "task-1",
|
||||
"user_id": "userA",
|
||||
"session_id": "sessionA1",
|
||||
"archive_uri": "viking://session/userA/sessionA1/history/archive_001",
|
||||
}
|
||||
Reference in New Issue
Block a user