Commit Graph

4 Commits

  • refactor(backend): phase 5 - optimize concurrency with RwLock and async IO
    Replace Mutex with RwLock for AppState.config to enable concurrent reads,
    improving performance for tray menu building and query operations that
    previously blocked each other unnecessarily.
    
    Key changes:
    - Migrate AppState.config from Mutex<MultiAppConfig> to RwLock<MultiAppConfig>
    - Distinguish read-only operations (read()) from mutations (write()) across
      all command handlers and service layers
    - Offload blocking file I/O in import/export commands to spawn_blocking threads,
      minimizing lock hold time and preventing main thread blocking
    - Extract load_config_for_import() to separate I/O logic from state updates
    - Update all integration tests to use RwLock semantics
    
    Performance impact:
    - Concurrent reads: Multiple threads can now query config simultaneously
      (tray menu, provider list, MCP config)
    - Reduced contention: Write locks only acquired during actual mutations
    - Non-blocking I/O: Config import/export no longer freezes UI thread
    
    All existing tests pass with new locking semantics.
  • refactor(backend): phase 4 - add test hooks and extend service layer
    - Extract internal functions in commands/mcp.rs and commands/provider.rs
      to enable unit testing without Tauri context
    - Add test hooks: set_mcp_enabled_test_hook, import_mcp_from_claude_test_hook,
      import_mcp_from_codex_test_hook, import_default_config_test_hook
    - Migrate error types from String to AppError for precise error matching in tests
    - Extend ProviderService with delete() method to unify Codex/Claude cleanup logic
    - Add comprehensive test coverage:
      - tests/mcp_commands.rs: command-level tests for MCP operations
      - tests/provider_service.rs: service-level tests for switch/delete operations
    - Run cargo fmt to fix formatting issues (EOF newlines)
    - Update BACKEND_REFACTOR_PLAN.md to mark phase 3 complete
  • refactor(backend): phase 4 - extract provider service layer
    Architecture improvements:
    - Extract ProviderService with switch/backfill/write methods
    - Reduce command layer from 160 to 13 lines via delegation
    - Separate business logic (services) from state management (commands)
    - Introduce precise error handling with structured validation
    
    Refactoring details:
    - Split Codex/Claude switching into symmetric private methods
    - Add multi-layer validation for Codex auth field (existence + type)
    - Extract import_config_from_path for command and test reuse
    - Expose export_config_to_file and ProviderService in public API
    
    Test coverage:
    - Add 10+ integration tests for Claude/Codex switching flows
    - Cover import/export success and failure scenarios (JSON parse, missing file)
    - Verify state consistency on error paths (current remains unchanged)
    - Test snapshot backfill for both old and new providers after switching
  • refactor(backend): phase 3 - unify error handling and fix backup timestamp bug
    Key improvements:
    - Extract switch_provider_internal() returning AppError for better testability
    - Fix backup mtime inheritance: use read+write instead of fs::copy to ensure latest backup survives cleanup
    - Add 15+ integration tests covering provider commands, atomic writes, and rollback scenarios
    - Expose write_codex_live_atomic, AppState, and test hooks in public API
    - Extract tests/support.rs with isolated HOME and mutex utilities
    
    Test coverage:
    - Provider switching with live config backfill and MCP sync
    - Codex atomic write success and failure rollback
    - Backup retention policy with proper mtime ordering
    - Negative cases: missing auth field, invalid provider ID