Skip to content

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

fix(storage): 修复中文路径的旧版锁交接恢复 (#6620)

xiejianqiaod840333cfdd9
diff --git a/openviking/pyagfs/async_client.py b/openviking/pyagfs/async_client.py
index 9b2bae15f..50fe84bd1 100644
--- a/openviking/pyagfs/async_client.py
+++ b/openviking/pyagfs/async_client.py
@@ -9,6 +9,7 @@ from collections.abc import Iterator
from typing import Any, BinaryIO, Dict, List, Union
from openviking.service.task_tracker_concurrency import run_to_completion
+from openviking.storage.errors import InvalidPathLockRequestError
from .protocols import AGFSSyncClientProtocol
@@ -551,11 +552,19 @@ class AsyncAGFSClient:
fs_ctx: Dict[str, str] | None = None,
) -> Dict[str, Any]:
"""Adopt a handoff ref, returning a new owned lease."""
- return await self.run(
- "pathlock_adopt",
- _fs_ctx_or_default("/", fs_ctx),
- handoff_ref,
- )
+ try:
+ return await self.run(
+ "pathlock_adopt",
+ _fs_ctx_or_default("/", fs_ctx),
+ handoff_ref,
+ )
+ except ValueError as exc:
+ # The native binding maps PathLockError::InvalidRequest to ValueError.
+ # Give queue consumers a typed, non-retryable error without changing
+ # unrelated ValueError contracts from legacy binding clients.
+ if str(exc).startswith("invalid lock request:"):
+ raise InvalidPathLockRequestError(str(exc)) from exc
+ raise
async def pathlock_is_locked(
self,
diff --git a/openviking/storage/errors.py b/openviking/storage/errors.py
index a6ac97b24..0e18d787a 100644
--- a/openviking/storage/errors.py
+++ b/openviking/storage/errors.py
@@ -65,6 +65,10 @@ class LockAcquisitionError(LockError):
"""Raised when lock acquisition fails."""
+class InvalidPathLockRequestError(ValueError):
+ """Raised when a native PathLock request or durable handoff is malformed."""
+
+
class ResourceBusyError(LockError):
"""Raised when a resource is locked by an ongoing operation."""
diff --git a/openviking/storage/queuefs/semantic_lock.py b/openviking/storage/queuefs/semantic_lock.py
index d4b0c7794..672c0f57e 100644
--- a/openviking/storage/queuefs/semantic_lock.py
+++ b/openviking/storage/queuefs/semantic_lock.py
@@ -7,7 +7,7 @@ from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Callable, Dict, Iterable, Optional
-from openviking.storage.errors import LockAcquisitionError
+from openviking.storage.errors import InvalidPathLockRequestError, LockAcquisitionError
from openviking.storage.internal_names import MULTIWRITE_PATH_LOCK_FILE
from openviking.storage.pathlock_transfer import PathLockGuard
from openviking.storage.viking_fs import get_viking_fs
@@ -18,6 +18,18 @@ logger = get_logger(__name__)
_TREE_LOCK_SUFFIX = f"/{MULTIWRITE_PATH_LOCK_FILE}"
+def _is_recoverable_handoff_error(error: Exception) -> bool:
+ """Return whether a durable handoff can be replaced by logical reacquisition."""
+ message = str(error)
+ if isinstance(error, LockAcquisitionError):
+ return "is no longer owned by" in message or (
+ "logical-path provider handoff requires covered_paths" in message
+ )
+ return isinstance(error, InvalidPathLockRequestError) and (
+ "handoff coverage" in message and "does not map to lock path" in message
+ )
+
+
def _tree_paths_from_handoff(lock_paths: Iterable[str]) -> list[str]:
"""Extract tree lock directory paths from legacy lock file handles."""
tree_paths: list[str] = []
@@ -55,13 +67,10 @@ class SemanticLockScope:
viking_fs = get_viking_fs()
try:
return cls(await viking_fs._async_agfs.pathlock_adopt(lock_handoff), _owned=True)
- except LockAcquisitionError as error:
+ except (InvalidPathLockRequestError, LockAcquisitionError) as error:
# ponytail: Replace this message check with a recoverable handoff
# exception when the native binding exposes typed handoff failures.
- error_message = str(error)
- if "is no longer owned by" not in error_message and (
- "logical-path provider handoff requires covered_paths" not in error_message
- ):
+ if not _is_recoverable_handoff_error(error):
raise
covered_paths = lock_handoff.get("covered_paths") or []
if not covered_paths:
diff --git a/openviking/storage/queuefs/semantic_processor.py b/openviking/storage/queuefs/semantic_processor.py
index d0e1ba136..03d39b4a9 100644
--- a/openviking/storage/queuefs/semantic_processor.py
+++ b/openviking/storage/queuefs/semantic_processor.py
@@ -49,7 +49,7 @@ from openviking.storage.abstract_overview import (
plan_abstract_overview_refresh,
write_abstract_overview,
)
-from openviking.storage.errors import LockAcquisitionError
+from openviking.storage.errors import InvalidPathLockRequestError, LockAcquisitionError
from openviking.storage.index_action import FieldPatch
from openviking.storage.pathlock_transfer import PathLockGuard
from openviking.storage.queuefs.named_queue import DequeueHandlerBase
@@ -84,6 +84,7 @@ _PERMANENT_STORAGE_ERRORS = (
IsADirectoryError,
NotADirectoryError,
AGFSNotADirectoryError,
+ InvalidPathLockRequestError,
)
diff --git a/openviking/utils/model_retry.py b/openviking/utils/model_retry.py
index f8a8584d3..74a511d60 100644
--- a/openviking/utils/model_retry.py
+++ b/openviking/utils/model_retry.py
@@ -16,6 +16,7 @@ from openviking.models.model_error import (
model_error_kind,
)
from openviking.pyagfs.exceptions import AGFSNotADirectoryError
+from openviking.storage.errors import InvalidPathLockRequestError
logger = logging.getLogger(__name__)
@@ -170,6 +171,7 @@ _PERMANENT_IO_ERRORS = (
IsADirectoryError,
NotADirectoryError,
AGFSNotADirectoryError,
+ InvalidPathLockRequestError,
)
TRANSIENT_API_ERROR_PATTERNS = (
diff --git a/tests/pyagfs/test_async_client_fs_ctx.py b/tests/pyagfs/test_async_client_fs_ctx.py
index e1fa97273..7807d01d0 100644
--- a/tests/pyagfs/test_async_client_fs_ctx.py
+++ b/tests/pyagfs/test_async_client_fs_ctx.py
@@ -6,6 +6,7 @@ import pytest
from openviking.pyagfs.async_client import AsyncAGFSClient
from openviking.pyagfs.helpers import cp
+from openviking.storage.errors import InvalidPathLockRequestError
class _RecordingClient:
@@ -94,9 +95,7 @@ async def test_async_client_passes_directories_only_to_tree_directory() -> None:
await agfs.tree_directory("/local/acct-1/data", directories_only=True)
- assert client.calls == [
- ("tree_directory", "/local/acct-1/data", {"account_id": "acct-1"})
- ]
+ assert client.calls == [("tree_directory", "/local/acct-1/data", {"account_id": "acct-1"})]
assert client.tree_options["directories_only"] is True
@@ -123,6 +122,26 @@ async def test_async_client_rejects_cross_account_mv() -> None:
await agfs.mv("/local/a/data/file.txt", "/local/b/data/file.txt")
+async def test_async_client_types_native_invalid_pathlock_handoff() -> None:
+ class _InvalidHandoffClient:
+ def pathlock_adopt(self, ctx, handoff_ref):
+ del ctx, handoff_ref
+ raise ValueError(
+ "invalid lock request: handoff coverage '/local/default/resources/"
+ "信贷风险管理_1.md' does not map to lock path "
+ "'/local/default/resources/.exact.ovlock.1.md.deadbeef'"
+ )
+
+ agfs = AsyncAGFSClient(_InvalidHandoffClient())
+
+ with pytest.raises(InvalidPathLockRequestError) as exc_info:
+ await agfs.pathlock_adopt({"owner_id": "producer"})
+
+ assert "handoff coverage" in str(exc_info.value)
+ assert isinstance(exc_info.value.__cause__, ValueError)
+
+
def test_cp_rejects_cross_account_raw_copy() -> None:
class _CpClient:
"""Minimal cp client; methods must not be called after account guard fails."""
diff --git a/tests/storage/test_semantic_processor_lock_ownership.py b/tests/storage/test_semantic_processor_lock_ownership.py
index a3f5368e9..5eedf5207 100644
--- a/tests/storage/test_semantic_processor_lock_ownership.py
+++ b/tests/storage/test_semantic_processor_lock_ownership.py
@@ -12,8 +12,10 @@ from unittest.mock import AsyncMock
import pytest
+from openviking.storage.errors import InvalidPathLockRequestError
from openviking.storage.queuefs.process_result import ProcessOutcome
from openviking.storage.queuefs.semantic_executor import SemanticTreeStats
+from openviking.storage.queuefs.semantic_lock import SemanticLockScope
from openviking.storage.queuefs.semantic_msg import SemanticMsg
from openviking.storage.queuefs.semantic_processor import SemanticProcessor
@@ -73,6 +75,41 @@ async def test_memory_semantic_directory_does_not_release_borrowed_lock(monkeypa
assert pathlock.release_calls == []
+async def test_semantic_lock_reacquires_unicode_coverage_after_legacy_mapping_error(monkeypatch):
+ covered_path = "/local/default/resources/agent-memory/credit-risk/信贷风险管理_1.md"
+ lock_path = (
+ "/local/default/resources/agent-memory/credit-risk/.exact.ovlock.1.md.14dd8bad93aaa5b0"
+ )
+ lease = {"id": "reacquired-lock"}
+ pathlock = SimpleNamespace(
+ pathlock_adopt=AsyncMock(
+ side_effect=InvalidPathLockRequestError(
+ f"invalid lock request: handoff coverage '{covered_path}' "
+ f"does not map to lock path '{lock_path}'"
+ )
+ ),
+ pathlock_acquire_batch=AsyncMock(return_value=lease),
+ pathlock_release=AsyncMock(),
+ )
+ monkeypatch.setattr(
+ "openviking.storage.queuefs.semantic_lock.get_viking_fs",
+ lambda: _FakeVikingFS(pathlock),
+ )
+ handoff = {
+ "owner_id": "legacy-owner",
+ "lock_paths": [lock_path],
+ "covered_paths": [{"path": covered_path, "kind": "tree"}],
+ }
+
+ scope = await SemanticLockScope.resolve(handoff)
+
+ assert scope.lock == lease
+ pathlock.pathlock_acquire_batch.assert_awaited_once_with(handoff["covered_paths"])
+ await scope.close()
+ pathlock.pathlock_release.assert_awaited_once_with(lease)
+
+
def test_semantic_tree_stats_aggregate_multiple_plans_for_one_request():
telemetry_id = "reindex-request-stats"
SemanticProcessor._cache_tree_stats(
diff --git a/tests/storage/test_semantic_processor_permanent_storage_error.py b/tests/storage/test_semantic_processor_permanent_storage_error.py
index eaefba68f..0d55a5577 100644
--- a/tests/storage/test_semantic_processor_permanent_storage_error.py
+++ b/tests/storage/test_semantic_processor_permanent_storage_error.py
@@ -236,6 +236,21 @@ async def test_lock_error_requeues_without_consuming_failure_budget(semantic_env
sleep.assert_not_awaited()
+async def test_unknown_semantic_error_is_terminal_without_tripping_breaker(semantic_env):
+ env = semantic_env
+ msg = env.message()
+ error = RuntimeError("unexpected semantic invariant violation")
+ env.dag.run.side_effect = error
+
+ result = await asyncio.wait_for(env.processor.on_dequeue(msg.to_dict()), timeout=1)
+
+ assert result.outcome is ProcessOutcome.FAILED
+ assert result.error == str(error)
+ env.retry_queue.enqueue.assert_not_awaited()
+ env.processor._account_breaker(msg.account_id).check()
+
+
@pytest.mark.asyncio
async def test_semantic_model_error_fails_without_requeue(semantic_env):
env = semantic_env
diff --git a/tests/unit/test_model_retry.py b/tests/unit/test_model_retry.py
index 1bef4a9ea..07d54ea67 100644
--- a/tests/unit/test_model_retry.py
+++ b/tests/unit/test_model_retry.py
@@ -15,6 +15,7 @@ from openviking.pyagfs.exceptions import (
AGFSNotADirectoryError,
AGFSTimeoutError,
)
+from openviking.storage.errors import InvalidPathLockRequestError
from openviking.utils.exceptions import AllCredentialsFailedError
from openviking.utils.model_retry import (
ERROR_CLASS_AUTH,
@@ -40,6 +41,7 @@ from openviking.utils.model_retry import (
IsADirectoryError,
NotADirectoryError,
AGFSNotADirectoryError,
+ InvalidPathLockRequestError,
],
)
@pytest.mark.parametrize("message", ["/resources/notes.txt", "timeout at /arbitrary/input.md"])