Skip to content

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

fix(langchain): isolate async history and preserve cancellation semantics (#3575)

Hao Zhe8db65ea9de9e
diff --git a/docs/en/agent-integrations/07-langchain-langgraph.md b/docs/en/agent-integrations/07-langchain-langgraph.md
index 85b478925..de9f622d5 100644
--- a/docs/en/agent-integrations/07-langchain-langgraph.md
+++ b/docs/en/agent-integrations/07-langchain-langgraph.md
@@ -78,7 +78,20 @@ or reset it before selecting another workspace.
and `aclear()`. `OpenVikingSessionRecorder` provides `arecord()`, `aflush()`,
and `aclose()`. Async LangGraph runs select `awrap_model_call()` and
`aafter_agent()` automatically. Concurrent first use creates one internal HTTP
-client per adapter and event loop.
+client per adapter and event loop. Each `with_openviking_context()` invocation
+owns the history snapshot, peer identity, and recalled-context references used
+to record its turn. Concurrent calls may therefore share a session without a
+second live history read losing messages, and an abandoned stream does not hold
+a session-wide lifecycle lock. Only the final append-and-commit step is
+serialized per session and event loop.
+
+If cancellation occurs after a recorder has confirmed part of a write,
+`arecord()` re-raises the original `asyncio.CancelledError`. Pass that exception,
+or a wrapping `asyncio.TimeoutError`, to
+`get_openviking_cancellation_progress()` to inspect the confirmed message
+prefix or pending-commit state before retrying. Preserving the original
+cancellation object also preserves the standard `asyncio.wait_for()` and
+`asyncio.timeout()` timeout behavior.
Adapters never close an injected client. When an adapter creates its own client,
release it with `await retriever.aclose()`, `await assembler.aclose()`,
diff --git a/docs/zh/agent-integrations/07-langchain-langgraph.md b/docs/zh/agent-integrations/07-langchain-langgraph.md
index 1c35eb68a..daa58bb7b 100644
--- a/docs/zh/agent-integrations/07-langchain-langgraph.md
+++ b/docs/zh/agent-integrations/07-langchain-langgraph.md
@@ -75,7 +75,17 @@ workspace;切换 workspace 前应先关闭或 reset 当前 client。
`aclear()`;`OpenVikingSessionRecorder` 提供 `arecord()`、`aflush()` 和
`aclose()`。异步 LangGraph 运行会自动选择 `awrap_model_call()` 和
`aafter_agent()`。同一 adapter 首次被并发调用时,每个 event loop 只会创建一个内部
-HTTP client。
+HTTP client。每次 `with_openviking_context()` 调用都独立持有本次写入所需的 history
+快照、peer 身份和召回上下文引用。因此,同一 session 的调用可以并发执行,不会因为
+退出时再次读取实时 history 而丢失消息;未消费完的 stream 也不会占用 session 级
+lifecycle lock。只有最终的 append-and-commit 步骤会在同一个 event loop 中按 session
+串行执行。
+
+如果 recorder 已确认部分写入后任务被取消,`arecord()` 会重新抛出原始
+`asyncio.CancelledError`。可将该异常或外层的 `asyncio.TimeoutError` 传给
+`get_openviking_cancellation_progress()`,在重试前读取已确认写入的消息前缀或待
+commit 状态,避免重复写入。保留原始取消异常也会保留 `asyncio.wait_for()` 和
+`asyncio.timeout()` 的标准超时行为。
Adapter 不会关闭调用方注入的 client。对于 adapter 自行创建的 client,应按实际使用的组件调用
`await retriever.aclose()`、`await assembler.aclose()`、
diff --git a/openviking/integrations/langchain/__init__.py b/openviking/integrations/langchain/__init__.py
index 21cb4707e..bedca28d6 100644
--- a/openviking/integrations/langchain/__init__.py
+++ b/openviking/integrations/langchain/__init__.py
@@ -13,6 +13,7 @@ from typing import Any
__all__ = [
"InMemoryOpenVikingClient",
"OpenVikingChatMessageHistory",
+ "OpenVikingCancellationProgress",
"OpenVikingCommitPolicy",
"OpenVikingContextMiddleware",
"OpenVikingPartialWriteError",
@@ -22,6 +23,7 @@ __all__ = [
"OpenVikingSessionRecorder",
"OpenVikingStore",
"create_openviking_tools",
+ "get_openviking_cancellation_progress",
"with_openviking_context",
]
@@ -59,6 +61,18 @@ def __getattr__(name: str) -> Any:
from openviking.integrations.langchain.recording import OpenVikingRecordResult
return OpenVikingRecordResult
+ if name == "OpenVikingCancellationProgress":
+ from openviking.integrations.langchain.recording import (
+ OpenVikingCancellationProgress,
+ )
+
+ return OpenVikingCancellationProgress
+ if name == "get_openviking_cancellation_progress":
+ from openviking.integrations.langchain.recording import (
+ get_openviking_cancellation_progress,
+ )
+
+ return get_openviking_cancellation_progress
if name == "OpenVikingCommitPolicy":
from openviking.integrations.langchain.client import OpenVikingCommitPolicy
diff --git a/openviking/integrations/langchain/context.py b/openviking/integrations/langchain/context.py
index 4b292b5e2..5d57ead20 100644
--- a/openviking/integrations/langchain/context.py
+++ b/openviking/integrations/langchain/context.py
@@ -6,12 +6,13 @@ from __future__ import annotations
import asyncio
import logging
-from collections.abc import Callable
+from collections.abc import AsyncIterator, Callable, Sequence
+from contextlib import asynccontextmanager
from dataclasses import dataclass, field
-from typing import Any, Sequence
+from typing import Any
try:
- from langchain_core.messages import SystemMessage
+ from langchain_core.messages import BaseMessage, SystemMessage
from langchain_core.runnables import ConfigurableFieldSpec, RunnableLambda
from langchain_core.runnables.history import RunnableWithMessageHistory
except ImportError as exc: # pragma: no cover - exercised by optional import path
@@ -51,6 +52,77 @@ class OpenVikingAssembledContext:
recall_documents: list[Any] = field(default_factory=list)
+@dataclass(slots=True)
+class _AsyncSessionWriteLockEntry:
+ lock: asyncio.Lock = field(default_factory=asyncio.Lock)
+ users: int = 0
+
+
+class _AsyncSessionWriteLockPool:
+ """Serialize completed writes without retaining inactive session IDs."""
+
+ def __init__(self) -> None:
+ self._entries: dict[str, _AsyncSessionWriteLockEntry] = {}
+
+ @asynccontextmanager
+ async def acquire(self, session_id: str) -> AsyncIterator[None]:
+ entry = self._entries.get(session_id)
+ if entry is None:
+ entry = _AsyncSessionWriteLockEntry()
+ self._entries[session_id] = entry
+ entry.users += 1
+ acquired = False
+ try:
+ await entry.lock.acquire()
+ acquired = True
+ yield
+ finally:
+ if acquired:
+ entry.lock.release()
+ entry.users -= 1
+ if entry.users == 0 and self._entries.get(session_id) is entry:
+ self._entries.pop(session_id, None)
+
+
+class _InvocationOpenVikingChatMessageHistory(OpenVikingChatMessageHistory):
+ """Hold the entry history snapshot for one runnable invocation."""
+
+ def __init__(
+ self,
+ *args: Any,
+ async_write_lock_pools: LoopScopedAsyncClientCache,
+ **kwargs: Any,
+ ):
+ super().__init__(*args, **kwargs)
+ self._entry_snapshot: list[BaseMessage] | None = None
+ self._async_write_lock_pools = async_write_lock_pools
+
+ @property
+ def messages(self) -> list[BaseMessage]:
+ if self._entry_snapshot is None:
+ self._entry_snapshot = list(super().messages)
+ return list(self._entry_snapshot)
+
+ async def aget_messages(self) -> list[BaseMessage]:
+ if self._entry_snapshot is None:
+ self._entry_snapshot = list(await super().aget_messages())
+ return list(self._entry_snapshot)
+
+ def add_messages(self, messages: Sequence[BaseMessage]) -> None:
+ try:
+ super().add_messages(messages)
+ finally:
+ self._entry_snapshot = None
+
+ async def aadd_messages(self, messages: Sequence[BaseMessage]) -> None:
+ try:
+ lock_pool = self._async_write_lock_pools.get(_AsyncSessionWriteLockPool)
+ async with lock_pool.acquire(self.session_id):
+ await super().aadd_messages(messages)
+ finally:
+ self._entry_snapshot = None
+
+
class OpenVikingSessionContextAssembler:
"""Assemble session working memory, archive context, and recall results."""
@@ -379,7 +451,7 @@ def with_openviking_context(
peer_id_config_key: str = "peer_id",
inject_context: bool = True,
) -> RunnableWithMessageHistory:
- """Wrap a LangChain runnable with OpenViking context and message history."""
+ """Wrap a runnable with invocation-scoped OpenViking history and context."""
assembler = OpenVikingSessionContextAssembler(
client=client,
@@ -401,29 +473,13 @@ def with_openviking_context(
include_active_messages=False,
include_recall=inject_context,
)
- pending_context_parts: dict[tuple[str, str], list[dict[str, Any]]] = {}
- active_peer_ids: dict[str, str | None] = {}
+ async_write_lock_pools = LoopScopedAsyncClientCache()
def make_history(active_session_id: str) -> OpenVikingChatMessageHistory:
- def pending_key(current_session_id: str) -> tuple[str, str]:
- return _pending_context_key(
- current_session_id,
- active_peer_ids.get(current_session_id, peer_id),
- )
-
- def provide_context_parts(current_session_id: str) -> list[dict[str, Any]]:
- return list(pending_context_parts.get(pending_key(current_session_id), []))
-
- def acknowledge_context_parts(current_session_id: str) -> None:
- pending_context_parts.pop(pending_key(current_session_id), None)
-
- return OpenVikingChatMessageHistory(
+ return _InvocationOpenVikingChatMessageHistory(
session_id=active_session_id,
+ async_write_lock_pools=async_write_lock_pools,
peer_id=peer_id,
- peer_id_provider=lambda current_session_id: active_peer_ids.get(
- current_session_id,
- peer_id,
- ),
client=client,
async_client=async_client,
url=url,
@@ -438,8 +494,6 @@ def with_openviking_context(
auto_initialize=auto_initialize,
token_budget=token_budget,
commit_policy=commit_policy,
- context_parts_provider=provide_context_parts,
- context_parts_acknowledger=acknowledge_context_parts,
)
if session_id is None:
@@ -475,7 +529,7 @@ def with_openviking_context(
def prepare_injection(
input_value: Any,
config: dict[str, Any] | None,
- ) -> tuple[str, tuple[str, str], str] | None:
+ ) -> tuple[str, _InvocationOpenVikingChatMessageHistory, str] | None:
resolved_session_id = session_id or _session_id_from_config(
config,
key=session_id_config_key,
@@ -485,55 +539,64 @@ def with_openviking_context(
key=peer_id_config_key,
default=peer_id,
)
- active_peer_ids[resolved_session_id] = resolved_peer_id
+ history = _invocation_history_from_config(config)
+ history._prepare_invocation(peer_id=resolved_peer_id)
if not inject_context:
return None
- pending_key = _pending_context_key(resolved_session_id, resolved_peer_id)
- pending_context_parts.pop(pending_key, None)
query = _latest_user_text_from_input(input_value, input_messages_key)
- return resolved_session_id, pending_key, query
+ return resolved_session_id, history, query
def apply_injection(
input_value: Any,
- pending_key: tuple[str, str],
+ history: _InvocationOpenVikingChatMessageHistory,
assembled: OpenVikingAssembledContext,
) -> Any:
if not assembled.block:
return input_value
if assembled.context_parts:
- pending_context_parts[pending_key] = assembled.context_parts
+ history._prepare_invocation(
+ peer_id=history.peer_id,
+ context_parts=assembled.context_parts,
+ )
return _inject_system_context(input_value, assembled.block, input_messages_key)
def inject(input_value: Any, config: dict[str, Any] | None = None) -> Any:
prepared = prepare_injection(input_value, config)
if prepared is None:
return input_value
- resolved_session_id, pending_key, query = prepared
+ resolved_session_id, history, query = prepared
assembled = assembler.assemble(
session_id=resolved_session_id,
query=query,
)
- return apply_injection(input_value, pending_key, assembled)
+ return apply_injection(input_value, history, assembled)
async def ainject(input_value: Any, config: dict[str, Any] | None = None) -> Any:
prepared = prepare_injection(input_value, config)
if prepared is None:
return input_value
- resolved_session_id, pending_key, query = prepared
+ resolved_session_id, history, query = prepared
assembled = await assembler.aassemble(
session_id=resolved_session_id,
query=query,
)
- return apply_injection(input_value, pending_key, assembled)
+ return apply_injection(input_value, history, assembled)
def clear_pending_on_error(_run: Any, config: dict[str, Any] | None = None) -> None:
- del config
- pending_context_parts.clear()
+ try:
+ history = _invocation_history_from_config(config)
+ except Exception:
+ logger.debug(
+ "OpenViking pending context cleanup could not resolve invocation history",
+ exc_info=True,
+ )
+ return
+ history._discard_invocation_context()
bound = (RunnableLambda(inject, afunc=ainject) | runnable).with_listeners(
on_error=clear_pending_on_error
)
- return RunnableWithMessageHistory(
+ wrapped = RunnableWithMessageHistory(
bound,
session_history_factory,
input_messages_key=input_messages_key,
@@ -541,6 +604,7 @@ def with_openviking_context(
history_messages_key=history_messages_key,
history_factory_config=history_factory_config,
)
+ return wrapped
def _session_id_from_config(config: dict[str, Any] | None, *, key: str) -> str:
@@ -573,6 +637,16 @@ def _validate_session_id(value: Any, *, key: str) -> str:
return session_id
+def _invocation_history_from_config(
+ config: dict[str, Any] | None,
+) -> _InvocationOpenVikingChatMessageHistory:
+ configurable = (config or {}).get("configurable") or {}
+ history = configurable.get("message_history")
+ if not isinstance(history, _InvocationOpenVikingChatMessageHistory):
+ raise RuntimeError("OpenViking runnable invocation history is unavailable")
+ return history
+
+
def _retriever_for_session(
retriever: Any,
session_id: str,
@@ -586,10 +660,6 @@ def _retriever_for_session(
return retriever
-def _pending_context_key(session_id: str, peer_id: str | None) -> tuple[str, str]:
- return (session_id, peer_id or "")
-
-
def _latest_user_text_from_input(input_value: Any, input_messages_key: str | None) -> str:
messages = _input_messages(input_value, input_messages_key)
if messages:
diff --git a/openviking/integrations/langchain/history.py b/openviking/integrations/langchain/history.py
index 4f27328b3..e02820392 100644
--- a/openviking/integrations/langchain/history.py
+++ b/openviking/integrations/langchain/history.py
@@ -4,6 +4,7 @@
from __future__ import annotations
+import asyncio
import logging
from collections.abc import Sequence
from typing import Any, Callable
@@ -33,6 +34,7 @@ from openviking.integrations.langchain.messages import (
from openviking.integrations.langchain.recording import (
OpenVikingPartialWriteError,
OpenVikingSessionRecorder,
+ get_openviking_cancellation_progress,
)
logger = logging.getLogger(__name__)
@@ -169,6 +171,11 @@ class OpenVikingChatMessageHistory(BaseChatMessageHistory):
if exc.context_attached:
self._acknowledge_context_parts()
raise
+ except asyncio.CancelledError as exc:
+ progress = get_openviking_cancellation_progress(exc)
+ if progress is not None and progress.context_attached:
+ self._acknowledge_context_parts()
+ raise
if result.context_attached:
self._acknowledge_context_parts()
@@ -227,6 +234,22 @@ class OpenVikingChatMessageHistory(BaseChatMessageHistory):
text = str(value).strip()
return text or None
+ def _prepare_invocation(
+ self,
+ *,
+ peer_id: str | None,
+ context_parts: Sequence[dict[str, Any]] = (),
+ ) -> None:
+ """Set invocation-local attribution and recalled context."""
+
+ self.peer_id = peer_id
+ self._pending_context_parts = list(context_parts)
+
+ def _discard_invocation_context(self) -> None:
+ """Discard recalled context owned by an interrupted invocation."""
+
+ self._pending_context_parts = []
+
def _acknowledge_context_parts(self) -> None:
had_pending_context = bool(self._pending_context_parts)
self._pending_context_parts = []
diff --git a/openviking/integrations/langchain/middleware.py b/openviking/integrations/langchain/middleware.py
index 84349765e..75c6e93ea 100644
--- a/openviking/integrations/langchain/middleware.py
+++ b/openviking/integrations/langchain/middleware.py
@@ -4,6 +4,7 @@
from __future__ import annotations
+import asyncio
import json
from dataclasses import dataclass
from typing import Any, Callable
@@ -33,8 +34,10 @@ from openviking.integrations.langchain.context import (
OpenVikingSessionContextAssembler,
)
from openviking.integrations.langchain.recording import (
+ OpenVikingCancellationProgress,
OpenVikingPartialWriteError,
OpenVikingSessionRecorder,
+ get_openviking_cancellation_progress,
)
from openviking.integrations.langchain.retrievers import OpenVikingRetriever
@@ -294,6 +297,11 @@ class OpenVikingContextMiddleware(AgentMiddleware):
except OpenVikingPartialWriteError as exc:
self._handle_partial_capture(plan, exc)
raise
+ except asyncio.CancelledError as exc:
+ progress = get_openviking_cancellation_progress(exc)
+ if progress is not None:
+ self._handle_partial_capture(plan, progress)
+ raise
self._complete_capture(plan, context_attached=result.context_attached)
return None
@@ -336,7 +344,7 @@ class OpenVikingContextMiddleware(AgentMiddleware):
def _handle_partial_capture(
self,
plan: _CapturePlan,
- error: OpenVikingPartialWriteError,
+ error: OpenVikingPartialWriteError | OpenVikingCancellationProgress,
) -> None:
if error.input_messages_consumed:
consumed_end = plan.start + error.input_messages_consumed
diff --git a/openviking/integrations/langchain/recording.py b/openviking/integrations/langchain/recording.py
index f14ea511d..1667ff1fb 100644
--- a/openviking/integrations/langchain/recording.py
+++ b/openviking/integrations/langchain/recording.py
@@ -96,6 +96,78 @@ class OpenVikingPartialWriteError(RuntimeError):
return self.stage == "commit"
+@dataclass(frozen=True, slots=True)
+class OpenVikingCancellationProgress:
+ """Confirmed recording progress attached to an original cancellation."""
+
+ session_id: str
+ result: OpenVikingRecordResult
+ stage: Literal["batch", "commit"] = "batch"
+
+ @property
+ def messages_written(self) -> int:
+ """Return the number of payloads confirmed written before cancellation."""
+
+ return self.result.messages_written
+
+ @property
+ def input_messages_consumed(self) -> int:
+ """Return the caller-message prefix that is safe to skip on retry."""
+
+ return self.result.input_messages_consumed
+
+ @property
+ def context_attached(self) -> bool:
+ """Return whether recalled context was confirmed persisted."""
+
+ return self.result.context_attached
+
+ @property
+ def commit_pending(self) -> bool:
+ """Return whether only the post-write commit remains incomplete."""
+
+ return self.stage == "commit"
+
+
+_CANCELLATION_PROGRESS_ATTRIBUTE = "_openviking_recording_progress"
+
+
+def get_openviking_cancellation_progress(
+ error: BaseException,
+) -> OpenVikingCancellationProgress | None:
+ """Return recording progress from a cancellation or a timeout wrapping it."""
+
+ current: BaseException | None = error
+ seen: set[int] = set()
+ while current is not None and id(current) not in seen:
+ seen.add(id(current))
+ progress = getattr(current, _CANCELLATION_PROGRESS_ATTRIBUTE, None)
+ if isinstance(progress, OpenVikingCancellationProgress):
+ return progress
+ current = current.__cause__ or current.__context__
+ return None
+
+
+def _attach_cancellation_progress(
+ error: asyncio.CancelledError,
+ *,
+ session_id: str,
+ result: OpenVikingRecordResult,
+ stage: Literal["batch", "commit"] = "batch",
+) -> None:
+ """Annotate the original cancellation without changing its identity."""
+
+ setattr(
+ error,
+ _CANCELLATION_PROGRESS_ATTRIBUTE,
+ OpenVikingCancellationProgress(
+ session_id=session_id,
+ result=result,
+ stage=stage,
+ ),
+ )
+
+
@dataclass(slots=True)
class _PreparedMessage:
input_end: int
@@ -268,6 +340,20 @@ class OpenVikingSessionRecorder:
session_id=session_id,
messages=batch.payloads,
)
+ except asyncio.CancelledError as exc:
+ if messages_written == 0:
+ raise
+ result = OpenVikingRecordResult(
+ messages_written=messages_written,
+ input_messages_consumed=input_messages_consumed,
+ context_attached=context_attached,
+ )
+ _attach_cancellation_progress(
+ exc,
+ session_id=session_id,
+ result=result,
+ )
+ raise
except Exception as exc:
if messages_written == 0:
raise
@@ -291,6 +377,15 @@ class OpenVikingSessionRecorder:
)
try:
await aapply_commit_policy(client, session_id, self.commit_policy)
+ except asyncio.CancelledError as exc:
+ self._pending_commit_sessions.add(session_id)
+ _attach_cancellation_progress(
+ exc,
+ session_id=session_id,
+ result=result,
+ stage="commit",
+ )
+ raise
except Exception as exc:
self._pending_commit_sessions.add(session_id)
raise OpenVikingPartialWriteError(
diff --git a/tests/integration/langchain_langgraph/test_async_recording.py b/tests/integration/langchain_langgraph/test_async_recording.py
index 865a6f1ce..299d6942c 100644
--- a/tests/integration/langchain_langgraph/test_async_recording.py
+++ b/tests/integration/langchain_langgraph/test_async_recording.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import asyncio
from typing import Any
import pytest
@@ -113,6 +114,436 @@ async def test_real_openviking_context_wrapper_ainvoke_uses_async_lifecycle():
assert any(part["type"] == "context" for part in assistant_parts)
+async def test_real_openviking_context_wrapper_preserves_concurrent_same_session_turns():
+ client = AsyncTrackingClient()
+ first_started = asyncio.Event()
+ release_first = asyncio.Event()
+ second_started = asyncio.Event()
+
+ async def answer(messages: list[BaseMessage]) -> AIMessage:
+ latest_user = next(
+ message.content for message in reversed(messages) if isinstance(message, HumanMessage)
+ )
+ if latest_user == "from-a":
+ first_started.set()
+ await release_first.wait()
+ else:
+ second_started.set()
+ return AIMessage(content=f"answer:{latest_user}")
+
+ app = with_openviking_context(
+ RunnableLambda(answer),
+ async_client=client,
+ session_id="real-async-concurrent-history",
+ inject_context=False,
+ )
+
+ first = asyncio.create_task(app.ainvoke([HumanMessage(content="from-a")]))
+ await asyncio.wait_for(first_started.wait(), timeout=1)
+ second = asyncio.create_task(app.ainvoke([HumanMessage(content="from-b")]))
+
+ try:
+ await asyncio.wait_for(second_started.wait(), timeout=1)
+ await asyncio.wait_for(second, timeout=1)
+ finally:
+ release_first.set()
+
+ await first
+
+ assert [
+ message["parts"][0]["text"]
+ for message in client.backing.sessions["real-async-concurrent-history"]
+ ] == [
+ "from-b",
+ "answer:from-b",
+ "from-a",
+ "answer:from-a",
+ ]
+
+
+async def test_concurrent_same_session_calls_serialize_only_final_persistence():
+ class BlockingWriteClient(AsyncTrackingClient):
+ def __init__(self):
+ super().__init__()
+ self.write_calls = 0
+ self.first_write_started = asyncio.Event()
+ self.release_first_write = asyncio.Event()
+ self.second_write_started = asyncio.Event()
+
+ async def batch_add_messages(
+ self,
+ session_id: str,
+ messages: list[dict[str, Any]],
+ **kwargs: Any,
+ ) -> dict[str, Any]:
+ self.write_calls += 1
+ self.calls.append("batch_add_messages")
+ self.batch_sizes.append(len(messages))
+ if self.write_calls == 1:
+ self.first_write_started.set()
+ await self.release_first_write.wait()
+ else:
+ self.second_write_started.set()
+ return self.backing.batch_add_messages(session_id, messages, **kwargs)
+
+ client = BlockingWriteClient()
+ both_models_ready = asyncio.Event()
+ model_calls = 0
+
+ async def answer(messages: list[BaseMessage]) -> AIMessage:
+ nonlocal model_calls
+ model_calls += 1
+ if model_calls == 2:
+ both_models_ready.set()
+ await both_models_ready.wait()
+ latest_user = next(
+ message.content for message in reversed(messages) if isinstance(message, HumanMessage)
+ )
+ return AIMessage(content=f"answer:{latest_user}")
+
+ app = with_openviking_context(
+ RunnableLambda(answer),
+ async_client=client,
+ session_id="real-async-write-lock",
+ inject_context=False,
+ )
+ first = asyncio.create_task(app.ainvoke([HumanMessage(content="from-a")]))
+ second = asyncio.create_task(app.ainvoke([HumanMessage(content="from-b")]))
+
+ await asyncio.wait_for(both_models_ready.wait(), timeout=1)
+ await asyncio.wait_for(client.first_write_started.wait(), timeout=1)
+ await asyncio.sleep(0.05)
+
+ try:
+ assert client.second_write_started.is_set() is False
+ finally:
+ client.release_first_write.set()
+ await asyncio.gather(first, second)
+
+ assert client.second_write_started.is_set() is True
+ assert client.write_calls == 2
+
+
+def test_context_wrapper_write_lock_is_scoped_per_event_loop():
+ class SlowWriteClient(AsyncTrackingClient):
+ async def batch_add_messages(
+ self,
+ session_id: str,
+ messages: list[dict[str, Any]],
+ **kwargs: Any,
+ ) -> dict[str, Any]:
+ await asyncio.sleep(0.01)
+ return self.backing.batch_add_messages(session_id, messages, **kwargs)
+
+ client = SlowWriteClient()
+
+ async def answer(messages: list[BaseMessage]) -> AIMessage:
+ latest_user = next(
+ message.content for message in reversed(messages) if isinstance(message, HumanMessage)
+ )
+ return AIMessage(content=f"answer:{latest_user}")
+
+ app = with_openviking_context(
+ RunnableLambda(answer),
+ async_client=client,
+ session_id="real-async-cross-loop-write-lock",
+ inject_context=False,
+ )
+
+ async def run_contended_pair(label: str) -> None:
+ results = await app.abatch(
+ [
+ [HumanMessage(content=f"{label}-a")],
+ [HumanMessage(content=f"{label}-b")],
+ ]
+ )
+ assert [result.content for result in results] == [
+ f"answer:{label}-a",
+ f"answer:{label}-b",
+ ]
+
+ asyncio.run(run_contended_pair("loop-one"))
+ asyncio.run(run_contended_pair("loop-two"))
+
+ assert len(client.backing.sessions["real-async-cross-loop-write-lock"]) == 8
+
+
+async def test_real_openviking_context_wrapper_keeps_different_sessions_concurrent():
+ client = AsyncTrackingClient()
+ first_started = asyncio.Event()
+ release_first = asyncio.Event()
+ second_started = asyncio.Event()
+
+ async def answer(messages: list[BaseMessage]) -> AIMessage:
+ latest_user = next(
+ message.content for message in reversed(messages) if isinstance(message, HumanMessage)
+ )
+ if latest_user == "from-a":
+ first_started.set()
+ await release_first.wait()
+ else:
+ second_started.set()
+ return AIMessage(content=f"answer:{latest_user}")
+
+ app = with_openviking_context(
+ RunnableLambda(answer),
+ async_client=client,
+ inject_context=False,
+ )
+
+ first = asyncio.create_task(
+ app.ainvoke(
+ [HumanMessage(content="from-a")],
+ config={"configurable": {"session_id": "real-async-session-a"}},
+ )
+ )
+ await asyncio.wait_for(first_started.wait(), timeout=1)
+ second = asyncio.create_task(
+ app.ainvoke(
+ [HumanMessage(content="from-b")],
+ config={"configurable": {"session_id": "real-async-session-b"}},
+ )
+ )
+
+ try:
+ await asyncio.wait_for(second_started.wait(), timeout=1)
+ finally:
+ release_first.set()
+
+ await asyncio.gather(first, second)
+
+ assert len(client.backing.sessions["real-async-session-a"]) == 2
+ assert len(client.backing.sessions["real-async-session-b"]) == 2
+
+
+async def test_concurrent_same_session_invocations_keep_peer_attribution_local():
+ client = AsyncTrackingClient()
+ first_started = asyncio.Event()
+ release_first = asyncio.Event()
+
+ async def answer(messages: list[BaseMessage]) -> AIMessage:
+ latest_user = next(
+ message.content for message in reversed(messages) if isinstance(message, HumanMessage)
+ )
+ if latest_user == "from-a":
+ first_started.set()
+ await release_first.wait()
+ return AIMessage(content=f"answer:{latest_user}")
+
+ app = with_openviking_context(
+ RunnableLambda(answer),
+ async_client=client,
+ session_id="real-async-peer-attribution",
+ inject_context=False,
+ )
+ first = asyncio.create_task(
+ app.ainvoke(
+ [HumanMessage(content="from-a")],
+ config={"configurable": {"peer_id": "peer-a"}},
+ )
+ )
+ await asyncio.wait_for(first_started.wait(), timeout=1)
+ second = asyncio.create_task(
+ app.ainvoke(
+ [HumanMessage(content="from-b")],
+ config={"configurable": {"peer_id": "peer-b"}},
+ )
+ )
+
+ try:
+ await asyncio.wait_for(second, timeout=1)
+ finally:
+ release_first.set()
+ await first
+
+ stored = client.backing.sessions["real-async-peer-attribution"]
+ by_text = {message["parts"][0]["text"]: message.get("peer_id") for message in stored}
+ assert by_text == {
+ "from-a": "peer-a",
+ "answer:from-a": "peer-a",
+ "from-b": "peer-b",
+ "answer:from-b": "peer-b",
+ }
+
+
+async def test_concurrent_same_session_invocations_keep_recalled_context_local():
+ alpha_uri = "viking://resources/runbooks/alpha.md"
+ beta_uri = "viking://resources/runbooks/beta.md"
+ client = AsyncTrackingClient(
+ {
+ alpha_uri: "Alpha deployment instructions.",
+ beta_uri: "Beta deployment instructions.",
+ }
+ )
+ first_started = asyncio.Event()
+ release_first = asyncio.Event()
+
+ async def answer(messages: list[BaseMessage]) -> AIMessage:
+ latest_user = next(
+ message.content for message in reversed(messages) if isinstance(message, HumanMessage)
+ )
+ if latest_user == "alpha":
+ first_started.set()
+ await release_first.wait()
+ return AIMessage(content=f"answer:{latest_user}")
+
+ app = with_openviking_context(
+ RunnableLambda(answer),
+ async_client=client,
+ session_id="real-async-context-attribution",
+ target_uri="viking://resources",
+ )
+ first = asyncio.create_task(app.ainvoke([HumanMessage(content="alpha")]))
+ await asyncio.wait_for(first_started.wait(), timeout=1)
+
+ try:
+ await asyncio.wait_for(
+ app.ainvoke([HumanMessage(content="beta")]),
+ timeout=1,
+ )
+ finally:
+ release_first.set()
+ await first
+
+ context_uri_by_answer: dict[str, str] = {}
+ for message in client.backing.sessions["real-async-context-attribution"]:
+ if message["role"] != "assistant":
+ continue
+ answer_text = next(part["text"] for part in message["parts"] if part["type"] == "text")
+ context_uri = next(part["uri"] for part in message["parts"] if part["type"] == "context")
+ context_uri_by_answer[answer_text] = context_uri
+
+ assert context_uri_by_answer == {
+ "answer:alpha": alpha_uri,
+ "answer:beta": beta_uri,
+ }
+
+
+async def test_concurrent_session_failure_keeps_other_session_context_attribution():
+ client = AsyncTrackingClient(
+ {"viking://resources/runbooks/concurrent.md": "Concurrent context is violet."}
+ )
+ second_started = asyncio.Event()
+ release_second = asyncio.Event()
+
+ async def answer(messages: list[BaseMessage]) -> AIMessage:
+ latest_user = next(
+ message.content for message in reversed(messages) if isinstance(message, HumanMessage)
+ )
+ if latest_user == "fail-a":
+ raise RuntimeError("session a failed")
+ second_started.set()
+ await release_second.wait()
+ return AIMessage(content="answer:from-b")
+
+ app = with_openviking_context(
+ RunnableLambda(answer),
+ async_client=client,
+ target_uri="viking://resources",
+ )
+ second = asyncio.create_task(
+ app.ainvoke(
+ [HumanMessage(content="violet")],
+ config={"configurable": {"session_id": "real-async-context-b"}},
+ )
+ )
+ await asyncio.wait_for(second_started.wait(), timeout=1)
+
+ try:
+ with pytest.raises(RuntimeError, match="session a failed"):
+ await app.ainvoke(
+ [HumanMessage(content="fail-a")],
+ config={"configurable": {"session_id": "real-async-context-a"}},
+ )
+ finally:
+ release_second.set()
+ await second
+
+ assistant_parts = client.backing.sessions["real-async-context-b"][-1]["parts"]
+ assert any(part["type"] == "context" for part in assistant_parts)
+
+
+async def test_real_openviking_context_wrapper_preserves_same_session_abatch_turns():
+ client = AsyncTrackingClient()
+
+ async def answer(messages: list[BaseMessage]) -> AIMessage:
+ latest_user = next(
+ message.content for message in reversed(messages) if isinstance(message, HumanMessage)
+ )
+ await asyncio.sleep(0)
+ return AIMessage(content=f"answer:{latest_user}")
+
+ app = with_openviking_context(
+ RunnableLambda(answer),
+ async_client=client,
+ session_id="real-async-batch-history",
+ inject_context=False,
+ )
+
+ results = await app.abatch(
+ [
+ [HumanMessage(content="from-a")],
+ [HumanMessage(content="from-b")],
+ ]
+ )
+
+ assert [result.content for result in results] == ["answer:from-a", "answer:from-b"]
+ stored = [
+ message["parts"][0]["text"]
+ for message in client.backing.sessions["real-async-batch-history"]
+ ]
+ assert len(stored) == 4
+ assert {tuple(stored[index : index + 2]) for index in range(0, len(stored), 2)} == {
+ ("from-a", "answer:from-a"),
+ ("from-b", "answer:from-b"),
+ }
+
+
+async def test_abandoned_stream_does_not_block_same_session_invocation():
+ client = AsyncTrackingClient()
+ keep_stream_open = asyncio.Event()
+
+ async def answer(messages: list[BaseMessage]):
+ latest_user = next(
+ message.content for message in reversed(messages) if isinstance(message, HumanMessage)
+ )
+ yield AIMessage(content=f"answer:{latest_user}")
+ if latest_user == "stream":
+ await keep_stream_open.wait()
+
+ app = with_openviking_context(
+ RunnableLambda(answer),
+ async_client=client,
+ session_id="real-async-abandoned-stream",
+ inject_context=False,
+ )
+ stream = app.astream([HumanMessage(content="stream")])
+ first_chunk = await anext(stream)
+
+ try:
+ result = await asyncio.wait_for(
+ app.ainvoke([HumanMessage(content="next")]),
+ timeout=1,
+ )
+ finally:
+ await stream.aclose()
+
+ assert first_chunk.content == "answer:stream"
+ assert result.content == "answer:next"
+ assert [
+ message["parts"][0]["text"]
+ for message in client.backing.sessions["real-async-abandoned-stream"]
+ ] == ["next", "answer:next"]
+
+
@pytest.mark.asyncio
async def test_real_async_context_wrapper_clears_pending_context_after_failure():
client = AsyncTrackingClient(
diff --git a/tests/unit/test_langchain_async_integration.py b/tests/unit/test_langchain_async_integration.py
index 03df23d23..615e6d5d5 100644
--- a/tests/unit/test_langchain_async_integration.py
+++ b/tests/unit/test_langchain_async_integration.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import asyncio
import copy
+import sys
import threading
from typing import Any
@@ -14,6 +15,7 @@ from langchain_core.messages import AIMessage, HumanMessage
from openviking.integrations.langchain import (
InMemoryOpenVikingClient,
+ OpenVikingCancellationProgress,
OpenVikingChatMessageHistory,
OpenVikingCommitPolicy,
OpenVikingContextMiddleware,
@@ -21,6 +23,7 @@ from openviking.integrations.langchain import (
OpenVikingRetriever,
OpenVikingSessionContextAssembler,
OpenVikingSessionRecorder,
+ get_openviking_cancellation_progress,
)
from openviking.integrations.langchain.client import (
OpenVikingAsyncClientHandle,
@@ -945,6 +948,164 @@ async def test_async_recorder_reports_confirmed_prefix_for_partial_batch_retry()
assert len(backing.sessions["async-partial-retry"]) == 101
+async def test_async_middleware_preserves_partial_progress_when_recording_is_cancelled():
+ class CancelSecondBatchClient(AsyncInMemoryOpenVikingClient):
+ def __init__(self):
+ super().__init__()
+ self.batch_calls = 0
+ self.cancellation = asyncio.CancelledError("cancel second batch")
+
+ async def batch_add_messages(
+ self,
+ session_id: str,
+ messages: list[dict[str, Any]],
+ **kwargs: Any,
+ ) -> dict[str, Any]:
+ self.batch_calls += 1
+ if self.batch_calls == 2:
+ raise self.cancellation
+ return self.backing.batch_add_messages(session_id, messages, **kwargs)
+
+ client = CancelSecondBatchClient()
+ middleware = OpenVikingContextMiddleware(
+ async_client=client,
+ session_id_resolver=lambda _state, _runtime: "async-cancelled-batch",
+ )
+ messages = [HumanMessage(content=f"Message {index}") for index in range(150)]
+ state = {"messages": messages}
+
+ recording = asyncio.create_task(middleware.aafter_agent(state, runtime=None))
+ with pytest.raises(asyncio.CancelledError) as captured:
+ await recording
+ await middleware.aafter_agent(state, runtime=None)
+
+ progress = get_openviking_cancellation_progress(captured.value)
+ assert recording.cancelled() is True
+ if sys.version_info >= (3, 11):
+ # Python 3.10 rebuilds CancelledError at task boundaries via
+ # Future._make_cancelled_error(), preserving the original in __context__.
+ assert captured.value is client.cancellation
+ assert not isinstance(captured.value, Exception)
+ assert isinstance(progress, OpenVikingCancellationProgress)
+ assert progress.messages_written == 100
+ assert progress.input_messages_consumed == 100
+ assert client.batch_calls == 3
+ assert len(client.backing.sessions["async-cancelled-batch"]) == 150
+
+
+async def test_async_middleware_retries_cancelled_commit_without_duplicate_writes():
+ class CancelFirstCommitClient(AsyncInMemoryOpenVikingClient):
+ def __init__(self):
+ super().__init__()
+ self.batch_calls = 0
+ self.commit_calls = 0
+ self.cancellation = asyncio.CancelledError("cancel first commit")
+
+ async def batch_add_messages(
+ self,
+ session_id: str,
+ messages: list[dict[str, Any]],
+ **kwargs: Any,
+ ) -> dict[str, Any]:
+ self.batch_calls += 1
+ return self.backing.batch_add_messages(session_id, messages, **kwargs)
+
+ async def commit_session(
+ self,
+ session_id: str,
+ **kwargs: Any,
+ ) -> dict[str, Any]:
+ self.commit_calls += 1
+ if self.commit_calls == 1:
+ raise self.cancellation
+ return self.backing.commit_session(session_id, **kwargs)
+
+ client = CancelFirstCommitClient()
+ middleware = OpenVikingContextMiddleware(
+ async_client=client,
+ session_id_resolver=lambda _state, _runtime: "async-cancelled-commit",
+ commit_policy=OpenVikingCommitPolicy(mode="always"),
+ )
+ state = {
+ "messages": [
+ HumanMessage(content="Persist once."),
+ AIMessage(content="Commit once."),
+ ]
+ }
+
+ with pytest.raises(asyncio.CancelledError) as captured:
+ await middleware.aafter_agent(state, runtime=None)
+ await middleware.aafter_agent(state, runtime=None)
+
+ progress = get_openviking_cancellation_progress(captured.value)
+ assert captured.value is client.cancellation
+ assert isinstance(progress, OpenVikingCancellationProgress)
+ assert progress.commit_pending is True
+ assert progress.input_messages_consumed == 2
+ assert client.batch_calls == 1
+ assert client.commit_calls == 2
+ assert len(client.backing.archives["async-cancelled-commit"][0]["messages"]) == 2
+
+
+ "timeout_api",
+ [
+ "wait_for",
+ pytest.param(
+ "timeout",
+ marks=pytest.mark.skipif(
+ not hasattr(asyncio, "timeout"),
+ reason="asyncio.timeout requires Python 3.11+",
+ ),
+ ),
+ ],
+)
+async def test_async_recording_cancellation_preserves_timeout_contract_and_retry(
+ timeout_api: str,
+):
+ class BlockSecondBatchClient(AsyncInMemoryOpenVikingClient):
+ def __init__(self):
+ super().__init__()
+ self.batch_calls = 0
+
+ async def batch_add_messages(
+ self,
+ session_id: str,
+ messages: list[dict[str, Any]],
+ **kwargs: Any,
+ ) -> dict[str, Any]:
+ self.batch_calls += 1
+ if self.batch_calls == 2:
+ await asyncio.Event().wait()
+ return self.backing.batch_add_messages(session_id, messages, **kwargs)
+
+ client = BlockSecondBatchClient()
+ middleware = OpenVikingContextMiddleware(
+ async_client=client,
+ session_id_resolver=lambda _state, _runtime: f"async-{timeout_api}",
+ )
+ state = {"messages": [HumanMessage(content=f"Message {index}") for index in range(150)]}
+
+ with pytest.raises(asyncio.TimeoutError) as captured:
+ if timeout_api == "wait_for":
+ await asyncio.wait_for(middleware.aafter_agent(state, runtime=None), timeout=0.05)
+ else:
+ async with asyncio.timeout(0.05):
+ await middleware.aafter_agent(state, runtime=None)
+
+ progress = get_openviking_cancellation_progress(captured.value)
+ assert isinstance(progress, OpenVikingCancellationProgress)
+ assert progress.input_messages_consumed == 100
+
+ await middleware.aafter_agent(state, runtime=None)
+
+ assert client.batch_calls == 3
+ assert len(client.backing.sessions[f"async-{timeout_api}"]) == 150
+
+
@pytest.mark.asyncio
async def test_async_recorder_closes_only_internally_created_client(monkeypatch):
instances: list[Any] = []