Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
from __future__ import annotations
from typing import Any
import pytest
from openviking.pyagfs.async_client import AsyncAGFSClient
from openviking.pyagfs.helpers import cp
from openviking.storage.errors import InvalidPathLockRequestError
class _RecordingClient:
"""Record ctx values received by the sync AGFS surface."""
def __init__(self) -> None:
self.calls: list[tuple[str, str, dict[str, str] | None]] = []
self.list_options: dict[str, Any] = {}
self.tree_options: dict[str, Any] = {}
def write(self, path: str, data: bytes, *, ctx: dict[str, str] | None = None) -> str:
"""Record write ctx and return a stable fake backend id."""
self.calls.append(("write", path, ctx))
return "written"
def read(self, path: str, *, ctx: dict[str, str] | None = None, **_: Any) -> bytes:
"""Record read ctx and return a stable fake payload."""
self.calls.append(("read", path, ctx))
return b"payload"
def ls(
self,
path: str,
*,
ctx: dict[str, str] | None = None,
**options: Any,
) -> list[dict[str, Any]]:
"""Record listing options and return an empty result."""
self.calls.append(("ls", path, ctx))
self.list_options = options
return []
def tree_directory(
self,
path: str,
*,
ctx: dict[str, str] | None = None,
**options: Any,
) -> list[dict[str, Any]]:
"""Record tree options and return an empty result."""
self.calls.append(("tree_directory", path, ctx))
self.tree_options = options
return []
@pytest.mark.asyncio
async def test_async_client_derives_account_ctx_from_local_agfs_path() -> None:
client = _RecordingClient()
agfs = AsyncAGFSClient(client)
await agfs.write("/local/acct-1/data/file.txt", b"x")
await agfs.ls(
"/local/acct-1/data",
offset=2,
limit=3,
sort_by="mtime",
sort_order="desc",
)
assert client.calls == [
("write", "/local/acct-1/data/file.txt", {"account_id": "acct-1"}),
("ls", "/local/acct-1/data", {"account_id": "acct-1"}),
]
assert client.list_options == {
"offset": 2,
"limit": 3,
"sort_by": "mtime",
"sort_order": "desc",
}
@pytest.mark.asyncio
async def test_async_client_uses_system_ctx_for_non_local_agfs_path() -> None:
client = _RecordingClient()
agfs = AsyncAGFSClient(client)
await agfs.read("/queue/semantic/dequeue")
assert client.calls == [("read", "/queue/semantic/dequeue", {"account_id": "_system"})]
@pytest.mark.asyncio
async def test_async_client_passes_directories_only_to_tree_directory() -> None:
client = _RecordingClient()
agfs = AsyncAGFSClient(client)
await agfs.tree_directory("/local/acct-1/data", directories_only=True)
assert client.calls == [("tree_directory", "/local/acct-1/data", {"account_id": "acct-1"})]
assert client.tree_options["directories_only"] is True
@pytest.mark.asyncio
async def test_async_client_preserves_explicit_fs_ctx() -> None:
client = _RecordingClient()
agfs = AsyncAGFSClient(client)
await agfs.write(
"/local/path-account/data/file.txt", b"x", fs_ctx={"account_id": "ctx-account"}
)
assert client.calls == [
("write", "/local/path-account/data/file.txt", {"account_id": "ctx-account"})
]
@pytest.mark.asyncio
async def test_async_client_rejects_cross_account_mv() -> None:
client = _RecordingClient()
agfs = AsyncAGFSClient(client)
with pytest.raises(ValueError, match="cross-account"):
await agfs.mv("/local/a/data/file.txt", "/local/b/data/file.txt")
@pytest.mark.asyncio
async def test_async_client_types_native_invalid_pathlock_handoff() -> None:
class _InvalidHandoffClient:
def pathlock_adopt(self, ctx, handoff_ref):
del ctx, handoff_ref
raise ValueError(
"invalid lock request: handoff coverage '/local/default/resources/"
"信贷风险管理_1.md' does not map to lock path "
"'/local/default/resources/.exact.ovlock.1.md.deadbeef'"
)
agfs = AsyncAGFSClient(_InvalidHandoffClient())
with pytest.raises(InvalidPathLockRequestError) as exc_info:
await agfs.pathlock_adopt({"owner_id": "producer"})
assert "handoff coverage" in str(exc_info.value)
assert isinstance(exc_info.value.__cause__, ValueError)
def test_cp_rejects_cross_account_raw_copy() -> None:
class _CpClient:
"""Minimal cp client; methods must not be called after account guard fails."""
def stat(self, path: str) -> dict[str, Any]:
"""Fail if cp checks storage before validating account boundaries."""
raise AssertionError(f"unexpected stat call: {path}")
with pytest.raises(ValueError, match="cross-account"):
cp(_CpClient(), "/local/a/data/file.txt", "/local/b/data/file.txt")
def test_cp_rejects_cross_encryption_domain_raw_copy() -> None:
class _CpClient:
"""Minimal cp client; methods must not be called after account guard fails."""
def stat(self, path: str) -> dict[str, Any]:
"""Fail if cp checks storage before validating account boundaries."""
raise AssertionError(f"unexpected stat call: {path}")
with pytest.raises(ValueError, match="cross-account"):
cp(_CpClient(), "/local/a/data/file.txt", "/mem/data/file.txt")
@pytest.mark.asyncio
async def test_async_cp_streams_when_same_mount_fast_path_is_unavailable() -> None:
class _FallbackClient:
def __init__(self) -> None:
self.cat_calls: list[tuple[str, bool, dict[str, str] | None]] = []
self.written_chunks: list[bytes] = []
def stat(self, path: str, *, ctx: dict[str, str] | None = None) -> dict[str, Any]:
del ctx
return {"isDir": path.endswith("/data")}
def copy_within_mount(
self, source: str, target: str, *, ctx: dict[str, str] | None = None
) -> dict[str, bool]:
del source, target, ctx
return {"performed": False}
def cat(
self,
path: str,
*,
stream: bool = False,
ctx: dict[str, str] | None = None,
):
self.cat_calls.append((path, stream, ctx))
if not stream:
raise AssertionError("fallback copy loaded the complete file")
return iter((b"first", b"second"))
def write(self, path: str, data, *, ctx: dict[str, str] | None = None) -> str:
del path, ctx
self.written_chunks = list(data)
return "written"
client = _FallbackClient()
agfs = AsyncAGFSClient(client)
await agfs.cp(
"/local/acct/data/source.bin",
"/local/acct/data/target.bin",
fs_ctx={"account_id": "acct", "lease_ref": "lease"},
)
assert client.cat_calls == [
(
"/local/acct/data/source.bin",
True,
{"account_id": "acct", "lease_ref": "lease"},
)
]
assert client.written_chunks == [b"first", b"second"]
@pytest.mark.asyncio
async def test_async_cp_streams_by_default_when_same_mount_fast_path_is_available() -> None:
class _FastPathClient:
def __init__(self) -> None:
self.fast_path_calls: list[tuple[str, str]] = []
self.cat_calls: list[tuple[str, bool]] = []
self.written_chunks: list[bytes] = []
def stat(self, path: str, *, ctx: dict[str, str] | None = None) -> dict[str, Any]:
del ctx
return {"isDir": path.endswith("/data")}
def copy_within_mount(
self, source: str, target: str, *, ctx: dict[str, str] | None = None
) -> dict[str, bool]:
del ctx
self.fast_path_calls.append((source, target))
return {"performed": True}
def cat(
self,
path: str,
*,
stream: bool = False,
ctx: dict[str, str] | None = None,
):
del ctx
self.cat_calls.append((path, stream))
return iter((b"first", b"second"))
def write(self, path: str, data, *, ctx: dict[str, str] | None = None) -> str:
del path, ctx
self.written_chunks = list(data)
return "written"
client = _FastPathClient()
agfs = AsyncAGFSClient(client)
await agfs.cp(
"/local/acct/data/source.bin",
"/local/acct/data/target.bin",
)
assert client.fast_path_calls == []
assert client.cat_calls == [("/local/acct/data/source.bin", True)]
assert client.written_chunks == [b"first", b"second"]
@pytest.mark.asyncio
async def test_async_cp_can_explicitly_use_same_mount_fast_path() -> None:
class _FastPathClient:
def __init__(self) -> None:
self.fast_path_calls: list[tuple[str, str]] = []
def stat(self, path: str, *, ctx: dict[str, str] | None = None) -> dict[str, Any]:
del ctx
return {"isDir": path.endswith("/data")}
def copy_within_mount(
self, source: str, target: str, *, ctx: dict[str, str] | None = None
) -> dict[str, bool]:
del ctx
self.fast_path_calls.append((source, target))
return {"performed": True}
def cat(self, *args, **kwargs):
raise AssertionError("explicit fast path must not stream file bytes")
client = _FastPathClient()
agfs = AsyncAGFSClient(client)
await agfs.cp(
"/local/acct/data/source.bin",
"/local/acct/data/target.bin",
allow_same_mount_fast_path=True,
)
assert client.fast_path_calls == [
("/local/acct/data/source.bin", "/local/acct/data/target.bin")
]
@pytest.mark.asyncio
async def test_async_cp_falls_back_to_complete_write_when_binding_cannot_stream() -> None:
class _BindingClient:
def __init__(self) -> None:
self.fast_path_calls: list[tuple[str, str]] = []
self.cat_calls: list[tuple[str, bool]] = []
self.written: bytes | None = None
def stat(self, path: str, *, ctx: dict[str, str] | None = None) -> dict[str, Any]:
del ctx
return {"isDir": path.endswith("/data")}
def copy_within_mount(
self, source: str, target: str, *, ctx: dict[str, str] | None = None
) -> dict[str, bool]:
del ctx
self.fast_path_calls.append((source, target))
return {"performed": True}
def cat(
self,
path: str,
*,
stream: bool = False,
ctx: dict[str, str] | None = None,
) -> bytes:
del ctx
self.cat_calls.append((path, stream))
if stream:
raise RuntimeError("Streaming not supported in binding mode")
return b"complete payload"
def write(self, path: str, data: bytes, *, ctx: dict[str, str] | None = None) -> str:
del path, ctx
self.written = data
return "written"
client = _BindingClient()
agfs = AsyncAGFSClient(client)
await agfs.cp(
"/local/acct/data/source.bin",
"/local/acct/data/target.bin",
)
assert client.fast_path_calls == []
assert client.cat_calls == [
("/local/acct/data/source.bin", True),
("/local/acct/data/source.bin", False),
]
assert client.written == b"complete payload"