Files
rag-technical-docs-assistant/tests/unit/test_voice_input_accounting.py
amirhosein mazaheri 9e6864ae2a fix(voice): expand speech buffer to 60s, handle 2.1 cancellation race, and bound cleanup
- Expand max_input_buffer_bytes to 3,072,000 (64s at 24kHz PCM16) with 65s fixed deadline
- Broaden response_cancel_not_active race classifier to include gpt-realtime-2.1/shimmer
- Add transport abort method and bound cleanup waits during websocket close
- Record repair progress in docs/plans/live-conversation-repair-progress.md
2026-10-12 18:51:06 +03:30

174 lines
8.7 KiB
Python

"""Silence and speech lifecycle bounds through real runtime sender/event paths."""
import asyncio
from types import SimpleNamespace
import pytest
from tests.unit.test_realtime_phase1 import (
make, until, input_turn, response, delta, done, runner, persisted,
)
async def stream(runtime, frames, now):
for _ in range(frames):
await runtime.input_audio(bytes(960))
async with asyncio.timeout(3):
while runtime.input_transport_count:
await asyncio.sleep(0)
now[0] += .02
assert runtime.usable
def test_silence_before_during_and_between_turns_preserves_absolute_positions(runner, persisted):
async def scenario():
now = [0.]
runtime, transport, output = await make(SimpleNamespace(engine=None), runner, persisted,
clock=lambda: now[0])
try:
await stream(runtime, 1500, now)
assert runtime.input_forwarded_total == 1440000
assert runtime.input_transport_bytes == runtime.uncommitted_speech_bytes == 0
first = await input_turn(runtime, start=30000)
assert first.stop_boundary == 30100 * 48
assert runtime.input_committed_total == first.stop_boundary
await response(runtime, transport)
await runtime.handle(delta(48))
await until(lambda: any(isinstance(e, bytes) for e in output))
audio = next(iter(runtime.audio.values()))
await runtime.playback_progress(audio.ref, 1)
await stream(runtime, 1500, now)
assert runtime.responses["r1"].terminal is False
await done(runtime)
await stream(runtime, 1500, now)
start = runtime.input_dispatched_total // 48
second = await input_turn(runtime, item="u2", start=start)
assert second.stop_boundary == (start + 100) * 48
assert second.committed and runtime.usable
assert runtime.uncommitted_speech_bytes == 0
assert all(cmd.pcm is None for cmd in runtime.pending_commands.values())
finally:
await runtime.close()
asyncio.run(scenario())
@pytest.mark.parametrize("missing", ["stop", "commit"])
def test_missing_speech_events_have_fixed_deadlines(runner, persisted, missing, caplog):
async def scenario():
now = [0.]
runtime, transport, _ = await make(SimpleNamespace(engine=None), runner, persisted,
clock=lambda: now[0])
try:
await stream(runtime, 1500, now)
await runtime.handle(dict(type="input_audio_buffer.speech_started",
item_id="u1", audio_start_ms=30000))
turn = runtime.current
if missing == "commit":
await runtime.input_audio(bytes(9600))
await until(lambda: runtime.input_transport_count == 0)
await runtime.handle(dict(type="input_audio_buffer.speech_stopped",
item_id="u1", audio_end_ms=30200))
deadline = turn.input_deadline
else:
deadline = turn.speech_deadline
await runtime.handle(dict(type="input_audio_buffer.speech_started",
item_id="u1", audio_start_ms=30000))
assert turn.speech_deadline == deadline
now[0] = deadline + .001
assert not runtime.check_deadlines()
assert runtime.failure == ("LIVE_BUFFER_OVERFLOW" if missing == "stop"
else "LIVE_CONTEXT_SYNC_FAILED")
assert runtime.input_transport_bytes == 0
assert ("SPEECH_STOP_DEADLINE" if missing == "stop" else
"INPUT_COMMIT_DEADLINE") in caplog.text
assert runtime.session_id in caplog.text
finally:
await runtime.close()
asyncio.run(scenario())
def test_late_commit_does_not_release_new_speech_interval(runner, persisted):
async def scenario():
now = [0.]
runtime, _, _ = await make(SimpleNamespace(engine=None), runner, persisted, clock=lambda: now[0])
try:
await stream(runtime, 1500, now)
await input_turn(runtime, item="old", start=30000, commit=False)
await runtime.handle(dict(type="input_audio_buffer.speech_started",
item_id="new", audio_start_ms=30150))
before = runtime.uncommitted_speech_bytes
await runtime.handle(dict(type="input_audio_buffer.committed", item_id="old"))
after = runtime.uncommitted_speech_bytes
assert before - after == 100 * 48
await runtime.handle(dict(type="input_audio_buffer.committed", item_id="old"))
assert runtime.uncommitted_speech_bytes == after
assert not runtime.current.committed
await runtime.handle(dict(type="input_audio_buffer.speech_started",
item_id="old", audio_start_ms=30000))
assert runtime.current.input_id == "new" and runtime.token == 2
finally:
await runtime.close()
asyncio.run(scenario())
@pytest.mark.parametrize("branch", ["transport", "speech", "late_start"])
def test_capacity_diagnostics_identify_the_failure_without_payloads(runner, persisted, caplog, branch):
async def scenario():
now = [0.]
runtime, _, _ = await make(SimpleNamespace(engine=None), runner, persisted, clock=lambda: now[0])
try:
if branch == "transport":
# No sender scheduling: all admitted PCM remains queued.
for _ in range(11):
await runtime.input_audio(bytes(9600))
reason = "CAPTURE_TRANSPORT_CAPACITY"
elif branch == "late_start":
await stream(runtime, 3300, now)
await runtime.handle(dict(type="input_audio_buffer.speech_started",
item_id="private-input-id", audio_start_ms=0))
reason = "UNCOMMITTED_SPEECH_BYTES"
else:
await runtime.handle(dict(type="input_audio_buffer.speech_started",
item_id="private-input-id", audio_start_ms=0))
# Fixed clock isolates the byte bound from the independent wall deadline.
for _ in range(320):
await runtime.input_audio(bytes(9600))
async with asyncio.timeout(3):
while runtime.input_transport_count:
await asyncio.sleep(0)
assert runtime.uncommitted_speech_bytes == 3072000
await runtime.input_audio(bytes(2))
reason = "UNCOMMITTED_SPEECH_BYTES"
assert runtime.failure == "LIVE_BUFFER_OVERFLOW" and runtime.close_code == 1013
assert runtime.input_transport_bytes == runtime.input_transport_count == 0
assert reason in caplog.text and runtime.session_id in caplog.text
assert "limit_bytes=" in caplog.text and "occupancy_bytes=" in caplog.text
assert "phase=capture_transport" in caplog.text if branch == "transport" else "phase=speaking" in caplog.text
assert "private-input-id" not in caplog.text and "delta=" not in caplog.text
finally:
await runtime.close()
asyncio.run(scenario())
@pytest.mark.parametrize("speech_seconds", [10, 30, 60])
def test_continuous_speech_with_padding_reaches_new_bound(runner, persisted, speech_seconds):
async def scenario():
now = [0.]
runtime, _, _ = await make(SimpleNamespace(engine=None), runner, persisted,
clock=lambda: now[0])
try:
await runtime.handle(dict(type="input_audio_buffer.speech_started",
item_id="u1", audio_start_ms=0))
turn = runtime.current
assert turn.speech_deadline == 65
# Four seconds covers the maximum configured VAD prefix/silence padding.
for _ in range((speech_seconds + 4) * 5):
await runtime.input_audio(bytes(9600))
await until(lambda: runtime.input_transport_count == 0)
now[0] += .2
assert runtime.usable
assert runtime.uncommitted_speech_bytes == (speech_seconds + 4) * 48000
await runtime.handle(dict(type="input_audio_buffer.speech_stopped",
item_id="u1", audio_end_ms=(speech_seconds + 4) * 1000))
await runtime.handle(dict(type="input_audio_buffer.committed", item_id="u1"))
assert turn.committed and runtime.uncommitted_speech_bytes == 0
finally:
await runtime.close()
asyncio.run(scenario())