The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(agent): the phone relay wrote user files the unsafe way
purple3a02a0b5a3c3
diff --git a/src-tauri/src/commands/agent.rs b/src-tauri/src/commands/agent.rsindex 4e96a230..4124b619 100644--- a/src-tauri/src/commands/agent.rs+++ b/src-tauri/src/commands/agent.rs@@ -414,7 +414,23 @@ pub fn file_write(if let Some(parent) = full_path.parent() {fs::create_dir_all(parent).map_err(|e| format!("Create dir: {}", e))?;}- fs::write(&full_path, &content).map_err(|e| format!("Write error: {}", e))?;+ // Same treatment the desktop path (fs_write) already gets: keep the file's+ // EOL/BOM convention, skip an identical write, and swap the new bytes in+ // atomically. A plain fs::write here could leave a user's source file+ // truncated if the app died mid-write, and it rewrote a CRLF repo as LF —+ // which shows up as "every line changed" in git.+ let existing = fs::read(&full_path).ok();+ let out_bytes = crate::commands::filesystem::normalize_to_existing_style(+ existing.as_deref(),+ &content,+ );+ if existing.as_deref() == Some(out_bytes.as_slice()) {+ return Ok(serde_json::json!({+ "status": "unchanged",+ "path": full_path.to_string_lossy(),+ }));+ }+ crate::commands::filesystem::write_atomic(&full_path, &out_bytes)?;Ok(serde_json::json!({"status": "saved", "path": full_path.to_string_lossy()}))}diff --git a/src-tauri/src/commands/filesystem.rs b/src-tauri/src/commands/filesystem.rsindex 21cc1614..52f5c432 100644--- a/src-tauri/src/commands/filesystem.rs+++ b/src-tauri/src/commands/filesystem.rs@@ -191,7 +191,7 @@ pub fn fs_read(path: String, chatId: Option<String>, workingDirectory: Option<St/// models emit `\n`; on a Windows repo whose files are CRLF, writing that raw/// turned every edit into a whole-file whitespace diff. For a NEW file we honor/// exactly what the caller sent — there is no convention to match.-fn normalize_to_existing_style(existing: Option<&[u8]>, new: &str) -> Vec<u8> {+pub(crate) fn normalize_to_existing_style(existing: Option<&[u8]>, new: &str) -> Vec<u8> {const BOM: [u8; 3] = [0xEF, 0xBB, 0xBF];let existing = match existing {Some(e) => e,@@ -215,7 +215,7 @@ fn normalize_to_existing_style(existing: Option<&[u8]>, new: &str) -> Vec<u8> {/// rename over the target. A crash or interrupt can never leave a half-written/// (truncated) file where the original was — std::fs::rename replaces the/// destination on both Unix and Windows.-fn write_atomic(target: &Path, bytes: &[u8]) -> Result<(), String> {+pub(crate) fn write_atomic(target: &Path, bytes: &[u8]) -> Result<(), String> {use std::sync::atomic::{AtomicU64, Ordering};static SEQ: AtomicU64 = AtomicU64::new(0);let parent = target.parent().ok_or_else(|| "No parent directory".to_string())?;@@ -513,6 +513,30 @@ mod tests {assert_eq!(normalize_to_existing_style(None, "a\r\nb"), b"a\r\nb");}+ #[test]+ fn write_atomic_replaces_and_leaves_no_debris() {+ use super::write_atomic;+ use std::fs;+ let dir = std::env::temp_dir().join(format!("lu-atomic-{}", std::process::id()));+ let _ = fs::create_dir_all(&dir);+ let target = dir.join("notes.txt");+ fs::write(&target, b"old").unwrap();++ write_atomic(&target, b"new content").unwrap();+ assert_eq!(fs::read(&target).unwrap(), b"new content");++ // The temp twin must be gone — a leftover .notes.txt.tmpNNN would show+ // up in the user's repo and in file_list.+ let leftovers: Vec<String> = fs::read_dir(&dir)+ .unwrap()+ .flatten()+ .map(|e| e.file_name().to_string_lossy().to_string())+ .filter(|n| n != "notes.txt")+ .collect();+ assert!(leftovers.is_empty(), "left behind: {:?}", leftovers);+ let _ = fs::remove_dir_all(&dir);+ }+#[test]fn crlf_file_gets_lf_content_converted() {let existing = b"old\r\nlines\r\n";