# Modular Configuration Loading Architecture\n\n## Overview\n\nThe configuration system has been refactored into modular components to achieve 2-3x performance improvements\nfor regular commands while maintaining full functionality for complex operations.\n\n## Architecture Layers\n\n### Layer 1: Minimal Loader (0.023s)\n\n**File**: `loader-minimal.nu` (~150 lines)\n\nContains only essential functions needed for:\n\n- Workspace detection\n- Environment determination\n- Project root discovery\n- Fast path detection\n\n**Exported Functions**:\n\n- `get-active-workspace` - Get current workspace\n- `detect-current-environment` - Determine dev/test/prod\n- `get-project-root` - Find project directory\n- `get-defaults-config-path` - Path to default config\n- `check-if-sops-encrypted` - SOPS file detection\n- `find-sops-config-path` - Locate SOPS config\n\n**Used by**:\n\n- Help commands (help infrastructure, help workspace, etc.)\n- Status commands\n- Workspace listing\n- Quick reference operations\n\n### Layer 2: Lazy Loader (decision layer)\n\n**File**: `loader-lazy.nu` (~80 lines)\n\nSmart loader that decides which configuration to load:\n\n- Fast path for help/status commands\n- Full path for operations that need config\n\n**Key Function**:\n\n- `command-needs-full-config` - Determines if full config required\n\n### Layer 3: Full Loader (0.091s)\n\n**File**: `loader.nu` (1990 lines)\n\nOriginal comprehensive loader that handles:\n\n- Hierarchical config loading\n- Variable interpolation\n- Config validation\n- Provider configuration\n- Platform configuration\n\n**Used by**:\n\n- Server creation\n- Infrastructure operations\n- Deployment commands\n- Anything needing full config\n\n## Performance Characteristics\n\n### Benchmarks\n\n| Operation | Time | Notes |\n| --------- | ---- | ----- |\n| Workspace detection | 0.023s | 23ms for minimal load |\n| Full config load | 0.091s | ~4x slower than minimal |\n| Help command | 0.040s | Uses minimal loader only |\n| Status command | 0.030s | Fast path, no full config |\n| Server operations | 0.150s+ | Requires full config load |\n\n### Performance Gains\n\n- **Help commands**: 30-40% faster (40ms vs 60ms with full config)\n- **Workspace operations**: 50% faster (uses minimal loader)\n- **Status checks**: Nearly instant (23ms)\n\n## Module Dependency Graph\n\n```\nHelp/Status Commands\n    ↓\nloader-lazy.nu\n    ↓\nloader-minimal.nu (workspace, environment detection)\n    ↓\n     (no further deps)\n\nInfrastructure/Server Commands\n    ↓\nloader-lazy.nu\n    ↓\nloader.nu (full configuration)\n    ├── loader-minimal.nu (for workspace detection)\n    ├── Interpolation functions\n    ├── Validation functions\n    └── Config merging logic\n```\n\n## Usage Examples\n\n### Fast Path (Help Commands)\n\n```\n# Uses minimal loader - 23ms\n./provisioning help infrastructure\n./provisioning workspace list\n./provisioning version\n```\n\n### Medium Path (Status Operations)\n\n```\n# Uses minimal loader with some full config - ~50ms\n./provisioning status\n./provisioning workspace active\n./provisioning config validate\n```\n\n### Full Path (Infrastructure Operations)\n\n```\n# Uses full loader - ~150ms\n./provisioning server create --infra myinfra\n./provisioning taskserv create kubernetes\n./provisioning workflow submit batch.yaml\n```\n\n## Implementation Details\n\n### Lazy Loading Decision Logic\n\n```\n# In loader-lazy.nu\nlet is_fast_command = (\n    $command == "help" or\n    $command == "status" or\n    $command == "version"\n)\n\nif $is_fast_command {\n    # Use minimal loader only (0.023s)\n    get-minimal-config\n} else {\n    # Load full configuration (0.091s)\n    load-provisioning-config\n}\n```\n\n### Minimal Config Structure\n\nThe minimal loader returns a lightweight config record:\n\n```\n{\n    workspace: {\n        name: "librecloud"\n        path: "/path/to/workspace_librecloud"\n    }\n    environment: "dev"\n    debug: false\n    paths: {\n        base: "/path/to/workspace_librecloud"\n    }\n}\n```\n\nThis is sufficient for:\n\n- Workspace identification\n- Environment determination\n- Path resolution\n- Help text generation\n\n### Full Config Structure\n\nThe full loader returns comprehensive configuration with:\n\n- Workspace settings\n- Provider configurations\n- Platform settings\n- Interpolated variables\n- Validation results\n- Environment-specific overrides\n\n## Migration Path\n\n### For CLI Commands\n\n1. Commands are already categorized (help, workspace, server, etc.)\n2. Help system uses fast path (minimal loader)\n3. Infrastructure commands use full path (full loader)\n4. No changes needed to command implementations\n\n### For New Modules\n\nWhen creating new modules:\n\n1. Check if full config is needed\n2. If not, use `loader-minimal.nu` functions only\n3. If yes, use `get-config` from main config accessor\n\n## Future Optimizations\n\n### Phase 2: Per-Command Config Caching\n\n- Cache full config for 60 seconds\n- Reuse config across related commands\n- Potential: Additional 50% improvement\n\n### Phase 3: Configuration Profiles\n\n- Create thin config profiles for common scenarios\n- Pre-loaded templates for workspace/infra combinations\n- Fast switching between profiles\n\n### Phase 4: Parallel Config Loading\n\n- Load workspace and provider configs in parallel\n- Async validation and interpolation\n- Potential: 30% improvement for full config load\n\n## Maintenance Notes\n\n### Adding New Functions to Minimal Loader\n\nOnly add if:\n\n1. Used by help/status commands\n2. Doesn't require full config\n3. Performance-critical path\n\n### Modifying Full Loader\n\n- Changes are backward compatible\n- Validate against existing config files\n- Update tests in test suite\n\n### Performance Testing\n\n```\n# Benchmark minimal loader\ntime nu -n -c "use loader-minimal.nu *; get-active-workspace"\n\n# Benchmark full loader\ntime nu -c "use config/accessor.nu *; get-config"\n\n# Benchmark help command\ntime ./provisioning help infrastructure\n```\n\n## See Also\n\n- `loader.nu` - Full configuration loading system\n- `loader-minimal.nu` - Fast path loader\n- `loader-lazy.nu` - Smart loader decision logic\n- `config/ARCHITECTURE.md` - Configuration architecture details
