-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_daemon.js
More file actions
298 lines (258 loc) · 12 KB
/
start_daemon.js
File metadata and controls
298 lines (258 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env node
/**
* start_daemon.js - SOMA Cognitive Daemon
*
* Runs SOMA background services 24/7 with low CPU impact.
* Focuses on:
* - TimekeeperArbiter (Scheduling)
* - ImmuneSystemArbiter (Self-Healing)
* - NighttimeLearningOrchestrator (Dreaming)
* - MetaLearningEngine (Optimization)
*
* Features:
* - Low-intensity heartbeat.
* - Auto-recovery via Immune System.
* - Ctrl+C graceful shutdown.
* - No UI/Web server overhead.
*/
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { config as dotenvConfig } from 'dotenv';
import fs from 'fs/promises';
// Load environment
dotenvConfig();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import { CONFIG } from './core/SomaConfig.js';
import { SomaBootstrap } from './core/SomaBootstrap.js';
import { logger } from './core/Logger.js';
import chokidar from 'chokidar';
import net from 'net';
// Daemon State - Shared with main launcher via IPC
const daemonState = {
startTime: Date.now(),
memoryConsolidations: 0,
fileChanges: [],
dreamInsights: [],
systemHealth: { healthy: true, alerts: [] },
lastConsolidation: null,
lastPersistence: null
};
logger.info(`
╔═══════════════════════════════════════════════════════════╗
║ 🌙 SOMA COGNITIVE DAEMON ACTIVE ║
║ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ║
║ 🛡️ Immune System: ON ⏰ Timekeeper: ON ║
║ 💤 Dream Cycles: ON 🧠 Meta-Learning: ON ║
║ 🏗️ Physics Sim: ON 📁 File Watcher: ON ║
║ 💾 Memory Consolidation: ON 🔗 IPC Bridge: ON ║
║ ║
║ [Running in background - Low Intensity Mode] ║
╚═══════════════════════════════════════════════════════════╝
`);
async function main() {
try {
// 1. Initialize Bootstrap in "Lightweight" mode if possible
let system = {};
try {
const bootstrap = new SomaBootstrap(process.cwd(), {
...CONFIG,
mode: 'daemon',
port: 0, // No port needed for web
clusterPort: 0 // Disable GMN server for daemon
});
logger.info('Initializing background arbiters...');
system = await bootstrap.initialize();
logger.success('Daemon bootstrap complete.');
} catch (bootstrapErr) {
logger.warn('Daemon bootstrap failed — running in minimal mode: ' + bootstrapErr.message);
// Continue: file watcher, IPC, heartbeat all work without bootstrap
}
// SimulationArbiter runs in the main server (launcher_ULTRA), not the daemon.
logger.success('Daemon mode fully operational.');
logger.info('Press Ctrl+C to stop the daemon.');
// 2. Memory Consolidation + Auto-Cleanup (Every hour)
const memoryConsolidation = setInterval(async () => {
try {
if (system.mnemonic && system.mnemonic.consolidate) {
logger.info('[Memory] Starting consolidation cycle...');
const result = await system.mnemonic.consolidate();
daemonState.memoryConsolidations++;
daemonState.lastConsolidation = Date.now();
logger.success(`[Memory] Consolidated ${result?.count || 0} fragments`);
}
// Auto-cleanup: prune old experience files
const experiencesDir = join(__dirname, '.soma', 'experiences');
const consolidatedLog = join(experiencesDir, '.consolidated.txt');
try {
const files = await fs.readdir(experiencesDir);
const indexed = new Set();
// Load list of indexed files
try {
const log = await fs.readFile(consolidatedLog, 'utf8');
log.split('\n').forEach(f => f.trim() && indexed.add(f.trim()));
} catch (e) { /* log doesn't exist yet */ }
let deleted = 0;
const now = Date.now();
const INDEXED_MAX_AGE = 7 * 24 * 60 * 60 * 1000; // 7 days
const UNINDEXED_MAX_AGE = 30 * 24 * 60 * 60 * 1000; // 30 days
for (const file of files) {
if (file.startsWith('.') || !file.endsWith('.json')) continue;
const filePath = join(experiencesDir, file);
const stats = await fs.stat(filePath);
const age = now - stats.mtimeMs;
const shouldDelete = indexed.has(file)
? age > INDEXED_MAX_AGE
: age > UNINDEXED_MAX_AGE;
if (shouldDelete) {
await fs.unlink(filePath);
deleted++;
}
}
if (deleted > 0) {
logger.info(`[Cleanup] Deleted ${deleted} old experience files`);
}
} catch (e) {
logger.warn('[Cleanup] Experience cleanup failed:', e.message);
}
} catch (e) {
logger.error('[Memory] Consolidation failed:', e.message);
}
}, 3600000); // 1 hour
// 3. File System Watcher (Monitor codebase changes)
const watcher = chokidar.watch([
join(__dirname, 'core/**/*.{js,mjs,cjs}'),
join(__dirname, 'arbiters/**/*.{js,mjs,cjs}'),
join(__dirname, 'frontend/**/*.{js,jsx,ts,tsx}')
], {
ignored: /(^|[\/\\])\..|(node_modules|dist|build)/,
persistent: true,
ignoreInitial: true
});
watcher.on('change', (path) => {
const event = { type: 'change', path, timestamp: Date.now() };
daemonState.fileChanges.push(event);
if (daemonState.fileChanges.length > 100) daemonState.fileChanges.shift();
logger.info(`[FileWatch] Changed: ${path}`);
});
watcher.on('add', (path) => {
const event = { type: 'add', path, timestamp: Date.now() };
daemonState.fileChanges.push(event);
if (daemonState.fileChanges.length > 100) daemonState.fileChanges.shift();
logger.info(`[FileWatch] Added: ${path}`);
});
// 4. IPC Server (Communication bridge with main launcher)
const IPC_PATH = process.platform === 'win32'
? '\\\\.\\pipe\\soma-daemon'
: '/tmp/soma-daemon.sock';
// Clean up old socket if exists
if (process.platform !== 'win32') {
try {
await fs.unlink(IPC_PATH);
} catch (e) {
// Socket doesn't exist, that's fine
}
}
const ipcServer = net.createServer((socket) => {
logger.info('[IPC] Main launcher connected');
socket.on('data', (data) => {
try {
const request = JSON.parse(data.toString());
if (request.type === 'getState') {
// Send full daemon state to main launcher
socket.write(JSON.stringify({
type: 'state',
payload: {
...daemonState,
uptime: Date.now() - daemonState.startTime,
memory: process.memoryUsage().rss / 1024 / 1024
}
}) + '\n');
} else if (request.type === 'getDreamInsights') {
socket.write(JSON.stringify({
type: 'insights',
payload: daemonState.dreamInsights
}) + '\n');
}
} catch (e) {
logger.error('[IPC] Invalid message:', e.message);
}
});
socket.on('end', () => {
logger.info('[IPC] Main launcher disconnected');
});
});
ipcServer.listen(IPC_PATH, () => {
logger.success(`[IPC] Bridge listening on ${IPC_PATH}`);
});
// 5. State Persistence (Write to disk every 10 minutes)
const statePersistence = setInterval(async () => {
try {
const stateFile = join(__dirname, 'data', 'daemon-state.json');
await fs.writeFile(stateFile, JSON.stringify({
...daemonState,
uptime: Date.now() - daemonState.startTime,
savedAt: new Date().toISOString()
}, null, 2));
daemonState.lastPersistence = Date.now();
logger.info('[Persistence] Daemon state saved');
} catch (e) {
logger.error('[Persistence] Failed to save state:', e.message);
}
}, 600000); // 10 minutes
// 6. Heartbeat logic (Every 10 minutes)
// This ensures the event loop stays alive and logs basic health
const heartbeat = setInterval(async () => {
const mem = process.memoryUsage().rss / 1024 / 1024;
logger.info(`[Heartbeat] SOMA is alive. Memory: ${mem.toFixed(1)} MB`);
// Check if Immune System is still healthy
if (system.immuneSystem) {
const health = await system.immuneSystem.watchdog.senseSystemState();
daemonState.systemHealth = health;
if (!health.healthy) {
logger.warn(`[Health Alert] ${health.alerts.join(', ')}`);
}
}
}, 600000); // 10 minutes
// 7. Capture Dream Insights (When nighttime learning produces insights)
if (system.nighttimeLearning) {
// Hook into dream cycle completion
const originalOnCycleComplete = system.nighttimeLearning.onCycleComplete;
system.nighttimeLearning.onCycleComplete = (insights) => {
if (insights && insights.length > 0) {
daemonState.dreamInsights.push(...insights.map(i => ({
...i,
timestamp: Date.now()
})));
// Keep only last 50 insights
if (daemonState.dreamInsights.length > 50) {
daemonState.dreamInsights = daemonState.dreamInsights.slice(-50);
}
logger.success(`[Dream] Captured ${insights.length} new insights`);
}
if (originalOnCycleComplete) originalOnCycleComplete(insights);
};
}
// 8. Graceful Shutdown
process.on('SIGINT', async () => {
logger.info('\nStopping SOMA Daemon...');
clearInterval(heartbeat);
clearInterval(memoryConsolidation);
clearInterval(statePersistence);
// Close file watcher
if (watcher) await watcher.close();
// Close IPC server
if (ipcServer) ipcServer.close();
if (system.conversationHistory) await system.conversationHistory.shutdown();
if (system.personalityForge) await system.personalityForge.shutdown();
if (system.learningPipeline) await system.learningPipeline.shutdown();
if (system.nighttimeLearning) await system.nighttimeLearning.shutdown();
logger.success('Daemon stopped. SOMA is now resting.');
process.exit(0);
});
} catch (error) {
logger.error('DAEMON FATAL ERROR:', error);
process.exit(1);
}
}
main();