`/align-project-retrofit` Command: Brownfield Project Adoption
Hey guys! Ever felt like bringing an existing project into a new, shiny development environment is like trying to fit a square peg in a round hole? Well, that’s where the /align-project-retrofit command comes in. This article dives deep into the need for this command, the research backing it, a detailed implementation plan, and all the nitty-gritty details you need to know. So, grab your favorite beverage, and let's get started!
Why /align-project-retrofit? Addressing the Brownfield Challenge
In the world of software development, most projects aren’t brand new. They're what we call brownfield projects – existing codebases with their own history, quirks, and established ways of doing things. Unlike greenfield projects, which start with a clean slate, brownfield projects come with legacy patterns, technical debt, and established conventions. Integrating a new plugin, especially one as comprehensive as the autonomous-dev plugin, can be a real challenge without proper tooling.
The autonomous-dev plugin, while fantastic for new projects with standardized PROJECT.md files and organized file structures, isn't quite plug-and-play for these brownfield environments. This is where our /align-project-retrofit command steps in to save the day!
The Current Gap Explained
The core issue is that the autonomous-dev plugin is optimized for greenfield projects. These projects typically adhere to strict standards from the get-go, including well-defined PROJECT.md files, organized file structures, and clean git workflows. Brownfield projects, on the other hand, often lack these characteristics. Manually creating a PROJECT.md file and aligning the project structure can be a monumental task, making adoption difficult and risky. This command bridges this gap by providing a safe, incremental path for existing projects to adopt the autonomous-dev plugin.
Why Brownfield Support Matters
Consider these compelling reasons why brownfield support is crucial:
- The 95% Rule: Over 95% of software projects are brownfield. That's a massive chunk of the software world! Ignoring brownfield projects means missing out on a huge opportunity for improvement and automation.
- Manual Alignment is a Pain: The current plugin requires manual creation of
PROJECT.mdand structure alignment, which is time-consuming and error-prone. Imagine having to reorganize a messy room without any help – that’s what it feels like! - Incremental Adoption is Key: Teams need to adopt new technologies incrementally to minimize risk and disruption. A big-bang approach can lead to chaos and failed deployments.
- Safety First: Backup and rollback mechanisms are critical when dealing with production code. No one wants to break things in a live environment.
Alignment with Project Goals
This initiative aligns perfectly with the project's overall goals, specifically:
- PROJECT.md Sprint 7 Goal #6: This goal, currently in the “planned” status, highlights the importance of PROJECT.md files in our development process.
- Sprint 8 Focus: “Brownfield Adoption”: This sprint is dedicated to tackling the challenges of integrating the plugin into existing projects.
- SCOPE: Brownfield alignment is explicitly mentioned as a key area of focus, ensuring that our efforts are aligned with the project’s strategic direction.
Research-Backed Implementation: Patterns and Best Practices
Before diving into the implementation, a thorough research phase was conducted to identify existing patterns and best practices. This ensures that the /align-project-retrofit command is built on a solid foundation, incorporating lessons learned from the codebase and industry standards.
Existing Patterns in the Codebase
Our research uncovered several key patterns already present in the codebase, which we can leverage to build the new command:
- Tech Stack Detection (
project-bootstrapper.md): This agent analyzes dependency files (e.g.,package.json,pyproject.toml) to detect frameworks like Django, FastAPI, and Next.js. The beauty of this approach is that it’s non-invasive, operating in a read-only mode. - Backup/Rollback Safety (
plugin_updater.py): The existing update mechanism follows a robust pattern: Backup → Change → Verify → Cleanup OR Rollback. This ensures that we can always revert to a previous state if something goes wrong. Timestamped backups with restricted permissions (0o700) add an extra layer of security. - Atomic File Updates (
project_md_updater.py): Updates to critical files are performed atomically, meaning they either fully succeed or fully fail. This prevents partial writes and data loss. The system also detects merge conflicts before updates, avoiding potential headaches. - Security-First Path Validation (
security_utils.py): A four-layer whitelist defense protects against path traversal and symlink attacks. This is crucial for ensuring that user-supplied paths don’t escape their intended directories. - File Organization Validation (
enforce_file_organization.py): This hook validates the file organization against aproject-structure.jsontemplate and even provides auto-fix capabilities. It's like having a meticulous librarian for your codebase!
Best Practices from Research
Beyond our codebase, we also looked at industry best practices to inform our approach:
- Codebase Analysis: Start by analyzing dependency files to understand the tech stack. Map the file organization against a standardized structure and measure code quality metrics (LOC, Cyclomatic Complexity, Maintainability Index). This gives us a comprehensive picture of the project's current state.
- PROJECT.md Generation: The goal is to document the current reality, not the ideal state. Extract context from existing documentation (README, CONTRIBUTING, CHANGELOG) and structure the file with GOALS, SCOPE, and CONSTRAINTS based on the current situation.
- Alignment Gap Assessment: Use frameworks like the 12-Factor App methodology for standardized assessment. Quantify technical debt in person-hours to make the ROI clear to business stakeholders. A plugin compatibility checklist helps to identify specific areas for improvement.
- Incremental Migration: The Strangler Fig pattern is our friend here. Run the old and new systems in parallel during a gradual migration. Always back up before changes and rollback on failure. Non-destructive analysis is key – propose changes, show the impact, and request approval before making any modifications.
- Security & Safety: Whitelist-based path validation is far superior to blacklisting. Test-driven changes ensure that we write tests before modifying code. Atomic writes for critical files and merge conflict detection are essential for data integrity.
Security Considerations: A Top Priority
Security is paramount, especially when dealing with existing codebases. We’ve identified several potential security risks and incorporated mitigations into our plan:
- Path Traversal (CWE-22): The risk is that user-supplied paths might escape the intended directory. Our mitigation is a four-layer whitelist validation process.
- Symlink Following (CWE-59): An attacker could create a symlink to a sensitive file. We mitigate this by detecting symlinks before operations and re-validating after directory creation.
- Command Injection (CWE-78): Unvalidated names used in shell commands could lead to command injection. We’ll validate input length and format and use subprocess with a list (not
shell=True). - Code Analysis Safety: Running arbitrary code during analysis is a major risk. We’ll stick to static analysis only, never executing target code, and validating thoroughly before parsing.
Implementation Plan: A Phased Approach to Retrofitting
Our recommended approach is a phased retrofit command. This five-phase command safely analyzes existing codebases and incrementally aligns them with plugin expectations. Each phase is non-destructive until explicitly approved by the user, providing a high degree of control and safety.
Phase 1: Codebase Analysis
Command: /align-project-retrofit --analyze
What It Does: This phase runs the project-bootstrapper agent to extract the current state of the codebase. It analyzes dependencies, file organization, documentation, and code metrics. The output is a comprehensive analysis report, but no modifications are made at this stage.
Components:
- Enhanced
project-bootstrapperagent (~200 LOC additions): We’ll add code metrics analysis (LOC, Cyclomatic Complexity), documentation metadata extraction, and file organization mapping. - Analysis report generator (~150 LOC): This component formats the findings in structured markdown, including a metrics summary and highlighting potential issues.
Time: 5-10 minutes
User Action: Review the findings.
Phase 2: Alignment Gap Assessment
Command: /align-project-retrofit --assess
What It Does: This phase generates a PROJECT.md file for the current state (without modifying existing files). It assesses the project against the 12-Factor App principles (0-36 point score) and identifies critical gaps, minor gaps, and alignment requirements. The output is an assessment report with prioritized action items.
Components:
- 12-Factor assessment logic (~300 LOC): This implements the scoring system and maps the current project to the 12 factors, calculating an alignment percentage.
- Gap analysis engine (~250 LOC): This component compares the current state against plugin expectations, prioritizes gaps by impact/effort, and generates action items.
PROJECT.mdgenerator for brownfield (~200 LOC): This extracts current GOALS from the README, SCOPE from the code, and documents current CONSTRAINTS.
Time: 5-10 minutes
User Action: Review and prioritize gaps.
Phase 3: Migration Plan Generation
Command: /align-project-retrofit --plan
What It Does: This phase generates a step-by-step alignment plan, estimating the effort per step, impact, and risk. It shows what changes are needed, why, and the potential impact. The output is a migration guide (still non-modifying).
Components:
- Migration plan generator (~350 LOC): This breaks the alignment into incremental steps, estimates effort in person-hours, and assesses the risk per step.
- Impact analysis (~200 LOC): This component shows the files affected per step, predicts the test coverage impact, and identifies breaking changes.
Time: 5-10 minutes
User Action: Approve the migration plan.
Phase 4: Gradual Retrofit
Command: /align-project-retrofit --execute [--step=N]
What It Does: This phase executes the approved plan in steps, following the Backup → Change → Verify → Cleanup OR Rollback pattern. It shows progress (X/Y steps completed) and requests user confirmation after each major change. The output includes a modified PROJECT.md, file organization improvements, and hook configuration.
Components:
- Step-by-step executor (~400 LOC): This executes one step at a time, showing progress and previewing the next step. It requests approval for major changes.
- Backup/rollback integration (~150 LOC): This leverages existing
plugin_updater.pypatterns, creating timestamped backups before each phase and automatically rolling back on any failure. - Verification checkpoints (~200 LOC): This validates after each step, runs tests if available, and checks file integrity.
Time: 10-30 minutes (depends on complexity)
User Action: Approve each major step.
Phase 5: Validation
Command: /align-project-retrofit --verify
What It Does: This phase runs compliance checks to ensure that PROJECT.md exists, the file organization is aligned, and hooks are configured. It runs tests to ensure no breakage and outputs a compatibility report, indicating readiness for /auto-implement.
Components:
- Compliance checker (~250 LOC): This verifies the
PROJECT.mdstructure, file organization, and hook configuration. - Test runner integration (~100 LOC): This runs existing tests if available and reports the results.
- Compatibility report generator (~150 LOC): This shows the alignment status, lists any remaining gaps, and confirms readiness for autonomous development.
Time: 5-10 minutes
User Action: Confirm readiness for automation.
Interactive Mode: A User-Friendly Approach
Command: /align-project-retrofit --interactive
What It Does: This mode runs all five phases with user prompts between each phase. It provides a single command for a complete end-to-end workflow and is the most user-friendly option for first-time users.
Components:
- Interactive orchestrator (~200 LOC): This runs the phases sequentially, shows a progress indicator, and provides clear prompts.
Time: 30-60 minutes total
User Action: Follow prompts and approve at checkpoints.
Integration Points: Leveraging Existing Components
The /align-project-retrofit command will integrate with several existing agents and libraries:
- Existing Agents:
project-bootstrapper: We'll enhance this for deeper analysis.alignment-analyzer: We can reuse this for gap detection.project-progress-tracker: We'll update this after retrofit completion.
- Existing Libraries:
security_utils.py: Reuse path validation.plugin_updater.py: Reuse backup/rollback patterns.project_md_updater.py: Reuse atomic write patterns.
- Existing Hooks:
enforce_file_organization.py: Validate after retrofit.validate_project_alignment.py: VerifyPROJECT.mdcorrectness.
Estimated Complexity: A Medium-High Endeavor
The overall complexity is estimated to be medium-high, with the execution phase being the most complex due to its error-handling requirements.
Breakdown:
- Phase 1 (Analysis): Low - leverages existing patterns.
- Phase 2 (Assessment): Medium - new 12-factor logic, but straightforward.
- Phase 3 (Planning): Medium - plan generation requires careful UX design.
- Phase 4 (Execution): High - most complex, requires robust error handling.
- Phase 5 (Validation): Low - validation logic is simple.
Total Estimated LOC: ~2,500-3,000 lines (command + agent enhancements + libraries)
Total Estimated Effort: 2-3 weeks for a single developer.
Acceptance Criteria: Ensuring Quality and Functionality
To ensure that the /align-project-retrofit command meets our standards, we’ve defined a set of acceptance criteria covering functional requirements, quality standards, security requirements, documentation, testing, and performance.
Functional Requirements: What the Command Must Do
/align-project-retrofit --analyze: Successfully analyzes existing codebases, detecting the tech stack, mapping file organization, extracting documentation metadata, calculating code quality metrics, and generating an analysis report (non-modifying)./align-project-retrofit --assess: Successfully assesses alignment gaps, generating aPROJECT.mdfor the current state (no modifications), calculating a 12-Factor App score, identifying critical vs. minor gaps, prioritizing action items, and generating an assessment report./align-project-retrofit --plan: Successfully generates a migration plan, creating a step-by-step alignment plan, estimating effort per step, assessing risk per step, showing files affected per step, and generating a migration guide (markdown)./align-project-retrofit --execute: Successfully executes alignment, creating a timestamped backup before changes, executing the plan in incremental steps, requesting user approval for major changes, showing a progress indicator, automatically rolling back on failure, generating aPROJECT.mdfile, improving file organization (if needed), and configuring git hooks (if approved)./align-project-retrofit --verify: Successfully validates alignment, verifying thatPROJECT.mdexists and is valid, that the file organization matches the standard, and that hooks are configured (if applicable). It should also run tests if available (reporting results), generate a compatibility report, and confirm that the project is ready for/auto-implement./align-project-retrofit --interactive: Successfully runs all phases sequentially with prompts, shows a clear progress indicator, allows the user to stop at any phase, and provides helpful guidance at each step.
Quality Standards: The Guiding Principles
- Non-destructive by default: No files should be modified without explicit user approval.
- Safety mechanisms: All changes must have backup/rollback capability.
- Transparent: Users need to see what will change before execution.
- Incremental: Users should be able to adopt the changes gradually (phase by phase).
- Reversible: Users should be able to rollback any phase if needed.
Security Requirements: Protecting the Codebase
- All path operations must use
security_utils.validate_path()(CWE-22, CWE-59 prevention). - Input validation is required for all user-supplied values (CWE-20, CWE-78 prevention).
- Backup directories must have secure permissions (0o700 - user-only).
- No arbitrary code execution during analysis (static analysis only).
- All operations must be logged to the security audit log.
Documentation Requirements: Keeping Everyone in the Loop
- Command documentation in
plugins/autonomous-dev/commands/align-project-retrofit.md - Agent documentation updated (for
project-bootstrapperenhancements) CLAUDE.mdupdated with the new commandREADME.mdupdated with a brownfield adoption sectionCHANGELOG.mdupdated with the feature addition- Migration guide created in
docs/BROWNFIELD-ADOPTION.md
Testing Requirements: Ensuring Reliability
- Unit tests for all new functions (>80% coverage)
- Integration tests for each phase (
--analyze,--assess,--plan,--execute,--verify) - End-to-end test with a sample brownfield project
- Security tests for path validation and input sanitization
- Rollback tests (verify backup/restore works correctly)
Performance Requirements: Keeping Things Speedy
- The analysis phase should complete in <10 minutes for projects with <50,000 LOC.
- The assessment phase should complete in <10 minutes.
- The planning phase should complete in <10 minutes.
- The execution phase should provide progress updates every 30 seconds.
- The verification phase should complete in <10 minutes.
Alternative Approaches Considered: Weighing the Options
We didn’t just jump into the phased approach without considering other options. We explored two main alternatives:
Alternative 1: Minimal Retrofit (Lightweight)
Approach: A single command that only generates PROJECT.md and proposes changes (no execution).
Pros:
- Simpler implementation (single agent).
- Faster execution (analysis only).
- Lower risk (no file modifications).
- Easier to understand.
Cons:
- Less automation (users execute changes manually).
- More manual steps required.
- No backup/rollback safety.
- Lower value proposition.
Decision: Not recommended – doesn't provide enough value over a manual approach.
Alternative 2: Aggressive Retrofit (Full Automation)
Approach: A single command that fully automates alignment (multi-step, minimal user interaction).
Pros:
- Maximum automation.
- Fastest path to alignment.
- Leverages the plugin's autonomous philosophy.
Cons:
- Higher risk (multiple changes without review).
- Less transparency.
- May not suit risk-averse teams.
- Harder to undo if something breaks.
Decision: Not recommended – too risky for production brownfield codebases.
Why the Phased Approach Won
The phased retrofit approach strikes a balance between automation and safety. It offers:
- âś… Non-destructive analysis first (building confidence).
- âś… User control at each major step (reducing risk).
- âś… Backup/rollback for safety (enabling experimentation).
- âś… Transparency (users see the impact before changes).
- âś… Incremental adoption (adopt at your own pace).
This approach aligns with the autonomous-dev philosophy: “Automation > Reminders > Hope” while respecting the reality of production systems.
Dependencies: What We Need to Make It Happen
We’ll be leveraging existing components and adding new ones:
Required (Existing)
project-bootstrapperagent (will be enhanced)security_utils.pylibrary (path validation)plugin_updater.pylibrary (backup/rollback patterns)project_md_updater.pylibrary (atomic writes)
Required (New)
- 12-Factor assessment logic (new library)
- Migration plan generator (new library)
- Step executor with checkpoints (new library)
Optional (Future Enhancements)
- Integration with
/auto-implement(auto-start after retrofit) - Integration with
/status(track retrofit progress) - Brownfield project templates (pre-configured settings)
Breaking Changes: Keeping the Peace
There are no breaking changes planned. This is a new feature that doesn't modify existing functionality.
References: Digging Deeper
For those who want to delve further, here are some helpful references:
Code Files
plugins/autonomous-dev/agents/project-bootstrapper.md- Tech stack detection patternplugins/autonomous-dev/lib/plugin_updater.py- Backup/rollback safety patternplugins/autonomous-dev/lib/project_md_updater.py- Atomic file update patternplugins/autonomous-dev/lib/security_utils.py- Path validation patternplugins/autonomous-dev/hooks/enforce_file_organization.py- File organization validation
Documentation
.claude/PROJECT.mdlines 147-151 - Brownfield alignment scope.claude/PROJECT.mdline 831 - Sprint 7 Goal #6.claude/PROJECT.mdline 863 - Sprint 8 focusdocs/MAINTAINING-PHILOSOPHY.md- Core philosophy guideCLAUDE.md- Project instructions and best practices
External Resources
- 12-Factor App - Standardized assessment framework
- Strangler Fig Pattern - Incremental migration strategy
- NDepend Code Metrics - Code quality measurement
- Refactoring.Guru - Refactoring best practices
Related Issues
- Issue #58 - Automatic GitHub issue creation with research (this feature uses a similar research-first workflow)
- Sprint 7 Goal #6 - Brownfield adoption (PLANNED)
- Sprint 8 - Focus on brownfield adoption
Conclusion: A Bright Future for Brownfield Projects
The /align-project-retrofit command is a significant step towards making the autonomous-dev plugin accessible to a broader range of projects. By providing a safe, incremental, and transparent path for brownfield adoption, we're empowering teams to bring their existing codebases into the future. With its phased approach, robust security measures, and comprehensive testing, this command promises to be a valuable tool in any developer's arsenal. Get ready to embrace the power of automation, no matter the age of your project!
Priority: High (Sprint 8 focus)
Complexity: Medium-High
Estimated Effort: 2-3 weeks
Labels: enhancement, brownfield, Sprint-8, research-backed