# Hermes Diagnostics — Corruption Signatures

## SQLite DB Integrity Check
```bash
for db in ~/.hermes/*.db; do
  echo "=== $(basename $db) ==="
  echo "Size: $(du -sh "$db" | cut -f1)"
  file "$db"
  python3 -c "
import sqlite3
path = '$db'
try:
    conn = sqlite3.connect(path)
    c = conn.cursor()
    c.execute('PRAGMA integrity_check')
    print('Integrity:', c.fetchone()[0])
    c.execute(\"SELECT count(*) FROM sqlite_master WHERE type='table'\")
    tables = c.fetchone()[0]
    print('Tables:', tables)
    conn.close()
except Exception as e:
    print('Error:', e)
"
  echo ""
done
```

**Signs of corruption:**
- `file` says "data" instead of "SQLite 3.x database" → overwritten/corrupted
- Integrity check fails → needs rebuild or backup restore
- File is 0 bytes → empty, needs re-initialization

## Fix Corrupted kanban.db
```bash
mv ~/.hermes/kanban.db ~/.hermes/kanban.db.corrupted
hermes kanban init
```

## Fix Corrupted state.db FTS5 indexes
```bash
python3 -c "
import sqlite3
conn = sqlite3.connect('/home/rurouni/.hermes/state.db')
c = conn.cursor()
c.execute('INSERT INTO messages_fts(messages_fts) VALUES(\"rebuild\")')
c.execute('INSERT INTO messages_fts_trigram(messages_fts_trigram) VALUES(\"rebuild\")')
conn.commit()
print('FTS5 indexes rebuilt')
conn.close()
"
```

**Important:** Rebuild on BOTH `messages_fts` AND `messages_fts_trigram` — doing only one leaves the other broken.

## Session DB
```bash
ls -la ~/.hermes/sessions/
file ~/.hermes/sessions/state.db 2>/dev/null || echo "sessions/state.db: missing"
```
If `state.db` is 0 bytes: delete it — Hermes recreates on next write.

The `~/.hermes/sessions/state.db` is separate from `~/.hermes/state.db` — sessions/ is the FTS5 search index, main is the project state DB.

## Known Corruption Patterns (Real Crashes)
See `references/known-corruption-signatures.md` for detailed fault signatures from real incidents: TLS-overwritten kanban.db, FTS5 format errors, zero-byte session DB, gateway cascade pattern.

## General Pitfalls
- Don't just check one DB — corruption often affects multiple files (disk/filesystem issue)
- After fixing kanban.db, the kanban dispatcher only re-checks on the next tick (60s) — no restart needed
- FTS5 rebuild must be on BOTH indexes
