From f2bf2ffe927991aaa07cb42c0ada427c0190e4de Mon Sep 17 00:00:00 2001 From: ekko Date: Wed, 29 Apr 2026 13:20:21 +0800 Subject: [PATCH] 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 --- packages/server/src/db/index.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/server/src/db/index.ts b/packages/server/src/db/index.ts index 1a9feaf6..4bce42c8 100644 --- a/packages/server/src/db/index.ts +++ b/packages/server/src/db/index.ts @@ -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