Skip to content

[P3] Refactor: Decompose LeadAgent monolith (110KB) into focused modules #276

@frankbria

Description

@frankbria

Problem

lead_agent.py at 110KB is a monolith that violates Single Responsibility Principle. It handles orchestration, discovery, planning, and conversation management all in one file.

Current State

  • File: codeframe/agents/lead_agent.py (~110KB, ~3000+ lines)
  • Combines: Agent orchestration, Discovery workflow, Planning pipeline, Conversation/chat, PRD generation

Impact

  • Hard to maintain and debug
  • Changes to discovery can break planning
  • Testing is complex due to tight coupling
  • New developers struggle to understand flow

Required Decomposition

New Module Structure

codeframe/agents/
├── lead_agent.py              # Thin orchestrator (reduced to ~500 lines)
├── coordinators/
│   ├── __init__.py
│   ├── discovery_coordinator.py    # Socratic Q&A workflow
│   ├── planning_coordinator.py     # Issue/task generation
│   └── agent_coordinator.py        # Worker agent management
└── managers/
    ├── __init__.py
    └── conversation_manager.py     # Chat persistence

Responsibility Split

Module Responsibility
LeadAgent High-level orchestration, state transitions
DiscoveryCoordinator Socratic questions, answer processing
PlanningCoordinator PRD → Issues → Tasks pipeline
AgentCoordinator Worker spawning, task assignment
ConversationManager Chat history, context persistence

Acceptance Criteria

  • LeadAgent reduced to <500 lines
  • Each coordinator is independently testable
  • No change to external API/behavior
  • All existing tests pass
  • Coverage maintained at >85%

Migration Strategy

  1. Extract DiscoveryCoordinator first (highest complexity)
  2. Extract PlanningCoordinator
  3. Extract AgentCoordinator
  4. Extract ConversationManager
  5. Thin LeadAgent to orchestrator only

Committee Report Reference

System Architect and Python Expert both flagged LeadAgent as "architectural smell". Quote: "Doing too much - orchestration, discovery, planning, conversation."

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3-post-betaLow priority - can wait until after beta (v2.0)architectureSystem architecture and design patternsenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions