Skip to main content

vtcode_memory/
manifest.rs

1use std::fs::{self, File};
2use std::io::BufReader;
3
4use crate::SessionManifest;
5use crate::TurnIndex;
6use crate::error::SessionStoreError;
7
8/// Manifest persistence helpers.
9///
10/// Separated from `event_log` so the hot append path does not carry
11/// serialization concerns, and so `open` can cheaply probe the manifest
12/// before deciding whether to run the O(n) scan.
13pub struct ManifestStore {
14    session_dir: std::path::PathBuf,
15}
16
17impl ManifestStore {
18    /// Create a new manifest store for the given session directory.
19    pub(crate) fn new(session_dir: std::path::PathBuf) -> Self {
20        Self { session_dir }
21    }
22
23    /// Path to `manifest.json` inside the session directory.
24    fn manifest_path(&self) -> std::path::PathBuf {
25        self.session_dir.join("manifest.json")
26    }
27
28    /// Path to `index/turns.json` inside the session directory.
29    fn turns_path(&self) -> std::path::PathBuf {
30        self.session_dir.join("index").join("turns.json")
31    }
32
33    /// Load the manifest if it exists and is parseable.
34    ///
35    /// Returns `Ok(None)` when the file is missing (fresh session) or
36    /// unreadable, rather than erroring — the caller can fall back to
37    /// scanning the event log.
38    pub(crate) fn load_manifest(&self) -> Result<Option<SessionManifest>, SessionStoreError> {
39        let path = self.manifest_path();
40        if !path.exists() {
41            return Ok(None);
42        }
43        let file = File::open(&path).map_err(|e| SessionStoreError::io(path.clone(), e))?;
44        let reader = BufReader::new(file);
45        let manifest: SessionManifest =
46            serde_json::from_reader(reader).map_err(|e| SessionStoreError::io(path.clone(), e.into()))?;
47        Ok(Some(manifest))
48    }
49
50    /// Load the turn index if it exists and is parseable.
51    ///
52    /// Returns `Ok(None)` when the file is missing or unreadable.
53    pub(crate) fn load_turn_index(&self) -> Result<Option<TurnIndex>, SessionStoreError> {
54        let path = self.turns_path();
55        if !path.exists() {
56            return Ok(None);
57        }
58        let file = File::open(&path).map_err(|e| SessionStoreError::io(path.clone(), e))?;
59        let reader = BufReader::new(file);
60        let index: TurnIndex =
61            serde_json::from_reader(reader).map_err(|e| SessionStoreError::io(path.clone(), e.into()))?;
62        Ok(Some(index))
63    }
64    /// Atomically write the manifest. Parent directories must already exist.
65    pub(crate) fn write_manifest(&self, manifest: &SessionManifest) -> Result<(), SessionStoreError> {
66        let path = self.manifest_path();
67        let bytes = serde_json::to_vec(manifest)?;
68        fs::write(&path, bytes).map_err(|e| SessionStoreError::io(path.clone(), e))
69    }
70
71    /// Atomically write the turn index. Parent directories must already exist.
72    pub(crate) fn write_turn_index(&self, index: &TurnIndex) -> Result<(), SessionStoreError> {
73        let path = self.turns_path();
74        let bytes = serde_json::to_vec(index)?;
75        fs::write(&path, bytes).map_err(|e| SessionStoreError::io(path.clone(), e))
76    }
77}