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(pathlock): stop locking missing targets in read/delete/cleanup paths (#5027)
Zayn Jarvis24118fe2c18b
diff --git a/crates/ragfs/src/lock/wrapper.rs b/crates/ragfs/src/lock/wrapper.rsindex 4bd9653ae..1afd56ce6 100644--- a/crates/ragfs/src/lock/wrapper.rs+++ b/crates/ragfs/src/lock/wrapper.rs@@ -40,6 +40,19 @@ impl PathLockWrappedFS {&self.inner}+ /// Return true when `path` does not exist.+ ///+ /// Deleting a missing path has nothing to protect, and acquiring a lock+ /// there would create the path (or its ancestors) just to hold lock+ /// metadata. Return not-found without delegating a delete: the target+ /// could be created by another writer after this check.+ async fn is_missing(&self, path: &str) -> bool {+ matches!(+ self.inner.stat(path).await,+ Err(crate::core::Error::NotFound(_))+ )+ }+/// Return true for virtual control paths and lock metadata that must not be auto-locked.fn should_bypass_auto_lock(path: &str) -> bool {path == "/queue"@@ -133,6 +146,9 @@ impl FileSystem for PathLockWrappedFS {if Self::should_bypass_auto_lock(path) {return self.inner.remove(path).await;}+ if self.is_missing(path).await {+ return Err(crate::core::Error::NotFound(path.to_string()));+ }let requests = [PathLockRequest {path: path.to_string(),kind: PathLockKind::Exact,@@ -154,6 +170,9 @@ impl FileSystem for PathLockWrappedFS {if Self::should_bypass_auto_lock(path) {return self.inner.remove_all(path).await;}+ if self.is_missing(path).await {+ return Err(crate::core::Error::NotFound(path.to_string()));+ }let requests = [PathLockRequest {path: path.to_string(),kind: PathLockKind::Tree,@@ -394,3 +413,146 @@ impl FileSystem for PathLockWrappedFS {self.inner.ensure_parent_dirs(path, mode).await}}++#[cfg(test)]+mod tests {+ use super::super::manager::PathLockConfig;+ use super::*;+ use crate::core::Error;+ use crate::lock::provider::MemoryPathLockProvider;+ use crate::plugins::memfs::MemFileSystem;++ /// Missing at stat time; any subsequent mutation would be a bug.+ struct MissingFS;++ #[async_trait]+ impl FileSystem for MissingFS {+ async fn stat(&self, path: &str) -> crate::core::Result<FileInfo> {+ Err(Error::NotFound(path.to_string()))+ }++ async fn remove(&self, _path: &str) -> crate::core::Result<()> {+ panic!("must not delete after stat returned NotFound")+ }++ async fn remove_all(&self, _path: &str) -> crate::core::Result<()> {+ panic!("must not delete recursively after stat returned NotFound")+ }++ async fn create(&self, _path: &str) -> crate::core::Result<()> {+ panic!("must not create lock metadata")+ }++ async fn mkdir(&self, _path: &str, _mode: u32) -> crate::core::Result<()> {+ panic!("must not create a lock directory")+ }++ async fn read(+ &self,+ _path: &str,+ _offset: u64,+ _size: u64,+ ) -> crate::core::Result<Vec<u8>> {+ unreachable!()+ }++ async fn write(+ &self,+ _path: &str,+ _data: &[u8],+ _offset: u64,+ _flags: WriteFlag,+ ) -> crate::core::Result<u64> {+ panic!("must not write lock metadata")+ }++ async fn read_dir(+ &self,+ _path: &str,+ _offset: Option<usize>,+ _limit: Option<usize>,+ _sort_by: Option<ListSortBy>,+ _sort_order: Option<SortOrder>,+ ) -> crate::core::Result<Vec<FileInfo>> {+ unreachable!()+ }++ async fn rename(&self, _old_path: &str, _new_path: &str) -> crate::core::Result<()> {+ unreachable!()+ }++ async fn chmod(&self, _path: &str, _mode: u32) -> crate::core::Result<()> {+ unreachable!()+ }+ }++ #[tokio::test]+ async fn missing_deletes_return_without_delegating() {+ let fs: Arc<dyn FileSystem> = Arc::new(MissingFS);+ let manager = Arc::new(PathLockManager::new(+ fs.clone(),+ Arc::new(MemoryPathLockProvider::new()),+ PathLockConfig::default(),+ ));+ let wrapped = PathLockWrappedFS::new(manager, fs);+ for recursive in [false, true] {+ let result = if recursive {+ wrapped.remove_all("/data/gone").await+ } else {+ wrapped.remove("/data/gone").await+ };+ assert!(matches!(result, Err(Error::NotFound(path)) if path == "/data/gone"));+ }+ }++ async fn wrapped() -> (PathLockWrappedFS, Arc<MemFileSystem>) {+ let fs = Arc::new(MemFileSystem::new());+ fs.mkdir("/data", 0o755).await.unwrap();+ let manager = Arc::new(PathLockManager::new(+ fs.clone() as Arc<dyn FileSystem>,+ Arc::new(MemoryPathLockProvider::new()),+ PathLockConfig::default(),+ ));+ (+ PathLockWrappedFS::new(manager, fs.clone() as Arc<dyn FileSystem>),+ fs,+ )+ }++ #[tokio::test]+ async fn remove_missing_path_does_not_create_ancestors() {+ let (wrapped, fs) = wrapped().await;+ assert!(matches!(+ wrapped.remove("/data/gone/file.md").await,+ Err(Error::NotFound(_))+ ));+ assert!(matches!(+ fs.stat("/data/gone").await,+ Err(Error::NotFound(_))+ ));+ }++ #[tokio::test]+ async fn remove_all_missing_path_does_not_create_ancestors() {+ let (wrapped, fs) = wrapped().await;+ assert!(matches!(+ wrapped.remove_all("/data/gone/sub").await,+ Err(Error::NotFound(_))+ ));+ assert!(matches!(+ fs.stat("/data/gone").await,+ Err(Error::NotFound(_))+ ));+ }++ #[tokio::test]+ async fn remove_existing_path_still_locks_and_deletes() {+ let (wrapped, fs) = wrapped().await;+ fs.create("/data/a.md").await.unwrap();+ wrapped.remove("/data/a.md").await.unwrap();+ assert!(matches!(+ fs.stat("/data/a.md").await,+ Err(Error::NotFound(_))+ ));+ }+}diff --git a/openviking/service/fs_service.py b/openviking/service/fs_service.pyindex ee5b46940..3a507b2aa 100644--- a/openviking/service/fs_service.py+++ b/openviking/service/fs_service.py@@ -394,6 +394,11 @@ class FSService:memory_overview_uri = self._memory_overview_parent_uri(uri, context_type)result = await viking_fs.rm(uri, recursive=recursive, ctx=ctx)await self._sync_watch_after_rm(uri, account_id=ctx.account_id, context_type=context_type)+ # A refresh on a parent that no longer exists would lock its sidecar+ # paths and thereby recreate the deleted directory. Nothing to+ # summarize there; skip it.+ if refresh_parent_uri and not await viking_fs.exists(refresh_parent_uri, ctx=ctx):+ refresh_parent_uri = Nonequeue_status = Nonerefresh_action: Optional[FreshnessAction] = Nonerequest_registered = False@@ -1174,9 +1179,7 @@ class FSService:ctx: RequestContext,acl_mode: Optional[AclMode] = None,) -> Dict[str, Any]:- return await self._ensure_initialized().set_acl(- uri, entries, ctx=ctx, acl_mode=acl_mode- )+ return await self._ensure_initialized().set_acl(uri, entries, ctx=ctx, acl_mode=acl_mode)async def grant_acl(self, uri: str, principal: str, level: str, ctx: RequestContextdiff --git a/openviking/service/reindex_executor.py b/openviking/service/reindex_executor.pyindex 4bd6933b2..ef7a2b6fa 100644--- a/openviking/service/reindex_executor.py+++ b/openviking/service/reindex_executor.py@@ -31,6 +31,7 @@ from openviking.service.task_tracker import get_task_trackerfrom openviking.service.task_work_index import bind_task_contextfrom openviking.session.memory.utils.memory_file_utils import MemoryFileUtilsfrom openviking.storage.abstract_overview import body_for_preview, embedding_text_for_body+from openviking.storage.errors import ResourceBusyErrorfrom openviking.storage.expr import And, Eq, Or, PathScopefrom openviking.storage.queuefs.embedding_msg_converter import EmbeddingMsgConverterfrom openviking.storage.queuefs.semantic_msg import SemanticMsg@@ -508,14 +509,25 @@ class ReindexExecutor:if telemetry_id:wait_tracker.register_request(telemetry_id)- acquire_lock = service.viking_fs._async_agfs.pathlock_acquire_tree+ # prune_orphans only touches the vector store. A missing target has+ # nothing on disk to protect, and acquiring a lock there would create+ # the directory just to hold lock metadata. Refuse only when another+ # owner is mid-write at that name (e.g. add_resource reserving it).+ lease = Noneif mode != "prune_orphans" or await service.viking_fs.exists(uri, ctx=ctx):+ acquire_lock = service.viking_fs._async_agfs.pathlock_acquire_treestat = await service.viking_fs.stat(uri, ctx=ctx, skip_count=True)if not stat.get("isDir", stat.get("is_dir")):acquire_lock = service.viking_fs._async_agfs.pathlock_acquire_exact- lease = await acquire_lock(path)+ lease = await acquire_lock(path)+ elif await service.viking_fs._async_agfs.pathlock_is_locked(path):+ raise ResourceBusyError(f"Resource is being processed: {uri}", uri=uri)try:- borrowed = await service.viking_fs._async_agfs.pathlock_as_borrowed(lease)+ borrowed = (+ await service.viking_fs._async_agfs.pathlock_as_borrowed(lease)+ if lease is not None+ else None+ )run = _ReindexRunContext(ctx=ctx,counters=counters,@@ -577,7 +589,8 @@ class ReindexExecutor:wait_tracker.build_queue_status(telemetry_id),)finally:- await service.viking_fs._async_agfs.pathlock_release(lease)+ if lease is not None:+ await service.viking_fs._async_agfs.pathlock_release(lease)if telemetry_id:wait_tracker.cleanup(telemetry_id)diff --git a/openviking/storage/abstract_overview.py b/openviking/storage/abstract_overview.pyindex cdc823d8f..46e663e5f 100644--- a/openviking/storage/abstract_overview.py+++ b/openviking/storage/abstract_overview.py@@ -669,19 +669,28 @@ async def read_abstract_overview_pending_snapshot(f"{dir_uri.rstrip('/')}/.overview.md",f"{dir_uri.rstrip('/')}/.abstract.md",]++ def _pending_of(documents: Sequence[Optional[AbstractOverviewDocument]]) -> int:+ pending_values = []+ for document in documents:+ freshness = document.metadata.get("freshness") if document else None+ if document is not None and not document.legacy and isinstance(freshness, Mapping):+ pending_values.append(int(freshness["pending_child_changes"]))+ return max(pending_values, default=0)+owns_lease = lock is Nonesnapshot_lease = lockif owns_lease:+ # Read-only: when neither sidecar exists there is no counter to+ # snapshot, and acquiring exact locks there would create the parent+ # directory just to hold lock metadata (a deleted directory reappears).+ preliminary = [await _read_existing_document(viking_fs, uri, ctx) for uri in uris]+ if all(document is None for document in preliminary):+ return 0lock_paths = [viking_fs._uri_to_path(uri, ctx=ctx) for uri in uris]snapshot_lease = await viking_fs._async_agfs.pathlock_acquire_exact_batch(lock_paths)try:- pending_values = []- for uri in uris:- document = await _read_existing_document(viking_fs, uri, ctx)- freshness = document.metadata.get("freshness") if document else None- if document is not None and not document.legacy and isinstance(freshness, Mapping):- pending_values.append(int(freshness["pending_child_changes"]))- return max(pending_values, default=0)+ return _pending_of([await _read_existing_document(viking_fs, uri, ctx) for uri in uris])finally:if owns_lease:await viking_fs._async_agfs.pathlock_release(snapshot_lease)diff --git a/openviking/storage/queuefs/semantic_processor.py b/openviking/storage/queuefs/semantic_processor.pyindex d459c8da7..b0099acd7 100644--- a/openviking/storage/queuefs/semantic_processor.py+++ b/openviking/storage/queuefs/semantic_processor.py@@ -406,6 +406,15 @@ class SemanticProcessor(DequeueHandlerBase):logger.info(f"Processing semantic generation for: {msg})")+ # Resolving the lock scope for a root that was deleted+ # after enqueue would recreate it (lock metadata needs a+ # parent directory). There is nothing to summarize; ack.+ if not await get_viking_fs().exists(msg.uri, ctx=current_ctx):+ logger.info("Skipping semantic message for missing root: uri=%s", msg.uri)+ if msg.telemetry_id and msg.id:+ get_request_wait_tracker().mark_semantic_done(msg.telemetry_id, msg.id)+ return ProcessResult.success()+semantic_lock = await SemanticLockScope.resolve(msg.lock_handoff,caller_lock=lock,diff --git a/openviking/storage/viking_fs/_access.py b/openviking/storage/viking_fs/_access.pyindex b16103156..dda787a8b 100644--- a/openviking/storage/viking_fs/_access.py+++ b/openviking/storage/viking_fs/_access.py@@ -281,13 +281,30 @@ class _AccessMixin:return real_ctxraise PermissionDeniedError(f"ACL management denied for {uri}", resource=uri)- async def _ensure_acl_target_exists(self, uri: str, ctx: RequestContext) -> None:+ async def _ensure_acl_target_exists(self, uri: str, ctx: RequestContext) -> bool:+ """Return whether the ACL target is a directory; raise if it is missing."""try:- await self._async_agfs.stat(self._uri_to_path(uri, ctx=ctx))+ stat = await self._async_agfs.stat(self._uri_to_path(uri, ctx=ctx))except Exception as exc:if is_not_found_error(exc):raise NotFoundError(uri, "resource") from excraise+ return bool(stat.get("isDir", False)) if isinstance(stat, dict) else False++ async def _acquire_acl_target_lock(self, uri: str, ctx: RequestContext) -> Dict[str, Any]:+ """Lock an existing ACL target: Exact for a file, Tree for a directory.++ The existence check runs before the lock so a missing target returns+ NotFound instead of materializing a directory for lock metadata.+ """+ is_dir = await self._ensure_acl_target_exists(uri, ctx)+ path = self._uri_to_path(uri, ctx=ctx)+ acquire = (+ self._async_agfs.pathlock_acquire_tree+ if is_dir+ else self._async_agfs.pathlock_acquire_exact+ )+ return await acquire(path)async def get_acl(self, uri: str, ctx: Optional[RequestContext] = None) -> Dict[str, Any]:real_ctx = await self._ensure_acl_manage(uri, ctx)@@ -304,8 +321,7 @@ class _AccessMixin:acl_mode: AclMode | None = None,) -> Dict[str, Any]:real_ctx = await self._ensure_acl_manage(uri, ctx)- path = self._uri_to_path(uri, ctx=real_ctx)- lease = await self._async_agfs.pathlock_acquire_tree(path)+ lease = await self._acquire_acl_target_lock(uri, real_ctx)try:await self._ensure_acl_manage(uri, real_ctx)await self._ensure_acl_target_exists(uri, real_ctx)@@ -341,8 +357,7 @@ class _AccessMixin:principal = normalize_acl_principal(principal)normalized_level = normalize_acl_level(level) if level is not None else Nonereal_ctx = await self._ensure_acl_manage(uri, ctx)- path = self._uri_to_path(uri, ctx=real_ctx)- lease = await self._async_agfs.pathlock_acquire_tree(path)+ lease = await self._acquire_acl_target_lock(uri, real_ctx)try:await self._ensure_acl_manage(uri, real_ctx)await self._ensure_acl_target_exists(uri, real_ctx)diff --git a/openviking/storage/viking_fs/_snapshot.py b/openviking/storage/viking_fs/_snapshot.pyindex 6cfb60273..77d052626 100644--- a/openviking/storage/viking_fs/_snapshot.py+++ b/openviking/storage/viking_fs/_snapshot.py@@ -31,6 +31,7 @@ from openviking_cli.exceptions import (def _pkg():return sys.modules[__package__]+class _SnapshotMixin:"""Snapshot/git-like version control (commit/restore/show/diff/log)."""@@ -95,8 +96,7 @@ class _SnapshotMixin:write_targets.append(uri)delete_targets = [- self._tree_path_to_uri(f"{tree_dir}/{path}".strip("/"))- for path in diff["to_delete"]+ self._tree_path_to_uri(f"{tree_dir}/{path}".strip("/")) for path in diff["to_delete"]]if write_targets:await self._ensure_access_many(@@ -272,7 +272,9 @@ class _SnapshotMixin:{self._uri_to_path(uri, ctx=real_ctx) for uri in paths},key=lambda value: (value.count("/"), value),):- if not any(path == root or path.startswith(f"{root.rstrip('/')}/") for root in lock_paths):+ if not any(+ path == root or path.startswith(f"{root.rstrip('/')}/") for root in lock_paths+ ):lock_paths.append(path)try:lease = await self._async_agfs.pathlock_acquire_tree_batch(lock_paths)@@ -370,6 +372,14 @@ class _SnapshotMixin:# trusted internal callers. Keep its existing dry-run path unchanged.if dry_run and real_ctx.role == Role.ROOT:return await self._async_agfs.run("git_restore", **kwargs)+ # A dry run only computes a plan; it must not take the tree lock,+ # which would recreate a deleted project_dir to hold lock metadata.+ if dry_run:+ assert acl_project_dir is not None+ await self._ensure_access(acl_project_dir, real_ctx)+ plan = await self._async_agfs.run("git_restore", **kwargs)+ await self._ensure_restore_plan_access(plan, tree_dir=tree_dir or "", ctx=real_ctx)+ return planfrom openviking.pyagfs.exceptions import GitRestoreWritebackPartialErrorfrom openviking.storage.errors import LockAcquisitionError, ResourceBusyError@@ -406,8 +416,6 @@ class _SnapshotMixin:tree_dir=tree_dir or "",ctx=real_ctx,)- if dry_run:- return plantry:result = await self._async_agfs.run("git_restore", **kwargs)except GitRestoreWritebackPartialError as exc:diff --git a/tests/agfs/test_viking_fs_git.py b/tests/agfs/test_viking_fs_git.pyindex fd27face0..e0edb7674 100644--- a/tests/agfs/test_viking_fs_git.py+++ b/tests/agfs/test_viking_fs_git.py@@ -124,8 +124,7 @@ class TestUriToTreePath:def test_session_uri(self, vfs):ctx = _make_ctx()assert (- vfs._uri_to_tree_path("viking://user/user1/sessions", ctx=ctx)- == "user/user1/sessions"+ vfs._uri_to_tree_path("viking://user/user1/sessions", ctx=ctx) == "user/user1/sessions")def test_trailing_slash_kept_as_directory(self, vfs):@@ -1107,3 +1106,43 @@ async def test_vikingfs_get_gitignore_maps_non_utf8_to_invalid_operation(vfs):with pytest.raises(AGFSInvalidOperationError) as excinfo:await vfs.get_gitignore(ctx=ctx)assert "utf-8" in str(excinfo.value).lower()+++async def test_restore_dry_run_for_user_does_not_take_tree_lock(vfs, monkeypatch):+ """A dry run only plans; locking would recreate a deleted project_dir."""+ from openviking.server.identity import Role++ root = _make_ctx(account="acct_dry_user")+ user = RequestContext(user=UserIdentifier("acct_dry_user", "user1"), role=Role.USER)+ await vfs.write_file("viking://resources/proj/a.md", b"v1", ctx=root)+ c1 = await vfs.commit(message="v1", paths=["viking://resources/proj/a.md"], ctx=root)+ await vfs.write_file("viking://resources/proj/a.md", b"v2", ctx=root)+ await vfs.commit(message="v2", paths=["viking://resources/proj/a.md"], ctx=root)++ tree_locks = []+ orig_tree = vfs._async_agfs.pathlock_acquire_tree++ async def spy_tree(path, *args, **kwargs):+ tree_locks.append(path)+ return await orig_tree(path, *args, **kwargs)++ monkeypatch.setattr(vfs._async_agfs, "pathlock_acquire_tree", spy_tree)++ result = await vfs.restore(+ project_dir="viking://resources/proj",+ source_commit=c1["commit_oid"],+ dry_run=True,+ ctx=user,+ )+ assert result["result"] == "dry_run"+ assert any(item["path"] == "a.md" for item in result["diff"]["to_write"])+ assert tree_locks == []++ applied = await vfs.restore(+ project_dir="viking://resources/proj",+ source_commit=c1["commit_oid"],+ ctx=user,+ )+ assert applied["result"] == "applied"+ assert tree_locks == ["/local/acct_dry_user/resources/proj"]diff --git a/tests/server/test_admin_rebuild_api.py b/tests/server/test_admin_rebuild_api.pyindex b69be2a4b..1f7f083fe 100644--- a/tests/server/test_admin_rebuild_api.py+++ b/tests/server/test_admin_rebuild_api.py@@ -571,26 +571,32 @@ async def test_reindex_prune_existing_file_uses_exact_lock(monkeypatch):@pytest.mark.asyncio-async def test_reindex_prune_missing_target_keeps_tree_lock(monkeypatch):[email protected]("locked", [False, True])+async def test_reindex_prune_missing_target_does_not_lock(monkeypatch, locked):+ """A missing prune target must not acquire a lock: doing so creates the directory."""from types import SimpleNamespacefrom openviking.service.reindex_executor import ReindexExecutor+ from openviking.storage.errors import ResourceBusyErrorcalls = []class FakeAGFS:async def pathlock_acquire_exact(self, path):- raise AssertionError("missing target must retain tree lock scope")+ raise AssertionError("missing target must not acquire a lock")async def pathlock_acquire_tree(self, path):- calls.append(("tree", path))- return {"id": "lease-1"}+ raise AssertionError("missing target must not acquire a lock")++ async def pathlock_is_locked(self, path):+ calls.append(("is_locked", path))+ return lockedasync def pathlock_as_borrowed(self, lease):- return lease+ raise AssertionError("no lease to borrow")async def pathlock_release(self, lease):- calls.append(("release", lease["id"]))+ raise AssertionError("no lease to release")class FakeFS:_async_agfs = FakeAGFS()@@ -619,19 +625,19 @@ async def test_reindex_prune_missing_target_keeps_tree_lock(monkeypatch):role=Role.ROOT,)- result = await executor._run(+ run = executor._run(uri="viking://resources/missing",object_type="resource",mode="prune_orphans",dry_run=True,ctx=ctx,)-- assert result["status"] == "completed"- assert calls == [- ("tree", "/local/default/resources/missing"),- ("release", "lease-1"),- ]+ if locked:+ with pytest.raises(ResourceBusyError):+ await run+ else:+ assert (await run)["status"] == "completed"+ assert calls == [("is_locked", "/local/default/resources/missing")]@pytest.mark.asynciodiff --git a/tests/service/test_fs_service.py b/tests/service/test_fs_service.pyindex afb0db3e3..b3edf547a 100644--- a/tests/service/test_fs_service.py+++ b/tests/service/test_fs_service.py@@ -15,12 +15,16 @@ from openviking_cli.session.user_id import UserIdentifierclass _FakeVikingFS:- def __init__(self, *, rm_error=None, events=None):+ def __init__(self, *, rm_error=None, events=None, parent_exists=True):self.rm_calls = []self.mv_calls = []self.cp_calls = []self.rm_error = rm_errorself.events = events+ self.parent_exists = parent_exists++ async def exists(self, uri, ctx=None):+ return self.parent_existsasync def rm(self, uri, recursive=False, ctx=None):self.rm_calls.append({"uri": uri, "recursive": recursive, "ctx": ctx})@@ -754,6 +758,21 @@ async def test_resource_rm_reports_failed_semantic_status_when_wait_queue_has_erassert result["semantic_status"] == "failed"+async def test_resource_rm_skips_parent_refresh_when_parent_is_gone(request_context):+ """Refreshing a deleted parent would lock its sidecars and recreate it."""+ viking_fs = _FakeVikingFS(parent_exists=False)+ service = FSService(viking_fs=viking_fs)+ service._enqueue_delete_refresh = AsyncMock()++ uri = "viking://resources/deleted/sub/a.md"+ result = await service.rm(uri, ctx=request_context, wait=True)++ assert viking_fs.rm_calls == [{"uri": uri, "recursive": False, "ctx": request_context}]+ service._enqueue_delete_refresh.assert_not_awaited()+ assert "semantic_root_uri" not in result++@pytest.mark.asyncioasync def test_resource_rm_without_wait_only_queues_refresh(request_context):viking_fs = _FakeVikingFS()diff --git a/tests/storage/test_abstract_overview_freshness.py b/tests/storage/test_abstract_overview_freshness.pyindex 70a0d5c6e..6c1052550 100644--- a/tests/storage/test_abstract_overview_freshness.py+++ b/tests/storage/test_abstract_overview_freshness.py@@ -10,6 +10,7 @@ from openviking.storage.abstract_overview import (freshness_metadata,parse_abstract_overview,plan_abstract_overview_refresh,+ read_abstract_overview_pending_snapshot,render_abstract_overview,write_abstract_overview,)@@ -83,9 +84,7 @@ async def test_pending_increment_and_threshold_decision_share_one_snapshot():assert second.action is FreshnessAction.REFRESH_NOWassert fs.lock_timeouts == [1.0, 0.0]for raw in fs.files.values():- assert parse_abstract_overview(raw).metadata["freshness"][- "pending_child_changes"- ] == 17+ assert parse_abstract_overview(raw).metadata["freshness"]["pending_child_changes"] == 17@pytest.mark.asyncio@@ -132,9 +131,7 @@ async def test_concurrent_pending_marks_do_not_overwrite_each_other():)for raw in fs.files.values():- assert parse_abstract_overview(raw).metadata["freshness"][- "pending_child_changes"- ] == 13+ assert parse_abstract_overview(raw).metadata["freshness"]["pending_child_changes"] == 13@pytest.mark.asyncio@@ -155,3 +152,26 @@ async def test_partial_sidecar_baseline_refreshes_immediately():assert decision.action is FreshnessAction.REFRESH_NOWassert decision.pending_after == 1+++async def test_pending_snapshot_of_missing_directory_does_not_lock():+ """Locking sidecars of a deleted directory would recreate it; return 0 instead."""+ fs = _FakeFS({})+ assert (+ await read_abstract_overview_pending_snapshot(+ viking_fs=fs, dir_uri="viking://resources/gone", ctx=None+ )+ == 0+ )+ assert fs.lock_timeouts == []+++async def test_pending_snapshot_reads_existing_sidecars_under_lock():+ dir_uri = "viking://resources/present"+ fs = _FakeFS(_files(dir_uri, pending=3))+ assert (+ await read_abstract_overview_pending_snapshot(viking_fs=fs, dir_uri=dir_uri, ctx=None) == 3+ )+ assert fs.lock_timeouts == [0.0]diff --git a/tests/storage/test_memory_semantic_stall.py b/tests/storage/test_memory_semantic_stall.pyindex 7267e1bf8..7c91668ad 100644--- a/tests/storage/test_memory_semantic_stall.py+++ b/tests/storage/test_memory_semantic_stall.py@@ -61,6 +61,7 @@ async def test_memory_empty_dir_still_returns_success():processor = SemanticProcessor()fake_fs = MagicMock()+ fake_fs.exists = AsyncMock(return_value=True)fake_fs.ls = AsyncMock(return_value=[])msg = _make_msg()@@ -93,6 +94,7 @@ async def test_memory_ls_error_returns_failed():processor = SemanticProcessor()fake_fs = MagicMock()+ fake_fs.exists = AsyncMock(return_value=True)fake_fs.ls = AsyncMock(side_effect=FileNotFoundError("/memories not found"))msg = _make_msg()@@ -127,6 +129,7 @@ async def test_memory_ls_transient_error_requeues():processor = SemanticProcessor()fake_fs = MagicMock()+ fake_fs.exists = AsyncMock(return_value=True)fake_fs.ls = AsyncMock(side_effect=RuntimeError("500 Internal Server Error"))msg = _make_msg(telemetry_id="tel-1")@@ -163,6 +166,7 @@ async def test_memory_write_error_returns_failed():processor = SemanticProcessor()fake_fs = MagicMock()+ fake_fs.exists = AsyncMock(return_value=True)fake_fs.ls = AsyncMock(return_value=[{"name": "file1.md", "isDir": False}])fake_fs.read_file = AsyncMock(return_value="some content")fake_fs.write_file = AsyncMock(side_effect=PermissionError("Permission denied"))diff --git a/tests/storage/test_semantic_dag_stats.py b/tests/storage/test_semantic_dag_stats.pyindex fca2b6ae9..4be498fb0 100644--- a/tests/storage/test_semantic_dag_stats.py+++ b/tests/storage/test_semantic_dag_stats.py@@ -7,6 +7,7 @@ from unittest.mock import AsyncMockimport pytest+from openviking.core.context import ContextLevelfrom openviking.server.identity import RequestContext, Rolefrom openviking.service.task_queue_middleware import TaskWorkQueueMiddlewarefrom openviking.service.task_work_index import (@@ -15,7 +16,11 @@ from openviking.service.task_work_index import (bind_task_context,get_task_context,)-from openviking.storage.abstract_overview import parse_abstract_overview+from openviking.storage.abstract_overview import (+ freshness_metadata,+ parse_abstract_overview,+ render_abstract_overview,+)from openviking.storage.errors import LockAcquisitionErrorfrom openviking.storage.queuefs.named_queue import NamedQueuefrom openviking.storage.queuefs.semantic_dag import (@@ -333,6 +338,18 @@ async def test_busy_parent_snapshot_preserves_changed_file_work(monkeypatch):root_uri: [{"name": f"file-{idx:03}.txt", "isDir": False} for idx in range(40)],}fake_fs = _FakeVikingFS(tree)+ # The pending-counter read only locks when a sidecar exists; provide one+ # so the busy lock is actually exercised.+ sidecar = render_abstract_overview(+ ContextLevel.ABSTRACT, root_uri, "abstract", {"freshness": freshness_metadata(40, 4, 0)}+ )++ async def read_file(uri, ctx=None):+ if uri == f"{root_uri}/.abstract.md":+ return sidecar+ raise FileNotFoundError(uri)++ monkeypatch.setattr(fake_fs, "read_file", read_file, raising=False)monkeypatch.setattr("openviking.storage.queuefs.semantic_dag.get_viking_fs", lambda: fake_fs)_patch_semantic_config(monkeypatch, overview_sample_limit=4)monkeypatch.setattr(diff --git a/tests/storage/test_semantic_processor_lock_ownership.py b/tests/storage/test_semantic_processor_lock_ownership.pyindex 09d3f5b3c..6c438744f 100644--- a/tests/storage/test_semantic_processor_lock_ownership.py+++ b/tests/storage/test_semantic_processor_lock_ownership.py@@ -57,3 +57,34 @@ async def test_memory_semantic_directory_does_not_release_borrowed_lock(monkeypa)assert pathlock.release_calls == []+++async def test_missing_root_is_acked_before_lock_scope_is_resolved(monkeypatch):+ """Resolving the lock for a deleted root would recreate it to hold lock metadata."""+ processor = SemanticProcessor()++ monkeypatch.setattr(+ "openviking.storage.queuefs.semantic_processor.get_viking_fs",+ lambda: _FakeVikingFS(),+ )++ async def fail_resolve(*args, **kwargs):+ raise AssertionError("lock scope must not be resolved for a missing root")++ monkeypatch.setattr(+ "openviking.storage.queuefs.semantic_processor.SemanticLockScope.resolve",+ fail_resolve,+ )++ msg = SemanticMsg(+ uri="viking://resources/deleted-parent",+ context_type="resource",+ recursive=False,+ account_id="default",+ user_id="alice",+ changes={"deleted": ["viking://resources/deleted-parent/a.md"]},+ generation_trigger="content_delete",+ )+ result = await processor.on_dequeue(msg.to_dict())+ assert result.successdiff --git a/tests/storage/test_viking_fs_acl_locking.py b/tests/storage/test_viking_fs_acl_locking.pynew file mode 100644index 000000000..1275e4643--- /dev/null+++ b/tests/storage/test_viking_fs_acl_locking.py@@ -0,0 +1,82 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0++from types import SimpleNamespace+from unittest.mock import AsyncMock++import pytest++from openviking.server.identity import RequestContext, Role+from openviking.storage.viking_fs import VikingFS+from openviking_cli.exceptions import NotFoundError+from openviking_cli.session.user_id import UserIdentifier+++class _AclAGFS:+ """Async AGFS stub that records stat/lock ordering for ACL updates."""++ def __init__(self, is_dir):+ self.is_dir = is_dir+ self.events = []++ async def stat(self, path):+ self.events.append(("stat", path))+ if self.is_dir is None:+ raise FileNotFoundError(path)+ return {"isDir": self.is_dir}++ async def pathlock_acquire_exact(self, path):+ self.events.append(("exact", path))+ return {"lease_ref": "exact"}++ async def pathlock_acquire_tree(self, path):+ self.events.append(("tree", path))+ return {"lease_ref": "tree"}++ async def pathlock_release(self, lease):+ self.events.append(("release", lease["lease_ref"]))+++def _fs(agfs):+ fs = VikingFS(agfs=SimpleNamespace())+ fs._async_agfs = agfs # type: ignore[assignment]+ ctx = RequestContext(user=UserIdentifier("default", "alice"), role=Role.ADMIN)+ fs._ensure_acl_manage = AsyncMock(return_value=ctx) # type: ignore[method-assign]+ fs._uri_to_path = lambda uri, **_: "/local/default/resources/target" # type: ignore[method-assign]+ fs.acl_manager = SimpleNamespace(+ set_acl=AsyncMock(return_value="effective"),+ get_direct=AsyncMock(return_value=SimpleNamespace(entries=[])),+ to_report=lambda uri, effective: {"uri": uri, "effective": effective},+ )+ return fs+++async def test_set_acl_missing_target_returns_not_found_without_locking():+ agfs = _AclAGFS(is_dir=None)+ fs = _fs(agfs)+ with pytest.raises(NotFoundError):+ await fs.set_acl("viking://resources/target", entries=[])+ assert [e[0] for e in agfs.events] == ["stat"]+++async def test_grant_acl_missing_target_returns_not_found_without_locking():+ agfs = _AclAGFS(is_dir=None)+ fs = _fs(agfs)+ with pytest.raises(NotFoundError):+ await fs.grant_acl("viking://resources/target", "user:bob", "read")+ assert [e[0] for e in agfs.events] == ["stat"]++[email protected]("is_dir,kind", [(False, "exact"), (True, "tree")])+async def test_set_acl_locks_existing_target_by_type(is_dir, kind):+ agfs = _AclAGFS(is_dir=is_dir)+ fs = _fs(agfs)+ result = await fs.set_acl("viking://resources/target", entries=[])+ assert result["effective"] == "effective"+ kinds = [e[0] for e in agfs.events]+ assert kinds[0] == "stat"+ assert kinds[1] == kind+ assert kinds[-1] == "release"