oxios-memory 1.1.0

Tiered agent memory — extracted from oxios-kernel per RFC-018
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
//! Compaction tree — 5-level memory compression hierarchy.
//!
//! Raw → Daily → Weekly → Monthly → Root
//!
//! Older memories are progressively compressed into higher-level summaries,
//! preserving key information while reducing storage and context size.
//!
//! ## Levels
//!
//! | Level   | Value | Threshold | Description                        |
//! |---------|-------|-----------|------------------------------------|
//! | Raw     | 0     | 200 lines | Uncompressed session data          |
//! | Daily   | 1     | 300 lines | Compressed from raw (per-day)      |
//! | Weekly  | 2     | 500 lines | Compressed from daily (per-week)   |
//! | Monthly | 3     | ∞         | Compressed from weekly (per-month) |
//! | Root    | 4     | ∞         | Top-level index entry              |

use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// CompactionLevel
// ---------------------------------------------------------------------------

/// Compaction level in the compression hierarchy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum CompactionLevel {
    /// Raw session data (uncompressed).
    Raw = 0,
    /// Daily summary (compressed from raw).
    Daily = 1,
    /// Weekly summary (compressed from weekly).
    Weekly = 2,
    /// Monthly summary (compressed from weekly).
    Monthly = 3,
    /// Root index entry (top-level summary).
    Root = 4,
}

impl CompactionLevel {
    /// Line count threshold for triggering compaction at this level.
    ///
    /// When content at this level exceeds the threshold, it becomes a
    /// candidate for promotion to the next level.
    pub fn threshold(&self) -> usize {
        match self {
            CompactionLevel::Raw => 200,
            CompactionLevel::Daily => 300,
            CompactionLevel::Weekly => 500,
            CompactionLevel::Monthly => usize::MAX,
            CompactionLevel::Root => usize::MAX,
        }
    }

    /// Storage subdirectory name for this level.
    pub fn dir_name(&self) -> &'static str {
        match self {
            CompactionLevel::Raw => "raw",
            CompactionLevel::Daily => "daily",
            CompactionLevel::Weekly => "weekly",
            CompactionLevel::Monthly => "monthly",
            CompactionLevel::Root => "root",
        }
    }

    /// All levels in ascending order.
    pub fn all() -> &'static [CompactionLevel] {
        &[
            CompactionLevel::Raw,
            CompactionLevel::Daily,
            CompactionLevel::Weekly,
            CompactionLevel::Monthly,
            CompactionLevel::Root,
        ]
    }

    /// Get the next higher compaction level.
    ///
    /// Returns `None` for `Root` (topmost level).
    pub fn next(&self) -> Option<CompactionLevel> {
        match self {
            CompactionLevel::Raw => Some(CompactionLevel::Daily),
            CompactionLevel::Daily => Some(CompactionLevel::Weekly),
            CompactionLevel::Weekly => Some(CompactionLevel::Monthly),
            CompactionLevel::Monthly => Some(CompactionLevel::Root),
            CompactionLevel::Root => None,
        }
    }

    /// Numeric value of this level.
    pub fn as_u8(&self) -> u8 {
        *self as u8
    }

    /// Try to convert a u8 to a CompactionLevel.
    pub fn from_u8(v: u8) -> Option<Self> {
        match v {
            0 => Some(CompactionLevel::Raw),
            1 => Some(CompactionLevel::Daily),
            2 => Some(CompactionLevel::Weekly),
            3 => Some(CompactionLevel::Monthly),
            4 => Some(CompactionLevel::Root),
            _ => None,
        }
    }

    /// Compression ratio for content being promoted *from* this level.
    ///
    /// The ratio indicates what fraction of lines to keep. Lower levels
    /// compress more aggressively because the source data is more verbose.
    fn compression_ratio(&self) -> f64 {
        match self {
            CompactionLevel::Raw => 0.30,     // Keep 30% of raw lines
            CompactionLevel::Daily => 0.40,   // Keep 40% of daily lines
            CompactionLevel::Weekly => 0.50,  // Keep 50% of weekly lines
            CompactionLevel::Monthly => 0.60, // Keep 60% of monthly lines
            CompactionLevel::Root => 1.0,     // Root is never compressed further
        }
    }

    /// Target number of summary lines when promoting content at this level.
    fn target_summary_lines(&self) -> usize {
        match self {
            CompactionLevel::Raw => 15,
            CompactionLevel::Daily => 20,
            CompactionLevel::Weekly => 10,
            CompactionLevel::Monthly => 5,
            CompactionLevel::Root => 0,
        }
    }
}

// ---------------------------------------------------------------------------
// CompactionTree
// ---------------------------------------------------------------------------

/// Compaction tree manager.
///
/// Manages the 5-level compression hierarchy. Dream calls `compact()` to
/// promote entries up the tree when they exceed size thresholds.
///
/// # Usage
///
/// ```ignore
/// let tree = CompactionTree::default_tree();
///
/// // Check if content should be promoted
/// if tree.should_promote("long content...", CompactionLevel::Raw) {
///     let compacted = tree.compact_to_level("long content...", CompactionLevel::Raw, CompactionLevel::Daily);
/// }
///
/// // Promote several entries at once
/// let entries = vec!["entry 1".to_string(), "entry 2".to_string()];
/// let daily_summary = tree.promote(&entries, CompactionLevel::Raw);
/// ```
pub struct CompactionTree {
    /// Line count threshold for triggering compaction.
    pub line_threshold: usize,
}

impl CompactionTree {
    /// Create a new compaction tree with the given line threshold.
    pub fn new(line_threshold: usize) -> Self {
        Self { line_threshold }
    }

    /// Create with default threshold (200 lines).
    pub fn default_tree() -> Self {
        Self::new(200)
    }

    /// Check if content should be compacted based on line count.
    ///
    /// Uses the tree's configured `line_threshold`.
    pub fn should_compact(&self, content: &str) -> bool {
        content.lines().count() >= self.line_threshold
    }

    /// Check if content at a given level should be promoted to the next level.
    ///
    /// Promotion is indicated when the content exceeds the threshold for
    /// its current compaction level.
    pub fn should_promote(&self, content: &str, current_level: CompactionLevel) -> bool {
        let line_count = content.lines().count();
        line_count >= current_level.threshold()
    }

    /// Compact content from one level to another.
    ///
    /// This applies rule-based compression appropriate for the target level.
    /// If `to` is not strictly higher than `from`, the content is returned
    /// unchanged.
    ///
    /// # Multi-level compaction
    ///
    /// When compacting across multiple levels (e.g., Raw → Weekly), the
    /// compression is applied progressively through each intermediate level
    /// for better quality.
    pub fn compact_to_level(
        &self,
        content: &str,
        from: CompactionLevel,
        to: CompactionLevel,
    ) -> String {
        if to.as_u8() <= from.as_u8() {
            // Cannot compact to same or lower level
            return content.to_string();
        }

        // Progressive compaction through each intermediate level
        let mut current_content = content.to_string();
        let mut current_level = from;

        while current_level < to {
            let next_level = match current_level.next() {
                Some(n) => n,
                None => break,
            };
            current_content = self.compact_single_level(&current_content, current_level);
            current_level = next_level;
        }

        current_content
    }

    /// Promote multiple entries from a given level into a single summary
    /// at the next level.
    ///
    /// This is used by the dream process to consolidate several entries
    /// (e.g., multiple daily summaries) into one higher-level summary
    /// (e.g., a weekly summary).
    ///
    /// # Returns
    ///
    /// A compacted summary string combining all entries.
    pub fn promote(&self, entries: &[String], level: CompactionLevel) -> String {
        if entries.is_empty() {
            return String::new();
        }

        if entries.len() == 1 {
            // Single entry: just compact it
            return self.compact_single_level(&entries[0], level);
        }

        // Combine entries with section markers
        let combined = entries.join("\n---\n");

        // Apply compaction for the source level
        let compacted = self.compact_single_level(&combined, level);

        // Add header indicating promotion
        let header = match level.next() {
            Some(next) => format!(
                "[{} summary from {} entries]",
                next.dir_name(),
                entries.len()
            ),
            None => format!("[Root summary from {} entries]", entries.len()),
        };

        format!("{header}\n{compacted}")
    }

    /// Simple rule-based compaction: preserve first/last sentences of each
    /// paragraph, discard middle.
    ///
    /// This is the fallback when LLM compaction is not available.
    pub fn rule_based_compact(&self, content: &str) -> String {
        self.compact_single_level(content, CompactionLevel::Raw)
    }

    // -----------------------------------------------------------------------
    // Internal helpers
    // -----------------------------------------------------------------------

    /// Compress content for promotion from a single level to the next.
    ///
    /// Uses a rule-based strategy:
    /// 1. If content is short enough, return as-is.
    /// 2. Extract key lines: section headers, first/last lines of sections.
    /// 3. Apply compression ratio to determine how many lines to keep.
    /// 4. Preserve structural markers (headers, separators).
    fn compact_single_level(&self, content: &str, from_level: CompactionLevel) -> String {
        let lines: Vec<&str> = content.lines().collect();

        // Short content: no compaction needed
        if lines.len() < 5 {
            return content.to_string();
        }

        let ratio = from_level.compression_ratio();
        let target = from_level.target_summary_lines();
        let keep_count = ((lines.len() as f64) * ratio) as usize;
        let keep_count = keep_count.max(target).min(lines.len());

        // If we'd keep everything, skip
        if keep_count >= lines.len() {
            return content.to_string();
        }

        // Strategy: keep structural lines + head + tail
        let mut kept_indices = std::collections::HashSet::new();

        // 1. Always preserve section markers (lines starting with # or ---)
        for (i, line) in lines.iter().enumerate() {
            let trimmed = line.trim();
            if trimmed.starts_with('#')
                || trimmed.starts_with("---")
                || trimmed.starts_with("===")
                || trimmed.starts_with('[')
                || trimmed.starts_with('*')
                || trimmed.is_empty()
            {
                kept_indices.insert(i);
            }
        }

        // 2. Preserve first N non-structural lines
        let head_count = (keep_count / 3).max(2);
        let mut head_taken = 0;
        for (i, _line) in lines.iter().enumerate() {
            if head_taken >= head_count {
                break;
            }
            if !kept_indices.contains(&i) {
                kept_indices.insert(i);
                head_taken += 1;
            }
        }

        // 3. Preserve last N non-structural lines
        let tail_count = (keep_count / 3).max(2);
        let mut tail_taken = 0;
        for (i, _line) in lines.iter().enumerate().rev() {
            if tail_taken >= tail_count {
                break;
            }
            if !kept_indices.contains(&i) {
                kept_indices.insert(i);
                tail_taken += 1;
            }
        }

        // 4. If still under target, sample middle lines at even intervals
        if kept_indices.len() < keep_count {
            let remaining = keep_count - kept_indices.len();
            let middle_lines: Vec<usize> = (0..lines.len())
                .filter(|i| !kept_indices.contains(i))
                .collect();

            if !middle_lines.is_empty() {
                let step = (middle_lines.len() as f64 / remaining as f64).max(1.0) as usize;
                for idx in (0..middle_lines.len()).step_by(step) {
                    if kept_indices.len() >= keep_count {
                        break;
                    }
                    kept_indices.insert(middle_lines[idx]);
                }
            }
        }

        // Build result in order
        let mut sorted_indices: Vec<usize> = kept_indices.into_iter().collect();
        sorted_indices.sort_unstable();

        let mut result = Vec::new();
        let mut last_idx = 0isize;
        for idx in sorted_indices {
            if last_idx >= 0 && (idx as isize) > last_idx + 1 {
                // Gap detected — add omission marker
                let omitted = idx - (last_idx as usize) - 1;
                result.push(format!("... ({omitted} lines omitted) ..."));
            }
            result.push(lines[idx].to_string());
            last_idx = idx as isize;
        }

        // Trailing omitted lines
        if (last_idx as usize) < lines.len() - 1 {
            let omitted = lines.len() - 1 - (last_idx as usize);
            result.push(format!("... ({omitted} lines omitted) ..."));
        }

        result.join("\n")
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_compaction_level_next() {
        assert_eq!(CompactionLevel::Raw.next(), Some(CompactionLevel::Daily));
        assert_eq!(CompactionLevel::Daily.next(), Some(CompactionLevel::Weekly));
        assert_eq!(
            CompactionLevel::Weekly.next(),
            Some(CompactionLevel::Monthly)
        );
        assert_eq!(CompactionLevel::Monthly.next(), Some(CompactionLevel::Root));
        assert_eq!(CompactionLevel::Root.next(), None);
    }

    #[test]
    fn test_compaction_level_u8_roundtrip() {
        for level in CompactionLevel::all() {
            assert_eq!(CompactionLevel::from_u8(level.as_u8()), Some(*level));
        }
        assert_eq!(CompactionLevel::from_u8(5), None);
    }

    #[test]
    fn test_compaction_level_thresholds() {
        assert_eq!(CompactionLevel::Raw.threshold(), 200);
        assert_eq!(CompactionLevel::Daily.threshold(), 300);
        assert_eq!(CompactionLevel::Weekly.threshold(), 500);
        assert_eq!(CompactionLevel::Monthly.threshold(), usize::MAX);
        assert_eq!(CompactionLevel::Root.threshold(), usize::MAX);
    }

    #[test]
    fn test_should_compact_short() {
        let tree = CompactionTree::new(10);
        let content = "line 1\nline 2\nline 3";
        assert!(!tree.should_compact(content));
    }

    #[test]
    fn test_should_compact_long() {
        let tree = CompactionTree::new(5);
        let content = (0..10)
            .map(|i| format!("line {}", i))
            .collect::<Vec<_>>()
            .join("\n");
        assert!(tree.should_compact(&content));
    }

    #[test]
    fn test_should_promote_at_threshold() {
        let tree = CompactionTree::default_tree();
        // Create content with 200+ lines (Raw threshold)
        let content: String = (0..200)
            .map(|i| format!("line {}", i))
            .collect::<Vec<_>>()
            .join("\n");
        assert!(tree.should_promote(&content, CompactionLevel::Raw));
    }

    #[test]
    fn test_should_not_promote_below_threshold() {
        let tree = CompactionTree::default_tree();
        let content = "line 1\nline 2\nline 3";
        assert!(!tree.should_promote(content, CompactionLevel::Raw));
    }

    #[test]
    fn test_rule_based_compact_short() {
        let tree = CompactionTree::new(10);
        let content = "line 1\nline 2\nline 3";
        let result = tree.rule_based_compact(content);
        assert_eq!(result, content);
    }

    #[test]
    fn test_rule_based_compact_long() {
        let tree = CompactionTree::new(10);
        let content = (0..50)
            .map(|i| format!("line {}", i))
            .collect::<Vec<_>>()
            .join("\n");
        let result = tree.rule_based_compact(&content);
        assert!(result.lines().count() < 50, "Should be compacted");
        assert!(result.contains("line 0"), "Should preserve first line");
        assert!(result.contains("line 49"), "Should preserve last line");
        assert!(
            result.contains("omitted"),
            "Should indicate omitted content"
        );
    }

    #[test]
    fn test_compact_to_level_same_level() {
        let tree = CompactionTree::default_tree();
        let content = "line 1\nline 2\nline 3\nline 4\nline 5";
        let result = tree.compact_to_level(content, CompactionLevel::Raw, CompactionLevel::Raw);
        assert_eq!(result, content, "Same level should return unchanged");
    }

    #[test]
    fn test_compact_to_level_lower_level() {
        let tree = CompactionTree::default_tree();
        let content = "line 1\nline 2\nline 3";
        let result = tree.compact_to_level(content, CompactionLevel::Daily, CompactionLevel::Raw);
        assert_eq!(
            result, content,
            "Compacting to lower level should return unchanged"
        );
    }

    #[test]
    fn test_compact_to_level_single_step() {
        let tree = CompactionTree::default_tree();
        let content: String = (0..50)
            .map(|i| format!("line {}", i))
            .collect::<Vec<_>>()
            .join("\n");

        let result = tree.compact_to_level(&content, CompactionLevel::Raw, CompactionLevel::Daily);
        assert!(
            result.lines().count() < content.lines().count(),
            "Should be compacted"
        );
        assert!(result.contains("line 0"), "Should preserve first line");
    }

    #[test]
    fn test_compact_to_level_multi_step() {
        let tree = CompactionTree::default_tree();
        let content: String = (0..100)
            .map(|i| format!("line {}", i))
            .collect::<Vec<_>>()
            .join("\n");

        // Raw → Weekly (2 steps: Raw→Daily→Weekly)
        let result = tree.compact_to_level(&content, CompactionLevel::Raw, CompactionLevel::Weekly);
        assert!(
            result.lines().count() < 100,
            "Multi-step compaction should reduce size"
        );
    }

    #[test]
    fn test_compact_preserves_headers() {
        let tree = CompactionTree::default_tree();
        let content = "# Header 1\n\
                       line 1\nline 2\nline 3\nline 4\nline 5\n\
                       # Header 2\n\
                       line 6\nline 7\nline 8\nline 9\nline 10";

        let result = tree.compact_single_level(content, CompactionLevel::Raw);
        assert!(result.contains("# Header 1"), "Should preserve headers");
        assert!(result.contains("# Header 2"), "Should preserve headers");
    }

    #[test]
    fn test_promote_multiple_entries() {
        let tree = CompactionTree::default_tree();
        let entries: Vec<String> = (0..3)
            .map(|i| {
                (0..20)
                    .map(|j| format!("entry {} line {}", i, j))
                    .collect::<Vec<_>>()
                    .join("\n")
            })
            .collect();

        let result = tree.promote(&entries, CompactionLevel::Raw);
        assert!(
            result.contains("[daily summary from 3 entries]"),
            "Should include promotion header"
        );
        // Result should be shorter than the sum of all entries
        let total_original: usize = entries.iter().map(|e| e.lines().count()).sum();
        assert!(
            result.lines().count() < total_original,
            "Promoted result should be shorter than combined originals"
        );
    }

    #[test]
    fn test_promote_single_entry() {
        let tree = CompactionTree::default_tree();
        let entries = vec![(0..20)
            .map(|i| format!("line {}", i))
            .collect::<Vec<_>>()
            .join("\n")];

        let result = tree.promote(&entries, CompactionLevel::Raw);
        // Single entry should just be compacted (no header)
        assert!(
            !result.contains("summary from"),
            "Single entry should not have multi-entry header"
        );
    }

    #[test]
    fn test_promote_empty() {
        let tree = CompactionTree::default_tree();
        let result = tree.promote(&[], CompactionLevel::Raw);
        assert!(result.is_empty(), "Empty input should return empty string");
    }

    #[test]
    fn test_compact_single_level_very_short() {
        let tree = CompactionTree::default_tree();
        let content = "line 1\nline 2";
        let result = tree.compact_single_level(content, CompactionLevel::Raw);
        assert_eq!(
            result, content,
            "Very short content should not be compacted"
        );
    }

    #[test]
    fn test_dir_name() {
        assert_eq!(CompactionLevel::Raw.dir_name(), "raw");
        assert_eq!(CompactionLevel::Daily.dir_name(), "daily");
        assert_eq!(CompactionLevel::Weekly.dir_name(), "weekly");
        assert_eq!(CompactionLevel::Monthly.dir_name(), "monthly");
        assert_eq!(CompactionLevel::Root.dir_name(), "root");
    }
}