Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor approval methods to reload from disk before saving
Maximilian Roos3b2e68ba6ede
diff --git a/src/config/user.rs b/src/config/user.rsindex d5c3d6904..e0164cef5 100644--- a/src/config/user.rs+++ b/src/config/user.rs@@ -451,12 +451,18 @@ impl WorktrunkConfig {}/// Add an approved command and save to a specific config file (for testing)+ ///+ /// Reloads from disk before modifying to reduce race conditions from concurrent processes.pub fn approve_command_to(&mut self,project: String,command: String,config_path: Option<&std::path::Path>,) -> Result<(), ConfigError> {+ // Reload from disk first to get fresh state (fixes race condition where+ // concurrent approvals would overwrite each other)+ self.reload_projects_from(config_path)?;+if self.is_command_approved(&project, &command) {return Ok(());}@@ -469,12 +475,58 @@ impl WorktrunkConfig {self.save_impl(config_path)}+ /// Reload only the projects section from disk, preserving other in-memory state+ ///+ /// This replaces the in-memory projects with the authoritative disk state,+ /// while keeping other config values (worktree-path, commit-generation, etc.).+ /// Callers should reload before modifying and saving to avoid race conditions.+ fn reload_projects_from(+ &mut self,+ config_path: Option<&std::path::Path>,+ ) -> Result<(), ConfigError> {+ let path = match config_path {+ Some(p) => Some(p.to_path_buf()),+ None => get_config_path(),+ };++ let Some(path) = path else {+ return Ok(()); // No config file to reload from+ };++ if !path.exists() {+ return Ok(()); // Nothing to reload+ }++ let content = std::fs::read_to_string(&path).map_err(|e| {+ ConfigError::Message(format!(+ "Failed to read config file {}: {}",+ path.display(),+ e+ ))+ })?;++ let disk_config: WorktrunkConfig = toml::from_str(&content).map_err(|e| {+ ConfigError::Message(format!(+ "Failed to parse config file {}: {}",+ path.display(),+ e+ ))+ })?;++ // Replace in-memory projects with disk state (disk is authoritative)+ self.projects = disk_config.projects;++ Ok(())+ }+/// Revoke an approved command and save to config filepub fn revoke_command(&mut self, project: &str, command: &str) -> Result<(), ConfigError> {self.revoke_command_to(project, command, None)}/// Revoke an approved command and save to a specific config file (for testing)+ ///+ /// Reloads from disk before modifying to reduce race conditions from concurrent processes.#[doc(hidden)]pub fn revoke_command_to(&mut self,@@ -482,6 +534,9 @@ impl WorktrunkConfig {command: &str,config_path: Option<&std::path::Path>,) -> Result<(), ConfigError> {+ // Reload from disk first to get fresh state (fixes race condition)+ self.reload_projects_from(config_path)?;+if let Some(project_config) = self.projects.get_mut(project) {let len_before = project_config.approved_commands.len();project_config.approved_commands.retain(|c| c != command);@@ -504,12 +559,17 @@ impl WorktrunkConfig {}/// Remove all approvals for a project and save to a specific config file (for testing)+ ///+ /// Reloads from disk before modifying to reduce race conditions from concurrent processes.#[doc(hidden)]pub fn revoke_project_to(&mut self,project: &str,config_path: Option<&std::path::Path>,) -> Result<(), ConfigError> {+ // Reload from disk first to get fresh state (fixes race condition)+ self.reload_projects_from(config_path)?;+if self.projects.remove(project).is_some() {self.save_impl(config_path)?;}diff --git a/tests/integration_tests/approval_save.rs b/tests/integration_tests/approval_save.rsindex b44731c69..d5fe23d37 100644--- a/tests/integration_tests/approval_save.rs+++ b/tests/integration_tests/approval_save.rs@@ -338,6 +338,188 @@ args = ["-s"]);}+/// Test that concurrent approve operations don't lose data+///+/// This tests a race condition where two config instances (simulating separate processes)+/// both approve commands. Without proper merging, the second save would overwrite+/// the first approval, losing it.+#[test]+fn test_concurrent_approve_preserves_all_approvals() {+ let temp_dir = TempDir::new().unwrap();+ let config_path = temp_dir.path().join("config.toml");++ // Process A: Start with empty config, approve "npm install"+ let mut config_a = WorktrunkConfig::default();++ // Process B: Start with empty config (simulating a separate process that loaded before A saved)+ let mut config_b = WorktrunkConfig::default();++ // Process A approves and saves "npm install"+ config_a+ .approve_command_to(+ "github.com/user/repo".to_string(),+ "npm install".to_string(),+ Some(&config_path),+ )+ .unwrap();++ // Verify file has "npm install"+ let toml_content = fs::read_to_string(&config_path).unwrap();+ assert!(+ toml_content.contains("npm install"),+ "File should contain 'npm install'"+ );++ // Process B (which loaded BEFORE Process A saved) now approves and saves "npm test"+ // The save_to method should merge with what's on disk, not overwrite+ config_b+ .approve_command_to(+ "github.com/user/repo".to_string(),+ "npm test".to_string(),+ Some(&config_path),+ )+ .unwrap();++ // Read the final state from disk+ let toml_content = fs::read_to_string(&config_path).unwrap();++ // Both approvals should be preserved+ assert!(+ toml_content.contains("npm install"),+ "BUG: 'npm install' approval was lost due to race condition. \+ config_b's save_to() should merge with disk state, not overwrite it. \+ Saved content:\n{toml_content}"+ );+ assert!(+ toml_content.contains("npm test"),+ "'npm test' approval should exist. Saved content:\n{toml_content}"+ );+}++/// Test that concurrent revoke operations don't lose data+///+/// This tests a race condition where two config instances (simulating separate processes)+/// both revoke commands. Without proper merging, the second save would restore+/// the revoked command from its stale in-memory state.+#[test]+fn test_concurrent_revoke_preserves_all_changes() {+ let temp_dir = TempDir::new().unwrap();+ let config_path = temp_dir.path().join("config.toml");++ // Setup: config file has two commands approved+ let mut setup_config = WorktrunkConfig::default();+ setup_config+ .approve_command_to(+ "github.com/user/repo".to_string(),+ "npm install".to_string(),+ Some(&config_path),+ )+ .unwrap();+ setup_config+ .approve_command_to(+ "github.com/user/repo".to_string(),+ "npm test".to_string(),+ Some(&config_path),+ )+ .unwrap();++ // Verify setup+ let toml_content = fs::read_to_string(&config_path).unwrap();+ assert!(toml_content.contains("npm install"));+ assert!(toml_content.contains("npm test"));++ // Process A: loads config (has ["npm install", "npm test"])+ let mut config_a = WorktrunkConfig::default();+ config_a+ .projects+ .entry("github.com/user/repo".to_string())+ .or_default()+ .approved_commands = vec!["npm install".to_string(), "npm test".to_string()];++ // Process B: loads config (has ["npm install", "npm test"])+ let mut config_b = WorktrunkConfig::default();+ config_b+ .projects+ .entry("github.com/user/repo".to_string())+ .or_default()+ .approved_commands = vec!["npm install".to_string(), "npm test".to_string()];++ // Process A revokes "npm install"+ config_a+ .revoke_command_to("github.com/user/repo", "npm install", Some(&config_path))+ .unwrap();++ // Process B (with stale state) revokes "npm test"+ // Should see that "npm install" was already revoked and preserve that+ config_b+ .revoke_command_to("github.com/user/repo", "npm test", Some(&config_path))+ .unwrap();++ // Read the final state from disk+ let toml_content = fs::read_to_string(&config_path).unwrap();++ // Both revocations should be respected - neither command should remain+ assert!(+ !toml_content.contains("npm install"),+ "'npm install' should have been revoked. Saved content:\n{toml_content}"+ );+ assert!(+ !toml_content.contains("npm test"),+ "'npm test' should have been revoked. Saved content:\n{toml_content}"+ );+}++/// Test that concurrent approve operations across different projects also work+#[test]+fn test_concurrent_approve_different_projects() {+ let temp_dir = TempDir::new().unwrap();+ let config_path = temp_dir.path().join("config.toml");++ // Process A: empty config+ let mut config_a = WorktrunkConfig::default();++ // Process B: empty config (loaded before A saved)+ let mut config_b = WorktrunkConfig::default();++ // Process A approves for project1+ config_a+ .approve_command_to(+ "github.com/user/project1".to_string(),+ "npm install".to_string(),+ Some(&config_path),+ )+ .unwrap();++ // Process B approves for project2+ // Should preserve project1's approval+ config_b+ .approve_command_to(+ "github.com/user/project2".to_string(),+ "cargo build".to_string(),+ Some(&config_path),+ )+ .unwrap();++ let toml_content = fs::read_to_string(&config_path).unwrap();++ assert!(+ toml_content.contains("github.com/user/project1"),+ "Project1 should be preserved. Content:\n{toml_content}"+ );+ assert!(+ toml_content.contains("npm install"),+ "'npm install' should be preserved. Content:\n{toml_content}"+ );+ assert!(+ toml_content.contains("github.com/user/project2"),+ "Project2 should exist. Content:\n{toml_content}"+ );+ assert!(+ toml_content.contains("cargo build"),+ "'cargo build' should exist. Content:\n{toml_content}"+ );+}+/// Test that permission errors when saving config are handled gracefully////// This tests the lower-level `approve_command_to()` method fails when permissions