fix(runtime): close failed auxiliary Relay calls

Signed-off-by: Alex Fournier <afournier@nvidia.com>
This commit is contained in:
Alex Fournier 2026-07-21 09:39:24 -07:00
parent df892a08f6
commit 82d923ad5a
2 changed files with 126 additions and 3 deletions

View file

@ -2359,6 +2359,9 @@ def _relay_auxiliary_call(callback):
})
try:
return callback(*args, **kwargs)
except BaseException:
_fail_relay_auxiliary_call()
raise
finally:
_RELAY_AUX_CALL_CONTEXT.reset(token)
@ -2381,6 +2384,9 @@ def _relay_auxiliary_call_async(callback):
})
try:
return await callback(*args, **kwargs)
except BaseException:
_fail_relay_auxiliary_call()
raise
finally:
_RELAY_AUX_CALL_CONTEXT.reset(token)
@ -7033,8 +7039,8 @@ def _validate_llm_response(
return response
def _complete_relay_auxiliary_call() -> None:
"""Close one auxiliary logical call only after Hermes accepts its response."""
def _complete_relay_auxiliary_call(*, outcome: str = "success") -> None:
"""Close one auxiliary logical call after acceptance or terminal failure."""
context = _RELAY_AUX_CALL_CONTEXT.get()
if context is None:
return
@ -7042,10 +7048,21 @@ def _complete_relay_auxiliary_call() -> None:
relay_llm.complete_logical_call(
str(context.get("request_id") or ""),
outcome="success",
outcome=outcome,
)
def _fail_relay_auxiliary_call() -> None:
"""Close a terminally failed call without replacing its original error."""
try:
_complete_relay_auxiliary_call(outcome="failed")
except Exception:
logger.warning(
"Relay auxiliary failure finalization failed",
exc_info=True,
)
def _recover_aux_response_message(response: Any) -> Optional[Any]:
"""Synthesize chat-completions shape from Responses-style text fields.

View file

@ -152,6 +152,112 @@ async def test_async_auxiliary_attempt_uses_inherited_relay_adapter(monkeypatch)
]
def test_terminal_auxiliary_failure_stays_failed_when_caller_catches_it(
relay_turn, monkeypatch
):
_relay, turn = relay_turn
consumer = "test.terminal-auxiliary-failure"
turn.lease.host.retain_managed_execution(consumer)
outcomes = []
original_pop = turn.lease.host.relay.scope.pop
def record_pop(*args, **kwargs):
outcomes.append((kwargs.get("output") or {}).get("outcome"))
return original_pop(*args, **kwargs)
monkeypatch.setattr(turn.lease.host.relay.scope, "pop", record_pop)
client = SimpleNamespace(
chat=SimpleNamespace(
completions=SimpleNamespace(
create=lambda **_kwargs: SimpleNamespace(choices=[]),
)
)
)
@auxiliary_client._relay_auxiliary_call
def run(task):
auxiliary_client._set_relay_auxiliary_route(
"openrouter",
"test-model",
"chat_completions",
)
with pytest.raises(RuntimeError, match="invalid response"):
auxiliary_client._validate_llm_response(
auxiliary_client._relay_sync_completion(
client,
{"model": "test-model", "messages": []},
),
task,
)
assert len(turn.logical_llm_calls) == 1
return auxiliary_client._validate_llm_response(
auxiliary_client._relay_sync_completion(
client,
{"model": "test-model", "messages": []},
),
task,
)
try:
with pytest.raises(RuntimeError, match="invalid response"):
run("compression")
assert outcomes == ["failed"]
assert turn.logical_llm_calls == {}
relay_runtime.SESSION_COORDINATOR.end_turn(turn, outcome="success")
assert outcomes == ["failed", "success"]
finally:
turn.lease.host.release_managed_execution(consumer)
@pytest.mark.asyncio
async def test_async_terminal_auxiliary_failure_closes_logical_call(relay_turn):
_relay, turn = relay_turn
consumer = "test.async-terminal-auxiliary-failure"
turn.lease.host.retain_managed_execution(consumer)
async def create(**_kwargs):
return SimpleNamespace(choices=[])
client = SimpleNamespace(
chat=SimpleNamespace(completions=SimpleNamespace(create=create))
)
@auxiliary_client._relay_auxiliary_call_async
async def run(task):
auxiliary_client._set_relay_auxiliary_route(
"anthropic",
"claude-test",
"chat_completions",
)
with pytest.raises(RuntimeError, match="invalid response"):
auxiliary_client._validate_llm_response(
await auxiliary_client._relay_async_completion(
client,
{"model": "claude-test", "messages": []},
),
task,
)
assert len(turn.logical_llm_calls) == 1
return auxiliary_client._validate_llm_response(
await auxiliary_client._relay_async_completion(
client,
{"model": "claude-test", "messages": []},
),
task,
)
try:
with pytest.raises(RuntimeError, match="invalid response"):
await run("title_generation")
assert turn.logical_llm_calls == {}
finally:
turn.lease.host.release_managed_execution(consumer)
def test_auxiliary_stream_uses_streaming_relay_primitive(monkeypatch):
captured = {}
raw_stream = iter([{"delta": "one"}, {"delta": "two"}])