[Core][Frontend] Bound duplex output delivery and reject cancelled audio (#7644)

Signed-off-by: Nolen Liang <nliang@nvidia.com>
Signed-off-by: bcsdhjew <nliang@nvidia.com>
This commit is contained in:
bcsdhjew authored and GitHub committed 2026-10-04 14:06:04 -07:00
1 parent 3f20252151
commit f781f7bb08
26 files changed
+1170 -147

No files matched your search

@@ -577,6 +577,8 @@ class ModelChannel:
close_reason, emitted_response = await self._send_model_output_events(
drain_result, expected_epoch=expected_epoch
)
if self._ctx.run.closing:
return
if close_reason is not None:
await self._close_from_runtime(close_reason)
return
@@ -607,7 +609,7 @@ class ModelChannel:
expected_epoch: int | None = None,
) -> tuple[str | None, bool]:
session = self._ctx.session
if expected_epoch is not None and session.epoch != expected_epoch:
if self._ctx.run.closing or (expected_epoch is not None and session.epoch != expected_epoch):
return None, False
close_reason: str | None = None
emitted_response = False
@@ -624,7 +626,7 @@ class ModelChannel:
)
emitted_response = emitted_response or did_emit
close_reason = close_reason or close_reason_for_result
if expected_epoch is not None and session.epoch != expected_epoch:
if self._ctx.run.closing or (expected_epoch is not None and session.epoch != expected_epoch):
return None, emitted_response
return close_reason, emitted_response
@@ -636,7 +638,7 @@ class ModelChannel:
str(model_result.get("error_code")),
str(model_result.get("error") or "Duplex native data-plane error"),
)
if response_id is None:
if self._ctx.run.closing or response_id is None:
return
session.end_response(commit_text=False)
self._out.emit(
@@ -734,6 +736,8 @@ class ModelChannel:
close_reason: str | None = None
emitted_response = False
self._end_active_response_before_future_model_turn(model_turn_id=model_turn_id)
if self._ctx.run.closing:
return close_reason, emitted_response
if (
session.active_response_id is not None
and model_turn_id is not None
@@ -776,10 +780,14 @@ class ModelChannel:
payload["response_id"] = response_id
self._attach_runtime_metadata(payload, model_result)
self._out.emit(payload)
if self._ctx.run.closing:
return close_reason, emitted_response
if model_result.get("abort_data_plane_request") is True and isinstance(data_plane_request_id, str):
# stage_port.abort_requests expects a list of ids; a bare str is
# iterated as characters and never matches the prewarmed binding.
await self._abort_request([data_plane_request_id], notify=False)
if self._ctx.run.closing:
return close_reason, emitted_response
if response_id is not None:
if not auto_response and self.response_continuations_remaining(response_id):
self._ctx.services.spawn(
@@ -822,7 +830,7 @@ class ModelChannel:
data_plane = self._ctx.plugin.data_plane
close_reason: str | None = None
emitted_response = False
if expected_epoch is not None and session.epoch != expected_epoch:
if self._ctx.run.closing or (expected_epoch is not None and session.epoch != expected_epoch):
return close_reason, emitted_response
data_plane_request_id = model_result.get("data_plane_request_id")
if isinstance(data_plane_request_id, str) and data_plane.is_terminal(data_plane_request_id):
@@ -932,6 +940,8 @@ class ModelChannel:
return close_reason, emitted_response
if draining_response_id is None:
self._end_active_response_before_future_model_turn(model_turn_id=model_turn_id)
if self._ctx.run.closing:
return close_reason, emitted_response
if (
draining_response_id is None
and session.active_response_id is not None
@@ -955,6 +965,8 @@ class ModelChannel:
if response_request_metrics:
created_payload["response_request_metrics"] = response_request_metrics
self._out.emit(created_payload)
if self._ctx.run.closing:
return close_reason, emitted_response
stage_metrics = model_result.get("stage_metrics")
response_stage_metrics = session.accumulate_response_stage_metrics(
stage_metrics if isinstance(stage_metrics, Mapping) else None
@@ -976,6 +988,8 @@ class ModelChannel:
response_request_metrics=response_request_metrics,
)
self._out.emit(speak_payload)
if self._ctx.run.closing:
return close_reason, emitted_response
target_id = draining_response_id if draining_response_id not in (None, session.active_response_id) else None
previous_sent_ms = session.playback_for_response(target_id).sent_ms
text_chars_before_append = len(session.assistant_transcript(target_id))
@@ -1052,6 +1066,8 @@ class ModelChannel:
response_request_metrics=response_request_metrics,
)
self._out.emit(payload)
if self._ctx.run.closing:
return close_reason, emitted_response
if (
not end_of_turn
and model_result.get("stage_role") == "tts"
@@ -1233,7 +1249,7 @@ class ModelChannel:
response_id = session.active_response_id
response_epoch = session.epoch
response_turn_id = model_turn_id if model_turn_id is not None else session.active_response_turn_id
if expected_epoch is not None and response_epoch != expected_epoch:
if self._ctx.run.closing or (expected_epoch is not None and response_epoch != expected_epoch):
return
model_state.clear_continuation()
payload: dict[str, object] = {
@@ -1246,6 +1262,8 @@ class ModelChannel:
if response_id is not None:
payload["response_id"] = response_id
self._out.emit(payload)
if self._ctx.run.closing:
return
if response_id is None:
if session.epoch == response_epoch and response_turn_id is not None:
session.complete_model_turn(response_turn_id)
@@ -1277,7 +1295,8 @@ class ModelChannel:
) -> bool:
session = self._ctx.session
stale_common_owner = (
session.state == DuplexSessionState.CLOSED
self._ctx.run.closing
or session.state == DuplexSessionState.CLOSED
or session.active_request_id != request_id
or (expected_epoch is not None and session.epoch != expected_epoch)
)
@@ -1372,6 +1391,6 @@ class ModelChannel:
except Exception as exc:
logger.exception("Failed to schedule duplex native response continuation: %s", exc)
scheduled = False
if scheduled:
if scheduled and not self._ctx.run.closing:
model_state.continuation_owner_id = owner_id
model_state.continuation_units = count + 1