fix(agent): only strip _moa_prepared_request when the live client is not the MoA facade

The unconditional pop from the previous commit also stripped the key when
agent.client was still the real MoA facade, forcing the facade to re-prepare
from scratch — a duplicate reference fan-out per turn (caught by
test_moa_virtual_provider_aggregator_is_actor). Gate the defensive strip on
the same prepare()-capability probe used at the injection site so the
handshake survives on the facade while a swapped-in native client is still
protected.
This commit is contained in:
Teknium
2026-08-14 20:57:44 -07:00
parent b2113202be
commit 8369170fea
+8 -4
View File
@@ -910,10 +910,14 @@ def _dispatch_nonstreaming_api_request(agent, api_kwargs: dict, *, make_client):
# dead-connection cleanup / fallback+restore), agent.client may
# become a native OpenAI client while agent.provider stays
# "moa". Pop the MoA-internal key so the native SDK does not
# reject it as an unexpected kwarg. The MoAClient facade
# already handles a missing key by falling through to its
# normal resolution path. (#78382)
api_kwargs.pop("_moa_prepared_request", None)
# reject it as an unexpected kwarg — but only when the live
# client is NOT the facade: the facade consumes the key, and
# stripping it there forces a wasteful duplicate reference
# fan-out (the facade re-prepares from scratch). Only the MoA
# facade's completions object exposes ``prepare()``. (#78382)
_completions = getattr(getattr(agent.client, "chat", None), "completions", None)
if not callable(getattr(_completions, "prepare", None)):
api_kwargs.pop("_moa_prepared_request", None)
return agent.client.chat.completions.create(**api_kwargs)
request_client = make_client("chat_completion_request")
return request_client.chat.completions.create(**api_kwargs)