mnemo-core 0.5.12

Core storage, data model, query engine, and indexing for Mnemo
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::error::Result;
use crate::hash::compute_content_hash;
use crate::model::event::{AgentEvent, EventType};
use crate::model::memory::{ConsolidationState, MemoryRecord, MemoryType, SourceType};
use crate::model::relation::Relation;
use crate::query::MnemoEngine;
use crate::storage::MemoryFilter;

/// Custom decay function types.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum DecayFunction {
    /// Exponential: base * e^(-rate * hours)  (default, Ebbinghaus-inspired)
    Exponential,
    /// Linear: base * max(0, 1 - rate * hours)
    Linear,
    /// Step function: base importance until threshold hours, then 0
    StepFunction(f32),
    /// Power law: base / (1 + rate * hours)^alpha
    PowerLaw(f32),
}

impl DecayFunction {
    pub fn from_str_opt(s: &str) -> Option<Self> {
        match s {
            "exponential" => Some(DecayFunction::Exponential),
            "linear" => Some(DecayFunction::Linear),
            s if s.starts_with("step:") => {
                s[5..].parse::<f32>().ok().map(DecayFunction::StepFunction)
            }
            s if s.starts_with("power_law:") => {
                s[10..].parse::<f32>().ok().map(DecayFunction::PowerLaw)
            }
            _ => None,
        }
    }
}

/// Compute effective importance using the specified or default decay curve.
/// Default (Exponential): `base_importance * e^(-decay_rate * hours) + 0.05 * ln(1 + access_count)`
pub fn effective_importance(record: &MemoryRecord) -> f32 {
    let decay_fn = record
        .decay_function
        .as_deref()
        .and_then(DecayFunction::from_str_opt)
        .unwrap_or(DecayFunction::Exponential);
    effective_importance_with(record, &decay_fn)
}

pub fn effective_importance_with(record: &MemoryRecord, decay_fn: &DecayFunction) -> f32 {
    let decay_rate = record.decay_rate.unwrap_or(0.01);
    let hours = hours_since_creation(&record.created_at);
    let access_boost = 0.05 * (1.0 + record.access_count as f32).ln();

    let base = match decay_fn {
        DecayFunction::Exponential => record.importance * (-decay_rate * hours).exp(),
        DecayFunction::Linear => record.importance * (1.0 - decay_rate * hours).max(0.0),
        DecayFunction::StepFunction(threshold_hours) => {
            if hours < *threshold_hours {
                record.importance
            } else {
                0.0
            }
        }
        DecayFunction::PowerLaw(alpha) => {
            record.importance / (1.0 + decay_rate * hours).powf(*alpha)
        }
    };

    (base + access_boost).min(1.0)
}

fn hours_since_creation(created_at: &str) -> f32 {
    let now = chrono::Utc::now();
    match chrono::DateTime::parse_from_rfc3339(created_at) {
        Ok(dt) => {
            let age = now - dt.with_timezone(&chrono::Utc);
            (age.num_seconds() as f32 / 3600.0).max(0.0)
        }
        Err(_) => 0.0,
    }
}

#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DecayPassResult {
    pub archived: usize,
    pub forgotten: usize,
    pub total_processed: usize,
}

impl DecayPassResult {
    pub fn new(archived: usize, forgotten: usize, total_processed: usize) -> Self {
        Self {
            archived,
            forgotten,
            total_processed,
        }
    }
}

/// Run a decay pass over all active memories for the given agent.
/// Memories below `forget_threshold` are marked Forgotten.
/// Memories below `archive_threshold` (but above forget) are marked Archived.
pub async fn run_decay_pass(
    engine: &MnemoEngine,
    agent_id: &str,
    archive_threshold: f32,
    forget_threshold: f32,
) -> Result<DecayPassResult> {
    let filter = MemoryFilter {
        agent_id: Some(agent_id.to_string()),
        include_deleted: false,
        ..Default::default()
    };
    let memories = engine
        .storage
        .list_memories(&filter, super::MAX_BATCH_QUERY_LIMIT, 0)
        .await?;

    let mut archived = 0;
    let mut forgotten = 0;
    let total_processed = memories.len();

    for mut record in memories {
        if record.consolidation_state == ConsolidationState::Forgotten
            || record.consolidation_state == ConsolidationState::Archived
        {
            continue;
        }

        let eff = effective_importance(&record);

        if eff < forget_threshold {
            record.consolidation_state = ConsolidationState::Forgotten;
            record.updated_at = chrono::Utc::now().to_rfc3339();
            engine.storage.update_memory(&record).await?;
            forgotten += 1;
        } else if eff < archive_threshold {
            record.consolidation_state = ConsolidationState::Archived;
            record.updated_at = chrono::Utc::now().to_rfc3339();
            engine.storage.update_memory(&record).await?;
            archived += 1;
        }
    }

    Ok(DecayPassResult {
        archived,
        forgotten,
        total_processed,
    })
}

#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsolidationResult {
    pub clusters_found: usize,
    pub new_memories_created: usize,
    pub originals_consolidated: usize,
    /// v0.4.10 — number of clusters skipped because the engine has a
    /// [`crate::query::maturity::ConsolidationPolicy::MaturityDriven`]
    /// policy attached and the cluster's combined maturity score did
    /// not clear the configured threshold. Always zero under the
    /// default `FixedSize` policy.
    #[serde(default)]
    pub clusters_skipped_below_threshold: usize,
}

impl ConsolidationResult {
    pub fn new(
        clusters_found: usize,
        new_memories_created: usize,
        originals_consolidated: usize,
    ) -> Self {
        Self {
            clusters_found,
            new_memories_created,
            originals_consolidated,
            clusters_skipped_below_threshold: 0,
        }
    }
}

/// Consolidate episodic memories into semantic summaries.
///
/// Clusters by tag overlap. Whether a cluster is actually consolidated
/// depends on the engine's
/// [`crate::query::maturity::ConsolidationPolicy`]:
///
/// - `FixedSize` (default): every cluster with at least
///   `min_cluster_size` members is consolidated unconditionally — the
///   v0.4.x behaviour.
/// - `MaturityDriven`: a cluster is consolidated iff its combined
///   maturity score `>=` the policy's `threshold` AND it has at least
///   `max(min_cluster_size, policy.min_cluster_size_floor)` members.
pub async fn run_consolidation(
    engine: &MnemoEngine,
    agent_id: &str,
    min_cluster_size: usize,
) -> Result<ConsolidationResult> {
    let filter = MemoryFilter {
        agent_id: Some(agent_id.to_string()),
        memory_type: Some(MemoryType::Episodic),
        include_deleted: false,
        ..Default::default()
    };
    let memories = engine
        .storage
        .list_memories(&filter, super::MAX_BATCH_QUERY_LIMIT, 0)
        .await?;

    // Only consider memories that are Raw or Active
    let active: Vec<MemoryRecord> = memories
        .into_iter()
        .filter(|m| {
            m.consolidation_state == ConsolidationState::Raw
                || m.consolidation_state == ConsolidationState::Active
        })
        .collect();

    // Cluster by tag overlap: group memories sharing at least one tag
    let mut clusters: Vec<Vec<&MemoryRecord>> = Vec::new();

    for record in &active {
        let mut found_cluster = false;
        for cluster in &mut clusters {
            // Check if this record shares any tag with any record in cluster
            if cluster
                .iter()
                .any(|c| c.tags.iter().any(|t| record.tags.contains(t)))
            {
                cluster.push(record);
                found_cluster = true;
                break;
            }
        }
        if !found_cluster {
            clusters.push(vec![record]);
        }
    }

    let mut clusters_found = 0;
    let mut new_memories_created = 0;
    let mut originals_consolidated = 0;
    let mut clusters_skipped_below_threshold = 0;

    // v0.4.10 — read the per-engine consolidation policy once. Default
    // FixedSize preserves the legacy unconditional path.
    let policy = engine.consolidation_policy.clone();

    for cluster in &clusters {
        let effective_min = match &policy {
            crate::query::maturity::ConsolidationPolicy::FixedSize => min_cluster_size,
            crate::query::maturity::ConsolidationPolicy::MaturityDriven(p) => {
                min_cluster_size.max(p.min_cluster_size_floor)
            }
        };
        if cluster.len() < effective_min {
            continue;
        }

        // Feedback-driven trigger gate: skip the cluster when its
        // combined maturity score does not clear the configured
        // threshold. FixedSize policy never enters this branch.
        if let crate::query::maturity::ConsolidationPolicy::MaturityDriven(p) = &policy {
            match crate::query::maturity::compute_cluster_maturity(
                engine,
                cluster,
                p.weights,
                p.saturation,
            )
            .await?
            {
                Some(b) if b.combined < p.threshold => {
                    tracing::debug!(
                        agent_id,
                        cluster_size = cluster.len(),
                        score = b.combined,
                        threshold = p.threshold,
                        "consolidation: cluster below maturity threshold"
                    );
                    clusters_skipped_below_threshold += 1;
                    continue;
                }
                _ => {}
            }
        }

        clusters_found += 1;

        // Create a consolidated semantic memory
        let combined_content: Vec<String> = cluster.iter().map(|m| m.content.clone()).collect();
        let content = format!(
            "[Consolidated from {} memories] {}",
            cluster.len(),
            combined_content.join(" | ")
        );
        let avg_importance =
            cluster.iter().map(|m| m.importance).sum::<f32>() / cluster.len() as f32;
        let all_tags: Vec<String> = cluster
            .iter()
            .flat_map(|m| m.tags.iter().cloned())
            .collect::<std::collections::HashSet<String>>()
            .into_iter()
            .collect();

        let now = chrono::Utc::now().to_rfc3339();
        let new_id = Uuid::now_v7();
        let content_hash = crate::hash::compute_content_hash(&content, agent_id, &now);

        let embedding = engine.embedding.embed(&content).await?;

        let prev_hash_raw = engine
            .storage
            .get_latest_memory_hash(agent_id, None)
            .await
            .ok()
            .flatten();
        let prev_hash = Some(crate::hash::compute_chain_hash(
            &content_hash,
            prev_hash_raw.as_deref(),
        ));

        let new_record = MemoryRecord {
            id: new_id,
            agent_id: agent_id.to_string(),
            content,
            memory_type: MemoryType::Semantic,
            scope: cluster[0].scope,
            importance: avg_importance,
            tags: all_tags,
            metadata: serde_json::json!({"consolidated_from": cluster.iter().map(|m| m.id.to_string()).collect::<Vec<_>>()}),
            embedding: Some(embedding.clone()),
            content_hash: content_hash.clone(),
            prev_hash,
            source_type: SourceType::Consolidation,
            source_id: None,
            consolidation_state: ConsolidationState::Active,
            access_count: 0,
            org_id: cluster[0].org_id.clone(),
            thread_id: None,
            created_at: now.clone(),
            updated_at: now,
            last_accessed_at: None,
            expires_at: None,
            deleted_at: None,
            decay_rate: None,
            created_by: Some("consolidation_engine".to_string()),
            version: 1,
            prev_version_id: None,
            quarantined: false,
            quarantine_reason: None,
            decay_function: None,
        };

        engine.storage.insert_memory(&new_record).await?;
        engine.index.add(new_id, &embedding)?;
        if let Some(ref ft) = engine.full_text {
            ft.add(new_id, &new_record.content)?;
            ft.commit()?;
        }
        new_memories_created += 1;

        // Create relations and mark originals as consolidated
        for original in cluster {
            let relation = Relation {
                id: Uuid::now_v7(),
                source_id: new_id,
                target_id: original.id,
                relation_type: "consolidated_from".to_string(),
                weight: 1.0,
                metadata: serde_json::Value::Object(serde_json::Map::new()),
                created_at: new_record.created_at.clone(),
            };
            if let Err(e) = engine.storage.insert_relation(&relation).await {
                tracing::error!(relation_id = %relation.id, error = %e, "failed to insert consolidation relation");
            }

            let mut updated = (*original).clone();
            updated.consolidation_state = ConsolidationState::Consolidated;
            updated.updated_at = chrono::Utc::now().to_rfc3339();
            if let Err(e) = engine.storage.update_memory(&updated).await {
                tracing::error!(memory_id = %updated.id, error = %e, "failed to update consolidation state");
            }
            originals_consolidated += 1;
        }
    }

    Ok(ConsolidationResult {
        clusters_found,
        new_memories_created,
        originals_consolidated,
        clusters_skipped_below_threshold,
    })
}

/// Report from a single TTL sweep pass.
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TtlReport {
    pub swept_count: usize,
    pub errors: Vec<TtlError>,
}

impl TtlReport {
    pub fn new(swept_count: usize, errors: Vec<TtlError>) -> Self {
        Self {
            swept_count,
            errors,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TtlError {
    pub memory_id: Uuid,
    pub error: String,
}

/// Hard-delete every memory whose `expires_at` is in the past.
///
/// Each deletion emits an `EventType::MemoryExpired` audit event so the chain
/// records which memories were purged and when. The function is idempotent
/// under concurrent callers (storage deletes of an already-absent row surface
/// as a storage error and are reported, not retried).
pub async fn run_ttl_sweep(engine: &MnemoEngine) -> Result<TtlReport> {
    let filter = MemoryFilter {
        include_deleted: false,
        ..Default::default()
    };
    let memories = engine
        .storage
        .list_memories(&filter, super::MAX_BATCH_QUERY_LIMIT, 0)
        .await?;

    let now = chrono::Utc::now();
    let now_str = now.to_rfc3339();
    let mut swept_count = 0;
    let mut errors = Vec::new();

    for record in memories {
        let Some(ref expires_at) = record.expires_at else {
            continue;
        };
        let Ok(exp) = chrono::DateTime::parse_from_rfc3339(expires_at) else {
            continue;
        };
        if exp > now {
            continue;
        }

        match engine.storage.hard_delete_memory(record.id).await {
            Ok(()) => {
                if let Err(e) = engine.index.remove(record.id) {
                    tracing::warn!(memory_id = %record.id, error = %e, "ttl sweep: vector index remove failed");
                }
                if let Some(ref ft) = engine.full_text {
                    if let Err(e) = ft.remove(record.id) {
                        tracing::warn!(memory_id = %record.id, error = %e, "ttl sweep: full-text remove failed");
                    }
                    let _ = ft.commit();
                }
                if let Some(ref cache) = engine.cache {
                    cache.invalidate(record.id);
                }
                emit_expiry_event(engine, &record, &now_str).await;
                swept_count += 1;
            }
            Err(e) => errors.push(TtlError {
                memory_id: record.id,
                error: e.to_string(),
            }),
        }
    }

    Ok(TtlReport {
        swept_count,
        errors,
    })
}

async fn emit_expiry_event(engine: &MnemoEngine, record: &MemoryRecord, now_str: &str) {
    let event_content_hash =
        compute_content_hash(&record.id.to_string(), &record.agent_id, now_str);
    let prev_event_hash = match engine
        .storage
        .get_latest_event_hash(&record.agent_id, None)
        .await
    {
        Ok(hash) => hash,
        Err(e) => {
            tracing::warn!(error = %e, "ttl sweep: failed to read prev event hash, starting new chain segment");
            None
        }
    };
    let event_prev_hash = Some(crate::hash::compute_chain_hash(
        &event_content_hash,
        prev_event_hash.as_deref(),
    ));

    let event = AgentEvent {
        id: Uuid::now_v7(),
        agent_id: record.agent_id.clone(),
        thread_id: None,
        run_id: None,
        parent_event_id: None,
        event_type: EventType::MemoryExpired,
        payload: serde_json::json!({
            "memory_id": record.id.to_string(),
            "expired_at": record.expires_at.clone(),
        }),
        trace_id: None,
        span_id: None,
        model: None,
        tokens_input: None,
        tokens_output: None,
        latency_ms: None,
        cost_usd: None,
        timestamp: now_str.to_string(),
        logical_clock: 0,
        content_hash: event_content_hash,
        prev_hash: event_prev_hash,
        embedding: None,
    };
    if let Err(e) = engine.storage.insert_event(&event).await {
        tracing::error!(event_id = %event.id, error = %e, "ttl sweep: failed to insert MemoryExpired event");
    }
}

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

    #[test]
    fn test_effective_importance_decay() {
        // Fresh memory with high importance
        let now = chrono::Utc::now().to_rfc3339();
        let record = MemoryRecord {
            id: Uuid::now_v7(),
            agent_id: "agent-1".to_string(),
            content: "test".to_string(),
            memory_type: MemoryType::Episodic,
            scope: Scope::Private,
            importance: 0.8,
            tags: vec![],
            metadata: serde_json::json!({}),
            embedding: None,
            content_hash: vec![],
            prev_hash: None,
            source_type: SourceType::Agent,
            source_id: None,
            consolidation_state: ConsolidationState::Raw,
            access_count: 0,
            org_id: None,
            thread_id: None,
            created_at: now,
            updated_at: "2025-01-01T00:00:00Z".to_string(),
            last_accessed_at: None,
            expires_at: None,
            deleted_at: None,
            decay_rate: Some(0.01),
            created_by: None,
            version: 1,
            prev_version_id: None,
            quarantined: false,
            quarantine_reason: None,
            decay_function: None,
        };

        let eff = effective_importance(&record);
        // Fresh memory should be close to base importance
        assert!(
            eff > 0.7,
            "effective importance {eff} should be > 0.7 for fresh memory"
        );

        // Old memory with high decay rate
        let old_date = (chrono::Utc::now() - chrono::Duration::hours(1000)).to_rfc3339();
        let old_record = MemoryRecord {
            created_at: old_date,
            decay_rate: Some(0.01),
            access_count: 0,
            ..record.clone()
        };
        let old_eff = effective_importance(&old_record);
        assert!(
            old_eff < eff,
            "old memory {old_eff} should have lower importance than fresh {eff}"
        );

        // Access count boosts importance
        let accessed_record = MemoryRecord {
            access_count: 100,
            ..old_record.clone()
        };
        let accessed_eff = effective_importance(&accessed_record);
        assert!(
            accessed_eff > old_eff,
            "accessed memory {accessed_eff} should be higher than unaccessed {old_eff}"
        );
    }

    use crate::embedding::NoopEmbedding;
    use crate::index::usearch::UsearchIndex;
    use crate::query::MnemoEngine;
    use crate::query::maturity::{
        ConsolidationPolicy, MaturityPolicy, MaturitySaturation, MaturityWeights,
    };
    use crate::query::remember::RememberRequest;
    use crate::storage::duckdb::DuckDbStorage;
    use std::sync::Arc;

    async fn seed_two_clusters(engine: &MnemoEngine, agent: &str) {
        // Cluster A: 3 records sharing tag "topic-a".
        for i in 0..3 {
            let mut req = RememberRequest::new(format!("a-fact-{i}"));
            req.tags = Some(vec!["topic-a".to_string()]);
            req.agent_id = Some(agent.to_string());
            engine.remember(req).await.expect("remember a");
        }
        // Cluster B: 3 records sharing tag "topic-b".
        for i in 0..3 {
            let mut req = RememberRequest::new(format!("b-fact-{i}"));
            req.tags = Some(vec!["topic-b".to_string()]);
            req.agent_id = Some(agent.to_string());
            engine.remember(req).await.expect("remember b");
        }
    }

    fn build_test_engine(policy: ConsolidationPolicy) -> MnemoEngine {
        let storage = Arc::new(DuckDbStorage::open_in_memory().unwrap());
        let index = Arc::new(UsearchIndex::new(3).unwrap());
        let embedding = Arc::new(NoopEmbedding::new(3));
        MnemoEngine::new(storage, index, embedding, "tester".to_string(), None)
            .with_consolidation_policy(policy)
    }

    #[tokio::test]
    async fn fixed_size_policy_consolidates_unconditionally() {
        let engine = build_test_engine(ConsolidationPolicy::FixedSize);
        seed_two_clusters(&engine, "tester").await;
        let result = run_consolidation(&engine, "tester", 2)
            .await
            .expect("run_consolidation");
        assert_eq!(result.clusters_found, 2);
        assert_eq!(result.new_memories_created, 2);
        assert_eq!(result.clusters_skipped_below_threshold, 0);
    }

    #[tokio::test]
    async fn maturity_policy_skips_below_threshold() {
        // Threshold high enough that fresh, never-accessed, edge-less
        // clusters with NoopEmbedding will never clear it.
        let policy = MaturityPolicy {
            weights: MaturityWeights::balanced(),
            saturation: MaturitySaturation::default(),
            threshold: 0.95,
            min_cluster_size_floor: 2,
            trigger_on_forget: false,
            trigger_on_checkpoint: false,
        };
        let engine = build_test_engine(ConsolidationPolicy::MaturityDriven(policy));
        seed_two_clusters(&engine, "tester").await;
        let result = run_consolidation(&engine, "tester", 2)
            .await
            .expect("run_consolidation");
        assert_eq!(result.clusters_found, 0);
        assert_eq!(result.new_memories_created, 0);
        assert_eq!(result.clusters_skipped_below_threshold, 2);
    }

    #[tokio::test]
    async fn maturity_policy_consolidates_when_threshold_clears() {
        // Threshold of 0.0 means every non-degenerate cluster passes.
        let policy = MaturityPolicy {
            weights: MaturityWeights::balanced(),
            saturation: MaturitySaturation::default(),
            threshold: 0.0,
            min_cluster_size_floor: 2,
            trigger_on_forget: false,
            trigger_on_checkpoint: false,
        };
        let engine = build_test_engine(ConsolidationPolicy::MaturityDriven(policy));
        seed_two_clusters(&engine, "tester").await;
        let result = run_consolidation(&engine, "tester", 2)
            .await
            .expect("run_consolidation");
        assert_eq!(result.clusters_found, 2);
        assert_eq!(result.new_memories_created, 2);
        assert_eq!(result.clusters_skipped_below_threshold, 0);
    }

    #[tokio::test]
    async fn maturity_policy_respects_min_cluster_floor() {
        // floor = 5 — clusters of 3 must be rejected on size alone.
        let policy = MaturityPolicy {
            weights: MaturityWeights::balanced(),
            saturation: MaturitySaturation::default(),
            threshold: 0.0,
            min_cluster_size_floor: 5,
            trigger_on_forget: false,
            trigger_on_checkpoint: false,
        };
        let engine = build_test_engine(ConsolidationPolicy::MaturityDriven(policy));
        seed_two_clusters(&engine, "tester").await;
        let result = run_consolidation(&engine, "tester", 2)
            .await
            .expect("run_consolidation");
        assert_eq!(result.clusters_found, 0);
        assert_eq!(result.new_memories_created, 0);
        // Skipped on size, not on threshold — the size check runs first.
        assert_eq!(result.clusters_skipped_below_threshold, 0);
    }
}