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(storage): 恢复 cp/mv 覆盖合并并缩小路径锁范围 (#4718)
Kchenb73108dabd1d
diff --git a/crates/ov_cli/README.md b/crates/ov_cli/README.mdindex 4814b7c4d..ef749bdeb 100644--- a/crates/ov_cli/README.md+++ b/crates/ov_cli/README.md@@ -137,7 +137,7 @@ Run `ov --help` and `ov <command> --help` for the exact command surface of your- `get` - Download a file to a local path.```bash-# The destination parent must exist and the destination itself must not exist.+# The destination parent must exist. Existing files are overwritten; directories are merged.ov cp viking://resources/docs/guide.md viking://resources/archive/guide-copy.mdov cp -r viking://resources/docs viking://resources/docs-backup```diff --git a/crates/ov_cli/README_CN.md b/crates/ov_cli/README_CN.mdindex 16ab4686e..fd7ceb050 100644--- a/crates/ov_cli/README_CN.md+++ b/crates/ov_cli/README_CN.md@@ -137,7 +137,7 @@ ov grep "openviking" --uri viking://resources- `get` - 下载文件到本地路径。```bash-# 目标父目录必须已存在,目标本身必须不存在。+# 目标父目录必须已存在;已有文件直接覆盖,已有目录递归合并。ov cp viking://resources/docs/guide.md viking://resources/archive/guide-copy.mdov cp -r viking://resources/docs viking://resources/docs-backup```diff --git a/docs/en/api/03-filesystem.md b/docs/en/api/03-filesystem.mdindex 358076edb..1694ea715 100644--- a/docs/en/api/03-filesystem.md+++ b/docs/en/api/03-filesystem.md@@ -626,7 +626,9 @@ When deleting `viking://resources/...`, the response may include `memory_cleanupCopy a file or directory to a new Viking URI. The source remains unchanged. Existing vector records under the source URI are copied and rewritten for the destination, so the copied content does not need to be parsed, described by a VLM, or embedded again.-The destination parent directory must already exist, and the destination itself must not exist. Copying a directory requires `recursive=true` (or `-r` in the CLI). The destination cannot equal the source or be inside the source directory tree.+The destination parent directory must already exist. Existing files are overwritten; existing directories are merged recursively, preserving destination-only files. `to_uri` is the exact destination, without appending the source directory name. File/directory type conflicts are rejected. Copying a directory requires `recursive=true` (or `-r` in the CLI). Source and destination must be distinct and neither may contain the other. Overwrite preserves the destination ACL; new entries inherit permissions from their destination parent.++Files use Exact Locks on both paths; directories use Tree Locks on both subtrees, without locking their parent trees. A content-copy failure can leave a partial destination. A vector-copy failure attempts to remove copied vectors and destination data. Existing destination contents are not backed up: rollback after a merge can delete the entire destination, including its preexisting contents. This is not an atomic transaction.**Parameters**@@ -702,13 +704,15 @@ ov cp -r viking://resources/docs viking://resources/docs-backup`semantic_status: "queued"` means the copy has already committed and the destination parent's overview and abstract will be rebuilt asynchronously from summaries available at the destination. The API does not wait for that refresh. A refresh enqueue failure may return `semantic_status: "failed"` and `semantic_error`; it does not roll back the completed file and vector copy.-Common errors include `NOT_FOUND` when the source or destination parent is missing, `CONFLICT` when the destination already exists or a path lock is busy, `FAILED_PRECONDITION` when a directory is copied without `recursive=true`, and `INVALID_ARGUMENT` for invalid source/destination relationships.+Common errors include `NOT_FOUND` when the source or destination parent is missing, `CONFLICT` when a path lock is busy, `FAILED_PRECONDITION` when a directory is copied without `recursive=true`, and `INVALID_ARGUMENT` for invalid source/destination relationships or file/directory type conflicts.---### mv()-Move file or directory.+Move a file or directory. Existing files are overwritten; existing directories are merged recursively, preserving destination-only entries. `to_uri` is the exact destination without appending the source directory name. Type conflicts and overlapping source/destination paths are rejected.++Files use two Exact Locks; directories use Tree Locks on the source and destination, not their parent trees. The operation copies destination data, moves vector records, then deletes source data. Content-copy failures leave partial destinations. Vector or ACL update failures attempt to restore source vectors and remove destination data. A final source-deletion failure leaves the destination and any remaining source data; it does not rebuild the source. Old destination contents are not backed up, and rollback may remove an entire merged destination, so failure does not guarantee restoration of the original state.**Parameters**diff --git a/docs/zh/api/03-filesystem.md b/docs/zh/api/03-filesystem.mdindex 5ac58b6a9..fb43a9181 100644--- a/docs/zh/api/03-filesystem.md+++ b/docs/zh/api/03-filesystem.md@@ -656,7 +656,9 @@ openviking rm viking://resources/old.md [--recursive]把文件或目录复制到新的 Viking URI,源内容保持不变。源 URI 下已有的向量记录会同步复制并改写为目标 URI,因此无需重新解析复制内容,也无需重新执行文件级 VLM 或 embedding。-目标父目录必须已经存在,目标本身必须不存在。复制目录时必须设置 `recursive=true`(CLI 中使用 `-r`)。目标不能与源相同,也不能位于源目录子树内。+目标父目录必须已经存在。目标文件存在时直接覆盖;目标目录存在时递归合并,保留目标独有文件。`to_uri` 就是实际目标位置,不额外追加源目录名;文件与目录类型冲突时拒绝。复制目录时必须设置 `recursive=true`(CLI 中使用 `-r`)。源、目标不能相同或互为祖先与后代。覆盖时保留目标原有访问权限;新目标继承目标父级权限。++文件使用源、目标双 Exact Lock,目录使用双 Tree Lock,不锁父目录整树。复制失败可能保留部分目标;向量复制失败时尝试清理目标向量和目标数据。旧目标不备份,合并后发生向量失败可能删除整个目标目录,包括其原有内容;该操作不是原子事务。**参数**@@ -732,13 +734,15 @@ ov cp -r viking://resources/docs viking://resources/docs-backup`semantic_status: "queued"` 表示复制已经提交,目标父目录的 overview 和 abstract 将根据目标目录中已有的摘要异步重建,接口不会等待刷新完成。若语义刷新入队失败,响应可能包含 `semantic_status: "failed"` 和 `semantic_error`;已经完成的文件和向量复制不会因此回滚。-常见错误包括:源或目标父目录不存在时返回 `NOT_FOUND`;目标已存在或路径锁繁忙时返回 `CONFLICT`;复制目录但未设置 `recursive=true` 时返回 `FAILED_PRECONDITION`;源和目标关系非法时返回 `INVALID_ARGUMENT`。+常见错误包括:源或目标父目录不存在时返回 `NOT_FOUND`;路径锁繁忙时返回 `CONFLICT`;复制目录但未设置 `recursive=true` 时返回 `FAILED_PRECONDITION`;源和目标关系非法或类型冲突时返回 `INVALID_ARGUMENT`。---### mv()-移动文件或目录。+移动文件或目录。目标文件存在时覆盖,目标目录存在时递归合并并保留目标独有内容;`to_uri` 为实际目标位置,不追加源目录名。文件与目录类型冲突、源目标相同或互相包含时拒绝。++文件使用双 Exact Lock,目录使用源、目标双 Tree Lock,不锁父目录整树。执行顺序为复制目标、迁移向量、删除源。复制失败不统一清理部分目标;向量迁移或 ACL 更新失败时尝试恢复源向量并删除目标;最后删除源失败时保留目标与残余源,不重建源。旧目标不备份,合并目标可能在回滚清理中被整体删除,因此不保证失败后恢复原状。**参数**diff --git a/openviking/service/fs_service.py b/openviking/service/fs_service.pyindex 5e2b16f18..b1cba9b7a 100644--- a/openviking/service/fs_service.py+++ b/openviking/service/fs_service.py@@ -608,6 +608,8 @@ class FSService:ctx: RequestContext,) -> Dict[str, Any]:"""Copy a resource without exposing a cancellable partial transaction."""+ from_uri = VikingFS._normalize_transfer_uri(from_uri)+ to_uri = VikingFS._normalize_transfer_uri(to_uri)return await self._finish_transfer_after_caller_cancel(self._cp_and_refresh(from_uri, to_uri, recursive=recursive, ctx=ctx),operation="copy",@@ -664,6 +666,8 @@ class FSService:async def mv(self, from_uri: str, to_uri: str, ctx: RequestContext) -> None:"""Move a resource without exposing a cancellable partial transaction."""+ from_uri = VikingFS._normalize_transfer_uri(from_uri)+ to_uri = VikingFS._normalize_transfer_uri(to_uri)await self._finish_transfer_after_caller_cancel(self._mv_and_refresh(from_uri, to_uri, ctx=ctx),operation="move",diff --git a/openviking/storage/viking_fs/_ops.py b/openviking/storage/viking_fs/_ops.pyindex 816d96a69..cba385e17 100644--- a/openviking/storage/viking_fs/_ops.py+++ b/openviking/storage/viking_fs/_ops.py@@ -39,7 +39,6 @@ from openviking.storage.viking_fs._base import ()from openviking.utils.time_utils import format_iso8601, parse_iso_datetimefrom openviking_cli.exceptions import (- ConflictError,FailedPreconditionError,InvalidArgumentError,NotFoundError,@@ -60,15 +59,6 @@ def _glob_match_uri(entry_uri: str, is_dir: Optional[bool]) -> str:return f"{entry_uri}/"-class TransferRollbackError(RuntimeError):- """A filesystem transfer failed and left a compensation failure to repair."""-- def __init__(self, message: str, *, phase: str, residual_uri: str):- super().__init__(message)- self.phase = phase- self.residual_uri = residual_uri--class _OpsMixin:"""Core filesystem operations (read/write/mkdir/rm/mv/stat/glob/tree/ls/temp)."""@@ -327,17 +317,20 @@ class _OpsMixin:lease_ref: Dict[str, Any] | None = None,) -> Dict[str, Any]:"""Copy a file or directory together with its vector records."""+ old_uri = self._normalize_transfer_uri(old_uri)+ new_uri = self._normalize_transfer_uri(new_uri)await self._ensure_copy_source_access(old_uri, recursive=recursive, ctx=ctx)await self._ensure_access(new_uri, ctx, action=AclAction.WRITE)old_scope = old_uri.rstrip("/")new_scope = new_uri.rstrip("/")if old_scope == new_scope:raise InvalidArgumentError("cp source and target must be different")- if new_scope.startswith(old_scope + "/"):- raise InvalidArgumentError("cp target cannot be inside the source subtree")+ if new_scope.startswith(old_scope + "/") or old_scope.startswith(new_scope + "/"):+ raise InvalidArgumentError("cp source and target subtrees must not overlap")old_path = self._uri_to_path(old_uri, ctx=ctx)new_path = self._uri_to_path(new_uri, ctx=ctx)+ self._validate_transfer_paths(old_path, new_path)try:stat = await self._async_agfs.stat(old_path)except Exception as exc:@@ -359,7 +352,7 @@ class _OpsMixin:)await self._ensure_transfer_parent_directory(new_path, new_uri, operation="cp")- await self._ensure_transfer_target_missing(new_path, new_uri)+ await self._ensure_transfer_target_type(new_path, new_uri, is_dir=is_dir)lock_requests = (self._directory_transfer_lock_requests(old_path, new_path)if is_dir@@ -374,38 +367,33 @@ class _OpsMixin:)operation_id = uuid.uuid4().hextry:- await self._ensure_transfer_target_missing(new_path, new_uri)+ source_uris = await self._prepare_transfer_entries(+ old_uri, new_uri, is_dir=is_dir, move=False, ctx=ctx+ )+ files_created = await self._copy_agfs_entry(+ old_path,+ new_path,+ old_uri=old_uri,+ new_uri=new_uri,+ is_dir=is_dir,+ ctx=ctx,+ lease_ref=lease,+ )try:- files_created = await self._copy_agfs_entry(- old_path,- new_path,- old_uri=old_uri,- new_uri=new_uri,- is_dir=is_dir,- ctx=ctx,- lease_ref=lease,- )vector_result = await self._copy_vector_store_uris(old_uri,new_uri,recursive=is_dir,ctx=ctx,+ **({"source_uris": source_uris} if is_dir else {}),)- except Exception as transfer_error:+ except Exception:try:await self._cleanup_transfer_target(- new_path,- is_dir=is_dir,- ctx=ctx,- lease_ref=lease,+ new_path, is_dir=is_dir, ctx=ctx, lease_ref=lease)- except Exception as rollback_error:- if not is_not_found_error(rollback_error):- raise TransferRollbackError(- f"cp failed and target cleanup failed for {new_uri}: {rollback_error}",- phase="target_cleanup",- residual_uri=new_uri,- ) from transfer_error+ except Exception:+ logger.warning("Failed to clean copy target %s", new_uri, exc_info=True)raiseresult: Dict[str, Any] = {"operation_id": operation_id,@@ -435,9 +423,10 @@ class _OpsMixin:finally:await self._async_agfs.pathlock_release(lease)- async def _ensure_transfer_target_missing(self, path: str, uri: str) -> None:+ async def _ensure_transfer_target_type(self, path: str, uri: str, *, is_dir: bool) -> None:+ """Allow overwrite/merge, but never replace a file with a directory or vice versa."""try:- await self._async_agfs.stat(path)+ stat = await self._async_agfs.stat(path)except Exception as exc:if is_not_found_error(exc):return@@ -445,7 +434,51 @@ class _OpsMixin:if mapped is not None:raise mapped from excraise- raise ConflictError(f"transfer target already exists: {uri}", resource=uri)+ if bool(stat.get("isDir", False)) != is_dir:+ raise InvalidArgumentError(f"transfer source and target types differ: {uri}")++ async def _prepare_transfer_entries(+ self,+ old_uri: str,+ new_uri: str,+ *,+ is_dir: bool,+ move: bool,+ ctx: Optional[RequestContext],+ ) -> List[str]:+ """Validate all affected entries under the lease before the first content write."""+ source_uris: List[str] = []++ async def visit(source: str, target: str, directory: bool) -> None:+ source_path = self._uri_to_path(source, ctx=ctx)+ stat = await self._async_agfs.stat(source_path)+ if bool(stat.get("isDir", False)) != directory:+ raise InvalidArgumentError(f"transfer source type changed: {source}")+ if move:+ if directory or source != old_uri:+ await self._ensure_access(source, ctx, action=AclAction.MANAGE)+ else:+ await self._ensure_copy_source_access(source, recursive=directory, ctx=ctx)+ await self._ensure_access(target, ctx, action=AclAction.WRITE)+ await self._ensure_transfer_target_type(+ self._uri_to_path(target, ctx=ctx), target, is_dir=directory+ )+ source_uris.append(source)+ if directory:+ # Match the copy traversal exactly; user content can use names+ # that the namespace listing API hides, such as tasks/_system.+ for entry in await self._async_agfs.ls(source_path):+ name = entry.get("name", "")+ if not name or name in (".", ".."):+ continue+ await visit(+ f"{source.rstrip('/')}/{name}",+ f"{target.rstrip('/')}/{name}",+ bool(entry.get("isDir", False)),+ )++ await visit(old_uri, new_uri, is_dir)+ return source_urisasync def _ensure_copy_source_access(self,@@ -502,6 +535,21 @@ class _OpsMixin:if not isinstance(parent_stat, dict) or not parent_stat.get("isDir", False):raise InvalidArgumentError(f"{operation} target parent is not a directory: {uri}")+ @classmethod+ def _normalize_transfer_uri(cls, uri: str) -> str:+ """Use the same path segments for permissions, filesystem I/O and vector URIs."""+ # VikingFS supplies this validator through _AccessMixin.+ parts = cls._safe_uri_parts(uri) # type: ignore[attr-defined]+ normalized = "viking://" + "/".join(parts)+ return normalized + "/" if parts and uri.endswith("/") else normalized++ @staticmethod+ def _validate_transfer_paths(old_path: str, new_path: str) -> None:+ """Reject aliases for the same or overlapping physical transfer scope."""+ source, target = old_path.rstrip("/"), new_path.rstrip("/")+ if source == target or target.startswith(source + "/") or source.startswith(target + "/"):+ raise InvalidArgumentError("transfer source and target paths must not overlap")+@staticmethoddef _transfer_parent_path(path: str) -> str:return path.rstrip("/").rsplit("/", 1)[0] or "/"@@ -510,30 +558,11 @@ class _OpsMixin:def _directory_transfer_lock_requests(cls, old_path: str, new_path: str) -> List[Dict[str, str]]:- """Lock stable parents so directory deletion and recreation stay covered."""-- parents: List[str] = []- for parent in (- cls._transfer_parent_path(old_path),- cls._transfer_parent_path(new_path),- ):- if any(cls._tree_lock_covers(existing, parent) for existing in parents):- continue- parents = [- existing for existing in parents if not cls._tree_lock_covers(parent, existing)- ]- parents.append(parent)- return [{"path": parent, "kind": "tree"} for parent in parents]-- @staticmethod- def _tree_lock_covers(ancestor: str, path: str) -> bool:- normalized_ancestor = ancestor.rstrip("/") or "/"- normalized_path = path.rstrip("/") or "/"- if normalized_ancestor == "/":- return True- return normalized_path == normalized_ancestor or normalized_path.startswith(- f"{normalized_ancestor}/"- )+ """Cover both transfer subtrees without blocking unrelated siblings."""+ return [+ {"path": old_path, "kind": "tree"},+ {"path": new_path, "kind": "tree"},+ ]async def _copy_agfs_entry(self,@@ -547,7 +576,7 @@ class _OpsMixin:lease_ref: Dict[str, Any],) -> int:if is_dir:- return await self._copy_directory_under_parent_locks(+ return await self._copy_directory_under_tree_locks(old_path,new_path,old_uri=old_uri,@@ -591,6 +620,8 @@ class _OpsMixin:On VectorDB update failure the copy is cleaned up so the source stays intact."""+ old_uri = self._normalize_transfer_uri(old_uri)+ new_uri = self._normalize_transfer_uri(new_uri)acl_manager = self.acl_manageracl_enabled = self._acl_enabled(ctx)guard_ctx = replace(self._ctx_or_default(ctx), bypass_acl=True)@@ -601,12 +632,11 @@ class _OpsMixin:new_scope = new_uri.rstrip("/")if old_scope == new_scope:raise InvalidArgumentError("mv source and target must be different")- if new_scope.startswith(old_scope + "/"):- raise InvalidArgumentError("mv target cannot be inside the source subtree")+ if new_scope.startswith(old_scope + "/") or old_scope.startswith(new_scope + "/"):+ raise InvalidArgumentError("mv source and target subtrees must not overlap")old_path = self._uri_to_path(old_uri, ctx=ctx)new_path = self._uri_to_path(new_uri, ctx=ctx)- source_vector_uri = self._path_to_uri(old_path, ctx=ctx)- target_uri = source_vector_uri+ self._validate_transfer_paths(old_path, new_path)new_acl_scope = acl_enabled and is_acl_uri(new_uri)# Verify source exists and determine type before locking.@@ -624,7 +654,7 @@ class _OpsMixin:if is_dir:await self._ensure_access(old_uri, ctx, action=AclAction.MANAGE)await self._ensure_transfer_parent_directory(new_path, new_uri, operation="mv")- await self._ensure_transfer_target_missing(new_path, new_uri)+ await self._ensure_transfer_target_type(new_path, new_uri, is_dir=is_dir)if not is_dir:if new_uri.rstrip("/") != new_uri:@@ -663,20 +693,9 @@ class _OpsMixin:operation_id = uuid.uuid4().hextry:- uris_to_move = (- await self._collect_uris(- old_path,- recursive=True,- ctx=ctx,- strict=is_dir and acl_enabled,- )- if is_dir- else []+ uris_to_move = await self._prepare_transfer_entries(+ old_uri, new_uri, is_dir=is_dir, move=True, ctx=ctx)- uris_to_move.append(target_uri)- if is_dir:- await self._ensure_access_many(uris_to_move, ctx, action=AclAction.MANAGE)- await self._ensure_transfer_target_missing(new_path, new_uri)# Check if it's temp directory (files already encrypted)is_temp = old_uri.startswith("viking://temp/")@@ -697,31 +716,13 @@ class _OpsMixin:or 0)except Exception as transfer_error:- try:- await self._cleanup_transfer_target(- new_path,- is_dir=is_dir,- ctx=ctx,- lease_ref=lease,- )- except Exception as rollback_error:- if not is_not_found_error(rollback_error):- raise TransferRollbackError(- f"mv AGFS copy failed and target cleanup failed for "- f"{new_uri}: {rollback_error}",- phase="target_cleanup",- residual_uri=new_uri,- ) from transfer_errorif is_not_found_error(transfer_error):try:await self._delete_from_vector_store(uris_to_move, ctx=ctx)- except Exception as vector_cleanup_error:- raise TransferRollbackError(- f"mv source disappeared and orphan vector cleanup failed for "- f"{old_uri}: {vector_cleanup_error}",- phase="source_vector_cleanup",- residual_uri=old_uri,- ) from transfer_error+ except Exception:+ logger.warning(+ "Failed to clean orphan move vectors for %s", old_uri, exc_info=True+ )else:logger.info(f"[VikingFS] mv source not found, cleaned orphan index: {old_uri}"@@ -736,6 +737,7 @@ class _OpsMixin:new_uri,recursive=is_dir,ctx=ctx,+ **({"source_uris": uris_to_move} if is_dir else {}),)vector_transfer_completed = Trueif acl_manager is not None and new_acl_scope:@@ -743,82 +745,35 @@ class _OpsMixin:new_uri,self._ctx_or_default(ctx),)- except Exception as transfer_error:+ except Exception:if vector_transfer_completed:try:+ moved_uris = [+ new_scope + uri.rstrip("/")[len(old_scope) :] for uri in uris_to_move+ ]await self._update_vector_store_uris(new_uri,old_uri,recursive=is_dir,ctx=ctx,+ **({"source_uris": moved_uris} if is_dir else {}),+ )+ except Exception:+ logger.warning(+ "Failed to restore move vectors for %s", old_uri, exc_info=True)- except Exception as rollback_error:- raise TransferRollbackError(- f"mv post-vector step failed and vector restore was incomplete for "- f"{old_uri} -> {new_uri}: {rollback_error}",- phase="vector_restore",- residual_uri=new_uri,- ) from transfer_errortry:await self._cleanup_transfer_target(- new_path,- is_dir=is_dir,- ctx=ctx,- lease_ref=lease,+ new_path, is_dir=is_dir, ctx=ctx, lease_ref=lease)- except Exception as rollback_error:- raise TransferRollbackError(- f"mv vector transfer failed and target cleanup failed for "- f"{new_uri}: {rollback_error}",- phase="target_cleanup",- residual_uri=new_uri,- ) from transfer_error+ except Exception:+ logger.warning("Failed to clean move target %s", new_uri, exc_info=True)raise- # Delete source- try:- await self._async_agfs.rm(- old_path,- recursive=is_dir,- fs_ctx=self._pathlock_fs_ctx(ctx, lease),- )- except Exception as delete_error:- try:- await self._cleanup_transfer_target(- old_path,- is_dir=is_dir,- ctx=ctx,- lease_ref=lease,- )- await self._copy_agfs_entry(- new_path,- old_path,- old_uri=new_uri,- new_uri=old_uri,- is_dir=is_dir,- ctx=ctx,- lease_ref=lease,- )- await self._update_vector_store_uris(- new_uri,- old_uri,- recursive=is_dir,- ctx=ctx,- )- await self._cleanup_transfer_target(- new_path,- is_dir=is_dir,- ctx=ctx,- lease_ref=lease,- )- except Exception as rollback_error:- raise TransferRollbackError(- f"mv source deletion failed and rollback was incomplete for "- f"{old_uri} -> {new_uri}: {rollback_error}",- phase="source_restore",- residual_uri=old_uri,- ) from delete_error- raise+ # Old mv semantics: source deletion is the last step, with no copy-back on failure.+ await self._async_agfs.rm(+ old_path, recursive=is_dir, fs_ctx=self._pathlock_fs_ctx(ctx, lease)+ )result: Dict[str, Any] = {"operation_id": operation_id,"operation": "move",@@ -873,7 +828,7 @@ class _OpsMixin:lease_ref=lease_ref,)- async def _copy_directory_under_parent_locks(+ async def _copy_directory_under_tree_locks(self,old_path: str,new_path: str,@@ -884,13 +839,13 @@ class _OpsMixin:transfer_source_uri: str | None = None,transfer_target_uri: str | None = None,) -> int:- """Copy a directory under the operation's stable parent Tree leases.+ """Copy a directory under the operation's source and target Tree leases.Args:old_path: Source backend directory path.new_path: Destination backend directory path.ctx: Request context used for filesystem operations.- lease_ref: Batch lease covering the source and destination parents.+ lease_ref: Batch lease covering the source and destination subtrees.Returns:Number of created directories and files.@@ -900,7 +855,16 @@ class _OpsMixin:transfer_source_uri = transfer_source_uri or old_uritransfer_target_uri = transfer_target_uri or new_urifs_ctx = self._pathlock_fs_ctx(ctx, lease_ref)- await self._async_agfs.mkdir(new_path, fs_ctx=fs_ctx)+ # Tree acquisition may already have created the target to hold its lock.+ try:+ stat = await self._async_agfs.stat(new_path)+ except Exception as exc:+ if not is_not_found_error(exc):+ raise+ await self._async_agfs.mkdir(new_path, fs_ctx=fs_ctx)+ else:+ if not stat.get("isDir", False):+ raise InvalidArgumentError(f"transfer target is not a directory: {new_uri}")copied = 1entries = await self._async_agfs.ls(old_path, fs_ctx=fs_ctx)for entry in entries:@@ -917,7 +881,7 @@ class _OpsMixin:ctx=ctx,)if entry.get("isDir", False):- copied += await self._copy_directory_under_parent_locks(+ copied += await self._copy_directory_under_tree_locks(old_child,new_child,old_uri=old_child_uri,diff --git a/openviking/storage/viking_fs/_vector.py b/openviking/storage/viking_fs/_vector.pyindex 000d408e5..e4ce41418 100644--- a/openviking/storage/viking_fs/_vector.py+++ b/openviking/storage/viking_fs/_vector.py@@ -2,6 +2,7 @@# SPDX-License-Identifier: AGPL-3.0"""Vector store integration mixin for VikingFS."""+from functools import partialfrom typing import TYPE_CHECKING, Any, List, Optionalfrom openviking.server.identity import RequestContext@@ -41,6 +42,7 @@ class _VectorMixin:*,recursive: bool,ctx: Optional[RequestContext] = None,+ source_uris: List[str] | None = None,) -> Any:"""Copy a complete vector URI scope while preserving the source."""vector_store = self._get_vector_store()@@ -51,6 +53,8 @@ class _VectorMixin:source_uri=old_base,target_uri=new_base,recursive=recursive,+ target_entry_exists=partial(self.exists, ctx=ctx), # type: ignore[attr-defined]+ **({"source_uris": source_uris} if source_uris is not None else {}),)async def _update_vector_store_uris(@@ -60,8 +64,9 @@ class _VectorMixin:*,recursive: bool,ctx: Optional[RequestContext] = None,+ source_uris: List[str] | None = None,) -> Any:- """Strictly move a complete vector URI scope.+ """Move a vector URI scope, overwriting matching target records.Preserves vector data and updates URI-derived identifiers without regenerating embeddings."""@@ -73,6 +78,8 @@ class _VectorMixin:source_uri=old_base,target_uri=new_base,recursive=recursive,+ target_entry_exists=partial(self.exists, ctx=ctx), # type: ignore[attr-defined]+ **({"source_uris": source_uris} if source_uris is not None else {}),)def _get_vector_store(self) -> Optional["VikingVectorIndexBackend"]:diff --git a/openviking/storage/viking_vector_index_backend.py b/openviking/storage/viking_vector_index_backend.pyindex 4f1f20074..4e4790930 100644--- a/openviking/storage/viking_vector_index_backend.py+++ b/openviking/storage/viking_vector_index_backend.py@@ -8,9 +8,14 @@ import asyncioimport uuidfrom contextlib import asynccontextmanagerfrom dataclasses import dataclass-from typing import Any, AsyncIterator, Dict, List, Mapping, Optional+from typing import Any, AsyncIterator, Awaitable, Callable, Container, Dict, List, Mapping, Optional-from openviking.core.namespace import canonical_user_root, resolve_uri, uri_parts, visible_roots+from openviking.core.namespace import (+ canonical_user_root,+ resolve_uri,+ uri_parts,+ visible_roots,+)from openviking.server.identity import RequestContext, Rolefrom openviking.storage.acl import (ACL_CONTEXT_FIELDS,@@ -18,9 +23,11 @@ from openviking.storage.acl import (AclManager,acl_grant_tokens,acl_principals,+ is_acl_uri,)from openviking.storage.expr import And, Contains, Eq, FilterExpr, In, Or, PathScope, RawDSLfrom openviking.storage.vector_migration import (+ rewrite_transfer_uri,rewrite_vector_record,uri_in_transfer_scope,)@@ -30,7 +37,7 @@ from openviking.storage.vectordb.utils.logging_init import init_cpp_loggingfrom openviking.storage.vectordb_adapters import create_collection_adapterfrom openviking.utils.tags import merge_search_tagsfrom openviking.utils.time_utils import get_current_timestamp-from openviking_cli.exceptions import ConflictError, InvalidArgumentError+from openviking_cli.exceptions import InvalidArgumentErrorfrom openviking_cli.utils import get_loggerfrom openviking_cli.utils.config.vectordb_config import DEFAULT_INDEX_NAME, VectorDBBackendConfigfrom openviking_cli.utils.uri import VikingURI@@ -181,9 +188,7 @@ class _AsyncVectorAdapter:index_meta = collection.get_index_meta_data(index_name) or {}current_scalar_index = index_meta.get("ScalarIndex", [])indexed_fields = set(current_scalar_index)- missing_scalar_fields = [- field for field in scalar_index if field not in indexed_fields- ]+ missing_scalar_fields = [field for field in scalar_index if field not in indexed_fields]if missing_scalar_fields:collection.update_index(index_name,@@ -389,9 +394,7 @@ class _SingleAccountBackend:async def update_collection_schema(self, fields: List[Dict[str, Any]], scalar_index: List[str]) -> None:- await self._async_adapter.update_collection_schema(- fields, scalar_index, self._index_name- )+ await self._async_adapter.update_collection_schema(fields, scalar_index, self._index_name)await self._refresh_meta_data_async()# =========================================================================@@ -1725,9 +1728,37 @@ class VikingVectorIndexBackend:recursive: bool,include_full_records: bool,batch_size: int = 100,+ entry_uris: List[str] | None = None,) -> tuple[List[Dict[str, Any]], int]:"""Scan one URI scope without a fixed total-record limit."""- transfer_filter = self._uri_transfer_filter(ctx, uri, recursive=recursive)+ selected_entries = set(entry_uris) if entry_uris is not None else None+ if selected_entries is not None:+ if not selected_entries:+ return [], 0+ filters: List[FilterExpr] = []+ for entry in sorted(selected_entries):+ if self.mode == "volcengine":+ filters.append(self._uri_transfer_filter(ctx, entry, recursive=False))+ else:+ # Raw prefix filters encode URI values to the stored path format.+ filters.append(+ And(+ [+ Eq("account_id", ctx.account_id),+ Or(+ [+ Eq("uri", entry),+ RawDSL(+ {"op": "prefix", "field": "uri", "prefix": entry + "#"}+ ),+ ]+ ),+ ]+ )+ )+ transfer_filter = filters[0] if len(filters) == 1 else Or(filters)+ else:+ transfer_filter = self._uri_transfer_filter(ctx, uri, recursive=recursive)expected_count = await self._strict_transfer_count(ctx, transfer_filter)records: List[Dict[str, Any]] = []cursor: Optional[str] = None@@ -1763,7 +1794,11 @@ class VikingVectorIndexBackend:recordfor record in pageif isinstance(record.get("uri"), str)- and uri_in_transfer_scope(record["uri"], uri, recursive=recursive)+ and (+ self._vector_entry_uri(record["uri"], selected_entries) in selected_entries+ if selected_entries is not None+ else uri_in_transfer_scope(record["uri"], uri, recursive=recursive)+ )]if include_full_records and scoped:ids = [str(record["id"]) for record in scoped if record.get("id")]@@ -1821,38 +1856,155 @@ class VikingVectorIndexBackend:deleted += batch_deletedreturn deleted+ @staticmethod+ def _vector_entry_uri(uri: str, known_uris: Container[str] = ()) -> str:+ """Prefer real entry URIs; only strip a trailing chunk identifier."""+ if uri in known_uris:+ return uri+ base, marker, suffix = uri.rpartition("#")+ if marker and "/" not in suffix and suffix.startswith(("chunk_", "chunk-")):+ return base+ return uri++ @staticmethod+ def _validate_uri_transfer_scopes(source_uri: str, target_uri: str) -> None:+ if uri_in_transfer_scope(target_uri, source_uri, recursive=True) or uri_in_transfer_scope(+ source_uri, target_uri, recursive=True+ ):+ raise InvalidArgumentError(+ "source and target vector scopes must not be equal or contain one another"+ )++ async def _prepare_uri_transfer(+ self,+ ctx: RequestContext,+ source_uri: str,+ target_uri: str,+ *,+ recursive: bool,+ source_uris: List[str] | None,+ preserve_target_acl: bool = False,+ target_entry_exists: Callable[[str], Awaitable[bool]] | None = None,+ ) -> tuple[List[Dict[str, Any]], int, Dict[str, Dict[str, Any]]]:+ """Read selected source records and remove affected target records."""+ source_records, batches = await self._scan_uri_transfer_scope(+ ctx,+ source_uri,+ recursive=recursive,+ include_full_records=True,+ )++ selected_source_uris = {+ resolve_uri(uri).uri+ for uri in (source_uris if source_uris is not None else [source_uri])+ }+ if source_uris is not None:+ source_records = [+ record+ for record in source_records+ if self._vector_entry_uri(str(record["uri"]), selected_source_uris)+ in selected_source_uris+ ]++ # Match legacy mv: unindexed source entries leave old target records intact.+ if not source_records:+ return source_records, batches, {}+ replacement_source_uris = {+ self._vector_entry_uri(str(record["uri"]), selected_source_uris)+ for record in source_records+ }+ replacement_target_uris = {+ rewrite_transfer_uri(uri, source_uri, target_uri) for uri in replacement_source_uris+ }+ if any(+ not uri_in_transfer_scope(uri, target_uri, recursive=recursive)+ for uri in replacement_target_uris+ ):+ raise InvalidArgumentError("replacement entry is outside the target vector scope")+ # URI spelling alone cannot distinguish a chunk from a real file whose+ # name ends in #chunk_*. Ask the filesystem only for ambiguous candidates.+ independent_targets: Dict[str, bool] = {}++ async def is_independent_target(uri: str) -> bool:+ if target_entry_exists is None or uri in replacement_target_uris:+ return False+ if self._vector_entry_uri(uri, replacement_target_uris) == uri:+ return False+ if uri not in independent_targets:+ independent_targets[uri] = await target_entry_exists(uri)+ return independent_targets[uri]++ for record in source_records:+ written_uri = rewrite_transfer_uri(str(record["uri"]), source_uri, target_uri)+ if await is_independent_target(written_uri):+ raise InvalidArgumentError(+ f"vector chunk conflicts with an existing filesystem entry: {written_uri}"+ )+ # A chunk cannot carry ACL for its base file URI. Keep the old main record+ # when copy has no main record to replace it, accepting its stale content.+ preserved_acl_uris: set[str] = set()+ if preserve_target_acl and self._acl_enabled(ctx):+ written_uris = {+ rewrite_transfer_uri(str(record["uri"]), source_uri, target_uri)+ for record in source_records+ }+ preserved_acl_uris = {+ uri for uri in replacement_target_uris - written_uris if is_acl_uri(uri)+ }+ # Bound the query expression and avoid scanning destination-only subtrees.+ target_entries = sorted(replacement_target_uris)+ affected_target_ids: List[str] = []+ for offset in range(0, len(target_entries), 100):+ target_records, _ = await self._scan_uri_transfer_scope(+ ctx,+ target_uri,+ recursive=False,+ include_full_records=False,+ entry_uris=target_entries[offset : offset + 100],+ )+ for record in target_records:+ if record["uri"] not in preserved_acl_uris and not await is_independent_target(+ str(record["uri"])+ ):+ affected_target_ids.append(str(record["id"]))+ target_acl_fields: Dict[str, Dict[str, Any]] = {}+ if preserve_target_acl and self._acl_enabled(ctx) and replacement_target_uris:+ assert self.acl_manager is not None+ acl_target_uris = {uri for uri in replacement_target_uris if is_acl_uri(uri)}+ target_acl_fields = {+ uri: effective.context_fields()+ for uri, effective in (+ await self.acl_manager.resolve_many(acl_target_uris, ctx)+ ).items()+ }+ if affected_target_ids:+ await self._delete_vector_transfer_ids(ctx, affected_target_ids)+ return source_records, batches, target_acl_fields+async def copy_uri_mapping(self,ctx: RequestContext,source_uri: str,target_uri: str,recursive: bool = False,+ *,+ source_uris: List[str] | None = None,+ target_entry_exists: Callable[[str], Awaitable[bool]] | None = None,) -> VectorTransferResult:"""Copy every vector record in a URI scope without regenerating embeddings."""source_uri = resolve_uri(source_uri).uritarget_uri = resolve_uri(target_uri).uri- target_records, _ = await self._scan_uri_transfer_scope(- ctx,- target_uri,- recursive=recursive,- include_full_records=False,- )- if target_records:- raise ConflictError(- f"copy target vector scope already exists: {target_uri}",- resource=target_uri,- )-- source_records, batches = await self._scan_uri_transfer_scope(+ self._validate_uri_transfer_scopes(source_uri, target_uri)+ source_records, batches, target_acl_fields = await self._prepare_uri_transfer(ctx,source_uri,+ target_uri,recursive=recursive,- include_full_records=True,+ source_uris=source_uris,+ target_entry_exists=target_entry_exists,+ preserve_target_acl=True,)result = VectorTransferResult(scanned=len(source_records), batches=batches)- if not source_records:- return result-timestamp = get_current_timestamp()target_payloads = [rewrite_vector_record(@@ -1865,12 +2017,27 @@ class VikingVectorIndexBackend:)for record in source_records]+ if target_acl_fields:+ for payload in target_payloads:+ acl_fields = target_acl_fields.get(+ self._vector_entry_uri(str(payload["uri"]), target_acl_fields)+ )+ if acl_fields is not None:+ payload.update(acl_fields)++ if not target_payloads:+ return result+attempted_target_ids: List[str] = []try:for offset in range(0, len(target_payloads), 100):payload_batch = target_payloads[offset : offset + 100]attempted_target_ids.extend(str(payload["id"]) for payload in payload_batch)- written_ids = await self.upsert_many(payload_batch, ctx=ctx)+ written_ids = (+ await self._upsert_many_raw(payload_batch, ctx=ctx)+ if target_acl_fields+ else await self.upsert_many(payload_batch, ctx=ctx)+ )if len(written_ids) != len(payload_batch):raise RuntimeError(f"Vector copy wrote {len(written_ids)} of {len(payload_batch)} records"@@ -1882,13 +2049,7 @@ class VikingVectorIndexBackend:except Exception as rollback_error:diagnostic_suffix = ""try:- residual_records, _ = await self._scan_uri_transfer_scope(- ctx,- target_uri,- recursive=recursive,- include_full_records=False,- )- residual_count = len(residual_records)+ residual_count = len(await self._strict_transfer_get(ctx, attempted_target_ids))except Exception as diagnostic_error:residual_count = len(attempted_target_ids)diagnostic_suffix = f"; residual scan failed: {diagnostic_error}"@@ -1907,27 +2068,21 @@ class VikingVectorIndexBackend:source_uri: str,target_uri: str,recursive: bool = False,+ *,+ source_uris: List[str] | None = None,+ target_entry_exists: Callable[[str], Awaitable[bool]] | None = None,) -> VectorTransferResult:"""Move every vector record in a URI scope with compensating rollback."""source_uri = resolve_uri(source_uri).uritarget_uri = resolve_uri(target_uri).uri- target_records, _ = await self._scan_uri_transfer_scope(- ctx,- target_uri,- recursive=recursive,- include_full_records=False,- )- if target_records:- raise ConflictError(- f"move target vector scope already exists: {target_uri}",- resource=target_uri,- )-- source_records, batches = await self._scan_uri_transfer_scope(+ self._validate_uri_transfer_scopes(source_uri, target_uri)+ source_records, batches, _ = await self._prepare_uri_transfer(ctx,source_uri,+ target_uri,recursive=recursive,- include_full_records=True,+ source_uris=source_uris,+ target_entry_exists=target_entry_exists,)result = VectorTransferResult(scanned=len(source_records), batches=batches)if not source_records:@@ -1979,10 +2134,17 @@ class VikingVectorIndexBackend:try:await self._delete_vector_transfer_ids(ctx, attempted_target_ids)except Exception as rollback_error:+ diagnostic_suffix = ""+ try:+ residual_count = len(await self._strict_transfer_get(ctx, attempted_target_ids))+ except Exception as diagnostic_error:+ residual_count = len(attempted_target_ids)+ diagnostic_suffix = f"; residual scan failed: {diagnostic_error}"raise VectorTransferRollbackError(- f"Vector move prepare failed and target cleanup failed: {rollback_error}",+ f"Vector move prepare failed and target cleanup failed: {rollback_error}"+ f"{diagnostic_suffix}",phase="move_target_cleanup",- residual_count=len(attempted_target_ids),+ residual_count=residual_count,) from transfer_errorraisediff --git a/tests/service/test_fs_service.py b/tests/service/test_fs_service.pyindex 5cd13a9c6..bbdfbda3d 100644--- a/tests/service/test_fs_service.py+++ b/tests/service/test_fs_service.py@@ -786,7 +786,10 @@ async def test_resource_rm_does_not_deactivate_watch_task_control_uri(request_co@pytest.mark.asyncio-async def test_resource_mv_validates_then_moves_then_rewrites_watch_tasks(request_context):[email protected]("separator", ["/", "//"])+async def test_resource_mv_validates_then_moves_then_rewrites_watch_tasks(+ request_context, separator+):events = []viking_fs = _FakeVikingFS(events=events)watch_manager = _FakeWatchManager(events=events)@@ -810,8 +813,8 @@ async def test_resource_mv_validates_then_moves_then_rewrites_watch_tasks(requesservice._enqueue_copy_refresh = enqueue_refreshawait service.mv(- "viking://resources/codeask/wiki",- "viking://resources/codeask/wiki-renamed",+ f"viking://resources{separator}codeask/wiki",+ f"viking://resources{separator}codeask/wiki-renamed",ctx=request_context,)@@ -922,7 +925,10 @@ async def test_resource_mv_watch_control_file_skips_parent_refresh(request_conte@pytest.mark.asyncio-async def test_resource_cp_coordinates_mutation_without_copying_watch_tasks(request_context):[email protected]("separator", ["/", "//"])+async def test_resource_cp_coordinates_mutation_without_copying_watch_tasks(+ request_context, separator+):source = "viking://resources/codeask/wiki"target = "viking://resources/archive/wiki"events = []@@ -948,7 +954,12 @@ async def test_resource_cp_coordinates_mutation_without_copying_watch_tasks(requservice._enqueue_copy_refresh = enqueue_refresh- result = await service.cp(source, target, recursive=True, ctx=request_context)+ result = await service.cp(+ source.replace("resources/", f"resources{separator}"),+ target.replace("resources/", f"resources{separator}"),+ recursive=True,+ ctx=request_context,+ )assert coordinator.calls == [{"account_id": "default", "uris": [source, target]}]assert viking_fs.cp_calls == [diff --git a/tests/storage/test_transfer_merge_binding.py b/tests/storage/test_transfer_merge_binding.pynew file mode 100644index 000000000..0d1e446fe--- /dev/null+++ b/tests/storage/test_transfer_merge_binding.py@@ -0,0 +1,479 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0+"""Transfer regressions using real RAGFS locking and local filesystem I/O."""++import json+from types import SimpleNamespace+from unittest.mock import AsyncMock++import pytest+import pytest_asyncio++from openviking.pyagfs import get_binding_client+from openviking.resource.watch_manager import WatchManager+from openviking.server.identity import RequestContext, Role+from openviking.storage.acl import AclAction, AclManager+from openviking.storage.collection_schemas import CollectionSchemas+from openviking.storage.vector_ids import vector_record_id+from openviking.storage.vectordb import engine as vectordb_engine+from openviking.storage.viking_fs import VikingFS+from openviking.storage.viking_vector_index_backend import VikingVectorIndexBackend+from openviking.utils.agfs_utils import RagfsBindingConfig, mount_agfs_backend+from openviking_cli.exceptions import InvalidArgumentError, PermissionDeniedError+from openviking_cli.session.user_id import UserIdentifier+from openviking_cli.utils.config.agfs_config import AGFSConfig+from openviking_cli.utils.config.vectordb_config import VectorDBBackendConfig+++def binding_fs(tmp_path):+ client_type, _ = get_binding_client()+ if client_type is None:+ pytest.skip("RAGFS native extension is unavailable")+ config = RagfsBindingConfig(agfs=AGFSConfig(path=str(tmp_path), backend="local"))+ client = client_type(None, config=config.to_binding_dict())+ mount_agfs_backend(client, config)+ return VikingFS(agfs=client)+++def root_ctx():+ return RequestContext(user=UserIdentifier.the_default_user(), role=Role(Role.ROOT))+++@pytest_asyncio.fixture+async def indexed_fs(binding_fs, tmp_path):+ if not getattr(vectordb_engine, "PersistStore", None):+ pytest.skip("local persistent vectordb engine is unavailable")+ backend = VikingVectorIndexBackend(+ config=VectorDBBackendConfig(+ backend="local", name="context", dimension=4, path=str(tmp_path / "vectors")+ )+ )+ try:+ assert await backend.create_collection(+ "context", CollectionSchemas.context_collection("context", 4)+ )+ binding_fs.vector_store = backend+ yield binding_fs, backend+ finally:+ await backend.close()+++async def seed_vector(backend, uri, content):+ return await backend.upsert(+ {+ "id": uri,+ "uri": uri,+ "level": 2,+ "vector": [0.1, 0.2, 0.3, 0.4],+ "abstract": content,+ "account_id": root_ctx().account_id,+ },+ ctx=root_ctx(),+ )++[email protected]("operation,indexed_source", [("cp", False), ("cp", True), ("mv", False)])+async def test_overwrite_preserves_target_acl_with_real_storage(+ indexed_fs, operation, indexed_source+):+ fs, backend = indexed_fs+ ctx = root_ctx()+ source, target = "viking://resources/source.txt", "viking://resources/target.txt"+ await fs.write_file_bytes(source, b"new", ctx=ctx)+ await fs.write_file_bytes(target, b"old private", ctx=ctx)+ if indexed_source:+ await seed_vector(backend, source, "new")+ await backend._upsert_many_raw(+ [+ {+ "id": "private-target",+ "uri": target,+ "account_id": ctx.account_id,+ "level": 2,+ "abstract": "old private",+ "vector": [0.9, 0.8, 0.7, 0.6],+ "acl_enabled": True,+ "acl_direct_grants": [f"3:user:{ctx.user.user_id}"],+ "acl_inherited_grants": [],+ }+ ],+ ctx=ctx,+ )+ acl = AclManager(backend)+ acl.set_enabled(ctx.account_id, True)+ fs.acl_manager = backend.acl_manager = acl++ await getattr(fs, operation)(source, target, ctx=ctx)++ assert await fs.read_file_bytes(target, ctx=ctx) == b"new"+ effective = await acl.resolve(target, ctx)+ assert effective.enabled+ assert effective.direct.principals_for(AclAction.WRITE) == {f"user:{ctx.user.user_id}"}+ records = await backend.get_context_by_uri(target, ctx=ctx)+ records = await backend.get([record["id"] for record in records], ctx=ctx)+ if indexed_source:+ assert [record["abstract"] for record in records] == ["new"]+ else:+ assert [record["id"] for record in records] == ["private-target"]+ assert [record["abstract"] for record in records] == ["old private"]+ assert records[0]["vector"] == pytest.approx([0.9, 0.8, 0.7, 0.6])+++async def test_chunk_only_copy_preserves_private_target_main_record(indexed_fs):+ fs, backend = indexed_fs+ ctx = root_ctx()+ source, target = "viking://resources/source", "viking://resources/target"+ target_file = f"{target}/file.md"+ await fs.write_file_bytes(f"{source}/file.md", b"new", ctx=ctx)+ await fs.write_file_bytes(target_file, b"old", ctx=ctx)+ await seed_vector(backend, f"{source}/file.md#chunk_0000", "new chunk")+ await seed_vector(backend, f"{target_file}#chunk_old", "obsolete chunk")+ old_main = {+ "id": "private-target",+ "uri": target_file,+ "account_id": ctx.account_id,+ "level": 2,+ "abstract": "old",+ "vector": [0.1] * 4,+ "acl_enabled": True,+ "acl_direct_grants": [f"7:user:{ctx.user.user_id}"],+ "acl_inherited_grants": [],+ }+ await backend._upsert_many_raw([old_main], ctx=ctx)+ acl = AclManager(backend)+ acl.set_enabled(ctx.account_id, True)+ fs.acl_manager = backend.acl_manager = acl+ outsider = RequestContext(user=UserIdentifier(ctx.account_id, "outsider"), role=Role(Role.USER))+ with pytest.raises(PermissionDeniedError):+ await fs.read_file_bytes(target_file, ctx=outsider)++ await fs.cp(source, target, recursive=True, ctx=ctx)++ assert await fs.read_file_bytes(target_file, ctx=ctx) == b"new"+ with pytest.raises(PermissionDeniedError):+ await fs.read_file_bytes(target_file, ctx=outsider)+ main = await backend.get(["private-target"], ctx=ctx)+ assert main[0]["abstract"] == "old"+ assert main[0]["acl_direct_grants"] == old_main["acl_direct_grants"]+ assert await backend.get_context_by_uri(f"{target_file}#chunk_0000", ctx=ctx)+ assert not await backend.get_context_by_uri(f"{target_file}#chunk_old", ctx=ctx)++[email protected]("operation", ["cp", "mv"])[email protected]("directory", [False, True])[email protected]("filename", ["C#notes.md", "file.md#chunk_0001"])+async def test_transfer_preserves_literal_hash_filenames(+ indexed_fs, operation, directory, filename+):+ fs, backend = indexed_fs+ ctx = root_ctx()+ source_dir, target_dir = "viking://resources/source", "viking://resources/target"+ source_file, target_file = f"{source_dir}/{filename}", f"{target_dir}/{filename}"+ await fs.write_file_bytes(source_file, b"new", ctx=ctx)+ await fs.write_file_bytes(target_file, b"old", ctx=ctx)+ await seed_vector(backend, source_file, "new")+ await seed_vector(backend, target_file, "old")+ source, target = (source_dir, target_dir) if directory else (source_file, target_file)++ kwargs = {"recursive": directory} if operation == "cp" else {}+ await getattr(fs, operation)(source, target, ctx=ctx, **kwargs)++ assert await fs.read_file_bytes(target_file, ctx=ctx) == b"new"+ copied = await backend.get_context_by_uri(target_file, ctx=ctx)+ copied = await backend.get([record["id"] for record in copied], ctx=ctx)+ assert [record["abstract"] for record in copied] == ["new"]+ assert bool(await backend.get_context_by_uri(source_file, ctx=ctx)) == (operation == "cp")+ if operation == "cp":+ assert await fs.read_file_bytes(source_file, ctx=ctx) == b"new"++[email protected]("operation", ["cp", "mv"])[email protected]("directory,incoming_chunk", [(False, False), (True, False), (True, True)])+async def test_transfer_protects_chunk_shaped_target_file(+ indexed_fs, operation, directory, incoming_chunk+):+ fs, backend = indexed_fs+ ctx = root_ctx()+ source, target = "viking://resources/source", "viking://resources/target"+ source_file = f"{source}/file.md" if directory else source+ target_file = f"{target}/file.md" if directory else target+ sibling = target_file + "#chunk_0001"+ await fs.write_file_bytes(source_file, b"new", ctx=ctx)+ await fs.write_file_bytes(sibling, b"private sibling", ctx=ctx)+ await seed_vector(backend, source_file, "new")+ if directory:+ await seed_vector(backend, source, "source directory")+ if incoming_chunk:+ await seed_vector(backend, source_file + "#chunk_0001", "incoming chunk")+ sibling_id = vector_record_id(ctx.account_id, sibling, 2)+ await backend._upsert_many_raw(+ [+ {+ "id": sibling_id,+ "uri": sibling,+ "account_id": ctx.account_id,+ "level": 2,+ "abstract": "private sibling",+ "vector": [0.1] * 4,+ "acl_enabled": True,+ "acl_direct_grants": [f"7:user:{ctx.user.user_id}"],+ "acl_inherited_grants": [],+ }+ ],+ ctx=ctx,+ )+ acl = AclManager(backend)+ acl.set_enabled(ctx.account_id, True)+ fs.acl_manager = backend.acl_manager = acl+ outsider = RequestContext(user=UserIdentifier(ctx.account_id, "outsider"), role=Role(Role.USER))+ with pytest.raises(PermissionDeniedError):+ await fs.read_file_bytes(sibling, ctx=outsider)+ kwargs = {"recursive": directory} if operation == "cp" else {}+ if incoming_chunk:+ with pytest.raises(InvalidArgumentError, match="chunk.*existing filesystem entry"):+ await getattr(fs, operation)(source, target, ctx=ctx, **kwargs)+ assert await fs.read_file_bytes(source_file, ctx=ctx) == b"new"+ assert await backend.get_context_by_uri(source_file, ctx=ctx)+ # Keep the approved weak rollback: vector failure removes the target+ # filesystem subtree, but must not delete or overwrite the sibling ACL.+ assert not await fs.exists(target, ctx=ctx)+ else:+ await getattr(fs, operation)(source, target, ctx=ctx, **kwargs)+ assert await fs.read_file_bytes(target_file, ctx=ctx) == b"new"+ assert await fs.read_file_bytes(sibling, ctx=ctx) == b"private sibling"+ with pytest.raises(PermissionDeniedError):+ await fs.read_file_bytes(sibling, ctx=outsider)+ saved = await backend.get([sibling_id], ctx=ctx)+ assert saved[0]["abstract"] == "private sibling"+ assert saved[0]["acl_direct_grants"] == [f"7:user:{ctx.user.user_id}"]++[email protected]("operation", ["cp", "mv"])+async def test_directory_merge_replaces_only_affected_file_vectors(+ indexed_fs, operation, monkeypatch+):+ fs, backend = indexed_fs+ ctx = root_ctx()+ source, target = "viking://resources/source", "viking://resources/target"+ for base, name, content in (+ (source, "indexed.txt", b"new"),+ (source, "unindexed.txt", b"new raw"),+ (source, "tasks/indexed.txt", b"new task"),+ (target, "indexed.txt", b"old"),+ (target, "unindexed.txt", b"old raw"),+ (target, "tasks/indexed.txt", b"old task"),+ (target, "only.txt", b"keep"),+ ):+ await fs.write_file_bytes(f"{base}/{name}", content, ctx=ctx)+ await seed_vector(backend, f"{source}/indexed.txt", "new")+ await seed_vector(backend, f"{source}/tasks/indexed.txt", "new task")+ await seed_vector(backend, f"{target}/indexed.txt", "old")+ await seed_vector(backend, f"{target}/indexed.txt#chunk-9", "old chunk")+ await seed_vector(backend, f"{target}/unindexed.txt", "old raw")+ await seed_vector(backend, f"{target}/tasks/indexed.txt", "old task")+ await seed_vector(backend, f"{target}/only.txt", "keep")++ scanned_uris: list[str] = []+ original_page = backend._strict_transfer_page++ async def counted_page(*args, **kwargs):+ page, cursor = await original_page(*args, **kwargs)+ scanned_uris.extend(record["uri"] for record in page)+ return page, cursor++ monkeypatch.setattr(backend, "_strict_transfer_page", counted_page)+ kwargs = {"recursive": True} if operation == "cp" else {}+ await getattr(fs, operation)(source, target, ctx=ctx, **kwargs)++ assert f"{target}/only.txt" not in scanned_uris+ assert f"{target}/unindexed.txt" not in scanned_uris+ copied = await backend.get_context_by_uri(f"{target}/indexed.txt", ctx=ctx)+ copied = await backend.get([record["id"] for record in copied], ctx=ctx)+ assert [record["abstract"] for record in copied] == ["new"]+ tasks = await backend.get_context_by_uri(f"{target}/tasks/indexed.txt", ctx=ctx)+ tasks = await backend.get([record["id"] for record in tasks], ctx=ctx)+ assert [record["abstract"] for record in tasks] == ["new task"]+ assert not await backend.get_context_by_uri(f"{target}/indexed.txt#chunk-9", ctx=ctx)+ stale = await backend.get_context_by_uri(f"{target}/unindexed.txt", ctx=ctx)+ stale = await backend.get([record["id"] for record in stale], ctx=ctx)+ assert [record["abstract"] for record in stale] == ["old raw"]+ assert await fs.read_file_bytes(f"{target}/unindexed.txt", ctx=ctx) == b"new raw"+ kept = await backend.get_context_by_uri(f"{target}/only.txt", ctx=ctx)+ kept = await backend.get([record["id"] for record in kept], ctx=ctx)+ assert [record["abstract"] for record in kept] == ["keep"]+ assert await fs.read_file_bytes(f"{target}/only.txt", ctx=ctx) == b"keep"+ assert bool(await backend.get_context_by_uri(f"{source}/indexed.txt", ctx=ctx)) == (+ operation == "cp"+ )+ assert bool(await backend.get_context_by_uri(f"{source}/tasks/indexed.txt", ctx=ctx)) == (+ operation == "cp"+ )++[email protected]("operation", ["cp", "mv"])[email protected]("kind", ["same_file", "child_directory", "ancestor_directory"])+async def test_transfer_rejects_overlapping_backend_paths(binding_fs, operation, kind):+ fs, ctx = binding_fs, root_ctx()+ file_uri = "viking://resources/source/file.txt"+ await fs.write_file_bytes(file_uri, b"must survive", ctx=ctx)+ if kind == "same_file":+ source, target = file_uri, "viking://resources//source/file.txt"+ elif kind == "child_directory":+ source, target = "viking://resources/source", "viking://resources//source/child"+ else:+ source, target = "viking://resources/source", "viking://resources/"+ kwargs = {"recursive": True} if operation == "cp" else {}+ with pytest.raises(InvalidArgumentError):+ await getattr(fs, operation)(source, target, ctx=ctx, **kwargs)+ assert await fs.read_file_bytes(file_uri, ctx=ctx) == b"must survive"++[email protected]("operation", ["cp", "mv"])[email protected]("alias_side", ["source", "target"])+async def test_transfer_normalizes_uri_for_files_and_vectors(indexed_fs, operation, alias_side):+ fs, backend = indexed_fs+ ctx = root_ctx()+ source, target = "viking://resources/source.txt", "viking://resources/target.txt"+ await fs.write_file_bytes(source, b"new", ctx=ctx)+ await fs.write_file_bytes(target, b"old", ctx=ctx)+ await seed_vector(backend, source, "new")+ await seed_vector(backend, target, "old")+ source_arg = source.replace("resources/", "resources//") if alias_side == "source" else source+ target_arg = target.replace("resources/", "resources//") if alias_side == "target" else target++ await getattr(fs, operation)(source_arg, target_arg, ctx=ctx)++ assert await fs.read_file_bytes(target, ctx=ctx) == b"new"+ records = await backend.get_context_by_uri(target, ctx=ctx)+ assert records+ records = await backend.get([record["id"] for record in records], ctx=ctx)+ assert [record["abstract"] for record in records] == ["new"]+ alias_id = vector_record_id(ctx.account_id, target.replace("resources/", "resources//"), 2)+ assert not await backend.get([alias_id], ctx=ctx)+ assert bool(await backend.get_context_by_uri(source, ctx=ctx)) == (operation == "cp")+++async def test_acl_failure_restores_only_moved_vectors_after_directory_merge(+ indexed_fs, monkeypatch+):+ fs, backend = indexed_fs+ ctx = root_ctx()+ source, target = "viking://resources/source", "viking://resources/target"+ await fs.write_file_bytes(f"{source}/file.txt", b"new", ctx=ctx)+ await fs.write_file_bytes(f"{target}/only.txt", b"keep", ctx=ctx)+ await seed_vector(backend, f"{source}/file.txt", "new")+ await seed_vector(backend, f"{target}/only.txt", "keep")+ monkeypatch.setattr(fs, "_ensure_access", AsyncMock())+ monkeypatch.setattr(+ fs,+ "acl_manager",+ SimpleNamespace(+ is_enabled=lambda _: True,+ refresh_context_subtree=AsyncMock(side_effect=RuntimeError("injected ACL failure")),+ ),+ )++ # Trailing slashes must not corrupt the reverse transfer manifest.+ with pytest.raises(RuntimeError, match="injected ACL failure"):+ await fs.mv(source + "/", target, ctx=ctx)++ assert await fs.read_file_bytes(f"{source}/file.txt", ctx=ctx) == b"new"+ assert await backend.get_context_by_uri(f"{source}/file.txt", ctx=ctx)+ assert not await backend.get_context_by_uri(f"{source}/only.txt", ctx=ctx)+ assert await backend.get_context_by_uri(f"{target}/only.txt", ctx=ctx)+ assert not await fs.exists(target, ctx=ctx)++[email protected]("operation", ["cp", "mv"])[email protected]("target_exists", [False, True])+async def test_directory_transfer_allows_sibling_lock_and_releases_own_locks(+ binding_fs, operation, target_exists+):+ fs = binding_fs+ ctx = root_ctx()+ source = "viking://resources/source"+ target = "viking://resources/target"+ await fs.write_file_bytes(f"{source}/file.bin", b"new", ctx=ctx)+ if target_exists:+ await fs.write_file_bytes(f"{target}/file.bin", b"old", ctx=ctx)+ await fs.write_file_bytes(f"{target}/only.bin", b"keep", ctx=ctx)+ sibling = fs._uri_to_path("viking://resources/sibling", ctx=ctx)+ sibling_lease = await fs._async_agfs.pathlock_acquire_tree(sibling)+ try:+ kwargs = {"recursive": True} if operation == "cp" else {}+ await getattr(fs, operation)(source, target, ctx=ctx, **kwargs)+ assert await fs.read_file_bytes(f"{target}/file.bin", ctx=ctx) == b"new"+ if target_exists:+ assert await fs.read_file_bytes(f"{target}/only.bin", ctx=ctx) == b"keep"+ assert await fs.exists(source, ctx=ctx) == (operation == "cp")+ # A new owner can lock the completed target; no lease is leaked.+ lease = await fs._async_agfs.pathlock_acquire_tree(fs._uri_to_path(target, ctx=ctx))+ await fs._async_agfs.pathlock_release(lease)+ finally:+ await fs._async_agfs.pathlock_release(sibling_lease)++[email protected]("operation", ["cp", "mv"])+async def test_directory_transfer_holds_both_subtrees_through_vector_phase(binding_fs, operation):+ fs = binding_fs+ ctx = root_ctx()+ source = "viking://resources/source"+ target = "viking://resources/target"+ await fs.write_file_bytes(f"{source}/file.bin", b"new", ctx=ctx)++ class VectorBoundary:+ async def copy_uri_mapping(self, **kwargs):+ await self.check_locks()++ async def update_uri_mapping(self, **kwargs):+ await self.check_locks()++ async def check_locks(self):+ for uri in (f"{source}/file.bin", f"{target}/file.bin"):+ # No owner lease is supplied: this represents a concurrent writer.+ with pytest.raises(Exception, match="lock|timed out"):+ await fs._async_agfs.pathlock_acquire_exact(fs._uri_to_path(uri, ctx=ctx))+ lease = await fs._async_agfs.pathlock_acquire_exact(+ fs._uri_to_path("viking://resources/unrelated.txt", ctx=ctx)+ )+ await fs._async_agfs.pathlock_release(lease)++ fs.vector_store = VectorBoundary()+ kwargs = {"recursive": True} if operation == "cp" else {}+ await getattr(fs, operation)(source, target, ctx=ctx, **kwargs)+ assert await fs.read_file_bytes(f"{target}/file.bin", ctx=ctx) == b"new"+++async def test_watch_persistence_overwrites_after_backup_removal_failure(binding_fs, monkeypatch):+ fs = binding_fs+ manager = WatchManager(viking_fs=fs)+ ctx = root_ctx()+ await manager._save_tasks()+ await manager._save_tasks()+ assert await fs.exists(manager.STORAGE_BAK_URI, ctx=ctx)+ real_rm = fs.rm++ async def fail_backup_removal(uri, **kwargs):+ if uri == manager.STORAGE_BAK_URI:+ raise RuntimeError("injected backup removal failure")+ return await real_rm(uri, **kwargs)++ monkeypatch.setattr(fs, "rm", fail_backup_removal)+ task = await manager.create_task(path="https://example.com/doc", watch_interval=60)+ stored = json.loads(await fs.read_file(manager.STORAGE_URI, ctx=ctx))+ assert [item["task_id"] for item in stored["tasks"]] == [task.task_id]+ backup = json.loads(await fs.read_file(manager.STORAGE_BAK_URI, ctx=ctx))+ assert backup["tasks"] == []+ assert not await fs.exists(manager.STORAGE_TMP_URI, ctx=ctx)diff --git a/tests/storage/test_vector_transfer.py b/tests/storage/test_vector_transfer.pyindex 9f68a283d..9295390b8 100644--- a/tests/storage/test_vector_transfer.py+++ b/tests/storage/test_vector_transfer.py@@ -9,16 +9,18 @@ from unittest.mock import AsyncMockimport pytest+from openviking.core.namespace import uri_partsfrom openviking.server.identity import RequestContext, Role+from openviking.storage.acl import AclManagerfrom openviking.storage.collection_schemas import CollectionSchemas-from openviking.storage.expr import And, Contains, Eq, In, Or, PathScope+from openviking.storage.expr import And, Contains, Eq, In, Or, PathScope, RawDSLfrom openviking.storage.vectordb import engine as vectordb_enginefrom openviking.storage.viking_vector_index_backend import (VectorTransferRollbackError,VikingVectorIndexBackend,_SingleAccountBackend,)-from openviking_cli.exceptions import ConflictError+from openviking_cli.exceptions import InvalidArgumentErrorfrom openviking_cli.session.user_id import UserIdentifierfrom openviking_cli.utils.config.vectordb_config import VectorDBBackendConfig@@ -55,6 +57,7 @@ class _MemoryTransferBackend(VikingVectorIndexBackend):self.partial_delete_count = 0self.drop_delete_requests = Falseself.backend_mode = "local"+ self.vector_dim = 2self.scroll_filters = []self.acl_manager = None@@ -172,6 +175,72 @@ class _AclMemoryTransferBackend(_MemoryTransferBackend):return await VikingVectorIndexBackend.upsert_many(self, data_list, ctx=ctx)+def _matches_filter(expr, record: dict[str, Any]) -> bool:+ if expr is None:+ return True+ if isinstance(expr, And):+ return all(_matches_filter(cond, record) for cond in expr.conds)+ if isinstance(expr, Or):+ return any(_matches_filter(cond, record) for cond in expr.conds)+ if isinstance(expr, Eq):+ return record.get(expr.field) == expr.value+ if isinstance(expr, In):+ value = record.get(expr.field)+ if isinstance(value, list):+ return any(item in expr.values for item in value)+ return value in expr.values+ if isinstance(expr, Contains):+ return expr.substring in str(record.get(expr.field, ""))+ if isinstance(expr, PathScope):+ root = uri_parts(expr.path)+ path = uri_parts(str(record.get(expr.field, "")))+ if path[: len(root)] != root:+ return False+ return expr.depth == -1 or len(path) - len(root) <= expr.depth+ if isinstance(expr, RawDSL) and expr.payload["op"] == "prefix":+ return str(record.get(expr.payload["field"], "")).startswith(expr.payload["prefix"])+ raise AssertionError(f"Unexpected filter expression in test: {expr!r}")+++class _RealAclMemoryTransferBackend(_MemoryTransferBackend):+ """Memory vector boundary with the production ACL manager."""++ def __init__(self, records: list[dict[str, Any]]) -> None:+ super().__init__(records)+ self.acl_manager = AclManager(self)+ self.acl_manager.set_enabled("acct", True)++ async def scroll(+ self,+ filter=None,+ limit: int = 100,+ cursor: str | None = None,+ output_fields=None,+ *,+ ctx: RequestContext,+ ) -> tuple[list[dict[str, Any]], str | None]:+ del output_fields, ctx+ self.scroll_filters.append(filter)+ offset = int(cursor or 0)+ ordered = [+ dict(self.records[key])+ for key in sorted(self.records)+ if _matches_filter(filter, self.records[key])+ ]+ page = ordered[offset : offset + limit]+ next_cursor = str(offset + limit) if offset + limit < len(ordered) else None+ return page, next_cursor++ async def _strict_transfer_count(self, ctx, filter):+ del ctx+ return sum(_matches_filter(filter, record) for record in self.records.values())++ async def upsert_many(+ self, data_list: list[dict[str, Any]], *, ctx: RequestContext+ ) -> list[str]:+ return await VikingVectorIndexBackend.upsert_many(self, data_list, ctx=ctx)++def _records_under(backend: _MemoryTransferBackend, uri: str, *, recursive: bool = True) -> list[dict[str, Any]]:@@ -383,24 +452,394 @@ async def test_volcengine_transfer_scope_avoids_unsupported_contains_filter():@pytest.mark.asyncio-async def test_copy_uri_mapping_rejects_preexisting_target_before_writing():[email protected]("transfer_method", ["copy_uri_mapping", "update_uri_mapping"])+async def test_uri_mapping_overwrites_affected_target_and_removes_obsolete_chunks(+ transfer_method,+):+ source = "viking://resources/src.md"+ target = "viking://resources/dst.md"backend = _MemoryTransferBackend([- _record("source", "viking://resources/src.md"),- _record("target", "viking://resources/dst.md#chunk_0001"),+ _record("source-file", source, content="new-file"),+ _record("source-chunk", f"{source}#chunk_0001", content="new-chunk"),+ _record("old-target-file", target, content="old-file"),+ _record("old-target-chunk", f"{target}#chunk_0001", content="old-chunk"),+ _record("obsolete-target-chunk", f"{target}#chunk_0002", content="obsolete"),])- with pytest.raises(ConflictError, match="target vector scope already exists"):- await backend.copy_uri_mapping(- _ctx(),- "viking://resources/src.md",- "viking://resources/dst.md",- recursive=False,- )+ result = await getattr(backend, transfer_method)(_ctx(), source, target, recursive=False)++ targets = sorted(_records_under(backend, target), key=lambda record: record["uri"])+ assert result.scanned == 2+ assert result.written == 2+ assert [(record["uri"], record["content"]) for record in targets] == [+ (target, "new-file"),+ (f"{target}#chunk_0001", "new-chunk"),+ ]++[email protected]("transfer_method", ["copy_uri_mapping", "update_uri_mapping"])+async def test_uri_mapping_preserves_target_records_for_unaffected_entries(transfer_method):+ source = "viking://resources/src"+ target = "viking://resources/dst"+ unique_target = f"{target}/unique.md"+ backend = _MemoryTransferBackend(+ [+ _record("source-root", source),+ _record("source-a", f"{source}/a.md", content="new-a"),+ _record("old-target-root", target),+ _record("old-target-a", f"{target}/a.md", content="old-a"),+ _record("unique-target", unique_target, content="keep-me"),+ ]+ )++ await getattr(backend, transfer_method)(_ctx(), source, target, recursive=True)++ target_records = _records_under(backend, target)+ assert (+ next(record for record in target_records if record["uri"] == unique_target)["content"]+ == "keep-me"+ )+ assert (+ next(record for record in target_records if record["uri"] == f"{target}/a.md")["content"]+ == "new-a"+ )++[email protected]("transfer_method", ["copy_uri_mapping", "update_uri_mapping"])[email protected]("backend_mode", ["local", "volcengine"])+async def test_merge_target_scan_excludes_unrelated_subtrees(+ transfer_method, backend_mode, monkeypatch+):+ source, target = "viking://resources/source", "viking://resources/target"+ affected = f"{target}/a.md"+ source_records = [_record("source-root", source), _record("source-file", f"{source}/a.md")]+ old_records = [_record("old-root", target), _record("old-file", affected)] + [+ _record(f"old-chunk-{i}", f"{affected}#chunk_{i}") for i in range(205)+ ]+ unique_records = [+ _record(f"unique-{i}", f"{target}/unrelated/file-{i}.md") for i in range(1000)+ ]+ backend = _RealAclMemoryTransferBackend(source_records + old_records + unique_records)+ backend.acl_manager = None+ backend.backend_mode = backend_mode+ original_page = backend._strict_transfer_page+ read_ids: list[str] = []++ async def counted_page(*args, **kwargs):+ page, cursor = await original_page(*args, **kwargs)+ read_ids.extend(str(record["id"]) for record in page)+ return page, cursor++ monkeypatch.setattr(backend, "_strict_transfer_page", counted_page)+ result = await getattr(backend, transfer_method)(+ _ctx(), source, target, recursive=True, source_uris=[source, f"{source}/a.md"]+ )++ assert not set(read_ids).intersection(record["id"] for record in unique_records)+ assert {record["id"] for record in old_records}.issubset(read_ids)+ assert result.written == 2+ assert not set(backend.records).intersection(record["id"] for record in old_records)+ assert all(backend.records[record["id"]] == record for record in unique_records)++[email protected]("transfer_method", ["copy_uri_mapping", "update_uri_mapping"])+async def test_target_replacement_cleans_chunks_across_entry_batches(transfer_method):+ source, target = "viking://resources/source", "viking://resources/target"+ source_records = [_record(f"source-{i}", f"{source}/file-{i}.md") for i in range(101)]+ old_chunks = [_record(f"old-{i}", f"{target}/file-{i}.md#chunk_old") for i in range(101)]+ backend = _RealAclMemoryTransferBackend(source_records + old_chunks)+ backend.acl_manager = None++ result = await getattr(backend, transfer_method)(_ctx(), source, target, recursive=True)++ assert result.written == 101+ assert len(_records_under(backend, target)) == 101+ assert not set(backend.records).intersection(record["id"] for record in old_chunks)++[email protected]("transfer_method", ["copy_uri_mapping", "update_uri_mapping"])+async def test_uri_mapping_empty_source_file_preserves_old_target_vectors(transfer_method):+ source = "viking://resources/src.md"+ target = "viking://resources/dst.md"+ backend = _MemoryTransferBackend(+ [+ _record("old-target-file", target),+ _record("old-target-chunk", f"{target}#chunk_0001"),+ _record("outside", "viking://resources/outside.md"),+ ]+ )++ original_records = dict(backend.records)++ result = await getattr(backend, transfer_method)(_ctx(), source, target, recursive=False)++ assert result.scanned == 0+ assert result.written == 0+ assert result.deleted == 0+ assert backend.records == original_records+ assert backend.delete_calls == backend.upsert_calls == 0++[email protected]("transfer_method", ["copy_uri_mapping", "update_uri_mapping"])+async def test_uri_mapping_write_failure_keeps_source_without_restoring_old_target(+ transfer_method,+):+ source = "viking://resources/src.md"+ target = "viking://resources/dst.md"+ backend = _MemoryTransferBackend(+ [+ _record("source-file", source),+ _record("source-chunk", f"{source}#chunk_0001"),+ _record("old-target-file", target, content="do-not-restore"),+ _record("old-target-chunk", f"{target}#chunk_0009", content="do-not-restore"),+ ]+ )+ backend.fail_upsert_at = 2++ with pytest.raises(RuntimeError, match="injected vector write failure"):+ await getattr(backend, transfer_method)(_ctx(), source, target, recursive=False)++ assert len(_records_under(backend, source, recursive=False)) == 2+ assert _records_under(backend, target, recursive=False) == []++[email protected]("transfer_method", ["copy_uri_mapping", "update_uri_mapping"])+async def test_uri_mapping_source_uris_limits_source_and_target_replacement_scope(+ transfer_method,+):+ source = "viking://resources/src"+ target = "viking://resources/dst"+ source_b = f"{source}/b.md"+ target_b = f"{target}/b.md"+ backend = _MemoryTransferBackend(+ [+ _record("source-root", source, content="new-root"),+ _record("source-a", f"{source}/a.md", content="new-a"),+ _record("source-b", source_b, content="source-b-stays"),+ _record("old-target-root", target, content="old-root"),+ _record("old-target-a", f"{target}/a.md", content="old-a"),+ _record("old-target-b", target_b, content="target-b-stays"),+ ]+ )++ result = await getattr(backend, transfer_method)(+ _ctx(),+ source,+ target,+ recursive=True,+ source_uris=[source, f"{source}/a.md"],+ )++ assert result.scanned == 2+ assert (+ next(record for record in backend.records.values() if record["uri"] == target_b)["content"]+ == "target-b-stays"+ )+ assert (+ next(record for record in backend.records.values() if record["uri"] == source_b)["content"]+ == "source-b-stays"+ )+++async def test_copy_over_private_target_preserves_target_acl_for_new_chunks():+ source = "viking://resources/src.md"+ target = "viking://resources/dst.md"+ backend = _RealAclMemoryTransferBackend(+ [+ _record("source-file", source),+ _record("source-chunk", f"{source}#chunk_0001"),+ _record(+ "target-file",+ target,+ acl_enabled=True,+ acl_direct_grants=["7:user:bob"],+ acl_inherited_grants=[],+ ),+ ]+ )++ await backend.copy_uri_mapping(_ctx(), source, target, recursive=False)++ targets = _records_under(backend, target, recursive=False)+ assert {record["uri"] for record in targets} == {+ target,+ f"{target}#chunk_0001",+ }+ assert all(record["acl_direct_grants"] == ["7:user:bob"] for record in targets)+ assert all(record["acl_inherited_grants"] == [] for record in targets)+++async def test_copy_target_without_direct_acl_keeps_parent_inheritance():+ source = "viking://resources/src.md"+ parent = "viking://resources/team"+ target = f"{parent}/dst.md"+ inherited = ["3:group:editors"]+ backend = _RealAclMemoryTransferBackend(+ [+ _record("source-file", source),+ _record(+ "parent",+ parent,+ acl_enabled=True,+ acl_direct_grants=inherited,+ acl_inherited_grants=[],+ ),+ _record(+ "target-file",+ target,+ acl_enabled=True,+ acl_direct_grants=[],+ acl_inherited_grants=inherited,+ ),+ ]+ )++ await backend.copy_uri_mapping(_ctx(), source, target, recursive=False)++ copied = _records_under(backend, target, recursive=False)+ assert len(copied) == 1+ assert copied[0]["acl_direct_grants"] == []+ assert copied[0]["acl_inherited_grants"] == inherited++[email protected]("transfer_method", ["copy_uri_mapping", "update_uri_mapping"])[email protected]("recursive,level", [(False, 2), (True, 0)])[email protected]("private", [False, True])+async def test_unindexed_source_preserves_target_records_and_acl(+ transfer_method, recursive, level, private+):+ source = "viking://resources/source"+ target = "viking://resources/target"+ backend = _RealAclMemoryTransferBackend(+ [+ _record(+ "target-record",+ target,+ level=level,+ abstract="old abstract",+ acl_enabled=private,+ acl_direct_grants=["7:user:bob"] if private else [],+ acl_inherited_grants=[],+ ),+ _record("target-chunk", f"{target}#chunk_0001"),+ ]+ )+ original_records = dict(backend.records)++ result = await getattr(backend, transfer_method)(+ _ctx(), source, target, recursive=recursive, source_uris=[source]+ )++ assert result.scanned == result.written == result.deleted == 0+ assert backend.records == original_records+ assert backend.delete_calls == backend.upsert_calls == 0+ assert backend.acl_manager is not None+ effective = await backend.acl_manager.resolve(target, _ctx())+ assert effective.context_fields() == {+ "acl_enabled": private,+ "acl_direct_grants": ["7:user:bob"] if private else [],+ "acl_inherited_grants": [],+ }+++async def test_copy_private_entry_skips_shared_resource_acl_resolution():+ source = "viking://user/alice/resources/src.md"+ target = "viking://user/alice/resources/dst.md"+ backend = _RealAclMemoryTransferBackend(+ [+ _record("source-file", source, content="new"),+ _record("target-file", target, content="old"),+ ]+ )++ await backend.copy_uri_mapping(_ctx(), source, target, recursive=False)++ copied = _records_under(backend, target, recursive=False)+ assert len(copied) == 1+ assert copied[0]["content"] == "new"+++async def test_copy_directory_preserves_root_acl_and_inherits_it_to_new_entries():+ source = "viking://resources/source"+ target = "viking://resources/target"+ source_child = f"{source}/child"+ source_file = f"{source_child}/file.md"+ target_child = f"{target}/child"+ target_file = f"{target_child}/file.md"+ unique_target = f"{target}/unique.md"+ root_acl = ["7:user:bob"]+ backend = _RealAclMemoryTransferBackend(+ [+ _record("source-root", source, level=0),+ _record("source-child", source_child, level=0),+ _record("source-file", source_file),+ _record(+ "target-root",+ target,+ level=0,+ acl_enabled=True,+ acl_direct_grants=root_acl,+ acl_inherited_grants=[],+ ),+ _record(+ "unique-target",+ unique_target,+ acl_enabled=True,+ acl_direct_grants=["3:user:carol"],+ acl_inherited_grants=root_acl,+ ),+ ]+ )++ await backend.copy_uri_mapping(+ _ctx(),+ source,+ target,+ recursive=True,+ source_uris=[source, source_child, source_file],+ )++ by_uri = {record["uri"]: record for record in _records_under(backend, target)}+ assert by_uri[target]["acl_direct_grants"] == root_acl+ assert by_uri[target]["acl_inherited_grants"] == []+ for uri in (target_child, target_file):+ assert by_uri[uri]["acl_direct_grants"] == []+ assert by_uri[uri]["acl_inherited_grants"] == root_acl+ assert by_uri[unique_target]["acl_direct_grants"] == ["3:user:carol"]+ assert by_uri[unique_target]["acl_inherited_grants"] == root_acl++[email protected]("transfer_method", ["copy_uri_mapping", "update_uri_mapping"])+ ("source", "target"),+ [+ ("viking://resources/same", "viking://resources/same"),+ ("viking://resources/parent", "viking://resources/parent/child"),+ ("viking://resources/parent/child", "viking://resources/parent"),+ ],+)+async def test_uri_mapping_rejects_equal_or_containing_scopes(transfer_method, source, target):+ backend = _MemoryTransferBackend([_record("source", source)])++ with pytest.raises(InvalidArgumentError):+ await getattr(backend, transfer_method)(_ctx(), source, target, recursive=True)assert backend.upsert_calls == 0- assert len(_records_under(backend, "viking://resources/src.md")) == 1+ assert list(backend.records) == ["source"]@pytest.mark.asyncio@@ -444,6 +883,33 @@ async def test_copy_uri_mapping_reports_actual_residual_after_cleanup_failure():assert len(_records_under(backend, "viking://resources/dst")) == 1[email protected]("transfer_method", ["copy_uri_mapping", "update_uri_mapping"])+async def test_uri_mapping_cleanup_residual_excludes_unaffected_target_records(+ transfer_method,+):+ source = "viking://resources/src"+ target = "viking://resources/dst"+ unique_target = f"{target}/unique.md"+ backend = _MemoryTransferBackend(+ [+ _record("source-root", source),+ _record("source-a", f"{source}/a.md"),+ _record("source-b", f"{source}/b.md"),+ _record("unique-target", unique_target),+ ]+ )+ backend.fail_upsert_at = 3+ backend.fail_delete_at = 1+ backend.partial_delete_count = 1++ with pytest.raises(VectorTransferRollbackError) as exc_info:+ await getattr(backend, transfer_method)(_ctx(), source, target, recursive=True)++ assert exc_info.value.residual_count == 1+ assert any(record["uri"] == unique_target for record in backend.records.values())++@pytest.mark.asyncioasync def test_copy_uri_mapping_reports_rollback_when_delete_returns_zero():source = "viking://resources/src"@@ -583,3 +1049,43 @@ async def test_update_uri_mapping_returns_empty_result_when_source_has_no_vectorassert result.scanned == 0assert result.written == 0assert result.deleted == 0++[email protected]("operation", ["copy_uri_mapping", "update_uri_mapping"])[email protected]("mode", ["local", "volcengine"])[email protected]("incoming_chunk", [False, True])+async def test_target_chunk_candidate_respects_filesystem_entry(operation, mode, incoming_chunk):+ source, target = "viking://resources/source.md", "viking://resources/target.md"+ sibling = target + "#chunk-1"+ records = [_record("source", source), _record("private", sibling)]+ if incoming_chunk:+ records.append(_record("source-chunk", source + "#chunk-1"))+ backend = _MemoryTransferBackend(records)+ backend.backend_mode = mode+ exists = AsyncMock(return_value=True)+ if incoming_chunk:+ with pytest.raises(InvalidArgumentError, match="chunk.*existing filesystem entry"):+ await getattr(backend, operation)(_ctx(), source, target, target_entry_exists=exists)+ assert backend.delete_calls == backend.upsert_calls == 0+ else:+ await getattr(backend, operation)(_ctx(), source, target, target_entry_exists=exists)+ assert any(record["uri"] == target for record in backend.records.values())+ assert backend.records["private"] == records[1]+ exists.assert_awaited_once_with(sibling)+++async def test_target_entry_lookup_failure_precedes_vector_mutation():+ source, target = "viking://resources/source.md", "viking://resources/target.md"+ backend = _MemoryTransferBackend(+ [_record("source", source), _record("old", target + "#chunk-1")]+ )+ with pytest.raises(RuntimeError, match="stat failed"):+ await backend.copy_uri_mapping(+ _ctx(),+ source,+ target,+ target_entry_exists=AsyncMock(side_effect=RuntimeError("stat failed")),+ )+ assert backend.delete_calls == backend.upsert_calls == 0diff --git a/tests/storage/test_viking_fs_cp.py b/tests/storage/test_viking_fs_cp.pyindex 5963dea12..47814f6d6 100644--- a/tests/storage/test_viking_fs_cp.py+++ b/tests/storage/test_viking_fs_cp.py@@ -10,10 +10,9 @@ import pytestfrom openviking.server.identity import RequestContext, Rolefrom openviking.storage.abstract_overview import parse_abstract_overview+from openviking.storage.acl import AclActionfrom openviking.storage.viking_fs import VikingFS-from openviking.storage.viking_fs._ops import TransferRollbackErrorfrom openviking_cli.exceptions import (- ConflictError,FailedPreconditionError,InvalidArgumentError,NotFoundError,@@ -366,7 +365,8 @@ async def test_cp_directory_preserves_empty_hidden_and_binary_entries(monkeypatcacquire = next(event for event in agfs.events if event[0] == "acquire-batch")assert acquire[1] == [- {"path": "/local/acct/resources", "kind": "tree"},+ {"path": "/local/acct/resources/source", "kind": "tree"},+ {"path": "/local/acct/resources/target", "kind": "tree"},]assert "/local/acct/resources/target/empty" in agfs.directoriesassert agfs.files["/local/acct/resources/target/.hidden"] == b"hidden"@@ -377,6 +377,12 @@ async def test_cp_directory_preserves_empty_hidden_and_binary_entries(monkeypatc"viking://resources/target",recursive=True,ctx=_ctx(),+ source_uris=[+ "viking://resources/source",+ "viking://resources/source/.hidden",+ "viking://resources/source/data.bin",+ "viking://resources/source/empty",+ ],)@@ -452,7 +458,7 @@ Back to viking://resources/source/data.bin.@pytest.mark.asyncio-async def test_cp_directory_locks_both_distinct_parent_trees(monkeypatch):+async def test_cp_directory_locks_only_source_and_target_trees(monkeypatch):agfs = _DirectoryCopyAGFS()agfs.directories = {"/local/acct/resources",@@ -476,26 +482,26 @@ async def test_cp_directory_locks_both_distinct_parent_trees(monkeypatch):acquire = next(event for event in agfs.events if event[0] == "acquire-batch")assert acquire[1] == [- {"path": "/local/acct/resources/source-parent", "kind": "tree"},- {"path": "/local/acct/resources/target-parent", "kind": "tree"},+ {"path": "/local/acct/resources/source-parent/source", "kind": "tree"},+ {"path": "/local/acct/resources/target-parent/target", "kind": "tree"},]assert agfs.exact_leases == []@pytest.mark.asyncio-async def test_cp_rejects_existing_target_without_copying(monkeypatch):- agfs = _CopyAGFS(target_exists=True)[email protected]("operation", ["cp", "mv"])+async def test_transfer_overwrites_existing_file(monkeypatch, operation):+ agfs = _DirectoryCopyAGFS()+ agfs.files = {+ "/local/acct/resources/source.md": b"new",+ "/local/acct/resources/target.md": b"old",+ }fs = _viking_fs(monkeypatch, agfs)- monkeypatch.setattr(fs, "_copy_vector_store_uris", AsyncMock(), raising=False)-- with pytest.raises(ConflictError, match="target already exists"):- await fs.cp(- "viking://resources/source.md",- "viking://resources/target.md",- ctx=_ctx(),- )-- assert not any(event[0] == "cp" for event in agfs.events)+ await getattr(fs, operation)(+ "viking://resources/source.md", "viking://resources/target.md", ctx=_ctx()+ )+ assert agfs.files["/local/acct/resources/target.md"] == b"new"+ assert ("/local/acct/resources/source.md" in agfs.files) == (operation == "cp")@pytest.mark.asyncio@@ -541,7 +547,7 @@ async def test_mv_rejects_missing_target_parent_before_locking(monkeypatch):@pytest.mark.asyncio-async def test_mv_removes_partial_target_when_agfs_copy_fails(monkeypatch):+async def test_mv_keeps_partial_target_when_agfs_copy_fails(monkeypatch):agfs = _CopyAGFS(fail_copy=True)fs = _viking_fs(monkeypatch, agfs)vector_move = AsyncMock()@@ -554,10 +560,10 @@ async def test_mv_removes_partial_target_when_agfs_copy_fails(monkeypatch):ctx=_ctx(),)- assert any(+ assert not any(event[0:3] == ("rm", "/local/acct/resources/target.md", False) for event in agfs.events)- assert not agfs.target_exists+ assert agfs.target_existsvector_move.assert_not_awaited()@@ -586,7 +592,7 @@ async def test_cp_removes_target_file_when_vector_copy_fails(monkeypatch):@pytest.mark.asyncio-async def test_cp_removes_partial_target_when_agfs_copy_fails(monkeypatch):+async def test_cp_keeps_partial_target_when_agfs_copy_fails(monkeypatch):agfs = _CopyAGFS(fail_copy=True)fs = _viking_fs(monkeypatch, agfs)vector_copy = AsyncMock()@@ -599,15 +605,15 @@ async def test_cp_removes_partial_target_when_agfs_copy_fails(monkeypatch):ctx=_ctx(),)- assert any(+ assert not any(event[0:3] == ("rm", "/local/acct/resources/target.md", False) for event in agfs.events)- assert not agfs.target_exists+ assert agfs.target_existsvector_copy.assert_not_awaited()@pytest.mark.asyncio-async def test_mv_restores_source_and_removes_target_when_source_delete_fails(monkeypatch):+async def test_mv_keeps_target_when_source_delete_fails(monkeypatch):agfs = _MoveRollbackAGFS()fs = _viking_fs(monkeypatch, agfs)vector_move = AsyncMock()@@ -620,15 +626,12 @@ async def test_mv_restores_source_and_removes_target_when_source_delete_fails(moctx=_ctx(),)- assert agfs.paths == {"/local/acct/resources/source.md"}+ assert agfs.paths == {"/local/acct/resources/target.md"}assert vector_move.await_args_list[0].args == ("viking://resources/source.md","viking://resources/target.md",)- assert vector_move.await_args_list[1].args == (- "viking://resources/target.md",- "viking://resources/source.md",- )+ assert vector_move.await_count == 1@pytest.mark.asyncio@@ -668,7 +671,7 @@ async def test_mv_restores_vectors_before_removing_target_when_acl_refresh_fails@pytest.mark.asyncio-async def test_mv_keeps_target_when_vector_restore_after_acl_refresh_fails(monkeypatch):+async def test_mv_still_cleans_target_when_vector_restore_after_acl_refresh_fails(monkeypatch):agfs = _CopyAGFS()fs = _viking_fs(monkeypatch, agfs)source_uri = "viking://resources/source.md"@@ -690,54 +693,31 @@ async def test_mv_keeps_target_when_vector_restore_after_acl_refresh_fails(monke)monkeypatch.setattr(fs, "_update_vector_store_uris", move_vectors)- with pytest.raises(TransferRollbackError) as exc_info:+ with pytest.raises(RuntimeError, match="ACL refresh failed"):await fs.mv(source_uri, target_uri, ctx=_ctx())-- assert exc_info.value.phase == "vector_restore"- assert exc_info.value.residual_uri == target_uri- assert isinstance(exc_info.value.__cause__, RuntimeError)- assert str(exc_info.value.__cause__) == "ACL refresh failed"assert vector_uris == {target_uri}- assert agfs.target_exists- assert not any(+ assert not agfs.target_exists+ assert any(event[0:3] == ("rm", "/local/acct/resources/target.md", False) for event in agfs.events)@pytest.mark.asyncio-async def test_mv_directory_rolls_back_partial_source_delete_under_parent_tree(monkeypatch):+async def test_mv_directory_keeps_target_after_partial_source_delete(monkeypatch):agfs = _DirectoryMoveRollbackAGFS()fs = _viking_fs(monkeypatch, agfs)vector_move = AsyncMock()monkeypatch.setattr(fs, "_update_vector_store_uris", vector_move)-- with pytest.raises(RuntimeError, match="injected partial source delete failure"):- await fs.mv(- "viking://resources/source",- "viking://resources/target",- ctx=_ctx(),- )-- assert agfs.directories == {- "/local/acct/resources",- "/local/acct/resources/source",- "/local/acct/resources/source/empty",- }+ with pytest.raises(RuntimeError, match="partial source delete"):+ await fs.mv("viking://resources/source", "viking://resources/target", ctx=_ctx())assert agfs.files == {"/local/acct/resources/source/.hidden": b"hidden",- "/local/acct/resources/source/data.bin": b"\x00\xff",+ "/local/acct/resources/target/.hidden": b"hidden",+ "/local/acct/resources/target/data.bin": b"\x00\xff",}- acquire = next(event for event in agfs.events if event[0] == "acquire-batch")- assert acquire[1] == [{"path": "/local/acct/resources", "kind": "tree"}]- assert agfs.exact_leases == []- assert vector_move.await_args_list[0].args[:2] == (- "viking://resources/source",- "viking://resources/target",- )- assert vector_move.await_args_list[1].args[:2] == (- "viking://resources/target",- "viking://resources/source",- )+ assert "/local/acct/resources/target/empty" in agfs.directories+ assert "/local/acct/resources/source/empty" not in agfs.directories+ assert vector_move.await_count == 1@pytest.mark.asyncio@@ -796,7 +776,7 @@ async def test_mv_returns_diagnostic_transfer_summary(monkeypatch):@pytest.mark.asyncio-async def test_mv_reports_target_residual_when_vector_failure_cleanup_fails(monkeypatch):+async def test_mv_preserves_original_error_when_vector_failure_cleanup_fails(monkeypatch):agfs = _CopyAGFS()fs = _viking_fs(monkeypatch, agfs)monkeypatch.setattr(@@ -810,41 +790,24 @@ async def test_mv_reports_target_residual_when_vector_failure_cleanup_fails(monkAsyncMock(side_effect=RuntimeError("target cleanup failed")),)- with pytest.raises(TransferRollbackError) as exc_info:- await fs.mv(- "viking://resources/source.md",- "viking://resources/target.md",- ctx=_ctx(),- )-- assert exc_info.value.phase == "target_cleanup"- assert exc_info.value.residual_uri == "viking://resources/target.md"- assert isinstance(exc_info.value.__cause__, RuntimeError)- assert str(exc_info.value.__cause__) == "vector move failed"+ with pytest.raises(RuntimeError, match="vector move failed"):+ await fs.mv("viking://resources/source.md", "viking://resources/target.md", ctx=_ctx())+ assert agfs.target_exists+ assert not any(+ event[0:3] == ("rm", "/local/acct/resources/source.md", False) for event in agfs.events+ )@pytest.mark.asyncio-async def test_mv_reports_source_residual_when_source_restore_fails(monkeypatch):+async def test_mv_does_not_copy_back_after_source_delete_failure(monkeypatch):agfs = _MoveRollbackAGFS()fs = _viking_fs(monkeypatch, agfs)- monkeypatch.setattr(fs, "_update_vector_store_uris", AsyncMock())- monkeypatch.setattr(- fs,- "_copy_agfs_entry",- AsyncMock(side_effect=[1, RuntimeError("source restore copy failed")]),- )-- with pytest.raises(TransferRollbackError) as exc_info:- await fs.mv(- "viking://resources/source.md",- "viking://resources/target.md",- ctx=_ctx(),- )-- assert exc_info.value.phase == "source_restore"- assert exc_info.value.residual_uri == "viking://resources/source.md"- assert isinstance(exc_info.value.__cause__, RuntimeError)- assert str(exc_info.value.__cause__) == "injected source delete failure"+ with pytest.raises(RuntimeError, match="injected source delete failure"):+ await fs.mv("viking://resources/source.md", "viking://resources/target.md", ctx=_ctx())+ assert [event[1:3] for event in agfs.events if event[0] == "cp"] == [+ ("/local/acct/resources/source.md", "/local/acct/resources/target.md")+ ]+ assert agfs.paths == {"/local/acct/resources/target.md"}@pytest.mark.asyncio@@ -864,3 +827,103 @@ async def test_mv_rejects_same_path_and_source_subtree(monkeypatch, source, targawait fs.mv(source, target, ctx=_ctx())assert not any(event[0] == "acquire-batch" for event in agfs.events)++[email protected]("operation", ["cp", "mv"])+async def test_transfer_merges_directory_preserving_target_only_files(monkeypatch, operation):+ agfs = _DirectoryCopyAGFS()+ agfs.directories.update({"/local/acct/resources/target", "/local/acct/resources/target/empty"})+ agfs.files.update(+ {+ "/local/acct/resources/target/data.bin": b"old",+ "/local/acct/resources/target/only.txt": b"keep",+ }+ )+ fs = _viking_fs(monkeypatch, agfs)+ kwargs = {"recursive": True} if operation == "cp" else {}+ await getattr(fs, operation)(+ "viking://resources/source", "viking://resources/target", ctx=_ctx(), **kwargs+ )+ assert agfs.files["/local/acct/resources/target/data.bin"] == b"\x00\xff"+ assert agfs.files["/local/acct/resources/target/only.txt"] == b"keep"+ assert agfs.files["/local/acct/resources/target/.hidden"] == b"hidden"+ assert ("/local/acct/resources/source" in agfs.directories) == (operation == "cp")++[email protected]("operation", ["cp", "mv"])[email protected]("nested", [False, True])+async def test_transfer_rejects_type_conflicts_before_copy(monkeypatch, operation, nested):+ agfs = _DirectoryCopyAGFS()+ if nested:+ agfs.directories.update(+ {"/local/acct/resources/target", "/local/acct/resources/target/data.bin"}+ )+ else:+ agfs.files["/local/acct/resources/target"] = b"target is a file"+ fs = _viking_fs(monkeypatch, agfs)+ before = dict(agfs.files)+ kwargs = {"recursive": True} if operation == "cp" else {}+ with pytest.raises(InvalidArgumentError):+ await getattr(fs, operation)(+ "viking://resources/source", "viking://resources/target", ctx=_ctx(), **kwargs+ )+ assert agfs.files == before+ assert not any(event[0] in {"cp", "rm"} for event in agfs.events)++[email protected]("operation", ["cp", "mv"])+async def test_transfer_rejects_target_ancestor(monkeypatch, operation):+ fs = _viking_fs(monkeypatch, _DirectoryCopyAGFS())+ kwargs = {"recursive": True} if operation == "cp" else {}+ with pytest.raises(InvalidArgumentError):+ await getattr(fs, operation)(+ "viking://resources/source", "viking://resources", ctx=_ctx(), **kwargs+ )++[email protected]("operation", ["cp", "mv"])[email protected]("child", ["data.bin", "tasks/private.txt", "_system/private.txt"])+async def test_transfer_checks_child_target_write_permission_before_copy(+ monkeypatch, operation, child+):+ agfs = _DirectoryCopyAGFS()+ agfs.directories.add("/local/acct/resources/target")+ if "/" in child:+ name = child.split("/")[0]+ agfs.directories.update(+ {f"/local/acct/resources/source/{name}", f"/local/acct/resources/target/{name}"}+ )+ agfs.files[f"/local/acct/resources/source/{child}"] = b"new"+ agfs.files[f"/local/acct/resources/target/{child}"] = b"protected"+ fs = _viking_fs(monkeypatch, agfs)++ async def ensure_access(uri, ctx, *, action=AclAction.READ):+ if uri == f"viking://resources/target/{child}" and action == AclAction.WRITE:+ raise PermissionDeniedError("protected child", resource=uri)++ monkeypatch.setattr(fs, "_ensure_access", ensure_access)+ before = dict(agfs.files)+ kwargs = {"recursive": True} if operation == "cp" else {}+ with pytest.raises(PermissionDeniedError, match="protected child"):+ await getattr(fs, operation)(+ "viking://resources/source", "viking://resources/target", ctx=_ctx(), **kwargs+ )+ assert agfs.files == before+ assert not any(event[0] in {"cp", "rm"} for event in agfs.events)+++async def test_mv_preserves_missing_source_error_when_orphan_cleanup_fails(monkeypatch):+ agfs = _CopyAGFS()+ fs = _viking_fs(monkeypatch, agfs)+ monkeypatch.setattr(fs, "_copy_for_mv", AsyncMock(side_effect=FileNotFoundError("source gone")))+ monkeypatch.setattr(+ fs, "_delete_from_vector_store", AsyncMock(side_effect=RuntimeError("cleanup failed"))+ )+ with pytest.raises(FileNotFoundError, match="source gone"):+ await fs.mv("viking://resources/source.md", "viking://resources/target.md", ctx=_ctx())+ assert not any(event[0] == "rm" for event in agfs.events)diff --git a/tests/storage/test_viking_fs_write_locking.py b/tests/storage/test_viking_fs_write_locking.pyindex 7af478183..dd0868df4 100644--- a/tests/storage/test_viking_fs_write_locking.py+++ b/tests/storage/test_viking_fs_write_locking.py@@ -74,6 +74,9 @@ class _AsyncMoveAGFS:return {"isDir": True}raise FileNotFoundError(path)+ async def ls(self, path):+ return []+async def pathlock_acquire_batch(self,requests,@@ -241,8 +244,8 @@ async def test_mv_extends_outer_lease_with_owned_capability(monkeypatch):@pytest.mark.asyncio-async def test_directory_mv_locks_stable_source_and_destination_parents(monkeypatch):- """Directory move keeps both stable parents locked through copy and cleanup."""+async def test_directory_mv_locks_source_and_destination_subtrees(monkeypatch):+ """Directory move covers both subtrees without locking unrelated siblings."""fake = _AsyncMoveAGFS(source_is_dir=True)fs = VikingFS(agfs=_FakeAGFS())fs._async_agfs = fake # type: ignore[assignment]@@ -269,14 +272,14 @@ async def test_directory_mv_locks_stable_source_and_destination_parents(monkeypa)assert fake.acquire_calls[0][0] == [- {"path": "/local/default/temp", "kind": "tree"},- {"path": "/local/default/resources", "kind": "tree"},+ {"path": "/local/default/temp/source", "kind": "tree"},+ {"path": "/local/default/resources/target", "kind": "tree"},]@pytest.mark.asyncio-async def test_directory_mv_reuses_parent_tree_for_failed_copy_cleanup(monkeypatch):- """Failed directory move cleans the target under the existing parent Tree lease."""+async def test_directory_mv_reuses_target_tree_for_vector_failure_cleanup(monkeypatch):+ """Vector failure cleans the target under the existing destination Tree lease."""fake = _AsyncMoveAGFS(source_is_dir=True)fs = VikingFS(agfs=_FakeAGFS())fs._async_agfs = fake # type: ignore[assignment]