Skip to content

Commit 85af072

Browse files
committed
fix(web): report worktree cleanup failures and clear stale selections
1 parent 3c6b1e7 commit 85af072

5 files changed

Lines changed: 102 additions & 12 deletions

File tree

‎apps/web/src/components/LegacySidebar.tsx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ import {
171171
SidebarMenuSubItem,
172172
useSidebar,
173173
} from "./ui/sidebar";
174-
import { useThreadSelectionStore } from "../threadSelectionStore";
174+
import {
175+
getThreadKeysToDeselectAfterDelete,
176+
useThreadSelectionStore,
177+
} from "../threadSelectionStore";
175178
import { isCommandPaletteOpen, openCommandPalette } from "../commandPaletteBus";
176179
import {
177180
archiveSelectedThreadEntries,
@@ -1933,7 +1936,12 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
19331936
}),
19341937
);
19351938
}
1936-
removeFromSelection([...deletedThreadKeys]);
1939+
removeFromSelection(
1940+
getThreadKeysToDeselectAfterDelete(threadKeys, deletedThreadKeys, (threadKey) => {
1941+
const threadRef = parseScopedThreadKey(threadKey);
1942+
return threadRef !== null && readThreadShell(threadRef) !== null;
1943+
}),
1944+
);
19371945
},
19381946
[
19391947
appSettingsConfirmThreadArchive,

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import { resolveSettledThreadTimestamp } from "@t3tools/client-runtime/state/thread-sort";
2626
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models";
2727
import {
28+
parseScopedThreadKey,
2829
scopeProjectRef,
2930
scopeThreadRef,
3031
scopedThreadKey,
@@ -101,7 +102,10 @@ import {
101102
type SidebarProjectSnapshot,
102103
} from "../sidebarProjectGrouping";
103104
import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore";
104-
import { useThreadSelectionStore } from "../threadSelectionStore";
105+
import {
106+
getThreadKeysToDeselectAfterDelete,
107+
useThreadSelectionStore,
108+
} from "../threadSelectionStore";
105109
import { useThreadActions } from "../hooks/useThreadActions";
106110
import { useHandleNewThread } from "../hooks/useHandleNewThread";
107111
import { isCommandPaletteOpen, openCommandPalette } from "../commandPaletteBus";
@@ -112,6 +116,7 @@ import { useLocalStorage } from "../hooks/useLocalStorage";
112116
import { useNowMinute } from "../hooks/useNowMinute";
113117
import { useEnvironments, usePrimaryEnvironmentId } from "../state/environments";
114118
import {
119+
readThreadShell,
115120
useAllEnvironmentProjectSnapshotsReady,
116121
useProjects,
117122
useThreadShells,
@@ -3021,8 +3026,9 @@ export default function Sidebar() {
30213026
// right now. Selections can outlive their rows (settled-tail paging,
30223027
// thread deletion elsewhere) and the menu labels must count only what
30233028
// the actions will touch.
3024-
const threadKeys = [...useThreadSelectionStore.getState().selectedThreadKeys].filter(
3025-
(threadKey) => threadByKeyRef.current.has(threadKey),
3029+
const selectedThreadKeys = [...useThreadSelectionStore.getState().selectedThreadKeys];
3030+
const threadKeys = selectedThreadKeys.filter((threadKey) =>
3031+
threadByKeyRef.current.has(threadKey),
30263032
);
30273033
if (threadKeys.length === 0) return;
30283034
const count = threadKeys.length;
@@ -3241,7 +3247,12 @@ export default function Sidebar() {
32413247
}),
32423248
);
32433249
}
3244-
removeFromSelection([...deletedThreadKeys]);
3250+
removeFromSelection(
3251+
getThreadKeysToDeselectAfterDelete(selectedThreadKeys, deletedThreadKeys, (threadKey) => {
3252+
const threadRef = parseScopedThreadKey(threadKey);
3253+
return threadRef !== null && readThreadShell(threadRef) !== null;
3254+
}),
3255+
);
32453256
},
32463257
[
32473258
attemptSettle,

‎apps/web/src/hooks/useThreadActions.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,10 @@ export function useThreadActions() {
444444
? refreshResult
445445
: null;
446446
if (cleanupFailure) {
447+
const removalFailed = removeResult._tag === "Failure";
447448
const error = squashAtomCommandFailure(cleanupFailure);
448-
const message = error instanceof Error ? error.message : "Unknown error removing worktree.";
449-
console.error("Failed to remove orphaned worktree after thread deletion", {
449+
const message = error instanceof Error ? error.message : "An error occurred.";
450+
console.error("Worktree cleanup failed after thread deletion", {
450451
threadId: threadRef.threadId,
451452
projectCwd: threadProject.workspaceRoot,
452453
worktreePath: orphanedWorktreePath,
@@ -455,11 +456,16 @@ export function useThreadActions() {
455456
toastManager.add(
456457
stackedThreadToast({
457458
type: "error",
458-
title: "Thread deleted, but worktree removal failed",
459-
description: `Could not remove ${displayWorktreePath ?? orphanedWorktreePath}. ${message}`,
459+
title: removalFailed
460+
? "Failed to delete worktree"
461+
: "Worktree deleted, but Git status refresh failed",
462+
description: removalFailed
463+
? `Could not remove ${displayWorktreePath ?? orphanedWorktreePath}. ${message}`
464+
: message,
460465
}),
461466
);
462-
return cleanupFailure;
467+
// The thread was deleted. Cleanup has its own toast; returning its
468+
// failure would make callers incorrectly report a thread deletion error.
463469
}
464470
return deleteResult;
465471
},

‎apps/web/src/threadSelectionStore.test.ts‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { ThreadId } from "@t3tools/contracts";
22
import { beforeEach, describe, expect, it } from "vite-plus/test";
33

4-
import { useThreadSelectionStore } from "./threadSelectionStore";
4+
import {
5+
getThreadKeysToDeselectAfterDelete,
6+
useThreadSelectionStore,
7+
} from "./threadSelectionStore";
58

69
const THREAD_A = ThreadId.make("thread-a");
710
const THREAD_B = ThreadId.make("thread-b");
@@ -16,6 +19,59 @@ describe("threadSelectionStore", () => {
1619
useThreadSelectionStore.getState().clearSelection();
1720
});
1821

22+
describe("bulk deletion cleanup", () => {
23+
it("clears missing selection rows and completed deletions while retaining a failed thread", () => {
24+
const store = useThreadSelectionStore.getState();
25+
store.toggleThread(THREAD_A);
26+
store.toggleThread(THREAD_B);
27+
store.toggleThread(THREAD_C);
28+
const selected = [...useThreadSelectionStore.getState().selectedThreadKeys];
29+
// A deleted successfully but its shell has not refreshed; B failed;
30+
// C was deleted elsewhere and never entered this client's delete loop.
31+
const existingThreads = new Set([THREAD_A, THREAD_B]);
32+
store.removeFromSelection(
33+
getThreadKeysToDeselectAfterDelete(selected, new Set([THREAD_A]), (key) =>
34+
existingThreads.has(ThreadId.make(key)),
35+
),
36+
);
37+
38+
expect([...useThreadSelectionStore.getState().selectedThreadKeys]).toEqual([THREAD_B]);
39+
expect(useThreadSelectionStore.getState().anchorThreadKey).toBeNull();
40+
});
41+
42+
it("exits selection mode when the last selected thread disappeared elsewhere", () => {
43+
const store = useThreadSelectionStore.getState();
44+
store.toggleThread(THREAD_A);
45+
store.removeFromSelection(
46+
getThreadKeysToDeselectAfterDelete([THREAD_A], new Set(), () => false),
47+
);
48+
49+
expect(useThreadSelectionStore.getState().hasSelection()).toBe(false);
50+
expect(useThreadSelectionStore.getState().anchorThreadKey).toBeNull();
51+
});
52+
53+
it("keeps unprocessed and hidden live threads and selections added while deletion was pending", () => {
54+
const store = useThreadSelectionStore.getState();
55+
store.toggleThread(THREAD_A);
56+
store.toggleThread(THREAD_B);
57+
store.toggleThread(THREAD_C);
58+
const selected = [...useThreadSelectionStore.getState().selectedThreadKeys];
59+
store.toggleThread(THREAD_D);
60+
// Only A completed before interruption. B is unprocessed; C still
61+
// has a shell even though its row is outside the rendered page.
62+
store.removeFromSelection(
63+
getThreadKeysToDeselectAfterDelete(selected, new Set([THREAD_A]), () => true),
64+
);
65+
66+
expect([...useThreadSelectionStore.getState().selectedThreadKeys]).toEqual([
67+
THREAD_B,
68+
THREAD_C,
69+
THREAD_D,
70+
]);
71+
expect(useThreadSelectionStore.getState().anchorThreadKey).toBe(THREAD_D);
72+
});
73+
});
74+
1975
describe("toggleThread", () => {
2076
it("adds a thread to empty selection", () => {
2177
useThreadSelectionStore.getState().toggleThread(THREAD_A);

‎apps/web/src/threadSelectionStore.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ interface ThreadSelectionStore extends ThreadSelectionState {
3434

3535
const EMPTY_SET = new Set<string>();
3636

37+
/** Clear completed deletions and missing threads, retaining failed or unprocessed threads. */
38+
export function getThreadKeysToDeselectAfterDelete(
39+
selectedThreadKeys: readonly string[],
40+
deletedThreadKeys: ReadonlySet<string>,
41+
hasThread: (threadKey: string) => boolean,
42+
): string[] {
43+
return selectedThreadKeys.filter((key) => deletedThreadKeys.has(key) || !hasThread(key));
44+
}
45+
3746
export const useThreadSelectionStore = create<ThreadSelectionStore>((set, get) => ({
3847
selectedThreadKeys: EMPTY_SET,
3948
anchorThreadKey: null,

0 commit comments

Comments
 (0)