Skip to content

Commit f923c0c

Browse files
thedotmackclaude
andcommitted
fix: complete better-sqlite3 to bun:sqlite migration
Must Fix: - Remove better-sqlite3 logic from smart-install.js (5 sections) - Update all documentation to reference bun:sqlite (7 files) Should Fix: - Add defensive break statement in worker-cli.ts:38 Nice to Have: - Add port validation in ProcessManager.start() (1024-65535) - Add one-time marker for PM2 cleanup migration - Verify clearPortCache() wiring (already correct) Addresses PR #248 review feedback (comment #3648517713) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
1 parent 1491123 commit f923c0c

File tree

19 files changed

+61
-171
lines changed

19 files changed

+61
-171
lines changed

docs/public/architecture-evolution.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ if (currentVersion !== installedVersion) {
151151
**Cached Check Logic**:
152152
1. Does `node_modules` exist?
153153
2. Does `.install-version` match `package.json` version?
154-
3. Is `better-sqlite3` present?
154+
3. Is `better-sqlite3` present? (Legacy: now uses bun:sqlite which requires no installation)
155155
156156
**Impact**:
157157
- SessionStart hook: 2-5 seconds → 10ms (99.5% faster)

docs/public/architecture/database.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: "SQLite schema, FTS5 search, and data storage"
55

66
# Database Architecture
77

8-
Claude-Mem uses SQLite 3 with the better-sqlite3 native module for persistent storage and FTS5 for full-text search.
8+
Claude-Mem uses SQLite 3 with the bun:sqlite native module for persistent storage and FTS5 for full-text search.
99

1010
## Database Location
1111

@@ -15,7 +15,7 @@ Claude-Mem uses SQLite 3 with the better-sqlite3 native module for persistent st
1515

1616
## Database Implementation
1717

18-
**Primary Implementation**: better-sqlite3 (native SQLite module)
18+
**Primary Implementation**: bun:sqlite (native SQLite module)
1919
- Used by: SessionStore and SessionSearch
2020
- Format: Synchronous API with better performance
2121
- **Note**: Database.ts (using bun:sqlite) is legacy code
@@ -301,8 +301,8 @@ Database schema is managed via migrations in `src/services/sqlite/migrations.ts`
301301
- **Indexes**: All foreign keys and frequently queried columns are indexed
302302
- **FTS5**: Full-text search is significantly faster than LIKE queries
303303
- **Triggers**: Automatic synchronization has minimal overhead
304-
- **Connection Pooling**: better-sqlite3 reuses connections efficiently
305-
- **Synchronous API**: better-sqlite3 uses synchronous API for better performance
304+
- **Connection Pooling**: bun:sqlite reuses connections efficiently
305+
- **Synchronous API**: bun:sqlite uses synchronous API for better performance
306306

307307
## Troubleshooting
308308

docs/public/architecture/overview.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Claude-Mem operates as a Claude Code plugin with five core components:
2222
|------------------------|-------------------------------------------|
2323
| **Language** | TypeScript (ES2022, ESNext modules) |
2424
| **Runtime** | Node.js 18+ |
25-
| **Database** | SQLite 3 with better-sqlite3 driver |
25+
| **Database** | SQLite 3 with bun:sqlite driver |
2626
| **Vector Store** | ChromaDB (optional, for semantic search) |
2727
| **HTTP Server** | Express.js 4.18 |
2828
| **Real-time** | Server-Sent Events (SSE) |
@@ -205,7 +205,7 @@ Express.js HTTP server on port 37777 (configurable) with:
205205
See [Worker Service](/architecture/worker-service) for HTTP API and endpoints.
206206

207207
### 3. Database Layer
208-
SQLite3 with better-sqlite3 driver featuring:
208+
SQLite3 with bun:sqlite driver featuring:
209209
- FTS5 virtual tables for full-text search
210210
- SessionStore for CRUD operations
211211
- SessionSearch for FTS5 queries

docs/public/hooks-architecture.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ Claude-Mem uses 6 lifecycle hook scripts across 5 lifecycle events, plus 1 pre-h
8585
2. Only runs `npm install` when necessary:
8686
- First-time installation
8787
- Version changed in package.json
88-
- Critical dependency missing (better-sqlite3)
8988
3. Provides Windows-specific error messages
9089
4. Starts Bun worker service
9190

plugin/scripts/cleanup-hook.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/scripts/context-hook.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/scripts/new-hook.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/scripts/save-hook.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/scripts/smart-install.js

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { existsSync, readFileSync, writeFileSync } from 'fs';
1717
import { execSync } from 'child_process';
1818
import { join, dirname } from 'path';
1919
import { homedir } from 'os';
20-
import { createRequire } from 'module';
2120
import { fileURLToPath } from 'url';
2221

2322
// Determine the directory where THIS script is running from
@@ -41,7 +40,6 @@ const PLUGIN_ROOT = IS_RUNNING_FROM_CACHE
4140
const PACKAGE_JSON_PATH = join(PLUGIN_ROOT, 'package.json');
4241
const VERSION_MARKER_PATH = join(PLUGIN_ROOT, '.install-version');
4342
const NODE_MODULES_PATH = join(PLUGIN_ROOT, 'node_modules');
44-
const BETTER_SQLITE3_PATH = join(NODE_MODULES_PATH, 'better-sqlite3');
4543

4644
// Colors for output
4745
const colors = {
@@ -126,12 +124,6 @@ function needsInstall() {
126124
return true;
127125
}
128126

129-
// Check if better-sqlite3 is installed
130-
if (!existsSync(BETTER_SQLITE3_PATH)) {
131-
log('📦 better-sqlite3 missing - reinstalling', colors.cyan);
132-
return true;
133-
}
134-
135127
// Check version marker
136128
const currentPackageVersion = getPackageVersion();
137129
const currentNodeVersion = getNodeVersion();
@@ -165,101 +157,6 @@ function needsInstall() {
165157
return false;
166158
}
167159

168-
/**
169-
* Verify that better-sqlite3 native module loads correctly
170-
* This catches ABI mismatches and corrupted builds
171-
*/
172-
async function verifyNativeModules() {
173-
try {
174-
log('🔍 Verifying native modules...', colors.dim);
175-
176-
// Use createRequire() to resolve from PLUGIN_ROOT's node_modules
177-
const require = createRequire(join(PLUGIN_ROOT, 'package.json'));
178-
const Database = require('better-sqlite3');
179-
180-
// Try to create a test in-memory database
181-
const db = new Database(':memory:');
182-
183-
// Run a simple query to ensure it works
184-
const result = db.prepare('SELECT 1 + 1 as result').get();
185-
186-
// Clean up
187-
db.close();
188-
189-
if (result.result !== 2) {
190-
throw new Error('SQLite math check failed');
191-
}
192-
193-
log('✓ Native modules verified', colors.dim);
194-
return true;
195-
196-
} catch (error) {
197-
if (error.code === 'ERR_DLOPEN_FAILED') {
198-
log('⚠️ Native module ABI mismatch detected', colors.yellow);
199-
return false;
200-
}
201-
202-
// Other errors are unexpected - log and fail
203-
log(`❌ Native module verification failed: ${error.message}`, colors.red);
204-
return false;
205-
}
206-
}
207-
208-
function getWindowsErrorHelp(errorOutput) {
209-
// Detect Python version at runtime
210-
let pythonStatus = ' Python not detected or version unknown';
211-
try {
212-
const pythonVersion = execSync('python --version', { encoding: 'utf-8', stdio: 'pipe' }).trim();
213-
const versionMatch = pythonVersion.match(/Python\s+([\d.]+)/);
214-
if (versionMatch) {
215-
pythonStatus = ` You have ${versionMatch[0]} installed ✓`;
216-
}
217-
} catch (error) {
218-
// Python not available or failed to detect - use default message
219-
}
220-
221-
const help = [
222-
'',
223-
'╔══════════════════════════════════════════════════════════════════════╗',
224-
'║ Windows Installation Help ║',
225-
'╚══════════════════════════════════════════════════════════════════════╝',
226-
'',
227-
'📋 better-sqlite3 requires build tools to compile native modules.',
228-
'',
229-
'🔧 Option 1: Install Visual Studio Build Tools (Recommended)',
230-
' 1. Download: https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2022',
231-
' 2. Install "Desktop development with C++"',
232-
' 3. Restart your terminal',
233-
' 4. Try again',
234-
'',
235-
'🔧 Option 2: Install via npm (automated)',
236-
' Run as Administrator:',
237-
' npm install --global windows-build-tools',
238-
'',
239-
'🐍 Python Requirement:',
240-
' Python 3.6+ is required.',
241-
pythonStatus,
242-
'',
243-
];
244-
245-
// Check for specific error patterns
246-
if (errorOutput.includes('MSBuild.exe')) {
247-
help.push('❌ MSBuild not found - install Visual Studio Build Tools');
248-
}
249-
if (errorOutput.includes('MSVS')) {
250-
help.push('❌ Visual Studio not detected - install Build Tools');
251-
}
252-
if (errorOutput.includes('permission') || errorOutput.includes('EPERM')) {
253-
help.push('❌ Permission denied - try running as Administrator');
254-
}
255-
256-
help.push('');
257-
help.push('📖 Full documentation: https://github.com/WiseLibs/better-sqlite3/blob/master/docs/troubleshooting.md');
258-
help.push('');
259-
260-
return help.join('\n');
261-
}
262-
263160
async function runNpmInstall() {
264161
const isWindows = process.platform === 'win32';
265162

@@ -310,11 +207,6 @@ async function runNpmInstall() {
310207
log('❌ Installation failed after retrying!', colors.bright);
311208
log('', colors.reset);
312209

313-
// Provide Windows-specific help
314-
if (isWindows && lastError && lastError.message && lastError.message.includes('better-sqlite3')) {
315-
log(getWindowsErrorHelp(lastError.message), colors.yellow);
316-
}
317-
318210
// Show generic error info with troubleshooting steps
319211
if (lastError) {
320212
if (lastError.stderr) {
@@ -360,21 +252,6 @@ async function main() {
360252
log('', colors.reset);
361253
process.exit(1);
362254
}
363-
} else {
364-
// Even if install not needed, verify native modules work
365-
const nativeModulesWork = await verifyNativeModules();
366-
367-
if (!nativeModulesWork) {
368-
log('📦 Native modules need rebuild - reinstalling', colors.cyan);
369-
const installSuccess = await runNpmInstall();
370-
371-
if (!installSuccess) {
372-
log('', colors.red);
373-
log('⚠️ Native module rebuild failed', colors.yellow);
374-
log('', colors.reset);
375-
process.exit(1);
376-
}
377-
}
378255
}
379256

380257
// NOTE: Worker auto-start disabled in smart-install.js

0 commit comments

Comments
 (0)