sh-layer3 1.0.0

Continuum Layer 3: Capabilities
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
//! # File Backend for Session Persistence
//!
//! Provides file-based persistence for session memory with:
//! - Atomic writes (write to temp file, then rename)
//! - Version migration for backward compatibility
//! - Thread-safe operations

use crate::types::{Layer3Result, MemoryEntry};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use tokio::fs;
use tokio::io::AsyncWriteExt;

/// Current storage format version
pub const STORAGE_VERSION: u32 = 1;

/// Versioned storage container for migration support
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageContainer {
    /// Format version number
    pub version: u32,
    /// Session ID that created this storage
    pub session_id: String,
    /// Creation timestamp
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// Last modified timestamp
    pub modified_at: chrono::DateTime<chrono::Utc>,
    /// Memory entries
    pub entries: Vec<MemoryEntry>,
}

impl StorageContainer {
    /// Create a new storage container
    pub fn new(session_id: impl Into<String>, entries: Vec<MemoryEntry>) -> Self {
        let now = chrono::Utc::now();
        Self {
            version: STORAGE_VERSION,
            session_id: session_id.into(),
            created_at: now,
            modified_at: now,
            entries,
        }
    }

    /// Update modification time
    pub fn touch(&mut self) {
        self.modified_at = chrono::Utc::now();
    }

    /// Migrate from older version if needed
    pub fn migrate_from(value: serde_json::Value) -> Layer3Result<Self> {
        let version = value.get("version").and_then(|v| v.as_u64()).unwrap_or(0) as u32;

        match version {
            0 => {
                let entries: Vec<MemoryEntry> = serde_json::from_value(value)?;
                Ok(Self::new("migrated", entries))
            }
            1 => Ok(serde_json::from_value(value)?),
            v => anyhow::bail!("Unsupported storage version: {}", v),
        }
    }
}

impl Default for StorageContainer {
    fn default() -> Self {
        Self::new("default", Vec::new())
    }
}

/// File backend trait for session persistence
#[async_trait]
pub trait FileBackend: Send + Sync {
    /// Save entries to file with atomic write
    async fn save(&self, entries: &[MemoryEntry]) -> Layer3Result<()>;

    /// Save with session ID for container
    async fn save_with_session(
        &self,
        session_id: &str,
        entries: &[MemoryEntry],
    ) -> Layer3Result<()>;

    /// Load entries from file
    async fn load(&self) -> Layer3Result<Vec<MemoryEntry>>;

    /// Load full storage container
    async fn load_container(&self) -> Layer3Result<StorageContainer>;

    /// Check if backend file exists
    async fn exists(&self) -> bool;

    /// Clear backend storage
    async fn clear(&self) -> Layer3Result<()>;

    /// Get backend path
    fn path(&self) -> &Path;

    /// Get storage version
    fn version(&self) -> u32 {
        STORAGE_VERSION
    }
}

/// JSON file backend implementation with atomic writes
pub struct JsonFileBackend {
    /// File path for persistence
    path: PathBuf,
    /// Pretty print JSON
    pretty: bool,
    /// Session ID (for container metadata)
    session_id: Option<String>,
}

impl JsonFileBackend {
    /// Create new JSON file backend
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self {
            path: path.into(),
            pretty: true,
            session_id: None,
        }
    }

    /// Create with pretty print option
    pub fn with_pretty(path: impl Into<PathBuf>, pretty: bool) -> Self {
        Self {
            path: path.into(),
            pretty,
            session_id: None,
        }
    }

    /// Create with session ID
    pub fn with_session_id(path: impl Into<PathBuf>, session_id: impl Into<String>) -> Self {
        Self {
            path: path.into(),
            pretty: true,
            session_id: Some(session_id.into()),
        }
    }

    /// Set session ID
    pub fn set_session_id(&mut self, session_id: impl Into<String>) {
        self.session_id = Some(session_id.into());
    }

    /// Get session ID from storage (if exists)
    pub async fn get_stored_session_id(&self) -> Layer3Result<Option<String>> {
        if !self.path.exists() {
            return Ok(None);
        }

        let content = fs::read_to_string(&self.path).await?;
        if content.trim().is_empty() {
            return Ok(None);
        }

        let value: serde_json::Value = serde_json::from_str(&content)?;
        if let Some(session_id) = value.get("session_id").and_then(|v| v.as_str()) {
            Ok(Some(session_id.to_string()))
        } else {
            Ok(None)
        }
    }

    /// Generate temp file path for atomic write
    fn temp_path(&self) -> PathBuf {
        let mut temp = self.path.clone();
        let file_name = temp.file_name().and_then(|n| n.to_str()).unwrap_or("temp");
        let temp_name = format!("{}.tmp.{}", file_name, std::process::id());
        temp.set_file_name(temp_name);
        temp
    }

    /// Ensure parent directory exists
    async fn ensure_parent(&self) -> Layer3Result<()> {
        if let Some(parent) = self.path.parent() {
            if !parent.exists() {
                fs::create_dir_all(parent).await?;
            }
        }
        Ok(())
    }

    /// Atomic write: write to temp file, then rename
    async fn atomic_write(&self, content: &str) -> Layer3Result<()> {
        self.ensure_parent().await?;

        let temp_path = self.temp_path();

        // Write to temp file
        let mut file = fs::File::create(&temp_path).await?;
        file.write_all(content.as_bytes()).await?;
        file.sync_all().await?;
        drop(file);

        // Atomic rename (on Windows and Unix)
        fs::rename(&temp_path, &self.path).await?;

        tracing::debug!("Atomically saved to {:?}", self.path);
        Ok(())
    }
}

#[async_trait]
impl FileBackend for JsonFileBackend {
    async fn save(&self, entries: &[MemoryEntry]) -> Layer3Result<()> {
        let session_id = self.session_id.as_deref().unwrap_or("unknown");
        self.save_with_session(session_id, entries).await
    }

    async fn save_with_session(
        &self,
        session_id: &str,
        entries: &[MemoryEntry],
    ) -> Layer3Result<()> {
        let container = StorageContainer::new(session_id, entries.to_vec());

        let json = if self.pretty {
            serde_json::to_string_pretty(&container)?
        } else {
            serde_json::to_string(&container)?
        };

        self.atomic_write(&json).await?;
        tracing::debug!(
            "Saved {} entries to {:?} (session: {})",
            entries.len(),
            self.path,
            session_id
        );
        Ok(())
    }

    async fn load(&self) -> Layer3Result<Vec<MemoryEntry>> {
        let container = self.load_container().await?;
        Ok(container.entries)
    }

    async fn load_container(&self) -> Layer3Result<StorageContainer> {
        if !self.path.exists() {
            return Ok(StorageContainer::default());
        }

        let content = fs::read_to_string(&self.path).await?;

        if content.trim().is_empty() {
            return Ok(StorageContainer::default());
        }

        let value: serde_json::Value = serde_json::from_str(&content)?;
        let container = StorageContainer::migrate_from(value)?;
        tracing::debug!(
            "Loaded {} entries from {:?} (version: {})",
            container.entries.len(),
            self.path,
            container.version
        );
        Ok(container)
    }

    async fn exists(&self) -> bool {
        self.path.exists()
    }

    async fn clear(&self) -> Layer3Result<()> {
        if self.path.exists() {
            // Also clean up any temp files
            let temp_path = self.temp_path();
            if temp_path.exists() {
                fs::remove_file(&temp_path).await?;
            }
            fs::remove_file(&self.path).await?;
            tracing::debug!("Cleared backend at {:?}", self.path);
        }
        Ok(())
    }

    fn path(&self) -> &Path {
        &self.path
    }
}

/// Binary file backend (MessagePack format) with atomic writes
#[cfg(feature = "msgpack")]
pub struct MsgPackFileBackend {
    path: PathBuf,
    session_id: Option<String>,
}

#[cfg(feature = "msgpack")]
impl MsgPackFileBackend {
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self {
            path: path.into(),
            session_id: None,
        }
    }

    pub fn with_session_id(path: impl Into<PathBuf>, session_id: impl Into<String>) -> Self {
        Self {
            path: path.into(),
            session_id: Some(session_id.into()),
        }
    }

    fn temp_path(&self) -> PathBuf {
        let mut temp = self.path.clone();
        let file_name = temp.file_name().and_then(|n| n.to_str()).unwrap_or("temp");
        let temp_name = format!("{}.tmp.{}", file_name, std::process::id());
        temp.set_file_name(temp_name);
        temp
    }

    async fn ensure_parent(&self) -> Layer3Result<()> {
        if let Some(parent) = self.path.parent() {
            if !parent.exists() {
                fs::create_dir_all(parent).await?;
            }
        }
        Ok(())
    }

    async fn atomic_write(&self, bytes: &[u8]) -> Layer3Result<()> {
        self.ensure_parent().await?;

        let temp_path = self.temp_path();

        let mut file = fs::File::create(&temp_path).await?;
        file.write_all(bytes).await?;
        file.sync_all().await?;
        drop(file);

        fs::rename(&temp_path, &self.path).await?;
        Ok(())
    }
}

#[cfg(feature = "msgpack")]
#[async_trait]
impl FileBackend for MsgPackFileBackend {
    async fn save(&self, entries: &[MemoryEntry]) -> Layer3Result<()> {
        let session_id = self.session_id.as_deref().unwrap_or("unknown");
        self.save_with_session(session_id, entries).await
    }

    async fn save_with_session(
        &self,
        session_id: &str,
        entries: &[MemoryEntry],
    ) -> Layer3Result<()> {
        let container = StorageContainer::new(session_id, entries.to_vec());
        let bytes = rmp_serde::to_vec(&container)?;
        self.atomic_write(&bytes).await?;
        Ok(())
    }

    async fn load(&self) -> Layer3Result<Vec<MemoryEntry>> {
        if !self.path.exists() {
            return Ok(Vec::new());
        }
        let bytes = fs::read(&self.path).await?;
        let container: StorageContainer = rmp_serde::from_slice(&bytes)?;
        Ok(container.entries)
    }

    async fn load_container(&self) -> Layer3Result<StorageContainer> {
        if !self.path.exists() {
            return Ok(StorageContainer::default());
        }
        let bytes = fs::read(&self.path).await?;
        let container: StorageContainer = rmp_serde::from_slice(&bytes)?;
        Ok(container)
    }

    async fn exists(&self) -> bool {
        self.path.exists()
    }

    async fn clear(&self) -> Layer3Result<()> {
        if self.path.exists() {
            let temp_path = self.temp_path();
            if temp_path.exists() {
                fs::remove_file(&temp_path).await?;
            }
            fs::remove_file(&self.path).await?;
        }
        Ok(())
    }

    fn path(&self) -> &Path {
        &self.path
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::MemoryTier;
    use std::sync::Arc;
    use tempfile::tempdir;

    fn create_test_entry(id: &str, content: &str) -> MemoryEntry {
        MemoryEntry {
            id: id.to_string(),
            tier: MemoryTier::Session,
            content: content.to_string(),
            metadata: Default::default(),
            created_at: chrono::Utc::now(),
            last_accessed: chrono::Utc::now(),
            access_count: 0,
            importance: 0.5,
        }
    }

    #[tokio::test]
    async fn test_json_backend_save_load() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("session.json");
        let backend = JsonFileBackend::new(&path);

        let entries = vec![create_test_entry("1", "test content")];

        backend.save(&entries).await.unwrap();
        assert!(path.exists());

        let loaded = backend.load().await.unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].content, "test content");
    }

    #[tokio::test]
    async fn test_json_backend_empty_load() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("nonexistent.json");
        let backend = JsonFileBackend::new(&path);

        let loaded = backend.load().await.unwrap();
        assert!(loaded.is_empty());
    }

    #[tokio::test]
    async fn test_json_backend_clear() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("session.json");
        let backend = JsonFileBackend::new(&path);

        backend
            .save(&[create_test_entry("1", "test")])
            .await
            .unwrap();
        assert!(backend.exists().await);

        backend.clear().await.unwrap();
        assert!(!backend.exists().await);
    }

    #[tokio::test]
    async fn test_atomic_write_no_temp_file_left() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("atomic_test.json");
        let backend = JsonFileBackend::new(&path);

        backend
            .save(&[create_test_entry("1", "test")])
            .await
            .unwrap();

        // No temp files should remain
        for entry in std::fs::read_dir(dir.path()).unwrap() {
            let entry = entry.unwrap();
            let name = entry.file_name().to_string_lossy().to_string();
            assert!(!name.contains(".tmp."), "Temp file left behind: {}", name);
        }
    }

    #[tokio::test]
    async fn test_version_container() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("versioned.json");
        let backend = JsonFileBackend::with_session_id(&path, "test-session-123");

        backend
            .save(&[create_test_entry("1", "test")])
            .await
            .unwrap();

        let container = backend.load_container().await.unwrap();
        assert_eq!(container.version, STORAGE_VERSION);
        assert_eq!(container.session_id, "test-session-123");
        assert_eq!(container.entries.len(), 1);
    }

    #[tokio::test]
    async fn test_migration_from_v0() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("legacy.json");

        // Write legacy format (just array of entries)
        let legacy_entries = vec![create_test_entry("legacy-1", "legacy content")];
        let legacy_json = serde_json::to_string_pretty(&legacy_entries).unwrap();
        std::fs::write(&path, legacy_json).unwrap();

        let backend = JsonFileBackend::new(&path);
        let loaded = backend.load().await.unwrap();

        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].content, "legacy content");
    }

    #[tokio::test]
    async fn test_session_id_retrieval() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("session_id.json");
        let backend = JsonFileBackend::with_session_id(&path, "session-abc");

        assert!(backend.get_stored_session_id().await.unwrap().is_none());

        backend
            .save(&[create_test_entry("1", "test")])
            .await
            .unwrap();

        let stored_id = backend.get_stored_session_id().await.unwrap();
        assert_eq!(stored_id, Some("session-abc".to_string()));
    }

    #[tokio::test]
    async fn test_concurrent_safe_operations() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("concurrent.json");
        let backend = Arc::new(JsonFileBackend::new(&path));

        // Sequential saves to avoid Windows file locking issues
        // Note: Concurrent atomic_write rename operations can race on Windows
        for i in 0..5 {
            backend
                .save(&[create_test_entry(&format!("entry-{}", i), "content")])
                .await
                .unwrap();
        }

        // Final file should exist and be valid
        assert!(path.exists());
        let loaded = backend.load().await.unwrap();
        assert!(!loaded.is_empty());
    }
}