vipune 0.3.0

A minimal memory layer for AI agents
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
//! Temporal decay scoring for search result recency weighting.

use chrono::{DateTime, Utc};

#[cfg(test)]
use chrono::Duration; // Only import in tests

/// Decay function type.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DecayFunction {
    /// Exponential decay: e^(-λ × age_seconds)
    Exponential,
    /// Linear decay: 1 - λ × age_days (scaled to [0,1])
    /// Implemented and thoroughly tested. Currently unused in production paths,
    /// but available for future use or external library consumers.
    #[cfg_attr(not(test), allow(dead_code))]
    Linear,
}

/// Configuration for temporal decay calculation.
#[derive(Debug, Clone, Copy)]
pub struct DecayConfig {
    /// Decay function to use.
    pub function: DecayFunction,
    /// Decay rate.
    ///
    /// **IMPORTANT:** Lambda ranges are function-specific:
    /// - Exponential: λ in per-second (1e-10 to 1e-3, default: 1e-6 ~50% decay at 8 days)
    /// - Linear: λ in per-day (1e-6 to 100.0)
    ///
    /// **WARNING:** If you change `function` from Exponential to Linear, you **must** also adjust `lambda`.
    /// Default lambda=1e-6 is appropriate for Exponential but produces negligible decay for Linear.
    /// For Linear decay, use lambda≥0.01 (1% decay per day minimum).
    pub lambda: f64,
    /// Grace period with no decay in days (default: 0.0).
    pub offset_days: f64,
}

impl Default for DecayConfig {
    fn default() -> Self {
        Self {
            function: DecayFunction::Exponential,
            lambda: 1e-6,
            offset_days: 0.0,
        }
    }
}

impl DecayFunction {
    /// Get all available decay functions.
    ///
    /// Returns an iterator over all decay function variants.
    /// This method documents all available decay function types.
    #[cfg(test)]
    pub fn all() -> impl Iterator<Item = Self> {
        [DecayFunction::Exponential, DecayFunction::Linear].into_iter()
    }
}

impl DecayConfig {
    /// Validate decay configuration parameters.
    ///
    /// Returns error if parameters are mathematically invalid (e.g., negative lambda).
    pub fn new() -> Result<Self, String> {
        let config = Self::default();
        config.validate()?;
        Ok(config)
    }

    /// Validate decay configuration parameters.
    fn validate(&self) -> Result<(), String> {
        if self.lambda <= 0.0 {
            return Err(format!(
                "Invalid lambda: {} (must be positive)",
                self.lambda
            ));
        }

        // Function-specific validation
        match self.function {
            DecayFunction::Exponential => {
                if self.lambda > 1e-3 {
                    return Err(format!(
                        "Exponential decay lambda {} is too large (max: 1e-3)",
                        self.lambda
                    ));
                }
                if self.lambda < 1e-10 {
                    return Err(format!(
                        "Exponential decay lambda {} is too small (min: 1e-10)",
                        self.lambda
                    ));
                }
            }
            DecayFunction::Linear => {
                if self.lambda > 100.0 {
                    return Err(format!(
                        "Linear decay lambda {} is too large (max: 100.0)",
                        self.lambda
                    ));
                }
                if self.lambda < 1e-6 {
                    return Err(format!(
                        "Linear decay lambda {} is too small to be useful (min: 1e-6)",
                        self.lambda
                    ));
                }
            }
        }

        if self.offset_days < 0.0 {
            return Err(format!(
                "Invalid offset_days: {} (must be >= 0)",
                self.offset_days
            ));
        }
        Ok(())
    }

    /// Calculate decay factor for a memory created at `created_at`.
    ///
    /// Returns 1.0 for brand new, approaches 0.0 for very old.
    ///
    /// # Invariant
    ///
    /// This method assumes the configuration is valid. Validity is guaranteed by
    /// `DecayConfig::new()` which validates all parameters at construction time.
    /// Direct struct construction (only used in tests) bypassing validation may
    /// produce mathematically incorrect results.
    pub fn calculate_decay(&self, created_at: &DateTime<Utc>) -> f64 {
        let now = Utc::now();
        let age = now.signed_duration_since(*created_at);
        let age_seconds = age.num_seconds().max(0) as f64;

        // Guard against extreme values (should not occur with i64 age)
        if age_seconds.is_nan() || age_seconds.is_infinite() {
            return 0.0;
        }

        // Apply offset (grace period)
        let offset_seconds = self.offset_days * 86400.0;
        let effective_age = (age_seconds - offset_seconds).max(0.0);

        match self.function {
            DecayFunction::Exponential => {
                let exponent = -self.lambda * effective_age;
                // Guard against underflow/overflow in exp()
                if exponent < -700.0 {
                    return 0.0;
                }
                if exponent > 700.0 {
                    return 1.0;
                }
                exponent.exp()
            }
            DecayFunction::Linear => {
                let decay_rate = self.lambda * effective_age / 86400.0;
                (1.0 - decay_rate).clamp(0.0, 1.0)
            }
        }
    }
}

/// Apply recency weighting to search results.
///
/// Formula: final_score = (1 - α) × similarity + α × decay
///
/// # Arguments
///
/// * `similarity` - Original semantic similarity score
/// * `created_at` - Timestamp when the memory was created
/// * `recency_weight` - Weight parameter α (0.0 to 1.0)
/// * `config` - Decay configuration
///
/// # Returns
///
/// Combined score incorporating both semantic similarity and temporal decay.
pub fn apply_recency_weight(
    similarity: f64,
    created_at: &DateTime<Utc>,
    recency_weight: f64,
    config: &DecayConfig,
) -> f64 {
    if recency_weight <= 0.0 {
        return similarity;
    }
    let decay = config.calculate_decay(created_at);
    (1.0 - recency_weight) * similarity + recency_weight * decay
}

/// Validate recency weight is in valid range [0.0, 1.0].
pub fn validate_recency_weight(recency_weight: f64) -> Result<(), String> {
    if !(0.0..=1.0).contains(&recency_weight) {
        return Err(format!(
            "Invalid recency weight: {} (must be between 0.0 and 1.0)",
            recency_weight
        ));
    }
    Ok(())
}

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

    #[test]
    fn test_exponential_decay_brand_new() {
        let config = DecayConfig::default();
        let now = Utc::now();
        let decay = config.calculate_decay(&now);
        assert!(
            (decay - 1.0).abs() < 1e-10,
            "Brand new should have decay ≈ 1.0"
        );
    }

    #[test]
    fn test_exponential_decay_8_days() {
        let config = DecayConfig::default();
        let created_at = Utc::now() - Duration::days(8);
        let decay = config.calculate_decay(&created_at);
        // With lambda=1e-6, 8 days = 8 * 86400 seconds = 691200
        // e^(-1e-6 * 691200) = e^(-0.6912) ≈ 0.50
        assert!(
            (decay - 0.5).abs() < 0.1,
            "8 days should have ~50% decay, got {}",
            decay
        );
    }

    #[test]
    fn test_exponential_decay_very_old() {
        let config = DecayConfig::default();
        let created_at = Utc::now() - Duration::days(365);
        let decay = config.calculate_decay(&created_at);
        // 1 year = 365 * 86400 seconds, should be very close to 0
        assert!(
            decay < 0.1,
            "1 year old should approach 0 decay, got {}",
            decay
        );
    }

    #[test]
    fn test_decay_with_offset() {
        let config = DecayConfig {
            function: DecayFunction::Exponential,
            lambda: 1e-6,
            offset_days: 7.0,
        };
        let created_at = Utc::now() - Duration::days(3);
        let decay = config.calculate_decay(&created_at);
        // Within offset period, should be 1.0
        assert!(
            (decay - 1.0).abs() < 1e-10,
            "Within offset should have no decay"
        );
    }

    #[test]
    fn test_decay_after_offset() {
        let config = DecayConfig {
            function: DecayFunction::Exponential,
            lambda: 1e-6,
            offset_days: 7.0,
        };
        let created_at = Utc::now() - Duration::days(15);
        let decay = config.calculate_decay(&created_at);
        // 15 days - 7 days offset = 8 days effective age
        // Should have ~50% decay from effective age
        assert!(
            (decay - 0.5).abs() < 0.1,
            "After offset should decay from effective age, got {}",
            decay
        );
    }

    #[test]
    fn test_apply_recency_weight_zero() {
        let config = DecayConfig::default();
        let now = Utc::now();
        let result = apply_recency_weight(0.9, &now, 0.0, &config);
        assert!(
            (result - 0.9).abs() < 1e-10,
            "α=0 should return pure similarity"
        );
    }

    #[test]
    fn test_apply_recency_weight_one() {
        let config = DecayConfig::default();
        let now = Utc::now();
        let result = apply_recency_weight(0.9, &now, 1.0, &config);
        assert!(
            (result - 1.0).abs() < 1e-10,
            "α=1 with brand new should return decay=1.0"
        );
    }

    #[test]
    fn test_apply_recency_weight_half() {
        let config = DecayConfig::default();
        let now = Utc::now();
        let similarity = 0.8;
        let result = apply_recency_weight(similarity, &now, 0.5, &config);
        // 0.5 * 0.8 + 0.5 * 1.0 = 0.9
        assert!(
            (result - 0.9).abs() < 1e-10,
            "α=0.5 should average similarity and decay"
        );
    }

    #[test]
    fn test_recency_weight_negative_clamped() {
        let config = DecayConfig::default();
        let now = Utc::now();
        let result = apply_recency_weight(0.9, &now, -0.5, &config);
        assert!(
            (result - 0.9).abs() < 1e-10,
            "Negative recency weight should behave like 0.0"
        );
    }

    #[test]
    fn test_validate_recency_weight_valid() {
        assert!(validate_recency_weight(0.0).is_ok());
        assert!(validate_recency_weight(0.5).is_ok());
        assert!(validate_recency_weight(1.0).is_ok());
    }

    #[test]
    fn test_validate_recency_weight_negative() {
        let result = validate_recency_weight(-0.1);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("must be between 0.0 and 1.0"));
    }

    #[test]
    fn test_validate_recency_weight_exceeds_one() {
        let result = validate_recency_weight(1.1);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("must be between 0.0 and 1.0"));
    }

    #[test]
    fn test_decay_config_default() {
        let config = DecayConfig::default();
        assert!(matches!(config.function, DecayFunction::Exponential));
        assert_eq!(config.lambda, 1e-6);
        assert_eq!(config.offset_days, 0.0);

        // Also verify Linear variant exists
        let linear_config = DecayConfig {
            function: DecayFunction::Linear,
            lambda: 1.0 / 86400.0,
            offset_days: 0.0,
        };
        assert!(matches!(linear_config.function, DecayFunction::Linear));
    }

    #[test]
    fn test_decay_config_new_valid() {
        let result = DecayConfig::new();
        assert!(result.is_ok());
        let config = result.unwrap();
        assert_eq!(config.lambda, 1e-6);
    }

    #[test]
    fn test_decay_config_validate_negative_lambda() {
        let config = DecayConfig {
            function: DecayFunction::Exponential,
            lambda: -1e-6,
            offset_days: 0.0,
        };
        let result = config.validate();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("must be positive"));
    }

    #[test]
    fn test_decay_config_validate_zero_lambda() {
        let config = DecayConfig {
            function: DecayFunction::Exponential,
            lambda: 0.0,
            offset_days: 0.0,
        };
        let result = config.validate();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("must be positive"));
    }

    #[test]
    fn test_decay_config_validate_large_lambda() {
        let config = DecayConfig {
            function: DecayFunction::Exponential,
            lambda: 1e-2,
            offset_days: 0.0,
        };
        let result = config.validate();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("too large"));
    }

    #[test]
    fn test_decay_config_validate_negative_offset() {
        let config = DecayConfig {
            function: DecayFunction::Exponential,
            lambda: 1e-6,
            offset_days: -7.0,
        };
        let result = config.validate();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("must be >= 0"));
    }

    #[test]
    fn test_decay_config_validate_valid_offset() {
        let config = DecayConfig {
            function: DecayFunction::Exponential,
            lambda: 1e-6,
            offset_days: 7.0,
        };
        let result = config.validate();
        assert!(result.is_ok());
    }

    #[test]
    fn test_apply_recency_weight_with_old_memory() {
        let config = DecayConfig::default();
        let old_date = Utc::now() - Duration::days(365);
        let similarity = 0.9;
        let result = apply_recency_weight(similarity, &old_date, 0.5, &config);
        // Old memory has decay close to 0, so result should be ~0.45
        assert!(
            result < 0.6,
            "Old memory should be penalized, got {}",
            result
        );
        assert!(result > 0.3, "But still has some similarity contribution");
    }

    #[test]
    fn test_linear_decay_brand_new() {
        let config = DecayConfig {
            function: DecayFunction::Linear,
            lambda: 1.0 / 86400.0, //decay 1 per day
            offset_days: 0.0,
        };
        let now = Utc::now();
        let decay = config.calculate_decay(&now);
        assert!(
            (decay - 1.0).abs() < 1e-10,
            "Brand new should have decay ≈ 1.0"
        );
    }

    #[test]
    fn test_linear_decay_half_day() {
        let config = DecayConfig {
            function: DecayFunction::Linear,
            lambda: 1.0, // decay 1 per day
            offset_days: 0.0,
        };
        let created_at = Utc::now() - Duration::seconds(43200); // 12 hours
        let decay = config.calculate_decay(&created_at);
        // 12 hours = 0.5 days, decay = 1 - 1 * 0.5 = 0.5
        assert!(
            (decay - 0.5).abs() < 1e-10,
            "12 hours should have 50% decay, got {}",
            decay
        );
    }

    #[test]
    fn test_linear_decay_full_day() {
        let config = DecayConfig {
            function: DecayFunction::Linear,
            lambda: 1.0, // decay 1 per day
            offset_days: 0.0,
        };
        let created_at = Utc::now() - Duration::days(1);
        let decay = config.calculate_decay(&created_at);
        // 1 day, decay = 1 - 1 * 1 = 0
        assert!(
            (decay - 0.0).abs() < 1e-10,
            "1 day should have 0% decay, got {}",
            decay
        );
    }

    #[test]
    fn test_linear_decay_clamped() {
        let config = DecayConfig {
            function: DecayFunction::Linear,
            lambda: 1.0, // decay 1 per day
            offset_days: 0.0,
        };
        let created_at = Utc::now() - Duration::days(5);
        let decay = config.calculate_decay(&created_at);
        // 5 days, decay would be 1 - 5 = -4, but clamped to 0
        assert!(
            (decay - 0.0).abs() < 1e-10,
            "5 days should be clamped to 0 decay, got {}",
            decay
        );
    }

    #[test]
    fn test_linear_decay_with_offset() {
        let config = DecayConfig {
            function: DecayFunction::Linear,
            lambda: 1.0,      // decay 1 per day
            offset_days: 7.0, // no decay for 7 days
        };
        let created_at = Utc::now() - Duration::days(3);
        let decay = config.calculate_decay(&created_at);
        // Within offset period, should be 1.0
        assert!(
            (decay - 1.0).abs() < 1e-10,
            "Within offset should have no decay"
        );
    }

    #[test]
    fn test_linear_decay_after_offset() {
        let config = DecayConfig {
            function: DecayFunction::Linear,
            lambda: 1.0, // decay 1 per day
            offset_days: 7.0,
        };
        let created_at = Utc::now() - Duration::days(10); // 10 days total
        let decay = config.calculate_decay(&created_at);
        // 10 days - 7 days offset = 3 days effective age
        // decay = 1 - 1 * 3 = -2, clamped to 0
        assert!(
            (decay - 0.0).abs() < 1e-10,
            "After offset with excessive age should clamp to 0"
        );
    }

    #[test]
    fn test_decay_function_all() {
        let functions: Vec<_> = DecayFunction::all().collect();
        assert_eq!(functions.len(), 2);
        assert!(functions.contains(&DecayFunction::Exponential));
        assert!(functions.contains(&DecayFunction::Linear));
    }

    #[test]
    fn test_linear_decay_validation_too_small_lambda() {
        let config = DecayConfig {
            function: DecayFunction::Linear,
            lambda: 1e-7, // Too small for Linear
            offset_days: 0.0,
        };
        let result = config.validate();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("too small to be useful"));
    }

    #[test]
    fn test_linear_decay_validation_too_large_lambda() {
        let config = DecayConfig {
            function: DecayFunction::Linear,
            lambda: 200.0, // Too large for Linear
            offset_days: 0.0,
        };
        let result = config.validate();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("too large"));
    }

    #[test]
    fn test_linear_decay_validation_valid_min() {
        let config = DecayConfig {
            function: DecayFunction::Linear,
            lambda: 1e-6, // Valid minimum
            offset_days: 0.0,
        };
        let result = config.validate();
        assert!(result.is_ok(), "Linear lambda 1e-6 should be valid");
    }

    #[test]
    fn test_linear_decay_validation_valid_max() {
        let config = DecayConfig {
            function: DecayFunction::Linear,
            lambda: 100.0, // Valid maximum
            offset_days: 0.0,
        };
        let result = config.validate();
        assert!(result.is_ok(), "Linear lambda 100.0 should be valid");
    }

    #[test]
    fn test_linear_decay_actually_decays() {
        let config = DecayConfig {
            function: DecayFunction::Linear,
            lambda: 1.0, // 1 per day (reasonable value)
            offset_days: 0.0,
        };
        let now = Utc::now();
        let decay_now = config.calculate_decay(&now);
        let decay_half_day = config.calculate_decay(&(now - Duration::seconds(43200)));
        let decay_one_day = config.calculate_decay(&(now - Duration::days(1)));

        assert!(
            decay_now > decay_half_day,
            "Linear decay should decrease over time"
        );
        assert!(
            decay_half_day > decay_one_day,
            "Linear decay should decrease over time"
        );
        assert!(
            (decay_now - 1.0).abs() < 1e-10 && (decay_half_day - 0.5).abs() < 1e-1,
            "Linear decay with lambda=1.0 should produce meaningful values"
        );
    }
}