# Scanner Debug Mode Real-time performance instrumentation for the card scanner pipeline. Use this to diagnose slow identification, rate-limit issues, or layer-specific bottlenecks on iOS Chrome, Mac webcam, or any device. ## Activation **Method 1: Browser console (temporary)** ```javascript window.__SCANNER_DEBUG = true ``` **Method 2: localStorage (persists across reloads)** ```javascript localStorage.setItem('SCANNER_DEBUG', 'true') ``` Then navigate to `/scanner` or reload the page. ## What Gets Logged Every scanner operation logs timestamped messages with emoji prefixes for quick visual scanning: ### Shutter Press & Overall Timing ``` [Scanner Debug 21:45:32.123] 🎯 Shutter pressed (tracker-42, attempt 1) [Scanner Debug 21:45:37.456] βœ… Card verified successfully β†’ 5333ms total ``` ### Layer 0: pgvector Visual Similarity ``` [Scanner Debug 21:45:32.150] πŸ” Layer 0 (pgvector visual) started [Scanner Debug 21:45:32.270] ⬆️ Layer 0 escalating β†’ 120ms { reason: 'low confidence' } ``` or ``` [Scanner Debug 21:45:32.270] βœ… Layer 0 resolved β†’ 120ms { card: 'Lightning Bolt', matches: undefined } ``` ### Layer 1: Tesseract OCR + pg_trgm ``` [Scanner Debug 21:45:32.280] πŸ“ Layer 1 (Tesseract OCR + pg_trgm) started [Scanner Debug 21:45:33.130] πŸ”€ OCR completed β†’ 850ms { nameText: 'Lightning Bolt', confidence: 88 } [Scanner Debug 21:45:33.280] βœ… Layer 1 resolved β†’ 1000ms { card: 'Lightning Bolt', matches: 3 } ``` ### Layer 2: Vision API (Gemini/OpenAI) ``` [Scanner Debug 21:45:33.290] πŸ€– Layer 2 (Vision API) call started [Scanner Debug 21:45:37.490] βœ… Vision API success β†’ 4200ms { card: 'Lightning Bolt', needsUserSelection: false } ``` ### Rate Limit / Cooldown ``` [Scanner Debug 21:45:37.500] 🚫 Rate limit hit (15/min) β€” cooldown until 21:46:37 β†’ 10ms ``` or ``` [Scanner Debug 21:45:38.000] ⏸️ Vision cooldown active β€” retry in 59s ``` ### Errors ``` [Scanner Debug 21:45:32.500] ⚠️ Layer 0 failed β†’ 220ms { rateLimited: true } [Scanner Debug 21:45:33.500] ❌ Verification failed (no outcome) β†’ 1000ms [Scanner Debug 21:45:34.500] πŸ’₯ Verification exception β†’ 1200ms { error: 'Network request failed' } ``` ## Reading the Output **Total scan time breakdown:** - **Layer 0 (pgvector):** Typically 80–200ms. If this escalates, L1 runs next. - **Layer 1 OCR:** Tesseract runs in-browser; expect 600–1200ms. If name extraction fails or confidence is low, escalates to L2. - **Layer 2 Vision API:** Network call to `/api/scan/identify` (Gemini/OpenAI). Typically 2–8 seconds depending on network + API latency. **Rate limit (15/min):** The scanner allows 15 vision API calls per minute per user (Redis key `deckhearth:scan:{userId}`). If you hit this, you'll see the cooldown message with the exact retry timestamp. ## Common Patterns ### Fast path (Layer 0 hit) ``` 🎯 Shutter pressed πŸ” Layer 0 started βœ… Layer 0 resolved β†’ 120ms πŸŽ‰ Pipeline complete (L0) β†’ 125ms total βœ… Card verified successfully β†’ 130ms total ``` ### OCR path (Layer 0 miss, Layer 1 hit) ``` 🎯 Shutter pressed πŸ” Layer 0 started ⬆️ Layer 0 escalating β†’ 120ms πŸ“ Layer 1 started πŸ”€ OCR completed β†’ 850ms βœ… Layer 1 resolved β†’ 1000ms πŸŽ‰ Pipeline complete (L1) β†’ 1020ms total βœ… Card verified successfully β†’ 1025ms total ``` ### Vision API path (both layers escalate) ``` 🎯 Shutter pressed πŸ” Layer 0 started ⬆️ Layer 0 escalating β†’ 120ms πŸ“ Layer 1 started πŸ”€ OCR completed β†’ 850ms ⬆️ Layer 1 escalating β†’ 1000ms πŸ€– Layer 2 (Vision API) started βœ… Vision API success β†’ 4200ms πŸŽ‰ Pipeline complete (L2) β†’ 5320ms total βœ… Card verified successfully β†’ 5325ms total ``` ### Rate limit hit ``` 🎯 Shutter pressed (attempt 16) πŸ” Layer 0 started ⬆️ Layer 0 escalating β†’ 110ms πŸ“ Layer 1 started ⬆️ Layer 1 escalating β†’ 950ms πŸ€– Layer 2 (Vision API) started 🚫 Vision API rate limited β†’ 80ms 🚫 Pipeline halted (rate limited) β†’ 1140ms total 🚫 Rate limit hit (15/min) β€” cooldown until 21:46:32 β†’ 1145ms ``` ## iOS Chrome Specific Issues If you see **no logs at all** on iOS Chrome: 1. Open Safari on macOS, connect your iPhone via USB 2. Develop β†’ [Your iPhone] β†’ [deckhearth tab] 3. The Safari Web Inspector console will show the debug logs If **Layer 0/1 succeed but L2 times out**: - iOS Chrome network throttling may be active (Settings β†’ Safari β†’ Advanced β†’ Experimental Features) - Vision API may be slow on cellular β€” test on Wi-Fi If **OCR step shows `nameText: ""` repeatedly**: - Camera capture may be producing black frames on iOS β€” check canvas output in the inspector ## Deactivation ```javascript delete window.__SCANNER_DEBUG localStorage.removeItem('SCANNER_DEBUG') ``` Then reload the page. ## Implementation Debug logs are injected at: - `lib/scanner-card-identify.js` β€” Layer 0/1/2 network calls + pipeline orchestration - `lib/use-scanner-identification.js` β€” Shutter press + verification wrapper The `isDebugMode()` check is inlined in every `debugLog()` call, so there's zero runtime overhead when debug mode is off.