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)
37 lines
1,021 B
Python
37 lines
1,021 B
Python
# app/db.py — SQLite connection and migration runner for Linnet
|
|
#
|
|
# Used only for corrections storage (tone annotation training data).
|
|
# Session state is in-memory; this DB persists user-submitted corrections.
|
|
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
from collections.abc import Iterator
|
|
from pathlib import Path
|
|
|
|
from circuitforge_core.db import run_migrations
|
|
|
|
from app.config import settings
|
|
|
|
_MIGRATIONS_DIR = Path(__file__).parent / "migrations"
|
|
|
|
|
|
def _db_path() -> Path:
|
|
path = Path(settings.linnet_db)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
return path
|
|
|
|
|
|
def get_connection() -> sqlite3.Connection:
|
|
conn = sqlite3.connect(str(_db_path()), check_same_thread=False)
|
|
conn.execute("PRAGMA journal_mode=WAL")
|
|
run_migrations(conn, _MIGRATIONS_DIR)
|
|
return conn
|
|
|
|
|
|
def get_db() -> Iterator[sqlite3.Connection]:
|
|
"""FastAPI dependency — yields a connection, closes on teardown."""
|
|
conn = get_connection()
|
|
try:
|
|
yield conn
|
|
finally:
|
|
conn.close()
|