Our system had become powerful. We had dynamic agents, an intelligent orchestrator, learning memory, adaptive quality gates and a health monitor. But with power came complexity.
Looking at our codebase, we noticed a concerning "code smell": the logic related to quality and deliverables was scattered across multiple modules. There were functions in database.py
, executor.py
, and various files within ai_quality_assurance
and deliverable_system
. While each piece worked, the overall picture was becoming difficult to understand and maintain.
We were violating fundamental software engineering principles: Don't Repeat Yourself (DRY) and the Single Responsibility Principle. It was time to stop, not to add new features, but to refactor and consolidate.
Our strategy was to identify the key responsibilities that were scattered and consolidate them into dedicated service "engines". An "engine" is a high-level class that orchestrates a specific business capability from start to finish.
We identified two critical areas for consolidation:
This led us to create two new central components:
UnifiedQualityEngine
: The single reference point for all quality-related operations.UnifiedDeliverableEngine
: The single reference point for all deliverable creation operations.Reference commit code: a454b34 (feat: Complete consolidation of QA and Deliverable systems)
Architecture Before and After Consolidation:
Let's take deliverable creation. Before refactoring, our Executor
had to:
database.py
to get completed tasks.concrete_asset_extractor.py
to extract assets.deliverable_assembly.py
to assemble content.unified_quality_engine.py
to validate the result.database.py
again to save the deliverable.The Executor knew too many implementation details. It was a fragile architecture.
After refactoring, the process became incredibly simpler and more robust:
Reference code: backend/executor.py
(simplified logic)
# AFTER REFACTORING
from deliverable_system import unified_deliverable_engine
async def handle_completed_goal(workspace_id, goal_id):
"""
The Executor now only needs to make a single call to a single engine.
All complexity is hidden behind this simple interface.
"""
try:
await unified_deliverable_engine.create_goal_specific_deliverable(
workspace_id=workspace_id,
goal_id=goal_id
)
logger.info(f"Deliverable creation for goal {goal_id} successfully triggered.")
except Exception as e:
logger.error(f"Failed to trigger deliverable creation: {e}")
All the complex logic for extraction, assembly and validation is now contained within the UnifiedDeliverableEngine
, completely invisible to the Executor.
Our testing approach had to change. Instead of testing every small piece in isolation, we started writing integration tests that focused on the public interface of our new engines.
Reference code: tests/test_deliverable_system_integration.py
The test no longer called test_asset_extractor
and test_assembly
separately. Instead, it did one thing:
unified_deliverable_engine.create_goal_specific_deliverable(...)
.This approach made our tests more resilient to internal changes. We could completely change how assets were extracted or assembled; as long as the engine's public interface worked as expected, the tests continued to pass.
Complexity in a software project is not an event, it's a process. It tends to increase naturally over time, unless deliberate actions are taken to combat it.
We learned that refactoring is not something to do "when you have time". It's an essential maintenance activity, like changing the oil in a car. Stopping to consolidate and simplify the architecture allowed us to accelerate future development, because we now had much more stable and understandable foundations to build on.
✓ Actively Fight Complexity: Plan regular refactoring sessions to consolidate logic and reduce technical debt.
✓ Think in Terms of "Engines" or "Services": Group related functionality into high-level classes with simple interfaces. Hide complexity, don't expose it.
✓ Test Interfaces, not Details: Write integration tests that focus on the public behavior of your services. This makes tests more robust and less fragile to internal changes.
✓ Simplification is a Prerequisite for Scalability: You can't scale a system that has become too complex to understand and modify.
Chapter Conclusion
With a consolidated architecture and clean service engines, our system was now not only powerful, but also elegant and maintainable. We were ready for the final maturity exam: "comprehensive" tests, designed to stress the entire system and verify that all its parts, now well-organized, could work in harmony to achieve a complex goal from start to finish.