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(snapshot): do not lock missing commit targets on the filesystem provider (#5070)
Zayn Jarvis28ad101eb320
diff --git a/openviking/storage/viking_fs/_snapshot.py b/openviking/storage/viking_fs/_snapshot.pyindex 77d052626..5d294cef4 100644--- a/openviking/storage/viking_fs/_snapshot.py+++ b/openviking/storage/viking_fs/_snapshot.py@@ -267,28 +267,65 @@ class _SnapshotMixin:from openviking.storage.errors import LockAcquisitionError, ResourceBusyError- lock_paths: List[str] = []+ # Lock by current state: Tree for a directory, Exact for a file. With+ # the filesystem PathLock provider a missing target gets no lock: a+ # Tree token there is stored as `{target}/.path.ovlock`, which creates+ # the target as a directory, and an Exact sidecar creates a missing+ # parent chain (#4966). Nothing is read under a missing name; the+ # commit only records its deletion. If another writer recreates the+ # name meanwhile the snapshot may miss it or read a partially written+ # file; the next commit records the final content. The cache (Redis)+ # provider keeps tokens off the filesystem, so it takes Tree as usual.+ missing_kind = self._snapshot_missing_target_lock_kind()+ lock_requests: List[Dict[str, str]] = []+ tree_roots: List[str] = []for path in sorted({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- ):- lock_paths.append(path)- try:- lease = await self._async_agfs.pathlock_acquire_tree_batch(lock_paths)- except LockAcquisitionError as exc:- raise ResourceBusyError(- "A snapshot path is being processed",- uri=paths[0],- ) from exc+ if any(path == root or path.startswith(f"{root.rstrip('/')}/") for root in tree_roots):+ continue+ try:+ stat = await self._async_agfs.stat(path)+ except Exception as exc:+ if not is_not_found_error(exc):+ raise+ if missing_kind is not None:+ tree_roots.append(path)+ lock_requests.append({"path": path, "kind": missing_kind})+ continue+ if isinstance(stat, dict) and stat.get("isDir", False):+ tree_roots.append(path)+ lock_requests.append({"path": path, "kind": "tree"})+ else:+ lock_requests.append({"path": path, "kind": "exact"})+ lease = None+ if lock_requests:+ try:+ lease = await self._async_agfs.pathlock_acquire_batch(lock_requests)+ except LockAcquisitionError as exc:+ raise ResourceBusyError(+ "A snapshot path is being processed",+ uri=paths[0],+ ) from exctry:scope_uris = await self._snapshot_scope_uris(paths, real_ctx)await self._ensure_access_many(scope_uris, real_ctx, action=AclAction.WRITE)return await self._async_agfs.run("git_commit", **kwargs)finally:- await self._async_agfs.pathlock_release(lease)+ if lease is not None:+ await self._async_agfs.pathlock_release(lease)++ @staticmethod+ def _snapshot_missing_target_lock_kind() -> Optional[str]:+ """Lock kind for a missing commit target: Tree on Redis, none on filesystem."""+ try:+ from openviking_cli.utils.config.open_viking_config import get_openviking_config++ provider = get_openviking_config().storage.agfs.pathlock.provider+ except Exception:+ provider = "filesystem"+ return "tree" if provider == "cache" else Noneasync def restore(self,diff --git a/tests/storage/test_snapshot_deleted_file_binding.py b/tests/storage/test_snapshot_deleted_file_binding.pynew file mode 100644index 000000000..b01ce5ab0--- /dev/null+++ b/tests/storage/test_snapshot_deleted_file_binding.py@@ -0,0 +1,118 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0+"""Committing a deleted path must not recreate it (#4966).++Non-ROOT commits took a Tree lock on every explicit path. For a deleted+file the lock resolver materialized ``example.md/`` as a directory to hold+the token, and the deletion was then committed as a noop. Missing targets+now get no lock on the filesystem provider (Tree on the Redis provider);+existing files get Exact, directories Tree.+"""++import pytest++from openviking.pyagfs import get_binding_client+from openviking.pyagfs.exceptions import AGFSNotFoundError+from openviking.server.identity import RequestContext, Role+from openviking.storage.viking_fs import VikingFS+from openviking_cli.session.user_id import UserIdentifier+++def snapshot_env(tmp_path):+ client_type, _ = get_binding_client()+ fs_root = tmp_path / "fs"+ fs_root.mkdir()+ config = tmp_path / "ragfs.toml"+ config.write_text(+ f'[git]\nenabled=true\nbackend="local"\n[git.local]\nbase_dir="{tmp_path / "git"}"\n'+ )+ client = client_type(git_config_path=str(config))+ client.mount("localfs", "/local", {"local_dir": str(fs_root)})+ return VikingFS(agfs=client), fs_root+++def _ctx(role):+ return RequestContext(user=UserIdentifier("test", "alice"), role=role)++[email protected]("role", [Role.ROOT, Role.USER])[email protected]("filename", ["example.md", "extensionless"])+async def test_commit_deleted_file_stays_absent(snapshot_env, role, filename):+ vfs, fs_root = snapshot_env+ ctx = _ctx(role)+ uri = f"viking://user/alice/memories/experiences/{filename}"+ target = fs_root / f"test/user/alice/memories/experiences/{filename}"++ await vfs.write_file(uri, "original experience", ctx=ctx)+ created = await vfs.commit(message="create", paths=[uri], ctx=ctx)+ await vfs.rm(uri, ctx=ctx)+ assert not target.exists()+ entries_after_delete = sorted(p.name for p in target.parent.iterdir())++ deleted = await vfs.commit(message="record deletion", paths=[uri], ctx=ctx)+ assert not target.exists(), "snapshot locking recreated the deleted file as a directory"+ assert sorted(p.name for p in target.parent.iterdir()) == entries_after_delete+ assert deleted["result"] == "created"+ assert deleted["commit_oid"] != created["commit_oid"]+ assert await vfs.show(created["commit_oid"], path=uri, ctx=ctx) == b"original experience"+ with pytest.raises(AGFSNotFoundError):+ await vfs.show(deleted["commit_oid"], path=uri, ctx=ctx)+ repeated = await vfs.commit(message="repeat deletion", paths=[uri], ctx=ctx)+ assert repeated["result"] == "noop"+ assert not target.exists()++ await vfs.write_file(uri, "replacement", ctx=ctx)+ assert target.is_file()+ assert target.read_text() == "replacement"+++async def test_commit_deleted_directory_stays_absent(snapshot_env):+ vfs, fs_root = snapshot_env+ ctx = _ctx(Role.USER)+ directory = "viking://user/alice/memories/experiences/topic"+ child = f"{directory}/note.md"+ target = fs_root / "test/user/alice/memories/experiences/topic"++ await vfs.write_file(child, "content", ctx=ctx)+ first = await vfs.commit(message="dir", paths=[directory], ctx=ctx)+ await vfs.rm(directory, recursive=True, ctx=ctx)+ assert not target.exists()++ deleted = await vfs.commit(message="drop dir", paths=[directory], ctx=ctx)+ assert not target.exists(), "snapshot locking recreated the deleted directory"+ assert deleted["result"] == "created"+ assert await vfs.show(first["commit_oid"], path=child, ctx=ctx) == b"content"+ with pytest.raises(AGFSNotFoundError):+ await vfs.show(deleted["commit_oid"], path=child, ctx=ctx)+++async def test_commit_lock_kinds_follow_target_state(snapshot_env, monkeypatch):+ vfs, _fs_root = snapshot_env+ ctx = _ctx(Role.USER)+ base = "viking://user/alice/memories/experiences"+ await vfs.write_file(f"{base}/dir/a.md", "a", ctx=ctx)+ await vfs.write_file(f"{base}/file.md", "f", ctx=ctx)+ await vfs.commit(message="seed", paths=[base], ctx=ctx)++ seen = []+ acquire = vfs._async_agfs.pathlock_acquire_batch++ async def recording_acquire(requests, *args, **kwargs):+ seen.append(sorted((r["path"].rsplit("/", 1)[1], r["kind"]) for r in requests))+ return await acquire(requests, *args, **kwargs)++ monkeypatch.setattr(vfs._async_agfs, "pathlock_acquire_batch", recording_acquire)+ targets = [f"{base}/dir", f"{base}/file.md", f"{base}/missing.md"]++ await vfs.commit(message="filesystem provider", paths=targets, ctx=ctx)+ assert seen[-1] == [("dir", "tree"), ("file.md", "exact")]++ monkeypatch.setattr(+ type(vfs), "_snapshot_missing_target_lock_kind", staticmethod(lambda: "tree")+ )+ await vfs.commit(message="cache provider", paths=targets, ctx=ctx)+ assert seen[-1] == [("dir", "tree"), ("file.md", "exact"), ("missing.md", "tree")]