|
| 1 | +"""Live speaker ordinal churn telemetry mirrors the display deferral, once per session. |
| 2 | +
|
| 3 | +The tracker must reproduce #23533's qualification exactly enough to be a churn |
| 4 | +signal: five seconds of non-overlapping placed speech on an unnamed scoped voice |
| 5 | +mint one ordinal, named voices mint none, and scope rotations are counted |
| 6 | +separately so churn is readable against provider-epoch and reconnect rotations. |
| 7 | +""" |
| 8 | + |
| 9 | +from types import SimpleNamespace |
| 10 | + |
| 11 | +from models.transcript_segment import TranscriptSegment |
| 12 | +from routers.listen.transcripts import TranscriptProcessor |
| 13 | +from utils.observability.live_speaker_ordinals import ( |
| 14 | + MAX_TRACKED_VOICES, |
| 15 | + OMI_LIVE_SPEAKER_ORDINALS_PER_SESSION, |
| 16 | + OMI_LIVE_SPEAKER_SCOPE_ROTATIONS_TOTAL, |
| 17 | + LiveSpeakerOrdinalTracker, |
| 18 | + record_live_speaker_ordinals, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def voice(sid, speaker, scope, start, end, *, is_user=False, person_id=None): |
| 23 | + return dict( |
| 24 | + id=sid, |
| 25 | + text='Testing one two', |
| 26 | + speaker_id=speaker, |
| 27 | + speaker_id_scope=scope, |
| 28 | + start=start, |
| 29 | + end=end, |
| 30 | + is_user=is_user, |
| 31 | + person_id=person_id, |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def test_deferral_floor_mints_no_ordinal_below_five_placed_seconds(): |
| 36 | + tracker = LiveSpeakerOrdinalTracker() |
| 37 | + tracker.observe(voice('s1', 0, 'scope:0', 0.0, 4.9)) |
| 38 | + assert tracker.close() == (0, 0) |
| 39 | + |
| 40 | + |
| 41 | +def test_five_seconds_of_one_voice_mints_exactly_one_ordinal(): |
| 42 | + tracker = LiveSpeakerOrdinalTracker() |
| 43 | + tracker.observe(voice('s1', 0, 'scope:0', 0.0, 2.0)) |
| 44 | + tracker.observe(voice('s2', 0, 'scope:0', 3.0, 6.0)) |
| 45 | + assert tracker.close() == (1, 0) |
| 46 | + |
| 47 | + |
| 48 | +def test_overlapping_and_replayed_spans_union_instead_of_double_counting(): |
| 49 | + tracker = LiveSpeakerOrdinalTracker() |
| 50 | + # 4s + 4s overlapping in a 1s window is a 7s union: one qualified voice, |
| 51 | + # and a replay of the first span adds nothing. |
| 52 | + tracker.observe(voice('s1', 0, 'scope:0', 0.0, 4.0)) |
| 53 | + tracker.observe(voice('s1', 0, 'scope:0', 3.0, 7.0)) |
| 54 | + tracker.observe(voice('s1', 0, 'scope:0', 0.0, 4.0)) |
| 55 | + assert tracker.close() == (1, 0) |
| 56 | + |
| 57 | + # Adjacent bursts that never reach the floor across two voices mint nothing. |
| 58 | + adjacent = LiveSpeakerOrdinalTracker() |
| 59 | + adjacent.observe(voice('a1', 0, 'scope:0', 0.0, 2.5)) |
| 60 | + adjacent.observe(voice('a2', 1, 'scope:0', 2.5, 5.0)) |
| 61 | + assert adjacent.close() == (0, 0) |
| 62 | + |
| 63 | + |
| 64 | +def test_same_speaker_in_new_scope_is_a_new_ordinal_and_one_rotation(): |
| 65 | + tracker = LiveSpeakerOrdinalTracker() |
| 66 | + tracker.observe(voice('s1', 0, 'scope:0', 0.0, 6.0)) |
| 67 | + tracker.observe(voice('s2', 1, 'scope:1', 10.0, 16.0)) |
| 68 | + tracker.observe(voice('s3', 2, 'scope:2', 20.0, 26.0)) |
| 69 | + assert tracker.close() == (3, 2) |
| 70 | + |
| 71 | + |
| 72 | +def test_named_voices_never_mint_ordinals(): |
| 73 | + tracker = LiveSpeakerOrdinalTracker() |
| 74 | + tracker.observe(voice('s1', 0, 'scope:0', 0.0, 6.0, is_user=True)) |
| 75 | + tracker.observe(voice('s2', 1, 'scope:0', 0.0, 6.0, person_id='person')) |
| 76 | + # A voice named only at close, through the matcher's final map. |
| 77 | + tracker.observe(voice('s3', 2, 'scope:0', 0.0, 6.0)) |
| 78 | + tracker.note_named_voices(SimpleNamespace(speaker_to_person={2: ('person', 'Ada')}, _voice_scopes={2: 'scope:0'})) |
| 79 | + assert tracker.close() == (0, 0) |
| 80 | + |
| 81 | + |
| 82 | +def test_unplaced_or_malformed_spans_add_no_seconds(): |
| 83 | + tracker = LiveSpeakerOrdinalTracker() |
| 84 | + tracker.observe(voice('s1', 0, 'scope:0', -1.0, -1.0)) |
| 85 | + tracker.observe(voice('s2', 0, 'scope:0', 4.0, 4.0)) |
| 86 | + tracker.observe(voice('s3', 0, 'scope:0', float('nan'), 6.0)) |
| 87 | + tracker.observe(voice('s4', 0, 'scope:0', None, '6.0')) |
| 88 | + assert tracker.close() == (0, 0) |
| 89 | + |
| 90 | + |
| 91 | +def test_segments_without_usable_speaker_id_are_ignored(): |
| 92 | + tracker = LiveSpeakerOrdinalTracker() |
| 93 | + tracker.observe({'id': 's1', 'start': 0.0, 'end': 9.0}) |
| 94 | + tracker.observe(voice('s2', 'zero', 'scope:0', 0.0, 9.0)) |
| 95 | + tracker.observe(voice('s3', True, 'scope:0', 0.0, 9.0)) |
| 96 | + assert tracker.close() == (0, 0) |
| 97 | + |
| 98 | + |
| 99 | +def test_voice_inventory_is_bounded(): |
| 100 | + beyond = LiveSpeakerOrdinalTracker() |
| 101 | + for index in range(MAX_TRACKED_VOICES): |
| 102 | + beyond.observe(voice(f's{index}', index, 'scope:0', 0.0, 1.0)) |
| 103 | + # The (MAX_TRACKED_VOICES + 1)-th distinct voice is dropped, never a crash. |
| 104 | + beyond.observe(voice('late', MAX_TRACKED_VOICES, 'scope:0', 0.0, 60.0)) |
| 105 | + assert beyond.close() == (0, 0) |
| 106 | + |
| 107 | + |
| 108 | +def test_record_observes_histogram_and_rotation_counter(): |
| 109 | + before_observations = OMI_LIVE_SPEAKER_ORDINALS_PER_SESSION._sum.get() |
| 110 | + before_rotations = OMI_LIVE_SPEAKER_SCOPE_ROTATIONS_TOTAL._value.get() |
| 111 | + tracker = LiveSpeakerOrdinalTracker() |
| 112 | + tracker.observe(voice('s1', 0, 'scope:0', 0.0, 6.0)) |
| 113 | + tracker.observe(voice('s2', 1, 'scope:1', 8.0, 14.0)) |
| 114 | + tracker.observe(voice('s3', 2, 'scope:2', 20.0, 26.0)) |
| 115 | + record_live_speaker_ordinals(tracker, SimpleNamespace(speaker_to_person={}, _voice_scopes={})) |
| 116 | + assert OMI_LIVE_SPEAKER_ORDINALS_PER_SESSION._sum.get() == before_observations + 3 |
| 117 | + assert OMI_LIVE_SPEAKER_SCOPE_ROTATIONS_TOTAL._value.get() == before_rotations + 2 |
| 118 | + |
| 119 | + |
| 120 | +def test_record_never_raises_and_close_resets_state(): |
| 121 | + record_live_speaker_ordinals(None, None) |
| 122 | + record_live_speaker_ordinals(LiveSpeakerOrdinalTracker(), object()) |
| 123 | + tracker = LiveSpeakerOrdinalTracker() |
| 124 | + tracker.observe(voice('s1', 0, 'scope:0', 0.0, 6.0)) |
| 125 | + assert tracker.close() == (1, 0) |
| 126 | + assert tracker.close() == (0, 0) |
| 127 | + |
| 128 | + |
| 129 | +async def test_speaker_detection_feeds_the_session_tracker(): |
| 130 | + observed = [] |
| 131 | + host = SimpleNamespace( |
| 132 | + limits=SimpleNamespace(max_segment_buffer_size=10, max_photo_buffer_size=10), |
| 133 | + translation_language=None, |
| 134 | + language='en', |
| 135 | + speakers=SimpleNamespace( |
| 136 | + observe_segment=lambda speaker_id, scope, segment_id: observed.append((speaker_id, scope, segment_id)), |
| 137 | + person_embeddings={}, |
| 138 | + speaker_to_person={}, |
| 139 | + ), |
| 140 | + state=SimpleNamespace(speaker_id_enabled=False, current_conversation_id='c1'), |
| 141 | + speaker_ordinal_tracker=LiveSpeakerOrdinalTracker(), |
| 142 | + ) |
| 143 | + processor = TranscriptProcessor(host) |
| 144 | + segments = [ |
| 145 | + TranscriptSegment( |
| 146 | + id='s1', |
| 147 | + text='Testing one two', |
| 148 | + speaker_id=0, |
| 149 | + speaker_id_scope='scope:0', |
| 150 | + start=0.0, |
| 151 | + end=6.0, |
| 152 | + is_user=False, |
| 153 | + speech_profile_processed=True, |
| 154 | + ), |
| 155 | + TranscriptSegment( |
| 156 | + id='s2', |
| 157 | + text='Testing three four', |
| 158 | + speaker_id=1, |
| 159 | + speaker_id_scope='scope:0', |
| 160 | + start=6.0, |
| 161 | + end=8.0, |
| 162 | + is_user=False, |
| 163 | + speech_profile_processed=True, |
| 164 | + ), |
| 165 | + ] |
| 166 | + await processor._speaker_detection(segments, 0.0) |
| 167 | + assert observed == [(0, 'scope:0', 's1'), (1, 'scope:0', 's2')] |
| 168 | + # One qualified voice plus one still below the deferral floor, one scope. |
| 169 | + assert host.speaker_ordinal_tracker.close() == (1, 0) |
0 commit comments