The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(agent/relay_llm): drop dead _relay_observes_chunks branch; _ManagedAttempt._complete; fold stream_current fast paths
Tekniumec3ad0c7e656
diff --git a/agent/relay_llm.py b/agent/relay_llm.pyindex a0875b497b9..b48ba40d246 100644--- a/agent/relay_llm.py+++ b/agent/relay_llm.py@@ -142,25 +142,27 @@ class _ManagedAttempt:"""Re-raise the provider's own error, or recover a completed provider result.Must be called from the ``except`` handling ``exc`` (bare ``raise``)."""callback_error = self.raw_response.get("error")- if (callback_error is not None and relay_runtime._is_relay_wrapped_callback_error(exc, callback_error)):+ if callback_error is not None and relay_runtime._is_relay_wrapped_callback_error(exc, callback_error):raise callback_error- if (not isinstance(exc, Exception) or callback_error is not None or "value" not in self.raw_response):+ if not isinstance(exc, Exception) or callback_error is not None or "value" not in self.raw_response:raiselogger.warning("NeMo Relay LLM post-processing failed after provider success; returning the provider response",exc_info=True,)- if not defer_logical_completion:- _complete_logical(self.logical, outcome="success")+ self._complete(defer_logical_completion)return self.raw_response["value"]def result(self, managed: Any, defer_logical_completion: bool) -> Any:- if not defer_logical_completion:- _complete_logical(self.logical, outcome="success")+ self._complete(defer_logical_completion)if "value" in self.raw_response and _json_equal(managed, self.raw_response["json"]):return self.raw_response["value"]return _namespace(managed)+ def _complete(self, defer_logical_completion: bool) -> None:+ if not defer_logical_completion:+ _complete_logical(self.logical, outcome="success")+def execute(request: dict[str, Any], callback: Callable[[dict[str, Any]], Any], *, session_id: str, name: str,@@ -242,12 +244,10 @@ def stream_current(pipeline: a genuine first chunk is buffered, but provider latency and pre-first-yielderrors may surface before this returns."""session_id = _current_session_id()- if session_id is None:- return stream_factory(request)- if _has_running_event_loop():- # We are on the Relay session's loop (inside a managed callback): a nested- # ManagedLlmStream would be iterated synchronously on that loop, which asyncio- # forbids. The outer managed stream already tracks this attempt.+ # On the Relay session's loop (inside a managed callback) a nested ManagedLlmStream would+ # be iterated synchronously on that loop, which asyncio forbids; the outer managed stream+ # already tracks this attempt.+ if session_id is None or _has_running_event_loop():return stream_factory(request)managed = stream(request, stream_factory, session_id=session_id, name=name, model_name=model_name,@@ -289,7 +289,6 @@ class ManagedLlmStream(Iterator[Any]):_callback_error: BaseException | None = None_logical: _LogicalCall | None = None_logical_response_model_name: str | None = None- _relay_observes_chunks = False_provider_completed = Falsedef __init__(@@ -400,7 +399,6 @@ class ManagedLlmStream(Iterator[Any]):self._release_runtime_lease()raiseself._loop = loop- self._relay_observes_chunks = Truetry:self._stream = loop.run_until_complete(attempt.run_managed(@@ -482,7 +480,7 @@ class ManagedLlmStream(Iterator[Any]):raise StopIteration from Noneexcept BaseException as exc:callback_error = self._callback_error- if (callback_error is not None and relay_runtime._is_relay_wrapped_callback_error(exc, callback_error)):+ if callback_error is not None and relay_runtime._is_relay_wrapped_callback_error(exc, callback_error):self._close(logical_outcome="failed")raise callback_errorif self._recoverable_relay_failure(exc):@@ -490,8 +488,6 @@ class ManagedLlmStream(Iterator[Any]):return next(self)self._close(logical_outcome="cancelled" if _is_cancellation(exc) else "failed")raise- if not self._relay_observes_chunks and self._on_chunk is not None:- self._on_chunk(chunk)for index, (encoded, raw) in enumerate(self._raw_chunks):if _json_equal(chunk, encoded):if index > 0:diff --git a/agent/relay_tools.py b/agent/relay_tools.pyindex 68f16f92d6a..28bb3ebb3bd 100644--- a/agent/relay_tools.py+++ b/agent/relay_tools.py@@ -54,7 +54,7 @@ def execute())except BaseException as exc:- if (callback_error is not None and relay_runtime._is_relay_wrapped_callback_error(exc, callback_error)):+ if callback_error is not None and relay_runtime._is_relay_wrapped_callback_error(exc, callback_error):raise callback_errorif isinstance(exc, Exception) and callback_error is None and "value" in raw_result:logger.warning(