Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/desktop/src/features/session/SessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions packages/desktop/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ async function initializeServices() {

sessionManager = new SessionManager(databaseService);
sessionManager.initializeFromDatabase();
sessionManager.cleanupOrphanedSessions();

gitExecutor = new GitExecutor(sessionManager);
worktreeManager = new WorktreeManager(gitExecutor);
Expand Down
Loading