Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(projects): keep group intact on refused removal
vastsad4dcf32d3fd0
diff --git a/crates/host-core/src/db/project_groups.rs b/crates/host-core/src/db/project_groups.rsindex a0c9fcec9..93d923895 100644--- a/crates/host-core/src/db/project_groups.rs+++ b/crates/host-core/src/db/project_groups.rs@@ -370,10 +370,11 @@ impl Database {}/// Removes one root from a stored group as part of that project's delete- /// (#1358). Unlike `update_project_group` this never rejects a path with- /// sessions — the caller deletes them right after — and it may remove the- /// primary, in which case the first remaining root becomes primary.- /// Removing the last root deletes the group record.+ /// (#1358). The caller preflights running sessions and then deletes this+ /// project's sessions as part of the same RPC flow. Unlike+ /// `update_project_group`, this may remove the fixed primary root; the+ /// first remaining root becomes primary. Removing the last root deletes+ /// the group record.pub fn remove_project_from_group(&self,id: &str,diff --git a/crates/host-core/src/db/tests.rs b/crates/host-core/src/db/tests.rsindex 0f9c38997..b4679290a 100644--- a/crates/host-core/src/db/tests.rs+++ b/crates/host-core/src/db/tests.rs@@ -1433,9 +1433,10 @@ fn remove_project_from_group_detaches_even_with_chats_and_may_move_primary() {Some(primary.to_string_lossy().into_owned()),).unwrap();+ let project_session_ids = db.project_session_ids(&group.primary_path).unwrap();assert_eq!(- db.project_session_ids(&group.primary_path).unwrap(),- [session.id]+ project_session_ids.as_slice(),+ std::slice::from_ref(&session.id));// Removing the primary moves the role to the first remaining root and@@ -1463,7 +1464,11 @@ fn remove_project_from_group_detaches_even_with_chats_and_may_move_primary() {.remove_project_from_group(&group.id, &canonical_first).unwrap();assert!(gone.is_none());- assert_eq!(db.list_project_groups().unwrap().len(), 0);+ assert!(!db+ .list_project_groups()+ .unwrap()+ .iter()+ .any(|candidate| candidate.id == group.id));// The chats of removed roots stay reachable for the caller's bulk delete.assert_eq!(db.project_session_ids(&group.primary_path).unwrap(),diff --git a/crates/host-core/src/rpc/mod.rs b/crates/host-core/src/rpc/mod.rsindex 5cbbd1a0b..5fb41e6f0 100644--- a/crates/host-core/src/rpc/mod.rs+++ b/crates/host-core/src/rpc/mod.rs@@ -1988,15 +1988,28 @@ async fn handle_request("CONFLICT",));}- // A path in a multi-folder project group is detached from the- // group as part of the delete: this handler already bulk-deletes- // the project's sessions, so requiring the user to remove the- // folder from the group first would deadlock against the group's- // own "still has chats" guard (#1358). The group keeps its- // primary when it is not the deleted path; otherwise the first- // remaining root becomes primary. A single-folder stored group is- // just a wrapper around one project, so removing that project- // also removes the now-empty group record.+ let session_ids = st+ .db+ .project_session_ids(&path)+ .map_err(|e| rpc_err(1000, e.to_string(), "INTERNAL"))?;+ // A running turn still owns its session's tools and working+ // directory and is still writing to that session's transcript, so+ // the bulk delete waits until every attached session is idle.+ for id in &session_ids {+ if sessions::session_has_running_turn(&st.db, id)+ .map_err(|e| rpc_err(1000, e.to_string(), "INTERNAL"))?+ {+ return Err(rpc_err(1008, "project has running sessions", "CONFLICT"));+ }+ }+ // Check for a running session before changing group membership.+ // A busy project must remain in its original group.+ // Once the project is known to be idle, detach its root as part of+ // this delete because the group editor keeps its primary root+ // fixed and preserves chats on detached non-primary roots (#1358).+ // If the primary is removed, the first remaining root becomes primary. A+ // single-folder group is just a wrapper around one project, so+ // deleting that project also removes the now-empty group record.if let Some(group) = st.db.stored_project_group_for_path(&path)@@ -2012,20 +2025,6 @@ async fn handle_request(.map_err(|e| rpc_err(1000, e.to_string(), "INTERNAL"))?;}}- let session_ids = st- .db- .project_session_ids(&path)- .map_err(|e| rpc_err(1000, e.to_string(), "INTERNAL"))?;- // A running turn still owns its session's tools and working- // directory and is still writing to that session's transcript, so- // the bulk delete waits until every attached session is idle.- for id in &session_ids {- if sessions::session_has_running_turn(&st.db, id)- .map_err(|e| rpc_err(1000, e.to_string(), "INTERNAL"))?- {- return Err(rpc_err(1008, "project has running sessions", "CONFLICT"));- }- }crate::scheduled::project::pause(&st.db, &path).map_err(|e| rpc_err(1000, e.to_string(), "INTERNAL"))?;let mut sessions_removed = 0;@@ -5815,7 +5814,7 @@ mod tests {}#[tokio::test]- async fn projects_remove_refuses_stored_group_root() {+ async fn projects_remove_detaches_stored_group_root_and_promotes_next_root() {let data_dir = tempfile::tempdir().unwrap();let grouped_dir = data_dir.path().join("grouped");let extra_dir = data_dir.path().join("extra");@@ -5824,47 +5823,57 @@ mod tests {let mut app_state = AppState::open(data_dir.path()).unwrap();app_state.handshook = true;let grouped_path = grouped_dir.to_string_lossy().to_string();+ let extra_path = extra_dir.to_string_lossy().to_string();app_state.db- .create_project_group(- "Grouped",- &[- grouped_path.clone(),- extra_dir.to_string_lossy().to_string(),- ],- )+ .create_project_group("Grouped", &[grouped_path.clone(), extra_path.clone()]).unwrap();let state = Arc::new(Mutex::new(app_state));- let error = handle_request(+ let removed = handle_request(state.clone(),"projects.remove",- json!({ "path": grouped_path }),+ json!({ "path": grouped_path.clone() }),mpsc::unbounded_channel().0,).await- .expect_err("a stored project group root must not be removed");- assert_eq!(error.code, 1002);- assert_eq!(- error.data.as_ref().and_then(|data| data.get("errorCode")),- Some(&json!("INVALID_PARAMS"))- );+ .expect("deleting a grouped project root also detaches it");+ assert_eq!(removed["removed"], json!(true));- let canonical =- crate::db::canonical_project_path(&grouped_path).expect("canonical project path");+ let canonical_grouped =+ crate::db::canonical_project_path(&grouped_path).expect("deleted project path");+ let canonical_extra =+ crate::db::canonical_project_path(&extra_path).expect("remaining project path");let projects = handle_request(- state,+ state.clone(),"projects.list",json!({}),mpsc::unbounded_channel().0,).await.unwrap();+ assert!(!projects["projects"]+ .as_array()+ .unwrap()+ .iter()+ .any(|project| project["path"].as_str() == Some(canonical_grouped.as_str())));assert!(projects["projects"].as_array().unwrap().iter()- .any(|project| project["path"].as_str() == Some(canonical.as_str())));+ .any(|project| project["path"].as_str() == Some(canonical_extra.as_str())));++ let groups = handle_request(+ state,+ "project.groups.list",+ json!({}),+ mpsc::unbounded_channel().0,+ )+ .await+ .unwrap();+ assert_eq!(groups["groups"].as_array().unwrap().len(), 1);+ assert_eq!(groups["groups"][0]["primaryPath"], json!(canonical_extra));+ assert_eq!(groups["groups"][0]["roots"].as_array().unwrap().len(), 1);}/// A single-folder stored project group is just a wrapper around one@@ -5915,13 +5924,23 @@ mod tests {/// A running turn owns its session's tools, working directory, and/// transcript writes, so the bulk delete waits until the project is idle.#[tokio::test]- async fn projects_remove_refuses_while_a_session_is_running() {+ async fn projects_remove_refuses_while_a_session_is_running_without_detaching_group() {let data_dir = tempfile::tempdir().unwrap();let project_dir = data_dir.path().join("busy-project");+ let remaining_dir = data_dir.path().join("remaining-project");fs::create_dir_all(&project_dir).unwrap();+ fs::create_dir_all(&remaining_dir).unwrap();let mut app_state = AppState::open(data_dir.path()).unwrap();app_state.handshook = true;let project_path = project_dir.to_string_lossy().to_string();+ let remaining_path = remaining_dir.to_string_lossy().to_string();+ app_state+ .db+ .create_project_group(+ "Busy group",+ &[project_path.clone(), remaining_path.clone()],+ )+ .unwrap();let session_id = sessions::create_session_with_options(&app_state.db,sessions::SessionCreateOptions {@@ -5972,6 +5991,25 @@ mod tests {.unwrap().iter().any(|project| project["path"].as_str() == Some(canonical.as_str())));+ let groups_while_busy = handle_request(+ state.clone(),+ "project.groups.list",+ json!({}),+ mpsc::unbounded_channel().0,+ )+ .await+ .unwrap();+ assert_eq!(+ groups_while_busy["groups"][0]["roots"]+ .as_array()+ .unwrap()+ .len(),+ 2+ );+ assert_eq!(+ groups_while_busy["groups"][0]["primaryPath"],+ json!(canonical)+ );let listed = handle_request(state.clone(),"session.list",@@ -5995,7 +6033,7 @@ mod tests {.await.unwrap();let removed = handle_request(- state,+ state.clone(),"projects.remove",json!({ "path": project_path }),mpsc::unbounded_channel().0,@@ -6004,6 +6042,28 @@ mod tests {.unwrap();assert_eq!(removed["removed"], json!(true));assert_eq!(removed["sessionsRemoved"], json!(1));+ let groups_after_delete = handle_request(+ state,+ "project.groups.list",+ json!({}),+ mpsc::unbounded_channel().0,+ )+ .await+ .unwrap();+ let remaining_canonical =+ crate::db::canonical_project_path(&remaining_path).expect("remaining project path");+ assert_eq!(groups_after_delete["groups"].as_array().unwrap().len(), 1);+ assert_eq!(+ groups_after_delete["groups"][0]["roots"]+ .as_array()+ .unwrap()+ .len(),+ 1+ );+ assert_eq!(+ groups_after_delete["groups"][0]["primaryPath"],+ json!(remaining_canonical)+ );}#[tokio::test]diff --git a/docs/spec/03-runtime/06-host-rpc-protocol.md b/docs/spec/03-runtime/06-host-rpc-protocol.mdindex 5a09a8d86..c41e40824 100644--- a/docs/spec/03-runtime/06-host-rpc-protocol.md+++ b/docs/spec/03-runtime/06-host-rpc-protocol.md@@ -222,10 +222,13 @@ type ToolBudgetHealth = {every session attached to it, removing those sessions' transcript, scratch,and review files and the project's durable memory, and never touching theproject folder on disk. Idempotent: an unknown path returns- `{ removed: false, sessionsRemoved: 0 }`. A path that is a root of a stored- multi-folder project group is refused so the group keeps a valid primary root,- and the call is refused (1008 / `CONFLICT`) while any attached session has a- running turn, so a live turn never loses the transcript it is writing.+ `{ removed: false, sessionsRemoved: 0 }`. If the path belongs to a stored+ project group, deletion detaches that root in the same flow; deleting the+ primary promotes the first remaining root, and deleting the last root also+ removes the group record. The call is refused (1008 / `CONFLICT`) while any+ attached session has a running turn, before changing group membership, so a+ live turn never loses the transcript it is writing and a rejected delete+ leaves the project group unchanged.- `project.memory.get({ path })` — returns the durable memory for the canonicalproject path, or an empty record when no memory has been saved- `project.memory.set({ path, entries })` — normalizes and stores visual memorydiff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.mdindex 07c2baf10..38224374a 100644--- a/docs/spec/04-ux/08-component-spec.md+++ b/docs/spec/04-ux/08-component-spec.md@@ -727,7 +727,7 @@ controls.| Project reorder | press-and-move on the title (8px), or ArrowUp/ArrowDown on that title, writes contiguous normalized-path order to sidebar preferences; accent insertion line; no visible grip || Project archive | omitted from default view; restorable from archived view || Project close | removes retained tab only; durable project/sessions remain |-| Project delete | row-menu danger action behind a second confirmation that names the project and the number of its sessions; refused with a message while any of those sessions is running; removes the durable project row, those sessions, their transcripts, and its project memory; never deletes the folder on disk; a path still owned by a multi-folder project group is refused with a message until detached in Edit project, and a path the host no longer knows is still removed from the list. Detaching a root with chats preserves them under a standalone project |+| Project delete | row-menu danger action behind a second confirmation that names the project and the number of its sessions; refused with a message while any of those sessions is running and leaves group membership unchanged; removes the durable project row, those sessions, their transcripts, and its project memory; never deletes the folder on disk; deleting a root of a multi-folder project group detaches it as part of deletion, promotes the first remaining root when the primary is deleted, and removes the group record when its last root is deleted. A path the host no longer knows is still removed from the list. Detaching a root with chats through Edit project preserves them under a standalone project || Project memory | row-menu editor reads and saves a compact list of titled or untitled memory cards for the exact project path; cards can be added, edited, and removed, the context is available in later chats, and it is never a higher-priority instruction || Session list | exact-path matches only; no basename grouping || Active group | exactly one group reflects the selected host workspace |diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.mdindex bd4b51612..c1eb88c8d 100644--- a/docs/spec/06-delivery/04-e2e-test-plan.md+++ b/docs/spec/06-delivery/04-e2e-test-plan.md@@ -2476,6 +2476,27 @@ identify the platform validation still needed.- **Milestone**: M3- **Status**: Source-level regression covered; full UI scenario Draft+#### E2E-012b: Delete a project root from a multi-folder group++- **Preconditions**: A two-root project group has sessions in both roots; one+ session in the root being deleted can be started and stopped.+- **Steps**:+ 1. Start a turn in the primary root, then request deletion of that project+ and confirm the action.+ 2. Confirm the running-session conflict and inspect the group and both+ session lists.+ 3. Let the turn finish, delete the primary project, and confirm again.+ 4. Inspect the remaining group, its sessions, shared instructions and+ memory, and both directories on disk.+- **Expected**: A running session blocks deletion without changing group+ membership or deleting data. After the session is idle, deletion removes the+ selected root, its sessions and project memory, promotes the first remaining+ root when needed, and preserves the remaining root's sessions and group-level+ instructions and memory. The original folders remain on disk.+- **Specs linked**: `03-runtime/06-host-rpc-protocol.md` (Projects),+ `04-ux/08-component-spec.md` (§3.9)+- **Status**: Draft+#### E2E-013: Read-only tools work in project- **Preconditions**: Project directory open.