Skip to content

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

6.9 KB
Raw Blame
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0

import pytest

from openviking.server.identity import RequestContext, Role
from openviking.storage.queuefs.semantic_executor import SemanticTreeExecutor
from openviking_cli.session.user_id import UserIdentifier


class _FakeVikingFS:
    def __init__(self, tree):
        self._tree = tree
        self.writes = []
        self._async_agfs = self

    async def ls(self, uri, node_limit=None, ctx=None):
        return self._tree.get(uri, [])

    async def write_file(self, path, content, ctx=None, lease_ref=None):
        self.writes.append((path, content))

    async def pathlock_acquire_exact_batch(self, paths):
        return {"paths": paths}

    async def pathlock_release(self, lease):
        return None

    def _uri_to_path(self, uri, ctx=None):
        return uri.replace("viking://", "/local/acc1/")


class _UnlistableRootVikingFS(_FakeVikingFS):
    def __init__(self, *, fail_sidecar_write, list_error):
        super().__init__({})
        self.fail_sidecar_write = fail_sidecar_write
        self.list_error = list_error
        self.materialized_dirs = set()

    async def ls(self, uri, node_limit=None, ctx=None):
        raise self.list_error(uri)

    async def write_file(self, path, content, ctx=None, lease_ref=None):
        self.materialized_dirs.add(path.rsplit("/", 1)[0])
        if self.fail_sidecar_write:
            raise OSError("sidecar write failed after creating parent")
        await super().write_file(path, content, ctx=ctx, lease_ref=lease_ref)


class _FakeProcessor:
    def __init__(self):
        self.summarized_files = []
        self.vectorized_files = []
        self.overview_contexts = []

    async def _generate_single_file_summary(self, file_path, llm_sem=None, ctx=None):
        self.summarized_files.append(file_path)
        return {"name": file_path.split("/")[-1], "summary": "summary"}

    async def _generate_overview(self, dir_uri, file_summaries, children_abstracts, **kwargs):
        self.overview_contexts.append(kwargs.get("ctx"))
        return "overview"

    def _normalize_overview_generation(self, overview):
        return overview, "abstract"

    async def _vectorize_directory(
        self,
        uri,
        context_type,
        abstract,
        overview,
        ctx=None,
        ingest_options=None,
    ):
        pass

    async def _vectorize_directory_simple(self, uri, context_type, abstract, overview, ctx=None):
        await self._vectorize_directory(uri, context_type, abstract, overview, ctx=ctx)

    async def _vectorize_single_file(
        self,
        parent_uri,
        context_type,
        file_path,
        summary_dict,
        ctx=None,
        use_summary=False,
        ingest_options=None,
        file_md5=None,
    ):
        self.vectorized_files.append(file_path)


@pytest.mark.asyncio
async def test_messages_jsonl_excluded_from_summary(monkeypatch):
    """messages.jsonl should be skipped by _list_dir and never summarized."""
    root_uri = "viking://user/user1/sessions/test-session"
    tree = {
        root_uri: [
            {"name": "messages.jsonl", "isDir": False},
            {"name": "notes.txt", "isDir": False},
            {"name": "document.pdf", "isDir": False},
        ],
    }
    fake_fs = _FakeVikingFS(tree)
    monkeypatch.setattr(
        "openviking.storage.queuefs.semantic_executor.get_viking_fs", lambda: fake_fs
    )

    processor = _FakeProcessor()
    ctx = RequestContext(user=UserIdentifier("acc1", "user1"), role=Role.USER)
    executor = SemanticTreeExecutor(
        processor=processor,
        context_type="session",
        max_concurrent_llm=2,
        ctx=ctx,
    )
    await executor.run(root_uri)

    summarized_names = [p.split("/")[-1] for p in processor.summarized_files]
    assert "messages.jsonl" not in summarized_names
    assert "notes.txt" in summarized_names
    assert "document.pdf" in summarized_names
    assert processor.overview_contexts == [ctx]


@pytest.mark.asyncio
async def test_messages_jsonl_excluded_in_subdirectory(monkeypatch):
    """messages.jsonl in a subdirectory should also be skipped."""
    root_uri = "viking://user/user1/sessions/test-session"
    tree = {
        root_uri: [
            {"name": "subdir", "isDir": True},
        ],
        f"{root_uri}/subdir": [
            {"name": "messages.jsonl", "isDir": False},
            {"name": "data.csv", "isDir": False},
        ],
    }
    fake_fs = _FakeVikingFS(tree)
    monkeypatch.setattr(
        "openviking.storage.queuefs.semantic_executor.get_viking_fs", lambda: fake_fs
    )

    processor = _FakeProcessor()
    ctx = RequestContext(user=UserIdentifier("acc1", "user1"), role=Role.USER)
    executor = SemanticTreeExecutor(
        processor=processor,
        context_type="session",
        max_concurrent_llm=2,
        ctx=ctx,
    )
    await executor.run(root_uri)

    summarized_names = [p.split("/")[-1] for p in processor.summarized_files]
    assert "messages.jsonl" not in summarized_names
    assert "data.csv" in summarized_names


@pytest.mark.asyncio
@pytest.mark.parametrize("fail_sidecar_write", [False, True])
@pytest.mark.parametrize("list_error", [FileNotFoundError, NotADirectoryError])
async def test_unlistable_semantic_root_is_not_materialized_as_directory(
    monkeypatch, fail_sidecar_write, list_error
):
    root_uri = "viking://user/user1/memories/profile.md"
    fake_fs = _UnlistableRootVikingFS(
        fail_sidecar_write=fail_sidecar_write,
        list_error=list_error,
    )
    monkeypatch.setattr(
        "openviking.storage.queuefs.semantic_executor.get_viking_fs", lambda: fake_fs
    )

    processor = _FakeProcessor()
    ctx = RequestContext(user=UserIdentifier("acc1", "user1"), role=Role.USER)
    executor = SemanticTreeExecutor(
        processor=processor,
        context_type="memory",
        max_concurrent_llm=2,
        ctx=ctx,
        generation_trigger="reindex",
        skip_vectorization=True,
    )

    await executor.run(root_uri)

    assert root_uri not in fake_fs.materialized_dirs
    assert fake_fs.writes == []


@pytest.mark.asyncio
async def test_empty_semantic_directory_still_receives_sidecars(monkeypatch):
    root_uri = "viking://user/user1/memories/empty"
    fake_fs = _FakeVikingFS({root_uri: []})
    monkeypatch.setattr(
        "openviking.storage.queuefs.semantic_executor.get_viking_fs", lambda: fake_fs
    )

    processor = _FakeProcessor()
    ctx = RequestContext(user=UserIdentifier("acc1", "user1"), role=Role.USER)
    executor = SemanticTreeExecutor(
        processor=processor,
        context_type="memory",
        max_concurrent_llm=2,
        ctx=ctx,
        generation_trigger="reindex",
        skip_vectorization=True,
    )

    await executor.run(root_uri)

    assert [path for path, _content in fake_fs.writes] == [
        f"{root_uri}/.overview.md",
        f"{root_uri}/.abstract.md",
    ]


if __name__ == "__main__":
    pytest.main([__file__])