linnet/app/services/annotator.py
pyr0ball 1bc47b8e0f fix: mobile tab timeout — idle session reaper + wake lock + visibility reconnect
Backend:
- Session.last_subscriber_left_at: monotonic timestamp set when last SSE subscriber
  leaves, cleared when a new one arrives
- Session.subscriber_count(): replaces len(_subscribers) access from outside the model
- session_store._reaper_loop(): kills sessions with no subscribers for >SESSION_IDLE_TTL_S
  (default 90s); runs every TTL/2 seconds via asyncio.create_task at startup
- session_store._reaper_loop_once(): single-cycle variant for deterministic tests
- app/main.py lifespan: starts reaper on startup, cancels it cleanly on shutdown
- config.py: SESSION_IDLE_TTL_S setting (90s default, overridable per-env)

Frontend:
- useWakeLock.ts: Screen Wake Lock API wrapper; acquires on connect, releases on
  disconnect; degrades silently when unsupported (battery saver, iOS Safari)
- useToneStream.ts: visibilitychange handler — on hidden: closes EventSource without
  ending backend session (grace window stays open); on visible: GET /session/{id}
  liveness check, reconnects SSE + re-acquires wake lock if alive, sets expired=true
  and calls store.reset() if reaped
- ComposeBar.vue: surfaces expired state with calm 'Session timed out' notice
  (not an error — expected behaviour on long screen-off)

Tests:
- test_reaper.py: 7 tests covering subscriber idle tracking, reaper eligibility
  (kills idle, spares active subscriber, spares within-TTL)
2026-04-11 09:42:42 -07:00

29 lines
1 KiB
Python

# app/services/annotator.py — VoiceFrame → ToneEvent pipeline
#
# BSL 1.1: this file applies the cf-voice inference results to produce
# session-scoped ToneEvents. The actual inference runs in cf-voice.
from __future__ import annotations
from cf_voice.models import VoiceFrame
from app.models.tone_event import ToneEvent
def annotate(
frame: VoiceFrame,
session_id: str,
elcor: bool = False,
threshold: float = 0.10,
) -> ToneEvent | None:
"""
Convert a VoiceFrame into a session ToneEvent.
Returns None if the frame is below the reliability threshold. The default
0.25 is calibrated for real wav2vec2 SER inference, which routinely scores
0.15-0.45 on conversational audio. The mock used 0.6 (mock confidence was
artificially inflated).
elcor=True switches subtext to bracketed tone-prefix format (easter egg).
"""
if not frame.is_reliable(threshold=threshold):
return None
return ToneEvent.from_voice_frame(frame, session_id=session_id, elcor=elcor)