Defense in Depth for a Local-First AI Tutor#
Shipping a local voice and Mandarin tutor means hardening both traditional web/mobile attack surfaces and local AI failure modes. In Phase 7 of SpeakOn (Celio1878/speakon-services, commit a2fc8a7 on develop), we paired automated chaos/reliability drills with strict input sanitization, structured failover handlers, POSIX least-privilege file modes, and full cross-repo release gates.
Structured 503 Failover & Prompt-Injection Sanitization#
Two subtle failure classes happen in local LLM applications: crashing with HTTP 500 when Ollama stops mid-session, and leaving untrusted learner text undelimited in structured JSON prompts.
- Zero Unhandled
500s (app/main.py): Global FastAPI exception handlers catchProviderError,httpx.HTTPError, andTimeoutError, returning503 Service Unavailablewith JSON{"detail": ..., "error_code": "provider_unavailable"}across/api/sessions/{id}/turnand/api/mandarin/*. - Untrusted Input Delimiting (
app/mandarin/ai_engines.py,app/mandarin/router.py):sanitize_untrusted_inputstrips ASCII control codes, zero-width spoofing characters (\u200b-\u200f,\ufeff), and closing tag breakouts before wrapping learner text in<untrusted_learner_input>...</untrusted_learner_input>across Dissect, Nuance, Coach, and Graded Stories, paired with30 req/minper-user rate limits, a5,000-character linguistics cap, and batch limits (<= 500logs/attempts,<= 20queued AI items per sync). - Host Least Privilege (
app/core/db.py,app/cli/backup.py): Data directories stay0o700, while SQLite WAL database files, backups, and SHA-256 backup manifests are chmodded0o600on POSIX.
Five Automated Reliability Drills (scripts/drills/run_reliability_drills.py)#
Every drill runs deterministically in isolated temporary directories and exits non-zero on regression:
- SQLite WAL
kill -9Survival (571.4 ms): Spawns a real writer subprocess, sendsSIGKILLafter committed WAL transactions, and verifiesPRAGMA check_db_integrityreturnssqlite:wal:okwith committed sessions intact. - Online Backup -> Lock Guard -> Wipe -> Restore (
49.4 ms): Verifiesrestore_backuprefuses whilehold_server_lockis active, wipes live DB and-wal/-shmfiles, restores from SHA-256 manifest, and validates integrity. - Ollama Stopped Mid-Turn ->
503(1,064.2 ms): TriggersProviderErrorandCircuitOpenErroracross voice turns, sentence dissection, composition coaching, and story generation—asserting HTTP503andprovider_unavailableon every route. - GPU OOM -> CPU Piper Failover (
4.7 ms): ForcesRuntimeError("CUDA out of memory")inside Kokoro and confirmsMultilingualTTSRouterfails over to MIT Piper with valid WAV bytes. - Offline Pack SHA-256 & Idempotent Sync (
231.5 ms): Builds and checksum-verifies the HSK 1–2 starter pack (integrity: "ok") and replays identicalPOST /api/mandarin/syncbatches twice (0duplicate ingestions).
Live RTX 3070 GPU Benchmark & Cross-Repo Aldeon Gates#
After warming en-core-web-sm and local HF/Ollama weights, the full RTX 3070 8 GB benchmark (scripts/bench_turn.py with llama3.1:latest) measured 1,922.9 ms warm end-to-end latency (158.3 ms STT + 1,664.4 ms LLM + 100.3 ms TTS first chunk; budget < 3,000 ms) with 5.501 GB VRAM delta (budget < 6.5 GB).
All 55 / 55 backend tests (46 CPU including test_phase7_hardening.py + 9 GPU golden roundtrips across en/es/zh/pt/de/it/fr + Silero VAD), mypy app (62 files), uvx pip-audit (0 vulnerabilities), Web Gate 1–2 (227 tests, 230 SSR renders, 192.3 KB gz landing JS), Mobile Gate 2 (14 tests, iOS & Android CNG prebuild), DNS Gate 3 (MX/TXT SPF/CNAME), and gitleaks (0 leaks across speakon-services and speakon-mobile full histories) passed on develop.
