fix(worktree): subagent sessions kept when deleting worktree group from sidebar (#1806)

* fix(worktree): include sessions when deleting worktree group from sidebar

allGroupSessions was guarded by group.isArchivedBucket, returning [] for
active worktree groups. This caused the 'delete worktree' button in the
sidebar to send an empty session list — SessionDialogs only removed the
git worktree directory and skipped archiving any sessions, leaving them
orphaned.

Remove the guard so all sessions (including recursive children /
subagent sessions) are collected regardless of archived state.

* fix(sessions): delete all descendants on hard-delete instead of relying on server cascade

The previous code sent only the root session ID and assumed the server
would cascade-delete all children. If the cascade failed, children were
left orphaned. Delete root + descendants individually; 404 responses
from already-cascade-deleted children are treated as success.

* fix(sessions): clear worktree metadata when deleting a session

Deleted sessions kept their worktree attachment in both
session-worktree-store and session-ui-store. Clean it up on successful
deletion and on 404 (already deleted).

* fix(worktree): search subagent sessions across all directories before delete

WorktreeSectionContent and BranchPickerDialog used useSessions(), which
is scoped to the current sync directory. Subagent sessions created in
other worktrees/project roots were missed and left orphaned. Search
across active + archived global sessions when collecting descendants.

---------

Co-authored-by: Leonid Skorobogatyy <bash@opencode.itc.local>
Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
This commit is contained in:
bashrusakh
2026-06-29 00:42:13 +03:00
committed by GitHub
co-authored by Leonid Skorobogatyy Bohdan Triapitsyn
parent ca64f1a886
commit c184ddf185
4 changed files with 33 additions and 16 deletions
@@ -675,12 +675,13 @@ function SessionGroupSectionBase(props: Props): React.ReactNode {
return collected;
}, []);
// The "delete all in group" handler closes over the full list of
// sessions in this group. Memoize so the recursive flatten only runs
// when the underlying source group nodes change, not on every render.
// Flat list of all sessions in this group (including nested children).
// Used by both the "delete all archived" button and the "delete worktree"
// button. Memoize so the recursive flatten only runs when the underlying
// source group nodes change, not on every render.
const allGroupSessions = React.useMemo(
() => (group.isArchivedBucket ? collectGroupSessions(sourceGroupNodes) : []),
[collectGroupSessions, sourceGroupNodes, group.isArchivedBucket],
() => collectGroupSessions(sourceGroupNodes),
[collectGroupSessions, sourceGroupNodes],
);
// Precompute the per-folder "delete all sessions in folder" list once
@@ -217,13 +217,12 @@ export const useSessionActions = (args: Args) => {
const ids = [session.id, ...descendantIds];
if (shouldHardDelete) {
// The server cascade-deletes all descendant sessions when the parent
// is removed. Only send the root session delete request; sending
// individual requests for each descendant would hit 404 (already
// deleted by cascade) and trigger rollback that restores them.
const success = await args.deleteSession(session.id);
if (success) {
const totalDeleted = descendantIds.length + 1;
// Delete root + all descendants individually. If the server
// cascade-deletes some children before we get to them, 404 is
// treated as success by deleteSession and no rollback occurs.
const { deletedIds, failedIds } = await args.deleteSessions(ids);
if (failedIds.length === 0) {
const totalDeleted = deletedIds.length;
toast.success(totalDeleted === 1
? t('sessions.sidebar.bulkActions.deletedSingle', { count: totalDeleted })
: t('sessions.sidebar.bulkActions.deletedPlural', { count: totalDeleted }));