Skip to content

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

fix(session): preserve outer lease when creating extracted skills (#5018)

Asuradaa5aae16d0b7d
diff --git a/openviking/session/skill/skill_operation_updater.py b/openviking/session/skill/skill_operation_updater.py
index 2ac7da7f4..eb5f939c0 100644
--- a/openviking/session/skill/skill_operation_updater.py
+++ b/openviking/session/skill/skill_operation_updater.py
@@ -61,6 +61,8 @@ class SkillOperationUpdater:
self,
operations: ResolvedOperations,
ctx: RequestContext,
+ *,
+ transaction_handle: Any = None,
) -> SkillOperationUpdateResult:
result = SkillOperationUpdateResult()
if not self._viking_fs:
@@ -73,7 +75,9 @@ class SkillOperationUpdater:
for operation in operations.upsert_operations:
try:
- op_result = await self._apply_upsert(operation, ctx)
+ op_result = await self._apply_upsert(
+ operation, ctx, transaction_handle=transaction_handle
+ )
result.add_result(op_result)
if op_result.get("action") == "create":
result.add_written(op_result["skill_md_uri"])
@@ -90,6 +94,8 @@ class SkillOperationUpdater:
self,
operation: ResolvedOperation,
ctx: RequestContext,
+ *,
+ transaction_handle: Any = None,
) -> Dict[str, Any]:
if not operation.uris:
raise ValueError("Session skill operation does not have a target URI")
@@ -105,6 +111,7 @@ class SkillOperationUpdater:
viking_fs=self._viking_fs,
ctx=ctx,
allow_local_path_resolution=False,
+ lease_ref=transaction_handle,
)
created_root_uri = (
processor_result.get("root_uri") or processor_result.get("uri") or root_uri
diff --git a/openviking/session/train/components/skill_policy_updater.py b/openviking/session/train/components/skill_policy_updater.py
index 4d70ccf48..83f08befb 100644
--- a/openviking/session/train/components/skill_policy_updater.py
+++ b/openviking/session/train/components/skill_policy_updater.py
@@ -89,7 +89,9 @@ class SkillPolicyUpdater:
skill_processor=processor,
viking_fs=viking_fs,
)
- result = await updater.apply_operations(operations, ctx)
+ result = await updater.apply_operations(
+ operations, ctx, transaction_handle=transaction_handle
+ )
errors = [f"{uri}: {exc}" for uri, exc in result.errors]
diff --git a/openviking/storage/viking_fs/_semantic.py b/openviking/storage/viking_fs/_semantic.py
index 1681ae4e0..205def709 100644
--- a/openviking/storage/viking_fs/_semantic.py
+++ b/openviking/storage/viking_fs/_semantic.py
@@ -527,6 +527,8 @@ class _SemanticMixin:
content_filename: str = "content.md",
is_leaf: bool = False,
ctx: Optional[RequestContext] = None,
+ *,
+ lease_ref: Any = None,
) -> None:
"""Write context to AGFS (L0/L1/L2)."""
@@ -534,16 +536,18 @@ class _SemanticMixin:
path = self._uri_to_path(uri, ctx=ctx)
try:
- await self._ensure_parent_dirs(path, ctx=ctx)
+ await self._ensure_parent_dirs(path, ctx=ctx, lease_ref=lease_ref)
try:
- await self._async_agfs.mkdir(path)
+ # _pathlock_fs_ctx is supplied by VikingFS's _AccessMixin.
+ fs_ctx = self._pathlock_fs_ctx(ctx, lease_ref) # type: ignore[attr-defined]
+ await self._async_agfs.mkdir(path, fs_ctx=fs_ctx)
except Exception as e:
if "exist" not in str(e).lower():
raise
if content:
content_uri = f"{uri}/{content_filename}"
- await self.write_file(content_uri, content, ctx=ctx)
+ await self.write_file(content_uri, content, ctx=ctx, lease_ref=lease_ref)
if abstract:
abstract_uri = f"{uri}/.abstract.md"
@@ -561,6 +565,7 @@ class _SemanticMixin:
},
),
ctx=ctx,
+ lease_ref=lease_ref,
)
if overview:
@@ -579,6 +584,7 @@ class _SemanticMixin:
},
),
ctx=ctx,
+ lease_ref=lease_ref,
)
except Exception as e:
diff --git a/openviking/utils/skill_processor.py b/openviking/utils/skill_processor.py
index b8e205552..4460c5005 100644
--- a/openviking/utils/skill_processor.py
+++ b/openviking/utils/skill_processor.py
@@ -118,6 +118,7 @@ class SkillProcessor:
apply_privacy: bool = True,
privacy_change_reason: str = "auto-extracted from add_skill",
target_uri: Optional[str] = None,
+ lease_ref: Any = None,
) -> Dict[str, Any]:
"""
Process and store a skill.
@@ -154,6 +155,7 @@ class SkillProcessor:
apply_privacy=apply_privacy,
privacy_change_reason=privacy_change_reason,
target_uri=target_uri,
+ lease_ref=lease_ref,
)
async def process_prepared_skill(
@@ -165,6 +167,7 @@ class SkillProcessor:
apply_privacy: bool = True,
privacy_change_reason: str = "auto-extracted from add_skill",
target_uri: Optional[str] = None,
+ lease_ref: Any = None,
) -> Dict[str, Any]:
cleanup_path = preparation.cleanup_path
skill_dict = preparation.skill_dict
@@ -212,6 +215,7 @@ class SkillProcessor:
abstract=skill_abstract,
overview=skill_dict.get("content", ""),
ctx=ctx,
+ lease_ref=lease_ref,
)
await self._write_auxiliary_files(
@@ -220,6 +224,7 @@ class SkillProcessor:
base_path=base_path,
skill_dir_uri=skill_dir_uri,
ctx=ctx,
+ lease_ref=lease_ref,
)
telemetry.set(
"skill.write.duration_ms", round((time.perf_counter() - write_start) * 1000, 3)
@@ -530,6 +535,7 @@ class SkillProcessor:
abstract: str,
overview: str,
ctx: RequestContext,
+ lease_ref: Any = None,
):
"""Write main skill content to VikingFS."""
await viking_fs.write_context(
@@ -540,6 +546,7 @@ class SkillProcessor:
content_filename="SKILL.md",
is_leaf=False,
ctx=ctx,
+ lease_ref=lease_ref,
)
async def _write_auxiliary_files(
@@ -549,6 +556,7 @@ class SkillProcessor:
base_path: Optional[Path],
skill_dir_uri: str,
ctx: RequestContext,
+ lease_ref: Any = None,
):
"""Write auxiliary files to VikingFS."""
for aux_file in auxiliary_files:
@@ -567,9 +575,11 @@ class SkillProcessor:
is_text = False
if is_text:
- await viking_fs.write_file(aux_uri, file_bytes.decode("utf-8"), ctx=ctx)
+ await viking_fs.write_file(
+ aux_uri, file_bytes.decode("utf-8"), ctx=ctx, lease_ref=lease_ref
+ )
else:
- await viking_fs.write_file_bytes(aux_uri, file_bytes, ctx=ctx)
+ await viking_fs.write_file_bytes(aux_uri, file_bytes, ctx=ctx, lease_ref=lease_ref)
async def _index_skill(self, context: Context, skill_dir_uri: str):
"""Write skill directory vector via async queue as L0."""
diff --git a/tests/session/train/test_train_components.py b/tests/session/train/test_train_components.py
index 12917fdf5..9999c632f 100644
--- a/tests/session/train/test_train_components.py
+++ b/tests/session/train/test_train_components.py
@@ -1225,3 +1225,64 @@ async def test_patch_merge_policy_optimizer_uses_session_skill_registry(monkeypa
assert result.updated_policy_set.policies[0].metadata["description"] == saved["description"]
if existing:
assert policy_set.policies[0].metadata["description"] == "Old description"
+
+
[email protected]("locked", [False, True], ids=["standalone", "outer-tree-lock"])
+async def test_skill_policy_creation_preserves_lock_ownership(tmp_path, monkeypatch, locked):
+ from contextlib import nullcontext
+ from types import SimpleNamespace
+ from unittest.mock import AsyncMock
+
+ from openviking.core.skill_loader import SkillLoader
+ from openviking.server.identity import RequestContext, Role
+ from openviking.session.train.components.skill_policy_updater import SkillPolicyUpdater
+ from openviking.session.train.domain import PolicyPlanItem
+ from openviking.storage.viking_fs import VikingFS
+ from openviking.utils.agfs_utils import RagfsBindingConfig, create_agfs_client
+ from openviking.utils.skill_processor import SkillProcessor
+ from openviking_cli.session.user_id import UserIdentifier
+ from openviking_cli.utils.config.agfs_config import AGFSConfig
+
+ agfs = create_agfs_client(RagfsBindingConfig(agfs=AGFSConfig(path=str(tmp_path))))
+ fs = VikingFS(agfs=agfs)
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role(Role.ROOT))
+ root = "viking://user/default/skills"
+ uri = f"{root}/code-review/SKILL.md"
+ processor = SkillProcessor(vikingdb=AsyncMock())
+ config = SimpleNamespace(
+ vlm=SimpleNamespace(get_completion_async=AsyncMock(return_value="Overview"))
+ )
+ monkeypatch.setattr("openviking.utils.skill_processor.get_openviking_config", lambda: config)
+ policy_set = ExperienceSet(root_uri=root, policies=[], viking_fs=fs, request_context=ctx)
+ updater = SkillPolicyUpdater(skill_processor=processor, viking_fs=fs)
+ plan = PolicyUpdatePlan(
+ items=[
+ PolicyPlanItem(
+ kind="upsert",
+ memory_type=SESSION_SKILL_MEMORY_TYPE,
+ target_name="code-review",
+ target_uri=uri,
+ before_content=None,
+ after_content="Review steps.",
+ metadata={"merge_memory_fields": {"description": "Review code changes"}},
+ )
+ ]
+ )
+ try:
+ async with policy_set.lock() if locked else nullcontext() as lease:
+ result = await updater.apply(plan, policy_set, ctx, transaction_handle=lease)
+ assert result.errors == []
+ assert result.written_uris == [uri]
+ assert SkillLoader.parse(await fs.read_file(uri, ctx=ctx))["content"] == "Review steps."
+ for filename in (".abstract.md", ".overview.md"):
+ assert await fs.read_file(f"{root}/code-review/{filename}", ctx=ctx)
+ if locked:
+ # The callee must not release the caller's tree lock.
+ with pytest.raises(LockAcquisitionError):
+ await fs._async_agfs.pathlock_acquire_exact(fs._uri_to_path(uri, ctx=ctx))
+ # The outer context releases its lease after the nested writes.
+ lease = await fs._async_agfs.pathlock_acquire_tree(fs._uri_to_path(root, ctx=ctx))
+ await fs._async_agfs.pathlock_release(lease)
+ finally:
+ agfs.close()
diff --git a/tests/unit/test_skill_processor_windows_paths.py b/tests/unit/test_skill_processor_windows_paths.py
index a211ab469..41bb757f3 100644
--- a/tests/unit/test_skill_processor_windows_paths.py
+++ b/tests/unit/test_skill_processor_windows_paths.py
@@ -88,6 +88,7 @@ async def test_write_auxiliary_files_normalizes_windows_separators(tmp_path):
"viking://user/default/skills/pdf/scripts/check_bounding_boxes.py",
"print('ok')",
ctx=None,
+ lease_ref=None,
)
viking_fs.write_file_bytes.assert_not_awaited()
@@ -118,5 +119,6 @@ async def test_write_auxiliary_files_preserves_posix_separators(tmp_path):
"viking://user/default/skills/pdf/scripts/check_bounding_boxes.py",
"print('ok')",
ctx=None,
+ lease_ref=None,
)
viking_fs.write_file_bytes.assert_not_awaited()