PERSISTENCE BUG CRITICAL

Documentation for Plans

✅ UNIVERSE PERSISTENCE BUG - WORKAROUND APPLIED

Issue: Universe disappears when navigating back to dashboard
Status: ✅ FRONTEND WORKAROUND APPLIED - Works Now!
Priority: P0 - RESOLVED (Temporary Fix)
Updated: November 3, 2025

✅ WORKING NOW!
Frontend detects when backend returns empty universeIds and keeps local version.
Universes now persist correctly across refreshes.
Backend investigation pending (needs restart for full diagnostics).
See: PERSISTENCE_BUG_FINAL_SOLUTION.md


✅ Workaround Applied (Working Now!)

Root Cause Confirmed: Backend receives universeIds correctly but returns empty array in response.

Evidence from Logs:

📤 Frontend sends: {"universeIds":["universe_xxx"]} ✅
   ↓ HTTP Request ↓
📥 Backend returns: {"universeIds":[]} ❌ BUG!

Frontend Workaround:

// In SpaceService.initializeFromApi()
if (!syncedSpace.universeIds || syncedSpace.universeIds.length === 0) {
  this.logger.warn('[SpaceService] ⚠️ Backend returned empty - KEEPING LOCAL');
  space = { ...syncedSpace, universeIds: space.universeIds };
}

File Changed: libs/core-angular/src/lib/services/space.service.ts

What It Does:

  1. Sends universeIds to backend ✅
  2. Receives response ✅
  3. Detects if backend returned empty ✅
  4. Keeps local version if empty ✅
  5. Logs warning ⚠️

Testing:

  1. Clear cache: clearCachedSpaceData()
  2. Create universe: "Test Workaround"
  3. Refresh page (F5)
  4. Universe persists! ✅
  5. See warning in console about backend bug

🔍 Bug Analysis (Historical)

What Happens

  1. User creates universe "Test Universe"
  2. SpaceService.createUniverse() is called
  3. Universe saved: ✅ storage.put('universe_test-universe', universe)
  4. Space updated: ✅ space.universeIds.push(universe.id)
  5. Space cached: ✅ cacheSpaceLocally(space)
  6. Signals updated: ✅ _universes.set([..._universes(), universe])
  7. User navigates to space-map: ✅
  8. User navigates to dashboard
  9. Dashboard calls unifiedData.refreshData()
  10. UnifiedData reloads space from storage
  11. Space loaded but universeIds might be stale
  12. Universe disappears! ❌

Root Cause

Race condition between space caching and data refresh:

// In SpaceService.createUniverse()
await this.cacheSpaceLocally(space);  // Async save
this._currentSpace.set(space);         // Update signal immediately

// Later, in UnifiedData.refreshData()
await this.loadSpace(spaceSlug);       // Reloads from storage
// If storage write didn't complete, gets old space without new universeId!
await this.loadUniverses();            // Can't find new universe

✅ Solution

Option 1: Force Sync Before Navigation (BEST)

In universe creation, ensure space is fully saved:

// After creating universe
await this.storage.put(`universe_${universe.id}`, universe);
space.universeIds.push(universe.id);
await this.cacheSpaceLocally(space);

// ADD THIS: Wait for storage to flush
await new Promise(resolve => setTimeout(resolve, 500));

// Then navigate

Option 2: Dashboard Reloads SpaceService Data

Make dashboard refresh SpaceService first:

async ngOnInit() {
  // Force SpaceService to reload from storage
  await this.spaceService.loadUniverses();
  // Then refresh unified data
  await this.unifiedData.refreshData();
}

Option 3: Use SpaceService Signal Directly

Instead of UnifiedData, dashboard reads from SpaceService:

// In dashboard
readonly universes = this.spaceService.universes;
// This always has latest data from signals

🎯 Recommended Fix

Combination of Option 1 + 2:

  1. In universe-creation: Wait longer before navigation (already done - 500ms)
  2. In dashboard: Force SpaceService refresh before UnifiedData refresh
  3. In SpaceService.createUniverse(): Ensure space cache completes before signal update

📝 Files to Modify

  1. apps/gograb-space/src/app/components/space-components/g-space-dashboard/g-space-dashboard.component.ts

    • Add spaceService.loadUniverses() before unifiedData.refreshData()
  2. libs/core-angular/src/lib/services/space.service.ts

    • Verify cacheSpaceLocally completes before signal updates

Priority: FIX NOW
Impact: CRITICAL - Users can't create universes without them disappearing