MCPcopy Create free account
hub / github.com/AI45Lab/Code / evict_if_needed

Method evict_if_needed

core/src/file_history.rs:116–142  ·  view source on GitHub ↗

Evict oldest snapshots when over the limit

(&self, snapshots: &mut HashMap<String, Vec<FileSnapshot>>)

Source from the content-addressed store, hash-verified

114
115 /// Evict oldest snapshots when over the limit
116 fn evict_if_needed(&self, snapshots: &mut HashMap<String, Vec<FileSnapshot>>) {
117 let total: usize = snapshots.values().map(|v| v.len()).sum();
118 if total <= self.max_snapshots {
119 return;
120 }
121
122 let to_remove = total - self.max_snapshots;
123
124 // Collect all snapshots with their file path, sorted by timestamp
125 let mut all_entries: Vec<(String, usize, DateTime<Utc>)> = Vec::new();
126 for (path, versions) in snapshots.iter() {
127 for snapshot in versions {
128 all_entries.push((path.clone(), snapshot.version, snapshot.timestamp));
129 }
130 }
131 all_entries.sort_by_key(|e| e.2);
132
133 // Remove the oldest entries
134 for (path, version, _) in all_entries.into_iter().take(to_remove) {
135 if let Some(versions) = snapshots.get_mut(&path) {
136 versions.retain(|s| s.version != version);
137 if versions.is_empty() {
138 snapshots.remove(&path);
139 }
140 }
141 }
142 }
143}
144
145/// Check if a tool name is a file-modifying tool that should trigger snapshots

Callers 1

save_snapshotMethod · 0.80

Calls 4

removeMethod · 0.80
lenMethod · 0.45
cloneMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected