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
import os
import uuid
import pytest
def write_or_skip_unavailable(api_client, uri, content):
response = api_client.fs_write(uri, content, mode="create", wait=True)
if response.status_code == 200:
return response
if os.getenv("HAS_SECRETS", "true").lower() != "true":
try:
error = response.json().get("error")
except Exception:
error = response.text
pytest.skip(
f"fs_write failed on this environment: {error}. "
"This may be due to AGFS service not being available."
)
assert response.status_code == 200, response.text
return response
class TestFsCp:
def test_cp_file_preserves_source_and_content(self, api_client):
suffix = uuid.uuid4().hex[:8]
source = f"viking://resources/cp-source-{suffix}.md"
target = f"viking://resources/cp-target-{suffix}.md"
content = f"copy payload {suffix}"
try:
write = write_or_skip_unavailable(api_client, source, content)
if os.getenv("HAS_SECRETS", "true").lower() == "true":
write_result = write.json().get("result", {})
preparation_error = f"Source index preparation failed: {write.text}"
assert write_result.get("vector_status") == "complete", preparation_error
assert write_result.get("semantic_status") != "failed", preparation_error
queue_status = write_result.get("queue_status") or {}
for queue_name in ("Semantic", "Embedding"):
queue_result = queue_status.get(queue_name, {})
assert not queue_result.get("error_count", 0), preparation_error
assert not queue_result.get("errors"), preparation_error
source_index = api_client.find(
query="",
target_uri=source,
filter={"op": "must", "field": "uri", "conds": [source]},
limit=5,
)
source_index_error = (
f"Source index missing before cp: write={write.text}; find={source_index.text}"
)
assert source_index.status_code == 200, source_index_error
resources = source_index.json().get("result", {}).get("resources", [])
assert any(item.get("uri") == source for item in resources), source_index_error
copied = api_client.fs_cp(source, target)
assert copied.status_code == 200, copied.text
result = copied.json().get("result", {})
assert result.get("from") == source
assert result.get("to") == target
assert result.get("recursive") is False
for uri in (source, target):
stat = api_client.fs_stat(uri)
assert stat.status_code == 200
read = api_client.fs_read(uri)
assert read.status_code == 200
assert content in read.json().get("result", "")
if os.getenv("HAS_SECRETS", "true").lower() == "true":
vectors = result.get("vectors", {})
assert vectors.get("scanned", 0) > 0, copied.text
assert vectors.get("written") == vectors["scanned"], copied.text
# This contract checks index copying, not the embedding model's
# similarity score for a random UUID. Filter-only find still
# reads the vector store and preserves tenant/access scoping.
for uri in (source, target):
found = api_client.find(
query="",
target_uri=uri,
filter={"op": "must", "field": "uri", "conds": [uri]},
limit=5,
)
assert found.status_code == 200, found.text
resources = found.json().get("result", {}).get("resources", [])
assert any(item.get("uri") == uri for item in resources), found.text
assert api_client.fs_rm(source).status_code == 200
assert api_client.fs_read(target).status_code == 200
finally:
api_client.fs_rm(source)
api_client.fs_rm(target)
def test_cp_directory_requires_recursive_and_copies_tree(self, api_client):
suffix = uuid.uuid4().hex[:8]
source = f"viking://resources/cp-dir-source-{suffix}"
target = f"viking://resources/cp-dir-target-{suffix}"
child = f"{source}/nested/child.md"
try:
assert api_client.fs_mkdir(source).status_code == 200
assert api_client.fs_mkdir(f"{source}/nested").status_code == 200
assert api_client.fs_mkdir(f"{source}/empty").status_code == 200
write_or_skip_unavailable(api_client, child, "recursive copy child")
without_recursive = api_client.fs_cp(source, target)
assert without_recursive.status_code == 400, without_recursive.text
# Request-scoped write completion can precede ancestor refreshes.
# Copy needs a lease on the whole source tree, so settle those first.
settled = api_client.system_wait()
assert settled.status_code == 200, settled.text
copied = api_client.fs_cp(source, target, recursive=True)
assert copied.status_code == 200, copied.text
assert api_client.fs_read(f"{target}/nested/child.md").status_code == 200
tree = api_client.fs_tree(target)
assert tree.status_code == 200
tree_text = tree.text
assert "nested/child.md" in tree_text
assert "empty" in tree_text
finally:
api_client.fs_rm(source, recursive=True)
api_client.fs_rm(target, recursive=True)
def test_cp_overwrites_existing_target_and_preserves_source(self, api_client):
suffix = uuid.uuid4().hex[:8]
source = f"viking://resources/cp-overwrite-source-{suffix}.md"
target = f"viking://resources/cp-overwrite-target-{suffix}.md"
source_content = f"new payload {suffix}"
target_content = f"old target content must be completely replaced {suffix}"
try:
write_or_skip_unavailable(api_client, source, source_content)
write_or_skip_unavailable(api_client, target, target_content)
copied = api_client.fs_cp(source, target)
assert copied.status_code == 200, copied.text
result = copied.json().get("result", {})
assert result.get("from") == source
assert result.get("to") == target
assert result.get("phase") == "completed"
assert result.get("recursive") is False
for uri in (source, target):
read = api_client.fs_read(uri)
assert read.status_code == 200, read.text
assert read.json().get("result") == source_content
finally:
api_client.fs_rm(source)
api_client.fs_rm(target)
@pytest.mark.parametrize("source_is_dir", [False, True], ids=["file-to-dir", "dir-to-file"])
def test_cp_rejects_file_directory_type_conflicts(self, api_client, source_is_dir):
suffix = uuid.uuid4().hex[:8]
file_uri = f"viking://resources/cp-type-file-{suffix}.md"
directory_uri = f"viking://resources/cp-type-dir-{suffix}"
child_uri = f"{directory_uri}/child.md"
file_content = f"file remains {suffix}"
child_content = f"directory child remains {suffix}"
try:
assert api_client.fs_mkdir(directory_uri).status_code == 200
for uri, content in ((file_uri, file_content), (child_uri, child_content)):
write_or_skip_unavailable(api_client, uri, content)
source, target = (
(directory_uri, file_uri) if source_is_dir else (file_uri, directory_uri)
)
copied = api_client.fs_cp(source, target, recursive=source_is_dir)
assert copied.status_code == 400, copied.text
assert copied.json().get("error", {}).get("code") == "INVALID_ARGUMENT"
for uri, content in ((file_uri, file_content), (child_uri, child_content)):
read = api_client.fs_read(uri)
assert read.status_code == 200, read.text
assert read.json().get("result") == content
finally:
api_client.fs_rm(file_uri)
api_client.fs_rm(directory_uri, recursive=True)