use std::collections::BTreeMap;
use std::path::Path;
use anyhow::{Context, Result};
use perspt_sdk::srbn;
use sha2::{Digest, Sha256};
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct WorkspaceState {
pub files: BTreeMap<String, Option<String>>,
}
impl WorkspaceState {
pub fn root_hash(&self) -> String {
let mut hasher = Sha256::new();
for (path, hash) in &self.files {
hasher.update(path.as_bytes());
hasher.update([0u8]);
hasher.update(hash.as_deref().unwrap_or("absent").as_bytes());
hasher.update([0u8]);
}
hex_digest(hasher)
}
}
pub fn snapshot_workspace(root: &Path, paths: &[String]) -> Result<WorkspaceState> {
let mut files = BTreeMap::new();
for rel in paths {
let absolute = root.join(rel);
let hash = match std::fs::read(&absolute) {
Ok(bytes) => {
let mut hasher = Sha256::new();
hasher.update(&bytes);
Some(hex_digest(hasher))
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
Err(e) => return Err(e).with_context(|| format!("re-reading {rel:?}")),
};
files.insert(rel.clone(), hash);
}
Ok(WorkspaceState { files })
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProjectionMismatch {
pub denied_proposals: u32,
pub stale_witnesses: u32,
pub ambiguous_edits: u32,
pub formatter_rewrites: u32,
pub partial_applications: u32,
}
impl ProjectionMismatch {
pub fn total(&self) -> u32 {
self.denied_proposals
+ self.stale_witnesses
+ self.ambiguous_edits
+ self.formatter_rewrites
+ self.partial_applications
}
}
pub struct SnapshotRealizer;
impl srbn::Realizer<WorkspaceState> for SnapshotRealizer {
fn realize(
&mut self,
proposed: WorkspaceState,
) -> srbn::SrbnResult<srbn::Realization<WorkspaceState>> {
Ok(srbn::Realization::unmeasured(proposed))
}
}
fn hex_digest(hasher: Sha256) -> String {
hasher
.finalize()
.iter()
.map(|b| format!("{b:02x}"))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn snapshot_reads_what_is_actually_on_disk() {
let dir = std::env::temp_dir().join(format!("perspt_realize_{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("a.rs"), "fn a() {}").unwrap();
let state =
snapshot_workspace(&dir, &["a.rs".to_string(), "missing.rs".to_string()]).unwrap();
assert!(state.files["a.rs"].is_some());
assert!(state.files["missing.rs"].is_none());
let again =
snapshot_workspace(&dir, &["a.rs".to_string(), "missing.rs".to_string()]).unwrap();
assert_eq!(state.root_hash(), again.root_hash());
std::fs::write(dir.join("a.rs"), "fn a() { changed() }").unwrap();
let changed = snapshot_workspace(&dir, &["a.rs".to_string()]).unwrap();
assert_ne!(state.files["a.rs"], changed.files["a.rs"]);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn root_hash_is_order_independent_by_construction() {
let mut a = WorkspaceState::default();
a.files.insert("x".into(), Some("h1".into()));
a.files.insert("y".into(), None);
let mut b = WorkspaceState::default();
b.files.insert("y".into(), None);
b.files.insert("x".into(), Some("h1".into()));
assert_eq!(a.root_hash(), b.root_hash());
}
#[test]
fn kernel_conformance_loop_runs_over_snapshots() {
let params = perspt_sdk::StabilityParameters::measured(0.5, 0.0);
let initial = WorkspaceState::default();
let barrier = |state: &WorkspaceState| {
let v = state.files.len() as f64;
perspt_sdk::AgentBarrierResult::new(state.files.is_empty(), v * v)
};
let updater = |mut state: WorkspaceState,
_b: &srbn::BarrierResult<
perspt_sdk::CorrectionDirectionSet,
perspt_sdk::Evidence,
>| {
state.files.pop_last();
Ok(state)
};
let mut start = initial;
for i in 0..3 {
start.files.insert(format!("f{i}"), None);
}
let result =
perspt_sdk::stabilize_realized(start, barrier, updater, SnapshotRealizer, ¶ms, 8)
.unwrap();
assert_eq!(result.status, srbn::Status::Stable);
assert!(result.max_realizability_residual().is_none(), "unmeasured");
}
}