Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Persist generation effects fingerprint cache

Bitti766151b9be1c
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 7032321..f200578 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -50,5 +50,5 @@ Changes since 0.3.2.
- Updated First/Last Frame Fast to the eight-step FastH3 Omni model and corrected Omni and First/Last Frame weight fallbacks.
- Image edits preserve source images, prompts and settings; queued results get separate entries, and whole-image edits disable feathering.
- Imported images now expose steps, seed and resolution; new images use generator step defaults, and renamed image roots retain their names.
-- Generation effects reuse VAE fingerprint checks to avoid repeated calibration work.
+- Generation effects reuse VAE fingerprint checks across app and worker restarts, avoiding repeated reads of unchanged VAE files while still detecting edits and replacements.
- Updated SlopFab to API 1.40 with music performance improvements; LAN protocol 19. Update workers alongside the app.
diff --git a/src-tauri/core/src/slopfab/lcs.rs b/src-tauri/core/src/slopfab/lcs.rs
index b5c4461..e2a2ba1 100644
--- a/src-tauri/core/src/slopfab/lcs.rs
+++ b/src-tauri/core/src/slopfab/lcs.rs
@@ -6,7 +6,7 @@ fn calibration_path(directory: &Path, vae: &Path, cancel: &AtomicBool, mut check
use super::lcs_fingerprint::{check_cancel, Fingerprints};
static FINGERPRINTS: std::sync::OnceLock<std::sync::Mutex<Fingerprints>> = std::sync::OnceLock::new();
let digest = FINGERPRINTS.get_or_init(Default::default).lock().map_err(|_| "VAE fingerprint cache lock failed.")?
- .get(vae, cancel, |source| {
+ .get(directory, vae, cancel, |source| {
checking();
let mut hash = Sha256::new();
let mut buffer = vec![0; 1024 * 1024];
diff --git a/src-tauri/core/src/slopfab/lcs_fingerprint.rs b/src-tauri/core/src/slopfab/lcs_fingerprint.rs
index 4faaa3e..670d574 100644
--- a/src-tauri/core/src/slopfab/lcs_fingerprint.rs
+++ b/src-tauri/core/src/slopfab/lcs_fingerprint.rs
@@ -1,13 +1,45 @@
-//! Avoid rereading multi-GB VAEs while their identity and change stamps match.
-use std::{collections::BTreeMap, fs::File, path::{Path, PathBuf}, sync::atomic::{AtomicBool, Ordering}, time::SystemTime};
+//! Avoid rereading multi-GB VAEs while their identity and change stamps match,
+//! including after an app or worker restart. Native LCS validation remains independent.
+use serde::{Deserialize, Serialize};
+use sha2::{Digest, Sha256};
+use std::{collections::BTreeMap, fs::File, io::{Read, Write}, path::{Path, PathBuf}, sync::atomic::{AtomicBool, Ordering}, time::SystemTime};
-#[derive(PartialEq, Eq)]
+#[derive(PartialEq, Eq, Serialize, Deserialize)]
struct FileStamp {
bytes: u64,
modified: SystemTime,
identity: [u64; 4],
}
+#[derive(Serialize, Deserialize)]
+struct SavedFingerprint {
+ path: PathBuf,
+ stamp: FileStamp,
+ digest: String,
+}
+
+fn cache_path(directory: &Path, path: &Path) -> PathBuf {
+ let key = Sha256::digest(path.to_string_lossy().as_bytes());
+ directory.join(format!("vae-fingerprint-v1-{key:x}.json"))
+}
+
+fn read_saved(path: &Path) -> Option<SavedFingerprint> {
+ // A corrupt cache is a miss, never a reason to fail generation or allocate
+ // an unbounded buffer. Identity checks below also cover replacement files.
+ let mut bytes = Vec::new();
+ File::open(path).ok()?.take(16 * 1024).read_to_end(&mut bytes).ok()?;
+ let saved: SavedFingerprint = serde_json::from_slice(&bytes).ok()?;
+ (saved.digest.len() == 64 && saved.digest.bytes().all(|b| b.is_ascii_hexdigit())).then_some(saved)
+}
+
+fn write_saved(path: &Path, saved: &SavedFingerprint) -> Result<(), String> {
+ let mut file = tempfile::NamedTempFile::new_in(path.parent().ok_or("Missing fingerprint directory")?).map_err(|e| e.to_string())?;
+ serde_json::to_writer(&mut file, saved).map_err(|e| e.to_string())?;
+ file.flush().map_err(|e| e.to_string())?;
+ file.persist(path).map_err(|e| e.to_string())?;
+ Ok(())
+}
+
impl FileStamp {
fn read(file: &File) -> Result<Self, String> {
let metadata = file.metadata().map_err(|e| e.to_string())?;
@@ -41,7 +73,7 @@ pub(super) fn check_cancel(cancel: &AtomicBool) -> Result<(), String> {
}
impl Fingerprints {
- pub(super) fn get(&mut self, vae: &Path, cancel: &AtomicBool, hash: impl FnOnce(&mut File) -> Result<String, String>) -> Result<String, String> {
+ pub(super) fn get(&mut self, directory: &Path, vae: &Path, cancel: &AtomicBool, hash: impl FnOnce(&mut File) -> Result<String, String>) -> Result<String, String> {
check_cancel(cancel)?;
let path = vae.canonicalize().map_err(|e| format!("Cannot locate the VAE for generation effects: {e}"))?;
let mut file = File::open(&path).map_err(|e| format!("Cannot read the VAE for generation effects: {e}"))?;
@@ -51,13 +83,26 @@ impl Fingerprints {
}
// Remove obsolete entries even if hashing is subsequently cancelled.
self.0.remove(&path);
+ let saved_path = cache_path(directory, &path);
+ if let Some(saved) = read_saved(&saved_path) {
+ if saved.path == path && saved.stamp == stamp {
+ check_cancel(cancel)?;
+ if self.0.len() >= 32 { self.0.clear(); }
+ self.0.insert(path, (stamp, saved.digest.clone()));
+ return Ok(saved.digest);
+ }
+ }
let digest = hash(&mut file)?;
check_cancel(cancel)?;
if FileStamp::read(&file)? != stamp || FileStamp::read(&File::open(&path).map_err(|e| e.to_string())?)? != stamp {
return Err("The VAE changed while checking generation effects. Retry generation.".into());
}
if self.0.len() >= 32 { self.0.clear(); }
- self.0.insert(path, (stamp, digest.clone()));
+ let saved = SavedFingerprint { path, stamp, digest: digest.clone() };
+ // Cache writes are optional: read-only/full cache storage must not stop
+ // a valid generation. Publish atomically so other workers see whole files.
+ let _ = write_saved(&saved_path, &saved);
+ self.0.insert(saved.path, (saved.stamp, digest.clone()));
Ok(digest)
}
}
@@ -69,35 +114,81 @@ mod tests {
fn hash(file: &mut File) -> Result<String, String> {
let mut value = String::new(); file.read_to_string(&mut value).map_err(|e| e.to_string())?; Ok(value)
}
+ fn sha256(file: &mut File) -> Result<String, String> {
+ Ok(format!("{:x}", Sha256::digest(hash(file)?.as_bytes())))
+ }
+ #[test]
+ fn restart_reuses_saved_fingerprint_but_edits_and_replacements_invalidate() {
+ let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("vae");
+ fs::write(&path, "first").unwrap();
+ let cancel = AtomicBool::new(false);
+ let first = Fingerprints::default().get(dir.path(), &path, &cancel, sha256).unwrap();
+ assert_eq!(Fingerprints::default().get(dir.path(), &path, &cancel, |_| panic!("restart must reuse the saved fingerprint")).unwrap(), first);
+ let time = fs::metadata(&path).unwrap().modified().unwrap();
+ fs::write(&path, "other").unwrap();
+ File::options().write(true).open(&path).unwrap().set_modified(time).unwrap();
+ let second = Fingerprints::default().get(dir.path(), &path, &cancel, sha256).unwrap();
+ assert_ne!(second, first);
+ let replacement = dir.path().join("replacement"); fs::write(&replacement, "third").unwrap();
+ File::options().write(true).open(&replacement).unwrap().set_modified(time).unwrap();
+ fs::remove_file(&path).unwrap(); fs::rename(replacement, &path).unwrap();
+ let third = Fingerprints::default().get(dir.path(), &path, &cancel, sha256).unwrap();
+ assert_ne!(third, second);
+ assert_eq!(Fingerprints::default().get(dir.path(), &path, &cancel, |_| panic!("replacement fingerprint must be saved")).unwrap(), third);
+ cancel.store(true, Ordering::Release);
+ assert!(Fingerprints::default().get(dir.path(), &path, &cancel, |_| panic!("cancel before disk lookup")).is_err());
+ }
+ #[test]
+ fn corrupt_saved_fingerprints_are_rebuilt_and_cache_writes_are_optional() {
+ let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("vae");
+ fs::write(&path, "first").unwrap();
+ let cancel = AtomicBool::new(false);
+ let expected = Fingerprints::default().get(dir.path(), &path, &cancel, sha256).unwrap();
+ let saved_path = cache_path(dir.path(), &path.canonicalize().unwrap());
+ fs::write(&saved_path, "{truncated").unwrap();
+ assert_eq!(Fingerprints::default().get(dir.path(), &path, &cancel, sha256).unwrap(), expected);
+ let mut saved = read_saved(&saved_path).unwrap();
+ saved.digest = "invalid".into();
+ write_saved(&saved_path, &saved).unwrap();
+ assert_eq!(Fingerprints::default().get(dir.path(), &path, &cancel, sha256).unwrap(), expected);
+ let mut saved = read_saved(&saved_path).unwrap();
+ saved.path = dir.path().join("another-vae");
+ write_saved(&saved_path, &saved).unwrap();
+ assert_eq!(Fingerprints::default().get(dir.path(), &path, &cancel, sha256).unwrap(), expected);
+ assert_eq!(Fingerprints::default().get(dir.path(), &path, &cancel, |_| panic!("repaired cache must be reused")).unwrap(), expected);
+ // The VAE itself is a regular file, so it cannot hold a cache file.
+ assert_eq!(Fingerprints::default().get(&path, &path, &cancel, sha256).unwrap(), expected);
+ }
#[test]
fn unchanged_vae_is_not_read_and_replacements_and_edits_invalidate() {
let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("vae");
fs::write(&path, "first").unwrap();
let cancel = AtomicBool::new(false); let mut cache = Fingerprints::default();
- assert_eq!(cache.get(&path, &cancel, hash).unwrap(), "first");
- assert_eq!(cache.get(&path, &cancel, |_| panic!("unchanged VAE must not be read")).unwrap(), "first");
+ assert_eq!(cache.get(dir.path(), &path, &cancel, hash).unwrap(), "first");
+ assert_eq!(cache.get(dir.path(), &path, &cancel, |_| panic!("unchanged VAE must not be read")).unwrap(), "first");
let time = fs::metadata(&path).unwrap().modified().unwrap();
fs::write(&path, "other").unwrap();
File::options().write(true).open(&path).unwrap().set_modified(time).unwrap();
- assert_eq!(cache.get(&path, &cancel, hash).unwrap(), "other");
+ assert_eq!(cache.get(dir.path(), &path, &cancel, hash).unwrap(), "other");
let replacement = dir.path().join("replacement"); fs::write(&replacement, "third").unwrap();
File::options().write(true).open(&replacement).unwrap().set_modified(time).unwrap();
fs::remove_file(&path).unwrap(); fs::rename(replacement, &path).unwrap();
- assert_eq!(cache.get(&path, &cancel, hash).unwrap(), "third");
+ assert_eq!(cache.get(dir.path(), &path, &cancel, hash).unwrap(), "third");
fs::remove_file(&path).unwrap();
- assert!(cache.get(&path, &cancel, hash).is_err());
+ assert!(cache.get(dir.path(), &path, &cancel, hash).is_err());
}
#[test]
fn failed_cancelled_and_changed_reads_are_never_cached() {
let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("vae");
fs::write(&path, "first").unwrap();
let cancel = AtomicBool::new(false); let mut cache = Fingerprints::default();
- assert!(cache.get(&path, &cancel, |_| Err("read error".into())).is_err());
- assert!(cache.get(&path, &cancel, |_| { cancel.store(true, Ordering::Release); Ok("bad".into()) }).is_err());
+ assert!(cache.get(dir.path(), &path, &cancel, |_| Err("read error".into())).is_err());
+ assert!(cache.get(dir.path(), &path, &cancel, |_| { cancel.store(true, Ordering::Release); Ok("bad".into()) }).is_err());
cancel.store(false, Ordering::Release);
- assert!(cache.get(&path, &cancel, |_| { fs::write(&path, "changed").unwrap(); Ok("bad".into()) }).is_err());
- assert_eq!(cache.get(&path, &cancel, hash).unwrap(), "changed");
+ assert!(cache.get(dir.path(), &path, &cancel, |_| { fs::write(&path, "changed").unwrap(); Ok("bad".into()) }).is_err());
+ assert!(!cache_path(dir.path(), &path.canonicalize().unwrap()).exists());
+ assert_eq!(cache.get(dir.path(), &path, &cancel, hash).unwrap(), "changed");
cancel.store(true, Ordering::Release);
- assert!(cache.get(&path, &cancel, |_| panic!("cancel before cache lookup")).is_err());
+ assert!(cache.get(dir.path(), &path, &cancel, |_| panic!("cancel before cache lookup")).is_err());
}
}