fix(db): improve WSL compatibility and SQLite settings

- Auto-detect WSL environment and use home directory for database to avoid cross-filesystem issues
- Change SQLite journal_mode from DELETE to WAL for better concurrency
- Add synchronous=NORMAL and busy_timeout=5000 for better reliability
- This fixes message write failures in WSL environments

WSL2's 9P protocol doesn't fully support POSIX file locks across filesystems,
causing SQLite write failures. Using WAL mode and local filesystem fixes this.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
ekko
2026-04-29 13:20:21 +08:00
parent f1b683d084
commit f2bf2ffe92
+8 -2
View File
@@ -4,7 +4,10 @@ import { resolve } from 'path'
import { homedir } from 'os'
const isDev = process.env.NODE_ENV !== 'production'
const DB_DIR = isDev
const isInWSL = existsSync('/proc/version') && readFileSync('/proc/version', 'utf-8').includes('microsoft')
// In WSL, always use home directory to avoid cross-filesystem issues
const DB_DIR = (isDev && !isInWSL)
? resolve(process.cwd(), 'packages/server/data')
: resolve(homedir(), '.hermes-web-ui')
const DB_PATH = resolve(DB_DIR, 'hermes-web-ui.db')
@@ -30,7 +33,10 @@ export function getDb(): DatabaseSync | null {
if (!_db) {
mkdirSync(DB_DIR, { recursive: true })
_db = new DatabaseSync(DB_PATH)
_db.exec('PRAGMA journal_mode=DELETE')
// Use WAL mode for better concurrency and WSL compatibility
_db.exec('PRAGMA journal_mode=WAL')
_db.exec('PRAGMA synchronous=NORMAL')
_db.exec('PRAGMA busy_timeout=5000')
_db.exec('PRAGMA foreign_keys=ON')
}
return _db