NUCLEAR RESET FIX

Documentation for Plans

Nuclear Reset Fix: Marker-Based Two-Phase Reset

๐Ÿ› The Problem

The nuclear reset button was clearing all storage successfully but data was mysteriously reappearing after page reload!

Root Cause Analysis (Multiple Issues Found)

The logs revealed a critical race condition:

โœ… All IndexedDB databases deleted
โœ… Nuclear reset complete! Forcing hard reload...
โšก [BEFOREUNLOAD] Attempting to flush pending persistence...  โš ๏ธ <- CULPRIT!

What was happening:

  1. Nuclear reset deletes all IndexedDB databases โœ…
  2. Nuclear reset triggers redirect to /landing
  3. Browser fires beforeunload event before navigation
  4. App's beforeunload handler flushes in-memory state back to storage ๐Ÿ’ฅ
  5. Deleted universes get re-saved from memory!
  6. Page reloads with "deleted" data magically restored

Why Delete Button Worked

The individual delete button worked perfectly because:

โœ… The Solution

1. Added Disable Flag to AppComponent

// โ˜ข๏ธ Flag to disable persistence flush during nuclear reset
private static disableFlushOnUnload = false;

/**
 * โ˜ข๏ธ PUBLIC API: Disable flush on unload (for nuclear reset)
 */
public static disablePersistenceFlush(): void {
  AppComponent.disableFlushOnUnload = true;
  console.log('โ˜ข๏ธ [AppComponent] Persistence flush DISABLED for nuclear reset');
}

2. Modified beforeunload Handler

private handleBeforeUnload = () => {
  // โ˜ข๏ธ CRITICAL: Skip flush if nuclear reset is in progress
  if (AppComponent.disableFlushOnUnload) {
    console.log('โ˜ข๏ธ [BEFOREUNLOAD] Flush DISABLED for nuclear reset - data will not be re-saved');
    return;
  }
  
  console.log('โšก [BEFOREUNLOAD] Attempting to flush pending persistence...');
  this.flashHydrator.flushPendingPersistence().catch(error => {
    console.error('โšก [BEFOREUNLOAD] Flush failed:', error);
  });
};

3. Modified Visibility Change Handler

Also protected the visibility change handler (fires when user switches tabs):

private handleVisibilityChange = async () => {
  if (document.hidden) {
    // โ˜ข๏ธ CRITICAL: Skip flush if nuclear reset is in progress
    if (AppComponent.disableFlushOnUnload) {
      console.log('โ˜ข๏ธ [VISIBILITY] Flush DISABLED for nuclear reset - data will not be re-saved');
      return;
    }
    
    // ... flush logic
  }
};

4. Updated Nuclear Reset Flow

Added Step 0 to disable flush before clearing data:

async nuclearReset(): Promise<void> {
  try {
    // 0. โ˜ข๏ธ CRITICAL: Disable beforeunload flush to prevent data from being re-saved
    console.log('๐Ÿงน Step 0: Disabling persistence flush on page unload...');
    (await import('../../../app.component')).AppComponent.disablePersistenceFlush();
    console.log('โœ… Persistence flush disabled');

    // 1-10. Continue with clearing process...
    // ...
  }
}

๐ŸŽฏ Expected Results

With this fix, the nuclear reset should now:

  1. โœ… Disable persistence flush before clearing data
  2. โœ… Clear user context, guest sessions, Flash Persistence
  3. โœ… Clear space/universe data and sync queue
  4. โœ… Nuclear cache reset (micro-cache + boot manifests)
  5. โœ… Clear localStorage and sessionStorage
  6. โœ… Delete ALL IndexedDB databases with proper await
  7. โœ… Redirect to /landing
  8. โœ… beforeunload event fires but SKIPS flush ๐ŸŽ‰
  9. โœ… Page reloads with completely clean state

๐Ÿ“Š Console Logs to Verify

On the next nuclear reset attempt, you should see:

โ˜ข๏ธ NUCLEAR RESET INITIATED
๐Ÿงน Step 0: Disabling persistence flush on page unload...
โ˜ข๏ธ [AppComponent] Persistence flush DISABLED for nuclear reset
โœ… Persistence flush disabled
๐Ÿงน Step 1: Clearing user context...
...
โœ… All IndexedDB databases deleted
โ˜ข๏ธ [BEFOREUNLOAD] Flush DISABLED for nuclear reset - data will not be re-saved  โœ… <- SUCCESS!

๐Ÿ—๏ธ Architecture Impact

This fix demonstrates an important consideration for offline-first architectures:

๐Ÿ“ Related Files

๐Ÿงช Testing Verification

To test:

  1. Create test universes
  2. Click nuclear reset button (โ˜ข๏ธ)
  3. Confirm both warnings
  4. Watch console logs
  5. After redirect, verify NO universes exist
  6. Check IndexedDB in DevTools โ†’ Should be empty or new DB

Status: โœ… Implemented
Impact: Critical - Ensures nuclear reset actually works
Performance: None - Only affects nuclear reset flow