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
Fix retriever (#382)
zhoujiahui2699d7c0de69
diff --git a/openviking/core/context.py b/openviking/core/context.pyindex 063cbcc18..ab72f1d45 100644--- a/openviking/core/context.py+++ b/openviking/core/context.py@@ -65,6 +65,7 @@ class Context:active_count: int = 0,related_uri: Optional[List[str]] = None,meta: Optional[Dict[str, Any]] = None,+ level: int | ContextLevel | None = None,session_id: Optional[str] = None,user: Optional[UserIdentifier] = None,account_id: Optional[str] = None,@@ -86,6 +87,10 @@ class Context:self.active_count = active_countself.related_uri = related_uri or []self.meta = meta or {}+ try:+ self.level = int(level) if level is not None else None+ except (TypeError, ValueError):+ self.level = Noneself.session_id = session_idself.user = userself.account_id = account_id or (user.account_id if user else "default")@@ -168,6 +173,8 @@ class Context:"account_id": self.account_id,"owner_space": self.owner_space,}+ if self.level is not None:+ data["level"] = int(self.level)if self.user:data["user"] = self.user.to_dict()@@ -204,6 +211,13 @@ class Context:active_count=data.get("active_count", 0),related_uri=data.get("related_uri", []),meta=data.get("meta", {}),+ level=(+ data.get("level")+ if data.get("level") is not None+ else data.get("meta", {}).get("level")+ if isinstance(data.get("meta"), dict)+ else None+ ),session_id=data.get("session_id"),user=user_obj,account_id=data.get("account_id"),diff --git a/openviking/core/directories.py b/openviking/core/directories.pyindex 080cf7ca3..252fc3c68 100644--- a/openviking/core/directories.py+++ b/openviking/core/directories.py@@ -219,41 +219,72 @@ class DirectoryInitializer:logger = get_logger(__name__)created = False+ agfs_created = False# 1. Ensure files exist in AGFSif not await self._check_agfs_files_exist(uri, ctx=ctx):logger.debug(f"[VikingFS] Creating directory: {uri} for scope {scope}")await self._create_agfs_structure(uri, defn.abstract, defn.overview, ctx=ctx)created = True+ agfs_created = Trueelse:logger.debug(f"[VikingFS] Directory {uri} already exists")- # 2. Ensure record exists in vector storage- existing = await self.vikingdb.get_context_by_uri(- account_id=ctx.account_id,- uri=uri,- limit=1,- )- if not existing:- owner_space = ""- if scope in {"user", "session"}:- owner_space = ctx.user.user_space_name()- elif scope == "agent":- owner_space = ctx.user.agent_space_name()+ # 2. Seed directory L0/L1 vectors only during fresh initialization.+ owner_space = self._owner_space_for_scope(scope=scope, ctx=ctx)+ if agfs_created:+ await self._ensure_directory_l0_l1_vectors(+ uri=uri,+ parent_uri=parent_uri,+ defn=defn,+ owner_space=owner_space,+ ctx=ctx,+ )+ return created++ async def _ensure_directory_l0_l1_vectors(+ self,+ uri: str,+ parent_uri: Optional[str],+ defn: DirectoryDefinition,+ owner_space: str,+ ctx: RequestContext,+ ) -> None:+ """Ensure L0/L1 vector records exist for a preset directory."""+ for level, vector_text in (+ (0, defn.abstract),+ (1, defn.overview),+ ):+ existing = await self.vikingdb.get_context_by_uri(+ account_id=ctx.account_id,+ uri=uri,+ level=level,+ limit=1,+ )+ if existing:+ continuecontext = Context(uri=uri,parent_uri=parent_uri,is_leaf=False,context_type=get_context_type_for_uri(uri),abstract=defn.abstract,+ level=level,user=ctx.user,account_id=ctx.account_id,owner_space=owner_space,)- context.set_vectorize(Vectorize(text=defn.overview))- dir_emb_msg = EmbeddingMsgConverter.from_context(context)- await self.vikingdb.enqueue_embedding_msg(dir_emb_msg)- created = True- return created+ context.set_vectorize(Vectorize(text=vector_text))+ emb_msg = EmbeddingMsgConverter.from_context(context)+ if emb_msg:+ await self.vikingdb.enqueue_embedding_msg(emb_msg)++ @staticmethod+ def _owner_space_for_scope(scope: str, ctx: RequestContext) -> str:+ if scope in {"user", "session"}:+ return ctx.user.user_space_name()+ if scope == "agent":+ return ctx.user.agent_space_name()+ return ""async def _check_agfs_files_exist(self, uri: str, ctx: RequestContext) -> bool:"""Check if L0/L1 files exist in AGFS."""diff --git a/openviking/retrieve/hierarchical_retriever.py b/openviking/retrieve/hierarchical_retriever.pyindex d342c3d99..b9494d7fb 100644--- a/openviking/retrieve/hierarchical_retriever.py+++ b/openviking/retrieve/hierarchical_retriever.py@@ -43,6 +43,7 @@ class HierarchicalRetriever:DIRECTORY_DOMINANCE_RATIO = 1.2 # Directory score must exceed max child scoreGLOBAL_SEARCH_TOPK = 3 # Global retrieval countHOTNESS_ALPHA = 0.2 # Weight for hotness score in final ranking (0 = disabled)+ LEVEL_URI_SUFFIX = {0: ".abstract.md", 1: ".overview.md"}def __init__(self,@@ -262,7 +263,7 @@ class HierarchicalRetriever:sparse_query_vector = sparse_query_vector or None- collected: List[Dict[str, Any]] = [] # Collected results (directories and leaves)+ collected_by_uri: Dict[str, Dict[str, Any]] = {}dir_queue: List[tuple] = [] # Priority queue: (-score, uri)visited: set = set()prev_topk_uris: set = set()@@ -324,26 +325,27 @@ class HierarchicalRetriever:)continue- # Always collect results that pass threshold, even if already- # visited as a directory starting point. The visited set only- # prevents re-entering directories for child search.- if not any(c.get("uri") == uri for c in collected):+ # Deduplicate by URI and keep the highest-scored candidate.+ previous = collected_by_uri.get(uri)+ if previous is None or final_score > previous.get("_final_score", 0):r["_final_score"] = final_score- collected.append(r)+ collected_by_uri[uri] = rlogger.debug(- f"[RecursiveSearch] Added URI: {uri} to candidates with score: {final_score}"+ "[RecursiveSearch] Updated URI: %s candidate score to %.4f",+ uri,+ final_score,)- if uri not in visited:- if r.get("level") == 2:- visited.add(uri)- else:- heapq.heappush(dir_queue, (-final_score, uri))+ # Only recurse into directories (L0/L1). L2 files are terminal hits.+ if uri not in visited and r.get("level", 2) != 2:+ heapq.heappush(dir_queue, (-final_score, uri))# Convergence check- current_topk = sorted(collected, key=lambda x: x.get("_final_score", 0), reverse=True)[- :limit- ]+ current_topk = sorted(+ collected_by_uri.values(),+ key=lambda x: x.get("_final_score", 0),+ reverse=True,+ )[:limit]current_topk_uris = {c.get("uri", "") for c in current_topk}if current_topk_uris == prev_topk_uris and len(current_topk_uris) >= limit:@@ -355,7 +357,11 @@ class HierarchicalRetriever:convergence_rounds = 0prev_topk_uris = current_topk_uris- collected.sort(key=lambda x: x.get("_final_score", 0), reverse=True)+ collected = sorted(+ collected_by_uri.values(),+ key=lambda x: x.get("_final_score", 0),+ reverse=True,+ )return collected[:limit]async def _convert_to_matched_contexts(@@ -407,14 +413,16 @@ class HierarchicalRetriever:alpha = self.HOTNESS_ALPHAfinal_score = (1 - alpha) * semantic_score + alpha * h_score+ level = c.get("level", 2)+ display_uri = self._append_level_suffix(c.get("uri", ""), level)results.append(MatchedContext(- uri=c.get("uri", ""),+ uri=display_uri,context_type=ContextType(c["context_type"])if c.get("context_type")else ContextType.RESOURCE,- level=c.get("level", 2),+ level=level,abstract=c.get("abstract", ""),category=c.get("category", ""),score=final_score,@@ -426,6 +434,20 @@ class HierarchicalRetriever:results.sort(key=lambda x: x.score, reverse=True)return results+ @classmethod+ def _append_level_suffix(cls, uri: str, level: int) -> str:+ """Return user-facing URI with L0/L1 suffix reconstructed by level."""+ suffix = cls.LEVEL_URI_SUFFIX.get(level)+ if not uri or not suffix:+ return uri+ if uri.endswith(f"/{suffix}"):+ return uri+ if uri.endswith("/.abstract.md") or uri.endswith("/.overview.md"):+ return uri+ if uri.endswith("/") and not uri.endswith("://"):+ uri = uri.rstrip("/")+ return f"{uri}/{suffix}"+def _get_root_uris_for_type(self, context_type: Optional[ContextType], ctx: Optional[RequestContext] = None) -> List[str]:diff --git a/openviking/storage/collection_schemas.py b/openviking/storage/collection_schemas.pyindex d095f3ba1..90c61d078 100644--- a/openviking/storage/collection_schemas.py+++ b/openviking/storage/collection_schemas.py@@ -146,6 +146,20 @@ class TextEmbeddingHandler(DequeueHandlerBase):"""Initialize the embedder instance from config."""self._embedder = config.embedding.get_embedder()+ @staticmethod+ def _seed_uri_for_id(uri: str, level: Any) -> str:+ """Build deterministic id seed URI from canonical uri + hierarchy level."""+ try:+ level_int = int(level)+ except (TypeError, ValueError):+ level_int = 2++ if level_int == 0:+ return uri if uri.endswith("/.abstract.md") else f"{uri}/.abstract.md"+ if level_int == 1:+ return uri if uri.endswith("/.overview.md") else f"{uri}/.overview.md"+ return uri+async def on_dequeue(self, data: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any]]:"""Process dequeued message and add embedding vector(s)."""if not data:@@ -205,11 +219,12 @@ class TextEmbeddingHandler(DequeueHandlerBase):# Write to vector databasetry:- # Ensure vector DB has at most one record per URI.+ # Ensure vector DB has deterministic IDs per semantic layer.uri = inserted_data.get("uri")if uri:account_id = inserted_data.get("account_id", "default")- id_seed = f"{account_id}:{uri}"+ seed_uri = self._seed_uri_for_id(uri, inserted_data.get("level", 2))+ id_seed = f"{account_id}:{seed_uri}"inserted_data["id"] = hashlib.md5(id_seed.encode("utf-8")).hexdigest()record_id = await self._vikingdb.upsert(inserted_data)diff --git a/openviking/storage/expr.py b/openviking/storage/expr.pyindex e19519bda..ef3aa8591 100644--- a/openviking/storage/expr.py+++ b/openviking/storage/expr.py@@ -58,4 +58,13 @@ class RawDSL:payload: Dict[str, Any]-FilterExpr = Union[And, Or, Eq, In, Range, Contains, TimeRange, RawDSL]+@dataclass(frozen=True)+class PathScope:+ """Path prefix scope expression with optional depth control."""++ field: str+ path: str+ depth: int = -1+++FilterExpr = Union[And, Or, Eq, In, Range, Contains, TimeRange, RawDSL, PathScope]diff --git a/openviking/storage/local_fs.py b/openviking/storage/local_fs.pyindex 900f5489e..0181c8739 100644--- a/openviking/storage/local_fs.py+++ b/openviking/storage/local_fs.py@@ -102,6 +102,7 @@ async def _enqueue_direct_vectorization(viking_fs, uri: str, ctx: RequestContextparent_uri=parent_uri,is_leaf=False,abstract=abstract,+ level=0,created_at=datetime.now(),active_count=0,related_uri=[],diff --git a/openviking/storage/queuefs/embedding_msg_converter.py b/openviking/storage/queuefs/embedding_msg_converter.pyindex d6f5b6483..b174b9411 100644--- a/openviking/storage/queuefs/embedding_msg_converter.py+++ b/openviking/storage/queuefs/embedding_msg_converter.py@@ -18,7 +18,7 @@ class EmbeddingMsgConverter:"""Converter for Context objects to EmbeddingMsg."""@staticmethod- def from_context(context: Context, **kwargs) -> EmbeddingMsg:+ def from_context(context: Context) -> EmbeddingMsg:"""Convert a Context object to EmbeddingMsg."""@@ -48,22 +48,28 @@ class EmbeddingMsgConverter:else:context_data["owner_space"] = ""- # Derive level field from URI for hierarchical retrieval.+ # Derive level field for hierarchical retrieval.uri = context_data.get("uri", "")- if uri.endswith("/.abstract.md"):- context_data["level"] = ContextLevel.ABSTRACT+ context_level = getattr(context, "level", None)+ if context_level is not None:+ resolved_level = context_level+ elif context_data.get("level") is not None:+ resolved_level = context_data.get("level")+ elif isinstance(context.meta, dict) and context.meta.get("level") is not None:+ resolved_level = context.meta.get("level")+ elif uri.endswith("/.abstract.md"):+ resolved_level = ContextLevel.ABSTRACTelif uri.endswith("/.overview.md"):- context_data["level"] = ContextLevel.OVERVIEW+ resolved_level = ContextLevel.OVERVIEWelse:- context_data["level"] = ContextLevel.DETAIL+ resolved_level = ContextLevel.DETAIL++ if isinstance(resolved_level, ContextLevel):+ resolved_level = int(resolved_level.value)+ context_data["level"] = int(resolved_level)embedding_msg = EmbeddingMsg(message=vectorization_text,context_data=context_data,)-- # Set any additional fields from kwargs- for key, value in kwargs.items():- if value is not None:- embedding_msg.context_data[key] = valuereturn embedding_msgdiff --git a/openviking/storage/queuefs/semantic_processor.py b/openviking/storage/queuefs/semantic_processor.pyindex 30514cbdc..7d62d7fe8 100644--- a/openviking/storage/queuefs/semantic_processor.py+++ b/openviking/storage/queuefs/semantic_processor.py@@ -520,46 +520,42 @@ class SemanticProcessor(DequeueHandlerBase):active_ctx = ctx or self._current_ctxqueue_manager = get_queue_manager()embedding_queue = queue_manager.get_queue(queue_manager.EMBEDDING)+ owner_space = self._owner_space_for_uri(uri, active_ctx)+ parent_uri = VikingURI(uri).parent.uri# Vectorize L0: .abstract.md (abstract), level=0- abstract_uri = f"{uri}/.abstract.md"context_abstract = Context(- uri=abstract_uri,- parent_uri=uri,+ uri=uri,+ parent_uri=parent_uri,is_leaf=False,abstract=abstract,context_type=context_type,+ level=0,user=active_ctx.user,account_id=active_ctx.account_id,- owner_space=self._owner_space_for_uri(uri, active_ctx),+ owner_space=owner_space,)context_abstract.set_vectorize(Vectorize(text=abstract))embedding_msg_abstract = EmbeddingMsgConverter.from_context(context_abstract)await embedding_queue.enqueue(embedding_msg_abstract) # type: ignore- logger.debug(f"Enqueued directory L0 (abstract) for vectorization: {abstract_uri}")+ logger.debug(f"Enqueued directory L0 (abstract) for vectorization: {uri}")# Vectorize L1: .overview.md (overview), level=1- overview_uri = f"{uri}/.overview.md"context_overview = Context(- uri=overview_uri,- parent_uri=uri,+ uri=uri,+ parent_uri=parent_uri,is_leaf=False,abstract=abstract,context_type=context_type,+ level=1,user=active_ctx.user,account_id=active_ctx.account_id,- owner_space=(- active_ctx.user.agent_space_name()- if uri.startswith("viking://agent/")- else active_ctx.user.user_space_name()- if uri.startswith("viking://user/") or uri.startswith("viking://session/")- else ""- ),+ owner_space=owner_space,)context_overview.set_vectorize(Vectorize(text=overview))embedding_msg_overview = EmbeddingMsgConverter.from_context(context_overview)await embedding_queue.enqueue(embedding_msg_overview) # type: ignore- logger.debug(f"Enqueued directory L1 (overview) for vectorization: {overview_uri}")+ logger.debug(f"Enqueued directory L1 (overview) for vectorization: {uri}")async def _vectorize_files(self,diff --git a/openviking/storage/vectordb/collection/volcengine_collection.py b/openviking/storage/vectordb/collection/volcengine_collection.pyindex 4acb94453..8ae7c0745 100644--- a/openviking/storage/vectordb/collection/volcengine_collection.py+++ b/openviking/storage/vectordb/collection/volcengine_collection.py@@ -121,6 +121,8 @@ class VolcengineCollection(ICollection):if not isinstance(v, str):return vs = v.strip()+ if s in {"/", "viking://"}:+ return "/"if s.startswith("viking://"):s = s[len("viking://") :]s = s.strip("/")diff --git a/openviking/storage/vectordb_adapters/base.py b/openviking/storage/vectordb_adapters/base.pyindex cee0f20f1..6afbf316f 100644--- a/openviking/storage/vectordb_adapters/base.py+++ b/openviking/storage/vectordb_adapters/base.py@@ -17,6 +17,7 @@ from openviking.storage.expr import (FilterExpr,In,Or,+ PathScope,Range,RawDSL,TimeRange,@@ -58,6 +59,7 @@ class CollectionAdapter(ABC):"""mode: str+ _URI_FIELD_NAMES = {"uri", "parent_uri"}def __init__(self, collection_name: str):self._collection_name = collection_name@@ -192,15 +194,75 @@ class CollectionAdapter(ABC):return index_metadef _normalize_record_for_read(self, record: Dict[str, Any]) -> Dict[str, Any]:- return record+ normalized = dict(record)+ for key in self._URI_FIELD_NAMES:+ if key in normalized:+ normalized[key] = self._decode_uri_field_value(normalized[key])+ return normalized++ def _normalize_record_for_write(self, record: Dict[str, Any]) -> Dict[str, Any]:+ normalized = dict(record)+ for key in self._URI_FIELD_NAMES:+ if key in normalized:+ normalized[key] = self._encode_uri_field_value(normalized[key])+ return normalized++ @staticmethod+ def _encode_uri_field_value(value: Any) -> Any:+ if not isinstance(value, str):+ return value+ stripped = value.strip()+ if not stripped.startswith("viking://"):+ return value+ suffix = stripped[len("viking://") :].strip("/")+ return f"/{suffix}" if suffix else "/"++ @staticmethod+ def _decode_uri_field_value(value: Any) -> Any:+ if not isinstance(value, str):+ return value+ stripped = value.strip()+ if stripped.startswith("viking://"):+ return stripped+ if not stripped.startswith("/"):+ return value+ suffix = stripped.strip("/")+ return f"viking://{suffix}" if suffix else "viking://"++ def _normalize_filter_payload_for_write(self, payload: Any) -> Any:+ if isinstance(payload, list):+ return [self._normalize_filter_payload_for_write(item) for item in payload]+ if not isinstance(payload, dict):+ return payload++ field_name = payload.get("field") if isinstance(payload.get("field"), str) else None+ normalized: Dict[str, Any] = {}+ for key, value in payload.items():+ if key in self._URI_FIELD_NAMES:+ normalized[key] = self._encode_uri_field_value(value)+ continue++ if key == "conds" and isinstance(value, list) and field_name in self._URI_FIELD_NAMES:+ normalized[key] = [+ self._encode_uri_field_value(item) if isinstance(item, str) else item+ for item in value+ ]+ continue++ if key == "prefix" and field_name in self._URI_FIELD_NAMES:+ normalized[key] = self._encode_uri_field_value(value)+ continue++ normalized[key] = self._normalize_filter_payload_for_write(value)+ return normalizeddef _compile_filter(self, expr: FilterExpr | Dict[str, Any] | None) -> Dict[str, Any]:if expr is None:return {}if isinstance(expr, dict):- return expr+ return self._normalize_filter_payload_for_write(expr)if isinstance(expr, RawDSL):- return expr.payload+ return self._normalize_filter_payload_for_write(expr.payload)if isinstance(expr, And):conds = [self._compile_filter(c) for c in expr.conds if c is not None]conds = [c for c in conds if c]@@ -218,9 +280,34 @@ class CollectionAdapter(ABC):return conds[0]return {"op": "or", "conds": conds}if isinstance(expr, Eq):- return {"op": "must", "field": expr.field, "conds": [expr.value]}+ value = (+ self._encode_uri_field_value(expr.value)+ if expr.field in self._URI_FIELD_NAMES+ else expr.value+ )+ payload = {"op": "must", "field": expr.field, "conds": [value]}+ if expr.field in self._URI_FIELD_NAMES:+ payload["para"] = "-d=0"+ return payloadif isinstance(expr, In):- return {"op": "must", "field": expr.field, "conds": list(expr.values)}+ values = (+ [self._encode_uri_field_value(v) for v in expr.values]+ if expr.field in self._URI_FIELD_NAMES+ else list(expr.values)+ )+ return {"op": "must", "field": expr.field, "conds": values}+ if isinstance(expr, PathScope):+ path = (+ self._encode_uri_field_value(expr.path)+ if expr.field in self._URI_FIELD_NAMES+ else expr.path+ )+ return {+ "op": "must",+ "field": expr.field,+ "conds": [path],+ "para": f"-d={expr.depth}",+ }if isinstance(expr, Range):payload: Dict[str, Any] = {"op": "range", "field": expr.field}if expr.gte is not None:@@ -287,7 +374,7 @@ class CollectionAdapter(ABC):normalized: list[Dict[str, Any]] = []ids: list[str] = []for item in records:- record = dict(item)+ record = self._normalize_record_for_write(item)record_id = record.get("id") or str(uuid.uuid4())record["id"] = record_idids.append(record_id)diff --git a/openviking/storage/vectordb_adapters/vikingdb_private_adapter.py b/openviking/storage/vectordb_adapters/vikingdb_private_adapter.pyindex fe15ad742..bb7ee1964 100644--- a/openviking/storage/vectordb_adapters/vikingdb_private_adapter.py+++ b/openviking/storage/vectordb_adapters/vikingdb_private_adapter.py@@ -112,10 +112,4 @@ class VikingDBPrivateCollectionAdapter(CollectionAdapter):return index_metadef _normalize_record_for_read(self, record: Dict[str, Any]) -> Dict[str, Any]:- for key in ("uri", "parent_uri"):- value = record.get(key)- if isinstance(value, str) and not value.startswith("viking://"):- stripped = value.strip("/")- if stripped:- record[key] = f"viking://{stripped}"- return record+ return super()._normalize_record_for_read(record)diff --git a/openviking/storage/vectordb_adapters/volcengine_adapter.py b/openviking/storage/vectordb_adapters/volcengine_adapter.pyindex 16d5ab08a..66ac0d8b6 100644--- a/openviking/storage/vectordb_adapters/volcengine_adapter.py+++ b/openviking/storage/vectordb_adapters/volcengine_adapter.py@@ -123,10 +123,4 @@ class VolcengineCollectionAdapter(CollectionAdapter):return index_metadef _normalize_record_for_read(self, record: Dict[str, Any]) -> Dict[str, Any]:- for key in ("uri", "parent_uri"):- value = record.get(key)- if isinstance(value, str) and not value.startswith("viking://"):- stripped = value.strip("/")- if stripped:- record[key] = f"viking://{stripped}"- return record+ return super()._normalize_record_for_read(record)diff --git a/openviking/storage/viking_fs.py b/openviking/storage/viking_fs.pyindex dc1fc0763..c18abc29c 100644--- a/openviking/storage/viking_fs.py+++ b/openviking/storage/viking_fs.py@@ -581,7 +581,7 @@ class VikingFS:if not self.rerank_config:raise RuntimeError("rerank_config is required for find")- if target_uri:+ if target_uri and target_uri not in {"/", "viking://"}:self._ensure_access(target_uri, ctx)storage = self._get_vector_store()@@ -667,7 +667,7 @@ class VikingFS:recent_messages = session_info.get("recent_messages") if session_info else Nonequery_plan: Optional[QueryPlan] = None- if target_uri:+ if target_uri and target_uri not in {"/", "viking://"}:self._ensure_access(target_uri, ctx)# When target_uri exists: read abstract, infer context_type@@ -711,7 +711,13 @@ class VikingFS:else:# No target_uri: query all typestyped_queries = [- TypedQuery(query=query, context_type=ctx_type, intent="", priority=1)+ TypedQuery(+ query=query,+ context_type=ctx_type,+ intent="",+ priority=1,+ target_directories=[target_uri] if target_uri else [],+ )for ctx_type in [ContextType.MEMORY, ContextType.RESOURCE, ContextType.SKILL]]@@ -917,6 +923,9 @@ class VikingFS:return Nonescope = parts[0]second = parts[1]+ # Treat scope-root metadata files as not having a tenant space segment.+ if len(parts) == 2 and second in {".abstract.md", ".overview.md"}:+ return Noneif scope == "user" and second not in self._USER_STRUCTURE_DIRS:return secondif scope == "agent" and second not in self._AGENT_STRUCTURE_DIRS:diff --git a/openviking/storage/viking_vector_index_backend.py b/openviking/storage/viking_vector_index_backend.pyindex 4c56024ec..c0627467b 100644--- a/openviking/storage/viking_vector_index_backend.py+++ b/openviking/storage/viking_vector_index_backend.py@@ -8,7 +8,7 @@ import uuidfrom typing import Any, Dict, List, Optionalfrom openviking.server.identity import RequestContext, Role-from openviking.storage.expr import And, Eq, FilterExpr, In, Or, RawDSL+from openviking.storage.expr import And, Eq, FilterExpr, In, Or, PathScope, RawDSLfrom openviking.storage.vectordb.collection.collection import Collectionfrom openviking.storage.vectordb.utils.logging_init import init_cpp_loggingfrom openviking.storage.vectordb_adapters import CollectionAdapter, create_collection_adapter@@ -363,7 +363,7 @@ class VikingVectorIndexBackend:limit: int = 10,) -> List[Dict[str, Any]]:merged_filter = self._merge_filters(- Eq("parent_uri", parent_uri),+ PathScope("uri", parent_uri, depth=1),self._build_scope_filter(ctx=ctx,context_type=context_type,@@ -407,11 +407,14 @@ class VikingVectorIndexBackend:account_id: str,uri: str,owner_space: Optional[str] = None,+ level: Optional[int] = None,limit: int = 1,) -> List[Dict[str, Any]]:- conds: List[FilterExpr] = [Eq("uri", uri), Eq("account_id", account_id)]+ conds: List[FilterExpr] = [PathScope("uri", uri, depth=0), Eq("account_id", account_id)]if owner_space:conds.append(Eq("owner_space", owner_space))+ if level is not None:+ conds.append(Eq("level", level))return await self.filter(filter=And(conds), limit=limit)async def delete_account_data(self, account_id: str) -> int:@@ -477,7 +480,11 @@ class VikingVectorIndexBackend:filters.append(tenant_filter)if target_directories:- uri_conds = [In("uri", [target_dir]) for target_dir in target_directories if target_dir]+ uri_conds = [+ PathScope("uri", target_dir, depth=-1)+ for target_dir in target_directories+ if target_dir+ ]if uri_conds:filters.append(Or(uri_conds))@@ -528,7 +535,16 @@ class VikingVectorIndexBackend:@staticmethoddef _merge_filters(*filters: Optional[FilterExpr]) -> Optional[FilterExpr]:- non_empty = [f for f in filters if f]+ non_empty = [+ f+ for f in filters+ if f+ and not (+ isinstance(f, RawDSL)+ and f.payload.get("op") == "and"+ and not f.payload.get("conds")+ )+ ]if not non_empty:return Noneif len(non_empty) == 1:diff --git a/openviking/utils/skill_processor.py b/openviking/utils/skill_processor.pyindex 2b9310d75..5f044ce80 100644--- a/openviking/utils/skill_processor.py+++ b/openviking/utils/skill_processor.py@@ -219,8 +219,13 @@ class SkillProcessor:await viking_fs.write_file_bytes(aux_uri, file_bytes, ctx=ctx)async def _index_skill(self, context: Context, skill_dir_uri: str):- """Write skill to vector store via async queue."""+ """Write skill directory vector via async queue as L0."""context.uri = skill_dir_uri+ context.parent_uri = "viking://agent/skills"+ context.is_leaf = False+ context.level = 0+ context.set_vectorize(Vectorize(text=context.abstract))embedding_msg = EmbeddingMsgConverter.from_context(context)- await self.vikingdb.enqueue_embedding_msg(embedding_msg)+ if embedding_msg:+ await self.vikingdb.enqueue_embedding_msg(embedding_msg)