microscope-memory 1.2.0

Pure binary cognitive memory engine with REST API bridge for LLM integration. Zero-JSON core, mmap-based, hierarchical, sub-microsecond search.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Synaptic Plasticity — Long-term potentiation, long-term depression, spike-timing-dependent plasticity
//! Core mechanisms of learning at synaptic level: activity-dependent weight changes

use std::collections::HashMap;
use std::fs;
use std::path::Path;

#[derive(Clone, Debug)]
pub struct Synapse {
    pub pre_block: u32,
    pub post_block: u32,
    pub weight: f32, // 0.0-1.0
    pub last_pre_spike_ms: u64,
    pub last_post_spike_ms: u64,
    pub spike_history: Vec<i64>, // relative timing of spikes (ms)
    pub ltp_count: u32,          // LTP events
    pub ltd_count: u32,          // LTD events
}

pub struct SynapticPlasticity {
    pub synapses: HashMap<(u32, u32), Synapse>,
    pub ltp_threshold_ms: i64, // spike timing window for LTP (+ms)
    pub ltd_threshold_ms: i64, // spike timing window for LTD (-ms)
    pub learning_rate: f32,    // 0.0-1.0
    pub total_updates: u32,
}

impl Default for SynapticPlasticity {
    fn default() -> Self {
        Self::new()
    }
}

impl SynapticPlasticity {
    pub fn new() -> Self {
        Self {
            synapses: HashMap::new(),
            ltp_threshold_ms: 20,  // post before pre: LTP
            ltd_threshold_ms: -20, // pre before post: LTD
            learning_rate: 0.1,
            total_updates: 0,
        }
    }

    /// Long-Term Potentiation: strengthen synapse
    pub fn ltp(&mut self, pre_block: u32, post_block: u32) -> f32 {
        let key = (pre_block, post_block);
        let now = Self::now_ms();

        let synapse = self.synapses.entry(key).or_insert_with(|| Synapse {
            pre_block,
            post_block,
            weight: 0.5,
            last_pre_spike_ms: now,
            last_post_spike_ms: now,
            spike_history: Vec::new(),
            ltp_count: 0,
            ltd_count: 0,
        });

        // Consolidate: weight increase
        let increment = self.learning_rate * (1.0 - synapse.weight);
        synapse.weight = (synapse.weight + increment).min(1.0);
        synapse.ltp_count += 1;
        synapse.last_pre_spike_ms = now;
        self.total_updates += 1;

        synapse.weight
    }

    /// Long-Term Depression: weaken synapse
    pub fn ltd(&mut self, pre_block: u32, post_block: u32) -> f32 {
        let key = (pre_block, post_block);
        let now = Self::now_ms();

        let synapse = self.synapses.entry(key).or_insert_with(|| Synapse {
            pre_block,
            post_block,
            weight: 0.5,
            last_pre_spike_ms: now,
            last_post_spike_ms: now,
            spike_history: Vec::new(),
            ltp_count: 0,
            ltd_count: 0,
        });

        // Weaken: weight decrease
        let decrement = self.learning_rate * synapse.weight;
        synapse.weight = (synapse.weight - decrement).max(0.0);
        synapse.ltd_count += 1;
        synapse.last_post_spike_ms = now;
        self.total_updates += 1;

        synapse.weight
    }

    /// Spike-Timing-Dependent Plasticity (STDP)
    /// Pre spike then post spike (causal): LTP
    /// Post spike then pre spike (acausal): LTD
    pub fn stdp(
        &mut self,
        pre_block: u32,
        post_block: u32,
        pre_time_ms: i64,
        post_time_ms: i64,
    ) -> f32 {
        let key = (pre_block, post_block);
        let timing_diff = post_time_ms - pre_time_ms; // positive = post after pre

        let synapse = self.synapses.entry(key).or_insert_with(|| Synapse {
            pre_block,
            post_block,
            weight: 0.5,
            last_pre_spike_ms: Self::now_ms(),
            last_post_spike_ms: Self::now_ms(),
            spike_history: Vec::new(),
            ltp_count: 0,
            ltd_count: 0,
        });

        synapse.spike_history.push(timing_diff);
        if synapse.spike_history.len() > 50 {
            synapse.spike_history.remove(0);
        }

        // Causal: pre before post → LTP
        if timing_diff > 0 && timing_diff <= self.ltp_threshold_ms {
            let strength = 1.0 - (timing_diff as f32 / self.ltp_threshold_ms as f32);
            let increment = self.learning_rate * strength * (1.0 - synapse.weight);
            synapse.weight = (synapse.weight + increment).min(1.0);
            synapse.ltp_count += 1;
        }
        // Anti-causal: post before pre → LTD
        else if timing_diff < 0 && timing_diff >= self.ltd_threshold_ms {
            let strength = 1.0 - (-timing_diff as f32 / -self.ltd_threshold_ms as f32);
            let decrement = self.learning_rate * strength * synapse.weight;
            synapse.weight = (synapse.weight - decrement).max(0.0);
            synapse.ltd_count += 1;
        }

        self.total_updates += 1;
        synapse.weight
    }

    /// Get synapse weight
    pub fn get_weight(&self, pre_block: u32, post_block: u32) -> f32 {
        self.synapses
            .get(&(pre_block, post_block))
            .map(|s| s.weight)
            .unwrap_or(0.0)
    }

    /// Get strongest synapses
    pub fn strongest_synapses(&self, k: usize) -> Vec<(&(u32, u32), &Synapse)> {
        let mut synapses: Vec<_> = self.synapses.iter().collect();
        synapses.sort_by(|a, b| b.1.weight.total_cmp(&a.1.weight));
        synapses.into_iter().take(k).collect()
    }

    /// Get synapses by plasticity type
    pub fn ltp_dominant(&self) -> Vec<&Synapse> {
        self.synapses
            .values()
            .filter(|s| s.ltp_count > s.ltd_count)
            .collect()
    }

    pub fn ltd_dominant(&self) -> Vec<&Synapse> {
        self.synapses
            .values()
            .filter(|s| s.ltd_count > s.ltp_count)
            .collect()
    }

    /// Calculate STDP curve value
    pub fn stdp_curve(&self, timing_diff_ms: i64) -> f32 {
        if timing_diff_ms > 0 && timing_diff_ms <= self.ltp_threshold_ms {
            // LTP: exponential decay
            (1.0 - (timing_diff_ms as f32 / self.ltp_threshold_ms as f32).abs()).max(0.0)
        } else if timing_diff_ms < 0 && timing_diff_ms >= self.ltd_threshold_ms {
            // LTD: negative decay
            -((timing_diff_ms as f32 / self.ltd_threshold_ms as f32).abs()).min(1.0)
        } else {
            0.0
        }
    }

    /// Get statistics
    pub fn stats(&self) -> (u32, u32, u32, f32, f32) {
        let total_synapses = self.synapses.len() as u32;
        let total_ltp: u32 = self.synapses.values().map(|s| s.ltp_count).sum();
        let total_ltd: u32 = self.synapses.values().map(|s| s.ltd_count).sum();

        let avg_weight = if !self.synapses.is_empty() {
            self.synapses.values().map(|s| s.weight).sum::<f32>() / self.synapses.len() as f32
        } else {
            0.0
        };

        let ltp_ratio = if total_ltp + total_ltd > 0 {
            total_ltp as f32 / (total_ltp + total_ltd) as f32
        } else {
            0.0
        };

        (total_synapses, total_ltp, total_ltd, avg_weight, ltp_ratio)
    }

    pub fn save(&self, dir: &Path) -> Result<(), String> {
        let path = dir.join("synaptic_plasticity.bin");
        let mut data = Vec::new();

        data.extend_from_slice(b"SYPLS");
        data.push(1);

        let synapse_count = self.synapses.len() as u32;
        data.extend_from_slice(&synapse_count.to_le_bytes());

        for ((pre, post), synapse) in &self.synapses {
            data.extend_from_slice(&pre.to_le_bytes());
            data.extend_from_slice(&post.to_le_bytes());
            data.extend_from_slice(&synapse.weight.to_le_bytes());
            data.extend_from_slice(&synapse.last_pre_spike_ms.to_le_bytes());
            data.extend_from_slice(&synapse.last_post_spike_ms.to_le_bytes());
            data.extend_from_slice(&synapse.ltp_count.to_le_bytes());
            data.extend_from_slice(&synapse.ltd_count.to_le_bytes());

            let hist_count = synapse.spike_history.len() as u16;
            data.extend_from_slice(&hist_count.to_le_bytes());
            for &timing in &synapse.spike_history {
                data.extend_from_slice(&timing.to_le_bytes());
            }
        }

        let tmp_path = dir.join("synaptic_plasticity.bin.tmp");
        fs::write(&tmp_path, data).map_err(|e| e.to_string())?;
        fs::rename(&tmp_path, &path).map_err(|e| e.to_string())
    }

    pub fn load(dir: &Path) -> Result<Self, String> {
        let path = dir.join("synaptic_plasticity.bin");
        if !path.exists() {
            return Ok(Self::new());
        }

        let data = fs::read(&path).map_err(|e| e.to_string())?;
        if data.len() < 5 || &data[0..5] != b"SYPLS" {
            return Ok(Self::new());
        }

        let mut idx = 6;
        let mut synapses = HashMap::new();

        if idx + 4 <= data.len() {
            let synapse_count =
                u32::from_le_bytes([data[idx], data[idx + 1], data[idx + 2], data[idx + 3]])
                    as usize;
            idx += 4;

            for _ in 0..synapse_count {
                if idx + 40 > data.len() {
                    break;
                }

                let pre =
                    u32::from_le_bytes([data[idx], data[idx + 1], data[idx + 2], data[idx + 3]]);
                idx += 4;
                let post =
                    u32::from_le_bytes([data[idx], data[idx + 1], data[idx + 2], data[idx + 3]]);
                idx += 4;
                let weight =
                    f32::from_le_bytes([data[idx], data[idx + 1], data[idx + 2], data[idx + 3]]);
                idx += 4;
                let last_pre = u64::from_le_bytes([
                    data[idx],
                    data[idx + 1],
                    data[idx + 2],
                    data[idx + 3],
                    data[idx + 4],
                    data[idx + 5],
                    data[idx + 6],
                    data[idx + 7],
                ]);
                idx += 8;
                let last_post = u64::from_le_bytes([
                    data[idx],
                    data[idx + 1],
                    data[idx + 2],
                    data[idx + 3],
                    data[idx + 4],
                    data[idx + 5],
                    data[idx + 6],
                    data[idx + 7],
                ]);
                idx += 8;
                let ltp_count =
                    u32::from_le_bytes([data[idx], data[idx + 1], data[idx + 2], data[idx + 3]]);
                idx += 4;
                let ltd_count =
                    u32::from_le_bytes([data[idx], data[idx + 1], data[idx + 2], data[idx + 3]]);
                idx += 4;

                let hist_count = u16::from_le_bytes([data[idx], data[idx + 1]]) as usize;
                idx += 2;

                let mut spike_history = Vec::new();
                for _ in 0..hist_count {
                    if idx + 8 > data.len() {
                        break;
                    }
                    let timing = i64::from_le_bytes([
                        data[idx],
                        data[idx + 1],
                        data[idx + 2],
                        data[idx + 3],
                        data[idx + 4],
                        data[idx + 5],
                        data[idx + 6],
                        data[idx + 7],
                    ]);
                    spike_history.push(timing);
                    idx += 8;
                }

                synapses.insert(
                    (pre, post),
                    Synapse {
                        pre_block: pre,
                        post_block: post,
                        weight,
                        last_pre_spike_ms: last_pre,
                        last_post_spike_ms: last_post,
                        spike_history,
                        ltp_count,
                        ltd_count,
                    },
                );
            }
        }

        Ok(Self {
            synapses,
            ltp_threshold_ms: 20,
            ltd_threshold_ms: -20,
            learning_rate: 0.1,
            total_updates: 0,
        })
    }

    pub fn load_or_init(dir: &Path) -> Self {
        Self::load(dir).unwrap_or_else(|_| Self::new())
    }

    /// Heterosynaptic Depression — weaken other synapses when one is active
    pub fn heterosynaptic_depression(
        &mut self,
        active_synapse: (u32, u32),
        neighborhood_radius: u32,
    ) {
        if let Some(active) = self.synapses.get(&active_synapse) {
            let pre = active.pre_block;
            let depression_strength = self.learning_rate * 0.5;

            // Weaken other synapses on same post-neuron
            let post = active_synapse.1;
            let keys_to_weaken: Vec<_> = self
                .synapses
                .keys()
                .filter(|(p_pre, p_post)| {
                    *p_post == post && self.distance(pre, *p_pre) <= neighborhood_radius
                })
                .cloned()
                .collect();

            for key in keys_to_weaken {
                if key != active_synapse {
                    if let Some(synapse) = self.synapses.get_mut(&key) {
                        synapse.weight = (synapse.weight - depression_strength).max(0.0);
                        synapse.ltd_count += 1;
                    }
                }
            }
        }
    }

    /// Calculate simple distance between blocks
    fn distance(&self, a: u32, b: u32) -> u32 {
        a.abs_diff(b)
    }

    /// Time-dependent plasticity: high at start, decreases with practice, increases with new strategy
    pub fn time_dependent_plasticity(
        &mut self,
        synapse_key: (u32, u32),
        practice_count: u32,
        strategy_age_ms: u64,
    ) -> f32 {
        let now = Self::now_ms();

        if let Some(synapse) = self.synapses.get_mut(&synapse_key) {
            // Phase 1: Early learning (high plasticity, 0-10 practices)
            let early_phase_rate = if practice_count < 10 {
                0.2 + (10 - practice_count) as f32 * 0.01 // 0.2-0.3
            } else {
                0.05
            };

            // Phase 2: Consolidation (low plasticity, 10-50 practices)
            let consolidation_decay = if (10..50).contains(&practice_count) {
                0.05 * (50 - practice_count) as f32 / 40.0 // 0.0625 → 0.05
            } else if practice_count >= 50 {
                0.02 // Very low
            } else {
                early_phase_rate
            };

            // Phase 3: New strategy detection (high plasticity boost if strategy is new)
            let strategy_boost = if strategy_age_ms < 300_000 {
                // 5 minutes
                0.1 * (1.0 - (strategy_age_ms as f32 / 300_000.0)) // 0.1 → 0.0
            } else {
                0.0
            };

            self.learning_rate = (consolidation_decay + strategy_boost).min(0.3);
            synapse.last_pre_spike_ms = now;

            self.learning_rate
        } else {
            0.2 // Default high plasticity for new synapses
        }
    }

    fn now_ms() -> u64 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64
    }
}