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,69 @@
1
+ /**
2
+ * Genesis 4.0 - Memory Agent
3
+ *
4
+ * Manages memory with Ebbinghaus forgetting curve.
5
+ * Stores, retrieves, consolidates, and forgets.
6
+ *
7
+ * Key formula: R(t) = e^(-t/S)
8
+ * where R = retention, t = time, S = stability
9
+ */
10
+ import { BaseAgent } from './base-agent.js';
11
+ import { MessageBus } from './message-bus.js';
12
+ import { Message, MessageType, MemoryItem, MemoryQuery } from './types.js';
13
+ export declare class MemoryAgent extends BaseAgent {
14
+ private workingMemory;
15
+ private shortTermMemory;
16
+ private longTermMemory;
17
+ private associations;
18
+ private consolidationInterval;
19
+ private lastConsolidation;
20
+ constructor(bus?: MessageBus);
21
+ protected getMessageTypes(): MessageType[];
22
+ process(message: Message): Promise<Message | null>;
23
+ private handleStore;
24
+ store(content: any, options?: {
25
+ type?: 'episodic' | 'semantic' | 'procedural';
26
+ importance?: number;
27
+ emotionalValence?: number;
28
+ associations?: string[];
29
+ }): MemoryItem;
30
+ private addToWorkingMemory;
31
+ private handleRetrieve;
32
+ retrieve(query: MemoryQuery): MemoryItem[];
33
+ private matchesQuery;
34
+ private accessMemory;
35
+ private handleQuery;
36
+ semanticSearch(query: string, limit?: number): MemoryItem[];
37
+ /**
38
+ * Calculate current retention using Ebbinghaus formula
39
+ * R(t) = e^(-t/S)
40
+ */
41
+ getRetention(memory: MemoryItem): number;
42
+ private startConsolidationCycle;
43
+ /**
44
+ * Consolidation process (runs during "sleep")
45
+ * 1. Scan working memory
46
+ * 2. Evaluate each memory
47
+ * 3. Decide: consolidate, keep, or forget
48
+ * 4. Compress similar memories
49
+ * 5. Prune weak memories
50
+ */
51
+ consolidate(): void;
52
+ private calculateConsolidationScore;
53
+ private updateAssociations;
54
+ private removeAssociations;
55
+ getAssociations(memoryId: string): string[];
56
+ private handleCommand;
57
+ getStats(): {
58
+ workingMemory: number;
59
+ shortTermMemory: number;
60
+ longTermMemory: number;
61
+ totalMemories: number;
62
+ associations: number;
63
+ lastConsolidation: Date;
64
+ };
65
+ getTotalMemoryCount(): number;
66
+ clear(): void;
67
+ shutdown(): void;
68
+ }
69
+ export declare function createMemoryAgent(bus?: MessageBus): MemoryAgent;
@@ -0,0 +1,404 @@
1
+ "use strict";
2
+ /**
3
+ * Genesis 4.0 - Memory Agent
4
+ *
5
+ * Manages memory with Ebbinghaus forgetting curve.
6
+ * Stores, retrieves, consolidates, and forgets.
7
+ *
8
+ * Key formula: R(t) = e^(-t/S)
9
+ * where R = retention, t = time, S = stability
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MemoryAgent = void 0;
13
+ exports.createMemoryAgent = createMemoryAgent;
14
+ const base_agent_js_1 = require("./base-agent.js");
15
+ const message_bus_js_1 = require("./message-bus.js");
16
+ const crypto_1 = require("crypto");
17
+ // ============================================================================
18
+ // Constants
19
+ // ============================================================================
20
+ const MEMORY_THRESHOLDS = {
21
+ CONSOLIDATE: 0.7, // Min score for long-term
22
+ KEEP_SHORT: 0.3, // Min score for short-term
23
+ FORGET: 0.01, // Below this = forgotten
24
+ SIMILARITY_MERGE: 0.85, // Threshold for merging similar memories
25
+ };
26
+ const MEMORY_LIMITS = {
27
+ WORKING: 9, // 7±2 rule
28
+ SHORT_TERM: 100,
29
+ LONG_TERM: 10000,
30
+ };
31
+ const STABILITY_MULTIPLIER = {
32
+ SUCCESS: 2.5, // Multiply stability on successful recall
33
+ IMPORTANCE_BOOST: 1.5, // Additional boost for important memories
34
+ EMOTIONAL_BOOST: 2.0, // Additional boost for emotional memories
35
+ };
36
+ // ============================================================================
37
+ // Memory Agent
38
+ // ============================================================================
39
+ class MemoryAgent extends base_agent_js_1.BaseAgent {
40
+ // Memory stores
41
+ workingMemory = [];
42
+ shortTermMemory = new Map();
43
+ longTermMemory = new Map();
44
+ // Associations graph
45
+ associations = new Map();
46
+ // Consolidation timer
47
+ consolidationInterval = null;
48
+ lastConsolidation = new Date();
49
+ constructor(bus = message_bus_js_1.messageBus) {
50
+ super({ type: 'memory' }, bus);
51
+ this.startConsolidationCycle();
52
+ }
53
+ // ============================================================================
54
+ // Message Handling
55
+ // ============================================================================
56
+ getMessageTypes() {
57
+ return ['MEMORY_STORE', 'MEMORY_RETRIEVE', 'QUERY', 'COMMAND'];
58
+ }
59
+ async process(message) {
60
+ switch (message.type) {
61
+ case 'MEMORY_STORE':
62
+ return this.handleStore(message);
63
+ case 'MEMORY_RETRIEVE':
64
+ return this.handleRetrieve(message);
65
+ case 'QUERY':
66
+ return this.handleQuery(message);
67
+ case 'COMMAND':
68
+ return this.handleCommand(message);
69
+ default:
70
+ return null;
71
+ }
72
+ }
73
+ // ============================================================================
74
+ // Store
75
+ // ============================================================================
76
+ async handleStore(message) {
77
+ const { content, type, importance, emotionalValence, associations } = message.payload;
78
+ const memory = this.store(content, {
79
+ type: type || 'episodic',
80
+ importance: importance || 0.5,
81
+ emotionalValence: emotionalValence || 0,
82
+ associations: associations || [],
83
+ });
84
+ this.log(`Stored memory: ${memory.id.slice(0, 8)} (importance: ${memory.importance})`);
85
+ return {
86
+ ...this.createResponse(message, 'RESPONSE', { memoryId: memory.id, success: true }),
87
+ id: '',
88
+ timestamp: new Date(),
89
+ };
90
+ }
91
+ store(content, options = {}) {
92
+ const now = new Date();
93
+ // Calculate initial strength based on importance and emotion
94
+ const baseStrength = 1.0;
95
+ const importanceBoost = (options.importance || 0.5) * 0.5;
96
+ const emotionalBoost = Math.abs(options.emotionalValence || 0) * 0.3;
97
+ const memory = {
98
+ id: (0, crypto_1.randomUUID)(),
99
+ type: options.type || 'episodic',
100
+ content,
101
+ created: now,
102
+ lastAccessed: now,
103
+ accessCount: 1,
104
+ R0: baseStrength + importanceBoost + emotionalBoost,
105
+ S: 1.0, // Initial stability = 1 day
106
+ importance: options.importance || 0.5,
107
+ emotionalValence: options.emotionalValence || 0,
108
+ associations: options.associations || [],
109
+ consolidated: false,
110
+ };
111
+ // Add to working memory
112
+ this.addToWorkingMemory(memory);
113
+ // Update associations
114
+ this.updateAssociations(memory);
115
+ return memory;
116
+ }
117
+ addToWorkingMemory(memory) {
118
+ this.workingMemory.push(memory);
119
+ // Enforce capacity limit
120
+ while (this.workingMemory.length > MEMORY_LIMITS.WORKING) {
121
+ const evicted = this.workingMemory.shift();
122
+ // Move to short-term
123
+ this.shortTermMemory.set(evicted.id, evicted);
124
+ }
125
+ }
126
+ // ============================================================================
127
+ // Retrieve
128
+ // ============================================================================
129
+ async handleRetrieve(message) {
130
+ const query = message.payload;
131
+ const memories = this.retrieve(query);
132
+ return {
133
+ ...this.createResponse(message, 'RESPONSE', { memories, count: memories.length }),
134
+ id: '',
135
+ timestamp: new Date(),
136
+ };
137
+ }
138
+ retrieve(query) {
139
+ const results = [];
140
+ // Search all memory stores
141
+ const allMemories = [
142
+ ...this.workingMemory,
143
+ ...this.shortTermMemory.values(),
144
+ ...this.longTermMemory.values(),
145
+ ];
146
+ for (const memory of allMemories) {
147
+ if (this.matchesQuery(memory, query)) {
148
+ // Update retention on access
149
+ this.accessMemory(memory);
150
+ results.push(memory);
151
+ }
152
+ }
153
+ // Sort by current strength
154
+ results.sort((a, b) => this.getRetention(b) - this.getRetention(a));
155
+ // Apply limit
156
+ if (query.limit) {
157
+ return results.slice(0, query.limit);
158
+ }
159
+ return results;
160
+ }
161
+ matchesQuery(memory, query) {
162
+ // Type filter
163
+ if (query.type && memory.type !== query.type)
164
+ return false;
165
+ // Importance filter
166
+ if (query.minImportance && memory.importance < query.minImportance)
167
+ return false;
168
+ // Time range filter
169
+ if (query.timeRange) {
170
+ if (memory.created < query.timeRange.start)
171
+ return false;
172
+ if (memory.created > query.timeRange.end)
173
+ return false;
174
+ }
175
+ // Keyword filter
176
+ if (query.keywords && query.keywords.length > 0) {
177
+ const contentStr = JSON.stringify(memory.content).toLowerCase();
178
+ const hasKeyword = query.keywords.some((k) => contentStr.includes(k.toLowerCase()));
179
+ if (!hasKeyword)
180
+ return false;
181
+ }
182
+ // Check if memory is still "alive" (retention > FORGET threshold)
183
+ if (this.getRetention(memory) < MEMORY_THRESHOLDS.FORGET) {
184
+ return false;
185
+ }
186
+ return true;
187
+ }
188
+ accessMemory(memory) {
189
+ memory.lastAccessed = new Date();
190
+ memory.accessCount++;
191
+ // Strengthen stability on successful recall (FSRS-style)
192
+ memory.S *= STABILITY_MULTIPLIER.SUCCESS;
193
+ // Bonus for important memories
194
+ if (memory.importance > 0.7) {
195
+ memory.S *= STABILITY_MULTIPLIER.IMPORTANCE_BOOST;
196
+ }
197
+ // Bonus for emotional memories
198
+ if (Math.abs(memory.emotionalValence) > 0.5) {
199
+ memory.S *= STABILITY_MULTIPLIER.EMOTIONAL_BOOST;
200
+ }
201
+ }
202
+ // ============================================================================
203
+ // Query (semantic search)
204
+ // ============================================================================
205
+ async handleQuery(message) {
206
+ const { query, limit } = message.payload;
207
+ const memories = this.semanticSearch(query, limit || 10);
208
+ return {
209
+ ...this.createResponse(message, 'RESPONSE', { memories, count: memories.length }),
210
+ id: '',
211
+ timestamp: new Date(),
212
+ };
213
+ }
214
+ semanticSearch(query, limit = 10) {
215
+ // Simple keyword-based search (would use embeddings in production)
216
+ const keywords = query.toLowerCase().split(/\s+/);
217
+ return this.retrieve({
218
+ keywords,
219
+ limit,
220
+ });
221
+ }
222
+ // ============================================================================
223
+ // Ebbinghaus Forgetting Curve
224
+ // ============================================================================
225
+ /**
226
+ * Calculate current retention using Ebbinghaus formula
227
+ * R(t) = e^(-t/S)
228
+ */
229
+ getRetention(memory) {
230
+ const now = Date.now();
231
+ const lastAccess = memory.lastAccessed.getTime();
232
+ const elapsedDays = (now - lastAccess) / (1000 * 60 * 60 * 24);
233
+ // R = R0 * e^(-t/S)
234
+ const retention = memory.R0 * Math.exp(-elapsedDays / memory.S);
235
+ return Math.max(0, Math.min(1, retention));
236
+ }
237
+ // ============================================================================
238
+ // Consolidation Cycle
239
+ // ============================================================================
240
+ startConsolidationCycle() {
241
+ // Run consolidation every 10 minutes (simulated "sleep")
242
+ this.consolidationInterval = setInterval(() => {
243
+ this.consolidate();
244
+ }, 10 * 60 * 1000);
245
+ }
246
+ /**
247
+ * Consolidation process (runs during "sleep")
248
+ * 1. Scan working memory
249
+ * 2. Evaluate each memory
250
+ * 3. Decide: consolidate, keep, or forget
251
+ * 4. Compress similar memories
252
+ * 5. Prune weak memories
253
+ */
254
+ consolidate() {
255
+ this.log('Starting consolidation cycle...');
256
+ let consolidated = 0;
257
+ let forgotten = 0;
258
+ let kept = 0;
259
+ // Process short-term memories
260
+ for (const [id, memory] of this.shortTermMemory) {
261
+ const score = this.calculateConsolidationScore(memory);
262
+ if (score >= MEMORY_THRESHOLDS.CONSOLIDATE) {
263
+ // Move to long-term
264
+ memory.consolidated = true;
265
+ this.longTermMemory.set(id, memory);
266
+ this.shortTermMemory.delete(id);
267
+ consolidated++;
268
+ }
269
+ else if (score < MEMORY_THRESHOLDS.KEEP_SHORT) {
270
+ // Forget
271
+ this.shortTermMemory.delete(id);
272
+ this.removeAssociations(memory);
273
+ forgotten++;
274
+ }
275
+ else {
276
+ kept++;
277
+ }
278
+ }
279
+ // Prune weak long-term memories
280
+ for (const [id, memory] of this.longTermMemory) {
281
+ const retention = this.getRetention(memory);
282
+ if (retention < MEMORY_THRESHOLDS.FORGET) {
283
+ this.longTermMemory.delete(id);
284
+ this.removeAssociations(memory);
285
+ forgotten++;
286
+ }
287
+ }
288
+ this.lastConsolidation = new Date();
289
+ this.log(`Consolidation complete: ${consolidated} consolidated, ${forgotten} forgotten, ${kept} kept`);
290
+ // Broadcast consolidation event
291
+ this.broadcast('MEMORY_STORE', {
292
+ event: 'consolidation',
293
+ consolidated,
294
+ forgotten,
295
+ kept,
296
+ totalMemories: this.getTotalMemoryCount(),
297
+ });
298
+ }
299
+ calculateConsolidationScore(memory) {
300
+ const retention = this.getRetention(memory);
301
+ const associationCount = this.associations.get(memory.id)?.size || 0;
302
+ const recency = 1 / (1 + (Date.now() - memory.lastAccessed.getTime()) / (1000 * 60 * 60 * 24));
303
+ // Score = importance * (1 + associations) * retention * recency
304
+ const score = memory.importance * (1 + associationCount * 0.1) * retention * recency;
305
+ return Math.min(1, score);
306
+ }
307
+ // ============================================================================
308
+ // Associations
309
+ // ============================================================================
310
+ updateAssociations(memory) {
311
+ if (!this.associations.has(memory.id)) {
312
+ this.associations.set(memory.id, new Set());
313
+ }
314
+ for (const assocId of memory.associations) {
315
+ // Bidirectional association
316
+ this.associations.get(memory.id).add(assocId);
317
+ if (!this.associations.has(assocId)) {
318
+ this.associations.set(assocId, new Set());
319
+ }
320
+ this.associations.get(assocId).add(memory.id);
321
+ }
322
+ }
323
+ removeAssociations(memory) {
324
+ const assocs = this.associations.get(memory.id);
325
+ if (assocs) {
326
+ for (const assocId of assocs) {
327
+ this.associations.get(assocId)?.delete(memory.id);
328
+ }
329
+ this.associations.delete(memory.id);
330
+ }
331
+ }
332
+ getAssociations(memoryId) {
333
+ return Array.from(this.associations.get(memoryId) || []);
334
+ }
335
+ // ============================================================================
336
+ // Commands
337
+ // ============================================================================
338
+ async handleCommand(message) {
339
+ const { command, params } = message.payload;
340
+ switch (command) {
341
+ case 'consolidate':
342
+ this.consolidate();
343
+ return {
344
+ ...this.createResponse(message, 'RESPONSE', { success: true }),
345
+ id: '',
346
+ timestamp: new Date(),
347
+ };
348
+ case 'stats':
349
+ return {
350
+ ...this.createResponse(message, 'RESPONSE', this.getStats()),
351
+ id: '',
352
+ timestamp: new Date(),
353
+ };
354
+ case 'clear':
355
+ this.clear();
356
+ return {
357
+ ...this.createResponse(message, 'RESPONSE', { success: true }),
358
+ id: '',
359
+ timestamp: new Date(),
360
+ };
361
+ default:
362
+ return null;
363
+ }
364
+ }
365
+ // ============================================================================
366
+ // Stats
367
+ // ============================================================================
368
+ getStats() {
369
+ return {
370
+ workingMemory: this.workingMemory.length,
371
+ shortTermMemory: this.shortTermMemory.size,
372
+ longTermMemory: this.longTermMemory.size,
373
+ totalMemories: this.getTotalMemoryCount(),
374
+ associations: this.associations.size,
375
+ lastConsolidation: this.lastConsolidation,
376
+ };
377
+ }
378
+ getTotalMemoryCount() {
379
+ return this.workingMemory.length + this.shortTermMemory.size + this.longTermMemory.size;
380
+ }
381
+ // ============================================================================
382
+ // Cleanup
383
+ // ============================================================================
384
+ clear() {
385
+ this.workingMemory = [];
386
+ this.shortTermMemory.clear();
387
+ this.longTermMemory.clear();
388
+ this.associations.clear();
389
+ }
390
+ shutdown() {
391
+ if (this.consolidationInterval) {
392
+ clearInterval(this.consolidationInterval);
393
+ }
394
+ super.shutdown();
395
+ }
396
+ }
397
+ exports.MemoryAgent = MemoryAgent;
398
+ // ============================================================================
399
+ // Register Factory
400
+ // ============================================================================
401
+ (0, base_agent_js_1.registerAgentFactory)('memory', (bus) => new MemoryAgent(bus));
402
+ function createMemoryAgent(bus) {
403
+ return new MemoryAgent(bus);
404
+ }
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Genesis 4.0 - Message Bus
3
+ *
4
+ * Pub/Sub message bus for agent communication.
5
+ * Based on EventEmitter pattern with typed messages.
6
+ */
7
+ import { Message, MessageType, MessagePriority, AgentId } from './types.js';
8
+ export type MessageHandler = (message: Message) => void | Promise<void>;
9
+ export interface Subscription {
10
+ id: string;
11
+ agentId: AgentId;
12
+ filter?: MessageFilter;
13
+ handler: MessageHandler;
14
+ }
15
+ export interface MessageFilter {
16
+ types?: MessageType[];
17
+ from?: AgentId[];
18
+ priority?: MessagePriority[];
19
+ }
20
+ export declare class MessageBus {
21
+ private subscriptions;
22
+ private messageQueue;
23
+ private processing;
24
+ private messageHistory;
25
+ private maxHistorySize;
26
+ private metrics;
27
+ /**
28
+ * Subscribe to messages
29
+ */
30
+ subscribe(agentId: AgentId, handler: MessageHandler, filter?: MessageFilter): string;
31
+ /**
32
+ * Unsubscribe from messages
33
+ */
34
+ unsubscribe(subscriptionId: string): boolean;
35
+ /**
36
+ * Publish a message
37
+ */
38
+ publish(message: Omit<Message, 'id' | 'timestamp'>): Promise<string>;
39
+ /**
40
+ * Create and publish a message (helper)
41
+ */
42
+ send(from: AgentId, to: AgentId | 'broadcast' | 'kernel', type: MessageType, payload: any, options?: {
43
+ priority?: MessagePriority;
44
+ replyTo?: string;
45
+ correlationId?: string;
46
+ }): Promise<string>;
47
+ /**
48
+ * Broadcast to all agents
49
+ */
50
+ broadcast(from: AgentId, type: MessageType, payload: any, priority?: MessagePriority): Promise<string>;
51
+ private processQueue;
52
+ private deliverMessage;
53
+ private findSubscriptions;
54
+ private addToHistory;
55
+ getHistory(filter?: {
56
+ from?: AgentId;
57
+ to?: AgentId;
58
+ type?: MessageType;
59
+ limit?: number;
60
+ }): Message[];
61
+ /**
62
+ * Wait for a specific message (useful for request-response)
63
+ */
64
+ waitForMessage(filter: {
65
+ replyTo?: string;
66
+ correlationId?: string;
67
+ type?: MessageType;
68
+ from?: AgentId;
69
+ to?: AgentId;
70
+ }, timeout?: number): Promise<Message>;
71
+ /**
72
+ * Request-response pattern
73
+ */
74
+ request(from: AgentId, to: AgentId, type: MessageType, payload: any, timeout?: number): Promise<Message>;
75
+ getMetrics(): {
76
+ uptime: number;
77
+ activeSubscriptions: number;
78
+ queueLength: number;
79
+ historySize: number;
80
+ messagesSent: number;
81
+ messagesDelivered: number;
82
+ messagesDropped: number;
83
+ startTime: Date;
84
+ };
85
+ clear(): void;
86
+ }
87
+ export declare const messageBus: MessageBus;
88
+ export declare function createMessage(from: AgentId, to: AgentId | 'broadcast' | 'kernel', type: MessageType, payload: any, options?: Partial<Message>): Message;