Skip to content

feat: Agent god class decomposition — extract chat, execution, and memory mixins #1214

@MervinPraison

Description

@MervinPraison

Summary

praisonaiagents/agent/agent.py is 8,915 lines with 189 methods. Decompose into focused mixins while maintaining 100% backward compatibility.

Current State

Existing mixins already extracted: chat_handler.py, tool_execution.py, session_manager.py, autonomy.py. But agent.py still has 8,915 lines.

Requirements

Round 1 — Extract chat/LLM methods → agent/chat_mixin.py

Move these methods from agent.py into a new ChatMixin class:

  • chat(), achat(), _process_agent_output(), _format_response(), _handle_tool_calls(), all streaming-related methods
  • Agent class inherits from ChatMixin
  • Target: ~1500 lines extracted

Round 2 — Extract run/execution methods → agent/execution_mixin.py

Move: run(), arun(), start(), astart(), run_until(), related helpers

  • Target: ~1200 lines extracted

Round 3 — Extract memory/cache methods → agent/memory_mixin.py

Move: _cache_put(), _cache_get(), _add_to_chat_history(), _truncate_chat_history(), related methods

  • Target: ~500 lines extracted

Architecture Rules (MUST follow)

  1. Use from praisonaiagents._logging import get_logger for logging (NOT logging.getLogger)
  2. Agent class MUST remain the single public API — users import from praisonaiagents import Agent
  3. All 34 __init__ params MUST remain unchanged
  4. All existing public methods MUST remain on Agent (via mixin inheritance)
  5. Zero features removed. 100% backward compatible.
  6. Type hints preserved on all methods

Verification (MUST run before creating PR)

# All tests must pass
pytest tests/unit/ -q --timeout=30
pytest tests/integration/test_agent_decomposition_real.py -v

# Real agentic test
python -c "
from praisonaiagents import Agent
agent = Agent(name=\"test\", instructions=\"You are helpful\")
result = agent.start(\"Say hello in one sentence\")
print(result)
"

# Import time check
python -c "import time; t=time.time(); import praisonaiagents; print(f'{(time.time()-t)*1000:.0f}ms')"

Success Criteria

  • agent.py reduced to ≤5000 lines
  • 0 public methods removed
  • 0 __init__ params removed
  • All 4473+ unit tests pass
  • All 56 integration tests pass
  • Import time stays <200ms
  • Real agentic test produces LLM output

Replaces old PR #977 approach. References issue #1161.

Metadata

Metadata

Assignees

No one assigned

    Labels

    claudeAuto-trigger Claude analysisenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions