From 509a2643852dfd7664e49da6bd31db085879a53f Mon Sep 17 00:00:00 2001 From: everpcpc Date: Tue, 10 Feb 2026 21:48:45 +0800 Subject: [PATCH] fix: clean up orphaned sessions with missing worktree paths on startup --- .../src/features/session/SessionManager.ts | 20 +++++++++++++++++++ packages/desktop/src/index.ts | 1 + 2 files changed, 21 insertions(+) diff --git a/packages/desktop/src/features/session/SessionManager.ts b/packages/desktop/src/features/session/SessionManager.ts index 2699a15..a685686 100644 --- a/packages/desktop/src/features/session/SessionManager.ts +++ b/packages/desktop/src/features/session/SessionManager.ts @@ -43,6 +43,7 @@ interface PanelStateWithCustomData extends ToolPanelState { [key: string]: unknown; } import { withLock } from '../../infrastructure/utils/mutex'; +import * as fs from 'fs'; import * as os from 'os'; import { panelManager } from '../panels/PanelManager'; @@ -372,6 +373,25 @@ export class SessionManager extends EventEmitter { this.emit('sessions-loaded', dbSessions.map(this.convertDbSessionToSession.bind(this))); } + cleanupOrphanedSessions(): void { + const allSessions = this.db.getAllSessionsIncludingArchived(); + let cleaned = 0; + for (const session of allSessions) { + if (session.is_main_repo) continue; + if (!session.worktree_path) continue; + if (fs.existsSync(session.worktree_path)) continue; + try { + this.deleteSessionPermanently(session.id); + cleaned++; + } catch { + // best-effort + } + } + if (cleaned > 0) { + console.log(`[SessionManager] Cleaned up ${cleaned} orphaned session(s) with missing worktree paths`); + } + } + private convertDbSessionToSession(dbSession: DbSession): Session { const toolTypeFromDb = (dbSession as DbSession & { tool_type?: string }).tool_type as 'claude' | 'codex' | 'gemini' | 'kimi' | 'none' | null | undefined; const normalizedToolType: 'claude' | 'codex' | 'gemini' | 'kimi' | 'none' = toolTypeFromDb === 'codex' diff --git a/packages/desktop/src/index.ts b/packages/desktop/src/index.ts index cbc8a49..02d2ce1 100644 --- a/packages/desktop/src/index.ts +++ b/packages/desktop/src/index.ts @@ -635,6 +635,7 @@ async function initializeServices() { sessionManager = new SessionManager(databaseService); sessionManager.initializeFromDatabase(); + sessionManager.cleanupOrphanedSessions(); gitExecutor = new GitExecutor(sessionManager); worktreeManager = new WorktreeManager(gitExecutor);