"""Cancellable pre-generation arbitration for the single shared TTS worker. Retry only an explicit 409/503 before SSE admission, never a partially accepted generation/network failure. No other session is cancelled or evicted. """ import asyncio,time from contextlib import asynccontextmanager @asynccontextmanager async def admitted_turn(client,body,on_wait,timeout=45.,poll=.2): started=time.monotonic();last_notice=-100. while time.monotonic()-started=2.: last_notice=now await on_wait(f'Waiting for the shared voice model ({now-started:.0f} s). You can stop this reply.','waiting') await asyncio.sleep(poll) raise TimeoutError('Shared voice model did not become available within the bounded wait')