Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.

Commit 509a264

Browse files
committed
fix: clean up orphaned sessions with missing worktree paths on startup
1 parent 46e1ae1 commit 509a264

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

packages/desktop/src/features/session/SessionManager.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ interface PanelStateWithCustomData extends ToolPanelState {
4343
[key: string]: unknown;
4444
}
4545
import { withLock } from '../../infrastructure/utils/mutex';
46+
import * as fs from 'fs';
4647
import * as os from 'os';
4748
import { panelManager } from '../panels/PanelManager';
4849

@@ -372,6 +373,25 @@ export class SessionManager extends EventEmitter {
372373
this.emit('sessions-loaded', dbSessions.map(this.convertDbSessionToSession.bind(this)));
373374
}
374375

376+
cleanupOrphanedSessions(): void {
377+
const allSessions = this.db.getAllSessionsIncludingArchived();
378+
let cleaned = 0;
379+
for (const session of allSessions) {
380+
if (session.is_main_repo) continue;
381+
if (!session.worktree_path) continue;
382+
if (fs.existsSync(session.worktree_path)) continue;
383+
try {
384+
this.deleteSessionPermanently(session.id);
385+
cleaned++;
386+
} catch {
387+
// best-effort
388+
}
389+
}
390+
if (cleaned > 0) {
391+
console.log(`[SessionManager] Cleaned up ${cleaned} orphaned session(s) with missing worktree paths`);
392+
}
393+
}
394+
375395
private convertDbSessionToSession(dbSession: DbSession): Session {
376396
const toolTypeFromDb = (dbSession as DbSession & { tool_type?: string }).tool_type as 'claude' | 'codex' | 'gemini' | 'kimi' | 'none' | null | undefined;
377397
const normalizedToolType: 'claude' | 'codex' | 'gemini' | 'kimi' | 'none' = toolTypeFromDb === 'codex'

packages/desktop/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ async function initializeServices() {
635635

636636
sessionManager = new SessionManager(databaseService);
637637
sessionManager.initializeFromDatabase();
638+
sessionManager.cleanupOrphanedSessions();
638639

639640
gitExecutor = new GitExecutor(sessionManager);
640641
worktreeManager = new WorktreeManager(gitExecutor);

0 commit comments

Comments
 (0)