Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix(cos): resolve circular dependency breaking task spawning

Adam Eivy5a688e3e52b9
diff --git a/server/services/agentGateway.js b/server/services/agentGateway.js
index f0fdaed5d1..8fb251ed68 100644
--- a/server/services/agentGateway.js
+++ b/server/services/agentGateway.js
@@ -5,7 +5,7 @@
* Provides request deduplication, caching, and coordination.
*/
-import { cosEvents } from './cos.js'
+import { cosEvents } from './cosEvents.js'
// Request deduplication cache
const pendingRequests = new Map()
diff --git a/server/services/contextUpgrader.js b/server/services/contextUpgrader.js
index f45f0a6357..a10478bbc3 100644
--- a/server/services/contextUpgrader.js
+++ b/server/services/contextUpgrader.js
@@ -7,7 +7,7 @@
import * as thinkingLevels from './thinkingLevels.js'
import * as localThinking from './localThinking.js'
-import { cosEvents } from './cos.js'
+import { cosEvents } from './cosEvents.js'
// Upgrade triggers
const UPGRADE_TRIGGERS = {
diff --git a/server/services/cos.js b/server/services/cos.js
index 9e2b8362e0..dce07a7b01 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -9,7 +9,6 @@ import { readFile, writeFile, mkdir, readdir, rm } from 'fs/promises';
import { existsSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
-import { EventEmitter } from 'events';
import { exec } from 'child_process';
import { promisify } from 'util';
import { v4 as uuidv4 } from 'uuid';
@@ -20,6 +19,9 @@ import { getAllApps } from './apps.js';
import { getAdaptiveCooldownMultiplier, getSkippedTaskTypes, getPerformanceSummary, checkAndRehabilitateSkippedTasks } from './taskLearning.js';
import { schedule as scheduleEvent, cancel as cancelEvent, getStats as getSchedulerStats } from './eventScheduler.js';
import { generateProactiveTasks as generateMissionTasks, getStats as getMissionStats } from './missions.js';
+// Import and re-export cosEvents from separate module to avoid circular dependencies
+import { cosEvents as _cosEvents } from './cosEvents.js';
+export const cosEvents = _cosEvents;
const execAsync = promisify(exec);
@@ -33,16 +35,13 @@ const REPORTS_DIR = join(COS_DIR, 'reports');
const SCRIPTS_DIR = join(COS_DIR, 'scripts');
const ROOT_DIR = join(__dirname, '../../');
-// Event emitter for CoS events
-export const cosEvents = new EventEmitter();
-
/**
* Emit a log event for UI display
* Exported for use by other CoS-related services
* @param {string} level - Log level: 'info', 'warn', 'error', 'success', 'debug'
* @param {string} message - Log message
* @param {Object} data - Additional data to include in log entry
- * @param {string} prefix - Optional prefix for console output (e.g., '🔧 SelfImprovement')
+ * @param {string} prefix - Optional prefix for console output (e.g., 'SelfImprovement')
*/
export function emitLog(level, message, data = {}, prefix = '') {
const logEntry = {
diff --git a/server/services/cosEvents.js b/server/services/cosEvents.js
new file mode 100644
index 0000000000..ca87408df1
--- /dev/null
+++ b/server/services/cosEvents.js
@@ -0,0 +1,42 @@
+/**
+ * CoS Events Module
+ *
+ * Centralized event emitter for Chief of Staff services.
+ * Separated to avoid circular dependencies between cos.js and other modules.
+ */
+
+import { EventEmitter } from 'events'
+
+// Event emitter for CoS events
+export const cosEvents = new EventEmitter()
+
+/**
+ * Emit a log event for UI display
+ * @param {string} level - Log level: 'info', 'warn', 'error', 'success', 'debug'
+ * @param {string} message - Log message
+ * @param {Object} data - Additional data to include in log entry
+ * @param {string} prefix - Optional prefix for console output (e.g., 'SelfImprovement')
+ */
+export function emitLog(level, message, data = {}, prefix = '') {
+ const logEntry = {
+ timestamp: new Date().toISOString(),
+ level,
+ message,
+ ...data
+ }
+
+ // Emit for UI
+ cosEvents.emit('log', logEntry)
+
+ // Also log to console with appropriate emoji
+ const levelEmojis = {
+ info: 'i',
+ warn: '!',
+ error: 'x',
+ success: '+',
+ debug: '.'
+ }
+ const emoji = levelEmojis[level] || 'i'
+ const prefixStr = prefix ? `${prefix}: ` : ''
+ console.log(`[${logEntry.timestamp}] ${emoji} ${prefixStr}${message}`)
+}
diff --git a/server/services/cosEvolution.js b/server/services/cosEvolution.js
index a172d50eb2..703b0672eb 100644
--- a/server/services/cosEvolution.js
+++ b/server/services/cosEvolution.js
@@ -13,7 +13,7 @@
import { promises as fs } from 'fs'
import path from 'path'
import { v4 as uuidv4 } from 'uuid'
-import { cosEvents } from './cos.js'
+import { cosEvents } from './cosEvents.js'
import * as lmStudioManager from './lmStudioManager.js'
const DATA_DIR = path.join(process.cwd(), 'data', 'cos')
diff --git a/server/services/errorRecovery.js b/server/services/errorRecovery.js
index c81c15a52e..cf4f48a4fa 100644
--- a/server/services/errorRecovery.js
+++ b/server/services/errorRecovery.js
@@ -5,7 +5,7 @@
* Provides structured error handling for agent operations.
*/
-import { cosEvents } from './cos.js'
+import { cosEvents } from './cosEvents.js'
// Recovery strategies
const STRATEGIES = {
diff --git a/server/services/eventScheduler.js b/server/services/eventScheduler.js
index 0215a6a99d..54f34d4183 100644
--- a/server/services/eventScheduler.js
+++ b/server/services/eventScheduler.js
@@ -5,7 +5,7 @@
* Replaces setInterval with more robust scheduling.
*/
-import { cosEvents } from './cos.js'
+import { cosEvents } from './cosEvents.js'
// Maximum safe setTimeout value (2^31 - 1 ms, ~24.8 days)
const MAX_TIMEOUT = 2147483647
diff --git a/server/services/executionLanes.js b/server/services/executionLanes.js
index 76c787b100..aab95253df 100644
--- a/server/services/executionLanes.js
+++ b/server/services/executionLanes.js
@@ -5,7 +5,7 @@
* Lanes: critical (1), standard (2), background (3)
*/
-import { cosEvents } from './cos.js'
+import { cosEvents } from './cosEvents.js'
// Lane configuration
const LANES = {
diff --git a/server/services/lmStudioManager.js b/server/services/lmStudioManager.js
index 1d112d7693..6e25f50f6e 100644
--- a/server/services/lmStudioManager.js
+++ b/server/services/lmStudioManager.js
@@ -5,7 +5,7 @@
* Provides model discovery, loading, unloading, and downloading.
*/
-import { cosEvents } from './cos.js'
+import { cosEvents } from './cosEvents.js'
// Default LM Studio configuration
const DEFAULT_CONFIG = {
diff --git a/server/services/localThinking.js b/server/services/localThinking.js
index cd6e8656bd..300e03aeed 100644
--- a/server/services/localThinking.js
+++ b/server/services/localThinking.js
@@ -6,7 +6,7 @@
*/
import * as lmStudio from './lmStudioManager.js'
-import { cosEvents } from './cos.js'
+import { cosEvents } from './cosEvents.js'
// Task complexity thresholds for escalation
const COMPLEXITY_THRESHOLDS = {
diff --git a/server/services/memoryExtractor.js b/server/services/memoryExtractor.js
index aa8bb47fb2..20716ffa2f 100644
--- a/server/services/memoryExtractor.js
+++ b/server/services/memoryExtractor.js
@@ -7,7 +7,7 @@
import { createMemory } from './memory.js';
import { generateMemoryEmbedding } from './memoryEmbeddings.js';
-import { cosEvents } from './cos.js';
+import { cosEvents } from './cosEvents.js';
import * as notifications from './notifications.js';
import { classifyMemories, isAvailable as isClassifierAvailable } from './memoryClassifier.js';
diff --git a/server/services/missions.js b/server/services/missions.js
index aac4cce8b5..0d9a872e23 100644
--- a/server/services/missions.js
+++ b/server/services/missions.js
@@ -8,7 +8,7 @@
import { promises as fs } from 'fs'
import path from 'path'
import { v4 as uuidv4 } from 'uuid'
-import { cosEvents } from './cos.js'
+import { cosEvents } from './cosEvents.js'
const DATA_DIR = path.join(process.cwd(), 'data', 'cos', 'missions')
diff --git a/server/services/scriptRunner.js b/server/services/scriptRunner.js
index 0bcd3b993c..233b31c767 100644
--- a/server/services/scriptRunner.js
+++ b/server/services/scriptRunner.js
@@ -12,7 +12,7 @@ import { writeFile, readFile, mkdir, readdir, rm } from 'fs/promises';
import { existsSync } from 'fs';
import { v4 as uuidv4 } from 'uuid';
import Cron from 'croner';
-import { cosEvents } from './cos.js';
+import { cosEvents } from './cosEvents.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
diff --git a/server/services/socket.js b/server/services/socket.js
index c3063aa938..fcce3b381a 100644
--- a/server/services/socket.js
+++ b/server/services/socket.js
@@ -1,6 +1,6 @@
import { spawn } from 'child_process';
import { streamDetection } from './streamingDetect.js';
-import { cosEvents } from './cos.js';
+import { cosEvents } from './cosEvents.js';
import { appsEvents } from './apps.js';
import { errorEvents } from '../lib/errorHandler.js';
import { handleErrorRecovery } from './autoFixer.js';
diff --git a/server/services/thinkingLevels.js b/server/services/thinkingLevels.js
index d7f673dd9b..c40490c46d 100644
--- a/server/services/thinkingLevels.js
+++ b/server/services/thinkingLevels.js
@@ -5,7 +5,7 @@
* Hierarchy: task → hooks → agent → provider defaults
*/
-import { cosEvents } from './cos.js'
+import { cosEvents } from './cosEvents.js'
// Thinking level definitions
const THINKING_LEVELS = {
diff --git a/server/services/toolStateMachine.js b/server/services/toolStateMachine.js
index 3afee98bfa..2ae6213c53 100644
--- a/server/services/toolStateMachine.js
+++ b/server/services/toolStateMachine.js
@@ -6,7 +6,7 @@
*/
import { v4 as uuidv4 } from 'uuid'
-import { cosEvents } from './cos.js'
+import { cosEvents } from './cosEvents.js'
// Tool execution states
const STATES = {