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(langchain): preserve conversation text, roles and tool history (#6275)
z1gonb1d0fa51ef25
diff --git a/integrations/frameworks/langchain/src/langchain_openviking/client.py b/integrations/frameworks/langchain/src/langchain_openviking/client.pyindex 581dbc966..212dae6b2 100644--- a/integrations/frameworks/langchain/src/langchain_openviking/client.py+++ b/integrations/frameworks/langchain/src/langchain_openviking/client.py@@ -814,7 +814,9 @@ def extract_message_text(content: Any) -> str:if isinstance(block, str):chunks.append(block)elif isinstance(block, dict):- if block.get("type") == "text" and isinstance(block.get("text"), str):+ if block.get("type") in ("text", "input_text", "output_text") and isinstance(+ block.get("text"), str+ ):chunks.append(block["text"])elif isinstance(block.get("content"), str):chunks.append(block["content"])@@ -824,15 +826,32 @@ def extract_message_text(content: Any) -> str:return str(content)+def get_message_role(message: Any) -> str:+ """Normalize conversation role aliases, preserving typed-message precedence."""++ if isinstance(message, dict):+ role = message.get("type") or message.get("role")+ else:+ role = getattr(message, "type", None) or getattr(message, "role", None)+ if role in ("chat", "ChatMessageChunk"):+ role = message.get("role") if isinstance(message, dict) else getattr(message, "role", None)+ if role in ("human", "user", "HumanMessageChunk"):+ return "user"+ if role in ("system", "developer", "SystemMessageChunk"):+ return "system"+ if role in ("ai", "assistant", "AIMessageChunk"):+ return "assistant"+ return str(role or "")++def get_latest_user_text(messages: Iterable[Any]) -> str:for message in reversed(list(messages)):- role = getattr(message, "type", None) or getattr(message, "role", None)- if isinstance(message, dict):- role = message.get("type") or message.get("role")- content = message.get("content")- else:- content = getattr(message, "content", "")- if role in {"human", "user"}:+ if get_message_role(message) == "user":+ content = (+ message.get("content")+ if isinstance(message, dict)+ else getattr(message, "content", "")+ )text = extract_message_text(content).strip()if text:return textdiff --git a/integrations/frameworks/langchain/src/langchain_openviking/messages.py b/integrations/frameworks/langchain/src/langchain_openviking/messages.pyindex f1923968a..2fdf7ab46 100644--- a/integrations/frameworks/langchain/src/langchain_openviking/messages.py+++ b/integrations/frameworks/langchain/src/langchain_openviking/messages.py@@ -14,6 +14,7 @@ try:BaseMessage,HumanMessage,SystemMessage,+ ToolCall,ToolMessage,)except ImportError as exc: # pragma: no cover - exercised by optional import path@@ -21,7 +22,7 @@ except ImportError as exc: # pragma: no cover - exercised by optional import paraise missing_dependency("langchain", "langchain-core") from exc-from langchain_openviking.client import extract_message_text+from langchain_openviking.client import extract_message_text, get_message_roleOPENVIKING_CONTEXT_MARKER = "<openviking_context>"LANGCHAIN_SUMMARIZATION_SOURCE = "summarization"@@ -96,13 +97,14 @@ def langchain_message_to_openviking(}]- if isinstance(message, SystemMessage):+ role = get_message_role(message)+ if role == "system":return []text = extract_message_text(getattr(message, "content", ""))if not text:return []- role = "user" if getattr(message, "type", "") == "human" else "assistant"+ role = "user" if role == "user" else "assistant"return [{"role": role, "parts": [{"type": "text", "text": text}]}]@@ -116,6 +118,7 @@ def openviking_message_to_langchain(message: dict[str, Any]) -> list[BaseMessagereturn [HumanMessage(content=text)]tool_calls = []+ tool_call_ids: set[str] = set()tool_messages = []for part in parts:if part.get("type") != "tool":@@ -123,8 +126,19 @@ def openviking_message_to_langchain(message: dict[str, Any]) -> list[BaseMessagetool_id = str(part.get("tool_id") or "")tool_name = str(part.get("tool_name") or "")status = str(part.get("tool_status") or "")- has_output = part.get("tool_output") is not None- is_completed_result = has_output or status in {"completed", "error"}+ is_completed_result = _has_tool_result(part)+ if not is_completed_result or (+ tool_id and tool_name and part.get("tool_input") is not None+ ):+ if not tool_id or tool_id not in tool_call_ids:+ tool_calls.append(+ {+ "id": tool_id,+ "name": tool_name,+ "args": _tool_args(part.get("tool_input")),+ }+ )+ tool_call_ids.add(tool_id)if is_completed_result:tool_messages.append(ToolMessage(@@ -134,14 +148,6 @@ def openviking_message_to_langchain(message: dict[str, Any]) -> list[BaseMessagestatus="error" if status == "error" else "success",))- else:- tool_calls.append(- {- "id": tool_id,- "name": tool_name,- "args": _tool_args(part.get("tool_input")),- }- )messages: list[BaseMessage] = []if text or tool_calls or not tool_messages:@@ -155,28 +161,93 @@ def openviking_message_to_langchain(message: dict[str, Any]) -> list[BaseMessagedef restore_openviking_messages(messages: Sequence[dict[str, Any]]) -> list[BaseMessage]:- """Restore a valid LangChain message sequence from OpenViking session messages."""+ """Restore LangChain history, matching tool results to recorded calls."""restored: list[BaseMessage] = []- active_tool_call_ids: set[str] = set()+ active_tool_calls: dict[str, ToolCall] = {}+ completed_tool_calls: dict[str, tuple[ToolCall, ToolMessage]] = {}for message in messages:- for langchain_message in openviking_message_to_langchain(message):+ converted = openviking_message_to_langchain(message)+ pending_call_ids = {+ str(part.get("tool_id") or "")+ for part in message.get("parts") or []+ if part.get("type") == "tool" and not _has_tool_result(part)+ }+ converted_calls = {+ str(call.get("id") or ""): call+ for item in converted+ if isinstance(item, AIMessage)+ for call in item.tool_calls+ }+ converted_results: dict[str, ToolMessage] = {}+ for item in converted:+ if isinstance(item, ToolMessage):+ converted_results.setdefault(item.tool_call_id, item)+ completed_here: set[str] = set()+ # Complete earlier calls before any new assistant text or calls.+ for result in converted_results.values():+ pending = active_tool_calls.get(result.tool_call_id)+ # Different arguments or names describe a new call that reused the ID.+ if pending is not None and converted_calls.get(result.tool_call_id, pending) == pending:+ restored.append(result)+ completed_tool_calls[result.tool_call_id] = (+ active_tool_calls.pop(result.tool_call_id),+ result,+ )+ completed_here.add(result.tool_call_id)+ for langchain_message in converted:if isinstance(langchain_message, AIMessage):+ kept: list[ToolCall] = []+ for call in langchain_message.tool_calls:+ tool_call_id = str(call.get("id") or "")+ if tool_call_id in completed_here:+ continue+ previous = completed_tool_calls.get(tool_call_id)+ call_result = converted_results.get(tool_call_id)+ # Only a completed pair can be a repeated stored record.+ if (+ tool_call_id not in pending_call_ids+ and call_result is not None+ and previous is not None+ and previous[0] == call+ and previous[1].content == call_result.content+ and previous[1].status == call_result.status+ ):+ continue+ kept.append(call)+ if len(kept) != len(langchain_message.tool_calls):+ if not kept and not langchain_message.content:+ continue+ langchain_message = langchain_message.model_copy(update={"tool_calls": kept})restored.append(langchain_message)for tool_call in langchain_message.tool_calls or []:tool_call_id = str(tool_call.get("id") or "")if tool_call_id:- active_tool_call_ids.add(tool_call_id)+ active_tool_calls[tool_call_id] = tool_call+ completed_tool_calls.pop(tool_call_id, None)elif isinstance(langchain_message, ToolMessage):tool_call_id = str(langchain_message.tool_call_id or "")- if tool_call_id and tool_call_id in active_tool_call_ids:+ if tool_call_id and tool_call_id in active_tool_calls:restored.append(langchain_message)- active_tool_call_ids.discard(tool_call_id)+ completed_tool_calls[tool_call_id] = (+ active_tool_calls.pop(tool_call_id),+ langchain_message,+ )else:+ if isinstance(langchain_message, HumanMessage):+ # Reset duplicate detection; pending calls may still finish later.+ completed_tool_calls.clear()restored.append(langchain_message)return restored+def _has_tool_result(part: dict[str, Any]) -> bool:+ return part.get("tool_output") is not None or str(part.get("tool_status") or "") in {+ "completed",+ "error",+ }++def _text_parts(content: Any) -> list[dict[str, Any]]:text = extract_message_text(content)return [{"type": "text", "text": text}] if text else []diff --git a/integrations/frameworks/langchain/tests/unit/test_langchain_integration.py b/integrations/frameworks/langchain/tests/unit/test_langchain_integration.pyindex 8370cb5ca..48fc0cbe6 100644--- a/integrations/frameworks/langchain/tests/unit/test_langchain_integration.py+++ b/integrations/frameworks/langchain/tests/unit/test_langchain_integration.py@@ -1,6 +1,7 @@from __future__ import annotationsimport json+from copy import deepcopyfrom types import MappingProxyTypefrom typing import Any@@ -12,8 +13,12 @@ pytest.importorskip("langchain_openviking")from langchain_core.messages import (AIMessage,+ ChatMessage,+ ChatMessageChunk,HumanMessage,+ HumanMessageChunk,SystemMessage,+ SystemMessageChunk,ToolMessage,convert_to_openai_messages,)@@ -29,6 +34,7 @@ from langchain_openviking import (OpenVikingContextMiddleware,OpenVikingRetriever,OpenVikingSessionContextAssembler,+ OpenVikingSessionRecorder,OpenVikingStore,create_openviking_tools,with_openviking_context,@@ -38,6 +44,7 @@ from langchain_openviking.client import (apply_commit_policy,call_openviking,ensure_client,+ get_latest_user_text,)from langchain_openviking.history import (langchain_message_to_openviking,@@ -789,41 +796,81 @@ def test_in_memory_openviking_client_batch_add_messages_records_messages():assert client.sessions["batch-session"][1]["created_at"] == "2026-05-26T00:00:00+00:00"-def test_chat_message_history_preserves_tool_parts():[email protected]("asynchronous", [False, True])+ "second_query,new_turn,second_output",+ [+ ("deployment", True, "deployment notes found."),+ ("Tokyo", True, "Tokyo notes found."),+ ("Tokyo", False, "Tokyo notes found."),+ ("deployment", False, "done"),+ ("deployment", False, "deployment notes found."),+ ("deployment", False, None),+ ],+)+async def test_chat_message_history_preserves_tool_parts(+ asynchronous, second_query, new_turn, second_output+):client = InMemoryOpenVikingClient()- history = OpenVikingChatMessageHistory(session_id="history-session", client=client)-- history.add_messages(- [- HumanMessage(content="Find deployment notes."),- AIMessage(- content="Searching.",- tool_calls=[- {- "id": "call-1",- "name": "viking_find",- "args": {"query": "deployment"},- }- ],- ),- ToolMessage(- content="Azure deployment notes found.",- tool_call_id="call-1",- name="viking_find",- ),- ]+ history = OpenVikingChatMessageHistory(+ session_id="history-session", client=client, async_client=client)+ messages = []+ outputs = ["deployment notes found.", second_output]+ for index, (query, output) in enumerate(+ zip(("deployment", second_query), outputs, strict=True)+ ):+ if index == 0 or new_turn:+ messages.append(HumanMessage(content=f"Find {query} notes."))+ messages.extend(+ [+ AIMessage(+ content="Searching.",+ tool_calls=[+ {+ "id": "call-1",+ "name": "viking_find",+ "args": {"query": query},+ }+ ],+ ),+ ]+ )+ if output is not None:+ messages.append(ToolMessage(content=output, tool_call_id="call-1", name="viking_find"))+ try:+ if asynchronous:+ await history.aadd_messages(messages)+ restored = await history.aget_messages()+ else:+ history.add_messages(messages)+ restored = history.messages+ finally:+ await history.aclose()+stored = client.sessions["history-session"]assert stored[1]["parts"][1]["type"] == "tool"- assert stored[2]["parts"][0]["tool_output"] == "Azure deployment notes found."+ assert stored[2]["parts"][0]["tool_output"] == "deployment notes found."- restored = history.messages+ assert [type(message) for message in restored] == (+ [HumanMessage, AIMessage, ToolMessage]+ + ([HumanMessage] if new_turn else [])+ + [AIMessage]+ + ([ToolMessage] if second_output is not None else [])+ )restored_ai_messages = [message for message in restored if isinstance(message, AIMessage)]restored_tool_messages = [message for message in restored if isinstance(message, ToolMessage)]- assert len(restored_ai_messages) == 1- assert len(restored_ai_messages[0].tool_calls) == 1- assert len(restored_tool_messages) == 1+ assert [message.tool_calls for message in restored_ai_messages] == [+ [{"id": "call-1", "name": "viking_find", "args": {"query": query}, "type": "tool_call"}]+ for query in ("deployment", second_query)+ ]+ expected_outputs = [output for output in outputs if output is not None]+ assert [message.tool_call_id for message in restored_tool_messages] == ["call-1"] * len(+ expected_outputs+ )+ assert [message.content for message in restored_tool_messages] == expected_outputsdef test_chat_message_history_writes_peer_id_to_messages():@@ -847,25 +894,238 @@ def test_chat_message_history_writes_peer_id_to_messages():]-def test_openviking_tool_result_restores_without_duplicate_ai_tool_call():- restored = openviking_message_to_langchain(- {- "role": "assistant",- "parts": [- {- "type": "tool",- "tool_id": "call-1",- "tool_name": "viking_find",- "tool_output": "Azure deployment notes found.",- "tool_status": "completed",- }- ],- }- )+ "fields",+ [{}, {"tool_input": None}, {"tool_input": {"a": 1}, "tool_name": ""}],+)+def test_openviking_tool_result_restores_without_duplicate_ai_tool_call(fields):+ message = {+ "role": "assistant",+ "parts": [+ {+ "type": "tool",+ "tool_id": "call-1",+ "tool_name": "viking_find",+ "tool_output": "Azure deployment notes found.",+ "tool_status": "completed",+ **fields,+ }+ ],+ }+ restored = openviking_message_to_langchain(message)assert len(restored) == 1assert isinstance(restored[0], ToolMessage)assert restored[0].tool_call_id == "call-1"+ client = InMemoryOpenVikingClient()+ client.add_message("orphan-result", "assistant", parts=message["parts"])+ history = OpenVikingChatMessageHistory("orphan-result", client=client)+ try:+ assert history.messages == []+ finally:+ history.close()++[email protected]("asynchronous", [False, True])+ "followup",+ [+ "none",+ "duplicate",+ "next_turn",+ "approval",+ "updated_output",+ "updated_status",+ "coalesced_poll",+ ],+)[email protected]("tool_input", [{"release": "stable"}, {}])[email protected]("status,output", [("completed", "Friday"), ("error", "")])+async def test_history_restores_completed_tool_part_with_input(+ asynchronous, followup, tool_input, status, output+):+ client = InMemoryOpenVikingClient()+ completed = {+ "type": "tool",+ "tool_id": "release-2",+ "tool_name": "read_notes",+ "tool_input": tool_input,+ "tool_output": output,+ "tool_status": status,+ }+ initial = deepcopy(completed)+ if followup == "approval":+ initial.pop("tool_output")+ initial["tool_status"] = "pending"+ following = deepcopy(completed)+ if followup == "updated_output":+ following["tool_output"] = f"{output} updated"+ elif followup == "updated_status":+ following["tool_status"] = "error" if status == "completed" else "completed"+ initial_parts = [initial]+ following_parts = [following]+ if followup == "coalesced_poll":+ pending = {**completed, "tool_status": "pending"}+ pending.pop("tool_output")+ initial_parts.insert(0, deepcopy(pending))+ following_parts.insert(0, deepcopy(pending))+ client.add_message(+ "combined-tool",+ "assistant",+ parts=initial_parts,+ )+ if followup != "none":+ if followup in ("next_turn", "approval"):+ client.add_message(+ "combined-tool",+ "user",+ content="Approved." if followup == "approval" else "Run the same tool again.",+ )+ for _ in range(2 if followup == "approval" else 1):+ client.add_message("combined-tool", "assistant", parts=deepcopy(following_parts))+ history = OpenVikingChatMessageHistory(+ session_id="combined-tool", client=client, async_client=client+ )+ stored = deepcopy(client.sessions["combined-tool"])+ try:+ messages = await history.aget_messages() if asynchronous else history.messages+ finally:+ await history.aclose()++ expected_types = {+ "next_turn": [AIMessage, ToolMessage, HumanMessage, AIMessage, ToolMessage],+ "approval": [AIMessage, HumanMessage, ToolMessage],+ "updated_output": [AIMessage, ToolMessage, AIMessage, ToolMessage],+ "updated_status": [AIMessage, ToolMessage, AIMessage, ToolMessage],+ "coalesced_poll": [AIMessage, ToolMessage, AIMessage, ToolMessage],+ }.get(followup, [AIMessage, ToolMessage])+ assert [type(message) for message in messages] == expected_types+ ai = [message for message in messages if isinstance(message, AIMessage)][-1]+ tool = messages[-1]+ assert isinstance(ai, AIMessage)+ assert ai.tool_calls[0]["id"] == "release-2"+ assert ai.tool_calls[0]["name"] == "read_notes"+ assert ai.tool_calls[0]["args"] == tool_input+ assert isinstance(tool, ToolMessage)+ assert tool.tool_call_id == "release-2"+ assert tool.content == following["tool_output"]+ assert tool.status == ("error" if following["tool_status"] == "error" else "success")+ assert client.sessions["combined-tool"] == stored++[email protected]("asynchronous", [False, True])[email protected]("tool_input", [{"a": 1}, {}])+ "layout", ["split", "coalesced", "mixed", "result_only", "approved_result_only"]+)+async def test_history_does_not_duplicate_pending_call_for_completed_tool_part(+ asynchronous, tool_input, layout+):+ client = InMemoryOpenVikingClient()+ approved = layout == "approved_result_only"+ if approved:+ layout = "result_only"+ pending = {+ "type": "tool",+ "tool_id": "c1",+ "tool_name": "read_notes",+ "tool_input": tool_input,+ "tool_status": "pending",+ }+ completed = {**pending, "tool_status": "completed", "tool_output": "ok"}+ if layout == "result_only":+ completed.pop("tool_input")+ if layout != "coalesced":+ client.add_message("split-tool", "assistant", parts=[pending])+ if approved:+ client.add_message("split-tool", "user", content="Approved.")+ parts = [pending, completed] if layout == "coalesced" else [completed]+ if layout in ("mixed", "result_only"):+ parts.append({"type": "text", "text": "Lookup complete."})+ if layout == "mixed":+ parts.append({**completed, "tool_id": "c2"})+ client.add_message("split-tool", "assistant", parts=parts)+ history = OpenVikingChatMessageHistory(+ session_id="split-tool", client=client, async_client=client+ )+ stored = deepcopy(client.sessions["split-tool"])+ try:+ messages = await history.aget_messages() if asynchronous else history.messages+ finally:+ await history.aclose()+ expected_types = [AIMessage, ToolMessage] * (2 if layout == "mixed" else 1)+ if layout == "result_only":+ expected_types.append(AIMessage)+ if approved:+ expected_types.insert(1, HumanMessage)+ assert [type(message) for message in messages] == expected_types+ assert [+ call["id"]+ for message in messages+ if isinstance(message, AIMessage)+ for call in message.tool_calls+ ] == (["c1", "c2"] if layout == "mixed" else ["c1"])+ assert [message.tool_call_id for message in messages if isinstance(message, ToolMessage)] == (+ ["c1", "c2"] if layout == "mixed" else ["c1"]+ )+ if layout in ("mixed", "result_only"):+ assert messages[3 if approved else 2].content == "Lookup complete."+ assert client.sessions["split-tool"] == stored++[email protected]("asynchronous", [False, True])[email protected]("earlier_result", ["pending", "combined", "result_only"])+async def test_history_keeps_new_combined_call_when_an_older_call_reuses_its_id(+ asynchronous, earlier_result+):+ client = InMemoryOpenVikingClient()+ pending = {+ "type": "tool",+ "tool_id": "call_0",+ "tool_name": "weather",+ "tool_input": {"city": "Paris"},+ "tool_status": "pending",+ }+ client.add_message("overlapping-id", "assistant", parts=[pending])+ client.add_message("overlapping-id", "user", content="Check Tokyo instead.")+ if earlier_result != "pending":+ result = {**pending, "tool_status": "completed", "tool_output": "18C"}+ if earlier_result == "result_only":+ result.pop("tool_input")+ for _ in range(2):+ client.add_message("overlapping-id", "assistant", parts=deepcopy([result]))+ client.add_message(+ "overlapping-id",+ "assistant",+ parts=[+ {+ **pending,+ "tool_input": {"city": "Tokyo"},+ "tool_status": "completed",+ "tool_output": "22C",+ }+ ],+ )+ stored = deepcopy(client.sessions["overlapping-id"])+ history = OpenVikingChatMessageHistory("overlapping-id", client=client, async_client=client)+ try:+ messages = await history.aget_messages() if asynchronous else history.messages+ finally:+ await history.aclose()+ assert [type(message) for message in messages] == (+ [AIMessage, HumanMessage]+ + ([] if earlier_result == "pending" else [ToolMessage])+ + [AIMessage, ToolMessage]+ )+ assert messages[0].tool_calls[0]["args"] == {"city": "Paris"}+ assert messages[-2].tool_calls[0]["args"] == {"city": "Tokyo"}+ assert [message.content for message in messages if isinstance(message, ToolMessage)] == (+ ["22C"] if earlier_result == "pending" else ["18C", "22C"]+ )+ assert client.sessions["overlapping-id"] == storeddef test_empty_human_message_uses_empty_text_part():@@ -883,6 +1143,68 @@ def test_system_messages_are_never_persisted_to_openviking_history():assert payloads == [][email protected]("entry", ["history", "ahistory", "recorder", "arecorder"])[email protected]("message_type", [ChatMessage, ChatMessageChunk])+ "role,expected",+ [+ ("user", "user"),+ ("human", "user"),+ ("system", None),+ ("developer", None),+ ("assistant", "assistant"),+ ],+)+async def test_generic_chat_message_roles_in_session_history(entry, message_type, role, expected):+ client = InMemoryOpenVikingClient()+ message = message_type(role=role, content="Role-specific conversation text.")+ history = OpenVikingChatMessageHistory("chat-roles", client=client, async_client=client)+ recorder = OpenVikingSessionRecorder(client=client, async_client=client)+ try:+ if entry == "history":+ history.add_messages([message])+ elif entry == "ahistory":+ await history.aadd_messages([message])+ elif entry == "recorder":+ recorder.record("chat-roles", [message])+ else:+ await recorder.arecord("chat-roles", [message])+ expected_roles = [] if expected is None else [expected]+ assert [item["role"] for item in client.sessions["chat-roles"]] == expected_roles+ restored = await history.aget_messages()+ assert [item.type for item in restored] == [+ "human" if value == "user" else "ai" for value in expected_roles+ ]+ assert [item.content for item in restored] == [message.content] * len(expected_roles)+ finally:+ await history.aclose()+ await recorder.aclose()+++ "message,expected",+ [+ (ChatMessage(role="user", content="Current question."), "Current question."),+ (ChatMessage(role="human", content="Current question."), "Current question."),+ (ChatMessageChunk(role="human", content="Current question."), "Current question."),+ ({"type": "chat", "role": "human", "content": "Current question."}, "Current question."),+ ({"type": "chat", "role": "user", "content": "Current question."}, "Current question."),+ (ChatMessage(role="system", content="Runtime policy."), "Earlier question."),+ (ChatMessage(role="developer", content="Runtime policy."), "Earlier question."),+ (ChatMessageChunk(role="developer", content="Runtime policy."), "Earlier question."),+ ({"type": "chat", "role": "developer", "content": "Runtime policy."}, "Earlier question."),+ (ChatMessage(role="critic", content="Critique."), "Earlier question."),+ (HumanMessage(content="Current question.", role="system"), "Current question."),+ (HumanMessageChunk(content="Current question.", role="system"), "Current question."),+ (SystemMessageChunk(content="Runtime policy.", role="user"), "Earlier question."),+ (ChatMessage(role="user", content=""), "Earlier question."),+ ],+)+def test_generic_chat_message_roles_in_recall_query(message, expected):+ assert get_latest_user_text([HumanMessage(content="Earlier question."), message]) == expected++def test_session_context_assembler_uses_archive_active_messages_and_recall():client = InMemoryOpenVikingClient({"viking://resources/runbooks/deploy.md": "Azure deployments use OpenViking context."}diff --git a/integrations/frameworks/langchain/tests/unit/test_langchain_recording.py b/integrations/frameworks/langchain/tests/unit/test_langchain_recording.pyindex 5177e8d74..e999c9eeb 100644--- a/integrations/frameworks/langchain/tests/unit/test_langchain_recording.py+++ b/integrations/frameworks/langchain/tests/unit/test_langchain_recording.py@@ -1,5 +1,6 @@from __future__ import annotations+import asynciofrom typing import Anyimport pytest@@ -25,6 +26,7 @@ from langchain_openviking import (OpenVikingRecordResult,OpenVikingSessionRecorder,)+from langchain_openviking.client import get_latest_user_textfrom langchain_openviking.messages import OPENVIKING_CONTEXT_MARKER@@ -804,3 +806,48 @@ def test_middleware_retries_failed_commit_without_duplicate_writes():]assert client.sessions["middleware-commit-retry"] == []assert len(client.archives["middleware-commit-retry"][0]["messages"]) == 2++[email protected]("asynchronous", [False, True])+ "message,expected",+ [+ (HumanMessage([{"type": "input_text", "text": "deployment west"}]), "deployment west"),+ (AIMessage([{"type": "output_text", "text": "remembered"}]), "remembered"),+ (+ HumanMessage(+ [+ {"type": "input_image", "image_url": "https://example.invalid/chart.png"},+ {"type": "input_text", "text": "chart caption"},+ {"type": "text", "text": "second line"},+ ]+ ),+ "chart caption\nsecond line",+ ),+ (HumanMessage([{"type": "text", "text": "standard"}]), "standard"),+ (AIMessage("plain"), "plain"),+ (HumanMessage([{"type": ["text"], "text": "ignored"}]), ""),+ (HumanMessage([{"type": {}, "text": "ignored"}]), ""),+ ],+)+def test_recorder_preserves_responses_text(message, expected, asynchronous):+ client = InMemoryOpenVikingClient()+ client.create_session("responses")+ recorder = OpenVikingSessionRecorder(client=client)+ if asynchronous:+ result = asyncio.run(recorder.arecord("responses", [message]))+ else:+ result = recorder.record("responses", [message])+ assert result.messages_written == 1+ assert client.sessions["responses"][0]["parts"] == [{"type": "text", "text": expected}]+++def test_responses_text_used_for_recall_and_tool_output():+ user = HumanMessage([{"type": "input_text", "text": "new question"}])+ assert get_latest_user_text([HumanMessage("old question"), user]) == "new question"+ tool = ToolMessage([{"type": "output_text", "text": "tool result"}], tool_call_id="call-1")+ client = InMemoryOpenVikingClient()+ client.create_session("tools")+ recorder = OpenVikingSessionRecorder(client=client)+ recorder.record("tools", [tool])+ assert client.sessions["tools"][0]["parts"][0]["tool_output"] == "tool result"diff --git a/openviking/message/message.py b/openviking/message/message.pyindex 3f2aa230f..10b1af282 100644--- a/openviking/message/message.py+++ b/openviking/message/message.py@@ -123,7 +123,7 @@ class Message:"skill_uri": part.skill_uri,"tool_status": part.tool_status,}- if part.tool_input:+ if part.tool_input is not None:d["tool_input"] = part.tool_inputif part.tool_output:d["tool_output"] = part.tool_outputdiff --git a/openviking/session/retention.py b/openviking/session/retention.pyindex ff18f383a..997f37dc6 100644--- a/openviking/session/retention.py+++ b/openviking/session/retention.py@@ -154,7 +154,7 @@ def _truncate_message_group(Durable messages are never mutated. Text/context bodies and tool outputs arethe only fields shortened. Tool IDs, names and result pairing remain intact;- oversized tool inputs are replaced only when their fixed serialized cost+ oversized tool inputs are omitted only when their fixed serialized costwould otherwise make the whole atomic Step impossible to return."""if token_budget <= 0 or not messages:@@ -186,7 +186,7 @@ def _truncate_message_group(for part in message.parts:if not isinstance(part, ToolPart) or not part.tool_input:continue- part.tool_input = {}+ part.tool_input = Nonetruncated_ids.add(message.id)if _message_tokens(cloned) <= token_budget:breakdiff --git a/sdk/python/openviking_sdk/message.py b/sdk/python/openviking_sdk/message.pyindex a0ac480dc..3996ff6aa 100644--- a/sdk/python/openviking_sdk/message.py+++ b/sdk/python/openviking_sdk/message.py@@ -102,7 +102,7 @@ def normalize_part(part: Union[Dict[str, Any], MessagePart, Any]) -> Dict[str, A"tool_output_externalized_reason",):value = getattr(part, field, None)- if value:+ if value or (field == "tool_input" and value is not None):payload[field] = valuefor field in ("duration_ms",diff --git a/sdk/python/tests/test_async_client_behaviors.py b/sdk/python/tests/test_async_client_behaviors.pyindex 77d431abf..aca3532f4 100644--- a/sdk/python/tests/test_async_client_behaviors.py+++ b/sdk/python/tests/test_async_client_behaviors.py@@ -145,7 +145,8 @@ async def test_async_http_client_sends_message_semantics_and_turn_retention():@pytest.mark.asyncio-async def test_async_http_client_normalizes_message_part_objects():[email protected]("tool_input", [None, {}, {"query": "OpenViking"}])+async def test_async_http_client_normalizes_message_part_objects(tool_input):client = AsyncHTTPClient(url="http://localhost:1933")fake_http = SimpleNamespace(post=AsyncMock(return_value=object()))client._http = fake_http@@ -165,7 +166,7 @@ async def test_async_http_client_normalizes_message_part_objects():ToolPart(tool_id="call-1",tool_name="search",- tool_input={"query": "OpenViking"},+ tool_input=tool_input,tool_status="completed",duration_ms=12.5,prompt_tokens=42,@@ -202,7 +203,7 @@ async def test_async_http_client_normalizes_message_part_objects():"type": "tool","tool_id": "call-1","tool_name": "search",- "tool_input": {"query": "OpenViking"},+ **({"tool_input": tool_input} if tool_input is not None else {}),"tool_status": "completed","duration_ms": 12.5,"prompt_tokens": 42,diff --git a/tests/session/test_retention.py b/tests/session/test_retention.pyindex 5d09e10cf..a79ebe2bd 100644--- a/tests/session/test_retention.py+++ b/tests/session/test_retention.py@@ -74,9 +74,7 @@ def test_explicit_assistant_role_tool_transport_stays_with_previous_step():turns = build_turns(messages)- assert [[message.id for message in step.messages] for step in turns[0].steps] == [- ["a1", "tr1"]- ]+ assert [[message.id for message in step.messages] for step in turns[0].steps] == [["a1", "tr1"]]def test_message_semantic_fields_round_trip_without_affecting_legacy_rows():@@ -269,3 +267,32 @@ def test_active_budget_never_splits_an_assistant_tool_step():assert plan.estimated_tokens <= 40assert ("a1" in returned_ids) == ("tr1" in returned_ids)++[email protected]("tool_input", [{}, {"query": "x" * 4000}])+def test_active_budget_distinguishes_removed_inputs_from_zero_arg_calls(tool_input):+ message = Message(+ id="a1",+ role="assistant",+ parts=[+ ToolPart(+ tool_id="t1",+ tool_name="search",+ tool_input=tool_input,+ tool_output="result",+ tool_status="completed",+ )+ ],+ )++ plan = fit_active_messages_to_budget([message], token_budget=32)++ assert plan.estimated_tokens <= 32+ assert [returned.id for returned in plan.messages] == ["a1"]+ part = Message.from_dict(plan.messages[0].to_dict()).parts[0]+ assert part.tool_input == (None if tool_input else {})+ assert part.tool_id == "t1"+ assert part.tool_output == "result"+ assert ("tool_input" in plan.messages[0].to_dict()["parts"][0]) == (not tool_input)+ assert plan.truncated_message_ids == (["a1"] if tool_input else [])+ assert message.parts[0].tool_input == tool_inputdiff --git a/tests/unit/test_message.py b/tests/unit/test_message.pyindex a0a65f96c..77352d8b3 100644--- a/tests/unit/test_message.py+++ b/tests/unit/test_message.py@@ -509,7 +509,8 @@ class TestMessageToDict:assert d["parts"][0]["type"] == "text"assert d["parts"][1]["type"] == "context"- def test_to_dict_with_tool_part(self):+ @pytest.mark.parametrize("tool_input", [None, {}, {"query": "OpenViking"}])+ def test_to_dict_with_tool_part(self, tool_input):"""Test to_dict with ToolPart."""msg = Message(id="msg-1",@@ -519,6 +520,7 @@ class TestMessageToDict:tool_id="call-1",tool_name="search",tool_uri="viking://tools/1",+ tool_input=tool_input,tool_status="completed",duration_ms=100,prompt_tokens=50,@@ -542,6 +544,9 @@ class TestMessageToDict:assert d["parts"][0]["tool_output_original_chars"] == 1000assert d["parts"][0]["tool_output_preview_chars"] == 100assert d["parts"][0]["tool_output_externalized_reason"] == "single_threshold"+ assert ("tool_input" in d["parts"][0]) == (tool_input is not None)+ assert Message.from_dict(d).parts[0].tool_input == tool_input+ assert Message.from_dict(json.loads(msg.to_jsonl())).to_dict()["parts"] == d["parts"]def test_to_dict_with_image_part(self):"""Test to_dict with ImagePart."""