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
use crate::mcp_server::capnp_conversion::{
deserialize_state_from_capnp, get_serialization_format, serialize_state_to_capnp,
};
use crate::models::refactor::RefactorStateMachine;
use std::fs;
use std::path::PathBuf;
use tracing::{debug, info};
/// Snapshot manager.
pub struct SnapshotManager {
snapshot_path: PathBuf,
}
impl SnapshotManager {
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
/// Create a new instance.
pub fn new() -> Self {
Self::with_path(".pmat-cache")
}
/// With path.
pub fn with_path<P: Into<PathBuf>>(cache_dir: P) -> Self {
let snapshot_dir = cache_dir.into();
// Ensure cache directory exists
if !snapshot_dir.exists() {
fs::create_dir_all(&snapshot_dir).ok();
}
Self {
snapshot_path: snapshot_dir.join("refactor-state.bin"),
}
}
/// Saves a refactor state machine snapshot to disk
///
/// # Examples
///
/// ```no_run
/// use pmat::mcp_server::snapshots::SnapshotManager;
/// use pmat::models::refactor::{RefactorStateMachine, RefactorConfig};
/// use std::path::PathBuf;
/// use tempfile::tempdir;
///
/// let dir = tempdir().unwrap();
/// let manager = SnapshotManager::with_path(dir.path());
///
/// let state = RefactorStateMachine::new(
/// vec![PathBuf::from("src/main.rs")],
/// RefactorConfig::default()
/// );
///
/// let result = manager.save_snapshot(&state);
/// assert!(result.is_ok());
/// ```
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn save_snapshot(&self, state: &RefactorStateMachine) -> Result<(), String> {
debug!(
"Saving refactor state snapshot to {:?} using {}",
self.snapshot_path,
get_serialization_format()
);
// Ensure parent directory exists before writing
if let Some(parent) = self.snapshot_path.parent() {
fs::create_dir_all(parent)
.map_err(|e| format!("Failed to create snapshot directory: {e}"))?;
}
// Use Cap'n Proto serialization with JSON fallback
let data = serialize_state_to_capnp(state)?;
// Use safe two-phase write: create .tmp file, then rename
let temp_path = self.snapshot_path.with_extension("tmp");
fs::write(&temp_path, data).map_err(|e| format!("Failed to write snapshot: {e}"))?;
fs::rename(&temp_path, &self.snapshot_path)
.map_err(|e| format!("Failed to rename snapshot: {e}"))?;
info!(
"Saved refactor state snapshot using {}",
get_serialization_format()
);
Ok(())
}
/// Loads a refactor state machine snapshot from disk
///
/// # Examples
///
/// ```no_run
/// use pmat::mcp_server::snapshots::SnapshotManager;
/// use tempfile::tempdir;
///
/// let dir = tempdir().unwrap();
/// let manager = SnapshotManager::with_path(dir.path());
///
/// match manager.load_snapshot() {
/// Ok(state) => println!("Loaded state"),
/// Err(e) => println!("No snapshot found: {}", e),
/// }
/// ```
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn load_snapshot(&self) -> Result<RefactorStateMachine, String> {
debug!(
"Loading refactor state snapshot from {:?} using {}",
self.snapshot_path,
get_serialization_format()
);
if !self.snapshot_path.exists() {
return Err("No snapshot file found".to_string());
}
// Use Cap'n Proto deserialization with JSON fallback
let data =
fs::read(&self.snapshot_path).map_err(|e| format!("Failed to read snapshot: {e}"))?;
let state = deserialize_state_from_capnp(&data)?;
info!(
"Loaded refactor state snapshot using {}",
get_serialization_format()
);
Ok(state)
}
/// Removes a saved snapshot from disk
///
/// # Examples
///
/// ```no_run
/// use pmat::mcp_server::snapshots::SnapshotManager;
/// use tempfile::tempdir;
///
/// let dir = tempdir().unwrap();
/// let manager = SnapshotManager::with_path(dir.path());
///
/// let result = manager.remove_snapshot();
/// // Ok if file was removed or didn't exist
/// ```
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn remove_snapshot(&self) -> Result<(), String> {
if self.snapshot_path.exists() {
fs::remove_file(&self.snapshot_path)
.map_err(|e| format!("Failed to remove snapshot: {e}"))?;
info!("Removed refactor state snapshot");
}
Ok(())
}
}
impl Default for SnapshotManager {
fn default() -> Self {
Self::new()
}
}
// Cap'n Proto serialization is implemented via capnp_conversion module
// Falls back to JSON when Cap'n Proto is not available
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod property_tests {
use proptest::prelude::*;
proptest! {
#[test]
fn basic_property_stability(_input in ".*") {
// Basic property test for coverage
prop_assert!(true);
}
#[test]
fn module_consistency_check(_x in 0u32..1000) {
// Module consistency verification
prop_assert!(_x < 1001);
}
}
}