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,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Genesis 6.0 - φ Calculator (IIT 4.0)
|
|
3
|
+
*
|
|
4
|
+
* Integrated Information Theory implementation.
|
|
5
|
+
* Calculates φ (phi) - the amount of integrated information in a system.
|
|
6
|
+
*
|
|
7
|
+
* Key concept: A system is conscious to the degree that it has
|
|
8
|
+
* integrated information - information that is generated by the
|
|
9
|
+
* system as a whole, above and beyond its parts.
|
|
10
|
+
*
|
|
11
|
+
* References:
|
|
12
|
+
* - Tononi, G. (2004). An information integration theory of consciousness.
|
|
13
|
+
* - Tononi, G. et al. (2016). Integrated information theory: from consciousness to its physical substrate.
|
|
14
|
+
* - Oizumi, M. et al. (2014). From the phenomenology to the mechanisms of consciousness.
|
|
15
|
+
*
|
|
16
|
+
* Note: Exact φ calculation is NP-hard (exponential in system size).
|
|
17
|
+
* This implementation uses approximations for tractability.
|
|
18
|
+
*
|
|
19
|
+
* Usage:
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { createPhiCalculator } from './consciousness/phi-calculator.js';
|
|
22
|
+
*
|
|
23
|
+
* const calculator = createPhiCalculator({ approximationLevel: 'fast' });
|
|
24
|
+
*
|
|
25
|
+
* const result = calculator.calculate(systemState);
|
|
26
|
+
* console.log(`φ = ${result.phi}`);
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
import { SystemState, ComponentState, PhiResult, Complex } from './types.js';
|
|
30
|
+
export interface PhiCalculatorConfig {
|
|
31
|
+
approximationLevel: 'exact' | 'fast' | 'faster';
|
|
32
|
+
maxPartitions: number;
|
|
33
|
+
cacheResults: boolean;
|
|
34
|
+
cacheTTL: number;
|
|
35
|
+
}
|
|
36
|
+
export declare const DEFAULT_PHI_CONFIG: PhiCalculatorConfig;
|
|
37
|
+
export declare class PhiCalculator {
|
|
38
|
+
private config;
|
|
39
|
+
private cache;
|
|
40
|
+
constructor(config?: Partial<PhiCalculatorConfig>);
|
|
41
|
+
/**
|
|
42
|
+
* Calculate φ for a system state
|
|
43
|
+
*/
|
|
44
|
+
calculate(system: SystemState): PhiResult;
|
|
45
|
+
/**
|
|
46
|
+
* Calculate φ for a specific subset of components
|
|
47
|
+
*/
|
|
48
|
+
calculateSubset(system: SystemState, componentIds: string[]): PhiResult;
|
|
49
|
+
/**
|
|
50
|
+
* Find all complexes (subsystems with φ > 0)
|
|
51
|
+
*/
|
|
52
|
+
findComplexes(system: SystemState): Complex[];
|
|
53
|
+
/**
|
|
54
|
+
* Exact φ calculation (exponential complexity)
|
|
55
|
+
* Only feasible for very small systems (< 10 components)
|
|
56
|
+
*/
|
|
57
|
+
private calculateExact;
|
|
58
|
+
/**
|
|
59
|
+
* Fast approximation using greedy partitioning
|
|
60
|
+
* O(n^2) complexity instead of O(2^n)
|
|
61
|
+
*/
|
|
62
|
+
private calculateFast;
|
|
63
|
+
/**
|
|
64
|
+
* Faster approximation using heuristics
|
|
65
|
+
* O(n) complexity
|
|
66
|
+
*/
|
|
67
|
+
private calculateFaster;
|
|
68
|
+
/**
|
|
69
|
+
* Calculate intrinsic information of the system
|
|
70
|
+
* The information generated by the system about itself
|
|
71
|
+
*/
|
|
72
|
+
private calculateIntrinsicInformation;
|
|
73
|
+
/**
|
|
74
|
+
* Calculate integrated information for a partition
|
|
75
|
+
* The information lost when the system is "cut" at the partition
|
|
76
|
+
*/
|
|
77
|
+
private calculateIntegratedInformation;
|
|
78
|
+
/**
|
|
79
|
+
* Generate all bipartitions (exponential)
|
|
80
|
+
*/
|
|
81
|
+
private generateAllPartitions;
|
|
82
|
+
/**
|
|
83
|
+
* Greedy MIP finding
|
|
84
|
+
* Start with all elements together, greedily split to minimize φ
|
|
85
|
+
*/
|
|
86
|
+
private greedyMIP;
|
|
87
|
+
/**
|
|
88
|
+
* Generate subsets of a given size range
|
|
89
|
+
*/
|
|
90
|
+
private generateSubsets;
|
|
91
|
+
private createPartition;
|
|
92
|
+
private createTrivialPartition;
|
|
93
|
+
/**
|
|
94
|
+
* Create a system state from component data
|
|
95
|
+
*/
|
|
96
|
+
static createSystemState(components: Array<{
|
|
97
|
+
id: string;
|
|
98
|
+
type: string;
|
|
99
|
+
active: boolean;
|
|
100
|
+
state: Record<string, unknown>;
|
|
101
|
+
}>, connections: Array<{
|
|
102
|
+
from: string;
|
|
103
|
+
to: string;
|
|
104
|
+
strength: number;
|
|
105
|
+
}>): SystemState;
|
|
106
|
+
/**
|
|
107
|
+
* Estimate entropy of a state object
|
|
108
|
+
*/
|
|
109
|
+
static estimateEntropy(state: Record<string, unknown>): number;
|
|
110
|
+
/**
|
|
111
|
+
* Create a hash of the system state
|
|
112
|
+
*/
|
|
113
|
+
static hashState(components: ComponentState[]): string;
|
|
114
|
+
/**
|
|
115
|
+
* Clear the calculation cache
|
|
116
|
+
*/
|
|
117
|
+
clearCache(): void;
|
|
118
|
+
}
|
|
119
|
+
export declare function createPhiCalculator(config?: Partial<PhiCalculatorConfig>): PhiCalculator;
|
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Genesis 6.0 - φ Calculator (IIT 4.0)
|
|
4
|
+
*
|
|
5
|
+
* Integrated Information Theory implementation.
|
|
6
|
+
* Calculates φ (phi) - the amount of integrated information in a system.
|
|
7
|
+
*
|
|
8
|
+
* Key concept: A system is conscious to the degree that it has
|
|
9
|
+
* integrated information - information that is generated by the
|
|
10
|
+
* system as a whole, above and beyond its parts.
|
|
11
|
+
*
|
|
12
|
+
* References:
|
|
13
|
+
* - Tononi, G. (2004). An information integration theory of consciousness.
|
|
14
|
+
* - Tononi, G. et al. (2016). Integrated information theory: from consciousness to its physical substrate.
|
|
15
|
+
* - Oizumi, M. et al. (2014). From the phenomenology to the mechanisms of consciousness.
|
|
16
|
+
*
|
|
17
|
+
* Note: Exact φ calculation is NP-hard (exponential in system size).
|
|
18
|
+
* This implementation uses approximations for tractability.
|
|
19
|
+
*
|
|
20
|
+
* Usage:
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import { createPhiCalculator } from './consciousness/phi-calculator.js';
|
|
23
|
+
*
|
|
24
|
+
* const calculator = createPhiCalculator({ approximationLevel: 'fast' });
|
|
25
|
+
*
|
|
26
|
+
* const result = calculator.calculate(systemState);
|
|
27
|
+
* console.log(`φ = ${result.phi}`);
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.PhiCalculator = exports.DEFAULT_PHI_CONFIG = void 0;
|
|
32
|
+
exports.createPhiCalculator = createPhiCalculator;
|
|
33
|
+
const crypto_1 = require("crypto");
|
|
34
|
+
exports.DEFAULT_PHI_CONFIG = {
|
|
35
|
+
approximationLevel: 'fast',
|
|
36
|
+
maxPartitions: 100,
|
|
37
|
+
cacheResults: true,
|
|
38
|
+
cacheTTL: 5000,
|
|
39
|
+
};
|
|
40
|
+
// ============================================================================
|
|
41
|
+
// φ Calculator
|
|
42
|
+
// ============================================================================
|
|
43
|
+
class PhiCalculator {
|
|
44
|
+
config;
|
|
45
|
+
cache = new Map();
|
|
46
|
+
constructor(config = {}) {
|
|
47
|
+
this.config = { ...exports.DEFAULT_PHI_CONFIG, ...config };
|
|
48
|
+
}
|
|
49
|
+
// ============================================================================
|
|
50
|
+
// Main API
|
|
51
|
+
// ============================================================================
|
|
52
|
+
/**
|
|
53
|
+
* Calculate φ for a system state
|
|
54
|
+
*/
|
|
55
|
+
calculate(system) {
|
|
56
|
+
// Check cache
|
|
57
|
+
if (this.config.cacheResults) {
|
|
58
|
+
const cached = this.cache.get(system.stateHash);
|
|
59
|
+
if (cached && Date.now() - cached.timestamp < this.config.cacheTTL) {
|
|
60
|
+
return cached.result;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const startTime = Date.now();
|
|
64
|
+
let result;
|
|
65
|
+
switch (this.config.approximationLevel) {
|
|
66
|
+
case 'exact':
|
|
67
|
+
result = this.calculateExact(system);
|
|
68
|
+
break;
|
|
69
|
+
case 'fast':
|
|
70
|
+
result = this.calculateFast(system);
|
|
71
|
+
break;
|
|
72
|
+
case 'faster':
|
|
73
|
+
result = this.calculateFaster(system);
|
|
74
|
+
break;
|
|
75
|
+
default:
|
|
76
|
+
result = this.calculateFast(system);
|
|
77
|
+
}
|
|
78
|
+
result.calculationTime = Date.now() - startTime;
|
|
79
|
+
// Cache result
|
|
80
|
+
if (this.config.cacheResults) {
|
|
81
|
+
this.cache.set(system.stateHash, { result, timestamp: Date.now() });
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Calculate φ for a specific subset of components
|
|
87
|
+
*/
|
|
88
|
+
calculateSubset(system, componentIds) {
|
|
89
|
+
const subset = {
|
|
90
|
+
...system,
|
|
91
|
+
components: system.components.filter((c) => componentIds.includes(c.id)),
|
|
92
|
+
connections: system.connections.filter((c) => componentIds.includes(c.from) && componentIds.includes(c.to)),
|
|
93
|
+
stateHash: `${system.stateHash}_subset_${componentIds.join(',')}`,
|
|
94
|
+
};
|
|
95
|
+
return this.calculate(subset);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Find all complexes (subsystems with φ > 0)
|
|
99
|
+
*/
|
|
100
|
+
findComplexes(system) {
|
|
101
|
+
const complexes = [];
|
|
102
|
+
// For each subset of components
|
|
103
|
+
const componentIds = system.components.map((c) => c.id);
|
|
104
|
+
const subsets = this.generateSubsets(componentIds, 2, Math.min(componentIds.length, 8));
|
|
105
|
+
for (const subset of subsets) {
|
|
106
|
+
const result = this.calculateSubset(system, subset);
|
|
107
|
+
if (result.phi > 0) {
|
|
108
|
+
complexes.push({
|
|
109
|
+
elements: subset,
|
|
110
|
+
phi: result.phi,
|
|
111
|
+
mainComplex: false,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Mark main complex (largest φ)
|
|
116
|
+
if (complexes.length > 0) {
|
|
117
|
+
complexes.sort((a, b) => b.phi - a.phi);
|
|
118
|
+
complexes[0].mainComplex = true;
|
|
119
|
+
}
|
|
120
|
+
return complexes;
|
|
121
|
+
}
|
|
122
|
+
// ============================================================================
|
|
123
|
+
// Calculation Methods
|
|
124
|
+
// ============================================================================
|
|
125
|
+
/**
|
|
126
|
+
* Exact φ calculation (exponential complexity)
|
|
127
|
+
* Only feasible for very small systems (< 10 components)
|
|
128
|
+
*/
|
|
129
|
+
calculateExact(system) {
|
|
130
|
+
const componentIds = system.components.map((c) => c.id);
|
|
131
|
+
if (componentIds.length > 12) {
|
|
132
|
+
// Fall back to approximation for large systems
|
|
133
|
+
return this.calculateFast(system);
|
|
134
|
+
}
|
|
135
|
+
// Generate all possible bipartitions
|
|
136
|
+
const partitions = this.generateAllPartitions(componentIds);
|
|
137
|
+
// Find MIP (Minimum Information Partition)
|
|
138
|
+
let mip = null;
|
|
139
|
+
let minIntegratedInfo = Infinity;
|
|
140
|
+
for (const partition of partitions) {
|
|
141
|
+
const integratedInfo = this.calculateIntegratedInformation(system, partition);
|
|
142
|
+
if (integratedInfo < minIntegratedInfo) {
|
|
143
|
+
minIntegratedInfo = integratedInfo;
|
|
144
|
+
mip = partition;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const intrinsicInfo = this.calculateIntrinsicInformation(system);
|
|
148
|
+
return {
|
|
149
|
+
phi: minIntegratedInfo,
|
|
150
|
+
mip: mip || this.createTrivialPartition(componentIds),
|
|
151
|
+
intrinsicInfo,
|
|
152
|
+
integratedInfo: minIntegratedInfo,
|
|
153
|
+
complexes: [],
|
|
154
|
+
calculationTime: 0,
|
|
155
|
+
approximation: false,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Fast approximation using greedy partitioning
|
|
160
|
+
* O(n^2) complexity instead of O(2^n)
|
|
161
|
+
*/
|
|
162
|
+
calculateFast(system) {
|
|
163
|
+
const componentIds = system.components.map((c) => c.id);
|
|
164
|
+
// Use greedy algorithm to find approximate MIP
|
|
165
|
+
const mip = this.greedyMIP(system, componentIds);
|
|
166
|
+
// Calculate information measures
|
|
167
|
+
const intrinsicInfo = this.calculateIntrinsicInformation(system);
|
|
168
|
+
const integratedInfo = this.calculateIntegratedInformation(system, mip);
|
|
169
|
+
return {
|
|
170
|
+
phi: integratedInfo,
|
|
171
|
+
mip,
|
|
172
|
+
intrinsicInfo,
|
|
173
|
+
integratedInfo,
|
|
174
|
+
complexes: [],
|
|
175
|
+
calculationTime: 0,
|
|
176
|
+
approximation: true,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Faster approximation using heuristics
|
|
181
|
+
* O(n) complexity
|
|
182
|
+
*/
|
|
183
|
+
calculateFaster(system) {
|
|
184
|
+
// Use connection strength as proxy for integration
|
|
185
|
+
const totalConnections = system.connections.length;
|
|
186
|
+
const totalStrength = system.connections.reduce((sum, c) => sum + c.strength, 0);
|
|
187
|
+
const avgStrength = totalConnections > 0 ? totalStrength / totalConnections : 0;
|
|
188
|
+
// Use entropy as proxy for information
|
|
189
|
+
const totalEntropy = system.components.reduce((sum, c) => sum + c.entropy, 0);
|
|
190
|
+
const avgEntropy = system.components.length > 0 ? totalEntropy / system.components.length : 0;
|
|
191
|
+
// Estimate φ from connectivity and entropy
|
|
192
|
+
const connectionDensity = totalConnections / Math.max(1, system.components.length * (system.components.length - 1));
|
|
193
|
+
const estimatedPhi = avgStrength * avgEntropy * connectionDensity;
|
|
194
|
+
// Normalize to 0-1 range
|
|
195
|
+
const normalizedPhi = Math.tanh(estimatedPhi);
|
|
196
|
+
const componentIds = system.components.map((c) => c.id);
|
|
197
|
+
return {
|
|
198
|
+
phi: normalizedPhi,
|
|
199
|
+
mip: this.createTrivialPartition(componentIds),
|
|
200
|
+
intrinsicInfo: totalEntropy,
|
|
201
|
+
integratedInfo: normalizedPhi,
|
|
202
|
+
complexes: [],
|
|
203
|
+
calculationTime: 0,
|
|
204
|
+
approximation: true,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
// ============================================================================
|
|
208
|
+
// Information Measures
|
|
209
|
+
// ============================================================================
|
|
210
|
+
/**
|
|
211
|
+
* Calculate intrinsic information of the system
|
|
212
|
+
* The information generated by the system about itself
|
|
213
|
+
*/
|
|
214
|
+
calculateIntrinsicInformation(system) {
|
|
215
|
+
// Sum of component entropies
|
|
216
|
+
let totalEntropy = 0;
|
|
217
|
+
for (const component of system.components) {
|
|
218
|
+
totalEntropy += component.entropy;
|
|
219
|
+
}
|
|
220
|
+
// Add mutual information from connections
|
|
221
|
+
for (const connection of system.connections) {
|
|
222
|
+
totalEntropy += connection.informationFlow * connection.strength;
|
|
223
|
+
}
|
|
224
|
+
return totalEntropy;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Calculate integrated information for a partition
|
|
228
|
+
* The information lost when the system is "cut" at the partition
|
|
229
|
+
*/
|
|
230
|
+
calculateIntegratedInformation(system, partition) {
|
|
231
|
+
// Calculate information in whole system
|
|
232
|
+
const wholeInfo = this.calculateIntrinsicInformation(system);
|
|
233
|
+
// Calculate information in partitioned system
|
|
234
|
+
let partitionedInfo = 0;
|
|
235
|
+
for (const part of partition.parts) {
|
|
236
|
+
const partSystem = {
|
|
237
|
+
...system,
|
|
238
|
+
components: system.components.filter((c) => part.includes(c.id)),
|
|
239
|
+
connections: system.connections.filter((c) => part.includes(c.from) && part.includes(c.to)),
|
|
240
|
+
stateHash: '',
|
|
241
|
+
timestamp: new Date(),
|
|
242
|
+
};
|
|
243
|
+
partitionedInfo += this.calculateIntrinsicInformation(partSystem);
|
|
244
|
+
}
|
|
245
|
+
// φ is the information lost by partitioning
|
|
246
|
+
return Math.max(0, wholeInfo - partitionedInfo);
|
|
247
|
+
}
|
|
248
|
+
// ============================================================================
|
|
249
|
+
// Partition Generation
|
|
250
|
+
// ============================================================================
|
|
251
|
+
/**
|
|
252
|
+
* Generate all bipartitions (exponential)
|
|
253
|
+
*/
|
|
254
|
+
generateAllPartitions(elements) {
|
|
255
|
+
const partitions = [];
|
|
256
|
+
const n = elements.length;
|
|
257
|
+
// Generate all possible ways to split into two non-empty sets
|
|
258
|
+
// Using binary representation: 1 to 2^(n-1) - 1
|
|
259
|
+
const limit = Math.pow(2, n - 1);
|
|
260
|
+
for (let i = 1; i < limit; i++) {
|
|
261
|
+
const part1 = [];
|
|
262
|
+
const part2 = [];
|
|
263
|
+
for (let j = 0; j < n; j++) {
|
|
264
|
+
if ((i >> j) & 1) {
|
|
265
|
+
part1.push(elements[j]);
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
part2.push(elements[j]);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (part1.length > 0 && part2.length > 0) {
|
|
272
|
+
partitions.push(this.createPartition(elements, [part1, part2]));
|
|
273
|
+
}
|
|
274
|
+
// Limit for approximation
|
|
275
|
+
if (partitions.length >= this.config.maxPartitions) {
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return partitions;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Greedy MIP finding
|
|
283
|
+
* Start with all elements together, greedily split to minimize φ
|
|
284
|
+
*/
|
|
285
|
+
greedyMIP(system, elements) {
|
|
286
|
+
if (elements.length <= 1) {
|
|
287
|
+
return this.createTrivialPartition(elements);
|
|
288
|
+
}
|
|
289
|
+
let bestPartition = this.createTrivialPartition(elements);
|
|
290
|
+
let bestPhi = Infinity;
|
|
291
|
+
// Try moving each element to a separate partition
|
|
292
|
+
for (let i = 0; i < elements.length; i++) {
|
|
293
|
+
const part1 = [elements[i]];
|
|
294
|
+
const part2 = elements.filter((_, j) => j !== i);
|
|
295
|
+
if (part2.length === 0)
|
|
296
|
+
continue;
|
|
297
|
+
const partition = this.createPartition(elements, [part1, part2]);
|
|
298
|
+
const phi = this.calculateIntegratedInformation(system, partition);
|
|
299
|
+
if (phi < bestPhi) {
|
|
300
|
+
bestPhi = phi;
|
|
301
|
+
bestPartition = partition;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
// Try pairwise splits
|
|
305
|
+
for (let i = 0; i < elements.length; i++) {
|
|
306
|
+
for (let j = i + 1; j < elements.length; j++) {
|
|
307
|
+
const part1 = elements.slice(0, j);
|
|
308
|
+
const part2 = elements.slice(j);
|
|
309
|
+
if (part1.length === 0 || part2.length === 0)
|
|
310
|
+
continue;
|
|
311
|
+
const partition = this.createPartition(elements, [part1, part2]);
|
|
312
|
+
const phi = this.calculateIntegratedInformation(system, partition);
|
|
313
|
+
if (phi < bestPhi) {
|
|
314
|
+
bestPhi = phi;
|
|
315
|
+
bestPartition = partition;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return bestPartition;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Generate subsets of a given size range
|
|
323
|
+
*/
|
|
324
|
+
generateSubsets(elements, minSize, maxSize) {
|
|
325
|
+
const subsets = [];
|
|
326
|
+
const generate = (start, current) => {
|
|
327
|
+
if (current.length >= minSize && current.length <= maxSize) {
|
|
328
|
+
subsets.push([...current]);
|
|
329
|
+
}
|
|
330
|
+
if (current.length >= maxSize)
|
|
331
|
+
return;
|
|
332
|
+
for (let i = start; i < elements.length; i++) {
|
|
333
|
+
current.push(elements[i]);
|
|
334
|
+
generate(i + 1, current);
|
|
335
|
+
current.pop();
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
generate(0, []);
|
|
339
|
+
return subsets;
|
|
340
|
+
}
|
|
341
|
+
// ============================================================================
|
|
342
|
+
// Helpers
|
|
343
|
+
// ============================================================================
|
|
344
|
+
createPartition(elements, parts) {
|
|
345
|
+
const severedConnections = [];
|
|
346
|
+
// Find connections that cross the partition
|
|
347
|
+
for (let i = 0; i < parts.length; i++) {
|
|
348
|
+
for (let j = i + 1; j < parts.length; j++) {
|
|
349
|
+
for (const from of parts[i]) {
|
|
350
|
+
for (const to of parts[j]) {
|
|
351
|
+
severedConnections.push({ from, to });
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return {
|
|
357
|
+
id: (0, crypto_1.randomUUID)(),
|
|
358
|
+
parts,
|
|
359
|
+
cut: {
|
|
360
|
+
severedConnections,
|
|
361
|
+
informationLoss: 0, // Calculated later
|
|
362
|
+
},
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
createTrivialPartition(elements) {
|
|
366
|
+
return {
|
|
367
|
+
id: (0, crypto_1.randomUUID)(),
|
|
368
|
+
parts: [elements],
|
|
369
|
+
cut: {
|
|
370
|
+
severedConnections: [],
|
|
371
|
+
informationLoss: 0,
|
|
372
|
+
},
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
// ============================================================================
|
|
376
|
+
// Utility Methods
|
|
377
|
+
// ============================================================================
|
|
378
|
+
/**
|
|
379
|
+
* Create a system state from component data
|
|
380
|
+
*/
|
|
381
|
+
static createSystemState(components, connections) {
|
|
382
|
+
const now = new Date();
|
|
383
|
+
const componentStates = components.map((c) => ({
|
|
384
|
+
...c,
|
|
385
|
+
entropy: PhiCalculator.estimateEntropy(c.state),
|
|
386
|
+
lastUpdate: now,
|
|
387
|
+
}));
|
|
388
|
+
const connectionStates = connections.map((c) => ({
|
|
389
|
+
...c,
|
|
390
|
+
informationFlow: c.strength, // Simplified
|
|
391
|
+
bidirectional: false,
|
|
392
|
+
}));
|
|
393
|
+
// Create state hash
|
|
394
|
+
const stateHash = PhiCalculator.hashState(componentStates);
|
|
395
|
+
return {
|
|
396
|
+
components: componentStates,
|
|
397
|
+
connections: connectionStates,
|
|
398
|
+
stateHash,
|
|
399
|
+
timestamp: now,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Estimate entropy of a state object
|
|
404
|
+
*/
|
|
405
|
+
static estimateEntropy(state) {
|
|
406
|
+
// Simplified entropy estimation based on state complexity
|
|
407
|
+
const stateString = JSON.stringify(state);
|
|
408
|
+
const uniqueChars = new Set(stateString).size;
|
|
409
|
+
const length = stateString.length;
|
|
410
|
+
// Shannon entropy approximation
|
|
411
|
+
if (length === 0)
|
|
412
|
+
return 0;
|
|
413
|
+
return Math.log2(uniqueChars) * (length / 100);
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Create a hash of the system state
|
|
417
|
+
*/
|
|
418
|
+
static hashState(components) {
|
|
419
|
+
const stateString = components
|
|
420
|
+
.map((c) => `${c.id}:${c.active}:${JSON.stringify(c.state)}`)
|
|
421
|
+
.sort()
|
|
422
|
+
.join('|');
|
|
423
|
+
// Simple hash function
|
|
424
|
+
let hash = 0;
|
|
425
|
+
for (let i = 0; i < stateString.length; i++) {
|
|
426
|
+
const char = stateString.charCodeAt(i);
|
|
427
|
+
hash = ((hash << 5) - hash) + char;
|
|
428
|
+
hash = hash & hash;
|
|
429
|
+
}
|
|
430
|
+
return hash.toString(16);
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Clear the calculation cache
|
|
434
|
+
*/
|
|
435
|
+
clearCache() {
|
|
436
|
+
this.cache.clear();
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
exports.PhiCalculator = PhiCalculator;
|
|
440
|
+
// ============================================================================
|
|
441
|
+
// Factory
|
|
442
|
+
// ============================================================================
|
|
443
|
+
function createPhiCalculator(config) {
|
|
444
|
+
return new PhiCalculator(config);
|
|
445
|
+
}
|