Skip to content

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

perf(grep): use native scanner for canonical sessions (#5271)

Lusen Dong515bd01f6783
diff --git a/openviking/service/fs_service.py b/openviking/service/fs_service.py
index da2723986..fc513e039 100644
--- a/openviking/service/fs_service.py
+++ b/openviking/service/fs_service.py
@@ -11,7 +11,13 @@ from collections.abc import Coroutine
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict, List, Literal, Optional
from openviking.core.context import ContextLevel
-from openviking.core.namespace import classify_uri, context_type_for_uri, uri_leaf_name, uri_parts
+from openviking.core.namespace import (
+ classify_uri,
+ context_type_for_uri,
+ is_session_uri,
+ uri_leaf_name,
+ uri_parts,
+)
from openviking.privacy import (
UserPrivacyConfigService,
get_skill_name_from_uri,
@@ -55,6 +61,8 @@ logger = get_logger(__name__)
def _may_include_memory_content(uri: str) -> bool:
"""Return whether a public subtree read can contain memory files."""
+ if is_session_uri(uri):
+ return False
classification = classify_uri(uri)
if classification.is_memory:
return True
diff --git a/openviking/storage/viking_fs/_grep.py b/openviking/storage/viking_fs/_grep.py
index 4ae02c403..26a698c70 100644
--- a/openviking/storage/viking_fs/_grep.py
+++ b/openviking/storage/viking_fs/_grep.py
@@ -8,6 +8,7 @@ import sys
import time
from typing import Any, Callable, Dict, List, Optional, Set
+from openviking.core.namespace import is_session_uri
from openviking.pyagfs.exceptions import AGFSNotSupportedError
from openviking.server.identity import RequestContext
from openviking.storage.expr import And, PathScope, RawDSL
@@ -82,7 +83,7 @@ class _GrepMixin:
# persisted raw content, so it cannot safely recall projected results.
resolved_engine = (
"fs"
- if content_transform is not None
+ if content_transform is not None or is_session_uri(uri)
else await self._resolve_grep_engine(engine, uri, ctx, switch_to_remote_threshold)
)
tags_by_uri: Dict[str, List[str]] = {}
@@ -248,15 +249,29 @@ class _GrepMixin:
after_context=0,
):
"""Filesystem grep path: prefer native agfs grep and fall back if unavailable."""
- if content_transform is None and allowed_uris is None:
+ native_safe = (
+ content_transform is None
+ and allowed_uris is None
+ and await self._session_native_grep_safe(uri, ctx)
+ )
+ if native_safe:
try:
+ # Session grep historically used the Python fallback, where
+ # level_limit counts directory expansions and therefore
+ # includes files one path segment deeper than native grep.
+ # Preserve that public behavior when selecting the fast path.
+ native_level_limit = (
+ level_limit + 1
+ if is_session_uri(uri) and level_limit is not None
+ else level_limit
+ )
return await self._grep_with_agfs(
uri=uri,
pattern=pattern,
exclude_uri=exclude_uri,
case_insensitive=case_insensitive,
node_limit=node_limit,
- level_limit=level_limit,
+ level_limit=native_level_limit,
ctx=ctx,
before_context=before_context,
after_context=after_context,
@@ -278,6 +293,37 @@ class _GrepMixin:
after_context=after_context,
)
+ async def _session_native_grep_safe(self, uri: str, ctx: Optional[RequestContext]) -> bool:
+ """Return whether native grep sees every visible path for ``uri``.
+
+ Canonical session reads merge the current user namespace with two
+ historical storage layouts. Native AGFS grep accepts one physical
+ root, so it is complete only when no visible legacy candidate exists.
+ """
+ legacy_uri = self._legacy_session_alias(uri)
+ if legacy_uri is None:
+ return True
+
+ real_ctx = self._ctx_or_default(ctx)
+ if self._is_session_root_uri(uri):
+ primary_path = self._uri_to_path(uri, ctx=ctx)
+ if not await self._agfs_path_exists(primary_path):
+ return False
+ legacy_path = self._legacy_session_path(legacy_uri, ctx=ctx)
+ owner_user_id = self._safe_uri_parts(uri)[1]
+ legacy_items = await self._legacy_session_root_items(
+ legacy_path, real_ctx, uri.rstrip("/"), owner_user_id
+ )
+ return not legacy_items
+
+ primary_path = self._uri_to_path(uri, ctx=ctx)
+ for path in self._read_paths(uri, ctx=ctx)[1:]:
+ if not await self._agfs_path_exists(path):
+ continue
+ if await self._read_path_visible(uri, path, primary_path, real_ctx):
+ return False
+ return True
+
async def _grep_vikingdb_then_fs(
self,
uri,
diff --git a/tests/service/test_fs_service.py b/tests/service/test_fs_service.py
index da6da48ac..d698f64ff 100644
--- a/tests/service/test_fs_service.py
+++ b/tests/service/test_fs_service.py
@@ -305,6 +305,14 @@ async def test_grep_projects_memory_content_but_keeps_resource_fast_path(request
await service.grep("viking://resources", "secret", ctx=request_context)
assert "content_transform" not in viking_fs.grep.await_args.kwargs
+ viking_fs.grep.reset_mock()
+ await service.grep(
+ "viking://user/ryoma/sessions/session-1",
+ "secret",
+ ctx=request_context,
+ )
+ assert "content_transform" not in viking_fs.grep.await_args.kwargs
+
@pytest.mark.asyncio
async def test_grep_forwards_context_to_viking_fs(request_context):
diff --git a/tests/storage/test_viking_fs_actor_peer_view.py b/tests/storage/test_viking_fs_actor_peer_view.py
index dfd167331..9513fa900 100644
--- a/tests/storage/test_viking_fs_actor_peer_view.py
+++ b/tests/storage/test_viking_fs_actor_peer_view.py
@@ -247,6 +247,64 @@ async def test_legacy_session_scope_merges_new_and_unmigrated_sessions(fs, actor
await fs.ls(f"{session_root}/other-owned", ctx=actor_ctx)
+async def test_session_grep_preserves_legacy_merge_and_primary_shadow(fs, actor_ctx):
+ session_root = "viking://user/support_bot/sessions"
+
+ result = await fs.grep(
+ session_root,
+ pattern="new|legacy|nested|other",
+ ctx=actor_ctx,
+ )
+
+ assert result["matches"] == [
+ {
+ "uri": f"{session_root}/duplicate/messages.jsonl",
+ "line": 1,
+ "content": '{"role":"user","content":"new"}',
+ },
+ {
+ "uri": f"{session_root}/new-session/messages.jsonl",
+ "line": 1,
+ "content": '{"role":"user","content":"new only"}',
+ },
+ {
+ "uri": f"{session_root}/legacy-session/messages.jsonl",
+ "line": 1,
+ "content": '{"role":"user","content":"legacy"}',
+ },
+ {
+ "uri": f"{session_root}/nested-session/messages.jsonl",
+ "line": 1,
+ "content": '{"role":"user","content":"nested"}',
+ },
+ ]
+
+
+async def test_session_native_grep_gate_allows_primary_only_session(fs, actor_ctx):
+ assert await fs._session_native_grep_safe(
+ "viking://user/support_bot/sessions/new-session", actor_ctx
+ )
+
+
+async def test_session_native_grep_gate_rejects_visible_legacy_layouts(fs, actor_ctx):
+ assert not await fs._session_native_grep_safe(
+ "viking://user/support_bot/sessions/legacy-session", actor_ctx
+ )
+ assert not await fs._session_native_grep_safe(
+ "viking://user/support_bot/sessions/nested-session", actor_ctx
+ )
+
+
+async def test_session_native_grep_gate_ignores_other_owner_legacy_data(fs, actor_ctx):
+ assert await fs._session_native_grep_safe(
+ "viking://user/support_bot/sessions/other-owned", actor_ctx
+ )
+
+
@pytest.mark.asyncio
async def test_actor_peer_view_filters_tree_from_user_root(fs, actor_ctx):
entries = await fs.tree(
diff --git a/tests/storage/test_viking_fs_grep.py b/tests/storage/test_viking_fs_grep.py
index c85845533..a4ae3cdf9 100644
--- a/tests/storage/test_viking_fs_grep.py
+++ b/tests/storage/test_viking_fs_grep.py
@@ -287,6 +287,157 @@ async def test_grep_without_config_uses_documented_remote_threshold(monkeypatch)
assert await fs._resolve_grep_engine("auto", "viking://resources", None) == "fs"
+ "uri",
+ [
+ "viking://user/alice/sessions/session-1",
+ "viking://session/session-1",
+ ],
+)
+async def test_session_grep_forces_fs_engine_in_auto_mode(monkeypatch, uri):
+ viking_fs = VikingFS(agfs=_DummyAgfs())
+ monkeypatch.setattr(viking_fs, "stat", AsyncMock(return_value={"isDir": True}))
+ resolve_engine = AsyncMock(return_value="vikingdb_then_fs")
+ grep_fs = AsyncMock(
+ return_value={"matches": [], "count": 0, "match_count": 0, "files_scanned": 0}
+ )
+ grep_vikingdb = AsyncMock()
+ monkeypatch.setattr(viking_fs, "_resolve_grep_engine", resolve_engine)
+ monkeypatch.setattr(viking_fs, "_grep_fs", grep_fs)
+ monkeypatch.setattr(viking_fs, "_grep_vikingdb_then_fs", grep_vikingdb)
+
+ await viking_fs.grep(
+ uri,
+ pattern="needle",
+ )
+
+ resolve_engine.assert_not_awaited()
+ grep_fs.assert_awaited_once()
+ grep_vikingdb.assert_not_awaited()
+
+
+async def test_primary_only_session_grep_uses_native_agfs(monkeypatch):
+ viking_fs = VikingFS(agfs=_DummyAgfs())
+ native_result = {"matches": [], "count": 0, "match_count": 0, "files_scanned": 4}
+ native_grep = AsyncMock(return_value=native_result)
+ fallback_grep = AsyncMock()
+ monkeypatch.setattr(viking_fs, "_session_native_grep_safe", AsyncMock(return_value=True))
+ monkeypatch.setattr(viking_fs, "_grep_with_agfs", native_grep)
+ monkeypatch.setattr(viking_fs, "_grep_encrypted", fallback_grep)
+
+ result = await viking_fs._grep_fs(
+ uri="viking://user/alice/sessions/session-1",
+ pattern="needle",
+ exclude_uri="viking://user/alice/sessions/session-1/tools",
+ case_insensitive=True,
+ node_limit=7,
+ level_limit=3,
+ ctx=None,
+ )
+
+ assert result == native_result
+ native_grep.assert_awaited_once_with(
+ uri="viking://user/alice/sessions/session-1",
+ pattern="needle",
+ exclude_uri="viking://user/alice/sessions/session-1/tools",
+ case_insensitive=True,
+ node_limit=7,
+ level_limit=4,
+ ctx=None,
+ before_context=0,
+ after_context=0,
+ )
+ fallback_grep.assert_not_awaited()
+
+
+async def test_session_grep_with_visible_legacy_data_uses_merge_fallback(monkeypatch):
+ viking_fs = VikingFS(agfs=_DummyAgfs())
+ fallback_result = {"matches": [], "count": 0, "match_count": 0, "files_scanned": 2}
+ native_grep = AsyncMock()
+ fallback_grep = AsyncMock(return_value=fallback_result)
+ monkeypatch.setattr(viking_fs, "_session_native_grep_safe", AsyncMock(return_value=False))
+ monkeypatch.setattr(viking_fs, "_grep_with_agfs", native_grep)
+ monkeypatch.setattr(viking_fs, "_grep_encrypted", fallback_grep)
+
+ result = await viking_fs._grep_fs(
+ uri="viking://user/alice/sessions/session-1",
+ pattern="needle",
+ exclude_uri=None,
+ case_insensitive=False,
+ node_limit=None,
+ level_limit=10,
+ ctx=None,
+ )
+
+ assert result == fallback_result
+ native_grep.assert_not_awaited()
+ fallback_grep.assert_awaited_once()
+ assert fallback_grep.await_args.kwargs["level_limit"] == 10
+
+
+async def test_session_native_and_legacy_fallback_keep_level_limit_results_equal(monkeypatch):
+ viking_fs = VikingFS(agfs=_DummyAgfs())
+ match = {
+ "uri": "viking://user/alice/sessions/s1/messages.jsonl",
+ "line": 1,
+ "content": "payment failed",
+ }
+
+ async def fake_native_grep(**kwargs):
+ matches = [match] if kwargs["level_limit"] >= 2 else []
+ return {
+ "matches": matches,
+ "count": len(matches),
+ "match_count": len(matches),
+ "files_scanned": len(matches),
+ }
+
+ async def fake_fallback_grep(**kwargs):
+ matches = [match] if kwargs["level_limit"] >= 1 else []
+ return {
+ "matches": matches,
+ "count": len(matches),
+ "match_count": len(matches),
+ "files_scanned": len(matches),
+ }
+
+ monkeypatch.setattr(viking_fs, "_grep_with_agfs", fake_native_grep)
+ monkeypatch.setattr(viking_fs, "_grep_encrypted", fake_fallback_grep)
+ native_safe = AsyncMock(side_effect=[True, False])
+ monkeypatch.setattr(viking_fs, "_session_native_grep_safe", native_safe)
+ kwargs = {
+ "uri": "viking://user/alice/sessions",
+ "pattern": "payment failed",
+ "exclude_uri": None,
+ "case_insensitive": False,
+ "node_limit": None,
+ "level_limit": 1,
+ "ctx": None,
+ }
+
+ assert await viking_fs._grep_fs(**kwargs) == await viking_fs._grep_fs(**kwargs)
+
+
+async def test_virtual_empty_session_root_uses_merge_fallback(monkeypatch):
+ viking_fs = VikingFS(agfs=_DummyAgfs())
+ primary_path = "/local/default/user/alice/sessions"
+ path_exists = AsyncMock(return_value=False)
+ legacy_items = AsyncMock(return_value=[])
+ monkeypatch.setattr(viking_fs, "_agfs_path_exists", path_exists)
+ monkeypatch.setattr(viking_fs, "_legacy_session_root_items", legacy_items)
+
+ assert not await viking_fs._session_native_grep_safe(
+ "viking://user/alice/sessions", None
+ )
+ path_exists.assert_awaited_once_with(primary_path)
+ legacy_items.assert_not_awaited()
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize(
("node_limit", "expected_remote_limit"),