State Corruption - PowerMode session can start while previous session is active
State Corruption - PowerMode session can start while previous session is active
Labels: bug, state-management, medium-priority
Description
PowerModeSessionManager.beginSession() can be called multiple times without properly ending the previous session, causing nested sessions that corrupt user settings and prevent proper state restoration.
User Impact
-
Settings not restored correctly after app switching
-
Wrong AI prompts applied due to session conflicts
-
Confusing behavior when rapidly switching between apps with PowerMode configs
-
State corruption requiring app restart to recover
Technical Details
File: VoiceInk/PowerMode/PowerModeSessionManager.swift
Lines: 38-66
Current code allows nested sessions:
func beginSession(with config: PowerModeConfig) async {
// No check if session already active!
guard !isApplyingPowerModeConfig else { return }
let snapshot = ApplicationState(...)
currentSession = Session(...)
saveSession() // Overwrites previous session!
// ...
}
If user rapidly switches apps with different PowerMode configurations, the second beginSession() overwrites the first session's state snapshot, causing incorrect settings to be restored when the session ends.
Reproduction
-
Configure PowerMode for Safari and Chrome with different settings
-
Start recording in Safari (PowerMode activates)
-
Quickly switch to Chrome before stopping (PowerMode tries to activate again)
-
Original Safari settings are lost
-
When recording ends, wrong settings are restored
Recommended Fix
Add guard at the beginning of beginSession() (after line 42):
func beginSession(with config: PowerModeConfig) async {
guard let whisperState = whisperState, let enhancementService = enhancementService else {
print("SessionManager not configured.")
return
}
// Prevent nested sessions - end current session first
if loadSession() != nil {
print("⚠️ PowerMode session already active. Ending previous session.")
await endSession()
}
guard !isApplyingPowerModeConfig else { return }
// ... rest of existing implementation
}
Testing
-
Rapidly switch between apps with different PowerMode configs
-
Verify settings are correctly restored
-
Check console logs for warning messages about nested sessions
-
Confirm no session nesting occurs
-
Verify state restoration works correctly in all scenarios