Self-Healing and Code Repair
Relevant source files
The following files were used as context for generating this wiki page:The DevSkyy project incorporates a comprehensive, multi-agent system for automated code repair and self-healing. This system is designed to autonomously detect, diagnose, and fix a wide range of issues, including syntax errors, security vulnerabilities, performance bottlenecks, and code quality deviations. It leverages a combination of static analysis, AI-powered models (like Claude and GPT-4), and a continuous learning mechanism to maintain and improve the codebase with minimal human intervention.
The system is composed of several key agents that can work independently or in concert, orchestrated by higher-level modules. These include scanners for issue detection, fixers for applying repairs, and integrated workflows for end-to-end automation. This automated approach is a core part of the platform's strategy for ensuring code quality, security, and reliability. For related processes, see the Code Review Guide and Security Policy.
Sources: agent/modules/backend/universal_self_healing_agent.py:8-20, code_repair.md
System Architecture and Core Components
The Self-Healing and Code Repair system is built around a set of specialized agents, each with a distinct role in the process. These agents can be orchestrated to provide a full-cycle, autonomous repair workflow.
The overall workflow is as follows:
This diagram shows the high-level process of scanning for issues, generating and validating fixes, and applying them, with a rollback mechanism for safety.
Sources: agent/modules/backend/universal_self_healing_agent.py:126-161, agent/modules/backend/enhanced_autofix.py
Agent Capabilities
The system comprises several agents, each with increasing levels of sophistication.
| Agent | Key Purpose | Features | Source File |
|---|---|---|---|
| Scanner V2 | Enterprise-grade scanning with ML. | Multi-threaded scanning, security vulnerability detection (OWASP), dependency analysis, performance bottleneck identification. | scanner_v2.py |
| Fixer V2 | AI-powered enterprise code repair. | AI-generated fixes (Claude/OpenAI), security patching, performance optimization, dry-run mode. | fixer_v2.py |
| Enhanced AutoFix | Complete automated repair workflow. | Integrates Scanner & Fixer, automatic Git branch creation, multi-stage fixing, automated commits, and reporting. | enhanced_autofix.py |
| Universal Self-Healing Agent | Autonomous, multi-language repair system. | Continuous learning, pattern recognition for proactive prevention, multi-model AI, real-time monitoring. | universal_self_healing_agent.py |
| Codex Orchestrator | AI-powered orchestration of healing agents. | Uses GPT-4 to generate healing strategies, coordinates multiple agents, validates fixes, and learns from successful repairs. | ml/codex_orchestrator.py |
Sources: code_repair.md, agent/modules/backend/universal_self_healing_agent.py, ml/codex_orchestrator.py
Issue Detection and Scanning
Issue detection is the first and most critical step in the self-healing process. The UniversalSelfHealingAgent and ScannerAgentV2 employ a multi-pronged approach to identify a broad spectrum of problems.
Detection Methods
The system uses four primary methods for issue detection:
- Static Analysis: For specific languages like Python,
astroidis used to parse the code and find issues like unused imports and missing docstrings. - Linters: Language-specific linters (e.g.,
pylint,eslint,sqlfluff) are executed to catch syntax, style, and common errors. - AI-Powered Detection: AI models (Claude) are used to analyze code for subtle logic errors, security vulnerabilities, and performance issues that traditional linters might miss.
- Learned Patterns: The system checks code against a database of previously identified and successfully fixed error patterns for proactive detection.
Sources: agent/modules/backend/universal_self_healing_agent.py:181-193
This diagram illustrates the flow of the detection process:
Security Scanning
A key feature of the scanning process is the detection of security vulnerabilities. The ScannerAgentV2 is pre-configured to identify common security patterns.
| Pattern Key | Description |
|---|---|
hardcoded_secret | Detects passwords, secrets, or API keys in assignments. |
sql_injection | Finds SQL queries built with string concatenation. |
xss_vulnerability | Identifies usage of innerHTML or dangerouslySetInnerHTML. |
command_injection | Flags use of os.system or subprocess with user input. |
path_traversal | Looks for ../ patterns in file operations. |
insecure_crypto | Detects usage of MD5 or SHA1 for passwords. |
Sources: code_repair.md
Code Repair and Healing
Once issues are detected, the system employs a sophisticated, multi-stage process to generate, apply, and validate fixes.
AI-Powered Fix Generation
The FixerAgentV2 and UniversalSelfHealingAgent use AI models to generate fixes. The process involves sending the problematic code along with the issue description to an AI model (like Claude) and asking for a complete, corrected version of the code.
The CodexOrchestrator takes this a step further by first using GPT-4 to devise a "healing strategy" before generating fixes. This allows for a more holistic repair that considers dependencies between issues.
Sources: agent/modules/backend/universal_self_healing_agent.py:448-498, ml/codex_orchestrator.py:181-218
The sequence diagram below shows the AI-orchestrated healing process from CodexOrchestrator.
This diagram illustrates the five phases of healing: Scan (review), Analyze (strategy), Fix (generate), Validate, and Learn (not shown but happens after successful validation).
Sources: ml/codex_orchestrator.py:100-150, CODEX_INTEGRATION.md
Enhanced AutoFix Workflow
The EnhancedAutoFix module provides a high-level, end-to-end workflow that integrates scanning, fixing, and version control.
The workflow proceeds in the following stages:
- Initialization: Creates a new Git branch for the fixes and backs up the current state.
- Scanning: Performs a comprehensive analysis of the code.
- Basic Fixes: Applies simple, automated fixes (e.g., formatting).
- Advanced Fixes: Uses AI to generate more complex improvements.
- Verification: Re-scans the code to confirm that all reported issues have been resolved.
- Commit: Automatically commits the changes to the new branch with a detailed message.
- Report: Generates a summary of the fixes applied.
Sources: code_repair.md, agent/modules/backend/enhanced_autofix.py
This process can be visualized as follows:
The run_enhanced_autofix function accepts several parameters to control this workflow.
| Parameter | Type | Description |
|---|---|---|
create_branch | bool | If True, creates a new Git branch for the fixes. |
branch_name | str | The name of the branch to create. |
auto_commit | bool | If True, automatically commits the changes. |
fix_types | list | A list of fix categories to apply, such as syntax, security, performance. |
Sources: code_repair.md
Fix Validation and Rollback
A crucial safety feature is the validation and rollback mechanism. After a fix is applied, the UniversalSelfHealingAgent re-validates the file.
Validation includes:
- Re-running linters to ensure no new issues were introduced.
- Performing a syntax check.
- Running associated tests (
pytest,jest, etc.) if configured.
If validation fails, the agent automatically reverts the file to its original state from a backup, preventing the introduction of broken code.
Sources: agent/modules/backend/universal_self_healing_agent.py:500-543
Universal Self-Healing Agent
The UniversalSelfHealingAgent is the most advanced component of the system, designed for full autonomy.
This diagram shows the key properties and methods of the UniversalSelfHealingAgent, highlighting its dependencies on AI clients and its internal state for learning.
Sources: agent/modules/backend/universal_self_healing_agent.py:23-595
Key Features
- Multi-Language Support: The agent is designed to be language-agnostic, with explicit support and configurations for Python, JavaScript, TypeScript, PHP, CSS, HTML, SQL, Rust, and Go.
- Continuous Learning: After every successful healing operation, the agent calls
_learn_from_healingto store the issue pattern and the successful fix. This knowledge is used to identify and repair similar issues more quickly and confidently in the future. - Configurability: The agent's behavior can be fine-tuned through the
healing_configdictionary.
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
auto_heal | bool | True | Enables or disables automatic healing. |
max_healing_attempts | int | 3 | The maximum number of times to attempt healing a file. |
learning_rate | float | 0.95 | Controls how quickly the agent adapts to new patterns. |
confidence_threshold | float | 0.85 | The minimum AI confidence score required to auto-apply a fix. |
backup_before_heal | bool | True | Creates a .backup file before modifying code. |
run_tests_after_heal | bool | True | Executes associated tests to validate a fix. |
validate_security | bool | True | Performs a security validation post-healing. |
Sources: agent/modules/backend/universal_self_healing_agent.py:107-116
Conclusion
The Self-Healing and Code Repair system in DevSkyy is a powerful, multi-layered framework that automates the maintenance of code health. By combining traditional static analysis with advanced AI-driven strategy and repair, it provides a robust mechanism for improving code quality, bolstering security, and reducing the manual effort required for codebase maintenance. The system's autonomous nature, continuous learning capabilities, and safety features like validation and rollback make it a cornerstone of the project's development and operational strategy.
Sources: agent/modules/backend/universal_self_healing_agent.py, code_repair.md