thermogram 0.5.2

Plastic memory capsule with 4-temperature tensor states (hot/warm/cool/cold), bidirectional transitions, and hash-chained auditability
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
//! Consolidation - Dirty → Clean state transitions
//!
//! Consolidation applies accumulated deltas to create a clean snapshot,
//! pruning weak memories and applying plasticity rules.
//!
//! ## Ternary Consolidation
//!
//! For ternary weights, consolidation uses discrete state transitions:
//! - **Pruning**: Only Zero weights are prunable (inactive)
//! - **Merging**: Uses majority voting across observations
//! - **Strength**: Ternary weights use state transitions instead of decay

use crate::delta::{Delta, DeltaType};
use crate::plasticity::PlasticityRule;
use crate::ternary::TernaryWeight;
use crate::error::Result;
use chrono::{DateTime, Utc, Duration};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Policy for when to consolidate
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsolidationPolicy {
    /// Triggers that initiate consolidation
    pub triggers: Vec<ConsolidationTrigger>,

    /// Whether to prune weak memories during consolidation
    pub enable_pruning: bool,

    /// Whether to merge similar memories
    pub enable_merging: bool,
}

/// Triggers for consolidation
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConsolidationTrigger {
    /// Consolidate after N deltas
    DeltaCount(usize),

    /// Consolidate after time period
    TimePeriod { hours: u64 },

    /// Consolidate when dirty state exceeds size
    DirtySize { bytes: usize },

    /// Manual trigger
    Manual,
}

impl ConsolidationPolicy {
    /// Default policy - consolidate every 1000 deltas or 24 hours
    pub fn default() -> Self {
        Self {
            triggers: vec![
                ConsolidationTrigger::DeltaCount(1000),
                ConsolidationTrigger::TimePeriod { hours: 24 },
            ],
            enable_pruning: true,
            enable_merging: true,
        }
    }

    /// Aggressive consolidation - frequent, with pruning
    pub fn aggressive() -> Self {
        Self {
            triggers: vec![
                ConsolidationTrigger::DeltaCount(100),
                ConsolidationTrigger::TimePeriod { hours: 1 },
            ],
            enable_pruning: true,
            enable_merging: true,
        }
    }

    /// Conservative consolidation - infrequent, no pruning
    pub fn conservative() -> Self {
        Self {
            triggers: vec![
                ConsolidationTrigger::DeltaCount(10000),
                ConsolidationTrigger::TimePeriod { hours: 168 }, // 1 week
            ],
            enable_pruning: false,
            enable_merging: false,
        }
    }

    /// Check if any trigger is met
    pub fn should_consolidate(
        &self,
        delta_count: usize,
        last_consolidation: &DateTime<Utc>,
        dirty_size: usize,
    ) -> bool {
        for trigger in &self.triggers {
            match trigger {
                ConsolidationTrigger::DeltaCount(n) => {
                    if delta_count >= *n {
                        return true;
                    }
                }
                ConsolidationTrigger::TimePeriod { hours } => {
                    let elapsed = Utc::now().signed_duration_since(*last_consolidation);
                    let threshold = Duration::hours(*hours as i64);
                    if elapsed >= threshold {
                        return true;
                    }
                }
                ConsolidationTrigger::DirtySize { bytes } => {
                    if dirty_size >= *bytes {
                        return true;
                    }
                }
                ConsolidationTrigger::Manual => {
                    // Manual triggers don't auto-fire
                }
            }
        }
        false
    }
}

/// Consolidated state entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsolidatedEntry {
    /// Key
    pub key: String,

    /// Consolidated value
    pub value: Vec<u8>,

    /// Current strength (after plasticity applied) - continuous value
    pub strength: f32,

    /// Ternary strength for quantized weights (+1, 0, -1)
    /// When present, this takes precedence over f32 strength for pruning
    #[serde(default)]
    pub ternary_strength: Option<TernaryWeight>,

    /// Last updated timestamp
    pub updated_at: DateTime<Utc>,

    /// Number of updates contributing to this entry
    pub update_count: usize,
}

impl ConsolidatedEntry {
    /// Check if this entry uses ternary strength
    pub fn is_ternary(&self) -> bool {
        self.ternary_strength.is_some()
    }

    /// Get effective strength as f32
    pub fn effective_strength(&self) -> f32 {
        if let Some(t) = self.ternary_strength {
            t.to_f32()
        } else {
            self.strength
        }
    }

    /// Check if this entry should be pruned
    /// For ternary: only Zero weights are prunable
    /// For continuous: uses threshold comparison
    pub fn should_prune(&self, threshold: f32) -> bool {
        if let Some(t) = self.ternary_strength {
            t == TernaryWeight::Zero
        } else {
            self.strength < threshold
        }
    }
}

/// Result of consolidation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsolidationResult {
    /// Number of deltas consolidated
    pub deltas_processed: usize,

    /// Number of entries in clean state
    pub entries_after: usize,

    /// Number of entries pruned
    pub entries_pruned: usize,

    /// Number of entries merged
    pub entries_merged: usize,

    /// Size reduction (bytes)
    pub size_reduction: usize,

    /// When consolidation happened
    pub timestamp: DateTime<Utc>,
}

/// Consolidate deltas into clean state
///
/// Handles both continuous (f32) and ternary weights. When a delta has
/// ternary_strength set, uses discrete state transitions instead of
/// continuous plasticity updates.
pub fn consolidate(
    dirty_deltas: &[Delta],
    clean_state: &HashMap<String, ConsolidatedEntry>,
    plasticity_rule: &PlasticityRule,
    policy: &ConsolidationPolicy,
) -> Result<(HashMap<String, ConsolidatedEntry>, ConsolidationResult)> {
    let mut new_state = clean_state.clone();
    let mut stats = ConsolidationResult {
        deltas_processed: dirty_deltas.len(),
        entries_after: 0,
        entries_pruned: 0,
        entries_merged: 0,
        size_reduction: 0,
        timestamp: Utc::now(),
    };

    // Apply each delta
    for delta in dirty_deltas {
        match delta.delta_type {
            DeltaType::Create | DeltaType::Update => {
                if let Some(existing) = new_state.get_mut(&delta.key) {
                    // Update existing entry
                    if delta.is_ternary() {
                        // Ternary update: use discrete state transitions
                        let current_ternary = existing
                            .ternary_strength
                            .unwrap_or(TernaryWeight::from_f32(existing.strength, 0.3));
                        let observed_ternary = delta.metadata.ternary_strength.unwrap();

                        // Use observation count as confidence proxy
                        let confidence = delta
                            .metadata
                            .observation_count
                            .map(|c| (c as f32 / 10.0).min(1.0))
                            .unwrap_or(0.5);

                        let new_ternary = plasticity_rule.apply_ternary_update(
                            current_ternary,
                            observed_ternary,
                            confidence,
                        );

                        existing.ternary_strength = Some(new_ternary);
                        existing.strength = new_ternary.to_f32();
                    } else {
                        // Continuous update: use traditional plasticity
                        let time_delta = delta
                            .metadata
                            .timestamp
                            .signed_duration_since(existing.updated_at)
                            .num_seconds() as f64;

                        let new_strength = plasticity_rule.apply_update(
                            existing.strength,
                            delta.metadata.strength,
                            time_delta,
                        );

                        existing.strength = new_strength;
                    }

                    existing.value = delta.value.clone();
                    existing.updated_at = delta.metadata.timestamp;
                    existing.update_count += 1;
                } else {
                    // Create new entry
                    new_state.insert(
                        delta.key.clone(),
                        ConsolidatedEntry {
                            key: delta.key.clone(),
                            value: delta.value.clone(),
                            strength: delta.metadata.strength,
                            ternary_strength: delta.metadata.ternary_strength,
                            updated_at: delta.metadata.timestamp,
                            update_count: 1,
                        },
                    );
                }
            }

            DeltaType::Delete => {
                new_state.remove(&delta.key);
            }

            DeltaType::Merge => {
                // Merge operation - combine with existing
                if let Some(existing) = new_state.get_mut(&delta.key) {
                    if delta.is_ternary() {
                        // Ternary merge: use majority voting
                        let current_ternary = existing
                            .ternary_strength
                            .unwrap_or(TernaryWeight::from_f32(existing.strength, 0.3));
                        let observed_ternary = delta.metadata.ternary_strength.unwrap();

                        // For merge, collect both weights and vote
                        let merged = PlasticityRule::ternary_majority_vote(&[
                            current_ternary,
                            observed_ternary,
                        ]);

                        existing.ternary_strength = Some(merged);
                        existing.strength = merged.to_f32();
                    } else {
                        // Continuous merge
                        let time_delta = delta
                            .metadata
                            .timestamp
                            .signed_duration_since(existing.updated_at)
                            .num_seconds() as f64;

                        let new_strength = plasticity_rule.apply_update(
                            existing.strength,
                            delta.metadata.strength,
                            time_delta,
                        );

                        existing.strength = new_strength;
                    }

                    existing.updated_at = delta.metadata.timestamp;
                    existing.update_count += 1;
                    stats.entries_merged += 1;
                }
            }
        }
    }

    // Prune weak entries if enabled
    if policy.enable_pruning {
        let before_count = new_state.len();
        new_state.retain(|_, entry| {
            // Use entry's own should_prune method which handles ternary
            !entry.should_prune(plasticity_rule.prune_threshold)
        });
        stats.entries_pruned = before_count - new_state.len();
    }

    stats.entries_after = new_state.len();

    Ok((new_state, stats))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_consolidation_policy_delta_count() {
        let policy = ConsolidationPolicy::default();
        let last_consolidation = Utc::now();

        assert!(!policy.should_consolidate(500, &last_consolidation, 0));
        assert!(policy.should_consolidate(1000, &last_consolidation, 0));
    }

    #[test]
    fn test_consolidation_policy_time() {
        let policy = ConsolidationPolicy::default();
        let last_consolidation = Utc::now() - Duration::hours(25);

        assert!(policy.should_consolidate(0, &last_consolidation, 0));
    }

    #[test]
    fn test_consolidate_create() {
        let deltas = vec![
            Delta::create("key1", b"value1".to_vec(), "source"),
        ];

        let clean_state = HashMap::new();
        let plasticity = PlasticityRule::stdp_like();
        let policy = ConsolidationPolicy::default();

        let (new_state, stats) = consolidate(&deltas, &clean_state, &plasticity, &policy).unwrap();

        assert_eq!(new_state.len(), 1);
        assert_eq!(stats.deltas_processed, 1);
        assert_eq!(stats.entries_after, 1);
    }

    #[test]
    fn test_consolidate_update() {
        let mut clean_state = HashMap::new();
        clean_state.insert(
            "key1".to_string(),
            ConsolidatedEntry {
                key: "key1".to_string(),
                value: b"old_value".to_vec(),
                strength: 0.5,
                ternary_strength: None,
                updated_at: Utc::now(),
                update_count: 1,
            },
        );

        let deltas = vec![
            Delta::update("key1", b"new_value".to_vec(), "source", 0.8, None),
        ];

        let plasticity = PlasticityRule::stdp_like();
        let policy = ConsolidationPolicy::default();

        let (new_state, _) = consolidate(&deltas, &clean_state, &plasticity, &policy).unwrap();

        let entry = new_state.get("key1").unwrap();
        assert_eq!(entry.value, b"new_value");
        assert!(entry.strength > 0.5); // Should have strengthened
    }

    #[test]
    fn test_consolidate_with_pruning() {
        let mut clean_state = HashMap::new();
        clean_state.insert(
            "weak_key".to_string(),
            ConsolidatedEntry {
                key: "weak_key".to_string(),
                value: b"value".to_vec(),
                strength: 0.05, // Below prune threshold
                ternary_strength: None,
                updated_at: Utc::now(),
                update_count: 1,
            },
        );

        let deltas = vec![];
        let plasticity = PlasticityRule::stdp_like();
        let policy = ConsolidationPolicy::default();

        let (new_state, stats) = consolidate(&deltas, &clean_state, &plasticity, &policy).unwrap();

        assert_eq!(new_state.len(), 0);
        assert_eq!(stats.entries_pruned, 1);
    }
}