refactor(agent/relay): unmanaged __next__ sentinel exit; codec baseline/_codec flattened; register_subagent turn check relies on active_turn invariant; small ctor/lookup collapses

This commit is contained in:
Teknium
2026-09-02 22:46:46 -07:00
parent 9b669ca507
commit feed17bd75
2 changed files with 28 additions and 54 deletions
+10 -21
View File
@@ -235,12 +235,9 @@ def stream_current(
completed_response_predicate=completed_response_predicate,
)
if completed_response_predicate is not None:
# Relay may defer the provider callback until the first pull; prime once so a
# completed response surfaces (a real first chunk is buffered).
# Relay may defer the provider callback until the first pull; prime once (a real first chunk is buffered).
managed._prime_completed_response()
if managed.final_response is not None:
return managed.final_response
return managed
return managed.final_response if managed.final_response is not None else managed
def _aclose_on_loop(loop: asyncio.AbstractEventLoop, stream: Any) -> None:
@@ -421,12 +418,8 @@ class ManagedLlmStream(Iterator[Any]):
if self._prefetched_chunks:
return self._prefetched_chunks.pop()
if self._loop is None:
try:
chunk = next(self._stream)
except StopIteration:
self._close(logical_outcome="cancelled")
raise
if self._accept_chunk is not None and not self._accept_chunk(chunk):
chunk = next(self._stream, self) # self: exhausted sentinel
if chunk is self or (self._accept_chunk is not None and not self._accept_chunk(chunk)):
self._close(logical_outcome="cancelled")
raise StopIteration
return chunk
@@ -596,10 +589,9 @@ class AnthropicStreamAccumulator:
for block in blocks:
partial = block.pop("_partial_json", None)
if partial is not None:
try:
block["input"] = json.loads(partial)
except (TypeError, ValueError):
block["input"] = partial
with contextlib.suppress(TypeError, ValueError):
partial = json.loads(partial)
block["input"] = partial
return {**self._message, "content": blocks}
def response(self, base: Any = None) -> Any:
@@ -788,11 +780,11 @@ def _codec_round_trip_request_body(
try:
encoded = codec.encode(codec.decode(relay_request), relay_request)
content = getattr(encoded, "content", encoded)
if isinstance(content, dict):
return _provider_request_body(content, metadata)
except Exception:
logger.warning("NeMo Relay request codec baseline failed; ignoring request rewrites", exc_info=True)
return None
if isinstance(content, dict):
return _provider_request_body(content, metadata)
logger.warning("NeMo Relay request codec returned an unsupported baseline; ignoring request rewrites")
return None
@@ -809,10 +801,7 @@ def _provider_request_body(content: dict[str, Any], metadata: dict[str, Any] | N
def _codec(relay: Any, metadata: dict[str, Any] | None) -> Any:
protocol = _RELAY_PROTOCOL_BY_API_MODE.get(_api_mode(metadata))
codecs = getattr(relay, "codecs", None)
if protocol is None or codecs is None:
return None
codec = getattr(codecs, protocol[1], None)
codec = getattr(getattr(relay, "codecs", None), protocol[1], None) if protocol is not None else None
return codec() if callable(codec) else None
+18 -33
View File
@@ -100,8 +100,7 @@ def pop_relay_scope(relay: Any, handle: Any, *, output: Any = None, metadata: An
"""Pop a Relay scope, forwarding only the kwargs the live binding accepts.
``scope.pop`` gained ``metadata`` in nemo-relay 0.4+; older wheels raise TypeError."""
pop = relay.scope.pop
candidates = (("output", output), ("metadata", metadata), ("timestamp", timestamp))
kwargs = {key: value for key, value in candidates if value is not None}
kwargs = {k: v for k, v in (("output", output), ("metadata", metadata), ("timestamp", timestamp)) if v is not None}
try:
params = inspect.signature(pop).parameters
except (TypeError, ValueError):
@@ -180,8 +179,7 @@ class RelayOperationLease:
"""Keep process-wide Relay plugins alive across a deferred operation."""
def __init__(self, runtime: "RelayRuntime") -> None:
self._lock = threading.Lock()
self._runtime: RelayRuntime | None = runtime
self._lock, self._runtime = threading.Lock(), runtime
def run_in_session(self, session: RelaySession, callback: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
"""Run cleanup while this lease still owns the runtime lifetime."""
@@ -406,9 +404,9 @@ class RelayRuntime:
return None
session = self._sessions.get(session_id)
if session is None:
parent_session_id = self._subagent_parents.get(session_id, "")
session = RelaySession(session_id=session_id, parent_session_id=parent_session_id)
self._sessions[session_id] = session
session = self._sessions[session_id] = RelaySession(
session_id=session_id, parent_session_id=self._subagent_parents.get(session_id, "")
)
with session.lock:
if session.closing:
return None
@@ -461,12 +459,8 @@ class RelayRuntime:
return None
parent = self.ensure_session({"session_id": parent_session_id})
parent_handle = None if parent is None else parent.handle
turn = active_turn(parent_session_id)
# active_turn() already proved liveness and, for a RelayRuntime host, an open session.
if (
turn is not None and turn.handle is not None and turn.lease.host is self
and turn.lease.session.session_id == parent_session_id
):
turn = active_turn(parent_session_id) # already proved liveness and (RelayRuntime host) an open session
if turn is not None and turn.handle is not None and turn.lease.host is self:
parent_handle = turn.handle
with self._sessions_lock:
if self._closing:
@@ -503,14 +497,13 @@ class RelayRuntime:
return None if session.closing else session
def _session_context(self, session: RelaySession, *, allow_closing: bool) -> contextvars.Context:
"""Copy the current context and overlay the session's saved Relay vars."""
"""Copy the current context and overlay the session's saved Relay vars (a copy: re-entrant from callbacks)."""
with session.lock:
if session.closing and not allow_closing:
raise RuntimeError("Hermes Relay session is closing")
if session.context is None or session.handle is None:
raise RuntimeError("Hermes Relay session context is unavailable")
relay_context = session.context.copy()
# A copy lets a helper inside a Relay callback re-enter the session's Context.
context = contextvars.copy_context()
for variable, value in relay_context.items():
context.run(variable.set, value)
@@ -606,9 +599,8 @@ class RelayRuntime:
def apply_tool_request_intercepts(self, *, session_id: str, tool_name: str, args: dict[str, Any]) -> dict[str, Any]:
"""Apply Relay request rewriting before Hermes authorizes a tool call."""
request_intercepts = getattr(getattr(self.relay, "tools", None), "request_intercepts", None)
if not self.managed_execution_enabled() or not callable(request_intercepts):
return args
session = self.ensure_session({"session_id": session_id})
managed = self.managed_execution_enabled() and callable(request_intercepts)
session = self.ensure_session({"session_id": session_id}) if managed else None
if session is None:
return args
result = self.run_in_session(session, request_intercepts, tool_name, args)
@@ -784,8 +776,7 @@ class RelayHostRegistry:
def shutdown_all(self) -> None:
with self._lock:
hosts = list(self._hosts.values())
self._hosts.clear()
hosts, self._hosts = list(self._hosts.values()), {}
for host in hosts:
host.shutdown()
@@ -904,9 +895,7 @@ class RelaySessionCoordinator:
def acquire_conversation(
self, *, profile_key: str, session_id: str, platform: str, parent_session_id: str = "", model: str = "",
) -> ConversationLease:
host = self.registry.for_profile(profile_key) or NoopRelayRuntime(
profile_key, "Relay host creation was disabled"
)
host = self.registry.for_profile(profile_key) or NoopRelayRuntime(profile_key, "Relay host creation was disabled")
session = None
if isinstance(host, RelayRuntime):
context = {
@@ -924,9 +913,8 @@ class RelaySessionCoordinator:
session_id, parent_session_id = context["session_id"], context["parent_session_id"]
metadata = {"hermes.execution_surface": context["platform"] or "unknown"}
if parent_session_id and parent_session_id != session_id:
return host.register_subagent(
{"parent_session_id": parent_session_id, "child_session_id": session_id}, metadata=metadata,
)
event = {"parent_session_id": parent_session_id, "child_session_id": session_id}
return host.register_subagent(event, metadata=metadata)
return host.ensure_session({"session_id": session_id}, metadata=metadata)
def begin_turn(self, lease: ConversationLease, *, turn_id: str, task_id: str) -> RelayTurnContext:
@@ -1191,9 +1179,7 @@ def current_profile_key() -> str:
home = get_hermes_home().expanduser()
if not home.is_absolute():
return str(home.resolve())
raw = str(home)
cached = _PROFILE_KEY_CACHE.get(raw)
return cached if cached is not None else _PROFILE_KEY_CACHE.setdefault(raw, str(home.resolve()))
return _PROFILE_KEY_CACHE.get(str(home)) or _PROFILE_KEY_CACHE.setdefault(str(home), str(home.resolve()))
def _load_nemo_relay() -> Any:
@@ -1230,11 +1216,10 @@ def _resolve_plugin_awaitable(value: Any) -> Any:
"""Resolve Relay's async plugin API from synchronous host construction."""
if not inspect.isawaitable(value):
return value
try:
with contextlib.suppress(RuntimeError):
asyncio.get_running_loop()
except RuntimeError:
return asyncio.run(value)
return _run_on_daemon_thread(lambda: asyncio.run(value), name="hermes-nemo-relay-plugin-lifecycle")
return _run_on_daemon_thread(lambda: asyncio.run(value), name="hermes-nemo-relay-plugin-lifecycle")
return asyncio.run(value)
def _session_id(event: dict[str, Any]) -> str: