Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(projects): break the group-removal deadlock in projects.remove

yexisuc215dbdb5ee5
diff --git a/crates/host-core/src/db/project_groups.rs b/crates/host-core/src/db/project_groups.rs
index b5719b432..a0c9fcec9 100644
--- a/crates/host-core/src/db/project_groups.rs
+++ b/crates/host-core/src/db/project_groups.rs
@@ -369,6 +369,80 @@ impl Database {
Ok(group)
}
+ /// 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.
+ pub fn remove_project_from_group(
+ &self,
+ id: &str,
+ path: &str,
+ ) -> Result<Option<ProjectGroupRecord>> {
+ let Some(current) = self.group_by_id(id.trim())? else {
+ return Err(anyhow!("project group not found"));
+ };
+ if current.legacy {
+ return Ok(Some(current));
+ }
+ let Some(removed) = current
+ .roots
+ .iter()
+ .find(|root| root.path == path)
+ .map(|root| root.path.clone())
+ else {
+ return Ok(Some(current));
+ };
+ let remaining: Vec<String> = current
+ .roots
+ .iter()
+ .map(|root| root.path.clone())
+ .filter(|candidate| candidate != &removed)
+ .collect();
+ if remaining.is_empty() {
+ self.delete_project_group_record(&current.id)?;
+ return Ok(None);
+ }
+ // The primary moves to the first remaining root when it is removed.
+ let primary = if current.primary_path == removed {
+ remaining[0].clone()
+ } else {
+ current.primary_path.clone()
+ };
+ let ordered = vec![primary.clone()];
+ let mut ordered = ordered;
+ ordered.extend(
+ remaining
+ .into_iter()
+ .filter(|candidate| candidate != &primary),
+ );
+
+ let now = now_ms();
+ let roots = ordered
+ .iter()
+ .enumerate()
+ .map(|(position, candidate)| ProjectGroupRoot {
+ path: candidate.clone(),
+ name: project_display_name(candidate),
+ position: position as i64,
+ })
+ .collect::<Vec<_>>();
+ let group = ProjectGroupRecord {
+ id: current.id,
+ name: current.name,
+ primary_path: primary,
+ roots,
+ created_at: current.created_at,
+ updated_at: now,
+ pinned: current.pinned,
+ last_opened_at: current.last_opened_at,
+ legacy: false,
+ detached_paths: current.detached_paths,
+ };
+ self.kv_set(GROUP_NAMESPACE, &group.id, &serde_json::to_value(&group)?)?;
+ Ok(Some(group))
+ }
+
pub fn rename_project_group(&self, id: &str, name: &str) -> Result<ProjectGroupRecord> {
let name = name.trim();
if name.is_empty() || name.chars().count() > MAX_PROJECT_GROUP_NAME_CHARS {
diff --git a/crates/host-core/src/db/tests.rs b/crates/host-core/src/db/tests.rs
index 4c67a7758..0f9c38997 100644
--- a/crates/host-core/src/db/tests.rs
+++ b/crates/host-core/src/db/tests.rs
@@ -1402,6 +1402,75 @@ fn project_group_update_detaches_roots_with_chats_without_orphaning_them() {
);
}
+#[test]
+fn remove_project_from_group_detaches_even_with_chats_and_may_move_primary() {
+ let dir = tempfile::tempdir().unwrap();
+ let primary = dir.path().join("primary");
+ let first = dir.path().join("first");
+ let second = dir.path().join("second");
+ std::fs::create_dir_all(&primary).unwrap();
+ std::fs::create_dir_all(&first).unwrap();
+ std::fs::create_dir_all(&second).unwrap();
+ let db = Database::open(&dir.path().join("pi.sqlite")).unwrap();
+ let group = db
+ .create_project_group(
+ "Deadlock",
+ &[
+ primary.to_string_lossy().into(),
+ first.to_string_lossy().into(),
+ second.to_string_lossy().into(),
+ ],
+ )
+ .unwrap();
+ // The project has chats: `update_project_group` refuses to detach it, so
+ // `projects.remove` must not depend on that path (#1358).
+ let session = crate::sessions::create_session(
+ &db,
+ Some("Blocked chat".into()),
+ Some("agent".into()),
+ None,
+ None,
+ Some(primary.to_string_lossy().into_owned()),
+ )
+ .unwrap();
+ assert_eq!(
+ db.project_session_ids(&group.primary_path).unwrap(),
+ [session.id]
+ );
+
+ // Removing the primary moves the role to the first remaining root and
+ // keeps the other root in the group.
+ let after_primary = db
+ .remove_project_from_group(&group.id, &group.primary_path)
+ .unwrap()
+ .expect("group survives removing its primary");
+ let canonical_first = crate::db::canonical_project_path(&first.to_string_lossy()).unwrap();
+ let canonical_second = crate::db::canonical_project_path(&second.to_string_lossy()).unwrap();
+ assert_eq!(after_primary.primary_path, canonical_first);
+ assert_eq!(after_primary.roots.len(), 2);
+ assert_eq!(after_primary.roots[0].path, canonical_first);
+
+ // Removing a non-primary root keeps the primary in place.
+ let after_second = db
+ .remove_project_from_group(&group.id, &canonical_second)
+ .unwrap()
+ .unwrap();
+ assert_eq!(after_second.primary_path, canonical_first);
+ assert_eq!(after_second.roots.len(), 1);
+
+ // Removing the last root deletes the group record.
+ let gone = db
+ .remove_project_from_group(&group.id, &canonical_first)
+ .unwrap();
+ assert!(gone.is_none());
+ assert_eq!(db.list_project_groups().unwrap().len(), 0);
+ // The chats of removed roots stay reachable for the caller's bulk delete.
+ assert_eq!(
+ db.project_session_ids(&group.primary_path).unwrap(),
+ [session.id]
+ );
+}
+
#[test]
fn editing_a_legacy_project_with_an_extra_folder_upgrades_it_to_a_group() {
let dir = tempfile::tempdir().unwrap();
diff --git a/crates/host-core/src/rpc/mod.rs b/crates/host-core/src/rpc/mod.rs
index b81ff2c94..5cbbd1a0b 100644
--- a/crates/host-core/src/rpc/mod.rs
+++ b/crates/host-core/src/rpc/mod.rs
@@ -1988,26 +1988,29 @@ async fn handle_request(
"CONFLICT",
));
}
- // A path that belongs to a multi-folder project group must stay put:
- // deleting one root would orphan the rest of the group, so callers
- // remove the folder from the group first. A single-folder stored
- // group is just a wrapper around one project, so removing that
- // project also removes the now-empty group record.
+ // 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.
if let Some(group) = st
.db
.stored_project_group_for_path(&path)
.map_err(|e| rpc_err(1000, e.to_string(), "INTERNAL"))?
{
if group.roots.len() > 1 {
- return Err(rpc_err(
- 1002,
- "project belongs to a multi-folder project group; remove the folder from the group first",
- "INVALID_PARAMS",
- ));
+ st.db
+ .remove_project_from_group(&group.id, &path)
+ .map_err(|e| rpc_err(1000, e.to_string(), "INTERNAL"))?;
+ } else {
+ st.db
+ .delete_project_group_record(&group.id)
+ .map_err(|e| rpc_err(1000, e.to_string(), "INTERNAL"))?;
}
- st.db
- .delete_project_group_record(&group.id)
- .map_err(|e| rpc_err(1000, e.to_string(), "INTERNAL"))?;
}
let session_ids = st
.db