Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 48 additions & 39 deletions openviking/service/fs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,45 +345,54 @@ async def mkdir(
viking_fs = self._ensure_initialized()
self._reject_storage_internal_target(uri)
directory_uri, abstract_uri = self._resolve_directory_uris(uri)
directory_preexisting = await viking_fs.exists(directory_uri, ctx=ctx)
await viking_fs.mkdir(uri, ctx=ctx)

abstract = self._normalize_directory_description(description)
if not abstract:
if await viking_fs.exists(abstract_uri, ctx=ctx):
return
abstract = f"# {uri_leaf_name(directory_uri)}"

await viking_fs.write_file(
abstract_uri,
render_abstract_overview(
ContextLevel.ABSTRACT,
directory_uri,
abstract,
{
"generated_by": {
"component": "FSService",
"trigger": "mkdir",
},
"freshness": {
"total_entries": 0,
"sampled_entries": 0,
"unsampled_entries": 0,
"pending_child_changes": 0,
},
},
),
ctx=ctx,
)
await vectorize_directory_meta(
uri=directory_uri,
abstract=abstract,
overview="",
context_type=context_type_for_uri(directory_uri),
ctx=ctx,
creator_acl_grant=(CreatorAclGrant.DIRECT if not directory_preexisting else None),
include_overview=False,
)
async with self._uri_mutation_coordinator.mutation(ctx.account_id, [directory_uri]):
directory_preexisting = await viking_fs.exists(directory_uri, ctx=ctx)
await viking_fs.mkdir(uri, ctx=ctx)

lock_path = viking_fs._uri_to_path(abstract_uri, ctx=ctx)
lease = await viking_fs._async_agfs.pathlock_acquire_exact(lock_path)
try:
abstract = self._normalize_directory_description(description)
if not abstract:
if await viking_fs.exists(abstract_uri, ctx=ctx):
return
abstract = f"# {uri_leaf_name(directory_uri)}"

await viking_fs.write_file(
abstract_uri,
render_abstract_overview(
ContextLevel.ABSTRACT,
directory_uri,
abstract,
{
"generated_by": {
"component": "FSService",
"trigger": "mkdir",
},
"freshness": {
"total_entries": 0,
"sampled_entries": 0,
"unsampled_entries": 0,
"pending_child_changes": 0,
},
},
),
ctx=ctx,
lease_ref=lease,
)
await vectorize_directory_meta(
uri=directory_uri,
abstract=abstract,
overview="",
context_type=context_type_for_uri(directory_uri),
ctx=ctx,
creator_acl_grant=(
CreatorAclGrant.DIRECT if not directory_preexisting else None
),
include_overview=False,
)
finally:
await viking_fs._async_agfs.pathlock_release(lease)

@staticmethod
def _normalize_directory_description(description: Optional[str]) -> Optional[str]:
Expand Down
109 changes: 109 additions & 0 deletions tests/service/test_fs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from openviking.server.identity import RequestContext, Role
from openviking.service.fs_service import FSService
from openviking.storage.abstract_overview import body_for_preview
from openviking.storage.errors import LockAcquisitionError
from openviking_cli.exceptions import InvalidArgumentError
from openviking_cli.session.user_id import UserIdentifier

Expand Down Expand Up @@ -100,6 +102,113 @@ async def mutation(self, account_id, uris):
self.events.append(("mutation-exit", *uris))


class _MkdirPathlockAGFS:
def __init__(self, owner, *, inject_custom_on_acquire=False):
self._owner = owner
self._lock = asyncio.Lock()
self._inject_custom_on_acquire = inject_custom_on_acquire
self.held = False
self.acquire_count = 0

async def pathlock_acquire_exact(self, _path):
if self._lock.locked():
raise LockAcquisitionError("exact path is busy")
await self._lock.acquire()
self.held = True
self.acquire_count += 1
if self._inject_custom_on_acquire and self._owner.abstract_content is None:
self._owner.abstract_content = "custom summary"
return {"lease_ref": f"mkdir-{self.acquire_count}"}

async def pathlock_release(self, _lease):
self.held = False
self._lock.release()


class _MkdirVikingFS:
def __init__(self, *, inject_custom_on_acquire=False):
self.directory_exists = False
self.abstract_content = None
self.abstract_checks = 0
self.abstract_checks_ready = asyncio.Event()
self._async_agfs = _MkdirPathlockAGFS(
self, inject_custom_on_acquire=inject_custom_on_acquire
)

def _uri_to_path(self, uri, ctx=None):
del ctx
return f"/local/default/{uri.removeprefix('viking://')}"

async def exists(self, uri, ctx=None):
del ctx
if uri.endswith("/.abstract.md"):
if self._async_agfs.held:
return self.abstract_content is not None
if self._async_agfs._inject_custom_on_acquire:
self.abstract_content = "custom summary"
return False
self.abstract_checks += 1
if self.abstract_checks == 2:
self.abstract_checks_ready.set()
await self.abstract_checks_ready.wait()
return False
return self.directory_exists

async def mkdir(self, _uri, ctx=None):
del ctx
self.directory_exists = True

async def write_file(self, _uri, content, ctx=None, lease_ref=None):
del ctx
assert self._async_agfs.held
assert lease_ref is not None
self.abstract_content = content


@pytest.mark.asyncio
async def test_mkdir_default_does_not_overwrite_concurrent_abstract(monkeypatch, request_context):
viking_fs = _MkdirVikingFS(inject_custom_on_acquire=True)
vectorized = []

async def record_vectorization(**kwargs):
assert viking_fs._async_agfs.held
vectorized.append(kwargs["abstract"])

monkeypatch.setattr(
"openviking.service.fs_service.vectorize_directory_meta", record_vectorization
)
service = FSService(viking_fs=viking_fs)

await service.mkdir("viking://resources/shared", ctx=request_context)

assert body_for_preview(viking_fs.abstract_content) == "custom summary"
assert vectorized == []


@pytest.mark.asyncio
async def test_concurrent_default_mkdir_vectorizes_once(monkeypatch, request_context):
viking_fs = _MkdirVikingFS()
vectorized = []

async def record_vectorization(**kwargs):
assert viking_fs._async_agfs.held
vectorized.append(kwargs["abstract"])
await asyncio.sleep(0)

monkeypatch.setattr(
"openviking.service.fs_service.vectorize_directory_meta", record_vectorization
)
service = FSService(viking_fs=viking_fs)

await asyncio.gather(
service.mkdir("viking://resources/shared", ctx=request_context),
service.mkdir("viking://resources/shared", ctx=request_context),
)

assert body_for_preview(viking_fs.abstract_content) == "# shared"
assert vectorized == ["# shared"]


@pytest.mark.asyncio
async def test_write_forwards_tags_and_tag_mode_to_content_coordinator(
monkeypatch, request_context
Expand Down