genesis-ai-cli 7.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +78 -0
- package/README.md +282 -0
- package/dist/src/active-inference/actions.d.ts +75 -0
- package/dist/src/active-inference/actions.js +250 -0
- package/dist/src/active-inference/autonomous-loop.d.ts +103 -0
- package/dist/src/active-inference/autonomous-loop.js +289 -0
- package/dist/src/active-inference/core.d.ts +85 -0
- package/dist/src/active-inference/core.js +555 -0
- package/dist/src/active-inference/demo-autonomous-loop.d.ts +8 -0
- package/dist/src/active-inference/demo-autonomous-loop.js +338 -0
- package/dist/src/active-inference/demo-value-integration.d.ts +8 -0
- package/dist/src/active-inference/demo-value-integration.js +174 -0
- package/dist/src/active-inference/index.d.ts +32 -0
- package/dist/src/active-inference/index.js +88 -0
- package/dist/src/active-inference/integration.d.ts +114 -0
- package/dist/src/active-inference/integration.js +698 -0
- package/dist/src/active-inference/memory-integration.d.ts +51 -0
- package/dist/src/active-inference/memory-integration.js +232 -0
- package/dist/src/active-inference/observations.d.ts +67 -0
- package/dist/src/active-inference/observations.js +147 -0
- package/dist/src/active-inference/test-active-inference.d.ts +8 -0
- package/dist/src/active-inference/test-active-inference.js +320 -0
- package/dist/src/active-inference/test-value-integration.d.ts +6 -0
- package/dist/src/active-inference/test-value-integration.js +168 -0
- package/dist/src/active-inference/types.d.ts +150 -0
- package/dist/src/active-inference/types.js +59 -0
- package/dist/src/active-inference/value-integration.d.ts +164 -0
- package/dist/src/active-inference/value-integration.js +459 -0
- package/dist/src/agents/base-agent.d.ts +53 -0
- package/dist/src/agents/base-agent.js +178 -0
- package/dist/src/agents/builder.d.ts +67 -0
- package/dist/src/agents/builder.js +537 -0
- package/dist/src/agents/critic.d.ts +35 -0
- package/dist/src/agents/critic.js +322 -0
- package/dist/src/agents/ethicist.d.ts +54 -0
- package/dist/src/agents/ethicist.js +393 -0
- package/dist/src/agents/explorer.d.ts +26 -0
- package/dist/src/agents/explorer.js +216 -0
- package/dist/src/agents/feeling.d.ts +41 -0
- package/dist/src/agents/feeling.js +320 -0
- package/dist/src/agents/index.d.ts +111 -0
- package/dist/src/agents/index.js +222 -0
- package/dist/src/agents/memory.d.ts +69 -0
- package/dist/src/agents/memory.js +404 -0
- package/dist/src/agents/message-bus.d.ts +88 -0
- package/dist/src/agents/message-bus.js +267 -0
- package/dist/src/agents/narrator.d.ts +90 -0
- package/dist/src/agents/narrator.js +473 -0
- package/dist/src/agents/planner.d.ts +38 -0
- package/dist/src/agents/planner.js +341 -0
- package/dist/src/agents/predictor.d.ts +73 -0
- package/dist/src/agents/predictor.js +506 -0
- package/dist/src/agents/sensor.d.ts +88 -0
- package/dist/src/agents/sensor.js +377 -0
- package/dist/src/agents/test-agents.d.ts +6 -0
- package/dist/src/agents/test-agents.js +73 -0
- package/dist/src/agents/types.d.ts +194 -0
- package/dist/src/agents/types.js +7 -0
- package/dist/src/brain/index.d.ts +185 -0
- package/dist/src/brain/index.js +843 -0
- package/dist/src/brain/trace.d.ts +91 -0
- package/dist/src/brain/trace.js +327 -0
- package/dist/src/brain/types.d.ts +165 -0
- package/dist/src/brain/types.js +51 -0
- package/dist/src/cli/chat.d.ts +237 -0
- package/dist/src/cli/chat.js +1959 -0
- package/dist/src/cli/dispatcher.d.ts +182 -0
- package/dist/src/cli/dispatcher.js +718 -0
- package/dist/src/cli/human-loop.d.ts +170 -0
- package/dist/src/cli/human-loop.js +543 -0
- package/dist/src/cli/index.d.ts +12 -0
- package/dist/src/cli/index.js +28 -0
- package/dist/src/cli/interactive.d.ts +141 -0
- package/dist/src/cli/interactive.js +757 -0
- package/dist/src/cli/ui.d.ts +205 -0
- package/dist/src/cli/ui.js +632 -0
- package/dist/src/consciousness/attention-schema.d.ts +154 -0
- package/dist/src/consciousness/attention-schema.js +432 -0
- package/dist/src/consciousness/global-workspace.d.ts +149 -0
- package/dist/src/consciousness/global-workspace.js +422 -0
- package/dist/src/consciousness/index.d.ts +186 -0
- package/dist/src/consciousness/index.js +476 -0
- package/dist/src/consciousness/phi-calculator.d.ts +119 -0
- package/dist/src/consciousness/phi-calculator.js +445 -0
- package/dist/src/consciousness/phi-decisions.d.ts +169 -0
- package/dist/src/consciousness/phi-decisions.js +383 -0
- package/dist/src/consciousness/phi-monitor.d.ts +153 -0
- package/dist/src/consciousness/phi-monitor.js +465 -0
- package/dist/src/consciousness/types.d.ts +260 -0
- package/dist/src/consciousness/types.js +44 -0
- package/dist/src/daemon/dream-mode.d.ts +115 -0
- package/dist/src/daemon/dream-mode.js +470 -0
- package/dist/src/daemon/index.d.ts +162 -0
- package/dist/src/daemon/index.js +542 -0
- package/dist/src/daemon/maintenance.d.ts +139 -0
- package/dist/src/daemon/maintenance.js +549 -0
- package/dist/src/daemon/process.d.ts +82 -0
- package/dist/src/daemon/process.js +442 -0
- package/dist/src/daemon/scheduler.d.ts +90 -0
- package/dist/src/daemon/scheduler.js +494 -0
- package/dist/src/daemon/types.d.ts +213 -0
- package/dist/src/daemon/types.js +50 -0
- package/dist/src/epistemic/index.d.ts +74 -0
- package/dist/src/epistemic/index.js +225 -0
- package/dist/src/grounding/epistemic-stack.d.ts +100 -0
- package/dist/src/grounding/epistemic-stack.js +408 -0
- package/dist/src/grounding/feedback.d.ts +98 -0
- package/dist/src/grounding/feedback.js +276 -0
- package/dist/src/grounding/index.d.ts +123 -0
- package/dist/src/grounding/index.js +224 -0
- package/dist/src/grounding/verifier.d.ts +149 -0
- package/dist/src/grounding/verifier.js +484 -0
- package/dist/src/healing/detector.d.ts +110 -0
- package/dist/src/healing/detector.js +436 -0
- package/dist/src/healing/fixer.d.ts +138 -0
- package/dist/src/healing/fixer.js +572 -0
- package/dist/src/healing/index.d.ts +23 -0
- package/dist/src/healing/index.js +43 -0
- package/dist/src/hooks/index.d.ts +135 -0
- package/dist/src/hooks/index.js +317 -0
- package/dist/src/index.d.ts +23 -0
- package/dist/src/index.js +1266 -0
- package/dist/src/kernel/index.d.ts +155 -0
- package/dist/src/kernel/index.js +795 -0
- package/dist/src/kernel/invariants.d.ts +153 -0
- package/dist/src/kernel/invariants.js +355 -0
- package/dist/src/kernel/test-kernel.d.ts +6 -0
- package/dist/src/kernel/test-kernel.js +108 -0
- package/dist/src/kernel/test-real-mcp.d.ts +10 -0
- package/dist/src/kernel/test-real-mcp.js +295 -0
- package/dist/src/llm/index.d.ts +146 -0
- package/dist/src/llm/index.js +428 -0
- package/dist/src/llm/router.d.ts +136 -0
- package/dist/src/llm/router.js +510 -0
- package/dist/src/mcp/index.d.ts +85 -0
- package/dist/src/mcp/index.js +657 -0
- package/dist/src/mcp/resilient.d.ts +139 -0
- package/dist/src/mcp/resilient.js +417 -0
- package/dist/src/memory/cache.d.ts +118 -0
- package/dist/src/memory/cache.js +356 -0
- package/dist/src/memory/cognitive-workspace.d.ts +231 -0
- package/dist/src/memory/cognitive-workspace.js +521 -0
- package/dist/src/memory/consolidation.d.ts +99 -0
- package/dist/src/memory/consolidation.js +443 -0
- package/dist/src/memory/episodic.d.ts +114 -0
- package/dist/src/memory/episodic.js +394 -0
- package/dist/src/memory/forgetting.d.ts +134 -0
- package/dist/src/memory/forgetting.js +324 -0
- package/dist/src/memory/index.d.ts +211 -0
- package/dist/src/memory/index.js +367 -0
- package/dist/src/memory/indexer.d.ts +123 -0
- package/dist/src/memory/indexer.js +479 -0
- package/dist/src/memory/procedural.d.ts +136 -0
- package/dist/src/memory/procedural.js +479 -0
- package/dist/src/memory/semantic.d.ts +132 -0
- package/dist/src/memory/semantic.js +497 -0
- package/dist/src/memory/types.d.ts +193 -0
- package/dist/src/memory/types.js +15 -0
- package/dist/src/orchestrator.d.ts +65 -0
- package/dist/src/orchestrator.js +317 -0
- package/dist/src/persistence/index.d.ts +257 -0
- package/dist/src/persistence/index.js +763 -0
- package/dist/src/pipeline/executor.d.ts +51 -0
- package/dist/src/pipeline/executor.js +695 -0
- package/dist/src/pipeline/index.d.ts +7 -0
- package/dist/src/pipeline/index.js +11 -0
- package/dist/src/self-production.d.ts +67 -0
- package/dist/src/self-production.js +205 -0
- package/dist/src/subagents/executor.d.ts +58 -0
- package/dist/src/subagents/executor.js +283 -0
- package/dist/src/subagents/index.d.ts +37 -0
- package/dist/src/subagents/index.js +53 -0
- package/dist/src/subagents/registry.d.ts +23 -0
- package/dist/src/subagents/registry.js +167 -0
- package/dist/src/subagents/types.d.ts +79 -0
- package/dist/src/subagents/types.js +14 -0
- package/dist/src/tools/bash.d.ts +139 -0
- package/dist/src/tools/bash.js +583 -0
- package/dist/src/tools/edit.d.ts +125 -0
- package/dist/src/tools/edit.js +424 -0
- package/dist/src/tools/git.d.ts +179 -0
- package/dist/src/tools/git.js +504 -0
- package/dist/src/tools/index.d.ts +21 -0
- package/dist/src/tools/index.js +163 -0
- package/dist/src/types.d.ts +145 -0
- package/dist/src/types.js +7 -0
- package/dist/src/world-model/decoder.d.ts +163 -0
- package/dist/src/world-model/decoder.js +517 -0
- package/dist/src/world-model/digital-twin.d.ts +219 -0
- package/dist/src/world-model/digital-twin.js +695 -0
- package/dist/src/world-model/encoder.d.ts +141 -0
- package/dist/src/world-model/encoder.js +564 -0
- package/dist/src/world-model/index.d.ts +221 -0
- package/dist/src/world-model/index.js +772 -0
- package/dist/src/world-model/predictor.d.ts +161 -0
- package/dist/src/world-model/predictor.js +681 -0
- package/dist/src/world-model/test-value-jepa.d.ts +8 -0
- package/dist/src/world-model/test-value-jepa.js +430 -0
- package/dist/src/world-model/types.d.ts +341 -0
- package/dist/src/world-model/types.js +69 -0
- package/dist/src/world-model/value-jepa.d.ts +247 -0
- package/dist/src/world-model/value-jepa.js +622 -0
- package/dist/test/brain.test.d.ts +11 -0
- package/dist/test/brain.test.js +358 -0
- package/dist/test/cli/dispatcher.test.d.ts +4 -0
- package/dist/test/cli/dispatcher.test.js +332 -0
- package/dist/test/cli/human-loop.test.d.ts +4 -0
- package/dist/test/cli/human-loop.test.js +270 -0
- package/dist/test/grounding/feedback.test.d.ts +4 -0
- package/dist/test/grounding/feedback.test.js +462 -0
- package/dist/test/grounding/verifier.test.d.ts +4 -0
- package/dist/test/grounding/verifier.test.js +442 -0
- package/dist/test/grounding.test.d.ts +6 -0
- package/dist/test/grounding.test.js +246 -0
- package/dist/test/healing/detector.test.d.ts +4 -0
- package/dist/test/healing/detector.test.js +266 -0
- package/dist/test/healing/fixer.test.d.ts +4 -0
- package/dist/test/healing/fixer.test.js +369 -0
- package/dist/test/integration.test.d.ts +5 -0
- package/dist/test/integration.test.js +290 -0
- package/dist/test/tools/bash.test.d.ts +4 -0
- package/dist/test/tools/bash.test.js +348 -0
- package/dist/test/tools/edit.test.d.ts +4 -0
- package/dist/test/tools/edit.test.js +350 -0
- package/dist/test/tools/git.test.d.ts +4 -0
- package/dist/test/tools/git.test.js +350 -0
- package/package.json +60 -0
|
@@ -0,0 +1,698 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Genesis 6.1 - Active Inference Integration
|
|
4
|
+
*
|
|
5
|
+
* Connects Active Inference to Kernel and Daemon for real autonomous operation.
|
|
6
|
+
*
|
|
7
|
+
* This module:
|
|
8
|
+
* - Bridges observations from Kernel state (energy, task status, agents)
|
|
9
|
+
* - Connects actions to Kernel operations (submit tasks, manage energy)
|
|
10
|
+
* - Registers Active Inference as a daemon scheduled task
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { integrateActiveInference } from './active-inference/integration.js';
|
|
15
|
+
* import { getKernel } from './kernel/index.js';
|
|
16
|
+
* import { getDaemon } from './daemon/index.js';
|
|
17
|
+
*
|
|
18
|
+
* const kernel = getKernel();
|
|
19
|
+
* const daemon = getDaemon();
|
|
20
|
+
*
|
|
21
|
+
* // Connect everything
|
|
22
|
+
* const aiLoop = integrateActiveInference(kernel, daemon, {
|
|
23
|
+
* enableDaemonTask: true,
|
|
24
|
+
* cycleInterval: 5000,
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Or run manually
|
|
28
|
+
* await aiLoop.run(100);
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
+
exports.DEFAULT_INTEGRATION_CONFIG = void 0;
|
|
33
|
+
exports.createKernelObservationBridge = createKernelObservationBridge;
|
|
34
|
+
exports.registerKernelActions = registerKernelActions;
|
|
35
|
+
exports.registerDaemonTask = registerDaemonTask;
|
|
36
|
+
exports.integrateActiveInference = integrateActiveInference;
|
|
37
|
+
exports.createIntegratedSystem = createIntegratedSystem;
|
|
38
|
+
exports.createMCPObservationBridge = createMCPObservationBridge;
|
|
39
|
+
exports.createMCPInferenceLoop = createMCPInferenceLoop;
|
|
40
|
+
const autonomous_loop_js_1 = require("./autonomous-loop.js");
|
|
41
|
+
const observations_js_1 = require("./observations.js");
|
|
42
|
+
const actions_js_1 = require("./actions.js");
|
|
43
|
+
exports.DEFAULT_INTEGRATION_CONFIG = {
|
|
44
|
+
cycleInterval: 1000,
|
|
45
|
+
maxCycles: 0,
|
|
46
|
+
enableDaemonTask: true,
|
|
47
|
+
daemonTaskInterval: 5000,
|
|
48
|
+
daemonTaskPriority: 'normal',
|
|
49
|
+
stopOnDormant: true,
|
|
50
|
+
stopOnError: false,
|
|
51
|
+
verbose: false,
|
|
52
|
+
};
|
|
53
|
+
// ============================================================================
|
|
54
|
+
// Kernel State Mapping
|
|
55
|
+
// ============================================================================
|
|
56
|
+
/**
|
|
57
|
+
* Maps KernelState to task status for observations
|
|
58
|
+
*/
|
|
59
|
+
function mapKernelStateToTaskStatus(state) {
|
|
60
|
+
switch (state) {
|
|
61
|
+
case 'idle':
|
|
62
|
+
return 'none';
|
|
63
|
+
case 'sensing':
|
|
64
|
+
case 'thinking':
|
|
65
|
+
case 'deciding':
|
|
66
|
+
return 'pending';
|
|
67
|
+
case 'acting':
|
|
68
|
+
case 'reflecting':
|
|
69
|
+
case 'self_improving':
|
|
70
|
+
return 'running';
|
|
71
|
+
case 'dormant':
|
|
72
|
+
return 'none';
|
|
73
|
+
case 'error':
|
|
74
|
+
return 'failed';
|
|
75
|
+
default:
|
|
76
|
+
return 'none';
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// ============================================================================
|
|
80
|
+
// Observation Bridge
|
|
81
|
+
// ============================================================================
|
|
82
|
+
/**
|
|
83
|
+
* Creates an observation gatherer connected to Kernel state
|
|
84
|
+
*/
|
|
85
|
+
function createKernelObservationBridge(kernel) {
|
|
86
|
+
const gatherer = (0, observations_js_1.createObservationGatherer)();
|
|
87
|
+
gatherer.configure({
|
|
88
|
+
// Map Kernel energy and state to observations
|
|
89
|
+
kernelState: () => ({
|
|
90
|
+
energy: kernel.getEnergy(),
|
|
91
|
+
state: kernel.getState(),
|
|
92
|
+
taskStatus: mapKernelStateToTaskStatus(kernel.getState()),
|
|
93
|
+
}),
|
|
94
|
+
// Phi state (placeholder - would connect to consciousness module)
|
|
95
|
+
phiState: () => {
|
|
96
|
+
// TODO: Connect to phi-monitor when available
|
|
97
|
+
const state = kernel.getState();
|
|
98
|
+
return {
|
|
99
|
+
phi: state === 'dormant' ? 0.1 : state === 'error' ? 0.3 : 0.7,
|
|
100
|
+
state: state === 'dormant' ? 'dormant' : 'aware',
|
|
101
|
+
};
|
|
102
|
+
},
|
|
103
|
+
// Sensor result from agent health
|
|
104
|
+
sensorResult: async () => {
|
|
105
|
+
const status = kernel.getStatus();
|
|
106
|
+
const healthRatio = status.agents.healthy / Math.max(1, status.agents.total);
|
|
107
|
+
return {
|
|
108
|
+
success: healthRatio > 0.5,
|
|
109
|
+
latency: healthRatio > 0.8 ? 100 : healthRatio > 0.5 ? 2000 : 10000,
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
// World model coherence from agent health and invariants
|
|
113
|
+
worldModelState: () => {
|
|
114
|
+
const status = kernel.getStatus();
|
|
115
|
+
const unhealthy = status.agents.total - status.agents.healthy;
|
|
116
|
+
return {
|
|
117
|
+
consistent: unhealthy === 0,
|
|
118
|
+
issues: unhealthy,
|
|
119
|
+
};
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
return gatherer;
|
|
123
|
+
}
|
|
124
|
+
// ============================================================================
|
|
125
|
+
// Action Executors
|
|
126
|
+
// ============================================================================
|
|
127
|
+
/**
|
|
128
|
+
* Registers action executors that connect to Kernel operations
|
|
129
|
+
*/
|
|
130
|
+
function registerKernelActions(kernel) {
|
|
131
|
+
// sense.mcp: Use Kernel's sensor agent
|
|
132
|
+
(0, actions_js_1.registerAction)('sense.mcp', async (context) => {
|
|
133
|
+
try {
|
|
134
|
+
// Get kernel status as "sensory data"
|
|
135
|
+
const status = kernel.getStatus();
|
|
136
|
+
return {
|
|
137
|
+
success: true,
|
|
138
|
+
action: 'sense.mcp',
|
|
139
|
+
data: {
|
|
140
|
+
state: status.state,
|
|
141
|
+
energy: status.energy,
|
|
142
|
+
agents: status.agents,
|
|
143
|
+
tasks: status.tasks,
|
|
144
|
+
},
|
|
145
|
+
duration: 0,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
return {
|
|
150
|
+
success: false,
|
|
151
|
+
action: 'sense.mcp',
|
|
152
|
+
error: error instanceof Error ? error.message : String(error),
|
|
153
|
+
duration: 0,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
// recall.memory: Query memory via Kernel
|
|
158
|
+
(0, actions_js_1.registerAction)('recall.memory', async (context) => {
|
|
159
|
+
try {
|
|
160
|
+
// TODO: Connect to Memory agent when integrated
|
|
161
|
+
return {
|
|
162
|
+
success: true,
|
|
163
|
+
action: 'recall.memory',
|
|
164
|
+
data: { recalled: [], query: context.goal },
|
|
165
|
+
duration: 0,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
return {
|
|
170
|
+
success: false,
|
|
171
|
+
action: 'recall.memory',
|
|
172
|
+
error: error instanceof Error ? error.message : String(error),
|
|
173
|
+
duration: 0,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
// plan.goals: Submit planning task to Kernel
|
|
178
|
+
(0, actions_js_1.registerAction)('plan.goals', async (context) => {
|
|
179
|
+
try {
|
|
180
|
+
if (context.goal) {
|
|
181
|
+
const taskId = await kernel.submit({
|
|
182
|
+
type: 'query',
|
|
183
|
+
goal: `Plan: ${context.goal}`,
|
|
184
|
+
priority: 'normal',
|
|
185
|
+
requester: 'active-inference',
|
|
186
|
+
});
|
|
187
|
+
return {
|
|
188
|
+
success: true,
|
|
189
|
+
action: 'plan.goals',
|
|
190
|
+
data: { taskId, goal: context.goal },
|
|
191
|
+
duration: 0,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
success: true,
|
|
196
|
+
action: 'plan.goals',
|
|
197
|
+
data: { goal: null },
|
|
198
|
+
duration: 0,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
return {
|
|
203
|
+
success: false,
|
|
204
|
+
action: 'plan.goals',
|
|
205
|
+
error: error instanceof Error ? error.message : String(error),
|
|
206
|
+
duration: 0,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
// verify.ethics: Ethical check via Kernel (delegated to ethicist agent)
|
|
211
|
+
(0, actions_js_1.registerAction)('verify.ethics', async (context) => {
|
|
212
|
+
// The Kernel already requires ethical checks for all tasks
|
|
213
|
+
// This action is more of an explicit verification
|
|
214
|
+
return {
|
|
215
|
+
success: true,
|
|
216
|
+
action: 'verify.ethics',
|
|
217
|
+
data: { approved: true, priority: 'flourishing' },
|
|
218
|
+
duration: 0,
|
|
219
|
+
};
|
|
220
|
+
});
|
|
221
|
+
// execute.task: Execute a task via Kernel
|
|
222
|
+
(0, actions_js_1.registerAction)('execute.task', async (context) => {
|
|
223
|
+
try {
|
|
224
|
+
if (context.taskId) {
|
|
225
|
+
// Task already submitted, just acknowledge
|
|
226
|
+
return {
|
|
227
|
+
success: true,
|
|
228
|
+
action: 'execute.task',
|
|
229
|
+
data: { taskId: context.taskId, status: 'delegated' },
|
|
230
|
+
duration: 0,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
if (context.goal) {
|
|
234
|
+
const taskId = await kernel.submit({
|
|
235
|
+
type: 'build',
|
|
236
|
+
goal: context.goal,
|
|
237
|
+
priority: 'normal',
|
|
238
|
+
requester: 'active-inference',
|
|
239
|
+
context: context.parameters,
|
|
240
|
+
});
|
|
241
|
+
return {
|
|
242
|
+
success: true,
|
|
243
|
+
action: 'execute.task',
|
|
244
|
+
data: { taskId },
|
|
245
|
+
duration: 0,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
return {
|
|
249
|
+
success: true,
|
|
250
|
+
action: 'execute.task',
|
|
251
|
+
data: { message: 'No task to execute' },
|
|
252
|
+
duration: 0,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
return {
|
|
257
|
+
success: false,
|
|
258
|
+
action: 'execute.task',
|
|
259
|
+
error: error instanceof Error ? error.message : String(error),
|
|
260
|
+
duration: 0,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
// dream.cycle: Trigger dream mode (requires daemon)
|
|
265
|
+
(0, actions_js_1.registerAction)('dream.cycle', async (context) => {
|
|
266
|
+
// Dream mode is handled by daemon, not kernel directly
|
|
267
|
+
return {
|
|
268
|
+
success: true,
|
|
269
|
+
action: 'dream.cycle',
|
|
270
|
+
data: { consolidated: 0, patterns: [] },
|
|
271
|
+
duration: 0,
|
|
272
|
+
};
|
|
273
|
+
});
|
|
274
|
+
// rest.idle: Do nothing, let Kernel rest
|
|
275
|
+
(0, actions_js_1.registerAction)('rest.idle', async (_context) => {
|
|
276
|
+
// Explicitly do nothing - this is Wu Wei
|
|
277
|
+
return {
|
|
278
|
+
success: true,
|
|
279
|
+
action: 'rest.idle',
|
|
280
|
+
data: { rested: true },
|
|
281
|
+
duration: 0,
|
|
282
|
+
};
|
|
283
|
+
});
|
|
284
|
+
// recharge: Restore energy
|
|
285
|
+
(0, actions_js_1.registerAction)('recharge', async (context) => {
|
|
286
|
+
try {
|
|
287
|
+
const currentEnergy = kernel.getEnergy();
|
|
288
|
+
const rechargeAmount = 0.1; // 10% per recharge
|
|
289
|
+
const newEnergy = Math.min(1.0, currentEnergy + rechargeAmount);
|
|
290
|
+
kernel.setEnergy(newEnergy);
|
|
291
|
+
return {
|
|
292
|
+
success: true,
|
|
293
|
+
action: 'recharge',
|
|
294
|
+
data: {
|
|
295
|
+
previousEnergy: currentEnergy,
|
|
296
|
+
newEnergy,
|
|
297
|
+
recharged: newEnergy - currentEnergy,
|
|
298
|
+
},
|
|
299
|
+
duration: 0,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
return {
|
|
304
|
+
success: false,
|
|
305
|
+
action: 'recharge',
|
|
306
|
+
error: error instanceof Error ? error.message : String(error),
|
|
307
|
+
duration: 0,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
// ============================================================================
|
|
313
|
+
// Daemon Task Registration
|
|
314
|
+
// ============================================================================
|
|
315
|
+
/**
|
|
316
|
+
* Registers Active Inference as a daemon scheduled task
|
|
317
|
+
*/
|
|
318
|
+
function registerDaemonTask(daemon, loop, config) {
|
|
319
|
+
daemon.schedule({
|
|
320
|
+
name: 'active-inference',
|
|
321
|
+
description: 'Run Active Inference cycle for autonomous decision-making',
|
|
322
|
+
schedule: { type: 'interval', intervalMs: config.daemonTaskInterval },
|
|
323
|
+
priority: config.daemonTaskPriority,
|
|
324
|
+
handler: async (ctx) => {
|
|
325
|
+
ctx.logger.debug('Running Active Inference cycle');
|
|
326
|
+
try {
|
|
327
|
+
// Run a single inference cycle
|
|
328
|
+
const action = await loop.cycle();
|
|
329
|
+
return {
|
|
330
|
+
success: true,
|
|
331
|
+
duration: 0,
|
|
332
|
+
output: {
|
|
333
|
+
action,
|
|
334
|
+
beliefs: loop.getMostLikelyState(),
|
|
335
|
+
cycle: loop.getCycleCount(),
|
|
336
|
+
},
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
catch (err) {
|
|
340
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
341
|
+
ctx.logger.error(`Active Inference error: ${error.message}`, error);
|
|
342
|
+
return {
|
|
343
|
+
success: false,
|
|
344
|
+
duration: 0,
|
|
345
|
+
error,
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
tags: ['system', 'ai', 'autonomous'],
|
|
350
|
+
retries: 0, // Don't retry - just run next cycle
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
// ============================================================================
|
|
354
|
+
// Integration Function
|
|
355
|
+
// ============================================================================
|
|
356
|
+
/**
|
|
357
|
+
* Integrates Active Inference with Kernel and Daemon
|
|
358
|
+
*
|
|
359
|
+
* @param kernel - The Genesis Kernel instance
|
|
360
|
+
* @param daemon - The Genesis Daemon instance (optional)
|
|
361
|
+
* @param config - Integration configuration
|
|
362
|
+
* @returns Configured AutonomousLoop
|
|
363
|
+
*/
|
|
364
|
+
function integrateActiveInference(kernel, daemon, config = {}) {
|
|
365
|
+
const fullConfig = { ...exports.DEFAULT_INTEGRATION_CONFIG, ...config };
|
|
366
|
+
// Create the loop
|
|
367
|
+
const loop = (0, autonomous_loop_js_1.createAutonomousLoop)({
|
|
368
|
+
cycleInterval: fullConfig.cycleInterval,
|
|
369
|
+
maxCycles: fullConfig.maxCycles,
|
|
370
|
+
stopOnEnergyCritical: fullConfig.stopOnDormant,
|
|
371
|
+
verbose: fullConfig.verbose,
|
|
372
|
+
});
|
|
373
|
+
// Connect observations to Kernel
|
|
374
|
+
const observationBridge = createKernelObservationBridge(kernel);
|
|
375
|
+
const components = loop.getComponents();
|
|
376
|
+
// Replace the observations gatherer configuration
|
|
377
|
+
components.observations.configure({
|
|
378
|
+
kernelState: () => ({
|
|
379
|
+
energy: kernel.getEnergy(),
|
|
380
|
+
state: kernel.getState(),
|
|
381
|
+
taskStatus: mapKernelStateToTaskStatus(kernel.getState()),
|
|
382
|
+
}),
|
|
383
|
+
phiState: () => {
|
|
384
|
+
const state = kernel.getState();
|
|
385
|
+
return {
|
|
386
|
+
phi: state === 'dormant' ? 0.1 : state === 'error' ? 0.3 : 0.7,
|
|
387
|
+
state: state === 'dormant' ? 'dormant' : 'aware',
|
|
388
|
+
};
|
|
389
|
+
},
|
|
390
|
+
sensorResult: async () => {
|
|
391
|
+
const status = kernel.getStatus();
|
|
392
|
+
const healthRatio = status.agents.healthy / Math.max(1, status.agents.total);
|
|
393
|
+
return {
|
|
394
|
+
success: healthRatio > 0.5,
|
|
395
|
+
latency: healthRatio > 0.8 ? 100 : 2000,
|
|
396
|
+
};
|
|
397
|
+
},
|
|
398
|
+
worldModelState: () => {
|
|
399
|
+
const status = kernel.getStatus();
|
|
400
|
+
return {
|
|
401
|
+
consistent: status.agents.healthy === status.agents.total,
|
|
402
|
+
issues: status.agents.total - status.agents.healthy,
|
|
403
|
+
};
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
// Register Kernel action executors
|
|
407
|
+
registerKernelActions(kernel);
|
|
408
|
+
// Connect to Kernel state changes
|
|
409
|
+
kernel.onStateChange((newState, prevState) => {
|
|
410
|
+
if (fullConfig.verbose) {
|
|
411
|
+
console.log(`[AI Integration] Kernel state: ${prevState} -> ${newState}`);
|
|
412
|
+
}
|
|
413
|
+
// Stop inference if kernel enters dormant or error state
|
|
414
|
+
if (fullConfig.stopOnDormant && newState === 'dormant') {
|
|
415
|
+
if (loop.isRunning()) {
|
|
416
|
+
loop.stop('kernel_dormant');
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (fullConfig.stopOnError && newState === 'error') {
|
|
420
|
+
if (loop.isRunning()) {
|
|
421
|
+
loop.stop('kernel_error');
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
// Register as daemon task if daemon provided
|
|
426
|
+
if (daemon && fullConfig.enableDaemonTask) {
|
|
427
|
+
registerDaemonTask(daemon, loop, fullConfig);
|
|
428
|
+
// Subscribe to daemon events
|
|
429
|
+
daemon.on((event) => {
|
|
430
|
+
if (event.type === 'daemon_stopped') {
|
|
431
|
+
if (loop.isRunning()) {
|
|
432
|
+
loop.stop('daemon_stopped');
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
// When dream mode starts, pause inference
|
|
436
|
+
if (event.type === 'dream_started') {
|
|
437
|
+
if (loop.isRunning()) {
|
|
438
|
+
loop.stop('dreaming');
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
return loop;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Create a fully integrated autonomous system
|
|
447
|
+
*/
|
|
448
|
+
async function createIntegratedSystem(config = {}) {
|
|
449
|
+
// Dynamic imports to avoid circular dependencies
|
|
450
|
+
const { getKernel } = await import('../kernel/index.js');
|
|
451
|
+
const { getDaemon } = await import('../daemon/index.js');
|
|
452
|
+
const kernel = getKernel();
|
|
453
|
+
// Build daemon dependencies from kernel
|
|
454
|
+
const daemonDeps = {
|
|
455
|
+
kernel: {
|
|
456
|
+
checkAgentHealth: async () => {
|
|
457
|
+
const status = kernel.getStatus();
|
|
458
|
+
// Return health for each agent type
|
|
459
|
+
const agents = kernel.getAgents();
|
|
460
|
+
return Array.from(agents.entries()).map(([type, agent]) => ({
|
|
461
|
+
id: agent.id,
|
|
462
|
+
healthy: ['idle', 'working', 'waiting'].includes(agent.health().state),
|
|
463
|
+
latency: 100,
|
|
464
|
+
}));
|
|
465
|
+
},
|
|
466
|
+
checkInvariants: async () => {
|
|
467
|
+
// Get invariant registry and check all
|
|
468
|
+
const registry = kernel.getInvariantRegistry();
|
|
469
|
+
const invariants = registry.getAll();
|
|
470
|
+
return invariants.map((inv) => ({
|
|
471
|
+
id: inv.id,
|
|
472
|
+
satisfied: true, // We can't check without context
|
|
473
|
+
message: inv.description,
|
|
474
|
+
}));
|
|
475
|
+
},
|
|
476
|
+
repairInvariant: async (id) => {
|
|
477
|
+
// Kernel doesn't have direct repair, return false
|
|
478
|
+
return false;
|
|
479
|
+
},
|
|
480
|
+
getState: () => ({
|
|
481
|
+
state: kernel.getState(),
|
|
482
|
+
energy: kernel.getEnergy(),
|
|
483
|
+
}),
|
|
484
|
+
rechargeEnergy: (amount) => {
|
|
485
|
+
kernel.setEnergy(kernel.getEnergy() + amount);
|
|
486
|
+
},
|
|
487
|
+
resetState: () => {
|
|
488
|
+
// Reset to idle state if possible
|
|
489
|
+
// The kernel manages its own state, so this is limited
|
|
490
|
+
},
|
|
491
|
+
},
|
|
492
|
+
};
|
|
493
|
+
const daemon = getDaemon(daemonDeps);
|
|
494
|
+
const loop = integrateActiveInference(kernel, daemon, config);
|
|
495
|
+
return {
|
|
496
|
+
kernel,
|
|
497
|
+
daemon,
|
|
498
|
+
loop,
|
|
499
|
+
start: async () => {
|
|
500
|
+
await kernel.start();
|
|
501
|
+
daemon.start();
|
|
502
|
+
},
|
|
503
|
+
stop: async () => {
|
|
504
|
+
if (loop.isRunning()) {
|
|
505
|
+
loop.stop('system_shutdown');
|
|
506
|
+
}
|
|
507
|
+
daemon.stop();
|
|
508
|
+
await kernel.stop();
|
|
509
|
+
},
|
|
510
|
+
status: () => ({
|
|
511
|
+
kernel: kernel.getStatus(),
|
|
512
|
+
daemon: daemon.status(),
|
|
513
|
+
inference: {
|
|
514
|
+
running: loop.isRunning(),
|
|
515
|
+
cycles: loop.getCycleCount(),
|
|
516
|
+
beliefs: loop.getMostLikelyState(),
|
|
517
|
+
},
|
|
518
|
+
}),
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Creates observation sources backed by real MCP calls
|
|
523
|
+
*
|
|
524
|
+
* Maps MCPs to observations:
|
|
525
|
+
* - memory.read_graph → energy (from entity state) + worldModel coherence
|
|
526
|
+
* - brave-search → tool perception (latency, success)
|
|
527
|
+
* - gemini → phi state (from search activity)
|
|
528
|
+
*/
|
|
529
|
+
async function createMCPObservationBridge(config = {}) {
|
|
530
|
+
const { getMCPClient } = await import('../mcp/index.js');
|
|
531
|
+
const mcpClient = getMCPClient();
|
|
532
|
+
// Track latest MCP results
|
|
533
|
+
let lastMemoryResult = null;
|
|
534
|
+
let lastSearchResult = null;
|
|
535
|
+
let mcpLatencies = {};
|
|
536
|
+
// Fetch memory graph (cached for 5 seconds)
|
|
537
|
+
let memoryCache = null;
|
|
538
|
+
const CACHE_TTL = 5000;
|
|
539
|
+
async function fetchMemoryGraph() {
|
|
540
|
+
const now = Date.now();
|
|
541
|
+
if (memoryCache && now - memoryCache.timestamp < CACHE_TTL) {
|
|
542
|
+
return memoryCache.data;
|
|
543
|
+
}
|
|
544
|
+
const start = Date.now();
|
|
545
|
+
try {
|
|
546
|
+
const result = await mcpClient.call('memory', 'read_graph', {});
|
|
547
|
+
mcpLatencies['memory'] = Date.now() - start;
|
|
548
|
+
lastMemoryResult = result.success ? result.data : null;
|
|
549
|
+
memoryCache = { data: lastMemoryResult, timestamp: now };
|
|
550
|
+
return lastMemoryResult;
|
|
551
|
+
}
|
|
552
|
+
catch (error) {
|
|
553
|
+
mcpLatencies['memory'] = Date.now() - start;
|
|
554
|
+
if (config.verbose) {
|
|
555
|
+
console.log(`[MCP Obs] Memory fetch error: ${error}`);
|
|
556
|
+
}
|
|
557
|
+
return null;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
// Calculate energy from memory graph
|
|
561
|
+
function calculateEnergy(memoryGraph) {
|
|
562
|
+
if (!memoryGraph?.entities)
|
|
563
|
+
return 0.5;
|
|
564
|
+
// More entities = more active system = higher energy
|
|
565
|
+
const entityCount = memoryGraph.entities.length;
|
|
566
|
+
const relationCount = memoryGraph.relations?.length ?? 0;
|
|
567
|
+
// Normalize: 0-10 entities = 0.3-1.0 energy
|
|
568
|
+
const entityEnergy = Math.min(1.0, 0.3 + (entityCount / 10) * 0.7);
|
|
569
|
+
const relationBonus = Math.min(0.2, relationCount * 0.02);
|
|
570
|
+
return Math.min(1.0, entityEnergy + relationBonus);
|
|
571
|
+
}
|
|
572
|
+
// Calculate world model coherence
|
|
573
|
+
function calculateCoherence(memoryGraph) {
|
|
574
|
+
if (!memoryGraph) {
|
|
575
|
+
return { consistent: false, issues: 1 };
|
|
576
|
+
}
|
|
577
|
+
// Check for dangling relations (referencing non-existent entities)
|
|
578
|
+
const entityNames = new Set((memoryGraph.entities || []).map((e) => e.name));
|
|
579
|
+
const relations = memoryGraph.relations || [];
|
|
580
|
+
let issues = 0;
|
|
581
|
+
for (const rel of relations) {
|
|
582
|
+
if (!entityNames.has(rel.from))
|
|
583
|
+
issues++;
|
|
584
|
+
if (!entityNames.has(rel.to))
|
|
585
|
+
issues++;
|
|
586
|
+
}
|
|
587
|
+
return { consistent: issues === 0, issues };
|
|
588
|
+
}
|
|
589
|
+
return {
|
|
590
|
+
kernelState: () => {
|
|
591
|
+
// Use cached memory result
|
|
592
|
+
const energy = memoryCache?.data ? calculateEnergy(memoryCache.data) : 0.5;
|
|
593
|
+
const avgLatency = Object.values(mcpLatencies).reduce((a, b) => a + b, 0) /
|
|
594
|
+
Math.max(1, Object.keys(mcpLatencies).length);
|
|
595
|
+
// Infer state from latency
|
|
596
|
+
let state = 'idle';
|
|
597
|
+
if (avgLatency < 500)
|
|
598
|
+
state = 'active';
|
|
599
|
+
else if (avgLatency < 2000)
|
|
600
|
+
state = 'sensing';
|
|
601
|
+
else
|
|
602
|
+
state = 'slow';
|
|
603
|
+
return {
|
|
604
|
+
energy,
|
|
605
|
+
state,
|
|
606
|
+
taskStatus: 'none',
|
|
607
|
+
};
|
|
608
|
+
},
|
|
609
|
+
phiState: () => {
|
|
610
|
+
// Infer phi from MCP responsiveness
|
|
611
|
+
const avgLatency = Object.values(mcpLatencies).reduce((a, b) => a + b, 0) /
|
|
612
|
+
Math.max(1, Object.keys(mcpLatencies).length);
|
|
613
|
+
const successCount = Object.keys(mcpLatencies).length;
|
|
614
|
+
// Phi represents integrated information (how well MCPs are working together)
|
|
615
|
+
let phi = 0.5;
|
|
616
|
+
if (successCount >= 3 && avgLatency < 1000)
|
|
617
|
+
phi = 0.9;
|
|
618
|
+
else if (successCount >= 2 && avgLatency < 2000)
|
|
619
|
+
phi = 0.7;
|
|
620
|
+
else if (successCount >= 1)
|
|
621
|
+
phi = 0.4;
|
|
622
|
+
else
|
|
623
|
+
phi = 0.1;
|
|
624
|
+
return {
|
|
625
|
+
phi,
|
|
626
|
+
state: phi > 0.6 ? 'aware' : phi > 0.3 ? 'alert' : 'drowsy',
|
|
627
|
+
};
|
|
628
|
+
},
|
|
629
|
+
sensorResult: async () => {
|
|
630
|
+
// Actually call an MCP to measure real latency
|
|
631
|
+
const start = Date.now();
|
|
632
|
+
try {
|
|
633
|
+
// Fetch memory graph (this is the primary sensor)
|
|
634
|
+
await fetchMemoryGraph();
|
|
635
|
+
// Also try a quick brave search to test connectivity
|
|
636
|
+
const searchStart = Date.now();
|
|
637
|
+
try {
|
|
638
|
+
const searchResult = await mcpClient.call('brave-search', 'brave_web_search', {
|
|
639
|
+
query: 'test connection',
|
|
640
|
+
count: 1,
|
|
641
|
+
});
|
|
642
|
+
mcpLatencies['brave-search'] = Date.now() - searchStart;
|
|
643
|
+
lastSearchResult = searchResult.success ? searchResult.data : null;
|
|
644
|
+
}
|
|
645
|
+
catch (e) {
|
|
646
|
+
mcpLatencies['brave-search'] = Date.now() - searchStart;
|
|
647
|
+
if (config.verbose) {
|
|
648
|
+
console.log(`[MCP Obs] Brave search error: ${e}`);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
const totalLatency = Date.now() - start;
|
|
652
|
+
return {
|
|
653
|
+
success: true,
|
|
654
|
+
latency: totalLatency,
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
catch (error) {
|
|
658
|
+
return {
|
|
659
|
+
success: false,
|
|
660
|
+
latency: Date.now() - start,
|
|
661
|
+
error: String(error),
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
},
|
|
665
|
+
worldModelState: () => {
|
|
666
|
+
return calculateCoherence(memoryCache?.data);
|
|
667
|
+
},
|
|
668
|
+
lastMCPResults: () => ({
|
|
669
|
+
memory: lastMemoryResult,
|
|
670
|
+
search: lastSearchResult,
|
|
671
|
+
latencies: { ...mcpLatencies },
|
|
672
|
+
}),
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* Create Active Inference loop with real MCP observations
|
|
677
|
+
*/
|
|
678
|
+
async function createMCPInferenceLoop(config = {}) {
|
|
679
|
+
const fullConfig = { ...exports.DEFAULT_INTEGRATION_CONFIG, ...config };
|
|
680
|
+
// Create MCP observation bridge
|
|
681
|
+
const mcpBridge = await createMCPObservationBridge({ verbose: fullConfig.verbose });
|
|
682
|
+
// Create the loop
|
|
683
|
+
const loop = (0, autonomous_loop_js_1.createAutonomousLoop)({
|
|
684
|
+
cycleInterval: fullConfig.cycleInterval,
|
|
685
|
+
maxCycles: fullConfig.maxCycles,
|
|
686
|
+
stopOnEnergyCritical: true,
|
|
687
|
+
verbose: fullConfig.verbose,
|
|
688
|
+
});
|
|
689
|
+
// Configure observations with MCP sources
|
|
690
|
+
const components = loop.getComponents();
|
|
691
|
+
components.observations.configure({
|
|
692
|
+
kernelState: mcpBridge.kernelState,
|
|
693
|
+
phiState: mcpBridge.phiState,
|
|
694
|
+
sensorResult: mcpBridge.sensorResult,
|
|
695
|
+
worldModelState: mcpBridge.worldModelState,
|
|
696
|
+
});
|
|
697
|
+
return { loop, mcpBridge };
|
|
698
|
+
}
|