π The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix: Handle native binding errors gracefully in memory and hive-mind commands
Clauded187232c2352
diff --git a/bin/hive-mind.js b/bin/hive-mind.jsindex 6e8c9b2239..2902449d27 100644--- a/bin/hive-mind.js+++ b/bin/hive-mind.js@@ -24,8 +24,26 @@ import {nonInteractiveSelect,} from '../utils/safe-interactive.js';-// Import SQLite for persistence-import Database from 'better-sqlite3';+// Import SQLite for persistence - dynamically loaded to handle missing bindings+let Database = null;+let sqliteAvailable = false;++// Try to load better-sqlite3, but don't fail if bindings aren't available+async function loadSqlite() {+ if (Database !== null) return sqliteAvailable;+ try {+ const sqlite = await import('better-sqlite3');+ Database = sqlite.default;+ // Test if bindings work by creating a temp in-memory database+ const testDb = new Database(':memory:');+ testDb.close();+ sqliteAvailable = true;+ } catch (err) {+ sqliteAvailable = false;+ Database = null;+ }+ return sqliteAvailable;+}// Import help formatterimport { HelpFormatter } from '../help-formatter.js';@@ -128,6 +146,38 @@ async function initHiveMind(flags) {mkdirSync(hiveMindDir, { recursive: true });}+ // Check SQLite availability+ const hasSqlite = await loadSqlite();++ if (!hasSqlite) {+ // Fall back to JSON-based initialization+ spinner.text = 'Initializing Hive Mind with JSON fallback...';++ const fallbackPath = path.join(hiveMindDir, 'memory.json');+ const fallbackData = {+ metadata: {+ type: 'fallback',+ initialized: new Date().toISOString(),+ version: '2.0.0',+ warning: 'Using JSON fallback - SQLite bindings not available'+ },+ swarms: [],+ agents: [],+ tasks: [],+ collective_memory: [],+ consensus_votes: [],+ messages: []+ };++ await writeFile(fallbackPath, JSON.stringify(fallbackData, null, 2), 'utf8');++ spinner.succeed('Hive Mind initialized with JSON fallback');+ console.log(chalk.yellow('\nβ οΈ Note: SQLite native bindings not available'));+ console.log(chalk.gray(' Using JSON fallback for data persistence'));+ console.log(chalk.gray(' For full SQLite features, install locally: npm install claude-flow'));+ return;+ }+// Initialize SQLite databaseconst dbPath = path.join(hiveMindDir, 'hive.db');const db = new Database(dbPath);@@ -917,9 +967,70 @@ function getAgentCapabilities(type) {*/async function showStatus(flags) {try {- const dbPath = path.join(cwd(), '.hive-mind', 'hive.db');+ const hiveMindDir = path.join(cwd(), '.hive-mind');+ const dbPath = path.join(hiveMindDir, 'hive.db');+ const fallbackPath = path.join(hiveMindDir, 'memory.json');++ // Check if hive-mind is initialized at all+ if (!existsSync(hiveMindDir)) {+ console.error(chalk.red('Error: Hive Mind not initialized'));+ console.log('Run "claude-flow hive-mind init" first');+ return;+ }++ // Check SQLite availability+ const hasSqlite = await loadSqlite();++ // If SQLite database exists but bindings aren't available+ if (existsSync(dbPath) && !hasSqlite) {+ console.log(chalk.yellow('β οΈ SQLite native bindings not available'));+ console.log(chalk.gray(' This is normal when using npx or remote execution'));+ console.log('');++ // Try to show basic status from fallback JSON+ if (existsSync(fallbackPath)) {+ try {+ const fallbackData = JSON.parse(await readFile(fallbackPath, 'utf8'));+ console.log(chalk.bold('π Hive Mind Status (from fallback memory)'));+ console.log(chalk.cyan(' Database:'), chalk.yellow('JSON fallback'));+ console.log(chalk.cyan(' Swarms:'), fallbackData.swarms?.length || 0);+ console.log(chalk.cyan(' Agents:'), fallbackData.agents?.length || 0);+ console.log('');+ console.log(chalk.gray('π‘ For full SQLite features, install locally:'));+ console.log(chalk.gray(' npm install claude-flow'));+ return;+ } catch {+ // Fallback file exists but couldn't be read+ }+ }++ // Show minimal status+ console.log(chalk.bold('π Hive Mind Status'));+ console.log(chalk.cyan(' Initialized:'), chalk.green('Yes'));+ console.log(chalk.cyan(' Database:'), chalk.yellow('SQLite (unavailable in npx mode)'));+ console.log('');+ console.log(chalk.gray('π‘ For full functionality, install locally:'));+ console.log(chalk.gray(' npm install claude-flow'));+ return;+ }if (!existsSync(dbPath)) {+ // Check for fallback JSON database+ if (existsSync(fallbackPath)) {+ try {+ const fallbackData = JSON.parse(await readFile(fallbackPath, 'utf8'));+ console.log(chalk.bold('π Hive Mind Status (JSON fallback mode)'));+ console.log(chalk.cyan(' Database:'), chalk.yellow('JSON fallback'));+ console.log(chalk.cyan(' Swarms:'), fallbackData.swarms?.length || 0);+ console.log(chalk.cyan(' Agents:'), fallbackData.agents?.length || 0);+ console.log(chalk.cyan(' Tasks:'), fallbackData.tasks?.length || 0);+ return;+ } catch {+ console.error(chalk.red('Error: Hive Mind not initialized'));+ console.log('Run "claude-flow hive-mind init" first');+ return;+ }+ }console.error(chalk.red('Error: Hive Mind not initialized'));console.log('Run "claude-flow hive-mind init" first');return;diff --git a/src/cli/simple-commands/hive-mind.js b/src/cli/simple-commands/hive-mind.jsindex e21fe4e487..1f8806281e 100644--- a/src/cli/simple-commands/hive-mind.js+++ b/src/cli/simple-commands/hive-mind.js@@ -24,8 +24,26 @@ import {nonInteractiveSelect,} from '../utils/safe-interactive.js';-// Import SQLite for persistence-import Database from 'better-sqlite3';+// Import SQLite for persistence - dynamically loaded to handle missing bindings+let Database = null;+let sqliteAvailable = false;++// Try to load better-sqlite3, but don't fail if bindings aren't available+async function loadSqlite() {+ if (Database !== null) return sqliteAvailable;+ try {+ const sqlite = await import('better-sqlite3');+ Database = sqlite.default;+ // Test if bindings work by creating a temp in-memory database+ const testDb = new Database(':memory:');+ testDb.close();+ sqliteAvailable = true;+ } catch (err) {+ sqliteAvailable = false;+ Database = null;+ }+ return sqliteAvailable;+}// Import help formatterimport { HelpFormatter } from '../help-formatter.js';@@ -129,6 +147,38 @@ async function initHiveMind(flags) {mkdirSync(hiveMindDir, { recursive: true });}+ // Check SQLite availability+ const hasSqlite = await loadSqlite();++ if (!hasSqlite) {+ // Fall back to JSON-based initialization+ spinner.text = 'Initializing Hive Mind with JSON fallback...';++ const fallbackPath = path.join(hiveMindDir, 'memory.json');+ const fallbackData = {+ metadata: {+ type: 'fallback',+ initialized: new Date().toISOString(),+ version: '2.0.0',+ warning: 'Using JSON fallback - SQLite bindings not available'+ },+ swarms: [],+ agents: [],+ tasks: [],+ collective_memory: [],+ consensus_votes: [],+ messages: []+ };++ await writeFile(fallbackPath, JSON.stringify(fallbackData, null, 2), 'utf8');++ spinner.succeed('Hive Mind initialized with JSON fallback');+ console.log(chalk.yellow('\nβ οΈ Note: SQLite native bindings not available'));+ console.log(chalk.gray(' Using JSON fallback for data persistence'));+ console.log(chalk.gray(' For full SQLite features, install locally: npm install claude-flow'));+ return;+ }+// Initialize SQLite databaseconst dbPath = path.join(hiveMindDir, 'hive.db');const db = new Database(dbPath);@@ -962,9 +1012,70 @@ function getAgentCapabilities(type) {*/async function showStatus(flags) {try {- const dbPath = path.join(cwd(), '.hive-mind', 'hive.db');+ const hiveMindDir = path.join(cwd(), '.hive-mind');+ const dbPath = path.join(hiveMindDir, 'hive.db');+ const fallbackPath = path.join(hiveMindDir, 'memory.json');++ // Check if hive-mind is initialized at all+ if (!existsSync(hiveMindDir)) {+ console.error(chalk.red('Error: Hive Mind not initialized'));+ console.log('Run "claude-flow hive-mind init" first');+ return;+ }++ // Check SQLite availability+ const hasSqlite = await loadSqlite();++ // If SQLite database exists but bindings aren't available+ if (existsSync(dbPath) && !hasSqlite) {+ console.log(chalk.yellow('β οΈ SQLite native bindings not available'));+ console.log(chalk.gray(' This is normal when using npx or remote execution'));+ console.log('');++ // Try to show basic status from fallback JSON+ if (existsSync(fallbackPath)) {+ try {+ const fallbackData = JSON.parse(await readFile(fallbackPath, 'utf8'));+ console.log(chalk.bold('π Hive Mind Status (from fallback memory)'));+ console.log(chalk.cyan(' Database:'), chalk.yellow('JSON fallback'));+ console.log(chalk.cyan(' Swarms:'), fallbackData.swarms?.length || 0);+ console.log(chalk.cyan(' Agents:'), fallbackData.agents?.length || 0);+ console.log('');+ console.log(chalk.gray('π‘ For full SQLite features, install locally:'));+ console.log(chalk.gray(' npm install claude-flow'));+ return;+ } catch {+ // Fallback file exists but couldn't be read+ }+ }++ // Show minimal status+ console.log(chalk.bold('π Hive Mind Status'));+ console.log(chalk.cyan(' Initialized:'), chalk.green('Yes'));+ console.log(chalk.cyan(' Database:'), chalk.yellow('SQLite (unavailable in npx mode)'));+ console.log('');+ console.log(chalk.gray('π‘ For full functionality, install locally:'));+ console.log(chalk.gray(' npm install claude-flow'));+ return;+ }if (!existsSync(dbPath)) {+ // Check for fallback JSON database+ if (existsSync(fallbackPath)) {+ try {+ const fallbackData = JSON.parse(await readFile(fallbackPath, 'utf8'));+ console.log(chalk.bold('π Hive Mind Status (JSON fallback mode)'));+ console.log(chalk.cyan(' Database:'), chalk.yellow('JSON fallback'));+ console.log(chalk.cyan(' Swarms:'), fallbackData.swarms?.length || 0);+ console.log(chalk.cyan(' Agents:'), fallbackData.agents?.length || 0);+ console.log(chalk.cyan(' Tasks:'), fallbackData.tasks?.length || 0);+ return;+ } catch {+ console.error(chalk.red('Error: Hive Mind not initialized'));+ console.log('Run "claude-flow hive-mind init" first');+ return;+ }+ }console.error(chalk.red('Error: Hive Mind not initialized'));console.log('Run "claude-flow hive-mind init" first');return;diff --git a/src/cli/simple-commands/memory.js b/src/cli/simple-commands/memory.jsindex 5f0645b4d3..4ec2055a72 100644--- a/src/cli/simple-commands/memory.js+++ b/src/cli/simple-commands/memory.js@@ -506,17 +506,20 @@ async function detectMemoryMode(flags, subArgs) {printInfo('ποΈ Initialized SQLite backend (.swarm/memory.db)');return 'reasoningbank';} catch (error) {- // SQLite initialization failed - fall back to JSON- const isSqliteError = error.message?.includes('BetterSqlite3') ||+ // SQLite/native binding initialization failed - fall back to JSON+ const isNativeBindingError = error.message?.includes('BetterSqlite3') ||error.message?.includes('better-sqlite3') ||+ error.message?.includes('hnswlib') ||+ error.message?.includes('bindings') ||error.message?.includes('could not run migrations') ||- error.message?.includes('ReasoningBank initialization failed');+ error.message?.includes('ReasoningBank initialization failed') ||+ error.message?.includes('Could not locate the bindings file');const isNpx = process.env.npm_config_user_agent?.includes('npx') ||- process.cwd().includes('_npx');+ process.cwd().includes('_npx') ||+ process.argv[1]?.includes('npx');- if (isSqliteError && isNpx) {- // Silent fallback for npx - error already shown by adapter- console.log('\nβ Automatically using JSON fallback for this command\n');+ if (isNativeBindingError) {+ // Silent fallback for native binding errorsreturn 'basic';} else {printWarning(`β οΈ SQLite unavailable, using JSON fallback`);@@ -542,8 +545,36 @@ async function isReasoningBankInitialized() {async function handleReasoningBankCommand(command, subArgs, flags) {const initialized = await isReasoningBankInitialized();- // Lazy load the adapter (ES modules)- const { initializeReasoningBank, storeMemory, queryMemories, listMemories, getStatus, checkReasoningBankTables, migrateReasoningBank, cleanup } = await import('../../reasoningbank/reasoningbank-adapter.js');+ // Lazy load the adapter (ES modules) with error handling for native bindings+ let adapterModule;+ try {+ adapterModule = await import('../../reasoningbank/reasoningbank-adapter.js');+ } catch (error) {+ // Native binding error - fallback to basic mode+ const isNativeBindingError = error.message?.includes('bindings') ||+ error.message?.includes('hnswlib') ||+ error.message?.includes('better-sqlite3') ||+ error.message?.includes('Could not locate');+ if (isNativeBindingError) {+ console.log('β οΈ ReasoningBank unavailable (native bindings not found)');+ console.log(' Using basic JSON memory instead');+ console.log(' π‘ For full features, install locally: npm install claude-flow\n');+ // Fall back to basic memory stats+ if (command === 'status' || command === 'stats') {+ await showMemoryStats(async () => {+ try {+ const content = await fs.readFile('./memory/memory-store.json', 'utf8');+ return JSON.parse(content);+ } catch {+ return {};+ }+ }, 'basic');+ }+ return;+ }+ throw error;+ }+ const { initializeReasoningBank, storeMemory, queryMemories, listMemories, getStatus, checkReasoningBankTables, migrateReasoningBank, cleanup } = adapterModule;// Special handling for 'init' commandif (command === 'init') {