From 8a25373fb98bc60b053311dadd7b1e75cd96074c Mon Sep 17 00:00:00 2001 From: Ben Brandt Date: Tue, 17 Mar 2026 11:26:06 +0100 Subject: [PATCH] agent: Save threads before closing them (#51744) Closes #46078 When we unloaded sessions, we weren't necesarily making sure we had saved the current state. Now we save it whenever we remove it from the session map. Release Notes: - Agent: Fix cases where thread state wasn't saved before closing the thread. --- crates/agent/src/agent.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/agent/src/agent.rs b/crates/agent/src/agent.rs index d4062fec85cb458ade372085d23fa42a47e631ed..140c872ecacc9d572881d2745bbbfc2810b1d309 100644 --- a/crates/agent/src/agent.rs +++ b/crates/agent/src/agent.rs @@ -1429,15 +1429,16 @@ impl acp_thread::AgentConnection for NativeAgentConnection { session_id: &acp::SessionId, cx: &mut App, ) -> Task> { - self.0.update(cx, |agent, _cx| { - let project_id = agent.sessions.get(session_id).map(|s| s.project_id); - agent.sessions.remove(session_id); - - if let Some(project_id) = project_id { - let has_remaining = agent.sessions.values().any(|s| s.project_id == project_id); - if !has_remaining { - agent.projects.remove(&project_id); - } + self.0.update(cx, |agent, cx| { + let Some(session) = agent.sessions.remove(session_id) else { + return; + }; + let project_id = session.project_id; + agent.save_thread(session.thread, cx); + + let has_remaining = agent.sessions.values().any(|s| s.project_id == project_id); + if !has_remaining { + agent.projects.remove(&project_id); } }); Task::ready(Ok(()))