fix(voice): harden noisy streaming turns

This commit is contained in:
Yera All
2026-04-17 00:30:41 +05:00
parent 8ca5f23e93
commit 39096e114f
6 changed files with 267 additions and 15 deletions
+24
View File
@@ -24,6 +24,30 @@ def test_context_summary_tracks_city_and_branch_slot_for_schedule():
assert summary["open_slots"] == ["branch_or_address"]
def test_context_summary_maps_common_asr_city_mishearing_to_almaty():
summary = update_context_summary_from_user_turn(
None,
channel="voice",
language="ru",
customer_name="Ернур",
text="Хочу узнать график работы.",
now="2026-04-12T00:00:00+00:00",
)
updated = update_context_summary_from_user_turn(
json.dumps(summary, ensure_ascii=False),
channel="voice",
language="ru",
customer_name="Ернур",
text="Матта.",
now="2026-04-12T00:00:02+00:00",
)
assert updated["active_intent"] == "schedule"
assert updated["confirmed_facts"]["city"] == "Алмата"
assert updated["open_slots"] == ["branch_or_address"]
def test_context_summary_does_not_overwrite_meaningful_request_with_low_signal():
summary = update_context_summary_from_user_turn(
None,
+38
View File
@@ -1655,6 +1655,44 @@ def test_voice_postprocess_reply_uses_summary_context_when_raw_window_lost_topic
assert "филиал" in normalized or "адрес" in normalized
def test_voice_decision_uses_summary_city_slot_instead_of_asking_city_again(monkeypatch):
def _unexpected_llm(messages):
raise AssertionError(f"LLM should not be called when summary slot prompt is enough: {messages!r}")
monkeypatch.setattr(ai_module, "_request_structured_model_decision", _unexpected_llm)
decision = voice_module._voice_decision(
language="ru",
customer=SimpleNamespace(display_name="Ернур"),
interaction=SimpleNamespace(interaction_id="int_voice_summary_slot", customer_id=None, status="open", queue_id=None, subject=None),
transcript_text="Матта.",
transcript_window=[
SimpleNamespace(speaker="caller", text="Матта.", sequence_no=1, source_type="voice_asr", barge_in_interrupted=False, created_at=utc_now_iso()),
],
context_summary=json.dumps(
{
"customer_name": "Ернур",
"active_intent": "schedule",
"active_request_text": "Хочу узнать график работы",
"confirmed_facts": {"city": "Алмата", "branch_hint": None, "service_hint": "график работы", "request_number": None},
"open_slots": ["branch_or_address"],
},
ensure_ascii=False,
),
kb_results=[],
disclosure_required=False,
customer_name_value="Ернур",
customer_name_status="name_obtained",
request_metadata={"voice_v2_enabled": True},
)
assert decision["intent"] == "clarification"
assert decision["needs_handoff"] is False
assert "Алмата" in decision["reply_text"]
assert "город" in decision["reply_text"].lower()
assert "филиал" in decision["reply_text"].lower() or "адрес" in decision["reply_text"].lower()
def test_ai_enqueue_creates_outbound_ai_reply_and_delivery_flow(monkeypatch):
monkeypatch.setenv("AI_TELEGRAM_ENABLED", "1")
monkeypatch.setenv("AI_PROVIDER", "stub")
+120
View File
@@ -1047,6 +1047,12 @@ def test_media_runtime_voice_v2_emits_blind_ack_on_first_turn_without_partial_si
assert speak_events[1][0] == "Подскажите подробнее, пожалуйста."
def test_media_runtime_low_signal_filter_catches_short_asr_noise():
assert AudioSocketMediaRuntime._is_low_signal_transcript("Давай")
assert AudioSocketMediaRuntime._is_low_signal_transcript("твой")
assert AudioSocketMediaRuntime._is_low_signal_transcript("Поргай, что это")
def test_media_runtime_ignores_low_signal_utterance_without_ack_or_turn():
planned: list[tuple[str, str, str, dict | None]] = []
delivered: list[tuple[str, str, bool]] = []
@@ -1624,6 +1630,120 @@ def test_media_runtime_streaming_sidecar_push_does_not_block_vad_finalization():
assert streaming_provider.push_count >= 1
def test_media_runtime_streaming_partial_poll_does_not_block_turn_close_ack():
events: list[str] = []
class _StreamingProvider(StreamingASRProvider):
name = "streaming-sidecar"
supports_streaming = True
def __init__(self) -> None:
self.poll_count = 0
def poll_partial(self, stream_id: str) -> StreamingASRPartial | None:
del stream_id
self.poll_count += 1
time.sleep(0.25)
return StreamingASRPartial(text="need schedule", language="ru", confidence=0.8, is_stable=True)
def finalize(self, stream_id: str) -> ASRTranscription:
assert stream_id == "stream-1"
events.append("finalize")
return ASRTranscription(text="need schedule", language="ru", confidence=0.9)
def close_stream(self, stream_id: str) -> None:
assert stream_id == "stream-1"
streaming_provider = _StreamingProvider()
runtime = AudioSocketMediaRuntime(
enabled=True,
host="127.0.0.1",
port=0,
frame_ms=20,
idle_timeout_seconds=2.0,
registration_wait_timeout_seconds=0.5,
min_speech_ms=40,
trailing_silence_ms=40,
max_turn_ms=2000,
asr_provider=_StubASRProvider(),
streaming_asr_provider=streaming_provider,
tts_provider=_StubTTSProvider(),
load_registration_by_media_uuid=lambda value: None,
mark_media_connected=lambda session_id, value: None,
mark_media_ended=lambda session_id, reason: None,
touch_media_frame=lambda session_id: None,
set_state=lambda session_id, state, handoff_reason, metadata: None,
get_pending_greeting=lambda session_id: None,
mark_reply_delivered=lambda session_id, text, is_greeting: None,
plan_reply=lambda session_id, text, metadata, kind: None,
process_turn=lambda session_id, transcript_text, language, barge_in, metadata: VoiceAITurnDecisionOut(
language=language or "ru",
intent="schedule",
reply_text="reply",
confidence=0.8,
needs_handoff=False,
handoff_reason=None,
case_action="keep_open",
kb_refs=[],
summary_text="reply ready",
model="stub-voice",
latency_ms=1,
status="active",
),
request_handoff=lambda session_id, customer_request_text, decision: None,
handle_media_error=lambda session_id, message, metadata: None,
)
async def _fake_speak_reply(
actor: MediaActor,
text: str,
*,
is_greeting: bool,
style_hints: dict[str, object] | None = None,
reply_phase: str | None = "main",
) -> None:
del actor, text, is_greeting, style_hints
events.append(str(reply_phase))
runtime._speak_reply = _fake_speak_reply # type: ignore[method-assign]
async def _scenario() -> None:
actor = MediaActor(
registration=MediaRegistration(
voice_session_id="avs_media_runtime_turn_close_no_poll",
call_id="call_media_runtime_turn_close_no_poll",
interaction_id="int_media_runtime_turn_close_no_poll",
ai_session_id="ais_media_runtime_turn_close_no_poll",
language="ru",
media_uuid=str(uuid.uuid4()),
queue_code="voice_lab_ai",
queue_id="que_voice_lab_ai",
agent_profile="voice_support",
voice_v2_enabled=True,
voice_v2_ack_mode="immediate_short",
voice_v2_streaming_tts=True,
voice_v2_partial_asr=True,
voice_v2_duplex=True,
voice_v2_streaming_asr_backend="local_sidecar",
),
reader=asyncio.StreamReader(),
writer=None, # type: ignore[arg-type]
vad=EnergyVAD(frame_ms=20, min_speech_ms=40, trailing_silence_ms=40, max_turn_ms=2000),
frame_ms=20,
frame_bytes=320,
)
actor.asr_streaming_enabled = True
actor.asr_stream_id = "stream-1"
pcm_frame = (1000).to_bytes(2, "little", signed=True) * 160
await runtime._process_utterance(actor, pcm_frame * 40, False)
asyncio.run(_scenario())
assert streaming_provider.poll_count == 0
assert events[:2] == ["ack", "finalize"]
assert "main" in events
def test_media_runtime_merges_thinking_continuation_into_current_utterance():
runtime = AudioSocketMediaRuntime(
enabled=True,