| name | refactoring-plan |
| description | Multi-phase refactoring and optimization plan with strict validation gates. Covers knowledge acquisition, API rationalization, project structure cleanup, code quality implementation (ruff, mypy, black), security and cost optimization, documentation, and emergency rollback protocols. |
| user-invokable | true |
| args | [{"name":"target","description":"The project to create a refactoring plan for (optional)","required":false}] |
Systematically refactor, clean up, and optimize the codebase. Work methodically through sequential phases with strict validation gates. DO NOT proceed to the next phase until the current phase is 100% complete with evidence.
Phase 0: Knowledge Acquisition (MANDATORY FIRST)
Complete this before making ANY code changes.
- Research RESTful API design patterns, Python project structure standards, code quality tooling configurations, auth/security patterns, and document processing architectures.
- Analyze the codebase: complete symbol mapping, duplicate code identification, API endpoint analysis, orphaned code detection, naming inconsistency audit.
Gate 0 Evidence Required:
Phase 1: API Endpoint Rationalization
- Design unified endpoint structure with parameter-based functionality.
- Standardize parameters:
ocr_mode, processing_mode, output_format, validation_level.
- Plan backward compatibility for existing clients.
Gate 1 Evidence Required:
Phase 2: Project Structure Cleanup
- Implement clean architecture:
api/, core/, models/, services/, utils/, config/, tests/.
- Eliminate ALL code duplication.
- Fix naming conventions across entire codebase.
Gate 2 Evidence Required:
Phase 3: Code Quality Implementation
- Implement tooling:
[tool.ruff]
select = ["E", "F", "W", "I", "N", "B", "A", "C90", "UP"]
line-length = 88
[tool.mypy]
python_version = "3.9"
strict = true
[tool.black]
line-length = 88
target-version = ['py39']
Gate 3 Evidence Required:
Phase 4: Security & Cost Optimization
- Input validation for all endpoints, secure token handling, rate limiting, secure file upload.
- Intelligent model selection, request caching/batching, cost monitoring, error handling to prevent API waste.
Gate 4 Evidence Required:
Phase 5: Documentation & Validation
- Complete API documentation, developer setup guide, architecture decision records, troubleshooting guides.
- End-to-end testing, performance validation, security re-verification.
Gate 5 Evidence Required:
Emergency Protocols
Rollback Strategies
- Phase-level git tags for reversion.
- Critical file backups before modification.
- API versioning for zero-downtime rollback.
- Backward-compatible database migrations.
Quality Gates That Cannot Be Bypassed
- All existing functionality continues working.
- No performance degradation >10%.
- Zero high/critical security vulnerabilities.
- Minimum 85% code coverage maintained.
- All public interfaces documented.
Validation Protocol
After Each Phase:
- Run comprehensive test suite.
- Perform static analysis validation.
- Generate evidence report.
- Get explicit confirmation before proceeding.
If Any Gate Fails:
- STOP immediately.
- Analyze root cause.
- Fix issues completely.
- Re-validate before proceeding.
Success Criteria