From 8369170feafa66b4c3e883d5158ae6ef30cd587c Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Fri, 14 Aug 2026 20:57:44 -0700 Subject: [PATCH] fix(agent): only strip _moa_prepared_request when the live client is not the MoA facade MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- agent/chat_completion_helpers.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py index cc357f20c8..b1ef005713 100644 --- a/agent/chat_completion_helpers.py +++ b/agent/chat_completion_helpers.py @@ -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)