Skip to content

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

32.1 KB
Raw Blame
# 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.service.fs_service import FSService
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.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


class _EnabledAclConfig:
    async def get_account(self, account_id: str, field: str):
        del account_id, field
        return SimpleNamespace(enabled=True)


@pytest.fixture
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(vector_backend_factory, binding_fs, tmp_path):
    if not getattr(vectordb_engine, "PersistStore", None):
        pytest.skip("local persistent vectordb engine is unavailable")
    backend = vector_backend_factory(
        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(),
    )


@pytest.mark.asyncio
@pytest.mark.parametrize("operation", ["cp", "mv"])
@pytest.mark.parametrize("directory", [False, True])
@pytest.mark.parametrize("phase", ["source", "target"])
async def test_transfer_uses_queries_without_count_or_sort(
    indexed_fs, monkeypatch, operation, directory, phase
):
    fs, backend = indexed_fs
    ctx = root_ctx()
    source, target = "viking://resources/source", "viking://resources/target"
    source_file = source + "/a.md" if directory else source
    target_file = target + "/a.md" if directory else target
    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")
    adapter = (await backend._get_backend_for_context(ctx))._adapter
    original_count, original_query = adapter.count, adapter.query
    count_calls, queries = [], []

    def stale_count(*args, **kwargs):
        count_calls.append(True)
        count = original_count(*args, **kwargs)
        return count + (len(count_calls) == (1 if phase == "source" else 2))

    def query(**kwargs):
        queries.append(kwargs)
        return original_query(**kwargs)

    monkeypatch.setattr(adapter, "count", stale_count)
    monkeypatch.setattr(adapter, "query", query)
    await getattr(fs, operation)(
        source, target, ctx=ctx, **({"recursive": directory} if operation == "cp" else {})
    )

    assert not count_calls
    assert len(queries) >= 2  # Both source reads and target replacement use this path.
    assert all(
        q.get("order_by") is None and q["offset"] == 0 and q["limit"] == 100 for q in queries
    )
    assert await fs.read_file_bytes(target_file, ctx=ctx) == b"new"
    records = await backend.get([vector_record_id(ctx.account_id, target_file, 2)], ctx=ctx)
    assert [record["abstract"] for record in records] == ["new"]
    assert await fs.exists(source, ctx=ctx) == (operation == "cp")


@pytest.mark.asyncio
@pytest.mark.parametrize("operation", ["cp", "mv"])
@pytest.mark.parametrize("disappear_all", [False, True])
async def test_transfer_skips_source_record_disappearing_before_fetch(
    indexed_fs, monkeypatch, operation, disappear_all
):
    fs, backend = indexed_fs
    ctx = root_ctx()
    source, target = "viking://resources/source", "viking://resources/target"
    a, b = source + "/a.md", source + "/b.md"
    for uri in [a, b]:
        await fs.write_file_bytes(uri, b"new", ctx=ctx)
        await seed_vector(backend, uri, "new")
    await seed_vector(backend, target + "/b.md", "old target index")
    original_get = backend._strict_transfer_get

    async def get_after_delete(ctx, ids):
        missing = [uri for uri in ids if uri == b or (disappear_all and uri == a)]
        if missing:
            await (await backend._get_backend_for_context(ctx)).strict_delete(missing)
        return await original_get(ctx, ids)

    monkeypatch.setattr(backend, "_strict_transfer_get", get_after_delete)
    result = await getattr(fs, operation)(
        source, target, ctx=ctx, **({"recursive": True} if operation == "cp" else {})
    )
    assert result["vectors"]["written"] == (0 if disappear_all else 1)
    assert await fs.read_file_bytes(target + "/b.md", ctx=ctx) == b"new"
    records = await backend.get([target + "/b.md"], ctx=ctx)
    assert [record["abstract"] for record in records] == ["old target index"]
    assert bool(
        await backend.get([vector_record_id(ctx.account_id, target + "/a.md", 2)], ctx=ctx)
    ) == (not disappear_all)


@pytest.mark.asyncio
@pytest.mark.parametrize("operation", ["cp", "mv"])
@pytest.mark.parametrize("failure", ["source_query", "target_query", "source_fetch"])
async def test_transfer_propagates_legacy_read_errors(indexed_fs, monkeypatch, operation, failure):
    fs, backend = indexed_fs
    ctx = root_ctx()
    source, target = "viking://resources/source.md", "viking://resources/target.md"
    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")
    adapter = (await backend._get_backend_for_context(ctx))._adapter
    original_query, original_get = adapter.query, adapter.get

    def query(**kwargs):
        records = original_query(**kwargs)
        failed_uri = target if failure == "target_query" else source
        if failure.endswith("query") and any(r["uri"] == failed_uri for r in records):
            raise RuntimeError("backend unavailable")
        return records

    def get(ids):
        if failure == "source_fetch" and source in ids:
            raise RuntimeError("backend unavailable")
        return original_get(ids)

    monkeypatch.setattr(adapter, "query", query)
    monkeypatch.setattr(adapter, "get", get)
    with pytest.raises(RuntimeError, match="backend unavailable"):
        await getattr(fs, operation)(source, target, ctx=ctx)
    assert await fs.read_file_bytes(source, ctx=ctx) == b"new"


@pytest.mark.asyncio
@pytest.mark.parametrize("operation", ["cp", "mv"])
@pytest.mark.parametrize("directory", [False, True])
@pytest.mark.parametrize("phase", ["source", "target"])
@pytest.mark.parametrize("mutation", ["add", "delete"])
async def test_transfer_ignores_concurrent_sibling_index_changes(
    indexed_fs, monkeypatch, operation, directory, phase, mutation
):
    fs, backend = indexed_fs
    ctx = root_ctx()
    source, target = "viking://resources/source", "viking://resources/target"
    source_file = source + "/file.md" if directory else source
    target_file = target + "/file.md" if directory else target
    sibling = "viking://resources/unrelated.md"
    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")
    if directory:
        await seed_vector(backend, source, "source summary")
        await seed_vector(backend, target, "target summary")
        await fs.write_file_bytes(target + "/keep.md", b"keep", ctx=ctx)
    if mutation == "delete":
        await fs.write_file_bytes(sibling, b"unrelated", ctx=ctx)
        await seed_vector(backend, sibling, "unrelated")

    account_backend = await backend._get_backend_for_context(ctx)
    original_query = account_backend.strict_query
    trigger_uri = source_file if phase == "source" else target_file
    changed = False

    async def query_with_sibling_change(*args, **kwargs):
        nonlocal changed
        records = await original_query(*args, **kwargs)
        if not changed and any(record["uri"] == trigger_uri for record in records):
            changed = True
            # No transfer lease is supplied: this is an independent writer.
            if mutation == "add":
                await fs.write_file_bytes(sibling, b"unrelated", ctx=ctx)
                await seed_vector(backend, sibling, "unrelated")
            else:
                await fs.rm(sibling, ctx=ctx)
        return records

    monkeypatch.setattr(account_backend, "strict_query", query_with_sibling_change)
    await getattr(fs, operation)(
        source, target, ctx=ctx, **({"recursive": directory} if operation == "cp" else {})
    )

    assert changed
    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 await fs.exists(source, ctx=ctx) == (operation == "cp")
    assert await fs.exists(sibling, ctx=ctx) == (mutation == "add")
    if directory:
        assert await fs.read_file_bytes(target + "/keep.md", ctx=ctx) == b"keep"


@pytest.mark.asyncio
@pytest.mark.parametrize("operation", ["cp", "mv"])
@pytest.mark.parametrize("directory", [False, True])
async def test_transfer_preserves_legacy_independent_chunk_records(
    indexed_fs, operation, directory
):
    fs, backend = indexed_fs
    ctx = root_ctx()
    source, target = "viking://resources/source", "viking://resources/target"
    source_file = source + "/file.md" if directory else source
    target_file = target + "/file.md" if directory else target
    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_chunk = source_file + "#chunk_0001"
    target_chunk = target_file + "#chunk_old"
    await seed_vector(backend, source_chunk, "source chunk")
    await seed_vector(backend, target_chunk, "old target chunk")

    await getattr(fs, operation)(
        source, target, ctx=ctx, **({"recursive": directory} if operation == "cp" else {})
    )

    assert await fs.read_file_bytes(target_file, ctx=ctx) == b"new"
    assert await backend.get_context_by_uri(source_chunk, ctx=ctx)
    assert await backend.get_context_by_uri(target_chunk, ctx=ctx)
    assert not await backend.get_context_by_uri(target_file + "#chunk_0001", ctx=ctx)


@pytest.mark.asyncio
@pytest.mark.parametrize("operation", ["cp", "mv"])
async def test_directory_transfer_reads_all_exact_entries_with_native_storage(
    indexed_fs, monkeypatch, operation
):
    fs, backend = indexed_fs
    ctx = root_ctx()
    source, target = "viking://resources/source", "viking://resources/target"
    names = [f"file-{i:03d}.md" for i in range(205)] + ["literal.md#chunk_0001"]
    for name in names:
        await fs.write_file_bytes(f"{source}/{name}", b"new", ctx=ctx)
    await fs.write_file_bytes(f"{target}/keep.md", b"keep", ctx=ctx)
    source_uris = [f"{source}/{name}" for name in names]
    old_targets = [f"{target}/{name}" for name in names]
    untouched = [
        f"{source}/missing.md",  # Orphan index, absent from the filesystem manifest.
        f"{source}/file-000.md#chunk_0001",
        f"{target}/file-000.md#chunk_old",
        f"{target}/keep.md",
        f"{target}/unrelated/deep.md",
    ]
    await backend._upsert_many_raw(
        [
            {"id": uri, "uri": uri, "level": 2, "vector": [0.1] * 4, "account_id": ctx.account_id}
            for uri in source_uris + old_targets + untouched
        ],
        ctx=ctx,
    )
    read_ids: set[str] = set()
    account_backend = await backend._get_backend_for_context(ctx)
    original_query = account_backend.strict_query

    async def tracked_query(*args, **kwargs):
        page = await original_query(*args, **kwargs)
        read_ids.update(record["id"] for record in page)
        return page

    monkeypatch.setattr(account_backend, "strict_query", tracked_query)
    result = await getattr(fs, operation)(
        source, target, ctx=ctx, **({"recursive": True} if operation == "cp" else {})
    )

    assert result["vectors"]["scanned"] == result["vectors"]["written"] == len(names)
    assert result["vectors"]["batches"] >= 3
    assert set(source_uris + old_targets).issubset(read_ids)
    assert not set(untouched).intersection(read_ids)
    target_ids = [vector_record_id(ctx.account_id, uri, 2) for uri in old_targets]
    assert {record["uri"] for record in await backend.get(target_ids, ctx=ctx)} == set(old_targets)
    assert not await backend.get(old_targets, ctx=ctx)
    assert {record["uri"] for record in await backend.get(untouched, ctx=ctx)} == set(untouched)
    assert len(await backend.get(source_uris, ctx=ctx)) == (len(names) if operation == "cp" else 0)
    assert await fs.read_file_bytes(f"{target}/keep.md", ctx=ctx) == b"keep"
    assert await fs.read_file_bytes(f"{target}/literal.md#chunk_0001", ctx=ctx) == b"new"


@pytest.mark.asyncio
@pytest.mark.parametrize("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_mode": "restricted",
                "acl_direct_grants": [f"3:user:{ctx.user.user_id}"],
                "acl_inherited_grants": [],
            }
        ],
        ctx=ctx,
    )
    acl = AclManager(backend, _EnabledAclConfig())
    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])


@pytest.mark.asyncio
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_mode": "restricted",
        "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, _EnabledAclConfig())
    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 not await backend.get_context_by_uri(f"{target_file}#chunk_0000", ctx=ctx)
    assert await backend.get_context_by_uri(f"{target_file}#chunk_old", ctx=ctx)


@pytest.mark.asyncio
@pytest.mark.parametrize("operation", ["cp", "mv"])
@pytest.mark.parametrize("directory", [False, True])
@pytest.mark.parametrize("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"


@pytest.mark.asyncio
@pytest.mark.parametrize("operation", ["cp", "mv"])
@pytest.mark.parametrize("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_mode": "restricted",
                "acl_direct_grants": [f"7:user:{ctx.user.user_id}"],
                "acl_inherited_grants": [],
            }
        ],
        ctx=ctx,
    )
    acl = AclManager(backend, _EnabledAclConfig())
    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 {}
    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}"]


@pytest.mark.asyncio
@pytest.mark.parametrize("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")

    fetched_ids: list[str] = []
    original_get = backend._strict_transfer_get

    async def counted_get(ctx, ids):
        fetched_ids.extend(ids)
        return await original_get(ctx, ids)

    monkeypatch.setattr(backend, "_strict_transfer_get", counted_get)
    kwargs = {"recursive": True} if operation == "cp" else {}
    await getattr(fs, operation)(source, target, ctx=ctx, **kwargs)

    # Exact entry queries must not load vectors for destination-only files.
    assert f"{target}/only.txt" not in fetched_ids
    assert f"{target}/unindexed.txt" not in fetched_ids
    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 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"
    )


@pytest.mark.asyncio
@pytest.mark.parametrize("operation", ["cp", "mv"])
@pytest.mark.parametrize("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"


@pytest.mark.asyncio
@pytest.mark.parametrize("operation", ["cp", "mv"])
@pytest.mark.parametrize("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")


@pytest.mark.asyncio
async def test_watch_failure_restores_moved_content_and_source_acl(indexed_fs):
    fs, backend = indexed_fs
    ctx = RequestContext(user=root_ctx().user, role=Role.ADMIN)
    source, target = "viking://resources/source", "viking://resources/target"
    await fs.write_file_bytes(f"{source}/file.txt", b"new", ctx=ctx)
    await seed_vector(backend, f"{source}/file.txt", "new")
    acl = AclManager(backend, _EnabledAclConfig())
    fs.acl_manager = backend.acl_manager = acl
    await acl.set_acl(f"{source}/file.txt", [{"principal": "user:alice", "level": "manage"}], ctx)
    before = await acl.resolve(f"{source}/file.txt", ctx)
    manager = SimpleNamespace(
        validate_target_prefix_rewrite_internal=AsyncMock(),
        rewrite_target_prefix_internal=AsyncMock(side_effect=RuntimeError("watch commit failed")),
    )
    service = FSService(viking_fs=fs, watch_scheduler=SimpleNamespace(watch_manager=manager))

    with pytest.raises(RuntimeError, match="watch commit failed"):
        await service._mv_and_refresh(source, target, ctx=ctx)

    assert await fs.read_file_bytes(f"{source}/file.txt", ctx=ctx) == b"new"
    assert await acl.resolve(f"{source}/file.txt", ctx) == before
    assert not await fs.exists(target, ctx=ctx)


@pytest.mark.asyncio
@pytest.mark.parametrize("operation", ["cp", "mv"])
@pytest.mark.parametrize("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)


@pytest.mark.asyncio
@pytest.mark.parametrize("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"


@pytest.mark.asyncio
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)