This repository was archived by the owner on Apr 15, 2026. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ interface PanelStateWithCustomData extends ToolPanelState {
4343 [ key : string ] : unknown ;
4444}
4545import { withLock } from '../../infrastructure/utils/mutex' ;
46+ import * as fs from 'fs' ;
4647import * as os from 'os' ;
4748import { 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'
Original file line number Diff line number Diff 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 ) ;
You can’t perform that action at this time.
0 commit comments