fix: improve stability with memory leak fixes and atomic writes

Session Manager:
- Store event handlers for proper cleanup on session stop
- Add resetSessionManager() for test isolation

State Store:
- Use atomic write pattern (temp file + rename) to prevent corruption
- Add tokenStats to AppState interface

Subagent Watcher:
- Clean up pendingToolCalls on agent completion
- Add cleanupStaleAgents() for 24h+ old agents
- Clear all state maps on stop() for clean restart

Web Server:
- Remove listeners from transcript watcher before stopping
- Clean up respawn timers when session is deleted
- Use cleanupSession() in spawn orchestrator for proper resource cleanup

Frontend:
- Close existing EventSource before reconnecting to prevent duplicates
- Close failed connection before scheduling reconnect

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
arkon
2026-01-26 12:35:00 +01:00
co-authored by Claude Opus 4.5
parent 6e4564abd1
commit fe334c2ea6
6 changed files with 131 additions and 37 deletions
+11
View File
@@ -965,11 +965,22 @@ class ClaudemanApp {
// ========== SSE Connection ==========
connectSSE() {
// Close existing EventSource before creating new one to prevent duplicate connections
if (this.eventSource) {
this.eventSource.close();
this.eventSource = null;
}
this.eventSource = new EventSource('/api/events');
this.eventSource.onopen = () => this.setConnectionStatus('connected');
this.eventSource.onerror = () => {
this.setConnectionStatus('disconnected');
// Close the failed connection before scheduling reconnect
if (this.eventSource) {
this.eventSource.close();
this.eventSource = null;
}
setTimeout(() => this.connectSSE(), 3000);
};