Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

fix(providers): drop streamed server-tool continuation chunks; end the turn when only server calls remain

Rufus8fad2da20d59
diff --git a/packages/core/openexecutive/architecture/architecture-facts.yaml b/packages/core/openexecutive/architecture/architecture-facts.yaml
index bf42a067..0323266a 100644
--- a/packages/core/openexecutive/architecture/architecture-facts.yaml
+++ b/packages/core/openexecutive/architecture/architecture-facts.yaml
@@ -132,10 +132,12 @@ caching:
is preserved by replacing the Anthropic web_search server tool with
OpenRouter's own ``openrouter:web_search`` server tool (see
providers.translator_shapes), so a research specialist or the
- Executive pinned to e.g. Gemini still searches, and with the SAME
- contract as Anthropic-direct: the model decides when to search,
- ``max_uses`` caps searches server-side (so RESEARCH_WEB_SEARCH_MAX_USES
- and WEB_SEARCH_MAX_USES mean the same thing on both deployment types),
+ Executive pinned to e.g. Gemini still searches, with the same
+ per-request contract as Anthropic-direct: the model decides when to
+ search, ``max_uses`` caps searches server-side per request (so
+ RESEARCH_WEB_SEARCH_MAX_USES and WEB_SEARCH_MAX_USES mean the same
+ thing on both deployment types; on either path a multi-step chat turn
+ gets the cap afresh on each step),
WEB_SEARCH_ALLOWED_DOMAINS / BLOCKED_DOMAINS map to allowed_domains /
excluded_domains, and the response's ``usage.web_search_requests`` is
surfaced as ``server_tool_use`` so the usage rows record searches on
@@ -225,8 +227,15 @@ providers:
via OpenRouter, Claude or not — without this a call routed
via OpenRouter would silently lose web search (the executive_research
council emitted zero findings). Server-side tool calls in the response
- (``tool_calls[].type != "function"``) are skipped, never surfaced as
- tool_use blocks for the caller to run. OpenRouter search makes the model cite sources
+ (``tool_calls[].type`` starting with ``openrouter:``, including the
+ untyped continuation chunks of a streamed call) are skipped, never
+ surfaced as tool_use blocks for the caller to run; a response whose
+ only tool calls were server-side ends the turn (``end_turn``) rather
+ than waiting on tool results. Unlike the Anthropic path, which echoes
+ ``server_tool_use`` / ``web_search_tool_result`` blocks back into the
+ next step's history, the OpenRouter path carries only the model's own
+ text forward, so across a multi-step chat turn it may search again for
+ what it already found. OpenRouter search makes the model cite sources
with inline ``<cite index="...">…</cite>`` markup; the response path
strips those wrapper tags from both text and tool-call arguments so they
don't leak into findings / chat replies. Streaming SSE chunks
diff --git a/packages/core/openexecutive/providers/translator.py b/packages/core/openexecutive/providers/translator.py
index bae4533c..d18d12fc 100644
--- a/packages/core/openexecutive/providers/translator.py
+++ b/packages/core/openexecutive/providers/translator.py
@@ -303,6 +303,15 @@ _WEB_SEARCH_TOOL_PREFIX = "web_search_"
# everything else Exa. See
# https://openrouter.ai/docs/guides/features/server-tools/web-search.
_OPENROUTER_WEB_SEARCH_TYPE = "openrouter:web_search"
+# Tool-call ``type`` prefix of OpenRouter's server-side tools in a response.
+# Those calls were executed upstream; they are never surfaced as tool_use
+# blocks for the caller to run.
+_OPENROUTER_SERVER_TOOL_PREFIX = "openrouter:"
+
+
+def _is_server_tool_call(call: dict[str, Any]) -> bool:
+ call_type = call.get("type")
+ return isinstance(call_type, str) and call_type.startswith(_OPENROUTER_SERVER_TOOL_PREFIX)
def _web_search_server_tool(tools: Any) -> dict[str, Any] | None:
@@ -619,10 +628,13 @@ def from_openai_response(body: dict[str, Any]) -> SimpleNamespace:
if isinstance(text, str) and text:
content_blocks.append(_block("text", text=_strip_cite_markup(text)))
+ skipped_server_calls = 0
for call in msg.get("tool_calls") or []:
- if call.get("type") not in (None, "function"):
- # A server-side tool call (OpenRouter executed it already) is not
- # for the caller to run.
+ if _is_server_tool_call(call):
+ # OpenRouter executed it already (web search); not for the
+ # caller to run.
+ skipped_server_calls += 1
+ logger.debug("openrouter: skipping server-side tool call %r", call.get("type"))
continue
fn = call.get("function") or {}
raw_args = fn.get("arguments") or "{}"
@@ -635,6 +647,11 @@ def from_openai_response(body: dict[str, Any]) -> SimpleNamespace:
input=_strip_cite_in_value(parsed),
)
)
+ stop_reason = _stop_reason_from_openai(choice.get("finish_reason"))
+ if skipped_server_calls and not any(b.type == "tool_use" for b in content_blocks):
+ # Every tool call was server-side: nothing is left for the caller
+ # to run, so the turn is complete, not waiting on tool results.
+ stop_reason = "end_turn"
# Reasoning LAST. Several callers read ``content[0].text`` (the SDK
# itself only ever emits text first for those prompts), so the synthetic
@@ -652,7 +669,7 @@ def from_openai_response(body: dict[str, Any]) -> SimpleNamespace:
role="assistant",
model=body.get("model", ""),
content=content_blocks,
- stop_reason=_stop_reason_from_openai(choice.get("finish_reason")),
+ stop_reason=stop_reason,
stop_sequence=None,
usage=SimpleNamespace(
input_tokens=usage.get("prompt_tokens", 0),
@@ -737,6 +754,10 @@ class StreamAccumulator:
self._cite_pending = ""
# tool_calls[idx] = {"id": ..., "name": ..., "arg_chunks": [..]}
self._tool_calls: dict[int, dict[str, Any]] = {}
+ # Indices of server-side tool calls (typed only on their first
+ # delta); their continuation chunks carry only index + arguments
+ # and must be dropped too.
+ self._server_tool_indices: set[int] = set()
# OpenRouter streams ``delta.reasoning_details`` chunks; collected
# verbatim and re-emitted as one block at finalize().
self._reasoning_details: list[Any] = []
@@ -796,9 +817,13 @@ class StreamAccumulator:
self._reasoning_details.extend(details)
for tc in delta.get("tool_calls") or []:
- if tc.get("type") not in (None, "function"):
- continue # server-side tool call, already executed upstream
idx = tc.get("index", 0)
+ if _is_server_tool_call(tc):
+ self._server_tool_indices.add(idx)
+ logger.debug("openrouter: skipping server-side tool call %r", tc.get("type"))
+ continue
+ if idx in self._server_tool_indices:
+ continue # continuation chunk of a server-side call
slot = self._tool_calls.setdefault(
idx, {"id": "", "name": "", "arg_chunks": []}
)
@@ -864,7 +889,11 @@ class StreamAccumulator:
role="assistant",
model=self._model,
content=content_blocks,
- stop_reason=_stop_reason_from_openai(self._finish_reason),
+ stop_reason=(
+ "end_turn"
+ if self._server_tool_indices and not self._tool_calls
+ else _stop_reason_from_openai(self._finish_reason)
+ ),
stop_sequence=None,
usage=SimpleNamespace(
input_tokens=self._usage.get("prompt_tokens", 0),
diff --git a/packages/core/tests/unit/test_openrouter_translator.py b/packages/core/tests/unit/test_openrouter_translator.py
index 3c4abf31..8d776a91 100644
--- a/packages/core/tests/unit/test_openrouter_translator.py
+++ b/packages/core/tests/unit/test_openrouter_translator.py
@@ -341,6 +341,34 @@ def test_response_reports_search_count_and_skips_server_tool_calls() -> None:
})
assert msg.usage.server_tool_use.web_search_requests == 3
assert [b.name for b in msg.content if b.type == "tool_use"] == ["emit_research_findings"]
+ assert msg.stop_reason == "tool_use" # a client tool call remains
+
+ # Only server-side calls: nothing is left for the caller to run, so the
+ # turn is complete — not "tool_use" with no tool blocks (that would make
+ # the Executive loop append an empty tool-result turn and spin).
+ only_server = from_openai_response({
+ "id": "x", "model": "m",
+ "choices": [{"finish_reason": "tool_calls", "message": {
+ "role": "assistant", "content": "searched and answered",
+ "tool_calls": [{"id": "s1", "type": "openrouter:web_search",
+ "function": {"name": "web_search", "arguments": "{}"}}],
+ }}],
+ "usage": {"prompt_tokens": 1, "completion_tokens": 1, "web_search_requests": 1},
+ })
+ assert only_server.stop_reason == "end_turn"
+ assert [b.type for b in only_server.content] == ["text"]
+
+ # An ordinary function call without a `type` field (quirky backends)
+ # is still a client tool call.
+ untyped = from_openai_response({
+ "id": "x", "model": "m",
+ "choices": [{"finish_reason": "tool_calls", "message": {
+ "role": "assistant", "content": None,
+ "tool_calls": [{"id": "c9", "function": {"name": "emit", "arguments": "{}"}}],
+ }}],
+ "usage": {"prompt_tokens": 1, "completion_tokens": 1},
+ })
+ assert [b.name for b in untyped.content if b.type == "tool_use"] == ["emit"]
# Absent → 0, never missing (audit usage reads it on every row).
plain = from_openai_response({
"id": "y", "model": "m",
@@ -1032,3 +1060,35 @@ def test_stream_accumulator_reports_search_count() -> None:
"usage": {"prompt_tokens": 5, "completion_tokens": 1, "web_search_requests": 2}})
msg = acc.finalize()
assert msg.usage.server_tool_use.web_search_requests == 2
+
+
+def test_stream_accumulator_drops_server_tool_call_continuation_chunks() -> None:
+ """A server-side call is typed only on its first delta; the argument
+ chunks that follow carry just index + arguments and must be dropped with
+ it, or a phantom tool_use with an empty id and name would be emitted."""
+ acc = StreamAccumulator()
+ acc.feed({"id": "s", "model": "m", "choices": [{"delta": {"tool_calls": [
+ {"index": 0, "id": "srv_1", "type": "openrouter:web_search",
+ "function": {"name": "web_search", "arguments": ""}}]}, "finish_reason": None}]})
+ acc.feed({"id": "s", "model": "m", "choices": [{"delta": {"tool_calls": [
+ {"index": 0, "function": {"arguments": "{\"query\": \"acme layoffs\"}"}}]}, "finish_reason": None}]})
+ acc.feed({"id": "s", "model": "m", "choices": [{"delta": {"tool_calls": [
+ {"index": 1, "id": "c1", "type": "function",
+ "function": {"name": "consult_specialist", "arguments": "{\"q\": 1}"}}]}, "finish_reason": None}]})
+ acc.feed({"id": "s", "model": "m", "choices": [{"delta": {"content": "Acme cut staff."},
+ "finish_reason": "tool_calls"}]})
+ msg = acc.finalize()
+ tool_uses = [b for b in msg.content if b.type == "tool_use"]
+ assert [(b.id, b.name) for b in tool_uses] == [("c1", "consult_specialist")]
+ assert msg.stop_reason == "tool_use"
+
+ # Only a server-side call in the stream: the turn is complete.
+ acc2 = StreamAccumulator()
+ acc2.feed({"id": "s", "model": "m", "choices": [{"delta": {"tool_calls": [
+ {"index": 0, "id": "srv_1", "type": "openrouter:web_search",
+ "function": {"name": "web_search", "arguments": ""}}]}, "finish_reason": None}]})
+ acc2.feed({"id": "s", "model": "m", "choices": [{"delta": {"tool_calls": [
+ {"index": 0, "function": {"arguments": "{}"}}]}, "finish_reason": None}]})
+ acc2.feed({"id": "s", "model": "m", "choices": [{"delta": {"content": "done"}, "finish_reason": "tool_calls"}]})
+ final = acc2.finalize()
+ assert [b.type for b in final.content] == ["text"] and final.stop_reason == "end_turn"