Skip to content

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

fix(semantic): skip contended parent freshness updates (#4559)

Qin Haojiec486088ee7c4
diff --git a/openviking/storage/abstract_overview.py b/openviking/storage/abstract_overview.py
index f11c16f8b..d539da1dd 100644
--- a/openviking/storage/abstract_overview.py
+++ b/openviking/storage/abstract_overview.py
@@ -488,6 +488,7 @@ async def plan_abstract_overview_refresh(
force_refresh: bool = False,
overview_sample_limit: int = 32,
refresh_ratio: float = 0.10,
+ lock_timeout_secs: float = 0.0,
) -> FreshnessDecision:
"""Atomically record direct-child changes and choose refresh scheduling.
@@ -530,7 +531,9 @@ async def plan_abstract_overview_refresh(
force_refresh=force_refresh,
)
lock_paths = [viking_fs._uri_to_path(uri, ctx=ctx) for uri in uris]
- lease = await viking_fs._async_agfs.pathlock_acquire_exact_batch(lock_paths)
+ lease = await viking_fs._async_agfs.pathlock_acquire_exact_batch(
+ lock_paths, timeout_secs=lock_timeout_secs
+ )
try:
documents = [await _read_existing_document(viking_fs, uri, ctx) for uri in uris]
baselines = [
diff --git a/openviking/storage/queuefs/semantic_processor.py b/openviking/storage/queuefs/semantic_processor.py
index 05163b646..fb5b9a671 100644
--- a/openviking/storage/queuefs/semantic_processor.py
+++ b/openviking/storage/queuefs/semantic_processor.py
@@ -264,19 +264,28 @@ class SemanticProcessor(DequeueHandlerBase):
return
parent_ctx = self._ctx_from_semantic_msg(msg)
semantic_config = get_openviking_config().semantic
- decision = await plan_abstract_overview_refresh(
- viking_fs=get_viking_fs(),
- dir_uri=parent_uri,
- changed_entries=1,
- ctx=parent_ctx,
- l0_body_changed=l0_body_changed,
- # This helper handles automatic upward propagation only. Manual
- # refresh/ingest bypasses the threshold for its requested root,
- # not for every ancestor reached afterwards.
- force_refresh=False,
- overview_sample_limit=getattr(semantic_config, "overview_sample_limit", 32),
- refresh_ratio=getattr(semantic_config, "freshness_refresh_ratio", 0.10),
- )
+ try:
+ decision = await plan_abstract_overview_refresh(
+ viking_fs=get_viking_fs(),
+ dir_uri=parent_uri,
+ changed_entries=1,
+ ctx=parent_ctx,
+ l0_body_changed=l0_body_changed,
+ # This helper handles automatic upward propagation only. Manual
+ # refresh/ingest bypasses the threshold for its requested root,
+ # not for every ancestor reached afterwards.
+ force_refresh=False,
+ overview_sample_limit=getattr(semantic_config, "overview_sample_limit", 32),
+ refresh_ratio=getattr(semantic_config, "freshness_refresh_ratio", 0.10),
+ lock_timeout_secs=1.0,
+ )
+ except LockAcquisitionError:
+ logger.info(
+ "Skipping best-effort parent freshness update because sidecars are busy: %s",
+ parent_uri,
+ )
+ return
+
if decision.action is not FreshnessAction.REFRESH_NOW:
logger.debug(
"Parent semantic refresh %s for %s (pending=%d, total=%d)",
@@ -290,8 +299,6 @@ class SemanticProcessor(DequeueHandlerBase):
from openviking.storage.queuefs import get_queue_manager
queue_manager = get_queue_manager()
- if queue_manager is None:
- return
semantic_queue = queue_manager.get_queue(queue_manager.SEMANTIC, allow_create=True)
parent_msg = SemanticMsg(
uri=parent_uri,
diff --git a/tests/storage/test_abstract_overview_freshness.py b/tests/storage/test_abstract_overview_freshness.py
index e6d0ae31d..62eed2678 100644
--- a/tests/storage/test_abstract_overview_freshness.py
+++ b/tests/storage/test_abstract_overview_freshness.py
@@ -21,6 +21,7 @@ class _FakeFS:
self.files = dict(files)
self._async_agfs = self
self._lock = asyncio.Lock()
+ self.lock_timeouts = []
async def read_file(self, uri, ctx=None):
if uri not in self.files:
@@ -33,7 +34,8 @@ class _FakeFS:
def _uri_to_path(self, uri, ctx=None):
return uri
- async def pathlock_acquire_exact_batch(self, paths):
+ async def pathlock_acquire_exact_batch(self, paths, timeout_secs=0.0):
+ self.lock_timeouts.append(timeout_secs)
await self._lock.acquire()
return {"paths": paths}
@@ -66,6 +68,7 @@ async def test_pending_increment_and_threshold_decision_share_one_snapshot():
ctx=None,
overview_sample_limit=32,
refresh_ratio=0.10,
+ lock_timeout_secs=1.0,
)
second = await plan_abstract_overview_refresh(
viking_fs=fs,
@@ -78,6 +81,7 @@ async def test_pending_increment_and_threshold_decision_share_one_snapshot():
assert first.action is FreshnessAction.MARK_PENDING
assert second.action is FreshnessAction.REFRESH_NOW
+ assert fs.lock_timeouts == [1.0, 0.0]
for raw in fs.files.values():
assert parse_abstract_overview(raw).metadata["freshness"][
"pending_child_changes"
diff --git a/tests/storage/test_semantic_parent_bubbling.py b/tests/storage/test_semantic_parent_bubbling.py
index 778fd57ec..3fa52653b 100644
--- a/tests/storage/test_semantic_parent_bubbling.py
+++ b/tests/storage/test_semantic_parent_bubbling.py
@@ -7,6 +7,7 @@ from unittest.mock import AsyncMock
import pytest
from openviking.server.identity import Role
+from openviking.storage.errors import LockAcquisitionError
from openviking.storage.queuefs.semantic_msg import SemanticMsg
from openviking.storage.queuefs.semantic_ops.freshness_policy import (
FreshnessAction,
@@ -129,13 +130,7 @@ async def test_parent_refresh_stops_at_nonsemantic_namespace_root(
async def test_parent_refresh_preserves_semantic_roots(
monkeypatch, uri, context_type, expected_parent
):
- plan = AsyncMock(
- return_value=FreshnessDecision(
- FreshnessAction.NOOP,
- pending_after=0,
- total_entries=1,
- )
- )
+ plan = AsyncMock(side_effect=LockAcquisitionError("parent sidecars are busy"))
monkeypatch.setattr(
"openviking.storage.queuefs.semantic_processor.plan_abstract_overview_refresh",
plan,
@@ -153,6 +148,12 @@ async def test_parent_refresh_preserves_semantic_roots(
"openviking.storage.queuefs.semantic_processor.get_viking_fs",
lambda: SimpleNamespace(),
)
+ get_queue_manager = AsyncMock(
+ side_effect=AssertionError("best-effort lock miss must not enqueue parent work")
+ )
+ monkeypatch.setattr(
+ "openviking.storage.queuefs.get_queue_manager", get_queue_manager
+ )
msg = SemanticMsg(uri=uri, context_type=context_type)
await SemanticProcessor()._enqueue_parent_refresh(
@@ -163,3 +164,5 @@ async def test_parent_refresh_preserves_semantic_roots(
plan.assert_awaited_once()
assert plan.await_args.kwargs["dir_uri"] == expected_parent
+ assert plan.await_args.kwargs["lock_timeout_secs"] == 1.0
+ get_queue_manager.assert_not_called()