nexo-core 0.1.2

Agent runtime: event bus, sessions, plugin trait, heartbeat, A2A delegation.
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
//! Dreaming — Phase 10.6.
//!
//! Background memory consolidation. Adapted from OpenClaw's three-phase model
//! (`research/docs/concepts/dreaming.md`):
//!
//! 1. **Light** — collect every memory that has at least one recall event and
//!    dedupe by memory id. No durable writes.
//! 2. **REM** — summarize themes for the diary. No durable writes.
//! 3. **Deep** — rank candidates with a weighted score, apply gate thresholds,
//!    append survivors to `MEMORY.md`, record the promotion in SQLite, and
//!    log a summary line to `DREAMS.md`.
//!
//! Gates (`min_score`, `min_recall_count`, `min_unique_queries`) borrow
//! OpenClaw's defaults. Weights likewise. Promoted memories are recorded in
//! `memory_promotions` so subsequent sweeps skip them — the sweep is
//! idempotent even if the cron fires twice.
use chrono::{DateTime, Utc};
use nexo_config::types::agents::{DreamingWeightsYaml, DreamingYamlConfig};
use nexo_memory::{LongTermMemory, RecallSignals};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use tokio::io::AsyncWriteExt;
use uuid::Uuid;
impl From<DreamingYamlConfig> for DreamingConfig {
    fn from(y: DreamingYamlConfig) -> Self {
        Self {
            enabled: y.enabled,
            interval_secs: y.interval_secs,
            min_score: y.min_score,
            min_recall_count: y.min_recall_count,
            min_unique_queries: y.min_unique_queries,
            weights: DreamWeights::from(y.weights),
            max_promotions_per_sweep: y.max_promotions_per_sweep,
        }
    }
}
impl From<DreamingWeightsYaml> for DreamWeights {
    fn from(w: DreamingWeightsYaml) -> Self {
        Self {
            frequency: w.frequency,
            relevance: w.relevance,
            recency: w.recency,
            diversity: w.diversity,
            consolidation: w.consolidation,
        }
    }
}
/// Config for a single dreaming sweep. Weights + gate thresholds; loaded from
/// YAML in `main.rs` (Phase 10.6 wiring).
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct DreamingConfig {
    #[serde(default)]
    pub enabled: bool,
    /// Interval between sweeps, in seconds. Default: 24h.
    #[serde(default = "default_interval_secs")]
    pub interval_secs: u64,
    #[serde(default = "default_min_score")]
    pub min_score: f32,
    #[serde(default = "default_min_recall_count")]
    pub min_recall_count: u32,
    #[serde(default = "default_min_unique_queries")]
    pub min_unique_queries: u32,
    #[serde(default)]
    pub weights: DreamWeights,
    #[serde(default = "default_max_promotions_per_sweep")]
    pub max_promotions_per_sweep: usize,
}
fn default_interval_secs() -> u64 {
    86_400
}
fn default_min_score() -> f32 {
    0.35
}
fn default_min_recall_count() -> u32 {
    3
}
fn default_min_unique_queries() -> u32 {
    2
}
fn default_max_promotions_per_sweep() -> usize {
    20
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct DreamWeights {
    pub frequency: f32,
    pub relevance: f32,
    pub recency: f32,
    pub diversity: f32,
    pub consolidation: f32,
}
impl Default for DreamWeights {
    // OpenClaw defaults (docs/concepts/dreaming.md), minus conceptual_richness
    // (0.06) which is deferred to Phase 10.7.
    fn default() -> Self {
        Self {
            frequency: 0.24,
            relevance: 0.30,
            recency: 0.15,
            diversity: 0.15,
            consolidation: 0.10,
        }
    }
}
impl Default for DreamingConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            interval_secs: default_interval_secs(),
            min_score: default_min_score(),
            min_recall_count: default_min_recall_count(),
            min_unique_queries: default_min_unique_queries(),
            weights: DreamWeights::default(),
            max_promotions_per_sweep: default_max_promotions_per_sweep(),
        }
    }
}
/// One candidate considered by the deep phase.
#[derive(Debug, Clone)]
pub struct DreamCandidate {
    pub memory_id: Uuid,
    pub content: String,
    pub signals: RecallSignals,
    pub score: f32,
    /// `true` when every gate threshold is met.
    pub passed_gates: bool,
}
/// Summary of a single sweep — returned to callers and persisted to DREAMS.md.
#[derive(Debug, Clone)]
pub struct DreamReport {
    pub started_at: DateTime<Utc>,
    pub finished_at: DateTime<Utc>,
    pub agent_id: String,
    pub candidates_considered: usize,
    pub promoted: Vec<DreamCandidate>,
    pub skipped_already_promoted: usize,
    /// Phase 80.1.e — `true` when the scoring sweep deferred because
    /// an autoDream fork-pass was holding the consolidation lock.
    /// Empty `promoted` + zero `candidates_considered` /
    /// `skipped_already_promoted` when set. SKIP pattern mirror of
    /// leak `extractMemories.ts:121-148` `hasMemoryWritesSince`.
    pub deferred_for_fork: bool,
}
pub struct DreamEngine {
    memory: std::sync::Arc<LongTermMemory>,
    workspace: PathBuf,
    config: DreamingConfig,
    /// Phase 77.7 — secret guard for scanning dream candidates
    /// before writing to MEMORY.md. None = no scanning.
    guard: Option<nexo_memory::SecretGuard>,
    /// Phase 80.1.e — optional consolidation-lock probe. When `Some`
    /// and `is_live_holder() == true`, `run_sweep` defers (returns
    /// `DreamReport { deferred_for_fork: true, .. }` without touching
    /// memory). Built at boot when the binding has both `dreaming`
    /// AND `auto_dream` enabled.
    consolidation_probe: Option<std::sync::Arc<dyn nexo_driver_types::ConsolidationLockProbe>>,
}
impl DreamEngine {
    pub fn new(
        memory: std::sync::Arc<LongTermMemory>,
        workspace: impl Into<PathBuf>,
        config: DreamingConfig,
    ) -> Self {
        Self {
            memory,
            workspace: workspace.into(),
            config,
            guard: None,
            consolidation_probe: None,
        }
    }

    /// Phase 77.7 — attach a secret guard for scanning content
    /// before writing to MEMORY.md.
    pub fn with_guard(mut self, guard: nexo_memory::SecretGuard) -> Self {
        self.guard = Some(guard);
        self
    }

    /// Phase 80.1.e — wire a consolidation-lock probe. When set and
    /// the probe reports a live holder, `run_sweep` returns early
    /// with `deferred_for_fork: true`. The autoDream fork-pass
    /// rewrites memory_dir as part of its own work — running the
    /// scoring sweep concurrently would race on `MEMORY.md` writes.
    /// SKIP pattern mirror of leak
    /// `extractMemories.ts:121-148`.
    ///
    /// **Wiring**: at boot, when an agent has both `dreaming.enabled`
    /// AND `auto_dream.is_some()`, construct
    /// `nexo_dream::ConsolidationLock::new(memory_dir, holder_stale)`
    /// and pass `Arc::new(lock) as Arc<dyn ConsolidationLockProbe>`
    /// here. Bindings without both enabled stay with `None` →
    /// no probe, no skip arm, original behaviour preserved.
    pub fn with_consolidation_probe(
        mut self,
        probe: std::sync::Arc<dyn nexo_driver_types::ConsolidationLockProbe>,
    ) -> Self {
        self.consolidation_probe = Some(probe);
        self
    }
    pub fn config(&self) -> &DreamingConfig {
        &self.config
    }
    pub fn workspace(&self) -> &Path {
        &self.workspace
    }
    /// Run one full sweep (light → REM → deep). Safe to call repeatedly:
    /// promoted memories are persisted in `memory_promotions` so later sweeps
    /// skip them.
    pub async fn run_sweep(&self, agent_id: &str) -> anyhow::Result<DreamReport> {
        let started_at = Utc::now();
        tracing::info!(
            agent_id = %agent_id,
            workspace = %self.workspace.display(),
            "dream sweep started"
        );
        // Phase 80.1.e — coordination skip. If a live PID is holding
        // the autoDream consolidation lock, defer this sweep entirely
        // (mirror leak `extractMemories.ts:121-148` SKIP pattern).
        // The fork-pass will rewrite memory_dir as part of its own
        // work; running the scoring sweep concurrently would race on
        // MEMORY.md writes. Probe is None when the binding doesn't
        // have both passes enabled — original behaviour preserved.
        if let Some(probe) = &self.consolidation_probe {
            if probe.is_live_holder() {
                tracing::info!(
                    agent_id = %agent_id,
                    "dream sweep deferred — autoDream fork holds consolidation lock"
                );
                let finished_at = Utc::now();
                return Ok(DreamReport {
                    started_at,
                    finished_at,
                    agent_id: agent_id.to_string(),
                    candidates_considered: 0,
                    promoted: Vec::new(),
                    skipped_already_promoted: 0,
                    deferred_for_fork: true,
                });
            }
        }
        // ── Light: gather every memory with at least one recall event ──────
        let recalled = self.memory.recalled_memories(agent_id).await?;
        let candidates_considered = recalled.len();
        // ── Deep: score + gate + promote ──────────────────────────────────
        let mut scored: Vec<DreamCandidate> = Vec::with_capacity(recalled.len());
        let mut skipped_already_promoted = 0usize;
        for (memory_id, content) in recalled {
            if self.memory.is_promoted(memory_id).await.unwrap_or(false) {
                skipped_already_promoted += 1;
                continue;
            }
            let signals = self
                .memory
                .recall_signals(agent_id, memory_id, None)
                .await?;
            let score = self.score(&signals);
            let passed_gates = signals.recall_count >= self.config.min_recall_count
                && signals.unique_days.max(1) >= 1
                && distinct_queries_for(&signals) >= self.config.min_unique_queries
                && score >= self.config.min_score;
            scored.push(DreamCandidate {
                memory_id,
                content,
                signals,
                score,
                passed_gates,
            });
        }
        scored.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        let mut promoted: Vec<DreamCandidate> = Vec::new();
        for cand in scored.iter() {
            if !cand.passed_gates {
                continue;
            }
            if promoted.len() >= self.config.max_promotions_per_sweep {
                break;
            }
            promoted.push(cand.clone());
        }
        // ── Durable writes: MEMORY.md append + SQLite promotion ledger ────
        if !promoted.is_empty() {
            self.append_to_memory_md(&promoted, started_at).await?;
            for cand in &promoted {
                // Phase 10.7: backfill concept_tags on promoted rows so recall
                // query expansion can find them later. Rows inserted before
                // 10.7 (or via paths that bypassed `remember`) have '[]'.
                let tags = nexo_memory::derive_concept_tags(
                    "",
                    &cand.content,
                    nexo_memory::MAX_CONCEPT_TAGS,
                );
                if !tags.is_empty() {
                    if let Err(e) = self.memory.set_concept_tags(cand.memory_id, &tags).await {
                        tracing::warn!(
                            memory_id = %cand.memory_id,
                            error = %e,
                            "failed to backfill concept_tags on promoted memory"
                        );
                    }
                }
                self.memory
                    .mark_promoted(agent_id, cand.memory_id, cand.score, "deep")
                    .await?;
            }
        }
        let finished_at = Utc::now();
        let report = DreamReport {
            started_at,
            finished_at,
            agent_id: agent_id.to_string(),
            candidates_considered,
            promoted,
            skipped_already_promoted,
            deferred_for_fork: false,
        };
        // ── REM: diary entry (human-readable) ─────────────────────────────
        if let Err(e) = self.append_to_dreams_md(&report).await {
            tracing::warn!(
                agent_id = %agent_id,
                error = %e,
                "DREAMS.md diary append failed — sweep result still valid"
            );
        }
        tracing::info!(
            agent_id = %agent_id,
            candidates = report.candidates_considered,
            promoted = report.promoted.len(),
            skipped = report.skipped_already_promoted,
            "dream sweep finished"
        );
        Ok(report)
    }
    /// Deep-phase weighted score. Uses `consolidation = unique_days / 5` as a
    /// proxy until multi-day recurrence gets its own tracker.
    pub fn score(&self, s: &RecallSignals) -> f32 {
        let consolidation = (s.unique_days as f32 / 5.0).min(1.0);
        let w = &self.config.weights;
        w.frequency * s.frequency
            + w.relevance * s.relevance
            + w.recency * s.recency
            + w.diversity * s.diversity
            + w.consolidation * consolidation
    }
    async fn append_to_memory_md(
        &self,
        promoted: &[DreamCandidate],
        at: DateTime<Utc>,
    ) -> anyhow::Result<()> {
        tokio::fs::create_dir_all(&self.workspace).await?;
        let path = self.workspace.join("MEMORY.md");
        let existed = tokio::fs::try_exists(&path).await.unwrap_or(false);
        let mut file = tokio::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&path)
            .await?;
        // First write seeds a top-level heading so the file reads cleanly.
        if !existed {
            file.write_all(b"# MEMORY.md\n\n").await?;
        }
        let mut block = String::new();
        block.push_str(&format!(
            "\n## Dreamed {}\n\n",
            at.format("%Y-%m-%d %H:%M UTC")
        ));
        for cand in promoted {
            block.push_str(&format!(
                "- {} _(score={:.2}, hits={}, days={})_\n",
                cand.content.trim(),
                cand.score,
                cand.signals.recall_count,
                cand.signals.unique_days
            ));
        }
        // Phase 77.7 — scan before writing to MEMORY.md.
        let block_to_write = if let Some(ref guard) = self.guard {
            match guard.check(&block) {
                Ok(redacted) => redacted,
                Err(e) => {
                    tracing::warn!(
                        target = "memory.secret.blocked",
                        rule_ids = ?e.rule_ids,
                        content_hash = %e.content_hash,
                        workspace = %self.workspace.display(),
                        "dreaming: MEMORY.md write blocked, skipping promoted content"
                    );
                    return Ok(()); // Block: skip write entirely
                }
            }
        } else {
            block
        };
        file.write_all(block_to_write.as_bytes()).await?;
        file.flush().await?;
        Ok(())
    }
    async fn append_to_dreams_md(&self, report: &DreamReport) -> anyhow::Result<()> {
        tokio::fs::create_dir_all(&self.workspace).await?;
        let path = self.workspace.join("DREAMS.md");
        let existed = tokio::fs::try_exists(&path).await.unwrap_or(false);
        let mut file = tokio::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&path)
            .await?;
        if !existed {
            file.write_all("# DREAMS.md — Dream Diary\n\n".as_bytes())
                .await?;
        }
        let mut block = String::new();
        block.push_str(&format!(
            "\n## Deep Sleep {}\n\n",
            report.started_at.format("%Y-%m-%d %H:%M UTC")
        ));
        block.push_str(&format!(
            "- candidates: {}\n- promoted: {}\n- skipped (already promoted): {}\n",
            report.candidates_considered,
            report.promoted.len(),
            report.skipped_already_promoted,
        ));
        if !report.promoted.is_empty() {
            block.push_str("\n### Promoted\n\n");
            for cand in &report.promoted {
                block.push_str(&format!(
                    "- {} — score {:.2}, hits {}\n",
                    cand.content.trim(),
                    cand.score,
                    cand.signals.recall_count,
                ));
            }
        }
        file.write_all(block.as_bytes()).await?;
        file.flush().await?;
        Ok(())
    }
}
/// Distinct-query count reconstruction — the signals struct exposes diversity
/// as a normalized float, but we also need the raw distinct-query count for
/// the `min_unique_queries` gate. We re-derive from the normalization rule
/// (`diversity = min(raw_count, 5) / 5`).
fn distinct_queries_for(s: &RecallSignals) -> u32 {
    (s.diversity * 5.0).round() as u32
}
#[cfg(test)]
mod tests {
    use super::*;
    fn mk_engine(
        ws: &Path,
        cfg: DreamingConfig,
        memory: std::sync::Arc<LongTermMemory>,
    ) -> DreamEngine {
        DreamEngine::new(memory, ws, cfg)
    }
    async fn seed_db() -> std::sync::Arc<LongTermMemory> {
        std::sync::Arc::new(LongTermMemory::open(":memory:").await.unwrap())
    }
    fn tmp_ws(label: &str) -> PathBuf {
        std::env::temp_dir().join(format!("dream-{label}-{}", Uuid::new_v4()))
    }
    #[tokio::test]
    async fn empty_memory_yields_empty_report() -> anyhow::Result<()> {
        let ws = tmp_ws("empty");
        let mem = seed_db().await;
        let engine = mk_engine(&ws, DreamingConfig::default(), mem);
        let report = engine.run_sweep("kate").await?;
        assert_eq!(report.candidates_considered, 0);
        assert_eq!(report.promoted.len(), 0);
        assert!(
            !ws.join("MEMORY.md").exists(),
            "no MEMORY.md without promotions"
        );
        // DREAMS.md is always written — even for empty sweeps — so the diary is auditable.
        assert!(ws.join("DREAMS.md").exists());
        tokio::fs::remove_dir_all(&ws).await.ok();
        Ok(())
    }
    #[tokio::test]
    async fn gate_filters_candidates_below_thresholds() -> anyhow::Result<()> {
        let ws = tmp_ws("gates");
        let mem = seed_db().await;
        // m_strong: 3 hits, 2 distinct queries, high score → promote
        let strong = mem.remember("kate", "user likes dark mode", &[]).await?;
        mem.record_recall_event("kate", strong, "dark", 1.0).await?;
        mem.record_recall_event("kate", strong, "dark mode", 1.0)
            .await?;
        mem.record_recall_event("kate", strong, "preferences", 1.0)
            .await?;
        // m_weak: 1 hit, 1 query → fails min_recall_count
        let weak = mem.remember("kate", "random detail", &[]).await?;
        mem.record_recall_event("kate", weak, "q", 0.5).await?;
        let engine = mk_engine(&ws, DreamingConfig::default(), mem);
        let report = engine.run_sweep("kate").await?;
        assert_eq!(report.candidates_considered, 2);
        assert_eq!(report.promoted.len(), 1, "only strong candidate promotes");
        assert_eq!(report.promoted[0].memory_id, strong);
        let md = tokio::fs::read_to_string(ws.join("MEMORY.md")).await?;
        assert!(md.contains("user likes dark mode"));
        assert!(!md.contains("random detail"));
        tokio::fs::remove_dir_all(&ws).await.ok();
        Ok(())
    }
    #[tokio::test]
    async fn idempotent_sweep_does_not_promote_twice() -> anyhow::Result<()> {
        let ws = tmp_ws("idempotent");
        let mem = seed_db().await;
        let id = mem.remember("kate", "important fact", &[]).await?;
        for q in ["q1", "q2", "q3"] {
            mem.record_recall_event("kate", id, q, 1.0).await?;
        }
        let engine = mk_engine(&ws, DreamingConfig::default(), mem.clone());
        let first = engine.run_sweep("kate").await?;
        assert_eq!(first.promoted.len(), 1);
        let second = engine.run_sweep("kate").await?;
        assert_eq!(second.promoted.len(), 0, "already promoted must be skipped");
        assert_eq!(second.skipped_already_promoted, 1);
        // MEMORY.md must contain exactly one "important fact" line.
        let md = tokio::fs::read_to_string(ws.join("MEMORY.md")).await?;
        let count = md.matches("important fact").count();
        assert_eq!(
            count, 1,
            "fact appended exactly once; got {count} in:\n{md}"
        );
        tokio::fs::remove_dir_all(&ws).await.ok();
        Ok(())
    }
    #[tokio::test]
    async fn score_respects_configured_weights() {
        // Zero-out everything except relevance — score should equal relevance.
        let cfg = DreamingConfig {
            weights: DreamWeights {
                frequency: 0.0,
                relevance: 1.0,
                recency: 0.0,
                diversity: 0.0,
                consolidation: 0.0,
            },
            ..DreamingConfig::default()
        };
        let mem = std::sync::Arc::new(LongTermMemory::open(":memory:").await.unwrap());
        let engine = DreamEngine::new(mem, "/tmp/unused", cfg);
        let s = RecallSignals {
            frequency: 1.0,
            relevance: 0.42,
            recency: 1.0,
            diversity: 1.0,
            recall_count: 9,
            unique_days: 3,
        };
        assert!((engine.score(&s) - 0.42).abs() < 1e-5);
    }
    #[tokio::test]
    async fn max_promotions_caps_output() -> anyhow::Result<()> {
        let ws = tmp_ws("cap");
        let mem = seed_db().await;
        for i in 0..5 {
            let id = mem.remember("kate", &format!("fact {i}"), &[]).await?;
            for q in ["q1", "q2", "q3"] {
                mem.record_recall_event("kate", id, q, 1.0).await?;
            }
        }
        let cfg = DreamingConfig {
            max_promotions_per_sweep: 2,
            ..DreamingConfig::default()
        };
        let engine = mk_engine(&ws, cfg, mem);
        let report = engine.run_sweep("kate").await?;
        assert_eq!(report.candidates_considered, 5);
        assert_eq!(report.promoted.len(), 2, "cap honored");
        tokio::fs::remove_dir_all(&ws).await.ok();
        Ok(())
    }

    // ── Phase 80.1.e — consolidation-lock skip arm ──

    use std::sync::atomic::{AtomicBool, Ordering as StdOrdering};

    /// Toggleable mock probe — flip via `live.store(true|false)`.
    struct MockProbe {
        live: AtomicBool,
    }
    impl MockProbe {
        fn new(initial: bool) -> std::sync::Arc<Self> {
            std::sync::Arc::new(Self {
                live: AtomicBool::new(initial),
            })
        }
    }
    impl nexo_driver_types::ConsolidationLockProbe for MockProbe {
        fn is_live_holder(&self) -> bool {
            self.live.load(StdOrdering::SeqCst)
        }
    }

    #[tokio::test]
    async fn run_sweep_proceeds_when_no_probe_configured() -> anyhow::Result<()> {
        // Probe is None — no skip, behaviour identical to pre-80.1.e.
        let ws = tmp_ws("probe-none");
        let mem = seed_db().await;
        let strong = mem.remember("kate", "user likes dark mode", &[]).await?;
        mem.record_recall_event("kate", strong, "dark", 1.0).await?;
        mem.record_recall_event("kate", strong, "dark mode", 1.0)
            .await?;
        mem.record_recall_event("kate", strong, "preferences", 1.0)
            .await?;
        let engine = mk_engine(&ws, DreamingConfig::default(), mem);
        let report = engine.run_sweep("kate").await?;
        assert!(!report.deferred_for_fork);
        assert_eq!(report.promoted.len(), 1);
        tokio::fs::remove_dir_all(&ws).await.ok();
        Ok(())
    }

    #[tokio::test]
    async fn run_sweep_proceeds_when_probe_says_dead() -> anyhow::Result<()> {
        // Probe present but reports no live holder → normal sweep.
        let ws = tmp_ws("probe-dead");
        let mem = seed_db().await;
        let strong = mem.remember("kate", "user likes dark mode", &[]).await?;
        mem.record_recall_event("kate", strong, "dark", 1.0).await?;
        mem.record_recall_event("kate", strong, "dark mode", 1.0)
            .await?;
        mem.record_recall_event("kate", strong, "preferences", 1.0)
            .await?;
        let engine = mk_engine(&ws, DreamingConfig::default(), mem)
            .with_consolidation_probe(MockProbe::new(false));
        let report = engine.run_sweep("kate").await?;
        assert!(!report.deferred_for_fork);
        assert_eq!(report.promoted.len(), 1);
        tokio::fs::remove_dir_all(&ws).await.ok();
        Ok(())
    }

    #[tokio::test]
    async fn run_sweep_skips_when_probe_says_live() -> anyhow::Result<()> {
        // Probe reports live holder → defer entirely. No promotions,
        // no MEMORY.md write, no DB writes.
        let ws = tmp_ws("probe-live");
        let mem = seed_db().await;
        let strong = mem.remember("kate", "user likes dark mode", &[]).await?;
        mem.record_recall_event("kate", strong, "dark", 1.0).await?;
        mem.record_recall_event("kate", strong, "dark mode", 1.0)
            .await?;
        mem.record_recall_event("kate", strong, "preferences", 1.0)
            .await?;
        let engine = mk_engine(&ws, DreamingConfig::default(), mem.clone())
            .with_consolidation_probe(MockProbe::new(true));
        let report = engine.run_sweep("kate").await?;
        assert!(report.deferred_for_fork, "must defer when probe says live");
        assert_eq!(report.candidates_considered, 0);
        assert_eq!(report.promoted.len(), 0);
        assert_eq!(report.skipped_already_promoted, 0);
        // No MEMORY.md should have been created.
        assert!(!ws.join("MEMORY.md").exists());
        // SQLite ledger must NOT have a promotion for this memory.
        assert!(!mem.is_promoted(strong).await.unwrap_or(true));
        tokio::fs::remove_dir_all(&ws).await.ok();
        Ok(())
    }
}