Skip to content

Commit 99a4a25

Browse files
committed
fix(web): make bulk thread deletion reliable
Respect deletion confirmations and retain the last thread for a worktree until cleanup succeeds. Keep failed deletions selected for retry, serialize worktree cleanup per repository, and recheck shared references before removing files. Implemented with GPT-6 Astra (Codex) in T3 Code. Signed-off-by: 0x00-sys <[email protected]>
1 parent f17165a commit 99a4a25

7 files changed

Lines changed: 549 additions & 126 deletions

File tree

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,25 +1957,36 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
19571957
const confirmed = await api.dialogs.confirm(
19581958
[
19591959
`Delete ${count} thread${count === 1 ? "" : "s"}?`,
1960-
"This permanently clears conversation history for these threads.",
1960+
"This permanently clears conversation history and deletes worktrees no other threads use.",
19611961
].join("\n"),
19621962
{ variant: "destructive" },
19631963
);
19641964
if (!confirmed) return;
19651965
}
19661966

1967+
const deletionToast = toastManager.add({
1968+
type: "loading",
1969+
title: `Deleting ${count} thread${count === 1 ? "" : "s"}…`,
1970+
timeout: 0,
1971+
});
19671972
const { deletedThreadKeys, firstFailure } = await deleteSelectedThreadEntries({
19681973
entries: selectedThreadEntries,
1969-
delete: ({ threadRef }, deletedThreadKeys) =>
1970-
deleteThread(threadRef, { deletedThreadKeys }),
1974+
delete: ({ threadRef }, deletedThreadKeys, deferDeletion) =>
1975+
deleteThread(threadRef, {
1976+
deletedThreadKeys,
1977+
deferDeletion,
1978+
worktreeDeletionConfirmed: true,
1979+
}),
19711980
});
1981+
toastManager.close(deletionToast);
19721982
if (firstFailure !== null) {
19731983
const firstError = squashAtomCommandFailure(firstFailure);
19741984
toastManager.add(
19751985
stackedThreadToast({
19761986
type: "error",
1977-
title: "Failed to delete threads",
1978-
description: firstError instanceof Error ? firstError.message : "An error occurred.",
1987+
title: "Some threads could not be deleted",
1988+
description: `${firstError instanceof Error ? firstError.message : "An error occurred."} Remaining threads were kept; you can retry deleting them.`,
1989+
timeout: 0,
19791990
}),
19801991
);
19811992
}

‎apps/web/src/components/Sidebar.logic.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,25 @@ export async function deleteSelectedThreadEntries<
408408
delete: (
409409
entry: TEntry,
410410
deletedThreadKeys: ReadonlySet<string>,
411+
deferDeletion: (deleteThread: () => Promise<AtomCommandResult<unknown, unknown>>) => void,
411412
) => Promise<AtomCommandResult<unknown, unknown> | null>;
412413
}) {
413414
const deletedThreadKeys = new Set<string>();
415+
const pendingDeletions: Array<Promise<void>> = [];
414416
let firstFailure: AsyncResult.Failure<unknown, unknown> | null = null;
415417

416418
for (const entry of input.entries) {
417-
const result = await input.delete(entry, deletedThreadKeys);
418-
if (result === null) continue;
419+
let deferred = false;
420+
const result = await input.delete(entry, deletedThreadKeys, (deleteThread) => {
421+
deferred = true;
422+
pendingDeletions.push(
423+
deleteThread().then((result) => {
424+
if (result._tag === "Success") deletedThreadKeys.add(entry.threadKey);
425+
else if (!isAtomCommandInterrupted(result)) firstFailure ??= result;
426+
}),
427+
);
428+
});
429+
if (result === null || deferred) continue;
419430
if (result._tag === "Failure") {
420431
if (isAtomCommandInterrupted(result)) break;
421432
firstFailure ??= result;
@@ -424,6 +435,9 @@ export async function deleteSelectedThreadEntries<
424435
deletedThreadKeys.add(entry.threadKey);
425436
}
426437

438+
// Worktree removal must succeed before its last thread disappears. Independent
439+
// deletions can overlap; the VCS scheduler serializes work within each repository.
440+
await Promise.all(pendingDeletions);
427441
return { deletedThreadKeys, firstFailure };
428442
}
429443

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3955,30 +3955,39 @@ export default function Sidebar() {
39553955
api.dialogs.confirm(
39563956
[
39573957
`Delete ${count} thread${count === 1 ? "" : "s"}?`,
3958-
"This permanently clears conversation history for these threads.",
3958+
"This permanently clears conversation history and deletes worktrees no other threads use.",
39593959
].join("\n"),
39603960
{ variant: "destructive" },
39613961
),
39623962
);
39633963
if (confirmed._tag === "Failure" || !confirmed.value) return;
39643964
}
3965+
const deletionToast = toastManager.add({
3966+
type: "loading",
3967+
title: `Deleting ${count} thread${count === 1 ? "" : "s"}…`,
3968+
timeout: 0,
3969+
});
39653970
const { deletedThreadKeys, firstFailure } = await deleteSelectedThreadEntries({
39663971
entries: threadKeys.map((threadKey) => ({ threadKey })),
3967-
delete: async ({ threadKey }, deletedThreadKeys) => {
3972+
delete: async ({ threadKey }, deletedThreadKeys, deferDeletion) => {
39683973
const thread = threadByKeyRef.current.get(threadKey);
39693974
if (!thread) return null;
39703975
return deleteThread(scopeThreadRef(thread.environmentId, thread.id), {
39713976
deletedThreadKeys,
3977+
deferDeletion,
3978+
worktreeDeletionConfirmed: true,
39723979
});
39733980
},
39743981
});
3982+
toastManager.close(deletionToast);
39753983
if (firstFailure !== null) {
39763984
const firstError = squashAtomCommandFailure(firstFailure);
39773985
toastManager.add(
39783986
stackedThreadToast({
39793987
type: "error",
3980-
title: "Failed to delete threads",
3981-
description: firstError instanceof Error ? firstError.message : "An error occurred.",
3988+
title: "Some threads could not be deleted",
3989+
description: `${firstError instanceof Error ? firstError.message : "An error occurred."} Remaining threads were kept; you can retry deleting them.`,
3990+
timeout: 0,
39823991
}),
39833992
);
39843993
}

‎apps/web/src/components/settings/SettingsPanels.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3005,7 +3005,7 @@ export function GeneralSettingsPanel() {
30053005

30063006
<SettingsRow
30073007
{...searchableSetting("delete-confirmation")}
3008-
description="Ask before deleting a thread and its chat history."
3008+
description="Ask before deleting threads and their worktrees. When off, worktrees no other threads use are deleted too."
30093009
resetAction={
30103010
settings.confirmThreadDelete !== DEFAULT_UNIFIED_SETTINGS.confirmThreadDelete ? (
30113011
<SettingResetButton

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,7 @@ export function useThreadActionMenu(input: {
315315
if (confirmed._tag === "Failure" || !confirmed.value) return;
316316
}
317317
const deleted = await deleteThread(threadRef);
318-
if (
319-
deleted._tag === "Failure" &&
320-
!isAtomCommandInterrupted(deleted) &&
321-
// A failure with the thread already gone is worktree cleanup
322-
// failing after a successful delete — deleteThread has toasted
323-
// that itself, and "Failed to delete thread" would be a lie.
324-
readThreadShell(threadRef) !== null
325-
) {
318+
if (deleted._tag === "Failure" && !isAtomCommandInterrupted(deleted)) {
326319
failureToast("Failed to delete thread", squashAtomCommandFailure(deleted));
327320
}
328321
return;

0 commit comments

Comments
 (0)