AI media harness for planning, generating, and editing locally
TypeScript
Add configurable LCS Drift effect
Bitti771422985e02
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.mdindex 155ebba..6b16125 100644--- a/RELEASE_NOTES.md+++ b/RELEASE_NOTES.md@@ -13,7 +13,7 @@ Changes since 0.3.2.- **Motions and agents:** Create editable JavaScript animations for the timeline or references. Agents can inspect media frames, generate music and sync Motions to audio.- **Image editing:** Combine or subtract lasso selections, queue batches of up to 100 images, and edit bounded regions of larger images while preserving their original resolution.- **Live previews:** Watch MiniMax H3 video and image generation in the editor. Cancelling a video generation saves its latest available preview as the Take's video. Optional TAEH3 preview weights are available in Settings.-- **Generation effects:** LCS color steering, tone, sharpness and color stabilization, with step ranges and optional regions.+- **Generation effects:** LCS color steering, tone, sharpness and color stabilization, with step ranges and optional regions. LCS Drift adds configurable highlight compression and learned drift correction using an imported scene calibration, including on LAN workers.- **Generator controls:** Compatible sampler/scheduler choices, including ER-SDE and Beta; global LoRAs, application ordering, scene overrides, Pika H3 Flex and Hyperflow 8-Step adapters.- **Generator selection:** Video, image and music remember separate active generators. Saved takes show the used seed and generator, with a button to select that generator again.- **Speech languages:** Set custom speech languages for shots.diff --git a/docs/slopfab-compatibility.md b/docs/slopfab-compatibility.mdindex f01ac42..e2b5440 100644--- a/docs/slopfab-compatibility.md+++ b/docs/slopfab-compatibility.md@@ -2,7 +2,7 @@## Bundled runtime (API 1.48.0)-Windows and Linux runtimes use SlopFab commit `c5297c243923f336d4f174980a80452f0003bd0f`+Windows and Linux runtimes use SlopFab commit `54362786a76e7530f7216dfeca130835a8f298f3`from `D:\Projects\slopfab`, with CUDA and Vulkan enabled and FFmpeg disabled.This update keeps Keystone and delayed Capstone context inside sampling: theoriginal front latent is restored before output, and Capstone adds one clean@@ -15,7 +15,7 @@ Video previews autoplay and loop inside the existing shot card player; imagepreviews update the image canvas and thumbnail. Both use the generation previewssetting and require the optional TAEH3 weights. Saved-latent upscaling remains supported.-LAN protocol is 29 so workers use the same Keystone conditioning, image crop+LAN protocol is 30 so workers support portable LCS Drift calibration and use the same Keystone conditioning, image cropmerging, Single Shot Tone and overlap behavior. Single Shot automatically shortens its preferred four-updateCapstone delay when the resolved video schedule is shorter, without changing theLoRA recipe. Restart Slopus and update workers to load the new runtime.@@ -118,7 +118,7 @@ appear in Music and timeline media. Song settings persist in `musicSongs`.## Generation effects (API 1.31)Scene and image root inspectors provide **Effects > Add effect** for native-LCS color steering, tone, experimental sharpness, and self/smooth color+LCS color steering, tone, experimental LCS Drift and sharpness, and self/smooth colorstabilization. Cards support bypass, reset and removal. A shared inclusivestep interval (zero-based) and optional percentage rectangle apply to allactive cards; the rectangle can be inverted. Neutral and bypassed controls@@ -145,7 +145,7 @@ buffers. Corrupt archives are regenerated. Planning never calibrates orruns inference. The generation request snapshots the archive before sampling;region masks use fractional coverage of the resolved latent patch grid.-LAN worker protocol 11 carries the portable controls and prevents older+LAN worker protocol 30 carries the portable controls and fitted drift archive and prevents olderworkers from silently dropping them. Each worker calibrates and caches itsown VAE. Update workers alongside the desktop app. A runtime missing the LCSexports reports an explicit upgrade error.@@ -158,6 +158,37 @@ serialization, mask validation, calibration cancellation and callback paniccontainment through the bundled DLL. Full generation and visual LCS qualitywith model weights were not evaluated in this integration.+### LCS Drift++The LCS Drift card exposes independent highlight strength (0–1), highlight+threshold (0–2), learned drift strength (0–1), and maximum correction (0–10 raw+VAE units). It defaults to highlight strength 0.5 and threshold 0.8; learned+correction defaults to zero. Highlight compression uses the automatic VAE+calibration and needs no imported file. Both effects run during sampling,+including Single Shot, and honor the shared step range and region mask.++Learned correction requires **Import drift calibration**: a SlopFab v1 LCS+`.safetensors` archive fitted from early and late latent generations, containing+`drift_direction` and `drift_reference`. SlopFab's `tools/lcs_drift.py fit` produces+this archive. Slopus validates its size, tensor shapes, finite values, unit+direction and color orthogonality, then embeds the complete small archive as+base64 in the scene. It survives save/load, Take snapshots and worker transfer;+there is no dependency on the importing machine's file path. Importing does not+enable learned correction until its strength is increased. Removing the file+resets learned strength to zero. Bypassed and neutral effects do no work.++For active learned correction, the worker attaches the imported calibration+instead of the automatic cache. SlopFab validates its full contents and checks+the VAE SHA-256 before sampling. All active LCS cards use that archive, so include+its sharpness calibration if also using Sharpness. The correction is specific+to the fitted scene/checkpoint and VAE; its quality is experimental, and Slopus+does not automatically fit a direction or choose an optimal step interval.+The upstream drift experiment documents the fitting workflow and measured limits.++The updated Windows and Linux bundles pass 23,119 native LCS checks each, plus+C API and tokenizer tests. Slopus tests exercise controls, portable archive+validation, persistence, fingerprints, worker serialization and native attachment.+## Local edits on larger imagesWhen an image editor's selected resolution is smaller than its source, rectanglediff --git a/fixtures/generation-effects.json b/fixtures/generation-effects.jsonindex bafc5ab..af6358b 100644--- a/fixtures/generation-effects.json+++ b/fixtures/generation-effects.json@@ -3,6 +3,7 @@"tone": { "contrast": 1.25, "brightness": -0.1, "saturation": 0.9, "temperature": 0.2 },"sharpness": { "strength": 0.1, "enabled": false },"anchor": { "mode": "self", "strength": 0.3 },+ "drift": {"strength":0.5,"max":1,"highlightStrength":0.5,"highlightKnee":0.8,"calibration":{"name":"test-drift.safetensors","data":"qAIAAAAAAAB7Il9fbWV0YWRhdGFfXyI6eyJzbG9wZmFiX2xjcyI6IjEiLCJ2YWVfc2hhMjU2IjoiYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYSJ9LCJiYXNpcyI6eyJkdHlwZSI6IkYzMiIsInNoYXBlIjpbOTYsM10sImRhdGFfb2Zmc2V0cyI6WzAsMTE1Ml19LCJtZWFuIjp7ImR0eXBlIjoiRjMyIiwic2hhcGUiOls5Nl0sImRhdGFfb2Zmc2V0cyI6WzExNTIsMTUzNl19LCJhbmNob3JfbGNzIjp7ImR0eXBlIjoiRjMyIiwic2hhcGUiOls4LDNdLCJkYXRhX29mZnNldHMiOlsxNTM2LDE2MzJdfSwibGF0ZW50X21lYW4iOnsiZ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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/AACAPg=="}},"startStep": 2,"endStep": 12,"region": { "x": 25, "y": 0, "width": 50, "height": 100, "invert": false }diff --git a/lib/slopfab/libslopfab.so b/lib/slopfab/libslopfab.soindex bbd8e91..15a38a4 100644--- a/lib/slopfab/libslopfab.so+++ b/lib/slopfab/libslopfab.so@@ -1,3 +1,3 @@version https://git-lfs.github.com/spec/v1-oid sha256:9ff56c773b3786d6f7fb7f25853b58e354d1fdb6becb933c7cfccc948a2fc58a-size 24086384+oid sha256:c9528eda973597e56582f008d9c193f8249ef3a96f2b12753949bebd5c730c1a+size 24094792diff --git a/lib/slopfab/slopfab.dll b/lib/slopfab/slopfab.dllindex dc418fb..19c760b 100644--- a/lib/slopfab/slopfab.dll+++ b/lib/slopfab/slopfab.dll@@ -1,3 +1,3 @@version https://git-lfs.github.com/spec/v1-oid sha256:180652874381eee566c51da8e77ce3444dcd6ed175b3f2a4c15532d7c8cb6cd1-size 20452864+oid sha256:d867c9ccf300fd3e2adb119033eb8ca2f56431ac5883c419895818065177fe03+size 20457984diff --git a/src-tauri/core/src/generation_effects.rs b/src-tauri/core/src/generation_effects.rsindex 1ff1700..2a6e377 100644--- a/src-tauri/core/src/generation_effects.rs+++ b/src-tauri/core/src/generation_effects.rs@@ -1,6 +1,8 @@//! Portable authoring settings; calibration files belong to the generation host.use serde::{Deserialize, Serialize};use serde_json::{json, Value};+mod calibration;+pub use calibration::DriftCalibration;#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]#[serde(rename_all = "camelCase", deny_unknown_fields)]@@ -14,6 +16,8 @@ pub struct GenerationEffects {#[serde(default, skip_serializing_if = "Option::is_none")]pub anchor: Option<Anchor>,#[serde(default, skip_serializing_if = "Option::is_none")]+ pub drift: Option<Drift>,+ #[serde(default, skip_serializing_if = "Option::is_none")]pub start_step: Option<i32>,#[serde(default, skip_serializing_if = "Option::is_none")]pub end_step: Option<i32>,@@ -39,6 +43,15 @@ pub struct Strength { #[serde(default, skip_serializing_if = "Option::is_none")]#[serde(deny_unknown_fields)]pub struct Anchor { #[serde(default, skip_serializing_if = "Option::is_none")] pub enabled: Option<bool>, pub mode: String, pub strength: f32 }#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]+#[serde(rename_all = "camelCase", deny_unknown_fields)]+pub struct Drift {+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub enabled: Option<bool>,+ pub strength: f32, pub max: f32, pub highlight_strength: f32, pub highlight_knee: f32,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub calibration: Option<DriftCalibration>,+}+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]#[serde(deny_unknown_fields)]pub struct Region { pub x: f32, pub y: f32, pub width: f32, pub height: f32, pub invert: bool }@@ -51,9 +64,13 @@ impl GenerationEffects {|| self.tone.as_ref().is_some_and(|t| !range(t.contrast, 0.0, 4.0) || !range(t.brightness, -1.0, 1.0) || !range(t.saturation, 0.0, 4.0) || !range(t.temperature, -1.0, 1.0))|| self.sharpness.as_ref().is_some_and(|s| !range(s.strength, -5.0, 5.0))|| self.anchor.as_ref().is_some_and(|a| !range(a.strength, 0.0, 1.0) || !matches!(a.mode.as_str(), "self" | "smooth"))+ || self.drift.as_ref().is_some_and(|d| !range(d.strength, 0.0, 1.0) || !range(d.max, 0.0, 10.0) || !range(d.highlight_strength, 0.0, 1.0) || !range(d.highlight_knee, 0.0, 2.0)+ || d.strength > 0.0 && d.max > 0.0 && d.calibration.is_none())|| self.start_step.unwrap_or(0) < 0 || self.end_step.unwrap_or(i32::MAX) < self.start_step.unwrap_or(0)|| self.region.as_ref().is_some_and(|r| !range(r.x, 0.0, 100.0) || !range(r.y, 0.0, 100.0) || !range(r.width, 0.1, 100.0) || !range(r.height, 0.1, 100.0) || r.x + r.width > 100.0 || r.y + r.height > 100.0);- if invalid { Err("Invalid generation effects settings.".into()) } else { Ok(()) }+ if invalid { return Err("Invalid generation effects settings.".into()); }+ if let Some(calibration) = self.drift.as_ref().and_then(|d| d.calibration.as_ref()) { calibration.bytes()?; }+ Ok(())}/// Translate to the native LCS schema; bypassed/neutral cards do no work.@@ -70,6 +87,10 @@ impl GenerationEffects {}if let Some(s) = self.sharpness.as_ref().filter(|s| s.enabled != Some(false) && s.strength != 0.0) { settings["sharpness"] = json!(s.strength); }if let Some(a) = self.anchor.as_ref().filter(|a| a.enabled != Some(false) && a.strength != 0.0) { settings["anchor"] = json!(a.mode); settings["anchor_strength"] = json!(a.strength); }+ if let Some(d) = self.drift.as_ref().filter(|d| d.enabled != Some(false)) {+ if d.strength > 0.0 && d.max > 0.0 { settings["drift_strength"] = json!(d.strength); settings["drift_max"] = json!(d.max); }+ if d.highlight_strength > 0.0 { settings["highlight_strength"] = json!(d.highlight_strength); settings["highlight_knee"] = json!(d.highlight_knee); }+ }if settings.as_object().unwrap().is_empty() { return Ok(None); }settings["start_step"] = json!(self.start_step.unwrap_or(0));settings["end_step"] = json!(self.end_step.unwrap_or(i32::MAX));@@ -105,6 +126,10 @@ mod tests {assert_eq!(native["start_step"], 2);assert_eq!(native["anchor"], "self");assert_eq!(native["contrast"], 1.25);+ assert_eq!(native["drift_strength"], 0.5);+ assert_eq!(native["drift_max"], 1.0);+ assert_eq!(native["highlight_strength"], 0.5);+ assert!((native["highlight_knee"].as_f64().unwrap() - 0.8).abs() < 0.00001);assert!(native.get("sharpness").is_none());assert!((native["color"][0].as_f64().unwrap() - 128.0 / 255.0).abs() < 0.00001);let roundtrip: GenerationEffects = serde_json::from_value(serde_json::to_value(&effects).unwrap()).unwrap();@@ -125,4 +150,25 @@ mod tests {assert!(neutral.native_settings().unwrap().is_none());assert!(serde_json::from_value::<GenerationEffects>(json!({"sharpness":{"strength":true}})).is_err());}+ #[test]+ fn drift_controls_are_independent_bounded_and_bypassable() {+ let mut effects: GenerationEffects = serde_json::from_value(json!({"drift":{"strength":0,"max":1,"highlightStrength":0.5,"highlightKnee":0.8}})).unwrap();+ let native = effects.native_settings().unwrap().unwrap();+ assert_eq!(native["highlight_strength"], 0.5);+ assert!(native.get("drift_strength").is_none());+ effects.drift.as_mut().unwrap().enabled = Some(false);+ assert!(effects.native_settings().unwrap().is_none());+ effects.drift.as_mut().unwrap().enabled = None;+ effects.drift.as_mut().unwrap().highlight_strength = 0.;+ assert!(effects.native_settings().unwrap().is_none());+ effects.drift.as_mut().unwrap().strength = 0.5;+ assert!(effects.validate().is_err()); // learned correction requires fitted data+ let fixture: GenerationEffects = serde_json::from_str(include_str!("../../../fixtures/generation-effects.json")).unwrap();+ for (field, value) in [("strength", 1.1), ("max", -1.), ("max", 11.), ("highlightStrength", 1.1), ("highlightKnee", 2.1)] {+ let mut invalid = serde_json::to_value(&fixture).unwrap();+ invalid["drift"]["enabled"] = json!(false);+ invalid["drift"][field] = json!(value);+ assert!(serde_json::from_value::<GenerationEffects>(invalid).unwrap().validate().is_err());+ }+ }}diff --git a/src-tauri/core/src/generation_effects/calibration.rs b/src-tauri/core/src/generation_effects/calibration.rsnew file mode 100644index 0000000..3f3ce1e--- /dev/null+++ b/src-tauri/core/src/generation_effects/calibration.rs@@ -0,0 +1,68 @@+use base64::Engine;+use serde::{Deserialize, Serialize};+use serde_json::{json, Value};++const LIMIT: usize = 256 * 1024;++/// The small fitted archive travels with projects and LAN requests, not a local path.+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]+#[serde(deny_unknown_fields)]+pub struct DriftCalibration { pub name: String, pub data: String }++impl DriftCalibration {+ pub fn bytes(&self) -> Result<Vec<u8>, String> {+ let invalid = || "Invalid fitted LCS drift calibration.".to_string();+ if self.name.is_empty() || self.name.chars().count() > 200 || self.data.len() > LIMIT.div_ceil(3) * 4 { return Err(invalid()); }+ let bytes = base64::engine::general_purpose::STANDARD.decode(&self.data).map_err(|_| invalid())?;+ if bytes.len() < 8 || bytes.len() > LIMIT { return Err(invalid()); }+ let length = u64::from_le_bytes(bytes[..8].try_into().unwrap());+ if !(2..=65536).contains(&length) || length + 8 > bytes.len() as u64 { return Err(invalid()); }+ let start = 8 + length as usize;+ let header: Value = serde_json::from_slice(&bytes[8..start]).map_err(|_| invalid())?;+ let hash = header["__metadata__"]["vae_sha256"].as_str().unwrap_or("");+ if header["__metadata__"]["slopfab_lcs"] != "1" || hash.len() != 64 || !hash.bytes().all(|c| c.is_ascii_hexdigit()) { return Err(invalid()); }+ let tensor = |name: &str, shape: &[usize]| -> Result<Vec<f64>, String> {+ let entry = &header[name];+ let begin = entry["data_offsets"][0].as_u64().ok_or_else(invalid)?;+ let end = entry["data_offsets"][1].as_u64().ok_or_else(invalid)?;+ if entry["dtype"] != "F32" || entry["shape"] != json!(shape)+ || end.checked_sub(begin) != Some((shape.iter().product::<usize>() * 4) as u64)+ || end > (bytes.len() - start) as u64 { return Err(invalid()); }+ let values: Vec<f64> = bytes[start + begin as usize..start + end as usize].chunks_exact(4)+ .map(|v| f32::from_le_bytes(v.try_into().unwrap()) as f64).collect();+ if !values.iter().all(|v| v.is_finite()) { return Err(invalid()); }+ Ok(values)+ };+ let basis = tensor("basis", &[96, 3])?;+ tensor("mean", &[96])?; tensor("anchor_lcs", &[8, 3])?; tensor("latent_mean", &[24])?;+ if tensor("latent_std", &[24])?.iter().any(|v| *v <= 0.) { return Err(invalid()); }+ tensor("explained_variance", &[1])?;+ let direction = tensor("drift_direction", &[96])?;+ let reference = tensor("drift_reference", &[2])?;+ let norm: f64 = direction.iter().map(|v| v * v).sum();+ let projection: f64 = (0..3).map(|axis| direction.iter().enumerate().map(|(i, v)| v * basis[i * 3 + axis]).sum::<f64>().powi(2)).sum();+ if (norm - 1.).abs() >= 0.0001 || projection.sqrt() >= 0.0001 || reference[1] < 0. { return Err(invalid()); }+ Ok(bytes)+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ #[test]+ fn portable_calibration_rejects_corrupt_and_unfitted_archives() {+ let fixture: Value = serde_json::from_str(include_str!("../../../../fixtures/generation-effects.json")).unwrap();+ let mut calibration: DriftCalibration = serde_json::from_value(fixture["drift"]["calibration"].clone()).unwrap();+ let mut bytes = calibration.bytes().unwrap();+ let start = 8 + u64::from_le_bytes(bytes[..8].try_into().unwrap()) as usize;+ let header: Value = serde_json::from_slice(&bytes[8..start]).unwrap();+ let offset = start + header["drift_direction"]["data_offsets"][0].as_u64().unwrap() as usize;+ bytes[offset + 12..offset + 16].copy_from_slice(&0_f32.to_le_bytes());+ calibration.data = base64::engine::general_purpose::STANDARD.encode(&bytes);+ assert!(calibration.bytes().is_err());+ calibration.data = base64::engine::general_purpose::STANDARD.encode(&bytes[..100]);+ assert!(calibration.bytes().is_err());+ calibration.data = "a".repeat(LIMIT * 2);+ assert!(calibration.bytes().is_err());+ }+}diff --git a/src-tauri/core/src/slopfab/ffi/lcs_tests.rs b/src-tauri/core/src/slopfab/ffi/lcs_tests.rsindex 678ce7e..9913d4a 100644--- a/src-tauri/core/src/slopfab/ffi/lcs_tests.rs+++ b/src-tauri/core/src/slopfab/ffi/lcs_tests.rs@@ -10,11 +10,13 @@ fn calibration(path: &Path) {let mut data = Vec::<u8>::new();let mut basis = vec![0_f32; 288];basis[0] = 1.; basis[4] = 1.; basis[8] = 1.;+ let mut drift = vec![0_f32; 96]; drift[3] = 1.;for (name, shape, values) in [("basis", vec![96, 3], basis), ("mean", vec![96], vec![0.; 96]),("anchor_lcs", vec![8, 3], vec![1.,0.,0., 0.,0.,1., 0.,1.,0., 1.,0.,1., 0.,1.,1., 1.,1.,0., 0.,0.,0., 1.,1.,1.]),("latent_mean", vec![24], vec![0.; 24]), ("latent_std", vec![24], vec![1.; 24]),("explained_variance", vec![1], vec![1.]),+ ("drift_direction", vec![96], drift), ("drift_reference", vec![2], vec![0.5, 0.25]),] {let start = data.len();data.extend(values.into_iter().flat_map(f32::to_le_bytes));@@ -56,6 +58,22 @@ fn bundled_lcs_snapshots_settings_and_validates_masks() {assert!(other.set_lcs_mask(&request, &[]).unwrap_err().contains("different runtime"));}+#[test]+fn bundled_lcs_accepts_portable_learned_drift_and_independent_highlights() {+ let api = bundled_api();+ let request = RequestHandle::new(&api).unwrap();+ let effects: crate::generation_effects::GenerationEffects = serde_json::from_str(include_str!("../../../../../fixtures/generation-effects.json")).unwrap();+ let archive = tempfile::NamedTempFile::new().unwrap().into_temp_path();+ std::fs::write(&archive, effects.drift.as_ref().unwrap().calibration.as_ref().unwrap().bytes().unwrap()).unwrap();+ api.set_lcs(&request, &archive, &effects.native_settings().unwrap().unwrap().to_string()).unwrap();+ api.resolve(&request).unwrap();+ api.set_lcs(&request, &archive, r#"{"highlight_strength":0.5,"highlight_knee":0.8}"#).unwrap();+ api.resolve(&request).unwrap();+ for settings in [r#"{"drift_strength":1.1}"#, r#"{"drift_max":11}"#, r#"{"highlight_strength":-1}"#, r#"{"highlight_knee":3}"#] {+ assert!(api.set_lcs(&request, &archive, settings).is_err());+ }+}+#[test]fn bundled_single_shot_output_tone_requires_ending_and_spatial_mask() {let api = bundled_api(); api.require_output_tone().unwrap();diff --git a/src-tauri/core/src/slopfab/lcs.rs b/src-tauri/core/src/slopfab/lcs.rsindex 52c9f97..82e6819 100644--- a/src-tauri/core/src/slopfab/lcs.rs+++ b/src-tauri/core/src/slopfab/lcs.rs@@ -43,6 +43,15 @@ pub(super) fn prepare(api: &Api, request: &RequestHandle, item: &QueueItem, plat!item.cancel.load(Ordering::Acquire)};super::lcs_fingerprint::check_cancel(&item.cancel)?;+ if let Some(calibration) = item.request.generation_effects.as_ref().and_then(|effects| effects.drift.as_ref())+ .filter(|drift| drift.enabled != Some(false) && drift.strength > 0.0 && drift.max > 0.0).and_then(|drift| drift.calibration.as_ref()) {+ // Native set_lcs snapshots and validates every tensor. Generation also+ // verifies the archive's VAE fingerprint against the actual loaded VAE.+ let path = tempfile::Builder::new().suffix(".safetensors").tempfile().map_err(|e| e.to_string())?.into_temp_path();+ std::fs::write(&path, calibration.bytes()?).map_err(|e| e.to_string())?;+ api.set_lcs(request, &path, &settings.to_string()).map_err(|e| format!("Cannot apply LCS Drift calibration: {e}"))?;+ return Ok(true);+ }let vae = item.configuration.models.iter().find(|(id, _, _)| *id == 3).and_then(|(_, _, path)| path.as_deref()).ok_or("Generation effects require a video VAE.")?;let log_path = PathBuf::from(crate::diagnostics::log_info()?.path);let directory = log_path.parent().ok_or("Cannot locate the generation effects cache.")?.join("lcs");diff --git a/src-tauri/core/src/worker/protocol.rs b/src-tauri/core/src/worker/protocol.rsindex edf69ca..942805d 100644--- a/src-tauri/core/src/worker/protocol.rs+++ b/src-tauri/core/src/worker/protocol.rs@@ -33,7 +33,8 @@ pub const SERVICE_TYPE: &str = "_slopus-worker._tcp.local.";// v27 grades Tone once after joining Single Shot's neutral ending and main clip.// v28 bounds image-edit inference to the target region and preserves the original canvas.// v29 aligns Single Shot's Keystone conditioning guide with its original context anchor.-pub const PROTOCOL_VERSION: u32 = 29;+// v30 carries portable learned-drift calibration and LCS highlight controls.+pub const PROTOCOL_VERSION: u32 = 30;/// Streaming SeedVR2 restoration; model paths are resolved on the worker.#[derive(Debug, Clone, Serialize, Deserialize)]@@ -388,7 +389,7 @@ mod tests {let single = restored.single_shot.unwrap();assert_eq!(single.ending_prompt, "The subject waves goodbye");assert!(single.ending_path.is_none());- assert_eq!(PROTOCOL_VERSION, 29);+ assert_eq!(PROTOCOL_VERSION, 30);}#[test]diff --git a/src/components/workspace/GenerationEffects.test.tsx b/src/components/workspace/GenerationEffects.test.tsxindex ef8d67c..1db8601 100644--- a/src/components/workspace/GenerationEffects.test.tsx+++ b/src/components/workspace/GenerationEffects.test.tsx@@ -1,10 +1,11 @@// @vitest-environment jsdomimport "@testing-library/jest-dom/vitest";import { useState } from "react";-import { cleanup, fireEvent, render, screen } from "@testing-library/react";+import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";import { afterEach, expect, it, vi } from "vitest";import { GenerationEffects } from "./GenerationEffects";import { activeGenerationEffects, type GenerationEffects as Settings } from "../../lib/generationEffects";+import fixture from "../../../fixtures/generation-effects.json";afterEach(() => { cleanup(); localStorage.clear(); });function Harness() {@@ -51,6 +52,47 @@ it("blocks unsupported models but allows bypassing or removing existing effects"expect(change).toHaveBeenLastCalledWith(undefined);});+it("configures both LCS Drift controls and imports portable learned calibration", async () => {+ render(<Harness />);+ fireEvent.click(screen.getByRole("button", { name: "Add effect" }));+ fireEvent.click(screen.getByRole("button", { name: "LCS Drift" }));+ expect(screen.getByRole("slider", { name: "Drift strength" })).toBeDisabled();+ fireEvent.change(screen.getByRole("slider", { name: "Highlight strength" }), { target: { value: "0.3" } });+ fireEvent.change(screen.getByRole("spinbutton", { name: "Highlight threshold value" }), { target: { value: "0.78" } });+ expect(saved().drift).toMatchObject({ highlightStrength: 0.3, highlightKnee: 0.78 });+ const bytes = Uint8Array.from(atob(fixture.drift.calibration.data), (c) => c.charCodeAt(0));+ const file = new File([bytes], "test-drift.safetensors");+ Object.defineProperty(file, "arrayBuffer", { value: async () => bytes.buffer });+ fireEvent.change(screen.getByLabelText("LCS drift calibration"), { target: { files: [file] } });+ await waitFor(() => expect(screen.getByRole("slider", { name: "Drift strength" })).not.toBeDisabled());+ fireEvent.change(screen.getByRole("slider", { name: "Drift strength" }), { target: { value: "0.5" } });+ fireEvent.change(screen.getByRole("spinbutton", { name: "Maximum correction value" }), { target: { value: "2" } });+ expect(saved().drift).toMatchObject({ strength: 0.5, max: 2, calibration: fixture.drift.calibration });+ fireEvent.click(screen.getByRole("checkbox", { name: "Enable LCS Drift" }));+ expect(activeGenerationEffects(saved())).toBeUndefined();+ fireEvent.click(screen.getByRole("checkbox", { name: "Enable LCS Drift" }));+ fireEvent.click(screen.getByRole("button", { name: "Remove drift calibration" }));+ expect(saved().drift.calibration).toBeUndefined();+ expect(saved().drift.strength).toBe(0);+ expect(activeGenerationEffects(saved())?.drift?.highlightStrength).toBe(0.3);+ fireEvent.click(screen.getByRole("button", { name: "Reset LCS Drift" }));+ expect(saved().drift).toEqual({ strength: 0, max: 1, highlightStrength: 0.5, highlightKnee: 0.8 });+ fireEvent.click(screen.getByRole("button", { name: "Remove LCS Drift" }));+ expect(saved()).toBeNull();+});++it("reports an invalid drift calibration without enabling learned correction", async () => {+ render(<Harness />);+ fireEvent.click(screen.getByRole("button", { name: "Add effect" }));+ fireEvent.click(screen.getByRole("button", { name: "LCS Drift" }));+ const file = new File(["invalid"], "wrong.safetensors");+ Object.defineProperty(file, "arrayBuffer", { value: async () => new ArrayBuffer(7) });+ fireEvent.change(screen.getByLabelText("LCS drift calibration"), { target: { files: [file] } });+ expect(await screen.findByRole("alert")).toBeInTheDocument();+ expect(saved().drift.calibration).toBeUndefined();+ expect(screen.getByRole("slider", { name: "Drift strength" })).toBeDisabled();+});+it("edits the selected video scene from its right panel", async () => {const { SceneInspector } = await import("./SceneEditor");const { createProjectConfig, sceneShots } = await import("../../lib/project");diff --git a/src/components/workspace/GenerationEffects.tsx b/src/components/workspace/GenerationEffects.tsxindex e81f05f..0995f0d 100644--- a/src/components/workspace/GenerationEffects.tsx+++ b/src/components/workspace/GenerationEffects.tsx@@ -3,6 +3,7 @@ import { Checkbox, ComboBox, Flyout, PropRow, PropSection, tooltipProps } from "import { SliderNumberInput } from "./SliderNumberInput";import { Add16, ChevronDown12, Delete14, Reset16 } from "../ui/icons";import { GENERATION_EFFECTS, GENERATION_EFFECT_DEFAULTS, generationEffectsSchema, type GenerationEffectId, type GenerationEffects as Settings } from "../../lib/generationEffects";+import { importLcsCalibration, MAX_LCS_CALIBRATION_BYTES } from "../../lib/lcsCalibration";export function GenerationEffects({ value, onChange, supported, disabled = false, finalTone = false }: {value?: Settings | null; onChange: (value: Settings | undefined) => void; supported: boolean; disabled?: boolean; finalTone?: boolean;@@ -12,6 +13,12 @@ export function GenerationEffects({ value, onChange, supported, disabled = falseconst [collapsed, setCollapsed] = useState<Set<string>>(new Set());const trigger = useRef<HTMLButtonElement>(null);const picker = useRef<HTMLDivElement>(null);+ const calibrationInput = useRef<HTMLInputElement>(null);+ const [calibrationError, setCalibrationError] = useState<string | null>(null);+ const [importing, setImporting] = useState(false);+ const latest = useRef({ value, disabled, supported }); latest.current = { value, disabled, supported };+ const importRevision = useRef(0);+ useEffect(() => () => { importRevision.current++; }, []);const moveFocus = (event: KeyboardEvent<HTMLDivElement>) => {if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) return;event.preventDefault();@@ -29,9 +36,9 @@ export function GenerationEffects({ value, onChange, supported, disabled = falseonChange(GENERATION_EFFECTS.some(({ id }) => next[id]) ? next : undefined);};const set = <K extends GenerationEffectId>(id: K, patch: Partial<NonNullable<Settings[K]>>) => update({ ...effects, [id]: { ...effects[id], ...patch } });- const param = (id: GenerationEffectId, field: string, label: string, min: number, max: number, initial: number) => {+ const param = (id: GenerationEffectId, field: string, label: string, min: number, max: number, initial: number, unavailable = false) => {const settings = effects[id] as unknown as Record<string, number>;- const inactive = locked || effects[id]?.enabled === false;+ const inactive = locked || effects[id]?.enabled === false || unavailable;const change = (next: number) => { if (!inactive) set(id, { [field]: next }); };return <PropRow key={field} label={label} value={settings[field]} defaultValue={initial} onReset={inactive ? undefined : () => change(initial)} scrub={inactive ? undefined : { value: settings[field], onChange: change, step: 0.01, min, max }}><SliderNumberInput aria-label={`${label} value`} sliderLabel={label} minimum={min} maximum={max} step={0.01} value={settings[field]} disabled={inactive} onCommit={change} />@@ -61,6 +68,32 @@ export function GenerationEffects({ value, onChange, supported, disabled = false<PropRow label="Anchor"><ComboBox aria-label="Color anchor" disabled={inactive} value={effects.anchor.mode} options={[{ value: "self", label: "Self" }, { value: "smooth", label: "Smooth" }]} onChange={(mode) => set("anchor", { mode: mode as "self" | "smooth" })} /></PropRow>{param(id, "strength", "Anchor strength", 0, 1, 0.25)}</>}+ {id === "drift" && effects.drift && <>+ {param(id, "highlightStrength", "Highlight strength", 0, 1, 0.5)}+ {param(id, "highlightKnee", "Highlight threshold", 0, 2, 0.8)}+ <p className="clip-effect__note">Compresses highlights above the threshold. Learned correction uses a calibration fitted from early and late generations of this scene with the same VAE.</p>+ <input ref={calibrationInput} type="file" accept=".safetensors" hidden aria-label="LCS drift calibration" disabled={inactive || importing}+ onChange={async (event) => {+ const file = event.target.files?.[0]; event.target.value = "";+ if (!file || inactive) return;+ const revision = ++importRevision.current;+ setCalibrationError(null); setImporting(true);+ try {+ if (file.size > MAX_LCS_CALIBRATION_BYTES) throw new Error("Choose an LCS calibration archive smaller than 256 KB.");+ const calibration = importLcsCalibration(file.name, new Uint8Array(await file.arrayBuffer()));+ if (revision === importRevision.current && latest.current.value === value && !latest.current.disabled && latest.current.supported) set("drift", { calibration });+ } catch (error) { if (revision === importRevision.current) setCalibrationError(error instanceof Error ? error.message : String(error)); }+ finally { if (revision === importRevision.current) setImporting(false); }+ }} />+ <button type="button" className="secondary-button" disabled={inactive || importing} onClick={() => calibrationInput.current?.click()}>{importing ? "Importing calibration…" : effects.drift.calibration ? "Replace drift calibration" : "Import drift calibration"}</button>+ {effects.drift.calibration && <PropRow label="Calibration"><span title={effects.drift.calibration.name}>{effects.drift.calibration.name}</span>+ <button type="button" className="clip-effect__action" aria-label="Remove drift calibration" disabled={inactive || importing} onClick={() => set("drift", { calibration: undefined, strength: 0 })}><Delete14 aria-hidden="true" /></button>+ </PropRow>}+ {calibrationError && <p className="clip-effect__note" role="alert">{calibrationError}</p>}+ {param(id, "strength", "Drift strength", 0, 1, 0, !effects.drift.calibration)}+ {param(id, "max", "Maximum correction", 0, 10, 1, !effects.drift.calibration)}+ <p className="clip-effect__note">Experimental: inspect the result before reusing these settings across continuations.</p>+ </>}</fieldset>}</section>;})}diff --git a/src/lib/generationEffects.test.ts b/src/lib/generationEffects.test.tsindex d083682..258ed02 100644--- a/src/lib/generationEffects.test.ts+++ b/src/lib/generationEffects.test.ts@@ -4,6 +4,7 @@ import { activeGenerationEffects, generationEffectsSchema } from "./generationEfimport { createProjectConfig, parseProjectConfig, sceneGenerationSnapshot } from "./project";import { sceneGenerationRequest } from "./sceneGeneration";import { createImageScene, imageSceneSchema } from "./imageScene";+import { importLcsCalibration } from "./lcsCalibration";it("preserves effects across project and image serialization and fingerprints active video effects", () => {const effects = generationEffectsSchema.parse(fixture);@@ -28,7 +29,32 @@ it.each([{ color: { color: "red", strength: 1, mode: "type_i" } },{ sharpness: { strength: Infinity } }, { sharpness: { strength: 6 } },{ anchor: { strength: 1, mode: "reference" } }, { startStep: 10, endStep: 9 },+ { drift: { strength: 0, max: 1, highlightStrength: 1.1, highlightKnee: 0.8 } },+ { drift: { strength: 0.5, max: 1, highlightStrength: 0, highlightKnee: 0.8 } },+ { drift: { ...fixture.drift, strength: -1 } }, { drift: { ...fixture.drift, max: 11 } },+ { drift: { ...fixture.drift, highlightKnee: 3 } },{ region: { x: 60, y: 0, width: 50, height: 100, invert: false } },])("rejects invalid native controls %j", (value) => {expect(generationEffectsSchema.safeParse(value).success).toBe(false);});++it("keeps highlight compression independent and ignores bypassed or zero drift controls", () => {+ const drift = generationEffectsSchema.parse(fixture).drift!;+ expect(activeGenerationEffects({ drift: { ...drift, enabled: false } })).toBeUndefined();+ expect(activeGenerationEffects({ drift: { ...drift, strength: 0, highlightStrength: 0 } })).toBeUndefined();+ expect(activeGenerationEffects({ drift: { ...drift, max: 0, highlightStrength: 0 } })).toBeUndefined();+ expect(activeGenerationEffects({ drift: { ...drift, strength: 0 } })?.drift?.calibration).toBeUndefined();+ expect(activeGenerationEffects({ drift })?.drift?.calibration).toEqual(fixture.drift.calibration);+});++it("imports fitted calibration bytes and rejects missing, truncated or invalid directions", () => {+ const bytes = Uint8Array.from(atob(fixture.drift.calibration.data), (c) => c.charCodeAt(0));+ expect(importLcsCalibration("test-drift.safetensors", bytes)).toEqual(fixture.drift.calibration);+ expect(() => importLcsCalibration("broken", bytes.subarray(0, 100))).toThrow();+ const view = new DataView(bytes.buffer);+ const length = Number(view.getBigUint64(0, true));+ const header = JSON.parse(new TextDecoder().decode(bytes.subarray(8, 8 + length)));+ view.setFloat32(8 + length + header.drift_direction.data_offsets[0] + 3 * 4, 0, true);+ expect(() => importLcsCalibration("zero-direction", bytes)).toThrow(/direction/);+ expect(generationEffectsSchema.safeParse({ drift: { ...fixture.drift, calibration: { name: "bad", data: "aGVsbG8=" } } }).success).toBe(false);+});diff --git a/src/lib/generationEffects.ts b/src/lib/generationEffects.tsindex 3c81387..38baab9 100644--- a/src/lib/generationEffects.ts+++ b/src/lib/generationEffects.ts@@ -1,4 +1,5 @@import { z } from "zod";+import { lcsCalibrationSchema } from "./lcsCalibration";const enabled = z.boolean().optional();const scalar = (min: number, max: number) => z.number().finite().min(min).max(max);@@ -7,6 +8,8 @@ export const generationEffectsSchema = z.object({tone: z.object({ enabled, contrast: scalar(0, 4), brightness: scalar(-1, 1), saturation: scalar(0, 4), temperature: scalar(-1, 1) }).strict().optional(),sharpness: z.object({ enabled, strength: scalar(-5, 5) }).strict().optional(),anchor: z.object({ enabled, mode: z.enum(["self", "smooth"]), strength: scalar(0, 1) }).strict().optional(),+ drift: z.object({ enabled, strength: scalar(0, 1), max: scalar(0, 10), highlightStrength: scalar(0, 1), highlightKnee: scalar(0, 2), calibration: lcsCalibrationSchema.optional() }).strict()+ .refine((drift) => drift.strength === 0 || drift.max === 0 || !!drift.calibration, "Learned drift correction needs a fitted LCS calibration.").optional(),startStep: z.number().int().min(0).max(2147483647).optional(),endStep: z.number().int().min(0).max(2147483647).optional(),region: z.object({ x: scalar(0, 100), y: scalar(0, 100), width: scalar(0.1, 100), height: scalar(0.1, 100), invert: z.boolean() }).strict()@@ -18,6 +21,7 @@ export const GENERATION_EFFECTS = [{ id: "tone", name: "Tone", description: "Adjust contrast, brightness, saturation and temperature during generation." },{ id: "sharpness", name: "Sharpness", description: "Sharpen or soften during generation." },{ id: "anchor", name: "Color stabilization", description: "Reduce color drift or smooth color boundaries during generation." },+ { id: "drift", name: "LCS Drift", description: "Compress highlights and optionally correct learned appearance drift during generation." },] as const;export type GenerationEffectId = typeof GENERATION_EFFECTS[number]["id"];export const GENERATION_EFFECT_DEFAULTS: Required<Pick<GenerationEffects, GenerationEffectId>> = {@@ -25,6 +29,7 @@ export const GENERATION_EFFECT_DEFAULTS: Required<Pick<GenerationEffects, Generatone: { contrast: 0.9, brightness: 0.1, saturation: 0.95, temperature: 0 },sharpness: { strength: 0.1 },anchor: { mode: "self", strength: 0.25 },+ drift: { strength: 0, max: 1, highlightStrength: 0.5, highlightKnee: 0.8 },};/** Only active, non-neutral controls are submitted or included in render fingerprints. */@@ -37,6 +42,10 @@ export function activeGenerationEffects(value?: GenerationEffects | null): Gener}if (value.sharpness && value.sharpness.enabled !== false && value.sharpness.strength !== 0) effects.sharpness = { strength: value.sharpness.strength };if (value.anchor && value.anchor.enabled !== false && value.anchor.strength !== 0) effects.anchor = { mode: value.anchor.mode, strength: value.anchor.strength };+ if (value.drift && value.drift.enabled !== false && (value.drift.strength > 0 && value.drift.max > 0 || value.drift.highlightStrength > 0)) {+ const { enabled: _enabled, calibration, ...drift } = value.drift;+ effects.drift = { ...drift, ...(drift.strength > 0 && drift.max > 0 && calibration ? { calibration } : {}) };+ }if (!Object.keys(effects).length) return;return { ...effects, ...(value.startStep !== undefined ? { startStep: value.startStep } : {}), ...(value.endStep !== undefined ? { endStep: value.endStep } : {}), ...(value.region ? { region: { ...value.region } } : {}) };}diff --git a/src/lib/lcsCalibration.ts b/src/lib/lcsCalibration.tsnew file mode 100644index 0000000..de64118--- /dev/null+++ b/src/lib/lcsCalibration.ts@@ -0,0 +1,48 @@+import { z } from "zod";++export const MAX_LCS_CALIBRATION_BYTES = 256 * 1024;++/** Fitted archives are small and travel with the scene, never as host-specific paths. */+export function validateLcsCalibration(bytes: Uint8Array): void {+ if (bytes.length < 8 || bytes.length > MAX_LCS_CALIBRATION_BYTES) throw new Error("Choose an LCS calibration archive smaller than 256 KB.");+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);+ const length = Number(view.getBigUint64(0, true));+ if (!Number.isSafeInteger(length) || length < 2 || length > 65536 || length + 8 > bytes.length) throw new Error("Invalid LCS calibration header.");+ const header = JSON.parse(new TextDecoder().decode(bytes.subarray(8, 8 + length)));+ if (header.__metadata__?.slopfab_lcs !== "1" || !/^[a-f0-9]{64}$/i.test(header.__metadata__?.vae_sha256 ?? "")) throw new Error("Choose a fitted SlopFab LCS calibration.");+ const read = (name: string, shape: number[]) => {+ const tensor = header[name], count = shape.reduce((a, b) => a * b, 1);+ const [start, end] = tensor?.data_offsets ?? [];+ if (tensor?.dtype !== "F32" || JSON.stringify(tensor.shape) !== JSON.stringify(shape)+ || !Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start < 0 || end - start !== count * 4 || end > bytes.length - 8 - length) {+ throw new Error(`Invalid or missing LCS ${name} tensor. Import a calibration fitted from early and late generations.`);+ }+ const values = Array.from({ length: count }, (_, i) => view.getFloat32(8 + length + start + i * 4, true));+ if (!values.every(Number.isFinite)) throw new Error(`Non-finite LCS ${name} values.`);+ return values;+ };+ const basis = read("basis", [96, 3]);+ read("mean", [96]); read("anchor_lcs", [8, 3]); read("latent_mean", [24]);+ if (read("latent_std", [24]).some((n) => n <= 0)) throw new Error("Invalid LCS latent scaling.");+ read("explained_variance", [1]);+ const direction = read("drift_direction", [96]), reference = read("drift_reference", [2]);+ const norm = direction.reduce((sum, n) => sum + n * n, 0);+ const projection = [0, 1, 2].map((axis) => direction.reduce((sum, n, i) => sum + n * basis[i * 3 + axis], 0));+ if (Math.abs(norm - 1) >= 0.0001 || Math.hypot(...projection) >= 0.0001 || reference[1] < 0) throw new Error("Invalid learned drift direction or tolerance.");+}++export const lcsCalibrationSchema = z.object({+ name: z.string().min(1).max(200),+ data: z.string().min(1).max(Math.ceil(MAX_LCS_CALIBRATION_BYTES / 3) * 4),+}).strict().superRefine((value, ctx) => {+ try { validateLcsCalibration(Uint8Array.from(atob(value.data), (c) => c.charCodeAt(0))); }+ catch (error) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: error instanceof Error ? error.message : "Invalid LCS calibration." }); }+});+export type LcsCalibration = z.infer<typeof lcsCalibrationSchema>;++export function importLcsCalibration(name: string, bytes: Uint8Array): LcsCalibration {+ validateLcsCalibration(bytes);+ let binary = "";+ for (const byte of bytes) binary += String.fromCharCode(byte);+ return lcsCalibrationSchema.parse({ name: name.slice(0, 200), data: btoa(binary) });+}