Skip to content

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

perf(gateway): cut per-request CPU in request preparation, reply relay and reply lookup (#6026)

t0saki26fa00b91596
diff --git a/openviking_gateway/capture.py b/openviking_gateway/capture.py
index 781b373dd..73334ffc7 100644
--- a/openviking_gateway/capture.py
+++ b/openviking_gateway/capture.py
@@ -25,7 +25,7 @@ from .models import Policy
from .notices import without_notices
from .protocols import clean_text, is_user, text_content, unwrap_client
from .records import RecordKind as K
-from .tool_protocols import hidden_chain
+from .tool_protocols import reply_chain
logger = logging.getLogger(__name__)
MAX_ATTEMPTS = 5
@@ -229,7 +229,7 @@ class CapturePipeline:
async def stage(self, request, response, policy):
messages = [*request.messages, *response.output]
- chain = await asyncio.to_thread(hidden_chain, messages, request.protocol)
+ chain = reply_chain(request, response.output)
while True:
old = await self.queue.get(request.scope, request.session)
if (
diff --git a/openviking_gateway/kernel.py b/openviking_gateway/kernel.py
index 3ca695eda..f09c44131 100644
--- a/openviking_gateway/kernel.py
+++ b/openviking_gateway/kernel.py
@@ -46,7 +46,7 @@ from .records import RecordKind as K
from .state_store import get_state
from .storage import KernelStore, digest
from .tool_catalog import reply_block_reason, select_tools, tool_block_reason
-from .tool_protocols import hidden_chain, replay_hidden, tool_protocol
+from .tool_protocols import hidden_chain, replay_hidden, reply_chain, tool_protocol
from .tool_protocols.common import PREFIX, SummaryError
from .vendors import parameter_fingerprint
from .windows import remind, status_line
@@ -636,9 +636,7 @@ class MemoryKernel:
for text in tool_protocol(request.protocol).unsigned_thinking(output):
await self.store.state.swap(request.scope, THINKING + digest(text), Document(), {})
if output and request.kind in {"user", "continuation"}:
- chain = await asyncio.to_thread(
- hidden_chain, [*request.messages, *output], request.protocol
- )
+ chain = reply_chain(request, output)
request.reply_anchor = chain[-1]
if response.finished and not request.disabled and replays_reasoning(request.upstream):
await self.record_reasoning(request, output, chain)
@@ -656,9 +654,7 @@ class MemoryKernel:
if not reasoning:
return
if visible is not output:
- chain = await asyncio.to_thread(
- hidden_chain, [*request.messages, *visible], request.protocol
- )
+ chain = reply_chain(request, visible)
# Without its reasoning items the reply ends at another anchor; forks find it there.
if chain[-1]:
await self.store.replay.put(request.scope, request.session, K.REPLY, chain[-1], {})
diff --git a/openviking_gateway/protocols.py b/openviking_gateway/protocols.py
index e88fdd46d..e044b820d 100644
--- a/openviking_gateway/protocols.py
+++ b/openviking_gateway/protocols.py
@@ -9,6 +9,7 @@ import hashlib
import json
import math
import re
+from collections.abc import Sequence
from decimal import Decimal
from importlib.resources import files
from typing import Any
@@ -20,6 +21,15 @@ _RULES = json.loads(files("openviking_gateway").joinpath("client-rules.json").re
PLUGIN_TAGS = tuple(_RULES["plugin_tags"])
NOISE_TAGS = tuple(_RULES["noise_tags"])
AUXILIARY_PATTERNS = tuple(_RULES["auxiliary_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).
+REI_CASE_EXCEPTIONS = "İıſ"
+LOWERED_AUXILIARY = (
+ tuple(re.compile(p.lower()) for p in AUXILIARY_PATTERNS)
+ if all(p.isascii() and not re.search(r"[\\\[]|\(\?", p) for p in AUXILIARY_PATTERNS)
+ else ()
+)
# Vendors whose models expect their reasoning back in later turns.
REASONING_VENDORS = {"deepseek", "ark", "byteplus"}
@@ -138,8 +148,12 @@ def normalize(value: Any) -> Any:
return value
-def prefix_chain(messages: list[dict]) -> list[str]:
- previous = hashlib.sha256(NORMALIZATION_VERSION.encode()).digest()
+def prefix_chain(messages: list[dict], after: Sequence[str] = ()) -> list[str]:
+ """Anchors of ``messages``; ``after``, the chain of the history before them, continues it."""
+ previous = next(
+ (bytes.fromhex(a) for a in reversed(after) if a),
+ hashlib.sha256(NORMALIZATION_VERSION.encode()).digest(),
+ )
result = []
for message in messages:
if (
@@ -263,9 +277,11 @@ def client_openviking_tools(body: dict, names) -> bool:
):
return True
# Deferred MCP tools, as in Claude Code's tool search, are listed only by name, one
- # per line; a name mentioned in prose is not a tool.
+ # per line; a name mentioned in prose is not a tool. Most texts have no "__" at all.
deferred = rf"^[ \t]*mcp__[\w-]*?{OPENVIKING_WORD}[\w-]*__(?:{alternatives})[ \t]*$"
- return any(re.search(deferred, text, re.I | re.M) for text in client_texts(body))
+ return any(
+ "__" in text and re.search(deferred, text, re.I | re.M) for text in client_texts(body)
+ )
def is_user(message):
@@ -281,6 +297,14 @@ def is_user(message):
)
+def auxiliary(text: str) -> bool:
+ """Whether ``text`` matches an auxiliary pattern, ignoring case."""
+ if LOWERED_AUXILIARY and not any(c in text for c in REI_CASE_EXCEPTIONS):
+ text = text.lower()
+ return any(pattern.search(text) for pattern in LOWERED_AUXILIARY)
+ return any(re.search(pattern, text, re.I) for pattern in AUXILIARY_PATTERNS)
+
+
def classify(body: dict, headers: dict, messages: list[dict]) -> tuple[str, int]:
anchor = next((i for i in range(len(messages) - 1, -1, -1) if is_user(messages[i])), -1)
request_class = headers.get("x-claude-code-request-class", "")
@@ -308,7 +332,7 @@ def classify(body: dict, headers: dict, messages: list[dict]) -> tuple[str, int]
probe = str(body.get("system", "")) + str(body.get("instructions", ""))
if anchor >= 0:
probe += text_content(messages[anchor])
- if any(re.search(pattern, probe, re.I) for pattern in AUXILIARY_PATTERNS):
+ if auxiliary(probe):
return "auxiliary", anchor
if anchor < 0:
return "continuation", anchor
@@ -379,16 +403,21 @@ def usage_of(value: dict) -> dict:
class SSEDecoder:
"""Incremental byte framing, including CRLF and frames split inside UTF-8."""
+ # A frame ends at a blank line. Only where it ends matters, so the pattern starts
+ # at the newline, which the scan finds much faster than an optional CR.
+ frame_end = re.compile(rb"\n\r?\n")
+
def __init__(self, max_buffer=8 * 1024 * 1024):
self.buffer = b""
self.max_buffer = max_buffer
def feed(self, chunk: bytes) -> list[bytes]:
- self.buffer += chunk
- frames = []
- while match := re.search(rb"\r?\n\r?\n", self.buffer):
- frames.append(self.buffer[: match.end()])
- self.buffer = self.buffer[match.end() :]
+ # One pass and one copy of the rest, however many frames the chunk holds.
+ buffer, frames, start = self.buffer + chunk, [], 0
+ for match in self.frame_end.finditer(buffer):
+ frames.append(buffer[start : match.end()])
+ start = match.end()
+ self.buffer = buffer[start:]
if len(self.buffer) > self.max_buffer:
raise ValueError("SSE event exceeds capture limit")
return frames
@@ -396,7 +425,7 @@ class SSEDecoder:
@staticmethod
def data(frame: bytes) -> dict | None:
data = b"\n".join(
- line[5:].lstrip(b" ") for line in frame.splitlines() if line.startswith(b"data:")
+ [line[5:].lstrip(b" ") for line in frame.splitlines() if line.startswith(b"data:")]
)
try:
value = orjson.loads(data)
diff --git a/openviking_gateway/proxy.py b/openviking_gateway/proxy.py
index bad57498b..891365f9a 100644
--- a/openviking_gateway/proxy.py
+++ b/openviking_gateway/proxy.py
@@ -482,10 +482,9 @@ class ProxyRequest:
async for chunk in response.content.iter_any():
if observe:
try:
- for frame in decoder.feed(chunk):
- data = decoder.data(frame)
- if data:
- capture.event(data)
+ # A chunk inside one event completes none: no reply to reread.
+ if frames := decoder.feed(chunk):
+ capture.events(d for d in map(decoder.data, frames) if d)
except Exception:
observe = False
capture.complete = False
diff --git a/openviking_gateway/replay_store.py b/openviking_gateway/replay_store.py
index 206edfb03..efc6254e2 100644
--- a/openviking_gateway/replay_store.py
+++ b/openviking_gateway/replay_store.py
@@ -55,8 +55,11 @@ class SQLiteReplayStore:
def replies():
owners = {}
with self.db.connect() as c:
+ # Without ANALYZE statistics SQLite prefers the covering primary key and
+ # scans every record of the scope.
for row in c.execute(
- "SELECT anchor,session FROM replay WHERE scope=? AND kind=? "
+ "SELECT anchor,session FROM replay INDEXED BY replay_kinds "
+ "WHERE scope=? AND kind=? "
"AND anchor IN (SELECT value FROM json_each(?))",
(scope, K.REPLY, orjson.dumps(anchors).decode()),
):
diff --git a/openviking_gateway/tool_loop.py b/openviking_gateway/tool_loop.py
index 119574741..67f90fc2a 100644
--- a/openviking_gateway/tool_loop.py
+++ b/openviking_gateway/tool_loop.py
@@ -14,7 +14,7 @@ from .notices import tool_tail
from .protocols import SSEDecoder, replays_reasoning, usage_of
from .records import RecordKind as K
from .tool_catalog import notice_head
-from .tool_protocols import hidden_chain, tool_protocol
+from .tool_protocols import reply_chain, tool_protocol
from .tool_protocols.common import PREFIX, ToolLoopError, ToolRound, add_usage
from .windows import ALONE, NEW_CONTEXT, window_number
@@ -69,19 +69,29 @@ class HiddenToolLoop:
received += len(chunk)
if received > 64 * 1024 * 1024:
raise ToolLoopError("Model stream exceeds tool response budget")
- for frame in decoder.feed(chunk):
- value = decoder.data(frame)
- if value is None:
- continue
- if "error" in value or value.get("type") == "error":
- raise ToolLoopError("Model stream failed")
- for event in adapter.event(value):
- yield adapter.encode(event)
+ # Events that arrive together leave in one write, any before a failure included.
+ events = []
+ try:
+ events.extend(self.decode(decoder, chunk))
+ finally:
+ if events:
+ yield b"".join(events)
if decoder.buffer.strip():
raise ToolLoopError("Incomplete model event stream")
self.round = adapter.end()
response.close()
+ def decode(self, decoder, chunk):
+ """Client bytes for the upstream events ``chunk`` completes."""
+ for frame in decoder.feed(chunk):
+ value = decoder.data(frame)
+ if value is None:
+ continue
+ if "error" in value or value.get("type") == "error":
+ raise ToolLoopError("Model stream failed")
+ for event in self.adapter.event(value):
+ yield self.adapter.encode(event)
+
def observe(self):
self.transcript.extend(self.round.output)
add_usage(self.usage, self.round.usage)
@@ -182,9 +192,7 @@ class HiddenToolLoop:
# The next request strips the recall notice before matching, so the anchor,
# the visible count and the replaced span all go without it.
visible = self.adapter.strip_lead(self.adapter.visible)
- anchor = (
- hidden_chain([*self.prepared.messages, *visible], self.protocol)[-1] if visible else ""
- )
+ anchor = reply_chain(self.prepared, visible)[-1] if visible else ""
if self.window:
# new_context refuses an anchor whose cut this request applies, so only a
# concurrent request can have stored another header here first.
@@ -204,7 +212,7 @@ class HiddenToolLoop:
# which relaying recorded as the same reply; the transcript replaces it there too.
dropped, _ = self.adapter.split_reasoning(visible)
if dropped is not visible and replays_reasoning(self.prepared.upstream):
- other = hidden_chain([*self.prepared.messages, *dropped], self.protocol)[-1]
+ other = reply_chain(self.prepared, dropped)[-1]
if dropped and other:
await self.store.replay.put(
self.prepared.scope,
diff --git a/openviking_gateway/tool_protocols/__init__.py b/openviking_gateway/tool_protocols/__init__.py
index 035832b24..b63a9e6b8 100644
--- a/openviking_gateway/tool_protocols/__init__.py
+++ b/openviking_gateway/tool_protocols/__init__.py
@@ -2,6 +2,7 @@
# SPDX-License-Identifier: AGPL-3.0
"""Native protocol adapters: what the gateway knows about each wire protocol."""
+from collections.abc import Sequence
from dataclasses import dataclass, field
from ..records import RecordKind as K
@@ -59,15 +60,32 @@ class ResponseCapture:
vars(self).update(adapter.reply())
def event(self, body):
+ self.events([body])
+
+ def events(self, bodies):
+ """Events that arrived together; the reply is read once, after the last of them."""
if self.assembler is None:
self.assembler = tool_protocol(self.protocol)({})
self.assembler.begin()
- self.assembler.accumulate(body)
- vars(self).update(self.assembler.reply())
+ try:
+ for body in bodies:
+ self.assembler.accumulate(body)
+ finally:
+ vars(self).update(self.assembler.reply())
+
+
+def hidden_chain(messages: list[dict], protocol: str, after: Sequence[str] = ()) -> list[str]:
+ """Anchors of ``messages``; ``after``, the chain of the history before them, continues it."""
+ return tool_protocol(protocol).history_chain(messages, after)
-def hidden_chain(messages: list[dict], protocol: str) -> list[str]:
- return tool_protocol(protocol).history_chain(messages)
+def reply_chain(request, output: list[dict]) -> list[str]:
+ """``hidden_chain`` of the request's history followed by ``output``.
+
+ The request's ``capture_chain`` is that history's chain already, so only ``output``
+ is hashed: a reply costs its own size, not the whole history's.
+ """
+ return [*request.capture_chain, *hidden_chain(output, request.protocol, request.capture_chain)]
def replay_hidden(
diff --git a/openviking_gateway/tool_protocols/anthropic.py b/openviking_gateway/tool_protocols/anthropic.py
index f41a4e0d7..dd7877a03 100644
--- a/openviking_gateway/tool_protocols/anthropic.py
+++ b/openviking_gateway/tool_protocols/anthropic.py
@@ -13,6 +13,7 @@ from .common import (
ToolLoopError,
ToolProtocol,
ToolRound,
+ append,
call,
strip_notices,
)
@@ -157,11 +158,11 @@ class AnthropicProtocol(ToolProtocol):
index, delta = value["index"], value["delta"]
block = self.blocks[index]
if "partial_json" in delta:
- self.arguments[index] = self.arguments.get(index, "") + delta["partial_json"]
+ append(self.arguments, index, delta["partial_json"])
else:
for key in ("text", "thinking", "signature"):
if key in delta:
- block[key] = block.get(key, "") + delta[key]
+ append(block, key, delta[key])
if delta.get("type") == "citations_delta":
block.setdefault("citations", []).append(copy.deepcopy(delta["citation"]))
elif kind == "content_block_stop":
diff --git a/openviking_gateway/tool_protocols/chat.py b/openviking_gateway/tool_protocols/chat.py
index 3cc5e7da0..b13065792 100644
--- a/openviking_gateway/tool_protocols/chat.py
+++ b/openviking_gateway/tool_protocols/chat.py
@@ -42,7 +42,7 @@ class ChatProtocol(ToolProtocol):
return ToolProtocol.block_reason(body)
@staticmethod
- def history_chain(messages):
+ def history_chain(messages, after=()):
# Chat clients often omit reasoning/vendor metadata. Match visible
# content and calls; the stored transcript retains the original objects.
canonical = []
@@ -54,7 +54,7 @@ class ChatProtocol(ToolProtocol):
**({"tool_calls": message["tool_calls"]} if message.get("tool_calls") else {}),
}
canonical.append(message)
- return prefix_chain(canonical)
+ return prefix_chain(canonical, after)
@staticmethod
def split_reasoning(output):
diff --git a/openviking_gateway/tool_protocols/common.py b/openviking_gateway/tool_protocols/common.py
index 8690f8a68..b18ad71d0 100644
--- a/openviking_gateway/tool_protocols/common.py
+++ b/openviking_gateway/tool_protocols/common.py
@@ -13,6 +13,7 @@ import functools
import itertools
import uuid
from abc import ABC, abstractmethod
+from collections.abc import Sequence
from dataclasses import dataclass
import orjson
@@ -43,6 +44,18 @@ class SummaryError(Exception):
self.reason = reason
+def append(target, key, text):
+ """``target[key] += text``, in linear time over a whole stream of deltas."""
+ value = target.get(key) or ""
+ # Without the dict's reference the string is the local's alone, so CPython grows it
+ # in place instead of copying everything streamed so far for every delta.
+ target[key] = ""
+ try:
+ value += text
+ finally:
+ target[key] = value
+
+
def merge_delta(target, delta):
for key, value in delta.items():
if value is None:
@@ -52,7 +65,7 @@ def merge_delta(target, delta):
target[key] = {}
merge_delta(target[key], value)
elif isinstance(value, str) and key not in {"role", "type", "id"}:
- target[key] = (target.get(key) or "") + value
+ append(target, key, value)
elif isinstance(value, list):
target.setdefault(key, []).extend(copy.deepcopy(value))
else:
@@ -255,8 +268,8 @@ class ToolProtocol(ABC):
body["tool_choice"] = "none"
@staticmethod
- def history_chain(messages: list[dict]) -> list[str]:
- return prefix_chain(messages)
+ def history_chain(messages: list[dict], after: Sequence[str] = ()) -> list[str]:
+ return prefix_chain(messages, after)
@staticmethod
def join_replayed_history(messages: list[dict]) -> list[dict]:
diff --git a/tests/gateway/test_kernel.py b/tests/gateway/test_kernel.py
index ae18922ca..ec09327e5 100644
--- a/tests/gateway/test_kernel.py
+++ b/tests/gateway/test_kernel.py
@@ -1,6 +1,7 @@
import asyncio
import copy
import re
+import sys
import time
import pytest
@@ -13,6 +14,10 @@ from openviking_gateway.client import VikingError
from openviking_gateway.kernel import token_estimate
from openviking_gateway.models import Policy
from openviking_gateway.protocols import (
+ AUXILIARY_PATTERNS,
+ LOWERED_AUXILIARY,
+ REI_CASE_EXCEPTIONS,
+ auxiliary,
classify,
client_openviking_tools,
normalize,
@@ -580,6 +585,45 @@ def test_classification_and_plugin_namespaces():
assert client_openviking_tools(namespaces, OV_NAMES)
+def test_lowercase_agrees_with_case_insensitive_matching():
+ assert LOWERED_AUXILIARY
+ ascii_any_case = re.compile(r"[\x00-\x7f]", re.I)
+ differing = set()
+ for code in range(sys.maxunicode + 1):
+ char = chr(code)
+ lower = char.lower()
+ if len(lower) != 1:
+ differing.add(char)
+ elif lower.isascii() or ascii_any_case.fullmatch(char):
+ for c in map(chr, range(128)):
+ if bool(re.fullmatch(re.escape(c), char, re.I)) != (lower == c.lower()):
+ differing.add(char)
+ assert differing == set(REI_CASE_EXCEPTIONS)
+
+
+ "text, expected",
+ [
+ ("Please provide a detailed summary of the conversation", True),
+ ("[{'text': 'PLEASE PROVIDE A DETAILED SUMMARY OF THE CONVERSATION'}]", True),
+ ("Generate a concise" + "-" * 40 + "title", True),
+ ("Generate a concise" + "-" * 41 + "title", False),
+ ("Generate a concise\ntitle", False),
+ ("Create a short" + "Σ" * 40 + "Title", True),
+ ("You are a permıssion classifier", True),
+ ("Your taſk is to create a detailed summary of the conversation", True),
+ ("Generate a concise" + "İ" * 40 + "title", True),
+ ("Please provİde a detailed summary of the conversation", True),
+ ("Your tas\u212a is to create a detailed summary of the conversation", True),
+ ("请 generate a concise 标题 title", True),
+ ("Deploy please", False),
+ ],
+)
+def test_auxiliary_matches_patterns_ignoring_case(text, expected):
+ assert any(re.search(p, text, re.I) for p in AUXILIARY_PATTERNS) == expected
+ assert auxiliary(text) == expected
+
+
OV_NAMES = ["read", "ls", "glob", "find", "search", "add_resource"]
@@ -682,6 +726,7 @@ def test_sse_chunk_boundaries_and_anthropic_usage():
for byte in raw:
frames += decoder.feed(bytes([byte]))
assert b"".join(frames) == raw
+ assert SSEDecoder().feed(raw) == frames
capture = ResponseCapture("anthropic")
capture.event(
{
diff --git a/tests/gateway/test_native_tools.py b/tests/gateway/test_native_tools.py
index 106f58305..0f4b48a96 100644
--- a/tests/gateway/test_native_tools.py
+++ b/tests/gateway/test_native_tools.py
@@ -806,6 +806,68 @@ async def test_native_text_streams_before_round_finishes_and_cancel_closes(proto
assert closed and not loop.capture.complete
[email protected]("protocol", ["responses", "anthropic"])
+async def test_native_events_arriving_together_leave_together(protocol):
+ """One write per upstream chunk, the same bytes as event by event, failures included."""
+ from types import SimpleNamespace
+
+ from openviking_gateway.tool_loop import HiddenToolLoop, ToolLoopError
+ from openviking_gateway.tool_protocols import ResponseCapture
+
+ events = native_events(protocol, native_response(protocol, 1, owned=True))[:-3]
+ error = {"type": "error", "error": {"type": "overloaded_error"}}
+
+ async def relay(chunks):
+ class Content:
+ async def iter_any(self):
+ for chunk in chunks:
+ yield chunk
+
+ response = SimpleNamespace(
+ status=200,
+ headers={"content-type": "text/event-stream"},
+ content=Content(),
+ close=lambda: None,
+ )
+ prepared = SimpleNamespace(
+ body=request_body(protocol, True),
+ protocol=protocol,
+ root={"policy": {}},
+ metrics={},
+ tools_active=True,
+ reply_lead="",
+ )
+ loop = HiddenToolLoop(
+ prepared,
+ SimpleNamespace(allowed={"openviking_search"}),
+ None,
+ ResponseCapture(protocol),
+ )
+ loop.adapter.identifier = "fixed"
+ writes = []
+ with pytest.raises(ToolLoopError, match="Model stream failed"):
+ async for write in loop.run(response, None):
+ writes.append(write)
+ return writes
+
+ one_by_one = await relay([sse(event) for event in [*events, error]])
+ together = await relay([b"".join(sse(event) for event in [*events, error])])
+ assert len(together) == 1 < len(one_by_one)
+ assert together[0] == b"".join(one_by_one)
+
+
+def test_streamed_text_append_keeps_the_text_on_a_bad_delta():
+ from openviking_gateway.tool_protocols.common import append
+
+ block = {"type": "text", "text": None}
+ for piece in ("ab", "c", "d"):
+ append(block, "text", piece)
+ assert block == {"type": "text", "text": "abcd"}
+ with pytest.raises(TypeError):
+ append(block, "text", None)
+ assert block == {"type": "text", "text": "abcd"}
+
+
@pytest.mark.parametrize("protocol", ["responses", "anthropic"])
async def test_native_replay_survives_restart_edits_and_archive_boundary(
setup_kernel, credential, policy, protocol
@@ -892,6 +954,26 @@ def test_custom_responses_tools_are_captured_as_complete_pairs():
]
[email protected]("protocol", ["chat", "responses", "anthropic"])
+def test_hidden_chain_continues_from_the_chain_before(protocol):
+ """A reply's anchors continue the request's chain: hashing only the reply gives the same."""
+ from openviking_gateway.tool_protocols import hidden_chain
+
+ messages = [
+ {"role": "system", "content": "Be brief."},
+ {"role": "user", "content": [{"type": "text", "text": "héllo 🙂", "cache_control": {}}]},
+ {"role": "assistant", "content": [{"type": "thinking", "thinking": "hm"}]},
+ {"role": "assistant", "content": "Hi", "reasoning_content": "hm"},
+ {"role": "user", "content": "<context>docs</context><user_query> why? </user_query>"},
+ {"type": "function_call", "call_id": "c-1", "name": "f", "arguments": "{}"},
+ ]
+ chain = hidden_chain(messages, protocol)
+ assert chain[0] == chain[2] == "" and all(chain[3:])
+ for index in range(len(messages) + 1):
+ head = hidden_chain(messages[:index], protocol)
+ assert [*head, *hidden_chain(messages[index:], protocol, head)] == chain
+
+
@pytest.mark.parametrize("archive", [False, True])
async def test_responses_notice_removal_follows_injection_replay_and_archive_cut(
setup_kernel, credential, policy, archive
diff --git a/tests/gateway/test_review_regressions.py b/tests/gateway/test_review_regressions.py
index 15a50fa66..04864471b 100644
--- a/tests/gateway/test_review_regressions.py
+++ b/tests/gateway/test_review_regressions.py
@@ -2,6 +2,7 @@
import asyncio
import time
+from contextlib import contextmanager
import pytest
from conftest import make_due, replay_records
@@ -294,6 +295,25 @@ async def test_repeated_prepare_has_one_read_and_no_capture_write(
assert len(old.value["pending"]) == 5
+async def test_reply_lookup_searches_its_index_without_statistics(setup_kernel, monkeypatch):
+ # Stores are never ANALYZEd; scanning a user's records cost 20 ms at 1k sessions.
+ _, store, _, _ = setup_kernel
+ statements, connect = [], store.connect
+
+ @contextmanager
+ def traced():
+ with connect() as c:
+ c.set_trace_callback(statements.append)
+ yield c
+ c.set_trace_callback(None)
+
+ monkeypatch.setattr(store, "connect", traced)
+ await store.replay.replies("scope", ["a", "b"])
+ with connect() as c:
+ plan = [row[3] for row in c.execute("EXPLAIN QUERY PLAN " + statements[0])]
+ assert any("INDEX replay_kinds (scope=? AND kind=? AND anchor=?)" in step for step in plan)
+
+
async def test_hot_read_ignores_unrelated_operational_documents(
setup_kernel, credential, policy, monkeypatch
):