Skip to content

Commit 55daeff

Browse files
colbymchenryclaude
andauthored
fix(db): surface SQLite backend in status + actionable WASM-fallback banner (colbymchenry#148)
Closes the visibility gap behind issues colbymchenry#138 (WASM-on-macOS) and colbymchenry#139 (MCP "database is locked"). `better-sqlite3` is in optionalDependencies, so when the native build fails npm install still succeeds and the runtime silently falls back to node-sqlite3-wasm — 5-10x slower and without WAL, so writers block readers (which is what makes the MCP server appear to "lock the DB" in colbymchenry#139). The only existing signal was a one-line `console.warn` to stderr that MCP transports typically swallow. This patch does NOT change install behavior — better-sqlite3 stays in optionalDependencies so cross-platform installs keep working. It just makes the substitution observable + recoverable. ## Visibility (4 surfaces) - CLI `codegraph status`: new `Backend:` line under Index Statistics. `native` rendered green; `wasm` rendered yellow with an inline `npm rebuild better-sqlite3` nudge. Also exposed in `--json` as `backend: 'native' | 'wasm'`. - MCP `codegraph_status`: new `**Backend:**` line. Native form reads `native (better-sqlite3)`; wasm form prepends a warning glyph and includes the full fix recipe. - Stderr banner on fallback (`buildWasmFallbackBanner`): replaces the bare one-line `console.warn` with a multi-line bordered banner covering macOS + Linux fix steps and optionally appending the native load error. - README troubleshooting: new "Indexing is slow / MCP database is locked / WASM fallback active" entry that walks users to the `Backend:` line and the fix. ## Per-instance backend tracking `createDatabase` previously set a module-level `activeBackend` global. MCP can open multiple project DBs in one process via the `getCodeGraph()` cache, so the global would race / overwrite. Refactor: `createDatabase` now returns `{db, backend}`, `DatabaseConnection` carries `private backend` and exposes `getBackend()`, and `CodeGraph.getBackend()` is the public surface. The CLI and MCP both call `cg.getBackend()`. ## What this does NOT fix The root cause of users landing on WASM is environment-specific (Mac without Xcode CLT, Node version mismatch, etc.) and not fixable in code without changing the optionalDependencies design. The README entry tells users what to run; `Backend: native` after rebuild is the confirmation signal. ## Tests New `__tests__/sqlite-backend.test.ts` (6 tests) pins the banner recipe content (so future edits can't strip the recovery commands), the `WASM_FALLBACK_FIX_RECIPE` constant, and per-instance `DatabaseConnection.getBackend()` / `CodeGraph.getBackend()` reporting. Suite: 503 → 509, all passing. Credit to @andreinknv whose analysis on colbymchenry#138 (and patches on his fork at 6d0e7a2 + 69f7001) framed the visibility approach. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent af7abd5 commit 55daeff

7 files changed

Lines changed: 215 additions & 20 deletions

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,30 @@ The `.codegraph/config.json` file controls indexing:
439439

440440
**Indexing is slow** — Check that `node_modules` and other large directories are excluded. Use `--quiet` to reduce output overhead.
441441

442+
**Indexing is slow / MCP `database is locked` / WASM fallback active**`codegraph` ships with a WASM SQLite fallback for environments where `better-sqlite3` (a native module, declared as `optionalDependencies`) can't install. The fallback is 5-10x slower than the native backend and uses a journal mode that lets writers block readers, so MCP queries can also hit `database is locked` while indexing runs. Run `codegraph status` and look at the `Backend:` line:
443+
444+
- `Backend: native` — you're on the fast path, nothing to do.
445+
- `Backend: wasm` — you're on the slow fallback. Common causes: missing C build tools, prebuilt binary unavailable for your Node version, or your Node version changed after install. Fix:
446+
447+
```bash
448+
# macOS
449+
xcode-select --install # installs the C compiler
450+
451+
# Linux (Debian / Ubuntu)
452+
sudo apt install build-essential python3 make
453+
454+
# Linux (RHEL / Fedora)
455+
sudo yum groupinstall "Development Tools"
456+
457+
# Then rebuild on any platform:
458+
npm rebuild better-sqlite3
459+
460+
# Or force-include as a hard dep:
461+
npm install better-sqlite3 --save
462+
```
463+
464+
After the fix, `codegraph status` should show `Backend: native`.
465+
442466
**MCP server not connecting** — Ensure the project is initialized/indexed, verify the path in your MCP config, and check that `codegraph serve --mcp` works from the command line.
443467

444468
**Missing symbols** — The MCP server auto-syncs on save (wait a couple seconds). Run `codegraph sync` manually if needed. Check that the file's language is supported and isn't excluded by config patterns.

__tests__/sqlite-backend.test.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* SQLite backend visibility tests
3+
*
4+
* Pins the WASM-fallback banner content + the per-instance backend
5+
* tracking. Closes the visibility gap behind issue #138.
6+
*/
7+
8+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
9+
import * as fs from 'fs';
10+
import * as path from 'path';
11+
import * as os from 'os';
12+
import {
13+
buildWasmFallbackBanner,
14+
WASM_FALLBACK_FIX_RECIPE,
15+
} from '../src/db/sqlite-adapter';
16+
import { DatabaseConnection } from '../src/db';
17+
import { CodeGraph } from '../src';
18+
19+
describe('buildWasmFallbackBanner — fix-recipe content', () => {
20+
it('includes the macOS / Linux / cross-platform fix commands', () => {
21+
const banner = buildWasmFallbackBanner();
22+
expect(banner).toContain('WASM SQLite fallback active');
23+
expect(banner).toContain('5-10x slower');
24+
expect(banner).toContain('xcode-select --install');
25+
expect(banner).toContain('apt install build-essential');
26+
expect(banner).toContain('npm rebuild better-sqlite3');
27+
expect(banner).toContain('npm install better-sqlite3 --save');
28+
expect(banner).toContain('codegraph status');
29+
});
30+
31+
it('appends the native load error when one is provided', () => {
32+
const banner = buildWasmFallbackBanner(
33+
"Cannot find module 'better-sqlite3'"
34+
);
35+
expect(banner).toContain(
36+
"Native load error: Cannot find module 'better-sqlite3'"
37+
);
38+
});
39+
40+
it('omits the load-error block when no error is supplied', () => {
41+
const banner = buildWasmFallbackBanner();
42+
expect(banner).not.toContain('Native load error:');
43+
});
44+
});
45+
46+
describe('WASM_FALLBACK_FIX_RECIPE — single source of truth', () => {
47+
it('mentions the three recovery commands', () => {
48+
expect(WASM_FALLBACK_FIX_RECIPE).toContain('xcode-select --install');
49+
expect(WASM_FALLBACK_FIX_RECIPE).toContain('npm rebuild better-sqlite3');
50+
expect(WASM_FALLBACK_FIX_RECIPE).toContain(
51+
'npm install better-sqlite3 --save'
52+
);
53+
});
54+
});
55+
56+
describe('DatabaseConnection — per-instance backend reporting', () => {
57+
let dir: string;
58+
59+
beforeEach(() => {
60+
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-backend-'));
61+
});
62+
63+
afterEach(() => {
64+
if (fs.existsSync(dir)) {
65+
fs.rmSync(dir, { recursive: true, force: true });
66+
}
67+
});
68+
69+
it('reports a concrete backend (native or wasm) for an initialized DB', () => {
70+
const dbPath = path.join(dir, 'test.db');
71+
const conn = DatabaseConnection.initialize(dbPath);
72+
const backend = conn.getBackend();
73+
expect(['native', 'wasm']).toContain(backend);
74+
conn.close();
75+
});
76+
77+
it('CodeGraph.getBackend() delegates to the underlying DatabaseConnection', async () => {
78+
fs.writeFileSync(path.join(dir, 'x.ts'), `export function x(): void {}\n`);
79+
const cg = await CodeGraph.init(dir, { index: true });
80+
try {
81+
expect(['native', 'wasm']).toContain(cg.getBackend());
82+
} finally {
83+
cg.destroy();
84+
}
85+
});
86+
});

src/bin/codegraph.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ program
643643
const cg = await CodeGraph.open(projectPath);
644644
const stats = cg.getStats();
645645
const changes = cg.getChangedFiles();
646+
const backend = cg.getBackend();
646647

647648
// JSON output mode
648649
if (options.json) {
@@ -653,6 +654,7 @@ program
653654
nodeCount: stats.nodeCount,
654655
edgeCount: stats.edgeCount,
655656
dbSizeBytes: stats.dbSizeBytes,
657+
backend,
656658
nodesByKind: stats.nodesByKind,
657659
languages: Object.entries(stats.filesByLanguage).filter(([, count]) => count > 0).map(([lang]) => lang),
658660
pendingChanges: {
@@ -677,6 +679,14 @@ program
677679
console.log(` Nodes: ${formatNumber(stats.nodeCount)}`);
678680
console.log(` Edges: ${formatNumber(stats.edgeCount)}`);
679681
console.log(` DB Size: ${(stats.dbSizeBytes / 1024 / 1024).toFixed(2)} MB`);
682+
// Surface the active SQLite backend so users can spot the silent
683+
// WASM fallback (5-10x slower). better-sqlite3 is in
684+
// `optionalDependencies`, so `npm install` succeeds without it
685+
// when the native build fails.
686+
const backendLabel = backend === 'native'
687+
? chalk.green('native')
688+
: chalk.yellow('wasm — slower fallback; run `npm rebuild better-sqlite3`');
689+
console.log(` Backend: ${backendLabel}`);
680690
console.log();
681691

682692
// Node breakdown

src/db/index.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@
44
* Handles SQLite database initialization and connection management.
55
*/
66

7-
import { SqliteDatabase, createDatabase } from './sqlite-adapter';
7+
import { SqliteDatabase, SqliteBackend, createDatabase } from './sqlite-adapter';
88
import * as fs from 'fs';
99
import * as path from 'path';
1010
import { SchemaVersion } from '../types';
1111
import { runMigrations, getCurrentVersion, CURRENT_SCHEMA_VERSION } from './migrations';
1212

13-
export { SqliteDatabase, getActiveBackend } from './sqlite-adapter';
13+
export { SqliteDatabase, SqliteBackend, WASM_FALLBACK_FIX_RECIPE } from './sqlite-adapter';
1414

1515
/**
1616
* Database connection wrapper with lifecycle management
1717
*/
1818
export class DatabaseConnection {
1919
private db: SqliteDatabase;
2020
private dbPath: string;
21+
private backend: SqliteBackend;
2122

22-
private constructor(db: SqliteDatabase, dbPath: string) {
23+
private constructor(db: SqliteDatabase, dbPath: string, backend: SqliteBackend) {
2324
this.db = db;
2425
this.dbPath = dbPath;
26+
this.backend = backend;
2527
}
2628

2729
/**
@@ -35,7 +37,7 @@ export class DatabaseConnection {
3537
}
3638

3739
// Create and configure database
38-
const db = createDatabase(dbPath);
40+
const { db, backend } = createDatabase(dbPath);
3941

4042
// Enable foreign keys and WAL mode for better performance
4143
db.pragma('foreign_keys = ON');
@@ -62,7 +64,7 @@ export class DatabaseConnection {
6264
).run(CURRENT_SCHEMA_VERSION, Date.now(), 'Initial schema includes all migrations');
6365
}
6466

65-
return new DatabaseConnection(db, dbPath);
67+
return new DatabaseConnection(db, dbPath, backend);
6668
}
6769

6870
/**
@@ -73,7 +75,7 @@ export class DatabaseConnection {
7375
throw new Error(`Database not found: ${dbPath}`);
7476
}
7577

76-
const db = createDatabase(dbPath);
78+
const { db, backend } = createDatabase(dbPath);
7779

7880
// Enable foreign keys and WAL mode
7981
db.pragma('foreign_keys = ON');
@@ -88,7 +90,7 @@ export class DatabaseConnection {
8890
db.pragma('mmap_size = 268435456');
8991

9092
// Check and run migrations if needed
91-
const conn = new DatabaseConnection(db, dbPath);
93+
const conn = new DatabaseConnection(db, dbPath, backend);
9294
const currentVersion = getCurrentVersion(db);
9395

9496
if (currentVersion < CURRENT_SCHEMA_VERSION) {
@@ -105,6 +107,15 @@ export class DatabaseConnection {
105107
return this.db;
106108
}
107109

110+
/**
111+
* Get the SQLite backend serving this connection. Per-instance so
112+
* MCP cross-project queries report the right backend even when
113+
* multiple project DBs are open in the same process.
114+
*/
115+
getBackend(): SqliteBackend {
116+
return this.backend;
117+
}
118+
108119
/**
109120
* Get database file path
110121
*/

src/db/sqlite-adapter.ts

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,51 @@ export interface SqliteDatabase {
2222

2323
export type SqliteBackend = 'native' | 'wasm';
2424

25-
let activeBackend: SqliteBackend | null = null;
25+
/**
26+
* One-line summary of the recovery steps shown when WASM fallback is
27+
* active. Single source of truth so the recipe can't drift between the
28+
* stderr banner and the MCP status formatter.
29+
*/
30+
export const WASM_FALLBACK_FIX_RECIPE =
31+
'`xcode-select --install` (macOS) or `apt install build-essential` (Debian/Ubuntu), ' +
32+
'then `npm rebuild better-sqlite3`, or `npm install better-sqlite3 --save` to force-include it.';
2633

2734
/**
28-
* Get the currently active SQLite backend.
35+
* Multi-line banner shown to stderr when `createDatabase` falls back to
36+
* WASM. Replaces a one-line `console.warn` that MCP transports (which
37+
* take stdout for the protocol) typically swallow, leaving users on a
38+
* 5-10x slower backend with no signal.
39+
*
40+
* Exported for unit testing — pinning the recipe content prevents
41+
* future edits from silently stripping the recovery commands.
2942
*/
30-
export function getActiveBackend(): SqliteBackend | null {
31-
return activeBackend;
43+
export function buildWasmFallbackBanner(nativeError?: string): string {
44+
const sep = '─'.repeat(72);
45+
const lines = [
46+
sep,
47+
'[CodeGraph] WASM SQLite fallback active (better-sqlite3 unavailable)',
48+
sep,
49+
'Indexing and sync will be 5-10x slower than the native backend.',
50+
'',
51+
'Fix on macOS:',
52+
' xcode-select --install # install C build tools',
53+
' npm rebuild better-sqlite3 # rebuild native binding for current Node',
54+
'',
55+
'Fix on Linux:',
56+
' sudo apt install build-essential python3 make # Debian/Ubuntu',
57+
' # or: sudo yum groupinstall "Development Tools" # RHEL/Fedora',
58+
' npm rebuild better-sqlite3',
59+
'',
60+
'Or force-include as a hard dependency on any platform:',
61+
' npm install better-sqlite3 --save',
62+
'',
63+
'Verify after fix: `codegraph status` should show `Backend: native`.',
64+
];
65+
if (nativeError) {
66+
lines.push('', `Native load error: ${nativeError}`);
67+
}
68+
lines.push(sep);
69+
return lines.join('\n');
3270
}
3371

3472
/**
@@ -192,9 +230,13 @@ class WasmDatabaseAdapter implements SqliteDatabase {
192230

193231
/**
194232
* Create a database connection. Tries native better-sqlite3 first,
195-
* falls back to node-sqlite3-wasm.
233+
* falls back to node-sqlite3-wasm. Returns the active backend
234+
* alongside the db so each `DatabaseConnection` can report its own
235+
* backend per-instance — MCP can open multiple project DBs in one
236+
* process (`tools.ts` getCodeGraph cache), so a process-global would
237+
* race / overwrite.
196238
*/
197-
export function createDatabase(dbPath: string): SqliteDatabase {
239+
export function createDatabase(dbPath: string): { db: SqliteDatabase; backend: SqliteBackend } {
198240
let nativeError: string | undefined;
199241
let wasmError: string | undefined;
200242

@@ -203,18 +245,16 @@ export function createDatabase(dbPath: string): SqliteDatabase {
203245
// eslint-disable-next-line @typescript-eslint/no-require-imports
204246
const Database = require('better-sqlite3');
205247
const db = new Database(dbPath);
206-
activeBackend = 'native';
207-
return db as SqliteDatabase;
248+
return { db: db as SqliteDatabase, backend: 'native' };
208249
} catch (error) {
209250
nativeError = error instanceof Error ? error.message : String(error);
210251
}
211252

212253
// Fall back to WASM
213254
try {
214255
const db = new WasmDatabaseAdapter(dbPath);
215-
activeBackend = 'wasm';
216-
console.warn('[CodeGraph] Using WASM SQLite backend (native better-sqlite3 unavailable)');
217-
return db;
256+
console.warn(buildWasmFallbackBanner(nativeError));
257+
return { db, backend: 'wasm' };
218258
} catch (error) {
219259
wasmError = error instanceof Error ? error.message : String(error);
220260
}

src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,16 @@ export class CodeGraph {
612612
return stats;
613613
}
614614

615+
/**
616+
* Active SQLite backend for this project's connection. `wasm` means
617+
* the native better-sqlite3 install failed and the WASM fallback is
618+
* serving requests at 5-10x the latency. Surfaced via `codegraph
619+
* status` and the `codegraph_status` MCP tool.
620+
*/
621+
getBackend(): import('./db').SqliteBackend {
622+
return this.db.getBackend();
623+
}
624+
615625
// ===========================================================================
616626
// Node Operations
617627
// ===========================================================================

src/mcp/tools.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { writeFileSync, readFileSync, existsSync } from 'fs';
1111
import { clamp, validatePathWithinRoot } from '../utils';
1212
import { tmpdir } from 'os';
1313
import { join } from 'path';
14+
import { WASM_FALLBACK_FIX_RECIPE } from '../db';
1415

1516
/** Maximum output length to prevent context bloat (characters) */
1617
const MAX_OUTPUT_LENGTH = 15000;
@@ -973,10 +974,23 @@ export class ToolHandler {
973974
`**Total nodes:** ${stats.nodeCount}`,
974975
`**Total edges:** ${stats.edgeCount}`,
975976
`**Database size:** ${(stats.dbSizeBytes / 1024 / 1024).toFixed(2)} MB`,
976-
'',
977-
'### Nodes by Kind:',
978977
];
979978

979+
// Surface the active SQLite backend. Without this, users on the
980+
// silent WASM fallback (better-sqlite3 install failed) see "slow"
981+
// indexing and DB-lock errors with no signal of why.
982+
const backend = cg.getBackend();
983+
if (backend === 'native') {
984+
lines.push(`**Backend:** native (better-sqlite3)`);
985+
} else {
986+
lines.push(
987+
`**Backend:** ⚠ wasm (better-sqlite3 unavailable) — ` +
988+
`5-10x slower than native. Fix: ${WASM_FALLBACK_FIX_RECIPE}`
989+
);
990+
}
991+
992+
lines.push('', '### Nodes by Kind:');
993+
980994
for (const [kind, count] of Object.entries(stats.nodesByKind)) {
981995
if ((count as number) > 0) {
982996
lines.push(`- ${kind}: ${count}`);

0 commit comments

Comments
 (0)