File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ import {
3434 watchStatusDir ,
3535 scanInitialStatuses ,
3636 writeStatusFile ,
37+ cleanupStaleStatuses ,
3738 SessionStatus ,
3839} from './session-status-hooks' ;
3940import {
@@ -1845,9 +1846,10 @@ ipcMain.handle('get-session-statuses', async () => {
18451846 const obj : Record < string , SessionStatus > = { } ;
18461847 fileStatuses . forEach ( ( v , k ) => { obj [ k ] = v ; } ) ;
18471848
1848- // Scan active sessions that don't have status files yet
1849+ // Scan active sessions that don't have status files yet + cleanup stale ones
18491850 try {
18501851 const activeMap = await detectActiveSessions ( ) ;
1852+ cleanupStaleStatuses ( new Set ( activeMap . keys ( ) ) ) ;
18511853 const allSessions = readClaudeSessions ( 500 ) ; // hoisted out of loop
18521854 const sessionsWithoutStatus = Array . from ( activeMap . entries ( ) )
18531855 . filter ( ( [ sessionId ] ) => ! obj [ sessionId ] )
Original file line number Diff line number Diff line change @@ -296,6 +296,23 @@ export const scanInitialStatuses = async (
296296 return statuses ;
297297} ;
298298
299+ /**
300+ * Clean up stale status files for sessions that are no longer active.
301+ * Call on startup to prevent accumulation from crashed sessions or missing SessionEnd hooks.
302+ */
303+ export const cleanupStaleStatuses = ( activeSessionIds : Set < string > ) : void => {
304+ try {
305+ if ( ! fs . existsSync ( STATUS_DIR ) ) return ;
306+ for ( const file of fs . readdirSync ( STATUS_DIR ) ) {
307+ if ( ! file . endsWith ( '.json' ) ) continue ;
308+ const sessionId = file . replace ( '.json' , '' ) ;
309+ if ( ! activeSessionIds . has ( sessionId ) ) {
310+ try { fs . unlinkSync ( path . join ( STATUS_DIR , file ) ) ; } catch { }
311+ }
312+ }
313+ } catch { }
314+ } ;
315+
299316/**
300317 * Write a status file for a session (used to persist JSONL-scanned statuses).
301318 */
You can’t perform that action at this time.
0 commit comments