Skip to content

Commit 2d82d42

Browse files
grimmerkclaude
andcommitted
fix: cleanup stale status files on startup
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 98adfc5 commit 2d82d42

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
watchStatusDir,
3535
scanInitialStatuses,
3636
writeStatusFile,
37+
cleanupStaleStatuses,
3738
SessionStatus,
3839
} from './session-status-hooks';
3940
import {
@@ -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])

src/session-status-hooks.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff 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
*/

0 commit comments

Comments
 (0)