meld 0.1.1

Deterministic filesystem state management using Merkle trees
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
//! Frame Heads
//!
//! Provides O(1) access to the "latest" frame for a given node and frame type.

use crate::error::StorageError;
use crate::types::{FrameID, NodeID};
use bincode;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

const HEAD_INDEX_VERSION_V1: u32 = 1;
const HEAD_INDEX_VERSION_V2: u32 = 2;

/// Head entry with optional tombstone marker.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct HeadEntry {
    pub frame_id: FrameID,
    pub tombstoned_at: Option<u64>,
}

/// Head index: (NodeID, frame_type) -> HeadEntry
pub struct HeadIndex {
    pub(crate) heads: HashMap<(NodeID, String), HeadEntry>,
}

impl Default for HeadIndex {
    fn default() -> Self {
        Self::new()
    }
}

impl HeadIndex {
    pub fn new() -> Self {
        HeadIndex {
            heads: HashMap::new(),
        }
    }

    /// Get active head for node and frame type (skips tombstoned entries).
    pub fn get_head(
        &self,
        node_id: &NodeID,
        frame_type: &str,
    ) -> Result<Option<FrameID>, StorageError> {
        Ok(self
            .heads
            .get(&(*node_id, frame_type.to_string()))
            .filter(|e| e.tombstoned_at.is_none())
            .map(|e| e.frame_id))
    }

    /// Get active head (alias for get_head; skips tombstoned).
    pub fn get_active_head(
        &self,
        node_id: &NodeID,
        frame_type: &str,
    ) -> Result<Option<FrameID>, StorageError> {
        self.get_head(node_id, frame_type)
    }

    pub fn update_head(
        &mut self,
        node_id: &NodeID,
        frame_type: &str,
        frame_id: &FrameID,
    ) -> Result<(), StorageError> {
        self.heads.insert(
            (*node_id, frame_type.to_string()),
            HeadEntry {
                frame_id: *frame_id,
                tombstoned_at: None,
            },
        );
        Ok(())
    }

    /// Tombstone all head entries for a node (all frame types).
    pub fn tombstone_heads_for_node(&mut self, node_id: &NodeID) {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        for ((nid, _), entry) in self.heads.iter_mut() {
            if *nid == *node_id {
                entry.tombstoned_at = Some(now);
            }
        }
    }

    /// Restore all head entries for a node (remove tombstone marker).
    pub fn restore_heads_for_node(&mut self, node_id: &NodeID) {
        for ((nid, _), entry) in self.heads.iter_mut() {
            if *nid == *node_id {
                entry.tombstoned_at = None;
            }
        }
    }

    /// Purge tombstoned head entries older than cutoff.
    pub fn purge_tombstoned(&mut self, cutoff: u64) {
        self.heads
            .retain(|_, e| e.tombstoned_at.map_or(true, |ts| ts > cutoff));
    }

    /// Get all frame IDs for a given node (including tombstoned; used e.g. for compact).
    pub fn get_all_heads_for_node(&self, node_id: &NodeID) -> Vec<FrameID> {
        self.heads
            .iter()
            .filter_map(|((nid, _), e)| {
                if *nid == *node_id {
                    Some(e.frame_id)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Get all unique node IDs that have active (non-tombstoned) heads.
    pub fn get_all_node_ids(&self) -> Vec<NodeID> {
        let mut node_ids = std::collections::HashSet::new();
        for ((node_id, _), e) in &self.heads {
            if e.tombstoned_at.is_none() {
                node_ids.insert(*node_id);
            }
        }
        node_ids.into_iter().collect()
    }

    /// Count distinct node IDs that have an active head for the given frame type.
    pub fn count_nodes_for_frame_type(&self, frame_type: &str) -> usize {
        let mut node_ids = std::collections::HashSet::new();
        for ((node_id, ft), e) in &self.heads {
            if ft.as_str() == frame_type && e.tombstoned_at.is_none() {
                node_ids.insert(*node_id);
            }
        }
        node_ids.len()
    }

    /// Get the persistence path for a workspace root
    ///
    /// Uses XDG data directory: $XDG_DATA_HOME/meld/workspaces/<hash>/head_index.bin
    pub fn persistence_path(workspace_root: &Path) -> PathBuf {
        // Try to use XDG data directory, fall back to .meld if XDG is not available
        if let Ok(data_dir) = crate::config::xdg::workspace_data_dir(workspace_root) {
            data_dir.join("head_index.bin")
        } else {
            // Fallback to old location if XDG is not available
            workspace_root.join(".meld").join("head_index.bin")
        }
    }

    /// Load head index from disk
    ///
    /// Returns an empty index if the file doesn't exist or is corrupted.
    pub fn load_from_disk<P: AsRef<Path>>(path: P) -> Result<Self, StorageError> {
        let path = path.as_ref();

        // Check if file exists
        if !path.exists() {
            return Ok(HeadIndex::new());
        }

        // Read file
        let bytes = fs::read(path).map_err(|e| {
            StorageError::IoError(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!("Failed to read head index from {:?}: {}", path, e),
            ))
        })?;

        // Try legacy V1 format (single bincode blob) first.
        if let Ok(persistence) = bincode::deserialize::<HeadIndexPersistenceV1>(&bytes) {
            if persistence.version == HEAD_INDEX_VERSION_V1 {
                let mut heads = HashMap::new();
                for entry in persistence.entries {
                    if entry.frame_id.len() != 32 || entry.node_id.len() != 32 {
                        return Err(StorageError::IoError(std::io::Error::new(
                            std::io::ErrorKind::InvalidData,
                            "Invalid frame_id or node_id length in head index".to_string(),
                        )));
                    }
                    let mut node_id = [0u8; 32];
                    node_id.copy_from_slice(&entry.node_id);
                    let mut frame_id = [0u8; 32];
                    frame_id.copy_from_slice(&entry.frame_id);
                    heads.insert(
                        (node_id, entry.frame_type),
                        HeadEntry {
                            frame_id,
                            tombstoned_at: None,
                        },
                    );
                }
                return Ok(HeadIndex { heads });
            }
        }

        // V2 format: 4-byte version then bincode(entries).
        if bytes.len() < 4 {
            return Err(StorageError::IoError(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Head index file too short".to_string(),
            )));
        }
        let version = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
        if version != HEAD_INDEX_VERSION_V2 {
            return Err(StorageError::IoError(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Unsupported head index version: {}", version),
            )));
        }
        let entries: Vec<HeadIndexEntry> = bincode::deserialize(&bytes[4..]).map_err(|e| {
            StorageError::IoError(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Failed to deserialize head index entries: {}", e),
            ))
        })?;

        let mut heads = HashMap::new();
        for entry in entries {
            if entry.frame_id.len() != 32 || entry.node_id.len() != 32 {
                return Err(StorageError::IoError(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    "Invalid frame_id or node_id length in head index".to_string(),
                )));
            }
            let mut node_id = [0u8; 32];
            node_id.copy_from_slice(&entry.node_id);
            let mut frame_id = [0u8; 32];
            frame_id.copy_from_slice(&entry.frame_id);
            heads.insert(
                (node_id, entry.frame_type),
                HeadEntry {
                    frame_id,
                    tombstoned_at: entry.tombstoned_at,
                },
            );
        }

        Ok(HeadIndex { heads })
    }

    /// Save head index to disk atomically
    ///
    /// Uses temporary file + rename for atomic writes.
    pub fn save_to_disk<P: AsRef<Path>>(&self, path: P) -> Result<(), StorageError> {
        let path = path.as_ref();

        // Create parent directories if needed
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).map_err(|e| {
                StorageError::IoError(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    format!("Failed to create parent directory {:?}: {}", parent, e),
                ))
            })?;
        }

        let mut entries = Vec::new();
        for ((node_id, frame_type), head_entry) in &self.heads {
            entries.push(HeadIndexEntry {
                node_id: node_id.to_vec(),
                frame_type: frame_type.clone(),
                frame_id: head_entry.frame_id.to_vec(),
                tombstoned_at: head_entry.tombstoned_at,
            });
        }

        // V2 format: 4-byte version then bincode(entries).
        let payload = bincode::serialize(&entries).map_err(|e| {
            StorageError::IoError(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!("Failed to serialize head index entries: {}", e),
            ))
        })?;
        let mut serialized = Vec::with_capacity(4 + payload.len());
        serialized.extend_from_slice(&HEAD_INDEX_VERSION_V2.to_le_bytes());
        serialized.extend_from_slice(&payload);

        // Write to temporary file (atomic write)
        let temp_path = path.with_extension("bin.tmp");
        fs::write(&temp_path, &serialized).map_err(|e| {
            StorageError::IoError(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!("Failed to write head index to {:?}: {}", temp_path, e),
            ))
        })?;

        // Atomically rename temp file to final location
        fs::rename(&temp_path, path).map_err(|e| {
            // Clean up temp file on error
            let _ = fs::remove_file(&temp_path);
            StorageError::IoError(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!("Failed to rename temp file to {:?}: {}", path, e),
            ))
        })?;

        Ok(())
    }
}

/// Persistence format for head index (version 1: legacy single-blob).
#[derive(Debug, Clone, Serialize, Deserialize)]
struct HeadIndexPersistenceV1 {
    version: u32,
    entries: Vec<HeadIndexEntryV1>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct HeadIndexEntryV1 {
    node_id: Vec<u8>,
    frame_type: String,
    frame_id: Vec<u8>,
}

/// Entry in the head index persistence format (version 2).
#[derive(Debug, Clone, Serialize, Deserialize)]
struct HeadIndexEntry {
    node_id: Vec<u8>,
    frame_type: String,
    frame_id: Vec<u8>,
    tombstoned_at: Option<u64>,
}

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

    #[test]
    fn test_save_and_load_head_index() {
        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("head_index.bin");

        // Create a head index with some entries
        let mut index = HeadIndex::new();
        let node_id: NodeID = [1u8; 32];
        let frame_id: FrameID = [2u8; 32];
        index.update_head(&node_id, "test", &frame_id).unwrap();

        // Save to disk
        index.save_to_disk(&path).unwrap();
        assert!(path.exists());

        // Load from disk
        let loaded = HeadIndex::load_from_disk(&path).unwrap();
        assert_eq!(loaded.heads.len(), 1);
        assert_eq!(loaded.get_head(&node_id, "test").unwrap(), Some(frame_id));
    }

    #[test]
    fn test_load_nonexistent_head_index() {
        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("nonexistent.bin");

        // Load from non-existent file should return empty index
        let loaded = HeadIndex::load_from_disk(&path).unwrap();
        assert_eq!(loaded.heads.len(), 0);
    }

    #[test]
    fn test_persistence_path() {
        let workspace_root = std::path::Path::new("/workspace");
        let path = HeadIndex::persistence_path(workspace_root);
        assert!(path.to_string_lossy().ends_with(".meld/head_index.bin"));
    }

    #[test]
    fn test_save_and_load_multiple_entries() {
        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("head_index.bin");

        // Create a head index with multiple entries
        let mut index = HeadIndex::new();
        let node_id1: NodeID = [1u8; 32];
        let node_id2: NodeID = [2u8; 32];
        let frame_id1: FrameID = [10u8; 32];
        let frame_id2: FrameID = [20u8; 32];
        let frame_id3: FrameID = [30u8; 32];

        index.update_head(&node_id1, "type1", &frame_id1).unwrap();
        index.update_head(&node_id1, "type2", &frame_id2).unwrap();
        index.update_head(&node_id2, "type1", &frame_id3).unwrap();

        // Save to disk
        index.save_to_disk(&path).unwrap();

        // Load from disk
        let loaded = HeadIndex::load_from_disk(&path).unwrap();
        assert_eq!(loaded.heads.len(), 3);
        assert_eq!(
            loaded.get_head(&node_id1, "type1").unwrap(),
            Some(frame_id1)
        );
        assert_eq!(
            loaded.get_head(&node_id1, "type2").unwrap(),
            Some(frame_id2)
        );
        assert_eq!(
            loaded.get_head(&node_id2, "type1").unwrap(),
            Some(frame_id3)
        );
    }

    #[test]
    fn test_tombstone_and_restore_heads() {
        let mut index = HeadIndex::new();
        let node_id: NodeID = [1u8; 32];
        let frame_id: FrameID = [2u8; 32];
        index.update_head(&node_id, "test", &frame_id).unwrap();
        assert_eq!(index.get_head(&node_id, "test").unwrap(), Some(frame_id));
        index.tombstone_heads_for_node(&node_id);
        assert_eq!(index.get_head(&node_id, "test").unwrap(), None);
        assert_eq!(index.get_active_head(&node_id, "test").unwrap(), None);
        index.restore_heads_for_node(&node_id);
        assert_eq!(index.get_head(&node_id, "test").unwrap(), Some(frame_id));
    }

    #[test]
    fn test_purge_tombstoned() {
        let mut index = HeadIndex::new();
        let node_id: NodeID = [1u8; 32];
        let frame_id: FrameID = [2u8; 32];
        index.update_head(&node_id, "test", &frame_id).unwrap();
        index.tombstone_heads_for_node(&node_id);
        let ts = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        index.purge_tombstoned(ts);
        assert_eq!(index.heads.len(), 0);
    }
}