|
| 1 | +use tauri::AppHandle; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + app_state::AppState, |
| 5 | + managed_agents::{ |
| 6 | + load_managed_agents, save_managed_agents, try_regenerate_nest, ManagedAgentRecord, |
| 7 | + }, |
| 8 | +}; |
| 9 | + |
| 10 | +#[derive(Debug)] |
| 11 | +pub(super) struct AgentUpdateRollback { |
| 12 | + attempted_record: ManagedAgentRecord, |
| 13 | + previous_record: ManagedAgentRecord, |
| 14 | +} |
| 15 | + |
| 16 | +impl AgentUpdateRollback { |
| 17 | + pub(super) fn new(previous_record: ManagedAgentRecord, attempted: &ManagedAgentRecord) -> Self { |
| 18 | + Self { |
| 19 | + attempted_record: attempted.clone(), |
| 20 | + previous_record, |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +fn copy_runtime_state(from: &ManagedAgentRecord, to: &mut ManagedAgentRecord) { |
| 26 | + to.runtime_pid = from.runtime_pid; |
| 27 | + to.backend = from.backend.clone(); |
| 28 | + to.backend_agent_id.clone_from(&from.backend_agent_id); |
| 29 | + to.provider_binary_path |
| 30 | + .clone_from(&from.provider_binary_path); |
| 31 | + to.last_started_at.clone_from(&from.last_started_at); |
| 32 | + to.last_stopped_at.clone_from(&from.last_stopped_at); |
| 33 | + to.last_exit_code = from.last_exit_code; |
| 34 | + to.last_error.clone_from(&from.last_error); |
| 35 | + to.last_error_code = from.last_error_code; |
| 36 | +} |
| 37 | + |
| 38 | +fn same_configuration(left: &ManagedAgentRecord, right: &ManagedAgentRecord) -> bool { |
| 39 | + let mut normalized_left = left.clone(); |
| 40 | + copy_runtime_state(right, &mut normalized_left); |
| 41 | + normalized_left.updated_at.clone_from(&right.updated_at); |
| 42 | + normalized_left == *right |
| 43 | +} |
| 44 | + |
| 45 | +fn restore_agent_update( |
| 46 | + records: &mut [ManagedAgentRecord], |
| 47 | + pubkey: &str, |
| 48 | + rollback: AgentUpdateRollback, |
| 49 | +) -> Result<(), String> { |
| 50 | + let current = records |
| 51 | + .iter_mut() |
| 52 | + .find(|record| record.pubkey == pubkey) |
| 53 | + .ok_or_else(|| format!("agent {pubkey} not found while rolling back failed rename"))?; |
| 54 | + |
| 55 | + if !same_configuration(current, &rollback.attempted_record) { |
| 56 | + return Err(format!( |
| 57 | + "agent {pubkey} changed again before the failed rename could be rolled back" |
| 58 | + )); |
| 59 | + } |
| 60 | + |
| 61 | + let runtime_changed = { |
| 62 | + let mut attempted_with_current_runtime = rollback.attempted_record.clone(); |
| 63 | + copy_runtime_state(current, &mut attempted_with_current_runtime); |
| 64 | + attempted_with_current_runtime != rollback.attempted_record |
| 65 | + }; |
| 66 | + let mut restored = rollback.previous_record; |
| 67 | + copy_runtime_state(current, &mut restored); |
| 68 | + if runtime_changed { |
| 69 | + restored.updated_at.clone_from(¤t.updated_at); |
| 70 | + } |
| 71 | + *current = restored; |
| 72 | + Ok(()) |
| 73 | +} |
| 74 | + |
| 75 | +pub(super) fn rollback_failed_agent_update( |
| 76 | + app: &AppHandle, |
| 77 | + state: &AppState, |
| 78 | + pubkey: &str, |
| 79 | + rollback: AgentUpdateRollback, |
| 80 | +) -> Result<(), String> { |
| 81 | + { |
| 82 | + let _store_guard = state |
| 83 | + .managed_agents_store_lock |
| 84 | + .lock() |
| 85 | + .map_err(|error| error.to_string())?; |
| 86 | + let mut records = load_managed_agents(app)?; |
| 87 | + restore_agent_update(&mut records, pubkey, rollback)?; |
| 88 | + save_managed_agents(app, &records)?; |
| 89 | + let restored = records |
| 90 | + .iter() |
| 91 | + .find(|record| record.pubkey == pubkey) |
| 92 | + .ok_or_else(|| format!("agent {pubkey} not found after failed rename rollback"))?; |
| 93 | + super::agents::retain_managed_agent_pending(app, state, restored); |
| 94 | + } |
| 95 | + try_regenerate_nest(app); |
| 96 | + Ok(()) |
| 97 | +} |
| 98 | + |
| 99 | +#[cfg(test)] |
| 100 | +mod tests { |
| 101 | + use super::*; |
| 102 | + |
| 103 | + fn record(name: &str, updated_at: &str) -> ManagedAgentRecord { |
| 104 | + let mut record: ManagedAgentRecord = serde_json::from_str( |
| 105 | + r#"{ |
| 106 | + "pubkey": "abcd1234", |
| 107 | + "name": "test-agent", |
| 108 | + "private_key_nsec": "nsec1fake", |
| 109 | + "relay_url": "wss://localhost:3000", |
| 110 | + "acp_command": "buzz-acp", |
| 111 | + "agent_command": "goose", |
| 112 | + "agent_args": [], |
| 113 | + "mcp_command": "", |
| 114 | + "turn_timeout_seconds": 320, |
| 115 | + "system_prompt": null, |
| 116 | + "model": null, |
| 117 | + "provider": null, |
| 118 | + "env_vars": {}, |
| 119 | + "created_at": "2026-01-01T00:00:00Z", |
| 120 | + "updated_at": "2026-01-01T00:00:00Z", |
| 121 | + "last_started_at": null, |
| 122 | + "last_stopped_at": null, |
| 123 | + "last_exit_code": null, |
| 124 | + "last_error": null |
| 125 | + }"#, |
| 126 | + ) |
| 127 | + .expect("sample managed agent record"); |
| 128 | + record.name = name.to_string(); |
| 129 | + record.updated_at = updated_at.to_string(); |
| 130 | + record |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn failed_profile_sync_restores_the_entire_agent_update() { |
| 135 | + let previous = record("Old name", "before"); |
| 136 | + let mut attempted = previous.clone(); |
| 137 | + attempted.name = "New name".to_string(); |
| 138 | + attempted.model = Some("new-model".to_string()); |
| 139 | + attempted.updated_at = "attempt".to_string(); |
| 140 | + let rollback = AgentUpdateRollback::new(previous, &attempted); |
| 141 | + let mut records = vec![attempted]; |
| 142 | + |
| 143 | + restore_agent_update(&mut records, "abcd1234", rollback) |
| 144 | + .expect("matching attempted update rolls back"); |
| 145 | + |
| 146 | + assert_eq!(records[0].name, "Old name"); |
| 147 | + assert_eq!(records[0].model, None); |
| 148 | + assert_eq!(records[0].updated_at, "before"); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn failed_profile_sync_does_not_overwrite_a_newer_agent_update() { |
| 153 | + let previous = record("Old name", "before"); |
| 154 | + let mut attempted = previous.clone(); |
| 155 | + attempted.name = "New name".to_string(); |
| 156 | + attempted.updated_at = "attempt".to_string(); |
| 157 | + let rollback = AgentUpdateRollback::new(previous, &attempted); |
| 158 | + let mut newer = attempted; |
| 159 | + newer.name = "Newest name".to_string(); |
| 160 | + newer.updated_at = "newer".to_string(); |
| 161 | + let mut records = vec![newer]; |
| 162 | + |
| 163 | + let error = restore_agent_update(&mut records, "abcd1234", rollback) |
| 164 | + .expect_err("a concurrent update must not be overwritten"); |
| 165 | + |
| 166 | + assert!(error.contains("changed again")); |
| 167 | + assert_eq!(records[0].name, "Newest name"); |
| 168 | + assert_eq!(records[0].updated_at, "newer"); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn failed_profile_sync_preserves_runtime_churn_while_restoring_configuration() { |
| 173 | + let previous = record("Old name", "before"); |
| 174 | + let mut attempted = previous.clone(); |
| 175 | + attempted.name = "New name".to_string(); |
| 176 | + attempted.model = Some("new-model".to_string()); |
| 177 | + attempted.updated_at = "attempt".to_string(); |
| 178 | + let rollback = AgentUpdateRollback::new(previous, &attempted); |
| 179 | + let mut churned = attempted; |
| 180 | + churned.runtime_pid = None; |
| 181 | + churned.last_stopped_at = Some("stopped".to_string()); |
| 182 | + churned.last_exit_code = Some(1); |
| 183 | + churned.last_error = Some("harness exited".to_string()); |
| 184 | + churned.updated_at = "runtime-change".to_string(); |
| 185 | + let mut records = vec![churned]; |
| 186 | + |
| 187 | + restore_agent_update(&mut records, "abcd1234", rollback) |
| 188 | + .expect("runtime-only churn must not prevent rollback"); |
| 189 | + |
| 190 | + assert_eq!(records[0].name, "Old name"); |
| 191 | + assert_eq!(records[0].model, None); |
| 192 | + assert_eq!(records[0].last_stopped_at.as_deref(), Some("stopped")); |
| 193 | + assert_eq!(records[0].last_exit_code, Some(1)); |
| 194 | + assert_eq!(records[0].last_error.as_deref(), Some("harness exited")); |
| 195 | + assert_eq!(records[0].updated_at, "runtime-change"); |
| 196 | + } |
| 197 | +} |
0 commit comments