fix(voice): correct input accounting and session termination
Separate detected speech limits from continuous microphone silence while preserving transport bounds and absolute commit fencing. Handle verified OpenAI cancellation races and terminal items, preserve causal close outcomes, and share bounded cleanup ownership with safe diagnostics. Add regression coverage and align the API contract and frontend handoff. Validation: 61 focused unit tests passed; staged diff whitespace checks passed. Live browser acceptance remains outstanding.
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
"""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, 1500, 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(50):
|
||||
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 == 480000
|
||||
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())
|
||||
Reference in New Issue
Block a user