Files
rag-technical-docs-assistant/tests/unit/test_local_s2s_history.py
T
11linear11 a94b8cd3ea feat(voice): implement local S2S retained history and provider selection
Add pinned CPU engine dependencies, retained interrupted-response history, provider admission, offline evaluation and isolated browser tooling.

Record Phase 2 acceptance and Phase 3/4 preparation, bounded paid-test accounting and interruption incident findings. Human interruption acceptance, paired OpenAI evaluation and rollback remain pending.
2026-10-16 17:09:27 -04:00

107 lines
5.6 KiB
Python

"""Private retained-draft proof is distinct from native OpenAI truncation."""
from copy import deepcopy
import pytest
from app.infrastructure.local_s2s import LocalS2SAdapter, LocalS2SContextError
from app.modules.voice.provider import HistoryRepair
from app.modules.voice.settings import RuntimeSettings
from tests.unit.test_local_s2s_phase2 import local_config, confirmed
from tests.unit.test_realtime_phase1 import Transport, runner, persisted
def adapter():
a=LocalS2SAdapter(Transport(),config=local_config(local_s2s_retain_interrupted_answers=True),settings=RuntimeSettings(_env_file=None))
session=confirmed(a);session['id']='native-session'
assert a.acknowledged(session)
return a
def receipt(a,repair,revision=1):
request=a.history_repair_request(repair)
return dict(request,type='conversation.item.truncated',s2s_native_type='s2s.history.retained',
event_id='native-proof',operation_id=repair.operation_id,history_revision=revision,
policy='retain_unheard_draft_v1',applied=True)
def test_new_policy_is_frozen_without_rewriting_phase2_profile():
old=local_config().profile()
new=local_config(local_s2s_retain_interrupted_answers=True).profile()
assert old['interruption_policy']=='close_until_phase3'
assert new['interruption_policy']=='retain_unheard_draft_v1'
assert old['local_s2s']['patch_revision']=='rag-local-s2s-phase2-v3'
assert new['local_s2s']['patch_revision']=='rag-local-s2s-phase3-retain-v1'
assert new['local_s2s']['contract_revision']=='rag-local-s2s-phase3-retain-v1'
assert old['local_s2s']['contract_revision']=='rag-local-s2s-phase2-v1'
assert old['local_s2s']['effective_config_sha256']!=new['local_s2s']['effective_config_sha256']
assert old['local_s2s']['components']==new['local_s2s']['components']
@pytest.mark.parametrize('field,value',[
('session_id','wrong'),('engine_session_id','wrong'),('generation',99),
('response_id','wrong'),('operation_id','wrong'),('item_id','wrong'),
('content_index',1),('audio_end_ms',101),('applied',False),
('policy','truncate'),('s2s_native_type','conversation.item.truncated'),
('event_id',None),('event_id',''),('history_revision',0),('history_revision',True),('history_revision',2)])
def test_wrong_or_unapplied_receipt_cannot_release(field,value):
a=adapter();r=HistoryRepair('session',2,'response','op','item',0,100)
event=receipt(a,r);event[field]=value
with pytest.raises(LocalS2SContextError):a.confirm_history_repair(r,event)
def test_applied_receipt_is_correlated_idempotent_and_revision_fenced():
a=adapter();r=HistoryRepair('session',2,'response','op','item',0,100)
event=receipt(a,r)
result=a.confirm_history_repair(r,event)
assert result.repair==r and result.provenance=='local_s2s.retained_unheard_draft'
assert a.confirm_history_repair(r,deepcopy(event))==result
with pytest.raises(LocalS2SContextError):a.confirm_history_repair(r,dict(event,history_revision=2))
r2=HistoryRepair('session',3,'response2','op2','item2',0,200)
with pytest.raises(LocalS2SContextError):a.confirm_history_repair(r2,receipt(a,r2))
assert a.confirm_history_repair(r2,receipt(a,r2,2)).repair==r2
@pytest.mark.parametrize('completed',[False,True])
@pytest.mark.parametrize('proof',['matching','missing','wrong'])
def test_runtime_retained_history_barrier_before_next_response(runner,persisted,completed,proof):
import asyncio
from types import SimpleNamespace
from uuid import uuid4
from app.modules.voice.runtime import VoiceRuntime
from tests.unit.test_realtime_phase1 import input_turn,response,delta,done,until,commands
from tests.unit.test_realtime_repairs import Clock,without_ticker
async def scenario():
a=adapter();wire=a.transport;out=[];clock=Clock()
async def emit(event):out.append(event)
async def authority(execution):return execution.capture(dict(id=execution.user_id,organization_id='fixture',role='user'),1)
r=VoiceRuntime(a,SimpleNamespace(engine=None),runner,uuid4(),emit,authority_check=authority,clock=clock)
try:
await r.start();await until(lambda:commands(wire,'session.update'))
session=confirmed(a);session['id']='native-session'
await r.handle(dict(type='session.updated',session=session))
await until(lambda:r.ready)
await without_ticker(r)
await input_turn(r);await response(r,wire)
await r.handle(delta(48000));await until(lambda:next(iter(r.audio.values())).sent==48000)
audio=next(iter(r.audio.values()));await r.playback_progress(audio.ref,100)
if completed:await done(r)
await input_turn(r,'u2',10)
assert len(commands(wire,'response.create'))==1
if not completed:
await until(lambda:commands(wire,'response.cancel'))
await done(r,status='cancelled')
clock.advance(1);await r.tick()
await until(lambda:commands(wire,'s2s.history.retain'))
assert not commands(wire,'conversation.item.truncate')
assert len(commands(wire,'response.create'))==1
if proof=='missing':
clock.now=audio.cleanup_deadline+.01;await r.tick()
else:
event=receipt(a,audio.repair)
if proof=='wrong':event['operation_id']='wrong'
await r.handle(event)
if proof=='matching':
await until(lambda:len(commands(wire,'response.create'))==2)
assert r.usable and audio.state=='retired'
else:assert not r.usable and len(commands(wire,'response.create'))==1
finally:await r.close()
asyncio.run(scenario())