{"record":{"id":"ba35019d55c25a22","repo":"musistudio/claude-code-router","slug":"backend-ownerid-sqlite-store-is-corrupt-and-w","errorCode":null,"errorMessage":"[backend:${ownerId}] SQLite store is corrupt and will be rebuilt: ${dbFile}. Corrupt copy saved to ${backupFile}. Error: ${formatError(error)}","messagePattern":"\\[backend:(.+?)\\] SQLite store is corrupt and will be rebuilt: (.+?)\\. Corrupt copy saved to (.+?)\\. Error: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/plugins/backend-service.ts","lineNumber":176,"sourceCode":"\nexport const backendService = new BackendService();\n\nfunction openSqliteDatabaseWithRecovery(ownerId: string, dbFile: string): SqlDatabase {\n  try {\n    const database = openBetterSqliteDatabase(dbFile);\n    assertSqliteDatabaseIntegrity(database);\n    return database;\n  } catch (error) {\n    if (!isSqliteOpenCorruptionError(error)) {\n      throw error;\n    }\n\n    const backupFile = nextCorruptSqliteBackupPath(dbFile);\n    if (existsSync(dbFile)) {\n      copyFileSync(dbFile, backupFile);\n    }\n    removeSqliteDatabaseFiles(dbFile);\n    console.warn(\n      `[backend:${ownerId}] SQLite store is corrupt and will be rebuilt: ${dbFile}. ` +\n      `Corrupt copy saved to ${backupFile}. Error: ${formatError(error)}`\n    );\n    return openBetterSqliteDatabase(dbFile);\n  }\n}\n\nfunction openBetterSqliteDatabase(dbFile: string): SqlDatabase {\n  const raw = createBetterSqliteDatabase(dbFile);\n  raw.pragma(\"journal_mode = WAL\");\n  raw.pragma(\"synchronous = NORMAL\");\n  raw.pragma(\"busy_timeout = 5000\");\n  return new SqliteCompatDatabase(raw);\n}\n\nclass SqliteCompatDatabase implements SqlDatabase {\n  constructor(private readonly raw: BetterSqliteDatabase) {}\n","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/musistudio/claude-code-router/blob/99f24806c6a2c660b16e53e95211c517448a6c90/packages/core/src/plugins/backend-service.ts#L158-L194","documentation":"Opening the backend's SQLite store failed in a way that indicates corruption. The service copies the corrupt database to a backup path, deletes the database files, logs the corruption cause, and recreates a fresh database. Data in the old store is lost from the service's perspective (only recoverable from the backup copy).","triggerScenarios":"better-sqlite3 throwing SQLITE_CORRUPT or a pragma integrity failure on open; truncated database file from a crash mid-write; power loss or killed process during a transaction; incompatible SQLite page corruption.","commonSituations":"Hard-killing the gateway process during heavy writes; disk full during commit; moving/copying DB files while running; FS corruption. Recurrence means an underlying storage or shutdown-hygiene problem.","solutions":["Restore from the saved backup copy if the data matters: inspect <backupFile> with the sqlite3 CLI (PRAGMA integrity_check) and recover rows before it gets overwritten","Ensure clean shutdowns (SIGTERM handling) so WAL checkpoints complete","Check disk health and free space — recurring corruption usually means failing storage or crashes mid-write","Consider periodic backups of the backend DB"],"exampleFix":"# inspect and salvage the backup\nsqlite3 /path/to/corrupt-backup.db \"PRAGMA integrity_check;\"\nsqlite3 /path/to/corrupt-backup.db .dump | sqlite3 recovered.db\n# then point config at recovered.db or restore it to the original dbFile path","handlingStrategy":"fallback","validationCode":"const ok = db.pragma(\"quick_check\", { simple: true }) === \"ok\";\nif (!ok) throw new Error(\"SQLite store failing integrity check — back it up now\");","typeGuard":null,"tryCatchPattern":"try { openDb(); } catch (e) { /* copy db aside, then rebuild from backup — never delete the corrupt copy */ }","preventionTips":["Handle SIGTERM so the process checkpoints SQLite cleanly","Keep the backend DB on reliable storage with free space","Schedule periodic backups of the backend DB","Never copy DB files while the process is running"],"tags":["sqlite","corruption","data-loss","backend"],"backgroundTag":"sqlite-database-corrupt","analyzedSha":"99f24806c6a2c660b16e53e95211c517448a6c90","analyzedAt":"2026-08-27T04:11:01.184Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}