Skip to content

Phase 2b: Migrate agent.py to @with_error_handling (batches 76-77) #29

Closed

Description

@mrveiss

Scope

Migrate all 5 endpoints in backend/api/agent.py to use @with_error_handling decorator.

File: backend/api/agent.py (412 lines)

Endpoints to Migrate

Batch 76 (First 3 endpoints)

  1. receive_goal (POST /goal) - Lines 31-179

    • Has try-catch (lines 76-179)
    • Complex orchestrator integration
    • Security checks, permission validation, event publishing, Prometheus metrics
    • Pattern: Simple (remove outer try-catch)
  2. pause_agent_api (POST /pause) - Lines 182-205

    • No try-catch
    • Simple security check
    • Pattern: Simple (add decorator only)
  3. resume_agent_api (POST /resume) - Lines 208-233

    • No try-catch
    • Simple security check
    • Pattern: Simple (add decorator only)

Batch 77 (Final 2 endpoints)

  1. command_approval (POST /command_approval) - Lines 236-287

    • Has try-catch (lines 260-287)
    • Redis pub/sub integration
    • Pattern: Simple (remove outer try-catch)
  2. execute_command (POST /execute_command) - Lines 290-412

    • Has try-catch (lines 346-412)
    • Async subprocess command execution
    • Pattern: Simple (remove outer try-catch)

Migration Strategy

All endpoints use Simple Pattern:

  • Add @with_error_handling decorator with ErrorCategory.SERVER_ERROR
  • Remove all try-catch blocks
  • Preserve JSONResponse error returns for business logic (403 permission denied)
  • Preserve event publishing and Prometheus metrics

Expected Results

  • Code reduction: ~60-80 lines (complex exception handlers in receive_goal and execute_command)
  • Tests: ~20-25 tests (comprehensive coverage for all endpoints)
  • Commits: 2 per batch (migration + tests)

Progress Tracking

  • Batch 76: receive_goal, pause_agent_api, resume_agent_api
  • Batch 77: command_approval, execute_command

Related

Activity

  1. mrveiss commented on Oct 31, 2025

    @mrveiss
    OwnerAuthor

    ✅ Batch 76 Complete

    Migrated first 3 of 5 endpoints in agent.py.

    Endpoints Migrated

    1. receive_goal (POST /goal) - Lines 32-163

      • Pattern: Simple (removed try-catch, 22 lines saved)
      • Complex endpoint with orchestrator integration
      • Preserved: Prometheus metrics, event publishing, security audit, 403 permission check
    2. pause_agent_api (POST /pause) - Lines 166-194

      • Pattern: Simple (added decorator only, no try-catch to remove)
      • Preserved: 403 permission check
    3. resume_agent_api (POST /resume) - Lines 197-223

      • Pattern: Simple (added decorator only, no try-catch to remove)
      • Preserved: 403 permission check

    Results

    • Code reduction: 390 → 368 lines (22 lines saved, 5.6% reduction)
    • Tests: 11/11 passing
    • Commits: 9e5484a (migration), c0eabc4 (tests)

    Progress Tracking

    • Batch 76: receive_goal, pause_agent_api, resume_agent_api (3/5 endpoints, 60%)
    • Batch 77: command_approval, execute_command (remaining 2/5 endpoints)
  2. mrveiss commented on Oct 31, 2025

    @mrveiss
    OwnerAuthor

    ✅ Batch 77 Complete - agent.py 100% COMPLETE!

    Migrated final 2 of 5 endpoints in agent.py.

    Endpoints Migrated

    1. command_approval (POST /command_approval) - Lines 230-277

      • Pattern: Simple (removed try-catch, 11 lines saved)
      • Redis pub/sub integration for command approval workflow
      • Preserved: Redis client logic, security audit, 403 permission check
    2. execute_command (POST /execute_command) - Lines 280-393

      • Pattern: Simple (removed try-catch, 13 lines saved)
      • Async subprocess command execution with success/failure handling
      • Preserved: Subprocess execution, event publishing, security audit, 400/403 checks

    Results

    • Code reduction: 368 → 344 lines (24 lines saved, 6.5% reduction this batch)
    • Total for agent.py: 46 lines saved (390 → 344 lines, 11.8% reduction overall)
    • Tests: 10/10 passing (includes special test_agent_py_100_percent_complete validator)
    • Commits: c5d73d6 (migration), f27a436 (tests)

    File Summary

    agent.py is now 100% complete!

    • Total endpoints: 5/5 migrated (100%)
    • Total handlers: 3 try-catch blocks removed
    • Total code reduction: 46 lines saved (390 → 344 lines, 11.8%)
    • Total tests: 21 comprehensive tests (11 batch 76 + 10 batch 77)
    • Pattern breakdown:
      • receive_goal: Simple (22 lines saved)
      • pause_agent_api: Simple (no try-catch, decorator only)
      • resume_agent_api: Simple (no try-catch, decorator only)
      • command_approval: Simple (11 lines saved)
      • execute_command: Simple (13 lines saved)

    Progress Update

    Overall Phase 2b Progress:

    • 175/1,070 handlers migrated (16.36%)
    • ~1,025 lines saved across all files
    • 869 tests passing (859 previous + 21 agent.py)
    • 7 files 100% complete: knowledge.py, research_browser.py, monitoring.py, agent_terminal.py, agent_enhanced.py, agent_config.py, agent.py ✅

    This is the 7th file to reach 100% completion!

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