llm-memory-graph 0.1.0

Graph-based context-tracking and prompt-lineage database for LLM systems
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
//! Retention policy and automatic archival scheduler

use super::archiver::{ArchiveEntry, ComplianceLevel, RetentionPolicy, VaultClient};
use crate::engine::AsyncMemoryGraph;
use crate::integrations::IntegrationError;
use chrono::{DateTime, Duration as ChronoDuration, Utc};
use std::sync::Arc;
use std::time::Duration;
use tokio::task::JoinHandle;
use tracing::{debug, error, info, warn};

/// Scheduler configuration for automatic archival
#[derive(Debug, Clone)]
pub struct SchedulerConfig {
    /// Interval between archival runs (in hours)
    pub interval_hours: u64,
    /// Archive sessions older than this many days
    pub archive_after_days: i64,
    /// Default retention period for archived sessions (in days)
    pub retention_days: i64,
    /// Maximum number of sessions to archive per batch
    pub batch_size: usize,
    /// Default compliance level
    pub default_compliance_level: ComplianceLevel,
    /// Enable automatic archival
    pub enabled: bool,
}

impl Default for SchedulerConfig {
    fn default() -> Self {
        Self {
            interval_hours: 24,
            archive_after_days: 30,
            retention_days: 365,
            batch_size: 100,
            default_compliance_level: ComplianceLevel::Standard,
            enabled: true,
        }
    }
}

impl SchedulerConfig {
    /// Create a new scheduler configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the archival interval in hours
    pub fn with_interval_hours(mut self, hours: u64) -> Self {
        self.interval_hours = hours;
        self
    }

    /// Set the age threshold for archival in days
    pub fn with_archive_after_days(mut self, days: i64) -> Self {
        self.archive_after_days = days;
        self
    }

    /// Set the retention period in days
    pub fn with_retention_days(mut self, days: i64) -> Self {
        self.retention_days = days;
        self
    }

    /// Set the batch size
    pub fn with_batch_size(mut self, size: usize) -> Self {
        self.batch_size = size;
        self
    }

    /// Set the default compliance level
    pub fn with_compliance_level(mut self, level: ComplianceLevel) -> Self {
        self.default_compliance_level = level;
        self
    }

    /// Enable or disable the scheduler
    pub fn with_enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }
}

/// Archival statistics
#[derive(Debug, Clone, Default)]
pub struct ArchivalStats {
    /// Total sessions processed
    pub total_processed: usize,
    /// Successfully archived
    pub archived_count: usize,
    /// Failed to archive
    pub failed_count: usize,
    /// Skipped sessions
    pub skipped_count: usize,
    /// Total time taken in milliseconds
    pub duration_ms: u64,
}

impl ArchivalStats {
    /// Create a new stats instance
    pub fn new() -> Self {
        Self::default()
    }

    /// Calculate success rate
    pub fn success_rate(&self) -> f64 {
        if self.total_processed == 0 {
            0.0
        } else {
            (self.archived_count as f64 / self.total_processed as f64) * 100.0
        }
    }
}

/// Automatic archival scheduler
///
/// Periodically archives old sessions based on configured retention policies.
pub struct ArchivalScheduler {
    vault_client: Arc<VaultClient>,
    graph: Arc<AsyncMemoryGraph>,
    config: SchedulerConfig,
    running: Arc<tokio::sync::RwLock<bool>>,
}

impl ArchivalScheduler {
    /// Create a new archival scheduler
    pub fn new(
        vault_client: VaultClient,
        graph: Arc<AsyncMemoryGraph>,
        config: SchedulerConfig,
    ) -> Self {
        Self {
            vault_client: Arc::new(vault_client),
            graph,
            config,
            running: Arc::new(tokio::sync::RwLock::new(false)),
        }
    }

    /// Check if the scheduler is currently running
    pub async fn is_running(&self) -> bool {
        *self.running.read().await
    }

    /// Start the archival scheduler
    ///
    /// Returns a join handle that can be used to stop the scheduler.
    pub async fn start(&self) -> JoinHandle<()> {
        if !self.config.enabled {
            info!("Archival scheduler is disabled");
            return tokio::spawn(async {});
        }

        let vault = Arc::clone(&self.vault_client);
        let graph = Arc::clone(&self.graph);
        let config = self.config.clone();
        let running = Arc::clone(&self.running);

        info!(
            "Starting archival scheduler (interval: {}h, archive after: {} days)",
            config.interval_hours, config.archive_after_days
        );

        tokio::spawn(async move {
            *running.write().await = true;
            let mut interval =
                tokio::time::interval(Duration::from_secs(config.interval_hours * 3600));

            loop {
                interval.tick().await;

                if !*running.read().await {
                    info!("Archival scheduler stopped");
                    break;
                }

                info!("Running archival scheduler iteration");
                let start = std::time::Instant::now();

                match Self::run_archival(&vault, &graph, &config).await {
                    Ok(stats) => {
                        let duration = start.elapsed();
                        info!(
                            "Archival iteration complete: {} processed, {} archived, {} failed, {} skipped (took {}ms, {:.2}% success)",
                            stats.total_processed,
                            stats.archived_count,
                            stats.failed_count,
                            stats.skipped_count,
                            duration.as_millis(),
                            stats.success_rate()
                        );
                    }
                    Err(e) => {
                        error!("Archival iteration failed: {}", e);
                    }
                }
            }
        })
    }

    /// Stop the scheduler
    pub async fn stop(&self) {
        info!("Stopping archival scheduler");
        *self.running.write().await = false;
    }

    /// Run a single archival iteration
    async fn run_archival(
        vault: &VaultClient,
        graph: &AsyncMemoryGraph,
        config: &SchedulerConfig,
    ) -> Result<ArchivalStats, IntegrationError> {
        let mut stats = ArchivalStats::new();
        let start_time = std::time::Instant::now();

        // Calculate cutoff date for archival
        let cutoff_date = Utc::now() - ChronoDuration::days(config.archive_after_days);
        debug!("Archiving sessions older than {}", cutoff_date);

        // Query for old sessions
        // Note: This is a placeholder. The actual implementation would need
        // to query the graph for sessions older than the cutoff date.
        // For now, we'll return empty stats.

        // TODO: Implement session querying by age when query API supports it
        // Example:
        // let old_sessions = graph
        //     .query()
        //     .session_older_than(cutoff_date)
        //     .limit(config.batch_size)
        //     .execute()
        //     .await?;

        warn!("Session age-based querying not yet implemented in graph query API");

        stats.duration_ms = start_time.elapsed().as_millis() as u64;
        Ok(stats)
    }

    /// Archive a specific session manually
    ///
    /// # Errors
    /// Returns an error if the archival fails.
    pub async fn archive_session_now(
        &self,
        session_id: &str,
        session_data: serde_json::Value,
    ) -> Result<String, IntegrationError> {
        info!("Manually archiving session: {}", session_id);

        let entry = ArchiveEntry::new(
            session_id,
            session_data,
            self.config.retention_days,
        )
        .with_tag("manual")
        .with_metadata(
            "archived_by",
            serde_json::json!("archival_scheduler"),
        );

        let response = self.vault_client.archive_session(entry).await?;
        Ok(response.archive_id)
    }

    /// Create a retention policy for a specific compliance level
    ///
    /// # Errors
    /// Returns an error if policy creation fails.
    pub async fn create_compliance_policy(
        &self,
        name: impl Into<String>,
        compliance_level: ComplianceLevel,
    ) -> Result<String, IntegrationError> {
        let retention_days = match compliance_level {
            ComplianceLevel::Standard => 365,
            ComplianceLevel::Hipaa => 2555, // 7 years
            ComplianceLevel::Gdpr => 2555,  // 7 years
            ComplianceLevel::Pci => 1095,   // 3 years
            ComplianceLevel::Soc2 => 2555,  // 7 years
        };

        let policy = RetentionPolicy::new(name, retention_days, compliance_level)
            .with_auto_delete(false)
            .with_tag(format!("{:?}", compliance_level).to_lowercase());

        self.vault_client.create_retention_policy(policy).await
    }

    /// Batch archive multiple sessions
    ///
    /// # Errors
    /// Returns an error if the batch archival fails.
    pub async fn batch_archive_sessions(
        &self,
        sessions: Vec<(String, serde_json::Value)>,
    ) -> Result<ArchivalStats, IntegrationError> {
        let mut stats = ArchivalStats::new();
        stats.total_processed = sessions.len();

        let entries: Vec<ArchiveEntry> = sessions
            .into_iter()
            .map(|(session_id, data)| {
                ArchiveEntry::new(session_id, data, self.config.retention_days)
                    .with_tag("batch")
            })
            .collect();

        let response = self.vault_client.batch_archive(entries).await?;

        stats.archived_count = response.success_count;
        stats.failed_count = response.failed.len();

        Ok(stats)
    }
}

/// Retention policy manager for creating and managing policies
pub struct RetentionPolicyManager {
    vault_client: Arc<VaultClient>,
}

impl RetentionPolicyManager {
    /// Create a new policy manager
    pub fn new(vault_client: VaultClient) -> Self {
        Self {
            vault_client: Arc::new(vault_client),
        }
    }

    /// Create a standard retention policy
    pub async fn create_standard_policy(
        &self,
        name: impl Into<String>,
        retention_days: i64,
    ) -> Result<String, IntegrationError> {
        let policy = RetentionPolicy::new(name, retention_days, ComplianceLevel::Standard);
        self.vault_client.create_retention_policy(policy).await
    }

    /// Create a HIPAA compliance policy (7 years)
    pub async fn create_hipaa_policy(
        &self,
        name: impl Into<String>,
    ) -> Result<String, IntegrationError> {
        let policy = RetentionPolicy::new(name, 2555, ComplianceLevel::Hipaa)
            .with_description("HIPAA 7-year retention requirement")
            .with_tag("healthcare")
            .with_tag("compliance");

        self.vault_client.create_retention_policy(policy).await
    }

    /// Create a GDPR compliance policy (7 years)
    pub async fn create_gdpr_policy(
        &self,
        name: impl Into<String>,
    ) -> Result<String, IntegrationError> {
        let policy = RetentionPolicy::new(name, 2555, ComplianceLevel::Gdpr)
            .with_description("GDPR 7-year retention for financial records")
            .with_tag("gdpr")
            .with_tag("compliance");

        self.vault_client.create_retention_policy(policy).await
    }

    /// Create a PCI-DSS compliance policy (3 years)
    pub async fn create_pci_policy(
        &self,
        name: impl Into<String>,
    ) -> Result<String, IntegrationError> {
        let policy = RetentionPolicy::new(name, 1095, ComplianceLevel::Pci)
            .with_description("PCI-DSS 3-year retention requirement")
            .with_tag("payment")
            .with_tag("compliance");

        self.vault_client.create_retention_policy(policy).await
    }

    /// Apply a policy to an archive
    pub async fn apply_policy(
        &self,
        archive_id: &str,
        policy_id: &str,
    ) -> Result<(), IntegrationError> {
        self.vault_client
            .apply_retention_policy(archive_id, policy_id)
            .await
    }
}

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

    #[test]
    fn test_scheduler_config_builder() {
        let config = SchedulerConfig::new()
            .with_interval_hours(12)
            .with_archive_after_days(60)
            .with_retention_days(730)
            .with_batch_size(50)
            .with_compliance_level(ComplianceLevel::Hipaa)
            .with_enabled(false);

        assert_eq!(config.interval_hours, 12);
        assert_eq!(config.archive_after_days, 60);
        assert_eq!(config.retention_days, 730);
        assert_eq!(config.batch_size, 50);
        assert_eq!(config.default_compliance_level, ComplianceLevel::Hipaa);
        assert!(!config.enabled);
    }

    #[test]
    fn test_archival_stats_success_rate() {
        let mut stats = ArchivalStats::new();
        stats.total_processed = 100;
        stats.archived_count = 95;
        stats.failed_count = 5;

        assert_eq!(stats.success_rate(), 95.0);
    }

    #[test]
    fn test_archival_stats_zero_processed() {
        let stats = ArchivalStats::new();
        assert_eq!(stats.success_rate(), 0.0);
    }
}