Session 43 - AI Generation Pipeline Overhaul

Complete fix for AI module generation, registration, and tool integration

Session 43: Tool Integration & Universe State Isolation

Overview

This session addressed critical issues in the platform builder's tool integration, focusing on:

  1. Universe state isolation - Fixed generator showing stale state across universes
  2. Navigation routing - Ensured "Refine in Workbench" navigates to modularity-lib
  3. Code generation quality - Fixed naming issues and duplicate parameters
  4. Structure persistence - Enhanced empty save protection with backend verification

Critical Fixes

1. Universe State Isolation ⭐ CRITICAL

Problem: Vibe Universe Generator used a global sessionStorage key, causing cross-universe state pollution. Switching from Universe A (with generated modules) to Universe B (empty) would show "Platform Generated Successfully!" for the wrong universe.

Solution: Made storage keys universe-scoped:

// Before:
private readonly GENERATION_STATE_KEY = 'vibe-universe:generation-state';

// After:
private getGenerationStateKey(universeId: string): string {
  return `vibe-universe:${universeId}:generation-state`;
}

Impact:

Files Changed:

Details: universe-state-isolation.md


2. Universe Switch Detection

Added: Effect to automatically detect universe switches and reset generator state:

effect(() => {
  const currentUniverseId = this.universeContext.universeId();
  
  if (this.lastUniverseId && currentUniverseId !== this.lastUniverseId) {
    // Universe switched - clear stale state and restore for new universe
    this.mode.set('input');
    this.workflowState.reset();
    this.restoreGenerationState();
  }
  
  this.lastUniverseId = currentUniverseId;
});

Impact: Generator automatically resets when navigating between universes in Unified Builder.


3. Empty Canvas Save Protection (Enhanced)

Problem: Canvas could overwrite generated structure if its signal was stale.

Solution: Two-layer protection:

  1. Check signal (fast path)
  2. Verify with backend (safety net for stale signals)
if (!options?.allowEmpty && modules.length === 0) {
  const existingStructure = this.universeModuleService.structure(universeId)();
  if (existingStructure.length > 0) return; // Signal check
  
  // Double-check with backend
  const backendModules = await firstValueFrom(
    this.universeModuleService.load(universeId, { force: true })
  );
  if (backendModules.length > 0) return; // Backend check
}

Impact: Prevents race conditions where Vibe generates modules but Canvas hasn't refreshed its signal yet.

Files Changed:


4. Module Hydration 404 Fix

Problem: UniverseModuleService called non-existent /api/universes/:id?hydrate=true endpoint.

Solution: Switched to correct space-scoped endpoint:

// Before:
return this.moduleResolution.fetchAndHydrateUniverse(universeId); // 404

// After:
return this.spaceApi.hydrateUniverseStructure(space.id, universeId);

Files Changed:


Code Generation Improvements

1. Fixed Duplicate Parameters in Update Actions

Problem: Generated update actions had duplicate id parameters:

// BEFORE (Bug):
inputs: [
  { name: 'id', type: 'string', label: 'ID', required: true },
  ...entity.fields.map(f => ({ ...f, required: false }))
  // ❌ If entity has 'id' field, it appears twice!
]

Solution: Filter out id from entity fields:

// AFTER (Fixed):
inputs: [
  { name: 'id', type: 'string', label: 'ID', required: true },
  ...entity.fields
    .filter(f => f.name !== 'id') // ✅ Exclude id
    .map(f => ({ ...f, required: false }))
]

Files Changed:

2. Fixed "UpDate" Naming Bug

Problem: normalizeCompoundWords() was splitting "update" → "up Date" → "UpDate", causing files like up-date-item.action.ts.

Solution: Protected words before normalization:

const protectedWords = ['update', 'updated', 'updating'];
// Tokenize protected words before compound normalization
// Restore after normalization completes

Files Changed:


Structured Tree Generation

Cosmic Hierarchy

Vibe Universe Generator now produces proper structured trees instead of flat module lists:

platform-root (split-sidebar layout)
  ├── navigation (sidebar-nav)
  └── galaxy (layout-container)
      └── world modules (business logic with moduleRef)
          ├── Menu Management
          ├── Order Management
          └── Inventory Management

Benefits:

Key Changes:

Files Changed:


Stats


Testing Instructions

Test 1: Universe Isolation

  1. Open Unified Builder with Universe A
  2. Generate "Complete Healthcare" platform
  3. Verify success screen shows
  4. Navigate to Universe B (create new if needed)
  5. Expected: Generator shows input screen (not stuck)
  6. Navigate back to Universe A
  7. Expected: Success screen still shows (state preserved)

Test 2: Structure Persistence

  1. Generate platform in Unified Builder
  2. Verify modules appear in canvas
  3. Refresh page
  4. Expected: Modules still visible (structure persists)
  5. Edit canvas (move/resize sections)
  6. Expected: Generated modules not lost

Test 3: Code Quality

  1. Generate platform with multiple entities
  2. Check generated update-*.action.ts files
  3. Expected: No up-date-*.action.ts files
  4. Expected: Update actions have no duplicate id parameters

Handoff to Next Session

What's Working

Known Limitations

Recommendations for Next Session

  1. Make tree structure configurable - Read layout preferences from manifest
  2. Support multi-galaxy platforms - Admin portal + Public site
  3. Add navigation type detection - Sidebar vs topnav vs both
  4. Improve module placement AI - Intelligent route/slot inference
  5. Add "Refine Structure" button - Let users customize generated tree before deploy

Related Documentation


Session Complete: January 14, 2026
Agent: Cursor (Claude Sonnet 4.5)
Quality: Production-ready, all lints passing