- Add internal reuse_last_evidence tool for continuing existing topic without repeated retrieval - Expand OpenAI output capacity to 120s / 5,760,000 bytes and add delivery fairness - Coalesce adjacent user ASR transcript deltas to prevent client control queue overflow - Add server-side OPENAI_REALTIME_SPEED configuration and update style to v4 - Add extensive unit and integration regressions for evidence reuse, capacity, and fairness
149 lines
7.0 KiB
Python
149 lines
7.0 KiB
Python
"""Finite OpenAI reservoir, aggregate accounting and independent playback credit."""
|
|
import asyncio
|
|
from types import SimpleNamespace
|
|
import tracemalloc
|
|
|
|
import pytest
|
|
|
|
from app.modules.voice.configuration import VoiceSettings
|
|
from app.modules.voice.settings import RuntimeSettings
|
|
from tests.unit.test_realtime_phase1 import (
|
|
runner, persisted, make, input_turn, response, delta, done, until, commands,
|
|
)
|
|
from tests.unit.test_realtime_repairs import Clock
|
|
|
|
|
|
def test_provider_capacity_defaults_and_operator_override(monkeypatch):
|
|
assert RuntimeSettings(_env_file=None).max_output_backlog_bytes == 1440000
|
|
assert RuntimeSettings.for_provider('gemini').max_output_backlog_bytes == 1440000
|
|
limits = RuntimeSettings.for_provider('openai')
|
|
assert limits.max_output_backlog_bytes == 5760000
|
|
assert limits.output_slab_limit == 664
|
|
assert limits.output_credit_window_bytes == 72000
|
|
assert limits.max_queued_audio_bytes == 96000
|
|
assert limits.max_binary_message_bytes == 9600
|
|
profile = VoiceSettings(_env_file=None, provider='openai').profile()
|
|
assert profile['output_backlog_bytes'] == 5760000
|
|
assert profile['output_slab_limit'] == 664
|
|
assert 'output_backlog_bytes' not in VoiceSettings(_env_file=None, provider='gemini').profile()
|
|
monkeypatch.setenv('LIVE_MAX_OUTPUT_BACKLOG_BYTES', '1440000')
|
|
assert RuntimeSettings.for_provider('openai').max_output_backlog_bytes == 1440000
|
|
|
|
|
|
@pytest.mark.parametrize('values', [
|
|
dict(max_output_backlog_bytes=1440048),
|
|
dict(output_capacity_profile='openai-repair', max_output_backlog_bytes=5760048),
|
|
dict(output_capacity_profile='openai-repair', max_output_backlog_bytes=5760001),
|
|
])
|
|
def test_capacity_configuration_remains_finite_and_aligned(values):
|
|
with pytest.raises(ValueError):
|
|
RuntimeSettings(_env_file=None, **values)
|
|
|
|
|
|
def test_gemini_rejects_openai_capacity_before_provider_open(monkeypatch):
|
|
from app.infrastructure import voice_provider
|
|
async def forbidden(*args):
|
|
pytest.fail('Gemini must reject the OpenAI capacity profile before opening')
|
|
monkeypatch.setattr(voice_provider, 'connect_gemini', forbidden)
|
|
config = VoiceSettings(_env_file=None, provider='gemini', gemini_api_key='fake-only')
|
|
with pytest.raises(ValueError, match='OpenAI output capacity'):
|
|
asyncio.run(voice_provider.create_adapter(config, RuntimeSettings.for_provider('openai')))
|
|
|
|
|
|
async def burst(runtime, seconds, item='a1', position=0):
|
|
for _ in range(seconds // 2):
|
|
event = delta(96000, item_id=item)
|
|
event['output_index'] = position
|
|
await runtime.handle(event)
|
|
|
|
|
|
def test_eighty_second_burst_across_refs_drains_without_increasing_credit(runner, persisted):
|
|
async def scenario():
|
|
runtime, wire, out = await make(SimpleNamespace(engine=None), runner, persisted)
|
|
runtime.settings = RuntimeSettings(_env_file=None, output_capacity_profile='openai-repair',
|
|
diagnostic_trace_enabled=1)
|
|
runtime.adapter.settings = runtime.settings
|
|
try:
|
|
await input_turn(runtime)
|
|
await response(runtime, wire)
|
|
for item, pos in [('a1', 0), ('a2', 1)]:
|
|
await burst(runtime, 40, item, pos)
|
|
await runtime.handle(dict(type='response.output_audio.done', response_id='r1',
|
|
item_id=item, output_index=pos, content_index=0))
|
|
await done(runtime)
|
|
assert runtime.usable
|
|
assert runtime.peak_backlog_bytes == 3840000
|
|
async with asyncio.timeout(10):
|
|
while any(a.state != 'retired' for a in runtime.audio.values()):
|
|
for audio in list(runtime.audio.values()):
|
|
assert audio.credit + audio.reserved <= 72000
|
|
if audio.started and audio.state != 'retired':
|
|
await runtime.playback_progress(audio.ref, audio.sent // 48)
|
|
await asyncio.sleep(.001)
|
|
assert runtime.usable
|
|
assert sum(len(e) for e in out if isinstance(e, bytes)) == 3840000
|
|
assert sum(a.played for a in runtime.audio.values()) == 80000
|
|
assert all(a.sent == a.generated and a.credit == 0 for a in runtime.audio.values())
|
|
finally:
|
|
await runtime.close()
|
|
asyncio.run(scenario())
|
|
|
|
|
|
def test_new_bound_rejects_before_decode_and_allocation(runner, persisted, monkeypatch):
|
|
async def scenario():
|
|
runtime, wire, _ = await make(SimpleNamespace(engine=None), runner, persisted)
|
|
runtime.settings = RuntimeSettings(_env_file=None, output_capacity_profile='openai-repair',
|
|
diagnostic_trace_enabled=1)
|
|
runtime.adapter.settings = runtime.settings
|
|
try:
|
|
await input_turn(runtime)
|
|
await response(runtime, wire)
|
|
tracemalloc.start()
|
|
try:
|
|
await burst(runtime, 120)
|
|
_, peak = tracemalloc.get_traced_memory()
|
|
print(f'120-second synthetic reservoir allocation peak: {peak} bytes')
|
|
assert peak < 16 * 1024 * 1024
|
|
finally:
|
|
tracemalloc.stop()
|
|
assert runtime.usable and runtime.peak_backlog_bytes == 5760000
|
|
audio = next(iter(runtime.audio.values()))
|
|
generated = audio.generated
|
|
def forbidden(*args, **kwargs):
|
|
pytest.fail('Overflow must reject before decoding another delta')
|
|
monkeypatch.setattr('app.modules.voice.runtime.base64.b64decode', forbidden)
|
|
await runtime.handle(delta(48))
|
|
assert runtime.failure == 'LIVE_BUFFER_OVERFLOW'
|
|
assert audio.generated == generated
|
|
finally:
|
|
await runtime.close()
|
|
asyncio.run(scenario())
|
|
|
|
|
|
@pytest.mark.parametrize('interrupt', [False, True])
|
|
def test_large_backlog_preserves_stall_and_cancellation_processing(runner, persisted, interrupt):
|
|
async def scenario():
|
|
clock = Clock()
|
|
runtime, wire, _ = await make(SimpleNamespace(engine=None), runner, persisted, clock=clock)
|
|
runtime.settings = RuntimeSettings(_env_file=None, output_capacity_profile='openai-repair')
|
|
runtime.adapter.settings = runtime.settings
|
|
try:
|
|
await input_turn(runtime)
|
|
await response(runtime, wire)
|
|
await burst(runtime, 80)
|
|
await until(lambda: next(iter(runtime.audio.values())).sent > 0)
|
|
if interrupt:
|
|
await runtime.handle(dict(type='input_audio_buffer.speech_started',
|
|
item_id='u2', audio_start_ms=10))
|
|
await until(lambda: bool(commands(wire, 'response.cancel')))
|
|
await done(runtime, status='cancelled')
|
|
assert runtime.responses['r1'].terminal and runtime.usable
|
|
assert all(not a.data for a in runtime.audio.values())
|
|
else:
|
|
clock.advance(5.01)
|
|
runtime.check_deadlines()
|
|
assert runtime.failure == 'LIVE_BUFFER_OVERFLOW'
|
|
finally:
|
|
await runtime.close()
|
|
asyncio.run(scenario())
|