umi-memory 0.1.0

Memory library for AI agents with deterministic simulation testing
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
//! Property-Based Testing for DST
//!
//! TigerStyle: Random operation sequences with invariant checking.
//!
//! # Philosophy
//!
//! Property-based testing generates random operations and verifies that
//! invariants hold after each operation. Combined with DST, this gives:
//! - Deterministic reproduction via seed
//! - Time control via SimClock
//! - Fault injection via FaultInjector
//!
//! # Example
//!
//! ```rust,ignore
//! use umi_memory::dst::{PropertyTest, PropertyTestable, SimClock, DeterministicRng};
//!
//! struct Counter { value: i64, min: i64, max: i64 }
//!
//! #[derive(Debug, Clone)]
//! enum CounterOp { Increment(i64), Decrement(i64), Reset }
//!
//! impl PropertyTestable for Counter {
//!     type Operation = CounterOp;
//!
//!     fn generate_operation(&self, rng: &mut DeterministicRng) -> Self::Operation {
//!         match rng.next_usize(0, 3) {
//!             0 => CounterOp::Increment(rng.next_usize(1, 10) as i64),
//!             1 => CounterOp::Decrement(rng.next_usize(1, 10) as i64),
//!             _ => CounterOp::Reset,
//!         }
//!     }
//!
//!     fn apply_operation(&mut self, op: &Self::Operation, _clock: &SimClock) {
//!         match op {
//!             CounterOp::Increment(n) => self.value = (self.value + n).min(self.max),
//!             CounterOp::Decrement(n) => self.value = (self.value - n).max(self.min),
//!             CounterOp::Reset => self.value = 0,
//!         }
//!     }
//!
//!     fn check_invariants(&self) -> Result<(), String> {
//!         if self.value < self.min {
//!             return Err(format!("value {} below min {}", self.value, self.min));
//!         }
//!         if self.value > self.max {
//!             return Err(format!("value {} above max {}", self.value, self.max));
//!         }
//!         Ok(())
//!     }
//! }
//!
//! #[test]
//! fn test_counter_properties() {
//!     let counter = Counter { value: 0, min: -100, max: 100 };
//!     let result = PropertyTest::new(42)
//!         .with_max_operations(1000)
//!         .run(counter);
//!     assert!(result.is_success());
//! }
//! ```

use std::fmt::Debug;

use super::clock::SimClock;
use super::rng::DeterministicRng;
use crate::constants::DST_SIMULATION_STEPS_MAX;

/// Trait for systems that can be property-tested.
///
/// TigerStyle: Explicit operation generation and invariant checking.
pub trait PropertyTestable {
    /// The type of operations that can be performed.
    type Operation: Debug + Clone;

    /// Generate a random operation based on current state.
    ///
    /// The operation should be valid for the current state.
    fn generate_operation(&self, rng: &mut DeterministicRng) -> Self::Operation;

    /// Apply an operation to the state.
    ///
    /// May use the clock for time-dependent operations.
    fn apply_operation(&mut self, op: &Self::Operation, clock: &SimClock);

    /// Check that all invariants hold.
    ///
    /// Returns Ok(()) if all invariants pass, Err(message) otherwise.
    fn check_invariants(&self) -> Result<(), String>;

    /// Optional: Describe the current state for debugging.
    fn describe_state(&self) -> String {
        String::from("(state description not implemented)")
    }
}

/// Result of a property test run.
#[derive(Debug)]
pub struct PropertyTestResult {
    /// Number of operations successfully executed
    pub operations_executed: u64,
    /// Seed used for reproduction
    pub seed: u64,
    /// Failure details, if any
    pub failure: Option<PropertyTestFailure>,
}

impl PropertyTestResult {
    /// Check if the test passed.
    #[must_use]
    pub fn is_success(&self) -> bool {
        self.failure.is_none()
    }

    /// Check if the test failed.
    #[must_use]
    pub fn is_failure(&self) -> bool {
        self.failure.is_some()
    }

    /// Unwrap the result, panicking with details if failed.
    ///
    /// # Panics
    /// Panics if the test failed, with reproduction info.
    pub fn unwrap(self) {
        if let Some(failure) = self.failure {
            panic!(
                "Property test failed!\n\
                 Seed: {} (use this to reproduce)\n\
                 Operation #{}: {:?}\n\
                 Invariant violation: {}\n\
                 State: {}",
                self.seed,
                failure.operation_index,
                failure.operation,
                failure.message,
                failure.state_description
            );
        }
    }
}

/// Details of a property test failure.
#[derive(Debug)]
pub struct PropertyTestFailure {
    /// Index of the failing operation (0-based)
    pub operation_index: u64,
    /// The operation that caused the failure
    pub operation: String,
    /// The invariant violation message
    pub message: String,
    /// Description of the state at failure
    pub state_description: String,
}

/// Configuration for time advancement during property tests.
#[derive(Debug, Clone)]
pub struct TimeAdvanceConfig {
    /// Minimum time to advance per operation (ms)
    pub min_ms: u64,
    /// Maximum time to advance per operation (ms)
    pub max_ms: u64,
    /// Probability of advancing time (0.0 to 1.0)
    pub probability: f64,
}

impl Default for TimeAdvanceConfig {
    fn default() -> Self {
        Self {
            min_ms: 0,
            max_ms: 1000,
            probability: 0.5,
        }
    }
}

impl TimeAdvanceConfig {
    /// No time advancement.
    #[must_use]
    pub fn none() -> Self {
        Self {
            min_ms: 0,
            max_ms: 0,
            probability: 0.0,
        }
    }

    /// Always advance by fixed amount.
    #[must_use]
    pub fn fixed(ms: u64) -> Self {
        Self {
            min_ms: ms,
            max_ms: ms,
            probability: 1.0,
        }
    }

    /// Advance with given range and probability.
    #[must_use]
    pub fn random(min_ms: u64, max_ms: u64, probability: f64) -> Self {
        assert!(probability >= 0.0 && probability <= 1.0);
        assert!(min_ms <= max_ms);
        Self {
            min_ms,
            max_ms,
            probability,
        }
    }
}

/// Property-based test runner.
///
/// TigerStyle:
/// - Deterministic via seed
/// - Explicit operation count limits
/// - Invariant checking after each operation
/// - Time advancement control
#[derive(Debug)]
pub struct PropertyTest {
    seed: u64,
    max_operations: u64,
    time_config: TimeAdvanceConfig,
    check_invariants_before: bool,
}

impl PropertyTest {
    /// Create a new property test with the given seed.
    ///
    /// # Panics
    /// Panics if max_operations would exceed DST_SIMULATION_STEPS_MAX.
    #[must_use]
    pub fn new(seed: u64) -> Self {
        Self {
            seed,
            max_operations: 100, // Sensible default
            time_config: TimeAdvanceConfig::default(),
            check_invariants_before: true,
        }
    }

    /// Set the maximum number of operations to run.
    ///
    /// # Panics
    /// Panics if max exceeds DST_SIMULATION_STEPS_MAX.
    #[must_use]
    pub fn with_max_operations(mut self, max: u64) -> Self {
        assert!(
            max <= DST_SIMULATION_STEPS_MAX,
            "max_operations {} exceeds DST_SIMULATION_STEPS_MAX {}",
            max,
            DST_SIMULATION_STEPS_MAX
        );
        self.max_operations = max;
        self
    }

    /// Configure time advancement between operations.
    #[must_use]
    pub fn with_time_advance(mut self, config: TimeAdvanceConfig) -> Self {
        self.time_config = config;
        self
    }

    /// Disable checking invariants before the first operation.
    #[must_use]
    pub fn skip_initial_invariant_check(mut self) -> Self {
        self.check_invariants_before = false;
        self
    }

    /// Run the property test.
    ///
    /// Generates random operations, applies them, and checks invariants
    /// after each operation. Returns detailed results.
    #[must_use]
    pub fn run<T: PropertyTestable>(self, mut state: T) -> PropertyTestResult {
        let mut rng = DeterministicRng::new(self.seed);
        let clock = SimClock::new();

        // Check initial invariants
        if self.check_invariants_before {
            if let Err(msg) = state.check_invariants() {
                return PropertyTestResult {
                    operations_executed: 0,
                    seed: self.seed,
                    failure: Some(PropertyTestFailure {
                        operation_index: 0,
                        operation: "(initial state)".to_string(),
                        message: format!("Initial state violates invariants: {}", msg),
                        state_description: state.describe_state(),
                    }),
                };
            }
        }

        for i in 0..self.max_operations {
            // Maybe advance time
            if self.time_config.probability > 0.0 && rng.next_bool(self.time_config.probability) {
                let advance = if self.time_config.min_ms == self.time_config.max_ms {
                    self.time_config.min_ms
                } else {
                    rng.next_usize(
                        self.time_config.min_ms as usize,
                        self.time_config.max_ms as usize,
                    ) as u64
                };
                clock.advance_ms(advance);
            }

            // Generate and apply operation
            let op = state.generate_operation(&mut rng);
            let op_debug = format!("{:?}", op);
            state.apply_operation(&op, &clock);

            // Check invariants
            if let Err(msg) = state.check_invariants() {
                return PropertyTestResult {
                    operations_executed: i + 1,
                    seed: self.seed,
                    failure: Some(PropertyTestFailure {
                        operation_index: i,
                        operation: op_debug,
                        message: msg,
                        state_description: state.describe_state(),
                    }),
                };
            }
        }

        PropertyTestResult {
            operations_executed: self.max_operations,
            seed: self.seed,
            failure: None,
        }
    }

    /// Run the property test, panicking on failure.
    ///
    /// Convenience method for use in #[test] functions.
    ///
    /// # Panics
    /// Panics if any invariant is violated.
    pub fn run_and_assert<T: PropertyTestable>(self, state: T) {
        self.run(state).unwrap();
    }
}

/// Run multiple property tests with different seeds.
///
/// TigerStyle: Multi-seed testing for broader coverage.
///
/// # Panics
/// Panics if any test fails.
pub fn run_property_tests<T, F>(seeds: &[u64], max_operations: u64, state_factory: F)
where
    T: PropertyTestable,
    F: Fn() -> T,
{
    for &seed in seeds {
        let state = state_factory();
        PropertyTest::new(seed)
            .with_max_operations(max_operations)
            .run_and_assert(state);
    }
}

/// Generate a set of test seeds including edge cases.
///
/// Returns seeds: [0, 1, 42, random, random, ...]
#[must_use]
pub fn test_seeds(count: usize) -> Vec<u64> {
    assert!(count >= 3, "need at least 3 seeds for edge cases");

    let mut seeds = vec![0, 1, 42]; // Edge cases + common test seed

    // Add random seeds
    let time_seed = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_nanos() as u64)
        .unwrap_or(12345);
    let mut rng = DeterministicRng::new(time_seed);

    while seeds.len() < count {
        // Generate u64 from two usize values
        let high = rng.next_usize(0, u32::MAX as usize) as u64;
        let low = rng.next_usize(0, u32::MAX as usize) as u64;
        seeds.push((high << 32) | low);
    }

    seeds
}

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

    /// Simple counter for testing the property test framework.
    struct BoundedCounter {
        value: i64,
        min: i64,
        max: i64,
    }

    #[derive(Debug, Clone)]
    enum CounterOp {
        Increment(i64),
        Decrement(i64),
        Reset,
    }

    impl PropertyTestable for BoundedCounter {
        type Operation = CounterOp;

        fn generate_operation(&self, rng: &mut DeterministicRng) -> Self::Operation {
            match rng.next_usize(0, 3) {
                0 => CounterOp::Increment(rng.next_usize(1, 20) as i64),
                1 => CounterOp::Decrement(rng.next_usize(1, 20) as i64),
                _ => CounterOp::Reset,
            }
        }

        fn apply_operation(&mut self, op: &Self::Operation, _clock: &SimClock) {
            match op {
                CounterOp::Increment(n) => {
                    self.value = (self.value + n).min(self.max);
                }
                CounterOp::Decrement(n) => {
                    self.value = (self.value - n).max(self.min);
                }
                CounterOp::Reset => {
                    self.value = 0;
                }
            }
        }

        fn check_invariants(&self) -> Result<(), String> {
            if self.value < self.min {
                return Err(format!("value {} below min {}", self.value, self.min));
            }
            if self.value > self.max {
                return Err(format!("value {} above max {}", self.value, self.max));
            }
            Ok(())
        }

        fn describe_state(&self) -> String {
            format!(
                "BoundedCounter {{ value: {}, min: {}, max: {} }}",
                self.value, self.min, self.max
            )
        }
    }

    #[test]
    fn test_property_test_success() {
        let counter = BoundedCounter {
            value: 0,
            min: -100,
            max: 100,
        };

        let result = PropertyTest::new(42)
            .with_max_operations(1000)
            .with_time_advance(TimeAdvanceConfig::none())
            .run(counter);

        assert!(result.is_success());
        assert_eq!(result.operations_executed, 1000);
        assert_eq!(result.seed, 42);
    }

    #[test]
    fn test_property_test_determinism() {
        // Same seed should produce same results
        let run1 = PropertyTest::new(12345)
            .with_max_operations(100)
            .run(BoundedCounter {
                value: 0,
                min: -50,
                max: 50,
            });

        let run2 = PropertyTest::new(12345)
            .with_max_operations(100)
            .run(BoundedCounter {
                value: 0,
                min: -50,
                max: 50,
            });

        assert_eq!(run1.operations_executed, run2.operations_executed);
        assert_eq!(run1.is_success(), run2.is_success());
    }

    /// Buggy counter that doesn't clamp properly - should fail.
    struct BuggyCounter {
        value: i64,
        max: i64,
    }

    #[derive(Debug, Clone)]
    enum BuggyOp {
        Add(i64),
    }

    impl PropertyTestable for BuggyCounter {
        type Operation = BuggyOp;

        fn generate_operation(&self, rng: &mut DeterministicRng) -> Self::Operation {
            BuggyOp::Add(rng.next_usize(1, 50) as i64)
        }

        fn apply_operation(&mut self, op: &Self::Operation, _clock: &SimClock) {
            match op {
                BuggyOp::Add(n) => {
                    // Bug: doesn't clamp to max!
                    self.value += n;
                }
            }
        }

        fn check_invariants(&self) -> Result<(), String> {
            if self.value > self.max {
                return Err(format!("value {} exceeds max {}", self.value, self.max));
            }
            Ok(())
        }

        fn describe_state(&self) -> String {
            format!(
                "BuggyCounter {{ value: {}, max: {} }}",
                self.value, self.max
            )
        }
    }

    #[test]
    fn test_property_test_catches_bug() {
        let counter = BuggyCounter { value: 0, max: 100 };

        let result = PropertyTest::new(42).with_max_operations(1000).run(counter);

        assert!(result.is_failure());
        let failure = result.failure.unwrap();
        assert!(failure.message.contains("exceeds max"));
    }

    #[test]
    fn test_time_advance_config() {
        // Test that time actually advances
        struct TimeTracker {
            last_time: u64,
            times_advanced: u64,
        }

        #[derive(Debug, Clone)]
        struct NoOp;

        impl PropertyTestable for TimeTracker {
            type Operation = NoOp;

            fn generate_operation(&self, _rng: &mut DeterministicRng) -> Self::Operation {
                NoOp
            }

            fn apply_operation(&mut self, _op: &Self::Operation, clock: &SimClock) {
                let now = clock.now_ms();
                if now > self.last_time {
                    self.times_advanced += 1;
                    self.last_time = now;
                }
            }

            fn check_invariants(&self) -> Result<(), String> {
                Ok(())
            }

            fn describe_state(&self) -> String {
                format!("TimeTracker {{ times_advanced: {} }}", self.times_advanced)
            }
        }

        let tracker = TimeTracker {
            last_time: 0,
            times_advanced: 0,
        };

        let _result = PropertyTest::new(42)
            .with_max_operations(100)
            .with_time_advance(TimeAdvanceConfig::fixed(10))
            .run(tracker);

        // Time should have advanced multiple times
        // (we can't easily check the internal state after run, but no panics = success)
    }

    #[test]
    fn test_test_seeds() {
        let seeds = test_seeds(10);
        assert_eq!(seeds.len(), 10);
        assert_eq!(seeds[0], 0); // Edge case
        assert_eq!(seeds[1], 1); // Edge case
        assert_eq!(seeds[2], 42); // Common test seed
    }

    #[test]
    fn test_run_property_tests_helper() {
        run_property_tests(&[0, 1, 42], 100, || BoundedCounter {
            value: 0,
            min: -100,
            max: 100,
        });
    }

    #[test]
    fn test_initial_invariant_check() {
        // State that starts invalid
        let bad_counter = BoundedCounter {
            value: 200, // Exceeds max!
            min: -100,
            max: 100,
        };

        let result = PropertyTest::new(42).run(bad_counter);

        assert!(result.is_failure());
        assert!(result
            .failure
            .unwrap()
            .message
            .contains("Initial state violates"));
    }

    #[test]
    fn test_skip_initial_invariant_check() {
        // Use BuggyCounter which doesn't clamp - starts invalid and stays invalid
        let bad_counter = BuggyCounter {
            value: 200,
            max: 100,
        };

        let result = PropertyTest::new(42)
            .skip_initial_invariant_check()
            .with_max_operations(1)
            .run(bad_counter);

        // Should fail when invariant is checked after first op (value still > max)
        assert!(result.is_failure());
    }

    #[test]
    fn test_skip_initial_but_fixes_itself() {
        // BoundedCounter clamps values, so invalid initial state gets fixed
        let bad_counter = BoundedCounter {
            value: 200,
            min: -100,
            max: 100,
        };

        let result = PropertyTest::new(42)
            .skip_initial_invariant_check()
            .with_max_operations(1)
            .run(bad_counter);

        // Should pass because BoundedCounter clamps to valid range on any operation
        assert!(result.is_success());
    }
}