selfware 0.6.4

Your personal AI workshop — software you own, software that lasts
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
//! Dream System - Background Memory Consolidation
//!
//! The Dream System consolidates and cleans up episodic memory between sessions,
//! preventing memory bloat and contradictions. It runs as a background process
//! (autoDream) that merges similar memories, resolves conflicts, and prunes stale data.
//!
//! # Three-Gate System
//!
//! Dreams only run when ALL three gates pass:
//! 1. Minimum 24 hours since last dream
//! 2. Minimum 5 sessions since last dream
//! 3. Consolidation lock available (prevents concurrent dreams)
//!
//! # Four-Phase Process
//!
//! 1. **Orient**: Load recent sessions, identify memory files to process
//! 2. **Gather**: Collect all memories from last 5 sessions
//! 3. **Consolidate**: Merge similar memories, resolve conflicts, delete contradictions
//! 4. **Prune & Index**: Cap MEMORY.md size, remove stale memories, re-index

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::{debug, info, warn};

/// Default maximum lines for MEMORY.md
const MAX_MEMORY_LINES: usize = 200;
/// Default maximum size for MEMORY.md in bytes (~25KB)
const MAX_MEMORY_SIZE: usize = 25 * 1024;
/// Default stale memory threshold in days
const STALE_MEMORY_DAYS: u64 = 90;
/// Default minimum hours between dreams
const MIN_HOURS_BETWEEN_DREAMS: u64 = 24;
/// Default minimum sessions between dreams
const MIN_SESSIONS_BETWEEN_DREAMS: usize = 5;

/// Dream trigger configuration (three-gate system)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DreamTrigger {
    /// Minimum hours since last dream (default: 24)
    pub min_hours_since_last: u64,
    /// Minimum sessions since last dream (default: 5)
    pub min_sessions_since_last: usize,
}

impl Default for DreamTrigger {
    fn default() -> Self {
        Self {
            min_hours_since_last: MIN_HOURS_BETWEEN_DREAMS,
            min_sessions_since_last: MIN_SESSIONS_BETWEEN_DREAMS,
        }
    }
}

impl DreamTrigger {
    /// Create a new dream trigger with default settings
    pub fn new() -> Self {
        Self::default()
    }

    /// Set minimum hours since last dream
    pub fn with_min_hours(mut self, hours: u64) -> Self {
        self.min_hours_since_last = hours;
        self
    }

    /// Set minimum sessions since last dream
    pub fn with_min_sessions(mut self, sessions: usize) -> Self {
        self.min_sessions_since_last = sessions;
        self
    }
}

/// Persisted dream state
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DreamState {
    /// Unix timestamp of last dream
    #[serde(default)]
    pub last_dream_timestamp: u64,
    /// Number of sessions since last dream
    #[serde(default)]
    pub sessions_since_last_dream: usize,
    /// Whether consolidation is currently running (lock)
    #[serde(skip)]
    pub consolidation_lock: bool,
    /// Total number of dreams run
    #[serde(default)]
    pub dream_count: usize,
    /// Lock file path for cross-process coordination
    #[serde(skip)]
    lock_file_path: Option<PathBuf>,
}

impl DreamState {
    /// Create a new dream state
    pub fn new() -> Self {
        Self::default()
    }

    /// Get the path to the dream state file
    fn state_file_path(base_dir: &Path) -> PathBuf {
        base_dir.join("dream_state.json")
    }

    /// Load dream state from disk
    pub fn load(base_dir: &Path) -> Result<Self> {
        let path = Self::state_file_path(base_dir);
        if !path.exists() {
            return Ok(Self::new());
        }

        let content = std::fs::read_to_string(&path)?;
        let mut state: DreamState = serde_json::from_str(&content)?;
        state.lock_file_path = Some(base_dir.join("dream.lock"));

        // Check if a lock file exists from a previous run
        if state
            .lock_file_path
            .as_ref()
            .map(|p| p.exists())
            .unwrap_or(false)
        {
            warn!("Found existing dream lock file - previous dream may have crashed");
            // Clear the stale lock
            let _ = std::fs::remove_file(state.lock_file_path.as_ref().unwrap());
        }

        Ok(state)
    }

    /// Save dream state to disk
    pub fn save(&self, base_dir: &Path) -> Result<()> {
        let path = Self::state_file_path(base_dir);
        std::fs::create_dir_all(base_dir)?;

        let json = serde_json::to_string_pretty(self)?;
        let temp_path = path.with_extension(format!("tmp.{}", std::process::id()));
        std::fs::write(&temp_path, json)?;
        std::fs::rename(&temp_path, &path)?;

        Ok(())
    }

    /// Acquire the consolidation lock (gate 3)
    ///
    /// Returns true if the lock was acquired, false if another dream is running
    pub fn acquire_consolidation_lock(&mut self) -> bool {
        if self.consolidation_lock {
            return false;
        }

        // Try to create lock file for cross-process coordination
        if let Some(ref lock_path) = self.lock_file_path {
            if lock_path.exists() {
                return false;
            }
            if let Err(e) = std::fs::write(lock_path, std::process::id().to_string()) {
                warn!("Failed to create dream lock file: {}", e);
                return false;
            }
        }

        self.consolidation_lock = true;
        true
    }

    /// Release the consolidation lock
    pub fn release_consolidation_lock(&mut self) {
        self.consolidation_lock = false;

        // Remove lock file
        if let Some(ref lock_path) = self.lock_file_path {
            let _ = std::fs::remove_file(lock_path);
        }
    }

    /// Record that a session has ended
    pub fn record_session_end(&mut self) {
        self.sessions_since_last_dream += 1;
    }

    /// Check if the state indicates a dream should run (without checking lock)
    pub fn should_run_dream_check_gates(&self, trigger: &DreamTrigger) -> bool {
        // Gate 1: Check minimum hours since last dream
        // If never run (timestamp is 0), we don't pass this gate
        if self.last_dream_timestamp == 0 {
            debug!("Dream gate 1 failed: no previous dream recorded");
            return false;
        }

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        let hours_since_last = (now - self.last_dream_timestamp) / 3600;

        if hours_since_last < trigger.min_hours_since_last {
            debug!(
                "Dream gate 1 failed: only {} hours since last dream (need {})",
                hours_since_last, trigger.min_hours_since_last
            );
            return false;
        }

        // Gate 2: Check minimum sessions since last dream
        if self.sessions_since_last_dream < trigger.min_sessions_since_last {
            debug!(
                "Dream gate 2 failed: only {} sessions since last dream (need {})",
                self.sessions_since_last_dream, trigger.min_sessions_since_last
            );
            return false;
        }

        true
    }

    /// Get hours until next dream based on trigger
    pub fn hours_until_next(&self, trigger: &DreamTrigger) -> u64 {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        let hours_since_last = (now - self.last_dream_timestamp) / 3600;

        trigger
            .min_hours_since_last
            .saturating_sub(hours_since_last)
    }

    /// Get sessions until next dream based on trigger
    pub fn sessions_until_next(&self, trigger: &DreamTrigger) -> usize {
        trigger
            .min_sessions_since_last
            .saturating_sub(self.sessions_since_last_dream)
    }
}

impl Drop for DreamState {
    fn drop(&mut self) {
        // Clean up lock on drop if we still hold it
        if self.consolidation_lock {
            self.release_consolidation_lock();
        }
    }
}

/// Check if a dream should run (all three gates)
///
/// Returns true only if all gates pass:
/// 1. Minimum hours since last dream
/// 2. Minimum sessions since last dream
/// 3. Consolidation lock available
pub fn should_run_dream(state: &mut DreamState, trigger: &DreamTrigger) -> bool {
    // Check gates 1 and 2
    if !state.should_run_dream_check_gates(trigger) {
        return false;
    }

    // Gate 3: Try to acquire lock
    if !state.acquire_consolidation_lock() {
        debug!("Dream gate 3 failed: consolidation already in progress");
        return false;
    }

    info!("All dream gates passed - dream should run");
    true
}

/// Represents a section in MEMORY.md
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MemorySection {
    Facts,
    Preferences,
    ArchitectureDecisions,
    Unknown(String),
}

impl MemorySection {
    fn from_header(header: &str) -> Self {
        match header.trim() {
            "Facts" | "Facts (consolidated)" => MemorySection::Facts,
            "Preferences" | "Preferences (user-defined)" => MemorySection::Preferences,
            "Architecture Decisions" => MemorySection::ArchitectureDecisions,
            other => MemorySection::Unknown(other.to_string()),
        }
    }

    fn header(&self) -> &str {
        match self {
            MemorySection::Facts => "## Facts (consolidated)",
            MemorySection::Preferences => "## Preferences (user-defined)",
            MemorySection::ArchitectureDecisions => "## Architecture Decisions",
            MemorySection::Unknown(s) => s.as_str(),
        }
    }
}

/// A single memory entry
#[derive(Debug, Clone)]
pub struct MemoryEntry {
    pub date: Option<String>,
    pub content: String,
    pub section: MemorySection,
    pub raw_line: String,
}

impl MemoryEntry {
    /// Parse a memory entry from a line
    pub fn parse(line: &str, current_section: &MemorySection) -> Option<Self> {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            return None;
        }

        // Remove bullet point prefix if present (- or *)
        let content_start = trimmed
            .strip_prefix("- ")
            .or_else(|| trimmed.strip_prefix("* "))
            .unwrap_or(trimmed);

        // Parse date if present: [YYYY-MM-DD] or [YYYY-MM-DD HH:MM]
        let (date, content) = if let Some(end_bracket) = content_start.find(']') {
            if content_start.starts_with('[') && end_bracket > 1 {
                let date_str = &content_start[1..end_bracket];
                let rest = content_start[end_bracket + 1..].trim().to_string();
                (Some(date_str.to_string()), rest)
            } else {
                (None, content_start.to_string())
            }
        } else {
            (None, content_start.to_string())
        };

        Some(Self {
            date,
            content,
            section: current_section.clone(),
            raw_line: line.to_string(),
        })
    }

    /// Check if this memory is stale (older than threshold days)
    pub fn is_stale(&self, threshold_days: u64) -> bool {
        let Some(ref date_str) = self.date else {
            return false; // Undated memories are never stale
        };

        let parsed_date = chrono::NaiveDate::parse_from_str(date_str, "%Y-%m-%d").or_else(|_| {
            // Try parsing with time component
            chrono::NaiveDateTime::parse_from_str(date_str, "%Y-%m-%d %H:%M").map(|dt| dt.date())
        });

        let Ok(memory_date) = parsed_date else {
            return false;
        };

        let today = chrono::Local::now().date_naive();
        let age_days = today.signed_duration_since(memory_date).num_days();

        age_days > threshold_days as i64
    }
}

/// Parsed MEMORY.md content
#[derive(Debug, Clone)]
pub struct MemoryStore {
    pub entries: Vec<MemoryEntry>,
    pub sections: HashMap<MemorySection, Vec<usize>>, // section -> entry indices
}

impl MemoryStore {
    /// Parse MEMORY.md content
    pub fn parse(content: &str) -> Self {
        let mut entries = Vec::new();
        let mut sections: HashMap<MemorySection, Vec<usize>> = HashMap::new();
        let mut current_section = MemorySection::Facts;

        for line in content.lines() {
            // Check for section headers
            if let Some(header) = line.strip_prefix("## ") {
                current_section = MemorySection::from_header(header);
                continue;
            }

            // Parse memory entries
            if let Some(entry) = MemoryEntry::parse(line, &current_section) {
                let idx = entries.len();
                entries.push(entry);
                sections
                    .entry(current_section.clone())
                    .or_default()
                    .push(idx);
            }
        }

        Self { entries, sections }
    }

    /// Format back to MEMORY.md format
    pub fn format(&self) -> String {
        let mut output = String::from("# Project Memory\n\n");

        // Output sections in order
        let section_order = [
            MemorySection::Facts,
            MemorySection::Preferences,
            MemorySection::ArchitectureDecisions,
        ];

        for section in &section_order {
            if let Some(indices) = self.sections.get(section) {
                if !indices.is_empty() {
                    output.push_str(section.header());
                    output.push('\n');
                    for &idx in indices {
                        if let Some(entry) = self.entries.get(idx) {
                            output.push_str(&entry.raw_line);
                            output.push('\n');
                        }
                    }
                    output.push('\n');
                }
            }
        }

        // Handle unknown sections
        for (section, indices) in &self.sections {
            if matches!(section, MemorySection::Unknown(_)) {
                output.push_str(section.header());
                output.push('\n');
                for &idx in indices {
                    if let Some(entry) = self.entries.get(idx) {
                        output.push_str(&entry.raw_line);
                        output.push('\n');
                    }
                }
                output.push('\n');
            }
        }

        output
    }

    /// Remove stale memories
    pub fn prune_stale(&mut self, threshold_days: u64) -> usize {
        let mut removed = 0;
        let mut new_entries = Vec::new();
        let mut old_to_new: HashMap<usize, usize> = HashMap::new();

        for (old_idx, entry) in self.entries.iter().enumerate() {
            if entry.is_stale(threshold_days) {
                removed += 1;
            } else {
                let new_idx = new_entries.len();
                new_entries.push(entry.clone());
                old_to_new.insert(old_idx, new_idx);
            }
        }

        // Update section indices
        for indices in self.sections.values_mut() {
            let new_indices: Vec<usize> = indices
                .iter()
                .filter_map(|&old_idx| old_to_new.get(&old_idx).copied())
                .collect();
            *indices = new_indices;
        }

        self.entries = new_entries;
        removed
    }

    /// Cap memory size by removing oldest entries
    pub fn cap_size(&mut self, max_lines: usize, max_bytes: usize) -> usize {
        let content = self.format();

        if content.lines().count() <= max_lines && content.len() <= max_bytes {
            return 0;
        }

        let mut removed = 0;
        let mut new_entries = Vec::new();
        let mut old_to_new: HashMap<usize, usize> = HashMap::new();

        // Keep entries that are not Facts (Preferences and Architecture are more important)
        let important_indices: Vec<usize> = self
            .entries
            .iter()
            .enumerate()
            .filter(|(_, e)| !matches!(e.section, MemorySection::Facts))
            .map(|(i, _)| i)
            .collect();

        for &idx in &important_indices {
            let new_idx = new_entries.len();
            old_to_new.insert(idx, new_idx);
            new_entries.push(self.entries[idx].clone());
        }

        // Add Facts entries until we hit the limit
        let fact_indices = self
            .sections
            .get(&MemorySection::Facts)
            .cloned()
            .unwrap_or_default();
        for idx in fact_indices {
            if !important_indices.contains(&idx) {
                let test_entries: Vec<MemoryEntry> = new_entries
                    .iter()
                    .chain(std::iter::once(&self.entries[idx]))
                    .cloned()
                    .collect();

                let test_store = MemoryStore {
                    entries: test_entries,
                    sections: HashMap::new(), // Simplified check
                };
                let test_content = test_store.format();

                if test_content.lines().count() > max_lines || test_content.len() > max_bytes {
                    removed += 1;
                } else {
                    let new_idx = new_entries.len();
                    old_to_new.insert(idx, new_idx);
                    new_entries.push(self.entries[idx].clone());
                }
            }
        }

        // Update section indices
        for indices in self.sections.values_mut() {
            let new_indices: Vec<usize> = indices
                .iter()
                .filter_map(|&old_idx| old_to_new.get(&old_idx).copied())
                .collect();
            *indices = new_indices;
        }

        self.entries = new_entries;
        removed
    }

    /// Get memory statistics
    pub fn stats(&self) -> MemoryStats {
        let total_entries = self.entries.len();
        let facts_count = self
            .sections
            .get(&MemorySection::Facts)
            .map(|v| v.len())
            .unwrap_or(0);
        let preferences_count = self
            .sections
            .get(&MemorySection::Preferences)
            .map(|v| v.len())
            .unwrap_or(0);
        let architecture_count = self
            .sections
            .get(&MemorySection::ArchitectureDecisions)
            .map(|v| v.len())
            .unwrap_or(0);

        let content = self.format();

        MemoryStats {
            total_entries,
            facts_count,
            preferences_count,
            architecture_count,
            total_lines: content.lines().count(),
            total_bytes: content.len(),
        }
    }
}

/// Memory statistics
#[derive(Debug, Clone, Copy)]
pub struct MemoryStats {
    pub total_entries: usize,
    pub facts_count: usize,
    pub preferences_count: usize,
    pub architecture_count: usize,
    pub total_lines: usize,
    pub total_bytes: usize,
}

/// Configuration for the dream process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DreamConfig {
    /// Maximum lines for MEMORY.md
    pub max_memory_lines: usize,
    /// Maximum bytes for MEMORY.md
    pub max_memory_size: usize,
    /// Days after which memories are considered stale
    pub stale_memory_days: u64,
    /// Base directory for dream state
    pub base_dir: PathBuf,
    /// Trigger configuration
    pub trigger: DreamTrigger,
}

impl Default for DreamConfig {
    fn default() -> Self {
        Self {
            max_memory_lines: MAX_MEMORY_LINES,
            max_memory_size: MAX_MEMORY_SIZE,
            stale_memory_days: STALE_MEMORY_DAYS,
            base_dir: Self::default_base_dir(),
            trigger: DreamTrigger::default(),
        }
    }
}

impl DreamConfig {
    /// Get the default base directory (~/.selfware/memory)
    fn default_base_dir() -> PathBuf {
        dirs::home_dir()
            .map(|h| h.join(".selfware").join("memory"))
            .unwrap_or_else(|| PathBuf::from(".selfware").join("memory"))
    }

    /// Create a new dream config
    pub fn new() -> Self {
        Self::default()
    }

    /// Set custom base directory
    pub fn with_base_dir(mut self, path: impl Into<PathBuf>) -> Self {
        self.base_dir = path.into();
        self
    }

    /// Get the path to MEMORY.md for a project
    pub fn memory_file_path(&self, project_key: &str) -> PathBuf {
        self.base_dir.join(format!("{}_MEMORY.md", project_key))
    }

    /// Get the path to dream state
    pub fn state_path(&self) -> PathBuf {
        self.base_dir.clone()
    }
}

/// The four phases of the dream process
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DreamPhase {
    Orient,
    Gather,
    Consolidate,
    PruneAndIndex,
}

impl std::fmt::Display for DreamPhase {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DreamPhase::Orient => write!(f, "Orient"),
            DreamPhase::Gather => write!(f, "Gather Recent Signal"),
            DreamPhase::Consolidate => write!(f, "Consolidate"),
            DreamPhase::PruneAndIndex => write!(f, "Prune & Index"),
        }
    }
}

/// Result of a dream run
#[derive(Debug, Clone)]
pub struct DreamResult {
    pub success: bool,
    pub phases_completed: Vec<DreamPhase>,
    pub memories_consolidated: usize,
    pub memories_pruned: usize,
    pub errors: Vec<String>,
    pub duration_secs: u64,
}

impl DreamResult {
    /// Create a successful result
    pub fn success(phases: Vec<DreamPhase>) -> Self {
        Self {
            success: true,
            phases_completed: phases,
            memories_consolidated: 0,
            memories_pruned: 0,
            errors: Vec::new(),
            duration_secs: 0,
        }
    }

    /// Create a failed result
    pub fn failure(error: impl Into<String>) -> Self {
        Self {
            success: false,
            phases_completed: Vec::new(),
            memories_consolidated: 0,
            memories_pruned: 0,
            errors: vec![error.into()],
            duration_secs: 0,
        }
    }

    /// Add a completed phase
    pub fn with_phase(mut self, phase: DreamPhase) -> Self {
        self.phases_completed.push(phase);
        self
    }

    /// Set consolidation count
    pub fn with_consolidated(mut self, count: usize) -> Self {
        self.memories_consolidated = count;
        self
    }

    /// Set prune count
    pub fn with_pruned(mut self, count: usize) -> Self {
        self.memories_pruned = count;
        self
    }
}

/// Generate LLM prompt for memory consolidation
pub fn generate_consolidation_prompt(memories: &[MemoryEntry]) -> String {
    let memory_text = memories
        .iter()
        .map(|m| {
            if let Some(ref date) = m.date {
                format!("- [{}] {}", date, m.content)
            } else {
                format!("- {}", m.content)
            }
        })
        .collect::<Vec<_>>()
        .join("\n");

    format!(
        r#"You are a memory consolidation assistant. Your task is to:

1. Merge similar memories into single, more comprehensive entries
2. Convert relative dates to absolute dates (YYYY-MM-DD format)
3. Delete or flag contradicted facts (keep the most recent)
4. Resolve conflicts by keeping the most specific and accurate information

Input memories:
{}

Consolidation rules:
- Group related facts together
- Remove duplicates
- Update outdated information
- Keep user preferences intact
- Preserve architecture decisions

Output format:
## Facts (consolidated)
- [YYYY-MM-DD] Consolidated fact here
- [YYYY-MM-DD] Another consolidated fact

## Preferences (user-defined)
- Preference 1
- Preference 2

## Architecture Decisions
- Decision 1 with rationale

Only output the consolidated memory content, no explanations."#,
        memory_text
    )
}

/// Dream status for UI display
#[derive(Debug, Clone)]
pub struct DreamStatus {
    pub last_dream_timestamp: Option<u64>,
    pub sessions_since_last_dream: usize,
    pub dream_count: usize,
    pub hours_until_next: u64,
    pub sessions_until_next: usize,
    pub is_running: bool,
}

#[cfg(test)]
#[path = "../../tests/unit/cognitive/dream/dream_test.rs"]
mod tests;