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.
Files changed (227) hide show
  1. package/.env.example +78 -0
  2. package/README.md +282 -0
  3. package/dist/src/active-inference/actions.d.ts +75 -0
  4. package/dist/src/active-inference/actions.js +250 -0
  5. package/dist/src/active-inference/autonomous-loop.d.ts +103 -0
  6. package/dist/src/active-inference/autonomous-loop.js +289 -0
  7. package/dist/src/active-inference/core.d.ts +85 -0
  8. package/dist/src/active-inference/core.js +555 -0
  9. package/dist/src/active-inference/demo-autonomous-loop.d.ts +8 -0
  10. package/dist/src/active-inference/demo-autonomous-loop.js +338 -0
  11. package/dist/src/active-inference/demo-value-integration.d.ts +8 -0
  12. package/dist/src/active-inference/demo-value-integration.js +174 -0
  13. package/dist/src/active-inference/index.d.ts +32 -0
  14. package/dist/src/active-inference/index.js +88 -0
  15. package/dist/src/active-inference/integration.d.ts +114 -0
  16. package/dist/src/active-inference/integration.js +698 -0
  17. package/dist/src/active-inference/memory-integration.d.ts +51 -0
  18. package/dist/src/active-inference/memory-integration.js +232 -0
  19. package/dist/src/active-inference/observations.d.ts +67 -0
  20. package/dist/src/active-inference/observations.js +147 -0
  21. package/dist/src/active-inference/test-active-inference.d.ts +8 -0
  22. package/dist/src/active-inference/test-active-inference.js +320 -0
  23. package/dist/src/active-inference/test-value-integration.d.ts +6 -0
  24. package/dist/src/active-inference/test-value-integration.js +168 -0
  25. package/dist/src/active-inference/types.d.ts +150 -0
  26. package/dist/src/active-inference/types.js +59 -0
  27. package/dist/src/active-inference/value-integration.d.ts +164 -0
  28. package/dist/src/active-inference/value-integration.js +459 -0
  29. package/dist/src/agents/base-agent.d.ts +53 -0
  30. package/dist/src/agents/base-agent.js +178 -0
  31. package/dist/src/agents/builder.d.ts +67 -0
  32. package/dist/src/agents/builder.js +537 -0
  33. package/dist/src/agents/critic.d.ts +35 -0
  34. package/dist/src/agents/critic.js +322 -0
  35. package/dist/src/agents/ethicist.d.ts +54 -0
  36. package/dist/src/agents/ethicist.js +393 -0
  37. package/dist/src/agents/explorer.d.ts +26 -0
  38. package/dist/src/agents/explorer.js +216 -0
  39. package/dist/src/agents/feeling.d.ts +41 -0
  40. package/dist/src/agents/feeling.js +320 -0
  41. package/dist/src/agents/index.d.ts +111 -0
  42. package/dist/src/agents/index.js +222 -0
  43. package/dist/src/agents/memory.d.ts +69 -0
  44. package/dist/src/agents/memory.js +404 -0
  45. package/dist/src/agents/message-bus.d.ts +88 -0
  46. package/dist/src/agents/message-bus.js +267 -0
  47. package/dist/src/agents/narrator.d.ts +90 -0
  48. package/dist/src/agents/narrator.js +473 -0
  49. package/dist/src/agents/planner.d.ts +38 -0
  50. package/dist/src/agents/planner.js +341 -0
  51. package/dist/src/agents/predictor.d.ts +73 -0
  52. package/dist/src/agents/predictor.js +506 -0
  53. package/dist/src/agents/sensor.d.ts +88 -0
  54. package/dist/src/agents/sensor.js +377 -0
  55. package/dist/src/agents/test-agents.d.ts +6 -0
  56. package/dist/src/agents/test-agents.js +73 -0
  57. package/dist/src/agents/types.d.ts +194 -0
  58. package/dist/src/agents/types.js +7 -0
  59. package/dist/src/brain/index.d.ts +185 -0
  60. package/dist/src/brain/index.js +843 -0
  61. package/dist/src/brain/trace.d.ts +91 -0
  62. package/dist/src/brain/trace.js +327 -0
  63. package/dist/src/brain/types.d.ts +165 -0
  64. package/dist/src/brain/types.js +51 -0
  65. package/dist/src/cli/chat.d.ts +237 -0
  66. package/dist/src/cli/chat.js +1959 -0
  67. package/dist/src/cli/dispatcher.d.ts +182 -0
  68. package/dist/src/cli/dispatcher.js +718 -0
  69. package/dist/src/cli/human-loop.d.ts +170 -0
  70. package/dist/src/cli/human-loop.js +543 -0
  71. package/dist/src/cli/index.d.ts +12 -0
  72. package/dist/src/cli/index.js +28 -0
  73. package/dist/src/cli/interactive.d.ts +141 -0
  74. package/dist/src/cli/interactive.js +757 -0
  75. package/dist/src/cli/ui.d.ts +205 -0
  76. package/dist/src/cli/ui.js +632 -0
  77. package/dist/src/consciousness/attention-schema.d.ts +154 -0
  78. package/dist/src/consciousness/attention-schema.js +432 -0
  79. package/dist/src/consciousness/global-workspace.d.ts +149 -0
  80. package/dist/src/consciousness/global-workspace.js +422 -0
  81. package/dist/src/consciousness/index.d.ts +186 -0
  82. package/dist/src/consciousness/index.js +476 -0
  83. package/dist/src/consciousness/phi-calculator.d.ts +119 -0
  84. package/dist/src/consciousness/phi-calculator.js +445 -0
  85. package/dist/src/consciousness/phi-decisions.d.ts +169 -0
  86. package/dist/src/consciousness/phi-decisions.js +383 -0
  87. package/dist/src/consciousness/phi-monitor.d.ts +153 -0
  88. package/dist/src/consciousness/phi-monitor.js +465 -0
  89. package/dist/src/consciousness/types.d.ts +260 -0
  90. package/dist/src/consciousness/types.js +44 -0
  91. package/dist/src/daemon/dream-mode.d.ts +115 -0
  92. package/dist/src/daemon/dream-mode.js +470 -0
  93. package/dist/src/daemon/index.d.ts +162 -0
  94. package/dist/src/daemon/index.js +542 -0
  95. package/dist/src/daemon/maintenance.d.ts +139 -0
  96. package/dist/src/daemon/maintenance.js +549 -0
  97. package/dist/src/daemon/process.d.ts +82 -0
  98. package/dist/src/daemon/process.js +442 -0
  99. package/dist/src/daemon/scheduler.d.ts +90 -0
  100. package/dist/src/daemon/scheduler.js +494 -0
  101. package/dist/src/daemon/types.d.ts +213 -0
  102. package/dist/src/daemon/types.js +50 -0
  103. package/dist/src/epistemic/index.d.ts +74 -0
  104. package/dist/src/epistemic/index.js +225 -0
  105. package/dist/src/grounding/epistemic-stack.d.ts +100 -0
  106. package/dist/src/grounding/epistemic-stack.js +408 -0
  107. package/dist/src/grounding/feedback.d.ts +98 -0
  108. package/dist/src/grounding/feedback.js +276 -0
  109. package/dist/src/grounding/index.d.ts +123 -0
  110. package/dist/src/grounding/index.js +224 -0
  111. package/dist/src/grounding/verifier.d.ts +149 -0
  112. package/dist/src/grounding/verifier.js +484 -0
  113. package/dist/src/healing/detector.d.ts +110 -0
  114. package/dist/src/healing/detector.js +436 -0
  115. package/dist/src/healing/fixer.d.ts +138 -0
  116. package/dist/src/healing/fixer.js +572 -0
  117. package/dist/src/healing/index.d.ts +23 -0
  118. package/dist/src/healing/index.js +43 -0
  119. package/dist/src/hooks/index.d.ts +135 -0
  120. package/dist/src/hooks/index.js +317 -0
  121. package/dist/src/index.d.ts +23 -0
  122. package/dist/src/index.js +1266 -0
  123. package/dist/src/kernel/index.d.ts +155 -0
  124. package/dist/src/kernel/index.js +795 -0
  125. package/dist/src/kernel/invariants.d.ts +153 -0
  126. package/dist/src/kernel/invariants.js +355 -0
  127. package/dist/src/kernel/test-kernel.d.ts +6 -0
  128. package/dist/src/kernel/test-kernel.js +108 -0
  129. package/dist/src/kernel/test-real-mcp.d.ts +10 -0
  130. package/dist/src/kernel/test-real-mcp.js +295 -0
  131. package/dist/src/llm/index.d.ts +146 -0
  132. package/dist/src/llm/index.js +428 -0
  133. package/dist/src/llm/router.d.ts +136 -0
  134. package/dist/src/llm/router.js +510 -0
  135. package/dist/src/mcp/index.d.ts +85 -0
  136. package/dist/src/mcp/index.js +657 -0
  137. package/dist/src/mcp/resilient.d.ts +139 -0
  138. package/dist/src/mcp/resilient.js +417 -0
  139. package/dist/src/memory/cache.d.ts +118 -0
  140. package/dist/src/memory/cache.js +356 -0
  141. package/dist/src/memory/cognitive-workspace.d.ts +231 -0
  142. package/dist/src/memory/cognitive-workspace.js +521 -0
  143. package/dist/src/memory/consolidation.d.ts +99 -0
  144. package/dist/src/memory/consolidation.js +443 -0
  145. package/dist/src/memory/episodic.d.ts +114 -0
  146. package/dist/src/memory/episodic.js +394 -0
  147. package/dist/src/memory/forgetting.d.ts +134 -0
  148. package/dist/src/memory/forgetting.js +324 -0
  149. package/dist/src/memory/index.d.ts +211 -0
  150. package/dist/src/memory/index.js +367 -0
  151. package/dist/src/memory/indexer.d.ts +123 -0
  152. package/dist/src/memory/indexer.js +479 -0
  153. package/dist/src/memory/procedural.d.ts +136 -0
  154. package/dist/src/memory/procedural.js +479 -0
  155. package/dist/src/memory/semantic.d.ts +132 -0
  156. package/dist/src/memory/semantic.js +497 -0
  157. package/dist/src/memory/types.d.ts +193 -0
  158. package/dist/src/memory/types.js +15 -0
  159. package/dist/src/orchestrator.d.ts +65 -0
  160. package/dist/src/orchestrator.js +317 -0
  161. package/dist/src/persistence/index.d.ts +257 -0
  162. package/dist/src/persistence/index.js +763 -0
  163. package/dist/src/pipeline/executor.d.ts +51 -0
  164. package/dist/src/pipeline/executor.js +695 -0
  165. package/dist/src/pipeline/index.d.ts +7 -0
  166. package/dist/src/pipeline/index.js +11 -0
  167. package/dist/src/self-production.d.ts +67 -0
  168. package/dist/src/self-production.js +205 -0
  169. package/dist/src/subagents/executor.d.ts +58 -0
  170. package/dist/src/subagents/executor.js +283 -0
  171. package/dist/src/subagents/index.d.ts +37 -0
  172. package/dist/src/subagents/index.js +53 -0
  173. package/dist/src/subagents/registry.d.ts +23 -0
  174. package/dist/src/subagents/registry.js +167 -0
  175. package/dist/src/subagents/types.d.ts +79 -0
  176. package/dist/src/subagents/types.js +14 -0
  177. package/dist/src/tools/bash.d.ts +139 -0
  178. package/dist/src/tools/bash.js +583 -0
  179. package/dist/src/tools/edit.d.ts +125 -0
  180. package/dist/src/tools/edit.js +424 -0
  181. package/dist/src/tools/git.d.ts +179 -0
  182. package/dist/src/tools/git.js +504 -0
  183. package/dist/src/tools/index.d.ts +21 -0
  184. package/dist/src/tools/index.js +163 -0
  185. package/dist/src/types.d.ts +145 -0
  186. package/dist/src/types.js +7 -0
  187. package/dist/src/world-model/decoder.d.ts +163 -0
  188. package/dist/src/world-model/decoder.js +517 -0
  189. package/dist/src/world-model/digital-twin.d.ts +219 -0
  190. package/dist/src/world-model/digital-twin.js +695 -0
  191. package/dist/src/world-model/encoder.d.ts +141 -0
  192. package/dist/src/world-model/encoder.js +564 -0
  193. package/dist/src/world-model/index.d.ts +221 -0
  194. package/dist/src/world-model/index.js +772 -0
  195. package/dist/src/world-model/predictor.d.ts +161 -0
  196. package/dist/src/world-model/predictor.js +681 -0
  197. package/dist/src/world-model/test-value-jepa.d.ts +8 -0
  198. package/dist/src/world-model/test-value-jepa.js +430 -0
  199. package/dist/src/world-model/types.d.ts +341 -0
  200. package/dist/src/world-model/types.js +69 -0
  201. package/dist/src/world-model/value-jepa.d.ts +247 -0
  202. package/dist/src/world-model/value-jepa.js +622 -0
  203. package/dist/test/brain.test.d.ts +11 -0
  204. package/dist/test/brain.test.js +358 -0
  205. package/dist/test/cli/dispatcher.test.d.ts +4 -0
  206. package/dist/test/cli/dispatcher.test.js +332 -0
  207. package/dist/test/cli/human-loop.test.d.ts +4 -0
  208. package/dist/test/cli/human-loop.test.js +270 -0
  209. package/dist/test/grounding/feedback.test.d.ts +4 -0
  210. package/dist/test/grounding/feedback.test.js +462 -0
  211. package/dist/test/grounding/verifier.test.d.ts +4 -0
  212. package/dist/test/grounding/verifier.test.js +442 -0
  213. package/dist/test/grounding.test.d.ts +6 -0
  214. package/dist/test/grounding.test.js +246 -0
  215. package/dist/test/healing/detector.test.d.ts +4 -0
  216. package/dist/test/healing/detector.test.js +266 -0
  217. package/dist/test/healing/fixer.test.d.ts +4 -0
  218. package/dist/test/healing/fixer.test.js +369 -0
  219. package/dist/test/integration.test.d.ts +5 -0
  220. package/dist/test/integration.test.js +290 -0
  221. package/dist/test/tools/bash.test.d.ts +4 -0
  222. package/dist/test/tools/bash.test.js +348 -0
  223. package/dist/test/tools/edit.test.d.ts +4 -0
  224. package/dist/test/tools/edit.test.js +350 -0
  225. package/dist/test/tools/git.test.d.ts +4 -0
  226. package/dist/test/tools/git.test.js +350 -0
  227. 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
+ }