feat(voice): add isolated Local S2S v3 candidate and acceptance evidence

Extract the provider boundary while retaining OpenAI/Gemini contracts. Add
an opt-in isolated Local S2S adapter, pinned engine extension, bounded
component gateway and frozen effective configuration profiles.

Enforce request token limits, zero retries, local asset validation,
connection deadlines, disconnect ownership, bounded cleanup and signals.
Include regression coverage, pinned source fixtures, operator guidance,
asset fingerprints and guarded disposable PostgreSQL acceptance tooling.
Preserve immutable fixture bytes across Git line-ending conversion.

Validation recorded in the reports:
- 74 repair, 107 combined and 242 preservation checks passed.
- Fresh PostgreSQL local checks: 2 passed.
- Expanded PostgreSQL: 57 passed, 6 documented baseline failures.
- Compose configurations, staged whitespace and fixture hashes passed.

Phase 1 live listening and Phase 2 complete engine/SDK/POSIX, real component
audio and Persian browser acceptance remain pending. No operational switch,
paid call, model download or Phase 3 implementation is included.
This commit is contained in:
amirhosein mazaheri
2026-10-15 12:28:17 +03:30
parent 872e5f25cb
commit 8d348f0ba1
56 changed files with 8802 additions and 44 deletions
+86
View File
@@ -0,0 +1,86 @@
"""Frozen, credential-free identity for the development-only local engine."""
from hashlib import sha256
from urllib.parse import urlsplit
from typing import Literal
from pydantic import BaseModel, Field, SecretStr, field_validator
from app.modules.voice.provider import canonical_json
UPSTREAM_REVISION = '619e3fe4f71d1c6f35c15870d588a3638de34722'
CONTRACT_REVISION = 'rag-local-s2s-phase2-v1'
PATCH_REVISION = 'rag-local-s2s-phase2-v3'
class LocalS2SSettings(BaseModel):
local_s2s_enabled: bool = False
local_s2s_endpoint: str = 'ws://local-s2s-engine:8765/v1/realtime'
local_s2s_token: SecretStr = SecretStr('')
local_s2s_engine_revision: Literal[UPSTREAM_REVISION] = UPSTREAM_REVISION
local_s2s_stt_model: str = Field(default='', max_length=200)
local_s2s_llm_model: str = Field(default='', max_length=200)
local_s2s_tts_model: str = Field(default='', max_length=200)
local_s2s_tts_voice: str = Field(default='', max_length=80)
local_s2s_language: Literal['fa'] = 'fa'
local_s2s_max_output_tokens: int = Field(default=256, ge=1, le=1024)
local_s2s_request_timeout_seconds: float = Field(default=30, gt=0, le=60, allow_inf_nan=False)
@field_validator('local_s2s_endpoint')
@classmethod
def endpoint(cls, value):
try:
p = urlsplit(value)
p.port
if (len(value) > 512 or p.scheme not in ('ws', 'wss') or not p.hostname
or p.username or p.password or p.query or p.fragment or p.path != '/v1/realtime'):
raise ValueError()
except ValueError:
raise ValueError('Invalid local engine endpoint') from None
return value
@field_validator('local_s2s_stt_model', 'local_s2s_llm_model', 'local_s2s_tts_model', 'local_s2s_tts_voice')
@classmethod
def local_component_identifier(cls, value):
if not value.isascii() or any(c not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._/' for c in value):
raise ValueError('Invalid local component identifier')
return value
def components(self):
return dict(stt=dict(backend='openai', model=self.local_s2s_stt_model,
language='fa', response_format='json', final_only=True),
llm=dict(backend='chat-completions', model=self.local_s2s_llm_model,
max_output_tokens=self.local_s2s_max_output_tokens),
tts=dict(backend='openai', model=self.local_s2s_tts_model,
voice=self.local_s2s_tts_voice, response_format='pcm', sample_rate=24000),
request_timeout_seconds=self.local_s2s_request_timeout_seconds)
def setup_session(config):
# Reuse the existing backend tool schema/policy; no second retrieval path.
from app.infrastructure.realtime import RealtimeAdapter
session = RealtimeAdapter(None).setup()['session']
session.pop('reasoning', None)
session['model'] = config.local_s2s_llm_model
session['max_output_tokens'] = config.local_s2s_max_output_tokens
session['audio']['input']['transcription'] = dict(model=config.local_s2s_stt_model, language='fa')
session['audio']['output']['voice'] = config.local_s2s_tts_voice
session['audio']['output']['speed'] = 1.0
return session
def effective_identity(components, session):
return dict(engine_revision=UPSTREAM_REVISION, patch_revision=PATCH_REVISION,
contract_revision=CONTRACT_REVISION, components=components,
effective_config_sha256=sha256(canonical_json(dict(components=components, session=session)).encode()).hexdigest())
def require_isolated_candidate(config, ingestion, *, database_url=None):
"""Run before startup reconciliation, never on the operational database."""
from sqlalchemy.engine import make_url
if config.provider != 'local_s2s':
return
config.require_voice()
url = make_url(database_url if database_url is not None else ingestion.database_url.get_secret_value())
if not url.database or not url.database.endswith('_local_s2s_test'):
raise ValueError('Local S2S requires an isolated *_local_s2s_test database')
for path in (ingestion.original_directory, ingestion.extraction_directory):
if 'local-s2s-test' not in path.resolve().parts:
raise ValueError('Local S2S requires dedicated local-s2s-test storage')