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: detach shared batch and snapshot fallback task contexts (#6331)
Lusen Dong35b51da9a89f
diff --git a/openviking/session/memory/utils/streaming_batcher.py b/openviking/session/memory/utils/streaming_batcher.pyindex d6644cf15..d500930e1 100644--- a/openviking/session/memory/utils/streaming_batcher.py+++ b/openviking/session/memory/utils/streaming_batcher.py@@ -8,10 +8,11 @@ import asyncioimport contextlibimport timeimport uuid-from collections.abc import Awaitable, Callable+from collections.abc import Awaitable, Callable, Coroutinefrom dataclasses import dataclass, fieldfrom typing import Any, Generic, TypeVar+from openviking.service.task_work_index import detach_task_contextfrom openviking.telemetry import tracerfrom openviking_cli.utils import get_logger@@ -21,6 +22,18 @@ T = TypeVar("T")R = TypeVar("R")+def _create_background_task(+ loop: asyncio.AbstractEventLoop,+ coroutine: Coroutine[Any, Any, R],+ *,+ name: str,+) -> asyncio.Task[R]:+ """Create shared batch work outside any submitter's Task lifecycle."""++ with detach_task_context():+ return loop.create_task(coroutine, name=name)++@dataclass(slots=True)class StreamingBatcherConfig:"""Count/time window configuration shared by streaming updaters."""@@ -130,7 +143,10 @@ class StreamingBatcher(Generic[T, R]):tracer.set("flush_reason", reason)tracer.set("request_count", len(items))tracer.set("input_size", sum(self._item_size(item.payload) for item in items))- result = await self.process_batch([item.payload for item in items], reason)+ # A shared batch has no single caller-owned Task, including+ # when flush() or close() is awaited directly.+ with detach_task_context():+ result = await self.process_batch([item.payload for item in items], reason)metadata = self._get_result_metadata(result)if metadata is not None:metadata.setdefault("batch_id", batch_id)@@ -156,7 +172,8 @@ class StreamingBatcher(Generic[T, R]):logger.warning("[%s] timer loop not started: no running event loop", self.name)self._timer_task = Nonereturn- self._timer_task = loop.create_task(+ self._timer_task = _create_background_task(+ loop,self._run_timer_loop(),name=f"{self.name}-flush-loop",)@@ -180,7 +197,8 @@ class StreamingBatcher(Generic[T, R]):try:await asyncio.sleep(self.config.timer_check_interval_seconds)if await self._should_flush_by_time_or_count():- flush_task = asyncio.create_task(+ flush_task = _create_background_task(+ asyncio.get_running_loop(),self.flush("time"),name=f"{self.name}-flush-time",)@@ -220,7 +238,11 @@ class StreamingBatcher(Generic[T, R]):except Exception as exc:logger.warning("[%s] background flush failed: %s", self.name, exc)- loop.create_task(_runner(), name=f"{self.name}-flush-{reason}")+ _create_background_task(+ loop,+ _runner(),+ name=f"{self.name}-flush-{reason}",+ )def _buffered_size_unlocked(self) -> int:return sum(self._item_size(item.payload) for item in self._buffer)diff --git a/openviking/storage/viking_fs/_snapshot.py b/openviking/storage/viking_fs/_snapshot.pyindex 9a7aa5506..eb1cb7c7a 100644--- a/openviking/storage/viking_fs/_snapshot.py+++ b/openviking/storage/viking_fs/_snapshot.py@@ -15,6 +15,7 @@ from openviking.pyagfs.exceptions import ()from openviking.server.error_mapping import is_not_found_error, map_exceptionfrom openviking.server.identity import RequestContext, Role+from openviking.service.task_work_index import detach_task_contextfrom openviking.storage.acl import AclActionfrom openviking.storage.viking_fs._base import (_prepare_snapshot_diff,@@ -827,18 +828,19 @@ class _SnapshotMixin:vlm_resolver=self._vlm_resolver,vector_config_resolver=self._vector_config_resolver,)- for op, uri, level in tasks:- loop.create_task(- self._run_vector_rebuild(- executor,- op,- uri,- level,- ctx,- file_task_owner="standalone",- ),- name=f"vikingfs-git-{op}:{uri}:{int(level)}",- )+ with detach_task_context():+ for op, uri, level in tasks:+ loop.create_task(+ self._run_vector_rebuild(+ executor,+ op,+ uri,+ level,+ ctx,+ file_task_owner="standalone",+ ),+ name=f"vikingfs-git-{op}:{uri}:{int(level)}",+ )async def _run_restore_rebuild_tracked(self,diff --git a/tests/unit/test_snapshot_rebuild_context.py b/tests/unit/test_snapshot_rebuild_context.pynew file mode 100644index 000000000..cf57e5d72--- /dev/null+++ b/tests/unit/test_snapshot_rebuild_context.py@@ -0,0 +1,51 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0++import asyncio+from unittest.mock import Mock++import pytest++from openviking.server.identity import RequestContext, Role+from openviking.service.task_work_index import bind_task_context, get_task_context+from openviking.storage.viking_fs import VikingFS+from openviking_cli.session.user_id import UserIdentifier+++async def test_fallback_rebuild_detaches_all_operations(monkeypatch):+ ctx = RequestContext(user=UserIdentifier("account", "user"), role=Role.ROOT)+ vfs = VikingFS(agfs=Mock())+ seen = []+ finished = asyncio.Event()++ async def record(op, uri, level, request_ctx):+ seen.append((op, get_task_context(), request_ctx))+ if len(seen) == 3:+ finished.set()++ class Executor:+ async def reindex_directory_marker(self, *, dir_uri, level, ctx):+ await record("marker", dir_uri, level, ctx)++ async def execute(self, *, uri, mode, wait, ctx):+ assert mode == "vectors_only" and wait+ await record("file", uri, 2, ctx)++ async def delete_uri_level(self, *, uri, level, ctx):+ await record("delete", uri, level, ctx)++ monkeypatch.setattr(+ "openviking.service.reindex_executor.ReindexExecutor", lambda **kwargs: Executor()+ )+ with bind_task_context("restore-caller", "account", "user"):+ owner = get_task_context()+ vfs._schedule_vector_rebuild(+ written=["resources/proj/.abstract.md", "resources/proj/file.md"],+ deleted=["resources/proj/deleted.md"],+ ctx=ctx,+ )+ assert get_task_context() is owner+ await asyncio.wait_for(finished.wait(), timeout=1)+ assert {op for op, _, _ in seen} == {"marker", "file", "delete"}+ assert all(context is None and request_ctx is ctx for _, context, request_ctx in seen)diff --git a/tests/unit/test_streaming_batcher.py b/tests/unit/test_streaming_batcher.pyindex fa5306fa7..f7db59ab5 100644--- a/tests/unit/test_streaming_batcher.py+++ b/tests/unit/test_streaming_batcher.py@@ -2,15 +2,125 @@# SPDX-License-Identifier: AGPL-3.0import asyncio+from contextvars import ContextVarimport pytest+from openviking.service.task_work_index import bind_task_context, get_task_contextfrom openviking.session.memory.utils.streaming_batcher import (StreamingBatcher,StreamingBatcherConfig,)[email protected]("close", [False, True])[email protected]("fail", [False, True])+async def test_explicit_flush_detaches_shared_batch_context(close, fail):+ trace = ContextVar[str | None]("batch_trace", default=None)+ seen = []++ async def process_batch(items, reason):+ seen.append((get_task_context(), trace.get(), items, reason))+ if fail:+ raise ValueError("batch failed")+ return ",".join(items)++ batcher = StreamingBatcher(+ name="explicit-context-test",+ process_batch=process_batch,+ config=StreamingBatcherConfig(max_wait_seconds=60),+ )+ submit = asyncio.create_task(batcher.submit("item"))+ try:+ await asyncio.sleep(0)+ assert await batcher.get_buffered_item_count() == 1+ token = trace.set("caller-trace")+ try:+ with bind_task_context("flush-caller", "account", "user"):+ owner = get_task_context()+ operation = batcher.close() if close else batcher.flush("manual")+ if fail:+ with pytest.raises(ValueError, match="batch failed") as error:+ await operation+ result = error.value+ else:+ result = await operation+ assert get_task_context() is owner+ assert trace.get() == "caller-trace"+ finally:+ trace.reset(token)+ submitted = await asyncio.gather(submit, return_exceptions=True)+ if fail:+ assert submitted[0] is result+ else:+ assert result == "item"+ assert submitted == ["item"]+ assert seen == [(None, "caller-trace", ["item"], "close" if close else "manual")]+ finally:+ await batcher.close()+++async def test_timer_flush_runs_without_submitter_context():+ seen_task_ids: list[str | None] = []++ async def process_batch(items: list[str], _reason: str) -> str:+ context = get_task_context()+ seen_task_ids.append(context.task_id if context is not None else None)+ return ",".join(items)++ batcher = StreamingBatcher(+ name="context-test",+ process_batch=process_batch,+ config=StreamingBatcherConfig(+ max_items_per_batch=10,+ max_wait_seconds=0.01,+ timer_check_interval_seconds=0.001,+ ),+ )+ with bind_task_context("first-task", "account", "user"):+ assert await asyncio.wait_for(batcher.submit("first"), timeout=1) == "first"++ assert await asyncio.wait_for(batcher.submit("second"), timeout=1) == "second"+ await batcher.close()++ assert seen_task_ids == [None, None]+++async def test_count_flush_runs_without_last_submitter_context():+ seen_task_ids: list[str | None] = []++ async def process_batch(items: list[str], _reason: str) -> str:+ context = get_task_context()+ seen_task_ids.append(context.task_id if context is not None else None)+ return ",".join(items)++ batcher = StreamingBatcher(+ name="context-test",+ process_batch=process_batch,+ config=StreamingBatcherConfig(+ max_items_per_batch=2,+ max_wait_seconds=10,+ timer_check_interval_seconds=1,+ ),+ )+ with bind_task_context("first-task", "account", "user"):+ first = asyncio.create_task(batcher.submit("first"))+ await asyncio.sleep(0)+ assert await batcher.get_buffered_item_count() == 1++ with bind_task_context("last-task", "account", "user"):+ second = asyncio.create_task(batcher.submit("second"))++ assert await asyncio.wait_for(first, timeout=1) == "first,second"+ assert await asyncio.wait_for(second, timeout=1) == "first,second"+ await batcher.close()++ assert seen_task_ids == [None]++@pytest.mark.asyncioasync def test_close_waits_for_in_flight_timer_flush():started = asyncio.Event()