Skip to content

The agent that grows with you

fix(moa): unwrap completed responses in the auxiliary streaming path

AKAZIK-pyada3663ce72f
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index 96dce03fae9..4f8ee48de52 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -2664,6 +2664,7 @@ def _relay_sync_stream(
model_name=str(kwargs.get("model") or fallback_model),
finalizer=dict,
metadata=metadata,
+ completed_response_predicate=lambda value: hasattr(value, "choices"),
)
_RUNTIME_MAIN_COMPAT_SNAPSHOT: Tuple[Any, ...] = ("", "", "", "", "", "")
_RUNTIME_MAIN_COMPAT_LOCK = threading.Lock()
diff --git a/agent/relay_llm.py b/agent/relay_llm.py
index 3481ca07237..2d3a2883e17 100644
--- a/agent/relay_llm.py
+++ b/agent/relay_llm.py
@@ -256,12 +256,23 @@ def stream_current(
finalizer: Callable[[], Any],
metadata: dict[str, Any] | None = None,
defer_logical_completion: bool = False,
+ completed_response_predicate: Callable[[Any], bool] | None = None,
) -> Any:
- """Run a provider stream under the inherited Hermes turn when present."""
+ """Run a provider stream under the inherited Hermes turn when present.
+
+ When ``completed_response_predicate`` is set and the stream_factory returns
+ a complete response instead of an iterator (e.g. AnthropicAuxiliaryClient
+ and other shims that ignore ``stream=True``), unwrap and return the
+ completed response directly. This mirrors the pre-Relay behavior where
+ ``call_llm(stream=True)`` returned the raw response and the consumer's
+ own ``hasattr(stream, "choices")`` check handled it (#11732, #55933) —
+ without the unwrap the response stays trapped as ``final_response`` on the
+ inner ManagedLlmStream and the outer consumer sees an empty stream.
+ """
turn = relay_runtime.active_turn()
if turn is None:
return stream_factory(request)
- return stream(
+ managed = stream(
request,
stream_factory,
session_id=turn.lease.session_id,
@@ -270,7 +281,17 @@ def stream_current(
finalizer=finalizer,
metadata=metadata,
defer_logical_completion=defer_logical_completion,
+ completed_response_predicate=completed_response_predicate,
)
+ # In the non-managed path the factory already ran eagerly during __init__,
+ # so a completed response is visible immediately and must surface raw.
+ # In the managed path the factory runs lazily on first pull, so
+ # final_response is still None here and the managed stream is returned.
+ if completed_response_predicate is not None:
+ completed = getattr(managed, "final_response", None)
+ if completed is not None:
+ return completed
+ return managed
def stream(
diff --git a/tests/agent/test_auxiliary_relay.py b/tests/agent/test_auxiliary_relay.py
index 5d6bb629d40..dd18782f0b2 100644
--- a/tests/agent/test_auxiliary_relay.py
+++ b/tests/agent/test_auxiliary_relay.py
@@ -260,3 +260,41 @@ def test_partial_auxiliary_stream_failure_closes_before_recovery(
turn.lease.host.release_managed_execution(consumer)
+
+
+def test_auxiliary_stream_unwraps_completed_response(relay_turn):
+ """MoA aggregator on an Anthropic-protocol provider: the client returns a
+ completed response for ``stream=True`` (the adapter ignores the flag), so
+ ``_relay_sync_stream`` must surface it raw for the consumer's
+ ``hasattr(stream, "choices")`` handling — regression of #11732/#55933 via
+ the Relay integration (SimpleNamespace is not iterable)."""
+ _relay, _turn = relay_turn
+ completed = SimpleNamespace(
+ choices=[
+ SimpleNamespace(
+ message=SimpleNamespace(content="aggregated"),
+ finish_reason="stop",
+ )
+ ],
+ model="kimi-k3",
+ )
+ client = SimpleNamespace(
+ chat=SimpleNamespace(
+ completions=SimpleNamespace(create=lambda **_kwargs: completed)
+ )
+ )
+
+ @auxiliary_client._relay_auxiliary_call
+ def run(task):
+ auxiliary_client._set_relay_auxiliary_route(
+ "kimi-coding",
+ "kimi-k3",
+ "chat_completions",
+ )
+ return auxiliary_client._relay_sync_stream(
+ client,
+ {"model": "kimi-k3", "messages": [], "stream": True},
+ )
+
+ assert run("moa_aggregator") is completed
+
diff --git a/tests/agent/test_relay_llm.py b/tests/agent/test_relay_llm.py
index 67acabc7fb0..e506fad1e88 100644
--- a/tests/agent/test_relay_llm.py
+++ b/tests/agent/test_relay_llm.py
@@ -714,3 +714,84 @@ def test_codec_baseline_failure_is_explicit(relay_turn, monkeypatch, caplog):
assert "ignoring request rewrites" in caplog.text
+def test_stream_current_unwraps_completed_response(tmp_path, monkeypatch):
+ """Auxiliary streaming (the MoA aggregator) must surface a completed
+ provider response raw instead of crashing when the client ignores
+ ``stream=True`` and returns a response object (AnthropicAuxiliaryClient
+ and other OpenAI-compatible shims).
+
+ Pre-Relay, ``call_llm(stream=True)`` returned the raw response and the
+ consumer's ``hasattr(stream, "choices")`` check handled it (#11732,
+ #55933). The Relay integration wrapped the call in a ManagedLlmStream
+ without threading ``completed_response_predicate``, regressing that path
+ into ``TypeError: 'types.SimpleNamespace' object is not iterable``.
+ """
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "profile"))
+ relay_runtime._reset_for_tests()
+ lease = relay_runtime.SESSION_COORDINATOR.acquire_conversation(
+ profile_key=relay_runtime.current_profile_key(),
+ session_id="session-moa",
+ platform="cli",
+ )
+ turn = relay_runtime.SESSION_COORDINATOR.begin_turn(
+ lease,
+ turn_id="turn-moa",
+ task_id="task-moa",
+ )
+ try:
+ completed = SimpleNamespace(
+ choices=[
+ SimpleNamespace(
+ message=SimpleNamespace(content="done"),
+ finish_reason="stop",
+ )
+ ],
+ model="kimi-k3",
+ )
+ result = relay_llm.stream_current(
+ {"model": "kimi-k3", "stream": True},
+ lambda request: completed,
+ name="kimi-coding",
+ model_name="kimi-k3",
+ finalizer=dict,
+ completed_response_predicate=lambda value: hasattr(value, "choices"),
+ )
+ # Unwrapped raw response — NOT a stream wrapper whose iteration would
+ # have raised TypeError pre-fix.
+ assert result is completed
+ finally:
+ relay_runtime.SESSION_COORDINATOR.end_turn(turn, outcome="success")
+ relay_runtime.SESSION_COORDINATOR.release_conversation(lease)
+ relay_runtime._reset_for_tests()
+
+
+def test_stream_current_streams_iterators_with_predicate(tmp_path, monkeypatch):
+ """A genuine chunk iterator still flows through as a stream when the
+ completed-response predicate is supplied."""
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "profile"))
+ relay_runtime._reset_for_tests()
+ lease = relay_runtime.SESSION_COORDINATOR.acquire_conversation(
+ profile_key=relay_runtime.current_profile_key(),
+ session_id="session-moa",
+ platform="cli",
+ )
+ turn = relay_runtime.SESSION_COORDINATOR.begin_turn(
+ lease,
+ turn_id="turn-moa",
+ task_id="task-moa",
+ )
+ try:
+ result = relay_llm.stream_current(
+ {"model": "m", "stream": True},
+ lambda request: iter([{"delta": "a"}, {"delta": "b"}]),
+ name="provider",
+ model_name="m",
+ finalizer=dict,
+ completed_response_predicate=lambda value: hasattr(value, "choices"),
+ )
+ assert list(result) == [{"delta": "a"}, {"delta": "b"}]
+ finally:
+ relay_runtime.SESSION_COORDINATOR.end_turn(turn, outcome="success")
+ relay_runtime.SESSION_COORDINATOR.release_conversation(lease)
+ relay_runtime._reset_for_tests()
+