Target State Alignment AI Pipeline - Implementation Plan

Target State Alignment AI Pipeline

Project Implementation Phases

This plan outlines the development of the "Target State Alignment AI Pipeline," broken down into four distinct epics. The diagram below illustrates the sequential flow of these implementation phases.

graph TD subgraph Target State Alignment AI Pipeline direction LR A[Epic 1: Foundation & Initialization] --> B[Epic 2: Iterative Refinement & Dynamic Validation]; B --> C[Epic 3: Failure Analysis & Resilience]; C --> D[Epic 4: Advanced Capabilities & Pilot]; end style A fill:#005b6d,stroke:#fff,stroke-width:2px,color:#fff style B fill:#007489,stroke:#fff,stroke-width:2px,color:#fff style C fill:#218993,stroke:#fff,stroke-width:2px,color:#fff style D fill:#7a9fa7,stroke:#fff,stroke-width:2px,color:#fff

Epic 1: Foundation and Initialization

Goal: Set up the core infrastructure and implement Workflow 1 (Initialization).

User Story 1.1: AI Agent Project Structure and Dockerization

As a DevOps engineer, I want to establish the foundational Python project structure and a lightweight Docker environment for the AI Agent so that it can run reliably within GitLab CI.

AI Prompt (Task: Generate Project Structure and Dockerfile)

Role: You are an expert Python and DevOps engineer setting up a new project called "AI Refinement Agent". This agent will interact with APIs (GitLab, LLMs) and manage local Git repositories.

Goal: Generate the necessary configuration files and a basic project structure.

Requirements:
  1. Use `Poetry` for dependency management.
  2. The project should have a CLI entry point (using `Typer`).
  3. Include standard Python best practices (e.g., `src` layout).
  4. Provide a `Dockerfile` optimized for running in GitLab CI.
Dockerfile Specifics:
  • Base Image: `python:3.11-slim`.
  • Must install `git` (essential for the agent's operation).
  • Install Poetry and dependencies efficiently.

Dependencies to include: `GitPython`, `python-gitlab`, `PyYAML`, `pydantic`, `typer`, and an LLM SDK (e.g., `openai`).

Output: Provide the content for `pyproject.toml`, `src/ai_agent/main.py` (Basic Typer setup with a placeholder command), and `Dockerfile`.

User Story 1.2: Implement the `StateManager` Module

As an AI engineer, I want to implement the `StateManager` module to robustly read, parse, validate, and update the `.ai` directory structure, ensuring the modernization state is consistent.

AI Prompt (Task: Implement `StateManager` Class)

Role: You are an expert Python developer implementing the core logic for the AI Refinement Agent.

Goal: Implement the `StateManager` class responsible for managing the `.ai` directory.

Context: The `.ai` directory structure includes `modernization_plan.yml` and `refinement_history.log` (JSONL).

Requirements:
  1. Use `Pydantic v2` to define models:
    • `TaskStatus` (Enum: PENDING, IN_PROGRESS, COMPLETED, FAILED).
    • `Task` (id: int, name: str, priority: int, status: TaskStatus).
    • `ModernizationPlan` (tasks: list[Task]).
    • `HistoryLogEntry` (timestamp: datetime, task_id: int, action: str, details: dict).
  2. The `StateManager` should be initialized with the project root path.
  3. Implement methods for:
    • `is_initialized()`: Verifies the `.ai` directory exists.
    • `load_plan()`: Reads and validates `modernization_plan.yml`.
    • `save_plan(plan: ModernizationPlan)`: Writes the plan back to YAML.
    • `get_next_task()`: Finds the highest priority `PENDING` task.
    • `update_task_status(task_id: int, status: TaskStatus)`
    • `log_history(...)`: Creates and appends a `HistoryLogEntry` to `refinement_history.log` (JSONL).
    • `load_failure_context()`: Reads `last_run_failure.md`.

Output: Provide the Python code for `src/ai_agent/models.py` and `src/ai_agent/state_manager.py`.

User Story 1.3: Implement the `GitManager` Module (Basic Operations)

As an AI engineer, I want the `GitManager` module to handle local repository operations and basic GitLab API interactions so that the agent can manipulate the codebase and create Merge Requests.

AI Prompt (Task: Implement `GitManager` Class)

Role: Expert Python developer specializing in Git automation.

Goal: Implement the foundational `GitManager` class.

Requirements:
  1. Use `GitPython` for local Git operations.
  2. Use `python-gitlab` for GitLab API interactions.
  3. The class should be initialized with GitLab connection details (URL, Project ID, Access Token) loaded securely.
  4. Implement methods for:
    • `initialize_repo(local_path: str)`
    • `create_and_checkout_branch(branch_name: str)`
    • `checkout_default_branch()`
    • `commit_changes(message: str)`: Stage all changes and commit.
    • `push_branch(branch_name: str)`
    • `create_merge_request(source_branch: str, target_branch: str, title: str, description: str)`: Create MR and set to remove source branch on merge.

Output: Provide the Python code for `src/ai_agent/git_manager.py`.

User Story 1.4: Implement the `Analyzer` and Codebase Scanning

As the AI Agent (Initialization Workflow), I need the capability to scan the project directory structure to identify languages, frameworks, and build tools so that I can establish the `current_state.md`.

AI Prompt (Task: Implement `Analyzer` Module)

Role: Expert Software Architect implementing a static analysis tool.

Goal: Implement the `Analyzer` module to scan a codebase and extract key technical details using heuristics.

Requirements:
  1. Recursively scan a given directory path.
  2. Identify key technologies based on file presence:
    • Build Tools: Detect Maven (`pom.xml`), Gradle (`build.gradle`), npm (`package.json`).
    • Languages: Detect primary languages (Java, Python, JS/TS).
    • Infrastructure: Detect `Dockerfile`, Terraform/OpenTofu files (`.tf`).
    • Tests: Identify common test directories (`src/test`, `tests`).
  3. The output should be a structured dictionary summarizing the findings, ready for LLM processing.
Example Output Structure:
{"languages": ["Java"], "build_tools": ["Maven"], "infra_files": ["Dockerfile"], "test_dirs": ["src/test/java"]}

Output: Provide the Python code for `src/ai_agent/analyzer.py`.

User Story 1.5: Develop Initialization Prompts

As an AI engineer, I need to develop and refine the core prompts for Workflow 1: synthesizing analysis results, analyzing test coverage, and generating the modernization plan.

AI Prompt (Task: Develop Workflow 1 Prompts)

Role: Expert Prompt Engineer specializing in software modernization.

Goal: Create the three primary prompts required for the AI Agent's Initialization Workflow.

Prompt 1: Current State Synthesis
  • Input: Raw structured data from the `Analyzer` module.
  • Output: A detailed Markdown document (`current_state.md`).
  • Requirements: Must clearly summarize Architecture, Tech Stack, Dependencies, Build Process, and Deployment Artifacts.
Prompt 2: Test Coverage Analysis
  • Input: Raw structured data from the `Analyzer` module (focusing on source vs. test file presence).
  • Output: A detailed Markdown document (`test_coverage_analysis.md`).
  • Requirements: Identify critical components that appear to lack tests. Focus on qualitative gaps based on structure.
Prompt 3: Modernization Plan Generation
  • Input: `current_state.md`, `test_coverage_analysis.md`, and `target_state_architecture.md` (TSA).
  • Output: A structured YAML file adhering to the `ModernizationPlan` Pydantic schema.
  • Critical Requirements:
    1. Bridge the gap between the current state and the TSA.
    2. Tasks must be small, iterative, and independently verifiable.
    3. Prioritize safety: Test coverage improvements (Priority 10) must come first.
    4. Foundational changes (build tools, containerization) come before major refactoring.

Output: Provide the full text for Prompt 1, Prompt 2, and Prompt 3, clearly indicating input placeholders.

Epic 2: Iterative Refinement and Dynamic Validation

Goal: Implement Workflow 2, enabling the agent to execute tasks and validate them using dynamic pipeline triggering.

User Story 2.1: Implement the `Orchestrator` and Workflow Routing

As an AI engineer, I want to implement the main `Orchestrator` logic that determines whether to run the Initialization or Iterative Refinement workflow.

AI Prompt (Task: Implement `Orchestrator` Logic)

Role: Expert Python developer implementing the control flow for the AI Agent.

Goal: Implement the `Orchestrator` class that manages the main execution flow.

Requirements:
  1. The `Orchestrator` initializes the `StateManager`, `GitManager`, `Analyzer`, and `LLMInterface`.
  2. Implement the `execute()` method.
  3. `execute()` logic:
    • Check status using `StateManager.is_initialized()`.
    • If initialized, call `run_refinement_workflow()`.
    • If not initialized, call `run_initialization_workflow()`.
  4. Implement the workflow methods, detailing the steps by coordinating the different managers (e.g., in `run_initialization_workflow()`, call analyzer, then LLMInterface for prompts, then StateManager to save, then GitManager to commit the `.ai` directory).

Output: Provide the Python code for `src/ai_agent/orchestrator.py`.

User Story 2.2: Develop the Task Execution Prompt

As the AI Agent (Refinement Workflow), I need a robust prompt that can take a specific task, analyze the relevant code, and generate the necessary code modifications, including incorporating failure context.

AI Prompt (Task: Develop Task Execution Prompt)

Role: Expert Prompt Engineer and Senior Software Developer.

Goal: Create the Master Task Execution Prompt for iteratively modernizing code.

Input Context Provided to the LLM:
  1. `[TASK_DETAILS]`: The specific task description from `modernization_plan.yml`.
  2. `[TSA_CONTEXT]`: Relevant snippets from the Target State Architecture.
  3. `[RELEVANT_FILES]`: Contents of files relevant to the task (Path: Content dictionary).
  4. `[FAILURE_CONTEXT]` (Optional): Content from `last_run_failure.md`.
Requirements for the Prompt:
  1. Instruct the LLM to act as an expert AI Software Engineer.
  2. Adhere strictly to the scope of the current task.
  3. Emphasize "Safety First": Do not break existing functionality.
  4. Crucial: If `[FAILURE_CONTEXT]` is provided, the LLM MUST understand the root cause and implement the suggested remediation strategy.
  5. The output must be a structured JSON object so the agent can easily parse and apply changes.
Required Output Format:
{
  "explanation": "A technical summary of the changes applied and any remediation strategy used.",
  "files": [
    {
      "path": "path/to/file.java",
      "action": "modify", // modify, create, delete
      "content": "[FULL NEW CONTENT OF THE FILE]"
    }
  ]
}

Output: Provide the full text of the Master Task Execution Prompt.

User Story 2.3: Implement Dynamic Pipeline Triggering and Monitoring

As a DevOps engineer, I need the `GitManager` to be capable of triggering the project's standard CI/CD pipeline on a feature branch and monitoring its status (success/failure) to validate AI-generated changes.

AI Prompt (Task: Enhance `GitManager` for Pipeline Management)

Role: Expert Python developer specializing in GitLab CI/CD automation.

Goal: Extend the existing `GitManager` class to handle dynamic pipeline triggering and monitoring using `python-gitlab`.

Assumptions: The `GitManager` class already exists and the GitLab client is initialized.

Requirements:
  1. Implement `trigger_pipeline(branch_name: str)`.
    • Use the GitLab API to create a new pipeline for the specified branch.
    • Return the pipeline object (including ID and URL).
  2. Implement `monitor_pipeline(pipeline_id: int, timeout_seconds=1800)`.
    • Poll the GitLab API periodically (e.g., every 30 seconds) to check the status.
    • Wait until the status is terminal (`success`, `failed`, `canceled`).
    • Handle timeouts.
    • Return the final status string.

Output: Provide the extended Python code for `src/ai_agent/git_manager.py` focusing on the new methods.

User Story 2.4: Develop the Merge Request Description Prompt

As a human reviewer, I want the AI-generated Merge Requests to have clear, comprehensive descriptions detailing the changes, the rationale, and the validation results.

AI Prompt (Task: Develop MR Description Prompt)

Role: Expert Technical Writer and Prompt Engineer.

Goal: Create a prompt to generate high-quality GitLab Merge Request descriptions.

Input Context Provided to the LLM:
  1. `[TASK_DETAILS]`: The completed task details.
  2. `[CHANGES_EXPLANATION]`: The "explanation" generated by the Task Execution Prompt.
  3. `[VALIDATION_RESULTS]`: Status (Success) and the URL to the pipeline run.
  4. `[TSA_CONTEXT]`: How this task aligns with the TSA.
Requirements for the Prompt:
  1. Output must be GitLab Flavored Markdown.
  2. Structure the description with the following sections:
    • AI Refinement Pipeline: Automatic Modernization (Header)
    • Task ID and Description
    • Rationale (Alignment with TSA)
    • Summary of Changes
    • Validation Status (Must include the link to the successful pipeline).

Output: Provide the full text of the MR Description Prompt.

Epic 3: Failure Analysis and Resilience

Goal: Implement Workflow 3, enabling the agent to analyze failures, document them, and adjust its strategy for subsequent runs (self-healing).

User Story 3.1: Implement Pipeline Log Retrieval

As the AI Agent (Failure Analysis Workflow), I need the ability to fetch the raw logs from the failed validation pipeline so that I can analyze the root cause of the failure.

AI Prompt (Task: Enhance `GitManager` for Log Retrieval)

Role: Expert Python developer specializing in GitLab API integration.

Goal: Extend the `GitManager` class to retrieve logs from a failed pipeline.

Requirements:
  1. Implement `get_failed_pipeline_logs(pipeline_id: int)`.
  2. Logic:
    • List all jobs in the pipeline via the GitLab API.
    • Identify jobs with the status 'failed'.
    • For each failed job, fetch the job trace (logs).
    • Concatenate the logs, clearly demarcating which log belongs to which job.

Output: Provide the extended Python code for `src/ai_agent/git_manager.py` focusing on the `get_failed_pipeline_logs` method.

User Story 3.2: Develop the Failure Analysis Prompt

As an AI engineer, I need a specialized prompt that can analyze raw pipeline logs and the attempted code changes to determine the root cause and suggest a remediation strategy.

AI Prompt (Task: Develop Failure Analysis Prompt)

Role: Expert Prompt Engineer and Debugging Specialist.

Goal: Create a prompt to analyze pipeline failures and enable self-correction, generating the content for `last_run_failure.md`.

Input Context Provided to the LLM:
  1. `[ATTEMPTED_TASK]`: Description of the task.
  2. `[ATTEMPTED_CHANGES_SUMMARY]`: The "explanation" from the failed execution attempt.
  3. `[RAW_PIPELINE_LOGS]`: Full logs from the failed jobs.
Requirements for the Prompt:
  1. Instruct the LLM to act as a senior developer debugging the failure.
  2. Pinpoint the exact error message, stack trace, or failed test case.
  3. Formulate a hypothesis for the root cause (e.g., incompatible dependency, missed configuration, unexpected API change).
  4. Propose a detailed, actionable strategy to fix the issue on the next attempt. This strategy must be precise enough for the Task Execution Prompt to utilize.
Required Output Structure (Markdown):
  • Observed Failure (including exact log snippets)
  • Root Cause Hypothesis
  • Suggested Remediation Strategy

Output: Provide the full text of the Failure Analysis Prompt.

User Story 3.3: Implement Failure Handling and Cleanup

As a DevOps engineer, when a modernization attempt fails validation, I want the system to automatically update the plan status to `failed`, document the failure, delete the temporary feature branch, and commit the updated state back to the main branch.

AI Prompt (Task: Implement Failure Handling in `Orchestrator`)

Role: Expert Python developer implementing the control flow for the AI Agent.

Goal: Implement the failure handling logic within the `Orchestrator` (Workflow 3 and the failure path of Workflow 2).

Context: This logic executes when `monitor_pipeline` returns 'failed'.

Requirements:
  1. Extend the `Orchestrator` to handle the failure path.
  2. If validation fails:
    • Retrieve logs using `GitManager.get_failed_pipeline_logs()`.
    • Generate analysis using the Failure Analysis Prompt (`LLMInterface`) and save to `.ai/last_run_failure.md`.
    • Update task status to 'FAILED' in `modernization_plan.yml`.
    • Log the failure in `refinement_history.log`.
    • Delete the failed feature branch (remotely) using `GitManager`.
    • Crucial: Switch back to the default branch, commit the updated `.ai` directory (plan, failure log), and push to the default branch. This ensures the next run has the correct state.

Output: Provide the updated Python code for `src/ai_agent/orchestrator.py`, focusing on the failure handling logic and the Git state commit process.

Epic 4: Advanced Capabilities and Pilot

Goal: Expand the agent's capabilities to handle complex tasks and validate the pipeline on real-world projects.

User Story 4.1: Implement Automated Test Generation Capability

As the AI Agent, I want the ability to automatically generate new unit or integration tests to ensure safety before major refactoring.

AI Prompt (Task: Develop Test Generation Prompt)

Role: Expert Prompt Engineer and QA Automation Specialist.

Goal: Create a specialized prompt variant of the Task Execution Prompt focused on generating automated tests.

Input Context Provided to the LLM:
  1. `[TASK_DETAILS]`: Specific area requiring coverage (e.g., "Add unit tests for UserService").
  2. `[TARGET_CODE]`: The source code of the class/module to be tested.
  3. `[PROJECT_CONTEXT]`: The frameworks used (e.g., JUnit 5, Mockito, Pytest).
  4. `[STYLE_EXAMPLES]` (Optional): Examples of existing tests to maintain consistency.
Requirements for the Prompt:
  1. Analyze the target code for execution paths, edge cases, and potential failures.
  2. Generate comprehensive tests covering these scenarios.
  3. Ensure tests adhere to the project's existing framework and style.
  4. Output must be complete, runnable test file(s) in the standard structured JSON format (as defined in Story 2.2).

Output: Provide the full text of the Test Generation Prompt.

User Story 4.2: Implement IaC Generation Capability (Containerization/Cloud)

As the AI Agent, when executing infrastructure-related tasks, I need the ability to generate configuration files like `Dockerfile` and Infrastructure-as-Code (e.g., Terraform/OpenTofu).

AI Prompt (Task: Develop IaC Generation Prompt)

Role: Expert Prompt Engineer and Cloud Architect (AWS/Terraform/OpenTofu/Docker).

Goal: Create a specialized prompt variant of the Task Execution Prompt focused on generating IaC.

Input Context Provided to the LLM:
  1. `[TASK_DETAILS]`: (e.g., "Create optimized Dockerfile", "Generate OpenTofu for Fargate").
  2. `[CURRENT_STATE]`: Application details (e.g., Java 21, Spring Boot, port 8080).
  3. `[TSA_REQUIREMENTS]`: Specific infrastructure standards (e.g., "Deploy to AWS Fargate," "Use approved base images," "IaC must be OpenTofu").
Requirements for the Prompt:
  1. Generate a multi-stage `Dockerfile` optimized for the application type, adhering to security best practices (e.g., non-root user) and TSA requirements.
  2. Generate the necessary OpenTofu (`.tf`) files to define the deployment infrastructure, adhering to the TSA.
  3. Output must be in the standard structured JSON format (as defined in Story 2.2).

Output: Provide the full text of the IaC Generation Prompt.

© 2025 AI Pipeline Project. All Rights Reserved.

Implementation Plan - Phase 1