fix(web): clean up worktrees when deleting archived threads #9116
Closedlnieuwenhuis(GitHub profile, opens in a new tab)wants to merge 5 commits into
main fromrefs/pull/9116/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/lib/archivedThreadsState.ts lines 43 to 44OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -26,3 +28,13 @@2628 appAtomRegistry.refresh(archivedSnapshotAtom(environmentId));2729 }283031+/** One-shot fetch of an environment's archived thread shells. The archived32+ snapshot atom is only mounted while Settings → Archived is open, so a33+ synchronous read can miss; this awaits a live result instead. Returns null34+ on failure so callers can fail soft. */35+export async function fetchArchivedThreadShells(36+ environmentId: EnvironmentId,37+): Promise<ReadonlyArray<EnvironmentThreadShell> | null> {38+ const result = await executeAtomQuery(appAtomRegistry, archivedSnapshotAtom(environmentId), {39+ reportDefect: false,40+ reportFailure: false,apps/web/src/hooks/useThreadActions.ts lines 375 to 378OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -316,6 +362,17 @@316362 ? threads.filter((entry) => entry.id === threadRef.threadId || !deletedIds.has(entry.id))317363 : threads;318- const orphanedWorktreePath = getOrphanedWorktreePathForThread(319- survivingThreads,320- threadRef.threadId,321- );364+ // `threads` only holds active threads; an archived target must be added.365+ const orphanCheckThreads = survivingThreads.some((entry) => entry.id === threadRef.threadId)366+ ? survivingThreads367+ : [...survivingThreads, thread];368+ // Archived siblings still link the worktree, so they count in the orphan369+ // check. Reuse the fetch from target resolution when there was one.370+ const orphanedWorktreePath = await resolveOrphanedWorktreePathForDelete({371+ threads: orphanCheckThreads,372+ threadId: threadRef.threadId,373+ fetchArchivedThreads: async () => {374+ const archivedThreads =375+ resolvedArchivedThreads ?? (await fetchArchivedThreadShells(threadRef.environmentId));376+ return archivedThreads && deletedIds && deletedIds.size > 0377+ ? archivedThreads.filter((entry) => !deletedIds.has(entry.id))378+ : archivedThreads;
- apps/web/src/lib/archivedThreadsState.ts line 49Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -26,3 +28,19 @@2628 appAtomRegistry.refresh(archivedSnapshotAtom(environmentId));2729 }283031+/** One-shot fetch of an environment's archived thread shells. The archived32+ snapshot atom is only mounted while Settings → Archived is open, so a33+ synchronous read can miss; this awaits a live result instead. Returns null34+ on failure so callers can fail soft. */35+export async function fetchArchivedThreadShells(36+ environmentId: EnvironmentId,37+): Promise<ReadonlyArray<EnvironmentThreadShell> | null> {38+ const result = await executeAtomQuery(appAtomRegistry, archivedSnapshotAtom(environmentId), {39+ reportDefect: false,40+ reportFailure: false,41+ });42+ if (result._tag !== "Success") {43+ return null;44+ }45+ return result.value.threads.map((thread) => scopeThreadShell(environmentId, thread));46+}
- apps/web/src/lib/archivedThreadsState.ts line 73OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -28,0 +53,21 @@53+ });54+}55+56+/** One-shot fetch of an environment's archived thread shells. The archived57+ snapshot atom is only mounted while Settings → Archived is open, so a58+ synchronous read can miss; this awaits a fresh result instead, bounded so a59+ disconnected environment cannot hang the caller. Returns null on failure or60+ timeout so callers can fail soft. */61+export async function fetchArchivedThreadShells(62+ environmentId: EnvironmentId,63+): Promise<ReadonlyArray<EnvironmentThreadShell> | null> {64+ const result = await settleWithin(65+ executeAtomQuery(appAtomRegistry, archivedSnapshotAtom(environmentId), {66+ refresh: true,67+ reportDefect: false,68+ reportFailure: false,69+ }),70+ ARCHIVED_FETCH_TIMEOUT_MS,71+ null,72+ );73+ if (result === null || result._tag !== "Success") {apps/web/src/lib/archivedThreadsState.ts line 54OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -28,0 +34,21 @@34+ Environment queries never settle while the environment is connecting or35+ in backoff, so callers that must not hang wait a bounded time instead. */36+export function settleWithin<T, F>(37+ promise: Promise<T>,38+ timeoutMs: number,39+ fallback: F,40+): Promise<T | F> {41+ return new Promise((resolve) => {42+ const timer = setTimeout(() => resolve(fallback), timeoutMs);43+ promise.then(44+ (value) => {45+ clearTimeout(timer);46+ resolve(value);47+ },48+ () => {49+ clearTimeout(timer);50+ resolve(fallback);51+ },52+ );53+ });54+}
juliusmarminge closed this
Sign in to join this conversation.


macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed