ruvnet / ruvnet/ruflo

Bug Report: SQLite Storage Always Falls Back to In-Memory Due to Logic Error

Open Beginner friendly
#442 2 comments 8 reactions 0 assignees View on GitHub
already-fixed
Dominant language
TypeScript
Stars
72.7k
Forks
8.6k
Avg merge
2d 23h
Merged PRs (30d)
83

Description

## Summary
Claude Flow always uses in-memory storage instead of SQLite due to a logic error in the `isSQLiteAvailable()` function, even when better-sqlite3 is properly installed and functional.

## Environment
- **Claude Flow Version**: v2.0.0-alpha.67
- **Node.js Version**: v20.19.4
- **Operating System**: Linux (Ubuntu in GitHub Codespaces)
- **Installation Method**: Both `npx claude-flow@alpha` and global install via `npm install -g claude-flow@alpha`

## Bug Description
The `isSQLiteAvailable()` function in `dist/memory/sqlite-wrapper.js` has a logic error that prevents SQLite from ever being loaded:

```javascript
// Line 11: sqliteAvailable is initialized as 'false'
let sqliteAvailable = false;

// Line 78: This check prevents SQLite from ever being loaded
export async function isSQLiteAvailable() {
if (sqliteAvailable !== null) { // false !== null is true
return sqliteAvailable; // Always returns false!
}
await tryLoadSQLite(); // This line is never reached
return sqliteAvailable;
}
```

Since `sqliteAvailable` is initialized as `false` and `false !== null` evaluates to `true`, the function always returns `false` without attempting to load SQLite.

## Expected Behavior
- When better-sqlite3 is available, Claude Flow should use SQLite for persistent storage
- Data should persist across sessions in `~/.claude-flow/memory.db`
- The fallback to in-memory storage should only occur when SQLite genuinely fails to load

## Actual Behavior
- Claude Flow always uses in-memory storage
- Log shows: `[WARN] [fallback-store] SQLite module not available: Unknown error`
- Data is lost between sessions
- SQLite is never attempted to be loaded due to the early return

## Steps to Reproduce
1. Install Claude Flow: `npm install -g claude-flow@alpha`
2. Start MCP server: `claude-flow mcp start`
3. Store data using `memory_usage` tool
4. Check logs - will show SQLite fallback warning
5. Restart server - previously stored data is lost

## Root Cause Analysis
The issue is in `/dist/memory/sqlite-wrapper.js`:

1. `sqliteAvailable` is initialized as `false` (line 11)
2. The `isSQLiteAvailable()` function checks `if (sqliteAvailable !== null)` (line 78)
3. Since `false !== null` is `true`, it returns `false` immediately
4. The `tryLoadSQLite()` function is never called
5. SQLite is never loaded, even though better-sqlite3 is installed and functional

## Proposed Fix
Change line 11 from:
```javascript
let sqliteAvailable = false;
```
To:
```javascript
let sqliteAvailable = null;
```

This allows the null check to work correctly and SQLite loading to be attempted.

## Verification
After applying the fix:
```javascript
// Test that SQLite loads correctly
node -e "import('./node_modules/claude-flow/dist/memory/sqlite-wrapper.js').then(m => m.isSQLiteAvailable()).then(console.log)"
// Output: true (instead of false)
```

## Impact
- All users are affected - nobody can use persistent SQLite storage
- Data is lost between sessions
- The feature appears broken even in environments where SQLite works perfectly

## Additional Notes
- better-sqlite3 is properly installed in node_modules
- The module loads successfully when tested directly
- The native binary is compatible with the Node.js version
- This is purely a logic error, not a compatibility issue

## Workaround
Users can manually edit `node_modules/claude-flow/dist/memory/sqlite-wrapper.js` and change line 11 as described above, but this is not ideal as it modifies the installed package.`

Contributor guide

Open the contributing guide

Research direction

Start in dist/memory/sqlite-wrapper.js by reading isSQLiteAvailable() and the sqliteAvailable initialization, then run the provided Node.js verification command. Done means SQLite availability is actually attempted when better-sqlite3 is installed, returns true in that environment, and persistent storage no longer falls back unnecessarily.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, sqlite, typescript
Domain
databases
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
82/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.