sqlite-adapter.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /**
  2. * SQLite Adapter
  3. *
  4. * Provides a unified interface over better-sqlite3 (native) and
  5. * node-sqlite3-wasm (WASM fallback) for universal cross-platform support.
  6. */
  7. export interface SqliteStatement {
  8. run(...params: any[]): { changes: number; lastInsertRowid: number | bigint };
  9. get(...params: any[]): any;
  10. all(...params: any[]): any[];
  11. }
  12. export interface SqliteDatabase {
  13. prepare(sql: string): SqliteStatement;
  14. exec(sql: string): void;
  15. pragma(str: string): any;
  16. transaction<T>(fn: (...args: any[]) => T): (...args: any[]) => T;
  17. close(): void;
  18. readonly open: boolean;
  19. }
  20. export type SqliteBackend = 'native' | 'node-sqlite' | 'wasm';
  21. /**
  22. * One-line summary of the recovery steps shown when WASM fallback is
  23. * active. Single source of truth so the recipe can't drift between the
  24. * stderr banner and the MCP status formatter.
  25. */
  26. export const WASM_FALLBACK_FIX_RECIPE =
  27. '`xcode-select --install` (macOS) or `apt install build-essential` (Debian/Ubuntu), ' +
  28. 'then `npm rebuild better-sqlite3`, or `npm install better-sqlite3 --save` to force-include it.';
  29. /**
  30. * Multi-line banner shown to stderr when `createDatabase` falls back to
  31. * WASM. Replaces a one-line `console.warn` that MCP transports (which
  32. * take stdout for the protocol) typically swallow, leaving users on a
  33. * 5-10x slower backend with no signal.
  34. *
  35. * Exported for unit testing — pinning the recipe content prevents
  36. * future edits from silently stripping the recovery commands.
  37. */
  38. export function buildWasmFallbackBanner(nativeError?: string): string {
  39. const sep = '─'.repeat(72);
  40. const lines = [
  41. sep,
  42. '[CodeGraph] WASM SQLite fallback active (better-sqlite3 unavailable)',
  43. sep,
  44. 'Indexing and sync will be 5-10x slower than the native backend.',
  45. '',
  46. 'Fix on macOS:',
  47. ' xcode-select --install # install C build tools',
  48. ' npm rebuild better-sqlite3 # rebuild native binding for current Node',
  49. '',
  50. 'Fix on Linux:',
  51. ' sudo apt install build-essential python3 make # Debian/Ubuntu',
  52. ' # or: sudo yum groupinstall "Development Tools" # RHEL/Fedora',
  53. ' npm rebuild better-sqlite3',
  54. '',
  55. 'Or force-include as a hard dependency on any platform:',
  56. ' npm install better-sqlite3 --save',
  57. '',
  58. 'Verify after fix: `codegraph status` should show `Backend: native`.',
  59. ];
  60. if (nativeError) {
  61. lines.push('', `Native load error: ${nativeError}`);
  62. }
  63. lines.push(sep);
  64. return lines.join('\n');
  65. }
  66. /**
  67. * Translate @named parameters (better-sqlite3 style) to positional ? params
  68. * for node-sqlite3-wasm, which only supports positional binding.
  69. *
  70. * Returns the rewritten SQL and an ordered list of parameter names.
  71. * If no named params are found, returns null for paramOrder (positional mode).
  72. */
  73. function translateNamedParams(sql: string): { sql: string; paramOrder: string[] | null } {
  74. const paramOrder: string[] = [];
  75. const rewritten = sql.replace(/@(\w+)/g, (_match, name: string) => {
  76. paramOrder.push(name);
  77. return '?';
  78. });
  79. if (paramOrder.length === 0) {
  80. return { sql, paramOrder: null };
  81. }
  82. return { sql: rewritten, paramOrder };
  83. }
  84. /**
  85. * Convert better-sqlite3-style params to a positional array for node-sqlite3-wasm.
  86. *
  87. * Handles three calling conventions:
  88. * - Named object: run({ id: '1', name: 'a' }) → positional array via paramOrder
  89. * - Positional args: run('a', 'b') → ['a', 'b']
  90. * - No args: run() → undefined
  91. */
  92. function resolveParams(params: any[], paramOrder: string[] | null): any {
  93. if (params.length === 0) return undefined;
  94. // If paramOrder exists and first arg is a plain object, do named→positional translation
  95. if (paramOrder && params.length === 1 && params[0] !== null && typeof params[0] === 'object' && !Array.isArray(params[0]) && !(params[0] instanceof Buffer) && !(params[0] instanceof Uint8Array)) {
  96. const obj = params[0];
  97. return paramOrder.map(name => obj[name]);
  98. }
  99. // Positional: single value or already an array
  100. if (params.length === 1) return params[0];
  101. return params;
  102. }
  103. /**
  104. * Whether an error is SQLite's SQLITE_BUSY / SQLITE_LOCKED ("database is
  105. * locked"). Checks better-sqlite3's `code` first, then falls back to message
  106. * text for the wasm backend (which throws a plain Error). Exported for tests.
  107. */
  108. export function isDatabaseLockedError(err: unknown): boolean {
  109. const code = (err as { code?: unknown } | null)?.code;
  110. if (code === 'SQLITE_BUSY' || code === 'SQLITE_LOCKED') return true;
  111. const msg = (err instanceof Error ? err.message : String(err)).toLowerCase();
  112. return (
  113. msg.includes('database is locked') ||
  114. msg.includes('database is busy') ||
  115. msg.includes('database table is locked') ||
  116. msg.includes('sqlite_busy') ||
  117. msg.includes('sqlite_locked')
  118. );
  119. }
  120. /**
  121. * Sleep synchronously for `ms` without spinning the CPU. The wasm backend is
  122. * single-threaded and synchronous, so an async sleep is useless at the
  123. * (synchronous) query call site — we have to actually block this turn while a
  124. * writer in another process clears.
  125. */
  126. function sleepSync(ms: number): void {
  127. if (ms <= 0) return;
  128. Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
  129. }
  130. export interface BusyRetryOptions {
  131. /** Total attempts, including the first. */
  132. attempts?: number;
  133. /** Backoff per retry (ms); the last entry repeats if more retries remain. */
  134. backoffMs?: number[];
  135. /** Sleep implementation — injectable so tests don't actually wait. */
  136. sleep?: (ms: number) => void;
  137. }
  138. /**
  139. * Run a read, retrying on SQLITE_BUSY with bounded backoff.
  140. *
  141. * Used only by the wasm backend: it can't use WAL (downgraded to DELETE), so a
  142. * writer in ANOTHER process (e.g. the git-hook `codegraph sync`) briefly blocks
  143. * readers. `busy_timeout` helps but can return immediately when SQLite detects a
  144. * would-be deadlock; a short retry rides out the writer. Reads only — never wrap
  145. * writes, which run inside transactions guarded by the cross-process FileLock.
  146. * The native backend doesn't use this: WAL lets readers proceed during a write.
  147. * See issue #238.
  148. */
  149. export function withBusyRetry<T>(fn: () => T, opts: BusyRetryOptions = {}): T {
  150. const attempts = opts.attempts ?? 3;
  151. const backoff = opts.backoffMs ?? [150, 400];
  152. const sleep = opts.sleep ?? sleepSync;
  153. let lastErr: unknown;
  154. for (let i = 0; i < attempts; i++) {
  155. try {
  156. return fn();
  157. } catch (err) {
  158. lastErr = err;
  159. if (i === attempts - 1 || !isDatabaseLockedError(err)) throw err;
  160. sleep(backoff.length > 0 ? backoff[Math.min(i, backoff.length - 1)]! : 0);
  161. }
  162. }
  163. throw lastErr;
  164. }
  165. /**
  166. * Wraps node-sqlite3-wasm to match the better-sqlite3 interface.
  167. *
  168. * Key differences handled:
  169. * - better-sqlite3 uses @named params; node-sqlite3-wasm uses positional ? only
  170. * - better-sqlite3 uses variadic args: stmt.run(a, b, c)
  171. * - node-sqlite3-wasm uses a single array/object: stmt.run([a, b, c])
  172. * - node-sqlite3-wasm has `isOpen` instead of `open`
  173. * - node-sqlite3-wasm doesn't have a `pragma()` method
  174. * - node-sqlite3-wasm doesn't have a `transaction()` method
  175. */
  176. class WasmDatabaseAdapter implements SqliteDatabase {
  177. private _db: any;
  178. // Track raw WASM statements so we can finalize them on close.
  179. // node-sqlite3-wasm won't release its file lock if statements are left open.
  180. private _openStmts = new Set<any>();
  181. constructor(dbPath: string) {
  182. // eslint-disable-next-line @typescript-eslint/no-require-imports
  183. const { Database } = require('node-sqlite3-wasm');
  184. this._db = new Database(dbPath);
  185. }
  186. get open(): boolean {
  187. return this._db.isOpen;
  188. }
  189. prepare(sql: string): SqliteStatement {
  190. const { sql: rewrittenSql, paramOrder } = translateNamedParams(sql);
  191. const stmt = this._db.prepare(rewrittenSql);
  192. this._openStmts.add(stmt);
  193. return {
  194. run(...params: any[]) {
  195. const resolved = resolveParams(params, paramOrder);
  196. const result = resolved !== undefined ? stmt.run(resolved) : stmt.run();
  197. return {
  198. changes: result?.changes ?? 0,
  199. lastInsertRowid: result?.lastInsertRowid ?? 0,
  200. };
  201. },
  202. get(...params: any[]) {
  203. // Reads retry on SQLITE_BUSY — the wasm backend has no WAL, so a writer
  204. // in another process can briefly block this read. See issue #238.
  205. return withBusyRetry(() => {
  206. const resolved = resolveParams(params, paramOrder);
  207. return resolved !== undefined ? stmt.get(resolved) : stmt.get();
  208. });
  209. },
  210. all(...params: any[]) {
  211. return withBusyRetry(() => {
  212. const resolved = resolveParams(params, paramOrder);
  213. return resolved !== undefined ? stmt.all(resolved) : stmt.all();
  214. });
  215. },
  216. };
  217. }
  218. exec(sql: string): void {
  219. this._db.exec(sql);
  220. }
  221. pragma(str: string): any {
  222. const trimmed = str.trim();
  223. // Write pragma: "key = value"
  224. if (trimmed.includes('=')) {
  225. const eqIdx = trimmed.indexOf('=');
  226. const key = trimmed.substring(0, eqIdx).trim();
  227. const value = trimmed.substring(eqIdx + 1).trim();
  228. // WAL is not supported in WASM SQLite — use DELETE journal mode
  229. if (key === 'journal_mode' && value.toUpperCase() === 'WAL') {
  230. this._db.exec('PRAGMA journal_mode = DELETE');
  231. return;
  232. }
  233. // mmap is not available in WASM — silently skip
  234. if (key === 'mmap_size') {
  235. return;
  236. }
  237. // synchronous = NORMAL is unsafe without WAL — use FULL
  238. if (key === 'synchronous' && value.toUpperCase() === 'NORMAL') {
  239. this._db.exec('PRAGMA synchronous = FULL');
  240. return;
  241. }
  242. this._db.exec(`PRAGMA ${key} = ${value}`);
  243. return;
  244. }
  245. // Read pragma: "key" — return the value
  246. const stmt = this._db.prepare(`PRAGMA ${trimmed}`);
  247. const result = stmt.get();
  248. stmt.finalize();
  249. return result;
  250. }
  251. transaction<T>(fn: (...args: any[]) => T): (...args: any[]) => T {
  252. return (...args: any[]) => {
  253. this._db.exec('BEGIN');
  254. try {
  255. const result = fn(...args);
  256. this._db.exec('COMMIT');
  257. return result;
  258. } catch (error) {
  259. this._db.exec('ROLLBACK');
  260. throw error;
  261. }
  262. };
  263. }
  264. close(): void {
  265. // Finalize all tracked statements before closing.
  266. // node-sqlite3-wasm won't release its directory-based file lock
  267. // if any prepared statements remain open.
  268. for (const stmt of this._openStmts) {
  269. try { stmt.finalize(); } catch { /* already finalized */ }
  270. }
  271. this._openStmts.clear();
  272. this._db.close();
  273. }
  274. }
  275. /**
  276. * Wraps Node's built-in `node:sqlite` (`DatabaseSync`) to match the
  277. * better-sqlite3 interface.
  278. *
  279. * Unlike the wasm adapter this is REAL SQLite compiled into Node, so it supports
  280. * WAL, FTS5, mmap, and `@named` params natively — the only shims needed are the
  281. * better-sqlite3 conveniences node:sqlite omits: a `.pragma()` helper, a
  282. * `.transaction()` helper, and `open` (node:sqlite exposes `isOpen`). It also
  283. * needs no statement finalization on close (node-sqlite3-wasm did).
  284. *
  285. * Available on Node >= 22.5 (the module is simply absent on older Node, so
  286. * `createDatabase` falls through to wasm there). The API is still flagged
  287. * experimental; `node:sqlite` emits a one-time ExperimentalWarning to stderr on
  288. * load, which is harmless for the MCP stdout protocol.
  289. */
  290. class NodeSqliteAdapter implements SqliteDatabase {
  291. private _db: any;
  292. constructor(dbPath: string) {
  293. // eslint-disable-next-line @typescript-eslint/no-require-imports
  294. const { DatabaseSync } = require('node:sqlite');
  295. this._db = new DatabaseSync(dbPath);
  296. }
  297. get open(): boolean {
  298. return this._db.isOpen;
  299. }
  300. prepare(sql: string): SqliteStatement {
  301. // node:sqlite matches better-sqlite3's calling convention (variadic
  302. // positional args, or a single object for @named params), so params forward
  303. // through unchanged — no positional translation like the wasm adapter needs.
  304. const stmt = this._db.prepare(sql);
  305. return {
  306. run(...params: any[]) {
  307. const r = stmt.run(...params);
  308. return {
  309. changes: Number(r?.changes ?? 0),
  310. lastInsertRowid: r?.lastInsertRowid ?? 0,
  311. };
  312. },
  313. get(...params: any[]) {
  314. return stmt.get(...params);
  315. },
  316. all(...params: any[]) {
  317. return stmt.all(...params);
  318. },
  319. };
  320. }
  321. exec(sql: string): void {
  322. this._db.exec(sql);
  323. }
  324. pragma(str: string): any {
  325. const trimmed = str.trim();
  326. // Write pragma ("key = value"): node:sqlite is real SQLite, so every pragma
  327. // (WAL, mmap, synchronous, …) applies as-is — no special-casing like wasm.
  328. if (trimmed.includes('=')) {
  329. this._db.exec(`PRAGMA ${trimmed}`);
  330. return;
  331. }
  332. // Read pragma: return the row object (e.g. { journal_mode: 'wal' }).
  333. return this._db.prepare(`PRAGMA ${trimmed}`).get();
  334. }
  335. transaction<T>(fn: (...args: any[]) => T): (...args: any[]) => T {
  336. return (...args: any[]) => {
  337. this._db.exec('BEGIN');
  338. try {
  339. const result = fn(...args);
  340. this._db.exec('COMMIT');
  341. return result;
  342. } catch (error) {
  343. this._db.exec('ROLLBACK');
  344. throw error;
  345. }
  346. };
  347. }
  348. close(): void {
  349. this._db.close();
  350. }
  351. }
  352. /**
  353. * Concise stderr notice shown when better-sqlite3 is unavailable but Node's
  354. * built-in node:sqlite is, so we use that instead of the slow wasm fallback.
  355. * Unlike wasm, node:sqlite has full WAL + FTS5 and near-native speed, so this is
  356. * informational — not a "fix me" warning. Exported for tests.
  357. */
  358. export function buildNodeSqliteNotice(nativeError?: string): string {
  359. const lines = [
  360. '[CodeGraph] better-sqlite3 unavailable — using the built-in node:sqlite backend.',
  361. 'Full WAL + FTS5 support, no native build required. To restore the (fastest)',
  362. `native backend: ${WASM_FALLBACK_FIX_RECIPE}`,
  363. ];
  364. if (nativeError) lines.push(`(better-sqlite3 load error: ${nativeError})`);
  365. return lines.join('\n') + '\n';
  366. }
  367. /**
  368. * Create a database connection, trying backends in order of preference:
  369. * 1. better-sqlite3 (native) — fastest, but needs a compiled binding
  370. * 2. node:sqlite (Node ≥22.5) — real WAL + FTS5, no native build, no wasm
  371. * 3. node-sqlite3-wasm — last resort (no WAL); only ancient Node
  372. *
  373. * node:sqlite sits ahead of wasm so that when the native binding fails to load
  374. * (common on Windows / locked-down CI), users land on a backend WITH WAL instead
  375. * of the no-WAL wasm path that causes concurrent-read lock errors (issue #238).
  376. *
  377. * `CODEGRAPH_SQLITE_BACKEND=native|node-sqlite|wasm` forces a single backend
  378. * (used for A/B testing and to opt into node:sqlite); a forced backend that
  379. * can't load throws rather than silently falling through.
  380. *
  381. * Returns the active backend alongside the db so each `DatabaseConnection` can
  382. * report its own backend per-instance — MCP can open multiple project DBs in one
  383. * process, so a process-global would race / overwrite.
  384. */
  385. export function createDatabase(dbPath: string): { db: SqliteDatabase; backend: SqliteBackend } {
  386. const forced = (process.env.CODEGRAPH_SQLITE_BACKEND || '').trim().toLowerCase();
  387. const errors: { native?: string; nodeSqlite?: string; wasm?: string } = {};
  388. const toMsg = (e: unknown) => (e instanceof Error ? e.message : String(e));
  389. const tryNative = !forced || forced === 'native';
  390. const tryNodeSqlite = !forced || forced === 'node-sqlite' || forced === 'node:sqlite';
  391. const tryWasm = !forced || forced === 'wasm';
  392. // 1. Native better-sqlite3
  393. if (tryNative) {
  394. try {
  395. // eslint-disable-next-line @typescript-eslint/no-require-imports
  396. const Database = require('better-sqlite3');
  397. return { db: new Database(dbPath) as SqliteDatabase, backend: 'native' };
  398. } catch (error) {
  399. errors.native = toMsg(error);
  400. }
  401. }
  402. // 2. Node's built-in node:sqlite (real WAL, no native build)
  403. if (tryNodeSqlite) {
  404. try {
  405. const db = new NodeSqliteAdapter(dbPath);
  406. // Announce only when this is a genuine fallback (native was tried & failed),
  407. // not when the caller explicitly forced node-sqlite.
  408. if (!forced && errors.native) {
  409. process.stderr.write(buildNodeSqliteNotice(errors.native));
  410. }
  411. return { db, backend: 'node-sqlite' };
  412. } catch (error) {
  413. errors.nodeSqlite = toMsg(error);
  414. }
  415. }
  416. // 3. WASM (no WAL) — last resort
  417. if (tryWasm) {
  418. try {
  419. const db = new WasmDatabaseAdapter(dbPath);
  420. console.warn(buildWasmFallbackBanner(errors.native));
  421. return { db, backend: 'wasm' };
  422. } catch (error) {
  423. errors.wasm = toMsg(error);
  424. }
  425. }
  426. throw new Error(
  427. `Failed to load a SQLite backend.\n` +
  428. (errors.native ? ` Native (better-sqlite3): ${errors.native}\n` : '') +
  429. (errors.nodeSqlite ? ` node:sqlite: ${errors.nodeSqlite}\n` : '') +
  430. (errors.wasm ? ` WASM (node-sqlite3-wasm): ${errors.wasm}\n` : '') +
  431. (forced ? ` (CODEGRAPH_SQLITE_BACKEND=${forced} restricted which backends were tried)` : '')
  432. );
  433. }