Skip to content

Phase 2b: Migrate agent_config.py to @with_error_handling (7 endpoints) #26

Closed

Description

@mrveiss

Overview

Migrate all endpoints in backend/api/agent_config.py from manual try-catch blocks to centralized @with_error_handling decorator.

Priority: 🟡 MEDIUM - Agent Configuration API
Parent Issue: #14


Scope

File: backend/api/agent_config.py
Endpoints: 7 endpoints (all need migration)
Exception Handlers: 13 total exception blocks
Estimated Batches: 3 batches (3+2+2 endpoints)


Migration Status

  • Batch 73: First 3 endpoints (GET /agents, GET /agents/{agent_id}, POST /agents/{agent_id}/model)
  • Batch 74: Next 2 endpoints (POST /agents/{agent_id}/enable, POST /agents/{agent_id}/disable)
  • Batch 75: Final 2 endpoints (GET /agents/{agent_id}/health, GET /status/overview)

Endpoints to Migrate

All 7 endpoints need migration:

  1. ⏳ GET /agents - list_agents (line 117)
  2. ⏳ GET /agents/{agent_id} - get_agent_config (line 176)
  3. ⏳ POST /agents/{agent_id}/model - update_agent_model (line 233)
  4. ⏳ POST /agents/{agent_id}/enable - enable_agent (line 293)
  5. ⏳ POST /agents/{agent_id}/disable - disable_agent (line 324)
  6. ⏳ GET /agents/{agent_id}/health - get_agent_health (line 357)
  7. ⏳ GET /status/overview - get_status_overview (line 433)

Migration Pattern Analysis Needed

Initial analysis suggests:

  • Most endpoints have outer try-catch with HTTPException patterns
  • Configuration validation error handling
  • Agent state management error patterns
  • Need to analyze each endpoint to determine Simple vs Mixed Pattern

Decorator config (expected):

@with_error_handling(
    category=ErrorCategory.SERVER_ERROR,
    operation="operation_name",
    error_code_prefix="AGENT_CONFIG",
)

Special considerations:

  • Agent configuration errors (not found, invalid state)
  • Model update validation errors
  • Enable/disable state transition errors
  • Health check error handling

Testing Requirements

  • Comprehensive tests for each batch (~12 tests per batch)
  • Configuration validation error handling verification
  • Business logic preservation (HTTPException for not found, invalid state)
  • Pattern compliance validation
  • Error handling configuration tests

Success Criteria

  • All 7 endpoints migrated
  • All 13 exception handlers replaced with decorator (or preserved if nested)
  • All tests passing (100%)
  • Estimated line savings: ~70-90 lines
  • No regression in agent configuration functionality
  • Consistent error responses across all agent config endpoints
  • Configuration validation preserved

Related

Activity

  1. mrveiss commented on Nov 15, 2025

    @mrveiss
    OwnerAuthor

    ✅ Verification Complete - Issue #26 ALREADY MIGRATED (100%)

    Verification Date: 2025-11-15

    Migration Status: ✅ COMPLETE

    File: backend/api/agent_config.py (469 lines)
    Migrated Endpoints: 7/7 (100%)
    Decorator Coverage: 7/7 @with_error_handling decorators (100%)
    Pattern Compliance: ✅ All endpoints using correct decorator pattern


    All Endpoints Migrated ✅

    1. GET /agents - list_agents (line 123)

    • Decorator: Lines 118-122 ✅
    • Operation: list_agents
    • Prefix: AGENT_CONFIG

    2. GET /agents/{agent_id} - get_agent_config (line 182)

    • Decorator: Lines 177-181 ✅
    • Operation: get_agent_config
    • Prefix: AGENT_CONFIG

    3. POST /agents/{agent_id}/model - update_agent_model (line 235)

    • Decorator: Lines 230-234 ✅
    • Operation: update_agent_model
    • Prefix: AGENT_CONFIG

    4. POST /agents/{agent_id}/enable - enable_agent (line 291)

    • Decorator: Lines 286-290 ✅
    • Operation: enable_agent
    • Prefix: AGENT_CONFIG

    5. POST /agents/{agent_id}/disable - disable_agent (line 320)

    • Decorator: Lines 315-319 ✅
    • Operation: disable_agent
    • Prefix: AGENT_CONFIG

    6. GET /agents/{agent_id}/health - check_agent_health (line 349)

    • Decorator: Lines 344-348 ✅
    • Operation: check_agent_health
    • Prefix: AGENT_CONFIG

    7. GET /status/overview - get_agents_overview (line 421)

    • Decorator: Lines 416-420 ✅
    • Operation: get_agents_overview
    • Prefix: AGENT_CONFIG

    Decorator Pattern Analysis

    All endpoints use correct pattern:

    @with_error_handling(
        category=ErrorCategory.SERVER_ERROR,
        operation="operation_name",
        error_code_prefix="AGENT_CONFIG",
    )
    @router.{method}("{path}")
    async def endpoint_function(...):
        # Business logic

    Try-Catch Block Analysis

    Total try-catch blocks: 1 (nested, preserved for granular error handling)

    Preserved nested try-catch (Mixed Pattern):

    • Line 366-391: Provider health check inside check_agent_health
      • Purpose: Graceful handling of provider availability check failures
      • Correctly preserved for specific error recovery
      • Does not wrap endpoint, only internal provider check logic

    No manual try-catch wrapping endpoints ✅


    Code Quality Assessment

    ✅ Strengths:

    • 100% decorator coverage across all endpoints
    • Consistent error code prefix (AGENT_CONFIG)
    • Proper HTTPException usage for not-found cases
    • Integration with unified_config_manager
    • Configuration validation and caching
    • Proper nested try-catch for provider health checks

    Business Logic Preservation:

    • ✅ Agent configuration management (get, update)
    • ✅ Agent enable/disable state transitions
    • ✅ Model update with provider configuration
    • ✅ Health checking with provider availability validation
    • ✅ Overview dashboard statistics
    • ✅ Cache invalidation after updates
    • ✅ Configuration persistence

    Integration Points:

    • unified_config_manager - Configuration management
    • ConfigService - Cache management
    • ProviderHealthManager - Provider health validation (line 372)
    • DEFAULT_AGENT_CONFIGS - Agent definitions
    • NetworkConstants - Network configuration

    Migration Impact

    Lines Saved: N/A (already migrated)
    Decorator Pattern: ✅ Fully compliant
    Error Handling: ✅ Centralized via @with_error_handling
    HTTPException Patterns: ✅ Preserved for business logic (404 not found)


    Verification Commands

    # Endpoint count
    grep -nE '@router\.(get|post)' backend/api/agent_config.py
    # Result: 7 endpoints ✅
    
    # Decorator count
    grep -n '@with_error_handling' backend/api/agent_config.py | wc -l
    # Result: 7 decorators ✅
    
    # Try-catch analysis
    grep -n 'try:' backend/api/agent_config.py
    # Result: Line 366 (nested provider health check) ✅

    Conclusion

    Status: ✅ COMPLETE - NO MIGRATION NEEDED

    agent_config.py was already fully migrated to @with_error_handling decorator pattern. All 7 endpoints have proper decorator coverage with consistent error code prefixes. The single nested try-catch block is correctly preserved for granular provider health check error handling.

    This issue can be closed as complete.


    Related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions