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(gateway): stop duplicate recall and new sessions for DSH and tool-less side requests (#6295)
t0sakie855796c83f9
diff --git a/docs/en/gateway/10-overview-and-requests.md b/docs/en/gateway/10-overview-and-requests.mdindex 73e4a8460..7a46ca74e 100644--- a/docs/en/gateway/10-overview-and-requests.md+++ b/docs/en/gateway/10-overview-and-requests.md@@ -100,7 +100,7 @@ Every record has one type. The type decides whether the request searches memory| --- | --- | --- || New message | A request whose last message is a new user message | Searched once, earlier memory replayed; the turn is saved || Tool step | The same turn continuing, for example with tool results | Earlier memory replayed; saved with its turn |-| Housekeeping | Title, summary, compaction, suggestion and permission-check requests | Earlier memory replayed; never saved |+| Housekeeping | Title, summary, compaction, suggestion and permission-check requests; once a conversation has sent client tools, a separate request in it with neither tools nor earlier replies, such as Claude Code summarizing a fetched page | Earlier memory replayed; never saved || Sub-agent | Requests from a client's sub-agents | Earlier memory replayed; never saved || Token count | Token-counting requests (`/gateway/v1/messages/count_tokens`) | Earlier memory replayed, so the count matches what is actually sent || Forwarded | Requests passed through unchanged, without memory: other endpoints, Responses requests that rely on provider-side state, compressed or unreadable bodies | None |diff --git a/docs/zh/gateway/10-overview-and-requests.md b/docs/zh/gateway/10-overview-and-requests.mdindex 6fef12afe..3b8e9dbd4 100644--- a/docs/zh/gateway/10-overview-and-requests.md+++ b/docs/zh/gateway/10-overview-and-requests.md@@ -100,7 +100,7 @@ description: 读懂 Studio 中 OpenViking 网关的“概览”和“请求日| --- | --- | --- || 新消息 | 最后一条是新用户消息的请求 | 检索一次,并放回之前的记忆;这一轮会被保存 || 工具步骤 | 同一轮的延续,例如带着工具结果继续 | 放回之前的记忆;随所在轮次一起保存 |-| 辅助请求 | 生成标题、摘要、压缩、建议和权限检查的请求 | 放回之前的记忆;从不保存 |+| 辅助请求 | 生成标题、摘要、压缩、建议和权限检查的请求;对话带过客户端工具后,同一对话里不带工具、也不带之前回复的单独请求,例如 Claude Code 汇总抓取的网页 | 放回之前的记忆;从不保存 || 子 Agent | 客户端的子 Agent 发出的请求 | 放回之前的记忆;从不保存 || Token 计数 | token 计数请求(`/gateway/v1/messages/count_tokens`) | 放回之前的记忆,计数才和实际发送的内容一致 || 直接转发 | 原样转发、不带记忆的请求:其他接口、依赖服务商端状态的 Responses 请求、压缩过或无法解析的请求体 | 无 |diff --git a/openviking_gateway/client-rules.json b/openviking_gateway/client-rules.jsonindex eba1157c1..df671a77b 100644--- a/openviking_gateway/client-rules.json+++ b/openviking_gateway/client-rules.json@@ -27,6 +27,12 @@"You are a helpful AI assistant tasked with summarizing","Generate a concise.{0,40}title","Create a short.{0,40}title",- "You are a permission classifier"+ "You are a permission classifier",+ "Generate the session title from"+ ],+ "noise_patterns": [+ "\\A\\s*Attached image\\(s\\) from tool result:\\s*\\Z",+ "\\A\\s*Current runtime context\\. This snapshot supersedes earlier runtime-context snapshots\\.[\\s\\S]*",+ "\\A\\s*Time sampled while preparing turn \\d+, step \\d+: [\\s\\S]*"]}diff --git a/openviking_gateway/kernel.py b/openviking_gateway/kernel.pyindex f09c44131..59de1d037 100644--- a/openviking_gateway/kernel.py+++ b/openviking_gateway/kernel.py@@ -214,6 +214,18 @@ class MemoryKernel:upstream = next((u for u in upstreams if u["id"] == root["upstream_id"]), upstream)policy = Policy.model_validate(root["policy"])kind, anchor = classify(body, headers, messages)+ if body.get("tools"):+ if (K.CLIENT_TOOLS, "") not in records:+ await self.store.replay.put(scope, sid, K.CLIENT_TOOLS, "", {})+ elif (+ kind == "user"+ and (K.CLIENT_TOOLS, "") in records+ and not any(adapter.is_reply(m) for m in messages)+ ):+ # Clients such as Claude Code send side requests, like summarizing a fetched+ # page, under the conversation's session header: a fresh exchange without+ # tools. A turn of the conversation itself resends its replies.+ kind = "auxiliary"disabled = (K.DISABLED, "") in recordsif plugin and not disabled:await self.store.replay.put(scope, sid, K.DISABLED, "", {"reason": "plugin_present"})diff --git a/openviking_gateway/protocols.py b/openviking_gateway/protocols.pyindex 42eb17219..348e412de 100644--- a/openviking_gateway/protocols.py+++ b/openviking_gateway/protocols.py@@ -21,6 +21,11 @@ _RULES = json.loads(files("openviking_gateway").joinpath("client-rules.json").rePLUGIN_TAGS = tuple(_RULES["plugin_tags"])NOISE_TAGS = tuple(_RULES["noise_tags"])AUXILIARY_PATTERNS = tuple(_RULES["auxiliary_patterns"])+# Whole messages clients write on the user's behalf: pi-ai's label for a tool result's+# images, which it sends as a user message after the tool result, and DSH's runtime and+# time snapshots, which DSH can append mid-turn. Each is a message of its own, so a+# snapshot pattern takes the rest of the text. Anchored, so other text fails at once.+NOISE_PATTERNS = tuple(re.compile(p) for p in _RULES["noise_patterns"])# re.I tries a pattern at every offset; plain ASCII patterns (no escapes, sets or flags),# lowercased, get re's literal search instead, the same match unless the text has one of# the only letters where lower() and re.I differ against ASCII (checked in the tests).@@ -38,6 +43,8 @@ def clean_text(text: str) -> str:for tag in NOISE_TAGS:text = re.sub(rf"<{tag}\b[^>]*>[\s\S]*?</{tag}>", " ", text, flags=re.I)text = re.sub(r"^\s*\[Subagent Context\][^\n]*", "", text, flags=re.M)+ for pattern in NOISE_PATTERNS:+ text = pattern.sub("", text)return text.replace("\0", "").strip()@@ -101,6 +108,21 @@ def text_content(message: dict) -> str:return ""+def canonical_arguments(value: Any) -> Any:+ """Tool-call arguments as compact JSON in their own key order, else unchanged.++ Clients such as pi-ai parse the arguments a reply streamed and send them back+ re-serialized, so an upstream that spaces its JSON never matches its own reply.+ Numbers keep their JSON spelling, so ``30.0`` and ``30`` still differ.+ """+ if not isinstance(value, str):+ return value+ try:+ return orjson.dumps(orjson.loads(value)).decode()+ except orjson.JSONDecodeError:+ return value++def normalize(value: Any) -> Any:if isinstance(value, list):return [@@ -123,6 +145,8 @@ def normalize(value: Any) -> Any:for k, v in value.items()if k != "cache_control"}+ if "arguments" in out:+ out["arguments"] = canonical_arguments(out["arguments"])if "content" in out:content = out["content"]if isinstance(content, list) and len(content) == 1 and isinstance(content[0], dict):diff --git a/openviking_gateway/records.py b/openviking_gateway/records.pyindex dc5a984c5..adc11e076 100644--- a/openviking_gateway/records.py+++ b/openviking_gateway/records.py@@ -12,6 +12,8 @@ class RecordKind(str, Enum):# The gateway tools frozen for the session, and the one-way switch that turns them off.TOOLS = "tools"TOOLS_CLOSED = "tools_closed"+ # The session's client sent tools of its own, which its turns then always carry.+ CLIENT_TOOLS = "client_tools"HIDDEN = "hidden"REPLACEMENT = "replacement"# The session whose model produced the reply ending at the anchor.diff --git a/tests/gateway/test_kernel.py b/tests/gateway/test_kernel.pyindex ec09327e5..53188e8cd 100644--- a/tests/gateway/test_kernel.py+++ b/tests/gateway/test_kernel.py@@ -18,6 +18,7 @@ from openviking_gateway.protocols import (LOWERED_AUXILIARY,REI_CASE_EXCEPTIONS,auxiliary,+ canonical_arguments,classify,client_openviking_tools,normalize,@@ -698,6 +699,99 @@ def test_client_openviking_tools_outside_tool_list():assert not client_openviking_tools({"tools": None, "mcp_servers": None}, OV_NAMES)+def test_tool_arguments_compact_in_their_own_order():+ assert (+ canonical_arguments('{"b": 1, "a": ["x", {"c": null}]}') == '{"b":1,"a":["x",{"c":null}]}'+ )+ assert canonical_arguments('{"q": "\\u84dd"}') == '{"q":"蓝"}'+ # Partial or free-form arguments, and the parsed objects some protocols carry, stay as sent.+ for value in ('{"b": 1', "pwd", {"b": 1}, None):+ assert canonical_arguments(value) == value+++def test_text_clients_add_for_the_user_is_not_a_user_turn():+ # As DSH sends a turn over Chat Completions: the runtime-context snapshot follows the+ # prompt, and pi-ai sends a tool result's image as a user message after the tool result.+ prompt = {"role": "user", "content": "Read red.png"}+ snapshot = {+ "role": "user",+ "content": "Current runtime context. This snapshot supersedes earlier runtime-context "+ 'snapshots.\n\nCurrent working directory: "/work".',+ }+ call = {"id": "1", "type": "function", "function": {"name": "read", "arguments": "{}"}}+ image = {+ "role": "user",+ "content": [+ {"type": "text", "text": "Attached image(s) from tool result:"},+ {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}},+ ],+ }+ turn = [prompt, snapshot]+ assert classify({}, {}, turn) == ("user", 0)+ turn += [+ {"role": "assistant", "content": None, "tool_calls": [call]},+ {"role": "tool", "tool_call_id": "1", "content": "(see attached image)"},+ image,+ ]+ assert classify({}, {}, turn) == ("continuation", 0)+ # DSH's time context, appended before a step once ten minutes have passed.+ time = "Time sampled while preparing turn 1, step 4: 2026-10-19T17:02:11+08:00[Asia/Shanghai]"+ turn += [+ {"role": "user", "content": time + "\nElapsed since the preceding step context: 10m 2s."}+ ]+ assert classify({}, {}, turn) == ("continuation", 0)+ # The user quoting the label is still the user.+ quoted = {"role": "user", "content": 'Why does it say "Attached image(s) from tool result:"?'}+ assert classify({}, {}, [*turn, quoted])[0] == "user"+ title = {+ "role": "user",+ "content": 'Generate the session title from this JSON array:\n[{"seq":8}]',+ }+ assert classify({}, {}, [title])[0] == "auxiliary"+++async def test_tool_less_request_in_a_session_with_client_tools_is_auxiliary(+ setup_kernel, credential, policy+):+ kernel, _, viking, _ = setup_kernel+ policy = {**policy, "show_recall": True}++ def body(text, tools=()):+ return {+ "model": "m",+ "max_tokens": 64,+ "messages": [{"role": "user", "content": text}],+ **({"tools": list(tools)} if tools else {}),+ }++ bash = {"name": "Bash", "input_schema": {"type": "object"}}+ # A helper request may come first; until the client sends tools, nothing changes.+ first = await prepare(kernel, body("Say hi"), credential, policy, "anthropic")+ assert first.kind == "user"+ main = await prepare(+ kernel, body("Deploy the blue cluster", [bash]), credential, policy, "anthropic"+ )+ assert main.kind == "user" and main.reply_lead+ # Claude Code summarizes a fetched page under the conversation's session header.+ page = body("Web page content:\n---\nExample Domain\n---\n\nWhat is the title?")+ side = await prepare(kernel, page, credential, policy, "anthropic")+ assert side.kind == "auxiliary" and not side.reply_lead+ assert not context_blocks(side.body["messages"][0]) and len(viking.recalls) == 2+ # The conversation's own turn with tools switched off resends its replies and is a turn.+ off = body("Deploy the blue cluster")+ off["messages"] += [+ {"role": "assistant", "content": "Which region?"},+ {"role": "user", "content": "Singapore"},+ ]+ assert (await prepare(kernel, off, credential, policy, "anthropic")).kind == "user"+ assert len(viking.recalls) == 3+ # A session that never sent client tools, such as one-shot SDK questions, recalls each time.+ for question in ("Where do I deploy?", "Who owns the cluster?"):+ sdk = await prepare(kernel, body(question), credential, policy, "anthropic", session="sdk")+ assert sdk.kind == "user"+ assert len(viking.recalls) == 5++def test_capture_pairs_tools_and_strips_noise():messages = [{"role": "user", "content": "<system-reminder>noise</system-reminder>please deploy"},diff --git a/tests/gateway/test_reply_inheritance.py b/tests/gateway/test_reply_inheritance.pyindex 77d2c44d2..28cea8c97 100644--- a/tests/gateway/test_reply_inheritance.py+++ b/tests/gateway/test_reply_inheritance.py@@ -163,6 +163,45 @@ async def test_anonymous_client_tool_handoff_keeps_the_session_and_captures_the_assert len(set(viking.write_sessions)) == 1[email protected]("protocol", ["chat", "responses"])+async def test_resent_call_matches_its_reply_whatever_its_json_spacing(+ setup_kernel, credential, policy, protocol+):+ # Ark's DeepSeek streams spaced arguments; pi-ai, as in DSH, resends them compact.+ kernel, _, _, _ = setup_kernel+ policy.update(recall=False)+ field = "input" if protocol == "responses" else "messages"+ question = {"role": "user", "content": "Weather in Paris?"}+ body = {+ "model": "model",+ field: [question],+ **({"store": False} if protocol == "responses" else {}),+ }+ first = await kernel.prepare(body, protocol, {}, credential, {"id": "upstream"}, policy)+ reply, _ = client_call_reply(protocol)+ if protocol == "chat":+ function = reply["choices"][0]["message"]["tool_calls"][0]["function"]+ else:+ function = reply["output"][0]+ function["arguments"] = '{"city": "Paris"}'+ capture = ResponseCapture(protocol)+ capture.nonstream(reply)+ await kernel.completed(first, credential, capture)+ call, result = tool_handoff(protocol)+ if protocol == "responses":+ item = {**reply["output"][0], "arguments": '{"city":"Paris"}'}+ call, result = item, {"type": "function_call_output", "call_id": "c1", "output": "Sunny"}+ continued = await kernel.prepare(+ {**body, field: [question, call, result]},+ protocol,+ {},+ credential,+ {"id": "upstream"},+ policy,+ )+ assert continued.session == first.session and continued.kind == "continuation"++def tool_handoff(protocol):if protocol == "chat":call = {diff --git a/tests/gateway/test_storage_ports.py b/tests/gateway/test_storage_ports.pyindex 1cfa0b62f..d60b09172 100644--- a/tests/gateway/test_storage_ports.py+++ b/tests/gateway/test_storage_ports.py@@ -200,6 +200,7 @@ async def test_kernel_and_capture_work_with_kv_ports(credential, protocol):K.DISABLED,K.TOOLS,K.TOOLS_CLOSED,+ K.CLIENT_TOOLS,K.HIDDEN,K.REPLACEMENT,K.REPLY,