oxirs-chat 0.3.1

RAG chat API with LLM integration and natural language to SPARQL translation
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
//! Session recovery, checkpoint loading, and partial recovery support.
//!
//! Sibling module of [`crate::persistence`]. Encapsulates the
//! corruption-tolerant code paths that try to recover sessions from
//! checkpoint files, backups, or by salvaging data from partially
//! readable storage files.

use anyhow::{anyhow, Context, Result};
use std::{
    collections::HashMap,
    fs,
    path::Path,
    time::{Duration, SystemTime},
};
use tokio::{fs::File, io::AsyncReadExt};
use tracing::{debug, error, info, warn};

use crate::{
    persistence_storage::SessionPersistenceManager,
    persistence_types::{PersistedSession, PersistentChatSession, RecoveryInfo, RecoveryStrategy},
};

impl SessionPersistenceManager {
    /// Attempt to recover a corrupted session.
    pub async fn attempt_recovery(
        &self,
        session_id: &str,
    ) -> Result<Option<PersistentChatSession>> {
        warn!("Attempting recovery for session {}", session_id);

        let recovery_info = self.analyze_recovery_options(session_id).await?;

        for strategy in &recovery_info.recovery_strategies {
            match strategy {
                RecoveryStrategy::LoadFromCheckpoint => {
                    if let Ok(Some(session)) = self.load_from_checkpoint(session_id).await {
                        info!(
                            "Successfully recovered session {} from checkpoint",
                            session_id
                        );
                        return Ok(Some(session));
                    }
                }
                RecoveryStrategy::LoadFromBackup => {
                    if let Ok(Some(session)) = self.load_from_backup(session_id).await {
                        info!("Successfully recovered session {} from backup", session_id);
                        return Ok(Some(session));
                    }
                }
                RecoveryStrategy::PartialRecovery => {
                    if let Ok(Some(session)) = self.attempt_partial_recovery(session_id).await {
                        warn!("Partial recovery successful for session {}", session_id);
                        return Ok(Some(session));
                    }
                }
                RecoveryStrategy::CreateNew => {
                    warn!(
                        "Creating new session to replace corrupted session {}",
                        session_id
                    );
                    return Ok(Some(self.create_emergency_session(session_id).await?));
                }
            }
        }

        // Update statistics
        {
            let mut stats = self.persistence_stats.write().await;
            stats.recovery_operations += 1;
            stats.corrupted_sessions += 1;
            stats.load_failures += 1;
        }

        error!("Failed to recover session {}", session_id);
        Ok(None)
    }

    pub(crate) async fn analyze_recovery_options(&self, session_id: &str) -> Result<RecoveryInfo> {
        let backup_available = self.get_backup_file_path(session_id).exists();
        let checkpoint_path = self.get_checkpoint_file_path(session_id);
        let checkpoint_available = checkpoint_path.exists();

        let mut recovery_strategies = Vec::new();

        // Add checkpoint recovery if available
        if checkpoint_available {
            recovery_strategies.push(RecoveryStrategy::LoadFromCheckpoint);
        }

        if backup_available {
            recovery_strategies.push(RecoveryStrategy::LoadFromBackup);
        }

        recovery_strategies.push(RecoveryStrategy::PartialRecovery);
        recovery_strategies.push(RecoveryStrategy::CreateNew);

        // Get actual checkpoint time from file metadata
        let last_checkpoint = if checkpoint_available {
            match fs::metadata(&checkpoint_path) {
                Ok(metadata) => {
                    // Use file modification time as checkpoint time
                    metadata.modified().unwrap_or_else(|_| SystemTime::now())
                }
                Err(_) => SystemTime::now(),
            }
        } else {
            SystemTime::UNIX_EPOCH // No checkpoint available
        };

        Ok(RecoveryInfo {
            session_id: session_id.to_string(),
            last_checkpoint,
            backup_available,
            corruption_detected: true,
            recovery_strategies,
        })
    }

    async fn load_from_checkpoint(
        &self,
        session_id: &str,
    ) -> Result<Option<PersistentChatSession>> {
        let checkpoint_path = self.get_checkpoint_file_path(session_id);
        if !checkpoint_path.exists() {
            debug!("No checkpoint file found for session: {}", session_id);
            return Ok(None);
        }

        info!(
            "Loading session {} from checkpoint: {:?}",
            session_id, checkpoint_path
        );

        // Read checkpoint file
        let mut file = File::open(&checkpoint_path)
            .await
            .context("Failed to open checkpoint file")?;

        let mut data = Vec::new();
        file.read_to_end(&mut data)
            .await
            .context("Failed to read checkpoint file")?;

        // Deserialize the persisted session
        let persisted: PersistedSession = if self.config.compression_enabled {
            self.decompress_session(&data)
                .await
                .context("Failed to decompress checkpoint session")?
        } else {
            oxicode::serde::decode_from_slice(&data, oxicode::config::standard())
                .map(|(session, _)| session)
                .context("Failed to deserialize checkpoint session")?
        };

        // Verify checksum if available
        if !persisted.checksum.is_empty() && !self.verify_checksum(&persisted).await? {
            warn!(
                "Checkpoint checksum verification failed for session: {}",
                session_id
            );
            return Ok(None);
        }

        // Convert to persistent chat session
        let session = self
            .convert_to_persistent_chat_session(&persisted)
            .await
            .context("Failed to convert persisted session")?;

        // Update stats
        {
            let mut stats = self.persistence_stats.write().await;
            stats.total_sessions_loaded += 1;
        }

        info!("Successfully loaded session {} from checkpoint", session_id);
        Ok(Some(session))
    }

    async fn load_from_backup(&self, session_id: &str) -> Result<Option<PersistentChatSession>> {
        let backup_path = self.get_backup_file_path(session_id);
        if !backup_path.exists() {
            return Ok(None);
        }

        let mut file = File::open(&backup_path).await?;
        let mut data = Vec::new();
        file.read_to_end(&mut data).await?;

        let persisted: PersistedSession = if self.config.compression_enabled {
            self.decompress_session(&data).await?
        } else {
            oxicode::serde::decode_from_slice(&data, oxicode::config::standard())
                .map_err(|e| anyhow!("Bincode decoding failed: {}", e))?
                .0
        };

        Ok(Some(
            self.convert_to_persistent_chat_session(&persisted).await?,
        ))
    }

    async fn attempt_partial_recovery(
        &self,
        session_id: &str,
    ) -> Result<Option<PersistentChatSession>> {
        info!("Attempting partial recovery for session: {}", session_id);

        let mut recovered_session = None;
        let mut recovery_sources = Vec::new();

        // Try to recover from various sources in order of preference

        // 1. Try to read from main session file with error tolerance
        let session_path = self.get_session_file_path(session_id);
        if session_path.exists() {
            if let Some(partial_session) = self
                .try_partial_file_recovery(&session_path, "session")
                .await
            {
                recovered_session = Some(partial_session);
                recovery_sources.push("main_session_file");
            }
        }

        // 2. Try to read from checkpoint file with error tolerance
        if recovered_session.is_none() {
            let checkpoint_path = self.get_checkpoint_file_path(session_id);
            if checkpoint_path.exists() {
                if let Some(partial_session) = self
                    .try_partial_file_recovery(&checkpoint_path, "checkpoint")
                    .await
                {
                    recovered_session = Some(partial_session);
                    recovery_sources.push("checkpoint_file");
                }
            }
        }

        // 3. Try to read from backup file with error tolerance
        if recovered_session.is_none() {
            let backup_path = self.get_backup_file_path(session_id);
            if backup_path.exists() {
                if let Some(partial_session) =
                    self.try_partial_file_recovery(&backup_path, "backup").await
                {
                    recovered_session = Some(partial_session);
                    recovery_sources.push("backup_file");
                }
            }
        }

        // 4. If we have some recovery, validate and sanitize it
        if let Some(mut session) = recovered_session {
            // Sanitize the session data
            session = self.sanitize_recovered_session(session, session_id).await;

            // Update recovery stats
            {
                let mut stats = self.persistence_stats.write().await;
                stats.recovery_operations += 1;
            }

            info!(
                "Partial recovery successful for session {} from sources: {:?}",
                session_id, recovery_sources
            );

            return Ok(Some(session));
        }

        warn!("Partial recovery failed for session: {}", session_id);
        Ok(None)
    }

    /// Attempt to read and recover data from a potentially corrupted file.
    async fn try_partial_file_recovery(
        &self,
        file_path: &Path,
        source_type: &str,
    ) -> Option<PersistentChatSession> {
        debug!(
            "Trying partial recovery from {} file: {:?}",
            source_type, file_path
        );

        // Try to read the file
        let data = match self.read_file_with_tolerance(file_path).await {
            Ok(data) => data,
            Err(e) => {
                warn!("Failed to read {} file: {}", source_type, e);
                return None;
            }
        };

        // Try to deserialize with error tolerance
        let persisted_session = if self.config.compression_enabled {
            // Try decompression first
            match self.decompress_session(&data).await {
                Ok(session) => session,
                Err(_) => {
                    // Fallback to direct deserialization if decompression fails
                    match oxicode::serde::decode_from_slice::<PersistedSession, _>(
                        &data,
                        oxicode::config::standard(),
                    ) {
                        Ok((session, _)) => session,
                        Err(e) => {
                            warn!("Failed to deserialize {} file: {}", source_type, e);
                            return None;
                        }
                    }
                }
            }
        } else {
            match oxicode::serde::decode_from_slice::<PersistedSession, _>(
                &data,
                oxicode::config::standard(),
            ) {
                Ok((session, _)) => session,
                Err(e) => {
                    warn!("Failed to deserialize {} file: {}", source_type, e);
                    return None;
                }
            }
        };

        // Convert to persistent chat session
        match self
            .convert_to_persistent_chat_session(&persisted_session)
            .await
        {
            Ok(session) => Some(session),
            Err(e) => {
                warn!("Failed to convert {} session: {}", source_type, e);
                None
            }
        }
    }

    /// Read file with error tolerance.
    async fn read_file_with_tolerance(&self, file_path: &Path) -> Result<Vec<u8>> {
        let mut file = File::open(file_path).await?;
        let mut data = Vec::new();

        // Try to read the entire file, but don't fail on partial reads
        match file.read_to_end(&mut data).await {
            Ok(_) => Ok(data),
            Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
                // File is truncated but we got some data
                if !data.is_empty() {
                    warn!("File truncated but partial data recovered: {:?}", file_path);
                    Ok(data)
                } else {
                    Err(e.into())
                }
            }
            Err(e) => Err(e.into()),
        }
    }

    /// Sanitize and repair a recovered session.
    async fn sanitize_recovered_session(
        &self,
        mut session: PersistentChatSession,
        session_id: &str,
    ) -> PersistentChatSession {
        // Ensure session ID matches
        session.session_id = session_id.to_string();

        // Validate and fix timestamps
        if session.created_at > SystemTime::now() {
            session.created_at = SystemTime::now() - Duration::from_secs(86400);
            // Set to yesterday (24 hours ago)
        }

        if session.last_accessed > SystemTime::now() {
            session.last_accessed = SystemTime::now();
        }

        // Filter out potentially corrupted messages
        session.messages.retain(|msg| {
            !msg.id.is_empty()
                && !msg.content.to_string().is_empty()
                && msg.timestamp <= chrono::Utc::now()
        });

        // Ensure we have at least basic metrics
        if session.metrics.total_messages == 0 && !session.messages.is_empty() {
            session.metrics.total_messages = session.messages.len();
        }

        // Reset user preferences if they seem corrupted
        if session.user_preferences.len() > 1000 {
            warn!(
                "User preferences seem corrupted, resetting for session: {}",
                session_id
            );
            session.user_preferences = HashMap::new();
        }

        // Add recovery metadata
        session.user_preferences.insert(
            "recovery_performed".to_string(),
            serde_json::Value::String(
                serde_json::to_string(&chrono::Utc::now()).unwrap_or_default(),
            ),
        );

        session.user_preferences.insert(
            "recovery_type".to_string(),
            serde_json::Value::String("partial".to_string()),
        );

        session
    }
}