Skip to content

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

fix(queue): recover stale non-tree lock handoffs (#3193)

huangruiteng0a6204221b09
diff --git a/openviking/storage/queuefs/semantic_lock.py b/openviking/storage/queuefs/semantic_lock.py
index 88ca0b616..f6e059920 100644
--- a/openviking/storage/queuefs/semantic_lock.py
+++ b/openviking/storage/queuefs/semantic_lock.py
@@ -5,7 +5,7 @@
from __future__ import annotations
from dataclasses import dataclass
-from typing import Iterable, Optional
+from typing import Callable, Iterable, Optional
from openviking.storage.errors import LockAcquisitionError
from openviking.storage.transaction import (
@@ -46,7 +46,9 @@ class SemanticLockScope:
lock_handoff: Optional[LockHandoffRef],
*,
caller_lock: LockLease = NO_LOCK,
+ fallback_path_factory: Optional[Callable[[], str]] = None,
) -> "SemanticLockScope":
+ """Resolve a live lock, lazily deriving a path for stale legacy handoffs."""
if lock_handoff and caller_lock.active:
raise ValueError("semantic lock must come from either message or caller, not both")
if caller_lock is not NO_LOCK and not caller_lock.active:
@@ -59,6 +61,8 @@ class SemanticLockScope:
return cls(await OwnedLockLease.from_handoff(lock_handoff, manager=manager))
except LockAcquisitionError as exc:
tree_paths = _tree_paths_from_handoff(lock_handoff.lock_paths)
+ if not tree_paths and fallback_path_factory:
+ tree_paths = [fallback_path_factory()]
if not tree_paths:
raise
diff --git a/openviking/storage/queuefs/semantic_processor.py b/openviking/storage/queuefs/semantic_processor.py
index 257b4d86f..2afe6dd74 100644
--- a/openviking/storage/queuefs/semantic_processor.py
+++ b/openviking/storage/queuefs/semantic_processor.py
@@ -368,6 +368,9 @@ class SemanticProcessor(DequeueHandlerBase):
semantic_lock = await SemanticLockScope.resolve(
msg.lock_handoff,
caller_lock=lock,
+ fallback_path_factory=lambda: get_viking_fs()._uri_to_path(
+ msg.uri, ctx=current_ctx
+ ),
)
lock_transferred = False
try:
diff --git a/tests/storage/test_semantic_processor_lock_ownership.py b/tests/storage/test_semantic_processor_lock_ownership.py
index b5ff173d3..267ec7fe2 100644
--- a/tests/storage/test_semantic_processor_lock_ownership.py
+++ b/tests/storage/test_semantic_processor_lock_ownership.py
@@ -41,6 +41,38 @@ class _FakeLockManager:
return True
+class _RecoveringLockManager:
+ def __init__(self):
+ self._handles = {}
+ self.acquire_tree_calls = []
+ self.release_calls = []
+
+ async def get_handle_async(self, handle_id):
+ return self._handles.get(handle_id)
+
+ async def adopt_handle_async(self, handle_id, lock_paths):
+ del handle_id, lock_paths
+ return None
+
+ def get_handle(self, handle_id):
+ return self._handles.get(handle_id)
+
+ def create_handle(self):
+ handle = _FakeHandle("new-lock")
+ handle.locks = []
+ self._handles[handle.id] = handle
+ return handle
+
+ async def acquire_tree(self, handle, lock_path):
+ self.acquire_tree_calls.append(lock_path)
+ handle.locks.append(f"{lock_path}/.path.ovlock")
+ return True
+
+ async def release(self, handle):
+ self.release_calls.append(handle.id)
+ self._handles.pop(handle.id, None)
+
+
class _FakeVikingFS:
async def exists(self, uri, ctx=None):
del uri, ctx
@@ -59,6 +91,7 @@ async def test_semantic_processor_borrows_caller_owned_lock(monkeypatch):
class _FakeDagExecutor:
def __init__(self, **kwargs):
self.lock = kwargs["lock"]
+ self.stale = False
async def run(self, root_uri):
assert root_uri == "viking://resources/demo"
@@ -93,40 +126,56 @@ async def test_semantic_processor_borrows_caller_owned_lock(monkeypatch):
@pytest.mark.asyncio
-async def test_semantic_lock_scope_reacquires_tree_lock_when_handoff_handle_is_stale(
- monkeypatch,
-):
- class _RecoveringLockManager:
- def __init__(self):
- self._handles = {}
- self.acquire_tree_calls = []
- self.release_calls = []
+async def test_semantic_processor_recovers_stale_non_tree_handoff(monkeypatch):
+ processor = SemanticProcessor()
+ lock_manager = _RecoveringLockManager()
- async def get_handle_async(self, handle_id):
- return self._handles.get(handle_id)
+ class _FakeDagExecutor:
+ def __init__(self, **kwargs):
+ self.lock = kwargs["lock"]
+ self.stale = False
+
+ async def run(self, root_uri):
+ assert root_uri == "viking://resources/demo"
+ assert self.lock.handle_id == "new-lock"
+ await self.lock.close()
- async def adopt_handle_async(self, handle_id, lock_paths):
- del handle_id, lock_paths
- return None
+ def get_stats(self):
+ return DagStats()
- def get_handle(self, handle_id):
- return self._handles.get(handle_id)
+ monkeypatch.setattr(
+ "openviking.storage.queuefs.semantic_processor.get_viking_fs",
+ lambda: _FakeVikingFS(),
+ )
+ monkeypatch.setattr(
+ "openviking.storage.queuefs.semantic_processor.SemanticDagExecutor",
+ lambda **kwargs: _FakeDagExecutor(**kwargs),
+ )
+ monkeypatch.setattr(
+ "openviking.storage.queuefs.semantic_lock.get_lock_manager",
+ lambda: lock_manager,
+ )
- def create_handle(self):
- handle = _FakeHandle("new-lock")
- handle.locks = []
- self._handles[handle.id] = handle
- return handle
+ await processor.on_dequeue(
+ SemanticMsg(
+ uri="viking://resources/demo",
+ context_type="resource",
+ recursive=False,
+ lock_handoff=LockHandoffRef(
+ handle_id="stale-lock",
+ lock_paths=("/fake/viking/resources/.mw_exact_demo.deadbeef",),
+ ),
+ ).to_dict()
+ )
- async def acquire_tree(self, handle, lock_path):
- self.acquire_tree_calls.append(lock_path)
- handle.locks.append(f"{lock_path}/.path.ovlock")
- return True
+ assert lock_manager.acquire_tree_calls == ["/fake/viking/resources/demo"]
+ assert lock_manager.release_calls == ["new-lock"]
- async def release(self, handle):
- self.release_calls.append(handle.id)
- self._handles.pop(handle.id, None)
+async def test_semantic_lock_scope_reacquires_tree_lock_when_handoff_handle_is_stale(
+ monkeypatch,
+):
lock_manager = _RecoveringLockManager()
monkeypatch.setattr(
"openviking.storage.queuefs.semantic_lock.get_lock_manager",
@@ -137,7 +186,10 @@ async def test_semantic_lock_scope_reacquires_tree_lock_when_handoff_handle_is_s
LockHandoffRef(
handle_id="stale-lock",
lock_paths=("/local/default/resources/CONTRIBUTING_CN_3/.path.ovlock",),
- )
+ ),
+ fallback_path_factory=lambda: (_ for _ in ()).throw(
+ AssertionError("tree handoffs must not evaluate the fallback path")
+ ),
)
try:
@@ -149,6 +201,33 @@ async def test_semantic_lock_scope_reacquires_tree_lock_when_handoff_handle_is_s
assert lock_manager.release_calls == ["new-lock"]
+async def test_semantic_lock_scope_reacquires_fallback_path_for_stale_non_tree_handoff(
+ monkeypatch,
+):
+ lock_manager = _RecoveringLockManager()
+ monkeypatch.setattr(
+ "openviking.storage.queuefs.semantic_lock.get_lock_manager",
+ lambda: lock_manager,
+ )
+
+ scope = await SemanticLockScope.resolve(
+ LockHandoffRef(
+ handle_id="stale-lock",
+ lock_paths=("/local/default/resources/.mw_exact_demo.deadbeef",),
+ ),
+ fallback_path_factory=lambda: "/local/default/resources/demo",
+ )
+
+ try:
+ assert scope.lock.handle_id == "new-lock"
+ assert lock_manager.acquire_tree_calls == ["/local/default/resources/demo"]
+ finally:
+ await scope.close()
+
+ assert lock_manager.release_calls == ["new-lock"]
+
+
@pytest.mark.asyncio
async def test_semantic_processor_lock_error_requeues_without_circuit_breaker(monkeypatch):
processor = SemanticProcessor()