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:
amirhosein mazaheri
2026-10-10 17:41:11 +03:30
parent b06eaf59f4
commit a769196bfa
23 changed files with 1809 additions and 92 deletions
+19 -11
View File
@@ -19,6 +19,7 @@ from app.modules.voice.durable_tools import DurableTools
from app.modules.voice import registry
from app.infrastructure.voice_provider import create_adapter
from app.modules.voice.authority import authority_failure_code
from app.modules.voice.diagnostics import diagnostic, terminal_failed
from app.modules.knowledge.calibration import load_cutoff
from app.modules.knowledge.evidence import RETRIEVAL_POLICY
from app.modules.documents.settings import IngestionSettings
@@ -232,7 +233,9 @@ async def handle_stream_websocket(websocket, session_id, auth_service, lifecycle
await blocking(lifecycle_service.validate_stream, user, session_id)
except Exception as exc:
async with runtime.lock:
runtime.fail(authority_failure_code(exc))
code = authority_failure_code(exc)
diagnostic(str(session_id), "authority_monitor", "AUTHORITY_CHECK_FAILED", exc=exc, code=code)
runtime.fail(code)
return
children = [asyncio.create_task(browser()), asyncio.create_task(runtime.receive_loop()),
asyncio.create_task(monitor()), asyncio.create_task(runtime.failure_signal.wait())]
@@ -249,7 +252,9 @@ async def handle_stream_websocket(websocket, session_id, auth_service, lifecycle
except TimeoutError:
pass
outcome = runtime.failure
failed = outcome.startswith('LIVE_') or outcome in ('SEARCH_TIMEOUT','SEARCH_SATURATED','DEPENDENCY_UNAVAILABLE','SEARCH_CANCELLED')
failed = terminal_failed(outcome)
elif not runtime.usable and runtime.close_code == 1000:
outcome, failed = "user_requested", False
except WebSocketDisconnect:
outcome = 'LIVE_TRANSPORT_FAILED'
failed = True
@@ -265,13 +270,6 @@ async def handle_stream_websocket(websocket, session_id, auth_service, lifecycle
for task in children:
task.cancel()
await asyncio.gather(*children, return_exceptions=True)
try:
if runtime:
await asyncio.wait_for(runtime.close(), 6)
elif adapter:
await asyncio.wait_for(adapter.close(), 6)
except Exception:
logger.warning('Bounded provider cleanup incomplete')
try:
# Saturation must not strand the durable terminal outcome once admission frees.
deadline = asyncio.get_running_loop().time() + 6
@@ -283,8 +281,18 @@ async def handle_stream_websocket(websocket, session_id, auth_service, lifecycle
if str(exc) != 'SERVICE_BUSY' or asyncio.get_running_loop().time() >= deadline:
raise
await asyncio.sleep(.025)
except Exception:
logger.warning('Live terminal persistence unavailable')
except Exception as exc:
diagnostic(str(session_id), "terminal_persistence", "TERMINAL_PERSISTENCE_FAILED",
exc=exc, code=outcome)
try:
if runtime:
await asyncio.wait_for(runtime.close(), 6)
elif adapter:
await asyncio.wait_for(adapter.close(), 6)
except Exception as exc:
diagnostic(str(session_id), "stream_cleanup",
"CLEANUP_TIMEOUT" if isinstance(exc, TimeoutError) else "CLEANUP_FAILED",
exc=exc, code=outcome)
await asyncio.wait_for(close_client(runtime.close_code or 1000 if runtime else 1011), 5)
finally:
registry.unregister(session_id)