"""Native answer provenance and playback, without loading multi-GB weights.""" from types import SimpleNamespace import numpy as np import pytest import torch from psi.runtime.swa_speech import schedule_speech def answer(**updates): value = dict(seconds=1., samples=7200, audio_path='answer_000_24k.wav', playback_buffer_seconds=.2, pcm_releases=[dict(sample_start=0, sample_end=4800, available_seconds=1.3), dict(sample_start=4800, sample_end=7200, available_seconds=1.4)]) value.update(updates) return value def test_fifo_keeps_all_pcm_and_does_not_backdate_or_overlap_replies(): segments = schedule_speech([answer(), answer(audio_path='answer_001_24k.wav')]) assert segments[0]['start'] == 36000 assert segments[1]['start'] == segments[0]['end'] assert all(b['start'] >= a['end'] for a, b in zip(segments, segments[1:])) assert sum(s['end'] - s['start'] for s in segments) == 14400 stalled = answer(pcm_releases=[dict(sample_start=0, sample_end=7200, available_seconds=9.)]) assert schedule_speech([stalled])[0]['start'] >= 220800 @pytest.mark.parametrize('change', [ dict(samples=7201), dict(pcm_releases=[dict(sample_start=1, sample_end=7200, available_seconds=1.3)]), dict(pcm_releases=[dict(sample_start=0, sample_end=7200, available_seconds=.5)]), ]) def test_invalid_pcm_is_not_silently_rendered(change): with pytest.raises(ValueError): schedule_speech([answer(**change)]) def test_native_voice_consumes_only_arrived_model_tokens(monkeypatch): import psi.runtime.native_streaming_speech as module class Learner: def __init__(self, *_): self.device = 'cpu' self.done = False self.thinker = SimpleNamespace(config=SimpleNamespace(eos_token_id=99)) self.i = 0 def pop(self): self.i += 1 if self.i == 3: self.done = True return None # Deliberately extreme learned features must NOT reach frozen voice. return torch.tensor([[self.i]]), torch.full((1, 1, 2), -999.), torch.full((1, 1, 2), -888.) def result(self): return dict(ids=[1, 2, 99], text='model words') monkeypatch.setattr(module, 'IncrementalAnswer', Learner) seen = [] class Voice: def get_input_embeddings(self): return lambda ids: ids.float().unsqueeze(-1).expand(-1, -1, 2) def model(self, **kwargs): seen.append(kwargs['inputs_embeds'].clone()) length = kwargs['attention_mask'].shape[1] return SimpleNamespace(last_hidden_state=kwargs['inputs_embeds'] + 10, past_key_values=SimpleNamespace(get_seq_length=lambda: length)) model = SimpleNamespace(vlm_processor=SimpleNamespace(tokenizer=SimpleNamespace( apply_chat_template=lambda *args, **kwargs: [7, 8]))) branch = module.NativeTextConditionAnswer(model, None, Voice()) assert len(seen) == 1 a = branch.pop() assert a[2][0, 0, 0] == 11 branch.pop() assert branch.pop() is None assert branch.result()['voice_arrived_ids'] == [1, 2] assert [x[0, :, 0].tolist() for x in seen] == [[7., 8.], [1.], [2.]]