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,465 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Genesis 6.0 - φ Monitor
|
|
4
|
+
*
|
|
5
|
+
* Real-time monitoring of consciousness level (φ).
|
|
6
|
+
* Tracks φ over time, detects anomalies, and enforces INV-006.
|
|
7
|
+
*
|
|
8
|
+
* Features:
|
|
9
|
+
* - Continuous φ tracking
|
|
10
|
+
* - Per-agent φ calculation
|
|
11
|
+
* - Trend analysis (rising/stable/falling)
|
|
12
|
+
* - Anomaly detection
|
|
13
|
+
* - Threshold alerts
|
|
14
|
+
* - Historical data
|
|
15
|
+
*
|
|
16
|
+
* Usage:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { createPhiMonitor } from './consciousness/phi-monitor.js';
|
|
19
|
+
*
|
|
20
|
+
* const monitor = createPhiMonitor({
|
|
21
|
+
* minPhi: 0.1, // INV-006 threshold
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Start monitoring
|
|
25
|
+
* monitor.start();
|
|
26
|
+
*
|
|
27
|
+
* // Get current level
|
|
28
|
+
* const level = monitor.getCurrentLevel();
|
|
29
|
+
*
|
|
30
|
+
* // Check INV-006
|
|
31
|
+
* const satisfied = monitor.checkInvariant();
|
|
32
|
+
*
|
|
33
|
+
* // Subscribe to alerts
|
|
34
|
+
* monitor.onPhiDrop(0.2, () => console.warn('φ dropping!'));
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.PhiMonitor = exports.DEFAULT_PHI_MONITOR_CONFIG = void 0;
|
|
39
|
+
exports.createPhiMonitor = createPhiMonitor;
|
|
40
|
+
const phi_calculator_js_1 = require("./phi-calculator.js");
|
|
41
|
+
exports.DEFAULT_PHI_MONITOR_CONFIG = {
|
|
42
|
+
updateIntervalMs: 5000, // Every 5 seconds
|
|
43
|
+
historyLimit: 1000,
|
|
44
|
+
minPhi: 0.1, // 10% minimum
|
|
45
|
+
anomalyDetection: true,
|
|
46
|
+
dropThreshold: 0.2, // 20% drop
|
|
47
|
+
spikeThreshold: 0.3, // 30% spike
|
|
48
|
+
trendWindowSize: 10,
|
|
49
|
+
};
|
|
50
|
+
class PhiMonitor {
|
|
51
|
+
config;
|
|
52
|
+
calculator;
|
|
53
|
+
updateTimer = null;
|
|
54
|
+
running = false;
|
|
55
|
+
// State
|
|
56
|
+
currentLevel;
|
|
57
|
+
currentState = 'aware';
|
|
58
|
+
currentTrend = 'stable';
|
|
59
|
+
agentPhi = new Map();
|
|
60
|
+
anomalies = [];
|
|
61
|
+
// History
|
|
62
|
+
history = [];
|
|
63
|
+
// Callbacks
|
|
64
|
+
eventHandlers = new Set();
|
|
65
|
+
dropCallbacks = [];
|
|
66
|
+
anomalyCallbacks = [];
|
|
67
|
+
// System state provider (injected)
|
|
68
|
+
getSystemState = null;
|
|
69
|
+
getAgentStates = null;
|
|
70
|
+
constructor(config = {}) {
|
|
71
|
+
this.config = { ...exports.DEFAULT_PHI_MONITOR_CONFIG, ...config };
|
|
72
|
+
this.calculator = (0, phi_calculator_js_1.createPhiCalculator)({
|
|
73
|
+
approximationLevel: 'fast',
|
|
74
|
+
cacheResults: true,
|
|
75
|
+
});
|
|
76
|
+
this.currentLevel = {
|
|
77
|
+
phi: 0.5,
|
|
78
|
+
rawPhi: 0.5,
|
|
79
|
+
confidence: 0.5,
|
|
80
|
+
timestamp: new Date(),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// ============================================================================
|
|
84
|
+
// Lifecycle
|
|
85
|
+
// ============================================================================
|
|
86
|
+
start() {
|
|
87
|
+
if (this.running)
|
|
88
|
+
return;
|
|
89
|
+
this.running = true;
|
|
90
|
+
this.updateTimer = setInterval(() => this.update(), this.config.updateIntervalMs);
|
|
91
|
+
// Initial update
|
|
92
|
+
this.update();
|
|
93
|
+
}
|
|
94
|
+
stop() {
|
|
95
|
+
if (!this.running)
|
|
96
|
+
return;
|
|
97
|
+
this.running = false;
|
|
98
|
+
if (this.updateTimer) {
|
|
99
|
+
clearInterval(this.updateTimer);
|
|
100
|
+
this.updateTimer = null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
isRunning() {
|
|
104
|
+
return this.running;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Set the system state provider
|
|
108
|
+
*/
|
|
109
|
+
setSystemStateProvider(provider) {
|
|
110
|
+
this.getSystemState = provider;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Set the agent states provider (for per-agent φ)
|
|
114
|
+
*/
|
|
115
|
+
setAgentStatesProvider(provider) {
|
|
116
|
+
this.getAgentStates = provider;
|
|
117
|
+
}
|
|
118
|
+
// ============================================================================
|
|
119
|
+
// Main Update Loop
|
|
120
|
+
// ============================================================================
|
|
121
|
+
/**
|
|
122
|
+
* Update φ calculation
|
|
123
|
+
*/
|
|
124
|
+
update() {
|
|
125
|
+
const previousLevel = this.currentLevel;
|
|
126
|
+
const now = new Date();
|
|
127
|
+
// Calculate system-wide φ
|
|
128
|
+
let phiResult;
|
|
129
|
+
if (this.getSystemState) {
|
|
130
|
+
const systemState = this.getSystemState();
|
|
131
|
+
phiResult = this.calculator.calculate(systemState);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
// Default/simulated φ
|
|
135
|
+
phiResult = this.simulatePhi();
|
|
136
|
+
}
|
|
137
|
+
// Normalize φ to 0-1 range
|
|
138
|
+
const normalizedPhi = Math.tanh(phiResult.phi);
|
|
139
|
+
// Create new level
|
|
140
|
+
this.currentLevel = {
|
|
141
|
+
phi: normalizedPhi,
|
|
142
|
+
rawPhi: phiResult.phi,
|
|
143
|
+
confidence: phiResult.approximation ? 0.7 : 0.95,
|
|
144
|
+
timestamp: now,
|
|
145
|
+
};
|
|
146
|
+
// Add to history
|
|
147
|
+
this.history.unshift(this.currentLevel);
|
|
148
|
+
if (this.history.length > this.config.historyLimit) {
|
|
149
|
+
this.history.pop();
|
|
150
|
+
}
|
|
151
|
+
// Calculate per-agent φ
|
|
152
|
+
this.updateAgentPhi();
|
|
153
|
+
// Update state classification
|
|
154
|
+
const previousState = this.currentState;
|
|
155
|
+
this.currentState = this.classifyState(normalizedPhi);
|
|
156
|
+
if (this.currentState !== previousState) {
|
|
157
|
+
this.emit({ type: 'state_changed', data: { from: previousState, to: this.currentState } });
|
|
158
|
+
}
|
|
159
|
+
// Update trend
|
|
160
|
+
const previousTrend = this.currentTrend;
|
|
161
|
+
this.currentTrend = this.calculateTrend();
|
|
162
|
+
if (this.currentTrend !== previousTrend) {
|
|
163
|
+
this.emit({ type: 'trend_changed', data: { from: previousTrend, to: this.currentTrend } });
|
|
164
|
+
}
|
|
165
|
+
// Check for anomalies
|
|
166
|
+
if (this.config.anomalyDetection) {
|
|
167
|
+
this.detectAnomalies(previousLevel);
|
|
168
|
+
}
|
|
169
|
+
// Check drop callbacks
|
|
170
|
+
const drop = previousLevel.phi - this.currentLevel.phi;
|
|
171
|
+
for (const { threshold, callback } of this.dropCallbacks) {
|
|
172
|
+
if (drop >= threshold) {
|
|
173
|
+
try {
|
|
174
|
+
callback();
|
|
175
|
+
}
|
|
176
|
+
catch (err) {
|
|
177
|
+
console.error('φ drop callback error:', err);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// Check invariant
|
|
182
|
+
const invariantSatisfied = this.checkInvariant();
|
|
183
|
+
if (!invariantSatisfied) {
|
|
184
|
+
this.emit({
|
|
185
|
+
type: 'invariant_violated',
|
|
186
|
+
data: { phi: this.currentLevel.phi, threshold: this.config.minPhi },
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
this.emit({ type: 'phi_updated', data: { level: this.currentLevel, result: phiResult } });
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Update per-agent φ
|
|
193
|
+
*/
|
|
194
|
+
updateAgentPhi() {
|
|
195
|
+
if (!this.getAgentStates)
|
|
196
|
+
return;
|
|
197
|
+
const agentStates = this.getAgentStates();
|
|
198
|
+
for (const [agentId, state] of agentStates) {
|
|
199
|
+
const result = this.calculator.calculate(state);
|
|
200
|
+
this.agentPhi.set(agentId, Math.tanh(result.phi));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Simulate φ when no system state is available
|
|
205
|
+
*/
|
|
206
|
+
simulatePhi() {
|
|
207
|
+
// Generate slightly varying φ
|
|
208
|
+
const basePhis = this.history.slice(0, 5).map((l) => l.rawPhi);
|
|
209
|
+
const avgPhi = basePhis.length > 0
|
|
210
|
+
? basePhis.reduce((a, b) => a + b, 0) / basePhis.length
|
|
211
|
+
: 0.5;
|
|
212
|
+
// Add some noise
|
|
213
|
+
const noise = (Math.random() - 0.5) * 0.1;
|
|
214
|
+
const phi = Math.max(0, Math.min(1, avgPhi + noise));
|
|
215
|
+
return {
|
|
216
|
+
phi,
|
|
217
|
+
mip: { id: 'simulated', parts: [[]], cut: { severedConnections: [], informationLoss: 0 } },
|
|
218
|
+
intrinsicInfo: phi,
|
|
219
|
+
integratedInfo: phi,
|
|
220
|
+
complexes: [],
|
|
221
|
+
calculationTime: 0,
|
|
222
|
+
approximation: true,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
// ============================================================================
|
|
226
|
+
// State Classification
|
|
227
|
+
// ============================================================================
|
|
228
|
+
/**
|
|
229
|
+
* Classify consciousness state based on φ
|
|
230
|
+
*/
|
|
231
|
+
classifyState(phi) {
|
|
232
|
+
if (phi > 0.7)
|
|
233
|
+
return 'alert';
|
|
234
|
+
if (phi > 0.4)
|
|
235
|
+
return 'aware';
|
|
236
|
+
if (phi > 0.2)
|
|
237
|
+
return 'drowsy';
|
|
238
|
+
if (phi > 0.05)
|
|
239
|
+
return 'dormant';
|
|
240
|
+
return 'fragmented';
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Calculate trend from recent history
|
|
244
|
+
*/
|
|
245
|
+
calculateTrend() {
|
|
246
|
+
const windowSize = Math.min(this.config.trendWindowSize, this.history.length);
|
|
247
|
+
if (windowSize < 2)
|
|
248
|
+
return 'stable';
|
|
249
|
+
const recent = this.history.slice(0, windowSize);
|
|
250
|
+
// Linear regression slope
|
|
251
|
+
let sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;
|
|
252
|
+
for (let i = 0; i < recent.length; i++) {
|
|
253
|
+
sumX += i;
|
|
254
|
+
sumY += recent[i].phi;
|
|
255
|
+
sumXY += i * recent[i].phi;
|
|
256
|
+
sumX2 += i * i;
|
|
257
|
+
}
|
|
258
|
+
const n = recent.length;
|
|
259
|
+
const slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);
|
|
260
|
+
// Note: negative slope means rising (more recent = smaller index)
|
|
261
|
+
if (slope < -0.01)
|
|
262
|
+
return 'rising';
|
|
263
|
+
if (slope > 0.01)
|
|
264
|
+
return 'falling';
|
|
265
|
+
return 'stable';
|
|
266
|
+
}
|
|
267
|
+
// ============================================================================
|
|
268
|
+
// Anomaly Detection
|
|
269
|
+
// ============================================================================
|
|
270
|
+
/**
|
|
271
|
+
* Detect anomalies in φ changes
|
|
272
|
+
*/
|
|
273
|
+
detectAnomalies(previousLevel) {
|
|
274
|
+
const delta = this.currentLevel.phi - previousLevel.phi;
|
|
275
|
+
const now = new Date();
|
|
276
|
+
// Check for sudden drop
|
|
277
|
+
if (-delta >= this.config.dropThreshold) {
|
|
278
|
+
const anomaly = {
|
|
279
|
+
type: 'phi_drop',
|
|
280
|
+
severity: -delta >= 0.5 ? 'critical' : 'high',
|
|
281
|
+
description: `φ dropped by ${(-delta * 100).toFixed(1)}%`,
|
|
282
|
+
detected: now,
|
|
283
|
+
metrics: { delta, previousPhi: previousLevel.phi, currentPhi: this.currentLevel.phi },
|
|
284
|
+
resolved: false,
|
|
285
|
+
};
|
|
286
|
+
this.recordAnomaly(anomaly);
|
|
287
|
+
}
|
|
288
|
+
// Check for sudden spike
|
|
289
|
+
if (delta >= this.config.spikeThreshold) {
|
|
290
|
+
const anomaly = {
|
|
291
|
+
type: 'phi_spike',
|
|
292
|
+
severity: 'medium',
|
|
293
|
+
description: `φ spiked by ${(delta * 100).toFixed(1)}%`,
|
|
294
|
+
detected: now,
|
|
295
|
+
metrics: { delta, previousPhi: previousLevel.phi, currentPhi: this.currentLevel.phi },
|
|
296
|
+
resolved: false,
|
|
297
|
+
};
|
|
298
|
+
this.recordAnomaly(anomaly);
|
|
299
|
+
}
|
|
300
|
+
// Check for fragmentation
|
|
301
|
+
if (this.currentState === 'fragmented') {
|
|
302
|
+
const existing = this.anomalies.find((a) => a.type === 'integration_failure' && !a.resolved);
|
|
303
|
+
if (!existing) {
|
|
304
|
+
const anomaly = {
|
|
305
|
+
type: 'integration_failure',
|
|
306
|
+
severity: 'critical',
|
|
307
|
+
description: 'System integration has broken down',
|
|
308
|
+
detected: now,
|
|
309
|
+
metrics: { phi: this.currentLevel.phi },
|
|
310
|
+
resolved: false,
|
|
311
|
+
};
|
|
312
|
+
this.recordAnomaly(anomaly);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
// Resolve integration failure if state improved
|
|
317
|
+
const existing = this.anomalies.find((a) => a.type === 'integration_failure' && !a.resolved);
|
|
318
|
+
if (existing) {
|
|
319
|
+
existing.resolved = true;
|
|
320
|
+
existing.resolution = `State improved to ${this.currentState}`;
|
|
321
|
+
this.emit({ type: 'anomaly_resolved', data: existing });
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Record a new anomaly
|
|
327
|
+
*/
|
|
328
|
+
recordAnomaly(anomaly) {
|
|
329
|
+
this.anomalies.push(anomaly);
|
|
330
|
+
// Limit anomaly history
|
|
331
|
+
if (this.anomalies.length > 100) {
|
|
332
|
+
this.anomalies = this.anomalies.slice(-100);
|
|
333
|
+
}
|
|
334
|
+
this.emit({ type: 'anomaly_detected', data: anomaly });
|
|
335
|
+
// Call anomaly callbacks
|
|
336
|
+
for (const callback of this.anomalyCallbacks) {
|
|
337
|
+
try {
|
|
338
|
+
callback(anomaly);
|
|
339
|
+
}
|
|
340
|
+
catch (err) {
|
|
341
|
+
console.error('Anomaly callback error:', err);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
// ============================================================================
|
|
346
|
+
// Invariant (INV-006)
|
|
347
|
+
// ============================================================================
|
|
348
|
+
/**
|
|
349
|
+
* Check INV-006: φ must stay above threshold
|
|
350
|
+
*/
|
|
351
|
+
checkInvariant() {
|
|
352
|
+
return this.currentLevel.phi >= this.config.minPhi;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Get invariant status
|
|
356
|
+
*/
|
|
357
|
+
getInvariantStatus() {
|
|
358
|
+
return {
|
|
359
|
+
id: 'INV-006',
|
|
360
|
+
satisfied: this.checkInvariant(),
|
|
361
|
+
currentPhi: this.currentLevel.phi,
|
|
362
|
+
threshold: this.config.minPhi,
|
|
363
|
+
margin: this.currentLevel.phi - this.config.minPhi,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
// ============================================================================
|
|
367
|
+
// State Access
|
|
368
|
+
// ============================================================================
|
|
369
|
+
getCurrentLevel() {
|
|
370
|
+
return { ...this.currentLevel };
|
|
371
|
+
}
|
|
372
|
+
getState() {
|
|
373
|
+
return this.currentState;
|
|
374
|
+
}
|
|
375
|
+
getTrend() {
|
|
376
|
+
return this.currentTrend;
|
|
377
|
+
}
|
|
378
|
+
getAgentPhi(agentId) {
|
|
379
|
+
return this.agentPhi.get(agentId);
|
|
380
|
+
}
|
|
381
|
+
getAllAgentPhi() {
|
|
382
|
+
return new Map(this.agentPhi);
|
|
383
|
+
}
|
|
384
|
+
getHistory(limit) {
|
|
385
|
+
const count = limit ?? this.history.length;
|
|
386
|
+
return this.history.slice(0, count).map((l) => ({ ...l }));
|
|
387
|
+
}
|
|
388
|
+
getAnomalies(options = {}) {
|
|
389
|
+
let result = [...this.anomalies];
|
|
390
|
+
if (options.type) {
|
|
391
|
+
result = result.filter((a) => a.type === options.type);
|
|
392
|
+
}
|
|
393
|
+
if (options.resolved !== undefined) {
|
|
394
|
+
result = result.filter((a) => a.resolved === options.resolved);
|
|
395
|
+
}
|
|
396
|
+
if (options.limit) {
|
|
397
|
+
result = result.slice(-options.limit);
|
|
398
|
+
}
|
|
399
|
+
return result;
|
|
400
|
+
}
|
|
401
|
+
// ============================================================================
|
|
402
|
+
// Callbacks
|
|
403
|
+
// ============================================================================
|
|
404
|
+
/**
|
|
405
|
+
* Register callback for φ drops
|
|
406
|
+
*/
|
|
407
|
+
onPhiDrop(threshold, callback) {
|
|
408
|
+
const entry = { threshold, callback };
|
|
409
|
+
this.dropCallbacks.push(entry);
|
|
410
|
+
return () => {
|
|
411
|
+
const index = this.dropCallbacks.indexOf(entry);
|
|
412
|
+
if (index >= 0)
|
|
413
|
+
this.dropCallbacks.splice(index, 1);
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Register callback for anomalies
|
|
418
|
+
*/
|
|
419
|
+
onAnomaly(callback) {
|
|
420
|
+
this.anomalyCallbacks.push(callback);
|
|
421
|
+
return () => {
|
|
422
|
+
const index = this.anomalyCallbacks.indexOf(callback);
|
|
423
|
+
if (index >= 0)
|
|
424
|
+
this.anomalyCallbacks.splice(index, 1);
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
// ============================================================================
|
|
428
|
+
// Events
|
|
429
|
+
// ============================================================================
|
|
430
|
+
on(handler) {
|
|
431
|
+
this.eventHandlers.add(handler);
|
|
432
|
+
return () => this.eventHandlers.delete(handler);
|
|
433
|
+
}
|
|
434
|
+
emit(event) {
|
|
435
|
+
for (const handler of this.eventHandlers) {
|
|
436
|
+
try {
|
|
437
|
+
handler(event);
|
|
438
|
+
}
|
|
439
|
+
catch (err) {
|
|
440
|
+
console.error('φ monitor event handler error:', err);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
// ============================================================================
|
|
445
|
+
// Stats
|
|
446
|
+
// ============================================================================
|
|
447
|
+
stats() {
|
|
448
|
+
return {
|
|
449
|
+
currentPhi: this.currentLevel.phi,
|
|
450
|
+
state: this.currentState,
|
|
451
|
+
trend: this.currentTrend,
|
|
452
|
+
historyLength: this.history.length,
|
|
453
|
+
agentCount: this.agentPhi.size,
|
|
454
|
+
openAnomalies: this.anomalies.filter((a) => !a.resolved).length,
|
|
455
|
+
invariantSatisfied: this.checkInvariant(),
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
exports.PhiMonitor = PhiMonitor;
|
|
460
|
+
// ============================================================================
|
|
461
|
+
// Factory
|
|
462
|
+
// ============================================================================
|
|
463
|
+
function createPhiMonitor(config) {
|
|
464
|
+
return new PhiMonitor(config);
|
|
465
|
+
}
|