Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 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

fix: ensure all required columns exist in hive-mind database before optimization

rUvd6c25350ee81
diff --git a/src/cli/simple-commands/hive-mind.js b/src/cli/simple-commands/hive-mind.js
index ba37e957eb..12ed7c354a 100644
--- a/src/cli/simple-commands/hive-mind.js
+++ b/src/cli/simple-commands/hive-mind.js
@@ -871,23 +871,52 @@ async function showMetrics(flags) {
});
}
- // Get agent performance
- const agentPerf = db.prepare(`
- SELECT
- a.name,
- a.type,
- COUNT(t.id) as tasks_assigned,
- SUM(CASE WHEN t.status = 'completed' THEN 1 ELSE 0 END) as tasks_completed,
- AVG(CASE WHEN t.completed_at IS NOT NULL
- THEN (julianday(t.completed_at) - julianday(t.created_at)) * 24 * 60
- ELSE NULL END) as avg_completion_minutes
- FROM agents a
- LEFT JOIN tasks t ON a.id = t.agent_id
- GROUP BY a.id
- HAVING tasks_assigned > 0
- ORDER BY tasks_completed DESC
- LIMIT 10
- `).all();
+ // Get agent performance (check for completed_at column)
+ let agentPerf = [];
+ try {
+ // Check if completed_at exists
+ const hasCompletedAt = db.prepare(`
+ SELECT COUNT(*) as count FROM pragma_table_info('tasks')
+ WHERE name = 'completed_at'
+ `).get();
+
+ if (hasCompletedAt && hasCompletedAt.count > 0) {
+ agentPerf = db.prepare(`
+ SELECT
+ a.name,
+ a.type,
+ COUNT(t.id) as tasks_assigned,
+ SUM(CASE WHEN t.status = 'completed' THEN 1 ELSE 0 END) as tasks_completed,
+ AVG(CASE WHEN t.completed_at IS NOT NULL
+ THEN (julianday(t.completed_at) - julianday(t.created_at)) * 24 * 60
+ ELSE NULL END) as avg_completion_minutes
+ FROM agents a
+ LEFT JOIN tasks t ON a.id = t.agent_id
+ GROUP BY a.id
+ HAVING tasks_assigned > 0
+ ORDER BY tasks_completed DESC
+ LIMIT 10
+ `).all();
+ } else {
+ // Simpler query without completed_at
+ agentPerf = db.prepare(`
+ SELECT
+ a.name,
+ a.type,
+ COUNT(t.id) as tasks_assigned,
+ SUM(CASE WHEN t.status = 'completed' THEN 1 ELSE 0 END) as tasks_completed,
+ NULL as avg_completion_minutes
+ FROM agents a
+ LEFT JOIN tasks t ON a.id = t.agent_id
+ GROUP BY a.id
+ HAVING tasks_assigned > 0
+ ORDER BY tasks_completed DESC
+ LIMIT 10
+ `).all();
+ }
+ } catch (error) {
+ console.warn('Could not get agent performance:', error.message);
+ }
if (agentPerf.length > 0) {
console.log('\n' + chalk.cyan('Top Performing Agents:'));
@@ -933,29 +962,70 @@ async function showMetrics(flags) {
}
// Get performance insights
- const avgTaskTime = db.prepare(`
- SELECT
- AVG(CASE WHEN completed_at IS NOT NULL
- THEN (julianday(completed_at) - julianday(created_at)) * 24 * 60
- ELSE NULL END) as avg_minutes
- FROM tasks
- WHERE status = 'completed'
- `).get();
+ let avgTaskTime = { avg_minutes: null };
+ try {
+ // Check if completed_at exists
+ const hasCompletedAt = db.prepare(`
+ SELECT COUNT(*) as count FROM pragma_table_info('tasks')
+ WHERE name = 'completed_at'
+ `).get();
+
+ if (hasCompletedAt && hasCompletedAt.count > 0) {
+ avgTaskTime = db.prepare(`
+ SELECT
+ AVG(CASE WHEN completed_at IS NOT NULL
+ THEN (julianday(completed_at) - julianday(created_at)) * 24 * 60
+ ELSE NULL END) as avg_minutes
+ FROM tasks
+ WHERE status = 'completed'
+ `).get();
+ }
+ } catch (error) {
+ console.warn('Could not calculate average task time:', error.message);
+ }
- const agentTypePerf = db.prepare(`
- SELECT
- a.type,
- COUNT(t.id) as total_tasks,
- SUM(CASE WHEN t.status = 'completed' THEN 1 ELSE 0 END) as completed_tasks,
- AVG(CASE WHEN t.completed_at IS NOT NULL
- THEN (julianday(t.completed_at) - julianday(t.created_at)) * 24 * 60
- ELSE NULL END) as avg_completion_minutes
- FROM agents a
- LEFT JOIN tasks t ON a.id = t.agent_id
- GROUP BY a.type
- HAVING total_tasks > 0
- ORDER BY completed_tasks DESC
- `).all();
+ // Get agent type performance
+ let agentTypePerf = [];
+ try {
+ // Check if completed_at exists
+ const hasCompletedAt = db.prepare(`
+ SELECT COUNT(*) as count FROM pragma_table_info('tasks')
+ WHERE name = 'completed_at'
+ `).get();
+
+ if (hasCompletedAt && hasCompletedAt.count > 0) {
+ agentTypePerf = db.prepare(`
+ SELECT
+ a.type,
+ COUNT(t.id) as total_tasks,
+ SUM(CASE WHEN t.status = 'completed' THEN 1 ELSE 0 END) as completed_tasks,
+ AVG(CASE WHEN t.completed_at IS NOT NULL
+ THEN (julianday(t.completed_at) - julianday(t.created_at)) * 24 * 60
+ ELSE NULL END) as avg_completion_minutes
+ FROM agents a
+ LEFT JOIN tasks t ON a.id = t.agent_id
+ GROUP BY a.type
+ HAVING total_tasks > 0
+ ORDER BY completed_tasks DESC
+ `).all();
+ } else {
+ // Simpler query without completed_at
+ agentTypePerf = db.prepare(`
+ SELECT
+ a.type,
+ COUNT(t.id) as total_tasks,
+ SUM(CASE WHEN t.status = 'completed' THEN 1 ELSE 0 END) as completed_tasks,
+ NULL as avg_completion_minutes
+ FROM agents a
+ LEFT JOIN tasks t ON a.id = t.agent_id
+ GROUP BY a.type
+ HAVING total_tasks > 0
+ ORDER BY completed_tasks DESC
+ `).all();
+ }
+ } catch (error) {
+ console.warn('Could not get agent type performance:', error.message);
+ }
if (avgTaskTime.avg_minutes) {
console.log('\n' + chalk.cyan('Performance Insights:'));
diff --git a/src/cli/simple-commands/hive-mind/db-optimizer.js b/src/cli/simple-commands/hive-mind/db-optimizer.js
index 1827355fd8..82984c46dd 100644
--- a/src/cli/simple-commands/hive-mind/db-optimizer.js
+++ b/src/cli/simple-commands/hive-mind/db-optimizer.js
@@ -165,6 +165,9 @@ function updateSchemaVersion(db, version, description = '') {
* Apply basic performance indexes
*/
function applyBasicIndexes(db) {
+ // First ensure all required columns exist
+ ensureRequiredColumns(db);
+
const indexes = [
// Swarms indexes
'CREATE INDEX IF NOT EXISTS idx_swarms_status ON swarms(status)',
@@ -194,6 +197,79 @@ function applyBasicIndexes(db) {
indexes.forEach(sql => db.exec(sql));
}
+/**
+ * Ensure all required columns exist
+ */
+function ensureRequiredColumns(db) {
+ // Check and add priority column to tasks table
+ const hasPriority = db.prepare(`
+ SELECT COUNT(*) as count FROM pragma_table_info('tasks')
+ WHERE name = 'priority'
+ `).get();
+
+ if (!hasPriority || hasPriority.count === 0) {
+ try {
+ db.exec('ALTER TABLE tasks ADD COLUMN priority INTEGER DEFAULT 5');
+ console.log('Added missing priority column to tasks table');
+ } catch (error) {
+ if (!error.message.includes('duplicate column')) {
+ throw error;
+ }
+ }
+ }
+
+ // Check and add completed_at column to tasks table
+ const hasCompletedAt = db.prepare(`
+ SELECT COUNT(*) as count FROM pragma_table_info('tasks')
+ WHERE name = 'completed_at'
+ `).get();
+
+ if (!hasCompletedAt || hasCompletedAt.count === 0) {
+ try {
+ db.exec('ALTER TABLE tasks ADD COLUMN completed_at DATETIME');
+ console.log('Added missing completed_at column to tasks table');
+ } catch (error) {
+ if (!error.message.includes('duplicate column')) {
+ throw error;
+ }
+ }
+ }
+
+ // Check and add result column to tasks table
+ const hasResult = db.prepare(`
+ SELECT COUNT(*) as count FROM pragma_table_info('tasks')
+ WHERE name = 'result'
+ `).get();
+
+ if (!hasResult || hasResult.count === 0) {
+ try {
+ db.exec('ALTER TABLE tasks ADD COLUMN result TEXT');
+ console.log('Added missing result column to tasks table');
+ } catch (error) {
+ if (!error.message.includes('duplicate column')) {
+ throw error;
+ }
+ }
+ }
+
+ // Check and add updated_at column to swarms table
+ const hasUpdatedAt = db.prepare(`
+ SELECT COUNT(*) as count FROM pragma_table_info('swarms')
+ WHERE name = 'updated_at'
+ `).get();
+
+ if (!hasUpdatedAt || hasUpdatedAt.count === 0) {
+ try {
+ db.exec('ALTER TABLE swarms ADD COLUMN updated_at DATETIME DEFAULT CURRENT_TIMESTAMP');
+ console.log('Added missing updated_at column to swarms table');
+ } catch (error) {
+ if (!error.message.includes('duplicate column')) {
+ throw error;
+ }
+ }
+ }
+}
+
/**
* Apply advanced performance indexes
*/
@@ -209,8 +285,8 @@ function applyAdvancedIndexes(db) {
'CREATE INDEX IF NOT EXISTS idx_agents_full ON agents(swarm_id, type, status, role)',
// Partial indexes for active records
- 'CREATE INDEX IF NOT EXISTS idx_swarms_active ON swarms(id, name) WHERE status = "active"',
- 'CREATE INDEX IF NOT EXISTS idx_tasks_pending ON tasks(swarm_id, priority) WHERE status = "pending"'
+ "CREATE INDEX IF NOT EXISTS idx_swarms_active ON swarms(id, name) WHERE status = 'active'",
+ "CREATE INDEX IF NOT EXISTS idx_tasks_pending ON tasks(swarm_id, priority) WHERE status = 'pending'"
];
indexes.forEach(sql => db.exec(sql));
@@ -268,16 +344,82 @@ function addPerformanceTracking(db) {
* Add memory optimization features
*/
function addMemoryOptimization(db) {
- // Add memory access tracking
- db.exec(`
- ALTER TABLE collective_memory
- ADD COLUMN access_count INTEGER DEFAULT 0
- `);
+ // Check and add access_count column
+ const hasAccessCount = db.prepare(`
+ SELECT COUNT(*) as count FROM pragma_table_info('collective_memory')
+ WHERE name = 'access_count'
+ `).get();
- db.exec(`
- ALTER TABLE collective_memory
- ADD COLUMN last_accessed DATETIME DEFAULT CURRENT_TIMESTAMP
- `);
+ if (!hasAccessCount || hasAccessCount.count === 0) {
+ try {
+ db.exec(`
+ ALTER TABLE collective_memory
+ ADD COLUMN access_count INTEGER DEFAULT 0
+ `);
+ console.log('Added access_count column to collective_memory table');
+ } catch (error) {
+ if (!error.message.includes('duplicate column')) {
+ throw error;
+ }
+ }
+ }
+
+ // Check and add accessed_at column (not last_accessed)
+ const hasAccessedAt = db.prepare(`
+ SELECT COUNT(*) as count FROM pragma_table_info('collective_memory')
+ WHERE name = 'accessed_at'
+ `).get();
+
+ if (!hasAccessedAt || hasAccessedAt.count === 0) {
+ try {
+ db.exec(`
+ ALTER TABLE collective_memory
+ ADD COLUMN accessed_at DATETIME DEFAULT CURRENT_TIMESTAMP
+ `);
+ console.log('Added accessed_at column to collective_memory table');
+ } catch (error) {
+ if (!error.message.includes('duplicate column')) {
+ throw error;
+ }
+ }
+ }
+
+ // Add compressed and size columns if missing
+ const hasCompressed = db.prepare(`
+ SELECT COUNT(*) as count FROM pragma_table_info('collective_memory')
+ WHERE name = 'compressed'
+ `).get();
+
+ if (!hasCompressed || hasCompressed.count === 0) {
+ try {
+ db.exec(`
+ ALTER TABLE collective_memory
+ ADD COLUMN compressed INTEGER DEFAULT 0
+ `);
+ } catch (error) {
+ if (!error.message.includes('duplicate column')) {
+ throw error;
+ }
+ }
+ }
+
+ const hasSize = db.prepare(`
+ SELECT COUNT(*) as count FROM pragma_table_info('collective_memory')
+ WHERE name = 'size'
+ `).get();
+
+ if (!hasSize || hasSize.count === 0) {
+ try {
+ db.exec(`
+ ALTER TABLE collective_memory
+ ADD COLUMN size INTEGER DEFAULT 0
+ `);
+ } catch (error) {
+ if (!error.message.includes('duplicate column')) {
+ throw error;
+ }
+ }
+ }
// Create memory usage summary view
db.exec(`