Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(resource): preserve reservation backpressure errors (#3201)
huangruiteng5b3c92da6684
diff --git a/openviking/service/resource_service.py b/openviking/service/resource_service.pyindex ac8dae028..b738b60c8 100644--- a/openviking/service/resource_service.py+++ b/openviking/service/resource_service.py@@ -230,12 +230,16 @@ class ResourceService:except ConflictError:raiseexcept Exception as e:- logger.warning(f"[ResourceService] Failed to create watch task for {watch_to}: {e}")+ logger.warning(+ f"[ResourceService] Failed to create watch task for {watch_to}: {e}"+ )elif target.to:try:await self._handle_watch_task_cancellation(to_uri=target.to, ctx=ctx)except Exception as e:- logger.warning(f"[ResourceService] Failed to cancel watch task for {target.to}: {e}")+ logger.warning(+ f"[ResourceService] Failed to cancel watch task for {target.to}: {e}"+ )def _normalize_add_resource_args(self,@@ -722,7 +726,10 @@ class ResourceService:)doc_name = self._target_doc_name(path, source_name, source_info)source_path = source_info.source_path or source_name or path- root_uri, candidate_uri = await self._resource_processor.tree_builder.resolve_target_uri(+ (+ root_uri,+ candidate_uri,+ ) = await self._resource_processor.tree_builder.resolve_target_uri(ctx=ctx,doc_name=doc_name,scope="resources",@@ -743,7 +750,9 @@ class ResourceService:async def _reserve_tree(uri: str) -> LockLease:dst_path = self._viking_fs._uri_to_path(uri, ctx=ctx)try:- return await OwnedLockLease.acquire_tree(lock_manager, dst_path, timeout=0.0)+ return await OwnedLockLease.acquire_tree(+ lock_manager, dst_path, timeout=0.0+ )except LockAcquisitionError as exc:raise ResourceBusyError(f"Resource is busy: {uri}",@@ -753,23 +762,10 @@ class ResourceService:) from excif candidate_uri:- max_attempts = 100- reserved = False- for attempt in range(max_attempts + 1):- attempt_uri = candidate_uri if attempt == 0 else f"{candidate_uri}_{attempt}"- if await self._viking_fs.exists(attempt_uri, ctx=ctx):- continue- try:- lock_lease = await _reserve_tree(attempt_uri)- root_uri = attempt_uri- reserved = True- break- except ResourceBusyError:- continue- if not reserved:- raise FileExistsError(- f"Cannot resolve unique name for {candidate_uri} after {max_attempts} attempts"- )+ root_uri, lock_lease = await self._resource_processor.reserve_unique_candidate(+ candidate_uri=candidate_uri,+ ctx=ctx,+ )else:lock_lease = await _reserve_tree(root_uri)diff --git a/openviking/utils/resource_processor.py b/openviking/utils/resource_processor.pyindex 2eb16bf71..7731a27e7 100644--- a/openviking/utils/resource_processor.py+++ b/openviking/utils/resource_processor.py@@ -17,13 +17,13 @@ from openviking.parse.tree_builder import TreeBuilderfrom openviking.server.identity import RequestContextfrom openviking.storage import VikingDBManagerfrom openviking.storage.errors import LockAcquisitionError+from openviking.storage.internal_names import STORAGE_INTERNAL_ENTRY_NAMESfrom openviking.storage.transaction import (LOCK_TIMEOUT_DEFAULT,NO_LOCK,LockLease,OwnedLockLease,)-from openviking.storage.internal_names import STORAGE_INTERNAL_ENTRY_NAMESfrom openviking.storage.viking_fs import LS_ALL_NODES, get_viking_fsfrom openviking.telemetry import get_current_telemetryfrom openviking.utils.embedding_utils import index_resource@@ -430,6 +430,7 @@ class ResourceProcessor:viking_fs = get_viking_fs()lock_manager = get_lock_manager()+ last_busy_error: Optional[ResourceBusyError] = Nonefor attempt in range(max_attempts + 1):root_uri = candidate_uri if attempt == 0 else f"{candidate_uri}_{attempt}"@@ -442,9 +443,19 @@ class ResourceProcessor:lock_manager, dst_path, uri=root_uri, timeout=0.0)return root_uri, resource_lock- except ResourceBusyError:+ except ResourceBusyError as exc:+ last_busy_error = exccontinue+ if last_busy_error is not None:+ raise ResourceBusyError(+ f"All auto-named candidates are temporarily busy for {candidate_uri} "+ f"after checking {max_attempts + 1} candidates",+ uri=candidate_uri,+ conflict_type="auto_name_reservation_busy",+ retryable=True,+ ) from last_busy_error+raise FileExistsError(f"Cannot resolve unique name for {candidate_uri} after {max_attempts} attempts")diff --git a/tests/unit/test_resource_reservation.py b/tests/unit/test_resource_reservation.pynew file mode 100644index 000000000..9e87a3c3a--- /dev/null+++ b/tests/unit/test_resource_reservation.py@@ -0,0 +1,96 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0++from unittest.mock import AsyncMock++import pytest++from openviking.storage.errors import ResourceBusyError+from openviking.utils import resource_processor as resource_processor_module+from openviking.utils.resource_processor import ResourceProcessor+++class _FakeVikingFS:+ def __init__(self, existing=()):+ self.existing = set(existing)++ async def exists(self, uri, *, ctx):+ return uri in self.existing++ def _uri_to_path(self, uri, *, ctx):+ return f"/agfs/{uri}"+++def _make_processor(monkeypatch, *, existing=()):+ processor = ResourceProcessor.__new__(ResourceProcessor)+ viking_fs = _FakeVikingFS(existing)+ monkeypatch.setattr(resource_processor_module, "get_viking_fs", lambda: viking_fs)+ monkeypatch.setattr(+ "openviking.storage.transaction.get_lock_manager",+ lambda: object(),+ )+ return processor+++async def test_reservation_exhaustion_reports_retryable_lock_contention(monkeypatch):+ processor = _make_processor(monkeypatch)+ processor.acquire_resource_lock = AsyncMock(+ side_effect=ResourceBusyError(+ "busy",+ uri="viking://resources/report",+ conflict_type="path_busy",+ )+ )++ with pytest.raises(ResourceBusyError) as exc_info:+ await processor.reserve_unique_candidate(+ candidate_uri="viking://resources/report",+ ctx=object(),+ max_attempts=2,+ )++ assert exc_info.value.uri == "viking://resources/report"+ assert exc_info.value.conflict_type == "auto_name_reservation_busy"+ assert exc_info.value.retryable is True+ assert "checking 3 candidates" in str(exc_info.value)+ assert processor.acquire_resource_lock.await_count == 3+++async def test_true_auto_name_exhaustion_remains_file_exists(monkeypatch):+ candidates = {+ "viking://resources/report",+ "viking://resources/report_1",+ "viking://resources/report_2",+ }+ processor = _make_processor(monkeypatch, existing=candidates)+ processor.acquire_resource_lock = AsyncMock()++ with pytest.raises(FileExistsError):+ await processor.reserve_unique_candidate(+ candidate_uri="viking://resources/report",+ ctx=object(),+ max_attempts=2,+ )++ processor.acquire_resource_lock.assert_not_awaited()+++async def test_reservation_returns_first_available_lock(monkeypatch):+ processor = _make_processor(+ monkeypatch,+ existing={"viking://resources/report"},+ )+ lease = object()+ processor.acquire_resource_lock = AsyncMock(return_value=lease)++ uri, acquired = await processor.reserve_unique_candidate(+ candidate_uri="viking://resources/report",+ ctx=object(),+ max_attempts=2,+ )++ assert uri == "viking://resources/report_1"+ assert acquired is lease